Flutter Engine
The Flutter Engine
GrGLUtil.h
Go to the documentation of this file.
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
17
18class SkMatrix;
19
20////////////////////////////////////////////////////////////////////////////////
21
22typedef uint32_t GrGLVersion;
23typedef uint32_t GrGLSLVersion;
24typedef 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
41static constexpr uint32_t GrGLFormatChannels(GrGLFormat format) {
42 switch (format) {
43 case GrGLFormat::kUnknown: return 0;
68 case GrGLFormat::kSTENCIL_INDEX8: return 0;
69 case GrGLFormat::kSTENCIL_INDEX16: return 0;
70 case GrGLFormat::kDEPTH24_STENCIL8: return 0;
71 }
73}
74
76 switch (format) {
78
81 case GrGLFormat::kR8:
101 case GrGLFormat::kRG8:
109 case GrGLFormat::kR16:
119
120 // Compressed texture formats are not expected to have a description.
125
126 // This type only describes color channels.
130 }
132}
133
134/**
135 * The Vendor and Renderer enum values are lazily updated as required.
136 */
137enum class GrGLVendor {
138 kARM,
139 kGoogle,
141 kIntel,
142 kQualcomm,
143 kNVIDIA,
144 kATI,
145 kApple,
146
147 kOther
148};
149
150enum class GrGLRenderer {
151 kTegra_PreK1, // Legacy Tegra architecture (pre-K1).
152 kTegra, // Tegra with the same architecture as NVIDIA desktop GPUs (K1+).
153
157
163 kAdreno615, // Pixel3a
164 kAdreno620, // Pixel5
165 kAdreno630, // Pixel3
166 kAdreno640, // Pixel4
168
169 /** Intel GPU families, ordered by generation **/
170 // 6th gen
172
173 // 7th gen
175 kIntelValleyView, // aka BayTrail
177
178 // 8th gen
179 kIntelCherryView, // aka Braswell
181
182 // 9th gen
188
189 // 11th gen
191
192 // 12th gen
196
198
199 kMali4xx,
200 /** G-3x, G-5x, or G-7x */
201 kMaliG,
202 /** T-6xx, T-7xx, or T-8xx */
203 kMaliT,
204
205 kAMDRadeonHD7xxx, // AMD Radeon HD 7000 Series
206 kAMDRadeonR9M3xx, // AMD Radeon R9 M300 Series
207 kAMDRadeonR9M4xx, // AMD Radeon R9 M400 Series
208 kAMDRadeonPro5xxx, // AMD Radeon Pro 5000 Series
209 kAMDRadeonProVegaxx, // AMD Radeon Pro Vega
210
211 kApple,
212
213 kWebGL,
214
215 kOther
216};
217
218enum class GrGLDriver {
219 kMesa,
220 kNVIDIA,
221 kIntel,
222 kQualcomm,
226 kARM,
227 kApple,
229};
230
232 kUnknown,
233 kD3D9,
234 kD3D11,
235 kMetal,
236 kOpenGL
237};
238
239////////////////////////////////////////////////////////////////////////////////
240
241/**
242 * Some drivers want the var-int arg to be zero-initialized on input.
243 */
244#define GR_GL_INIT_ZERO 0
245#define GR_GL_GetIntegerv(gl, e, p) \
246 do { \
247 *(p) = GR_GL_INIT_ZERO; \
248 GR_GL_CALL(gl, GetIntegerv(e, p)); \
249 } while (0)
250
251#define GR_GL_GetFloatv(gl, e, p) \
252 do { \
253 *(p) = GR_GL_INIT_ZERO; \
254 GR_GL_CALL(gl, GetFloatv(e, p)); \
255 } while (0)
256
257#define GR_GL_GetFramebufferAttachmentParameteriv(gl, t, a, pname, p) \
258 do { \
259 *(p) = GR_GL_INIT_ZERO; \
260 GR_GL_CALL(gl, GetFramebufferAttachmentParameteriv(t, a, pname, p)); \
261 } while (0)
262
263#define GR_GL_GetInternalformativ(gl, t, f, n, s, p) \
264 do { \
265 *(p) = GR_GL_INIT_ZERO; \
266 GR_GL_CALL(gl, GetInternalformativ(t, f, n, s, p)); \
267 } while (0)
268
269#define GR_GL_GetNamedFramebufferAttachmentParameteriv(gl, fb, a, pname, p) \
270 do { \
271 *(p) = GR_GL_INIT_ZERO; \
272 GR_GL_CALL(gl, GetNamedFramebufferAttachmentParameteriv(fb, a, pname, p)); \
273 } while (0)
274
275#define GR_GL_GetRenderbufferParameteriv(gl, t, pname, p) \
276 do { \
277 *(p) = GR_GL_INIT_ZERO; \
278 GR_GL_CALL(gl, GetRenderbufferParameteriv(t, pname, p)); \
279 } while (0)
280
281#define GR_GL_GetTexLevelParameteriv(gl, t, l, pname, p) \
282 do { \
283 *(p) = GR_GL_INIT_ZERO; \
284 GR_GL_CALL(gl, GetTexLevelParameteriv(t, l, pname, p)); \
285 } while (0)
286
287#define GR_GL_GetShaderPrecisionFormat(gl, st, pt, range, precision) \
288 do { \
289 (range)[0] = GR_GL_INIT_ZERO; \
290 (range)[1] = GR_GL_INIT_ZERO; \
291 (*precision) = GR_GL_INIT_ZERO; \
292 GR_GL_CALL(gl, GetShaderPrecisionFormat(st, pt, range, precision)); \
293 } while (0)
294
295////////////////////////////////////////////////////////////////////////////////
296
297GrGLStandard GrGLGetStandardInUseFromString(const char* versionString);
300
309
315
318
319 // Are we running over the Chrome interprocess command buffer?
321
322 // Running over virgl guest driver.
324};
325
327
328/**
329 * Helpers for glGetError()
330 */
331
333 const char* location,
334 const char* call);
335
336////////////////////////////////////////////////////////////////////////////////
337
338/**
339 * GR_STRING makes a string of X where X is expanded before conversion to a string
340 * if X itself contains macros.
341 */
342#define GR_STRING(X) GR_STRING_IMPL(X)
343#define GR_STRING_IMPL(X) #X
344
345/**
346 * Creates a string of the form "<filename>(<linenumber>) : "
347 */
348#define GR_FILE_AND_LINE_STR __FILE__ "(" GR_STRING(__LINE__) ") : "
349
350/**
351 * Macros for using GrGLInterface to make GL calls
352 */
353
354// Conditionally checks glGetError based on compile-time and run-time flags.
355#if GR_GL_CHECK_ERROR
356 extern bool gCheckErrorGL;
357#define GR_GL_CHECK_ERROR_IMPL(IFACE, X) \
358 do { \
359 if (gCheckErrorGL) { \
360 IFACE->checkError(GR_FILE_AND_LINE_STR, #X); \
361 } \
362 } while (false)
363#else
364#define GR_GL_CHECK_ERROR_IMPL(IFACE, X) \
365 do { \
366 } while (false)
367#endif
368
369// internal macro to conditionally log the gl call using SkDebugf based on
370// compile-time and run-time flags.
371#if GR_GL_LOG_CALLS
372 extern bool gLogCallsGL;
373 #define GR_GL_LOG_CALLS_IMPL(X) \
374 if (gLogCallsGL) \
375 SkDebugf(GR_FILE_AND_LINE_STR "GL: " #X "\n")
376#else
377 #define GR_GL_LOG_CALLS_IMPL(X)
378#endif
379
380// makes a GL call on the interface and does any error checking and logging
381#define GR_GL_CALL(IFACE, X) \
382 do { \
383 GR_GL_CALL_NOERRCHECK(IFACE, X); \
384 GR_GL_CHECK_ERROR_IMPL(IFACE, X); \
385 } while (false)
386
387// Variant of above that always skips the error check. This is useful when
388// the caller wants to do its own glGetError() call and examine the error value.
389#define GR_GL_CALL_NOERRCHECK(IFACE, X) \
390 do { \
391 (IFACE)->fFunctions.f##X; \
392 GR_GL_LOG_CALLS_IMPL(X); \
393 } while (false)
394
395// same as GR_GL_CALL but stores the return value of the gl call in RET
396#define GR_GL_CALL_RET(IFACE, RET, X) \
397 do { \
398 GR_GL_CALL_RET_NOERRCHECK(IFACE, RET, X); \
399 GR_GL_CHECK_ERROR_IMPL(IFACE, X); \
400 } while (false)
401
402// same as GR_GL_CALL_RET but always skips the error check.
403#define GR_GL_CALL_RET_NOERRCHECK(IFACE, RET, X) \
404 do { \
405 (RET) = (IFACE)->fFunctions.f##X; \
406 GR_GL_LOG_CALLS_IMPL(X); \
407 } while (false)
408
409static constexpr GrGLFormat GrGLFormatFromGLEnum(GrGLenum glFormat) {
410 switch (glFormat) {
411 case GR_GL_RGBA8: return GrGLFormat::kRGBA8;
412 case GR_GL_R8: return GrGLFormat::kR8;
416 case GR_GL_BGRA8: return GrGLFormat::kBGRA8;
420 case GR_GL_R16F: return GrGLFormat::kR16F;
421 case GR_GL_RGB8: return GrGLFormat::kRGB8;
422 case GR_GL_RGBX8: return GrGLFormat::kRGBX8;
423 case GR_GL_RG8: return GrGLFormat::kRG8;
425 case GR_GL_RGBA4: return GrGLFormat::kRGBA4;
431 case GR_GL_R16: return GrGLFormat::kR16;
432 case GR_GL_RG16: return GrGLFormat::kRG16;
434 case GR_GL_RG16F: return GrGLFormat::kRG16F;
438
439
440 default: return GrGLFormat::kUnknown;
441 }
442}
443
444/** Returns either the sized internal format or compressed internal format of the GrGLFormat. */
446 switch (format) {
447 case GrGLFormat::kRGBA8: return GR_GL_RGBA8;
448 case GrGLFormat::kR8: return GR_GL_R8;
452 case GrGLFormat::kBGRA8: return GR_GL_BGRA8;
456 case GrGLFormat::kR16F: return GR_GL_R16F;
457 case GrGLFormat::kRGB8: return GR_GL_RGB8;
458 case GrGLFormat::kRGBX8: return GR_GL_RGBX8;
459 case GrGLFormat::kRG8: return GR_GL_RG8;
461 case GrGLFormat::kRGBA4: return GR_GL_RGBA4;
467 case GrGLFormat::kR16: return GR_GL_R16;
468 case GrGLFormat::kRG16: return GR_GL_RG16;
470 case GrGLFormat::kRG16F: return GR_GL_RG16F;
474 case GrGLFormat::kUnknown: return 0;
475 }
477}
478
479static constexpr size_t GrGLFormatBytesPerBlock(GrGLFormat format) {
480 switch (format) {
481 case GrGLFormat::kRGBA8: return 4;
482 case GrGLFormat::kR8: return 1;
483 case GrGLFormat::kALPHA8: return 1;
484 case GrGLFormat::kLUMINANCE8: return 1;
485 case GrGLFormat::kLUMINANCE8_ALPHA8: return 2;
486 case GrGLFormat::kBGRA8: return 4;
487 case GrGLFormat::kRGB565: return 2;
488 case GrGLFormat::kRGBA16F: return 8;
489 case GrGLFormat::kLUMINANCE16F: return 2;
490 case GrGLFormat::kR16F: return 2;
491 // We assume the GPU stores this format 4 byte aligned
492 case GrGLFormat::kRGB8: return 4;
493 case GrGLFormat::kRGBX8: return 4;
494 case GrGLFormat::kRG8: return 2;
495 case GrGLFormat::kRGB10_A2: return 4;
496 case GrGLFormat::kRGBA4: return 2;
497 case GrGLFormat::kSRGB8_ALPHA8: return 4;
502 case GrGLFormat::kR16: return 2;
503 case GrGLFormat::kRG16: return 4;
504 case GrGLFormat::kRGBA16: return 8;
505 case GrGLFormat::kRG16F: return 4;
506 case GrGLFormat::kSTENCIL_INDEX8: return 1;
507 case GrGLFormat::kSTENCIL_INDEX16: return 2;
508 case GrGLFormat::kDEPTH24_STENCIL8: return 4;
509 case GrGLFormat::kUnknown: return 0;
510 }
512}
513
515 switch (format) {
517 return 8;
519 return 16;
521 return 8;
527 case GrGLFormat::kR8:
538 case GrGLFormat::kRG8:
542 case GrGLFormat::kR16:
547 return 0;
548 }
550}
551
553 switch (format) {
555 return true;
561 case GrGLFormat::kR8:
572 case GrGLFormat::kRG8:
576 case GrGLFormat::kR16:
583 return false;
584 }
586}
587
588static constexpr bool GrGLFormatIsSRGB(GrGLFormat format) {
589 switch (format) {
591 return true;
597 case GrGLFormat::kR8:
608 case GrGLFormat::kRG8:
611 case GrGLFormat::kR16:
619 return false;
620 }
622}
623
624#if defined(SK_DEBUG) || defined(GR_TEST_UTILS)
625static constexpr const char* GrGLFormatToStr(GrGLenum glFormat) {
626 switch (glFormat) {
627 case GR_GL_RGBA8: return "RGBA8";
628 case GR_GL_R8: return "R8";
629 case GR_GL_ALPHA8: return "ALPHA8";
630 case GR_GL_LUMINANCE8: return "LUMINANCE8";
631 case GR_GL_LUMINANCE8_ALPHA8: return "LUMINANCE8_ALPHA8";
632 case GR_GL_BGRA8: return "BGRA8";
633 case GR_GL_RGB565: return "RGB565";
634 case GR_GL_RGBA16F: return "RGBA16F";
635 case GR_GL_LUMINANCE16F: return "LUMINANCE16F";
636 case GR_GL_R16F: return "R16F";
637 case GR_GL_RGB8: return "RGB8";
638 case GR_GL_RG8: return "RG8";
639 case GR_GL_RGB10_A2: return "RGB10_A2";
640 case GR_GL_RGBA4: return "RGBA4";
641 case GR_GL_RGBA32F: return "RGBA32F";
642 case GR_GL_SRGB8_ALPHA8: return "SRGB8_ALPHA8";
643 case GR_GL_COMPRESSED_ETC1_RGB8: return "ETC1";
644 case GR_GL_COMPRESSED_RGB8_ETC2: return "ETC2";
645 case GR_GL_COMPRESSED_RGB_S3TC_DXT1_EXT: return "RGB8_BC1";
646 case GR_GL_COMPRESSED_RGBA_S3TC_DXT1_EXT: return "RGBA8_BC1";
647 case GR_GL_R16: return "R16";
648 case GR_GL_RG16: return "RG16";
649 case GR_GL_RGBA16: return "RGBA16";
650 case GR_GL_RG16F: return "RG16F";
651 case GR_GL_STENCIL_INDEX8: return "STENCIL_INDEX8";
652 case GR_GL_STENCIL_INDEX16: return "STENCIL_INDEX16";
653 case GR_GL_DEPTH24_STENCIL8: return "DEPTH24_STENCIL8";
654
655 default: return "Unknown";
656 }
657}
658#endif
659
661
662/**
663 * Returns true if the format is compressed.
664 */
666
667#endif
#define GR_GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
Definition: GrGLDefines.h:261
#define GR_GL_RGB8
Definition: GrGLDefines.h:509
#define GR_GL_SRGB8_ALPHA8
Definition: GrGLDefines.h:526
#define GR_GL_RGB10_A2
Definition: GrGLDefines.h:525
#define GR_GL_R16
Definition: GrGLDefines.h:460
#define GR_GL_RGBA8
Definition: GrGLDefines.h:524
#define GR_GL_R8
Definition: GrGLDefines.h:459
#define GR_GL_R16F
Definition: GrGLDefines.h:494
#define GR_GL_STENCIL_INDEX16
Definition: GrGLDefines.h:450
#define GR_GL_LUMINANCE8
Definition: GrGLDefines.h:473
#define GR_GL_RG8
Definition: GrGLDefines.h:492
#define GR_GL_RGBA16
Definition: GrGLDefines.h:530
#define GR_GL_COMPRESSED_ETC1_RGB8
Definition: GrGLDefines.h:278
#define GR_GL_COMPRESSED_RGB8_ETC2
Definition: GrGLDefines.h:285
#define GR_GL_LUMINANCE8_ALPHA8
Definition: GrGLDefines.h:474
#define GR_GL_LUMINANCE16F
Definition: GrGLDefines.h:475
#define GR_GL_RGBA32F
Definition: GrGLDefines.h:528
#define GR_GL_DEPTH24_STENCIL8
Definition: GrGLDefines.h:456
#define GR_GL_RGBA4
Definition: GrGLDefines.h:522
#define GR_GL_RG16F
Definition: GrGLDefines.h:496
#define GR_GL_STENCIL_INDEX8
Definition: GrGLDefines.h:449
#define GR_GL_RGBA16F
Definition: GrGLDefines.h:527
#define GR_GL_BGRA8
Definition: GrGLDefines.h:541
#define GR_GL_COMPRESSED_RGB_S3TC_DXT1_EXT
Definition: GrGLDefines.h:260
#define GR_GL_RGBX8
Definition: GrGLDefines.h:511
#define GR_GL_RGB565
Definition: GrGLDefines.h:508
#define GR_GL_ALPHA8
Definition: GrGLDefines.h:478
#define GR_GL_RG16
Definition: GrGLDefines.h:493
GrGLFormat
Definition: GrGLTypes.h:59
@ kCOMPRESSED_ETC1_RGB8
@ kCOMPRESSED_RGB8_BC1
@ kCOMPRESSED_RGB8_ETC2
@ kCOMPRESSED_RGBA8_BC1
@ kLUMINANCE8_ALPHA8
GrGLStandard
Definition: GrGLTypes.h:19
@ kNone_GrGLStandard
Definition: GrGLTypes.h:20
unsigned int GrGLenum
Definition: GrGLTypes.h:102
#define GR_GL_DRIVER_UNKNOWN_VER
Definition: GrGLUtil.h:39
#define GR_GL_INVALID_VER
Definition: GrGLUtil.h:37
static constexpr size_t GrGLFormatBytesPerBlock(GrGLFormat format)
Definition: GrGLUtil.h:479
#define GR_GLSL_INVALID_VER
Definition: GrGLUtil.h:38
static constexpr bool GrGLFormatIsSRGB(GrGLFormat format)
Definition: GrGLUtil.h:588
GrGLDriver
Definition: GrGLUtil.h:218
GrGLVendor
Definition: GrGLUtil.h:137
uint64_t GrGLDriverVersion
Definition: GrGLUtil.h:24
GrGLSLVersion GrGLGetVersionFromString(const char *)
Definition: GrGLUtil.cpp:68
uint32_t GrGLSLVersion
Definition: GrGLUtil.h:23
GrGLANGLEBackend
Definition: GrGLUtil.h:231
GrGLDriverInfo GrGLGetDriverInfo(const GrGLInterface *)
Definition: GrGLUtil.cpp:723
GrGLRenderer
Definition: GrGLUtil.h:150
static constexpr int GrGLFormatStencilBits(GrGLFormat format)
Definition: GrGLUtil.h:514
GrGLenum GrToGLStencilFunc(GrStencilTest test)
Definition: GrGLUtil.cpp:788
static constexpr GrGLFormat GrGLFormatFromGLEnum(GrGLenum glFormat)
Definition: GrGLUtil.h:409
GrGLSLVersion GrGLGetVersion(const GrGLInterface *)
Definition: GrGLUtil.cpp:109
uint32_t GrGLVersion
Definition: GrGLUtil.h:22
GrGLStandard GrGLGetStandardInUseFromString(const char *versionString)
Definition: GrGLUtil.cpp:31
static constexpr GrGLenum GrGLFormatToEnum(GrGLFormat format)
Definition: GrGLUtil.h:445
static constexpr GrColorFormatDesc GrGLFormatDesc(GrGLFormat format)
Definition: GrGLUtil.h:75
void GrGLCheckErr(const GrGLInterface *gl, const char *location, const char *call)
static constexpr uint32_t GrGLFormatChannels(GrGLFormat format)
Definition: GrGLUtil.h:41
bool GrGLFormatIsCompressed(GrGLFormat)
Definition: GrGLUtil.cpp:812
static constexpr bool GrGLFormatIsPackedDepthStencil(GrGLFormat format)
Definition: GrGLUtil.h:552
GrStencilTest
#define SkUNREACHABLE
Definition: SkAssert.h:135
@ kRGB_SkColorChannelFlags
Definition: SkColor.h:247
@ kRGBA_SkColorChannelFlags
Definition: SkColor.h:248
@ kRed_SkColorChannelFlag
Definition: SkColor.h:239
@ kGrayAlpha_SkColorChannelFlags
Definition: SkColor.h:245
@ kRG_SkColorChannelFlags
Definition: SkColor.h:246
@ kAlpha_SkColorChannelFlag
Definition: SkColor.h:242
@ kGray_SkColorChannelFlag
Definition: SkColor.h:243
static constexpr GrColorFormatDesc MakeInvalid()
Definition: GrTypesPriv.h:756
static constexpr GrColorFormatDesc MakeRGBA(int rgba, GrColorTypeEncoding e)
Definition: GrTypesPriv.h:720
static constexpr GrColorFormatDesc MakeAlpha(int a, GrColorTypeEncoding e)
Definition: GrTypesPriv.h:736
static constexpr GrColorFormatDesc MakeGray(int grayBits, GrColorTypeEncoding e)
Definition: GrTypesPriv.h:748
static constexpr GrColorFormatDesc MakeRG(int rg, GrColorTypeEncoding e)
Definition: GrTypesPriv.h:744
static constexpr GrColorFormatDesc MakeRGB(int rgb, GrColorTypeEncoding e)
Definition: GrTypesPriv.h:728
static constexpr GrColorFormatDesc MakeGrayAlpha(int grayAlpha, GrColorTypeEncoding e)
Definition: GrTypesPriv.h:752
static constexpr GrColorFormatDesc MakeR(int r, GrColorTypeEncoding e)
Definition: GrTypesPriv.h:740
uint32_t uint32_t * format
def call(args)
Definition: dom.py:159
gl
Definition: malisc.py:41
GrGLANGLEBackend fANGLEBackend
Definition: GrGLUtil.h:310
bool fIsOverCommandBuffer
Definition: GrGLUtil.h:320
GrGLDriver fANGLEDriver
Definition: GrGLUtil.h:313
GrGLDriver fDriver
Definition: GrGLUtil.h:307
GrGLVendor fWebGLVendor
Definition: GrGLUtil.h:316
GrGLStandard fStandard
Definition: GrGLUtil.h:302
GrGLSLVersion fGLSLVersion
Definition: GrGLUtil.h:304
GrGLDriverVersion fDriverVersion
Definition: GrGLUtil.h:308
GrGLRenderer fANGLERenderer
Definition: GrGLUtil.h:312
bool fIsRunningOverVirgl
Definition: GrGLUtil.h:323
GrGLRenderer fWebGLRenderer
Definition: GrGLUtil.h:317
GrGLVersion fVersion
Definition: GrGLUtil.h:303
GrGLVendor fANGLEVendor
Definition: GrGLUtil.h:311
GrGLDriverVersion fANGLEDriverVersion
Definition: GrGLUtil.h:314
GrGLVendor fVendor
Definition: GrGLUtil.h:305
GrGLRenderer fRenderer
Definition: GrGLUtil.h:306