Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(19)

Side by Side Diff: ui/compositor/compositor.cc

Issue 134623005: Make SingleThreadProxy a SchedulerClient (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix typo Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/compositor/compositor.h" 5 #include "ui/compositor/compositor.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <deque> 8 #include <deque>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 21 matching lines...) Expand all
32 #include "ui/compositor/layer.h" 32 #include "ui/compositor/layer.h"
33 #include "ui/gfx/frame_time.h" 33 #include "ui/gfx/frame_time.h"
34 #include "ui/gl/gl_context.h" 34 #include "ui/gl/gl_context.h"
35 #include "ui/gl/gl_switches.h" 35 #include "ui/gl/gl_switches.h"
36 36
37 namespace { 37 namespace {
38 38
39 const double kDefaultRefreshRate = 60.0; 39 const double kDefaultRefreshRate = 60.0;
40 const double kTestRefreshRate = 200.0; 40 const double kTestRefreshRate = 200.0;
41 41
42 enum SwapType {
43 DRAW_SWAP,
44 };
45
46 bool g_compositor_initialized = false; 42 bool g_compositor_initialized = false;
47 base::Thread* g_compositor_thread = NULL; 43 base::Thread* g_compositor_thread = NULL;
48 44
49 ui::ContextFactory* g_context_factory = NULL; 45 ui::ContextFactory* g_context_factory = NULL;
50 46
51 const int kCompositorLockTimeoutMs = 67; 47 const int kCompositorLockTimeoutMs = 67;
52 48
53 class PendingSwap {
54 public:
55 PendingSwap(SwapType type, ui::PostedSwapQueue* posted_swaps);
56 ~PendingSwap();
57
58 SwapType type() const { return type_; }
59 bool posted() const { return posted_; }
60
61 private:
62 friend class ui::PostedSwapQueue;
63
64 SwapType type_;
65 bool posted_;
66 ui::PostedSwapQueue* posted_swaps_;
67
68 DISALLOW_COPY_AND_ASSIGN(PendingSwap);
69 };
70
71 } // namespace 49 } // namespace
72 50
73 namespace ui { 51 namespace ui {
74 52
75 // static 53 // static
76 ContextFactory* ContextFactory::GetInstance() { 54 ContextFactory* ContextFactory::GetInstance() {
77 DCHECK(g_context_factory); 55 DCHECK(g_context_factory);
78 return g_context_factory; 56 return g_context_factory;
79 } 57 }
80 58
(...skipping 27 matching lines...) Expand all
108 CancelLock(); 86 CancelLock();
109 } 87 }
110 88
111 void CompositorLock::CancelLock() { 89 void CompositorLock::CancelLock() {
112 if (!compositor_) 90 if (!compositor_)
113 return; 91 return;
114 compositor_->UnlockCompositor(); 92 compositor_->UnlockCompositor();
115 compositor_ = NULL; 93 compositor_ = NULL;
116 } 94 }
117 95
118 class PostedSwapQueue {
119 public:
120 PostedSwapQueue() : pending_swap_(NULL) {
121 }
122
123 ~PostedSwapQueue() {
124 DCHECK(!pending_swap_);
125 }
126
127 SwapType NextPostedSwap() const {
128 return queue_.front();
129 }
130
131 bool AreSwapsPosted() const {
132 return !queue_.empty();
133 }
134
135 int NumSwapsPosted(SwapType type) const {
136 int count = 0;
137 for (std::deque<SwapType>::const_iterator it = queue_.begin();
138 it != queue_.end(); ++it) {
139 if (*it == type)
140 count++;
141 }
142 return count;
143 }
144
145 void PostSwap() {
146 DCHECK(pending_swap_);
147 queue_.push_back(pending_swap_->type());
148 pending_swap_->posted_ = true;
149 }
150
151 void EndSwap() {
152 queue_.pop_front();
153 }
154
155 private:
156 friend class ::PendingSwap;
157
158 PendingSwap* pending_swap_;
159 std::deque<SwapType> queue_;
160
161 DISALLOW_COPY_AND_ASSIGN(PostedSwapQueue);
162 };
163
164 } // namespace ui 96 } // namespace ui
165 97
166 namespace {
167
168 PendingSwap::PendingSwap(SwapType type, ui::PostedSwapQueue* posted_swaps)
169 : type_(type), posted_(false), posted_swaps_(posted_swaps) {
170 // Only one pending swap in flight.
171 DCHECK_EQ(static_cast<PendingSwap*>(NULL), posted_swaps_->pending_swap_);
172 posted_swaps_->pending_swap_ = this;
173 }
174
175 PendingSwap::~PendingSwap() {
176 DCHECK_EQ(this, posted_swaps_->pending_swap_);
177 posted_swaps_->pending_swap_ = NULL;
178 }
179
180 } // namespace
181
182 namespace ui { 98 namespace ui {
183 99
184 Compositor::Compositor(gfx::AcceleratedWidget widget) 100 Compositor::Compositor(gfx::AcceleratedWidget widget)
185 : root_layer_(NULL), 101 : root_layer_(NULL),
186 widget_(widget), 102 widget_(widget),
187 vsync_manager_(new CompositorVSyncManager()), 103 vsync_manager_(new CompositorVSyncManager()),
188 posted_swaps_(new PostedSwapQueue()),
189 device_scale_factor_(0.0f), 104 device_scale_factor_(0.0f),
190 last_started_frame_(0), 105 last_started_frame_(0),
191 last_ended_frame_(0), 106 last_ended_frame_(0),
192 next_draw_is_resize_(false),
193 disable_schedule_composite_(false), 107 disable_schedule_composite_(false),
194 compositor_lock_(NULL), 108 compositor_lock_(NULL),
195 defer_draw_scheduling_(false),
196 waiting_on_compositing_end_(false),
197 draw_on_compositing_end_(false),
198 schedule_draw_factory_(this) { 109 schedule_draw_factory_(this) {
199 DCHECK(g_compositor_initialized) 110 DCHECK(g_compositor_initialized)
200 << "Compositor::Initialize must be called before creating a Compositor."; 111 << "Compositor::Initialize must be called before creating a Compositor.";
201 112
202 root_web_layer_ = cc::Layer::Create(); 113 root_web_layer_ = cc::Layer::Create();
203 root_web_layer_->SetAnchorPoint(gfx::PointF(0.f, 0.f)); 114 root_web_layer_->SetAnchorPoint(gfx::PointF());
204 115
205 CommandLine* command_line = CommandLine::ForCurrentProcess(); 116 CommandLine* command_line = CommandLine::ForCurrentProcess();
206 117
207 cc::LayerTreeSettings settings; 118 cc::LayerTreeSettings settings;
208 settings.refresh_rate = 119 settings.refresh_rate =
209 ContextFactory::GetInstance()->DoesCreateTestContexts() 120 ContextFactory::GetInstance()->DoesCreateTestContexts()
210 ? kTestRefreshRate 121 ? kTestRefreshRate
211 : kDefaultRefreshRate; 122 : kDefaultRefreshRate;
212 settings.deadline_scheduling_enabled = 123 settings.deadline_scheduling_enabled =
213 switches::IsUIDeadlineSchedulingEnabled(); 124 switches::IsUIDeadlineSchedulingEnabled();
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
271 182
272 ContextFactory::GetInstance()->RemoveCompositor(this); 183 ContextFactory::GetInstance()->RemoveCompositor(this);
273 } 184 }
274 185
275 // static 186 // static
276 void Compositor::Initialize() { 187 void Compositor::Initialize() {
277 #if defined(OS_CHROMEOS) 188 #if defined(OS_CHROMEOS)
278 bool use_thread = !CommandLine::ForCurrentProcess()->HasSwitch( 189 bool use_thread = !CommandLine::ForCurrentProcess()->HasSwitch(
279 switches::kUIDisableThreadedCompositing); 190 switches::kUIDisableThreadedCompositing);
280 #else 191 #else
281 bool use_thread = 192 bool use_thread =
danakj 2014/02/19 20:42:39 A nice followup would be to remove these switches
enne (OOO) 2014/02/20 00:35:44 Hmm! Who is using the single-threaded browser comp
danakj 2014/02/20 19:30:55 Everyone except ChromeOS ya.
282 CommandLine::ForCurrentProcess()->HasSwitch( 193 CommandLine::ForCurrentProcess()->HasSwitch(
283 switches::kUIEnableThreadedCompositing) && 194 switches::kUIEnableThreadedCompositing) &&
284 !CommandLine::ForCurrentProcess()->HasSwitch( 195 !CommandLine::ForCurrentProcess()->HasSwitch(
285 switches::kUIDisableThreadedCompositing); 196 switches::kUIDisableThreadedCompositing);
286 #endif 197 #endif
287 if (use_thread) { 198 if (use_thread) {
288 g_compositor_thread = new base::Thread("Browser Compositor"); 199 g_compositor_thread = new base::Thread("Browser Compositor");
289 g_compositor_thread->Start(); 200 g_compositor_thread->Start();
290 } 201 }
291 202
(...skipping 20 matching lines...) Expand all
312 if (g_compositor_thread) { 223 if (g_compositor_thread) {
313 g_compositor_thread->Stop(); 224 g_compositor_thread->Stop();
314 delete g_compositor_thread; 225 delete g_compositor_thread;
315 g_compositor_thread = NULL; 226 g_compositor_thread = NULL;
316 } 227 }
317 228
318 DCHECK(g_compositor_initialized) << "Compositor::Initialize() didn't happen."; 229 DCHECK(g_compositor_initialized) << "Compositor::Initialize() didn't happen.";
319 g_compositor_initialized = false; 230 g_compositor_initialized = false;
320 } 231 }
321 232
322 void Compositor::ScheduleDraw() { 233 void Compositor::ScheduleDraw() { host_->SetNeedsCommit(); }
323 if (g_compositor_thread) {
324 host_->Composite(gfx::FrameTime::Now());
325 } else if (!defer_draw_scheduling_) {
326 defer_draw_scheduling_ = true;
327 base::MessageLoop::current()->PostTask(
328 FROM_HERE,
329 base::Bind(&Compositor::Draw, schedule_draw_factory_.GetWeakPtr()));
330 }
331 }
332 234
333 void Compositor::SetRootLayer(Layer* root_layer) { 235 void Compositor::SetRootLayer(Layer* root_layer) {
334 if (root_layer_ == root_layer) 236 if (root_layer_ == root_layer)
335 return; 237 return;
336 if (root_layer_) 238 if (root_layer_)
337 root_layer_->SetCompositor(NULL); 239 root_layer_->SetCompositor(NULL);
338 root_layer_ = root_layer; 240 root_layer_ = root_layer;
339 if (root_layer_ && !root_layer_->GetCompositor()) 241 if (root_layer_ && !root_layer_->GetCompositor())
340 root_layer_->SetCompositor(this); 242 root_layer_->SetCompositor(this);
341 root_web_layer_->RemoveAllChildren(); 243 root_web_layer_->RemoveAllChildren();
342 if (root_layer_) 244 if (root_layer_)
343 root_web_layer_->AddChild(root_layer_->cc_layer()); 245 root_web_layer_->AddChild(root_layer_->cc_layer());
344 } 246 }
345 247
346 void Compositor::SetHostHasTransparentBackground( 248 void Compositor::SetHostHasTransparentBackground(
347 bool host_has_transparent_background) { 249 bool host_has_transparent_background) {
348 host_->set_has_transparent_background(host_has_transparent_background); 250 host_->set_has_transparent_background(host_has_transparent_background);
349 } 251 }
350 252
351 void Compositor::Draw() {
352 DCHECK(!g_compositor_thread);
353
354 defer_draw_scheduling_ = false;
355 if (waiting_on_compositing_end_) {
356 draw_on_compositing_end_ = true;
357 return;
358 }
359 waiting_on_compositing_end_ = true;
360
361 TRACE_EVENT_ASYNC_BEGIN0("ui", "Compositor::Draw", last_started_frame_ + 1);
362
363 if (!root_layer_)
364 return;
365
366 last_started_frame_++;
367 PendingSwap pending_swap(DRAW_SWAP, posted_swaps_.get());
368 if (!IsLocked()) {
369 // TODO(nduca): Temporary while compositor calls
370 // compositeImmediately() directly.
371 Layout();
372 host_->Composite(gfx::FrameTime::Now());
373
374 #if defined(OS_WIN)
375 // While we resize, we are usually a few frames behind. By blocking
376 // the UI thread here we minize the area that is mis-painted, specially
377 // in the non-client area. See RenderWidgetHostViewAura::SetBounds for
378 // more details and bug 177115.
379 if (next_draw_is_resize_ && (last_ended_frame_ > 1)) {
380 next_draw_is_resize_ = false;
381 host_->FinishAllRendering();
382 }
383 #endif
384
385 }
386 if (!pending_swap.posted())
387 NotifyEnd();
388 }
389
390 void Compositor::ScheduleFullRedraw() { 253 void Compositor::ScheduleFullRedraw() {
391 host_->SetNeedsRedraw(); 254 host_->SetNeedsRedraw();
392 } 255 }
393 256
394 void Compositor::ScheduleRedrawRect(const gfx::Rect& damage_rect) { 257 void Compositor::ScheduleRedrawRect(const gfx::Rect& damage_rect) {
395 host_->SetNeedsRedrawRect(damage_rect); 258 host_->SetNeedsRedrawRect(damage_rect);
396 } 259 }
397 260
398 void Compositor::SetLatencyInfo(const ui::LatencyInfo& latency_info) { 261 void Compositor::SetLatencyInfo(const ui::LatencyInfo& latency_info) {
399 scoped_ptr<cc::SwapPromise> swap_promise( 262 scoped_ptr<cc::SwapPromise> swap_promise(
400 new cc::LatencyInfoSwapPromise(latency_info)); 263 new cc::LatencyInfoSwapPromise(latency_info));
401 host_->QueueSwapPromise(swap_promise.Pass()); 264 host_->QueueSwapPromise(swap_promise.Pass());
402 } 265 }
403 266
404 void Compositor::SetScaleAndSize(float scale, const gfx::Size& size_in_pixel) { 267 void Compositor::SetScaleAndSize(float scale, const gfx::Size& size_in_pixel) {
405 DCHECK_GT(scale, 0); 268 DCHECK_GT(scale, 0);
406 if (!size_in_pixel.IsEmpty()) { 269 if (!size_in_pixel.IsEmpty()) {
407 size_ = size_in_pixel; 270 size_ = size_in_pixel;
408 host_->SetViewportSize(size_in_pixel); 271 host_->SetViewportSize(size_in_pixel);
409 root_web_layer_->SetBounds(size_in_pixel); 272 root_web_layer_->SetBounds(size_in_pixel);
410
411 next_draw_is_resize_ = true;
412 } 273 }
413 if (device_scale_factor_ != scale) { 274 if (device_scale_factor_ != scale) {
414 device_scale_factor_ = scale; 275 device_scale_factor_ = scale;
415 if (root_layer_) 276 if (root_layer_)
416 root_layer_->OnDeviceScaleFactorChanged(scale); 277 root_layer_->OnDeviceScaleFactorChanged(scale);
417 } 278 }
418 } 279 }
419 280
420 void Compositor::SetBackgroundColor(SkColor color) { 281 void Compositor::SetBackgroundColor(SkColor color) {
421 host_->set_background_color(color); 282 host_->set_background_color(color);
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
458 OnCompositingDidCommit(this)); 319 OnCompositingDidCommit(this));
459 } 320 }
460 321
461 void Compositor::DidCommitAndDrawFrame() { 322 void Compositor::DidCommitAndDrawFrame() {
462 base::TimeTicks start_time = gfx::FrameTime::Now(); 323 base::TimeTicks start_time = gfx::FrameTime::Now();
463 FOR_EACH_OBSERVER(CompositorObserver, 324 FOR_EACH_OBSERVER(CompositorObserver,
464 observer_list_, 325 observer_list_,
465 OnCompositingStarted(this, start_time)); 326 OnCompositingStarted(this, start_time));
466 } 327 }
467 328
468 void Compositor::DidCompleteSwapBuffers() { 329 void Compositor::DidCompleteSwapBuffers() { NotifyEnd(); }
469 if (g_compositor_thread) {
470 NotifyEnd();
471 } else {
472 DCHECK(posted_swaps_->AreSwapsPosted());
473 DCHECK_GE(1, posted_swaps_->NumSwapsPosted(DRAW_SWAP));
474 if (posted_swaps_->NextPostedSwap() == DRAW_SWAP)
475 NotifyEnd();
476 posted_swaps_->EndSwap();
477 }
478 }
479 330
480 scoped_refptr<cc::ContextProvider> Compositor::OffscreenContextProvider() { 331 scoped_refptr<cc::ContextProvider> Compositor::OffscreenContextProvider() {
481 return ContextFactory::GetInstance()->OffscreenCompositorContextProvider(); 332 return ContextFactory::GetInstance()->OffscreenCompositorContextProvider();
482 } 333 }
483 334
484 void Compositor::ScheduleComposite() { 335 void Compositor::ScheduleComposite() {
485 if (!disable_schedule_composite_) 336 if (!disable_schedule_composite_)
486 ScheduleDraw(); 337 ScheduleDraw();
487 } 338 }
488 339
489 void Compositor::ScheduleAnimation() { 340 void Compositor::ScheduleAnimation() {
490 ScheduleComposite(); 341 ScheduleComposite();
491 } 342 }
492 343
493 void Compositor::DidPostSwapBuffers() { 344 void Compositor::DidPostSwapBuffers() {
494 DCHECK(!g_compositor_thread);
495 posted_swaps_->PostSwap();
496 } 345 }
497 346
498 void Compositor::DidAbortSwapBuffers() { 347 void Compositor::DidAbortSwapBuffers() {
499 if (!g_compositor_thread) {
500 DCHECK_GE(1, posted_swaps_->NumSwapsPosted(DRAW_SWAP));
501
502 // We've just lost the context, so unwind all posted_swaps.
503 while (posted_swaps_->AreSwapsPosted()) {
504 if (posted_swaps_->NextPostedSwap() == DRAW_SWAP)
505 NotifyEnd();
506 posted_swaps_->EndSwap();
507 }
508 }
509
510 FOR_EACH_OBSERVER(CompositorObserver, 348 FOR_EACH_OBSERVER(CompositorObserver,
511 observer_list_, 349 observer_list_,
512 OnCompositingAborted(this)); 350 OnCompositingAborted(this));
513 } 351 }
514 352
515 const cc::LayerTreeDebugState& Compositor::GetLayerTreeDebugState() const { 353 const cc::LayerTreeDebugState& Compositor::GetLayerTreeDebugState() const {
516 return host_->debug_state(); 354 return host_->debug_state();
517 } 355 }
518 356
519 void Compositor::SetLayerTreeDebugState( 357 void Compositor::SetLayerTreeDebugState(
520 const cc::LayerTreeDebugState& debug_state) { 358 const cc::LayerTreeDebugState& debug_state) {
521 host_->SetDebugState(debug_state); 359 host_->SetDebugState(debug_state);
522 } 360 }
523 361
524 scoped_refptr<CompositorLock> Compositor::GetCompositorLock() { 362 scoped_refptr<CompositorLock> Compositor::GetCompositorLock() {
525 if (!compositor_lock_) { 363 if (!compositor_lock_) {
526 compositor_lock_ = new CompositorLock(this); 364 compositor_lock_ = new CompositorLock(this);
527 if (g_compositor_thread) 365 host_->SetDeferCommits(true);
528 host_->SetDeferCommits(true);
529 FOR_EACH_OBSERVER(CompositorObserver, 366 FOR_EACH_OBSERVER(CompositorObserver,
530 observer_list_, 367 observer_list_,
531 OnCompositingLockStateChanged(this)); 368 OnCompositingLockStateChanged(this));
532 } 369 }
533 return compositor_lock_; 370 return compositor_lock_;
534 } 371 }
535 372
536 void Compositor::UnlockCompositor() { 373 void Compositor::UnlockCompositor() {
537 DCHECK(compositor_lock_); 374 DCHECK(compositor_lock_);
538 compositor_lock_ = NULL; 375 compositor_lock_ = NULL;
539 if (g_compositor_thread) 376 host_->SetDeferCommits(false);
540 host_->SetDeferCommits(false);
541 FOR_EACH_OBSERVER(CompositorObserver, 377 FOR_EACH_OBSERVER(CompositorObserver,
542 observer_list_, 378 observer_list_,
543 OnCompositingLockStateChanged(this)); 379 OnCompositingLockStateChanged(this));
544 } 380 }
545 381
546 void Compositor::CancelCompositorLock() { 382 void Compositor::CancelCompositorLock() {
547 if (compositor_lock_) 383 if (compositor_lock_)
548 compositor_lock_->CancelLock(); 384 compositor_lock_->CancelLock();
549 } 385 }
550 386
551 void Compositor::NotifyEnd() { 387 void Compositor::NotifyEnd() {
552 last_ended_frame_++; 388 last_ended_frame_++;
danakj 2014/02/19 20:42:39 Can we move this stuff up to the one callside of t
enne (OOO) 2014/02/20 00:35:44 Done.
553 TRACE_EVENT_ASYNC_END0("ui", "Compositor::Draw", last_ended_frame_); 389 TRACE_EVENT_ASYNC_END0("ui", "Compositor::Draw", last_ended_frame_);
554 waiting_on_compositing_end_ = false;
555 if (draw_on_compositing_end_) {
556 draw_on_compositing_end_ = false;
557
558 // Call ScheduleDraw() instead of Draw() in order to allow other
559 // CompositorObservers to be notified before starting another
560 // draw cycle.
561 ScheduleDraw();
562 }
563 FOR_EACH_OBSERVER(CompositorObserver, 390 FOR_EACH_OBSERVER(CompositorObserver,
564 observer_list_, 391 observer_list_,
565 OnCompositingEnded(this)); 392 OnCompositingEnded(this));
566 } 393 }
567 394
568 } // namespace ui 395 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698