Chromium Code Reviews| 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/compositor/compositor.h" | 5 #include "ui/compositor/compositor.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <deque> | 8 #include <deque> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 12 #include "base/debug/trace_event.h" | 12 #include "base/debug/trace_event.h" |
| 13 #include "base/memory/singleton.h" | 13 #include "base/memory/singleton.h" |
| 14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
| 15 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
| 16 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
| 17 #include "base/sys_info.h" | 17 #include "base/sys_info.h" |
| 18 #include "base/threading/thread.h" | 18 #include "base/threading/thread.h" |
| 19 #include "base/threading/thread_restrictions.h" | 19 #include "base/threading/thread_restrictions.h" |
| 20 #include "cc/base/switches.h" | 20 #include "cc/base/switches.h" |
| 21 #include "cc/debug/test_context_provider.h" | |
| 22 #include "cc/debug/test_web_graphics_context_3d.h" | |
| 23 #include "cc/input/input_handler.h" | 21 #include "cc/input/input_handler.h" |
| 24 #include "cc/layers/layer.h" | 22 #include "cc/layers/layer.h" |
| 25 #include "cc/output/context_provider.h" | 23 #include "cc/output/context_provider.h" |
| 26 #include "cc/output/output_surface.h" | |
| 27 #include "cc/trees/layer_tree_host.h" | 24 #include "cc/trees/layer_tree_host.h" |
| 28 #include "third_party/skia/include/core/SkBitmap.h" | 25 #include "third_party/skia/include/core/SkBitmap.h" |
| 29 #include "ui/compositor/compositor_observer.h" | 26 #include "ui/compositor/compositor_observer.h" |
| 30 #include "ui/compositor/compositor_switches.h" | 27 #include "ui/compositor/compositor_switches.h" |
| 31 #include "ui/compositor/dip_util.h" | 28 #include "ui/compositor/dip_util.h" |
| 32 #include "ui/compositor/layer.h" | 29 #include "ui/compositor/layer.h" |
| 33 #include "ui/compositor/reflector.h" | |
| 34 #include "ui/gl/gl_context.h" | 30 #include "ui/gl/gl_context.h" |
| 35 #include "ui/gl/gl_implementation.h" | |
| 36 #include "ui/gl/gl_surface.h" | |
| 37 #include "ui/gl/gl_switches.h" | 31 #include "ui/gl/gl_switches.h" |
| 38 #include "webkit/common/gpu/context_provider_in_process.h" | |
| 39 #include "webkit/common/gpu/grcontext_for_webgraphicscontext3d.h" | |
| 40 #include "webkit/common/gpu/webgraphicscontext3d_in_process_command_buffer_impl. h" | |
| 41 | 32 |
| 42 namespace { | 33 namespace { |
| 43 | 34 |
| 44 const double kDefaultRefreshRate = 60.0; | 35 const double kDefaultRefreshRate = 60.0; |
| 45 const double kTestRefreshRate = 200.0; | 36 const double kTestRefreshRate = 200.0; |
| 46 | 37 |
| 47 enum SwapType { | 38 enum SwapType { |
| 48 DRAW_SWAP, | 39 DRAW_SWAP, |
| 49 READPIXELS_SWAP, | 40 READPIXELS_SWAP, |
| 50 }; | 41 }; |
| 51 | 42 |
| 52 bool g_compositor_initialized = false; | 43 bool g_compositor_initialized = false; |
| 53 base::Thread* g_compositor_thread = NULL; | 44 base::Thread* g_compositor_thread = NULL; |
| 54 | 45 |
| 55 ui::ContextFactory* g_implicit_factory = NULL; | |
| 56 ui::ContextFactory* g_context_factory = NULL; | 46 ui::ContextFactory* g_context_factory = NULL; |
| 57 | 47 |
| 58 const int kCompositorLockTimeoutMs = 67; | 48 const int kCompositorLockTimeoutMs = 67; |
| 59 | 49 |
| 60 class PendingSwap { | 50 class PendingSwap { |
| 61 public: | 51 public: |
| 62 PendingSwap(SwapType type, ui::PostedSwapQueue* posted_swaps); | 52 PendingSwap(SwapType type, ui::PostedSwapQueue* posted_swaps); |
| 63 ~PendingSwap(); | 53 ~PendingSwap(); |
| 64 | 54 |
| 65 SwapType type() const { return type_; } | 55 SwapType type() const { return type_; } |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 83 ContextFactory* ContextFactory::GetInstance() { | 73 ContextFactory* ContextFactory::GetInstance() { |
| 84 DCHECK(g_context_factory); | 74 DCHECK(g_context_factory); |
| 85 return g_context_factory; | 75 return g_context_factory; |
| 86 } | 76 } |
| 87 | 77 |
| 88 // static | 78 // static |
| 89 void ContextFactory::SetInstance(ContextFactory* instance) { | 79 void ContextFactory::SetInstance(ContextFactory* instance) { |
| 90 g_context_factory = instance; | 80 g_context_factory = instance; |
| 91 } | 81 } |
| 92 | 82 |
| 93 DefaultContextFactory::DefaultContextFactory() { | |
| 94 } | |
| 95 | |
| 96 DefaultContextFactory::~DefaultContextFactory() { | |
| 97 } | |
| 98 | |
| 99 bool DefaultContextFactory::Initialize() { | |
| 100 if (!gfx::GLSurface::InitializeOneOff() || | |
| 101 gfx::GetGLImplementation() == gfx::kGLImplementationNone) { | |
| 102 LOG(ERROR) << "Could not load the GL bindings"; | |
| 103 return false; | |
| 104 } | |
| 105 return true; | |
| 106 } | |
| 107 | |
| 108 scoped_ptr<cc::OutputSurface> DefaultContextFactory::CreateOutputSurface( | |
| 109 Compositor* compositor) { | |
| 110 WebKit::WebGraphicsContext3D::Attributes attrs; | |
| 111 attrs.depth = false; | |
| 112 attrs.stencil = false; | |
| 113 attrs.antialias = false; | |
| 114 attrs.shareResources = true; | |
| 115 | |
| 116 using webkit::gpu::WebGraphicsContext3DInProcessCommandBufferImpl; | |
| 117 scoped_ptr<WebGraphicsContext3DInProcessCommandBufferImpl> context3d( | |
| 118 WebGraphicsContext3DInProcessCommandBufferImpl::CreateViewContext( | |
| 119 attrs, compositor->widget())); | |
| 120 CHECK(context3d); | |
| 121 | |
| 122 using webkit::gpu::ContextProviderInProcess; | |
| 123 scoped_refptr<ContextProviderInProcess> context_provider = | |
| 124 ContextProviderInProcess::Create(context3d.Pass(), | |
| 125 "UICompositor"); | |
| 126 | |
| 127 return make_scoped_ptr(new cc::OutputSurface(context_provider)); | |
| 128 } | |
| 129 | |
| 130 scoped_refptr<Reflector> DefaultContextFactory::CreateReflector( | |
| 131 Compositor* mirroed_compositor, | |
| 132 Layer* mirroring_layer) { | |
| 133 return NULL; | |
| 134 } | |
| 135 | |
| 136 void DefaultContextFactory::RemoveReflector( | |
| 137 scoped_refptr<Reflector> reflector) { | |
| 138 } | |
| 139 | |
| 140 scoped_refptr<cc::ContextProvider> | |
| 141 DefaultContextFactory::OffscreenCompositorContextProvider() { | |
| 142 if (!offscreen_compositor_contexts_.get() || | |
| 143 !offscreen_compositor_contexts_->DestroyedOnMainThread()) { | |
| 144 offscreen_compositor_contexts_ = | |
| 145 webkit::gpu::ContextProviderInProcess::CreateOffscreen(); | |
| 146 } | |
| 147 return offscreen_compositor_contexts_; | |
| 148 } | |
| 149 | |
| 150 scoped_refptr<cc::ContextProvider> | |
| 151 DefaultContextFactory::SharedMainThreadContextProvider() { | |
| 152 if (shared_main_thread_contexts_ && | |
| 153 !shared_main_thread_contexts_->DestroyedOnMainThread()) | |
| 154 return shared_main_thread_contexts_; | |
| 155 | |
| 156 if (ui::Compositor::WasInitializedWithThread()) { | |
| 157 shared_main_thread_contexts_ = | |
| 158 webkit::gpu::ContextProviderInProcess::CreateOffscreen(); | |
| 159 } else { | |
| 160 shared_main_thread_contexts_ = | |
| 161 static_cast<webkit::gpu::ContextProviderInProcess*>( | |
| 162 OffscreenCompositorContextProvider().get()); | |
| 163 } | |
| 164 if (shared_main_thread_contexts_ && | |
| 165 !shared_main_thread_contexts_->BindToCurrentThread()) | |
| 166 shared_main_thread_contexts_ = NULL; | |
| 167 | |
| 168 return shared_main_thread_contexts_; | |
| 169 } | |
| 170 | |
| 171 void DefaultContextFactory::RemoveCompositor(Compositor* compositor) { | |
| 172 } | |
| 173 | |
| 174 bool DefaultContextFactory::DoesCreateTestContexts() { return false; } | |
| 175 | |
| 176 TestContextFactory::TestContextFactory() {} | |
| 177 | |
| 178 TestContextFactory::~TestContextFactory() {} | |
| 179 | |
| 180 scoped_ptr<cc::OutputSurface> TestContextFactory::CreateOutputSurface( | |
| 181 Compositor* compositor) { | |
| 182 return make_scoped_ptr( | |
| 183 new cc::OutputSurface(cc::TestContextProvider::Create())); | |
| 184 } | |
| 185 | |
| 186 scoped_refptr<Reflector> TestContextFactory::CreateReflector( | |
| 187 Compositor* mirrored_compositor, | |
| 188 Layer* mirroring_layer) { | |
| 189 return new Reflector(); | |
| 190 } | |
| 191 | |
| 192 void TestContextFactory::RemoveReflector(scoped_refptr<Reflector> reflector) { | |
| 193 } | |
| 194 | |
| 195 scoped_refptr<cc::ContextProvider> | |
| 196 TestContextFactory::OffscreenCompositorContextProvider() { | |
| 197 if (!offscreen_compositor_contexts_.get() || | |
| 198 offscreen_compositor_contexts_->DestroyedOnMainThread()) | |
| 199 offscreen_compositor_contexts_ = cc::TestContextProvider::Create(); | |
| 200 return offscreen_compositor_contexts_; | |
| 201 } | |
| 202 | |
| 203 scoped_refptr<cc::ContextProvider> | |
| 204 TestContextFactory::SharedMainThreadContextProvider() { | |
| 205 if (shared_main_thread_contexts_ && | |
| 206 !shared_main_thread_contexts_->DestroyedOnMainThread()) | |
| 207 return shared_main_thread_contexts_; | |
| 208 | |
| 209 if (ui::Compositor::WasInitializedWithThread()) { | |
| 210 shared_main_thread_contexts_ = cc::TestContextProvider::Create(); | |
| 211 } else { | |
| 212 shared_main_thread_contexts_ = | |
| 213 static_cast<cc::TestContextProvider*>( | |
| 214 OffscreenCompositorContextProvider().get()); | |
| 215 } | |
| 216 if (shared_main_thread_contexts_ && | |
| 217 !shared_main_thread_contexts_->BindToCurrentThread()) | |
| 218 shared_main_thread_contexts_ = NULL; | |
| 219 | |
| 220 return shared_main_thread_contexts_; | |
| 221 } | |
| 222 | |
| 223 void TestContextFactory::RemoveCompositor(Compositor* compositor) { | |
| 224 } | |
| 225 | |
| 226 bool TestContextFactory::DoesCreateTestContexts() { return true; } | |
| 227 | |
| 228 Texture::Texture(bool flipped, const gfx::Size& size, float device_scale_factor) | 83 Texture::Texture(bool flipped, const gfx::Size& size, float device_scale_factor) |
| 229 : size_(size), | 84 : size_(size), |
| 230 flipped_(flipped), | 85 flipped_(flipped), |
| 231 device_scale_factor_(device_scale_factor) { | 86 device_scale_factor_(device_scale_factor) { |
| 232 } | 87 } |
| 233 | 88 |
| 234 Texture::~Texture() { | 89 Texture::~Texture() { |
| 235 } | 90 } |
| 236 | 91 |
| 237 std::string Texture::Produce() { | 92 std::string Texture::Produce() { |
| (...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 450 root_layer_->SetCompositor(NULL); | 305 root_layer_->SetCompositor(NULL); |
| 451 | 306 |
| 452 // Stop all outstanding draws before telling the ContextFactory to tear | 307 // Stop all outstanding draws before telling the ContextFactory to tear |
| 453 // down any contexts that the |host_| may rely upon. | 308 // down any contexts that the |host_| may rely upon. |
| 454 host_.reset(); | 309 host_.reset(); |
| 455 | 310 |
| 456 ContextFactory::GetInstance()->RemoveCompositor(this); | 311 ContextFactory::GetInstance()->RemoveCompositor(this); |
| 457 } | 312 } |
| 458 | 313 |
| 459 // static | 314 // static |
| 460 void Compositor::InitializeContextFactoryForTests(bool allow_test_contexts) { | |
| 461 // The factory may already have been initialized by the content layer, in | |
| 462 // which case, use that one. | |
| 463 if (g_context_factory) | |
| 464 return; | |
| 465 DCHECK(!g_implicit_factory) << | |
| 466 "ContextFactory for tests already initialized."; | |
| 467 | |
| 468 bool use_test_contexts = true; | |
| 469 | |
| 470 // Always use test contexts unless the disable command line flag is used. | |
| 471 CommandLine* command_line = CommandLine::ForCurrentProcess(); | |
| 472 if (command_line->HasSwitch(switches::kDisableTestCompositor)) | |
| 473 use_test_contexts = false; | |
| 474 | |
| 475 #if defined(OS_CHROMEOS) | |
| 476 // If the test is running on the chromeos envrionment (such as | |
| 477 // device or vm bots), always use real contexts. | |
| 478 if (base::SysInfo::IsRunningOnChromeOS()) | |
| 479 use_test_contexts = false; | |
| 480 #endif | |
| 481 | |
| 482 if (!allow_test_contexts) | |
| 483 use_test_contexts = false; | |
| 484 | |
| 485 if (use_test_contexts) { | |
| 486 g_implicit_factory = new ui::TestContextFactory; | |
| 487 } else { | |
| 488 DVLOG(1) << "Using DefaultContextFactory"; | |
| 489 scoped_ptr<ui::DefaultContextFactory> instance( | |
| 490 new ui::DefaultContextFactory()); | |
| 491 if (instance->Initialize()) | |
| 492 g_implicit_factory = instance.release(); | |
| 493 } | |
| 494 g_context_factory = g_implicit_factory; | |
| 495 } | |
| 496 | |
| 497 // static | |
| 498 void Compositor::Initialize() { | 315 void Compositor::Initialize() { |
| 499 #if defined(OS_CHROMEOS) | 316 #if defined(OS_CHROMEOS) |
| 500 bool use_thread = !CommandLine::ForCurrentProcess()->HasSwitch( | 317 bool use_thread = !CommandLine::ForCurrentProcess()->HasSwitch( |
| 501 switches::kUIDisableThreadedCompositing); | 318 switches::kUIDisableThreadedCompositing); |
| 502 #else | 319 #else |
| 503 bool use_thread = | 320 bool use_thread = |
| 504 CommandLine::ForCurrentProcess()->HasSwitch( | 321 CommandLine::ForCurrentProcess()->HasSwitch( |
| 505 switches::kUIEnableThreadedCompositing) && | 322 switches::kUIEnableThreadedCompositing) && |
| 506 !CommandLine::ForCurrentProcess()->HasSwitch( | 323 !CommandLine::ForCurrentProcess()->HasSwitch( |
| 507 switches::kUIDisableThreadedCompositing); | 324 switches::kUIDisableThreadedCompositing); |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 524 // static | 341 // static |
| 525 scoped_refptr<base::MessageLoopProxy> Compositor::GetCompositorMessageLoop() { | 342 scoped_refptr<base::MessageLoopProxy> Compositor::GetCompositorMessageLoop() { |
| 526 scoped_refptr<base::MessageLoopProxy> proxy; | 343 scoped_refptr<base::MessageLoopProxy> proxy; |
| 527 if (g_compositor_thread) | 344 if (g_compositor_thread) |
| 528 proxy = g_compositor_thread->message_loop_proxy(); | 345 proxy = g_compositor_thread->message_loop_proxy(); |
| 529 return proxy; | 346 return proxy; |
| 530 } | 347 } |
| 531 | 348 |
| 532 // static | 349 // static |
| 533 void Compositor::Terminate() { | 350 void Compositor::Terminate() { |
| 534 if (g_context_factory) { | 351 if (g_context_factory) |
|
piman
2013/10/26 00:38:36
nit: no need for the test any more.
(I wonder if w
| |
| 535 if (g_implicit_factory) { | |
| 536 delete g_implicit_factory; | |
| 537 g_implicit_factory = NULL; | |
| 538 } | |
| 539 g_context_factory = NULL; | 352 g_context_factory = NULL; |
| 540 } | |
| 541 | 353 |
| 542 if (g_compositor_thread) { | 354 if (g_compositor_thread) { |
| 543 DCHECK(!g_context_factory) | 355 DCHECK(!g_context_factory) |
| 544 << "The ContextFactory should not outlive the compositor thread."; | 356 << "The ContextFactory should not outlive the compositor thread."; |
|
piman
2013/10/26 00:38:36
This DCHECK actually will never hit. Mind removing
| |
| 545 g_compositor_thread->Stop(); | 357 g_compositor_thread->Stop(); |
| 546 delete g_compositor_thread; | 358 delete g_compositor_thread; |
| 547 g_compositor_thread = NULL; | 359 g_compositor_thread = NULL; |
| 548 } | 360 } |
| 549 | 361 |
| 550 DCHECK(g_compositor_initialized) << "Compositor::Initialize() didn't happen."; | 362 DCHECK(g_compositor_initialized) << "Compositor::Initialize() didn't happen."; |
| 551 g_compositor_initialized = false; | 363 g_compositor_initialized = false; |
| 552 } | 364 } |
| 553 | 365 |
| 554 void Compositor::ScheduleDraw() { | 366 void Compositor::ScheduleDraw() { |
| (...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 805 // CompositorObservers to be notified before starting another | 617 // CompositorObservers to be notified before starting another |
| 806 // draw cycle. | 618 // draw cycle. |
| 807 ScheduleDraw(); | 619 ScheduleDraw(); |
| 808 } | 620 } |
| 809 FOR_EACH_OBSERVER(CompositorObserver, | 621 FOR_EACH_OBSERVER(CompositorObserver, |
| 810 observer_list_, | 622 observer_list_, |
| 811 OnCompositingEnded(this)); | 623 OnCompositingEnded(this)); |
| 812 } | 624 } |
| 813 | 625 |
| 814 } // namespace ui | 626 } // namespace ui |
| OLD | NEW |