Flutter Engine
flutter_runner::VsyncWaiter Class Referencefinal

#include <vsync_waiter.h>

Inheritance diagram for flutter_runner::VsyncWaiter:
flutter::VsyncWaiter

Public Member Functions

 VsyncWaiter (std::string debug_label, zx_handle_t session_present_handle, flutter::TaskRunners task_runners, fml::TimeDelta vsync_offset)
 
 ~VsyncWaiter () override
 
- Public Member Functions inherited from flutter::VsyncWaiter
void AsyncWaitForVsync (const Callback &callback)
 
void ScheduleSecondaryCallback (const fml::closure &callback)
 

Static Public Member Functions

static fml::TimePoint SnapToNextPhase (const fml::TimePoint now, const fml::TimePoint last_frame_presentation_time, const fml::TimeDelta presentation_interval)
 

Static Public Attributes

static constexpr zx_signals_t SessionPresentSignal = ZX_EVENT_SIGNALED
 

Additional Inherited Members

- Public Types inherited from flutter::VsyncWaiter
using Callback = std::function< void(fml::TimePoint frame_start_time, fml::TimePoint frame_target_time)>
 
- Protected Member Functions inherited from flutter::VsyncWaiter
 VsyncWaiter (TaskRunners task_runners)
 
void FireCallback (fml::TimePoint frame_start_time, fml::TimePoint frame_target_time)
 
- Protected Attributes inherited from flutter::VsyncWaiter
const TaskRunners task_runners_
 

Detailed Description

Definition at line 19 of file vsync_waiter.h.

Constructor & Destructor Documentation

◆ VsyncWaiter()

flutter_runner::VsyncWaiter::VsyncWaiter ( std::string  debug_label,
zx_handle_t  session_present_handle,
flutter::TaskRunners  task_runners,
fml::TimeDelta  vsync_offset 
)

Definition at line 22 of file vsync_waiter.cc.

References FML_LOG, fml::TimeDelta::FromSeconds(), flutter::TaskRunners::GetUITaskRunner(), fml::MakeCopyable(), fml::TaskRunner::RunNowOrPostTask(), flutter::VsyncWaiter::task_runners_, fml::TimeDelta::ToMicroseconds(), and fml::TimeDelta::ToMilliseconds().

26  : flutter::VsyncWaiter(task_runners),
27  debug_label_(std::move(debug_label)),
28  session_wait_(session_present_handle, SessionPresentSignal),
29  vsync_offset_(vsync_offset),
30  weak_factory_ui_(nullptr),
31  weak_factory_(this) {
32  auto wait_handler = [&](async_dispatcher_t* dispatcher, //
33  async::Wait* wait, //
34  zx_status_t status, //
35  const zx_packet_signal_t* signal //
36  ) {
37  if (status != ZX_OK) {
38  FML_LOG(ERROR) << "Vsync wait failed.";
39  return;
40  }
41 
42  wait->Cancel();
43 
44  FireCallbackNow();
45  };
46 
47  // Generate a WeakPtrFactory for use with the UI thread. This does not need
48  // to wait on a latch because we only ever use the WeakPtrFactory on the UI
49  // thread so we have ordering guarantees (see ::AwaitVSync())
51  task_runners_.GetUITaskRunner(), fml::MakeCopyable([this]() mutable {
52  this->weak_factory_ui_ =
53  std::make_unique<fml::WeakPtrFactory<VsyncWaiter>>(this);
54  }));
55  session_wait_.set_handler(wait_handler);
56 
57  if (vsync_offset_ >= fml::TimeDelta::FromSeconds(1)) {
58  FML_LOG(WARNING) << "Given vsync_offset is extremely high: "
59  << vsync_offset_.ToMilliseconds() << "ms";
60  } else {
61  FML_LOG(INFO) << "Set vsync_offset to " << vsync_offset_.ToMicroseconds()
62  << "us";
63  }
64 }
static constexpr TimeDelta FromSeconds(int64_t seconds)
Definition: time_delta.h:49
constexpr int64_t ToMilliseconds() const
Definition: time_delta.h:63
static void RunNowOrPostTask(fml::RefPtr< fml::TaskRunner > runner, const fml::closure &task)
Definition: task_runner.cc:55
#define FML_LOG(severity)
Definition: logging.h:65
internal::CopyableLambda< T > MakeCopyable(T lambda)
Definition: make_copyable.h:57
constexpr int64_t ToMicroseconds() const
Definition: time_delta.h:62
fml::RefPtr< fml::TaskRunner > GetUITaskRunner() const
Definition: task_runners.cc:34
const TaskRunners task_runners_
Definition: vsync_waiter.h:39
static constexpr zx_signals_t SessionPresentSignal
Definition: vsync_waiter.h:21

◆ ~VsyncWaiter()

flutter_runner::VsyncWaiter::~VsyncWaiter ( )
overridevirtual

Reimplemented from flutter::VsyncWaiter.

Definition at line 66 of file vsync_waiter.cc.

References flutter::TaskRunners::GetUITaskRunner(), fml::MakeCopyable(), fml::TaskRunner::RunNowOrPostTask(), fml::AutoResetWaitableEvent::Signal(), flutter::VsyncWaiter::task_runners_, and fml::AutoResetWaitableEvent::Wait().

66  {
67  session_wait_.Cancel();
68 
73  [weak_factory_ui = std::move(weak_factory_ui_), &ui_latch]() mutable {
74  weak_factory_ui.reset();
75  ui_latch.Signal();
76  }));
77  ui_latch.Wait();
78 }
static void RunNowOrPostTask(fml::RefPtr< fml::TaskRunner > runner, const fml::closure &task)
Definition: task_runner.cc:55
internal::CopyableLambda< T > MakeCopyable(T lambda)
Definition: make_copyable.h:57
fml::RefPtr< fml::TaskRunner > GetUITaskRunner() const
Definition: task_runners.cc:34
const TaskRunners task_runners_
Definition: vsync_waiter.h:39

Member Function Documentation

◆ SnapToNextPhase()

fml::TimePoint flutter_runner::VsyncWaiter::SnapToNextPhase ( const fml::TimePoint  now,
const fml::TimePoint  last_frame_presentation_time,
const fml::TimeDelta  presentation_interval 
)
static

Returns the system time at which the next frame is likely to be presented.

Consider the following scenarios, where in both the scenarious the result will be the same.

Scenario 1: presentation_interval is 2 ^ ^ ^ ^ ^

  • + + + + 0–1–2–3–4–5–6–7–8–9–
  • + + | | +------—> result: next_presentation_time | v v now last_presentation_time

Scenario 2: presentation_interval is 2 ^ ^ ^ ^ ^

  • + + + + 0–1–2–3–4–5–6–7–8–9–
    • + + | | +------—>result: next_presentation_time | | | +>now | +->last_presentation_time

Definition at line 107 of file vsync_waiter.cc.

References flutter::VsyncWaiter::FireCallback(), FML_DCHECK, FML_LOG, flutter_runner::VsyncRecorder::GetCurrentVsyncInfo(), flutter_runner::VsyncRecorder::GetInstance(), flutter_runner::VsyncRecorder::GetLastPresentationTime(), flutter::TaskRunners::GetUITaskRunner(), fml::TimePoint::Now(), fml::TaskRunner::PostDelayedTask(), flutter_runner::VsyncInfo::presentation_interval, flutter_runner::VsyncInfo::presentation_time, fml::TaskRunner::RunsTasksOnCurrentThread(), flutter::VsyncWaiter::task_runners_, fml::TimeDelta::ToMilliseconds(), TRACE_EVENT0, and fml::TimeDelta::Zero().

Referenced by flutter_runner_test::TEST_F().

110  {
111  if (presentation_interval <= fml::TimeDelta::Zero()) {
112  FML_LOG(ERROR) << "Presentation interval must be positive. The value was: "
113  << presentation_interval.ToMilliseconds() << "ms.";
114  return now;
115  }
116 
117  if (last_frame_presentation_time >= now) {
118  FML_LOG(ERROR)
119  << "Last frame was presented in the future. Clamping to now.";
120  return now + presentation_interval;
121  }
122 
123  const fml::TimeDelta time_since_last_presentation =
124  now - last_frame_presentation_time;
125  // this will be the most likely scenario if we are rendering at a good
126  // frame rate, short circuiting the other checks in this case.
127  if (time_since_last_presentation < presentation_interval) {
128  return last_frame_presentation_time + presentation_interval;
129  } else {
130  const int64_t num_phases_passed =
131  (time_since_last_presentation / presentation_interval);
132  return last_frame_presentation_time +
133  (presentation_interval * (num_phases_passed + 1));
134  }
135 }
static constexpr TimeDelta Zero()
Definition: time_delta.h:33
constexpr int64_t ToMilliseconds() const
Definition: time_delta.h:63
#define FML_LOG(severity)
Definition: logging.h:65

Member Data Documentation

◆ SessionPresentSignal

constexpr zx_signals_t flutter_runner::VsyncWaiter::SessionPresentSignal = ZX_EVENT_SIGNALED
static

The documentation for this class was generated from the following files: