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<GrGLBindVertexArrayProc> fBindVertexArray; 150 GLPtr<GrGLBlendColorProc> fBlendColor; 151 GLPtr<GrGLBlendFuncProc> fBlendFunc; 152 GLPtr<GrGLBlitFramebufferProc> fBlitFramebuffer; 153 GLPtr<GrGLBufferDataProc> fBufferData; 154 GLPtr<GrGLBufferSubDataProc> fBufferSubData; 155 GLPtr<GrGLCheckFramebufferStatusProc> fCheckFramebufferStatus; 156 GLPtr<GrGLClearProc> fClear; 157 GLPtr<GrGLClearColorProc> fClearColor; 158 GLPtr<GrGLClearStencilProc> fClearStencil; 159 GLPtr<GrGLColorMaskProc> fColorMask; 160 GLPtr<GrGLCompileShaderProc> fCompileShader; 161 GLPtr<GrGLCompressedTexImage2DProc> fCompressedTexImage2D; 162 GLPtr<GrGLCopyTexSubImage2DProc> fCopyTexSubImage2D; 163 GLPtr<GrGLCreateProgramProc> fCreateProgram; 164 GLPtr<GrGLCreateShaderProc> fCreateShader; 165 GLPtr<GrGLCullFaceProc> fCullFace; 166 GLPtr<GrGLDeleteBuffersProc> fDeleteBuffers; 167 GLPtr<GrGLDeleteFramebuffersProc> fDeleteFramebuffers; 168 GLPtr<GrGLDeleteProgramProc> fDeleteProgram; 169 GLPtr<GrGLDeleteQueriesProc> fDeleteQueries; 170 GLPtr<GrGLDeleteRenderbuffersProc> fDeleteRenderbuffers; 171 GLPtr<GrGLDeleteShaderProc> fDeleteShader; 172 GLPtr<GrGLDeleteTexturesProc> fDeleteTextures; 173 GLPtr<GrGLDeleteVertexArraysProc> fDeleteVertexArrays; 174 GLPtr<GrGLDepthMaskProc> fDepthMask; 175 GLPtr<GrGLDisableProc> fDisable; 176 GLPtr<GrGLDisableVertexAttribArrayProc> fDisableVertexAttribArray; 177 GLPtr<GrGLDrawArraysProc> fDrawArrays; 178 GLPtr<GrGLDrawBufferProc> fDrawBuffer; 179 GLPtr<GrGLDrawBuffersProc> fDrawBuffers; 180 GLPtr<GrGLDrawElementsProc> fDrawElements; 181 GLPtr<GrGLEnableProc> fEnable; 182 GLPtr<GrGLEnableVertexAttribArrayProc> fEnableVertexAttribArray; 183 GLPtr<GrGLEndQueryProc> fEndQuery; 184 GLPtr<GrGLFinishProc> fFinish; 185 GLPtr<GrGLFlushProc> fFlush; 186 GLPtr<GrGLFramebufferRenderbufferProc> fFramebufferRenderbuffer; 187 GLPtr<GrGLFramebufferTexture2DProc> fFramebufferTexture2D; 188 GLPtr<GrGLFramebufferTexture2DMultisampleProc> fFramebufferTexture2DMultisample; 189 GLPtr<GrGLFrontFaceProc> fFrontFace; 190 GLPtr<GrGLGenBuffersProc> fGenBuffers; 191 GLPtr<GrGLGenFramebuffersProc> fGenFramebuffers; 192 GLPtr<GrGLGenerateMipmapProc> fGenerateMipmap; 193 GLPtr<GrGLGenQueriesProc> fGenQueries; 194 GLPtr<GrGLGenRenderbuffersProc> fGenRenderbuffers; 195 GLPtr<GrGLGenTexturesProc> fGenTextures; 196 GLPtr<GrGLGenVertexArraysProc> fGenVertexArrays; 197 GLPtr<GrGLGetBufferParameterivProc> fGetBufferParameteriv; 198 GLPtr<GrGLGetErrorProc> fGetError; 199 GLPtr<GrGLGetFramebufferAttachmentParameterivProc> fGetFramebufferAttachmentParameteriv; 200 GLPtr<GrGLGetIntegervProc> fGetIntegerv; 201 GLPtr<GrGLGetQueryObjecti64vProc> fGetQueryObjecti64v; 202 GLPtr<GrGLGetQueryObjectivProc> fGetQueryObjectiv; 203 GLPtr<GrGLGetQueryObjectui64vProc> fGetQueryObjectui64v; 204 GLPtr<GrGLGetQueryObjectuivProc> fGetQueryObjectuiv; 205 GLPtr<GrGLGetQueryivProc> fGetQueryiv; 206 GLPtr<GrGLGetProgramInfoLogProc> fGetProgramInfoLog; 207 GLPtr<GrGLGetProgramivProc> fGetProgramiv; 208 GLPtr<GrGLGetRenderbufferParameterivProc> fGetRenderbufferParameteriv; 209 GLPtr<GrGLGetShaderInfoLogProc> fGetShaderInfoLog; 210 GLPtr<GrGLGetShaderivProc> fGetShaderiv; 211 GLPtr<GrGLGetStringProc> fGetString; 212 GLPtr<GrGLGetStringiProc> fGetStringi; 213 GLPtr<GrGLGetTexLevelParameterivProc> fGetTexLevelParameteriv; 214 GLPtr<GrGLGetUniformLocationProc> fGetUniformLocation; 215 GLPtr<GrGLLineWidthProc> fLineWidth; 216 GLPtr<GrGLLinkProgramProc> fLinkProgram; 217 GLPtr<GrGLMapBufferProc> fMapBuffer; 218 GLPtr<GrGLPixelStoreiProc> fPixelStorei; 219 GLPtr<GrGLQueryCounterProc> fQueryCounter; 220 GLPtr<GrGLReadBufferProc> fReadBuffer; 221 GLPtr<GrGLReadPixelsProc> fReadPixels; 222 GLPtr<GrGLRenderbufferStorageProc> fRenderbufferStorage; 223 GLPtr<GrGLRenderbufferStorageMultisampleProc> fRenderbufferStorageMultisample; 224 GLPtr<GrGLRenderbufferStorageMultisampleCoverageProc> fRenderbufferStorageMultisampleCoverage; 225 GLPtr<GrGLResolveMultisampleFramebufferProc> fResolveMultisampleFramebuffer; 226 GLPtr<GrGLScissorProc> fScissor; 227 GLPtr<GrGLShaderSourceProc> fShaderSource; 228 GLPtr<GrGLStencilFuncProc> fStencilFunc; 229 GLPtr<GrGLStencilFuncSeparateProc> fStencilFuncSeparate; 230 GLPtr<GrGLStencilMaskProc> fStencilMask; 231 GLPtr<GrGLStencilMaskSeparateProc> fStencilMaskSeparate; 232 GLPtr<GrGLStencilOpProc> fStencilOp; 233 GLPtr<GrGLStencilOpSeparateProc> fStencilOpSeparate; 234 GLPtr<GrGLTexImage2DProc> fTexImage2D; 235 GLPtr<GrGLTexParameteriProc> fTexParameteri; 236 GLPtr<GrGLTexParameterivProc> fTexParameteriv; 237 GLPtr<GrGLTexSubImage2DProc> fTexSubImage2D; 238 GLPtr<GrGLTexStorage2DProc> fTexStorage2D; 239 GLPtr<GrGLDiscardFramebufferProc> fDiscardFramebuffer; 240 GLPtr<GrGLUniform1fProc> fUniform1f; 241 GLPtr<GrGLUniform1iProc> fUniform1i; 242 GLPtr<GrGLUniform1fvProc> fUniform1fv; 243 GLPtr<GrGLUniform1ivProc> fUniform1iv; 244 GLPtr<GrGLUniform2fProc> fUniform2f; 245 GLPtr<GrGLUniform2iProc> fUniform2i; 246 GLPtr<GrGLUniform2fvProc> fUniform2fv; 247 GLPtr<GrGLUniform2ivProc> fUniform2iv; 248 GLPtr<GrGLUniform3fProc> fUniform3f; 249 GLPtr<GrGLUniform3iProc> fUniform3i; 250 GLPtr<GrGLUniform3fvProc> fUniform3fv; 251 GLPtr<GrGLUniform3ivProc> fUniform3iv; 252 GLPtr<GrGLUniform4fProc> fUniform4f; 253 GLPtr<GrGLUniform4iProc> fUniform4i; 254 GLPtr<GrGLUniform4fvProc> fUniform4fv; 255 GLPtr<GrGLUniform4ivProc> fUniform4iv; 256 GLPtr<GrGLUniformMatrix2fvProc> fUniformMatrix2fv; 257 GLPtr<GrGLUniformMatrix3fvProc> fUniformMatrix3fv; 258 GLPtr<GrGLUniformMatrix4fvProc> fUniformMatrix4fv; 259 GLPtr<GrGLUnmapBufferProc> fUnmapBuffer; 260 GLPtr<GrGLUseProgramProc> fUseProgram; 261 GLPtr<GrGLVertexAttrib4fvProc> fVertexAttrib4fv; 262 GLPtr<GrGLVertexAttribPointerProc> fVertexAttribPointer; 263 GLPtr<GrGLViewportProc> fViewport; 264 265 // Experimental: Functions for GL_NV_path_rendering. These will be 266 // alphabetized with the above functions once this is fully supported 267 // (and functions we are unlikely to use will possibly be omitted). 268 GLPtr<GrGLMatrixModeProc> fMatrixMode; 269 GLPtr<GrGLLoadIdentityProc> fLoadIdentity; 270 GLPtr<GrGLLoadMatrixfProc> fLoadMatrixf; 271 GLPtr<GrGLPathCommandsProc> fPathCommands; 272 GLPtr<GrGLPathCoordsProc> fPathCoords; 273 GLPtr<GrGLPathSubCommandsProc> fPathSubCommands; 274 GLPtr<GrGLPathSubCoordsProc> fPathSubCoords; 275 GLPtr<GrGLPathStringProc> fPathString; 276 GLPtr<GrGLPathGlyphsProc> fPathGlyphs; 277 GLPtr<GrGLPathGlyphRangeProc> fPathGlyphRange; 278 GLPtr<GrGLWeightPathsProc> fWeightPaths; 279 GLPtr<GrGLCopyPathProc> fCopyPath; 280 GLPtr<GrGLInterpolatePathsProc> fInterpolatePaths; 281 GLPtr<GrGLTransformPathProc> fTransformPath; 282 GLPtr<GrGLPathParameterivProc> fPathParameteriv; 283 GLPtr<GrGLPathParameteriProc> fPathParameteri; 284 GLPtr<GrGLPathParameterfvProc> fPathParameterfv; 285 GLPtr<GrGLPathParameterfProc> fPathParameterf; 286 GLPtr<GrGLPathDashArrayProc> fPathDashArray; 287 GLPtr<GrGLGenPathsProc> fGenPaths; 288 GLPtr<GrGLDeletePathsProc> fDeletePaths; 289 GLPtr<GrGLIsPathProc> fIsPath; 290 GLPtr<GrGLPathStencilFuncProc> fPathStencilFunc; 291 GLPtr<GrGLPathStencilDepthOffsetProc> fPathStencilDepthOffset; 292 GLPtr<GrGLStencilFillPathProc> fStencilFillPath; 293 GLPtr<GrGLStencilStrokePathProc> fStencilStrokePath; 294 GLPtr<GrGLStencilFillPathInstancedProc> fStencilFillPathInstanced; 295 GLPtr<GrGLStencilStrokePathInstancedProc> fStencilStrokePathInstanced; 296 GLPtr<GrGLPathCoverDepthFuncProc> fPathCoverDepthFunc; 297 GLPtr<GrGLPathColorGenProc> fPathColorGen; 298 GLPtr<GrGLPathTexGenProc> fPathTexGen; 299 GLPtr<GrGLPathFogGenProc> fPathFogGen; 300 GLPtr<GrGLCoverFillPathProc> fCoverFillPath; 301 GLPtr<GrGLCoverStrokePathProc> fCoverStrokePath; 302 GLPtr<GrGLCoverFillPathInstancedProc> fCoverFillPathInstanced; 303 GLPtr<GrGLCoverStrokePathInstancedProc> fCoverStrokePathInstanced; 304 GLPtr<GrGLGetPathParameterivProc> fGetPathParameteriv; 305 GLPtr<GrGLGetPathParameterfvProc> fGetPathParameterfv; 306 GLPtr<GrGLGetPathCommandsProc> fGetPathCommands; 307 GLPtr<GrGLGetPathCoordsProc> fGetPathCoords; 308 GLPtr<GrGLGetPathDashArrayProc> fGetPathDashArray; 309 GLPtr<GrGLGetPathMetricsProc> fGetPathMetrics; 310 GLPtr<GrGLGetPathMetricRangeProc> fGetPathMetricRange; 311 GLPtr<GrGLGetPathSpacingProc> fGetPathSpacing; 312 GLPtr<GrGLGetPathColorGenivProc> fGetPathColorGeniv; 313 GLPtr<GrGLGetPathColorGenfvProc> fGetPathColorGenfv; 314 GLPtr<GrGLGetPathTexGenivProc> fGetPathTexGeniv; 315 GLPtr<GrGLGetPathTexGenfvProc> fGetPathTexGenfv; 316 GLPtr<GrGLIsPointInFillPathProc> fIsPointInFillPath; 317 GLPtr<GrGLIsPointInStrokePathProc> fIsPointInStrokePath; 318 GLPtr<GrGLGetPathLengthProc> fGetPathLength; 319 GLPtr<GrGLPointAlongPathProc> fPointAlongPath; 320 321 // Per-GL func callback 322 #if GR_GL_PER_GL_FUNC_CALLBACK 323 GrGLInterfaceCallbackProc fCallback; 324 GrGLInterfaceCallbackData fCallbackData; 325 #endif 326 327 }; 328 329 #endif 330