blob: e371bbca7f7c16aff7c0a7fa5ba520e12b535b5c [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"
scroggocc2feb12015-08-14 08:32:46 -070014#include "SkCodec.h"
mtklein27c3fdd2016-02-26 14:43:21 -080015#include "SkColorPriv.h"
msarett888dc162016-05-23 10:21:17 -070016#include "SkColorSpace.h"
caryclark17f0b6d2014-07-22 10:15:34 -070017#include "SkCommonFlags.h"
kkinnunen3e980c32015-12-23 01:33:00 -080018#include "SkCommonFlagsConfig.h"
brianosman3c579dc2016-04-19 09:18:11 -070019#include "SkData.h"
caryclark83ca6282015-06-10 09:31:09 -070020#include "SkFontMgr.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +000021#include "SkGraphics.h"
mtklein27c3fdd2016-02-26 14:43:21 -080022#include "SkHalf.h"
halcanary4dbbd042016-06-07 17:21:10 -070023#include "SkLeanWindows.h"
mtklein748ca3b2015-01-15 10:56:12 -080024#include "SkMD5.h"
mtklein1b249332015-07-07 12:21:21 -070025#include "SkMutex.h"
mtklein1d0f1642014-09-08 08:05:18 -070026#include "SkOSFile.h"
Ben Wagnerbf111d72016-11-07 18:05:29 -050027#include "SkOSPath.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"
Mike Kleinadacaef2017-02-06 09:26:14 -050035#include "ios_utils.h"
brianosman3c579dc2016-04-19 09:18:11 -070036#include "picture_utils.h"
caryclark83ca6282015-06-10 09:31:09 -070037#include "sk_tool_utils.h"
liyuqian38911a72016-10-04 11:23:22 -070038#include "SkScan.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +000039
scroggo19b91532016-10-24 09:03:26 -070040#include <vector>
41
halcanary7a14b312015-10-01 07:28:13 -070042#ifdef SK_PDF_IMAGE_STATS
43extern void SkPDFImageDumpStats();
44#endif
45
mtkleina5114d72015-08-24 13:27:01 -070046#include "png.h"
47
bungeman60e0fee2015-08-26 05:15:46 -070048#include <stdlib.h>
49
scroggo27a58342015-10-28 08:56:41 -070050#ifndef SK_BUILD_FOR_WIN32
51 #include <unistd.h>
52#endif
53
djsollen54416de2015-04-03 07:24:48 -070054DEFINE_string(src, "tests gm skp image", "Source types to test.");
mtklein748ca3b2015-01-15 10:56:12 -080055DEFINE_bool(nameByHash, false,
56 "If true, write to FLAGS_writePath[0]/<hash>.png instead of "
djsollen54416de2015-04-03 07:24:48 -070057 "to FLAGS_writePath[0]/<config>/<sourceType>/<sourceOptions>/<name>.png");
mtklein748ca3b2015-01-15 10:56:12 -080058DEFINE_bool2(pathOpsExtended, x, false, "Run extended pathOps tests.");
mtkleind603b222015-02-17 11:13:33 -080059DEFINE_string(matrix, "1 0 0 1",
60 "2x2 scale+skew matrix to apply or upright when using "
61 "'matrix' or 'upright' in config.");
mtklein82d28432015-01-15 12:46:02 -080062DEFINE_bool(gpu_threading, false, "Allow GPU work to run on multiple threads?");
mtklein@google.comd36522d2013-10-16 13:02:15 +000063
mtkleina2ef6422015-01-15 13:44:22 -080064DEFINE_string(blacklist, "",
djsollen54416de2015-04-03 07:24:48 -070065 "Space-separated config/src/srcOptions/name quadruples to blacklist. '_' matches anything. E.g. \n"
66 "'--blacklist gpu skp _ _' will blacklist all SKPs drawn into the gpu config.\n"
67 "'--blacklist gpu skp _ _ 8888 gm _ aarects' will also blacklist the aarects GM on 8888.");
mtkleina2ef6422015-01-15 13:44:22 -080068
mtklein62bd1a62015-01-27 14:46:26 -080069DEFINE_string2(readPath, r, "", "If set check for equality with golden results in this directory.");
70
borenet09ed4802015-04-03 14:15:33 -070071DEFINE_string(uninterestingHashesFile, "",
72 "File containing a list of uninteresting hashes. If a result hashes to something in "
73 "this list, no image is written for that result.");
74
mtklein6393c062015-04-27 08:45:01 -070075DEFINE_int32(shards, 1, "We're splitting source data into this many shards.");
76DEFINE_int32(shard, 0, "Which shard do I run?");
77
halcanary45420a92016-06-02 12:41:14 -070078DEFINE_string(mskps, "", "Directory to read mskps from, or a single mskp file.");
79
mtklein748ca3b2015-01-15 10:56:12 -080080using namespace DM;
bsalomon3724e572016-03-30 18:56:19 -070081using sk_gpu_test::GrContextFactory;
bsalomon273c0f52016-03-31 10:59:06 -070082using sk_gpu_test::GLTestContext;
bsalomonf2f1c172016-04-05 12:59:06 -070083using sk_gpu_test::ContextInfo;
mtklein@google.comd36522d2013-10-16 13:02:15 +000084
mtklein748ca3b2015-01-15 10:56:12 -080085/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
86
benjaminwagnerec4d4d72016-03-25 12:59:53 -070087static const double kStartMs = SkTime::GetMSecs();
mtkleinc41fd922016-03-08 09:01:39 -080088
89static FILE* gVLog;
90
91template <typename... Args>
92static void vlog(const char* fmt, Args&&... args) {
93 if (gVLog) {
94 fprintf(gVLog, "%s\t", HumanizeMs(SkTime::GetMSecs() - kStartMs).c_str());
95 fprintf(gVLog, fmt, args...);
96 fflush(gVLog);
97 }
98}
99
100template <typename... Args>
101static void info(const char* fmt, Args&&... args) {
102 vlog(fmt, args...);
103 if (!FLAGS_quiet) {
104 printf(fmt, args...);
105 }
106}
107static void info(const char* fmt) {
108 if (!FLAGS_quiet) {
109 printf("%s", fmt); // Clang warns printf(fmt) is insecure.
110 }
111}
112
reed086eea92016-05-04 17:12:46 -0700113SK_DECLARE_STATIC_MUTEX(gFailuresMutex);
mtklein748ca3b2015-01-15 10:56:12 -0800114static SkTArray<SkString> gFailures;
115
mtklein3eed7dd2016-02-24 19:07:07 -0800116static void fail(const SkString& err) {
mtklein748ca3b2015-01-15 10:56:12 -0800117 SkAutoMutexAcquire lock(gFailuresMutex);
118 SkDebugf("\n\nFAILURE: %s\n\n", err.c_str());
119 gFailures.push_back(err);
halcanary9e398f72015-01-10 11:18:04 -0800120}
121
mtklein0f7748a2016-07-25 15:27:29 -0700122struct Running {
123 SkString id;
124 SkThreadID thread;
125
126 void dump() const {
127 info("\t%s\n", id.c_str());
128 }
129};
mtkleinb37cb412015-03-18 05:27:14 -0700130
mtklein246ba3a2016-02-23 10:39:36 -0800131// We use a spinlock to make locking this in a signal handler _somewhat_ safe.
mtklein15923c92016-02-29 10:14:38 -0800132static SkSpinlock gMutex;
mtklein0f7748a2016-07-25 15:27:29 -0700133static int32_t gPending;
134static SkTArray<Running> gRunning;
mtklein748ca3b2015-01-15 10:56:12 -0800135
mtklein3eed7dd2016-02-24 19:07:07 -0800136static void done(const char* config, const char* src, const char* srcOptions, const char* name) {
137 SkString id = SkStringPrintf("%s %s %s %s", config, src, srcOptions, name);
mtkleinc41fd922016-03-08 09:01:39 -0800138 vlog("done %s\n", id.c_str());
mtkleinafae30a2016-02-24 12:28:32 -0800139 int pending;
mtkleinb37cb412015-03-18 05:27:14 -0700140 {
mtkleinf10637f2016-06-10 13:56:35 -0700141 SkAutoMutexAcquire lock(gMutex);
mtkleinb37cb412015-03-18 05:27:14 -0700142 for (int i = 0; i < gRunning.count(); i++) {
mtklein0f7748a2016-07-25 15:27:29 -0700143 if (gRunning[i].id == id) {
mtkleinb37cb412015-03-18 05:27:14 -0700144 gRunning.removeShuffle(i);
145 break;
146 }
147 }
mtkleinafae30a2016-02-24 12:28:32 -0800148 pending = --gPending;
reed50bc0512015-05-19 14:13:31 -0700149 }
mtkleina17241b2015-01-23 05:48:00 -0800150 // We write our dm.json file every once in a while in case we crash.
151 // Notice this also handles the final dm.json when pending == 0.
152 if (pending % 500 == 0) {
153 JsonWriter::DumpJson();
154 }
mtklein@google.comd36522d2013-10-16 13:02:15 +0000155}
156
mtklein3eed7dd2016-02-24 19:07:07 -0800157static void start(const char* config, const char* src, const char* srcOptions, const char* name) {
158 SkString id = SkStringPrintf("%s %s %s %s", config, src, srcOptions, name);
mtkleinc41fd922016-03-08 09:01:39 -0800159 vlog("start %s\n", id.c_str());
mtkleinf10637f2016-06-10 13:56:35 -0700160 SkAutoMutexAcquire lock(gMutex);
mtklein0f7748a2016-07-25 15:27:29 -0700161 gRunning.push_back({id,SkGetThreadID()});
mtkleinb37cb412015-03-18 05:27:14 -0700162}
163
mtkleinafae30a2016-02-24 12:28:32 -0800164static void print_status() {
mtkleinafae30a2016-02-24 12:28:32 -0800165 int curr = sk_tools::getCurrResidentSetSizeMB(),
166 peak = sk_tools::getMaxResidentSetSizeMB();
mtkleinc41fd922016-03-08 09:01:39 -0800167 SkString elapsed = HumanizeMs(SkTime::GetMSecs() - kStartMs);
mtkleinafae30a2016-02-24 12:28:32 -0800168
mtkleinf10637f2016-06-10 13:56:35 -0700169 SkAutoMutexAcquire lock(gMutex);
mtkleinc41fd922016-03-08 09:01:39 -0800170 info("\n%s elapsed, %d active, %d queued, %dMB RAM, %dMB peak\n",
171 elapsed.c_str(), gRunning.count(), gPending - gRunning.count(), curr, peak);
mtkleinafae30a2016-02-24 12:28:32 -0800172 for (auto& task : gRunning) {
mtklein0f7748a2016-07-25 15:27:29 -0700173 task.dump();
mtkleinafae30a2016-02-24 12:28:32 -0800174 }
175}
176
Mike Klein3cc2d202016-10-24 11:39:43 -0400177static void find_culprit() {
178 // Assumes gMutex is locked.
179 SkThreadID thisThread = SkGetThreadID();
180 for (auto& task : gRunning) {
181 if (task.thread == thisThread) {
182 info("Likely culprit:\n");
183 task.dump();
mtklein0f7748a2016-07-25 15:27:29 -0700184 }
185 }
Mike Klein3cc2d202016-10-24 11:39:43 -0400186}
mtklein0f7748a2016-07-25 15:27:29 -0700187
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 }
222
Mike Klein3cc2d202016-10-24 11:39:43 -0400223 static void setup_crash_handler() {
224 SetUnhandledExceptionFilter(crash_handler);
225 }
226#else
mtklein246ba3a2016-02-23 10:39:36 -0800227 #include <signal.h>
Mike Klein3cc2d202016-10-24 11:39:43 -0400228 #if !defined(SK_BUILD_FOR_ANDROID)
229 #include <execinfo.h>
230 #endif
231
232 static constexpr int max_of() { return 0; }
233 template <typename... Rest>
234 static constexpr int max_of(int x, Rest... rest) {
235 return x > max_of(rest...) ? x : max_of(rest...);
236 }
237
238 static void (*previous_handler[max_of(SIGABRT,SIGBUS,SIGFPE,SIGILL,SIGSEGV)+1])(int);
mtkleinf8557292016-02-29 06:35:28 -0800239
mtklein5b64dab2016-06-09 08:59:48 -0700240 static void crash_handler(int sig) {
mtkleinf10637f2016-06-10 13:56:35 -0700241 SkAutoMutexAcquire lock(gMutex);
mtklein5b64dab2016-06-09 08:59:48 -0700242
243 info("\nCaught signal %d [%s], was running:\n", sig, strsignal(sig));
244 for (auto& task : gRunning) {
mtklein0f7748a2016-07-25 15:27:29 -0700245 task.dump();
mtklein5b64dab2016-06-09 08:59:48 -0700246 }
mtklein0f7748a2016-07-25 15:27:29 -0700247 find_culprit();
mtklein5b64dab2016-06-09 08:59:48 -0700248
Mike Klein3cc2d202016-10-24 11:39:43 -0400249 #if !defined(SK_BUILD_FOR_ANDROID)
mtklein5b64dab2016-06-09 08:59:48 -0700250 void* stack[64];
251 int count = backtrace(stack, SK_ARRAY_COUNT(stack));
252 char** symbols = backtrace_symbols(stack, count);
253 info("\nStack trace:\n");
254 for (int i = 0; i < count; i++) {
255 info(" %s\n", symbols[i]);
256 }
Mike Klein3cc2d202016-10-24 11:39:43 -0400257 #endif
mtklein5b64dab2016-06-09 08:59:48 -0700258 fflush(stdout);
259
Mike Klein3cc2d202016-10-24 11:39:43 -0400260 signal(sig, previous_handler[sig]);
261 raise(sig);
mtklein5b64dab2016-06-09 08:59:48 -0700262 }
263
mtklein246ba3a2016-02-23 10:39:36 -0800264 static void setup_crash_handler() {
265 const int kSignals[] = { SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGSEGV };
266 for (int sig : kSignals) {
Mike Klein3cc2d202016-10-24 11:39:43 -0400267 previous_handler[sig] = signal(sig, crash_handler);
mtklein246ba3a2016-02-23 10:39:36 -0800268 }
269 }
270#endif
271
mtklein748ca3b2015-01-15 10:56:12 -0800272/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
mtklein114c3cd2015-01-15 10:15:02 -0800273
mtklein62bd1a62015-01-27 14:46:26 -0800274struct Gold : public SkString {
mtkleina82f5622015-02-20 12:30:19 -0800275 Gold() : SkString("") {}
mtklein3eed7dd2016-02-24 19:07:07 -0800276 Gold(const SkString& sink, const SkString& src,
277 const SkString& srcOptions, const SkString& name,
278 const SkString& md5)
mtklein62bd1a62015-01-27 14:46:26 -0800279 : SkString("") {
280 this->append(sink);
281 this->append(src);
djsollen54416de2015-04-03 07:24:48 -0700282 this->append(srcOptions);
mtklein62bd1a62015-01-27 14:46:26 -0800283 this->append(name);
284 this->append(md5);
mtklein62bd1a62015-01-27 14:46:26 -0800285 }
mtkleinc8d1dd42015-10-15 12:23:01 -0700286 struct Hash {
287 uint32_t operator()(const Gold& g) const {
288 return SkGoodHash()((const SkString&)g);
289 }
290 };
mtklein62bd1a62015-01-27 14:46:26 -0800291};
mtkleina82f5622015-02-20 12:30:19 -0800292static SkTHashSet<Gold, Gold::Hash> gGold;
mtklein62bd1a62015-01-27 14:46:26 -0800293
294static void add_gold(JsonWriter::BitmapResult r) {
djsollen54416de2015-04-03 07:24:48 -0700295 gGold.add(Gold(r.config, r.sourceType, r.sourceOptions, r.name, r.md5));
mtklein62bd1a62015-01-27 14:46:26 -0800296}
297
298static void gather_gold() {
299 if (!FLAGS_readPath.isEmpty()) {
300 SkString path(FLAGS_readPath[0]);
301 path.append("/dm.json");
302 if (!JsonWriter::ReadJson(path.c_str(), add_gold)) {
303 fail(SkStringPrintf("Couldn't read %s for golden results.", path.c_str()));
304 }
305 }
306}
307
308/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
309
mtklein935f1b12016-03-16 08:37:19 -0700310#if defined(SK_BUILD_FOR_WIN32)
311 static const char* kNewline = "\r\n";
312#else
313 static const char* kNewline = "\n";
314#endif
315
borenet09ed4802015-04-03 14:15:33 -0700316static SkTHashSet<SkString> gUninterestingHashes;
317
318static void gather_uninteresting_hashes() {
319 if (!FLAGS_uninterestingHashesFile.isEmpty()) {
bungeman38d909e2016-08-02 14:40:46 -0700320 sk_sp<SkData> data(SkData::MakeFromFileName(FLAGS_uninterestingHashesFile[0]));
mtkleincc334b32015-09-22 11:43:53 -0700321 if (!data) {
mtkleinc41fd922016-03-08 09:01:39 -0800322 info("WARNING: unable to read uninteresting hashes from %s\n",
323 FLAGS_uninterestingHashesFile[0]);
mtkleincc334b32015-09-22 11:43:53 -0700324 return;
325 }
borenet09ed4802015-04-03 14:15:33 -0700326 SkTArray<SkString> hashes;
mtklein935f1b12016-03-16 08:37:19 -0700327 SkStrSplit((const char*)data->data(), kNewline, &hashes);
borenet09ed4802015-04-03 14:15:33 -0700328 for (const SkString& hash : hashes) {
329 gUninterestingHashes.add(hash);
330 }
mtkleinc41fd922016-03-08 09:01:39 -0800331 info("FYI: loaded %d distinct uninteresting hashes from %d lines\n",
332 gUninterestingHashes.count(), hashes.count());
borenet09ed4802015-04-03 14:15:33 -0700333 }
334}
335
336/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
337
Ben Wagner145dbcd2016-11-03 14:40:50 -0400338struct TaggedSrc : public std::unique_ptr<Src> {
mtklein3eed7dd2016-02-24 19:07:07 -0800339 SkString tag;
340 SkString options;
mtkleine0effd62015-07-29 06:37:28 -0700341};
342
Ben Wagner145dbcd2016-11-03 14:40:50 -0400343struct TaggedSink : public std::unique_ptr<Sink> {
kkinnunen3e980c32015-12-23 01:33:00 -0800344 SkString tag;
djsollen54416de2015-04-03 07:24:48 -0700345};
mtklein748ca3b2015-01-15 10:56:12 -0800346
347static const bool kMemcpyOK = true;
348
mtkleine0effd62015-07-29 06:37:28 -0700349static SkTArray<TaggedSrc, kMemcpyOK> gSrcs;
350static SkTArray<TaggedSink, kMemcpyOK> gSinks;
mtklein748ca3b2015-01-15 10:56:12 -0800351
mtklein6393c062015-04-27 08:45:01 -0700352static bool in_shard() {
353 static int N = 0;
354 return N++ % FLAGS_shards == FLAGS_shard;
355}
356
mtklein3eed7dd2016-02-24 19:07:07 -0800357static void push_src(const char* tag, ImplicitString options, Src* s) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400358 std::unique_ptr<Src> src(s);
mtklein6393c062015-04-27 08:45:01 -0700359 if (in_shard() &&
mtklein3eed7dd2016-02-24 19:07:07 -0800360 FLAGS_src.contains(tag) &&
mtklein748ca3b2015-01-15 10:56:12 -0800361 !SkCommandLineFlags::ShouldSkip(FLAGS_match, src->name().c_str())) {
mtkleine0effd62015-07-29 06:37:28 -0700362 TaggedSrc& s = gSrcs.push_back();
mtklein18300a32016-03-16 13:53:35 -0700363 s.reset(src.release());
mtklein748ca3b2015-01-15 10:56:12 -0800364 s.tag = tag;
djsollen54416de2015-04-03 07:24:48 -0700365 s.options = options;
mtklein114c3cd2015-01-15 10:15:02 -0800366 }
mtklein748ca3b2015-01-15 10:56:12 -0800367}
mtklein114c3cd2015-01-15 10:15:02 -0800368
msarett9e707a02015-09-01 14:57:57 -0700369static void push_codec_src(Path path, CodecSrc::Mode mode, CodecSrc::DstColorType dstColorType,
scroggoc5560be2016-02-03 09:42:42 -0800370 SkAlphaType dstAlphaType, float scale) {
scroggoe4499842016-02-25 11:03:47 -0800371 if (FLAGS_simpleCodec) {
scroggo19b91532016-10-24 09:03:26 -0700372 const bool simple = CodecSrc::kCodec_Mode == mode || CodecSrc::kAnimated_Mode == mode;
373 if (!simple || dstColorType != CodecSrc::kGetFromCanvas_DstColorType || scale != 1.0f) {
scroggoe4499842016-02-25 11:03:47 -0800374 // Only decode in the simple case.
375 return;
scroggo19b91532016-10-24 09:03:26 -0700376 }
scroggoe4499842016-02-25 11:03:47 -0800377 }
msarett9e707a02015-09-01 14:57:57 -0700378 SkString folder;
379 switch (mode) {
380 case CodecSrc::kCodec_Mode:
381 folder.append("codec");
382 break;
msarettbb25b532016-01-13 09:31:39 -0800383 case CodecSrc::kCodecZeroInit_Mode:
384 folder.append("codec_zero_init");
385 break;
msarett9e707a02015-09-01 14:57:57 -0700386 case CodecSrc::kScanline_Mode:
387 folder.append("scanline");
388 break;
msarett9e707a02015-09-01 14:57:57 -0700389 case CodecSrc::kStripe_Mode:
390 folder.append("stripe");
391 break;
msarett91c22b22016-02-22 12:27:46 -0800392 case CodecSrc::kCroppedScanline_Mode:
393 folder.append("crop");
394 break;
msarett9e707a02015-09-01 14:57:57 -0700395 case CodecSrc::kSubset_Mode:
msarett36c37962015-09-02 13:20:52 -0700396 folder.append("codec_subset");
msarett9e707a02015-09-01 14:57:57 -0700397 break;
scroggo19b91532016-10-24 09:03:26 -0700398 case CodecSrc::kAnimated_Mode:
399 folder.append("codec_animated");
400 break;
msarett9e707a02015-09-01 14:57:57 -0700401 }
402
403 switch (dstColorType) {
404 case CodecSrc::kGrayscale_Always_DstColorType:
405 folder.append("_kGray8");
406 break;
407 case CodecSrc::kIndex8_Always_DstColorType:
408 folder.append("_kIndex8");
409 break;
msarett34e0ec42016-04-22 16:27:24 -0700410 case CodecSrc::kNonNative8888_Always_DstColorType:
411 folder.append("_kNonNative");
412 break;
msarett9e707a02015-09-01 14:57:57 -0700413 default:
414 break;
415 }
416
scroggoc5560be2016-02-03 09:42:42 -0800417 switch (dstAlphaType) {
scroggoc5560be2016-02-03 09:42:42 -0800418 case kPremul_SkAlphaType:
419 folder.append("_premul");
420 break;
421 case kUnpremul_SkAlphaType:
422 folder.append("_unpremul");
423 break;
424 default:
425 break;
426 }
427
msarett9e707a02015-09-01 14:57:57 -0700428 if (1.0f != scale) {
429 folder.appendf("_%.3f", scale);
430 }
431
scroggoc5560be2016-02-03 09:42:42 -0800432 CodecSrc* src = new CodecSrc(path, mode, dstColorType, dstAlphaType, scale);
msarett9e707a02015-09-01 14:57:57 -0700433 push_src("image", folder, src);
434}
435
scroggof8dc9df2016-05-16 09:04:13 -0700436static void push_android_codec_src(Path path, CodecSrc::DstColorType dstColorType,
437 SkAlphaType dstAlphaType, int sampleSize) {
msarett3d9d7a72015-10-21 10:27:10 -0700438 SkString folder;
scroggof8dc9df2016-05-16 09:04:13 -0700439 folder.append("scaled_codec");
msarett3d9d7a72015-10-21 10:27:10 -0700440
441 switch (dstColorType) {
442 case CodecSrc::kGrayscale_Always_DstColorType:
443 folder.append("_kGray8");
444 break;
445 case CodecSrc::kIndex8_Always_DstColorType:
446 folder.append("_kIndex8");
447 break;
msarett34e0ec42016-04-22 16:27:24 -0700448 case CodecSrc::kNonNative8888_Always_DstColorType:
449 folder.append("_kNonNative");
450 break;
msarett3d9d7a72015-10-21 10:27:10 -0700451 default:
452 break;
453 }
454
scroggoc5560be2016-02-03 09:42:42 -0800455 switch (dstAlphaType) {
scroggoc5560be2016-02-03 09:42:42 -0800456 case kPremul_SkAlphaType:
457 folder.append("_premul");
458 break;
msarett9e9444c2016-02-03 12:39:10 -0800459 case kUnpremul_SkAlphaType:
460 folder.append("_unpremul");
461 break;
scroggoc5560be2016-02-03 09:42:42 -0800462 default:
463 break;
464 }
465
msarett3d9d7a72015-10-21 10:27:10 -0700466 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800467 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700468 }
469
scroggof8dc9df2016-05-16 09:04:13 -0700470 AndroidCodecSrc* src = new AndroidCodecSrc(path, dstColorType, dstAlphaType, sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700471 push_src("image", folder, src);
472}
473
msarett18976312016-03-09 14:20:58 -0800474static void push_image_gen_src(Path path, ImageGenSrc::Mode mode, SkAlphaType alphaType, bool isGpu)
475{
476 SkString folder;
477 switch (mode) {
478 case ImageGenSrc::kCodec_Mode:
479 folder.append("gen_codec");
480 break;
481 case ImageGenSrc::kPlatform_Mode:
482 folder.append("gen_platform");
483 break;
484 }
485
486 if (isGpu) {
487 folder.append("_gpu");
488 } else {
489 switch (alphaType) {
490 case kOpaque_SkAlphaType:
491 folder.append("_opaque");
492 break;
493 case kPremul_SkAlphaType:
494 folder.append("_premul");
495 break;
496 case kUnpremul_SkAlphaType:
497 folder.append("_unpremul");
498 break;
499 default:
500 break;
501 }
502 }
503
504 ImageGenSrc* src = new ImageGenSrc(path, mode, alphaType, isGpu);
505 push_src("image", folder, src);
506}
507
msarett438b2ad2015-04-09 12:43:10 -0700508static void push_codec_srcs(Path path) {
bungeman38d909e2016-08-02 14:40:46 -0700509 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
msarett438b2ad2015-04-09 12:43:10 -0700510 if (!encoded) {
mtkleinc41fd922016-03-08 09:01:39 -0800511 info("Couldn't read %s.", path.c_str());
msarett438b2ad2015-04-09 12:43:10 -0700512 return;
513 }
Ben Wagner145dbcd2016-11-03 14:40:50 -0400514 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(encoded));
halcanary96fcdcc2015-08-27 07:41:13 -0700515 if (nullptr == codec.get()) {
mtkleinc41fd922016-03-08 09:01:39 -0800516 info("Couldn't create codec for %s.", path.c_str());
msarett438b2ad2015-04-09 12:43:10 -0700517 return;
518 }
519
scroggo9366aff2016-05-20 08:38:54 -0700520 // native scaling is only supported by WEBP and JPEG
521 bool supportsNativeScaling = false;
msarett438b2ad2015-04-09 12:43:10 -0700522
msarett91c22b22016-02-22 12:27:46 -0800523 SkTArray<CodecSrc::Mode> nativeModes;
524 nativeModes.push_back(CodecSrc::kCodec_Mode);
525 nativeModes.push_back(CodecSrc::kCodecZeroInit_Mode);
msarett91c22b22016-02-22 12:27:46 -0800526 switch (codec->getEncodedFormat()) {
Hal Canarydb683012016-11-23 08:55:18 -0700527 case SkEncodedImageFormat::kJPEG:
msarett91c22b22016-02-22 12:27:46 -0800528 nativeModes.push_back(CodecSrc::kScanline_Mode);
529 nativeModes.push_back(CodecSrc::kStripe_Mode);
530 nativeModes.push_back(CodecSrc::kCroppedScanline_Mode);
scroggo9366aff2016-05-20 08:38:54 -0700531 supportsNativeScaling = true;
msarett91c22b22016-02-22 12:27:46 -0800532 break;
Hal Canarydb683012016-11-23 08:55:18 -0700533 case SkEncodedImageFormat::kWEBP:
msarett91c22b22016-02-22 12:27:46 -0800534 nativeModes.push_back(CodecSrc::kSubset_Mode);
scroggo9366aff2016-05-20 08:38:54 -0700535 supportsNativeScaling = true;
msarett91c22b22016-02-22 12:27:46 -0800536 break;
Hal Canarydb683012016-11-23 08:55:18 -0700537 case SkEncodedImageFormat::kDNG:
msarettb65e6042016-02-23 05:37:25 -0800538 break;
msarett91c22b22016-02-22 12:27:46 -0800539 default:
540 nativeModes.push_back(CodecSrc::kScanline_Mode);
msarett91c22b22016-02-22 12:27:46 -0800541 break;
542 }
msarett9e707a02015-09-01 14:57:57 -0700543
msarett91c22b22016-02-22 12:27:46 -0800544 SkTArray<CodecSrc::DstColorType> colorTypes;
545 colorTypes.push_back(CodecSrc::kGetFromCanvas_DstColorType);
msarett34e0ec42016-04-22 16:27:24 -0700546 colorTypes.push_back(CodecSrc::kNonNative8888_Always_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700547 switch (codec->getInfo().colorType()) {
548 case kGray_8_SkColorType:
msarett91c22b22016-02-22 12:27:46 -0800549 colorTypes.push_back(CodecSrc::kGrayscale_Always_DstColorType);
Hal Canarydb683012016-11-23 08:55:18 -0700550 if (SkEncodedImageFormat::kWBMP == codec->getEncodedFormat()) {
msarett91c22b22016-02-22 12:27:46 -0800551 colorTypes.push_back(CodecSrc::kIndex8_Always_DstColorType);
msarett55f7bdd2016-02-16 13:24:54 -0800552 }
msarett36c37962015-09-02 13:20:52 -0700553 break;
554 case kIndex_8_SkColorType:
msarett91c22b22016-02-22 12:27:46 -0800555 colorTypes.push_back(CodecSrc::kIndex8_Always_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700556 break;
557 default:
msarett36c37962015-09-02 13:20:52 -0700558 break;
559 }
msarett9e707a02015-09-01 14:57:57 -0700560
scroggoc5560be2016-02-03 09:42:42 -0800561 SkTArray<SkAlphaType> alphaModes;
562 alphaModes.push_back(kPremul_SkAlphaType);
scroggof2c96a22016-05-20 11:08:27 -0700563 if (codec->getInfo().alphaType() != kOpaque_SkAlphaType) {
564 alphaModes.push_back(kUnpremul_SkAlphaType);
scroggoc5560be2016-02-03 09:42:42 -0800565 }
msarettb714fb02016-01-22 14:46:42 -0800566
567 for (CodecSrc::Mode mode : nativeModes) {
scroggo9366aff2016-05-20 08:38:54 -0700568 for (CodecSrc::DstColorType colorType : colorTypes) {
569 for (SkAlphaType alphaType : alphaModes) {
scroggof2c96a22016-05-20 11:08:27 -0700570 // Only test kCroppedScanline_Mode when the alpha type is premul. The test is
scroggo9366aff2016-05-20 08:38:54 -0700571 // slow and won't be interestingly different with different alpha types.
572 if (CodecSrc::kCroppedScanline_Mode == mode &&
scroggof2c96a22016-05-20 11:08:27 -0700573 kPremul_SkAlphaType != alphaType) {
scroggo9366aff2016-05-20 08:38:54 -0700574 continue;
575 }
msarett91c22b22016-02-22 12:27:46 -0800576
scroggo9366aff2016-05-20 08:38:54 -0700577 push_codec_src(path, mode, colorType, alphaType, 1.0f);
msarettf3dc1882016-04-26 13:06:38 -0700578
scroggo9366aff2016-05-20 08:38:54 -0700579 // Skip kNonNative on different native scales. It won't be interestingly
580 // different.
581 if (supportsNativeScaling &&
582 CodecSrc::kNonNative8888_Always_DstColorType == colorType) {
583 // Native Scales
584 // SkJpegCodec natively supports scaling to the following:
585 for (auto scale : { 0.125f, 0.25f, 0.375f, 0.5f, 0.625f, 0.750f, 0.875f }) {
586 push_codec_src(path, mode, colorType, alphaType, scale);
587 }
scroggoc5560be2016-02-03 09:42:42 -0800588 }
msarett9e707a02015-09-01 14:57:57 -0700589 }
590 }
msarett0a242972015-06-11 14:27:27 -0700591 }
msarett36c37962015-09-02 13:20:52 -0700592
scroggo19b91532016-10-24 09:03:26 -0700593 {
594 std::vector<SkCodec::FrameInfo> frameInfos = codec->getFrameInfo();
595 if (frameInfos.size() > 1) {
596 push_codec_src(path, CodecSrc::kAnimated_Mode, CodecSrc::kGetFromCanvas_DstColorType,
597 kPremul_SkAlphaType, 1.0f);
598 }
599
600 }
601
scroggoe4499842016-02-25 11:03:47 -0800602 if (FLAGS_simpleCodec) {
603 return;
604 }
605
msarett3d9d7a72015-10-21 10:27:10 -0700606 const int sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
msarett36c37962015-09-02 13:20:52 -0700607
msarett3d9d7a72015-10-21 10:27:10 -0700608 for (int sampleSize : sampleSizes) {
msarett91c22b22016-02-22 12:27:46 -0800609 for (CodecSrc::DstColorType colorType : colorTypes) {
scroggoc5560be2016-02-03 09:42:42 -0800610 for (SkAlphaType alphaType : alphaModes) {
msarettf3dc1882016-04-26 13:06:38 -0700611 // We can exercise all of the kNonNative support code in the swizzler with just a
612 // few sample sizes. Skip the rest.
613 if (CodecSrc::kNonNative8888_Always_DstColorType == colorType && sampleSize > 3) {
614 continue;
615 }
616
scroggof8dc9df2016-05-16 09:04:13 -0700617 push_android_codec_src(path, colorType, alphaType, sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700618 }
msarett36c37962015-09-02 13:20:52 -0700619 }
620 }
msarett18976312016-03-09 14:20:58 -0800621
622 static const char* const rawExts[] = {
623 "arw", "cr2", "dng", "nef", "nrw", "orf", "raf", "rw2", "pef", "srw",
624 "ARW", "CR2", "DNG", "NEF", "NRW", "ORF", "RAF", "RW2", "PEF", "SRW",
625 };
626
627 // There is not currently a reason to test RAW images on image generator.
628 // If we want to enable these tests, we will need to fix skbug.com/5079.
629 for (const char* ext : rawExts) {
630 if (path.endsWith(ext)) {
631 return;
632 }
633 }
634
635 // Push image generator GPU test.
brianosmanc9ced392016-03-24 07:27:43 -0700636 push_image_gen_src(path, ImageGenSrc::kCodec_Mode, codec->getInfo().alphaType(), true);
msarett18976312016-03-09 14:20:58 -0800637
638 // Push image generator CPU tests.
639 for (SkAlphaType alphaType : alphaModes) {
640 push_image_gen_src(path, ImageGenSrc::kCodec_Mode, alphaType, false);
641
642#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
Hal Canarydb683012016-11-23 08:55:18 -0700643 if (SkEncodedImageFormat::kWEBP != codec->getEncodedFormat() &&
644 SkEncodedImageFormat::kWBMP != codec->getEncodedFormat() &&
msarett18976312016-03-09 14:20:58 -0800645 kUnpremul_SkAlphaType != alphaType)
646 {
647 push_image_gen_src(path, ImageGenSrc::kPlatform_Mode, alphaType, false);
648 }
msarettfc0b6d12016-03-17 13:50:17 -0700649#elif defined(SK_BUILD_FOR_WIN)
Hal Canarydb683012016-11-23 08:55:18 -0700650 if (SkEncodedImageFormat::kWEBP != codec->getEncodedFormat() &&
651 SkEncodedImageFormat::kWBMP != codec->getEncodedFormat())
msarettfc0b6d12016-03-17 13:50:17 -0700652 {
653 push_image_gen_src(path, ImageGenSrc::kPlatform_Mode, alphaType, false);
654 }
msarett18976312016-03-09 14:20:58 -0800655#endif
656 }
msarett438b2ad2015-04-09 12:43:10 -0700657}
658
msarettd1227a72016-05-18 06:23:57 -0700659static void push_brd_src(Path path, CodecSrc::DstColorType dstColorType, BRDSrc::Mode mode,
660 uint32_t sampleSize) {
661 SkString folder("brd_android_codec");
msaretta5783ae2015-09-08 15:35:32 -0700662 switch (mode) {
663 case BRDSrc::kFullImage_Mode:
664 break;
665 case BRDSrc::kDivisor_Mode:
666 folder.append("_divisor");
667 break;
668 default:
669 SkASSERT(false);
670 return;
671 }
672
673 switch (dstColorType) {
674 case CodecSrc::kGetFromCanvas_DstColorType:
675 break;
676 case CodecSrc::kIndex8_Always_DstColorType:
677 folder.append("_kIndex");
678 break;
679 case CodecSrc::kGrayscale_Always_DstColorType:
680 folder.append("_kGray");
681 break;
682 default:
683 SkASSERT(false);
684 return;
685 }
686
687 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800688 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700689 }
690
msarettd1227a72016-05-18 06:23:57 -0700691 BRDSrc* src = new BRDSrc(path, mode, dstColorType, sampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700692 push_src("image", folder, src);
693}
694
695static void push_brd_srcs(Path path) {
scroggo24a9bd72016-05-20 06:02:42 -0700696 // Only run Index8 and grayscale to one sampleSize and Mode. Though interesting
697 // to test these color types, they should not reveal anything across various
698 // sampleSizes and Modes
699 for (auto type : { CodecSrc::kIndex8_Always_DstColorType,
700 CodecSrc::kGrayscale_Always_DstColorType }) {
701 // Arbitrarily choose Mode and sampleSize.
702 push_brd_src(path, type, BRDSrc::kFullImage_Mode, 2);
703 }
704
705
scroggo501b7342015-11-03 07:55:11 -0800706 // Test on a variety of sampleSizes, making sure to include:
707 // - 2, 4, and 8, which are natively supported by jpeg
708 // - multiples of 2 which are not divisible by 4 (analogous for 4)
709 // - larger powers of two, since BRD clients generally use powers of 2
710 // We will only produce output for the larger sizes on large images.
711 const uint32_t sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 24, 32, 64 };
msarett6efbe052015-09-11 09:01:16 -0700712
msaretta5783ae2015-09-08 15:35:32 -0700713 const BRDSrc::Mode modes[] = {
msarett26ad17b2015-10-22 07:29:19 -0700714 BRDSrc::kFullImage_Mode,
715 BRDSrc::kDivisor_Mode,
msaretta5783ae2015-09-08 15:35:32 -0700716 };
717
msarettd1227a72016-05-18 06:23:57 -0700718 for (uint32_t sampleSize : sampleSizes) {
scroggo24a9bd72016-05-20 06:02:42 -0700719 for (BRDSrc::Mode mode : modes) {
720 push_brd_src(path, CodecSrc::kGetFromCanvas_DstColorType, mode, sampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700721 }
722 }
723}
724
725static bool brd_supported(const char* ext) {
msarett8c8f22a2015-04-01 06:58:48 -0700726 static const char* const exts[] = {
msaretta5783ae2015-09-08 15:35:32 -0700727 "jpg", "jpeg", "png", "webp",
728 "JPG", "JPEG", "PNG", "WEBP",
msarett8c8f22a2015-04-01 06:58:48 -0700729 };
730
731 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
732 if (0 == strcmp(exts[i], ext)) {
733 return true;
734 }
735 }
736 return false;
scroggo9b77ddd2015-03-19 06:03:39 -0700737}
738
fmalitaa2b9fdf2016-08-03 19:53:36 -0700739template <typename T>
740void gather_file_srcs(const SkCommandLineFlags::StringArray& flags, const char* ext) {
741 for (int i = 0; i < flags.count(); i++) {
742 const char* path = flags[i];
743 if (sk_isdir(path)) {
744 SkOSFile::Iter it(path, ext);
745 for (SkString file; it.next(&file); ) {
746 push_src(ext, "", new T(SkOSPath::Join(path, file.c_str())));
747 }
748 } else {
749 push_src(ext, "", new T(path));
750 }
751 }
752}
753
scroggo86737142016-02-03 12:19:11 -0800754static bool gather_srcs() {
mtklein748ca3b2015-01-15 10:56:12 -0800755 for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) {
djsollen54416de2015-04-03 07:24:48 -0700756 push_src("gm", "", new GMSrc(r->factory()));
mtklein748ca3b2015-01-15 10:56:12 -0800757 }
scroggo86737142016-02-03 12:19:11 -0800758
fmalitaa2b9fdf2016-08-03 19:53:36 -0700759 gather_file_srcs<SKPSrc>(FLAGS_skps, "skp");
760 gather_file_srcs<MSKPSrc>(FLAGS_mskps, "mskp");
761#if defined(SK_XML)
762 gather_file_srcs<SVGSrc>(FLAGS_svgs, "svg");
763#endif
halcanary45420a92016-06-02 12:41:14 -0700764
scroggo86737142016-02-03 12:19:11 -0800765 SkTArray<SkString> images;
msarett69deca82016-04-29 09:38:40 -0700766 if (!CollectImages(FLAGS_images, &images)) {
scroggo86737142016-02-03 12:19:11 -0800767 return false;
768 }
769
770 for (auto image : images) {
771 push_codec_srcs(image);
scroggoe4499842016-02-25 11:03:47 -0800772 if (FLAGS_simpleCodec) {
773 continue;
774 }
775
mtklein21eaf3b2016-02-08 12:39:59 -0800776 const char* ext = strrchr(image.c_str(), '.');
777 if (ext && brd_supported(ext+1)) {
scroggo86737142016-02-03 12:19:11 -0800778 push_brd_srcs(image);
mtklein709d2c32015-01-15 08:30:25 -0800779 }
mtklein709d2c32015-01-15 08:30:25 -0800780 }
scroggo86737142016-02-03 12:19:11 -0800781
msarett69deca82016-04-29 09:38:40 -0700782 SkTArray<SkString> colorImages;
783 if (!CollectImages(FLAGS_colorImages, &colorImages)) {
784 return false;
785 }
786
787 for (auto colorImage : colorImages) {
msarett9ce3a542016-07-15 13:54:38 -0700788 ColorCodecSrc* src = new ColorCodecSrc(colorImage, ColorCodecSrc::kBaseline_Mode,
789 kN32_SkColorType);
msarettb8d1aac2016-08-04 12:22:06 -0700790 push_src("colorImage", "color_codec_baseline", src);
msarett888dc162016-05-23 10:21:17 -0700791
msarett9ce3a542016-07-15 13:54:38 -0700792 src = new ColorCodecSrc(colorImage, ColorCodecSrc::kDst_HPZR30w_Mode, kN32_SkColorType);
msarettb8d1aac2016-08-04 12:22:06 -0700793 push_src("colorImage", "color_codec_HPZR30w", src);
msarett9ce3a542016-07-15 13:54:38 -0700794 // TODO (msarett):
795 // 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 -0700796
msarett9ce3a542016-07-15 13:54:38 -0700797 src = new ColorCodecSrc(colorImage, ColorCodecSrc::kDst_sRGB_Mode, kN32_SkColorType);
msarettb8d1aac2016-08-04 12:22:06 -0700798 push_src("colorImage", "color_codec_sRGB_kN32", src);
msarett9ce3a542016-07-15 13:54:38 -0700799 src = new ColorCodecSrc(colorImage, ColorCodecSrc::kDst_sRGB_Mode, kRGBA_F16_SkColorType);
msarettb8d1aac2016-08-04 12:22:06 -0700800 push_src("colorImage", "color_codec_sRGB_kF16", src);
msarett69deca82016-04-29 09:38:40 -0700801 }
802
scroggo86737142016-02-03 12:19:11 -0800803 return true;
mtklein709d2c32015-01-15 08:30:25 -0800804}
805
kkinnunen3e980c32015-12-23 01:33:00 -0800806static void push_sink(const SkCommandLineConfig& config, Sink* s) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400807 std::unique_ptr<Sink> sink(s);
kkinnunen3e980c32015-12-23 01:33:00 -0800808
mtkleine0effd62015-07-29 06:37:28 -0700809 // Try a simple Src as a canary. If it fails, skip this sink.
mtklein748ca3b2015-01-15 10:56:12 -0800810 struct : public Src {
mtkleine0effd62015-07-29 06:37:28 -0700811 Error draw(SkCanvas* c) const override {
812 c->drawRect(SkRect::MakeWH(1,1), SkPaint());
813 return "";
814 }
mtklein36352bf2015-03-25 18:17:31 -0700815 SkISize size() const override { return SkISize::Make(16, 16); }
mtkleine0effd62015-07-29 06:37:28 -0700816 Name name() const override { return "justOneRect"; }
817 } justOneRect;
mtklein114c3cd2015-01-15 10:15:02 -0800818
mtklein748ca3b2015-01-15 10:56:12 -0800819 SkBitmap bitmap;
820 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800821 SkString log;
mtkleine0effd62015-07-29 06:37:28 -0700822 Error err = sink->draw(justOneRect, &bitmap, &stream, &log);
mtklein4089ef72015-03-05 08:40:28 -0800823 if (err.isFatal()) {
mtkleinc41fd922016-03-08 09:01:39 -0800824 info("Could not run %s: %s\n", config.getTag().c_str(), err.c_str());
mtklein05641a52015-04-21 10:49:13 -0700825 exit(1);
mtklein114c3cd2015-01-15 10:15:02 -0800826 }
827
mtkleine0effd62015-07-29 06:37:28 -0700828 TaggedSink& ts = gSinks.push_back();
mtklein18300a32016-03-16 13:53:35 -0700829 ts.reset(sink.release());
kkinnunen3e980c32015-12-23 01:33:00 -0800830 ts.tag = config.getTag();
mtklein748ca3b2015-01-15 10:56:12 -0800831}
832
833static bool gpu_supported() {
834#if SK_SUPPORT_GPU
835 return FLAGS_gpu;
836#else
837 return false;
838#endif
839}
kkinnunen9ebc3f02015-12-21 23:48:13 -0800840
kkinnunen3e980c32015-12-23 01:33:00 -0800841static Sink* create_sink(const SkCommandLineConfig* config) {
842#if SK_SUPPORT_GPU
843 if (gpu_supported()) {
844 if (const SkCommandLineConfigGpu* gpuConfig = config->asConfigGpu()) {
bsalomon85b4b532016-04-05 11:06:27 -0700845 GrContextFactory::ContextType contextType = gpuConfig->getContextType();
csmartdalton6270e552016-09-13 10:41:49 -0700846 GrContextFactory::ContextOptions contextOptions = gpuConfig->getContextOptions();
kkinnunen3e980c32015-12-23 01:33:00 -0800847 GrContextFactory testFactory;
848 if (!testFactory.get(contextType, contextOptions)) {
mtkleinc41fd922016-03-08 09:01:39 -0800849 info("WARNING: can not create GPU context for config '%s'. "
850 "GM tests will be skipped.\n", gpuConfig->getTag().c_str());
kkinnunen3e980c32015-12-23 01:33:00 -0800851 return nullptr;
852 }
853 return new GPUSink(contextType, contextOptions, gpuConfig->getSamples(),
brianosmand93c1202016-03-10 07:49:08 -0800854 gpuConfig->getUseDIText(), gpuConfig->getColorType(),
brianosmanb109b8c2016-06-16 13:03:24 -0700855 sk_ref_sp(gpuConfig->getColorSpace()), FLAGS_gpu_threading);
kkinnunen3e980c32015-12-23 01:33:00 -0800856 }
857 }
858#endif
859
860#define SINK(t, sink, ...) if (config->getBackend().equals(t)) { return new sink(__VA_ARGS__); }
861
mtklein748ca3b2015-01-15 10:56:12 -0800862 if (FLAGS_cpu) {
Brian Osman526972e2016-10-24 09:24:02 -0400863 auto srgbColorSpace = SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named);
864 auto srgbLinearColorSpace = SkColorSpace::MakeNamed(SkColorSpace::kSRGBLinear_Named);
brianosmanb109b8c2016-06-16 13:03:24 -0700865
mtklein748ca3b2015-01-15 10:56:12 -0800866 SINK("565", RasterSink, kRGB_565_SkColorType);
867 SINK("8888", RasterSink, kN32_SkColorType);
brianosmanb109b8c2016-06-16 13:03:24 -0700868 SINK("srgb", RasterSink, kN32_SkColorType, srgbColorSpace);
raftias7c602de2016-10-13 10:45:44 -0700869 SINK("f16", RasterSink, kRGBA_F16_SkColorType, srgbLinearColorSpace);
halcanary94b87bd2016-04-04 06:12:15 -0700870 SINK("pdf", PDFSink);
mtklein9c3f17d2015-01-28 11:35:18 -0800871 SINK("skp", SKPSink);
reed54dc4872016-09-13 08:09:45 -0700872 SINK("pipe", PipeSink);
mtklein8a4527e2015-01-31 20:00:58 -0800873 SINK("svg", SVGSink);
874 SINK("null", NullSink);
halcanary47ef4d52015-03-03 09:13:09 -0800875 SINK("xps", XPSSink);
halcanary4b656662016-04-27 07:45:18 -0700876 SINK("pdfa", PDFSink, true);
Hal Canary85c7fe82016-10-25 10:33:27 -0400877 SINK("jsdebug", DebugSink);
mtklein748ca3b2015-01-15 10:56:12 -0800878 }
879#undef SINK
halcanary96fcdcc2015-08-27 07:41:13 -0700880 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800881}
882
kkinnunen3e980c32015-12-23 01:33:00 -0800883static Sink* create_via(const SkString& tag, Sink* wrapped) {
884#define VIA(t, via, ...) if (tag.equals(t)) { return new via(__VA_ARGS__); }
mtklein9c5052f2016-08-06 12:51:51 -0700885 VIA("lite", ViaLite, wrapped);
reed54dc4872016-09-13 08:09:45 -0700886 VIA("pipe", ViaPipe, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700887 VIA("twice", ViaTwice, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700888 VIA("serialize", ViaSerialization, wrapped);
mtklein4a34ecb2016-01-08 10:19:35 -0800889 VIA("pic", ViaPicture, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700890 VIA("2ndpic", ViaSecondPicture, wrapped);
mtkleind31c13d2015-05-05 12:59:56 -0700891 VIA("sp", ViaSingletonPictures, wrapped);
reedbabc3de2016-07-08 08:43:27 -0700892 VIA("defer", ViaDefer, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700893 VIA("tiles", ViaTiles, 256, 256, nullptr, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800894 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800895
mtkleind603b222015-02-17 11:13:33 -0800896 if (FLAGS_matrix.count() == 4) {
mtklein748ca3b2015-01-15 10:56:12 -0800897 SkMatrix m;
mtkleind603b222015-02-17 11:13:33 -0800898 m.reset();
899 m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
900 m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
901 m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
902 m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
903 VIA("matrix", ViaMatrix, m, wrapped);
904 VIA("upright", ViaUpright, m, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800905 }
tomhudson64de1e12015-03-05 08:01:07 -0800906
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]{
Ben Wagner145dbcd2016-11-03 14:40:50 -04001075 std::unique_ptr<SkStreamAsset> ownedData(data);
mtkleincd50bca2016-01-05 06:20:20 -08001076
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) {
Brian Osman7039f742016-11-01 15:56:16 -04001139 gammaCorrect = SkToBool(bitmap->info().colorSpace());
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() {
Mike Kleinadacaef2017-02-06 09:26:14 -05001288#if defined(SK_BUILD_FOR_IOS)
1289 cd_Documents();
1290#endif
bsalomon71de3532016-04-26 14:27:21 -07001291 setbuf(stdout, nullptr);
mtklein246ba3a2016-02-23 10:39:36 -08001292 setup_crash_handler();
mtkleinafae30a2016-02-24 12:28:32 -08001293
Yuqian Liba62b4a2016-12-14 13:46:53 -05001294 gSkUseAnalyticAA = FLAGS_analyticAA;
liyuqian38911a72016-10-04 11:23:22 -07001295
Yuqian Li550148b2017-01-13 10:13:13 -05001296 if (FLAGS_forceAnalyticAA) {
1297 gSkForceAnalyticAA = true;
1298 }
1299
mtkleina483da32016-03-17 12:53:36 -07001300 if (FLAGS_verbose) {
1301 gVLog = stderr;
1302 } else if (!FLAGS_writePath.isEmpty()) {
mtklein51c8cfc2016-03-11 12:59:09 -08001303 sk_mkdir(FLAGS_writePath[0]);
mtkleina1ce2162016-07-22 12:23:46 -07001304 gVLog = fopen(SkOSPath::Join(FLAGS_writePath[0], "verbose.log").c_str(), "w");
mtkleinc41fd922016-03-08 09:01:39 -08001305 }
1306
mtklein5286f022016-01-22 08:18:14 -08001307 JsonWriter::DumpJson(); // It's handy for the bots to assume this is ~never missing.
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +00001308 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -07001309 SkTaskGroup::Enabler enabled(FLAGS_threads);
caryclark83ca6282015-06-10 09:31:09 -07001310 gCreateTypefaceDelegate = &create_from_name;
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +00001311
halcanary7d571242016-02-24 17:59:16 -08001312 {
1313 SkString testResourcePath = GetResourcePath("color_wheel.png");
1314 SkFILEStream testResource(testResourcePath.c_str());
1315 if (!testResource.isValid()) {
mtkleinc41fd922016-03-08 09:01:39 -08001316 info("Some resources are missing. Do you need to set --resourcePath?\n");
halcanary7d571242016-02-24 17:59:16 -08001317 }
1318 }
mtklein62bd1a62015-01-27 14:46:26 -08001319 gather_gold();
borenet09ed4802015-04-03 14:15:33 -07001320 gather_uninteresting_hashes();
mtklein62bd1a62015-01-27 14:46:26 -08001321
scroggo86737142016-02-03 12:19:11 -08001322 if (!gather_srcs()) {
1323 return 1;
1324 }
brianosman05c987c2016-05-09 13:42:16 -07001325 if (!gather_sinks()) {
1326 return 1;
1327 }
mtklein748ca3b2015-01-15 10:56:12 -08001328 gather_tests();
mtklein21eaf3b2016-02-08 12:39:59 -08001329 gPending = gSrcs.count() * gSinks.count() + gParallelTests.count() + gSerialTests.count();
mtkleinc41fd922016-03-08 09:01:39 -08001330 info("%d srcs * %d sinks + %d tests == %d tasks",
1331 gSrcs.count(), gSinks.count(), gParallelTests.count() + gSerialTests.count(), gPending);
Ben Wagner145dbcd2016-11-03 14:40:50 -04001332 std::unique_ptr<SkThread> statusThread(start_status_thread());
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) {
1356 parallel.add([test] { run_test(test); });
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); }
1361 for (auto test : gSerialTests) { run_test(test); }
1362
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
1388 print_status();
Herb Derby5a523fe2017-01-26 16:48:28 -05001389 SkGraphics::PurgeAllCaches();
mtkleinc41fd922016-03-08 09:01:39 -08001390 info("Finished!\n");
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}
Brian Osman287f6512016-12-05 11:35:07 -05001411const char* ContextTypeName(GrContextFactory::ContextType contextType) {
1412 switch (contextType) {
1413 case GrContextFactory::kGL_ContextType:
1414 return "OpenGL";
1415 case GrContextFactory::kGLES_ContextType:
1416 return "OpenGLES";
1417 case GrContextFactory::kANGLE_D3D9_ES2_ContextType:
1418 return "ANGLE D3D9 ES2";
1419 case GrContextFactory::kANGLE_D3D11_ES2_ContextType:
1420 return "ANGLE D3D11 ES2";
1421 case GrContextFactory::kANGLE_D3D11_ES3_ContextType:
1422 return "ANGLE D3D11 ES3";
1423 case GrContextFactory::kANGLE_GL_ES2_ContextType:
1424 return "ANGLE GL ES2";
1425 case GrContextFactory::kANGLE_GL_ES3_ContextType:
1426 return "ANGLE GL ES3";
1427 case GrContextFactory::kCommandBuffer_ContextType:
1428 return "Command Buffer";
1429 case GrContextFactory::kMESA_ContextType:
1430 return "Mesa";
1431 case GrContextFactory::kNullGL_ContextType:
1432 return "Null GL";
1433 case GrContextFactory::kDebugGL_ContextType:
1434 return "Debug GL";
1435 case GrContextFactory::kVulkan_ContextType:
1436 return "Vulkan";
1437 }
1438 SkDEBUGFAIL("Unreachable");
1439 return "Unknown";
1440}
bsalomon758586c2016-04-06 14:02:39 -07001441#else
1442bool IsGLContextType(int) { return false; }
bsalomondc0fcd42016-04-11 14:21:33 -07001443bool IsVulkanContextType(int) { return false; }
bsalomon758586c2016-04-06 14:02:39 -07001444bool IsRenderingGLContextType(int) { return false; }
1445bool IsNullGLContextType(int) { return false; }
1446#endif
1447
1448void RunWithGPUTestContexts(GrContextTestFn* test, GrContextTypeFilterFn* contextTypeFilter,
bsalomonf2f1c172016-04-05 12:59:06 -07001449 Reporter* reporter, GrContextFactory* factory) {
kkinnunen179a8f52015-11-20 13:32:24 -08001450#if SK_SUPPORT_GPU
kkinnunen3e980c32015-12-23 01:33:00 -08001451
bsalomon758586c2016-04-06 14:02:39 -07001452 for (int typeInt = 0; typeInt < GrContextFactory::kContextTypeCnt; ++typeInt) {
1453 GrContextFactory::ContextType contextType = (GrContextFactory::ContextType) typeInt;
Brian Osmanaa2219c2016-12-05 10:12:17 -05001454 // Use "native" instead of explicitly trying OpenGL and OpenGL ES. Do not use GLES on
bsalomon758586c2016-04-06 14:02:39 -07001455 // desktop since tests do not account for not fixing http://skbug.com/2809
1456 if (contextType == GrContextFactory::kGL_ContextType ||
1457 contextType == GrContextFactory::kGLES_ContextType) {
1458 if (contextType != GrContextFactory::kNativeGL_ContextType) {
1459 continue;
1460 }
1461 }
Brian Osmanaa2219c2016-12-05 10:12:17 -05001462 ContextInfo ctxInfo = factory->getContextInfo(contextType);
1463 if (contextTypeFilter && !(*contextTypeFilter)(contextType)) {
1464 continue;
1465 }
Brian Osman287f6512016-12-05 11:35:07 -05001466 ReporterContext ctx(reporter, SkString(ContextTypeName(contextType)));
bsalomon8b7451a2016-05-11 06:33:06 -07001467 if (ctxInfo.grContext()) {
bsalomonf2f1c172016-04-05 12:59:06 -07001468 (*test)(reporter, ctxInfo);
kkinnunen5219fd92015-12-10 06:28:13 -08001469 }
bsalomonf2f1c172016-04-05 12:59:06 -07001470 ctxInfo = factory->getContextInfo(contextType,
csmartdalton6270e552016-09-13 10:41:49 -07001471 GrContextFactory::ContextOptions::kEnableNVPR);
bsalomon8b7451a2016-05-11 06:33:06 -07001472 if (ctxInfo.grContext()) {
bsalomonf2f1c172016-04-05 12:59:06 -07001473 (*test)(reporter, ctxInfo);
kkinnunen179a8f52015-11-20 13:32:24 -08001474 }
1475 }
1476#endif
1477}
kkinnunen179a8f52015-11-20 13:32:24 -08001478} // namespace skiatest
1479
Mike Klein6d3b7aa2017-01-30 14:00:54 -05001480#if !defined(SK_BUILD_FOR_IOS) || defined(DM_DEFINE_MAIN)
jcgregorio3b27ade2014-11-13 08:06:40 -08001481int main(int argc, char** argv) {
1482 SkCommandLineFlags::Parse(argc, argv);
1483 return dm_main();
1484}
1485#endif