blob: 386a6958fb6e4aed8ec48978835b44fc80fa02a0 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
robertphillips@google.com6177e692013-02-28 20:16:25 +00003 * Copyright 2013 Google Inc.
epoger@google.comec3ed6a2011-07-28 14:26:00 +00004 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
robertphillips@google.com6177e692013-02-28 20:16:25 +00008#ifndef SkGLContextHelper_DEFINED
9#define SkGLContextHelper_DEFINED
reed@google.comc31ce102010-12-21 16:26:39 +000010
bsalomon@google.com1744f972013-02-26 21:46:32 +000011#include "GrGLExtensions.h"
bsalomon@google.com373a6632011-10-19 20:43:20 +000012#include "GrGLInterface.h"
reed@google.comc31ce102010-12-21 16:26:39 +000013
reed@google.com873cb1e2010-12-23 15:00:45 +000014/**
bsalomon@google.com373a6632011-10-19 20:43:20 +000015 * Create an offscreen opengl context with an RGBA8 / 8bit stencil FBO.
16 * Provides a GrGLInterface struct of function pointers for the context.
reed@google.com873cb1e2010-12-23 15:00:45 +000017 */
bsalomon@google.com373a6632011-10-19 20:43:20 +000018
robertphillips@google.com6177e692013-02-28 20:16:25 +000019class SkGLContextHelper : public SkRefCnt {
reed@google.comc31ce102010-12-21 16:26:39 +000020public:
robertphillips@google.com6177e692013-02-28 20:16:25 +000021 SK_DECLARE_INST_COUNT(SkGLContextHelper)
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +000022
robertphillips@google.com6177e692013-02-28 20:16:25 +000023 SkGLContextHelper();
24 virtual ~SkGLContextHelper();
reed@google.comc31ce102010-12-21 16:26:39 +000025
bsalomon@google.com57f5d982011-10-24 21:17:53 +000026 /**
27 * Initializes the context and makes it current.
28 */
bungeman@google.com0e454412011-05-19 17:47:02 +000029 bool init(const int width, const int height);
reed@google.comc31ce102010-12-21 16:26:39 +000030
bsalomon@google.com971d0c82011-08-19 17:22:05 +000031 int getFBOID() const { return fFBO; }
32
bsalomon@google.com373a6632011-10-19 20:43:20 +000033 const GrGLInterface* gl() const { return fGL; }
bungeman@google.com16bab872011-05-17 14:24:46 +000034
bsalomon@google.com373a6632011-10-19 20:43:20 +000035 virtual void makeCurrent() const = 0;
36
bsalomon@google.com1744f972013-02-26 21:46:32 +000037 bool hasExtension(const char* extensionName) const {
38 GrAssert(NULL != fGL);
39 return fExtensions.has(extensionName);
40 }
bsalomon@google.com6e859372012-02-09 15:25:13 +000041
bsalomon@google.com373a6632011-10-19 20:43:20 +000042protected:
43 /**
rmistry@google.comfbfcd562012-08-23 18:09:54 +000044 * Subclass implements this to make a GL context. The returned GrGLInterface
45 * should be populated with functions compatible with the context. The
bsalomon@google.com373a6632011-10-19 20:43:20 +000046 * 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:
bsalomon@google.com1744f972013-02-26 21:46:32 +000057 GrGLExtensions fExtensions;
bsalomon@google.com373a6632011-10-19 20:43:20 +000058 GrGLuint fFBO;
robertphillips@google.com7c959422012-03-22 20:43:56 +000059 GrGLuint fColorBufferID;
60 GrGLuint fDepthStencilBufferID;
bsalomon@google.com373a6632011-10-19 20:43:20 +000061 const GrGLInterface* fGL;
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +000062
63 typedef SkRefCnt INHERITED;
reed@google.comc31ce102010-12-21 16:26:39 +000064};
65
bsalomon@google.com373a6632011-10-19 20:43:20 +000066/**
robertphillips@google.comfe1b5362013-02-07 19:45:46 +000067 * Helper macros for using the GL context through the GrGLInterface. Example:
bsalomon@google.com373a6632011-10-19 20:43:20 +000068 * SK_GL(glCtx, GenTextures(1, &texID));
69 */
robertphillips@google.comfe1b5362013-02-07 19:45:46 +000070#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
bsalomon@google.com373a6632011-10-19 20:43:20 +000076
reed@google.com873cb1e2010-12-23 15:00:45 +000077#endif