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 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/private/base/SkAPI.h" 14 15 #include "webgpu/webgpu_cpp.h" // NO_G3_REWRITE 16 17 namespace skgpu::graphite { 18 class BackendTexture; 19 class TextureInfo; 20 21 struct DawnTextureInfo { 22 uint32_t fSampleCount = 1; 23 Mipmapped fMipmapped = Mipmapped::kNo; 24 25 // wgpu::TextureDescriptor properties 26 wgpu::TextureFormat fFormat = wgpu::TextureFormat::Undefined; 27 // `fViewFormat` for multiplanar formats corresponds to the plane TextureView's format. 28 wgpu::TextureFormat fViewFormat = wgpu::TextureFormat::Undefined; 29 wgpu::TextureUsage fUsage = wgpu::TextureUsage::None; 30 // TODO(b/308944094): Migrate aspect information to BackendTextureViews. 31 wgpu::TextureAspect fAspect = wgpu::TextureAspect::All; 32 uint32_t fSlice = 0; 33 34 #if !defined(__EMSCRIPTEN__) 35 // The descriptor of the YCbCr info (if any) for this texture. Dawn's YCbCr 36 // sampling will be used for this texture if this info is set. Setting the 37 // info is supported only on Android and only if using Vulkan as the 38 // underlying GPU driver. 39 wgpu::YCbCrVkDescriptor fYcbcrVkDescriptor = {}; 40 #endif 41 getViewFormatDawnTextureInfo42 wgpu::TextureFormat getViewFormat() const { 43 return fViewFormat != wgpu::TextureFormat::Undefined ? fViewFormat : fFormat; 44 } 45 46 DawnTextureInfo() = default; 47 DawnTextureInfoDawnTextureInfo48 DawnTextureInfo(uint32_t sampleCount, 49 Mipmapped mipmapped, 50 wgpu::TextureFormat format, 51 wgpu::TextureUsage usage, 52 wgpu::TextureAspect aspect) 53 : DawnTextureInfo(sampleCount, 54 mipmapped, 55 /*format=*/format, 56 /*viewFormat=*/format, 57 usage, 58 aspect, 59 /*slice=*/0) {} 60 DawnTextureInfoDawnTextureInfo61 DawnTextureInfo(uint32_t sampleCount, 62 Mipmapped mipmapped, 63 wgpu::TextureFormat format, 64 wgpu::TextureFormat viewFormat, 65 wgpu::TextureUsage usage, 66 wgpu::TextureAspect aspect, 67 uint32_t slice) 68 : fSampleCount(sampleCount) 69 , fMipmapped(mipmapped) 70 , fFormat(format) 71 , fViewFormat(viewFormat) 72 , fUsage(usage) 73 , fAspect(aspect) 74 , fSlice(slice) {} 75 76 #if !defined(__EMSCRIPTEN__) DawnTextureInfoDawnTextureInfo77 DawnTextureInfo(uint32_t sampleCount, 78 Mipmapped mipmapped, 79 wgpu::TextureFormat format, 80 wgpu::TextureFormat viewFormat, 81 wgpu::TextureUsage usage, 82 wgpu::TextureAspect aspect, 83 uint32_t slice, 84 wgpu::YCbCrVkDescriptor ycbcrVkDescriptor) 85 : fSampleCount(sampleCount) 86 , fMipmapped(mipmapped) 87 , fFormat(format) 88 , fViewFormat(viewFormat) 89 , fUsage(usage) 90 , fAspect(aspect) 91 , fSlice(slice) 92 , fYcbcrVkDescriptor(ycbcrVkDescriptor) {} 93 #endif 94 }; 95 96 namespace TextureInfos { 97 SK_API TextureInfo MakeDawn(const DawnTextureInfo& dawnInfo); 98 99 SK_API bool GetDawnTextureInfo(const TextureInfo&, DawnTextureInfo*); 100 } // namespace TextureInfos 101 102 namespace BackendTextures { 103 // Create a BackendTexture from a WGPUTexture. Texture info will be queried from the texture. 104 // 105 // This is the recommended way of specifying a BackendTexture for Dawn. See the note below on 106 // the constructor that takes a WGPUTextureView for a fuller explanation. 107 // 108 // The BackendTexture will not call retain or release on the passed in WGPUTexture. Thus, the 109 // client must keep the WGPUTexture valid until they are no longer using the BackendTexture. 110 // However, any SkImage or SkSurface that wraps the BackendTexture *will* retain and release 111 // the WGPUTexture. 112 SK_API BackendTexture MakeDawn(WGPUTexture); 113 114 // Create a BackendTexture from a WGPUTexture. Texture planeDimensions, plane aspect and 115 // info have to be provided. This is intended to be used only when accessing a plane 116 // of a WGPUTexture. 117 // 118 // The BackendTexture will not call retain or release on the passed in WGPUTexture. Thus, the 119 // client must keep the WGPUTexture valid until they are no longer using the BackendTexture. 120 // However, any SkImage or SkSurface that wraps the BackendTexture *will* retain and release 121 // the WGPUTexture. 122 SK_API BackendTexture MakeDawn(SkISize planeDimensions, const DawnTextureInfo&, WGPUTexture); 123 124 // Create a BackendTexture from a WGPUTextureView. Texture dimensions and 125 // info have to be provided. 126 // 127 // Using a WGPUTextureView rather than a WGPUTexture is less effecient for operations that 128 // require buffer transfers to or from the texture (e.g. methods on graphite::Context that read 129 // pixels or SkSurface::writePixels). In such cases an intermediate copy to or from a 130 // WGPUTexture is required. Thus, it is recommended to use this functionality only for cases 131 // where a WGPUTexture is unavailable, in particular when using wgpu::SwapChain. 132 // 133 // The BackendTexture will not call retain or release on the passed in WGPUTextureView. Thus, 134 // the client must keep the WGPUTextureView valid until they are no longer using the 135 // BackendTexture. However, any SkImage or SkSurface that wraps the BackendTexture *will* retain 136 // and release the WGPUTextureView. 137 SK_API BackendTexture MakeDawn(SkISize dimensions, 138 const DawnTextureInfo& info, 139 WGPUTextureView textureView); 140 141 } // namespace BackendTextures 142 143 } // namespace skgpu::graphite 144 145 #endif // skgpu_graphite_DawnTypes_DEFINED 146 147 148