1 /* 2 * Copyright 2021 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_BackendTexture_DEFINED 9 #define skgpu_graphite_BackendTexture_DEFINED 10 11 #include "include/core/SkRefCnt.h" 12 #include "include/core/SkSize.h" 13 #include "include/gpu/graphite/GraphiteTypes.h" 14 #include "include/gpu/graphite/TextureInfo.h" 15 16 #ifdef SK_DAWN 17 #include "include/gpu/graphite/dawn/DawnTypes.h" 18 #endif 19 20 #ifdef SK_METAL 21 #include "include/gpu/graphite/mtl/MtlTypes.h" 22 #endif 23 24 #ifdef SK_VULKAN 25 #include "include/private/gpu/vk/SkiaVulkan.h" 26 #endif 27 28 namespace skgpu { 29 class MutableTextureState; 30 class MutableTextureStateRef; 31 } 32 33 namespace skgpu::graphite { 34 35 class BackendTexture { 36 public: 37 BackendTexture(); 38 #ifdef SK_DAWN 39 // Create a BackendTexture from a wgpu::Texture. Texture info will be 40 // queried from the texture. Comparing to wgpu::TextureView, 41 // SkImage::readPixels(), SkSurface::readPixels() and 42 // SkSurface::writePixels() are implemented by direct buffer copy. They 43 // should be more efficient. For wgpu::TextureView, those methods will use 44 // create an intermediate wgpu::Texture, and use it to transfer pixels. 45 // Note: for better performance, using wgpu::Texture IS RECOMMENDED. 46 BackendTexture(wgpu::Texture texture); 47 // Create a BackendTexture from a wgpu::TextureView. Texture dimensions and 48 // info have to be provided. 49 // Note: this method is for importing wgpu::TextureView from wgpu::SwapChain 50 // only. 51 BackendTexture(SkISize dimensions, 52 const DawnTextureInfo& info, 53 wgpu::TextureView textureView); 54 #endif 55 #ifdef SK_METAL 56 // The BackendTexture will not call retain or release on the passed in MtlHandle. Thus the 57 // client must keep the MtlHandle valid until they are no longer using the BackendTexture. 58 BackendTexture(SkISize dimensions, MtlHandle mtlTexture); 59 #endif 60 61 #ifdef SK_VULKAN 62 BackendTexture(SkISize dimensions, 63 const VulkanTextureInfo&, 64 VkImageLayout, 65 uint32_t queueFamilyIndex, 66 VkImage); 67 #endif 68 69 BackendTexture(const BackendTexture&); 70 71 ~BackendTexture(); 72 73 BackendTexture& operator=(const BackendTexture&); 74 75 bool operator==(const BackendTexture&) const; 76 bool operator!=(const BackendTexture& that) const { return !(*this == that); } 77 isValid()78 bool isValid() const { return fInfo.isValid(); } backend()79 BackendApi backend() const { return fInfo.backend(); } 80 dimensions()81 SkISize dimensions() const { return fDimensions; } 82 info()83 const TextureInfo& info() const { return fInfo; } 84 85 // If the client changes any of the mutable backend of the GrBackendTexture they should call 86 // this function to inform Skia that those values have changed. The backend API specific state 87 // that can be set from this function are: 88 // 89 // Vulkan: VkImageLayout and QueueFamilyIndex 90 void setMutableState(const skgpu::MutableTextureState&); 91 92 #ifdef SK_DAWN 93 wgpu::Texture getDawnTexture() const; 94 wgpu::TextureView getDawnTextureView() const; 95 #endif 96 #ifdef SK_METAL 97 MtlHandle getMtlTexture() const; 98 #endif 99 100 #ifdef SK_VULKAN 101 VkImage getVkImage() const; 102 VkImageLayout getVkImageLayout() const; 103 uint32_t getVkQueueFamilyIndex() const; 104 #endif 105 106 private: 107 sk_sp<MutableTextureStateRef> mutableState() const; 108 109 SkISize fDimensions; 110 TextureInfo fInfo; 111 112 sk_sp<MutableTextureStateRef> fMutableState; 113 114 #ifdef SK_DAWN 115 struct Dawn { DawnDawn116 Dawn(wgpu::Texture texture) : fTexture(std::move(texture)) {} DawnDawn117 Dawn(wgpu::TextureView textureView) : fTextureView(std::move(textureView)) {} 118 119 bool operator==(const Dawn& that) const { 120 return fTexture.Get() == that.fTexture.Get() && 121 fTextureView.Get() == that.fTextureView.Get(); 122 } 123 bool operator!=(const Dawn& that) const { 124 return !this->operator==(that); 125 } 126 Dawn& operator=(const Dawn& that) { 127 fTexture = that.fTexture; 128 fTextureView = that.fTextureView; 129 return *this; 130 } 131 132 wgpu::Texture fTexture; 133 wgpu::TextureView fTextureView; 134 }; 135 #endif 136 137 union { 138 #ifdef SK_DAWN 139 Dawn fDawn; 140 #endif 141 #ifdef SK_METAL 142 MtlHandle fMtlTexture; 143 #endif 144 #ifdef SK_VULKAN 145 VkImage fVkImage; 146 #endif 147 }; 148 }; 149 150 } // namespace skgpu::graphite 151 152 #endif // skgpu_graphite_BackendTexture_DEFINED 153 154