OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "cc/output/output_surface.h" | 5 #include "cc/output/output_surface.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/debug/trace_event.h" | 12 #include "base/debug/trace_event.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
15 #include "base/strings/string_split.h" | 15 #include "base/strings/string_split.h" |
16 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
17 #include "cc/output/compositor_frame.h" | 17 #include "cc/output/compositor_frame.h" |
18 #include "cc/output/managed_memory_policy.h" | 18 #include "cc/output/managed_memory_policy.h" |
19 #include "cc/output/output_surface_client.h" | 19 #include "cc/output/output_surface_client.h" |
20 #include "cc/scheduler/delay_based_time_source.h" | 20 #include "cc/scheduler/delay_based_time_source.h" |
21 #include "third_party/WebKit/public/platform/WebGraphicsContext3D.h" | 21 #include "third_party/WebKit/public/platform/WebGraphicsContext3D.h" |
22 #include "third_party/WebKit/public/platform/WebGraphicsMemoryAllocation.h" | |
23 #include "third_party/khronos/GLES2/gl2.h" | 22 #include "third_party/khronos/GLES2/gl2.h" |
24 #include "third_party/khronos/GLES2/gl2ext.h" | 23 #include "third_party/khronos/GLES2/gl2ext.h" |
25 #include "ui/gfx/rect.h" | 24 #include "ui/gfx/rect.h" |
26 #include "ui/gfx/size.h" | 25 #include "ui/gfx/size.h" |
27 | 26 |
28 using std::set; | 27 using std::set; |
29 using std::string; | 28 using std::string; |
30 using std::vector; | 29 using std::vector; |
31 | 30 |
32 namespace cc { | 31 namespace cc { |
33 namespace { | |
34 | |
35 ManagedMemoryPolicy::PriorityCutoff ConvertPriorityCutoff( | |
36 WebKit::WebGraphicsMemoryAllocation::PriorityCutoff priority_cutoff) { | |
37 // This is simple a 1:1 map, the names differ only because the WebKit names | |
38 // should be to match the cc names. | |
39 switch (priority_cutoff) { | |
40 case WebKit::WebGraphicsMemoryAllocation::PriorityCutoffAllowNothing: | |
41 return ManagedMemoryPolicy::CUTOFF_ALLOW_NOTHING; | |
42 case WebKit::WebGraphicsMemoryAllocation::PriorityCutoffAllowVisibleOnly: | |
43 return ManagedMemoryPolicy::CUTOFF_ALLOW_REQUIRED_ONLY; | |
44 case WebKit::WebGraphicsMemoryAllocation:: | |
45 PriorityCutoffAllowVisibleAndNearby: | |
46 return ManagedMemoryPolicy::CUTOFF_ALLOW_NICE_TO_HAVE; | |
47 case WebKit::WebGraphicsMemoryAllocation::PriorityCutoffAllowEverything: | |
48 return ManagedMemoryPolicy::CUTOFF_ALLOW_EVERYTHING; | |
49 } | |
50 NOTREACHED(); | |
51 return ManagedMemoryPolicy::CUTOFF_ALLOW_NOTHING; | |
52 } | |
53 | |
54 } // anonymous namespace | |
55 | |
56 class OutputSurfaceCallbacks | |
57 : public WebKit::WebGraphicsContext3D:: | |
58 WebGraphicsSwapBuffersCompleteCallbackCHROMIUM, | |
59 public WebKit::WebGraphicsContext3D::WebGraphicsContextLostCallback, | |
60 public WebKit::WebGraphicsContext3D:: | |
61 WebGraphicsMemoryAllocationChangedCallbackCHROMIUM { | |
62 public: | |
63 explicit OutputSurfaceCallbacks(OutputSurface* client) | |
64 : client_(client) { | |
65 DCHECK(client_); | |
66 } | |
67 | |
68 // WK:WGC3D::WGSwapBuffersCompleteCallbackCHROMIUM implementation. | |
69 virtual void onSwapBuffersComplete() { client_->OnSwapBuffersComplete(NULL); } | |
70 | |
71 // WK:WGC3D::WGContextLostCallback implementation. | |
72 virtual void onContextLost() { client_->DidLoseOutputSurface(); } | |
73 | |
74 // WK:WGC3D::WGMemoryAllocationChangedCallbackCHROMIUM implementation. | |
75 virtual void onMemoryAllocationChanged( | |
76 WebKit::WebGraphicsMemoryAllocation allocation) { | |
77 ManagedMemoryPolicy policy( | |
78 allocation.bytesLimitWhenVisible, | |
79 ConvertPriorityCutoff(allocation.priorityCutoffWhenVisible), | |
80 allocation.bytesLimitWhenNotVisible, | |
81 ConvertPriorityCutoff(allocation.priorityCutoffWhenNotVisible), | |
82 ManagedMemoryPolicy::kDefaultNumResourcesLimit); | |
83 bool discard_backbuffer = !allocation.suggestHaveBackbuffer; | |
84 client_->SetMemoryPolicy(policy, discard_backbuffer); | |
85 } | |
86 | |
87 private: | |
88 OutputSurface* client_; | |
89 }; | |
90 | 32 |
91 OutputSurface::OutputSurface( | 33 OutputSurface::OutputSurface( |
92 scoped_ptr<WebKit::WebGraphicsContext3D> context3d) | 34 scoped_refptr<ContextProvider> context_provider) |
93 : context3d_(context3d.Pass()), | 35 : context_provider_(context_provider), |
94 has_gl_discard_backbuffer_(false), | 36 has_gl_discard_backbuffer_(false), |
95 has_swap_buffers_complete_callback_(false), | 37 has_swap_buffers_complete_callback_(false), |
96 device_scale_factor_(-1), | 38 device_scale_factor_(-1), |
97 weak_ptr_factory_(this), | 39 weak_ptr_factory_(this), |
98 max_frames_pending_(0), | 40 max_frames_pending_(0), |
99 pending_swap_buffers_(0), | 41 pending_swap_buffers_(0), |
100 needs_begin_frame_(false), | 42 needs_begin_frame_(false), |
101 begin_frame_pending_(false), | 43 begin_frame_pending_(false), |
102 client_(NULL), | 44 client_(NULL), |
103 check_for_retroactive_begin_frame_pending_(false) { | 45 check_for_retroactive_begin_frame_pending_(false) { |
104 } | 46 } |
105 | 47 |
106 OutputSurface::OutputSurface( | 48 OutputSurface::OutputSurface( |
107 scoped_ptr<cc::SoftwareOutputDevice> software_device) | 49 scoped_ptr<cc::SoftwareOutputDevice> software_device) |
108 : software_device_(software_device.Pass()), | 50 : software_device_(software_device.Pass()), |
109 has_gl_discard_backbuffer_(false), | 51 has_gl_discard_backbuffer_(false), |
110 has_swap_buffers_complete_callback_(false), | 52 has_swap_buffers_complete_callback_(false), |
111 device_scale_factor_(-1), | 53 device_scale_factor_(-1), |
112 weak_ptr_factory_(this), | 54 weak_ptr_factory_(this), |
113 max_frames_pending_(0), | 55 max_frames_pending_(0), |
114 pending_swap_buffers_(0), | 56 pending_swap_buffers_(0), |
115 needs_begin_frame_(false), | 57 needs_begin_frame_(false), |
116 begin_frame_pending_(false), | 58 begin_frame_pending_(false), |
117 client_(NULL), | 59 client_(NULL), |
118 check_for_retroactive_begin_frame_pending_(false) { | 60 check_for_retroactive_begin_frame_pending_(false) { |
119 } | 61 } |
120 | 62 |
121 OutputSurface::OutputSurface( | 63 OutputSurface::OutputSurface( |
122 scoped_ptr<WebKit::WebGraphicsContext3D> context3d, | 64 scoped_refptr<ContextProvider> context_provider, |
123 scoped_ptr<cc::SoftwareOutputDevice> software_device) | 65 scoped_ptr<cc::SoftwareOutputDevice> software_device) |
124 : context3d_(context3d.Pass()), | 66 : context_provider_(context_provider), |
125 software_device_(software_device.Pass()), | 67 software_device_(software_device.Pass()), |
126 has_gl_discard_backbuffer_(false), | 68 has_gl_discard_backbuffer_(false), |
127 has_swap_buffers_complete_callback_(false), | 69 has_swap_buffers_complete_callback_(false), |
128 device_scale_factor_(-1), | 70 device_scale_factor_(-1), |
129 weak_ptr_factory_(this), | 71 weak_ptr_factory_(this), |
130 max_frames_pending_(0), | 72 max_frames_pending_(0), |
131 pending_swap_buffers_(0), | 73 pending_swap_buffers_(0), |
132 needs_begin_frame_(false), | 74 needs_begin_frame_(false), |
133 begin_frame_pending_(false), | 75 begin_frame_pending_(false), |
134 client_(NULL), | 76 client_(NULL), |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
280 } | 222 } |
281 | 223 |
282 void OutputSurface::SetExternalDrawConstraints(const gfx::Transform& transform, | 224 void OutputSurface::SetExternalDrawConstraints(const gfx::Transform& transform, |
283 gfx::Rect viewport) { | 225 gfx::Rect viewport) { |
284 client_->SetExternalDrawConstraints(transform, viewport); | 226 client_->SetExternalDrawConstraints(transform, viewport); |
285 } | 227 } |
286 | 228 |
287 OutputSurface::~OutputSurface() { | 229 OutputSurface::~OutputSurface() { |
288 if (frame_rate_controller_) | 230 if (frame_rate_controller_) |
289 frame_rate_controller_->SetActive(false); | 231 frame_rate_controller_->SetActive(false); |
290 | 232 ResetContext3d(); |
291 if (context3d_) { | |
292 context3d_->setSwapBuffersCompleteCallbackCHROMIUM(NULL); | |
293 context3d_->setContextLostCallback(NULL); | |
294 context3d_->setMemoryAllocationChangedCallbackCHROMIUM(NULL); | |
295 } | |
296 } | 233 } |
297 | 234 |
298 bool OutputSurface::ForcedDrawToSoftwareDevice() const { | 235 bool OutputSurface::ForcedDrawToSoftwareDevice() const { |
299 return false; | 236 return false; |
300 } | 237 } |
301 | 238 |
302 bool OutputSurface::BindToClient(cc::OutputSurfaceClient* client) { | 239 bool OutputSurface::BindToClient(cc::OutputSurfaceClient* client) { |
303 DCHECK(client); | 240 DCHECK(client); |
304 client_ = client; | 241 client_ = client; |
305 bool success = true; | 242 bool success = true; |
306 | 243 |
307 if (context3d_) { | 244 if (context_provider_) { |
308 success = context3d_->makeContextCurrent(); | 245 success = context_provider_->BindToCurrentThread(); |
309 if (success) | 246 if (success) |
310 SetContext3D(context3d_.Pass()); | 247 SetUpContext3d(); |
311 } | 248 } |
312 | 249 |
313 if (!success) | 250 if (!success) |
314 client_ = NULL; | 251 client_ = NULL; |
315 | 252 |
316 return success; | 253 return success; |
317 } | 254 } |
318 | 255 |
319 bool OutputSurface::InitializeAndSetContext3D( | 256 bool OutputSurface::InitializeAndSetContext3d( |
320 scoped_ptr<WebKit::WebGraphicsContext3D> context3d, | 257 scoped_refptr<ContextProvider> context_provider, |
321 scoped_refptr<ContextProvider> offscreen_context_provider) { | 258 scoped_refptr<ContextProvider> offscreen_context_provider) { |
322 DCHECK(!context3d_); | 259 DCHECK(!context_provider_); |
323 DCHECK(context3d); | 260 DCHECK(context_provider); |
324 DCHECK(client_); | 261 DCHECK(client_); |
325 | 262 |
326 bool success = false; | 263 bool success = false; |
327 if (context3d->makeContextCurrent()) { | 264 if (context_provider->BindToCurrentThread()) { |
328 SetContext3D(context3d.Pass()); | 265 context_provider_ = context_provider; |
| 266 SetUpContext3d(); |
329 if (client_->DeferredInitialize(offscreen_context_provider)) | 267 if (client_->DeferredInitialize(offscreen_context_provider)) |
330 success = true; | 268 success = true; |
331 } | 269 } |
332 | 270 |
333 if (!success) | 271 if (!success) |
334 ResetContext3D(); | 272 ResetContext3d(); |
335 | 273 |
336 return success; | 274 return success; |
337 } | 275 } |
338 | 276 |
339 void OutputSurface::ReleaseGL() { | 277 void OutputSurface::ReleaseGL() { |
340 DCHECK(client_); | 278 DCHECK(client_); |
341 DCHECK(context3d_); | 279 DCHECK(context_provider_); |
342 client_->ReleaseGL(); | 280 client_->ReleaseGL(); |
343 ResetContext3D(); | 281 ResetContext3d(); |
344 } | 282 } |
345 | 283 |
346 void OutputSurface::SetContext3D( | 284 void OutputSurface::SetUpContext3d() { |
347 scoped_ptr<WebKit::WebGraphicsContext3D> context3d) { | 285 DCHECK(context_provider_); |
348 DCHECK(!context3d_); | |
349 DCHECK(context3d); | |
350 DCHECK(client_); | 286 DCHECK(client_); |
351 | 287 |
352 string extensions_string = UTF16ToASCII(context3d->getString(GL_EXTENSIONS)); | 288 WebKit::WebGraphicsContext3D* context3d = context_provider_->Context3d(); |
| 289 |
| 290 string extensions_string = |
| 291 UTF16ToASCII(context3d->getString(GL_EXTENSIONS)); |
353 vector<string> extensions_list; | 292 vector<string> extensions_list; |
354 base::SplitString(extensions_string, ' ', &extensions_list); | 293 base::SplitString(extensions_string, ' ', &extensions_list); |
355 set<string> extensions(extensions_list.begin(), extensions_list.end()); | 294 set<string> extensions(extensions_list.begin(), extensions_list.end()); |
356 has_gl_discard_backbuffer_ = | 295 has_gl_discard_backbuffer_ = |
357 extensions.count("GL_CHROMIUM_discard_backbuffer") > 0; | 296 extensions.count("GL_CHROMIUM_discard_backbuffer") > 0; |
358 has_swap_buffers_complete_callback_ = | 297 has_swap_buffers_complete_callback_ = |
359 extensions.count("GL_CHROMIUM_swapbuffers_complete_callback") > 0; | 298 extensions.count("GL_CHROMIUM_swapbuffers_complete_callback") > 0; |
360 | 299 |
361 | 300 context_provider_->SetLostContextCallback( |
362 context3d_ = context3d.Pass(); | 301 base::Bind(&OutputSurface::DidLoseOutputSurface, |
363 callbacks_.reset(new OutputSurfaceCallbacks(this)); | 302 base::Unretained(this))); |
364 context3d_->setSwapBuffersCompleteCallbackCHROMIUM(callbacks_.get()); | 303 context_provider_->SetSwapBuffersCompleteCallback( |
365 context3d_->setContextLostCallback(callbacks_.get()); | 304 base::Bind(&OutputSurface::OnSwapBuffersComplete, |
366 context3d_->setMemoryAllocationChangedCallbackCHROMIUM(callbacks_.get()); | 305 base::Unretained(this), |
| 306 static_cast<CompositorFrameAck*>(NULL))); |
| 307 context_provider_->SetMemoryPolicyChangedCallback( |
| 308 base::Bind(&OutputSurface::SetMemoryPolicy, |
| 309 base::Unretained(this))); |
367 } | 310 } |
368 | 311 |
369 void OutputSurface::ResetContext3D() { | 312 void OutputSurface::ResetContext3d() { |
370 context3d_.reset(); | 313 if (context_provider_.get()) { |
371 callbacks_.reset(); | 314 context_provider_->SetLostContextCallback( |
| 315 ContextProvider::LostContextCallback()); |
| 316 context_provider_->SetSwapBuffersCompleteCallback( |
| 317 ContextProvider::SwapBuffersCompleteCallback()); |
| 318 context_provider_->SetMemoryPolicyChangedCallback( |
| 319 ContextProvider::MemoryPolicyChangedCallback()); |
| 320 } |
| 321 context_provider_ = NULL; |
372 } | 322 } |
373 | 323 |
374 void OutputSurface::EnsureBackbuffer() { | 324 void OutputSurface::EnsureBackbuffer() { |
375 DCHECK(context3d_); | 325 DCHECK(context_provider_); |
376 if (has_gl_discard_backbuffer_) | 326 if (has_gl_discard_backbuffer_) |
377 context3d_->ensureBackbufferCHROMIUM(); | 327 context_provider_->Context3d()->ensureBackbufferCHROMIUM(); |
378 } | 328 } |
379 | 329 |
380 void OutputSurface::DiscardBackbuffer() { | 330 void OutputSurface::DiscardBackbuffer() { |
381 DCHECK(context3d_); | 331 DCHECK(context_provider_); |
382 if (has_gl_discard_backbuffer_) | 332 if (has_gl_discard_backbuffer_) |
383 context3d_->discardBackbufferCHROMIUM(); | 333 context_provider_->Context3d()->discardBackbufferCHROMIUM(); |
384 } | 334 } |
385 | 335 |
386 void OutputSurface::Reshape(gfx::Size size, float scale_factor) { | 336 void OutputSurface::Reshape(gfx::Size size, float scale_factor) { |
387 if (size == surface_size_ && scale_factor == device_scale_factor_) | 337 if (size == surface_size_ && scale_factor == device_scale_factor_) |
388 return; | 338 return; |
389 | 339 |
390 surface_size_ = size; | 340 surface_size_ = size; |
391 device_scale_factor_ = scale_factor; | 341 device_scale_factor_ = scale_factor; |
392 if (context3d_) { | 342 if (context_provider_) { |
393 context3d_->reshapeWithScaleFactor( | 343 context_provider_->Context3d()->reshapeWithScaleFactor( |
394 size.width(), size.height(), scale_factor); | 344 size.width(), size.height(), scale_factor); |
395 } | 345 } |
396 if (software_device_) | 346 if (software_device_) |
397 software_device_->Resize(size); | 347 software_device_->Resize(size); |
398 } | 348 } |
399 | 349 |
400 gfx::Size OutputSurface::SurfaceSize() const { | 350 gfx::Size OutputSurface::SurfaceSize() const { |
401 return surface_size_; | 351 return surface_size_; |
402 } | 352 } |
403 | 353 |
404 void OutputSurface::BindFramebuffer() { | 354 void OutputSurface::BindFramebuffer() { |
405 DCHECK(context3d_); | 355 DCHECK(context_provider_); |
406 context3d_->bindFramebuffer(GL_FRAMEBUFFER, 0); | 356 context_provider_->Context3d()->bindFramebuffer(GL_FRAMEBUFFER, 0); |
407 } | 357 } |
408 | 358 |
409 void OutputSurface::SwapBuffers(cc::CompositorFrame* frame) { | 359 void OutputSurface::SwapBuffers(cc::CompositorFrame* frame) { |
410 if (frame->software_frame_data) { | 360 if (frame->software_frame_data) { |
411 PostSwapBuffersComplete(); | 361 PostSwapBuffersComplete(); |
412 DidSwapBuffers(); | 362 DidSwapBuffers(); |
413 return; | 363 return; |
414 } | 364 } |
415 | 365 |
416 DCHECK(context3d_); | 366 DCHECK(context_provider_); |
417 DCHECK(frame->gl_frame_data); | 367 DCHECK(frame->gl_frame_data); |
418 | 368 |
419 if (frame->gl_frame_data->sub_buffer_rect == | 369 if (frame->gl_frame_data->sub_buffer_rect == |
420 gfx::Rect(frame->gl_frame_data->size)) { | 370 gfx::Rect(frame->gl_frame_data->size)) { |
421 // Note that currently this has the same effect as SwapBuffers; we should | 371 // Note that currently this has the same effect as SwapBuffers; we should |
422 // consider exposing a different entry point on WebGraphicsContext3D. | 372 // consider exposing a different entry point on WebGraphicsContext3D. |
423 context3d()->prepareTexture(); | 373 context_provider_->Context3d()->prepareTexture(); |
424 } else { | 374 } else { |
425 gfx::Rect sub_buffer_rect = frame->gl_frame_data->sub_buffer_rect; | 375 gfx::Rect sub_buffer_rect = frame->gl_frame_data->sub_buffer_rect; |
426 context3d()->postSubBufferCHROMIUM(sub_buffer_rect.x(), | 376 context_provider_->Context3d()->postSubBufferCHROMIUM( |
427 sub_buffer_rect.y(), | 377 sub_buffer_rect.x(), |
428 sub_buffer_rect.width(), | 378 sub_buffer_rect.y(), |
429 sub_buffer_rect.height()); | 379 sub_buffer_rect.width(), |
| 380 sub_buffer_rect.height()); |
430 } | 381 } |
431 | 382 |
432 if (!has_swap_buffers_complete_callback_) | 383 if (!has_swap_buffers_complete_callback_) |
433 PostSwapBuffersComplete(); | 384 PostSwapBuffersComplete(); |
434 | 385 |
435 DidSwapBuffers(); | 386 DidSwapBuffers(); |
436 } | 387 } |
437 | 388 |
438 void OutputSurface::PostSwapBuffersComplete() { | 389 void OutputSurface::PostSwapBuffersComplete() { |
439 base::MessageLoop::current()->PostTask( | 390 base::MessageLoop::current()->PostTask( |
(...skipping 10 matching lines...) Expand all Loading... |
450 "discard_backbuffer", discard_backbuffer); | 401 "discard_backbuffer", discard_backbuffer); |
451 // Just ignore the memory manager when it says to set the limit to zero | 402 // Just ignore the memory manager when it says to set the limit to zero |
452 // bytes. This will happen when the memory manager thinks that the renderer | 403 // bytes. This will happen when the memory manager thinks that the renderer |
453 // is not visible (which the renderer knows better). | 404 // is not visible (which the renderer knows better). |
454 if (policy.bytes_limit_when_visible) | 405 if (policy.bytes_limit_when_visible) |
455 client_->SetMemoryPolicy(policy); | 406 client_->SetMemoryPolicy(policy); |
456 client_->SetDiscardBackBufferWhenNotVisible(discard_backbuffer); | 407 client_->SetDiscardBackBufferWhenNotVisible(discard_backbuffer); |
457 } | 408 } |
458 | 409 |
459 } // namespace cc | 410 } // namespace cc |
OLD | NEW |