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