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