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