blob: d558e08ca64772b6d85ee4398d26bf59bb160c2a [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();
188 }
189 // Execute default exception handler... hopefully, exit.
190 return EXCEPTION_EXECUTE_HANDLER;
mtklein246ba3a2016-02-23 10:39:36 -0800191 }
mtkleinf8557292016-02-29 06:35:28 -0800192 static void setup_crash_handler() { SetUnhandledExceptionFilter(handler); }
mtklein246ba3a2016-02-23 10:39:36 -0800193
mtklein819ab102016-02-29 17:02:52 -0800194#elif !defined(SK_BUILD_FOR_ANDROID)
195 #include <execinfo.h>
mtklein246ba3a2016-02-23 10:39:36 -0800196 #include <signal.h>
mtklein819ab102016-02-29 17:02:52 -0800197 #include <stdlib.h>
mtkleinf8557292016-02-29 06:35:28 -0800198
mtklein246ba3a2016-02-23 10:39:36 -0800199 static void setup_crash_handler() {
200 const int kSignals[] = { SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGSEGV };
201 for (int sig : kSignals) {
202 signal(sig, [](int sig) {
mtkleinf8557292016-02-29 06:35:28 -0800203 if (!in_signal_handler.exchange(true)) {
mtklein819ab102016-02-29 17:02:52 -0800204 SkAutoTAcquire<SkSpinlock> lock(gMutex);
mtkleinc41fd922016-03-08 09:01:39 -0800205 info("\nCaught signal %d [%s], was running:\n", sig, strsignal(sig));
mtklein819ab102016-02-29 17:02:52 -0800206 for (auto& task : gRunning) {
mtkleinc41fd922016-03-08 09:01:39 -0800207 info("\t%s\n", task.c_str());
mtklein819ab102016-02-29 17:02:52 -0800208 }
209
210 void* stack[64];
211 int count = backtrace(stack, SK_ARRAY_COUNT(stack));
212 char** symbols = backtrace_symbols(stack, count);
mtkleinc41fd922016-03-08 09:01:39 -0800213 info("\nStack trace:\n");
mtklein819ab102016-02-29 17:02:52 -0800214 for (int i = 0; i < count; i++) {
mtkleinc41fd922016-03-08 09:01:39 -0800215 info(" %s\n", symbols[i]);
mtklein819ab102016-02-29 17:02:52 -0800216 }
mtkleinf8557292016-02-29 06:35:28 -0800217 }
mtklein819ab102016-02-29 17:02:52 -0800218 _Exit(sig);
mtklein246ba3a2016-02-23 10:39:36 -0800219 });
220 }
221 }
mtklein819ab102016-02-29 17:02:52 -0800222
223#else // Android
224 static void setup_crash_handler() {}
mtklein246ba3a2016-02-23 10:39:36 -0800225#endif
226
mtklein748ca3b2015-01-15 10:56:12 -0800227/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
mtklein114c3cd2015-01-15 10:15:02 -0800228
mtklein62bd1a62015-01-27 14:46:26 -0800229struct Gold : public SkString {
mtkleina82f5622015-02-20 12:30:19 -0800230 Gold() : SkString("") {}
mtklein3eed7dd2016-02-24 19:07:07 -0800231 Gold(const SkString& sink, const SkString& src,
232 const SkString& srcOptions, const SkString& name,
233 const SkString& md5)
mtklein62bd1a62015-01-27 14:46:26 -0800234 : SkString("") {
235 this->append(sink);
236 this->append(src);
djsollen54416de2015-04-03 07:24:48 -0700237 this->append(srcOptions);
mtklein62bd1a62015-01-27 14:46:26 -0800238 this->append(name);
239 this->append(md5);
mtklein62bd1a62015-01-27 14:46:26 -0800240 }
mtkleinc8d1dd42015-10-15 12:23:01 -0700241 struct Hash {
242 uint32_t operator()(const Gold& g) const {
243 return SkGoodHash()((const SkString&)g);
244 }
245 };
mtklein62bd1a62015-01-27 14:46:26 -0800246};
mtkleina82f5622015-02-20 12:30:19 -0800247static SkTHashSet<Gold, Gold::Hash> gGold;
mtklein62bd1a62015-01-27 14:46:26 -0800248
249static void add_gold(JsonWriter::BitmapResult r) {
djsollen54416de2015-04-03 07:24:48 -0700250 gGold.add(Gold(r.config, r.sourceType, r.sourceOptions, r.name, r.md5));
mtklein62bd1a62015-01-27 14:46:26 -0800251}
252
253static void gather_gold() {
254 if (!FLAGS_readPath.isEmpty()) {
255 SkString path(FLAGS_readPath[0]);
256 path.append("/dm.json");
257 if (!JsonWriter::ReadJson(path.c_str(), add_gold)) {
258 fail(SkStringPrintf("Couldn't read %s for golden results.", path.c_str()));
259 }
260 }
261}
262
263/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
264
borenet09ed4802015-04-03 14:15:33 -0700265static SkTHashSet<SkString> gUninterestingHashes;
266
267static void gather_uninteresting_hashes() {
268 if (!FLAGS_uninterestingHashesFile.isEmpty()) {
269 SkAutoTUnref<SkData> data(SkData::NewFromFileName(FLAGS_uninterestingHashesFile[0]));
mtkleincc334b32015-09-22 11:43:53 -0700270 if (!data) {
mtkleinc41fd922016-03-08 09:01:39 -0800271 info("WARNING: unable to read uninteresting hashes from %s\n",
272 FLAGS_uninterestingHashesFile[0]);
mtkleincc334b32015-09-22 11:43:53 -0700273 return;
274 }
borenet09ed4802015-04-03 14:15:33 -0700275 SkTArray<SkString> hashes;
276 SkStrSplit((const char*)data->data(), "\n", &hashes);
277 for (const SkString& hash : hashes) {
278 gUninterestingHashes.add(hash);
279 }
mtkleinc41fd922016-03-08 09:01:39 -0800280 info("FYI: loaded %d distinct uninteresting hashes from %d lines\n",
281 gUninterestingHashes.count(), hashes.count());
borenet09ed4802015-04-03 14:15:33 -0700282 }
283}
284
285/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
286
mtkleine0effd62015-07-29 06:37:28 -0700287struct TaggedSrc : public SkAutoTDelete<Src> {
mtklein3eed7dd2016-02-24 19:07:07 -0800288 SkString tag;
289 SkString options;
mtkleine0effd62015-07-29 06:37:28 -0700290};
291
292struct TaggedSink : public SkAutoTDelete<Sink> {
kkinnunen3e980c32015-12-23 01:33:00 -0800293 SkString tag;
djsollen54416de2015-04-03 07:24:48 -0700294};
mtklein748ca3b2015-01-15 10:56:12 -0800295
296static const bool kMemcpyOK = true;
297
mtkleine0effd62015-07-29 06:37:28 -0700298static SkTArray<TaggedSrc, kMemcpyOK> gSrcs;
299static SkTArray<TaggedSink, kMemcpyOK> gSinks;
mtklein748ca3b2015-01-15 10:56:12 -0800300
mtklein6393c062015-04-27 08:45:01 -0700301static bool in_shard() {
302 static int N = 0;
303 return N++ % FLAGS_shards == FLAGS_shard;
304}
305
mtklein3eed7dd2016-02-24 19:07:07 -0800306static void push_src(const char* tag, ImplicitString options, Src* s) {
mtklein748ca3b2015-01-15 10:56:12 -0800307 SkAutoTDelete<Src> src(s);
mtklein6393c062015-04-27 08:45:01 -0700308 if (in_shard() &&
mtklein3eed7dd2016-02-24 19:07:07 -0800309 FLAGS_src.contains(tag) &&
mtklein748ca3b2015-01-15 10:56:12 -0800310 !SkCommandLineFlags::ShouldSkip(FLAGS_match, src->name().c_str())) {
mtkleine0effd62015-07-29 06:37:28 -0700311 TaggedSrc& s = gSrcs.push_back();
mtklein748ca3b2015-01-15 10:56:12 -0800312 s.reset(src.detach());
313 s.tag = tag;
djsollen54416de2015-04-03 07:24:48 -0700314 s.options = options;
mtklein114c3cd2015-01-15 10:15:02 -0800315 }
mtklein748ca3b2015-01-15 10:56:12 -0800316}
mtklein114c3cd2015-01-15 10:15:02 -0800317
msarett9e707a02015-09-01 14:57:57 -0700318static void push_codec_src(Path path, CodecSrc::Mode mode, CodecSrc::DstColorType dstColorType,
scroggoc5560be2016-02-03 09:42:42 -0800319 SkAlphaType dstAlphaType, float scale) {
scroggoe4499842016-02-25 11:03:47 -0800320 if (FLAGS_simpleCodec) {
321 if (mode != CodecSrc::kCodec_Mode || dstColorType != CodecSrc::kGetFromCanvas_DstColorType
322 || scale != 1.0f)
323 // Only decode in the simple case.
324 return;
325 }
msarett9e707a02015-09-01 14:57:57 -0700326 SkString folder;
327 switch (mode) {
328 case CodecSrc::kCodec_Mode:
329 folder.append("codec");
330 break;
msarettbb25b532016-01-13 09:31:39 -0800331 case CodecSrc::kCodecZeroInit_Mode:
332 folder.append("codec_zero_init");
333 break;
msarett9e707a02015-09-01 14:57:57 -0700334 case CodecSrc::kScanline_Mode:
335 folder.append("scanline");
336 break;
msarett9e707a02015-09-01 14:57:57 -0700337 case CodecSrc::kStripe_Mode:
338 folder.append("stripe");
339 break;
msarett91c22b22016-02-22 12:27:46 -0800340 case CodecSrc::kCroppedScanline_Mode:
341 folder.append("crop");
342 break;
msarett9e707a02015-09-01 14:57:57 -0700343 case CodecSrc::kSubset_Mode:
msarett36c37962015-09-02 13:20:52 -0700344 folder.append("codec_subset");
msarett9e707a02015-09-01 14:57:57 -0700345 break;
346 }
347
348 switch (dstColorType) {
349 case CodecSrc::kGrayscale_Always_DstColorType:
350 folder.append("_kGray8");
351 break;
352 case CodecSrc::kIndex8_Always_DstColorType:
353 folder.append("_kIndex8");
354 break;
355 default:
356 break;
357 }
358
scroggoc5560be2016-02-03 09:42:42 -0800359 switch (dstAlphaType) {
360 case kOpaque_SkAlphaType:
361 folder.append("_opaque");
362 break;
363 case kPremul_SkAlphaType:
364 folder.append("_premul");
365 break;
366 case kUnpremul_SkAlphaType:
367 folder.append("_unpremul");
368 break;
369 default:
370 break;
371 }
372
msarett9e707a02015-09-01 14:57:57 -0700373 if (1.0f != scale) {
374 folder.appendf("_%.3f", scale);
375 }
376
scroggoc5560be2016-02-03 09:42:42 -0800377 CodecSrc* src = new CodecSrc(path, mode, dstColorType, dstAlphaType, scale);
msarett9e707a02015-09-01 14:57:57 -0700378 push_src("image", folder, src);
379}
380
msarett3d9d7a72015-10-21 10:27:10 -0700381static void push_android_codec_src(Path path, AndroidCodecSrc::Mode mode,
scroggoc5560be2016-02-03 09:42:42 -0800382 CodecSrc::DstColorType dstColorType, SkAlphaType dstAlphaType, int sampleSize) {
msarett3d9d7a72015-10-21 10:27:10 -0700383 SkString folder;
384 switch (mode) {
385 case AndroidCodecSrc::kFullImage_Mode:
386 folder.append("scaled_codec");
387 break;
388 case AndroidCodecSrc::kDivisor_Mode:
389 folder.append("scaled_codec_divisor");
390 break;
391 }
392
393 switch (dstColorType) {
394 case CodecSrc::kGrayscale_Always_DstColorType:
395 folder.append("_kGray8");
396 break;
397 case CodecSrc::kIndex8_Always_DstColorType:
398 folder.append("_kIndex8");
399 break;
400 default:
401 break;
402 }
403
scroggoc5560be2016-02-03 09:42:42 -0800404 switch (dstAlphaType) {
405 case kOpaque_SkAlphaType:
406 folder.append("_opaque");
407 break;
408 case kPremul_SkAlphaType:
409 folder.append("_premul");
410 break;
msarett9e9444c2016-02-03 12:39:10 -0800411 case kUnpremul_SkAlphaType:
412 folder.append("_unpremul");
413 break;
scroggoc5560be2016-02-03 09:42:42 -0800414 default:
415 break;
416 }
417
msarett3d9d7a72015-10-21 10:27:10 -0700418 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800419 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700420 }
421
scroggoc5560be2016-02-03 09:42:42 -0800422 AndroidCodecSrc* src = new AndroidCodecSrc(path, mode, dstColorType, dstAlphaType, sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700423 push_src("image", folder, src);
424}
425
msarett18976312016-03-09 14:20:58 -0800426static void push_image_gen_src(Path path, ImageGenSrc::Mode mode, SkAlphaType alphaType, bool isGpu)
427{
428 SkString folder;
429 switch (mode) {
430 case ImageGenSrc::kCodec_Mode:
431 folder.append("gen_codec");
432 break;
433 case ImageGenSrc::kPlatform_Mode:
434 folder.append("gen_platform");
435 break;
436 }
437
438 if (isGpu) {
439 folder.append("_gpu");
440 } else {
441 switch (alphaType) {
442 case kOpaque_SkAlphaType:
443 folder.append("_opaque");
444 break;
445 case kPremul_SkAlphaType:
446 folder.append("_premul");
447 break;
448 case kUnpremul_SkAlphaType:
449 folder.append("_unpremul");
450 break;
451 default:
452 break;
453 }
454 }
455
456 ImageGenSrc* src = new ImageGenSrc(path, mode, alphaType, isGpu);
457 push_src("image", folder, src);
458}
459
msarett438b2ad2015-04-09 12:43:10 -0700460static void push_codec_srcs(Path path) {
461 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
462 if (!encoded) {
mtkleinc41fd922016-03-08 09:01:39 -0800463 info("Couldn't read %s.", path.c_str());
msarett438b2ad2015-04-09 12:43:10 -0700464 return;
465 }
466 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
halcanary96fcdcc2015-08-27 07:41:13 -0700467 if (nullptr == codec.get()) {
mtkleinc41fd922016-03-08 09:01:39 -0800468 info("Couldn't create codec for %s.", path.c_str());
msarett438b2ad2015-04-09 12:43:10 -0700469 return;
470 }
471
msarett36c37962015-09-02 13:20:52 -0700472 // Native Scales
emmaleer8f4ba762015-08-14 07:44:46 -0700473 // 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 -0700474 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 -0700475
msarett91c22b22016-02-22 12:27:46 -0800476 SkTArray<CodecSrc::Mode> nativeModes;
477 nativeModes.push_back(CodecSrc::kCodec_Mode);
478 nativeModes.push_back(CodecSrc::kCodecZeroInit_Mode);
msarett91c22b22016-02-22 12:27:46 -0800479 switch (codec->getEncodedFormat()) {
480 case SkEncodedFormat::kJPEG_SkEncodedFormat:
481 nativeModes.push_back(CodecSrc::kScanline_Mode);
482 nativeModes.push_back(CodecSrc::kStripe_Mode);
483 nativeModes.push_back(CodecSrc::kCroppedScanline_Mode);
484 break;
485 case SkEncodedFormat::kWEBP_SkEncodedFormat:
486 nativeModes.push_back(CodecSrc::kSubset_Mode);
487 break;
yujieqin7a307df2016-03-11 07:32:33 -0800488 case SkEncodedFormat::kDNG_SkEncodedFormat:
msarettb65e6042016-02-23 05:37:25 -0800489 break;
msarett91c22b22016-02-22 12:27:46 -0800490 default:
491 nativeModes.push_back(CodecSrc::kScanline_Mode);
492 nativeModes.push_back(CodecSrc::kStripe_Mode);
493 break;
494 }
msarett9e707a02015-09-01 14:57:57 -0700495
msarett91c22b22016-02-22 12:27:46 -0800496 SkTArray<CodecSrc::DstColorType> colorTypes;
497 colorTypes.push_back(CodecSrc::kGetFromCanvas_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700498 switch (codec->getInfo().colorType()) {
499 case kGray_8_SkColorType:
msarett91c22b22016-02-22 12:27:46 -0800500 colorTypes.push_back(CodecSrc::kGrayscale_Always_DstColorType);
msarett55f7bdd2016-02-16 13:24:54 -0800501 if (kWBMP_SkEncodedFormat == codec->getEncodedFormat()) {
msarett91c22b22016-02-22 12:27:46 -0800502 colorTypes.push_back(CodecSrc::kIndex8_Always_DstColorType);
msarett55f7bdd2016-02-16 13:24:54 -0800503 }
msarett36c37962015-09-02 13:20:52 -0700504 break;
505 case kIndex_8_SkColorType:
msarett91c22b22016-02-22 12:27:46 -0800506 colorTypes.push_back(CodecSrc::kIndex8_Always_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700507 break;
508 default:
msarett36c37962015-09-02 13:20:52 -0700509 break;
510 }
msarett9e707a02015-09-01 14:57:57 -0700511
scroggoc5560be2016-02-03 09:42:42 -0800512 SkTArray<SkAlphaType> alphaModes;
513 alphaModes.push_back(kPremul_SkAlphaType);
msarett9e9444c2016-02-03 12:39:10 -0800514 alphaModes.push_back(kUnpremul_SkAlphaType);
scroggoc5560be2016-02-03 09:42:42 -0800515 if (codec->getInfo().alphaType() == kOpaque_SkAlphaType) {
516 alphaModes.push_back(kOpaque_SkAlphaType);
517 }
msarettb714fb02016-01-22 14:46:42 -0800518
519 for (CodecSrc::Mode mode : nativeModes) {
msarettb714fb02016-01-22 14:46:42 -0800520 for (float scale : nativeScales) {
msarett91c22b22016-02-22 12:27:46 -0800521 for (CodecSrc::DstColorType colorType : colorTypes) {
scroggoc5560be2016-02-03 09:42:42 -0800522 for (SkAlphaType alphaType : alphaModes) {
msarett91c22b22016-02-22 12:27:46 -0800523 // Only test kCroppedScanline_Mode when the alpha type is opaque. The test is
524 // slow and won't be interestingly different with different alpha types.
525 if (CodecSrc::kCroppedScanline_Mode == mode &&
526 kOpaque_SkAlphaType != alphaType) {
527 continue;
528 }
529
530 push_codec_src(path, mode, colorType, alphaType, scale);
scroggoc5560be2016-02-03 09:42:42 -0800531 }
msarett9e707a02015-09-01 14:57:57 -0700532 }
533 }
msarett0a242972015-06-11 14:27:27 -0700534 }
msarett36c37962015-09-02 13:20:52 -0700535
scroggoe4499842016-02-25 11:03:47 -0800536 if (FLAGS_simpleCodec) {
537 return;
538 }
539
halcanary6950de62015-11-07 05:29:00 -0800540 // https://bug.skia.org/4428
msarett33c76232015-11-16 13:43:40 -0800541 bool subset = false;
542 // The following image types are supported by BitmapRegionDecoder,
543 // so we will test full image decodes and subset decodes.
msarettbe8216a2015-12-04 08:00:50 -0800544 static const char* const exts[] = {
msarett3d9d7a72015-10-21 10:27:10 -0700545 "jpg", "jpeg", "png", "webp",
546 "JPG", "JPEG", "PNG", "WEBP",
547 };
msarettbe8216a2015-12-04 08:00:50 -0800548 for (const char* ext : exts) {
msarett3d9d7a72015-10-21 10:27:10 -0700549 if (path.endsWith(ext)) {
msarett33c76232015-11-16 13:43:40 -0800550 subset = true;
msarett3d9d7a72015-10-21 10:27:10 -0700551 break;
552 }
553 }
msarett33c76232015-11-16 13:43:40 -0800554
msarett3d9d7a72015-10-21 10:27:10 -0700555 const int sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
msarett36c37962015-09-02 13:20:52 -0700556
msarett3d9d7a72015-10-21 10:27:10 -0700557 for (int sampleSize : sampleSizes) {
msarett91c22b22016-02-22 12:27:46 -0800558 for (CodecSrc::DstColorType colorType : colorTypes) {
scroggoc5560be2016-02-03 09:42:42 -0800559 for (SkAlphaType alphaType : alphaModes) {
msarett91c22b22016-02-22 12:27:46 -0800560 push_android_codec_src(path, AndroidCodecSrc::kFullImage_Mode, colorType,
scroggoc5560be2016-02-03 09:42:42 -0800561 alphaType, sampleSize);
562 if (subset) {
msarett91c22b22016-02-22 12:27:46 -0800563 push_android_codec_src(path, AndroidCodecSrc::kDivisor_Mode, colorType,
scroggoc5560be2016-02-03 09:42:42 -0800564 alphaType, sampleSize);
565 }
msarett3d9d7a72015-10-21 10:27:10 -0700566 }
msarett36c37962015-09-02 13:20:52 -0700567 }
568 }
msarett18976312016-03-09 14:20:58 -0800569
570 static const char* const rawExts[] = {
571 "arw", "cr2", "dng", "nef", "nrw", "orf", "raf", "rw2", "pef", "srw",
572 "ARW", "CR2", "DNG", "NEF", "NRW", "ORF", "RAF", "RW2", "PEF", "SRW",
573 };
574
575 // There is not currently a reason to test RAW images on image generator.
576 // If we want to enable these tests, we will need to fix skbug.com/5079.
577 for (const char* ext : rawExts) {
578 if (path.endsWith(ext)) {
579 return;
580 }
581 }
582
583 // Push image generator GPU test.
584 // FIXME: The gpu backend does not draw kGray sources correctly. (skbug.com/4822)
585 if (kGray_8_SkColorType != codec->getInfo().colorType()) {
586 push_image_gen_src(path, ImageGenSrc::kCodec_Mode, codec->getInfo().alphaType(), true);
587 }
588
589 // Push image generator CPU tests.
590 for (SkAlphaType alphaType : alphaModes) {
591 push_image_gen_src(path, ImageGenSrc::kCodec_Mode, alphaType, false);
592
593#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
594 if (kWEBP_SkEncodedFormat != codec->getEncodedFormat() &&
595 kWBMP_SkEncodedFormat != codec->getEncodedFormat() &&
596 kUnpremul_SkAlphaType != alphaType)
597 {
598 push_image_gen_src(path, ImageGenSrc::kPlatform_Mode, alphaType, false);
599 }
600#endif
601 }
msarett438b2ad2015-04-09 12:43:10 -0700602}
603
msarett5cb48852015-11-06 08:56:32 -0800604static bool brd_color_type_supported(SkBitmapRegionDecoder::Strategy strategy,
msaretta5783ae2015-09-08 15:35:32 -0700605 CodecSrc::DstColorType dstColorType) {
606 switch (strategy) {
msarett5cb48852015-11-06 08:56:32 -0800607 case SkBitmapRegionDecoder::kCanvas_Strategy:
msaretta5783ae2015-09-08 15:35:32 -0700608 if (CodecSrc::kGetFromCanvas_DstColorType == dstColorType) {
609 return true;
610 }
611 return false;
msarett5cb48852015-11-06 08:56:32 -0800612 case SkBitmapRegionDecoder::kAndroidCodec_Strategy:
msarett26ad17b2015-10-22 07:29:19 -0700613 switch (dstColorType) {
614 case CodecSrc::kGetFromCanvas_DstColorType:
615 case CodecSrc::kIndex8_Always_DstColorType:
616 case CodecSrc::kGrayscale_Always_DstColorType:
617 return true;
618 default:
619 return false;
620 }
msaretta5783ae2015-09-08 15:35:32 -0700621 default:
622 SkASSERT(false);
623 return false;
624 }
625}
626
msarett5cb48852015-11-06 08:56:32 -0800627static void push_brd_src(Path path, SkBitmapRegionDecoder::Strategy strategy,
msaretta5783ae2015-09-08 15:35:32 -0700628 CodecSrc::DstColorType dstColorType, BRDSrc::Mode mode, uint32_t sampleSize) {
629 SkString folder;
630 switch (strategy) {
msarett5cb48852015-11-06 08:56:32 -0800631 case SkBitmapRegionDecoder::kCanvas_Strategy:
msaretta5783ae2015-09-08 15:35:32 -0700632 folder.append("brd_canvas");
633 break;
msarett5cb48852015-11-06 08:56:32 -0800634 case SkBitmapRegionDecoder::kAndroidCodec_Strategy:
msarett26ad17b2015-10-22 07:29:19 -0700635 folder.append("brd_android_codec");
636 break;
msaretta5783ae2015-09-08 15:35:32 -0700637 default:
638 SkASSERT(false);
639 return;
640 }
641
642 switch (mode) {
643 case BRDSrc::kFullImage_Mode:
644 break;
645 case BRDSrc::kDivisor_Mode:
646 folder.append("_divisor");
647 break;
648 default:
649 SkASSERT(false);
650 return;
651 }
652
653 switch (dstColorType) {
654 case CodecSrc::kGetFromCanvas_DstColorType:
655 break;
656 case CodecSrc::kIndex8_Always_DstColorType:
657 folder.append("_kIndex");
658 break;
659 case CodecSrc::kGrayscale_Always_DstColorType:
660 folder.append("_kGray");
661 break;
662 default:
663 SkASSERT(false);
664 return;
665 }
666
667 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800668 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700669 }
670
671 BRDSrc* src = new BRDSrc(path, strategy, mode, dstColorType, sampleSize);
672 push_src("image", folder, src);
673}
674
675static void push_brd_srcs(Path path) {
676
msarett5cb48852015-11-06 08:56:32 -0800677 const SkBitmapRegionDecoder::Strategy strategies[] = {
678 SkBitmapRegionDecoder::kCanvas_Strategy,
msarett5cb48852015-11-06 08:56:32 -0800679 SkBitmapRegionDecoder::kAndroidCodec_Strategy,
msaretta5783ae2015-09-08 15:35:32 -0700680 };
681
scroggo501b7342015-11-03 07:55:11 -0800682 // Test on a variety of sampleSizes, making sure to include:
683 // - 2, 4, and 8, which are natively supported by jpeg
684 // - multiples of 2 which are not divisible by 4 (analogous for 4)
685 // - larger powers of two, since BRD clients generally use powers of 2
686 // We will only produce output for the larger sizes on large images.
687 const uint32_t sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 24, 32, 64 };
msarett6efbe052015-09-11 09:01:16 -0700688
msaretta5783ae2015-09-08 15:35:32 -0700689 // We will only test to one backend (8888), but we will test all of the
690 // color types that we need to decode to on this backend.
691 const CodecSrc::DstColorType dstColorTypes[] = {
msarett26ad17b2015-10-22 07:29:19 -0700692 CodecSrc::kGetFromCanvas_DstColorType,
693 CodecSrc::kIndex8_Always_DstColorType,
694 CodecSrc::kGrayscale_Always_DstColorType,
msaretta5783ae2015-09-08 15:35:32 -0700695 };
696
697 const BRDSrc::Mode modes[] = {
msarett26ad17b2015-10-22 07:29:19 -0700698 BRDSrc::kFullImage_Mode,
699 BRDSrc::kDivisor_Mode,
msaretta5783ae2015-09-08 15:35:32 -0700700 };
701
msarett5cb48852015-11-06 08:56:32 -0800702 for (SkBitmapRegionDecoder::Strategy strategy : strategies) {
msarett6efbe052015-09-11 09:01:16 -0700703 for (uint32_t sampleSize : sampleSizes) {
msarett6efbe052015-09-11 09:01:16 -0700704 for (CodecSrc::DstColorType dstColorType : dstColorTypes) {
705 if (brd_color_type_supported(strategy, dstColorType)) {
706 for (BRDSrc::Mode mode : modes) {
msaretta5783ae2015-09-08 15:35:32 -0700707 push_brd_src(path, strategy, dstColorType, mode, sampleSize);
708 }
709 }
710 }
711 }
712 }
713}
714
715static bool brd_supported(const char* ext) {
msarett8c8f22a2015-04-01 06:58:48 -0700716 static const char* const exts[] = {
msaretta5783ae2015-09-08 15:35:32 -0700717 "jpg", "jpeg", "png", "webp",
718 "JPG", "JPEG", "PNG", "WEBP",
msarett8c8f22a2015-04-01 06:58:48 -0700719 };
720
721 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
722 if (0 == strcmp(exts[i], ext)) {
723 return true;
724 }
725 }
726 return false;
scroggo9b77ddd2015-03-19 06:03:39 -0700727}
728
scroggo86737142016-02-03 12:19:11 -0800729static bool gather_srcs() {
mtklein748ca3b2015-01-15 10:56:12 -0800730 for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) {
djsollen54416de2015-04-03 07:24:48 -0700731 push_src("gm", "", new GMSrc(r->factory()));
mtklein748ca3b2015-01-15 10:56:12 -0800732 }
halcanaryfc37ad12015-01-30 07:31:19 -0800733 for (int i = 0; i < FLAGS_skps.count(); i++) {
734 const char* path = FLAGS_skps[i];
735 if (sk_isdir(path)) {
736 SkOSFile::Iter it(path, "skp");
737 for (SkString file; it.next(&file); ) {
djsollen54416de2015-04-03 07:24:48 -0700738 push_src("skp", "", new SKPSrc(SkOSPath::Join(path, file.c_str())));
halcanaryfc37ad12015-01-30 07:31:19 -0800739 }
740 } else {
djsollen54416de2015-04-03 07:24:48 -0700741 push_src("skp", "", new SKPSrc(path));
mtklein114c3cd2015-01-15 10:15:02 -0800742 }
743 }
scroggo86737142016-02-03 12:19:11 -0800744
745 SkTArray<SkString> images;
746 if (!CollectImages(&images)) {
747 return false;
748 }
749
750 for (auto image : images) {
751 push_codec_srcs(image);
scroggoe4499842016-02-25 11:03:47 -0800752 if (FLAGS_simpleCodec) {
753 continue;
754 }
755
mtklein21eaf3b2016-02-08 12:39:59 -0800756 const char* ext = strrchr(image.c_str(), '.');
757 if (ext && brd_supported(ext+1)) {
scroggo86737142016-02-03 12:19:11 -0800758 push_brd_srcs(image);
mtklein709d2c32015-01-15 08:30:25 -0800759 }
mtklein709d2c32015-01-15 08:30:25 -0800760 }
scroggo86737142016-02-03 12:19:11 -0800761
762 return true;
mtklein709d2c32015-01-15 08:30:25 -0800763}
764
kkinnunen3e980c32015-12-23 01:33:00 -0800765static void push_sink(const SkCommandLineConfig& config, Sink* s) {
rmistry0f515bd2015-12-22 10:22:26 -0800766 SkAutoTDelete<Sink> sink(s);
kkinnunen3e980c32015-12-23 01:33:00 -0800767
mtkleine0effd62015-07-29 06:37:28 -0700768 // Try a simple Src as a canary. If it fails, skip this sink.
mtklein748ca3b2015-01-15 10:56:12 -0800769 struct : public Src {
mtkleine0effd62015-07-29 06:37:28 -0700770 Error draw(SkCanvas* c) const override {
771 c->drawRect(SkRect::MakeWH(1,1), SkPaint());
772 return "";
773 }
mtklein36352bf2015-03-25 18:17:31 -0700774 SkISize size() const override { return SkISize::Make(16, 16); }
mtkleine0effd62015-07-29 06:37:28 -0700775 Name name() const override { return "justOneRect"; }
776 } justOneRect;
mtklein114c3cd2015-01-15 10:15:02 -0800777
mtklein748ca3b2015-01-15 10:56:12 -0800778 SkBitmap bitmap;
779 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800780 SkString log;
mtkleine0effd62015-07-29 06:37:28 -0700781 Error err = sink->draw(justOneRect, &bitmap, &stream, &log);
mtklein4089ef72015-03-05 08:40:28 -0800782 if (err.isFatal()) {
mtkleinc41fd922016-03-08 09:01:39 -0800783 info("Could not run %s: %s\n", config.getTag().c_str(), err.c_str());
mtklein05641a52015-04-21 10:49:13 -0700784 exit(1);
mtklein114c3cd2015-01-15 10:15:02 -0800785 }
786
mtkleine0effd62015-07-29 06:37:28 -0700787 TaggedSink& ts = gSinks.push_back();
mtklein748ca3b2015-01-15 10:56:12 -0800788 ts.reset(sink.detach());
kkinnunen3e980c32015-12-23 01:33:00 -0800789 ts.tag = config.getTag();
mtklein748ca3b2015-01-15 10:56:12 -0800790}
791
792static bool gpu_supported() {
793#if SK_SUPPORT_GPU
794 return FLAGS_gpu;
795#else
796 return false;
797#endif
798}
kkinnunen9ebc3f02015-12-21 23:48:13 -0800799
kkinnunen3e980c32015-12-23 01:33:00 -0800800static Sink* create_sink(const SkCommandLineConfig* config) {
801#if SK_SUPPORT_GPU
802 if (gpu_supported()) {
803 if (const SkCommandLineConfigGpu* gpuConfig = config->asConfigGpu()) {
804 GrContextFactory::GLContextType contextType = gpuConfig->getContextType();
805 GrContextFactory::GLContextOptions contextOptions =
806 GrContextFactory::kNone_GLContextOptions;
807 if (gpuConfig->getUseNVPR()) {
808 contextOptions = static_cast<GrContextFactory::GLContextOptions>(
809 contextOptions | GrContextFactory::kEnableNVPR_GLContextOptions);
810 }
811 GrContextFactory testFactory;
812 if (!testFactory.get(contextType, contextOptions)) {
mtkleinc41fd922016-03-08 09:01:39 -0800813 info("WARNING: can not create GPU context for config '%s'. "
814 "GM tests will be skipped.\n", gpuConfig->getTag().c_str());
kkinnunen3e980c32015-12-23 01:33:00 -0800815 return nullptr;
816 }
817 return new GPUSink(contextType, contextOptions, gpuConfig->getSamples(),
brianosmand93c1202016-03-10 07:49:08 -0800818 gpuConfig->getUseDIText(), gpuConfig->getColorType(),
819 gpuConfig->getProfileType(), FLAGS_gpu_threading);
kkinnunen3e980c32015-12-23 01:33:00 -0800820 }
821 }
822#endif
823
824#define SINK(t, sink, ...) if (config->getBackend().equals(t)) { return new sink(__VA_ARGS__); }
825
tomhudsoneebc39a2015-02-23 12:18:05 -0800826#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
827 SINK("hwui", HWUISink);
828#endif
829
mtklein748ca3b2015-01-15 10:56:12 -0800830 if (FLAGS_cpu) {
831 SINK("565", RasterSink, kRGB_565_SkColorType);
832 SINK("8888", RasterSink, kN32_SkColorType);
mtklein27c3fdd2016-02-26 14:43:21 -0800833 SINK("srgb", RasterSink, kN32_SkColorType, kSRGB_SkColorProfileType);
834 SINK("f16", RasterSink, kRGBA_F16_SkColorType);
halcanaryc11c62f2015-09-28 11:51:54 -0700835 SINK("pdf", PDFSink, "Pdfium");
836 SINK("pdf_poppler", PDFSink, "Poppler");
mtklein9c3f17d2015-01-28 11:35:18 -0800837 SINK("skp", SKPSink);
mtklein8a4527e2015-01-31 20:00:58 -0800838 SINK("svg", SVGSink);
839 SINK("null", NullSink);
halcanary47ef4d52015-03-03 09:13:09 -0800840 SINK("xps", XPSSink);
mtklein748ca3b2015-01-15 10:56:12 -0800841 }
842#undef SINK
halcanary96fcdcc2015-08-27 07:41:13 -0700843 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800844}
845
kkinnunen3e980c32015-12-23 01:33:00 -0800846static Sink* create_via(const SkString& tag, Sink* wrapped) {
847#define VIA(t, via, ...) if (tag.equals(t)) { return new via(__VA_ARGS__); }
halcanary96fcdcc2015-08-27 07:41:13 -0700848 VIA("twice", ViaTwice, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700849 VIA("serialize", ViaSerialization, wrapped);
mtklein4a34ecb2016-01-08 10:19:35 -0800850 VIA("pic", ViaPicture, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700851 VIA("2ndpic", ViaSecondPicture, wrapped);
mtkleind31c13d2015-05-05 12:59:56 -0700852 VIA("sp", ViaSingletonPictures, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700853 VIA("tiles", ViaTiles, 256, 256, nullptr, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800854 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
halcanary7a76f9c2016-02-03 11:53:18 -0800855 VIA("mojo", ViaMojo, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800856
mtkleind603b222015-02-17 11:13:33 -0800857 if (FLAGS_matrix.count() == 4) {
mtklein748ca3b2015-01-15 10:56:12 -0800858 SkMatrix m;
mtkleind603b222015-02-17 11:13:33 -0800859 m.reset();
860 m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
861 m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
862 m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
863 m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
864 VIA("matrix", ViaMatrix, m, wrapped);
865 VIA("upright", ViaUpright, m, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800866 }
tomhudson64de1e12015-03-05 08:01:07 -0800867
868#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
869 VIA("androidsdk", ViaAndroidSDK, wrapped);
870#endif
871
mtklein748ca3b2015-01-15 10:56:12 -0800872#undef VIA
halcanary96fcdcc2015-08-27 07:41:13 -0700873 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800874}
875
mtklein748ca3b2015-01-15 10:56:12 -0800876static void gather_sinks() {
kkinnunen3e980c32015-12-23 01:33:00 -0800877 SkCommandLineConfigArray configs;
878 ParseConfigs(FLAGS_config, &configs);
879 for (int i = 0; i < configs.count(); i++) {
880 const SkCommandLineConfig& config = *configs[i];
881 Sink* sink = create_sink(&config);
882 if (sink == nullptr) {
mtkleinc41fd922016-03-08 09:01:39 -0800883 info("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
884 config.getTag().c_str());
kkinnunen3e980c32015-12-23 01:33:00 -0800885 continue;
886 }
mtklein748ca3b2015-01-15 10:56:12 -0800887
kkinnunen3e980c32015-12-23 01:33:00 -0800888 const SkTArray<SkString>& parts = config.getViaParts();
889 for (int j = parts.count(); j-- > 0;) {
890 const SkString& part = parts[j];
891 Sink* next = create_via(part, sink);
halcanary96fcdcc2015-08-27 07:41:13 -0700892 if (next == nullptr) {
mtkleinc41fd922016-03-08 09:01:39 -0800893 info("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
894 part.c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800895 delete sink;
halcanary96fcdcc2015-08-27 07:41:13 -0700896 sink = nullptr;
mtklein748ca3b2015-01-15 10:56:12 -0800897 break;
898 }
899 sink = next;
900 }
901 if (sink) {
902 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800903 }
904 }
905}
mtklein709d2c32015-01-15 08:30:25 -0800906
mtkleina5114d72015-08-24 13:27:01 -0700907static bool dump_png(SkBitmap bitmap, const char* path, const char* md5) {
908 const int w = bitmap.width(),
909 h = bitmap.height();
mtklein27c3fdd2016-02-26 14:43:21 -0800910 // PNG wants unpremultiplied 8-bit RGBA pixels (16-bit could work fine too).
911 // We leave the gamma of these bytes unspecified, to continue the status quo,
912 // which we think generally is to interpret them as sRGB.
mtkleina5114d72015-08-24 13:27:01 -0700913
mtklein27c3fdd2016-02-26 14:43:21 -0800914 SkAutoTMalloc<uint32_t> rgba(w*h);
915
916 if (bitmap. colorType() == kN32_SkColorType &&
917 bitmap.profileType() == kSRGB_SkColorProfileType) {
918 // These are premul sRGB 8-bit pixels in SkPMColor order.
919 // We want unpremul sRGB 8-bit pixels in RGBA order. We'll get there via floats.
920 bitmap.lockPixels();
921 auto px = (const uint32_t*)bitmap.getPixels();
922 if (!px) {
mtkleina5114d72015-08-24 13:27:01 -0700923 return false;
924 }
mtklein27c3fdd2016-02-26 14:43:21 -0800925 for (int i = 0; i < w*h; i++) {
926 Sk4f fs = Sk4f_fromS32(px[i]); // Convert up to linear floats.
927 #if defined(SK_PMCOLOR_IS_BGRA)
928 fs = SkNx_shuffle<2,1,0,3>(fs); // Shuffle to RGBA, if not there already.
929 #endif
930 float invA = 1.0f / fs[3];
931 fs = fs * Sk4f(invA, invA, invA, 1); // Unpremultiply.
932 rgba[i] = Sk4f_toS32(fs); // Pack down to sRGB bytes.
933 }
mtkleina5114d72015-08-24 13:27:01 -0700934
mtklein27c3fdd2016-02-26 14:43:21 -0800935 } else if (bitmap.colorType() == kRGBA_F16_SkColorType) {
936 // These are premul linear half-float pixels in RGBA order.
937 // We want unpremul sRGB 8-bit pixels in RGBA order. We'll get there via floats.
938 bitmap.lockPixels();
939 auto px = (const uint64_t*)bitmap.getPixels();
940 if (!px) {
941 return false;
942 }
943 for (int i = 0; i < w*h; i++) {
944 Sk4f fs = SkHalfToFloat_01(px[i]); // Convert up to linear floats.
945 float invA = 1.0f / fs[3];
946 fs = fs * Sk4f(invA, invA, invA, 1); // Unpremultiply.
947 rgba[i] = Sk4f_toS32(fs); // Pack down to sRGB bytes.
948 }
949
950
951 } else {
952 // We "should" gamma correct in here but we don't.
953 // We want Gold to show exactly what our clients are seeing, broken gamma.
954
955 // Convert smaller formats up to premul linear 8-bit (in SkPMColor order).
956 if (bitmap.colorType() != kN32_SkColorType) {
957 SkBitmap n32;
958 if (!bitmap.copyTo(&n32, kN32_SkColorType)) {
959 return false;
960 }
961 bitmap = n32;
962 }
963
964 // Convert premul linear 8-bit to unpremul linear 8-bit RGBA.
965 if (!bitmap.readPixels(SkImageInfo::Make(w,h, kRGBA_8888_SkColorType,
966 kUnpremul_SkAlphaType),
967 rgba, 4*w, 0,0)) {
968 return false;
969 }
mtkleina5114d72015-08-24 13:27:01 -0700970 }
971
972 // We don't need bitmap anymore. Might as well drop our ref.
mtkleinfccb77d2015-08-24 14:13:29 -0700973 bitmap.reset();
mtkleina5114d72015-08-24 13:27:01 -0700974
mtkleinc64137c2015-08-25 10:56:08 -0700975 FILE* f = fopen(path, "wb");
mtkleina5114d72015-08-24 13:27:01 -0700976 if (!f) { return false; }
977
978 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
979 if (!png) {
980 fclose(f);
981 return false;
982 }
983
984 png_infop info = png_create_info_struct(png);
985 if (!info) {
986 png_destroy_write_struct(&png, &info);
987 fclose(f);
988 return false;
989 }
990
mtkleind69ece62015-09-10 10:37:44 -0700991 SkString description;
992 description.append("Key: ");
993 for (int i = 0; i < FLAGS_key.count(); i++) {
994 description.appendf("%s ", FLAGS_key[i]);
995 }
996 description.append("Properties: ");
997 for (int i = 0; i < FLAGS_properties.count(); i++) {
998 description.appendf("%s ", FLAGS_properties[i]);
999 }
1000 description.appendf("MD5: %s", md5);
1001
mtkleina5114d72015-08-24 13:27:01 -07001002 png_text text[2];
1003 text[0].key = (png_charp)"Author";
1004 text[0].text = (png_charp)"DM dump_png()";
1005 text[0].compression = PNG_TEXT_COMPRESSION_NONE;
1006 text[1].key = (png_charp)"Description";
mtkleind69ece62015-09-10 10:37:44 -07001007 text[1].text = (png_charp)description.c_str();
mtkleina5114d72015-08-24 13:27:01 -07001008 text[1].compression = PNG_TEXT_COMPRESSION_NONE;
1009 png_set_text(png, info, text, 2);
1010
1011 png_init_io(png, f);
1012 png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8,
1013 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
1014 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
1015 png_write_info(png, info);
1016 for (int j = 0; j < h; j++) {
1017 png_bytep row = (png_bytep)(rgba.get() + w*j);
1018 png_write_rows(png, &row, 1);
1019 }
1020 png_write_end(png, info);
1021
1022 png_destroy_write_struct(&png, &info);
1023 fclose(f);
1024 return true;
1025}
1026
mtkleina2ef6422015-01-15 13:44:22 -08001027static bool match(const char* needle, const char* haystack) {
halcanary96fcdcc2015-08-27 07:41:13 -07001028 return 0 == strcmp("_", needle) || nullptr != strstr(haystack, needle);
mtkleina2ef6422015-01-15 13:44:22 -08001029}
1030
mtklein3eed7dd2016-02-24 19:07:07 -08001031static bool is_blacklisted(const char* sink, const char* src,
1032 const char* srcOptions, const char* name) {
mtklein0d243ff2015-04-03 07:51:00 -07001033 for (int i = 0; i < FLAGS_blacklist.count() - 3; i += 4) {
mtkleina2ef6422015-01-15 13:44:22 -08001034 if (match(FLAGS_blacklist[i+0], sink) &&
djsollen54416de2015-04-03 07:24:48 -07001035 match(FLAGS_blacklist[i+1], src) &&
1036 match(FLAGS_blacklist[i+2], srcOptions) &&
1037 match(FLAGS_blacklist[i+3], name)) {
mtklein3eed7dd2016-02-24 19:07:07 -08001038 return true;
mtkleina2ef6422015-01-15 13:44:22 -08001039 }
1040 }
mtklein3eed7dd2016-02-24 19:07:07 -08001041 return false;
mtkleina2ef6422015-01-15 13:44:22 -08001042}
1043
mtkleincd50bca2016-01-05 06:20:20 -08001044// Even when a Task Sink reports to be non-threadsafe (e.g. GPU), we know things like
1045// .png encoding are definitely thread safe. This lets us offload that work to CPU threads.
1046static SkTaskGroup gDefinitelyThreadSafeWork;
1047
mtklein748ca3b2015-01-15 10:56:12 -08001048// The finest-grained unit of work we can run: draw a single Src into a single Sink,
1049// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
1050struct Task {
mtkleine0effd62015-07-29 06:37:28 -07001051 Task(const TaggedSrc& src, const TaggedSink& sink) : src(src), sink(sink) {}
1052 const TaggedSrc& src;
1053 const TaggedSink& sink;
mtklein748ca3b2015-01-15 10:56:12 -08001054
mtklein21eaf3b2016-02-08 12:39:59 -08001055 static void Run(const Task& task) {
1056 SkString name = task.src->name();
mtkleine0effd62015-07-29 06:37:28 -07001057
mtkleinb9eb4ac2015-02-02 18:26:03 -08001058 SkString log;
mtklein3eed7dd2016-02-24 19:07:07 -08001059 if (!FLAGS_dryRun) {
mtklein748ca3b2015-01-15 10:56:12 -08001060 SkBitmap bitmap;
1061 SkDynamicMemoryWStream stream;
mtklein3eed7dd2016-02-24 19:07:07 -08001062 start(task.sink.tag.c_str(), task.src.tag.c_str(),
1063 task.src.options.c_str(), name.c_str());
mtklein21eaf3b2016-02-08 12:39:59 -08001064 Error err = task.sink->draw(*task.src, &bitmap, &stream, &log);
mtkleinb3b13b72016-03-07 13:20:52 -08001065 if (!log.isEmpty()) {
mtkleinc41fd922016-03-08 09:01:39 -08001066 info("%s %s %s %s:\n%s\n", task.sink.tag.c_str()
1067 , task.src.tag.c_str()
1068 , task.src.options.c_str()
1069 , name.c_str()
1070 , log.c_str());
mtkleinb3b13b72016-03-07 13:20:52 -08001071 }
mtklein748ca3b2015-01-15 10:56:12 -08001072 if (!err.isEmpty()) {
mtklein4089ef72015-03-05 08:40:28 -08001073 if (err.isFatal()) {
djsollen54416de2015-04-03 07:24:48 -07001074 fail(SkStringPrintf("%s %s %s %s: %s",
mtklein21eaf3b2016-02-08 12:39:59 -08001075 task.sink.tag.c_str(),
1076 task.src.tag.c_str(),
1077 task.src.options.c_str(),
mtklein4089ef72015-03-05 08:40:28 -08001078 name.c_str(),
1079 err.c_str()));
mtkleinb37cb412015-03-18 05:27:14 -07001080 } else {
mtklein3eed7dd2016-02-24 19:07:07 -08001081 done(task.sink.tag.c_str(), task.src.tag.c_str(),
1082 task.src.options.c_str(), name.c_str());
mtkleinba6ada72016-01-21 09:39:35 -08001083 return;
mtklein4089ef72015-03-05 08:40:28 -08001084 }
mtklein748ca3b2015-01-15 10:56:12 -08001085 }
mtklein62bd1a62015-01-27 14:46:26 -08001086
mtkleincd50bca2016-01-05 06:20:20 -08001087 // We're likely switching threads here, so we must capture by value, [=] or [foo,bar].
1088 SkStreamAsset* data = stream.detachAsStream();
1089 gDefinitelyThreadSafeWork.add([task,name,bitmap,data]{
1090 SkAutoTDelete<SkStreamAsset> ownedData(data);
1091
1092 // Why doesn't the copy constructor do this when we have pre-locked pixels?
1093 bitmap.lockPixels();
1094
1095 SkString md5;
1096 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
1097 SkMD5 hash;
1098 if (data->getLength()) {
1099 hash.writeStream(data, data->getLength());
1100 data->rewind();
mtklein38408462015-07-08 07:25:27 -07001101 } else {
mtkleincd50bca2016-01-05 06:20:20 -08001102 // If we're BGRA (Linux, Windows), swizzle over to RGBA (Mac, Android).
1103 // This helps eliminate multiple 0-pixel-diff hashes on gold.skia.org.
1104 // (Android's general slow speed breaks the tie arbitrarily in RGBA's favor.)
1105 // We might consider promoting 565 to RGBA too.
1106 if (bitmap.colorType() == kBGRA_8888_SkColorType) {
1107 SkBitmap swizzle;
1108 SkAssertResult(bitmap.copyTo(&swizzle, kRGBA_8888_SkColorType));
1109 hash.write(swizzle.getPixels(), swizzle.getSize());
1110 } else {
1111 hash.write(bitmap.getPixels(), bitmap.getSize());
1112 }
1113 }
1114 SkMD5::Digest digest;
1115 hash.finish(digest);
1116 for (int i = 0; i < 16; i++) {
1117 md5.appendf("%02x", digest.data[i]);
mtklein38408462015-07-08 07:25:27 -07001118 }
mtklein62bd1a62015-01-27 14:46:26 -08001119 }
mtklein62bd1a62015-01-27 14:46:26 -08001120
mtkleincd50bca2016-01-05 06:20:20 -08001121 if (!FLAGS_readPath.isEmpty() &&
mtklein3eed7dd2016-02-24 19:07:07 -08001122 !gGold.contains(Gold(task.sink.tag, task.src.tag,
1123 task.src.options, name, md5))) {
mtkleincd50bca2016-01-05 06:20:20 -08001124 fail(SkStringPrintf("%s not found for %s %s %s %s in %s",
1125 md5.c_str(),
mtklein21eaf3b2016-02-08 12:39:59 -08001126 task.sink.tag.c_str(),
1127 task.src.tag.c_str(),
1128 task.src.options.c_str(),
mtkleincd50bca2016-01-05 06:20:20 -08001129 name.c_str(),
1130 FLAGS_readPath[0]));
mtklein748ca3b2015-01-15 10:56:12 -08001131 }
mtkleincd50bca2016-01-05 06:20:20 -08001132
1133 if (!FLAGS_writePath.isEmpty()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001134 const char* ext = task.sink->fileExtension();
mtkleincd50bca2016-01-05 06:20:20 -08001135 if (data->getLength()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001136 WriteToDisk(task, md5, ext, data, data->getLength(), nullptr);
mtkleincd50bca2016-01-05 06:20:20 -08001137 SkASSERT(bitmap.drawsNothing());
1138 } else if (!bitmap.drawsNothing()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001139 WriteToDisk(task, md5, ext, nullptr, 0, &bitmap);
mtkleincd50bca2016-01-05 06:20:20 -08001140 }
1141 }
1142 });
mtklein748ca3b2015-01-15 10:56:12 -08001143 }
mtklein3eed7dd2016-02-24 19:07:07 -08001144 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 -08001145 }
1146
1147 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -08001148 SkString md5,
1149 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -08001150 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -08001151 const SkBitmap* bitmap) {
mtklein409d4702016-02-29 07:38:01 -08001152 bool gammaCorrect = false;
1153 if (bitmap) {
1154 gammaCorrect = bitmap->profileType() == kSRGB_SkColorProfileType
1155 || bitmap-> colorType() == kRGBA_F16_SkColorType;
1156 }
1157
mtklein748ca3b2015-01-15 10:56:12 -08001158 JsonWriter::BitmapResult result;
djsollen54416de2015-04-03 07:24:48 -07001159 result.name = task.src->name();
mtklein3eed7dd2016-02-24 19:07:07 -08001160 result.config = task.sink.tag;
djsollen54416de2015-04-03 07:24:48 -07001161 result.sourceType = task.src.tag;
1162 result.sourceOptions = task.src.options;
1163 result.ext = ext;
mtklein409d4702016-02-29 07:38:01 -08001164 result.gammaCorrect = gammaCorrect;
djsollen54416de2015-04-03 07:24:48 -07001165 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -08001166 JsonWriter::AddBitmapResult(result);
1167
mtkleinb0531a72015-04-07 13:38:48 -07001168 // If an MD5 is uninteresting, we want it noted in the JSON file,
1169 // but don't want to dump it out as a .png (or whatever ext is).
1170 if (gUninterestingHashes.contains(md5)) {
1171 return;
1172 }
1173
mtklein748ca3b2015-01-15 10:56:12 -08001174 const char* dir = FLAGS_writePath[0];
1175 if (0 == strcmp(dir, "@")) { // Needed for iOS.
1176 dir = FLAGS_resourcePath[0];
1177 }
1178 sk_mkdir(dir);
1179
1180 SkString path;
1181 if (FLAGS_nameByHash) {
1182 path = SkOSPath::Join(dir, result.md5.c_str());
1183 path.append(".");
1184 path.append(ext);
1185 if (sk_exists(path.c_str())) {
1186 return; // Content-addressed. If it exists already, we're done.
1187 }
1188 } else {
kkinnunen3e980c32015-12-23 01:33:00 -08001189 path = SkOSPath::Join(dir, task.sink.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001190 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001191 path = SkOSPath::Join(path.c_str(), task.src.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001192 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001193 if (strcmp(task.src.options.c_str(), "") != 0) {
1194 path = SkOSPath::Join(path.c_str(), task.src.options.c_str());
djsollen54416de2015-04-03 07:24:48 -07001195 sk_mkdir(path.c_str());
1196 }
mtklein748ca3b2015-01-15 10:56:12 -08001197 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
1198 path.append(".");
1199 path.append(ext);
1200 }
1201
mtklein748ca3b2015-01-15 10:56:12 -08001202 if (bitmap) {
mtkleina5114d72015-08-24 13:27:01 -07001203 if (!dump_png(*bitmap, path.c_str(), result.md5.c_str())) {
mtklein748ca3b2015-01-15 10:56:12 -08001204 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
1205 return;
1206 }
1207 } else {
mtkleina5114d72015-08-24 13:27:01 -07001208 SkFILEWStream file(path.c_str());
1209 if (!file.isValid()) {
1210 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
1211 return;
1212 }
mtklein748ca3b2015-01-15 10:56:12 -08001213 if (!file.writeStream(data, len)) {
1214 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
1215 return;
1216 }
1217 }
1218 }
1219};
1220
mtklein748ca3b2015-01-15 10:56:12 -08001221/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1222
1223// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
1224
mtklein21eaf3b2016-02-08 12:39:59 -08001225static SkTDArray<skiatest::Test> gParallelTests, gSerialTests;
mtklein748ca3b2015-01-15 10:56:12 -08001226
1227static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -08001228 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -08001229 return;
1230 }
mtklein6393c062015-04-27 08:45:01 -07001231 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r; r = r->next()) {
1232 if (!in_shard()) {
1233 continue;
1234 }
halcanary87f3ba42015-01-20 09:30:20 -08001235 // Despite its name, factory() is returning a reference to
1236 // link-time static const POD data.
1237 const skiatest::Test& test = r->factory();
1238 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -08001239 continue;
1240 }
halcanary87f3ba42015-01-20 09:30:20 -08001241 if (test.needsGpu && gpu_supported()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001242 (FLAGS_gpu_threading ? gParallelTests : gSerialTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -08001243 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein21eaf3b2016-02-08 12:39:59 -08001244 gParallelTests.push(test);
mtklein82d28432015-01-15 12:46:02 -08001245 }
mtklein748ca3b2015-01-15 10:56:12 -08001246 }
1247}
1248
mtklein21eaf3b2016-02-08 12:39:59 -08001249static void run_test(skiatest::Test test) {
halcanary87f3ba42015-01-20 09:30:20 -08001250 struct : public skiatest::Reporter {
mtklein36352bf2015-03-25 18:17:31 -07001251 void reportFailed(const skiatest::Failure& failure) override {
halcanary87f3ba42015-01-20 09:30:20 -08001252 fail(failure.toString());
1253 JsonWriter::AddTestFailure(failure);
1254 }
mtklein36352bf2015-03-25 18:17:31 -07001255 bool allowExtendedTest() const override {
halcanary87f3ba42015-01-20 09:30:20 -08001256 return FLAGS_pathOpsExtended;
1257 }
mtklein36352bf2015-03-25 18:17:31 -07001258 bool verbose() const override { return FLAGS_veryVerbose; }
halcanary87f3ba42015-01-20 09:30:20 -08001259 } reporter;
djsollen824996a2015-06-12 12:06:22 -07001260
mtklein3eed7dd2016-02-24 19:07:07 -08001261 if (!FLAGS_dryRun && !is_blacklisted("_", "tests", "_", test.name)) {
mtklein21eaf3b2016-02-08 12:39:59 -08001262 start("unit", "test", "", test.name);
mtklein55e88b22015-01-21 15:50:13 -08001263 GrContextFactory factory;
mtklein21eaf3b2016-02-08 12:39:59 -08001264 test.proc(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -08001265 }
mtklein3eed7dd2016-02-24 19:07:07 -08001266 done("unit", "test", "", test.name);
mtklein748ca3b2015-01-15 10:56:12 -08001267}
1268
1269/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1270
mtkleinafae30a2016-02-24 12:28:32 -08001271DEFINE_int32(status_sec, 15, "Print status this often (and if we crash).");
1272
1273SkThread* start_status_thread() {
1274 auto thread = new SkThread([] (void*) {
1275 for (;;) {
1276 print_status();
1277 #if defined(SK_BUILD_FOR_WIN)
1278 Sleep(FLAGS_status_sec * 1000);
1279 #else
1280 sleep(FLAGS_status_sec);
1281 #endif
mtklein2e1c47e2015-03-12 07:16:56 -07001282 }
mtkleinafae30a2016-02-24 12:28:32 -08001283 });
1284 thread->start();
1285 return thread;
mtkleinde6fc2b2015-03-12 06:28:54 -07001286}
1287
caryclark83ca6282015-06-10 09:31:09 -07001288#define PORTABLE_FONT_PREFIX "Toy Liberation "
1289
1290static SkTypeface* create_from_name(const char familyName[], SkTypeface::Style style) {
1291 if (familyName && strlen(familyName) > sizeof(PORTABLE_FONT_PREFIX)
1292 && !strncmp(familyName, PORTABLE_FONT_PREFIX, sizeof(PORTABLE_FONT_PREFIX) - 1)) {
caryclark1818acb2015-07-24 12:09:25 -07001293 return sk_tool_utils::create_portable_typeface(familyName, style);
caryclark83ca6282015-06-10 09:31:09 -07001294 }
halcanary96fcdcc2015-08-27 07:41:13 -07001295 return nullptr;
caryclark83ca6282015-06-10 09:31:09 -07001296}
1297
1298#undef PORTABLE_FONT_PREFIX
1299
1300extern SkTypeface* (*gCreateTypefaceDelegate)(const char [], SkTypeface::Style );
1301
jcgregorio3b27ade2014-11-13 08:06:40 -08001302int dm_main();
caryclark17f0b6d2014-07-22 10:15:34 -07001303int dm_main() {
mtklein246ba3a2016-02-23 10:39:36 -08001304 setup_crash_handler();
mtkleinafae30a2016-02-24 12:28:32 -08001305
mtklein51c8cfc2016-03-11 12:59:09 -08001306 if (FLAGS_verbose && !FLAGS_writePath.isEmpty()) {
1307 sk_mkdir(FLAGS_writePath[0]);
1308 gVLog = freopen(SkOSPath::Join(FLAGS_writePath[0], "verbose.log").c_str(), "w", stderr);
mtkleinc41fd922016-03-08 09:01:39 -08001309 }
1310
mtklein5286f022016-01-22 08:18:14 -08001311 JsonWriter::DumpJson(); // It's handy for the bots to assume this is ~never missing.
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +00001312 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -07001313 SkTaskGroup::Enabler enabled(FLAGS_threads);
caryclark83ca6282015-06-10 09:31:09 -07001314 gCreateTypefaceDelegate = &create_from_name;
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +00001315
halcanary7d571242016-02-24 17:59:16 -08001316 {
1317 SkString testResourcePath = GetResourcePath("color_wheel.png");
1318 SkFILEStream testResource(testResourcePath.c_str());
1319 if (!testResource.isValid()) {
mtkleinc41fd922016-03-08 09:01:39 -08001320 info("Some resources are missing. Do you need to set --resourcePath?\n");
halcanary7d571242016-02-24 17:59:16 -08001321 }
1322 }
mtklein62bd1a62015-01-27 14:46:26 -08001323 gather_gold();
borenet09ed4802015-04-03 14:15:33 -07001324 gather_uninteresting_hashes();
mtklein62bd1a62015-01-27 14:46:26 -08001325
scroggo86737142016-02-03 12:19:11 -08001326 if (!gather_srcs()) {
1327 return 1;
1328 }
mtklein748ca3b2015-01-15 10:56:12 -08001329 gather_sinks();
1330 gather_tests();
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +00001331
mtklein21eaf3b2016-02-08 12:39:59 -08001332 gPending = gSrcs.count() * gSinks.count() + gParallelTests.count() + gSerialTests.count();
mtkleinc41fd922016-03-08 09:01:39 -08001333 info("%d srcs * %d sinks + %d tests == %d tasks",
1334 gSrcs.count(), gSinks.count(), gParallelTests.count() + gSerialTests.count(), gPending);
mtkleinafae30a2016-02-24 12:28:32 -08001335 SkAutoTDelete<SkThread> statusThread(start_status_thread());
mtklein748ca3b2015-01-15 10:56:12 -08001336
mtklein21eaf3b2016-02-08 12:39:59 -08001337 // Kick off as much parallel work as we can, making note of any serial work we'll need to do.
1338 SkTaskGroup parallel;
1339 SkTArray<Task> serial;
1340
1341 for (auto& sink : gSinks)
1342 for (auto& src : gSrcs) {
mtklein3eed7dd2016-02-24 19:07:07 -08001343 if (src->veto(sink->flags()) ||
1344 is_blacklisted(sink.tag.c_str(), src.tag.c_str(),
1345 src.options.c_str(), src->name().c_str())) {
mtklein15923c92016-02-29 10:14:38 -08001346 SkAutoTAcquire<SkSpinlock> lock(gMutex);
mtklein3eed7dd2016-02-24 19:07:07 -08001347 gPending--;
1348 continue;
1349 }
1350
mtklein21eaf3b2016-02-08 12:39:59 -08001351 Task task(src, sink);
1352 if (src->serial() || sink->serial()) {
1353 serial.push_back(task);
1354 } else {
1355 parallel.add([task] { Task::Run(task); });
mtklein748ca3b2015-01-15 10:56:12 -08001356 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001357 }
mtklein21eaf3b2016-02-08 12:39:59 -08001358 for (auto test : gParallelTests) {
1359 parallel.add([test] { run_test(test); });
mtklein55e88b22015-01-21 15:50:13 -08001360 }
mtklein21eaf3b2016-02-08 12:39:59 -08001361
1362 // With the parallel work running, run serial tasks and tests here on main thread.
1363 for (auto task : serial) { Task::Run(task); }
1364 for (auto test : gSerialTests) { run_test(test); }
1365
1366 // Wait for any remaining parallel work to complete (including any spun off of serial tasks).
1367 parallel.wait();
mtkleincd50bca2016-01-05 06:20:20 -08001368 gDefinitelyThreadSafeWork.wait();
1369
mtkleinda884c42016-02-24 18:00:23 -08001370 // We'd better have run everything.
1371 SkASSERT(gPending == 0);
mtkleine027f172016-02-26 15:53:06 -08001372 // Make sure we've flushed all our results to disk.
1373 JsonWriter::DumpJson();
mtkleinda884c42016-02-24 18:00:23 -08001374
mtklein748ca3b2015-01-15 10:56:12 -08001375 // At this point we're back in single-threaded land.
caryclarkf53ce802015-06-15 06:48:30 -07001376 sk_tool_utils::release_portable_typefaces();
mtklein@google.comd36522d2013-10-16 13:02:15 +00001377
mtklein748ca3b2015-01-15 10:56:12 -08001378 if (gFailures.count() > 0) {
mtkleinc41fd922016-03-08 09:01:39 -08001379 info("Failures:\n");
mtklein748ca3b2015-01-15 10:56:12 -08001380 for (int i = 0; i < gFailures.count(); i++) {
mtkleinc41fd922016-03-08 09:01:39 -08001381 info("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001382 }
mtkleinc41fd922016-03-08 09:01:39 -08001383 info("%d failures\n", gFailures.count());
mtklein748ca3b2015-01-15 10:56:12 -08001384 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001385 }
mtkleinafae30a2016-02-24 12:28:32 -08001386
1387#ifdef SK_PDF_IMAGE_STATS
halcanary7a14b312015-10-01 07:28:13 -07001388 SkPDFImageDumpStats();
mtkleinafae30a2016-02-24 12:28:32 -08001389#endif // SK_PDF_IMAGE_STATS
1390
1391 print_status();
mtkleinc41fd922016-03-08 09:01:39 -08001392 info("Finished!\n");
mtklein748ca3b2015-01-15 10:56:12 -08001393 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +00001394}
jcgregorio3b27ade2014-11-13 08:06:40 -08001395
kkinnunen179a8f52015-11-20 13:32:24 -08001396// TODO: currently many GPU tests are declared outside SK_SUPPORT_GPU guards.
1397// Thus we export the empty RunWithGPUTestContexts when SK_SUPPORT_GPU=0.
1398namespace skiatest {
1399namespace {
1400typedef void(*TestWithGrContext)(skiatest::Reporter*, GrContext*);
1401typedef void(*TestWithGrContextAndGLContext)(skiatest::Reporter*, GrContext*, SkGLContext*);
1402#if SK_SUPPORT_GPU
1403template<typename T>
kkinnunen34058002016-01-06 23:49:30 -08001404void call_test(T test, skiatest::Reporter* reporter, const GrContextFactory::ContextInfo& context);
kkinnunen179a8f52015-11-20 13:32:24 -08001405template<>
1406void call_test(TestWithGrContext test, skiatest::Reporter* reporter,
kkinnunen34058002016-01-06 23:49:30 -08001407 const GrContextFactory::ContextInfo& context) {
1408 test(reporter, context.fGrContext);
kkinnunen179a8f52015-11-20 13:32:24 -08001409}
1410template<>
1411void call_test(TestWithGrContextAndGLContext test, skiatest::Reporter* reporter,
kkinnunen34058002016-01-06 23:49:30 -08001412 const GrContextFactory::ContextInfo& context) {
1413 test(reporter, context.fGrContext, context.fGLContext);
kkinnunen179a8f52015-11-20 13:32:24 -08001414}
1415#endif
1416} // namespace
1417
kkinnunen179a8f52015-11-20 13:32:24 -08001418template<typename T>
1419void RunWithGPUTestContexts(T test, GPUTestContexts testContexts, Reporter* reporter,
1420 GrContextFactory* factory) {
1421#if SK_SUPPORT_GPU
kkinnunen3e980c32015-12-23 01:33:00 -08001422 // Iterate over context types, except use "native" instead of explicitly trying OpenGL and
1423 // OpenGL ES. Do not use GLES on desktop, since tests do not account for not fixing
1424 // http://skbug.com/2809
1425 GrContextFactory::GLContextType contextTypes[] = {
1426 GrContextFactory::kNative_GLContextType,
1427#if SK_ANGLE
1428#ifdef SK_BUILD_FOR_WIN
1429 GrContextFactory::kANGLE_GLContextType,
1430#endif
1431 GrContextFactory::kANGLE_GL_GLContextType,
1432#endif
1433#if SK_COMMAND_BUFFER
kkinnunenf655e932016-03-03 07:39:48 -08001434 GrContextFactory::kCommandBuffer_GLContextType,
kkinnunen3e980c32015-12-23 01:33:00 -08001435#endif
1436#if SK_MESA
1437 GrContextFactory::kMESA_GLContextType,
1438#endif
1439 GrContextFactory::kNull_GLContextType,
1440 GrContextFactory::kDebug_GLContextType,
1441 };
1442 static_assert(SK_ARRAY_COUNT(contextTypes) == GrContextFactory::kGLContextTypeCnt - 2,
1443 "Skipping unexpected GLContextType for GPU tests");
1444
1445 for (auto& contextType : contextTypes) {
kkinnunen179a8f52015-11-20 13:32:24 -08001446 int contextSelector = kNone_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001447 if (GrContextFactory::IsRenderingGLContext(contextType)) {
kkinnunen179a8f52015-11-20 13:32:24 -08001448 contextSelector |= kAllRendering_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001449 } else if (contextType == GrContextFactory::kNative_GLContextType) {
kkinnunen179a8f52015-11-20 13:32:24 -08001450 contextSelector |= kNative_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001451 } else if (contextType == GrContextFactory::kNull_GLContextType) {
kkinnunen179a8f52015-11-20 13:32:24 -08001452 contextSelector |= kNull_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001453 } else if (contextType == GrContextFactory::kDebug_GLContextType) {
kkinnunen55eeae92015-12-10 23:19:29 -08001454 contextSelector |= kDebug_GPUTestContexts;
kkinnunen179a8f52015-11-20 13:32:24 -08001455 }
1456 if ((testContexts & contextSelector) == 0) {
1457 continue;
1458 }
kkinnunen34058002016-01-06 23:49:30 -08001459 GrContextFactory::ContextInfo context = factory->getContextInfo(contextType);
1460 if (context.fGrContext) {
kkinnunen5219fd92015-12-10 06:28:13 -08001461 call_test(test, reporter, context);
1462 }
kkinnunen34058002016-01-06 23:49:30 -08001463 context = factory->getContextInfo(contextType,
1464 GrContextFactory::kEnableNVPR_GLContextOptions);
1465 if (context.fGrContext) {
kkinnunen179a8f52015-11-20 13:32:24 -08001466 call_test(test, reporter, context);
1467 }
1468 }
1469#endif
1470}
1471
1472template
1473void RunWithGPUTestContexts<TestWithGrContext>(TestWithGrContext test,
1474 GPUTestContexts testContexts,
1475 Reporter* reporter,
1476 GrContextFactory* factory);
1477template
1478void RunWithGPUTestContexts<TestWithGrContextAndGLContext>(TestWithGrContextAndGLContext test,
1479 GPUTestContexts testContexts,
1480 Reporter* reporter,
1481 GrContextFactory* factory);
1482} // namespace skiatest
1483
borenet48087572015-04-02 12:16:36 -07001484#if !defined(SK_BUILD_FOR_IOS)
jcgregorio3b27ade2014-11-13 08:06:40 -08001485int main(int argc, char** argv) {
1486 SkCommandLineFlags::Parse(argc, argv);
1487 return dm_main();
1488}
1489#endif