• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/SkGraphics.h"
18 #include "include/private/SkChecksum.h"
19 #include "include/private/SkHalf.h"
20 #include "include/private/SkSpinlock.h"
21 #include "include/private/SkTHash.h"
22 #include "src/core/SkColorSpacePriv.h"
23 #include "src/core/SkLeanWindows.h"
24 #include "src/core/SkMD5.h"
25 #include "src/core/SkOSFile.h"
26 #include "src/core/SkTaskGroup.h"
27 #include "src/utils/SkOSPath.h"
28 #include "tests/Test.h"
29 #include "tests/TestHarness.h"
30 #include "tools/AutoreleasePool.h"
31 #include "tools/HashAndEncode.h"
32 #include "tools/ProcStats.h"
33 #include "tools/Resources.h"
34 #include "tools/ToolUtils.h"
35 #include "tools/flags/CommonFlags.h"
36 #include "tools/flags/CommonFlagsConfig.h"
37 #include "tools/ios_utils.h"
38 #include "tools/trace/ChromeTracingTracer.h"
39 #include "tools/trace/EventTracingPriv.h"
40 #include "tools/trace/SkDebugfTracer.h"
41 
42 #include <memory>
43 #include <vector>
44 
45 #include <stdlib.h>
46 
47 #ifndef SK_BUILD_FOR_WIN
48     #include <unistd.h>
49 #endif
50 
51 #if defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) && defined(SK_HAS_HEIF_LIBRARY)
52     #include <binder/IPCThreadState.h>
53 #endif
54 
55 #if defined(SK_BUILD_FOR_MAC)
56     #include "include/utils/mac/SkCGUtils.h"
57     #include "src/utils/mac/SkUniqueCFRef.h"
58 #endif
59 
60 extern bool gSkForceRasterPipelineBlitter;
61 extern bool gUseSkVMBlitter;
62 extern bool gSkVMAllowJIT;
63 
64 static DEFINE_string(src, "tests gm skp mskp lottie rive svg image colorImage",
65                      "Source types to test.");
66 static DEFINE_bool(nameByHash, false,
67                    "If true, write to FLAGS_writePath[0]/<hash>.png instead of "
68                    "to FLAGS_writePath[0]/<config>/<sourceType>/<sourceOptions>/<name>.png");
69 static DEFINE_bool2(pathOpsExtended, x, false, "Run extended pathOps tests.");
70 static DEFINE_string(matrix, "1 0 0 1",
71                     "2x2 scale+skew matrix to apply or upright when using "
72                     "'matrix' or 'upright' in config.");
73 
74 static DEFINE_string(skip, "",
75         "Space-separated config/src/srcOptions/name quadruples to skip. "
76         "'_' matches anything. '~' negates the match. E.g. \n"
77         "'--skip gpu skp _ _' will skip all SKPs drawn into the gpu config.\n"
78         "'--skip gpu skp _ _ 8888 gm _ aarects' will also skip the aarects GM on 8888.\n"
79         "'--skip ~8888 svg _ svgparse_' blocks non-8888 SVGs that contain \"svgparse_\" in "
80                                             "the name.");
81 
82 static DEFINE_string2(readPath, r, "",
83                       "If set check for equality with golden results in this directory.");
84 DEFINE_string2(writePath, w, "", "If set, write bitmaps here as .pngs.");
85 
86 
87 static DEFINE_string(uninterestingHashesFile, "",
88         "File containing a list of uninteresting hashes. If a result hashes to something in "
89         "this list, no image is written for that result.");
90 
91 static DEFINE_int(shards, 1, "We're splitting source data into this many shards.");
92 static DEFINE_int(shard,  0, "Which shard do I run?");
93 
94 static DEFINE_string(mskps, "", "Directory to read mskps from, or a single mskp file.");
95 static DEFINE_bool(forceRasterPipeline, false, "sets gSkForceRasterPipelineBlitter");
96 static DEFINE_bool(skvm, false, "sets gUseSkVMBlitter");
97 static DEFINE_bool(jit,  true,  "sets gSkVMAllowJIT");
98 
99 static DEFINE_string(bisect, "",
100         "Pair of: SKP file to bisect, followed by an l/r bisect trail string (e.g., 'lrll'). The "
101         "l/r trail specifies which half to keep at each step of a binary search through the SKP's "
102         "paths. An empty string performs no bisect. Only the SkPaths are bisected; all other draws "
103         "are thrown out. This is useful for finding a reduced repo case for path drawing bugs.");
104 
105 static DEFINE_bool(ignoreSigInt, false, "ignore SIGINT signals during test execution");
106 
107 static DEFINE_bool(checkF16, false, "Ensure that F16Norm pixels are clamped.");
108 
109 static DEFINE_string(colorImages, "",
110               "List of images and/or directories to decode with color correction. "
111               "A directory with no images is treated as a fatal error.");
112 
113 static DEFINE_bool2(veryVerbose, V, false, "tell individual tests to be verbose.");
114 
115 static DEFINE_bool(cpu, true, "Run CPU-bound work?");
116 static DEFINE_bool(gpu, true, "Run GPU-bound work?");
117 static DEFINE_bool(graphite, true, "Run Graphite 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::ContextInfo;
180 #ifdef SK_GL
181 using sk_gpu_test::GLTestContext;
182 #endif
183 
184 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
185 
186 static FILE* gVLog;
187 
188 static void vlog(const char* fmt, ...) SK_PRINTF_LIKE(1, 2);
189 
vlog(const char * fmt,...)190 static void vlog(const char* fmt, ...) {
191     if (gVLog) {
192         va_list args;
193         va_start(args, fmt);
194         vfprintf(gVLog, fmt, args);
195         fflush(gVLog);
196         va_end(args);
197     }
198 }
199 
200 static void info(const char* fmt, ...) SK_PRINTF_LIKE(1, 2);
201 
info(const char * fmt,...)202 static void info(const char* fmt, ...) {
203     va_list args;
204     va_start(args, fmt);
205 
206     if (gVLog) {
207         va_list vlogArgs;
208         va_copy(vlogArgs, args);
209         vfprintf(gVLog, fmt, vlogArgs);
210         fflush(gVLog);
211         va_end(vlogArgs);
212     }
213 
214     if (!FLAGS_quiet) {
215         vprintf(fmt, args);
216     }
217 
218     va_end(args);
219 }
220 
221 static SkTArray<SkString>* gFailures = new SkTArray<SkString>;
222 
fail(const SkString & err)223 static void fail(const SkString& err) {
224     static SkSpinlock mutex;
225     SkAutoSpinlock lock(mutex);
226     SkDebugf("\n\nFAILURE: %s\n\n", err.c_str());
227     gFailures->push_back(err);
228 }
229 
230 struct Running {
231     SkString   id;
232     SkThreadID thread;
233 
dumpRunning234     void dump() const {
235         info("\t%s\n", id.c_str());
236     }
237 };
238 
dump_json()239 static void dump_json() {
240     if (!FLAGS_writePath.isEmpty()) {
241         JsonWriter::DumpJson(FLAGS_writePath[0], FLAGS_key, FLAGS_properties);
242     }
243 }
244 
245 // We use a spinlock to make locking this in a signal handler _somewhat_ safe.
246 static SkSpinlock*        gMutex = new SkSpinlock;
247 static int                gPending;
248 static SkTArray<Running>* gRunning = new SkTArray<Running>;
249 
done(const char * config,const char * src,const char * srcOptions,const char * name)250 static void done(const char* config, const char* src, const char* srcOptions, const char* name) {
251     SkString id = SkStringPrintf("%s %s %s %s", config, src, srcOptions, name);
252     vlog("done  %s\n", id.c_str());
253     int pending;
254     {
255         SkAutoSpinlock lock(*gMutex);
256         for (int i = 0; i < gRunning->count(); i++) {
257             if (gRunning->at(i).id == id) {
258                 gRunning->removeShuffle(i);
259                 break;
260             }
261         }
262         pending = --gPending;
263     }
264 
265     // We write out dm.json file and print out a progress update every once in a while.
266     // Notice this also handles the final dm.json and progress update when pending == 0.
267     if (pending % 500 == 0) {
268         dump_json();
269 
270         int curr = sk_tools::getCurrResidentSetSizeMB(),
271             peak = sk_tools::getMaxResidentSetSizeMB();
272 
273         SkAutoSpinlock lock(*gMutex);
274         info("\n%dMB RAM, %dMB peak, %d queued, %d active:\n",
275              curr, peak, gPending - gRunning->count(), gRunning->count());
276         for (auto& task : *gRunning) {
277             task.dump();
278         }
279     }
280 }
281 
start(const char * config,const char * src,const char * srcOptions,const char * name)282 static void start(const char* config, const char* src, const char* srcOptions, const char* name) {
283     SkString id = SkStringPrintf("%s %s %s %s", config, src, srcOptions, name);
284     vlog("start %s\n", id.c_str());
285     SkAutoSpinlock lock(*gMutex);
286     gRunning->push_back({id,SkGetThreadID()});
287 }
288 
find_culprit()289 static void find_culprit() {
290     // Assumes gMutex is locked.
291     SkThreadID thisThread = SkGetThreadID();
292     for (auto& task : *gRunning) {
293         if (task.thread == thisThread) {
294             info("Likely culprit:\n");
295             task.dump();
296         }
297     }
298 }
299 
300 #if defined(SK_BUILD_FOR_WIN)
crash_handler(EXCEPTION_POINTERS * e)301     static LONG WINAPI crash_handler(EXCEPTION_POINTERS* e) {
302         static const struct {
303             const char* name;
304             DWORD code;
305         } kExceptions[] = {
306         #define _(E) {#E, E}
307             _(EXCEPTION_ACCESS_VIOLATION),
308             _(EXCEPTION_BREAKPOINT),
309             _(EXCEPTION_INT_DIVIDE_BY_ZERO),
310             _(EXCEPTION_STACK_OVERFLOW),
311             // TODO: more?
312         #undef _
313         };
314 
315         SkAutoSpinlock lock(*gMutex);
316 
317         const DWORD code = e->ExceptionRecord->ExceptionCode;
318         info("\nCaught exception %lu", code);
319         for (const auto& exception : kExceptions) {
320             if (exception.code == code) {
321                 info(" %s", exception.name);
322             }
323         }
324         info(", was running:\n");
325         for (auto& task : *gRunning) {
326             task.dump();
327         }
328         find_culprit();
329         fflush(stdout);
330 
331         // Execute default exception handler... hopefully, exit.
332         return EXCEPTION_EXECUTE_HANDLER;
333     }
334 
setup_crash_handler()335     static void setup_crash_handler() {
336         SetUnhandledExceptionFilter(crash_handler);
337     }
338 #else
339     #include <signal.h>
340     #if !defined(SK_BUILD_FOR_ANDROID)
341         #include <execinfo.h>
342 
343 #endif
344 
max_of()345     static constexpr int max_of() { return 0; }
346     template <typename... Rest>
max_of(int x,Rest...rest)347     static constexpr int max_of(int x, Rest... rest) {
348         return x > max_of(rest...) ? x : max_of(rest...);
349     }
350 
351     static void (*previous_handler[max_of(SIGABRT,SIGBUS,SIGFPE,SIGILL,SIGSEGV,SIGTERM)+1])(int);
352 
crash_handler(int sig)353     static void crash_handler(int sig) {
354         SkAutoSpinlock lock(*gMutex);
355 
356         info("\nCaught signal %d [%s] (%dMB RAM, peak %dMB), was running:\n",
357              sig, strsignal(sig),
358              sk_tools::getCurrResidentSetSizeMB(), sk_tools::getMaxResidentSetSizeMB());
359 
360         for (auto& task : *gRunning) {
361             task.dump();
362         }
363         find_culprit();
364 
365     #if !defined(SK_BUILD_FOR_ANDROID)
366         void* stack[128];
367         int count = backtrace(stack, SK_ARRAY_COUNT(stack));
368         char** symbols = backtrace_symbols(stack, count);
369         info("\nStack trace:\n");
370         for (int i = 0; i < count; i++) {
371             info("    %s\n", symbols[i]);
372         }
373     #endif
374         fflush(stdout);
375 
376         if (sig == SIGINT && FLAGS_ignoreSigInt) {
377             info("Ignoring signal %d because of --ignoreSigInt.\n"
378                  "This is probably a sign the bot is overloaded with work.\n", sig);
379         } else {
380             signal(sig, previous_handler[sig]);
381             raise(sig);
382         }
383     }
384 
setup_crash_handler()385     static void setup_crash_handler() {
386         const int kSignals[] = { SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGINT, SIGSEGV, SIGTERM };
387         for (int sig : kSignals) {
388             previous_handler[sig] = signal(sig, crash_handler);
389         }
390     }
391 #endif
392 
393 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
394 
395 struct Gold : public SkString {
GoldGold396     Gold() : SkString("") {}
GoldGold397     Gold(const SkString& sink, const SkString& src,
398          const SkString& srcOptions, const SkString& name,
399          const SkString& md5)
400         : SkString("") {
401         this->append(sink);
402         this->append(src);
403         this->append(srcOptions);
404         this->append(name);
405         this->append(md5);
406     }
407     struct Hash {
operator ()Gold::Hash408         uint32_t operator()(const Gold& g) const {
409             return SkGoodHash()((const SkString&)g);
410         }
411     };
412 };
413 static SkTHashSet<Gold, Gold::Hash>* gGold = new SkTHashSet<Gold, Gold::Hash>;
414 
add_gold(JsonWriter::BitmapResult r)415 static void add_gold(JsonWriter::BitmapResult r) {
416     gGold->add(Gold(r.config, r.sourceType, r.sourceOptions, r.name, r.md5));
417 }
418 
gather_gold()419 static void gather_gold() {
420     if (!FLAGS_readPath.isEmpty()) {
421         SkString path(FLAGS_readPath[0]);
422         path.append("/dm.json");
423         if (!JsonWriter::ReadJson(path.c_str(), add_gold)) {
424             fail(SkStringPrintf("Couldn't read %s for golden results.", path.c_str()));
425         }
426     }
427 }
428 
429 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
430 
431 #if defined(SK_BUILD_FOR_WIN)
432     static constexpr char kNewline[] = "\r\n";
433 #else
434     static constexpr char kNewline[] = "\n";
435 #endif
436 
437 static SkTHashSet<SkString>* gUninterestingHashes = new SkTHashSet<SkString>;
438 
gather_uninteresting_hashes()439 static void gather_uninteresting_hashes() {
440     if (!FLAGS_uninterestingHashesFile.isEmpty()) {
441         sk_sp<SkData> data(SkData::MakeFromFileName(FLAGS_uninterestingHashesFile[0]));
442         if (!data) {
443             info("WARNING: unable to read uninteresting hashes from %s\n",
444                  FLAGS_uninterestingHashesFile[0]);
445             return;
446         }
447 
448         // Copy to a string to make sure SkStrSplit has a terminating \0 to find.
449         SkString contents((const char*)data->data(), data->size());
450 
451         SkTArray<SkString> hashes;
452         SkStrSplit(contents.c_str(), kNewline, &hashes);
453         for (const SkString& hash : hashes) {
454             gUninterestingHashes->add(hash);
455         }
456         info("FYI: loaded %d distinct uninteresting hashes from %d lines\n",
457              gUninterestingHashes->count(), hashes.count());
458     }
459 }
460 
461 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
462 
463 struct TaggedSrc : public std::unique_ptr<Src> {
464     SkString tag;
465     SkString options;
466 };
467 
468 struct TaggedSink : public std::unique_ptr<Sink> {
469     SkString tag;
470 };
471 
472 static constexpr bool kMemcpyOK = true;
473 
474 static SkTArray<TaggedSrc,  kMemcpyOK>* gSrcs  = new SkTArray<TaggedSrc,  kMemcpyOK>;
475 static SkTArray<TaggedSink, kMemcpyOK>* gSinks = new SkTArray<TaggedSink, kMemcpyOK>;
476 
in_shard()477 static bool in_shard() {
478     static int N = 0;
479     return N++ % FLAGS_shards == FLAGS_shard;
480 }
481 
push_src(const char * tag,ImplicitString options,Src * inSrc)482 static void push_src(const char* tag, ImplicitString options, Src* inSrc) {
483     std::unique_ptr<Src> src(inSrc);
484     if (in_shard() && FLAGS_src.contains(tag) &&
485         !CommandLineFlags::ShouldSkip(FLAGS_match, src->name().c_str())) {
486         TaggedSrc& s = gSrcs->push_back();
487         s.reset(src.release());
488         s.tag = tag;
489         s.options = options;
490     }
491 }
492 
push_codec_src(Path path,CodecSrc::Mode mode,CodecSrc::DstColorType dstColorType,SkAlphaType dstAlphaType,float scale)493 static void push_codec_src(Path path, CodecSrc::Mode mode, CodecSrc::DstColorType dstColorType,
494         SkAlphaType dstAlphaType, float scale) {
495     if (FLAGS_simpleCodec) {
496         const bool simple = CodecSrc::kCodec_Mode == mode || CodecSrc::kAnimated_Mode == mode;
497         if (!simple || dstColorType != CodecSrc::kGetFromCanvas_DstColorType || scale != 1.0f) {
498             // Only decode in the simple case.
499             return;
500         }
501     }
502     SkString folder;
503     switch (mode) {
504         case CodecSrc::kCodec_Mode:
505             folder.append("codec");
506             break;
507         case CodecSrc::kCodecZeroInit_Mode:
508             folder.append("codec_zero_init");
509             break;
510         case CodecSrc::kScanline_Mode:
511             folder.append("scanline");
512             break;
513         case CodecSrc::kStripe_Mode:
514             folder.append("stripe");
515             break;
516         case CodecSrc::kCroppedScanline_Mode:
517             folder.append("crop");
518             break;
519         case CodecSrc::kSubset_Mode:
520             folder.append("codec_subset");
521             break;
522         case CodecSrc::kAnimated_Mode:
523             folder.append("codec_animated");
524             break;
525     }
526 
527     switch (dstColorType) {
528         case CodecSrc::kGrayscale_Always_DstColorType:
529             folder.append("_kGray8");
530             break;
531         case CodecSrc::kNonNative8888_Always_DstColorType:
532             folder.append("_kNonNative");
533             break;
534         default:
535             break;
536     }
537 
538     switch (dstAlphaType) {
539         case kPremul_SkAlphaType:
540             folder.append("_premul");
541             break;
542         case kUnpremul_SkAlphaType:
543             folder.append("_unpremul");
544             break;
545         default:
546             break;
547     }
548 
549     if (1.0f != scale) {
550         folder.appendf("_%.3f", scale);
551     }
552 
553     CodecSrc* src = new CodecSrc(path, mode, dstColorType, dstAlphaType, scale);
554     push_src("image", folder, src);
555 }
556 
push_android_codec_src(Path path,CodecSrc::DstColorType dstColorType,SkAlphaType dstAlphaType,int sampleSize)557 static void push_android_codec_src(Path path, CodecSrc::DstColorType dstColorType,
558         SkAlphaType dstAlphaType, int sampleSize) {
559     SkString folder;
560     folder.append("scaled_codec");
561 
562     switch (dstColorType) {
563         case CodecSrc::kGrayscale_Always_DstColorType:
564             folder.append("_kGray8");
565             break;
566         case CodecSrc::kNonNative8888_Always_DstColorType:
567             folder.append("_kNonNative");
568             break;
569         default:
570             break;
571     }
572 
573     switch (dstAlphaType) {
574         case kPremul_SkAlphaType:
575             folder.append("_premul");
576             break;
577         case kUnpremul_SkAlphaType:
578             folder.append("_unpremul");
579             break;
580         default:
581             break;
582     }
583 
584     if (1 != sampleSize) {
585         folder.appendf("_%.3f", 1.0f / (float) sampleSize);
586     }
587 
588     AndroidCodecSrc* src = new AndroidCodecSrc(path, dstColorType, dstAlphaType, sampleSize);
589     push_src("image", folder, src);
590 }
591 
push_image_gen_src(Path path,ImageGenSrc::Mode mode,SkAlphaType alphaType,bool isGpu)592 static void push_image_gen_src(Path path, ImageGenSrc::Mode mode, SkAlphaType alphaType, bool isGpu)
593 {
594     SkString folder;
595     switch (mode) {
596         case ImageGenSrc::kCodec_Mode:
597             folder.append("gen_codec");
598             break;
599         case ImageGenSrc::kPlatform_Mode:
600             folder.append("gen_platform");
601             break;
602     }
603 
604     if (isGpu) {
605         folder.append("_gpu");
606     } else {
607         switch (alphaType) {
608             case kOpaque_SkAlphaType:
609                 folder.append("_opaque");
610                 break;
611             case kPremul_SkAlphaType:
612                 folder.append("_premul");
613                 break;
614             case kUnpremul_SkAlphaType:
615                 folder.append("_unpremul");
616                 break;
617             default:
618                 break;
619         }
620     }
621 
622     ImageGenSrc* src = new ImageGenSrc(path, mode, alphaType, isGpu);
623     push_src("image", folder, src);
624 }
625 
626 #ifdef SK_ENABLE_ANDROID_UTILS
push_brd_src(Path path,CodecSrc::DstColorType dstColorType,BRDSrc::Mode mode,uint32_t sampleSize)627 static void push_brd_src(Path path, CodecSrc::DstColorType dstColorType, BRDSrc::Mode mode,
628         uint32_t sampleSize) {
629     SkString folder("brd_android_codec");
630     switch (mode) {
631         case BRDSrc::kFullImage_Mode:
632             break;
633         case BRDSrc::kDivisor_Mode:
634             folder.append("_divisor");
635             break;
636         default:
637             SkASSERT(false);
638             return;
639     }
640 
641     switch (dstColorType) {
642         case CodecSrc::kGetFromCanvas_DstColorType:
643             break;
644         case CodecSrc::kGrayscale_Always_DstColorType:
645             folder.append("_kGray");
646             break;
647         default:
648             SkASSERT(false);
649             return;
650     }
651 
652     if (1 != sampleSize) {
653         folder.appendf("_%.3f", 1.0f / (float) sampleSize);
654     }
655 
656     BRDSrc* src = new BRDSrc(path, mode, dstColorType, sampleSize);
657     push_src("image", folder, src);
658 }
659 
push_brd_srcs(Path path,bool gray)660 static void push_brd_srcs(Path path, bool gray) {
661     if (gray) {
662         // Only run grayscale to one sampleSize and Mode. Though interesting
663         // to test grayscale, it should not reveal anything across various
664         // sampleSizes and Modes
665         // Arbitrarily choose Mode and sampleSize.
666         push_brd_src(path, CodecSrc::kGrayscale_Always_DstColorType,
667                      BRDSrc::kFullImage_Mode, 2);
668     }
669 
670     // Test on a variety of sampleSizes, making sure to include:
671     // - 2, 4, and 8, which are natively supported by jpeg
672     // - multiples of 2 which are not divisible by 4 (analogous for 4)
673     // - larger powers of two, since BRD clients generally use powers of 2
674     // We will only produce output for the larger sizes on large images.
675     const uint32_t sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 24, 32, 64 };
676 
677     const BRDSrc::Mode modes[] = { BRDSrc::kFullImage_Mode, BRDSrc::kDivisor_Mode, };
678 
679     for (uint32_t sampleSize : sampleSizes) {
680         for (BRDSrc::Mode mode : modes) {
681             push_brd_src(path, CodecSrc::kGetFromCanvas_DstColorType, mode, sampleSize);
682         }
683     }
684 }
685 #endif // SK_ENABLE_ANDROID_UTILS
686 
push_codec_srcs(Path path)687 static void push_codec_srcs(Path path) {
688     sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
689     if (!encoded) {
690         info("Couldn't read %s.", path.c_str());
691         return;
692     }
693     std::unique_ptr<SkCodec> codec = SkCodec::MakeFromData(encoded);
694     if (nullptr == codec) {
695         info("Couldn't create codec for %s.", path.c_str());
696         return;
697     }
698 
699     // native scaling is only supported by WEBP and JPEG
700     bool supportsNativeScaling = false;
701 
702     SkTArray<CodecSrc::Mode> nativeModes;
703     nativeModes.push_back(CodecSrc::kCodec_Mode);
704     nativeModes.push_back(CodecSrc::kCodecZeroInit_Mode);
705     switch (codec->getEncodedFormat()) {
706         case SkEncodedImageFormat::kJPEG:
707             nativeModes.push_back(CodecSrc::kScanline_Mode);
708             nativeModes.push_back(CodecSrc::kStripe_Mode);
709             nativeModes.push_back(CodecSrc::kCroppedScanline_Mode);
710             supportsNativeScaling = true;
711             break;
712         case SkEncodedImageFormat::kWEBP:
713             nativeModes.push_back(CodecSrc::kSubset_Mode);
714             supportsNativeScaling = true;
715             break;
716         case SkEncodedImageFormat::kDNG:
717             break;
718         default:
719             nativeModes.push_back(CodecSrc::kScanline_Mode);
720             break;
721     }
722 
723     SkTArray<CodecSrc::DstColorType> colorTypes;
724     colorTypes.push_back(CodecSrc::kGetFromCanvas_DstColorType);
725     colorTypes.push_back(CodecSrc::kNonNative8888_Always_DstColorType);
726     switch (codec->getInfo().colorType()) {
727         case kGray_8_SkColorType:
728             colorTypes.push_back(CodecSrc::kGrayscale_Always_DstColorType);
729             break;
730         default:
731             break;
732     }
733 
734     SkTArray<SkAlphaType> alphaModes;
735     alphaModes.push_back(kPremul_SkAlphaType);
736     if (codec->getInfo().alphaType() != kOpaque_SkAlphaType) {
737         alphaModes.push_back(kUnpremul_SkAlphaType);
738     }
739 
740     for (CodecSrc::Mode mode : nativeModes) {
741         for (CodecSrc::DstColorType colorType : colorTypes) {
742             for (SkAlphaType alphaType : alphaModes) {
743                 // Only test kCroppedScanline_Mode when the alpha type is premul.  The test is
744                 // slow and won't be interestingly different with different alpha types.
745                 if (CodecSrc::kCroppedScanline_Mode == mode &&
746                         kPremul_SkAlphaType != alphaType) {
747                     continue;
748                 }
749 
750                 push_codec_src(path, mode, colorType, alphaType, 1.0f);
751 
752                 // Skip kNonNative on different native scales.  It won't be interestingly
753                 // different.
754                 if (supportsNativeScaling &&
755                         CodecSrc::kNonNative8888_Always_DstColorType == colorType) {
756                     // Native Scales
757                     // SkJpegCodec natively supports scaling to the following:
758                     for (auto scale : { 0.125f, 0.25f, 0.375f, 0.5f, 0.625f, 0.750f, 0.875f }) {
759                         push_codec_src(path, mode, colorType, alphaType, scale);
760                     }
761                 }
762             }
763         }
764     }
765 
766     {
767         std::vector<SkCodec::FrameInfo> frameInfos = codec->getFrameInfo();
768         if (frameInfos.size() > 1) {
769             for (auto dstCT : { CodecSrc::kNonNative8888_Always_DstColorType,
770                     CodecSrc::kGetFromCanvas_DstColorType }) {
771                 for (auto at : { kUnpremul_SkAlphaType, kPremul_SkAlphaType }) {
772                     push_codec_src(path, CodecSrc::kAnimated_Mode, dstCT, at, 1.0f);
773                 }
774             }
775             for (float scale : { .5f, .33f }) {
776                 push_codec_src(path, CodecSrc::kAnimated_Mode, CodecSrc::kGetFromCanvas_DstColorType,
777                                kPremul_SkAlphaType, scale);
778             }
779         }
780 
781     }
782 
783     if (FLAGS_simpleCodec) {
784         return;
785     }
786 
787     const int sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
788 
789     for (int sampleSize : sampleSizes) {
790         for (CodecSrc::DstColorType colorType : colorTypes) {
791             for (SkAlphaType alphaType : alphaModes) {
792                 // We can exercise all of the kNonNative support code in the swizzler with just a
793                 // few sample sizes.  Skip the rest.
794                 if (CodecSrc::kNonNative8888_Always_DstColorType == colorType && sampleSize > 3) {
795                     continue;
796                 }
797 
798                 push_android_codec_src(path, colorType, alphaType, sampleSize);
799             }
800         }
801     }
802 
803     const char* ext = strrchr(path.c_str(), '.');
804     if (ext) {
805         ext++;
806 
807         static const char* const rawExts[] = {
808             "arw", "cr2", "dng", "nef", "nrw", "orf", "raf", "rw2", "pef", "srw",
809             "ARW", "CR2", "DNG", "NEF", "NRW", "ORF", "RAF", "RW2", "PEF", "SRW",
810         };
811         for (const char* rawExt : rawExts) {
812             if (0 == strcmp(rawExt, ext)) {
813                 // RAW is not supported by image generator (skbug.com/5079) or BRD.
814                 return;
815             }
816         }
817 
818 #ifdef SK_ENABLE_ANDROID_UTILS
819         static const char* const brdExts[] = {
820             "jpg", "jpeg", "png", "webp",
821             "JPG", "JPEG", "PNG", "WEBP",
822         };
823         for (const char* brdExt : brdExts) {
824             if (0 == strcmp(brdExt, ext)) {
825                 bool gray = codec->getInfo().colorType() == kGray_8_SkColorType;
826                 push_brd_srcs(path, gray);
827                 break;
828             }
829         }
830 #endif
831     }
832 
833     // Push image generator GPU test.
834     push_image_gen_src(path, ImageGenSrc::kCodec_Mode, codec->getInfo().alphaType(), true);
835 
836     // Push image generator CPU tests.
837     for (SkAlphaType alphaType : alphaModes) {
838         push_image_gen_src(path, ImageGenSrc::kCodec_Mode, alphaType, false);
839 
840 #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
841         if (SkEncodedImageFormat::kWEBP != codec->getEncodedFormat() &&
842             SkEncodedImageFormat::kWBMP != codec->getEncodedFormat() &&
843             kUnpremul_SkAlphaType != alphaType)
844         {
845             push_image_gen_src(path, ImageGenSrc::kPlatform_Mode, alphaType, false);
846         }
847 #elif defined(SK_BUILD_FOR_WIN)
848         if (SkEncodedImageFormat::kWEBP != codec->getEncodedFormat() &&
849             SkEncodedImageFormat::kWBMP != codec->getEncodedFormat())
850         {
851             push_image_gen_src(path, ImageGenSrc::kPlatform_Mode, alphaType, false);
852         }
853 #elif defined(SK_ENABLE_NDK_IMAGES)
854         push_image_gen_src(path, ImageGenSrc::kPlatform_Mode, alphaType, false);
855 #endif
856     }
857 }
858 
859 template <typename T>
gather_file_srcs(const CommandLineFlags::StringArray & flags,const char * ext,const char * src_name=nullptr)860 void gather_file_srcs(const CommandLineFlags::StringArray& flags,
861                       const char*                          ext,
862                       const char*                          src_name = nullptr) {
863     if (!src_name) {
864         // With the exception of Lottie files, the source name is the extension.
865         src_name = ext;
866     }
867 
868     for (int i = 0; i < flags.count(); i++) {
869         const char* path = flags[i];
870         if (sk_isdir(path)) {
871             SkOSFile::Iter it(path, ext);
872             for (SkString file; it.next(&file); ) {
873                 push_src(src_name, "", new T(SkOSPath::Join(path, file.c_str())));
874             }
875         } else {
876             push_src(src_name, "", new T(path));
877         }
878     }
879 }
880 
gather_srcs()881 static bool gather_srcs() {
882     for (skiagm::GMFactory f : skiagm::GMRegistry::Range()) {
883         push_src("gm", "", new GMSrc(f));
884     }
885 
886     gather_file_srcs<SKPSrc>(FLAGS_skps, "skp");
887     gather_file_srcs<MSKPSrc>(FLAGS_mskps, "mskp");
888 #if defined(SK_ENABLE_SKOTTIE)
889     gather_file_srcs<SkottieSrc>(FLAGS_lotties, "json", "lottie");
890 #endif
891 #if defined(SK_ENABLE_SKRIVE)
892     gather_file_srcs<SkRiveSrc>(FLAGS_rives, "flr", "rive");
893 #endif
894 #if defined(SK_ENABLE_SVG)
895     gather_file_srcs<SVGSrc>(FLAGS_svgs, "svg");
896 #endif
897     if (!FLAGS_bisect.isEmpty()) {
898         // An empty l/r trail string will draw all the paths.
899         push_src("bisect", "",
900                  new BisectSrc(FLAGS_bisect[0], FLAGS_bisect.count() > 1 ? FLAGS_bisect[1] : ""));
901     }
902 
903     SkTArray<SkString> images;
904     if (!CommonFlags::CollectImages(FLAGS_images, &images)) {
905         return false;
906     }
907 
908     for (const SkString& image : images) {
909         push_codec_srcs(image);
910     }
911 
912     SkTArray<SkString> colorImages;
913     if (!CommonFlags::CollectImages(FLAGS_colorImages, &colorImages)) {
914         return false;
915     }
916 
917     for (const SkString& colorImage : colorImages) {
918         push_src("colorImage", "decode_native", new ColorCodecSrc(colorImage, false));
919         push_src("colorImage", "decode_to_dst", new ColorCodecSrc(colorImage,  true));
920     }
921 
922     return true;
923 }
924 
push_sink(const SkCommandLineConfig & config,Sink * s)925 static void push_sink(const SkCommandLineConfig& config, Sink* s) {
926     std::unique_ptr<Sink> sink(s);
927 
928     // Try a simple Src as a canary.  If it fails, skip this sink.
929     struct : public Src {
930         Result draw(GrDirectContext*, SkCanvas* c) const override {
931             c->drawRect(SkRect::MakeWH(1,1), SkPaint());
932             return Result::Ok();
933         }
934         SkISize size() const override { return SkISize::Make(16, 16); }
935         Name name() const override { return "justOneRect"; }
936     } justOneRect;
937 
938     SkBitmap bitmap;
939     SkDynamicMemoryWStream stream;
940     SkString log;
941     Result result = sink->draw(justOneRect, &bitmap, &stream, &log);
942     if (result.isFatal()) {
943         info("Could not run %s: %s\n", config.getTag().c_str(), result.c_str());
944         exit(1);
945     }
946 
947     TaggedSink& ts = gSinks->push_back();
948     ts.reset(sink.release());
949     ts.tag = config.getTag();
950 }
951 
create_sink(const GrContextOptions & grCtxOptions,const SkCommandLineConfig * config)952 static Sink* create_sink(const GrContextOptions& grCtxOptions, const SkCommandLineConfig* config) {
953     if (FLAGS_gpu) {
954         if (const SkCommandLineConfigGpu* gpuConfig = config->asConfigGpu()) {
955             GrContextFactory testFactory(grCtxOptions);
956             if (!testFactory.get(gpuConfig->getContextType(), gpuConfig->getContextOverrides())) {
957                 info("WARNING: can not create GPU context for config '%s'. "
958                      "GM tests will be skipped.\n", gpuConfig->getTag().c_str());
959                 return nullptr;
960             }
961             if (gpuConfig->getTestThreading()) {
962                 SkASSERT(!gpuConfig->getTestPersistentCache());
963                 return new GPUThreadTestingSink(gpuConfig, grCtxOptions);
964             } else if (gpuConfig->getTestPersistentCache()) {
965                 return new GPUPersistentCacheTestingSink(gpuConfig, grCtxOptions);
966             } else if (gpuConfig->getTestPrecompile()) {
967                 return new GPUPrecompileTestingSink(gpuConfig, grCtxOptions);
968             } else if (gpuConfig->getUseDDLSink()) {
969                 return new GPUDDLSink(gpuConfig, grCtxOptions);
970             } else if (gpuConfig->getOOPRish()) {
971                 return new GPUOOPRSink(gpuConfig, grCtxOptions);
972             } else {
973                 return new GPUSink(gpuConfig, grCtxOptions);
974             }
975         }
976     }
977 #ifdef SK_GRAPHITE_ENABLED
978     if (FLAGS_graphite) {
979         if (const SkCommandLineConfigGraphite *graphiteConfig = config->asConfigGraphite()) {
980             return new GraphiteSink(graphiteConfig);
981         }
982     }
983 #endif
984     if (const SkCommandLineConfigSvg* svgConfig = config->asConfigSvg()) {
985         int pageIndex = svgConfig->getPageIndex();
986         return new SVGSink(pageIndex);
987     }
988 
989 #define SINK(t, sink, ...) if (config->getBackend().equals(t)) return new sink(__VA_ARGS__)
990 
991     if (FLAGS_cpu) {
992         SINK("r8",          RasterSink, kR8_unorm_SkColorType);
993         SINK("565",         RasterSink, kRGB_565_SkColorType);
994         SINK("4444",        RasterSink, kARGB_4444_SkColorType);
995         SINK("8888",        RasterSink, kN32_SkColorType);
996         SINK("rgba",        RasterSink, kRGBA_8888_SkColorType);
997         SINK("bgra",        RasterSink, kBGRA_8888_SkColorType);
998         SINK("rgbx",        RasterSink, kRGB_888x_SkColorType);
999         SINK("1010102",     RasterSink, kRGBA_1010102_SkColorType);
1000         SINK("101010x",     RasterSink, kRGB_101010x_SkColorType);
1001         SINK("bgra1010102", RasterSink, kBGRA_1010102_SkColorType);
1002         SINK("bgr101010x",  RasterSink, kBGR_101010x_SkColorType);
1003         SINK("f16",         RasterSink, kRGBA_F16_SkColorType);
1004         SINK("f16norm",     RasterSink, kRGBA_F16Norm_SkColorType);
1005         SINK("f32",         RasterSink, kRGBA_F32_SkColorType);
1006         SINK("srgba",       RasterSink, kSRGBA_8888_SkColorType);
1007 
1008         SINK("pdf",         PDFSink, false, SK_ScalarDefaultRasterDPI);
1009         SINK("skp",         SKPSink);
1010         SINK("svg",         SVGSink);
1011         SINK("null",        NullSink);
1012         SINK("xps",         XPSSink);
1013         SINK("pdfa",        PDFSink, true,  SK_ScalarDefaultRasterDPI);
1014         SINK("pdf300",      PDFSink, false, 300);
1015         SINK("jsdebug",     DebugSink);
1016     }
1017 #undef SINK
1018     return nullptr;
1019 }
1020 
create_via(const SkString & tag,Sink * wrapped)1021 static Sink* create_via(const SkString& tag, Sink* wrapped) {
1022 #define VIA(t, via, ...) if (tag.equals(t)) return new via(__VA_ARGS__)
1023 #ifdef TEST_VIA_SVG
1024     VIA("svg",       ViaSVG,               wrapped);
1025 #endif
1026     VIA("serialize", ViaSerialization,     wrapped);
1027     VIA("pic",       ViaPicture,           wrapped);
1028     VIA("rtblend",   ViaRuntimeBlend,      wrapped);
1029 
1030     if (FLAGS_matrix.count() == 4) {
1031         SkMatrix m;
1032         m.reset();
1033         m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
1034         m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
1035         m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
1036         m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
1037         VIA("matrix",  ViaMatrix,  m, wrapped);
1038         VIA("upright", ViaUpright, m, wrapped);
1039     }
1040 
1041 #undef VIA
1042 
1043     return nullptr;
1044 }
1045 
gather_sinks(const GrContextOptions & grCtxOptions,bool defaultConfigs)1046 static bool gather_sinks(const GrContextOptions& grCtxOptions, bool defaultConfigs) {
1047     SkCommandLineConfigArray configs;
1048     ParseConfigs(FLAGS_config, &configs);
1049     AutoreleasePool pool;
1050     for (int i = 0; i < configs.count(); i++) {
1051         const SkCommandLineConfig& config = *configs[i];
1052         Sink* sink = create_sink(grCtxOptions, &config);
1053         if (sink == nullptr) {
1054             info("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
1055                  config.getTag().c_str());
1056             continue;
1057         }
1058 
1059         // The command line config already parsed out the via-style color space. Apply it here.
1060         sink->setColorSpace(config.refColorSpace());
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 (%.3g)",
1328                                       tf.a, tf.b, tf.c, tf.d, tf.e, tf.f+1);
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.fName)) {
1450             continue;
1451         }
1452         if (test.fNeedsGpu && FLAGS_gpu) {
1453             gSerialTests->push_back(test);
1454         } else if (test.fNeedsGraphite && FLAGS_graphite) {
1455             gSerialTests->push_back(test);
1456         } else if (!test.fNeedsGpu && !test.fNeedsGraphite && FLAGS_cpu) {
1457             gParallelTests->push_back(test);
1458         }
1459     }
1460 }
1461 
run_test(skiatest::Test test,const GrContextOptions & grCtxOptions)1462 static void run_test(skiatest::Test test, const GrContextOptions& grCtxOptions) {
1463     struct : public skiatest::Reporter {
1464         void reportFailed(const skiatest::Failure& failure) override {
1465             fail(failure.toString());
1466         }
1467         bool allowExtendedTest() const override {
1468             return FLAGS_pathOpsExtended;
1469         }
1470         bool verbose() const override { return FLAGS_veryVerbose; }
1471     } reporter;
1472 
1473     if (!FLAGS_dryRun && !should_skip("_", "tests", "_", test.fName)) {
1474         AutoreleasePool pool;
1475         GrContextOptions options = grCtxOptions;
1476         test.modifyGrContextOptions(&options);
1477 
1478         skiatest::ReporterContext ctx(&reporter, SkString(test.fName));
1479         start("unit", "test", "", test.fName);
1480         test.run(&reporter, options);
1481     }
1482     done("unit", "test", "", test.fName);
1483 }
1484 
1485 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1486 
CurrentTestHarness()1487 TestHarness CurrentTestHarness() {
1488     return TestHarness::kDM;
1489 }
1490 
1491 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1492 
main(int argc,char ** argv)1493 int main(int argc, char** argv) {
1494 #if defined(__MSVC_RUNTIME_CHECKS)
1495     _RTC_SetErrorFunc(RuntimeCheckErrorFunc);
1496 #endif
1497 #if defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) && defined(SK_HAS_HEIF_LIBRARY)
1498     android::ProcessState::self()->startThreadPool();
1499 #endif
1500     CommandLineFlags::Parse(argc, argv);
1501 
1502     initializeEventTracingForTools();
1503 
1504 #if !defined(SK_BUILD_FOR_GOOGLE3) && defined(SK_BUILD_FOR_IOS)
1505     cd_Documents();
1506 #endif
1507     setbuf(stdout, nullptr);
1508     setup_crash_handler();
1509 
1510     CommonFlags::SetDefaultFontMgr();
1511     CommonFlags::SetAnalyticAA();
1512 
1513     gSkForceRasterPipelineBlitter = FLAGS_forceRasterPipeline;
1514     gUseSkVMBlitter               = FLAGS_skvm;
1515     gSkVMAllowJIT                 = FLAGS_jit;
1516 
1517     // The bots like having a verbose.log to upload, so always touch the file even if --verbose.
1518     if (!FLAGS_writePath.isEmpty()) {
1519         sk_mkdir(FLAGS_writePath[0]);
1520         gVLog = fopen(SkOSPath::Join(FLAGS_writePath[0], "verbose.log").c_str(), "w");
1521     }
1522     if (FLAGS_verbose) {
1523         gVLog = stderr;
1524     }
1525 
1526     GrContextOptions grCtxOptions;
1527     CommonFlags::SetCtxOptions(&grCtxOptions);
1528 
1529     dump_json();  // It's handy for the bots to assume this is ~never missing.
1530 
1531     SkAutoGraphics ag;
1532     SkTaskGroup::Enabler enabled(FLAGS_threads);
1533 
1534     if (nullptr == GetResourceAsData("images/color_wheel.png")) {
1535         info("Some resources are missing.  Do you need to set --resourcePath?\n");
1536     }
1537     gather_gold();
1538     gather_uninteresting_hashes();
1539 
1540     if (!gather_srcs()) {
1541         return 1;
1542     }
1543     // TODO(dogben): This is a bit ugly. Find a cleaner way to do this.
1544     bool defaultConfigs = true;
1545     for (int i = 0; i < argc; i++) {
1546         static constexpr char kConfigArg[] = "--config";
1547         if (strcmp(argv[i], kConfigArg) == 0) {
1548             defaultConfigs = false;
1549             break;
1550         }
1551     }
1552     if (!gather_sinks(grCtxOptions, defaultConfigs)) {
1553         return 1;
1554     }
1555     gather_tests();
1556     gPending = gSrcs->count() * gSinks->count() + gParallelTests->count() + gSerialTests->count();
1557     info("%d srcs * %d sinks + %d tests == %d tasks\n",
1558          gSrcs->count(), gSinks->count(), gParallelTests->count() + gSerialTests->count(),
1559          gPending);
1560 
1561     // Kick off as much parallel work as we can, making note of any serial work we'll need to do.
1562     SkTaskGroup parallel;
1563     SkTArray<Task> serial;
1564 
1565     for (TaggedSink& sink : *gSinks) {
1566         for (TaggedSrc& src : *gSrcs) {
1567             if (src->veto(sink->flags()) ||
1568                 should_skip(sink.tag.c_str(), src.tag.c_str(),
1569                             src.options.c_str(), src->name().c_str())) {
1570                 SkAutoSpinlock lock(*gMutex);
1571                 gPending--;
1572                 continue;
1573             }
1574 
1575             Task task(src, sink);
1576             if (src->serial() || sink->serial()) {
1577                 serial.push_back(task);
1578             } else {
1579                 parallel.add([task] { Task::Run(task); });
1580             }
1581         }
1582     }
1583     for (skiatest::Test& test : *gParallelTests) {
1584         parallel.add([test, grCtxOptions] { run_test(test, grCtxOptions); });
1585     }
1586 
1587     // With the parallel work running, run serial tasks and tests here on main thread.
1588     for (Task& task : serial) { Task::Run(task); }
1589     for (skiatest::Test& test : *gSerialTests) { run_test(test, grCtxOptions); }
1590 
1591     // Wait for any remaining parallel work to complete (including any spun off of serial tasks).
1592     parallel.wait();
1593     gDefinitelyThreadSafeWork->wait();
1594 
1595     // At this point we're back in single-threaded land.
1596 
1597     // We'd better have run everything.
1598     SkASSERT(gPending == 0);
1599     // Make sure we've flushed all our results to disk.
1600     dump_json();
1601 
1602     if (!gFailures->empty()) {
1603         info("Failures:\n");
1604         for (const SkString& fail : *gFailures) {
1605             info("\t%s\n", fail.c_str());
1606         }
1607         info("%d failures\n", gFailures->count());
1608         return 1;
1609     }
1610 
1611     SkGraphics::PurgeAllCaches();
1612     info("Finished!\n");
1613 
1614     return 0;
1615 }
1616