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 // Deprecated alternative to GrGLMakeNativeInterface().
35 SK_API const GrGLInterface* GrGLCreateNativeInterface();
36 
37 /**
38  * GrContext uses the following interface to make all calls into OpenGL. When a
39  * GrContext is created it is given a GrGLInterface. The interface's function
40  * pointers must be valid for the OpenGL context associated with the GrContext.
41  * On some platforms, such as Windows, function pointers for OpenGL extensions
42  * may vary between OpenGL contexts. So the caller must be careful to use a
43  * GrGLInterface initialized for the correct context. All functions that should
44  * be available based on the OpenGL's version and extension string must be
45  * non-NULL or GrContext creation will fail. This can be tested with the
46  * validate() method when the OpenGL context has been made current.
47  */
48 struct SK_API GrGLInterface : public SkRefCnt {
49 private:
50     using INHERITED = SkRefCnt;
51 
52 #if GR_GL_CHECK_ERROR
53     // This is here to avoid having our debug code that checks for a GL error after most GL calls
54     // accidentally swallow an OOM that should be reported.
55     mutable bool fOOMed = false;
56     bool fSuppressErrorLogging = false;
57 #endif
58 
59 public:
60     GrGLInterface();
61 
62     // Validates that the GrGLInterface supports its advertised standard. This means the necessary
63     // function pointers have been initialized for both the GL version and any advertised
64     // extensions.
65     bool validate() const;
66 
67 #if GR_GL_CHECK_ERROR
68     GrGLenum checkError(const char* location, const char* call) const;
69     bool checkAndResetOOMed() const;
70     void suppressErrorLogging();
71 #endif
72 
73 #if GR_TEST_UTILS
GrGLInterfaceGrGLInterface74     GrGLInterface(const GrGLInterface& that)
75             : fStandard(that.fStandard)
76             , fExtensions(that.fExtensions)
77             , fFunctions(that.fFunctions) {}
78 #endif
79 
80     // Indicates the type of GL implementation
81     union {
82         GrGLStandard fStandard;
83         GrGLStandard fBindingsExported; // Legacy name, will be remove when Chromium is updated.
84     };
85 
86     GrGLExtensions fExtensions;
87 
hasExtensionGrGLInterface88     bool hasExtension(const char ext[]) const { return fExtensions.has(ext); }
89 
90     /**
91      * The function pointers are in a struct so that we can have a compiler generated assignment
92      * operator.
93      */
94     struct Functions {
95         GrGLFunction<GrGLActiveTextureFn> fActiveTexture;
96         GrGLFunction<GrGLAttachShaderFn> fAttachShader;
97         GrGLFunction<GrGLBeginQueryFn> fBeginQuery;
98         GrGLFunction<GrGLBindAttribLocationFn> fBindAttribLocation;
99         GrGLFunction<GrGLBindBufferFn> fBindBuffer;
100         GrGLFunction<GrGLBindFragDataLocationFn> fBindFragDataLocation;
101         GrGLFunction<GrGLBindFragDataLocationIndexedFn> fBindFragDataLocationIndexed;
102         GrGLFunction<GrGLBindFramebufferFn> fBindFramebuffer;
103         GrGLFunction<GrGLBindRenderbufferFn> fBindRenderbuffer;
104         GrGLFunction<GrGLBindSamplerFn> fBindSampler;
105         GrGLFunction<GrGLBindTextureFn> fBindTexture;
106         GrGLFunction<GrGLBindVertexArrayFn> fBindVertexArray;
107         GrGLFunction<GrGLBlendBarrierFn> fBlendBarrier;
108         GrGLFunction<GrGLBlendColorFn> fBlendColor;
109         GrGLFunction<GrGLBlendEquationFn> fBlendEquation;
110         GrGLFunction<GrGLBlendFuncFn> fBlendFunc;
111         GrGLFunction<GrGLBlitFramebufferFn> fBlitFramebuffer;
112         GrGLFunction<GrGLBufferDataFn> fBufferData;
113         GrGLFunction<GrGLBufferSubDataFn> fBufferSubData;
114         GrGLFunction<GrGLCheckFramebufferStatusFn> fCheckFramebufferStatus;
115         GrGLFunction<GrGLClearFn> fClear;
116         GrGLFunction<GrGLClearColorFn> fClearColor;
117         GrGLFunction<GrGLClearStencilFn> fClearStencil;
118         GrGLFunction<GrGLClearTexImageFn> fClearTexImage;
119         GrGLFunction<GrGLClearTexSubImageFn> fClearTexSubImage;
120         GrGLFunction<GrGLColorMaskFn> fColorMask;
121         GrGLFunction<GrGLCompileShaderFn> fCompileShader;
122         GrGLFunction<GrGLCompressedTexImage2DFn> fCompressedTexImage2D;
123         GrGLFunction<GrGLCompressedTexSubImage2DFn> fCompressedTexSubImage2D;
124         GrGLFunction<GrGLCopyTexSubImage2DFn> fCopyTexSubImage2D;
125         GrGLFunction<GrGLCreateProgramFn> fCreateProgram;
126         GrGLFunction<GrGLCreateShaderFn> fCreateShader;
127         GrGLFunction<GrGLCullFaceFn> fCullFace;
128         GrGLFunction<GrGLDeleteBuffersFn> fDeleteBuffers;
129         GrGLFunction<GrGLDeleteFencesFn> fDeleteFences;
130         GrGLFunction<GrGLDeleteFramebuffersFn> fDeleteFramebuffers;
131         GrGLFunction<GrGLDeleteProgramFn> fDeleteProgram;
132         GrGLFunction<GrGLDeleteQueriesFn> fDeleteQueries;
133         GrGLFunction<GrGLDeleteRenderbuffersFn> fDeleteRenderbuffers;
134         GrGLFunction<GrGLDeleteSamplersFn> fDeleteSamplers;
135         GrGLFunction<GrGLDeleteShaderFn> fDeleteShader;
136         GrGLFunction<GrGLDeleteTexturesFn> fDeleteTextures;
137         GrGLFunction<GrGLDeleteVertexArraysFn> fDeleteVertexArrays;
138         GrGLFunction<GrGLDepthMaskFn> fDepthMask;
139         GrGLFunction<GrGLDisableFn> fDisable;
140         GrGLFunction<GrGLDisableVertexAttribArrayFn> fDisableVertexAttribArray;
141         GrGLFunction<GrGLDrawArraysFn> fDrawArrays;
142         GrGLFunction<GrGLDrawArraysIndirectFn> fDrawArraysIndirect;
143         GrGLFunction<GrGLDrawArraysInstancedFn> fDrawArraysInstanced;
144         GrGLFunction<GrGLDrawBufferFn> fDrawBuffer;
145         GrGLFunction<GrGLDrawBuffersFn> fDrawBuffers;
146         GrGLFunction<GrGLDrawElementsFn> fDrawElements;
147         GrGLFunction<GrGLDrawElementsIndirectFn> fDrawElementsIndirect;
148         GrGLFunction<GrGLDrawElementsInstancedFn> fDrawElementsInstanced;
149         GrGLFunction<GrGLDrawRangeElementsFn> fDrawRangeElements;
150         GrGLFunction<GrGLEnableFn> fEnable;
151         GrGLFunction<GrGLEnableVertexAttribArrayFn> fEnableVertexAttribArray;
152         GrGLFunction<GrGLEndQueryFn> fEndQuery;
153         GrGLFunction<GrGLFinishFn> fFinish;
154         GrGLFunction<GrGLFinishFenceFn> fFinishFence;
155         GrGLFunction<GrGLFlushFn> fFlush;
156         GrGLFunction<GrGLFlushMappedBufferRangeFn> fFlushMappedBufferRange;
157         GrGLFunction<GrGLFramebufferRenderbufferFn> fFramebufferRenderbuffer;
158         GrGLFunction<GrGLFramebufferTexture2DFn> fFramebufferTexture2D;
159         GrGLFunction<GrGLFramebufferTexture2DMultisampleFn> fFramebufferTexture2DMultisample;
160         GrGLFunction<GrGLFrontFaceFn> fFrontFace;
161         GrGLFunction<GrGLGenBuffersFn> fGenBuffers;
162         GrGLFunction<GrGLGenFencesFn> fGenFences;
163         GrGLFunction<GrGLGenFramebuffersFn> fGenFramebuffers;
164         GrGLFunction<GrGLGenerateMipmapFn> fGenerateMipmap;
165         GrGLFunction<GrGLGenQueriesFn> fGenQueries;
166         GrGLFunction<GrGLGenRenderbuffersFn> fGenRenderbuffers;
167         GrGLFunction<GrGLGenSamplersFn> fGenSamplers;
168         GrGLFunction<GrGLGenTexturesFn> fGenTextures;
169         GrGLFunction<GrGLGenVertexArraysFn> fGenVertexArrays;
170         GrGLFunction<GrGLGetBufferParameterivFn> fGetBufferParameteriv;
171         GrGLFunction<GrGLGetErrorFn> fGetError;
172         GrGLFunction<GrGLGetFramebufferAttachmentParameterivFn> fGetFramebufferAttachmentParameteriv;
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<GrGLSamplerParameteriFn> fSamplerParameteri;
252         GrGLFunction<GrGLSamplerParameterivFn> fSamplerParameteriv;
253         GrGLFunction<GrGLScissorFn> fScissor;
254         GrGLFunction<GrGLSetFenceFn> fSetFence;
255         GrGLFunction<GrGLShaderSourceFn> fShaderSource;
256         GrGLFunction<GrGLStencilFuncFn> fStencilFunc;
257         GrGLFunction<GrGLStencilFuncSeparateFn> fStencilFuncSeparate;
258         GrGLFunction<GrGLStencilMaskFn> fStencilMask;
259         GrGLFunction<GrGLStencilMaskSeparateFn> fStencilMaskSeparate;
260         GrGLFunction<GrGLStencilOpFn> fStencilOp;
261         GrGLFunction<GrGLStencilOpSeparateFn> fStencilOpSeparate;
262         GrGLFunction<GrGLTestFenceFn> fTestFence;
263         GrGLFunction<GrGLTexBufferFn> fTexBuffer;
264         GrGLFunction<GrGLTexBufferRangeFn> fTexBufferRange;
265         GrGLFunction<GrGLTexImage2DFn> fTexImage2D;
266         GrGLFunction<GrGLTexParameterfFn> fTexParameterf;
267         GrGLFunction<GrGLTexParameterfvFn> fTexParameterfv;
268         GrGLFunction<GrGLTexParameteriFn> fTexParameteri;
269         GrGLFunction<GrGLTexParameterivFn> fTexParameteriv;
270         GrGLFunction<GrGLTexSubImage2DFn> fTexSubImage2D;
271         GrGLFunction<GrGLTexStorage2DFn> fTexStorage2D;
272         GrGLFunction<GrGLTextureBarrierFn> fTextureBarrier;
273         GrGLFunction<GrGLDiscardFramebufferFn> fDiscardFramebuffer;
274         GrGLFunction<GrGLUniform1fFn> fUniform1f;
275         GrGLFunction<GrGLUniform1iFn> fUniform1i;
276         GrGLFunction<GrGLUniform1fvFn> fUniform1fv;
277         GrGLFunction<GrGLUniform1ivFn> fUniform1iv;
278         GrGLFunction<GrGLUniform2fFn> fUniform2f;
279         GrGLFunction<GrGLUniform2iFn> fUniform2i;
280         GrGLFunction<GrGLUniform2fvFn> fUniform2fv;
281         GrGLFunction<GrGLUniform2ivFn> fUniform2iv;
282         GrGLFunction<GrGLUniform3fFn> fUniform3f;
283         GrGLFunction<GrGLUniform3iFn> fUniform3i;
284         GrGLFunction<GrGLUniform3fvFn> fUniform3fv;
285         GrGLFunction<GrGLUniform3ivFn> fUniform3iv;
286         GrGLFunction<GrGLUniform4fFn> fUniform4f;
287         GrGLFunction<GrGLUniform4iFn> fUniform4i;
288         GrGLFunction<GrGLUniform4fvFn> fUniform4fv;
289         GrGLFunction<GrGLUniform4ivFn> fUniform4iv;
290         GrGLFunction<GrGLUniformMatrix2fvFn> fUniformMatrix2fv;
291         GrGLFunction<GrGLUniformMatrix3fvFn> fUniformMatrix3fv;
292         GrGLFunction<GrGLUniformMatrix4fvFn> fUniformMatrix4fv;
293         GrGLFunction<GrGLUnmapBufferFn> fUnmapBuffer;
294         GrGLFunction<GrGLUnmapBufferSubDataFn> fUnmapBufferSubData;
295         GrGLFunction<GrGLUnmapTexSubImage2DFn> fUnmapTexSubImage2D;
296         GrGLFunction<GrGLUseProgramFn> fUseProgram;
297         GrGLFunction<GrGLVertexAttrib1fFn> fVertexAttrib1f;
298         GrGLFunction<GrGLVertexAttrib2fvFn> fVertexAttrib2fv;
299         GrGLFunction<GrGLVertexAttrib3fvFn> fVertexAttrib3fv;
300         GrGLFunction<GrGLVertexAttrib4fvFn> fVertexAttrib4fv;
301         GrGLFunction<GrGLVertexAttribDivisorFn> fVertexAttribDivisor;
302         GrGLFunction<GrGLVertexAttribIPointerFn> fVertexAttribIPointer;
303         GrGLFunction<GrGLVertexAttribPointerFn> fVertexAttribPointer;
304         GrGLFunction<GrGLViewportFn> fViewport;
305 
306         /* NV_framebuffer_mixed_samples */
307         GrGLFunction<GrGLCoverageModulationFn> fCoverageModulation;
308 
309         /* ARB_sync */
310         GrGLFunction<GrGLFenceSyncFn> fFenceSync;
311         GrGLFunction<GrGLIsSyncFn> fIsSync;
312         GrGLFunction<GrGLClientWaitSyncFn> fClientWaitSync;
313         GrGLFunction<GrGLWaitSyncFn> fWaitSync;
314         GrGLFunction<GrGLDeleteSyncFn> fDeleteSync;
315 
316         /* ARB_internalforamt_query */
317         GrGLFunction<GrGLGetInternalformativFn> fGetInternalformativ;
318 
319         /* KHR_debug */
320         GrGLFunction<GrGLDebugMessageControlFn> fDebugMessageControl;
321         GrGLFunction<GrGLDebugMessageInsertFn> fDebugMessageInsert;
322         GrGLFunction<GrGLDebugMessageCallbackFn> fDebugMessageCallback;
323         GrGLFunction<GrGLGetDebugMessageLogFn> fGetDebugMessageLog;
324         GrGLFunction<GrGLPushDebugGroupFn> fPushDebugGroup;
325         GrGLFunction<GrGLPopDebugGroupFn> fPopDebugGroup;
326         GrGLFunction<GrGLObjectLabelFn> fObjectLabel;
327 
328         /* EXT_window_rectangles */
329         GrGLFunction<GrGLWindowRectanglesFn> fWindowRectangles;
330 
331         /* GL_QCOM_tiled_rendering */
332         GrGLFunction<GrGLStartTilingFn> fStartTiling;
333         GrGLFunction<GrGLEndTilingFn> fEndTiling;
334     } fFunctions;
335 
336 #if GR_TEST_UTILS
337     // This exists for internal testing.
338     virtual void abandon() const;
339 #endif
340 };
341 
342 #endif
343