Home | History | Annotate | Download | only in skqp
      1 // Copyright 2018 Google LLC.
      2 // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
      3 
      4 // Jitter GMs
      5 //
      6 // Re-execute rendering tests with slight translational changes and see if
      7 // there is a significant change.  Print `1` if the named test has no
      8 // significant change, `0` otherwise
      9 
     10 #include "gm.h"
     11 #include "SkGraphics.h"
     12 #include "SkExecutor.h"
     13 #include "SkSemaphore.h"
     14 
     15 #include "skqp_model.h"
     16 
     17 #include <algorithm>
     18 #include <cstdio>
     19 #include <fstream>
     20 #include <iostream>
     21 #include <mutex>
     22 #include <string>
     23 #include <vector>
     24 
     25 // Error tolerance distance in 8888 color space with Manhattan metric on color channel.
     26 static constexpr uint8_t kSkiaSkqpGlobalErrorTolerance = 8;
     27 
     28 // Number of times to jitter the canvas.
     29 static constexpr int kNumberOfJitters = 7;
     30 
     31 // Distance to translate the canvas in each jitter (direction will be different each time).
     32 static constexpr float kJitterMagnitude = 0.03125f;
     33 
     34 // The `kNumberOfJitters` different runs will each go in a different direction.
     35 // this is the angle (in radians) for the first one.
     36 static constexpr float kPhase = 0.3f;
     37 
     38 static void do_gm(SkBitmap* bm, skiagm::GM* gm, SkPoint jitter) {
     39     SkASSERT(bm);
     40     SkASSERT(gm);
     41     SkASSERT(bm->dimensions() == gm->getISize());
     42     SkCanvas canvas(*bm);
     43     SkAutoCanvasRestore autoCanvasRestore(&canvas, true);
     44     canvas.clear(SK_ColorWHITE);
     45     canvas.translate(jitter.x(), jitter.y());
     46     gm->draw(&canvas);
     47     canvas.flush();
     48 }
     49 
     50 // Return true if passes jitter test.
     51 static bool test_jitter(skiagm::GM* gm) {
     52     SkASSERT(gm);
     53     SkISize size = gm->getISize();
     54     SkBitmap control, experimental;
     55     control.allocN32Pixels(size.width(), size.height());
     56     experimental.allocN32Pixels(size.width(), size.height());
     57     do_gm(&control, gm, {0, 0});
     58     for (int i = 0; i < kNumberOfJitters; ++i) {
     59         float angle = i * (6.2831853f / kNumberOfJitters) + kPhase;
     60         do_gm(&experimental, gm, SkPoint{kJitterMagnitude * cosf(angle),
     61                                          kJitterMagnitude * sinf(angle)});
     62         SkQP::RenderOutcome result = skqp::Check(
     63                 control.pixmap(), control.pixmap(), experimental.pixmap(),
     64                 kSkiaSkqpGlobalErrorTolerance, nullptr);
     65         if (result.fTotalError > 0) {
     66             return false;
     67         }
     68     }
     69     return true;
     70 }
     71 
     72 static bool do_this_test(const char* name,
     73                     const std::vector<std::string>& doNotRun,
     74                     const std::vector<std::string>& testOnlyThese) {
     75     for (const std::string& bad : doNotRun) {
     76         if (bad == name) {
     77             return false;
     78         }
     79     }
     80     for (const std::string& good : testOnlyThese) {
     81         if (good == name) {
     82             return true;
     83         }
     84     }
     85     return testOnlyThese.empty();
     86 }
     87 
     88 
     89 int main(int argc, char** argv) {
     90     std::vector<std::string> doNotRun;
     91     std::vector<std::string> testOnlyThese;
     92     if (argc > 1) {
     93         std::ifstream ifs(argv[1]);
     94         if (ifs.is_open()) {
     95             std::string str;
     96             while (std::getline(ifs, str)) {
     97                 doNotRun.push_back(str);
     98             }
     99         }
    100     }
    101     if (argc > 2) {
    102         for (int i = 2; i < argc; ++i) {
    103             testOnlyThese.emplace_back(argv[i]);
    104         }
    105     }
    106     SkGraphics::Init();
    107     std::mutex mutex;
    108     std::vector<std::string> goodResults;
    109     std::vector<std::string> badResults;
    110 
    111     int total = 0;
    112     SkSemaphore semaphore;
    113     auto executor = SkExecutor::MakeFIFOThreadPool();
    114     for (skiagm::GMFactory factory : skiagm::GMRegistry::Range()) {
    115         ++total;
    116         executor->add([factory, &mutex, &goodResults, &badResults,
    117                        &semaphore, &doNotRun, &testOnlyThese](){
    118             std::unique_ptr<skiagm::GM> gm(factory(nullptr));
    119             const char* name = gm->getName();
    120             if (do_this_test(name, doNotRun, testOnlyThese)) {
    121                 bool success = test_jitter(gm.get());
    122                 std::lock_guard<std::mutex> lock(mutex);
    123                 if (success) {
    124                     goodResults.emplace_back(name);
    125                 } else {
    126                     badResults.emplace_back(name);
    127                 }
    128                 fputc('.', stderr);
    129                 fflush(stderr);
    130             }
    131             semaphore.signal();
    132         });
    133     }
    134     while (total-- > 0) { semaphore.wait(); }
    135     fputc('\n', stderr);
    136     fflush(stderr);
    137     std::sort(goodResults.begin(), goodResults.end());
    138     std::sort(badResults.begin(), badResults.end());
    139     std::ofstream good("good.txt");
    140     std::ofstream bad("bad.txt");
    141     for (const std::string& s : goodResults) { good << s << '\n'; }
    142     for (const std::string& s : badResults) { bad << s << '\n'; }
    143     fprintf(stderr, "good = %u\nbad = %u\n\n",
    144             (unsigned)goodResults.size(), (unsigned)badResults.size());
    145 }
    146