[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] | 1e7565a | 2013-04-08 20:11:14 | [diff] [blame] | 11 | #include "base/cancelable_callback.h" |
[email protected] | ab662737 | 2012-01-29 21:22:13 | [diff] [blame] | 12 | #include "base/command_line.h" |
[email protected] | 0a45172 | 2013-02-22 20:32:05 | [diff] [blame] | 13 | #include "base/memory/singleton.h" |
[email protected] | 6b16679e | 2012-10-27 00:44:28 | [diff] [blame] | 14 | #include "base/message_loop.h" |
[email protected] | 1e7565a | 2013-04-08 20:11:14 | [diff] [blame] | 15 | #include "base/run_loop.h" |
[email protected] | 06666c0 | 2013-03-07 19:32:30 | [diff] [blame] | 16 | #include "base/string_util.h" |
[email protected] | ec05af5 | 2012-11-21 23:07:00 | [diff] [blame] | 17 | #include "base/threading/thread.h" |
[email protected] | 894e8fc | 2012-02-24 13:29:50 | [diff] [blame] | 18 | #include "base/threading/thread_restrictions.h" |
[email protected] | 4d5e676 | 2013-03-19 18:46:57 | [diff] [blame] | 19 | #include "cc/base/switches.h" |
[email protected] | 681ccff | 2013-03-18 06:13:52 | [diff] [blame] | 20 | #include "cc/base/thread_impl.h" |
[email protected] | 3052b10f | 2013-03-18 07:41:21 | [diff] [blame] | 21 | #include "cc/input/input_handler.h" |
[email protected] | cc3cfaa | 2013-03-18 09:05:52 | [diff] [blame] | 22 | #include "cc/layers/layer.h" |
[email protected] | 7f0d825f | 2013-03-18 07:24:30 | [diff] [blame] | 23 | #include "cc/output/context_provider.h" |
| 24 | #include "cc/output/output_surface.h" |
[email protected] | 556fd29 | 2013-03-18 08:03:04 | [diff] [blame] | 25 | #include "cc/trees/layer_tree_host.h" |
[email protected] | 83afcbcc | 2012-07-27 03:06:27 | [diff] [blame] | 26 | #include "third_party/skia/include/core/SkBitmap.h" |
[email protected] | 116302fc | 2012-05-05 21:45:41 | [diff] [blame] | 27 | #include "ui/compositor/compositor_observer.h" |
| 28 | #include "ui/compositor/compositor_switches.h" |
[email protected] | cd9a61c7 | 2012-05-08 19:16:59 | [diff] [blame] | 29 | #include "ui/compositor/dip_util.h" |
[email protected] | 116302fc | 2012-05-05 21:45:41 | [diff] [blame] | 30 | #include "ui/compositor/layer.h" |
| 31 | #include "ui/compositor/test_web_graphics_context_3d.h" |
[email protected] | c9e2cbbb | 2012-05-12 21:17:27 | [diff] [blame] | 32 | #include "ui/gl/gl_context.h" |
| 33 | #include "ui/gl/gl_implementation.h" |
| 34 | #include "ui/gl/gl_surface.h" |
[email protected] | cc2ae01 | 2012-09-21 19:35:25 | [diff] [blame] | 35 | #include "ui/gl/gl_switches.h" |
[email protected] | 0a45172 | 2013-02-22 20:32:05 | [diff] [blame] | 36 | #include "webkit/gpu/grcontext_for_webgraphicscontext3d.h" |
[email protected] | ab662737 | 2012-01-29 21:22:13 | [diff] [blame] | 37 | #include "webkit/gpu/webgraphicscontext3d_in_process_impl.h" |
| 38 | |
[email protected] | 8391a49e | 2012-05-26 06:31:04 | [diff] [blame] | 39 | #if defined(OS_CHROMEOS) |
| 40 | #include "base/chromeos/chromeos_version.h" |
| 41 | #endif |
| 42 | |
[email protected] | ab662737 | 2012-01-29 21:22:13 | [diff] [blame] | 43 | namespace { |
| 44 | |
| 45 | const double kDefaultRefreshRate = 60.0; |
[email protected] | 7ddeaab | 2013-04-06 00:47:05 | [diff] [blame] | 46 | const double kTestRefreshRate = 200.0; |
[email protected] | ab662737 | 2012-01-29 21:22:13 | [diff] [blame] | 47 | |
[email protected] | 337bd04 | 2012-11-05 23:43:43 | [diff] [blame] | 48 | enum SwapType { |
| 49 | DRAW_SWAP, |
| 50 | READPIXELS_SWAP, |
| 51 | }; |
| 52 | |
[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] | 8cbffc6 | 2013-01-23 22:11:09 | [diff] [blame] | 55 | bool g_test_compositor_enabled = false; |
[email protected] | ab662737 | 2012-01-29 21:22:13 | [diff] [blame] | 56 | |
[email protected] | 40034cf | 2013-03-13 05:05:37 | [diff] [blame] | 57 | ui::ContextFactory* g_implicit_factory = NULL; |
[email protected] | 894e8fc | 2012-02-24 13:29:50 | [diff] [blame] | 58 | ui::ContextFactory* g_context_factory = NULL; |
| 59 | |
[email protected] | 6b16679e | 2012-10-27 00:44:28 | [diff] [blame] | 60 | const int kCompositorLockTimeoutMs = 67; |
| 61 | |
[email protected] | 337bd04 | 2012-11-05 23:43:43 | [diff] [blame] | 62 | class PendingSwap { |
| 63 | public: |
| 64 | PendingSwap(SwapType type, ui::PostedSwapQueue* posted_swaps); |
| 65 | ~PendingSwap(); |
| 66 | |
| 67 | SwapType type() const { return type_; } |
| 68 | bool posted() const { return posted_; } |
| 69 | |
| 70 | private: |
| 71 | friend class ui::PostedSwapQueue; |
| 72 | |
| 73 | SwapType type_; |
| 74 | bool posted_; |
| 75 | ui::PostedSwapQueue* posted_swaps_; |
| 76 | |
| 77 | DISALLOW_COPY_AND_ASSIGN(PendingSwap); |
| 78 | }; |
| 79 | |
[email protected] | 40034cf | 2013-03-13 05:05:37 | [diff] [blame] | 80 | void SetupImplicitFactory() { |
| 81 | // We leak the implicit factory so that we don't race with the tear down of |
| 82 | // the gl_bindings. |
| 83 | DCHECK(!g_context_factory); |
| 84 | DCHECK(!g_implicit_factory); |
| 85 | if (g_test_compositor_enabled) { |
| 86 | g_implicit_factory = new ui::TestContextFactory; |
| 87 | } else { |
| 88 | DVLOG(1) << "Using DefaultContextFactory"; |
| 89 | scoped_ptr<ui::DefaultContextFactory> instance( |
| 90 | new ui::DefaultContextFactory()); |
| 91 | if (instance->Initialize()) |
| 92 | g_implicit_factory = instance.release(); |
[email protected] | 0a45172 | 2013-02-22 20:32:05 | [diff] [blame] | 93 | } |
[email protected] | 40034cf | 2013-03-13 05:05:37 | [diff] [blame] | 94 | g_context_factory = g_implicit_factory; |
| 95 | } |
[email protected] | 0a45172 | 2013-02-22 20:32:05 | [diff] [blame] | 96 | |
[email protected] | 40034cf | 2013-03-13 05:05:37 | [diff] [blame] | 97 | void ResetImplicitFactory() { |
| 98 | if (!g_implicit_factory || g_context_factory != g_implicit_factory) |
| 99 | return; |
| 100 | delete g_implicit_factory; |
| 101 | g_implicit_factory = NULL; |
| 102 | g_context_factory = NULL; |
| 103 | } |
[email protected] | 0a45172 | 2013-02-22 20:32:05 | [diff] [blame] | 104 | |
[email protected] | 83afcbcc | 2012-07-27 03:06:27 | [diff] [blame] | 105 | } // namespace |
[email protected] | c797cd4 | 2011-03-15 02:18:36 | [diff] [blame] | 106 | |
| 107 | namespace ui { |
| 108 | |
[email protected] | 894e8fc | 2012-02-24 13:29:50 | [diff] [blame] | 109 | // static |
| 110 | ContextFactory* ContextFactory::GetInstance() { |
[email protected] | 40034cf | 2013-03-13 05:05:37 | [diff] [blame] | 111 | if (!g_context_factory) |
| 112 | SetupImplicitFactory(); |
[email protected] | 894e8fc | 2012-02-24 13:29:50 | [diff] [blame] | 113 | return g_context_factory; |
[email protected] | ca80663 | 2012-02-16 02:15:59 | [diff] [blame] | 114 | } |
| 115 | |
[email protected] | 58b4b6d | 2012-02-16 01:40:24 | [diff] [blame] | 116 | // static |
[email protected] | 894e8fc | 2012-02-24 13:29:50 | [diff] [blame] | 117 | void ContextFactory::SetInstance(ContextFactory* instance) { |
| 118 | g_context_factory = instance; |
[email protected] | ca80663 | 2012-02-16 02:15:59 | [diff] [blame] | 119 | } |
| 120 | |
[email protected] | 40034cf | 2013-03-13 05:05:37 | [diff] [blame] | 121 | class ContextProviderFromContextFactory : public cc::ContextProvider { |
[email protected] | 0a45172 | 2013-02-22 20:32:05 | [diff] [blame] | 122 | public: |
[email protected] | e06e112 | 2013-03-15 17:12:38 | [diff] [blame] | 123 | static scoped_refptr<ContextProviderFromContextFactory> Create( |
| 124 | ContextFactory* factory) { |
| 125 | scoped_refptr<ContextProviderFromContextFactory> provider = |
| 126 | new ContextProviderFromContextFactory(factory); |
| 127 | if (!provider->InitializeOnMainThread()) |
| 128 | return NULL; |
| 129 | return provider; |
[email protected] | 0a45172 | 2013-02-22 20:32:05 | [diff] [blame] | 130 | } |
| 131 | |
[email protected] | aa3174b | 2013-03-15 09:12:48 | [diff] [blame] | 132 | virtual bool BindToCurrentThread() OVERRIDE { |
[email protected] | e06e112 | 2013-03-15 17:12:38 | [diff] [blame] | 133 | DCHECK(context3d_); |
| 134 | |
[email protected] | 0a45172 | 2013-02-22 20:32:05 | [diff] [blame] | 135 | return context3d_->makeContextCurrent(); |
| 136 | } |
| 137 | |
[email protected] | aa3174b | 2013-03-15 09:12:48 | [diff] [blame] | 138 | virtual WebKit::WebGraphicsContext3D* Context3d() OVERRIDE { |
[email protected] | e06e112 | 2013-03-15 17:12:38 | [diff] [blame] | 139 | DCHECK(context3d_); |
| 140 | |
[email protected] | aa3174b | 2013-03-15 09:12:48 | [diff] [blame] | 141 | return context3d_.get(); |
| 142 | } |
[email protected] | 0a45172 | 2013-02-22 20:32:05 | [diff] [blame] | 143 | |
[email protected] | aa3174b | 2013-03-15 09:12:48 | [diff] [blame] | 144 | virtual class GrContext* GrContext() OVERRIDE { |
[email protected] | e06e112 | 2013-03-15 17:12:38 | [diff] [blame] | 145 | DCHECK(context3d_); |
| 146 | |
[email protected] | 0a45172 | 2013-02-22 20:32:05 | [diff] [blame] | 147 | if (!gr_context_) { |
| 148 | gr_context_.reset( |
| 149 | new webkit::gpu::GrContextForWebGraphicsContext3D(context3d_.get())); |
| 150 | } |
| 151 | return gr_context_->get(); |
| 152 | } |
| 153 | |
| 154 | virtual void VerifyContexts() OVERRIDE { |
[email protected] | e06e112 | 2013-03-15 17:12:38 | [diff] [blame] | 155 | DCHECK(context3d_); |
| 156 | |
| 157 | if (context3d_->isContextLost()) { |
| 158 | base::AutoLock lock(destroyed_lock_); |
| 159 | destroyed_ = true; |
| 160 | } |
[email protected] | 0a45172 | 2013-02-22 20:32:05 | [diff] [blame] | 161 | } |
| 162 | |
[email protected] | aa3174b | 2013-03-15 09:12:48 | [diff] [blame] | 163 | virtual bool DestroyedOnMainThread() OVERRIDE { |
[email protected] | 0a45172 | 2013-02-22 20:32:05 | [diff] [blame] | 164 | base::AutoLock lock(destroyed_lock_); |
| 165 | return destroyed_; |
| 166 | } |
| 167 | |
| 168 | protected: |
[email protected] | e06e112 | 2013-03-15 17:12:38 | [diff] [blame] | 169 | explicit ContextProviderFromContextFactory(ContextFactory* factory) |
| 170 | : factory_(factory), |
| 171 | destroyed_(false) {} |
[email protected] | 40034cf | 2013-03-13 05:05:37 | [diff] [blame] | 172 | virtual ~ContextProviderFromContextFactory() {} |
[email protected] | 0a45172 | 2013-02-22 20:32:05 | [diff] [blame] | 173 | |
[email protected] | e06e112 | 2013-03-15 17:12:38 | [diff] [blame] | 174 | bool InitializeOnMainThread() { |
| 175 | if (context3d_) |
| 176 | return true; |
| 177 | context3d_.reset(factory_->CreateOffscreenContext()); |
| 178 | return !!context3d_; |
| 179 | } |
| 180 | |
[email protected] | 0a45172 | 2013-02-22 20:32:05 | [diff] [blame] | 181 | private: |
| 182 | ContextFactory* factory_; |
| 183 | base::Lock destroyed_lock_; |
| 184 | bool destroyed_; |
| 185 | scoped_ptr<WebKit::WebGraphicsContext3D> context3d_; |
| 186 | scoped_ptr<webkit::gpu::GrContextForWebGraphicsContext3D> gr_context_; |
| 187 | }; |
| 188 | |
[email protected] | 40034cf | 2013-03-13 05:05:37 | [diff] [blame] | 189 | DefaultContextFactory::DefaultContextFactory() { |
| 190 | } |
| 191 | |
| 192 | DefaultContextFactory::~DefaultContextFactory() { |
| 193 | } |
| 194 | |
| 195 | bool DefaultContextFactory::Initialize() { |
| 196 | if (!gfx::GLSurface::InitializeOneOff() || |
| 197 | gfx::GetGLImplementation() == gfx::kGLImplementationNone) { |
| 198 | LOG(ERROR) << "Could not load the GL bindings"; |
| 199 | return false; |
| 200 | } |
| 201 | return true; |
| 202 | } |
| 203 | |
| 204 | cc::OutputSurface* DefaultContextFactory::CreateOutputSurface( |
| 205 | Compositor* compositor) { |
| 206 | return new cc::OutputSurface( |
| 207 | make_scoped_ptr(CreateContextCommon(compositor, false))); |
| 208 | } |
| 209 | |
| 210 | WebKit::WebGraphicsContext3D* DefaultContextFactory::CreateOffscreenContext() { |
| 211 | return CreateContextCommon(NULL, true); |
| 212 | } |
| 213 | |
[email protected] | 0a45172 | 2013-02-22 20:32:05 | [diff] [blame] | 214 | scoped_refptr<cc::ContextProvider> |
| 215 | DefaultContextFactory::OffscreenContextProviderForMainThread() { |
| 216 | if (!offscreen_contexts_main_thread_ || |
| 217 | !offscreen_contexts_main_thread_->DestroyedOnMainThread()) { |
[email protected] | 40034cf | 2013-03-13 05:05:37 | [diff] [blame] | 218 | offscreen_contexts_main_thread_ = |
[email protected] | e06e112 | 2013-03-15 17:12:38 | [diff] [blame] | 219 | ContextProviderFromContextFactory::Create(this); |
| 220 | if (offscreen_contexts_main_thread_ && |
| 221 | !offscreen_contexts_main_thread_->BindToCurrentThread()) |
| 222 | offscreen_contexts_main_thread_ = NULL; |
[email protected] | 0a45172 | 2013-02-22 20:32:05 | [diff] [blame] | 223 | } |
| 224 | return offscreen_contexts_main_thread_; |
| 225 | } |
| 226 | |
| 227 | scoped_refptr<cc::ContextProvider> |
| 228 | DefaultContextFactory::OffscreenContextProviderForCompositorThread() { |
| 229 | if (!offscreen_contexts_compositor_thread_ || |
| 230 | !offscreen_contexts_compositor_thread_->DestroyedOnMainThread()) { |
[email protected] | 40034cf | 2013-03-13 05:05:37 | [diff] [blame] | 231 | offscreen_contexts_compositor_thread_ = |
[email protected] | e06e112 | 2013-03-15 17:12:38 | [diff] [blame] | 232 | ContextProviderFromContextFactory::Create(this); |
[email protected] | 0a45172 | 2013-02-22 20:32:05 | [diff] [blame] | 233 | } |
| 234 | return offscreen_contexts_compositor_thread_; |
| 235 | } |
| 236 | |
[email protected] | 84dba8ca | 2012-04-10 03:27:39 | [diff] [blame] | 237 | void DefaultContextFactory::RemoveCompositor(Compositor* compositor) { |
| 238 | } |
| 239 | |
| 240 | WebKit::WebGraphicsContext3D* DefaultContextFactory::CreateContextCommon( |
| 241 | Compositor* compositor, |
| 242 | bool offscreen) { |
[email protected] | b18ed0aa | 2012-08-22 04:20:20 | [diff] [blame] | 243 | DCHECK(offscreen || compositor); |
[email protected] | 894e8fc | 2012-02-24 13:29:50 | [diff] [blame] | 244 | WebKit::WebGraphicsContext3D::Attributes attrs; |
[email protected] | b63dce5 | 2012-08-22 22:51:50 | [diff] [blame] | 245 | attrs.depth = false; |
| 246 | attrs.stencil = false; |
| 247 | attrs.antialias = false; |
[email protected] | 894e8fc | 2012-02-24 13:29:50 | [diff] [blame] | 248 | attrs.shareResources = true; |
| 249 | WebKit::WebGraphicsContext3D* context = |
[email protected] | 84dba8ca | 2012-04-10 03:27:39 | [diff] [blame] | 250 | offscreen ? |
| 251 | webkit::gpu::WebGraphicsContext3DInProcessImpl::CreateForWebView( |
| 252 | attrs, false) : |
[email protected] | 894e8fc | 2012-02-24 13:29:50 | [diff] [blame] | 253 | webkit::gpu::WebGraphicsContext3DInProcessImpl::CreateForWindow( |
[email protected] | 40034cf | 2013-03-13 05:05:37 | [diff] [blame] | 254 | attrs, compositor->widget(), NULL); |
[email protected] | 69b8e8f | 2012-06-18 02:42:59 | [diff] [blame] | 255 | if (!context) |
| 256 | return NULL; |
| 257 | |
[email protected] | 894e8fc | 2012-02-24 13:29:50 | [diff] [blame] | 258 | CommandLine* command_line = CommandLine::ForCurrentProcess(); |
[email protected] | 8fcaffb | 2012-05-25 23:19:00 | [diff] [blame] | 259 | if (!offscreen) { |
[email protected] | 894e8fc | 2012-02-24 13:29:50 | [diff] [blame] | 260 | context->makeContextCurrent(); |
| 261 | gfx::GLContext* gl_context = gfx::GLContext::GetCurrent(); |
[email protected] | cc2ae01 | 2012-09-21 19:35:25 | [diff] [blame] | 262 | bool vsync = !command_line->HasSwitch(switches::kDisableGpuVsync); |
[email protected] | 8fcaffb | 2012-05-25 23:19:00 | [diff] [blame] | 263 | gl_context->SetSwapInterval(vsync ? 1 : 0); |
[email protected] | 894e8fc | 2012-02-24 13:29:50 | [diff] [blame] | 264 | gl_context->ReleaseCurrent(NULL); |
| 265 | } |
| 266 | return context; |
[email protected] | ab662737 | 2012-01-29 21:22:13 | [diff] [blame] | 267 | } |
| 268 | |
[email protected] | e06e112 | 2013-03-15 17:12:38 | [diff] [blame] | 269 | TestContextFactory::TestContextFactory() {} |
[email protected] | 40034cf | 2013-03-13 05:05:37 | [diff] [blame] | 270 | |
[email protected] | e06e112 | 2013-03-15 17:12:38 | [diff] [blame] | 271 | TestContextFactory::~TestContextFactory() {} |
[email protected] | 40034cf | 2013-03-13 05:05:37 | [diff] [blame] | 272 | |
| 273 | cc::OutputSurface* TestContextFactory::CreateOutputSurface( |
| 274 | Compositor* compositor) { |
| 275 | return new cc::OutputSurface(make_scoped_ptr(CreateOffscreenContext())); |
| 276 | } |
| 277 | |
| 278 | WebKit::WebGraphicsContext3D* TestContextFactory::CreateOffscreenContext() { |
| 279 | ui::TestWebGraphicsContext3D* context = new ui::TestWebGraphicsContext3D; |
| 280 | context->Initialize(); |
| 281 | return context; |
| 282 | } |
| 283 | |
| 284 | scoped_refptr<cc::ContextProvider> |
| 285 | TestContextFactory::OffscreenContextProviderForMainThread() { |
[email protected] | e06e112 | 2013-03-15 17:12:38 | [diff] [blame] | 286 | if (!offscreen_contexts_main_thread_ || |
| 287 | offscreen_contexts_main_thread_->DestroyedOnMainThread()) { |
| 288 | offscreen_contexts_main_thread_ = |
| 289 | ContextProviderFromContextFactory::Create(this); |
| 290 | CHECK(offscreen_contexts_main_thread_->BindToCurrentThread()); |
| 291 | } |
[email protected] | 40034cf | 2013-03-13 05:05:37 | [diff] [blame] | 292 | return offscreen_contexts_main_thread_; |
| 293 | } |
| 294 | |
| 295 | scoped_refptr<cc::ContextProvider> |
| 296 | TestContextFactory::OffscreenContextProviderForCompositorThread() { |
[email protected] | e06e112 | 2013-03-15 17:12:38 | [diff] [blame] | 297 | if (!offscreen_contexts_compositor_thread_ || |
| 298 | offscreen_contexts_compositor_thread_->DestroyedOnMainThread()) { |
| 299 | offscreen_contexts_compositor_thread_ = |
| 300 | ContextProviderFromContextFactory::Create(this); |
| 301 | } |
[email protected] | 40034cf | 2013-03-13 05:05:37 | [diff] [blame] | 302 | return offscreen_contexts_compositor_thread_; |
| 303 | } |
| 304 | |
| 305 | void TestContextFactory::RemoveCompositor(Compositor* compositor) { |
| 306 | } |
| 307 | |
[email protected] | d3407cdb | 2012-09-21 22:33:25 | [diff] [blame] | 308 | Texture::Texture(bool flipped, const gfx::Size& size, float device_scale_factor) |
[email protected] | 34466d0 | 2012-12-10 18:00:34 | [diff] [blame] | 309 | : size_(size), |
| 310 | flipped_(flipped), |
[email protected] | d3407cdb | 2012-09-21 22:33:25 | [diff] [blame] | 311 | device_scale_factor_(device_scale_factor) { |
[email protected] | ab662737 | 2012-01-29 21:22:13 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | Texture::~Texture() { |
| 315 | } |
| 316 | |
[email protected] | 06666c0 | 2013-03-07 19:32:30 | [diff] [blame] | 317 | std::string Texture::Produce() { |
| 318 | return EmptyString(); |
[email protected] | f027ca150 | 2012-12-21 01:42:32 | [diff] [blame] | 319 | } |
| 320 | |
[email protected] | 6b16679e | 2012-10-27 00:44:28 | [diff] [blame] | 321 | CompositorLock::CompositorLock(Compositor* compositor) |
| 322 | : compositor_(compositor) { |
| 323 | MessageLoop::current()->PostDelayedTask( |
| 324 | FROM_HERE, |
| 325 | base::Bind(&CompositorLock::CancelLock, AsWeakPtr()), |
| 326 | base::TimeDelta::FromMilliseconds(kCompositorLockTimeoutMs)); |
| 327 | } |
| 328 | |
| 329 | CompositorLock::~CompositorLock() { |
| 330 | CancelLock(); |
| 331 | } |
| 332 | |
| 333 | void CompositorLock::CancelLock() { |
| 334 | if (!compositor_) |
| 335 | return; |
| 336 | compositor_->UnlockCompositor(); |
| 337 | compositor_ = NULL; |
| 338 | } |
| 339 | |
[email protected] | 1e7565a | 2013-04-08 20:11:14 | [diff] [blame] | 340 | // static |
| 341 | bool DrawWaiterForTest::Wait(Compositor* compositor) { |
| 342 | DrawWaiterForTest waiter; |
| 343 | return waiter.WaitImpl(compositor); |
| 344 | } |
| 345 | |
| 346 | DrawWaiterForTest::DrawWaiterForTest() |
| 347 | : kDrawWaitTimeOutMs(1000), |
| 348 | did_draw_(false) { |
| 349 | } |
| 350 | |
| 351 | DrawWaiterForTest::~DrawWaiterForTest() { |
| 352 | } |
| 353 | |
| 354 | bool DrawWaiterForTest::WaitImpl(Compositor* compositor) { |
| 355 | did_draw_ = false; |
| 356 | compositor->AddObserver(this); |
| 357 | wait_run_loop_.reset(new base::RunLoop()); |
| 358 | base::CancelableClosure timeout( |
| 359 | base::Bind(&DrawWaiterForTest::TimedOutWhileWaiting, |
| 360 | base::Unretained(this))); |
| 361 | MessageLoop::current()->PostDelayedTask( |
| 362 | FROM_HERE, timeout.callback(), |
| 363 | base::TimeDelta::FromMilliseconds(kDrawWaitTimeOutMs)); |
| 364 | wait_run_loop_->Run(); |
| 365 | compositor->RemoveObserver(this); |
| 366 | return did_draw_; |
| 367 | } |
| 368 | |
| 369 | void DrawWaiterForTest::TimedOutWhileWaiting() { |
| 370 | LOG(ERROR) << "Timed out waiting for draw."; |
| 371 | wait_run_loop_->Quit(); |
| 372 | } |
| 373 | |
| 374 | void DrawWaiterForTest::OnCompositingDidCommit(Compositor* compositor) { |
| 375 | } |
| 376 | |
| 377 | void DrawWaiterForTest::OnCompositingStarted(Compositor* compositor, |
| 378 | base::TimeTicks start_time) { |
| 379 | } |
| 380 | |
| 381 | void DrawWaiterForTest::OnCompositingEnded(Compositor* compositor) { |
| 382 | did_draw_ = true; |
| 383 | wait_run_loop_->Quit(); |
| 384 | } |
| 385 | |
| 386 | void DrawWaiterForTest::OnCompositingAborted(Compositor* compositor) { |
| 387 | } |
| 388 | |
| 389 | void DrawWaiterForTest::OnCompositingLockStateChanged(Compositor* compositor) { |
| 390 | } |
| 391 | |
| 392 | void DrawWaiterForTest::OnUpdateVSyncParameters(Compositor* compositor, |
| 393 | base::TimeTicks timebase, |
| 394 | base::TimeDelta interval) { |
| 395 | } |
| 396 | |
[email protected] | 337bd04 | 2012-11-05 23:43:43 | [diff] [blame] | 397 | class PostedSwapQueue { |
| 398 | public: |
| 399 | PostedSwapQueue() : pending_swap_(NULL) { |
| 400 | } |
| 401 | |
| 402 | ~PostedSwapQueue() { |
| 403 | DCHECK(!pending_swap_); |
| 404 | } |
| 405 | |
| 406 | SwapType NextPostedSwap() const { |
| 407 | return queue_.front(); |
| 408 | } |
| 409 | |
| 410 | bool AreSwapsPosted() const { |
| 411 | return !queue_.empty(); |
| 412 | } |
| 413 | |
| 414 | int NumSwapsPosted(SwapType type) const { |
| 415 | int count = 0; |
| 416 | for (std::deque<SwapType>::const_iterator it = queue_.begin(); |
| 417 | it != queue_.end(); ++it) { |
| 418 | if (*it == type) |
| 419 | count++; |
| 420 | } |
| 421 | return count; |
| 422 | } |
| 423 | |
| 424 | void PostSwap() { |
| 425 | DCHECK(pending_swap_); |
| 426 | queue_.push_back(pending_swap_->type()); |
| 427 | pending_swap_->posted_ = true; |
| 428 | } |
| 429 | |
| 430 | void EndSwap() { |
| 431 | queue_.pop_front(); |
| 432 | } |
| 433 | |
| 434 | private: |
| 435 | friend class ::PendingSwap; |
| 436 | |
| 437 | PendingSwap* pending_swap_; |
| 438 | std::deque<SwapType> queue_; |
| 439 | |
| 440 | DISALLOW_COPY_AND_ASSIGN(PostedSwapQueue); |
| 441 | }; |
| 442 | |
| 443 | } // namespace ui |
| 444 | |
| 445 | namespace { |
| 446 | |
| 447 | PendingSwap::PendingSwap(SwapType type, ui::PostedSwapQueue* posted_swaps) |
| 448 | : type_(type), posted_(false), posted_swaps_(posted_swaps) { |
| 449 | // Only one pending swap in flight. |
| 450 | DCHECK_EQ(static_cast<PendingSwap*>(NULL), posted_swaps_->pending_swap_); |
| 451 | posted_swaps_->pending_swap_ = this; |
| 452 | } |
| 453 | |
| 454 | PendingSwap::~PendingSwap() { |
| 455 | DCHECK_EQ(this, posted_swaps_->pending_swap_); |
| 456 | posted_swaps_->pending_swap_ = NULL; |
| 457 | } |
| 458 | |
| 459 | } // namespace |
| 460 | |
| 461 | namespace ui { |
| 462 | |
[email protected] | ab662737 | 2012-01-29 21:22:13 | [diff] [blame] | 463 | Compositor::Compositor(CompositorDelegate* delegate, |
[email protected] | cd9a61c7 | 2012-05-08 19:16:59 | [diff] [blame] | 464 | gfx::AcceleratedWidget widget) |
[email protected] | 82a01ac | 2011-09-08 16:00:18 | [diff] [blame] | 465 | : delegate_(delegate), |
[email protected] | ab662737 | 2012-01-29 21:22:13 | [diff] [blame] | 466 | root_layer_(NULL), |
| 467 | widget_(widget), |
[email protected] | 337bd04 | 2012-11-05 23:43:43 | [diff] [blame] | 468 | posted_swaps_(new PostedSwapQueue()), |
[email protected] | d4ae8057 | 2012-06-06 23:02:06 | [diff] [blame] | 469 | device_scale_factor_(0.0f), |
| 470 | last_started_frame_(0), |
| 471 | last_ended_frame_(0), |
[email protected] | 8d9080ab | 2013-04-06 00:03:20 | [diff] [blame] | 472 | next_draw_is_resize_(false), |
[email protected] | 6b16679e | 2012-10-27 00:44:28 | [diff] [blame] | 473 | disable_schedule_composite_(false), |
| 474 | compositor_lock_(NULL) { |
[email protected] | 7aba666 | 2013-03-12 10:17:34 | [diff] [blame] | 475 | root_web_layer_ = cc::Layer::Create(); |
| 476 | root_web_layer_->SetAnchorPoint(gfx::PointF(0.f, 0.f)); |
[email protected] | ec05af5 | 2012-11-21 23:07:00 | [diff] [blame] | 477 | |
[email protected] | ab662737 | 2012-01-29 21:22:13 | [diff] [blame] | 478 | CommandLine* command_line = CommandLine::ForCurrentProcess(); |
[email protected] | 4d5e676 | 2013-03-19 18:46:57 | [diff] [blame] | 479 | |
[email protected] | ec05af5 | 2012-11-21 23:07:00 | [diff] [blame] | 480 | cc::LayerTreeSettings settings; |
[email protected] | 8e0176d | 2013-03-21 03:14:52 | [diff] [blame] | 481 | settings.refresh_rate = |
[email protected] | 8cbffc6 | 2013-01-23 22:11:09 | [diff] [blame] | 482 | g_test_compositor_enabled ? kTestRefreshRate : kDefaultRefreshRate; |
[email protected] | 8e0176d | 2013-03-21 03:14:52 | [diff] [blame] | 483 | settings.partial_swap_enabled = |
[email protected] | 4d5e676 | 2013-03-19 18:46:57 | [diff] [blame] | 484 | command_line->HasSwitch(cc::switches::kUIEnablePartialSwap); |
[email protected] | 8e0176d | 2013-03-21 03:14:52 | [diff] [blame] | 485 | settings.per_tile_painting_enabled = |
[email protected] | 4d5e676 | 2013-03-19 18:46:57 | [diff] [blame] | 486 | command_line->HasSwitch(cc::switches::kUIEnablePerTilePainting); |
| 487 | |
| 488 | // These flags should be mirrored by renderer versions in content/renderer/. |
[email protected] | 8e0176d | 2013-03-21 03:14:52 | [diff] [blame] | 489 | settings.initial_debug_state.show_debug_borders = |
[email protected] | 4d5e676 | 2013-03-19 18:46:57 | [diff] [blame] | 490 | command_line->HasSwitch(cc::switches::kUIShowCompositedLayerBorders); |
[email protected] | 8e0176d | 2013-03-21 03:14:52 | [diff] [blame] | 491 | settings.initial_debug_state.show_fps_counter = |
[email protected] | 4d5e676 | 2013-03-19 18:46:57 | [diff] [blame] | 492 | command_line->HasSwitch(cc::switches::kUIShowFPSCounter); |
[email protected] | 8e0176d | 2013-03-21 03:14:52 | [diff] [blame] | 493 | settings.initial_debug_state.show_paint_rects = |
[email protected] | 4d5e676 | 2013-03-19 18:46:57 | [diff] [blame] | 494 | command_line->HasSwitch(switches::kUIShowPaintRects); |
[email protected] | 8e0176d | 2013-03-21 03:14:52 | [diff] [blame] | 495 | settings.initial_debug_state.show_platform_layer_tree = |
[email protected] | 4d5e676 | 2013-03-19 18:46:57 | [diff] [blame] | 496 | command_line->HasSwitch(cc::switches::kUIShowCompositedLayerTree); |
[email protected] | 8e0176d | 2013-03-21 03:14:52 | [diff] [blame] | 497 | settings.initial_debug_state.show_property_changed_rects = |
[email protected] | 4d5e676 | 2013-03-19 18:46:57 | [diff] [blame] | 498 | command_line->HasSwitch(cc::switches::kUIShowPropertyChangedRects); |
[email protected] | 8e0176d | 2013-03-21 03:14:52 | [diff] [blame] | 499 | settings.initial_debug_state.show_surface_damage_rects = |
[email protected] | 4d5e676 | 2013-03-19 18:46:57 | [diff] [blame] | 500 | command_line->HasSwitch(cc::switches::kUIShowSurfaceDamageRects); |
[email protected] | 8e0176d | 2013-03-21 03:14:52 | [diff] [blame] | 501 | settings.initial_debug_state.show_screen_space_rects = |
[email protected] | 4d5e676 | 2013-03-19 18:46:57 | [diff] [blame] | 502 | command_line->HasSwitch(cc::switches::kUIShowScreenSpaceRects); |
[email protected] | 8e0176d | 2013-03-21 03:14:52 | [diff] [blame] | 503 | settings.initial_debug_state.show_replica_screen_space_rects = |
[email protected] | 4d5e676 | 2013-03-19 18:46:57 | [diff] [blame] | 504 | command_line->HasSwitch(cc::switches::kUIShowReplicaScreenSpaceRects); |
[email protected] | 8e0176d | 2013-03-21 03:14:52 | [diff] [blame] | 505 | settings.initial_debug_state.show_occluding_rects = |
[email protected] | 4d5e676 | 2013-03-19 18:46:57 | [diff] [blame] | 506 | command_line->HasSwitch(cc::switches::kUIShowOccludingRects); |
[email protected] | 8e0176d | 2013-03-21 03:14:52 | [diff] [blame] | 507 | settings.initial_debug_state.show_non_occluding_rects = |
[email protected] | 4d5e676 | 2013-03-19 18:46:57 | [diff] [blame] | 508 | command_line->HasSwitch(cc::switches::kUIShowNonOccludingRects); |
[email protected] | 302fe42 | 2012-06-11 14:49:11 | [diff] [blame] | 509 | |
[email protected] | ec05af5 | 2012-11-21 23:07:00 | [diff] [blame] | 510 | scoped_ptr<cc::Thread> thread; |
| 511 | if (g_compositor_thread) { |
[email protected] | a5069b57 | 2013-03-17 21:50:42 | [diff] [blame] | 512 | thread = cc::ThreadImpl::CreateForDifferentThread( |
[email protected] | ec05af5 | 2012-11-21 23:07:00 | [diff] [blame] | 513 | g_compositor_thread->message_loop_proxy()); |
| 514 | } |
| 515 | |
[email protected] | 804c898 | 2013-03-13 16:32:21 | [diff] [blame] | 516 | host_ = cc::LayerTreeHost::Create(this, settings, thread.Pass()); |
| 517 | host_->SetRootLayer(root_web_layer_); |
| 518 | host_->SetSurfaceReady(); |
[email protected] | 82a01ac | 2011-09-08 16:00:18 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | Compositor::~Compositor() { |
[email protected] | 6b16679e | 2012-10-27 00:44:28 | [diff] [blame] | 522 | CancelCompositorLock(); |
| 523 | DCHECK(!compositor_lock_); |
| 524 | |
[email protected] | e332a1b | 2012-05-10 20:32:27 | [diff] [blame] | 525 | // Don't call |CompositorDelegate::ScheduleDraw| from this point. |
| 526 | delegate_ = NULL; |
[email protected] | 7ab3f27 | 2011-11-16 00:51:56 | [diff] [blame] | 527 | if (root_layer_) |
| 528 | root_layer_->SetCompositor(NULL); |
[email protected] | 2700daddd | 2012-07-13 19:35:37 | [diff] [blame] | 529 | |
| 530 | // Stop all outstanding draws before telling the ContextFactory to tear |
| 531 | // down any contexts that the |host_| may rely upon. |
| 532 | host_.reset(); |
| 533 | |
[email protected] | 40034cf | 2013-03-13 05:05:37 | [diff] [blame] | 534 | ContextFactory::GetInstance()->RemoveCompositor(this); |
[email protected] | 82a01ac | 2011-09-08 16:00:18 | [diff] [blame] | 535 | } |
| 536 | |
[email protected] | 44ed4bd | 2013-04-06 00:11:27 | [diff] [blame] | 537 | // static |
[email protected] | ab662737 | 2012-01-29 21:22:13 | [diff] [blame] | 538 | void Compositor::Initialize(bool use_thread) { |
[email protected] | ec05af5 | 2012-11-21 23:07:00 | [diff] [blame] | 539 | if (use_thread) { |
| 540 | g_compositor_thread = new base::Thread("Browser Compositor"); |
| 541 | g_compositor_thread->Start(); |
| 542 | } |
[email protected] | ab662737 | 2012-01-29 21:22:13 | [diff] [blame] | 543 | } |
| 544 | |
[email protected] | 44ed4bd | 2013-04-06 00:11:27 | [diff] [blame] | 545 | // static |
| 546 | bool Compositor::WasInitializedWithThread() { |
| 547 | return !!g_compositor_thread; |
| 548 | } |
| 549 | |
| 550 | // static |
[email protected] | ab662737 | 2012-01-29 21:22:13 | [diff] [blame] | 551 | void Compositor::Terminate() { |
[email protected] | ab662737 | 2012-01-29 21:22:13 | [diff] [blame] | 552 | if (g_compositor_thread) { |
[email protected] | ec05af5 | 2012-11-21 23:07:00 | [diff] [blame] | 553 | g_compositor_thread->Stop(); |
[email protected] | ab662737 | 2012-01-29 21:22:13 | [diff] [blame] | 554 | delete g_compositor_thread; |
| 555 | g_compositor_thread = NULL; |
| 556 | } |
| 557 | } |
| 558 | |
[email protected] | 33274903 | 2011-10-22 00:32:46 | [diff] [blame] | 559 | void Compositor::ScheduleDraw() { |
[email protected] | 4ea4e2d | 2012-11-20 15:17:09 | [diff] [blame] | 560 | if (g_compositor_thread) |
[email protected] | f0c2a24 | 2013-03-15 19:34:52 | [diff] [blame] | 561 | host_->Composite(base::TimeTicks::Now()); |
[email protected] | 4ea4e2d | 2012-11-20 15:17:09 | [diff] [blame] | 562 | else if (delegate_) |
[email protected] | ab662737 | 2012-01-29 21:22:13 | [diff] [blame] | 563 | delegate_->ScheduleDraw(); |
[email protected] | 33274903 | 2011-10-22 00:32:46 | [diff] [blame] | 564 | } |
| 565 | |
[email protected] | 993d6b32 | 2011-09-27 19:14:38 | [diff] [blame] | 566 | void Compositor::SetRootLayer(Layer* root_layer) { |
[email protected] | 12233c36 | 2011-11-21 16:09:25 | [diff] [blame] | 567 | if (root_layer_ == root_layer) |
| 568 | return; |
[email protected] | 7ab3f27 | 2011-11-16 00:51:56 | [diff] [blame] | 569 | if (root_layer_) |
| 570 | root_layer_->SetCompositor(NULL); |
[email protected] | 993d6b32 | 2011-09-27 19:14:38 | [diff] [blame] | 571 | root_layer_ = root_layer; |
[email protected] | 7ab3f27 | 2011-11-16 00:51:56 | [diff] [blame] | 572 | if (root_layer_ && !root_layer_->GetCompositor()) |
[email protected] | 993d6b32 | 2011-09-27 19:14:38 | [diff] [blame] | 573 | root_layer_->SetCompositor(this); |
[email protected] | 7aba666 | 2013-03-12 10:17:34 | [diff] [blame] | 574 | root_web_layer_->RemoveAllChildren(); |
[email protected] | 66efabe | 2012-08-18 03:06:06 | [diff] [blame] | 575 | if (root_layer_) |
[email protected] | 7aba666 | 2013-03-12 10:17:34 | [diff] [blame] | 576 | root_web_layer_->AddChild(root_layer_->cc_layer()); |
[email protected] | 993d6b32 | 2011-09-27 19:14:38 | [diff] [blame] | 577 | } |
| 578 | |
[email protected] | ebd5252 | 2012-10-04 15:49:40 | [diff] [blame] | 579 | void Compositor::SetHostHasTransparentBackground( |
| 580 | bool host_has_transparent_background) { |
[email protected] | 804c898 | 2013-03-13 16:32:21 | [diff] [blame] | 581 | host_->set_has_transparent_background(host_has_transparent_background); |
[email protected] | ebd5252 | 2012-10-04 15:49:40 | [diff] [blame] | 582 | } |
| 583 | |
[email protected] | ed8de92d | 2011-09-14 04:16:48 | [diff] [blame] | 584 | void Compositor::Draw(bool force_clear) { |
[email protected] | 337bd04 | 2012-11-05 23:43:43 | [diff] [blame] | 585 | DCHECK(!g_compositor_thread); |
| 586 | |
[email protected] | ed8de92d | 2011-09-14 04:16:48 | [diff] [blame] | 587 | if (!root_layer_) |
| 588 | return; |
| 589 | |
[email protected] | d4ae8057 | 2012-06-06 23:02:06 | [diff] [blame] | 590 | last_started_frame_++; |
[email protected] | 337bd04 | 2012-11-05 23:43:43 | [diff] [blame] | 591 | PendingSwap pending_swap(DRAW_SWAP, posted_swaps_.get()); |
| 592 | if (!IsLocked()) { |
[email protected] | 6b16679e | 2012-10-27 00:44:28 | [diff] [blame] | 593 | // TODO(nduca): Temporary while compositor calls |
| 594 | // compositeImmediately() directly. |
[email protected] | 408b5e2 | 2013-03-19 09:48:09 | [diff] [blame] | 595 | Layout(); |
[email protected] | f0c2a24 | 2013-03-15 19:34:52 | [diff] [blame] | 596 | host_->Composite(base::TimeTicks::Now()); |
[email protected] | 8d9080ab | 2013-04-06 00:03:20 | [diff] [blame] | 597 | |
| 598 | #if defined(OS_WIN) |
| 599 | // While we resize, we are usually a few frames behind. By blocking |
| 600 | // the UI thread here we minize the area that is mis-painted, specially |
| 601 | // in the non-client area. See RenderWidgetHostViewAura::SetBounds for |
| 602 | // more details and bug 177115. |
| 603 | if (next_draw_is_resize_ && (last_ended_frame_ > 1)) { |
| 604 | next_draw_is_resize_ = false; |
| 605 | host_->FinishAllRendering(); |
| 606 | } |
| 607 | #endif |
| 608 | |
[email protected] | 6b16679e | 2012-10-27 00:44:28 | [diff] [blame] | 609 | } |
[email protected] | 337bd04 | 2012-11-05 23:43:43 | [diff] [blame] | 610 | if (!pending_swap.posted()) |
[email protected] | 192093059 | 2012-01-11 14:54:48 | [diff] [blame] | 611 | NotifyEnd(); |
[email protected] | ab662737 | 2012-01-29 21:22:13 | [diff] [blame] | 612 | } |
| 613 | |
[email protected] | 7df588fbd | 2012-02-10 14:15:56 | [diff] [blame] | 614 | void Compositor::ScheduleFullDraw() { |
[email protected] | 804c898 | 2013-03-13 16:32:21 | [diff] [blame] | 615 | host_->SetNeedsRedraw(); |
[email protected] | 7df588fbd | 2012-02-10 14:15:56 | [diff] [blame] | 616 | } |
| 617 | |
[email protected] | cd9a61c7 | 2012-05-08 19:16:59 | [diff] [blame] | 618 | bool Compositor::ReadPixels(SkBitmap* bitmap, |
| 619 | const gfx::Rect& bounds_in_pixel) { |
| 620 | if (bounds_in_pixel.right() > size().width() || |
| 621 | bounds_in_pixel.bottom() > size().height()) |
[email protected] | ab662737 | 2012-01-29 21:22:13 | [diff] [blame] | 622 | return false; |
[email protected] | ab662737 | 2012-01-29 21:22:13 | [diff] [blame] | 623 | bitmap->setConfig(SkBitmap::kARGB_8888_Config, |
[email protected] | cd9a61c7 | 2012-05-08 19:16:59 | [diff] [blame] | 624 | bounds_in_pixel.width(), bounds_in_pixel.height()); |
[email protected] | ab662737 | 2012-01-29 21:22:13 | [diff] [blame] | 625 | bitmap->allocPixels(); |
| 626 | SkAutoLockPixels lock_image(*bitmap); |
| 627 | unsigned char* pixels = static_cast<unsigned char*>(bitmap->getPixels()); |
[email protected] | 6b16679e | 2012-10-27 00:44:28 | [diff] [blame] | 628 | CancelCompositorLock(); |
[email protected] | 337bd04 | 2012-11-05 23:43:43 | [diff] [blame] | 629 | PendingSwap pending_swap(READPIXELS_SWAP, posted_swaps_.get()); |
[email protected] | 804c898 | 2013-03-13 16:32:21 | [diff] [blame] | 630 | return host_->CompositeAndReadback(pixels, bounds_in_pixel); |
[email protected] | ab662737 | 2012-01-29 21:22:13 | [diff] [blame] | 631 | } |
| 632 | |
[email protected] | cd9a61c7 | 2012-05-08 19:16:59 | [diff] [blame] | 633 | void Compositor::SetScaleAndSize(float scale, const gfx::Size& size_in_pixel) { |
[email protected] | cff176a | 2012-06-29 21:11:00 | [diff] [blame] | 634 | DCHECK_GT(scale, 0); |
[email protected] | 351b8ce | 2012-11-27 23:37:16 | [diff] [blame] | 635 | if (!size_in_pixel.IsEmpty()) { |
| 636 | size_ = size_in_pixel; |
[email protected] | 18ce5970 | 2013-04-09 04:58:40 | [diff] [blame^] | 637 | host_->SetViewportSize(size_in_pixel); |
[email protected] | 7aba666 | 2013-03-12 10:17:34 | [diff] [blame] | 638 | root_web_layer_->SetBounds(size_in_pixel); |
[email protected] | 8d9080ab | 2013-04-06 00:03:20 | [diff] [blame] | 639 | |
| 640 | next_draw_is_resize_ = true; |
[email protected] | 351b8ce | 2012-11-27 23:37:16 | [diff] [blame] | 641 | } |
[email protected] | 2e2216e4 | 2012-05-17 15:17:00 | [diff] [blame] | 642 | if (device_scale_factor_ != scale) { |
[email protected] | cd9a61c7 | 2012-05-08 19:16:59 | [diff] [blame] | 643 | device_scale_factor_ = scale; |
| 644 | if (root_layer_) |
| 645 | root_layer_->OnDeviceScaleFactorChanged(scale); |
| 646 | } |
[email protected] | ed8de92d | 2011-09-14 04:16:48 | [diff] [blame] | 647 | } |
| 648 | |
[email protected] | 8760192 | 2013-04-02 03:56:42 | [diff] [blame] | 649 | void Compositor::SetBackgroundColor(SkColor color) { |
| 650 | host_->set_background_color(color); |
| 651 | ScheduleDraw(); |
| 652 | } |
| 653 | |
[email protected] | ed8de92d | 2011-09-14 04:16:48 | [diff] [blame] | 654 | void Compositor::AddObserver(CompositorObserver* observer) { |
| 655 | observer_list_.AddObserver(observer); |
| 656 | } |
| 657 | |
| 658 | void Compositor::RemoveObserver(CompositorObserver* observer) { |
| 659 | observer_list_.RemoveObserver(observer); |
| 660 | } |
| 661 | |
[email protected] | 3ce2feb | 2011-09-19 18:44:23 | [diff] [blame] | 662 | bool Compositor::HasObserver(CompositorObserver* observer) { |
| 663 | return observer_list_.HasObserver(observer); |
| 664 | } |
| 665 | |
[email protected] | 894e8fc | 2012-02-24 13:29:50 | [diff] [blame] | 666 | void Compositor::OnSwapBuffersPosted() { |
[email protected] | 337bd04 | 2012-11-05 23:43:43 | [diff] [blame] | 667 | DCHECK(!g_compositor_thread); |
| 668 | posted_swaps_->PostSwap(); |
[email protected] | 894e8fc | 2012-02-24 13:29:50 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | void Compositor::OnSwapBuffersComplete() { |
[email protected] | 337bd04 | 2012-11-05 23:43:43 | [diff] [blame] | 672 | DCHECK(!g_compositor_thread); |
| 673 | DCHECK(posted_swaps_->AreSwapsPosted()); |
| 674 | DCHECK_GE(1, posted_swaps_->NumSwapsPosted(DRAW_SWAP)); |
| 675 | if (posted_swaps_->NextPostedSwap() == DRAW_SWAP) |
| 676 | NotifyEnd(); |
| 677 | posted_swaps_->EndSwap(); |
[email protected] | 894e8fc | 2012-02-24 13:29:50 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | void Compositor::OnSwapBuffersAborted() { |
[email protected] | f7972906 | 2013-04-03 20:01:50 | [diff] [blame] | 681 | if (!g_compositor_thread) { |
| 682 | DCHECK_GE(1, posted_swaps_->NumSwapsPosted(DRAW_SWAP)); |
[email protected] | 337bd04 | 2012-11-05 23:43:43 | [diff] [blame] | 683 | |
[email protected] | f7972906 | 2013-04-03 20:01:50 | [diff] [blame] | 684 | // We've just lost the context, so unwind all posted_swaps. |
| 685 | while (posted_swaps_->AreSwapsPosted()) { |
| 686 | if (posted_swaps_->NextPostedSwap() == DRAW_SWAP) |
| 687 | NotifyEnd(); |
| 688 | posted_swaps_->EndSwap(); |
| 689 | } |
[email protected] | d912f8c | 2012-11-03 02:35:01 | [diff] [blame] | 690 | } |
[email protected] | 337bd04 | 2012-11-05 23:43:43 | [diff] [blame] | 691 | |
[email protected] | 91f4c79 | 2012-06-06 02:21:19 | [diff] [blame] | 692 | FOR_EACH_OBSERVER(CompositorObserver, |
| 693 | observer_list_, |
| 694 | OnCompositingAborted(this)); |
[email protected] | 894e8fc | 2012-02-24 13:29:50 | [diff] [blame] | 695 | } |
[email protected] | ab662737 | 2012-01-29 21:22:13 | [diff] [blame] | 696 | |
[email protected] | 3b608551 | 2013-02-21 01:26:20 | [diff] [blame] | 697 | void Compositor::OnUpdateVSyncParameters(base::TimeTicks timebase, |
| 698 | base::TimeDelta interval) { |
| 699 | FOR_EACH_OBSERVER(CompositorObserver, |
| 700 | observer_list_, |
| 701 | OnUpdateVSyncParameters(this, timebase, interval)); |
| 702 | } |
| 703 | |
[email protected] | 408b5e2 | 2013-03-19 09:48:09 | [diff] [blame] | 704 | void Compositor::Layout() { |
[email protected] | d4ae8057 | 2012-06-06 23:02:06 | [diff] [blame] | 705 | // We're sending damage that will be addressed during this composite |
| 706 | // cycle, so we don't need to schedule another composite to address it. |
| 707 | disable_schedule_composite_ = true; |
[email protected] | f78649ea | 2012-02-23 18:39:04 | [diff] [blame] | 708 | if (root_layer_) |
[email protected] | cedc395 | 2012-03-06 06:15:55 | [diff] [blame] | 709 | root_layer_->SendDamagedRects(); |
[email protected] | d4ae8057 | 2012-06-06 23:02:06 | [diff] [blame] | 710 | disable_schedule_composite_ = false; |
[email protected] | ab662737 | 2012-01-29 21:22:13 | [diff] [blame] | 711 | } |
| 712 | |
[email protected] | 408b5e2 | 2013-03-19 09:48:09 | [diff] [blame] | 713 | scoped_ptr<cc::OutputSurface> Compositor::CreateOutputSurface() { |
[email protected] | 40034cf | 2013-03-13 05:05:37 | [diff] [blame] | 714 | return make_scoped_ptr( |
| 715 | ContextFactory::GetInstance()->CreateOutputSurface(this)); |
[email protected] | ab662737 | 2012-01-29 21:22:13 | [diff] [blame] | 716 | } |
| 717 | |
[email protected] | 408b5e2 | 2013-03-19 09:48:09 | [diff] [blame] | 718 | scoped_ptr<cc::InputHandler> Compositor::CreateInputHandler() { |
[email protected] | ec05af5 | 2012-11-21 23:07:00 | [diff] [blame] | 719 | return scoped_ptr<cc::InputHandler>(); |
| 720 | } |
| 721 | |
[email protected] | 408b5e2 | 2013-03-19 09:48:09 | [diff] [blame] | 722 | void Compositor::DidCommit() { |
[email protected] | 6b16679e | 2012-10-27 00:44:28 | [diff] [blame] | 723 | DCHECK(!IsLocked()); |
[email protected] | 2700daddd | 2012-07-13 19:35:37 | [diff] [blame] | 724 | FOR_EACH_OBSERVER(CompositorObserver, |
| 725 | observer_list_, |
| 726 | OnCompositingDidCommit(this)); |
| 727 | } |
| 728 | |
[email protected] | 408b5e2 | 2013-03-19 09:48:09 | [diff] [blame] | 729 | void Compositor::DidCommitAndDrawFrame() { |
[email protected] | 3b608551 | 2013-02-21 01:26:20 | [diff] [blame] | 730 | base::TimeTicks start_time = base::TimeTicks::Now(); |
[email protected] | a8f677c | 2012-03-23 01:36:06 | [diff] [blame] | 731 | FOR_EACH_OBSERVER(CompositorObserver, |
| 732 | observer_list_, |
[email protected] | 3b608551 | 2013-02-21 01:26:20 | [diff] [blame] | 733 | OnCompositingStarted(this, start_time)); |
[email protected] | f7972906 | 2013-04-03 20:01:50 | [diff] [blame] | 734 | // If we're threaded without a swap complete callback, we have to |
| 735 | // call DidCompleteSwapBuffersManually. |
| 736 | if (g_compositor_thread && |
| 737 | !host_->GetRendererCapabilities().using_swap_complete_callback) |
| 738 | DidCompleteSwapBuffers(); |
[email protected] | ba7aeb8 | 2012-02-24 23:36:13 | [diff] [blame] | 739 | } |
| 740 | |
[email protected] | 408b5e2 | 2013-03-19 09:48:09 | [diff] [blame] | 741 | void Compositor::DidCompleteSwapBuffers() { |
[email protected] | 337bd04 | 2012-11-05 23:43:43 | [diff] [blame] | 742 | DCHECK(g_compositor_thread); |
[email protected] | ba7aeb8 | 2012-02-24 23:36:13 | [diff] [blame] | 743 | NotifyEnd(); |
[email protected] | ab662737 | 2012-01-29 21:22:13 | [diff] [blame] | 744 | } |
| 745 | |
[email protected] | 408b5e2 | 2013-03-19 09:48:09 | [diff] [blame] | 746 | void Compositor::ScheduleComposite() { |
[email protected] | d4ae8057 | 2012-06-06 23:02:06 | [diff] [blame] | 747 | if (!disable_schedule_composite_) |
| 748 | ScheduleDraw(); |
[email protected] | 33274903 | 2011-10-22 00:32:46 | [diff] [blame] | 749 | } |
| 750 | |
[email protected] | 0a45172 | 2013-02-22 20:32:05 | [diff] [blame] | 751 | scoped_refptr<cc::ContextProvider> |
| 752 | Compositor::OffscreenContextProviderForMainThread() { |
[email protected] | 0a45172 | 2013-02-22 20:32:05 | [diff] [blame] | 753 | return ContextFactory::GetInstance()->OffscreenContextProviderForMainThread(); |
| 754 | } |
| 755 | |
| 756 | scoped_refptr<cc::ContextProvider> |
| 757 | Compositor::OffscreenContextProviderForCompositorThread() { |
[email protected] | 0a45172 | 2013-02-22 20:32:05 | [diff] [blame] | 758 | return ContextFactory::GetInstance()-> |
| 759 | OffscreenContextProviderForCompositorThread(); |
| 760 | } |
| 761 | |
[email protected] | 6b16679e | 2012-10-27 00:44:28 | [diff] [blame] | 762 | scoped_refptr<CompositorLock> Compositor::GetCompositorLock() { |
| 763 | if (!compositor_lock_) { |
| 764 | compositor_lock_ = new CompositorLock(this); |
| 765 | if (g_compositor_thread) |
[email protected] | 804c898 | 2013-03-13 16:32:21 | [diff] [blame] | 766 | host_->SetDeferCommits(true); |
[email protected] | 6b16679e | 2012-10-27 00:44:28 | [diff] [blame] | 767 | FOR_EACH_OBSERVER(CompositorObserver, |
| 768 | observer_list_, |
| 769 | OnCompositingLockStateChanged(this)); |
| 770 | } |
| 771 | return compositor_lock_; |
| 772 | } |
| 773 | |
| 774 | void Compositor::UnlockCompositor() { |
| 775 | DCHECK(compositor_lock_); |
| 776 | compositor_lock_ = NULL; |
| 777 | if (g_compositor_thread) |
[email protected] | 804c898 | 2013-03-13 16:32:21 | [diff] [blame] | 778 | host_->SetDeferCommits(false); |
[email protected] | 6b16679e | 2012-10-27 00:44:28 | [diff] [blame] | 779 | FOR_EACH_OBSERVER(CompositorObserver, |
| 780 | observer_list_, |
| 781 | OnCompositingLockStateChanged(this)); |
| 782 | } |
| 783 | |
| 784 | void Compositor::CancelCompositorLock() { |
| 785 | if (compositor_lock_) |
| 786 | compositor_lock_->CancelLock(); |
| 787 | } |
| 788 | |
[email protected] | a8f2115 | 2011-09-08 15:30:02 | [diff] [blame] | 789 | void Compositor::NotifyEnd() { |
[email protected] | d4ae8057 | 2012-06-06 23:02:06 | [diff] [blame] | 790 | last_ended_frame_++; |
[email protected] | a8f2115 | 2011-09-08 15:30:02 | [diff] [blame] | 791 | FOR_EACH_OBSERVER(CompositorObserver, |
| 792 | observer_list_, |
[email protected] | 3ce2feb | 2011-09-19 18:44:23 | [diff] [blame] | 793 | OnCompositingEnded(this)); |
[email protected] | a8f2115 | 2011-09-08 15:30:02 | [diff] [blame] | 794 | } |
| 795 | |
[email protected] | ab662737 | 2012-01-29 21:22:13 | [diff] [blame] | 796 | COMPOSITOR_EXPORT void SetupTestCompositor() { |
| 797 | if (!CommandLine::ForCurrentProcess()->HasSwitch( |
| 798 | switches::kDisableTestCompositor)) { |
[email protected] | 8cbffc6 | 2013-01-23 22:11:09 | [diff] [blame] | 799 | g_test_compositor_enabled = true; |
[email protected] | ab662737 | 2012-01-29 21:22:13 | [diff] [blame] | 800 | } |
[email protected] | 8391a49e | 2012-05-26 06:31:04 | [diff] [blame] | 801 | #if defined(OS_CHROMEOS) |
| 802 | // If the test is running on the chromeos envrionment (such as |
| 803 | // device or vm bots), use the real compositor. |
| 804 | if (base::chromeos::IsRunningOnChromeOS()) |
[email protected] | 8cbffc6 | 2013-01-23 22:11:09 | [diff] [blame] | 805 | g_test_compositor_enabled = false; |
[email protected] | 8391a49e | 2012-05-26 06:31:04 | [diff] [blame] | 806 | #endif |
[email protected] | 40034cf | 2013-03-13 05:05:37 | [diff] [blame] | 807 | ResetImplicitFactory(); |
[email protected] | ab662737 | 2012-01-29 21:22:13 | [diff] [blame] | 808 | } |
| 809 | |
| 810 | COMPOSITOR_EXPORT void DisableTestCompositor() { |
[email protected] | 40034cf | 2013-03-13 05:05:37 | [diff] [blame] | 811 | ResetImplicitFactory(); |
[email protected] | 8cbffc6 | 2013-01-23 22:11:09 | [diff] [blame] | 812 | g_test_compositor_enabled = false; |
[email protected] | 192093059 | 2012-01-11 14:54:48 | [diff] [blame] | 813 | } |
| 814 | |
[email protected] | 894e8fc | 2012-02-24 13:29:50 | [diff] [blame] | 815 | COMPOSITOR_EXPORT bool IsTestCompositorEnabled() { |
[email protected] | 8cbffc6 | 2013-01-23 22:11:09 | [diff] [blame] | 816 | return g_test_compositor_enabled; |
[email protected] | 894e8fc | 2012-02-24 13:29:50 | [diff] [blame] | 817 | } |
| 818 | |
[email protected] | c797cd4 | 2011-03-15 02:18:36 | [diff] [blame] | 819 | } // namespace ui |