| 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 |
|
| 43 |
wgpu::TextureFormat getViewFormat() const { |
| 44 |
return fViewFormat != wgpu::TextureFormat::Undefined ? fViewFormat : fFormat; |
| 45 |
} |
| 46 |
|
| 47 |
DawnTextureInfo() = default; |
| 48 |
|
| 49 |
explicit DawnTextureInfo(WGPUTexture texture); |
| 50 |
|
| 51 |
DawnTextureInfo(SampleCount 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 |
|
| 64 |
DawnTextureInfo(SampleCount 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__) |
| 79 |
DawnTextureInfo(SampleCount 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 |
|
| 103 |
Protected isProtected() const { return Protected::kNo; } |
| 104 |
TextureFormat viewFormat() const; |
| 105 |
|
| 106 |
// Virtual API when the specific backend type is not available. |
| 107 |
SkString toBackendString() const override; |
| 108 |
|
| 109 |
void copyTo(TextureInfo::AnyTextureInfoData& dstData) const override { |
| 110 |
dstData.emplace<DawnTextureInfo>(*this); |
| 111 |
} |
| 112 |
bool isCompatible(const TextureInfo& that, bool requireExact) const override; |
| 113 |
}; |
| 114 |
|
| 115 |
namespace TextureInfos { |
| 116 |
SK_API TextureInfo MakeDawn(const DawnTextureInfo& dawnInfo); |
| 117 |
|
| 118 |
SK_API bool GetDawnTextureInfo(const TextureInfo&, DawnTextureInfo*); |
| 119 |
} // namespace TextureInfos |
| 120 |
|
| 121 |
namespace BackendTextures { |
| 122 |
// Create a BackendTexture from a WGPUTexture. Texture info will be queried from the texture. |
| 123 |
// |
| 124 |
// This is the recommended way of specifying a BackendTexture for Dawn. See the note below on |
| 125 |
// the constructor that takes a WGPUTextureView for a fuller explanation. |
| 126 |
// |
| 127 |
// The BackendTexture will not call retain or release on the passed in WGPUTexture. Thus, the |
| 128 |
// client must keep the WGPUTexture valid until they are no longer using the BackendTexture. |
| 129 |
// However, any SkImage or SkSurface that wraps the BackendTexture *will* retain and release |
| 130 |
// the WGPUTexture. |
| 131 |
SK_API BackendTexture MakeDawn(WGPUTexture); |
| 132 |
|
| 133 |
// Create a BackendTexture from a WGPUTexture. Texture planeDimensions, plane aspect and |
| 134 |
// info have to be provided. This is intended to be used only when accessing a plane |
| 135 |
// of a WGPUTexture. |
| 136 |
// |
| 137 |
// The BackendTexture will not call retain or release on the passed in WGPUTexture. Thus, the |
| 138 |
// client must keep the WGPUTexture valid until they are no longer using the BackendTexture. |
| 139 |
// However, any SkImage or SkSurface that wraps the BackendTexture *will* retain and release |
| 140 |
// the WGPUTexture. |
| 141 |
SK_API BackendTexture MakeDawn(SkISize planeDimensions, const DawnTextureInfo&, WGPUTexture); |
| 142 |
|
| 143 |
// Create a BackendTexture from a WGPUTextureView. Texture dimensions and |
| 144 |
// info have to be provided. |
| 145 |
// |
| 146 |
// Using a WGPUTextureView rather than a WGPUTexture is less effecient for operations that |
| 147 |
// require buffer transfers to or from the texture (e.g. methods on graphite::Context that read |
| 148 |
// pixels or SkSurface::writePixels). In such cases an intermediate copy to or from a |
| 149 |
// WGPUTexture is required. Thus, it is recommended to use this functionality only for cases |
| 150 |
// where a WGPUTexture is unavailable, in particular when using wgpu::SwapChain. |
| 151 |
// |
| 152 |
// The BackendTexture will not call retain or release on the passed in WGPUTextureView. Thus, |
| 153 |
// the client must keep the WGPUTextureView valid until they are no longer using the |
| 154 |
// BackendTexture. However, any SkImage or SkSurface that wraps the BackendTexture *will* retain |
| 155 |
// and release the WGPUTextureView. |
| 156 |
SK_API BackendTexture MakeDawn(SkISize dimensions, |
| 157 |
const DawnTextureInfo& info, |
| 158 |
WGPUTextureView textureView); |
| 159 |
} // namespace BackendTextures |
| 160 |
|
| 161 |
} // namespace skgpu::graphite |
| 162 |
|
| 163 |
#endif // skgpu_graphite_DawnTypes_DEFINED |