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