Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / skia / src / gpu / gl / GrGLUtil.h
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
14 class SkMatrix;
15
16 ////////////////////////////////////////////////////////////////////////////////
17
18 typedef uint32_t GrGLVersion;
19 typedef uint32_t GrGLSLVersion;
20
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))
25
26 #define GR_GL_INVALID_VER GR_GL_VER(0, 0)
27 #define GR_GLSL_INVALID_VER GR_GL_VER(0, 0)
28
29 /**
30  * The Vendor and Renderer enum values are lazily updated as required.
31  */
32 enum GrGLVendor {
33     kARM_GrGLVendor,
34     kImagination_GrGLVendor,
35     kIntel_GrGLVendor,
36     kQualcomm_GrGLVendor,
37     kNVIDIA_GrGLVendor,
38
39     kOther_GrGLVendor
40 };
41
42 enum GrGLRenderer {
43     kTegra2_GrGLRenderer,
44     kTegra3_GrGLRenderer,
45
46     kOther_GrGLRenderer
47 };
48
49 ////////////////////////////////////////////////////////////////////////////////
50
51 /**
52  *  Some drivers want the var-int arg to be zero-initialized on input.
53  */
54 #define GR_GL_INIT_ZERO     0
55 #define GR_GL_GetIntegerv(gl, e, p)                                            \
56     do {                                                                       \
57         *(p) = GR_GL_INIT_ZERO;                                                \
58         GR_GL_CALL(gl, GetIntegerv(e, p));                                     \
59     } while (0)
60
61 #define GR_GL_GetFramebufferAttachmentParameteriv(gl, t, a, pname, p)          \
62     do {                                                                       \
63         *(p) = GR_GL_INIT_ZERO;                                                \
64         GR_GL_CALL(gl, GetFramebufferAttachmentParameteriv(t, a, pname, p));   \
65     } while (0)
66
67 #define GR_GL_GetRenderbufferParameteriv(gl, t, pname, p)                      \
68     do {                                                                       \
69         *(p) = GR_GL_INIT_ZERO;                                                \
70         GR_GL_CALL(gl, GetRenderbufferParameteriv(t, pname, p));               \
71     } while (0)
72 #define GR_GL_GetTexLevelParameteriv(gl, t, l, pname, p)                       \
73     do {                                                                       \
74         *(p) = GR_GL_INIT_ZERO;                                                \
75         GR_GL_CALL(gl, GetTexLevelParameteriv(t, l, pname, p));                \
76     } while (0)
77
78 ////////////////////////////////////////////////////////////////////////////////
79
80 /**
81  * Helpers for glGetString()
82  */
83
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);
92
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*);
98
99
100 /**
101  * Helpers for glGetError()
102  */
103
104 void GrGLCheckErr(const GrGLInterface* gl,
105                   const char* location,
106                   const char* call);
107
108 void GrGLClearErr(const GrGLInterface* gl);
109
110 /**
111  * Helper for converting SkMatrix to a column-major GL float array
112  */
113 template<int MatrixSize> void GrGLGetMatrix(GrGLfloat* dest, const SkMatrix& src);
114
115 ////////////////////////////////////////////////////////////////////////////////
116
117 /**
118  * Macros for using GrGLInterface to make GL calls
119  */
120
121 // internal macro to conditionally call glGetError based on compile-time and
122 // run-time flags.
123 #if GR_GL_CHECK_ERROR
124     extern bool gCheckErrorGL;
125     #define GR_GL_CHECK_ERROR_IMPL(IFACE, X)                    \
126         if (gCheckErrorGL)                                      \
127             GrGLCheckErr(IFACE, GR_FILE_AND_LINE_STR, #X)
128 #else
129     #define GR_GL_CHECK_ERROR_IMPL(IFACE, X)
130 #endif
131
132 // internal macro to conditionally log the gl call using GrPrintf based on
133 // compile-time and run-time flags.
134 #if GR_GL_LOG_CALLS
135     extern bool gLogCallsGL;
136     #define GR_GL_LOG_CALLS_IMPL(X)                             \
137         if (gLogCallsGL)                                        \
138             GrPrintf(GR_FILE_AND_LINE_STR "GL: " #X "\n")
139 #else
140     #define GR_GL_LOG_CALLS_IMPL(X)
141 #endif
142
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)
146 #else
147     #define GR_GL_CALLBACK_IMPL(IFACE)
148 #endif
149
150 // makes a GL call on the interface and does any error checking and logging
151 #define GR_GL_CALL(IFACE, X)                                    \
152     do {                                                        \
153         GR_GL_CALL_NOERRCHECK(IFACE, X);                        \
154         GR_GL_CHECK_ERROR_IMPL(IFACE, X);                       \
155     } while (false)
156
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)                         \
160     do {                                                        \
161         GR_GL_CALLBACK_IMPL(IFACE);                             \
162         (IFACE)->fFunctions.f##X;                               \
163         GR_GL_LOG_CALLS_IMPL(X);                                \
164     } while (false)
165
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)                           \
168     do {                                                        \
169         GR_GL_CALL_RET_NOERRCHECK(IFACE, RET, X);               \
170         GR_GL_CHECK_ERROR_IMPL(IFACE, X);                       \
171     } while (false)
172
173 // same as GR_GL_CALL_RET but always skips the error check.
174 #define GR_GL_CALL_RET_NOERRCHECK(IFACE, RET, X)                \
175     do {                                                        \
176         GR_GL_CALLBACK_IMPL(IFACE);                             \
177         (RET) = (IFACE)->fFunctions.f##X;                       \
178         GR_GL_LOG_CALLS_IMPL(X);                                \
179     } while (false)
180
181 // call glGetError without doing a redundant error check or logging.
182 #define GR_GL_GET_ERROR(IFACE) (IFACE)->fFunctions.fGetError()
183
184 #endif