Home | History | Annotate | Download | only in gl
      1 
      2 /*
      3  * Copyright 2011 Google Inc.
      4  *
      5  * Use of this source code is governed by a BSD-style license that can be
      6  * found in the LICENSE file.
      7  */
      8 
      9 
     10 #ifndef GrGLInterface_DEFINED
     11 #define GrGLInterface_DEFINED
     12 
     13 #include "GrGLFunctions.h"
     14 #include "GrRefCnt.h"
     15 
     16 ////////////////////////////////////////////////////////////////////////////////
     17 
     18 /**
     19  * Classifies GL contexts (currently as Desktop vs. ES2). This is a bitfield.
     20  * A GrGLInterface (defined below) may support multiple bindings.
     21  */
     22 enum GrGLBinding {
     23     kNone_GrGLBinding = 0x0,
     24 
     25     kDesktop_GrGLBinding = 0x01,
     26     kES2_GrGLBinding = 0x02,
     27 
     28     // for iteration of GrGLBindings
     29     kFirstGrGLBinding = kDesktop_GrGLBinding,
     30     kLastGrGLBinding = kES2_GrGLBinding
     31 };
     32 
     33 ////////////////////////////////////////////////////////////////////////////////
     34 
     35 /**
     36  * Rather than depend on platform-specific GL headers and libraries, we require
     37  * the client to provide a struct of GL function pointers. This struct can be
     38  * specified per-GrContext as a parameter to GrContext::Create. If NULL is
     39  * passed to Create then the "default" GL interface is used. If the default is
     40  * also NULL GrContext creation will fail.
     41  *
     42  * The default interface is returned by GrGLDefaultInterface. This function's
     43  * implementation is platform-specific. Several have been provided, along with
     44  * an implementation that simply returns NULL. It is implementation-specific
     45  * whether the same GrGLInterface is returned or whether a new one is created
     46  * at each call. Some platforms may not be able to use a single GrGLInterface
     47  * because extension function ptrs vary across contexts. Note that GrGLInterface
     48  * is ref-counted. So if the same object is returned by multiple calls to
     49  * GrGLDefaultInterface, each should bump the ref count.
     50  *
     51  * By defining GR_GL_PER_GL_CALL_IFACE_CALLBACK to 1 the client can specify a
     52  * callback function that will be called prior to each GL function call. See
     53  * comments in GrGLConfig.h
     54  */
     55 
     56 struct GrGLInterface;
     57 
     58 const GrGLInterface* GrGLDefaultInterface();
     59 
     60 /**
     61  * Creates a GrGLInterface for a "native" GL context (e.g. WGL on windows,
     62  * GLX on linux, AGL on Mac). On platforms that have context-specific function
     63  * pointers for GL extensions (e.g. windows) the returned interface is only
     64  * valid for the context that was current at creation.
     65  */
     66 const GrGLInterface* GrGLCreateNativeInterface();
     67 
     68 #if SK_MESA
     69 /**
     70  * Creates a GrGLInterface for an OSMesa context.
     71  */
     72 const GrGLInterface* GrGLCreateMesaInterface();
     73 #endif
     74 
     75 #if SK_ANGLE
     76 /**
     77  * Creates a GrGLInterface for an ANGLE context.
     78  */
     79 const GrGLInterface* GrGLCreateANGLEInterface();
     80 #endif
     81 
     82 /**
     83  * Creates a null GrGLInterface that doesn't draw anything. Used for measuring
     84  * CPU overhead.
     85  */
     86 const GrGLInterface* GrGLCreateNullInterface();
     87 
     88 /**
     89  * Creates a debugging GrGLInterface that doesn't draw anything. Used for
     90  * finding memory leaks and invalid memory accesses.
     91  */
     92 const GrGLInterface* GrGLCreateDebugInterface();
     93 
     94 #if GR_GL_PER_GL_FUNC_CALLBACK
     95 typedef void (*GrGLInterfaceCallbackProc)(const GrGLInterface*);
     96 typedef intptr_t GrGLInterfaceCallbackData;
     97 #endif
     98 
     99 /*
    100  * GrContext uses the following interface to make all calls into OpenGL. When a
    101  * GrContext is created it is given a GrGLInterface. The interface's function
    102  * pointers must be valid for the OpenGL context associated with the GrContext.
    103  * On some platforms, such as Windows, function pointers for OpenGL extensions
    104  * may vary between OpenGL contexts. So the caller must be careful to use a
    105  * GrGLInterface initialized for the correct context. All functions that should
    106  * be available based on the OpenGL's version and extension string must be
    107  * non-NULL or GrContext creation will fail. This can be tested with the
    108  * validate() method when the OpenGL context has been made current.
    109  */
    110 struct GR_API GrGLInterface : public GrRefCnt {
    111 private:
    112     // simple wrapper class that exists only to initialize a pointer to NULL
    113     template <typename FNPTR_TYPE> class GLPtr {
    114     public:
    115         GLPtr() : fPtr(NULL) {}
    116         GLPtr operator =(FNPTR_TYPE ptr) { fPtr = ptr; return *this; }
    117         operator FNPTR_TYPE() const { return fPtr; }
    118     private:
    119         FNPTR_TYPE fPtr;
    120     };
    121 
    122     typedef GrRefCnt INHERITED;
    123 
    124 public:
    125     SK_DECLARE_INST_COUNT(GrGLInterface)
    126 
    127     GrGLInterface();
    128 
    129     // Validates that the GrGLInterface supports a binding. This means that
    130     // the GrGLinterface advertises the binding in fBindingsExported and all
    131     // the necessary function pointers have been initialized. The interface is
    132     // validated for the current OpenGL context.
    133     bool validate(GrGLBinding binding) const;
    134 
    135     // Indicator variable specifying the type of GL implementation
    136     // exported:  GLES2 and/or Desktop.
    137     GrGLBinding fBindingsExported;
    138 
    139     GLPtr<GrGLActiveTextureProc> fActiveTexture;
    140     GLPtr<GrGLAttachShaderProc> fAttachShader;
    141     GLPtr<GrGLBeginQueryProc> fBeginQuery;
    142     GLPtr<GrGLBindAttribLocationProc> fBindAttribLocation;
    143     GLPtr<GrGLBindBufferProc> fBindBuffer;
    144     GLPtr<GrGLBindFragDataLocationProc> fBindFragDataLocation;
    145     GLPtr<GrGLBindFragDataLocationIndexedProc> fBindFragDataLocationIndexed;
    146     GLPtr<GrGLBindFramebufferProc> fBindFramebuffer;
    147     GLPtr<GrGLBindRenderbufferProc> fBindRenderbuffer;
    148     GLPtr<GrGLBindTextureProc> fBindTexture;
    149     GLPtr<GrGLBlendColorProc> fBlendColor;
    150     GLPtr<GrGLBlendFuncProc> fBlendFunc;
    151     GLPtr<GrGLBlitFramebufferProc> fBlitFramebuffer;
    152     GLPtr<GrGLBufferDataProc> fBufferData;
    153     GLPtr<GrGLBufferSubDataProc> fBufferSubData;
    154     GLPtr<GrGLCheckFramebufferStatusProc> fCheckFramebufferStatus;
    155     GLPtr<GrGLClearProc> fClear;
    156     GLPtr<GrGLClearColorProc> fClearColor;
    157     GLPtr<GrGLClearStencilProc> fClearStencil;
    158     GLPtr<GrGLColorMaskProc> fColorMask;
    159     GLPtr<GrGLCompileShaderProc> fCompileShader;
    160     GLPtr<GrGLCompressedTexImage2DProc> fCompressedTexImage2D;
    161     GLPtr<GrGLCreateProgramProc> fCreateProgram;
    162     GLPtr<GrGLCreateShaderProc> fCreateShader;
    163     GLPtr<GrGLCullFaceProc> fCullFace;
    164     GLPtr<GrGLDeleteBuffersProc> fDeleteBuffers;
    165     GLPtr<GrGLDeleteFramebuffersProc> fDeleteFramebuffers;
    166     GLPtr<GrGLDeleteProgramProc> fDeleteProgram;
    167     GLPtr<GrGLDeleteQueriesProc> fDeleteQueries;
    168     GLPtr<GrGLDeleteRenderbuffersProc> fDeleteRenderbuffers;
    169     GLPtr<GrGLDeleteShaderProc> fDeleteShader;
    170     GLPtr<GrGLDeleteTexturesProc> fDeleteTextures;
    171     GLPtr<GrGLDepthMaskProc> fDepthMask;
    172     GLPtr<GrGLDisableProc> fDisable;
    173     GLPtr<GrGLDisableVertexAttribArrayProc> fDisableVertexAttribArray;
    174     GLPtr<GrGLDrawArraysProc> fDrawArrays;
    175     GLPtr<GrGLDrawBufferProc> fDrawBuffer;
    176     GLPtr<GrGLDrawBuffersProc> fDrawBuffers;
    177     GLPtr<GrGLDrawElementsProc> fDrawElements;
    178     GLPtr<GrGLEnableProc> fEnable;
    179     GLPtr<GrGLEnableVertexAttribArrayProc> fEnableVertexAttribArray;
    180     GLPtr<GrGLEndQueryProc> fEndQuery;
    181     GLPtr<GrGLFinishProc> fFinish;
    182     GLPtr<GrGLFlushProc> fFlush;
    183     GLPtr<GrGLFramebufferRenderbufferProc> fFramebufferRenderbuffer;
    184     GLPtr<GrGLFramebufferTexture2DProc> fFramebufferTexture2D;
    185     GLPtr<GrGLFrontFaceProc> fFrontFace;
    186     GLPtr<GrGLGenBuffersProc> fGenBuffers;
    187     GLPtr<GrGLGenFramebuffersProc> fGenFramebuffers;
    188     GLPtr<GrGLGenQueriesProc> fGenQueries;
    189     GLPtr<GrGLGenRenderbuffersProc> fGenRenderbuffers;
    190     GLPtr<GrGLGenTexturesProc> fGenTextures;
    191     GLPtr<GrGLGetBufferParameterivProc> fGetBufferParameteriv;
    192     GLPtr<GrGLGetErrorProc> fGetError;
    193     GLPtr<GrGLGetFramebufferAttachmentParameterivProc> fGetFramebufferAttachmentParameteriv;
    194     GLPtr<GrGLGetIntegervProc> fGetIntegerv;
    195     GLPtr<GrGLGetQueryObjecti64vProc> fGetQueryObjecti64v;
    196     GLPtr<GrGLGetQueryObjectivProc> fGetQueryObjectiv;
    197     GLPtr<GrGLGetQueryObjectui64vProc> fGetQueryObjectui64v;
    198     GLPtr<GrGLGetQueryObjectuivProc> fGetQueryObjectuiv;
    199     GLPtr<GrGLGetQueryivProc> fGetQueryiv;
    200     GLPtr<GrGLGetProgramInfoLogProc> fGetProgramInfoLog;
    201     GLPtr<GrGLGetProgramivProc> fGetProgramiv;
    202     GLPtr<GrGLGetRenderbufferParameterivProc> fGetRenderbufferParameteriv;
    203     GLPtr<GrGLGetShaderInfoLogProc> fGetShaderInfoLog;
    204     GLPtr<GrGLGetShaderivProc> fGetShaderiv;
    205     GLPtr<GrGLGetStringProc> fGetString;
    206     GLPtr<GrGLGetTexLevelParameterivProc> fGetTexLevelParameteriv;
    207     GLPtr<GrGLGetUniformLocationProc> fGetUniformLocation;
    208     GLPtr<GrGLLineWidthProc> fLineWidth;
    209     GLPtr<GrGLLinkProgramProc> fLinkProgram;
    210     GLPtr<GrGLMapBufferProc> fMapBuffer;
    211     GLPtr<GrGLPixelStoreiProc> fPixelStorei;
    212     GLPtr<GrGLQueryCounterProc> fQueryCounter;
    213     GLPtr<GrGLReadBufferProc> fReadBuffer;
    214     GLPtr<GrGLReadPixelsProc> fReadPixels;
    215     GLPtr<GrGLRenderbufferStorageProc> fRenderbufferStorage;
    216     GLPtr<GrGLRenderbufferStorageMultisampleProc> fRenderbufferStorageMultisample;
    217     GLPtr<GrGLRenderbufferStorageMultisampleCoverageProc> fRenderbufferStorageMultisampleCoverage;
    218     GLPtr<GrGLResolveMultisampleFramebufferProc> fResolveMultisampleFramebuffer;
    219     GLPtr<GrGLScissorProc> fScissor;
    220     GLPtr<GrGLShaderSourceProc> fShaderSource;
    221     GLPtr<GrGLStencilFuncProc> fStencilFunc;
    222     GLPtr<GrGLStencilFuncSeparateProc> fStencilFuncSeparate;
    223     GLPtr<GrGLStencilMaskProc> fStencilMask;
    224     GLPtr<GrGLStencilMaskSeparateProc> fStencilMaskSeparate;
    225     GLPtr<GrGLStencilOpProc> fStencilOp;
    226     GLPtr<GrGLStencilOpSeparateProc> fStencilOpSeparate;
    227     GLPtr<GrGLTexImage2DProc> fTexImage2D;
    228     GLPtr<GrGLTexParameteriProc> fTexParameteri;
    229     GLPtr<GrGLTexParameterivProc> fTexParameteriv;
    230     GLPtr<GrGLTexSubImage2DProc> fTexSubImage2D;
    231     GLPtr<GrGLTexStorage2DProc> fTexStorage2D;
    232     GLPtr<GrGLUniform1fProc> fUniform1f;
    233     GLPtr<GrGLUniform1iProc> fUniform1i;
    234     GLPtr<GrGLUniform1fvProc> fUniform1fv;
    235     GLPtr<GrGLUniform1ivProc> fUniform1iv;
    236     GLPtr<GrGLUniform2fProc> fUniform2f;
    237     GLPtr<GrGLUniform2iProc> fUniform2i;
    238     GLPtr<GrGLUniform2fvProc> fUniform2fv;
    239     GLPtr<GrGLUniform2ivProc> fUniform2iv;
    240     GLPtr<GrGLUniform3fProc> fUniform3f;
    241     GLPtr<GrGLUniform3iProc> fUniform3i;
    242     GLPtr<GrGLUniform3fvProc> fUniform3fv;
    243     GLPtr<GrGLUniform3ivProc> fUniform3iv;
    244     GLPtr<GrGLUniform4fProc> fUniform4f;
    245     GLPtr<GrGLUniform4iProc> fUniform4i;
    246     GLPtr<GrGLUniform4fvProc> fUniform4fv;
    247     GLPtr<GrGLUniform4ivProc> fUniform4iv;
    248     GLPtr<GrGLUniformMatrix2fvProc> fUniformMatrix2fv;
    249     GLPtr<GrGLUniformMatrix3fvProc> fUniformMatrix3fv;
    250     GLPtr<GrGLUniformMatrix4fvProc> fUniformMatrix4fv;
    251     GLPtr<GrGLUnmapBufferProc> fUnmapBuffer;
    252     GLPtr<GrGLUseProgramProc> fUseProgram;
    253     GLPtr<GrGLVertexAttrib4fvProc> fVertexAttrib4fv;
    254     GLPtr<GrGLVertexAttribPointerProc> fVertexAttribPointer;
    255     GLPtr<GrGLViewportProc> fViewport;
    256 
    257     // Experimental: Functions for GL_NV_path_rendering. These will be
    258     // alphabetized with the above functions once this is fully supported
    259     // (and functions we are unlikely to use will possibly be omitted).
    260     GLPtr<GrGLMatrixModeProc> fMatrixMode;
    261     GLPtr<GrGLLoadIdentityProc> fLoadIdentity;
    262     GLPtr<GrGLLoadMatrixfProc> fLoadMatrixf;
    263     GLPtr<GrGLPathCommandsProc> fPathCommands;
    264     GLPtr<GrGLPathCoordsProc> fPathCoords;
    265     GLPtr<GrGLPathSubCommandsProc> fPathSubCommands;
    266     GLPtr<GrGLPathSubCoordsProc> fPathSubCoords;
    267     GLPtr<GrGLPathStringProc> fPathString;
    268     GLPtr<GrGLPathGlyphsProc> fPathGlyphs;
    269     GLPtr<GrGLPathGlyphRangeProc> fPathGlyphRange;
    270     GLPtr<GrGLWeightPathsProc> fWeightPaths;
    271     GLPtr<GrGLCopyPathProc> fCopyPath;
    272     GLPtr<GrGLInterpolatePathsProc> fInterpolatePaths;
    273     GLPtr<GrGLTransformPathProc> fTransformPath;
    274     GLPtr<GrGLPathParameterivProc> fPathParameteriv;
    275     GLPtr<GrGLPathParameteriProc> fPathParameteri;
    276     GLPtr<GrGLPathParameterfvProc> fPathParameterfv;
    277     GLPtr<GrGLPathParameterfProc> fPathParameterf;
    278     GLPtr<GrGLPathDashArrayProc> fPathDashArray;
    279     GLPtr<GrGLGenPathsProc> fGenPaths;
    280     GLPtr<GrGLDeletePathsProc> fDeletePaths;
    281     GLPtr<GrGLIsPathProc> fIsPath;
    282     GLPtr<GrGLPathStencilFuncProc> fPathStencilFunc;
    283     GLPtr<GrGLPathStencilDepthOffsetProc> fPathStencilDepthOffset;
    284     GLPtr<GrGLStencilFillPathProc> fStencilFillPath;
    285     GLPtr<GrGLStencilStrokePathProc> fStencilStrokePath;
    286     GLPtr<GrGLStencilFillPathInstancedProc> fStencilFillPathInstanced;
    287     GLPtr<GrGLStencilStrokePathInstancedProc> fStencilStrokePathInstanced;
    288     GLPtr<GrGLPathCoverDepthFuncProc> fPathCoverDepthFunc;
    289     GLPtr<GrGLPathColorGenProc> fPathColorGen;
    290     GLPtr<GrGLPathTexGenProc> fPathTexGen;
    291     GLPtr<GrGLPathFogGenProc> fPathFogGen;
    292     GLPtr<GrGLCoverFillPathProc> fCoverFillPath;
    293     GLPtr<GrGLCoverStrokePathProc> fCoverStrokePath;
    294     GLPtr<GrGLCoverFillPathInstancedProc> fCoverFillPathInstanced;
    295     GLPtr<GrGLCoverStrokePathInstancedProc> fCoverStrokePathInstanced;
    296     GLPtr<GrGLGetPathParameterivProc> fGetPathParameteriv;
    297     GLPtr<GrGLGetPathParameterfvProc> fGetPathParameterfv;
    298     GLPtr<GrGLGetPathCommandsProc> fGetPathCommands;
    299     GLPtr<GrGLGetPathCoordsProc> fGetPathCoords;
    300     GLPtr<GrGLGetPathDashArrayProc> fGetPathDashArray;
    301     GLPtr<GrGLGetPathMetricsProc> fGetPathMetrics;
    302     GLPtr<GrGLGetPathMetricRangeProc> fGetPathMetricRange;
    303     GLPtr<GrGLGetPathSpacingProc> fGetPathSpacing;
    304     GLPtr<GrGLGetPathColorGenivProc> fGetPathColorGeniv;
    305     GLPtr<GrGLGetPathColorGenfvProc> fGetPathColorGenfv;
    306     GLPtr<GrGLGetPathTexGenivProc> fGetPathTexGeniv;
    307     GLPtr<GrGLGetPathTexGenfvProc> fGetPathTexGenfv;
    308     GLPtr<GrGLIsPointInFillPathProc> fIsPointInFillPath;
    309     GLPtr<GrGLIsPointInStrokePathProc> fIsPointInStrokePath;
    310     GLPtr<GrGLGetPathLengthProc> fGetPathLength;
    311     GLPtr<GrGLPointAlongPathProc> fPointAlongPath;
    312 
    313     // Per-GL func callback
    314 #if GR_GL_PER_GL_FUNC_CALLBACK
    315     GrGLInterfaceCallbackProc fCallback;
    316     GrGLInterfaceCallbackData fCallbackData;
    317 #endif
    318 
    319 };
    320 
    321 #endif
    322