• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2025 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 
8 #ifndef skgpu_graphite_DawnTypes_DEFINED
9 #define skgpu_graphite_DawnTypes_DEFINED
10 
11 #include "include/core/SkSize.h"
12 #include "include/gpu/graphite/GraphiteTypes.h"
13 #include "include/gpu/graphite/TextureInfo.h"
14 #include "include/private/base/SkAPI.h"
15 
16 #include "webgpu/webgpu_cpp.h"  // NO_G3_REWRITE
17 
18 class SkStream;
19 class SkWStream;
20 
21 namespace skgpu::graphite {
22 class BackendTexture;
23 
24 class SK_API DawnTextureInfo final : public TextureInfo::Data {
25 public:
26     // wgpu::TextureDescriptor properties
27     wgpu::TextureFormat fFormat = wgpu::TextureFormat::Undefined;
28     // `fViewFormat` for multiplanar formats corresponds to the plane TextureView's format.
29     wgpu::TextureFormat fViewFormat = wgpu::TextureFormat::Undefined;
30     wgpu::TextureUsage fUsage = wgpu::TextureUsage::None;
31     // TODO(b/308944094): Migrate aspect information to BackendTextureViews.
32     wgpu::TextureAspect fAspect = wgpu::TextureAspect::All;
33     uint32_t fSlice = 0;
34 
35 #if !defined(__EMSCRIPTEN__)
36     // The descriptor of the YCbCr info (if any) for this texture. Dawn's YCbCr
37     // sampling will be used for this texture if this info is set. Setting the
38     // info is supported only on Android and only if using Vulkan as the
39     // underlying GPU driver.
40     wgpu::YCbCrVkDescriptor fYcbcrVkDescriptor = {};
41 #endif
42 
getViewFormat()43     wgpu::TextureFormat getViewFormat() const {
44         return fViewFormat != wgpu::TextureFormat::Undefined ? fViewFormat : fFormat;
45     }
46 
47     DawnTextureInfo() = default;
48 
49     DawnTextureInfo(WGPUTexture texture);
50 
DawnTextureInfo(uint32_t sampleCount,Mipmapped mipmapped,wgpu::TextureFormat format,wgpu::TextureUsage usage,wgpu::TextureAspect aspect)51     DawnTextureInfo(uint32_t sampleCount,
52                     Mipmapped mipmapped,
53                     wgpu::TextureFormat format,
54                     wgpu::TextureUsage usage,
55                     wgpu::TextureAspect aspect)
56             : DawnTextureInfo(sampleCount,
57                               mipmapped,
58                               /*format=*/format,
59                               /*viewFormat=*/format,
60                               usage,
61                               aspect,
62                               /*slice=*/0) {}
63 
DawnTextureInfo(uint32_t sampleCount,Mipmapped mipmapped,wgpu::TextureFormat format,wgpu::TextureFormat viewFormat,wgpu::TextureUsage usage,wgpu::TextureAspect aspect,uint32_t slice)64     DawnTextureInfo(uint32_t sampleCount,
65                     Mipmapped mipmapped,
66                     wgpu::TextureFormat format,
67                     wgpu::TextureFormat viewFormat,
68                     wgpu::TextureUsage usage,
69                     wgpu::TextureAspect aspect,
70                     uint32_t slice)
71             : Data(sampleCount, mipmapped)
72             , fFormat(format)
73             , fViewFormat(viewFormat)
74             , fUsage(usage)
75             , fAspect(aspect)
76             , fSlice(slice) {}
77 
78 #if !defined(__EMSCRIPTEN__)
DawnTextureInfo(uint32_t sampleCount,Mipmapped mipmapped,wgpu::TextureFormat format,wgpu::TextureFormat viewFormat,wgpu::TextureUsage usage,wgpu::TextureAspect aspect,uint32_t slice,wgpu::YCbCrVkDescriptor ycbcrVkDescriptor)79     DawnTextureInfo(uint32_t sampleCount,
80                     Mipmapped mipmapped,
81                     wgpu::TextureFormat format,
82                     wgpu::TextureFormat viewFormat,
83                     wgpu::TextureUsage usage,
84                     wgpu::TextureAspect aspect,
85                     uint32_t slice,
86                     wgpu::YCbCrVkDescriptor ycbcrVkDescriptor)
87             : Data(sampleCount, mipmapped)
88             , fFormat(format)
89             , fViewFormat(viewFormat)
90             , fUsage(usage)
91             , fAspect(aspect)
92             , fSlice(slice)
93             , fYcbcrVkDescriptor(ycbcrVkDescriptor) {}
94 #endif
95 
96 private:
97     friend class TextureInfo;
98     friend class TextureInfoPriv;
99 
100     // Non-virtual template API for TextureInfo::Data accessed directly when backend type is known.
101     static constexpr skgpu::BackendApi kBackend = skgpu::BackendApi::kDawn;
102 
isProtected()103     Protected isProtected() const { return Protected::kNo; }
104     TextureFormat viewFormat() const;
105 
106     bool serialize(SkWStream*) const;
107     bool deserialize(SkStream*);
108 
109     // Virtual API when the specific backend type is not available.
110     SkString toBackendString() const override;
111 
copyTo(TextureInfo::AnyTextureInfoData & dstData)112     void copyTo(TextureInfo::AnyTextureInfoData& dstData) const override {
113         dstData.emplace<DawnTextureInfo>(*this);
114     }
115     bool isCompatible(const TextureInfo& that, bool requireExact) const override;
116 };
117 
118 namespace TextureInfos {
119 SK_API TextureInfo MakeDawn(const DawnTextureInfo& dawnInfo);
120 
121 SK_API bool GetDawnTextureInfo(const TextureInfo&, DawnTextureInfo*);
122 }  // namespace TextureInfos
123 
124 namespace BackendTextures {
125 // Create a BackendTexture from a WGPUTexture. Texture info will be queried from the texture.
126 //
127 // This is the recommended way of specifying a BackendTexture for Dawn. See the note below on
128 // the constructor that takes a WGPUTextureView for a fuller explanation.
129 //
130 // The BackendTexture will not call retain or release on the passed in WGPUTexture. Thus, the
131 // client must keep the WGPUTexture valid until they are no longer using the BackendTexture.
132 // However, any SkImage or SkSurface that wraps the BackendTexture *will* retain and release
133 // the WGPUTexture.
134 SK_API BackendTexture MakeDawn(WGPUTexture);
135 
136 // Create a BackendTexture from a WGPUTexture. Texture planeDimensions, plane aspect and
137 // info have to be provided. This is intended to be used only when accessing a plane
138 // of a WGPUTexture.
139 //
140 // The BackendTexture will not call retain or release on the passed in WGPUTexture. Thus, the
141 // client must keep the WGPUTexture valid until they are no longer using the BackendTexture.
142 // However, any SkImage or SkSurface that wraps the BackendTexture *will* retain and release
143 // the WGPUTexture.
144 SK_API BackendTexture MakeDawn(SkISize planeDimensions, const DawnTextureInfo&, WGPUTexture);
145 
146 // Create a BackendTexture from a WGPUTextureView. Texture dimensions and
147 // info have to be provided.
148 //
149 // Using a WGPUTextureView rather than a WGPUTexture is less effecient for operations that
150 // require buffer transfers to or from the texture (e.g. methods on graphite::Context that read
151 // pixels or SkSurface::writePixels). In such cases an intermediate copy to or from a
152 // WGPUTexture is required. Thus, it is recommended to use this functionality only for cases
153 // where a WGPUTexture is unavailable, in particular when using wgpu::SwapChain.
154 //
155 // The BackendTexture will not call retain or release on the passed in WGPUTextureView. Thus,
156 // the client must keep the WGPUTextureView valid until they are no longer using the
157 // BackendTexture. However, any SkImage or SkSurface that wraps the BackendTexture *will* retain
158 // and release the WGPUTextureView.
159 SK_API BackendTexture MakeDawn(SkISize dimensions,
160                                const DawnTextureInfo& info,
161                                WGPUTextureView textureView);
162 }  // namespace BackendTextures
163 
164 }  // namespace skgpu::graphite
165 
166 #endif // skgpu_graphite_DawnTypes_DEFINED
167