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