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