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 "gl/GrGLInterface.h"
12 #include "GrGLDefines.h"
13 #include "GrStencilSettings.h"
14 
15 class SkMatrix;
16 
17 ////////////////////////////////////////////////////////////////////////////////
18 
19 typedef uint32_t GrGLVersion;
20 typedef uint32_t GrGLSLVersion;
21 typedef uint32_t GrGLDriverVersion;
22 
23 #define GR_GL_VER(major, minor) ((static_cast<int>(major) << 16) | \
24                                  static_cast<int>(minor))
25 #define GR_GLSL_VER(major, minor) ((static_cast<int>(major) << 16) | \
26                                    static_cast<int>(minor))
27 #define GR_GL_DRIVER_VER(major, minor) ((static_cast<int>(major) << 16) | \
28                                         static_cast<int>(minor))
29 
30 #define GR_GL_INVALID_VER GR_GL_VER(0, 0)
31 #define GR_GLSL_INVALID_VER GR_GLSL_VER(0, 0)
32 #define GR_GL_DRIVER_UNKNOWN_VER GR_GL_DRIVER_VER(0, 0)
33 
34 /**
35  * The Vendor and Renderer enum values are lazily updated as required.
36  */
37 enum GrGLVendor {
38     kARM_GrGLVendor,
39     kImagination_GrGLVendor,
40     kIntel_GrGLVendor,
41     kQualcomm_GrGLVendor,
42     kNVIDIA_GrGLVendor,
43     kATI_GrGLVendor,
44 
45     kOther_GrGLVendor
46 };
47 
48 enum GrGLRenderer {
49     kTegra2_GrGLRenderer,
50     kTegra3_GrGLRenderer,
51     kPowerVR54x_GrGLRenderer,
52     kPowerVRRogue_GrGLRenderer,
53     kAdreno3xx_GrGLRenderer,
54     kAdreno4xx_GrGLRenderer,
55     kAdreno5xx_GrGLRenderer,
56     kOSMesa_GrGLRenderer,
57     kOther_GrGLRenderer
58 };
59 
60 enum GrGLDriver {
61     kMesa_GrGLDriver,
62     kChromium_GrGLDriver,
63     kNVIDIA_GrGLDriver,
64     kIntel_GrGLDriver,
65     kANGLE_GrGLDriver,
66     kUnknown_GrGLDriver
67 };
68 
69 ////////////////////////////////////////////////////////////////////////////////
70 
71 /**
72  *  Some drivers want the var-int arg to be zero-initialized on input.
73  */
74 #define GR_GL_INIT_ZERO     0
75 #define GR_GL_GetIntegerv(gl, e, p)                                            \
76     do {                                                                       \
77         *(p) = GR_GL_INIT_ZERO;                                                \
78         GR_GL_CALL(gl, GetIntegerv(e, p));                                     \
79     } while (0)
80 
81 #define GR_GL_GetFramebufferAttachmentParameteriv(gl, t, a, pname, p)          \
82     do {                                                                       \
83         *(p) = GR_GL_INIT_ZERO;                                                \
84         GR_GL_CALL(gl, GetFramebufferAttachmentParameteriv(t, a, pname, p));   \
85     } while (0)
86 
87 #define GR_GL_GetNamedFramebufferAttachmentParameteriv(gl, fb, a, pname, p)          \
88     do {                                                                             \
89         *(p) = GR_GL_INIT_ZERO;                                                      \
90         GR_GL_CALL(gl, GetNamedFramebufferAttachmentParameteriv(fb, a, pname, p));   \
91     } while (0)
92 
93 #define GR_GL_GetRenderbufferParameteriv(gl, t, pname, p)                      \
94     do {                                                                       \
95         *(p) = GR_GL_INIT_ZERO;                                                \
96         GR_GL_CALL(gl, GetRenderbufferParameteriv(t, pname, p));               \
97     } while (0)
98 
99 #define GR_GL_GetTexLevelParameteriv(gl, t, l, pname, p)                       \
100     do {                                                                       \
101         *(p) = GR_GL_INIT_ZERO;                                                \
102         GR_GL_CALL(gl, GetTexLevelParameteriv(t, l, pname, p));                \
103     } while (0)
104 
105 #define GR_GL_GetShaderPrecisionFormat(gl, st, pt, range, precision)           \
106     do {                                                                       \
107         (range)[0] = GR_GL_INIT_ZERO;                                          \
108         (range)[1] = GR_GL_INIT_ZERO;                                          \
109         (*precision) = GR_GL_INIT_ZERO;                                        \
110         GR_GL_CALL(gl, GetShaderPrecisionFormat(st, pt, range, precision));    \
111     } while (0)
112 
113 ////////////////////////////////////////////////////////////////////////////////
114 
115 /**
116  * Helpers for glGetString()
117  */
118 
119 // these variants assume caller already has a string from glGetString()
120 GrGLVersion GrGLGetVersionFromString(const char* versionString);
121 GrGLStandard GrGLGetStandardInUseFromString(const char* versionString);
122 GrGLSLVersion GrGLGetGLSLVersionFromString(const char* versionString);
123 GrGLVendor GrGLGetVendorFromString(const char* vendorString);
124 GrGLRenderer GrGLGetRendererFromString(const char* rendererString);
125 
126 void GrGLGetDriverInfo(GrGLStandard standard,
127                        GrGLVendor vendor,
128                        const char* rendererString,
129                        const char* versionString,
130                        GrGLDriver* outDriver,
131                        GrGLDriverVersion* outVersion);
132 
133 // these variants call glGetString()
134 GrGLVersion GrGLGetVersion(const GrGLInterface*);
135 GrGLSLVersion GrGLGetGLSLVersion(const GrGLInterface*);
136 GrGLVendor GrGLGetVendor(const GrGLInterface*);
137 GrGLRenderer GrGLGetRenderer(const GrGLInterface*);
138 
139 
140 /**
141  * Helpers for glGetError()
142  */
143 
144 void GrGLCheckErr(const GrGLInterface* gl,
145                   const char* location,
146                   const char* call);
147 
148 void GrGLClearErr(const GrGLInterface* gl);
149 
150 ////////////////////////////////////////////////////////////////////////////////
151 
152 /**
153  * Macros for using GrGLInterface to make GL calls
154  */
155 
156 // internal macro to conditionally call glGetError based on compile-time and
157 // run-time flags.
158 #if GR_GL_CHECK_ERROR
159     extern bool gCheckErrorGL;
160     #define GR_GL_CHECK_ERROR_IMPL(IFACE, X)                    \
161         if (gCheckErrorGL)                                      \
162             GrGLCheckErr(IFACE, GR_FILE_AND_LINE_STR, #X)
163 #else
164     #define GR_GL_CHECK_ERROR_IMPL(IFACE, X)
165 #endif
166 
167 // internal macro to conditionally log the gl call using SkDebugf based on
168 // compile-time and run-time flags.
169 #if GR_GL_LOG_CALLS
170     extern bool gLogCallsGL;
171     #define GR_GL_LOG_CALLS_IMPL(X)                             \
172         if (gLogCallsGL)                                        \
173             SkDebugf(GR_FILE_AND_LINE_STR "GL: " #X "\n")
174 #else
175     #define GR_GL_LOG_CALLS_IMPL(X)
176 #endif
177 
178 // makes a GL call on the interface and does any error checking and logging
179 #define GR_GL_CALL(IFACE, X)                                    \
180     do {                                                        \
181         GR_GL_CALL_NOERRCHECK(IFACE, X);                        \
182         GR_GL_CHECK_ERROR_IMPL(IFACE, X);                       \
183     } while (false)
184 
185 // Variant of above that always skips the error check. This is useful when
186 // the caller wants to do its own glGetError() call and examine the error value.
187 #define GR_GL_CALL_NOERRCHECK(IFACE, X)                         \
188     do {                                                        \
189         (IFACE)->fFunctions.f##X;                               \
190         GR_GL_LOG_CALLS_IMPL(X);                                \
191     } while (false)
192 
193 // same as GR_GL_CALL but stores the return value of the gl call in RET
194 #define GR_GL_CALL_RET(IFACE, RET, X)                           \
195     do {                                                        \
196         GR_GL_CALL_RET_NOERRCHECK(IFACE, RET, X);               \
197         GR_GL_CHECK_ERROR_IMPL(IFACE, X);                       \
198     } while (false)
199 
200 // same as GR_GL_CALL_RET but always skips the error check.
201 #define GR_GL_CALL_RET_NOERRCHECK(IFACE, RET, X)                \
202     do {                                                        \
203         (RET) = (IFACE)->fFunctions.f##X;                       \
204         GR_GL_LOG_CALLS_IMPL(X);                                \
205     } while (false)
206 
207 // call glGetError without doing a redundant error check or logging.
208 #define GR_GL_GET_ERROR(IFACE) (IFACE)->fFunctions.fGetError()
209 
210 GrGLenum GrToGLStencilFunc(GrStencilTest test);
211 
212 
213 #endif
214