Skip to content

Commit

Permalink
[Impeller] Adds the ability to specify a golden threshold (flutter#40824
Browse files Browse the repository at this point in the history
)

[Impeller] Adds the ability to specify a golden threshold
  • Loading branch information
gaaclarke authored Apr 3, 2023
1 parent aa60150 commit 6a6d8cc
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 21 deletions.
3 changes: 0 additions & 3 deletions impeller/aiks/aiks_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1243,9 +1243,6 @@ TEST_P(AiksTest, CanRenderTextOutsideBoundaries) {
}

TEST_P(AiksTest, TextRotated) {
#ifdef IMPELLER_GOLDEN_TESTS
GTEST_SKIP() << "Test has small differences on different mac hosts";
#endif
Canvas canvas;
canvas.Transform(Matrix(0.5, -0.3, 0, -0.002, //
0, 1, 0, 0, //
Expand Down
21 changes: 18 additions & 3 deletions impeller/golden_tests/golden_digest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

#include <fstream>

static const double kMaxDiffPixelsPercent = 0.01;
static const int32_t kMaxColorDelta = 8;

namespace impeller {
namespace testing {

Expand All @@ -24,7 +27,8 @@ void GoldenDigest::AddImage(const std::string& test_name,
const std::string& filename,
int32_t width,
int32_t height) {
entries_.push_back({test_name, filename, width, height});
entries_.push_back({test_name, filename, width, height, kMaxDiffPixelsPercent,
kMaxColorDelta});
}

bool GoldenDigest::Write(WorkingDirectory* working_directory) {
Expand All @@ -46,8 +50,19 @@ bool GoldenDigest::Write(WorkingDirectory* working_directory) {
<< "\"testName\" : \"" << entry.test_name << "\", "
<< "\"filename\" : \"" << entry.filename << "\", "
<< "\"width\" : " << entry.width << ", "
<< "\"height\" : " << entry.height << " "
<< "}";
<< "\"height\" : " << entry.height << ", ";

if (entry.max_diff_pixels_percent ==
static_cast<int64_t>(entry.max_diff_pixels_percent)) {
fout << "\"maxDiffPixelsPercent\" : " << entry.max_diff_pixels_percent
<< ".0, ";
} else {
fout << "\"maxDiffPixelsPercent\" : " << entry.max_diff_pixels_percent
<< ", ";
}

fout << "\"maxColorDelta\":" << entry.max_color_delta << " ";
fout << "}";
}
fout << std::endl << "]" << std::endl;

Expand Down
2 changes: 2 additions & 0 deletions impeller/golden_tests/golden_digest.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class GoldenDigest {
std::string filename;
int32_t width;
int32_t height;
double max_diff_pixels_percent;
int32_t max_color_delta;
};

static GoldenDigest* instance_;
Expand Down
15 changes: 7 additions & 8 deletions impeller/golden_tests/golden_playground_test_mac.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,9 @@ bool SaveScreenshot(std::unique_ptr<testing::MetalScreenshot> screenshot) {
} // namespace

struct GoldenPlaygroundTest::GoldenPlaygroundTestImpl {
GoldenPlaygroundTestImpl()
: screenshoter_(new testing::MetalScreenshoter()) {}
std::unique_ptr<testing::MetalScreenshoter> screenshoter_;
ISize window_size_ = ISize{1024, 768};
GoldenPlaygroundTestImpl() : screenshoter(new testing::MetalScreenshoter()) {}
std::unique_ptr<testing::MetalScreenshoter> screenshoter;
ISize window_size = ISize{1024, 768};
};

GoldenPlaygroundTest::GoldenPlaygroundTest()
Expand Down Expand Up @@ -93,7 +92,7 @@ PlaygroundBackend GoldenPlaygroundTest::GetBackend() const {

bool GoldenPlaygroundTest::OpenPlaygroundHere(const Picture& picture) {
auto screenshot =
pimpl_->screenshoter_->MakeScreenshot(picture, pimpl_->window_size_);
pimpl_->screenshoter->MakeScreenshot(picture, pimpl_->window_size);
return SaveScreenshot(std::move(screenshot));
}

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

std::shared_ptr<Context> GoldenPlaygroundTest::GetContext() const {
return pimpl_->screenshoter_->GetContext().GetContext();
return pimpl_->screenshoter->GetContext().GetContext();
}

Point GoldenPlaygroundTest::GetContentScale() const {
return pimpl_->screenshoter_->GetPlayground().GetContentScale();
return pimpl_->screenshoter->GetPlayground().GetContentScale();
}

Scalar GoldenPlaygroundTest::GetSecondsElapsed() const {
return 0.0f;
}

ISize GoldenPlaygroundTest::GetWindowSize() const {
return pimpl_->window_size_;
return pimpl_->window_size;
}

} // namespace impeller
1 change: 1 addition & 0 deletions impeller/golden_tests/golden_tests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ bool SaveScreenshot(std::unique_ptr<MetalScreenshot> screenshot) {
return screenshot->WriteToPNG(
WorkingDirectory::Instance()->GetFilenamePath(filename));
}

} // namespace

class GoldenTests : public ::testing::Test {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class FakeSkiaGoldClient implements SkiaGoldClient {
{double differentPixelsRate = 0.01,
int pixelColorDelta = 0,
required int screenshotSize}) async {
Logger.instance.log('addImg $testName ${goldenFile.path} $screenshotSize');
Logger.instance.log('addImg testName:$testName goldenFile:${goldenFile.path} screenshotSize:$screenshotSize differentPixelsRate:$differentPixelsRate pixelColorDelta:$pixelColorDelta');
}

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@ Future<void> harvest(
final String filename = (map['filename'] as String?)!;
final int width = (map['width'] as int?)!;
final int height = (map['height'] as int?)!;
final double maxDiffPixelsPercent = (map['maxDiffPixelsPercent'] as double?)!;
final int maxColorDelta = (map['maxColorDelta'] as int?)!;
final File goldenImage = File(p.join(workDirectory.path, filename));
final Future<void> future = skiaGoldClient
.addImg(filename, goldenImage, screenshotSize: width * height)
.addImg(filename, goldenImage,
screenshotSize: width * height, differentPixelsRate: maxDiffPixelsPercent, pixelColorDelta: maxColorDelta)
.catchError((dynamic err) {
Logger.instance.log('skia gold comparison failed: $err');
throw Exception('Failed comparison: $filename');
Expand Down
16 changes: 11 additions & 5 deletions testing/skia_gold_client/lib/skia_gold_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -178,15 +178,21 @@ class SkiaGoldClient {
/// The [testName] and [goldenFile] parameters reference the current
/// comparison being evaluated.
///
/// [pixelColorDelta] defines maximum acceptable difference in RGB channels of each pixel,
/// such that:
/// [pixelColorDelta] defines maximum acceptable difference in RGB channels of
/// each pixel, such that:
///
/// ```
/// abs(r(image) - r(golden)) + abs(g(image) - g(golden)) + abs(b(image) - b(golden)) <= pixelDeltaThreshold
/// bool isSame(Color image, Color golden, int pixelDeltaThreshold) {
/// return abs(image.r - golden.r)
/// + abs(image.g - golden.g)
/// + abs(image.b - golden.b) <= pixelDeltaThreshold;
/// }
/// ```
///
/// [differentPixelsRate] is the fraction of accepted pixels to be wrong in the range [0.0, 1.0].
/// Defaults to 0.01. A value of 0.01 means that 1% of the pixels are allowed to change.
/// [differentPixelsRate] is the fraction of pixels that can differ, as
/// determined by the [pixelColorDelta] parameter. It's in the range [0.0,
/// 1.0] and defaults to 0.01. A value of 0.01 means that 1% of the pixels are
/// allowed to be different.
Future<void> addImg(
String testName,
File goldenFile, {
Expand Down

0 comments on commit 6a6d8cc

Please sign in to comment.