Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
DawnTexture.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
14#include "src/core/SkMipmap.h"
19
20namespace skgpu::graphite {
21namespace {
22const char* texture_info_to_label(const TextureInfo& info,
23 const DawnTextureSpec& dawnSpec) {
24 if (dawnSpec.fUsage & wgpu::TextureUsage::RenderAttachment) {
25 if (DawnFormatIsDepthOrStencil(dawnSpec.fFormat)) {
26 return "DepthStencil";
27 } else {
28 if (info.numSamples() > 1) {
29 if (dawnSpec.fUsage & wgpu::TextureUsage::TextureBinding) {
30 return "MSAA SampledTexture-ColorAttachment";
31 } else {
32 return "MSAA ColorAttachment";
33 }
34 } else {
35 if (dawnSpec.fUsage & wgpu::TextureUsage::TextureBinding) {
36 return "SampledTexture-ColorAttachment";
37 } else {
38 return "ColorAttachment";
39 }
40 }
41 }
42 } else {
43 SkASSERT(dawnSpec.fUsage & wgpu::TextureUsage::TextureBinding);
44 return "SampledTexture";
45 }
46}
47}
48
49wgpu::Texture DawnTexture::MakeDawnTexture(const DawnSharedContext* sharedContext,
50 SkISize dimensions,
51 const TextureInfo& info) {
52 const Caps* caps = sharedContext->caps();
53 if (dimensions.width() > caps->maxTextureSize() ||
54 dimensions.height() > caps->maxTextureSize()) {
55 SKGPU_LOG_E("Texture creation failure: dimensions %d x %d too large.",
57 return {};
58 }
59
60 const DawnTextureSpec& dawnSpec = info.dawnTextureSpec();
61
62 if (dawnSpec.fUsage & wgpu::TextureUsage::TextureBinding && !caps->isTexturable(info)) {
63 return {};
64 }
65
66 if (dawnSpec.fUsage & wgpu::TextureUsage::RenderAttachment &&
67 !(caps->isRenderable(info) || DawnFormatIsDepthOrStencil(dawnSpec.fFormat))) {
68 return {};
69 }
70
71 if (dawnSpec.fUsage & wgpu::TextureUsage::StorageBinding && !caps->isStorage(info)) {
72 return {};
73 }
74
75 int numMipLevels = 1;
76 if (info.mipmapped() == Mipmapped::kYes) {
78 }
79
80 wgpu::TextureDescriptor desc;
81#ifdef SK_DEBUG
82 desc.label = texture_info_to_label(info, dawnSpec);
83#endif
84 desc.usage = dawnSpec.fUsage;
85 desc.dimension = wgpu::TextureDimension::e2D;
86 desc.size.width = dimensions.width();
87 desc.size.height = dimensions.height();
88 desc.size.depthOrArrayLayers = 1;
89 desc.format = dawnSpec.fFormat;
90 desc.mipLevelCount = numMipLevels;
91 desc.sampleCount = info.numSamples();
92 desc.viewFormatCount = 0;
93 desc.viewFormats = nullptr;
94
95 auto texture = sharedContext->device().CreateTexture(&desc);
96 if (!texture) {
97 return {};
98 }
99
100 return texture;
101}
102
103DawnTexture::DawnTexture(const DawnSharedContext* sharedContext,
104 SkISize dimensions,
105 const TextureInfo& info,
106 wgpu::Texture texture,
107 wgpu::TextureView sampleTextureView,
108 wgpu::TextureView renderTextureView,
109 Ownership ownership,
110 skgpu::Budgeted budgeted)
111 : Texture(sharedContext,
112 dimensions,
113 info,
114 /*mutableState=*/nullptr,
115 ownership,
116 budgeted)
117 , fTexture(std::move(texture))
118 , fSampleTextureView(std::move(sampleTextureView))
119 , fRenderTextureView(std::move(renderTextureView)) {}
120
121// static
122std::pair<wgpu::TextureView, wgpu::TextureView> DawnTexture::CreateTextureViews(
123 const wgpu::Texture& texture, const TextureInfo& info) {
124 const auto aspect = info.dawnTextureSpec().fAspect;
125 if (aspect == wgpu::TextureAspect::All) {
126 wgpu::TextureViewDescriptor viewDesc = {};
127 viewDesc.dimension = wgpu::TextureViewDimension::e2D;
128 viewDesc.baseArrayLayer = info.dawnTextureSpec().fSlice;
129 viewDesc.arrayLayerCount = 1;
130 wgpu::TextureView sampleTextureView = texture.CreateView(&viewDesc);
131 wgpu::TextureView renderTextureView;
132 if (info.mipmapped() == Mipmapped::kYes) {
133 viewDesc.baseMipLevel = 0;
134 viewDesc.mipLevelCount = 1;
135 renderTextureView = texture.CreateView(&viewDesc);
136 } else {
138 }
140 }
141
142#if defined(__EMSCRIPTEN__)
143 SkASSERT(false);
144 return {};
145#else
146 SkASSERT(aspect == wgpu::TextureAspect::Plane0Only ||
147 aspect == wgpu::TextureAspect::Plane1Only ||
148 aspect == wgpu::TextureAspect::Plane2Only);
149 wgpu::TextureView planeTextureView;
150 wgpu::TextureViewDescriptor planeViewDesc = {};
151 planeViewDesc.format = info.dawnTextureSpec().fViewFormat;
152 planeViewDesc.dimension = wgpu::TextureViewDimension::e2D;
153 planeViewDesc.aspect = aspect;
154 planeViewDesc.baseArrayLayer = info.dawnTextureSpec().fSlice;
155 planeViewDesc.arrayLayerCount = 1;
156 planeTextureView = texture.CreateView(&planeViewDesc);
157 return {planeTextureView, planeTextureView};
158#endif
159}
160
162 SkISize dimensions,
163 const TextureInfo& info,
164 skgpu::Budgeted budgeted) {
166 if (!texture) {
167 return {};
168 }
169 auto [sampleTextureView, renderTextureView] = CreateTextureViews(texture, info);
172 info,
173 std::move(texture),
174 std::move(sampleTextureView),
175 std::move(renderTextureView),
177 budgeted));
178}
179
181 SkISize dimensions,
182 const TextureInfo& info,
183 wgpu::Texture texture) {
184 if (!texture) {
185 SKGPU_LOG_E("No valid texture passed into MakeWrapped\n");
186 return {};
187 }
188
189 auto [sampleTextureView, renderTextureView] = CreateTextureViews(texture, info);
192 info,
193 std::move(texture),
194 std::move(sampleTextureView),
195 std::move(renderTextureView),
198}
199
201 SkISize dimensions,
202 const TextureInfo& info,
203 const wgpu::TextureView& textureView) {
204 if (!textureView) {
205 SKGPU_LOG_E("No valid texture view passed into MakeWrapped\n");
206 return {};
207 }
210 info,
211 /*texture=*/nullptr,
212 /*sampleTextureView=*/textureView,
213 /*renderTextureView=*/textureView,
216}
217
219 if (this->ownership() != Ownership::kWrapped && fTexture) {
220 // Destroy the texture even if it is still referenced by other BindGroup or views.
221 // Graphite should already guarantee that all command buffers using this texture (indirectly
222 // via BindGroup or views) are already completed.
223 fTexture.Destroy();
224 }
225 fTexture = nullptr;
226 fSampleTextureView = nullptr;
227 fRenderTextureView = nullptr;
228}
229
231 const char* dumpName) const {
232 Texture::onDumpMemoryStatistics(traceMemoryDump, dumpName);
233 traceMemoryDump->dumpStringValue(
234 dumpName,
235 "backend_label",
236 texture_info_to_label(this->textureInfo(), this->textureInfo().dawnTextureSpec()));
237}
238
239} // namespace skgpu::graphite
240
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition DM.cpp:213
#define SKGPU_LOG_E(fmt,...)
Definition Log.h:38
#define SkASSERT(cond)
Definition SkAssert.h:116
static int ComputeLevelCount(int baseWidth, int baseHeight)
Definition SkMipmap.cpp:134
virtual void dumpStringValue(const char *, const char *, const char *)
bool isTexturable(const TextureInfo &) const
Definition Caps.cpp:65
virtual bool isStorage(const TextureInfo &) const =0
virtual bool isRenderable(const TextureInfo &) const =0
int maxTextureSize() const
Definition Caps.h:134
static sk_sp< Texture > MakeWrapped(const DawnSharedContext *, SkISize dimensions, const TextureInfo &, wgpu::Texture)
static wgpu::Texture MakeDawnTexture(const DawnSharedContext *, SkISize dimensions, const TextureInfo &)
const wgpu::TextureView & renderTextureView() const
Definition DawnTexture.h:44
const wgpu::TextureView & sampleTextureView() const
Definition DawnTexture.h:43
static sk_sp< Texture > Make(const DawnSharedContext *, SkISize dimensions, const TextureInfo &, skgpu::Budgeted)
void onDumpMemoryStatistics(SkTraceMemoryDump *traceMemoryDump, const char *dumpName) const override
skgpu::Budgeted budgeted() const
Definition Resource.h:100
const SharedContext * sharedContext() const
Definition Resource.h:187
Ownership ownership() const
Definition Resource.h:98
const Caps * caps() const
void onDumpMemoryStatistics(SkTraceMemoryDump *traceMemoryDump, const char *dumpName) const override
Definition Texture.cpp:51
SkISize dimensions() const
Definition Texture.h:31
const TextureInfo & textureInfo() const
Definition Texture.h:32
FlTexture * texture
bool DawnFormatIsDepthOrStencil(wgpu::TextureFormat format)
Budgeted
Definition GpuTypes.h:35
Definition ref_ptr.h:256
constexpr int32_t width() const
Definition SkSize.h:36
constexpr int32_t height() const
Definition SkSize.h:37