• 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 "src/gpu/GrDataUtils.h"
14 #include "src/gpu/GrStencilSettings.h"
15 #include "src/gpu/gl/GrGLDefines.h"
16 
17 class SkMatrix;
18 
19 ////////////////////////////////////////////////////////////////////////////////
20 
21 typedef uint32_t GrGLVersion;
22 typedef uint32_t GrGLSLVersion;
23 typedef uint64_t GrGLDriverVersion;
24 
25 #define GR_GL_VER(major, minor) ((static_cast<uint32_t>(major) << 16) | \
26                                  static_cast<uint32_t>(minor))
27 #define GR_GLSL_VER(major, minor) ((static_cast<uint32_t>(major) << 16) | \
28                                     static_cast<uint32_t>(minor))
29 #define GR_GL_DRIVER_VER(major, minor, point) ((static_cast<uint64_t>(major) << 32) | \
30                                                (static_cast<uint64_t>(minor) << 16) | \
31                                                 static_cast<uint64_t>(point))
32 
33 #define GR_GL_MAJOR_VER(version) (static_cast<uint32_t>(version) >> 16)
34 #define GR_GL_MINOR_VER(version) (static_cast<uint32_t>(version) & 0xFFFF)
35 
36 #define GR_GL_INVALID_VER GR_GL_VER(0, 0)
37 #define GR_GLSL_INVALID_VER GR_GLSL_VER(0, 0)
38 #define GR_GL_DRIVER_UNKNOWN_VER GR_GL_DRIVER_VER(0, 0, 0)
39 
40 /**
41  * The Vendor and Renderer enum values are lazily updated as required.
42  */
43 enum GrGLVendor {
44     kARM_GrGLVendor,
45     kGoogle_GrGLVendor,
46     kImagination_GrGLVendor,
47     kIntel_GrGLVendor,
48     kQualcomm_GrGLVendor,
49     kNVIDIA_GrGLVendor,
50     kATI_GrGLVendor,
51 
52     kOther_GrGLVendor
53 };
54 
55 enum GrGLRenderer {
56     kTegra_PreK1_GrGLRenderer,  // Legacy Tegra architecture (pre-K1).
57     kTegra_GrGLRenderer,  // Tegra with the same architecture as NVIDIA desktop GPUs (K1+).
58     kPowerVR54x_GrGLRenderer,
59     kPowerVRRogue_GrGLRenderer,
60     kAdreno3xx_GrGLRenderer,
61     kAdreno430_GrGLRenderer,
62     kAdreno4xx_other_GrGLRenderer,
63     kAdreno5xx_GrGLRenderer,
64     kAdreno615_GrGLRenderer,  // Pixel3a
65     kAdreno630_GrGLRenderer,  // Pixel3
66     kAdreno640_GrGLRenderer,  // Pixel4
67     kGoogleSwiftShader_GrGLRenderer,
68 
69     /** Intel GPU families, ordered by generation **/
70     // 6th gen
71     kIntelSandyBridge_GrGLRenderer,
72 
73     // 7th gen
74     kIntelIvyBridge_GrGLRenderer,
75     kIntelValleyView_GrGLRenderer, // aka BayTrail
76     kIntelHaswell_GrGLRenderer,
77 
78     // 8th gen
79     kIntelCherryView_GrGLRenderer, // aka Braswell
80     kIntelBroadwell_GrGLRenderer,
81 
82     // 9th gen
83     kIntelApolloLake_GrGLRenderer,
84     kIntelSkyLake_GrGLRenderer,
85     kIntelGeminiLake_GrGLRenderer,
86     kIntelKabyLake_GrGLRenderer,
87     kIntelCoffeeLake_GrGLRenderer,
88 
89     // 11th gen
90     kIntelIceLake_GrGLRenderer,
91 
92     kGalliumLLVM_GrGLRenderer,
93     kMali4xx_GrGLRenderer,
94     /** T-6xx, T-7xx, or T-8xx */
95     kMaliT_GrGLRenderer,
96     kANGLE_GrGLRenderer,
97 
98     kAMDRadeonHD7xxx_GrGLRenderer,    // AMD Radeon HD 7000 Series
99     kAMDRadeonR9M3xx_GrGLRenderer,    // AMD Radeon R9 M300 Series
100     kAMDRadeonR9M4xx_GrGLRenderer,    // AMD Radeon R9 M400 Series
101     kAMDRadeonProVegaxx_GrGLRenderer, // AMD Radeon Pro Vega
102 
103     kOther_GrGLRenderer
104 };
105 
106 enum GrGLDriver {
107     kMesa_GrGLDriver,
108     kChromium_GrGLDriver,
109     kNVIDIA_GrGLDriver,
110     kIntel_GrGLDriver,
111     kANGLE_GrGLDriver,
112     kSwiftShader_GrGLDriver,
113     kQualcomm_GrGLDriver,
114     kAndroidEmulator_GrGLDriver,
115     kUnknown_GrGLDriver
116 };
117 
118 enum class GrGLANGLEBackend {
119     kUnknown,
120     kD3D9,
121     kD3D11,
122     kOpenGL
123 };
124 
125 enum class GrGLANGLEVendor {
126     kUnknown,
127     kIntel
128 };
129 
130 enum class GrGLANGLERenderer {
131     kUnknown,
132     kSandyBridge,
133     kIvyBridge,
134     kSkylake
135 };
136 
137 ////////////////////////////////////////////////////////////////////////////////
138 
139 /**
140  *  Some drivers want the var-int arg to be zero-initialized on input.
141  */
142 #define GR_GL_INIT_ZERO     0
143 #define GR_GL_GetIntegerv(gl, e, p)                                            \
144     do {                                                                       \
145         *(p) = GR_GL_INIT_ZERO;                                                \
146         GR_GL_CALL(gl, GetIntegerv(e, p));                                     \
147     } while (0)
148 
149 #define GR_GL_GetFramebufferAttachmentParameteriv(gl, t, a, pname, p)          \
150     do {                                                                       \
151         *(p) = GR_GL_INIT_ZERO;                                                \
152         GR_GL_CALL(gl, GetFramebufferAttachmentParameteriv(t, a, pname, p));   \
153     } while (0)
154 
155 #define GR_GL_GetInternalformativ(gl, t, f, n, s, p)                           \
156     do {                                                                       \
157         *(p) = GR_GL_INIT_ZERO;                                                \
158         GR_GL_CALL(gl, GetInternalformativ(t, f, n, s, p));                    \
159     } while (0)
160 
161 #define GR_GL_GetNamedFramebufferAttachmentParameteriv(gl, fb, a, pname, p)          \
162     do {                                                                             \
163         *(p) = GR_GL_INIT_ZERO;                                                      \
164         GR_GL_CALL(gl, GetNamedFramebufferAttachmentParameteriv(fb, a, pname, p));   \
165     } while (0)
166 
167 #define GR_GL_GetRenderbufferParameteriv(gl, t, pname, p)                      \
168     do {                                                                       \
169         *(p) = GR_GL_INIT_ZERO;                                                \
170         GR_GL_CALL(gl, GetRenderbufferParameteriv(t, pname, p));               \
171     } while (0)
172 
173 #define GR_GL_GetTexLevelParameteriv(gl, t, l, pname, p)                       \
174     do {                                                                       \
175         *(p) = GR_GL_INIT_ZERO;                                                \
176         GR_GL_CALL(gl, GetTexLevelParameteriv(t, l, pname, p));                \
177     } while (0)
178 
179 #define GR_GL_GetShaderPrecisionFormat(gl, st, pt, range, precision)           \
180     do {                                                                       \
181         (range)[0] = GR_GL_INIT_ZERO;                                          \
182         (range)[1] = GR_GL_INIT_ZERO;                                          \
183         (*precision) = GR_GL_INIT_ZERO;                                        \
184         GR_GL_CALL(gl, GetShaderPrecisionFormat(st, pt, range, precision));    \
185     } while (0)
186 
187 ////////////////////////////////////////////////////////////////////////////////
188 
189 /**
190  * Helpers for glGetString()
191  */
192 
193 // these variants assume caller already has a string from glGetString()
194 GrGLVersion GrGLGetVersionFromString(const char* versionString);
195 GrGLStandard GrGLGetStandardInUseFromString(const char* versionString);
196 GrGLSLVersion GrGLGetGLSLVersionFromString(const char* versionString);
197 GrGLVendor GrGLGetVendorFromString(const char* vendorString);
198 GrGLRenderer GrGLGetRendererFromStrings(const char* rendererString, const GrGLExtensions&);
199 void GrGLGetANGLEInfoFromString(const char* rendererString, GrGLANGLEBackend*,
200                                 GrGLANGLEVendor*, GrGLANGLERenderer*);
201 
202 void GrGLGetDriverInfo(GrGLStandard standard,
203                        GrGLVendor vendor,
204                        const char* rendererString,
205                        const char* versionString,
206                        GrGLDriver* outDriver,
207                        GrGLDriverVersion* outVersion);
208 
209 // these variants call glGetString()
210 GrGLVersion GrGLGetVersion(const GrGLInterface*);
211 GrGLSLVersion GrGLGetGLSLVersion(const GrGLInterface*);
212 GrGLVendor GrGLGetVendor(const GrGLInterface*);
213 GrGLRenderer GrGLGetRenderer(const GrGLInterface*);
214 
215 /**
216  * Helpers for glGetError()
217  */
218 
219 void GrGLCheckErr(const GrGLInterface* gl,
220                   const char* location,
221                   const char* call);
222 
223 void GrGLClearErr(const GrGLInterface* gl);
224 
225 ////////////////////////////////////////////////////////////////////////////////
226 
227 /**
228  * Macros for using GrGLInterface to make GL calls
229  */
230 
231 // internal macro to conditionally call glGetError based on compile-time and
232 // run-time flags.
233 #if GR_GL_CHECK_ERROR
234     extern bool gCheckErrorGL;
235     #define GR_GL_CHECK_ERROR_IMPL(IFACE, X)                    \
236         if (gCheckErrorGL)                                      \
237             GrGLCheckErr(IFACE, GR_FILE_AND_LINE_STR, #X)
238 #else
239     #define GR_GL_CHECK_ERROR_IMPL(IFACE, X)
240 #endif
241 
242 // internal macro to conditionally log the gl call using SkDebugf based on
243 // compile-time and run-time flags.
244 #if GR_GL_LOG_CALLS
245     extern bool gLogCallsGL;
246     #define GR_GL_LOG_CALLS_IMPL(X)                             \
247         if (gLogCallsGL)                                        \
248             SkDebugf(GR_FILE_AND_LINE_STR "GL: " #X "\n")
249 #else
250     #define GR_GL_LOG_CALLS_IMPL(X)
251 #endif
252 
253 // makes a GL call on the interface and does any error checking and logging
254 #define GR_GL_CALL(IFACE, X)                                    \
255     do {                                                        \
256         GR_GL_CALL_NOERRCHECK(IFACE, X);                        \
257         GR_GL_CHECK_ERROR_IMPL(IFACE, X);                       \
258     } while (false)
259 
260 // Variant of above that always skips the error check. This is useful when
261 // the caller wants to do its own glGetError() call and examine the error value.
262 #define GR_GL_CALL_NOERRCHECK(IFACE, X)                         \
263     do {                                                        \
264         (IFACE)->fFunctions.f##X;                               \
265         GR_GL_LOG_CALLS_IMPL(X);                                \
266     } while (false)
267 
268 // same as GR_GL_CALL but stores the return value of the gl call in RET
269 #define GR_GL_CALL_RET(IFACE, RET, X)                           \
270     do {                                                        \
271         GR_GL_CALL_RET_NOERRCHECK(IFACE, RET, X);               \
272         GR_GL_CHECK_ERROR_IMPL(IFACE, X);                       \
273     } while (false)
274 
275 // same as GR_GL_CALL_RET but always skips the error check.
276 #define GR_GL_CALL_RET_NOERRCHECK(IFACE, RET, X)                \
277     do {                                                        \
278         (RET) = (IFACE)->fFunctions.f##X;                       \
279         GR_GL_LOG_CALLS_IMPL(X);                                \
280     } while (false)
281 
282 // call glGetError without doing a redundant error check or logging.
283 #define GR_GL_GET_ERROR(IFACE) (IFACE)->fFunctions.fGetError()
284 
GrGLFormatFromGLEnum(GrGLenum glFormat)285 static constexpr GrGLFormat GrGLFormatFromGLEnum(GrGLenum glFormat) {
286     switch (glFormat) {
287         case GR_GL_RGBA8:                return GrGLFormat::kRGBA8;
288         case GR_GL_R8:                   return GrGLFormat::kR8;
289         case GR_GL_ALPHA8:               return GrGLFormat::kALPHA8;
290         case GR_GL_LUMINANCE8:           return GrGLFormat::kLUMINANCE8;
291         case GR_GL_BGRA8:                return GrGLFormat::kBGRA8;
292         case GR_GL_RGB565:               return GrGLFormat::kRGB565;
293         case GR_GL_RGBA16F:              return GrGLFormat::kRGBA16F;
294         case GR_GL_LUMINANCE16F:         return GrGLFormat::kLUMINANCE16F;
295         case GR_GL_R16F:                 return GrGLFormat::kR16F;
296         case GR_GL_RGB8:                 return GrGLFormat::kRGB8;
297         case GR_GL_RG8:                  return GrGLFormat::kRG8;
298         case GR_GL_RGB10_A2:             return GrGLFormat::kRGB10_A2;
299         case GR_GL_RGBA4:                return GrGLFormat::kRGBA4;
300         case GR_GL_SRGB8_ALPHA8:         return GrGLFormat::kSRGB8_ALPHA8;
301         case GR_GL_COMPRESSED_ETC1_RGB8: return GrGLFormat::kCOMPRESSED_ETC1_RGB8;
302         case GR_GL_COMPRESSED_RGB8_ETC2: return GrGLFormat::kCOMPRESSED_RGB8_ETC2;
303         case GR_GL_COMPRESSED_RGB_S3TC_DXT1_EXT: return GrGLFormat::kCOMPRESSED_RGB8_BC1;
304         case GR_GL_COMPRESSED_RGBA_S3TC_DXT1_EXT: return GrGLFormat::kCOMPRESSED_RGBA8_BC1;
305         case GR_GL_R16:                  return GrGLFormat::kR16;
306         case GR_GL_RG16:                 return GrGLFormat::kRG16;
307         case GR_GL_RGBA16:               return GrGLFormat::kRGBA16;
308         case GR_GL_RG16F:                return GrGLFormat::kRG16F;
309 
310         default:                         return GrGLFormat::kUnknown;
311     }
312 }
313 
314 /** Returns either the sized internal format or compressed internal format of the GrGLFormat. */
GrGLFormatToEnum(GrGLFormat format)315 static constexpr GrGLenum GrGLFormatToEnum(GrGLFormat format) {
316     switch (format) {
317         case GrGLFormat::kRGBA8:                return GR_GL_RGBA8;
318         case GrGLFormat::kR8:                   return GR_GL_R8;
319         case GrGLFormat::kALPHA8:               return GR_GL_ALPHA8;
320         case GrGLFormat::kLUMINANCE8:           return GR_GL_LUMINANCE8;
321         case GrGLFormat::kBGRA8:                return GR_GL_BGRA8;
322         case GrGLFormat::kRGB565:               return GR_GL_RGB565;
323         case GrGLFormat::kRGBA16F:              return GR_GL_RGBA16F;
324         case GrGLFormat::kLUMINANCE16F:         return GR_GL_LUMINANCE16F;
325         case GrGLFormat::kR16F:                 return GR_GL_R16F;
326         case GrGLFormat::kRGB8:                 return GR_GL_RGB8;
327         case GrGLFormat::kRG8:                  return GR_GL_RG8;
328         case GrGLFormat::kRGB10_A2:             return GR_GL_RGB10_A2;
329         case GrGLFormat::kRGBA4:                return GR_GL_RGBA4;
330         case GrGLFormat::kSRGB8_ALPHA8:         return GR_GL_SRGB8_ALPHA8;
331         case GrGLFormat::kCOMPRESSED_ETC1_RGB8: return GR_GL_COMPRESSED_ETC1_RGB8;
332         case GrGLFormat::kCOMPRESSED_RGB8_ETC2: return GR_GL_COMPRESSED_RGB8_ETC2;
333         case GrGLFormat::kCOMPRESSED_RGB8_BC1:  return GR_GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
334         case GrGLFormat::kCOMPRESSED_RGBA8_BC1: return GR_GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
335         case GrGLFormat::kR16:                  return GR_GL_R16;
336         case GrGLFormat::kRG16:                 return GR_GL_RG16;
337         case GrGLFormat::kRGBA16:               return GR_GL_RGBA16;
338         case GrGLFormat::kRG16F:                return GR_GL_RG16F;
339         case GrGLFormat::kUnknown:              return 0;
340     }
341     SkUNREACHABLE;
342 }
343 
344 #if GR_TEST_UTILS
GrGLFormatToStr(GrGLenum glFormat)345 static constexpr const char* GrGLFormatToStr(GrGLenum glFormat) {
346     switch (glFormat) {
347         case GR_GL_RGBA8:                return "RGBA8";
348         case GR_GL_R8:                   return "R8";
349         case GR_GL_ALPHA8:               return "ALPHA8";
350         case GR_GL_LUMINANCE8:           return "LUMINANCE8";
351         case GR_GL_BGRA8:                return "BGRA8";
352         case GR_GL_RGB565:               return "RGB565";
353         case GR_GL_RGBA16F:              return "RGBA16F";
354         case GR_GL_LUMINANCE16F:         return "LUMINANCE16F";
355         case GR_GL_R16F:                 return "R16F";
356         case GR_GL_RGB8:                 return "RGB8";
357         case GR_GL_RG8:                  return "RG8";
358         case GR_GL_RGB10_A2:             return "RGB10_A2";
359         case GR_GL_RGBA4:                return "RGBA4";
360         case GR_GL_RGBA32F:              return "RGBA32F";
361         case GR_GL_SRGB8_ALPHA8:         return "SRGB8_ALPHA8";
362         case GR_GL_COMPRESSED_ETC1_RGB8: return "ETC1";
363         case GR_GL_COMPRESSED_RGB8_ETC2: return "ETC2";
364         case GR_GL_COMPRESSED_RGB_S3TC_DXT1_EXT: return "RGB8_BC1";
365         case GR_GL_COMPRESSED_RGBA_S3TC_DXT1_EXT: return "RGBA8_BC1";
366         case GR_GL_R16:                  return "R16";
367         case GR_GL_RG16:                 return "RG16";
368         case GR_GL_RGBA16:               return "RGBA16";
369         case GR_GL_RG16F:                return "RG16F";
370 
371         default:                         return "Unknown";
372     }
373 }
374 #endif
375 
376 GrGLenum GrToGLStencilFunc(GrStencilTest test);
377 
378 /**
379  * Returns true if the format is compressed.
380  */
381 bool GrGLFormatIsCompressed(GrGLFormat);
382 
383 /**
384  * This will return CompressionType::kNone if the format is uncompressed.
385  */
386 SkImage::CompressionType GrGLFormatToCompressionType(GrGLFormat);
387 
388 #endif
389