• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "include/core/SkRefCnt.h"
12 #include "include/gpu/gl/GrGLExtensions.h"
13 #include "include/gpu/gl/GrGLFunctions.h"
14 
15 ////////////////////////////////////////////////////////////////////////////////
16 
17 typedef void(*GrGLFuncPtr)();
18 struct GrGLInterface;
19 
20 
21 /**
22  * Rather than depend on platform-specific GL headers and libraries, we require
23  * the client to provide a struct of GL function pointers. This struct can be
24  * specified per-GrContext as a parameter to GrContext::MakeGL. If no interface is
25  * passed to MakeGL then a default GL interface is created using GrGLMakeNativeInterface().
26  * If this returns nullptr then GrContext::MakeGL() will fail.
27  *
28  * The implementation of GrGLMakeNativeInterface is platform-specific. Several
29  * implementations have been provided (for GLX, WGL, EGL, etc), along with an
30  * implementation that simply returns nullptr. Clients should select the most
31  * appropriate one to build.
32  */
33 SK_API sk_sp<const GrGLInterface> GrGLMakeNativeInterface();
34 
35 /**
36  * GrContext uses the following interface to make all calls into OpenGL. When a
37  * GrContext is created it is given a GrGLInterface. The interface's function
38  * pointers must be valid for the OpenGL context associated with the GrContext.
39  * On some platforms, such as Windows, function pointers for OpenGL extensions
40  * may vary between OpenGL contexts. So the caller must be careful to use a
41  * GrGLInterface initialized for the correct context. All functions that should
42  * be available based on the OpenGL's version and extension string must be
43  * non-NULL or GrContext creation will fail. This can be tested with the
44  * validate() method when the OpenGL context has been made current.
45  */
46 struct SK_API GrGLInterface : public SkRefCnt {
47 private:
48     using INHERITED = SkRefCnt;
49 
50 #if GR_GL_CHECK_ERROR
51     // This is here to avoid having our debug code that checks for a GL error after most GL calls
52     // accidentally swallow an OOM that should be reported.
53     mutable bool fOOMed = false;
54     bool fSuppressErrorLogging = false;
55 #endif
56 
57 public:
58     GrGLInterface();
59 
60     // Validates that the GrGLInterface supports its advertised standard. This means the necessary
61     // function pointers have been initialized for both the GL version and any advertised
62     // extensions.
63     bool validate() const;
64 
65 #if GR_GL_CHECK_ERROR
66     GrGLenum checkError(const char* location, const char* call) const;
67     bool checkAndResetOOMed() const;
68     void suppressErrorLogging();
69 #endif
70 
71 #if GR_TEST_UTILS
GrGLInterfaceGrGLInterface72     GrGLInterface(const GrGLInterface& that)
73             : fStandard(that.fStandard)
74             , fExtensions(that.fExtensions)
75             , fFunctions(that.fFunctions) {}
76 #endif
77 
78     // Indicates the type of GL implementation
79     union {
80         GrGLStandard fStandard;
81         GrGLStandard fBindingsExported; // Legacy name, will be remove when Chromium is updated.
82     };
83 
84     GrGLExtensions fExtensions;
85 
hasExtensionGrGLInterface86     bool hasExtension(const char ext[]) const { return fExtensions.has(ext); }
87 
88     /**
89      * The function pointers are in a struct so that we can have a compiler generated assignment
90      * operator.
91      */
92     struct Functions {
93         GrGLFunction<GrGLActiveTextureFn> fActiveTexture;
94         GrGLFunction<GrGLAttachShaderFn> fAttachShader;
95         GrGLFunction<GrGLBeginQueryFn> fBeginQuery;
96         GrGLFunction<GrGLBindAttribLocationFn> fBindAttribLocation;
97         GrGLFunction<GrGLBindBufferFn> fBindBuffer;
98         GrGLFunction<GrGLBindFragDataLocationFn> fBindFragDataLocation;
99         GrGLFunction<GrGLBindFragDataLocationIndexedFn> fBindFragDataLocationIndexed;
100         GrGLFunction<GrGLBindFramebufferFn> fBindFramebuffer;
101         GrGLFunction<GrGLBindRenderbufferFn> fBindRenderbuffer;
102         GrGLFunction<GrGLBindSamplerFn> fBindSampler;
103         GrGLFunction<GrGLBindTextureFn> fBindTexture;
104         GrGLFunction<GrGLBindVertexArrayFn> fBindVertexArray;
105         GrGLFunction<GrGLBlendBarrierFn> fBlendBarrier;
106         GrGLFunction<GrGLBlendColorFn> fBlendColor;
107         GrGLFunction<GrGLBlendEquationFn> fBlendEquation;
108         GrGLFunction<GrGLBlendFuncFn> fBlendFunc;
109         GrGLFunction<GrGLBlitFramebufferFn> fBlitFramebuffer;
110         GrGLFunction<GrGLBufferDataFn> fBufferData;
111         GrGLFunction<GrGLBufferSubDataFn> fBufferSubData;
112         GrGLFunction<GrGLCheckFramebufferStatusFn> fCheckFramebufferStatus;
113         GrGLFunction<GrGLClearFn> fClear;
114         GrGLFunction<GrGLClearColorFn> fClearColor;
115         GrGLFunction<GrGLClearStencilFn> fClearStencil;
116         GrGLFunction<GrGLClearTexImageFn> fClearTexImage;
117         GrGLFunction<GrGLClearTexSubImageFn> fClearTexSubImage;
118         GrGLFunction<GrGLColorMaskFn> fColorMask;
119         GrGLFunction<GrGLCompileShaderFn> fCompileShader;
120         GrGLFunction<GrGLCompressedTexImage2DFn> fCompressedTexImage2D;
121         GrGLFunction<GrGLCompressedTexSubImage2DFn> fCompressedTexSubImage2D;
122         GrGLFunction<GrGLCopyBufferSubDataFn> fCopyBufferSubData;
123         GrGLFunction<GrGLCopyTexSubImage2DFn> fCopyTexSubImage2D;
124         GrGLFunction<GrGLCreateProgramFn> fCreateProgram;
125         GrGLFunction<GrGLCreateShaderFn> fCreateShader;
126         GrGLFunction<GrGLCullFaceFn> fCullFace;
127         GrGLFunction<GrGLDeleteBuffersFn> fDeleteBuffers;
128         GrGLFunction<GrGLDeleteFencesFn> fDeleteFences;
129         GrGLFunction<GrGLDeleteFramebuffersFn> fDeleteFramebuffers;
130         GrGLFunction<GrGLDeleteProgramFn> fDeleteProgram;
131         GrGLFunction<GrGLDeleteQueriesFn> fDeleteQueries;
132         GrGLFunction<GrGLDeleteRenderbuffersFn> fDeleteRenderbuffers;
133         GrGLFunction<GrGLDeleteSamplersFn> fDeleteSamplers;
134         GrGLFunction<GrGLDeleteShaderFn> fDeleteShader;
135         GrGLFunction<GrGLDeleteTexturesFn> fDeleteTextures;
136         GrGLFunction<GrGLDeleteVertexArraysFn> fDeleteVertexArrays;
137         GrGLFunction<GrGLDepthMaskFn> fDepthMask;
138         GrGLFunction<GrGLDisableFn> fDisable;
139         GrGLFunction<GrGLDisableVertexAttribArrayFn> fDisableVertexAttribArray;
140         GrGLFunction<GrGLDrawArraysFn> fDrawArrays;
141         GrGLFunction<GrGLDrawArraysIndirectFn> fDrawArraysIndirect;
142         GrGLFunction<GrGLDrawArraysInstancedFn> fDrawArraysInstanced;
143         GrGLFunction<GrGLDrawBufferFn> fDrawBuffer;
144         GrGLFunction<GrGLDrawBuffersFn> fDrawBuffers;
145         GrGLFunction<GrGLDrawElementsFn> fDrawElements;
146         GrGLFunction<GrGLDrawElementsIndirectFn> fDrawElementsIndirect;
147         GrGLFunction<GrGLDrawElementsInstancedFn> fDrawElementsInstanced;
148         GrGLFunction<GrGLDrawRangeElementsFn> fDrawRangeElements;
149         GrGLFunction<GrGLEnableFn> fEnable;
150         GrGLFunction<GrGLEnableVertexAttribArrayFn> fEnableVertexAttribArray;
151         GrGLFunction<GrGLEndQueryFn> fEndQuery;
152         GrGLFunction<GrGLFinishFn> fFinish;
153         GrGLFunction<GrGLFinishFenceFn> fFinishFence;
154         GrGLFunction<GrGLFlushFn> fFlush;
155         GrGLFunction<GrGLFlushMappedBufferRangeFn> fFlushMappedBufferRange;
156         GrGLFunction<GrGLFramebufferRenderbufferFn> fFramebufferRenderbuffer;
157         GrGLFunction<GrGLFramebufferTexture2DFn> fFramebufferTexture2D;
158         GrGLFunction<GrGLFramebufferTexture2DMultisampleFn> fFramebufferTexture2DMultisample;
159         GrGLFunction<GrGLFrontFaceFn> fFrontFace;
160         GrGLFunction<GrGLGenBuffersFn> fGenBuffers;
161         GrGLFunction<GrGLGenFencesFn> fGenFences;
162         GrGLFunction<GrGLGenFramebuffersFn> fGenFramebuffers;
163         GrGLFunction<GrGLGenerateMipmapFn> fGenerateMipmap;
164         GrGLFunction<GrGLGenQueriesFn> fGenQueries;
165         GrGLFunction<GrGLGenRenderbuffersFn> fGenRenderbuffers;
166         GrGLFunction<GrGLGenSamplersFn> fGenSamplers;
167         GrGLFunction<GrGLGenTexturesFn> fGenTextures;
168         GrGLFunction<GrGLGenVertexArraysFn> fGenVertexArrays;
169         GrGLFunction<GrGLGetBufferParameterivFn> fGetBufferParameteriv;
170         GrGLFunction<GrGLGetErrorFn> fGetError;
171         GrGLFunction<GrGLGetFramebufferAttachmentParameterivFn> fGetFramebufferAttachmentParameteriv;
172         GrGLFunction<GrGLGetFloatvFn> fGetFloatv;
173         GrGLFunction<GrGLGetIntegervFn> fGetIntegerv;
174         GrGLFunction<GrGLGetMultisamplefvFn> fGetMultisamplefv;
175         GrGLFunction<GrGLGetProgramBinaryFn> fGetProgramBinary;
176         GrGLFunction<GrGLGetProgramInfoLogFn> fGetProgramInfoLog;
177         GrGLFunction<GrGLGetProgramivFn> fGetProgramiv;
178         GrGLFunction<GrGLGetQueryObjecti64vFn> fGetQueryObjecti64v;
179         GrGLFunction<GrGLGetQueryObjectivFn> fGetQueryObjectiv;
180         GrGLFunction<GrGLGetQueryObjectui64vFn> fGetQueryObjectui64v;
181         GrGLFunction<GrGLGetQueryObjectuivFn> fGetQueryObjectuiv;
182         GrGLFunction<GrGLGetQueryivFn> fGetQueryiv;
183         GrGLFunction<GrGLGetRenderbufferParameterivFn> fGetRenderbufferParameteriv;
184         GrGLFunction<GrGLGetShaderInfoLogFn> fGetShaderInfoLog;
185         GrGLFunction<GrGLGetShaderivFn> fGetShaderiv;
186         GrGLFunction<GrGLGetShaderPrecisionFormatFn> fGetShaderPrecisionFormat;
187         GrGLFunction<GrGLGetStringFn> fGetString;
188         GrGLFunction<GrGLGetStringiFn> fGetStringi;
189         GrGLFunction<GrGLGetTexLevelParameterivFn> fGetTexLevelParameteriv;
190         GrGLFunction<GrGLGetUniformLocationFn> fGetUniformLocation;
191         GrGLFunction<GrGLInsertEventMarkerFn> fInsertEventMarker;
192         GrGLFunction<GrGLInvalidateBufferDataFn> fInvalidateBufferData;
193         GrGLFunction<GrGLInvalidateBufferSubDataFn> fInvalidateBufferSubData;
194         GrGLFunction<GrGLInvalidateFramebufferFn> fInvalidateFramebuffer;
195         GrGLFunction<GrGLInvalidateSubFramebufferFn> fInvalidateSubFramebuffer;
196         GrGLFunction<GrGLInvalidateTexImageFn> fInvalidateTexImage;
197         GrGLFunction<GrGLInvalidateTexSubImageFn> fInvalidateTexSubImage;
198         GrGLFunction<GrGLIsTextureFn> fIsTexture;
199         GrGLFunction<GrGLLineWidthFn> fLineWidth;
200         GrGLFunction<GrGLLinkProgramFn> fLinkProgram;
201         GrGLFunction<GrGLProgramBinaryFn> fProgramBinary;
202         GrGLFunction<GrGLProgramParameteriFn> fProgramParameteri;
203         GrGLFunction<GrGLMapBufferFn> fMapBuffer;
204         GrGLFunction<GrGLMapBufferRangeFn> fMapBufferRange;
205         GrGLFunction<GrGLMapBufferSubDataFn> fMapBufferSubData;
206         GrGLFunction<GrGLMapTexSubImage2DFn> fMapTexSubImage2D;
207         GrGLFunction<GrGLMemoryBarrierFn> fMemoryBarrier;
208         GrGLFunction<GrGLDrawArraysInstancedBaseInstanceFn> fDrawArraysInstancedBaseInstance;
209         GrGLFunction<GrGLDrawElementsInstancedBaseVertexBaseInstanceFn> fDrawElementsInstancedBaseVertexBaseInstance;
210         GrGLFunction<GrGLMultiDrawArraysIndirectFn> fMultiDrawArraysIndirect;
211         GrGLFunction<GrGLMultiDrawElementsIndirectFn> fMultiDrawElementsIndirect;
212         GrGLFunction<GrGLMultiDrawArraysInstancedBaseInstanceFn> fMultiDrawArraysInstancedBaseInstance;
213         GrGLFunction<GrGLMultiDrawElementsInstancedBaseVertexBaseInstanceFn> fMultiDrawElementsInstancedBaseVertexBaseInstance;
214         GrGLFunction<GrGLPatchParameteriFn> fPatchParameteri;
215         GrGLFunction<GrGLPixelStoreiFn> fPixelStorei;
216         GrGLFunction<GrGLPolygonModeFn> fPolygonMode;
217         GrGLFunction<GrGLPopGroupMarkerFn> fPopGroupMarker;
218         GrGLFunction<GrGLPushGroupMarkerFn> fPushGroupMarker;
219         GrGLFunction<GrGLQueryCounterFn> fQueryCounter;
220         GrGLFunction<GrGLReadBufferFn> fReadBuffer;
221         GrGLFunction<GrGLReadPixelsFn> fReadPixels;
222         GrGLFunction<GrGLRenderbufferStorageFn> fRenderbufferStorage;
223 
224         //  On OpenGL ES there are multiple incompatible extensions that add support for MSAA
225         //  and ES3 adds MSAA support to the standard. On an ES3 driver we may still use the
226         //  older extensions for performance reasons or due to ES3 driver bugs. We want the function
227         //  that creates the GrGLInterface to provide all available functions and internally
228         //  we will select among them. They all have a method called glRenderbufferStorageMultisample*.
229         //  So we have separate function pointers for GL_IMG/EXT_multisampled_to_texture,
230         //  GL_CHROMIUM/ANGLE_framebuffer_multisample/ES3, and GL_APPLE_framebuffer_multisample
231         //  variations.
232         //
233         //  If a driver supports multiple GL_ARB_framebuffer_multisample-style extensions then we will
234         //  assume the function pointers for the standard (or equivalent GL_ARB) version have
235         //  been preferred over GL_EXT, GL_CHROMIUM, or GL_ANGLE variations that have reduced
236         //  functionality.
237 
238         //  GL_EXT_multisampled_render_to_texture (preferred) or GL_IMG_multisampled_render_to_texture
239         GrGLFunction<GrGLRenderbufferStorageMultisampleFn> fRenderbufferStorageMultisampleES2EXT;
240         //  GL_APPLE_framebuffer_multisample
241         GrGLFunction<GrGLRenderbufferStorageMultisampleFn> fRenderbufferStorageMultisampleES2APPLE;
242 
243         //  This is used to store the pointer for GL_ARB/EXT/ANGLE/CHROMIUM_framebuffer_multisample or
244         //  the standard function in ES3+ or GL 3.0+.
245         GrGLFunction<GrGLRenderbufferStorageMultisampleFn> fRenderbufferStorageMultisample;
246 
247         // Pointer to BindUniformLocationCHROMIUM from the GL_CHROMIUM_bind_uniform_location extension.
248         GrGLFunction<GrGLBindUniformLocationFn> fBindUniformLocation;
249 
250         GrGLFunction<GrGLResolveMultisampleFramebufferFn> fResolveMultisampleFramebuffer;
251         GrGLFunction<GrGLSamplerParameterfFn> fSamplerParameterf;
252         GrGLFunction<GrGLSamplerParameteriFn> fSamplerParameteri;
253         GrGLFunction<GrGLSamplerParameterivFn> fSamplerParameteriv;
254         GrGLFunction<GrGLScissorFn> fScissor;
255         GrGLFunction<GrGLSetFenceFn> fSetFence;
256         GrGLFunction<GrGLShaderSourceFn> fShaderSource;
257         GrGLFunction<GrGLStencilFuncFn> fStencilFunc;
258         GrGLFunction<GrGLStencilFuncSeparateFn> fStencilFuncSeparate;
259         GrGLFunction<GrGLStencilMaskFn> fStencilMask;
260         GrGLFunction<GrGLStencilMaskSeparateFn> fStencilMaskSeparate;
261         GrGLFunction<GrGLStencilOpFn> fStencilOp;
262         GrGLFunction<GrGLStencilOpSeparateFn> fStencilOpSeparate;
263         GrGLFunction<GrGLTestFenceFn> fTestFence;
264         GrGLFunction<GrGLTexBufferFn> fTexBuffer;
265         GrGLFunction<GrGLTexBufferRangeFn> fTexBufferRange;
266         GrGLFunction<GrGLTexImage2DFn> fTexImage2D;
267         GrGLFunction<GrGLTexParameterfFn> fTexParameterf;
268         GrGLFunction<GrGLTexParameterfvFn> fTexParameterfv;
269         GrGLFunction<GrGLTexParameteriFn> fTexParameteri;
270         GrGLFunction<GrGLTexParameterivFn> fTexParameteriv;
271         GrGLFunction<GrGLTexSubImage2DFn> fTexSubImage2D;
272         GrGLFunction<GrGLTexStorage2DFn> fTexStorage2D;
273         GrGLFunction<GrGLTextureBarrierFn> fTextureBarrier;
274         GrGLFunction<GrGLDiscardFramebufferFn> fDiscardFramebuffer;
275         GrGLFunction<GrGLUniform1fFn> fUniform1f;
276         GrGLFunction<GrGLUniform1iFn> fUniform1i;
277         GrGLFunction<GrGLUniform1fvFn> fUniform1fv;
278         GrGLFunction<GrGLUniform1ivFn> fUniform1iv;
279         GrGLFunction<GrGLUniform2fFn> fUniform2f;
280         GrGLFunction<GrGLUniform2iFn> fUniform2i;
281         GrGLFunction<GrGLUniform2fvFn> fUniform2fv;
282         GrGLFunction<GrGLUniform2ivFn> fUniform2iv;
283         GrGLFunction<GrGLUniform3fFn> fUniform3f;
284         GrGLFunction<GrGLUniform3iFn> fUniform3i;
285         GrGLFunction<GrGLUniform3fvFn> fUniform3fv;
286         GrGLFunction<GrGLUniform3ivFn> fUniform3iv;
287         GrGLFunction<GrGLUniform4fFn> fUniform4f;
288         GrGLFunction<GrGLUniform4iFn> fUniform4i;
289         GrGLFunction<GrGLUniform4fvFn> fUniform4fv;
290         GrGLFunction<GrGLUniform4ivFn> fUniform4iv;
291         GrGLFunction<GrGLUniformMatrix2fvFn> fUniformMatrix2fv;
292         GrGLFunction<GrGLUniformMatrix3fvFn> fUniformMatrix3fv;
293         GrGLFunction<GrGLUniformMatrix4fvFn> fUniformMatrix4fv;
294         GrGLFunction<GrGLUnmapBufferFn> fUnmapBuffer;
295         GrGLFunction<GrGLUnmapBufferSubDataFn> fUnmapBufferSubData;
296         GrGLFunction<GrGLUnmapTexSubImage2DFn> fUnmapTexSubImage2D;
297         GrGLFunction<GrGLUseProgramFn> fUseProgram;
298         GrGLFunction<GrGLVertexAttrib1fFn> fVertexAttrib1f;
299         GrGLFunction<GrGLVertexAttrib2fvFn> fVertexAttrib2fv;
300         GrGLFunction<GrGLVertexAttrib3fvFn> fVertexAttrib3fv;
301         GrGLFunction<GrGLVertexAttrib4fvFn> fVertexAttrib4fv;
302         GrGLFunction<GrGLVertexAttribDivisorFn> fVertexAttribDivisor;
303         GrGLFunction<GrGLVertexAttribIPointerFn> fVertexAttribIPointer;
304         GrGLFunction<GrGLVertexAttribPointerFn> fVertexAttribPointer;
305         GrGLFunction<GrGLViewportFn> fViewport;
306 
307         /* ARB_sync */
308         GrGLFunction<GrGLFenceSyncFn> fFenceSync;
309         GrGLFunction<GrGLIsSyncFn> fIsSync;
310         GrGLFunction<GrGLClientWaitSyncFn> fClientWaitSync;
311         GrGLFunction<GrGLWaitSyncFn> fWaitSync;
312         GrGLFunction<GrGLDeleteSyncFn> fDeleteSync;
313 
314         /* ARB_internalforamt_query */
315         GrGLFunction<GrGLGetInternalformativFn> fGetInternalformativ;
316 
317         /* KHR_debug */
318         GrGLFunction<GrGLDebugMessageControlFn> fDebugMessageControl;
319         GrGLFunction<GrGLDebugMessageInsertFn> fDebugMessageInsert;
320         GrGLFunction<GrGLDebugMessageCallbackFn> fDebugMessageCallback;
321         GrGLFunction<GrGLGetDebugMessageLogFn> fGetDebugMessageLog;
322         GrGLFunction<GrGLPushDebugGroupFn> fPushDebugGroup;
323         GrGLFunction<GrGLPopDebugGroupFn> fPopDebugGroup;
324         GrGLFunction<GrGLObjectLabelFn> fObjectLabel;
325 
326         /* EXT_window_rectangles */
327         GrGLFunction<GrGLWindowRectanglesFn> fWindowRectangles;
328 
329         /* GL_QCOM_tiled_rendering */
330         GrGLFunction<GrGLStartTilingFn> fStartTiling;
331         GrGLFunction<GrGLEndTilingFn> fEndTiling;
332     } fFunctions;
333 
334 #if GR_TEST_UTILS
335     // This exists for internal testing.
336     virtual void abandon() const;
337 #endif
338 };
339 
340 #endif
341