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