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_4x4: 46 return SkImage::CompressionType::kASTC_RGBA8_4x4; 47 case GrGLFormat::kCOMPRESSED_ASTC_RGBA8_6x6: 48 return SkImage::CompressionType::kASTC_RGBA8_6x6; 49 case GrGLFormat::kCOMPRESSED_ASTC_RGBA8_8x8: 50 return SkImage::CompressionType::kASTC_RGBA8_8x8; 51 default: 52 return SkImage::CompressionType::kNone; 53 } 54 #endif 55 break; 56 } 57 case GrBackendApi::kVulkan: { 58 #ifdef SK_VULKAN 59 VkFormat vkFormat; 60 SkAssertResult(format.asVkFormat(&vkFormat)); 61 switch (vkFormat) { 62 case VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK: 63 return SkImage::CompressionType::kETC2_RGB8_UNORM; 64 case VK_FORMAT_BC1_RGB_UNORM_BLOCK: 65 return SkImage::CompressionType::kBC1_RGB8_UNORM; 66 case VK_FORMAT_BC1_RGBA_UNORM_BLOCK: 67 return SkImage::CompressionType::kBC1_RGBA8_UNORM; 68 case VK_FORMAT_ASTC_4x4_UNORM_BLOCK: 69 return SkImage::CompressionType::kASTC_RGBA8_4x4; 70 case VK_FORMAT_ASTC_6x6_UNORM_BLOCK: 71 return SkImage::CompressionType::kASTC_RGBA8_6x6; 72 case VK_FORMAT_ASTC_8x8_UNORM_BLOCK: 73 return SkImage::CompressionType::kASTC_RGBA8_8x8; 74 default: 75 return SkImage::CompressionType::kNone; 76 } 77 #endif 78 break; 79 } 80 case GrBackendApi::kMetal: { 81 #ifdef SK_METAL 82 return GrMtlBackendFormatToCompressionType(format); 83 #endif 84 break; 85 } 86 case GrBackendApi::kDirect3D: { 87 #ifdef SK_DIRECT3D 88 DXGI_FORMAT dxgiFormat; 89 SkAssertResult(format.asDxgiFormat(&dxgiFormat)); 90 switch (dxgiFormat) { 91 case DXGI_FORMAT_BC1_UNORM: 92 return SkImage::CompressionType::kBC1_RGBA8_UNORM; 93 default: 94 return SkImage::CompressionType::kNone; 95 } 96 #endif 97 break; 98 } 99 case GrBackendApi::kDawn: { 100 return SkImage::CompressionType::kNone; 101 } 102 case GrBackendApi::kMock: { 103 return format.asMockCompressionType(); 104 } 105 } 106 return SkImage::CompressionType::kNone; 107 } 108 GrBackendFormatBytesPerBlock(const GrBackendFormat & format)109size_t GrBackendFormatBytesPerBlock(const GrBackendFormat& format) { 110 switch (format.backend()) { 111 case GrBackendApi::kOpenGL: { 112 #ifdef SK_GL 113 GrGLFormat glFormat = format.asGLFormat(); 114 return GrGLFormatBytesPerBlock(glFormat); 115 #endif 116 break; 117 } 118 case GrBackendApi::kVulkan: { 119 #ifdef SK_VULKAN 120 VkFormat vkFormat; 121 SkAssertResult(format.asVkFormat(&vkFormat)); 122 return GrVkFormatBytesPerBlock(vkFormat); 123 #endif 124 break; 125 } 126 case GrBackendApi::kMetal: { 127 #ifdef SK_METAL 128 return GrMtlBackendFormatBytesPerBlock(format); 129 #endif 130 break; 131 } 132 case GrBackendApi::kDirect3D: { 133 #ifdef SK_DIRECT3D 134 DXGI_FORMAT dxgiFormat; 135 SkAssertResult(format.asDxgiFormat(&dxgiFormat)); 136 return GrDxgiFormatBytesPerBlock(dxgiFormat); 137 #endif 138 break; 139 } 140 case GrBackendApi::kDawn: { 141 #ifdef SK_DAWN 142 wgpu::TextureFormat dawnFormat; 143 SkAssertResult(format.asDawnFormat(&dawnFormat)); 144 return GrDawnBytesPerBlock(dawnFormat); 145 #endif 146 break; 147 } 148 case GrBackendApi::kMock: { 149 SkImage::CompressionType compression = format.asMockCompressionType(); 150 if (compression != SkImage::CompressionType::kNone) { 151 return GrCompressedRowBytes(compression, 1); 152 } else if (format.isMockStencilFormat()) { 153 static constexpr int kMockStencilSize = 4; 154 return kMockStencilSize; 155 } 156 return GrColorTypeBytesPerPixel(format.asMockColorType()); 157 } 158 } 159 return 0; 160 } 161 GrBackendFormatBytesPerPixel(const GrBackendFormat & format)162size_t GrBackendFormatBytesPerPixel(const GrBackendFormat& format) { 163 if (GrBackendFormatToCompressionType(format) != SkImage::CompressionType::kNone) { 164 return 0; 165 } 166 return GrBackendFormatBytesPerBlock(format); 167 } 168 GrBackendFormatStencilBits(const GrBackendFormat & format)169int GrBackendFormatStencilBits(const GrBackendFormat& format) { 170 switch (format.backend()) { 171 case GrBackendApi::kOpenGL: { 172 #ifdef SK_GL 173 GrGLFormat glFormat = format.asGLFormat(); 174 return GrGLFormatStencilBits(glFormat); 175 #endif 176 break; 177 } 178 case GrBackendApi::kVulkan: { 179 #ifdef SK_VULKAN 180 VkFormat vkFormat; 181 SkAssertResult(format.asVkFormat(&vkFormat)); 182 return GrVkFormatStencilBits(vkFormat); 183 #endif 184 break; 185 } 186 case GrBackendApi::kMetal: { 187 #ifdef SK_METAL 188 return GrMtlBackendFormatStencilBits(format); 189 #endif 190 break; 191 } 192 case GrBackendApi::kDirect3D: { 193 #ifdef SK_DIRECT3D 194 DXGI_FORMAT dxgiFormat; 195 SkAssertResult(format.asDxgiFormat(&dxgiFormat)); 196 return GrDxgiFormatStencilBits(dxgiFormat); 197 #endif 198 break; 199 } 200 case GrBackendApi::kDawn: { 201 #ifdef SK_DAWN 202 wgpu::TextureFormat dawnFormat; 203 SkAssertResult(format.asDawnFormat(&dawnFormat)); 204 return GrDawnFormatStencilBits(dawnFormat); 205 #endif 206 break; 207 } 208 case GrBackendApi::kMock: { 209 if (format.isMockStencilFormat()) { 210 static constexpr int kMockStencilBits = 8; 211 return kMockStencilBits; 212 } 213 } 214 } 215 return 0; 216 } 217