| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/gl/gl_fence.h" | 5 #include "ui/gl/gl_fence.h" |
| 6 | 6 |
| 7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
| 8 #include "ui/gl/gl_bindings.h" | 8 #include "ui/gl/gl_bindings.h" |
| 9 #include "ui/gl/gl_context.h" | 9 #include "ui/gl/gl_context.h" |
| 10 | 10 |
| 11 namespace { | 11 namespace { |
| 12 | 12 |
| 13 class GLFenceNVFence: public gfx::GLFence { | 13 class GLFenceNVFence: public gfx::GLFence { |
| 14 public: | 14 public: |
| 15 GLFenceNVFence(bool flush) { | 15 GLFenceNVFence(bool flush) { |
| 16 // What if either of these GL calls fails? TestFenceNV will return true. | 16 // What if either of these GL calls fails? TestFenceNV will return true. |
| 17 // See spec: | 17 // See spec: |
| 18 // http://www.opengl.org/registry/specs/NV/fence.txt | 18 // http://www.opengl.org/registry/specs/NV/fence.txt |
| 19 // | 19 // |
| 20 // What should happen if TestFenceNV is called for a name before SetFenceNV | 20 // What should happen if TestFenceNV is called for a name before SetFenceNV |
| 21 // is called? | 21 // is called? |
| 22 // We generate an INVALID_OPERATION error, and return TRUE. | 22 // We generate an INVALID_OPERATION error, and return TRUE. |
| 23 // This follows the semantics for texture object names before | 23 // This follows the semantics for texture object names before |
| 24 // they are bound, in that they acquire their state upon binding. | 24 // they are bound, in that they acquire their state upon binding. |
| 25 // We will arbitrarily return TRUE for consistency. | 25 // We will arbitrarily return TRUE for consistency. |
| 26 glGenFencesNV(1, &fence_); | 26 glGenFencesNV(1, &fence_); |
| 27 glSetFenceNV(fence_, GL_ALL_COMPLETED_NV); | 27 glSetFenceNV(fence_, GL_ALL_COMPLETED_NV); |
| 28 if (flush) | 28 if (flush) { |
| 29 glFlush(); | 29 glFlush(); |
| 30 } else { |
| 31 flush_event_ = gfx::GLContext::GetCurrent()->SignalFlush(); |
| 32 } |
| 30 } | 33 } |
| 31 | 34 |
| 32 virtual bool HasCompleted() OVERRIDE { | 35 virtual bool HasCompleted() OVERRIDE { |
| 33 return !!glTestFenceNV(fence_); | 36 return !!glTestFenceNV(fence_); |
| 34 } | 37 } |
| 35 | 38 |
| 36 virtual void ClientWait() OVERRIDE { | 39 virtual void ClientWait() OVERRIDE { |
| 37 glFinishFenceNV(fence_); | 40 if (!flush_event_ || flush_event_->IsSignaled()) { |
| 41 glFinishFenceNV(fence_); |
| 42 } else { |
| 43 LOG(ERROR) << "Trying to wait for uncommitted fence. Skipping..."; |
| 44 } |
| 38 } | 45 } |
| 39 | 46 |
| 40 virtual void ServerWait() OVERRIDE { | 47 virtual void ServerWait() OVERRIDE { |
| 41 glFinishFenceNV(fence_); | 48 ClientWait(); |
| 42 } | 49 } |
| 43 | 50 |
| 44 private: | 51 private: |
| 45 virtual ~GLFenceNVFence() { | 52 virtual ~GLFenceNVFence() { |
| 46 glDeleteFencesNV(1, &fence_); | 53 glDeleteFencesNV(1, &fence_); |
| 47 } | 54 } |
| 48 | 55 |
| 49 GLuint fence_; | 56 GLuint fence_; |
| 57 scoped_refptr<gfx::GLContext::FlushEvent> flush_event_; |
| 50 }; | 58 }; |
| 51 | 59 |
| 52 class GLFenceARBSync: public gfx::GLFence { | 60 class GLFenceARBSync: public gfx::GLFence { |
| 53 public: | 61 public: |
| 54 GLFenceARBSync(bool flush) { | 62 GLFenceARBSync(bool flush) { |
| 55 sync_ = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0); | 63 sync_ = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0); |
| 56 if (flush) | 64 if (flush) { |
| 57 glFlush(); | 65 glFlush(); |
| 66 } else { |
| 67 flush_event_ = gfx::GLContext::GetCurrent()->SignalFlush(); |
| 68 } |
| 58 } | 69 } |
| 59 | 70 |
| 60 virtual bool HasCompleted() OVERRIDE { | 71 virtual bool HasCompleted() OVERRIDE { |
| 61 // Handle the case where FenceSync failed. | 72 // Handle the case where FenceSync failed. |
| 62 if (!sync_) | 73 if (!sync_) |
| 63 return true; | 74 return true; |
| 64 | 75 |
| 65 // We could potentially use glGetSynciv here, but it doesn't work | 76 // We could potentially use glGetSynciv here, but it doesn't work |
| 66 // on OSX 10.7 (always says the fence is not signaled yet). | 77 // on OSX 10.7 (always says the fence is not signaled yet). |
| 67 // glClientWaitSync works better, so let's use that instead. | 78 // glClientWaitSync works better, so let's use that instead. |
| 68 return glClientWaitSync(sync_, 0, 0) != GL_TIMEOUT_EXPIRED; | 79 return glClientWaitSync(sync_, 0, 0) != GL_TIMEOUT_EXPIRED; |
| 69 } | 80 } |
| 70 | 81 |
| 71 virtual void ClientWait() OVERRIDE { | 82 virtual void ClientWait() OVERRIDE { |
| 72 glClientWaitSync(sync_, GL_SYNC_FLUSH_COMMANDS_BIT, GL_TIMEOUT_IGNORED); | 83 if (!flush_event_ || flush_event_->IsSignaled()) { |
| 84 glClientWaitSync(sync_, GL_SYNC_FLUSH_COMMANDS_BIT, GL_TIMEOUT_IGNORED); |
| 85 } else { |
| 86 LOG(ERROR) << "Trying to wait for uncommitted fence. Skipping..."; |
| 87 } |
| 73 } | 88 } |
| 74 | 89 |
| 75 virtual void ServerWait() OVERRIDE { | 90 virtual void ServerWait() OVERRIDE { |
| 76 glWaitSync(sync_, 0, GL_TIMEOUT_IGNORED); | 91 if (!flush_event_ || flush_event_->IsSignaled()) { |
| 92 glWaitSync(sync_, 0, GL_TIMEOUT_IGNORED); |
| 93 } else { |
| 94 LOG(ERROR) << "Trying to wait for uncommitted fence. Skipping..."; |
| 95 } |
| 77 } | 96 } |
| 78 | 97 |
| 79 private: | 98 private: |
| 80 virtual ~GLFenceARBSync() { | 99 virtual ~GLFenceARBSync() { |
| 81 glDeleteSync(sync_); | 100 glDeleteSync(sync_); |
| 82 } | 101 } |
| 83 | 102 |
| 84 GLsync sync_; | 103 GLsync sync_; |
| 104 scoped_refptr<gfx::GLContext::FlushEvent> flush_event_; |
| 85 }; | 105 }; |
| 86 | 106 |
| 87 #if !defined(OS_MACOSX) | 107 #if !defined(OS_MACOSX) |
| 88 class EGLFenceSync : public gfx::GLFence { | 108 class EGLFenceSync : public gfx::GLFence { |
| 89 public: | 109 public: |
| 90 EGLFenceSync(bool flush) { | 110 EGLFenceSync(bool flush) { |
| 91 display_ = eglGetCurrentDisplay(); | 111 display_ = eglGetCurrentDisplay(); |
| 92 sync_ = eglCreateSyncKHR(display_, EGL_SYNC_FENCE_KHR, NULL); | 112 sync_ = eglCreateSyncKHR(display_, EGL_SYNC_FENCE_KHR, NULL); |
| 93 if (flush) | 113 if (flush) { |
| 94 glFlush(); | 114 glFlush(); |
| 115 } else { |
| 116 flush_event_ = gfx::GLContext::GetCurrent()->SignalFlush(); |
| 117 } |
| 95 } | 118 } |
| 96 | 119 |
| 97 virtual bool HasCompleted() OVERRIDE { | 120 virtual bool HasCompleted() OVERRIDE { |
| 98 EGLint value = 0; | 121 EGLint value = 0; |
| 99 eglGetSyncAttribKHR(display_, sync_, EGL_SYNC_STATUS_KHR, &value); | 122 eglGetSyncAttribKHR(display_, sync_, EGL_SYNC_STATUS_KHR, &value); |
| 100 DCHECK(value == EGL_SIGNALED_KHR || value == EGL_UNSIGNALED_KHR); | 123 DCHECK(value == EGL_SIGNALED_KHR || value == EGL_UNSIGNALED_KHR); |
| 101 return !value || value == EGL_SIGNALED_KHR; | 124 return !value || value == EGL_SIGNALED_KHR; |
| 102 } | 125 } |
| 103 | 126 |
| 104 virtual void ClientWait() OVERRIDE { | 127 virtual void ClientWait() OVERRIDE { |
| 105 EGLint flags = 0; | 128 if (!flush_event_ || flush_event_->IsSignaled()) { |
| 106 EGLTimeKHR time = EGL_FOREVER_KHR; | 129 EGLint flags = 0; |
| 107 eglClientWaitSyncKHR(display_, sync_, flags, time); | 130 EGLTimeKHR time = EGL_FOREVER_KHR; |
| 131 eglClientWaitSyncKHR(display_, sync_, flags, time); |
| 132 } else { |
| 133 LOG(ERROR) << "Trying to wait for uncommitted fence. Skipping..."; |
| 134 } |
| 108 } | 135 } |
| 109 | 136 |
| 110 virtual void ServerWait() OVERRIDE { | 137 virtual void ServerWait() OVERRIDE { |
| 111 EGLint flags = 0; | 138 if (!flush_event_ || flush_event_->IsSignaled()) { |
| 112 eglWaitSyncKHR(display_, sync_, flags); | 139 EGLint flags = 0; |
| 140 eglWaitSyncKHR(display_, sync_, flags); |
| 141 } else { |
| 142 LOG(ERROR) << "Trying to wait for uncommitted fence. Skipping..."; |
| 143 } |
| 113 } | 144 } |
| 114 | 145 |
| 115 | 146 |
| 116 private: | 147 private: |
| 117 virtual ~EGLFenceSync() { | 148 virtual ~EGLFenceSync() { |
| 118 eglDestroySyncKHR(display_, sync_); | 149 eglDestroySyncKHR(display_, sync_); |
| 119 } | 150 } |
| 120 | 151 |
| 121 EGLSyncKHR sync_; | 152 EGLSyncKHR sync_; |
| 122 EGLDisplay display_; | 153 EGLDisplay display_; |
| 154 scoped_refptr<gfx::GLContext::FlushEvent> flush_event_; |
| 123 }; | 155 }; |
| 124 #endif // !OS_MACOSX | 156 #endif // !OS_MACOSX |
| 125 | 157 |
| 126 // static | 158 // static |
| 127 gfx::GLFence* CreateFence(bool flush) { | 159 gfx::GLFence* CreateFence(bool flush) { |
| 160 DCHECK(gfx::GLContext::GetCurrent()) |
| 161 << "Trying to create fence with no context"; |
| 162 |
| 128 #if !defined(OS_MACOSX) | 163 #if !defined(OS_MACOSX) |
| 129 if (gfx::g_driver_egl.ext.b_EGL_KHR_fence_sync) | 164 if (gfx::g_driver_egl.ext.b_EGL_KHR_fence_sync) |
| 130 return new EGLFenceSync(flush); | 165 return new EGLFenceSync(flush); |
| 131 #endif | 166 #endif |
| 132 // Prefer ARB_sync which supports server-side wait. | 167 // Prefer ARB_sync which supports server-side wait. |
| 133 if (gfx::g_driver_gl.ext.b_GL_ARB_sync) | 168 if (gfx::g_driver_gl.ext.b_GL_ARB_sync) |
| 134 return new GLFenceARBSync(flush); | 169 return new GLFenceARBSync(flush); |
| 135 if (gfx::g_driver_gl.ext.b_GL_NV_fence) | 170 if (gfx::g_driver_gl.ext.b_GL_NV_fence) |
| 136 return new GLFenceNVFence(flush); | 171 return new GLFenceNVFence(flush); |
| 137 return NULL; | 172 return NULL; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 149 | 184 |
| 150 GLFence* GLFence::Create() { | 185 GLFence* GLFence::Create() { |
| 151 return CreateFence(true); | 186 return CreateFence(true); |
| 152 } | 187 } |
| 153 | 188 |
| 154 GLFence* GLFence::CreateWithoutFlush() { | 189 GLFence* GLFence::CreateWithoutFlush() { |
| 155 return CreateFence(false); | 190 return CreateFence(false); |
| 156 } | 191 } |
| 157 | 192 |
| 158 } // namespace gfx | 193 } // namespace gfx |
| OLD | NEW |