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
15class SkMatrix;
16
17////////////////////////////////////////////////////////////////////////////////
18
19typedef uint32_t GrGLVersion;
20typedef 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 */
33enum GrGLVendor {
34    kARM_GrGLVendor,
35    kImagination_GrGLVendor,
36    kIntel_GrGLVendor,
37    kQualcomm_GrGLVendor,
38    kNVIDIA_GrGLVendor,
39
40    kOther_GrGLVendor
41};
42
43enum GrGLRenderer {
44    kTegra2_GrGLRenderer,
45    kTegra3_GrGLRenderer,
46
47    kOther_GrGLRenderer
48};
49
50////////////////////////////////////////////////////////////////////////////////
51
52/**
53 *  Some drivers want the var-int arg to be zero-initialized on input.
54 */
55#define GR_GL_INIT_ZERO     0
56#define GR_GL_GetIntegerv(gl, e, p)                                            \
57    do {                                                                       \
58        *(p) = GR_GL_INIT_ZERO;                                                \
59        GR_GL_CALL(gl, GetIntegerv(e, p));                                     \
60    } while (0)
61
62#define GR_GL_GetFramebufferAttachmentParameteriv(gl, t, a, pname, p)          \
63    do {                                                                       \
64        *(p) = GR_GL_INIT_ZERO;                                                \
65        GR_GL_CALL(gl, GetFramebufferAttachmentParameteriv(t, a, pname, p));   \
66    } while (0)
67
68#define GR_GL_GetRenderbufferParameteriv(gl, t, pname, p)                      \
69    do {                                                                       \
70        *(p) = GR_GL_INIT_ZERO;                                                \
71        GR_GL_CALL(gl, GetRenderbufferParameteriv(t, pname, p));               \
72    } while (0)
73#define GR_GL_GetTexLevelParameteriv(gl, t, l, pname, p)                       \
74    do {                                                                       \
75        *(p) = GR_GL_INIT_ZERO;                                                \
76        GR_GL_CALL(gl, GetTexLevelParameteriv(t, l, pname, p));                \
77    } while (0)
78
79////////////////////////////////////////////////////////////////////////////////
80
81/**
82 * Helpers for glGetString()
83 */
84
85// these variants assume caller already has a string from glGetString()
86GrGLVersion GrGLGetVersionFromString(const char* versionString);
87GrGLStandard GrGLGetStandardInUseFromString(const char* versionString);
88GrGLSLVersion GrGLGetGLSLVersionFromString(const char* versionString);
89bool GrGLIsMesaFromVersionString(const char* versionString);
90GrGLVendor GrGLGetVendorFromString(const char* vendorString);
91GrGLRenderer GrGLGetRendererFromString(const char* rendererString);
92bool GrGLIsChromiumFromRendererString(const char* rendererString);
93
94// these variants call glGetString()
95GrGLVersion GrGLGetVersion(const GrGLInterface*);
96GrGLSLVersion GrGLGetGLSLVersion(const GrGLInterface*);
97GrGLVendor GrGLGetVendor(const GrGLInterface*);
98GrGLRenderer GrGLGetRenderer(const GrGLInterface*);
99
100
101/**
102 * Helpers for glGetError()
103 */
104
105void GrGLCheckErr(const GrGLInterface* gl,
106                  const char* location,
107                  const char* call);
108
109void GrGLClearErr(const GrGLInterface* gl);
110
111/**
112 * Helper for converting SkMatrix to a column-major GL float array
113 */
114template<int MatrixSize> void GrGLGetMatrix(GrGLfloat* dest, const SkMatrix& src);
115
116////////////////////////////////////////////////////////////////////////////////
117
118/**
119 * Macros for using GrGLInterface to make GL calls
120 */
121
122// internal macro to conditionally call glGetError based on compile-time and
123// run-time flags.
124#if GR_GL_CHECK_ERROR
125    extern bool gCheckErrorGL;
126    #define GR_GL_CHECK_ERROR_IMPL(IFACE, X)                    \
127        if (gCheckErrorGL)                                      \
128            GrGLCheckErr(IFACE, GR_FILE_AND_LINE_STR, #X)
129#else
130    #define GR_GL_CHECK_ERROR_IMPL(IFACE, X)
131#endif
132
133// internal macro to conditionally log the gl call using GrPrintf based on
134// compile-time and run-time flags.
135#if GR_GL_LOG_CALLS
136    extern bool gLogCallsGL;
137    #define GR_GL_LOG_CALLS_IMPL(X)                             \
138        if (gLogCallsGL)                                        \
139            GrPrintf(GR_FILE_AND_LINE_STR "GL: " #X "\n")
140#else
141    #define GR_GL_LOG_CALLS_IMPL(X)
142#endif
143
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)
147#else
148    #define GR_GL_CALLBACK_IMPL(IFACE)
149#endif
150
151// makes a GL call on the interface and does any error checking and logging
152#define GR_GL_CALL(IFACE, X)                                    \
153    do {                                                        \
154        GR_GL_CALL_NOERRCHECK(IFACE, X);                        \
155        GR_GL_CHECK_ERROR_IMPL(IFACE, X);                       \
156    } while (false)
157
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)                         \
161    do {                                                        \
162        GR_GL_CALLBACK_IMPL(IFACE);                             \
163        (IFACE)->fFunctions.f##X;                               \
164        GR_GL_LOG_CALLS_IMPL(X);                                \
165    } while (false)
166
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)                           \
169    do {                                                        \
170        GR_GL_CALL_RET_NOERRCHECK(IFACE, RET, X);               \
171        GR_GL_CHECK_ERROR_IMPL(IFACE, X);                       \
172    } while (false)
173
174// same as GR_GL_CALL_RET but always skips the error check.
175#define GR_GL_CALL_RET_NOERRCHECK(IFACE, RET, X)                \
176    do {                                                        \
177        GR_GL_CALLBACK_IMPL(IFACE);                             \
178        (RET) = (IFACE)->fFunctions.f##X;                       \
179        GR_GL_LOG_CALLS_IMPL(X);                                \
180    } while (false)
181
182// call glGetError without doing a redundant error check or logging.
183#define GR_GL_GET_ERROR(IFACE) (IFACE)->fFunctions.fGetError()
184
185GrGLenum GrToGLStencilFunc(GrStencilFunc basicFunc);
186
187
188#endif
189