Flutter Engine
vsync_waiter_fallback.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter 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 "flutter/shell/common/vsync_waiter_fallback.h"
6 
7 #include <memory>
8 
9 #include "flutter/fml/logging.h"
10 #include "flutter/fml/message_loop.h"
11 #include "flutter/fml/trace_event.h"
12 
13 namespace flutter {
14 namespace {
15 
16 static fml::TimePoint SnapToNextTick(fml::TimePoint value,
17  fml::TimePoint tick_phase,
18  fml::TimeDelta tick_interval) {
19  fml::TimeDelta offset = (tick_phase - value) % tick_interval;
20  if (offset != fml::TimeDelta::Zero())
21  offset = offset + tick_interval;
22  return value + offset;
23 }
24 
25 } // namespace
26 
28  bool for_testing)
29  : VsyncWaiter(std::move(task_runners)),
30  phase_(fml::TimePoint::Now()),
31  for_testing_(for_testing) {}
32 
34 
35 // |VsyncWaiter|
36 void VsyncWaiterFallback::AwaitVSync() {
37  TRACE_EVENT0("flutter", "VSYNC");
38 
39  constexpr fml::TimeDelta kSingleFrameInterval =
40  fml::TimeDelta::FromSecondsF(1.0 / 60.0);
41  auto frame_start_time =
42  SnapToNextTick(fml::TimePoint::Now(), phase_, kSingleFrameInterval);
43  auto frame_target_time = frame_start_time + kSingleFrameInterval;
44  std::weak_ptr<VsyncWaiterFallback> weak_this =
45  std::static_pointer_cast<VsyncWaiterFallback>(shared_from_this());
46 
48  [frame_start_time, frame_target_time, weak_this]() {
49  if (auto vsync_waiter = weak_this.lock()) {
50  vsync_waiter->FireCallback(frame_start_time, frame_target_time,
51  !vsync_waiter->for_testing_);
52  }
53  },
54  frame_start_time);
55 }
56 
57 } // namespace flutter
#define TRACE_EVENT0(category_group, name)
Definition: trace_event.h:90
static constexpr TimeDelta Zero()
Definition: time_delta.h:33
static constexpr TimeDelta FromSecondsF(double seconds)
Definition: time_delta.h:53
A |VsyncWaiter| that will fire at 60 fps irrespective of the vsync.
Definition: ref_ptr.h:252
virtual void PostTaskForTime(const fml::closure &task, fml::TimePoint target_time)
Definition: task_runner.cc:28
VsyncWaiterFallback(TaskRunners task_runners, bool for_testing=false)
Definition: ascii_trie.cc:9
uint8_t value
fml::RefPtr< fml::TaskRunner > GetUITaskRunner() const
Definition: task_runners.cc:34
const TaskRunners task_runners_
Definition: vsync_waiter.h:41
static TimePoint Now()
Definition: time_point.cc:39