blob: 26883b107ee5cdcc3820fe9724d2c97123c3070a [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"
brianosman3c579dc2016-04-19 09:18:11 -070035#include "picture_utils.h"
caryclark83ca6282015-06-10 09:31:09 -070036#include "sk_tool_utils.h"
liyuqian38911a72016-10-04 11:23:22 -070037#include "SkScan.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +000038
scroggo19b91532016-10-24 09:03:26 -070039#include <vector>
40
halcanary7a14b312015-10-01 07:28:13 -070041#ifdef SK_PDF_IMAGE_STATS
42extern void SkPDFImageDumpStats();
43#endif
44
mtkleina5114d72015-08-24 13:27:01 -070045#include "png.h"
46
bungeman60e0fee2015-08-26 05:15:46 -070047#include <stdlib.h>
48
scroggo27a58342015-10-28 08:56:41 -070049#ifndef SK_BUILD_FOR_WIN32
50 #include <unistd.h>
51#endif
52
djsollen54416de2015-04-03 07:24:48 -070053DEFINE_string(src, "tests gm skp image", "Source types to test.");
mtklein748ca3b2015-01-15 10:56:12 -080054DEFINE_bool(nameByHash, false,
55 "If true, write to FLAGS_writePath[0]/<hash>.png instead of "
djsollen54416de2015-04-03 07:24:48 -070056 "to FLAGS_writePath[0]/<config>/<sourceType>/<sourceOptions>/<name>.png");
mtklein748ca3b2015-01-15 10:56:12 -080057DEFINE_bool2(pathOpsExtended, x, false, "Run extended pathOps tests.");
mtkleind603b222015-02-17 11:13:33 -080058DEFINE_string(matrix, "1 0 0 1",
59 "2x2 scale+skew matrix to apply or upright when using "
60 "'matrix' or 'upright' in config.");
mtklein82d28432015-01-15 12:46:02 -080061DEFINE_bool(gpu_threading, false, "Allow GPU work to run on multiple threads?");
mtklein@google.comd36522d2013-10-16 13:02:15 +000062
mtkleina2ef6422015-01-15 13:44:22 -080063DEFINE_string(blacklist, "",
djsollen54416de2015-04-03 07:24:48 -070064 "Space-separated config/src/srcOptions/name quadruples to blacklist. '_' matches anything. E.g. \n"
65 "'--blacklist gpu skp _ _' will blacklist all SKPs drawn into the gpu config.\n"
66 "'--blacklist gpu skp _ _ 8888 gm _ aarects' will also blacklist the aarects GM on 8888.");
mtkleina2ef6422015-01-15 13:44:22 -080067
mtklein62bd1a62015-01-27 14:46:26 -080068DEFINE_string2(readPath, r, "", "If set check for equality with golden results in this directory.");
69
borenet09ed4802015-04-03 14:15:33 -070070DEFINE_string(uninterestingHashesFile, "",
71 "File containing a list of uninteresting hashes. If a result hashes to something in "
72 "this list, no image is written for that result.");
73
mtklein6393c062015-04-27 08:45:01 -070074DEFINE_int32(shards, 1, "We're splitting source data into this many shards.");
75DEFINE_int32(shard, 0, "Which shard do I run?");
76
halcanary45420a92016-06-02 12:41:14 -070077DEFINE_string(mskps, "", "Directory to read mskps from, or a single mskp file.");
78
mtklein748ca3b2015-01-15 10:56:12 -080079using namespace DM;
bsalomon3724e572016-03-30 18:56:19 -070080using sk_gpu_test::GrContextFactory;
bsalomon273c0f52016-03-31 10:59:06 -070081using sk_gpu_test::GLTestContext;
bsalomonf2f1c172016-04-05 12:59:06 -070082using sk_gpu_test::ContextInfo;
mtklein@google.comd36522d2013-10-16 13:02:15 +000083
mtklein748ca3b2015-01-15 10:56:12 -080084/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
85
benjaminwagnerec4d4d72016-03-25 12:59:53 -070086static const double kStartMs = SkTime::GetMSecs();
mtkleinc41fd922016-03-08 09:01:39 -080087
88static FILE* gVLog;
89
90template <typename... Args>
91static void vlog(const char* fmt, Args&&... args) {
92 if (gVLog) {
93 fprintf(gVLog, "%s\t", HumanizeMs(SkTime::GetMSecs() - kStartMs).c_str());
94 fprintf(gVLog, fmt, args...);
95 fflush(gVLog);
96 }
97}
98
99template <typename... Args>
100static void info(const char* fmt, Args&&... args) {
101 vlog(fmt, args...);
102 if (!FLAGS_quiet) {
103 printf(fmt, args...);
104 }
105}
106static void info(const char* fmt) {
107 if (!FLAGS_quiet) {
108 printf("%s", fmt); // Clang warns printf(fmt) is insecure.
109 }
110}
111
reed086eea92016-05-04 17:12:46 -0700112SK_DECLARE_STATIC_MUTEX(gFailuresMutex);
mtklein748ca3b2015-01-15 10:56:12 -0800113static SkTArray<SkString> gFailures;
114
mtklein3eed7dd2016-02-24 19:07:07 -0800115static void fail(const SkString& err) {
mtklein748ca3b2015-01-15 10:56:12 -0800116 SkAutoMutexAcquire lock(gFailuresMutex);
117 SkDebugf("\n\nFAILURE: %s\n\n", err.c_str());
118 gFailures.push_back(err);
halcanary9e398f72015-01-10 11:18:04 -0800119}
120
mtklein0f7748a2016-07-25 15:27:29 -0700121struct Running {
122 SkString id;
123 SkThreadID thread;
124
125 void dump() const {
126 info("\t%s\n", id.c_str());
127 }
128};
mtkleinb37cb412015-03-18 05:27:14 -0700129
mtklein246ba3a2016-02-23 10:39:36 -0800130// We use a spinlock to make locking this in a signal handler _somewhat_ safe.
mtklein15923c92016-02-29 10:14:38 -0800131static SkSpinlock gMutex;
mtklein0f7748a2016-07-25 15:27:29 -0700132static int32_t gPending;
133static SkTArray<Running> gRunning;
mtklein748ca3b2015-01-15 10:56:12 -0800134
mtklein3eed7dd2016-02-24 19:07:07 -0800135static void done(const char* config, const char* src, const char* srcOptions, const char* name) {
136 SkString id = SkStringPrintf("%s %s %s %s", config, src, srcOptions, name);
mtkleinc41fd922016-03-08 09:01:39 -0800137 vlog("done %s\n", id.c_str());
mtkleinafae30a2016-02-24 12:28:32 -0800138 int pending;
mtkleinb37cb412015-03-18 05:27:14 -0700139 {
mtkleinf10637f2016-06-10 13:56:35 -0700140 SkAutoMutexAcquire lock(gMutex);
mtkleinb37cb412015-03-18 05:27:14 -0700141 for (int i = 0; i < gRunning.count(); i++) {
mtklein0f7748a2016-07-25 15:27:29 -0700142 if (gRunning[i].id == id) {
mtkleinb37cb412015-03-18 05:27:14 -0700143 gRunning.removeShuffle(i);
144 break;
145 }
146 }
mtkleinafae30a2016-02-24 12:28:32 -0800147 pending = --gPending;
reed50bc0512015-05-19 14:13:31 -0700148 }
mtkleina17241b2015-01-23 05:48:00 -0800149 // We write our dm.json file every once in a while in case we crash.
150 // Notice this also handles the final dm.json when pending == 0.
151 if (pending % 500 == 0) {
152 JsonWriter::DumpJson();
153 }
mtklein@google.comd36522d2013-10-16 13:02:15 +0000154}
155
mtklein3eed7dd2016-02-24 19:07:07 -0800156static void start(const char* config, const char* src, const char* srcOptions, const char* name) {
157 SkString id = SkStringPrintf("%s %s %s %s", config, src, srcOptions, name);
mtkleinc41fd922016-03-08 09:01:39 -0800158 vlog("start %s\n", id.c_str());
mtkleinf10637f2016-06-10 13:56:35 -0700159 SkAutoMutexAcquire lock(gMutex);
mtklein0f7748a2016-07-25 15:27:29 -0700160 gRunning.push_back({id,SkGetThreadID()});
mtkleinb37cb412015-03-18 05:27:14 -0700161}
162
mtkleinafae30a2016-02-24 12:28:32 -0800163static void print_status() {
mtkleinafae30a2016-02-24 12:28:32 -0800164 int curr = sk_tools::getCurrResidentSetSizeMB(),
165 peak = sk_tools::getMaxResidentSetSizeMB();
mtkleinc41fd922016-03-08 09:01:39 -0800166 SkString elapsed = HumanizeMs(SkTime::GetMSecs() - kStartMs);
mtkleinafae30a2016-02-24 12:28:32 -0800167
mtkleinf10637f2016-06-10 13:56:35 -0700168 SkAutoMutexAcquire lock(gMutex);
mtkleinc41fd922016-03-08 09:01:39 -0800169 info("\n%s elapsed, %d active, %d queued, %dMB RAM, %dMB peak\n",
170 elapsed.c_str(), gRunning.count(), gPending - gRunning.count(), curr, peak);
mtkleinafae30a2016-02-24 12:28:32 -0800171 for (auto& task : gRunning) {
mtklein0f7748a2016-07-25 15:27:29 -0700172 task.dump();
mtkleinafae30a2016-02-24 12:28:32 -0800173 }
174}
175
Mike Klein3cc2d202016-10-24 11:39:43 -0400176static void find_culprit() {
177 // Assumes gMutex is locked.
178 SkThreadID thisThread = SkGetThreadID();
179 for (auto& task : gRunning) {
180 if (task.thread == thisThread) {
181 info("Likely culprit:\n");
182 task.dump();
mtklein0f7748a2016-07-25 15:27:29 -0700183 }
184 }
Mike Klein3cc2d202016-10-24 11:39:43 -0400185}
mtklein0f7748a2016-07-25 15:27:29 -0700186
mtklein246ba3a2016-02-23 10:39:36 -0800187#if defined(SK_BUILD_FOR_WIN32)
mtklein5b64dab2016-06-09 08:59:48 -0700188 static LONG WINAPI crash_handler(EXCEPTION_POINTERS* e) {
mtkleinf8557292016-02-29 06:35:28 -0800189 static const struct {
190 const char* name;
robertphillips75467862016-03-01 14:10:23 -0800191 DWORD code;
mtkleinf8557292016-02-29 06:35:28 -0800192 } kExceptions[] = {
193 #define _(E) {#E, E}
194 _(EXCEPTION_ACCESS_VIOLATION),
195 _(EXCEPTION_BREAKPOINT),
196 _(EXCEPTION_INT_DIVIDE_BY_ZERO),
197 _(EXCEPTION_STACK_OVERFLOW),
198 // TODO: more?
199 #undef _
200 };
201
mtkleinf10637f2016-06-10 13:56:35 -0700202 SkAutoMutexAcquire lock(gMutex);
mtklein5b64dab2016-06-09 08:59:48 -0700203
204 const DWORD code = e->ExceptionRecord->ExceptionCode;
205 info("\nCaught exception %u", code);
206 for (const auto& exception : kExceptions) {
207 if (exception.code == code) {
208 info(" %s", exception.name);
mtkleinf8557292016-02-29 06:35:28 -0800209 }
mtkleinf8557292016-02-29 06:35:28 -0800210 }
mtklein5b64dab2016-06-09 08:59:48 -0700211 info(", was running:\n");
212 for (auto& task : gRunning) {
mtklein0f7748a2016-07-25 15:27:29 -0700213 task.dump();
mtklein5b64dab2016-06-09 08:59:48 -0700214 }
mtklein0f7748a2016-07-25 15:27:29 -0700215 find_culprit();
mtklein5b64dab2016-06-09 08:59:48 -0700216 fflush(stdout);
217
mtkleinf8557292016-02-29 06:35:28 -0800218 // Execute default exception handler... hopefully, exit.
219 return EXCEPTION_EXECUTE_HANDLER;
mtklein246ba3a2016-02-23 10:39:36 -0800220 }
221
Mike Klein3cc2d202016-10-24 11:39:43 -0400222 static void setup_crash_handler() {
223 SetUnhandledExceptionFilter(crash_handler);
224 }
225#else
mtklein246ba3a2016-02-23 10:39:36 -0800226 #include <signal.h>
Mike Klein3cc2d202016-10-24 11:39:43 -0400227 #if !defined(SK_BUILD_FOR_ANDROID)
228 #include <execinfo.h>
229 #endif
230
231 static constexpr int max_of() { return 0; }
232 template <typename... Rest>
233 static constexpr int max_of(int x, Rest... rest) {
234 return x > max_of(rest...) ? x : max_of(rest...);
235 }
236
237 static void (*previous_handler[max_of(SIGABRT,SIGBUS,SIGFPE,SIGILL,SIGSEGV)+1])(int);
mtkleinf8557292016-02-29 06:35:28 -0800238
mtklein5b64dab2016-06-09 08:59:48 -0700239 static void crash_handler(int sig) {
mtkleinf10637f2016-06-10 13:56:35 -0700240 SkAutoMutexAcquire lock(gMutex);
mtklein5b64dab2016-06-09 08:59:48 -0700241
242 info("\nCaught signal %d [%s], was running:\n", sig, strsignal(sig));
243 for (auto& task : gRunning) {
mtklein0f7748a2016-07-25 15:27:29 -0700244 task.dump();
mtklein5b64dab2016-06-09 08:59:48 -0700245 }
mtklein0f7748a2016-07-25 15:27:29 -0700246 find_culprit();
mtklein5b64dab2016-06-09 08:59:48 -0700247
Mike Klein3cc2d202016-10-24 11:39:43 -0400248 #if !defined(SK_BUILD_FOR_ANDROID)
mtklein5b64dab2016-06-09 08:59:48 -0700249 void* stack[64];
250 int count = backtrace(stack, SK_ARRAY_COUNT(stack));
251 char** symbols = backtrace_symbols(stack, count);
252 info("\nStack trace:\n");
253 for (int i = 0; i < count; i++) {
254 info(" %s\n", symbols[i]);
255 }
Mike Klein3cc2d202016-10-24 11:39:43 -0400256 #endif
mtklein5b64dab2016-06-09 08:59:48 -0700257 fflush(stdout);
258
Mike Klein3cc2d202016-10-24 11:39:43 -0400259 signal(sig, previous_handler[sig]);
260 raise(sig);
mtklein5b64dab2016-06-09 08:59:48 -0700261 }
262
mtklein246ba3a2016-02-23 10:39:36 -0800263 static void setup_crash_handler() {
264 const int kSignals[] = { SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGSEGV };
265 for (int sig : kSignals) {
Mike Klein3cc2d202016-10-24 11:39:43 -0400266 previous_handler[sig] = signal(sig, crash_handler);
mtklein246ba3a2016-02-23 10:39:36 -0800267 }
268 }
269#endif
270
mtklein748ca3b2015-01-15 10:56:12 -0800271/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
mtklein114c3cd2015-01-15 10:15:02 -0800272
mtklein62bd1a62015-01-27 14:46:26 -0800273struct Gold : public SkString {
mtkleina82f5622015-02-20 12:30:19 -0800274 Gold() : SkString("") {}
mtklein3eed7dd2016-02-24 19:07:07 -0800275 Gold(const SkString& sink, const SkString& src,
276 const SkString& srcOptions, const SkString& name,
277 const SkString& md5)
mtklein62bd1a62015-01-27 14:46:26 -0800278 : SkString("") {
279 this->append(sink);
280 this->append(src);
djsollen54416de2015-04-03 07:24:48 -0700281 this->append(srcOptions);
mtklein62bd1a62015-01-27 14:46:26 -0800282 this->append(name);
283 this->append(md5);
mtklein62bd1a62015-01-27 14:46:26 -0800284 }
mtkleinc8d1dd42015-10-15 12:23:01 -0700285 struct Hash {
286 uint32_t operator()(const Gold& g) const {
287 return SkGoodHash()((const SkString&)g);
288 }
289 };
mtklein62bd1a62015-01-27 14:46:26 -0800290};
mtkleina82f5622015-02-20 12:30:19 -0800291static SkTHashSet<Gold, Gold::Hash> gGold;
mtklein62bd1a62015-01-27 14:46:26 -0800292
293static void add_gold(JsonWriter::BitmapResult r) {
djsollen54416de2015-04-03 07:24:48 -0700294 gGold.add(Gold(r.config, r.sourceType, r.sourceOptions, r.name, r.md5));
mtklein62bd1a62015-01-27 14:46:26 -0800295}
296
297static void gather_gold() {
298 if (!FLAGS_readPath.isEmpty()) {
299 SkString path(FLAGS_readPath[0]);
300 path.append("/dm.json");
301 if (!JsonWriter::ReadJson(path.c_str(), add_gold)) {
302 fail(SkStringPrintf("Couldn't read %s for golden results.", path.c_str()));
303 }
304 }
305}
306
307/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
308
mtklein935f1b12016-03-16 08:37:19 -0700309#if defined(SK_BUILD_FOR_WIN32)
310 static const char* kNewline = "\r\n";
311#else
312 static const char* kNewline = "\n";
313#endif
314
borenet09ed4802015-04-03 14:15:33 -0700315static SkTHashSet<SkString> gUninterestingHashes;
316
317static void gather_uninteresting_hashes() {
318 if (!FLAGS_uninterestingHashesFile.isEmpty()) {
bungeman38d909e2016-08-02 14:40:46 -0700319 sk_sp<SkData> data(SkData::MakeFromFileName(FLAGS_uninterestingHashesFile[0]));
mtkleincc334b32015-09-22 11:43:53 -0700320 if (!data) {
mtkleinc41fd922016-03-08 09:01:39 -0800321 info("WARNING: unable to read uninteresting hashes from %s\n",
322 FLAGS_uninterestingHashesFile[0]);
mtkleincc334b32015-09-22 11:43:53 -0700323 return;
324 }
borenet09ed4802015-04-03 14:15:33 -0700325 SkTArray<SkString> hashes;
mtklein935f1b12016-03-16 08:37:19 -0700326 SkStrSplit((const char*)data->data(), kNewline, &hashes);
borenet09ed4802015-04-03 14:15:33 -0700327 for (const SkString& hash : hashes) {
328 gUninterestingHashes.add(hash);
329 }
mtkleinc41fd922016-03-08 09:01:39 -0800330 info("FYI: loaded %d distinct uninteresting hashes from %d lines\n",
331 gUninterestingHashes.count(), hashes.count());
borenet09ed4802015-04-03 14:15:33 -0700332 }
333}
334
335/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
336
Ben Wagner145dbcd2016-11-03 14:40:50 -0400337struct TaggedSrc : public std::unique_ptr<Src> {
mtklein3eed7dd2016-02-24 19:07:07 -0800338 SkString tag;
339 SkString options;
mtkleine0effd62015-07-29 06:37:28 -0700340};
341
Ben Wagner145dbcd2016-11-03 14:40:50 -0400342struct TaggedSink : public std::unique_ptr<Sink> {
kkinnunen3e980c32015-12-23 01:33:00 -0800343 SkString tag;
djsollen54416de2015-04-03 07:24:48 -0700344};
mtklein748ca3b2015-01-15 10:56:12 -0800345
346static const bool kMemcpyOK = true;
347
mtkleine0effd62015-07-29 06:37:28 -0700348static SkTArray<TaggedSrc, kMemcpyOK> gSrcs;
349static SkTArray<TaggedSink, kMemcpyOK> gSinks;
mtklein748ca3b2015-01-15 10:56:12 -0800350
mtklein6393c062015-04-27 08:45:01 -0700351static bool in_shard() {
352 static int N = 0;
353 return N++ % FLAGS_shards == FLAGS_shard;
354}
355
mtklein3eed7dd2016-02-24 19:07:07 -0800356static void push_src(const char* tag, ImplicitString options, Src* s) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400357 std::unique_ptr<Src> src(s);
mtklein6393c062015-04-27 08:45:01 -0700358 if (in_shard() &&
mtklein3eed7dd2016-02-24 19:07:07 -0800359 FLAGS_src.contains(tag) &&
mtklein748ca3b2015-01-15 10:56:12 -0800360 !SkCommandLineFlags::ShouldSkip(FLAGS_match, src->name().c_str())) {
mtkleine0effd62015-07-29 06:37:28 -0700361 TaggedSrc& s = gSrcs.push_back();
mtklein18300a32016-03-16 13:53:35 -0700362 s.reset(src.release());
mtklein748ca3b2015-01-15 10:56:12 -0800363 s.tag = tag;
djsollen54416de2015-04-03 07:24:48 -0700364 s.options = options;
mtklein114c3cd2015-01-15 10:15:02 -0800365 }
mtklein748ca3b2015-01-15 10:56:12 -0800366}
mtklein114c3cd2015-01-15 10:15:02 -0800367
msarett9e707a02015-09-01 14:57:57 -0700368static void push_codec_src(Path path, CodecSrc::Mode mode, CodecSrc::DstColorType dstColorType,
scroggoc5560be2016-02-03 09:42:42 -0800369 SkAlphaType dstAlphaType, float scale) {
scroggoe4499842016-02-25 11:03:47 -0800370 if (FLAGS_simpleCodec) {
scroggo19b91532016-10-24 09:03:26 -0700371 const bool simple = CodecSrc::kCodec_Mode == mode || CodecSrc::kAnimated_Mode == mode;
372 if (!simple || dstColorType != CodecSrc::kGetFromCanvas_DstColorType || scale != 1.0f) {
scroggoe4499842016-02-25 11:03:47 -0800373 // Only decode in the simple case.
374 return;
scroggo19b91532016-10-24 09:03:26 -0700375 }
scroggoe4499842016-02-25 11:03:47 -0800376 }
msarett9e707a02015-09-01 14:57:57 -0700377 SkString folder;
378 switch (mode) {
379 case CodecSrc::kCodec_Mode:
380 folder.append("codec");
381 break;
msarettbb25b532016-01-13 09:31:39 -0800382 case CodecSrc::kCodecZeroInit_Mode:
383 folder.append("codec_zero_init");
384 break;
msarett9e707a02015-09-01 14:57:57 -0700385 case CodecSrc::kScanline_Mode:
386 folder.append("scanline");
387 break;
msarett9e707a02015-09-01 14:57:57 -0700388 case CodecSrc::kStripe_Mode:
389 folder.append("stripe");
390 break;
msarett91c22b22016-02-22 12:27:46 -0800391 case CodecSrc::kCroppedScanline_Mode:
392 folder.append("crop");
393 break;
msarett9e707a02015-09-01 14:57:57 -0700394 case CodecSrc::kSubset_Mode:
msarett36c37962015-09-02 13:20:52 -0700395 folder.append("codec_subset");
msarett9e707a02015-09-01 14:57:57 -0700396 break;
scroggo19b91532016-10-24 09:03:26 -0700397 case CodecSrc::kAnimated_Mode:
398 folder.append("codec_animated");
399 break;
msarett9e707a02015-09-01 14:57:57 -0700400 }
401
402 switch (dstColorType) {
403 case CodecSrc::kGrayscale_Always_DstColorType:
404 folder.append("_kGray8");
405 break;
406 case CodecSrc::kIndex8_Always_DstColorType:
407 folder.append("_kIndex8");
408 break;
msarett34e0ec42016-04-22 16:27:24 -0700409 case CodecSrc::kNonNative8888_Always_DstColorType:
410 folder.append("_kNonNative");
411 break;
msarett9e707a02015-09-01 14:57:57 -0700412 default:
413 break;
414 }
415
scroggoc5560be2016-02-03 09:42:42 -0800416 switch (dstAlphaType) {
scroggoc5560be2016-02-03 09:42:42 -0800417 case kPremul_SkAlphaType:
418 folder.append("_premul");
419 break;
420 case kUnpremul_SkAlphaType:
421 folder.append("_unpremul");
422 break;
423 default:
424 break;
425 }
426
msarett9e707a02015-09-01 14:57:57 -0700427 if (1.0f != scale) {
428 folder.appendf("_%.3f", scale);
429 }
430
scroggoc5560be2016-02-03 09:42:42 -0800431 CodecSrc* src = new CodecSrc(path, mode, dstColorType, dstAlphaType, scale);
msarett9e707a02015-09-01 14:57:57 -0700432 push_src("image", folder, src);
433}
434
scroggof8dc9df2016-05-16 09:04:13 -0700435static void push_android_codec_src(Path path, CodecSrc::DstColorType dstColorType,
436 SkAlphaType dstAlphaType, int sampleSize) {
msarett3d9d7a72015-10-21 10:27:10 -0700437 SkString folder;
scroggof8dc9df2016-05-16 09:04:13 -0700438 folder.append("scaled_codec");
msarett3d9d7a72015-10-21 10:27:10 -0700439
440 switch (dstColorType) {
441 case CodecSrc::kGrayscale_Always_DstColorType:
442 folder.append("_kGray8");
443 break;
444 case CodecSrc::kIndex8_Always_DstColorType:
445 folder.append("_kIndex8");
446 break;
msarett34e0ec42016-04-22 16:27:24 -0700447 case CodecSrc::kNonNative8888_Always_DstColorType:
448 folder.append("_kNonNative");
449 break;
msarett3d9d7a72015-10-21 10:27:10 -0700450 default:
451 break;
452 }
453
scroggoc5560be2016-02-03 09:42:42 -0800454 switch (dstAlphaType) {
scroggoc5560be2016-02-03 09:42:42 -0800455 case kPremul_SkAlphaType:
456 folder.append("_premul");
457 break;
msarett9e9444c2016-02-03 12:39:10 -0800458 case kUnpremul_SkAlphaType:
459 folder.append("_unpremul");
460 break;
scroggoc5560be2016-02-03 09:42:42 -0800461 default:
462 break;
463 }
464
msarett3d9d7a72015-10-21 10:27:10 -0700465 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800466 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700467 }
468
scroggof8dc9df2016-05-16 09:04:13 -0700469 AndroidCodecSrc* src = new AndroidCodecSrc(path, dstColorType, dstAlphaType, sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700470 push_src("image", folder, src);
471}
472
msarett18976312016-03-09 14:20:58 -0800473static void push_image_gen_src(Path path, ImageGenSrc::Mode mode, SkAlphaType alphaType, bool isGpu)
474{
475 SkString folder;
476 switch (mode) {
477 case ImageGenSrc::kCodec_Mode:
478 folder.append("gen_codec");
479 break;
480 case ImageGenSrc::kPlatform_Mode:
481 folder.append("gen_platform");
482 break;
483 }
484
485 if (isGpu) {
486 folder.append("_gpu");
487 } else {
488 switch (alphaType) {
489 case kOpaque_SkAlphaType:
490 folder.append("_opaque");
491 break;
492 case kPremul_SkAlphaType:
493 folder.append("_premul");
494 break;
495 case kUnpremul_SkAlphaType:
496 folder.append("_unpremul");
497 break;
498 default:
499 break;
500 }
501 }
502
503 ImageGenSrc* src = new ImageGenSrc(path, mode, alphaType, isGpu);
504 push_src("image", folder, src);
505}
506
msarett438b2ad2015-04-09 12:43:10 -0700507static void push_codec_srcs(Path path) {
bungeman38d909e2016-08-02 14:40:46 -0700508 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
msarett438b2ad2015-04-09 12:43:10 -0700509 if (!encoded) {
mtkleinc41fd922016-03-08 09:01:39 -0800510 info("Couldn't read %s.", path.c_str());
msarett438b2ad2015-04-09 12:43:10 -0700511 return;
512 }
Ben Wagner145dbcd2016-11-03 14:40:50 -0400513 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(encoded));
halcanary96fcdcc2015-08-27 07:41:13 -0700514 if (nullptr == codec.get()) {
mtkleinc41fd922016-03-08 09:01:39 -0800515 info("Couldn't create codec for %s.", path.c_str());
msarett438b2ad2015-04-09 12:43:10 -0700516 return;
517 }
518
scroggo9366aff2016-05-20 08:38:54 -0700519 // native scaling is only supported by WEBP and JPEG
520 bool supportsNativeScaling = false;
msarett438b2ad2015-04-09 12:43:10 -0700521
msarett91c22b22016-02-22 12:27:46 -0800522 SkTArray<CodecSrc::Mode> nativeModes;
523 nativeModes.push_back(CodecSrc::kCodec_Mode);
524 nativeModes.push_back(CodecSrc::kCodecZeroInit_Mode);
msarett91c22b22016-02-22 12:27:46 -0800525 switch (codec->getEncodedFormat()) {
Hal Canarydb683012016-11-23 08:55:18 -0700526 case SkEncodedImageFormat::kJPEG:
msarett91c22b22016-02-22 12:27:46 -0800527 nativeModes.push_back(CodecSrc::kScanline_Mode);
528 nativeModes.push_back(CodecSrc::kStripe_Mode);
529 nativeModes.push_back(CodecSrc::kCroppedScanline_Mode);
scroggo9366aff2016-05-20 08:38:54 -0700530 supportsNativeScaling = true;
msarett91c22b22016-02-22 12:27:46 -0800531 break;
Hal Canarydb683012016-11-23 08:55:18 -0700532 case SkEncodedImageFormat::kWEBP:
msarett91c22b22016-02-22 12:27:46 -0800533 nativeModes.push_back(CodecSrc::kSubset_Mode);
scroggo9366aff2016-05-20 08:38:54 -0700534 supportsNativeScaling = true;
msarett91c22b22016-02-22 12:27:46 -0800535 break;
Hal Canarydb683012016-11-23 08:55:18 -0700536 case SkEncodedImageFormat::kDNG:
msarettb65e6042016-02-23 05:37:25 -0800537 break;
msarett91c22b22016-02-22 12:27:46 -0800538 default:
539 nativeModes.push_back(CodecSrc::kScanline_Mode);
msarett91c22b22016-02-22 12:27:46 -0800540 break;
541 }
msarett9e707a02015-09-01 14:57:57 -0700542
msarett91c22b22016-02-22 12:27:46 -0800543 SkTArray<CodecSrc::DstColorType> colorTypes;
544 colorTypes.push_back(CodecSrc::kGetFromCanvas_DstColorType);
msarett34e0ec42016-04-22 16:27:24 -0700545 colorTypes.push_back(CodecSrc::kNonNative8888_Always_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700546 switch (codec->getInfo().colorType()) {
547 case kGray_8_SkColorType:
msarett91c22b22016-02-22 12:27:46 -0800548 colorTypes.push_back(CodecSrc::kGrayscale_Always_DstColorType);
Hal Canarydb683012016-11-23 08:55:18 -0700549 if (SkEncodedImageFormat::kWBMP == codec->getEncodedFormat()) {
msarett91c22b22016-02-22 12:27:46 -0800550 colorTypes.push_back(CodecSrc::kIndex8_Always_DstColorType);
msarett55f7bdd2016-02-16 13:24:54 -0800551 }
msarett36c37962015-09-02 13:20:52 -0700552 break;
553 case kIndex_8_SkColorType:
msarett91c22b22016-02-22 12:27:46 -0800554 colorTypes.push_back(CodecSrc::kIndex8_Always_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700555 break;
556 default:
msarett36c37962015-09-02 13:20:52 -0700557 break;
558 }
msarett9e707a02015-09-01 14:57:57 -0700559
scroggoc5560be2016-02-03 09:42:42 -0800560 SkTArray<SkAlphaType> alphaModes;
561 alphaModes.push_back(kPremul_SkAlphaType);
scroggof2c96a22016-05-20 11:08:27 -0700562 if (codec->getInfo().alphaType() != kOpaque_SkAlphaType) {
563 alphaModes.push_back(kUnpremul_SkAlphaType);
scroggoc5560be2016-02-03 09:42:42 -0800564 }
msarettb714fb02016-01-22 14:46:42 -0800565
566 for (CodecSrc::Mode mode : nativeModes) {
scroggo9366aff2016-05-20 08:38:54 -0700567 for (CodecSrc::DstColorType colorType : colorTypes) {
568 for (SkAlphaType alphaType : alphaModes) {
scroggof2c96a22016-05-20 11:08:27 -0700569 // Only test kCroppedScanline_Mode when the alpha type is premul. The test is
scroggo9366aff2016-05-20 08:38:54 -0700570 // slow and won't be interestingly different with different alpha types.
571 if (CodecSrc::kCroppedScanline_Mode == mode &&
scroggof2c96a22016-05-20 11:08:27 -0700572 kPremul_SkAlphaType != alphaType) {
scroggo9366aff2016-05-20 08:38:54 -0700573 continue;
574 }
msarett91c22b22016-02-22 12:27:46 -0800575
scroggo9366aff2016-05-20 08:38:54 -0700576 push_codec_src(path, mode, colorType, alphaType, 1.0f);
msarettf3dc1882016-04-26 13:06:38 -0700577
scroggo9366aff2016-05-20 08:38:54 -0700578 // Skip kNonNative on different native scales. It won't be interestingly
579 // different.
580 if (supportsNativeScaling &&
581 CodecSrc::kNonNative8888_Always_DstColorType == colorType) {
582 // Native Scales
583 // SkJpegCodec natively supports scaling to the following:
584 for (auto scale : { 0.125f, 0.25f, 0.375f, 0.5f, 0.625f, 0.750f, 0.875f }) {
585 push_codec_src(path, mode, colorType, alphaType, scale);
586 }
scroggoc5560be2016-02-03 09:42:42 -0800587 }
msarett9e707a02015-09-01 14:57:57 -0700588 }
589 }
msarett0a242972015-06-11 14:27:27 -0700590 }
msarett36c37962015-09-02 13:20:52 -0700591
scroggo19b91532016-10-24 09:03:26 -0700592 {
593 std::vector<SkCodec::FrameInfo> frameInfos = codec->getFrameInfo();
594 if (frameInfos.size() > 1) {
595 push_codec_src(path, CodecSrc::kAnimated_Mode, CodecSrc::kGetFromCanvas_DstColorType,
596 kPremul_SkAlphaType, 1.0f);
597 }
598
599 }
600
scroggoe4499842016-02-25 11:03:47 -0800601 if (FLAGS_simpleCodec) {
602 return;
603 }
604
msarett3d9d7a72015-10-21 10:27:10 -0700605 const int sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
msarett36c37962015-09-02 13:20:52 -0700606
msarett3d9d7a72015-10-21 10:27:10 -0700607 for (int sampleSize : sampleSizes) {
msarett91c22b22016-02-22 12:27:46 -0800608 for (CodecSrc::DstColorType colorType : colorTypes) {
scroggoc5560be2016-02-03 09:42:42 -0800609 for (SkAlphaType alphaType : alphaModes) {
msarettf3dc1882016-04-26 13:06:38 -0700610 // We can exercise all of the kNonNative support code in the swizzler with just a
611 // few sample sizes. Skip the rest.
612 if (CodecSrc::kNonNative8888_Always_DstColorType == colorType && sampleSize > 3) {
613 continue;
614 }
615
scroggof8dc9df2016-05-16 09:04:13 -0700616 push_android_codec_src(path, colorType, alphaType, sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700617 }
msarett36c37962015-09-02 13:20:52 -0700618 }
619 }
msarett18976312016-03-09 14:20:58 -0800620
621 static const char* const rawExts[] = {
622 "arw", "cr2", "dng", "nef", "nrw", "orf", "raf", "rw2", "pef", "srw",
623 "ARW", "CR2", "DNG", "NEF", "NRW", "ORF", "RAF", "RW2", "PEF", "SRW",
624 };
625
626 // There is not currently a reason to test RAW images on image generator.
627 // If we want to enable these tests, we will need to fix skbug.com/5079.
628 for (const char* ext : rawExts) {
629 if (path.endsWith(ext)) {
630 return;
631 }
632 }
633
634 // Push image generator GPU test.
brianosmanc9ced392016-03-24 07:27:43 -0700635 push_image_gen_src(path, ImageGenSrc::kCodec_Mode, codec->getInfo().alphaType(), true);
msarett18976312016-03-09 14:20:58 -0800636
637 // Push image generator CPU tests.
638 for (SkAlphaType alphaType : alphaModes) {
639 push_image_gen_src(path, ImageGenSrc::kCodec_Mode, alphaType, false);
640
641#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
Hal Canarydb683012016-11-23 08:55:18 -0700642 if (SkEncodedImageFormat::kWEBP != codec->getEncodedFormat() &&
643 SkEncodedImageFormat::kWBMP != codec->getEncodedFormat() &&
msarett18976312016-03-09 14:20:58 -0800644 kUnpremul_SkAlphaType != alphaType)
645 {
646 push_image_gen_src(path, ImageGenSrc::kPlatform_Mode, alphaType, false);
647 }
msarettfc0b6d12016-03-17 13:50:17 -0700648#elif defined(SK_BUILD_FOR_WIN)
Hal Canarydb683012016-11-23 08:55:18 -0700649 if (SkEncodedImageFormat::kWEBP != codec->getEncodedFormat() &&
650 SkEncodedImageFormat::kWBMP != codec->getEncodedFormat())
msarettfc0b6d12016-03-17 13:50:17 -0700651 {
652 push_image_gen_src(path, ImageGenSrc::kPlatform_Mode, alphaType, false);
653 }
msarett18976312016-03-09 14:20:58 -0800654#endif
655 }
msarett438b2ad2015-04-09 12:43:10 -0700656}
657
msarettd1227a72016-05-18 06:23:57 -0700658static void push_brd_src(Path path, CodecSrc::DstColorType dstColorType, BRDSrc::Mode mode,
659 uint32_t sampleSize) {
660 SkString folder("brd_android_codec");
msaretta5783ae2015-09-08 15:35:32 -0700661 switch (mode) {
662 case BRDSrc::kFullImage_Mode:
663 break;
664 case BRDSrc::kDivisor_Mode:
665 folder.append("_divisor");
666 break;
667 default:
668 SkASSERT(false);
669 return;
670 }
671
672 switch (dstColorType) {
673 case CodecSrc::kGetFromCanvas_DstColorType:
674 break;
675 case CodecSrc::kIndex8_Always_DstColorType:
676 folder.append("_kIndex");
677 break;
678 case CodecSrc::kGrayscale_Always_DstColorType:
679 folder.append("_kGray");
680 break;
681 default:
682 SkASSERT(false);
683 return;
684 }
685
686 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800687 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700688 }
689
msarettd1227a72016-05-18 06:23:57 -0700690 BRDSrc* src = new BRDSrc(path, mode, dstColorType, sampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700691 push_src("image", folder, src);
692}
693
694static void push_brd_srcs(Path path) {
scroggo24a9bd72016-05-20 06:02:42 -0700695 // Only run Index8 and grayscale to one sampleSize and Mode. Though interesting
696 // to test these color types, they should not reveal anything across various
697 // sampleSizes and Modes
698 for (auto type : { CodecSrc::kIndex8_Always_DstColorType,
699 CodecSrc::kGrayscale_Always_DstColorType }) {
700 // Arbitrarily choose Mode and sampleSize.
701 push_brd_src(path, type, BRDSrc::kFullImage_Mode, 2);
702 }
703
704
scroggo501b7342015-11-03 07:55:11 -0800705 // Test on a variety of sampleSizes, making sure to include:
706 // - 2, 4, and 8, which are natively supported by jpeg
707 // - multiples of 2 which are not divisible by 4 (analogous for 4)
708 // - larger powers of two, since BRD clients generally use powers of 2
709 // We will only produce output for the larger sizes on large images.
710 const uint32_t sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 24, 32, 64 };
msarett6efbe052015-09-11 09:01:16 -0700711
msaretta5783ae2015-09-08 15:35:32 -0700712 const BRDSrc::Mode modes[] = {
msarett26ad17b2015-10-22 07:29:19 -0700713 BRDSrc::kFullImage_Mode,
714 BRDSrc::kDivisor_Mode,
msaretta5783ae2015-09-08 15:35:32 -0700715 };
716
msarettd1227a72016-05-18 06:23:57 -0700717 for (uint32_t sampleSize : sampleSizes) {
scroggo24a9bd72016-05-20 06:02:42 -0700718 for (BRDSrc::Mode mode : modes) {
719 push_brd_src(path, CodecSrc::kGetFromCanvas_DstColorType, mode, sampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700720 }
721 }
722}
723
724static bool brd_supported(const char* ext) {
msarett8c8f22a2015-04-01 06:58:48 -0700725 static const char* const exts[] = {
msaretta5783ae2015-09-08 15:35:32 -0700726 "jpg", "jpeg", "png", "webp",
727 "JPG", "JPEG", "PNG", "WEBP",
msarett8c8f22a2015-04-01 06:58:48 -0700728 };
729
730 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
731 if (0 == strcmp(exts[i], ext)) {
732 return true;
733 }
734 }
735 return false;
scroggo9b77ddd2015-03-19 06:03:39 -0700736}
737
fmalitaa2b9fdf2016-08-03 19:53:36 -0700738template <typename T>
739void gather_file_srcs(const SkCommandLineFlags::StringArray& flags, const char* ext) {
740 for (int i = 0; i < flags.count(); i++) {
741 const char* path = flags[i];
742 if (sk_isdir(path)) {
743 SkOSFile::Iter it(path, ext);
744 for (SkString file; it.next(&file); ) {
745 push_src(ext, "", new T(SkOSPath::Join(path, file.c_str())));
746 }
747 } else {
748 push_src(ext, "", new T(path));
749 }
750 }
751}
752
scroggo86737142016-02-03 12:19:11 -0800753static bool gather_srcs() {
mtklein748ca3b2015-01-15 10:56:12 -0800754 for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) {
djsollen54416de2015-04-03 07:24:48 -0700755 push_src("gm", "", new GMSrc(r->factory()));
mtklein748ca3b2015-01-15 10:56:12 -0800756 }
scroggo86737142016-02-03 12:19:11 -0800757
fmalitaa2b9fdf2016-08-03 19:53:36 -0700758 gather_file_srcs<SKPSrc>(FLAGS_skps, "skp");
759 gather_file_srcs<MSKPSrc>(FLAGS_mskps, "mskp");
760#if defined(SK_XML)
761 gather_file_srcs<SVGSrc>(FLAGS_svgs, "svg");
762#endif
halcanary45420a92016-06-02 12:41:14 -0700763
scroggo86737142016-02-03 12:19:11 -0800764 SkTArray<SkString> images;
msarett69deca82016-04-29 09:38:40 -0700765 if (!CollectImages(FLAGS_images, &images)) {
scroggo86737142016-02-03 12:19:11 -0800766 return false;
767 }
768
769 for (auto image : images) {
770 push_codec_srcs(image);
scroggoe4499842016-02-25 11:03:47 -0800771 if (FLAGS_simpleCodec) {
772 continue;
773 }
774
mtklein21eaf3b2016-02-08 12:39:59 -0800775 const char* ext = strrchr(image.c_str(), '.');
776 if (ext && brd_supported(ext+1)) {
scroggo86737142016-02-03 12:19:11 -0800777 push_brd_srcs(image);
mtklein709d2c32015-01-15 08:30:25 -0800778 }
mtklein709d2c32015-01-15 08:30:25 -0800779 }
scroggo86737142016-02-03 12:19:11 -0800780
msarett69deca82016-04-29 09:38:40 -0700781 SkTArray<SkString> colorImages;
782 if (!CollectImages(FLAGS_colorImages, &colorImages)) {
783 return false;
784 }
785
786 for (auto colorImage : colorImages) {
msarett9ce3a542016-07-15 13:54:38 -0700787 ColorCodecSrc* src = new ColorCodecSrc(colorImage, ColorCodecSrc::kBaseline_Mode,
788 kN32_SkColorType);
msarettb8d1aac2016-08-04 12:22:06 -0700789 push_src("colorImage", "color_codec_baseline", src);
msarett888dc162016-05-23 10:21:17 -0700790
msarett9ce3a542016-07-15 13:54:38 -0700791 src = new ColorCodecSrc(colorImage, ColorCodecSrc::kDst_HPZR30w_Mode, kN32_SkColorType);
msarettb8d1aac2016-08-04 12:22:06 -0700792 push_src("colorImage", "color_codec_HPZR30w", src);
msarett9ce3a542016-07-15 13:54:38 -0700793 // TODO (msarett):
794 // 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 -0700795
msarett9ce3a542016-07-15 13:54:38 -0700796 src = new ColorCodecSrc(colorImage, ColorCodecSrc::kDst_sRGB_Mode, kN32_SkColorType);
msarettb8d1aac2016-08-04 12:22:06 -0700797 push_src("colorImage", "color_codec_sRGB_kN32", src);
msarett9ce3a542016-07-15 13:54:38 -0700798 src = new ColorCodecSrc(colorImage, ColorCodecSrc::kDst_sRGB_Mode, kRGBA_F16_SkColorType);
msarettb8d1aac2016-08-04 12:22:06 -0700799 push_src("colorImage", "color_codec_sRGB_kF16", src);
msarett69deca82016-04-29 09:38:40 -0700800 }
801
scroggo86737142016-02-03 12:19:11 -0800802 return true;
mtklein709d2c32015-01-15 08:30:25 -0800803}
804
kkinnunen3e980c32015-12-23 01:33:00 -0800805static void push_sink(const SkCommandLineConfig& config, Sink* s) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400806 std::unique_ptr<Sink> sink(s);
kkinnunen3e980c32015-12-23 01:33:00 -0800807
mtkleine0effd62015-07-29 06:37:28 -0700808 // Try a simple Src as a canary. If it fails, skip this sink.
mtklein748ca3b2015-01-15 10:56:12 -0800809 struct : public Src {
mtkleine0effd62015-07-29 06:37:28 -0700810 Error draw(SkCanvas* c) const override {
811 c->drawRect(SkRect::MakeWH(1,1), SkPaint());
812 return "";
813 }
mtklein36352bf2015-03-25 18:17:31 -0700814 SkISize size() const override { return SkISize::Make(16, 16); }
mtkleine0effd62015-07-29 06:37:28 -0700815 Name name() const override { return "justOneRect"; }
816 } justOneRect;
mtklein114c3cd2015-01-15 10:15:02 -0800817
mtklein748ca3b2015-01-15 10:56:12 -0800818 SkBitmap bitmap;
819 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800820 SkString log;
mtkleine0effd62015-07-29 06:37:28 -0700821 Error err = sink->draw(justOneRect, &bitmap, &stream, &log);
mtklein4089ef72015-03-05 08:40:28 -0800822 if (err.isFatal()) {
mtkleinc41fd922016-03-08 09:01:39 -0800823 info("Could not run %s: %s\n", config.getTag().c_str(), err.c_str());
mtklein05641a52015-04-21 10:49:13 -0700824 exit(1);
mtklein114c3cd2015-01-15 10:15:02 -0800825 }
826
mtkleine0effd62015-07-29 06:37:28 -0700827 TaggedSink& ts = gSinks.push_back();
mtklein18300a32016-03-16 13:53:35 -0700828 ts.reset(sink.release());
kkinnunen3e980c32015-12-23 01:33:00 -0800829 ts.tag = config.getTag();
mtklein748ca3b2015-01-15 10:56:12 -0800830}
831
832static bool gpu_supported() {
833#if SK_SUPPORT_GPU
834 return FLAGS_gpu;
835#else
836 return false;
837#endif
838}
kkinnunen9ebc3f02015-12-21 23:48:13 -0800839
kkinnunen3e980c32015-12-23 01:33:00 -0800840static Sink* create_sink(const SkCommandLineConfig* config) {
841#if SK_SUPPORT_GPU
842 if (gpu_supported()) {
843 if (const SkCommandLineConfigGpu* gpuConfig = config->asConfigGpu()) {
bsalomon85b4b532016-04-05 11:06:27 -0700844 GrContextFactory::ContextType contextType = gpuConfig->getContextType();
csmartdalton6270e552016-09-13 10:41:49 -0700845 GrContextFactory::ContextOptions contextOptions = gpuConfig->getContextOptions();
kkinnunen3e980c32015-12-23 01:33:00 -0800846 GrContextFactory testFactory;
847 if (!testFactory.get(contextType, contextOptions)) {
mtkleinc41fd922016-03-08 09:01:39 -0800848 info("WARNING: can not create GPU context for config '%s'. "
849 "GM tests will be skipped.\n", gpuConfig->getTag().c_str());
kkinnunen3e980c32015-12-23 01:33:00 -0800850 return nullptr;
851 }
852 return new GPUSink(contextType, contextOptions, gpuConfig->getSamples(),
brianosmand93c1202016-03-10 07:49:08 -0800853 gpuConfig->getUseDIText(), gpuConfig->getColorType(),
brianosmanb109b8c2016-06-16 13:03:24 -0700854 sk_ref_sp(gpuConfig->getColorSpace()), FLAGS_gpu_threading);
kkinnunen3e980c32015-12-23 01:33:00 -0800855 }
856 }
857#endif
858
859#define SINK(t, sink, ...) if (config->getBackend().equals(t)) { return new sink(__VA_ARGS__); }
860
mtklein748ca3b2015-01-15 10:56:12 -0800861 if (FLAGS_cpu) {
Brian Osman526972e2016-10-24 09:24:02 -0400862 auto srgbColorSpace = SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named);
863 auto srgbLinearColorSpace = SkColorSpace::MakeNamed(SkColorSpace::kSRGBLinear_Named);
brianosmanb109b8c2016-06-16 13:03:24 -0700864
mtklein748ca3b2015-01-15 10:56:12 -0800865 SINK("565", RasterSink, kRGB_565_SkColorType);
866 SINK("8888", RasterSink, kN32_SkColorType);
brianosmanb109b8c2016-06-16 13:03:24 -0700867 SINK("srgb", RasterSink, kN32_SkColorType, srgbColorSpace);
raftias7c602de2016-10-13 10:45:44 -0700868 SINK("f16", RasterSink, kRGBA_F16_SkColorType, srgbLinearColorSpace);
halcanary94b87bd2016-04-04 06:12:15 -0700869 SINK("pdf", PDFSink);
mtklein9c3f17d2015-01-28 11:35:18 -0800870 SINK("skp", SKPSink);
reed54dc4872016-09-13 08:09:45 -0700871 SINK("pipe", PipeSink);
mtklein8a4527e2015-01-31 20:00:58 -0800872 SINK("svg", SVGSink);
873 SINK("null", NullSink);
halcanary47ef4d52015-03-03 09:13:09 -0800874 SINK("xps", XPSSink);
halcanary4b656662016-04-27 07:45:18 -0700875 SINK("pdfa", PDFSink, true);
Hal Canary85c7fe82016-10-25 10:33:27 -0400876 SINK("jsdebug", DebugSink);
mtklein748ca3b2015-01-15 10:56:12 -0800877 }
878#undef SINK
halcanary96fcdcc2015-08-27 07:41:13 -0700879 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800880}
881
kkinnunen3e980c32015-12-23 01:33:00 -0800882static Sink* create_via(const SkString& tag, Sink* wrapped) {
883#define VIA(t, via, ...) if (tag.equals(t)) { return new via(__VA_ARGS__); }
mtklein9c5052f2016-08-06 12:51:51 -0700884 VIA("lite", ViaLite, wrapped);
reed54dc4872016-09-13 08:09:45 -0700885 VIA("pipe", ViaPipe, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700886 VIA("twice", ViaTwice, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700887 VIA("serialize", ViaSerialization, wrapped);
mtklein4a34ecb2016-01-08 10:19:35 -0800888 VIA("pic", ViaPicture, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700889 VIA("2ndpic", ViaSecondPicture, wrapped);
mtkleind31c13d2015-05-05 12:59:56 -0700890 VIA("sp", ViaSingletonPictures, wrapped);
reedbabc3de2016-07-08 08:43:27 -0700891 VIA("defer", ViaDefer, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700892 VIA("tiles", ViaTiles, 256, 256, nullptr, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800893 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800894
mtkleind603b222015-02-17 11:13:33 -0800895 if (FLAGS_matrix.count() == 4) {
mtklein748ca3b2015-01-15 10:56:12 -0800896 SkMatrix m;
mtkleind603b222015-02-17 11:13:33 -0800897 m.reset();
898 m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
899 m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
900 m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
901 m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
902 VIA("matrix", ViaMatrix, m, wrapped);
903 VIA("upright", ViaUpright, m, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800904 }
tomhudson64de1e12015-03-05 08:01:07 -0800905
mtklein748ca3b2015-01-15 10:56:12 -0800906#undef VIA
halcanary96fcdcc2015-08-27 07:41:13 -0700907 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800908}
909
brianosman05c987c2016-05-09 13:42:16 -0700910static bool gather_sinks() {
kkinnunen3e980c32015-12-23 01:33:00 -0800911 SkCommandLineConfigArray configs;
912 ParseConfigs(FLAGS_config, &configs);
913 for (int i = 0; i < configs.count(); i++) {
914 const SkCommandLineConfig& config = *configs[i];
915 Sink* sink = create_sink(&config);
916 if (sink == nullptr) {
mtkleinc41fd922016-03-08 09:01:39 -0800917 info("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
918 config.getTag().c_str());
kkinnunen3e980c32015-12-23 01:33:00 -0800919 continue;
920 }
mtklein748ca3b2015-01-15 10:56:12 -0800921
kkinnunen3e980c32015-12-23 01:33:00 -0800922 const SkTArray<SkString>& parts = config.getViaParts();
923 for (int j = parts.count(); j-- > 0;) {
924 const SkString& part = parts[j];
925 Sink* next = create_via(part, sink);
halcanary96fcdcc2015-08-27 07:41:13 -0700926 if (next == nullptr) {
mtkleinc41fd922016-03-08 09:01:39 -0800927 info("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
928 part.c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800929 delete sink;
halcanary96fcdcc2015-08-27 07:41:13 -0700930 sink = nullptr;
mtklein748ca3b2015-01-15 10:56:12 -0800931 break;
932 }
933 sink = next;
934 }
935 if (sink) {
936 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800937 }
938 }
brianosman05c987c2016-05-09 13:42:16 -0700939
940 // If no configs were requested (just running tests, perhaps?), then we're okay.
941 // Otherwise, make sure that at least one sink was constructed correctly. This catches
942 // the case of bots without a GPU being assigned GPU configs.
943 return (configs.count() == 0) || (gSinks.count() > 0);
mtklein114c3cd2015-01-15 10:15:02 -0800944}
mtklein709d2c32015-01-15 08:30:25 -0800945
mtkleina5114d72015-08-24 13:27:01 -0700946static bool dump_png(SkBitmap bitmap, const char* path, const char* md5) {
947 const int w = bitmap.width(),
948 h = bitmap.height();
949
brianosman3c579dc2016-04-19 09:18:11 -0700950 sk_sp<SkData> encodedBitmap = sk_tools::encode_bitmap_for_png(bitmap);
951 if (encodedBitmap.get() == nullptr) {
952 return false;
mtkleina5114d72015-08-24 13:27:01 -0700953 }
brianosman3c579dc2016-04-19 09:18:11 -0700954 uint32_t* rgba = static_cast<uint32_t*>(encodedBitmap.get()->writable_data());
mtkleina5114d72015-08-24 13:27:01 -0700955
956 // We don't need bitmap anymore. Might as well drop our ref.
mtkleinfccb77d2015-08-24 14:13:29 -0700957 bitmap.reset();
mtkleina5114d72015-08-24 13:27:01 -0700958
mtkleinc64137c2015-08-25 10:56:08 -0700959 FILE* f = fopen(path, "wb");
mtkleina5114d72015-08-24 13:27:01 -0700960 if (!f) { return false; }
961
962 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
963 if (!png) {
964 fclose(f);
965 return false;
966 }
967
968 png_infop info = png_create_info_struct(png);
969 if (!info) {
970 png_destroy_write_struct(&png, &info);
971 fclose(f);
972 return false;
973 }
974
mtkleind69ece62015-09-10 10:37:44 -0700975 SkString description;
976 description.append("Key: ");
977 for (int i = 0; i < FLAGS_key.count(); i++) {
978 description.appendf("%s ", FLAGS_key[i]);
979 }
980 description.append("Properties: ");
981 for (int i = 0; i < FLAGS_properties.count(); i++) {
982 description.appendf("%s ", FLAGS_properties[i]);
983 }
984 description.appendf("MD5: %s", md5);
985
mtkleina5114d72015-08-24 13:27:01 -0700986 png_text text[2];
987 text[0].key = (png_charp)"Author";
988 text[0].text = (png_charp)"DM dump_png()";
989 text[0].compression = PNG_TEXT_COMPRESSION_NONE;
990 text[1].key = (png_charp)"Description";
mtkleind69ece62015-09-10 10:37:44 -0700991 text[1].text = (png_charp)description.c_str();
mtkleina5114d72015-08-24 13:27:01 -0700992 text[1].compression = PNG_TEXT_COMPRESSION_NONE;
993 png_set_text(png, info, text, 2);
994
995 png_init_io(png, f);
996 png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8,
997 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
998 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
999 png_write_info(png, info);
1000 for (int j = 0; j < h; j++) {
brianosman3c579dc2016-04-19 09:18:11 -07001001 png_bytep row = (png_bytep)(rgba + w*j);
mtkleina5114d72015-08-24 13:27:01 -07001002 png_write_rows(png, &row, 1);
1003 }
1004 png_write_end(png, info);
1005
1006 png_destroy_write_struct(&png, &info);
1007 fclose(f);
1008 return true;
1009}
1010
mtkleina2ef6422015-01-15 13:44:22 -08001011static bool match(const char* needle, const char* haystack) {
halcanary96fcdcc2015-08-27 07:41:13 -07001012 return 0 == strcmp("_", needle) || nullptr != strstr(haystack, needle);
mtkleina2ef6422015-01-15 13:44:22 -08001013}
1014
mtklein3eed7dd2016-02-24 19:07:07 -08001015static bool is_blacklisted(const char* sink, const char* src,
1016 const char* srcOptions, const char* name) {
mtklein0d243ff2015-04-03 07:51:00 -07001017 for (int i = 0; i < FLAGS_blacklist.count() - 3; i += 4) {
mtkleina2ef6422015-01-15 13:44:22 -08001018 if (match(FLAGS_blacklist[i+0], sink) &&
djsollen54416de2015-04-03 07:24:48 -07001019 match(FLAGS_blacklist[i+1], src) &&
1020 match(FLAGS_blacklist[i+2], srcOptions) &&
1021 match(FLAGS_blacklist[i+3], name)) {
mtklein3eed7dd2016-02-24 19:07:07 -08001022 return true;
mtkleina2ef6422015-01-15 13:44:22 -08001023 }
1024 }
mtklein3eed7dd2016-02-24 19:07:07 -08001025 return false;
mtkleina2ef6422015-01-15 13:44:22 -08001026}
1027
mtkleincd50bca2016-01-05 06:20:20 -08001028// Even when a Task Sink reports to be non-threadsafe (e.g. GPU), we know things like
1029// .png encoding are definitely thread safe. This lets us offload that work to CPU threads.
1030static SkTaskGroup gDefinitelyThreadSafeWork;
1031
mtklein748ca3b2015-01-15 10:56:12 -08001032// The finest-grained unit of work we can run: draw a single Src into a single Sink,
1033// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
1034struct Task {
mtkleine0effd62015-07-29 06:37:28 -07001035 Task(const TaggedSrc& src, const TaggedSink& sink) : src(src), sink(sink) {}
1036 const TaggedSrc& src;
1037 const TaggedSink& sink;
mtklein748ca3b2015-01-15 10:56:12 -08001038
mtklein21eaf3b2016-02-08 12:39:59 -08001039 static void Run(const Task& task) {
1040 SkString name = task.src->name();
mtkleine0effd62015-07-29 06:37:28 -07001041
mtkleinb9eb4ac2015-02-02 18:26:03 -08001042 SkString log;
mtklein3eed7dd2016-02-24 19:07:07 -08001043 if (!FLAGS_dryRun) {
mtklein748ca3b2015-01-15 10:56:12 -08001044 SkBitmap bitmap;
1045 SkDynamicMemoryWStream stream;
mtklein3eed7dd2016-02-24 19:07:07 -08001046 start(task.sink.tag.c_str(), task.src.tag.c_str(),
1047 task.src.options.c_str(), name.c_str());
mtklein21eaf3b2016-02-08 12:39:59 -08001048 Error err = task.sink->draw(*task.src, &bitmap, &stream, &log);
mtkleinb3b13b72016-03-07 13:20:52 -08001049 if (!log.isEmpty()) {
mtkleinc41fd922016-03-08 09:01:39 -08001050 info("%s %s %s %s:\n%s\n", task.sink.tag.c_str()
1051 , task.src.tag.c_str()
1052 , task.src.options.c_str()
1053 , name.c_str()
1054 , log.c_str());
mtkleinb3b13b72016-03-07 13:20:52 -08001055 }
mtklein748ca3b2015-01-15 10:56:12 -08001056 if (!err.isEmpty()) {
mtklein4089ef72015-03-05 08:40:28 -08001057 if (err.isFatal()) {
djsollen54416de2015-04-03 07:24:48 -07001058 fail(SkStringPrintf("%s %s %s %s: %s",
mtklein21eaf3b2016-02-08 12:39:59 -08001059 task.sink.tag.c_str(),
1060 task.src.tag.c_str(),
1061 task.src.options.c_str(),
mtklein4089ef72015-03-05 08:40:28 -08001062 name.c_str(),
1063 err.c_str()));
mtkleinb37cb412015-03-18 05:27:14 -07001064 } else {
mtklein3eed7dd2016-02-24 19:07:07 -08001065 done(task.sink.tag.c_str(), task.src.tag.c_str(),
1066 task.src.options.c_str(), name.c_str());
mtkleinba6ada72016-01-21 09:39:35 -08001067 return;
mtklein4089ef72015-03-05 08:40:28 -08001068 }
mtklein748ca3b2015-01-15 10:56:12 -08001069 }
mtklein62bd1a62015-01-27 14:46:26 -08001070
mtkleincd50bca2016-01-05 06:20:20 -08001071 // We're likely switching threads here, so we must capture by value, [=] or [foo,bar].
1072 SkStreamAsset* data = stream.detachAsStream();
1073 gDefinitelyThreadSafeWork.add([task,name,bitmap,data]{
Ben Wagner145dbcd2016-11-03 14:40:50 -04001074 std::unique_ptr<SkStreamAsset> ownedData(data);
mtkleincd50bca2016-01-05 06:20:20 -08001075
1076 // Why doesn't the copy constructor do this when we have pre-locked pixels?
1077 bitmap.lockPixels();
1078
1079 SkString md5;
1080 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
1081 SkMD5 hash;
1082 if (data->getLength()) {
1083 hash.writeStream(data, data->getLength());
1084 data->rewind();
mtklein38408462015-07-08 07:25:27 -07001085 } else {
mtkleincd50bca2016-01-05 06:20:20 -08001086 // If we're BGRA (Linux, Windows), swizzle over to RGBA (Mac, Android).
1087 // This helps eliminate multiple 0-pixel-diff hashes on gold.skia.org.
1088 // (Android's general slow speed breaks the tie arbitrarily in RGBA's favor.)
1089 // We might consider promoting 565 to RGBA too.
1090 if (bitmap.colorType() == kBGRA_8888_SkColorType) {
1091 SkBitmap swizzle;
1092 SkAssertResult(bitmap.copyTo(&swizzle, kRGBA_8888_SkColorType));
1093 hash.write(swizzle.getPixels(), swizzle.getSize());
1094 } else {
1095 hash.write(bitmap.getPixels(), bitmap.getSize());
1096 }
1097 }
1098 SkMD5::Digest digest;
1099 hash.finish(digest);
1100 for (int i = 0; i < 16; i++) {
1101 md5.appendf("%02x", digest.data[i]);
mtklein38408462015-07-08 07:25:27 -07001102 }
mtklein62bd1a62015-01-27 14:46:26 -08001103 }
mtklein62bd1a62015-01-27 14:46:26 -08001104
mtkleincd50bca2016-01-05 06:20:20 -08001105 if (!FLAGS_readPath.isEmpty() &&
mtklein3eed7dd2016-02-24 19:07:07 -08001106 !gGold.contains(Gold(task.sink.tag, task.src.tag,
1107 task.src.options, name, md5))) {
mtkleincd50bca2016-01-05 06:20:20 -08001108 fail(SkStringPrintf("%s not found for %s %s %s %s in %s",
1109 md5.c_str(),
mtklein21eaf3b2016-02-08 12:39:59 -08001110 task.sink.tag.c_str(),
1111 task.src.tag.c_str(),
1112 task.src.options.c_str(),
mtkleincd50bca2016-01-05 06:20:20 -08001113 name.c_str(),
1114 FLAGS_readPath[0]));
mtklein748ca3b2015-01-15 10:56:12 -08001115 }
mtkleincd50bca2016-01-05 06:20:20 -08001116
1117 if (!FLAGS_writePath.isEmpty()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001118 const char* ext = task.sink->fileExtension();
mtkleincd50bca2016-01-05 06:20:20 -08001119 if (data->getLength()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001120 WriteToDisk(task, md5, ext, data, data->getLength(), nullptr);
mtkleincd50bca2016-01-05 06:20:20 -08001121 SkASSERT(bitmap.drawsNothing());
1122 } else if (!bitmap.drawsNothing()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001123 WriteToDisk(task, md5, ext, nullptr, 0, &bitmap);
mtkleincd50bca2016-01-05 06:20:20 -08001124 }
1125 }
1126 });
mtklein748ca3b2015-01-15 10:56:12 -08001127 }
mtklein3eed7dd2016-02-24 19:07:07 -08001128 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 -08001129 }
1130
1131 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -08001132 SkString md5,
1133 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -08001134 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -08001135 const SkBitmap* bitmap) {
mtklein409d4702016-02-29 07:38:01 -08001136 bool gammaCorrect = false;
1137 if (bitmap) {
Brian Osman7039f742016-11-01 15:56:16 -04001138 gammaCorrect = SkToBool(bitmap->info().colorSpace());
mtklein409d4702016-02-29 07:38:01 -08001139 }
1140
mtklein748ca3b2015-01-15 10:56:12 -08001141 JsonWriter::BitmapResult result;
djsollen54416de2015-04-03 07:24:48 -07001142 result.name = task.src->name();
mtklein3eed7dd2016-02-24 19:07:07 -08001143 result.config = task.sink.tag;
djsollen54416de2015-04-03 07:24:48 -07001144 result.sourceType = task.src.tag;
1145 result.sourceOptions = task.src.options;
1146 result.ext = ext;
mtklein409d4702016-02-29 07:38:01 -08001147 result.gammaCorrect = gammaCorrect;
djsollen54416de2015-04-03 07:24:48 -07001148 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -08001149 JsonWriter::AddBitmapResult(result);
1150
mtkleinb0531a72015-04-07 13:38:48 -07001151 // If an MD5 is uninteresting, we want it noted in the JSON file,
1152 // but don't want to dump it out as a .png (or whatever ext is).
1153 if (gUninterestingHashes.contains(md5)) {
1154 return;
1155 }
1156
mtklein748ca3b2015-01-15 10:56:12 -08001157 const char* dir = FLAGS_writePath[0];
1158 if (0 == strcmp(dir, "@")) { // Needed for iOS.
1159 dir = FLAGS_resourcePath[0];
1160 }
1161 sk_mkdir(dir);
1162
1163 SkString path;
1164 if (FLAGS_nameByHash) {
1165 path = SkOSPath::Join(dir, result.md5.c_str());
1166 path.append(".");
1167 path.append(ext);
1168 if (sk_exists(path.c_str())) {
1169 return; // Content-addressed. If it exists already, we're done.
1170 }
1171 } else {
kkinnunen3e980c32015-12-23 01:33:00 -08001172 path = SkOSPath::Join(dir, task.sink.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001173 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001174 path = SkOSPath::Join(path.c_str(), task.src.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001175 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001176 if (strcmp(task.src.options.c_str(), "") != 0) {
1177 path = SkOSPath::Join(path.c_str(), task.src.options.c_str());
djsollen54416de2015-04-03 07:24:48 -07001178 sk_mkdir(path.c_str());
1179 }
mtklein748ca3b2015-01-15 10:56:12 -08001180 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
1181 path.append(".");
1182 path.append(ext);
1183 }
1184
mtklein748ca3b2015-01-15 10:56:12 -08001185 if (bitmap) {
mtkleina5114d72015-08-24 13:27:01 -07001186 if (!dump_png(*bitmap, path.c_str(), result.md5.c_str())) {
mtklein748ca3b2015-01-15 10:56:12 -08001187 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
1188 return;
1189 }
1190 } else {
mtkleina5114d72015-08-24 13:27:01 -07001191 SkFILEWStream file(path.c_str());
1192 if (!file.isValid()) {
1193 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
1194 return;
1195 }
mtklein748ca3b2015-01-15 10:56:12 -08001196 if (!file.writeStream(data, len)) {
1197 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
1198 return;
1199 }
1200 }
1201 }
1202};
1203
mtklein748ca3b2015-01-15 10:56:12 -08001204/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1205
1206// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
1207
mtklein21eaf3b2016-02-08 12:39:59 -08001208static SkTDArray<skiatest::Test> gParallelTests, gSerialTests;
mtklein748ca3b2015-01-15 10:56:12 -08001209
1210static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -08001211 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -08001212 return;
1213 }
mtklein6393c062015-04-27 08:45:01 -07001214 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r; r = r->next()) {
1215 if (!in_shard()) {
1216 continue;
1217 }
halcanary87f3ba42015-01-20 09:30:20 -08001218 // Despite its name, factory() is returning a reference to
1219 // link-time static const POD data.
1220 const skiatest::Test& test = r->factory();
1221 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -08001222 continue;
1223 }
halcanary87f3ba42015-01-20 09:30:20 -08001224 if (test.needsGpu && gpu_supported()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001225 (FLAGS_gpu_threading ? gParallelTests : gSerialTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -08001226 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein21eaf3b2016-02-08 12:39:59 -08001227 gParallelTests.push(test);
mtklein82d28432015-01-15 12:46:02 -08001228 }
mtklein748ca3b2015-01-15 10:56:12 -08001229 }
1230}
1231
mtklein21eaf3b2016-02-08 12:39:59 -08001232static void run_test(skiatest::Test test) {
halcanary87f3ba42015-01-20 09:30:20 -08001233 struct : public skiatest::Reporter {
mtklein36352bf2015-03-25 18:17:31 -07001234 void reportFailed(const skiatest::Failure& failure) override {
halcanary87f3ba42015-01-20 09:30:20 -08001235 fail(failure.toString());
1236 JsonWriter::AddTestFailure(failure);
1237 }
mtklein36352bf2015-03-25 18:17:31 -07001238 bool allowExtendedTest() const override {
halcanary87f3ba42015-01-20 09:30:20 -08001239 return FLAGS_pathOpsExtended;
1240 }
mtklein36352bf2015-03-25 18:17:31 -07001241 bool verbose() const override { return FLAGS_veryVerbose; }
halcanary87f3ba42015-01-20 09:30:20 -08001242 } reporter;
djsollen824996a2015-06-12 12:06:22 -07001243
mtklein3eed7dd2016-02-24 19:07:07 -08001244 if (!FLAGS_dryRun && !is_blacklisted("_", "tests", "_", test.name)) {
mtklein21eaf3b2016-02-08 12:39:59 -08001245 start("unit", "test", "", test.name);
mtklein55e88b22015-01-21 15:50:13 -08001246 GrContextFactory factory;
mtklein21eaf3b2016-02-08 12:39:59 -08001247 test.proc(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -08001248 }
mtklein3eed7dd2016-02-24 19:07:07 -08001249 done("unit", "test", "", test.name);
mtklein748ca3b2015-01-15 10:56:12 -08001250}
1251
1252/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1253
mtkleinafae30a2016-02-24 12:28:32 -08001254DEFINE_int32(status_sec, 15, "Print status this often (and if we crash).");
1255
1256SkThread* start_status_thread() {
1257 auto thread = new SkThread([] (void*) {
1258 for (;;) {
1259 print_status();
1260 #if defined(SK_BUILD_FOR_WIN)
1261 Sleep(FLAGS_status_sec * 1000);
1262 #else
1263 sleep(FLAGS_status_sec);
1264 #endif
mtklein2e1c47e2015-03-12 07:16:56 -07001265 }
mtkleinafae30a2016-02-24 12:28:32 -08001266 });
1267 thread->start();
1268 return thread;
mtkleinde6fc2b2015-03-12 06:28:54 -07001269}
1270
caryclark83ca6282015-06-10 09:31:09 -07001271#define PORTABLE_FONT_PREFIX "Toy Liberation "
1272
mbocee6a9912016-05-31 11:42:36 -07001273static sk_sp<SkTypeface> create_from_name(const char familyName[], SkFontStyle style) {
caryclark83ca6282015-06-10 09:31:09 -07001274 if (familyName && strlen(familyName) > sizeof(PORTABLE_FONT_PREFIX)
1275 && !strncmp(familyName, PORTABLE_FONT_PREFIX, sizeof(PORTABLE_FONT_PREFIX) - 1)) {
caryclark1818acb2015-07-24 12:09:25 -07001276 return sk_tool_utils::create_portable_typeface(familyName, style);
caryclark83ca6282015-06-10 09:31:09 -07001277 }
halcanary96fcdcc2015-08-27 07:41:13 -07001278 return nullptr;
caryclark83ca6282015-06-10 09:31:09 -07001279}
1280
1281#undef PORTABLE_FONT_PREFIX
1282
mbocee6a9912016-05-31 11:42:36 -07001283extern sk_sp<SkTypeface> (*gCreateTypefaceDelegate)(const char [], SkFontStyle );
caryclark83ca6282015-06-10 09:31:09 -07001284
jcgregorio3b27ade2014-11-13 08:06:40 -08001285int dm_main();
caryclark17f0b6d2014-07-22 10:15:34 -07001286int dm_main() {
bsalomon71de3532016-04-26 14:27:21 -07001287 setbuf(stdout, nullptr);
mtklein246ba3a2016-02-23 10:39:36 -08001288 setup_crash_handler();
mtkleinafae30a2016-02-24 12:28:32 -08001289
Yuqian Liba62b4a2016-12-14 13:46:53 -05001290 gSkUseAnalyticAA = FLAGS_analyticAA;
liyuqian38911a72016-10-04 11:23:22 -07001291
Yuqian Li550148b2017-01-13 10:13:13 -05001292 if (FLAGS_forceAnalyticAA) {
1293 gSkForceAnalyticAA = true;
1294 }
1295
mtkleina483da32016-03-17 12:53:36 -07001296 if (FLAGS_verbose) {
1297 gVLog = stderr;
1298 } else if (!FLAGS_writePath.isEmpty()) {
mtklein51c8cfc2016-03-11 12:59:09 -08001299 sk_mkdir(FLAGS_writePath[0]);
mtkleina1ce2162016-07-22 12:23:46 -07001300 gVLog = fopen(SkOSPath::Join(FLAGS_writePath[0], "verbose.log").c_str(), "w");
mtkleinc41fd922016-03-08 09:01:39 -08001301 }
1302
mtklein5286f022016-01-22 08:18:14 -08001303 JsonWriter::DumpJson(); // It's handy for the bots to assume this is ~never missing.
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +00001304 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -07001305 SkTaskGroup::Enabler enabled(FLAGS_threads);
caryclark83ca6282015-06-10 09:31:09 -07001306 gCreateTypefaceDelegate = &create_from_name;
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +00001307
halcanary7d571242016-02-24 17:59:16 -08001308 {
1309 SkString testResourcePath = GetResourcePath("color_wheel.png");
1310 SkFILEStream testResource(testResourcePath.c_str());
1311 if (!testResource.isValid()) {
mtkleinc41fd922016-03-08 09:01:39 -08001312 info("Some resources are missing. Do you need to set --resourcePath?\n");
halcanary7d571242016-02-24 17:59:16 -08001313 }
1314 }
mtklein62bd1a62015-01-27 14:46:26 -08001315 gather_gold();
borenet09ed4802015-04-03 14:15:33 -07001316 gather_uninteresting_hashes();
mtklein62bd1a62015-01-27 14:46:26 -08001317
scroggo86737142016-02-03 12:19:11 -08001318 if (!gather_srcs()) {
1319 return 1;
1320 }
brianosman05c987c2016-05-09 13:42:16 -07001321 if (!gather_sinks()) {
1322 return 1;
1323 }
mtklein748ca3b2015-01-15 10:56:12 -08001324 gather_tests();
mtklein21eaf3b2016-02-08 12:39:59 -08001325 gPending = gSrcs.count() * gSinks.count() + gParallelTests.count() + gSerialTests.count();
mtkleinc41fd922016-03-08 09:01:39 -08001326 info("%d srcs * %d sinks + %d tests == %d tasks",
1327 gSrcs.count(), gSinks.count(), gParallelTests.count() + gSerialTests.count(), gPending);
Ben Wagner145dbcd2016-11-03 14:40:50 -04001328 std::unique_ptr<SkThread> statusThread(start_status_thread());
mtklein748ca3b2015-01-15 10:56:12 -08001329
mtklein21eaf3b2016-02-08 12:39:59 -08001330 // Kick off as much parallel work as we can, making note of any serial work we'll need to do.
1331 SkTaskGroup parallel;
1332 SkTArray<Task> serial;
1333
1334 for (auto& sink : gSinks)
1335 for (auto& src : gSrcs) {
mtklein3eed7dd2016-02-24 19:07:07 -08001336 if (src->veto(sink->flags()) ||
1337 is_blacklisted(sink.tag.c_str(), src.tag.c_str(),
1338 src.options.c_str(), src->name().c_str())) {
mtkleinf10637f2016-06-10 13:56:35 -07001339 SkAutoMutexAcquire lock(gMutex);
mtklein3eed7dd2016-02-24 19:07:07 -08001340 gPending--;
1341 continue;
1342 }
1343
mtklein21eaf3b2016-02-08 12:39:59 -08001344 Task task(src, sink);
1345 if (src->serial() || sink->serial()) {
1346 serial.push_back(task);
1347 } else {
1348 parallel.add([task] { Task::Run(task); });
mtklein748ca3b2015-01-15 10:56:12 -08001349 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001350 }
mtklein21eaf3b2016-02-08 12:39:59 -08001351 for (auto test : gParallelTests) {
1352 parallel.add([test] { run_test(test); });
mtklein55e88b22015-01-21 15:50:13 -08001353 }
mtklein21eaf3b2016-02-08 12:39:59 -08001354
1355 // With the parallel work running, run serial tasks and tests here on main thread.
1356 for (auto task : serial) { Task::Run(task); }
1357 for (auto test : gSerialTests) { run_test(test); }
1358
1359 // Wait for any remaining parallel work to complete (including any spun off of serial tasks).
1360 parallel.wait();
mtkleincd50bca2016-01-05 06:20:20 -08001361 gDefinitelyThreadSafeWork.wait();
1362
mtkleinda884c42016-02-24 18:00:23 -08001363 // We'd better have run everything.
1364 SkASSERT(gPending == 0);
mtkleine027f172016-02-26 15:53:06 -08001365 // Make sure we've flushed all our results to disk.
1366 JsonWriter::DumpJson();
mtkleinda884c42016-02-24 18:00:23 -08001367
mtklein748ca3b2015-01-15 10:56:12 -08001368 // At this point we're back in single-threaded land.
caryclarkf53ce802015-06-15 06:48:30 -07001369 sk_tool_utils::release_portable_typefaces();
mtklein@google.comd36522d2013-10-16 13:02:15 +00001370
mtklein748ca3b2015-01-15 10:56:12 -08001371 if (gFailures.count() > 0) {
mtkleinc41fd922016-03-08 09:01:39 -08001372 info("Failures:\n");
mtklein748ca3b2015-01-15 10:56:12 -08001373 for (int i = 0; i < gFailures.count(); i++) {
mtkleinc41fd922016-03-08 09:01:39 -08001374 info("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001375 }
mtkleinc41fd922016-03-08 09:01:39 -08001376 info("%d failures\n", gFailures.count());
mtklein748ca3b2015-01-15 10:56:12 -08001377 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001378 }
mtkleinafae30a2016-02-24 12:28:32 -08001379
1380#ifdef SK_PDF_IMAGE_STATS
halcanary7a14b312015-10-01 07:28:13 -07001381 SkPDFImageDumpStats();
mtkleinafae30a2016-02-24 12:28:32 -08001382#endif // SK_PDF_IMAGE_STATS
1383
1384 print_status();
mtkleinc41fd922016-03-08 09:01:39 -08001385 info("Finished!\n");
mtklein748ca3b2015-01-15 10:56:12 -08001386 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +00001387}
jcgregorio3b27ade2014-11-13 08:06:40 -08001388
kkinnunen179a8f52015-11-20 13:32:24 -08001389// TODO: currently many GPU tests are declared outside SK_SUPPORT_GPU guards.
1390// Thus we export the empty RunWithGPUTestContexts when SK_SUPPORT_GPU=0.
1391namespace skiatest {
bsalomon758586c2016-04-06 14:02:39 -07001392
1393#if SK_SUPPORT_GPU
1394bool IsGLContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1395 return kOpenGL_GrBackend == GrContextFactory::ContextTypeBackend(type);
1396}
bsalomondc0fcd42016-04-11 14:21:33 -07001397bool IsVulkanContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1398 return kVulkan_GrBackend == GrContextFactory::ContextTypeBackend(type);
1399}
bsalomon758586c2016-04-06 14:02:39 -07001400bool IsRenderingGLContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1401 return IsGLContextType(type) && GrContextFactory::IsRenderingContext(type);
1402}
1403bool IsNullGLContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1404 return type == GrContextFactory::kNullGL_ContextType;
1405}
Brian Osman287f6512016-12-05 11:35:07 -05001406const char* ContextTypeName(GrContextFactory::ContextType contextType) {
1407 switch (contextType) {
1408 case GrContextFactory::kGL_ContextType:
1409 return "OpenGL";
1410 case GrContextFactory::kGLES_ContextType:
1411 return "OpenGLES";
1412 case GrContextFactory::kANGLE_D3D9_ES2_ContextType:
1413 return "ANGLE D3D9 ES2";
1414 case GrContextFactory::kANGLE_D3D11_ES2_ContextType:
1415 return "ANGLE D3D11 ES2";
1416 case GrContextFactory::kANGLE_D3D11_ES3_ContextType:
1417 return "ANGLE D3D11 ES3";
1418 case GrContextFactory::kANGLE_GL_ES2_ContextType:
1419 return "ANGLE GL ES2";
1420 case GrContextFactory::kANGLE_GL_ES3_ContextType:
1421 return "ANGLE GL ES3";
1422 case GrContextFactory::kCommandBuffer_ContextType:
1423 return "Command Buffer";
1424 case GrContextFactory::kMESA_ContextType:
1425 return "Mesa";
1426 case GrContextFactory::kNullGL_ContextType:
1427 return "Null GL";
1428 case GrContextFactory::kDebugGL_ContextType:
1429 return "Debug GL";
1430 case GrContextFactory::kVulkan_ContextType:
1431 return "Vulkan";
1432 }
1433 SkDEBUGFAIL("Unreachable");
1434 return "Unknown";
1435}
bsalomon758586c2016-04-06 14:02:39 -07001436#else
1437bool IsGLContextType(int) { return false; }
bsalomondc0fcd42016-04-11 14:21:33 -07001438bool IsVulkanContextType(int) { return false; }
bsalomon758586c2016-04-06 14:02:39 -07001439bool IsRenderingGLContextType(int) { return false; }
1440bool IsNullGLContextType(int) { return false; }
1441#endif
1442
1443void RunWithGPUTestContexts(GrContextTestFn* test, GrContextTypeFilterFn* contextTypeFilter,
bsalomonf2f1c172016-04-05 12:59:06 -07001444 Reporter* reporter, GrContextFactory* factory) {
kkinnunen179a8f52015-11-20 13:32:24 -08001445#if SK_SUPPORT_GPU
kkinnunen3e980c32015-12-23 01:33:00 -08001446
bsalomon758586c2016-04-06 14:02:39 -07001447 for (int typeInt = 0; typeInt < GrContextFactory::kContextTypeCnt; ++typeInt) {
1448 GrContextFactory::ContextType contextType = (GrContextFactory::ContextType) typeInt;
Brian Osmanaa2219c2016-12-05 10:12:17 -05001449 // Use "native" instead of explicitly trying OpenGL and OpenGL ES. Do not use GLES on
bsalomon758586c2016-04-06 14:02:39 -07001450 // desktop since tests do not account for not fixing http://skbug.com/2809
1451 if (contextType == GrContextFactory::kGL_ContextType ||
1452 contextType == GrContextFactory::kGLES_ContextType) {
1453 if (contextType != GrContextFactory::kNativeGL_ContextType) {
1454 continue;
1455 }
1456 }
Brian Osmanaa2219c2016-12-05 10:12:17 -05001457 ContextInfo ctxInfo = factory->getContextInfo(contextType);
1458 if (contextTypeFilter && !(*contextTypeFilter)(contextType)) {
1459 continue;
1460 }
Brian Osman287f6512016-12-05 11:35:07 -05001461 ReporterContext ctx(reporter, SkString(ContextTypeName(contextType)));
bsalomon8b7451a2016-05-11 06:33:06 -07001462 if (ctxInfo.grContext()) {
bsalomonf2f1c172016-04-05 12:59:06 -07001463 (*test)(reporter, ctxInfo);
kkinnunen5219fd92015-12-10 06:28:13 -08001464 }
bsalomonf2f1c172016-04-05 12:59:06 -07001465 ctxInfo = factory->getContextInfo(contextType,
csmartdalton6270e552016-09-13 10:41:49 -07001466 GrContextFactory::ContextOptions::kEnableNVPR);
bsalomon8b7451a2016-05-11 06:33:06 -07001467 if (ctxInfo.grContext()) {
bsalomonf2f1c172016-04-05 12:59:06 -07001468 (*test)(reporter, ctxInfo);
kkinnunen179a8f52015-11-20 13:32:24 -08001469 }
1470 }
1471#endif
1472}
kkinnunen179a8f52015-11-20 13:32:24 -08001473} // namespace skiatest
1474
borenet48087572015-04-02 12:16:36 -07001475#if !defined(SK_BUILD_FOR_IOS)
jcgregorio3b27ade2014-11-13 08:06:40 -08001476int main(int argc, char** argv) {
1477 SkCommandLineFlags::Parse(argc, argv);
1478 return dm_main();
1479}
1480#endif