blob: 3420a47973228594d4d1f8f6fc24cca9df685f3c [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 */
kkinnunen9e61bb72014-10-09 05:24:15 -07008#ifndef SkGLContext_DEFINED
9#define SkGLContext_DEFINED
reed@google.comc31ce102010-12-21 16:26:39 +000010
bsalomon@google.com373a6632011-10-19 20:43:20 +000011#include "GrGLInterface.h"
mtklein22355c42015-07-29 11:10:46 -070012#include "../private/SkGpuFenceSync.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.
bsalomon7ea33f52015-11-22 14:51:00 -080017 * This class is intended for Skia's testing needs and not for general
18 * use.
reed@google.com873cb1e2010-12-23 15:00:45 +000019 */
jcgregorio54e2ca52016-01-05 04:15:23 -080020class SK_API SkGLContext : public SkRefCnt {
reed@google.comc31ce102010-12-21 16:26:39 +000021public:
jcgregorio54e2ca52016-01-05 04:15:23 -080022 ~SkGLContext() override;
reed@google.comc31ce102010-12-21 16:26:39 +000023
kkinnunen30bc88c2014-10-15 23:03:54 -070024 bool isValid() const { return NULL != gl(); }
reed@google.comc31ce102010-12-21 16:26:39 +000025
kkinnunen30bc88c2014-10-15 23:03:54 -070026 const GrGLInterface* gl() const { return fGL.get(); }
bungeman@google.com16bab872011-05-17 14:24:46 +000027
cdaltond416a5b2015-06-23 13:23:44 -070028 bool fenceSyncSupport() const { return SkToBool(fFenceSync); }
29
30 bool getMaxGpuFrameLag(int* maxFrameLag) const {
31 if (!fFenceSync) {
32 return false;
33 }
34 *maxFrameLag = kMaxFrameLag;
35 return true;
36 }
37
38 void makeCurrent() const;
bsalomon@google.com373a6632011-10-19 20:43:20 +000039
bsalomon7ea33f52015-11-22 14:51:00 -080040 /** Used for testing EGLImage integration. Take a GL_TEXTURE_2D and wraps it in an EGL Image */
41 virtual GrEGLImage texture2DToEGLImage(GrGLuint /*texID*/) const { return 0; }
42 virtual void destroyEGLImage(GrEGLImage) const {}
43
44 /**
45 * Used for testing EGLImage integration. Takes a EGLImage and wraps it in a
46 * GL_TEXTURE_EXTERNAL_OES.
47 */
48 virtual GrGLuint eglImageToExternalTexture(GrEGLImage) const { return 0; }
49
djsollen@google.comc9542ca2013-10-09 18:25:38 +000050 /**
cdaltond416a5b2015-06-23 13:23:44 -070051 * The only purpose of this function it to provide a means of scheduling
djsollen@google.comc9542ca2013-10-09 18:25:38 +000052 * work on the GPU (since all of the subclasses create primary buffers for
53 * testing that are small and not meant to be rendered to the screen).
54 *
cdaltond416a5b2015-06-23 13:23:44 -070055 * If the platform supports fence sync (OpenGL 3.2+ or EGL_KHR_fence_sync),
56 * this will not swap any buffers, but rather emulate triple buffer
57 * synchronization using fences.
58 *
59 * Otherwise it will call the platform SwapBuffers method. This may or may
60 * not perform some sort of synchronization, depending on whether the
61 * drawing surface provided by the platform is double buffered.
djsollen@google.comc9542ca2013-10-09 18:25:38 +000062 */
cdaltond416a5b2015-06-23 13:23:44 -070063 void swapBuffers();
djsollen@google.comc9542ca2013-10-09 18:25:38 +000064
bsalomon944bcf02014-07-29 08:01:52 -070065 /**
66 * This notifies the context that we are deliberately testing abandoning
67 * the context. It is useful for debugging contexts that would otherwise
68 * test that GPU resources are properly deleted. It also allows a debugging
69 * context to test that further GL calls are not made by Skia GPU code.
70 */
71 void testAbandon();
72
bsalomon7ea33f52015-11-22 14:51:00 -080073 /**
74 * Creates a new GL context of the same type and makes the returned context current
75 * (if not null).
76 */
77 virtual SkGLContext* createNew() const { return nullptr; }
78
cdaltond416a5b2015-06-23 13:23:44 -070079 class GLFenceSync; // SkGpuFenceSync implementation that uses the OpenGL functionality.
80
bsalomon@google.com373a6632011-10-19 20:43:20 +000081protected:
kkinnunen30bc88c2014-10-15 23:03:54 -070082 SkGLContext();
bsalomon@google.com373a6632011-10-19 20:43:20 +000083
cdaltond416a5b2015-06-23 13:23:44 -070084 /*
85 * Methods that sublcasses must call from their constructors and destructors.
86 */
87 void init(const GrGLInterface*, SkGpuFenceSync* = NULL);
88 void teardown();
89
90 /*
91 * Operations that have a platform-dependent implementation.
92 */
93 virtual void onPlatformMakeCurrent() const = 0;
94 virtual void onPlatformSwapBuffers() const = 0;
95 virtual GrGLFuncPtr onPlatformGetProcAddress(const char*) const = 0;
96
97private:
98 enum { kMaxFrameLag = 3 };
99
100 SkAutoTDelete<SkGpuFenceSync> fFenceSync;
101 SkPlatformGpuFence fFrameFences[kMaxFrameLag - 1];
102 int fCurrentFenceIdx;
103
kkinnunen30bc88c2014-10-15 23:03:54 -0700104 /** Subclass provides the gl interface object if construction was
105 * successful. */
106 SkAutoTUnref<const GrGLInterface> fGL;
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +0000107
cdaltond416a5b2015-06-23 13:23:44 -0700108 friend class GLFenceSync; // For onPlatformGetProcAddress.
jcgregorio54e2ca52016-01-05 04:15:23 -0800109
110 typedef SkRefCnt INHERITED;
reed@google.comc31ce102010-12-21 16:26:39 +0000111};
112
kkinnunen9e61bb72014-10-09 05:24:15 -0700113/** Creates platform-dependent GL context object
kkinnunen30bc88c2014-10-15 23:03:54 -0700114 * Returns a valid gl context object or NULL if such can not be created.
kkinnunen9e61bb72014-10-09 05:24:15 -0700115 * Note: If Skia embedder needs a custom GL context that sets up the GL
116 * interface, this function should be implemented by the embedder.
117 * Otherwise, the default implementation for the platform should be compiled in
118 * the library.
119 */
kkinnunen30bc88c2014-10-15 23:03:54 -0700120SK_API SkGLContext* SkCreatePlatformGLContext(GrGLStandard forcedGpuAPI);
kkinnunen9e61bb72014-10-09 05:24:15 -0700121
bsalomon@google.com373a6632011-10-19 20:43:20 +0000122/**
robertphillips@google.comfe1b5362013-02-07 19:45:46 +0000123 * Helper macros for using the GL context through the GrGLInterface. Example:
bsalomon@google.com373a6632011-10-19 20:43:20 +0000124 * SK_GL(glCtx, GenTextures(1, &texID));
125 */
commit-bot@chromium.orgc72425a2014-01-21 16:09:18 +0000126#define SK_GL(ctx, X) (ctx).gl()->fFunctions.f ## X; \
bsalomon9245b7e2014-07-01 07:20:11 -0700127 SkASSERT(0 == (ctx).gl()->fFunctions.fGetError())
commit-bot@chromium.orgc72425a2014-01-21 16:09:18 +0000128#define SK_GL_RET(ctx, RET, X) (RET) = (ctx).gl()->fFunctions.f ## X; \
bsalomon9245b7e2014-07-01 07:20:11 -0700129 SkASSERT(0 == (ctx).gl()->fFunctions.fGetError())
commit-bot@chromium.orgc72425a2014-01-21 16:09:18 +0000130#define SK_GL_NOERRCHECK(ctx, X) (ctx).gl()->fFunctions.f ## X
131#define SK_GL_RET_NOERRCHECK(ctx, RET, X) (RET) = (ctx).gl()->fFunctions.f ## X
bsalomon@google.com373a6632011-10-19 20:43:20 +0000132
reed@google.com873cb1e2010-12-23 15:00:45 +0000133#endif