blob: 34f8a961e3f4c503182c95e49e2efad9bfaa2d40 [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
benjaminwagnerec4d4d72016-03-25 12:59:53 -070076static const double kStartMs = SkTime::GetMSecs();
mtkleinc41fd922016-03-08 09:01:39 -080077
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.
brianosmanc9ced392016-03-24 07:27:43 -0700592 push_image_gen_src(path, ImageGenSrc::kCodec_Mode, codec->getInfo().alphaType(), true);
msarett18976312016-03-09 14:20:58 -0800593
594 // Push image generator CPU tests.
595 for (SkAlphaType alphaType : alphaModes) {
596 push_image_gen_src(path, ImageGenSrc::kCodec_Mode, alphaType, false);
597
598#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
599 if (kWEBP_SkEncodedFormat != codec->getEncodedFormat() &&
600 kWBMP_SkEncodedFormat != codec->getEncodedFormat() &&
601 kUnpremul_SkAlphaType != alphaType)
602 {
603 push_image_gen_src(path, ImageGenSrc::kPlatform_Mode, alphaType, false);
604 }
msarettfc0b6d12016-03-17 13:50:17 -0700605#elif defined(SK_BUILD_FOR_WIN)
606 if (kWEBP_SkEncodedFormat != codec->getEncodedFormat() &&
607 kWBMP_SkEncodedFormat != codec->getEncodedFormat())
608 {
609 push_image_gen_src(path, ImageGenSrc::kPlatform_Mode, alphaType, false);
610 }
msarett18976312016-03-09 14:20:58 -0800611#endif
612 }
msarett438b2ad2015-04-09 12:43:10 -0700613}
614
msarett5cb48852015-11-06 08:56:32 -0800615static bool brd_color_type_supported(SkBitmapRegionDecoder::Strategy strategy,
msaretta5783ae2015-09-08 15:35:32 -0700616 CodecSrc::DstColorType dstColorType) {
617 switch (strategy) {
msarett5cb48852015-11-06 08:56:32 -0800618 case SkBitmapRegionDecoder::kCanvas_Strategy:
msaretta5783ae2015-09-08 15:35:32 -0700619 if (CodecSrc::kGetFromCanvas_DstColorType == dstColorType) {
620 return true;
621 }
622 return false;
msarett5cb48852015-11-06 08:56:32 -0800623 case SkBitmapRegionDecoder::kAndroidCodec_Strategy:
msarett26ad17b2015-10-22 07:29:19 -0700624 switch (dstColorType) {
625 case CodecSrc::kGetFromCanvas_DstColorType:
626 case CodecSrc::kIndex8_Always_DstColorType:
627 case CodecSrc::kGrayscale_Always_DstColorType:
628 return true;
629 default:
630 return false;
631 }
msaretta5783ae2015-09-08 15:35:32 -0700632 default:
633 SkASSERT(false);
634 return false;
635 }
636}
637
msarett5cb48852015-11-06 08:56:32 -0800638static void push_brd_src(Path path, SkBitmapRegionDecoder::Strategy strategy,
msaretta5783ae2015-09-08 15:35:32 -0700639 CodecSrc::DstColorType dstColorType, BRDSrc::Mode mode, uint32_t sampleSize) {
640 SkString folder;
641 switch (strategy) {
msarett5cb48852015-11-06 08:56:32 -0800642 case SkBitmapRegionDecoder::kCanvas_Strategy:
msaretta5783ae2015-09-08 15:35:32 -0700643 folder.append("brd_canvas");
644 break;
msarett5cb48852015-11-06 08:56:32 -0800645 case SkBitmapRegionDecoder::kAndroidCodec_Strategy:
msarett26ad17b2015-10-22 07:29:19 -0700646 folder.append("brd_android_codec");
647 break;
msaretta5783ae2015-09-08 15:35:32 -0700648 default:
649 SkASSERT(false);
650 return;
651 }
652
653 switch (mode) {
654 case BRDSrc::kFullImage_Mode:
655 break;
656 case BRDSrc::kDivisor_Mode:
657 folder.append("_divisor");
658 break;
659 default:
660 SkASSERT(false);
661 return;
662 }
663
664 switch (dstColorType) {
665 case CodecSrc::kGetFromCanvas_DstColorType:
666 break;
667 case CodecSrc::kIndex8_Always_DstColorType:
668 folder.append("_kIndex");
669 break;
670 case CodecSrc::kGrayscale_Always_DstColorType:
671 folder.append("_kGray");
672 break;
673 default:
674 SkASSERT(false);
675 return;
676 }
677
678 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800679 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700680 }
681
682 BRDSrc* src = new BRDSrc(path, strategy, mode, dstColorType, sampleSize);
683 push_src("image", folder, src);
684}
685
686static void push_brd_srcs(Path path) {
687
msarett5cb48852015-11-06 08:56:32 -0800688 const SkBitmapRegionDecoder::Strategy strategies[] = {
689 SkBitmapRegionDecoder::kCanvas_Strategy,
msarett5cb48852015-11-06 08:56:32 -0800690 SkBitmapRegionDecoder::kAndroidCodec_Strategy,
msaretta5783ae2015-09-08 15:35:32 -0700691 };
692
scroggo501b7342015-11-03 07:55:11 -0800693 // Test on a variety of sampleSizes, making sure to include:
694 // - 2, 4, and 8, which are natively supported by jpeg
695 // - multiples of 2 which are not divisible by 4 (analogous for 4)
696 // - larger powers of two, since BRD clients generally use powers of 2
697 // We will only produce output for the larger sizes on large images.
698 const uint32_t sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 24, 32, 64 };
msarett6efbe052015-09-11 09:01:16 -0700699
msaretta5783ae2015-09-08 15:35:32 -0700700 // We will only test to one backend (8888), but we will test all of the
701 // color types that we need to decode to on this backend.
702 const CodecSrc::DstColorType dstColorTypes[] = {
msarett26ad17b2015-10-22 07:29:19 -0700703 CodecSrc::kGetFromCanvas_DstColorType,
704 CodecSrc::kIndex8_Always_DstColorType,
705 CodecSrc::kGrayscale_Always_DstColorType,
msaretta5783ae2015-09-08 15:35:32 -0700706 };
707
708 const BRDSrc::Mode modes[] = {
msarett26ad17b2015-10-22 07:29:19 -0700709 BRDSrc::kFullImage_Mode,
710 BRDSrc::kDivisor_Mode,
msaretta5783ae2015-09-08 15:35:32 -0700711 };
712
msarett5cb48852015-11-06 08:56:32 -0800713 for (SkBitmapRegionDecoder::Strategy strategy : strategies) {
msarett6efbe052015-09-11 09:01:16 -0700714 for (uint32_t sampleSize : sampleSizes) {
msarett6efbe052015-09-11 09:01:16 -0700715 for (CodecSrc::DstColorType dstColorType : dstColorTypes) {
716 if (brd_color_type_supported(strategy, dstColorType)) {
717 for (BRDSrc::Mode mode : modes) {
msaretta5783ae2015-09-08 15:35:32 -0700718 push_brd_src(path, strategy, dstColorType, mode, sampleSize);
719 }
720 }
721 }
722 }
723 }
724}
725
726static bool brd_supported(const char* ext) {
msarett8c8f22a2015-04-01 06:58:48 -0700727 static const char* const exts[] = {
msaretta5783ae2015-09-08 15:35:32 -0700728 "jpg", "jpeg", "png", "webp",
729 "JPG", "JPEG", "PNG", "WEBP",
msarett8c8f22a2015-04-01 06:58:48 -0700730 };
731
732 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
733 if (0 == strcmp(exts[i], ext)) {
734 return true;
735 }
736 }
737 return false;
scroggo9b77ddd2015-03-19 06:03:39 -0700738}
739
scroggo86737142016-02-03 12:19:11 -0800740static bool gather_srcs() {
mtklein748ca3b2015-01-15 10:56:12 -0800741 for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) {
djsollen54416de2015-04-03 07:24:48 -0700742 push_src("gm", "", new GMSrc(r->factory()));
mtklein748ca3b2015-01-15 10:56:12 -0800743 }
halcanaryfc37ad12015-01-30 07:31:19 -0800744 for (int i = 0; i < FLAGS_skps.count(); i++) {
745 const char* path = FLAGS_skps[i];
746 if (sk_isdir(path)) {
747 SkOSFile::Iter it(path, "skp");
748 for (SkString file; it.next(&file); ) {
djsollen54416de2015-04-03 07:24:48 -0700749 push_src("skp", "", new SKPSrc(SkOSPath::Join(path, file.c_str())));
halcanaryfc37ad12015-01-30 07:31:19 -0800750 }
751 } else {
djsollen54416de2015-04-03 07:24:48 -0700752 push_src("skp", "", new SKPSrc(path));
mtklein114c3cd2015-01-15 10:15:02 -0800753 }
754 }
scroggo86737142016-02-03 12:19:11 -0800755
756 SkTArray<SkString> images;
757 if (!CollectImages(&images)) {
758 return false;
759 }
760
761 for (auto image : images) {
762 push_codec_srcs(image);
scroggoe4499842016-02-25 11:03:47 -0800763 if (FLAGS_simpleCodec) {
764 continue;
765 }
766
mtklein21eaf3b2016-02-08 12:39:59 -0800767 const char* ext = strrchr(image.c_str(), '.');
768 if (ext && brd_supported(ext+1)) {
scroggo86737142016-02-03 12:19:11 -0800769 push_brd_srcs(image);
mtklein709d2c32015-01-15 08:30:25 -0800770 }
mtklein709d2c32015-01-15 08:30:25 -0800771 }
scroggo86737142016-02-03 12:19:11 -0800772
773 return true;
mtklein709d2c32015-01-15 08:30:25 -0800774}
775
kkinnunen3e980c32015-12-23 01:33:00 -0800776static void push_sink(const SkCommandLineConfig& config, Sink* s) {
rmistry0f515bd2015-12-22 10:22:26 -0800777 SkAutoTDelete<Sink> sink(s);
kkinnunen3e980c32015-12-23 01:33:00 -0800778
mtkleine0effd62015-07-29 06:37:28 -0700779 // Try a simple Src as a canary. If it fails, skip this sink.
mtklein748ca3b2015-01-15 10:56:12 -0800780 struct : public Src {
mtkleine0effd62015-07-29 06:37:28 -0700781 Error draw(SkCanvas* c) const override {
782 c->drawRect(SkRect::MakeWH(1,1), SkPaint());
783 return "";
784 }
mtklein36352bf2015-03-25 18:17:31 -0700785 SkISize size() const override { return SkISize::Make(16, 16); }
mtkleine0effd62015-07-29 06:37:28 -0700786 Name name() const override { return "justOneRect"; }
787 } justOneRect;
mtklein114c3cd2015-01-15 10:15:02 -0800788
mtklein748ca3b2015-01-15 10:56:12 -0800789 SkBitmap bitmap;
790 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800791 SkString log;
mtkleine0effd62015-07-29 06:37:28 -0700792 Error err = sink->draw(justOneRect, &bitmap, &stream, &log);
mtklein4089ef72015-03-05 08:40:28 -0800793 if (err.isFatal()) {
mtkleinc41fd922016-03-08 09:01:39 -0800794 info("Could not run %s: %s\n", config.getTag().c_str(), err.c_str());
mtklein05641a52015-04-21 10:49:13 -0700795 exit(1);
mtklein114c3cd2015-01-15 10:15:02 -0800796 }
797
mtkleine0effd62015-07-29 06:37:28 -0700798 TaggedSink& ts = gSinks.push_back();
mtklein18300a32016-03-16 13:53:35 -0700799 ts.reset(sink.release());
kkinnunen3e980c32015-12-23 01:33:00 -0800800 ts.tag = config.getTag();
mtklein748ca3b2015-01-15 10:56:12 -0800801}
802
803static bool gpu_supported() {
804#if SK_SUPPORT_GPU
805 return FLAGS_gpu;
806#else
807 return false;
808#endif
809}
kkinnunen9ebc3f02015-12-21 23:48:13 -0800810
kkinnunen3e980c32015-12-23 01:33:00 -0800811static Sink* create_sink(const SkCommandLineConfig* config) {
812#if SK_SUPPORT_GPU
813 if (gpu_supported()) {
814 if (const SkCommandLineConfigGpu* gpuConfig = config->asConfigGpu()) {
815 GrContextFactory::GLContextType contextType = gpuConfig->getContextType();
816 GrContextFactory::GLContextOptions contextOptions =
817 GrContextFactory::kNone_GLContextOptions;
818 if (gpuConfig->getUseNVPR()) {
819 contextOptions = static_cast<GrContextFactory::GLContextOptions>(
820 contextOptions | GrContextFactory::kEnableNVPR_GLContextOptions);
821 }
822 GrContextFactory testFactory;
823 if (!testFactory.get(contextType, contextOptions)) {
mtkleinc41fd922016-03-08 09:01:39 -0800824 info("WARNING: can not create GPU context for config '%s'. "
825 "GM tests will be skipped.\n", gpuConfig->getTag().c_str());
kkinnunen3e980c32015-12-23 01:33:00 -0800826 return nullptr;
827 }
828 return new GPUSink(contextType, contextOptions, gpuConfig->getSamples(),
brianosmand93c1202016-03-10 07:49:08 -0800829 gpuConfig->getUseDIText(), gpuConfig->getColorType(),
830 gpuConfig->getProfileType(), FLAGS_gpu_threading);
kkinnunen3e980c32015-12-23 01:33:00 -0800831 }
832 }
833#endif
834
835#define SINK(t, sink, ...) if (config->getBackend().equals(t)) { return new sink(__VA_ARGS__); }
836
tomhudsoneebc39a2015-02-23 12:18:05 -0800837#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
838 SINK("hwui", HWUISink);
839#endif
840
mtklein748ca3b2015-01-15 10:56:12 -0800841 if (FLAGS_cpu) {
842 SINK("565", RasterSink, kRGB_565_SkColorType);
843 SINK("8888", RasterSink, kN32_SkColorType);
mtklein27c3fdd2016-02-26 14:43:21 -0800844 SINK("srgb", RasterSink, kN32_SkColorType, kSRGB_SkColorProfileType);
845 SINK("f16", RasterSink, kRGBA_F16_SkColorType);
halcanaryc11c62f2015-09-28 11:51:54 -0700846 SINK("pdf", PDFSink, "Pdfium");
847 SINK("pdf_poppler", PDFSink, "Poppler");
mtklein9c3f17d2015-01-28 11:35:18 -0800848 SINK("skp", SKPSink);
mtklein8a4527e2015-01-31 20:00:58 -0800849 SINK("svg", SVGSink);
850 SINK("null", NullSink);
halcanary47ef4d52015-03-03 09:13:09 -0800851 SINK("xps", XPSSink);
mtklein748ca3b2015-01-15 10:56:12 -0800852 }
853#undef SINK
halcanary96fcdcc2015-08-27 07:41:13 -0700854 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800855}
856
kkinnunen3e980c32015-12-23 01:33:00 -0800857static Sink* create_via(const SkString& tag, Sink* wrapped) {
858#define VIA(t, via, ...) if (tag.equals(t)) { return new via(__VA_ARGS__); }
halcanary96fcdcc2015-08-27 07:41:13 -0700859 VIA("twice", ViaTwice, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700860 VIA("serialize", ViaSerialization, wrapped);
mtklein4a34ecb2016-01-08 10:19:35 -0800861 VIA("pic", ViaPicture, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700862 VIA("2ndpic", ViaSecondPicture, wrapped);
mtkleind31c13d2015-05-05 12:59:56 -0700863 VIA("sp", ViaSingletonPictures, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700864 VIA("tiles", ViaTiles, 256, 256, nullptr, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800865 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
halcanary7a76f9c2016-02-03 11:53:18 -0800866 VIA("mojo", ViaMojo, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800867
mtkleind603b222015-02-17 11:13:33 -0800868 if (FLAGS_matrix.count() == 4) {
mtklein748ca3b2015-01-15 10:56:12 -0800869 SkMatrix m;
mtkleind603b222015-02-17 11:13:33 -0800870 m.reset();
871 m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
872 m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
873 m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
874 m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
875 VIA("matrix", ViaMatrix, m, wrapped);
876 VIA("upright", ViaUpright, m, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800877 }
tomhudson64de1e12015-03-05 08:01:07 -0800878
879#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
880 VIA("androidsdk", ViaAndroidSDK, wrapped);
881#endif
882
mtklein748ca3b2015-01-15 10:56:12 -0800883#undef VIA
halcanary96fcdcc2015-08-27 07:41:13 -0700884 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800885}
886
mtklein748ca3b2015-01-15 10:56:12 -0800887static void gather_sinks() {
kkinnunen3e980c32015-12-23 01:33:00 -0800888 SkCommandLineConfigArray configs;
889 ParseConfigs(FLAGS_config, &configs);
890 for (int i = 0; i < configs.count(); i++) {
891 const SkCommandLineConfig& config = *configs[i];
892 Sink* sink = create_sink(&config);
893 if (sink == nullptr) {
mtkleinc41fd922016-03-08 09:01:39 -0800894 info("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
895 config.getTag().c_str());
kkinnunen3e980c32015-12-23 01:33:00 -0800896 continue;
897 }
mtklein748ca3b2015-01-15 10:56:12 -0800898
kkinnunen3e980c32015-12-23 01:33:00 -0800899 const SkTArray<SkString>& parts = config.getViaParts();
900 for (int j = parts.count(); j-- > 0;) {
901 const SkString& part = parts[j];
902 Sink* next = create_via(part, sink);
halcanary96fcdcc2015-08-27 07:41:13 -0700903 if (next == nullptr) {
mtkleinc41fd922016-03-08 09:01:39 -0800904 info("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
905 part.c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800906 delete sink;
halcanary96fcdcc2015-08-27 07:41:13 -0700907 sink = nullptr;
mtklein748ca3b2015-01-15 10:56:12 -0800908 break;
909 }
910 sink = next;
911 }
912 if (sink) {
913 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800914 }
915 }
916}
mtklein709d2c32015-01-15 08:30:25 -0800917
mtkleina5114d72015-08-24 13:27:01 -0700918static bool dump_png(SkBitmap bitmap, const char* path, const char* md5) {
919 const int w = bitmap.width(),
920 h = bitmap.height();
mtklein27c3fdd2016-02-26 14:43:21 -0800921 // PNG wants unpremultiplied 8-bit RGBA pixels (16-bit could work fine too).
922 // We leave the gamma of these bytes unspecified, to continue the status quo,
923 // which we think generally is to interpret them as sRGB.
mtkleina5114d72015-08-24 13:27:01 -0700924
mtklein27c3fdd2016-02-26 14:43:21 -0800925 SkAutoTMalloc<uint32_t> rgba(w*h);
926
927 if (bitmap. colorType() == kN32_SkColorType &&
928 bitmap.profileType() == kSRGB_SkColorProfileType) {
929 // These are premul sRGB 8-bit pixels in SkPMColor order.
930 // We want unpremul sRGB 8-bit pixels in RGBA order. We'll get there via floats.
931 bitmap.lockPixels();
932 auto px = (const uint32_t*)bitmap.getPixels();
933 if (!px) {
mtkleina5114d72015-08-24 13:27:01 -0700934 return false;
935 }
mtklein27c3fdd2016-02-26 14:43:21 -0800936 for (int i = 0; i < w*h; i++) {
937 Sk4f fs = Sk4f_fromS32(px[i]); // Convert up to linear floats.
938 #if defined(SK_PMCOLOR_IS_BGRA)
939 fs = SkNx_shuffle<2,1,0,3>(fs); // Shuffle to RGBA, if not there already.
940 #endif
941 float invA = 1.0f / fs[3];
942 fs = fs * Sk4f(invA, invA, invA, 1); // Unpremultiply.
943 rgba[i] = Sk4f_toS32(fs); // Pack down to sRGB bytes.
944 }
mtkleina5114d72015-08-24 13:27:01 -0700945
mtklein27c3fdd2016-02-26 14:43:21 -0800946 } else if (bitmap.colorType() == kRGBA_F16_SkColorType) {
947 // These are premul linear half-float pixels in RGBA order.
948 // We want unpremul sRGB 8-bit pixels in RGBA order. We'll get there via floats.
949 bitmap.lockPixels();
950 auto px = (const uint64_t*)bitmap.getPixels();
951 if (!px) {
952 return false;
953 }
954 for (int i = 0; i < w*h; i++) {
955 Sk4f fs = SkHalfToFloat_01(px[i]); // Convert up to linear floats.
956 float invA = 1.0f / fs[3];
957 fs = fs * Sk4f(invA, invA, invA, 1); // Unpremultiply.
958 rgba[i] = Sk4f_toS32(fs); // Pack down to sRGB bytes.
959 }
960
961
962 } else {
963 // We "should" gamma correct in here but we don't.
964 // We want Gold to show exactly what our clients are seeing, broken gamma.
965
966 // Convert smaller formats up to premul linear 8-bit (in SkPMColor order).
967 if (bitmap.colorType() != kN32_SkColorType) {
968 SkBitmap n32;
969 if (!bitmap.copyTo(&n32, kN32_SkColorType)) {
970 return false;
971 }
972 bitmap = n32;
973 }
974
975 // Convert premul linear 8-bit to unpremul linear 8-bit RGBA.
976 if (!bitmap.readPixels(SkImageInfo::Make(w,h, kRGBA_8888_SkColorType,
977 kUnpremul_SkAlphaType),
978 rgba, 4*w, 0,0)) {
979 return false;
980 }
mtkleina5114d72015-08-24 13:27:01 -0700981 }
982
983 // We don't need bitmap anymore. Might as well drop our ref.
mtkleinfccb77d2015-08-24 14:13:29 -0700984 bitmap.reset();
mtkleina5114d72015-08-24 13:27:01 -0700985
mtkleinc64137c2015-08-25 10:56:08 -0700986 FILE* f = fopen(path, "wb");
mtkleina5114d72015-08-24 13:27:01 -0700987 if (!f) { return false; }
988
989 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
990 if (!png) {
991 fclose(f);
992 return false;
993 }
994
995 png_infop info = png_create_info_struct(png);
996 if (!info) {
997 png_destroy_write_struct(&png, &info);
998 fclose(f);
999 return false;
1000 }
1001
mtkleind69ece62015-09-10 10:37:44 -07001002 SkString description;
1003 description.append("Key: ");
1004 for (int i = 0; i < FLAGS_key.count(); i++) {
1005 description.appendf("%s ", FLAGS_key[i]);
1006 }
1007 description.append("Properties: ");
1008 for (int i = 0; i < FLAGS_properties.count(); i++) {
1009 description.appendf("%s ", FLAGS_properties[i]);
1010 }
1011 description.appendf("MD5: %s", md5);
1012
mtkleina5114d72015-08-24 13:27:01 -07001013 png_text text[2];
1014 text[0].key = (png_charp)"Author";
1015 text[0].text = (png_charp)"DM dump_png()";
1016 text[0].compression = PNG_TEXT_COMPRESSION_NONE;
1017 text[1].key = (png_charp)"Description";
mtkleind69ece62015-09-10 10:37:44 -07001018 text[1].text = (png_charp)description.c_str();
mtkleina5114d72015-08-24 13:27:01 -07001019 text[1].compression = PNG_TEXT_COMPRESSION_NONE;
1020 png_set_text(png, info, text, 2);
1021
1022 png_init_io(png, f);
1023 png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8,
1024 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
1025 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
1026 png_write_info(png, info);
1027 for (int j = 0; j < h; j++) {
1028 png_bytep row = (png_bytep)(rgba.get() + w*j);
1029 png_write_rows(png, &row, 1);
1030 }
1031 png_write_end(png, info);
1032
1033 png_destroy_write_struct(&png, &info);
1034 fclose(f);
1035 return true;
1036}
1037
mtkleina2ef6422015-01-15 13:44:22 -08001038static bool match(const char* needle, const char* haystack) {
halcanary96fcdcc2015-08-27 07:41:13 -07001039 return 0 == strcmp("_", needle) || nullptr != strstr(haystack, needle);
mtkleina2ef6422015-01-15 13:44:22 -08001040}
1041
mtklein3eed7dd2016-02-24 19:07:07 -08001042static bool is_blacklisted(const char* sink, const char* src,
1043 const char* srcOptions, const char* name) {
mtklein0d243ff2015-04-03 07:51:00 -07001044 for (int i = 0; i < FLAGS_blacklist.count() - 3; i += 4) {
mtkleina2ef6422015-01-15 13:44:22 -08001045 if (match(FLAGS_blacklist[i+0], sink) &&
djsollen54416de2015-04-03 07:24:48 -07001046 match(FLAGS_blacklist[i+1], src) &&
1047 match(FLAGS_blacklist[i+2], srcOptions) &&
1048 match(FLAGS_blacklist[i+3], name)) {
mtklein3eed7dd2016-02-24 19:07:07 -08001049 return true;
mtkleina2ef6422015-01-15 13:44:22 -08001050 }
1051 }
mtklein3eed7dd2016-02-24 19:07:07 -08001052 return false;
mtkleina2ef6422015-01-15 13:44:22 -08001053}
1054
mtkleincd50bca2016-01-05 06:20:20 -08001055// Even when a Task Sink reports to be non-threadsafe (e.g. GPU), we know things like
1056// .png encoding are definitely thread safe. This lets us offload that work to CPU threads.
1057static SkTaskGroup gDefinitelyThreadSafeWork;
1058
mtklein748ca3b2015-01-15 10:56:12 -08001059// The finest-grained unit of work we can run: draw a single Src into a single Sink,
1060// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
1061struct Task {
mtkleine0effd62015-07-29 06:37:28 -07001062 Task(const TaggedSrc& src, const TaggedSink& sink) : src(src), sink(sink) {}
1063 const TaggedSrc& src;
1064 const TaggedSink& sink;
mtklein748ca3b2015-01-15 10:56:12 -08001065
mtklein21eaf3b2016-02-08 12:39:59 -08001066 static void Run(const Task& task) {
1067 SkString name = task.src->name();
mtkleine0effd62015-07-29 06:37:28 -07001068
mtkleinb9eb4ac2015-02-02 18:26:03 -08001069 SkString log;
mtklein3eed7dd2016-02-24 19:07:07 -08001070 if (!FLAGS_dryRun) {
mtklein748ca3b2015-01-15 10:56:12 -08001071 SkBitmap bitmap;
1072 SkDynamicMemoryWStream stream;
mtklein3eed7dd2016-02-24 19:07:07 -08001073 start(task.sink.tag.c_str(), task.src.tag.c_str(),
1074 task.src.options.c_str(), name.c_str());
mtklein21eaf3b2016-02-08 12:39:59 -08001075 Error err = task.sink->draw(*task.src, &bitmap, &stream, &log);
mtkleinb3b13b72016-03-07 13:20:52 -08001076 if (!log.isEmpty()) {
mtkleinc41fd922016-03-08 09:01:39 -08001077 info("%s %s %s %s:\n%s\n", task.sink.tag.c_str()
1078 , task.src.tag.c_str()
1079 , task.src.options.c_str()
1080 , name.c_str()
1081 , log.c_str());
mtkleinb3b13b72016-03-07 13:20:52 -08001082 }
mtklein748ca3b2015-01-15 10:56:12 -08001083 if (!err.isEmpty()) {
mtklein4089ef72015-03-05 08:40:28 -08001084 if (err.isFatal()) {
djsollen54416de2015-04-03 07:24:48 -07001085 fail(SkStringPrintf("%s %s %s %s: %s",
mtklein21eaf3b2016-02-08 12:39:59 -08001086 task.sink.tag.c_str(),
1087 task.src.tag.c_str(),
1088 task.src.options.c_str(),
mtklein4089ef72015-03-05 08:40:28 -08001089 name.c_str(),
1090 err.c_str()));
mtkleinb37cb412015-03-18 05:27:14 -07001091 } else {
mtklein3eed7dd2016-02-24 19:07:07 -08001092 done(task.sink.tag.c_str(), task.src.tag.c_str(),
1093 task.src.options.c_str(), name.c_str());
mtkleinba6ada72016-01-21 09:39:35 -08001094 return;
mtklein4089ef72015-03-05 08:40:28 -08001095 }
mtklein748ca3b2015-01-15 10:56:12 -08001096 }
mtklein62bd1a62015-01-27 14:46:26 -08001097
mtkleincd50bca2016-01-05 06:20:20 -08001098 // We're likely switching threads here, so we must capture by value, [=] or [foo,bar].
1099 SkStreamAsset* data = stream.detachAsStream();
1100 gDefinitelyThreadSafeWork.add([task,name,bitmap,data]{
1101 SkAutoTDelete<SkStreamAsset> ownedData(data);
1102
1103 // Why doesn't the copy constructor do this when we have pre-locked pixels?
1104 bitmap.lockPixels();
1105
1106 SkString md5;
1107 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
1108 SkMD5 hash;
1109 if (data->getLength()) {
1110 hash.writeStream(data, data->getLength());
1111 data->rewind();
mtklein38408462015-07-08 07:25:27 -07001112 } else {
mtkleincd50bca2016-01-05 06:20:20 -08001113 // If we're BGRA (Linux, Windows), swizzle over to RGBA (Mac, Android).
1114 // This helps eliminate multiple 0-pixel-diff hashes on gold.skia.org.
1115 // (Android's general slow speed breaks the tie arbitrarily in RGBA's favor.)
1116 // We might consider promoting 565 to RGBA too.
1117 if (bitmap.colorType() == kBGRA_8888_SkColorType) {
1118 SkBitmap swizzle;
1119 SkAssertResult(bitmap.copyTo(&swizzle, kRGBA_8888_SkColorType));
1120 hash.write(swizzle.getPixels(), swizzle.getSize());
1121 } else {
1122 hash.write(bitmap.getPixels(), bitmap.getSize());
1123 }
1124 }
1125 SkMD5::Digest digest;
1126 hash.finish(digest);
1127 for (int i = 0; i < 16; i++) {
1128 md5.appendf("%02x", digest.data[i]);
mtklein38408462015-07-08 07:25:27 -07001129 }
mtklein62bd1a62015-01-27 14:46:26 -08001130 }
mtklein62bd1a62015-01-27 14:46:26 -08001131
mtkleincd50bca2016-01-05 06:20:20 -08001132 if (!FLAGS_readPath.isEmpty() &&
mtklein3eed7dd2016-02-24 19:07:07 -08001133 !gGold.contains(Gold(task.sink.tag, task.src.tag,
1134 task.src.options, name, md5))) {
mtkleincd50bca2016-01-05 06:20:20 -08001135 fail(SkStringPrintf("%s not found for %s %s %s %s in %s",
1136 md5.c_str(),
mtklein21eaf3b2016-02-08 12:39:59 -08001137 task.sink.tag.c_str(),
1138 task.src.tag.c_str(),
1139 task.src.options.c_str(),
mtkleincd50bca2016-01-05 06:20:20 -08001140 name.c_str(),
1141 FLAGS_readPath[0]));
mtklein748ca3b2015-01-15 10:56:12 -08001142 }
mtkleincd50bca2016-01-05 06:20:20 -08001143
1144 if (!FLAGS_writePath.isEmpty()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001145 const char* ext = task.sink->fileExtension();
mtkleincd50bca2016-01-05 06:20:20 -08001146 if (data->getLength()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001147 WriteToDisk(task, md5, ext, data, data->getLength(), nullptr);
mtkleincd50bca2016-01-05 06:20:20 -08001148 SkASSERT(bitmap.drawsNothing());
1149 } else if (!bitmap.drawsNothing()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001150 WriteToDisk(task, md5, ext, nullptr, 0, &bitmap);
mtkleincd50bca2016-01-05 06:20:20 -08001151 }
1152 }
1153 });
mtklein748ca3b2015-01-15 10:56:12 -08001154 }
mtklein3eed7dd2016-02-24 19:07:07 -08001155 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 -08001156 }
1157
1158 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -08001159 SkString md5,
1160 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -08001161 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -08001162 const SkBitmap* bitmap) {
mtklein409d4702016-02-29 07:38:01 -08001163 bool gammaCorrect = false;
1164 if (bitmap) {
1165 gammaCorrect = bitmap->profileType() == kSRGB_SkColorProfileType
1166 || bitmap-> colorType() == kRGBA_F16_SkColorType;
1167 }
1168
mtklein748ca3b2015-01-15 10:56:12 -08001169 JsonWriter::BitmapResult result;
djsollen54416de2015-04-03 07:24:48 -07001170 result.name = task.src->name();
mtklein3eed7dd2016-02-24 19:07:07 -08001171 result.config = task.sink.tag;
djsollen54416de2015-04-03 07:24:48 -07001172 result.sourceType = task.src.tag;
1173 result.sourceOptions = task.src.options;
1174 result.ext = ext;
mtklein409d4702016-02-29 07:38:01 -08001175 result.gammaCorrect = gammaCorrect;
djsollen54416de2015-04-03 07:24:48 -07001176 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -08001177 JsonWriter::AddBitmapResult(result);
1178
mtkleinb0531a72015-04-07 13:38:48 -07001179 // If an MD5 is uninteresting, we want it noted in the JSON file,
1180 // but don't want to dump it out as a .png (or whatever ext is).
1181 if (gUninterestingHashes.contains(md5)) {
1182 return;
1183 }
1184
mtklein748ca3b2015-01-15 10:56:12 -08001185 const char* dir = FLAGS_writePath[0];
1186 if (0 == strcmp(dir, "@")) { // Needed for iOS.
1187 dir = FLAGS_resourcePath[0];
1188 }
1189 sk_mkdir(dir);
1190
1191 SkString path;
1192 if (FLAGS_nameByHash) {
1193 path = SkOSPath::Join(dir, result.md5.c_str());
1194 path.append(".");
1195 path.append(ext);
1196 if (sk_exists(path.c_str())) {
1197 return; // Content-addressed. If it exists already, we're done.
1198 }
1199 } else {
kkinnunen3e980c32015-12-23 01:33:00 -08001200 path = SkOSPath::Join(dir, task.sink.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001201 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001202 path = SkOSPath::Join(path.c_str(), task.src.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001203 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001204 if (strcmp(task.src.options.c_str(), "") != 0) {
1205 path = SkOSPath::Join(path.c_str(), task.src.options.c_str());
djsollen54416de2015-04-03 07:24:48 -07001206 sk_mkdir(path.c_str());
1207 }
mtklein748ca3b2015-01-15 10:56:12 -08001208 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
1209 path.append(".");
1210 path.append(ext);
1211 }
1212
mtklein748ca3b2015-01-15 10:56:12 -08001213 if (bitmap) {
mtkleina5114d72015-08-24 13:27:01 -07001214 if (!dump_png(*bitmap, path.c_str(), result.md5.c_str())) {
mtklein748ca3b2015-01-15 10:56:12 -08001215 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
1216 return;
1217 }
1218 } else {
mtkleina5114d72015-08-24 13:27:01 -07001219 SkFILEWStream file(path.c_str());
1220 if (!file.isValid()) {
1221 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
1222 return;
1223 }
mtklein748ca3b2015-01-15 10:56:12 -08001224 if (!file.writeStream(data, len)) {
1225 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
1226 return;
1227 }
1228 }
1229 }
1230};
1231
mtklein748ca3b2015-01-15 10:56:12 -08001232/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1233
1234// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
1235
mtklein21eaf3b2016-02-08 12:39:59 -08001236static SkTDArray<skiatest::Test> gParallelTests, gSerialTests;
mtklein748ca3b2015-01-15 10:56:12 -08001237
1238static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -08001239 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -08001240 return;
1241 }
mtklein6393c062015-04-27 08:45:01 -07001242 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r; r = r->next()) {
1243 if (!in_shard()) {
1244 continue;
1245 }
halcanary87f3ba42015-01-20 09:30:20 -08001246 // Despite its name, factory() is returning a reference to
1247 // link-time static const POD data.
1248 const skiatest::Test& test = r->factory();
1249 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -08001250 continue;
1251 }
halcanary87f3ba42015-01-20 09:30:20 -08001252 if (test.needsGpu && gpu_supported()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001253 (FLAGS_gpu_threading ? gParallelTests : gSerialTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -08001254 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein21eaf3b2016-02-08 12:39:59 -08001255 gParallelTests.push(test);
mtklein82d28432015-01-15 12:46:02 -08001256 }
mtklein748ca3b2015-01-15 10:56:12 -08001257 }
1258}
1259
mtklein21eaf3b2016-02-08 12:39:59 -08001260static void run_test(skiatest::Test test) {
halcanary87f3ba42015-01-20 09:30:20 -08001261 struct : public skiatest::Reporter {
mtklein36352bf2015-03-25 18:17:31 -07001262 void reportFailed(const skiatest::Failure& failure) override {
halcanary87f3ba42015-01-20 09:30:20 -08001263 fail(failure.toString());
1264 JsonWriter::AddTestFailure(failure);
1265 }
mtklein36352bf2015-03-25 18:17:31 -07001266 bool allowExtendedTest() const override {
halcanary87f3ba42015-01-20 09:30:20 -08001267 return FLAGS_pathOpsExtended;
1268 }
mtklein36352bf2015-03-25 18:17:31 -07001269 bool verbose() const override { return FLAGS_veryVerbose; }
halcanary87f3ba42015-01-20 09:30:20 -08001270 } reporter;
djsollen824996a2015-06-12 12:06:22 -07001271
mtklein3eed7dd2016-02-24 19:07:07 -08001272 if (!FLAGS_dryRun && !is_blacklisted("_", "tests", "_", test.name)) {
mtklein21eaf3b2016-02-08 12:39:59 -08001273 start("unit", "test", "", test.name);
mtklein55e88b22015-01-21 15:50:13 -08001274 GrContextFactory factory;
mtklein21eaf3b2016-02-08 12:39:59 -08001275 test.proc(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -08001276 }
mtklein3eed7dd2016-02-24 19:07:07 -08001277 done("unit", "test", "", test.name);
mtklein748ca3b2015-01-15 10:56:12 -08001278}
1279
1280/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1281
mtkleinafae30a2016-02-24 12:28:32 -08001282DEFINE_int32(status_sec, 15, "Print status this often (and if we crash).");
1283
1284SkThread* start_status_thread() {
1285 auto thread = new SkThread([] (void*) {
1286 for (;;) {
1287 print_status();
1288 #if defined(SK_BUILD_FOR_WIN)
1289 Sleep(FLAGS_status_sec * 1000);
1290 #else
1291 sleep(FLAGS_status_sec);
1292 #endif
mtklein2e1c47e2015-03-12 07:16:56 -07001293 }
mtkleinafae30a2016-02-24 12:28:32 -08001294 });
1295 thread->start();
1296 return thread;
mtkleinde6fc2b2015-03-12 06:28:54 -07001297}
1298
caryclark83ca6282015-06-10 09:31:09 -07001299#define PORTABLE_FONT_PREFIX "Toy Liberation "
1300
1301static SkTypeface* create_from_name(const char familyName[], SkTypeface::Style style) {
1302 if (familyName && strlen(familyName) > sizeof(PORTABLE_FONT_PREFIX)
1303 && !strncmp(familyName, PORTABLE_FONT_PREFIX, sizeof(PORTABLE_FONT_PREFIX) - 1)) {
caryclark1818acb2015-07-24 12:09:25 -07001304 return sk_tool_utils::create_portable_typeface(familyName, style);
caryclark83ca6282015-06-10 09:31:09 -07001305 }
halcanary96fcdcc2015-08-27 07:41:13 -07001306 return nullptr;
caryclark83ca6282015-06-10 09:31:09 -07001307}
1308
1309#undef PORTABLE_FONT_PREFIX
1310
1311extern SkTypeface* (*gCreateTypefaceDelegate)(const char [], SkTypeface::Style );
1312
jcgregorio3b27ade2014-11-13 08:06:40 -08001313int dm_main();
caryclark17f0b6d2014-07-22 10:15:34 -07001314int dm_main() {
mtklein246ba3a2016-02-23 10:39:36 -08001315 setup_crash_handler();
mtkleinafae30a2016-02-24 12:28:32 -08001316
mtkleina483da32016-03-17 12:53:36 -07001317 if (FLAGS_verbose) {
1318 gVLog = stderr;
1319 } else if (!FLAGS_writePath.isEmpty()) {
mtklein51c8cfc2016-03-11 12:59:09 -08001320 sk_mkdir(FLAGS_writePath[0]);
1321 gVLog = freopen(SkOSPath::Join(FLAGS_writePath[0], "verbose.log").c_str(), "w", stderr);
mtkleinc41fd922016-03-08 09:01:39 -08001322 }
1323
mtklein5286f022016-01-22 08:18:14 -08001324 JsonWriter::DumpJson(); // It's handy for the bots to assume this is ~never missing.
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +00001325 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -07001326 SkTaskGroup::Enabler enabled(FLAGS_threads);
caryclark83ca6282015-06-10 09:31:09 -07001327 gCreateTypefaceDelegate = &create_from_name;
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +00001328
halcanary7d571242016-02-24 17:59:16 -08001329 {
1330 SkString testResourcePath = GetResourcePath("color_wheel.png");
1331 SkFILEStream testResource(testResourcePath.c_str());
1332 if (!testResource.isValid()) {
mtkleinc41fd922016-03-08 09:01:39 -08001333 info("Some resources are missing. Do you need to set --resourcePath?\n");
halcanary7d571242016-02-24 17:59:16 -08001334 }
1335 }
mtklein62bd1a62015-01-27 14:46:26 -08001336 gather_gold();
borenet09ed4802015-04-03 14:15:33 -07001337 gather_uninteresting_hashes();
mtklein62bd1a62015-01-27 14:46:26 -08001338
scroggo86737142016-02-03 12:19:11 -08001339 if (!gather_srcs()) {
1340 return 1;
1341 }
mtklein748ca3b2015-01-15 10:56:12 -08001342 gather_sinks();
1343 gather_tests();
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +00001344
mtklein21eaf3b2016-02-08 12:39:59 -08001345 gPending = gSrcs.count() * gSinks.count() + gParallelTests.count() + gSerialTests.count();
mtkleinc41fd922016-03-08 09:01:39 -08001346 info("%d srcs * %d sinks + %d tests == %d tasks",
1347 gSrcs.count(), gSinks.count(), gParallelTests.count() + gSerialTests.count(), gPending);
mtkleinafae30a2016-02-24 12:28:32 -08001348 SkAutoTDelete<SkThread> statusThread(start_status_thread());
mtklein748ca3b2015-01-15 10:56:12 -08001349
mtklein21eaf3b2016-02-08 12:39:59 -08001350 // Kick off as much parallel work as we can, making note of any serial work we'll need to do.
1351 SkTaskGroup parallel;
1352 SkTArray<Task> serial;
1353
1354 for (auto& sink : gSinks)
1355 for (auto& src : gSrcs) {
mtklein3eed7dd2016-02-24 19:07:07 -08001356 if (src->veto(sink->flags()) ||
1357 is_blacklisted(sink.tag.c_str(), src.tag.c_str(),
1358 src.options.c_str(), src->name().c_str())) {
mtklein15923c92016-02-29 10:14:38 -08001359 SkAutoTAcquire<SkSpinlock> lock(gMutex);
mtklein3eed7dd2016-02-24 19:07:07 -08001360 gPending--;
1361 continue;
1362 }
1363
mtklein21eaf3b2016-02-08 12:39:59 -08001364 Task task(src, sink);
1365 if (src->serial() || sink->serial()) {
1366 serial.push_back(task);
1367 } else {
1368 parallel.add([task] { Task::Run(task); });
mtklein748ca3b2015-01-15 10:56:12 -08001369 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001370 }
mtklein21eaf3b2016-02-08 12:39:59 -08001371 for (auto test : gParallelTests) {
1372 parallel.add([test] { run_test(test); });
mtklein55e88b22015-01-21 15:50:13 -08001373 }
mtklein21eaf3b2016-02-08 12:39:59 -08001374
1375 // With the parallel work running, run serial tasks and tests here on main thread.
1376 for (auto task : serial) { Task::Run(task); }
1377 for (auto test : gSerialTests) { run_test(test); }
1378
1379 // Wait for any remaining parallel work to complete (including any spun off of serial tasks).
1380 parallel.wait();
mtkleincd50bca2016-01-05 06:20:20 -08001381 gDefinitelyThreadSafeWork.wait();
1382
mtkleinda884c42016-02-24 18:00:23 -08001383 // We'd better have run everything.
1384 SkASSERT(gPending == 0);
mtkleine027f172016-02-26 15:53:06 -08001385 // Make sure we've flushed all our results to disk.
1386 JsonWriter::DumpJson();
mtkleinda884c42016-02-24 18:00:23 -08001387
mtklein748ca3b2015-01-15 10:56:12 -08001388 // At this point we're back in single-threaded land.
caryclarkf53ce802015-06-15 06:48:30 -07001389 sk_tool_utils::release_portable_typefaces();
mtklein@google.comd36522d2013-10-16 13:02:15 +00001390
mtklein748ca3b2015-01-15 10:56:12 -08001391 if (gFailures.count() > 0) {
mtkleinc41fd922016-03-08 09:01:39 -08001392 info("Failures:\n");
mtklein748ca3b2015-01-15 10:56:12 -08001393 for (int i = 0; i < gFailures.count(); i++) {
mtkleinc41fd922016-03-08 09:01:39 -08001394 info("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001395 }
mtkleinc41fd922016-03-08 09:01:39 -08001396 info("%d failures\n", gFailures.count());
mtklein748ca3b2015-01-15 10:56:12 -08001397 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001398 }
mtkleinafae30a2016-02-24 12:28:32 -08001399
1400#ifdef SK_PDF_IMAGE_STATS
halcanary7a14b312015-10-01 07:28:13 -07001401 SkPDFImageDumpStats();
mtkleinafae30a2016-02-24 12:28:32 -08001402#endif // SK_PDF_IMAGE_STATS
1403
1404 print_status();
mtkleinc41fd922016-03-08 09:01:39 -08001405 info("Finished!\n");
mtklein748ca3b2015-01-15 10:56:12 -08001406 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +00001407}
jcgregorio3b27ade2014-11-13 08:06:40 -08001408
kkinnunen179a8f52015-11-20 13:32:24 -08001409// TODO: currently many GPU tests are declared outside SK_SUPPORT_GPU guards.
1410// Thus we export the empty RunWithGPUTestContexts when SK_SUPPORT_GPU=0.
1411namespace skiatest {
1412namespace {
1413typedef void(*TestWithGrContext)(skiatest::Reporter*, GrContext*);
1414typedef void(*TestWithGrContextAndGLContext)(skiatest::Reporter*, GrContext*, SkGLContext*);
1415#if SK_SUPPORT_GPU
1416template<typename T>
kkinnunen34058002016-01-06 23:49:30 -08001417void call_test(T test, skiatest::Reporter* reporter, const GrContextFactory::ContextInfo& context);
kkinnunen179a8f52015-11-20 13:32:24 -08001418template<>
1419void call_test(TestWithGrContext test, skiatest::Reporter* reporter,
kkinnunen34058002016-01-06 23:49:30 -08001420 const GrContextFactory::ContextInfo& context) {
1421 test(reporter, context.fGrContext);
kkinnunen179a8f52015-11-20 13:32:24 -08001422}
1423template<>
1424void call_test(TestWithGrContextAndGLContext test, skiatest::Reporter* reporter,
kkinnunen34058002016-01-06 23:49:30 -08001425 const GrContextFactory::ContextInfo& context) {
1426 test(reporter, context.fGrContext, context.fGLContext);
kkinnunen179a8f52015-11-20 13:32:24 -08001427}
1428#endif
1429} // namespace
1430
kkinnunen179a8f52015-11-20 13:32:24 -08001431template<typename T>
1432void RunWithGPUTestContexts(T test, GPUTestContexts testContexts, Reporter* reporter,
1433 GrContextFactory* factory) {
1434#if SK_SUPPORT_GPU
kkinnunen3e980c32015-12-23 01:33:00 -08001435 // Iterate over context types, except use "native" instead of explicitly trying OpenGL and
1436 // OpenGL ES. Do not use GLES on desktop, since tests do not account for not fixing
1437 // http://skbug.com/2809
1438 GrContextFactory::GLContextType contextTypes[] = {
1439 GrContextFactory::kNative_GLContextType,
1440#if SK_ANGLE
1441#ifdef SK_BUILD_FOR_WIN
1442 GrContextFactory::kANGLE_GLContextType,
1443#endif
1444 GrContextFactory::kANGLE_GL_GLContextType,
1445#endif
1446#if SK_COMMAND_BUFFER
kkinnunenf655e932016-03-03 07:39:48 -08001447 GrContextFactory::kCommandBuffer_GLContextType,
kkinnunen3e980c32015-12-23 01:33:00 -08001448#endif
1449#if SK_MESA
1450 GrContextFactory::kMESA_GLContextType,
1451#endif
1452 GrContextFactory::kNull_GLContextType,
1453 GrContextFactory::kDebug_GLContextType,
1454 };
1455 static_assert(SK_ARRAY_COUNT(contextTypes) == GrContextFactory::kGLContextTypeCnt - 2,
1456 "Skipping unexpected GLContextType for GPU tests");
1457
1458 for (auto& contextType : contextTypes) {
kkinnunen179a8f52015-11-20 13:32:24 -08001459 int contextSelector = kNone_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001460 if (GrContextFactory::IsRenderingGLContext(contextType)) {
kkinnunen179a8f52015-11-20 13:32:24 -08001461 contextSelector |= kAllRendering_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001462 } else if (contextType == GrContextFactory::kNative_GLContextType) {
kkinnunen179a8f52015-11-20 13:32:24 -08001463 contextSelector |= kNative_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001464 } else if (contextType == GrContextFactory::kNull_GLContextType) {
kkinnunen179a8f52015-11-20 13:32:24 -08001465 contextSelector |= kNull_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001466 } else if (contextType == GrContextFactory::kDebug_GLContextType) {
kkinnunen55eeae92015-12-10 23:19:29 -08001467 contextSelector |= kDebug_GPUTestContexts;
kkinnunen179a8f52015-11-20 13:32:24 -08001468 }
1469 if ((testContexts & contextSelector) == 0) {
1470 continue;
1471 }
kkinnunen34058002016-01-06 23:49:30 -08001472 GrContextFactory::ContextInfo context = factory->getContextInfo(contextType);
1473 if (context.fGrContext) {
kkinnunen5219fd92015-12-10 06:28:13 -08001474 call_test(test, reporter, context);
1475 }
kkinnunen34058002016-01-06 23:49:30 -08001476 context = factory->getContextInfo(contextType,
1477 GrContextFactory::kEnableNVPR_GLContextOptions);
1478 if (context.fGrContext) {
kkinnunen179a8f52015-11-20 13:32:24 -08001479 call_test(test, reporter, context);
1480 }
1481 }
1482#endif
1483}
1484
1485template
1486void RunWithGPUTestContexts<TestWithGrContext>(TestWithGrContext test,
1487 GPUTestContexts testContexts,
1488 Reporter* reporter,
1489 GrContextFactory* factory);
1490template
1491void RunWithGPUTestContexts<TestWithGrContextAndGLContext>(TestWithGrContextAndGLContext test,
1492 GPUTestContexts testContexts,
1493 Reporter* reporter,
1494 GrContextFactory* factory);
1495} // namespace skiatest
1496
borenet48087572015-04-02 12:16:36 -07001497#if !defined(SK_BUILD_FOR_IOS)
jcgregorio3b27ade2014-11-13 08:06:40 -08001498int main(int argc, char** argv) {
1499 SkCommandLineFlags::Parse(argc, argv);
1500 return dm_main();
1501}
1502#endif