Index: cc/surfaces/display_scheduler.cc |
diff --git a/cc/surfaces/display_scheduler.cc b/cc/surfaces/display_scheduler.cc |
index 40e80bf97bd36034d435bbe34c3b4242295cccd4..b775a11324d4dfb484c98728447e9832933bce6e 100644 |
--- a/cc/surfaces/display_scheduler.cc |
+++ b/cc/surfaces/display_scheduler.cc |
@@ -10,6 +10,8 @@ |
#include "base/stl_util.h" |
#include "base/trace_event/trace_event.h" |
#include "cc/output/output_surface.h" |
+#include "cc/surfaces/surface_info.h" |
+#include "cc/surfaces/surface_manager.h" |
namespace cc { |
@@ -17,6 +19,7 @@ DisplayScheduler::DisplayScheduler(base::SingleThreadTaskRunner* task_runner, |
int max_pending_swaps) |
: client_(nullptr), |
begin_frame_source_(nullptr), |
+ surface_manager_(nullptr), |
task_runner_(task_runner), |
inside_surface_damaged_(false), |
visible_(false), |
@@ -25,13 +28,11 @@ DisplayScheduler::DisplayScheduler(base::SingleThreadTaskRunner* task_runner, |
inside_begin_frame_deadline_interval_(false), |
needs_draw_(false), |
expecting_root_surface_damage_because_of_resize_(false), |
- all_active_child_surfaces_ready_to_draw_(false), |
+ has_pending_surfaces_(false), |
next_swap_id_(1), |
pending_swaps_(0), |
max_pending_swaps_(max_pending_swaps), |
observing_begin_frame_source_(false), |
- root_surface_damaged_(false), |
- expect_damage_from_root_surface_(false), |
weak_ptr_factory_(this) { |
begin_frame_deadline_closure_ = base::Bind( |
&DisplayScheduler::OnBeginFrameDeadline, weak_ptr_factory_.GetWeakPtr()); |
@@ -50,6 +51,10 @@ void DisplayScheduler::SetBeginFrameSource( |
begin_frame_source_ = begin_frame_source; |
} |
+void DisplayScheduler::SetSurfaceManager(SurfaceManager* surface_manager) { |
+ surface_manager_ = surface_manager; |
+} |
+ |
void DisplayScheduler::SetVisible(bool visible) { |
if (visible_ == visible) |
return; |
@@ -81,7 +86,6 @@ void DisplayScheduler::ForceImmediateSwapIfPossible() { |
void DisplayScheduler::DisplayResized() { |
expecting_root_surface_damage_because_of_resize_ = true; |
- expect_damage_from_root_surface_ = true; |
needs_draw_ = true; |
ScheduleBeginFrameDeadline(); |
} |
@@ -108,21 +112,105 @@ void DisplayScheduler::SurfaceDamaged(const SurfaceId& surface_id) { |
needs_draw_ = true; |
- if (surface_id == root_surface_id_) { |
- root_surface_damaged_ = true; |
+ if (surface_id == root_surface_id_) |
expecting_root_surface_damage_because_of_resize_ = false; |
- } else { |
- child_surface_ids_damaged_.insert(surface_id); |
- |
- // TODO(mithro): Use hints from SetNeedsBeginFrames and SwapAborts. |
- all_active_child_surfaces_ready_to_draw_ = base::STLIncludes( |
- child_surface_ids_damaged_, child_surface_ids_to_expect_damage_from_); |
- } |
StartObservingBeginFrames(); |
ScheduleBeginFrameDeadline(); |
} |
+void DisplayScheduler::SurfaceCreated(const SurfaceInfo& surface_info) { |
+ SurfaceId surface_id = surface_info.id(); |
+ DCHECK(!base::ContainsKey(surface_states_, surface_id)); |
+ surface_states_[surface_id] = SurfaceBeginFrameState(); |
+} |
+ |
+void DisplayScheduler::SurfaceDestroyed(const SurfaceId& surface_id) { |
+ auto it = surface_states_.find(surface_id); |
+ if (it == surface_states_.end()) |
+ return; |
+ surface_states_.erase(it); |
+} |
+ |
+void DisplayScheduler::SurfaceReceivedBeginFrame(const SurfaceId& surface_id, |
+ const BeginFrameArgs& args) { |
+ TRACE_EVENT1("cc", "DisplayScheduler::SurfaceReceivedBeginFrame", |
+ "surface_id", surface_id.ToString()); |
+ auto it = surface_states_.find(surface_id); |
+ if (it == surface_states_.end()) |
+ return; |
+ it->second.last_args = args; |
+ if (UpdateHasPendingSurfaces(surface_id)) |
+ ScheduleBeginFrameDeadline(); |
+} |
+ |
+void DisplayScheduler::SurfaceFinishedBeginFrame(const SurfaceId& surface_id, |
+ const BeginFrameAck& ack) { |
+ TRACE_EVENT1("cc", "DisplayScheduler::SurfaceFinishedBeginFrame", |
+ "surface_id", surface_id.ToString()); |
+ auto it = surface_states_.find(surface_id); |
+ if (it == surface_states_.end()) |
+ return; |
+ it->second.last_ack = ack; |
+ if (UpdateHasPendingSurfaces(surface_id)) |
+ ScheduleBeginFrameDeadline(); |
+} |
+ |
+bool DisplayScheduler::UpdateHasPendingSurfaces( |
+ const SurfaceId& changed_surface_id) { |
+ if (!client_ || !surface_manager_ || |
+ !base::ContainsKey(client_->GetPreviousContainedSurfaces(), |
+ changed_surface_id)) { |
+ return false; |
+ } |
+ |
+ bool old_value = has_pending_surfaces_; |
+ |
+ // We only need to check for the state of previously referenced surfaced, as |
+ // newly referenced child surfaces will already have damage. (An exception to |
+ // this are surfaces referenced by a CompositorFrame that is activated because |
+ // of a surface synchronization deadline. By activating the parent frame, |
+ // however, we decided not to wait for these child surfaces.) |
+ for (const std::pair<SurfaceId, int>& entry : |
+ client_->GetPreviousContainedSurfaces()) { |
+ const SurfaceId& surface_id = entry.first; |
+ auto it = surface_states_.find(surface_id); |
+ |
+ // Surface is ready if it was destroyed or doesn't exist yet (see above). |
+ if (it == surface_states_.end()) |
+ continue; |
+ |
+ const SurfaceBeginFrameState& state = it->second; |
+ |
+ // Surface is ready if it has acknowledged the last BeginFrame it |
+ // received or hasn't ever received a BeginFrame. |
+ if (!state.last_args.IsValid() || |
+ (state.last_args.source_id == state.last_ack.source_id && |
+ state.last_args.sequence_number == state.last_ack.sequence_number)) { |
+ continue; |
+ } |
+ |
+ // Surface is ready if there is an undrawn (active or pending) |
+ // CompositorFrame, because its producer is CompositorFrameAck throttled. |
+ Surface* surface = surface_manager_->GetSurfaceForId(entry.first); |
+ DCHECK(surface); |
+ if (surface->HasUndrawnFrame()) |
+ continue; |
+ |
+ has_pending_surfaces_ = true; |
+ TRACE_EVENT_INSTANT2("cc", "DisplayScheduler::UpdateHasPendingSurfaces", |
+ TRACE_EVENT_SCOPE_THREAD, "has_pending_surfaces", |
+ has_pending_surfaces_, "pending_surface_id", |
+ surface_id.ToString()); |
+ return has_pending_surfaces_ != old_value; |
+ } |
+ has_pending_surfaces_ = false; |
+ TRACE_EVENT_INSTANT1("cc", "DisplayScheduler::UpdateHasPendingSurfaces", |
+ TRACE_EVENT_SCOPE_THREAD, "has_pending_surfaces", |
+ has_pending_surfaces_); |
+ return has_pending_surfaces_ != old_value; |
+} |
+ |
void DisplayScheduler::OutputSurfaceLost() { |
TRACE_EVENT0("cc", "DisplayScheduler::OutputSurfaceLost"); |
output_surface_lost_ = true; |
@@ -138,19 +226,7 @@ bool DisplayScheduler::DrawAndSwap() { |
if (!success) |
return false; |
- child_surface_ids_to_expect_damage_from_ = |
- base::STLSetIntersection<std::vector<SurfaceId>>( |
- child_surface_ids_damaged_, child_surface_ids_damaged_prev_); |
- |
- child_surface_ids_damaged_prev_.swap(child_surface_ids_damaged_); |
- child_surface_ids_damaged_.clear(); |
- |
needs_draw_ = false; |
- all_active_child_surfaces_ready_to_draw_ = |
- child_surface_ids_to_expect_damage_from_.empty(); |
- |
- expect_damage_from_root_surface_ = root_surface_damaged_; |
- root_surface_damaged_ = false; |
return true; |
} |
@@ -243,7 +319,9 @@ base::TimeTicks DisplayScheduler::DesiredBeginFrameDeadlineTime() { |
current_begin_frame_args_.interval; |
} |
- if (!needs_draw_) { |
+ bool all_surfaces_ready = |
+ !has_pending_surfaces_ && root_surface_id_.is_valid(); |
+ if (!needs_draw_ && !all_surfaces_ready) { |
TRACE_EVENT_INSTANT0("cc", "No damage yet", TRACE_EVENT_SCOPE_THREAD); |
return current_begin_frame_args_.frame_time + |
current_begin_frame_args_.interval; |
@@ -256,10 +334,7 @@ base::TimeTicks DisplayScheduler::DesiredBeginFrameDeadlineTime() { |
current_begin_frame_args_.interval; |
} |
- bool root_ready_to_draw = |
- !expect_damage_from_root_surface_ || root_surface_damaged_; |
- |
- if (all_active_child_surfaces_ready_to_draw_ && root_ready_to_draw) { |
+ if (all_surfaces_ready && !expecting_root_surface_damage_because_of_resize_) { |
TRACE_EVENT_INSTANT0("cc", "All active surfaces ready", |
TRACE_EVENT_SCOPE_THREAD); |
return base::TimeTicks(); |
@@ -273,27 +348,6 @@ base::TimeTicks DisplayScheduler::DesiredBeginFrameDeadlineTime() { |
current_begin_frame_args_.interval; |
} |
- // Use an earlier deadline if we are only waiting for the root surface |
- // in case our expect_damage_from_root_surface heuristic is incorrect. |
- // TODO(mithro): Replace this with SetNeedsBeginFrame and SwapAbort |
- // logic. |
- if (all_active_child_surfaces_ready_to_draw_ && |
- expect_damage_from_root_surface_) { |
- TRACE_EVENT_INSTANT0("cc", "Waiting for damage from root surface", |
- TRACE_EVENT_SCOPE_THREAD); |
- // This adjusts the deadline by DefaultEstimatedParentDrawTime for |
- // a second time. The first one represented the Surfaces draw to display |
- // latency. This one represents root surface commit+raster+draw latency. |
- // We treat the root surface differently since it lives on the same thread |
- // as Surfaces and waiting for it too long may push out the Surfaces draw. |
- // If we also assume the root surface is fast to start a commit after the |
- // beginning of a frame, it'll have a chance to lock its resources, which |
- // will cause us to wait for it to unlock its resources above. |
- // TODO(mithro): Replace hard coded estimates. |
- return current_begin_frame_args_.deadline - |
- BeginFrameArgs::DefaultEstimatedParentDrawTime(); |
- } |
- |
TRACE_EVENT_INSTANT0("cc", "More damage expected soon", |
TRACE_EVENT_SCOPE_THREAD); |
return current_begin_frame_args_.deadline; |
@@ -344,11 +398,9 @@ bool DisplayScheduler::AttemptDrawAndSwap() { |
return DrawAndSwap(); |
} else { |
// We are going idle, so reset expectations. |
- child_surface_ids_to_expect_damage_from_.clear(); |
- child_surface_ids_damaged_prev_.clear(); |
- child_surface_ids_damaged_.clear(); |
- all_active_child_surfaces_ready_to_draw_ = true; |
- expect_damage_from_root_surface_ = false; |
+ // TODO(eseckler): Should we avoid going idle if |
+ // |expecting_root_surface_damage_because_of_resize_| is true? |
+ expecting_root_surface_damage_because_of_resize_ = false; |
StopObservingBeginFrames(); |
} |