OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2013 Google Inc. | 3 * Copyright 2013 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 #include "gl/SkGLContext.h" | 8 #include "gl/SkGLContext.h" |
9 #include "GrGLUtil.h" | 9 #include "GrGLUtil.h" |
| 10 #include "SkGpuFenceSync.h" |
10 | 11 |
11 SkGLContext::SkGLContext() { | 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, GrGLbitfiel
d); |
| 30 typedef GrGLenum (GR_GL_FUNCTION_TYPE* GLClientWaitSyncProc) (GLsync, GrGLbi
tfield, 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)); |
12 } | 43 } |
13 | 44 |
14 SkGLContext::~SkGLContext() { | 45 SkGLContext::~SkGLContext() { |
15 SkASSERT(NULL == fGL.get()); // Subclass should destroy the interface. | 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 |
| 52 SkASSERT(NULL == fGL.get()); |
| 53 SkASSERT(NULL == fFenceSync.get()); |
| 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 } |
| 70 fFenceSync.reset(NULL); |
| 71 } |
| 72 |
| 73 fGL.reset(NULL); |
| 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); |
16 } | 96 } |
17 | 97 |
18 void SkGLContext::testAbandon() { | 98 void SkGLContext::testAbandon() { |
19 if (fGL) { | 99 if (fGL) { |
20 fGL->abandon(); | 100 fGL->abandon(); |
21 } | 101 } |
| 102 if (fFenceSync) { |
| 103 memset(fFrameFences, 0, sizeof(fFrameFences)); |
| 104 } |
22 } | 105 } |
| 106 |
| 107 SkGLContext::GLFenceSync* SkGLContext::GLFenceSync::CreateIfSupported(const SkGL
Context* ctx) { |
| 108 SkAutoTDelete<GLFenceSync> ret(SkNEW(GLFenceSync)); |
| 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 ch
ar*>(versionStr)); |
| 114 if (version < GR_GL_VER(3,2) && !ctx->gl()->hasExtension("GL_ARB_sync"))
{ |
| 115 return NULL; |
| 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")) { |
| 125 return NULL; |
| 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) { |
| 136 return NULL; |
| 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_BI
T, -1); |
| 149 } |
| 150 |
| 151 void SkGLContext::GLFenceSync::deleteFence(SkPlatformGpuFence fence) const { |
| 152 GLsync glsync = static_cast<GLsync>(fence); |
| 153 fGLDeleteSync(glsync); |
| 154 } |
OLD | NEW |