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