Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "cc/surfaces/display_scheduler.h" | |
| 6 | |
| 7 #include <vector> | |
| 8 | |
| 9 #include "base/stl_util.h" | |
| 10 #include "base/trace_event/trace_event.h" | |
| 11 #include "cc/output/output_surface.h" | |
| 12 #include "ui/gfx/frame_time.h" | |
| 13 | |
| 14 namespace cc { | |
| 15 | |
| 16 DisplayScheduler::DisplayScheduler( | |
| 17 DisplaySchedulerClient* client, | |
| 18 BeginFrameSource* begin_frame_source, | |
| 19 scoped_refptr<base::SingleThreadTaskRunner> task_runner, | |
| 20 int max_pending_swaps) | |
| 21 : client_(client), | |
| 22 begin_frame_source_(begin_frame_source), | |
| 23 task_runner_(task_runner), | |
| 24 output_surface_lost_(false), | |
| 25 resources_locked_by_browser_(true), | |
| 26 inside_begin_frame_deadline_interval_(false), | |
| 27 needs_draw_(false), | |
| 28 entire_display_damaged_(false), | |
| 29 all_active_surfaces_ready_to_draw_(false), | |
| 30 pending_swaps_(0), | |
| 31 max_pending_swaps_(max_pending_swaps), | |
| 32 browser_damaged_(false), | |
| 33 expect_damage_from_browser_(false), | |
| 34 weak_ptr_factory_(this) { | |
| 35 begin_frame_source_->AddObserver(this); | |
| 36 begin_frame_deadline_closure_ = base::Bind( | |
| 37 &DisplayScheduler::OnBeginFrameDeadline, weak_ptr_factory_.GetWeakPtr()); | |
| 38 } | |
| 39 | |
| 40 DisplayScheduler::~DisplayScheduler() { | |
| 41 begin_frame_source_->RemoveObserver(this); | |
| 42 } | |
| 43 | |
| 44 // If we try to draw when the Browser has locked it's resources, the | |
| 45 // draw will fail. | |
| 46 void DisplayScheduler::SetResourcesLockedByBrowser(bool locked) { | |
| 47 resources_locked_by_browser_ = locked; | |
| 48 ScheduleBeginFrameDeadline(); | |
| 49 } | |
| 50 | |
| 51 // Notification that there was a resize or the root surface changed and | |
| 52 // that we should just draw immediately. | |
| 53 void DisplayScheduler::EntireDisplayDamaged(SurfaceId browser_surface_id) { | |
| 54 TRACE_EVENT0("cc", "DisplayScheduler::EntireDisplayDamaged"); | |
| 55 needs_draw_ = true; | |
| 56 entire_display_damaged_ = true; | |
| 57 browser_surface_id_ = browser_surface_id; | |
| 58 | |
| 59 begin_frame_source_->SetNeedsBeginFrames(!output_surface_lost_); | |
| 60 ScheduleBeginFrameDeadline(); | |
| 61 } | |
| 62 | |
| 63 // Indicates that there was damage to one of the surfaces. | |
| 64 // Has some logic to wait for multiple active surfaces before | |
| 65 // triggering the deadline. | |
| 66 void DisplayScheduler::SurfaceDamaged(SurfaceId surface_id) { | |
| 67 TRACE_EVENT1("cc", "DisplayScheduler::SurfaceDamaged", "surface_id", | |
| 68 surface_id.id); | |
| 69 | |
| 70 needs_draw_ = true; | |
| 71 | |
| 72 if (surface_id == browser_surface_id_) { | |
| 73 browser_damaged_ = true; | |
|
jbauman
2015/05/02 01:20:03
Could this cause all_active_surfaces_ready_to_draw
brianderson
2015/05/02 03:28:37
I will add a unit test to verify, but it should be
| |
| 74 } else { | |
| 75 surface_ids_damaged_.insert(surface_id); | |
| 76 | |
| 77 // TODO(mithro): Use hints from SetNeedsBeginFrames and SwapAborts. | |
| 78 all_active_surfaces_ready_to_draw_ = base::STLIncludes( | |
| 79 surface_ids_damaged_, surface_ids_to_expect_damage_from_); | |
| 80 } | |
| 81 | |
| 82 begin_frame_source_->SetNeedsBeginFrames(!output_surface_lost_); | |
| 83 ScheduleBeginFrameDeadline(); | |
| 84 } | |
| 85 | |
| 86 void DisplayScheduler::OutputSurfaceLost() { | |
| 87 TRACE_EVENT0("cc", "DisplayScheduler::OutputSurfaceLost"); | |
| 88 output_surface_lost_ = true; | |
| 89 begin_frame_source_->SetNeedsBeginFrames(false); | |
| 90 ScheduleBeginFrameDeadline(); | |
| 91 } | |
| 92 | |
| 93 void DisplayScheduler::DrawAndSwap() { | |
| 94 TRACE_EVENT0("cc", "DisplayScheduler::DrawAndSwap"); | |
| 95 DCHECK_LT(pending_swaps_, max_pending_swaps_); | |
| 96 DCHECK(!output_surface_lost_); | |
| 97 | |
| 98 bool success = client_->DrawAndSwap(); | |
| 99 if (!success) | |
| 100 return; | |
| 101 | |
| 102 needs_draw_ = false; | |
| 103 entire_display_damaged_ = false; | |
| 104 all_active_surfaces_ready_to_draw_ = false; | |
| 105 | |
| 106 expect_damage_from_browser_ = browser_damaged_; | |
| 107 browser_damaged_ = false; | |
| 108 | |
| 109 surface_ids_to_expect_damage_from_ = | |
| 110 base::STLSetIntersection<std::vector<SurfaceId>>( | |
| 111 surface_ids_damaged_, surface_ids_damaged_prev_); | |
| 112 | |
| 113 surface_ids_damaged_prev_.swap(surface_ids_damaged_); | |
| 114 surface_ids_damaged_.clear(); | |
| 115 } | |
| 116 | |
| 117 bool DisplayScheduler::OnBeginFrameMixInDelegate(const BeginFrameArgs& args) { | |
| 118 base::TimeTicks now = gfx::FrameTime::Now(); | |
| 119 TRACE_EVENT2("cc", "DisplayScheduler::BeginFrame", "args", args.AsValue(), | |
| 120 "now", now); | |
| 121 | |
| 122 // Only service missed BeginFrames if they haven't already expired. | |
| 123 base::TimeTicks adjusted_deadline = | |
| 124 args.deadline - BeginFrameArgs::DefaultEstimatedParentDrawTime(); | |
| 125 if (args.type == BeginFrameArgs::MISSED && now > adjusted_deadline) | |
| 126 return false; | |
| 127 | |
| 128 // If we get another BeginFrame before the previous deadline, | |
| 129 // synchronously trigger the previous deadline before progressing. | |
| 130 if (inside_begin_frame_deadline_interval_) { | |
| 131 OnBeginFrameDeadline(); | |
| 132 } | |
| 133 | |
| 134 // Schedule the deadline. | |
| 135 current_begin_frame_args_ = args; | |
| 136 current_begin_frame_args_.deadline = adjusted_deadline; | |
| 137 inside_begin_frame_deadline_interval_ = true; | |
| 138 ScheduleBeginFrameDeadline(); | |
| 139 | |
| 140 return true; | |
| 141 } | |
| 142 | |
| 143 base::TimeTicks DisplayScheduler::DesiredBeginFrameDeadlineTime() { | |
| 144 if (output_surface_lost_) { | |
| 145 TRACE_EVENT_INSTANT0("cc", "Lost output surface", TRACE_EVENT_SCOPE_THREAD); | |
| 146 return base::TimeTicks(); | |
| 147 } | |
| 148 | |
| 149 if (pending_swaps_ >= max_pending_swaps_) { | |
| 150 TRACE_EVENT_INSTANT0("cc", "Swap throttled", TRACE_EVENT_SCOPE_THREAD); | |
| 151 return current_begin_frame_args_.deadline; | |
| 152 } | |
| 153 | |
| 154 if (!needs_draw_) { | |
| 155 TRACE_EVENT_INSTANT0("cc", "No damage yet", TRACE_EVENT_SCOPE_THREAD); | |
| 156 return current_begin_frame_args_.frame_time + | |
| 157 current_begin_frame_args_.interval; | |
| 158 } | |
| 159 | |
| 160 if (resources_locked_by_browser_) { | |
| 161 TRACE_EVENT_INSTANT0("cc", "Resources locked by Browser", | |
| 162 TRACE_EVENT_SCOPE_THREAD); | |
| 163 return current_begin_frame_args_.frame_time + | |
| 164 current_begin_frame_args_.interval; | |
| 165 } | |
| 166 | |
| 167 // TODO(mithro): Be smarter about resize deadlines. | |
| 168 if (entire_display_damaged_) { | |
| 169 TRACE_EVENT_INSTANT0("cc", "Entire display damaged", | |
| 170 TRACE_EVENT_SCOPE_THREAD); | |
| 171 return base::TimeTicks(); | |
| 172 } | |
| 173 | |
| 174 bool browser_ready_to_draw = !expect_damage_from_browser_ || browser_damaged_; | |
| 175 | |
| 176 if (all_active_surfaces_ready_to_draw_ && browser_ready_to_draw) { | |
| 177 TRACE_EVENT_INSTANT0("cc", "All active surfaces ready", | |
| 178 TRACE_EVENT_SCOPE_THREAD); | |
| 179 return base::TimeTicks(); | |
| 180 } | |
| 181 | |
| 182 // Use an earlier deadline if we are only waiting for the browser | |
| 183 // in case our expect_damage_from_browser heuristic is incorrect. | |
| 184 // TODO(mithro): Replace this with SetNeedsBeginFrame and SwapAbort | |
| 185 // logic. | |
| 186 if (all_active_surfaces_ready_to_draw_ && expect_damage_from_browser_) { | |
| 187 TRACE_EVENT_INSTANT0("cc", "Waiting for damage from Browser", | |
| 188 TRACE_EVENT_SCOPE_THREAD); | |
| 189 return current_begin_frame_args_.deadline - | |
| 190 BeginFrameArgs::DefaultEstimatedParentDrawTime(); | |
|
jbauman
2015/05/02 01:20:03
Would all_active_surfaces_ready_to_draw_ be true i
brianderson
2015/05/02 03:28:37
As coded, it will. I need to rename all_active_sur
| |
| 191 } | |
| 192 | |
| 193 TRACE_EVENT_INSTANT0("cc", "More damage expected soon", | |
| 194 TRACE_EVENT_SCOPE_THREAD); | |
| 195 return current_begin_frame_args_.deadline; | |
| 196 } | |
| 197 | |
| 198 void DisplayScheduler::ScheduleBeginFrameDeadline() { | |
| 199 TRACE_EVENT0("cc", "DisplayScheduler::ScheduleBeginFrameDeadline"); | |
| 200 | |
| 201 // We need to wait for the next BeginFrame before scheduling a deadline. | |
| 202 if (!inside_begin_frame_deadline_interval_) { | |
| 203 TRACE_EVENT_INSTANT0("cc", "Waiting for next BeginFrame", | |
| 204 TRACE_EVENT_SCOPE_THREAD); | |
| 205 DCHECK(begin_frame_deadline_task_.IsCancelled()); | |
| 206 return; | |
| 207 } | |
| 208 | |
| 209 // Determine the deadline we want to use. | |
| 210 base::TimeTicks desired_deadline = DesiredBeginFrameDeadlineTime(); | |
| 211 | |
| 212 // Avoid re-scheduling the deadline if it's already correctly scheduled. | |
| 213 if (!begin_frame_deadline_task_.IsCancelled() && | |
| 214 desired_deadline == begin_frame_deadline_task_time_) { | |
| 215 TRACE_EVENT_INSTANT0("cc", "Using existing deadline", | |
| 216 TRACE_EVENT_SCOPE_THREAD); | |
| 217 return; | |
| 218 } | |
| 219 | |
| 220 // Schedule the deadline. | |
| 221 begin_frame_deadline_task_time_ = desired_deadline; | |
| 222 begin_frame_deadline_task_.Cancel(); | |
| 223 begin_frame_deadline_task_.Reset(begin_frame_deadline_closure_); | |
| 224 | |
| 225 base::TimeDelta delta = | |
| 226 std::max(base::TimeDelta(), desired_deadline - gfx::FrameTime::Now()); | |
| 227 task_runner_->PostDelayedTask(FROM_HERE, | |
| 228 begin_frame_deadline_task_.callback(), delta); | |
| 229 TRACE_EVENT2("cc", "Using new deadline", "delta", delta.ToInternalValue(), | |
| 230 "desired_deadline", desired_deadline); | |
| 231 } | |
| 232 | |
| 233 void DisplayScheduler::OnBeginFrameDeadline() { | |
| 234 TRACE_EVENT0("cc", "DisplayScheduler::OnBeginFrameDeadline"); | |
| 235 inside_begin_frame_deadline_interval_ = false; | |
| 236 begin_frame_deadline_task_.Cancel(); | |
| 237 begin_frame_deadline_task_time_ = base::TimeTicks(); | |
| 238 | |
| 239 if (needs_draw_ && !output_surface_lost_) { | |
| 240 if (pending_swaps_ < max_pending_swaps_ && !resources_locked_by_browser_) | |
| 241 DrawAndSwap(); | |
| 242 } else { | |
| 243 begin_frame_source_->SetNeedsBeginFrames(false); | |
| 244 } | |
| 245 | |
| 246 begin_frame_source_->DidFinishFrame(0); | |
| 247 } | |
| 248 | |
| 249 void DisplayScheduler::DidSwapBuffers() { | |
| 250 pending_swaps_++; | |
| 251 TRACE_EVENT1("cc", "DisplayScheduler::DidSwapBuffers", "pending_frames", | |
| 252 pending_swaps_); | |
| 253 } | |
| 254 | |
| 255 void DisplayScheduler::DidSwapBuffersComplete() { | |
| 256 pending_swaps_--; | |
| 257 TRACE_EVENT1("cc", "DisplayScheduler::DidSwapBuffersComplete", | |
| 258 "pending_frames", pending_swaps_); | |
| 259 ScheduleBeginFrameDeadline(); | |
| 260 } | |
| 261 | |
| 262 } // namespace cc | |
| OLD | NEW |