• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/gpu/ganesh/GrTypesPriv.h"
13 #include "src/core/SkImageInfoPriv.h"
14 #include "src/gpu/ganesh/GrDataUtils.h"
15 #include "src/gpu/ganesh/GrStencilSettings.h"
16 #include "src/gpu/ganesh/gl/GrGLDefines_impl.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     /** Intel GPU families, ordered by generation **/
168     // 6th gen
169     kIntelSandyBridge,
170 
171     // 7th gen
172     kIntelIvyBridge,
173     kIntelValleyView,  // aka BayTrail
174     kIntelHaswell,
175 
176     // 8th gen
177     kIntelCherryView,  // aka Braswell
178     kIntelBroadwell,
179 
180     // 9th gen
181     kIntelApolloLake,
182     kIntelSkyLake,
183     kIntelGeminiLake,
184     kIntelKabyLake,
185     kIntelCoffeeLake,
186 
187     // 11th gen
188     kIntelIceLake,
189 
190     // 12th gen
191     kIntelRocketLake,
192     kIntelTigerLake,
193     kIntelAlderLake,
194 
195     kGalliumLLVM,
196 
197     kMali4xx,
198     /** G-3x, G-5x, or G-7x */
199     kMaliG,
200     /** T-6xx, T-7xx, or T-8xx */
201     kMaliT,
202 
203     kAMDRadeonHD7xxx,     // AMD Radeon HD 7000 Series
204     kAMDRadeonR9M3xx,     // AMD Radeon R9 M300 Series
205     kAMDRadeonR9M4xx,     // AMD Radeon R9 M400 Series
206     kAMDRadeonPro5xxx,    // AMD Radeon Pro 5000 Series
207     kAMDRadeonProVegaxx,  // AMD Radeon Pro Vega
208 
209     kWebGL,
210 
211     kOther
212 };
213 
214 enum class GrGLDriver {
215     kMesa,
216     kNVIDIA,
217     kIntel,
218     kQualcomm,
219     kFreedreno,
220     kAndroidEmulator,
221     kImagination,
222     kARM,
223     kUnknown
224 };
225 
226 enum class GrGLANGLEBackend {
227     kUnknown,
228     kD3D9,
229     kD3D11,
230     kMetal,
231     kOpenGL
232 };
233 
234 ////////////////////////////////////////////////////////////////////////////////
235 
236 /**
237  *  Some drivers want the var-int arg to be zero-initialized on input.
238  */
239 #define GR_GL_INIT_ZERO     0
240 #define GR_GL_GetIntegerv(gl, e, p)                                            \
241     do {                                                                       \
242         *(p) = GR_GL_INIT_ZERO;                                                \
243         GR_GL_CALL(gl, GetIntegerv(e, p));                                     \
244     } while (0)
245 
246 #define GR_GL_GetFloatv(gl, e, p)                                              \
247     do {                                                                       \
248         *(p) = GR_GL_INIT_ZERO;                                                \
249         GR_GL_CALL(gl, GetFloatv(e, p));                                       \
250     } while (0)
251 
252 #define GR_GL_GetFramebufferAttachmentParameteriv(gl, t, a, pname, p)          \
253     do {                                                                       \
254         *(p) = GR_GL_INIT_ZERO;                                                \
255         GR_GL_CALL(gl, GetFramebufferAttachmentParameteriv(t, a, pname, p));   \
256     } while (0)
257 
258 #define GR_GL_GetInternalformativ(gl, t, f, n, s, p)                           \
259     do {                                                                       \
260         *(p) = GR_GL_INIT_ZERO;                                                \
261         GR_GL_CALL(gl, GetInternalformativ(t, f, n, s, p));                    \
262     } while (0)
263 
264 #define GR_GL_GetNamedFramebufferAttachmentParameteriv(gl, fb, a, pname, p)          \
265     do {                                                                             \
266         *(p) = GR_GL_INIT_ZERO;                                                      \
267         GR_GL_CALL(gl, GetNamedFramebufferAttachmentParameteriv(fb, a, pname, p));   \
268     } while (0)
269 
270 #define GR_GL_GetRenderbufferParameteriv(gl, t, pname, p)                      \
271     do {                                                                       \
272         *(p) = GR_GL_INIT_ZERO;                                                \
273         GR_GL_CALL(gl, GetRenderbufferParameteriv(t, pname, p));               \
274     } while (0)
275 
276 #define GR_GL_GetTexLevelParameteriv(gl, t, l, pname, p)                       \
277     do {                                                                       \
278         *(p) = GR_GL_INIT_ZERO;                                                \
279         GR_GL_CALL(gl, GetTexLevelParameteriv(t, l, pname, p));                \
280     } while (0)
281 
282 #define GR_GL_GetShaderPrecisionFormat(gl, st, pt, range, precision)           \
283     do {                                                                       \
284         (range)[0] = GR_GL_INIT_ZERO;                                          \
285         (range)[1] = GR_GL_INIT_ZERO;                                          \
286         (*precision) = GR_GL_INIT_ZERO;                                        \
287         GR_GL_CALL(gl, GetShaderPrecisionFormat(st, pt, range, precision));    \
288     } while (0)
289 
290 ////////////////////////////////////////////////////////////////////////////////
291 
292 GrGLStandard GrGLGetStandardInUseFromString(const char* versionString);
293 GrGLSLVersion GrGLGetVersion(const GrGLInterface*);
294 GrGLSLVersion GrGLGetVersionFromString(const char*);
295 
296 struct GrGLDriverInfo {
297     GrGLStandard      fStandard      = kNone_GrGLStandard;
298     GrGLVersion       fVersion       = GR_GL_INVALID_VER;
299     GrGLSLVersion     fGLSLVersion   = GR_GLSL_INVALID_VER;
300     GrGLVendor        fVendor        = GrGLVendor::kOther;
301     GrGLRenderer      fRenderer      = GrGLRenderer::kOther;
302     GrGLDriver        fDriver        = GrGLDriver::kUnknown;
303     GrGLDriverVersion fDriverVersion = GR_GL_DRIVER_UNKNOWN_VER;
304 
305     GrGLANGLEBackend  fANGLEBackend       = GrGLANGLEBackend::kUnknown;
306     GrGLVendor        fANGLEVendor        = GrGLVendor::kOther;
307     GrGLRenderer      fANGLERenderer      = GrGLRenderer::kOther;
308     GrGLDriver        fANGLEDriver        = GrGLDriver::kUnknown;
309     GrGLDriverVersion fANGLEDriverVersion = GR_GL_DRIVER_UNKNOWN_VER;
310 
311     GrGLVendor        fWebGLVendor        = GrGLVendor::kOther;
312     GrGLRenderer      fWebGLRenderer      = GrGLRenderer::kOther;
313 
314     // Are we running over the Chrome interprocess command buffer?
315     bool fIsOverCommandBuffer = false;
316 
317     // Running over virgl guest driver.
318     bool fIsRunningOverVirgl = false;
319 };
320 
321 GrGLDriverInfo GrGLGetDriverInfo(const GrGLInterface*);
322 
323 /**
324  * Helpers for glGetError()
325  */
326 
327 void GrGLCheckErr(const GrGLInterface* gl,
328                   const char* location,
329                   const char* call);
330 
331 ////////////////////////////////////////////////////////////////////////////////
332 
333 /**
334  *  GR_STRING makes a string of X where X is expanded before conversion to a string
335  *  if X itself contains macros.
336  */
337 #define GR_STRING(X) GR_STRING_IMPL(X)
338 #define GR_STRING_IMPL(X) #X
339 
340 /**
341  *  Creates a string of the form "<filename>(<linenumber>) : "
342  */
343 #define GR_FILE_AND_LINE_STR __FILE__ "(" GR_STRING(__LINE__) ") : "
344 
345 /**
346  * Macros for using GrGLInterface to make GL calls
347  */
348 
349 // Conditionally checks glGetError based on compile-time and run-time flags.
350 #if GR_GL_CHECK_ERROR
351     extern bool gCheckErrorGL;
352 #define GR_GL_CHECK_ERROR_IMPL(IFACE, X)                 \
353     do {                                                 \
354         if (gCheckErrorGL) {                             \
355             IFACE->checkError(GR_FILE_AND_LINE_STR, #X); \
356         }                                                \
357     } while (false)
358 #else
359 #define GR_GL_CHECK_ERROR_IMPL(IFACE, X) \
360     do {                                 \
361     } while (false)
362 #endif
363 
364 // internal macro to conditionally log the gl call using SkDebugf based on
365 // compile-time and run-time flags.
366 #if GR_GL_LOG_CALLS
367     extern bool gLogCallsGL;
368     #define GR_GL_LOG_CALLS_IMPL(X)                             \
369         if (gLogCallsGL)                                        \
370             SkDebugf(GR_FILE_AND_LINE_STR "GL: " #X "\n")
371 #else
372     #define GR_GL_LOG_CALLS_IMPL(X)
373 #endif
374 
375 // makes a GL call on the interface and does any error checking and logging
376 #define GR_GL_CALL(IFACE, X)                                    \
377     do {                                                        \
378         GR_GL_CALL_NOERRCHECK(IFACE, X);                        \
379         GR_GL_CHECK_ERROR_IMPL(IFACE, X);                       \
380     } while (false)
381 
382 // Variant of above that always skips the error check. This is useful when
383 // the caller wants to do its own glGetError() call and examine the error value.
384 #define GR_GL_CALL_NOERRCHECK(IFACE, X)                         \
385     do {                                                        \
386         (IFACE)->fFunctions.f##X;                               \
387         GR_GL_LOG_CALLS_IMPL(X);                                \
388     } while (false)
389 
390 // same as GR_GL_CALL but stores the return value of the gl call in RET
391 #define GR_GL_CALL_RET(IFACE, RET, X)                           \
392     do {                                                        \
393         GR_GL_CALL_RET_NOERRCHECK(IFACE, RET, X);               \
394         GR_GL_CHECK_ERROR_IMPL(IFACE, X);                       \
395     } while (false)
396 
397 // same as GR_GL_CALL_RET but always skips the error check.
398 #define GR_GL_CALL_RET_NOERRCHECK(IFACE, RET, X)                \
399     do {                                                        \
400         (RET) = (IFACE)->fFunctions.f##X;                       \
401         GR_GL_LOG_CALLS_IMPL(X);                                \
402     } while (false)
403 
GrGLFormatFromGLEnum(GrGLenum glFormat)404 static constexpr GrGLFormat GrGLFormatFromGLEnum(GrGLenum glFormat) {
405     switch (glFormat) {
406         case GR_GL_RGBA8:                return GrGLFormat::kRGBA8;
407         case GR_GL_R8:                   return GrGLFormat::kR8;
408         case GR_GL_ALPHA8:               return GrGLFormat::kALPHA8;
409         case GR_GL_LUMINANCE8:           return GrGLFormat::kLUMINANCE8;
410         case GR_GL_LUMINANCE8_ALPHA8:    return GrGLFormat::kLUMINANCE8_ALPHA8;
411         case GR_GL_BGRA8:                return GrGLFormat::kBGRA8;
412         case GR_GL_RGB565:               return GrGLFormat::kRGB565;
413         case GR_GL_RGBA16F:              return GrGLFormat::kRGBA16F;
414         case GR_GL_LUMINANCE16F:         return GrGLFormat::kLUMINANCE16F;
415         case GR_GL_R16F:                 return GrGLFormat::kR16F;
416         case GR_GL_RGB8:                 return GrGLFormat::kRGB8;
417         case GR_GL_RGBX8:                return GrGLFormat::kRGBX8;
418         case GR_GL_RG8:                  return GrGLFormat::kRG8;
419         case GR_GL_RGB10_A2:             return GrGLFormat::kRGB10_A2;
420         case GR_GL_RGBA4:                return GrGLFormat::kRGBA4;
421         case GR_GL_SRGB8_ALPHA8:         return GrGLFormat::kSRGB8_ALPHA8;
422         case GR_GL_COMPRESSED_ETC1_RGB8: return GrGLFormat::kCOMPRESSED_ETC1_RGB8;
423         case GR_GL_COMPRESSED_RGB8_ETC2: return GrGLFormat::kCOMPRESSED_RGB8_ETC2;
424         case GR_GL_COMPRESSED_RGB_S3TC_DXT1_EXT: return GrGLFormat::kCOMPRESSED_RGB8_BC1;
425         case GR_GL_COMPRESSED_RGBA_S3TC_DXT1_EXT: return GrGLFormat::kCOMPRESSED_RGBA8_BC1;
426         case GR_GL_R16:                  return GrGLFormat::kR16;
427         case GR_GL_RG16:                 return GrGLFormat::kRG16;
428         case GR_GL_RGBA16:               return GrGLFormat::kRGBA16;
429         case GR_GL_RG16F:                return GrGLFormat::kRG16F;
430         case GR_GL_STENCIL_INDEX8:       return GrGLFormat::kSTENCIL_INDEX8;
431         case GR_GL_STENCIL_INDEX16:      return GrGLFormat::kSTENCIL_INDEX16;
432         case GR_GL_DEPTH24_STENCIL8:     return GrGLFormat::kDEPTH24_STENCIL8;
433 
434 
435         default:                         return GrGLFormat::kUnknown;
436     }
437 }
438 
439 /** Returns either the sized internal format or compressed internal format of the GrGLFormat. */
GrGLFormatToEnum(GrGLFormat format)440 static constexpr GrGLenum GrGLFormatToEnum(GrGLFormat format) {
441     switch (format) {
442         case GrGLFormat::kRGBA8:                return GR_GL_RGBA8;
443         case GrGLFormat::kR8:                   return GR_GL_R8;
444         case GrGLFormat::kALPHA8:               return GR_GL_ALPHA8;
445         case GrGLFormat::kLUMINANCE8:           return GR_GL_LUMINANCE8;
446         case GrGLFormat::kLUMINANCE8_ALPHA8:    return GR_GL_LUMINANCE8_ALPHA8;
447         case GrGLFormat::kBGRA8:                return GR_GL_BGRA8;
448         case GrGLFormat::kRGB565:               return GR_GL_RGB565;
449         case GrGLFormat::kRGBA16F:              return GR_GL_RGBA16F;
450         case GrGLFormat::kLUMINANCE16F:         return GR_GL_LUMINANCE16F;
451         case GrGLFormat::kR16F:                 return GR_GL_R16F;
452         case GrGLFormat::kRGB8:                 return GR_GL_RGB8;
453         case GrGLFormat::kRGBX8:                return GR_GL_RGBX8;
454         case GrGLFormat::kRG8:                  return GR_GL_RG8;
455         case GrGLFormat::kRGB10_A2:             return GR_GL_RGB10_A2;
456         case GrGLFormat::kRGBA4:                return GR_GL_RGBA4;
457         case GrGLFormat::kSRGB8_ALPHA8:         return GR_GL_SRGB8_ALPHA8;
458         case GrGLFormat::kCOMPRESSED_ETC1_RGB8: return GR_GL_COMPRESSED_ETC1_RGB8;
459         case GrGLFormat::kCOMPRESSED_RGB8_ETC2: return GR_GL_COMPRESSED_RGB8_ETC2;
460         case GrGLFormat::kCOMPRESSED_RGB8_BC1:  return GR_GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
461         case GrGLFormat::kCOMPRESSED_RGBA8_BC1: return GR_GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
462         case GrGLFormat::kR16:                  return GR_GL_R16;
463         case GrGLFormat::kRG16:                 return GR_GL_RG16;
464         case GrGLFormat::kRGBA16:               return GR_GL_RGBA16;
465         case GrGLFormat::kRG16F:                return GR_GL_RG16F;
466         case GrGLFormat::kSTENCIL_INDEX8:       return GR_GL_STENCIL_INDEX8;
467         case GrGLFormat::kSTENCIL_INDEX16:      return GR_GL_STENCIL_INDEX16;
468         case GrGLFormat::kDEPTH24_STENCIL8:     return GR_GL_DEPTH24_STENCIL8;
469         case GrGLFormat::kUnknown:              return 0;
470     }
471     SkUNREACHABLE;
472 }
473 
GrGLFormatBytesPerBlock(GrGLFormat format)474 static constexpr size_t GrGLFormatBytesPerBlock(GrGLFormat format) {
475     switch (format) {
476         case GrGLFormat::kRGBA8:                return 4;
477         case GrGLFormat::kR8:                   return 1;
478         case GrGLFormat::kALPHA8:               return 1;
479         case GrGLFormat::kLUMINANCE8:           return 1;
480         case GrGLFormat::kLUMINANCE8_ALPHA8:    return 2;
481         case GrGLFormat::kBGRA8:                return 4;
482         case GrGLFormat::kRGB565:               return 2;
483         case GrGLFormat::kRGBA16F:              return 8;
484         case GrGLFormat::kLUMINANCE16F:         return 2;
485         case GrGLFormat::kR16F:                 return 2;
486         // We assume the GPU stores this format 4 byte aligned
487         case GrGLFormat::kRGB8:                 return 4;
488         case GrGLFormat::kRGBX8:                return 4;
489         case GrGLFormat::kRG8:                  return 2;
490         case GrGLFormat::kRGB10_A2:             return 4;
491         case GrGLFormat::kRGBA4:                return 2;
492         case GrGLFormat::kSRGB8_ALPHA8:         return 4;
493         case GrGLFormat::kCOMPRESSED_ETC1_RGB8: return 8;
494         case GrGLFormat::kCOMPRESSED_RGB8_ETC2: return 8;
495         case GrGLFormat::kCOMPRESSED_RGB8_BC1:  return 8;
496         case GrGLFormat::kCOMPRESSED_RGBA8_BC1: return 8;
497         case GrGLFormat::kR16:                  return 2;
498         case GrGLFormat::kRG16:                 return 4;
499         case GrGLFormat::kRGBA16:               return 8;
500         case GrGLFormat::kRG16F:                return 4;
501         case GrGLFormat::kSTENCIL_INDEX8:       return 1;
502         case GrGLFormat::kSTENCIL_INDEX16:      return 2;
503         case GrGLFormat::kDEPTH24_STENCIL8:     return 4;
504         case GrGLFormat::kUnknown:              return 0;
505     }
506     SkUNREACHABLE;
507 }
508 
GrGLFormatStencilBits(GrGLFormat format)509 static constexpr int GrGLFormatStencilBits(GrGLFormat format) {
510     switch (format) {
511         case GrGLFormat::kSTENCIL_INDEX8:
512             return 8;
513         case GrGLFormat::kSTENCIL_INDEX16:
514             return 16;
515         case GrGLFormat::kDEPTH24_STENCIL8:
516             return 8;
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::kRGBX8:
533         case GrGLFormat::kRG8:
534         case GrGLFormat::kRGB10_A2:
535         case GrGLFormat::kRGBA4:
536         case GrGLFormat::kSRGB8_ALPHA8:
537         case GrGLFormat::kR16:
538         case GrGLFormat::kRG16:
539         case GrGLFormat::kRGBA16:
540         case GrGLFormat::kRG16F:
541         case GrGLFormat::kUnknown:
542             return 0;
543     }
544     SkUNREACHABLE;
545 }
546 
GrGLFormatIsPackedDepthStencil(GrGLFormat format)547 static constexpr bool GrGLFormatIsPackedDepthStencil(GrGLFormat format) {
548     switch (format) {
549         case GrGLFormat::kDEPTH24_STENCIL8:
550             return true;
551         case GrGLFormat::kCOMPRESSED_ETC1_RGB8:
552         case GrGLFormat::kCOMPRESSED_RGB8_ETC2:
553         case GrGLFormat::kCOMPRESSED_RGB8_BC1:
554         case GrGLFormat::kCOMPRESSED_RGBA8_BC1:
555         case GrGLFormat::kRGBA8:
556         case GrGLFormat::kR8:
557         case GrGLFormat::kALPHA8:
558         case GrGLFormat::kLUMINANCE8:
559         case GrGLFormat::kLUMINANCE8_ALPHA8:
560         case GrGLFormat::kBGRA8:
561         case GrGLFormat::kRGB565:
562         case GrGLFormat::kRGBA16F:
563         case GrGLFormat::kR16F:
564         case GrGLFormat::kLUMINANCE16F:
565         case GrGLFormat::kRGB8:
566         case GrGLFormat::kRGBX8:
567         case GrGLFormat::kRG8:
568         case GrGLFormat::kRGB10_A2:
569         case GrGLFormat::kRGBA4:
570         case GrGLFormat::kSRGB8_ALPHA8:
571         case GrGLFormat::kR16:
572         case GrGLFormat::kRG16:
573         case GrGLFormat::kRGBA16:
574         case GrGLFormat::kRG16F:
575         case GrGLFormat::kSTENCIL_INDEX8:
576         case GrGLFormat::kSTENCIL_INDEX16:
577         case GrGLFormat::kUnknown:
578             return false;
579     }
580     SkUNREACHABLE;
581 }
582 
GrGLFormatIsSRGB(GrGLFormat format)583 static constexpr bool GrGLFormatIsSRGB(GrGLFormat format) {
584     switch (format) {
585     case GrGLFormat::kSRGB8_ALPHA8:
586         return true;
587     case GrGLFormat::kCOMPRESSED_ETC1_RGB8:
588     case GrGLFormat::kCOMPRESSED_RGB8_ETC2:
589     case GrGLFormat::kCOMPRESSED_RGB8_BC1:
590     case GrGLFormat::kCOMPRESSED_RGBA8_BC1:
591     case GrGLFormat::kRGBA8:
592     case GrGLFormat::kR8:
593     case GrGLFormat::kALPHA8:
594     case GrGLFormat::kLUMINANCE8:
595     case GrGLFormat::kLUMINANCE8_ALPHA8:
596     case GrGLFormat::kBGRA8:
597     case GrGLFormat::kRGB565:
598     case GrGLFormat::kRGBA16F:
599     case GrGLFormat::kR16F:
600     case GrGLFormat::kLUMINANCE16F:
601     case GrGLFormat::kRGB8:
602     case GrGLFormat::kRGBX8:
603     case GrGLFormat::kRG8:
604     case GrGLFormat::kRGB10_A2:
605     case GrGLFormat::kRGBA4:
606     case GrGLFormat::kR16:
607     case GrGLFormat::kRG16:
608     case GrGLFormat::kRGBA16:
609     case GrGLFormat::kRG16F:
610     case GrGLFormat::kSTENCIL_INDEX8:
611     case GrGLFormat::kSTENCIL_INDEX16:
612     case GrGLFormat::kDEPTH24_STENCIL8:
613     case GrGLFormat::kUnknown:
614         return false;
615     }
616     SkUNREACHABLE;
617 }
618 
619 #if defined(SK_DEBUG) || GR_TEST_UTILS
GrGLFormatToStr(GrGLenum glFormat)620 static constexpr const char* GrGLFormatToStr(GrGLenum glFormat) {
621     switch (glFormat) {
622         case GR_GL_RGBA8:                return "RGBA8";
623         case GR_GL_R8:                   return "R8";
624         case GR_GL_ALPHA8:               return "ALPHA8";
625         case GR_GL_LUMINANCE8:           return "LUMINANCE8";
626         case GR_GL_LUMINANCE8_ALPHA8:    return "LUMINANCE8_ALPHA8";
627         case GR_GL_BGRA8:                return "BGRA8";
628         case GR_GL_RGB565:               return "RGB565";
629         case GR_GL_RGBA16F:              return "RGBA16F";
630         case GR_GL_LUMINANCE16F:         return "LUMINANCE16F";
631         case GR_GL_R16F:                 return "R16F";
632         case GR_GL_RGB8:                 return "RGB8";
633         case GR_GL_RG8:                  return "RG8";
634         case GR_GL_RGB10_A2:             return "RGB10_A2";
635         case GR_GL_RGBA4:                return "RGBA4";
636         case GR_GL_RGBA32F:              return "RGBA32F";
637         case GR_GL_SRGB8_ALPHA8:         return "SRGB8_ALPHA8";
638         case GR_GL_COMPRESSED_ETC1_RGB8: return "ETC1";
639         case GR_GL_COMPRESSED_RGB8_ETC2: return "ETC2";
640         case GR_GL_COMPRESSED_RGB_S3TC_DXT1_EXT: return "RGB8_BC1";
641         case GR_GL_COMPRESSED_RGBA_S3TC_DXT1_EXT: return "RGBA8_BC1";
642         case GR_GL_R16:                  return "R16";
643         case GR_GL_RG16:                 return "RG16";
644         case GR_GL_RGBA16:               return "RGBA16";
645         case GR_GL_RG16F:                return "RG16F";
646         case GR_GL_STENCIL_INDEX8:       return "STENCIL_INDEX8";
647         case GR_GL_STENCIL_INDEX16:      return "STENCIL_INDEX16";
648         case GR_GL_DEPTH24_STENCIL8:     return "DEPTH24_STENCIL8";
649 
650         default:                         return "Unknown";
651     }
652 }
653 #endif
654 
655 GrGLenum GrToGLStencilFunc(GrStencilTest test);
656 
657 /**
658  * Returns true if the format is compressed.
659  */
660 bool GrGLFormatIsCompressed(GrGLFormat);
661 
662 #endif
663