epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 3 | * Copyright 2013 Google Inc. |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
kkinnunen | 9e61bb7 | 2014-10-09 05:24:15 -0700 | [diff] [blame] | 8 | #ifndef SkGLContext_DEFINED |
| 9 | #define SkGLContext_DEFINED |
reed@google.com | c31ce10 | 2010-12-21 16:26:39 +0000 | [diff] [blame] | 10 | |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 11 | #include "GrGLInterface.h" |
mtklein | 22355c4 | 2015-07-29 11:10:46 -0700 | [diff] [blame] | 12 | #include "../private/SkGpuFenceSync.h" |
reed@google.com | c31ce10 | 2010-12-21 16:26:39 +0000 | [diff] [blame] | 13 | |
reed@google.com | 873cb1e | 2010-12-23 15:00:45 +0000 | [diff] [blame] | 14 | /** |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 15 | * Create an offscreen opengl context with an RGBA8 / 8bit stencil FBO. |
| 16 | * Provides a GrGLInterface struct of function pointers for the context. |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 17 | * This class is intended for Skia's testing needs and not for general |
| 18 | * use. |
reed@google.com | 873cb1e | 2010-12-23 15:00:45 +0000 | [diff] [blame] | 19 | */ |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 20 | class SK_API SkGLContext : public SkNoncopyable { |
reed@google.com | c31ce10 | 2010-12-21 16:26:39 +0000 | [diff] [blame] | 21 | public: |
kkinnunen | 3405800 | 2016-01-06 23:49:30 -0800 | [diff] [blame] | 22 | virtual ~SkGLContext(); |
reed@google.com | c31ce10 | 2010-12-21 16:26:39 +0000 | [diff] [blame] | 23 | |
kkinnunen | 30bc88c | 2014-10-15 23:03:54 -0700 | [diff] [blame] | 24 | bool isValid() const { return NULL != gl(); } |
reed@google.com | c31ce10 | 2010-12-21 16:26:39 +0000 | [diff] [blame] | 25 | |
kkinnunen | 30bc88c | 2014-10-15 23:03:54 -0700 | [diff] [blame] | 26 | const GrGLInterface* gl() const { return fGL.get(); } |
bungeman@google.com | 16bab87 | 2011-05-17 14:24:46 +0000 | [diff] [blame] | 27 | |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 28 | 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.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 39 | |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 40 | /** 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 | |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 44 | /** 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 | |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 49 | /** |
| 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 | |
joshualitt | 01836ad | 2016-01-20 13:09:12 -0800 | [diff] [blame] | 55 | void swapBuffers(); |
| 56 | |
djsollen@google.com | c9542ca | 2013-10-09 18:25:38 +0000 | [diff] [blame] | 57 | /** |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 58 | * The only purpose of this function it to provide a means of scheduling |
djsollen@google.com | c9542ca | 2013-10-09 18:25:38 +0000 | [diff] [blame] | 59 | * 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 | * |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 62 | * 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.com | c9542ca | 2013-10-09 18:25:38 +0000 | [diff] [blame] | 69 | */ |
joshualitt | 01836ad | 2016-01-20 13:09:12 -0800 | [diff] [blame] | 70 | void waitOnSyncOrSwap(); |
djsollen@google.com | c9542ca | 2013-10-09 18:25:38 +0000 | [diff] [blame] | 71 | |
bsalomon | 944bcf0 | 2014-07-29 08:01:52 -0700 | [diff] [blame] | 72 | /** |
| 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 | |
bsalomon | 7ea33f5 | 2015-11-22 14:51:00 -0800 | [diff] [blame] | 80 | /** |
| 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 | |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 86 | class GLFenceSync; // SkGpuFenceSync implementation that uses the OpenGL functionality. |
| 87 | |
joshualitt | b35c82d | 2016-01-28 06:26:35 -0800 | [diff] [blame] | 88 | /* |
| 89 | * returns the fencesync object owned by this SkGLContext |
| 90 | */ |
| 91 | SkGpuFenceSync* fenceSync() { return fFenceSync.get(); } |
| 92 | |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 93 | protected: |
kkinnunen | 30bc88c | 2014-10-15 23:03:54 -0700 | [diff] [blame] | 94 | SkGLContext(); |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 95 | |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 96 | /* |
| 97 | * Methods that sublcasses must call from their constructors and destructors. |
| 98 | */ |
| 99 | void init(const GrGLInterface*, SkGpuFenceSync* = NULL); |
| 100 | void teardown(); |
| 101 | |
| 102 | /* |
| 103 | * Operations that have a platform-dependent implementation. |
| 104 | */ |
| 105 | virtual void onPlatformMakeCurrent() const = 0; |
| 106 | virtual void onPlatformSwapBuffers() const = 0; |
| 107 | virtual GrGLFuncPtr onPlatformGetProcAddress(const char*) const = 0; |
| 108 | |
| 109 | private: |
| 110 | enum { kMaxFrameLag = 3 }; |
| 111 | |
| 112 | SkAutoTDelete<SkGpuFenceSync> fFenceSync; |
| 113 | SkPlatformGpuFence fFrameFences[kMaxFrameLag - 1]; |
| 114 | int fCurrentFenceIdx; |
| 115 | |
kkinnunen | 30bc88c | 2014-10-15 23:03:54 -0700 | [diff] [blame] | 116 | /** Subclass provides the gl interface object if construction was |
| 117 | * successful. */ |
| 118 | SkAutoTUnref<const GrGLInterface> fGL; |
robertphillips@google.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 119 | |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 120 | friend class GLFenceSync; // For onPlatformGetProcAddress. |
reed@google.com | c31ce10 | 2010-12-21 16:26:39 +0000 | [diff] [blame] | 121 | }; |
| 122 | |
joshualitt | b59d1bc | 2016-01-20 08:07:01 -0800 | [diff] [blame] | 123 | /** Creates platform-dependent GL context object. The shareContext parameter is in an optional |
| 124 | * context with which to share display lists. This should be a pointer to an SkGLContext created |
| 125 | * with SkCreatePlatformGLContext. NULL indicates that no sharing is to take place. Returns a valid |
| 126 | * gl context object or NULL if such can not be created. |
| 127 | * Note: If Skia embedder needs a custom GL context that sets up the GL interface, this function |
| 128 | * should be implemented by the embedder. Otherwise, the default implementation for the platform |
| 129 | * should be compiled in the library. |
kkinnunen | 9e61bb7 | 2014-10-09 05:24:15 -0700 | [diff] [blame] | 130 | */ |
joshualitt | b59d1bc | 2016-01-20 08:07:01 -0800 | [diff] [blame] | 131 | SK_API SkGLContext* SkCreatePlatformGLContext(GrGLStandard forcedGpuAPI, |
| 132 | SkGLContext* shareContext = nullptr); |
kkinnunen | 9e61bb7 | 2014-10-09 05:24:15 -0700 | [diff] [blame] | 133 | |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 134 | /** |
robertphillips@google.com | fe1b536 | 2013-02-07 19:45:46 +0000 | [diff] [blame] | 135 | * Helper macros for using the GL context through the GrGLInterface. Example: |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 136 | * SK_GL(glCtx, GenTextures(1, &texID)); |
| 137 | */ |
commit-bot@chromium.org | c72425a | 2014-01-21 16:09:18 +0000 | [diff] [blame] | 138 | #define SK_GL(ctx, X) (ctx).gl()->fFunctions.f ## X; \ |
bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 139 | SkASSERT(0 == (ctx).gl()->fFunctions.fGetError()) |
commit-bot@chromium.org | c72425a | 2014-01-21 16:09:18 +0000 | [diff] [blame] | 140 | #define SK_GL_RET(ctx, RET, X) (RET) = (ctx).gl()->fFunctions.f ## X; \ |
bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 141 | SkASSERT(0 == (ctx).gl()->fFunctions.fGetError()) |
commit-bot@chromium.org | c72425a | 2014-01-21 16:09:18 +0000 | [diff] [blame] | 142 | #define SK_GL_NOERRCHECK(ctx, X) (ctx).gl()->fFunctions.f ## X |
| 143 | #define SK_GL_RET_NOERRCHECK(ctx, RET, X) (RET) = (ctx).gl()->fFunctions.f ## X |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 144 | |
reed@google.com | 873cb1e | 2010-12-23 15:00:45 +0000 | [diff] [blame] | 145 | #endif |