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 } | |
33 } | 30 } |
34 | 31 |
35 virtual bool HasCompleted() OVERRIDE { | 32 virtual bool HasCompleted() OVERRIDE { |
36 return !!glTestFenceNV(fence_); | 33 return !!glTestFenceNV(fence_); |
37 } | 34 } |
38 | 35 |
39 virtual void ClientWait() OVERRIDE { | 36 virtual void ClientWait() OVERRIDE { |
40 if (!flush_event_ || flush_event_->IsSignaled()) { | 37 glFinishFenceNV(fence_); |
41 glFinishFenceNV(fence_); | |
42 } else { | |
43 LOG(ERROR) << "Trying to wait for uncommitted fence. Skipping..."; | |
44 } | |
45 } | 38 } |
46 | 39 |
47 virtual void ServerWait() OVERRIDE { | 40 virtual void ServerWait() OVERRIDE { |
48 ClientWait(); | 41 glFinishFenceNV(fence_); |
49 } | 42 } |
50 | 43 |
51 private: | 44 private: |
52 virtual ~GLFenceNVFence() { | 45 virtual ~GLFenceNVFence() { |
53 glDeleteFencesNV(1, &fence_); | 46 glDeleteFencesNV(1, &fence_); |
54 } | 47 } |
55 | 48 |
56 GLuint fence_; | 49 GLuint fence_; |
57 scoped_refptr<gfx::GLContext::FlushEvent> flush_event_; | |
58 }; | 50 }; |
59 | 51 |
60 class GLFenceARBSync: public gfx::GLFence { | 52 class GLFenceARBSync: public gfx::GLFence { |
61 public: | 53 public: |
62 GLFenceARBSync(bool flush) { | 54 GLFenceARBSync(bool flush) { |
63 sync_ = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0); | 55 sync_ = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0); |
64 if (flush) { | 56 if (flush) |
65 glFlush(); | 57 glFlush(); |
66 } else { | |
67 flush_event_ = gfx::GLContext::GetCurrent()->SignalFlush(); | |
68 } | |
69 } | 58 } |
70 | 59 |
71 virtual bool HasCompleted() OVERRIDE { | 60 virtual bool HasCompleted() OVERRIDE { |
72 // Handle the case where FenceSync failed. | 61 // Handle the case where FenceSync failed. |
73 if (!sync_) | 62 if (!sync_) |
74 return true; | 63 return true; |
75 | 64 |
76 // We could potentially use glGetSynciv here, but it doesn't work | 65 // We could potentially use glGetSynciv here, but it doesn't work |
77 // on OSX 10.7 (always says the fence is not signaled yet). | 66 // on OSX 10.7 (always says the fence is not signaled yet). |
78 // glClientWaitSync works better, so let's use that instead. | 67 // glClientWaitSync works better, so let's use that instead. |
79 return glClientWaitSync(sync_, 0, 0) != GL_TIMEOUT_EXPIRED; | 68 return glClientWaitSync(sync_, 0, 0) != GL_TIMEOUT_EXPIRED; |
80 } | 69 } |
81 | 70 |
82 virtual void ClientWait() OVERRIDE { | 71 virtual void ClientWait() OVERRIDE { |
83 if (!flush_event_ || flush_event_->IsSignaled()) { | 72 glClientWaitSync(sync_, GL_SYNC_FLUSH_COMMANDS_BIT, GL_TIMEOUT_IGNORED); |
84 glClientWaitSync(sync_, GL_SYNC_FLUSH_COMMANDS_BIT, GL_TIMEOUT_IGNORED); | |
85 } else { | |
86 LOG(ERROR) << "Trying to wait for uncommitted fence. Skipping..."; | |
87 } | |
88 } | 73 } |
89 | 74 |
90 virtual void ServerWait() OVERRIDE { | 75 virtual void ServerWait() OVERRIDE { |
91 if (!flush_event_ || flush_event_->IsSignaled()) { | 76 glWaitSync(sync_, 0, GL_TIMEOUT_IGNORED); |
92 glWaitSync(sync_, 0, GL_TIMEOUT_IGNORED); | |
93 } else { | |
94 LOG(ERROR) << "Trying to wait for uncommitted fence. Skipping..."; | |
95 } | |
96 } | 77 } |
97 | 78 |
98 private: | 79 private: |
99 virtual ~GLFenceARBSync() { | 80 virtual ~GLFenceARBSync() { |
100 glDeleteSync(sync_); | 81 glDeleteSync(sync_); |
101 } | 82 } |
102 | 83 |
103 GLsync sync_; | 84 GLsync sync_; |
104 scoped_refptr<gfx::GLContext::FlushEvent> flush_event_; | |
105 }; | 85 }; |
106 | 86 |
107 #if !defined(OS_MACOSX) | 87 #if !defined(OS_MACOSX) |
108 class EGLFenceSync : public gfx::GLFence { | 88 class EGLFenceSync : public gfx::GLFence { |
109 public: | 89 public: |
110 EGLFenceSync(bool flush) { | 90 EGLFenceSync(bool flush) { |
111 display_ = eglGetCurrentDisplay(); | 91 display_ = eglGetCurrentDisplay(); |
112 sync_ = eglCreateSyncKHR(display_, EGL_SYNC_FENCE_KHR, NULL); | 92 sync_ = eglCreateSyncKHR(display_, EGL_SYNC_FENCE_KHR, NULL); |
113 if (flush) { | 93 if (flush) |
114 glFlush(); | 94 glFlush(); |
115 } else { | |
116 flush_event_ = gfx::GLContext::GetCurrent()->SignalFlush(); | |
117 } | |
118 } | 95 } |
119 | 96 |
120 virtual bool HasCompleted() OVERRIDE { | 97 virtual bool HasCompleted() OVERRIDE { |
121 EGLint value = 0; | 98 EGLint value = 0; |
122 eglGetSyncAttribKHR(display_, sync_, EGL_SYNC_STATUS_KHR, &value); | 99 eglGetSyncAttribKHR(display_, sync_, EGL_SYNC_STATUS_KHR, &value); |
123 DCHECK(value == EGL_SIGNALED_KHR || value == EGL_UNSIGNALED_KHR); | 100 DCHECK(value == EGL_SIGNALED_KHR || value == EGL_UNSIGNALED_KHR); |
124 return !value || value == EGL_SIGNALED_KHR; | 101 return !value || value == EGL_SIGNALED_KHR; |
125 } | 102 } |
126 | 103 |
127 virtual void ClientWait() OVERRIDE { | 104 virtual void ClientWait() OVERRIDE { |
128 if (!flush_event_ || flush_event_->IsSignaled()) { | 105 EGLint flags = 0; |
129 EGLint flags = 0; | 106 EGLTimeKHR time = EGL_FOREVER_KHR; |
130 EGLTimeKHR time = EGL_FOREVER_KHR; | 107 eglClientWaitSyncKHR(display_, sync_, flags, time); |
131 eglClientWaitSyncKHR(display_, sync_, flags, time); | |
132 } else { | |
133 LOG(ERROR) << "Trying to wait for uncommitted fence. Skipping..."; | |
134 } | |
135 } | 108 } |
136 | 109 |
137 virtual void ServerWait() OVERRIDE { | 110 virtual void ServerWait() OVERRIDE { |
138 if (!flush_event_ || flush_event_->IsSignaled()) { | 111 EGLint flags = 0; |
139 EGLint flags = 0; | 112 eglWaitSyncKHR(display_, sync_, flags); |
140 eglWaitSyncKHR(display_, sync_, flags); | |
141 } else { | |
142 LOG(ERROR) << "Trying to wait for uncommitted fence. Skipping..."; | |
143 } | |
144 } | 113 } |
145 | 114 |
146 | 115 |
147 private: | 116 private: |
148 virtual ~EGLFenceSync() { | 117 virtual ~EGLFenceSync() { |
149 eglDestroySyncKHR(display_, sync_); | 118 eglDestroySyncKHR(display_, sync_); |
150 } | 119 } |
151 | 120 |
152 EGLSyncKHR sync_; | 121 EGLSyncKHR sync_; |
153 EGLDisplay display_; | 122 EGLDisplay display_; |
154 scoped_refptr<gfx::GLContext::FlushEvent> flush_event_; | |
155 }; | 123 }; |
156 #endif // !OS_MACOSX | 124 #endif // !OS_MACOSX |
157 | 125 |
158 // static | 126 // static |
159 gfx::GLFence* CreateFence(bool flush) { | 127 gfx::GLFence* CreateFence(bool flush) { |
160 DCHECK(gfx::GLContext::GetCurrent()) | |
161 << "Trying to create fence with no context"; | |
162 | |
163 #if !defined(OS_MACOSX) | 128 #if !defined(OS_MACOSX) |
164 if (gfx::g_driver_egl.ext.b_EGL_KHR_fence_sync) | 129 if (gfx::g_driver_egl.ext.b_EGL_KHR_fence_sync) |
165 return new EGLFenceSync(flush); | 130 return new EGLFenceSync(flush); |
166 #endif | 131 #endif |
167 // Prefer ARB_sync which supports server-side wait. | 132 // Prefer ARB_sync which supports server-side wait. |
168 if (gfx::g_driver_gl.ext.b_GL_ARB_sync) | 133 if (gfx::g_driver_gl.ext.b_GL_ARB_sync) |
169 return new GLFenceARBSync(flush); | 134 return new GLFenceARBSync(flush); |
170 if (gfx::g_driver_gl.ext.b_GL_NV_fence) | 135 if (gfx::g_driver_gl.ext.b_GL_NV_fence) |
171 return new GLFenceNVFence(flush); | 136 return new GLFenceNVFence(flush); |
172 return NULL; | 137 return NULL; |
(...skipping 11 matching lines...) Expand all Loading... |
184 | 149 |
185 GLFence* GLFence::Create() { | 150 GLFence* GLFence::Create() { |
186 return CreateFence(true); | 151 return CreateFence(true); |
187 } | 152 } |
188 | 153 |
189 GLFence* GLFence::CreateWithoutFlush() { | 154 GLFence* GLFence::CreateWithoutFlush() { |
190 return CreateFence(false); | 155 return CreateFence(false); |
191 } | 156 } |
192 | 157 |
193 } // namespace gfx | 158 } // namespace gfx |
OLD | NEW |