Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(14)

Unified Diff: blimp/engine/renderer/scheduler.cc

Issue 2413063002: content/blimp: Set up hooks for enabling LTHRemote in the renderer. (Closed)
Patch Set: proto comment Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: blimp/engine/renderer/scheduler.cc
diff --git a/blimp/engine/renderer/scheduler.cc b/blimp/engine/renderer/scheduler.cc
new file mode 100644
index 0000000000000000000000000000000000000000..d0ac2dd6b917fb672b1cb5acb8882671dc31a428
--- /dev/null
+++ b/blimp/engine/renderer/scheduler.cc
@@ -0,0 +1,93 @@
+// 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 "blimp/engine/renderer/scheduler.h"
+
+#include "base/logging.h"
+
+namespace blimp {
+namespace engine {
+namespace {
+// This is the temporary frame delay to keep pages which make animation requests
+// but don't mutate the state on the engine from running main frames
+// back-to-back. We need smarter throttling of engine updates. See
+// crbug.com/597829.
+constexpr base::TimeDelta kDefaultFrameDelay =
+ base::TimeDelta::FromMilliseconds(30);
+} // namespace
+
+Scheduler::Scheduler(scoped_refptr<base::SingleThreadTaskRunner> task_runner,
+ SchedulerClient* client)
+ : Scheduler(kDefaultFrameDelay, std::move(task_runner), client) {}
+
+Scheduler::Scheduler(base::TimeDelta frame_delay,
+ scoped_refptr<base::SingleThreadTaskRunner> task_runner,
+ SchedulerClient* client)
+ : next_frame_time_(base::TimeTicks::Now()),
+ frame_delay_(frame_delay),
+ client_(client) {
+ DCHECK(client_);
+ frame_tick_timer_.SetTaskRunner(std::move(task_runner));
+}
+
+Scheduler::~Scheduler() = default;
+
+void Scheduler::SetNeedsMainFrame() {
+ if (needs_main_frame_)
+ return;
+
+ needs_main_frame_ = true;
+ ScheduleMainFrameIfNecessary();
+}
+
+void Scheduler::DidSendFrameUpdateToClient() {
+ DCHECK(!frame_ack_pending_) << "We can have only frame update in flight";
+
+ frame_ack_pending_ = true;
+ ScheduleMainFrameIfNecessary();
+}
+
+void Scheduler::DidReceiveFrameUpdateAck() {
+ DCHECK(frame_ack_pending_);
+
+ frame_ack_pending_ = false;
+ ScheduleMainFrameIfNecessary();
+}
+
+void Scheduler::ScheduleMainFrameIfNecessary() {
+ // If we can't produce main frame updates right now, don't schedule a task.
+ if (!CanProduceMainFrames()) {
+ return;
+ }
+
+ // If a task has already been scheduled, we don't need to schedule again.
+ if (frame_tick_timer_.IsRunning())
+ return;
+
+ base::TimeDelta delay = next_frame_time_ - base::TimeTicks::Now();
+ frame_tick_timer_.Start(
+ FROM_HERE, delay,
+ base::Bind(&Scheduler::StartMainFrame, base::Unretained(this)));
+}
+
+bool Scheduler::CanProduceMainFrames() const {
+ return needs_main_frame_ && !frame_ack_pending_;
+}
+
+void Scheduler::StartMainFrame() {
+ DCHECK(needs_main_frame_);
+
+ // If an update was sent to the client since this request, we can not start
+ // another frame. Early out here, we'll come back when an Ack is received from
+ // the client.
+ if (frame_ack_pending_)
+ return;
+
+ needs_main_frame_ = false;
+ next_frame_time_ = base::TimeTicks::Now() + frame_delay_;
+ client_->StartMainFrameUpdate();
+}
+
+} // namespace engine
+} // namespace blimp

Powered by Google App Engine
This is Rietveld 408576698