[email protected] | 192093059 | 2012-01-11 14:54:48 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | c797cd4 | 2011-03-15 02:18:36 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 116302fc | 2012-05-05 21:45:41 | [diff] [blame] | 5 | #include "ui/compositor/compositor.h" |
[email protected] | ed8de92d | 2011-09-14 04:16:48 | [diff] [blame] | 6 | |
[email protected] | cff176a | 2012-06-29 21:11:00 | [diff] [blame] | 7 | #include <algorithm> |
[email protected] | 337bd04 | 2012-11-05 23:43:43 | [diff] [blame] | 8 | #include <deque> |
[email protected] | cff176a | 2012-06-29 21:11:00 | [diff] [blame] | 9 | |
[email protected] | 6b16679e | 2012-10-27 00:44:28 | [diff] [blame] | 10 | #include "base/bind.h" |
[email protected] | ab662737 | 2012-01-29 21:22:13 | [diff] [blame] | 11 | #include "base/command_line.h" |
[email protected] | 89af400 | 2013-09-06 07:47:07 | [diff] [blame] | 12 | #include "base/debug/trace_event.h" |
[email protected] | 0a45172 | 2013-02-22 20:32:05 | [diff] [blame] | 13 | #include "base/memory/singleton.h" |
[email protected] | 4e2d03e2 | 2013-07-18 04:19:54 | [diff] [blame] | 14 | #include "base/message_loop/message_loop.h" |
[email protected] | 1e7565a | 2013-04-08 20:11:14 | [diff] [blame] | 15 | #include "base/run_loop.h" |
[email protected] | f3652ff9 | 2013-06-11 13:54:31 | [diff] [blame] | 16 | #include "base/strings/string_util.h" |
[email protected] | 49c4cf85 | 2013-09-27 19:28:24 | [diff] [blame^] | 17 | #include "base/sys_info.h" |
[email protected] | ec05af5 | 2012-11-21 23:07:00 | [diff] [blame] | 18 | #include "base/threading/thread.h" |
[email protected] | 894e8fc | 2012-02-24 13:29:50 | [diff] [blame] | 19 | #include "base/threading/thread_restrictions.h" |
[email protected] | 4d5e676 | 2013-03-19 18:46:57 | [diff] [blame] | 20 | #include "cc/base/switches.h" |
[email protected] | ff7c2e4b | 2013-08-13 23:47:35 | [diff] [blame] | 21 | #include "cc/debug/test_context_provider.h" |
[email protected] | 95fc4214 | 2013-08-13 19:31:05 | [diff] [blame] | 22 | #include "cc/debug/test_web_graphics_context_3d.h" |
[email protected] | 3052b10f | 2013-03-18 07:41:21 | [diff] [blame] | 23 | #include "cc/input/input_handler.h" |
[email protected] | cc3cfaa | 2013-03-18 09:05:52 | [diff] [blame] | 24 | #include "cc/layers/layer.h" |
[email protected] | 7f0d825f | 2013-03-18 07:24:30 | [diff] [blame] | 25 | #include "cc/output/context_provider.h" |
| 26 | #include "cc/output/output_surface.h" |
[email protected] | 556fd29 | 2013-03-18 08:03:04 | [diff] [blame] | 27 | #include "cc/trees/layer_tree_host.h" |
[email protected] | 83afcbcc | 2012-07-27 03:06:27 | [diff] [blame] | 28 | #include "third_party/skia/include/core/SkBitmap.h" |
[email protected] | 116302fc | 2012-05-05 21:45:41 | [diff] [blame] | 29 | #include "ui/compositor/compositor_observer.h" |
| 30 | #include "ui/compositor/compositor_switches.h" |
[email protected] | cd9a61c7 | 2012-05-08 19:16:59 | [diff] [blame] | 31 | #include "ui/compositor/dip_util.h" |
[email protected] | 116302fc | 2012-05-05 21:45:41 | [diff] [blame] | 32 | #include "ui/compositor/layer.h" |
[email protected] | bc90af6b | 2013-07-29 20:32:39 | [diff] [blame] | 33 | #include "ui/compositor/reflector.h" |
[email protected] | c9e2cbbb | 2012-05-12 21:17:27 | [diff] [blame] | 34 | #include "ui/gl/gl_context.h" |
| 35 | #include "ui/gl/gl_implementation.h" |
| 36 | #include "ui/gl/gl_surface.h" |
[email protected] | cc2ae01 | 2012-09-21 19:35:25 | [diff] [blame] | 37 | #include "ui/gl/gl_switches.h" |
[email protected] | 199b715e | 2013-08-13 05:18:34 | [diff] [blame] | 38 | #include "webkit/common/gpu/context_provider_in_process.h" |
[email protected] | 1fb9e7e | 2013-05-26 03:23:48 | [diff] [blame] | 39 | #include "webkit/common/gpu/grcontext_for_webgraphicscontext3d.h" |
| 40 | #include "webkit/common/gpu/webgraphicscontext3d_in_process_command_buffer_impl.h" |
[email protected] | ab662737 | 2012-01-29 21:22:13 | [diff] [blame] | 41 | |
| 42 | namespace { |
| 43 | |
| 44 | const double kDefaultRefreshRate = 60.0; |
[email protected] | 7ddeaab | 2013-04-06 00:47:05 | [diff] [blame] | 45 | const double kTestRefreshRate = 200.0; |
[email protected] | ab662737 | 2012-01-29 21:22:13 | [diff] [blame] | 46 | |
[email protected] | 337bd04 | 2012-11-05 23:43:43 | [diff] [blame] | 47 | enum SwapType { |
| 48 | DRAW_SWAP, |
| 49 | READPIXELS_SWAP, |
| 50 | }; |
| 51 | |
[email protected] | d56d3bb | 2013-08-12 20:58:01 | [diff] [blame] | 52 | bool g_compositor_initialized = false; |
[email protected] | ec05af5 | 2012-11-21 23:07:00 | [diff] [blame] | 53 | base::Thread* g_compositor_thread = NULL; |
[email protected] | ab662737 | 2012-01-29 21:22:13 | [diff] [blame] | 54 | |
[email protected] | 40034cf | 2013-03-13 05:05:37 | [diff] [blame] | 55 | ui::ContextFactory* g_implicit_factory = NULL; |
[email protected] | 894e8fc | 2012-02-24 13:29:50 | [diff] [blame] | 56 | ui::ContextFactory* g_context_factory = NULL; |
| 57 | |
[email protected] | 6b16679e | 2012-10-27 00:44:28 | [diff] [blame] | 58 | const int kCompositorLockTimeoutMs = 67; |
| 59 | |
[email protected] | 337bd04 | 2012-11-05 23:43:43 | [diff] [blame] | 60 | class PendingSwap { |
| 61 | public: |
| 62 | PendingSwap(SwapType type, ui::PostedSwapQueue* posted_swaps); |
| 63 | ~PendingSwap(); |
| 64 | |
| 65 | SwapType type() const { return type_; } |
| 66 | bool posted() const { return posted_; } |
| 67 | |
| 68 | private: |
| 69 | friend class ui::PostedSwapQueue; |
| 70 | |
| 71 | SwapType type_; |
| 72 | bool posted_; |
| 73 | ui::PostedSwapQueue* posted_swaps_; |
| 74 | |
| 75 | DISALLOW_COPY_AND_ASSIGN(PendingSwap); |
| 76 | }; |
| 77 | |
[email protected] | 83afcbcc | 2012-07-27 03:06:27 | [diff] [blame] | 78 | } // namespace |
[email protected] | c797cd4 | 2011-03-15 02:18:36 | [diff] [blame] | 79 | |
| 80 | namespace ui { |
| 81 | |
[email protected] | 894e8fc | 2012-02-24 13:29:50 | [diff] [blame] | 82 | // static |
| 83 | ContextFactory* ContextFactory::GetInstance() { |
[email protected] | d56d3bb | 2013-08-12 20:58:01 | [diff] [blame] | 84 | DCHECK(g_context_factory); |
[email protected] | 894e8fc | 2012-02-24 13:29:50 | [diff] [blame] | 85 | return g_context_factory; |
[email protected] | ca80663 | 2012-02-16 02:15:59 | [diff] [blame] | 86 | } |
| 87 | |
[email protected] | 58b4b6d | 2012-02-16 01:40:24 | [diff] [blame] | 88 | // static |
[email protected] | 894e8fc | 2012-02-24 13:29:50 | [diff] [blame] | 89 | void ContextFactory::SetInstance(ContextFactory* instance) { |
| 90 | g_context_factory = instance; |
[email protected] | ca80663 | 2012-02-16 02:15:59 | [diff] [blame] | 91 | } |
| 92 | |
[email protected] | 40034cf | 2013-03-13 05:05:37 | [diff] [blame] | 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 | |
[email protected] | bc90af6b | 2013-07-29 20:32:39 | [diff] [blame] | 108 | scoped_ptr<cc::OutputSurface> DefaultContextFactory::CreateOutputSurface( |
[email protected] | 40034cf | 2013-03-13 05:05:37 | [diff] [blame] | 109 | Compositor* compositor) { |
[email protected] | 3a98aed9 | 2013-08-16 04:58:13 | [diff] [blame] | 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); |
[email protected] | 40034cf | 2013-03-13 05:05:37 | [diff] [blame] | 121 | |
[email protected] | 0634cdd4 | 2013-08-16 00:46:09 | [diff] [blame] | 122 | using webkit::gpu::ContextProviderInProcess; |
| 123 | scoped_refptr<ContextProviderInProcess> context_provider = |
[email protected] | af13d83 | 2013-09-13 06:56:27 | [diff] [blame] | 124 | ContextProviderInProcess::Create(context3d.Pass(), |
| 125 | "UICompositor"); |
[email protected] | 3a98aed9 | 2013-08-16 04:58:13 | [diff] [blame] | 126 | |
[email protected] | 0634cdd4 | 2013-08-16 00:46:09 | [diff] [blame] | 127 | return make_scoped_ptr(new cc::OutputSurface(context_provider)); |
[email protected] | 40034cf | 2013-03-13 05:05:37 | [diff] [blame] | 128 | } |
| 129 | |
[email protected] | 57bed4d | 2013-06-08 20:54:18 | [diff] [blame] | 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 | |
[email protected] | 0a45172 | 2013-02-22 20:32:05 | [diff] [blame] | 140 | scoped_refptr<cc::ContextProvider> |
[email protected] | f10dc47 | 2013-09-27 03:31:59 | [diff] [blame] | 141 | DefaultContextFactory::OffscreenCompositorContextProvider() { |
| 142 | if (!offscreen_compositor_contexts_.get() || |
| 143 | !offscreen_compositor_contexts_->DestroyedOnMainThread()) { |
| 144 | offscreen_compositor_contexts_ = |
[email protected] | 3d6b512 | 2013-08-14 22:25:34 | [diff] [blame] | 145 | webkit::gpu::ContextProviderInProcess::CreateOffscreen(); |
[email protected] | 0a45172 | 2013-02-22 20:32:05 | [diff] [blame] | 146 | } |
[email protected] | f10dc47 | 2013-09-27 03:31:59 | [diff] [blame] | 147 | return offscreen_compositor_contexts_; |
[email protected] | 0a45172 | 2013-02-22 20:32:05 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | scoped_refptr<cc::ContextProvider> |
[email protected] | f10dc47 | 2013-09-27 03:31:59 | [diff] [blame] | 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_ = |
[email protected] | 3d6b512 | 2013-08-14 22:25:34 | [diff] [blame] | 158 | webkit::gpu::ContextProviderInProcess::CreateOffscreen(); |
[email protected] | f10dc47 | 2013-09-27 03:31:59 | [diff] [blame] | 159 | } else { |
| 160 | shared_main_thread_contexts_ = |
| 161 | static_cast<webkit::gpu::ContextProviderInProcess*>( |
| 162 | OffscreenCompositorContextProvider().get()); |
[email protected] | 0a45172 | 2013-02-22 20:32:05 | [diff] [blame] | 163 | } |
[email protected] | f10dc47 | 2013-09-27 03:31:59 | [diff] [blame] | 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_; |
[email protected] | 0a45172 | 2013-02-22 20:32:05 | [diff] [blame] | 169 | } |
| 170 | |
[email protected] | 84dba8ca | 2012-04-10 03:27:39 | [diff] [blame] | 171 | void DefaultContextFactory::RemoveCompositor(Compositor* compositor) { |
| 172 | } |
| 173 | |
[email protected] | d56d3bb | 2013-08-12 20:58:01 | [diff] [blame] | 174 | bool DefaultContextFactory::DoesCreateTestContexts() { return false; } |
| 175 | |
[email protected] | e06e112 | 2013-03-15 17:12:38 | [diff] [blame] | 176 | TestContextFactory::TestContextFactory() {} |
[email protected] | 40034cf | 2013-03-13 05:05:37 | [diff] [blame] | 177 | |
[email protected] | e06e112 | 2013-03-15 17:12:38 | [diff] [blame] | 178 | TestContextFactory::~TestContextFactory() {} |
[email protected] | 40034cf | 2013-03-13 05:05:37 | [diff] [blame] | 179 | |
[email protected] | bc90af6b | 2013-07-29 20:32:39 | [diff] [blame] | 180 | scoped_ptr<cc::OutputSurface> TestContextFactory::CreateOutputSurface( |
[email protected] | 40034cf | 2013-03-13 05:05:37 | [diff] [blame] | 181 | Compositor* compositor) { |
[email protected] | 0634cdd4 | 2013-08-16 00:46:09 | [diff] [blame] | 182 | return make_scoped_ptr( |
| 183 | new cc::OutputSurface(cc::TestContextProvider::Create())); |
[email protected] | 40034cf | 2013-03-13 05:05:37 | [diff] [blame] | 184 | } |
| 185 | |
[email protected] | 57bed4d | 2013-06-08 20:54:18 | [diff] [blame] | 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 | |
[email protected] | 40034cf | 2013-03-13 05:05:37 | [diff] [blame] | 195 | scoped_refptr<cc::ContextProvider> |
[email protected] | f10dc47 | 2013-09-27 03:31:59 | [diff] [blame] | 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_; |
[email protected] | 40034cf | 2013-03-13 05:05:37 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | scoped_refptr<cc::ContextProvider> |
[email protected] | f10dc47 | 2013-09-27 03:31:59 | [diff] [blame] | 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()); |
[email protected] | e06e112 | 2013-03-15 17:12:38 | [diff] [blame] | 215 | } |
[email protected] | f10dc47 | 2013-09-27 03:31:59 | [diff] [blame] | 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_; |
[email protected] | 40034cf | 2013-03-13 05:05:37 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | void TestContextFactory::RemoveCompositor(Compositor* compositor) { |
| 224 | } |
| 225 | |
[email protected] | d56d3bb | 2013-08-12 20:58:01 | [diff] [blame] | 226 | bool TestContextFactory::DoesCreateTestContexts() { return true; } |
| 227 | |
[email protected] | d3407cdb | 2012-09-21 22:33:25 | [diff] [blame] | 228 | Texture::Texture(bool flipped, const gfx::Size& size, float device_scale_factor) |
[email protected] | 34466d0 | 2012-12-10 18:00:34 | [diff] [blame] | 229 | : size_(size), |
| 230 | flipped_(flipped), |
[email protected] | d3407cdb | 2012-09-21 22:33:25 | [diff] [blame] | 231 | device_scale_factor_(device_scale_factor) { |
[email protected] | ab662737 | 2012-01-29 21:22:13 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | Texture::~Texture() { |
| 235 | } |
| 236 | |
[email protected] | 06666c0 | 2013-03-07 19:32:30 | [diff] [blame] | 237 | std::string Texture::Produce() { |
| 238 | return EmptyString(); |
[email protected] | f027ca150 | 2012-12-21 01:42:32 | [diff] [blame] | 239 | } |
| 240 | |
[email protected] | 6b16679e | 2012-10-27 00:44:28 | [diff] [blame] | 241 | CompositorLock::CompositorLock(Compositor* compositor) |
| 242 | : compositor_(compositor) { |
[email protected] | 7060d659 | 2013-04-29 19:01:48 | [diff] [blame] | 243 | base::MessageLoop::current()->PostDelayedTask( |
[email protected] | 6b16679e | 2012-10-27 00:44:28 | [diff] [blame] | 244 | FROM_HERE, |
| 245 | base::Bind(&CompositorLock::CancelLock, AsWeakPtr()), |
| 246 | base::TimeDelta::FromMilliseconds(kCompositorLockTimeoutMs)); |
| 247 | } |
| 248 | |
| 249 | CompositorLock::~CompositorLock() { |
| 250 | CancelLock(); |
| 251 | } |
| 252 | |
| 253 | void CompositorLock::CancelLock() { |
| 254 | if (!compositor_) |
| 255 | return; |
| 256 | compositor_->UnlockCompositor(); |
| 257 | compositor_ = NULL; |
| 258 | } |
| 259 | |
[email protected] | 1e7565a | 2013-04-08 20:11:14 | [diff] [blame] | 260 | // static |
[email protected] | 260c321 | 2013-04-11 22:25:38 | [diff] [blame] | 261 | void DrawWaiterForTest::Wait(Compositor* compositor) { |
[email protected] | 1e7565a | 2013-04-08 20:11:14 | [diff] [blame] | 262 | DrawWaiterForTest waiter; |
[email protected] | 36e5ff1 | 2013-06-11 12:19:29 | [diff] [blame] | 263 | waiter.wait_for_commit_ = false; |
| 264 | waiter.WaitImpl(compositor); |
| 265 | } |
| 266 | |
| 267 | // static |
| 268 | void DrawWaiterForTest::WaitForCommit(Compositor* compositor) { |
| 269 | DrawWaiterForTest waiter; |
| 270 | waiter.wait_for_commit_ = true; |
[email protected] | 260c321 | 2013-04-11 22:25:38 | [diff] [blame] | 271 | waiter.WaitImpl(compositor); |
[email protected] | 1e7565a | 2013-04-08 20:11:14 | [diff] [blame] | 272 | } |
| 273 | |
[email protected] | 260c321 | 2013-04-11 22:25:38 | [diff] [blame] | 274 | DrawWaiterForTest::DrawWaiterForTest() { |
[email protected] | 1e7565a | 2013-04-08 20:11:14 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | DrawWaiterForTest::~DrawWaiterForTest() { |
| 278 | } |
| 279 | |
[email protected] | 260c321 | 2013-04-11 22:25:38 | [diff] [blame] | 280 | void DrawWaiterForTest::WaitImpl(Compositor* compositor) { |
[email protected] | 1e7565a | 2013-04-08 20:11:14 | [diff] [blame] | 281 | compositor->AddObserver(this); |
| 282 | wait_run_loop_.reset(new base::RunLoop()); |
[email protected] | 1e7565a | 2013-04-08 20:11:14 | [diff] [blame] | 283 | wait_run_loop_->Run(); |
| 284 | compositor->RemoveObserver(this); |
[email protected] | 1e7565a | 2013-04-08 20:11:14 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | void DrawWaiterForTest::OnCompositingDidCommit(Compositor* compositor) { |
[email protected] | 36e5ff1 | 2013-06-11 12:19:29 | [diff] [blame] | 288 | if (wait_for_commit_) |
| 289 | wait_run_loop_->Quit(); |
[email protected] | 1e7565a | 2013-04-08 20:11:14 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | void DrawWaiterForTest::OnCompositingStarted(Compositor* compositor, |
| 293 | base::TimeTicks start_time) { |
| 294 | } |
| 295 | |
| 296 | void DrawWaiterForTest::OnCompositingEnded(Compositor* compositor) { |
[email protected] | 36e5ff1 | 2013-06-11 12:19:29 | [diff] [blame] | 297 | if (!wait_for_commit_) |
| 298 | wait_run_loop_->Quit(); |
[email protected] | 1e7565a | 2013-04-08 20:11:14 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | void DrawWaiterForTest::OnCompositingAborted(Compositor* compositor) { |
| 302 | } |
| 303 | |
| 304 | void DrawWaiterForTest::OnCompositingLockStateChanged(Compositor* compositor) { |
| 305 | } |
| 306 | |
| 307 | void DrawWaiterForTest::OnUpdateVSyncParameters(Compositor* compositor, |
| 308 | base::TimeTicks timebase, |
| 309 | base::TimeDelta interval) { |
| 310 | } |
| 311 | |
[email protected] | 337bd04 | 2012-11-05 23:43:43 | [diff] [blame] | 312 | class PostedSwapQueue { |
| 313 | public: |
| 314 | PostedSwapQueue() : pending_swap_(NULL) { |
| 315 | } |
| 316 | |
| 317 | ~PostedSwapQueue() { |
| 318 | DCHECK(!pending_swap_); |
| 319 | } |
| 320 | |
| 321 | SwapType NextPostedSwap() const { |
| 322 | return queue_.front(); |
| 323 | } |
| 324 | |
| 325 | bool AreSwapsPosted() const { |
| 326 | return !queue_.empty(); |
| 327 | } |
| 328 | |
| 329 | int NumSwapsPosted(SwapType type) const { |
| 330 | int count = 0; |
| 331 | for (std::deque<SwapType>::const_iterator it = queue_.begin(); |
| 332 | it != queue_.end(); ++it) { |
| 333 | if (*it == type) |
| 334 | count++; |
| 335 | } |
| 336 | return count; |
| 337 | } |
| 338 | |
| 339 | void PostSwap() { |
| 340 | DCHECK(pending_swap_); |
| 341 | queue_.push_back(pending_swap_->type()); |
| 342 | pending_swap_->posted_ = true; |
| 343 | } |
| 344 | |
| 345 | void EndSwap() { |
| 346 | queue_.pop_front(); |
| 347 | } |
| 348 | |
| 349 | private: |
| 350 | friend class ::PendingSwap; |
| 351 | |
| 352 | PendingSwap* pending_swap_; |
| 353 | std::deque<SwapType> queue_; |
| 354 | |
| 355 | DISALLOW_COPY_AND_ASSIGN(PostedSwapQueue); |
| 356 | }; |
| 357 | |
| 358 | } // namespace ui |
| 359 | |
| 360 | namespace { |
| 361 | |
| 362 | PendingSwap::PendingSwap(SwapType type, ui::PostedSwapQueue* posted_swaps) |
| 363 | : type_(type), posted_(false), posted_swaps_(posted_swaps) { |
| 364 | // Only one pending swap in flight. |
| 365 | DCHECK_EQ(static_cast<PendingSwap*>(NULL), posted_swaps_->pending_swap_); |
| 366 | posted_swaps_->pending_swap_ = this; |
| 367 | } |
| 368 | |
| 369 | PendingSwap::~PendingSwap() { |
| 370 | DCHECK_EQ(this, posted_swaps_->pending_swap_); |
| 371 | posted_swaps_->pending_swap_ = NULL; |
| 372 | } |
| 373 | |
| 374 | } // namespace |
| 375 | |
| 376 | namespace ui { |
| 377 | |
[email protected] | ab662737 | 2012-01-29 21:22:13 | [diff] [blame] | 378 | Compositor::Compositor(CompositorDelegate* delegate, |
[email protected] | cd9a61c7 | 2012-05-08 19:16:59 | [diff] [blame] | 379 | gfx::AcceleratedWidget widget) |
[email protected] | 82a01ac | 2011-09-08 16:00:18 | [diff] [blame] | 380 | : delegate_(delegate), |
[email protected] | ab662737 | 2012-01-29 21:22:13 | [diff] [blame] | 381 | root_layer_(NULL), |
| 382 | widget_(widget), |
[email protected] | 337bd04 | 2012-11-05 23:43:43 | [diff] [blame] | 383 | posted_swaps_(new PostedSwapQueue()), |
[email protected] | d4ae8057 | 2012-06-06 23:02:06 | [diff] [blame] | 384 | device_scale_factor_(0.0f), |
| 385 | last_started_frame_(0), |
| 386 | last_ended_frame_(0), |
[email protected] | 8d9080ab | 2013-04-06 00:03:20 | [diff] [blame] | 387 | next_draw_is_resize_(false), |
[email protected] | 6b16679e | 2012-10-27 00:44:28 | [diff] [blame] | 388 | disable_schedule_composite_(false), |
| 389 | compositor_lock_(NULL) { |
[email protected] | d56d3bb | 2013-08-12 20:58:01 | [diff] [blame] | 390 | DCHECK(g_compositor_initialized) |
| 391 | << "Compositor::Initialize must be called before creating a Compositor."; |
| 392 | |
[email protected] | 7aba666 | 2013-03-12 10:17:34 | [diff] [blame] | 393 | root_web_layer_ = cc::Layer::Create(); |
| 394 | root_web_layer_->SetAnchorPoint(gfx::PointF(0.f, 0.f)); |
[email protected] | ec05af5 | 2012-11-21 23:07:00 | [diff] [blame] | 395 | |
[email protected] | ab662737 | 2012-01-29 21:22:13 | [diff] [blame] | 396 | CommandLine* command_line = CommandLine::ForCurrentProcess(); |
[email protected] | 4d5e676 | 2013-03-19 18:46:57 | [diff] [blame] | 397 | |
[email protected] | ec05af5 | 2012-11-21 23:07:00 | [diff] [blame] | 398 | cc::LayerTreeSettings settings; |
[email protected] | 8e0176d | 2013-03-21 03:14:52 | [diff] [blame] | 399 | settings.refresh_rate = |
[email protected] | d56d3bb | 2013-08-12 20:58:01 | [diff] [blame] | 400 | ContextFactory::GetInstance()->DoesCreateTestContexts() |
| 401 | ? kTestRefreshRate |
| 402 | : kDefaultRefreshRate; |
[email protected] | 910b310 | 2013-09-21 16:12:23 | [diff] [blame] | 403 | settings.deadline_scheduling_enabled = |
| 404 | switches::IsUIDeadlineSchedulingEnabled(); |
[email protected] | 50e15727 | 2013-04-13 05:07:19 | [diff] [blame] | 405 | settings.partial_swap_enabled = |
| 406 | !command_line->HasSwitch(cc::switches::kUIDisablePartialSwap); |
[email protected] | 8e0176d | 2013-03-21 03:14:52 | [diff] [blame] | 407 | settings.per_tile_painting_enabled = |
[email protected] | 4d5e676 | 2013-03-19 18:46:57 | [diff] [blame] | 408 | command_line->HasSwitch(cc::switches::kUIEnablePerTilePainting); |
| 409 | |
| 410 | // These flags should be mirrored by renderer versions in content/renderer/. |
[email protected] | 8e0176d | 2013-03-21 03:14:52 | [diff] [blame] | 411 | settings.initial_debug_state.show_debug_borders = |
[email protected] | 4d5e676 | 2013-03-19 18:46:57 | [diff] [blame] | 412 | command_line->HasSwitch(cc::switches::kUIShowCompositedLayerBorders); |
[email protected] | 8e0176d | 2013-03-21 03:14:52 | [diff] [blame] | 413 | settings.initial_debug_state.show_fps_counter = |
[email protected] | 4d5e676 | 2013-03-19 18:46:57 | [diff] [blame] | 414 | command_line->HasSwitch(cc::switches::kUIShowFPSCounter); |
[email protected] | 8e0176d | 2013-03-21 03:14:52 | [diff] [blame] | 415 | settings.initial_debug_state.show_paint_rects = |
[email protected] | 4d5e676 | 2013-03-19 18:46:57 | [diff] [blame] | 416 | command_line->HasSwitch(switches::kUIShowPaintRects); |
[email protected] | 8e0176d | 2013-03-21 03:14:52 | [diff] [blame] | 417 | settings.initial_debug_state.show_property_changed_rects = |
[email protected] | 4d5e676 | 2013-03-19 18:46:57 | [diff] [blame] | 418 | command_line->HasSwitch(cc::switches::kUIShowPropertyChangedRects); |
[email protected] | 8e0176d | 2013-03-21 03:14:52 | [diff] [blame] | 419 | settings.initial_debug_state.show_surface_damage_rects = |
[email protected] | 4d5e676 | 2013-03-19 18:46:57 | [diff] [blame] | 420 | command_line->HasSwitch(cc::switches::kUIShowSurfaceDamageRects); |
[email protected] | 8e0176d | 2013-03-21 03:14:52 | [diff] [blame] | 421 | settings.initial_debug_state.show_screen_space_rects = |
[email protected] | 4d5e676 | 2013-03-19 18:46:57 | [diff] [blame] | 422 | command_line->HasSwitch(cc::switches::kUIShowScreenSpaceRects); |
[email protected] | 8e0176d | 2013-03-21 03:14:52 | [diff] [blame] | 423 | settings.initial_debug_state.show_replica_screen_space_rects = |
[email protected] | 4d5e676 | 2013-03-19 18:46:57 | [diff] [blame] | 424 | command_line->HasSwitch(cc::switches::kUIShowReplicaScreenSpaceRects); |
[email protected] | 8e0176d | 2013-03-21 03:14:52 | [diff] [blame] | 425 | settings.initial_debug_state.show_occluding_rects = |
[email protected] | 4d5e676 | 2013-03-19 18:46:57 | [diff] [blame] | 426 | command_line->HasSwitch(cc::switches::kUIShowOccludingRects); |
[email protected] | 8e0176d | 2013-03-21 03:14:52 | [diff] [blame] | 427 | settings.initial_debug_state.show_non_occluding_rects = |
[email protected] | 4d5e676 | 2013-03-19 18:46:57 | [diff] [blame] | 428 | command_line->HasSwitch(cc::switches::kUIShowNonOccludingRects); |
[email protected] | 302fe42 | 2012-06-11 14:49:11 | [diff] [blame] | 429 | |
[email protected] | 810d40b7 | 2013-06-20 18:26:15 | [diff] [blame] | 430 | scoped_refptr<base::SingleThreadTaskRunner> compositor_task_runner = |
| 431 | g_compositor_thread ? g_compositor_thread->message_loop_proxy() : NULL; |
[email protected] | ec05af5 | 2012-11-21 23:07:00 | [diff] [blame] | 432 | |
[email protected] | 810d40b7 | 2013-06-20 18:26:15 | [diff] [blame] | 433 | host_ = cc::LayerTreeHost::Create(this, settings, compositor_task_runner); |
[email protected] | 804c898 | 2013-03-13 16:32:21 | [diff] [blame] | 434 | host_->SetRootLayer(root_web_layer_); |
[email protected] | 14bd554 | 2013-05-08 21:51:30 | [diff] [blame] | 435 | host_->SetLayerTreeHostClientReady(); |
[email protected] | 82a01ac | 2011-09-08 16:00:18 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | Compositor::~Compositor() { |
[email protected] | 89af400 | 2013-09-06 07:47:07 | [diff] [blame] | 439 | TRACE_EVENT0("shutdown", "Compositor::destructor"); |
| 440 | |
[email protected] | d56d3bb | 2013-08-12 20:58:01 | [diff] [blame] | 441 | DCHECK(g_compositor_initialized); |
| 442 | |
[email protected] | 6b16679e | 2012-10-27 00:44:28 | [diff] [blame] | 443 | CancelCompositorLock(); |
| 444 | DCHECK(!compositor_lock_); |
| 445 | |
[email protected] | e332a1b | 2012-05-10 20:32:27 | [diff] [blame] | 446 | // Don't call |CompositorDelegate::ScheduleDraw| from this point. |
| 447 | delegate_ = NULL; |
[email protected] | 7ab3f27 | 2011-11-16 00:51:56 | [diff] [blame] | 448 | if (root_layer_) |
| 449 | root_layer_->SetCompositor(NULL); |
[email protected] | 2700daddd | 2012-07-13 19:35:37 | [diff] [blame] | 450 | |
| 451 | // Stop all outstanding draws before telling the ContextFactory to tear |
| 452 | // down any contexts that the |host_| may rely upon. |
| 453 | host_.reset(); |
| 454 | |
[email protected] | 40034cf | 2013-03-13 05:05:37 | [diff] [blame] | 455 | ContextFactory::GetInstance()->RemoveCompositor(this); |
[email protected] | 82a01ac | 2011-09-08 16:00:18 | [diff] [blame] | 456 | } |
| 457 | |
[email protected] | 44ed4bd | 2013-04-06 00:11:27 | [diff] [blame] | 458 | // static |
[email protected] | d56d3bb | 2013-08-12 20:58:01 | [diff] [blame] | 459 | void Compositor::InitializeContextFactoryForTests(bool allow_test_contexts) { |
[email protected] | 4baecad | 2013-09-12 23:00:36 | [diff] [blame] | 460 | // The factory may already have been initialized by the content layer, in |
| 461 | // which case, use that one. |
| 462 | if (g_context_factory) |
| 463 | return; |
[email protected] | d56d3bb | 2013-08-12 20:58:01 | [diff] [blame] | 464 | DCHECK(!g_implicit_factory) << |
| 465 | "ContextFactory for tests already initialized."; |
| 466 | |
| 467 | bool use_test_contexts = true; |
| 468 | |
| 469 | // Always use test contexts unless the disable command line flag is used. |
| 470 | CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 471 | if (command_line->HasSwitch(switches::kDisableTestCompositor)) |
| 472 | use_test_contexts = false; |
| 473 | |
| 474 | #if defined(OS_CHROMEOS) |
| 475 | // If the test is running on the chromeos envrionment (such as |
| 476 | // device or vm bots), always use real contexts. |
[email protected] | 49c4cf85 | 2013-09-27 19:28:24 | [diff] [blame^] | 477 | if (base::SysInfo::IsRunningOnChromeOS()) |
[email protected] | d56d3bb | 2013-08-12 20:58:01 | [diff] [blame] | 478 | use_test_contexts = false; |
| 479 | #endif |
| 480 | |
| 481 | if (!allow_test_contexts) |
| 482 | use_test_contexts = false; |
| 483 | |
| 484 | if (use_test_contexts) { |
| 485 | g_implicit_factory = new ui::TestContextFactory; |
| 486 | } else { |
| 487 | DVLOG(1) << "Using DefaultContextFactory"; |
| 488 | scoped_ptr<ui::DefaultContextFactory> instance( |
| 489 | new ui::DefaultContextFactory()); |
| 490 | if (instance->Initialize()) |
| 491 | g_implicit_factory = instance.release(); |
| 492 | } |
| 493 | g_context_factory = g_implicit_factory; |
| 494 | } |
| 495 | |
| 496 | // static |
[email protected] | 188df65 | 2013-04-15 14:03:39 | [diff] [blame] | 497 | void Compositor::Initialize() { |
| 498 | #if defined(OS_CHROMEOS) |
| 499 | bool use_thread = !CommandLine::ForCurrentProcess()->HasSwitch( |
| 500 | switches::kUIDisableThreadedCompositing); |
| 501 | #else |
| 502 | bool use_thread = |
| 503 | CommandLine::ForCurrentProcess()->HasSwitch( |
| 504 | switches::kUIEnableThreadedCompositing) && |
| 505 | !CommandLine::ForCurrentProcess()->HasSwitch( |
| 506 | switches::kUIDisableThreadedCompositing); |
| 507 | #endif |
[email protected] | ec05af5 | 2012-11-21 23:07:00 | [diff] [blame] | 508 | if (use_thread) { |
| 509 | g_compositor_thread = new base::Thread("Browser Compositor"); |
[email protected] | e99339f | 2013-09-18 22:19:27 | [diff] [blame] | 510 | #if defined(OS_POSIX) |
| 511 | // Workaround for crbug.com/293736 |
| 512 | // On Posix, MessagePumpDefault uses system time, so delayed tasks (for |
| 513 | // compositor scheduling) work incorrectly across system time changes (e.g. |
| 514 | // tlsdate). So instead, use an IO loop, which uses libevent, that uses |
| 515 | // monotonic time (immune to these problems). |
| 516 | base::Thread::Options options; |
| 517 | options.message_loop_type = base::MessageLoop::TYPE_IO; |
| 518 | g_compositor_thread->StartWithOptions(options); |
| 519 | #else |
[email protected] | ec05af5 | 2012-11-21 23:07:00 | [diff] [blame] | 520 | g_compositor_thread->Start(); |
[email protected] | e99339f | 2013-09-18 22:19:27 | [diff] [blame] | 521 | #endif |
[email protected] | ec05af5 | 2012-11-21 23:07:00 | [diff] [blame] | 522 | } |
[email protected] | d56d3bb | 2013-08-12 20:58:01 | [diff] [blame] | 523 | |
| 524 | DCHECK(!g_compositor_initialized) << "Compositor initialized twice."; |
| 525 | g_compositor_initialized = true; |
[email protected] | ab662737 | 2012-01-29 21:22:13 | [diff] [blame] | 526 | } |
| 527 | |
[email protected] | 44ed4bd | 2013-04-06 00:11:27 | [diff] [blame] | 528 | // static |
| 529 | bool Compositor::WasInitializedWithThread() { |
[email protected] | f10dc47 | 2013-09-27 03:31:59 | [diff] [blame] | 530 | DCHECK(g_compositor_initialized); |
[email protected] | 44ed4bd | 2013-04-06 00:11:27 | [diff] [blame] | 531 | return !!g_compositor_thread; |
| 532 | } |
| 533 | |
| 534 | // static |
[email protected] | c7abcfd8 | 2013-06-01 04:02:12 | [diff] [blame] | 535 | scoped_refptr<base::MessageLoopProxy> Compositor::GetCompositorMessageLoop() { |
| 536 | scoped_refptr<base::MessageLoopProxy> proxy; |
| 537 | if (g_compositor_thread) |
| 538 | proxy = g_compositor_thread->message_loop_proxy(); |
| 539 | return proxy; |
| 540 | } |
| 541 | |
| 542 | // static |
[email protected] | ab662737 | 2012-01-29 21:22:13 | [diff] [blame] | 543 | void Compositor::Terminate() { |
[email protected] | d56d3bb | 2013-08-12 20:58:01 | [diff] [blame] | 544 | if (g_context_factory) { |
| 545 | if (g_implicit_factory) { |
| 546 | delete g_implicit_factory; |
| 547 | g_implicit_factory = NULL; |
| 548 | } |
| 549 | g_context_factory = NULL; |
| 550 | } |
| 551 | |
[email protected] | ab662737 | 2012-01-29 21:22:13 | [diff] [blame] | 552 | if (g_compositor_thread) { |
[email protected] | d56d3bb | 2013-08-12 20:58:01 | [diff] [blame] | 553 | DCHECK(!g_context_factory) |
| 554 | << "The ContextFactory should not outlive the compositor thread."; |
[email protected] | ec05af5 | 2012-11-21 23:07:00 | [diff] [blame] | 555 | g_compositor_thread->Stop(); |
[email protected] | ab662737 | 2012-01-29 21:22:13 | [diff] [blame] | 556 | delete g_compositor_thread; |
| 557 | g_compositor_thread = NULL; |
| 558 | } |
[email protected] | d56d3bb | 2013-08-12 20:58:01 | [diff] [blame] | 559 | |
| 560 | DCHECK(g_compositor_initialized) << "Compositor::Initialize() didn't happen."; |
| 561 | g_compositor_initialized = false; |
[email protected] | ab662737 | 2012-01-29 21:22:13 | [diff] [blame] | 562 | } |
| 563 | |
[email protected] | 33274903 | 2011-10-22 00:32:46 | [diff] [blame] | 564 | void Compositor::ScheduleDraw() { |
[email protected] | 4ea4e2d | 2012-11-20 15:17:09 | [diff] [blame] | 565 | if (g_compositor_thread) |
[email protected] | f0c2a24 | 2013-03-15 19:34:52 | [diff] [blame] | 566 | host_->Composite(base::TimeTicks::Now()); |
[email protected] | 4ea4e2d | 2012-11-20 15:17:09 | [diff] [blame] | 567 | else if (delegate_) |
[email protected] | ab662737 | 2012-01-29 21:22:13 | [diff] [blame] | 568 | delegate_->ScheduleDraw(); |
[email protected] | 33274903 | 2011-10-22 00:32:46 | [diff] [blame] | 569 | } |
| 570 | |
[email protected] | 993d6b32 | 2011-09-27 19:14:38 | [diff] [blame] | 571 | void Compositor::SetRootLayer(Layer* root_layer) { |
[email protected] | 12233c36 | 2011-11-21 16:09:25 | [diff] [blame] | 572 | if (root_layer_ == root_layer) |
| 573 | return; |
[email protected] | 7ab3f27 | 2011-11-16 00:51:56 | [diff] [blame] | 574 | if (root_layer_) |
| 575 | root_layer_->SetCompositor(NULL); |
[email protected] | 993d6b32 | 2011-09-27 19:14:38 | [diff] [blame] | 576 | root_layer_ = root_layer; |
[email protected] | 7ab3f27 | 2011-11-16 00:51:56 | [diff] [blame] | 577 | if (root_layer_ && !root_layer_->GetCompositor()) |
[email protected] | 993d6b32 | 2011-09-27 19:14:38 | [diff] [blame] | 578 | root_layer_->SetCompositor(this); |
[email protected] | 7aba666 | 2013-03-12 10:17:34 | [diff] [blame] | 579 | root_web_layer_->RemoveAllChildren(); |
[email protected] | 66efabe | 2012-08-18 03:06:06 | [diff] [blame] | 580 | if (root_layer_) |
[email protected] | 7aba666 | 2013-03-12 10:17:34 | [diff] [blame] | 581 | root_web_layer_->AddChild(root_layer_->cc_layer()); |
[email protected] | 993d6b32 | 2011-09-27 19:14:38 | [diff] [blame] | 582 | } |
| 583 | |
[email protected] | ebd5252 | 2012-10-04 15:49:40 | [diff] [blame] | 584 | void Compositor::SetHostHasTransparentBackground( |
| 585 | bool host_has_transparent_background) { |
[email protected] | 804c898 | 2013-03-13 16:32:21 | [diff] [blame] | 586 | host_->set_has_transparent_background(host_has_transparent_background); |
[email protected] | ebd5252 | 2012-10-04 15:49:40 | [diff] [blame] | 587 | } |
| 588 | |
[email protected] | 878705be | 2013-04-15 22:44:02 | [diff] [blame] | 589 | void Compositor::Draw() { |
[email protected] | 337bd04 | 2012-11-05 23:43:43 | [diff] [blame] | 590 | DCHECK(!g_compositor_thread); |
| 591 | |
[email protected] | ed8de92d | 2011-09-14 04:16:48 | [diff] [blame] | 592 | if (!root_layer_) |
| 593 | return; |
| 594 | |
[email protected] | d4ae8057 | 2012-06-06 23:02:06 | [diff] [blame] | 595 | last_started_frame_++; |
[email protected] | 337bd04 | 2012-11-05 23:43:43 | [diff] [blame] | 596 | PendingSwap pending_swap(DRAW_SWAP, posted_swaps_.get()); |
| 597 | if (!IsLocked()) { |
[email protected] | 6b16679e | 2012-10-27 00:44:28 | [diff] [blame] | 598 | // TODO(nduca): Temporary while compositor calls |
| 599 | // compositeImmediately() directly. |
[email protected] | 408b5e2 | 2013-03-19 09:48:09 | [diff] [blame] | 600 | Layout(); |
[email protected] | f0c2a24 | 2013-03-15 19:34:52 | [diff] [blame] | 601 | host_->Composite(base::TimeTicks::Now()); |
[email protected] | 8d9080ab | 2013-04-06 00:03:20 | [diff] [blame] | 602 | |
| 603 | #if defined(OS_WIN) |
| 604 | // While we resize, we are usually a few frames behind. By blocking |
| 605 | // the UI thread here we minize the area that is mis-painted, specially |
| 606 | // in the non-client area. See RenderWidgetHostViewAura::SetBounds for |
| 607 | // more details and bug 177115. |
| 608 | if (next_draw_is_resize_ && (last_ended_frame_ > 1)) { |
| 609 | next_draw_is_resize_ = false; |
| 610 | host_->FinishAllRendering(); |
| 611 | } |
| 612 | #endif |
| 613 | |
[email protected] | 6b16679e | 2012-10-27 00:44:28 | [diff] [blame] | 614 | } |
[email protected] | 337bd04 | 2012-11-05 23:43:43 | [diff] [blame] | 615 | if (!pending_swap.posted()) |
[email protected] | 192093059 | 2012-01-11 14:54:48 | [diff] [blame] | 616 | NotifyEnd(); |
[email protected] | ab662737 | 2012-01-29 21:22:13 | [diff] [blame] | 617 | } |
| 618 | |
[email protected] | 878705be | 2013-04-15 22:44:02 | [diff] [blame] | 619 | void Compositor::ScheduleFullRedraw() { |
[email protected] | 804c898 | 2013-03-13 16:32:21 | [diff] [blame] | 620 | host_->SetNeedsRedraw(); |
[email protected] | 7df588fbd | 2012-02-10 14:15:56 | [diff] [blame] | 621 | } |
| 622 | |
[email protected] | 878705be | 2013-04-15 22:44:02 | [diff] [blame] | 623 | void Compositor::ScheduleRedrawRect(const gfx::Rect& damage_rect) { |
| 624 | host_->SetNeedsRedrawRect(damage_rect); |
| 625 | } |
| 626 | |
[email protected] | 66239a2 | 2013-06-05 03:38:26 | [diff] [blame] | 627 | void Compositor::SetLatencyInfo(const ui::LatencyInfo& latency_info) { |
| 628 | host_->SetLatencyInfo(latency_info); |
| 629 | } |
| 630 | |
[email protected] | cd9a61c7 | 2012-05-08 19:16:59 | [diff] [blame] | 631 | bool Compositor::ReadPixels(SkBitmap* bitmap, |
| 632 | const gfx::Rect& bounds_in_pixel) { |
| 633 | if (bounds_in_pixel.right() > size().width() || |
| 634 | bounds_in_pixel.bottom() > size().height()) |
[email protected] | ab662737 | 2012-01-29 21:22:13 | [diff] [blame] | 635 | return false; |
[email protected] | ab662737 | 2012-01-29 21:22:13 | [diff] [blame] | 636 | bitmap->setConfig(SkBitmap::kARGB_8888_Config, |
[email protected] | cd9a61c7 | 2012-05-08 19:16:59 | [diff] [blame] | 637 | bounds_in_pixel.width(), bounds_in_pixel.height()); |
[email protected] | ab662737 | 2012-01-29 21:22:13 | [diff] [blame] | 638 | bitmap->allocPixels(); |
| 639 | SkAutoLockPixels lock_image(*bitmap); |
| 640 | unsigned char* pixels = static_cast<unsigned char*>(bitmap->getPixels()); |
[email protected] | 6b16679e | 2012-10-27 00:44:28 | [diff] [blame] | 641 | CancelCompositorLock(); |
[email protected] | 337bd04 | 2012-11-05 23:43:43 | [diff] [blame] | 642 | PendingSwap pending_swap(READPIXELS_SWAP, posted_swaps_.get()); |
[email protected] | 804c898 | 2013-03-13 16:32:21 | [diff] [blame] | 643 | return host_->CompositeAndReadback(pixels, bounds_in_pixel); |
[email protected] | ab662737 | 2012-01-29 21:22:13 | [diff] [blame] | 644 | } |
| 645 | |
[email protected] | cd9a61c7 | 2012-05-08 19:16:59 | [diff] [blame] | 646 | void Compositor::SetScaleAndSize(float scale, const gfx::Size& size_in_pixel) { |
[email protected] | cff176a | 2012-06-29 21:11:00 | [diff] [blame] | 647 | DCHECK_GT(scale, 0); |
[email protected] | 351b8ce | 2012-11-27 23:37:16 | [diff] [blame] | 648 | if (!size_in_pixel.IsEmpty()) { |
| 649 | size_ = size_in_pixel; |
[email protected] | 18ce5970 | 2013-04-09 04:58:40 | [diff] [blame] | 650 | host_->SetViewportSize(size_in_pixel); |
[email protected] | 7aba666 | 2013-03-12 10:17:34 | [diff] [blame] | 651 | root_web_layer_->SetBounds(size_in_pixel); |
[email protected] | 8d9080ab | 2013-04-06 00:03:20 | [diff] [blame] | 652 | |
| 653 | next_draw_is_resize_ = true; |
[email protected] | 351b8ce | 2012-11-27 23:37:16 | [diff] [blame] | 654 | } |
[email protected] | 2e2216e4 | 2012-05-17 15:17:00 | [diff] [blame] | 655 | if (device_scale_factor_ != scale) { |
[email protected] | cd9a61c7 | 2012-05-08 19:16:59 | [diff] [blame] | 656 | device_scale_factor_ = scale; |
| 657 | if (root_layer_) |
| 658 | root_layer_->OnDeviceScaleFactorChanged(scale); |
| 659 | } |
[email protected] | ed8de92d | 2011-09-14 04:16:48 | [diff] [blame] | 660 | } |
| 661 | |
[email protected] | 8760192 | 2013-04-02 03:56:42 | [diff] [blame] | 662 | void Compositor::SetBackgroundColor(SkColor color) { |
| 663 | host_->set_background_color(color); |
| 664 | ScheduleDraw(); |
| 665 | } |
| 666 | |
[email protected] | ed8de92d | 2011-09-14 04:16:48 | [diff] [blame] | 667 | void Compositor::AddObserver(CompositorObserver* observer) { |
| 668 | observer_list_.AddObserver(observer); |
| 669 | } |
| 670 | |
| 671 | void Compositor::RemoveObserver(CompositorObserver* observer) { |
| 672 | observer_list_.RemoveObserver(observer); |
| 673 | } |
| 674 | |
[email protected] | 3ce2feb | 2011-09-19 18:44:23 | [diff] [blame] | 675 | bool Compositor::HasObserver(CompositorObserver* observer) { |
| 676 | return observer_list_.HasObserver(observer); |
| 677 | } |
| 678 | |
[email protected] | 894e8fc | 2012-02-24 13:29:50 | [diff] [blame] | 679 | void Compositor::OnSwapBuffersPosted() { |
[email protected] | 337bd04 | 2012-11-05 23:43:43 | [diff] [blame] | 680 | DCHECK(!g_compositor_thread); |
| 681 | posted_swaps_->PostSwap(); |
[email protected] | 894e8fc | 2012-02-24 13:29:50 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | void Compositor::OnSwapBuffersComplete() { |
[email protected] | 337bd04 | 2012-11-05 23:43:43 | [diff] [blame] | 685 | DCHECK(!g_compositor_thread); |
| 686 | DCHECK(posted_swaps_->AreSwapsPosted()); |
| 687 | DCHECK_GE(1, posted_swaps_->NumSwapsPosted(DRAW_SWAP)); |
| 688 | if (posted_swaps_->NextPostedSwap() == DRAW_SWAP) |
| 689 | NotifyEnd(); |
| 690 | posted_swaps_->EndSwap(); |
[email protected] | 894e8fc | 2012-02-24 13:29:50 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | void Compositor::OnSwapBuffersAborted() { |
[email protected] | f7972906 | 2013-04-03 20:01:50 | [diff] [blame] | 694 | if (!g_compositor_thread) { |
| 695 | DCHECK_GE(1, posted_swaps_->NumSwapsPosted(DRAW_SWAP)); |
[email protected] | 337bd04 | 2012-11-05 23:43:43 | [diff] [blame] | 696 | |
[email protected] | f7972906 | 2013-04-03 20:01:50 | [diff] [blame] | 697 | // We've just lost the context, so unwind all posted_swaps. |
| 698 | while (posted_swaps_->AreSwapsPosted()) { |
| 699 | if (posted_swaps_->NextPostedSwap() == DRAW_SWAP) |
| 700 | NotifyEnd(); |
| 701 | posted_swaps_->EndSwap(); |
| 702 | } |
[email protected] | d912f8c | 2012-11-03 02:35:01 | [diff] [blame] | 703 | } |
[email protected] | 337bd04 | 2012-11-05 23:43:43 | [diff] [blame] | 704 | |
[email protected] | 91f4c79 | 2012-06-06 02:21:19 | [diff] [blame] | 705 | FOR_EACH_OBSERVER(CompositorObserver, |
| 706 | observer_list_, |
| 707 | OnCompositingAborted(this)); |
[email protected] | 894e8fc | 2012-02-24 13:29:50 | [diff] [blame] | 708 | } |
[email protected] | ab662737 | 2012-01-29 21:22:13 | [diff] [blame] | 709 | |
[email protected] | 3b608551 | 2013-02-21 01:26:20 | [diff] [blame] | 710 | void Compositor::OnUpdateVSyncParameters(base::TimeTicks timebase, |
| 711 | base::TimeDelta interval) { |
| 712 | FOR_EACH_OBSERVER(CompositorObserver, |
| 713 | observer_list_, |
| 714 | OnUpdateVSyncParameters(this, timebase, interval)); |
| 715 | } |
| 716 | |
[email protected] | 408b5e2 | 2013-03-19 09:48:09 | [diff] [blame] | 717 | void Compositor::Layout() { |
[email protected] | d4ae8057 | 2012-06-06 23:02:06 | [diff] [blame] | 718 | // We're sending damage that will be addressed during this composite |
| 719 | // cycle, so we don't need to schedule another composite to address it. |
| 720 | disable_schedule_composite_ = true; |
[email protected] | f78649ea | 2012-02-23 18:39:04 | [diff] [blame] | 721 | if (root_layer_) |
[email protected] | cedc395 | 2012-03-06 06:15:55 | [diff] [blame] | 722 | root_layer_->SendDamagedRects(); |
[email protected] | d4ae8057 | 2012-06-06 23:02:06 | [diff] [blame] | 723 | disable_schedule_composite_ = false; |
[email protected] | ab662737 | 2012-01-29 21:22:13 | [diff] [blame] | 724 | } |
| 725 | |
[email protected] | ebc0e1df | 2013-08-01 02:46:22 | [diff] [blame] | 726 | scoped_ptr<cc::OutputSurface> Compositor::CreateOutputSurface(bool fallback) { |
[email protected] | bc90af6b | 2013-07-29 20:32:39 | [diff] [blame] | 727 | return ContextFactory::GetInstance()->CreateOutputSurface(this); |
[email protected] | ab662737 | 2012-01-29 21:22:13 | [diff] [blame] | 728 | } |
| 729 | |
[email protected] | 408b5e2 | 2013-03-19 09:48:09 | [diff] [blame] | 730 | void Compositor::DidCommit() { |
[email protected] | 6b16679e | 2012-10-27 00:44:28 | [diff] [blame] | 731 | DCHECK(!IsLocked()); |
[email protected] | 2700daddd | 2012-07-13 19:35:37 | [diff] [blame] | 732 | FOR_EACH_OBSERVER(CompositorObserver, |
| 733 | observer_list_, |
| 734 | OnCompositingDidCommit(this)); |
| 735 | } |
| 736 | |
[email protected] | 408b5e2 | 2013-03-19 09:48:09 | [diff] [blame] | 737 | void Compositor::DidCommitAndDrawFrame() { |
[email protected] | 3b608551 | 2013-02-21 01:26:20 | [diff] [blame] | 738 | base::TimeTicks start_time = base::TimeTicks::Now(); |
[email protected] | a8f677c | 2012-03-23 01:36:06 | [diff] [blame] | 739 | FOR_EACH_OBSERVER(CompositorObserver, |
| 740 | observer_list_, |
[email protected] | 3b608551 | 2013-02-21 01:26:20 | [diff] [blame] | 741 | OnCompositingStarted(this, start_time)); |
[email protected] | ba7aeb8 | 2012-02-24 23:36:13 | [diff] [blame] | 742 | } |
| 743 | |
[email protected] | 408b5e2 | 2013-03-19 09:48:09 | [diff] [blame] | 744 | void Compositor::DidCompleteSwapBuffers() { |
[email protected] | 337bd04 | 2012-11-05 23:43:43 | [diff] [blame] | 745 | DCHECK(g_compositor_thread); |
[email protected] | ba7aeb8 | 2012-02-24 23:36:13 | [diff] [blame] | 746 | NotifyEnd(); |
[email protected] | ab662737 | 2012-01-29 21:22:13 | [diff] [blame] | 747 | } |
| 748 | |
[email protected] | 408b5e2 | 2013-03-19 09:48:09 | [diff] [blame] | 749 | void Compositor::ScheduleComposite() { |
[email protected] | d4ae8057 | 2012-06-06 23:02:06 | [diff] [blame] | 750 | if (!disable_schedule_composite_) |
| 751 | ScheduleDraw(); |
[email protected] | 33274903 | 2011-10-22 00:32:46 | [diff] [blame] | 752 | } |
| 753 | |
[email protected] | f10dc47 | 2013-09-27 03:31:59 | [diff] [blame] | 754 | scoped_refptr<cc::ContextProvider> Compositor::OffscreenContextProvider() { |
| 755 | return ContextFactory::GetInstance()->OffscreenCompositorContextProvider(); |
[email protected] | 0a45172 | 2013-02-22 20:32:05 | [diff] [blame] | 756 | } |
| 757 | |
[email protected] | 2e77cdbb | 2013-04-29 13:59:14 | [diff] [blame] | 758 | const cc::LayerTreeDebugState& Compositor::GetLayerTreeDebugState() const { |
| 759 | return host_->debug_state(); |
[email protected] | 918f8db4 | 2013-04-27 01:53:40 | [diff] [blame] | 760 | } |
| 761 | |
[email protected] | 2e77cdbb | 2013-04-29 13:59:14 | [diff] [blame] | 762 | void Compositor::SetLayerTreeDebugState( |
| 763 | const cc::LayerTreeDebugState& debug_state) { |
[email protected] | 918f8db4 | 2013-04-27 01:53:40 | [diff] [blame] | 764 | host_->SetDebugState(debug_state); |
| 765 | } |
| 766 | |
[email protected] | 6b16679e | 2012-10-27 00:44:28 | [diff] [blame] | 767 | scoped_refptr<CompositorLock> Compositor::GetCompositorLock() { |
| 768 | if (!compositor_lock_) { |
| 769 | compositor_lock_ = new CompositorLock(this); |
| 770 | if (g_compositor_thread) |
[email protected] | 804c898 | 2013-03-13 16:32:21 | [diff] [blame] | 771 | host_->SetDeferCommits(true); |
[email protected] | 6b16679e | 2012-10-27 00:44:28 | [diff] [blame] | 772 | FOR_EACH_OBSERVER(CompositorObserver, |
| 773 | observer_list_, |
| 774 | OnCompositingLockStateChanged(this)); |
| 775 | } |
| 776 | return compositor_lock_; |
| 777 | } |
| 778 | |
| 779 | void Compositor::UnlockCompositor() { |
| 780 | DCHECK(compositor_lock_); |
| 781 | compositor_lock_ = NULL; |
| 782 | if (g_compositor_thread) |
[email protected] | 804c898 | 2013-03-13 16:32:21 | [diff] [blame] | 783 | host_->SetDeferCommits(false); |
[email protected] | 6b16679e | 2012-10-27 00:44:28 | [diff] [blame] | 784 | FOR_EACH_OBSERVER(CompositorObserver, |
| 785 | observer_list_, |
| 786 | OnCompositingLockStateChanged(this)); |
| 787 | } |
| 788 | |
| 789 | void Compositor::CancelCompositorLock() { |
| 790 | if (compositor_lock_) |
| 791 | compositor_lock_->CancelLock(); |
| 792 | } |
| 793 | |
[email protected] | a8f2115 | 2011-09-08 15:30:02 | [diff] [blame] | 794 | void Compositor::NotifyEnd() { |
[email protected] | d4ae8057 | 2012-06-06 23:02:06 | [diff] [blame] | 795 | last_ended_frame_++; |
[email protected] | a8f2115 | 2011-09-08 15:30:02 | [diff] [blame] | 796 | FOR_EACH_OBSERVER(CompositorObserver, |
| 797 | observer_list_, |
[email protected] | 3ce2feb | 2011-09-19 18:44:23 | [diff] [blame] | 798 | OnCompositingEnded(this)); |
[email protected] | a8f2115 | 2011-09-08 15:30:02 | [diff] [blame] | 799 | } |
| 800 | |
[email protected] | c797cd4 | 2011-03-15 02:18:36 | [diff] [blame] | 801 | } // namespace ui |