blob: 6b76a92a1e94b177dbd56f6c6a0881437cf36827 [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"
Brian Osmanf9810662017-08-30 10:02:10 -040021#include "SkCommonFlagsGpuThreads.h"
csmartdalton008b9d82017-02-22 12:00:42 -070022#include "SkCommonFlagsPathRenderer.h"
brianosman3c579dc2016-04-19 09:18:11 -070023#include "SkData.h"
Hal Canaryc7d295e2017-07-20 15:36:00 -040024#include "SkDocument.h"
Brian Salomondcbb9d92017-07-19 10:53:20 -040025#include "SkDebugfTracer.h"
Brian Osman53136aa2017-07-20 15:43:35 -040026#include "SkEventTracingPriv.h"
caryclark83ca6282015-06-10 09:31:09 -070027#include "SkFontMgr.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"
mtkleina82f5622015-02-20 12:30:19 -080039#include "SkTHash.h"
mtklein406654b2014-09-03 15:34:37 -070040#include "SkTaskGroup.h"
mtkleinde6fc2b2015-03-12 06:28:54 -070041#include "SkThreadUtils.h"
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +000042#include "Test.h"
mtklein748ca3b2015-01-15 10:56:12 -080043#include "Timer.h"
Mike Kleinadacaef2017-02-06 09:26:14 -050044#include "ios_utils.h"
brianosman3c579dc2016-04-19 09:18:11 -070045#include "picture_utils.h"
caryclark83ca6282015-06-10 09:31:09 -070046#include "sk_tool_utils.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +000047
scroggo19b91532016-10-24 09:03:26 -070048#include <vector>
49
halcanary7a14b312015-10-01 07:28:13 -070050#ifdef SK_PDF_IMAGE_STATS
51extern void SkPDFImageDumpStats();
52#endif
53
mtkleina5114d72015-08-24 13:27:01 -070054#include "png.h"
55
bungeman60e0fee2015-08-26 05:15:46 -070056#include <stdlib.h>
57
scroggo27a58342015-10-28 08:56:41 -070058#ifndef SK_BUILD_FOR_WIN32
59 #include <unistd.h>
60#endif
61
Mike Reed7c26ca72017-07-05 15:45:52 -040062extern bool gSkForceRasterPipelineBlitter;
63
Ben Wagner32fa5102017-08-10 21:25:55 -040064DECLARE_bool(undefok);
djsollen54416de2015-04-03 07:24:48 -070065DEFINE_string(src, "tests gm skp image", "Source types to test.");
mtklein748ca3b2015-01-15 10:56:12 -080066DEFINE_bool(nameByHash, false,
67 "If true, write to FLAGS_writePath[0]/<hash>.png instead of "
djsollen54416de2015-04-03 07:24:48 -070068 "to FLAGS_writePath[0]/<config>/<sourceType>/<sourceOptions>/<name>.png");
mtklein748ca3b2015-01-15 10:56:12 -080069DEFINE_bool2(pathOpsExtended, x, false, "Run extended pathOps tests.");
mtkleind603b222015-02-17 11:13:33 -080070DEFINE_string(matrix, "1 0 0 1",
71 "2x2 scale+skew matrix to apply or upright when using "
72 "'matrix' or 'upright' in config.");
mtklein82d28432015-01-15 12:46:02 -080073DEFINE_bool(gpu_threading, false, "Allow GPU work to run on multiple threads?");
mtklein@google.comd36522d2013-10-16 13:02:15 +000074
mtkleina2ef6422015-01-15 13:44:22 -080075DEFINE_string(blacklist, "",
Chris Daltonecf78ac2017-08-15 15:43:08 -060076 "Space-separated config/src/srcOptions/name quadruples to blacklist. "
77 "'_' matches anything. '~' negates the match. E.g. \n"
djsollen54416de2015-04-03 07:24:48 -070078 "'--blacklist gpu skp _ _' will blacklist all SKPs drawn into the gpu config.\n"
Chris Daltonecf78ac2017-08-15 15:43:08 -060079 "'--blacklist gpu skp _ _ 8888 gm _ aarects' will also blacklist the aarects GM on 8888.\n"
80 "'--blacklist ~8888 svg _ svgparse_' blocks non-8888 SVGs that contain \"svgparse_\" in "
81 "the name.");
mtkleina2ef6422015-01-15 13:44:22 -080082
mtklein62bd1a62015-01-27 14:46:26 -080083DEFINE_string2(readPath, r, "", "If set check for equality with golden results in this directory.");
84
borenet09ed4802015-04-03 14:15:33 -070085DEFINE_string(uninterestingHashesFile, "",
86 "File containing a list of uninteresting hashes. If a result hashes to something in "
87 "this list, no image is written for that result.");
88
mtklein6393c062015-04-27 08:45:01 -070089DEFINE_int32(shards, 1, "We're splitting source data into this many shards.");
90DEFINE_int32(shard, 0, "Which shard do I run?");
91
halcanary45420a92016-06-02 12:41:14 -070092DEFINE_string(mskps, "", "Directory to read mskps from, or a single mskp file.");
Mike Reed7c26ca72017-07-05 15:45:52 -040093DEFINE_bool(forceRasterPipeline, false, "sets gSkForceRasterPipelineBlitter");
halcanary45420a92016-06-02 12:41:14 -070094
Robert Phillipsad8a43f2017-08-30 12:06:35 -040095DEFINE_bool(ddl, false, "If true, use DeferredDisplayLists for GPU SKP rendering.");
96
csmartdalton008b9d82017-02-22 12:00:42 -070097#if SK_SUPPORT_GPU
98DEFINE_pathrenderer_flag;
99#endif
100
Derek Sollenbergeredfe3df2017-07-19 15:25:24 -0400101DEFINE_bool(ignoreSigInt, false, "ignore SIGINT signals during test execution");
102
Mike Klein97d6a7a2017-07-24 10:37:19 -0400103DEFINE_string(dont_write, "", "File extensions to skip writing to --writePath."); // See skia:6821
104
mtklein748ca3b2015-01-15 10:56:12 -0800105using namespace DM;
bsalomon3724e572016-03-30 18:56:19 -0700106using sk_gpu_test::GrContextFactory;
bsalomon273c0f52016-03-31 10:59:06 -0700107using sk_gpu_test::GLTestContext;
bsalomonf2f1c172016-04-05 12:59:06 -0700108using sk_gpu_test::ContextInfo;
mtklein@google.comd36522d2013-10-16 13:02:15 +0000109
mtklein748ca3b2015-01-15 10:56:12 -0800110/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
111
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700112static const double kStartMs = SkTime::GetMSecs();
mtkleinc41fd922016-03-08 09:01:39 -0800113
114static FILE* gVLog;
115
116template <typename... Args>
117static void vlog(const char* fmt, Args&&... args) {
118 if (gVLog) {
Jim Van Verth32bbf372017-09-20 12:53:00 -0400119 char s[64];
120 HumanizeMs(s, 64, SkTime::GetMSecs() - kStartMs);
121 fprintf(gVLog, "%s\t", s);
mtkleinc41fd922016-03-08 09:01:39 -0800122 fprintf(gVLog, fmt, args...);
123 fflush(gVLog);
124 }
125}
126
127template <typename... Args>
128static void info(const char* fmt, Args&&... args) {
129 vlog(fmt, args...);
130 if (!FLAGS_quiet) {
131 printf(fmt, args...);
132 }
133}
134static void info(const char* fmt) {
135 if (!FLAGS_quiet) {
136 printf("%s", fmt); // Clang warns printf(fmt) is insecure.
137 }
138}
139
reed086eea92016-05-04 17:12:46 -0700140SK_DECLARE_STATIC_MUTEX(gFailuresMutex);
mtklein748ca3b2015-01-15 10:56:12 -0800141static SkTArray<SkString> gFailures;
142
mtklein3eed7dd2016-02-24 19:07:07 -0800143static void fail(const SkString& err) {
mtklein748ca3b2015-01-15 10:56:12 -0800144 SkAutoMutexAcquire lock(gFailuresMutex);
145 SkDebugf("\n\nFAILURE: %s\n\n", err.c_str());
146 gFailures.push_back(err);
halcanary9e398f72015-01-10 11:18:04 -0800147}
148
mtklein0f7748a2016-07-25 15:27:29 -0700149struct Running {
150 SkString id;
151 SkThreadID thread;
152
153 void dump() const {
154 info("\t%s\n", id.c_str());
155 }
156};
mtkleinb37cb412015-03-18 05:27:14 -0700157
mtklein246ba3a2016-02-23 10:39:36 -0800158// We use a spinlock to make locking this in a signal handler _somewhat_ safe.
Mike Klein03a95ad2017-06-23 12:36:32 -0400159static SkSpinlock gMutex;
160static int gPending;
mtklein0f7748a2016-07-25 15:27:29 -0700161static SkTArray<Running> gRunning;
mtklein748ca3b2015-01-15 10:56:12 -0800162
mtklein3eed7dd2016-02-24 19:07:07 -0800163static void done(const char* config, const char* src, const char* srcOptions, const char* name) {
164 SkString id = SkStringPrintf("%s %s %s %s", config, src, srcOptions, name);
mtkleinc41fd922016-03-08 09:01:39 -0800165 vlog("done %s\n", id.c_str());
mtkleinafae30a2016-02-24 12:28:32 -0800166 int pending;
mtkleinb37cb412015-03-18 05:27:14 -0700167 {
mtkleinf10637f2016-06-10 13:56:35 -0700168 SkAutoMutexAcquire lock(gMutex);
mtkleinb37cb412015-03-18 05:27:14 -0700169 for (int i = 0; i < gRunning.count(); i++) {
mtklein0f7748a2016-07-25 15:27:29 -0700170 if (gRunning[i].id == id) {
mtkleinb37cb412015-03-18 05:27:14 -0700171 gRunning.removeShuffle(i);
172 break;
173 }
174 }
mtkleinafae30a2016-02-24 12:28:32 -0800175 pending = --gPending;
reed50bc0512015-05-19 14:13:31 -0700176 }
Mike Klein03a95ad2017-06-23 12:36:32 -0400177
178 // We write out dm.json file and print out a progress update every once in a while.
179 // Notice this also handles the final dm.json and progress update when pending == 0.
mtkleina17241b2015-01-23 05:48:00 -0800180 if (pending % 500 == 0) {
181 JsonWriter::DumpJson();
Mike Klein03a95ad2017-06-23 12:36:32 -0400182
183 int curr = sk_tools::getCurrResidentSetSizeMB(),
184 peak = sk_tools::getMaxResidentSetSizeMB();
185 SkString elapsed = HumanizeMs(SkTime::GetMSecs() - kStartMs);
186
187 SkAutoMutexAcquire lock(gMutex);
188 info("\n%dMB RAM, %dMB peak, %s elapsed, %d queued, %d active:\n",
189 curr, peak, elapsed.c_str(), gPending - gRunning.count(), gRunning.count());
190 for (auto& task : gRunning) {
191 task.dump();
192 }
mtkleina17241b2015-01-23 05:48:00 -0800193 }
mtklein@google.comd36522d2013-10-16 13:02:15 +0000194}
195
mtklein3eed7dd2016-02-24 19:07:07 -0800196static void start(const char* config, const char* src, const char* srcOptions, const char* name) {
197 SkString id = SkStringPrintf("%s %s %s %s", config, src, srcOptions, name);
mtkleinc41fd922016-03-08 09:01:39 -0800198 vlog("start %s\n", id.c_str());
mtkleinf10637f2016-06-10 13:56:35 -0700199 SkAutoMutexAcquire lock(gMutex);
mtklein0f7748a2016-07-25 15:27:29 -0700200 gRunning.push_back({id,SkGetThreadID()});
mtkleinb37cb412015-03-18 05:27:14 -0700201}
202
Mike Klein3cc2d202016-10-24 11:39:43 -0400203static void find_culprit() {
204 // Assumes gMutex is locked.
205 SkThreadID thisThread = SkGetThreadID();
206 for (auto& task : gRunning) {
207 if (task.thread == thisThread) {
208 info("Likely culprit:\n");
209 task.dump();
mtklein0f7748a2016-07-25 15:27:29 -0700210 }
211 }
Mike Klein3cc2d202016-10-24 11:39:43 -0400212}
mtklein0f7748a2016-07-25 15:27:29 -0700213
mtklein246ba3a2016-02-23 10:39:36 -0800214#if defined(SK_BUILD_FOR_WIN32)
mtklein5b64dab2016-06-09 08:59:48 -0700215 static LONG WINAPI crash_handler(EXCEPTION_POINTERS* e) {
mtkleinf8557292016-02-29 06:35:28 -0800216 static const struct {
217 const char* name;
robertphillips75467862016-03-01 14:10:23 -0800218 DWORD code;
mtkleinf8557292016-02-29 06:35:28 -0800219 } kExceptions[] = {
220 #define _(E) {#E, E}
221 _(EXCEPTION_ACCESS_VIOLATION),
222 _(EXCEPTION_BREAKPOINT),
223 _(EXCEPTION_INT_DIVIDE_BY_ZERO),
224 _(EXCEPTION_STACK_OVERFLOW),
225 // TODO: more?
226 #undef _
227 };
228
mtkleinf10637f2016-06-10 13:56:35 -0700229 SkAutoMutexAcquire lock(gMutex);
mtklein5b64dab2016-06-09 08:59:48 -0700230
231 const DWORD code = e->ExceptionRecord->ExceptionCode;
232 info("\nCaught exception %u", code);
233 for (const auto& exception : kExceptions) {
234 if (exception.code == code) {
235 info(" %s", exception.name);
mtkleinf8557292016-02-29 06:35:28 -0800236 }
mtkleinf8557292016-02-29 06:35:28 -0800237 }
mtklein5b64dab2016-06-09 08:59:48 -0700238 info(", was running:\n");
239 for (auto& task : gRunning) {
mtklein0f7748a2016-07-25 15:27:29 -0700240 task.dump();
mtklein5b64dab2016-06-09 08:59:48 -0700241 }
mtklein0f7748a2016-07-25 15:27:29 -0700242 find_culprit();
mtklein5b64dab2016-06-09 08:59:48 -0700243 fflush(stdout);
244
mtkleinf8557292016-02-29 06:35:28 -0800245 // Execute default exception handler... hopefully, exit.
246 return EXCEPTION_EXECUTE_HANDLER;
mtklein246ba3a2016-02-23 10:39:36 -0800247 }
248
Mike Klein3cc2d202016-10-24 11:39:43 -0400249 static void setup_crash_handler() {
250 SetUnhandledExceptionFilter(crash_handler);
251 }
252#else
mtklein246ba3a2016-02-23 10:39:36 -0800253 #include <signal.h>
Mike Klein3cc2d202016-10-24 11:39:43 -0400254 #if !defined(SK_BUILD_FOR_ANDROID)
255 #include <execinfo.h>
Brian Salomondcbb9d92017-07-19 10:53:20 -0400256
257#endif
Mike Klein3cc2d202016-10-24 11:39:43 -0400258
259 static constexpr int max_of() { return 0; }
260 template <typename... Rest>
261 static constexpr int max_of(int x, Rest... rest) {
262 return x > max_of(rest...) ? x : max_of(rest...);
263 }
264
265 static void (*previous_handler[max_of(SIGABRT,SIGBUS,SIGFPE,SIGILL,SIGSEGV)+1])(int);
mtkleinf8557292016-02-29 06:35:28 -0800266
mtklein5b64dab2016-06-09 08:59:48 -0700267 static void crash_handler(int sig) {
mtkleinf10637f2016-06-10 13:56:35 -0700268 SkAutoMutexAcquire lock(gMutex);
mtklein5b64dab2016-06-09 08:59:48 -0700269
270 info("\nCaught signal %d [%s], was running:\n", sig, strsignal(sig));
271 for (auto& task : gRunning) {
mtklein0f7748a2016-07-25 15:27:29 -0700272 task.dump();
mtklein5b64dab2016-06-09 08:59:48 -0700273 }
mtklein0f7748a2016-07-25 15:27:29 -0700274 find_culprit();
mtklein5b64dab2016-06-09 08:59:48 -0700275
Mike Klein3cc2d202016-10-24 11:39:43 -0400276 #if !defined(SK_BUILD_FOR_ANDROID)
mtklein5b64dab2016-06-09 08:59:48 -0700277 void* stack[64];
278 int count = backtrace(stack, SK_ARRAY_COUNT(stack));
279 char** symbols = backtrace_symbols(stack, count);
280 info("\nStack trace:\n");
281 for (int i = 0; i < count; i++) {
282 info(" %s\n", symbols[i]);
283 }
Mike Klein3cc2d202016-10-24 11:39:43 -0400284 #endif
mtklein5b64dab2016-06-09 08:59:48 -0700285 fflush(stdout);
286
Mike Klein3cc2d202016-10-24 11:39:43 -0400287 signal(sig, previous_handler[sig]);
288 raise(sig);
mtklein5b64dab2016-06-09 08:59:48 -0700289 }
290
mtklein246ba3a2016-02-23 10:39:36 -0800291 static void setup_crash_handler() {
292 const int kSignals[] = { SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGSEGV };
293 for (int sig : kSignals) {
Mike Klein3cc2d202016-10-24 11:39:43 -0400294 previous_handler[sig] = signal(sig, crash_handler);
mtklein246ba3a2016-02-23 10:39:36 -0800295 }
Derek Sollenbergeredfe3df2017-07-19 15:25:24 -0400296
297 if (FLAGS_ignoreSigInt) {
298 signal(SIGINT, SIG_IGN);
299 }
mtklein246ba3a2016-02-23 10:39:36 -0800300 }
301#endif
302
mtklein748ca3b2015-01-15 10:56:12 -0800303/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
mtklein114c3cd2015-01-15 10:15:02 -0800304
mtklein62bd1a62015-01-27 14:46:26 -0800305struct Gold : public SkString {
mtkleina82f5622015-02-20 12:30:19 -0800306 Gold() : SkString("") {}
mtklein3eed7dd2016-02-24 19:07:07 -0800307 Gold(const SkString& sink, const SkString& src,
308 const SkString& srcOptions, const SkString& name,
309 const SkString& md5)
mtklein62bd1a62015-01-27 14:46:26 -0800310 : SkString("") {
311 this->append(sink);
312 this->append(src);
djsollen54416de2015-04-03 07:24:48 -0700313 this->append(srcOptions);
mtklein62bd1a62015-01-27 14:46:26 -0800314 this->append(name);
315 this->append(md5);
mtklein62bd1a62015-01-27 14:46:26 -0800316 }
mtkleinc8d1dd42015-10-15 12:23:01 -0700317 struct Hash {
318 uint32_t operator()(const Gold& g) const {
319 return SkGoodHash()((const SkString&)g);
320 }
321 };
mtklein62bd1a62015-01-27 14:46:26 -0800322};
mtkleina82f5622015-02-20 12:30:19 -0800323static SkTHashSet<Gold, Gold::Hash> gGold;
mtklein62bd1a62015-01-27 14:46:26 -0800324
325static void add_gold(JsonWriter::BitmapResult r) {
djsollen54416de2015-04-03 07:24:48 -0700326 gGold.add(Gold(r.config, r.sourceType, r.sourceOptions, r.name, r.md5));
mtklein62bd1a62015-01-27 14:46:26 -0800327}
328
329static void gather_gold() {
330 if (!FLAGS_readPath.isEmpty()) {
331 SkString path(FLAGS_readPath[0]);
332 path.append("/dm.json");
333 if (!JsonWriter::ReadJson(path.c_str(), add_gold)) {
334 fail(SkStringPrintf("Couldn't read %s for golden results.", path.c_str()));
335 }
336 }
337}
338
339/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
340
mtklein935f1b12016-03-16 08:37:19 -0700341#if defined(SK_BUILD_FOR_WIN32)
342 static const char* kNewline = "\r\n";
343#else
344 static const char* kNewline = "\n";
345#endif
346
borenet09ed4802015-04-03 14:15:33 -0700347static SkTHashSet<SkString> gUninterestingHashes;
348
349static void gather_uninteresting_hashes() {
350 if (!FLAGS_uninterestingHashesFile.isEmpty()) {
bungeman38d909e2016-08-02 14:40:46 -0700351 sk_sp<SkData> data(SkData::MakeFromFileName(FLAGS_uninterestingHashesFile[0]));
mtkleincc334b32015-09-22 11:43:53 -0700352 if (!data) {
mtkleinc41fd922016-03-08 09:01:39 -0800353 info("WARNING: unable to read uninteresting hashes from %s\n",
354 FLAGS_uninterestingHashesFile[0]);
mtkleincc334b32015-09-22 11:43:53 -0700355 return;
356 }
Mike Klein45dcc0c2017-04-05 18:04:31 -0400357
358 // Copy to a string to make sure SkStrSplit has a terminating \0 to find.
359 SkString contents((const char*)data->data(), data->size());
360
borenet09ed4802015-04-03 14:15:33 -0700361 SkTArray<SkString> hashes;
Mike Klein45dcc0c2017-04-05 18:04:31 -0400362 SkStrSplit(contents.c_str(), kNewline, &hashes);
borenet09ed4802015-04-03 14:15:33 -0700363 for (const SkString& hash : hashes) {
364 gUninterestingHashes.add(hash);
365 }
mtkleinc41fd922016-03-08 09:01:39 -0800366 info("FYI: loaded %d distinct uninteresting hashes from %d lines\n",
367 gUninterestingHashes.count(), hashes.count());
borenet09ed4802015-04-03 14:15:33 -0700368 }
369}
370
371/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
372
Ben Wagner145dbcd2016-11-03 14:40:50 -0400373struct TaggedSrc : public std::unique_ptr<Src> {
mtklein3eed7dd2016-02-24 19:07:07 -0800374 SkString tag;
375 SkString options;
mtkleine0effd62015-07-29 06:37:28 -0700376};
377
Ben Wagner145dbcd2016-11-03 14:40:50 -0400378struct TaggedSink : public std::unique_ptr<Sink> {
kkinnunen3e980c32015-12-23 01:33:00 -0800379 SkString tag;
djsollen54416de2015-04-03 07:24:48 -0700380};
mtklein748ca3b2015-01-15 10:56:12 -0800381
382static const bool kMemcpyOK = true;
383
mtkleine0effd62015-07-29 06:37:28 -0700384static SkTArray<TaggedSrc, kMemcpyOK> gSrcs;
385static SkTArray<TaggedSink, kMemcpyOK> gSinks;
mtklein748ca3b2015-01-15 10:56:12 -0800386
mtklein6393c062015-04-27 08:45:01 -0700387static bool in_shard() {
388 static int N = 0;
389 return N++ % FLAGS_shards == FLAGS_shard;
390}
391
mtklein3eed7dd2016-02-24 19:07:07 -0800392static void push_src(const char* tag, ImplicitString options, Src* s) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400393 std::unique_ptr<Src> src(s);
mtklein6393c062015-04-27 08:45:01 -0700394 if (in_shard() &&
mtklein3eed7dd2016-02-24 19:07:07 -0800395 FLAGS_src.contains(tag) &&
mtklein748ca3b2015-01-15 10:56:12 -0800396 !SkCommandLineFlags::ShouldSkip(FLAGS_match, src->name().c_str())) {
mtkleine0effd62015-07-29 06:37:28 -0700397 TaggedSrc& s = gSrcs.push_back();
mtklein18300a32016-03-16 13:53:35 -0700398 s.reset(src.release());
mtklein748ca3b2015-01-15 10:56:12 -0800399 s.tag = tag;
djsollen54416de2015-04-03 07:24:48 -0700400 s.options = options;
mtklein114c3cd2015-01-15 10:15:02 -0800401 }
mtklein748ca3b2015-01-15 10:56:12 -0800402}
mtklein114c3cd2015-01-15 10:15:02 -0800403
msarett9e707a02015-09-01 14:57:57 -0700404static void push_codec_src(Path path, CodecSrc::Mode mode, CodecSrc::DstColorType dstColorType,
scroggoc5560be2016-02-03 09:42:42 -0800405 SkAlphaType dstAlphaType, float scale) {
scroggoe4499842016-02-25 11:03:47 -0800406 if (FLAGS_simpleCodec) {
scroggo19b91532016-10-24 09:03:26 -0700407 const bool simple = CodecSrc::kCodec_Mode == mode || CodecSrc::kAnimated_Mode == mode;
408 if (!simple || dstColorType != CodecSrc::kGetFromCanvas_DstColorType || scale != 1.0f) {
scroggoe4499842016-02-25 11:03:47 -0800409 // Only decode in the simple case.
410 return;
scroggo19b91532016-10-24 09:03:26 -0700411 }
scroggoe4499842016-02-25 11:03:47 -0800412 }
msarett9e707a02015-09-01 14:57:57 -0700413 SkString folder;
414 switch (mode) {
415 case CodecSrc::kCodec_Mode:
416 folder.append("codec");
417 break;
msarettbb25b532016-01-13 09:31:39 -0800418 case CodecSrc::kCodecZeroInit_Mode:
419 folder.append("codec_zero_init");
420 break;
msarett9e707a02015-09-01 14:57:57 -0700421 case CodecSrc::kScanline_Mode:
422 folder.append("scanline");
423 break;
msarett9e707a02015-09-01 14:57:57 -0700424 case CodecSrc::kStripe_Mode:
425 folder.append("stripe");
426 break;
msarett91c22b22016-02-22 12:27:46 -0800427 case CodecSrc::kCroppedScanline_Mode:
428 folder.append("crop");
429 break;
msarett9e707a02015-09-01 14:57:57 -0700430 case CodecSrc::kSubset_Mode:
msarett36c37962015-09-02 13:20:52 -0700431 folder.append("codec_subset");
msarett9e707a02015-09-01 14:57:57 -0700432 break;
scroggo19b91532016-10-24 09:03:26 -0700433 case CodecSrc::kAnimated_Mode:
434 folder.append("codec_animated");
435 break;
msarett9e707a02015-09-01 14:57:57 -0700436 }
437
438 switch (dstColorType) {
439 case CodecSrc::kGrayscale_Always_DstColorType:
440 folder.append("_kGray8");
441 break;
msarett34e0ec42016-04-22 16:27:24 -0700442 case CodecSrc::kNonNative8888_Always_DstColorType:
443 folder.append("_kNonNative");
444 break;
msarett9e707a02015-09-01 14:57:57 -0700445 default:
446 break;
447 }
448
scroggoc5560be2016-02-03 09:42:42 -0800449 switch (dstAlphaType) {
scroggoc5560be2016-02-03 09:42:42 -0800450 case kPremul_SkAlphaType:
451 folder.append("_premul");
452 break;
453 case kUnpremul_SkAlphaType:
454 folder.append("_unpremul");
455 break;
456 default:
457 break;
458 }
459
msarett9e707a02015-09-01 14:57:57 -0700460 if (1.0f != scale) {
461 folder.appendf("_%.3f", scale);
462 }
463
scroggoc5560be2016-02-03 09:42:42 -0800464 CodecSrc* src = new CodecSrc(path, mode, dstColorType, dstAlphaType, scale);
msarett9e707a02015-09-01 14:57:57 -0700465 push_src("image", folder, src);
466}
467
scroggof8dc9df2016-05-16 09:04:13 -0700468static void push_android_codec_src(Path path, CodecSrc::DstColorType dstColorType,
469 SkAlphaType dstAlphaType, int sampleSize) {
msarett3d9d7a72015-10-21 10:27:10 -0700470 SkString folder;
scroggof8dc9df2016-05-16 09:04:13 -0700471 folder.append("scaled_codec");
msarett3d9d7a72015-10-21 10:27:10 -0700472
473 switch (dstColorType) {
474 case CodecSrc::kGrayscale_Always_DstColorType:
475 folder.append("_kGray8");
476 break;
msarett34e0ec42016-04-22 16:27:24 -0700477 case CodecSrc::kNonNative8888_Always_DstColorType:
478 folder.append("_kNonNative");
479 break;
msarett3d9d7a72015-10-21 10:27:10 -0700480 default:
481 break;
482 }
483
scroggoc5560be2016-02-03 09:42:42 -0800484 switch (dstAlphaType) {
scroggoc5560be2016-02-03 09:42:42 -0800485 case kPremul_SkAlphaType:
486 folder.append("_premul");
487 break;
msarett9e9444c2016-02-03 12:39:10 -0800488 case kUnpremul_SkAlphaType:
489 folder.append("_unpremul");
490 break;
scroggoc5560be2016-02-03 09:42:42 -0800491 default:
492 break;
493 }
494
msarett3d9d7a72015-10-21 10:27:10 -0700495 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800496 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700497 }
498
scroggof8dc9df2016-05-16 09:04:13 -0700499 AndroidCodecSrc* src = new AndroidCodecSrc(path, dstColorType, dstAlphaType, sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700500 push_src("image", folder, src);
501}
502
msarett18976312016-03-09 14:20:58 -0800503static void push_image_gen_src(Path path, ImageGenSrc::Mode mode, SkAlphaType alphaType, bool isGpu)
504{
505 SkString folder;
506 switch (mode) {
507 case ImageGenSrc::kCodec_Mode:
508 folder.append("gen_codec");
509 break;
510 case ImageGenSrc::kPlatform_Mode:
511 folder.append("gen_platform");
512 break;
513 }
514
515 if (isGpu) {
516 folder.append("_gpu");
517 } else {
518 switch (alphaType) {
519 case kOpaque_SkAlphaType:
520 folder.append("_opaque");
521 break;
522 case kPremul_SkAlphaType:
523 folder.append("_premul");
524 break;
525 case kUnpremul_SkAlphaType:
526 folder.append("_unpremul");
527 break;
528 default:
529 break;
530 }
531 }
532
533 ImageGenSrc* src = new ImageGenSrc(path, mode, alphaType, isGpu);
534 push_src("image", folder, src);
535}
536
Leon Scroggins III07418182017-08-15 12:24:02 -0400537static void push_brd_src(Path path, CodecSrc::DstColorType dstColorType, BRDSrc::Mode mode,
538 uint32_t sampleSize) {
539 SkString folder("brd_android_codec");
540 switch (mode) {
541 case BRDSrc::kFullImage_Mode:
542 break;
543 case BRDSrc::kDivisor_Mode:
544 folder.append("_divisor");
545 break;
546 default:
547 SkASSERT(false);
548 return;
549 }
550
551 switch (dstColorType) {
552 case CodecSrc::kGetFromCanvas_DstColorType:
553 break;
554 case CodecSrc::kGrayscale_Always_DstColorType:
555 folder.append("_kGray");
556 break;
557 default:
558 SkASSERT(false);
559 return;
560 }
561
562 if (1 != sampleSize) {
563 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
564 }
565
566 BRDSrc* src = new BRDSrc(path, mode, dstColorType, sampleSize);
567 push_src("image", folder, src);
568}
569
570static void push_brd_srcs(Path path, bool gray) {
571 if (gray) {
572 // Only run grayscale to one sampleSize and Mode. Though interesting
573 // to test grayscale, it should not reveal anything across various
574 // sampleSizes and Modes
575 // Arbitrarily choose Mode and sampleSize.
576 push_brd_src(path, CodecSrc::kGrayscale_Always_DstColorType,
577 BRDSrc::kFullImage_Mode, 2);
578 }
579
580 // Test on a variety of sampleSizes, making sure to include:
581 // - 2, 4, and 8, which are natively supported by jpeg
582 // - multiples of 2 which are not divisible by 4 (analogous for 4)
583 // - larger powers of two, since BRD clients generally use powers of 2
584 // We will only produce output for the larger sizes on large images.
585 const uint32_t sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 24, 32, 64 };
586
587 const BRDSrc::Mode modes[] = { BRDSrc::kFullImage_Mode, BRDSrc::kDivisor_Mode, };
588
589 for (uint32_t sampleSize : sampleSizes) {
590 for (BRDSrc::Mode mode : modes) {
591 push_brd_src(path, CodecSrc::kGetFromCanvas_DstColorType, mode, sampleSize);
592 }
593 }
594}
595
msarett438b2ad2015-04-09 12:43:10 -0700596static void push_codec_srcs(Path path) {
bungeman38d909e2016-08-02 14:40:46 -0700597 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
msarett438b2ad2015-04-09 12:43:10 -0700598 if (!encoded) {
mtkleinc41fd922016-03-08 09:01:39 -0800599 info("Couldn't read %s.", path.c_str());
msarett438b2ad2015-04-09 12:43:10 -0700600 return;
601 }
Mike Reedede7bac2017-07-23 15:30:02 -0400602 std::unique_ptr<SkCodec> codec = SkCodec::MakeFromData(encoded);
halcanary96fcdcc2015-08-27 07:41:13 -0700603 if (nullptr == codec.get()) {
mtkleinc41fd922016-03-08 09:01:39 -0800604 info("Couldn't create codec for %s.", path.c_str());
msarett438b2ad2015-04-09 12:43:10 -0700605 return;
606 }
607
scroggo9366aff2016-05-20 08:38:54 -0700608 // native scaling is only supported by WEBP and JPEG
609 bool supportsNativeScaling = false;
msarett438b2ad2015-04-09 12:43:10 -0700610
msarett91c22b22016-02-22 12:27:46 -0800611 SkTArray<CodecSrc::Mode> nativeModes;
612 nativeModes.push_back(CodecSrc::kCodec_Mode);
613 nativeModes.push_back(CodecSrc::kCodecZeroInit_Mode);
msarett91c22b22016-02-22 12:27:46 -0800614 switch (codec->getEncodedFormat()) {
Hal Canarydb683012016-11-23 08:55:18 -0700615 case SkEncodedImageFormat::kJPEG:
msarett91c22b22016-02-22 12:27:46 -0800616 nativeModes.push_back(CodecSrc::kScanline_Mode);
617 nativeModes.push_back(CodecSrc::kStripe_Mode);
618 nativeModes.push_back(CodecSrc::kCroppedScanline_Mode);
scroggo9366aff2016-05-20 08:38:54 -0700619 supportsNativeScaling = true;
msarett91c22b22016-02-22 12:27:46 -0800620 break;
Hal Canarydb683012016-11-23 08:55:18 -0700621 case SkEncodedImageFormat::kWEBP:
msarett91c22b22016-02-22 12:27:46 -0800622 nativeModes.push_back(CodecSrc::kSubset_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::kDNG:
msarettb65e6042016-02-23 05:37:25 -0800626 break;
msarett91c22b22016-02-22 12:27:46 -0800627 default:
628 nativeModes.push_back(CodecSrc::kScanline_Mode);
msarett91c22b22016-02-22 12:27:46 -0800629 break;
630 }
msarett9e707a02015-09-01 14:57:57 -0700631
msarett91c22b22016-02-22 12:27:46 -0800632 SkTArray<CodecSrc::DstColorType> colorTypes;
633 colorTypes.push_back(CodecSrc::kGetFromCanvas_DstColorType);
msarett34e0ec42016-04-22 16:27:24 -0700634 colorTypes.push_back(CodecSrc::kNonNative8888_Always_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700635 switch (codec->getInfo().colorType()) {
636 case kGray_8_SkColorType:
msarett91c22b22016-02-22 12:27:46 -0800637 colorTypes.push_back(CodecSrc::kGrayscale_Always_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700638 break;
639 default:
msarett36c37962015-09-02 13:20:52 -0700640 break;
641 }
msarett9e707a02015-09-01 14:57:57 -0700642
scroggoc5560be2016-02-03 09:42:42 -0800643 SkTArray<SkAlphaType> alphaModes;
644 alphaModes.push_back(kPremul_SkAlphaType);
scroggof2c96a22016-05-20 11:08:27 -0700645 if (codec->getInfo().alphaType() != kOpaque_SkAlphaType) {
646 alphaModes.push_back(kUnpremul_SkAlphaType);
scroggoc5560be2016-02-03 09:42:42 -0800647 }
msarettb714fb02016-01-22 14:46:42 -0800648
649 for (CodecSrc::Mode mode : nativeModes) {
scroggo9366aff2016-05-20 08:38:54 -0700650 for (CodecSrc::DstColorType colorType : colorTypes) {
651 for (SkAlphaType alphaType : alphaModes) {
scroggof2c96a22016-05-20 11:08:27 -0700652 // Only test kCroppedScanline_Mode when the alpha type is premul. The test is
scroggo9366aff2016-05-20 08:38:54 -0700653 // slow and won't be interestingly different with different alpha types.
654 if (CodecSrc::kCroppedScanline_Mode == mode &&
scroggof2c96a22016-05-20 11:08:27 -0700655 kPremul_SkAlphaType != alphaType) {
scroggo9366aff2016-05-20 08:38:54 -0700656 continue;
657 }
msarett91c22b22016-02-22 12:27:46 -0800658
scroggo9366aff2016-05-20 08:38:54 -0700659 push_codec_src(path, mode, colorType, alphaType, 1.0f);
msarettf3dc1882016-04-26 13:06:38 -0700660
scroggo9366aff2016-05-20 08:38:54 -0700661 // Skip kNonNative on different native scales. It won't be interestingly
662 // different.
663 if (supportsNativeScaling &&
664 CodecSrc::kNonNative8888_Always_DstColorType == colorType) {
665 // Native Scales
666 // SkJpegCodec natively supports scaling to the following:
667 for (auto scale : { 0.125f, 0.25f, 0.375f, 0.5f, 0.625f, 0.750f, 0.875f }) {
668 push_codec_src(path, mode, colorType, alphaType, scale);
669 }
scroggoc5560be2016-02-03 09:42:42 -0800670 }
msarett9e707a02015-09-01 14:57:57 -0700671 }
672 }
msarett0a242972015-06-11 14:27:27 -0700673 }
msarett36c37962015-09-02 13:20:52 -0700674
scroggo19b91532016-10-24 09:03:26 -0700675 {
676 std::vector<SkCodec::FrameInfo> frameInfos = codec->getFrameInfo();
677 if (frameInfos.size() > 1) {
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400678 for (auto dstCT : { CodecSrc::kNonNative8888_Always_DstColorType,
679 CodecSrc::kGetFromCanvas_DstColorType }) {
680 for (auto at : { kUnpremul_SkAlphaType, kPremul_SkAlphaType }) {
681 push_codec_src(path, CodecSrc::kAnimated_Mode, dstCT, at, 1.0f);
682 }
683 }
scroggo19b91532016-10-24 09:03:26 -0700684 }
685
686 }
687
scroggoe4499842016-02-25 11:03:47 -0800688 if (FLAGS_simpleCodec) {
689 return;
690 }
691
msarett3d9d7a72015-10-21 10:27:10 -0700692 const int sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
msarett36c37962015-09-02 13:20:52 -0700693
msarett3d9d7a72015-10-21 10:27:10 -0700694 for (int sampleSize : sampleSizes) {
msarett91c22b22016-02-22 12:27:46 -0800695 for (CodecSrc::DstColorType colorType : colorTypes) {
scroggoc5560be2016-02-03 09:42:42 -0800696 for (SkAlphaType alphaType : alphaModes) {
msarettf3dc1882016-04-26 13:06:38 -0700697 // We can exercise all of the kNonNative support code in the swizzler with just a
698 // few sample sizes. Skip the rest.
699 if (CodecSrc::kNonNative8888_Always_DstColorType == colorType && sampleSize > 3) {
700 continue;
701 }
702
scroggof8dc9df2016-05-16 09:04:13 -0700703 push_android_codec_src(path, colorType, alphaType, sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700704 }
msarett36c37962015-09-02 13:20:52 -0700705 }
706 }
msarett18976312016-03-09 14:20:58 -0800707
Leon Scroggins III07418182017-08-15 12:24:02 -0400708 const char* ext = strrchr(path.c_str(), '.');
709 if (ext) {
710 ext++;
msarett18976312016-03-09 14:20:58 -0800711
Leon Scroggins III07418182017-08-15 12:24:02 -0400712 static const char* const rawExts[] = {
713 "arw", "cr2", "dng", "nef", "nrw", "orf", "raf", "rw2", "pef", "srw",
714 "ARW", "CR2", "DNG", "NEF", "NRW", "ORF", "RAF", "RW2", "PEF", "SRW",
715 };
716 for (const char* rawExt : rawExts) {
717 if (0 == strcmp(rawExt, ext)) {
718 // RAW is not supported by image generator (skbug.com/5079) or BRD.
719 return;
720 }
721 }
722
723 static const char* const brdExts[] = {
724 "jpg", "jpeg", "png", "webp",
725 "JPG", "JPEG", "PNG", "WEBP",
726 };
727 for (const char* brdExt : brdExts) {
728 if (0 == strcmp(brdExt, ext)) {
729 bool gray = codec->getEncodedInfo().color() == SkEncodedInfo::kGray_Color;
730 push_brd_srcs(path, gray);
731 break;
732 }
msarett18976312016-03-09 14:20:58 -0800733 }
734 }
735
736 // Push image generator GPU test.
brianosmanc9ced392016-03-24 07:27:43 -0700737 push_image_gen_src(path, ImageGenSrc::kCodec_Mode, codec->getInfo().alphaType(), true);
msarett18976312016-03-09 14:20:58 -0800738
739 // Push image generator CPU tests.
740 for (SkAlphaType alphaType : alphaModes) {
741 push_image_gen_src(path, ImageGenSrc::kCodec_Mode, alphaType, false);
742
743#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
Hal Canarydb683012016-11-23 08:55:18 -0700744 if (SkEncodedImageFormat::kWEBP != codec->getEncodedFormat() &&
745 SkEncodedImageFormat::kWBMP != codec->getEncodedFormat() &&
msarett18976312016-03-09 14:20:58 -0800746 kUnpremul_SkAlphaType != alphaType)
747 {
748 push_image_gen_src(path, ImageGenSrc::kPlatform_Mode, alphaType, false);
749 }
msarettfc0b6d12016-03-17 13:50:17 -0700750#elif defined(SK_BUILD_FOR_WIN)
Hal Canarydb683012016-11-23 08:55:18 -0700751 if (SkEncodedImageFormat::kWEBP != codec->getEncodedFormat() &&
752 SkEncodedImageFormat::kWBMP != codec->getEncodedFormat())
msarettfc0b6d12016-03-17 13:50:17 -0700753 {
754 push_image_gen_src(path, ImageGenSrc::kPlatform_Mode, alphaType, false);
755 }
msarett18976312016-03-09 14:20:58 -0800756#endif
757 }
msarett438b2ad2015-04-09 12:43:10 -0700758}
759
fmalitaa2b9fdf2016-08-03 19:53:36 -0700760template <typename T>
761void gather_file_srcs(const SkCommandLineFlags::StringArray& flags, const char* ext) {
762 for (int i = 0; i < flags.count(); i++) {
763 const char* path = flags[i];
764 if (sk_isdir(path)) {
765 SkOSFile::Iter it(path, ext);
766 for (SkString file; it.next(&file); ) {
767 push_src(ext, "", new T(SkOSPath::Join(path, file.c_str())));
768 }
769 } else {
770 push_src(ext, "", new T(path));
771 }
772 }
773}
774
scroggo86737142016-02-03 12:19:11 -0800775static bool gather_srcs() {
mtklein748ca3b2015-01-15 10:56:12 -0800776 for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) {
djsollen54416de2015-04-03 07:24:48 -0700777 push_src("gm", "", new GMSrc(r->factory()));
mtklein748ca3b2015-01-15 10:56:12 -0800778 }
scroggo86737142016-02-03 12:19:11 -0800779
Robert Phillipsad8a43f2017-08-30 12:06:35 -0400780 if (FLAGS_ddl) {
781 gather_file_srcs<DDLSKPSrc>(FLAGS_skps, "skp");
782 } else {
783 gather_file_srcs<SKPSrc>(FLAGS_skps, "skp");
784 }
fmalitaa2b9fdf2016-08-03 19:53:36 -0700785 gather_file_srcs<MSKPSrc>(FLAGS_mskps, "mskp");
786#if defined(SK_XML)
787 gather_file_srcs<SVGSrc>(FLAGS_svgs, "svg");
788#endif
halcanary45420a92016-06-02 12:41:14 -0700789
scroggo86737142016-02-03 12:19:11 -0800790 SkTArray<SkString> images;
msarett69deca82016-04-29 09:38:40 -0700791 if (!CollectImages(FLAGS_images, &images)) {
scroggo86737142016-02-03 12:19:11 -0800792 return false;
793 }
794
795 for (auto image : images) {
796 push_codec_srcs(image);
mtklein709d2c32015-01-15 08:30:25 -0800797 }
scroggo86737142016-02-03 12:19:11 -0800798
msarett69deca82016-04-29 09:38:40 -0700799 SkTArray<SkString> colorImages;
800 if (!CollectImages(FLAGS_colorImages, &colorImages)) {
801 return false;
802 }
803
804 for (auto colorImage : colorImages) {
msarett9ce3a542016-07-15 13:54:38 -0700805 ColorCodecSrc* src = new ColorCodecSrc(colorImage, ColorCodecSrc::kBaseline_Mode,
806 kN32_SkColorType);
msarettb8d1aac2016-08-04 12:22:06 -0700807 push_src("colorImage", "color_codec_baseline", src);
msarett888dc162016-05-23 10:21:17 -0700808
msarett9ce3a542016-07-15 13:54:38 -0700809 src = new ColorCodecSrc(colorImage, ColorCodecSrc::kDst_HPZR30w_Mode, kN32_SkColorType);
msarettb8d1aac2016-08-04 12:22:06 -0700810 push_src("colorImage", "color_codec_HPZR30w", src);
msarett9ce3a542016-07-15 13:54:38 -0700811 // TODO (msarett):
812 // 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 -0700813
msarett9ce3a542016-07-15 13:54:38 -0700814 src = new ColorCodecSrc(colorImage, ColorCodecSrc::kDst_sRGB_Mode, kN32_SkColorType);
msarettb8d1aac2016-08-04 12:22:06 -0700815 push_src("colorImage", "color_codec_sRGB_kN32", src);
msarett9ce3a542016-07-15 13:54:38 -0700816 src = new ColorCodecSrc(colorImage, ColorCodecSrc::kDst_sRGB_Mode, kRGBA_F16_SkColorType);
msarettb8d1aac2016-08-04 12:22:06 -0700817 push_src("colorImage", "color_codec_sRGB_kF16", src);
msarett69deca82016-04-29 09:38:40 -0700818 }
819
scroggo86737142016-02-03 12:19:11 -0800820 return true;
mtklein709d2c32015-01-15 08:30:25 -0800821}
822
kkinnunen3e980c32015-12-23 01:33:00 -0800823static void push_sink(const SkCommandLineConfig& config, Sink* s) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400824 std::unique_ptr<Sink> sink(s);
kkinnunen3e980c32015-12-23 01:33:00 -0800825
mtkleine0effd62015-07-29 06:37:28 -0700826 // Try a simple Src as a canary. If it fails, skip this sink.
mtklein748ca3b2015-01-15 10:56:12 -0800827 struct : public Src {
mtkleine0effd62015-07-29 06:37:28 -0700828 Error draw(SkCanvas* c) const override {
829 c->drawRect(SkRect::MakeWH(1,1), SkPaint());
830 return "";
831 }
mtklein36352bf2015-03-25 18:17:31 -0700832 SkISize size() const override { return SkISize::Make(16, 16); }
mtkleine0effd62015-07-29 06:37:28 -0700833 Name name() const override { return "justOneRect"; }
834 } justOneRect;
mtklein114c3cd2015-01-15 10:15:02 -0800835
mtklein748ca3b2015-01-15 10:56:12 -0800836 SkBitmap bitmap;
837 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800838 SkString log;
mtkleine0effd62015-07-29 06:37:28 -0700839 Error err = sink->draw(justOneRect, &bitmap, &stream, &log);
mtklein4089ef72015-03-05 08:40:28 -0800840 if (err.isFatal()) {
mtkleinc41fd922016-03-08 09:01:39 -0800841 info("Could not run %s: %s\n", config.getTag().c_str(), err.c_str());
mtklein05641a52015-04-21 10:49:13 -0700842 exit(1);
mtklein114c3cd2015-01-15 10:15:02 -0800843 }
844
mtkleine0effd62015-07-29 06:37:28 -0700845 TaggedSink& ts = gSinks.push_back();
mtklein18300a32016-03-16 13:53:35 -0700846 ts.reset(sink.release());
kkinnunen3e980c32015-12-23 01:33:00 -0800847 ts.tag = config.getTag();
mtklein748ca3b2015-01-15 10:56:12 -0800848}
849
850static bool gpu_supported() {
851#if SK_SUPPORT_GPU
852 return FLAGS_gpu;
853#else
854 return false;
855#endif
856}
kkinnunen9ebc3f02015-12-21 23:48:13 -0800857
csmartdalton008b9d82017-02-22 12:00:42 -0700858static Sink* create_sink(const GrContextOptions& grCtxOptions, const SkCommandLineConfig* config) {
kkinnunen3e980c32015-12-23 01:33:00 -0800859#if SK_SUPPORT_GPU
860 if (gpu_supported()) {
861 if (const SkCommandLineConfigGpu* gpuConfig = config->asConfigGpu()) {
bsalomon85b4b532016-04-05 11:06:27 -0700862 GrContextFactory::ContextType contextType = gpuConfig->getContextType();
csmartdaltone812d492017-02-21 12:36:05 -0700863 GrContextFactory::ContextOverrides contextOverrides = gpuConfig->getContextOverrides();
csmartdalton008b9d82017-02-22 12:00:42 -0700864 GrContextFactory testFactory(grCtxOptions);
csmartdaltone812d492017-02-21 12:36:05 -0700865 if (!testFactory.get(contextType, contextOverrides)) {
mtkleinc41fd922016-03-08 09:01:39 -0800866 info("WARNING: can not create GPU context for config '%s'. "
867 "GM tests will be skipped.\n", gpuConfig->getTag().c_str());
kkinnunen3e980c32015-12-23 01:33:00 -0800868 return nullptr;
869 }
Brian Osmanf9810662017-08-30 10:02:10 -0400870 if (gpuConfig->getTestThreading()) {
871 return new GPUThreadTestingSink(contextType, contextOverrides,
872 gpuConfig->getSamples(), gpuConfig->getUseDIText(),
873 gpuConfig->getColorType(),
874 gpuConfig->getAlphaType(),
875 sk_ref_sp(gpuConfig->getColorSpace()),
876 FLAGS_gpu_threading, grCtxOptions);
877 } else {
878 return new GPUSink(contextType, contextOverrides, gpuConfig->getSamples(),
879 gpuConfig->getUseDIText(), gpuConfig->getColorType(),
880 gpuConfig->getAlphaType(), sk_ref_sp(gpuConfig->getColorSpace()),
881 FLAGS_gpu_threading, grCtxOptions);
882 }
kkinnunen3e980c32015-12-23 01:33:00 -0800883 }
884 }
885#endif
886
887#define SINK(t, sink, ...) if (config->getBackend().equals(t)) { return new sink(__VA_ARGS__); }
888
mtklein748ca3b2015-01-15 10:56:12 -0800889 if (FLAGS_cpu) {
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500890 auto srgbColorSpace = SkColorSpace::MakeSRGB();
891 auto srgbLinearColorSpace = SkColorSpace::MakeSRGBLinear();
brianosmanb109b8c2016-06-16 13:03:24 -0700892
Matt Sarettd2228302017-03-02 08:53:46 -0500893 SINK("565", RasterSink, kRGB_565_SkColorType);
894 SINK("8888", RasterSink, kN32_SkColorType);
895 SINK("srgb", RasterSink, kN32_SkColorType, srgbColorSpace);
Matt Sarettd2228302017-03-02 08:53:46 -0500896 SINK("f16", RasterSink, kRGBA_F16_SkColorType, srgbLinearColorSpace);
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);
mtklein748ca3b2015-01-15 10:56:12 -0800906 }
907#undef SINK
halcanary96fcdcc2015-08-27 07:41:13 -0700908 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800909}
910
Mike Klein841101d2017-03-10 09:55:51 -0500911static sk_sp<SkColorSpace> adobe_rgb() {
912 return SkColorSpace::MakeRGB(SkColorSpace::kSRGB_RenderTargetGamma,
913 SkColorSpace::kAdobeRGB_Gamut);
914}
915
Mike Klein919cc452017-03-18 15:36:52 +0000916static sk_sp<SkColorSpace> rgb_to_gbr() {
917 float gbr[9];
918 gbr[0] = gSRGB_toXYZD50[1];
919 gbr[1] = gSRGB_toXYZD50[2];
920 gbr[2] = gSRGB_toXYZD50[0];
921 gbr[3] = gSRGB_toXYZD50[4];
922 gbr[4] = gSRGB_toXYZD50[5];
923 gbr[5] = gSRGB_toXYZD50[3];
924 gbr[6] = gSRGB_toXYZD50[7];
925 gbr[7] = gSRGB_toXYZD50[8];
926 gbr[8] = gSRGB_toXYZD50[6];
927 SkMatrix44 toXYZD50(SkMatrix44::kUninitialized_Constructor);
928 toXYZD50.set3x3RowMajorf(gbr);
929 return SkColorSpace::MakeRGB(SkColorSpace::kSRGB_RenderTargetGamma, toXYZD50);
930}
931
kkinnunen3e980c32015-12-23 01:33:00 -0800932static Sink* create_via(const SkString& tag, Sink* wrapped) {
933#define VIA(t, via, ...) if (tag.equals(t)) { return new via(__VA_ARGS__); }
Mike Klein919cc452017-03-18 15:36:52 +0000934 VIA("adobe", ViaCSXform, wrapped, adobe_rgb(), false);
935 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);
halcanary96fcdcc2015-08-27 07:41:13 -0700938 VIA("twice", ViaTwice, wrapped);
Mike Reedbae888e2017-02-18 16:50:45 -0500939#ifdef TEST_VIA_SVG
Mike Reedf67c4592017-02-17 17:06:11 -0500940 VIA("svg", ViaSVG, wrapped);
Mike Reedbae888e2017-02-18 16:50:45 -0500941#endif
halcanary96fcdcc2015-08-27 07:41:13 -0700942 VIA("serialize", ViaSerialization, wrapped);
mtklein4a34ecb2016-01-08 10:19:35 -0800943 VIA("pic", ViaPicture, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700944 VIA("2ndpic", ViaSecondPicture, wrapped);
mtkleind31c13d2015-05-05 12:59:56 -0700945 VIA("sp", ViaSingletonPictures, wrapped);
reedbabc3de2016-07-08 08:43:27 -0700946 VIA("defer", ViaDefer, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700947 VIA("tiles", ViaTiles, 256, 256, nullptr, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800948 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800949
mtkleind603b222015-02-17 11:13:33 -0800950 if (FLAGS_matrix.count() == 4) {
mtklein748ca3b2015-01-15 10:56:12 -0800951 SkMatrix m;
mtkleind603b222015-02-17 11:13:33 -0800952 m.reset();
953 m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
954 m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
955 m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
956 m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
957 VIA("matrix", ViaMatrix, m, wrapped);
958 VIA("upright", ViaUpright, m, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800959 }
tomhudson64de1e12015-03-05 08:01:07 -0800960
mtklein748ca3b2015-01-15 10:56:12 -0800961#undef VIA
halcanary96fcdcc2015-08-27 07:41:13 -0700962 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800963}
964
Ben Wagnerf28d4722017-08-11 17:33:22 -0400965static bool gather_sinks(const GrContextOptions& grCtxOptions, bool defaultConfigs) {
kkinnunen3e980c32015-12-23 01:33:00 -0800966 SkCommandLineConfigArray configs;
967 ParseConfigs(FLAGS_config, &configs);
968 for (int i = 0; i < configs.count(); i++) {
969 const SkCommandLineConfig& config = *configs[i];
csmartdalton008b9d82017-02-22 12:00:42 -0700970 Sink* sink = create_sink(grCtxOptions, &config);
kkinnunen3e980c32015-12-23 01:33:00 -0800971 if (sink == nullptr) {
mtkleinc41fd922016-03-08 09:01:39 -0800972 info("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
973 config.getTag().c_str());
kkinnunen3e980c32015-12-23 01:33:00 -0800974 continue;
975 }
mtklein748ca3b2015-01-15 10:56:12 -0800976
kkinnunen3e980c32015-12-23 01:33:00 -0800977 const SkTArray<SkString>& parts = config.getViaParts();
978 for (int j = parts.count(); j-- > 0;) {
979 const SkString& part = parts[j];
980 Sink* next = create_via(part, sink);
halcanary96fcdcc2015-08-27 07:41:13 -0700981 if (next == nullptr) {
mtkleinc41fd922016-03-08 09:01:39 -0800982 info("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
983 part.c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800984 delete sink;
halcanary96fcdcc2015-08-27 07:41:13 -0700985 sink = nullptr;
mtklein748ca3b2015-01-15 10:56:12 -0800986 break;
987 }
988 sink = next;
989 }
990 if (sink) {
991 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800992 }
993 }
brianosman05c987c2016-05-09 13:42:16 -0700994
995 // If no configs were requested (just running tests, perhaps?), then we're okay.
Ben Wagnerf28d4722017-08-11 17:33:22 -0400996 if (configs.count() == 0 ||
997 // If we're using the default configs, we're okay.
998 defaultConfigs ||
Ben Wagner32fa5102017-08-10 21:25:55 -0400999 // If we've been told to ignore undefined flags, we're okay.
1000 FLAGS_undefok ||
1001 // Otherwise, make sure that all specified configs have become sinks.
1002 configs.count() == gSinks.count()) {
1003 return true;
1004 }
1005 info("Invalid --config. Use --undefok to bypass this warning.\n");
1006 return false;
mtklein114c3cd2015-01-15 10:15:02 -08001007}
mtklein709d2c32015-01-15 08:30:25 -08001008
mtkleina5114d72015-08-24 13:27:01 -07001009static bool dump_png(SkBitmap bitmap, const char* path, const char* md5) {
Mike Klein13b6afd2017-06-12 12:45:41 -04001010 SkPixmap pm;
1011 if (!bitmap.peekPixels(&pm)) {
1012 return false; // Ought to never happen... we're already read-back at this point.
mtkleina5114d72015-08-24 13:27:01 -07001013 }
Mike Klein13b6afd2017-06-12 12:45:41 -04001014 SkFILEWStream dst{path};
mtkleina5114d72015-08-24 13:27:01 -07001015
mtkleind69ece62015-09-10 10:37:44 -07001016 SkString description;
1017 description.append("Key: ");
1018 for (int i = 0; i < FLAGS_key.count(); i++) {
1019 description.appendf("%s ", FLAGS_key[i]);
1020 }
1021 description.append("Properties: ");
1022 for (int i = 0; i < FLAGS_properties.count(); i++) {
1023 description.appendf("%s ", FLAGS_properties[i]);
1024 }
1025 description.appendf("MD5: %s", md5);
1026
Mike Klein13b6afd2017-06-12 12:45:41 -04001027 const char* comments[] = {
1028 "Author", "DM dump_png()",
1029 "Description", description.c_str(),
1030 };
1031 size_t lengths[] = {
1032 strlen(comments[0])+1, strlen(comments[1])+1,
1033 strlen(comments[2])+1, strlen(comments[3])+1,
1034 };
mtkleina5114d72015-08-24 13:27:01 -07001035
Mike Klein13b6afd2017-06-12 12:45:41 -04001036 SkPngEncoder::Options options;
1037 options.fComments = SkDataTable::MakeCopyArrays((const void**)comments, lengths, 4);
1038 options.fFilterFlags = SkPngEncoder::FilterFlag::kNone;
1039 options.fZLibLevel = 1;
1040 options.fUnpremulBehavior = pm.colorSpace() ? SkTransferFunctionBehavior::kRespect
1041 : SkTransferFunctionBehavior::kIgnore;
1042 return SkPngEncoder::Encode(&dst, pm, options);
mtkleina5114d72015-08-24 13:27:01 -07001043}
1044
mtkleina2ef6422015-01-15 13:44:22 -08001045static bool match(const char* needle, const char* haystack) {
Chris Daltonecf78ac2017-08-15 15:43:08 -06001046 if ('~' == needle[0]) {
1047 return !match(needle + 1, haystack);
1048 }
1049 if (0 == strcmp("_", needle)) {
1050 return true;
1051 }
1052 return nullptr != strstr(haystack, needle);
mtkleina2ef6422015-01-15 13:44:22 -08001053}
1054
mtklein3eed7dd2016-02-24 19:07:07 -08001055static bool is_blacklisted(const char* sink, const char* src,
1056 const char* srcOptions, const char* name) {
mtklein0d243ff2015-04-03 07:51:00 -07001057 for (int i = 0; i < FLAGS_blacklist.count() - 3; i += 4) {
mtkleina2ef6422015-01-15 13:44:22 -08001058 if (match(FLAGS_blacklist[i+0], sink) &&
djsollen54416de2015-04-03 07:24:48 -07001059 match(FLAGS_blacklist[i+1], src) &&
1060 match(FLAGS_blacklist[i+2], srcOptions) &&
1061 match(FLAGS_blacklist[i+3], name)) {
mtklein3eed7dd2016-02-24 19:07:07 -08001062 return true;
mtkleina2ef6422015-01-15 13:44:22 -08001063 }
1064 }
mtklein3eed7dd2016-02-24 19:07:07 -08001065 return false;
mtkleina2ef6422015-01-15 13:44:22 -08001066}
1067
mtkleincd50bca2016-01-05 06:20:20 -08001068// Even when a Task Sink reports to be non-threadsafe (e.g. GPU), we know things like
1069// .png encoding are definitely thread safe. This lets us offload that work to CPU threads.
1070static SkTaskGroup gDefinitelyThreadSafeWork;
1071
mtklein748ca3b2015-01-15 10:56:12 -08001072// The finest-grained unit of work we can run: draw a single Src into a single Sink,
1073// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
1074struct Task {
mtkleine0effd62015-07-29 06:37:28 -07001075 Task(const TaggedSrc& src, const TaggedSink& sink) : src(src), sink(sink) {}
1076 const TaggedSrc& src;
1077 const TaggedSink& sink;
mtklein748ca3b2015-01-15 10:56:12 -08001078
mtklein21eaf3b2016-02-08 12:39:59 -08001079 static void Run(const Task& task) {
1080 SkString name = task.src->name();
mtkleine0effd62015-07-29 06:37:28 -07001081
mtkleinb9eb4ac2015-02-02 18:26:03 -08001082 SkString log;
mtklein3eed7dd2016-02-24 19:07:07 -08001083 if (!FLAGS_dryRun) {
mtklein748ca3b2015-01-15 10:56:12 -08001084 SkBitmap bitmap;
1085 SkDynamicMemoryWStream stream;
mtklein3eed7dd2016-02-24 19:07:07 -08001086 start(task.sink.tag.c_str(), task.src.tag.c_str(),
1087 task.src.options.c_str(), name.c_str());
mtklein21eaf3b2016-02-08 12:39:59 -08001088 Error err = task.sink->draw(*task.src, &bitmap, &stream, &log);
mtkleinb3b13b72016-03-07 13:20:52 -08001089 if (!log.isEmpty()) {
mtkleinc41fd922016-03-08 09:01:39 -08001090 info("%s %s %s %s:\n%s\n", task.sink.tag.c_str()
1091 , task.src.tag.c_str()
1092 , task.src.options.c_str()
1093 , name.c_str()
1094 , log.c_str());
mtkleinb3b13b72016-03-07 13:20:52 -08001095 }
mtklein748ca3b2015-01-15 10:56:12 -08001096 if (!err.isEmpty()) {
mtklein4089ef72015-03-05 08:40:28 -08001097 if (err.isFatal()) {
djsollen54416de2015-04-03 07:24:48 -07001098 fail(SkStringPrintf("%s %s %s %s: %s",
mtklein21eaf3b2016-02-08 12:39:59 -08001099 task.sink.tag.c_str(),
1100 task.src.tag.c_str(),
1101 task.src.options.c_str(),
mtklein4089ef72015-03-05 08:40:28 -08001102 name.c_str(),
1103 err.c_str()));
mtkleinb37cb412015-03-18 05:27:14 -07001104 } else {
mtklein3eed7dd2016-02-24 19:07:07 -08001105 done(task.sink.tag.c_str(), task.src.tag.c_str(),
1106 task.src.options.c_str(), name.c_str());
mtkleinba6ada72016-01-21 09:39:35 -08001107 return;
mtklein4089ef72015-03-05 08:40:28 -08001108 }
mtklein748ca3b2015-01-15 10:56:12 -08001109 }
mtklein62bd1a62015-01-27 14:46:26 -08001110
mtkleincd50bca2016-01-05 06:20:20 -08001111 // We're likely switching threads here, so we must capture by value, [=] or [foo,bar].
Hal Canary0b833192017-03-14 14:35:02 -04001112 SkStreamAsset* data = stream.detachAsStream().release();
mtkleincd50bca2016-01-05 06:20:20 -08001113 gDefinitelyThreadSafeWork.add([task,name,bitmap,data]{
Ben Wagner145dbcd2016-11-03 14:40:50 -04001114 std::unique_ptr<SkStreamAsset> ownedData(data);
mtkleincd50bca2016-01-05 06:20:20 -08001115
mtkleincd50bca2016-01-05 06:20:20 -08001116 SkString md5;
1117 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
1118 SkMD5 hash;
1119 if (data->getLength()) {
1120 hash.writeStream(data, data->getLength());
1121 data->rewind();
mtklein38408462015-07-08 07:25:27 -07001122 } else {
mtkleincd50bca2016-01-05 06:20:20 -08001123 // If we're BGRA (Linux, Windows), swizzle over to RGBA (Mac, Android).
1124 // This helps eliminate multiple 0-pixel-diff hashes on gold.skia.org.
1125 // (Android's general slow speed breaks the tie arbitrarily in RGBA's favor.)
1126 // We might consider promoting 565 to RGBA too.
1127 if (bitmap.colorType() == kBGRA_8888_SkColorType) {
1128 SkBitmap swizzle;
Matt Sarett68b8e3d2017-04-28 11:15:22 -04001129 SkAssertResult(sk_tool_utils::copy_to(&swizzle, kRGBA_8888_SkColorType,
1130 bitmap));
Mike Reedf0ffb892017-10-03 14:47:21 -04001131 hash.write(swizzle.getPixels(), swizzle.computeByteSize());
mtkleincd50bca2016-01-05 06:20:20 -08001132 } else {
Mike Reedf0ffb892017-10-03 14:47:21 -04001133 hash.write(bitmap.getPixels(), bitmap.computeByteSize());
mtkleincd50bca2016-01-05 06:20:20 -08001134 }
1135 }
1136 SkMD5::Digest digest;
1137 hash.finish(digest);
1138 for (int i = 0; i < 16; i++) {
1139 md5.appendf("%02x", digest.data[i]);
mtklein38408462015-07-08 07:25:27 -07001140 }
mtklein62bd1a62015-01-27 14:46:26 -08001141 }
mtklein62bd1a62015-01-27 14:46:26 -08001142
mtkleincd50bca2016-01-05 06:20:20 -08001143 if (!FLAGS_readPath.isEmpty() &&
mtklein3eed7dd2016-02-24 19:07:07 -08001144 !gGold.contains(Gold(task.sink.tag, task.src.tag,
1145 task.src.options, name, md5))) {
mtkleincd50bca2016-01-05 06:20:20 -08001146 fail(SkStringPrintf("%s not found for %s %s %s %s in %s",
1147 md5.c_str(),
mtklein21eaf3b2016-02-08 12:39:59 -08001148 task.sink.tag.c_str(),
1149 task.src.tag.c_str(),
1150 task.src.options.c_str(),
mtkleincd50bca2016-01-05 06:20:20 -08001151 name.c_str(),
1152 FLAGS_readPath[0]));
mtklein748ca3b2015-01-15 10:56:12 -08001153 }
mtkleincd50bca2016-01-05 06:20:20 -08001154
1155 if (!FLAGS_writePath.isEmpty()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001156 const char* ext = task.sink->fileExtension();
Brian Osmanf1942de2017-08-30 14:50:22 -04001157 if (ext && !FLAGS_dont_write.contains(ext)) {
Mike Klein97d6a7a2017-07-24 10:37:19 -04001158 if (data->getLength()) {
1159 WriteToDisk(task, md5, ext, data, data->getLength(), nullptr);
1160 SkASSERT(bitmap.drawsNothing());
1161 } else if (!bitmap.drawsNothing()) {
1162 WriteToDisk(task, md5, ext, nullptr, 0, &bitmap);
1163 }
mtkleincd50bca2016-01-05 06:20:20 -08001164 }
1165 }
1166 });
mtklein748ca3b2015-01-15 10:56:12 -08001167 }
mtklein3eed7dd2016-02-24 19:07:07 -08001168 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 -08001169 }
1170
1171 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -08001172 SkString md5,
1173 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -08001174 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -08001175 const SkBitmap* bitmap) {
mtklein409d4702016-02-29 07:38:01 -08001176 bool gammaCorrect = false;
1177 if (bitmap) {
Brian Osman7039f742016-11-01 15:56:16 -04001178 gammaCorrect = SkToBool(bitmap->info().colorSpace());
mtklein409d4702016-02-29 07:38:01 -08001179 }
1180
mtklein748ca3b2015-01-15 10:56:12 -08001181 JsonWriter::BitmapResult result;
djsollen54416de2015-04-03 07:24:48 -07001182 result.name = task.src->name();
mtklein3eed7dd2016-02-24 19:07:07 -08001183 result.config = task.sink.tag;
djsollen54416de2015-04-03 07:24:48 -07001184 result.sourceType = task.src.tag;
1185 result.sourceOptions = task.src.options;
1186 result.ext = ext;
mtklein409d4702016-02-29 07:38:01 -08001187 result.gammaCorrect = gammaCorrect;
djsollen54416de2015-04-03 07:24:48 -07001188 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -08001189 JsonWriter::AddBitmapResult(result);
1190
mtkleinb0531a72015-04-07 13:38:48 -07001191 // If an MD5 is uninteresting, we want it noted in the JSON file,
1192 // but don't want to dump it out as a .png (or whatever ext is).
1193 if (gUninterestingHashes.contains(md5)) {
1194 return;
1195 }
1196
mtklein748ca3b2015-01-15 10:56:12 -08001197 const char* dir = FLAGS_writePath[0];
1198 if (0 == strcmp(dir, "@")) { // Needed for iOS.
1199 dir = FLAGS_resourcePath[0];
1200 }
1201 sk_mkdir(dir);
1202
1203 SkString path;
1204 if (FLAGS_nameByHash) {
1205 path = SkOSPath::Join(dir, result.md5.c_str());
1206 path.append(".");
1207 path.append(ext);
1208 if (sk_exists(path.c_str())) {
1209 return; // Content-addressed. If it exists already, we're done.
1210 }
1211 } else {
kkinnunen3e980c32015-12-23 01:33:00 -08001212 path = SkOSPath::Join(dir, task.sink.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001213 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001214 path = SkOSPath::Join(path.c_str(), task.src.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001215 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001216 if (strcmp(task.src.options.c_str(), "") != 0) {
1217 path = SkOSPath::Join(path.c_str(), task.src.options.c_str());
djsollen54416de2015-04-03 07:24:48 -07001218 sk_mkdir(path.c_str());
1219 }
mtklein748ca3b2015-01-15 10:56:12 -08001220 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
1221 path.append(".");
1222 path.append(ext);
1223 }
1224
mtklein748ca3b2015-01-15 10:56:12 -08001225 if (bitmap) {
mtkleina5114d72015-08-24 13:27:01 -07001226 if (!dump_png(*bitmap, path.c_str(), result.md5.c_str())) {
mtklein748ca3b2015-01-15 10:56:12 -08001227 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
1228 return;
1229 }
1230 } else {
mtkleina5114d72015-08-24 13:27:01 -07001231 SkFILEWStream file(path.c_str());
1232 if (!file.isValid()) {
1233 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
1234 return;
1235 }
mtklein748ca3b2015-01-15 10:56:12 -08001236 if (!file.writeStream(data, len)) {
1237 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
1238 return;
1239 }
1240 }
1241 }
1242};
1243
mtklein748ca3b2015-01-15 10:56:12 -08001244/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1245
1246// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
1247
mtklein21eaf3b2016-02-08 12:39:59 -08001248static SkTDArray<skiatest::Test> gParallelTests, gSerialTests;
mtklein748ca3b2015-01-15 10:56:12 -08001249
1250static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -08001251 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -08001252 return;
1253 }
mtklein6393c062015-04-27 08:45:01 -07001254 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r; r = r->next()) {
1255 if (!in_shard()) {
1256 continue;
1257 }
halcanary87f3ba42015-01-20 09:30:20 -08001258 // Despite its name, factory() is returning a reference to
1259 // link-time static const POD data.
1260 const skiatest::Test& test = r->factory();
1261 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -08001262 continue;
1263 }
halcanary87f3ba42015-01-20 09:30:20 -08001264 if (test.needsGpu && gpu_supported()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001265 (FLAGS_gpu_threading ? gParallelTests : gSerialTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -08001266 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein21eaf3b2016-02-08 12:39:59 -08001267 gParallelTests.push(test);
mtklein82d28432015-01-15 12:46:02 -08001268 }
mtklein748ca3b2015-01-15 10:56:12 -08001269 }
1270}
1271
csmartdalton008b9d82017-02-22 12:00:42 -07001272static void run_test(skiatest::Test test, const GrContextOptions& grCtxOptions) {
halcanary87f3ba42015-01-20 09:30:20 -08001273 struct : public skiatest::Reporter {
mtklein36352bf2015-03-25 18:17:31 -07001274 void reportFailed(const skiatest::Failure& failure) override {
halcanary87f3ba42015-01-20 09:30:20 -08001275 fail(failure.toString());
1276 JsonWriter::AddTestFailure(failure);
1277 }
mtklein36352bf2015-03-25 18:17:31 -07001278 bool allowExtendedTest() const override {
halcanary87f3ba42015-01-20 09:30:20 -08001279 return FLAGS_pathOpsExtended;
1280 }
mtklein36352bf2015-03-25 18:17:31 -07001281 bool verbose() const override { return FLAGS_veryVerbose; }
halcanary87f3ba42015-01-20 09:30:20 -08001282 } reporter;
djsollen824996a2015-06-12 12:06:22 -07001283
mtklein3eed7dd2016-02-24 19:07:07 -08001284 if (!FLAGS_dryRun && !is_blacklisted("_", "tests", "_", test.name)) {
mtklein21eaf3b2016-02-08 12:39:59 -08001285 start("unit", "test", "", test.name);
csmartdalton008b9d82017-02-22 12:00:42 -07001286 GrContextFactory factory(grCtxOptions);
Mike Kleinb323a5e2017-07-24 15:21:31 -04001287 test.run(&reporter, &factory);
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) {
1309 SkCommandLineFlags::Parse(argc, argv);
Brian Osman53136aa2017-07-20 15:43:35 -04001310
Brian Osmanbc8150f2017-07-24 11:38:01 -04001311 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -04001312
Mike Klein135a1b12017-08-15 13:13:59 -04001313#if !defined(GOOGLE3) && defined(SK_BUILD_FOR_IOS)
Mike Kleinadacaef2017-02-06 09:26:14 -05001314 cd_Documents();
1315#endif
bsalomon71de3532016-04-26 14:27:21 -07001316 setbuf(stdout, nullptr);
mtklein246ba3a2016-02-23 10:39:36 -08001317 setup_crash_handler();
mtkleinafae30a2016-02-24 12:28:32 -08001318
Yuqian Liba62b4a2016-12-14 13:46:53 -05001319 gSkUseAnalyticAA = FLAGS_analyticAA;
Yuqian Lidf60e362017-07-25 11:26:31 -04001320 gSkUseDeltaAA = FLAGS_deltaAA;
liyuqian38911a72016-10-04 11:23:22 -07001321
Yuqian Li550148b2017-01-13 10:13:13 -05001322 if (FLAGS_forceAnalyticAA) {
1323 gSkForceAnalyticAA = true;
1324 }
Yuqian Lia81b6262017-09-06 17:10:05 -04001325 if (FLAGS_forceDeltaAA) {
1326 gSkForceDeltaAA = true;
1327 }
Mike Reed7c26ca72017-07-05 15:45:52 -04001328 if (FLAGS_forceRasterPipeline) {
1329 gSkForceRasterPipelineBlitter = true;
1330 }
Yuqian Li550148b2017-01-13 10:13:13 -05001331
Mike Kleine9f187b2017-06-26 13:23:12 -04001332 // The bots like having a verbose.log to upload, so always touch the file even if --verbose.
1333 if (!FLAGS_writePath.isEmpty()) {
mtklein51c8cfc2016-03-11 12:59:09 -08001334 sk_mkdir(FLAGS_writePath[0]);
mtkleina1ce2162016-07-22 12:23:46 -07001335 gVLog = fopen(SkOSPath::Join(FLAGS_writePath[0], "verbose.log").c_str(), "w");
mtkleinc41fd922016-03-08 09:01:39 -08001336 }
Mike Kleine9f187b2017-06-26 13:23:12 -04001337 if (FLAGS_verbose) {
1338 gVLog = stderr;
1339 }
mtkleinc41fd922016-03-08 09:01:39 -08001340
csmartdalton008b9d82017-02-22 12:00:42 -07001341 GrContextOptions grCtxOptions;
1342#if SK_SUPPORT_GPU
1343 grCtxOptions.fGpuPathRenderers = CollectGpuPathRenderersFromFlags();
Brian Osmanf9810662017-08-30 10:02:10 -04001344 grCtxOptions.fExecutor = GpuExecutorForTools();
csmartdalton008b9d82017-02-22 12:00:42 -07001345#endif
1346
mtklein5286f022016-01-22 08:18:14 -08001347 JsonWriter::DumpJson(); // It's handy for the bots to assume this is ~never missing.
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +00001348 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -07001349 SkTaskGroup::Enabler enabled(FLAGS_threads);
caryclark83ca6282015-06-10 09:31:09 -07001350 gCreateTypefaceDelegate = &create_from_name;
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +00001351
halcanary7d571242016-02-24 17:59:16 -08001352 {
1353 SkString testResourcePath = GetResourcePath("color_wheel.png");
1354 SkFILEStream testResource(testResourcePath.c_str());
1355 if (!testResource.isValid()) {
mtkleinc41fd922016-03-08 09:01:39 -08001356 info("Some resources are missing. Do you need to set --resourcePath?\n");
halcanary7d571242016-02-24 17:59:16 -08001357 }
1358 }
mtklein62bd1a62015-01-27 14:46:26 -08001359 gather_gold();
borenet09ed4802015-04-03 14:15:33 -07001360 gather_uninteresting_hashes();
mtklein62bd1a62015-01-27 14:46:26 -08001361
scroggo86737142016-02-03 12:19:11 -08001362 if (!gather_srcs()) {
1363 return 1;
1364 }
Ben Wagnerf28d4722017-08-11 17:33:22 -04001365 // TODO(dogben): This is a bit ugly. Find a cleaner way to do this.
1366 bool defaultConfigs = true;
1367 for (int i = 0; i < argc; i++) {
1368 static const char* kConfigArg = "--config";
1369 if (strcmp(argv[i], kConfigArg) == 0) {
1370 defaultConfigs = false;
1371 break;
1372 }
1373 }
1374 if (!gather_sinks(grCtxOptions, defaultConfigs)) {
brianosman05c987c2016-05-09 13:42:16 -07001375 return 1;
1376 }
mtklein748ca3b2015-01-15 10:56:12 -08001377 gather_tests();
mtklein21eaf3b2016-02-08 12:39:59 -08001378 gPending = gSrcs.count() * gSinks.count() + gParallelTests.count() + gSerialTests.count();
Brian Osmand0677bc2017-08-03 12:13:47 -04001379 info("%d srcs * %d sinks + %d tests == %d tasks\n",
mtkleinc41fd922016-03-08 09:01:39 -08001380 gSrcs.count(), gSinks.count(), gParallelTests.count() + gSerialTests.count(), gPending);
mtklein748ca3b2015-01-15 10:56:12 -08001381
mtklein21eaf3b2016-02-08 12:39:59 -08001382 // Kick off as much parallel work as we can, making note of any serial work we'll need to do.
1383 SkTaskGroup parallel;
1384 SkTArray<Task> serial;
1385
1386 for (auto& sink : gSinks)
1387 for (auto& src : gSrcs) {
mtklein3eed7dd2016-02-24 19:07:07 -08001388 if (src->veto(sink->flags()) ||
1389 is_blacklisted(sink.tag.c_str(), src.tag.c_str(),
1390 src.options.c_str(), src->name().c_str())) {
mtkleinf10637f2016-06-10 13:56:35 -07001391 SkAutoMutexAcquire lock(gMutex);
mtklein3eed7dd2016-02-24 19:07:07 -08001392 gPending--;
1393 continue;
1394 }
1395
mtklein21eaf3b2016-02-08 12:39:59 -08001396 Task task(src, sink);
1397 if (src->serial() || sink->serial()) {
1398 serial.push_back(task);
1399 } else {
1400 parallel.add([task] { Task::Run(task); });
mtklein748ca3b2015-01-15 10:56:12 -08001401 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001402 }
mtklein21eaf3b2016-02-08 12:39:59 -08001403 for (auto test : gParallelTests) {
csmartdalton008b9d82017-02-22 12:00:42 -07001404 parallel.add([test, grCtxOptions] { run_test(test, grCtxOptions); });
mtklein55e88b22015-01-21 15:50:13 -08001405 }
mtklein21eaf3b2016-02-08 12:39:59 -08001406
1407 // With the parallel work running, run serial tasks and tests here on main thread.
1408 for (auto task : serial) { Task::Run(task); }
csmartdalton008b9d82017-02-22 12:00:42 -07001409 for (auto test : gSerialTests) { run_test(test, grCtxOptions); }
mtklein21eaf3b2016-02-08 12:39:59 -08001410
1411 // Wait for any remaining parallel work to complete (including any spun off of serial tasks).
1412 parallel.wait();
mtkleincd50bca2016-01-05 06:20:20 -08001413 gDefinitelyThreadSafeWork.wait();
1414
mtkleinda884c42016-02-24 18:00:23 -08001415 // We'd better have run everything.
1416 SkASSERT(gPending == 0);
mtkleine027f172016-02-26 15:53:06 -08001417 // Make sure we've flushed all our results to disk.
1418 JsonWriter::DumpJson();
mtkleinda884c42016-02-24 18:00:23 -08001419
mtklein748ca3b2015-01-15 10:56:12 -08001420 // At this point we're back in single-threaded land.
caryclarkf53ce802015-06-15 06:48:30 -07001421 sk_tool_utils::release_portable_typefaces();
mtklein@google.comd36522d2013-10-16 13:02:15 +00001422
mtklein748ca3b2015-01-15 10:56:12 -08001423 if (gFailures.count() > 0) {
mtkleinc41fd922016-03-08 09:01:39 -08001424 info("Failures:\n");
mtklein748ca3b2015-01-15 10:56:12 -08001425 for (int i = 0; i < gFailures.count(); i++) {
mtkleinc41fd922016-03-08 09:01:39 -08001426 info("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001427 }
mtkleinc41fd922016-03-08 09:01:39 -08001428 info("%d failures\n", gFailures.count());
mtklein748ca3b2015-01-15 10:56:12 -08001429 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001430 }
mtkleinafae30a2016-02-24 12:28:32 -08001431
1432#ifdef SK_PDF_IMAGE_STATS
halcanary7a14b312015-10-01 07:28:13 -07001433 SkPDFImageDumpStats();
mtkleinafae30a2016-02-24 12:28:32 -08001434#endif // SK_PDF_IMAGE_STATS
1435
Herb Derby5a523fe2017-01-26 16:48:28 -05001436 SkGraphics::PurgeAllCaches();
mtkleinc41fd922016-03-08 09:01:39 -08001437 info("Finished!\n");
Brian Osman53136aa2017-07-20 15:43:35 -04001438
mtklein748ca3b2015-01-15 10:56:12 -08001439 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +00001440}