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"
13 #include "GrStencil.h"
17 ////////////////////////////////////////////////////////////////////////////////
19 typedef uint32_t GrGLVersion;
20 typedef uint32_t GrGLSLVersion;
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))
27 #define GR_GL_INVALID_VER GR_GL_VER(0, 0)
28 #define GR_GLSL_INVALID_VER GR_GL_VER(0, 0)
31 * The Vendor and Renderer enum values are lazily updated as required.
35 kImagination_GrGLVendor,
50 ////////////////////////////////////////////////////////////////////////////////
53 * Some drivers want the var-int arg to be zero-initialized on input.
55 #define GR_GL_INIT_ZERO 0
56 #define GR_GL_GetIntegerv(gl, e, p) \
58 *(p) = GR_GL_INIT_ZERO; \
59 GR_GL_CALL(gl, GetIntegerv(e, p)); \
62 #define GR_GL_GetFramebufferAttachmentParameteriv(gl, t, a, pname, p) \
64 *(p) = GR_GL_INIT_ZERO; \
65 GR_GL_CALL(gl, GetFramebufferAttachmentParameteriv(t, a, pname, p)); \
68 #define GR_GL_GetRenderbufferParameteriv(gl, t, pname, p) \
70 *(p) = GR_GL_INIT_ZERO; \
71 GR_GL_CALL(gl, GetRenderbufferParameteriv(t, pname, p)); \
73 #define GR_GL_GetTexLevelParameteriv(gl, t, l, pname, p) \
75 *(p) = GR_GL_INIT_ZERO; \
76 GR_GL_CALL(gl, GetTexLevelParameteriv(t, l, pname, p)); \
79 ////////////////////////////////////////////////////////////////////////////////
82 * Helpers for glGetString()
85 // these variants assume caller already has a string from glGetString()
86 GrGLVersion GrGLGetVersionFromString(const char* versionString);
87 GrGLStandard GrGLGetStandardInUseFromString(const char* versionString);
88 GrGLSLVersion GrGLGetGLSLVersionFromString(const char* versionString);
89 bool GrGLIsMesaFromVersionString(const char* versionString);
90 GrGLVendor GrGLGetVendorFromString(const char* vendorString);
91 GrGLRenderer GrGLGetRendererFromString(const char* rendererString);
92 bool GrGLIsChromiumFromRendererString(const char* rendererString);
94 // these variants call glGetString()
95 GrGLVersion GrGLGetVersion(const GrGLInterface*);
96 GrGLSLVersion GrGLGetGLSLVersion(const GrGLInterface*);
97 GrGLVendor GrGLGetVendor(const GrGLInterface*);
98 GrGLRenderer GrGLGetRenderer(const GrGLInterface*);
102 * Helpers for glGetError()
105 void GrGLCheckErr(const GrGLInterface* gl,
106 const char* location,
109 void GrGLClearErr(const GrGLInterface* gl);
112 * Helper for converting SkMatrix to a column-major GL float array
114 template<int MatrixSize> void GrGLGetMatrix(GrGLfloat* dest, const SkMatrix& src);
116 ////////////////////////////////////////////////////////////////////////////////
119 * Macros for using GrGLInterface to make GL calls
122 // internal macro to conditionally call glGetError based on compile-time and
124 #if GR_GL_CHECK_ERROR
125 extern bool gCheckErrorGL;
126 #define GR_GL_CHECK_ERROR_IMPL(IFACE, X) \
128 GrGLCheckErr(IFACE, GR_FILE_AND_LINE_STR, #X)
130 #define GR_GL_CHECK_ERROR_IMPL(IFACE, X)
133 // internal macro to conditionally log the gl call using GrPrintf based on
134 // compile-time and run-time flags.
136 extern bool gLogCallsGL;
137 #define GR_GL_LOG_CALLS_IMPL(X) \
139 GrPrintf(GR_FILE_AND_LINE_STR "GL: " #X "\n")
141 #define GR_GL_LOG_CALLS_IMPL(X)
144 // internal macro that does the per-GL-call callback (if necessary)
145 #if GR_GL_PER_GL_FUNC_CALLBACK
146 #define GR_GL_CALLBACK_IMPL(IFACE) (IFACE)->fCallback(IFACE)
148 #define GR_GL_CALLBACK_IMPL(IFACE)
151 // makes a GL call on the interface and does any error checking and logging
152 #define GR_GL_CALL(IFACE, X) \
154 GR_GL_CALL_NOERRCHECK(IFACE, X); \
155 GR_GL_CHECK_ERROR_IMPL(IFACE, X); \
158 // Variant of above that always skips the error check. This is useful when
159 // the caller wants to do its own glGetError() call and examine the error value.
160 #define GR_GL_CALL_NOERRCHECK(IFACE, X) \
162 GR_GL_CALLBACK_IMPL(IFACE); \
163 (IFACE)->fFunctions.f##X; \
164 GR_GL_LOG_CALLS_IMPL(X); \
167 // same as GR_GL_CALL but stores the return value of the gl call in RET
168 #define GR_GL_CALL_RET(IFACE, RET, X) \
170 GR_GL_CALL_RET_NOERRCHECK(IFACE, RET, X); \
171 GR_GL_CHECK_ERROR_IMPL(IFACE, X); \
174 // same as GR_GL_CALL_RET but always skips the error check.
175 #define GR_GL_CALL_RET_NOERRCHECK(IFACE, RET, X) \
177 GR_GL_CALLBACK_IMPL(IFACE); \
178 (RET) = (IFACE)->fFunctions.f##X; \
179 GR_GL_LOG_CALLS_IMPL(X); \
182 // call glGetError without doing a redundant error check or logging.
183 #define GR_GL_GET_ERROR(IFACE) (IFACE)->fFunctions.fGetError()
185 GrGLenum GrToGLStencilFunc(GrStencilFunc basicFunc);