blob: 992edf4efd2631f8ae540e7c0bf19480d7ceb518 [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 */
kkinnunen34058002016-01-06 23:49:30 -080020class SK_API SkGLContext : public SkNoncopyable {
reed@google.comc31ce102010-12-21 16:26:39 +000021public:
kkinnunen34058002016-01-06 23:49:30 -080022 virtual ~SkGLContext();
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
bsalomone5286e02016-01-14 09:24:09 -080044 /** Used for testing GL_TEXTURE_RECTANGLE integration. */
45 GrGLint createTextureRectangle(int width, int height, GrGLenum internalFormat,
46 GrGLenum externalFormat, GrGLenum externalType,
47 GrGLvoid* data);
48
bsalomon7ea33f52015-11-22 14:51:00 -080049 /**
50 * Used for testing EGLImage integration. Takes a EGLImage and wraps it in a
51 * GL_TEXTURE_EXTERNAL_OES.
52 */
53 virtual GrGLuint eglImageToExternalTexture(GrEGLImage) const { return 0; }
54
joshualitt01836ad2016-01-20 13:09:12 -080055 void swapBuffers();
56
djsollen@google.comc9542ca2013-10-09 18:25:38 +000057 /**
cdaltond416a5b2015-06-23 13:23:44 -070058 * The only purpose of this function it to provide a means of scheduling
djsollen@google.comc9542ca2013-10-09 18:25:38 +000059 * work on the GPU (since all of the subclasses create primary buffers for
60 * testing that are small and not meant to be rendered to the screen).
61 *
cdaltond416a5b2015-06-23 13:23:44 -070062 * If the platform supports fence sync (OpenGL 3.2+ or EGL_KHR_fence_sync),
63 * this will not swap any buffers, but rather emulate triple buffer
64 * synchronization using fences.
65 *
66 * Otherwise it will call the platform SwapBuffers method. This may or may
67 * not perform some sort of synchronization, depending on whether the
68 * drawing surface provided by the platform is double buffered.
djsollen@google.comc9542ca2013-10-09 18:25:38 +000069 */
joshualitt01836ad2016-01-20 13:09:12 -080070 void waitOnSyncOrSwap();
djsollen@google.comc9542ca2013-10-09 18:25:38 +000071
bsalomon944bcf02014-07-29 08:01:52 -070072 /**
73 * This notifies the context that we are deliberately testing abandoning
74 * the context. It is useful for debugging contexts that would otherwise
75 * test that GPU resources are properly deleted. It also allows a debugging
76 * context to test that further GL calls are not made by Skia GPU code.
77 */
78 void testAbandon();
79
bsalomon7ea33f52015-11-22 14:51:00 -080080 /**
81 * Creates a new GL context of the same type and makes the returned context current
82 * (if not null).
83 */
84 virtual SkGLContext* createNew() const { return nullptr; }
85
cdaltond416a5b2015-06-23 13:23:44 -070086 class GLFenceSync; // SkGpuFenceSync implementation that uses the OpenGL functionality.
87
bsalomon@google.com373a6632011-10-19 20:43:20 +000088protected:
kkinnunen30bc88c2014-10-15 23:03:54 -070089 SkGLContext();
bsalomon@google.com373a6632011-10-19 20:43:20 +000090
cdaltond416a5b2015-06-23 13:23:44 -070091 /*
92 * Methods that sublcasses must call from their constructors and destructors.
93 */
94 void init(const GrGLInterface*, SkGpuFenceSync* = NULL);
95 void teardown();
96
97 /*
98 * Operations that have a platform-dependent implementation.
99 */
100 virtual void onPlatformMakeCurrent() const = 0;
101 virtual void onPlatformSwapBuffers() const = 0;
102 virtual GrGLFuncPtr onPlatformGetProcAddress(const char*) const = 0;
103
104private:
105 enum { kMaxFrameLag = 3 };
106
107 SkAutoTDelete<SkGpuFenceSync> fFenceSync;
108 SkPlatformGpuFence fFrameFences[kMaxFrameLag - 1];
109 int fCurrentFenceIdx;
110
kkinnunen30bc88c2014-10-15 23:03:54 -0700111 /** Subclass provides the gl interface object if construction was
112 * successful. */
113 SkAutoTUnref<const GrGLInterface> fGL;
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +0000114
cdaltond416a5b2015-06-23 13:23:44 -0700115 friend class GLFenceSync; // For onPlatformGetProcAddress.
reed@google.comc31ce102010-12-21 16:26:39 +0000116};
117
joshualittb59d1bc2016-01-20 08:07:01 -0800118/** Creates platform-dependent GL context object. The shareContext parameter is in an optional
119 * context with which to share display lists. This should be a pointer to an SkGLContext created
120 * with SkCreatePlatformGLContext. NULL indicates that no sharing is to take place. Returns a valid
121 * gl context object or NULL if such can not be created.
122 * Note: If Skia embedder needs a custom GL context that sets up the GL interface, this function
123 * should be implemented by the embedder. Otherwise, the default implementation for the platform
124 * should be compiled in the library.
kkinnunen9e61bb72014-10-09 05:24:15 -0700125 */
joshualittb59d1bc2016-01-20 08:07:01 -0800126SK_API SkGLContext* SkCreatePlatformGLContext(GrGLStandard forcedGpuAPI,
127 SkGLContext* shareContext = nullptr);
kkinnunen9e61bb72014-10-09 05:24:15 -0700128
bsalomon@google.com373a6632011-10-19 20:43:20 +0000129/**
robertphillips@google.comfe1b5362013-02-07 19:45:46 +0000130 * Helper macros for using the GL context through the GrGLInterface. Example:
bsalomon@google.com373a6632011-10-19 20:43:20 +0000131 * SK_GL(glCtx, GenTextures(1, &texID));
132 */
commit-bot@chromium.orgc72425a2014-01-21 16:09:18 +0000133#define SK_GL(ctx, X) (ctx).gl()->fFunctions.f ## X; \
bsalomon9245b7e2014-07-01 07:20:11 -0700134 SkASSERT(0 == (ctx).gl()->fFunctions.fGetError())
commit-bot@chromium.orgc72425a2014-01-21 16:09:18 +0000135#define SK_GL_RET(ctx, RET, X) (RET) = (ctx).gl()->fFunctions.f ## X; \
bsalomon9245b7e2014-07-01 07:20:11 -0700136 SkASSERT(0 == (ctx).gl()->fFunctions.fGetError())
commit-bot@chromium.orgc72425a2014-01-21 16:09:18 +0000137#define SK_GL_NOERRCHECK(ctx, X) (ctx).gl()->fFunctions.f ## X
138#define SK_GL_RET_NOERRCHECK(ctx, RET, X) (RET) = (ctx).gl()->fFunctions.f ## X
bsalomon@google.com373a6632011-10-19 20:43:20 +0000139
reed@google.com873cb1e2010-12-23 15:00:45 +0000140#endif