blob: c4d9bdf9979c4165cd67f404d708711b0f716291 [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 */
bsalomon10805962014-10-08 04:45:09 -07008#ifndef SkGLContextHelper_DEFINED
9#define SkGLContextHelper_DEFINED
reed@google.comc31ce102010-12-21 16:26:39 +000010
bsalomon@google.com373a6632011-10-19 20:43:20 +000011#include "GrGLInterface.h"
reed@google.comc31ce102010-12-21 16:26:39 +000012
reed@google.com873cb1e2010-12-23 15:00:45 +000013/**
bsalomon@google.com373a6632011-10-19 20:43:20 +000014 * Create an offscreen opengl context with an RGBA8 / 8bit stencil FBO.
15 * Provides a GrGLInterface struct of function pointers for the context.
reed@google.com873cb1e2010-12-23 15:00:45 +000016 */
bsalomon@google.com373a6632011-10-19 20:43:20 +000017
bsalomon10805962014-10-08 04:45:09 -070018class SK_API SkGLContextHelper : public SkRefCnt {
reed@google.comc31ce102010-12-21 16:26:39 +000019public:
bsalomon10805962014-10-08 04:45:09 -070020 SK_DECLARE_INST_COUNT(SkGLContextHelper)
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +000021
bsalomon10805962014-10-08 04:45:09 -070022 SkGLContextHelper();
23 virtual ~SkGLContextHelper();
reed@google.comc31ce102010-12-21 16:26:39 +000024
bsalomon@google.com57f5d982011-10-24 21:17:53 +000025 /**
26 * Initializes the context and makes it current.
27 */
kkinnunen80549fc2014-06-30 06:36:31 -070028 bool init(GrGLStandard forcedGpuAPI, const int width, const int height);
reed@google.comc31ce102010-12-21 16:26:39 +000029
bsalomon@google.com971d0c82011-08-19 17:22:05 +000030 int getFBOID() const { return fFBO; }
31
bsalomon@google.com373a6632011-10-19 20:43:20 +000032 const GrGLInterface* gl() const { return fGL; }
bungeman@google.com16bab872011-05-17 14:24:46 +000033
bsalomon@google.com373a6632011-10-19 20:43:20 +000034 virtual void makeCurrent() const = 0;
35
djsollen@google.comc9542ca2013-10-09 18:25:38 +000036 /**
37 * The primary purpose of this function it to provide a means of scheduling
38 * work on the GPU (since all of the subclasses create primary buffers for
39 * testing that are small and not meant to be rendered to the screen).
40 *
41 * If the drawing surface provided by the platform is double buffered this
42 * call will cause the platform to swap which buffer is currently being
43 * targeted. If the current surface does not include a back buffer, this
44 * call has no effect.
45 */
46 virtual void swapBuffers() const = 0;
47
bsalomon@google.com1744f972013-02-26 21:46:32 +000048 bool hasExtension(const char* extensionName) const {
bsalomon49f085d2014-09-05 13:34:00 -070049 SkASSERT(fGL);
commit-bot@chromium.org90313cc2014-01-17 15:05:38 +000050 return fGL->hasExtension(extensionName);
bsalomon@google.com1744f972013-02-26 21:46:32 +000051 }
bsalomon@google.com6e859372012-02-09 15:25:13 +000052
bsalomon944bcf02014-07-29 08:01:52 -070053 /**
54 * This notifies the context that we are deliberately testing abandoning
55 * the context. It is useful for debugging contexts that would otherwise
56 * test that GPU resources are properly deleted. It also allows a debugging
57 * context to test that further GL calls are not made by Skia GPU code.
58 */
59 void testAbandon();
60
bsalomon@google.com373a6632011-10-19 20:43:20 +000061protected:
62 /**
rmistry@google.comfbfcd562012-08-23 18:09:54 +000063 * Subclass implements this to make a GL context. The returned GrGLInterface
64 * should be populated with functions compatible with the context. The
bsalomon@google.com373a6632011-10-19 20:43:20 +000065 * format and size of backbuffers does not matter since an FBO will be
66 * created.
67 */
kkinnunen80549fc2014-06-30 06:36:31 -070068 virtual const GrGLInterface* createGLContext(GrGLStandard forcedGpuAPI) = 0;
bsalomon@google.com373a6632011-10-19 20:43:20 +000069
70 /**
71 * Subclass should destroy the underlying GL context.
72 */
73 virtual void destroyGLContext() = 0;
74
75private:
76 GrGLuint fFBO;
robertphillips@google.com7c959422012-03-22 20:43:56 +000077 GrGLuint fColorBufferID;
78 GrGLuint fDepthStencilBufferID;
bsalomon@google.com373a6632011-10-19 20:43:20 +000079 const GrGLInterface* fGL;
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +000080
81 typedef SkRefCnt INHERITED;
reed@google.comc31ce102010-12-21 16:26:39 +000082};
83
bsalomon@google.com373a6632011-10-19 20:43:20 +000084/**
robertphillips@google.comfe1b5362013-02-07 19:45:46 +000085 * Helper macros for using the GL context through the GrGLInterface. Example:
bsalomon@google.com373a6632011-10-19 20:43:20 +000086 * SK_GL(glCtx, GenTextures(1, &texID));
87 */
commit-bot@chromium.orgc72425a2014-01-21 16:09:18 +000088#define SK_GL(ctx, X) (ctx).gl()->fFunctions.f ## X; \
bsalomon9245b7e2014-07-01 07:20:11 -070089 SkASSERT(0 == (ctx).gl()->fFunctions.fGetError())
commit-bot@chromium.orgc72425a2014-01-21 16:09:18 +000090#define SK_GL_RET(ctx, RET, X) (RET) = (ctx).gl()->fFunctions.f ## X; \
bsalomon9245b7e2014-07-01 07:20:11 -070091 SkASSERT(0 == (ctx).gl()->fFunctions.fGetError())
commit-bot@chromium.orgc72425a2014-01-21 16:09:18 +000092#define SK_GL_NOERRCHECK(ctx, X) (ctx).gl()->fFunctions.f ## X
93#define SK_GL_RET_NOERRCHECK(ctx, RET, X) (RET) = (ctx).gl()->fFunctions.f ## X
bsalomon@google.com373a6632011-10-19 20:43:20 +000094
reed@google.com873cb1e2010-12-23 15:00:45 +000095#endif