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