1 /*
2 * Copyright 2012 Google Inc.
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 #ifndef GrGLUtil_DEFINED
9 #define GrGLUtil_DEFINED
10
11 #include "include/gpu/gl/GrGLInterface.h"
12 #include "include/private/GrTypesPriv.h"
13 #include "include/private/SkImageInfoPriv.h"
14 #include "src/gpu/GrDataUtils.h"
15 #include "src/gpu/GrStencilSettings.h"
16 #include "src/gpu/gl/GrGLDefines.h"
17
18 class SkMatrix;
19
20 ////////////////////////////////////////////////////////////////////////////////
21
22 typedef uint32_t GrGLVersion;
23 typedef uint32_t GrGLSLVersion;
24 typedef uint64_t GrGLDriverVersion;
25
26 #define GR_GL_VER(major, minor) ((static_cast<uint32_t>(major) << 16) | \
27 static_cast<uint32_t>(minor))
28 #define GR_GLSL_VER(major, minor) ((static_cast<uint32_t>(major) << 16) | \
29 static_cast<uint32_t>(minor))
30 #define GR_GL_DRIVER_VER(major, minor, point) ((static_cast<uint64_t>(major) << 32) | \
31 (static_cast<uint64_t>(minor) << 16) | \
32 static_cast<uint64_t>(point))
33
34 #define GR_GL_MAJOR_VER(version) (static_cast<uint32_t>(version) >> 16)
35 #define GR_GL_MINOR_VER(version) (static_cast<uint32_t>(version) & 0xFFFF)
36
37 #define GR_GL_INVALID_VER GR_GL_VER(0, 0)
38 #define GR_GLSL_INVALID_VER GR_GLSL_VER(0, 0)
39 #define GR_GL_DRIVER_UNKNOWN_VER GR_GL_DRIVER_VER(0, 0, 0)
40
GrGLFormatChannels(GrGLFormat format)41 static constexpr uint32_t GrGLFormatChannels(GrGLFormat format) {
42 switch (format) {
43 case GrGLFormat::kUnknown: return 0;
44 case GrGLFormat::kRGBA8: return kRGBA_SkColorChannelFlags;
45 case GrGLFormat::kR8: return kRed_SkColorChannelFlag;
46 case GrGLFormat::kALPHA8: return kAlpha_SkColorChannelFlag;
47 case GrGLFormat::kLUMINANCE8: return kGray_SkColorChannelFlag;
48 case GrGLFormat::kLUMINANCE8_ALPHA8: return kGrayAlpha_SkColorChannelFlags;
49 case GrGLFormat::kBGRA8: return kRGBA_SkColorChannelFlags;
50 case GrGLFormat::kRGB565: return kRGB_SkColorChannelFlags;
51 case GrGLFormat::kRGBA16F: return kRGBA_SkColorChannelFlags;
52 case GrGLFormat::kR16F: return kRed_SkColorChannelFlag;
53 case GrGLFormat::kRGB8: return kRGB_SkColorChannelFlags;
54 case GrGLFormat::kRG8: return kRG_SkColorChannelFlags;
55 case GrGLFormat::kRGB10_A2: return kRGBA_SkColorChannelFlags;
56 case GrGLFormat::kRGBA4: return kRGBA_SkColorChannelFlags;
57 case GrGLFormat::kSRGB8_ALPHA8: return kRGBA_SkColorChannelFlags;
58 case GrGLFormat::kCOMPRESSED_ETC1_RGB8: return kRGB_SkColorChannelFlags;
59 case GrGLFormat::kCOMPRESSED_RGB8_ETC2: return kRGB_SkColorChannelFlags;
60 case GrGLFormat::kCOMPRESSED_RGB8_BC1: return kRGB_SkColorChannelFlags;
61 case GrGLFormat::kCOMPRESSED_RGBA8_BC1: return kRGBA_SkColorChannelFlags;
62 case GrGLFormat::kR16: return kRed_SkColorChannelFlag;
63 case GrGLFormat::kRG16: return kRG_SkColorChannelFlags;
64 case GrGLFormat::kRGBA16: return kRGBA_SkColorChannelFlags;
65 case GrGLFormat::kRG16F: return kRG_SkColorChannelFlags;
66 case GrGLFormat::kLUMINANCE16F: return kGray_SkColorChannelFlag;
67 case GrGLFormat::kSTENCIL_INDEX8: return 0;
68 case GrGLFormat::kSTENCIL_INDEX16: return 0;
69 case GrGLFormat::kDEPTH24_STENCIL8: return 0;
70 }
71 SkUNREACHABLE;
72 }
73
GrGLFormatDesc(GrGLFormat format)74 static constexpr GrColorFormatDesc GrGLFormatDesc(GrGLFormat format) {
75 switch (format) {
76 case GrGLFormat::kUnknown: return GrColorFormatDesc::MakeInvalid();
77
78 case GrGLFormat::kRGBA8:
79 return GrColorFormatDesc::MakeRGBA(8, GrColorTypeEncoding::kUnorm);
80 case GrGLFormat::kR8:
81 return GrColorFormatDesc::MakeR(8, GrColorTypeEncoding::kUnorm);
82 case GrGLFormat::kALPHA8:
83 return GrColorFormatDesc::MakeAlpha(8, GrColorTypeEncoding::kUnorm);
84 case GrGLFormat::kLUMINANCE8:
85 return GrColorFormatDesc::MakeGray(8, GrColorTypeEncoding::kUnorm);
86 case GrGLFormat::kLUMINANCE8_ALPHA8:
87 return GrColorFormatDesc::MakeGrayAlpha(8, GrColorTypeEncoding::kUnorm);
88 case GrGLFormat::kBGRA8:
89 return GrColorFormatDesc::MakeRGBA(8, GrColorTypeEncoding::kUnorm);
90 case GrGLFormat::kRGB565:
91 return GrColorFormatDesc::MakeRGB(5, 6, 5, GrColorTypeEncoding::kUnorm);
92 case GrGLFormat::kRGBA16F:
93 return GrColorFormatDesc::MakeRGBA(16, GrColorTypeEncoding::kFloat);
94 case GrGLFormat::kR16F:
95 return GrColorFormatDesc::MakeR(16, GrColorTypeEncoding::kFloat);
96 case GrGLFormat::kRGB8:
97 return GrColorFormatDesc::MakeRGB(8, GrColorTypeEncoding::kUnorm);
98 case GrGLFormat::kRG8:
99 return GrColorFormatDesc::MakeRG(8, GrColorTypeEncoding::kUnorm);
100 case GrGLFormat::kRGB10_A2:
101 return GrColorFormatDesc::MakeRGBA(10, 2, GrColorTypeEncoding::kUnorm);
102 case GrGLFormat::kRGBA4:
103 return GrColorFormatDesc::MakeRGBA(4, GrColorTypeEncoding::kUnorm);
104 case GrGLFormat::kSRGB8_ALPHA8:
105 return GrColorFormatDesc::MakeRGBA(8, GrColorTypeEncoding::kSRGBUnorm);
106 case GrGLFormat::kR16:
107 return GrColorFormatDesc::MakeR(16, GrColorTypeEncoding::kUnorm);
108 case GrGLFormat::kRG16:
109 return GrColorFormatDesc::MakeRG(16, GrColorTypeEncoding::kUnorm);
110 case GrGLFormat::kRGBA16:
111 return GrColorFormatDesc::MakeRGBA(16, GrColorTypeEncoding::kUnorm);
112 case GrGLFormat::kRG16F:
113 return GrColorFormatDesc::MakeRG(16, GrColorTypeEncoding::kFloat);
114 case GrGLFormat::kLUMINANCE16F:
115 return GrColorFormatDesc::MakeGray(16, GrColorTypeEncoding::kFloat);
116
117 // Compressed texture formats are not expected to have a description.
118 case GrGLFormat::kCOMPRESSED_ETC1_RGB8: return GrColorFormatDesc::MakeInvalid();
119 case GrGLFormat::kCOMPRESSED_RGB8_ETC2: return GrColorFormatDesc::MakeInvalid();
120 case GrGLFormat::kCOMPRESSED_RGB8_BC1: return GrColorFormatDesc::MakeInvalid();
121 case GrGLFormat::kCOMPRESSED_RGBA8_BC1: return GrColorFormatDesc::MakeInvalid();
122
123 // This type only describes color channels.
124 case GrGLFormat::kSTENCIL_INDEX8: return GrColorFormatDesc::MakeInvalid();
125 case GrGLFormat::kSTENCIL_INDEX16: return GrColorFormatDesc::MakeInvalid();
126 case GrGLFormat::kDEPTH24_STENCIL8: return GrColorFormatDesc::MakeInvalid();
127 }
128 SkUNREACHABLE;
129 }
130
131 /**
132 * The Vendor and Renderer enum values are lazily updated as required.
133 */
134 enum class GrGLVendor {
135 kARM,
136 kGoogle,
137 kImagination,
138 kIntel,
139 kQualcomm,
140 kNVIDIA,
141 kATI,
142
143 kOther
144 };
145
146 enum class GrGLRenderer {
147 kTegra_PreK1, // Legacy Tegra architecture (pre-K1).
148 kTegra, // Tegra with the same architecture as NVIDIA desktop GPUs (K1+).
149
150 kPowerVR54x,
151 kPowerVRRogue,
152
153 kAdreno3xx,
154 kAdreno430,
155 kAdreno4xx_other,
156 kAdreno530,
157 kAdreno5xx_other,
158 kAdreno615, // Pixel3a
159 kAdreno620, // Pixel5
160 kAdreno630, // Pixel3
161 kAdreno640, // Pixel4
162 kAdreno6xx_other,
163
164 kGoogleSwiftShader,
165
166 /** Intel GPU families, ordered by generation **/
167 // 6th gen
168 kIntelSandyBridge,
169
170 // 7th gen
171 kIntelIvyBridge,
172 kIntelValleyView, // aka BayTrail
173 kIntelHaswell,
174
175 // 8th gen
176 kIntelCherryView, // aka Braswell
177 kIntelBroadwell,
178
179 // 9th gen
180 kIntelApolloLake,
181 kIntelSkyLake,
182 kIntelGeminiLake,
183 kIntelKabyLake,
184 kIntelCoffeeLake,
185
186 // 11th gen
187 kIntelIceLake,
188
189 kGalliumLLVM,
190
191 kVirgl,
192
193 kMali4xx,
194 /** G-3x, G-5x, or G-7x */
195 kMaliG,
196 /** T-6xx, T-7xx, or T-8xx */
197 kMaliT,
198
199 kAMDRadeonHD7xxx, // AMD Radeon HD 7000 Series
200 kAMDRadeonR9M3xx, // AMD Radeon R9 M300 Series
201 kAMDRadeonR9M4xx, // AMD Radeon R9 M400 Series
202 kAMDRadeonPro5xxx, // AMD Radeon Pro 5000 Series
203 kAMDRadeonProVegaxx, // AMD Radeon Pro Vega
204
205 kOther
206 };
207
208 enum class GrGLDriver {
209 kMesa,
210 kNVIDIA,
211 kIntel,
212 kSwiftShader,
213 kQualcomm,
214 kFreedreno,
215 kAndroidEmulator,
216 kImagination,
217 kARM,
218 kUnknown
219 };
220
221 enum class GrGLANGLEBackend {
222 kUnknown,
223 kD3D9,
224 kD3D11,
225 kOpenGL
226 };
227
228 ////////////////////////////////////////////////////////////////////////////////
229
230 /**
231 * Some drivers want the var-int arg to be zero-initialized on input.
232 */
233 #define GR_GL_INIT_ZERO 0
234 #define GR_GL_GetIntegerv(gl, e, p) \
235 do { \
236 *(p) = GR_GL_INIT_ZERO; \
237 GR_GL_CALL(gl, GetIntegerv(e, p)); \
238 } while (0)
239
240 #define GR_GL_GetFramebufferAttachmentParameteriv(gl, t, a, pname, p) \
241 do { \
242 *(p) = GR_GL_INIT_ZERO; \
243 GR_GL_CALL(gl, GetFramebufferAttachmentParameteriv(t, a, pname, p)); \
244 } while (0)
245
246 #define GR_GL_GetInternalformativ(gl, t, f, n, s, p) \
247 do { \
248 *(p) = GR_GL_INIT_ZERO; \
249 GR_GL_CALL(gl, GetInternalformativ(t, f, n, s, p)); \
250 } while (0)
251
252 #define GR_GL_GetNamedFramebufferAttachmentParameteriv(gl, fb, a, pname, p) \
253 do { \
254 *(p) = GR_GL_INIT_ZERO; \
255 GR_GL_CALL(gl, GetNamedFramebufferAttachmentParameteriv(fb, a, pname, p)); \
256 } while (0)
257
258 #define GR_GL_GetRenderbufferParameteriv(gl, t, pname, p) \
259 do { \
260 *(p) = GR_GL_INIT_ZERO; \
261 GR_GL_CALL(gl, GetRenderbufferParameteriv(t, pname, p)); \
262 } while (0)
263
264 #define GR_GL_GetTexLevelParameteriv(gl, t, l, pname, p) \
265 do { \
266 *(p) = GR_GL_INIT_ZERO; \
267 GR_GL_CALL(gl, GetTexLevelParameteriv(t, l, pname, p)); \
268 } while (0)
269
270 #define GR_GL_GetShaderPrecisionFormat(gl, st, pt, range, precision) \
271 do { \
272 (range)[0] = GR_GL_INIT_ZERO; \
273 (range)[1] = GR_GL_INIT_ZERO; \
274 (*precision) = GR_GL_INIT_ZERO; \
275 GR_GL_CALL(gl, GetShaderPrecisionFormat(st, pt, range, precision)); \
276 } while (0)
277
278 ////////////////////////////////////////////////////////////////////////////////
279
280 GrGLStandard GrGLGetStandardInUseFromString(const char* versionString);
281 GrGLSLVersion GrGLGetVersion(const GrGLInterface*);
282 GrGLSLVersion GrGLGetVersionFromString(const char*);
283
284 struct GrGLDriverInfo {
285 GrGLStandard fStandard = kNone_GrGLStandard;
286 GrGLVersion fVersion = GR_GL_INVALID_VER;
287 GrGLSLVersion fGLSLVersion = GR_GLSL_INVALID_VER;
288 GrGLVendor fVendor = GrGLVendor::kOther;
289 GrGLRenderer fRenderer = GrGLRenderer::kOther;
290 GrGLDriver fDriver = GrGLDriver::kUnknown;
291 GrGLDriverVersion fDriverVersion = GR_GL_DRIVER_UNKNOWN_VER;
292
293 GrGLANGLEBackend fANGLEBackend = GrGLANGLEBackend::kUnknown;
294 GrGLVendor fANGLEVendor = GrGLVendor::kOther;
295 GrGLRenderer fANGLERenderer = GrGLRenderer::kOther;
296 GrGLDriver fANGLEDriver = GrGLDriver::kUnknown;
297 GrGLDriverVersion fANGLEDriverVersion = GR_GL_DRIVER_UNKNOWN_VER;
298
299 // Are we running over the Chrome interprocess command buffer?
300 bool fIsOverCommandBuffer = false;
301 };
302
303 GrGLDriverInfo GrGLGetDriverInfo(const GrGLInterface*);
304
305 /**
306 * Helpers for glGetError()
307 */
308
309 void GrGLCheckErr(const GrGLInterface* gl,
310 const char* location,
311 const char* call);
312
313 ////////////////////////////////////////////////////////////////////////////////
314
315 /**
316 * Macros for using GrGLInterface to make GL calls
317 */
318
319 // Conditionally checks glGetError based on compile-time and run-time flags.
320 #if GR_GL_CHECK_ERROR
321 extern bool gCheckErrorGL;
322 #define GR_GL_CHECK_ERROR_IMPL(IFACE, X) \
323 do { \
324 if (gCheckErrorGL) { \
325 IFACE->checkError(GR_FILE_AND_LINE_STR, #X); \
326 } \
327 } while (false)
328 #else
329 #define GR_GL_CHECK_ERROR_IMPL(IFACE, X) \
330 do { \
331 } while (false)
332 #endif
333
334 // internal macro to conditionally log the gl call using SkDebugf based on
335 // compile-time and run-time flags.
336 #if GR_GL_LOG_CALLS
337 extern bool gLogCallsGL;
338 #define GR_GL_LOG_CALLS_IMPL(X) \
339 if (gLogCallsGL) \
340 SkDebugf(GR_FILE_AND_LINE_STR "GL: " #X "\n")
341 #else
342 #define GR_GL_LOG_CALLS_IMPL(X)
343 #endif
344
345 // makes a GL call on the interface and does any error checking and logging
346 #define GR_GL_CALL(IFACE, X) \
347 do { \
348 GR_GL_CALL_NOERRCHECK(IFACE, X); \
349 GR_GL_CHECK_ERROR_IMPL(IFACE, X); \
350 } while (false)
351
352 // Variant of above that always skips the error check. This is useful when
353 // the caller wants to do its own glGetError() call and examine the error value.
354 #define GR_GL_CALL_NOERRCHECK(IFACE, X) \
355 do { \
356 (IFACE)->fFunctions.f##X; \
357 GR_GL_LOG_CALLS_IMPL(X); \
358 } while (false)
359
360 // same as GR_GL_CALL but stores the return value of the gl call in RET
361 #define GR_GL_CALL_RET(IFACE, RET, X) \
362 do { \
363 GR_GL_CALL_RET_NOERRCHECK(IFACE, RET, X); \
364 GR_GL_CHECK_ERROR_IMPL(IFACE, X); \
365 } while (false)
366
367 // same as GR_GL_CALL_RET but always skips the error check.
368 #define GR_GL_CALL_RET_NOERRCHECK(IFACE, RET, X) \
369 do { \
370 (RET) = (IFACE)->fFunctions.f##X; \
371 GR_GL_LOG_CALLS_IMPL(X); \
372 } while (false)
373
GrGLFormatFromGLEnum(GrGLenum glFormat)374 static constexpr GrGLFormat GrGLFormatFromGLEnum(GrGLenum glFormat) {
375 switch (glFormat) {
376 case GR_GL_RGBA8: return GrGLFormat::kRGBA8;
377 case GR_GL_R8: return GrGLFormat::kR8;
378 case GR_GL_ALPHA8: return GrGLFormat::kALPHA8;
379 case GR_GL_LUMINANCE8: return GrGLFormat::kLUMINANCE8;
380 case GR_GL_LUMINANCE8_ALPHA8: return GrGLFormat::kLUMINANCE8_ALPHA8;
381 case GR_GL_BGRA8: return GrGLFormat::kBGRA8;
382 case GR_GL_RGB565: return GrGLFormat::kRGB565;
383 case GR_GL_RGBA16F: return GrGLFormat::kRGBA16F;
384 case GR_GL_LUMINANCE16F: return GrGLFormat::kLUMINANCE16F;
385 case GR_GL_R16F: return GrGLFormat::kR16F;
386 case GR_GL_RGB8: return GrGLFormat::kRGB8;
387 case GR_GL_RG8: return GrGLFormat::kRG8;
388 case GR_GL_RGB10_A2: return GrGLFormat::kRGB10_A2;
389 case GR_GL_RGBA4: return GrGLFormat::kRGBA4;
390 case GR_GL_SRGB8_ALPHA8: return GrGLFormat::kSRGB8_ALPHA8;
391 case GR_GL_COMPRESSED_ETC1_RGB8: return GrGLFormat::kCOMPRESSED_ETC1_RGB8;
392 case GR_GL_COMPRESSED_RGB8_ETC2: return GrGLFormat::kCOMPRESSED_RGB8_ETC2;
393 case GR_GL_COMPRESSED_RGB_S3TC_DXT1_EXT: return GrGLFormat::kCOMPRESSED_RGB8_BC1;
394 case GR_GL_COMPRESSED_RGBA_S3TC_DXT1_EXT: return GrGLFormat::kCOMPRESSED_RGBA8_BC1;
395 case GR_GL_R16: return GrGLFormat::kR16;
396 case GR_GL_RG16: return GrGLFormat::kRG16;
397 case GR_GL_RGBA16: return GrGLFormat::kRGBA16;
398 case GR_GL_RG16F: return GrGLFormat::kRG16F;
399 case GR_GL_STENCIL_INDEX8: return GrGLFormat::kSTENCIL_INDEX8;
400 case GR_GL_STENCIL_INDEX16: return GrGLFormat::kSTENCIL_INDEX16;
401 case GR_GL_DEPTH24_STENCIL8: return GrGLFormat::kDEPTH24_STENCIL8;
402
403
404 default: return GrGLFormat::kUnknown;
405 }
406 }
407
408 /** Returns either the sized internal format or compressed internal format of the GrGLFormat. */
GrGLFormatToEnum(GrGLFormat format)409 static constexpr GrGLenum GrGLFormatToEnum(GrGLFormat format) {
410 switch (format) {
411 case GrGLFormat::kRGBA8: return GR_GL_RGBA8;
412 case GrGLFormat::kR8: return GR_GL_R8;
413 case GrGLFormat::kALPHA8: return GR_GL_ALPHA8;
414 case GrGLFormat::kLUMINANCE8: return GR_GL_LUMINANCE8;
415 case GrGLFormat::kLUMINANCE8_ALPHA8: return GR_GL_LUMINANCE8_ALPHA8;
416 case GrGLFormat::kBGRA8: return GR_GL_BGRA8;
417 case GrGLFormat::kRGB565: return GR_GL_RGB565;
418 case GrGLFormat::kRGBA16F: return GR_GL_RGBA16F;
419 case GrGLFormat::kLUMINANCE16F: return GR_GL_LUMINANCE16F;
420 case GrGLFormat::kR16F: return GR_GL_R16F;
421 case GrGLFormat::kRGB8: return GR_GL_RGB8;
422 case GrGLFormat::kRG8: return GR_GL_RG8;
423 case GrGLFormat::kRGB10_A2: return GR_GL_RGB10_A2;
424 case GrGLFormat::kRGBA4: return GR_GL_RGBA4;
425 case GrGLFormat::kSRGB8_ALPHA8: return GR_GL_SRGB8_ALPHA8;
426 case GrGLFormat::kCOMPRESSED_ETC1_RGB8: return GR_GL_COMPRESSED_ETC1_RGB8;
427 case GrGLFormat::kCOMPRESSED_RGB8_ETC2: return GR_GL_COMPRESSED_RGB8_ETC2;
428 case GrGLFormat::kCOMPRESSED_RGB8_BC1: return GR_GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
429 case GrGLFormat::kCOMPRESSED_RGBA8_BC1: return GR_GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
430 case GrGLFormat::kR16: return GR_GL_R16;
431 case GrGLFormat::kRG16: return GR_GL_RG16;
432 case GrGLFormat::kRGBA16: return GR_GL_RGBA16;
433 case GrGLFormat::kRG16F: return GR_GL_RG16F;
434 case GrGLFormat::kSTENCIL_INDEX8: return GR_GL_STENCIL_INDEX8;
435 case GrGLFormat::kSTENCIL_INDEX16: return GR_GL_STENCIL_INDEX16;
436 case GrGLFormat::kDEPTH24_STENCIL8: return GR_GL_DEPTH24_STENCIL8;
437 case GrGLFormat::kUnknown: return 0;
438 }
439 SkUNREACHABLE;
440 }
441
GrGLFormatBytesPerBlock(GrGLFormat format)442 static constexpr size_t GrGLFormatBytesPerBlock(GrGLFormat format) {
443 switch (format) {
444 case GrGLFormat::kRGBA8: return 4;
445 case GrGLFormat::kR8: return 1;
446 case GrGLFormat::kALPHA8: return 1;
447 case GrGLFormat::kLUMINANCE8: return 1;
448 case GrGLFormat::kLUMINANCE8_ALPHA8: return 2;
449 case GrGLFormat::kBGRA8: return 4;
450 case GrGLFormat::kRGB565: return 2;
451 case GrGLFormat::kRGBA16F: return 8;
452 case GrGLFormat::kLUMINANCE16F: return 2;
453 case GrGLFormat::kR16F: return 2;
454 // We assume the GPU stores this format 4 byte aligned
455 case GrGLFormat::kRGB8: return 4;
456 case GrGLFormat::kRG8: return 2;
457 case GrGLFormat::kRGB10_A2: return 4;
458 case GrGLFormat::kRGBA4: return 2;
459 case GrGLFormat::kSRGB8_ALPHA8: return 4;
460 case GrGLFormat::kCOMPRESSED_ETC1_RGB8: return 8;
461 case GrGLFormat::kCOMPRESSED_RGB8_ETC2: return 8;
462 case GrGLFormat::kCOMPRESSED_RGB8_BC1: return 8;
463 case GrGLFormat::kCOMPRESSED_RGBA8_BC1: return 8;
464 case GrGLFormat::kR16: return 2;
465 case GrGLFormat::kRG16: return 4;
466 case GrGLFormat::kRGBA16: return 8;
467 case GrGLFormat::kRG16F: return 4;
468 case GrGLFormat::kSTENCIL_INDEX8: return 1;
469 case GrGLFormat::kSTENCIL_INDEX16: return 2;
470 case GrGLFormat::kDEPTH24_STENCIL8: return 4;
471 case GrGLFormat::kUnknown: return 0;
472 }
473 SkUNREACHABLE;
474 }
475
GrGLFormatStencilBits(GrGLFormat format)476 static constexpr int GrGLFormatStencilBits(GrGLFormat format) {
477 switch (format) {
478 case GrGLFormat::kSTENCIL_INDEX8:
479 return 8;
480 case GrGLFormat::kSTENCIL_INDEX16:
481 return 16;
482 case GrGLFormat::kDEPTH24_STENCIL8:
483 return 8;
484 case GrGLFormat::kCOMPRESSED_ETC1_RGB8:
485 case GrGLFormat::kCOMPRESSED_RGB8_ETC2:
486 case GrGLFormat::kCOMPRESSED_RGB8_BC1:
487 case GrGLFormat::kCOMPRESSED_RGBA8_BC1:
488 case GrGLFormat::kRGBA8:
489 case GrGLFormat::kR8:
490 case GrGLFormat::kALPHA8:
491 case GrGLFormat::kLUMINANCE8:
492 case GrGLFormat::kLUMINANCE8_ALPHA8:
493 case GrGLFormat::kBGRA8:
494 case GrGLFormat::kRGB565:
495 case GrGLFormat::kRGBA16F:
496 case GrGLFormat::kR16F:
497 case GrGLFormat::kLUMINANCE16F:
498 case GrGLFormat::kRGB8:
499 case GrGLFormat::kRG8:
500 case GrGLFormat::kRGB10_A2:
501 case GrGLFormat::kRGBA4:
502 case GrGLFormat::kSRGB8_ALPHA8:
503 case GrGLFormat::kR16:
504 case GrGLFormat::kRG16:
505 case GrGLFormat::kRGBA16:
506 case GrGLFormat::kRG16F:
507 case GrGLFormat::kUnknown:
508 return 0;
509 }
510 SkUNREACHABLE;
511 }
512
GrGLFormatIsPackedDepthStencil(GrGLFormat format)513 static constexpr bool GrGLFormatIsPackedDepthStencil(GrGLFormat format) {
514 switch (format) {
515 case GrGLFormat::kDEPTH24_STENCIL8:
516 return true;
517 case GrGLFormat::kCOMPRESSED_ETC1_RGB8:
518 case GrGLFormat::kCOMPRESSED_RGB8_ETC2:
519 case GrGLFormat::kCOMPRESSED_RGB8_BC1:
520 case GrGLFormat::kCOMPRESSED_RGBA8_BC1:
521 case GrGLFormat::kRGBA8:
522 case GrGLFormat::kR8:
523 case GrGLFormat::kALPHA8:
524 case GrGLFormat::kLUMINANCE8:
525 case GrGLFormat::kLUMINANCE8_ALPHA8:
526 case GrGLFormat::kBGRA8:
527 case GrGLFormat::kRGB565:
528 case GrGLFormat::kRGBA16F:
529 case GrGLFormat::kR16F:
530 case GrGLFormat::kLUMINANCE16F:
531 case GrGLFormat::kRGB8:
532 case GrGLFormat::kRG8:
533 case GrGLFormat::kRGB10_A2:
534 case GrGLFormat::kRGBA4:
535 case GrGLFormat::kSRGB8_ALPHA8:
536 case GrGLFormat::kR16:
537 case GrGLFormat::kRG16:
538 case GrGLFormat::kRGBA16:
539 case GrGLFormat::kRG16F:
540 case GrGLFormat::kSTENCIL_INDEX8:
541 case GrGLFormat::kSTENCIL_INDEX16:
542 case GrGLFormat::kUnknown:
543 return false;
544 }
545 SkUNREACHABLE;
546 }
547
GrGLFormatIsSRGB(GrGLFormat format)548 static constexpr bool GrGLFormatIsSRGB(GrGLFormat format) {
549 switch (format) {
550 case GrGLFormat::kSRGB8_ALPHA8:
551 return true;
552 case GrGLFormat::kCOMPRESSED_ETC1_RGB8:
553 case GrGLFormat::kCOMPRESSED_RGB8_ETC2:
554 case GrGLFormat::kCOMPRESSED_RGB8_BC1:
555 case GrGLFormat::kCOMPRESSED_RGBA8_BC1:
556 case GrGLFormat::kRGBA8:
557 case GrGLFormat::kR8:
558 case GrGLFormat::kALPHA8:
559 case GrGLFormat::kLUMINANCE8:
560 case GrGLFormat::kLUMINANCE8_ALPHA8:
561 case GrGLFormat::kBGRA8:
562 case GrGLFormat::kRGB565:
563 case GrGLFormat::kRGBA16F:
564 case GrGLFormat::kR16F:
565 case GrGLFormat::kLUMINANCE16F:
566 case GrGLFormat::kRGB8:
567 case GrGLFormat::kRG8:
568 case GrGLFormat::kRGB10_A2:
569 case GrGLFormat::kRGBA4:
570 case GrGLFormat::kR16:
571 case GrGLFormat::kRG16:
572 case GrGLFormat::kRGBA16:
573 case GrGLFormat::kRG16F:
574 case GrGLFormat::kSTENCIL_INDEX8:
575 case GrGLFormat::kSTENCIL_INDEX16:
576 case GrGLFormat::kDEPTH24_STENCIL8:
577 case GrGLFormat::kUnknown:
578 return false;
579 }
580 SkUNREACHABLE;
581 }
582
583 #if defined(SK_DEBUG) || GR_TEST_UTILS
GrGLFormatToStr(GrGLenum glFormat)584 static constexpr const char* GrGLFormatToStr(GrGLenum glFormat) {
585 switch (glFormat) {
586 case GR_GL_RGBA8: return "RGBA8";
587 case GR_GL_R8: return "R8";
588 case GR_GL_ALPHA8: return "ALPHA8";
589 case GR_GL_LUMINANCE8: return "LUMINANCE8";
590 case GR_GL_LUMINANCE8_ALPHA8: return "LUMINANCE8_ALPHA8";
591 case GR_GL_BGRA8: return "BGRA8";
592 case GR_GL_RGB565: return "RGB565";
593 case GR_GL_RGBA16F: return "RGBA16F";
594 case GR_GL_LUMINANCE16F: return "LUMINANCE16F";
595 case GR_GL_R16F: return "R16F";
596 case GR_GL_RGB8: return "RGB8";
597 case GR_GL_RG8: return "RG8";
598 case GR_GL_RGB10_A2: return "RGB10_A2";
599 case GR_GL_RGBA4: return "RGBA4";
600 case GR_GL_RGBA32F: return "RGBA32F";
601 case GR_GL_SRGB8_ALPHA8: return "SRGB8_ALPHA8";
602 case GR_GL_COMPRESSED_ETC1_RGB8: return "ETC1";
603 case GR_GL_COMPRESSED_RGB8_ETC2: return "ETC2";
604 case GR_GL_COMPRESSED_RGB_S3TC_DXT1_EXT: return "RGB8_BC1";
605 case GR_GL_COMPRESSED_RGBA_S3TC_DXT1_EXT: return "RGBA8_BC1";
606 case GR_GL_R16: return "R16";
607 case GR_GL_RG16: return "RG16";
608 case GR_GL_RGBA16: return "RGBA16";
609 case GR_GL_RG16F: return "RG16F";
610 case GR_GL_STENCIL_INDEX8: return "STENCIL_INDEX8";
611 case GR_GL_STENCIL_INDEX16: return "STENCIL_INDEX16";
612 case GR_GL_DEPTH24_STENCIL8: return "DEPTH24_STENCIL8";
613
614 default: return "Unknown";
615 }
616 }
617 #endif
618
619 GrGLenum GrToGLStencilFunc(GrStencilTest test);
620
621 /**
622 * Returns true if the format is compressed.
623 */
624 bool GrGLFormatIsCompressed(GrGLFormat);
625
626 #endif
627