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

Commit 28cf41b

Browse files
committed
add tests
1 parent 3bbc2a6 commit 28cf41b

File tree

5 files changed

+127
-25
lines changed

5 files changed

+127
-25
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ FILE: ../../../flutter/flow/flow_test_utils.cc
4444
FILE: ../../../flutter/flow/flow_test_utils.h
4545
FILE: ../../../flutter/flow/frame_timings.cc
4646
FILE: ../../../flutter/flow/frame_timings.h
47+
FILE: ../../../flutter/flow/frame_timings_recorder_unittests.cc
4748
FILE: ../../../flutter/flow/gl_context_switch_unittests.cc
4849
FILE: ../../../flutter/flow/instrumentation.cc
4950
FILE: ../../../flutter/flow/instrumentation.h

flow/BUILD.gn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ if (enable_unittests) {
145145
"flow_run_all_unittests.cc",
146146
"flow_test_utils.cc",
147147
"flow_test_utils.h",
148+
"frame_timings_recorder_unittests.cc",
148149
"gl_context_switch_unittests.cc",
149150
"layers/backdrop_filter_layer_unittests.cc",
150151
"layers/checkerboard_layertree_unittests.cc",

flow/frame_timings.cc

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,79 +17,79 @@ FrameTimingsRecorder::~FrameTimingsRecorder() = default;
1717

1818
fml::TimePoint FrameTimingsRecorder::GetVsyncStartTime() const {
1919
std::scoped_lock state_lock(state_mutex_);
20-
FML_CHECK(state_ >= State::kVsync);
20+
FML_DCHECK(state_ >= State::kVsync);
2121
return vsync_start_;
2222
}
2323

2424
fml::TimePoint FrameTimingsRecorder::GetVsyncTargetTime() const {
2525
std::scoped_lock state_lock(state_mutex_);
26-
FML_CHECK(state_ >= State::kVsync);
26+
FML_DCHECK(state_ >= State::kVsync);
2727
return vsync_target_;
2828
}
2929

3030
fml::TimePoint FrameTimingsRecorder::GetBuildStartTime() const {
3131
std::scoped_lock state_lock(state_mutex_);
32-
FML_CHECK(state_ >= State::kBuildStart);
32+
FML_DCHECK(state_ >= State::kBuildStart);
3333
return build_start_;
3434
}
3535

3636
fml::TimePoint FrameTimingsRecorder::GetBuildEndTime() const {
3737
std::scoped_lock state_lock(state_mutex_);
38-
FML_CHECK(state_ >= State::kBuildEnd);
38+
FML_DCHECK(state_ >= State::kBuildEnd);
3939
return build_end_;
4040
}
4141

4242
fml::TimePoint FrameTimingsRecorder::GetRasterStartTime() const {
4343
std::scoped_lock state_lock(state_mutex_);
44-
FML_CHECK(state_ >= State::kRasterStart);
44+
FML_DCHECK(state_ >= State::kRasterStart);
4545
return raster_start_;
4646
}
4747

4848
fml::TimePoint FrameTimingsRecorder::GetRasterEndTime() const {
4949
std::scoped_lock state_lock(state_mutex_);
50-
FML_CHECK(state_ >= State::kRasterEnd);
50+
FML_DCHECK(state_ >= State::kRasterEnd);
5151
return raster_end_;
5252
}
5353

5454
fml::TimeDelta FrameTimingsRecorder::GetBuildDuration() const {
5555
std::scoped_lock state_lock(state_mutex_);
56-
FML_CHECK(state_ >= State::kBuildEnd);
56+
FML_DCHECK(state_ >= State::kBuildEnd);
5757
return build_end_ - build_start_;
5858
}
5959

6060
void FrameTimingsRecorder::RecordVsync(fml::TimePoint vsync_start,
6161
fml::TimePoint vsync_target) {
6262
std::scoped_lock state_lock(state_mutex_);
63-
FML_CHECK(state_ == State::kUninitialized);
63+
FML_DCHECK(state_ == State::kUninitialized);
6464
state_ = State::kVsync;
6565
vsync_start_ = vsync_start;
6666
vsync_target_ = vsync_target;
6767
}
6868

6969
void FrameTimingsRecorder::RecordBuildStart(fml::TimePoint build_start) {
7070
std::scoped_lock state_lock(state_mutex_);
71-
FML_CHECK(state_ == State::kVsync);
71+
FML_DCHECK(state_ == State::kVsync);
7272
state_ = State::kBuildStart;
7373
build_start_ = build_start;
7474
}
7575

7676
void FrameTimingsRecorder::RecordBuildEnd(fml::TimePoint build_end) {
7777
std::scoped_lock state_lock(state_mutex_);
78-
FML_CHECK(state_ == State::kBuildStart);
78+
FML_DCHECK(state_ == State::kBuildStart);
7979
state_ = State::kBuildEnd;
8080
build_end_ = build_end;
8181
}
8282

8383
void FrameTimingsRecorder::RecordRasterStart(fml::TimePoint raster_start) {
8484
std::scoped_lock state_lock(state_mutex_);
85-
FML_CHECK(state_ == State::kBuildEnd);
85+
FML_DCHECK(state_ == State::kBuildEnd);
8686
state_ = State::kRasterStart;
8787
raster_start_ = raster_start;
8888
}
8989

9090
FrameTiming FrameTimingsRecorder::RecordRasterEnd(fml::TimePoint raster_end) {
9191
std::scoped_lock state_lock(state_mutex_);
92-
FML_CHECK(state_ == State::kRasterStart);
92+
FML_DCHECK(state_ == State::kRasterStart);
9393
state_ = State::kRasterEnd;
9494
raster_end_ = raster_end;
9595
FrameTiming timing;

flow/frame_timings.h

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,19 @@ namespace flutter {
1717
/// Records timestamps for various phases of a frame rendering process.
1818
///
1919
/// Recorder is created on vsync and destroyed after the rasterization of the
20-
/// frame.
20+
/// frame. This class is thread safe and doesn't require additional
21+
/// synchronization.
2122
class FrameTimingsRecorder {
2223
public:
2324
/// Various states that the recorder can be in. When created the recorder is
2425
/// in an unitialized state and transtions in sequential order of the states.
2526
enum class State : uint32_t {
26-
kUninitialized = 1,
27-
kVsync = 2,
28-
kBuildStart = 3,
29-
kBuildEnd = 4,
30-
kRasterStart = 5,
31-
kRasterEnd = 6,
27+
kUninitialized = 0,
28+
kVsync = 1,
29+
kBuildStart = 2,
30+
kBuildEnd = 3,
31+
kRasterStart = 4,
32+
kRasterEnd = 5,
3233
};
3334

3435
/// Default constructor, initializes the recorder with State::kUninitialized.
@@ -82,12 +83,12 @@ class FrameTimingsRecorder {
8283
mutable std::mutex state_mutex_;
8384
State state_ = State::kUninitialized;
8485

85-
fml::TimePoint vsync_start_ = fml::TimePoint::Min();
86-
fml::TimePoint vsync_target_ = fml::TimePoint::Min();
87-
fml::TimePoint build_start_ = fml::TimePoint::Min();
88-
fml::TimePoint build_end_ = fml::TimePoint::Min();
89-
fml::TimePoint raster_start_ = fml::TimePoint::Min();
90-
fml::TimePoint raster_end_ = fml::TimePoint::Min();
86+
fml::TimePoint vsync_start_;
87+
fml::TimePoint vsync_target_;
88+
fml::TimePoint build_start_;
89+
fml::TimePoint build_end_;
90+
fml::TimePoint raster_start_;
91+
fml::TimePoint raster_end_;
9192

9293
FML_DISALLOW_COPY_ASSIGN_AND_MOVE(FrameTimingsRecorder);
9394
};
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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/flow/frame_timings.h"
6+
7+
#include "flutter/fml/time/time_delta.h"
8+
#include "flutter/fml/time/time_point.h"
9+
10+
#include "gtest/gtest.h"
11+
12+
namespace flutter {
13+
namespace testing {
14+
15+
TEST(FrameTimingsRecorderTest, RecordVsync) {
16+
auto recorder = std::make_unique<FrameTimingsRecorder>();
17+
const auto st = fml::TimePoint::Now();
18+
const auto en = st + fml::TimeDelta::FromMillisecondsF(16);
19+
recorder->RecordVsync(st, en);
20+
21+
ASSERT_EQ(st, recorder->GetVsyncStartTime());
22+
ASSERT_EQ(en, recorder->GetVsyncTargetTime());
23+
}
24+
25+
TEST(FrameTimingsRecorderTest, RecordBuildTimes) {
26+
auto recorder = std::make_unique<FrameTimingsRecorder>();
27+
28+
const auto st = fml::TimePoint::Now();
29+
const auto en = st + fml::TimeDelta::FromMillisecondsF(16);
30+
recorder->RecordVsync(st, en);
31+
32+
const auto build_start = fml::TimePoint::Now();
33+
const auto build_end = build_start + fml::TimeDelta::FromMillisecondsF(16);
34+
recorder->RecordBuildStart(build_start);
35+
recorder->RecordBuildEnd(build_end);
36+
37+
ASSERT_EQ(build_start, recorder->GetBuildStartTime());
38+
ASSERT_EQ(build_end, recorder->GetBuildEndTime());
39+
}
40+
41+
TEST(FrameTimingsRecorderTest, RecordRasterTimes) {
42+
auto recorder = std::make_unique<FrameTimingsRecorder>();
43+
44+
const auto st = fml::TimePoint::Now();
45+
const auto en = st + fml::TimeDelta::FromMillisecondsF(16);
46+
recorder->RecordVsync(st, en);
47+
48+
const auto build_start = fml::TimePoint::Now();
49+
const auto build_end = build_start + fml::TimeDelta::FromMillisecondsF(16);
50+
recorder->RecordBuildStart(build_start);
51+
recorder->RecordBuildEnd(build_end);
52+
53+
const auto raster_start = fml::TimePoint::Now();
54+
const auto raster_end = raster_start + fml::TimeDelta::FromMillisecondsF(16);
55+
recorder->RecordRasterStart(raster_start);
56+
recorder->RecordRasterEnd(raster_end);
57+
58+
ASSERT_EQ(raster_start, recorder->GetRasterStartTime());
59+
ASSERT_EQ(raster_end, recorder->GetRasterEndTime());
60+
}
61+
62+
#ifndef OS_FUCHSIA
63+
64+
// Windows doesn't allow testing with killed by signal.
65+
#ifdef OS_WIN
66+
TEST(FrameTimingsRecorderTest, DISABLED_ThrowWhenRecordBuildBeforeVsync) {
67+
#else
68+
TEST(FrameTimingsRecorderTest, ThrowWhenRecordBuildBeforeVsync) {
69+
#endif
70+
auto recorder = std::make_unique<FrameTimingsRecorder>();
71+
72+
const auto build_start = fml::TimePoint::Now();
73+
EXPECT_EXIT(recorder->RecordBuildStart(build_start),
74+
::testing::KilledBySignal(SIGABRT),
75+
"Check failed: state_ == State::kVsync.");
76+
}
77+
78+
// Windows doesn't allow testing with killed by signal.
79+
#ifdef OS_WIN
80+
TEST(FrameTimingsRecorderTest, DISABLED_ThrowWhenRecordRasterBeforeBuildEnd) {
81+
#else
82+
TEST(FrameTimingsRecorderTest, ThrowWhenRecordRasterBeforeBuildEnd) {
83+
#endif
84+
auto recorder = std::make_unique<FrameTimingsRecorder>();
85+
86+
const auto st = fml::TimePoint::Now();
87+
const auto en = st + fml::TimeDelta::FromMillisecondsF(16);
88+
recorder->RecordVsync(st, en);
89+
90+
const auto raster_start = fml::TimePoint::Now();
91+
EXPECT_EXIT(recorder->RecordRasterStart(raster_start),
92+
::testing::KilledBySignal(SIGABRT),
93+
"Check failed: state_ == State::kBuildEnd.");
94+
}
95+
96+
#endif
97+
98+
} // namespace testing
99+
} // namespace flutter

0 commit comments

Comments
 (0)