Debian Codegraph @

src/gpu/ganesh/gl/GrGLUtil.h

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