Index: cc/surfaces/display_begin_frame_source.cc |
diff --git a/cc/surfaces/display_begin_frame_source.cc b/cc/surfaces/display_begin_frame_source.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..73b34a9c7cb5e7d295fce46a6579a9c80b8fe15d |
--- /dev/null |
+++ b/cc/surfaces/display_begin_frame_source.cc |
@@ -0,0 +1,198 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "cc/surfaces/display_begin_frame_source.h" |
+ |
+#include <algorithm> |
+ |
+namespace cc { |
+ |
+DisplayBeginFrameSource::DisplayBeginFrameSource( |
+ std::unique_ptr<BeginFrameSource> wrapped_source) |
+ : wrapped_source_(std::move(wrapped_source)), |
+ observing_begin_frames_(false), |
+ frame_active_(false), |
+ current_source_id_(0), |
+ oldest_incorporated_frame_(0), |
+ client_(nullptr), |
+ client_needs_begin_frames_(false) {} |
+ |
+DisplayBeginFrameSource::~DisplayBeginFrameSource() { |
+ if (wrapped_source_ && observing_begin_frames_) |
+ wrapped_source_->RemoveObserver(this); |
+} |
+ |
+void DisplayBeginFrameSource::SetClient(DisplayBeginFrameSourceClient* client) { |
+ client_ = client; |
+ ObserverStatusChanged(); |
+} |
+ |
+void DisplayBeginFrameSource::SetClientNeedsBeginFrames( |
+ bool needs_begin_frames) { |
+ DCHECK(client_); |
+ |
+ if (client_needs_begin_frames_ == needs_begin_frames) |
+ return; |
+ client_needs_begin_frames_ = needs_begin_frames; |
+ |
+ if (client_needs_begin_frames_ && observing_begin_frames_) { |
+ // We were already observing, but the client may have missed the last |
+ // BeginFrame. |
+ BeginFrameArgs missed_args = |
+ GetMissedArgs(client_->LastUsedBeginFrameArgs()); |
+ if (missed_args.IsValid()) |
+ client_->OnBeginFrame(missed_args); |
+ } |
+ |
+ UpdateObservingBeginFrames(); |
+} |
+ |
+void DisplayBeginFrameSource::FinishClientFrame(bool had_updates, |
+ bool has_pending_updates) { |
+ DCHECK(frame_active_); |
+ frame_active_ = false; |
+ |
+ // Only update the oldest_incorporated_frame if there are no further pending |
+ // updates (i.e. the Display didn't have to draw or drew successfully). |
+ if (!has_pending_updates) |
+ oldest_incorporated_frame_ = ack_tracker_.OldestIncorporatedFrame(); |
+ |
+ BeginFrameAck ack(current_begin_frame_args_.source_id, |
+ current_begin_frame_args_.sequence_number, |
+ oldest_incorporated_frame_, 0, had_updates); |
+ if (wrapped_source_) |
+ wrapped_source_->DidFinishFrame(this, ack); |
+} |
+ |
+bool DisplayBeginFrameSource::HasObservers() const { |
+ return !observers_.empty(); |
+} |
+ |
+bool DisplayBeginFrameSource::AllObserversFinishedFrame() const { |
+ return ack_tracker_.AllObserversFinishedFrame(); |
+} |
+ |
+void DisplayBeginFrameSource::SwapWrappedSource( |
+ std::unique_ptr<BeginFrameSource>* begin_frame_source) { |
+ if (wrapped_source_ && observing_begin_frames_) |
+ wrapped_source_->RemoveObserver(this); |
+ |
+ wrapped_source_.swap(*begin_frame_source); |
+ |
+ if (wrapped_source_ && observing_begin_frames_) |
+ wrapped_source_->AddObserver(this); |
+} |
+ |
+void DisplayBeginFrameSource::DidFinishFrame(BeginFrameObserver* obs, |
+ const BeginFrameAck& ack) { |
+ ack_tracker_.OnObserverFinishedFrame(obs, ack); |
+ ObserverStatusChanged(); |
+} |
+ |
+void DisplayBeginFrameSource::AddObserver(BeginFrameObserver* obs) { |
+ observers_.insert(obs); |
+ ack_tracker_.OnObserverAdded(obs); |
+ |
+ if (observing_begin_frames_) { |
+ // We were already observing, but the observer may have missed the last |
+ // BeginFrame. |
+ BeginFrameArgs missed_args = GetMissedArgs(obs->LastUsedBeginFrameArgs()); |
+ if (missed_args.IsValid()) { |
+ obs->OnBeginFrame(missed_args); |
+ ack_tracker_.OnObserverBeginFrame(obs, missed_args); |
+ } |
+ } |
+ |
+ UpdateObservingBeginFrames(); |
+ ObserverStatusChanged(); |
+} |
+ |
+void DisplayBeginFrameSource::RemoveObserver(BeginFrameObserver* obs) { |
+ observers_.erase(obs); |
+ ack_tracker_.OnObserverRemoved(obs); |
+ |
+ UpdateObservingBeginFrames(); |
+ ObserverStatusChanged(); |
+} |
+ |
+bool DisplayBeginFrameSource::IsThrottled() const { |
+ if (wrapped_source_) |
+ return wrapped_source_->IsThrottled(); |
+ return true; |
+} |
+ |
+void DisplayBeginFrameSource::OnBeginFrame(const BeginFrameArgs& args) { |
+ if (client_ && client_needs_begin_frames_) |
+ client_->OnBeginFrame(args); |
+ |
+ // Reset for the new frame. |
+ frame_active_ = true; |
+ ack_tracker_.OnBeginFrame(args); |
+ |
+ // BeginFrameObserverAckTracker will have updated the oldest incorporated |
+ // frame if the wrapped source changes. |
+ if (args.source_id != current_source_id_) |
+ oldest_incorporated_frame_ = ack_tracker_.OldestIncorporatedFrame(); |
+ |
+ current_begin_frame_args_ = args; |
+ |
+ // Observers may remove themselves during OnBeginFrame(), so use a copy. |
+ std::set<BeginFrameObserver*> observers = observers_; |
+ for (BeginFrameObserver* obs : observers) { |
+ obs->OnBeginFrame(args); |
+ ack_tracker_.OnObserverBeginFrame(obs, args); |
+ } |
+ |
+ ObserverStatusChanged(); |
+} |
+ |
+const BeginFrameArgs& DisplayBeginFrameSource::LastUsedBeginFrameArgs() const { |
+ return current_begin_frame_args_; |
+} |
+ |
+void DisplayBeginFrameSource::OnBeginFrameSourcePausedChanged(bool paused) { |
+ // BeginFrameSources used as wrapped sources do not make use of this feature. |
+ DCHECK(!paused); |
+} |
+ |
+void DisplayBeginFrameSource::UpdateObservingBeginFrames() { |
+ bool should_observe = client_needs_begin_frames_ || !observers_.empty(); |
+ if (should_observe == observing_begin_frames_) |
+ return; |
+ |
+ observing_begin_frames_ = should_observe; |
+ |
+ if (!wrapped_source_) |
+ return; |
+ |
+ if (observing_begin_frames_) { |
+ // We need to start observing, a missed BeginFrame will be triggered by |
+ // the wrapped source and forwarded to the client or observer. |
+ wrapped_source_->AddObserver(this); |
+ } else { |
+ wrapped_source_->RemoveObserver(this); |
+ } |
+} |
+ |
+BeginFrameArgs DisplayBeginFrameSource::GetMissedArgs( |
+ const BeginFrameArgs& last_args) const { |
+ if (!current_begin_frame_args_.IsValid() || !wrapped_source_ || |
+ !frame_active_) |
+ return BeginFrameArgs(); |
+ |
+ if (last_args.IsValid() && |
+ (last_args.sequence_number >= current_begin_frame_args_.sequence_number)) |
+ return BeginFrameArgs(); |
+ |
+ BeginFrameArgs missed_args = current_begin_frame_args_; |
+ missed_args.type = BeginFrameArgs::MISSED; |
+ return missed_args; |
+} |
+ |
+void DisplayBeginFrameSource::ObserverStatusChanged() { |
+ if (client_) |
+ client_->BeginFrameObserverStatusChanged(); |
+} |
+ |
+} // namespace cc |