2 * Copyright 2012 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #ifndef GrGLUtil_DEFINED
9 #define GrGLUtil_DEFINED
11 #include "gl/GrGLInterface.h"
12 #include "GrGLDefines.h"
16 ////////////////////////////////////////////////////////////////////////////////
18 typedef uint32_t GrGLVersion;
19 typedef uint32_t GrGLSLVersion;
21 #define GR_GL_VER(major, minor) ((static_cast<int>(major) << 16) | \
22 static_cast<int>(minor))
23 #define GR_GLSL_VER(major, minor) ((static_cast<int>(major) << 16) | \
24 static_cast<int>(minor))
26 #define GR_GL_INVALID_VER GR_GL_VER(0, 0)
27 #define GR_GLSL_INVALID_VER GR_GL_VER(0, 0)
30 * The Vendor and Renderer enum values are lazily updated as required.
34 kImagination_GrGLVendor,
49 ////////////////////////////////////////////////////////////////////////////////
52 * Some drivers want the var-int arg to be zero-initialized on input.
54 #define GR_GL_INIT_ZERO 0
55 #define GR_GL_GetIntegerv(gl, e, p) \
57 *(p) = GR_GL_INIT_ZERO; \
58 GR_GL_CALL(gl, GetIntegerv(e, p)); \
61 #define GR_GL_GetFramebufferAttachmentParameteriv(gl, t, a, pname, p) \
63 *(p) = GR_GL_INIT_ZERO; \
64 GR_GL_CALL(gl, GetFramebufferAttachmentParameteriv(t, a, pname, p)); \
67 #define GR_GL_GetRenderbufferParameteriv(gl, t, pname, p) \
69 *(p) = GR_GL_INIT_ZERO; \
70 GR_GL_CALL(gl, GetRenderbufferParameteriv(t, pname, p)); \
72 #define GR_GL_GetTexLevelParameteriv(gl, t, l, pname, p) \
74 *(p) = GR_GL_INIT_ZERO; \
75 GR_GL_CALL(gl, GetTexLevelParameteriv(t, l, pname, p)); \
78 ////////////////////////////////////////////////////////////////////////////////
81 * Helpers for glGetString()
84 // these variants assume caller already has a string from glGetString()
85 GrGLVersion GrGLGetVersionFromString(const char* versionString);
86 GrGLStandard GrGLGetStandardInUseFromString(const char* versionString);
87 GrGLSLVersion GrGLGetGLSLVersionFromString(const char* versionString);
88 bool GrGLIsMesaFromVersionString(const char* versionString);
89 GrGLVendor GrGLGetVendorFromString(const char* vendorString);
90 GrGLRenderer GrGLGetRendererFromString(const char* rendererString);
91 bool GrGLIsChromiumFromRendererString(const char* rendererString);
93 // these variants call glGetString()
94 GrGLVersion GrGLGetVersion(const GrGLInterface*);
95 GrGLSLVersion GrGLGetGLSLVersion(const GrGLInterface*);
96 GrGLVendor GrGLGetVendor(const GrGLInterface*);
97 GrGLRenderer GrGLGetRenderer(const GrGLInterface*);
101 * Helpers for glGetError()
104 void GrGLCheckErr(const GrGLInterface* gl,
105 const char* location,
108 void GrGLClearErr(const GrGLInterface* gl);
111 * Helper for converting SkMatrix to a column-major GL float array
113 template<int MatrixSize> void GrGLGetMatrix(GrGLfloat* dest, const SkMatrix& src);
115 ////////////////////////////////////////////////////////////////////////////////
118 * Macros for using GrGLInterface to make GL calls
121 // internal macro to conditionally call glGetError based on compile-time and
123 #if GR_GL_CHECK_ERROR
124 extern bool gCheckErrorGL;
125 #define GR_GL_CHECK_ERROR_IMPL(IFACE, X) \
127 GrGLCheckErr(IFACE, GR_FILE_AND_LINE_STR, #X)
129 #define GR_GL_CHECK_ERROR_IMPL(IFACE, X)
132 // internal macro to conditionally log the gl call using GrPrintf based on
133 // compile-time and run-time flags.
135 extern bool gLogCallsGL;
136 #define GR_GL_LOG_CALLS_IMPL(X) \
138 GrPrintf(GR_FILE_AND_LINE_STR "GL: " #X "\n")
140 #define GR_GL_LOG_CALLS_IMPL(X)
143 // internal macro that does the per-GL-call callback (if necessary)
144 #if GR_GL_PER_GL_FUNC_CALLBACK
145 #define GR_GL_CALLBACK_IMPL(IFACE) (IFACE)->fCallback(IFACE)
147 #define GR_GL_CALLBACK_IMPL(IFACE)
150 // makes a GL call on the interface and does any error checking and logging
151 #define GR_GL_CALL(IFACE, X) \
153 GR_GL_CALL_NOERRCHECK(IFACE, X); \
154 GR_GL_CHECK_ERROR_IMPL(IFACE, X); \
157 // Variant of above that always skips the error check. This is useful when
158 // the caller wants to do its own glGetError() call and examine the error value.
159 #define GR_GL_CALL_NOERRCHECK(IFACE, X) \
161 GR_GL_CALLBACK_IMPL(IFACE); \
162 (IFACE)->fFunctions.f##X; \
163 GR_GL_LOG_CALLS_IMPL(X); \
166 // same as GR_GL_CALL but stores the return value of the gl call in RET
167 #define GR_GL_CALL_RET(IFACE, RET, X) \
169 GR_GL_CALL_RET_NOERRCHECK(IFACE, RET, X); \
170 GR_GL_CHECK_ERROR_IMPL(IFACE, X); \
173 // same as GR_GL_CALL_RET but always skips the error check.
174 #define GR_GL_CALL_RET_NOERRCHECK(IFACE, RET, X) \
176 GR_GL_CALLBACK_IMPL(IFACE); \
177 (RET) = (IFACE)->fFunctions.f##X; \
178 GR_GL_LOG_CALLS_IMPL(X); \
181 // call glGetError without doing a redundant error check or logging.
182 #define GR_GL_GET_ERROR(IFACE) (IFACE)->fFunctions.fGetError()