2018-11-26 19:07:41 +00:00
|
|
|
// Copyright 2018 Google LLC.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Jitter GMs
|
|
|
|
//
|
|
|
|
// Re-execute rendering tests with slight translational changes and see if
|
|
|
|
// there is a significant change. Print `1` if the named test has no
|
|
|
|
// significant change, `0` otherwise
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkBitmap.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkColor.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkExecutor.h"
|
|
|
|
#include "include/core/SkGraphics.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkPoint.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/private/SkSemaphore.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "tools/Registry.h"
|
|
|
|
#include "tools/skqp/src/skqp.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tools/skqp/src/skqp_model.h"
|
2018-11-26 19:07:41 +00:00
|
|
|
|
2019-05-01 21:28:53 +00:00
|
|
|
#include <math.h>
|
2018-11-26 19:07:41 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <fstream>
|
2019-05-01 21:28:53 +00:00
|
|
|
#include <memory>
|
2018-11-26 19:07:41 +00:00
|
|
|
#include <mutex>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
// Error tolerance distance in 8888 color space with Manhattan metric on color channel.
|
|
|
|
static constexpr uint8_t kSkiaSkqpGlobalErrorTolerance = 8;
|
|
|
|
|
|
|
|
// Number of times to jitter the canvas.
|
|
|
|
static constexpr int kNumberOfJitters = 7;
|
|
|
|
|
|
|
|
// Distance to translate the canvas in each jitter (direction will be different each time).
|
|
|
|
static constexpr float kJitterMagnitude = 0.03125f;
|
|
|
|
|
|
|
|
// The `kNumberOfJitters` different runs will each go in a different direction.
|
|
|
|
// this is the angle (in radians) for the first one.
|
|
|
|
static constexpr float kPhase = 0.3f;
|
|
|
|
|
|
|
|
static void do_gm(SkBitmap* bm, skiagm::GM* gm, SkPoint jitter) {
|
|
|
|
SkASSERT(bm);
|
|
|
|
SkASSERT(gm);
|
|
|
|
SkASSERT(bm->dimensions() == gm->getISize());
|
|
|
|
SkCanvas canvas(*bm);
|
|
|
|
SkAutoCanvasRestore autoCanvasRestore(&canvas, true);
|
|
|
|
canvas.clear(SK_ColorWHITE);
|
|
|
|
canvas.translate(jitter.x(), jitter.y());
|
|
|
|
gm->draw(&canvas);
|
|
|
|
canvas.flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return true if passes jitter test.
|
|
|
|
static bool test_jitter(skiagm::GM* gm) {
|
|
|
|
SkASSERT(gm);
|
|
|
|
SkISize size = gm->getISize();
|
|
|
|
SkBitmap control, experimental;
|
|
|
|
control.allocN32Pixels(size.width(), size.height());
|
|
|
|
experimental.allocN32Pixels(size.width(), size.height());
|
|
|
|
do_gm(&control, gm, {0, 0});
|
|
|
|
for (int i = 0; i < kNumberOfJitters; ++i) {
|
|
|
|
float angle = i * (6.2831853f / kNumberOfJitters) + kPhase;
|
|
|
|
do_gm(&experimental, gm, SkPoint{kJitterMagnitude * cosf(angle),
|
|
|
|
kJitterMagnitude * sinf(angle)});
|
|
|
|
SkQP::RenderOutcome result = skqp::Check(
|
|
|
|
control.pixmap(), control.pixmap(), experimental.pixmap(),
|
|
|
|
kSkiaSkqpGlobalErrorTolerance, nullptr);
|
|
|
|
if (result.fTotalError > 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool do_this_test(const char* name,
|
|
|
|
const std::vector<std::string>& doNotRun,
|
|
|
|
const std::vector<std::string>& testOnlyThese) {
|
|
|
|
for (const std::string& bad : doNotRun) {
|
|
|
|
if (bad == name) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (const std::string& good : testOnlyThese) {
|
|
|
|
if (good == name) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return testOnlyThese.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
|
|
|
std::vector<std::string> doNotRun;
|
|
|
|
std::vector<std::string> testOnlyThese;
|
|
|
|
if (argc > 1) {
|
|
|
|
std::ifstream ifs(argv[1]);
|
|
|
|
if (ifs.is_open()) {
|
|
|
|
std::string str;
|
|
|
|
while (std::getline(ifs, str)) {
|
|
|
|
doNotRun.push_back(str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (argc > 2) {
|
|
|
|
for (int i = 2; i < argc; ++i) {
|
|
|
|
testOnlyThese.emplace_back(argv[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SkGraphics::Init();
|
|
|
|
std::mutex mutex;
|
|
|
|
std::vector<std::string> goodResults;
|
|
|
|
std::vector<std::string> badResults;
|
|
|
|
|
|
|
|
int total = 0;
|
|
|
|
SkSemaphore semaphore;
|
|
|
|
auto executor = SkExecutor::MakeFIFOThreadPool();
|
|
|
|
for (skiagm::GMFactory factory : skiagm::GMRegistry::Range()) {
|
|
|
|
++total;
|
|
|
|
executor->add([factory, &mutex, &goodResults, &badResults,
|
|
|
|
&semaphore, &doNotRun, &testOnlyThese](){
|
2019-08-05 14:28:09 +00:00
|
|
|
std::unique_ptr<skiagm::GM> gm(factory());
|
2018-11-26 19:07:41 +00:00
|
|
|
const char* name = gm->getName();
|
|
|
|
if (do_this_test(name, doNotRun, testOnlyThese)) {
|
|
|
|
bool success = test_jitter(gm.get());
|
|
|
|
std::lock_guard<std::mutex> lock(mutex);
|
|
|
|
if (success) {
|
|
|
|
goodResults.emplace_back(name);
|
|
|
|
} else {
|
|
|
|
badResults.emplace_back(name);
|
|
|
|
}
|
|
|
|
fputc('.', stderr);
|
|
|
|
fflush(stderr);
|
|
|
|
}
|
|
|
|
semaphore.signal();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
while (total-- > 0) { semaphore.wait(); }
|
|
|
|
fputc('\n', stderr);
|
|
|
|
fflush(stderr);
|
|
|
|
std::sort(goodResults.begin(), goodResults.end());
|
|
|
|
std::sort(badResults.begin(), badResults.end());
|
|
|
|
std::ofstream good("good.txt");
|
|
|
|
std::ofstream bad("bad.txt");
|
|
|
|
for (const std::string& s : goodResults) { good << s << '\n'; }
|
|
|
|
for (const std::string& s : badResults) { bad << s << '\n'; }
|
|
|
|
fprintf(stderr, "good = %u\nbad = %u\n\n",
|
|
|
|
(unsigned)goodResults.size(), (unsigned)badResults.size());
|
|
|
|
}
|