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