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:
GLPtrGrGLInterface102         GLPtr() : fPtr(NULL) {}
103         GLPtr operator=(FNPTR_TYPE ptr) { fPtr = ptr; return *this; }
FNPTR_TYPEGrGLInterface104         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:
GLPtrAliasGrGLInterface113         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 
hasExtensionGrGLInterface141     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<GrGLBlendBarrierProc> fBlendBarrier;
160         GLPtr<GrGLBlendColorProc> fBlendColor;
161         GLPtr<GrGLBlendEquationProc> fBlendEquation;
162         GLPtr<GrGLBlendFuncProc> fBlendFunc;
163         GLPtr<GrGLBlitFramebufferProc> fBlitFramebuffer;
164         GLPtr<GrGLBufferDataProc> fBufferData;
165         GLPtr<GrGLBufferSubDataProc> fBufferSubData;
166         GLPtr<GrGLCheckFramebufferStatusProc> fCheckFramebufferStatus;
167         GLPtr<GrGLClearProc> fClear;
168         GLPtr<GrGLClearColorProc> fClearColor;
169         GLPtr<GrGLClearStencilProc> fClearStencil;
170         GLPtr<GrGLColorMaskProc> fColorMask;
171         GLPtr<GrGLCompileShaderProc> fCompileShader;
172         GLPtr<GrGLCompressedTexImage2DProc> fCompressedTexImage2D;
173         GLPtr<GrGLCompressedTexSubImage2DProc> fCompressedTexSubImage2D;
174         GLPtr<GrGLCopyTexSubImage2DProc> fCopyTexSubImage2D;
175         GLPtr<GrGLCopyTextureCHROMIUMProc> fCopyTextureCHROMIUM;
176         GLPtr<GrGLCreateProgramProc> fCreateProgram;
177         GLPtr<GrGLCreateShaderProc> fCreateShader;
178         GLPtr<GrGLCullFaceProc> fCullFace;
179         GLPtr<GrGLDeleteBuffersProc> fDeleteBuffers;
180         GLPtr<GrGLDeleteFramebuffersProc> fDeleteFramebuffers;
181         GLPtr<GrGLDeleteProgramProc> fDeleteProgram;
182         GLPtr<GrGLDeleteQueriesProc> fDeleteQueries;
183         GLPtr<GrGLDeleteRenderbuffersProc> fDeleteRenderbuffers;
184         GLPtr<GrGLDeleteShaderProc> fDeleteShader;
185         GLPtr<GrGLDeleteTexturesProc> fDeleteTextures;
186         GLPtr<GrGLDeleteVertexArraysProc> fDeleteVertexArrays;
187         GLPtr<GrGLDepthMaskProc> fDepthMask;
188         GLPtr<GrGLDisableProc> fDisable;
189         GLPtr<GrGLDisableVertexAttribArrayProc> fDisableVertexAttribArray;
190         GLPtr<GrGLDrawArraysProc> fDrawArrays;
191         GLPtr<GrGLDrawBufferProc> fDrawBuffer;
192         GLPtr<GrGLDrawBuffersProc> fDrawBuffers;
193         GLPtr<GrGLDrawElementsProc> fDrawElements;
194         GLPtr<GrGLEnableProc> fEnable;
195         GLPtr<GrGLEnableVertexAttribArrayProc> fEnableVertexAttribArray;
196         GLPtr<GrGLEndQueryProc> fEndQuery;
197         GLPtr<GrGLFinishProc> fFinish;
198         GLPtr<GrGLFlushProc> fFlush;
199         GLPtr<GrGLFlushMappedBufferRangeProc> fFlushMappedBufferRange;
200         GLPtr<GrGLFramebufferRenderbufferProc> fFramebufferRenderbuffer;
201         GLPtr<GrGLFramebufferTexture2DProc> fFramebufferTexture2D;
202         GLPtr<GrGLFramebufferTexture2DMultisampleProc> fFramebufferTexture2DMultisample;
203         GLPtr<GrGLFrontFaceProc> fFrontFace;
204         GLPtr<GrGLGenBuffersProc> fGenBuffers;
205         GLPtr<GrGLGenFramebuffersProc> fGenFramebuffers;
206         GLPtr<GrGLGenerateMipmapProc> fGenerateMipmap;
207         GLPtr<GrGLGenQueriesProc> fGenQueries;
208         GLPtr<GrGLGenRenderbuffersProc> fGenRenderbuffers;
209         GLPtr<GrGLGenTexturesProc> fGenTextures;
210         GLPtr<GrGLGenVertexArraysProc> fGenVertexArrays;
211         GLPtr<GrGLGetBufferParameterivProc> fGetBufferParameteriv;
212         GLPtr<GrGLGetErrorProc> fGetError;
213         GLPtr<GrGLGetFramebufferAttachmentParameterivProc> fGetFramebufferAttachmentParameteriv;
214         GLPtr<GrGLGetIntegervProc> fGetIntegerv;
215         GLPtr<GrGLGetQueryObjecti64vProc> fGetQueryObjecti64v;
216         GLPtr<GrGLGetQueryObjectivProc> fGetQueryObjectiv;
217         GLPtr<GrGLGetQueryObjectui64vProc> fGetQueryObjectui64v;
218         GLPtr<GrGLGetQueryObjectuivProc> fGetQueryObjectuiv;
219         GLPtr<GrGLGetQueryivProc> fGetQueryiv;
220         GLPtr<GrGLGetProgramInfoLogProc> fGetProgramInfoLog;
221         GLPtr<GrGLGetProgramivProc> fGetProgramiv;
222         GLPtr<GrGLGetRenderbufferParameterivProc> fGetRenderbufferParameteriv;
223         GLPtr<GrGLGetShaderInfoLogProc> fGetShaderInfoLog;
224         GLPtr<GrGLGetShaderivProc> fGetShaderiv;
225         GLPtr<GrGLGetShaderPrecisionFormatProc> fGetShaderPrecisionFormat;
226         GLPtr<GrGLGetStringProc> fGetString;
227         GLPtr<GrGLGetStringiProc> fGetStringi;
228         GLPtr<GrGLGetTexLevelParameterivProc> fGetTexLevelParameteriv;
229         GLPtr<GrGLGetUniformLocationProc> fGetUniformLocation;
230         GLPtr<GrGLInsertEventMarkerProc> fInsertEventMarker;
231         GLPtr<GrGLInvalidateBufferDataProc> fInvalidateBufferData;
232         GLPtr<GrGLInvalidateBufferSubDataProc> fInvalidateBufferSubData;
233         GLPtr<GrGLInvalidateFramebufferProc> fInvalidateFramebuffer;
234         GLPtr<GrGLInvalidateSubFramebufferProc> fInvalidateSubFramebuffer;
235         GLPtr<GrGLInvalidateTexImageProc> fInvalidateTexImage;
236         GLPtr<GrGLInvalidateTexSubImageProc> fInvalidateTexSubImage;
237         GLPtr<GrGLLineWidthProc> fLineWidth;
238         GLPtr<GrGLLinkProgramProc> fLinkProgram;
239         GLPtr<GrGLMapBufferProc> fMapBuffer;
240         GLPtr<GrGLMapBufferRangeProc> fMapBufferRange;
241         GLPtr<GrGLMapBufferSubDataProc> fMapBufferSubData;
242         GLPtr<GrGLMapTexSubImage2DProc> fMapTexSubImage2D;
243         GLPtr<GrGLMatrixLoadfProc> fMatrixLoadf;
244         GLPtr<GrGLMatrixLoadIdentityProc> fMatrixLoadIdentity;
245         GLPtr<GrGLPixelStoreiProc> fPixelStorei;
246         GLPtr<GrGLPopGroupMarkerProc> fPopGroupMarker;
247         GLPtr<GrGLPushGroupMarkerProc> fPushGroupMarker;
248         GLPtr<GrGLQueryCounterProc> fQueryCounter;
249         GLPtr<GrGLReadBufferProc> fReadBuffer;
250         GLPtr<GrGLReadPixelsProc> fReadPixels;
251         GLPtr<GrGLRenderbufferStorageProc> fRenderbufferStorage;
252 
253         //  On OpenGL ES there are multiple incompatible extensions that add support for MSAA
254         //  and ES3 adds MSAA support to the standard. On an ES3 driver we may still use the
255         //  older extensions for performance reasons or due to ES3 driver bugs. We want the function
256         //  that creates the GrGLInterface to provide all available functions and internally
257         //  we will select among them. They all have a method called glRenderbufferStorageMultisample*.
258         //  So we have separate function pointers for GL_IMG/EXT_multisampled_to_texture,
259         //  GL_CHROMIUM/ANGLE_framebuffer_multisample/ES3, and GL_APPLE_framebuffer_multisample
260         //  variations.
261         //
262         //  If a driver supports multiple GL_ARB_framebuffer_multisample-style extensions then we will
263         //  assume the function pointers for the standard (or equivalent GL_ARB) version have
264         //  been preferred over GL_EXT, GL_CHROMIUM, or GL_ANGLE variations that have reduced
265         //  functionality.
266 
267         //  GL_EXT_multisampled_render_to_texture (preferred) or GL_IMG_multisampled_render_to_texture
268         GLPtr<GrGLRenderbufferStorageMultisampleProc> fRenderbufferStorageMultisampleES2EXT;
269         //  GL_APPLE_framebuffer_multisample
270         GLPtr<GrGLRenderbufferStorageMultisampleProc> fRenderbufferStorageMultisampleES2APPLE;
271 
272         //  This is used to store the pointer for GL_ARB/EXT/ANGLE/CHROMIUM_framebuffer_multisample or
273         //  the standard function in ES3+ or GL 3.0+.
274         GLPtr<GrGLRenderbufferStorageMultisampleProc> fRenderbufferStorageMultisample;
275 
276         // Pointer to BindUniformLocationCHROMIUM from the GL_CHROMIUM_bind_uniform_location extension.
277         GLPtr<GrGLBindUniformLocation> fBindUniformLocation;
278 
279         GLPtr<GrGLResolveMultisampleFramebufferProc> fResolveMultisampleFramebuffer;
280         GLPtr<GrGLScissorProc> fScissor;
281         GLPtr<GrGLShaderSourceProc> fShaderSource;
282         GLPtr<GrGLStencilFuncProc> fStencilFunc;
283         GLPtr<GrGLStencilFuncSeparateProc> fStencilFuncSeparate;
284         GLPtr<GrGLStencilMaskProc> fStencilMask;
285         GLPtr<GrGLStencilMaskSeparateProc> fStencilMaskSeparate;
286         GLPtr<GrGLStencilOpProc> fStencilOp;
287         GLPtr<GrGLStencilOpSeparateProc> fStencilOpSeparate;
288         GLPtr<GrGLTexImage2DProc> fTexImage2D;
289         GLPtr<GrGLTexParameteriProc> fTexParameteri;
290         GLPtr<GrGLTexParameterivProc> fTexParameteriv;
291         GLPtr<GrGLTexSubImage2DProc> fTexSubImage2D;
292         GLPtr<GrGLTexStorage2DProc> fTexStorage2D;
293         GLPtr<GrGLTextureBarrierProc> fTextureBarrier;
294         GLPtr<GrGLDiscardFramebufferProc> fDiscardFramebuffer;
295         GLPtr<GrGLUniform1fProc> fUniform1f;
296         GLPtr<GrGLUniform1iProc> fUniform1i;
297         GLPtr<GrGLUniform1fvProc> fUniform1fv;
298         GLPtr<GrGLUniform1ivProc> fUniform1iv;
299         GLPtr<GrGLUniform2fProc> fUniform2f;
300         GLPtr<GrGLUniform2iProc> fUniform2i;
301         GLPtr<GrGLUniform2fvProc> fUniform2fv;
302         GLPtr<GrGLUniform2ivProc> fUniform2iv;
303         GLPtr<GrGLUniform3fProc> fUniform3f;
304         GLPtr<GrGLUniform3iProc> fUniform3i;
305         GLPtr<GrGLUniform3fvProc> fUniform3fv;
306         GLPtr<GrGLUniform3ivProc> fUniform3iv;
307         GLPtr<GrGLUniform4fProc> fUniform4f;
308         GLPtr<GrGLUniform4iProc> fUniform4i;
309         GLPtr<GrGLUniform4fvProc> fUniform4fv;
310         GLPtr<GrGLUniform4ivProc> fUniform4iv;
311         GLPtr<GrGLUniformMatrix2fvProc> fUniformMatrix2fv;
312         GLPtr<GrGLUniformMatrix3fvProc> fUniformMatrix3fv;
313         GLPtr<GrGLUniformMatrix4fvProc> fUniformMatrix4fv;
314         GLPtr<GrGLUnmapBufferProc> fUnmapBuffer;
315         GLPtr<GrGLUnmapBufferSubDataProc> fUnmapBufferSubData;
316         GLPtr<GrGLUnmapTexSubImage2DProc> fUnmapTexSubImage2D;
317         GLPtr<GrGLUseProgramProc> fUseProgram;
318         GLPtr<GrGLVertexAttrib1fProc> fVertexAttrib1f;
319         GLPtr<GrGLVertexAttrib2fvProc> fVertexAttrib2fv;
320         GLPtr<GrGLVertexAttrib3fvProc> fVertexAttrib3fv;
321         GLPtr<GrGLVertexAttrib4fvProc> fVertexAttrib4fv;
322         GLPtr<GrGLVertexAttribPointerProc> fVertexAttribPointer;
323         GLPtr<GrGLViewportProc> fViewport;
324 
325         // Experimental: Functions for GL_NV_path_rendering. These will be
326         // alphabetized with the above functions once this is fully supported
327         // (and functions we are unlikely to use will possibly be omitted).
328         GLPtr<GrGLGetProgramResourceLocationProc> fGetProgramResourceLocation;
329         GLPtr<GrGLPathCommandsProc> fPathCommands;
330         GLPtr<GrGLPathCoordsProc> fPathCoords;
331         GLPtr<GrGLPathParameteriProc> fPathParameteri;
332         GLPtr<GrGLPathParameterfProc> fPathParameterf;
333         GLPtr<GrGLGenPathsProc> fGenPaths;
334         GLPtr<GrGLDeletePathsProc> fDeletePaths;
335         GLPtr<GrGLIsPathProc> fIsPath;
336         GLPtr<GrGLPathStencilFuncProc> fPathStencilFunc;
337         GLPtr<GrGLStencilFillPathProc> fStencilFillPath;
338         GLPtr<GrGLStencilStrokePathProc> fStencilStrokePath;
339         GLPtr<GrGLStencilFillPathInstancedProc> fStencilFillPathInstanced;
340         GLPtr<GrGLStencilStrokePathInstancedProc> fStencilStrokePathInstanced;
341         GLPtr<GrGLPathTexGenProc> fPathTexGen;
342         GLPtr<GrGLCoverFillPathProc> fCoverFillPath;
343         GLPtr<GrGLCoverStrokePathProc> fCoverStrokePath;
344         GLPtr<GrGLCoverFillPathInstancedProc> fCoverFillPathInstanced;
345         GLPtr<GrGLCoverStrokePathInstancedProc> fCoverStrokePathInstanced;
346         // NV_path_rendering v1.2
347         GLPtr<GrGLStencilThenCoverFillPathProc> fStencilThenCoverFillPath;
348         GLPtr<GrGLStencilThenCoverStrokePathProc> fStencilThenCoverStrokePath;
349         GLPtr<GrGLStencilThenCoverFillPathInstancedProc> fStencilThenCoverFillPathInstanced;
350         GLPtr<GrGLStencilThenCoverStrokePathInstancedProc> fStencilThenCoverStrokePathInstanced;
351         // NV_path_rendering v1.3
352         GLPtr<GrGLProgramPathFragmentInputGenProc> fProgramPathFragmentInputGen;
353         GLPtr<GrGLPathMemoryGlyphIndexArrayProc> fPathMemoryGlyphIndexArray;
354         // NV_framebuffer_mixed_samples
355         GLPtr<GrGLCoverageModulationProc> fCoverageModulation;
356     } fFunctions;
357 
358     // Per-GL func callback
359 #if GR_GL_PER_GL_FUNC_CALLBACK
360     GrGLInterfaceCallbackProc fCallback;
361     GrGLInterfaceCallbackData fCallbackData;
362 #endif
363 
364     // This exists for internal testing.
abandonGrGLInterface365     virtual void abandon() const {}
366 };
367 
368 #endif
369