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