aboutsummaryrefslogtreecommitdiffhomepage
path: root/gpu/src/GrGLInterface.cpp
blob: fa6aa92f1113689bcbea3719440c56e148a90530 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
    Copyright 2011 Google Inc.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

         http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
 */


#include "GrGLInterface.h"
#include "GrTypes.h"

#include <stdio.h>

#if defined(GR_GL_PROC_ADDRESS_HEADER)
    #include GR_GL_PROC_ADDRESS_HEADER
#endif

#if !defined(GR_GL_PROC_ADDRESS)
    #error "Must define GR_GL_PROC_ADDRESS"
#endif

#define GR_GL_GET_PROC(PROC_NAME) \
    glBindings->f##PROC_NAME = \
                reinterpret_cast<GrGLInterface::GrGL##PROC_NAME##Proc>( \
                            GR_GL_PROC_ADDRESS(gl##PROC_NAME)); \
    GrAssert(NULL != glBindings->f##PROC_NAME && \
             "Missing GL binding: " #PROC_NAME);

#define GR_GL_GET_PROC_SUFFIX(PROC_NAME, SUFFIX) \
    glBindings->f##PROC_NAME = \
                reinterpret_cast<GrGLInterface::GrGL##PROC_NAME##Proc>( \
                        GR_GL_PROC_ADDRESS(gl##PROC_NAME##SUFFIX)); \
    GrAssert(NULL != glBindings->f##PROC_NAME && \
             "Missing GL binding: " #PROC_NAME);

#define GR_GL_GET_PROC_SYMBOL(PROC_NAME) \
    glBindings->f##PROC_NAME = reinterpret_cast<GrGLInterface::GrGL##PROC_NAME##Proc>(gl##PROC_NAME);

namespace {

void gl_version_from_string(int* major, int* minor,
                            const char* versionString) {
    if (NULL == versionString) {
        GrAssert(0);
        *major = 0;
        *minor = 0;
        return;
    }
#if GR_SUPPORT_GLDESKTOP
    int n = sscanf(versionString, "%d.%d", major, minor);
    if (n != 2) {
        GrAssert(0);
        *major = 0;
        *minor = 0;
        return;
    }
#else
    char profile[2];
    int n = sscanf(versionString, "OpenGL ES-%c%c %d.%d", profile, profile+1,
                   major, minor);
    bool ok = 4 == n;
    if (!ok) {
        int n = sscanf(versionString, "OpenGL ES %d.%d", major, minor);
        ok = 2 == n;
    }
    if (!ok) {
        GrAssert(0);
        *major = 0;
        *minor = 0;
        return;
    }
#endif
}

bool has_gl_extension_from_string(const char* ext,
                                  const char* extensionString) {
    int extLength = strlen(ext);

    while (true) {
        int n = strcspn(extensionString, " ");
        if (n == extLength && 0 == strncmp(ext, extensionString, n)) {
            return true;
        }
        if (0 == extensionString[n]) {
            return false;
        }
        extensionString += n+1;
    }

    return false;
}

GrGLInterface* gGLInterface = NULL;

void InitializeGLInterfaceExtensions(GrGLInterface* glBindings) {
    int major, minor;
    const char* versionString = reinterpret_cast<const char*>(
                glBindings->fGetString(GL_VERSION));
    const char* extensionString = reinterpret_cast<const char*>(
                glBindings->fGetString(GL_EXTENSIONS));
    gl_version_from_string(&major, &minor, versionString);

    bool fboFound = false;
#if GR_SUPPORT_GLDESKTOP
    if (major >= 3 || has_gl_extension_from_string("GL_ARB_framebuffer_object",
                                                   extensionString)) {
        // GL_ARB_framebuffer_object doesn't use ARB suffix.
        GR_GL_GET_PROC(GenFramebuffers);
        GR_GL_GET_PROC(BindFramebuffer);
        GR_GL_GET_PROC(FramebufferTexture2D);
        GR_GL_GET_PROC(CheckFramebufferStatus);
        GR_GL_GET_PROC(DeleteFramebuffers);
        GR_GL_GET_PROC(RenderbufferStorage);
        GR_GL_GET_PROC(GenRenderbuffers);
        GR_GL_GET_PROC(DeleteRenderbuffers);
        GR_GL_GET_PROC(FramebufferRenderbuffer);
        GR_GL_GET_PROC(BindRenderbuffer);
        GR_GL_GET_PROC(RenderbufferStorageMultisample);
        GR_GL_GET_PROC(BlitFramebuffer);
        fboFound = true;
    }

    #if GL_EXT_framebuffer_object && !GR_MAC_BUILD
    if (!fboFound &&
            has_gl_extension_from_string("GL_EXT_framebuffer_object",
                                         extensionString)) {
        GR_GL_GET_PROC_SUFFIX(GenFramebuffers, EXT);
        GR_GL_GET_PROC_SUFFIX(BindFramebuffer, EXT);
        GR_GL_GET_PROC_SUFFIX(FramebufferTexture2D, EXT);
        GR_GL_GET_PROC_SUFFIX(CheckFramebufferStatus, EXT);
        GR_GL_GET_PROC_SUFFIX(DeleteFramebuffers, EXT);
        GR_GL_GET_PROC_SUFFIX(RenderbufferStorage, EXT);
        GR_GL_GET_PROC_SUFFIX(GenRenderbuffers, EXT);
        GR_GL_GET_PROC_SUFFIX(DeleteRenderbuffers, EXT);
        GR_GL_GET_PROC_SUFFIX(FramebufferRenderbuffer, EXT);
        GR_GL_GET_PROC_SUFFIX(BindRenderbuffer, EXT);
        fboFound = true;

        if (has_gl_extension_from_string("GL_EXT_framebuffer_multisample",
                                         extensionString)) {
            GR_GL_GET_PROC_SUFFIX(RenderbufferStorageMultisample, EXT);
        }
        if (has_gl_extension_from_string("GL_EXT_framebuffer_blit",
                                         extensionString)) {
            GR_GL_GET_PROC_SUFFIX(BlitFramebuffer, EXT);
        }
    }
    #endif

    // we assume we have at least GL 1.5 or higher (VBOs introduced in 1.5)
    GrAssert((major == 1 && minor >= 5) || major >=2);
    GR_GL_GET_PROC(MapBuffer);
    GR_GL_GET_PROC(UnmapBuffer);
#else // !GR_SUPPORT_GLDESKTOP
    #if GR_SUPPORT_GLES2
    if (!fboFound && major >= 2) {// ES 2.0 supports FBO
        GR_GL_GET_PROC(GenFramebuffers);
        GR_GL_GET_PROC(BindFramebuffer);
        GR_GL_GET_PROC(FramebufferTexture2D);
        GR_GL_GET_PROC(CheckFramebufferStatus);
        GR_GL_GET_PROC(DeleteFramebuffers);
        GR_GL_GET_PROC(RenderbufferStorage);
        GR_GL_GET_PROC(GenRenderbuffers);
        GR_GL_GET_PROC(DeleteRenderbuffers);
        GR_GL_GET_PROC(FramebufferRenderbuffer);
        GR_GL_GET_PROC(BindRenderbuffer);
        fboFound = true;
    }
    #endif

    #if GL_OES_mapbuffer
    if (!fboFound &&
            has_gl_extension_from_string("GL_OES_framebuffer_object",
                                         extensionString)) {
        GR_GL_GET_PROC_SUFFIX(GenFramebuffers, OES);
        GR_GL_GET_PROC_SUFFIX(BindFramebuffer, OES);
        GR_GL_GET_PROC_SUFFIX(FramebufferTexture2D, OES);
        GR_GL_GET_PROC_SUFFIX(CheckFramebufferStatus, OES);
        GR_GL_GET_PROC_SUFFIX(DeleteFramebuffers, OES);
        GR_GL_GET_PROC_SUFFIX(RenderbufferStorage, OES);
        GR_GL_GET_PROC_SUFFIX(GenRenderbuffers, OES);
        GR_GL_GET_PROC_SUFFIX(DeleteRenderbuffers, OES);
        GR_GL_GET_PROC_SUFFIX(FramebufferRenderbuffer, OES);
        GR_GL_GET_PROC_SUFFIX(BindRenderbuffer, OES);
        fboFound = true;
    }
    #endif

    #if GL_APPLE_framebuffer_multisample
    if (has_gl_extension_from_string("GL_APPLE_framebuffer_multisample",
                                     extensionString)) {
        GR_GL_GET_PROC_SUFFIX(ResolveMultisampleFramebuffer, APPLE);
    }
    #endif

    #if GL_IMG_multisampled_render_to_texture
    if (has_gl_extension_from_string(
                "GL_IMG_multisampled_render_to_texture", extensionString)) {
        GR_GL_GET_PROC_SUFFIX(FramebufferTexture2DMultisample, IMG);
    }
    #endif

    #if GL_OES_mapbuffer
    if (has_gl_extension_from_string("GL_OES_mapbuffer", extensionString)) {
        GR_GL_GET_PROC_SUFFIX(MapBuffer, OES);
        GR_GL_GET_PROC_SUFFIX(UnmapBuffer, OES);
    }
    #endif
#endif  // !GR_SUPPORT_GLDESKTOP

    if (!fboFound) {
        // we require some form of FBO
        GrAssert(!"No FBOs supported?");
    }
}

void GrGLInitializeGLInterface(GrGLInterface* glBindings) {
    Gr_bzero(glBindings, sizeof(GrGLInterface));

#if GR_SUPPORT_GLDESKTOP || GR_SUPPORT_GLES1
    // These entry points only exist on desktop GL implementations.
    GR_GL_GET_PROC_SYMBOL(Color4ub);
    GR_GL_GET_PROC_SYMBOL(ColorPointer);
    GR_GL_GET_PROC_SYMBOL(DisableClientState);
    GR_GL_GET_PROC_SYMBOL(EnableClientState);
    GR_GL_GET_PROC_SYMBOL(LoadMatrixf);
    GR_GL_GET_PROC_SYMBOL(MatrixMode);
    GR_GL_GET_PROC_SYMBOL(PointSize);
    GR_GL_GET_PROC_SYMBOL(ShadeModel);
    GR_GL_GET_PROC_SYMBOL(TexCoordPointer);
    GR_GL_GET_PROC_SYMBOL(TexEnvi);
    GR_GL_GET_PROC_SYMBOL(VertexPointer);
    GR_GL_GET_PROC(ClientActiveTexture);
#endif

    // The following gl entry points are part of GL 1.1, and will always be
    // exported as symbols.
    // Note that on windows, the wglGetProcAddress call will fail to retrieve
    // these entry points.
    GR_GL_GET_PROC_SYMBOL(BlendFunc);
    GR_GL_GET_PROC_SYMBOL(Clear);
    GR_GL_GET_PROC_SYMBOL(ClearColor);
    GR_GL_GET_PROC_SYMBOL(ClearStencil);
    GR_GL_GET_PROC_SYMBOL(ColorMask);
    GR_GL_GET_PROC_SYMBOL(CullFace);
    GR_GL_GET_PROC_SYMBOL(DeleteTextures);
    GR_GL_GET_PROC_SYMBOL(DepthMask);
    GR_GL_GET_PROC_SYMBOL(Disable);
    GR_GL_GET_PROC_SYMBOL(DrawArrays);
    GR_GL_GET_PROC_SYMBOL(DrawElements);
    GR_GL_GET_PROC_SYMBOL(Enable);
    GR_GL_GET_PROC_SYMBOL(FrontFace);
    GR_GL_GET_PROC_SYMBOL(GenTextures);
    GR_GL_GET_PROC_SYMBOL(GetError);
    GR_GL_GET_PROC_SYMBOL(GetIntegerv);
    GR_GL_GET_PROC_SYMBOL(GetString);
    GR_GL_GET_PROC_SYMBOL(LineWidth);
    GR_GL_GET_PROC_SYMBOL(PixelStorei);
    GR_GL_GET_PROC_SYMBOL(ReadPixels);
    GR_GL_GET_PROC_SYMBOL(Scissor);
    GR_GL_GET_PROC_SYMBOL(StencilFunc);
    GR_GL_GET_PROC_SYMBOL(StencilMask);
    GR_GL_GET_PROC_SYMBOL(StencilOp);
    GR_GL_GET_PROC_SYMBOL(TexImage2D);
    GR_GL_GET_PROC_SYMBOL(TexParameteri);
    GR_GL_GET_PROC_SYMBOL(TexSubImage2D);
    GR_GL_GET_PROC_SYMBOL(Viewport);

    // Capture the remaining entry points as gl extensions.
    GR_GL_GET_PROC(ActiveTexture);
    GR_GL_GET_PROC(AttachShader);
    GR_GL_GET_PROC(BindAttribLocation);
    GR_GL_GET_PROC(BindBuffer);
    GR_GL_GET_PROC(BindTexture);
    GR_GL_GET_PROC(BlendColor);
    GR_GL_GET_PROC(BufferData);
    GR_GL_GET_PROC(BufferSubData);
    GR_GL_GET_PROC(CompileShader);
    GR_GL_GET_PROC(CompressedTexImage2D);
    GR_GL_GET_PROC(CreateProgram);
    GR_GL_GET_PROC(CreateShader);
    GR_GL_GET_PROC(DeleteBuffers);
    GR_GL_GET_PROC(DeleteProgram);
    GR_GL_GET_PROC(DeleteShader);
    GR_GL_GET_PROC(DisableVertexAttribArray);
    GR_GL_GET_PROC(EnableVertexAttribArray);
    GR_GL_GET_PROC(GenBuffers);
    GR_GL_GET_PROC(GetBufferParameteriv);
    GR_GL_GET_PROC(GetProgramInfoLog);
    GR_GL_GET_PROC(GetProgramiv);
    GR_GL_GET_PROC(GetShaderInfoLog);
    GR_GL_GET_PROC(GetShaderiv);
    GR_GL_GET_PROC(GetUniformLocation);
    GR_GL_GET_PROC(LinkProgram);
    GR_GL_GET_PROC(ShaderSource);
    GR_GL_GET_PROC(StencilFuncSeparate);
    GR_GL_GET_PROC(StencilMaskSeparate);
    GR_GL_GET_PROC(StencilOpSeparate);
    GR_GL_GET_PROC(Uniform1fv);
    GR_GL_GET_PROC(Uniform1i);
    GR_GL_GET_PROC(Uniform4fv);
    GR_GL_GET_PROC(UniformMatrix3fv);
    GR_GL_GET_PROC(UseProgram);
    GR_GL_GET_PROC(VertexAttrib4fv);
    GR_GL_GET_PROC(VertexAttribPointer);

    InitializeGLInterfaceExtensions(glBindings);
}

}  // unnamed namespace

void GrGLSetGLInterface(GrGLInterface* gl_interface) {
    gGLInterface = gl_interface;
}

GrGLInterface* GrGLGetGLInterface() {
    return gGLInterface;
}

void GrGLSetDefaultGLInterface() {
    static GrGLInterface gDefaultInterface;
    static bool gDefaultInitialized = false;
    GrAssert(!gDefaultInitialized);

    if (!gDefaultInitialized) {
        GrGLInitializeGLInterface(&gDefaultInterface);
        GrGLSetGLInterface(&gDefaultInterface);
    }
}

bool has_gl_extension(const char* ext) {
    const char* glstr = reinterpret_cast<const char*>(
                GrGLGetGLInterface()->fGetString(GL_EXTENSIONS));

    return has_gl_extension_from_string(ext, glstr);
}

void gl_version(int* major, int* minor) {
    const char* v = reinterpret_cast<const char*>(
                GrGLGetGLInterface()->fGetString(GL_VERSION));
    gl_version_from_string(major, minor, v);
}