blob: 30b9adbb2981a97be97d3015ff2b0d05753fd325 [file] [log] [blame]
scroggo478652e2015-03-25 07:11:02 -07001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "dm/DMJsonWriter.h"
9#include "dm/DMSrcSink.h"
Tyler Denniston45f94f82020-02-04 16:09:08 -050010#include "gm/verifiers/gmverifier.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "include/private/SkSpinlock.h"
23#include "include/private/SkTHash.h"
24#include "src/core/SkColorSpacePriv.h"
Ben Wagnerab6eefe2019-05-20 11:02:49 -040025#include "src/core/SkLeanWindows.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#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"
Jim Van Verth8a9a3712019-05-31 10:49:12 -040031#include "tools/AutoreleasePool.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050032#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"
mtklein@google.comd36522d2013-10-16 13:02:15 +000042
John Stilesfbd050b2020-08-03 13:21:46 -040043#include <memory>
scroggo19b91532016-10-24 09:03:26 -070044#include <vector>
45
bungeman60e0fee2015-08-26 05:15:46 -070046#include <stdlib.h>
47
Mike Klein8f11d4d2018-01-24 12:42:55 -050048#ifndef SK_BUILD_FOR_WIN
scroggo27a58342015-10-28 08:56:41 -070049 #include <unistd.h>
50#endif
51
Dongwon Kang0c7861f2018-02-16 10:55:21 -080052#if defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) && defined(SK_HAS_HEIF_LIBRARY)
Mike Klein055f5352020-01-30 11:11:12 -060053 #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"
Dongwon Kang0c7861f2018-02-16 10:55:21 -080059#endif
60
Mike Reed7c26ca72017-07-05 15:45:52 -040061extern bool gSkForceRasterPipelineBlitter;
Mike Klein9ae06632020-01-28 12:41:02 -060062extern bool gUseSkVMBlitter;
Mike Klein813e8cc2020-08-05 09:33:38 -050063extern bool gSkVMAllowJIT;
Mike Reed7c26ca72017-07-05 15:45:52 -040064
Florin Malita02616ea2020-06-25 13:33:17 -040065static DEFINE_string(src, "tests gm skp mskp lottie rive svg image colorImage",
66 "Source types to test.");
Mike Klein84836b72019-03-21 11:31:36 -050067static 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");
70static DEFINE_bool2(pathOpsExtended, x, false, "Run extended pathOps tests.");
71static DEFINE_string(matrix, "1 0 0 1",
72 "2x2 scale+skew matrix to apply or upright when using "
73 "'matrix' or 'upright' in config.");
mtklein@google.comd36522d2013-10-16 13:02:15 +000074
Mike Kleina54d3802020-07-24 12:37:32 -050075static DEFINE_string(skip, "",
76 "Space-separated config/src/srcOptions/name quadruples to skip. "
Chris Daltonecf78ac2017-08-15 15:43:08 -060077 "'_' matches anything. '~' negates the match. E.g. \n"
Mike Kleina54d3802020-07-24 12:37:32 -050078 "'--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 "
Chris Daltonecf78ac2017-08-15 15:43:08 -060081 "the name.");
mtkleina2ef6422015-01-15 13:44:22 -080082
Mike Klein84836b72019-03-21 11:31:36 -050083static DEFINE_string2(readPath, r, "",
84 "If set check for equality with golden results in this directory.");
Mike Kleinc6142d82019-03-25 10:54:59 -050085DEFINE_string2(writePath, w, "", "If set, write bitmaps here as .pngs.");
86
Brian Osman57796b32019-01-25 18:02:59 +000087
Mike Klein84836b72019-03-21 11:31:36 -050088static DEFINE_string(uninterestingHashesFile, "",
borenet09ed4802015-04-03 14:15:33 -070089 "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
Mike Klein5b3f3432019-03-21 11:42:21 -050092static DEFINE_int(shards, 1, "We're splitting source data into this many shards.");
93static DEFINE_int(shard, 0, "Which shard do I run?");
mtklein6393c062015-04-27 08:45:01 -070094
Mike Klein84836b72019-03-21 11:31:36 -050095static DEFINE_string(mskps, "", "Directory to read mskps from, or a single mskp file.");
96static DEFINE_bool(forceRasterPipeline, false, "sets gSkForceRasterPipelineBlitter");
Mike Klein9ae06632020-01-28 12:41:02 -060097static DEFINE_bool(skvm, false, "sets gUseSkVMBlitter");
Mike Klein813e8cc2020-08-05 09:33:38 -050098static DEFINE_bool(jit, true, "sets gSkVMAllowJIT");
halcanary45420a92016-06-02 12:41:14 -070099
Mike Klein84836b72019-03-21 11:31:36 -0500100static DEFINE_string(bisect, "",
Chris Dalton184c37e2018-09-28 11:27:39 -0600101 "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
Mike Klein84836b72019-03-21 11:31:36 -0500106static DEFINE_bool(ignoreSigInt, false, "ignore SIGINT signals during test execution");
Derek Sollenbergeredfe3df2017-07-19 15:25:24 -0400107
Mike Klein84836b72019-03-21 11:31:36 -0500108static DEFINE_bool(checkF16, false, "Ensure that F16Norm pixels are clamped.");
Brian Osman582f6862019-03-11 11:20:55 -0400109
Mike Klein19fb3972019-03-21 13:08:08 -0500110static 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
Mike Kleind0f321b2019-03-22 13:15:11 -0500114static DEFINE_bool2(veryVerbose, V, false, "tell individual tests to be verbose.");
115
Leon Scroggins III1ff07062020-07-27 14:52:19 -0400116static DEFINE_bool(cpu, true, "Run CPU-bound work?");
117static DEFINE_bool(gpu, true, "Run GPU-bound work?");
Mike Klein629f5fc2019-03-22 14:55:19 -0500118
119static DEFINE_bool(dryRun, false,
120 "just print the tests that would be run, without actually running them.");
121
122static 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
126static 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.");
Mike Klein19fb3972019-03-21 13:08:08 -0500129
Mike Kleinc6142d82019-03-25 10:54:59 -0500130static 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
140static DEFINE_bool2(quiet, q, false, "if true, don't print status updates.");
141static DEFINE_bool2(verbose, v, false, "enable verbose output from the test driver.");
142
143static DEFINE_string(skps, "skps", "Directory to read skps from.");
144static DEFINE_string(lotties, "lotties", "Directory to read (Bodymovin) jsons from.");
Florin Malita02616ea2020-06-25 13:33:17 -0400145static DEFINE_string(rives, "rives", "Directory to read Rive/Flare files from.");
Mike Kleinc6142d82019-03-25 10:54:59 -0500146static DEFINE_string(svgs, "", "Directory to read SVGs from, or a single SVG file.");
147
148static 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
152static DEFINE_string(key, "",
153 "Space-separated key/value pairs to add to JSON identifying this builder.");
154static DEFINE_string(properties, "",
155 "Space-separated key/value pairs to add to JSON identifying this run.");
156
Mike Klein055f5352020-01-30 11:11:12 -0600157static DEFINE_bool(rasterize_pdf, false, "Rasterize PDFs when possible.");
158
Tyler Denniston45f94f82020-02-04 16:09:08 -0500159static DEFINE_bool(runVerifiers, false,
160 "if true, run SkQP-style verification of GM-produced images.");
161
Brian Osman1c1208e2019-06-24 13:36:05 -0400162#if defined(__MSVC_RUNTIME_CHECKS)
163#include <rtcapi.h>
164int 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
mtklein748ca3b2015-01-15 10:56:12 -0800177using namespace DM;
bsalomon3724e572016-03-30 18:56:19 -0700178using sk_gpu_test::GrContextFactory;
bsalomon273c0f52016-03-31 10:59:06 -0700179using sk_gpu_test::GLTestContext;
bsalomonf2f1c172016-04-05 12:59:06 -0700180using sk_gpu_test::ContextInfo;
mtklein@google.comd36522d2013-10-16 13:02:15 +0000181
mtklein748ca3b2015-01-15 10:56:12 -0800182/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
183
Mike Kleine1271702019-03-07 11:46:59 -0600184static sk_sp<SkColorSpace> rec2020() {
Hal Canarybe67a172019-05-24 10:13:36 -0400185 return SkColorSpace::MakeRGB(SkNamedTransferFn::kRec2020, SkNamedGamut::kRec2020);
Mike Kleine1271702019-03-07 11:46:59 -0600186}
187
188/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
189
mtkleinc41fd922016-03-08 09:01:39 -0800190static FILE* gVLog;
191
192template <typename... Args>
193static void vlog(const char* fmt, Args&&... args) {
194 if (gVLog) {
mtkleinc41fd922016-03-08 09:01:39 -0800195 fprintf(gVLog, fmt, args...);
196 fflush(gVLog);
197 }
198}
199
200template <typename... Args>
201static void info(const char* fmt, Args&&... args) {
202 vlog(fmt, args...);
203 if (!FLAGS_quiet) {
204 printf(fmt, args...);
205 }
206}
207static void info(const char* fmt) {
208 if (!FLAGS_quiet) {
209 printf("%s", fmt); // Clang warns printf(fmt) is insecure.
210 }
211}
212
John Stiles33e044f2020-06-19 18:34:51 -0400213static SkTArray<SkString>* gFailures = new SkTArray<SkString>;
mtklein748ca3b2015-01-15 10:56:12 -0800214
mtklein3eed7dd2016-02-24 19:07:07 -0800215static void fail(const SkString& err) {
Herb Derby9c71e7b2019-06-17 14:40:42 -0400216 static SkSpinlock mutex;
217 SkAutoSpinlock lock(mutex);
mtklein748ca3b2015-01-15 10:56:12 -0800218 SkDebugf("\n\nFAILURE: %s\n\n", err.c_str());
John Stiles33e044f2020-06-19 18:34:51 -0400219 gFailures->push_back(err);
halcanary9e398f72015-01-10 11:18:04 -0800220}
221
mtklein0f7748a2016-07-25 15:27:29 -0700222struct Running {
223 SkString id;
224 SkThreadID thread;
225
226 void dump() const {
227 info("\t%s\n", id.c_str());
228 }
229};
mtkleinb37cb412015-03-18 05:27:14 -0700230
Mike Kleinc6142d82019-03-25 10:54:59 -0500231static void dump_json() {
232 if (!FLAGS_writePath.isEmpty()) {
233 JsonWriter::DumpJson(FLAGS_writePath[0], FLAGS_key, FLAGS_properties);
234 }
235}
236
mtklein246ba3a2016-02-23 10:39:36 -0800237// We use a spinlock to make locking this in a signal handler _somewhat_ safe.
John Stiles33e044f2020-06-19 18:34:51 -0400238static SkSpinlock* gMutex = new SkSpinlock;
239static int gPending;
240static SkTArray<Running>* gRunning = new SkTArray<Running>;
mtklein748ca3b2015-01-15 10:56:12 -0800241
mtklein3eed7dd2016-02-24 19:07:07 -0800242static 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);
mtkleinc41fd922016-03-08 09:01:39 -0800244 vlog("done %s\n", id.c_str());
mtkleinafae30a2016-02-24 12:28:32 -0800245 int pending;
mtkleinb37cb412015-03-18 05:27:14 -0700246 {
John Stiles33e044f2020-06-19 18:34:51 -0400247 SkAutoSpinlock lock(*gMutex);
248 for (int i = 0; i < gRunning->count(); i++) {
249 if (gRunning->at(i).id == id) {
250 gRunning->removeShuffle(i);
mtkleinb37cb412015-03-18 05:27:14 -0700251 break;
252 }
253 }
mtkleinafae30a2016-02-24 12:28:32 -0800254 pending = --gPending;
reed50bc0512015-05-19 14:13:31 -0700255 }
Mike Klein03a95ad2017-06-23 12:36:32 -0400256
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.
mtkleina17241b2015-01-23 05:48:00 -0800259 if (pending % 500 == 0) {
Mike Kleinc6142d82019-03-25 10:54:59 -0500260 dump_json();
Mike Klein03a95ad2017-06-23 12:36:32 -0400261
262 int curr = sk_tools::getCurrResidentSetSizeMB(),
263 peak = sk_tools::getMaxResidentSetSizeMB();
Mike Klein03a95ad2017-06-23 12:36:32 -0400264
John Stiles33e044f2020-06-19 18:34:51 -0400265 SkAutoSpinlock lock(*gMutex);
Mike Klein21ba7722018-10-02 13:57:44 +0000266 info("\n%dMB RAM, %dMB peak, %d queued, %d active:\n",
John Stiles33e044f2020-06-19 18:34:51 -0400267 curr, peak, gPending - gRunning->count(), gRunning->count());
268 for (auto& task : *gRunning) {
Mike Klein03a95ad2017-06-23 12:36:32 -0400269 task.dump();
270 }
mtkleina17241b2015-01-23 05:48:00 -0800271 }
mtklein@google.comd36522d2013-10-16 13:02:15 +0000272}
273
mtklein3eed7dd2016-02-24 19:07:07 -0800274static 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);
mtkleinc41fd922016-03-08 09:01:39 -0800276 vlog("start %s\n", id.c_str());
John Stiles33e044f2020-06-19 18:34:51 -0400277 SkAutoSpinlock lock(*gMutex);
278 gRunning->push_back({id,SkGetThreadID()});
mtkleinb37cb412015-03-18 05:27:14 -0700279}
280
Mike Klein3cc2d202016-10-24 11:39:43 -0400281static void find_culprit() {
282 // Assumes gMutex is locked.
283 SkThreadID thisThread = SkGetThreadID();
John Stiles33e044f2020-06-19 18:34:51 -0400284 for (auto& task : *gRunning) {
Mike Klein3cc2d202016-10-24 11:39:43 -0400285 if (task.thread == thisThread) {
286 info("Likely culprit:\n");
287 task.dump();
mtklein0f7748a2016-07-25 15:27:29 -0700288 }
289 }
Mike Klein3cc2d202016-10-24 11:39:43 -0400290}
mtklein0f7748a2016-07-25 15:27:29 -0700291
Mike Klein8f11d4d2018-01-24 12:42:55 -0500292#if defined(SK_BUILD_FOR_WIN)
mtklein5b64dab2016-06-09 08:59:48 -0700293 static LONG WINAPI crash_handler(EXCEPTION_POINTERS* e) {
mtkleinf8557292016-02-29 06:35:28 -0800294 static const struct {
295 const char* name;
robertphillips75467862016-03-01 14:10:23 -0800296 DWORD code;
mtkleinf8557292016-02-29 06:35:28 -0800297 } 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
John Stiles33e044f2020-06-19 18:34:51 -0400307 SkAutoSpinlock lock(*gMutex);
mtklein5b64dab2016-06-09 08:59:48 -0700308
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);
mtkleinf8557292016-02-29 06:35:28 -0800314 }
mtkleinf8557292016-02-29 06:35:28 -0800315 }
mtklein5b64dab2016-06-09 08:59:48 -0700316 info(", was running:\n");
John Stiles33e044f2020-06-19 18:34:51 -0400317 for (auto& task : *gRunning) {
mtklein0f7748a2016-07-25 15:27:29 -0700318 task.dump();
mtklein5b64dab2016-06-09 08:59:48 -0700319 }
mtklein0f7748a2016-07-25 15:27:29 -0700320 find_culprit();
mtklein5b64dab2016-06-09 08:59:48 -0700321 fflush(stdout);
322
mtkleinf8557292016-02-29 06:35:28 -0800323 // Execute default exception handler... hopefully, exit.
324 return EXCEPTION_EXECUTE_HANDLER;
mtklein246ba3a2016-02-23 10:39:36 -0800325 }
326
Mike Klein3cc2d202016-10-24 11:39:43 -0400327 static void setup_crash_handler() {
328 SetUnhandledExceptionFilter(crash_handler);
329 }
330#else
mtklein246ba3a2016-02-23 10:39:36 -0800331 #include <signal.h>
Mike Klein3cc2d202016-10-24 11:39:43 -0400332 #if !defined(SK_BUILD_FOR_ANDROID)
333 #include <execinfo.h>
Brian Salomondcbb9d92017-07-19 10:53:20 -0400334
335#endif
Mike Klein3cc2d202016-10-24 11:39:43 -0400336
337 static constexpr int max_of() { return 0; }
338 template <typename... Rest>
339 static constexpr int max_of(int x, Rest... rest) {
340 return x > max_of(rest...) ? x : max_of(rest...);
341 }
342
Mike Kleinc7278ad2018-02-15 09:26:54 -0500343 static void (*previous_handler[max_of(SIGABRT,SIGBUS,SIGFPE,SIGILL,SIGSEGV,SIGTERM)+1])(int);
mtkleinf8557292016-02-29 06:35:28 -0800344
mtklein5b64dab2016-06-09 08:59:48 -0700345 static void crash_handler(int sig) {
John Stiles33e044f2020-06-19 18:34:51 -0400346 SkAutoSpinlock lock(*gMutex);
mtklein5b64dab2016-06-09 08:59:48 -0700347
Mike Klein701a3582018-06-28 14:08:19 +0000348 info("\nCaught signal %d [%s] (%dMB RAM, peak %dMB), was running:\n",
349 sig, strsignal(sig),
350 sk_tools::getCurrResidentSetSizeMB(), sk_tools::getMaxResidentSetSizeMB());
351
John Stiles33e044f2020-06-19 18:34:51 -0400352 for (auto& task : *gRunning) {
mtklein0f7748a2016-07-25 15:27:29 -0700353 task.dump();
mtklein5b64dab2016-06-09 08:59:48 -0700354 }
mtklein0f7748a2016-07-25 15:27:29 -0700355 find_culprit();
mtklein5b64dab2016-06-09 08:59:48 -0700356
Mike Klein3cc2d202016-10-24 11:39:43 -0400357 #if !defined(SK_BUILD_FOR_ANDROID)
John Stilesede793a2020-10-15 13:27:12 -0400358 void* stack[128];
mtklein5b64dab2016-06-09 08:59:48 -0700359 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 }
Mike Klein3cc2d202016-10-24 11:39:43 -0400365 #endif
mtklein5b64dab2016-06-09 08:59:48 -0700366 fflush(stdout);
367
Mike Kleinca8087f2019-03-18 09:01:39 -0500368 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 }
mtklein5b64dab2016-06-09 08:59:48 -0700375 }
376
mtklein246ba3a2016-02-23 10:39:36 -0800377 static void setup_crash_handler() {
Mike Klein701a3582018-06-28 14:08:19 +0000378 const int kSignals[] = { SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGINT, SIGSEGV, SIGTERM };
mtklein246ba3a2016-02-23 10:39:36 -0800379 for (int sig : kSignals) {
Mike Klein3cc2d202016-10-24 11:39:43 -0400380 previous_handler[sig] = signal(sig, crash_handler);
mtklein246ba3a2016-02-23 10:39:36 -0800381 }
382 }
383#endif
384
mtklein748ca3b2015-01-15 10:56:12 -0800385/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
mtklein114c3cd2015-01-15 10:15:02 -0800386
Brian Osman57796b32019-01-25 18:02:59 +0000387struct Gold : public SkString {
388 Gold() : SkString("") {}
389 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 {
400 uint32_t operator()(const Gold& g) const {
401 return SkGoodHash()((const SkString&)g);
402 }
403 };
404};
John Stiles33e044f2020-06-19 18:34:51 -0400405static SkTHashSet<Gold, Gold::Hash>* gGold = new SkTHashSet<Gold, Gold::Hash>;
Brian Osman57796b32019-01-25 18:02:59 +0000406
407static void add_gold(JsonWriter::BitmapResult r) {
John Stiles33e044f2020-06-19 18:34:51 -0400408 gGold->add(Gold(r.config, r.sourceType, r.sourceOptions, r.name, r.md5));
Brian Osman57796b32019-01-25 18:02:59 +0000409}
410
411static 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
Mike Klein8f11d4d2018-01-24 12:42:55 -0500423#if defined(SK_BUILD_FOR_WIN)
John Stiles33e044f2020-06-19 18:34:51 -0400424 static constexpr char kNewline[] = "\r\n";
mtklein935f1b12016-03-16 08:37:19 -0700425#else
John Stiles33e044f2020-06-19 18:34:51 -0400426 static constexpr char kNewline[] = "\n";
mtklein935f1b12016-03-16 08:37:19 -0700427#endif
428
John Stiles33e044f2020-06-19 18:34:51 -0400429static SkTHashSet<SkString>* gUninterestingHashes = new SkTHashSet<SkString>;
borenet09ed4802015-04-03 14:15:33 -0700430
431static void gather_uninteresting_hashes() {
432 if (!FLAGS_uninterestingHashesFile.isEmpty()) {
bungeman38d909e2016-08-02 14:40:46 -0700433 sk_sp<SkData> data(SkData::MakeFromFileName(FLAGS_uninterestingHashesFile[0]));
mtkleincc334b32015-09-22 11:43:53 -0700434 if (!data) {
mtkleinc41fd922016-03-08 09:01:39 -0800435 info("WARNING: unable to read uninteresting hashes from %s\n",
436 FLAGS_uninterestingHashesFile[0]);
mtkleincc334b32015-09-22 11:43:53 -0700437 return;
438 }
Mike Klein45dcc0c2017-04-05 18:04:31 -0400439
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
borenet09ed4802015-04-03 14:15:33 -0700443 SkTArray<SkString> hashes;
Mike Klein45dcc0c2017-04-05 18:04:31 -0400444 SkStrSplit(contents.c_str(), kNewline, &hashes);
borenet09ed4802015-04-03 14:15:33 -0700445 for (const SkString& hash : hashes) {
John Stiles33e044f2020-06-19 18:34:51 -0400446 gUninterestingHashes->add(hash);
borenet09ed4802015-04-03 14:15:33 -0700447 }
mtkleinc41fd922016-03-08 09:01:39 -0800448 info("FYI: loaded %d distinct uninteresting hashes from %d lines\n",
John Stiles33e044f2020-06-19 18:34:51 -0400449 gUninterestingHashes->count(), hashes.count());
borenet09ed4802015-04-03 14:15:33 -0700450 }
451}
452
453/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
454
Ben Wagner145dbcd2016-11-03 14:40:50 -0400455struct TaggedSrc : public std::unique_ptr<Src> {
mtklein3eed7dd2016-02-24 19:07:07 -0800456 SkString tag;
457 SkString options;
mtkleine0effd62015-07-29 06:37:28 -0700458};
459
Ben Wagner145dbcd2016-11-03 14:40:50 -0400460struct TaggedSink : public std::unique_ptr<Sink> {
kkinnunen3e980c32015-12-23 01:33:00 -0800461 SkString tag;
djsollen54416de2015-04-03 07:24:48 -0700462};
mtklein748ca3b2015-01-15 10:56:12 -0800463
John Stiles33e044f2020-06-19 18:34:51 -0400464static constexpr bool kMemcpyOK = true;
mtklein748ca3b2015-01-15 10:56:12 -0800465
John Stiles33e044f2020-06-19 18:34:51 -0400466static SkTArray<TaggedSrc, kMemcpyOK>* gSrcs = new SkTArray<TaggedSrc, kMemcpyOK>;
467static SkTArray<TaggedSink, kMemcpyOK>* gSinks = new SkTArray<TaggedSink, kMemcpyOK>;
mtklein748ca3b2015-01-15 10:56:12 -0800468
mtklein6393c062015-04-27 08:45:01 -0700469static bool in_shard() {
470 static int N = 0;
471 return N++ % FLAGS_shards == FLAGS_shard;
472}
473
mtklein3eed7dd2016-02-24 19:07:07 -0800474static void push_src(const char* tag, ImplicitString options, Src* s) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400475 std::unique_ptr<Src> src(s);
Mike Klein88544fb2019-03-20 10:50:33 -0500476 if (in_shard() && FLAGS_src.contains(tag) &&
477 !CommandLineFlags::ShouldSkip(FLAGS_match, src->name().c_str())) {
John Stiles33e044f2020-06-19 18:34:51 -0400478 TaggedSrc& s = gSrcs->push_back();
mtklein18300a32016-03-16 13:53:35 -0700479 s.reset(src.release());
mtklein748ca3b2015-01-15 10:56:12 -0800480 s.tag = tag;
djsollen54416de2015-04-03 07:24:48 -0700481 s.options = options;
mtklein114c3cd2015-01-15 10:15:02 -0800482 }
mtklein748ca3b2015-01-15 10:56:12 -0800483}
mtklein114c3cd2015-01-15 10:15:02 -0800484
msarett9e707a02015-09-01 14:57:57 -0700485static void push_codec_src(Path path, CodecSrc::Mode mode, CodecSrc::DstColorType dstColorType,
scroggoc5560be2016-02-03 09:42:42 -0800486 SkAlphaType dstAlphaType, float scale) {
scroggoe4499842016-02-25 11:03:47 -0800487 if (FLAGS_simpleCodec) {
scroggo19b91532016-10-24 09:03:26 -0700488 const bool simple = CodecSrc::kCodec_Mode == mode || CodecSrc::kAnimated_Mode == mode;
489 if (!simple || dstColorType != CodecSrc::kGetFromCanvas_DstColorType || scale != 1.0f) {
scroggoe4499842016-02-25 11:03:47 -0800490 // Only decode in the simple case.
491 return;
scroggo19b91532016-10-24 09:03:26 -0700492 }
scroggoe4499842016-02-25 11:03:47 -0800493 }
msarett9e707a02015-09-01 14:57:57 -0700494 SkString folder;
495 switch (mode) {
496 case CodecSrc::kCodec_Mode:
497 folder.append("codec");
498 break;
msarettbb25b532016-01-13 09:31:39 -0800499 case CodecSrc::kCodecZeroInit_Mode:
500 folder.append("codec_zero_init");
501 break;
msarett9e707a02015-09-01 14:57:57 -0700502 case CodecSrc::kScanline_Mode:
503 folder.append("scanline");
504 break;
msarett9e707a02015-09-01 14:57:57 -0700505 case CodecSrc::kStripe_Mode:
506 folder.append("stripe");
507 break;
msarett91c22b22016-02-22 12:27:46 -0800508 case CodecSrc::kCroppedScanline_Mode:
509 folder.append("crop");
510 break;
msarett9e707a02015-09-01 14:57:57 -0700511 case CodecSrc::kSubset_Mode:
msarett36c37962015-09-02 13:20:52 -0700512 folder.append("codec_subset");
msarett9e707a02015-09-01 14:57:57 -0700513 break;
scroggo19b91532016-10-24 09:03:26 -0700514 case CodecSrc::kAnimated_Mode:
515 folder.append("codec_animated");
516 break;
msarett9e707a02015-09-01 14:57:57 -0700517 }
518
519 switch (dstColorType) {
520 case CodecSrc::kGrayscale_Always_DstColorType:
521 folder.append("_kGray8");
522 break;
msarett34e0ec42016-04-22 16:27:24 -0700523 case CodecSrc::kNonNative8888_Always_DstColorType:
524 folder.append("_kNonNative");
525 break;
msarett9e707a02015-09-01 14:57:57 -0700526 default:
527 break;
528 }
529
scroggoc5560be2016-02-03 09:42:42 -0800530 switch (dstAlphaType) {
scroggoc5560be2016-02-03 09:42:42 -0800531 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
msarett9e707a02015-09-01 14:57:57 -0700541 if (1.0f != scale) {
542 folder.appendf("_%.3f", scale);
543 }
544
scroggoc5560be2016-02-03 09:42:42 -0800545 CodecSrc* src = new CodecSrc(path, mode, dstColorType, dstAlphaType, scale);
msarett9e707a02015-09-01 14:57:57 -0700546 push_src("image", folder, src);
547}
548
scroggof8dc9df2016-05-16 09:04:13 -0700549static void push_android_codec_src(Path path, CodecSrc::DstColorType dstColorType,
550 SkAlphaType dstAlphaType, int sampleSize) {
msarett3d9d7a72015-10-21 10:27:10 -0700551 SkString folder;
scroggof8dc9df2016-05-16 09:04:13 -0700552 folder.append("scaled_codec");
msarett3d9d7a72015-10-21 10:27:10 -0700553
554 switch (dstColorType) {
555 case CodecSrc::kGrayscale_Always_DstColorType:
556 folder.append("_kGray8");
557 break;
msarett34e0ec42016-04-22 16:27:24 -0700558 case CodecSrc::kNonNative8888_Always_DstColorType:
559 folder.append("_kNonNative");
560 break;
msarett3d9d7a72015-10-21 10:27:10 -0700561 default:
562 break;
563 }
564
scroggoc5560be2016-02-03 09:42:42 -0800565 switch (dstAlphaType) {
scroggoc5560be2016-02-03 09:42:42 -0800566 case kPremul_SkAlphaType:
567 folder.append("_premul");
568 break;
msarett9e9444c2016-02-03 12:39:10 -0800569 case kUnpremul_SkAlphaType:
570 folder.append("_unpremul");
571 break;
scroggoc5560be2016-02-03 09:42:42 -0800572 default:
573 break;
574 }
575
msarett3d9d7a72015-10-21 10:27:10 -0700576 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800577 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700578 }
579
scroggof8dc9df2016-05-16 09:04:13 -0700580 AndroidCodecSrc* src = new AndroidCodecSrc(path, dstColorType, dstAlphaType, sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700581 push_src("image", folder, src);
582}
583
msarett18976312016-03-09 14:20:58 -0800584static 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
Leon Scroggins III87caae62020-05-04 10:02:45 -0400618#ifdef SK_ENABLE_ANDROID_UTILS
Leon Scroggins III07418182017-08-15 12:24:02 -0400619static 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
652static 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}
Leon Scroggins III87caae62020-05-04 10:02:45 -0400677#endif // SK_ENABLE_ANDROID_UTILS
Leon Scroggins III07418182017-08-15 12:24:02 -0400678
msarett438b2ad2015-04-09 12:43:10 -0700679static void push_codec_srcs(Path path) {
bungeman38d909e2016-08-02 14:40:46 -0700680 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
msarett438b2ad2015-04-09 12:43:10 -0700681 if (!encoded) {
mtkleinc41fd922016-03-08 09:01:39 -0800682 info("Couldn't read %s.", path.c_str());
msarett438b2ad2015-04-09 12:43:10 -0700683 return;
684 }
Mike Reedede7bac2017-07-23 15:30:02 -0400685 std::unique_ptr<SkCodec> codec = SkCodec::MakeFromData(encoded);
John Stilesa008b0f2020-08-16 08:48:02 -0400686 if (nullptr == codec) {
mtkleinc41fd922016-03-08 09:01:39 -0800687 info("Couldn't create codec for %s.", path.c_str());
msarett438b2ad2015-04-09 12:43:10 -0700688 return;
689 }
690
scroggo9366aff2016-05-20 08:38:54 -0700691 // native scaling is only supported by WEBP and JPEG
692 bool supportsNativeScaling = false;
msarett438b2ad2015-04-09 12:43:10 -0700693
msarett91c22b22016-02-22 12:27:46 -0800694 SkTArray<CodecSrc::Mode> nativeModes;
695 nativeModes.push_back(CodecSrc::kCodec_Mode);
696 nativeModes.push_back(CodecSrc::kCodecZeroInit_Mode);
msarett91c22b22016-02-22 12:27:46 -0800697 switch (codec->getEncodedFormat()) {
Hal Canarydb683012016-11-23 08:55:18 -0700698 case SkEncodedImageFormat::kJPEG:
msarett91c22b22016-02-22 12:27:46 -0800699 nativeModes.push_back(CodecSrc::kScanline_Mode);
700 nativeModes.push_back(CodecSrc::kStripe_Mode);
701 nativeModes.push_back(CodecSrc::kCroppedScanline_Mode);
scroggo9366aff2016-05-20 08:38:54 -0700702 supportsNativeScaling = true;
msarett91c22b22016-02-22 12:27:46 -0800703 break;
Hal Canarydb683012016-11-23 08:55:18 -0700704 case SkEncodedImageFormat::kWEBP:
msarett91c22b22016-02-22 12:27:46 -0800705 nativeModes.push_back(CodecSrc::kSubset_Mode);
scroggo9366aff2016-05-20 08:38:54 -0700706 supportsNativeScaling = true;
msarett91c22b22016-02-22 12:27:46 -0800707 break;
Hal Canarydb683012016-11-23 08:55:18 -0700708 case SkEncodedImageFormat::kDNG:
msarettb65e6042016-02-23 05:37:25 -0800709 break;
msarett91c22b22016-02-22 12:27:46 -0800710 default:
711 nativeModes.push_back(CodecSrc::kScanline_Mode);
msarett91c22b22016-02-22 12:27:46 -0800712 break;
713 }
msarett9e707a02015-09-01 14:57:57 -0700714
msarett91c22b22016-02-22 12:27:46 -0800715 SkTArray<CodecSrc::DstColorType> colorTypes;
716 colorTypes.push_back(CodecSrc::kGetFromCanvas_DstColorType);
msarett34e0ec42016-04-22 16:27:24 -0700717 colorTypes.push_back(CodecSrc::kNonNative8888_Always_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700718 switch (codec->getInfo().colorType()) {
719 case kGray_8_SkColorType:
msarett91c22b22016-02-22 12:27:46 -0800720 colorTypes.push_back(CodecSrc::kGrayscale_Always_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700721 break;
722 default:
msarett36c37962015-09-02 13:20:52 -0700723 break;
724 }
msarett9e707a02015-09-01 14:57:57 -0700725
scroggoc5560be2016-02-03 09:42:42 -0800726 SkTArray<SkAlphaType> alphaModes;
727 alphaModes.push_back(kPremul_SkAlphaType);
scroggof2c96a22016-05-20 11:08:27 -0700728 if (codec->getInfo().alphaType() != kOpaque_SkAlphaType) {
729 alphaModes.push_back(kUnpremul_SkAlphaType);
scroggoc5560be2016-02-03 09:42:42 -0800730 }
msarettb714fb02016-01-22 14:46:42 -0800731
732 for (CodecSrc::Mode mode : nativeModes) {
scroggo9366aff2016-05-20 08:38:54 -0700733 for (CodecSrc::DstColorType colorType : colorTypes) {
734 for (SkAlphaType alphaType : alphaModes) {
scroggof2c96a22016-05-20 11:08:27 -0700735 // Only test kCroppedScanline_Mode when the alpha type is premul. The test is
scroggo9366aff2016-05-20 08:38:54 -0700736 // slow and won't be interestingly different with different alpha types.
737 if (CodecSrc::kCroppedScanline_Mode == mode &&
scroggof2c96a22016-05-20 11:08:27 -0700738 kPremul_SkAlphaType != alphaType) {
scroggo9366aff2016-05-20 08:38:54 -0700739 continue;
740 }
msarett91c22b22016-02-22 12:27:46 -0800741
scroggo9366aff2016-05-20 08:38:54 -0700742 push_codec_src(path, mode, colorType, alphaType, 1.0f);
msarettf3dc1882016-04-26 13:06:38 -0700743
scroggo9366aff2016-05-20 08:38:54 -0700744 // 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 }
scroggoc5560be2016-02-03 09:42:42 -0800753 }
msarett9e707a02015-09-01 14:57:57 -0700754 }
755 }
msarett0a242972015-06-11 14:27:27 -0700756 }
msarett36c37962015-09-02 13:20:52 -0700757
scroggo19b91532016-10-24 09:03:26 -0700758 {
759 std::vector<SkCodec::FrameInfo> frameInfos = codec->getFrameInfo();
760 if (frameInfos.size() > 1) {
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400761 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 }
Leon Scroggins1340dbd2020-11-09 14:18:12 -0500767 for (float scale : { .5f, .33f }) {
768 push_codec_src(path, CodecSrc::kAnimated_Mode, CodecSrc::kGetFromCanvas_DstColorType,
769 kPremul_SkAlphaType, scale);
770 }
scroggo19b91532016-10-24 09:03:26 -0700771 }
772
773 }
774
scroggoe4499842016-02-25 11:03:47 -0800775 if (FLAGS_simpleCodec) {
776 return;
777 }
778
msarett3d9d7a72015-10-21 10:27:10 -0700779 const int sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
msarett36c37962015-09-02 13:20:52 -0700780
msarett3d9d7a72015-10-21 10:27:10 -0700781 for (int sampleSize : sampleSizes) {
msarett91c22b22016-02-22 12:27:46 -0800782 for (CodecSrc::DstColorType colorType : colorTypes) {
scroggoc5560be2016-02-03 09:42:42 -0800783 for (SkAlphaType alphaType : alphaModes) {
msarettf3dc1882016-04-26 13:06:38 -0700784 // 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
scroggof8dc9df2016-05-16 09:04:13 -0700790 push_android_codec_src(path, colorType, alphaType, sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700791 }
msarett36c37962015-09-02 13:20:52 -0700792 }
793 }
msarett18976312016-03-09 14:20:58 -0800794
Leon Scroggins III07418182017-08-15 12:24:02 -0400795 const char* ext = strrchr(path.c_str(), '.');
796 if (ext) {
797 ext++;
msarett18976312016-03-09 14:20:58 -0800798
Leon Scroggins III07418182017-08-15 12:24:02 -0400799 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
Leon Scroggins III87caae62020-05-04 10:02:45 -0400810#ifdef SK_ENABLE_ANDROID_UTILS
Leon Scroggins III07418182017-08-15 12:24:02 -0400811 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)) {
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -0500817 bool gray = codec->getInfo().colorType() == kGray_8_SkColorType;
Leon Scroggins III07418182017-08-15 12:24:02 -0400818 push_brd_srcs(path, gray);
819 break;
820 }
msarett18976312016-03-09 14:20:58 -0800821 }
Leon Scroggins III87caae62020-05-04 10:02:45 -0400822#endif
msarett18976312016-03-09 14:20:58 -0800823 }
824
825 // Push image generator GPU test.
brianosmanc9ced392016-03-24 07:27:43 -0700826 push_image_gen_src(path, ImageGenSrc::kCodec_Mode, codec->getInfo().alphaType(), true);
msarett18976312016-03-09 14:20:58 -0800827
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)
Hal Canarydb683012016-11-23 08:55:18 -0700833 if (SkEncodedImageFormat::kWEBP != codec->getEncodedFormat() &&
834 SkEncodedImageFormat::kWBMP != codec->getEncodedFormat() &&
msarett18976312016-03-09 14:20:58 -0800835 kUnpremul_SkAlphaType != alphaType)
836 {
837 push_image_gen_src(path, ImageGenSrc::kPlatform_Mode, alphaType, false);
838 }
msarettfc0b6d12016-03-17 13:50:17 -0700839#elif defined(SK_BUILD_FOR_WIN)
Hal Canarydb683012016-11-23 08:55:18 -0700840 if (SkEncodedImageFormat::kWEBP != codec->getEncodedFormat() &&
841 SkEncodedImageFormat::kWBMP != codec->getEncodedFormat())
msarettfc0b6d12016-03-17 13:50:17 -0700842 {
843 push_image_gen_src(path, ImageGenSrc::kPlatform_Mode, alphaType, false);
844 }
Leon Scroggins III326b9892020-08-05 16:51:10 -0400845#elif defined(SK_ENABLE_NDK_IMAGES)
Leon Scroggins IIIf21d6b92020-08-05 10:44:17 -0400846 push_image_gen_src(path, ImageGenSrc::kPlatform_Mode, alphaType, false);
msarett18976312016-03-09 14:20:58 -0800847#endif
848 }
msarett438b2ad2015-04-09 12:43:10 -0700849}
850
fmalitaa2b9fdf2016-08-03 19:53:36 -0700851template <typename T>
Mike Klein88544fb2019-03-20 10:50:33 -0500852void gather_file_srcs(const CommandLineFlags::StringArray& flags,
853 const char* ext,
854 const char* src_name = nullptr) {
Florin Malitaf1ba3ae2018-07-19 15:09:11 -0400855 if (!src_name) {
856 // With the exception of Lottie files, the source name is the extension.
857 src_name = ext;
858 }
859
fmalitaa2b9fdf2016-08-03 19:53:36 -0700860 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); ) {
Florin Malitaf1ba3ae2018-07-19 15:09:11 -0400865 push_src(src_name, "", new T(SkOSPath::Join(path, file.c_str())));
fmalitaa2b9fdf2016-08-03 19:53:36 -0700866 }
867 } else {
Florin Malitaf1ba3ae2018-07-19 15:09:11 -0400868 push_src(src_name, "", new T(path));
fmalitaa2b9fdf2016-08-03 19:53:36 -0700869 }
870 }
871}
872
scroggo86737142016-02-03 12:19:11 -0800873static bool gather_srcs() {
Hal Canary972eba32018-07-30 17:07:07 -0400874 for (skiagm::GMFactory f : skiagm::GMRegistry::Range()) {
875 push_src("gm", "", new GMSrc(f));
mtklein748ca3b2015-01-15 10:56:12 -0800876 }
scroggo86737142016-02-03 12:19:11 -0800877
Robert Phillipse47f0a02018-03-27 17:01:16 -0400878 gather_file_srcs<SKPSrc>(FLAGS_skps, "skp");
fmalitaa2b9fdf2016-08-03 19:53:36 -0700879 gather_file_srcs<MSKPSrc>(FLAGS_mskps, "mskp");
Florin Malita87ccf332018-05-04 12:23:24 -0400880#if defined(SK_ENABLE_SKOTTIE)
Florin Malitaf1ba3ae2018-07-19 15:09:11 -0400881 gather_file_srcs<SkottieSrc>(FLAGS_lotties, "json", "lottie");
Florin Malita124d5af2017-12-31 17:02:26 -0500882#endif
Florin Malita02616ea2020-06-25 13:33:17 -0400883#if defined(SK_ENABLE_SKRIVE)
884 gather_file_srcs<SkRiveSrc>(FLAGS_rives, "flr", "rive");
885#endif
fmalitaa2b9fdf2016-08-03 19:53:36 -0700886#if defined(SK_XML)
887 gather_file_srcs<SVGSrc>(FLAGS_svgs, "svg");
888#endif
Chris Dalton184c37e2018-09-28 11:27:39 -0600889 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 }
halcanary45420a92016-06-02 12:41:14 -0700894
scroggo86737142016-02-03 12:19:11 -0800895 SkTArray<SkString> images;
msarett69deca82016-04-29 09:38:40 -0700896 if (!CollectImages(FLAGS_images, &images)) {
scroggo86737142016-02-03 12:19:11 -0800897 return false;
898 }
899
John Stilesbd3ffa42020-07-30 20:24:57 -0400900 for (const SkString& image : images) {
scroggo86737142016-02-03 12:19:11 -0800901 push_codec_srcs(image);
mtklein709d2c32015-01-15 08:30:25 -0800902 }
scroggo86737142016-02-03 12:19:11 -0800903
msarett69deca82016-04-29 09:38:40 -0700904 SkTArray<SkString> colorImages;
905 if (!CollectImages(FLAGS_colorImages, &colorImages)) {
906 return false;
907 }
908
John Stilesbd3ffa42020-07-30 20:24:57 -0400909 for (const SkString& colorImage : colorImages) {
Mike Klein0d5d1422019-03-06 10:56:04 -0600910 push_src("colorImage", "decode_native", new ColorCodecSrc(colorImage, false));
911 push_src("colorImage", "decode_to_dst", new ColorCodecSrc(colorImage, true));
msarett69deca82016-04-29 09:38:40 -0700912 }
913
scroggo86737142016-02-03 12:19:11 -0800914 return true;
mtklein709d2c32015-01-15 08:30:25 -0800915}
916
kkinnunen3e980c32015-12-23 01:33:00 -0800917static void push_sink(const SkCommandLineConfig& config, Sink* s) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400918 std::unique_ptr<Sink> sink(s);
kkinnunen3e980c32015-12-23 01:33:00 -0800919
mtkleine0effd62015-07-29 06:37:28 -0700920 // Try a simple Src as a canary. If it fails, skip this sink.
mtklein748ca3b2015-01-15 10:56:12 -0800921 struct : public Src {
Robert Phillipsb87b39b2020-07-01 14:45:24 -0400922 Result draw(GrDirectContext*, SkCanvas* c) const override {
mtkleine0effd62015-07-29 06:37:28 -0700923 c->drawRect(SkRect::MakeWH(1,1), SkPaint());
Ben Wagnerea25fcf2020-02-12 11:18:46 -0500924 return Result::Ok();
mtkleine0effd62015-07-29 06:37:28 -0700925 }
mtklein36352bf2015-03-25 18:17:31 -0700926 SkISize size() const override { return SkISize::Make(16, 16); }
mtkleine0effd62015-07-29 06:37:28 -0700927 Name name() const override { return "justOneRect"; }
928 } justOneRect;
mtklein114c3cd2015-01-15 10:15:02 -0800929
mtklein748ca3b2015-01-15 10:56:12 -0800930 SkBitmap bitmap;
931 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800932 SkString log;
Ben Wagnerea25fcf2020-02-12 11:18:46 -0500933 Result result = sink->draw(justOneRect, &bitmap, &stream, &log);
934 if (result.isFatal()) {
Robert Phillips45f37c32020-02-13 20:26:58 +0000935 info("Could not run %s: %s\n", config.getTag().c_str(), result.c_str());
mtklein05641a52015-04-21 10:49:13 -0700936 exit(1);
mtklein114c3cd2015-01-15 10:15:02 -0800937 }
938
John Stiles33e044f2020-06-19 18:34:51 -0400939 TaggedSink& ts = gSinks->push_back();
mtklein18300a32016-03-16 13:53:35 -0700940 ts.reset(sink.release());
kkinnunen3e980c32015-12-23 01:33:00 -0800941 ts.tag = config.getTag();
mtklein748ca3b2015-01-15 10:56:12 -0800942}
943
Mike Kleinc7a09162019-02-19 06:14:39 -0500944static sk_sp<SkColorSpace> rgb_to_gbr() {
945 return SkColorSpace::MakeSRGB()->makeColorSpin();
946}
947
csmartdalton008b9d82017-02-22 12:00:42 -0700948static Sink* create_sink(const GrContextOptions& grCtxOptions, const SkCommandLineConfig* config) {
Brian Osmanc7ad40f2018-05-31 14:27:17 -0400949 if (FLAGS_gpu) {
kkinnunen3e980c32015-12-23 01:33:00 -0800950 if (const SkCommandLineConfigGpu* gpuConfig = config->asConfigGpu()) {
csmartdalton008b9d82017-02-22 12:00:42 -0700951 GrContextFactory testFactory(grCtxOptions);
Brian Osman3fdfe282019-09-09 13:46:52 -0400952 if (!testFactory.get(gpuConfig->getContextType(), gpuConfig->getContextOverrides())) {
mtkleinc41fd922016-03-08 09:01:39 -0800953 info("WARNING: can not create GPU context for config '%s'. "
954 "GM tests will be skipped.\n", gpuConfig->getTag().c_str());
kkinnunen3e980c32015-12-23 01:33:00 -0800955 return nullptr;
956 }
Brian Osmanf9810662017-08-30 10:02:10 -0400957 if (gpuConfig->getTestThreading()) {
Brian Salomon00a5eb82018-07-11 15:32:05 -0400958 SkASSERT(!gpuConfig->getTestPersistentCache());
Brian Osman3fdfe282019-09-09 13:46:52 -0400959 return new GPUThreadTestingSink(gpuConfig, grCtxOptions);
Brian Salomon00a5eb82018-07-11 15:32:05 -0400960 } else if (gpuConfig->getTestPersistentCache()) {
Brian Osman3fdfe282019-09-09 13:46:52 -0400961 return new GPUPersistentCacheTestingSink(gpuConfig, grCtxOptions);
Brian Osmaned58e002019-09-06 14:42:43 -0400962 } else if (gpuConfig->getTestPrecompile()) {
Brian Osman3fdfe282019-09-09 13:46:52 -0400963 return new GPUPrecompileTestingSink(gpuConfig, grCtxOptions);
Robert Phillips291f3402020-02-19 14:14:47 -0500964 } else if (gpuConfig->getUseDDLSink()) {
965 return new GPUDDLSink(gpuConfig, grCtxOptions);
Robert Phillips762cb4e2020-06-15 13:12:32 -0400966 } else if (gpuConfig->getOOPRish()) {
967 return new GPUOOPRSink(gpuConfig, grCtxOptions);
Brian Osmanf9810662017-08-30 10:02:10 -0400968 } else {
Brian Osman3fdfe282019-09-09 13:46:52 -0400969 return new GPUSink(gpuConfig, grCtxOptions);
Brian Osmanf9810662017-08-30 10:02:10 -0400970 }
kkinnunen3e980c32015-12-23 01:33:00 -0800971 }
972 }
Bryce Thomas95a7b762018-03-02 13:54:21 -0800973 if (const SkCommandLineConfigSvg* svgConfig = config->asConfigSvg()) {
974 int pageIndex = svgConfig->getPageIndex();
975 return new SVGSink(pageIndex);
976 }
kkinnunen3e980c32015-12-23 01:33:00 -0800977
Brian Salomon23356442018-11-30 15:33:19 -0500978#define SINK(t, sink, ...) if (config->getBackend().equals(t)) return new sink(__VA_ARGS__)
kkinnunen3e980c32015-12-23 01:33:00 -0800979
mtklein748ca3b2015-01-15 10:56:12 -0800980 if (FLAGS_cpu) {
Mike Kleinf7eb0542020-02-11 12:19:08 -0600981 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);
Mike Klein0e4041f2018-06-19 16:00:40 -04001000
1001 // Configs relevant to color management testing (and 8888 for reference).
1002
1003 // 'narrow' has a gamut narrower than sRGB, and different transfer function.
Brian Osman82ebe042019-01-04 17:03:00 -05001004 auto narrow = SkColorSpace::MakeRGB(SkNamedTransferFn::k2Dot2, gNarrow_toXYZD50),
Mike Klein0e4041f2018-06-19 16:00:40 -04001005 srgb = SkColorSpace::MakeSRGB(),
Mike Klein4de05072018-08-16 09:09:14 -04001006 srgbLinear = SkColorSpace::MakeSRGBLinear(),
Mike Kleinb147ace2020-01-16 11:11:06 -06001007 p3 = SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB, SkNamedGamut::kDisplayP3);
Mike Klein0e4041f2018-06-19 16:00:40 -04001008
Mike Kleinff6a73c2018-08-17 16:19:24 -04001009 SINK( "f16", RasterSink, kRGBA_F16_SkColorType, srgbLinear);
1010 SINK( "srgb", RasterSink, kRGBA_8888_SkColorType, srgb );
1011 SINK( "esrgb", RasterSink, kRGBA_F16_SkColorType, srgb );
Mike Kleinc7a09162019-02-19 06:14:39 -05001012 SINK( "esgbr", RasterSink, kRGBA_F16_SkColorType, rgb_to_gbr());
Mike Kleinff6a73c2018-08-17 16:19:24 -04001013 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 );
Mike Kleinae4b1f42018-09-13 11:13:15 -04001017 SINK( "rec2020", RasterSink, kRGBA_8888_SkColorType, rec2020() );
1018 SINK("erec2020", RasterSink, kRGBA_F16_SkColorType, rec2020() );
Mike Klein37854712018-06-26 11:43:06 -04001019
Mike Kleinb70990e2019-02-28 10:03:27 -06001020 SINK("f16norm", RasterSink, kRGBA_F16Norm_SkColorType, srgb);
1021
Mike Klein37854712018-06-26 11:43:06 -04001022 SINK( "f32", RasterSink, kRGBA_F32_SkColorType, srgbLinear);
mtklein748ca3b2015-01-15 10:56:12 -08001023 }
1024#undef SINK
halcanary96fcdcc2015-08-27 07:41:13 -07001025 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -08001026}
1027
kkinnunen3e980c32015-12-23 01:33:00 -08001028static Sink* create_via(const SkString& tag, Sink* wrapped) {
Brian Salomon23356442018-11-30 15:33:19 -05001029#define VIA(t, via, ...) if (tag.equals(t)) return new via(__VA_ARGS__)
Mike Reedbae888e2017-02-18 16:50:45 -05001030#ifdef TEST_VIA_SVG
Mike Reedf67c4592017-02-17 17:06:11 -05001031 VIA("svg", ViaSVG, wrapped);
Mike Reedbae888e2017-02-18 16:50:45 -05001032#endif
halcanary96fcdcc2015-08-27 07:41:13 -07001033 VIA("serialize", ViaSerialization, wrapped);
mtklein4a34ecb2016-01-08 10:19:35 -08001034 VIA("pic", ViaPicture, wrapped);
Robert Phillips33f02ed2018-03-27 08:06:57 -04001035
mtkleind603b222015-02-17 11:13:33 -08001036 if (FLAGS_matrix.count() == 4) {
mtklein748ca3b2015-01-15 10:56:12 -08001037 SkMatrix m;
mtkleind603b222015-02-17 11:13:33 -08001038 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);
mtklein748ca3b2015-01-15 10:56:12 -08001045 }
tomhudson64de1e12015-03-05 08:01:07 -08001046
mtklein748ca3b2015-01-15 10:56:12 -08001047#undef VIA
halcanary96fcdcc2015-08-27 07:41:13 -07001048 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -08001049}
1050
Ben Wagnerf28d4722017-08-11 17:33:22 -04001051static bool gather_sinks(const GrContextOptions& grCtxOptions, bool defaultConfigs) {
kkinnunen3e980c32015-12-23 01:33:00 -08001052 SkCommandLineConfigArray configs;
1053 ParseConfigs(FLAGS_config, &configs);
Jim Van Verth8a9a3712019-05-31 10:49:12 -04001054 AutoreleasePool pool;
kkinnunen3e980c32015-12-23 01:33:00 -08001055 for (int i = 0; i < configs.count(); i++) {
1056 const SkCommandLineConfig& config = *configs[i];
csmartdalton008b9d82017-02-22 12:00:42 -07001057 Sink* sink = create_sink(grCtxOptions, &config);
kkinnunen3e980c32015-12-23 01:33:00 -08001058 if (sink == nullptr) {
mtkleinc41fd922016-03-08 09:01:39 -08001059 info("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
1060 config.getTag().c_str());
kkinnunen3e980c32015-12-23 01:33:00 -08001061 continue;
1062 }
mtklein748ca3b2015-01-15 10:56:12 -08001063
kkinnunen3e980c32015-12-23 01:33:00 -08001064 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);
halcanary96fcdcc2015-08-27 07:41:13 -07001068 if (next == nullptr) {
mtkleinc41fd922016-03-08 09:01:39 -08001069 info("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
1070 part.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001071 delete sink;
halcanary96fcdcc2015-08-27 07:41:13 -07001072 sink = nullptr;
mtklein748ca3b2015-01-15 10:56:12 -08001073 break;
1074 }
1075 sink = next;
1076 }
1077 if (sink) {
1078 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -08001079 }
1080 }
brianosman05c987c2016-05-09 13:42:16 -07001081
1082 // If no configs were requested (just running tests, perhaps?), then we're okay.
Ben Wagnerf28d4722017-08-11 17:33:22 -04001083 if (configs.count() == 0 ||
1084 // If we're using the default configs, we're okay.
1085 defaultConfigs ||
Ben Wagner32fa5102017-08-10 21:25:55 -04001086 // Otherwise, make sure that all specified configs have become sinks.
John Stiles33e044f2020-06-19 18:34:51 -04001087 configs.count() == gSinks->count()) {
Ben Wagner32fa5102017-08-10 21:25:55 -04001088 return true;
1089 }
Ben Wagner32fa5102017-08-10 21:25:55 -04001090 return false;
mtklein114c3cd2015-01-15 10:15:02 -08001091}
mtklein709d2c32015-01-15 08:30:25 -08001092
mtkleina2ef6422015-01-15 13:44:22 -08001093static bool match(const char* needle, const char* haystack) {
Chris Daltonecf78ac2017-08-15 15:43:08 -06001094 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);
mtkleina2ef6422015-01-15 13:44:22 -08001101}
1102
Mike Kleina54d3802020-07-24 12:37:32 -05001103static 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)) {
mtklein3eed7dd2016-02-24 19:07:07 -08001110 return true;
mtkleina2ef6422015-01-15 13:44:22 -08001111 }
1112 }
mtklein3eed7dd2016-02-24 19:07:07 -08001113 return false;
mtkleina2ef6422015-01-15 13:44:22 -08001114}
1115
mtkleincd50bca2016-01-05 06:20:20 -08001116// 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.
John Stiles33e044f2020-06-19 18:34:51 -04001118static SkTaskGroup* gDefinitelyThreadSafeWork = new SkTaskGroup;
mtkleincd50bca2016-01-05 06:20:20 -08001119
mtklein748ca3b2015-01-15 10:56:12 -08001120// 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.
1122struct Task {
mtkleine0effd62015-07-29 06:37:28 -07001123 Task(const TaggedSrc& src, const TaggedSink& sink) : src(src), sink(sink) {}
1124 const TaggedSrc& src;
1125 const TaggedSink& sink;
mtklein748ca3b2015-01-15 10:56:12 -08001126
mtklein21eaf3b2016-02-08 12:39:59 -08001127 static void Run(const Task& task) {
Jim Van Verth8a9a3712019-05-31 10:49:12 -04001128 AutoreleasePool pool;
mtklein21eaf3b2016-02-08 12:39:59 -08001129 SkString name = task.src->name();
mtkleine0effd62015-07-29 06:37:28 -07001130
mtkleinb9eb4ac2015-02-02 18:26:03 -08001131 SkString log;
mtklein3eed7dd2016-02-24 19:07:07 -08001132 if (!FLAGS_dryRun) {
mtklein748ca3b2015-01-15 10:56:12 -08001133 SkBitmap bitmap;
1134 SkDynamicMemoryWStream stream;
mtklein3eed7dd2016-02-24 19:07:07 -08001135 start(task.sink.tag.c_str(), task.src.tag.c_str(),
1136 task.src.options.c_str(), name.c_str());
Ben Wagnerea25fcf2020-02-12 11:18:46 -05001137 Result result = task.sink->draw(*task.src, &bitmap, &stream, &log);
mtkleinb3b13b72016-03-07 13:20:52 -08001138 if (!log.isEmpty()) {
mtkleinc41fd922016-03-08 09:01:39 -08001139 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());
mtkleinb3b13b72016-03-07 13:20:52 -08001144 }
Ben Wagnerea25fcf2020-02-12 11:18:46 -05001145 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()));
mtklein748ca3b2015-01-15 10:56:12 -08001157 }
mtklein62bd1a62015-01-27 14:46:26 -08001158
Tyler Denniston45f94f82020-02-04 16:09:08 -05001159 // Run verifiers if specified
1160 if (FLAGS_runVerifiers) {
1161 RunGMVerifiers(task, bitmap);
1162 }
1163
mtkleincd50bca2016-01-05 06:20:20 -08001164 // We're likely switching threads here, so we must capture by value, [=] or [foo,bar].
Hal Canary0b833192017-03-14 14:35:02 -04001165 SkStreamAsset* data = stream.detachAsStream().release();
John Stiles33e044f2020-06-19 18:34:51 -04001166 gDefinitelyThreadSafeWork->add([task,name,bitmap,data]{
Ben Wagner145dbcd2016-11-03 14:40:50 -04001167 std::unique_ptr<SkStreamAsset> ownedData(data);
mtkleincd50bca2016-01-05 06:20:20 -08001168
Mike Kleine1271702019-03-07 11:46:59 -06001169 std::unique_ptr<HashAndEncode> hashAndEncode;
1170
mtkleincd50bca2016-01-05 06:20:20 -08001171 SkString md5;
Brian Osman57796b32019-01-25 18:02:59 +00001172 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
mtkleincd50bca2016-01-05 06:20:20 -08001173 SkMD5 hash;
1174 if (data->getLength()) {
1175 hash.writeStream(data, data->getLength());
1176 data->rewind();
mtklein38408462015-07-08 07:25:27 -07001177 } else {
John Stilesfbd050b2020-08-03 13:21:46 -04001178 hashAndEncode = std::make_unique<HashAndEncode>(bitmap);
Mike Klein989f5bf2020-10-07 10:11:50 -05001179 hashAndEncode->feedHash(&hash);
mtkleincd50bca2016-01-05 06:20:20 -08001180 }
Hal Canary0f2f5222019-04-03 10:13:45 -04001181 SkMD5::Digest digest = hash.finish();
mtkleincd50bca2016-01-05 06:20:20 -08001182 for (int i = 0; i < 16; i++) {
1183 md5.appendf("%02x", digest.data[i]);
mtklein38408462015-07-08 07:25:27 -07001184 }
mtklein62bd1a62015-01-27 14:46:26 -08001185 }
mtklein62bd1a62015-01-27 14:46:26 -08001186
Brian Osman57796b32019-01-25 18:02:59 +00001187 if (!FLAGS_readPath.isEmpty() &&
John Stiles33e044f2020-06-19 18:34:51 -04001188 !gGold->contains(Gold(task.sink.tag, task.src.tag,
1189 task.src.options, name, md5))) {
Brian Osman57796b32019-01-25 18:02:59 +00001190 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
Mike Klein055f5352020-01-30 11:11:12 -06001199 // 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;
John Stiles637838d2020-06-12 12:10:54 -04001224 rasterized.allocPixels(SkImageInfo::Make(
1225 w, h, kRGBA_8888_SkColorType, kPremul_SkAlphaType));
Mike Klein055f5352020-01-30 11:11:12 -06001226 rasterized.eraseColor(SK_ColorWHITE);
1227
John Stiles637838d2020-06-12 12:10:54 -04001228 SkUniqueCFRef<CGColorSpaceRef> cs{CGColorSpaceCreateDeviceRGB()};
1229 CGBitmapInfo info = kCGBitmapByteOrder32Big |
1230 (CGBitmapInfo)kCGImageAlphaPremultipliedLast;
Mike Klein055f5352020-01-30 11:11:12 -06001231
John Stiles637838d2020-06-12 12:10:54 -04001232 SkUniqueCFRef<CGContextRef> ctx{CGBitmapContextCreate(
1233 rasterized.getPixels(), w,h,8, rasterized.rowBytes(), cs.get(), info)};
Mike Klein055f5352020-01-30 11:11:12 -06001234 CGContextDrawPDFPage(ctx.get(), page);
1235
Mike Klein989f5bf2020-10-07 10:11:50 -05001236 // Skip calling hashAndEncode->feedHash(SkMD5*)... we want the .pdf's hash.
John Stilesfbd050b2020-08-03 13:21:46 -04001237 hashAndEncode = std::make_unique<HashAndEncode>(rasterized);
Mike Klein055f5352020-01-30 11:11:12 -06001238 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());
mtkleincd50bca2016-01-05 06:20:20 -08001246 }
1247 }
Brian Osman582f6862019-03-11 11:20:55 -04001248
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()) {
Brian Osmanb73e6072019-03-15 10:02:16 -04001258 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]);
Brian Osman582f6862019-03-11 11:20:55 -04001260 unclamped = true;
1261 }
1262 }
1263 }
mtkleincd50bca2016-01-05 06:20:20 -08001264 });
mtklein748ca3b2015-01-15 10:56:12 -08001265 }
mtklein3eed7dd2016-02-24 19:07:07 -08001266 done(task.sink.tag.c_str(), task.src.tag.c_str(), task.src.options.c_str(), name.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001267 }
1268
Mike Klein66f09a72019-02-12 13:03:54 -05001269 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
Mike Kleinb147ace2020-01-16 11:11:06 -06001284 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"); }
Mike Klein66f09a72019-02-12 13:03:54 -05001290 return SkString("other");
1291 }
1292 return SkString("non-XYZ");
1293 }
1294
1295 static SkString identify_transfer_fn(SkColorSpace* cs) {
1296 if (!cs) {
1297 return SkString("untagged");
1298 }
1299
Brian Osman11e6aa82019-10-16 13:58:42 -04001300 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 };
Mike Klein66f09a72019-02-12 13:03:54 -05001309
Brian Osman11e6aa82019-10-16 13:58:42 -04001310 skcms_TransferFunction tf;
Brian Osman09ee1122019-11-05 11:41:16 -05001311 cs->transferFn(&tf);
Brian Osman11e6aa82019-10-16 13:58:42 -04001312 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"); }
Mike Klein627c0022021-01-07 10:50:01 -06001329 return SkStringPrintf("HLGish %.3g %.3g %.3g %.3g %.3g (%.3g)",
1330 tf.a, tf.b, tf.c, tf.d, tf.e, tf.f+1);
Brian Osman11e6aa82019-10-16 13:58:42 -04001331
1332 case HLGinvish_TF: break;
1333 case Bad_TF: break;
Mike Klein66f09a72019-02-12 13:03:54 -05001334 }
1335 return SkString("non-numeric");
1336 }
1337
mtklein748ca3b2015-01-15 10:56:12 -08001338 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -08001339 SkString md5,
1340 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -08001341 SkStream* data, size_t len,
Mike Kleine1271702019-03-07 11:46:59 -06001342 const SkBitmap* bitmap,
1343 const HashAndEncode* hashAndEncode) {
mtklein409d4702016-02-29 07:38:01 -08001344
mtklein748ca3b2015-01-15 10:56:12 -08001345 JsonWriter::BitmapResult result;
djsollen54416de2015-04-03 07:24:48 -07001346 result.name = task.src->name();
mtklein3eed7dd2016-02-24 19:07:07 -08001347 result.config = task.sink.tag;
djsollen54416de2015-04-03 07:24:48 -07001348 result.sourceType = task.src.tag;
1349 result.sourceOptions = task.src.options;
1350 result.ext = ext;
1351 result.md5 = md5;
Mike Klein0abbaca2019-03-06 09:57:49 -06001352 if (bitmap) {
Mike Klein98168782019-04-09 13:45:02 -05001353 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());
Mike Klein0abbaca2019-03-06 09:57:49 -06001358 }
mtklein748ca3b2015-01-15 10:56:12 -08001359 JsonWriter::AddBitmapResult(result);
1360
mtkleinb0531a72015-04-07 13:38:48 -07001361 // 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).
John Stiles33e044f2020-06-19 18:34:51 -04001363 if (gUninterestingHashes->contains(md5)) {
mtkleinb0531a72015-04-07 13:38:48 -07001364 return;
1365 }
1366
mtklein748ca3b2015-01-15 10:56:12 -08001367 const char* dir = FLAGS_writePath[0];
Mike Kleinc6142d82019-03-25 10:54:59 -05001368 SkString resources = GetResourcePath();
mtklein748ca3b2015-01-15 10:56:12 -08001369 if (0 == strcmp(dir, "@")) { // Needed for iOS.
Mike Kleinc6142d82019-03-25 10:54:59 -05001370 dir = resources.c_str();
mtklein748ca3b2015-01-15 10:56:12 -08001371 }
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 {
kkinnunen3e980c32015-12-23 01:33:00 -08001383 path = SkOSPath::Join(dir, task.sink.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001384 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001385 path = SkOSPath::Join(path.c_str(), task.src.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001386 sk_mkdir(path.c_str());
John Stilesc1c3c6d2020-08-15 23:22:53 -04001387 if (0 != strcmp(task.src.options.c_str(), "")) {
msarett9e707a02015-09-01 14:57:57 -07001388 path = SkOSPath::Join(path.c_str(), task.src.options.c_str());
djsollen54416de2015-04-03 07:24:48 -07001389 sk_mkdir(path.c_str());
1390 }
mtklein748ca3b2015-01-15 10:56:12 -08001391 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
1392 path.append(".");
1393 path.append(ext);
1394 }
1395
Mike Klein989f5bf2020-10-07 10:11:50 -05001396 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 }
mtklein748ca3b2015-01-15 10:56:12 -08001401 if (bitmap) {
Mike Kleine1271702019-03-07 11:46:59 -06001402 SkASSERT(hashAndEncode);
Mike Klein989f5bf2020-10-07 10:11:50 -05001403 if (!hashAndEncode->encodePNG(&file,
1404 result.md5.c_str(),
1405 FLAGS_key,
1406 FLAGS_properties)) {
mtklein748ca3b2015-01-15 10:56:12 -08001407 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 }
Tyler Denniston45f94f82020-02-04 16:09:08 -05001417
1418 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 }
mtklein748ca3b2015-01-15 10:56:12 -08001434};
1435
mtklein748ca3b2015-01-15 10:56:12 -08001436/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1437
1438// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
1439
John Stiles33e044f2020-06-19 18:34:51 -04001440static SkTDArray<skiatest::Test>* gParallelTests = new SkTDArray<skiatest::Test>;
1441static SkTDArray<skiatest::Test>* gSerialTests = new SkTDArray<skiatest::Test>;
mtklein748ca3b2015-01-15 10:56:12 -08001442
1443static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -08001444 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -08001445 return;
1446 }
Hal Canary972eba32018-07-30 17:07:07 -04001447 for (const skiatest::Test& test : skiatest::TestRegistry::Range()) {
mtklein6393c062015-04-27 08:45:01 -07001448 if (!in_shard()) {
1449 continue;
1450 }
Mike Klein88544fb2019-03-20 10:50:33 -05001451 if (CommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -08001452 continue;
1453 }
Brian Osmanc7ad40f2018-05-31 14:27:17 -04001454 if (test.needsGpu && FLAGS_gpu) {
John Stiles33e044f2020-06-19 18:34:51 -04001455 gSerialTests->push_back(test);
halcanary87f3ba42015-01-20 09:30:20 -08001456 } else if (!test.needsGpu && FLAGS_cpu) {
John Stiles33e044f2020-06-19 18:34:51 -04001457 gParallelTests->push_back(test);
mtklein82d28432015-01-15 12:46:02 -08001458 }
mtklein748ca3b2015-01-15 10:56:12 -08001459 }
1460}
1461
csmartdalton008b9d82017-02-22 12:00:42 -07001462static void run_test(skiatest::Test test, const GrContextOptions& grCtxOptions) {
halcanary87f3ba42015-01-20 09:30:20 -08001463 struct : public skiatest::Reporter {
mtklein36352bf2015-03-25 18:17:31 -07001464 void reportFailed(const skiatest::Failure& failure) override {
halcanary87f3ba42015-01-20 09:30:20 -08001465 fail(failure.toString());
halcanary87f3ba42015-01-20 09:30:20 -08001466 }
mtklein36352bf2015-03-25 18:17:31 -07001467 bool allowExtendedTest() const override {
halcanary87f3ba42015-01-20 09:30:20 -08001468 return FLAGS_pathOpsExtended;
1469 }
mtklein36352bf2015-03-25 18:17:31 -07001470 bool verbose() const override { return FLAGS_veryVerbose; }
halcanary87f3ba42015-01-20 09:30:20 -08001471 } reporter;
djsollen824996a2015-06-12 12:06:22 -07001472
Mike Kleina54d3802020-07-24 12:37:32 -05001473 if (!FLAGS_dryRun && !should_skip("_", "tests", "_", test.name)) {
Jim Van Verth8a9a3712019-05-31 10:49:12 -04001474 AutoreleasePool pool;
Robert Phillipsec325342017-10-30 18:02:48 +00001475 GrContextOptions options = grCtxOptions;
1476 test.modifyGrContextOptions(&options);
1477
Brian Osmancdf31742019-03-13 10:13:46 -04001478 skiatest::ReporterContext ctx(&reporter, SkString(test.name));
mtklein21eaf3b2016-02-08 12:39:59 -08001479 start("unit", "test", "", test.name);
Brian Salomondcfca432017-11-15 15:48:03 -05001480 test.run(&reporter, options);
mtklein748ca3b2015-01-15 10:56:12 -08001481 }
mtklein3eed7dd2016-02-24 19:07:07 -08001482 done("unit", "test", "", test.name);
mtklein748ca3b2015-01-15 10:56:12 -08001483}
1484
1485/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1486
Mike Kleinbe28ee22017-02-06 12:46:20 -05001487int main(int argc, char** argv) {
Brian Osman1c1208e2019-06-24 13:36:05 -04001488#if defined(__MSVC_RUNTIME_CHECKS)
1489 _RTC_SetErrorFunc(RuntimeCheckErrorFunc);
1490#endif
Dongwon Kang0c7861f2018-02-16 10:55:21 -08001491#if defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) && defined(SK_HAS_HEIF_LIBRARY)
1492 android::ProcessState::self()->startThreadPool();
1493#endif
Mike Klein88544fb2019-03-20 10:50:33 -05001494 CommandLineFlags::Parse(argc, argv);
Brian Osman53136aa2017-07-20 15:43:35 -04001495
Brian Osmanbc8150f2017-07-24 11:38:01 -04001496 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -04001497
Mike Klein6613cc52017-12-19 09:09:33 -05001498#if !defined(SK_BUILD_FOR_GOOGLE3) && defined(SK_BUILD_FOR_IOS)
Mike Kleinadacaef2017-02-06 09:26:14 -05001499 cd_Documents();
1500#endif
bsalomon71de3532016-04-26 14:27:21 -07001501 setbuf(stdout, nullptr);
mtklein246ba3a2016-02-23 10:39:36 -08001502 setup_crash_handler();
mtkleinafae30a2016-02-24 12:28:32 -08001503
Mike Klein19cc0f62019-03-22 15:30:07 -05001504 ToolUtils::SetDefaultFontMgr();
Mike Klein03fa5d42019-03-22 11:39:09 -05001505 SetAnalyticAAFromCommonFlags();
liyuqian38911a72016-10-04 11:23:22 -07001506
Mike Klein9ae06632020-01-28 12:41:02 -06001507 gSkForceRasterPipelineBlitter = FLAGS_forceRasterPipeline;
1508 gUseSkVMBlitter = FLAGS_skvm;
Mike Klein813e8cc2020-08-05 09:33:38 -05001509 gSkVMAllowJIT = FLAGS_jit;
Yuqian Li550148b2017-01-13 10:13:13 -05001510
Mike Kleine9f187b2017-06-26 13:23:12 -04001511 // The bots like having a verbose.log to upload, so always touch the file even if --verbose.
1512 if (!FLAGS_writePath.isEmpty()) {
mtklein51c8cfc2016-03-11 12:59:09 -08001513 sk_mkdir(FLAGS_writePath[0]);
mtkleina1ce2162016-07-22 12:23:46 -07001514 gVLog = fopen(SkOSPath::Join(FLAGS_writePath[0], "verbose.log").c_str(), "w");
mtkleinc41fd922016-03-08 09:01:39 -08001515 }
Mike Kleine9f187b2017-06-26 13:23:12 -04001516 if (FLAGS_verbose) {
1517 gVLog = stderr;
1518 }
mtkleinc41fd922016-03-08 09:01:39 -08001519
csmartdalton008b9d82017-02-22 12:00:42 -07001520 GrContextOptions grCtxOptions;
Chris Dalton040238b2017-12-18 14:22:34 -07001521 SetCtxOptionsFromCommonFlags(&grCtxOptions);
csmartdalton008b9d82017-02-22 12:00:42 -07001522
Mike Kleinc6142d82019-03-25 10:54:59 -05001523 dump_json(); // It's handy for the bots to assume this is ~never missing.
1524
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +00001525 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -07001526 SkTaskGroup::Enabler enabled(FLAGS_threads);
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +00001527
Mike Reed0933bc92017-12-09 01:27:41 +00001528 if (nullptr == GetResourceAsData("images/color_wheel.png")) {
1529 info("Some resources are missing. Do you need to set --resourcePath?\n");
halcanary7d571242016-02-24 17:59:16 -08001530 }
Brian Osman57796b32019-01-25 18:02:59 +00001531 gather_gold();
borenet09ed4802015-04-03 14:15:33 -07001532 gather_uninteresting_hashes();
mtklein62bd1a62015-01-27 14:46:26 -08001533
scroggo86737142016-02-03 12:19:11 -08001534 if (!gather_srcs()) {
1535 return 1;
1536 }
Ben Wagnerf28d4722017-08-11 17:33:22 -04001537 // 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++) {
John Stiles33e044f2020-06-19 18:34:51 -04001540 static constexpr char kConfigArg[] = "--config";
Ben Wagnerf28d4722017-08-11 17:33:22 -04001541 if (strcmp(argv[i], kConfigArg) == 0) {
1542 defaultConfigs = false;
1543 break;
1544 }
1545 }
1546 if (!gather_sinks(grCtxOptions, defaultConfigs)) {
brianosman05c987c2016-05-09 13:42:16 -07001547 return 1;
1548 }
mtklein748ca3b2015-01-15 10:56:12 -08001549 gather_tests();
John Stiles33e044f2020-06-19 18:34:51 -04001550 gPending = gSrcs->count() * gSinks->count() + gParallelTests->count() + gSerialTests->count();
Brian Osmand0677bc2017-08-03 12:13:47 -04001551 info("%d srcs * %d sinks + %d tests == %d tasks\n",
John Stiles33e044f2020-06-19 18:34:51 -04001552 gSrcs->count(), gSinks->count(), gParallelTests->count() + gSerialTests->count(),
1553 gPending);
mtklein748ca3b2015-01-15 10:56:12 -08001554
mtklein21eaf3b2016-02-08 12:39:59 -08001555 // 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
John Stiles33e044f2020-06-19 18:34:51 -04001559 for (TaggedSink& sink : *gSinks) {
1560 for (TaggedSrc& src : *gSrcs) {
1561 if (src->veto(sink->flags()) ||
Mike Kleina54d3802020-07-24 12:37:32 -05001562 should_skip(sink.tag.c_str(), src.tag.c_str(),
1563 src.options.c_str(), src->name().c_str())) {
John Stiles33e044f2020-06-19 18:34:51 -04001564 SkAutoSpinlock lock(*gMutex);
1565 gPending--;
1566 continue;
1567 }
mtklein3eed7dd2016-02-24 19:07:07 -08001568
John Stiles33e044f2020-06-19 18:34:51 -04001569 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 }
mtklein748ca3b2015-01-15 10:56:12 -08001575 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001576 }
John Stiles33e044f2020-06-19 18:34:51 -04001577 for (skiatest::Test& test : *gParallelTests) {
csmartdalton008b9d82017-02-22 12:00:42 -07001578 parallel.add([test, grCtxOptions] { run_test(test, grCtxOptions); });
mtklein55e88b22015-01-21 15:50:13 -08001579 }
mtklein21eaf3b2016-02-08 12:39:59 -08001580
1581 // With the parallel work running, run serial tasks and tests here on main thread.
John Stiles33e044f2020-06-19 18:34:51 -04001582 for (Task& task : serial) { Task::Run(task); }
1583 for (skiatest::Test& test : *gSerialTests) { run_test(test, grCtxOptions); }
mtklein21eaf3b2016-02-08 12:39:59 -08001584
1585 // Wait for any remaining parallel work to complete (including any spun off of serial tasks).
1586 parallel.wait();
John Stiles33e044f2020-06-19 18:34:51 -04001587 gDefinitelyThreadSafeWork->wait();
mtkleincd50bca2016-01-05 06:20:20 -08001588
Ben Wagner83c6b962018-07-10 19:40:15 -04001589 // At this point we're back in single-threaded land.
1590
mtkleinda884c42016-02-24 18:00:23 -08001591 // We'd better have run everything.
1592 SkASSERT(gPending == 0);
mtkleine027f172016-02-26 15:53:06 -08001593 // Make sure we've flushed all our results to disk.
Mike Kleinc6142d82019-03-25 10:54:59 -05001594 dump_json();
mtkleinda884c42016-02-24 18:00:23 -08001595
John Stiles33e044f2020-06-19 18:34:51 -04001596 if (!gFailures->empty()) {
mtkleinc41fd922016-03-08 09:01:39 -08001597 info("Failures:\n");
John Stiles33e044f2020-06-19 18:34:51 -04001598 for (const SkString& fail : *gFailures) {
1599 info("\t%s\n", fail.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001600 }
John Stiles33e044f2020-06-19 18:34:51 -04001601 info("%d failures\n", gFailures->count());
mtklein748ca3b2015-01-15 10:56:12 -08001602 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001603 }
mtkleinafae30a2016-02-24 12:28:32 -08001604
Herb Derby5a523fe2017-01-26 16:48:28 -05001605 SkGraphics::PurgeAllCaches();
mtkleinc41fd922016-03-08 09:01:39 -08001606 info("Finished!\n");
Brian Osman53136aa2017-07-20 15:43:35 -04001607
mtklein748ca3b2015-01-15 10:56:12 -08001608 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +00001609}