Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions fml/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ source_set("fml") {
"thread.h",
"thread_local.cc",
"thread_local.h",
"time/dart_timestamp_provider.cc",
"time/dart_timestamp_provider.h",
"time/time_delta.h",
"time/time_point.cc",
"time/time_point.h",
Expand Down
2 changes: 0 additions & 2 deletions fml/time/chrono_timestamp_provider.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

#include <chrono>

#include "fml/time/time_delta.h"

namespace fml {

ChronoTimestampProvider::ChronoTimestampProvider() = default;
Expand Down
2 changes: 1 addition & 1 deletion fml/time/chrono_timestamp_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "flutter/fml/time/timestamp_provider.h"

#include "flutter/fml/macros.h"
#include "fml/time/time_point.h"
#include "flutter/fml/time/time_point.h"

namespace fml {

Expand Down
23 changes: 23 additions & 0 deletions fml/time/dart_timestamp_provider.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2013 The Flutter 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 "flutter/fml/time/dart_timestamp_provider.h"

#include "dart_tools_api.h"

namespace fml {

DartTimestampProvider::DartTimestampProvider() = default;

DartTimestampProvider::~DartTimestampProvider() = default;

fml::TimePoint DartTimestampProvider::Now() {
return fml::TimePoint::FromTicks(Dart_TimelineGetTicks());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps I don't understand this API but this seems off to me. Here, we are depending on the timebases of fml::TimePoint and Dart_Timeline to be identical. Looking at what was recently exposed we'd need to take into the timebase using Dart_TimelineGetTicksFrequency. Maybe we are getting away with these because both are in nanoseconds?

}

fml::TimePoint DartTimelineTicksSinceEpoch() {
return DartTimestampProvider::Instance().Now();
}

} // namespace fml
37 changes: 37 additions & 0 deletions fml/time/dart_timestamp_provider.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef FLUTTER_FML_TIME_DART_TIMESTAMP_PROVIDER_H_
#define FLUTTER_FML_TIME_DART_TIMESTAMP_PROVIDER_H_

#include "flutter/fml/time/timestamp_provider.h"

#include "flutter/fml/macros.h"
#include "flutter/fml/time/time_point.h"

namespace fml {

fml::TimePoint DartTimelineTicksSinceEpoch();

/// TimestampProvider implementation that is backed by Dart_TimelineGetTicks
class DartTimestampProvider : TimestampProvider {
public:
static DartTimestampProvider& Instance() {
static DartTimestampProvider instance;
return instance;
}

~DartTimestampProvider() override;

fml::TimePoint Now() override;

private:
DartTimestampProvider();

FML_DISALLOW_COPY_AND_ASSIGN(DartTimestampProvider);
};

} // namespace fml

#endif // FLUTTER_FML_TIME_DART_TIMESTAMP_PROVIDER_H_
4 changes: 2 additions & 2 deletions fml/time/time_point.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "flutter/fml/build_config.h"
#include "flutter/fml/logging.h"
#include "flutter/fml/time/dart_timestamp_provider.h"

#if defined(OS_FUCHSIA)
#include <zircon/syscalls.h>
Expand Down Expand Up @@ -36,8 +37,7 @@ static int64_t NanosSinceEpoch(
}

TimePoint TimePoint::Now() {
const int64_t nanos = NanosSinceEpoch(std::chrono::steady_clock::now());
return TimePoint(nanos);
return DartTimelineTicksSinceEpoch();
}

TimePoint TimePoint::CurrentWallTime() {
Expand Down
12 changes: 12 additions & 0 deletions fml/time/time_point_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

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

#include "flutter/fml/time/dart_timestamp_provider.h"

#include "gtest/gtest.h"

namespace fml {
Expand All @@ -14,5 +16,15 @@ TEST(TimePoint, Control) {
EXPECT_GT(TimePoint::Max(), ChronoTicksSinceEpoch());
}

TEST(TimePoint, DartClockIsMonotonic) {
const auto t1 = DartTimelineTicksSinceEpoch();
const auto t2 = DartTimelineTicksSinceEpoch();
const auto t3 = DartTimelineTicksSinceEpoch();
EXPECT_LT(TimePoint::Min(), t1);
EXPECT_LT(t1, t2);
EXPECT_LT(t2, t3);
EXPECT_LT(t3, TimePoint::Max());
}

} // namespace
} // namespace fml