blob: 9b3a42f48bd62ef85e6fb08463436aa8206ef4a6 [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
jcgregorio3b27ade2014-11-13 08:06:40 -08008#include "CrashHandler.h"
mtklein748ca3b2015-01-15 10:56:12 -08009#include "DMJsonWriter.h"
10#include "DMSrcSink.h"
tomhudsoneebc39a2015-02-23 12:18:05 -080011#include "DMSrcSinkAndroid.h"
mtklein748ca3b2015-01-15 10:56:12 -080012#include "ProcStats.h"
halcanary7d571242016-02-24 17:59:16 -080013#include "Resources.h"
mtklein748ca3b2015-01-15 10:56:12 -080014#include "SkBBHFactory.h"
mtklein62bd1a62015-01-27 14:46:26 -080015#include "SkChecksum.h"
scroggocc2feb12015-08-14 08:32:46 -070016#include "SkCodec.h"
mtklein27c3fdd2016-02-26 14:43:21 -080017#include "SkColorPriv.h"
caryclark17f0b6d2014-07-22 10:15:34 -070018#include "SkCommonFlags.h"
kkinnunen3e980c32015-12-23 01:33:00 -080019#include "SkCommonFlagsConfig.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"
mtklein748ca3b2015-01-15 10:56:12 -080023#include "SkMD5.h"
mtklein1b249332015-07-07 12:21:21 -070024#include "SkMutex.h"
mtklein1d0f1642014-09-08 08:05:18 -070025#include "SkOSFile.h"
mtklein27c3fdd2016-02-26 14:43:21 -080026#include "SkPM4fPriv.h"
mtklein246ba3a2016-02-23 10:39:36 -080027#include "SkSpinlock.h"
mtkleina82f5622015-02-20 12:30:19 -080028#include "SkTHash.h"
mtklein406654b2014-09-03 15:34:37 -070029#include "SkTaskGroup.h"
mtkleinde6fc2b2015-03-12 06:28:54 -070030#include "SkThreadUtils.h"
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +000031#include "Test.h"
mtklein748ca3b2015-01-15 10:56:12 -080032#include "Timer.h"
caryclark83ca6282015-06-10 09:31:09 -070033#include "sk_tool_utils.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +000034
halcanary7a14b312015-10-01 07:28:13 -070035#ifdef SK_PDF_IMAGE_STATS
36extern void SkPDFImageDumpStats();
37#endif
38
mtkleina5114d72015-08-24 13:27:01 -070039#include "png.h"
40
bungeman60e0fee2015-08-26 05:15:46 -070041#include <stdlib.h>
42
scroggo27a58342015-10-28 08:56:41 -070043#ifndef SK_BUILD_FOR_WIN32
44 #include <unistd.h>
45#endif
46
djsollen54416de2015-04-03 07:24:48 -070047DEFINE_string(src, "tests gm skp image", "Source types to test.");
mtklein748ca3b2015-01-15 10:56:12 -080048DEFINE_bool(nameByHash, false,
49 "If true, write to FLAGS_writePath[0]/<hash>.png instead of "
djsollen54416de2015-04-03 07:24:48 -070050 "to FLAGS_writePath[0]/<config>/<sourceType>/<sourceOptions>/<name>.png");
mtklein748ca3b2015-01-15 10:56:12 -080051DEFINE_bool2(pathOpsExtended, x, false, "Run extended pathOps tests.");
mtkleind603b222015-02-17 11:13:33 -080052DEFINE_string(matrix, "1 0 0 1",
53 "2x2 scale+skew matrix to apply or upright when using "
54 "'matrix' or 'upright' in config.");
mtklein82d28432015-01-15 12:46:02 -080055DEFINE_bool(gpu_threading, false, "Allow GPU work to run on multiple threads?");
mtklein@google.comd36522d2013-10-16 13:02:15 +000056
mtkleina2ef6422015-01-15 13:44:22 -080057DEFINE_string(blacklist, "",
djsollen54416de2015-04-03 07:24:48 -070058 "Space-separated config/src/srcOptions/name quadruples to blacklist. '_' matches anything. E.g. \n"
59 "'--blacklist gpu skp _ _' will blacklist all SKPs drawn into the gpu config.\n"
60 "'--blacklist gpu skp _ _ 8888 gm _ aarects' will also blacklist the aarects GM on 8888.");
mtkleina2ef6422015-01-15 13:44:22 -080061
mtklein62bd1a62015-01-27 14:46:26 -080062DEFINE_string2(readPath, r, "", "If set check for equality with golden results in this directory.");
63
borenet09ed4802015-04-03 14:15:33 -070064DEFINE_string(uninterestingHashesFile, "",
65 "File containing a list of uninteresting hashes. If a result hashes to something in "
66 "this list, no image is written for that result.");
67
mtklein6393c062015-04-27 08:45:01 -070068DEFINE_int32(shards, 1, "We're splitting source data into this many shards.");
69DEFINE_int32(shard, 0, "Which shard do I run?");
scroggoe4499842016-02-25 11:03:47 -080070DEFINE_bool(simpleCodec, false, "Only decode images to native scale");
mtklein6393c062015-04-27 08:45:01 -070071
mtklein748ca3b2015-01-15 10:56:12 -080072using namespace DM;
mtklein@google.comd36522d2013-10-16 13:02:15 +000073
mtklein748ca3b2015-01-15 10:56:12 -080074/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
75
mtkleinc41fd922016-03-08 09:01:39 -080076static const SkMSec kStartMs = SkTime::GetMSecs();
77
78static FILE* gVLog;
79
80template <typename... Args>
81static void vlog(const char* fmt, Args&&... args) {
82 if (gVLog) {
83 fprintf(gVLog, "%s\t", HumanizeMs(SkTime::GetMSecs() - kStartMs).c_str());
84 fprintf(gVLog, fmt, args...);
85 fflush(gVLog);
86 }
87}
88
89template <typename... Args>
90static void info(const char* fmt, Args&&... args) {
91 vlog(fmt, args...);
92 if (!FLAGS_quiet) {
93 printf(fmt, args...);
94 }
95}
96static void info(const char* fmt) {
97 if (!FLAGS_quiet) {
98 printf("%s", fmt); // Clang warns printf(fmt) is insecure.
99 }
100}
101
mtklein748ca3b2015-01-15 10:56:12 -0800102SK_DECLARE_STATIC_MUTEX(gFailuresMutex);
103static SkTArray<SkString> gFailures;
104
mtklein3eed7dd2016-02-24 19:07:07 -0800105static void fail(const SkString& err) {
mtklein748ca3b2015-01-15 10:56:12 -0800106 SkAutoMutexAcquire lock(gFailuresMutex);
107 SkDebugf("\n\nFAILURE: %s\n\n", err.c_str());
108 gFailures.push_back(err);
halcanary9e398f72015-01-10 11:18:04 -0800109}
110
mtkleinb37cb412015-03-18 05:27:14 -0700111
mtklein246ba3a2016-02-23 10:39:36 -0800112// We use a spinlock to make locking this in a signal handler _somewhat_ safe.
mtklein15923c92016-02-29 10:14:38 -0800113static SkSpinlock gMutex;
mtklein3eed7dd2016-02-24 19:07:07 -0800114static int32_t gPending;
115static SkTArray<SkString> gRunning;
mtklein748ca3b2015-01-15 10:56:12 -0800116
mtklein3eed7dd2016-02-24 19:07:07 -0800117static void done(const char* config, const char* src, const char* srcOptions, const char* name) {
118 SkString id = SkStringPrintf("%s %s %s %s", config, src, srcOptions, name);
mtkleinc41fd922016-03-08 09:01:39 -0800119 vlog("done %s\n", id.c_str());
mtkleinafae30a2016-02-24 12:28:32 -0800120 int pending;
mtkleinb37cb412015-03-18 05:27:14 -0700121 {
mtklein15923c92016-02-29 10:14:38 -0800122 SkAutoTAcquire<SkSpinlock> lock(gMutex);
mtkleinb37cb412015-03-18 05:27:14 -0700123 for (int i = 0; i < gRunning.count(); i++) {
124 if (gRunning[i] == id) {
125 gRunning.removeShuffle(i);
126 break;
127 }
128 }
mtkleinafae30a2016-02-24 12:28:32 -0800129 pending = --gPending;
reed50bc0512015-05-19 14:13:31 -0700130 }
mtkleina17241b2015-01-23 05:48:00 -0800131 // We write our dm.json file every once in a while in case we crash.
132 // Notice this also handles the final dm.json when pending == 0.
133 if (pending % 500 == 0) {
134 JsonWriter::DumpJson();
135 }
mtklein@google.comd36522d2013-10-16 13:02:15 +0000136}
137
mtklein3eed7dd2016-02-24 19:07:07 -0800138static void start(const char* config, const char* src, const char* srcOptions, const char* name) {
139 SkString id = SkStringPrintf("%s %s %s %s", config, src, srcOptions, name);
mtkleinc41fd922016-03-08 09:01:39 -0800140 vlog("start %s\n", id.c_str());
mtklein15923c92016-02-29 10:14:38 -0800141 SkAutoTAcquire<SkSpinlock> lock(gMutex);
mtkleinb37cb412015-03-18 05:27:14 -0700142 gRunning.push_back(id);
143}
144
mtkleinafae30a2016-02-24 12:28:32 -0800145static void print_status() {
mtkleinafae30a2016-02-24 12:28:32 -0800146 int curr = sk_tools::getCurrResidentSetSizeMB(),
147 peak = sk_tools::getMaxResidentSetSizeMB();
mtkleinc41fd922016-03-08 09:01:39 -0800148 SkString elapsed = HumanizeMs(SkTime::GetMSecs() - kStartMs);
mtkleinafae30a2016-02-24 12:28:32 -0800149
mtklein15923c92016-02-29 10:14:38 -0800150 SkAutoTAcquire<SkSpinlock> lock(gMutex);
mtkleinc41fd922016-03-08 09:01:39 -0800151 info("\n%s elapsed, %d active, %d queued, %dMB RAM, %dMB peak\n",
152 elapsed.c_str(), gRunning.count(), gPending - gRunning.count(), curr, peak);
mtkleinafae30a2016-02-24 12:28:32 -0800153 for (auto& task : gRunning) {
mtkleinc41fd922016-03-08 09:01:39 -0800154 info("\t%s\n", task.c_str());
mtkleinafae30a2016-02-24 12:28:32 -0800155 }
156}
157
mtkleinf8557292016-02-29 06:35:28 -0800158// Yo dawg, I heard you like signals so I caught a signal in your
159// signal handler so you can handle signals while you handle signals.
160// Let's not get into that situation. Only print if we're the first ones to get a crash signal.
161static std::atomic<bool> in_signal_handler{false};
162
mtklein246ba3a2016-02-23 10:39:36 -0800163#if defined(SK_BUILD_FOR_WIN32)
mtkleinf8557292016-02-29 06:35:28 -0800164 static LONG WINAPI handler(EXCEPTION_POINTERS* e) {
165 static const struct {
166 const char* name;
robertphillips75467862016-03-01 14:10:23 -0800167 DWORD code;
mtkleinf8557292016-02-29 06:35:28 -0800168 } kExceptions[] = {
169 #define _(E) {#E, E}
170 _(EXCEPTION_ACCESS_VIOLATION),
171 _(EXCEPTION_BREAKPOINT),
172 _(EXCEPTION_INT_DIVIDE_BY_ZERO),
173 _(EXCEPTION_STACK_OVERFLOW),
174 // TODO: more?
175 #undef _
176 };
177
178 if (!in_signal_handler.exchange(true)) {
179 const DWORD code = e->ExceptionRecord->ExceptionCode;
mtkleinc41fd922016-03-08 09:01:39 -0800180 info("\nCaught exception %u", code);
mtkleinf8557292016-02-29 06:35:28 -0800181 for (const auto& exception : kExceptions) {
182 if (exception.code == code) {
mtkleinc41fd922016-03-08 09:01:39 -0800183 info(" %s", exception.name);
mtkleinf8557292016-02-29 06:35:28 -0800184 }
185 }
mtkleinc41fd922016-03-08 09:01:39 -0800186 info("\n");
mtkleinf8557292016-02-29 06:35:28 -0800187 print_status();
mtklein13bf3392016-03-17 10:16:12 -0700188 fflush(stdout);
mtkleinf8557292016-02-29 06:35:28 -0800189 }
190 // Execute default exception handler... hopefully, exit.
191 return EXCEPTION_EXECUTE_HANDLER;
mtklein246ba3a2016-02-23 10:39:36 -0800192 }
mtkleinf8557292016-02-29 06:35:28 -0800193 static void setup_crash_handler() { SetUnhandledExceptionFilter(handler); }
mtklein246ba3a2016-02-23 10:39:36 -0800194
mtklein819ab102016-02-29 17:02:52 -0800195#elif !defined(SK_BUILD_FOR_ANDROID)
196 #include <execinfo.h>
mtklein246ba3a2016-02-23 10:39:36 -0800197 #include <signal.h>
mtklein819ab102016-02-29 17:02:52 -0800198 #include <stdlib.h>
mtkleinf8557292016-02-29 06:35:28 -0800199
mtklein246ba3a2016-02-23 10:39:36 -0800200 static void setup_crash_handler() {
201 const int kSignals[] = { SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGSEGV };
202 for (int sig : kSignals) {
203 signal(sig, [](int sig) {
mtkleinf8557292016-02-29 06:35:28 -0800204 if (!in_signal_handler.exchange(true)) {
mtklein819ab102016-02-29 17:02:52 -0800205 SkAutoTAcquire<SkSpinlock> lock(gMutex);
mtkleinc41fd922016-03-08 09:01:39 -0800206 info("\nCaught signal %d [%s], was running:\n", sig, strsignal(sig));
mtklein819ab102016-02-29 17:02:52 -0800207 for (auto& task : gRunning) {
mtkleinc41fd922016-03-08 09:01:39 -0800208 info("\t%s\n", task.c_str());
mtklein819ab102016-02-29 17:02:52 -0800209 }
210
211 void* stack[64];
212 int count = backtrace(stack, SK_ARRAY_COUNT(stack));
213 char** symbols = backtrace_symbols(stack, count);
mtkleinc41fd922016-03-08 09:01:39 -0800214 info("\nStack trace:\n");
mtklein819ab102016-02-29 17:02:52 -0800215 for (int i = 0; i < count; i++) {
mtkleinc41fd922016-03-08 09:01:39 -0800216 info(" %s\n", symbols[i]);
mtklein819ab102016-02-29 17:02:52 -0800217 }
mtklein13bf3392016-03-17 10:16:12 -0700218 fflush(stdout);
mtkleinf8557292016-02-29 06:35:28 -0800219 }
mtklein819ab102016-02-29 17:02:52 -0800220 _Exit(sig);
mtklein246ba3a2016-02-23 10:39:36 -0800221 });
222 }
223 }
mtklein819ab102016-02-29 17:02:52 -0800224
225#else // Android
226 static void setup_crash_handler() {}
mtklein246ba3a2016-02-23 10:39:36 -0800227#endif
228
mtklein748ca3b2015-01-15 10:56:12 -0800229/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
mtklein114c3cd2015-01-15 10:15:02 -0800230
mtklein62bd1a62015-01-27 14:46:26 -0800231struct Gold : public SkString {
mtkleina82f5622015-02-20 12:30:19 -0800232 Gold() : SkString("") {}
mtklein3eed7dd2016-02-24 19:07:07 -0800233 Gold(const SkString& sink, const SkString& src,
234 const SkString& srcOptions, const SkString& name,
235 const SkString& md5)
mtklein62bd1a62015-01-27 14:46:26 -0800236 : SkString("") {
237 this->append(sink);
238 this->append(src);
djsollen54416de2015-04-03 07:24:48 -0700239 this->append(srcOptions);
mtklein62bd1a62015-01-27 14:46:26 -0800240 this->append(name);
241 this->append(md5);
mtklein62bd1a62015-01-27 14:46:26 -0800242 }
mtkleinc8d1dd42015-10-15 12:23:01 -0700243 struct Hash {
244 uint32_t operator()(const Gold& g) const {
245 return SkGoodHash()((const SkString&)g);
246 }
247 };
mtklein62bd1a62015-01-27 14:46:26 -0800248};
mtkleina82f5622015-02-20 12:30:19 -0800249static SkTHashSet<Gold, Gold::Hash> gGold;
mtklein62bd1a62015-01-27 14:46:26 -0800250
251static void add_gold(JsonWriter::BitmapResult r) {
djsollen54416de2015-04-03 07:24:48 -0700252 gGold.add(Gold(r.config, r.sourceType, r.sourceOptions, r.name, r.md5));
mtklein62bd1a62015-01-27 14:46:26 -0800253}
254
255static void gather_gold() {
256 if (!FLAGS_readPath.isEmpty()) {
257 SkString path(FLAGS_readPath[0]);
258 path.append("/dm.json");
259 if (!JsonWriter::ReadJson(path.c_str(), add_gold)) {
260 fail(SkStringPrintf("Couldn't read %s for golden results.", path.c_str()));
261 }
262 }
263}
264
265/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
266
mtklein935f1b12016-03-16 08:37:19 -0700267#if defined(SK_BUILD_FOR_WIN32)
268 static const char* kNewline = "\r\n";
269#else
270 static const char* kNewline = "\n";
271#endif
272
borenet09ed4802015-04-03 14:15:33 -0700273static SkTHashSet<SkString> gUninterestingHashes;
274
275static void gather_uninteresting_hashes() {
276 if (!FLAGS_uninterestingHashesFile.isEmpty()) {
277 SkAutoTUnref<SkData> data(SkData::NewFromFileName(FLAGS_uninterestingHashesFile[0]));
mtkleincc334b32015-09-22 11:43:53 -0700278 if (!data) {
mtkleinc41fd922016-03-08 09:01:39 -0800279 info("WARNING: unable to read uninteresting hashes from %s\n",
280 FLAGS_uninterestingHashesFile[0]);
mtkleincc334b32015-09-22 11:43:53 -0700281 return;
282 }
borenet09ed4802015-04-03 14:15:33 -0700283 SkTArray<SkString> hashes;
mtklein935f1b12016-03-16 08:37:19 -0700284 SkStrSplit((const char*)data->data(), kNewline, &hashes);
borenet09ed4802015-04-03 14:15:33 -0700285 for (const SkString& hash : hashes) {
286 gUninterestingHashes.add(hash);
287 }
mtkleinc41fd922016-03-08 09:01:39 -0800288 info("FYI: loaded %d distinct uninteresting hashes from %d lines\n",
289 gUninterestingHashes.count(), hashes.count());
borenet09ed4802015-04-03 14:15:33 -0700290 }
291}
292
293/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
294
mtkleine0effd62015-07-29 06:37:28 -0700295struct TaggedSrc : public SkAutoTDelete<Src> {
mtklein3eed7dd2016-02-24 19:07:07 -0800296 SkString tag;
297 SkString options;
mtkleine0effd62015-07-29 06:37:28 -0700298};
299
300struct TaggedSink : public SkAutoTDelete<Sink> {
kkinnunen3e980c32015-12-23 01:33:00 -0800301 SkString tag;
djsollen54416de2015-04-03 07:24:48 -0700302};
mtklein748ca3b2015-01-15 10:56:12 -0800303
304static const bool kMemcpyOK = true;
305
mtkleine0effd62015-07-29 06:37:28 -0700306static SkTArray<TaggedSrc, kMemcpyOK> gSrcs;
307static SkTArray<TaggedSink, kMemcpyOK> gSinks;
mtklein748ca3b2015-01-15 10:56:12 -0800308
mtklein6393c062015-04-27 08:45:01 -0700309static bool in_shard() {
310 static int N = 0;
311 return N++ % FLAGS_shards == FLAGS_shard;
312}
313
mtklein3eed7dd2016-02-24 19:07:07 -0800314static void push_src(const char* tag, ImplicitString options, Src* s) {
mtklein748ca3b2015-01-15 10:56:12 -0800315 SkAutoTDelete<Src> src(s);
mtklein6393c062015-04-27 08:45:01 -0700316 if (in_shard() &&
mtklein3eed7dd2016-02-24 19:07:07 -0800317 FLAGS_src.contains(tag) &&
mtklein748ca3b2015-01-15 10:56:12 -0800318 !SkCommandLineFlags::ShouldSkip(FLAGS_match, src->name().c_str())) {
mtkleine0effd62015-07-29 06:37:28 -0700319 TaggedSrc& s = gSrcs.push_back();
mtklein18300a32016-03-16 13:53:35 -0700320 s.reset(src.release());
mtklein748ca3b2015-01-15 10:56:12 -0800321 s.tag = tag;
djsollen54416de2015-04-03 07:24:48 -0700322 s.options = options;
mtklein114c3cd2015-01-15 10:15:02 -0800323 }
mtklein748ca3b2015-01-15 10:56:12 -0800324}
mtklein114c3cd2015-01-15 10:15:02 -0800325
msarett9e707a02015-09-01 14:57:57 -0700326static void push_codec_src(Path path, CodecSrc::Mode mode, CodecSrc::DstColorType dstColorType,
scroggoc5560be2016-02-03 09:42:42 -0800327 SkAlphaType dstAlphaType, float scale) {
scroggoe4499842016-02-25 11:03:47 -0800328 if (FLAGS_simpleCodec) {
329 if (mode != CodecSrc::kCodec_Mode || dstColorType != CodecSrc::kGetFromCanvas_DstColorType
330 || scale != 1.0f)
331 // Only decode in the simple case.
332 return;
333 }
msarett9e707a02015-09-01 14:57:57 -0700334 SkString folder;
335 switch (mode) {
336 case CodecSrc::kCodec_Mode:
337 folder.append("codec");
338 break;
msarettbb25b532016-01-13 09:31:39 -0800339 case CodecSrc::kCodecZeroInit_Mode:
340 folder.append("codec_zero_init");
341 break;
msarett9e707a02015-09-01 14:57:57 -0700342 case CodecSrc::kScanline_Mode:
343 folder.append("scanline");
344 break;
msarett9e707a02015-09-01 14:57:57 -0700345 case CodecSrc::kStripe_Mode:
346 folder.append("stripe");
347 break;
msarett91c22b22016-02-22 12:27:46 -0800348 case CodecSrc::kCroppedScanline_Mode:
349 folder.append("crop");
350 break;
msarett9e707a02015-09-01 14:57:57 -0700351 case CodecSrc::kSubset_Mode:
msarett36c37962015-09-02 13:20:52 -0700352 folder.append("codec_subset");
msarett9e707a02015-09-01 14:57:57 -0700353 break;
354 }
355
356 switch (dstColorType) {
357 case CodecSrc::kGrayscale_Always_DstColorType:
358 folder.append("_kGray8");
359 break;
360 case CodecSrc::kIndex8_Always_DstColorType:
361 folder.append("_kIndex8");
362 break;
363 default:
364 break;
365 }
366
scroggoc5560be2016-02-03 09:42:42 -0800367 switch (dstAlphaType) {
368 case kOpaque_SkAlphaType:
369 folder.append("_opaque");
370 break;
371 case kPremul_SkAlphaType:
372 folder.append("_premul");
373 break;
374 case kUnpremul_SkAlphaType:
375 folder.append("_unpremul");
376 break;
377 default:
378 break;
379 }
380
msarett9e707a02015-09-01 14:57:57 -0700381 if (1.0f != scale) {
382 folder.appendf("_%.3f", scale);
383 }
384
scroggoc5560be2016-02-03 09:42:42 -0800385 CodecSrc* src = new CodecSrc(path, mode, dstColorType, dstAlphaType, scale);
msarett9e707a02015-09-01 14:57:57 -0700386 push_src("image", folder, src);
387}
388
msarett3d9d7a72015-10-21 10:27:10 -0700389static void push_android_codec_src(Path path, AndroidCodecSrc::Mode mode,
scroggoc5560be2016-02-03 09:42:42 -0800390 CodecSrc::DstColorType dstColorType, SkAlphaType dstAlphaType, int sampleSize) {
msarett3d9d7a72015-10-21 10:27:10 -0700391 SkString folder;
392 switch (mode) {
393 case AndroidCodecSrc::kFullImage_Mode:
394 folder.append("scaled_codec");
395 break;
396 case AndroidCodecSrc::kDivisor_Mode:
397 folder.append("scaled_codec_divisor");
398 break;
399 }
400
401 switch (dstColorType) {
402 case CodecSrc::kGrayscale_Always_DstColorType:
403 folder.append("_kGray8");
404 break;
405 case CodecSrc::kIndex8_Always_DstColorType:
406 folder.append("_kIndex8");
407 break;
408 default:
409 break;
410 }
411
scroggoc5560be2016-02-03 09:42:42 -0800412 switch (dstAlphaType) {
413 case kOpaque_SkAlphaType:
414 folder.append("_opaque");
415 break;
416 case kPremul_SkAlphaType:
417 folder.append("_premul");
418 break;
msarett9e9444c2016-02-03 12:39:10 -0800419 case kUnpremul_SkAlphaType:
420 folder.append("_unpremul");
421 break;
scroggoc5560be2016-02-03 09:42:42 -0800422 default:
423 break;
424 }
425
msarett3d9d7a72015-10-21 10:27:10 -0700426 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800427 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700428 }
429
scroggoc5560be2016-02-03 09:42:42 -0800430 AndroidCodecSrc* src = new AndroidCodecSrc(path, mode, dstColorType, dstAlphaType, sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700431 push_src("image", folder, src);
432}
433
msarett18976312016-03-09 14:20:58 -0800434static void push_image_gen_src(Path path, ImageGenSrc::Mode mode, SkAlphaType alphaType, bool isGpu)
435{
436 SkString folder;
437 switch (mode) {
438 case ImageGenSrc::kCodec_Mode:
439 folder.append("gen_codec");
440 break;
441 case ImageGenSrc::kPlatform_Mode:
442 folder.append("gen_platform");
443 break;
444 }
445
446 if (isGpu) {
447 folder.append("_gpu");
448 } else {
449 switch (alphaType) {
450 case kOpaque_SkAlphaType:
451 folder.append("_opaque");
452 break;
453 case kPremul_SkAlphaType:
454 folder.append("_premul");
455 break;
456 case kUnpremul_SkAlphaType:
457 folder.append("_unpremul");
458 break;
459 default:
460 break;
461 }
462 }
463
464 ImageGenSrc* src = new ImageGenSrc(path, mode, alphaType, isGpu);
465 push_src("image", folder, src);
466}
467
msarett438b2ad2015-04-09 12:43:10 -0700468static void push_codec_srcs(Path path) {
469 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
470 if (!encoded) {
mtkleinc41fd922016-03-08 09:01:39 -0800471 info("Couldn't read %s.", path.c_str());
msarett438b2ad2015-04-09 12:43:10 -0700472 return;
473 }
474 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
halcanary96fcdcc2015-08-27 07:41:13 -0700475 if (nullptr == codec.get()) {
mtkleinc41fd922016-03-08 09:01:39 -0800476 info("Couldn't create codec for %s.", path.c_str());
msarett438b2ad2015-04-09 12:43:10 -0700477 return;
478 }
479
msarett36c37962015-09-02 13:20:52 -0700480 // Native Scales
emmaleer8f4ba762015-08-14 07:44:46 -0700481 // SkJpegCodec natively supports scaling to: 0.125, 0.25, 0.375, 0.5, 0.625, 0.75, 0.875
msarett36c37962015-09-02 13:20:52 -0700482 const float nativeScales[] = { 0.125f, 0.25f, 0.375f, 0.5f, 0.625f, 0.750f, 0.875f, 1.0f };
msarett438b2ad2015-04-09 12:43:10 -0700483
msarett91c22b22016-02-22 12:27:46 -0800484 SkTArray<CodecSrc::Mode> nativeModes;
485 nativeModes.push_back(CodecSrc::kCodec_Mode);
486 nativeModes.push_back(CodecSrc::kCodecZeroInit_Mode);
msarett91c22b22016-02-22 12:27:46 -0800487 switch (codec->getEncodedFormat()) {
488 case SkEncodedFormat::kJPEG_SkEncodedFormat:
489 nativeModes.push_back(CodecSrc::kScanline_Mode);
490 nativeModes.push_back(CodecSrc::kStripe_Mode);
491 nativeModes.push_back(CodecSrc::kCroppedScanline_Mode);
492 break;
493 case SkEncodedFormat::kWEBP_SkEncodedFormat:
494 nativeModes.push_back(CodecSrc::kSubset_Mode);
495 break;
yujieqin7a307df2016-03-11 07:32:33 -0800496 case SkEncodedFormat::kDNG_SkEncodedFormat:
msarettb65e6042016-02-23 05:37:25 -0800497 break;
msarett91c22b22016-02-22 12:27:46 -0800498 default:
499 nativeModes.push_back(CodecSrc::kScanline_Mode);
500 nativeModes.push_back(CodecSrc::kStripe_Mode);
501 break;
502 }
msarett9e707a02015-09-01 14:57:57 -0700503
msarett91c22b22016-02-22 12:27:46 -0800504 SkTArray<CodecSrc::DstColorType> colorTypes;
505 colorTypes.push_back(CodecSrc::kGetFromCanvas_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700506 switch (codec->getInfo().colorType()) {
507 case kGray_8_SkColorType:
msarett91c22b22016-02-22 12:27:46 -0800508 colorTypes.push_back(CodecSrc::kGrayscale_Always_DstColorType);
msarett55f7bdd2016-02-16 13:24:54 -0800509 if (kWBMP_SkEncodedFormat == codec->getEncodedFormat()) {
msarett91c22b22016-02-22 12:27:46 -0800510 colorTypes.push_back(CodecSrc::kIndex8_Always_DstColorType);
msarett55f7bdd2016-02-16 13:24:54 -0800511 }
msarett36c37962015-09-02 13:20:52 -0700512 break;
513 case kIndex_8_SkColorType:
msarett91c22b22016-02-22 12:27:46 -0800514 colorTypes.push_back(CodecSrc::kIndex8_Always_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700515 break;
516 default:
msarett36c37962015-09-02 13:20:52 -0700517 break;
518 }
msarett9e707a02015-09-01 14:57:57 -0700519
scroggoc5560be2016-02-03 09:42:42 -0800520 SkTArray<SkAlphaType> alphaModes;
521 alphaModes.push_back(kPremul_SkAlphaType);
msarett9e9444c2016-02-03 12:39:10 -0800522 alphaModes.push_back(kUnpremul_SkAlphaType);
scroggoc5560be2016-02-03 09:42:42 -0800523 if (codec->getInfo().alphaType() == kOpaque_SkAlphaType) {
524 alphaModes.push_back(kOpaque_SkAlphaType);
525 }
msarettb714fb02016-01-22 14:46:42 -0800526
527 for (CodecSrc::Mode mode : nativeModes) {
msarettb714fb02016-01-22 14:46:42 -0800528 for (float scale : nativeScales) {
msarett91c22b22016-02-22 12:27:46 -0800529 for (CodecSrc::DstColorType colorType : colorTypes) {
scroggoc5560be2016-02-03 09:42:42 -0800530 for (SkAlphaType alphaType : alphaModes) {
msarett91c22b22016-02-22 12:27:46 -0800531 // Only test kCroppedScanline_Mode when the alpha type is opaque. The test is
532 // slow and won't be interestingly different with different alpha types.
533 if (CodecSrc::kCroppedScanline_Mode == mode &&
534 kOpaque_SkAlphaType != alphaType) {
535 continue;
536 }
537
538 push_codec_src(path, mode, colorType, alphaType, scale);
scroggoc5560be2016-02-03 09:42:42 -0800539 }
msarett9e707a02015-09-01 14:57:57 -0700540 }
541 }
msarett0a242972015-06-11 14:27:27 -0700542 }
msarett36c37962015-09-02 13:20:52 -0700543
scroggoe4499842016-02-25 11:03:47 -0800544 if (FLAGS_simpleCodec) {
545 return;
546 }
547
halcanary6950de62015-11-07 05:29:00 -0800548 // https://bug.skia.org/4428
msarett33c76232015-11-16 13:43:40 -0800549 bool subset = false;
550 // The following image types are supported by BitmapRegionDecoder,
551 // so we will test full image decodes and subset decodes.
msarettbe8216a2015-12-04 08:00:50 -0800552 static const char* const exts[] = {
msarett3d9d7a72015-10-21 10:27:10 -0700553 "jpg", "jpeg", "png", "webp",
554 "JPG", "JPEG", "PNG", "WEBP",
555 };
msarettbe8216a2015-12-04 08:00:50 -0800556 for (const char* ext : exts) {
msarett3d9d7a72015-10-21 10:27:10 -0700557 if (path.endsWith(ext)) {
msarett33c76232015-11-16 13:43:40 -0800558 subset = true;
msarett3d9d7a72015-10-21 10:27:10 -0700559 break;
560 }
561 }
msarett33c76232015-11-16 13:43:40 -0800562
msarett3d9d7a72015-10-21 10:27:10 -0700563 const int sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
msarett36c37962015-09-02 13:20:52 -0700564
msarett3d9d7a72015-10-21 10:27:10 -0700565 for (int sampleSize : sampleSizes) {
msarett91c22b22016-02-22 12:27:46 -0800566 for (CodecSrc::DstColorType colorType : colorTypes) {
scroggoc5560be2016-02-03 09:42:42 -0800567 for (SkAlphaType alphaType : alphaModes) {
msarett91c22b22016-02-22 12:27:46 -0800568 push_android_codec_src(path, AndroidCodecSrc::kFullImage_Mode, colorType,
scroggoc5560be2016-02-03 09:42:42 -0800569 alphaType, sampleSize);
570 if (subset) {
msarett91c22b22016-02-22 12:27:46 -0800571 push_android_codec_src(path, AndroidCodecSrc::kDivisor_Mode, colorType,
scroggoc5560be2016-02-03 09:42:42 -0800572 alphaType, sampleSize);
573 }
msarett3d9d7a72015-10-21 10:27:10 -0700574 }
msarett36c37962015-09-02 13:20:52 -0700575 }
576 }
msarett18976312016-03-09 14:20:58 -0800577
578 static const char* const rawExts[] = {
579 "arw", "cr2", "dng", "nef", "nrw", "orf", "raf", "rw2", "pef", "srw",
580 "ARW", "CR2", "DNG", "NEF", "NRW", "ORF", "RAF", "RW2", "PEF", "SRW",
581 };
582
583 // There is not currently a reason to test RAW images on image generator.
584 // If we want to enable these tests, we will need to fix skbug.com/5079.
585 for (const char* ext : rawExts) {
586 if (path.endsWith(ext)) {
587 return;
588 }
589 }
590
591 // Push image generator GPU test.
592 // FIXME: The gpu backend does not draw kGray sources correctly. (skbug.com/4822)
593 if (kGray_8_SkColorType != codec->getInfo().colorType()) {
594 push_image_gen_src(path, ImageGenSrc::kCodec_Mode, codec->getInfo().alphaType(), true);
595 }
596
597 // Push image generator CPU tests.
598 for (SkAlphaType alphaType : alphaModes) {
599 push_image_gen_src(path, ImageGenSrc::kCodec_Mode, alphaType, false);
600
601#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
602 if (kWEBP_SkEncodedFormat != codec->getEncodedFormat() &&
603 kWBMP_SkEncodedFormat != codec->getEncodedFormat() &&
604 kUnpremul_SkAlphaType != alphaType)
605 {
606 push_image_gen_src(path, ImageGenSrc::kPlatform_Mode, alphaType, false);
607 }
msarettfc0b6d12016-03-17 13:50:17 -0700608#elif defined(SK_BUILD_FOR_WIN)
609 if (kWEBP_SkEncodedFormat != codec->getEncodedFormat() &&
610 kWBMP_SkEncodedFormat != codec->getEncodedFormat())
611 {
612 push_image_gen_src(path, ImageGenSrc::kPlatform_Mode, alphaType, false);
613 }
msarett18976312016-03-09 14:20:58 -0800614#endif
615 }
msarett438b2ad2015-04-09 12:43:10 -0700616}
617
msarett5cb48852015-11-06 08:56:32 -0800618static bool brd_color_type_supported(SkBitmapRegionDecoder::Strategy strategy,
msaretta5783ae2015-09-08 15:35:32 -0700619 CodecSrc::DstColorType dstColorType) {
620 switch (strategy) {
msarett5cb48852015-11-06 08:56:32 -0800621 case SkBitmapRegionDecoder::kCanvas_Strategy:
msaretta5783ae2015-09-08 15:35:32 -0700622 if (CodecSrc::kGetFromCanvas_DstColorType == dstColorType) {
623 return true;
624 }
625 return false;
msarett5cb48852015-11-06 08:56:32 -0800626 case SkBitmapRegionDecoder::kAndroidCodec_Strategy:
msarett26ad17b2015-10-22 07:29:19 -0700627 switch (dstColorType) {
628 case CodecSrc::kGetFromCanvas_DstColorType:
629 case CodecSrc::kIndex8_Always_DstColorType:
630 case CodecSrc::kGrayscale_Always_DstColorType:
631 return true;
632 default:
633 return false;
634 }
msaretta5783ae2015-09-08 15:35:32 -0700635 default:
636 SkASSERT(false);
637 return false;
638 }
639}
640
msarett5cb48852015-11-06 08:56:32 -0800641static void push_brd_src(Path path, SkBitmapRegionDecoder::Strategy strategy,
msaretta5783ae2015-09-08 15:35:32 -0700642 CodecSrc::DstColorType dstColorType, BRDSrc::Mode mode, uint32_t sampleSize) {
643 SkString folder;
644 switch (strategy) {
msarett5cb48852015-11-06 08:56:32 -0800645 case SkBitmapRegionDecoder::kCanvas_Strategy:
msaretta5783ae2015-09-08 15:35:32 -0700646 folder.append("brd_canvas");
647 break;
msarett5cb48852015-11-06 08:56:32 -0800648 case SkBitmapRegionDecoder::kAndroidCodec_Strategy:
msarett26ad17b2015-10-22 07:29:19 -0700649 folder.append("brd_android_codec");
650 break;
msaretta5783ae2015-09-08 15:35:32 -0700651 default:
652 SkASSERT(false);
653 return;
654 }
655
656 switch (mode) {
657 case BRDSrc::kFullImage_Mode:
658 break;
659 case BRDSrc::kDivisor_Mode:
660 folder.append("_divisor");
661 break;
662 default:
663 SkASSERT(false);
664 return;
665 }
666
667 switch (dstColorType) {
668 case CodecSrc::kGetFromCanvas_DstColorType:
669 break;
670 case CodecSrc::kIndex8_Always_DstColorType:
671 folder.append("_kIndex");
672 break;
673 case CodecSrc::kGrayscale_Always_DstColorType:
674 folder.append("_kGray");
675 break;
676 default:
677 SkASSERT(false);
678 return;
679 }
680
681 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800682 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700683 }
684
685 BRDSrc* src = new BRDSrc(path, strategy, mode, dstColorType, sampleSize);
686 push_src("image", folder, src);
687}
688
689static void push_brd_srcs(Path path) {
690
msarett5cb48852015-11-06 08:56:32 -0800691 const SkBitmapRegionDecoder::Strategy strategies[] = {
692 SkBitmapRegionDecoder::kCanvas_Strategy,
msarett5cb48852015-11-06 08:56:32 -0800693 SkBitmapRegionDecoder::kAndroidCodec_Strategy,
msaretta5783ae2015-09-08 15:35:32 -0700694 };
695
scroggo501b7342015-11-03 07:55:11 -0800696 // Test on a variety of sampleSizes, making sure to include:
697 // - 2, 4, and 8, which are natively supported by jpeg
698 // - multiples of 2 which are not divisible by 4 (analogous for 4)
699 // - larger powers of two, since BRD clients generally use powers of 2
700 // We will only produce output for the larger sizes on large images.
701 const uint32_t sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 24, 32, 64 };
msarett6efbe052015-09-11 09:01:16 -0700702
msaretta5783ae2015-09-08 15:35:32 -0700703 // We will only test to one backend (8888), but we will test all of the
704 // color types that we need to decode to on this backend.
705 const CodecSrc::DstColorType dstColorTypes[] = {
msarett26ad17b2015-10-22 07:29:19 -0700706 CodecSrc::kGetFromCanvas_DstColorType,
707 CodecSrc::kIndex8_Always_DstColorType,
708 CodecSrc::kGrayscale_Always_DstColorType,
msaretta5783ae2015-09-08 15:35:32 -0700709 };
710
711 const BRDSrc::Mode modes[] = {
msarett26ad17b2015-10-22 07:29:19 -0700712 BRDSrc::kFullImage_Mode,
713 BRDSrc::kDivisor_Mode,
msaretta5783ae2015-09-08 15:35:32 -0700714 };
715
msarett5cb48852015-11-06 08:56:32 -0800716 for (SkBitmapRegionDecoder::Strategy strategy : strategies) {
msarett6efbe052015-09-11 09:01:16 -0700717 for (uint32_t sampleSize : sampleSizes) {
msarett6efbe052015-09-11 09:01:16 -0700718 for (CodecSrc::DstColorType dstColorType : dstColorTypes) {
719 if (brd_color_type_supported(strategy, dstColorType)) {
720 for (BRDSrc::Mode mode : modes) {
msaretta5783ae2015-09-08 15:35:32 -0700721 push_brd_src(path, strategy, dstColorType, mode, sampleSize);
722 }
723 }
724 }
725 }
726 }
727}
728
729static bool brd_supported(const char* ext) {
msarett8c8f22a2015-04-01 06:58:48 -0700730 static const char* const exts[] = {
msaretta5783ae2015-09-08 15:35:32 -0700731 "jpg", "jpeg", "png", "webp",
732 "JPG", "JPEG", "PNG", "WEBP",
msarett8c8f22a2015-04-01 06:58:48 -0700733 };
734
735 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
736 if (0 == strcmp(exts[i], ext)) {
737 return true;
738 }
739 }
740 return false;
scroggo9b77ddd2015-03-19 06:03:39 -0700741}
742
scroggo86737142016-02-03 12:19:11 -0800743static bool gather_srcs() {
mtklein748ca3b2015-01-15 10:56:12 -0800744 for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) {
djsollen54416de2015-04-03 07:24:48 -0700745 push_src("gm", "", new GMSrc(r->factory()));
mtklein748ca3b2015-01-15 10:56:12 -0800746 }
halcanaryfc37ad12015-01-30 07:31:19 -0800747 for (int i = 0; i < FLAGS_skps.count(); i++) {
748 const char* path = FLAGS_skps[i];
749 if (sk_isdir(path)) {
750 SkOSFile::Iter it(path, "skp");
751 for (SkString file; it.next(&file); ) {
djsollen54416de2015-04-03 07:24:48 -0700752 push_src("skp", "", new SKPSrc(SkOSPath::Join(path, file.c_str())));
halcanaryfc37ad12015-01-30 07:31:19 -0800753 }
754 } else {
djsollen54416de2015-04-03 07:24:48 -0700755 push_src("skp", "", new SKPSrc(path));
mtklein114c3cd2015-01-15 10:15:02 -0800756 }
757 }
scroggo86737142016-02-03 12:19:11 -0800758
759 SkTArray<SkString> images;
760 if (!CollectImages(&images)) {
761 return false;
762 }
763
764 for (auto image : images) {
765 push_codec_srcs(image);
scroggoe4499842016-02-25 11:03:47 -0800766 if (FLAGS_simpleCodec) {
767 continue;
768 }
769
mtklein21eaf3b2016-02-08 12:39:59 -0800770 const char* ext = strrchr(image.c_str(), '.');
771 if (ext && brd_supported(ext+1)) {
scroggo86737142016-02-03 12:19:11 -0800772 push_brd_srcs(image);
mtklein709d2c32015-01-15 08:30:25 -0800773 }
mtklein709d2c32015-01-15 08:30:25 -0800774 }
scroggo86737142016-02-03 12:19:11 -0800775
776 return true;
mtklein709d2c32015-01-15 08:30:25 -0800777}
778
kkinnunen3e980c32015-12-23 01:33:00 -0800779static void push_sink(const SkCommandLineConfig& config, Sink* s) {
rmistry0f515bd2015-12-22 10:22:26 -0800780 SkAutoTDelete<Sink> sink(s);
kkinnunen3e980c32015-12-23 01:33:00 -0800781
mtkleine0effd62015-07-29 06:37:28 -0700782 // Try a simple Src as a canary. If it fails, skip this sink.
mtklein748ca3b2015-01-15 10:56:12 -0800783 struct : public Src {
mtkleine0effd62015-07-29 06:37:28 -0700784 Error draw(SkCanvas* c) const override {
785 c->drawRect(SkRect::MakeWH(1,1), SkPaint());
786 return "";
787 }
mtklein36352bf2015-03-25 18:17:31 -0700788 SkISize size() const override { return SkISize::Make(16, 16); }
mtkleine0effd62015-07-29 06:37:28 -0700789 Name name() const override { return "justOneRect"; }
790 } justOneRect;
mtklein114c3cd2015-01-15 10:15:02 -0800791
mtklein748ca3b2015-01-15 10:56:12 -0800792 SkBitmap bitmap;
793 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800794 SkString log;
mtkleine0effd62015-07-29 06:37:28 -0700795 Error err = sink->draw(justOneRect, &bitmap, &stream, &log);
mtklein4089ef72015-03-05 08:40:28 -0800796 if (err.isFatal()) {
mtkleinc41fd922016-03-08 09:01:39 -0800797 info("Could not run %s: %s\n", config.getTag().c_str(), err.c_str());
mtklein05641a52015-04-21 10:49:13 -0700798 exit(1);
mtklein114c3cd2015-01-15 10:15:02 -0800799 }
800
mtkleine0effd62015-07-29 06:37:28 -0700801 TaggedSink& ts = gSinks.push_back();
mtklein18300a32016-03-16 13:53:35 -0700802 ts.reset(sink.release());
kkinnunen3e980c32015-12-23 01:33:00 -0800803 ts.tag = config.getTag();
mtklein748ca3b2015-01-15 10:56:12 -0800804}
805
806static bool gpu_supported() {
807#if SK_SUPPORT_GPU
808 return FLAGS_gpu;
809#else
810 return false;
811#endif
812}
kkinnunen9ebc3f02015-12-21 23:48:13 -0800813
kkinnunen3e980c32015-12-23 01:33:00 -0800814static Sink* create_sink(const SkCommandLineConfig* config) {
815#if SK_SUPPORT_GPU
816 if (gpu_supported()) {
817 if (const SkCommandLineConfigGpu* gpuConfig = config->asConfigGpu()) {
818 GrContextFactory::GLContextType contextType = gpuConfig->getContextType();
819 GrContextFactory::GLContextOptions contextOptions =
820 GrContextFactory::kNone_GLContextOptions;
821 if (gpuConfig->getUseNVPR()) {
822 contextOptions = static_cast<GrContextFactory::GLContextOptions>(
823 contextOptions | GrContextFactory::kEnableNVPR_GLContextOptions);
824 }
825 GrContextFactory testFactory;
826 if (!testFactory.get(contextType, contextOptions)) {
mtkleinc41fd922016-03-08 09:01:39 -0800827 info("WARNING: can not create GPU context for config '%s'. "
828 "GM tests will be skipped.\n", gpuConfig->getTag().c_str());
kkinnunen3e980c32015-12-23 01:33:00 -0800829 return nullptr;
830 }
831 return new GPUSink(contextType, contextOptions, gpuConfig->getSamples(),
brianosmand93c1202016-03-10 07:49:08 -0800832 gpuConfig->getUseDIText(), gpuConfig->getColorType(),
833 gpuConfig->getProfileType(), FLAGS_gpu_threading);
kkinnunen3e980c32015-12-23 01:33:00 -0800834 }
835 }
836#endif
837
838#define SINK(t, sink, ...) if (config->getBackend().equals(t)) { return new sink(__VA_ARGS__); }
839
tomhudsoneebc39a2015-02-23 12:18:05 -0800840#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
841 SINK("hwui", HWUISink);
842#endif
843
mtklein748ca3b2015-01-15 10:56:12 -0800844 if (FLAGS_cpu) {
845 SINK("565", RasterSink, kRGB_565_SkColorType);
846 SINK("8888", RasterSink, kN32_SkColorType);
mtklein27c3fdd2016-02-26 14:43:21 -0800847 SINK("srgb", RasterSink, kN32_SkColorType, kSRGB_SkColorProfileType);
848 SINK("f16", RasterSink, kRGBA_F16_SkColorType);
halcanaryc11c62f2015-09-28 11:51:54 -0700849 SINK("pdf", PDFSink, "Pdfium");
850 SINK("pdf_poppler", PDFSink, "Poppler");
mtklein9c3f17d2015-01-28 11:35:18 -0800851 SINK("skp", SKPSink);
mtklein8a4527e2015-01-31 20:00:58 -0800852 SINK("svg", SVGSink);
853 SINK("null", NullSink);
halcanary47ef4d52015-03-03 09:13:09 -0800854 SINK("xps", XPSSink);
mtklein748ca3b2015-01-15 10:56:12 -0800855 }
856#undef SINK
halcanary96fcdcc2015-08-27 07:41:13 -0700857 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800858}
859
kkinnunen3e980c32015-12-23 01:33:00 -0800860static Sink* create_via(const SkString& tag, Sink* wrapped) {
861#define VIA(t, via, ...) if (tag.equals(t)) { return new via(__VA_ARGS__); }
halcanary96fcdcc2015-08-27 07:41:13 -0700862 VIA("twice", ViaTwice, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700863 VIA("serialize", ViaSerialization, wrapped);
mtklein4a34ecb2016-01-08 10:19:35 -0800864 VIA("pic", ViaPicture, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700865 VIA("2ndpic", ViaSecondPicture, wrapped);
mtkleind31c13d2015-05-05 12:59:56 -0700866 VIA("sp", ViaSingletonPictures, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700867 VIA("tiles", ViaTiles, 256, 256, nullptr, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800868 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
halcanary7a76f9c2016-02-03 11:53:18 -0800869 VIA("mojo", ViaMojo, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800870
mtkleind603b222015-02-17 11:13:33 -0800871 if (FLAGS_matrix.count() == 4) {
mtklein748ca3b2015-01-15 10:56:12 -0800872 SkMatrix m;
mtkleind603b222015-02-17 11:13:33 -0800873 m.reset();
874 m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
875 m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
876 m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
877 m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
878 VIA("matrix", ViaMatrix, m, wrapped);
879 VIA("upright", ViaUpright, m, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800880 }
tomhudson64de1e12015-03-05 08:01:07 -0800881
882#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
883 VIA("androidsdk", ViaAndroidSDK, wrapped);
884#endif
885
mtklein748ca3b2015-01-15 10:56:12 -0800886#undef VIA
halcanary96fcdcc2015-08-27 07:41:13 -0700887 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800888}
889
mtklein748ca3b2015-01-15 10:56:12 -0800890static void gather_sinks() {
kkinnunen3e980c32015-12-23 01:33:00 -0800891 SkCommandLineConfigArray configs;
892 ParseConfigs(FLAGS_config, &configs);
893 for (int i = 0; i < configs.count(); i++) {
894 const SkCommandLineConfig& config = *configs[i];
895 Sink* sink = create_sink(&config);
896 if (sink == nullptr) {
mtkleinc41fd922016-03-08 09:01:39 -0800897 info("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
898 config.getTag().c_str());
kkinnunen3e980c32015-12-23 01:33:00 -0800899 continue;
900 }
mtklein748ca3b2015-01-15 10:56:12 -0800901
kkinnunen3e980c32015-12-23 01:33:00 -0800902 const SkTArray<SkString>& parts = config.getViaParts();
903 for (int j = parts.count(); j-- > 0;) {
904 const SkString& part = parts[j];
905 Sink* next = create_via(part, sink);
halcanary96fcdcc2015-08-27 07:41:13 -0700906 if (next == nullptr) {
mtkleinc41fd922016-03-08 09:01:39 -0800907 info("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
908 part.c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800909 delete sink;
halcanary96fcdcc2015-08-27 07:41:13 -0700910 sink = nullptr;
mtklein748ca3b2015-01-15 10:56:12 -0800911 break;
912 }
913 sink = next;
914 }
915 if (sink) {
916 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800917 }
918 }
919}
mtklein709d2c32015-01-15 08:30:25 -0800920
mtkleina5114d72015-08-24 13:27:01 -0700921static bool dump_png(SkBitmap bitmap, const char* path, const char* md5) {
922 const int w = bitmap.width(),
923 h = bitmap.height();
mtklein27c3fdd2016-02-26 14:43:21 -0800924 // PNG wants unpremultiplied 8-bit RGBA pixels (16-bit could work fine too).
925 // We leave the gamma of these bytes unspecified, to continue the status quo,
926 // which we think generally is to interpret them as sRGB.
mtkleina5114d72015-08-24 13:27:01 -0700927
mtklein27c3fdd2016-02-26 14:43:21 -0800928 SkAutoTMalloc<uint32_t> rgba(w*h);
929
930 if (bitmap. colorType() == kN32_SkColorType &&
931 bitmap.profileType() == kSRGB_SkColorProfileType) {
932 // These are premul sRGB 8-bit pixels in SkPMColor order.
933 // We want unpremul sRGB 8-bit pixels in RGBA order. We'll get there via floats.
934 bitmap.lockPixels();
935 auto px = (const uint32_t*)bitmap.getPixels();
936 if (!px) {
mtkleina5114d72015-08-24 13:27:01 -0700937 return false;
938 }
mtklein27c3fdd2016-02-26 14:43:21 -0800939 for (int i = 0; i < w*h; i++) {
940 Sk4f fs = Sk4f_fromS32(px[i]); // Convert up to linear floats.
941 #if defined(SK_PMCOLOR_IS_BGRA)
942 fs = SkNx_shuffle<2,1,0,3>(fs); // Shuffle to RGBA, if not there already.
943 #endif
944 float invA = 1.0f / fs[3];
945 fs = fs * Sk4f(invA, invA, invA, 1); // Unpremultiply.
946 rgba[i] = Sk4f_toS32(fs); // Pack down to sRGB bytes.
947 }
mtkleina5114d72015-08-24 13:27:01 -0700948
mtklein27c3fdd2016-02-26 14:43:21 -0800949 } else if (bitmap.colorType() == kRGBA_F16_SkColorType) {
950 // These are premul linear half-float pixels in RGBA order.
951 // We want unpremul sRGB 8-bit pixels in RGBA order. We'll get there via floats.
952 bitmap.lockPixels();
953 auto px = (const uint64_t*)bitmap.getPixels();
954 if (!px) {
955 return false;
956 }
957 for (int i = 0; i < w*h; i++) {
958 Sk4f fs = SkHalfToFloat_01(px[i]); // Convert up to linear floats.
959 float invA = 1.0f / fs[3];
960 fs = fs * Sk4f(invA, invA, invA, 1); // Unpremultiply.
961 rgba[i] = Sk4f_toS32(fs); // Pack down to sRGB bytes.
962 }
963
964
965 } else {
966 // We "should" gamma correct in here but we don't.
967 // We want Gold to show exactly what our clients are seeing, broken gamma.
968
969 // Convert smaller formats up to premul linear 8-bit (in SkPMColor order).
970 if (bitmap.colorType() != kN32_SkColorType) {
971 SkBitmap n32;
972 if (!bitmap.copyTo(&n32, kN32_SkColorType)) {
973 return false;
974 }
975 bitmap = n32;
976 }
977
978 // Convert premul linear 8-bit to unpremul linear 8-bit RGBA.
979 if (!bitmap.readPixels(SkImageInfo::Make(w,h, kRGBA_8888_SkColorType,
980 kUnpremul_SkAlphaType),
981 rgba, 4*w, 0,0)) {
982 return false;
983 }
mtkleina5114d72015-08-24 13:27:01 -0700984 }
985
986 // We don't need bitmap anymore. Might as well drop our ref.
mtkleinfccb77d2015-08-24 14:13:29 -0700987 bitmap.reset();
mtkleina5114d72015-08-24 13:27:01 -0700988
mtkleinc64137c2015-08-25 10:56:08 -0700989 FILE* f = fopen(path, "wb");
mtkleina5114d72015-08-24 13:27:01 -0700990 if (!f) { return false; }
991
992 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
993 if (!png) {
994 fclose(f);
995 return false;
996 }
997
998 png_infop info = png_create_info_struct(png);
999 if (!info) {
1000 png_destroy_write_struct(&png, &info);
1001 fclose(f);
1002 return false;
1003 }
1004
mtkleind69ece62015-09-10 10:37:44 -07001005 SkString description;
1006 description.append("Key: ");
1007 for (int i = 0; i < FLAGS_key.count(); i++) {
1008 description.appendf("%s ", FLAGS_key[i]);
1009 }
1010 description.append("Properties: ");
1011 for (int i = 0; i < FLAGS_properties.count(); i++) {
1012 description.appendf("%s ", FLAGS_properties[i]);
1013 }
1014 description.appendf("MD5: %s", md5);
1015
mtkleina5114d72015-08-24 13:27:01 -07001016 png_text text[2];
1017 text[0].key = (png_charp)"Author";
1018 text[0].text = (png_charp)"DM dump_png()";
1019 text[0].compression = PNG_TEXT_COMPRESSION_NONE;
1020 text[1].key = (png_charp)"Description";
mtkleind69ece62015-09-10 10:37:44 -07001021 text[1].text = (png_charp)description.c_str();
mtkleina5114d72015-08-24 13:27:01 -07001022 text[1].compression = PNG_TEXT_COMPRESSION_NONE;
1023 png_set_text(png, info, text, 2);
1024
1025 png_init_io(png, f);
1026 png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8,
1027 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
1028 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
1029 png_write_info(png, info);
1030 for (int j = 0; j < h; j++) {
1031 png_bytep row = (png_bytep)(rgba.get() + w*j);
1032 png_write_rows(png, &row, 1);
1033 }
1034 png_write_end(png, info);
1035
1036 png_destroy_write_struct(&png, &info);
1037 fclose(f);
1038 return true;
1039}
1040
mtkleina2ef6422015-01-15 13:44:22 -08001041static bool match(const char* needle, const char* haystack) {
halcanary96fcdcc2015-08-27 07:41:13 -07001042 return 0 == strcmp("_", needle) || nullptr != strstr(haystack, needle);
mtkleina2ef6422015-01-15 13:44:22 -08001043}
1044
mtklein3eed7dd2016-02-24 19:07:07 -08001045static bool is_blacklisted(const char* sink, const char* src,
1046 const char* srcOptions, const char* name) {
mtklein0d243ff2015-04-03 07:51:00 -07001047 for (int i = 0; i < FLAGS_blacklist.count() - 3; i += 4) {
mtkleina2ef6422015-01-15 13:44:22 -08001048 if (match(FLAGS_blacklist[i+0], sink) &&
djsollen54416de2015-04-03 07:24:48 -07001049 match(FLAGS_blacklist[i+1], src) &&
1050 match(FLAGS_blacklist[i+2], srcOptions) &&
1051 match(FLAGS_blacklist[i+3], name)) {
mtklein3eed7dd2016-02-24 19:07:07 -08001052 return true;
mtkleina2ef6422015-01-15 13:44:22 -08001053 }
1054 }
mtklein3eed7dd2016-02-24 19:07:07 -08001055 return false;
mtkleina2ef6422015-01-15 13:44:22 -08001056}
1057
mtkleincd50bca2016-01-05 06:20:20 -08001058// Even when a Task Sink reports to be non-threadsafe (e.g. GPU), we know things like
1059// .png encoding are definitely thread safe. This lets us offload that work to CPU threads.
1060static SkTaskGroup gDefinitelyThreadSafeWork;
1061
mtklein748ca3b2015-01-15 10:56:12 -08001062// The finest-grained unit of work we can run: draw a single Src into a single Sink,
1063// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
1064struct Task {
mtkleine0effd62015-07-29 06:37:28 -07001065 Task(const TaggedSrc& src, const TaggedSink& sink) : src(src), sink(sink) {}
1066 const TaggedSrc& src;
1067 const TaggedSink& sink;
mtklein748ca3b2015-01-15 10:56:12 -08001068
mtklein21eaf3b2016-02-08 12:39:59 -08001069 static void Run(const Task& task) {
1070 SkString name = task.src->name();
mtkleine0effd62015-07-29 06:37:28 -07001071
mtkleinb9eb4ac2015-02-02 18:26:03 -08001072 SkString log;
mtklein3eed7dd2016-02-24 19:07:07 -08001073 if (!FLAGS_dryRun) {
mtklein748ca3b2015-01-15 10:56:12 -08001074 SkBitmap bitmap;
1075 SkDynamicMemoryWStream stream;
mtklein3eed7dd2016-02-24 19:07:07 -08001076 start(task.sink.tag.c_str(), task.src.tag.c_str(),
1077 task.src.options.c_str(), name.c_str());
mtklein21eaf3b2016-02-08 12:39:59 -08001078 Error err = task.sink->draw(*task.src, &bitmap, &stream, &log);
mtkleinb3b13b72016-03-07 13:20:52 -08001079 if (!log.isEmpty()) {
mtkleinc41fd922016-03-08 09:01:39 -08001080 info("%s %s %s %s:\n%s\n", task.sink.tag.c_str()
1081 , task.src.tag.c_str()
1082 , task.src.options.c_str()
1083 , name.c_str()
1084 , log.c_str());
mtkleinb3b13b72016-03-07 13:20:52 -08001085 }
mtklein748ca3b2015-01-15 10:56:12 -08001086 if (!err.isEmpty()) {
mtklein4089ef72015-03-05 08:40:28 -08001087 if (err.isFatal()) {
djsollen54416de2015-04-03 07:24:48 -07001088 fail(SkStringPrintf("%s %s %s %s: %s",
mtklein21eaf3b2016-02-08 12:39:59 -08001089 task.sink.tag.c_str(),
1090 task.src.tag.c_str(),
1091 task.src.options.c_str(),
mtklein4089ef72015-03-05 08:40:28 -08001092 name.c_str(),
1093 err.c_str()));
mtkleinb37cb412015-03-18 05:27:14 -07001094 } else {
mtklein3eed7dd2016-02-24 19:07:07 -08001095 done(task.sink.tag.c_str(), task.src.tag.c_str(),
1096 task.src.options.c_str(), name.c_str());
mtkleinba6ada72016-01-21 09:39:35 -08001097 return;
mtklein4089ef72015-03-05 08:40:28 -08001098 }
mtklein748ca3b2015-01-15 10:56:12 -08001099 }
mtklein62bd1a62015-01-27 14:46:26 -08001100
mtkleincd50bca2016-01-05 06:20:20 -08001101 // We're likely switching threads here, so we must capture by value, [=] or [foo,bar].
1102 SkStreamAsset* data = stream.detachAsStream();
1103 gDefinitelyThreadSafeWork.add([task,name,bitmap,data]{
1104 SkAutoTDelete<SkStreamAsset> ownedData(data);
1105
1106 // Why doesn't the copy constructor do this when we have pre-locked pixels?
1107 bitmap.lockPixels();
1108
1109 SkString md5;
1110 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
1111 SkMD5 hash;
1112 if (data->getLength()) {
1113 hash.writeStream(data, data->getLength());
1114 data->rewind();
mtklein38408462015-07-08 07:25:27 -07001115 } else {
mtkleincd50bca2016-01-05 06:20:20 -08001116 // If we're BGRA (Linux, Windows), swizzle over to RGBA (Mac, Android).
1117 // This helps eliminate multiple 0-pixel-diff hashes on gold.skia.org.
1118 // (Android's general slow speed breaks the tie arbitrarily in RGBA's favor.)
1119 // We might consider promoting 565 to RGBA too.
1120 if (bitmap.colorType() == kBGRA_8888_SkColorType) {
1121 SkBitmap swizzle;
1122 SkAssertResult(bitmap.copyTo(&swizzle, kRGBA_8888_SkColorType));
1123 hash.write(swizzle.getPixels(), swizzle.getSize());
1124 } else {
1125 hash.write(bitmap.getPixels(), bitmap.getSize());
1126 }
1127 }
1128 SkMD5::Digest digest;
1129 hash.finish(digest);
1130 for (int i = 0; i < 16; i++) {
1131 md5.appendf("%02x", digest.data[i]);
mtklein38408462015-07-08 07:25:27 -07001132 }
mtklein62bd1a62015-01-27 14:46:26 -08001133 }
mtklein62bd1a62015-01-27 14:46:26 -08001134
mtkleincd50bca2016-01-05 06:20:20 -08001135 if (!FLAGS_readPath.isEmpty() &&
mtklein3eed7dd2016-02-24 19:07:07 -08001136 !gGold.contains(Gold(task.sink.tag, task.src.tag,
1137 task.src.options, name, md5))) {
mtkleincd50bca2016-01-05 06:20:20 -08001138 fail(SkStringPrintf("%s not found for %s %s %s %s in %s",
1139 md5.c_str(),
mtklein21eaf3b2016-02-08 12:39:59 -08001140 task.sink.tag.c_str(),
1141 task.src.tag.c_str(),
1142 task.src.options.c_str(),
mtkleincd50bca2016-01-05 06:20:20 -08001143 name.c_str(),
1144 FLAGS_readPath[0]));
mtklein748ca3b2015-01-15 10:56:12 -08001145 }
mtkleincd50bca2016-01-05 06:20:20 -08001146
1147 if (!FLAGS_writePath.isEmpty()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001148 const char* ext = task.sink->fileExtension();
mtkleincd50bca2016-01-05 06:20:20 -08001149 if (data->getLength()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001150 WriteToDisk(task, md5, ext, data, data->getLength(), nullptr);
mtkleincd50bca2016-01-05 06:20:20 -08001151 SkASSERT(bitmap.drawsNothing());
1152 } else if (!bitmap.drawsNothing()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001153 WriteToDisk(task, md5, ext, nullptr, 0, &bitmap);
mtkleincd50bca2016-01-05 06:20:20 -08001154 }
1155 }
1156 });
mtklein748ca3b2015-01-15 10:56:12 -08001157 }
mtklein3eed7dd2016-02-24 19:07:07 -08001158 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 -08001159 }
1160
1161 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -08001162 SkString md5,
1163 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -08001164 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -08001165 const SkBitmap* bitmap) {
mtklein409d4702016-02-29 07:38:01 -08001166 bool gammaCorrect = false;
1167 if (bitmap) {
1168 gammaCorrect = bitmap->profileType() == kSRGB_SkColorProfileType
1169 || bitmap-> colorType() == kRGBA_F16_SkColorType;
1170 }
1171
mtklein748ca3b2015-01-15 10:56:12 -08001172 JsonWriter::BitmapResult result;
djsollen54416de2015-04-03 07:24:48 -07001173 result.name = task.src->name();
mtklein3eed7dd2016-02-24 19:07:07 -08001174 result.config = task.sink.tag;
djsollen54416de2015-04-03 07:24:48 -07001175 result.sourceType = task.src.tag;
1176 result.sourceOptions = task.src.options;
1177 result.ext = ext;
mtklein409d4702016-02-29 07:38:01 -08001178 result.gammaCorrect = gammaCorrect;
djsollen54416de2015-04-03 07:24:48 -07001179 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -08001180 JsonWriter::AddBitmapResult(result);
1181
mtkleinb0531a72015-04-07 13:38:48 -07001182 // If an MD5 is uninteresting, we want it noted in the JSON file,
1183 // but don't want to dump it out as a .png (or whatever ext is).
1184 if (gUninterestingHashes.contains(md5)) {
1185 return;
1186 }
1187
mtklein748ca3b2015-01-15 10:56:12 -08001188 const char* dir = FLAGS_writePath[0];
1189 if (0 == strcmp(dir, "@")) { // Needed for iOS.
1190 dir = FLAGS_resourcePath[0];
1191 }
1192 sk_mkdir(dir);
1193
1194 SkString path;
1195 if (FLAGS_nameByHash) {
1196 path = SkOSPath::Join(dir, result.md5.c_str());
1197 path.append(".");
1198 path.append(ext);
1199 if (sk_exists(path.c_str())) {
1200 return; // Content-addressed. If it exists already, we're done.
1201 }
1202 } else {
kkinnunen3e980c32015-12-23 01:33:00 -08001203 path = SkOSPath::Join(dir, task.sink.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001204 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001205 path = SkOSPath::Join(path.c_str(), task.src.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001206 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001207 if (strcmp(task.src.options.c_str(), "") != 0) {
1208 path = SkOSPath::Join(path.c_str(), task.src.options.c_str());
djsollen54416de2015-04-03 07:24:48 -07001209 sk_mkdir(path.c_str());
1210 }
mtklein748ca3b2015-01-15 10:56:12 -08001211 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
1212 path.append(".");
1213 path.append(ext);
1214 }
1215
mtklein748ca3b2015-01-15 10:56:12 -08001216 if (bitmap) {
mtkleina5114d72015-08-24 13:27:01 -07001217 if (!dump_png(*bitmap, path.c_str(), result.md5.c_str())) {
mtklein748ca3b2015-01-15 10:56:12 -08001218 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
1219 return;
1220 }
1221 } else {
mtkleina5114d72015-08-24 13:27:01 -07001222 SkFILEWStream file(path.c_str());
1223 if (!file.isValid()) {
1224 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
1225 return;
1226 }
mtklein748ca3b2015-01-15 10:56:12 -08001227 if (!file.writeStream(data, len)) {
1228 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
1229 return;
1230 }
1231 }
1232 }
1233};
1234
mtklein748ca3b2015-01-15 10:56:12 -08001235/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1236
1237// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
1238
mtklein21eaf3b2016-02-08 12:39:59 -08001239static SkTDArray<skiatest::Test> gParallelTests, gSerialTests;
mtklein748ca3b2015-01-15 10:56:12 -08001240
1241static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -08001242 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -08001243 return;
1244 }
mtklein6393c062015-04-27 08:45:01 -07001245 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r; r = r->next()) {
1246 if (!in_shard()) {
1247 continue;
1248 }
halcanary87f3ba42015-01-20 09:30:20 -08001249 // Despite its name, factory() is returning a reference to
1250 // link-time static const POD data.
1251 const skiatest::Test& test = r->factory();
1252 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -08001253 continue;
1254 }
halcanary87f3ba42015-01-20 09:30:20 -08001255 if (test.needsGpu && gpu_supported()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001256 (FLAGS_gpu_threading ? gParallelTests : gSerialTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -08001257 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein21eaf3b2016-02-08 12:39:59 -08001258 gParallelTests.push(test);
mtklein82d28432015-01-15 12:46:02 -08001259 }
mtklein748ca3b2015-01-15 10:56:12 -08001260 }
1261}
1262
mtklein21eaf3b2016-02-08 12:39:59 -08001263static void run_test(skiatest::Test test) {
halcanary87f3ba42015-01-20 09:30:20 -08001264 struct : public skiatest::Reporter {
mtklein36352bf2015-03-25 18:17:31 -07001265 void reportFailed(const skiatest::Failure& failure) override {
halcanary87f3ba42015-01-20 09:30:20 -08001266 fail(failure.toString());
1267 JsonWriter::AddTestFailure(failure);
1268 }
mtklein36352bf2015-03-25 18:17:31 -07001269 bool allowExtendedTest() const override {
halcanary87f3ba42015-01-20 09:30:20 -08001270 return FLAGS_pathOpsExtended;
1271 }
mtklein36352bf2015-03-25 18:17:31 -07001272 bool verbose() const override { return FLAGS_veryVerbose; }
halcanary87f3ba42015-01-20 09:30:20 -08001273 } reporter;
djsollen824996a2015-06-12 12:06:22 -07001274
mtklein3eed7dd2016-02-24 19:07:07 -08001275 if (!FLAGS_dryRun && !is_blacklisted("_", "tests", "_", test.name)) {
mtklein21eaf3b2016-02-08 12:39:59 -08001276 start("unit", "test", "", test.name);
mtklein55e88b22015-01-21 15:50:13 -08001277 GrContextFactory factory;
mtklein21eaf3b2016-02-08 12:39:59 -08001278 test.proc(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -08001279 }
mtklein3eed7dd2016-02-24 19:07:07 -08001280 done("unit", "test", "", test.name);
mtklein748ca3b2015-01-15 10:56:12 -08001281}
1282
1283/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1284
mtkleinafae30a2016-02-24 12:28:32 -08001285DEFINE_int32(status_sec, 15, "Print status this often (and if we crash).");
1286
1287SkThread* start_status_thread() {
1288 auto thread = new SkThread([] (void*) {
1289 for (;;) {
1290 print_status();
1291 #if defined(SK_BUILD_FOR_WIN)
1292 Sleep(FLAGS_status_sec * 1000);
1293 #else
1294 sleep(FLAGS_status_sec);
1295 #endif
mtklein2e1c47e2015-03-12 07:16:56 -07001296 }
mtkleinafae30a2016-02-24 12:28:32 -08001297 });
1298 thread->start();
1299 return thread;
mtkleinde6fc2b2015-03-12 06:28:54 -07001300}
1301
caryclark83ca6282015-06-10 09:31:09 -07001302#define PORTABLE_FONT_PREFIX "Toy Liberation "
1303
1304static SkTypeface* create_from_name(const char familyName[], SkTypeface::Style style) {
1305 if (familyName && strlen(familyName) > sizeof(PORTABLE_FONT_PREFIX)
1306 && !strncmp(familyName, PORTABLE_FONT_PREFIX, sizeof(PORTABLE_FONT_PREFIX) - 1)) {
caryclark1818acb2015-07-24 12:09:25 -07001307 return sk_tool_utils::create_portable_typeface(familyName, style);
caryclark83ca6282015-06-10 09:31:09 -07001308 }
halcanary96fcdcc2015-08-27 07:41:13 -07001309 return nullptr;
caryclark83ca6282015-06-10 09:31:09 -07001310}
1311
1312#undef PORTABLE_FONT_PREFIX
1313
1314extern SkTypeface* (*gCreateTypefaceDelegate)(const char [], SkTypeface::Style );
1315
jcgregorio3b27ade2014-11-13 08:06:40 -08001316int dm_main();
caryclark17f0b6d2014-07-22 10:15:34 -07001317int dm_main() {
mtklein246ba3a2016-02-23 10:39:36 -08001318 setup_crash_handler();
mtkleinafae30a2016-02-24 12:28:32 -08001319
mtkleina483da32016-03-17 12:53:36 -07001320 if (FLAGS_verbose) {
1321 gVLog = stderr;
1322 } else if (!FLAGS_writePath.isEmpty()) {
mtklein51c8cfc2016-03-11 12:59:09 -08001323 sk_mkdir(FLAGS_writePath[0]);
1324 gVLog = freopen(SkOSPath::Join(FLAGS_writePath[0], "verbose.log").c_str(), "w", stderr);
mtkleinc41fd922016-03-08 09:01:39 -08001325 }
1326
mtklein5286f022016-01-22 08:18:14 -08001327 JsonWriter::DumpJson(); // It's handy for the bots to assume this is ~never missing.
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +00001328 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -07001329 SkTaskGroup::Enabler enabled(FLAGS_threads);
caryclark83ca6282015-06-10 09:31:09 -07001330 gCreateTypefaceDelegate = &create_from_name;
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +00001331
halcanary7d571242016-02-24 17:59:16 -08001332 {
1333 SkString testResourcePath = GetResourcePath("color_wheel.png");
1334 SkFILEStream testResource(testResourcePath.c_str());
1335 if (!testResource.isValid()) {
mtkleinc41fd922016-03-08 09:01:39 -08001336 info("Some resources are missing. Do you need to set --resourcePath?\n");
halcanary7d571242016-02-24 17:59:16 -08001337 }
1338 }
mtklein62bd1a62015-01-27 14:46:26 -08001339 gather_gold();
borenet09ed4802015-04-03 14:15:33 -07001340 gather_uninteresting_hashes();
mtklein62bd1a62015-01-27 14:46:26 -08001341
scroggo86737142016-02-03 12:19:11 -08001342 if (!gather_srcs()) {
1343 return 1;
1344 }
mtklein748ca3b2015-01-15 10:56:12 -08001345 gather_sinks();
1346 gather_tests();
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +00001347
mtklein21eaf3b2016-02-08 12:39:59 -08001348 gPending = gSrcs.count() * gSinks.count() + gParallelTests.count() + gSerialTests.count();
mtkleinc41fd922016-03-08 09:01:39 -08001349 info("%d srcs * %d sinks + %d tests == %d tasks",
1350 gSrcs.count(), gSinks.count(), gParallelTests.count() + gSerialTests.count(), gPending);
mtkleinafae30a2016-02-24 12:28:32 -08001351 SkAutoTDelete<SkThread> statusThread(start_status_thread());
mtklein748ca3b2015-01-15 10:56:12 -08001352
mtklein21eaf3b2016-02-08 12:39:59 -08001353 // Kick off as much parallel work as we can, making note of any serial work we'll need to do.
1354 SkTaskGroup parallel;
1355 SkTArray<Task> serial;
1356
1357 for (auto& sink : gSinks)
1358 for (auto& src : gSrcs) {
mtklein3eed7dd2016-02-24 19:07:07 -08001359 if (src->veto(sink->flags()) ||
1360 is_blacklisted(sink.tag.c_str(), src.tag.c_str(),
1361 src.options.c_str(), src->name().c_str())) {
mtklein15923c92016-02-29 10:14:38 -08001362 SkAutoTAcquire<SkSpinlock> lock(gMutex);
mtklein3eed7dd2016-02-24 19:07:07 -08001363 gPending--;
1364 continue;
1365 }
1366
mtklein21eaf3b2016-02-08 12:39:59 -08001367 Task task(src, sink);
1368 if (src->serial() || sink->serial()) {
1369 serial.push_back(task);
1370 } else {
1371 parallel.add([task] { Task::Run(task); });
mtklein748ca3b2015-01-15 10:56:12 -08001372 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001373 }
mtklein21eaf3b2016-02-08 12:39:59 -08001374 for (auto test : gParallelTests) {
1375 parallel.add([test] { run_test(test); });
mtklein55e88b22015-01-21 15:50:13 -08001376 }
mtklein21eaf3b2016-02-08 12:39:59 -08001377
1378 // With the parallel work running, run serial tasks and tests here on main thread.
1379 for (auto task : serial) { Task::Run(task); }
1380 for (auto test : gSerialTests) { run_test(test); }
1381
1382 // Wait for any remaining parallel work to complete (including any spun off of serial tasks).
1383 parallel.wait();
mtkleincd50bca2016-01-05 06:20:20 -08001384 gDefinitelyThreadSafeWork.wait();
1385
mtkleinda884c42016-02-24 18:00:23 -08001386 // We'd better have run everything.
1387 SkASSERT(gPending == 0);
mtkleine027f172016-02-26 15:53:06 -08001388 // Make sure we've flushed all our results to disk.
1389 JsonWriter::DumpJson();
mtkleinda884c42016-02-24 18:00:23 -08001390
mtklein748ca3b2015-01-15 10:56:12 -08001391 // At this point we're back in single-threaded land.
caryclarkf53ce802015-06-15 06:48:30 -07001392 sk_tool_utils::release_portable_typefaces();
mtklein@google.comd36522d2013-10-16 13:02:15 +00001393
mtklein748ca3b2015-01-15 10:56:12 -08001394 if (gFailures.count() > 0) {
mtkleinc41fd922016-03-08 09:01:39 -08001395 info("Failures:\n");
mtklein748ca3b2015-01-15 10:56:12 -08001396 for (int i = 0; i < gFailures.count(); i++) {
mtkleinc41fd922016-03-08 09:01:39 -08001397 info("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001398 }
mtkleinc41fd922016-03-08 09:01:39 -08001399 info("%d failures\n", gFailures.count());
mtklein748ca3b2015-01-15 10:56:12 -08001400 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001401 }
mtkleinafae30a2016-02-24 12:28:32 -08001402
1403#ifdef SK_PDF_IMAGE_STATS
halcanary7a14b312015-10-01 07:28:13 -07001404 SkPDFImageDumpStats();
mtkleinafae30a2016-02-24 12:28:32 -08001405#endif // SK_PDF_IMAGE_STATS
1406
1407 print_status();
mtkleinc41fd922016-03-08 09:01:39 -08001408 info("Finished!\n");
mtklein748ca3b2015-01-15 10:56:12 -08001409 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +00001410}
jcgregorio3b27ade2014-11-13 08:06:40 -08001411
kkinnunen179a8f52015-11-20 13:32:24 -08001412// TODO: currently many GPU tests are declared outside SK_SUPPORT_GPU guards.
1413// Thus we export the empty RunWithGPUTestContexts when SK_SUPPORT_GPU=0.
1414namespace skiatest {
1415namespace {
1416typedef void(*TestWithGrContext)(skiatest::Reporter*, GrContext*);
1417typedef void(*TestWithGrContextAndGLContext)(skiatest::Reporter*, GrContext*, SkGLContext*);
1418#if SK_SUPPORT_GPU
1419template<typename T>
kkinnunen34058002016-01-06 23:49:30 -08001420void call_test(T test, skiatest::Reporter* reporter, const GrContextFactory::ContextInfo& context);
kkinnunen179a8f52015-11-20 13:32:24 -08001421template<>
1422void call_test(TestWithGrContext test, skiatest::Reporter* reporter,
kkinnunen34058002016-01-06 23:49:30 -08001423 const GrContextFactory::ContextInfo& context) {
1424 test(reporter, context.fGrContext);
kkinnunen179a8f52015-11-20 13:32:24 -08001425}
1426template<>
1427void call_test(TestWithGrContextAndGLContext test, skiatest::Reporter* reporter,
kkinnunen34058002016-01-06 23:49:30 -08001428 const GrContextFactory::ContextInfo& context) {
1429 test(reporter, context.fGrContext, context.fGLContext);
kkinnunen179a8f52015-11-20 13:32:24 -08001430}
1431#endif
1432} // namespace
1433
kkinnunen179a8f52015-11-20 13:32:24 -08001434template<typename T>
1435void RunWithGPUTestContexts(T test, GPUTestContexts testContexts, Reporter* reporter,
1436 GrContextFactory* factory) {
1437#if SK_SUPPORT_GPU
kkinnunen3e980c32015-12-23 01:33:00 -08001438 // Iterate over context types, except use "native" instead of explicitly trying OpenGL and
1439 // OpenGL ES. Do not use GLES on desktop, since tests do not account for not fixing
1440 // http://skbug.com/2809
1441 GrContextFactory::GLContextType contextTypes[] = {
1442 GrContextFactory::kNative_GLContextType,
1443#if SK_ANGLE
1444#ifdef SK_BUILD_FOR_WIN
1445 GrContextFactory::kANGLE_GLContextType,
1446#endif
1447 GrContextFactory::kANGLE_GL_GLContextType,
1448#endif
1449#if SK_COMMAND_BUFFER
kkinnunenf655e932016-03-03 07:39:48 -08001450 GrContextFactory::kCommandBuffer_GLContextType,
kkinnunen3e980c32015-12-23 01:33:00 -08001451#endif
1452#if SK_MESA
1453 GrContextFactory::kMESA_GLContextType,
1454#endif
1455 GrContextFactory::kNull_GLContextType,
1456 GrContextFactory::kDebug_GLContextType,
1457 };
1458 static_assert(SK_ARRAY_COUNT(contextTypes) == GrContextFactory::kGLContextTypeCnt - 2,
1459 "Skipping unexpected GLContextType for GPU tests");
1460
1461 for (auto& contextType : contextTypes) {
kkinnunen179a8f52015-11-20 13:32:24 -08001462 int contextSelector = kNone_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001463 if (GrContextFactory::IsRenderingGLContext(contextType)) {
kkinnunen179a8f52015-11-20 13:32:24 -08001464 contextSelector |= kAllRendering_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001465 } else if (contextType == GrContextFactory::kNative_GLContextType) {
kkinnunen179a8f52015-11-20 13:32:24 -08001466 contextSelector |= kNative_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001467 } else if (contextType == GrContextFactory::kNull_GLContextType) {
kkinnunen179a8f52015-11-20 13:32:24 -08001468 contextSelector |= kNull_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001469 } else if (contextType == GrContextFactory::kDebug_GLContextType) {
kkinnunen55eeae92015-12-10 23:19:29 -08001470 contextSelector |= kDebug_GPUTestContexts;
kkinnunen179a8f52015-11-20 13:32:24 -08001471 }
1472 if ((testContexts & contextSelector) == 0) {
1473 continue;
1474 }
kkinnunen34058002016-01-06 23:49:30 -08001475 GrContextFactory::ContextInfo context = factory->getContextInfo(contextType);
1476 if (context.fGrContext) {
kkinnunen5219fd92015-12-10 06:28:13 -08001477 call_test(test, reporter, context);
1478 }
kkinnunen34058002016-01-06 23:49:30 -08001479 context = factory->getContextInfo(contextType,
1480 GrContextFactory::kEnableNVPR_GLContextOptions);
1481 if (context.fGrContext) {
kkinnunen179a8f52015-11-20 13:32:24 -08001482 call_test(test, reporter, context);
1483 }
1484 }
1485#endif
1486}
1487
1488template
1489void RunWithGPUTestContexts<TestWithGrContext>(TestWithGrContext test,
1490 GPUTestContexts testContexts,
1491 Reporter* reporter,
1492 GrContextFactory* factory);
1493template
1494void RunWithGPUTestContexts<TestWithGrContextAndGLContext>(TestWithGrContextAndGLContext test,
1495 GPUTestContexts testContexts,
1496 Reporter* reporter,
1497 GrContextFactory* factory);
1498} // namespace skiatest
1499
borenet48087572015-04-02 12:16:36 -07001500#if !defined(SK_BUILD_FOR_IOS)
jcgregorio3b27ade2014-11-13 08:06:40 -08001501int main(int argc, char** argv) {
1502 SkCommandLineFlags::Parse(argc, argv);
1503 return dm_main();
1504}
1505#endif