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 | |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 88 | protected: |
kkinnunen | 30bc88c | 2014-10-15 23:03:54 -0700 | [diff] [blame] | 89 | SkGLContext(); |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 90 | |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 91 | /* |
| 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 | |
| 104 | private: |
| 105 | enum { kMaxFrameLag = 3 }; |
| 106 | |
| 107 | SkAutoTDelete<SkGpuFenceSync> fFenceSync; |
| 108 | SkPlatformGpuFence fFrameFences[kMaxFrameLag - 1]; |
| 109 | int fCurrentFenceIdx; |
| 110 | |
kkinnunen | 30bc88c | 2014-10-15 23:03:54 -0700 | [diff] [blame] | 111 | /** Subclass provides the gl interface object if construction was |
| 112 | * successful. */ |
| 113 | SkAutoTUnref<const GrGLInterface> fGL; |
robertphillips@google.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 114 | |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 115 | friend class GLFenceSync; // For onPlatformGetProcAddress. |
reed@google.com | c31ce10 | 2010-12-21 16:26:39 +0000 | [diff] [blame] | 116 | }; |
| 117 | |
joshualitt | b59d1bc | 2016-01-20 08:07:01 -0800 | [diff] [blame] | 118 | /** 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. |
kkinnunen | 9e61bb7 | 2014-10-09 05:24:15 -0700 | [diff] [blame] | 125 | */ |
joshualitt | b59d1bc | 2016-01-20 08:07:01 -0800 | [diff] [blame] | 126 | SK_API SkGLContext* SkCreatePlatformGLContext(GrGLStandard forcedGpuAPI, |
| 127 | SkGLContext* shareContext = nullptr); |
kkinnunen | 9e61bb7 | 2014-10-09 05:24:15 -0700 | [diff] [blame] | 128 | |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 129 | /** |
robertphillips@google.com | fe1b536 | 2013-02-07 19:45:46 +0000 | [diff] [blame] | 130 | * Helper macros for using the GL context through the GrGLInterface. Example: |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 131 | * SK_GL(glCtx, GenTextures(1, &texID)); |
| 132 | */ |
commit-bot@chromium.org | c72425a | 2014-01-21 16:09:18 +0000 | [diff] [blame] | 133 | #define SK_GL(ctx, X) (ctx).gl()->fFunctions.f ## X; \ |
bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 134 | SkASSERT(0 == (ctx).gl()->fFunctions.fGetError()) |
commit-bot@chromium.org | c72425a | 2014-01-21 16:09:18 +0000 | [diff] [blame] | 135 | #define SK_GL_RET(ctx, RET, X) (RET) = (ctx).gl()->fFunctions.f ## X; \ |
bsalomon | 9245b7e | 2014-07-01 07:20:11 -0700 | [diff] [blame] | 136 | SkASSERT(0 == (ctx).gl()->fFunctions.fGetError()) |
commit-bot@chromium.org | c72425a | 2014-01-21 16:09:18 +0000 | [diff] [blame] | 137 | #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.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 139 | |
reed@google.com | 873cb1e | 2010-12-23 15:00:45 +0000 | [diff] [blame] | 140 | #endif |