blob: 3dced058d087ee72d7f3cfc9f81fad0e9a4328a6 [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) {
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500863 auto srgbColorSpace = SkColorSpace::MakeSRGB();
864 auto srgbLinearColorSpace = SkColorSpace::MakeSRGBLinear();
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);
Mike Reedf67c4592017-02-17 17:06:11 -0500888 VIA("svg", ViaSVG, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700889 VIA("serialize", ViaSerialization, wrapped);
mtklein4a34ecb2016-01-08 10:19:35 -0800890 VIA("pic", ViaPicture, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700891 VIA("2ndpic", ViaSecondPicture, wrapped);
mtkleind31c13d2015-05-05 12:59:56 -0700892 VIA("sp", ViaSingletonPictures, wrapped);
reedbabc3de2016-07-08 08:43:27 -0700893 VIA("defer", ViaDefer, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700894 VIA("tiles", ViaTiles, 256, 256, nullptr, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800895 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800896
mtkleind603b222015-02-17 11:13:33 -0800897 if (FLAGS_matrix.count() == 4) {
mtklein748ca3b2015-01-15 10:56:12 -0800898 SkMatrix m;
mtkleind603b222015-02-17 11:13:33 -0800899 m.reset();
900 m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
901 m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
902 m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
903 m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
904 VIA("matrix", ViaMatrix, m, wrapped);
905 VIA("upright", ViaUpright, m, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800906 }
tomhudson64de1e12015-03-05 08:01:07 -0800907
mtklein748ca3b2015-01-15 10:56:12 -0800908#undef VIA
halcanary96fcdcc2015-08-27 07:41:13 -0700909 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800910}
911
brianosman05c987c2016-05-09 13:42:16 -0700912static bool gather_sinks() {
kkinnunen3e980c32015-12-23 01:33:00 -0800913 SkCommandLineConfigArray configs;
914 ParseConfigs(FLAGS_config, &configs);
915 for (int i = 0; i < configs.count(); i++) {
916 const SkCommandLineConfig& config = *configs[i];
917 Sink* sink = create_sink(&config);
918 if (sink == nullptr) {
mtkleinc41fd922016-03-08 09:01:39 -0800919 info("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
920 config.getTag().c_str());
kkinnunen3e980c32015-12-23 01:33:00 -0800921 continue;
922 }
mtklein748ca3b2015-01-15 10:56:12 -0800923
kkinnunen3e980c32015-12-23 01:33:00 -0800924 const SkTArray<SkString>& parts = config.getViaParts();
925 for (int j = parts.count(); j-- > 0;) {
926 const SkString& part = parts[j];
927 Sink* next = create_via(part, sink);
halcanary96fcdcc2015-08-27 07:41:13 -0700928 if (next == nullptr) {
mtkleinc41fd922016-03-08 09:01:39 -0800929 info("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
930 part.c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800931 delete sink;
halcanary96fcdcc2015-08-27 07:41:13 -0700932 sink = nullptr;
mtklein748ca3b2015-01-15 10:56:12 -0800933 break;
934 }
935 sink = next;
936 }
937 if (sink) {
938 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800939 }
940 }
brianosman05c987c2016-05-09 13:42:16 -0700941
942 // If no configs were requested (just running tests, perhaps?), then we're okay.
943 // Otherwise, make sure that at least one sink was constructed correctly. This catches
944 // the case of bots without a GPU being assigned GPU configs.
945 return (configs.count() == 0) || (gSinks.count() > 0);
mtklein114c3cd2015-01-15 10:15:02 -0800946}
mtklein709d2c32015-01-15 08:30:25 -0800947
mtkleina5114d72015-08-24 13:27:01 -0700948static bool dump_png(SkBitmap bitmap, const char* path, const char* md5) {
949 const int w = bitmap.width(),
950 h = bitmap.height();
951
brianosman3c579dc2016-04-19 09:18:11 -0700952 sk_sp<SkData> encodedBitmap = sk_tools::encode_bitmap_for_png(bitmap);
953 if (encodedBitmap.get() == nullptr) {
954 return false;
mtkleina5114d72015-08-24 13:27:01 -0700955 }
brianosman3c579dc2016-04-19 09:18:11 -0700956 uint32_t* rgba = static_cast<uint32_t*>(encodedBitmap.get()->writable_data());
mtkleina5114d72015-08-24 13:27:01 -0700957
958 // We don't need bitmap anymore. Might as well drop our ref.
mtkleinfccb77d2015-08-24 14:13:29 -0700959 bitmap.reset();
mtkleina5114d72015-08-24 13:27:01 -0700960
mtkleinc64137c2015-08-25 10:56:08 -0700961 FILE* f = fopen(path, "wb");
mtkleina5114d72015-08-24 13:27:01 -0700962 if (!f) { return false; }
963
964 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
965 if (!png) {
966 fclose(f);
967 return false;
968 }
969
970 png_infop info = png_create_info_struct(png);
971 if (!info) {
972 png_destroy_write_struct(&png, &info);
973 fclose(f);
974 return false;
975 }
976
mtkleind69ece62015-09-10 10:37:44 -0700977 SkString description;
978 description.append("Key: ");
979 for (int i = 0; i < FLAGS_key.count(); i++) {
980 description.appendf("%s ", FLAGS_key[i]);
981 }
982 description.append("Properties: ");
983 for (int i = 0; i < FLAGS_properties.count(); i++) {
984 description.appendf("%s ", FLAGS_properties[i]);
985 }
986 description.appendf("MD5: %s", md5);
987
mtkleina5114d72015-08-24 13:27:01 -0700988 png_text text[2];
989 text[0].key = (png_charp)"Author";
990 text[0].text = (png_charp)"DM dump_png()";
991 text[0].compression = PNG_TEXT_COMPRESSION_NONE;
992 text[1].key = (png_charp)"Description";
mtkleind69ece62015-09-10 10:37:44 -0700993 text[1].text = (png_charp)description.c_str();
mtkleina5114d72015-08-24 13:27:01 -0700994 text[1].compression = PNG_TEXT_COMPRESSION_NONE;
995 png_set_text(png, info, text, 2);
996
997 png_init_io(png, f);
998 png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8,
999 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
1000 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
1001 png_write_info(png, info);
1002 for (int j = 0; j < h; j++) {
brianosman3c579dc2016-04-19 09:18:11 -07001003 png_bytep row = (png_bytep)(rgba + w*j);
mtkleina5114d72015-08-24 13:27:01 -07001004 png_write_rows(png, &row, 1);
1005 }
1006 png_write_end(png, info);
1007
1008 png_destroy_write_struct(&png, &info);
1009 fclose(f);
1010 return true;
1011}
1012
mtkleina2ef6422015-01-15 13:44:22 -08001013static bool match(const char* needle, const char* haystack) {
halcanary96fcdcc2015-08-27 07:41:13 -07001014 return 0 == strcmp("_", needle) || nullptr != strstr(haystack, needle);
mtkleina2ef6422015-01-15 13:44:22 -08001015}
1016
mtklein3eed7dd2016-02-24 19:07:07 -08001017static bool is_blacklisted(const char* sink, const char* src,
1018 const char* srcOptions, const char* name) {
mtklein0d243ff2015-04-03 07:51:00 -07001019 for (int i = 0; i < FLAGS_blacklist.count() - 3; i += 4) {
mtkleina2ef6422015-01-15 13:44:22 -08001020 if (match(FLAGS_blacklist[i+0], sink) &&
djsollen54416de2015-04-03 07:24:48 -07001021 match(FLAGS_blacklist[i+1], src) &&
1022 match(FLAGS_blacklist[i+2], srcOptions) &&
1023 match(FLAGS_blacklist[i+3], name)) {
mtklein3eed7dd2016-02-24 19:07:07 -08001024 return true;
mtkleina2ef6422015-01-15 13:44:22 -08001025 }
1026 }
mtklein3eed7dd2016-02-24 19:07:07 -08001027 return false;
mtkleina2ef6422015-01-15 13:44:22 -08001028}
1029
mtkleincd50bca2016-01-05 06:20:20 -08001030// Even when a Task Sink reports to be non-threadsafe (e.g. GPU), we know things like
1031// .png encoding are definitely thread safe. This lets us offload that work to CPU threads.
1032static SkTaskGroup gDefinitelyThreadSafeWork;
1033
mtklein748ca3b2015-01-15 10:56:12 -08001034// The finest-grained unit of work we can run: draw a single Src into a single Sink,
1035// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
1036struct Task {
mtkleine0effd62015-07-29 06:37:28 -07001037 Task(const TaggedSrc& src, const TaggedSink& sink) : src(src), sink(sink) {}
1038 const TaggedSrc& src;
1039 const TaggedSink& sink;
mtklein748ca3b2015-01-15 10:56:12 -08001040
mtklein21eaf3b2016-02-08 12:39:59 -08001041 static void Run(const Task& task) {
1042 SkString name = task.src->name();
mtkleine0effd62015-07-29 06:37:28 -07001043
mtkleinb9eb4ac2015-02-02 18:26:03 -08001044 SkString log;
mtklein3eed7dd2016-02-24 19:07:07 -08001045 if (!FLAGS_dryRun) {
mtklein748ca3b2015-01-15 10:56:12 -08001046 SkBitmap bitmap;
1047 SkDynamicMemoryWStream stream;
mtklein3eed7dd2016-02-24 19:07:07 -08001048 start(task.sink.tag.c_str(), task.src.tag.c_str(),
1049 task.src.options.c_str(), name.c_str());
mtklein21eaf3b2016-02-08 12:39:59 -08001050 Error err = task.sink->draw(*task.src, &bitmap, &stream, &log);
mtkleinb3b13b72016-03-07 13:20:52 -08001051 if (!log.isEmpty()) {
mtkleinc41fd922016-03-08 09:01:39 -08001052 info("%s %s %s %s:\n%s\n", task.sink.tag.c_str()
1053 , task.src.tag.c_str()
1054 , task.src.options.c_str()
1055 , name.c_str()
1056 , log.c_str());
mtkleinb3b13b72016-03-07 13:20:52 -08001057 }
mtklein748ca3b2015-01-15 10:56:12 -08001058 if (!err.isEmpty()) {
mtklein4089ef72015-03-05 08:40:28 -08001059 if (err.isFatal()) {
djsollen54416de2015-04-03 07:24:48 -07001060 fail(SkStringPrintf("%s %s %s %s: %s",
mtklein21eaf3b2016-02-08 12:39:59 -08001061 task.sink.tag.c_str(),
1062 task.src.tag.c_str(),
1063 task.src.options.c_str(),
mtklein4089ef72015-03-05 08:40:28 -08001064 name.c_str(),
1065 err.c_str()));
mtkleinb37cb412015-03-18 05:27:14 -07001066 } else {
mtklein3eed7dd2016-02-24 19:07:07 -08001067 done(task.sink.tag.c_str(), task.src.tag.c_str(),
1068 task.src.options.c_str(), name.c_str());
mtkleinba6ada72016-01-21 09:39:35 -08001069 return;
mtklein4089ef72015-03-05 08:40:28 -08001070 }
mtklein748ca3b2015-01-15 10:56:12 -08001071 }
mtklein62bd1a62015-01-27 14:46:26 -08001072
mtkleincd50bca2016-01-05 06:20:20 -08001073 // We're likely switching threads here, so we must capture by value, [=] or [foo,bar].
1074 SkStreamAsset* data = stream.detachAsStream();
1075 gDefinitelyThreadSafeWork.add([task,name,bitmap,data]{
Ben Wagner145dbcd2016-11-03 14:40:50 -04001076 std::unique_ptr<SkStreamAsset> ownedData(data);
mtkleincd50bca2016-01-05 06:20:20 -08001077
1078 // Why doesn't the copy constructor do this when we have pre-locked pixels?
1079 bitmap.lockPixels();
1080
1081 SkString md5;
1082 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
1083 SkMD5 hash;
1084 if (data->getLength()) {
1085 hash.writeStream(data, data->getLength());
1086 data->rewind();
mtklein38408462015-07-08 07:25:27 -07001087 } else {
mtkleincd50bca2016-01-05 06:20:20 -08001088 // If we're BGRA (Linux, Windows), swizzle over to RGBA (Mac, Android).
1089 // This helps eliminate multiple 0-pixel-diff hashes on gold.skia.org.
1090 // (Android's general slow speed breaks the tie arbitrarily in RGBA's favor.)
1091 // We might consider promoting 565 to RGBA too.
1092 if (bitmap.colorType() == kBGRA_8888_SkColorType) {
1093 SkBitmap swizzle;
1094 SkAssertResult(bitmap.copyTo(&swizzle, kRGBA_8888_SkColorType));
1095 hash.write(swizzle.getPixels(), swizzle.getSize());
1096 } else {
1097 hash.write(bitmap.getPixels(), bitmap.getSize());
1098 }
1099 }
1100 SkMD5::Digest digest;
1101 hash.finish(digest);
1102 for (int i = 0; i < 16; i++) {
1103 md5.appendf("%02x", digest.data[i]);
mtklein38408462015-07-08 07:25:27 -07001104 }
mtklein62bd1a62015-01-27 14:46:26 -08001105 }
mtklein62bd1a62015-01-27 14:46:26 -08001106
mtkleincd50bca2016-01-05 06:20:20 -08001107 if (!FLAGS_readPath.isEmpty() &&
mtklein3eed7dd2016-02-24 19:07:07 -08001108 !gGold.contains(Gold(task.sink.tag, task.src.tag,
1109 task.src.options, name, md5))) {
mtkleincd50bca2016-01-05 06:20:20 -08001110 fail(SkStringPrintf("%s not found for %s %s %s %s in %s",
1111 md5.c_str(),
mtklein21eaf3b2016-02-08 12:39:59 -08001112 task.sink.tag.c_str(),
1113 task.src.tag.c_str(),
1114 task.src.options.c_str(),
mtkleincd50bca2016-01-05 06:20:20 -08001115 name.c_str(),
1116 FLAGS_readPath[0]));
mtklein748ca3b2015-01-15 10:56:12 -08001117 }
mtkleincd50bca2016-01-05 06:20:20 -08001118
1119 if (!FLAGS_writePath.isEmpty()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001120 const char* ext = task.sink->fileExtension();
mtkleincd50bca2016-01-05 06:20:20 -08001121 if (data->getLength()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001122 WriteToDisk(task, md5, ext, data, data->getLength(), nullptr);
mtkleincd50bca2016-01-05 06:20:20 -08001123 SkASSERT(bitmap.drawsNothing());
1124 } else if (!bitmap.drawsNothing()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001125 WriteToDisk(task, md5, ext, nullptr, 0, &bitmap);
mtkleincd50bca2016-01-05 06:20:20 -08001126 }
1127 }
1128 });
mtklein748ca3b2015-01-15 10:56:12 -08001129 }
mtklein3eed7dd2016-02-24 19:07:07 -08001130 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 -08001131 }
1132
1133 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -08001134 SkString md5,
1135 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -08001136 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -08001137 const SkBitmap* bitmap) {
mtklein409d4702016-02-29 07:38:01 -08001138 bool gammaCorrect = false;
1139 if (bitmap) {
Brian Osman7039f742016-11-01 15:56:16 -04001140 gammaCorrect = SkToBool(bitmap->info().colorSpace());
mtklein409d4702016-02-29 07:38:01 -08001141 }
1142
mtklein748ca3b2015-01-15 10:56:12 -08001143 JsonWriter::BitmapResult result;
djsollen54416de2015-04-03 07:24:48 -07001144 result.name = task.src->name();
mtklein3eed7dd2016-02-24 19:07:07 -08001145 result.config = task.sink.tag;
djsollen54416de2015-04-03 07:24:48 -07001146 result.sourceType = task.src.tag;
1147 result.sourceOptions = task.src.options;
1148 result.ext = ext;
mtklein409d4702016-02-29 07:38:01 -08001149 result.gammaCorrect = gammaCorrect;
djsollen54416de2015-04-03 07:24:48 -07001150 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -08001151 JsonWriter::AddBitmapResult(result);
1152
mtkleinb0531a72015-04-07 13:38:48 -07001153 // If an MD5 is uninteresting, we want it noted in the JSON file,
1154 // but don't want to dump it out as a .png (or whatever ext is).
1155 if (gUninterestingHashes.contains(md5)) {
1156 return;
1157 }
1158
mtklein748ca3b2015-01-15 10:56:12 -08001159 const char* dir = FLAGS_writePath[0];
1160 if (0 == strcmp(dir, "@")) { // Needed for iOS.
1161 dir = FLAGS_resourcePath[0];
1162 }
1163 sk_mkdir(dir);
1164
1165 SkString path;
1166 if (FLAGS_nameByHash) {
1167 path = SkOSPath::Join(dir, result.md5.c_str());
1168 path.append(".");
1169 path.append(ext);
1170 if (sk_exists(path.c_str())) {
1171 return; // Content-addressed. If it exists already, we're done.
1172 }
1173 } else {
kkinnunen3e980c32015-12-23 01:33:00 -08001174 path = SkOSPath::Join(dir, task.sink.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001175 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001176 path = SkOSPath::Join(path.c_str(), task.src.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001177 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001178 if (strcmp(task.src.options.c_str(), "") != 0) {
1179 path = SkOSPath::Join(path.c_str(), task.src.options.c_str());
djsollen54416de2015-04-03 07:24:48 -07001180 sk_mkdir(path.c_str());
1181 }
mtklein748ca3b2015-01-15 10:56:12 -08001182 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
1183 path.append(".");
1184 path.append(ext);
1185 }
1186
mtklein748ca3b2015-01-15 10:56:12 -08001187 if (bitmap) {
mtkleina5114d72015-08-24 13:27:01 -07001188 if (!dump_png(*bitmap, path.c_str(), result.md5.c_str())) {
mtklein748ca3b2015-01-15 10:56:12 -08001189 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
1190 return;
1191 }
1192 } else {
mtkleina5114d72015-08-24 13:27:01 -07001193 SkFILEWStream file(path.c_str());
1194 if (!file.isValid()) {
1195 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
1196 return;
1197 }
mtklein748ca3b2015-01-15 10:56:12 -08001198 if (!file.writeStream(data, len)) {
1199 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
1200 return;
1201 }
1202 }
1203 }
1204};
1205
mtklein748ca3b2015-01-15 10:56:12 -08001206/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1207
1208// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
1209
mtklein21eaf3b2016-02-08 12:39:59 -08001210static SkTDArray<skiatest::Test> gParallelTests, gSerialTests;
mtklein748ca3b2015-01-15 10:56:12 -08001211
1212static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -08001213 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -08001214 return;
1215 }
mtklein6393c062015-04-27 08:45:01 -07001216 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r; r = r->next()) {
1217 if (!in_shard()) {
1218 continue;
1219 }
halcanary87f3ba42015-01-20 09:30:20 -08001220 // Despite its name, factory() is returning a reference to
1221 // link-time static const POD data.
1222 const skiatest::Test& test = r->factory();
1223 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -08001224 continue;
1225 }
halcanary87f3ba42015-01-20 09:30:20 -08001226 if (test.needsGpu && gpu_supported()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001227 (FLAGS_gpu_threading ? gParallelTests : gSerialTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -08001228 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein21eaf3b2016-02-08 12:39:59 -08001229 gParallelTests.push(test);
mtklein82d28432015-01-15 12:46:02 -08001230 }
mtklein748ca3b2015-01-15 10:56:12 -08001231 }
1232}
1233
mtklein21eaf3b2016-02-08 12:39:59 -08001234static void run_test(skiatest::Test test) {
halcanary87f3ba42015-01-20 09:30:20 -08001235 struct : public skiatest::Reporter {
mtklein36352bf2015-03-25 18:17:31 -07001236 void reportFailed(const skiatest::Failure& failure) override {
halcanary87f3ba42015-01-20 09:30:20 -08001237 fail(failure.toString());
1238 JsonWriter::AddTestFailure(failure);
1239 }
mtklein36352bf2015-03-25 18:17:31 -07001240 bool allowExtendedTest() const override {
halcanary87f3ba42015-01-20 09:30:20 -08001241 return FLAGS_pathOpsExtended;
1242 }
mtklein36352bf2015-03-25 18:17:31 -07001243 bool verbose() const override { return FLAGS_veryVerbose; }
halcanary87f3ba42015-01-20 09:30:20 -08001244 } reporter;
djsollen824996a2015-06-12 12:06:22 -07001245
mtklein3eed7dd2016-02-24 19:07:07 -08001246 if (!FLAGS_dryRun && !is_blacklisted("_", "tests", "_", test.name)) {
mtklein21eaf3b2016-02-08 12:39:59 -08001247 start("unit", "test", "", test.name);
mtklein55e88b22015-01-21 15:50:13 -08001248 GrContextFactory factory;
mtklein21eaf3b2016-02-08 12:39:59 -08001249 test.proc(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -08001250 }
mtklein3eed7dd2016-02-24 19:07:07 -08001251 done("unit", "test", "", test.name);
mtklein748ca3b2015-01-15 10:56:12 -08001252}
1253
1254/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1255
mtkleinafae30a2016-02-24 12:28:32 -08001256DEFINE_int32(status_sec, 15, "Print status this often (and if we crash).");
1257
1258SkThread* start_status_thread() {
1259 auto thread = new SkThread([] (void*) {
1260 for (;;) {
1261 print_status();
1262 #if defined(SK_BUILD_FOR_WIN)
1263 Sleep(FLAGS_status_sec * 1000);
1264 #else
1265 sleep(FLAGS_status_sec);
1266 #endif
mtklein2e1c47e2015-03-12 07:16:56 -07001267 }
mtkleinafae30a2016-02-24 12:28:32 -08001268 });
1269 thread->start();
1270 return thread;
mtkleinde6fc2b2015-03-12 06:28:54 -07001271}
1272
caryclark83ca6282015-06-10 09:31:09 -07001273#define PORTABLE_FONT_PREFIX "Toy Liberation "
1274
mbocee6a9912016-05-31 11:42:36 -07001275static sk_sp<SkTypeface> create_from_name(const char familyName[], SkFontStyle style) {
caryclark83ca6282015-06-10 09:31:09 -07001276 if (familyName && strlen(familyName) > sizeof(PORTABLE_FONT_PREFIX)
1277 && !strncmp(familyName, PORTABLE_FONT_PREFIX, sizeof(PORTABLE_FONT_PREFIX) - 1)) {
caryclark1818acb2015-07-24 12:09:25 -07001278 return sk_tool_utils::create_portable_typeface(familyName, style);
caryclark83ca6282015-06-10 09:31:09 -07001279 }
halcanary96fcdcc2015-08-27 07:41:13 -07001280 return nullptr;
caryclark83ca6282015-06-10 09:31:09 -07001281}
1282
1283#undef PORTABLE_FONT_PREFIX
1284
mbocee6a9912016-05-31 11:42:36 -07001285extern sk_sp<SkTypeface> (*gCreateTypefaceDelegate)(const char [], SkFontStyle );
caryclark83ca6282015-06-10 09:31:09 -07001286
Mike Kleinbe28ee22017-02-06 12:46:20 -05001287int main(int argc, char** argv) {
1288 SkCommandLineFlags::Parse(argc, argv);
Mike Kleinadacaef2017-02-06 09:26:14 -05001289#if defined(SK_BUILD_FOR_IOS)
1290 cd_Documents();
1291#endif
bsalomon71de3532016-04-26 14:27:21 -07001292 setbuf(stdout, nullptr);
mtklein246ba3a2016-02-23 10:39:36 -08001293 setup_crash_handler();
mtkleinafae30a2016-02-24 12:28:32 -08001294
Yuqian Liba62b4a2016-12-14 13:46:53 -05001295 gSkUseAnalyticAA = FLAGS_analyticAA;
liyuqian38911a72016-10-04 11:23:22 -07001296
Yuqian Li550148b2017-01-13 10:13:13 -05001297 if (FLAGS_forceAnalyticAA) {
1298 gSkForceAnalyticAA = true;
1299 }
1300
mtkleina483da32016-03-17 12:53:36 -07001301 if (FLAGS_verbose) {
1302 gVLog = stderr;
1303 } else if (!FLAGS_writePath.isEmpty()) {
mtklein51c8cfc2016-03-11 12:59:09 -08001304 sk_mkdir(FLAGS_writePath[0]);
mtkleina1ce2162016-07-22 12:23:46 -07001305 gVLog = fopen(SkOSPath::Join(FLAGS_writePath[0], "verbose.log").c_str(), "w");
mtkleinc41fd922016-03-08 09:01:39 -08001306 }
1307
mtklein5286f022016-01-22 08:18:14 -08001308 JsonWriter::DumpJson(); // It's handy for the bots to assume this is ~never missing.
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +00001309 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -07001310 SkTaskGroup::Enabler enabled(FLAGS_threads);
caryclark83ca6282015-06-10 09:31:09 -07001311 gCreateTypefaceDelegate = &create_from_name;
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +00001312
halcanary7d571242016-02-24 17:59:16 -08001313 {
1314 SkString testResourcePath = GetResourcePath("color_wheel.png");
1315 SkFILEStream testResource(testResourcePath.c_str());
1316 if (!testResource.isValid()) {
mtkleinc41fd922016-03-08 09:01:39 -08001317 info("Some resources are missing. Do you need to set --resourcePath?\n");
halcanary7d571242016-02-24 17:59:16 -08001318 }
1319 }
mtklein62bd1a62015-01-27 14:46:26 -08001320 gather_gold();
borenet09ed4802015-04-03 14:15:33 -07001321 gather_uninteresting_hashes();
mtklein62bd1a62015-01-27 14:46:26 -08001322
scroggo86737142016-02-03 12:19:11 -08001323 if (!gather_srcs()) {
1324 return 1;
1325 }
brianosman05c987c2016-05-09 13:42:16 -07001326 if (!gather_sinks()) {
1327 return 1;
1328 }
mtklein748ca3b2015-01-15 10:56:12 -08001329 gather_tests();
mtklein21eaf3b2016-02-08 12:39:59 -08001330 gPending = gSrcs.count() * gSinks.count() + gParallelTests.count() + gSerialTests.count();
mtkleinc41fd922016-03-08 09:01:39 -08001331 info("%d srcs * %d sinks + %d tests == %d tasks",
1332 gSrcs.count(), gSinks.count(), gParallelTests.count() + gSerialTests.count(), gPending);
Ben Wagner145dbcd2016-11-03 14:40:50 -04001333 std::unique_ptr<SkThread> statusThread(start_status_thread());
mtklein748ca3b2015-01-15 10:56:12 -08001334
mtklein21eaf3b2016-02-08 12:39:59 -08001335 // Kick off as much parallel work as we can, making note of any serial work we'll need to do.
1336 SkTaskGroup parallel;
1337 SkTArray<Task> serial;
1338
1339 for (auto& sink : gSinks)
1340 for (auto& src : gSrcs) {
mtklein3eed7dd2016-02-24 19:07:07 -08001341 if (src->veto(sink->flags()) ||
1342 is_blacklisted(sink.tag.c_str(), src.tag.c_str(),
1343 src.options.c_str(), src->name().c_str())) {
mtkleinf10637f2016-06-10 13:56:35 -07001344 SkAutoMutexAcquire lock(gMutex);
mtklein3eed7dd2016-02-24 19:07:07 -08001345 gPending--;
1346 continue;
1347 }
1348
mtklein21eaf3b2016-02-08 12:39:59 -08001349 Task task(src, sink);
1350 if (src->serial() || sink->serial()) {
1351 serial.push_back(task);
1352 } else {
1353 parallel.add([task] { Task::Run(task); });
mtklein748ca3b2015-01-15 10:56:12 -08001354 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001355 }
mtklein21eaf3b2016-02-08 12:39:59 -08001356 for (auto test : gParallelTests) {
1357 parallel.add([test] { run_test(test); });
mtklein55e88b22015-01-21 15:50:13 -08001358 }
mtklein21eaf3b2016-02-08 12:39:59 -08001359
1360 // With the parallel work running, run serial tasks and tests here on main thread.
1361 for (auto task : serial) { Task::Run(task); }
1362 for (auto test : gSerialTests) { run_test(test); }
1363
1364 // Wait for any remaining parallel work to complete (including any spun off of serial tasks).
1365 parallel.wait();
mtkleincd50bca2016-01-05 06:20:20 -08001366 gDefinitelyThreadSafeWork.wait();
1367
mtkleinda884c42016-02-24 18:00:23 -08001368 // We'd better have run everything.
1369 SkASSERT(gPending == 0);
mtkleine027f172016-02-26 15:53:06 -08001370 // Make sure we've flushed all our results to disk.
1371 JsonWriter::DumpJson();
mtkleinda884c42016-02-24 18:00:23 -08001372
mtklein748ca3b2015-01-15 10:56:12 -08001373 // At this point we're back in single-threaded land.
caryclarkf53ce802015-06-15 06:48:30 -07001374 sk_tool_utils::release_portable_typefaces();
mtklein@google.comd36522d2013-10-16 13:02:15 +00001375
mtklein748ca3b2015-01-15 10:56:12 -08001376 if (gFailures.count() > 0) {
mtkleinc41fd922016-03-08 09:01:39 -08001377 info("Failures:\n");
mtklein748ca3b2015-01-15 10:56:12 -08001378 for (int i = 0; i < gFailures.count(); i++) {
mtkleinc41fd922016-03-08 09:01:39 -08001379 info("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001380 }
mtkleinc41fd922016-03-08 09:01:39 -08001381 info("%d failures\n", gFailures.count());
mtklein748ca3b2015-01-15 10:56:12 -08001382 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001383 }
mtkleinafae30a2016-02-24 12:28:32 -08001384
1385#ifdef SK_PDF_IMAGE_STATS
halcanary7a14b312015-10-01 07:28:13 -07001386 SkPDFImageDumpStats();
mtkleinafae30a2016-02-24 12:28:32 -08001387#endif // SK_PDF_IMAGE_STATS
1388
1389 print_status();
Herb Derby5a523fe2017-01-26 16:48:28 -05001390 SkGraphics::PurgeAllCaches();
mtkleinc41fd922016-03-08 09:01:39 -08001391 info("Finished!\n");
mtklein748ca3b2015-01-15 10:56:12 -08001392 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +00001393}
jcgregorio3b27ade2014-11-13 08:06:40 -08001394
kkinnunen179a8f52015-11-20 13:32:24 -08001395// TODO: currently many GPU tests are declared outside SK_SUPPORT_GPU guards.
1396// Thus we export the empty RunWithGPUTestContexts when SK_SUPPORT_GPU=0.
1397namespace skiatest {
bsalomon758586c2016-04-06 14:02:39 -07001398
1399#if SK_SUPPORT_GPU
1400bool IsGLContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1401 return kOpenGL_GrBackend == GrContextFactory::ContextTypeBackend(type);
1402}
bsalomondc0fcd42016-04-11 14:21:33 -07001403bool IsVulkanContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1404 return kVulkan_GrBackend == GrContextFactory::ContextTypeBackend(type);
1405}
bsalomon758586c2016-04-06 14:02:39 -07001406bool IsRenderingGLContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1407 return IsGLContextType(type) && GrContextFactory::IsRenderingContext(type);
1408}
1409bool IsNullGLContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1410 return type == GrContextFactory::kNullGL_ContextType;
1411}
Brian Osman287f6512016-12-05 11:35:07 -05001412const char* ContextTypeName(GrContextFactory::ContextType contextType) {
1413 switch (contextType) {
1414 case GrContextFactory::kGL_ContextType:
1415 return "OpenGL";
1416 case GrContextFactory::kGLES_ContextType:
1417 return "OpenGLES";
1418 case GrContextFactory::kANGLE_D3D9_ES2_ContextType:
1419 return "ANGLE D3D9 ES2";
1420 case GrContextFactory::kANGLE_D3D11_ES2_ContextType:
1421 return "ANGLE D3D11 ES2";
1422 case GrContextFactory::kANGLE_D3D11_ES3_ContextType:
1423 return "ANGLE D3D11 ES3";
1424 case GrContextFactory::kANGLE_GL_ES2_ContextType:
1425 return "ANGLE GL ES2";
1426 case GrContextFactory::kANGLE_GL_ES3_ContextType:
1427 return "ANGLE GL ES3";
1428 case GrContextFactory::kCommandBuffer_ContextType:
1429 return "Command Buffer";
1430 case GrContextFactory::kMESA_ContextType:
1431 return "Mesa";
1432 case GrContextFactory::kNullGL_ContextType:
1433 return "Null GL";
1434 case GrContextFactory::kDebugGL_ContextType:
1435 return "Debug GL";
1436 case GrContextFactory::kVulkan_ContextType:
1437 return "Vulkan";
1438 }
1439 SkDEBUGFAIL("Unreachable");
1440 return "Unknown";
1441}
bsalomon758586c2016-04-06 14:02:39 -07001442#else
1443bool IsGLContextType(int) { return false; }
bsalomondc0fcd42016-04-11 14:21:33 -07001444bool IsVulkanContextType(int) { return false; }
bsalomon758586c2016-04-06 14:02:39 -07001445bool IsRenderingGLContextType(int) { return false; }
1446bool IsNullGLContextType(int) { return false; }
1447#endif
1448
1449void RunWithGPUTestContexts(GrContextTestFn* test, GrContextTypeFilterFn* contextTypeFilter,
bsalomonf2f1c172016-04-05 12:59:06 -07001450 Reporter* reporter, GrContextFactory* factory) {
kkinnunen179a8f52015-11-20 13:32:24 -08001451#if SK_SUPPORT_GPU
kkinnunen3e980c32015-12-23 01:33:00 -08001452
bsalomon758586c2016-04-06 14:02:39 -07001453 for (int typeInt = 0; typeInt < GrContextFactory::kContextTypeCnt; ++typeInt) {
1454 GrContextFactory::ContextType contextType = (GrContextFactory::ContextType) typeInt;
Brian Osmanaa2219c2016-12-05 10:12:17 -05001455 // Use "native" instead of explicitly trying OpenGL and OpenGL ES. Do not use GLES on
bsalomon758586c2016-04-06 14:02:39 -07001456 // desktop since tests do not account for not fixing http://skbug.com/2809
1457 if (contextType == GrContextFactory::kGL_ContextType ||
1458 contextType == GrContextFactory::kGLES_ContextType) {
1459 if (contextType != GrContextFactory::kNativeGL_ContextType) {
1460 continue;
1461 }
1462 }
Brian Osmanaa2219c2016-12-05 10:12:17 -05001463 ContextInfo ctxInfo = factory->getContextInfo(contextType);
1464 if (contextTypeFilter && !(*contextTypeFilter)(contextType)) {
1465 continue;
1466 }
Brian Osman287f6512016-12-05 11:35:07 -05001467 ReporterContext ctx(reporter, SkString(ContextTypeName(contextType)));
bsalomon8b7451a2016-05-11 06:33:06 -07001468 if (ctxInfo.grContext()) {
bsalomonf2f1c172016-04-05 12:59:06 -07001469 (*test)(reporter, ctxInfo);
kkinnunen5219fd92015-12-10 06:28:13 -08001470 }
bsalomonf2f1c172016-04-05 12:59:06 -07001471 ctxInfo = factory->getContextInfo(contextType,
csmartdalton6270e552016-09-13 10:41:49 -07001472 GrContextFactory::ContextOptions::kEnableNVPR);
bsalomon8b7451a2016-05-11 06:33:06 -07001473 if (ctxInfo.grContext()) {
bsalomonf2f1c172016-04-05 12:59:06 -07001474 (*test)(reporter, ctxInfo);
kkinnunen179a8f52015-11-20 13:32:24 -08001475 }
1476 }
1477#endif
1478}
kkinnunen179a8f52015-11-20 13:32:24 -08001479} // namespace skiatest