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