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