Flutter Engine
The Flutter Engine
message_loop_win.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/fml/platform/win/message_loop_win.h"
6
7#include <VersionHelpers.h>
8#include <timeapi.h>
9
10#include "flutter/fml/logging.h"
11
12constexpr uint32_t kHighResolutionTimer = 1; // 1 ms
13constexpr uint32_t kLowResolutionTimer = 15; // 15 ms
14
15namespace fml {
16
17MessageLoopWin::MessageLoopWin()
18 : timer_(CreateWaitableTimer(NULL, FALSE, NULL)) {
19 FML_CHECK(timer_.is_valid());
20 // Flutter uses timers to schedule frames. By default, Windows timers do
21 // not have the precision to reliably schedule frame rates greater than
22 // 60hz. We can increase the precision, but on versions of Windows before
23 // 10, this would globally increase timer precision leading to increased
24 // resource usage. This would be particularly problematic on a laptop or
25 // mobile device.
26 if (IsWindows10OrGreater()) {
27 timer_resolution_ = kHighResolutionTimer;
28 } else {
29 timer_resolution_ = kLowResolutionTimer;
30 }
31 timeBeginPeriod(timer_resolution_);
32}
33
34MessageLoopWin::~MessageLoopWin() = default;
35
36void MessageLoopWin::Run() {
37 running_ = true;
38
39 while (running_) {
40 FML_CHECK(WaitForSingleObject(timer_.get(), INFINITE) == 0);
42 }
43}
44
45void MessageLoopWin::Terminate() {
46 running_ = false;
47 WakeUp(fml::TimePoint::Now());
48 timeEndPeriod(timer_resolution_);
49}
50
51void MessageLoopWin::WakeUp(fml::TimePoint time_point) {
52 LARGE_INTEGER due_time = {0};
54 if (time_point > now) {
55 due_time.QuadPart = (time_point - now).ToNanoseconds() / -100;
56 }
57 FML_CHECK(SetWaitableTimer(timer_.get(), &due_time, 0, NULL, NULL, FALSE));
58}
59
60} // namespace fml
static TimePoint Now()
Definition: time_point.cc:49
const T & get() const
Definition: unique_object.h:87
#define FML_CHECK(condition)
Definition: logging.h:85
constexpr uint32_t kLowResolutionTimer
constexpr uint32_t kHighResolutionTimer
return FALSE
Definition: ascii_trie.cc:9