Flutter Engine
The Flutter Engine
DawnSampler.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2022 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
9
13
14#include <cfloat>
15
16namespace skgpu::graphite {
17
18namespace {
19
20wgpu::FilterMode filter_mode_to_dawn_filter_mode(SkFilterMode mode) {
21 switch (mode) {
23 return wgpu::FilterMode::Nearest;
25 return wgpu::FilterMode::Linear;
26 }
28}
29
30wgpu::MipmapFilterMode mipmap_mode_to_dawn_filter_mode(SkMipmapMode mode) {
31 switch (mode) {
33 // Dawn doesn't have none filter mode.
34 return wgpu::MipmapFilterMode::Nearest;
36 return wgpu::MipmapFilterMode::Nearest;
38 return wgpu::MipmapFilterMode::Linear;
39 }
41}
42}
43
44DawnSampler::DawnSampler(const DawnSharedContext* sharedContext,
45 wgpu::Sampler sampler)
46 : Sampler(sharedContext)
47 , fSampler(std::move(sampler)) {}
48
49static inline wgpu::AddressMode tile_mode_to_dawn_address_mode(SkTileMode tileMode) {
50 switch (tileMode) {
52 return wgpu::AddressMode::ClampToEdge;
54 return wgpu::AddressMode::Repeat;
56 return wgpu::AddressMode::MirrorRepeat;
58 // Dawn doesn't support this mode.
59 return wgpu::AddressMode::ClampToEdge;
60 }
62}
63
65 const SkSamplingOptions& samplingOptions,
66 SkTileMode xTileMode,
67 SkTileMode yTileMode) {
68 wgpu::SamplerDescriptor desc;
69 desc.addressModeU = tile_mode_to_dawn_address_mode(xTileMode);
70 desc.addressModeV = tile_mode_to_dawn_address_mode(yTileMode);
71 desc.magFilter = filter_mode_to_dawn_filter_mode(samplingOptions.filter);
72 desc.minFilter = desc.magFilter;
73 desc.mipmapFilter = mipmap_mode_to_dawn_filter_mode(samplingOptions.mipmap);
74 desc.lodMinClamp = 0.0f;
75 if (samplingOptions.mipmap == SkMipmapMode::kNone) {
76 // Disabling mipmap by clamping max lod to first level only.
77 desc.lodMaxClamp = 0.0f;
78 } else {
79 desc.lodMaxClamp = FLT_MAX;
80 }
81 desc.maxAnisotropy = 1;
82 desc.compare = wgpu::CompareFunction::Undefined;
83
84 std::string label;
85 if (sharedContext->caps()->setBackendLabels()) {
86 static const char* tileModeLabels[] = {"Clamp", "Repeat", "Mirror", "Decal"};
87 static const char* minMagFilterLabels[] = {"Nearest", "Linear"};
88 static const char* mipFilterLabels[] = {"MipNone", "MipNearest", "MipLinear"};
89 label.append("X").append(tileModeLabels[static_cast<int>(xTileMode)]);
90 label.append("Y").append(tileModeLabels[static_cast<int>(yTileMode)]);
91 label.append(minMagFilterLabels[static_cast<int>(samplingOptions.filter)]);
92 label.append(mipFilterLabels[static_cast<int>(samplingOptions.mipmap)]);
93 desc.label = label.c_str();
94 }
95
96 auto sampler = sharedContext->device().CreateSampler(&desc);
97 if (!sampler) {
98 return {};
99 }
100 return sk_sp<DawnSampler>(new DawnSampler(sharedContext, std::move(sampler)));
101}
102
103void DawnSampler::freeGpuData() {
104 fSampler = nullptr;
105}
106
107} // namespace skgpu::graphite
108
#define SkUNREACHABLE
Definition: SkAssert.h:135
SkFilterMode
SkMipmapMode
SkTileMode
Definition: SkTileMode.h:13
bool setBackendLabels() const
Definition: Caps.h:297
const wgpu::Device & device() const
const Caps * caps() const
Definition: SharedContext.h:39
SK_API sk_sp< SkDocument > Make(SkWStream *dst, const SkSerialProcs *=nullptr, std::function< void(const SkPicture *)> onEndPage=nullptr)
std::function< ProfileSample(void)> Sampler
Sampler is run during SamplingProfiler::SampleRepeatedly. Each platform should implement its version ...
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive mode
Definition: switches.h:228
static wgpu::AddressMode tile_mode_to_dawn_address_mode(SkTileMode tileMode)
Definition: DawnSampler.cpp:49
Definition: ref_ptr.h:256
const SkFilterMode filter
const SkMipmapMode mipmap