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 extern "C" { | 5 extern "C" { |
6 #include <X11/Xlib.h> | 6 #include <X11/Xlib.h> |
7 } | 7 } |
8 | 8 |
9 #include "ui/gl/gl_surface_glx.h" | 9 #include "ui/gl/gl_surface_glx.h" |
10 | 10 |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 DCHECK(CalledOnValidThread()); | 116 DCHECK(CalledOnValidThread()); |
117 g_video_sync_thread = NULL; | 117 g_video_sync_thread = NULL; |
118 Stop(); | 118 Stop(); |
119 } | 119 } |
120 | 120 |
121 static SGIVideoSyncThread* g_video_sync_thread; | 121 static SGIVideoSyncThread* g_video_sync_thread; |
122 | 122 |
123 DISALLOW_COPY_AND_ASSIGN(SGIVideoSyncThread); | 123 DISALLOW_COPY_AND_ASSIGN(SGIVideoSyncThread); |
124 }; | 124 }; |
125 | 125 |
126 class SGIVideoSyncProviderThreadShim | 126 class SGIVideoSyncProviderThreadShim { |
127 : public base::SupportsWeakPtr<SGIVideoSyncProviderThreadShim> { | |
128 public: | 127 public: |
129 explicit SGIVideoSyncProviderThreadShim(XID window) | 128 explicit SGIVideoSyncProviderThreadShim(XID window) |
130 : window_(window), | 129 : window_(window), |
131 context_(NULL), | 130 context_(NULL), |
132 message_loop_(base::MessageLoopProxy::current()), | 131 message_loop_(base::MessageLoopProxy::current()), |
133 cancel_vsync_flag_(), | 132 cancel_vsync_flag_(), |
134 vsync_lock_() { | 133 vsync_lock_() { |
135 // This ensures that creation of |window_| has occured when this shim | 134 // This ensures that creation of |window_| has occured when this shim |
136 // is executing in the same process as the call to create |window_|. | 135 // is executing in the same process as the call to create |window_|. |
137 XSync(g_display, False); | 136 XSync(g_display, False); |
138 } | 137 } |
139 | 138 |
| 139 virtual ~SGIVideoSyncProviderThreadShim() { |
| 140 if (context_) { |
| 141 glXDestroyContext(display_, context_); |
| 142 context_ = NULL; |
| 143 } |
| 144 } |
| 145 |
140 base::CancellationFlag* cancel_vsync_flag() { | 146 base::CancellationFlag* cancel_vsync_flag() { |
141 return &cancel_vsync_flag_; | 147 return &cancel_vsync_flag_; |
142 } | 148 } |
143 | 149 |
144 base::Lock* vsync_lock() { | 150 base::Lock* vsync_lock() { |
145 return &vsync_lock_; | 151 return &vsync_lock_; |
146 } | 152 } |
147 | 153 |
148 void Initialize() { | 154 void Initialize() { |
149 DCHECK(display_); | 155 DCHECK(display_); |
(...skipping 17 matching lines...) Expand all Loading... |
167 if (visual_info_count == 0) { | 173 if (visual_info_count == 0) { |
168 LOG(ERROR) << "No visual info for visual ID."; | 174 LOG(ERROR) << "No visual info for visual ID."; |
169 return; | 175 return; |
170 } | 176 } |
171 | 177 |
172 context_ = glXCreateContext(display_, visual_info_list.get(), NULL, True); | 178 context_ = glXCreateContext(display_, visual_info_list.get(), NULL, True); |
173 | 179 |
174 DCHECK(NULL != context_); | 180 DCHECK(NULL != context_); |
175 } | 181 } |
176 | 182 |
177 void Destroy() { | |
178 if (context_) { | |
179 glXDestroyContext(display_, context_); | |
180 context_ = NULL; | |
181 } | |
182 delete this; | |
183 } | |
184 | |
185 void GetVSyncParameters(const VSyncProvider::UpdateVSyncCallback& callback) { | 183 void GetVSyncParameters(const VSyncProvider::UpdateVSyncCallback& callback) { |
186 base::TimeTicks now; | 184 base::TimeTicks now; |
187 { | 185 { |
188 // Don't allow |window_| destruction while we're probing vsync. | 186 // Don't allow |window_| destruction while we're probing vsync. |
189 base::AutoLock locked(vsync_lock_); | 187 base::AutoLock locked(vsync_lock_); |
190 | 188 |
191 if (!context_ || cancel_vsync_flag_.IsSet()) | 189 if (!context_ || cancel_vsync_flag_.IsSet()) |
192 return; | 190 return; |
193 | 191 |
194 glXMakeCurrent(display_, window_, context_); | 192 glXMakeCurrent(display_, window_, context_); |
(...skipping 13 matching lines...) Expand all Loading... |
208 | 206 |
209 message_loop_->PostTask( | 207 message_loop_->PostTask( |
210 FROM_HERE, base::Bind(callback, now, kDefaultInterval)); | 208 FROM_HERE, base::Bind(callback, now, kDefaultInterval)); |
211 } | 209 } |
212 | 210 |
213 private: | 211 private: |
214 // For initialization of display_ in GLSurface::InitializeOneOff before | 212 // For initialization of display_ in GLSurface::InitializeOneOff before |
215 // the sandbox goes up. | 213 // the sandbox goes up. |
216 friend class gfx::GLSurfaceGLX; | 214 friend class gfx::GLSurfaceGLX; |
217 | 215 |
218 virtual ~SGIVideoSyncProviderThreadShim() { | |
219 } | |
220 | |
221 static Display* display_; | 216 static Display* display_; |
222 | 217 |
223 XID window_; | 218 XID window_; |
224 GLXContext context_; | 219 GLXContext context_; |
225 | 220 |
226 scoped_refptr<base::MessageLoopProxy> message_loop_; | 221 scoped_refptr<base::MessageLoopProxy> message_loop_; |
227 | 222 |
228 base::CancellationFlag cancel_vsync_flag_; | 223 base::CancellationFlag cancel_vsync_flag_; |
229 base::Lock vsync_lock_; | 224 base::Lock vsync_lock_; |
230 | 225 |
231 DISALLOW_COPY_AND_ASSIGN(SGIVideoSyncProviderThreadShim); | 226 DISALLOW_COPY_AND_ASSIGN(SGIVideoSyncProviderThreadShim); |
232 }; | 227 }; |
233 | 228 |
234 class SGIVideoSyncVSyncProvider | 229 class SGIVideoSyncVSyncProvider |
235 : public gfx::VSyncProvider, | 230 : public gfx::VSyncProvider, |
236 public base::SupportsWeakPtr<SGIVideoSyncVSyncProvider> { | 231 public base::SupportsWeakPtr<SGIVideoSyncVSyncProvider> { |
237 public: | 232 public: |
238 explicit SGIVideoSyncVSyncProvider(gfx::AcceleratedWidget window) | 233 explicit SGIVideoSyncVSyncProvider(gfx::AcceleratedWidget window) |
239 : vsync_thread_(SGIVideoSyncThread::Create()), | 234 : vsync_thread_(SGIVideoSyncThread::Create()), |
240 shim_((new SGIVideoSyncProviderThreadShim(window))->AsWeakPtr()), | 235 shim_(new SGIVideoSyncProviderThreadShim(window)), |
241 cancel_vsync_flag_(shim_->cancel_vsync_flag()), | 236 cancel_vsync_flag_(shim_->cancel_vsync_flag()), |
242 vsync_lock_(shim_->vsync_lock()) { | 237 vsync_lock_(shim_->vsync_lock()) { |
243 // The WeakPtr is bound to the SGIVideoSyncThread. We only use it for | |
244 // PostTask. | |
245 shim_->DetachFromThread(); | |
246 vsync_thread_->message_loop()->PostTask( | 238 vsync_thread_->message_loop()->PostTask( |
247 FROM_HERE, | 239 FROM_HERE, |
248 base::Bind(&SGIVideoSyncProviderThreadShim::Initialize, shim_)); | 240 base::Bind(&SGIVideoSyncProviderThreadShim::Initialize, |
| 241 base::Unretained(shim_.get()))); |
249 } | 242 } |
250 | 243 |
251 virtual ~SGIVideoSyncVSyncProvider() { | 244 virtual ~SGIVideoSyncVSyncProvider() { |
252 { | 245 { |
253 base::AutoLock locked(*vsync_lock_); | 246 base::AutoLock locked(*vsync_lock_); |
254 cancel_vsync_flag_->Set(); | 247 cancel_vsync_flag_->Set(); |
255 } | 248 } |
256 vsync_thread_->message_loop()->PostTask( | 249 |
| 250 // Hand-off |shim_| to be deleted on the |vsync_thread_|. |
| 251 vsync_thread_->message_loop()->DeleteSoon( |
257 FROM_HERE, | 252 FROM_HERE, |
258 base::Bind(&SGIVideoSyncProviderThreadShim::Destroy, shim_)); | 253 shim_.release()); |
259 } | 254 } |
260 | 255 |
261 virtual void GetVSyncParameters( | 256 virtual void GetVSyncParameters( |
262 const VSyncProvider::UpdateVSyncCallback& callback) OVERRIDE { | 257 const VSyncProvider::UpdateVSyncCallback& callback) OVERRIDE { |
263 // Only one outstanding request per surface. | 258 // Only one outstanding request per surface. |
264 if (!pending_callback_) { | 259 if (!pending_callback_) { |
265 pending_callback_.reset( | 260 pending_callback_.reset( |
266 new VSyncProvider::UpdateVSyncCallback(callback)); | 261 new VSyncProvider::UpdateVSyncCallback(callback)); |
267 vsync_thread_->message_loop()->PostTask( | 262 vsync_thread_->message_loop()->PostTask( |
268 FROM_HERE, | 263 FROM_HERE, |
269 base::Bind(&SGIVideoSyncProviderThreadShim::GetVSyncParameters, | 264 base::Bind(&SGIVideoSyncProviderThreadShim::GetVSyncParameters, |
270 shim_, base::Bind( | 265 base::Unretained(shim_.get()), |
| 266 base::Bind( |
271 &SGIVideoSyncVSyncProvider::PendingCallbackRunner, | 267 &SGIVideoSyncVSyncProvider::PendingCallbackRunner, |
272 AsWeakPtr()))); | 268 AsWeakPtr()))); |
273 } | 269 } |
274 } | 270 } |
275 | 271 |
276 private: | 272 private: |
277 void PendingCallbackRunner(const base::TimeTicks timebase, | 273 void PendingCallbackRunner(const base::TimeTicks timebase, |
278 const base::TimeDelta interval) { | 274 const base::TimeDelta interval) { |
279 DCHECK(pending_callback_); | 275 DCHECK(pending_callback_); |
280 pending_callback_->Run(timebase, interval); | 276 pending_callback_->Run(timebase, interval); |
281 pending_callback_.reset(); | 277 pending_callback_.reset(); |
282 } | 278 } |
283 | 279 |
284 scoped_refptr<SGIVideoSyncThread> vsync_thread_; | 280 scoped_refptr<SGIVideoSyncThread> vsync_thread_; |
285 base::WeakPtr<SGIVideoSyncProviderThreadShim> shim_; | 281 |
| 282 // Thread shim through which the sync provider is accessed on |vsync_thread_|. |
| 283 scoped_ptr<SGIVideoSyncProviderThreadShim> shim_; |
286 | 284 |
287 scoped_ptr<VSyncProvider::UpdateVSyncCallback> pending_callback_; | 285 scoped_ptr<VSyncProvider::UpdateVSyncCallback> pending_callback_; |
288 | 286 |
289 // Raw pointers to sync primitives owned by the shim_. | 287 // Raw pointers to sync primitives owned by the shim_. |
290 // These will only be referenced before we post a task to destroy | 288 // These will only be referenced before we post a task to destroy |
291 // the shim_, so they are safe to access. | 289 // the shim_, so they are safe to access. |
292 base::CancellationFlag* cancel_vsync_flag_; | 290 base::CancellationFlag* cancel_vsync_flag_; |
293 base::Lock* vsync_lock_; | 291 base::Lock* vsync_lock_; |
294 | 292 |
295 DISALLOW_COPY_AND_ASSIGN(SGIVideoSyncVSyncProvider); | 293 DISALLOW_COPY_AND_ASSIGN(SGIVideoSyncVSyncProvider); |
(...skipping 319 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
615 | 613 |
616 void* PbufferGLSurfaceGLX::GetConfig() { | 614 void* PbufferGLSurfaceGLX::GetConfig() { |
617 return config_; | 615 return config_; |
618 } | 616 } |
619 | 617 |
620 PbufferGLSurfaceGLX::~PbufferGLSurfaceGLX() { | 618 PbufferGLSurfaceGLX::~PbufferGLSurfaceGLX() { |
621 Destroy(); | 619 Destroy(); |
622 } | 620 } |
623 | 621 |
624 } // namespace gfx | 622 } // namespace gfx |
OLD | NEW |