1 /*
2 * Copyright 2013 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 "dm/DMJsonWriter.h"
9 #include "dm/DMSrcSink.h"
10 #include "gm/verifiers/gmverifier.h"
11 #include "include/codec/SkCodec.h"
12 #include "include/core/SkBBHFactory.h"
13 #include "include/core/SkColorPriv.h"
14 #include "include/core/SkColorSpace.h"
15 #include "include/core/SkData.h"
16 #include "include/core/SkDocument.h"
17 #include "include/core/SkFontMgr.h"
18 #include "include/core/SkGraphics.h"
19 #include "include/ports/SkTypeface_win.h"
20 #include "include/private/SkChecksum.h"
21 #include "include/private/SkHalf.h"
22 #include "include/private/SkSpinlock.h"
23 #include "include/private/SkTHash.h"
24 #include "src/core/SkColorSpacePriv.h"
25 #include "src/core/SkLeanWindows.h"
26 #include "src/core/SkMD5.h"
27 #include "src/core/SkOSFile.h"
28 #include "src/core/SkTaskGroup.h"
29 #include "src/utils/SkOSPath.h"
30 #include "tests/Test.h"
31 #include "tools/AutoreleasePool.h"
32 #include "tools/HashAndEncode.h"
33 #include "tools/ProcStats.h"
34 #include "tools/Resources.h"
35 #include "tools/ToolUtils.h"
36 #include "tools/flags/CommonFlags.h"
37 #include "tools/flags/CommonFlagsConfig.h"
38 #include "tools/ios_utils.h"
39 #include "tools/trace/ChromeTracingTracer.h"
40 #include "tools/trace/EventTracingPriv.h"
41 #include "tools/trace/SkDebugfTracer.h"
42
43 #include <memory>
44 #include <vector>
45
46 #include <stdlib.h>
47
48 #ifndef SK_BUILD_FOR_WIN
49 #include <unistd.h>
50 #endif
51
52 #if defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) && defined(SK_HAS_HEIF_LIBRARY)
53 #include <binder/IPCThreadState.h>
54 #endif
55
56 #if defined(SK_BUILD_FOR_MAC)
57 #include "include/utils/mac/SkCGUtils.h"
58 #include "src/utils/mac/SkUniqueCFRef.h"
59 #endif
60
61 extern bool gSkForceRasterPipelineBlitter;
62 extern bool gUseSkVMBlitter;
63 extern bool gSkVMAllowJIT;
64
65 static DEFINE_string(src, "tests gm skp mskp lottie rive svg image colorImage",
66 "Source types to test.");
67 static DEFINE_bool(nameByHash, false,
68 "If true, write to FLAGS_writePath[0]/<hash>.png instead of "
69 "to FLAGS_writePath[0]/<config>/<sourceType>/<sourceOptions>/<name>.png");
70 static DEFINE_bool2(pathOpsExtended, x, false, "Run extended pathOps tests.");
71 static DEFINE_string(matrix, "1 0 0 1",
72 "2x2 scale+skew matrix to apply or upright when using "
73 "'matrix' or 'upright' in config.");
74
75 static DEFINE_string(skip, "",
76 "Space-separated config/src/srcOptions/name quadruples to skip. "
77 "'_' matches anything. '~' negates the match. E.g. \n"
78 "'--skip gpu skp _ _' will skip all SKPs drawn into the gpu config.\n"
79 "'--skip gpu skp _ _ 8888 gm _ aarects' will also skip the aarects GM on 8888.\n"
80 "'--skip ~8888 svg _ svgparse_' blocks non-8888 SVGs that contain \"svgparse_\" in "
81 "the name.");
82
83 static DEFINE_string2(readPath, r, "",
84 "If set check for equality with golden results in this directory.");
85 DEFINE_string2(writePath, w, "", "If set, write bitmaps here as .pngs.");
86
87
88 static DEFINE_string(uninterestingHashesFile, "",
89 "File containing a list of uninteresting hashes. If a result hashes to something in "
90 "this list, no image is written for that result.");
91
92 static DEFINE_int(shards, 1, "We're splitting source data into this many shards.");
93 static DEFINE_int(shard, 0, "Which shard do I run?");
94
95 static DEFINE_string(mskps, "", "Directory to read mskps from, or a single mskp file.");
96 static DEFINE_bool(forceRasterPipeline, false, "sets gSkForceRasterPipelineBlitter");
97 static DEFINE_bool(skvm, false, "sets gUseSkVMBlitter");
98 static DEFINE_bool(jit, true, "sets gSkVMAllowJIT");
99
100 static DEFINE_string(bisect, "",
101 "Pair of: SKP file to bisect, followed by an l/r bisect trail string (e.g., 'lrll'). The "
102 "l/r trail specifies which half to keep at each step of a binary search through the SKP's "
103 "paths. An empty string performs no bisect. Only the SkPaths are bisected; all other draws "
104 "are thrown out. This is useful for finding a reduced repo case for path drawing bugs.");
105
106 static DEFINE_bool(ignoreSigInt, false, "ignore SIGINT signals during test execution");
107
108 static DEFINE_bool(checkF16, false, "Ensure that F16Norm pixels are clamped.");
109
110 static DEFINE_string(colorImages, "",
111 "List of images and/or directories to decode with color correction. "
112 "A directory with no images is treated as a fatal error.");
113
114 static DEFINE_bool2(veryVerbose, V, false, "tell individual tests to be verbose.");
115
116 static DEFINE_bool(cpu, true, "Run CPU-bound work?");
117 static DEFINE_bool(gpu, true, "Run GPU-bound work?");
118
119 static DEFINE_bool(dryRun, false,
120 "just print the tests that would be run, without actually running them.");
121
122 static DEFINE_string(images, "",
123 "List of images and/or directories to decode. A directory with no images"
124 " is treated as a fatal error.");
125
126 static DEFINE_bool(simpleCodec, false,
127 "Runs of a subset of the codec tests, "
128 "with no scaling or subsetting, always using the canvas color type.");
129
130 static DEFINE_string2(match, m, nullptr,
131 "[~][^]substring[$] [...] of name to run.\n"
132 "Multiple matches may be separated by spaces.\n"
133 "~ causes a matching name to always be skipped\n"
134 "^ requires the start of the name to match\n"
135 "$ requires the end of the name to match\n"
136 "^ and $ requires an exact match\n"
137 "If a name does not match any list entry,\n"
138 "it is skipped unless some list entry starts with ~");
139
140 static DEFINE_bool2(quiet, q, false, "if true, don't print status updates.");
141 static DEFINE_bool2(verbose, v, false, "enable verbose output from the test driver.");
142
143 static DEFINE_string(skps, "skps", "Directory to read skps from.");
144 static DEFINE_string(lotties, "lotties", "Directory to read (Bodymovin) jsons from.");
145 static DEFINE_string(rives, "rives", "Directory to read Rive/Flare files from.");
146 static DEFINE_string(svgs, "", "Directory to read SVGs from, or a single SVG file.");
147
148 static DEFINE_int_2(threads, j, -1,
149 "Run threadsafe tests on a threadpool with this many extra threads, "
150 "defaulting to one extra thread per core.");
151
152 static DEFINE_string(key, "",
153 "Space-separated key/value pairs to add to JSON identifying this builder.");
154 static DEFINE_string(properties, "",
155 "Space-separated key/value pairs to add to JSON identifying this run.");
156
157 static DEFINE_bool(rasterize_pdf, false, "Rasterize PDFs when possible.");
158
159 static DEFINE_bool(runVerifiers, false,
160 "if true, run SkQP-style verification of GM-produced images.");
161
162 #if defined(__MSVC_RUNTIME_CHECKS)
163 #include <rtcapi.h>
RuntimeCheckErrorFunc(int errorType,const char * filename,int linenumber,const char * moduleName,const char * fmt,...)164 int RuntimeCheckErrorFunc(int errorType, const char* filename, int linenumber,
165 const char* moduleName, const char* fmt, ...) {
166 va_list args;
167 va_start(args, fmt);
168 vfprintf(stderr, fmt, args);
169 va_end(args);
170
171 SkDebugf("Line #%d\nFile: %s\nModule: %s\n",
172 linenumber, filename ? filename : "Unknown", moduleName ? moduleName : "Unknwon");
173 return 1;
174 }
175 #endif
176
177 using namespace DM;
178 using sk_gpu_test::GrContextFactory;
179 using sk_gpu_test::GLTestContext;
180 using sk_gpu_test::ContextInfo;
181
182 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
183
rec2020()184 static sk_sp<SkColorSpace> rec2020() {
185 return SkColorSpace::MakeRGB(SkNamedTransferFn::kRec2020, SkNamedGamut::kRec2020);
186 }
187
188 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
189
190 static FILE* gVLog;
191
192 template <typename... Args>
vlog(const char * fmt,Args &&...args)193 static void vlog(const char* fmt, Args&&... args) {
194 if (gVLog) {
195 fprintf(gVLog, fmt, args...);
196 fflush(gVLog);
197 }
198 }
199
200 template <typename... Args>
info(const char * fmt,Args &&...args)201 static void info(const char* fmt, Args&&... args) {
202 vlog(fmt, args...);
203 if (!FLAGS_quiet) {
204 printf(fmt, args...);
205 }
206 }
info(const char * fmt)207 static void info(const char* fmt) {
208 if (!FLAGS_quiet) {
209 printf("%s", fmt); // Clang warns printf(fmt) is insecure.
210 }
211 }
212
213 static SkTArray<SkString>* gFailures = new SkTArray<SkString>;
214
fail(const SkString & err)215 static void fail(const SkString& err) {
216 static SkSpinlock mutex;
217 SkAutoSpinlock lock(mutex);
218 SkDebugf("\n\nFAILURE: %s\n\n", err.c_str());
219 gFailures->push_back(err);
220 }
221
222 struct Running {
223 SkString id;
224 SkThreadID thread;
225
dumpRunning226 void dump() const {
227 info("\t%s\n", id.c_str());
228 }
229 };
230
dump_json()231 static void dump_json() {
232 if (!FLAGS_writePath.isEmpty()) {
233 JsonWriter::DumpJson(FLAGS_writePath[0], FLAGS_key, FLAGS_properties);
234 }
235 }
236
237 // We use a spinlock to make locking this in a signal handler _somewhat_ safe.
238 static SkSpinlock* gMutex = new SkSpinlock;
239 static int gPending;
240 static SkTArray<Running>* gRunning = new SkTArray<Running>;
241
done(const char * config,const char * src,const char * srcOptions,const char * name)242 static void done(const char* config, const char* src, const char* srcOptions, const char* name) {
243 SkString id = SkStringPrintf("%s %s %s %s", config, src, srcOptions, name);
244 vlog("done %s\n", id.c_str());
245 int pending;
246 {
247 SkAutoSpinlock lock(*gMutex);
248 for (int i = 0; i < gRunning->count(); i++) {
249 if (gRunning->at(i).id == id) {
250 gRunning->removeShuffle(i);
251 break;
252 }
253 }
254 pending = --gPending;
255 }
256
257 // We write out dm.json file and print out a progress update every once in a while.
258 // Notice this also handles the final dm.json and progress update when pending == 0.
259 if (pending % 500 == 0) {
260 dump_json();
261
262 int curr = sk_tools::getCurrResidentSetSizeMB(),
263 peak = sk_tools::getMaxResidentSetSizeMB();
264
265 SkAutoSpinlock lock(*gMutex);
266 info("\n%dMB RAM, %dMB peak, %d queued, %d active:\n",
267 curr, peak, gPending - gRunning->count(), gRunning->count());
268 for (auto& task : *gRunning) {
269 task.dump();
270 }
271 }
272 }
273
start(const char * config,const char * src,const char * srcOptions,const char * name)274 static void start(const char* config, const char* src, const char* srcOptions, const char* name) {
275 SkString id = SkStringPrintf("%s %s %s %s", config, src, srcOptions, name);
276 vlog("start %s\n", id.c_str());
277 SkAutoSpinlock lock(*gMutex);
278 gRunning->push_back({id,SkGetThreadID()});
279 }
280
find_culprit()281 static void find_culprit() {
282 // Assumes gMutex is locked.
283 SkThreadID thisThread = SkGetThreadID();
284 for (auto& task : *gRunning) {
285 if (task.thread == thisThread) {
286 info("Likely culprit:\n");
287 task.dump();
288 }
289 }
290 }
291
292 #if defined(SK_BUILD_FOR_WIN)
crash_handler(EXCEPTION_POINTERS * e)293 static LONG WINAPI crash_handler(EXCEPTION_POINTERS* e) {
294 static const struct {
295 const char* name;
296 DWORD code;
297 } kExceptions[] = {
298 #define _(E) {#E, E}
299 _(EXCEPTION_ACCESS_VIOLATION),
300 _(EXCEPTION_BREAKPOINT),
301 _(EXCEPTION_INT_DIVIDE_BY_ZERO),
302 _(EXCEPTION_STACK_OVERFLOW),
303 // TODO: more?
304 #undef _
305 };
306
307 SkAutoSpinlock lock(*gMutex);
308
309 const DWORD code = e->ExceptionRecord->ExceptionCode;
310 info("\nCaught exception %u", code);
311 for (const auto& exception : kExceptions) {
312 if (exception.code == code) {
313 info(" %s", exception.name);
314 }
315 }
316 info(", was running:\n");
317 for (auto& task : *gRunning) {
318 task.dump();
319 }
320 find_culprit();
321 fflush(stdout);
322
323 // Execute default exception handler... hopefully, exit.
324 return EXCEPTION_EXECUTE_HANDLER;
325 }
326
setup_crash_handler()327 static void setup_crash_handler() {
328 SetUnhandledExceptionFilter(crash_handler);
329 }
330 #else
331 #include <signal.h>
332 #if !defined(SK_BUILD_FOR_ANDROID)
333 #include <execinfo.h>
334
335 #endif
336
max_of()337 static constexpr int max_of() { return 0; }
338 template <typename... Rest>
max_of(int x,Rest...rest)339 static constexpr int max_of(int x, Rest... rest) {
340 return x > max_of(rest...) ? x : max_of(rest...);
341 }
342
343 static void (*previous_handler[max_of(SIGABRT,SIGBUS,SIGFPE,SIGILL,SIGSEGV,SIGTERM)+1])(int);
344
crash_handler(int sig)345 static void crash_handler(int sig) {
346 SkAutoSpinlock lock(*gMutex);
347
348 info("\nCaught signal %d [%s] (%dMB RAM, peak %dMB), was running:\n",
349 sig, strsignal(sig),
350 sk_tools::getCurrResidentSetSizeMB(), sk_tools::getMaxResidentSetSizeMB());
351
352 for (auto& task : *gRunning) {
353 task.dump();
354 }
355 find_culprit();
356
357 #if !defined(SK_BUILD_FOR_ANDROID)
358 void* stack[128];
359 int count = backtrace(stack, SK_ARRAY_COUNT(stack));
360 char** symbols = backtrace_symbols(stack, count);
361 info("\nStack trace:\n");
362 for (int i = 0; i < count; i++) {
363 info(" %s\n", symbols[i]);
364 }
365 #endif
366 fflush(stdout);
367
368 if (sig == SIGINT && FLAGS_ignoreSigInt) {
369 info("Ignoring signal %d because of --ignoreSigInt.\n"
370 "This is probably a sign the bot is overloaded with work.\n", sig);
371 } else {
372 signal(sig, previous_handler[sig]);
373 raise(sig);
374 }
375 }
376
setup_crash_handler()377 static void setup_crash_handler() {
378 const int kSignals[] = { SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGINT, SIGSEGV, SIGTERM };
379 for (int sig : kSignals) {
380 previous_handler[sig] = signal(sig, crash_handler);
381 }
382 }
383 #endif
384
385 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
386
387 struct Gold : public SkString {
GoldGold388 Gold() : SkString("") {}
GoldGold389 Gold(const SkString& sink, const SkString& src,
390 const SkString& srcOptions, const SkString& name,
391 const SkString& md5)
392 : SkString("") {
393 this->append(sink);
394 this->append(src);
395 this->append(srcOptions);
396 this->append(name);
397 this->append(md5);
398 }
399 struct Hash {
operator ()Gold::Hash400 uint32_t operator()(const Gold& g) const {
401 return SkGoodHash()((const SkString&)g);
402 }
403 };
404 };
405 static SkTHashSet<Gold, Gold::Hash>* gGold = new SkTHashSet<Gold, Gold::Hash>;
406
add_gold(JsonWriter::BitmapResult r)407 static void add_gold(JsonWriter::BitmapResult r) {
408 gGold->add(Gold(r.config, r.sourceType, r.sourceOptions, r.name, r.md5));
409 }
410
gather_gold()411 static void gather_gold() {
412 if (!FLAGS_readPath.isEmpty()) {
413 SkString path(FLAGS_readPath[0]);
414 path.append("/dm.json");
415 if (!JsonWriter::ReadJson(path.c_str(), add_gold)) {
416 fail(SkStringPrintf("Couldn't read %s for golden results.", path.c_str()));
417 }
418 }
419 }
420
421 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
422
423 #if defined(SK_BUILD_FOR_WIN)
424 static constexpr char kNewline[] = "\r\n";
425 #else
426 static constexpr char kNewline[] = "\n";
427 #endif
428
429 static SkTHashSet<SkString>* gUninterestingHashes = new SkTHashSet<SkString>;
430
gather_uninteresting_hashes()431 static void gather_uninteresting_hashes() {
432 if (!FLAGS_uninterestingHashesFile.isEmpty()) {
433 sk_sp<SkData> data(SkData::MakeFromFileName(FLAGS_uninterestingHashesFile[0]));
434 if (!data) {
435 info("WARNING: unable to read uninteresting hashes from %s\n",
436 FLAGS_uninterestingHashesFile[0]);
437 return;
438 }
439
440 // Copy to a string to make sure SkStrSplit has a terminating \0 to find.
441 SkString contents((const char*)data->data(), data->size());
442
443 SkTArray<SkString> hashes;
444 SkStrSplit(contents.c_str(), kNewline, &hashes);
445 for (const SkString& hash : hashes) {
446 gUninterestingHashes->add(hash);
447 }
448 info("FYI: loaded %d distinct uninteresting hashes from %d lines\n",
449 gUninterestingHashes->count(), hashes.count());
450 }
451 }
452
453 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
454
455 struct TaggedSrc : public std::unique_ptr<Src> {
456 SkString tag;
457 SkString options;
458 };
459
460 struct TaggedSink : public std::unique_ptr<Sink> {
461 SkString tag;
462 };
463
464 static constexpr bool kMemcpyOK = true;
465
466 static SkTArray<TaggedSrc, kMemcpyOK>* gSrcs = new SkTArray<TaggedSrc, kMemcpyOK>;
467 static SkTArray<TaggedSink, kMemcpyOK>* gSinks = new SkTArray<TaggedSink, kMemcpyOK>;
468
in_shard()469 static bool in_shard() {
470 static int N = 0;
471 return N++ % FLAGS_shards == FLAGS_shard;
472 }
473
push_src(const char * tag,ImplicitString options,Src * s)474 static void push_src(const char* tag, ImplicitString options, Src* s) {
475 std::unique_ptr<Src> src(s);
476 if (in_shard() && FLAGS_src.contains(tag) &&
477 !CommandLineFlags::ShouldSkip(FLAGS_match, src->name().c_str())) {
478 TaggedSrc& s = gSrcs->push_back();
479 s.reset(src.release());
480 s.tag = tag;
481 s.options = options;
482 }
483 }
484
push_codec_src(Path path,CodecSrc::Mode mode,CodecSrc::DstColorType dstColorType,SkAlphaType dstAlphaType,float scale)485 static void push_codec_src(Path path, CodecSrc::Mode mode, CodecSrc::DstColorType dstColorType,
486 SkAlphaType dstAlphaType, float scale) {
487 if (FLAGS_simpleCodec) {
488 const bool simple = CodecSrc::kCodec_Mode == mode || CodecSrc::kAnimated_Mode == mode;
489 if (!simple || dstColorType != CodecSrc::kGetFromCanvas_DstColorType || scale != 1.0f) {
490 // Only decode in the simple case.
491 return;
492 }
493 }
494 SkString folder;
495 switch (mode) {
496 case CodecSrc::kCodec_Mode:
497 folder.append("codec");
498 break;
499 case CodecSrc::kCodecZeroInit_Mode:
500 folder.append("codec_zero_init");
501 break;
502 case CodecSrc::kScanline_Mode:
503 folder.append("scanline");
504 break;
505 case CodecSrc::kStripe_Mode:
506 folder.append("stripe");
507 break;
508 case CodecSrc::kCroppedScanline_Mode:
509 folder.append("crop");
510 break;
511 case CodecSrc::kSubset_Mode:
512 folder.append("codec_subset");
513 break;
514 case CodecSrc::kAnimated_Mode:
515 folder.append("codec_animated");
516 break;
517 }
518
519 switch (dstColorType) {
520 case CodecSrc::kGrayscale_Always_DstColorType:
521 folder.append("_kGray8");
522 break;
523 case CodecSrc::kNonNative8888_Always_DstColorType:
524 folder.append("_kNonNative");
525 break;
526 default:
527 break;
528 }
529
530 switch (dstAlphaType) {
531 case kPremul_SkAlphaType:
532 folder.append("_premul");
533 break;
534 case kUnpremul_SkAlphaType:
535 folder.append("_unpremul");
536 break;
537 default:
538 break;
539 }
540
541 if (1.0f != scale) {
542 folder.appendf("_%.3f", scale);
543 }
544
545 CodecSrc* src = new CodecSrc(path, mode, dstColorType, dstAlphaType, scale);
546 push_src("image", folder, src);
547 }
548
push_android_codec_src(Path path,CodecSrc::DstColorType dstColorType,SkAlphaType dstAlphaType,int sampleSize)549 static void push_android_codec_src(Path path, CodecSrc::DstColorType dstColorType,
550 SkAlphaType dstAlphaType, int sampleSize) {
551 SkString folder;
552 folder.append("scaled_codec");
553
554 switch (dstColorType) {
555 case CodecSrc::kGrayscale_Always_DstColorType:
556 folder.append("_kGray8");
557 break;
558 case CodecSrc::kNonNative8888_Always_DstColorType:
559 folder.append("_kNonNative");
560 break;
561 default:
562 break;
563 }
564
565 switch (dstAlphaType) {
566 case kPremul_SkAlphaType:
567 folder.append("_premul");
568 break;
569 case kUnpremul_SkAlphaType:
570 folder.append("_unpremul");
571 break;
572 default:
573 break;
574 }
575
576 if (1 != sampleSize) {
577 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
578 }
579
580 AndroidCodecSrc* src = new AndroidCodecSrc(path, dstColorType, dstAlphaType, sampleSize);
581 push_src("image", folder, src);
582 }
583
push_image_gen_src(Path path,ImageGenSrc::Mode mode,SkAlphaType alphaType,bool isGpu)584 static void push_image_gen_src(Path path, ImageGenSrc::Mode mode, SkAlphaType alphaType, bool isGpu)
585 {
586 SkString folder;
587 switch (mode) {
588 case ImageGenSrc::kCodec_Mode:
589 folder.append("gen_codec");
590 break;
591 case ImageGenSrc::kPlatform_Mode:
592 folder.append("gen_platform");
593 break;
594 }
595
596 if (isGpu) {
597 folder.append("_gpu");
598 } else {
599 switch (alphaType) {
600 case kOpaque_SkAlphaType:
601 folder.append("_opaque");
602 break;
603 case kPremul_SkAlphaType:
604 folder.append("_premul");
605 break;
606 case kUnpremul_SkAlphaType:
607 folder.append("_unpremul");
608 break;
609 default:
610 break;
611 }
612 }
613
614 ImageGenSrc* src = new ImageGenSrc(path, mode, alphaType, isGpu);
615 push_src("image", folder, src);
616 }
617
618 #ifdef SK_ENABLE_ANDROID_UTILS
push_brd_src(Path path,CodecSrc::DstColorType dstColorType,BRDSrc::Mode mode,uint32_t sampleSize)619 static void push_brd_src(Path path, CodecSrc::DstColorType dstColorType, BRDSrc::Mode mode,
620 uint32_t sampleSize) {
621 SkString folder("brd_android_codec");
622 switch (mode) {
623 case BRDSrc::kFullImage_Mode:
624 break;
625 case BRDSrc::kDivisor_Mode:
626 folder.append("_divisor");
627 break;
628 default:
629 SkASSERT(false);
630 return;
631 }
632
633 switch (dstColorType) {
634 case CodecSrc::kGetFromCanvas_DstColorType:
635 break;
636 case CodecSrc::kGrayscale_Always_DstColorType:
637 folder.append("_kGray");
638 break;
639 default:
640 SkASSERT(false);
641 return;
642 }
643
644 if (1 != sampleSize) {
645 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
646 }
647
648 BRDSrc* src = new BRDSrc(path, mode, dstColorType, sampleSize);
649 push_src("image", folder, src);
650 }
651
push_brd_srcs(Path path,bool gray)652 static void push_brd_srcs(Path path, bool gray) {
653 if (gray) {
654 // Only run grayscale to one sampleSize and Mode. Though interesting
655 // to test grayscale, it should not reveal anything across various
656 // sampleSizes and Modes
657 // Arbitrarily choose Mode and sampleSize.
658 push_brd_src(path, CodecSrc::kGrayscale_Always_DstColorType,
659 BRDSrc::kFullImage_Mode, 2);
660 }
661
662 // Test on a variety of sampleSizes, making sure to include:
663 // - 2, 4, and 8, which are natively supported by jpeg
664 // - multiples of 2 which are not divisible by 4 (analogous for 4)
665 // - larger powers of two, since BRD clients generally use powers of 2
666 // We will only produce output for the larger sizes on large images.
667 const uint32_t sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 24, 32, 64 };
668
669 const BRDSrc::Mode modes[] = { BRDSrc::kFullImage_Mode, BRDSrc::kDivisor_Mode, };
670
671 for (uint32_t sampleSize : sampleSizes) {
672 for (BRDSrc::Mode mode : modes) {
673 push_brd_src(path, CodecSrc::kGetFromCanvas_DstColorType, mode, sampleSize);
674 }
675 }
676 }
677 #endif // SK_ENABLE_ANDROID_UTILS
678
push_codec_srcs(Path path)679 static void push_codec_srcs(Path path) {
680 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
681 if (!encoded) {
682 info("Couldn't read %s.", path.c_str());
683 return;
684 }
685 std::unique_ptr<SkCodec> codec = SkCodec::MakeFromData(encoded);
686 if (nullptr == codec) {
687 info("Couldn't create codec for %s.", path.c_str());
688 return;
689 }
690
691 // native scaling is only supported by WEBP and JPEG
692 bool supportsNativeScaling = false;
693
694 SkTArray<CodecSrc::Mode> nativeModes;
695 nativeModes.push_back(CodecSrc::kCodec_Mode);
696 nativeModes.push_back(CodecSrc::kCodecZeroInit_Mode);
697 switch (codec->getEncodedFormat()) {
698 case SkEncodedImageFormat::kJPEG:
699 nativeModes.push_back(CodecSrc::kScanline_Mode);
700 nativeModes.push_back(CodecSrc::kStripe_Mode);
701 nativeModes.push_back(CodecSrc::kCroppedScanline_Mode);
702 supportsNativeScaling = true;
703 break;
704 case SkEncodedImageFormat::kWEBP:
705 nativeModes.push_back(CodecSrc::kSubset_Mode);
706 supportsNativeScaling = true;
707 break;
708 case SkEncodedImageFormat::kDNG:
709 break;
710 default:
711 nativeModes.push_back(CodecSrc::kScanline_Mode);
712 break;
713 }
714
715 SkTArray<CodecSrc::DstColorType> colorTypes;
716 colorTypes.push_back(CodecSrc::kGetFromCanvas_DstColorType);
717 colorTypes.push_back(CodecSrc::kNonNative8888_Always_DstColorType);
718 switch (codec->getInfo().colorType()) {
719 case kGray_8_SkColorType:
720 colorTypes.push_back(CodecSrc::kGrayscale_Always_DstColorType);
721 break;
722 default:
723 break;
724 }
725
726 SkTArray<SkAlphaType> alphaModes;
727 alphaModes.push_back(kPremul_SkAlphaType);
728 if (codec->getInfo().alphaType() != kOpaque_SkAlphaType) {
729 alphaModes.push_back(kUnpremul_SkAlphaType);
730 }
731
732 for (CodecSrc::Mode mode : nativeModes) {
733 for (CodecSrc::DstColorType colorType : colorTypes) {
734 for (SkAlphaType alphaType : alphaModes) {
735 // Only test kCroppedScanline_Mode when the alpha type is premul. The test is
736 // slow and won't be interestingly different with different alpha types.
737 if (CodecSrc::kCroppedScanline_Mode == mode &&
738 kPremul_SkAlphaType != alphaType) {
739 continue;
740 }
741
742 push_codec_src(path, mode, colorType, alphaType, 1.0f);
743
744 // Skip kNonNative on different native scales. It won't be interestingly
745 // different.
746 if (supportsNativeScaling &&
747 CodecSrc::kNonNative8888_Always_DstColorType == colorType) {
748 // Native Scales
749 // SkJpegCodec natively supports scaling to the following:
750 for (auto scale : { 0.125f, 0.25f, 0.375f, 0.5f, 0.625f, 0.750f, 0.875f }) {
751 push_codec_src(path, mode, colorType, alphaType, scale);
752 }
753 }
754 }
755 }
756 }
757
758 {
759 std::vector<SkCodec::FrameInfo> frameInfos = codec->getFrameInfo();
760 if (frameInfos.size() > 1) {
761 for (auto dstCT : { CodecSrc::kNonNative8888_Always_DstColorType,
762 CodecSrc::kGetFromCanvas_DstColorType }) {
763 for (auto at : { kUnpremul_SkAlphaType, kPremul_SkAlphaType }) {
764 push_codec_src(path, CodecSrc::kAnimated_Mode, dstCT, at, 1.0f);
765 }
766 }
767 }
768
769 }
770
771 if (FLAGS_simpleCodec) {
772 return;
773 }
774
775 const int sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
776
777 for (int sampleSize : sampleSizes) {
778 for (CodecSrc::DstColorType colorType : colorTypes) {
779 for (SkAlphaType alphaType : alphaModes) {
780 // We can exercise all of the kNonNative support code in the swizzler with just a
781 // few sample sizes. Skip the rest.
782 if (CodecSrc::kNonNative8888_Always_DstColorType == colorType && sampleSize > 3) {
783 continue;
784 }
785
786 push_android_codec_src(path, colorType, alphaType, sampleSize);
787 }
788 }
789 }
790
791 const char* ext = strrchr(path.c_str(), '.');
792 if (ext) {
793 ext++;
794
795 static const char* const rawExts[] = {
796 "arw", "cr2", "dng", "nef", "nrw", "orf", "raf", "rw2", "pef", "srw",
797 "ARW", "CR2", "DNG", "NEF", "NRW", "ORF", "RAF", "RW2", "PEF", "SRW",
798 };
799 for (const char* rawExt : rawExts) {
800 if (0 == strcmp(rawExt, ext)) {
801 // RAW is not supported by image generator (skbug.com/5079) or BRD.
802 return;
803 }
804 }
805
806 #ifdef SK_ENABLE_ANDROID_UTILS
807 static const char* const brdExts[] = {
808 "jpg", "jpeg", "png", "webp",
809 "JPG", "JPEG", "PNG", "WEBP",
810 };
811 for (const char* brdExt : brdExts) {
812 if (0 == strcmp(brdExt, ext)) {
813 bool gray = codec->getInfo().colorType() == kGray_8_SkColorType;
814 push_brd_srcs(path, gray);
815 break;
816 }
817 }
818 #endif
819 }
820
821 // Push image generator GPU test.
822 push_image_gen_src(path, ImageGenSrc::kCodec_Mode, codec->getInfo().alphaType(), true);
823
824 // Push image generator CPU tests.
825 for (SkAlphaType alphaType : alphaModes) {
826 push_image_gen_src(path, ImageGenSrc::kCodec_Mode, alphaType, false);
827
828 #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
829 if (SkEncodedImageFormat::kWEBP != codec->getEncodedFormat() &&
830 SkEncodedImageFormat::kWBMP != codec->getEncodedFormat() &&
831 kUnpremul_SkAlphaType != alphaType)
832 {
833 push_image_gen_src(path, ImageGenSrc::kPlatform_Mode, alphaType, false);
834 }
835 #elif defined(SK_BUILD_FOR_WIN)
836 if (SkEncodedImageFormat::kWEBP != codec->getEncodedFormat() &&
837 SkEncodedImageFormat::kWBMP != codec->getEncodedFormat())
838 {
839 push_image_gen_src(path, ImageGenSrc::kPlatform_Mode, alphaType, false);
840 }
841 #elif defined(SK_ENABLE_NDK_IMAGES)
842 push_image_gen_src(path, ImageGenSrc::kPlatform_Mode, alphaType, false);
843 #endif
844 }
845 }
846
847 template <typename T>
gather_file_srcs(const CommandLineFlags::StringArray & flags,const char * ext,const char * src_name=nullptr)848 void gather_file_srcs(const CommandLineFlags::StringArray& flags,
849 const char* ext,
850 const char* src_name = nullptr) {
851 if (!src_name) {
852 // With the exception of Lottie files, the source name is the extension.
853 src_name = ext;
854 }
855
856 for (int i = 0; i < flags.count(); i++) {
857 const char* path = flags[i];
858 if (sk_isdir(path)) {
859 SkOSFile::Iter it(path, ext);
860 for (SkString file; it.next(&file); ) {
861 push_src(src_name, "", new T(SkOSPath::Join(path, file.c_str())));
862 }
863 } else {
864 push_src(src_name, "", new T(path));
865 }
866 }
867 }
868
gather_srcs()869 static bool gather_srcs() {
870 for (skiagm::GMFactory f : skiagm::GMRegistry::Range()) {
871 push_src("gm", "", new GMSrc(f));
872 }
873
874 gather_file_srcs<SKPSrc>(FLAGS_skps, "skp");
875 gather_file_srcs<MSKPSrc>(FLAGS_mskps, "mskp");
876 #if defined(SK_ENABLE_SKOTTIE)
877 gather_file_srcs<SkottieSrc>(FLAGS_lotties, "json", "lottie");
878 #endif
879 #if defined(SK_ENABLE_SKRIVE)
880 gather_file_srcs<SkRiveSrc>(FLAGS_rives, "flr", "rive");
881 #endif
882 #if defined(SK_XML)
883 gather_file_srcs<SVGSrc>(FLAGS_svgs, "svg");
884 #endif
885 if (!FLAGS_bisect.isEmpty()) {
886 // An empty l/r trail string will draw all the paths.
887 push_src("bisect", "",
888 new BisectSrc(FLAGS_bisect[0], FLAGS_bisect.count() > 1 ? FLAGS_bisect[1] : ""));
889 }
890
891 SkTArray<SkString> images;
892 if (!CollectImages(FLAGS_images, &images)) {
893 return false;
894 }
895
896 for (const SkString& image : images) {
897 push_codec_srcs(image);
898 }
899
900 SkTArray<SkString> colorImages;
901 if (!CollectImages(FLAGS_colorImages, &colorImages)) {
902 return false;
903 }
904
905 for (const SkString& colorImage : colorImages) {
906 push_src("colorImage", "decode_native", new ColorCodecSrc(colorImage, false));
907 push_src("colorImage", "decode_to_dst", new ColorCodecSrc(colorImage, true));
908 }
909
910 return true;
911 }
912
push_sink(const SkCommandLineConfig & config,Sink * s)913 static void push_sink(const SkCommandLineConfig& config, Sink* s) {
914 std::unique_ptr<Sink> sink(s);
915
916 // Try a simple Src as a canary. If it fails, skip this sink.
917 struct : public Src {
918 Result draw(GrDirectContext*, SkCanvas* c) const override {
919 c->drawRect(SkRect::MakeWH(1,1), SkPaint());
920 return Result::Ok();
921 }
922 SkISize size() const override { return SkISize::Make(16, 16); }
923 Name name() const override { return "justOneRect"; }
924 } justOneRect;
925
926 SkBitmap bitmap;
927 SkDynamicMemoryWStream stream;
928 SkString log;
929 Result result = sink->draw(justOneRect, &bitmap, &stream, &log);
930 if (result.isFatal()) {
931 info("Could not run %s: %s\n", config.getTag().c_str(), result.c_str());
932 exit(1);
933 }
934
935 TaggedSink& ts = gSinks->push_back();
936 ts.reset(sink.release());
937 ts.tag = config.getTag();
938 }
939
rgb_to_gbr()940 static sk_sp<SkColorSpace> rgb_to_gbr() {
941 return SkColorSpace::MakeSRGB()->makeColorSpin();
942 }
943
create_sink(const GrContextOptions & grCtxOptions,const SkCommandLineConfig * config)944 static Sink* create_sink(const GrContextOptions& grCtxOptions, const SkCommandLineConfig* config) {
945 if (FLAGS_gpu) {
946 if (const SkCommandLineConfigGpu* gpuConfig = config->asConfigGpu()) {
947 GrContextFactory testFactory(grCtxOptions);
948 if (!testFactory.get(gpuConfig->getContextType(), gpuConfig->getContextOverrides())) {
949 info("WARNING: can not create GPU context for config '%s'. "
950 "GM tests will be skipped.\n", gpuConfig->getTag().c_str());
951 return nullptr;
952 }
953 if (gpuConfig->getTestThreading()) {
954 SkASSERT(!gpuConfig->getTestPersistentCache());
955 return new GPUThreadTestingSink(gpuConfig, grCtxOptions);
956 } else if (gpuConfig->getTestPersistentCache()) {
957 return new GPUPersistentCacheTestingSink(gpuConfig, grCtxOptions);
958 } else if (gpuConfig->getTestPrecompile()) {
959 return new GPUPrecompileTestingSink(gpuConfig, grCtxOptions);
960 } else if (gpuConfig->getUseDDLSink()) {
961 return new GPUDDLSink(gpuConfig, grCtxOptions);
962 } else if (gpuConfig->getOOPRish()) {
963 return new GPUOOPRSink(gpuConfig, grCtxOptions);
964 } else {
965 return new GPUSink(gpuConfig, grCtxOptions);
966 }
967 }
968 }
969 if (const SkCommandLineConfigSvg* svgConfig = config->asConfigSvg()) {
970 int pageIndex = svgConfig->getPageIndex();
971 return new SVGSink(pageIndex);
972 }
973
974 #define SINK(t, sink, ...) if (config->getBackend().equals(t)) return new sink(__VA_ARGS__)
975
976 if (FLAGS_cpu) {
977 SINK("g8", RasterSink, kGray_8_SkColorType);
978 SINK("565", RasterSink, kRGB_565_SkColorType);
979 SINK("4444", RasterSink, kARGB_4444_SkColorType);
980 SINK("8888", RasterSink, kN32_SkColorType);
981 SINK("rgba", RasterSink, kRGBA_8888_SkColorType);
982 SINK("bgra", RasterSink, kBGRA_8888_SkColorType);
983 SINK("rgbx", RasterSink, kRGB_888x_SkColorType);
984 SINK("1010102", RasterSink, kRGBA_1010102_SkColorType);
985 SINK("101010x", RasterSink, kRGB_101010x_SkColorType);
986 SINK("bgra1010102", RasterSink, kBGRA_1010102_SkColorType);
987 SINK("bgr101010x", RasterSink, kBGR_101010x_SkColorType);
988 SINK("pdf", PDFSink, false, SK_ScalarDefaultRasterDPI);
989 SINK("skp", SKPSink);
990 SINK("svg", SVGSink);
991 SINK("null", NullSink);
992 SINK("xps", XPSSink);
993 SINK("pdfa", PDFSink, true, SK_ScalarDefaultRasterDPI);
994 SINK("pdf300", PDFSink, false, 300);
995 SINK("jsdebug", DebugSink);
996
997 // Configs relevant to color management testing (and 8888 for reference).
998
999 // 'narrow' has a gamut narrower than sRGB, and different transfer function.
1000 auto narrow = SkColorSpace::MakeRGB(SkNamedTransferFn::k2Dot2, gNarrow_toXYZD50),
1001 srgb = SkColorSpace::MakeSRGB(),
1002 srgbLinear = SkColorSpace::MakeSRGBLinear(),
1003 p3 = SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB, SkNamedGamut::kDisplayP3);
1004
1005 SINK( "f16", RasterSink, kRGBA_F16_SkColorType, srgbLinear);
1006 SINK( "srgb", RasterSink, kRGBA_8888_SkColorType, srgb );
1007 SINK( "esrgb", RasterSink, kRGBA_F16_SkColorType, srgb );
1008 SINK( "esgbr", RasterSink, kRGBA_F16_SkColorType, rgb_to_gbr());
1009 SINK( "narrow", RasterSink, kRGBA_8888_SkColorType, narrow );
1010 SINK( "enarrow", RasterSink, kRGBA_F16_SkColorType, narrow );
1011 SINK( "p3", RasterSink, kRGBA_8888_SkColorType, p3 );
1012 SINK( "ep3", RasterSink, kRGBA_F16_SkColorType, p3 );
1013 SINK( "rec2020", RasterSink, kRGBA_8888_SkColorType, rec2020() );
1014 SINK("erec2020", RasterSink, kRGBA_F16_SkColorType, rec2020() );
1015
1016 SINK("f16norm", RasterSink, kRGBA_F16Norm_SkColorType, srgb);
1017
1018 SINK( "f32", RasterSink, kRGBA_F32_SkColorType, srgbLinear);
1019 }
1020 #undef SINK
1021 return nullptr;
1022 }
1023
create_via(const SkString & tag,Sink * wrapped)1024 static Sink* create_via(const SkString& tag, Sink* wrapped) {
1025 #define VIA(t, via, ...) if (tag.equals(t)) return new via(__VA_ARGS__)
1026 #ifdef TEST_VIA_SVG
1027 VIA("svg", ViaSVG, wrapped);
1028 #endif
1029 VIA("serialize", ViaSerialization, wrapped);
1030 VIA("pic", ViaPicture, wrapped);
1031 VIA("ddl", ViaDDL, 1, 3, wrapped);
1032 VIA("ddl2", ViaDDL, 2, 3, wrapped);
1033
1034 if (FLAGS_matrix.count() == 4) {
1035 SkMatrix m;
1036 m.reset();
1037 m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
1038 m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
1039 m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
1040 m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
1041 VIA("matrix", ViaMatrix, m, wrapped);
1042 VIA("upright", ViaUpright, m, wrapped);
1043 }
1044
1045 #undef VIA
1046 return nullptr;
1047 }
1048
gather_sinks(const GrContextOptions & grCtxOptions,bool defaultConfigs)1049 static bool gather_sinks(const GrContextOptions& grCtxOptions, bool defaultConfigs) {
1050 SkCommandLineConfigArray configs;
1051 ParseConfigs(FLAGS_config, &configs);
1052 AutoreleasePool pool;
1053 for (int i = 0; i < configs.count(); i++) {
1054 const SkCommandLineConfig& config = *configs[i];
1055 Sink* sink = create_sink(grCtxOptions, &config);
1056 if (sink == nullptr) {
1057 info("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
1058 config.getTag().c_str());
1059 continue;
1060 }
1061
1062 const SkTArray<SkString>& parts = config.getViaParts();
1063 for (int j = parts.count(); j-- > 0;) {
1064 const SkString& part = parts[j];
1065 Sink* next = create_via(part, sink);
1066 if (next == nullptr) {
1067 info("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
1068 part.c_str());
1069 delete sink;
1070 sink = nullptr;
1071 break;
1072 }
1073 sink = next;
1074 }
1075 if (sink) {
1076 push_sink(config, sink);
1077 }
1078 }
1079
1080 // If no configs were requested (just running tests, perhaps?), then we're okay.
1081 if (configs.count() == 0 ||
1082 // If we're using the default configs, we're okay.
1083 defaultConfigs ||
1084 // Otherwise, make sure that all specified configs have become sinks.
1085 configs.count() == gSinks->count()) {
1086 return true;
1087 }
1088 return false;
1089 }
1090
match(const char * needle,const char * haystack)1091 static bool match(const char* needle, const char* haystack) {
1092 if ('~' == needle[0]) {
1093 return !match(needle + 1, haystack);
1094 }
1095 if (0 == strcmp("_", needle)) {
1096 return true;
1097 }
1098 return nullptr != strstr(haystack, needle);
1099 }
1100
should_skip(const char * sink,const char * src,const char * srcOptions,const char * name)1101 static bool should_skip(const char* sink, const char* src,
1102 const char* srcOptions, const char* name) {
1103 for (int i = 0; i < FLAGS_skip.count() - 3; i += 4) {
1104 if (match(FLAGS_skip[i+0], sink) &&
1105 match(FLAGS_skip[i+1], src) &&
1106 match(FLAGS_skip[i+2], srcOptions) &&
1107 match(FLAGS_skip[i+3], name)) {
1108 return true;
1109 }
1110 }
1111 return false;
1112 }
1113
1114 // Even when a Task Sink reports to be non-threadsafe (e.g. GPU), we know things like
1115 // .png encoding are definitely thread safe. This lets us offload that work to CPU threads.
1116 static SkTaskGroup* gDefinitelyThreadSafeWork = new SkTaskGroup;
1117
1118 // The finest-grained unit of work we can run: draw a single Src into a single Sink,
1119 // report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
1120 struct Task {
TaskTask1121 Task(const TaggedSrc& src, const TaggedSink& sink) : src(src), sink(sink) {}
1122 const TaggedSrc& src;
1123 const TaggedSink& sink;
1124
RunTask1125 static void Run(const Task& task) {
1126 AutoreleasePool pool;
1127 SkString name = task.src->name();
1128
1129 SkString log;
1130 if (!FLAGS_dryRun) {
1131 SkBitmap bitmap;
1132 SkDynamicMemoryWStream stream;
1133 start(task.sink.tag.c_str(), task.src.tag.c_str(),
1134 task.src.options.c_str(), name.c_str());
1135 Result result = task.sink->draw(*task.src, &bitmap, &stream, &log);
1136 if (!log.isEmpty()) {
1137 info("%s %s %s %s:\n%s\n", task.sink.tag.c_str()
1138 , task.src.tag.c_str()
1139 , task.src.options.c_str()
1140 , name.c_str()
1141 , log.c_str());
1142 }
1143 if (result.isSkip()) {
1144 done(task.sink.tag.c_str(), task.src.tag.c_str(),
1145 task.src.options.c_str(), name.c_str());
1146 return;
1147 }
1148 if (result.isFatal()) {
1149 fail(SkStringPrintf("%s %s %s %s: %s",
1150 task.sink.tag.c_str(),
1151 task.src.tag.c_str(),
1152 task.src.options.c_str(),
1153 name.c_str(),
1154 result.c_str()));
1155 }
1156
1157 // Run verifiers if specified
1158 if (FLAGS_runVerifiers) {
1159 RunGMVerifiers(task, bitmap);
1160 }
1161
1162 // We're likely switching threads here, so we must capture by value, [=] or [foo,bar].
1163 SkStreamAsset* data = stream.detachAsStream().release();
1164 gDefinitelyThreadSafeWork->add([task,name,bitmap,data]{
1165 std::unique_ptr<SkStreamAsset> ownedData(data);
1166
1167 std::unique_ptr<HashAndEncode> hashAndEncode;
1168
1169 SkString md5;
1170 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
1171 SkMD5 hash;
1172 if (data->getLength()) {
1173 hash.writeStream(data, data->getLength());
1174 data->rewind();
1175 } else {
1176 hashAndEncode = std::make_unique<HashAndEncode>(bitmap);
1177 hashAndEncode->feedHash(&hash);
1178 }
1179 SkMD5::Digest digest = hash.finish();
1180 for (int i = 0; i < 16; i++) {
1181 md5.appendf("%02x", digest.data[i]);
1182 }
1183 }
1184
1185 if (!FLAGS_readPath.isEmpty() &&
1186 !gGold->contains(Gold(task.sink.tag, task.src.tag,
1187 task.src.options, name, md5))) {
1188 fail(SkStringPrintf("%s not found for %s %s %s %s in %s",
1189 md5.c_str(),
1190 task.sink.tag.c_str(),
1191 task.src.tag.c_str(),
1192 task.src.options.c_str(),
1193 name.c_str(),
1194 FLAGS_readPath[0]));
1195 }
1196
1197 // Tests sometimes use a nullptr ext to indicate no image should be uploaded.
1198 const char* ext = task.sink->fileExtension();
1199 if (ext && !FLAGS_writePath.isEmpty()) {
1200 #if defined(SK_BUILD_FOR_MAC)
1201 if (FLAGS_rasterize_pdf && SkString("pdf").equals(ext)) {
1202 SkASSERT(data->getLength() > 0);
1203
1204 sk_sp<SkData> blob = SkData::MakeFromStream(data, data->getLength());
1205
1206 SkUniqueCFRef<CGDataProviderRef> provider{
1207 CGDataProviderCreateWithData(nullptr,
1208 blob->data(),
1209 blob->size(),
1210 nullptr)};
1211
1212 SkUniqueCFRef<CGPDFDocumentRef> pdf{
1213 CGPDFDocumentCreateWithProvider(provider.get())};
1214
1215 CGPDFPageRef page = CGPDFDocumentGetPage(pdf.get(), 1);
1216
1217 CGRect bounds = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
1218 const int w = (int)CGRectGetWidth (bounds),
1219 h = (int)CGRectGetHeight(bounds);
1220
1221 SkBitmap rasterized;
1222 rasterized.allocPixels(SkImageInfo::Make(
1223 w, h, kRGBA_8888_SkColorType, kPremul_SkAlphaType));
1224 rasterized.eraseColor(SK_ColorWHITE);
1225
1226 SkUniqueCFRef<CGColorSpaceRef> cs{CGColorSpaceCreateDeviceRGB()};
1227 CGBitmapInfo info = kCGBitmapByteOrder32Big |
1228 (CGBitmapInfo)kCGImageAlphaPremultipliedLast;
1229
1230 SkUniqueCFRef<CGContextRef> ctx{CGBitmapContextCreate(
1231 rasterized.getPixels(), w,h,8, rasterized.rowBytes(), cs.get(), info)};
1232 CGContextDrawPDFPage(ctx.get(), page);
1233
1234 // Skip calling hashAndEncode->feedHash(SkMD5*)... we want the .pdf's hash.
1235 hashAndEncode = std::make_unique<HashAndEncode>(rasterized);
1236 WriteToDisk(task, md5, "png", nullptr,0, &rasterized, hashAndEncode.get());
1237 } else
1238 #endif
1239 if (data->getLength()) {
1240 WriteToDisk(task, md5, ext, data, data->getLength(), nullptr, nullptr);
1241 SkASSERT(bitmap.drawsNothing());
1242 } else if (!bitmap.drawsNothing()) {
1243 WriteToDisk(task, md5, ext, nullptr, 0, &bitmap, hashAndEncode.get());
1244 }
1245 }
1246
1247 SkPixmap pm;
1248 if (FLAGS_checkF16 && bitmap.colorType() == kRGBA_F16Norm_SkColorType &&
1249 bitmap.peekPixels(&pm)) {
1250 bool unclamped = false;
1251 for (int y = 0; y < pm.height() && !unclamped; ++y)
1252 for (int x = 0; x < pm.width() && !unclamped; ++x) {
1253 Sk4f rgba = SkHalfToFloat_finite_ftz(*pm.addr64(x, y));
1254 float a = rgba[3];
1255 if (a > 1.0f || (rgba < 0.0f).anyTrue() || (rgba > a).anyTrue()) {
1256 SkDebugf("[%s] F16Norm pixel [%d, %d] unclamped: (%g, %g, %g, %g)\n",
1257 name.c_str(), x, y, rgba[0], rgba[1], rgba[2], rgba[3]);
1258 unclamped = true;
1259 }
1260 }
1261 }
1262 });
1263 }
1264 done(task.sink.tag.c_str(), task.src.tag.c_str(), task.src.options.c_str(), name.c_str());
1265 }
1266
identify_gamutTask1267 static SkString identify_gamut(SkColorSpace* cs) {
1268 if (!cs) {
1269 return SkString("untagged");
1270 }
1271
1272 skcms_Matrix3x3 gamut;
1273 if (cs->toXYZD50(&gamut)) {
1274 auto eq = [](skcms_Matrix3x3 x, skcms_Matrix3x3 y) {
1275 for (int i = 0; i < 3; i++)
1276 for (int j = 0; j < 3; j++) {
1277 if (x.vals[i][j] != y.vals[i][j]) { return false; }
1278 }
1279 return true;
1280 };
1281
1282 if (eq(gamut, SkNamedGamut::kSRGB )) { return SkString("sRGB"); }
1283 if (eq(gamut, SkNamedGamut::kAdobeRGB )) { return SkString("Adobe"); }
1284 if (eq(gamut, SkNamedGamut::kDisplayP3)) { return SkString("P3"); }
1285 if (eq(gamut, SkNamedGamut::kRec2020 )) { return SkString("2020"); }
1286 if (eq(gamut, SkNamedGamut::kXYZ )) { return SkString("XYZ"); }
1287 if (eq(gamut, gNarrow_toXYZD50 )) { return SkString("narrow"); }
1288 return SkString("other");
1289 }
1290 return SkString("non-XYZ");
1291 }
1292
identify_transfer_fnTask1293 static SkString identify_transfer_fn(SkColorSpace* cs) {
1294 if (!cs) {
1295 return SkString("untagged");
1296 }
1297
1298 auto eq = [](skcms_TransferFunction x, skcms_TransferFunction y) {
1299 return x.g == y.g
1300 && x.a == y.a
1301 && x.b == y.b
1302 && x.c == y.c
1303 && x.d == y.d
1304 && x.e == y.e
1305 && x.f == y.f;
1306 };
1307
1308 skcms_TransferFunction tf;
1309 cs->transferFn(&tf);
1310 switch (classify_transfer_fn(tf)) {
1311 case sRGBish_TF:
1312 if (tf.a == 1 && tf.b == 0 && tf.c == 0 && tf.d == 0 && tf.e == 0 && tf.f == 0) {
1313 return SkStringPrintf("gamma %.3g", tf.g);
1314 }
1315 if (eq(tf, SkNamedTransferFn::kSRGB)) { return SkString("sRGB"); }
1316 if (eq(tf, SkNamedTransferFn::kRec2020)) { return SkString("2020"); }
1317 return SkStringPrintf("%.3g %.3g %.3g %.3g %.3g %.3g %.3g",
1318 tf.g, tf.a, tf.b, tf.c, tf.d, tf.e, tf.f);
1319
1320 case PQish_TF:
1321 if (eq(tf, SkNamedTransferFn::kPQ)) { return SkString("PQ"); }
1322 return SkStringPrintf("PQish %.3g %.3g %.3g %.3g %.3g %.3g",
1323 tf.a, tf.b, tf.c, tf.d, tf.e, tf.f);
1324
1325 case HLGish_TF:
1326 if (eq(tf, SkNamedTransferFn::kHLG)) { return SkString("HLG"); }
1327 return SkStringPrintf("HLGish %.3g %.3g %.3g %.3g %.3g",
1328 tf.a, tf.b, tf.c, tf.d, tf.e);
1329
1330 case HLGinvish_TF: break;
1331 case Bad_TF: break;
1332 }
1333 return SkString("non-numeric");
1334 }
1335
WriteToDiskTask1336 static void WriteToDisk(const Task& task,
1337 SkString md5,
1338 const char* ext,
1339 SkStream* data, size_t len,
1340 const SkBitmap* bitmap,
1341 const HashAndEncode* hashAndEncode) {
1342
1343 JsonWriter::BitmapResult result;
1344 result.name = task.src->name();
1345 result.config = task.sink.tag;
1346 result.sourceType = task.src.tag;
1347 result.sourceOptions = task.src.options;
1348 result.ext = ext;
1349 result.md5 = md5;
1350 if (bitmap) {
1351 result.gamut = identify_gamut (bitmap->colorSpace());
1352 result.transferFn = identify_transfer_fn (bitmap->colorSpace());
1353 result.colorType = ToolUtils::colortype_name (bitmap->colorType());
1354 result.alphaType = ToolUtils::alphatype_name (bitmap->alphaType());
1355 result.colorDepth = ToolUtils::colortype_depth(bitmap->colorType());
1356 }
1357 JsonWriter::AddBitmapResult(result);
1358
1359 // If an MD5 is uninteresting, we want it noted in the JSON file,
1360 // but don't want to dump it out as a .png (or whatever ext is).
1361 if (gUninterestingHashes->contains(md5)) {
1362 return;
1363 }
1364
1365 const char* dir = FLAGS_writePath[0];
1366 SkString resources = GetResourcePath();
1367 if (0 == strcmp(dir, "@")) { // Needed for iOS.
1368 dir = resources.c_str();
1369 }
1370 sk_mkdir(dir);
1371
1372 SkString path;
1373 if (FLAGS_nameByHash) {
1374 path = SkOSPath::Join(dir, result.md5.c_str());
1375 path.append(".");
1376 path.append(ext);
1377 if (sk_exists(path.c_str())) {
1378 return; // Content-addressed. If it exists already, we're done.
1379 }
1380 } else {
1381 path = SkOSPath::Join(dir, task.sink.tag.c_str());
1382 sk_mkdir(path.c_str());
1383 path = SkOSPath::Join(path.c_str(), task.src.tag.c_str());
1384 sk_mkdir(path.c_str());
1385 if (0 != strcmp(task.src.options.c_str(), "")) {
1386 path = SkOSPath::Join(path.c_str(), task.src.options.c_str());
1387 sk_mkdir(path.c_str());
1388 }
1389 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
1390 path.append(".");
1391 path.append(ext);
1392 }
1393
1394 SkFILEWStream file(path.c_str());
1395 if (!file.isValid()) {
1396 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
1397 return;
1398 }
1399 if (bitmap) {
1400 SkASSERT(hashAndEncode);
1401 if (!hashAndEncode->encodePNG(&file,
1402 result.md5.c_str(),
1403 FLAGS_key,
1404 FLAGS_properties)) {
1405 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
1406 return;
1407 }
1408 } else {
1409 if (!file.writeStream(data, len)) {
1410 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
1411 return;
1412 }
1413 }
1414 }
1415
RunGMVerifiersTask1416 static void RunGMVerifiers(const Task& task, const SkBitmap& actualBitmap) {
1417 const SkString name = task.src->name();
1418 auto verifierList = task.src->getVerifiers();
1419 if (verifierList == nullptr) {
1420 return;
1421 }
1422
1423 skiagm::verifiers::VerifierResult
1424 res = verifierList->verifyAll(task.sink->colorInfo(), actualBitmap);
1425 if (!res.ok()) {
1426 fail(
1427 SkStringPrintf(
1428 "%s %s %s %s: verifier failed: %s", task.sink.tag.c_str(), task.src.tag.c_str(),
1429 task.src.options.c_str(), name.c_str(), res.message().c_str()));
1430 }
1431 }
1432 };
1433
1434 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1435
1436 // Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
1437
1438 static SkTDArray<skiatest::Test>* gParallelTests = new SkTDArray<skiatest::Test>;
1439 static SkTDArray<skiatest::Test>* gSerialTests = new SkTDArray<skiatest::Test>;
1440
gather_tests()1441 static void gather_tests() {
1442 if (!FLAGS_src.contains("tests")) {
1443 return;
1444 }
1445 for (const skiatest::Test& test : skiatest::TestRegistry::Range()) {
1446 if (!in_shard()) {
1447 continue;
1448 }
1449 if (CommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
1450 continue;
1451 }
1452 if (test.needsGpu && FLAGS_gpu) {
1453 gSerialTests->push_back(test);
1454 } else if (!test.needsGpu && FLAGS_cpu) {
1455 gParallelTests->push_back(test);
1456 }
1457 }
1458 }
1459
run_test(skiatest::Test test,const GrContextOptions & grCtxOptions)1460 static void run_test(skiatest::Test test, const GrContextOptions& grCtxOptions) {
1461 struct : public skiatest::Reporter {
1462 void reportFailed(const skiatest::Failure& failure) override {
1463 fail(failure.toString());
1464 }
1465 bool allowExtendedTest() const override {
1466 return FLAGS_pathOpsExtended;
1467 }
1468 bool verbose() const override { return FLAGS_veryVerbose; }
1469 } reporter;
1470
1471 if (!FLAGS_dryRun && !should_skip("_", "tests", "_", test.name)) {
1472 AutoreleasePool pool;
1473 GrContextOptions options = grCtxOptions;
1474 test.modifyGrContextOptions(&options);
1475
1476 skiatest::ReporterContext ctx(&reporter, SkString(test.name));
1477 start("unit", "test", "", test.name);
1478 test.run(&reporter, options);
1479 }
1480 done("unit", "test", "", test.name);
1481 }
1482
1483 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1484
main(int argc,char ** argv)1485 int main(int argc, char** argv) {
1486 #if defined(__MSVC_RUNTIME_CHECKS)
1487 _RTC_SetErrorFunc(RuntimeCheckErrorFunc);
1488 #endif
1489 #if defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) && defined(SK_HAS_HEIF_LIBRARY)
1490 android::ProcessState::self()->startThreadPool();
1491 #endif
1492 CommandLineFlags::Parse(argc, argv);
1493
1494 initializeEventTracingForTools();
1495
1496 #if !defined(SK_BUILD_FOR_GOOGLE3) && defined(SK_BUILD_FOR_IOS)
1497 cd_Documents();
1498 #endif
1499 setbuf(stdout, nullptr);
1500 setup_crash_handler();
1501
1502 ToolUtils::SetDefaultFontMgr();
1503 SetAnalyticAAFromCommonFlags();
1504
1505 gSkForceRasterPipelineBlitter = FLAGS_forceRasterPipeline;
1506 gUseSkVMBlitter = FLAGS_skvm;
1507 gSkVMAllowJIT = FLAGS_jit;
1508
1509 // The bots like having a verbose.log to upload, so always touch the file even if --verbose.
1510 if (!FLAGS_writePath.isEmpty()) {
1511 sk_mkdir(FLAGS_writePath[0]);
1512 gVLog = fopen(SkOSPath::Join(FLAGS_writePath[0], "verbose.log").c_str(), "w");
1513 }
1514 if (FLAGS_verbose) {
1515 gVLog = stderr;
1516 }
1517
1518 GrContextOptions grCtxOptions;
1519 SetCtxOptionsFromCommonFlags(&grCtxOptions);
1520
1521 dump_json(); // It's handy for the bots to assume this is ~never missing.
1522
1523 SkAutoGraphics ag;
1524 SkTaskGroup::Enabler enabled(FLAGS_threads);
1525
1526 if (nullptr == GetResourceAsData("images/color_wheel.png")) {
1527 info("Some resources are missing. Do you need to set --resourcePath?\n");
1528 }
1529 gather_gold();
1530 gather_uninteresting_hashes();
1531
1532 if (!gather_srcs()) {
1533 return 1;
1534 }
1535 // TODO(dogben): This is a bit ugly. Find a cleaner way to do this.
1536 bool defaultConfigs = true;
1537 for (int i = 0; i < argc; i++) {
1538 static constexpr char kConfigArg[] = "--config";
1539 if (strcmp(argv[i], kConfigArg) == 0) {
1540 defaultConfigs = false;
1541 break;
1542 }
1543 }
1544 if (!gather_sinks(grCtxOptions, defaultConfigs)) {
1545 return 1;
1546 }
1547 gather_tests();
1548 gPending = gSrcs->count() * gSinks->count() + gParallelTests->count() + gSerialTests->count();
1549 info("%d srcs * %d sinks + %d tests == %d tasks\n",
1550 gSrcs->count(), gSinks->count(), gParallelTests->count() + gSerialTests->count(),
1551 gPending);
1552
1553 // Kick off as much parallel work as we can, making note of any serial work we'll need to do.
1554 SkTaskGroup parallel;
1555 SkTArray<Task> serial;
1556
1557 for (TaggedSink& sink : *gSinks) {
1558 for (TaggedSrc& src : *gSrcs) {
1559 if (src->veto(sink->flags()) ||
1560 should_skip(sink.tag.c_str(), src.tag.c_str(),
1561 src.options.c_str(), src->name().c_str())) {
1562 SkAutoSpinlock lock(*gMutex);
1563 gPending--;
1564 continue;
1565 }
1566
1567 Task task(src, sink);
1568 if (src->serial() || sink->serial()) {
1569 serial.push_back(task);
1570 } else {
1571 parallel.add([task] { Task::Run(task); });
1572 }
1573 }
1574 }
1575 for (skiatest::Test& test : *gParallelTests) {
1576 parallel.add([test, grCtxOptions] { run_test(test, grCtxOptions); });
1577 }
1578
1579 // With the parallel work running, run serial tasks and tests here on main thread.
1580 for (Task& task : serial) { Task::Run(task); }
1581 for (skiatest::Test& test : *gSerialTests) { run_test(test, grCtxOptions); }
1582
1583 // Wait for any remaining parallel work to complete (including any spun off of serial tasks).
1584 parallel.wait();
1585 gDefinitelyThreadSafeWork->wait();
1586
1587 // At this point we're back in single-threaded land.
1588
1589 // We'd better have run everything.
1590 SkASSERT(gPending == 0);
1591 // Make sure we've flushed all our results to disk.
1592 dump_json();
1593
1594 if (!gFailures->empty()) {
1595 info("Failures:\n");
1596 for (const SkString& fail : *gFailures) {
1597 info("\t%s\n", fail.c_str());
1598 }
1599 info("%d failures\n", gFailures->count());
1600 return 1;
1601 }
1602
1603 SkGraphics::PurgeAllCaches();
1604 info("Finished!\n");
1605
1606 return 0;
1607 }
1608