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