|
| 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 | +} |
0 commit comments