Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit d0343f1

Browse files
authored
TimePoint::Now uses DartTimestampProvider (#27737)
1 parent bf06b19 commit d0343f1

File tree

9 files changed

+104
-5
lines changed

9 files changed

+104
-5
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,8 @@ FILE: ../../../flutter/fml/thread_local_unittests.cc
292292
FILE: ../../../flutter/fml/thread_unittests.cc
293293
FILE: ../../../flutter/fml/time/chrono_timestamp_provider.cc
294294
FILE: ../../../flutter/fml/time/chrono_timestamp_provider.h
295+
FILE: ../../../flutter/fml/time/dart_timestamp_provider.cc
296+
FILE: ../../../flutter/fml/time/dart_timestamp_provider.h
295297
FILE: ../../../flutter/fml/time/time_delta.h
296298
FILE: ../../../flutter/fml/time/time_delta_unittest.cc
297299
FILE: ../../../flutter/fml/time/time_point.cc

fml/BUILD.gn

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ source_set("fml") {
7777
"thread.h",
7878
"thread_local.cc",
7979
"thread_local.h",
80+
"time/dart_timestamp_provider.cc",
81+
"time/dart_timestamp_provider.h",
8082
"time/time_delta.h",
8183
"time/time_point.cc",
8284
"time/time_point.h",

fml/time/chrono_timestamp_provider.cc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
#include <chrono>
88

9-
#include "fml/time/time_delta.h"
10-
119
namespace fml {
1210

1311
ChronoTimestampProvider::ChronoTimestampProvider() = default;

fml/time/chrono_timestamp_provider.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include "flutter/fml/time/timestamp_provider.h"
99

1010
#include "flutter/fml/macros.h"
11-
#include "fml/time/time_point.h"
11+
#include "flutter/fml/time/time_point.h"
1212

1313
namespace fml {
1414

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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/time/dart_timestamp_provider.h"
6+
7+
#include "dart_tools_api.h"
8+
9+
namespace fml {
10+
11+
DartTimestampProvider::DartTimestampProvider() = default;
12+
13+
DartTimestampProvider::~DartTimestampProvider() = default;
14+
15+
int64_t DartTimestampProvider::ConvertToNanos(int64_t ticks,
16+
int64_t frequency) {
17+
int64_t nano_seconds = (ticks / frequency) * kNanosPerSecond;
18+
int64_t leftover_ticks = ticks % frequency;
19+
int64_t leftover_nanos = (leftover_ticks * kNanosPerSecond) / frequency;
20+
return nano_seconds + leftover_nanos;
21+
}
22+
23+
fml::TimePoint DartTimestampProvider::Now() {
24+
const int64_t ticks = Dart_TimelineGetTicks();
25+
const int64_t frequency = Dart_TimelineGetTicksFrequency();
26+
// optimization for the most common case.
27+
if (frequency != kNanosPerSecond) {
28+
return fml::TimePoint::FromTicks(ConvertToNanos(ticks, frequency));
29+
} else {
30+
return fml::TimePoint::FromTicks(ticks);
31+
}
32+
}
33+
34+
fml::TimePoint DartTimelineTicksSinceEpoch() {
35+
return DartTimestampProvider::Instance().Now();
36+
}
37+
38+
} // namespace fml

fml/time/dart_timestamp_provider.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
#ifndef FLUTTER_FML_TIME_DART_TIMESTAMP_PROVIDER_H_
6+
#define FLUTTER_FML_TIME_DART_TIMESTAMP_PROVIDER_H_
7+
8+
#include "flutter/fml/time/timestamp_provider.h"
9+
10+
#include "flutter/fml/macros.h"
11+
#include "flutter/fml/time/time_point.h"
12+
13+
namespace fml {
14+
15+
fml::TimePoint DartTimelineTicksSinceEpoch();
16+
17+
/// TimestampProvider implementation that is backed by Dart_TimelineGetTicks
18+
class DartTimestampProvider : TimestampProvider {
19+
public:
20+
static DartTimestampProvider& Instance() {
21+
static DartTimestampProvider instance;
22+
return instance;
23+
}
24+
25+
~DartTimestampProvider() override;
26+
27+
fml::TimePoint Now() override;
28+
29+
private:
30+
static constexpr int64_t kNanosPerSecond = 1000000000;
31+
32+
int64_t ConvertToNanos(int64_t ticks, int64_t frequency);
33+
34+
DartTimestampProvider();
35+
36+
FML_DISALLOW_COPY_AND_ASSIGN(DartTimestampProvider);
37+
};
38+
39+
} // namespace fml
40+
41+
#endif // FLUTTER_FML_TIME_DART_TIMESTAMP_PROVIDER_H_

fml/time/time_point.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "flutter/fml/build_config.h"
88
#include "flutter/fml/logging.h"
9+
#include "flutter/fml/time/dart_timestamp_provider.h"
910

1011
#if defined(OS_FUCHSIA)
1112
#include <zircon/syscalls.h>
@@ -36,8 +37,7 @@ static int64_t NanosSinceEpoch(
3637
}
3738

3839
TimePoint TimePoint::Now() {
39-
const int64_t nanos = NanosSinceEpoch(std::chrono::steady_clock::now());
40-
return TimePoint(nanos);
40+
return DartTimelineTicksSinceEpoch();
4141
}
4242

4343
TimePoint TimePoint::CurrentWallTime() {

fml/time/time_point.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class TimePoint {
3939
return TimePoint(ticks.ToNanoseconds());
4040
}
4141

42+
// Expects ticks in nanos.
4243
static constexpr TimePoint FromTicks(int64_t ticks) {
4344
return TimePoint(ticks);
4445
}

fml/time/time_point_unittest.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
#include "flutter/fml/time/chrono_timestamp_provider.h"
66

7+
#include "flutter/fml/time/dart_timestamp_provider.h"
8+
9+
#include <thread>
10+
711
#include "gtest/gtest.h"
812

913
namespace fml {
@@ -14,5 +18,18 @@ TEST(TimePoint, Control) {
1418
EXPECT_GT(TimePoint::Max(), ChronoTicksSinceEpoch());
1519
}
1620

21+
TEST(TimePoint, DartClockIsMonotonic) {
22+
using namespace std::chrono_literals;
23+
const auto t1 = DartTimelineTicksSinceEpoch();
24+
std::this_thread::sleep_for(1us);
25+
const auto t2 = DartTimelineTicksSinceEpoch();
26+
std::this_thread::sleep_for(1us);
27+
const auto t3 = DartTimelineTicksSinceEpoch();
28+
EXPECT_LT(TimePoint::Min(), t1);
29+
EXPECT_LE(t1, t2);
30+
EXPECT_LE(t2, t3);
31+
EXPECT_LT(t3, TimePoint::Max());
32+
}
33+
1734
} // namespace
1835
} // namespace fml

0 commit comments

Comments
 (0)