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