| 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/memory/singleton.h" |
| 12 #include "base/message_loop.h" | 13 #include "base/message_loop.h" |
| 13 #include "base/string_util.h" | 14 #include "base/string_util.h" |
| 14 #include "base/threading/thread.h" | 15 #include "base/threading/thread.h" |
| 15 #include "base/threading/thread_restrictions.h" | 16 #include "base/threading/thread_restrictions.h" |
| 17 #include "cc/context_provider.h" |
| 16 #include "cc/input_handler.h" | 18 #include "cc/input_handler.h" |
| 17 #include "cc/layer.h" | 19 #include "cc/layer.h" |
| 18 #include "cc/layer_tree_host.h" | 20 #include "cc/layer_tree_host.h" |
| 19 #include "cc/output_surface.h" | 21 #include "cc/output_surface.h" |
| 20 #include "cc/thread_impl.h" | 22 #include "cc/thread_impl.h" |
| 21 #include "third_party/skia/include/core/SkBitmap.h" | 23 #include "third_party/skia/include/core/SkBitmap.h" |
| 22 #include "ui/compositor/compositor_observer.h" | 24 #include "ui/compositor/compositor_observer.h" |
| 23 #include "ui/compositor/compositor_switches.h" | 25 #include "ui/compositor/compositor_switches.h" |
| 24 #include "ui/compositor/dip_util.h" | 26 #include "ui/compositor/dip_util.h" |
| 25 #include "ui/compositor/layer.h" | 27 #include "ui/compositor/layer.h" |
| 26 #include "ui/compositor/test_web_graphics_context_3d.h" | 28 #include "ui/compositor/test_web_graphics_context_3d.h" |
| 27 #include "ui/gl/gl_context.h" | 29 #include "ui/gl/gl_context.h" |
| 28 #include "ui/gl/gl_implementation.h" | 30 #include "ui/gl/gl_implementation.h" |
| 29 #include "ui/gl/gl_surface.h" | 31 #include "ui/gl/gl_surface.h" |
| 30 #include "ui/gl/gl_switches.h" | 32 #include "ui/gl/gl_switches.h" |
| 33 #include "webkit/gpu/grcontext_for_webgraphicscontext3d.h" |
| 31 #include "webkit/gpu/webgraphicscontext3d_in_process_impl.h" | 34 #include "webkit/gpu/webgraphicscontext3d_in_process_impl.h" |
| 32 | 35 |
| 33 #if defined(OS_CHROMEOS) | 36 #if defined(OS_CHROMEOS) |
| 34 #include "base/chromeos/chromeos_version.h" | 37 #include "base/chromeos/chromeos_version.h" |
| 35 #endif | 38 #endif |
| 36 | 39 |
| 37 namespace { | 40 namespace { |
| 38 | 41 |
| 39 const double kDefaultRefreshRate = 60.0; | 42 const double kDefaultRefreshRate = 60.0; |
| 40 const double kTestRefreshRate = 100.0; | 43 const double kTestRefreshRate = 100.0; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 63 private: | 66 private: |
| 64 friend class ui::PostedSwapQueue; | 67 friend class ui::PostedSwapQueue; |
| 65 | 68 |
| 66 SwapType type_; | 69 SwapType type_; |
| 67 bool posted_; | 70 bool posted_; |
| 68 ui::PostedSwapQueue* posted_swaps_; | 71 ui::PostedSwapQueue* posted_swaps_; |
| 69 | 72 |
| 70 DISALLOW_COPY_AND_ASSIGN(PendingSwap); | 73 DISALLOW_COPY_AND_ASSIGN(PendingSwap); |
| 71 }; | 74 }; |
| 72 | 75 |
| 76 class NullContextProvider : public cc::ContextProvider { |
| 77 public: |
| 78 virtual bool InitializeOnMainThread() OVERRIDE { return false; } |
| 79 virtual bool BindToCurrentThread() OVERRIDE { return false; } |
| 80 virtual WebKit::WebGraphicsContext3D* Context3d() OVERRIDE { return NULL; } |
| 81 virtual class GrContext* GrContext() OVERRIDE { return NULL; } |
| 82 virtual void VerifyContexts() OVERRIDE {} |
| 83 |
| 84 protected: |
| 85 virtual ~NullContextProvider() {} |
| 86 }; |
| 87 |
| 88 struct MainThreadNullContextProvider { |
| 89 scoped_refptr<NullContextProvider> provider; |
| 90 |
| 91 static MainThreadNullContextProvider* GetInstance() { |
| 92 return Singleton<MainThreadNullContextProvider>::get(); |
| 93 } |
| 94 }; |
| 95 |
| 96 struct CompositorThreadNullContextProvider { |
| 97 scoped_refptr<NullContextProvider> provider; |
| 98 |
| 99 static CompositorThreadNullContextProvider* GetInstance() { |
| 100 return Singleton<CompositorThreadNullContextProvider>::get(); |
| 101 } |
| 102 }; |
| 103 |
| 73 } // namespace | 104 } // namespace |
| 74 | 105 |
| 75 namespace ui { | 106 namespace ui { |
| 76 | 107 |
| 77 // static | 108 // static |
| 78 ContextFactory* ContextFactory::GetInstance() { | 109 ContextFactory* ContextFactory::GetInstance() { |
| 79 // We leak the shared resources so that we don't race with | 110 // We leak the shared resources so that we don't race with |
| 80 // the tear down of the gl_bindings. | 111 // the tear down of the gl_bindings. |
| 81 if (!g_context_factory) { | 112 if (!g_context_factory) { |
| 82 DVLOG(1) << "Using DefaultSharedResource"; | 113 DVLOG(1) << "Using DefaultSharedResource"; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 cc::OutputSurface* DefaultContextFactory::CreateOutputSurface( | 146 cc::OutputSurface* DefaultContextFactory::CreateOutputSurface( |
| 116 Compositor* compositor) { | 147 Compositor* compositor) { |
| 117 return new cc::OutputSurface( | 148 return new cc::OutputSurface( |
| 118 make_scoped_ptr(CreateContextCommon(compositor, false))); | 149 make_scoped_ptr(CreateContextCommon(compositor, false))); |
| 119 } | 150 } |
| 120 | 151 |
| 121 WebKit::WebGraphicsContext3D* DefaultContextFactory::CreateOffscreenContext() { | 152 WebKit::WebGraphicsContext3D* DefaultContextFactory::CreateOffscreenContext() { |
| 122 return CreateContextCommon(NULL, true); | 153 return CreateContextCommon(NULL, true); |
| 123 } | 154 } |
| 124 | 155 |
| 156 class DefaultContextFactory::DefaultContextProvider |
| 157 : public cc::ContextProvider { |
| 158 public: |
| 159 DefaultContextProvider(ContextFactory* factory) |
| 160 : factory_(factory), |
| 161 destroyed_(false) {} |
| 162 |
| 163 virtual bool InitializeOnMainThread() OVERRIDE { |
| 164 context3d_.reset(factory_->CreateOffscreenContext()); |
| 165 return !!context3d_; |
| 166 } |
| 167 |
| 168 virtual bool BindToCurrentThread() { |
| 169 return context3d_->makeContextCurrent(); |
| 170 } |
| 171 |
| 172 virtual WebKit::WebGraphicsContext3D* Context3d() { return context3d_.get(); } |
| 173 |
| 174 virtual class GrContext* GrContext() { |
| 175 if (!gr_context_) { |
| 176 gr_context_.reset( |
| 177 new webkit::gpu::GrContextForWebGraphicsContext3D(context3d_.get())); |
| 178 } |
| 179 return gr_context_->get(); |
| 180 } |
| 181 |
| 182 virtual void VerifyContexts() OVERRIDE { |
| 183 if (context3d_ && !context3d_->isContextLost()) |
| 184 return; |
| 185 base::AutoLock lock(destroyed_lock_); |
| 186 destroyed_ = true; |
| 187 } |
| 188 |
| 189 bool DestroyedOnMainThread() { |
| 190 base::AutoLock lock(destroyed_lock_); |
| 191 return destroyed_; |
| 192 } |
| 193 |
| 194 protected: |
| 195 virtual ~DefaultContextProvider() {} |
| 196 |
| 197 private: |
| 198 ContextFactory* factory_; |
| 199 base::Lock destroyed_lock_; |
| 200 bool destroyed_; |
| 201 scoped_ptr<WebKit::WebGraphicsContext3D> context3d_; |
| 202 scoped_ptr<webkit::gpu::GrContextForWebGraphicsContext3D> gr_context_; |
| 203 }; |
| 204 |
| 205 scoped_refptr<cc::ContextProvider> |
| 206 DefaultContextFactory::OffscreenContextProviderForMainThread() { |
| 207 if (!offscreen_contexts_main_thread_ || |
| 208 !offscreen_contexts_main_thread_->DestroyedOnMainThread()) { |
| 209 offscreen_contexts_main_thread_ = new DefaultContextProvider(this); |
| 210 } |
| 211 return offscreen_contexts_main_thread_; |
| 212 } |
| 213 |
| 214 scoped_refptr<cc::ContextProvider> |
| 215 DefaultContextFactory::OffscreenContextProviderForCompositorThread() { |
| 216 if (!offscreen_contexts_compositor_thread_ || |
| 217 !offscreen_contexts_compositor_thread_->DestroyedOnMainThread()) { |
| 218 offscreen_contexts_compositor_thread_ = new DefaultContextProvider(this); |
| 219 } |
| 220 return offscreen_contexts_compositor_thread_; |
| 221 } |
| 222 |
| 125 void DefaultContextFactory::RemoveCompositor(Compositor* compositor) { | 223 void DefaultContextFactory::RemoveCompositor(Compositor* compositor) { |
| 126 } | 224 } |
| 127 | 225 |
| 128 WebKit::WebGraphicsContext3D* DefaultContextFactory::CreateContextCommon( | 226 WebKit::WebGraphicsContext3D* DefaultContextFactory::CreateContextCommon( |
| 129 Compositor* compositor, | 227 Compositor* compositor, |
| 130 bool offscreen) { | 228 bool offscreen) { |
| 131 DCHECK(offscreen || compositor); | 229 DCHECK(offscreen || compositor); |
| 132 WebKit::WebGraphicsContext3D::Attributes attrs; | 230 WebKit::WebGraphicsContext3D::Attributes attrs; |
| 133 attrs.depth = false; | 231 attrs.depth = false; |
| 134 attrs.stencil = false; | 232 attrs.stencil = false; |
| (...skipping 376 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 511 void Compositor::didCompleteSwapBuffers() { | 609 void Compositor::didCompleteSwapBuffers() { |
| 512 DCHECK(g_compositor_thread); | 610 DCHECK(g_compositor_thread); |
| 513 NotifyEnd(); | 611 NotifyEnd(); |
| 514 } | 612 } |
| 515 | 613 |
| 516 void Compositor::scheduleComposite() { | 614 void Compositor::scheduleComposite() { |
| 517 if (!disable_schedule_composite_) | 615 if (!disable_schedule_composite_) |
| 518 ScheduleDraw(); | 616 ScheduleDraw(); |
| 519 } | 617 } |
| 520 | 618 |
| 619 scoped_refptr<cc::ContextProvider> |
| 620 Compositor::OffscreenContextProviderForMainThread() { |
| 621 if (g_test_compositor_enabled) { |
| 622 if (!MainThreadNullContextProvider::GetInstance()->provider) { |
| 623 MainThreadNullContextProvider::GetInstance()->provider = |
| 624 new NullContextProvider; |
| 625 } |
| 626 return MainThreadNullContextProvider::GetInstance()->provider; |
| 627 } |
| 628 return ContextFactory::GetInstance()->OffscreenContextProviderForMainThread(); |
| 629 } |
| 630 |
| 631 scoped_refptr<cc::ContextProvider> |
| 632 Compositor::OffscreenContextProviderForCompositorThread() { |
| 633 if (g_test_compositor_enabled) { |
| 634 if (!CompositorThreadNullContextProvider::GetInstance()->provider) { |
| 635 CompositorThreadNullContextProvider::GetInstance()->provider = |
| 636 new NullContextProvider; |
| 637 } |
| 638 return CompositorThreadNullContextProvider::GetInstance()->provider; |
| 639 } |
| 640 return ContextFactory::GetInstance()-> |
| 641 OffscreenContextProviderForCompositorThread(); |
| 642 } |
| 643 |
| 521 scoped_refptr<CompositorLock> Compositor::GetCompositorLock() { | 644 scoped_refptr<CompositorLock> Compositor::GetCompositorLock() { |
| 522 if (!compositor_lock_) { | 645 if (!compositor_lock_) { |
| 523 compositor_lock_ = new CompositorLock(this); | 646 compositor_lock_ = new CompositorLock(this); |
| 524 if (g_compositor_thread) | 647 if (g_compositor_thread) |
| 525 host_->setDeferCommits(true); | 648 host_->setDeferCommits(true); |
| 526 FOR_EACH_OBSERVER(CompositorObserver, | 649 FOR_EACH_OBSERVER(CompositorObserver, |
| 527 observer_list_, | 650 observer_list_, |
| 528 OnCompositingLockStateChanged(this)); | 651 OnCompositingLockStateChanged(this)); |
| 529 } | 652 } |
| 530 return compositor_lock_; | 653 return compositor_lock_; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 567 | 690 |
| 568 COMPOSITOR_EXPORT void DisableTestCompositor() { | 691 COMPOSITOR_EXPORT void DisableTestCompositor() { |
| 569 g_test_compositor_enabled = false; | 692 g_test_compositor_enabled = false; |
| 570 } | 693 } |
| 571 | 694 |
| 572 COMPOSITOR_EXPORT bool IsTestCompositorEnabled() { | 695 COMPOSITOR_EXPORT bool IsTestCompositorEnabled() { |
| 573 return g_test_compositor_enabled; | 696 return g_test_compositor_enabled; |
| 574 } | 697 } |
| 575 | 698 |
| 576 } // namespace ui | 699 } // namespace ui |
| OLD | NEW |