• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2025 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/graphite/TextureFormat.h"
9 
10  #include "include/core/SkColor.h"
11 
12 namespace skgpu::graphite {
13 
TextureFormatName(TextureFormat format)14 const char* TextureFormatName(TextureFormat format) {
15     switch (format) {
16         case TextureFormat::kUnsupported:    return "Unsupported";
17         case TextureFormat::kR8:             return "R8";
18         case TextureFormat::kR16:            return "R16";
19         case TextureFormat::kR16F:           return "R16F";
20         case TextureFormat::kR32F:           return "R32F";
21         case TextureFormat::kA8:             return "A8";
22         case TextureFormat::kRG8:            return "RG8";
23         case TextureFormat::kRG16:           return "RG16";
24         case TextureFormat::kRG16F:          return "RG16F";
25         case TextureFormat::kRG32F:          return "RG32F";
26         case TextureFormat::kRGB8:           return "RGB8";
27         case TextureFormat::kBGR8:           return "BGR8";
28         case TextureFormat::kB5_G6_R5:       return "B5_G6_R5";
29         case TextureFormat::kR5_G6_B5:       return "R5_G6_B5";
30         case TextureFormat::kRGB16:          return "RGB16";
31         case TextureFormat::kRGB16F:         return "RGB16F";
32         case TextureFormat::kRGB32F:         return "RGB32F";
33         case TextureFormat::kRGB8_sRGB:      return "RGB8_sRGB";
34         case TextureFormat::kBGR10_XR:       return "BGR10_XR";
35         case TextureFormat::kRGBA8:          return "RGBA8";
36         case TextureFormat::kRGBA16:         return "RBGA16";
37         case TextureFormat::kRGBA16F:        return "RGBA16F";
38         case TextureFormat::kRGBA32F:        return "RGBA32F";
39         case TextureFormat::kRGB10_A2:       return "RGB10_A2";
40         case TextureFormat::kRGBA8_sRGB:     return "RGBA8_sRGB";
41         case TextureFormat::kBGRA8:          return "BGRA8";
42         case TextureFormat::kBGR10_A2:       return "BGR10_A2";
43         case TextureFormat::kBGRA8_sRGB:     return "BGRA8_sRGB";
44         case TextureFormat::kABGR4:          return "ABGR4";
45         case TextureFormat::kARGB4:          return "ARGB4";
46         case TextureFormat::kBGRA10x6_XR:    return "BGRA10x6_XR";
47         case TextureFormat::kRGB8_ETC2:      return "RGB8_ETC2";
48         case TextureFormat::kRGB8_ETC2_sRGB: return "RGB8_ETC2_sRGB";
49         case TextureFormat::kRGB8_BC1:       return "RGB8_BC1";
50         case TextureFormat::kRGBA8_BC1:      return "RGBA8_BC1";
51         case TextureFormat::kRGBA8_BC1_sRGB: return "RGBA8_BC1_sRGB";
52         case TextureFormat::kYUV8_P2_420:    return "YUV8_P2_420";
53         case TextureFormat::kYUV8_P3_420:    return "YUV8_P3_420";
54         case TextureFormat::kYUV10x6_P2_420: return "YUV10x6_P2_420";
55         case TextureFormat::kExternal:       return "External";
56         case TextureFormat::kS8:             return "S8";
57         case TextureFormat::kD16:            return "D16";
58         case TextureFormat::kD32F:           return "D32F";
59         case TextureFormat::kD24_S8:         return "D24_S8";
60         case TextureFormat::kD32F_S8:        return "D32F_S8";
61     }
62     SkUNREACHABLE;
63 }
64 
TextureFormatCompressionType(TextureFormat format)65 SkTextureCompressionType TextureFormatCompressionType(TextureFormat format) {
66     switch (format) {
67         case TextureFormat::kRGB8_ETC2:      [[fallthrough]];
68         case TextureFormat::kRGB8_ETC2_sRGB: return SkTextureCompressionType::kETC2_RGB8_UNORM;
69         case TextureFormat::kRGB8_BC1:       return SkTextureCompressionType::kBC1_RGB8_UNORM;
70         case TextureFormat::kRGBA8_BC1:      [[fallthrough]];
71         case TextureFormat::kRGBA8_BC1_sRGB: return SkTextureCompressionType::kBC1_RGBA8_UNORM;
72         default:                             return SkTextureCompressionType::kNone;
73     }
74 }
75 
TextureFormatBytesPerBlock(TextureFormat format)76 size_t TextureFormatBytesPerBlock(TextureFormat format) {
77     switch (format) {
78         case TextureFormat::kUnsupported: return 0;
79         case TextureFormat::kR8:          return 1;
80         case TextureFormat::kR16:         return 2;
81         case TextureFormat::kR16F:        return 2;
82         case TextureFormat::kR32F:        return 4;
83         case TextureFormat::kA8:          return 1;
84         case TextureFormat::kRG8:         return 2;
85         case TextureFormat::kRG16:        return 4;
86         case TextureFormat::kRG16F:       return 4;
87         case TextureFormat::kRG32F:       return 8;
88         case TextureFormat::kRGB8:        return 3;
89         case TextureFormat::kBGR8:        return 3;
90         case TextureFormat::kB5_G6_R5:    return 2;
91         case TextureFormat::kR5_G6_B5:    return 2;
92         case TextureFormat::kRGB16:       return 6;
93         case TextureFormat::kRGB16F:      return 6;
94         case TextureFormat::kRGB32F:      return 12;
95         case TextureFormat::kRGB8_sRGB:   return 3;
96         case TextureFormat::kBGR10_XR:    return 4;
97         case TextureFormat::kRGBA8:       return 4;
98         case TextureFormat::kRGBA16:      return 8;
99         case TextureFormat::kRGBA16F:     return 8;
100         case TextureFormat::kRGBA32F:     return 16;
101         case TextureFormat::kRGB10_A2:    return 4;
102         case TextureFormat::kRGBA8_sRGB:  return 4;
103         case TextureFormat::kBGRA8:       return 4;
104         case TextureFormat::kBGR10_A2:    return 4;
105         case TextureFormat::kBGRA8_sRGB:  return 4;
106         case TextureFormat::kABGR4:       return 2;
107         case TextureFormat::kARGB4:       return 2;
108         case TextureFormat::kBGRA10x6_XR: return 8;
109         case TextureFormat::kS8:          return 1;
110         case TextureFormat::kD16:         return 2;
111         case TextureFormat::kD32F:        return 4;
112         case TextureFormat::kD24_S8:      return 4;
113         case TextureFormat::kD32F_S8:     return 8;
114         // NOTE: For compressed formats, the block size refers to an actual compressed block of
115         // multiple texels, whereas with other formats the block size represents a single pixel.
116         case TextureFormat::kRGB8_ETC2:
117         case TextureFormat::kRGB8_ETC2_sRGB:
118         case TextureFormat::kRGB8_BC1:
119         case TextureFormat::kRGBA8_BC1:
120         case TextureFormat::kRGBA8_BC1_sRGB:
121             return 8;
122         // NOTE: We don't actually know the size of external formats, so this is an arbitrary value.
123         // We will see external formats only in wrapped SkImages, so this won't impact Skia's
124         // internal budgeting.
125         case TextureFormat::kExternal:
126             return 4;
127         // TODO(b/401016699): We are just over estimating this value to be used in gpu size
128         // calculations even though the actually size is probably less. We should instead treat
129         // planar formats similar to compressed textures that go through their own special query for
130         // calculating size.
131         case TextureFormat::kYUV8_P2_420:
132         case TextureFormat::kYUV8_P3_420:
133             return 3;
134         case TextureFormat::kYUV10x6_P2_420:
135             return 6;
136     }
137     SkUNREACHABLE;
138 }
139 
TextureFormatChannelMask(TextureFormat format)140 uint32_t TextureFormatChannelMask(TextureFormat format) {
141     switch (format) {
142         case TextureFormat::kA8:             return kAlpha_SkColorChannelFlag;
143 
144         case TextureFormat::kR8:             [[fallthrough]];
145         case TextureFormat::kR16:
146         case TextureFormat::kR16F:
147         case TextureFormat::kR32F:           return kRed_SkColorChannelFlag;
148 
149         case TextureFormat::kRG8:            [[fallthrough]];
150         case TextureFormat::kRG16:
151         case TextureFormat::kRG16F:
152         case TextureFormat::kRG32F:          return kRG_SkColorChannelFlags;
153 
154         case TextureFormat::kRGB8:           [[fallthrough]];
155         case TextureFormat::kBGR8:
156         case TextureFormat::kB5_G6_R5:
157         case TextureFormat::kR5_G6_B5:
158         case TextureFormat::kRGB16:
159         case TextureFormat::kRGB16F:
160         case TextureFormat::kRGB32F:
161         case TextureFormat::kRGB8_sRGB:
162         case TextureFormat::kBGR10_XR:
163         case TextureFormat::kRGB8_ETC2:
164         case TextureFormat::kRGB8_ETC2_sRGB:
165         case TextureFormat::kRGB8_BC1:
166         case TextureFormat::kYUV8_P2_420:
167         case TextureFormat::kYUV8_P3_420:
168         case TextureFormat::kYUV10x6_P2_420: return kRGB_SkColorChannelFlags;
169 
170         case TextureFormat::kRGBA8:          [[fallthrough]];
171         case TextureFormat::kRGBA16:
172         case TextureFormat::kRGBA16F:
173         case TextureFormat::kRGBA32F:
174         case TextureFormat::kRGB10_A2:
175         case TextureFormat::kRGBA8_sRGB:
176         case TextureFormat::kBGRA8:
177         case TextureFormat::kBGR10_A2:
178         case TextureFormat::kBGRA8_sRGB:
179         case TextureFormat::kABGR4:
180         case TextureFormat::kARGB4:
181         case TextureFormat::kBGRA10x6_XR:
182         case TextureFormat::kRGBA8_BC1:
183         case TextureFormat::kRGBA8_BC1_sRGB:
184         case TextureFormat::kExternal:       return kRGBA_SkColorChannelFlags;
185 
186         case TextureFormat::kS8:             [[fallthrough]];
187         case TextureFormat::kD16:
188         case TextureFormat::kD32F:
189         case TextureFormat::kD24_S8:
190         case TextureFormat::kD32F_S8:
191         case TextureFormat::kUnsupported:    return 0;
192     }
193     SkUNREACHABLE;
194 }
195 
TextureFormatIsDepthOrStencil(TextureFormat format)196 bool TextureFormatIsDepthOrStencil(TextureFormat format) {
197     switch (format) {
198         case TextureFormat::kS8:      [[fallthrough]];
199         case TextureFormat::kD16:
200         case TextureFormat::kD32F:
201         case TextureFormat::kD24_S8:
202         case TextureFormat::kD32F_S8:
203             return true;
204         default:
205             return false;
206     }
207 }
208 
TextureFormatHasDepth(TextureFormat format)209 bool TextureFormatHasDepth(TextureFormat format) {
210     switch (format) {
211         case TextureFormat::kD16:     [[fallthrough]];
212         case TextureFormat::kD32F:
213         case TextureFormat::kD24_S8:
214         case TextureFormat::kD32F_S8:
215             return true;
216         default:
217             return false;
218     }
219 }
220 
TextureFormatHasStencil(TextureFormat format)221 bool TextureFormatHasStencil(TextureFormat format) {
222     switch (format) {
223         case TextureFormat::kS8:      [[fallthrough]];
224         case TextureFormat::kD24_S8:
225         case TextureFormat::kD32F_S8:
226             return true;
227         default:
228             return false;
229     }
230 }
231 
TextureFormatIsMultiplanar(TextureFormat format)232 bool TextureFormatIsMultiplanar(TextureFormat format) {
233     switch (format) {
234         case TextureFormat::kYUV8_P2_420:    [[fallthrough]];
235         case TextureFormat::kYUV8_P3_420:
236         case TextureFormat::kYUV10x6_P2_420:
237             return true;
238         default:
239             return false;
240     }
241 }
242 
243 } // namespace skgpu::graphite
244