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_TextureInfo_DEFINED 9 #define skgpu_graphite_TextureInfo_DEFINED 10 11 #include "include/core/SkString.h" 12 #include "include/core/SkTextureCompressionType.h" 13 #include "include/gpu/graphite/GraphiteTypes.h" 14 15 #ifdef SK_DAWN 16 #include "include/private/gpu/graphite/DawnTypesPriv.h" 17 #endif 18 19 #ifdef SK_METAL 20 #include "include/private/gpu/graphite/MtlGraphiteTypesPriv.h" 21 #endif 22 23 #ifdef SK_VULKAN 24 #include "include/private/gpu/graphite/VulkanGraphiteTypesPriv.h" 25 #endif 26 27 struct SkISize; 28 29 namespace skgpu::graphite { 30 31 class SK_API TextureInfo { 32 public: TextureInfo()33 TextureInfo() {} 34 #ifdef SK_DAWN TextureInfo(const DawnTextureInfo & dawnInfo)35 TextureInfo(const DawnTextureInfo& dawnInfo) 36 : fBackend(BackendApi::kDawn) 37 , fValid(true) 38 , fSampleCount(dawnInfo.fSampleCount) 39 , fMipmapped(dawnInfo.fMipmapped) 40 , fProtected(Protected::kNo) 41 , fDawnSpec(dawnInfo) {} 42 #endif 43 44 #ifdef SK_METAL TextureInfo(const MtlTextureInfo & mtlInfo)45 TextureInfo(const MtlTextureInfo& mtlInfo) 46 : fBackend(BackendApi::kMetal) 47 , fValid(true) 48 , fSampleCount(mtlInfo.fSampleCount) 49 , fMipmapped(mtlInfo.fMipmapped) 50 , fProtected(Protected::kNo) 51 , fMtlSpec(mtlInfo) {} 52 #endif 53 54 #ifdef SK_VULKAN TextureInfo(const VulkanTextureInfo & vkInfo)55 TextureInfo(const VulkanTextureInfo& vkInfo) 56 : fBackend(BackendApi::kVulkan) 57 , fValid(true) 58 , fSampleCount(vkInfo.fSampleCount) 59 , fMipmapped(vkInfo.fMipmapped) 60 , fProtected(Protected::kNo) 61 , fVkSpec(vkInfo) { 62 if (vkInfo.fFlags & VK_IMAGE_CREATE_PROTECTED_BIT) { 63 fProtected = Protected::kYes; 64 } 65 } 66 #endif 67 ~TextureInfo()68 ~TextureInfo() {} 69 TextureInfo(const TextureInfo&) = default; 70 TextureInfo& operator=(const TextureInfo&); 71 72 bool operator==(const TextureInfo&) const; 73 bool operator!=(const TextureInfo& that) const { return !(*this == that); } 74 isValid()75 bool isValid() const { return fValid; } backend()76 BackendApi backend() const { return fBackend; } 77 numSamples()78 uint32_t numSamples() const { return fSampleCount; } mipmapped()79 Mipmapped mipmapped() const { return fMipmapped; } isProtected()80 Protected isProtected() const { return fProtected; } 81 SkTextureCompressionType compressionType() const; 82 83 #ifdef SK_DAWN 84 bool getDawnTextureInfo(DawnTextureInfo* info) const; 85 #endif 86 87 #ifdef SK_METAL getMtlTextureInfo(MtlTextureInfo * info)88 bool getMtlTextureInfo(MtlTextureInfo* info) const { 89 if (!this->isValid() || fBackend != BackendApi::kMetal) { 90 return false; 91 } 92 *info = MtlTextureSpecToTextureInfo(fMtlSpec, fSampleCount, fMipmapped); 93 return true; 94 } 95 #endif 96 97 #ifdef SK_VULKAN getVulkanTextureInfo(VulkanTextureInfo * info)98 bool getVulkanTextureInfo(VulkanTextureInfo* info) const { 99 if (!this->isValid() || fBackend != BackendApi::kVulkan) { 100 return false; 101 } 102 *info = VulkanTextureSpecToTextureInfo(fVkSpec, fSampleCount, fMipmapped); 103 return true; 104 } 105 #endif 106 107 bool isCompatible(const TextureInfo& that) const; 108 // Return a string containing the full description of this TextureInfo. 109 SkString toString() const; 110 // Return a string containing only the info relevant for its use as a RenderPass attachment. 111 SkString toRPAttachmentString() const; 112 113 private: 114 friend size_t ComputeSize(SkISize dimensions, const TextureInfo&); // for bytesPerPixel 115 116 size_t bytesPerPixel() const; 117 118 #ifdef SK_DAWN 119 friend class DawnCaps; 120 friend class DawnCommandBuffer; 121 friend class DawnComputePipeline; 122 friend class DawnGraphicsPipeline; 123 friend class DawnResourceProvider; 124 friend class DawnTexture; dawnTextureSpec()125 const DawnTextureSpec& dawnTextureSpec() const { 126 SkASSERT(fValid && fBackend == BackendApi::kDawn); 127 return fDawnSpec; 128 } 129 #endif 130 131 #ifdef SK_METAL 132 friend class MtlCaps; 133 friend class MtlGraphicsPipeline; 134 friend class MtlTexture; mtlTextureSpec()135 const MtlTextureSpec& mtlTextureSpec() const { 136 SkASSERT(fValid && fBackend == BackendApi::kMetal); 137 return fMtlSpec; 138 } 139 #endif 140 141 #ifdef SK_VULKAN 142 friend class VulkanCaps; 143 friend class VulkanResourceProvider; 144 friend class VulkanTexture; vulkanTextureSpec()145 const VulkanTextureSpec& vulkanTextureSpec() const { 146 SkASSERT(fValid && fBackend == BackendApi::kVulkan); 147 return fVkSpec; 148 } 149 #endif 150 151 BackendApi fBackend = BackendApi::kMock; 152 bool fValid = false; 153 154 uint32_t fSampleCount = 1; 155 Mipmapped fMipmapped = Mipmapped::kNo; 156 Protected fProtected = Protected::kNo; 157 158 union { 159 #ifdef SK_DAWN 160 DawnTextureSpec fDawnSpec; 161 #endif 162 #ifdef SK_METAL 163 MtlTextureSpec fMtlSpec; 164 #endif 165 #ifdef SK_VULKAN 166 VulkanTextureSpec fVkSpec; 167 #endif 168 void* fEnsureUnionNonEmpty; 169 }; 170 }; 171 172 } // namespace skgpu::graphite 173 174 #endif //skgpu_graphite_TextureInfo_DEFINED 175