Home | History | Annotate | Download | only in unix
      1 /*
      2     Copyright 2011 Google Inc.
      3 
      4     Licensed under the Apache License, Version 2.0 (the "License");
      5     you may not use this file except in compliance with the License.
      6     You may obtain a copy of the License at
      7 
      8          http://www.apache.org/licenses/LICENSE-2.0
      9 
     10     Unless required by applicable law or agreed to in writing, software
     11     distributed under the License is distributed on an "AS IS" BASIS,
     12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13     See the License for the specific language governing permissions and
     14     limitations under the License.
     15  */
     16 
     17 #include "GrGLInterface.h"
     18 
     19 #include <GL/glx.h>
     20 #include <GL/gl.h>
     21 #include <GL/glext.h>
     22 #include <GL/glu.h>
     23 
     24 #define GR_GL_GET_PROC(F) gDefaultInterface.f ## F = (GrGL ## F ## Proc) \
     25         glXGetProcAddress(reinterpret_cast<const GLubyte*>("gl" #F));
     26 #define GR_GL_GET_PROC_SUFFIX(F, S) gDefaultInterface.f ## F = (GrGL ## F ## Proc) \
     27         glXGetProcAddress(reinterpret_cast<const GLubyte*>("gl" #F #S));
     28 
     29 void GrGLSetDefaultGLInterface() {
     30     static GrGLInterface gDefaultInterface;
     31     static bool gDefaultInterfaceInit;
     32     if (!gDefaultInterfaceInit && NULL != glXGetCurrentContext()) {
     33         int major, minor;
     34         const char* versionString = (const char*) glGetString(GL_VERSION);
     35         const char* extString = (const char*) glGetString(GL_EXTENSIONS);
     36         gl_version_from_string(&major, &minor, versionString);
     37 
     38         if (major == 1 && minor < 5) {
     39             // We must have array and element_array buffer objects.
     40             return;
     41         }
     42 
     43         gDefaultInterface.fActiveTexture = glActiveTexture;
     44         GR_GL_GET_PROC(AttachShader);
     45         GR_GL_GET_PROC(BindAttribLocation);
     46         GR_GL_GET_PROC(BindBuffer);
     47         gDefaultInterface.fBindTexture = glBindTexture;
     48         gDefaultInterface.fBlendColor = glBlendColor;
     49         gDefaultInterface.fBlendFunc = glBlendFunc;
     50         GR_GL_GET_PROC(BufferData);
     51         GR_GL_GET_PROC(BufferSubData);
     52         gDefaultInterface.fClear = glClear;
     53         gDefaultInterface.fClearColor = glClearColor;
     54         gDefaultInterface.fClearStencil = glClearStencil;
     55         gDefaultInterface.fClientActiveTexture = glClientActiveTexture;
     56         gDefaultInterface.fColorMask = glColorMask;
     57         gDefaultInterface.fColorPointer = glColorPointer;
     58         gDefaultInterface.fColor4ub = glColor4ub;
     59         GR_GL_GET_PROC(CompileShader);
     60         gDefaultInterface.fCompressedTexImage2D = glCompressedTexImage2D;
     61         GR_GL_GET_PROC(CreateProgram);
     62         GR_GL_GET_PROC(CreateShader);
     63         gDefaultInterface.fCullFace = glCullFace;
     64         GR_GL_GET_PROC(DeleteBuffers);
     65         GR_GL_GET_PROC(DeleteProgram);
     66         GR_GL_GET_PROC(DeleteShader);
     67         gDefaultInterface.fDeleteTextures = glDeleteTextures;
     68         gDefaultInterface.fDepthMask = glDepthMask;
     69         gDefaultInterface.fDisable = glDisable;
     70         gDefaultInterface.fDisableClientState = glDisableClientState;
     71         GR_GL_GET_PROC(DisableVertexAttribArray);
     72         gDefaultInterface.fDrawArrays = glDrawArrays;
     73         gDefaultInterface.fDrawElements = glDrawElements;
     74         gDefaultInterface.fEnable = glEnable;
     75         gDefaultInterface.fEnableClientState = glEnableClientState;
     76         GR_GL_GET_PROC(EnableVertexAttribArray);
     77         gDefaultInterface.fFrontFace = glFrontFace;
     78         GR_GL_GET_PROC(GenBuffers);
     79         GR_GL_GET_PROC(GetBufferParameteriv);
     80         gDefaultInterface.fGetError = glGetError;
     81         gDefaultInterface.fGetIntegerv = glGetIntegerv;
     82         GR_GL_GET_PROC(GetProgramInfoLog);
     83         GR_GL_GET_PROC(GetProgramiv);
     84         GR_GL_GET_PROC(GetShaderInfoLog);
     85         GR_GL_GET_PROC(GetShaderiv);
     86         gDefaultInterface.fGetString = glGetString;
     87         gDefaultInterface.fGenTextures = glGenTextures;
     88         GR_GL_GET_PROC(GetUniformLocation);
     89         gDefaultInterface.fLineWidth = glLineWidth;
     90         GR_GL_GET_PROC(LinkProgram);
     91         gDefaultInterface.fLoadMatrixf = glLoadMatrixf;
     92         GR_GL_GET_PROC(MapBuffer);
     93         gDefaultInterface.fMatrixMode = glMatrixMode;
     94         gDefaultInterface.fPointSize = glPointSize;
     95         gDefaultInterface.fPixelStorei = glPixelStorei;
     96         gDefaultInterface.fReadPixels = glReadPixels;
     97         gDefaultInterface.fScissor = glScissor;
     98         gDefaultInterface.fShadeModel = glShadeModel;
     99         GR_GL_GET_PROC(ShaderSource);
    100         gDefaultInterface.fStencilFunc = glStencilFunc;
    101         GR_GL_GET_PROC(StencilFuncSeparate);
    102         gDefaultInterface.fStencilMask = glStencilMask;
    103         GR_GL_GET_PROC(StencilMaskSeparate);
    104         gDefaultInterface.fStencilOp = glStencilOp;
    105         GR_GL_GET_PROC(StencilOpSeparate);
    106         gDefaultInterface.fTexCoordPointer = glTexCoordPointer;
    107         gDefaultInterface.fTexEnvi = glTexEnvi;
    108         gDefaultInterface.fTexImage2D = glTexImage2D;
    109         gDefaultInterface.fTexParameteri = glTexParameteri;
    110         gDefaultInterface.fTexSubImage2D = glTexSubImage2D;
    111         GR_GL_GET_PROC(Uniform1f);
    112         GR_GL_GET_PROC(Uniform1i);
    113         GR_GL_GET_PROC(Uniform1fv);
    114         GR_GL_GET_PROC(Uniform1iv);
    115         GR_GL_GET_PROC(Uniform2f);
    116         GR_GL_GET_PROC(Uniform2i);
    117         GR_GL_GET_PROC(Uniform2fv);
    118         GR_GL_GET_PROC(Uniform2iv);
    119         GR_GL_GET_PROC(Uniform3f);
    120         GR_GL_GET_PROC(Uniform3i);
    121         GR_GL_GET_PROC(Uniform3fv);
    122         GR_GL_GET_PROC(Uniform3iv);
    123         GR_GL_GET_PROC(Uniform4f);
    124         GR_GL_GET_PROC(Uniform4i);
    125         GR_GL_GET_PROC(Uniform4fv);
    126         GR_GL_GET_PROC(Uniform4iv);
    127         GR_GL_GET_PROC(UniformMatrix2fv);
    128         GR_GL_GET_PROC(UniformMatrix3fv);
    129         GR_GL_GET_PROC(UniformMatrix4fv);
    130         GR_GL_GET_PROC(UnmapBuffer);
    131         GR_GL_GET_PROC(UseProgram);
    132         GR_GL_GET_PROC(VertexAttrib4fv);
    133         GR_GL_GET_PROC(VertexAttribPointer);
    134         gDefaultInterface.fVertexPointer = glVertexPointer;
    135         gDefaultInterface.fViewport = glViewport;
    136         GR_GL_GET_PROC(BindFragDataLocationIndexed);
    137 
    138         // First look for GL3.0 FBO or GL_ARB_framebuffer_object (same since
    139         // GL_ARB_framebuffer_object doesn't use ARB suffix.)
    140         if (major >= 3 || has_gl_extension_from_string(
    141                 "GL_ARB_framebuffer_object", extString)) {
    142             GR_GL_GET_PROC(GenFramebuffers);
    143             GR_GL_GET_PROC(BindFramebuffer);
    144             GR_GL_GET_PROC(FramebufferTexture2D);
    145             GR_GL_GET_PROC(CheckFramebufferStatus);
    146             GR_GL_GET_PROC(DeleteFramebuffers);
    147             GR_GL_GET_PROC(RenderbufferStorage);
    148             GR_GL_GET_PROC(GenRenderbuffers);
    149             GR_GL_GET_PROC(DeleteRenderbuffers);
    150             GR_GL_GET_PROC(FramebufferRenderbuffer);
    151             GR_GL_GET_PROC(BindRenderbuffer);
    152             GR_GL_GET_PROC(RenderbufferStorageMultisample);
    153             GR_GL_GET_PROC(BlitFramebuffer);
    154         } else if (has_gl_extension_from_string("GL_EXT_framebuffer_object",
    155                                                 extString)) {
    156             GR_GL_GET_PROC_SUFFIX(GenFramebuffers, EXT);
    157             GR_GL_GET_PROC_SUFFIX(BindFramebuffer, EXT);
    158             GR_GL_GET_PROC_SUFFIX(FramebufferTexture2D, EXT);
    159             GR_GL_GET_PROC_SUFFIX(CheckFramebufferStatus, EXT);
    160             GR_GL_GET_PROC_SUFFIX(DeleteFramebuffers, EXT);
    161             GR_GL_GET_PROC_SUFFIX(RenderbufferStorage, EXT);
    162             GR_GL_GET_PROC_SUFFIX(GenRenderbuffers, EXT);
    163             GR_GL_GET_PROC_SUFFIX(DeleteRenderbuffers, EXT);
    164             GR_GL_GET_PROC_SUFFIX(FramebufferRenderbuffer, EXT);
    165             GR_GL_GET_PROC_SUFFIX(BindRenderbuffer, EXT);
    166             if (has_gl_extension_from_string("GL_EXT_framebuffer_multisample",
    167                                              extString)) {
    168                 GR_GL_GET_PROC_SUFFIX(RenderbufferStorageMultisample, EXT);
    169             }
    170             if (has_gl_extension_from_string("GL_EXT_framebuffer_blit",
    171                                              extString)) {
    172                 GR_GL_GET_PROC_SUFFIX(BlitFramebuffer, EXT);
    173             }
    174         } else {
    175             // we must have FBOs
    176             return;
    177         }
    178         gDefaultInterface.fBindingsExported = kDesktop_GrGLBinding;
    179 
    180         gDefaultInterfaceInit = true;
    181     }
    182     if (gDefaultInterfaceInit)
    183         GrGLSetGLInterface(&gDefaultInterface);
    184 }
    185