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