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