blob: 6310ed77f4fc252d6af993d885e5dc3af49d5c0d [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"
csmartdalton008b9d82017-02-22 12:00:42 -070021#include "SkCommonFlagsPathRenderer.h"
brianosman3c579dc2016-04-19 09:18:11 -070022#include "SkData.h"
Brian Salomondcbb9d92017-07-19 10:53:20 -040023#include "SkDebugfTracer.h"
Brian Osman53136aa2017-07-20 15:43:35 -040024#include "SkEventTracingPriv.h"
caryclark83ca6282015-06-10 09:31:09 -070025#include "SkFontMgr.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +000026#include "SkGraphics.h"
mtklein27c3fdd2016-02-26 14:43:21 -080027#include "SkHalf.h"
halcanary4dbbd042016-06-07 17:21:10 -070028#include "SkLeanWindows.h"
mtklein748ca3b2015-01-15 10:56:12 -080029#include "SkMD5.h"
mtklein1b249332015-07-07 12:21:21 -070030#include "SkMutex.h"
mtklein1d0f1642014-09-08 08:05:18 -070031#include "SkOSFile.h"
Ben Wagnerbf111d72016-11-07 18:05:29 -050032#include "SkOSPath.h"
mtklein27c3fdd2016-02-26 14:43:21 -080033#include "SkPM4fPriv.h"
Mike Klein13b6afd2017-06-12 12:45:41 -040034#include "SkPngEncoder.h"
Brian Salomondcbb9d92017-07-19 10:53:20 -040035#include "SkScan.h"
mtklein246ba3a2016-02-23 10:39:36 -080036#include "SkSpinlock.h"
mtkleina82f5622015-02-20 12:30:19 -080037#include "SkTHash.h"
mtklein406654b2014-09-03 15:34:37 -070038#include "SkTaskGroup.h"
mtkleinde6fc2b2015-03-12 06:28:54 -070039#include "SkThreadUtils.h"
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +000040#include "Test.h"
mtklein748ca3b2015-01-15 10:56:12 -080041#include "Timer.h"
Mike Kleinadacaef2017-02-06 09:26:14 -050042#include "ios_utils.h"
brianosman3c579dc2016-04-19 09:18:11 -070043#include "picture_utils.h"
caryclark83ca6282015-06-10 09:31:09 -070044#include "sk_tool_utils.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +000045
scroggo19b91532016-10-24 09:03:26 -070046#include <vector>
47
halcanary7a14b312015-10-01 07:28:13 -070048#ifdef SK_PDF_IMAGE_STATS
49extern void SkPDFImageDumpStats();
50#endif
51
mtkleina5114d72015-08-24 13:27:01 -070052#include "png.h"
53
bungeman60e0fee2015-08-26 05:15:46 -070054#include <stdlib.h>
55
scroggo27a58342015-10-28 08:56:41 -070056#ifndef SK_BUILD_FOR_WIN32
57 #include <unistd.h>
58#endif
59
Mike Reed7c26ca72017-07-05 15:45:52 -040060extern bool gSkForceRasterPipelineBlitter;
61
djsollen54416de2015-04-03 07:24:48 -070062DEFINE_string(src, "tests gm skp image", "Source types to test.");
mtklein748ca3b2015-01-15 10:56:12 -080063DEFINE_bool(nameByHash, false,
64 "If true, write to FLAGS_writePath[0]/<hash>.png instead of "
djsollen54416de2015-04-03 07:24:48 -070065 "to FLAGS_writePath[0]/<config>/<sourceType>/<sourceOptions>/<name>.png");
mtklein748ca3b2015-01-15 10:56:12 -080066DEFINE_bool2(pathOpsExtended, x, false, "Run extended pathOps tests.");
mtkleind603b222015-02-17 11:13:33 -080067DEFINE_string(matrix, "1 0 0 1",
68 "2x2 scale+skew matrix to apply or upright when using "
69 "'matrix' or 'upright' in config.");
mtklein82d28432015-01-15 12:46:02 -080070DEFINE_bool(gpu_threading, false, "Allow GPU work to run on multiple threads?");
mtklein@google.comd36522d2013-10-16 13:02:15 +000071
mtkleina2ef6422015-01-15 13:44:22 -080072DEFINE_string(blacklist, "",
djsollen54416de2015-04-03 07:24:48 -070073 "Space-separated config/src/srcOptions/name quadruples to blacklist. '_' matches anything. E.g. \n"
74 "'--blacklist gpu skp _ _' will blacklist all SKPs drawn into the gpu config.\n"
75 "'--blacklist gpu skp _ _ 8888 gm _ aarects' will also blacklist the aarects GM on 8888.");
mtkleina2ef6422015-01-15 13:44:22 -080076
mtklein62bd1a62015-01-27 14:46:26 -080077DEFINE_string2(readPath, r, "", "If set check for equality with golden results in this directory.");
78
borenet09ed4802015-04-03 14:15:33 -070079DEFINE_string(uninterestingHashesFile, "",
80 "File containing a list of uninteresting hashes. If a result hashes to something in "
81 "this list, no image is written for that result.");
82
mtklein6393c062015-04-27 08:45:01 -070083DEFINE_int32(shards, 1, "We're splitting source data into this many shards.");
84DEFINE_int32(shard, 0, "Which shard do I run?");
85
halcanary45420a92016-06-02 12:41:14 -070086DEFINE_string(mskps, "", "Directory to read mskps from, or a single mskp file.");
Mike Reed7c26ca72017-07-05 15:45:52 -040087DEFINE_bool(forceRasterPipeline, false, "sets gSkForceRasterPipelineBlitter");
halcanary45420a92016-06-02 12:41:14 -070088
csmartdalton008b9d82017-02-22 12:00:42 -070089#if SK_SUPPORT_GPU
90DEFINE_pathrenderer_flag;
91#endif
92
Derek Sollenbergeredfe3df2017-07-19 15:25:24 -040093DEFINE_bool(ignoreSigInt, false, "ignore SIGINT signals during test execution");
94
mtklein748ca3b2015-01-15 10:56:12 -080095using namespace DM;
bsalomon3724e572016-03-30 18:56:19 -070096using sk_gpu_test::GrContextFactory;
bsalomon273c0f52016-03-31 10:59:06 -070097using sk_gpu_test::GLTestContext;
bsalomonf2f1c172016-04-05 12:59:06 -070098using sk_gpu_test::ContextInfo;
mtklein@google.comd36522d2013-10-16 13:02:15 +000099
mtklein748ca3b2015-01-15 10:56:12 -0800100/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
101
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700102static const double kStartMs = SkTime::GetMSecs();
mtkleinc41fd922016-03-08 09:01:39 -0800103
104static FILE* gVLog;
105
106template <typename... Args>
107static void vlog(const char* fmt, Args&&... args) {
108 if (gVLog) {
109 fprintf(gVLog, "%s\t", HumanizeMs(SkTime::GetMSecs() - kStartMs).c_str());
110 fprintf(gVLog, fmt, args...);
111 fflush(gVLog);
112 }
113}
114
115template <typename... Args>
116static void info(const char* fmt, Args&&... args) {
117 vlog(fmt, args...);
118 if (!FLAGS_quiet) {
119 printf(fmt, args...);
120 }
121}
122static void info(const char* fmt) {
123 if (!FLAGS_quiet) {
124 printf("%s", fmt); // Clang warns printf(fmt) is insecure.
125 }
126}
127
reed086eea92016-05-04 17:12:46 -0700128SK_DECLARE_STATIC_MUTEX(gFailuresMutex);
mtklein748ca3b2015-01-15 10:56:12 -0800129static SkTArray<SkString> gFailures;
130
mtklein3eed7dd2016-02-24 19:07:07 -0800131static void fail(const SkString& err) {
mtklein748ca3b2015-01-15 10:56:12 -0800132 SkAutoMutexAcquire lock(gFailuresMutex);
133 SkDebugf("\n\nFAILURE: %s\n\n", err.c_str());
134 gFailures.push_back(err);
halcanary9e398f72015-01-10 11:18:04 -0800135}
136
mtklein0f7748a2016-07-25 15:27:29 -0700137struct Running {
138 SkString id;
139 SkThreadID thread;
140
141 void dump() const {
142 info("\t%s\n", id.c_str());
143 }
144};
mtkleinb37cb412015-03-18 05:27:14 -0700145
mtklein246ba3a2016-02-23 10:39:36 -0800146// We use a spinlock to make locking this in a signal handler _somewhat_ safe.
Mike Klein03a95ad2017-06-23 12:36:32 -0400147static SkSpinlock gMutex;
148static int gPending;
mtklein0f7748a2016-07-25 15:27:29 -0700149static SkTArray<Running> gRunning;
mtklein748ca3b2015-01-15 10:56:12 -0800150
mtklein3eed7dd2016-02-24 19:07:07 -0800151static void done(const char* config, const char* src, const char* srcOptions, const char* name) {
152 SkString id = SkStringPrintf("%s %s %s %s", config, src, srcOptions, name);
mtkleinc41fd922016-03-08 09:01:39 -0800153 vlog("done %s\n", id.c_str());
mtkleinafae30a2016-02-24 12:28:32 -0800154 int pending;
mtkleinb37cb412015-03-18 05:27:14 -0700155 {
mtkleinf10637f2016-06-10 13:56:35 -0700156 SkAutoMutexAcquire lock(gMutex);
mtkleinb37cb412015-03-18 05:27:14 -0700157 for (int i = 0; i < gRunning.count(); i++) {
mtklein0f7748a2016-07-25 15:27:29 -0700158 if (gRunning[i].id == id) {
mtkleinb37cb412015-03-18 05:27:14 -0700159 gRunning.removeShuffle(i);
160 break;
161 }
162 }
mtkleinafae30a2016-02-24 12:28:32 -0800163 pending = --gPending;
reed50bc0512015-05-19 14:13:31 -0700164 }
Mike Klein03a95ad2017-06-23 12:36:32 -0400165
166 // We write out dm.json file and print out a progress update every once in a while.
167 // Notice this also handles the final dm.json and progress update when pending == 0.
mtkleina17241b2015-01-23 05:48:00 -0800168 if (pending % 500 == 0) {
169 JsonWriter::DumpJson();
Mike Klein03a95ad2017-06-23 12:36:32 -0400170
171 int curr = sk_tools::getCurrResidentSetSizeMB(),
172 peak = sk_tools::getMaxResidentSetSizeMB();
173 SkString elapsed = HumanizeMs(SkTime::GetMSecs() - kStartMs);
174
175 SkAutoMutexAcquire lock(gMutex);
176 info("\n%dMB RAM, %dMB peak, %s elapsed, %d queued, %d active:\n",
177 curr, peak, elapsed.c_str(), gPending - gRunning.count(), gRunning.count());
178 for (auto& task : gRunning) {
179 task.dump();
180 }
mtkleina17241b2015-01-23 05:48:00 -0800181 }
mtklein@google.comd36522d2013-10-16 13:02:15 +0000182}
183
mtklein3eed7dd2016-02-24 19:07:07 -0800184static void start(const char* config, const char* src, const char* srcOptions, const char* name) {
185 SkString id = SkStringPrintf("%s %s %s %s", config, src, srcOptions, name);
mtkleinc41fd922016-03-08 09:01:39 -0800186 vlog("start %s\n", id.c_str());
mtkleinf10637f2016-06-10 13:56:35 -0700187 SkAutoMutexAcquire lock(gMutex);
mtklein0f7748a2016-07-25 15:27:29 -0700188 gRunning.push_back({id,SkGetThreadID()});
mtkleinb37cb412015-03-18 05:27:14 -0700189}
190
Mike Klein3cc2d202016-10-24 11:39:43 -0400191static void find_culprit() {
192 // Assumes gMutex is locked.
193 SkThreadID thisThread = SkGetThreadID();
194 for (auto& task : gRunning) {
195 if (task.thread == thisThread) {
196 info("Likely culprit:\n");
197 task.dump();
mtklein0f7748a2016-07-25 15:27:29 -0700198 }
199 }
Mike Klein3cc2d202016-10-24 11:39:43 -0400200}
mtklein0f7748a2016-07-25 15:27:29 -0700201
mtklein246ba3a2016-02-23 10:39:36 -0800202#if defined(SK_BUILD_FOR_WIN32)
mtklein5b64dab2016-06-09 08:59:48 -0700203 static LONG WINAPI crash_handler(EXCEPTION_POINTERS* e) {
mtkleinf8557292016-02-29 06:35:28 -0800204 static const struct {
205 const char* name;
robertphillips75467862016-03-01 14:10:23 -0800206 DWORD code;
mtkleinf8557292016-02-29 06:35:28 -0800207 } kExceptions[] = {
208 #define _(E) {#E, E}
209 _(EXCEPTION_ACCESS_VIOLATION),
210 _(EXCEPTION_BREAKPOINT),
211 _(EXCEPTION_INT_DIVIDE_BY_ZERO),
212 _(EXCEPTION_STACK_OVERFLOW),
213 // TODO: more?
214 #undef _
215 };
216
mtkleinf10637f2016-06-10 13:56:35 -0700217 SkAutoMutexAcquire lock(gMutex);
mtklein5b64dab2016-06-09 08:59:48 -0700218
219 const DWORD code = e->ExceptionRecord->ExceptionCode;
220 info("\nCaught exception %u", code);
221 for (const auto& exception : kExceptions) {
222 if (exception.code == code) {
223 info(" %s", exception.name);
mtkleinf8557292016-02-29 06:35:28 -0800224 }
mtkleinf8557292016-02-29 06:35:28 -0800225 }
mtklein5b64dab2016-06-09 08:59:48 -0700226 info(", was running:\n");
227 for (auto& task : gRunning) {
mtklein0f7748a2016-07-25 15:27:29 -0700228 task.dump();
mtklein5b64dab2016-06-09 08:59:48 -0700229 }
mtklein0f7748a2016-07-25 15:27:29 -0700230 find_culprit();
mtklein5b64dab2016-06-09 08:59:48 -0700231 fflush(stdout);
232
mtkleinf8557292016-02-29 06:35:28 -0800233 // Execute default exception handler... hopefully, exit.
234 return EXCEPTION_EXECUTE_HANDLER;
mtklein246ba3a2016-02-23 10:39:36 -0800235 }
236
Mike Klein3cc2d202016-10-24 11:39:43 -0400237 static void setup_crash_handler() {
238 SetUnhandledExceptionFilter(crash_handler);
239 }
240#else
mtklein246ba3a2016-02-23 10:39:36 -0800241 #include <signal.h>
Mike Klein3cc2d202016-10-24 11:39:43 -0400242 #if !defined(SK_BUILD_FOR_ANDROID)
243 #include <execinfo.h>
Brian Salomondcbb9d92017-07-19 10:53:20 -0400244
245#endif
Mike Klein3cc2d202016-10-24 11:39:43 -0400246
247 static constexpr int max_of() { return 0; }
248 template <typename... Rest>
249 static constexpr int max_of(int x, Rest... rest) {
250 return x > max_of(rest...) ? x : max_of(rest...);
251 }
252
253 static void (*previous_handler[max_of(SIGABRT,SIGBUS,SIGFPE,SIGILL,SIGSEGV)+1])(int);
mtkleinf8557292016-02-29 06:35:28 -0800254
mtklein5b64dab2016-06-09 08:59:48 -0700255 static void crash_handler(int sig) {
mtkleinf10637f2016-06-10 13:56:35 -0700256 SkAutoMutexAcquire lock(gMutex);
mtklein5b64dab2016-06-09 08:59:48 -0700257
258 info("\nCaught signal %d [%s], was running:\n", sig, strsignal(sig));
259 for (auto& task : gRunning) {
mtklein0f7748a2016-07-25 15:27:29 -0700260 task.dump();
mtklein5b64dab2016-06-09 08:59:48 -0700261 }
mtklein0f7748a2016-07-25 15:27:29 -0700262 find_culprit();
mtklein5b64dab2016-06-09 08:59:48 -0700263
Mike Klein3cc2d202016-10-24 11:39:43 -0400264 #if !defined(SK_BUILD_FOR_ANDROID)
mtklein5b64dab2016-06-09 08:59:48 -0700265 void* stack[64];
266 int count = backtrace(stack, SK_ARRAY_COUNT(stack));
267 char** symbols = backtrace_symbols(stack, count);
268 info("\nStack trace:\n");
269 for (int i = 0; i < count; i++) {
270 info(" %s\n", symbols[i]);
271 }
Mike Klein3cc2d202016-10-24 11:39:43 -0400272 #endif
mtklein5b64dab2016-06-09 08:59:48 -0700273 fflush(stdout);
274
Mike Klein3cc2d202016-10-24 11:39:43 -0400275 signal(sig, previous_handler[sig]);
276 raise(sig);
mtklein5b64dab2016-06-09 08:59:48 -0700277 }
278
mtklein246ba3a2016-02-23 10:39:36 -0800279 static void setup_crash_handler() {
280 const int kSignals[] = { SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGSEGV };
281 for (int sig : kSignals) {
Mike Klein3cc2d202016-10-24 11:39:43 -0400282 previous_handler[sig] = signal(sig, crash_handler);
mtklein246ba3a2016-02-23 10:39:36 -0800283 }
Derek Sollenbergeredfe3df2017-07-19 15:25:24 -0400284
285 if (FLAGS_ignoreSigInt) {
286 signal(SIGINT, SIG_IGN);
287 }
mtklein246ba3a2016-02-23 10:39:36 -0800288 }
289#endif
290
mtklein748ca3b2015-01-15 10:56:12 -0800291/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
mtklein114c3cd2015-01-15 10:15:02 -0800292
mtklein62bd1a62015-01-27 14:46:26 -0800293struct Gold : public SkString {
mtkleina82f5622015-02-20 12:30:19 -0800294 Gold() : SkString("") {}
mtklein3eed7dd2016-02-24 19:07:07 -0800295 Gold(const SkString& sink, const SkString& src,
296 const SkString& srcOptions, const SkString& name,
297 const SkString& md5)
mtklein62bd1a62015-01-27 14:46:26 -0800298 : SkString("") {
299 this->append(sink);
300 this->append(src);
djsollen54416de2015-04-03 07:24:48 -0700301 this->append(srcOptions);
mtklein62bd1a62015-01-27 14:46:26 -0800302 this->append(name);
303 this->append(md5);
mtklein62bd1a62015-01-27 14:46:26 -0800304 }
mtkleinc8d1dd42015-10-15 12:23:01 -0700305 struct Hash {
306 uint32_t operator()(const Gold& g) const {
307 return SkGoodHash()((const SkString&)g);
308 }
309 };
mtklein62bd1a62015-01-27 14:46:26 -0800310};
mtkleina82f5622015-02-20 12:30:19 -0800311static SkTHashSet<Gold, Gold::Hash> gGold;
mtklein62bd1a62015-01-27 14:46:26 -0800312
313static void add_gold(JsonWriter::BitmapResult r) {
djsollen54416de2015-04-03 07:24:48 -0700314 gGold.add(Gold(r.config, r.sourceType, r.sourceOptions, r.name, r.md5));
mtklein62bd1a62015-01-27 14:46:26 -0800315}
316
317static void gather_gold() {
318 if (!FLAGS_readPath.isEmpty()) {
319 SkString path(FLAGS_readPath[0]);
320 path.append("/dm.json");
321 if (!JsonWriter::ReadJson(path.c_str(), add_gold)) {
322 fail(SkStringPrintf("Couldn't read %s for golden results.", path.c_str()));
323 }
324 }
325}
326
327/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
328
mtklein935f1b12016-03-16 08:37:19 -0700329#if defined(SK_BUILD_FOR_WIN32)
330 static const char* kNewline = "\r\n";
331#else
332 static const char* kNewline = "\n";
333#endif
334
borenet09ed4802015-04-03 14:15:33 -0700335static SkTHashSet<SkString> gUninterestingHashes;
336
337static void gather_uninteresting_hashes() {
338 if (!FLAGS_uninterestingHashesFile.isEmpty()) {
bungeman38d909e2016-08-02 14:40:46 -0700339 sk_sp<SkData> data(SkData::MakeFromFileName(FLAGS_uninterestingHashesFile[0]));
mtkleincc334b32015-09-22 11:43:53 -0700340 if (!data) {
mtkleinc41fd922016-03-08 09:01:39 -0800341 info("WARNING: unable to read uninteresting hashes from %s\n",
342 FLAGS_uninterestingHashesFile[0]);
mtkleincc334b32015-09-22 11:43:53 -0700343 return;
344 }
Mike Klein45dcc0c2017-04-05 18:04:31 -0400345
346 // Copy to a string to make sure SkStrSplit has a terminating \0 to find.
347 SkString contents((const char*)data->data(), data->size());
348
borenet09ed4802015-04-03 14:15:33 -0700349 SkTArray<SkString> hashes;
Mike Klein45dcc0c2017-04-05 18:04:31 -0400350 SkStrSplit(contents.c_str(), kNewline, &hashes);
borenet09ed4802015-04-03 14:15:33 -0700351 for (const SkString& hash : hashes) {
352 gUninterestingHashes.add(hash);
353 }
mtkleinc41fd922016-03-08 09:01:39 -0800354 info("FYI: loaded %d distinct uninteresting hashes from %d lines\n",
355 gUninterestingHashes.count(), hashes.count());
borenet09ed4802015-04-03 14:15:33 -0700356 }
357}
358
359/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
360
Ben Wagner145dbcd2016-11-03 14:40:50 -0400361struct TaggedSrc : public std::unique_ptr<Src> {
mtklein3eed7dd2016-02-24 19:07:07 -0800362 SkString tag;
363 SkString options;
mtkleine0effd62015-07-29 06:37:28 -0700364};
365
Ben Wagner145dbcd2016-11-03 14:40:50 -0400366struct TaggedSink : public std::unique_ptr<Sink> {
kkinnunen3e980c32015-12-23 01:33:00 -0800367 SkString tag;
djsollen54416de2015-04-03 07:24:48 -0700368};
mtklein748ca3b2015-01-15 10:56:12 -0800369
370static const bool kMemcpyOK = true;
371
mtkleine0effd62015-07-29 06:37:28 -0700372static SkTArray<TaggedSrc, kMemcpyOK> gSrcs;
373static SkTArray<TaggedSink, kMemcpyOK> gSinks;
mtklein748ca3b2015-01-15 10:56:12 -0800374
mtklein6393c062015-04-27 08:45:01 -0700375static bool in_shard() {
376 static int N = 0;
377 return N++ % FLAGS_shards == FLAGS_shard;
378}
379
mtklein3eed7dd2016-02-24 19:07:07 -0800380static void push_src(const char* tag, ImplicitString options, Src* s) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400381 std::unique_ptr<Src> src(s);
mtklein6393c062015-04-27 08:45:01 -0700382 if (in_shard() &&
mtklein3eed7dd2016-02-24 19:07:07 -0800383 FLAGS_src.contains(tag) &&
mtklein748ca3b2015-01-15 10:56:12 -0800384 !SkCommandLineFlags::ShouldSkip(FLAGS_match, src->name().c_str())) {
mtkleine0effd62015-07-29 06:37:28 -0700385 TaggedSrc& s = gSrcs.push_back();
mtklein18300a32016-03-16 13:53:35 -0700386 s.reset(src.release());
mtklein748ca3b2015-01-15 10:56:12 -0800387 s.tag = tag;
djsollen54416de2015-04-03 07:24:48 -0700388 s.options = options;
mtklein114c3cd2015-01-15 10:15:02 -0800389 }
mtklein748ca3b2015-01-15 10:56:12 -0800390}
mtklein114c3cd2015-01-15 10:15:02 -0800391
msarett9e707a02015-09-01 14:57:57 -0700392static void push_codec_src(Path path, CodecSrc::Mode mode, CodecSrc::DstColorType dstColorType,
scroggoc5560be2016-02-03 09:42:42 -0800393 SkAlphaType dstAlphaType, float scale) {
scroggoe4499842016-02-25 11:03:47 -0800394 if (FLAGS_simpleCodec) {
scroggo19b91532016-10-24 09:03:26 -0700395 const bool simple = CodecSrc::kCodec_Mode == mode || CodecSrc::kAnimated_Mode == mode;
396 if (!simple || dstColorType != CodecSrc::kGetFromCanvas_DstColorType || scale != 1.0f) {
scroggoe4499842016-02-25 11:03:47 -0800397 // Only decode in the simple case.
398 return;
scroggo19b91532016-10-24 09:03:26 -0700399 }
scroggoe4499842016-02-25 11:03:47 -0800400 }
msarett9e707a02015-09-01 14:57:57 -0700401 SkString folder;
402 switch (mode) {
403 case CodecSrc::kCodec_Mode:
404 folder.append("codec");
405 break;
msarettbb25b532016-01-13 09:31:39 -0800406 case CodecSrc::kCodecZeroInit_Mode:
407 folder.append("codec_zero_init");
408 break;
msarett9e707a02015-09-01 14:57:57 -0700409 case CodecSrc::kScanline_Mode:
410 folder.append("scanline");
411 break;
msarett9e707a02015-09-01 14:57:57 -0700412 case CodecSrc::kStripe_Mode:
413 folder.append("stripe");
414 break;
msarett91c22b22016-02-22 12:27:46 -0800415 case CodecSrc::kCroppedScanline_Mode:
416 folder.append("crop");
417 break;
msarett9e707a02015-09-01 14:57:57 -0700418 case CodecSrc::kSubset_Mode:
msarett36c37962015-09-02 13:20:52 -0700419 folder.append("codec_subset");
msarett9e707a02015-09-01 14:57:57 -0700420 break;
scroggo19b91532016-10-24 09:03:26 -0700421 case CodecSrc::kAnimated_Mode:
422 folder.append("codec_animated");
423 break;
msarett9e707a02015-09-01 14:57:57 -0700424 }
425
426 switch (dstColorType) {
427 case CodecSrc::kGrayscale_Always_DstColorType:
428 folder.append("_kGray8");
429 break;
msarett34e0ec42016-04-22 16:27:24 -0700430 case CodecSrc::kNonNative8888_Always_DstColorType:
431 folder.append("_kNonNative");
432 break;
msarett9e707a02015-09-01 14:57:57 -0700433 default:
434 break;
435 }
436
scroggoc5560be2016-02-03 09:42:42 -0800437 switch (dstAlphaType) {
scroggoc5560be2016-02-03 09:42:42 -0800438 case kPremul_SkAlphaType:
439 folder.append("_premul");
440 break;
441 case kUnpremul_SkAlphaType:
442 folder.append("_unpremul");
443 break;
444 default:
445 break;
446 }
447
msarett9e707a02015-09-01 14:57:57 -0700448 if (1.0f != scale) {
449 folder.appendf("_%.3f", scale);
450 }
451
scroggoc5560be2016-02-03 09:42:42 -0800452 CodecSrc* src = new CodecSrc(path, mode, dstColorType, dstAlphaType, scale);
msarett9e707a02015-09-01 14:57:57 -0700453 push_src("image", folder, src);
454}
455
scroggof8dc9df2016-05-16 09:04:13 -0700456static void push_android_codec_src(Path path, CodecSrc::DstColorType dstColorType,
457 SkAlphaType dstAlphaType, int sampleSize) {
msarett3d9d7a72015-10-21 10:27:10 -0700458 SkString folder;
scroggof8dc9df2016-05-16 09:04:13 -0700459 folder.append("scaled_codec");
msarett3d9d7a72015-10-21 10:27:10 -0700460
461 switch (dstColorType) {
462 case CodecSrc::kGrayscale_Always_DstColorType:
463 folder.append("_kGray8");
464 break;
msarett34e0ec42016-04-22 16:27:24 -0700465 case CodecSrc::kNonNative8888_Always_DstColorType:
466 folder.append("_kNonNative");
467 break;
msarett3d9d7a72015-10-21 10:27:10 -0700468 default:
469 break;
470 }
471
scroggoc5560be2016-02-03 09:42:42 -0800472 switch (dstAlphaType) {
scroggoc5560be2016-02-03 09:42:42 -0800473 case kPremul_SkAlphaType:
474 folder.append("_premul");
475 break;
msarett9e9444c2016-02-03 12:39:10 -0800476 case kUnpremul_SkAlphaType:
477 folder.append("_unpremul");
478 break;
scroggoc5560be2016-02-03 09:42:42 -0800479 default:
480 break;
481 }
482
msarett3d9d7a72015-10-21 10:27:10 -0700483 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800484 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700485 }
486
scroggof8dc9df2016-05-16 09:04:13 -0700487 AndroidCodecSrc* src = new AndroidCodecSrc(path, dstColorType, dstAlphaType, sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700488 push_src("image", folder, src);
489}
490
msarett18976312016-03-09 14:20:58 -0800491static void push_image_gen_src(Path path, ImageGenSrc::Mode mode, SkAlphaType alphaType, bool isGpu)
492{
493 SkString folder;
494 switch (mode) {
495 case ImageGenSrc::kCodec_Mode:
496 folder.append("gen_codec");
497 break;
498 case ImageGenSrc::kPlatform_Mode:
499 folder.append("gen_platform");
500 break;
501 }
502
503 if (isGpu) {
504 folder.append("_gpu");
505 } else {
506 switch (alphaType) {
507 case kOpaque_SkAlphaType:
508 folder.append("_opaque");
509 break;
510 case kPremul_SkAlphaType:
511 folder.append("_premul");
512 break;
513 case kUnpremul_SkAlphaType:
514 folder.append("_unpremul");
515 break;
516 default:
517 break;
518 }
519 }
520
521 ImageGenSrc* src = new ImageGenSrc(path, mode, alphaType, isGpu);
522 push_src("image", folder, src);
523}
524
msarett438b2ad2015-04-09 12:43:10 -0700525static void push_codec_srcs(Path path) {
bungeman38d909e2016-08-02 14:40:46 -0700526 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
msarett438b2ad2015-04-09 12:43:10 -0700527 if (!encoded) {
mtkleinc41fd922016-03-08 09:01:39 -0800528 info("Couldn't read %s.", path.c_str());
msarett438b2ad2015-04-09 12:43:10 -0700529 return;
530 }
Ben Wagner145dbcd2016-11-03 14:40:50 -0400531 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(encoded));
halcanary96fcdcc2015-08-27 07:41:13 -0700532 if (nullptr == codec.get()) {
mtkleinc41fd922016-03-08 09:01:39 -0800533 info("Couldn't create codec for %s.", path.c_str());
msarett438b2ad2015-04-09 12:43:10 -0700534 return;
535 }
536
scroggo9366aff2016-05-20 08:38:54 -0700537 // native scaling is only supported by WEBP and JPEG
538 bool supportsNativeScaling = false;
msarett438b2ad2015-04-09 12:43:10 -0700539
msarett91c22b22016-02-22 12:27:46 -0800540 SkTArray<CodecSrc::Mode> nativeModes;
541 nativeModes.push_back(CodecSrc::kCodec_Mode);
542 nativeModes.push_back(CodecSrc::kCodecZeroInit_Mode);
msarett91c22b22016-02-22 12:27:46 -0800543 switch (codec->getEncodedFormat()) {
Hal Canarydb683012016-11-23 08:55:18 -0700544 case SkEncodedImageFormat::kJPEG:
msarett91c22b22016-02-22 12:27:46 -0800545 nativeModes.push_back(CodecSrc::kScanline_Mode);
546 nativeModes.push_back(CodecSrc::kStripe_Mode);
547 nativeModes.push_back(CodecSrc::kCroppedScanline_Mode);
scroggo9366aff2016-05-20 08:38:54 -0700548 supportsNativeScaling = true;
msarett91c22b22016-02-22 12:27:46 -0800549 break;
Hal Canarydb683012016-11-23 08:55:18 -0700550 case SkEncodedImageFormat::kWEBP:
msarett91c22b22016-02-22 12:27:46 -0800551 nativeModes.push_back(CodecSrc::kSubset_Mode);
scroggo9366aff2016-05-20 08:38:54 -0700552 supportsNativeScaling = true;
msarett91c22b22016-02-22 12:27:46 -0800553 break;
Hal Canarydb683012016-11-23 08:55:18 -0700554 case SkEncodedImageFormat::kDNG:
msarettb65e6042016-02-23 05:37:25 -0800555 break;
msarett91c22b22016-02-22 12:27:46 -0800556 default:
557 nativeModes.push_back(CodecSrc::kScanline_Mode);
msarett91c22b22016-02-22 12:27:46 -0800558 break;
559 }
msarett9e707a02015-09-01 14:57:57 -0700560
msarett91c22b22016-02-22 12:27:46 -0800561 SkTArray<CodecSrc::DstColorType> colorTypes;
562 colorTypes.push_back(CodecSrc::kGetFromCanvas_DstColorType);
msarett34e0ec42016-04-22 16:27:24 -0700563 colorTypes.push_back(CodecSrc::kNonNative8888_Always_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700564 switch (codec->getInfo().colorType()) {
565 case kGray_8_SkColorType:
msarett91c22b22016-02-22 12:27:46 -0800566 colorTypes.push_back(CodecSrc::kGrayscale_Always_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700567 break;
568 default:
msarett36c37962015-09-02 13:20:52 -0700569 break;
570 }
msarett9e707a02015-09-01 14:57:57 -0700571
scroggoc5560be2016-02-03 09:42:42 -0800572 SkTArray<SkAlphaType> alphaModes;
573 alphaModes.push_back(kPremul_SkAlphaType);
scroggof2c96a22016-05-20 11:08:27 -0700574 if (codec->getInfo().alphaType() != kOpaque_SkAlphaType) {
575 alphaModes.push_back(kUnpremul_SkAlphaType);
scroggoc5560be2016-02-03 09:42:42 -0800576 }
msarettb714fb02016-01-22 14:46:42 -0800577
578 for (CodecSrc::Mode mode : nativeModes) {
scroggo9366aff2016-05-20 08:38:54 -0700579 for (CodecSrc::DstColorType colorType : colorTypes) {
580 for (SkAlphaType alphaType : alphaModes) {
scroggof2c96a22016-05-20 11:08:27 -0700581 // Only test kCroppedScanline_Mode when the alpha type is premul. The test is
scroggo9366aff2016-05-20 08:38:54 -0700582 // slow and won't be interestingly different with different alpha types.
583 if (CodecSrc::kCroppedScanline_Mode == mode &&
scroggof2c96a22016-05-20 11:08:27 -0700584 kPremul_SkAlphaType != alphaType) {
scroggo9366aff2016-05-20 08:38:54 -0700585 continue;
586 }
msarett91c22b22016-02-22 12:27:46 -0800587
scroggo9366aff2016-05-20 08:38:54 -0700588 push_codec_src(path, mode, colorType, alphaType, 1.0f);
msarettf3dc1882016-04-26 13:06:38 -0700589
scroggo9366aff2016-05-20 08:38:54 -0700590 // Skip kNonNative on different native scales. It won't be interestingly
591 // different.
592 if (supportsNativeScaling &&
593 CodecSrc::kNonNative8888_Always_DstColorType == colorType) {
594 // Native Scales
595 // SkJpegCodec natively supports scaling to the following:
596 for (auto scale : { 0.125f, 0.25f, 0.375f, 0.5f, 0.625f, 0.750f, 0.875f }) {
597 push_codec_src(path, mode, colorType, alphaType, scale);
598 }
scroggoc5560be2016-02-03 09:42:42 -0800599 }
msarett9e707a02015-09-01 14:57:57 -0700600 }
601 }
msarett0a242972015-06-11 14:27:27 -0700602 }
msarett36c37962015-09-02 13:20:52 -0700603
scroggo19b91532016-10-24 09:03:26 -0700604 {
605 std::vector<SkCodec::FrameInfo> frameInfos = codec->getFrameInfo();
606 if (frameInfos.size() > 1) {
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400607 for (auto dstCT : { CodecSrc::kNonNative8888_Always_DstColorType,
608 CodecSrc::kGetFromCanvas_DstColorType }) {
609 for (auto at : { kUnpremul_SkAlphaType, kPremul_SkAlphaType }) {
610 push_codec_src(path, CodecSrc::kAnimated_Mode, dstCT, at, 1.0f);
611 }
612 }
scroggo19b91532016-10-24 09:03:26 -0700613 }
614
615 }
616
scroggoe4499842016-02-25 11:03:47 -0800617 if (FLAGS_simpleCodec) {
618 return;
619 }
620
msarett3d9d7a72015-10-21 10:27:10 -0700621 const int sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
msarett36c37962015-09-02 13:20:52 -0700622
msarett3d9d7a72015-10-21 10:27:10 -0700623 for (int sampleSize : sampleSizes) {
msarett91c22b22016-02-22 12:27:46 -0800624 for (CodecSrc::DstColorType colorType : colorTypes) {
scroggoc5560be2016-02-03 09:42:42 -0800625 for (SkAlphaType alphaType : alphaModes) {
msarettf3dc1882016-04-26 13:06:38 -0700626 // We can exercise all of the kNonNative support code in the swizzler with just a
627 // few sample sizes. Skip the rest.
628 if (CodecSrc::kNonNative8888_Always_DstColorType == colorType && sampleSize > 3) {
629 continue;
630 }
631
scroggof8dc9df2016-05-16 09:04:13 -0700632 push_android_codec_src(path, colorType, alphaType, sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700633 }
msarett36c37962015-09-02 13:20:52 -0700634 }
635 }
msarett18976312016-03-09 14:20:58 -0800636
637 static const char* const rawExts[] = {
638 "arw", "cr2", "dng", "nef", "nrw", "orf", "raf", "rw2", "pef", "srw",
639 "ARW", "CR2", "DNG", "NEF", "NRW", "ORF", "RAF", "RW2", "PEF", "SRW",
640 };
641
642 // There is not currently a reason to test RAW images on image generator.
643 // If we want to enable these tests, we will need to fix skbug.com/5079.
644 for (const char* ext : rawExts) {
645 if (path.endsWith(ext)) {
646 return;
647 }
648 }
649
650 // Push image generator GPU test.
brianosmanc9ced392016-03-24 07:27:43 -0700651 push_image_gen_src(path, ImageGenSrc::kCodec_Mode, codec->getInfo().alphaType(), true);
msarett18976312016-03-09 14:20:58 -0800652
653 // Push image generator CPU tests.
654 for (SkAlphaType alphaType : alphaModes) {
655 push_image_gen_src(path, ImageGenSrc::kCodec_Mode, alphaType, false);
656
657#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
Hal Canarydb683012016-11-23 08:55:18 -0700658 if (SkEncodedImageFormat::kWEBP != codec->getEncodedFormat() &&
659 SkEncodedImageFormat::kWBMP != codec->getEncodedFormat() &&
msarett18976312016-03-09 14:20:58 -0800660 kUnpremul_SkAlphaType != alphaType)
661 {
662 push_image_gen_src(path, ImageGenSrc::kPlatform_Mode, alphaType, false);
663 }
msarettfc0b6d12016-03-17 13:50:17 -0700664#elif defined(SK_BUILD_FOR_WIN)
Hal Canarydb683012016-11-23 08:55:18 -0700665 if (SkEncodedImageFormat::kWEBP != codec->getEncodedFormat() &&
666 SkEncodedImageFormat::kWBMP != codec->getEncodedFormat())
msarettfc0b6d12016-03-17 13:50:17 -0700667 {
668 push_image_gen_src(path, ImageGenSrc::kPlatform_Mode, alphaType, false);
669 }
msarett18976312016-03-09 14:20:58 -0800670#endif
671 }
msarett438b2ad2015-04-09 12:43:10 -0700672}
673
msarettd1227a72016-05-18 06:23:57 -0700674static void push_brd_src(Path path, CodecSrc::DstColorType dstColorType, BRDSrc::Mode mode,
675 uint32_t sampleSize) {
676 SkString folder("brd_android_codec");
msaretta5783ae2015-09-08 15:35:32 -0700677 switch (mode) {
678 case BRDSrc::kFullImage_Mode:
679 break;
680 case BRDSrc::kDivisor_Mode:
681 folder.append("_divisor");
682 break;
683 default:
684 SkASSERT(false);
685 return;
686 }
687
688 switch (dstColorType) {
689 case CodecSrc::kGetFromCanvas_DstColorType:
690 break;
msaretta5783ae2015-09-08 15:35:32 -0700691 case CodecSrc::kGrayscale_Always_DstColorType:
692 folder.append("_kGray");
693 break;
694 default:
695 SkASSERT(false);
696 return;
697 }
698
699 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800700 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700701 }
702
msarettd1227a72016-05-18 06:23:57 -0700703 BRDSrc* src = new BRDSrc(path, mode, dstColorType, sampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700704 push_src("image", folder, src);
705}
706
707static void push_brd_srcs(Path path) {
Leon Scroggins IIIbace7a92017-07-05 11:39:27 -0400708 // Only run grayscale to one sampleSize and Mode. Though interesting
709 // to test grayscale, it should not reveal anything across various
scroggo24a9bd72016-05-20 06:02:42 -0700710 // sampleSizes and Modes
Leon Scroggins IIIbace7a92017-07-05 11:39:27 -0400711 // Arbitrarily choose Mode and sampleSize.
712 push_brd_src(path, CodecSrc::kGrayscale_Always_DstColorType, BRDSrc::kFullImage_Mode, 2);
scroggo24a9bd72016-05-20 06:02:42 -0700713
714
scroggo501b7342015-11-03 07:55:11 -0800715 // Test on a variety of sampleSizes, making sure to include:
716 // - 2, 4, and 8, which are natively supported by jpeg
717 // - multiples of 2 which are not divisible by 4 (analogous for 4)
718 // - larger powers of two, since BRD clients generally use powers of 2
719 // We will only produce output for the larger sizes on large images.
720 const uint32_t sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 24, 32, 64 };
msarett6efbe052015-09-11 09:01:16 -0700721
msaretta5783ae2015-09-08 15:35:32 -0700722 const BRDSrc::Mode modes[] = {
msarett26ad17b2015-10-22 07:29:19 -0700723 BRDSrc::kFullImage_Mode,
724 BRDSrc::kDivisor_Mode,
msaretta5783ae2015-09-08 15:35:32 -0700725 };
726
msarettd1227a72016-05-18 06:23:57 -0700727 for (uint32_t sampleSize : sampleSizes) {
scroggo24a9bd72016-05-20 06:02:42 -0700728 for (BRDSrc::Mode mode : modes) {
729 push_brd_src(path, CodecSrc::kGetFromCanvas_DstColorType, mode, sampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700730 }
731 }
732}
733
734static bool brd_supported(const char* ext) {
msarett8c8f22a2015-04-01 06:58:48 -0700735 static const char* const exts[] = {
msaretta5783ae2015-09-08 15:35:32 -0700736 "jpg", "jpeg", "png", "webp",
737 "JPG", "JPEG", "PNG", "WEBP",
msarett8c8f22a2015-04-01 06:58:48 -0700738 };
739
740 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
741 if (0 == strcmp(exts[i], ext)) {
742 return true;
743 }
744 }
745 return false;
scroggo9b77ddd2015-03-19 06:03:39 -0700746}
747
fmalitaa2b9fdf2016-08-03 19:53:36 -0700748template <typename T>
749void gather_file_srcs(const SkCommandLineFlags::StringArray& flags, const char* ext) {
750 for (int i = 0; i < flags.count(); i++) {
751 const char* path = flags[i];
752 if (sk_isdir(path)) {
753 SkOSFile::Iter it(path, ext);
754 for (SkString file; it.next(&file); ) {
755 push_src(ext, "", new T(SkOSPath::Join(path, file.c_str())));
756 }
757 } else {
758 push_src(ext, "", new T(path));
759 }
760 }
761}
762
scroggo86737142016-02-03 12:19:11 -0800763static bool gather_srcs() {
mtklein748ca3b2015-01-15 10:56:12 -0800764 for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) {
djsollen54416de2015-04-03 07:24:48 -0700765 push_src("gm", "", new GMSrc(r->factory()));
mtklein748ca3b2015-01-15 10:56:12 -0800766 }
scroggo86737142016-02-03 12:19:11 -0800767
fmalitaa2b9fdf2016-08-03 19:53:36 -0700768 gather_file_srcs<SKPSrc>(FLAGS_skps, "skp");
769 gather_file_srcs<MSKPSrc>(FLAGS_mskps, "mskp");
770#if defined(SK_XML)
771 gather_file_srcs<SVGSrc>(FLAGS_svgs, "svg");
772#endif
halcanary45420a92016-06-02 12:41:14 -0700773
scroggo86737142016-02-03 12:19:11 -0800774 SkTArray<SkString> images;
msarett69deca82016-04-29 09:38:40 -0700775 if (!CollectImages(FLAGS_images, &images)) {
scroggo86737142016-02-03 12:19:11 -0800776 return false;
777 }
778
779 for (auto image : images) {
780 push_codec_srcs(image);
scroggoe4499842016-02-25 11:03:47 -0800781 if (FLAGS_simpleCodec) {
782 continue;
783 }
784
mtklein21eaf3b2016-02-08 12:39:59 -0800785 const char* ext = strrchr(image.c_str(), '.');
786 if (ext && brd_supported(ext+1)) {
scroggo86737142016-02-03 12:19:11 -0800787 push_brd_srcs(image);
mtklein709d2c32015-01-15 08:30:25 -0800788 }
mtklein709d2c32015-01-15 08:30:25 -0800789 }
scroggo86737142016-02-03 12:19:11 -0800790
msarett69deca82016-04-29 09:38:40 -0700791 SkTArray<SkString> colorImages;
792 if (!CollectImages(FLAGS_colorImages, &colorImages)) {
793 return false;
794 }
795
796 for (auto colorImage : colorImages) {
msarett9ce3a542016-07-15 13:54:38 -0700797 ColorCodecSrc* src = new ColorCodecSrc(colorImage, ColorCodecSrc::kBaseline_Mode,
798 kN32_SkColorType);
msarettb8d1aac2016-08-04 12:22:06 -0700799 push_src("colorImage", "color_codec_baseline", src);
msarett888dc162016-05-23 10:21:17 -0700800
msarett9ce3a542016-07-15 13:54:38 -0700801 src = new ColorCodecSrc(colorImage, ColorCodecSrc::kDst_HPZR30w_Mode, kN32_SkColorType);
msarettb8d1aac2016-08-04 12:22:06 -0700802 push_src("colorImage", "color_codec_HPZR30w", src);
msarett9ce3a542016-07-15 13:54:38 -0700803 // TODO (msarett):
804 // 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 -0700805
msarett9ce3a542016-07-15 13:54:38 -0700806 src = new ColorCodecSrc(colorImage, ColorCodecSrc::kDst_sRGB_Mode, kN32_SkColorType);
msarettb8d1aac2016-08-04 12:22:06 -0700807 push_src("colorImage", "color_codec_sRGB_kN32", src);
msarett9ce3a542016-07-15 13:54:38 -0700808 src = new ColorCodecSrc(colorImage, ColorCodecSrc::kDst_sRGB_Mode, kRGBA_F16_SkColorType);
msarettb8d1aac2016-08-04 12:22:06 -0700809 push_src("colorImage", "color_codec_sRGB_kF16", src);
msarett69deca82016-04-29 09:38:40 -0700810 }
811
scroggo86737142016-02-03 12:19:11 -0800812 return true;
mtklein709d2c32015-01-15 08:30:25 -0800813}
814
kkinnunen3e980c32015-12-23 01:33:00 -0800815static void push_sink(const SkCommandLineConfig& config, Sink* s) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400816 std::unique_ptr<Sink> sink(s);
kkinnunen3e980c32015-12-23 01:33:00 -0800817
mtkleine0effd62015-07-29 06:37:28 -0700818 // Try a simple Src as a canary. If it fails, skip this sink.
mtklein748ca3b2015-01-15 10:56:12 -0800819 struct : public Src {
mtkleine0effd62015-07-29 06:37:28 -0700820 Error draw(SkCanvas* c) const override {
821 c->drawRect(SkRect::MakeWH(1,1), SkPaint());
822 return "";
823 }
mtklein36352bf2015-03-25 18:17:31 -0700824 SkISize size() const override { return SkISize::Make(16, 16); }
mtkleine0effd62015-07-29 06:37:28 -0700825 Name name() const override { return "justOneRect"; }
826 } justOneRect;
mtklein114c3cd2015-01-15 10:15:02 -0800827
mtklein748ca3b2015-01-15 10:56:12 -0800828 SkBitmap bitmap;
829 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800830 SkString log;
mtkleine0effd62015-07-29 06:37:28 -0700831 Error err = sink->draw(justOneRect, &bitmap, &stream, &log);
mtklein4089ef72015-03-05 08:40:28 -0800832 if (err.isFatal()) {
mtkleinc41fd922016-03-08 09:01:39 -0800833 info("Could not run %s: %s\n", config.getTag().c_str(), err.c_str());
mtklein05641a52015-04-21 10:49:13 -0700834 exit(1);
mtklein114c3cd2015-01-15 10:15:02 -0800835 }
836
mtkleine0effd62015-07-29 06:37:28 -0700837 TaggedSink& ts = gSinks.push_back();
mtklein18300a32016-03-16 13:53:35 -0700838 ts.reset(sink.release());
kkinnunen3e980c32015-12-23 01:33:00 -0800839 ts.tag = config.getTag();
mtklein748ca3b2015-01-15 10:56:12 -0800840}
841
842static bool gpu_supported() {
843#if SK_SUPPORT_GPU
844 return FLAGS_gpu;
845#else
846 return false;
847#endif
848}
kkinnunen9ebc3f02015-12-21 23:48:13 -0800849
csmartdalton008b9d82017-02-22 12:00:42 -0700850static Sink* create_sink(const GrContextOptions& grCtxOptions, const SkCommandLineConfig* config) {
kkinnunen3e980c32015-12-23 01:33:00 -0800851#if SK_SUPPORT_GPU
852 if (gpu_supported()) {
853 if (const SkCommandLineConfigGpu* gpuConfig = config->asConfigGpu()) {
bsalomon85b4b532016-04-05 11:06:27 -0700854 GrContextFactory::ContextType contextType = gpuConfig->getContextType();
csmartdaltone812d492017-02-21 12:36:05 -0700855 GrContextFactory::ContextOverrides contextOverrides = gpuConfig->getContextOverrides();
csmartdalton008b9d82017-02-22 12:00:42 -0700856 GrContextFactory testFactory(grCtxOptions);
csmartdaltone812d492017-02-21 12:36:05 -0700857 if (!testFactory.get(contextType, contextOverrides)) {
mtkleinc41fd922016-03-08 09:01:39 -0800858 info("WARNING: can not create GPU context for config '%s'. "
859 "GM tests will be skipped.\n", gpuConfig->getTag().c_str());
kkinnunen3e980c32015-12-23 01:33:00 -0800860 return nullptr;
861 }
csmartdaltone812d492017-02-21 12:36:05 -0700862 return new GPUSink(contextType, contextOverrides, gpuConfig->getSamples(),
brianosmand93c1202016-03-10 07:49:08 -0800863 gpuConfig->getUseDIText(), gpuConfig->getColorType(),
Brian Salomonce5ee602017-07-17 11:31:31 -0400864 gpuConfig->getAlphaType(), sk_ref_sp(gpuConfig->getColorSpace()),
865 FLAGS_gpu_threading);
kkinnunen3e980c32015-12-23 01:33:00 -0800866 }
867 }
868#endif
869
870#define SINK(t, sink, ...) if (config->getBackend().equals(t)) { return new sink(__VA_ARGS__); }
871
mtklein748ca3b2015-01-15 10:56:12 -0800872 if (FLAGS_cpu) {
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500873 auto srgbColorSpace = SkColorSpace::MakeSRGB();
874 auto srgbLinearColorSpace = SkColorSpace::MakeSRGBLinear();
brianosmanb109b8c2016-06-16 13:03:24 -0700875
Matt Sarettd2228302017-03-02 08:53:46 -0500876 SINK("565", RasterSink, kRGB_565_SkColorType);
877 SINK("8888", RasterSink, kN32_SkColorType);
878 SINK("srgb", RasterSink, kN32_SkColorType, srgbColorSpace);
Matt Sarettd2228302017-03-02 08:53:46 -0500879 SINK("f16", RasterSink, kRGBA_F16_SkColorType, srgbLinearColorSpace);
880 SINK("pdf", PDFSink);
881 SINK("skp", SKPSink);
882 SINK("pipe", PipeSink);
883 SINK("svg", SVGSink);
884 SINK("null", NullSink);
885 SINK("xps", XPSSink);
886 SINK("pdfa", PDFSink, true);
Hal Canary85c7fe82016-10-25 10:33:27 -0400887 SINK("jsdebug", DebugSink);
mtklein748ca3b2015-01-15 10:56:12 -0800888 }
889#undef SINK
halcanary96fcdcc2015-08-27 07:41:13 -0700890 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800891}
892
Mike Klein841101d2017-03-10 09:55:51 -0500893static sk_sp<SkColorSpace> adobe_rgb() {
894 return SkColorSpace::MakeRGB(SkColorSpace::kSRGB_RenderTargetGamma,
895 SkColorSpace::kAdobeRGB_Gamut);
896}
897
Mike Klein919cc452017-03-18 15:36:52 +0000898static sk_sp<SkColorSpace> rgb_to_gbr() {
899 float gbr[9];
900 gbr[0] = gSRGB_toXYZD50[1];
901 gbr[1] = gSRGB_toXYZD50[2];
902 gbr[2] = gSRGB_toXYZD50[0];
903 gbr[3] = gSRGB_toXYZD50[4];
904 gbr[4] = gSRGB_toXYZD50[5];
905 gbr[5] = gSRGB_toXYZD50[3];
906 gbr[6] = gSRGB_toXYZD50[7];
907 gbr[7] = gSRGB_toXYZD50[8];
908 gbr[8] = gSRGB_toXYZD50[6];
909 SkMatrix44 toXYZD50(SkMatrix44::kUninitialized_Constructor);
910 toXYZD50.set3x3RowMajorf(gbr);
911 return SkColorSpace::MakeRGB(SkColorSpace::kSRGB_RenderTargetGamma, toXYZD50);
912}
913
kkinnunen3e980c32015-12-23 01:33:00 -0800914static Sink* create_via(const SkString& tag, Sink* wrapped) {
915#define VIA(t, via, ...) if (tag.equals(t)) { return new via(__VA_ARGS__); }
Mike Klein919cc452017-03-18 15:36:52 +0000916 VIA("adobe", ViaCSXform, wrapped, adobe_rgb(), false);
917 VIA("gbr", ViaCSXform, wrapped, rgb_to_gbr(), true);
mtklein9c5052f2016-08-06 12:51:51 -0700918 VIA("lite", ViaLite, wrapped);
reed54dc4872016-09-13 08:09:45 -0700919 VIA("pipe", ViaPipe, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700920 VIA("twice", ViaTwice, wrapped);
Mike Reedbae888e2017-02-18 16:50:45 -0500921#ifdef TEST_VIA_SVG
Mike Reedf67c4592017-02-17 17:06:11 -0500922 VIA("svg", ViaSVG, wrapped);
Mike Reedbae888e2017-02-18 16:50:45 -0500923#endif
halcanary96fcdcc2015-08-27 07:41:13 -0700924 VIA("serialize", ViaSerialization, wrapped);
mtklein4a34ecb2016-01-08 10:19:35 -0800925 VIA("pic", ViaPicture, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700926 VIA("2ndpic", ViaSecondPicture, wrapped);
mtkleind31c13d2015-05-05 12:59:56 -0700927 VIA("sp", ViaSingletonPictures, wrapped);
reedbabc3de2016-07-08 08:43:27 -0700928 VIA("defer", ViaDefer, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700929 VIA("tiles", ViaTiles, 256, 256, nullptr, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800930 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800931
mtkleind603b222015-02-17 11:13:33 -0800932 if (FLAGS_matrix.count() == 4) {
mtklein748ca3b2015-01-15 10:56:12 -0800933 SkMatrix m;
mtkleind603b222015-02-17 11:13:33 -0800934 m.reset();
935 m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
936 m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
937 m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
938 m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
939 VIA("matrix", ViaMatrix, m, wrapped);
940 VIA("upright", ViaUpright, m, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800941 }
tomhudson64de1e12015-03-05 08:01:07 -0800942
mtklein748ca3b2015-01-15 10:56:12 -0800943#undef VIA
halcanary96fcdcc2015-08-27 07:41:13 -0700944 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800945}
946
csmartdalton008b9d82017-02-22 12:00:42 -0700947static bool gather_sinks(const GrContextOptions& grCtxOptions) {
kkinnunen3e980c32015-12-23 01:33:00 -0800948 SkCommandLineConfigArray configs;
949 ParseConfigs(FLAGS_config, &configs);
950 for (int i = 0; i < configs.count(); i++) {
951 const SkCommandLineConfig& config = *configs[i];
csmartdalton008b9d82017-02-22 12:00:42 -0700952 Sink* sink = create_sink(grCtxOptions, &config);
kkinnunen3e980c32015-12-23 01:33:00 -0800953 if (sink == nullptr) {
mtkleinc41fd922016-03-08 09:01:39 -0800954 info("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
955 config.getTag().c_str());
kkinnunen3e980c32015-12-23 01:33:00 -0800956 continue;
957 }
mtklein748ca3b2015-01-15 10:56:12 -0800958
kkinnunen3e980c32015-12-23 01:33:00 -0800959 const SkTArray<SkString>& parts = config.getViaParts();
960 for (int j = parts.count(); j-- > 0;) {
961 const SkString& part = parts[j];
962 Sink* next = create_via(part, sink);
halcanary96fcdcc2015-08-27 07:41:13 -0700963 if (next == nullptr) {
mtkleinc41fd922016-03-08 09:01:39 -0800964 info("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
965 part.c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800966 delete sink;
halcanary96fcdcc2015-08-27 07:41:13 -0700967 sink = nullptr;
mtklein748ca3b2015-01-15 10:56:12 -0800968 break;
969 }
970 sink = next;
971 }
972 if (sink) {
973 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800974 }
975 }
brianosman05c987c2016-05-09 13:42:16 -0700976
977 // If no configs were requested (just running tests, perhaps?), then we're okay.
978 // Otherwise, make sure that at least one sink was constructed correctly. This catches
979 // the case of bots without a GPU being assigned GPU configs.
980 return (configs.count() == 0) || (gSinks.count() > 0);
mtklein114c3cd2015-01-15 10:15:02 -0800981}
mtklein709d2c32015-01-15 08:30:25 -0800982
mtkleina5114d72015-08-24 13:27:01 -0700983static bool dump_png(SkBitmap bitmap, const char* path, const char* md5) {
Mike Klein13b6afd2017-06-12 12:45:41 -0400984 SkPixmap pm;
985 if (!bitmap.peekPixels(&pm)) {
986 return false; // Ought to never happen... we're already read-back at this point.
mtkleina5114d72015-08-24 13:27:01 -0700987 }
Mike Klein13b6afd2017-06-12 12:45:41 -0400988 SkFILEWStream dst{path};
mtkleina5114d72015-08-24 13:27:01 -0700989
mtkleind69ece62015-09-10 10:37:44 -0700990 SkString description;
991 description.append("Key: ");
992 for (int i = 0; i < FLAGS_key.count(); i++) {
993 description.appendf("%s ", FLAGS_key[i]);
994 }
995 description.append("Properties: ");
996 for (int i = 0; i < FLAGS_properties.count(); i++) {
997 description.appendf("%s ", FLAGS_properties[i]);
998 }
999 description.appendf("MD5: %s", md5);
1000
Mike Klein13b6afd2017-06-12 12:45:41 -04001001 const char* comments[] = {
1002 "Author", "DM dump_png()",
1003 "Description", description.c_str(),
1004 };
1005 size_t lengths[] = {
1006 strlen(comments[0])+1, strlen(comments[1])+1,
1007 strlen(comments[2])+1, strlen(comments[3])+1,
1008 };
mtkleina5114d72015-08-24 13:27:01 -07001009
Mike Klein13b6afd2017-06-12 12:45:41 -04001010 SkPngEncoder::Options options;
1011 options.fComments = SkDataTable::MakeCopyArrays((const void**)comments, lengths, 4);
1012 options.fFilterFlags = SkPngEncoder::FilterFlag::kNone;
1013 options.fZLibLevel = 1;
1014 options.fUnpremulBehavior = pm.colorSpace() ? SkTransferFunctionBehavior::kRespect
1015 : SkTransferFunctionBehavior::kIgnore;
1016 return SkPngEncoder::Encode(&dst, pm, options);
mtkleina5114d72015-08-24 13:27:01 -07001017}
1018
mtkleina2ef6422015-01-15 13:44:22 -08001019static bool match(const char* needle, const char* haystack) {
halcanary96fcdcc2015-08-27 07:41:13 -07001020 return 0 == strcmp("_", needle) || nullptr != strstr(haystack, needle);
mtkleina2ef6422015-01-15 13:44:22 -08001021}
1022
mtklein3eed7dd2016-02-24 19:07:07 -08001023static bool is_blacklisted(const char* sink, const char* src,
1024 const char* srcOptions, const char* name) {
mtklein0d243ff2015-04-03 07:51:00 -07001025 for (int i = 0; i < FLAGS_blacklist.count() - 3; i += 4) {
mtkleina2ef6422015-01-15 13:44:22 -08001026 if (match(FLAGS_blacklist[i+0], sink) &&
djsollen54416de2015-04-03 07:24:48 -07001027 match(FLAGS_blacklist[i+1], src) &&
1028 match(FLAGS_blacklist[i+2], srcOptions) &&
1029 match(FLAGS_blacklist[i+3], name)) {
mtklein3eed7dd2016-02-24 19:07:07 -08001030 return true;
mtkleina2ef6422015-01-15 13:44:22 -08001031 }
1032 }
mtklein3eed7dd2016-02-24 19:07:07 -08001033 return false;
mtkleina2ef6422015-01-15 13:44:22 -08001034}
1035
mtkleincd50bca2016-01-05 06:20:20 -08001036// Even when a Task Sink reports to be non-threadsafe (e.g. GPU), we know things like
1037// .png encoding are definitely thread safe. This lets us offload that work to CPU threads.
1038static SkTaskGroup gDefinitelyThreadSafeWork;
1039
mtklein748ca3b2015-01-15 10:56:12 -08001040// The finest-grained unit of work we can run: draw a single Src into a single Sink,
1041// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
1042struct Task {
mtkleine0effd62015-07-29 06:37:28 -07001043 Task(const TaggedSrc& src, const TaggedSink& sink) : src(src), sink(sink) {}
1044 const TaggedSrc& src;
1045 const TaggedSink& sink;
mtklein748ca3b2015-01-15 10:56:12 -08001046
mtklein21eaf3b2016-02-08 12:39:59 -08001047 static void Run(const Task& task) {
1048 SkString name = task.src->name();
mtkleine0effd62015-07-29 06:37:28 -07001049
mtkleinb9eb4ac2015-02-02 18:26:03 -08001050 SkString log;
mtklein3eed7dd2016-02-24 19:07:07 -08001051 if (!FLAGS_dryRun) {
mtklein748ca3b2015-01-15 10:56:12 -08001052 SkBitmap bitmap;
1053 SkDynamicMemoryWStream stream;
mtklein3eed7dd2016-02-24 19:07:07 -08001054 start(task.sink.tag.c_str(), task.src.tag.c_str(),
1055 task.src.options.c_str(), name.c_str());
mtklein21eaf3b2016-02-08 12:39:59 -08001056 Error err = task.sink->draw(*task.src, &bitmap, &stream, &log);
mtkleinb3b13b72016-03-07 13:20:52 -08001057 if (!log.isEmpty()) {
mtkleinc41fd922016-03-08 09:01:39 -08001058 info("%s %s %s %s:\n%s\n", task.sink.tag.c_str()
1059 , task.src.tag.c_str()
1060 , task.src.options.c_str()
1061 , name.c_str()
1062 , log.c_str());
mtkleinb3b13b72016-03-07 13:20:52 -08001063 }
mtklein748ca3b2015-01-15 10:56:12 -08001064 if (!err.isEmpty()) {
mtklein4089ef72015-03-05 08:40:28 -08001065 if (err.isFatal()) {
djsollen54416de2015-04-03 07:24:48 -07001066 fail(SkStringPrintf("%s %s %s %s: %s",
mtklein21eaf3b2016-02-08 12:39:59 -08001067 task.sink.tag.c_str(),
1068 task.src.tag.c_str(),
1069 task.src.options.c_str(),
mtklein4089ef72015-03-05 08:40:28 -08001070 name.c_str(),
1071 err.c_str()));
mtkleinb37cb412015-03-18 05:27:14 -07001072 } else {
mtklein3eed7dd2016-02-24 19:07:07 -08001073 done(task.sink.tag.c_str(), task.src.tag.c_str(),
1074 task.src.options.c_str(), name.c_str());
mtkleinba6ada72016-01-21 09:39:35 -08001075 return;
mtklein4089ef72015-03-05 08:40:28 -08001076 }
mtklein748ca3b2015-01-15 10:56:12 -08001077 }
mtklein62bd1a62015-01-27 14:46:26 -08001078
mtkleincd50bca2016-01-05 06:20:20 -08001079 // We're likely switching threads here, so we must capture by value, [=] or [foo,bar].
Hal Canary0b833192017-03-14 14:35:02 -04001080 SkStreamAsset* data = stream.detachAsStream().release();
mtkleincd50bca2016-01-05 06:20:20 -08001081 gDefinitelyThreadSafeWork.add([task,name,bitmap,data]{
Ben Wagner145dbcd2016-11-03 14:40:50 -04001082 std::unique_ptr<SkStreamAsset> ownedData(data);
mtkleincd50bca2016-01-05 06:20:20 -08001083
mtkleincd50bca2016-01-05 06:20:20 -08001084 SkString md5;
1085 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
1086 SkMD5 hash;
1087 if (data->getLength()) {
1088 hash.writeStream(data, data->getLength());
1089 data->rewind();
mtklein38408462015-07-08 07:25:27 -07001090 } else {
mtkleincd50bca2016-01-05 06:20:20 -08001091 // If we're BGRA (Linux, Windows), swizzle over to RGBA (Mac, Android).
1092 // This helps eliminate multiple 0-pixel-diff hashes on gold.skia.org.
1093 // (Android's general slow speed breaks the tie arbitrarily in RGBA's favor.)
1094 // We might consider promoting 565 to RGBA too.
1095 if (bitmap.colorType() == kBGRA_8888_SkColorType) {
1096 SkBitmap swizzle;
Matt Sarett68b8e3d2017-04-28 11:15:22 -04001097 SkAssertResult(sk_tool_utils::copy_to(&swizzle, kRGBA_8888_SkColorType,
1098 bitmap));
mtkleincd50bca2016-01-05 06:20:20 -08001099 hash.write(swizzle.getPixels(), swizzle.getSize());
1100 } else {
1101 hash.write(bitmap.getPixels(), bitmap.getSize());
1102 }
1103 }
1104 SkMD5::Digest digest;
1105 hash.finish(digest);
1106 for (int i = 0; i < 16; i++) {
1107 md5.appendf("%02x", digest.data[i]);
mtklein38408462015-07-08 07:25:27 -07001108 }
mtklein62bd1a62015-01-27 14:46:26 -08001109 }
mtklein62bd1a62015-01-27 14:46:26 -08001110
mtkleincd50bca2016-01-05 06:20:20 -08001111 if (!FLAGS_readPath.isEmpty() &&
mtklein3eed7dd2016-02-24 19:07:07 -08001112 !gGold.contains(Gold(task.sink.tag, task.src.tag,
1113 task.src.options, name, md5))) {
mtkleincd50bca2016-01-05 06:20:20 -08001114 fail(SkStringPrintf("%s not found for %s %s %s %s in %s",
1115 md5.c_str(),
mtklein21eaf3b2016-02-08 12:39:59 -08001116 task.sink.tag.c_str(),
1117 task.src.tag.c_str(),
1118 task.src.options.c_str(),
mtkleincd50bca2016-01-05 06:20:20 -08001119 name.c_str(),
1120 FLAGS_readPath[0]));
mtklein748ca3b2015-01-15 10:56:12 -08001121 }
mtkleincd50bca2016-01-05 06:20:20 -08001122
1123 if (!FLAGS_writePath.isEmpty()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001124 const char* ext = task.sink->fileExtension();
mtkleincd50bca2016-01-05 06:20:20 -08001125 if (data->getLength()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001126 WriteToDisk(task, md5, ext, data, data->getLength(), nullptr);
mtkleincd50bca2016-01-05 06:20:20 -08001127 SkASSERT(bitmap.drawsNothing());
1128 } else if (!bitmap.drawsNothing()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001129 WriteToDisk(task, md5, ext, nullptr, 0, &bitmap);
mtkleincd50bca2016-01-05 06:20:20 -08001130 }
1131 }
1132 });
mtklein748ca3b2015-01-15 10:56:12 -08001133 }
mtklein3eed7dd2016-02-24 19:07:07 -08001134 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 -08001135 }
1136
1137 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -08001138 SkString md5,
1139 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -08001140 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -08001141 const SkBitmap* bitmap) {
mtklein409d4702016-02-29 07:38:01 -08001142 bool gammaCorrect = false;
1143 if (bitmap) {
Brian Osman7039f742016-11-01 15:56:16 -04001144 gammaCorrect = SkToBool(bitmap->info().colorSpace());
mtklein409d4702016-02-29 07:38:01 -08001145 }
1146
mtklein748ca3b2015-01-15 10:56:12 -08001147 JsonWriter::BitmapResult result;
djsollen54416de2015-04-03 07:24:48 -07001148 result.name = task.src->name();
mtklein3eed7dd2016-02-24 19:07:07 -08001149 result.config = task.sink.tag;
djsollen54416de2015-04-03 07:24:48 -07001150 result.sourceType = task.src.tag;
1151 result.sourceOptions = task.src.options;
1152 result.ext = ext;
mtklein409d4702016-02-29 07:38:01 -08001153 result.gammaCorrect = gammaCorrect;
djsollen54416de2015-04-03 07:24:48 -07001154 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -08001155 JsonWriter::AddBitmapResult(result);
1156
mtkleinb0531a72015-04-07 13:38:48 -07001157 // If an MD5 is uninteresting, we want it noted in the JSON file,
1158 // but don't want to dump it out as a .png (or whatever ext is).
1159 if (gUninterestingHashes.contains(md5)) {
1160 return;
1161 }
1162
mtklein748ca3b2015-01-15 10:56:12 -08001163 const char* dir = FLAGS_writePath[0];
1164 if (0 == strcmp(dir, "@")) { // Needed for iOS.
1165 dir = FLAGS_resourcePath[0];
1166 }
1167 sk_mkdir(dir);
1168
1169 SkString path;
1170 if (FLAGS_nameByHash) {
1171 path = SkOSPath::Join(dir, result.md5.c_str());
1172 path.append(".");
1173 path.append(ext);
1174 if (sk_exists(path.c_str())) {
1175 return; // Content-addressed. If it exists already, we're done.
1176 }
1177 } else {
kkinnunen3e980c32015-12-23 01:33:00 -08001178 path = SkOSPath::Join(dir, task.sink.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001179 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001180 path = SkOSPath::Join(path.c_str(), task.src.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001181 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001182 if (strcmp(task.src.options.c_str(), "") != 0) {
1183 path = SkOSPath::Join(path.c_str(), task.src.options.c_str());
djsollen54416de2015-04-03 07:24:48 -07001184 sk_mkdir(path.c_str());
1185 }
mtklein748ca3b2015-01-15 10:56:12 -08001186 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
1187 path.append(".");
1188 path.append(ext);
1189 }
1190
mtklein748ca3b2015-01-15 10:56:12 -08001191 if (bitmap) {
mtkleina5114d72015-08-24 13:27:01 -07001192 if (!dump_png(*bitmap, path.c_str(), result.md5.c_str())) {
mtklein748ca3b2015-01-15 10:56:12 -08001193 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
1194 return;
1195 }
1196 } else {
mtkleina5114d72015-08-24 13:27:01 -07001197 SkFILEWStream file(path.c_str());
1198 if (!file.isValid()) {
1199 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
1200 return;
1201 }
mtklein748ca3b2015-01-15 10:56:12 -08001202 if (!file.writeStream(data, len)) {
1203 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
1204 return;
1205 }
1206 }
1207 }
1208};
1209
mtklein748ca3b2015-01-15 10:56:12 -08001210/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1211
1212// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
1213
mtklein21eaf3b2016-02-08 12:39:59 -08001214static SkTDArray<skiatest::Test> gParallelTests, gSerialTests;
mtklein748ca3b2015-01-15 10:56:12 -08001215
1216static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -08001217 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -08001218 return;
1219 }
mtklein6393c062015-04-27 08:45:01 -07001220 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r; r = r->next()) {
1221 if (!in_shard()) {
1222 continue;
1223 }
halcanary87f3ba42015-01-20 09:30:20 -08001224 // Despite its name, factory() is returning a reference to
1225 // link-time static const POD data.
1226 const skiatest::Test& test = r->factory();
1227 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -08001228 continue;
1229 }
halcanary87f3ba42015-01-20 09:30:20 -08001230 if (test.needsGpu && gpu_supported()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001231 (FLAGS_gpu_threading ? gParallelTests : gSerialTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -08001232 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein21eaf3b2016-02-08 12:39:59 -08001233 gParallelTests.push(test);
mtklein82d28432015-01-15 12:46:02 -08001234 }
mtklein748ca3b2015-01-15 10:56:12 -08001235 }
1236}
1237
csmartdalton008b9d82017-02-22 12:00:42 -07001238static void run_test(skiatest::Test test, const GrContextOptions& grCtxOptions) {
halcanary87f3ba42015-01-20 09:30:20 -08001239 struct : public skiatest::Reporter {
mtklein36352bf2015-03-25 18:17:31 -07001240 void reportFailed(const skiatest::Failure& failure) override {
halcanary87f3ba42015-01-20 09:30:20 -08001241 fail(failure.toString());
1242 JsonWriter::AddTestFailure(failure);
1243 }
mtklein36352bf2015-03-25 18:17:31 -07001244 bool allowExtendedTest() const override {
halcanary87f3ba42015-01-20 09:30:20 -08001245 return FLAGS_pathOpsExtended;
1246 }
mtklein36352bf2015-03-25 18:17:31 -07001247 bool verbose() const override { return FLAGS_veryVerbose; }
halcanary87f3ba42015-01-20 09:30:20 -08001248 } reporter;
djsollen824996a2015-06-12 12:06:22 -07001249
mtklein3eed7dd2016-02-24 19:07:07 -08001250 if (!FLAGS_dryRun && !is_blacklisted("_", "tests", "_", test.name)) {
mtklein21eaf3b2016-02-08 12:39:59 -08001251 start("unit", "test", "", test.name);
csmartdalton008b9d82017-02-22 12:00:42 -07001252 GrContextFactory factory(grCtxOptions);
mtklein21eaf3b2016-02-08 12:39:59 -08001253 test.proc(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -08001254 }
mtklein3eed7dd2016-02-24 19:07:07 -08001255 done("unit", "test", "", test.name);
mtklein748ca3b2015-01-15 10:56:12 -08001256}
1257
1258/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1259
caryclark83ca6282015-06-10 09:31:09 -07001260#define PORTABLE_FONT_PREFIX "Toy Liberation "
1261
mbocee6a9912016-05-31 11:42:36 -07001262static sk_sp<SkTypeface> create_from_name(const char familyName[], SkFontStyle style) {
caryclark83ca6282015-06-10 09:31:09 -07001263 if (familyName && strlen(familyName) > sizeof(PORTABLE_FONT_PREFIX)
1264 && !strncmp(familyName, PORTABLE_FONT_PREFIX, sizeof(PORTABLE_FONT_PREFIX) - 1)) {
caryclark1818acb2015-07-24 12:09:25 -07001265 return sk_tool_utils::create_portable_typeface(familyName, style);
caryclark83ca6282015-06-10 09:31:09 -07001266 }
halcanary96fcdcc2015-08-27 07:41:13 -07001267 return nullptr;
caryclark83ca6282015-06-10 09:31:09 -07001268}
1269
1270#undef PORTABLE_FONT_PREFIX
1271
mbocee6a9912016-05-31 11:42:36 -07001272extern sk_sp<SkTypeface> (*gCreateTypefaceDelegate)(const char [], SkFontStyle );
caryclark83ca6282015-06-10 09:31:09 -07001273
Mike Kleinbe28ee22017-02-06 12:46:20 -05001274int main(int argc, char** argv) {
1275 SkCommandLineFlags::Parse(argc, argv);
Brian Osman53136aa2017-07-20 15:43:35 -04001276
1277 initializeEventTracingForTools(&FLAGS_threads);
1278
1279#if defined(SK_BUILD_FOR_IOS)
Mike Kleinadacaef2017-02-06 09:26:14 -05001280 cd_Documents();
1281#endif
bsalomon71de3532016-04-26 14:27:21 -07001282 setbuf(stdout, nullptr);
mtklein246ba3a2016-02-23 10:39:36 -08001283 setup_crash_handler();
mtkleinafae30a2016-02-24 12:28:32 -08001284
Yuqian Liba62b4a2016-12-14 13:46:53 -05001285 gSkUseAnalyticAA = FLAGS_analyticAA;
liyuqian38911a72016-10-04 11:23:22 -07001286
Yuqian Li550148b2017-01-13 10:13:13 -05001287 if (FLAGS_forceAnalyticAA) {
1288 gSkForceAnalyticAA = true;
1289 }
Mike Reed7c26ca72017-07-05 15:45:52 -04001290 if (FLAGS_forceRasterPipeline) {
1291 gSkForceRasterPipelineBlitter = true;
1292 }
Yuqian Li550148b2017-01-13 10:13:13 -05001293
Mike Kleine9f187b2017-06-26 13:23:12 -04001294 // The bots like having a verbose.log to upload, so always touch the file even if --verbose.
1295 if (!FLAGS_writePath.isEmpty()) {
mtklein51c8cfc2016-03-11 12:59:09 -08001296 sk_mkdir(FLAGS_writePath[0]);
mtkleina1ce2162016-07-22 12:23:46 -07001297 gVLog = fopen(SkOSPath::Join(FLAGS_writePath[0], "verbose.log").c_str(), "w");
mtkleinc41fd922016-03-08 09:01:39 -08001298 }
Mike Kleine9f187b2017-06-26 13:23:12 -04001299 if (FLAGS_verbose) {
1300 gVLog = stderr;
1301 }
mtkleinc41fd922016-03-08 09:01:39 -08001302
csmartdalton008b9d82017-02-22 12:00:42 -07001303 GrContextOptions grCtxOptions;
1304#if SK_SUPPORT_GPU
1305 grCtxOptions.fGpuPathRenderers = CollectGpuPathRenderersFromFlags();
1306#endif
1307
mtklein5286f022016-01-22 08:18:14 -08001308 JsonWriter::DumpJson(); // It's handy for the bots to assume this is ~never missing.
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +00001309 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -07001310 SkTaskGroup::Enabler enabled(FLAGS_threads);
caryclark83ca6282015-06-10 09:31:09 -07001311 gCreateTypefaceDelegate = &create_from_name;
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +00001312
halcanary7d571242016-02-24 17:59:16 -08001313 {
1314 SkString testResourcePath = GetResourcePath("color_wheel.png");
1315 SkFILEStream testResource(testResourcePath.c_str());
1316 if (!testResource.isValid()) {
mtkleinc41fd922016-03-08 09:01:39 -08001317 info("Some resources are missing. Do you need to set --resourcePath?\n");
halcanary7d571242016-02-24 17:59:16 -08001318 }
1319 }
mtklein62bd1a62015-01-27 14:46:26 -08001320 gather_gold();
borenet09ed4802015-04-03 14:15:33 -07001321 gather_uninteresting_hashes();
mtklein62bd1a62015-01-27 14:46:26 -08001322
scroggo86737142016-02-03 12:19:11 -08001323 if (!gather_srcs()) {
1324 return 1;
1325 }
csmartdalton008b9d82017-02-22 12:00:42 -07001326 if (!gather_sinks(grCtxOptions)) {
brianosman05c987c2016-05-09 13:42:16 -07001327 return 1;
1328 }
mtklein748ca3b2015-01-15 10:56:12 -08001329 gather_tests();
mtklein21eaf3b2016-02-08 12:39:59 -08001330 gPending = gSrcs.count() * gSinks.count() + gParallelTests.count() + gSerialTests.count();
mtkleinc41fd922016-03-08 09:01:39 -08001331 info("%d srcs * %d sinks + %d tests == %d tasks",
1332 gSrcs.count(), gSinks.count(), gParallelTests.count() + gSerialTests.count(), gPending);
mtklein748ca3b2015-01-15 10:56:12 -08001333
mtklein21eaf3b2016-02-08 12:39:59 -08001334 // Kick off as much parallel work as we can, making note of any serial work we'll need to do.
1335 SkTaskGroup parallel;
1336 SkTArray<Task> serial;
1337
1338 for (auto& sink : gSinks)
1339 for (auto& src : gSrcs) {
mtklein3eed7dd2016-02-24 19:07:07 -08001340 if (src->veto(sink->flags()) ||
1341 is_blacklisted(sink.tag.c_str(), src.tag.c_str(),
1342 src.options.c_str(), src->name().c_str())) {
mtkleinf10637f2016-06-10 13:56:35 -07001343 SkAutoMutexAcquire lock(gMutex);
mtklein3eed7dd2016-02-24 19:07:07 -08001344 gPending--;
1345 continue;
1346 }
1347
mtklein21eaf3b2016-02-08 12:39:59 -08001348 Task task(src, sink);
1349 if (src->serial() || sink->serial()) {
1350 serial.push_back(task);
1351 } else {
1352 parallel.add([task] { Task::Run(task); });
mtklein748ca3b2015-01-15 10:56:12 -08001353 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001354 }
mtklein21eaf3b2016-02-08 12:39:59 -08001355 for (auto test : gParallelTests) {
csmartdalton008b9d82017-02-22 12:00:42 -07001356 parallel.add([test, grCtxOptions] { run_test(test, grCtxOptions); });
mtklein55e88b22015-01-21 15:50:13 -08001357 }
mtklein21eaf3b2016-02-08 12:39:59 -08001358
1359 // With the parallel work running, run serial tasks and tests here on main thread.
1360 for (auto task : serial) { Task::Run(task); }
csmartdalton008b9d82017-02-22 12:00:42 -07001361 for (auto test : gSerialTests) { run_test(test, grCtxOptions); }
mtklein21eaf3b2016-02-08 12:39:59 -08001362
1363 // Wait for any remaining parallel work to complete (including any spun off of serial tasks).
1364 parallel.wait();
mtkleincd50bca2016-01-05 06:20:20 -08001365 gDefinitelyThreadSafeWork.wait();
1366
mtkleinda884c42016-02-24 18:00:23 -08001367 // We'd better have run everything.
1368 SkASSERT(gPending == 0);
mtkleine027f172016-02-26 15:53:06 -08001369 // Make sure we've flushed all our results to disk.
1370 JsonWriter::DumpJson();
mtkleinda884c42016-02-24 18:00:23 -08001371
mtklein748ca3b2015-01-15 10:56:12 -08001372 // At this point we're back in single-threaded land.
caryclarkf53ce802015-06-15 06:48:30 -07001373 sk_tool_utils::release_portable_typefaces();
mtklein@google.comd36522d2013-10-16 13:02:15 +00001374
mtklein748ca3b2015-01-15 10:56:12 -08001375 if (gFailures.count() > 0) {
mtkleinc41fd922016-03-08 09:01:39 -08001376 info("Failures:\n");
mtklein748ca3b2015-01-15 10:56:12 -08001377 for (int i = 0; i < gFailures.count(); i++) {
mtkleinc41fd922016-03-08 09:01:39 -08001378 info("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001379 }
mtkleinc41fd922016-03-08 09:01:39 -08001380 info("%d failures\n", gFailures.count());
mtklein748ca3b2015-01-15 10:56:12 -08001381 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001382 }
mtkleinafae30a2016-02-24 12:28:32 -08001383
1384#ifdef SK_PDF_IMAGE_STATS
halcanary7a14b312015-10-01 07:28:13 -07001385 SkPDFImageDumpStats();
mtkleinafae30a2016-02-24 12:28:32 -08001386#endif // SK_PDF_IMAGE_STATS
1387
Herb Derby5a523fe2017-01-26 16:48:28 -05001388 SkGraphics::PurgeAllCaches();
mtkleinc41fd922016-03-08 09:01:39 -08001389 info("Finished!\n");
Brian Osman53136aa2017-07-20 15:43:35 -04001390
mtklein748ca3b2015-01-15 10:56:12 -08001391 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +00001392}
jcgregorio3b27ade2014-11-13 08:06:40 -08001393
kkinnunen179a8f52015-11-20 13:32:24 -08001394// TODO: currently many GPU tests are declared outside SK_SUPPORT_GPU guards.
1395// Thus we export the empty RunWithGPUTestContexts when SK_SUPPORT_GPU=0.
1396namespace skiatest {
bsalomon758586c2016-04-06 14:02:39 -07001397
1398#if SK_SUPPORT_GPU
1399bool IsGLContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1400 return kOpenGL_GrBackend == GrContextFactory::ContextTypeBackend(type);
1401}
bsalomondc0fcd42016-04-11 14:21:33 -07001402bool IsVulkanContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1403 return kVulkan_GrBackend == GrContextFactory::ContextTypeBackend(type);
1404}
bsalomon758586c2016-04-06 14:02:39 -07001405bool IsRenderingGLContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1406 return IsGLContextType(type) && GrContextFactory::IsRenderingContext(type);
1407}
1408bool IsNullGLContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1409 return type == GrContextFactory::kNullGL_ContextType;
1410}
1411#else
1412bool IsGLContextType(int) { return false; }
bsalomondc0fcd42016-04-11 14:21:33 -07001413bool IsVulkanContextType(int) { return false; }
bsalomon758586c2016-04-06 14:02:39 -07001414bool IsRenderingGLContextType(int) { return false; }
1415bool IsNullGLContextType(int) { return false; }
1416#endif
1417
1418void RunWithGPUTestContexts(GrContextTestFn* test, GrContextTypeFilterFn* contextTypeFilter,
bsalomonf2f1c172016-04-05 12:59:06 -07001419 Reporter* reporter, GrContextFactory* factory) {
kkinnunen179a8f52015-11-20 13:32:24 -08001420#if SK_SUPPORT_GPU
kkinnunen3e980c32015-12-23 01:33:00 -08001421
Brian Salomon6405e712017-03-20 08:54:16 -04001422#if defined(SK_BUILD_FOR_UNIX) || defined(SK_BUILD_FOR_WIN) || defined(SK_BUILD_FOR_MAC)
1423 static constexpr auto kNativeGLType = GrContextFactory::kGL_ContextType;
1424#else
1425 static constexpr auto kNativeGLType = GrContextFactory::kGLES_ContextType;
1426#endif
1427
bsalomon758586c2016-04-06 14:02:39 -07001428 for (int typeInt = 0; typeInt < GrContextFactory::kContextTypeCnt; ++typeInt) {
1429 GrContextFactory::ContextType contextType = (GrContextFactory::ContextType) typeInt;
Brian Osmanaa2219c2016-12-05 10:12:17 -05001430 // Use "native" instead of explicitly trying OpenGL and OpenGL ES. Do not use GLES on
bsalomon758586c2016-04-06 14:02:39 -07001431 // desktop since tests do not account for not fixing http://skbug.com/2809
1432 if (contextType == GrContextFactory::kGL_ContextType ||
1433 contextType == GrContextFactory::kGLES_ContextType) {
Brian Salomon6405e712017-03-20 08:54:16 -04001434 if (contextType != kNativeGLType) {
bsalomon758586c2016-04-06 14:02:39 -07001435 continue;
1436 }
1437 }
csmartdaltone812d492017-02-21 12:36:05 -07001438 ContextInfo ctxInfo = factory->getContextInfo(contextType,
1439 GrContextFactory::ContextOverrides::kDisableNVPR);
Brian Osmanaa2219c2016-12-05 10:12:17 -05001440 if (contextTypeFilter && !(*contextTypeFilter)(contextType)) {
1441 continue;
1442 }
Greg Daniela5cb7812017-06-16 09:45:32 -04001443 ReporterContext ctx(reporter, SkString(GrContextFactory::ContextTypeName(contextType)));
bsalomon8b7451a2016-05-11 06:33:06 -07001444 if (ctxInfo.grContext()) {
bsalomonf2f1c172016-04-05 12:59:06 -07001445 (*test)(reporter, ctxInfo);
kkinnunen5219fd92015-12-10 06:28:13 -08001446 }
bsalomonf2f1c172016-04-05 12:59:06 -07001447 ctxInfo = factory->getContextInfo(contextType,
csmartdaltone812d492017-02-21 12:36:05 -07001448 GrContextFactory::ContextOverrides::kRequireNVPRSupport);
bsalomon8b7451a2016-05-11 06:33:06 -07001449 if (ctxInfo.grContext()) {
bsalomonf2f1c172016-04-05 12:59:06 -07001450 (*test)(reporter, ctxInfo);
kkinnunen179a8f52015-11-20 13:32:24 -08001451 }
1452 }
1453#endif
1454}
kkinnunen179a8f52015-11-20 13:32:24 -08001455} // namespace skiatest