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