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