blob: b50e5741c3240b49a557c7291d75c6bdc7c4eeb7 [file] [log] [blame]
Greg Daniel0eca74c2020-10-01 13:46:00 -04001/*
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
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 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
97size_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);
140 }
141 return GrColorTypeBytesPerPixel(format.asMockColorType());
142 }
143 }
144 return 0;
145}
146
147size_t GrBackendFormatBytesPerPixel(const GrBackendFormat& format) {
148 if (GrBackendFormatToCompressionType(format) != SkImage::CompressionType::kNone) {
149 return 0;
150 }
151 return GrBackendFormatBytesPerBlock(format);
152}