blob: 686943bdcef2c82626a43f4e8860208b3bb6d710 [file] [log] [blame]
bsalomon@google.com9c1f1ac2012-05-07 17:09:37 +00001/*
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"
bsalomon@google.com91bcc942012-05-07 17:28:41 +000012#include "GrGLDefines.h"
bsalomon@google.com9c1f1ac2012-05-07 17:09:37 +000013
commit-bot@chromium.org215a6822013-09-05 18:28:42 +000014class SkMatrix;
15
bsalomon@google.com9c1f1ac2012-05-07 17:09:37 +000016////////////////////////////////////////////////////////////////////////////////
17
18typedef uint32_t GrGLVersion;
19typedef uint32_t GrGLSLVersion;
20
bsalomon@google.com0b1e4812012-10-23 13:52:43 +000021/**
22 * This list is lazily updated as required.
23 */
24enum GrGLVendor {
bsalomon@google.com96966a52013-02-21 16:34:21 +000025 kARM_GrGLVendor,
bsalomon@google.com3012ded2013-02-22 16:44:04 +000026 kImagination_GrGLVendor,
27 kIntel_GrGLVendor,
commit-bot@chromium.org7a434a22013-08-21 14:01:56 +000028 kQualcomm_GrGLVendor,
bsalomon@google.com3012ded2013-02-22 16:44:04 +000029
30 kOther_GrGLVendor
bsalomon@google.com0b1e4812012-10-23 13:52:43 +000031};
32
bsalomon@google.com9c1f1ac2012-05-07 17:09:37 +000033#define GR_GL_VER(major, minor) ((static_cast<int>(major) << 16) | \
34 static_cast<int>(minor))
35#define GR_GLSL_VER(major, minor) ((static_cast<int>(major) << 16) | \
36 static_cast<int>(minor))
37
38////////////////////////////////////////////////////////////////////////////////
39
40/**
41 * Some drivers want the var-int arg to be zero-initialized on input.
42 */
43#define GR_GL_INIT_ZERO 0
44#define GR_GL_GetIntegerv(gl, e, p) \
45 do { \
46 *(p) = GR_GL_INIT_ZERO; \
47 GR_GL_CALL(gl, GetIntegerv(e, p)); \
48 } while (0)
49
50#define GR_GL_GetFramebufferAttachmentParameteriv(gl, t, a, pname, p) \
51 do { \
52 *(p) = GR_GL_INIT_ZERO; \
53 GR_GL_CALL(gl, GetFramebufferAttachmentParameteriv(t, a, pname, p)); \
54 } while (0)
55
56#define GR_GL_GetRenderbufferParameteriv(gl, t, pname, p) \
57 do { \
58 *(p) = GR_GL_INIT_ZERO; \
59 GR_GL_CALL(gl, GetRenderbufferParameteriv(t, pname, p)); \
60 } while (0)
61#define GR_GL_GetTexLevelParameteriv(gl, t, l, pname, p) \
62 do { \
63 *(p) = GR_GL_INIT_ZERO; \
64 GR_GL_CALL(gl, GetTexLevelParameteriv(t, l, pname, p)); \
65 } while (0)
66
67////////////////////////////////////////////////////////////////////////////////
68
69/**
70 * Helpers for glGetString()
71 */
72
73// these variants assume caller already has a string from glGetString()
74GrGLVersion GrGLGetVersionFromString(const char* versionString);
75GrGLBinding GrGLGetBindingInUseFromString(const char* versionString);
76GrGLSLVersion GrGLGetGLSLVersionFromString(const char* versionString);
commit-bot@chromium.org459104c2013-06-14 14:42:56 +000077bool GrGLIsMesaFromVersionString(const char* versionString);
bsalomon@google.com0b1e4812012-10-23 13:52:43 +000078GrGLVendor GrGLGetVendorFromString(const char* vendorString);
bsalomon@google.com9c1f1ac2012-05-07 17:09:37 +000079
80// these variants call glGetString()
bsalomon@google.com9c1f1ac2012-05-07 17:09:37 +000081GrGLBinding GrGLGetBindingInUse(const GrGLInterface*);
82GrGLVersion GrGLGetVersion(const GrGLInterface*);
83GrGLSLVersion GrGLGetGLSLVersion(const GrGLInterface*);
bsalomon@google.com0b1e4812012-10-23 13:52:43 +000084GrGLVendor GrGLGetVendor(const GrGLInterface*);
bsalomon@google.com9c1f1ac2012-05-07 17:09:37 +000085
86/**
87 * Helpers for glGetError()
88 */
89
bsalomon@google.com2717d562012-05-07 19:10:52 +000090void GrGLCheckErr(const GrGLInterface* gl,
91 const char* location,
92 const char* call);
bsalomon@google.com9c1f1ac2012-05-07 17:09:37 +000093
bsalomon@google.com2717d562012-05-07 19:10:52 +000094void GrGLClearErr(const GrGLInterface* gl);
95
commit-bot@chromium.org215a6822013-09-05 18:28:42 +000096/**
97 * Helper for converting SkMatrix to a column-major GL float array
98 */
99template<int MatrixSize> void GrGLGetMatrix(GrGLfloat* dest, const SkMatrix& src);
100
bsalomon@google.com2717d562012-05-07 19:10:52 +0000101////////////////////////////////////////////////////////////////////////////////
102
103/**
104 * Macros for using GrGLInterface to make GL calls
105 */
106
107// internal macro to conditionally call glGetError based on compile-time and
108// run-time flags.
109#if GR_GL_CHECK_ERROR
110 extern bool gCheckErrorGL;
111 #define GR_GL_CHECK_ERROR_IMPL(IFACE, X) \
112 if (gCheckErrorGL) \
113 GrGLCheckErr(IFACE, GR_FILE_AND_LINE_STR, #X)
114#else
115 #define GR_GL_CHECK_ERROR_IMPL(IFACE, X)
116#endif
117
118// internal macro to conditionally log the gl call using GrPrintf based on
119// compile-time and run-time flags.
120#if GR_GL_LOG_CALLS
121 extern bool gLogCallsGL;
122 #define GR_GL_LOG_CALLS_IMPL(X) \
123 if (gLogCallsGL) \
124 GrPrintf(GR_FILE_AND_LINE_STR "GL: " #X "\n")
125#else
126 #define GR_GL_LOG_CALLS_IMPL(X)
127#endif
128
129// internal macro that does the per-GL-call callback (if necessary)
130#if GR_GL_PER_GL_FUNC_CALLBACK
131 #define GR_GL_CALLBACK_IMPL(IFACE) (IFACE)->fCallback(IFACE)
132#else
133 #define GR_GL_CALLBACK_IMPL(IFACE)
134#endif
135
136// makes a GL call on the interface and does any error checking and logging
137#define GR_GL_CALL(IFACE, X) \
138 do { \
139 GR_GL_CALL_NOERRCHECK(IFACE, X); \
140 GR_GL_CHECK_ERROR_IMPL(IFACE, X); \
141 } while (false)
142
143// Variant of above that always skips the error check. This is useful when
144// the caller wants to do its own glGetError() call and examine the error value.
145#define GR_GL_CALL_NOERRCHECK(IFACE, X) \
146 do { \
147 GR_GL_CALLBACK_IMPL(IFACE); \
148 (IFACE)->f##X; \
149 GR_GL_LOG_CALLS_IMPL(X); \
150 } while (false)
151
152// same as GR_GL_CALL but stores the return value of the gl call in RET
153#define GR_GL_CALL_RET(IFACE, RET, X) \
154 do { \
155 GR_GL_CALL_RET_NOERRCHECK(IFACE, RET, X); \
156 GR_GL_CHECK_ERROR_IMPL(IFACE, X); \
157 } while (false)
158
159// same as GR_GL_CALL_RET but always skips the error check.
160#define GR_GL_CALL_RET_NOERRCHECK(IFACE, RET, X) \
161 do { \
162 GR_GL_CALLBACK_IMPL(IFACE); \
163 (RET) = (IFACE)->f##X; \
164 GR_GL_LOG_CALLS_IMPL(X); \
165 } while (false)
166
167// call glGetError without doing a redundant error check or logging.
168#define GR_GL_GET_ERROR(IFACE) (IFACE)->fGetError()
bsalomon@google.com9c1f1ac2012-05-07 17:09:37 +0000169
170#endif