/* * Copyright 2011 Google Inc. * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #ifndef GrGLInterface_DEFINED #define GrGLInterface_DEFINED #include "GrGLFunctions.h" #include "GrGLExtensions.h" #include "SkRefCnt.h" //////////////////////////////////////////////////////////////////////////////// /** * Rather than depend on platform-specific GL headers and libraries, we require * the client to provide a struct of GL function pointers. This struct can be * specified per-GrContext as a parameter to GrContext::Create. If NULL is * passed to Create then a "default" GL interface is created. If the default is * also NULL GrContext creation will fail. * * The default interface is returned by GrGLDefaultInterface. This function's * implementation is platform-specific. Several have been provided, along with * an implementation that simply returns NULL. * * By defining GR_GL_PER_GL_CALL_IFACE_CALLBACK to 1 the client can specify a * callback function that will be called prior to each GL function call. See * comments in GrGLConfig.h */ typedef void(*GrGLFuncPtr)(); struct GrGLInterface; const GrGLInterface* GrGLDefaultInterface(); /** * Creates a GrGLInterface for a "native" GL context (e.g. WGL on windows, * GLX on linux, AGL on Mac). The interface is only valid for the GL context * that is current when the interface is created. */ const GrGLInterface* GrGLCreateNativeInterface(); #if SK_MESA /** * Creates a GrGLInterface for an OSMesa context. */ const GrGLInterface* GrGLCreateMesaInterface(); #endif #if SK_ANGLE /** * Creates a GrGLInterface for an ANGLE context. */ const GrGLInterface* GrGLCreateANGLEInterface(); #endif #if SK_COMMAND_BUFFER /** * Creates a GrGLInterface for a Command Buffer context. */ const GrGLInterface* GrGLCreateCommandBufferInterface(); #endif /** * Creates a null GrGLInterface that doesn't draw anything. Used for measuring * CPU overhead. */ const SK_API GrGLInterface* GrGLCreateNullInterface(); /** * Creates a debugging GrGLInterface that doesn't draw anything. Used for * finding memory leaks and invalid memory accesses. */ const GrGLInterface* GrGLCreateDebugInterface(); #if GR_GL_PER_GL_FUNC_CALLBACK typedef void (*GrGLInterfaceCallbackProc)(const GrGLInterface*); typedef intptr_t GrGLInterfaceCallbackData; #endif /** Function that returns a new interface identical to "interface" but without support for GL_NV_path_rendering. */ const GrGLInterface* GrGLInterfaceRemoveNVPR(const GrGLInterface*); /** Function that returns a new interface identical to "interface" but with support for test version of GL_EXT_debug_marker. */ const GrGLInterface* GrGLInterfaceAddTestDebugMarker(const GrGLInterface*, GrGLInsertEventMarkerProc insertEventMarkerFn, GrGLPushGroupMarkerProc pushGroupMarkerFn, GrGLPopGroupMarkerProc popGroupMarkerFn); /** * GrContext uses the following interface to make all calls into OpenGL. When a * GrContext is created it is given a GrGLInterface. The interface's function * pointers must be valid for the OpenGL context associated with the GrContext. * On some platforms, such as Windows, function pointers for OpenGL extensions * may vary between OpenGL contexts. So the caller must be careful to use a * GrGLInterface initialized for the correct context. All functions that should * be available based on the OpenGL's version and extension string must be * non-NULL or GrContext creation will fail. This can be tested with the * validate() method when the OpenGL context has been made current. */ struct SK_API GrGLInterface : public SkRefCnt { private: // simple wrapper class that exists only to initialize a pointer to NULL template class GLPtr { public: GLPtr() : fPtr(NULL) {} GLPtr operator=(FNPTR_TYPE ptr) { fPtr = ptr; return *this; } operator FNPTR_TYPE() const { return fPtr; } private: FNPTR_TYPE fPtr; }; // This is a temporary workaround to keep Chromium's GrGLInterface factories compiling until // they're updated to use the Functions struct. template class GLPtrAlias { public: GLPtrAlias(GLPtr* base) : fBase(base) {} void operator=(FNPTR_TYPE ptr) { *fBase = ptr; } private: GLPtr* fBase; }; typedef SkRefCnt INHERITED; public: GrGLInterface(); static GrGLInterface* NewClone(const GrGLInterface*); // Validates that the GrGLInterface supports its advertised standard. This means the necessary // function pointers have been initialized for both the GL version and any advertised // extensions. bool validate() const; // Indicates the type of GL implementation union { GrGLStandard fStandard; GrGLStandard fBindingsExported; // Legacy name, will be remove when Chromium is updated. }; GrGLExtensions fExtensions; bool hasExtension(const char ext[]) const { return fExtensions.has(ext); } /** * The function pointers are in a struct so that we can have a compiler generated assignment * operator. */ struct Functions { GLPtr fActiveTexture; GLPtr fAttachShader; GLPtr fBeginQuery; GLPtr fBindAttribLocation; GLPtr fBindBuffer; GLPtr fBindFragDataLocation; GLPtr fBindFragDataLocationIndexed; GLPtr fBindFramebuffer; GLPtr fBindRenderbuffer; GLPtr fBindTexture; GLPtr fBindVertexArray; GLPtr fBlendBarrier; GLPtr fBlendColor; GLPtr fBlendEquation; GLPtr fBlendFunc; GLPtr fBlitFramebuffer; GLPtr fBufferData; GLPtr fBufferSubData; GLPtr fCheckFramebufferStatus; GLPtr fClear; GLPtr fClearColor; GLPtr fClearStencil; GLPtr fColorMask; GLPtr fCompileShader; GLPtr fCompressedTexImage2D; GLPtr fCompressedTexSubImage2D; GLPtr fCopyTexSubImage2D; GLPtr fCopyTextureCHROMIUM; GLPtr fCreateProgram; GLPtr fCreateShader; GLPtr fCullFace; GLPtr fDeleteBuffers; GLPtr fDeleteFramebuffers; GLPtr fDeleteProgram; GLPtr fDeleteQueries; GLPtr fDeleteRenderbuffers; GLPtr fDeleteShader; GLPtr fDeleteTextures; GLPtr fDeleteVertexArrays; GLPtr fDepthMask; GLPtr fDisable; GLPtr fDisableVertexAttribArray; GLPtr fDrawArrays; GLPtr fDrawBuffer; GLPtr fDrawBuffers; GLPtr fDrawElements; GLPtr fEnable; GLPtr fEnableVertexAttribArray; GLPtr fEndQuery; GLPtr fFinish; GLPtr fFlush; GLPtr fFlushMappedBufferRange; GLPtr fFramebufferRenderbuffer; GLPtr fFramebufferTexture2D; GLPtr fFramebufferTexture2DMultisample; GLPtr fFrontFace; GLPtr fGenBuffers; GLPtr fGenFramebuffers; GLPtr fGenerateMipmap; GLPtr fGenQueries; GLPtr fGenRenderbuffers; GLPtr fGenTextures; GLPtr fGenVertexArrays; GLPtr fGetBufferParameteriv; GLPtr fGetError; GLPtr fGetFramebufferAttachmentParameteriv; GLPtr fGetIntegerv; GLPtr fGetQueryObjecti64v; GLPtr fGetQueryObjectiv; GLPtr fGetQueryObjectui64v; GLPtr fGetQueryObjectuiv; GLPtr fGetQueryiv; GLPtr fGetProgramInfoLog; GLPtr fGetProgramiv; GLPtr fGetRenderbufferParameteriv; GLPtr fGetShaderInfoLog; GLPtr fGetShaderiv; GLPtr fGetShaderPrecisionFormat; GLPtr fGetString; GLPtr fGetStringi; GLPtr fGetTexLevelParameteriv; GLPtr fGetUniformLocation; GLPtr fInsertEventMarker; GLPtr fInvalidateBufferData; GLPtr fInvalidateBufferSubData; GLPtr fInvalidateFramebuffer; GLPtr fInvalidateSubFramebuffer; GLPtr fInvalidateTexImage; GLPtr fInvalidateTexSubImage; GLPtr fIsTexture; GLPtr fLineWidth; GLPtr fLinkProgram; GLPtr fMapBuffer; GLPtr fMapBufferRange; GLPtr fMapBufferSubData; GLPtr fMapTexSubImage2D; GLPtr fPixelStorei; GLPtr fPopGroupMarker; GLPtr fPushGroupMarker; GLPtr fQueryCounter; GLPtr fRasterSamples; GLPtr fReadBuffer; GLPtr fReadPixels; GLPtr fRenderbufferStorage; // On OpenGL ES there are multiple incompatible extensions that add support for MSAA // and ES3 adds MSAA support to the standard. On an ES3 driver we may still use the // older extensions for performance reasons or due to ES3 driver bugs. We want the function // that creates the GrGLInterface to provide all available functions and internally // we will select among them. They all have a method called glRenderbufferStorageMultisample*. // So we have separate function pointers for GL_IMG/EXT_multisampled_to_texture, // GL_CHROMIUM/ANGLE_framebuffer_multisample/ES3, and GL_APPLE_framebuffer_multisample // variations. // // If a driver supports multiple GL_ARB_framebuffer_multisample-style extensions then we will // assume the function pointers for the standard (or equivalent GL_ARB) version have // been preferred over GL_EXT, GL_CHROMIUM, or GL_ANGLE variations that have reduced // functionality. // GL_EXT_multisampled_render_to_texture (preferred) or GL_IMG_multisampled_render_to_texture GLPtr fRenderbufferStorageMultisampleES2EXT; // GL_APPLE_framebuffer_multisample GLPtr fRenderbufferStorageMultisampleES2APPLE; // This is used to store the pointer for GL_ARB/EXT/ANGLE/CHROMIUM_framebuffer_multisample or // the standard function in ES3+ or GL 3.0+. GLPtr fRenderbufferStorageMultisample; // Pointer to BindUniformLocationCHROMIUM from the GL_CHROMIUM_bind_uniform_location extension. GLPtr fBindUniformLocation; GLPtr fResolveMultisampleFramebuffer; GLPtr fScissor; GLPtr fShaderSource; GLPtr fStencilFunc; GLPtr fStencilFuncSeparate; GLPtr fStencilMask; GLPtr fStencilMaskSeparate; GLPtr fStencilOp; GLPtr fStencilOpSeparate; GLPtr fTexImage2D; GLPtr fTexParameteri; GLPtr fTexParameteriv; GLPtr fTexSubImage2D; GLPtr fTexStorage2D; GLPtr fTextureBarrier; GLPtr fDiscardFramebuffer; GLPtr fUniform1f; GLPtr fUniform1i; GLPtr fUniform1fv; GLPtr fUniform1iv; GLPtr fUniform2f; GLPtr fUniform2i; GLPtr fUniform2fv; GLPtr fUniform2iv; GLPtr fUniform3f; GLPtr fUniform3i; GLPtr fUniform3fv; GLPtr fUniform3iv; GLPtr fUniform4f; GLPtr fUniform4i; GLPtr fUniform4fv; GLPtr fUniform4iv; GLPtr fUniformMatrix2fv; GLPtr fUniformMatrix3fv; GLPtr fUniformMatrix4fv; GLPtr fUnmapBuffer; GLPtr fUnmapBufferSubData; GLPtr fUnmapTexSubImage2D; GLPtr fUseProgram; GLPtr fVertexAttrib1f; GLPtr fVertexAttrib2fv; GLPtr fVertexAttrib3fv; GLPtr fVertexAttrib4fv; GLPtr fVertexAttribPointer; GLPtr fViewport; /* GL_NV_path_rendering */ GLPtr fMatrixLoadf; GLPtr fMatrixLoadIdentity; GLPtr fGetProgramResourceLocation; GLPtr fPathCommands; GLPtr fPathParameteri; GLPtr fPathParameterf; GLPtr fGenPaths; GLPtr fDeletePaths; GLPtr fIsPath; GLPtr fPathStencilFunc; GLPtr fStencilFillPath; GLPtr fStencilStrokePath; GLPtr fStencilFillPathInstanced; GLPtr fStencilStrokePathInstanced; GLPtr fCoverFillPath; GLPtr fCoverStrokePath; GLPtr fCoverFillPathInstanced; GLPtr fCoverStrokePathInstanced; // NV_path_rendering v1.2 GLPtr fStencilThenCoverFillPath; GLPtr fStencilThenCoverStrokePath; GLPtr fStencilThenCoverFillPathInstanced; GLPtr fStencilThenCoverStrokePathInstanced; // NV_path_rendering v1.3 GLPtr fProgramPathFragmentInputGen; // CHROMIUM_path_rendering GLPtr fBindFragmentInputLocation; /* NV_framebuffer_mixed_samples */ GLPtr fCoverageModulation; /* ARB_draw_instanced */ GLPtr fDrawArraysInstanced; GLPtr fDrawElementsInstanced; /* ARB_instanced_arrays */ GLPtr fVertexAttribDivisor; /* NV_bindless_texture */ // We use the NVIDIA verson for now because it does not require dynamically uniform handles. // We may switch the the ARB version and/or omit methods in the future. GLPtr fGetTextureHandle; GLPtr fGetTextureSamplerHandle; GLPtr fMakeTextureHandleResident; GLPtr fMakeTextureHandleNonResident; GLPtr fGetImageHandle; GLPtr fMakeImageHandleResident; GLPtr fMakeImageHandleNonResident; GLPtr fIsTextureHandleResident; GLPtr fIsImageHandleResident; GLPtr fUniformHandleui64; GLPtr fUniformHandleui64v; GLPtr fProgramUniformHandleui64; GLPtr fProgramUniformHandleui64v; /* EXT_direct_state_access */ // We use the EXT verson because it is more expansive and interacts with more extensions // than the ARB or core (4.5) versions. We may switch and/or omit methods in the future. GLPtr fTextureParameteri; GLPtr fTextureParameteriv; GLPtr fTextureParameterf; GLPtr fTextureParameterfv; GLPtr fTextureImage1D; GLPtr fTextureImage2D; GLPtr fTextureSubImage1D; GLPtr fTextureSubImage2D; GLPtr fCopyTextureImage1D; GLPtr fCopyTextureImage2D; GLPtr fCopyTextureSubImage1D; GLPtr fCopyTextureSubImage2D; GLPtr fGetTextureImage; GLPtr fGetTextureParameterfv; GLPtr fGetTextureParameteriv; GLPtr fGetTextureLevelParameterfv; GLPtr fGetTextureLevelParameteriv; // OpenGL 1.2 GLPtr fTextureImage3D; GLPtr fTextureSubImage3D; GLPtr fCopyTextureSubImage3D; GLPtr fCompressedTextureImage3D; GLPtr fCompressedTextureImage2D; GLPtr fCompressedTextureImage1D; GLPtr fCompressedTextureSubImage3D; GLPtr fCompressedTextureSubImage2D; GLPtr fCompressedTextureSubImage1D; GLPtr fGetCompressedTextureImage; // OpenGL 1.5 GLPtr fNamedBufferData; GLPtr fNamedBufferSubData; GLPtr fMapNamedBuffer; GLPtr fUnmapNamedBuffer; GLPtr fGetNamedBufferParameteriv; GLPtr fGetNamedBufferPointerv; GLPtr fGetNamedBufferSubData; // OpenGL 2.0 GLPtr fProgramUniform1f; GLPtr fProgramUniform2f; GLPtr fProgramUniform3f; GLPtr fProgramUniform4f; GLPtr fProgramUniform1i; GLPtr fProgramUniform2i; GLPtr fProgramUniform3i; GLPtr fProgramUniform4i; GLPtr fProgramUniform1fv; GLPtr fProgramUniform2fv; GLPtr fProgramUniform3fv; GLPtr fProgramUniform4fv; GLPtr fProgramUniform1iv; GLPtr fProgramUniform2iv; GLPtr fProgramUniform3iv; GLPtr fProgramUniform4iv; GLPtr fProgramUniformMatrix2fv; GLPtr fProgramUniformMatrix3fv; GLPtr fProgramUniformMatrix4fv; // OpenGL 2.1 GLPtr fProgramUniformMatrix2x3fv; GLPtr fProgramUniformMatrix3x2fv; GLPtr fProgramUniformMatrix2x4fv; GLPtr fProgramUniformMatrix4x2fv; GLPtr fProgramUniformMatrix3x4fv; GLPtr fProgramUniformMatrix4x3fv; // OpenGL 3.0 GLPtr fNamedRenderbufferStorage; GLPtr fGetNamedRenderbufferParameteriv; GLPtr fNamedRenderbufferStorageMultisample; GLPtr fCheckNamedFramebufferStatus; GLPtr fNamedFramebufferTexture1D; GLPtr fNamedFramebufferTexture2D; GLPtr fNamedFramebufferTexture3D; GLPtr fNamedFramebufferRenderbuffer; GLPtr fGetNamedFramebufferAttachmentParameteriv; GLPtr fGenerateTextureMipmap; GLPtr fFramebufferDrawBuffer; GLPtr fFramebufferDrawBuffers; GLPtr fFramebufferReadBuffer; GLPtr fGetFramebufferParameteriv; GLPtr fNamedCopyBufferSubData; GLPtr fVertexArrayVertexOffset; GLPtr fVertexArrayColorOffset; GLPtr fVertexArrayEdgeFlagOffset; GLPtr fVertexArrayIndexOffset; GLPtr fVertexArrayNormalOffset; GLPtr fVertexArrayTexCoordOffset; GLPtr fVertexArrayMultiTexCoordOffset; GLPtr fVertexArrayFogCoordOffset; GLPtr fVertexArraySecondaryColorOffset; GLPtr fVertexArrayVertexAttribOffset; GLPtr fVertexArrayVertexAttribIOffset; GLPtr fEnableVertexArray; GLPtr fDisableVertexArray; GLPtr fEnableVertexArrayAttrib; GLPtr fDisableVertexArrayAttrib; GLPtr fGetVertexArrayIntegerv; GLPtr fGetVertexArrayPointerv; GLPtr fGetVertexArrayIntegeri_v; GLPtr fGetVertexArrayPointeri_v; GLPtr fMapNamedBufferRange; GLPtr fFlushMappedNamedBufferRange; /* KHR_debug */ GLPtr fDebugMessageControl; GLPtr fDebugMessageInsert; GLPtr fDebugMessageCallback; GLPtr fGetDebugMessageLog; GLPtr fPushDebugGroup; GLPtr fPopDebugGroup; GLPtr fObjectLabel; } fFunctions; // Per-GL func callback #if GR_GL_PER_GL_FUNC_CALLBACK GrGLInterfaceCallbackProc fCallback; GrGLInterfaceCallbackData fCallbackData; #endif // This exists for internal testing. virtual void abandon() const {} }; #endif