blob: 28369e9c08bfb6e354e022f6319f1d86f3c91e59 [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
mtklein748ca3b2015-01-15 10:56:12 -08008#include "DMJsonWriter.h"
9#include "DMSrcSink.h"
mtklein748ca3b2015-01-15 10:56:12 -080010#include "ProcStats.h"
halcanary7d571242016-02-24 17:59:16 -080011#include "Resources.h"
mtklein748ca3b2015-01-15 10:56:12 -080012#include "SkBBHFactory.h"
mtklein62bd1a62015-01-27 14:46:26 -080013#include "SkChecksum.h"
Brian Osman53136aa2017-07-20 15:43:35 -040014#include "SkChromeTracingTracer.h"
scroggocc2feb12015-08-14 08:32:46 -070015#include "SkCodec.h"
mtklein27c3fdd2016-02-26 14:43:21 -080016#include "SkColorPriv.h"
msarett888dc162016-05-23 10:21:17 -070017#include "SkColorSpace.h"
Mike Klein919cc452017-03-18 15:36:52 +000018#include "SkColorSpacePriv.h"
caryclark17f0b6d2014-07-22 10:15:34 -070019#include "SkCommonFlags.h"
kkinnunen3e980c32015-12-23 01:33:00 -080020#include "SkCommonFlagsConfig.h"
Chris Dalton040238b2017-12-18 14:22:34 -070021#include "SkCommonFlagsGpu.h"
brianosman3c579dc2016-04-19 09:18:11 -070022#include "SkData.h"
Brian Salomondcbb9d92017-07-19 10:53:20 -040023#include "SkDebugfTracer.h"
Hal Canaryd7b38452017-12-11 17:46:26 -050024#include "SkDocument.h"
Brian Osman53136aa2017-07-20 15:43:35 -040025#include "SkEventTracingPriv.h"
caryclark83ca6282015-06-10 09:31:09 -070026#include "SkFontMgr.h"
Hal Canaryd7b38452017-12-11 17:46:26 -050027#include "SkFontMgrPriv.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +000028#include "SkGraphics.h"
mtklein27c3fdd2016-02-26 14:43:21 -080029#include "SkHalf.h"
halcanary4dbbd042016-06-07 17:21:10 -070030#include "SkLeanWindows.h"
mtklein748ca3b2015-01-15 10:56:12 -080031#include "SkMD5.h"
mtklein1b249332015-07-07 12:21:21 -070032#include "SkMutex.h"
mtklein1d0f1642014-09-08 08:05:18 -070033#include "SkOSFile.h"
Ben Wagnerbf111d72016-11-07 18:05:29 -050034#include "SkOSPath.h"
mtklein27c3fdd2016-02-26 14:43:21 -080035#include "SkPM4fPriv.h"
Mike Klein13b6afd2017-06-12 12:45:41 -040036#include "SkPngEncoder.h"
Brian Salomondcbb9d92017-07-19 10:53:20 -040037#include "SkScan.h"
mtklein246ba3a2016-02-23 10:39:36 -080038#include "SkSpinlock.h"
Ben Wagner483c7722018-02-20 17:06:07 -050039#include "SkTestFontMgr.h"
mtkleina82f5622015-02-20 12:30:19 -080040#include "SkTHash.h"
mtklein406654b2014-09-03 15:34:37 -070041#include "SkTaskGroup.h"
Mike Klein0c5fdcb2017-11-13 12:40:46 -050042#include "SkTypeface_win.h"
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +000043#include "Test.h"
mtklein748ca3b2015-01-15 10:56:12 -080044#include "Timer.h"
Mike Kleinadacaef2017-02-06 09:26:14 -050045#include "ios_utils.h"
brianosman3c579dc2016-04-19 09:18:11 -070046#include "picture_utils.h"
caryclark83ca6282015-06-10 09:31:09 -070047#include "sk_tool_utils.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +000048
scroggo19b91532016-10-24 09:03:26 -070049#include <vector>
50
halcanary7a14b312015-10-01 07:28:13 -070051#ifdef SK_PDF_IMAGE_STATS
52extern void SkPDFImageDumpStats();
53#endif
54
mtkleina5114d72015-08-24 13:27:01 -070055#include "png.h"
56
bungeman60e0fee2015-08-26 05:15:46 -070057#include <stdlib.h>
58
Mike Klein8f11d4d2018-01-24 12:42:55 -050059#ifndef SK_BUILD_FOR_WIN
scroggo27a58342015-10-28 08:56:41 -070060 #include <unistd.h>
61#endif
62
Dongwon Kang0c7861f2018-02-16 10:55:21 -080063#if defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) && defined(SK_HAS_HEIF_LIBRARY)
64#include <binder/IPCThreadState.h>
65#endif
66
Mike Reed7c26ca72017-07-05 15:45:52 -040067extern bool gSkForceRasterPipelineBlitter;
68
Ben Wagner32fa5102017-08-10 21:25:55 -040069DECLARE_bool(undefok);
djsollen54416de2015-04-03 07:24:48 -070070DEFINE_string(src, "tests gm skp image", "Source types to test.");
mtklein748ca3b2015-01-15 10:56:12 -080071DEFINE_bool(nameByHash, false,
72 "If true, write to FLAGS_writePath[0]/<hash>.png instead of "
djsollen54416de2015-04-03 07:24:48 -070073 "to FLAGS_writePath[0]/<config>/<sourceType>/<sourceOptions>/<name>.png");
mtklein748ca3b2015-01-15 10:56:12 -080074DEFINE_bool2(pathOpsExtended, x, false, "Run extended pathOps tests.");
mtkleind603b222015-02-17 11:13:33 -080075DEFINE_string(matrix, "1 0 0 1",
76 "2x2 scale+skew matrix to apply or upright when using "
77 "'matrix' or 'upright' in config.");
mtklein82d28432015-01-15 12:46:02 -080078DEFINE_bool(gpu_threading, false, "Allow GPU work to run on multiple threads?");
mtklein@google.comd36522d2013-10-16 13:02:15 +000079
mtkleina2ef6422015-01-15 13:44:22 -080080DEFINE_string(blacklist, "",
Chris Daltonecf78ac2017-08-15 15:43:08 -060081 "Space-separated config/src/srcOptions/name quadruples to blacklist. "
82 "'_' matches anything. '~' negates the match. E.g. \n"
djsollen54416de2015-04-03 07:24:48 -070083 "'--blacklist gpu skp _ _' will blacklist all SKPs drawn into the gpu config.\n"
Chris Daltonecf78ac2017-08-15 15:43:08 -060084 "'--blacklist gpu skp _ _ 8888 gm _ aarects' will also blacklist the aarects GM on 8888.\n"
85 "'--blacklist ~8888 svg _ svgparse_' blocks non-8888 SVGs that contain \"svgparse_\" in "
86 "the name.");
mtkleina2ef6422015-01-15 13:44:22 -080087
mtklein62bd1a62015-01-27 14:46:26 -080088DEFINE_string2(readPath, r, "", "If set check for equality with golden results in this directory.");
89
borenet09ed4802015-04-03 14:15:33 -070090DEFINE_string(uninterestingHashesFile, "",
91 "File containing a list of uninteresting hashes. If a result hashes to something in "
92 "this list, no image is written for that result.");
93
mtklein6393c062015-04-27 08:45:01 -070094DEFINE_int32(shards, 1, "We're splitting source data into this many shards.");
95DEFINE_int32(shard, 0, "Which shard do I run?");
96
halcanary45420a92016-06-02 12:41:14 -070097DEFINE_string(mskps, "", "Directory to read mskps from, or a single mskp file.");
Mike Reed7c26ca72017-07-05 15:45:52 -040098DEFINE_bool(forceRasterPipeline, false, "sets gSkForceRasterPipelineBlitter");
halcanary45420a92016-06-02 12:41:14 -070099
Derek Sollenbergeredfe3df2017-07-19 15:25:24 -0400100DEFINE_bool(ignoreSigInt, false, "ignore SIGINT signals during test execution");
101
Mike Klein97d6a7a2017-07-24 10:37:19 -0400102DEFINE_string(dont_write, "", "File extensions to skip writing to --writePath."); // See skia:6821
103
Mike Klein0c5fdcb2017-11-13 12:40:46 -0500104DEFINE_bool(gdi, false, "On Windows, use GDI instead of DirectWrite for font rendering.");
Mike Klein31868212017-11-06 12:02:47 -0500105
mtklein748ca3b2015-01-15 10:56:12 -0800106using namespace DM;
bsalomon3724e572016-03-30 18:56:19 -0700107using sk_gpu_test::GrContextFactory;
bsalomon273c0f52016-03-31 10:59:06 -0700108using sk_gpu_test::GLTestContext;
bsalomonf2f1c172016-04-05 12:59:06 -0700109using sk_gpu_test::ContextInfo;
mtklein@google.comd36522d2013-10-16 13:02:15 +0000110
mtklein748ca3b2015-01-15 10:56:12 -0800111/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
112
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700113static const double kStartMs = SkTime::GetMSecs();
mtkleinc41fd922016-03-08 09:01:39 -0800114
115static FILE* gVLog;
116
117template <typename... Args>
118static void vlog(const char* fmt, Args&&... args) {
119 if (gVLog) {
Jim Van Verth32bbf372017-09-20 12:53:00 -0400120 char s[64];
121 HumanizeMs(s, 64, SkTime::GetMSecs() - kStartMs);
122 fprintf(gVLog, "%s\t", s);
mtkleinc41fd922016-03-08 09:01:39 -0800123 fprintf(gVLog, fmt, args...);
124 fflush(gVLog);
125 }
126}
127
128template <typename... Args>
129static void info(const char* fmt, Args&&... args) {
130 vlog(fmt, args...);
131 if (!FLAGS_quiet) {
132 printf(fmt, args...);
133 }
134}
135static void info(const char* fmt) {
136 if (!FLAGS_quiet) {
137 printf("%s", fmt); // Clang warns printf(fmt) is insecure.
138 }
139}
140
reed086eea92016-05-04 17:12:46 -0700141SK_DECLARE_STATIC_MUTEX(gFailuresMutex);
mtklein748ca3b2015-01-15 10:56:12 -0800142static SkTArray<SkString> gFailures;
143
mtklein3eed7dd2016-02-24 19:07:07 -0800144static void fail(const SkString& err) {
mtklein748ca3b2015-01-15 10:56:12 -0800145 SkAutoMutexAcquire lock(gFailuresMutex);
146 SkDebugf("\n\nFAILURE: %s\n\n", err.c_str());
147 gFailures.push_back(err);
halcanary9e398f72015-01-10 11:18:04 -0800148}
149
mtklein0f7748a2016-07-25 15:27:29 -0700150struct Running {
151 SkString id;
152 SkThreadID thread;
153
154 void dump() const {
155 info("\t%s\n", id.c_str());
156 }
157};
mtkleinb37cb412015-03-18 05:27:14 -0700158
mtklein246ba3a2016-02-23 10:39:36 -0800159// We use a spinlock to make locking this in a signal handler _somewhat_ safe.
Mike Klein03a95ad2017-06-23 12:36:32 -0400160static SkSpinlock gMutex;
161static int gPending;
mtklein0f7748a2016-07-25 15:27:29 -0700162static SkTArray<Running> gRunning;
mtklein748ca3b2015-01-15 10:56:12 -0800163
mtklein3eed7dd2016-02-24 19:07:07 -0800164static void done(const char* config, const char* src, const char* srcOptions, const char* name) {
165 SkString id = SkStringPrintf("%s %s %s %s", config, src, srcOptions, name);
mtkleinc41fd922016-03-08 09:01:39 -0800166 vlog("done %s\n", id.c_str());
mtkleinafae30a2016-02-24 12:28:32 -0800167 int pending;
mtkleinb37cb412015-03-18 05:27:14 -0700168 {
mtkleinf10637f2016-06-10 13:56:35 -0700169 SkAutoMutexAcquire lock(gMutex);
mtkleinb37cb412015-03-18 05:27:14 -0700170 for (int i = 0; i < gRunning.count(); i++) {
mtklein0f7748a2016-07-25 15:27:29 -0700171 if (gRunning[i].id == id) {
mtkleinb37cb412015-03-18 05:27:14 -0700172 gRunning.removeShuffle(i);
173 break;
174 }
175 }
mtkleinafae30a2016-02-24 12:28:32 -0800176 pending = --gPending;
reed50bc0512015-05-19 14:13:31 -0700177 }
Mike Klein03a95ad2017-06-23 12:36:32 -0400178
179 // We write out dm.json file and print out a progress update every once in a while.
180 // Notice this also handles the final dm.json and progress update when pending == 0.
mtkleina17241b2015-01-23 05:48:00 -0800181 if (pending % 500 == 0) {
182 JsonWriter::DumpJson();
Mike Klein03a95ad2017-06-23 12:36:32 -0400183
184 int curr = sk_tools::getCurrResidentSetSizeMB(),
185 peak = sk_tools::getMaxResidentSetSizeMB();
186 SkString elapsed = HumanizeMs(SkTime::GetMSecs() - kStartMs);
187
188 SkAutoMutexAcquire lock(gMutex);
189 info("\n%dMB RAM, %dMB peak, %s elapsed, %d queued, %d active:\n",
190 curr, peak, elapsed.c_str(), gPending - gRunning.count(), gRunning.count());
191 for (auto& task : gRunning) {
192 task.dump();
193 }
mtkleina17241b2015-01-23 05:48:00 -0800194 }
mtklein@google.comd36522d2013-10-16 13:02:15 +0000195}
196
mtklein3eed7dd2016-02-24 19:07:07 -0800197static void start(const char* config, const char* src, const char* srcOptions, const char* name) {
198 SkString id = SkStringPrintf("%s %s %s %s", config, src, srcOptions, name);
mtkleinc41fd922016-03-08 09:01:39 -0800199 vlog("start %s\n", id.c_str());
mtkleinf10637f2016-06-10 13:56:35 -0700200 SkAutoMutexAcquire lock(gMutex);
mtklein0f7748a2016-07-25 15:27:29 -0700201 gRunning.push_back({id,SkGetThreadID()});
mtkleinb37cb412015-03-18 05:27:14 -0700202}
203
Mike Klein3cc2d202016-10-24 11:39:43 -0400204static void find_culprit() {
205 // Assumes gMutex is locked.
206 SkThreadID thisThread = SkGetThreadID();
207 for (auto& task : gRunning) {
208 if (task.thread == thisThread) {
209 info("Likely culprit:\n");
210 task.dump();
mtklein0f7748a2016-07-25 15:27:29 -0700211 }
212 }
Mike Klein3cc2d202016-10-24 11:39:43 -0400213}
mtklein0f7748a2016-07-25 15:27:29 -0700214
Mike Klein8f11d4d2018-01-24 12:42:55 -0500215#if defined(SK_BUILD_FOR_WIN)
mtklein5b64dab2016-06-09 08:59:48 -0700216 static LONG WINAPI crash_handler(EXCEPTION_POINTERS* e) {
mtkleinf8557292016-02-29 06:35:28 -0800217 static const struct {
218 const char* name;
robertphillips75467862016-03-01 14:10:23 -0800219 DWORD code;
mtkleinf8557292016-02-29 06:35:28 -0800220 } kExceptions[] = {
221 #define _(E) {#E, E}
222 _(EXCEPTION_ACCESS_VIOLATION),
223 _(EXCEPTION_BREAKPOINT),
224 _(EXCEPTION_INT_DIVIDE_BY_ZERO),
225 _(EXCEPTION_STACK_OVERFLOW),
226 // TODO: more?
227 #undef _
228 };
229
mtkleinf10637f2016-06-10 13:56:35 -0700230 SkAutoMutexAcquire lock(gMutex);
mtklein5b64dab2016-06-09 08:59:48 -0700231
232 const DWORD code = e->ExceptionRecord->ExceptionCode;
233 info("\nCaught exception %u", code);
234 for (const auto& exception : kExceptions) {
235 if (exception.code == code) {
236 info(" %s", exception.name);
mtkleinf8557292016-02-29 06:35:28 -0800237 }
mtkleinf8557292016-02-29 06:35:28 -0800238 }
mtklein5b64dab2016-06-09 08:59:48 -0700239 info(", was running:\n");
240 for (auto& task : gRunning) {
mtklein0f7748a2016-07-25 15:27:29 -0700241 task.dump();
mtklein5b64dab2016-06-09 08:59:48 -0700242 }
mtklein0f7748a2016-07-25 15:27:29 -0700243 find_culprit();
mtklein5b64dab2016-06-09 08:59:48 -0700244 fflush(stdout);
245
mtkleinf8557292016-02-29 06:35:28 -0800246 // Execute default exception handler... hopefully, exit.
247 return EXCEPTION_EXECUTE_HANDLER;
mtklein246ba3a2016-02-23 10:39:36 -0800248 }
249
Mike Klein3cc2d202016-10-24 11:39:43 -0400250 static void setup_crash_handler() {
251 SetUnhandledExceptionFilter(crash_handler);
252 }
253#else
mtklein246ba3a2016-02-23 10:39:36 -0800254 #include <signal.h>
Mike Klein3cc2d202016-10-24 11:39:43 -0400255 #if !defined(SK_BUILD_FOR_ANDROID)
256 #include <execinfo.h>
Brian Salomondcbb9d92017-07-19 10:53:20 -0400257
258#endif
Mike Klein3cc2d202016-10-24 11:39:43 -0400259
260 static constexpr int max_of() { return 0; }
261 template <typename... Rest>
262 static constexpr int max_of(int x, Rest... rest) {
263 return x > max_of(rest...) ? x : max_of(rest...);
264 }
265
Mike Kleinc7278ad2018-02-15 09:26:54 -0500266 static void (*previous_handler[max_of(SIGABRT,SIGBUS,SIGFPE,SIGILL,SIGSEGV,SIGTERM)+1])(int);
mtkleinf8557292016-02-29 06:35:28 -0800267
mtklein5b64dab2016-06-09 08:59:48 -0700268 static void crash_handler(int sig) {
mtkleinf10637f2016-06-10 13:56:35 -0700269 SkAutoMutexAcquire lock(gMutex);
mtklein5b64dab2016-06-09 08:59:48 -0700270
Mike Klein701a3582018-06-28 14:08:19 +0000271 info("\nCaught signal %d [%s] (%dMB RAM, peak %dMB), was running:\n",
272 sig, strsignal(sig),
273 sk_tools::getCurrResidentSetSizeMB(), sk_tools::getMaxResidentSetSizeMB());
274
mtklein5b64dab2016-06-09 08:59:48 -0700275 for (auto& task : gRunning) {
mtklein0f7748a2016-07-25 15:27:29 -0700276 task.dump();
mtklein5b64dab2016-06-09 08:59:48 -0700277 }
mtklein0f7748a2016-07-25 15:27:29 -0700278 find_culprit();
mtklein5b64dab2016-06-09 08:59:48 -0700279
Mike Klein3cc2d202016-10-24 11:39:43 -0400280 #if !defined(SK_BUILD_FOR_ANDROID)
mtklein5b64dab2016-06-09 08:59:48 -0700281 void* stack[64];
282 int count = backtrace(stack, SK_ARRAY_COUNT(stack));
283 char** symbols = backtrace_symbols(stack, count);
284 info("\nStack trace:\n");
285 for (int i = 0; i < count; i++) {
286 info(" %s\n", symbols[i]);
287 }
Mike Klein3cc2d202016-10-24 11:39:43 -0400288 #endif
mtklein5b64dab2016-06-09 08:59:48 -0700289 fflush(stdout);
290
Mike Klein3cc2d202016-10-24 11:39:43 -0400291 signal(sig, previous_handler[sig]);
292 raise(sig);
mtklein5b64dab2016-06-09 08:59:48 -0700293 }
294
mtklein246ba3a2016-02-23 10:39:36 -0800295 static void setup_crash_handler() {
Mike Klein701a3582018-06-28 14:08:19 +0000296 const int kSignals[] = { SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGINT, SIGSEGV, SIGTERM };
mtklein246ba3a2016-02-23 10:39:36 -0800297 for (int sig : kSignals) {
Mike Klein3cc2d202016-10-24 11:39:43 -0400298 previous_handler[sig] = signal(sig, crash_handler);
mtklein246ba3a2016-02-23 10:39:36 -0800299 }
Derek Sollenbergeredfe3df2017-07-19 15:25:24 -0400300
301 if (FLAGS_ignoreSigInt) {
302 signal(SIGINT, SIG_IGN);
303 }
mtklein246ba3a2016-02-23 10:39:36 -0800304 }
305#endif
306
mtklein748ca3b2015-01-15 10:56:12 -0800307/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
mtklein114c3cd2015-01-15 10:15:02 -0800308
mtklein62bd1a62015-01-27 14:46:26 -0800309struct Gold : public SkString {
mtkleina82f5622015-02-20 12:30:19 -0800310 Gold() : SkString("") {}
mtklein3eed7dd2016-02-24 19:07:07 -0800311 Gold(const SkString& sink, const SkString& src,
312 const SkString& srcOptions, const SkString& name,
313 const SkString& md5)
mtklein62bd1a62015-01-27 14:46:26 -0800314 : SkString("") {
315 this->append(sink);
316 this->append(src);
djsollen54416de2015-04-03 07:24:48 -0700317 this->append(srcOptions);
mtklein62bd1a62015-01-27 14:46:26 -0800318 this->append(name);
319 this->append(md5);
mtklein62bd1a62015-01-27 14:46:26 -0800320 }
mtkleinc8d1dd42015-10-15 12:23:01 -0700321 struct Hash {
322 uint32_t operator()(const Gold& g) const {
323 return SkGoodHash()((const SkString&)g);
324 }
325 };
mtklein62bd1a62015-01-27 14:46:26 -0800326};
mtkleina82f5622015-02-20 12:30:19 -0800327static SkTHashSet<Gold, Gold::Hash> gGold;
mtklein62bd1a62015-01-27 14:46:26 -0800328
329static void add_gold(JsonWriter::BitmapResult r) {
djsollen54416de2015-04-03 07:24:48 -0700330 gGold.add(Gold(r.config, r.sourceType, r.sourceOptions, r.name, r.md5));
mtklein62bd1a62015-01-27 14:46:26 -0800331}
332
333static void gather_gold() {
334 if (!FLAGS_readPath.isEmpty()) {
335 SkString path(FLAGS_readPath[0]);
336 path.append("/dm.json");
337 if (!JsonWriter::ReadJson(path.c_str(), add_gold)) {
338 fail(SkStringPrintf("Couldn't read %s for golden results.", path.c_str()));
339 }
340 }
341}
342
343/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
344
Mike Klein8f11d4d2018-01-24 12:42:55 -0500345#if defined(SK_BUILD_FOR_WIN)
mtklein935f1b12016-03-16 08:37:19 -0700346 static const char* kNewline = "\r\n";
347#else
348 static const char* kNewline = "\n";
349#endif
350
borenet09ed4802015-04-03 14:15:33 -0700351static SkTHashSet<SkString> gUninterestingHashes;
352
353static void gather_uninteresting_hashes() {
354 if (!FLAGS_uninterestingHashesFile.isEmpty()) {
bungeman38d909e2016-08-02 14:40:46 -0700355 sk_sp<SkData> data(SkData::MakeFromFileName(FLAGS_uninterestingHashesFile[0]));
mtkleincc334b32015-09-22 11:43:53 -0700356 if (!data) {
mtkleinc41fd922016-03-08 09:01:39 -0800357 info("WARNING: unable to read uninteresting hashes from %s\n",
358 FLAGS_uninterestingHashesFile[0]);
mtkleincc334b32015-09-22 11:43:53 -0700359 return;
360 }
Mike Klein45dcc0c2017-04-05 18:04:31 -0400361
362 // Copy to a string to make sure SkStrSplit has a terminating \0 to find.
363 SkString contents((const char*)data->data(), data->size());
364
borenet09ed4802015-04-03 14:15:33 -0700365 SkTArray<SkString> hashes;
Mike Klein45dcc0c2017-04-05 18:04:31 -0400366 SkStrSplit(contents.c_str(), kNewline, &hashes);
borenet09ed4802015-04-03 14:15:33 -0700367 for (const SkString& hash : hashes) {
368 gUninterestingHashes.add(hash);
369 }
mtkleinc41fd922016-03-08 09:01:39 -0800370 info("FYI: loaded %d distinct uninteresting hashes from %d lines\n",
371 gUninterestingHashes.count(), hashes.count());
borenet09ed4802015-04-03 14:15:33 -0700372 }
373}
374
375/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
376
Ben Wagner145dbcd2016-11-03 14:40:50 -0400377struct TaggedSrc : public std::unique_ptr<Src> {
mtklein3eed7dd2016-02-24 19:07:07 -0800378 SkString tag;
379 SkString options;
mtkleine0effd62015-07-29 06:37:28 -0700380};
381
Ben Wagner145dbcd2016-11-03 14:40:50 -0400382struct TaggedSink : public std::unique_ptr<Sink> {
kkinnunen3e980c32015-12-23 01:33:00 -0800383 SkString tag;
djsollen54416de2015-04-03 07:24:48 -0700384};
mtklein748ca3b2015-01-15 10:56:12 -0800385
386static const bool kMemcpyOK = true;
387
mtkleine0effd62015-07-29 06:37:28 -0700388static SkTArray<TaggedSrc, kMemcpyOK> gSrcs;
389static SkTArray<TaggedSink, kMemcpyOK> gSinks;
mtklein748ca3b2015-01-15 10:56:12 -0800390
mtklein6393c062015-04-27 08:45:01 -0700391static bool in_shard() {
392 static int N = 0;
393 return N++ % FLAGS_shards == FLAGS_shard;
394}
395
mtklein3eed7dd2016-02-24 19:07:07 -0800396static void push_src(const char* tag, ImplicitString options, Src* s) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400397 std::unique_ptr<Src> src(s);
mtklein6393c062015-04-27 08:45:01 -0700398 if (in_shard() &&
mtklein3eed7dd2016-02-24 19:07:07 -0800399 FLAGS_src.contains(tag) &&
mtklein748ca3b2015-01-15 10:56:12 -0800400 !SkCommandLineFlags::ShouldSkip(FLAGS_match, src->name().c_str())) {
mtkleine0effd62015-07-29 06:37:28 -0700401 TaggedSrc& s = gSrcs.push_back();
mtklein18300a32016-03-16 13:53:35 -0700402 s.reset(src.release());
mtklein748ca3b2015-01-15 10:56:12 -0800403 s.tag = tag;
djsollen54416de2015-04-03 07:24:48 -0700404 s.options = options;
mtklein114c3cd2015-01-15 10:15:02 -0800405 }
mtklein748ca3b2015-01-15 10:56:12 -0800406}
mtklein114c3cd2015-01-15 10:15:02 -0800407
msarett9e707a02015-09-01 14:57:57 -0700408static void push_codec_src(Path path, CodecSrc::Mode mode, CodecSrc::DstColorType dstColorType,
scroggoc5560be2016-02-03 09:42:42 -0800409 SkAlphaType dstAlphaType, float scale) {
scroggoe4499842016-02-25 11:03:47 -0800410 if (FLAGS_simpleCodec) {
scroggo19b91532016-10-24 09:03:26 -0700411 const bool simple = CodecSrc::kCodec_Mode == mode || CodecSrc::kAnimated_Mode == mode;
412 if (!simple || dstColorType != CodecSrc::kGetFromCanvas_DstColorType || scale != 1.0f) {
scroggoe4499842016-02-25 11:03:47 -0800413 // Only decode in the simple case.
414 return;
scroggo19b91532016-10-24 09:03:26 -0700415 }
scroggoe4499842016-02-25 11:03:47 -0800416 }
msarett9e707a02015-09-01 14:57:57 -0700417 SkString folder;
418 switch (mode) {
419 case CodecSrc::kCodec_Mode:
420 folder.append("codec");
421 break;
msarettbb25b532016-01-13 09:31:39 -0800422 case CodecSrc::kCodecZeroInit_Mode:
423 folder.append("codec_zero_init");
424 break;
msarett9e707a02015-09-01 14:57:57 -0700425 case CodecSrc::kScanline_Mode:
426 folder.append("scanline");
427 break;
msarett9e707a02015-09-01 14:57:57 -0700428 case CodecSrc::kStripe_Mode:
429 folder.append("stripe");
430 break;
msarett91c22b22016-02-22 12:27:46 -0800431 case CodecSrc::kCroppedScanline_Mode:
432 folder.append("crop");
433 break;
msarett9e707a02015-09-01 14:57:57 -0700434 case CodecSrc::kSubset_Mode:
msarett36c37962015-09-02 13:20:52 -0700435 folder.append("codec_subset");
msarett9e707a02015-09-01 14:57:57 -0700436 break;
scroggo19b91532016-10-24 09:03:26 -0700437 case CodecSrc::kAnimated_Mode:
438 folder.append("codec_animated");
439 break;
msarett9e707a02015-09-01 14:57:57 -0700440 }
441
442 switch (dstColorType) {
443 case CodecSrc::kGrayscale_Always_DstColorType:
444 folder.append("_kGray8");
445 break;
msarett34e0ec42016-04-22 16:27:24 -0700446 case CodecSrc::kNonNative8888_Always_DstColorType:
447 folder.append("_kNonNative");
448 break;
msarett9e707a02015-09-01 14:57:57 -0700449 default:
450 break;
451 }
452
scroggoc5560be2016-02-03 09:42:42 -0800453 switch (dstAlphaType) {
scroggoc5560be2016-02-03 09:42:42 -0800454 case kPremul_SkAlphaType:
455 folder.append("_premul");
456 break;
457 case kUnpremul_SkAlphaType:
458 folder.append("_unpremul");
459 break;
460 default:
461 break;
462 }
463
msarett9e707a02015-09-01 14:57:57 -0700464 if (1.0f != scale) {
465 folder.appendf("_%.3f", scale);
466 }
467
scroggoc5560be2016-02-03 09:42:42 -0800468 CodecSrc* src = new CodecSrc(path, mode, dstColorType, dstAlphaType, scale);
msarett9e707a02015-09-01 14:57:57 -0700469 push_src("image", folder, src);
470}
471
scroggof8dc9df2016-05-16 09:04:13 -0700472static void push_android_codec_src(Path path, CodecSrc::DstColorType dstColorType,
473 SkAlphaType dstAlphaType, int sampleSize) {
msarett3d9d7a72015-10-21 10:27:10 -0700474 SkString folder;
scroggof8dc9df2016-05-16 09:04:13 -0700475 folder.append("scaled_codec");
msarett3d9d7a72015-10-21 10:27:10 -0700476
477 switch (dstColorType) {
478 case CodecSrc::kGrayscale_Always_DstColorType:
479 folder.append("_kGray8");
480 break;
msarett34e0ec42016-04-22 16:27:24 -0700481 case CodecSrc::kNonNative8888_Always_DstColorType:
482 folder.append("_kNonNative");
483 break;
msarett3d9d7a72015-10-21 10:27:10 -0700484 default:
485 break;
486 }
487
scroggoc5560be2016-02-03 09:42:42 -0800488 switch (dstAlphaType) {
scroggoc5560be2016-02-03 09:42:42 -0800489 case kPremul_SkAlphaType:
490 folder.append("_premul");
491 break;
msarett9e9444c2016-02-03 12:39:10 -0800492 case kUnpremul_SkAlphaType:
493 folder.append("_unpremul");
494 break;
scroggoc5560be2016-02-03 09:42:42 -0800495 default:
496 break;
497 }
498
msarett3d9d7a72015-10-21 10:27:10 -0700499 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800500 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700501 }
502
scroggof8dc9df2016-05-16 09:04:13 -0700503 AndroidCodecSrc* src = new AndroidCodecSrc(path, dstColorType, dstAlphaType, sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700504 push_src("image", folder, src);
505}
506
msarett18976312016-03-09 14:20:58 -0800507static void push_image_gen_src(Path path, ImageGenSrc::Mode mode, SkAlphaType alphaType, bool isGpu)
508{
509 SkString folder;
510 switch (mode) {
511 case ImageGenSrc::kCodec_Mode:
512 folder.append("gen_codec");
513 break;
514 case ImageGenSrc::kPlatform_Mode:
515 folder.append("gen_platform");
516 break;
517 }
518
519 if (isGpu) {
520 folder.append("_gpu");
521 } else {
522 switch (alphaType) {
523 case kOpaque_SkAlphaType:
524 folder.append("_opaque");
525 break;
526 case kPremul_SkAlphaType:
527 folder.append("_premul");
528 break;
529 case kUnpremul_SkAlphaType:
530 folder.append("_unpremul");
531 break;
532 default:
533 break;
534 }
535 }
536
537 ImageGenSrc* src = new ImageGenSrc(path, mode, alphaType, isGpu);
538 push_src("image", folder, src);
539}
540
Leon Scroggins III07418182017-08-15 12:24:02 -0400541static void push_brd_src(Path path, CodecSrc::DstColorType dstColorType, BRDSrc::Mode mode,
542 uint32_t sampleSize) {
543 SkString folder("brd_android_codec");
544 switch (mode) {
545 case BRDSrc::kFullImage_Mode:
546 break;
547 case BRDSrc::kDivisor_Mode:
548 folder.append("_divisor");
549 break;
550 default:
551 SkASSERT(false);
552 return;
553 }
554
555 switch (dstColorType) {
556 case CodecSrc::kGetFromCanvas_DstColorType:
557 break;
558 case CodecSrc::kGrayscale_Always_DstColorType:
559 folder.append("_kGray");
560 break;
561 default:
562 SkASSERT(false);
563 return;
564 }
565
566 if (1 != sampleSize) {
567 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
568 }
569
570 BRDSrc* src = new BRDSrc(path, mode, dstColorType, sampleSize);
571 push_src("image", folder, src);
572}
573
574static void push_brd_srcs(Path path, bool gray) {
575 if (gray) {
576 // Only run grayscale to one sampleSize and Mode. Though interesting
577 // to test grayscale, it should not reveal anything across various
578 // sampleSizes and Modes
579 // Arbitrarily choose Mode and sampleSize.
580 push_brd_src(path, CodecSrc::kGrayscale_Always_DstColorType,
581 BRDSrc::kFullImage_Mode, 2);
582 }
583
584 // Test on a variety of sampleSizes, making sure to include:
585 // - 2, 4, and 8, which are natively supported by jpeg
586 // - multiples of 2 which are not divisible by 4 (analogous for 4)
587 // - larger powers of two, since BRD clients generally use powers of 2
588 // We will only produce output for the larger sizes on large images.
589 const uint32_t sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 24, 32, 64 };
590
591 const BRDSrc::Mode modes[] = { BRDSrc::kFullImage_Mode, BRDSrc::kDivisor_Mode, };
592
593 for (uint32_t sampleSize : sampleSizes) {
594 for (BRDSrc::Mode mode : modes) {
595 push_brd_src(path, CodecSrc::kGetFromCanvas_DstColorType, mode, sampleSize);
596 }
597 }
598}
599
msarett438b2ad2015-04-09 12:43:10 -0700600static void push_codec_srcs(Path path) {
bungeman38d909e2016-08-02 14:40:46 -0700601 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
msarett438b2ad2015-04-09 12:43:10 -0700602 if (!encoded) {
mtkleinc41fd922016-03-08 09:01:39 -0800603 info("Couldn't read %s.", path.c_str());
msarett438b2ad2015-04-09 12:43:10 -0700604 return;
605 }
Mike Reedede7bac2017-07-23 15:30:02 -0400606 std::unique_ptr<SkCodec> codec = SkCodec::MakeFromData(encoded);
halcanary96fcdcc2015-08-27 07:41:13 -0700607 if (nullptr == codec.get()) {
mtkleinc41fd922016-03-08 09:01:39 -0800608 info("Couldn't create codec for %s.", path.c_str());
msarett438b2ad2015-04-09 12:43:10 -0700609 return;
610 }
611
scroggo9366aff2016-05-20 08:38:54 -0700612 // native scaling is only supported by WEBP and JPEG
613 bool supportsNativeScaling = false;
msarett438b2ad2015-04-09 12:43:10 -0700614
msarett91c22b22016-02-22 12:27:46 -0800615 SkTArray<CodecSrc::Mode> nativeModes;
616 nativeModes.push_back(CodecSrc::kCodec_Mode);
617 nativeModes.push_back(CodecSrc::kCodecZeroInit_Mode);
msarett91c22b22016-02-22 12:27:46 -0800618 switch (codec->getEncodedFormat()) {
Hal Canarydb683012016-11-23 08:55:18 -0700619 case SkEncodedImageFormat::kJPEG:
msarett91c22b22016-02-22 12:27:46 -0800620 nativeModes.push_back(CodecSrc::kScanline_Mode);
621 nativeModes.push_back(CodecSrc::kStripe_Mode);
622 nativeModes.push_back(CodecSrc::kCroppedScanline_Mode);
scroggo9366aff2016-05-20 08:38:54 -0700623 supportsNativeScaling = true;
msarett91c22b22016-02-22 12:27:46 -0800624 break;
Hal Canarydb683012016-11-23 08:55:18 -0700625 case SkEncodedImageFormat::kWEBP:
msarett91c22b22016-02-22 12:27:46 -0800626 nativeModes.push_back(CodecSrc::kSubset_Mode);
scroggo9366aff2016-05-20 08:38:54 -0700627 supportsNativeScaling = true;
msarett91c22b22016-02-22 12:27:46 -0800628 break;
Hal Canarydb683012016-11-23 08:55:18 -0700629 case SkEncodedImageFormat::kDNG:
msarettb65e6042016-02-23 05:37:25 -0800630 break;
msarett91c22b22016-02-22 12:27:46 -0800631 default:
632 nativeModes.push_back(CodecSrc::kScanline_Mode);
msarett91c22b22016-02-22 12:27:46 -0800633 break;
634 }
msarett9e707a02015-09-01 14:57:57 -0700635
msarett91c22b22016-02-22 12:27:46 -0800636 SkTArray<CodecSrc::DstColorType> colorTypes;
637 colorTypes.push_back(CodecSrc::kGetFromCanvas_DstColorType);
msarett34e0ec42016-04-22 16:27:24 -0700638 colorTypes.push_back(CodecSrc::kNonNative8888_Always_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700639 switch (codec->getInfo().colorType()) {
640 case kGray_8_SkColorType:
msarett91c22b22016-02-22 12:27:46 -0800641 colorTypes.push_back(CodecSrc::kGrayscale_Always_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700642 break;
643 default:
msarett36c37962015-09-02 13:20:52 -0700644 break;
645 }
msarett9e707a02015-09-01 14:57:57 -0700646
scroggoc5560be2016-02-03 09:42:42 -0800647 SkTArray<SkAlphaType> alphaModes;
648 alphaModes.push_back(kPremul_SkAlphaType);
scroggof2c96a22016-05-20 11:08:27 -0700649 if (codec->getInfo().alphaType() != kOpaque_SkAlphaType) {
650 alphaModes.push_back(kUnpremul_SkAlphaType);
scroggoc5560be2016-02-03 09:42:42 -0800651 }
msarettb714fb02016-01-22 14:46:42 -0800652
653 for (CodecSrc::Mode mode : nativeModes) {
scroggo9366aff2016-05-20 08:38:54 -0700654 for (CodecSrc::DstColorType colorType : colorTypes) {
655 for (SkAlphaType alphaType : alphaModes) {
scroggof2c96a22016-05-20 11:08:27 -0700656 // Only test kCroppedScanline_Mode when the alpha type is premul. The test is
scroggo9366aff2016-05-20 08:38:54 -0700657 // slow and won't be interestingly different with different alpha types.
658 if (CodecSrc::kCroppedScanline_Mode == mode &&
scroggof2c96a22016-05-20 11:08:27 -0700659 kPremul_SkAlphaType != alphaType) {
scroggo9366aff2016-05-20 08:38:54 -0700660 continue;
661 }
msarett91c22b22016-02-22 12:27:46 -0800662
scroggo9366aff2016-05-20 08:38:54 -0700663 push_codec_src(path, mode, colorType, alphaType, 1.0f);
msarettf3dc1882016-04-26 13:06:38 -0700664
scroggo9366aff2016-05-20 08:38:54 -0700665 // Skip kNonNative on different native scales. It won't be interestingly
666 // different.
667 if (supportsNativeScaling &&
668 CodecSrc::kNonNative8888_Always_DstColorType == colorType) {
669 // Native Scales
670 // SkJpegCodec natively supports scaling to the following:
671 for (auto scale : { 0.125f, 0.25f, 0.375f, 0.5f, 0.625f, 0.750f, 0.875f }) {
672 push_codec_src(path, mode, colorType, alphaType, scale);
673 }
scroggoc5560be2016-02-03 09:42:42 -0800674 }
msarett9e707a02015-09-01 14:57:57 -0700675 }
676 }
msarett0a242972015-06-11 14:27:27 -0700677 }
msarett36c37962015-09-02 13:20:52 -0700678
scroggo19b91532016-10-24 09:03:26 -0700679 {
680 std::vector<SkCodec::FrameInfo> frameInfos = codec->getFrameInfo();
681 if (frameInfos.size() > 1) {
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400682 for (auto dstCT : { CodecSrc::kNonNative8888_Always_DstColorType,
683 CodecSrc::kGetFromCanvas_DstColorType }) {
684 for (auto at : { kUnpremul_SkAlphaType, kPremul_SkAlphaType }) {
685 push_codec_src(path, CodecSrc::kAnimated_Mode, dstCT, at, 1.0f);
686 }
687 }
scroggo19b91532016-10-24 09:03:26 -0700688 }
689
690 }
691
scroggoe4499842016-02-25 11:03:47 -0800692 if (FLAGS_simpleCodec) {
693 return;
694 }
695
msarett3d9d7a72015-10-21 10:27:10 -0700696 const int sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
msarett36c37962015-09-02 13:20:52 -0700697
msarett3d9d7a72015-10-21 10:27:10 -0700698 for (int sampleSize : sampleSizes) {
msarett91c22b22016-02-22 12:27:46 -0800699 for (CodecSrc::DstColorType colorType : colorTypes) {
scroggoc5560be2016-02-03 09:42:42 -0800700 for (SkAlphaType alphaType : alphaModes) {
msarettf3dc1882016-04-26 13:06:38 -0700701 // We can exercise all of the kNonNative support code in the swizzler with just a
702 // few sample sizes. Skip the rest.
703 if (CodecSrc::kNonNative8888_Always_DstColorType == colorType && sampleSize > 3) {
704 continue;
705 }
706
scroggof8dc9df2016-05-16 09:04:13 -0700707 push_android_codec_src(path, colorType, alphaType, sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700708 }
msarett36c37962015-09-02 13:20:52 -0700709 }
710 }
msarett18976312016-03-09 14:20:58 -0800711
Leon Scroggins III07418182017-08-15 12:24:02 -0400712 const char* ext = strrchr(path.c_str(), '.');
713 if (ext) {
714 ext++;
msarett18976312016-03-09 14:20:58 -0800715
Leon Scroggins III07418182017-08-15 12:24:02 -0400716 static const char* const rawExts[] = {
717 "arw", "cr2", "dng", "nef", "nrw", "orf", "raf", "rw2", "pef", "srw",
718 "ARW", "CR2", "DNG", "NEF", "NRW", "ORF", "RAF", "RW2", "PEF", "SRW",
719 };
720 for (const char* rawExt : rawExts) {
721 if (0 == strcmp(rawExt, ext)) {
722 // RAW is not supported by image generator (skbug.com/5079) or BRD.
723 return;
724 }
725 }
726
727 static const char* const brdExts[] = {
728 "jpg", "jpeg", "png", "webp",
729 "JPG", "JPEG", "PNG", "WEBP",
730 };
731 for (const char* brdExt : brdExts) {
732 if (0 == strcmp(brdExt, ext)) {
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -0500733 bool gray = codec->getInfo().colorType() == kGray_8_SkColorType;
Leon Scroggins III07418182017-08-15 12:24:02 -0400734 push_brd_srcs(path, gray);
735 break;
736 }
msarett18976312016-03-09 14:20:58 -0800737 }
738 }
739
740 // Push image generator GPU test.
brianosmanc9ced392016-03-24 07:27:43 -0700741 push_image_gen_src(path, ImageGenSrc::kCodec_Mode, codec->getInfo().alphaType(), true);
msarett18976312016-03-09 14:20:58 -0800742
743 // Push image generator CPU tests.
744 for (SkAlphaType alphaType : alphaModes) {
745 push_image_gen_src(path, ImageGenSrc::kCodec_Mode, alphaType, false);
746
747#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
Hal Canarydb683012016-11-23 08:55:18 -0700748 if (SkEncodedImageFormat::kWEBP != codec->getEncodedFormat() &&
749 SkEncodedImageFormat::kWBMP != codec->getEncodedFormat() &&
msarett18976312016-03-09 14:20:58 -0800750 kUnpremul_SkAlphaType != alphaType)
751 {
752 push_image_gen_src(path, ImageGenSrc::kPlatform_Mode, alphaType, false);
753 }
msarettfc0b6d12016-03-17 13:50:17 -0700754#elif defined(SK_BUILD_FOR_WIN)
Hal Canarydb683012016-11-23 08:55:18 -0700755 if (SkEncodedImageFormat::kWEBP != codec->getEncodedFormat() &&
756 SkEncodedImageFormat::kWBMP != codec->getEncodedFormat())
msarettfc0b6d12016-03-17 13:50:17 -0700757 {
758 push_image_gen_src(path, ImageGenSrc::kPlatform_Mode, alphaType, false);
759 }
msarett18976312016-03-09 14:20:58 -0800760#endif
761 }
msarett438b2ad2015-04-09 12:43:10 -0700762}
763
fmalitaa2b9fdf2016-08-03 19:53:36 -0700764template <typename T>
765void gather_file_srcs(const SkCommandLineFlags::StringArray& flags, const char* ext) {
766 for (int i = 0; i < flags.count(); i++) {
767 const char* path = flags[i];
768 if (sk_isdir(path)) {
769 SkOSFile::Iter it(path, ext);
770 for (SkString file; it.next(&file); ) {
771 push_src(ext, "", new T(SkOSPath::Join(path, file.c_str())));
772 }
773 } else {
774 push_src(ext, "", new T(path));
775 }
776 }
777}
778
scroggo86737142016-02-03 12:19:11 -0800779static bool gather_srcs() {
mtklein748ca3b2015-01-15 10:56:12 -0800780 for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) {
djsollen54416de2015-04-03 07:24:48 -0700781 push_src("gm", "", new GMSrc(r->factory()));
mtklein748ca3b2015-01-15 10:56:12 -0800782 }
scroggo86737142016-02-03 12:19:11 -0800783
Robert Phillipse47f0a02018-03-27 17:01:16 -0400784 gather_file_srcs<SKPSrc>(FLAGS_skps, "skp");
fmalitaa2b9fdf2016-08-03 19:53:36 -0700785 gather_file_srcs<MSKPSrc>(FLAGS_mskps, "mskp");
Florin Malita87ccf332018-05-04 12:23:24 -0400786#if defined(SK_ENABLE_SKOTTIE)
Florin Malita54f65c42018-01-16 17:04:30 -0500787 gather_file_srcs<SkottieSrc>(FLAGS_jsons, "json");
Florin Malita124d5af2017-12-31 17:02:26 -0500788#endif
fmalitaa2b9fdf2016-08-03 19:53:36 -0700789#if defined(SK_XML)
790 gather_file_srcs<SVGSrc>(FLAGS_svgs, "svg");
791#endif
halcanary45420a92016-06-02 12:41:14 -0700792
scroggo86737142016-02-03 12:19:11 -0800793 SkTArray<SkString> images;
msarett69deca82016-04-29 09:38:40 -0700794 if (!CollectImages(FLAGS_images, &images)) {
scroggo86737142016-02-03 12:19:11 -0800795 return false;
796 }
797
798 for (auto image : images) {
799 push_codec_srcs(image);
mtklein709d2c32015-01-15 08:30:25 -0800800 }
scroggo86737142016-02-03 12:19:11 -0800801
msarett69deca82016-04-29 09:38:40 -0700802 SkTArray<SkString> colorImages;
803 if (!CollectImages(FLAGS_colorImages, &colorImages)) {
804 return false;
805 }
806
807 for (auto colorImage : colorImages) {
msarett9ce3a542016-07-15 13:54:38 -0700808 ColorCodecSrc* src = new ColorCodecSrc(colorImage, ColorCodecSrc::kBaseline_Mode,
809 kN32_SkColorType);
msarettb8d1aac2016-08-04 12:22:06 -0700810 push_src("colorImage", "color_codec_baseline", src);
msarett888dc162016-05-23 10:21:17 -0700811
msarett9ce3a542016-07-15 13:54:38 -0700812 src = new ColorCodecSrc(colorImage, ColorCodecSrc::kDst_HPZR30w_Mode, kN32_SkColorType);
msarettb8d1aac2016-08-04 12:22:06 -0700813 push_src("colorImage", "color_codec_HPZR30w", src);
msarett9ce3a542016-07-15 13:54:38 -0700814 // TODO (msarett):
815 // Should we test this Dst in F16 mode (even though the Dst gamma is 2.2 instead of sRGB)?
msarett9876ac52016-06-01 14:47:18 -0700816
msarett9ce3a542016-07-15 13:54:38 -0700817 src = new ColorCodecSrc(colorImage, ColorCodecSrc::kDst_sRGB_Mode, kN32_SkColorType);
msarettb8d1aac2016-08-04 12:22:06 -0700818 push_src("colorImage", "color_codec_sRGB_kN32", src);
msarett9ce3a542016-07-15 13:54:38 -0700819 src = new ColorCodecSrc(colorImage, ColorCodecSrc::kDst_sRGB_Mode, kRGBA_F16_SkColorType);
msarettb8d1aac2016-08-04 12:22:06 -0700820 push_src("colorImage", "color_codec_sRGB_kF16", src);
msarett69deca82016-04-29 09:38:40 -0700821 }
822
scroggo86737142016-02-03 12:19:11 -0800823 return true;
mtklein709d2c32015-01-15 08:30:25 -0800824}
825
kkinnunen3e980c32015-12-23 01:33:00 -0800826static void push_sink(const SkCommandLineConfig& config, Sink* s) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400827 std::unique_ptr<Sink> sink(s);
kkinnunen3e980c32015-12-23 01:33:00 -0800828
mtkleine0effd62015-07-29 06:37:28 -0700829 // Try a simple Src as a canary. If it fails, skip this sink.
mtklein748ca3b2015-01-15 10:56:12 -0800830 struct : public Src {
mtkleine0effd62015-07-29 06:37:28 -0700831 Error draw(SkCanvas* c) const override {
832 c->drawRect(SkRect::MakeWH(1,1), SkPaint());
833 return "";
834 }
mtklein36352bf2015-03-25 18:17:31 -0700835 SkISize size() const override { return SkISize::Make(16, 16); }
mtkleine0effd62015-07-29 06:37:28 -0700836 Name name() const override { return "justOneRect"; }
837 } justOneRect;
mtklein114c3cd2015-01-15 10:15:02 -0800838
mtklein748ca3b2015-01-15 10:56:12 -0800839 SkBitmap bitmap;
840 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800841 SkString log;
mtkleine0effd62015-07-29 06:37:28 -0700842 Error err = sink->draw(justOneRect, &bitmap, &stream, &log);
mtklein4089ef72015-03-05 08:40:28 -0800843 if (err.isFatal()) {
mtkleinc41fd922016-03-08 09:01:39 -0800844 info("Could not run %s: %s\n", config.getTag().c_str(), err.c_str());
mtklein05641a52015-04-21 10:49:13 -0700845 exit(1);
mtklein114c3cd2015-01-15 10:15:02 -0800846 }
847
mtkleine0effd62015-07-29 06:37:28 -0700848 TaggedSink& ts = gSinks.push_back();
mtklein18300a32016-03-16 13:53:35 -0700849 ts.reset(sink.release());
kkinnunen3e980c32015-12-23 01:33:00 -0800850 ts.tag = config.getTag();
mtklein748ca3b2015-01-15 10:56:12 -0800851}
852
csmartdalton008b9d82017-02-22 12:00:42 -0700853static Sink* create_sink(const GrContextOptions& grCtxOptions, const SkCommandLineConfig* config) {
Brian Osmanc7ad40f2018-05-31 14:27:17 -0400854 if (FLAGS_gpu) {
kkinnunen3e980c32015-12-23 01:33:00 -0800855 if (const SkCommandLineConfigGpu* gpuConfig = config->asConfigGpu()) {
bsalomon85b4b532016-04-05 11:06:27 -0700856 GrContextFactory::ContextType contextType = gpuConfig->getContextType();
csmartdaltone812d492017-02-21 12:36:05 -0700857 GrContextFactory::ContextOverrides contextOverrides = gpuConfig->getContextOverrides();
csmartdalton008b9d82017-02-22 12:00:42 -0700858 GrContextFactory testFactory(grCtxOptions);
csmartdaltone812d492017-02-21 12:36:05 -0700859 if (!testFactory.get(contextType, contextOverrides)) {
mtkleinc41fd922016-03-08 09:01:39 -0800860 info("WARNING: can not create GPU context for config '%s'. "
861 "GM tests will be skipped.\n", gpuConfig->getTag().c_str());
kkinnunen3e980c32015-12-23 01:33:00 -0800862 return nullptr;
863 }
Brian Osmanf9810662017-08-30 10:02:10 -0400864 if (gpuConfig->getTestThreading()) {
Brian Salomonf865b052018-03-09 09:01:53 -0500865 return new GPUThreadTestingSink(
866 contextType, contextOverrides, gpuConfig->getSurfType(),
867 gpuConfig->getSamples(), gpuConfig->getUseDIText(),
868 gpuConfig->getColorType(), gpuConfig->getAlphaType(),
869 sk_ref_sp(gpuConfig->getColorSpace()), FLAGS_gpu_threading, grCtxOptions);
Brian Osmanf9810662017-08-30 10:02:10 -0400870 } else {
Brian Salomonf865b052018-03-09 09:01:53 -0500871 return new GPUSink(contextType, contextOverrides, gpuConfig->getSurfType(),
872 gpuConfig->getSamples(), gpuConfig->getUseDIText(),
873 gpuConfig->getColorType(), gpuConfig->getAlphaType(),
874 sk_ref_sp(gpuConfig->getColorSpace()), FLAGS_gpu_threading,
875 grCtxOptions);
Brian Osmanf9810662017-08-30 10:02:10 -0400876 }
kkinnunen3e980c32015-12-23 01:33:00 -0800877 }
878 }
Bryce Thomas95a7b762018-03-02 13:54:21 -0800879 if (const SkCommandLineConfigSvg* svgConfig = config->asConfigSvg()) {
880 int pageIndex = svgConfig->getPageIndex();
881 return new SVGSink(pageIndex);
882 }
kkinnunen3e980c32015-12-23 01:33:00 -0800883
884#define SINK(t, sink, ...) if (config->getBackend().equals(t)) { return new sink(__VA_ARGS__); }
885
mtklein748ca3b2015-01-15 10:56:12 -0800886 if (FLAGS_cpu) {
Mike Klein515bda62018-01-09 11:21:58 -0500887 SINK("g8", RasterSink, kGray_8_SkColorType);
Matt Sarettd2228302017-03-02 08:53:46 -0500888 SINK("565", RasterSink, kRGB_565_SkColorType);
Mike Klein5340b3f2018-01-24 13:42:05 -0500889 SINK("4444", RasterSink, kARGB_4444_SkColorType);
Matt Sarettd2228302017-03-02 08:53:46 -0500890 SINK("8888", RasterSink, kN32_SkColorType);
Mike Kleinac568a92018-01-25 09:09:32 -0500891 SINK("rgba", RasterSink, kRGBA_8888_SkColorType);
892 SINK("bgra", RasterSink, kBGRA_8888_SkColorType);
893 SINK("rgbx", RasterSink, kRGB_888x_SkColorType);
894 SINK("1010102", RasterSink, kRGBA_1010102_SkColorType);
895 SINK("101010x", RasterSink, kRGB_101010x_SkColorType);
Yuqian Lib8b62532018-02-23 14:13:36 +0800896 SINK("t8888", ThreadedSink, kN32_SkColorType);
Hal Canaryc7d295e2017-07-20 15:36:00 -0400897 SINK("pdf", PDFSink, false, SK_ScalarDefaultRasterDPI);
Matt Sarettd2228302017-03-02 08:53:46 -0500898 SINK("skp", SKPSink);
899 SINK("pipe", PipeSink);
900 SINK("svg", SVGSink);
901 SINK("null", NullSink);
902 SINK("xps", XPSSink);
Hal Canaryc7d295e2017-07-20 15:36:00 -0400903 SINK("pdfa", PDFSink, true, SK_ScalarDefaultRasterDPI);
904 SINK("pdf300", PDFSink, false, 300);
Hal Canary85c7fe82016-10-25 10:33:27 -0400905 SINK("jsdebug", DebugSink);
Mike Klein0e4041f2018-06-19 16:00:40 -0400906
907 // Configs relevant to color management testing (and 8888 for reference).
908
909 // 'narrow' has a gamut narrower than sRGB, and different transfer function.
910 SkMatrix44 narrow_gamut(SkMatrix44::kUninitialized_Constructor);
911 narrow_gamut.set3x3RowMajorf(gNarrow_toXYZD50);
912
913 auto narrow = SkColorSpace::MakeRGB(k2Dot2Curve_SkGammaNamed, narrow_gamut),
914 srgb = SkColorSpace::MakeSRGB(),
915 srgbLinear = SkColorSpace::MakeSRGBLinear();
916
917 SINK( "f16", RasterSink, kRGBA_F16_SkColorType, srgbLinear);
918 SINK( "srgb", RasterSink, kRGBA_8888_SkColorType, srgb );
919 SINK( "esrgb", RasterSink, kRGBA_F16_SkColorType, srgb );
920 SINK( "narrow", RasterSink, kRGBA_8888_SkColorType, narrow );
921 SINK("enarrow", RasterSink, kRGBA_F16_SkColorType, narrow );
Mike Klein37854712018-06-26 11:43:06 -0400922
923 SINK( "f32", RasterSink, kRGBA_F32_SkColorType, srgbLinear);
mtklein748ca3b2015-01-15 10:56:12 -0800924 }
925#undef SINK
halcanary96fcdcc2015-08-27 07:41:13 -0700926 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800927}
928
Mike Kleinb7ea3da2017-10-31 17:42:08 +0000929static sk_sp<SkColorSpace> rgb_to_gbr() {
Brian Osman36703d92017-12-12 14:09:31 -0500930 return SkColorSpace::MakeSRGB()->makeColorSpin();
Mike Klein919cc452017-03-18 15:36:52 +0000931}
932
kkinnunen3e980c32015-12-23 01:33:00 -0800933static Sink* create_via(const SkString& tag, Sink* wrapped) {
934#define VIA(t, via, ...) if (tag.equals(t)) { return new via(__VA_ARGS__); }
Mike Kleinb7ea3da2017-10-31 17:42:08 +0000935 VIA("gbr", ViaCSXform, wrapped, rgb_to_gbr(), true);
mtklein9c5052f2016-08-06 12:51:51 -0700936 VIA("lite", ViaLite, wrapped);
reed54dc4872016-09-13 08:09:45 -0700937 VIA("pipe", ViaPipe, wrapped);
Mike Reedbae888e2017-02-18 16:50:45 -0500938#ifdef TEST_VIA_SVG
Mike Reedf67c4592017-02-17 17:06:11 -0500939 VIA("svg", ViaSVG, wrapped);
Mike Reedbae888e2017-02-18 16:50:45 -0500940#endif
halcanary96fcdcc2015-08-27 07:41:13 -0700941 VIA("serialize", ViaSerialization, wrapped);
mtklein4a34ecb2016-01-08 10:19:35 -0800942 VIA("pic", ViaPicture, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700943 VIA("tiles", ViaTiles, 256, 256, nullptr, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800944 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800945
Robert Phillips33f02ed2018-03-27 08:06:57 -0400946 VIA("ddl", ViaDDL, 3, wrapped);
947
mtkleind603b222015-02-17 11:13:33 -0800948 if (FLAGS_matrix.count() == 4) {
mtklein748ca3b2015-01-15 10:56:12 -0800949 SkMatrix m;
mtkleind603b222015-02-17 11:13:33 -0800950 m.reset();
951 m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
952 m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
953 m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
954 m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
955 VIA("matrix", ViaMatrix, m, wrapped);
956 VIA("upright", ViaUpright, m, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800957 }
tomhudson64de1e12015-03-05 08:01:07 -0800958
mtklein748ca3b2015-01-15 10:56:12 -0800959#undef VIA
halcanary96fcdcc2015-08-27 07:41:13 -0700960 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800961}
962
Ben Wagnerf28d4722017-08-11 17:33:22 -0400963static bool gather_sinks(const GrContextOptions& grCtxOptions, bool defaultConfigs) {
kkinnunen3e980c32015-12-23 01:33:00 -0800964 SkCommandLineConfigArray configs;
965 ParseConfigs(FLAGS_config, &configs);
966 for (int i = 0; i < configs.count(); i++) {
967 const SkCommandLineConfig& config = *configs[i];
csmartdalton008b9d82017-02-22 12:00:42 -0700968 Sink* sink = create_sink(grCtxOptions, &config);
kkinnunen3e980c32015-12-23 01:33:00 -0800969 if (sink == nullptr) {
mtkleinc41fd922016-03-08 09:01:39 -0800970 info("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
971 config.getTag().c_str());
kkinnunen3e980c32015-12-23 01:33:00 -0800972 continue;
973 }
mtklein748ca3b2015-01-15 10:56:12 -0800974
kkinnunen3e980c32015-12-23 01:33:00 -0800975 const SkTArray<SkString>& parts = config.getViaParts();
976 for (int j = parts.count(); j-- > 0;) {
977 const SkString& part = parts[j];
978 Sink* next = create_via(part, sink);
halcanary96fcdcc2015-08-27 07:41:13 -0700979 if (next == nullptr) {
mtkleinc41fd922016-03-08 09:01:39 -0800980 info("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
981 part.c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800982 delete sink;
halcanary96fcdcc2015-08-27 07:41:13 -0700983 sink = nullptr;
mtklein748ca3b2015-01-15 10:56:12 -0800984 break;
985 }
986 sink = next;
987 }
988 if (sink) {
989 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800990 }
991 }
brianosman05c987c2016-05-09 13:42:16 -0700992
993 // If no configs were requested (just running tests, perhaps?), then we're okay.
Ben Wagnerf28d4722017-08-11 17:33:22 -0400994 if (configs.count() == 0 ||
995 // If we're using the default configs, we're okay.
996 defaultConfigs ||
Ben Wagner32fa5102017-08-10 21:25:55 -0400997 // If we've been told to ignore undefined flags, we're okay.
998 FLAGS_undefok ||
999 // Otherwise, make sure that all specified configs have become sinks.
1000 configs.count() == gSinks.count()) {
1001 return true;
1002 }
1003 info("Invalid --config. Use --undefok to bypass this warning.\n");
1004 return false;
mtklein114c3cd2015-01-15 10:15:02 -08001005}
mtklein709d2c32015-01-15 08:30:25 -08001006
mtkleina5114d72015-08-24 13:27:01 -07001007static bool dump_png(SkBitmap bitmap, const char* path, const char* md5) {
Mike Klein13b6afd2017-06-12 12:45:41 -04001008 SkPixmap pm;
1009 if (!bitmap.peekPixels(&pm)) {
1010 return false; // Ought to never happen... we're already read-back at this point.
mtkleina5114d72015-08-24 13:27:01 -07001011 }
Mike Klein13b6afd2017-06-12 12:45:41 -04001012 SkFILEWStream dst{path};
mtkleina5114d72015-08-24 13:27:01 -07001013
mtkleind69ece62015-09-10 10:37:44 -07001014 SkString description;
1015 description.append("Key: ");
1016 for (int i = 0; i < FLAGS_key.count(); i++) {
1017 description.appendf("%s ", FLAGS_key[i]);
1018 }
1019 description.append("Properties: ");
1020 for (int i = 0; i < FLAGS_properties.count(); i++) {
1021 description.appendf("%s ", FLAGS_properties[i]);
1022 }
1023 description.appendf("MD5: %s", md5);
1024
Mike Klein13b6afd2017-06-12 12:45:41 -04001025 const char* comments[] = {
1026 "Author", "DM dump_png()",
1027 "Description", description.c_str(),
1028 };
1029 size_t lengths[] = {
1030 strlen(comments[0])+1, strlen(comments[1])+1,
1031 strlen(comments[2])+1, strlen(comments[3])+1,
1032 };
mtkleina5114d72015-08-24 13:27:01 -07001033
Mike Klein13b6afd2017-06-12 12:45:41 -04001034 SkPngEncoder::Options options;
1035 options.fComments = SkDataTable::MakeCopyArrays((const void**)comments, lengths, 4);
1036 options.fFilterFlags = SkPngEncoder::FilterFlag::kNone;
1037 options.fZLibLevel = 1;
1038 options.fUnpremulBehavior = pm.colorSpace() ? SkTransferFunctionBehavior::kRespect
1039 : SkTransferFunctionBehavior::kIgnore;
1040 return SkPngEncoder::Encode(&dst, pm, options);
mtkleina5114d72015-08-24 13:27:01 -07001041}
1042
mtkleina2ef6422015-01-15 13:44:22 -08001043static bool match(const char* needle, const char* haystack) {
Chris Daltonecf78ac2017-08-15 15:43:08 -06001044 if ('~' == needle[0]) {
1045 return !match(needle + 1, haystack);
1046 }
1047 if (0 == strcmp("_", needle)) {
1048 return true;
1049 }
1050 return nullptr != strstr(haystack, needle);
mtkleina2ef6422015-01-15 13:44:22 -08001051}
1052
mtklein3eed7dd2016-02-24 19:07:07 -08001053static bool is_blacklisted(const char* sink, const char* src,
1054 const char* srcOptions, const char* name) {
mtklein0d243ff2015-04-03 07:51:00 -07001055 for (int i = 0; i < FLAGS_blacklist.count() - 3; i += 4) {
mtkleina2ef6422015-01-15 13:44:22 -08001056 if (match(FLAGS_blacklist[i+0], sink) &&
djsollen54416de2015-04-03 07:24:48 -07001057 match(FLAGS_blacklist[i+1], src) &&
1058 match(FLAGS_blacklist[i+2], srcOptions) &&
1059 match(FLAGS_blacklist[i+3], name)) {
mtklein3eed7dd2016-02-24 19:07:07 -08001060 return true;
mtkleina2ef6422015-01-15 13:44:22 -08001061 }
1062 }
mtklein3eed7dd2016-02-24 19:07:07 -08001063 return false;
mtkleina2ef6422015-01-15 13:44:22 -08001064}
1065
mtkleincd50bca2016-01-05 06:20:20 -08001066// Even when a Task Sink reports to be non-threadsafe (e.g. GPU), we know things like
1067// .png encoding are definitely thread safe. This lets us offload that work to CPU threads.
1068static SkTaskGroup gDefinitelyThreadSafeWork;
1069
mtklein748ca3b2015-01-15 10:56:12 -08001070// The finest-grained unit of work we can run: draw a single Src into a single Sink,
1071// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
1072struct Task {
mtkleine0effd62015-07-29 06:37:28 -07001073 Task(const TaggedSrc& src, const TaggedSink& sink) : src(src), sink(sink) {}
1074 const TaggedSrc& src;
1075 const TaggedSink& sink;
mtklein748ca3b2015-01-15 10:56:12 -08001076
mtklein21eaf3b2016-02-08 12:39:59 -08001077 static void Run(const Task& task) {
1078 SkString name = task.src->name();
mtkleine0effd62015-07-29 06:37:28 -07001079
mtkleinb9eb4ac2015-02-02 18:26:03 -08001080 SkString log;
mtklein3eed7dd2016-02-24 19:07:07 -08001081 if (!FLAGS_dryRun) {
mtklein748ca3b2015-01-15 10:56:12 -08001082 SkBitmap bitmap;
1083 SkDynamicMemoryWStream stream;
mtklein3eed7dd2016-02-24 19:07:07 -08001084 start(task.sink.tag.c_str(), task.src.tag.c_str(),
1085 task.src.options.c_str(), name.c_str());
mtklein21eaf3b2016-02-08 12:39:59 -08001086 Error err = task.sink->draw(*task.src, &bitmap, &stream, &log);
mtkleinb3b13b72016-03-07 13:20:52 -08001087 if (!log.isEmpty()) {
mtkleinc41fd922016-03-08 09:01:39 -08001088 info("%s %s %s %s:\n%s\n", task.sink.tag.c_str()
1089 , task.src.tag.c_str()
1090 , task.src.options.c_str()
1091 , name.c_str()
1092 , log.c_str());
mtkleinb3b13b72016-03-07 13:20:52 -08001093 }
mtklein748ca3b2015-01-15 10:56:12 -08001094 if (!err.isEmpty()) {
mtklein4089ef72015-03-05 08:40:28 -08001095 if (err.isFatal()) {
djsollen54416de2015-04-03 07:24:48 -07001096 fail(SkStringPrintf("%s %s %s %s: %s",
mtklein21eaf3b2016-02-08 12:39:59 -08001097 task.sink.tag.c_str(),
1098 task.src.tag.c_str(),
1099 task.src.options.c_str(),
mtklein4089ef72015-03-05 08:40:28 -08001100 name.c_str(),
1101 err.c_str()));
mtkleinb37cb412015-03-18 05:27:14 -07001102 } else {
mtklein3eed7dd2016-02-24 19:07:07 -08001103 done(task.sink.tag.c_str(), task.src.tag.c_str(),
1104 task.src.options.c_str(), name.c_str());
mtkleinba6ada72016-01-21 09:39:35 -08001105 return;
mtklein4089ef72015-03-05 08:40:28 -08001106 }
mtklein748ca3b2015-01-15 10:56:12 -08001107 }
mtklein62bd1a62015-01-27 14:46:26 -08001108
mtkleincd50bca2016-01-05 06:20:20 -08001109 // We're likely switching threads here, so we must capture by value, [=] or [foo,bar].
Hal Canary0b833192017-03-14 14:35:02 -04001110 SkStreamAsset* data = stream.detachAsStream().release();
mtkleincd50bca2016-01-05 06:20:20 -08001111 gDefinitelyThreadSafeWork.add([task,name,bitmap,data]{
Ben Wagner145dbcd2016-11-03 14:40:50 -04001112 std::unique_ptr<SkStreamAsset> ownedData(data);
mtkleincd50bca2016-01-05 06:20:20 -08001113
mtkleincd50bca2016-01-05 06:20:20 -08001114 SkString md5;
1115 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
1116 SkMD5 hash;
1117 if (data->getLength()) {
1118 hash.writeStream(data, data->getLength());
1119 data->rewind();
mtklein38408462015-07-08 07:25:27 -07001120 } else {
mtkleincd50bca2016-01-05 06:20:20 -08001121 // If we're BGRA (Linux, Windows), swizzle over to RGBA (Mac, Android).
1122 // This helps eliminate multiple 0-pixel-diff hashes on gold.skia.org.
1123 // (Android's general slow speed breaks the tie arbitrarily in RGBA's favor.)
1124 // We might consider promoting 565 to RGBA too.
1125 if (bitmap.colorType() == kBGRA_8888_SkColorType) {
1126 SkBitmap swizzle;
Matt Sarett68b8e3d2017-04-28 11:15:22 -04001127 SkAssertResult(sk_tool_utils::copy_to(&swizzle, kRGBA_8888_SkColorType,
1128 bitmap));
Mike Reedf0ffb892017-10-03 14:47:21 -04001129 hash.write(swizzle.getPixels(), swizzle.computeByteSize());
mtkleincd50bca2016-01-05 06:20:20 -08001130 } else {
Mike Reedf0ffb892017-10-03 14:47:21 -04001131 hash.write(bitmap.getPixels(), bitmap.computeByteSize());
mtkleincd50bca2016-01-05 06:20:20 -08001132 }
1133 }
1134 SkMD5::Digest digest;
1135 hash.finish(digest);
1136 for (int i = 0; i < 16; i++) {
1137 md5.appendf("%02x", digest.data[i]);
mtklein38408462015-07-08 07:25:27 -07001138 }
mtklein62bd1a62015-01-27 14:46:26 -08001139 }
mtklein62bd1a62015-01-27 14:46:26 -08001140
mtkleincd50bca2016-01-05 06:20:20 -08001141 if (!FLAGS_readPath.isEmpty() &&
mtklein3eed7dd2016-02-24 19:07:07 -08001142 !gGold.contains(Gold(task.sink.tag, task.src.tag,
1143 task.src.options, name, md5))) {
mtkleincd50bca2016-01-05 06:20:20 -08001144 fail(SkStringPrintf("%s not found for %s %s %s %s in %s",
1145 md5.c_str(),
mtklein21eaf3b2016-02-08 12:39:59 -08001146 task.sink.tag.c_str(),
1147 task.src.tag.c_str(),
1148 task.src.options.c_str(),
mtkleincd50bca2016-01-05 06:20:20 -08001149 name.c_str(),
1150 FLAGS_readPath[0]));
mtklein748ca3b2015-01-15 10:56:12 -08001151 }
mtkleincd50bca2016-01-05 06:20:20 -08001152
1153 if (!FLAGS_writePath.isEmpty()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001154 const char* ext = task.sink->fileExtension();
Brian Osmanf1942de2017-08-30 14:50:22 -04001155 if (ext && !FLAGS_dont_write.contains(ext)) {
Mike Klein97d6a7a2017-07-24 10:37:19 -04001156 if (data->getLength()) {
1157 WriteToDisk(task, md5, ext, data, data->getLength(), nullptr);
1158 SkASSERT(bitmap.drawsNothing());
1159 } else if (!bitmap.drawsNothing()) {
1160 WriteToDisk(task, md5, ext, nullptr, 0, &bitmap);
1161 }
mtkleincd50bca2016-01-05 06:20:20 -08001162 }
1163 }
1164 });
mtklein748ca3b2015-01-15 10:56:12 -08001165 }
mtklein3eed7dd2016-02-24 19:07:07 -08001166 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 -08001167 }
1168
1169 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -08001170 SkString md5,
1171 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -08001172 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -08001173 const SkBitmap* bitmap) {
mtklein409d4702016-02-29 07:38:01 -08001174 bool gammaCorrect = false;
1175 if (bitmap) {
Brian Osman7039f742016-11-01 15:56:16 -04001176 gammaCorrect = SkToBool(bitmap->info().colorSpace());
mtklein409d4702016-02-29 07:38:01 -08001177 }
1178
mtklein748ca3b2015-01-15 10:56:12 -08001179 JsonWriter::BitmapResult result;
djsollen54416de2015-04-03 07:24:48 -07001180 result.name = task.src->name();
mtklein3eed7dd2016-02-24 19:07:07 -08001181 result.config = task.sink.tag;
djsollen54416de2015-04-03 07:24:48 -07001182 result.sourceType = task.src.tag;
1183 result.sourceOptions = task.src.options;
1184 result.ext = ext;
mtklein409d4702016-02-29 07:38:01 -08001185 result.gammaCorrect = gammaCorrect;
djsollen54416de2015-04-03 07:24:48 -07001186 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -08001187 JsonWriter::AddBitmapResult(result);
1188
mtkleinb0531a72015-04-07 13:38:48 -07001189 // If an MD5 is uninteresting, we want it noted in the JSON file,
1190 // but don't want to dump it out as a .png (or whatever ext is).
1191 if (gUninterestingHashes.contains(md5)) {
1192 return;
1193 }
1194
mtklein748ca3b2015-01-15 10:56:12 -08001195 const char* dir = FLAGS_writePath[0];
1196 if (0 == strcmp(dir, "@")) { // Needed for iOS.
1197 dir = FLAGS_resourcePath[0];
1198 }
1199 sk_mkdir(dir);
1200
1201 SkString path;
1202 if (FLAGS_nameByHash) {
1203 path = SkOSPath::Join(dir, result.md5.c_str());
1204 path.append(".");
1205 path.append(ext);
1206 if (sk_exists(path.c_str())) {
1207 return; // Content-addressed. If it exists already, we're done.
1208 }
1209 } else {
kkinnunen3e980c32015-12-23 01:33:00 -08001210 path = SkOSPath::Join(dir, task.sink.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001211 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001212 path = SkOSPath::Join(path.c_str(), task.src.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001213 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001214 if (strcmp(task.src.options.c_str(), "") != 0) {
1215 path = SkOSPath::Join(path.c_str(), task.src.options.c_str());
djsollen54416de2015-04-03 07:24:48 -07001216 sk_mkdir(path.c_str());
1217 }
mtklein748ca3b2015-01-15 10:56:12 -08001218 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
1219 path.append(".");
1220 path.append(ext);
1221 }
1222
mtklein748ca3b2015-01-15 10:56:12 -08001223 if (bitmap) {
mtkleina5114d72015-08-24 13:27:01 -07001224 if (!dump_png(*bitmap, path.c_str(), result.md5.c_str())) {
mtklein748ca3b2015-01-15 10:56:12 -08001225 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
1226 return;
1227 }
1228 } else {
mtkleina5114d72015-08-24 13:27:01 -07001229 SkFILEWStream file(path.c_str());
1230 if (!file.isValid()) {
1231 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
1232 return;
1233 }
mtklein748ca3b2015-01-15 10:56:12 -08001234 if (!file.writeStream(data, len)) {
1235 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
1236 return;
1237 }
1238 }
1239 }
1240};
1241
mtklein748ca3b2015-01-15 10:56:12 -08001242/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1243
1244// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
1245
mtklein21eaf3b2016-02-08 12:39:59 -08001246static SkTDArray<skiatest::Test> gParallelTests, gSerialTests;
mtklein748ca3b2015-01-15 10:56:12 -08001247
1248static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -08001249 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -08001250 return;
1251 }
mtklein6393c062015-04-27 08:45:01 -07001252 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r; r = r->next()) {
1253 if (!in_shard()) {
1254 continue;
1255 }
halcanary87f3ba42015-01-20 09:30:20 -08001256 // Despite its name, factory() is returning a reference to
1257 // link-time static const POD data.
1258 const skiatest::Test& test = r->factory();
1259 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -08001260 continue;
1261 }
Brian Osmanc7ad40f2018-05-31 14:27:17 -04001262 if (test.needsGpu && FLAGS_gpu) {
mtklein21eaf3b2016-02-08 12:39:59 -08001263 (FLAGS_gpu_threading ? gParallelTests : gSerialTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -08001264 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein21eaf3b2016-02-08 12:39:59 -08001265 gParallelTests.push(test);
mtklein82d28432015-01-15 12:46:02 -08001266 }
mtklein748ca3b2015-01-15 10:56:12 -08001267 }
1268}
1269
csmartdalton008b9d82017-02-22 12:00:42 -07001270static void run_test(skiatest::Test test, const GrContextOptions& grCtxOptions) {
halcanary87f3ba42015-01-20 09:30:20 -08001271 struct : public skiatest::Reporter {
mtklein36352bf2015-03-25 18:17:31 -07001272 void reportFailed(const skiatest::Failure& failure) override {
halcanary87f3ba42015-01-20 09:30:20 -08001273 fail(failure.toString());
1274 JsonWriter::AddTestFailure(failure);
1275 }
mtklein36352bf2015-03-25 18:17:31 -07001276 bool allowExtendedTest() const override {
halcanary87f3ba42015-01-20 09:30:20 -08001277 return FLAGS_pathOpsExtended;
1278 }
mtklein36352bf2015-03-25 18:17:31 -07001279 bool verbose() const override { return FLAGS_veryVerbose; }
halcanary87f3ba42015-01-20 09:30:20 -08001280 } reporter;
djsollen824996a2015-06-12 12:06:22 -07001281
mtklein3eed7dd2016-02-24 19:07:07 -08001282 if (!FLAGS_dryRun && !is_blacklisted("_", "tests", "_", test.name)) {
Robert Phillipsec325342017-10-30 18:02:48 +00001283 GrContextOptions options = grCtxOptions;
1284 test.modifyGrContextOptions(&options);
1285
mtklein21eaf3b2016-02-08 12:39:59 -08001286 start("unit", "test", "", test.name);
Brian Salomondcfca432017-11-15 15:48:03 -05001287 test.run(&reporter, options);
mtklein748ca3b2015-01-15 10:56:12 -08001288 }
mtklein3eed7dd2016-02-24 19:07:07 -08001289 done("unit", "test", "", test.name);
mtklein748ca3b2015-01-15 10:56:12 -08001290}
1291
1292/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1293
caryclark83ca6282015-06-10 09:31:09 -07001294#define PORTABLE_FONT_PREFIX "Toy Liberation "
1295
mbocee6a9912016-05-31 11:42:36 -07001296static sk_sp<SkTypeface> create_from_name(const char familyName[], SkFontStyle style) {
caryclark83ca6282015-06-10 09:31:09 -07001297 if (familyName && strlen(familyName) > sizeof(PORTABLE_FONT_PREFIX)
1298 && !strncmp(familyName, PORTABLE_FONT_PREFIX, sizeof(PORTABLE_FONT_PREFIX) - 1)) {
caryclark1818acb2015-07-24 12:09:25 -07001299 return sk_tool_utils::create_portable_typeface(familyName, style);
caryclark83ca6282015-06-10 09:31:09 -07001300 }
halcanary96fcdcc2015-08-27 07:41:13 -07001301 return nullptr;
caryclark83ca6282015-06-10 09:31:09 -07001302}
1303
1304#undef PORTABLE_FONT_PREFIX
1305
mbocee6a9912016-05-31 11:42:36 -07001306extern sk_sp<SkTypeface> (*gCreateTypefaceDelegate)(const char [], SkFontStyle );
caryclark83ca6282015-06-10 09:31:09 -07001307
Mike Kleinbe28ee22017-02-06 12:46:20 -05001308int main(int argc, char** argv) {
Dongwon Kang0c7861f2018-02-16 10:55:21 -08001309#if defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) && defined(SK_HAS_HEIF_LIBRARY)
1310 android::ProcessState::self()->startThreadPool();
1311#endif
Mike Kleinbe28ee22017-02-06 12:46:20 -05001312 SkCommandLineFlags::Parse(argc, argv);
Brian Osman53136aa2017-07-20 15:43:35 -04001313
Mike Klein31868212017-11-06 12:02:47 -05001314 if (!FLAGS_nativeFonts) {
Ben Wagner483c7722018-02-20 17:06:07 -05001315 gSkFontMgr_DefaultFactory = &sk_tool_utils::MakePortableFontMgr;
Mike Klein31868212017-11-06 12:02:47 -05001316 }
1317
Mike Klein0c5fdcb2017-11-13 12:40:46 -05001318#if defined(SK_BUILD_FOR_WIN)
1319 if (FLAGS_gdi) {
Hal Canaryccafca02017-11-14 10:34:48 -05001320 gSkFontMgr_DefaultFactory = &SkFontMgr_New_GDI;
Mike Klein0c5fdcb2017-11-13 12:40:46 -05001321 }
1322#endif
1323
Brian Osmanbc8150f2017-07-24 11:38:01 -04001324 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -04001325
Mike Klein6613cc52017-12-19 09:09:33 -05001326#if !defined(SK_BUILD_FOR_GOOGLE3) && defined(SK_BUILD_FOR_IOS)
Mike Kleinadacaef2017-02-06 09:26:14 -05001327 cd_Documents();
1328#endif
bsalomon71de3532016-04-26 14:27:21 -07001329 setbuf(stdout, nullptr);
mtklein246ba3a2016-02-23 10:39:36 -08001330 setup_crash_handler();
mtkleinafae30a2016-02-24 12:28:32 -08001331
Yuqian Liba62b4a2016-12-14 13:46:53 -05001332 gSkUseAnalyticAA = FLAGS_analyticAA;
Yuqian Lidf60e362017-07-25 11:26:31 -04001333 gSkUseDeltaAA = FLAGS_deltaAA;
liyuqian38911a72016-10-04 11:23:22 -07001334
Yuqian Li550148b2017-01-13 10:13:13 -05001335 if (FLAGS_forceAnalyticAA) {
1336 gSkForceAnalyticAA = true;
1337 }
Yuqian Lia81b6262017-09-06 17:10:05 -04001338 if (FLAGS_forceDeltaAA) {
1339 gSkForceDeltaAA = true;
1340 }
Mike Reed7c26ca72017-07-05 15:45:52 -04001341 if (FLAGS_forceRasterPipeline) {
1342 gSkForceRasterPipelineBlitter = true;
1343 }
Yuqian Li550148b2017-01-13 10:13:13 -05001344
Mike Kleine9f187b2017-06-26 13:23:12 -04001345 // The bots like having a verbose.log to upload, so always touch the file even if --verbose.
1346 if (!FLAGS_writePath.isEmpty()) {
mtklein51c8cfc2016-03-11 12:59:09 -08001347 sk_mkdir(FLAGS_writePath[0]);
mtkleina1ce2162016-07-22 12:23:46 -07001348 gVLog = fopen(SkOSPath::Join(FLAGS_writePath[0], "verbose.log").c_str(), "w");
mtkleinc41fd922016-03-08 09:01:39 -08001349 }
Mike Kleine9f187b2017-06-26 13:23:12 -04001350 if (FLAGS_verbose) {
1351 gVLog = stderr;
1352 }
mtkleinc41fd922016-03-08 09:01:39 -08001353
csmartdalton008b9d82017-02-22 12:00:42 -07001354 GrContextOptions grCtxOptions;
Chris Dalton040238b2017-12-18 14:22:34 -07001355 SetCtxOptionsFromCommonFlags(&grCtxOptions);
csmartdalton008b9d82017-02-22 12:00:42 -07001356
mtklein5286f022016-01-22 08:18:14 -08001357 JsonWriter::DumpJson(); // It's handy for the bots to assume this is ~never missing.
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +00001358 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -07001359 SkTaskGroup::Enabler enabled(FLAGS_threads);
caryclark83ca6282015-06-10 09:31:09 -07001360 gCreateTypefaceDelegate = &create_from_name;
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +00001361
Mike Reed0933bc92017-12-09 01:27:41 +00001362 if (nullptr == GetResourceAsData("images/color_wheel.png")) {
1363 info("Some resources are missing. Do you need to set --resourcePath?\n");
halcanary7d571242016-02-24 17:59:16 -08001364 }
mtklein62bd1a62015-01-27 14:46:26 -08001365 gather_gold();
borenet09ed4802015-04-03 14:15:33 -07001366 gather_uninteresting_hashes();
mtklein62bd1a62015-01-27 14:46:26 -08001367
scroggo86737142016-02-03 12:19:11 -08001368 if (!gather_srcs()) {
1369 return 1;
1370 }
Ben Wagnerf28d4722017-08-11 17:33:22 -04001371 // TODO(dogben): This is a bit ugly. Find a cleaner way to do this.
1372 bool defaultConfigs = true;
1373 for (int i = 0; i < argc; i++) {
1374 static const char* kConfigArg = "--config";
1375 if (strcmp(argv[i], kConfigArg) == 0) {
1376 defaultConfigs = false;
1377 break;
1378 }
1379 }
1380 if (!gather_sinks(grCtxOptions, defaultConfigs)) {
brianosman05c987c2016-05-09 13:42:16 -07001381 return 1;
1382 }
mtklein748ca3b2015-01-15 10:56:12 -08001383 gather_tests();
mtklein21eaf3b2016-02-08 12:39:59 -08001384 gPending = gSrcs.count() * gSinks.count() + gParallelTests.count() + gSerialTests.count();
Brian Osmand0677bc2017-08-03 12:13:47 -04001385 info("%d srcs * %d sinks + %d tests == %d tasks\n",
mtkleinc41fd922016-03-08 09:01:39 -08001386 gSrcs.count(), gSinks.count(), gParallelTests.count() + gSerialTests.count(), gPending);
mtklein748ca3b2015-01-15 10:56:12 -08001387
mtklein21eaf3b2016-02-08 12:39:59 -08001388 // Kick off as much parallel work as we can, making note of any serial work we'll need to do.
1389 SkTaskGroup parallel;
1390 SkTArray<Task> serial;
1391
1392 for (auto& sink : gSinks)
1393 for (auto& src : gSrcs) {
mtklein3eed7dd2016-02-24 19:07:07 -08001394 if (src->veto(sink->flags()) ||
1395 is_blacklisted(sink.tag.c_str(), src.tag.c_str(),
1396 src.options.c_str(), src->name().c_str())) {
mtkleinf10637f2016-06-10 13:56:35 -07001397 SkAutoMutexAcquire lock(gMutex);
mtklein3eed7dd2016-02-24 19:07:07 -08001398 gPending--;
1399 continue;
1400 }
1401
mtklein21eaf3b2016-02-08 12:39:59 -08001402 Task task(src, sink);
1403 if (src->serial() || sink->serial()) {
1404 serial.push_back(task);
1405 } else {
1406 parallel.add([task] { Task::Run(task); });
mtklein748ca3b2015-01-15 10:56:12 -08001407 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001408 }
mtklein21eaf3b2016-02-08 12:39:59 -08001409 for (auto test : gParallelTests) {
csmartdalton008b9d82017-02-22 12:00:42 -07001410 parallel.add([test, grCtxOptions] { run_test(test, grCtxOptions); });
mtklein55e88b22015-01-21 15:50:13 -08001411 }
mtklein21eaf3b2016-02-08 12:39:59 -08001412
1413 // With the parallel work running, run serial tasks and tests here on main thread.
1414 for (auto task : serial) { Task::Run(task); }
csmartdalton008b9d82017-02-22 12:00:42 -07001415 for (auto test : gSerialTests) { run_test(test, grCtxOptions); }
mtklein21eaf3b2016-02-08 12:39:59 -08001416
1417 // Wait for any remaining parallel work to complete (including any spun off of serial tasks).
1418 parallel.wait();
mtkleincd50bca2016-01-05 06:20:20 -08001419 gDefinitelyThreadSafeWork.wait();
1420
mtkleinda884c42016-02-24 18:00:23 -08001421 // We'd better have run everything.
1422 SkASSERT(gPending == 0);
mtkleine027f172016-02-26 15:53:06 -08001423 // Make sure we've flushed all our results to disk.
1424 JsonWriter::DumpJson();
mtkleinda884c42016-02-24 18:00:23 -08001425
mtklein748ca3b2015-01-15 10:56:12 -08001426 // At this point we're back in single-threaded land.
caryclarkf53ce802015-06-15 06:48:30 -07001427 sk_tool_utils::release_portable_typefaces();
mtklein@google.comd36522d2013-10-16 13:02:15 +00001428
mtklein748ca3b2015-01-15 10:56:12 -08001429 if (gFailures.count() > 0) {
mtkleinc41fd922016-03-08 09:01:39 -08001430 info("Failures:\n");
mtklein748ca3b2015-01-15 10:56:12 -08001431 for (int i = 0; i < gFailures.count(); i++) {
mtkleinc41fd922016-03-08 09:01:39 -08001432 info("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001433 }
mtkleinc41fd922016-03-08 09:01:39 -08001434 info("%d failures\n", gFailures.count());
mtklein748ca3b2015-01-15 10:56:12 -08001435 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001436 }
mtkleinafae30a2016-02-24 12:28:32 -08001437
1438#ifdef SK_PDF_IMAGE_STATS
halcanary7a14b312015-10-01 07:28:13 -07001439 SkPDFImageDumpStats();
mtkleinafae30a2016-02-24 12:28:32 -08001440#endif // SK_PDF_IMAGE_STATS
1441
Herb Derby5a523fe2017-01-26 16:48:28 -05001442 SkGraphics::PurgeAllCaches();
mtkleinc41fd922016-03-08 09:01:39 -08001443 info("Finished!\n");
Brian Osman53136aa2017-07-20 15:43:35 -04001444
mtklein748ca3b2015-01-15 10:56:12 -08001445 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +00001446}