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_ && needs_draw_ && !output_surface_lost_ && | |
195 visible_) { | |
196 begin_frame_source_->AddObserver(this); | |
197 observing_begin_frame_source_ = true; | |
198 } | |
199 } | |
200 | |
201 void DisplayScheduler::StopObservingBeginFrames() { | |
202 if (observing_begin_frame_source_) { | |
203 begin_frame_source_->RemoveObserver(this); | |
204 observing_begin_frame_source_ = false; | |
205 | |
206 // A missed BeginFrame may be queued, so drop that too if we're going to | |
207 // stop listening. | |
208 missed_begin_frame_task_.Cancel(); | |
209 } | |
210 } | |
211 | |
186 void DisplayScheduler::OnBeginFrameSourcePausedChanged(bool paused) { | 212 void DisplayScheduler::OnBeginFrameSourcePausedChanged(bool paused) { |
187 // BeginFrameSources used with DisplayScheduler do not make use of this | 213 // BeginFrameSources used with DisplayScheduler do not make use of this |
188 // feature. | 214 // feature. |
189 if (paused) | 215 if (paused) |
190 NOTIMPLEMENTED(); | 216 NOTIMPLEMENTED(); |
191 } | 217 } |
192 | 218 |
193 base::TimeTicks DisplayScheduler::DesiredBeginFrameDeadlineTime() { | 219 base::TimeTicks DisplayScheduler::DesiredBeginFrameDeadlineTime() { |
194 if (output_surface_lost_) { | 220 if (output_surface_lost_) { |
195 TRACE_EVENT_INSTANT0("cc", "Lost output surface", TRACE_EVENT_SCOPE_THREAD); | 221 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); | 317 begin_frame_deadline_task_.callback(), delta); |
292 TRACE_EVENT2("cc", "Using new deadline", "delta", delta.ToInternalValue(), | 318 TRACE_EVENT2("cc", "Using new deadline", "delta", delta.ToInternalValue(), |
293 "desired_deadline", desired_deadline); | 319 "desired_deadline", desired_deadline); |
294 } | 320 } |
295 | 321 |
296 void DisplayScheduler::AttemptDrawAndSwap() { | 322 void DisplayScheduler::AttemptDrawAndSwap() { |
297 inside_begin_frame_deadline_interval_ = false; | 323 inside_begin_frame_deadline_interval_ = false; |
298 begin_frame_deadline_task_.Cancel(); | 324 begin_frame_deadline_task_.Cancel(); |
299 begin_frame_deadline_task_time_ = base::TimeTicks(); | 325 begin_frame_deadline_task_time_ = base::TimeTicks(); |
300 | 326 |
301 if (needs_draw_ && !output_surface_lost_) { | 327 if (needs_draw_ && !output_surface_lost_ && visible_) { |
sunnyps
2016/08/16 20:18:13
nit: Can we extract needs_draw_ && !output_surface
danakj
2016/08/16 20:33:06
Done.
| |
302 if (pending_swaps_ < max_pending_swaps_ && !root_surface_resources_locked_) | 328 if (pending_swaps_ < max_pending_swaps_ && !root_surface_resources_locked_) |
303 DrawAndSwap(); | 329 DrawAndSwap(); |
304 } else { | 330 } else { |
305 // We are going idle, so reset expectations. | 331 // We are going idle, so reset expectations. |
306 child_surface_ids_to_expect_damage_from_.clear(); | 332 child_surface_ids_to_expect_damage_from_.clear(); |
307 child_surface_ids_damaged_prev_.clear(); | 333 child_surface_ids_damaged_prev_.clear(); |
308 child_surface_ids_damaged_.clear(); | 334 child_surface_ids_damaged_.clear(); |
309 all_active_child_surfaces_ready_to_draw_ = true; | 335 all_active_child_surfaces_ready_to_draw_ = true; |
310 expect_damage_from_root_surface_ = false; | 336 expect_damage_from_root_surface_ = false; |
311 | 337 |
312 if (observing_begin_frame_source_) { | 338 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 } | 339 } |
320 } | 340 } |
321 | 341 |
322 void DisplayScheduler::OnBeginFrameDeadline() { | 342 void DisplayScheduler::OnBeginFrameDeadline() { |
323 TRACE_EVENT0("cc", "DisplayScheduler::OnBeginFrameDeadline"); | 343 TRACE_EVENT0("cc", "DisplayScheduler::OnBeginFrameDeadline"); |
324 | 344 |
325 AttemptDrawAndSwap(); | 345 AttemptDrawAndSwap(); |
326 begin_frame_source_->DidFinishFrame(this, 0); | 346 begin_frame_source_->DidFinishFrame(this, 0); |
327 } | 347 } |
328 | 348 |
329 void DisplayScheduler::DidSwapBuffers() { | 349 void DisplayScheduler::DidSwapBuffers() { |
330 pending_swaps_++; | 350 pending_swaps_++; |
331 TRACE_EVENT_ASYNC_BEGIN1("cc", "DisplayScheduler:pending_swaps", this, | 351 TRACE_EVENT_ASYNC_BEGIN1("cc", "DisplayScheduler:pending_swaps", this, |
332 "pending_frames", pending_swaps_); | 352 "pending_frames", pending_swaps_); |
333 } | 353 } |
334 | 354 |
335 void DisplayScheduler::DidSwapBuffersComplete() { | 355 void DisplayScheduler::DidSwapBuffersComplete() { |
336 pending_swaps_--; | 356 pending_swaps_--; |
337 TRACE_EVENT_ASYNC_END1("cc", "DisplayScheduler:pending_swaps", this, | 357 TRACE_EVENT_ASYNC_END1("cc", "DisplayScheduler:pending_swaps", this, |
338 "pending_frames", pending_swaps_); | 358 "pending_frames", pending_swaps_); |
339 ScheduleBeginFrameDeadline(); | 359 ScheduleBeginFrameDeadline(); |
340 } | 360 } |
341 | 361 |
342 } // namespace cc | 362 } // namespace cc |
OLD | NEW |