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