1 /*
2 * Copyright 2018 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "tools/skqp/src/skqp.h"
9
10 #include "gm/gm.h"
11 #include "include/core/SkFontStyle.h"
12 #include "include/core/SkGraphics.h"
13 #include "include/core/SkStream.h"
14 #include "include/core/SkSurface.h"
15 #include "include/encode/SkPngEncoder.h"
16 #include "include/gpu/GrContextOptions.h"
17 #include "include/gpu/GrDirectContext.h"
18 #include "include/private/SkImageInfoPriv.h"
19 #include "src/core/SkFontMgrPriv.h"
20 #include "src/core/SkOSFile.h"
21 #include "src/core/SkStreamPriv.h"
22 #include "src/utils/SkOSPath.h"
23 #include "tests/Test.h"
24 #include "tools/fonts/TestFontMgr.h"
25 #ifdef SK_GL
26 #include "tools/gpu/gl/GLTestContext.h"
27 #endif
28 #ifdef SK_VULKAN
29 #include "tools/gpu/vk/VkTestContext.h"
30 #endif
31
32 #include <limits.h>
33 #include <algorithm>
34 #include <cinttypes>
35 #include <sstream>
36
37 #include "tools/skqp/src/skqp_model.h"
38
39 #define IMAGES_DIRECTORY_PATH "images"
40 #define PATH_MAX_PNG "max.png"
41 #define PATH_MIN_PNG "min.png"
42 #define PATH_IMG_PNG "image.png"
43 #define PATH_ERR_PNG "errors.png"
44 #define PATH_MODEL "model"
45
46 static constexpr char kRenderTestCSVReport[] = "out.csv";
47 static constexpr char kRenderTestReportPath[] = "report.html";
48 static constexpr char kDefaultRenderTestsPath[] = "skqp/rendertests.txt";
49 static constexpr char kUnitTestReportPath[] = "unit_tests.txt";
50 static constexpr char kUnitTestsPath[] = "skqp/unittests.txt";
51
52 // Kind of like Python's readlines(), but without any allocation.
53 // Calls f() on each line.
54 // F is [](const char*, size_t) -> void
55 template <typename F>
readlines(const void * data,size_t size,F f)56 static void readlines(const void* data, size_t size, F f) {
57 const char* start = (const char*)data;
58 const char* end = start + size;
59 const char* ptr = start;
60 while (ptr < end) {
61 while (*ptr++ != '\n' && ptr < end) {}
62 size_t len = ptr - start;
63 f(start, len);
64 start = ptr;
65 }
66 }
67
get_unit_tests(SkQPAssetManager * mgr,std::vector<SkQP::UnitTest> * unitTests)68 static void get_unit_tests(SkQPAssetManager* mgr, std::vector<SkQP::UnitTest>* unitTests) {
69 std::unordered_set<std::string> testset;
70 auto insert = [&testset](const char* s, size_t l) {
71 SkASSERT(l > 1) ;
72 if (l > 0 && s[l - 1] == '\n') { // strip line endings.
73 --l;
74 }
75 if (l > 0) { // only add non-empty strings.
76 testset.insert(std::string(s, l));
77 }
78 };
79 if (sk_sp<SkData> dat = mgr->open(kUnitTestsPath)) {
80 readlines(dat->data(), dat->size(), insert);
81 }
82 for (const skiatest::Test& test : skiatest::TestRegistry::Range()) {
83 if ((testset.empty() || testset.count(std::string(test.fName)) > 0) && test.fNeedsGpu) {
84 unitTests->push_back(&test);
85 }
86 }
87 auto lt = [](SkQP::UnitTest u, SkQP::UnitTest v) { return strcmp(u->fName, v->fName) < 0; };
88 std::sort(unitTests->begin(), unitTests->end(), lt);
89 }
90
get_render_tests(SkQPAssetManager * mgr,const char * renderTestsIn,std::vector<SkQP::GMFactory> * gmlist,std::unordered_map<std::string,int64_t> * gmThresholds)91 static void get_render_tests(SkQPAssetManager* mgr,
92 const char *renderTestsIn,
93 std::vector<SkQP::GMFactory>* gmlist,
94 std::unordered_map<std::string, int64_t>* gmThresholds) {
95 // Runs all render tests if the |renderTests| file can't be found or is empty.
96 const char *renderTests = (renderTestsIn && renderTestsIn[0]) ?
97 renderTestsIn : kDefaultRenderTestsPath;
98 auto insert = [gmThresholds](const char* s, size_t l) {
99 SkASSERT(l > 1) ;
100 if (l > 0 && s[l - 1] == '\n') { // strip line endings.
101 --l;
102 }
103 if (l == 0) {
104 return;
105 }
106 const char* end = s + l;
107 const char* ptr = s;
108 constexpr char kDelimeter = ',';
109 while (ptr < end && *ptr != kDelimeter) { ++ptr; }
110 if (ptr + 1 >= end) {
111 SkASSERT(false); // missing delimeter
112 return;
113 }
114 std::string key(s, ptr - s);
115 ++ptr; // skip delimeter
116 std::string number(ptr, end - ptr); // null-terminated copy.
117 int64_t value = 0;
118 if (1 != sscanf(number.c_str(), "%" SCNd64 , &value)) {
119 SkASSERT(false); // Not a number
120 return;
121 }
122 gmThresholds->insert({std::move(key), value}); // (*gmThresholds)[s] = value;
123 };
124 if (sk_sp<SkData> dat = mgr->open(renderTests)) {
125 readlines(dat->data(), dat->size(), insert);
126 }
127 using GmAndName = std::pair<SkQP::GMFactory, std::string>;
128 std::vector<GmAndName> gmsWithNames;
129 for (skiagm::GMFactory f : skiagm::GMRegistry::Range()) {
130 std::string name = SkQP::GetGMName(f);
131 if ((gmThresholds->empty() || gmThresholds->count(name) > 0)) {
132 gmsWithNames.push_back(std::make_pair(f, std::move(name)));
133 }
134 }
135 std::sort(gmsWithNames.begin(), gmsWithNames.end(),
136 [](GmAndName u, GmAndName v) { return u.second < v.second; });
137 gmlist->reserve(gmsWithNames.size());
138 for (const GmAndName& gmn : gmsWithNames) {
139 gmlist->push_back(gmn.first);
140 }
141 }
142
make_test_context(SkQP::SkiaBackend backend)143 static std::unique_ptr<sk_gpu_test::TestContext> make_test_context(SkQP::SkiaBackend backend) {
144 using U = std::unique_ptr<sk_gpu_test::TestContext>;
145 switch (backend) {
146 // TODO(halcanary): Fuchsia will have SK_SUPPORT_GPU and SK_VULKAN, but *not* SK_GL.
147 #ifdef SK_GL
148 case SkQP::SkiaBackend::kGL:
149 return U(sk_gpu_test::CreatePlatformGLTestContext(kGL_GrGLStandard, nullptr));
150 case SkQP::SkiaBackend::kGLES:
151 return U(sk_gpu_test::CreatePlatformGLTestContext(kGLES_GrGLStandard, nullptr));
152 #endif
153 #ifdef SK_VULKAN
154 case SkQP::SkiaBackend::kVulkan:
155 return U(sk_gpu_test::CreatePlatformVkTestContext(nullptr));
156 #endif
157 default:
158 return nullptr;
159 }
160 }
161
context_options(skiagm::GM * gm=nullptr)162 static GrContextOptions context_options(skiagm::GM* gm = nullptr) {
163 GrContextOptions grContextOptions;
164 grContextOptions.fAllowPathMaskCaching = true;
165 grContextOptions.fDisableDriverCorrectnessWorkarounds = true;
166 if (gm) {
167 gm->modifyGrContextOptions(&grContextOptions);
168 }
169 return grContextOptions;
170 }
171
get_backends()172 static std::vector<SkQP::SkiaBackend> get_backends() {
173 std::vector<SkQP::SkiaBackend> result;
174 SkQP::SkiaBackend backends[] = {
175 #ifdef SK_GL
176 #ifndef SK_BUILD_FOR_ANDROID
177 SkQP::SkiaBackend::kGL, // Used for testing on desktop machines.
178 #endif
179 SkQP::SkiaBackend::kGLES,
180 #endif // SK_GL
181 #ifdef SK_VULKAN
182 SkQP::SkiaBackend::kVulkan,
183 #endif
184 };
185 for (SkQP::SkiaBackend backend : backends) {
186 std::unique_ptr<sk_gpu_test::TestContext> testCtx = make_test_context(backend);
187 if (testCtx) {
188 testCtx->makeCurrent();
189 if (nullptr != testCtx->makeContext(context_options())) {
190 result.push_back(backend);
191 }
192 }
193 }
194 SkASSERT_RELEASE(result.size() > 0);
195 return result;
196 }
197
print_backend_info(const char * dstPath,const std::vector<SkQP::SkiaBackend> & backends)198 static void print_backend_info(const char* dstPath,
199 const std::vector<SkQP::SkiaBackend>& backends) {
200 #ifdef SK_ENABLE_DUMP_GPU
201 SkFILEWStream out(dstPath);
202 out.writeText("[\n");
203 for (SkQP::SkiaBackend backend : backends) {
204 if (std::unique_ptr<sk_gpu_test::TestContext> testCtx = make_test_context(backend)) {
205 testCtx->makeCurrent();
206 if (sk_sp<GrDirectContext> ctx = testCtx->makeContext(context_options())) {
207 SkString info = ctx->dump();
208 // remove null
209 out.write(info.c_str(), info.size());
210 out.writeText(",\n");
211 }
212 }
213 }
214 out.writeText("]\n");
215 #endif
216 }
217
encode_png(const SkBitmap & src,const std::string & dst)218 static void encode_png(const SkBitmap& src, const std::string& dst) {
219 SkFILEWStream wStream(dst.c_str());
220 SkPngEncoder::Options options;
221 bool success = wStream.isValid() && SkPngEncoder::Encode(&wStream, src.pixmap(), options);
222 SkASSERT_RELEASE(success);
223 }
224
write_to_file(const sk_sp<SkData> & src,const std::string & dst)225 static void write_to_file(const sk_sp<SkData>& src, const std::string& dst) {
226 SkFILEWStream wStream(dst.c_str());
227 bool success = wStream.isValid() && wStream.write(src->data(), src->size());
228 SkASSERT_RELEASE(success);
229 }
230
231 ////////////////////////////////////////////////////////////////////////////////
232
GetBackendName(SkQP::SkiaBackend b)233 const char* SkQP::GetBackendName(SkQP::SkiaBackend b) {
234 switch (b) {
235 case SkQP::SkiaBackend::kGL: return "gl";
236 case SkQP::SkiaBackend::kGLES: return "gles";
237 case SkQP::SkiaBackend::kVulkan: return "vk";
238 }
239 return "";
240 }
241
GetGMName(SkQP::GMFactory f)242 std::string SkQP::GetGMName(SkQP::GMFactory f) {
243 std::unique_ptr<skiagm::GM> gm(f ? f() : nullptr);
244 return std::string(gm ? gm->getName() : "");
245 }
246
GetUnitTestName(SkQP::UnitTest t)247 const char* SkQP::GetUnitTestName(SkQP::UnitTest t) { return t->fName; }
248
SkQP()249 SkQP::SkQP() {}
250
~SkQP()251 SkQP::~SkQP() {}
252
init(SkQPAssetManager * am,const char * renderTests,const char * reportDirectory)253 void SkQP::init(SkQPAssetManager* am, const char* renderTests, const char* reportDirectory) {
254 SkASSERT_RELEASE(!fAssetManager);
255 SkASSERT_RELEASE(am);
256 fAssetManager = am;
257 fReportDirectory = reportDirectory;
258
259 SkGraphics::Init();
260 gSkFontMgr_DefaultFactory = &ToolUtils::MakePortableFontMgr;
261
262 get_render_tests(fAssetManager, renderTests, &fGMs, &fGMThresholds);
263 /* If the file "skqp/unittests.txt" does not exist or is empty, run all gpu
264 unit tests. Otherwise only run tests mentioned in that file. */
265 get_unit_tests(fAssetManager, &fUnitTests);
266 fSupportedBackends = get_backends();
267
268 print_backend_info((fReportDirectory + "/grdump.txt").c_str(), fSupportedBackends);
269 }
270
evaluateGM(SkQP::SkiaBackend backend,SkQP::GMFactory gmFact)271 std::tuple<SkQP::RenderOutcome, std::string> SkQP::evaluateGM(SkQP::SkiaBackend backend,
272 SkQP::GMFactory gmFact) {
273 SkASSERT_RELEASE(fAssetManager);
274 static constexpr SkQP::RenderOutcome kError = {INT_MAX, INT_MAX, INT64_MAX};
275 static constexpr SkQP::RenderOutcome kPass = {0, 0, 0};
276
277 std::unique_ptr<sk_gpu_test::TestContext> testCtx = make_test_context(backend);
278 if (!testCtx) {
279 return std::make_tuple(kError, "Skia Failure: test context");
280 }
281 testCtx->makeCurrent();
282
283 SkASSERT(gmFact);
284 std::unique_ptr<skiagm::GM> gm(gmFact());
285 SkASSERT(gm);
286 const char* const name = gm->getName();
287 const SkISize size = gm->getISize();
288 const int w = size.width();
289 const int h = size.height();
290 const SkImageInfo info =
291 SkImageInfo::Make(w, h, skqp::kColorType, kPremul_SkAlphaType, nullptr);
292 const SkSurfaceProps props(0, kRGB_H_SkPixelGeometry);
293
294 sk_sp<SkSurface> surf = SkSurface::MakeRenderTarget(
295 testCtx->makeContext(context_options(gm.get())).get(),
296 SkBudgeted::kNo, info, 0, &props);
297 if (!surf) {
298 return std::make_tuple(kError, "Skia Failure: gr-context");
299 }
300 gm->draw(surf->getCanvas());
301
302 SkBitmap image;
303 image.allocPixels(SkImageInfo::Make(w, h, skqp::kColorType, skqp::kAlphaType));
304
305 // SkColorTypeBytesPerPixel should be constexpr, but is not.
306 SkASSERT(SkColorTypeBytesPerPixel(skqp::kColorType) == sizeof(uint32_t));
307 // Call readPixels because we need to compare pixels.
308 if (!surf->readPixels(image.pixmap(), 0, 0)) {
309 return std::make_tuple(kError, "Skia Failure: read pixels");
310 }
311 int64_t passingThreshold = fGMThresholds.empty() ? -1 : fGMThresholds[std::string(name)];
312
313 if (-1 == passingThreshold) {
314 return std::make_tuple(kPass, "");
315 }
316 skqp::ModelResult modelResult =
317 skqp::CheckAgainstModel(name, image.pixmap(), fAssetManager);
318
319 if (!modelResult.fErrorString.empty()) {
320 return std::make_tuple(kError, std::move(modelResult.fErrorString));
321 }
322 fRenderResults.push_back(SkQP::RenderResult{backend, gmFact, modelResult.fOutcome});
323 if (modelResult.fOutcome.fMaxError <= passingThreshold) {
324 return std::make_tuple(kPass, "");
325 }
326 std::string imagesDirectory = fReportDirectory + "/" IMAGES_DIRECTORY_PATH;
327 if (!sk_mkdir(imagesDirectory.c_str())) {
328 SkDebugf("ERROR: sk_mkdir('%s');\n", imagesDirectory.c_str());
329 return std::make_tuple(modelResult.fOutcome, "");
330 }
331 std::ostringstream tmp;
332 tmp << imagesDirectory << '/' << SkQP::GetBackendName(backend) << '_' << name << '_';
333 std::string imagesPathPrefix1 = tmp.str();
334 tmp = std::ostringstream();
335 tmp << imagesDirectory << '/' << PATH_MODEL << '_' << name << '_';
336 std::string imagesPathPrefix2 = tmp.str();
337 encode_png(image, imagesPathPrefix1 + PATH_IMG_PNG);
338 encode_png(modelResult.fErrors, imagesPathPrefix1 + PATH_ERR_PNG);
339 write_to_file(modelResult.fMaxPng, imagesPathPrefix2 + PATH_MAX_PNG);
340 write_to_file(modelResult.fMinPng, imagesPathPrefix2 + PATH_MIN_PNG);
341 return std::make_tuple(modelResult.fOutcome, "");
342 }
343
executeTest(SkQP::UnitTest test)344 std::vector<std::string> SkQP::executeTest(SkQP::UnitTest test) {
345 SkASSERT_RELEASE(fAssetManager);
346 struct : public skiatest::Reporter {
347 std::vector<std::string> fErrors;
348 void reportFailed(const skiatest::Failure& failure) override {
349 SkString desc = failure.toString();
350 fErrors.push_back(std::string(desc.c_str(), desc.size()));
351 }
352 } r;
353 GrContextOptions options;
354 options.fDisableDriverCorrectnessWorkarounds = true;
355 if (test->fContextOptionsProc) {
356 test->fContextOptionsProc(&options);
357 }
358 test->fProc(&r, options);
359 fUnitTestResults.push_back(UnitTestResult{test, r.fErrors});
360 return r.fErrors;
361 }
362
363 ////////////////////////////////////////////////////////////////////////////////
364
365 static constexpr char kDocHead[] =
366 "<!doctype html>\n"
367 "<html lang=\"en\">\n"
368 "<head>\n"
369 "<meta charset=\"UTF-8\">\n"
370 "<title>SkQP Report</title>\n"
371 "<style>\n"
372 "img { max-width:48%; border:1px green solid;\n"
373 " image-rendering: pixelated;\n"
374 " background-image:url('data:image/png;base64,iVBORw0KGgoA"
375 "AAANSUhEUgAAABAAAAAQCAAAAAA6mKC9AAAAAXNSR0IArs4c6QAAAAJiS0dEAP+H"
376 "j8y/AAAACXBIWXMAAA7DAAAOwwHHb6hkAAAAB3RJTUUH3gUBEi4DGRAQYgAAAB1J"
377 "REFUGNNjfMoAAVJQmokBDdBHgPE/lPFsYN0BABdaAwN6tehMAAAAAElFTkSuQmCC"
378 "'); }\n"
379 "</style>\n"
380 "<script>\n"
381 "function ce(t) { return document.createElement(t); }\n"
382 "function ct(n) { return document.createTextNode(n); }\n"
383 "function ac(u,v) { return u.appendChild(v); }\n"
384 "function br(u) { ac(u, ce(\"br\")); }\n"
385 "function ma(s, c) { var a = ce(\"a\"); a.href = s; ac(a, c); return a; }\n"
386 "function f(backend, gm, e1, e2, e3) {\n"
387 " var b = ce(\"div\");\n"
388 " var x = ce(\"h2\");\n"
389 " var t = backend + \"_\" + gm;\n"
390 " ac(x, ct(t));\n"
391 " ac(b, x);\n"
392 " ac(b, ct(\"backend: \" + backend));\n"
393 " br(b);\n"
394 " ac(b, ct(\"gm name: \" + gm));\n"
395 " br(b);\n"
396 " ac(b, ct(\"maximum error: \" + e1));\n"
397 " br(b);\n"
398 " ac(b, ct(\"bad pixel counts: \" + e2));\n"
399 " br(b);\n"
400 " ac(b, ct(\"total error: \" + e3));\n"
401 " br(b);\n"
402 " var q = \"" IMAGES_DIRECTORY_PATH "/\" + backend + \"_\" + gm + \"_\";\n"
403 " var p = \"" IMAGES_DIRECTORY_PATH "/" PATH_MODEL "_\" + gm + \"_\";\n"
404 " var i = ce(\"img\");\n"
405 " i.src = q + \"" PATH_IMG_PNG "\";\n"
406 " i.alt = \"img\";\n"
407 " ac(b, ma(i.src, i));\n"
408 " i = ce(\"img\");\n"
409 " i.src = q + \"" PATH_ERR_PNG "\";\n"
410 " i.alt = \"err\";\n"
411 " ac(b, ma(i.src, i));\n"
412 " br(b);\n"
413 " ac(b, ct(\"Expectation: \"));\n"
414 " ac(b, ma(p + \"" PATH_MAX_PNG "\", ct(\"max\")));\n"
415 " ac(b, ct(\" | \"));\n"
416 " ac(b, ma(p + \"" PATH_MIN_PNG "\", ct(\"min\")));\n"
417 " ac(b, ce(\"hr\"));\n"
418 " b.id = backend + \":\" + gm;\n"
419 " ac(document.body, b);\n"
420 " l = ce(\"li\");\n"
421 " ac(l, ct(\"[\" + e3 + \"] \"));\n"
422 " ac(l, ma(\"#\" + backend +\":\"+ gm , ct(t)));\n"
423 " ac(document.getElementById(\"toc\"), l);\n"
424 "}\n"
425 "function main() {\n";
426
427 static constexpr char kDocMiddle[] =
428 "}\n"
429 "</script>\n"
430 "</head>\n"
431 "<body onload=\"main()\">\n"
432 "<h1>SkQP Report</h1>\n";
433
434 static constexpr char kDocTail[] =
435 "<ul id=\"toc\"></ul>\n"
436 "<hr>\n"
437 "<p>Left image: test result<br>\n"
438 "Right image: errors (white = no error, black = smallest error, red = biggest error; "
439 "other errors are a color between black and red.)</p>\n"
440 "<hr>\n"
441 "</body>\n"
442 "</html>\n";
443
444 template <typename T>
write(SkWStream * wStream,const T & text)445 inline void write(SkWStream* wStream, const T& text) {
446 wStream->write(text.c_str(), text.size());
447 }
448
makeReport()449 void SkQP::makeReport() {
450 SkASSERT_RELEASE(fAssetManager);
451 int glesErrorCount = 0, vkErrorCount = 0, gles = 0, vk = 0;
452
453 if (!sk_isdir(fReportDirectory.c_str())) {
454 SkDebugf("Report destination does not exist: '%s'\n", fReportDirectory.c_str());
455 return;
456 }
457 SkFILEWStream csvOut(SkOSPath::Join(fReportDirectory.c_str(), kRenderTestCSVReport).c_str());
458 SkFILEWStream htmOut(SkOSPath::Join(fReportDirectory.c_str(), kRenderTestReportPath).c_str());
459 SkASSERT_RELEASE(csvOut.isValid() && htmOut.isValid());
460 htmOut.writeText(kDocHead);
461 for (const SkQP::RenderResult& run : fRenderResults) {
462 switch (run.fBackend) {
463 case SkQP::SkiaBackend::kGLES: ++gles; break;
464 case SkQP::SkiaBackend::kVulkan: ++vk; break;
465 default: break;
466 }
467 const char* backendName = SkQP::GetBackendName(run.fBackend);
468 std::string gmName = SkQP::GetGMName(run.fGM);
469 const SkQP::RenderOutcome& outcome = run.fOutcome;
470 auto str = SkStringPrintf("\"%s\",\"%s\",%d,%d,%" PRId64, backendName, gmName.c_str(),
471 outcome.fMaxError, outcome.fBadPixelCount, outcome.fTotalError);
472 write(&csvOut, SkStringPrintf("%s\n", str.c_str()));
473
474 int64_t passingThreshold = fGMThresholds.empty() ? 0 : fGMThresholds[gmName];
475 if (passingThreshold == -1 || outcome.fMaxError <= passingThreshold) {
476 continue;
477 }
478 write(&htmOut, SkStringPrintf(" f(%s);\n", str.c_str()));
479 switch (run.fBackend) {
480 case SkQP::SkiaBackend::kGLES: ++glesErrorCount; break;
481 case SkQP::SkiaBackend::kVulkan: ++vkErrorCount; break;
482 default: break;
483 }
484 }
485 htmOut.writeText(kDocMiddle);
486 write(&htmOut, SkStringPrintf("<p>gles errors: %d (of %d)</br>\n"
487 "vk errors: %d (of %d)</p>\n",
488 glesErrorCount, gles, vkErrorCount, vk));
489 htmOut.writeText(kDocTail);
490 SkFILEWStream unitOut(SkOSPath::Join(fReportDirectory.c_str(), kUnitTestReportPath).c_str());
491 SkASSERT_RELEASE(unitOut.isValid());
492 for (const SkQP::UnitTestResult& result : fUnitTestResults) {
493 unitOut.writeText(GetUnitTestName(result.fUnitTest));
494 if (result.fErrors.empty()) {
495 unitOut.writeText(" PASSED\n* * *\n");
496 } else {
497 write(&unitOut, SkStringPrintf(" FAILED (%zu errors)\n", result.fErrors.size()));
498 for (const std::string& err : result.fErrors) {
499 write(&unitOut, err);
500 unitOut.newline();
501 }
502 unitOut.writeText("* * *\n");
503 }
504 }
505 }
506