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

Commit 8427d73

Browse files
authored
Reland PerformanceOverlayLayer golden test (#7863)
* Revert "Revert "Add mock capability to PerformanceOverlayLayer (#7537)" (#7765)" This reverts commit 693645e. * Add command line args for golden dir and font file
1 parent 61fc178 commit 8427d73

File tree

9 files changed

+234
-12
lines changed

9 files changed

+234
-12
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ FILE: ../../../flutter/flow/layers/opacity_layer.cc
5757
FILE: ../../../flutter/flow/layers/opacity_layer.h
5858
FILE: ../../../flutter/flow/layers/performance_overlay_layer.cc
5959
FILE: ../../../flutter/flow/layers/performance_overlay_layer.h
60+
FILE: ../../../flutter/flow/layers/performance_overlay_layer_unittests.cc
6061
FILE: ../../../flutter/flow/layers/physical_shape_layer.cc
6162
FILE: ../../../flutter/flow/layers/physical_shape_layer.h
6263
FILE: ../../../flutter/flow/layers/picture_layer.cc
@@ -692,9 +693,13 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
692693
====================================================================================================
693694

694695
====================================================================================================
696+
LIBRARY: engine
695697
LIBRARY: txt
696698
ORIGIN: ../../../flutter/third_party/txt/LICENSE
697699
TYPE: LicenseType.apache
700+
FILE: ../../../flutter/flow/flow_run_all_unittests.cc
701+
FILE: ../../../flutter/flow/flow_test_utils.cc
702+
FILE: ../../../flutter/flow/flow_test_utils.h
698703
FILE: ../../../flutter/third_party/txt/benchmarks/paint_record_benchmarks.cc
699704
FILE: ../../../flutter/third_party/txt/benchmarks/paragraph_benchmarks.cc
700705
FILE: ../../../flutter/third_party/txt/benchmarks/paragraph_builder_benchmarks.cc

flow/BUILD.gn

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,18 @@ executable("flow_unittests") {
9393
testonly = true
9494

9595
sources = [
96+
"flow_run_all_unittests.cc",
97+
"flow_test_utils.h",
98+
"flow_test_utils.cc",
9699
"matrix_decomposition_unittests.cc",
97100
"raster_cache_unittests.cc",
101+
"layers/performance_overlay_layer_unittests.cc",
98102
]
99103

100104
deps = [
101105
":flow",
102-
"$flutter_root/testing",
106+
"$flutter_root/fml",
107+
"//third_party/googletest:gtest",
103108
"//third_party/dart/runtime:libdart_jit", # for tracing
104109
"//third_party/skia",
105110
]

flow/flow_run_all_unittests.cc

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2017 Google, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "flutter/fml/command_line.h"
18+
#include "flutter/fml/logging.h"
19+
#include "gtest/gtest.h"
20+
21+
#include "flow_test_utils.h"
22+
23+
int main(int argc, char** argv) {
24+
testing::InitGoogleTest(&argc, argv);
25+
fml::CommandLine cmd = fml::CommandLineFromArgcArgv(argc, argv);
26+
flow::SetGoldenDir(
27+
cmd.GetOptionValueWithDefault("golden-dir", "flutter/testing/resources"));
28+
flow::SetFontFile(cmd.GetOptionValueWithDefault(
29+
"font-file",
30+
"flutter/third_party/txt/third_party/fonts/Roboto-Regular.ttf"));
31+
return RUN_ALL_TESTS();
32+
}

flow/flow_test_utils.cc

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2017 Google, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include <string>
18+
19+
namespace flow {
20+
21+
static std::string gGoldenDir;
22+
static std::string gFontFile;
23+
24+
const std::string& GetGoldenDir() {
25+
return gGoldenDir;
26+
}
27+
28+
void SetGoldenDir(const std::string& dir) {
29+
gGoldenDir = dir;
30+
}
31+
32+
const std::string& GetFontFile() {
33+
return gFontFile;
34+
}
35+
36+
void SetFontFile(const std::string& file) {
37+
gFontFile = file;
38+
}
39+
40+
} // namespace flow

flow/flow_test_utils.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2017 Google, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include <string>
18+
19+
namespace flow {
20+
21+
const std::string& GetGoldenDir();
22+
23+
void SetGoldenDir(const std::string& dir);
24+
25+
const std::string& GetFontFile();
26+
27+
void SetFontFile(const std::string& dir);
28+
29+
} // namespace flow

flow/layers/performance_overlay_layer.cc

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@ namespace {
1515
void DrawStatisticsText(SkCanvas& canvas,
1616
const std::string& string,
1717
int x,
18-
int y) {
18+
int y,
19+
const std::string& font_path) {
1920
SkFont font;
21+
if (font_path != "") {
22+
font = SkFont(SkTypeface::MakeFromFile(font_path.c_str()));
23+
}
2024
font.setSize(15);
2125
font.setLinearMetrics(false);
2226
SkPaint paint;
@@ -33,7 +37,8 @@ void VisualizeStopWatch(SkCanvas& canvas,
3337
SkScalar height,
3438
bool show_graph,
3539
bool show_labels,
36-
const std::string& label_prefix) {
40+
const std::string& label_prefix,
41+
const std::string& font_path) {
3742
const int label_x = 8; // distance from x
3843
const int label_y = -10; // distance from y+height
3944

@@ -51,14 +56,20 @@ void VisualizeStopWatch(SkCanvas& canvas,
5156
stream << label_prefix << " "
5257
<< "max " << max_ms_per_frame << " ms/frame, "
5358
<< "avg " << average_ms_per_frame << " ms/frame";
54-
DrawStatisticsText(canvas, stream.str(), x + label_x, y + height + label_y);
59+
DrawStatisticsText(canvas, stream.str(), x + label_x, y + height + label_y,
60+
font_path);
5561
}
5662
}
5763

5864
} // namespace
5965

60-
PerformanceOverlayLayer::PerformanceOverlayLayer(uint64_t options)
61-
: options_(options) {}
66+
PerformanceOverlayLayer::PerformanceOverlayLayer(uint64_t options,
67+
const char* font_path)
68+
: options_(options) {
69+
if (font_path != nullptr) {
70+
font_path_ = font_path;
71+
}
72+
}
6273

6374
void PerformanceOverlayLayer::Paint(PaintContext& context) const {
6475
const int padding = 8;
@@ -73,15 +84,15 @@ void PerformanceOverlayLayer::Paint(PaintContext& context) const {
7384
SkScalar height = paint_bounds().height() / 2;
7485
SkAutoCanvasRestore save(context.leaf_nodes_canvas, true);
7586

76-
VisualizeStopWatch(*context.leaf_nodes_canvas, context.frame_time, x, y,
77-
width, height - padding,
78-
options_ & kVisualizeRasterizerStatistics,
79-
options_ & kDisplayRasterizerStatistics, "GPU");
87+
VisualizeStopWatch(
88+
*context.leaf_nodes_canvas, context.frame_time, x, y, width,
89+
height - padding, options_ & kVisualizeRasterizerStatistics,
90+
options_ & kDisplayRasterizerStatistics, "GPU", font_path_);
8091

8192
VisualizeStopWatch(*context.leaf_nodes_canvas, context.engine_time, x,
8293
y + height, width, height - padding,
8394
options_ & kVisualizeEngineStatistics,
84-
options_ & kDisplayEngineStatistics, "UI");
95+
options_ & kDisplayEngineStatistics, "UI", font_path_);
8596
}
8697

8798
} // namespace flow

flow/layers/performance_overlay_layer.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#ifndef FLUTTER_FLOW_LAYERS_PERFORMANCE_OVERLAY_LAYER_H_
66
#define FLUTTER_FLOW_LAYERS_PERFORMANCE_OVERLAY_LAYER_H_
77

8+
#include <string>
9+
810
#include "flutter/flow/layers/layer.h"
911
#include "flutter/fml/macros.h"
1012

@@ -17,12 +19,14 @@ const int kVisualizeEngineStatistics = 1 << 3;
1719

1820
class PerformanceOverlayLayer : public Layer {
1921
public:
20-
explicit PerformanceOverlayLayer(uint64_t options);
22+
explicit PerformanceOverlayLayer(uint64_t options,
23+
const char* font_path = nullptr);
2124

2225
void Paint(PaintContext& context) const override;
2326

2427
private:
2528
int options_;
29+
std::string font_path_;
2630

2731
FML_DISALLOW_COPY_AND_ASSIGN(PerformanceOverlayLayer);
2832
};
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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/flow_test_utils.h"
6+
#include "flutter/flow/layers/performance_overlay_layer.h"
7+
#include "flutter/flow/raster_cache.h"
8+
9+
#include "third_party/skia/include/core/SkSurface.h"
10+
#include "third_party/skia/include/utils/SkBase64.h"
11+
12+
#include "gtest/gtest.h"
13+
14+
// To get the size of kMockedTimes in compile time.
15+
template <class T, std::size_t N>
16+
constexpr int size(const T (&array)[N]) noexcept {
17+
return N;
18+
}
19+
20+
constexpr int kMockedTimes[] = {17, 1, 4, 24, 4, 25, 30, 4, 13, 34,
21+
14, 0, 18, 9, 32, 36, 26, 23, 5, 8,
22+
32, 18, 29, 16, 29, 18, 0, 36, 33, 10};
23+
24+
// Relative to the flutter/src/engine/flutter directory
25+
const char* kGoldenFileName = "performance_overlay_gold.png";
26+
27+
// Relative to the flutter/src/engine/flutter directory
28+
const char* kNewGoldenFileName = "performance_overlay_gold_new.png";
29+
30+
TEST(PerformanceOverlayLayer, Gold) {
31+
const std::string& golden_dir = flow::GetGoldenDir();
32+
// This unit test should only be run on Linux (not even on Mac since it's a
33+
// golden test). Hence we don't have to worry about the "/" vs. "\".
34+
std::string golden_file_path = golden_dir + "/" + kGoldenFileName;
35+
std::string new_golden_file_path = golden_dir + "/" + kNewGoldenFileName;
36+
37+
flow::Stopwatch mock_stopwatch;
38+
for (int i = 0; i < size(kMockedTimes); ++i) {
39+
mock_stopwatch.SetLapTime(
40+
fml::TimeDelta::FromMilliseconds(kMockedTimes[i]));
41+
}
42+
43+
const SkImageInfo image_info = SkImageInfo::MakeN32Premul(1000, 1000);
44+
sk_sp<SkSurface> surface = SkSurface::MakeRaster(image_info);
45+
46+
ASSERT_TRUE(surface != nullptr);
47+
48+
flow::TextureRegistry unused_texture_registry;
49+
50+
flow::Layer::PaintContext paintContext = {
51+
nullptr, surface->getCanvas(), nullptr, mock_stopwatch,
52+
mock_stopwatch, unused_texture_registry, nullptr, false};
53+
54+
// Specify font file to ensure the same font across different operation
55+
// systems.
56+
flow::PerformanceOverlayLayer layer(flow::kDisplayRasterizerStatistics |
57+
flow::kVisualizeRasterizerStatistics |
58+
flow::kDisplayEngineStatistics |
59+
flow::kVisualizeEngineStatistics,
60+
flow::GetFontFile().c_str());
61+
layer.set_paint_bounds(SkRect::MakeWH(1000, 400));
62+
surface->getCanvas()->clear(SK_ColorTRANSPARENT);
63+
layer.Paint(paintContext);
64+
65+
sk_sp<SkImage> snapshot = surface->makeImageSnapshot();
66+
sk_sp<SkData> snapshot_data = snapshot->encodeToData();
67+
68+
sk_sp<SkData> golden_data =
69+
SkData::MakeFromFileName(golden_file_path.c_str());
70+
EXPECT_TRUE(golden_data != nullptr)
71+
<< "Golden file not found: " << golden_file_path << ".\n"
72+
<< "Please either set --golden-dir, or make sure that the unit test is "
73+
<< "run from the right directory (e.g., flutter/engine/src).";
74+
75+
const bool golden_data_matches = golden_data->equals(snapshot_data.get());
76+
if (!golden_data_matches) {
77+
SkFILEWStream wstream(new_golden_file_path.c_str());
78+
wstream.write(snapshot_data->data(), snapshot_data->size());
79+
wstream.flush();
80+
81+
size_t b64_size =
82+
SkBase64::Encode(snapshot_data->data(), snapshot_data->size(), nullptr);
83+
char* b64_data = new char[b64_size];
84+
SkBase64::Encode(snapshot_data->data(), snapshot_data->size(), b64_data);
85+
86+
EXPECT_TRUE(golden_data_matches)
87+
<< "Golden file mismatch. Please check "
88+
<< "the difference between " << kGoldenFileName << " and "
89+
<< kNewGoldenFileName << ", and replace the former "
90+
<< "with the latter if the difference looks good.\n\n"
91+
<< "See also the base64 encoded " << kNewGoldenFileName << ":\n"
92+
<< b64_data;
93+
94+
delete[] b64_data;
95+
}
96+
}
16.2 KB
Loading

0 commit comments

Comments
 (0)