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
19class SkGLContextHelper : public SkRefCnt {
20public:
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
31    int getFBOID() const { return fFBO; }
32
33    const GrGLInterface* gl() const { return fGL; }
34
35    virtual void makeCurrent() const = 0;
36
37    bool hasExtension(const char* extensionName) const {
38        GrAssert(NULL != fGL);
39        return fExtensions.has(extensionName);
40    }
41
42protected:
43    /**
44     * Subclass implements this to make a GL context. The returned GrGLInterface
45     * should be populated with functions compatible with the context. The
46     * format and size of backbuffers does not matter since an FBO will be
47     * created.
48     */
49    virtual const GrGLInterface* createGLContext() = 0;
50
51    /**
52     * Subclass should destroy the underlying GL context.
53     */
54    virtual void destroyGLContext() = 0;
55
56private:
57    GrGLExtensions fExtensions;
58    GrGLuint fFBO;
59    GrGLuint fColorBufferID;
60    GrGLuint fDepthStencilBufferID;
61    const GrGLInterface* fGL;
62
63    typedef SkRefCnt INHERITED;
64};
65
66/**
67 * Helper macros for using the GL context through the GrGLInterface. Example:
68 * SK_GL(glCtx, GenTextures(1, &texID));
69 */
70#define SK_GL(ctx, X) (ctx).gl()->f ## X;    \
71                      SkASSERT(GR_GL_NO_ERROR == (ctx).gl()->fGetError())
72#define SK_GL_RET(ctx, RET, X) (RET) = (ctx).gl()->f ## X;    \
73                  SkASSERT(GR_GL_NO_ERROR == (ctx).gl()->fGetError())
74#define SK_GL_NOERRCHECK(ctx, X) (ctx).gl()->f ## X
75#define SK_GL_RET_NOERRCHECK(ctx, RET, X) (RET) = (ctx).gl()->f ## X
76
77#endif
78