blob: 1e3e5d021ce945b9dd7c20e1913f12c339aae26a [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;
msarettb714fb02016-01-22 14:46:42 -0800346 case CodecSrc::kGen_Mode:
347 folder.append("gen");
348 break;
msarett9e707a02015-09-01 14:57:57 -0700349 }
350
351 switch (dstColorType) {
352 case CodecSrc::kGrayscale_Always_DstColorType:
353 folder.append("_kGray8");
354 break;
355 case CodecSrc::kIndex8_Always_DstColorType:
356 folder.append("_kIndex8");
357 break;
358 default:
359 break;
360 }
361
scroggoc5560be2016-02-03 09:42:42 -0800362 switch (dstAlphaType) {
363 case kOpaque_SkAlphaType:
364 folder.append("_opaque");
365 break;
366 case kPremul_SkAlphaType:
367 folder.append("_premul");
368 break;
369 case kUnpremul_SkAlphaType:
370 folder.append("_unpremul");
371 break;
372 default:
373 break;
374 }
375
msarett9e707a02015-09-01 14:57:57 -0700376 if (1.0f != scale) {
377 folder.appendf("_%.3f", scale);
378 }
379
scroggoc5560be2016-02-03 09:42:42 -0800380 CodecSrc* src = new CodecSrc(path, mode, dstColorType, dstAlphaType, scale);
msarett9e707a02015-09-01 14:57:57 -0700381 push_src("image", folder, src);
382}
383
msarett3d9d7a72015-10-21 10:27:10 -0700384static void push_android_codec_src(Path path, AndroidCodecSrc::Mode mode,
scroggoc5560be2016-02-03 09:42:42 -0800385 CodecSrc::DstColorType dstColorType, SkAlphaType dstAlphaType, int sampleSize) {
msarett3d9d7a72015-10-21 10:27:10 -0700386 SkString folder;
387 switch (mode) {
388 case AndroidCodecSrc::kFullImage_Mode:
389 folder.append("scaled_codec");
390 break;
391 case AndroidCodecSrc::kDivisor_Mode:
392 folder.append("scaled_codec_divisor");
393 break;
394 }
395
396 switch (dstColorType) {
397 case CodecSrc::kGrayscale_Always_DstColorType:
398 folder.append("_kGray8");
399 break;
400 case CodecSrc::kIndex8_Always_DstColorType:
401 folder.append("_kIndex8");
402 break;
403 default:
404 break;
405 }
406
scroggoc5560be2016-02-03 09:42:42 -0800407 switch (dstAlphaType) {
408 case kOpaque_SkAlphaType:
409 folder.append("_opaque");
410 break;
411 case kPremul_SkAlphaType:
412 folder.append("_premul");
413 break;
msarett9e9444c2016-02-03 12:39:10 -0800414 case kUnpremul_SkAlphaType:
415 folder.append("_unpremul");
416 break;
scroggoc5560be2016-02-03 09:42:42 -0800417 default:
418 break;
419 }
420
msarett3d9d7a72015-10-21 10:27:10 -0700421 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800422 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700423 }
424
scroggoc5560be2016-02-03 09:42:42 -0800425 AndroidCodecSrc* src = new AndroidCodecSrc(path, mode, dstColorType, dstAlphaType, sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700426 push_src("image", folder, src);
427}
428
msarett438b2ad2015-04-09 12:43:10 -0700429static void push_codec_srcs(Path path) {
430 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
431 if (!encoded) {
mtkleinc41fd922016-03-08 09:01:39 -0800432 info("Couldn't read %s.", path.c_str());
msarett438b2ad2015-04-09 12:43:10 -0700433 return;
434 }
435 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
halcanary96fcdcc2015-08-27 07:41:13 -0700436 if (nullptr == codec.get()) {
mtkleinc41fd922016-03-08 09:01:39 -0800437 info("Couldn't create codec for %s.", path.c_str());
msarett438b2ad2015-04-09 12:43:10 -0700438 return;
439 }
440
msarett36c37962015-09-02 13:20:52 -0700441 // Native Scales
emmaleer8f4ba762015-08-14 07:44:46 -0700442 // 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 -0700443 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 -0700444
msarett91c22b22016-02-22 12:27:46 -0800445 SkTArray<CodecSrc::Mode> nativeModes;
446 nativeModes.push_back(CodecSrc::kCodec_Mode);
447 nativeModes.push_back(CodecSrc::kCodecZeroInit_Mode);
448 nativeModes.push_back(CodecSrc::kGen_Mode);
449 switch (codec->getEncodedFormat()) {
450 case SkEncodedFormat::kJPEG_SkEncodedFormat:
451 nativeModes.push_back(CodecSrc::kScanline_Mode);
452 nativeModes.push_back(CodecSrc::kStripe_Mode);
453 nativeModes.push_back(CodecSrc::kCroppedScanline_Mode);
454 break;
455 case SkEncodedFormat::kWEBP_SkEncodedFormat:
456 nativeModes.push_back(CodecSrc::kSubset_Mode);
457 break;
msarettb65e6042016-02-23 05:37:25 -0800458 case SkEncodedFormat::kRAW_SkEncodedFormat:
459 break;
msarett91c22b22016-02-22 12:27:46 -0800460 default:
461 nativeModes.push_back(CodecSrc::kScanline_Mode);
462 nativeModes.push_back(CodecSrc::kStripe_Mode);
463 break;
464 }
msarett9e707a02015-09-01 14:57:57 -0700465
msarett91c22b22016-02-22 12:27:46 -0800466 SkTArray<CodecSrc::DstColorType> colorTypes;
467 colorTypes.push_back(CodecSrc::kGetFromCanvas_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700468 switch (codec->getInfo().colorType()) {
469 case kGray_8_SkColorType:
msarett91c22b22016-02-22 12:27:46 -0800470 colorTypes.push_back(CodecSrc::kGrayscale_Always_DstColorType);
msarett55f7bdd2016-02-16 13:24:54 -0800471 if (kWBMP_SkEncodedFormat == codec->getEncodedFormat()) {
msarett91c22b22016-02-22 12:27:46 -0800472 colorTypes.push_back(CodecSrc::kIndex8_Always_DstColorType);
msarett55f7bdd2016-02-16 13:24:54 -0800473 }
msarett36c37962015-09-02 13:20:52 -0700474 break;
475 case kIndex_8_SkColorType:
msarett91c22b22016-02-22 12:27:46 -0800476 colorTypes.push_back(CodecSrc::kIndex8_Always_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700477 break;
478 default:
msarett36c37962015-09-02 13:20:52 -0700479 break;
480 }
msarett9e707a02015-09-01 14:57:57 -0700481
scroggoc5560be2016-02-03 09:42:42 -0800482 SkTArray<SkAlphaType> alphaModes;
483 alphaModes.push_back(kPremul_SkAlphaType);
msarett9e9444c2016-02-03 12:39:10 -0800484 alphaModes.push_back(kUnpremul_SkAlphaType);
scroggoc5560be2016-02-03 09:42:42 -0800485 if (codec->getInfo().alphaType() == kOpaque_SkAlphaType) {
486 alphaModes.push_back(kOpaque_SkAlphaType);
487 }
msarettb714fb02016-01-22 14:46:42 -0800488
489 for (CodecSrc::Mode mode : nativeModes) {
490 // SkCodecImageGenerator only runs for the default colorType
491 // recommended by SkCodec. There is no need to generate multiple
492 // tests for different colorTypes.
493 // TODO (msarett): Add scaling support to SkCodecImageGenerator.
494 if (CodecSrc::kGen_Mode == mode) {
495 // FIXME: The gpu backend does not draw kGray sources correctly. (skbug.com/4822)
496 if (kGray_8_SkColorType != codec->getInfo().colorType()) {
scroggoc5560be2016-02-03 09:42:42 -0800497 push_codec_src(path, mode, CodecSrc::kGetFromCanvas_DstColorType,
498 codec->getInfo().alphaType(), 1.0f);
msarettb714fb02016-01-22 14:46:42 -0800499 }
500 continue;
501 }
502
503 for (float scale : nativeScales) {
msarett91c22b22016-02-22 12:27:46 -0800504 for (CodecSrc::DstColorType colorType : colorTypes) {
scroggoc5560be2016-02-03 09:42:42 -0800505 for (SkAlphaType alphaType : alphaModes) {
msarett91c22b22016-02-22 12:27:46 -0800506 // Only test kCroppedScanline_Mode when the alpha type is opaque. The test is
507 // slow and won't be interestingly different with different alpha types.
508 if (CodecSrc::kCroppedScanline_Mode == mode &&
509 kOpaque_SkAlphaType != alphaType) {
510 continue;
511 }
512
513 push_codec_src(path, mode, colorType, alphaType, scale);
scroggoc5560be2016-02-03 09:42:42 -0800514 }
msarett9e707a02015-09-01 14:57:57 -0700515 }
516 }
msarett0a242972015-06-11 14:27:27 -0700517 }
msarett36c37962015-09-02 13:20:52 -0700518
scroggoe4499842016-02-25 11:03:47 -0800519 if (FLAGS_simpleCodec) {
520 return;
521 }
522
halcanary6950de62015-11-07 05:29:00 -0800523 // https://bug.skia.org/4428
msarett33c76232015-11-16 13:43:40 -0800524 bool subset = false;
525 // The following image types are supported by BitmapRegionDecoder,
526 // so we will test full image decodes and subset decodes.
msarettbe8216a2015-12-04 08:00:50 -0800527 static const char* const exts[] = {
msarett3d9d7a72015-10-21 10:27:10 -0700528 "jpg", "jpeg", "png", "webp",
529 "JPG", "JPEG", "PNG", "WEBP",
530 };
msarettbe8216a2015-12-04 08:00:50 -0800531 for (const char* ext : exts) {
msarett3d9d7a72015-10-21 10:27:10 -0700532 if (path.endsWith(ext)) {
msarett33c76232015-11-16 13:43:40 -0800533 subset = true;
msarett3d9d7a72015-10-21 10:27:10 -0700534 break;
535 }
536 }
msarett33c76232015-11-16 13:43:40 -0800537
msarett3d9d7a72015-10-21 10:27:10 -0700538 const int sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
msarett36c37962015-09-02 13:20:52 -0700539
msarett3d9d7a72015-10-21 10:27:10 -0700540 for (int sampleSize : sampleSizes) {
msarett91c22b22016-02-22 12:27:46 -0800541 for (CodecSrc::DstColorType colorType : colorTypes) {
scroggoc5560be2016-02-03 09:42:42 -0800542 for (SkAlphaType alphaType : alphaModes) {
msarett91c22b22016-02-22 12:27:46 -0800543 push_android_codec_src(path, AndroidCodecSrc::kFullImage_Mode, colorType,
scroggoc5560be2016-02-03 09:42:42 -0800544 alphaType, sampleSize);
545 if (subset) {
msarett91c22b22016-02-22 12:27:46 -0800546 push_android_codec_src(path, AndroidCodecSrc::kDivisor_Mode, colorType,
scroggoc5560be2016-02-03 09:42:42 -0800547 alphaType, sampleSize);
548 }
msarett3d9d7a72015-10-21 10:27:10 -0700549 }
msarett36c37962015-09-02 13:20:52 -0700550 }
551 }
msarett438b2ad2015-04-09 12:43:10 -0700552}
553
msarett5cb48852015-11-06 08:56:32 -0800554static bool brd_color_type_supported(SkBitmapRegionDecoder::Strategy strategy,
msaretta5783ae2015-09-08 15:35:32 -0700555 CodecSrc::DstColorType dstColorType) {
556 switch (strategy) {
msarett5cb48852015-11-06 08:56:32 -0800557 case SkBitmapRegionDecoder::kCanvas_Strategy:
msaretta5783ae2015-09-08 15:35:32 -0700558 if (CodecSrc::kGetFromCanvas_DstColorType == dstColorType) {
559 return true;
560 }
561 return false;
msarett5cb48852015-11-06 08:56:32 -0800562 case SkBitmapRegionDecoder::kAndroidCodec_Strategy:
msarett26ad17b2015-10-22 07:29:19 -0700563 switch (dstColorType) {
564 case CodecSrc::kGetFromCanvas_DstColorType:
565 case CodecSrc::kIndex8_Always_DstColorType:
566 case CodecSrc::kGrayscale_Always_DstColorType:
567 return true;
568 default:
569 return false;
570 }
msaretta5783ae2015-09-08 15:35:32 -0700571 default:
572 SkASSERT(false);
573 return false;
574 }
575}
576
msarett5cb48852015-11-06 08:56:32 -0800577static void push_brd_src(Path path, SkBitmapRegionDecoder::Strategy strategy,
msaretta5783ae2015-09-08 15:35:32 -0700578 CodecSrc::DstColorType dstColorType, BRDSrc::Mode mode, uint32_t sampleSize) {
579 SkString folder;
580 switch (strategy) {
msarett5cb48852015-11-06 08:56:32 -0800581 case SkBitmapRegionDecoder::kCanvas_Strategy:
msaretta5783ae2015-09-08 15:35:32 -0700582 folder.append("brd_canvas");
583 break;
msarett5cb48852015-11-06 08:56:32 -0800584 case SkBitmapRegionDecoder::kAndroidCodec_Strategy:
msarett26ad17b2015-10-22 07:29:19 -0700585 folder.append("brd_android_codec");
586 break;
msaretta5783ae2015-09-08 15:35:32 -0700587 default:
588 SkASSERT(false);
589 return;
590 }
591
592 switch (mode) {
593 case BRDSrc::kFullImage_Mode:
594 break;
595 case BRDSrc::kDivisor_Mode:
596 folder.append("_divisor");
597 break;
598 default:
599 SkASSERT(false);
600 return;
601 }
602
603 switch (dstColorType) {
604 case CodecSrc::kGetFromCanvas_DstColorType:
605 break;
606 case CodecSrc::kIndex8_Always_DstColorType:
607 folder.append("_kIndex");
608 break;
609 case CodecSrc::kGrayscale_Always_DstColorType:
610 folder.append("_kGray");
611 break;
612 default:
613 SkASSERT(false);
614 return;
615 }
616
617 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800618 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700619 }
620
621 BRDSrc* src = new BRDSrc(path, strategy, mode, dstColorType, sampleSize);
622 push_src("image", folder, src);
623}
624
625static void push_brd_srcs(Path path) {
626
msarett5cb48852015-11-06 08:56:32 -0800627 const SkBitmapRegionDecoder::Strategy strategies[] = {
628 SkBitmapRegionDecoder::kCanvas_Strategy,
msarett5cb48852015-11-06 08:56:32 -0800629 SkBitmapRegionDecoder::kAndroidCodec_Strategy,
msaretta5783ae2015-09-08 15:35:32 -0700630 };
631
scroggo501b7342015-11-03 07:55:11 -0800632 // Test on a variety of sampleSizes, making sure to include:
633 // - 2, 4, and 8, which are natively supported by jpeg
634 // - multiples of 2 which are not divisible by 4 (analogous for 4)
635 // - larger powers of two, since BRD clients generally use powers of 2
636 // We will only produce output for the larger sizes on large images.
637 const uint32_t sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 24, 32, 64 };
msarett6efbe052015-09-11 09:01:16 -0700638
msaretta5783ae2015-09-08 15:35:32 -0700639 // We will only test to one backend (8888), but we will test all of the
640 // color types that we need to decode to on this backend.
641 const CodecSrc::DstColorType dstColorTypes[] = {
msarett26ad17b2015-10-22 07:29:19 -0700642 CodecSrc::kGetFromCanvas_DstColorType,
643 CodecSrc::kIndex8_Always_DstColorType,
644 CodecSrc::kGrayscale_Always_DstColorType,
msaretta5783ae2015-09-08 15:35:32 -0700645 };
646
647 const BRDSrc::Mode modes[] = {
msarett26ad17b2015-10-22 07:29:19 -0700648 BRDSrc::kFullImage_Mode,
649 BRDSrc::kDivisor_Mode,
msaretta5783ae2015-09-08 15:35:32 -0700650 };
651
msarett5cb48852015-11-06 08:56:32 -0800652 for (SkBitmapRegionDecoder::Strategy strategy : strategies) {
msarett6efbe052015-09-11 09:01:16 -0700653 for (uint32_t sampleSize : sampleSizes) {
msarett6efbe052015-09-11 09:01:16 -0700654 for (CodecSrc::DstColorType dstColorType : dstColorTypes) {
655 if (brd_color_type_supported(strategy, dstColorType)) {
656 for (BRDSrc::Mode mode : modes) {
msaretta5783ae2015-09-08 15:35:32 -0700657 push_brd_src(path, strategy, dstColorType, mode, sampleSize);
658 }
659 }
660 }
661 }
662 }
663}
664
665static bool brd_supported(const char* ext) {
msarett8c8f22a2015-04-01 06:58:48 -0700666 static const char* const exts[] = {
msaretta5783ae2015-09-08 15:35:32 -0700667 "jpg", "jpeg", "png", "webp",
668 "JPG", "JPEG", "PNG", "WEBP",
msarett8c8f22a2015-04-01 06:58:48 -0700669 };
670
671 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
672 if (0 == strcmp(exts[i], ext)) {
673 return true;
674 }
675 }
676 return false;
scroggo9b77ddd2015-03-19 06:03:39 -0700677}
678
scroggo86737142016-02-03 12:19:11 -0800679static bool gather_srcs() {
mtklein748ca3b2015-01-15 10:56:12 -0800680 for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) {
djsollen54416de2015-04-03 07:24:48 -0700681 push_src("gm", "", new GMSrc(r->factory()));
mtklein748ca3b2015-01-15 10:56:12 -0800682 }
halcanaryfc37ad12015-01-30 07:31:19 -0800683 for (int i = 0; i < FLAGS_skps.count(); i++) {
684 const char* path = FLAGS_skps[i];
685 if (sk_isdir(path)) {
686 SkOSFile::Iter it(path, "skp");
687 for (SkString file; it.next(&file); ) {
djsollen54416de2015-04-03 07:24:48 -0700688 push_src("skp", "", new SKPSrc(SkOSPath::Join(path, file.c_str())));
halcanaryfc37ad12015-01-30 07:31:19 -0800689 }
690 } else {
djsollen54416de2015-04-03 07:24:48 -0700691 push_src("skp", "", new SKPSrc(path));
mtklein114c3cd2015-01-15 10:15:02 -0800692 }
693 }
scroggo86737142016-02-03 12:19:11 -0800694
695 SkTArray<SkString> images;
696 if (!CollectImages(&images)) {
697 return false;
698 }
699
700 for (auto image : images) {
701 push_codec_srcs(image);
scroggoe4499842016-02-25 11:03:47 -0800702 if (FLAGS_simpleCodec) {
703 continue;
704 }
705
mtklein21eaf3b2016-02-08 12:39:59 -0800706 const char* ext = strrchr(image.c_str(), '.');
707 if (ext && brd_supported(ext+1)) {
scroggo86737142016-02-03 12:19:11 -0800708 push_brd_srcs(image);
mtklein709d2c32015-01-15 08:30:25 -0800709 }
mtklein709d2c32015-01-15 08:30:25 -0800710 }
scroggo86737142016-02-03 12:19:11 -0800711
712 return true;
mtklein709d2c32015-01-15 08:30:25 -0800713}
714
kkinnunen3e980c32015-12-23 01:33:00 -0800715static void push_sink(const SkCommandLineConfig& config, Sink* s) {
rmistry0f515bd2015-12-22 10:22:26 -0800716 SkAutoTDelete<Sink> sink(s);
kkinnunen3e980c32015-12-23 01:33:00 -0800717
mtkleine0effd62015-07-29 06:37:28 -0700718 // Try a simple Src as a canary. If it fails, skip this sink.
mtklein748ca3b2015-01-15 10:56:12 -0800719 struct : public Src {
mtkleine0effd62015-07-29 06:37:28 -0700720 Error draw(SkCanvas* c) const override {
721 c->drawRect(SkRect::MakeWH(1,1), SkPaint());
722 return "";
723 }
mtklein36352bf2015-03-25 18:17:31 -0700724 SkISize size() const override { return SkISize::Make(16, 16); }
mtkleine0effd62015-07-29 06:37:28 -0700725 Name name() const override { return "justOneRect"; }
726 } justOneRect;
mtklein114c3cd2015-01-15 10:15:02 -0800727
mtklein748ca3b2015-01-15 10:56:12 -0800728 SkBitmap bitmap;
729 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800730 SkString log;
mtkleine0effd62015-07-29 06:37:28 -0700731 Error err = sink->draw(justOneRect, &bitmap, &stream, &log);
mtklein4089ef72015-03-05 08:40:28 -0800732 if (err.isFatal()) {
mtkleinc41fd922016-03-08 09:01:39 -0800733 info("Could not run %s: %s\n", config.getTag().c_str(), err.c_str());
mtklein05641a52015-04-21 10:49:13 -0700734 exit(1);
mtklein114c3cd2015-01-15 10:15:02 -0800735 }
736
mtkleine0effd62015-07-29 06:37:28 -0700737 TaggedSink& ts = gSinks.push_back();
mtklein748ca3b2015-01-15 10:56:12 -0800738 ts.reset(sink.detach());
kkinnunen3e980c32015-12-23 01:33:00 -0800739 ts.tag = config.getTag();
mtklein748ca3b2015-01-15 10:56:12 -0800740}
741
742static bool gpu_supported() {
743#if SK_SUPPORT_GPU
744 return FLAGS_gpu;
745#else
746 return false;
747#endif
748}
kkinnunen9ebc3f02015-12-21 23:48:13 -0800749
kkinnunen3e980c32015-12-23 01:33:00 -0800750static Sink* create_sink(const SkCommandLineConfig* config) {
751#if SK_SUPPORT_GPU
752 if (gpu_supported()) {
753 if (const SkCommandLineConfigGpu* gpuConfig = config->asConfigGpu()) {
754 GrContextFactory::GLContextType contextType = gpuConfig->getContextType();
755 GrContextFactory::GLContextOptions contextOptions =
756 GrContextFactory::kNone_GLContextOptions;
757 if (gpuConfig->getUseNVPR()) {
758 contextOptions = static_cast<GrContextFactory::GLContextOptions>(
759 contextOptions | GrContextFactory::kEnableNVPR_GLContextOptions);
760 }
761 GrContextFactory testFactory;
762 if (!testFactory.get(contextType, contextOptions)) {
mtkleinc41fd922016-03-08 09:01:39 -0800763 info("WARNING: can not create GPU context for config '%s'. "
764 "GM tests will be skipped.\n", gpuConfig->getTag().c_str());
kkinnunen3e980c32015-12-23 01:33:00 -0800765 return nullptr;
766 }
767 return new GPUSink(contextType, contextOptions, gpuConfig->getSamples(),
brianosman744898a2016-03-01 13:44:28 -0800768 gpuConfig->getUseDIText(), FLAGS_gpu_threading);
kkinnunen3e980c32015-12-23 01:33:00 -0800769 }
770 }
771#endif
772
773#define SINK(t, sink, ...) if (config->getBackend().equals(t)) { return new sink(__VA_ARGS__); }
774
tomhudsoneebc39a2015-02-23 12:18:05 -0800775#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
776 SINK("hwui", HWUISink);
777#endif
778
mtklein748ca3b2015-01-15 10:56:12 -0800779 if (FLAGS_cpu) {
780 SINK("565", RasterSink, kRGB_565_SkColorType);
781 SINK("8888", RasterSink, kN32_SkColorType);
mtklein27c3fdd2016-02-26 14:43:21 -0800782 SINK("srgb", RasterSink, kN32_SkColorType, kSRGB_SkColorProfileType);
783 SINK("f16", RasterSink, kRGBA_F16_SkColorType);
halcanaryc11c62f2015-09-28 11:51:54 -0700784 SINK("pdf", PDFSink, "Pdfium");
785 SINK("pdf_poppler", PDFSink, "Poppler");
mtklein9c3f17d2015-01-28 11:35:18 -0800786 SINK("skp", SKPSink);
mtklein8a4527e2015-01-31 20:00:58 -0800787 SINK("svg", SVGSink);
788 SINK("null", NullSink);
halcanary47ef4d52015-03-03 09:13:09 -0800789 SINK("xps", XPSSink);
mtklein748ca3b2015-01-15 10:56:12 -0800790 }
791#undef SINK
halcanary96fcdcc2015-08-27 07:41:13 -0700792 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800793}
794
kkinnunen3e980c32015-12-23 01:33:00 -0800795static Sink* create_via(const SkString& tag, Sink* wrapped) {
796#define VIA(t, via, ...) if (tag.equals(t)) { return new via(__VA_ARGS__); }
halcanary96fcdcc2015-08-27 07:41:13 -0700797 VIA("twice", ViaTwice, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700798 VIA("serialize", ViaSerialization, wrapped);
mtklein4a34ecb2016-01-08 10:19:35 -0800799 VIA("pic", ViaPicture, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700800 VIA("2ndpic", ViaSecondPicture, wrapped);
mtkleind31c13d2015-05-05 12:59:56 -0700801 VIA("sp", ViaSingletonPictures, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700802 VIA("tiles", ViaTiles, 256, 256, nullptr, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800803 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
halcanary7a76f9c2016-02-03 11:53:18 -0800804 VIA("mojo", ViaMojo, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800805
mtkleind603b222015-02-17 11:13:33 -0800806 if (FLAGS_matrix.count() == 4) {
mtklein748ca3b2015-01-15 10:56:12 -0800807 SkMatrix m;
mtkleind603b222015-02-17 11:13:33 -0800808 m.reset();
809 m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
810 m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
811 m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
812 m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
813 VIA("matrix", ViaMatrix, m, wrapped);
814 VIA("upright", ViaUpright, m, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800815 }
tomhudson64de1e12015-03-05 08:01:07 -0800816
817#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
818 VIA("androidsdk", ViaAndroidSDK, wrapped);
819#endif
820
mtklein748ca3b2015-01-15 10:56:12 -0800821#undef VIA
halcanary96fcdcc2015-08-27 07:41:13 -0700822 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800823}
824
mtklein748ca3b2015-01-15 10:56:12 -0800825static void gather_sinks() {
kkinnunen3e980c32015-12-23 01:33:00 -0800826 SkCommandLineConfigArray configs;
827 ParseConfigs(FLAGS_config, &configs);
828 for (int i = 0; i < configs.count(); i++) {
829 const SkCommandLineConfig& config = *configs[i];
830 Sink* sink = create_sink(&config);
831 if (sink == nullptr) {
mtkleinc41fd922016-03-08 09:01:39 -0800832 info("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
833 config.getTag().c_str());
kkinnunen3e980c32015-12-23 01:33:00 -0800834 continue;
835 }
mtklein748ca3b2015-01-15 10:56:12 -0800836
kkinnunen3e980c32015-12-23 01:33:00 -0800837 const SkTArray<SkString>& parts = config.getViaParts();
838 for (int j = parts.count(); j-- > 0;) {
839 const SkString& part = parts[j];
840 Sink* next = create_via(part, sink);
halcanary96fcdcc2015-08-27 07:41:13 -0700841 if (next == nullptr) {
mtkleinc41fd922016-03-08 09:01:39 -0800842 info("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
843 part.c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800844 delete sink;
halcanary96fcdcc2015-08-27 07:41:13 -0700845 sink = nullptr;
mtklein748ca3b2015-01-15 10:56:12 -0800846 break;
847 }
848 sink = next;
849 }
850 if (sink) {
851 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800852 }
853 }
854}
mtklein709d2c32015-01-15 08:30:25 -0800855
mtkleina5114d72015-08-24 13:27:01 -0700856static bool dump_png(SkBitmap bitmap, const char* path, const char* md5) {
857 const int w = bitmap.width(),
858 h = bitmap.height();
mtklein27c3fdd2016-02-26 14:43:21 -0800859 // PNG wants unpremultiplied 8-bit RGBA pixels (16-bit could work fine too).
860 // We leave the gamma of these bytes unspecified, to continue the status quo,
861 // which we think generally is to interpret them as sRGB.
mtkleina5114d72015-08-24 13:27:01 -0700862
mtklein27c3fdd2016-02-26 14:43:21 -0800863 SkAutoTMalloc<uint32_t> rgba(w*h);
864
865 if (bitmap. colorType() == kN32_SkColorType &&
866 bitmap.profileType() == kSRGB_SkColorProfileType) {
867 // These are premul sRGB 8-bit pixels in SkPMColor order.
868 // We want unpremul sRGB 8-bit pixels in RGBA order. We'll get there via floats.
869 bitmap.lockPixels();
870 auto px = (const uint32_t*)bitmap.getPixels();
871 if (!px) {
mtkleina5114d72015-08-24 13:27:01 -0700872 return false;
873 }
mtklein27c3fdd2016-02-26 14:43:21 -0800874 for (int i = 0; i < w*h; i++) {
875 Sk4f fs = Sk4f_fromS32(px[i]); // Convert up to linear floats.
876 #if defined(SK_PMCOLOR_IS_BGRA)
877 fs = SkNx_shuffle<2,1,0,3>(fs); // Shuffle to RGBA, if not there already.
878 #endif
879 float invA = 1.0f / fs[3];
880 fs = fs * Sk4f(invA, invA, invA, 1); // Unpremultiply.
881 rgba[i] = Sk4f_toS32(fs); // Pack down to sRGB bytes.
882 }
mtkleina5114d72015-08-24 13:27:01 -0700883
mtklein27c3fdd2016-02-26 14:43:21 -0800884 } else if (bitmap.colorType() == kRGBA_F16_SkColorType) {
885 // These are premul linear half-float pixels in RGBA order.
886 // We want unpremul sRGB 8-bit pixels in RGBA order. We'll get there via floats.
887 bitmap.lockPixels();
888 auto px = (const uint64_t*)bitmap.getPixels();
889 if (!px) {
890 return false;
891 }
892 for (int i = 0; i < w*h; i++) {
893 Sk4f fs = SkHalfToFloat_01(px[i]); // Convert up to linear floats.
894 float invA = 1.0f / fs[3];
895 fs = fs * Sk4f(invA, invA, invA, 1); // Unpremultiply.
896 rgba[i] = Sk4f_toS32(fs); // Pack down to sRGB bytes.
897 }
898
899
900 } else {
901 // We "should" gamma correct in here but we don't.
902 // We want Gold to show exactly what our clients are seeing, broken gamma.
903
904 // Convert smaller formats up to premul linear 8-bit (in SkPMColor order).
905 if (bitmap.colorType() != kN32_SkColorType) {
906 SkBitmap n32;
907 if (!bitmap.copyTo(&n32, kN32_SkColorType)) {
908 return false;
909 }
910 bitmap = n32;
911 }
912
913 // Convert premul linear 8-bit to unpremul linear 8-bit RGBA.
914 if (!bitmap.readPixels(SkImageInfo::Make(w,h, kRGBA_8888_SkColorType,
915 kUnpremul_SkAlphaType),
916 rgba, 4*w, 0,0)) {
917 return false;
918 }
mtkleina5114d72015-08-24 13:27:01 -0700919 }
920
921 // We don't need bitmap anymore. Might as well drop our ref.
mtkleinfccb77d2015-08-24 14:13:29 -0700922 bitmap.reset();
mtkleina5114d72015-08-24 13:27:01 -0700923
mtkleinc64137c2015-08-25 10:56:08 -0700924 FILE* f = fopen(path, "wb");
mtkleina5114d72015-08-24 13:27:01 -0700925 if (!f) { return false; }
926
927 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
928 if (!png) {
929 fclose(f);
930 return false;
931 }
932
933 png_infop info = png_create_info_struct(png);
934 if (!info) {
935 png_destroy_write_struct(&png, &info);
936 fclose(f);
937 return false;
938 }
939
mtkleind69ece62015-09-10 10:37:44 -0700940 SkString description;
941 description.append("Key: ");
942 for (int i = 0; i < FLAGS_key.count(); i++) {
943 description.appendf("%s ", FLAGS_key[i]);
944 }
945 description.append("Properties: ");
946 for (int i = 0; i < FLAGS_properties.count(); i++) {
947 description.appendf("%s ", FLAGS_properties[i]);
948 }
949 description.appendf("MD5: %s", md5);
950
mtkleina5114d72015-08-24 13:27:01 -0700951 png_text text[2];
952 text[0].key = (png_charp)"Author";
953 text[0].text = (png_charp)"DM dump_png()";
954 text[0].compression = PNG_TEXT_COMPRESSION_NONE;
955 text[1].key = (png_charp)"Description";
mtkleind69ece62015-09-10 10:37:44 -0700956 text[1].text = (png_charp)description.c_str();
mtkleina5114d72015-08-24 13:27:01 -0700957 text[1].compression = PNG_TEXT_COMPRESSION_NONE;
958 png_set_text(png, info, text, 2);
959
960 png_init_io(png, f);
961 png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8,
962 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
963 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
964 png_write_info(png, info);
965 for (int j = 0; j < h; j++) {
966 png_bytep row = (png_bytep)(rgba.get() + w*j);
967 png_write_rows(png, &row, 1);
968 }
969 png_write_end(png, info);
970
971 png_destroy_write_struct(&png, &info);
972 fclose(f);
973 return true;
974}
975
mtkleina2ef6422015-01-15 13:44:22 -0800976static bool match(const char* needle, const char* haystack) {
halcanary96fcdcc2015-08-27 07:41:13 -0700977 return 0 == strcmp("_", needle) || nullptr != strstr(haystack, needle);
mtkleina2ef6422015-01-15 13:44:22 -0800978}
979
mtklein3eed7dd2016-02-24 19:07:07 -0800980static bool is_blacklisted(const char* sink, const char* src,
981 const char* srcOptions, const char* name) {
mtklein0d243ff2015-04-03 07:51:00 -0700982 for (int i = 0; i < FLAGS_blacklist.count() - 3; i += 4) {
mtkleina2ef6422015-01-15 13:44:22 -0800983 if (match(FLAGS_blacklist[i+0], sink) &&
djsollen54416de2015-04-03 07:24:48 -0700984 match(FLAGS_blacklist[i+1], src) &&
985 match(FLAGS_blacklist[i+2], srcOptions) &&
986 match(FLAGS_blacklist[i+3], name)) {
mtklein3eed7dd2016-02-24 19:07:07 -0800987 return true;
mtkleina2ef6422015-01-15 13:44:22 -0800988 }
989 }
mtklein3eed7dd2016-02-24 19:07:07 -0800990 return false;
mtkleina2ef6422015-01-15 13:44:22 -0800991}
992
mtkleincd50bca2016-01-05 06:20:20 -0800993// Even when a Task Sink reports to be non-threadsafe (e.g. GPU), we know things like
994// .png encoding are definitely thread safe. This lets us offload that work to CPU threads.
995static SkTaskGroup gDefinitelyThreadSafeWork;
996
mtklein748ca3b2015-01-15 10:56:12 -0800997// The finest-grained unit of work we can run: draw a single Src into a single Sink,
998// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
999struct Task {
mtkleine0effd62015-07-29 06:37:28 -07001000 Task(const TaggedSrc& src, const TaggedSink& sink) : src(src), sink(sink) {}
1001 const TaggedSrc& src;
1002 const TaggedSink& sink;
mtklein748ca3b2015-01-15 10:56:12 -08001003
mtklein21eaf3b2016-02-08 12:39:59 -08001004 static void Run(const Task& task) {
1005 SkString name = task.src->name();
mtkleine0effd62015-07-29 06:37:28 -07001006
mtkleinb9eb4ac2015-02-02 18:26:03 -08001007 SkString log;
mtklein3eed7dd2016-02-24 19:07:07 -08001008 if (!FLAGS_dryRun) {
mtklein748ca3b2015-01-15 10:56:12 -08001009 SkBitmap bitmap;
1010 SkDynamicMemoryWStream stream;
mtklein3eed7dd2016-02-24 19:07:07 -08001011 start(task.sink.tag.c_str(), task.src.tag.c_str(),
1012 task.src.options.c_str(), name.c_str());
mtklein21eaf3b2016-02-08 12:39:59 -08001013 Error err = task.sink->draw(*task.src, &bitmap, &stream, &log);
mtkleinb3b13b72016-03-07 13:20:52 -08001014 if (!log.isEmpty()) {
mtkleinc41fd922016-03-08 09:01:39 -08001015 info("%s %s %s %s:\n%s\n", task.sink.tag.c_str()
1016 , task.src.tag.c_str()
1017 , task.src.options.c_str()
1018 , name.c_str()
1019 , log.c_str());
mtkleinb3b13b72016-03-07 13:20:52 -08001020 }
mtklein748ca3b2015-01-15 10:56:12 -08001021 if (!err.isEmpty()) {
mtklein4089ef72015-03-05 08:40:28 -08001022 if (err.isFatal()) {
djsollen54416de2015-04-03 07:24:48 -07001023 fail(SkStringPrintf("%s %s %s %s: %s",
mtklein21eaf3b2016-02-08 12:39:59 -08001024 task.sink.tag.c_str(),
1025 task.src.tag.c_str(),
1026 task.src.options.c_str(),
mtklein4089ef72015-03-05 08:40:28 -08001027 name.c_str(),
1028 err.c_str()));
mtkleinb37cb412015-03-18 05:27:14 -07001029 } else {
mtklein3eed7dd2016-02-24 19:07:07 -08001030 done(task.sink.tag.c_str(), task.src.tag.c_str(),
1031 task.src.options.c_str(), name.c_str());
mtkleinba6ada72016-01-21 09:39:35 -08001032 return;
mtklein4089ef72015-03-05 08:40:28 -08001033 }
mtklein748ca3b2015-01-15 10:56:12 -08001034 }
mtklein62bd1a62015-01-27 14:46:26 -08001035
mtkleincd50bca2016-01-05 06:20:20 -08001036 // We're likely switching threads here, so we must capture by value, [=] or [foo,bar].
1037 SkStreamAsset* data = stream.detachAsStream();
1038 gDefinitelyThreadSafeWork.add([task,name,bitmap,data]{
1039 SkAutoTDelete<SkStreamAsset> ownedData(data);
1040
1041 // Why doesn't the copy constructor do this when we have pre-locked pixels?
1042 bitmap.lockPixels();
1043
1044 SkString md5;
1045 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
1046 SkMD5 hash;
1047 if (data->getLength()) {
1048 hash.writeStream(data, data->getLength());
1049 data->rewind();
mtklein38408462015-07-08 07:25:27 -07001050 } else {
mtkleincd50bca2016-01-05 06:20:20 -08001051 // If we're BGRA (Linux, Windows), swizzle over to RGBA (Mac, Android).
1052 // This helps eliminate multiple 0-pixel-diff hashes on gold.skia.org.
1053 // (Android's general slow speed breaks the tie arbitrarily in RGBA's favor.)
1054 // We might consider promoting 565 to RGBA too.
1055 if (bitmap.colorType() == kBGRA_8888_SkColorType) {
1056 SkBitmap swizzle;
1057 SkAssertResult(bitmap.copyTo(&swizzle, kRGBA_8888_SkColorType));
1058 hash.write(swizzle.getPixels(), swizzle.getSize());
1059 } else {
1060 hash.write(bitmap.getPixels(), bitmap.getSize());
1061 }
1062 }
1063 SkMD5::Digest digest;
1064 hash.finish(digest);
1065 for (int i = 0; i < 16; i++) {
1066 md5.appendf("%02x", digest.data[i]);
mtklein38408462015-07-08 07:25:27 -07001067 }
mtklein62bd1a62015-01-27 14:46:26 -08001068 }
mtklein62bd1a62015-01-27 14:46:26 -08001069
mtkleincd50bca2016-01-05 06:20:20 -08001070 if (!FLAGS_readPath.isEmpty() &&
mtklein3eed7dd2016-02-24 19:07:07 -08001071 !gGold.contains(Gold(task.sink.tag, task.src.tag,
1072 task.src.options, name, md5))) {
mtkleincd50bca2016-01-05 06:20:20 -08001073 fail(SkStringPrintf("%s not found for %s %s %s %s in %s",
1074 md5.c_str(),
mtklein21eaf3b2016-02-08 12:39:59 -08001075 task.sink.tag.c_str(),
1076 task.src.tag.c_str(),
1077 task.src.options.c_str(),
mtkleincd50bca2016-01-05 06:20:20 -08001078 name.c_str(),
1079 FLAGS_readPath[0]));
mtklein748ca3b2015-01-15 10:56:12 -08001080 }
mtkleincd50bca2016-01-05 06:20:20 -08001081
1082 if (!FLAGS_writePath.isEmpty()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001083 const char* ext = task.sink->fileExtension();
mtkleincd50bca2016-01-05 06:20:20 -08001084 if (data->getLength()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001085 WriteToDisk(task, md5, ext, data, data->getLength(), nullptr);
mtkleincd50bca2016-01-05 06:20:20 -08001086 SkASSERT(bitmap.drawsNothing());
1087 } else if (!bitmap.drawsNothing()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001088 WriteToDisk(task, md5, ext, nullptr, 0, &bitmap);
mtkleincd50bca2016-01-05 06:20:20 -08001089 }
1090 }
1091 });
mtklein748ca3b2015-01-15 10:56:12 -08001092 }
mtklein3eed7dd2016-02-24 19:07:07 -08001093 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 -08001094 }
1095
1096 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -08001097 SkString md5,
1098 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -08001099 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -08001100 const SkBitmap* bitmap) {
mtklein409d4702016-02-29 07:38:01 -08001101 bool gammaCorrect = false;
1102 if (bitmap) {
1103 gammaCorrect = bitmap->profileType() == kSRGB_SkColorProfileType
1104 || bitmap-> colorType() == kRGBA_F16_SkColorType;
1105 }
1106
mtklein748ca3b2015-01-15 10:56:12 -08001107 JsonWriter::BitmapResult result;
djsollen54416de2015-04-03 07:24:48 -07001108 result.name = task.src->name();
mtklein3eed7dd2016-02-24 19:07:07 -08001109 result.config = task.sink.tag;
djsollen54416de2015-04-03 07:24:48 -07001110 result.sourceType = task.src.tag;
1111 result.sourceOptions = task.src.options;
1112 result.ext = ext;
mtklein409d4702016-02-29 07:38:01 -08001113 result.gammaCorrect = gammaCorrect;
djsollen54416de2015-04-03 07:24:48 -07001114 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -08001115 JsonWriter::AddBitmapResult(result);
1116
mtkleinb0531a72015-04-07 13:38:48 -07001117 // If an MD5 is uninteresting, we want it noted in the JSON file,
1118 // but don't want to dump it out as a .png (or whatever ext is).
1119 if (gUninterestingHashes.contains(md5)) {
1120 return;
1121 }
1122
mtklein748ca3b2015-01-15 10:56:12 -08001123 const char* dir = FLAGS_writePath[0];
1124 if (0 == strcmp(dir, "@")) { // Needed for iOS.
1125 dir = FLAGS_resourcePath[0];
1126 }
1127 sk_mkdir(dir);
1128
1129 SkString path;
1130 if (FLAGS_nameByHash) {
1131 path = SkOSPath::Join(dir, result.md5.c_str());
1132 path.append(".");
1133 path.append(ext);
1134 if (sk_exists(path.c_str())) {
1135 return; // Content-addressed. If it exists already, we're done.
1136 }
1137 } else {
kkinnunen3e980c32015-12-23 01:33:00 -08001138 path = SkOSPath::Join(dir, task.sink.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001139 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001140 path = SkOSPath::Join(path.c_str(), task.src.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001141 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001142 if (strcmp(task.src.options.c_str(), "") != 0) {
1143 path = SkOSPath::Join(path.c_str(), task.src.options.c_str());
djsollen54416de2015-04-03 07:24:48 -07001144 sk_mkdir(path.c_str());
1145 }
mtklein748ca3b2015-01-15 10:56:12 -08001146 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
1147 path.append(".");
1148 path.append(ext);
1149 }
1150
mtklein748ca3b2015-01-15 10:56:12 -08001151 if (bitmap) {
mtkleina5114d72015-08-24 13:27:01 -07001152 if (!dump_png(*bitmap, path.c_str(), result.md5.c_str())) {
mtklein748ca3b2015-01-15 10:56:12 -08001153 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
1154 return;
1155 }
1156 } else {
mtkleina5114d72015-08-24 13:27:01 -07001157 SkFILEWStream file(path.c_str());
1158 if (!file.isValid()) {
1159 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
1160 return;
1161 }
mtklein748ca3b2015-01-15 10:56:12 -08001162 if (!file.writeStream(data, len)) {
1163 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
1164 return;
1165 }
1166 }
1167 }
1168};
1169
mtklein748ca3b2015-01-15 10:56:12 -08001170/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1171
1172// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
1173
mtklein21eaf3b2016-02-08 12:39:59 -08001174static SkTDArray<skiatest::Test> gParallelTests, gSerialTests;
mtklein748ca3b2015-01-15 10:56:12 -08001175
1176static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -08001177 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -08001178 return;
1179 }
mtklein6393c062015-04-27 08:45:01 -07001180 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r; r = r->next()) {
1181 if (!in_shard()) {
1182 continue;
1183 }
halcanary87f3ba42015-01-20 09:30:20 -08001184 // Despite its name, factory() is returning a reference to
1185 // link-time static const POD data.
1186 const skiatest::Test& test = r->factory();
1187 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -08001188 continue;
1189 }
halcanary87f3ba42015-01-20 09:30:20 -08001190 if (test.needsGpu && gpu_supported()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001191 (FLAGS_gpu_threading ? gParallelTests : gSerialTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -08001192 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein21eaf3b2016-02-08 12:39:59 -08001193 gParallelTests.push(test);
mtklein82d28432015-01-15 12:46:02 -08001194 }
mtklein748ca3b2015-01-15 10:56:12 -08001195 }
1196}
1197
mtklein21eaf3b2016-02-08 12:39:59 -08001198static void run_test(skiatest::Test test) {
halcanary87f3ba42015-01-20 09:30:20 -08001199 struct : public skiatest::Reporter {
mtklein36352bf2015-03-25 18:17:31 -07001200 void reportFailed(const skiatest::Failure& failure) override {
halcanary87f3ba42015-01-20 09:30:20 -08001201 fail(failure.toString());
1202 JsonWriter::AddTestFailure(failure);
1203 }
mtklein36352bf2015-03-25 18:17:31 -07001204 bool allowExtendedTest() const override {
halcanary87f3ba42015-01-20 09:30:20 -08001205 return FLAGS_pathOpsExtended;
1206 }
mtklein36352bf2015-03-25 18:17:31 -07001207 bool verbose() const override { return FLAGS_veryVerbose; }
halcanary87f3ba42015-01-20 09:30:20 -08001208 } reporter;
djsollen824996a2015-06-12 12:06:22 -07001209
mtklein3eed7dd2016-02-24 19:07:07 -08001210 if (!FLAGS_dryRun && !is_blacklisted("_", "tests", "_", test.name)) {
mtklein21eaf3b2016-02-08 12:39:59 -08001211 start("unit", "test", "", test.name);
mtklein55e88b22015-01-21 15:50:13 -08001212 GrContextFactory factory;
mtklein21eaf3b2016-02-08 12:39:59 -08001213 test.proc(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -08001214 }
mtklein3eed7dd2016-02-24 19:07:07 -08001215 done("unit", "test", "", test.name);
mtklein748ca3b2015-01-15 10:56:12 -08001216}
1217
1218/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1219
mtkleinafae30a2016-02-24 12:28:32 -08001220DEFINE_int32(status_sec, 15, "Print status this often (and if we crash).");
1221
1222SkThread* start_status_thread() {
1223 auto thread = new SkThread([] (void*) {
1224 for (;;) {
1225 print_status();
1226 #if defined(SK_BUILD_FOR_WIN)
1227 Sleep(FLAGS_status_sec * 1000);
1228 #else
1229 sleep(FLAGS_status_sec);
1230 #endif
mtklein2e1c47e2015-03-12 07:16:56 -07001231 }
mtkleinafae30a2016-02-24 12:28:32 -08001232 });
1233 thread->start();
1234 return thread;
mtkleinde6fc2b2015-03-12 06:28:54 -07001235}
1236
caryclark83ca6282015-06-10 09:31:09 -07001237#define PORTABLE_FONT_PREFIX "Toy Liberation "
1238
1239static SkTypeface* create_from_name(const char familyName[], SkTypeface::Style style) {
1240 if (familyName && strlen(familyName) > sizeof(PORTABLE_FONT_PREFIX)
1241 && !strncmp(familyName, PORTABLE_FONT_PREFIX, sizeof(PORTABLE_FONT_PREFIX) - 1)) {
caryclark1818acb2015-07-24 12:09:25 -07001242 return sk_tool_utils::create_portable_typeface(familyName, style);
caryclark83ca6282015-06-10 09:31:09 -07001243 }
halcanary96fcdcc2015-08-27 07:41:13 -07001244 return nullptr;
caryclark83ca6282015-06-10 09:31:09 -07001245}
1246
1247#undef PORTABLE_FONT_PREFIX
1248
1249extern SkTypeface* (*gCreateTypefaceDelegate)(const char [], SkTypeface::Style );
1250
jcgregorio3b27ade2014-11-13 08:06:40 -08001251int dm_main();
caryclark17f0b6d2014-07-22 10:15:34 -07001252int dm_main() {
mtklein246ba3a2016-02-23 10:39:36 -08001253 setup_crash_handler();
mtkleinafae30a2016-02-24 12:28:32 -08001254
mtkleinc41fd922016-03-08 09:01:39 -08001255 if (FLAGS_verbose && !FLAGS_writePath.isEmpty()) {
1256 sk_mkdir(FLAGS_writePath[0]);
1257 gVLog = freopen(SkOSPath::Join(FLAGS_writePath[0], "verbose.log").c_str(), "w", stderr);
1258 }
1259
mtklein5286f022016-01-22 08:18:14 -08001260 JsonWriter::DumpJson(); // It's handy for the bots to assume this is ~never missing.
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +00001261 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -07001262 SkTaskGroup::Enabler enabled(FLAGS_threads);
caryclark83ca6282015-06-10 09:31:09 -07001263 gCreateTypefaceDelegate = &create_from_name;
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +00001264
halcanary7d571242016-02-24 17:59:16 -08001265 {
1266 SkString testResourcePath = GetResourcePath("color_wheel.png");
1267 SkFILEStream testResource(testResourcePath.c_str());
1268 if (!testResource.isValid()) {
mtkleinc41fd922016-03-08 09:01:39 -08001269 info("Some resources are missing. Do you need to set --resourcePath?\n");
halcanary7d571242016-02-24 17:59:16 -08001270 }
1271 }
mtklein62bd1a62015-01-27 14:46:26 -08001272 gather_gold();
borenet09ed4802015-04-03 14:15:33 -07001273 gather_uninteresting_hashes();
mtklein62bd1a62015-01-27 14:46:26 -08001274
scroggo86737142016-02-03 12:19:11 -08001275 if (!gather_srcs()) {
1276 return 1;
1277 }
mtklein748ca3b2015-01-15 10:56:12 -08001278 gather_sinks();
1279 gather_tests();
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +00001280
mtklein21eaf3b2016-02-08 12:39:59 -08001281 gPending = gSrcs.count() * gSinks.count() + gParallelTests.count() + gSerialTests.count();
mtkleinc41fd922016-03-08 09:01:39 -08001282 info("%d srcs * %d sinks + %d tests == %d tasks",
1283 gSrcs.count(), gSinks.count(), gParallelTests.count() + gSerialTests.count(), gPending);
mtkleinafae30a2016-02-24 12:28:32 -08001284 SkAutoTDelete<SkThread> statusThread(start_status_thread());
mtklein748ca3b2015-01-15 10:56:12 -08001285
mtklein21eaf3b2016-02-08 12:39:59 -08001286 // Kick off as much parallel work as we can, making note of any serial work we'll need to do.
1287 SkTaskGroup parallel;
1288 SkTArray<Task> serial;
1289
1290 for (auto& sink : gSinks)
1291 for (auto& src : gSrcs) {
mtklein3eed7dd2016-02-24 19:07:07 -08001292 if (src->veto(sink->flags()) ||
1293 is_blacklisted(sink.tag.c_str(), src.tag.c_str(),
1294 src.options.c_str(), src->name().c_str())) {
mtklein15923c92016-02-29 10:14:38 -08001295 SkAutoTAcquire<SkSpinlock> lock(gMutex);
mtklein3eed7dd2016-02-24 19:07:07 -08001296 gPending--;
1297 continue;
1298 }
1299
mtklein21eaf3b2016-02-08 12:39:59 -08001300 Task task(src, sink);
1301 if (src->serial() || sink->serial()) {
1302 serial.push_back(task);
1303 } else {
1304 parallel.add([task] { Task::Run(task); });
mtklein748ca3b2015-01-15 10:56:12 -08001305 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001306 }
mtklein21eaf3b2016-02-08 12:39:59 -08001307 for (auto test : gParallelTests) {
1308 parallel.add([test] { run_test(test); });
mtklein55e88b22015-01-21 15:50:13 -08001309 }
mtklein21eaf3b2016-02-08 12:39:59 -08001310
1311 // With the parallel work running, run serial tasks and tests here on main thread.
1312 for (auto task : serial) { Task::Run(task); }
1313 for (auto test : gSerialTests) { run_test(test); }
1314
1315 // Wait for any remaining parallel work to complete (including any spun off of serial tasks).
1316 parallel.wait();
mtkleincd50bca2016-01-05 06:20:20 -08001317 gDefinitelyThreadSafeWork.wait();
1318
mtkleinda884c42016-02-24 18:00:23 -08001319 // We'd better have run everything.
1320 SkASSERT(gPending == 0);
mtkleine027f172016-02-26 15:53:06 -08001321 // Make sure we've flushed all our results to disk.
1322 JsonWriter::DumpJson();
mtkleinda884c42016-02-24 18:00:23 -08001323
mtklein748ca3b2015-01-15 10:56:12 -08001324 // At this point we're back in single-threaded land.
caryclarkf53ce802015-06-15 06:48:30 -07001325 sk_tool_utils::release_portable_typefaces();
mtklein@google.comd36522d2013-10-16 13:02:15 +00001326
mtklein748ca3b2015-01-15 10:56:12 -08001327 if (gFailures.count() > 0) {
mtkleinc41fd922016-03-08 09:01:39 -08001328 info("Failures:\n");
mtklein748ca3b2015-01-15 10:56:12 -08001329 for (int i = 0; i < gFailures.count(); i++) {
mtkleinc41fd922016-03-08 09:01:39 -08001330 info("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001331 }
mtkleinc41fd922016-03-08 09:01:39 -08001332 info("%d failures\n", gFailures.count());
mtklein748ca3b2015-01-15 10:56:12 -08001333 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001334 }
mtkleinafae30a2016-02-24 12:28:32 -08001335
1336#ifdef SK_PDF_IMAGE_STATS
halcanary7a14b312015-10-01 07:28:13 -07001337 SkPDFImageDumpStats();
mtkleinafae30a2016-02-24 12:28:32 -08001338#endif // SK_PDF_IMAGE_STATS
1339
1340 print_status();
mtkleinc41fd922016-03-08 09:01:39 -08001341 info("Finished!\n");
mtklein748ca3b2015-01-15 10:56:12 -08001342 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +00001343}
jcgregorio3b27ade2014-11-13 08:06:40 -08001344
kkinnunen179a8f52015-11-20 13:32:24 -08001345// TODO: currently many GPU tests are declared outside SK_SUPPORT_GPU guards.
1346// Thus we export the empty RunWithGPUTestContexts when SK_SUPPORT_GPU=0.
1347namespace skiatest {
1348namespace {
1349typedef void(*TestWithGrContext)(skiatest::Reporter*, GrContext*);
1350typedef void(*TestWithGrContextAndGLContext)(skiatest::Reporter*, GrContext*, SkGLContext*);
1351#if SK_SUPPORT_GPU
1352template<typename T>
kkinnunen34058002016-01-06 23:49:30 -08001353void call_test(T test, skiatest::Reporter* reporter, const GrContextFactory::ContextInfo& context);
kkinnunen179a8f52015-11-20 13:32:24 -08001354template<>
1355void call_test(TestWithGrContext test, skiatest::Reporter* reporter,
kkinnunen34058002016-01-06 23:49:30 -08001356 const GrContextFactory::ContextInfo& context) {
1357 test(reporter, context.fGrContext);
kkinnunen179a8f52015-11-20 13:32:24 -08001358}
1359template<>
1360void call_test(TestWithGrContextAndGLContext test, skiatest::Reporter* reporter,
kkinnunen34058002016-01-06 23:49:30 -08001361 const GrContextFactory::ContextInfo& context) {
1362 test(reporter, context.fGrContext, context.fGLContext);
kkinnunen179a8f52015-11-20 13:32:24 -08001363}
1364#endif
1365} // namespace
1366
kkinnunen179a8f52015-11-20 13:32:24 -08001367template<typename T>
1368void RunWithGPUTestContexts(T test, GPUTestContexts testContexts, Reporter* reporter,
1369 GrContextFactory* factory) {
1370#if SK_SUPPORT_GPU
kkinnunen3e980c32015-12-23 01:33:00 -08001371 // Iterate over context types, except use "native" instead of explicitly trying OpenGL and
1372 // OpenGL ES. Do not use GLES on desktop, since tests do not account for not fixing
1373 // http://skbug.com/2809
1374 GrContextFactory::GLContextType contextTypes[] = {
1375 GrContextFactory::kNative_GLContextType,
1376#if SK_ANGLE
1377#ifdef SK_BUILD_FOR_WIN
1378 GrContextFactory::kANGLE_GLContextType,
1379#endif
1380 GrContextFactory::kANGLE_GL_GLContextType,
1381#endif
1382#if SK_COMMAND_BUFFER
kkinnunenf655e932016-03-03 07:39:48 -08001383 GrContextFactory::kCommandBuffer_GLContextType,
kkinnunen3e980c32015-12-23 01:33:00 -08001384#endif
1385#if SK_MESA
1386 GrContextFactory::kMESA_GLContextType,
1387#endif
1388 GrContextFactory::kNull_GLContextType,
1389 GrContextFactory::kDebug_GLContextType,
1390 };
1391 static_assert(SK_ARRAY_COUNT(contextTypes) == GrContextFactory::kGLContextTypeCnt - 2,
1392 "Skipping unexpected GLContextType for GPU tests");
1393
1394 for (auto& contextType : contextTypes) {
kkinnunen179a8f52015-11-20 13:32:24 -08001395 int contextSelector = kNone_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001396 if (GrContextFactory::IsRenderingGLContext(contextType)) {
kkinnunen179a8f52015-11-20 13:32:24 -08001397 contextSelector |= kAllRendering_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001398 } else if (contextType == GrContextFactory::kNative_GLContextType) {
kkinnunen179a8f52015-11-20 13:32:24 -08001399 contextSelector |= kNative_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001400 } else if (contextType == GrContextFactory::kNull_GLContextType) {
kkinnunen179a8f52015-11-20 13:32:24 -08001401 contextSelector |= kNull_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001402 } else if (contextType == GrContextFactory::kDebug_GLContextType) {
kkinnunen55eeae92015-12-10 23:19:29 -08001403 contextSelector |= kDebug_GPUTestContexts;
kkinnunen179a8f52015-11-20 13:32:24 -08001404 }
1405 if ((testContexts & contextSelector) == 0) {
1406 continue;
1407 }
kkinnunen34058002016-01-06 23:49:30 -08001408 GrContextFactory::ContextInfo context = factory->getContextInfo(contextType);
1409 if (context.fGrContext) {
kkinnunen5219fd92015-12-10 06:28:13 -08001410 call_test(test, reporter, context);
1411 }
kkinnunen34058002016-01-06 23:49:30 -08001412 context = factory->getContextInfo(contextType,
1413 GrContextFactory::kEnableNVPR_GLContextOptions);
1414 if (context.fGrContext) {
kkinnunen179a8f52015-11-20 13:32:24 -08001415 call_test(test, reporter, context);
1416 }
1417 }
1418#endif
1419}
1420
1421template
1422void RunWithGPUTestContexts<TestWithGrContext>(TestWithGrContext test,
1423 GPUTestContexts testContexts,
1424 Reporter* reporter,
1425 GrContextFactory* factory);
1426template
1427void RunWithGPUTestContexts<TestWithGrContextAndGLContext>(TestWithGrContextAndGLContext test,
1428 GPUTestContexts testContexts,
1429 Reporter* reporter,
1430 GrContextFactory* factory);
1431} // namespace skiatest
1432
borenet48087572015-04-02 12:16:36 -07001433#if !defined(SK_BUILD_FOR_IOS)
jcgregorio3b27ade2014-11-13 08:06:40 -08001434int main(int argc, char** argv) {
1435 SkCommandLineFlags::Parse(argc, argv);
1436 return dm_main();
1437}
1438#endif