Home | History | Annotate | Download | only in gl
      1 /*
      2  * Copyright 2011 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 GrGLInterface_DEFINED
      9 #define GrGLInterface_DEFINED
     10 
     11 #include "GrGLFunctions.h"
     12 #include "GrGLExtensions.h"
     13 #include "SkRefCnt.h"
     14 
     15 ////////////////////////////////////////////////////////////////////////////////
     16 
     17 /**
     18  * Rather than depend on platform-specific GL headers and libraries, we require
     19  * the client to provide a struct of GL function pointers. This struct can be
     20  * specified per-GrContext as a parameter to GrContext::Create. If NULL is
     21  * passed to Create then a "default" GL interface is created. If the default is
     22  * also NULL GrContext creation will fail.
     23  *
     24  * The default interface is returned by GrGLDefaultInterface. This function's
     25  * implementation is platform-specific. Several have been provided, along with
     26  * an implementation that simply returns NULL.
     27  *
     28  * By defining GR_GL_PER_GL_CALL_IFACE_CALLBACK to 1 the client can specify a
     29  * callback function that will be called prior to each GL function call. See
     30  * comments in GrGLConfig.h
     31  */
     32 
     33 struct GrGLInterface;
     34 
     35 const GrGLInterface* GrGLDefaultInterface();
     36 
     37 /**
     38  * Creates a GrGLInterface for a "native" GL context (e.g. WGL on windows,
     39  * GLX on linux, AGL on Mac). The interface is only valid for the GL context
     40  * that is current when the interface is created.
     41  */
     42 const GrGLInterface* GrGLCreateNativeInterface();
     43 
     44 #if SK_MESA
     45 /**
     46  * Creates a GrGLInterface for an OSMesa context.
     47  */
     48 const GrGLInterface* GrGLCreateMesaInterface();
     49 #endif
     50 
     51 #if SK_ANGLE
     52 /**
     53  * Creates a GrGLInterface for an ANGLE context.
     54  */
     55 const GrGLInterface* GrGLCreateANGLEInterface();
     56 #endif
     57 
     58 /**
     59  * Creates a null GrGLInterface that doesn't draw anything. Used for measuring
     60  * CPU overhead.
     61  */
     62 const SK_API GrGLInterface* GrGLCreateNullInterface();
     63 
     64 /**
     65  * Creates a debugging GrGLInterface that doesn't draw anything. Used for
     66  * finding memory leaks and invalid memory accesses.
     67  */
     68 const GrGLInterface* GrGLCreateDebugInterface();
     69 
     70 #if GR_GL_PER_GL_FUNC_CALLBACK
     71 typedef void (*GrGLInterfaceCallbackProc)(const GrGLInterface*);
     72 typedef intptr_t GrGLInterfaceCallbackData;
     73 #endif
     74 
     75 /** Function that returns a new interface identical to "interface" but without support for
     76     GL_NV_path_rendering. */
     77 const GrGLInterface* GrGLInterfaceRemoveNVPR(const GrGLInterface*);
     78 
     79 /** Function that returns a new interface identical to "interface" but with support for
     80     test version of GL_EXT_debug_marker. */
     81 const GrGLInterface* GrGLInterfaceAddTestDebugMarker(const GrGLInterface*,
     82                                                      GrGLInsertEventMarkerProc insertEventMarkerFn,
     83                                                      GrGLPushGroupMarkerProc pushGroupMarkerFn,
     84                                                      GrGLPopGroupMarkerProc popGroupMarkerFn);
     85 
     86 /**
     87  * GrContext uses the following interface to make all calls into OpenGL. When a
     88  * GrContext is created it is given a GrGLInterface. The interface's function
     89  * pointers must be valid for the OpenGL context associated with the GrContext.
     90  * On some platforms, such as Windows, function pointers for OpenGL extensions
     91  * may vary between OpenGL contexts. So the caller must be careful to use a
     92  * GrGLInterface initialized for the correct context. All functions that should
     93  * be available based on the OpenGL's version and extension string must be
     94  * non-NULL or GrContext creation will fail. This can be tested with the
     95  * validate() method when the OpenGL context has been made current.
     96  */
     97 struct SK_API GrGLInterface : public SkRefCnt {
     98 private:
     99     // simple wrapper class that exists only to initialize a pointer to NULL
    100     template <typename FNPTR_TYPE> class GLPtr {
    101     public:
    102         GLPtr() : fPtr(NULL) {}
    103         GLPtr operator=(FNPTR_TYPE ptr) { fPtr = ptr; return *this; }
    104         operator FNPTR_TYPE() const { return fPtr; }
    105     private:
    106         FNPTR_TYPE fPtr;
    107     };
    108 
    109     // This is a temporary workaround to keep Chromium's GrGLInterface factories compiling until
    110     // they're updated to use the Functions struct.
    111     template <typename FNPTR_TYPE> class GLPtrAlias {
    112     public:
    113         GLPtrAlias(GLPtr<FNPTR_TYPE>* base) : fBase(base) {}
    114         void operator=(FNPTR_TYPE ptr) { *fBase = ptr; }
    115     private:
    116         GLPtr<FNPTR_TYPE>* fBase;
    117     };
    118 
    119     typedef SkRefCnt INHERITED;
    120 
    121 public:
    122     SK_DECLARE_INST_COUNT(GrGLInterface)
    123 
    124     GrGLInterface();
    125 
    126     static GrGLInterface* NewClone(const GrGLInterface*);
    127 
    128     // Validates that the GrGLInterface supports its advertised standard. This means the necessary
    129     // function pointers have been initialized for both the GL version and any advertised
    130     // extensions.
    131     bool validate() const;
    132 
    133     // Indicates the type of GL implementation
    134     union {
    135         GrGLStandard fStandard;
    136         GrGLStandard fBindingsExported; // Legacy name, will be remove when Chromium is updated.
    137     };
    138 
    139     GrGLExtensions fExtensions;
    140 
    141     bool hasExtension(const char ext[]) const { return fExtensions.has(ext); }
    142 
    143     /**
    144      * The function pointers are in a struct so that we can have a compiler generated assignment
    145      * operator.
    146      */
    147     struct Functions {
    148         GLPtr<GrGLActiveTextureProc> fActiveTexture;
    149         GLPtr<GrGLAttachShaderProc> fAttachShader;
    150         GLPtr<GrGLBeginQueryProc> fBeginQuery;
    151         GLPtr<GrGLBindAttribLocationProc> fBindAttribLocation;
    152         GLPtr<GrGLBindBufferProc> fBindBuffer;
    153         GLPtr<GrGLBindFragDataLocationProc> fBindFragDataLocation;
    154         GLPtr<GrGLBindFragDataLocationIndexedProc> fBindFragDataLocationIndexed;
    155         GLPtr<GrGLBindFramebufferProc> fBindFramebuffer;
    156         GLPtr<GrGLBindRenderbufferProc> fBindRenderbuffer;
    157         GLPtr<GrGLBindTextureProc> fBindTexture;
    158         GLPtr<GrGLBindVertexArrayProc> fBindVertexArray;
    159         GLPtr<GrGLBlendColorProc> fBlendColor;
    160         GLPtr<GrGLBlendFuncProc> fBlendFunc;
    161         GLPtr<GrGLBlitFramebufferProc> fBlitFramebuffer;
    162         GLPtr<GrGLBufferDataProc> fBufferData;
    163         GLPtr<GrGLBufferSubDataProc> fBufferSubData;
    164         GLPtr<GrGLCheckFramebufferStatusProc> fCheckFramebufferStatus;
    165         GLPtr<GrGLClearProc> fClear;
    166         GLPtr<GrGLClearColorProc> fClearColor;
    167         GLPtr<GrGLClearStencilProc> fClearStencil;
    168         GLPtr<GrGLColorMaskProc> fColorMask;
    169         GLPtr<GrGLCompileShaderProc> fCompileShader;
    170         GLPtr<GrGLCompressedTexImage2DProc> fCompressedTexImage2D;
    171         GLPtr<GrGLCompressedTexSubImage2DProc> fCompressedTexSubImage2D;
    172         GLPtr<GrGLCopyTexSubImage2DProc> fCopyTexSubImage2D;
    173         GLPtr<GrGLCopyTextureCHROMIUMProc> fCopyTextureCHROMIUM;
    174         GLPtr<GrGLCreateProgramProc> fCreateProgram;
    175         GLPtr<GrGLCreateShaderProc> fCreateShader;
    176         GLPtr<GrGLCullFaceProc> fCullFace;
    177         GLPtr<GrGLDeleteBuffersProc> fDeleteBuffers;
    178         GLPtr<GrGLDeleteFramebuffersProc> fDeleteFramebuffers;
    179         GLPtr<GrGLDeleteProgramProc> fDeleteProgram;
    180         GLPtr<GrGLDeleteQueriesProc> fDeleteQueries;
    181         GLPtr<GrGLDeleteRenderbuffersProc> fDeleteRenderbuffers;
    182         GLPtr<GrGLDeleteShaderProc> fDeleteShader;
    183         GLPtr<GrGLDeleteTexturesProc> fDeleteTextures;
    184         GLPtr<GrGLDeleteVertexArraysProc> fDeleteVertexArrays;
    185         GLPtr<GrGLDepthMaskProc> fDepthMask;
    186         GLPtr<GrGLDisableProc> fDisable;
    187         GLPtr<GrGLDisableVertexAttribArrayProc> fDisableVertexAttribArray;
    188         GLPtr<GrGLDrawArraysProc> fDrawArrays;
    189         GLPtr<GrGLDrawBufferProc> fDrawBuffer;
    190         GLPtr<GrGLDrawBuffersProc> fDrawBuffers;
    191         GLPtr<GrGLDrawElementsProc> fDrawElements;
    192         GLPtr<GrGLEnableProc> fEnable;
    193         GLPtr<GrGLEnableVertexAttribArrayProc> fEnableVertexAttribArray;
    194         GLPtr<GrGLEndQueryProc> fEndQuery;
    195         GLPtr<GrGLFinishProc> fFinish;
    196         GLPtr<GrGLFlushProc> fFlush;
    197         GLPtr<GrGLFlushMappedBufferRangeProc> fFlushMappedBufferRange;
    198         GLPtr<GrGLFramebufferRenderbufferProc> fFramebufferRenderbuffer;
    199         GLPtr<GrGLFramebufferTexture2DProc> fFramebufferTexture2D;
    200         GLPtr<GrGLFramebufferTexture2DMultisampleProc> fFramebufferTexture2DMultisample;
    201         GLPtr<GrGLFrontFaceProc> fFrontFace;
    202         GLPtr<GrGLGenBuffersProc> fGenBuffers;
    203         GLPtr<GrGLGenFramebuffersProc> fGenFramebuffers;
    204         GLPtr<GrGLGenerateMipmapProc> fGenerateMipmap;
    205         GLPtr<GrGLGenQueriesProc> fGenQueries;
    206         GLPtr<GrGLGenRenderbuffersProc> fGenRenderbuffers;
    207         GLPtr<GrGLGenTexturesProc> fGenTextures;
    208         GLPtr<GrGLGenVertexArraysProc> fGenVertexArrays;
    209         GLPtr<GrGLGetBufferParameterivProc> fGetBufferParameteriv;
    210         GLPtr<GrGLGetErrorProc> fGetError;
    211         GLPtr<GrGLGetFramebufferAttachmentParameterivProc> fGetFramebufferAttachmentParameteriv;
    212         GLPtr<GrGLGetIntegervProc> fGetIntegerv;
    213         GLPtr<GrGLGetQueryObjecti64vProc> fGetQueryObjecti64v;
    214         GLPtr<GrGLGetQueryObjectivProc> fGetQueryObjectiv;
    215         GLPtr<GrGLGetQueryObjectui64vProc> fGetQueryObjectui64v;
    216         GLPtr<GrGLGetQueryObjectuivProc> fGetQueryObjectuiv;
    217         GLPtr<GrGLGetQueryivProc> fGetQueryiv;
    218         GLPtr<GrGLGetProgramInfoLogProc> fGetProgramInfoLog;
    219         GLPtr<GrGLGetProgramivProc> fGetProgramiv;
    220         GLPtr<GrGLGetRenderbufferParameterivProc> fGetRenderbufferParameteriv;
    221         GLPtr<GrGLGetShaderInfoLogProc> fGetShaderInfoLog;
    222         GLPtr<GrGLGetShaderivProc> fGetShaderiv;
    223         GLPtr<GrGLGetStringProc> fGetString;
    224         GLPtr<GrGLGetStringiProc> fGetStringi;
    225         GLPtr<GrGLGetTexLevelParameterivProc> fGetTexLevelParameteriv;
    226         GLPtr<GrGLGetUniformLocationProc> fGetUniformLocation;
    227         GLPtr<GrGLInsertEventMarkerProc> fInsertEventMarker;
    228         GLPtr<GrGLInvalidateBufferDataProc> fInvalidateBufferData;
    229         GLPtr<GrGLInvalidateBufferSubDataProc> fInvalidateBufferSubData;
    230         GLPtr<GrGLInvalidateFramebufferProc> fInvalidateFramebuffer;
    231         GLPtr<GrGLInvalidateSubFramebufferProc> fInvalidateSubFramebuffer;
    232         GLPtr<GrGLInvalidateTexImageProc> fInvalidateTexImage;
    233         GLPtr<GrGLInvalidateTexSubImageProc> fInvalidateTexSubImage;
    234         GLPtr<GrGLLineWidthProc> fLineWidth;
    235         GLPtr<GrGLLinkProgramProc> fLinkProgram;
    236         GLPtr<GrGLMapBufferProc> fMapBuffer;
    237         GLPtr<GrGLMapBufferRangeProc> fMapBufferRange;
    238         GLPtr<GrGLMapBufferSubDataProc> fMapBufferSubData;
    239         GLPtr<GrGLMapTexSubImage2DProc> fMapTexSubImage2D;
    240         GLPtr<GrGLMatrixLoadfProc> fMatrixLoadf;
    241         GLPtr<GrGLMatrixLoadIdentityProc> fMatrixLoadIdentity;
    242         GLPtr<GrGLPixelStoreiProc> fPixelStorei;
    243         GLPtr<GrGLPopGroupMarkerProc> fPopGroupMarker;
    244         GLPtr<GrGLPushGroupMarkerProc> fPushGroupMarker;
    245         GLPtr<GrGLQueryCounterProc> fQueryCounter;
    246         GLPtr<GrGLReadBufferProc> fReadBuffer;
    247         GLPtr<GrGLReadPixelsProc> fReadPixels;
    248         GLPtr<GrGLRenderbufferStorageProc> fRenderbufferStorage;
    249 
    250         //  On OpenGL ES there are multiple incompatible extensions that add support for MSAA
    251         //  and ES3 adds MSAA support to the standard. On an ES3 driver we may still use the
    252         //  older extensions for performance reasons or due to ES3 driver bugs. We want the function
    253         //  that creates the GrGLInterface to provide all available functions and internally
    254         //  we will select among them. They all have a method called glRenderbufferStorageMultisample*.
    255         //  So we have separate function pointers for GL_IMG/EXT_multisampled_to_texture,
    256         //  GL_CHROMIUM/ANGLE_framebuffer_multisample/ES3, and GL_APPLE_framebuffer_multisample
    257         //  variations.
    258         //
    259         //  If a driver supports multiple GL_ARB_framebuffer_multisample-style extensions then we will
    260         //  assume the function pointers for the standard (or equivalent GL_ARB) version have
    261         //  been preferred over GL_EXT, GL_CHROMIUM, or GL_ANGLE variations that have reduced
    262         //  functionality.
    263 
    264         //  GL_EXT_multisampled_render_to_texture (preferred) or GL_IMG_multisampled_render_to_texture
    265         GLPtr<GrGLRenderbufferStorageMultisampleProc> fRenderbufferStorageMultisampleES2EXT;
    266         //  GL_APPLE_framebuffer_multisample
    267         GLPtr<GrGLRenderbufferStorageMultisampleProc> fRenderbufferStorageMultisampleES2APPLE;
    268 
    269         //  This is used to store the pointer for GL_ARB/EXT/ANGLE/CHROMIUM_framebuffer_multisample or
    270         //  the standard function in ES3+ or GL 3.0+.
    271         GLPtr<GrGLRenderbufferStorageMultisampleProc> fRenderbufferStorageMultisample;
    272 
    273         // Pointer to BindUniformLocationCHROMIUM from the GL_CHROMIUM_bind_uniform_location extension.
    274         GLPtr<GrGLBindUniformLocation> fBindUniformLocation;
    275 
    276         GLPtr<GrGLResolveMultisampleFramebufferProc> fResolveMultisampleFramebuffer;
    277         GLPtr<GrGLScissorProc> fScissor;
    278         GLPtr<GrGLShaderSourceProc> fShaderSource;
    279         GLPtr<GrGLStencilFuncProc> fStencilFunc;
    280         GLPtr<GrGLStencilFuncSeparateProc> fStencilFuncSeparate;
    281         GLPtr<GrGLStencilMaskProc> fStencilMask;
    282         GLPtr<GrGLStencilMaskSeparateProc> fStencilMaskSeparate;
    283         GLPtr<GrGLStencilOpProc> fStencilOp;
    284         GLPtr<GrGLStencilOpSeparateProc> fStencilOpSeparate;
    285         GLPtr<GrGLTexImage2DProc> fTexImage2D;
    286         GLPtr<GrGLTexParameteriProc> fTexParameteri;
    287         GLPtr<GrGLTexParameterivProc> fTexParameteriv;
    288         GLPtr<GrGLTexSubImage2DProc> fTexSubImage2D;
    289         GLPtr<GrGLTexStorage2DProc> fTexStorage2D;
    290         GLPtr<GrGLDiscardFramebufferProc> fDiscardFramebuffer;
    291         GLPtr<GrGLUniform1fProc> fUniform1f;
    292         GLPtr<GrGLUniform1iProc> fUniform1i;
    293         GLPtr<GrGLUniform1fvProc> fUniform1fv;
    294         GLPtr<GrGLUniform1ivProc> fUniform1iv;
    295         GLPtr<GrGLUniform2fProc> fUniform2f;
    296         GLPtr<GrGLUniform2iProc> fUniform2i;
    297         GLPtr<GrGLUniform2fvProc> fUniform2fv;
    298         GLPtr<GrGLUniform2ivProc> fUniform2iv;
    299         GLPtr<GrGLUniform3fProc> fUniform3f;
    300         GLPtr<GrGLUniform3iProc> fUniform3i;
    301         GLPtr<GrGLUniform3fvProc> fUniform3fv;
    302         GLPtr<GrGLUniform3ivProc> fUniform3iv;
    303         GLPtr<GrGLUniform4fProc> fUniform4f;
    304         GLPtr<GrGLUniform4iProc> fUniform4i;
    305         GLPtr<GrGLUniform4fvProc> fUniform4fv;
    306         GLPtr<GrGLUniform4ivProc> fUniform4iv;
    307         GLPtr<GrGLUniformMatrix2fvProc> fUniformMatrix2fv;
    308         GLPtr<GrGLUniformMatrix3fvProc> fUniformMatrix3fv;
    309         GLPtr<GrGLUniformMatrix4fvProc> fUniformMatrix4fv;
    310         GLPtr<GrGLUnmapBufferProc> fUnmapBuffer;
    311         GLPtr<GrGLUnmapBufferSubDataProc> fUnmapBufferSubData;
    312         GLPtr<GrGLUnmapTexSubImage2DProc> fUnmapTexSubImage2D;
    313         GLPtr<GrGLUseProgramProc> fUseProgram;
    314         GLPtr<GrGLVertexAttrib4fvProc> fVertexAttrib4fv;
    315         GLPtr<GrGLVertexAttribPointerProc> fVertexAttribPointer;
    316         GLPtr<GrGLViewportProc> fViewport;
    317 
    318         // Experimental: Functions for GL_NV_path_rendering. These will be
    319         // alphabetized with the above functions once this is fully supported
    320         // (and functions we are unlikely to use will possibly be omitted).
    321         GLPtr<GrGLGetProgramResourceLocationProc> fGetProgramResourceLocation;
    322         GLPtr<GrGLPathCommandsProc> fPathCommands;
    323         GLPtr<GrGLPathCoordsProc> fPathCoords;
    324         GLPtr<GrGLPathParameteriProc> fPathParameteri;
    325         GLPtr<GrGLPathParameterfProc> fPathParameterf;
    326         GLPtr<GrGLGenPathsProc> fGenPaths;
    327         GLPtr<GrGLDeletePathsProc> fDeletePaths;
    328         GLPtr<GrGLIsPathProc> fIsPath;
    329         GLPtr<GrGLPathStencilFuncProc> fPathStencilFunc;
    330         GLPtr<GrGLStencilFillPathProc> fStencilFillPath;
    331         GLPtr<GrGLStencilStrokePathProc> fStencilStrokePath;
    332         GLPtr<GrGLStencilFillPathInstancedProc> fStencilFillPathInstanced;
    333         GLPtr<GrGLStencilStrokePathInstancedProc> fStencilStrokePathInstanced;
    334         GLPtr<GrGLPathTexGenProc> fPathTexGen;
    335         GLPtr<GrGLCoverFillPathProc> fCoverFillPath;
    336         GLPtr<GrGLCoverStrokePathProc> fCoverStrokePath;
    337         GLPtr<GrGLCoverFillPathInstancedProc> fCoverFillPathInstanced;
    338         GLPtr<GrGLCoverStrokePathInstancedProc> fCoverStrokePathInstanced;
    339         // NV_path_rendering v1.2
    340         GLPtr<GrGLStencilThenCoverFillPathProc> fStencilThenCoverFillPath;
    341         GLPtr<GrGLStencilThenCoverStrokePathProc> fStencilThenCoverStrokePath;
    342         GLPtr<GrGLStencilThenCoverFillPathInstancedProc> fStencilThenCoverFillPathInstanced;
    343         GLPtr<GrGLStencilThenCoverStrokePathInstancedProc> fStencilThenCoverStrokePathInstanced;
    344         // NV_path_rendering v1.3
    345         GLPtr<GrGLProgramPathFragmentInputGenProc> fProgramPathFragmentInputGen;
    346         GLPtr<GrGLPathMemoryGlyphIndexArrayProc> fPathMemoryGlyphIndexArray;
    347     } fFunctions;
    348 
    349     // Per-GL func callback
    350 #if GR_GL_PER_GL_FUNC_CALLBACK
    351     GrGLInterfaceCallbackProc fCallback;
    352     GrGLInterfaceCallbackData fCallbackData;
    353 #endif
    354 
    355     // This exists for internal testing.
    356     virtual void abandon() const {}
    357 };
    358 
    359 #endif
    360