1 /* 2 * Copyright 2020 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 #include "src/gpu/GrBackendUtils.h" 9 10 #include "src/gpu/GrDataUtils.h" 11 12 #ifdef SK_GL 13 #include "src/gpu/gl/GrGLUtil.h" 14 #endif 15 16 #ifdef SK_VULKAN 17 #include "src/gpu/vk/GrVkUtil.h" 18 #endif 19 20 #ifdef SK_DIRECT3D 21 #include "src/gpu/d3d/GrD3DUtil.h" 22 #endif 23 24 #ifdef SK_METAL 25 #include "src/gpu/mtl/GrMtlCppUtil.h" 26 #endif 27 28 #ifdef SK_DAWN 29 #include "src/gpu/dawn/GrDawnUtil.h" 30 #endif 31 GrBackendFormatToCompressionType(const GrBackendFormat & format)32SkImage::CompressionType GrBackendFormatToCompressionType(const GrBackendFormat& format) { 33 switch (format.backend()) { 34 case GrBackendApi::kOpenGL: { 35 #ifdef SK_GL 36 GrGLFormat glFormat = format.asGLFormat(); 37 switch (glFormat) { 38 case GrGLFormat::kCOMPRESSED_ETC1_RGB8: 39 case GrGLFormat::kCOMPRESSED_RGB8_ETC2: 40 return SkImage::CompressionType::kETC2_RGB8_UNORM; 41 case GrGLFormat::kCOMPRESSED_RGB8_BC1: 42 return SkImage::CompressionType::kBC1_RGB8_UNORM; 43 case GrGLFormat::kCOMPRESSED_RGBA8_BC1: 44 return SkImage::CompressionType::kBC1_RGBA8_UNORM; 45 case GrGLFormat::kCOMPRESSED_ASTC_RGBA8: 46 return SkImage::CompressionType::kASTC_RGBA8_UNORM; 47 default: 48 return SkImage::CompressionType::kNone; 49 } 50 #endif 51 break; 52 } 53 case GrBackendApi::kVulkan: { 54 #ifdef SK_VULKAN 55 VkFormat vkFormat; 56 SkAssertResult(format.asVkFormat(&vkFormat)); 57 switch (vkFormat) { 58 case VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK: 59 return SkImage::CompressionType::kETC2_RGB8_UNORM; 60 case VK_FORMAT_BC1_RGB_UNORM_BLOCK: 61 return SkImage::CompressionType::kBC1_RGB8_UNORM; 62 case VK_FORMAT_BC1_RGBA_UNORM_BLOCK: 63 return SkImage::CompressionType::kBC1_RGBA8_UNORM; 64 default: 65 return SkImage::CompressionType::kNone; 66 } 67 #endif 68 break; 69 } 70 case GrBackendApi::kMetal: { 71 #ifdef SK_METAL 72 return GrMtlBackendFormatToCompressionType(format); 73 #endif 74 break; 75 } 76 case GrBackendApi::kDirect3D: { 77 #ifdef SK_DIRECT3D 78 DXGI_FORMAT dxgiFormat; 79 SkAssertResult(format.asDxgiFormat(&dxgiFormat)); 80 switch (dxgiFormat) { 81 case DXGI_FORMAT_BC1_UNORM: 82 return SkImage::CompressionType::kBC1_RGBA8_UNORM; 83 default: 84 return SkImage::CompressionType::kNone; 85 } 86 #endif 87 break; 88 } 89 case GrBackendApi::kDawn: { 90 return SkImage::CompressionType::kNone; 91 } 92 case GrBackendApi::kMock: { 93 return format.asMockCompressionType(); 94 } 95 } 96 return SkImage::CompressionType::kNone; 97 } 98 GrBackendFormatBytesPerBlock(const GrBackendFormat & format)99size_t GrBackendFormatBytesPerBlock(const GrBackendFormat& format) { 100 switch (format.backend()) { 101 case GrBackendApi::kOpenGL: { 102 #ifdef SK_GL 103 GrGLFormat glFormat = format.asGLFormat(); 104 return GrGLFormatBytesPerBlock(glFormat); 105 #endif 106 break; 107 } 108 case GrBackendApi::kVulkan: { 109 #ifdef SK_VULKAN 110 VkFormat vkFormat; 111 SkAssertResult(format.asVkFormat(&vkFormat)); 112 return GrVkFormatBytesPerBlock(vkFormat); 113 #endif 114 break; 115 } 116 case GrBackendApi::kMetal: { 117 #ifdef SK_METAL 118 return GrMtlBackendFormatBytesPerBlock(format); 119 #endif 120 break; 121 } 122 case GrBackendApi::kDirect3D: { 123 #ifdef SK_DIRECT3D 124 DXGI_FORMAT dxgiFormat; 125 SkAssertResult(format.asDxgiFormat(&dxgiFormat)); 126 return GrDxgiFormatBytesPerBlock(dxgiFormat); 127 #endif 128 break; 129 } 130 case GrBackendApi::kDawn: { 131 #ifdef SK_DAWN 132 wgpu::TextureFormat dawnFormat; 133 SkAssertResult(format.asDawnFormat(&dawnFormat)); 134 return GrDawnBytesPerBlock(dawnFormat); 135 #endif 136 break; 137 } 138 case GrBackendApi::kMock: { 139 SkImage::CompressionType compression = format.asMockCompressionType(); 140 if (compression != SkImage::CompressionType::kNone) { 141 return GrCompressedRowBytes(compression, 1); 142 } else if (format.isMockStencilFormat()) { 143 static constexpr int kMockStencilSize = 4; 144 return kMockStencilSize; 145 } 146 return GrColorTypeBytesPerPixel(format.asMockColorType()); 147 } 148 } 149 return 0; 150 } 151 GrBackendFormatBytesPerPixel(const GrBackendFormat & format)152size_t GrBackendFormatBytesPerPixel(const GrBackendFormat& format) { 153 if (GrBackendFormatToCompressionType(format) != SkImage::CompressionType::kNone) { 154 return 0; 155 } 156 return GrBackendFormatBytesPerBlock(format); 157 } 158 GrBackendFormatStencilBits(const GrBackendFormat & format)159int GrBackendFormatStencilBits(const GrBackendFormat& format) { 160 switch (format.backend()) { 161 case GrBackendApi::kOpenGL: { 162 #ifdef SK_GL 163 GrGLFormat glFormat = format.asGLFormat(); 164 return GrGLFormatStencilBits(glFormat); 165 #endif 166 break; 167 } 168 case GrBackendApi::kVulkan: { 169 #ifdef SK_VULKAN 170 VkFormat vkFormat; 171 SkAssertResult(format.asVkFormat(&vkFormat)); 172 return GrVkFormatStencilBits(vkFormat); 173 #endif 174 break; 175 } 176 case GrBackendApi::kMetal: { 177 #ifdef SK_METAL 178 return GrMtlBackendFormatStencilBits(format); 179 #endif 180 break; 181 } 182 case GrBackendApi::kDirect3D: { 183 #ifdef SK_DIRECT3D 184 DXGI_FORMAT dxgiFormat; 185 SkAssertResult(format.asDxgiFormat(&dxgiFormat)); 186 return GrDxgiFormatStencilBits(dxgiFormat); 187 #endif 188 break; 189 } 190 case GrBackendApi::kDawn: { 191 #ifdef SK_DAWN 192 wgpu::TextureFormat dawnFormat; 193 SkAssertResult(format.asDawnFormat(&dawnFormat)); 194 return GrDawnFormatStencilBits(dawnFormat); 195 #endif 196 break; 197 } 198 case GrBackendApi::kMock: { 199 if (format.isMockStencilFormat()) { 200 static constexpr int kMockStencilBits = 8; 201 return kMockStencilBits; 202 } 203 } 204 } 205 return 0; 206 } 207