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

Commit 6a6d8cc

Browse files
authored
[Impeller] Adds the ability to specify a golden threshold (#40824)
[Impeller] Adds the ability to specify a golden threshold
1 parent aa60150 commit 6a6d8cc

File tree

8 files changed

+44
-21
lines changed

8 files changed

+44
-21
lines changed

impeller/aiks/aiks_unittests.cc

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,9 +1243,6 @@ TEST_P(AiksTest, CanRenderTextOutsideBoundaries) {
12431243
}
12441244

12451245
TEST_P(AiksTest, TextRotated) {
1246-
#ifdef IMPELLER_GOLDEN_TESTS
1247-
GTEST_SKIP() << "Test has small differences on different mac hosts";
1248-
#endif
12491246
Canvas canvas;
12501247
canvas.Transform(Matrix(0.5, -0.3, 0, -0.002, //
12511248
0, 1, 0, 0, //

impeller/golden_tests/golden_digest.cc

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
#include <fstream>
88

9+
static const double kMaxDiffPixelsPercent = 0.01;
10+
static const int32_t kMaxColorDelta = 8;
11+
912
namespace impeller {
1013
namespace testing {
1114

@@ -24,7 +27,8 @@ void GoldenDigest::AddImage(const std::string& test_name,
2427
const std::string& filename,
2528
int32_t width,
2629
int32_t height) {
27-
entries_.push_back({test_name, filename, width, height});
30+
entries_.push_back({test_name, filename, width, height, kMaxDiffPixelsPercent,
31+
kMaxColorDelta});
2832
}
2933

3034
bool GoldenDigest::Write(WorkingDirectory* working_directory) {
@@ -46,8 +50,19 @@ bool GoldenDigest::Write(WorkingDirectory* working_directory) {
4650
<< "\"testName\" : \"" << entry.test_name << "\", "
4751
<< "\"filename\" : \"" << entry.filename << "\", "
4852
<< "\"width\" : " << entry.width << ", "
49-
<< "\"height\" : " << entry.height << " "
50-
<< "}";
53+
<< "\"height\" : " << entry.height << ", ";
54+
55+
if (entry.max_diff_pixels_percent ==
56+
static_cast<int64_t>(entry.max_diff_pixels_percent)) {
57+
fout << "\"maxDiffPixelsPercent\" : " << entry.max_diff_pixels_percent
58+
<< ".0, ";
59+
} else {
60+
fout << "\"maxDiffPixelsPercent\" : " << entry.max_diff_pixels_percent
61+
<< ", ";
62+
}
63+
64+
fout << "\"maxColorDelta\":" << entry.max_color_delta << " ";
65+
fout << "}";
5166
}
5267
fout << std::endl << "]" << std::endl;
5368

impeller/golden_tests/golden_digest.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ class GoldenDigest {
3636
std::string filename;
3737
int32_t width;
3838
int32_t height;
39+
double max_diff_pixels_percent;
40+
int32_t max_color_delta;
3941
};
4042

4143
static GoldenDigest* instance_;

impeller/golden_tests/golden_playground_test_mac.cc

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,9 @@ bool SaveScreenshot(std::unique_ptr<testing::MetalScreenshot> screenshot) {
4242
} // namespace
4343

4444
struct GoldenPlaygroundTest::GoldenPlaygroundTestImpl {
45-
GoldenPlaygroundTestImpl()
46-
: screenshoter_(new testing::MetalScreenshoter()) {}
47-
std::unique_ptr<testing::MetalScreenshoter> screenshoter_;
48-
ISize window_size_ = ISize{1024, 768};
45+
GoldenPlaygroundTestImpl() : screenshoter(new testing::MetalScreenshoter()) {}
46+
std::unique_ptr<testing::MetalScreenshoter> screenshoter;
47+
ISize window_size = ISize{1024, 768};
4948
};
5049

5150
GoldenPlaygroundTest::GoldenPlaygroundTest()
@@ -93,7 +92,7 @@ PlaygroundBackend GoldenPlaygroundTest::GetBackend() const {
9392

9493
bool GoldenPlaygroundTest::OpenPlaygroundHere(const Picture& picture) {
9594
auto screenshot =
96-
pimpl_->screenshoter_->MakeScreenshot(picture, pimpl_->window_size_);
95+
pimpl_->screenshoter->MakeScreenshot(picture, pimpl_->window_size);
9796
return SaveScreenshot(std::move(screenshot));
9897
}
9998

@@ -116,19 +115,19 @@ std::shared_ptr<Texture> GoldenPlaygroundTest::CreateTextureForFixture(
116115
}
117116

118117
std::shared_ptr<Context> GoldenPlaygroundTest::GetContext() const {
119-
return pimpl_->screenshoter_->GetContext().GetContext();
118+
return pimpl_->screenshoter->GetContext().GetContext();
120119
}
121120

122121
Point GoldenPlaygroundTest::GetContentScale() const {
123-
return pimpl_->screenshoter_->GetPlayground().GetContentScale();
122+
return pimpl_->screenshoter->GetPlayground().GetContentScale();
124123
}
125124

126125
Scalar GoldenPlaygroundTest::GetSecondsElapsed() const {
127126
return 0.0f;
128127
}
129128

130129
ISize GoldenPlaygroundTest::GetWindowSize() const {
131-
return pimpl_->window_size_;
130+
return pimpl_->window_size;
132131
}
133132

134133
} // namespace impeller

impeller/golden_tests/golden_tests.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ bool SaveScreenshot(std::unique_ptr<MetalScreenshot> screenshot) {
4343
return screenshot->WriteToPNG(
4444
WorkingDirectory::Instance()->GetFilenamePath(filename));
4545
}
46+
4647
} // namespace
4748

4849
class GoldenTests : public ::testing::Test {

impeller/golden_tests_harvester/bin/golden_tests_harvester.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class FakeSkiaGoldClient implements SkiaGoldClient {
2323
{double differentPixelsRate = 0.01,
2424
int pixelColorDelta = 0,
2525
required int screenshotSize}) async {
26-
Logger.instance.log('addImg $testName ${goldenFile.path} $screenshotSize');
26+
Logger.instance.log('addImg testName:$testName goldenFile:${goldenFile.path} screenshotSize:$screenshotSize differentPixelsRate:$differentPixelsRate pixelColorDelta:$pixelColorDelta');
2727
}
2828

2929
@override

impeller/golden_tests_harvester/lib/golden_tests_harvester.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,12 @@ Future<void> harvest(
3232
final String filename = (map['filename'] as String?)!;
3333
final int width = (map['width'] as int?)!;
3434
final int height = (map['height'] as int?)!;
35+
final double maxDiffPixelsPercent = (map['maxDiffPixelsPercent'] as double?)!;
36+
final int maxColorDelta = (map['maxColorDelta'] as int?)!;
3537
final File goldenImage = File(p.join(workDirectory.path, filename));
3638
final Future<void> future = skiaGoldClient
37-
.addImg(filename, goldenImage, screenshotSize: width * height)
39+
.addImg(filename, goldenImage,
40+
screenshotSize: width * height, differentPixelsRate: maxDiffPixelsPercent, pixelColorDelta: maxColorDelta)
3841
.catchError((dynamic err) {
3942
Logger.instance.log('skia gold comparison failed: $err');
4043
throw Exception('Failed comparison: $filename');

testing/skia_gold_client/lib/skia_gold_client.dart

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,15 +178,21 @@ class SkiaGoldClient {
178178
/// The [testName] and [goldenFile] parameters reference the current
179179
/// comparison being evaluated.
180180
///
181-
/// [pixelColorDelta] defines maximum acceptable difference in RGB channels of each pixel,
182-
/// such that:
181+
/// [pixelColorDelta] defines maximum acceptable difference in RGB channels of
182+
/// each pixel, such that:
183183
///
184184
/// ```
185-
/// abs(r(image) - r(golden)) + abs(g(image) - g(golden)) + abs(b(image) - b(golden)) <= pixelDeltaThreshold
185+
/// bool isSame(Color image, Color golden, int pixelDeltaThreshold) {
186+
/// return abs(image.r - golden.r)
187+
/// + abs(image.g - golden.g)
188+
/// + abs(image.b - golden.b) <= pixelDeltaThreshold;
189+
/// }
186190
/// ```
187191
///
188-
/// [differentPixelsRate] is the fraction of accepted pixels to be wrong in the range [0.0, 1.0].
189-
/// Defaults to 0.01. A value of 0.01 means that 1% of the pixels are allowed to change.
192+
/// [differentPixelsRate] is the fraction of pixels that can differ, as
193+
/// determined by the [pixelColorDelta] parameter. It's in the range [0.0,
194+
/// 1.0] and defaults to 0.01. A value of 0.01 means that 1% of the pixels are
195+
/// allowed to be different.
190196
Future<void> addImg(
191197
String testName,
192198
File goldenFile, {

0 commit comments

Comments
 (0)