blob: a815827fc4b38c238dd81ead2d469e6e4dbff7eb [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"
tomhudsoneebc39a2015-02-23 12:18:05 -080010#include "DMSrcSinkAndroid.h"
mtklein748ca3b2015-01-15 10:56:12 -080011#include "ProcStats.h"
halcanary7d571242016-02-24 17:59:16 -080012#include "Resources.h"
mtklein748ca3b2015-01-15 10:56:12 -080013#include "SkBBHFactory.h"
mtklein62bd1a62015-01-27 14:46:26 -080014#include "SkChecksum.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"
caryclark17f0b6d2014-07-22 10:15:34 -070018#include "SkCommonFlags.h"
kkinnunen3e980c32015-12-23 01:33:00 -080019#include "SkCommonFlagsConfig.h"
brianosman3c579dc2016-04-19 09:18:11 -070020#include "SkData.h"
caryclark83ca6282015-06-10 09:31:09 -070021#include "SkFontMgr.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +000022#include "SkGraphics.h"
mtklein27c3fdd2016-02-26 14:43:21 -080023#include "SkHalf.h"
halcanary4dbbd042016-06-07 17:21:10 -070024#include "SkLeanWindows.h"
mtklein748ca3b2015-01-15 10:56:12 -080025#include "SkMD5.h"
mtklein1b249332015-07-07 12:21:21 -070026#include "SkMutex.h"
mtklein1d0f1642014-09-08 08:05:18 -070027#include "SkOSFile.h"
mtklein27c3fdd2016-02-26 14:43:21 -080028#include "SkPM4fPriv.h"
mtklein246ba3a2016-02-23 10:39:36 -080029#include "SkSpinlock.h"
mtkleina82f5622015-02-20 12:30:19 -080030#include "SkTHash.h"
mtklein406654b2014-09-03 15:34:37 -070031#include "SkTaskGroup.h"
mtkleinde6fc2b2015-03-12 06:28:54 -070032#include "SkThreadUtils.h"
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +000033#include "Test.h"
mtklein748ca3b2015-01-15 10:56:12 -080034#include "Timer.h"
brianosman3c579dc2016-04-19 09:18:11 -070035#include "picture_utils.h"
caryclark83ca6282015-06-10 09:31:09 -070036#include "sk_tool_utils.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +000037
halcanary7a14b312015-10-01 07:28:13 -070038#ifdef SK_PDF_IMAGE_STATS
39extern void SkPDFImageDumpStats();
40#endif
41
mtkleina5114d72015-08-24 13:27:01 -070042#include "png.h"
43
bungeman60e0fee2015-08-26 05:15:46 -070044#include <stdlib.h>
45
scroggo27a58342015-10-28 08:56:41 -070046#ifndef SK_BUILD_FOR_WIN32
47 #include <unistd.h>
48#endif
49
djsollen54416de2015-04-03 07:24:48 -070050DEFINE_string(src, "tests gm skp image", "Source types to test.");
mtklein748ca3b2015-01-15 10:56:12 -080051DEFINE_bool(nameByHash, false,
52 "If true, write to FLAGS_writePath[0]/<hash>.png instead of "
djsollen54416de2015-04-03 07:24:48 -070053 "to FLAGS_writePath[0]/<config>/<sourceType>/<sourceOptions>/<name>.png");
mtklein748ca3b2015-01-15 10:56:12 -080054DEFINE_bool2(pathOpsExtended, x, false, "Run extended pathOps tests.");
mtkleind603b222015-02-17 11:13:33 -080055DEFINE_string(matrix, "1 0 0 1",
56 "2x2 scale+skew matrix to apply or upright when using "
57 "'matrix' or 'upright' in config.");
mtklein82d28432015-01-15 12:46:02 -080058DEFINE_bool(gpu_threading, false, "Allow GPU work to run on multiple threads?");
mtklein@google.comd36522d2013-10-16 13:02:15 +000059
mtkleina2ef6422015-01-15 13:44:22 -080060DEFINE_string(blacklist, "",
djsollen54416de2015-04-03 07:24:48 -070061 "Space-separated config/src/srcOptions/name quadruples to blacklist. '_' matches anything. E.g. \n"
62 "'--blacklist gpu skp _ _' will blacklist all SKPs drawn into the gpu config.\n"
63 "'--blacklist gpu skp _ _ 8888 gm _ aarects' will also blacklist the aarects GM on 8888.");
mtkleina2ef6422015-01-15 13:44:22 -080064
mtklein62bd1a62015-01-27 14:46:26 -080065DEFINE_string2(readPath, r, "", "If set check for equality with golden results in this directory.");
66
borenet09ed4802015-04-03 14:15:33 -070067DEFINE_string(uninterestingHashesFile, "",
68 "File containing a list of uninteresting hashes. If a result hashes to something in "
69 "this list, no image is written for that result.");
70
mtklein6393c062015-04-27 08:45:01 -070071DEFINE_int32(shards, 1, "We're splitting source data into this many shards.");
72DEFINE_int32(shard, 0, "Which shard do I run?");
73
halcanary45420a92016-06-02 12:41:14 -070074DEFINE_string(mskps, "", "Directory to read mskps from, or a single mskp file.");
75
fmalitaa2b9fdf2016-08-03 19:53:36 -070076DEFINE_string(svgs, "", "Directory to read SVGs from, or a single SVG file.");
77
mtklein748ca3b2015-01-15 10:56:12 -080078using namespace DM;
bsalomon3724e572016-03-30 18:56:19 -070079using sk_gpu_test::GrContextFactory;
bsalomon273c0f52016-03-31 10:59:06 -070080using sk_gpu_test::GLTestContext;
bsalomonf2f1c172016-04-05 12:59:06 -070081using sk_gpu_test::ContextInfo;
mtklein@google.comd36522d2013-10-16 13:02:15 +000082
mtklein748ca3b2015-01-15 10:56:12 -080083/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
84
benjaminwagnerec4d4d72016-03-25 12:59:53 -070085static const double kStartMs = SkTime::GetMSecs();
mtkleinc41fd922016-03-08 09:01:39 -080086
87static FILE* gVLog;
88
89template <typename... Args>
90static void vlog(const char* fmt, Args&&... args) {
91 if (gVLog) {
92 fprintf(gVLog, "%s\t", HumanizeMs(SkTime::GetMSecs() - kStartMs).c_str());
93 fprintf(gVLog, fmt, args...);
94 fflush(gVLog);
95 }
96}
97
98template <typename... Args>
99static void info(const char* fmt, Args&&... args) {
100 vlog(fmt, args...);
101 if (!FLAGS_quiet) {
102 printf(fmt, args...);
103 }
104}
105static void info(const char* fmt) {
106 if (!FLAGS_quiet) {
107 printf("%s", fmt); // Clang warns printf(fmt) is insecure.
108 }
109}
110
reed086eea92016-05-04 17:12:46 -0700111SK_DECLARE_STATIC_MUTEX(gFailuresMutex);
mtklein748ca3b2015-01-15 10:56:12 -0800112static SkTArray<SkString> gFailures;
113
mtklein3eed7dd2016-02-24 19:07:07 -0800114static void fail(const SkString& err) {
mtklein748ca3b2015-01-15 10:56:12 -0800115 SkAutoMutexAcquire lock(gFailuresMutex);
116 SkDebugf("\n\nFAILURE: %s\n\n", err.c_str());
117 gFailures.push_back(err);
halcanary9e398f72015-01-10 11:18:04 -0800118}
119
mtklein0f7748a2016-07-25 15:27:29 -0700120struct Running {
121 SkString id;
122 SkThreadID thread;
123
124 void dump() const {
125 info("\t%s\n", id.c_str());
126 }
127};
mtkleinb37cb412015-03-18 05:27:14 -0700128
mtklein246ba3a2016-02-23 10:39:36 -0800129// We use a spinlock to make locking this in a signal handler _somewhat_ safe.
mtklein15923c92016-02-29 10:14:38 -0800130static SkSpinlock gMutex;
mtklein0f7748a2016-07-25 15:27:29 -0700131static int32_t gPending;
132static SkTArray<Running> gRunning;
mtklein748ca3b2015-01-15 10:56:12 -0800133
mtklein3eed7dd2016-02-24 19:07:07 -0800134static void done(const char* config, const char* src, const char* srcOptions, const char* name) {
135 SkString id = SkStringPrintf("%s %s %s %s", config, src, srcOptions, name);
mtkleinc41fd922016-03-08 09:01:39 -0800136 vlog("done %s\n", id.c_str());
mtkleinafae30a2016-02-24 12:28:32 -0800137 int pending;
mtkleinb37cb412015-03-18 05:27:14 -0700138 {
mtkleinf10637f2016-06-10 13:56:35 -0700139 SkAutoMutexAcquire lock(gMutex);
mtkleinb37cb412015-03-18 05:27:14 -0700140 for (int i = 0; i < gRunning.count(); i++) {
mtklein0f7748a2016-07-25 15:27:29 -0700141 if (gRunning[i].id == id) {
mtkleinb37cb412015-03-18 05:27:14 -0700142 gRunning.removeShuffle(i);
143 break;
144 }
145 }
mtkleinafae30a2016-02-24 12:28:32 -0800146 pending = --gPending;
reed50bc0512015-05-19 14:13:31 -0700147 }
mtkleina17241b2015-01-23 05:48:00 -0800148 // We write our dm.json file every once in a while in case we crash.
149 // Notice this also handles the final dm.json when pending == 0.
150 if (pending % 500 == 0) {
151 JsonWriter::DumpJson();
152 }
mtklein@google.comd36522d2013-10-16 13:02:15 +0000153}
154
mtklein3eed7dd2016-02-24 19:07:07 -0800155static void start(const char* config, const char* src, const char* srcOptions, const char* name) {
156 SkString id = SkStringPrintf("%s %s %s %s", config, src, srcOptions, name);
mtkleinc41fd922016-03-08 09:01:39 -0800157 vlog("start %s\n", id.c_str());
mtkleinf10637f2016-06-10 13:56:35 -0700158 SkAutoMutexAcquire lock(gMutex);
mtklein0f7748a2016-07-25 15:27:29 -0700159 gRunning.push_back({id,SkGetThreadID()});
mtkleinb37cb412015-03-18 05:27:14 -0700160}
161
mtkleinafae30a2016-02-24 12:28:32 -0800162static void print_status() {
mtkleinafae30a2016-02-24 12:28:32 -0800163 int curr = sk_tools::getCurrResidentSetSizeMB(),
164 peak = sk_tools::getMaxResidentSetSizeMB();
mtkleinc41fd922016-03-08 09:01:39 -0800165 SkString elapsed = HumanizeMs(SkTime::GetMSecs() - kStartMs);
mtkleinafae30a2016-02-24 12:28:32 -0800166
mtkleinf10637f2016-06-10 13:56:35 -0700167 SkAutoMutexAcquire lock(gMutex);
mtkleinc41fd922016-03-08 09:01:39 -0800168 info("\n%s elapsed, %d active, %d queued, %dMB RAM, %dMB peak\n",
169 elapsed.c_str(), gRunning.count(), gPending - gRunning.count(), curr, peak);
mtkleinafae30a2016-02-24 12:28:32 -0800170 for (auto& task : gRunning) {
mtklein0f7748a2016-07-25 15:27:29 -0700171 task.dump();
mtkleinafae30a2016-02-24 12:28:32 -0800172 }
173}
174
mtklein0f7748a2016-07-25 15:27:29 -0700175#if !defined(SK_BUILD_FOR_ANDROID)
176 static void find_culprit() {
177 // Assumes gMutex is locked.
178 SkThreadID thisThread = SkGetThreadID();
179 for (auto& task : gRunning) {
180 if (task.thread == thisThread) {
181 info("Likely culprit:\n");
182 task.dump();
183 }
184 }
185 }
186#endif
187
mtklein246ba3a2016-02-23 10:39:36 -0800188#if defined(SK_BUILD_FOR_WIN32)
mtklein5b64dab2016-06-09 08:59:48 -0700189 static LONG WINAPI crash_handler(EXCEPTION_POINTERS* e) {
mtkleinf8557292016-02-29 06:35:28 -0800190 static const struct {
191 const char* name;
robertphillips75467862016-03-01 14:10:23 -0800192 DWORD code;
mtkleinf8557292016-02-29 06:35:28 -0800193 } kExceptions[] = {
194 #define _(E) {#E, E}
195 _(EXCEPTION_ACCESS_VIOLATION),
196 _(EXCEPTION_BREAKPOINT),
197 _(EXCEPTION_INT_DIVIDE_BY_ZERO),
198 _(EXCEPTION_STACK_OVERFLOW),
199 // TODO: more?
200 #undef _
201 };
202
mtkleinf10637f2016-06-10 13:56:35 -0700203 SkAutoMutexAcquire lock(gMutex);
mtklein5b64dab2016-06-09 08:59:48 -0700204
205 const DWORD code = e->ExceptionRecord->ExceptionCode;
206 info("\nCaught exception %u", code);
207 for (const auto& exception : kExceptions) {
208 if (exception.code == code) {
209 info(" %s", exception.name);
mtkleinf8557292016-02-29 06:35:28 -0800210 }
mtkleinf8557292016-02-29 06:35:28 -0800211 }
mtklein5b64dab2016-06-09 08:59:48 -0700212 info(", was running:\n");
213 for (auto& task : gRunning) {
mtklein0f7748a2016-07-25 15:27:29 -0700214 task.dump();
mtklein5b64dab2016-06-09 08:59:48 -0700215 }
mtklein0f7748a2016-07-25 15:27:29 -0700216 find_culprit();
mtklein5b64dab2016-06-09 08:59:48 -0700217 fflush(stdout);
218
mtkleinf8557292016-02-29 06:35:28 -0800219 // Execute default exception handler... hopefully, exit.
220 return EXCEPTION_EXECUTE_HANDLER;
mtklein246ba3a2016-02-23 10:39:36 -0800221 }
mtklein5b64dab2016-06-09 08:59:48 -0700222 static void setup_crash_handler() { SetUnhandledExceptionFilter(crash_handler); }
mtklein246ba3a2016-02-23 10:39:36 -0800223
mtklein819ab102016-02-29 17:02:52 -0800224#elif !defined(SK_BUILD_FOR_ANDROID)
225 #include <execinfo.h>
mtklein246ba3a2016-02-23 10:39:36 -0800226 #include <signal.h>
mtklein819ab102016-02-29 17:02:52 -0800227 #include <stdlib.h>
mtkleinf8557292016-02-29 06:35:28 -0800228
mtklein5b64dab2016-06-09 08:59:48 -0700229 static void crash_handler(int sig) {
mtkleinf10637f2016-06-10 13:56:35 -0700230 SkAutoMutexAcquire lock(gMutex);
mtklein5b64dab2016-06-09 08:59:48 -0700231
232 info("\nCaught signal %d [%s], was running:\n", sig, strsignal(sig));
233 for (auto& task : gRunning) {
mtklein0f7748a2016-07-25 15:27:29 -0700234 task.dump();
mtklein5b64dab2016-06-09 08:59:48 -0700235 }
mtklein0f7748a2016-07-25 15:27:29 -0700236 find_culprit();
mtklein5b64dab2016-06-09 08:59:48 -0700237
238 void* stack[64];
239 int count = backtrace(stack, SK_ARRAY_COUNT(stack));
240 char** symbols = backtrace_symbols(stack, count);
241 info("\nStack trace:\n");
242 for (int i = 0; i < count; i++) {
243 info(" %s\n", symbols[i]);
244 }
245 fflush(stdout);
246
247 _Exit(sig);
248 }
249
mtklein246ba3a2016-02-23 10:39:36 -0800250 static void setup_crash_handler() {
251 const int kSignals[] = { SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGSEGV };
252 for (int sig : kSignals) {
mtklein5b64dab2016-06-09 08:59:48 -0700253 signal(sig, crash_handler);
mtklein246ba3a2016-02-23 10:39:36 -0800254 }
255 }
mtklein819ab102016-02-29 17:02:52 -0800256
257#else // Android
258 static void setup_crash_handler() {}
mtklein246ba3a2016-02-23 10:39:36 -0800259#endif
260
mtklein748ca3b2015-01-15 10:56:12 -0800261/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
mtklein114c3cd2015-01-15 10:15:02 -0800262
mtklein62bd1a62015-01-27 14:46:26 -0800263struct Gold : public SkString {
mtkleina82f5622015-02-20 12:30:19 -0800264 Gold() : SkString("") {}
mtklein3eed7dd2016-02-24 19:07:07 -0800265 Gold(const SkString& sink, const SkString& src,
266 const SkString& srcOptions, const SkString& name,
267 const SkString& md5)
mtklein62bd1a62015-01-27 14:46:26 -0800268 : SkString("") {
269 this->append(sink);
270 this->append(src);
djsollen54416de2015-04-03 07:24:48 -0700271 this->append(srcOptions);
mtklein62bd1a62015-01-27 14:46:26 -0800272 this->append(name);
273 this->append(md5);
mtklein62bd1a62015-01-27 14:46:26 -0800274 }
mtkleinc8d1dd42015-10-15 12:23:01 -0700275 struct Hash {
276 uint32_t operator()(const Gold& g) const {
277 return SkGoodHash()((const SkString&)g);
278 }
279 };
mtklein62bd1a62015-01-27 14:46:26 -0800280};
mtkleina82f5622015-02-20 12:30:19 -0800281static SkTHashSet<Gold, Gold::Hash> gGold;
mtklein62bd1a62015-01-27 14:46:26 -0800282
283static void add_gold(JsonWriter::BitmapResult r) {
djsollen54416de2015-04-03 07:24:48 -0700284 gGold.add(Gold(r.config, r.sourceType, r.sourceOptions, r.name, r.md5));
mtklein62bd1a62015-01-27 14:46:26 -0800285}
286
287static void gather_gold() {
288 if (!FLAGS_readPath.isEmpty()) {
289 SkString path(FLAGS_readPath[0]);
290 path.append("/dm.json");
291 if (!JsonWriter::ReadJson(path.c_str(), add_gold)) {
292 fail(SkStringPrintf("Couldn't read %s for golden results.", path.c_str()));
293 }
294 }
295}
296
297/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
298
mtklein935f1b12016-03-16 08:37:19 -0700299#if defined(SK_BUILD_FOR_WIN32)
300 static const char* kNewline = "\r\n";
301#else
302 static const char* kNewline = "\n";
303#endif
304
borenet09ed4802015-04-03 14:15:33 -0700305static SkTHashSet<SkString> gUninterestingHashes;
306
307static void gather_uninteresting_hashes() {
308 if (!FLAGS_uninterestingHashesFile.isEmpty()) {
bungeman38d909e2016-08-02 14:40:46 -0700309 sk_sp<SkData> data(SkData::MakeFromFileName(FLAGS_uninterestingHashesFile[0]));
mtkleincc334b32015-09-22 11:43:53 -0700310 if (!data) {
mtkleinc41fd922016-03-08 09:01:39 -0800311 info("WARNING: unable to read uninteresting hashes from %s\n",
312 FLAGS_uninterestingHashesFile[0]);
mtkleincc334b32015-09-22 11:43:53 -0700313 return;
314 }
borenet09ed4802015-04-03 14:15:33 -0700315 SkTArray<SkString> hashes;
mtklein935f1b12016-03-16 08:37:19 -0700316 SkStrSplit((const char*)data->data(), kNewline, &hashes);
borenet09ed4802015-04-03 14:15:33 -0700317 for (const SkString& hash : hashes) {
318 gUninterestingHashes.add(hash);
319 }
mtkleinc41fd922016-03-08 09:01:39 -0800320 info("FYI: loaded %d distinct uninteresting hashes from %d lines\n",
321 gUninterestingHashes.count(), hashes.count());
borenet09ed4802015-04-03 14:15:33 -0700322 }
323}
324
325/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
326
mtkleine0effd62015-07-29 06:37:28 -0700327struct TaggedSrc : public SkAutoTDelete<Src> {
mtklein3eed7dd2016-02-24 19:07:07 -0800328 SkString tag;
329 SkString options;
mtkleine0effd62015-07-29 06:37:28 -0700330};
331
332struct TaggedSink : public SkAutoTDelete<Sink> {
kkinnunen3e980c32015-12-23 01:33:00 -0800333 SkString tag;
djsollen54416de2015-04-03 07:24:48 -0700334};
mtklein748ca3b2015-01-15 10:56:12 -0800335
336static const bool kMemcpyOK = true;
337
mtkleine0effd62015-07-29 06:37:28 -0700338static SkTArray<TaggedSrc, kMemcpyOK> gSrcs;
339static SkTArray<TaggedSink, kMemcpyOK> gSinks;
mtklein748ca3b2015-01-15 10:56:12 -0800340
mtklein6393c062015-04-27 08:45:01 -0700341static bool in_shard() {
342 static int N = 0;
343 return N++ % FLAGS_shards == FLAGS_shard;
344}
345
mtklein3eed7dd2016-02-24 19:07:07 -0800346static void push_src(const char* tag, ImplicitString options, Src* s) {
mtklein748ca3b2015-01-15 10:56:12 -0800347 SkAutoTDelete<Src> src(s);
mtklein6393c062015-04-27 08:45:01 -0700348 if (in_shard() &&
mtklein3eed7dd2016-02-24 19:07:07 -0800349 FLAGS_src.contains(tag) &&
mtklein748ca3b2015-01-15 10:56:12 -0800350 !SkCommandLineFlags::ShouldSkip(FLAGS_match, src->name().c_str())) {
mtkleine0effd62015-07-29 06:37:28 -0700351 TaggedSrc& s = gSrcs.push_back();
mtklein18300a32016-03-16 13:53:35 -0700352 s.reset(src.release());
mtklein748ca3b2015-01-15 10:56:12 -0800353 s.tag = tag;
djsollen54416de2015-04-03 07:24:48 -0700354 s.options = options;
mtklein114c3cd2015-01-15 10:15:02 -0800355 }
mtklein748ca3b2015-01-15 10:56:12 -0800356}
mtklein114c3cd2015-01-15 10:15:02 -0800357
msarett9e707a02015-09-01 14:57:57 -0700358static void push_codec_src(Path path, CodecSrc::Mode mode, CodecSrc::DstColorType dstColorType,
scroggoc5560be2016-02-03 09:42:42 -0800359 SkAlphaType dstAlphaType, float scale) {
scroggoe4499842016-02-25 11:03:47 -0800360 if (FLAGS_simpleCodec) {
361 if (mode != CodecSrc::kCodec_Mode || dstColorType != CodecSrc::kGetFromCanvas_DstColorType
362 || scale != 1.0f)
363 // Only decode in the simple case.
364 return;
365 }
msarett9e707a02015-09-01 14:57:57 -0700366 SkString folder;
367 switch (mode) {
368 case CodecSrc::kCodec_Mode:
369 folder.append("codec");
370 break;
msarettbb25b532016-01-13 09:31:39 -0800371 case CodecSrc::kCodecZeroInit_Mode:
372 folder.append("codec_zero_init");
373 break;
msarett9e707a02015-09-01 14:57:57 -0700374 case CodecSrc::kScanline_Mode:
375 folder.append("scanline");
376 break;
msarett9e707a02015-09-01 14:57:57 -0700377 case CodecSrc::kStripe_Mode:
378 folder.append("stripe");
379 break;
msarett91c22b22016-02-22 12:27:46 -0800380 case CodecSrc::kCroppedScanline_Mode:
381 folder.append("crop");
382 break;
msarett9e707a02015-09-01 14:57:57 -0700383 case CodecSrc::kSubset_Mode:
msarett36c37962015-09-02 13:20:52 -0700384 folder.append("codec_subset");
msarett9e707a02015-09-01 14:57:57 -0700385 break;
386 }
387
388 switch (dstColorType) {
389 case CodecSrc::kGrayscale_Always_DstColorType:
390 folder.append("_kGray8");
391 break;
392 case CodecSrc::kIndex8_Always_DstColorType:
393 folder.append("_kIndex8");
394 break;
msarett34e0ec42016-04-22 16:27:24 -0700395 case CodecSrc::kNonNative8888_Always_DstColorType:
396 folder.append("_kNonNative");
397 break;
msarett9e707a02015-09-01 14:57:57 -0700398 default:
399 break;
400 }
401
scroggoc5560be2016-02-03 09:42:42 -0800402 switch (dstAlphaType) {
scroggoc5560be2016-02-03 09:42:42 -0800403 case kPremul_SkAlphaType:
404 folder.append("_premul");
405 break;
406 case kUnpremul_SkAlphaType:
407 folder.append("_unpremul");
408 break;
409 default:
410 break;
411 }
412
msarett9e707a02015-09-01 14:57:57 -0700413 if (1.0f != scale) {
414 folder.appendf("_%.3f", scale);
415 }
416
scroggoc5560be2016-02-03 09:42:42 -0800417 CodecSrc* src = new CodecSrc(path, mode, dstColorType, dstAlphaType, scale);
msarett9e707a02015-09-01 14:57:57 -0700418 push_src("image", folder, src);
419}
420
scroggof8dc9df2016-05-16 09:04:13 -0700421static void push_android_codec_src(Path path, CodecSrc::DstColorType dstColorType,
422 SkAlphaType dstAlphaType, int sampleSize) {
msarett3d9d7a72015-10-21 10:27:10 -0700423 SkString folder;
scroggof8dc9df2016-05-16 09:04:13 -0700424 folder.append("scaled_codec");
msarett3d9d7a72015-10-21 10:27:10 -0700425
426 switch (dstColorType) {
427 case CodecSrc::kGrayscale_Always_DstColorType:
428 folder.append("_kGray8");
429 break;
430 case CodecSrc::kIndex8_Always_DstColorType:
431 folder.append("_kIndex8");
432 break;
msarett34e0ec42016-04-22 16:27:24 -0700433 case CodecSrc::kNonNative8888_Always_DstColorType:
434 folder.append("_kNonNative");
435 break;
msarett3d9d7a72015-10-21 10:27:10 -0700436 default:
437 break;
438 }
439
scroggoc5560be2016-02-03 09:42:42 -0800440 switch (dstAlphaType) {
scroggoc5560be2016-02-03 09:42:42 -0800441 case kPremul_SkAlphaType:
442 folder.append("_premul");
443 break;
msarett9e9444c2016-02-03 12:39:10 -0800444 case kUnpremul_SkAlphaType:
445 folder.append("_unpremul");
446 break;
scroggoc5560be2016-02-03 09:42:42 -0800447 default:
448 break;
449 }
450
msarett3d9d7a72015-10-21 10:27:10 -0700451 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800452 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700453 }
454
scroggof8dc9df2016-05-16 09:04:13 -0700455 AndroidCodecSrc* src = new AndroidCodecSrc(path, dstColorType, dstAlphaType, sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700456 push_src("image", folder, src);
457}
458
msarett18976312016-03-09 14:20:58 -0800459static void push_image_gen_src(Path path, ImageGenSrc::Mode mode, SkAlphaType alphaType, bool isGpu)
460{
461 SkString folder;
462 switch (mode) {
463 case ImageGenSrc::kCodec_Mode:
464 folder.append("gen_codec");
465 break;
466 case ImageGenSrc::kPlatform_Mode:
467 folder.append("gen_platform");
468 break;
469 }
470
471 if (isGpu) {
472 folder.append("_gpu");
473 } else {
474 switch (alphaType) {
475 case kOpaque_SkAlphaType:
476 folder.append("_opaque");
477 break;
478 case kPremul_SkAlphaType:
479 folder.append("_premul");
480 break;
481 case kUnpremul_SkAlphaType:
482 folder.append("_unpremul");
483 break;
484 default:
485 break;
486 }
487 }
488
489 ImageGenSrc* src = new ImageGenSrc(path, mode, alphaType, isGpu);
490 push_src("image", folder, src);
491}
492
msarett438b2ad2015-04-09 12:43:10 -0700493static void push_codec_srcs(Path path) {
bungeman38d909e2016-08-02 14:40:46 -0700494 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
msarett438b2ad2015-04-09 12:43:10 -0700495 if (!encoded) {
mtkleinc41fd922016-03-08 09:01:39 -0800496 info("Couldn't read %s.", path.c_str());
msarett438b2ad2015-04-09 12:43:10 -0700497 return;
498 }
bungeman38d909e2016-08-02 14:40:46 -0700499 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded.get()));
halcanary96fcdcc2015-08-27 07:41:13 -0700500 if (nullptr == codec.get()) {
mtkleinc41fd922016-03-08 09:01:39 -0800501 info("Couldn't create codec for %s.", path.c_str());
msarett438b2ad2015-04-09 12:43:10 -0700502 return;
503 }
504
scroggo9366aff2016-05-20 08:38:54 -0700505 // native scaling is only supported by WEBP and JPEG
506 bool supportsNativeScaling = false;
msarett438b2ad2015-04-09 12:43:10 -0700507
msarett91c22b22016-02-22 12:27:46 -0800508 SkTArray<CodecSrc::Mode> nativeModes;
509 nativeModes.push_back(CodecSrc::kCodec_Mode);
510 nativeModes.push_back(CodecSrc::kCodecZeroInit_Mode);
msarett91c22b22016-02-22 12:27:46 -0800511 switch (codec->getEncodedFormat()) {
512 case SkEncodedFormat::kJPEG_SkEncodedFormat:
513 nativeModes.push_back(CodecSrc::kScanline_Mode);
514 nativeModes.push_back(CodecSrc::kStripe_Mode);
515 nativeModes.push_back(CodecSrc::kCroppedScanline_Mode);
scroggo9366aff2016-05-20 08:38:54 -0700516 supportsNativeScaling = true;
msarett91c22b22016-02-22 12:27:46 -0800517 break;
518 case SkEncodedFormat::kWEBP_SkEncodedFormat:
519 nativeModes.push_back(CodecSrc::kSubset_Mode);
scroggo9366aff2016-05-20 08:38:54 -0700520 supportsNativeScaling = true;
msarett91c22b22016-02-22 12:27:46 -0800521 break;
yujieqin7a307df2016-03-11 07:32:33 -0800522 case SkEncodedFormat::kDNG_SkEncodedFormat:
msarettb65e6042016-02-23 05:37:25 -0800523 break;
msarett91c22b22016-02-22 12:27:46 -0800524 default:
525 nativeModes.push_back(CodecSrc::kScanline_Mode);
msarett91c22b22016-02-22 12:27:46 -0800526 break;
527 }
msarett9e707a02015-09-01 14:57:57 -0700528
msarett91c22b22016-02-22 12:27:46 -0800529 SkTArray<CodecSrc::DstColorType> colorTypes;
530 colorTypes.push_back(CodecSrc::kGetFromCanvas_DstColorType);
msarett34e0ec42016-04-22 16:27:24 -0700531 colorTypes.push_back(CodecSrc::kNonNative8888_Always_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700532 switch (codec->getInfo().colorType()) {
533 case kGray_8_SkColorType:
msarett91c22b22016-02-22 12:27:46 -0800534 colorTypes.push_back(CodecSrc::kGrayscale_Always_DstColorType);
msarett55f7bdd2016-02-16 13:24:54 -0800535 if (kWBMP_SkEncodedFormat == codec->getEncodedFormat()) {
msarett91c22b22016-02-22 12:27:46 -0800536 colorTypes.push_back(CodecSrc::kIndex8_Always_DstColorType);
msarett55f7bdd2016-02-16 13:24:54 -0800537 }
msarett36c37962015-09-02 13:20:52 -0700538 break;
539 case kIndex_8_SkColorType:
msarett91c22b22016-02-22 12:27:46 -0800540 colorTypes.push_back(CodecSrc::kIndex8_Always_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700541 break;
542 default:
msarett36c37962015-09-02 13:20:52 -0700543 break;
544 }
msarett9e707a02015-09-01 14:57:57 -0700545
scroggoc5560be2016-02-03 09:42:42 -0800546 SkTArray<SkAlphaType> alphaModes;
547 alphaModes.push_back(kPremul_SkAlphaType);
scroggof2c96a22016-05-20 11:08:27 -0700548 if (codec->getInfo().alphaType() != kOpaque_SkAlphaType) {
549 alphaModes.push_back(kUnpremul_SkAlphaType);
scroggoc5560be2016-02-03 09:42:42 -0800550 }
msarettb714fb02016-01-22 14:46:42 -0800551
552 for (CodecSrc::Mode mode : nativeModes) {
scroggo9366aff2016-05-20 08:38:54 -0700553 for (CodecSrc::DstColorType colorType : colorTypes) {
554 for (SkAlphaType alphaType : alphaModes) {
scroggof2c96a22016-05-20 11:08:27 -0700555 // Only test kCroppedScanline_Mode when the alpha type is premul. The test is
scroggo9366aff2016-05-20 08:38:54 -0700556 // slow and won't be interestingly different with different alpha types.
557 if (CodecSrc::kCroppedScanline_Mode == mode &&
scroggof2c96a22016-05-20 11:08:27 -0700558 kPremul_SkAlphaType != alphaType) {
scroggo9366aff2016-05-20 08:38:54 -0700559 continue;
560 }
msarett91c22b22016-02-22 12:27:46 -0800561
scroggo9366aff2016-05-20 08:38:54 -0700562 push_codec_src(path, mode, colorType, alphaType, 1.0f);
msarettf3dc1882016-04-26 13:06:38 -0700563
scroggo9366aff2016-05-20 08:38:54 -0700564 // Skip kNonNative on different native scales. It won't be interestingly
565 // different.
566 if (supportsNativeScaling &&
567 CodecSrc::kNonNative8888_Always_DstColorType == colorType) {
568 // Native Scales
569 // SkJpegCodec natively supports scaling to the following:
570 for (auto scale : { 0.125f, 0.25f, 0.375f, 0.5f, 0.625f, 0.750f, 0.875f }) {
571 push_codec_src(path, mode, colorType, alphaType, scale);
572 }
scroggoc5560be2016-02-03 09:42:42 -0800573 }
msarett9e707a02015-09-01 14:57:57 -0700574 }
575 }
msarett0a242972015-06-11 14:27:27 -0700576 }
msarett36c37962015-09-02 13:20:52 -0700577
scroggoe4499842016-02-25 11:03:47 -0800578 if (FLAGS_simpleCodec) {
579 return;
580 }
581
msarett3d9d7a72015-10-21 10:27:10 -0700582 const int sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
msarett36c37962015-09-02 13:20:52 -0700583
msarett3d9d7a72015-10-21 10:27:10 -0700584 for (int sampleSize : sampleSizes) {
msarett91c22b22016-02-22 12:27:46 -0800585 for (CodecSrc::DstColorType colorType : colorTypes) {
scroggoc5560be2016-02-03 09:42:42 -0800586 for (SkAlphaType alphaType : alphaModes) {
msarettf3dc1882016-04-26 13:06:38 -0700587 // We can exercise all of the kNonNative support code in the swizzler with just a
588 // few sample sizes. Skip the rest.
589 if (CodecSrc::kNonNative8888_Always_DstColorType == colorType && sampleSize > 3) {
590 continue;
591 }
592
scroggof8dc9df2016-05-16 09:04:13 -0700593 push_android_codec_src(path, colorType, alphaType, sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700594 }
msarett36c37962015-09-02 13:20:52 -0700595 }
596 }
msarett18976312016-03-09 14:20:58 -0800597
598 static const char* const rawExts[] = {
599 "arw", "cr2", "dng", "nef", "nrw", "orf", "raf", "rw2", "pef", "srw",
600 "ARW", "CR2", "DNG", "NEF", "NRW", "ORF", "RAF", "RW2", "PEF", "SRW",
601 };
602
603 // There is not currently a reason to test RAW images on image generator.
604 // If we want to enable these tests, we will need to fix skbug.com/5079.
605 for (const char* ext : rawExts) {
606 if (path.endsWith(ext)) {
607 return;
608 }
609 }
610
611 // Push image generator GPU test.
brianosmanc9ced392016-03-24 07:27:43 -0700612 push_image_gen_src(path, ImageGenSrc::kCodec_Mode, codec->getInfo().alphaType(), true);
msarett18976312016-03-09 14:20:58 -0800613
614 // Push image generator CPU tests.
615 for (SkAlphaType alphaType : alphaModes) {
616 push_image_gen_src(path, ImageGenSrc::kCodec_Mode, alphaType, false);
617
618#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
619 if (kWEBP_SkEncodedFormat != codec->getEncodedFormat() &&
620 kWBMP_SkEncodedFormat != codec->getEncodedFormat() &&
621 kUnpremul_SkAlphaType != alphaType)
622 {
623 push_image_gen_src(path, ImageGenSrc::kPlatform_Mode, alphaType, false);
624 }
msarettfc0b6d12016-03-17 13:50:17 -0700625#elif defined(SK_BUILD_FOR_WIN)
626 if (kWEBP_SkEncodedFormat != codec->getEncodedFormat() &&
627 kWBMP_SkEncodedFormat != codec->getEncodedFormat())
628 {
629 push_image_gen_src(path, ImageGenSrc::kPlatform_Mode, alphaType, false);
630 }
msarett18976312016-03-09 14:20:58 -0800631#endif
632 }
msarett438b2ad2015-04-09 12:43:10 -0700633}
634
msarettd1227a72016-05-18 06:23:57 -0700635static void push_brd_src(Path path, CodecSrc::DstColorType dstColorType, BRDSrc::Mode mode,
636 uint32_t sampleSize) {
637 SkString folder("brd_android_codec");
msaretta5783ae2015-09-08 15:35:32 -0700638 switch (mode) {
639 case BRDSrc::kFullImage_Mode:
640 break;
641 case BRDSrc::kDivisor_Mode:
642 folder.append("_divisor");
643 break;
644 default:
645 SkASSERT(false);
646 return;
647 }
648
649 switch (dstColorType) {
650 case CodecSrc::kGetFromCanvas_DstColorType:
651 break;
652 case CodecSrc::kIndex8_Always_DstColorType:
653 folder.append("_kIndex");
654 break;
655 case CodecSrc::kGrayscale_Always_DstColorType:
656 folder.append("_kGray");
657 break;
658 default:
659 SkASSERT(false);
660 return;
661 }
662
663 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800664 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700665 }
666
msarettd1227a72016-05-18 06:23:57 -0700667 BRDSrc* src = new BRDSrc(path, mode, dstColorType, sampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700668 push_src("image", folder, src);
669}
670
671static void push_brd_srcs(Path path) {
scroggo24a9bd72016-05-20 06:02:42 -0700672 // Only run Index8 and grayscale to one sampleSize and Mode. Though interesting
673 // to test these color types, they should not reveal anything across various
674 // sampleSizes and Modes
675 for (auto type : { CodecSrc::kIndex8_Always_DstColorType,
676 CodecSrc::kGrayscale_Always_DstColorType }) {
677 // Arbitrarily choose Mode and sampleSize.
678 push_brd_src(path, type, BRDSrc::kFullImage_Mode, 2);
679 }
680
681
scroggo501b7342015-11-03 07:55:11 -0800682 // Test on a variety of sampleSizes, making sure to include:
683 // - 2, 4, and 8, which are natively supported by jpeg
684 // - multiples of 2 which are not divisible by 4 (analogous for 4)
685 // - larger powers of two, since BRD clients generally use powers of 2
686 // We will only produce output for the larger sizes on large images.
687 const uint32_t sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 24, 32, 64 };
msarett6efbe052015-09-11 09:01:16 -0700688
msaretta5783ae2015-09-08 15:35:32 -0700689 const BRDSrc::Mode modes[] = {
msarett26ad17b2015-10-22 07:29:19 -0700690 BRDSrc::kFullImage_Mode,
691 BRDSrc::kDivisor_Mode,
msaretta5783ae2015-09-08 15:35:32 -0700692 };
693
msarettd1227a72016-05-18 06:23:57 -0700694 for (uint32_t sampleSize : sampleSizes) {
scroggo24a9bd72016-05-20 06:02:42 -0700695 for (BRDSrc::Mode mode : modes) {
696 push_brd_src(path, CodecSrc::kGetFromCanvas_DstColorType, mode, sampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700697 }
698 }
699}
700
701static bool brd_supported(const char* ext) {
msarett8c8f22a2015-04-01 06:58:48 -0700702 static const char* const exts[] = {
msaretta5783ae2015-09-08 15:35:32 -0700703 "jpg", "jpeg", "png", "webp",
704 "JPG", "JPEG", "PNG", "WEBP",
msarett8c8f22a2015-04-01 06:58:48 -0700705 };
706
707 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
708 if (0 == strcmp(exts[i], ext)) {
709 return true;
710 }
711 }
712 return false;
scroggo9b77ddd2015-03-19 06:03:39 -0700713}
714
fmalitaa2b9fdf2016-08-03 19:53:36 -0700715template <typename T>
716void gather_file_srcs(const SkCommandLineFlags::StringArray& flags, const char* ext) {
717 for (int i = 0; i < flags.count(); i++) {
718 const char* path = flags[i];
719 if (sk_isdir(path)) {
720 SkOSFile::Iter it(path, ext);
721 for (SkString file; it.next(&file); ) {
722 push_src(ext, "", new T(SkOSPath::Join(path, file.c_str())));
723 }
724 } else {
725 push_src(ext, "", new T(path));
726 }
727 }
728}
729
scroggo86737142016-02-03 12:19:11 -0800730static bool gather_srcs() {
mtklein748ca3b2015-01-15 10:56:12 -0800731 for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) {
djsollen54416de2015-04-03 07:24:48 -0700732 push_src("gm", "", new GMSrc(r->factory()));
mtklein748ca3b2015-01-15 10:56:12 -0800733 }
scroggo86737142016-02-03 12:19:11 -0800734
fmalitaa2b9fdf2016-08-03 19:53:36 -0700735 gather_file_srcs<SKPSrc>(FLAGS_skps, "skp");
736 gather_file_srcs<MSKPSrc>(FLAGS_mskps, "mskp");
737#if defined(SK_XML)
738 gather_file_srcs<SVGSrc>(FLAGS_svgs, "svg");
739#endif
halcanary45420a92016-06-02 12:41:14 -0700740
scroggo86737142016-02-03 12:19:11 -0800741 SkTArray<SkString> images;
msarett69deca82016-04-29 09:38:40 -0700742 if (!CollectImages(FLAGS_images, &images)) {
scroggo86737142016-02-03 12:19:11 -0800743 return false;
744 }
745
746 for (auto image : images) {
747 push_codec_srcs(image);
scroggoe4499842016-02-25 11:03:47 -0800748 if (FLAGS_simpleCodec) {
749 continue;
750 }
751
mtklein21eaf3b2016-02-08 12:39:59 -0800752 const char* ext = strrchr(image.c_str(), '.');
753 if (ext && brd_supported(ext+1)) {
scroggo86737142016-02-03 12:19:11 -0800754 push_brd_srcs(image);
mtklein709d2c32015-01-15 08:30:25 -0800755 }
mtklein709d2c32015-01-15 08:30:25 -0800756 }
scroggo86737142016-02-03 12:19:11 -0800757
msarett69deca82016-04-29 09:38:40 -0700758 SkTArray<SkString> colorImages;
759 if (!CollectImages(FLAGS_colorImages, &colorImages)) {
760 return false;
761 }
762
763 for (auto colorImage : colorImages) {
msarett9ce3a542016-07-15 13:54:38 -0700764 ColorCodecSrc* src = new ColorCodecSrc(colorImage, ColorCodecSrc::kBaseline_Mode,
765 kN32_SkColorType);
msarettb8d1aac2016-08-04 12:22:06 -0700766 push_src("colorImage", "color_codec_baseline", src);
msarett888dc162016-05-23 10:21:17 -0700767
msarett9ce3a542016-07-15 13:54:38 -0700768 src = new ColorCodecSrc(colorImage, ColorCodecSrc::kDst_HPZR30w_Mode, kN32_SkColorType);
msarettb8d1aac2016-08-04 12:22:06 -0700769 push_src("colorImage", "color_codec_HPZR30w", src);
msarett9ce3a542016-07-15 13:54:38 -0700770 // TODO (msarett):
771 // 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 -0700772
msarett9ce3a542016-07-15 13:54:38 -0700773 src = new ColorCodecSrc(colorImage, ColorCodecSrc::kDst_sRGB_Mode, kN32_SkColorType);
msarettb8d1aac2016-08-04 12:22:06 -0700774 push_src("colorImage", "color_codec_sRGB_kN32", src);
msarett9ce3a542016-07-15 13:54:38 -0700775 src = new ColorCodecSrc(colorImage, ColorCodecSrc::kDst_sRGB_Mode, kRGBA_F16_SkColorType);
msarettb8d1aac2016-08-04 12:22:06 -0700776 push_src("colorImage", "color_codec_sRGB_kF16", src);
msarettd2809572016-06-20 06:07:45 -0700777
msarett469f1c52016-06-06 08:20:37 -0700778#if defined(SK_TEST_QCMS)
msarett50ce1f22016-07-29 06:23:33 -0700779 src = new ColorCodecSrc(colorImage, ColorCodecSrc::kQCMS_HPZR30w_Mode,
780 kRGBA_8888_SkColorType);
msarettb8d1aac2016-08-04 12:22:06 -0700781 push_src("colorImage", "color_codec_QCMS_HPZR30w", src);
msarett9876ac52016-06-01 14:47:18 -0700782#endif
msarett69deca82016-04-29 09:38:40 -0700783 }
784
scroggo86737142016-02-03 12:19:11 -0800785 return true;
mtklein709d2c32015-01-15 08:30:25 -0800786}
787
kkinnunen3e980c32015-12-23 01:33:00 -0800788static void push_sink(const SkCommandLineConfig& config, Sink* s) {
rmistry0f515bd2015-12-22 10:22:26 -0800789 SkAutoTDelete<Sink> sink(s);
kkinnunen3e980c32015-12-23 01:33:00 -0800790
mtkleine0effd62015-07-29 06:37:28 -0700791 // Try a simple Src as a canary. If it fails, skip this sink.
mtklein748ca3b2015-01-15 10:56:12 -0800792 struct : public Src {
mtkleine0effd62015-07-29 06:37:28 -0700793 Error draw(SkCanvas* c) const override {
794 c->drawRect(SkRect::MakeWH(1,1), SkPaint());
795 return "";
796 }
mtklein36352bf2015-03-25 18:17:31 -0700797 SkISize size() const override { return SkISize::Make(16, 16); }
mtkleine0effd62015-07-29 06:37:28 -0700798 Name name() const override { return "justOneRect"; }
799 } justOneRect;
mtklein114c3cd2015-01-15 10:15:02 -0800800
mtklein748ca3b2015-01-15 10:56:12 -0800801 SkBitmap bitmap;
802 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800803 SkString log;
mtkleine0effd62015-07-29 06:37:28 -0700804 Error err = sink->draw(justOneRect, &bitmap, &stream, &log);
mtklein4089ef72015-03-05 08:40:28 -0800805 if (err.isFatal()) {
mtkleinc41fd922016-03-08 09:01:39 -0800806 info("Could not run %s: %s\n", config.getTag().c_str(), err.c_str());
mtklein05641a52015-04-21 10:49:13 -0700807 exit(1);
mtklein114c3cd2015-01-15 10:15:02 -0800808 }
809
mtkleine0effd62015-07-29 06:37:28 -0700810 TaggedSink& ts = gSinks.push_back();
mtklein18300a32016-03-16 13:53:35 -0700811 ts.reset(sink.release());
kkinnunen3e980c32015-12-23 01:33:00 -0800812 ts.tag = config.getTag();
mtklein748ca3b2015-01-15 10:56:12 -0800813}
814
815static bool gpu_supported() {
816#if SK_SUPPORT_GPU
817 return FLAGS_gpu;
818#else
819 return false;
820#endif
821}
kkinnunen9ebc3f02015-12-21 23:48:13 -0800822
kkinnunen3e980c32015-12-23 01:33:00 -0800823static Sink* create_sink(const SkCommandLineConfig* config) {
824#if SK_SUPPORT_GPU
825 if (gpu_supported()) {
826 if (const SkCommandLineConfigGpu* gpuConfig = config->asConfigGpu()) {
bsalomon85b4b532016-04-05 11:06:27 -0700827 GrContextFactory::ContextType contextType = gpuConfig->getContextType();
828 GrContextFactory::ContextOptions contextOptions =
829 GrContextFactory::kNone_ContextOptions;
kkinnunen3e980c32015-12-23 01:33:00 -0800830 if (gpuConfig->getUseNVPR()) {
bsalomon85b4b532016-04-05 11:06:27 -0700831 contextOptions = static_cast<GrContextFactory::ContextOptions>(
832 contextOptions | GrContextFactory::kEnableNVPR_ContextOptions);
kkinnunen3e980c32015-12-23 01:33:00 -0800833 }
csmartdaltone0d36292016-07-29 08:14:20 -0700834 if (gpuConfig->getUseInstanced()) {
835 contextOptions = static_cast<GrContextFactory::ContextOptions>(
836 contextOptions | GrContextFactory::kUseInstanced_ContextOptions);
837 }
brianosmanb109b8c2016-06-16 13:03:24 -0700838 if (SkColorAndColorSpaceAreGammaCorrect(gpuConfig->getColorType(),
839 gpuConfig->getColorSpace())) {
bsalomon85b4b532016-04-05 11:06:27 -0700840 contextOptions = static_cast<GrContextFactory::ContextOptions>(
841 contextOptions | GrContextFactory::kRequireSRGBSupport_ContextOptions);
brianosman61d3b082016-03-30 11:19:36 -0700842 }
kkinnunen3e980c32015-12-23 01:33:00 -0800843 GrContextFactory testFactory;
844 if (!testFactory.get(contextType, contextOptions)) {
mtkleinc41fd922016-03-08 09:01:39 -0800845 info("WARNING: can not create GPU context for config '%s'. "
846 "GM tests will be skipped.\n", gpuConfig->getTag().c_str());
kkinnunen3e980c32015-12-23 01:33:00 -0800847 return nullptr;
848 }
849 return new GPUSink(contextType, contextOptions, gpuConfig->getSamples(),
brianosmand93c1202016-03-10 07:49:08 -0800850 gpuConfig->getUseDIText(), gpuConfig->getColorType(),
brianosmanb109b8c2016-06-16 13:03:24 -0700851 sk_ref_sp(gpuConfig->getColorSpace()), FLAGS_gpu_threading);
kkinnunen3e980c32015-12-23 01:33:00 -0800852 }
853 }
854#endif
855
856#define SINK(t, sink, ...) if (config->getBackend().equals(t)) { return new sink(__VA_ARGS__); }
857
tomhudsoneebc39a2015-02-23 12:18:05 -0800858#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
859 SINK("hwui", HWUISink);
860#endif
861
mtklein748ca3b2015-01-15 10:56:12 -0800862 if (FLAGS_cpu) {
brianosmanb109b8c2016-06-16 13:03:24 -0700863 auto srgbColorSpace = SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named);
864
mtklein748ca3b2015-01-15 10:56:12 -0800865 SINK("565", RasterSink, kRGB_565_SkColorType);
866 SINK("8888", RasterSink, kN32_SkColorType);
brianosmanb109b8c2016-06-16 13:03:24 -0700867 SINK("srgb", RasterSink, kN32_SkColorType, srgbColorSpace);
brianosman0e22eb82016-08-30 07:07:59 -0700868 SINK("f16", RasterSink, kRGBA_F16_SkColorType, srgbColorSpace->makeLinearGamma());
halcanary94b87bd2016-04-04 06:12:15 -0700869 SINK("pdf", PDFSink);
mtklein9c3f17d2015-01-28 11:35:18 -0800870 SINK("skp", SKPSink);
mtklein8a4527e2015-01-31 20:00:58 -0800871 SINK("svg", SVGSink);
872 SINK("null", NullSink);
halcanary47ef4d52015-03-03 09:13:09 -0800873 SINK("xps", XPSSink);
halcanary4b656662016-04-27 07:45:18 -0700874 SINK("pdfa", PDFSink, true);
mtklein748ca3b2015-01-15 10:56:12 -0800875 }
876#undef SINK
halcanary96fcdcc2015-08-27 07:41:13 -0700877 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800878}
879
kkinnunen3e980c32015-12-23 01:33:00 -0800880static Sink* create_via(const SkString& tag, Sink* wrapped) {
881#define VIA(t, via, ...) if (tag.equals(t)) { return new via(__VA_ARGS__); }
mtklein9c5052f2016-08-06 12:51:51 -0700882 VIA("lite", ViaLite, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700883 VIA("twice", ViaTwice, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700884 VIA("serialize", ViaSerialization, wrapped);
mtklein4a34ecb2016-01-08 10:19:35 -0800885 VIA("pic", ViaPicture, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700886 VIA("2ndpic", ViaSecondPicture, wrapped);
mtkleind31c13d2015-05-05 12:59:56 -0700887 VIA("sp", ViaSingletonPictures, wrapped);
reedbabc3de2016-07-08 08:43:27 -0700888 VIA("defer", ViaDefer, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700889 VIA("tiles", ViaTiles, 256, 256, nullptr, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800890 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800891
mtkleind603b222015-02-17 11:13:33 -0800892 if (FLAGS_matrix.count() == 4) {
mtklein748ca3b2015-01-15 10:56:12 -0800893 SkMatrix m;
mtkleind603b222015-02-17 11:13:33 -0800894 m.reset();
895 m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
896 m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
897 m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
898 m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
899 VIA("matrix", ViaMatrix, m, wrapped);
900 VIA("upright", ViaUpright, m, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800901 }
tomhudson64de1e12015-03-05 08:01:07 -0800902
903#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
904 VIA("androidsdk", ViaAndroidSDK, wrapped);
905#endif
906
mtklein748ca3b2015-01-15 10:56:12 -0800907#undef VIA
halcanary96fcdcc2015-08-27 07:41:13 -0700908 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800909}
910
brianosman05c987c2016-05-09 13:42:16 -0700911static bool gather_sinks() {
kkinnunen3e980c32015-12-23 01:33:00 -0800912 SkCommandLineConfigArray configs;
913 ParseConfigs(FLAGS_config, &configs);
914 for (int i = 0; i < configs.count(); i++) {
915 const SkCommandLineConfig& config = *configs[i];
916 Sink* sink = create_sink(&config);
917 if (sink == nullptr) {
mtkleinc41fd922016-03-08 09:01:39 -0800918 info("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
919 config.getTag().c_str());
kkinnunen3e980c32015-12-23 01:33:00 -0800920 continue;
921 }
mtklein748ca3b2015-01-15 10:56:12 -0800922
kkinnunen3e980c32015-12-23 01:33:00 -0800923 const SkTArray<SkString>& parts = config.getViaParts();
924 for (int j = parts.count(); j-- > 0;) {
925 const SkString& part = parts[j];
926 Sink* next = create_via(part, sink);
halcanary96fcdcc2015-08-27 07:41:13 -0700927 if (next == nullptr) {
mtkleinc41fd922016-03-08 09:01:39 -0800928 info("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
929 part.c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800930 delete sink;
halcanary96fcdcc2015-08-27 07:41:13 -0700931 sink = nullptr;
mtklein748ca3b2015-01-15 10:56:12 -0800932 break;
933 }
934 sink = next;
935 }
936 if (sink) {
937 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800938 }
939 }
brianosman05c987c2016-05-09 13:42:16 -0700940
941 // If no configs were requested (just running tests, perhaps?), then we're okay.
942 // Otherwise, make sure that at least one sink was constructed correctly. This catches
943 // the case of bots without a GPU being assigned GPU configs.
944 return (configs.count() == 0) || (gSinks.count() > 0);
mtklein114c3cd2015-01-15 10:15:02 -0800945}
mtklein709d2c32015-01-15 08:30:25 -0800946
mtkleina5114d72015-08-24 13:27:01 -0700947static bool dump_png(SkBitmap bitmap, const char* path, const char* md5) {
948 const int w = bitmap.width(),
949 h = bitmap.height();
950
brianosman3c579dc2016-04-19 09:18:11 -0700951 sk_sp<SkData> encodedBitmap = sk_tools::encode_bitmap_for_png(bitmap);
952 if (encodedBitmap.get() == nullptr) {
953 return false;
mtkleina5114d72015-08-24 13:27:01 -0700954 }
brianosman3c579dc2016-04-19 09:18:11 -0700955 uint32_t* rgba = static_cast<uint32_t*>(encodedBitmap.get()->writable_data());
mtkleina5114d72015-08-24 13:27:01 -0700956
957 // We don't need bitmap anymore. Might as well drop our ref.
mtkleinfccb77d2015-08-24 14:13:29 -0700958 bitmap.reset();
mtkleina5114d72015-08-24 13:27:01 -0700959
mtkleinc64137c2015-08-25 10:56:08 -0700960 FILE* f = fopen(path, "wb");
mtkleina5114d72015-08-24 13:27:01 -0700961 if (!f) { return false; }
962
963 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
964 if (!png) {
965 fclose(f);
966 return false;
967 }
968
969 png_infop info = png_create_info_struct(png);
970 if (!info) {
971 png_destroy_write_struct(&png, &info);
972 fclose(f);
973 return false;
974 }
975
mtkleind69ece62015-09-10 10:37:44 -0700976 SkString description;
977 description.append("Key: ");
978 for (int i = 0; i < FLAGS_key.count(); i++) {
979 description.appendf("%s ", FLAGS_key[i]);
980 }
981 description.append("Properties: ");
982 for (int i = 0; i < FLAGS_properties.count(); i++) {
983 description.appendf("%s ", FLAGS_properties[i]);
984 }
985 description.appendf("MD5: %s", md5);
986
mtkleina5114d72015-08-24 13:27:01 -0700987 png_text text[2];
988 text[0].key = (png_charp)"Author";
989 text[0].text = (png_charp)"DM dump_png()";
990 text[0].compression = PNG_TEXT_COMPRESSION_NONE;
991 text[1].key = (png_charp)"Description";
mtkleind69ece62015-09-10 10:37:44 -0700992 text[1].text = (png_charp)description.c_str();
mtkleina5114d72015-08-24 13:27:01 -0700993 text[1].compression = PNG_TEXT_COMPRESSION_NONE;
994 png_set_text(png, info, text, 2);
995
996 png_init_io(png, f);
997 png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8,
998 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
999 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
1000 png_write_info(png, info);
1001 for (int j = 0; j < h; j++) {
brianosman3c579dc2016-04-19 09:18:11 -07001002 png_bytep row = (png_bytep)(rgba + w*j);
mtkleina5114d72015-08-24 13:27:01 -07001003 png_write_rows(png, &row, 1);
1004 }
1005 png_write_end(png, info);
1006
1007 png_destroy_write_struct(&png, &info);
1008 fclose(f);
1009 return true;
1010}
1011
mtkleina2ef6422015-01-15 13:44:22 -08001012static bool match(const char* needle, const char* haystack) {
halcanary96fcdcc2015-08-27 07:41:13 -07001013 return 0 == strcmp("_", needle) || nullptr != strstr(haystack, needle);
mtkleina2ef6422015-01-15 13:44:22 -08001014}
1015
mtklein3eed7dd2016-02-24 19:07:07 -08001016static bool is_blacklisted(const char* sink, const char* src,
1017 const char* srcOptions, const char* name) {
mtklein0d243ff2015-04-03 07:51:00 -07001018 for (int i = 0; i < FLAGS_blacklist.count() - 3; i += 4) {
mtkleina2ef6422015-01-15 13:44:22 -08001019 if (match(FLAGS_blacklist[i+0], sink) &&
djsollen54416de2015-04-03 07:24:48 -07001020 match(FLAGS_blacklist[i+1], src) &&
1021 match(FLAGS_blacklist[i+2], srcOptions) &&
1022 match(FLAGS_blacklist[i+3], name)) {
mtklein3eed7dd2016-02-24 19:07:07 -08001023 return true;
mtkleina2ef6422015-01-15 13:44:22 -08001024 }
1025 }
mtklein3eed7dd2016-02-24 19:07:07 -08001026 return false;
mtkleina2ef6422015-01-15 13:44:22 -08001027}
1028
mtkleincd50bca2016-01-05 06:20:20 -08001029// Even when a Task Sink reports to be non-threadsafe (e.g. GPU), we know things like
1030// .png encoding are definitely thread safe. This lets us offload that work to CPU threads.
1031static SkTaskGroup gDefinitelyThreadSafeWork;
1032
mtklein748ca3b2015-01-15 10:56:12 -08001033// The finest-grained unit of work we can run: draw a single Src into a single Sink,
1034// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
1035struct Task {
mtkleine0effd62015-07-29 06:37:28 -07001036 Task(const TaggedSrc& src, const TaggedSink& sink) : src(src), sink(sink) {}
1037 const TaggedSrc& src;
1038 const TaggedSink& sink;
mtklein748ca3b2015-01-15 10:56:12 -08001039
mtklein21eaf3b2016-02-08 12:39:59 -08001040 static void Run(const Task& task) {
1041 SkString name = task.src->name();
mtkleine0effd62015-07-29 06:37:28 -07001042
mtkleinb9eb4ac2015-02-02 18:26:03 -08001043 SkString log;
mtklein3eed7dd2016-02-24 19:07:07 -08001044 if (!FLAGS_dryRun) {
mtklein748ca3b2015-01-15 10:56:12 -08001045 SkBitmap bitmap;
1046 SkDynamicMemoryWStream stream;
mtklein3eed7dd2016-02-24 19:07:07 -08001047 start(task.sink.tag.c_str(), task.src.tag.c_str(),
1048 task.src.options.c_str(), name.c_str());
mtklein21eaf3b2016-02-08 12:39:59 -08001049 Error err = task.sink->draw(*task.src, &bitmap, &stream, &log);
mtkleinb3b13b72016-03-07 13:20:52 -08001050 if (!log.isEmpty()) {
mtkleinc41fd922016-03-08 09:01:39 -08001051 info("%s %s %s %s:\n%s\n", task.sink.tag.c_str()
1052 , task.src.tag.c_str()
1053 , task.src.options.c_str()
1054 , name.c_str()
1055 , log.c_str());
mtkleinb3b13b72016-03-07 13:20:52 -08001056 }
mtklein748ca3b2015-01-15 10:56:12 -08001057 if (!err.isEmpty()) {
mtklein4089ef72015-03-05 08:40:28 -08001058 if (err.isFatal()) {
djsollen54416de2015-04-03 07:24:48 -07001059 fail(SkStringPrintf("%s %s %s %s: %s",
mtklein21eaf3b2016-02-08 12:39:59 -08001060 task.sink.tag.c_str(),
1061 task.src.tag.c_str(),
1062 task.src.options.c_str(),
mtklein4089ef72015-03-05 08:40:28 -08001063 name.c_str(),
1064 err.c_str()));
mtkleinb37cb412015-03-18 05:27:14 -07001065 } else {
mtklein3eed7dd2016-02-24 19:07:07 -08001066 done(task.sink.tag.c_str(), task.src.tag.c_str(),
1067 task.src.options.c_str(), name.c_str());
mtkleinba6ada72016-01-21 09:39:35 -08001068 return;
mtklein4089ef72015-03-05 08:40:28 -08001069 }
mtklein748ca3b2015-01-15 10:56:12 -08001070 }
mtklein62bd1a62015-01-27 14:46:26 -08001071
mtkleincd50bca2016-01-05 06:20:20 -08001072 // We're likely switching threads here, so we must capture by value, [=] or [foo,bar].
1073 SkStreamAsset* data = stream.detachAsStream();
1074 gDefinitelyThreadSafeWork.add([task,name,bitmap,data]{
1075 SkAutoTDelete<SkStreamAsset> ownedData(data);
1076
1077 // Why doesn't the copy constructor do this when we have pre-locked pixels?
1078 bitmap.lockPixels();
1079
1080 SkString md5;
1081 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
1082 SkMD5 hash;
1083 if (data->getLength()) {
1084 hash.writeStream(data, data->getLength());
1085 data->rewind();
mtklein38408462015-07-08 07:25:27 -07001086 } else {
mtkleincd50bca2016-01-05 06:20:20 -08001087 // If we're BGRA (Linux, Windows), swizzle over to RGBA (Mac, Android).
1088 // This helps eliminate multiple 0-pixel-diff hashes on gold.skia.org.
1089 // (Android's general slow speed breaks the tie arbitrarily in RGBA's favor.)
1090 // We might consider promoting 565 to RGBA too.
1091 if (bitmap.colorType() == kBGRA_8888_SkColorType) {
1092 SkBitmap swizzle;
1093 SkAssertResult(bitmap.copyTo(&swizzle, kRGBA_8888_SkColorType));
1094 hash.write(swizzle.getPixels(), swizzle.getSize());
1095 } else {
1096 hash.write(bitmap.getPixels(), bitmap.getSize());
1097 }
1098 }
1099 SkMD5::Digest digest;
1100 hash.finish(digest);
1101 for (int i = 0; i < 16; i++) {
1102 md5.appendf("%02x", digest.data[i]);
mtklein38408462015-07-08 07:25:27 -07001103 }
mtklein62bd1a62015-01-27 14:46:26 -08001104 }
mtklein62bd1a62015-01-27 14:46:26 -08001105
mtkleincd50bca2016-01-05 06:20:20 -08001106 if (!FLAGS_readPath.isEmpty() &&
mtklein3eed7dd2016-02-24 19:07:07 -08001107 !gGold.contains(Gold(task.sink.tag, task.src.tag,
1108 task.src.options, name, md5))) {
mtkleincd50bca2016-01-05 06:20:20 -08001109 fail(SkStringPrintf("%s not found for %s %s %s %s in %s",
1110 md5.c_str(),
mtklein21eaf3b2016-02-08 12:39:59 -08001111 task.sink.tag.c_str(),
1112 task.src.tag.c_str(),
1113 task.src.options.c_str(),
mtkleincd50bca2016-01-05 06:20:20 -08001114 name.c_str(),
1115 FLAGS_readPath[0]));
mtklein748ca3b2015-01-15 10:56:12 -08001116 }
mtkleincd50bca2016-01-05 06:20:20 -08001117
1118 if (!FLAGS_writePath.isEmpty()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001119 const char* ext = task.sink->fileExtension();
mtkleincd50bca2016-01-05 06:20:20 -08001120 if (data->getLength()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001121 WriteToDisk(task, md5, ext, data, data->getLength(), nullptr);
mtkleincd50bca2016-01-05 06:20:20 -08001122 SkASSERT(bitmap.drawsNothing());
1123 } else if (!bitmap.drawsNothing()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001124 WriteToDisk(task, md5, ext, nullptr, 0, &bitmap);
mtkleincd50bca2016-01-05 06:20:20 -08001125 }
1126 }
1127 });
mtklein748ca3b2015-01-15 10:56:12 -08001128 }
mtklein3eed7dd2016-02-24 19:07:07 -08001129 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 -08001130 }
1131
1132 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -08001133 SkString md5,
1134 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -08001135 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -08001136 const SkBitmap* bitmap) {
mtklein409d4702016-02-29 07:38:01 -08001137 bool gammaCorrect = false;
1138 if (bitmap) {
brianosman898235c2016-04-06 07:38:23 -07001139 gammaCorrect = SkImageInfoIsGammaCorrect(bitmap->info());
mtklein409d4702016-02-29 07:38:01 -08001140 }
1141
mtklein748ca3b2015-01-15 10:56:12 -08001142 JsonWriter::BitmapResult result;
djsollen54416de2015-04-03 07:24:48 -07001143 result.name = task.src->name();
mtklein3eed7dd2016-02-24 19:07:07 -08001144 result.config = task.sink.tag;
djsollen54416de2015-04-03 07:24:48 -07001145 result.sourceType = task.src.tag;
1146 result.sourceOptions = task.src.options;
1147 result.ext = ext;
mtklein409d4702016-02-29 07:38:01 -08001148 result.gammaCorrect = gammaCorrect;
djsollen54416de2015-04-03 07:24:48 -07001149 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -08001150 JsonWriter::AddBitmapResult(result);
1151
mtkleinb0531a72015-04-07 13:38:48 -07001152 // If an MD5 is uninteresting, we want it noted in the JSON file,
1153 // but don't want to dump it out as a .png (or whatever ext is).
1154 if (gUninterestingHashes.contains(md5)) {
1155 return;
1156 }
1157
mtklein748ca3b2015-01-15 10:56:12 -08001158 const char* dir = FLAGS_writePath[0];
1159 if (0 == strcmp(dir, "@")) { // Needed for iOS.
1160 dir = FLAGS_resourcePath[0];
1161 }
1162 sk_mkdir(dir);
1163
1164 SkString path;
1165 if (FLAGS_nameByHash) {
1166 path = SkOSPath::Join(dir, result.md5.c_str());
1167 path.append(".");
1168 path.append(ext);
1169 if (sk_exists(path.c_str())) {
1170 return; // Content-addressed. If it exists already, we're done.
1171 }
1172 } else {
kkinnunen3e980c32015-12-23 01:33:00 -08001173 path = SkOSPath::Join(dir, task.sink.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001174 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001175 path = SkOSPath::Join(path.c_str(), task.src.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001176 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001177 if (strcmp(task.src.options.c_str(), "") != 0) {
1178 path = SkOSPath::Join(path.c_str(), task.src.options.c_str());
djsollen54416de2015-04-03 07:24:48 -07001179 sk_mkdir(path.c_str());
1180 }
mtklein748ca3b2015-01-15 10:56:12 -08001181 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
1182 path.append(".");
1183 path.append(ext);
1184 }
1185
mtklein748ca3b2015-01-15 10:56:12 -08001186 if (bitmap) {
mtkleina5114d72015-08-24 13:27:01 -07001187 if (!dump_png(*bitmap, path.c_str(), result.md5.c_str())) {
mtklein748ca3b2015-01-15 10:56:12 -08001188 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
1189 return;
1190 }
1191 } else {
mtkleina5114d72015-08-24 13:27:01 -07001192 SkFILEWStream file(path.c_str());
1193 if (!file.isValid()) {
1194 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
1195 return;
1196 }
mtklein748ca3b2015-01-15 10:56:12 -08001197 if (!file.writeStream(data, len)) {
1198 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
1199 return;
1200 }
1201 }
1202 }
1203};
1204
mtklein748ca3b2015-01-15 10:56:12 -08001205/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1206
1207// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
1208
mtklein21eaf3b2016-02-08 12:39:59 -08001209static SkTDArray<skiatest::Test> gParallelTests, gSerialTests;
mtklein748ca3b2015-01-15 10:56:12 -08001210
1211static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -08001212 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -08001213 return;
1214 }
mtklein6393c062015-04-27 08:45:01 -07001215 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r; r = r->next()) {
1216 if (!in_shard()) {
1217 continue;
1218 }
halcanary87f3ba42015-01-20 09:30:20 -08001219 // Despite its name, factory() is returning a reference to
1220 // link-time static const POD data.
1221 const skiatest::Test& test = r->factory();
1222 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -08001223 continue;
1224 }
halcanary87f3ba42015-01-20 09:30:20 -08001225 if (test.needsGpu && gpu_supported()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001226 (FLAGS_gpu_threading ? gParallelTests : gSerialTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -08001227 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein21eaf3b2016-02-08 12:39:59 -08001228 gParallelTests.push(test);
mtklein82d28432015-01-15 12:46:02 -08001229 }
mtklein748ca3b2015-01-15 10:56:12 -08001230 }
1231}
1232
mtklein21eaf3b2016-02-08 12:39:59 -08001233static void run_test(skiatest::Test test) {
halcanary87f3ba42015-01-20 09:30:20 -08001234 struct : public skiatest::Reporter {
mtklein36352bf2015-03-25 18:17:31 -07001235 void reportFailed(const skiatest::Failure& failure) override {
halcanary87f3ba42015-01-20 09:30:20 -08001236 fail(failure.toString());
1237 JsonWriter::AddTestFailure(failure);
1238 }
mtklein36352bf2015-03-25 18:17:31 -07001239 bool allowExtendedTest() const override {
halcanary87f3ba42015-01-20 09:30:20 -08001240 return FLAGS_pathOpsExtended;
1241 }
mtklein36352bf2015-03-25 18:17:31 -07001242 bool verbose() const override { return FLAGS_veryVerbose; }
halcanary87f3ba42015-01-20 09:30:20 -08001243 } reporter;
djsollen824996a2015-06-12 12:06:22 -07001244
mtklein3eed7dd2016-02-24 19:07:07 -08001245 if (!FLAGS_dryRun && !is_blacklisted("_", "tests", "_", test.name)) {
mtklein21eaf3b2016-02-08 12:39:59 -08001246 start("unit", "test", "", test.name);
mtklein55e88b22015-01-21 15:50:13 -08001247 GrContextFactory factory;
mtklein21eaf3b2016-02-08 12:39:59 -08001248 test.proc(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -08001249 }
mtklein3eed7dd2016-02-24 19:07:07 -08001250 done("unit", "test", "", test.name);
mtklein748ca3b2015-01-15 10:56:12 -08001251}
1252
1253/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1254
mtkleinafae30a2016-02-24 12:28:32 -08001255DEFINE_int32(status_sec, 15, "Print status this often (and if we crash).");
1256
1257SkThread* start_status_thread() {
1258 auto thread = new SkThread([] (void*) {
1259 for (;;) {
1260 print_status();
1261 #if defined(SK_BUILD_FOR_WIN)
1262 Sleep(FLAGS_status_sec * 1000);
1263 #else
1264 sleep(FLAGS_status_sec);
1265 #endif
mtklein2e1c47e2015-03-12 07:16:56 -07001266 }
mtkleinafae30a2016-02-24 12:28:32 -08001267 });
1268 thread->start();
1269 return thread;
mtkleinde6fc2b2015-03-12 06:28:54 -07001270}
1271
caryclark83ca6282015-06-10 09:31:09 -07001272#define PORTABLE_FONT_PREFIX "Toy Liberation "
1273
mbocee6a9912016-05-31 11:42:36 -07001274static sk_sp<SkTypeface> create_from_name(const char familyName[], SkFontStyle style) {
caryclark83ca6282015-06-10 09:31:09 -07001275 if (familyName && strlen(familyName) > sizeof(PORTABLE_FONT_PREFIX)
1276 && !strncmp(familyName, PORTABLE_FONT_PREFIX, sizeof(PORTABLE_FONT_PREFIX) - 1)) {
caryclark1818acb2015-07-24 12:09:25 -07001277 return sk_tool_utils::create_portable_typeface(familyName, style);
caryclark83ca6282015-06-10 09:31:09 -07001278 }
halcanary96fcdcc2015-08-27 07:41:13 -07001279 return nullptr;
caryclark83ca6282015-06-10 09:31:09 -07001280}
1281
1282#undef PORTABLE_FONT_PREFIX
1283
mbocee6a9912016-05-31 11:42:36 -07001284extern sk_sp<SkTypeface> (*gCreateTypefaceDelegate)(const char [], SkFontStyle );
caryclark83ca6282015-06-10 09:31:09 -07001285
jcgregorio3b27ade2014-11-13 08:06:40 -08001286int dm_main();
caryclark17f0b6d2014-07-22 10:15:34 -07001287int dm_main() {
bsalomon71de3532016-04-26 14:27:21 -07001288 setbuf(stdout, nullptr);
mtklein246ba3a2016-02-23 10:39:36 -08001289 setup_crash_handler();
mtkleinafae30a2016-02-24 12:28:32 -08001290
mtkleina483da32016-03-17 12:53:36 -07001291 if (FLAGS_verbose) {
1292 gVLog = stderr;
1293 } else if (!FLAGS_writePath.isEmpty()) {
mtklein51c8cfc2016-03-11 12:59:09 -08001294 sk_mkdir(FLAGS_writePath[0]);
mtkleina1ce2162016-07-22 12:23:46 -07001295 gVLog = fopen(SkOSPath::Join(FLAGS_writePath[0], "verbose.log").c_str(), "w");
mtkleinc41fd922016-03-08 09:01:39 -08001296 }
1297
mtklein5286f022016-01-22 08:18:14 -08001298 JsonWriter::DumpJson(); // It's handy for the bots to assume this is ~never missing.
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +00001299 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -07001300 SkTaskGroup::Enabler enabled(FLAGS_threads);
caryclark83ca6282015-06-10 09:31:09 -07001301 gCreateTypefaceDelegate = &create_from_name;
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +00001302
halcanary7d571242016-02-24 17:59:16 -08001303 {
1304 SkString testResourcePath = GetResourcePath("color_wheel.png");
1305 SkFILEStream testResource(testResourcePath.c_str());
1306 if (!testResource.isValid()) {
mtkleinc41fd922016-03-08 09:01:39 -08001307 info("Some resources are missing. Do you need to set --resourcePath?\n");
halcanary7d571242016-02-24 17:59:16 -08001308 }
1309 }
mtklein62bd1a62015-01-27 14:46:26 -08001310 gather_gold();
borenet09ed4802015-04-03 14:15:33 -07001311 gather_uninteresting_hashes();
mtklein62bd1a62015-01-27 14:46:26 -08001312
scroggo86737142016-02-03 12:19:11 -08001313 if (!gather_srcs()) {
1314 return 1;
1315 }
brianosman05c987c2016-05-09 13:42:16 -07001316 if (!gather_sinks()) {
1317 return 1;
1318 }
mtklein748ca3b2015-01-15 10:56:12 -08001319 gather_tests();
mtklein21eaf3b2016-02-08 12:39:59 -08001320 gPending = gSrcs.count() * gSinks.count() + gParallelTests.count() + gSerialTests.count();
mtkleinc41fd922016-03-08 09:01:39 -08001321 info("%d srcs * %d sinks + %d tests == %d tasks",
1322 gSrcs.count(), gSinks.count(), gParallelTests.count() + gSerialTests.count(), gPending);
mtkleinafae30a2016-02-24 12:28:32 -08001323 SkAutoTDelete<SkThread> statusThread(start_status_thread());
mtklein748ca3b2015-01-15 10:56:12 -08001324
mtklein21eaf3b2016-02-08 12:39:59 -08001325 // Kick off as much parallel work as we can, making note of any serial work we'll need to do.
1326 SkTaskGroup parallel;
1327 SkTArray<Task> serial;
1328
1329 for (auto& sink : gSinks)
1330 for (auto& src : gSrcs) {
mtklein3eed7dd2016-02-24 19:07:07 -08001331 if (src->veto(sink->flags()) ||
1332 is_blacklisted(sink.tag.c_str(), src.tag.c_str(),
1333 src.options.c_str(), src->name().c_str())) {
mtkleinf10637f2016-06-10 13:56:35 -07001334 SkAutoMutexAcquire lock(gMutex);
mtklein3eed7dd2016-02-24 19:07:07 -08001335 gPending--;
1336 continue;
1337 }
1338
mtklein21eaf3b2016-02-08 12:39:59 -08001339 Task task(src, sink);
1340 if (src->serial() || sink->serial()) {
1341 serial.push_back(task);
1342 } else {
1343 parallel.add([task] { Task::Run(task); });
mtklein748ca3b2015-01-15 10:56:12 -08001344 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001345 }
mtklein21eaf3b2016-02-08 12:39:59 -08001346 for (auto test : gParallelTests) {
1347 parallel.add([test] { run_test(test); });
mtklein55e88b22015-01-21 15:50:13 -08001348 }
mtklein21eaf3b2016-02-08 12:39:59 -08001349
1350 // With the parallel work running, run serial tasks and tests here on main thread.
1351 for (auto task : serial) { Task::Run(task); }
1352 for (auto test : gSerialTests) { run_test(test); }
1353
1354 // Wait for any remaining parallel work to complete (including any spun off of serial tasks).
1355 parallel.wait();
mtkleincd50bca2016-01-05 06:20:20 -08001356 gDefinitelyThreadSafeWork.wait();
1357
mtkleinda884c42016-02-24 18:00:23 -08001358 // We'd better have run everything.
1359 SkASSERT(gPending == 0);
mtkleine027f172016-02-26 15:53:06 -08001360 // Make sure we've flushed all our results to disk.
1361 JsonWriter::DumpJson();
mtkleinda884c42016-02-24 18:00:23 -08001362
mtklein748ca3b2015-01-15 10:56:12 -08001363 // At this point we're back in single-threaded land.
caryclarkf53ce802015-06-15 06:48:30 -07001364 sk_tool_utils::release_portable_typefaces();
mtklein@google.comd36522d2013-10-16 13:02:15 +00001365
mtklein748ca3b2015-01-15 10:56:12 -08001366 if (gFailures.count() > 0) {
mtkleinc41fd922016-03-08 09:01:39 -08001367 info("Failures:\n");
mtklein748ca3b2015-01-15 10:56:12 -08001368 for (int i = 0; i < gFailures.count(); i++) {
mtkleinc41fd922016-03-08 09:01:39 -08001369 info("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001370 }
mtkleinc41fd922016-03-08 09:01:39 -08001371 info("%d failures\n", gFailures.count());
mtklein748ca3b2015-01-15 10:56:12 -08001372 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001373 }
mtkleinafae30a2016-02-24 12:28:32 -08001374
1375#ifdef SK_PDF_IMAGE_STATS
halcanary7a14b312015-10-01 07:28:13 -07001376 SkPDFImageDumpStats();
mtkleinafae30a2016-02-24 12:28:32 -08001377#endif // SK_PDF_IMAGE_STATS
1378
1379 print_status();
mtkleinc41fd922016-03-08 09:01:39 -08001380 info("Finished!\n");
mtklein748ca3b2015-01-15 10:56:12 -08001381 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +00001382}
jcgregorio3b27ade2014-11-13 08:06:40 -08001383
kkinnunen179a8f52015-11-20 13:32:24 -08001384// TODO: currently many GPU tests are declared outside SK_SUPPORT_GPU guards.
1385// Thus we export the empty RunWithGPUTestContexts when SK_SUPPORT_GPU=0.
1386namespace skiatest {
bsalomon758586c2016-04-06 14:02:39 -07001387
1388#if SK_SUPPORT_GPU
1389bool IsGLContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1390 return kOpenGL_GrBackend == GrContextFactory::ContextTypeBackend(type);
1391}
bsalomondc0fcd42016-04-11 14:21:33 -07001392bool IsVulkanContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1393 return kVulkan_GrBackend == GrContextFactory::ContextTypeBackend(type);
1394}
bsalomon758586c2016-04-06 14:02:39 -07001395bool IsRenderingGLContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1396 return IsGLContextType(type) && GrContextFactory::IsRenderingContext(type);
1397}
1398bool IsNullGLContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1399 return type == GrContextFactory::kNullGL_ContextType;
1400}
1401#else
1402bool IsGLContextType(int) { return false; }
bsalomondc0fcd42016-04-11 14:21:33 -07001403bool IsVulkanContextType(int) { return false; }
bsalomon758586c2016-04-06 14:02:39 -07001404bool IsRenderingGLContextType(int) { return false; }
1405bool IsNullGLContextType(int) { return false; }
1406#endif
1407
1408void RunWithGPUTestContexts(GrContextTestFn* test, GrContextTypeFilterFn* contextTypeFilter,
bsalomonf2f1c172016-04-05 12:59:06 -07001409 Reporter* reporter, GrContextFactory* factory) {
kkinnunen179a8f52015-11-20 13:32:24 -08001410#if SK_SUPPORT_GPU
kkinnunen3e980c32015-12-23 01:33:00 -08001411
bsalomon758586c2016-04-06 14:02:39 -07001412 for (int typeInt = 0; typeInt < GrContextFactory::kContextTypeCnt; ++typeInt) {
1413 GrContextFactory::ContextType contextType = (GrContextFactory::ContextType) typeInt;
1414 ContextInfo ctxInfo = factory->getContextInfo(contextType);
bsalomonfda88072016-04-11 14:40:50 -07001415 if (contextTypeFilter && !(*contextTypeFilter)(contextType)) {
kkinnunen179a8f52015-11-20 13:32:24 -08001416 continue;
1417 }
bsalomon758586c2016-04-06 14:02:39 -07001418 // Use "native" instead of explicitly trying OpenGL and OpenGL ES. Do not use GLES on,
1419 // desktop since tests do not account for not fixing http://skbug.com/2809
1420 if (contextType == GrContextFactory::kGL_ContextType ||
1421 contextType == GrContextFactory::kGLES_ContextType) {
1422 if (contextType != GrContextFactory::kNativeGL_ContextType) {
1423 continue;
1424 }
1425 }
bsalomon8b7451a2016-05-11 06:33:06 -07001426 if (ctxInfo.grContext()) {
bsalomonf2f1c172016-04-05 12:59:06 -07001427 (*test)(reporter, ctxInfo);
kkinnunen5219fd92015-12-10 06:28:13 -08001428 }
bsalomonf2f1c172016-04-05 12:59:06 -07001429 ctxInfo = factory->getContextInfo(contextType,
bsalomon85b4b532016-04-05 11:06:27 -07001430 GrContextFactory::kEnableNVPR_ContextOptions);
bsalomon8b7451a2016-05-11 06:33:06 -07001431 if (ctxInfo.grContext()) {
bsalomonf2f1c172016-04-05 12:59:06 -07001432 (*test)(reporter, ctxInfo);
kkinnunen179a8f52015-11-20 13:32:24 -08001433 }
1434 }
1435#endif
1436}
kkinnunen179a8f52015-11-20 13:32:24 -08001437} // namespace skiatest
1438
borenet48087572015-04-02 12:16:36 -07001439#if !defined(SK_BUILD_FOR_IOS)
jcgregorio3b27ade2014-11-13 08:06:40 -08001440int main(int argc, char** argv) {
1441 SkCommandLineFlags::Parse(argc, argv);
1442 return dm_main();
1443}
1444#endif