| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "cc/surfaces/display_scheduler.h" | 5 #include "cc/surfaces/display_scheduler.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/auto_reset.h" | 9 #include "base/auto_reset.h" |
| 10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| 11 #include "base/trace_event/trace_event.h" | 11 #include "base/trace_event/trace_event.h" |
| 12 #include "cc/output/output_surface.h" | 12 #include "cc/output/output_surface.h" |
| 13 | 13 |
| 14 namespace cc { | 14 namespace cc { |
| 15 | 15 |
| 16 DisplayScheduler::DisplayScheduler(BeginFrameSource* begin_frame_source, | 16 DisplayScheduler::DisplayScheduler(BeginFrameSource* begin_frame_source, |
| 17 base::SingleThreadTaskRunner* task_runner, | 17 base::SingleThreadTaskRunner* task_runner, |
| 18 int max_pending_swaps) | 18 int max_pending_swaps) |
| 19 : begin_frame_source_(begin_frame_source), | 19 : begin_frame_source_(begin_frame_source), |
| 20 task_runner_(task_runner), | 20 task_runner_(task_runner), |
| 21 inside_surface_damaged_(false), | 21 inside_surface_damaged_(false), |
| 22 visible_(false), |
| 22 output_surface_lost_(false), | 23 output_surface_lost_(false), |
| 23 root_surface_resources_locked_(true), | 24 root_surface_resources_locked_(true), |
| 24 inside_begin_frame_deadline_interval_(false), | 25 inside_begin_frame_deadline_interval_(false), |
| 25 needs_draw_(false), | 26 needs_draw_(false), |
| 26 expecting_root_surface_damage_because_of_resize_(false), | 27 expecting_root_surface_damage_because_of_resize_(false), |
| 27 all_active_child_surfaces_ready_to_draw_(false), | 28 all_active_child_surfaces_ready_to_draw_(false), |
| 28 pending_swaps_(0), | 29 pending_swaps_(0), |
| 29 max_pending_swaps_(max_pending_swaps), | 30 max_pending_swaps_(max_pending_swaps), |
| 30 observing_begin_frame_source_(false), | 31 observing_begin_frame_source_(false), |
| 31 root_surface_damaged_(false), | 32 root_surface_damaged_(false), |
| 32 expect_damage_from_root_surface_(false), | 33 expect_damage_from_root_surface_(false), |
| 33 weak_ptr_factory_(this) { | 34 weak_ptr_factory_(this) { |
| 34 begin_frame_deadline_closure_ = base::Bind( | 35 begin_frame_deadline_closure_ = base::Bind( |
| 35 &DisplayScheduler::OnBeginFrameDeadline, weak_ptr_factory_.GetWeakPtr()); | 36 &DisplayScheduler::OnBeginFrameDeadline, weak_ptr_factory_.GetWeakPtr()); |
| 36 } | 37 } |
| 37 | 38 |
| 38 DisplayScheduler::~DisplayScheduler() { | 39 DisplayScheduler::~DisplayScheduler() { |
| 39 if (observing_begin_frame_source_) | 40 StopObservingBeginFrames(); |
| 40 begin_frame_source_->RemoveObserver(this); | |
| 41 } | 41 } |
| 42 | 42 |
| 43 void DisplayScheduler::SetClient(DisplaySchedulerClient* client) { | 43 void DisplayScheduler::SetClient(DisplaySchedulerClient* client) { |
| 44 client_ = client; | 44 client_ = client; |
| 45 } | 45 } |
| 46 | 46 |
| 47 void DisplayScheduler::SetVisible(bool visible) { |
| 48 if (visible_ == visible) |
| 49 return; |
| 50 |
| 51 visible_ = visible; |
| 52 // If going invisible, we'll stop observing begin frames once we try |
| 53 // to draw and fail. |
| 54 StartObservingBeginFrames(); |
| 55 ScheduleBeginFrameDeadline(); |
| 56 } |
| 57 |
| 47 // If we try to draw when the root surface resources are locked, the | 58 // If we try to draw when the root surface resources are locked, the |
| 48 // draw will fail. | 59 // draw will fail. |
| 49 void DisplayScheduler::SetRootSurfaceResourcesLocked(bool locked) { | 60 void DisplayScheduler::SetRootSurfaceResourcesLocked(bool locked) { |
| 50 TRACE_EVENT1("cc", "DisplayScheduler::SetRootSurfaceResourcesLocked", | 61 TRACE_EVENT1("cc", "DisplayScheduler::SetRootSurfaceResourcesLocked", |
| 51 "locked", locked); | 62 "locked", locked); |
| 52 root_surface_resources_locked_ = locked; | 63 root_surface_resources_locked_ = locked; |
| 53 ScheduleBeginFrameDeadline(); | 64 ScheduleBeginFrameDeadline(); |
| 54 } | 65 } |
| 55 | 66 |
| 56 // This is used to force an immediate swap before a resize. | 67 // This is used to force an immediate swap before a resize. |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 root_surface_damaged_ = true; | 106 root_surface_damaged_ = true; |
| 96 expecting_root_surface_damage_because_of_resize_ = false; | 107 expecting_root_surface_damage_because_of_resize_ = false; |
| 97 } else { | 108 } else { |
| 98 child_surface_ids_damaged_.insert(surface_id); | 109 child_surface_ids_damaged_.insert(surface_id); |
| 99 | 110 |
| 100 // TODO(mithro): Use hints from SetNeedsBeginFrames and SwapAborts. | 111 // TODO(mithro): Use hints from SetNeedsBeginFrames and SwapAborts. |
| 101 all_active_child_surfaces_ready_to_draw_ = base::STLIncludes( | 112 all_active_child_surfaces_ready_to_draw_ = base::STLIncludes( |
| 102 child_surface_ids_damaged_, child_surface_ids_to_expect_damage_from_); | 113 child_surface_ids_damaged_, child_surface_ids_to_expect_damage_from_); |
| 103 } | 114 } |
| 104 | 115 |
| 105 if (!output_surface_lost_ && !observing_begin_frame_source_) { | 116 StartObservingBeginFrames(); |
| 106 observing_begin_frame_source_ = true; | |
| 107 begin_frame_source_->AddObserver(this); | |
| 108 } | |
| 109 | |
| 110 ScheduleBeginFrameDeadline(); | 117 ScheduleBeginFrameDeadline(); |
| 111 } | 118 } |
| 112 | 119 |
| 113 void DisplayScheduler::OutputSurfaceLost() { | 120 void DisplayScheduler::OutputSurfaceLost() { |
| 114 TRACE_EVENT0("cc", "DisplayScheduler::OutputSurfaceLost"); | 121 TRACE_EVENT0("cc", "DisplayScheduler::OutputSurfaceLost"); |
| 115 output_surface_lost_ = true; | 122 output_surface_lost_ = true; |
| 116 ScheduleBeginFrameDeadline(); | 123 ScheduleBeginFrameDeadline(); |
| 117 } | 124 } |
| 118 | 125 |
| 119 void DisplayScheduler::DrawAndSwap() { | 126 void DisplayScheduler::DrawAndSwap() { |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 | 183 |
| 177 // If we get another BeginFrame before a posted missed frame, just drop the | 184 // If we get another BeginFrame before a posted missed frame, just drop the |
| 178 // missed frame. Also if this was the missed frame, drop the Callback inside | 185 // missed frame. Also if this was the missed frame, drop the Callback inside |
| 179 // it. Do this last because this might be the missed frame and we don't want | 186 // it. Do this last because this might be the missed frame and we don't want |
| 180 // to destroy |args| prematurely. | 187 // to destroy |args| prematurely. |
| 181 missed_begin_frame_task_.Cancel(); | 188 missed_begin_frame_task_.Cancel(); |
| 182 | 189 |
| 183 return true; | 190 return true; |
| 184 } | 191 } |
| 185 | 192 |
| 193 void DisplayScheduler::StartObservingBeginFrames() { |
| 194 if (!observing_begin_frame_source_ && ShouldDraw()) { |
| 195 begin_frame_source_->AddObserver(this); |
| 196 observing_begin_frame_source_ = true; |
| 197 } |
| 198 } |
| 199 |
| 200 void DisplayScheduler::StopObservingBeginFrames() { |
| 201 if (observing_begin_frame_source_) { |
| 202 begin_frame_source_->RemoveObserver(this); |
| 203 observing_begin_frame_source_ = false; |
| 204 |
| 205 // A missed BeginFrame may be queued, so drop that too if we're going to |
| 206 // stop listening. |
| 207 missed_begin_frame_task_.Cancel(); |
| 208 } |
| 209 } |
| 210 |
| 211 bool DisplayScheduler::ShouldDraw() { |
| 212 // Note: When any of these cases becomes true, StartObservingBeginFrames must |
| 213 // be called to ensure the draw will happen. |
| 214 return needs_draw_ && !output_surface_lost_ && visible_; |
| 215 } |
| 216 |
| 186 void DisplayScheduler::OnBeginFrameSourcePausedChanged(bool paused) { | 217 void DisplayScheduler::OnBeginFrameSourcePausedChanged(bool paused) { |
| 187 // BeginFrameSources used with DisplayScheduler do not make use of this | 218 // BeginFrameSources used with DisplayScheduler do not make use of this |
| 188 // feature. | 219 // feature. |
| 189 if (paused) | 220 if (paused) |
| 190 NOTIMPLEMENTED(); | 221 NOTIMPLEMENTED(); |
| 191 } | 222 } |
| 192 | 223 |
| 193 base::TimeTicks DisplayScheduler::DesiredBeginFrameDeadlineTime() { | 224 base::TimeTicks DisplayScheduler::DesiredBeginFrameDeadlineTime() { |
| 194 if (output_surface_lost_) { | 225 if (output_surface_lost_) { |
| 195 TRACE_EVENT_INSTANT0("cc", "Lost output surface", TRACE_EVENT_SCOPE_THREAD); | 226 TRACE_EVENT_INSTANT0("cc", "Lost output surface", TRACE_EVENT_SCOPE_THREAD); |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 291 begin_frame_deadline_task_.callback(), delta); | 322 begin_frame_deadline_task_.callback(), delta); |
| 292 TRACE_EVENT2("cc", "Using new deadline", "delta", delta.ToInternalValue(), | 323 TRACE_EVENT2("cc", "Using new deadline", "delta", delta.ToInternalValue(), |
| 293 "desired_deadline", desired_deadline); | 324 "desired_deadline", desired_deadline); |
| 294 } | 325 } |
| 295 | 326 |
| 296 void DisplayScheduler::AttemptDrawAndSwap() { | 327 void DisplayScheduler::AttemptDrawAndSwap() { |
| 297 inside_begin_frame_deadline_interval_ = false; | 328 inside_begin_frame_deadline_interval_ = false; |
| 298 begin_frame_deadline_task_.Cancel(); | 329 begin_frame_deadline_task_.Cancel(); |
| 299 begin_frame_deadline_task_time_ = base::TimeTicks(); | 330 begin_frame_deadline_task_time_ = base::TimeTicks(); |
| 300 | 331 |
| 301 if (needs_draw_ && !output_surface_lost_) { | 332 if (ShouldDraw()) { |
| 302 if (pending_swaps_ < max_pending_swaps_ && !root_surface_resources_locked_) | 333 if (pending_swaps_ < max_pending_swaps_ && !root_surface_resources_locked_) |
| 303 DrawAndSwap(); | 334 DrawAndSwap(); |
| 304 } else { | 335 } else { |
| 305 // We are going idle, so reset expectations. | 336 // We are going idle, so reset expectations. |
| 306 child_surface_ids_to_expect_damage_from_.clear(); | 337 child_surface_ids_to_expect_damage_from_.clear(); |
| 307 child_surface_ids_damaged_prev_.clear(); | 338 child_surface_ids_damaged_prev_.clear(); |
| 308 child_surface_ids_damaged_.clear(); | 339 child_surface_ids_damaged_.clear(); |
| 309 all_active_child_surfaces_ready_to_draw_ = true; | 340 all_active_child_surfaces_ready_to_draw_ = true; |
| 310 expect_damage_from_root_surface_ = false; | 341 expect_damage_from_root_surface_ = false; |
| 311 | 342 |
| 312 if (observing_begin_frame_source_) { | 343 StopObservingBeginFrames(); |
| 313 observing_begin_frame_source_ = false; | |
| 314 begin_frame_source_->RemoveObserver(this); | |
| 315 // A missed BeginFrame may be queued, so drop that too if we're going to | |
| 316 // stop listening. | |
| 317 missed_begin_frame_task_.Cancel(); | |
| 318 } | |
| 319 } | 344 } |
| 320 } | 345 } |
| 321 | 346 |
| 322 void DisplayScheduler::OnBeginFrameDeadline() { | 347 void DisplayScheduler::OnBeginFrameDeadline() { |
| 323 TRACE_EVENT0("cc", "DisplayScheduler::OnBeginFrameDeadline"); | 348 TRACE_EVENT0("cc", "DisplayScheduler::OnBeginFrameDeadline"); |
| 324 | 349 |
| 325 AttemptDrawAndSwap(); | 350 AttemptDrawAndSwap(); |
| 326 begin_frame_source_->DidFinishFrame(this, 0); | 351 begin_frame_source_->DidFinishFrame(this, 0); |
| 327 } | 352 } |
| 328 | 353 |
| 329 void DisplayScheduler::DidSwapBuffers() { | 354 void DisplayScheduler::DidSwapBuffers() { |
| 330 pending_swaps_++; | 355 pending_swaps_++; |
| 331 TRACE_EVENT_ASYNC_BEGIN1("cc", "DisplayScheduler:pending_swaps", this, | 356 TRACE_EVENT_ASYNC_BEGIN1("cc", "DisplayScheduler:pending_swaps", this, |
| 332 "pending_frames", pending_swaps_); | 357 "pending_frames", pending_swaps_); |
| 333 } | 358 } |
| 334 | 359 |
| 335 void DisplayScheduler::DidSwapBuffersComplete() { | 360 void DisplayScheduler::DidSwapBuffersComplete() { |
| 336 pending_swaps_--; | 361 pending_swaps_--; |
| 337 TRACE_EVENT_ASYNC_END1("cc", "DisplayScheduler:pending_swaps", this, | 362 TRACE_EVENT_ASYNC_END1("cc", "DisplayScheduler:pending_swaps", this, |
| 338 "pending_frames", pending_swaps_); | 363 "pending_frames", pending_swaps_); |
| 339 ScheduleBeginFrameDeadline(); | 364 ScheduleBeginFrameDeadline(); |
| 340 } | 365 } |
| 341 | 366 |
| 342 } // namespace cc | 367 } // namespace cc |
| OLD | NEW |