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