bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 3 | * Copyright 2013 Google Inc. |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +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 | #include "gl/SkGLContext.h" |
bsalomon@google.com | 1744f97 | 2013-02-26 21:46:32 +0000 | [diff] [blame] | 9 | #include "GrGLUtil.h" |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 10 | #include "SkGpuFenceSync.h" |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 11 | |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 12 | class SkGLContext::GLFenceSync : public SkGpuFenceSync { |
| 13 | public: |
| 14 | static GLFenceSync* CreateIfSupported(const SkGLContext*); |
| 15 | |
| 16 | SkPlatformGpuFence SK_WARN_UNUSED_RESULT insertFence() const override; |
| 17 | bool flushAndWaitFence(SkPlatformGpuFence fence) const override; |
| 18 | void deleteFence(SkPlatformGpuFence fence) const override; |
| 19 | |
| 20 | private: |
| 21 | GLFenceSync() {} |
| 22 | |
| 23 | static const GrGLenum GL_SYNC_GPU_COMMANDS_COMPLETE = 0x9117; |
| 24 | static const GrGLenum GL_WAIT_FAILED = 0x911d; |
| 25 | static const GrGLbitfield GL_SYNC_FLUSH_COMMANDS_BIT = 0x00000001; |
| 26 | |
| 27 | typedef struct __GLsync *GLsync; |
| 28 | |
| 29 | typedef GLsync (GR_GL_FUNCTION_TYPE* GLFenceSyncProc) (GrGLenum, GrGLbitfield); |
| 30 | typedef GrGLenum (GR_GL_FUNCTION_TYPE* GLClientWaitSyncProc) (GLsync, GrGLbitfield, GrGLuint64); |
| 31 | typedef GrGLvoid (GR_GL_FUNCTION_TYPE* GLDeleteSyncProc) (GLsync); |
| 32 | |
| 33 | GLFenceSyncProc fGLFenceSync; |
| 34 | GLClientWaitSyncProc fGLClientWaitSync; |
| 35 | GLDeleteSyncProc fGLDeleteSync; |
| 36 | |
| 37 | typedef SkGpuFenceSync INHERITED; |
| 38 | }; |
| 39 | |
| 40 | SkGLContext::SkGLContext() |
| 41 | : fCurrentFenceIdx(0) { |
| 42 | memset(fFrameFences, 0, sizeof(fFrameFences)); |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 43 | } |
| 44 | |
kkinnunen | 9e61bb7 | 2014-10-09 05:24:15 -0700 | [diff] [blame] | 45 | SkGLContext::~SkGLContext() { |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 46 | // Subclass should call teardown. |
| 47 | #ifdef SK_DEBUG |
| 48 | for (size_t i = 0; i < SK_ARRAY_COUNT(fFrameFences); i++) { |
| 49 | SkASSERT(0 == fFrameFences[i]); |
| 50 | } |
| 51 | #endif |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 52 | SkASSERT(nullptr == fGL.get()); |
| 53 | SkASSERT(nullptr == fFenceSync.get()); |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | void SkGLContext::init(const GrGLInterface* gl, SkGpuFenceSync* fenceSync) { |
| 57 | SkASSERT(!fGL.get()); |
| 58 | fGL.reset(gl); |
| 59 | fFenceSync.reset(fenceSync ? fenceSync : GLFenceSync::CreateIfSupported(this)); |
| 60 | } |
| 61 | |
| 62 | void SkGLContext::teardown() { |
| 63 | if (fFenceSync) { |
| 64 | for (size_t i = 0; i < SK_ARRAY_COUNT(fFrameFences); i++) { |
| 65 | if (fFrameFences[i]) { |
| 66 | fFenceSync->deleteFence(fFrameFences[i]); |
| 67 | fFrameFences[i] = 0; |
| 68 | } |
| 69 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 70 | fFenceSync.reset(nullptr); |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 71 | } |
| 72 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 73 | fGL.reset(nullptr); |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | void SkGLContext::makeCurrent() const { |
| 77 | this->onPlatformMakeCurrent(); |
| 78 | } |
| 79 | |
| 80 | void SkGLContext::swapBuffers() { |
| 81 | if (!fFenceSync) { |
| 82 | // Fallback on the platform SwapBuffers method for synchronization. This may have no effect. |
| 83 | this->onPlatformSwapBuffers(); |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | if (fFrameFences[fCurrentFenceIdx]) { |
| 88 | if (!fFenceSync->flushAndWaitFence(fFrameFences[fCurrentFenceIdx])) { |
| 89 | SkDebugf("WARNING: Wait failed for fence sync. Timings might not be accurate.\n"); |
| 90 | } |
| 91 | fFenceSync->deleteFence(fFrameFences[fCurrentFenceIdx]); |
| 92 | } |
| 93 | |
| 94 | fFrameFences[fCurrentFenceIdx] = fFenceSync->insertFence(); |
| 95 | fCurrentFenceIdx = (fCurrentFenceIdx + 1) % SK_ARRAY_COUNT(fFrameFences); |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 96 | } |
bsalomon | 944bcf0 | 2014-07-29 08:01:52 -0700 | [diff] [blame] | 97 | |
kkinnunen | 9e61bb7 | 2014-10-09 05:24:15 -0700 | [diff] [blame] | 98 | void SkGLContext::testAbandon() { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 99 | if (fGL) { |
bsalomon | 944bcf0 | 2014-07-29 08:01:52 -0700 | [diff] [blame] | 100 | fGL->abandon(); |
| 101 | } |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 102 | if (fFenceSync) { |
| 103 | memset(fFrameFences, 0, sizeof(fFrameFences)); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | SkGLContext::GLFenceSync* SkGLContext::GLFenceSync::CreateIfSupported(const SkGLContext* ctx) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 108 | SkAutoTDelete<GLFenceSync> ret(new GLFenceSync); |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 109 | |
| 110 | if (kGL_GrGLStandard == ctx->gl()->fStandard) { |
| 111 | const GrGLubyte* versionStr; |
| 112 | SK_GL_RET(*ctx, versionStr, GetString(GR_GL_VERSION)); |
| 113 | GrGLVersion version = GrGLGetVersionFromString(reinterpret_cast<const char*>(versionStr)); |
| 114 | if (version < GR_GL_VER(3,2) && !ctx->gl()->hasExtension("GL_ARB_sync")) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 115 | return nullptr; |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 116 | } |
| 117 | ret->fGLFenceSync = reinterpret_cast<GLFenceSyncProc>( |
| 118 | ctx->onPlatformGetProcAddress("glFenceSync")); |
| 119 | ret->fGLClientWaitSync = reinterpret_cast<GLClientWaitSyncProc>( |
| 120 | ctx->onPlatformGetProcAddress("glClientWaitSync")); |
| 121 | ret->fGLDeleteSync = reinterpret_cast<GLDeleteSyncProc>( |
| 122 | ctx->onPlatformGetProcAddress("glDeleteSync")); |
| 123 | } else { |
| 124 | if (!ctx->gl()->hasExtension("GL_APPLE_sync")) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 125 | return nullptr; |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 126 | } |
| 127 | ret->fGLFenceSync = reinterpret_cast<GLFenceSyncProc>( |
| 128 | ctx->onPlatformGetProcAddress("glFenceSyncAPPLE")); |
| 129 | ret->fGLClientWaitSync = reinterpret_cast<GLClientWaitSyncProc>( |
| 130 | ctx->onPlatformGetProcAddress("glClientWaitSyncAPPLE")); |
| 131 | ret->fGLDeleteSync = reinterpret_cast<GLDeleteSyncProc>( |
| 132 | ctx->onPlatformGetProcAddress("glDeleteSyncAPPLE")); |
| 133 | } |
| 134 | |
| 135 | if (!ret->fGLFenceSync || !ret->fGLClientWaitSync || !ret->fGLDeleteSync) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 136 | return nullptr; |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | return ret.detach(); |
| 140 | } |
| 141 | |
| 142 | SkPlatformGpuFence SkGLContext::GLFenceSync::insertFence() const { |
| 143 | return fGLFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0); |
| 144 | } |
| 145 | |
| 146 | bool SkGLContext::GLFenceSync::flushAndWaitFence(SkPlatformGpuFence fence) const { |
| 147 | GLsync glsync = static_cast<GLsync>(fence); |
| 148 | return GL_WAIT_FAILED != fGLClientWaitSync(glsync, GL_SYNC_FLUSH_COMMANDS_BIT, -1); |
| 149 | } |
| 150 | |
| 151 | void SkGLContext::GLFenceSync::deleteFence(SkPlatformGpuFence fence) const { |
| 152 | GLsync glsync = static_cast<GLsync>(fence); |
| 153 | fGLDeleteSync(glsync); |
bsalomon | 944bcf0 | 2014-07-29 08:01:52 -0700 | [diff] [blame] | 154 | } |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 155 | |
| 156 | GrGLint SkGLContext::createTextureRectangle(int width, int height, GrGLenum internalFormat, |
| 157 | GrGLenum externalFormat, GrGLenum externalType, |
| 158 | GrGLvoid* data) { |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame^] | 159 | if (!(kGL_GrGLStandard == fGL->fStandard && GrGLGetVersion(fGL) >= GR_GL_VER(3, 1)) && |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 160 | !fGL->fExtensions.has("GL_ARB_texture_rectangle")) { |
| 161 | return 0; |
| 162 | } |
bsalomon | e179a91 | 2016-01-20 06:18:10 -0800 | [diff] [blame^] | 163 | |
| 164 | if (GrGLGetGLSLVersion(fGL) < GR_GLSL_VER(1, 40)) { |
| 165 | return 0; |
| 166 | } |
| 167 | |
bsalomon | e5286e0 | 2016-01-14 09:24:09 -0800 | [diff] [blame] | 168 | GrGLuint id; |
| 169 | GR_GL_CALL(fGL, GenTextures(1, &id)); |
| 170 | GR_GL_CALL(fGL, BindTexture(GR_GL_TEXTURE_RECTANGLE, id)); |
| 171 | GR_GL_CALL(fGL, TexParameteri(GR_GL_TEXTURE_RECTANGLE, GR_GL_TEXTURE_MAG_FILTER, |
| 172 | GR_GL_NEAREST)); |
| 173 | GR_GL_CALL(fGL, TexParameteri(GR_GL_TEXTURE_RECTANGLE, GR_GL_TEXTURE_MIN_FILTER, |
| 174 | GR_GL_NEAREST)); |
| 175 | GR_GL_CALL(fGL, TexParameteri(GR_GL_TEXTURE_RECTANGLE, GR_GL_TEXTURE_WRAP_S, |
| 176 | GR_GL_CLAMP_TO_EDGE)); |
| 177 | GR_GL_CALL(fGL, TexParameteri(GR_GL_TEXTURE_RECTANGLE, GR_GL_TEXTURE_WRAP_T, |
| 178 | GR_GL_CLAMP_TO_EDGE)); |
| 179 | GR_GL_CALL(fGL, TexImage2D(GR_GL_TEXTURE_RECTANGLE, 0, internalFormat, width, height, 0, |
| 180 | externalFormat, externalType, data)); |
| 181 | return id; |
| 182 | } |