blob: cb5332d77aaf4da47bd52d646986940c9c240ad3 [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"
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +000041#include "Test.h"
mtklein748ca3b2015-01-15 10:56:12 -080042#include "Timer.h"
Mike Kleinadacaef2017-02-06 09:26:14 -050043#include "ios_utils.h"
brianosman3c579dc2016-04-19 09:18:11 -070044#include "picture_utils.h"
caryclark83ca6282015-06-10 09:31:09 -070045#include "sk_tool_utils.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +000046
scroggo19b91532016-10-24 09:03:26 -070047#include <vector>
48
halcanary7a14b312015-10-01 07:28:13 -070049#ifdef SK_PDF_IMAGE_STATS
50extern void SkPDFImageDumpStats();
51#endif
52
mtkleina5114d72015-08-24 13:27:01 -070053#include "png.h"
54
bungeman60e0fee2015-08-26 05:15:46 -070055#include <stdlib.h>
56
scroggo27a58342015-10-28 08:56:41 -070057#ifndef SK_BUILD_FOR_WIN32
58 #include <unistd.h>
59#endif
60
Mike Reed7c26ca72017-07-05 15:45:52 -040061extern bool gSkForceRasterPipelineBlitter;
62
Ben Wagner32fa5102017-08-10 21:25:55 -040063DECLARE_bool(undefok);
djsollen54416de2015-04-03 07:24:48 -070064DEFINE_string(src, "tests gm skp image", "Source types to test.");
mtklein748ca3b2015-01-15 10:56:12 -080065DEFINE_bool(nameByHash, false,
66 "If true, write to FLAGS_writePath[0]/<hash>.png instead of "
djsollen54416de2015-04-03 07:24:48 -070067 "to FLAGS_writePath[0]/<config>/<sourceType>/<sourceOptions>/<name>.png");
mtklein748ca3b2015-01-15 10:56:12 -080068DEFINE_bool2(pathOpsExtended, x, false, "Run extended pathOps tests.");
mtkleind603b222015-02-17 11:13:33 -080069DEFINE_string(matrix, "1 0 0 1",
70 "2x2 scale+skew matrix to apply or upright when using "
71 "'matrix' or 'upright' in config.");
mtklein82d28432015-01-15 12:46:02 -080072DEFINE_bool(gpu_threading, false, "Allow GPU work to run on multiple threads?");
mtklein@google.comd36522d2013-10-16 13:02:15 +000073
mtkleina2ef6422015-01-15 13:44:22 -080074DEFINE_string(blacklist, "",
Chris Daltonecf78ac2017-08-15 15:43:08 -060075 "Space-separated config/src/srcOptions/name quadruples to blacklist. "
76 "'_' matches anything. '~' negates the match. E.g. \n"
djsollen54416de2015-04-03 07:24:48 -070077 "'--blacklist gpu skp _ _' will blacklist all SKPs drawn into the gpu config.\n"
Chris Daltonecf78ac2017-08-15 15:43:08 -060078 "'--blacklist gpu skp _ _ 8888 gm _ aarects' will also blacklist the aarects GM on 8888.\n"
79 "'--blacklist ~8888 svg _ svgparse_' blocks non-8888 SVGs that contain \"svgparse_\" in "
80 "the name.");
mtkleina2ef6422015-01-15 13:44:22 -080081
mtklein62bd1a62015-01-27 14:46:26 -080082DEFINE_string2(readPath, r, "", "If set check for equality with golden results in this directory.");
83
borenet09ed4802015-04-03 14:15:33 -070084DEFINE_string(uninterestingHashesFile, "",
85 "File containing a list of uninteresting hashes. If a result hashes to something in "
86 "this list, no image is written for that result.");
87
mtklein6393c062015-04-27 08:45:01 -070088DEFINE_int32(shards, 1, "We're splitting source data into this many shards.");
89DEFINE_int32(shard, 0, "Which shard do I run?");
90
halcanary45420a92016-06-02 12:41:14 -070091DEFINE_string(mskps, "", "Directory to read mskps from, or a single mskp file.");
Mike Reed7c26ca72017-07-05 15:45:52 -040092DEFINE_bool(forceRasterPipeline, false, "sets gSkForceRasterPipelineBlitter");
halcanary45420a92016-06-02 12:41:14 -070093
Robert Phillipsad8a43f2017-08-30 12:06:35 -040094DEFINE_bool(ddl, false, "If true, use DeferredDisplayLists for GPU SKP rendering.");
95
csmartdalton008b9d82017-02-22 12:00:42 -070096#if SK_SUPPORT_GPU
97DEFINE_pathrenderer_flag;
98#endif
99
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
mtklein748ca3b2015-01-15 10:56:12 -0800104using namespace DM;
bsalomon3724e572016-03-30 18:56:19 -0700105using sk_gpu_test::GrContextFactory;
bsalomon273c0f52016-03-31 10:59:06 -0700106using sk_gpu_test::GLTestContext;
bsalomonf2f1c172016-04-05 12:59:06 -0700107using sk_gpu_test::ContextInfo;
mtklein@google.comd36522d2013-10-16 13:02:15 +0000108
mtklein748ca3b2015-01-15 10:56:12 -0800109/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
110
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700111static const double kStartMs = SkTime::GetMSecs();
mtkleinc41fd922016-03-08 09:01:39 -0800112
113static FILE* gVLog;
114
115template <typename... Args>
116static void vlog(const char* fmt, Args&&... args) {
117 if (gVLog) {
Jim Van Verth32bbf372017-09-20 12:53:00 -0400118 char s[64];
119 HumanizeMs(s, 64, SkTime::GetMSecs() - kStartMs);
120 fprintf(gVLog, "%s\t", s);
mtkleinc41fd922016-03-08 09:01:39 -0800121 fprintf(gVLog, fmt, args...);
122 fflush(gVLog);
123 }
124}
125
126template <typename... Args>
127static void info(const char* fmt, Args&&... args) {
128 vlog(fmt, args...);
129 if (!FLAGS_quiet) {
130 printf(fmt, args...);
131 }
132}
133static void info(const char* fmt) {
134 if (!FLAGS_quiet) {
135 printf("%s", fmt); // Clang warns printf(fmt) is insecure.
136 }
137}
138
reed086eea92016-05-04 17:12:46 -0700139SK_DECLARE_STATIC_MUTEX(gFailuresMutex);
mtklein748ca3b2015-01-15 10:56:12 -0800140static SkTArray<SkString> gFailures;
141
mtklein3eed7dd2016-02-24 19:07:07 -0800142static void fail(const SkString& err) {
mtklein748ca3b2015-01-15 10:56:12 -0800143 SkAutoMutexAcquire lock(gFailuresMutex);
144 SkDebugf("\n\nFAILURE: %s\n\n", err.c_str());
145 gFailures.push_back(err);
halcanary9e398f72015-01-10 11:18:04 -0800146}
147
mtklein0f7748a2016-07-25 15:27:29 -0700148struct Running {
149 SkString id;
150 SkThreadID thread;
151
152 void dump() const {
153 info("\t%s\n", id.c_str());
154 }
155};
mtkleinb37cb412015-03-18 05:27:14 -0700156
mtklein246ba3a2016-02-23 10:39:36 -0800157// We use a spinlock to make locking this in a signal handler _somewhat_ safe.
Mike Klein03a95ad2017-06-23 12:36:32 -0400158static SkSpinlock gMutex;
159static int gPending;
mtklein0f7748a2016-07-25 15:27:29 -0700160static SkTArray<Running> gRunning;
mtklein748ca3b2015-01-15 10:56:12 -0800161
mtklein3eed7dd2016-02-24 19:07:07 -0800162static void done(const char* config, const char* src, const char* srcOptions, const char* name) {
163 SkString id = SkStringPrintf("%s %s %s %s", config, src, srcOptions, name);
mtkleinc41fd922016-03-08 09:01:39 -0800164 vlog("done %s\n", id.c_str());
mtkleinafae30a2016-02-24 12:28:32 -0800165 int pending;
mtkleinb37cb412015-03-18 05:27:14 -0700166 {
mtkleinf10637f2016-06-10 13:56:35 -0700167 SkAutoMutexAcquire lock(gMutex);
mtkleinb37cb412015-03-18 05:27:14 -0700168 for (int i = 0; i < gRunning.count(); i++) {
mtklein0f7748a2016-07-25 15:27:29 -0700169 if (gRunning[i].id == id) {
mtkleinb37cb412015-03-18 05:27:14 -0700170 gRunning.removeShuffle(i);
171 break;
172 }
173 }
mtkleinafae30a2016-02-24 12:28:32 -0800174 pending = --gPending;
reed50bc0512015-05-19 14:13:31 -0700175 }
Mike Klein03a95ad2017-06-23 12:36:32 -0400176
177 // We write out dm.json file and print out a progress update every once in a while.
178 // Notice this also handles the final dm.json and progress update when pending == 0.
mtkleina17241b2015-01-23 05:48:00 -0800179 if (pending % 500 == 0) {
180 JsonWriter::DumpJson();
Mike Klein03a95ad2017-06-23 12:36:32 -0400181
182 int curr = sk_tools::getCurrResidentSetSizeMB(),
183 peak = sk_tools::getMaxResidentSetSizeMB();
184 SkString elapsed = HumanizeMs(SkTime::GetMSecs() - kStartMs);
185
186 SkAutoMutexAcquire lock(gMutex);
187 info("\n%dMB RAM, %dMB peak, %s elapsed, %d queued, %d active:\n",
188 curr, peak, elapsed.c_str(), gPending - gRunning.count(), gRunning.count());
189 for (auto& task : gRunning) {
190 task.dump();
191 }
mtkleina17241b2015-01-23 05:48:00 -0800192 }
mtklein@google.comd36522d2013-10-16 13:02:15 +0000193}
194
mtklein3eed7dd2016-02-24 19:07:07 -0800195static void start(const char* config, const char* src, const char* srcOptions, const char* name) {
196 SkString id = SkStringPrintf("%s %s %s %s", config, src, srcOptions, name);
mtkleinc41fd922016-03-08 09:01:39 -0800197 vlog("start %s\n", id.c_str());
mtkleinf10637f2016-06-10 13:56:35 -0700198 SkAutoMutexAcquire lock(gMutex);
mtklein0f7748a2016-07-25 15:27:29 -0700199 gRunning.push_back({id,SkGetThreadID()});
mtkleinb37cb412015-03-18 05:27:14 -0700200}
201
Mike Klein3cc2d202016-10-24 11:39:43 -0400202static void find_culprit() {
203 // Assumes gMutex is locked.
204 SkThreadID thisThread = SkGetThreadID();
205 for (auto& task : gRunning) {
206 if (task.thread == thisThread) {
207 info("Likely culprit:\n");
208 task.dump();
mtklein0f7748a2016-07-25 15:27:29 -0700209 }
210 }
Mike Klein3cc2d202016-10-24 11:39:43 -0400211}
mtklein0f7748a2016-07-25 15:27:29 -0700212
mtklein246ba3a2016-02-23 10:39:36 -0800213#if defined(SK_BUILD_FOR_WIN32)
mtklein5b64dab2016-06-09 08:59:48 -0700214 static LONG WINAPI crash_handler(EXCEPTION_POINTERS* e) {
mtkleinf8557292016-02-29 06:35:28 -0800215 static const struct {
216 const char* name;
robertphillips75467862016-03-01 14:10:23 -0800217 DWORD code;
mtkleinf8557292016-02-29 06:35:28 -0800218 } kExceptions[] = {
219 #define _(E) {#E, E}
220 _(EXCEPTION_ACCESS_VIOLATION),
221 _(EXCEPTION_BREAKPOINT),
222 _(EXCEPTION_INT_DIVIDE_BY_ZERO),
223 _(EXCEPTION_STACK_OVERFLOW),
224 // TODO: more?
225 #undef _
226 };
227
mtkleinf10637f2016-06-10 13:56:35 -0700228 SkAutoMutexAcquire lock(gMutex);
mtklein5b64dab2016-06-09 08:59:48 -0700229
230 const DWORD code = e->ExceptionRecord->ExceptionCode;
231 info("\nCaught exception %u", code);
232 for (const auto& exception : kExceptions) {
233 if (exception.code == code) {
234 info(" %s", exception.name);
mtkleinf8557292016-02-29 06:35:28 -0800235 }
mtkleinf8557292016-02-29 06:35:28 -0800236 }
mtklein5b64dab2016-06-09 08:59:48 -0700237 info(", was running:\n");
238 for (auto& task : gRunning) {
mtklein0f7748a2016-07-25 15:27:29 -0700239 task.dump();
mtklein5b64dab2016-06-09 08:59:48 -0700240 }
mtklein0f7748a2016-07-25 15:27:29 -0700241 find_culprit();
mtklein5b64dab2016-06-09 08:59:48 -0700242 fflush(stdout);
243
mtkleinf8557292016-02-29 06:35:28 -0800244 // Execute default exception handler... hopefully, exit.
245 return EXCEPTION_EXECUTE_HANDLER;
mtklein246ba3a2016-02-23 10:39:36 -0800246 }
247
Mike Klein3cc2d202016-10-24 11:39:43 -0400248 static void setup_crash_handler() {
249 SetUnhandledExceptionFilter(crash_handler);
250 }
251#else
mtklein246ba3a2016-02-23 10:39:36 -0800252 #include <signal.h>
Mike Klein3cc2d202016-10-24 11:39:43 -0400253 #if !defined(SK_BUILD_FOR_ANDROID)
254 #include <execinfo.h>
Brian Salomondcbb9d92017-07-19 10:53:20 -0400255
256#endif
Mike Klein3cc2d202016-10-24 11:39:43 -0400257
258 static constexpr int max_of() { return 0; }
259 template <typename... Rest>
260 static constexpr int max_of(int x, Rest... rest) {
261 return x > max_of(rest...) ? x : max_of(rest...);
262 }
263
264 static void (*previous_handler[max_of(SIGABRT,SIGBUS,SIGFPE,SIGILL,SIGSEGV)+1])(int);
mtkleinf8557292016-02-29 06:35:28 -0800265
mtklein5b64dab2016-06-09 08:59:48 -0700266 static void crash_handler(int sig) {
mtkleinf10637f2016-06-10 13:56:35 -0700267 SkAutoMutexAcquire lock(gMutex);
mtklein5b64dab2016-06-09 08:59:48 -0700268
269 info("\nCaught signal %d [%s], was running:\n", sig, strsignal(sig));
270 for (auto& task : gRunning) {
mtklein0f7748a2016-07-25 15:27:29 -0700271 task.dump();
mtklein5b64dab2016-06-09 08:59:48 -0700272 }
mtklein0f7748a2016-07-25 15:27:29 -0700273 find_culprit();
mtklein5b64dab2016-06-09 08:59:48 -0700274
Mike Klein3cc2d202016-10-24 11:39:43 -0400275 #if !defined(SK_BUILD_FOR_ANDROID)
mtklein5b64dab2016-06-09 08:59:48 -0700276 void* stack[64];
277 int count = backtrace(stack, SK_ARRAY_COUNT(stack));
278 char** symbols = backtrace_symbols(stack, count);
279 info("\nStack trace:\n");
280 for (int i = 0; i < count; i++) {
281 info(" %s\n", symbols[i]);
282 }
Mike Klein3cc2d202016-10-24 11:39:43 -0400283 #endif
mtklein5b64dab2016-06-09 08:59:48 -0700284 fflush(stdout);
285
Mike Klein3cc2d202016-10-24 11:39:43 -0400286 signal(sig, previous_handler[sig]);
287 raise(sig);
mtklein5b64dab2016-06-09 08:59:48 -0700288 }
289
mtklein246ba3a2016-02-23 10:39:36 -0800290 static void setup_crash_handler() {
291 const int kSignals[] = { SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGSEGV };
292 for (int sig : kSignals) {
Mike Klein3cc2d202016-10-24 11:39:43 -0400293 previous_handler[sig] = signal(sig, crash_handler);
mtklein246ba3a2016-02-23 10:39:36 -0800294 }
Derek Sollenbergeredfe3df2017-07-19 15:25:24 -0400295
296 if (FLAGS_ignoreSigInt) {
297 signal(SIGINT, SIG_IGN);
298 }
mtklein246ba3a2016-02-23 10:39:36 -0800299 }
300#endif
301
mtklein748ca3b2015-01-15 10:56:12 -0800302/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
mtklein114c3cd2015-01-15 10:15:02 -0800303
mtklein62bd1a62015-01-27 14:46:26 -0800304struct Gold : public SkString {
mtkleina82f5622015-02-20 12:30:19 -0800305 Gold() : SkString("") {}
mtklein3eed7dd2016-02-24 19:07:07 -0800306 Gold(const SkString& sink, const SkString& src,
307 const SkString& srcOptions, const SkString& name,
308 const SkString& md5)
mtklein62bd1a62015-01-27 14:46:26 -0800309 : SkString("") {
310 this->append(sink);
311 this->append(src);
djsollen54416de2015-04-03 07:24:48 -0700312 this->append(srcOptions);
mtklein62bd1a62015-01-27 14:46:26 -0800313 this->append(name);
314 this->append(md5);
mtklein62bd1a62015-01-27 14:46:26 -0800315 }
mtkleinc8d1dd42015-10-15 12:23:01 -0700316 struct Hash {
317 uint32_t operator()(const Gold& g) const {
318 return SkGoodHash()((const SkString&)g);
319 }
320 };
mtklein62bd1a62015-01-27 14:46:26 -0800321};
mtkleina82f5622015-02-20 12:30:19 -0800322static SkTHashSet<Gold, Gold::Hash> gGold;
mtklein62bd1a62015-01-27 14:46:26 -0800323
324static void add_gold(JsonWriter::BitmapResult r) {
djsollen54416de2015-04-03 07:24:48 -0700325 gGold.add(Gold(r.config, r.sourceType, r.sourceOptions, r.name, r.md5));
mtklein62bd1a62015-01-27 14:46:26 -0800326}
327
328static void gather_gold() {
329 if (!FLAGS_readPath.isEmpty()) {
330 SkString path(FLAGS_readPath[0]);
331 path.append("/dm.json");
332 if (!JsonWriter::ReadJson(path.c_str(), add_gold)) {
333 fail(SkStringPrintf("Couldn't read %s for golden results.", path.c_str()));
334 }
335 }
336}
337
338/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
339
mtklein935f1b12016-03-16 08:37:19 -0700340#if defined(SK_BUILD_FOR_WIN32)
341 static const char* kNewline = "\r\n";
342#else
343 static const char* kNewline = "\n";
344#endif
345
borenet09ed4802015-04-03 14:15:33 -0700346static SkTHashSet<SkString> gUninterestingHashes;
347
348static void gather_uninteresting_hashes() {
349 if (!FLAGS_uninterestingHashesFile.isEmpty()) {
bungeman38d909e2016-08-02 14:40:46 -0700350 sk_sp<SkData> data(SkData::MakeFromFileName(FLAGS_uninterestingHashesFile[0]));
mtkleincc334b32015-09-22 11:43:53 -0700351 if (!data) {
mtkleinc41fd922016-03-08 09:01:39 -0800352 info("WARNING: unable to read uninteresting hashes from %s\n",
353 FLAGS_uninterestingHashesFile[0]);
mtkleincc334b32015-09-22 11:43:53 -0700354 return;
355 }
Mike Klein45dcc0c2017-04-05 18:04:31 -0400356
357 // Copy to a string to make sure SkStrSplit has a terminating \0 to find.
358 SkString contents((const char*)data->data(), data->size());
359
borenet09ed4802015-04-03 14:15:33 -0700360 SkTArray<SkString> hashes;
Mike Klein45dcc0c2017-04-05 18:04:31 -0400361 SkStrSplit(contents.c_str(), kNewline, &hashes);
borenet09ed4802015-04-03 14:15:33 -0700362 for (const SkString& hash : hashes) {
363 gUninterestingHashes.add(hash);
364 }
mtkleinc41fd922016-03-08 09:01:39 -0800365 info("FYI: loaded %d distinct uninteresting hashes from %d lines\n",
366 gUninterestingHashes.count(), hashes.count());
borenet09ed4802015-04-03 14:15:33 -0700367 }
368}
369
370/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
371
Ben Wagner145dbcd2016-11-03 14:40:50 -0400372struct TaggedSrc : public std::unique_ptr<Src> {
mtklein3eed7dd2016-02-24 19:07:07 -0800373 SkString tag;
374 SkString options;
mtkleine0effd62015-07-29 06:37:28 -0700375};
376
Ben Wagner145dbcd2016-11-03 14:40:50 -0400377struct TaggedSink : public std::unique_ptr<Sink> {
kkinnunen3e980c32015-12-23 01:33:00 -0800378 SkString tag;
djsollen54416de2015-04-03 07:24:48 -0700379};
mtklein748ca3b2015-01-15 10:56:12 -0800380
381static const bool kMemcpyOK = true;
382
mtkleine0effd62015-07-29 06:37:28 -0700383static SkTArray<TaggedSrc, kMemcpyOK> gSrcs;
384static SkTArray<TaggedSink, kMemcpyOK> gSinks;
mtklein748ca3b2015-01-15 10:56:12 -0800385
mtklein6393c062015-04-27 08:45:01 -0700386static bool in_shard() {
387 static int N = 0;
388 return N++ % FLAGS_shards == FLAGS_shard;
389}
390
mtklein3eed7dd2016-02-24 19:07:07 -0800391static void push_src(const char* tag, ImplicitString options, Src* s) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400392 std::unique_ptr<Src> src(s);
mtklein6393c062015-04-27 08:45:01 -0700393 if (in_shard() &&
mtklein3eed7dd2016-02-24 19:07:07 -0800394 FLAGS_src.contains(tag) &&
mtklein748ca3b2015-01-15 10:56:12 -0800395 !SkCommandLineFlags::ShouldSkip(FLAGS_match, src->name().c_str())) {
mtkleine0effd62015-07-29 06:37:28 -0700396 TaggedSrc& s = gSrcs.push_back();
mtklein18300a32016-03-16 13:53:35 -0700397 s.reset(src.release());
mtklein748ca3b2015-01-15 10:56:12 -0800398 s.tag = tag;
djsollen54416de2015-04-03 07:24:48 -0700399 s.options = options;
mtklein114c3cd2015-01-15 10:15:02 -0800400 }
mtklein748ca3b2015-01-15 10:56:12 -0800401}
mtklein114c3cd2015-01-15 10:15:02 -0800402
msarett9e707a02015-09-01 14:57:57 -0700403static void push_codec_src(Path path, CodecSrc::Mode mode, CodecSrc::DstColorType dstColorType,
scroggoc5560be2016-02-03 09:42:42 -0800404 SkAlphaType dstAlphaType, float scale) {
scroggoe4499842016-02-25 11:03:47 -0800405 if (FLAGS_simpleCodec) {
scroggo19b91532016-10-24 09:03:26 -0700406 const bool simple = CodecSrc::kCodec_Mode == mode || CodecSrc::kAnimated_Mode == mode;
407 if (!simple || dstColorType != CodecSrc::kGetFromCanvas_DstColorType || scale != 1.0f) {
scroggoe4499842016-02-25 11:03:47 -0800408 // Only decode in the simple case.
409 return;
scroggo19b91532016-10-24 09:03:26 -0700410 }
scroggoe4499842016-02-25 11:03:47 -0800411 }
msarett9e707a02015-09-01 14:57:57 -0700412 SkString folder;
413 switch (mode) {
414 case CodecSrc::kCodec_Mode:
415 folder.append("codec");
416 break;
msarettbb25b532016-01-13 09:31:39 -0800417 case CodecSrc::kCodecZeroInit_Mode:
418 folder.append("codec_zero_init");
419 break;
msarett9e707a02015-09-01 14:57:57 -0700420 case CodecSrc::kScanline_Mode:
421 folder.append("scanline");
422 break;
msarett9e707a02015-09-01 14:57:57 -0700423 case CodecSrc::kStripe_Mode:
424 folder.append("stripe");
425 break;
msarett91c22b22016-02-22 12:27:46 -0800426 case CodecSrc::kCroppedScanline_Mode:
427 folder.append("crop");
428 break;
msarett9e707a02015-09-01 14:57:57 -0700429 case CodecSrc::kSubset_Mode:
msarett36c37962015-09-02 13:20:52 -0700430 folder.append("codec_subset");
msarett9e707a02015-09-01 14:57:57 -0700431 break;
scroggo19b91532016-10-24 09:03:26 -0700432 case CodecSrc::kAnimated_Mode:
433 folder.append("codec_animated");
434 break;
msarett9e707a02015-09-01 14:57:57 -0700435 }
436
437 switch (dstColorType) {
438 case CodecSrc::kGrayscale_Always_DstColorType:
439 folder.append("_kGray8");
440 break;
msarett34e0ec42016-04-22 16:27:24 -0700441 case CodecSrc::kNonNative8888_Always_DstColorType:
442 folder.append("_kNonNative");
443 break;
msarett9e707a02015-09-01 14:57:57 -0700444 default:
445 break;
446 }
447
scroggoc5560be2016-02-03 09:42:42 -0800448 switch (dstAlphaType) {
scroggoc5560be2016-02-03 09:42:42 -0800449 case kPremul_SkAlphaType:
450 folder.append("_premul");
451 break;
452 case kUnpremul_SkAlphaType:
453 folder.append("_unpremul");
454 break;
455 default:
456 break;
457 }
458
msarett9e707a02015-09-01 14:57:57 -0700459 if (1.0f != scale) {
460 folder.appendf("_%.3f", scale);
461 }
462
scroggoc5560be2016-02-03 09:42:42 -0800463 CodecSrc* src = new CodecSrc(path, mode, dstColorType, dstAlphaType, scale);
msarett9e707a02015-09-01 14:57:57 -0700464 push_src("image", folder, src);
465}
466
scroggof8dc9df2016-05-16 09:04:13 -0700467static void push_android_codec_src(Path path, CodecSrc::DstColorType dstColorType,
468 SkAlphaType dstAlphaType, int sampleSize) {
msarett3d9d7a72015-10-21 10:27:10 -0700469 SkString folder;
scroggof8dc9df2016-05-16 09:04:13 -0700470 folder.append("scaled_codec");
msarett3d9d7a72015-10-21 10:27:10 -0700471
472 switch (dstColorType) {
473 case CodecSrc::kGrayscale_Always_DstColorType:
474 folder.append("_kGray8");
475 break;
msarett34e0ec42016-04-22 16:27:24 -0700476 case CodecSrc::kNonNative8888_Always_DstColorType:
477 folder.append("_kNonNative");
478 break;
msarett3d9d7a72015-10-21 10:27:10 -0700479 default:
480 break;
481 }
482
scroggoc5560be2016-02-03 09:42:42 -0800483 switch (dstAlphaType) {
scroggoc5560be2016-02-03 09:42:42 -0800484 case kPremul_SkAlphaType:
485 folder.append("_premul");
486 break;
msarett9e9444c2016-02-03 12:39:10 -0800487 case kUnpremul_SkAlphaType:
488 folder.append("_unpremul");
489 break;
scroggoc5560be2016-02-03 09:42:42 -0800490 default:
491 break;
492 }
493
msarett3d9d7a72015-10-21 10:27:10 -0700494 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800495 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700496 }
497
scroggof8dc9df2016-05-16 09:04:13 -0700498 AndroidCodecSrc* src = new AndroidCodecSrc(path, dstColorType, dstAlphaType, sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700499 push_src("image", folder, src);
500}
501
msarett18976312016-03-09 14:20:58 -0800502static void push_image_gen_src(Path path, ImageGenSrc::Mode mode, SkAlphaType alphaType, bool isGpu)
503{
504 SkString folder;
505 switch (mode) {
506 case ImageGenSrc::kCodec_Mode:
507 folder.append("gen_codec");
508 break;
509 case ImageGenSrc::kPlatform_Mode:
510 folder.append("gen_platform");
511 break;
512 }
513
514 if (isGpu) {
515 folder.append("_gpu");
516 } else {
517 switch (alphaType) {
518 case kOpaque_SkAlphaType:
519 folder.append("_opaque");
520 break;
521 case kPremul_SkAlphaType:
522 folder.append("_premul");
523 break;
524 case kUnpremul_SkAlphaType:
525 folder.append("_unpremul");
526 break;
527 default:
528 break;
529 }
530 }
531
532 ImageGenSrc* src = new ImageGenSrc(path, mode, alphaType, isGpu);
533 push_src("image", folder, src);
534}
535
Leon Scroggins III07418182017-08-15 12:24:02 -0400536static void push_brd_src(Path path, CodecSrc::DstColorType dstColorType, BRDSrc::Mode mode,
537 uint32_t sampleSize) {
538 SkString folder("brd_android_codec");
539 switch (mode) {
540 case BRDSrc::kFullImage_Mode:
541 break;
542 case BRDSrc::kDivisor_Mode:
543 folder.append("_divisor");
544 break;
545 default:
546 SkASSERT(false);
547 return;
548 }
549
550 switch (dstColorType) {
551 case CodecSrc::kGetFromCanvas_DstColorType:
552 break;
553 case CodecSrc::kGrayscale_Always_DstColorType:
554 folder.append("_kGray");
555 break;
556 default:
557 SkASSERT(false);
558 return;
559 }
560
561 if (1 != sampleSize) {
562 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
563 }
564
565 BRDSrc* src = new BRDSrc(path, mode, dstColorType, sampleSize);
566 push_src("image", folder, src);
567}
568
569static void push_brd_srcs(Path path, bool gray) {
570 if (gray) {
571 // Only run grayscale to one sampleSize and Mode. Though interesting
572 // to test grayscale, it should not reveal anything across various
573 // sampleSizes and Modes
574 // Arbitrarily choose Mode and sampleSize.
575 push_brd_src(path, CodecSrc::kGrayscale_Always_DstColorType,
576 BRDSrc::kFullImage_Mode, 2);
577 }
578
579 // Test on a variety of sampleSizes, making sure to include:
580 // - 2, 4, and 8, which are natively supported by jpeg
581 // - multiples of 2 which are not divisible by 4 (analogous for 4)
582 // - larger powers of two, since BRD clients generally use powers of 2
583 // We will only produce output for the larger sizes on large images.
584 const uint32_t sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 24, 32, 64 };
585
586 const BRDSrc::Mode modes[] = { BRDSrc::kFullImage_Mode, BRDSrc::kDivisor_Mode, };
587
588 for (uint32_t sampleSize : sampleSizes) {
589 for (BRDSrc::Mode mode : modes) {
590 push_brd_src(path, CodecSrc::kGetFromCanvas_DstColorType, mode, sampleSize);
591 }
592 }
593}
594
msarett438b2ad2015-04-09 12:43:10 -0700595static void push_codec_srcs(Path path) {
bungeman38d909e2016-08-02 14:40:46 -0700596 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
msarett438b2ad2015-04-09 12:43:10 -0700597 if (!encoded) {
mtkleinc41fd922016-03-08 09:01:39 -0800598 info("Couldn't read %s.", path.c_str());
msarett438b2ad2015-04-09 12:43:10 -0700599 return;
600 }
Mike Reedede7bac2017-07-23 15:30:02 -0400601 std::unique_ptr<SkCodec> codec = SkCodec::MakeFromData(encoded);
halcanary96fcdcc2015-08-27 07:41:13 -0700602 if (nullptr == codec.get()) {
mtkleinc41fd922016-03-08 09:01:39 -0800603 info("Couldn't create codec for %s.", path.c_str());
msarett438b2ad2015-04-09 12:43:10 -0700604 return;
605 }
606
scroggo9366aff2016-05-20 08:38:54 -0700607 // native scaling is only supported by WEBP and JPEG
608 bool supportsNativeScaling = false;
msarett438b2ad2015-04-09 12:43:10 -0700609
msarett91c22b22016-02-22 12:27:46 -0800610 SkTArray<CodecSrc::Mode> nativeModes;
611 nativeModes.push_back(CodecSrc::kCodec_Mode);
612 nativeModes.push_back(CodecSrc::kCodecZeroInit_Mode);
msarett91c22b22016-02-22 12:27:46 -0800613 switch (codec->getEncodedFormat()) {
Hal Canarydb683012016-11-23 08:55:18 -0700614 case SkEncodedImageFormat::kJPEG:
msarett91c22b22016-02-22 12:27:46 -0800615 nativeModes.push_back(CodecSrc::kScanline_Mode);
616 nativeModes.push_back(CodecSrc::kStripe_Mode);
617 nativeModes.push_back(CodecSrc::kCroppedScanline_Mode);
scroggo9366aff2016-05-20 08:38:54 -0700618 supportsNativeScaling = true;
msarett91c22b22016-02-22 12:27:46 -0800619 break;
Hal Canarydb683012016-11-23 08:55:18 -0700620 case SkEncodedImageFormat::kWEBP:
msarett91c22b22016-02-22 12:27:46 -0800621 nativeModes.push_back(CodecSrc::kSubset_Mode);
scroggo9366aff2016-05-20 08:38:54 -0700622 supportsNativeScaling = true;
msarett91c22b22016-02-22 12:27:46 -0800623 break;
Hal Canarydb683012016-11-23 08:55:18 -0700624 case SkEncodedImageFormat::kDNG:
msarettb65e6042016-02-23 05:37:25 -0800625 break;
msarett91c22b22016-02-22 12:27:46 -0800626 default:
627 nativeModes.push_back(CodecSrc::kScanline_Mode);
msarett91c22b22016-02-22 12:27:46 -0800628 break;
629 }
msarett9e707a02015-09-01 14:57:57 -0700630
msarett91c22b22016-02-22 12:27:46 -0800631 SkTArray<CodecSrc::DstColorType> colorTypes;
632 colorTypes.push_back(CodecSrc::kGetFromCanvas_DstColorType);
msarett34e0ec42016-04-22 16:27:24 -0700633 colorTypes.push_back(CodecSrc::kNonNative8888_Always_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700634 switch (codec->getInfo().colorType()) {
635 case kGray_8_SkColorType:
msarett91c22b22016-02-22 12:27:46 -0800636 colorTypes.push_back(CodecSrc::kGrayscale_Always_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700637 break;
638 default:
msarett36c37962015-09-02 13:20:52 -0700639 break;
640 }
msarett9e707a02015-09-01 14:57:57 -0700641
scroggoc5560be2016-02-03 09:42:42 -0800642 SkTArray<SkAlphaType> alphaModes;
643 alphaModes.push_back(kPremul_SkAlphaType);
scroggof2c96a22016-05-20 11:08:27 -0700644 if (codec->getInfo().alphaType() != kOpaque_SkAlphaType) {
645 alphaModes.push_back(kUnpremul_SkAlphaType);
scroggoc5560be2016-02-03 09:42:42 -0800646 }
msarettb714fb02016-01-22 14:46:42 -0800647
648 for (CodecSrc::Mode mode : nativeModes) {
scroggo9366aff2016-05-20 08:38:54 -0700649 for (CodecSrc::DstColorType colorType : colorTypes) {
650 for (SkAlphaType alphaType : alphaModes) {
scroggof2c96a22016-05-20 11:08:27 -0700651 // Only test kCroppedScanline_Mode when the alpha type is premul. The test is
scroggo9366aff2016-05-20 08:38:54 -0700652 // slow and won't be interestingly different with different alpha types.
653 if (CodecSrc::kCroppedScanline_Mode == mode &&
scroggof2c96a22016-05-20 11:08:27 -0700654 kPremul_SkAlphaType != alphaType) {
scroggo9366aff2016-05-20 08:38:54 -0700655 continue;
656 }
msarett91c22b22016-02-22 12:27:46 -0800657
scroggo9366aff2016-05-20 08:38:54 -0700658 push_codec_src(path, mode, colorType, alphaType, 1.0f);
msarettf3dc1882016-04-26 13:06:38 -0700659
scroggo9366aff2016-05-20 08:38:54 -0700660 // Skip kNonNative on different native scales. It won't be interestingly
661 // different.
662 if (supportsNativeScaling &&
663 CodecSrc::kNonNative8888_Always_DstColorType == colorType) {
664 // Native Scales
665 // SkJpegCodec natively supports scaling to the following:
666 for (auto scale : { 0.125f, 0.25f, 0.375f, 0.5f, 0.625f, 0.750f, 0.875f }) {
667 push_codec_src(path, mode, colorType, alphaType, scale);
668 }
scroggoc5560be2016-02-03 09:42:42 -0800669 }
msarett9e707a02015-09-01 14:57:57 -0700670 }
671 }
msarett0a242972015-06-11 14:27:27 -0700672 }
msarett36c37962015-09-02 13:20:52 -0700673
scroggo19b91532016-10-24 09:03:26 -0700674 {
675 std::vector<SkCodec::FrameInfo> frameInfos = codec->getFrameInfo();
676 if (frameInfos.size() > 1) {
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400677 for (auto dstCT : { CodecSrc::kNonNative8888_Always_DstColorType,
678 CodecSrc::kGetFromCanvas_DstColorType }) {
679 for (auto at : { kUnpremul_SkAlphaType, kPremul_SkAlphaType }) {
680 push_codec_src(path, CodecSrc::kAnimated_Mode, dstCT, at, 1.0f);
681 }
682 }
scroggo19b91532016-10-24 09:03:26 -0700683 }
684
685 }
686
scroggoe4499842016-02-25 11:03:47 -0800687 if (FLAGS_simpleCodec) {
688 return;
689 }
690
msarett3d9d7a72015-10-21 10:27:10 -0700691 const int sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
msarett36c37962015-09-02 13:20:52 -0700692
msarett3d9d7a72015-10-21 10:27:10 -0700693 for (int sampleSize : sampleSizes) {
msarett91c22b22016-02-22 12:27:46 -0800694 for (CodecSrc::DstColorType colorType : colorTypes) {
scroggoc5560be2016-02-03 09:42:42 -0800695 for (SkAlphaType alphaType : alphaModes) {
msarettf3dc1882016-04-26 13:06:38 -0700696 // We can exercise all of the kNonNative support code in the swizzler with just a
697 // few sample sizes. Skip the rest.
698 if (CodecSrc::kNonNative8888_Always_DstColorType == colorType && sampleSize > 3) {
699 continue;
700 }
701
scroggof8dc9df2016-05-16 09:04:13 -0700702 push_android_codec_src(path, colorType, alphaType, sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700703 }
msarett36c37962015-09-02 13:20:52 -0700704 }
705 }
msarett18976312016-03-09 14:20:58 -0800706
Leon Scroggins III07418182017-08-15 12:24:02 -0400707 const char* ext = strrchr(path.c_str(), '.');
708 if (ext) {
709 ext++;
msarett18976312016-03-09 14:20:58 -0800710
Leon Scroggins III07418182017-08-15 12:24:02 -0400711 static const char* const rawExts[] = {
712 "arw", "cr2", "dng", "nef", "nrw", "orf", "raf", "rw2", "pef", "srw",
713 "ARW", "CR2", "DNG", "NEF", "NRW", "ORF", "RAF", "RW2", "PEF", "SRW",
714 };
715 for (const char* rawExt : rawExts) {
716 if (0 == strcmp(rawExt, ext)) {
717 // RAW is not supported by image generator (skbug.com/5079) or BRD.
718 return;
719 }
720 }
721
722 static const char* const brdExts[] = {
723 "jpg", "jpeg", "png", "webp",
724 "JPG", "JPEG", "PNG", "WEBP",
725 };
726 for (const char* brdExt : brdExts) {
727 if (0 == strcmp(brdExt, ext)) {
728 bool gray = codec->getEncodedInfo().color() == SkEncodedInfo::kGray_Color;
729 push_brd_srcs(path, gray);
730 break;
731 }
msarett18976312016-03-09 14:20:58 -0800732 }
733 }
734
735 // Push image generator GPU test.
brianosmanc9ced392016-03-24 07:27:43 -0700736 push_image_gen_src(path, ImageGenSrc::kCodec_Mode, codec->getInfo().alphaType(), true);
msarett18976312016-03-09 14:20:58 -0800737
738 // Push image generator CPU tests.
739 for (SkAlphaType alphaType : alphaModes) {
740 push_image_gen_src(path, ImageGenSrc::kCodec_Mode, alphaType, false);
741
742#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
Hal Canarydb683012016-11-23 08:55:18 -0700743 if (SkEncodedImageFormat::kWEBP != codec->getEncodedFormat() &&
744 SkEncodedImageFormat::kWBMP != codec->getEncodedFormat() &&
msarett18976312016-03-09 14:20:58 -0800745 kUnpremul_SkAlphaType != alphaType)
746 {
747 push_image_gen_src(path, ImageGenSrc::kPlatform_Mode, alphaType, false);
748 }
msarettfc0b6d12016-03-17 13:50:17 -0700749#elif defined(SK_BUILD_FOR_WIN)
Hal Canarydb683012016-11-23 08:55:18 -0700750 if (SkEncodedImageFormat::kWEBP != codec->getEncodedFormat() &&
751 SkEncodedImageFormat::kWBMP != codec->getEncodedFormat())
msarettfc0b6d12016-03-17 13:50:17 -0700752 {
753 push_image_gen_src(path, ImageGenSrc::kPlatform_Mode, alphaType, false);
754 }
msarett18976312016-03-09 14:20:58 -0800755#endif
756 }
msarett438b2ad2015-04-09 12:43:10 -0700757}
758
fmalitaa2b9fdf2016-08-03 19:53:36 -0700759template <typename T>
760void gather_file_srcs(const SkCommandLineFlags::StringArray& flags, const char* ext) {
761 for (int i = 0; i < flags.count(); i++) {
762 const char* path = flags[i];
763 if (sk_isdir(path)) {
764 SkOSFile::Iter it(path, ext);
765 for (SkString file; it.next(&file); ) {
766 push_src(ext, "", new T(SkOSPath::Join(path, file.c_str())));
767 }
768 } else {
769 push_src(ext, "", new T(path));
770 }
771 }
772}
773
scroggo86737142016-02-03 12:19:11 -0800774static bool gather_srcs() {
mtklein748ca3b2015-01-15 10:56:12 -0800775 for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) {
djsollen54416de2015-04-03 07:24:48 -0700776 push_src("gm", "", new GMSrc(r->factory()));
mtklein748ca3b2015-01-15 10:56:12 -0800777 }
scroggo86737142016-02-03 12:19:11 -0800778
Robert Phillipsad8a43f2017-08-30 12:06:35 -0400779 if (FLAGS_ddl) {
780 gather_file_srcs<DDLSKPSrc>(FLAGS_skps, "skp");
781 } else {
782 gather_file_srcs<SKPSrc>(FLAGS_skps, "skp");
783 }
fmalitaa2b9fdf2016-08-03 19:53:36 -0700784 gather_file_srcs<MSKPSrc>(FLAGS_mskps, "mskp");
785#if defined(SK_XML)
786 gather_file_srcs<SVGSrc>(FLAGS_svgs, "svg");
787#endif
halcanary45420a92016-06-02 12:41:14 -0700788
scroggo86737142016-02-03 12:19:11 -0800789 SkTArray<SkString> images;
msarett69deca82016-04-29 09:38:40 -0700790 if (!CollectImages(FLAGS_images, &images)) {
scroggo86737142016-02-03 12:19:11 -0800791 return false;
792 }
793
794 for (auto image : images) {
795 push_codec_srcs(image);
mtklein709d2c32015-01-15 08:30:25 -0800796 }
scroggo86737142016-02-03 12:19:11 -0800797
msarett69deca82016-04-29 09:38:40 -0700798 SkTArray<SkString> colorImages;
799 if (!CollectImages(FLAGS_colorImages, &colorImages)) {
800 return false;
801 }
802
803 for (auto colorImage : colorImages) {
msarett9ce3a542016-07-15 13:54:38 -0700804 ColorCodecSrc* src = new ColorCodecSrc(colorImage, ColorCodecSrc::kBaseline_Mode,
805 kN32_SkColorType);
msarettb8d1aac2016-08-04 12:22:06 -0700806 push_src("colorImage", "color_codec_baseline", src);
msarett888dc162016-05-23 10:21:17 -0700807
msarett9ce3a542016-07-15 13:54:38 -0700808 src = new ColorCodecSrc(colorImage, ColorCodecSrc::kDst_HPZR30w_Mode, kN32_SkColorType);
msarettb8d1aac2016-08-04 12:22:06 -0700809 push_src("colorImage", "color_codec_HPZR30w", src);
msarett9ce3a542016-07-15 13:54:38 -0700810 // TODO (msarett):
811 // 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 -0700812
msarett9ce3a542016-07-15 13:54:38 -0700813 src = new ColorCodecSrc(colorImage, ColorCodecSrc::kDst_sRGB_Mode, kN32_SkColorType);
msarettb8d1aac2016-08-04 12:22:06 -0700814 push_src("colorImage", "color_codec_sRGB_kN32", src);
msarett9ce3a542016-07-15 13:54:38 -0700815 src = new ColorCodecSrc(colorImage, ColorCodecSrc::kDst_sRGB_Mode, kRGBA_F16_SkColorType);
msarettb8d1aac2016-08-04 12:22:06 -0700816 push_src("colorImage", "color_codec_sRGB_kF16", src);
msarett69deca82016-04-29 09:38:40 -0700817 }
818
scroggo86737142016-02-03 12:19:11 -0800819 return true;
mtklein709d2c32015-01-15 08:30:25 -0800820}
821
kkinnunen3e980c32015-12-23 01:33:00 -0800822static void push_sink(const SkCommandLineConfig& config, Sink* s) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400823 std::unique_ptr<Sink> sink(s);
kkinnunen3e980c32015-12-23 01:33:00 -0800824
mtkleine0effd62015-07-29 06:37:28 -0700825 // Try a simple Src as a canary. If it fails, skip this sink.
mtklein748ca3b2015-01-15 10:56:12 -0800826 struct : public Src {
mtkleine0effd62015-07-29 06:37:28 -0700827 Error draw(SkCanvas* c) const override {
828 c->drawRect(SkRect::MakeWH(1,1), SkPaint());
829 return "";
830 }
mtklein36352bf2015-03-25 18:17:31 -0700831 SkISize size() const override { return SkISize::Make(16, 16); }
mtkleine0effd62015-07-29 06:37:28 -0700832 Name name() const override { return "justOneRect"; }
833 } justOneRect;
mtklein114c3cd2015-01-15 10:15:02 -0800834
mtklein748ca3b2015-01-15 10:56:12 -0800835 SkBitmap bitmap;
836 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800837 SkString log;
mtkleine0effd62015-07-29 06:37:28 -0700838 Error err = sink->draw(justOneRect, &bitmap, &stream, &log);
mtklein4089ef72015-03-05 08:40:28 -0800839 if (err.isFatal()) {
mtkleinc41fd922016-03-08 09:01:39 -0800840 info("Could not run %s: %s\n", config.getTag().c_str(), err.c_str());
mtklein05641a52015-04-21 10:49:13 -0700841 exit(1);
mtklein114c3cd2015-01-15 10:15:02 -0800842 }
843
mtkleine0effd62015-07-29 06:37:28 -0700844 TaggedSink& ts = gSinks.push_back();
mtklein18300a32016-03-16 13:53:35 -0700845 ts.reset(sink.release());
kkinnunen3e980c32015-12-23 01:33:00 -0800846 ts.tag = config.getTag();
mtklein748ca3b2015-01-15 10:56:12 -0800847}
848
849static bool gpu_supported() {
850#if SK_SUPPORT_GPU
851 return FLAGS_gpu;
852#else
853 return false;
854#endif
855}
kkinnunen9ebc3f02015-12-21 23:48:13 -0800856
csmartdalton008b9d82017-02-22 12:00:42 -0700857static Sink* create_sink(const GrContextOptions& grCtxOptions, const SkCommandLineConfig* config) {
kkinnunen3e980c32015-12-23 01:33:00 -0800858#if SK_SUPPORT_GPU
859 if (gpu_supported()) {
860 if (const SkCommandLineConfigGpu* gpuConfig = config->asConfigGpu()) {
bsalomon85b4b532016-04-05 11:06:27 -0700861 GrContextFactory::ContextType contextType = gpuConfig->getContextType();
csmartdaltone812d492017-02-21 12:36:05 -0700862 GrContextFactory::ContextOverrides contextOverrides = gpuConfig->getContextOverrides();
csmartdalton008b9d82017-02-22 12:00:42 -0700863 GrContextFactory testFactory(grCtxOptions);
csmartdaltone812d492017-02-21 12:36:05 -0700864 if (!testFactory.get(contextType, contextOverrides)) {
mtkleinc41fd922016-03-08 09:01:39 -0800865 info("WARNING: can not create GPU context for config '%s'. "
866 "GM tests will be skipped.\n", gpuConfig->getTag().c_str());
kkinnunen3e980c32015-12-23 01:33:00 -0800867 return nullptr;
868 }
Brian Osmanf9810662017-08-30 10:02:10 -0400869 if (gpuConfig->getTestThreading()) {
870 return new GPUThreadTestingSink(contextType, contextOverrides,
871 gpuConfig->getSamples(), gpuConfig->getUseDIText(),
872 gpuConfig->getColorType(),
873 gpuConfig->getAlphaType(),
874 sk_ref_sp(gpuConfig->getColorSpace()),
875 FLAGS_gpu_threading, grCtxOptions);
876 } else {
877 return new GPUSink(contextType, contextOverrides, gpuConfig->getSamples(),
878 gpuConfig->getUseDIText(), gpuConfig->getColorType(),
879 gpuConfig->getAlphaType(), sk_ref_sp(gpuConfig->getColorSpace()),
880 FLAGS_gpu_threading, grCtxOptions);
881 }
kkinnunen3e980c32015-12-23 01:33:00 -0800882 }
883 }
884#endif
885
886#define SINK(t, sink, ...) if (config->getBackend().equals(t)) { return new sink(__VA_ARGS__); }
887
mtklein748ca3b2015-01-15 10:56:12 -0800888 if (FLAGS_cpu) {
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500889 auto srgbColorSpace = SkColorSpace::MakeSRGB();
890 auto srgbLinearColorSpace = SkColorSpace::MakeSRGBLinear();
brianosmanb109b8c2016-06-16 13:03:24 -0700891
Matt Sarettd2228302017-03-02 08:53:46 -0500892 SINK("565", RasterSink, kRGB_565_SkColorType);
893 SINK("8888", RasterSink, kN32_SkColorType);
894 SINK("srgb", RasterSink, kN32_SkColorType, srgbColorSpace);
Matt Sarettd2228302017-03-02 08:53:46 -0500895 SINK("f16", RasterSink, kRGBA_F16_SkColorType, srgbLinearColorSpace);
Hal Canaryc7d295e2017-07-20 15:36:00 -0400896 SINK("pdf", PDFSink, false, SK_ScalarDefaultRasterDPI);
Matt Sarettd2228302017-03-02 08:53:46 -0500897 SINK("skp", SKPSink);
898 SINK("pipe", PipeSink);
899 SINK("svg", SVGSink);
900 SINK("null", NullSink);
901 SINK("xps", XPSSink);
Hal Canaryc7d295e2017-07-20 15:36:00 -0400902 SINK("pdfa", PDFSink, true, SK_ScalarDefaultRasterDPI);
903 SINK("pdf300", PDFSink, false, 300);
Hal Canary85c7fe82016-10-25 10:33:27 -0400904 SINK("jsdebug", DebugSink);
mtklein748ca3b2015-01-15 10:56:12 -0800905 }
906#undef SINK
halcanary96fcdcc2015-08-27 07:41:13 -0700907 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800908}
909
Mike Klein841101d2017-03-10 09:55:51 -0500910static sk_sp<SkColorSpace> adobe_rgb() {
911 return SkColorSpace::MakeRGB(SkColorSpace::kSRGB_RenderTargetGamma,
912 SkColorSpace::kAdobeRGB_Gamut);
913}
914
Mike Kleinb7ea3da2017-10-31 17:42:08 +0000915static sk_sp<SkColorSpace> rgb_to_gbr() {
916 float gbr[9];
917 gbr[0] = gSRGB_toXYZD50[1];
918 gbr[1] = gSRGB_toXYZD50[2];
919 gbr[2] = gSRGB_toXYZD50[0];
920 gbr[3] = gSRGB_toXYZD50[4];
921 gbr[4] = gSRGB_toXYZD50[5];
922 gbr[5] = gSRGB_toXYZD50[3];
923 gbr[6] = gSRGB_toXYZD50[7];
924 gbr[7] = gSRGB_toXYZD50[8];
925 gbr[8] = gSRGB_toXYZD50[6];
Mike Klein919cc452017-03-18 15:36:52 +0000926 SkMatrix44 toXYZD50(SkMatrix44::kUninitialized_Constructor);
Mike Kleinb7ea3da2017-10-31 17:42:08 +0000927 toXYZD50.set3x3RowMajorf(gbr);
Mike Klein919cc452017-03-18 15:36:52 +0000928 return SkColorSpace::MakeRGB(SkColorSpace::kSRGB_RenderTargetGamma, toXYZD50);
929}
930
kkinnunen3e980c32015-12-23 01:33:00 -0800931static Sink* create_via(const SkString& tag, Sink* wrapped) {
932#define VIA(t, via, ...) if (tag.equals(t)) { return new via(__VA_ARGS__); }
Mike Klein919cc452017-03-18 15:36:52 +0000933 VIA("adobe", ViaCSXform, wrapped, adobe_rgb(), false);
Mike Kleinb7ea3da2017-10-31 17:42:08 +0000934 VIA("gbr", ViaCSXform, wrapped, rgb_to_gbr(), true);
mtklein9c5052f2016-08-06 12:51:51 -0700935 VIA("lite", ViaLite, wrapped);
reed54dc4872016-09-13 08:09:45 -0700936 VIA("pipe", ViaPipe, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700937 VIA("twice", ViaTwice, 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("2ndpic", ViaSecondPicture, wrapped);
mtkleind31c13d2015-05-05 12:59:56 -0700944 VIA("sp", ViaSingletonPictures, wrapped);
reedbabc3de2016-07-08 08:43:27 -0700945 VIA("defer", ViaDefer, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700946 VIA("tiles", ViaTiles, 256, 256, nullptr, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800947 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800948
mtkleind603b222015-02-17 11:13:33 -0800949 if (FLAGS_matrix.count() == 4) {
mtklein748ca3b2015-01-15 10:56:12 -0800950 SkMatrix m;
mtkleind603b222015-02-17 11:13:33 -0800951 m.reset();
952 m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
953 m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
954 m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
955 m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
956 VIA("matrix", ViaMatrix, m, wrapped);
957 VIA("upright", ViaUpright, m, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800958 }
tomhudson64de1e12015-03-05 08:01:07 -0800959
mtklein748ca3b2015-01-15 10:56:12 -0800960#undef VIA
halcanary96fcdcc2015-08-27 07:41:13 -0700961 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800962}
963
Ben Wagnerf28d4722017-08-11 17:33:22 -0400964static bool gather_sinks(const GrContextOptions& grCtxOptions, bool defaultConfigs) {
kkinnunen3e980c32015-12-23 01:33:00 -0800965 SkCommandLineConfigArray configs;
966 ParseConfigs(FLAGS_config, &configs);
967 for (int i = 0; i < configs.count(); i++) {
968 const SkCommandLineConfig& config = *configs[i];
csmartdalton008b9d82017-02-22 12:00:42 -0700969 Sink* sink = create_sink(grCtxOptions, &config);
kkinnunen3e980c32015-12-23 01:33:00 -0800970 if (sink == nullptr) {
mtkleinc41fd922016-03-08 09:01:39 -0800971 info("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
972 config.getTag().c_str());
kkinnunen3e980c32015-12-23 01:33:00 -0800973 continue;
974 }
mtklein748ca3b2015-01-15 10:56:12 -0800975
kkinnunen3e980c32015-12-23 01:33:00 -0800976 const SkTArray<SkString>& parts = config.getViaParts();
977 for (int j = parts.count(); j-- > 0;) {
978 const SkString& part = parts[j];
979 Sink* next = create_via(part, sink);
halcanary96fcdcc2015-08-27 07:41:13 -0700980 if (next == nullptr) {
mtkleinc41fd922016-03-08 09:01:39 -0800981 info("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
982 part.c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800983 delete sink;
halcanary96fcdcc2015-08-27 07:41:13 -0700984 sink = nullptr;
mtklein748ca3b2015-01-15 10:56:12 -0800985 break;
986 }
987 sink = next;
988 }
989 if (sink) {
990 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800991 }
992 }
brianosman05c987c2016-05-09 13:42:16 -0700993
994 // If no configs were requested (just running tests, perhaps?), then we're okay.
Ben Wagnerf28d4722017-08-11 17:33:22 -0400995 if (configs.count() == 0 ||
996 // If we're using the default configs, we're okay.
997 defaultConfigs ||
Ben Wagner32fa5102017-08-10 21:25:55 -0400998 // If we've been told to ignore undefined flags, we're okay.
999 FLAGS_undefok ||
1000 // Otherwise, make sure that all specified configs have become sinks.
1001 configs.count() == gSinks.count()) {
1002 return true;
1003 }
1004 info("Invalid --config. Use --undefok to bypass this warning.\n");
1005 return false;
mtklein114c3cd2015-01-15 10:15:02 -08001006}
mtklein709d2c32015-01-15 08:30:25 -08001007
mtkleina5114d72015-08-24 13:27:01 -07001008static bool dump_png(SkBitmap bitmap, const char* path, const char* md5) {
Mike Klein13b6afd2017-06-12 12:45:41 -04001009 SkPixmap pm;
1010 if (!bitmap.peekPixels(&pm)) {
1011 return false; // Ought to never happen... we're already read-back at this point.
mtkleina5114d72015-08-24 13:27:01 -07001012 }
Mike Klein13b6afd2017-06-12 12:45:41 -04001013 SkFILEWStream dst{path};
mtkleina5114d72015-08-24 13:27:01 -07001014
mtkleind69ece62015-09-10 10:37:44 -07001015 SkString description;
1016 description.append("Key: ");
1017 for (int i = 0; i < FLAGS_key.count(); i++) {
1018 description.appendf("%s ", FLAGS_key[i]);
1019 }
1020 description.append("Properties: ");
1021 for (int i = 0; i < FLAGS_properties.count(); i++) {
1022 description.appendf("%s ", FLAGS_properties[i]);
1023 }
1024 description.appendf("MD5: %s", md5);
1025
Mike Klein13b6afd2017-06-12 12:45:41 -04001026 const char* comments[] = {
1027 "Author", "DM dump_png()",
1028 "Description", description.c_str(),
1029 };
1030 size_t lengths[] = {
1031 strlen(comments[0])+1, strlen(comments[1])+1,
1032 strlen(comments[2])+1, strlen(comments[3])+1,
1033 };
mtkleina5114d72015-08-24 13:27:01 -07001034
Mike Klein13b6afd2017-06-12 12:45:41 -04001035 SkPngEncoder::Options options;
1036 options.fComments = SkDataTable::MakeCopyArrays((const void**)comments, lengths, 4);
1037 options.fFilterFlags = SkPngEncoder::FilterFlag::kNone;
1038 options.fZLibLevel = 1;
1039 options.fUnpremulBehavior = pm.colorSpace() ? SkTransferFunctionBehavior::kRespect
1040 : SkTransferFunctionBehavior::kIgnore;
1041 return SkPngEncoder::Encode(&dst, pm, options);
mtkleina5114d72015-08-24 13:27:01 -07001042}
1043
mtkleina2ef6422015-01-15 13:44:22 -08001044static bool match(const char* needle, const char* haystack) {
Chris Daltonecf78ac2017-08-15 15:43:08 -06001045 if ('~' == needle[0]) {
1046 return !match(needle + 1, haystack);
1047 }
1048 if (0 == strcmp("_", needle)) {
1049 return true;
1050 }
1051 return nullptr != strstr(haystack, needle);
mtkleina2ef6422015-01-15 13:44:22 -08001052}
1053
mtklein3eed7dd2016-02-24 19:07:07 -08001054static bool is_blacklisted(const char* sink, const char* src,
1055 const char* srcOptions, const char* name) {
mtklein0d243ff2015-04-03 07:51:00 -07001056 for (int i = 0; i < FLAGS_blacklist.count() - 3; i += 4) {
mtkleina2ef6422015-01-15 13:44:22 -08001057 if (match(FLAGS_blacklist[i+0], sink) &&
djsollen54416de2015-04-03 07:24:48 -07001058 match(FLAGS_blacklist[i+1], src) &&
1059 match(FLAGS_blacklist[i+2], srcOptions) &&
1060 match(FLAGS_blacklist[i+3], name)) {
mtklein3eed7dd2016-02-24 19:07:07 -08001061 return true;
mtkleina2ef6422015-01-15 13:44:22 -08001062 }
1063 }
mtklein3eed7dd2016-02-24 19:07:07 -08001064 return false;
mtkleina2ef6422015-01-15 13:44:22 -08001065}
1066
mtkleincd50bca2016-01-05 06:20:20 -08001067// Even when a Task Sink reports to be non-threadsafe (e.g. GPU), we know things like
1068// .png encoding are definitely thread safe. This lets us offload that work to CPU threads.
1069static SkTaskGroup gDefinitelyThreadSafeWork;
1070
mtklein748ca3b2015-01-15 10:56:12 -08001071// The finest-grained unit of work we can run: draw a single Src into a single Sink,
1072// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
1073struct Task {
mtkleine0effd62015-07-29 06:37:28 -07001074 Task(const TaggedSrc& src, const TaggedSink& sink) : src(src), sink(sink) {}
1075 const TaggedSrc& src;
1076 const TaggedSink& sink;
mtklein748ca3b2015-01-15 10:56:12 -08001077
mtklein21eaf3b2016-02-08 12:39:59 -08001078 static void Run(const Task& task) {
1079 SkString name = task.src->name();
mtkleine0effd62015-07-29 06:37:28 -07001080
mtkleinb9eb4ac2015-02-02 18:26:03 -08001081 SkString log;
mtklein3eed7dd2016-02-24 19:07:07 -08001082 if (!FLAGS_dryRun) {
mtklein748ca3b2015-01-15 10:56:12 -08001083 SkBitmap bitmap;
1084 SkDynamicMemoryWStream stream;
mtklein3eed7dd2016-02-24 19:07:07 -08001085 start(task.sink.tag.c_str(), task.src.tag.c_str(),
1086 task.src.options.c_str(), name.c_str());
mtklein21eaf3b2016-02-08 12:39:59 -08001087 Error err = task.sink->draw(*task.src, &bitmap, &stream, &log);
mtkleinb3b13b72016-03-07 13:20:52 -08001088 if (!log.isEmpty()) {
mtkleinc41fd922016-03-08 09:01:39 -08001089 info("%s %s %s %s:\n%s\n", task.sink.tag.c_str()
1090 , task.src.tag.c_str()
1091 , task.src.options.c_str()
1092 , name.c_str()
1093 , log.c_str());
mtkleinb3b13b72016-03-07 13:20:52 -08001094 }
mtklein748ca3b2015-01-15 10:56:12 -08001095 if (!err.isEmpty()) {
mtklein4089ef72015-03-05 08:40:28 -08001096 if (err.isFatal()) {
djsollen54416de2015-04-03 07:24:48 -07001097 fail(SkStringPrintf("%s %s %s %s: %s",
mtklein21eaf3b2016-02-08 12:39:59 -08001098 task.sink.tag.c_str(),
1099 task.src.tag.c_str(),
1100 task.src.options.c_str(),
mtklein4089ef72015-03-05 08:40:28 -08001101 name.c_str(),
1102 err.c_str()));
mtkleinb37cb412015-03-18 05:27:14 -07001103 } else {
mtklein3eed7dd2016-02-24 19:07:07 -08001104 done(task.sink.tag.c_str(), task.src.tag.c_str(),
1105 task.src.options.c_str(), name.c_str());
mtkleinba6ada72016-01-21 09:39:35 -08001106 return;
mtklein4089ef72015-03-05 08:40:28 -08001107 }
mtklein748ca3b2015-01-15 10:56:12 -08001108 }
mtklein62bd1a62015-01-27 14:46:26 -08001109
mtkleincd50bca2016-01-05 06:20:20 -08001110 // We're likely switching threads here, so we must capture by value, [=] or [foo,bar].
Hal Canary0b833192017-03-14 14:35:02 -04001111 SkStreamAsset* data = stream.detachAsStream().release();
mtkleincd50bca2016-01-05 06:20:20 -08001112 gDefinitelyThreadSafeWork.add([task,name,bitmap,data]{
Ben Wagner145dbcd2016-11-03 14:40:50 -04001113 std::unique_ptr<SkStreamAsset> ownedData(data);
mtkleincd50bca2016-01-05 06:20:20 -08001114
mtkleincd50bca2016-01-05 06:20:20 -08001115 SkString md5;
1116 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
1117 SkMD5 hash;
1118 if (data->getLength()) {
1119 hash.writeStream(data, data->getLength());
1120 data->rewind();
mtklein38408462015-07-08 07:25:27 -07001121 } else {
mtkleincd50bca2016-01-05 06:20:20 -08001122 // If we're BGRA (Linux, Windows), swizzle over to RGBA (Mac, Android).
1123 // This helps eliminate multiple 0-pixel-diff hashes on gold.skia.org.
1124 // (Android's general slow speed breaks the tie arbitrarily in RGBA's favor.)
1125 // We might consider promoting 565 to RGBA too.
1126 if (bitmap.colorType() == kBGRA_8888_SkColorType) {
1127 SkBitmap swizzle;
Matt Sarett68b8e3d2017-04-28 11:15:22 -04001128 SkAssertResult(sk_tool_utils::copy_to(&swizzle, kRGBA_8888_SkColorType,
1129 bitmap));
Mike Reedf0ffb892017-10-03 14:47:21 -04001130 hash.write(swizzle.getPixels(), swizzle.computeByteSize());
mtkleincd50bca2016-01-05 06:20:20 -08001131 } else {
Mike Reedf0ffb892017-10-03 14:47:21 -04001132 hash.write(bitmap.getPixels(), bitmap.computeByteSize());
mtkleincd50bca2016-01-05 06:20:20 -08001133 }
1134 }
1135 SkMD5::Digest digest;
1136 hash.finish(digest);
1137 for (int i = 0; i < 16; i++) {
1138 md5.appendf("%02x", digest.data[i]);
mtklein38408462015-07-08 07:25:27 -07001139 }
mtklein62bd1a62015-01-27 14:46:26 -08001140 }
mtklein62bd1a62015-01-27 14:46:26 -08001141
mtkleincd50bca2016-01-05 06:20:20 -08001142 if (!FLAGS_readPath.isEmpty() &&
mtklein3eed7dd2016-02-24 19:07:07 -08001143 !gGold.contains(Gold(task.sink.tag, task.src.tag,
1144 task.src.options, name, md5))) {
mtkleincd50bca2016-01-05 06:20:20 -08001145 fail(SkStringPrintf("%s not found for %s %s %s %s in %s",
1146 md5.c_str(),
mtklein21eaf3b2016-02-08 12:39:59 -08001147 task.sink.tag.c_str(),
1148 task.src.tag.c_str(),
1149 task.src.options.c_str(),
mtkleincd50bca2016-01-05 06:20:20 -08001150 name.c_str(),
1151 FLAGS_readPath[0]));
mtklein748ca3b2015-01-15 10:56:12 -08001152 }
mtkleincd50bca2016-01-05 06:20:20 -08001153
1154 if (!FLAGS_writePath.isEmpty()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001155 const char* ext = task.sink->fileExtension();
Brian Osmanf1942de2017-08-30 14:50:22 -04001156 if (ext && !FLAGS_dont_write.contains(ext)) {
Mike Klein97d6a7a2017-07-24 10:37:19 -04001157 if (data->getLength()) {
1158 WriteToDisk(task, md5, ext, data, data->getLength(), nullptr);
1159 SkASSERT(bitmap.drawsNothing());
1160 } else if (!bitmap.drawsNothing()) {
1161 WriteToDisk(task, md5, ext, nullptr, 0, &bitmap);
1162 }
mtkleincd50bca2016-01-05 06:20:20 -08001163 }
1164 }
1165 });
mtklein748ca3b2015-01-15 10:56:12 -08001166 }
mtklein3eed7dd2016-02-24 19:07:07 -08001167 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 -08001168 }
1169
1170 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -08001171 SkString md5,
1172 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -08001173 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -08001174 const SkBitmap* bitmap) {
mtklein409d4702016-02-29 07:38:01 -08001175 bool gammaCorrect = false;
1176 if (bitmap) {
Brian Osman7039f742016-11-01 15:56:16 -04001177 gammaCorrect = SkToBool(bitmap->info().colorSpace());
mtklein409d4702016-02-29 07:38:01 -08001178 }
1179
mtklein748ca3b2015-01-15 10:56:12 -08001180 JsonWriter::BitmapResult result;
djsollen54416de2015-04-03 07:24:48 -07001181 result.name = task.src->name();
mtklein3eed7dd2016-02-24 19:07:07 -08001182 result.config = task.sink.tag;
djsollen54416de2015-04-03 07:24:48 -07001183 result.sourceType = task.src.tag;
1184 result.sourceOptions = task.src.options;
1185 result.ext = ext;
mtklein409d4702016-02-29 07:38:01 -08001186 result.gammaCorrect = gammaCorrect;
djsollen54416de2015-04-03 07:24:48 -07001187 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -08001188 JsonWriter::AddBitmapResult(result);
1189
mtkleinb0531a72015-04-07 13:38:48 -07001190 // If an MD5 is uninteresting, we want it noted in the JSON file,
1191 // but don't want to dump it out as a .png (or whatever ext is).
1192 if (gUninterestingHashes.contains(md5)) {
1193 return;
1194 }
1195
mtklein748ca3b2015-01-15 10:56:12 -08001196 const char* dir = FLAGS_writePath[0];
1197 if (0 == strcmp(dir, "@")) { // Needed for iOS.
1198 dir = FLAGS_resourcePath[0];
1199 }
1200 sk_mkdir(dir);
1201
1202 SkString path;
1203 if (FLAGS_nameByHash) {
1204 path = SkOSPath::Join(dir, result.md5.c_str());
1205 path.append(".");
1206 path.append(ext);
1207 if (sk_exists(path.c_str())) {
1208 return; // Content-addressed. If it exists already, we're done.
1209 }
1210 } else {
kkinnunen3e980c32015-12-23 01:33:00 -08001211 path = SkOSPath::Join(dir, task.sink.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001212 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001213 path = SkOSPath::Join(path.c_str(), task.src.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001214 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001215 if (strcmp(task.src.options.c_str(), "") != 0) {
1216 path = SkOSPath::Join(path.c_str(), task.src.options.c_str());
djsollen54416de2015-04-03 07:24:48 -07001217 sk_mkdir(path.c_str());
1218 }
mtklein748ca3b2015-01-15 10:56:12 -08001219 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
1220 path.append(".");
1221 path.append(ext);
1222 }
1223
mtklein748ca3b2015-01-15 10:56:12 -08001224 if (bitmap) {
mtkleina5114d72015-08-24 13:27:01 -07001225 if (!dump_png(*bitmap, path.c_str(), result.md5.c_str())) {
mtklein748ca3b2015-01-15 10:56:12 -08001226 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
1227 return;
1228 }
1229 } else {
mtkleina5114d72015-08-24 13:27:01 -07001230 SkFILEWStream file(path.c_str());
1231 if (!file.isValid()) {
1232 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
1233 return;
1234 }
mtklein748ca3b2015-01-15 10:56:12 -08001235 if (!file.writeStream(data, len)) {
1236 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
1237 return;
1238 }
1239 }
1240 }
1241};
1242
mtklein748ca3b2015-01-15 10:56:12 -08001243/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1244
1245// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
1246
mtklein21eaf3b2016-02-08 12:39:59 -08001247static SkTDArray<skiatest::Test> gParallelTests, gSerialTests;
mtklein748ca3b2015-01-15 10:56:12 -08001248
1249static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -08001250 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -08001251 return;
1252 }
mtklein6393c062015-04-27 08:45:01 -07001253 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r; r = r->next()) {
1254 if (!in_shard()) {
1255 continue;
1256 }
halcanary87f3ba42015-01-20 09:30:20 -08001257 // Despite its name, factory() is returning a reference to
1258 // link-time static const POD data.
1259 const skiatest::Test& test = r->factory();
1260 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -08001261 continue;
1262 }
halcanary87f3ba42015-01-20 09:30:20 -08001263 if (test.needsGpu && gpu_supported()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001264 (FLAGS_gpu_threading ? gParallelTests : gSerialTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -08001265 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein21eaf3b2016-02-08 12:39:59 -08001266 gParallelTests.push(test);
mtklein82d28432015-01-15 12:46:02 -08001267 }
mtklein748ca3b2015-01-15 10:56:12 -08001268 }
1269}
1270
csmartdalton008b9d82017-02-22 12:00:42 -07001271static void run_test(skiatest::Test test, const GrContextOptions& grCtxOptions) {
halcanary87f3ba42015-01-20 09:30:20 -08001272 struct : public skiatest::Reporter {
mtklein36352bf2015-03-25 18:17:31 -07001273 void reportFailed(const skiatest::Failure& failure) override {
halcanary87f3ba42015-01-20 09:30:20 -08001274 fail(failure.toString());
1275 JsonWriter::AddTestFailure(failure);
1276 }
mtklein36352bf2015-03-25 18:17:31 -07001277 bool allowExtendedTest() const override {
halcanary87f3ba42015-01-20 09:30:20 -08001278 return FLAGS_pathOpsExtended;
1279 }
mtklein36352bf2015-03-25 18:17:31 -07001280 bool verbose() const override { return FLAGS_veryVerbose; }
halcanary87f3ba42015-01-20 09:30:20 -08001281 } reporter;
djsollen824996a2015-06-12 12:06:22 -07001282
mtklein3eed7dd2016-02-24 19:07:07 -08001283 if (!FLAGS_dryRun && !is_blacklisted("_", "tests", "_", test.name)) {
Robert Phillipsec325342017-10-30 18:02:48 +00001284 GrContextOptions options = grCtxOptions;
1285 test.modifyGrContextOptions(&options);
1286
mtklein21eaf3b2016-02-08 12:39:59 -08001287 start("unit", "test", "", test.name);
Robert Phillipsec325342017-10-30 18:02:48 +00001288 GrContextFactory factory(options);
Mike Kleinb323a5e2017-07-24 15:21:31 -04001289 test.run(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -08001290 }
mtklein3eed7dd2016-02-24 19:07:07 -08001291 done("unit", "test", "", test.name);
mtklein748ca3b2015-01-15 10:56:12 -08001292}
1293
1294/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1295
caryclark83ca6282015-06-10 09:31:09 -07001296#define PORTABLE_FONT_PREFIX "Toy Liberation "
1297
mbocee6a9912016-05-31 11:42:36 -07001298static sk_sp<SkTypeface> create_from_name(const char familyName[], SkFontStyle style) {
caryclark83ca6282015-06-10 09:31:09 -07001299 if (familyName && strlen(familyName) > sizeof(PORTABLE_FONT_PREFIX)
1300 && !strncmp(familyName, PORTABLE_FONT_PREFIX, sizeof(PORTABLE_FONT_PREFIX) - 1)) {
caryclark1818acb2015-07-24 12:09:25 -07001301 return sk_tool_utils::create_portable_typeface(familyName, style);
caryclark83ca6282015-06-10 09:31:09 -07001302 }
halcanary96fcdcc2015-08-27 07:41:13 -07001303 return nullptr;
caryclark83ca6282015-06-10 09:31:09 -07001304}
1305
1306#undef PORTABLE_FONT_PREFIX
1307
mbocee6a9912016-05-31 11:42:36 -07001308extern sk_sp<SkTypeface> (*gCreateTypefaceDelegate)(const char [], SkFontStyle );
caryclark83ca6282015-06-10 09:31:09 -07001309
Mike Kleinbe28ee22017-02-06 12:46:20 -05001310int main(int argc, char** argv) {
1311 SkCommandLineFlags::Parse(argc, argv);
Brian Osman53136aa2017-07-20 15:43:35 -04001312
Brian Osmanbc8150f2017-07-24 11:38:01 -04001313 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -04001314
Mike Klein135a1b12017-08-15 13:13:59 -04001315#if !defined(GOOGLE3) && defined(SK_BUILD_FOR_IOS)
Mike Kleinadacaef2017-02-06 09:26:14 -05001316 cd_Documents();
1317#endif
bsalomon71de3532016-04-26 14:27:21 -07001318 setbuf(stdout, nullptr);
mtklein246ba3a2016-02-23 10:39:36 -08001319 setup_crash_handler();
mtkleinafae30a2016-02-24 12:28:32 -08001320
Yuqian Liba62b4a2016-12-14 13:46:53 -05001321 gSkUseAnalyticAA = FLAGS_analyticAA;
Yuqian Lidf60e362017-07-25 11:26:31 -04001322 gSkUseDeltaAA = FLAGS_deltaAA;
liyuqian38911a72016-10-04 11:23:22 -07001323
Yuqian Li550148b2017-01-13 10:13:13 -05001324 if (FLAGS_forceAnalyticAA) {
1325 gSkForceAnalyticAA = true;
1326 }
Yuqian Lia81b6262017-09-06 17:10:05 -04001327 if (FLAGS_forceDeltaAA) {
1328 gSkForceDeltaAA = true;
1329 }
Mike Reed7c26ca72017-07-05 15:45:52 -04001330 if (FLAGS_forceRasterPipeline) {
1331 gSkForceRasterPipelineBlitter = true;
1332 }
Yuqian Li550148b2017-01-13 10:13:13 -05001333
Mike Kleine9f187b2017-06-26 13:23:12 -04001334 // The bots like having a verbose.log to upload, so always touch the file even if --verbose.
1335 if (!FLAGS_writePath.isEmpty()) {
mtklein51c8cfc2016-03-11 12:59:09 -08001336 sk_mkdir(FLAGS_writePath[0]);
mtkleina1ce2162016-07-22 12:23:46 -07001337 gVLog = fopen(SkOSPath::Join(FLAGS_writePath[0], "verbose.log").c_str(), "w");
mtkleinc41fd922016-03-08 09:01:39 -08001338 }
Mike Kleine9f187b2017-06-26 13:23:12 -04001339 if (FLAGS_verbose) {
1340 gVLog = stderr;
1341 }
mtkleinc41fd922016-03-08 09:01:39 -08001342
csmartdalton008b9d82017-02-22 12:00:42 -07001343 GrContextOptions grCtxOptions;
1344#if SK_SUPPORT_GPU
1345 grCtxOptions.fGpuPathRenderers = CollectGpuPathRenderersFromFlags();
Chris Dalton7a0ebfc2017-10-13 12:35:50 -06001346 grCtxOptions.fAllowPathMaskCaching = FLAGS_cachePathMasks;
Brian Osmanf9810662017-08-30 10:02:10 -04001347 grCtxOptions.fExecutor = GpuExecutorForTools();
csmartdalton008b9d82017-02-22 12:00:42 -07001348#endif
1349
mtklein5286f022016-01-22 08:18:14 -08001350 JsonWriter::DumpJson(); // It's handy for the bots to assume this is ~never missing.
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +00001351 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -07001352 SkTaskGroup::Enabler enabled(FLAGS_threads);
caryclark83ca6282015-06-10 09:31:09 -07001353 gCreateTypefaceDelegate = &create_from_name;
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +00001354
halcanary7d571242016-02-24 17:59:16 -08001355 {
1356 SkString testResourcePath = GetResourcePath("color_wheel.png");
1357 SkFILEStream testResource(testResourcePath.c_str());
1358 if (!testResource.isValid()) {
mtkleinc41fd922016-03-08 09:01:39 -08001359 info("Some resources are missing. Do you need to set --resourcePath?\n");
halcanary7d571242016-02-24 17:59:16 -08001360 }
1361 }
mtklein62bd1a62015-01-27 14:46:26 -08001362 gather_gold();
borenet09ed4802015-04-03 14:15:33 -07001363 gather_uninteresting_hashes();
mtklein62bd1a62015-01-27 14:46:26 -08001364
scroggo86737142016-02-03 12:19:11 -08001365 if (!gather_srcs()) {
1366 return 1;
1367 }
Ben Wagnerf28d4722017-08-11 17:33:22 -04001368 // TODO(dogben): This is a bit ugly. Find a cleaner way to do this.
1369 bool defaultConfigs = true;
1370 for (int i = 0; i < argc; i++) {
1371 static const char* kConfigArg = "--config";
1372 if (strcmp(argv[i], kConfigArg) == 0) {
1373 defaultConfigs = false;
1374 break;
1375 }
1376 }
1377 if (!gather_sinks(grCtxOptions, defaultConfigs)) {
brianosman05c987c2016-05-09 13:42:16 -07001378 return 1;
1379 }
mtklein748ca3b2015-01-15 10:56:12 -08001380 gather_tests();
mtklein21eaf3b2016-02-08 12:39:59 -08001381 gPending = gSrcs.count() * gSinks.count() + gParallelTests.count() + gSerialTests.count();
Brian Osmand0677bc2017-08-03 12:13:47 -04001382 info("%d srcs * %d sinks + %d tests == %d tasks\n",
mtkleinc41fd922016-03-08 09:01:39 -08001383 gSrcs.count(), gSinks.count(), gParallelTests.count() + gSerialTests.count(), gPending);
mtklein748ca3b2015-01-15 10:56:12 -08001384
mtklein21eaf3b2016-02-08 12:39:59 -08001385 // Kick off as much parallel work as we can, making note of any serial work we'll need to do.
1386 SkTaskGroup parallel;
1387 SkTArray<Task> serial;
1388
1389 for (auto& sink : gSinks)
1390 for (auto& src : gSrcs) {
mtklein3eed7dd2016-02-24 19:07:07 -08001391 if (src->veto(sink->flags()) ||
1392 is_blacklisted(sink.tag.c_str(), src.tag.c_str(),
1393 src.options.c_str(), src->name().c_str())) {
mtkleinf10637f2016-06-10 13:56:35 -07001394 SkAutoMutexAcquire lock(gMutex);
mtklein3eed7dd2016-02-24 19:07:07 -08001395 gPending--;
1396 continue;
1397 }
1398
mtklein21eaf3b2016-02-08 12:39:59 -08001399 Task task(src, sink);
1400 if (src->serial() || sink->serial()) {
1401 serial.push_back(task);
1402 } else {
1403 parallel.add([task] { Task::Run(task); });
mtklein748ca3b2015-01-15 10:56:12 -08001404 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001405 }
mtklein21eaf3b2016-02-08 12:39:59 -08001406 for (auto test : gParallelTests) {
csmartdalton008b9d82017-02-22 12:00:42 -07001407 parallel.add([test, grCtxOptions] { run_test(test, grCtxOptions); });
mtklein55e88b22015-01-21 15:50:13 -08001408 }
mtklein21eaf3b2016-02-08 12:39:59 -08001409
1410 // With the parallel work running, run serial tasks and tests here on main thread.
1411 for (auto task : serial) { Task::Run(task); }
csmartdalton008b9d82017-02-22 12:00:42 -07001412 for (auto test : gSerialTests) { run_test(test, grCtxOptions); }
mtklein21eaf3b2016-02-08 12:39:59 -08001413
1414 // Wait for any remaining parallel work to complete (including any spun off of serial tasks).
1415 parallel.wait();
mtkleincd50bca2016-01-05 06:20:20 -08001416 gDefinitelyThreadSafeWork.wait();
1417
mtkleinda884c42016-02-24 18:00:23 -08001418 // We'd better have run everything.
1419 SkASSERT(gPending == 0);
mtkleine027f172016-02-26 15:53:06 -08001420 // Make sure we've flushed all our results to disk.
1421 JsonWriter::DumpJson();
mtkleinda884c42016-02-24 18:00:23 -08001422
mtklein748ca3b2015-01-15 10:56:12 -08001423 // At this point we're back in single-threaded land.
caryclarkf53ce802015-06-15 06:48:30 -07001424 sk_tool_utils::release_portable_typefaces();
mtklein@google.comd36522d2013-10-16 13:02:15 +00001425
mtklein748ca3b2015-01-15 10:56:12 -08001426 if (gFailures.count() > 0) {
mtkleinc41fd922016-03-08 09:01:39 -08001427 info("Failures:\n");
mtklein748ca3b2015-01-15 10:56:12 -08001428 for (int i = 0; i < gFailures.count(); i++) {
mtkleinc41fd922016-03-08 09:01:39 -08001429 info("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001430 }
mtkleinc41fd922016-03-08 09:01:39 -08001431 info("%d failures\n", gFailures.count());
mtklein748ca3b2015-01-15 10:56:12 -08001432 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001433 }
mtkleinafae30a2016-02-24 12:28:32 -08001434
1435#ifdef SK_PDF_IMAGE_STATS
halcanary7a14b312015-10-01 07:28:13 -07001436 SkPDFImageDumpStats();
mtkleinafae30a2016-02-24 12:28:32 -08001437#endif // SK_PDF_IMAGE_STATS
1438
Herb Derby5a523fe2017-01-26 16:48:28 -05001439 SkGraphics::PurgeAllCaches();
mtkleinc41fd922016-03-08 09:01:39 -08001440 info("Finished!\n");
Brian Osman53136aa2017-07-20 15:43:35 -04001441
mtklein748ca3b2015-01-15 10:56:12 -08001442 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +00001443}