• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /*
3  * Copyright 2013 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 #ifndef SkGLContextHelper_DEFINED
9 #define SkGLContextHelper_DEFINED
10 
11 #include "GrGLExtensions.h"
12 #include "GrGLInterface.h"
13 
14 /**
15  * Create an offscreen opengl context with an RGBA8 / 8bit stencil FBO.
16  * Provides a GrGLInterface struct of function pointers for the context.
17  */
18 
19 class SkGLContextHelper : public SkRefCnt {
20 public:
21     SK_DECLARE_INST_COUNT(SkGLContextHelper)
22 
23     SkGLContextHelper();
24     virtual ~SkGLContextHelper();
25 
26     /**
27      * Initializes the context and makes it current.
28      */
29     bool init(const int width, const int height);
30 
getFBOID()31     int getFBOID() const { return fFBO; }
32 
gl()33     const GrGLInterface* gl() const { return fGL; }
34 
35     virtual void makeCurrent() const = 0;
36 
37     /**
38      * The primary purpose of this function it to provide a means of scheduling
39      * work on the GPU (since all of the subclasses create primary buffers for
40      * testing that are small and not meant to be rendered to the screen).
41      *
42      * If the drawing surface provided by the platform is double buffered this
43      * call will cause the platform to swap which buffer is currently being
44      * targeted.  If the current surface does not include a back buffer, this
45      * call has no effect.
46      */
47     virtual void swapBuffers() const = 0;
48 
hasExtension(const char * extensionName)49     bool hasExtension(const char* extensionName) const {
50         SkASSERT(NULL != fGL);
51         return fExtensions.has(extensionName);
52     }
53 
54 protected:
55     /**
56      * Subclass implements this to make a GL context. The returned GrGLInterface
57      * should be populated with functions compatible with the context. The
58      * format and size of backbuffers does not matter since an FBO will be
59      * created.
60      */
61     virtual const GrGLInterface* createGLContext() = 0;
62 
63     /**
64      * Subclass should destroy the underlying GL context.
65      */
66     virtual void destroyGLContext() = 0;
67 
68 private:
69     GrGLExtensions fExtensions;
70     GrGLuint fFBO;
71     GrGLuint fColorBufferID;
72     GrGLuint fDepthStencilBufferID;
73     const GrGLInterface* fGL;
74 
75     typedef SkRefCnt INHERITED;
76 };
77 
78 /**
79  * Helper macros for using the GL context through the GrGLInterface. Example:
80  * SK_GL(glCtx, GenTextures(1, &texID));
81  */
82 #define SK_GL(ctx, X) (ctx).gl()->f ## X;    \
83                       SkASSERT(GR_GL_NO_ERROR == (ctx).gl()->fGetError())
84 #define SK_GL_RET(ctx, RET, X) (RET) = (ctx).gl()->f ## X;    \
85                   SkASSERT(GR_GL_NO_ERROR == (ctx).gl()->fGetError())
86 #define SK_GL_NOERRCHECK(ctx, X) (ctx).gl()->f ## X
87 #define SK_GL_RET_NOERRCHECK(ctx, RET, X) (RET) = (ctx).gl()->f ## X
88 
89 #endif
90