• 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 "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     /** Either HD 6xxx or Iris 6xxx */
58     kIntel6xxx_GrGLRenderer,
59     /** T-6xx, T-7xx, or T-8xx */
60     kMaliT_GrGLRenderer,
61     kANGLE_GrGLRenderer,
62     kOther_GrGLRenderer
63 };
64 
65 enum GrGLDriver {
66     kMesa_GrGLDriver,
67     kChromium_GrGLDriver,
68     kNVIDIA_GrGLDriver,
69     kIntel_GrGLDriver,
70     kANGLE_GrGLDriver,
71     kQualcomm_GrGLDriver,
72     kUnknown_GrGLDriver
73 };
74 
75 ////////////////////////////////////////////////////////////////////////////////
76 
77 /**
78  *  Some drivers want the var-int arg to be zero-initialized on input.
79  */
80 #define GR_GL_INIT_ZERO     0
81 #define GR_GL_GetIntegerv(gl, e, p)                                            \
82     do {                                                                       \
83         *(p) = GR_GL_INIT_ZERO;                                                \
84         GR_GL_CALL(gl, GetIntegerv(e, p));                                     \
85     } while (0)
86 
87 #define GR_GL_GetFramebufferAttachmentParameteriv(gl, t, a, pname, p)          \
88     do {                                                                       \
89         *(p) = GR_GL_INIT_ZERO;                                                \
90         GR_GL_CALL(gl, GetFramebufferAttachmentParameteriv(t, a, pname, p));   \
91     } while (0)
92 
93 #define GR_GL_GetInternalformativ(gl, t, f, n, s, p)                           \
94     do {                                                                       \
95         *(p) = GR_GL_INIT_ZERO;                                                \
96         GR_GL_CALL(gl, GetInternalformativ(t, f, n, s, p));                    \
97     } while (0)
98 
99 #define GR_GL_GetNamedFramebufferAttachmentParameteriv(gl, fb, a, pname, p)          \
100     do {                                                                             \
101         *(p) = GR_GL_INIT_ZERO;                                                      \
102         GR_GL_CALL(gl, GetNamedFramebufferAttachmentParameteriv(fb, a, pname, p));   \
103     } while (0)
104 
105 #define GR_GL_GetRenderbufferParameteriv(gl, t, pname, p)                      \
106     do {                                                                       \
107         *(p) = GR_GL_INIT_ZERO;                                                \
108         GR_GL_CALL(gl, GetRenderbufferParameteriv(t, pname, p));               \
109     } while (0)
110 
111 #define GR_GL_GetTexLevelParameteriv(gl, t, l, pname, p)                       \
112     do {                                                                       \
113         *(p) = GR_GL_INIT_ZERO;                                                \
114         GR_GL_CALL(gl, GetTexLevelParameteriv(t, l, pname, p));                \
115     } while (0)
116 
117 #define GR_GL_GetShaderPrecisionFormat(gl, st, pt, range, precision)           \
118     do {                                                                       \
119         (range)[0] = GR_GL_INIT_ZERO;                                          \
120         (range)[1] = GR_GL_INIT_ZERO;                                          \
121         (*precision) = GR_GL_INIT_ZERO;                                        \
122         GR_GL_CALL(gl, GetShaderPrecisionFormat(st, pt, range, precision));    \
123     } while (0)
124 
125 ////////////////////////////////////////////////////////////////////////////////
126 
127 /**
128  * Helpers for glGetString()
129  */
130 
131 // these variants assume caller already has a string from glGetString()
132 GrGLVersion GrGLGetVersionFromString(const char* versionString);
133 GrGLStandard GrGLGetStandardInUseFromString(const char* versionString);
134 GrGLSLVersion GrGLGetGLSLVersionFromString(const char* versionString);
135 GrGLVendor GrGLGetVendorFromString(const char* vendorString);
136 GrGLRenderer GrGLGetRendererFromString(const char* rendererString);
137 
138 void GrGLGetDriverInfo(GrGLStandard standard,
139                        GrGLVendor vendor,
140                        const char* rendererString,
141                        const char* versionString,
142                        GrGLDriver* outDriver,
143                        GrGLDriverVersion* outVersion);
144 
145 // these variants call glGetString()
146 GrGLVersion GrGLGetVersion(const GrGLInterface*);
147 GrGLSLVersion GrGLGetGLSLVersion(const GrGLInterface*);
148 GrGLVendor GrGLGetVendor(const GrGLInterface*);
149 GrGLRenderer GrGLGetRenderer(const GrGLInterface*);
150 
151 
152 /**
153  * Helpers for glGetError()
154  */
155 
156 void GrGLCheckErr(const GrGLInterface* gl,
157                   const char* location,
158                   const char* call);
159 
160 void GrGLClearErr(const GrGLInterface* gl);
161 
162 ////////////////////////////////////////////////////////////////////////////////
163 
164 /**
165  * Macros for using GrGLInterface to make GL calls
166  */
167 
168 // internal macro to conditionally call glGetError based on compile-time and
169 // run-time flags.
170 #if GR_GL_CHECK_ERROR
171     extern bool gCheckErrorGL;
172     #define GR_GL_CHECK_ERROR_IMPL(IFACE, X)                    \
173         if (gCheckErrorGL)                                      \
174             GrGLCheckErr(IFACE, GR_FILE_AND_LINE_STR, #X)
175 #else
176     #define GR_GL_CHECK_ERROR_IMPL(IFACE, X)
177 #endif
178 
179 // internal macro to conditionally log the gl call using SkDebugf based on
180 // compile-time and run-time flags.
181 #if GR_GL_LOG_CALLS
182     extern bool gLogCallsGL;
183     #define GR_GL_LOG_CALLS_IMPL(X)                             \
184         if (gLogCallsGL)                                        \
185             SkDebugf(GR_FILE_AND_LINE_STR "GL: " #X "\n")
186 #else
187     #define GR_GL_LOG_CALLS_IMPL(X)
188 #endif
189 
190 // makes a GL call on the interface and does any error checking and logging
191 #define GR_GL_CALL(IFACE, X)                                    \
192     do {                                                        \
193         GR_GL_CALL_NOERRCHECK(IFACE, X);                        \
194         GR_GL_CHECK_ERROR_IMPL(IFACE, X);                       \
195     } while (false)
196 
197 // Variant of above that always skips the error check. This is useful when
198 // the caller wants to do its own glGetError() call and examine the error value.
199 #define GR_GL_CALL_NOERRCHECK(IFACE, X)                         \
200     do {                                                        \
201         (IFACE)->fFunctions.f##X;                               \
202         GR_GL_LOG_CALLS_IMPL(X);                                \
203     } while (false)
204 
205 // same as GR_GL_CALL but stores the return value of the gl call in RET
206 #define GR_GL_CALL_RET(IFACE, RET, X)                           \
207     do {                                                        \
208         GR_GL_CALL_RET_NOERRCHECK(IFACE, RET, X);               \
209         GR_GL_CHECK_ERROR_IMPL(IFACE, X);                       \
210     } while (false)
211 
212 // same as GR_GL_CALL_RET but always skips the error check.
213 #define GR_GL_CALL_RET_NOERRCHECK(IFACE, RET, X)                \
214     do {                                                        \
215         (RET) = (IFACE)->fFunctions.f##X;                       \
216         GR_GL_LOG_CALLS_IMPL(X);                                \
217     } while (false)
218 
219 // call glGetError without doing a redundant error check or logging.
220 #define GR_GL_GET_ERROR(IFACE) (IFACE)->fFunctions.fGetError()
221 
222 GrGLenum GrToGLStencilFunc(GrStencilTest test);
223 
224 
225 #endif
226