blob: d8421c95b062915d45262904e6c7a6866f28274a [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
mtklein935f1b12016-03-16 08:37:19 -0700265#if defined(SK_BUILD_FOR_WIN32)
266 static const char* kNewline = "\r\n";
267#else
268 static const char* kNewline = "\n";
269#endif
270
borenet09ed4802015-04-03 14:15:33 -0700271static SkTHashSet<SkString> gUninterestingHashes;
272
273static void gather_uninteresting_hashes() {
274 if (!FLAGS_uninterestingHashesFile.isEmpty()) {
275 SkAutoTUnref<SkData> data(SkData::NewFromFileName(FLAGS_uninterestingHashesFile[0]));
mtkleincc334b32015-09-22 11:43:53 -0700276 if (!data) {
mtkleinc41fd922016-03-08 09:01:39 -0800277 info("WARNING: unable to read uninteresting hashes from %s\n",
278 FLAGS_uninterestingHashesFile[0]);
mtkleincc334b32015-09-22 11:43:53 -0700279 return;
280 }
borenet09ed4802015-04-03 14:15:33 -0700281 SkTArray<SkString> hashes;
mtklein935f1b12016-03-16 08:37:19 -0700282 SkStrSplit((const char*)data->data(), kNewline, &hashes);
borenet09ed4802015-04-03 14:15:33 -0700283 for (const SkString& hash : hashes) {
284 gUninterestingHashes.add(hash);
285 }
mtkleinc41fd922016-03-08 09:01:39 -0800286 info("FYI: loaded %d distinct uninteresting hashes from %d lines\n",
287 gUninterestingHashes.count(), hashes.count());
borenet09ed4802015-04-03 14:15:33 -0700288 }
289}
290
291/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
292
mtkleine0effd62015-07-29 06:37:28 -0700293struct TaggedSrc : public SkAutoTDelete<Src> {
mtklein3eed7dd2016-02-24 19:07:07 -0800294 SkString tag;
295 SkString options;
mtkleine0effd62015-07-29 06:37:28 -0700296};
297
298struct TaggedSink : public SkAutoTDelete<Sink> {
kkinnunen3e980c32015-12-23 01:33:00 -0800299 SkString tag;
djsollen54416de2015-04-03 07:24:48 -0700300};
mtklein748ca3b2015-01-15 10:56:12 -0800301
302static const bool kMemcpyOK = true;
303
mtkleine0effd62015-07-29 06:37:28 -0700304static SkTArray<TaggedSrc, kMemcpyOK> gSrcs;
305static SkTArray<TaggedSink, kMemcpyOK> gSinks;
mtklein748ca3b2015-01-15 10:56:12 -0800306
mtklein6393c062015-04-27 08:45:01 -0700307static bool in_shard() {
308 static int N = 0;
309 return N++ % FLAGS_shards == FLAGS_shard;
310}
311
mtklein3eed7dd2016-02-24 19:07:07 -0800312static void push_src(const char* tag, ImplicitString options, Src* s) {
mtklein748ca3b2015-01-15 10:56:12 -0800313 SkAutoTDelete<Src> src(s);
mtklein6393c062015-04-27 08:45:01 -0700314 if (in_shard() &&
mtklein3eed7dd2016-02-24 19:07:07 -0800315 FLAGS_src.contains(tag) &&
mtklein748ca3b2015-01-15 10:56:12 -0800316 !SkCommandLineFlags::ShouldSkip(FLAGS_match, src->name().c_str())) {
mtkleine0effd62015-07-29 06:37:28 -0700317 TaggedSrc& s = gSrcs.push_back();
mtklein18300a32016-03-16 13:53:35 -0700318 s.reset(src.release());
mtklein748ca3b2015-01-15 10:56:12 -0800319 s.tag = tag;
djsollen54416de2015-04-03 07:24:48 -0700320 s.options = options;
mtklein114c3cd2015-01-15 10:15:02 -0800321 }
mtklein748ca3b2015-01-15 10:56:12 -0800322}
mtklein114c3cd2015-01-15 10:15:02 -0800323
msarett9e707a02015-09-01 14:57:57 -0700324static void push_codec_src(Path path, CodecSrc::Mode mode, CodecSrc::DstColorType dstColorType,
scroggoc5560be2016-02-03 09:42:42 -0800325 SkAlphaType dstAlphaType, float scale) {
scroggoe4499842016-02-25 11:03:47 -0800326 if (FLAGS_simpleCodec) {
327 if (mode != CodecSrc::kCodec_Mode || dstColorType != CodecSrc::kGetFromCanvas_DstColorType
328 || scale != 1.0f)
329 // Only decode in the simple case.
330 return;
331 }
msarett9e707a02015-09-01 14:57:57 -0700332 SkString folder;
333 switch (mode) {
334 case CodecSrc::kCodec_Mode:
335 folder.append("codec");
336 break;
msarettbb25b532016-01-13 09:31:39 -0800337 case CodecSrc::kCodecZeroInit_Mode:
338 folder.append("codec_zero_init");
339 break;
msarett9e707a02015-09-01 14:57:57 -0700340 case CodecSrc::kScanline_Mode:
341 folder.append("scanline");
342 break;
msarett9e707a02015-09-01 14:57:57 -0700343 case CodecSrc::kStripe_Mode:
344 folder.append("stripe");
345 break;
msarett91c22b22016-02-22 12:27:46 -0800346 case CodecSrc::kCroppedScanline_Mode:
347 folder.append("crop");
348 break;
msarett9e707a02015-09-01 14:57:57 -0700349 case CodecSrc::kSubset_Mode:
msarett36c37962015-09-02 13:20:52 -0700350 folder.append("codec_subset");
msarett9e707a02015-09-01 14:57:57 -0700351 break;
352 }
353
354 switch (dstColorType) {
355 case CodecSrc::kGrayscale_Always_DstColorType:
356 folder.append("_kGray8");
357 break;
358 case CodecSrc::kIndex8_Always_DstColorType:
359 folder.append("_kIndex8");
360 break;
361 default:
362 break;
363 }
364
scroggoc5560be2016-02-03 09:42:42 -0800365 switch (dstAlphaType) {
366 case kOpaque_SkAlphaType:
367 folder.append("_opaque");
368 break;
369 case kPremul_SkAlphaType:
370 folder.append("_premul");
371 break;
372 case kUnpremul_SkAlphaType:
373 folder.append("_unpremul");
374 break;
375 default:
376 break;
377 }
378
msarett9e707a02015-09-01 14:57:57 -0700379 if (1.0f != scale) {
380 folder.appendf("_%.3f", scale);
381 }
382
scroggoc5560be2016-02-03 09:42:42 -0800383 CodecSrc* src = new CodecSrc(path, mode, dstColorType, dstAlphaType, scale);
msarett9e707a02015-09-01 14:57:57 -0700384 push_src("image", folder, src);
385}
386
msarett3d9d7a72015-10-21 10:27:10 -0700387static void push_android_codec_src(Path path, AndroidCodecSrc::Mode mode,
scroggoc5560be2016-02-03 09:42:42 -0800388 CodecSrc::DstColorType dstColorType, SkAlphaType dstAlphaType, int sampleSize) {
msarett3d9d7a72015-10-21 10:27:10 -0700389 SkString folder;
390 switch (mode) {
391 case AndroidCodecSrc::kFullImage_Mode:
392 folder.append("scaled_codec");
393 break;
394 case AndroidCodecSrc::kDivisor_Mode:
395 folder.append("scaled_codec_divisor");
396 break;
397 }
398
399 switch (dstColorType) {
400 case CodecSrc::kGrayscale_Always_DstColorType:
401 folder.append("_kGray8");
402 break;
403 case CodecSrc::kIndex8_Always_DstColorType:
404 folder.append("_kIndex8");
405 break;
406 default:
407 break;
408 }
409
scroggoc5560be2016-02-03 09:42:42 -0800410 switch (dstAlphaType) {
411 case kOpaque_SkAlphaType:
412 folder.append("_opaque");
413 break;
414 case kPremul_SkAlphaType:
415 folder.append("_premul");
416 break;
msarett9e9444c2016-02-03 12:39:10 -0800417 case kUnpremul_SkAlphaType:
418 folder.append("_unpremul");
419 break;
scroggoc5560be2016-02-03 09:42:42 -0800420 default:
421 break;
422 }
423
msarett3d9d7a72015-10-21 10:27:10 -0700424 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800425 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700426 }
427
scroggoc5560be2016-02-03 09:42:42 -0800428 AndroidCodecSrc* src = new AndroidCodecSrc(path, mode, dstColorType, dstAlphaType, sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700429 push_src("image", folder, src);
430}
431
msarett18976312016-03-09 14:20:58 -0800432static void push_image_gen_src(Path path, ImageGenSrc::Mode mode, SkAlphaType alphaType, bool isGpu)
433{
434 SkString folder;
435 switch (mode) {
436 case ImageGenSrc::kCodec_Mode:
437 folder.append("gen_codec");
438 break;
439 case ImageGenSrc::kPlatform_Mode:
440 folder.append("gen_platform");
441 break;
442 }
443
444 if (isGpu) {
445 folder.append("_gpu");
446 } else {
447 switch (alphaType) {
448 case kOpaque_SkAlphaType:
449 folder.append("_opaque");
450 break;
451 case kPremul_SkAlphaType:
452 folder.append("_premul");
453 break;
454 case kUnpremul_SkAlphaType:
455 folder.append("_unpremul");
456 break;
457 default:
458 break;
459 }
460 }
461
462 ImageGenSrc* src = new ImageGenSrc(path, mode, alphaType, isGpu);
463 push_src("image", folder, src);
464}
465
msarett438b2ad2015-04-09 12:43:10 -0700466static void push_codec_srcs(Path path) {
467 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
468 if (!encoded) {
mtkleinc41fd922016-03-08 09:01:39 -0800469 info("Couldn't read %s.", path.c_str());
msarett438b2ad2015-04-09 12:43:10 -0700470 return;
471 }
472 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
halcanary96fcdcc2015-08-27 07:41:13 -0700473 if (nullptr == codec.get()) {
mtkleinc41fd922016-03-08 09:01:39 -0800474 info("Couldn't create codec for %s.", path.c_str());
msarett438b2ad2015-04-09 12:43:10 -0700475 return;
476 }
477
msarett36c37962015-09-02 13:20:52 -0700478 // Native Scales
emmaleer8f4ba762015-08-14 07:44:46 -0700479 // 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 -0700480 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 -0700481
msarett91c22b22016-02-22 12:27:46 -0800482 SkTArray<CodecSrc::Mode> nativeModes;
483 nativeModes.push_back(CodecSrc::kCodec_Mode);
484 nativeModes.push_back(CodecSrc::kCodecZeroInit_Mode);
msarett91c22b22016-02-22 12:27:46 -0800485 switch (codec->getEncodedFormat()) {
486 case SkEncodedFormat::kJPEG_SkEncodedFormat:
487 nativeModes.push_back(CodecSrc::kScanline_Mode);
488 nativeModes.push_back(CodecSrc::kStripe_Mode);
489 nativeModes.push_back(CodecSrc::kCroppedScanline_Mode);
490 break;
491 case SkEncodedFormat::kWEBP_SkEncodedFormat:
492 nativeModes.push_back(CodecSrc::kSubset_Mode);
493 break;
yujieqin7a307df2016-03-11 07:32:33 -0800494 case SkEncodedFormat::kDNG_SkEncodedFormat:
msarettb65e6042016-02-23 05:37:25 -0800495 break;
msarett91c22b22016-02-22 12:27:46 -0800496 default:
497 nativeModes.push_back(CodecSrc::kScanline_Mode);
498 nativeModes.push_back(CodecSrc::kStripe_Mode);
499 break;
500 }
msarett9e707a02015-09-01 14:57:57 -0700501
msarett91c22b22016-02-22 12:27:46 -0800502 SkTArray<CodecSrc::DstColorType> colorTypes;
503 colorTypes.push_back(CodecSrc::kGetFromCanvas_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700504 switch (codec->getInfo().colorType()) {
505 case kGray_8_SkColorType:
msarett91c22b22016-02-22 12:27:46 -0800506 colorTypes.push_back(CodecSrc::kGrayscale_Always_DstColorType);
msarett55f7bdd2016-02-16 13:24:54 -0800507 if (kWBMP_SkEncodedFormat == codec->getEncodedFormat()) {
msarett91c22b22016-02-22 12:27:46 -0800508 colorTypes.push_back(CodecSrc::kIndex8_Always_DstColorType);
msarett55f7bdd2016-02-16 13:24:54 -0800509 }
msarett36c37962015-09-02 13:20:52 -0700510 break;
511 case kIndex_8_SkColorType:
msarett91c22b22016-02-22 12:27:46 -0800512 colorTypes.push_back(CodecSrc::kIndex8_Always_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700513 break;
514 default:
msarett36c37962015-09-02 13:20:52 -0700515 break;
516 }
msarett9e707a02015-09-01 14:57:57 -0700517
scroggoc5560be2016-02-03 09:42:42 -0800518 SkTArray<SkAlphaType> alphaModes;
519 alphaModes.push_back(kPremul_SkAlphaType);
msarett9e9444c2016-02-03 12:39:10 -0800520 alphaModes.push_back(kUnpremul_SkAlphaType);
scroggoc5560be2016-02-03 09:42:42 -0800521 if (codec->getInfo().alphaType() == kOpaque_SkAlphaType) {
522 alphaModes.push_back(kOpaque_SkAlphaType);
523 }
msarettb714fb02016-01-22 14:46:42 -0800524
525 for (CodecSrc::Mode mode : nativeModes) {
msarettb714fb02016-01-22 14:46:42 -0800526 for (float scale : nativeScales) {
msarett91c22b22016-02-22 12:27:46 -0800527 for (CodecSrc::DstColorType colorType : colorTypes) {
scroggoc5560be2016-02-03 09:42:42 -0800528 for (SkAlphaType alphaType : alphaModes) {
msarett91c22b22016-02-22 12:27:46 -0800529 // Only test kCroppedScanline_Mode when the alpha type is opaque. The test is
530 // slow and won't be interestingly different with different alpha types.
531 if (CodecSrc::kCroppedScanline_Mode == mode &&
532 kOpaque_SkAlphaType != alphaType) {
533 continue;
534 }
535
536 push_codec_src(path, mode, colorType, alphaType, scale);
scroggoc5560be2016-02-03 09:42:42 -0800537 }
msarett9e707a02015-09-01 14:57:57 -0700538 }
539 }
msarett0a242972015-06-11 14:27:27 -0700540 }
msarett36c37962015-09-02 13:20:52 -0700541
scroggoe4499842016-02-25 11:03:47 -0800542 if (FLAGS_simpleCodec) {
543 return;
544 }
545
halcanary6950de62015-11-07 05:29:00 -0800546 // https://bug.skia.org/4428
msarett33c76232015-11-16 13:43:40 -0800547 bool subset = false;
548 // The following image types are supported by BitmapRegionDecoder,
549 // so we will test full image decodes and subset decodes.
msarettbe8216a2015-12-04 08:00:50 -0800550 static const char* const exts[] = {
msarett3d9d7a72015-10-21 10:27:10 -0700551 "jpg", "jpeg", "png", "webp",
552 "JPG", "JPEG", "PNG", "WEBP",
553 };
msarettbe8216a2015-12-04 08:00:50 -0800554 for (const char* ext : exts) {
msarett3d9d7a72015-10-21 10:27:10 -0700555 if (path.endsWith(ext)) {
msarett33c76232015-11-16 13:43:40 -0800556 subset = true;
msarett3d9d7a72015-10-21 10:27:10 -0700557 break;
558 }
559 }
msarett33c76232015-11-16 13:43:40 -0800560
msarett3d9d7a72015-10-21 10:27:10 -0700561 const int sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
msarett36c37962015-09-02 13:20:52 -0700562
msarett3d9d7a72015-10-21 10:27:10 -0700563 for (int sampleSize : sampleSizes) {
msarett91c22b22016-02-22 12:27:46 -0800564 for (CodecSrc::DstColorType colorType : colorTypes) {
scroggoc5560be2016-02-03 09:42:42 -0800565 for (SkAlphaType alphaType : alphaModes) {
msarett91c22b22016-02-22 12:27:46 -0800566 push_android_codec_src(path, AndroidCodecSrc::kFullImage_Mode, colorType,
scroggoc5560be2016-02-03 09:42:42 -0800567 alphaType, sampleSize);
568 if (subset) {
msarett91c22b22016-02-22 12:27:46 -0800569 push_android_codec_src(path, AndroidCodecSrc::kDivisor_Mode, colorType,
scroggoc5560be2016-02-03 09:42:42 -0800570 alphaType, sampleSize);
571 }
msarett3d9d7a72015-10-21 10:27:10 -0700572 }
msarett36c37962015-09-02 13:20:52 -0700573 }
574 }
msarett18976312016-03-09 14:20:58 -0800575
576 static const char* const rawExts[] = {
577 "arw", "cr2", "dng", "nef", "nrw", "orf", "raf", "rw2", "pef", "srw",
578 "ARW", "CR2", "DNG", "NEF", "NRW", "ORF", "RAF", "RW2", "PEF", "SRW",
579 };
580
581 // There is not currently a reason to test RAW images on image generator.
582 // If we want to enable these tests, we will need to fix skbug.com/5079.
583 for (const char* ext : rawExts) {
584 if (path.endsWith(ext)) {
585 return;
586 }
587 }
588
589 // Push image generator GPU test.
590 // FIXME: The gpu backend does not draw kGray sources correctly. (skbug.com/4822)
591 if (kGray_8_SkColorType != codec->getInfo().colorType()) {
592 push_image_gen_src(path, ImageGenSrc::kCodec_Mode, codec->getInfo().alphaType(), true);
593 }
594
595 // Push image generator CPU tests.
596 for (SkAlphaType alphaType : alphaModes) {
597 push_image_gen_src(path, ImageGenSrc::kCodec_Mode, alphaType, false);
598
599#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
600 if (kWEBP_SkEncodedFormat != codec->getEncodedFormat() &&
601 kWBMP_SkEncodedFormat != codec->getEncodedFormat() &&
602 kUnpremul_SkAlphaType != alphaType)
603 {
604 push_image_gen_src(path, ImageGenSrc::kPlatform_Mode, alphaType, false);
605 }
606#endif
607 }
msarett438b2ad2015-04-09 12:43:10 -0700608}
609
msarett5cb48852015-11-06 08:56:32 -0800610static bool brd_color_type_supported(SkBitmapRegionDecoder::Strategy strategy,
msaretta5783ae2015-09-08 15:35:32 -0700611 CodecSrc::DstColorType dstColorType) {
612 switch (strategy) {
msarett5cb48852015-11-06 08:56:32 -0800613 case SkBitmapRegionDecoder::kCanvas_Strategy:
msaretta5783ae2015-09-08 15:35:32 -0700614 if (CodecSrc::kGetFromCanvas_DstColorType == dstColorType) {
615 return true;
616 }
617 return false;
msarett5cb48852015-11-06 08:56:32 -0800618 case SkBitmapRegionDecoder::kAndroidCodec_Strategy:
msarett26ad17b2015-10-22 07:29:19 -0700619 switch (dstColorType) {
620 case CodecSrc::kGetFromCanvas_DstColorType:
621 case CodecSrc::kIndex8_Always_DstColorType:
622 case CodecSrc::kGrayscale_Always_DstColorType:
623 return true;
624 default:
625 return false;
626 }
msaretta5783ae2015-09-08 15:35:32 -0700627 default:
628 SkASSERT(false);
629 return false;
630 }
631}
632
msarett5cb48852015-11-06 08:56:32 -0800633static void push_brd_src(Path path, SkBitmapRegionDecoder::Strategy strategy,
msaretta5783ae2015-09-08 15:35:32 -0700634 CodecSrc::DstColorType dstColorType, BRDSrc::Mode mode, uint32_t sampleSize) {
635 SkString folder;
636 switch (strategy) {
msarett5cb48852015-11-06 08:56:32 -0800637 case SkBitmapRegionDecoder::kCanvas_Strategy:
msaretta5783ae2015-09-08 15:35:32 -0700638 folder.append("brd_canvas");
639 break;
msarett5cb48852015-11-06 08:56:32 -0800640 case SkBitmapRegionDecoder::kAndroidCodec_Strategy:
msarett26ad17b2015-10-22 07:29:19 -0700641 folder.append("brd_android_codec");
642 break;
msaretta5783ae2015-09-08 15:35:32 -0700643 default:
644 SkASSERT(false);
645 return;
646 }
647
648 switch (mode) {
649 case BRDSrc::kFullImage_Mode:
650 break;
651 case BRDSrc::kDivisor_Mode:
652 folder.append("_divisor");
653 break;
654 default:
655 SkASSERT(false);
656 return;
657 }
658
659 switch (dstColorType) {
660 case CodecSrc::kGetFromCanvas_DstColorType:
661 break;
662 case CodecSrc::kIndex8_Always_DstColorType:
663 folder.append("_kIndex");
664 break;
665 case CodecSrc::kGrayscale_Always_DstColorType:
666 folder.append("_kGray");
667 break;
668 default:
669 SkASSERT(false);
670 return;
671 }
672
673 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800674 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700675 }
676
677 BRDSrc* src = new BRDSrc(path, strategy, mode, dstColorType, sampleSize);
678 push_src("image", folder, src);
679}
680
681static void push_brd_srcs(Path path) {
682
msarett5cb48852015-11-06 08:56:32 -0800683 const SkBitmapRegionDecoder::Strategy strategies[] = {
684 SkBitmapRegionDecoder::kCanvas_Strategy,
msarett5cb48852015-11-06 08:56:32 -0800685 SkBitmapRegionDecoder::kAndroidCodec_Strategy,
msaretta5783ae2015-09-08 15:35:32 -0700686 };
687
scroggo501b7342015-11-03 07:55:11 -0800688 // Test on a variety of sampleSizes, making sure to include:
689 // - 2, 4, and 8, which are natively supported by jpeg
690 // - multiples of 2 which are not divisible by 4 (analogous for 4)
691 // - larger powers of two, since BRD clients generally use powers of 2
692 // We will only produce output for the larger sizes on large images.
693 const uint32_t sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 24, 32, 64 };
msarett6efbe052015-09-11 09:01:16 -0700694
msaretta5783ae2015-09-08 15:35:32 -0700695 // We will only test to one backend (8888), but we will test all of the
696 // color types that we need to decode to on this backend.
697 const CodecSrc::DstColorType dstColorTypes[] = {
msarett26ad17b2015-10-22 07:29:19 -0700698 CodecSrc::kGetFromCanvas_DstColorType,
699 CodecSrc::kIndex8_Always_DstColorType,
700 CodecSrc::kGrayscale_Always_DstColorType,
msaretta5783ae2015-09-08 15:35:32 -0700701 };
702
703 const BRDSrc::Mode modes[] = {
msarett26ad17b2015-10-22 07:29:19 -0700704 BRDSrc::kFullImage_Mode,
705 BRDSrc::kDivisor_Mode,
msaretta5783ae2015-09-08 15:35:32 -0700706 };
707
msarett5cb48852015-11-06 08:56:32 -0800708 for (SkBitmapRegionDecoder::Strategy strategy : strategies) {
msarett6efbe052015-09-11 09:01:16 -0700709 for (uint32_t sampleSize : sampleSizes) {
msarett6efbe052015-09-11 09:01:16 -0700710 for (CodecSrc::DstColorType dstColorType : dstColorTypes) {
711 if (brd_color_type_supported(strategy, dstColorType)) {
712 for (BRDSrc::Mode mode : modes) {
msaretta5783ae2015-09-08 15:35:32 -0700713 push_brd_src(path, strategy, dstColorType, mode, sampleSize);
714 }
715 }
716 }
717 }
718 }
719}
720
721static bool brd_supported(const char* ext) {
msarett8c8f22a2015-04-01 06:58:48 -0700722 static const char* const exts[] = {
msaretta5783ae2015-09-08 15:35:32 -0700723 "jpg", "jpeg", "png", "webp",
724 "JPG", "JPEG", "PNG", "WEBP",
msarett8c8f22a2015-04-01 06:58:48 -0700725 };
726
727 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
728 if (0 == strcmp(exts[i], ext)) {
729 return true;
730 }
731 }
732 return false;
scroggo9b77ddd2015-03-19 06:03:39 -0700733}
734
scroggo86737142016-02-03 12:19:11 -0800735static bool gather_srcs() {
mtklein748ca3b2015-01-15 10:56:12 -0800736 for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) {
djsollen54416de2015-04-03 07:24:48 -0700737 push_src("gm", "", new GMSrc(r->factory()));
mtklein748ca3b2015-01-15 10:56:12 -0800738 }
halcanaryfc37ad12015-01-30 07:31:19 -0800739 for (int i = 0; i < FLAGS_skps.count(); i++) {
740 const char* path = FLAGS_skps[i];
741 if (sk_isdir(path)) {
742 SkOSFile::Iter it(path, "skp");
743 for (SkString file; it.next(&file); ) {
djsollen54416de2015-04-03 07:24:48 -0700744 push_src("skp", "", new SKPSrc(SkOSPath::Join(path, file.c_str())));
halcanaryfc37ad12015-01-30 07:31:19 -0800745 }
746 } else {
djsollen54416de2015-04-03 07:24:48 -0700747 push_src("skp", "", new SKPSrc(path));
mtklein114c3cd2015-01-15 10:15:02 -0800748 }
749 }
scroggo86737142016-02-03 12:19:11 -0800750
751 SkTArray<SkString> images;
752 if (!CollectImages(&images)) {
753 return false;
754 }
755
756 for (auto image : images) {
757 push_codec_srcs(image);
scroggoe4499842016-02-25 11:03:47 -0800758 if (FLAGS_simpleCodec) {
759 continue;
760 }
761
mtklein21eaf3b2016-02-08 12:39:59 -0800762 const char* ext = strrchr(image.c_str(), '.');
763 if (ext && brd_supported(ext+1)) {
scroggo86737142016-02-03 12:19:11 -0800764 push_brd_srcs(image);
mtklein709d2c32015-01-15 08:30:25 -0800765 }
mtklein709d2c32015-01-15 08:30:25 -0800766 }
scroggo86737142016-02-03 12:19:11 -0800767
768 return true;
mtklein709d2c32015-01-15 08:30:25 -0800769}
770
kkinnunen3e980c32015-12-23 01:33:00 -0800771static void push_sink(const SkCommandLineConfig& config, Sink* s) {
rmistry0f515bd2015-12-22 10:22:26 -0800772 SkAutoTDelete<Sink> sink(s);
kkinnunen3e980c32015-12-23 01:33:00 -0800773
mtkleine0effd62015-07-29 06:37:28 -0700774 // Try a simple Src as a canary. If it fails, skip this sink.
mtklein748ca3b2015-01-15 10:56:12 -0800775 struct : public Src {
mtkleine0effd62015-07-29 06:37:28 -0700776 Error draw(SkCanvas* c) const override {
777 c->drawRect(SkRect::MakeWH(1,1), SkPaint());
778 return "";
779 }
mtklein36352bf2015-03-25 18:17:31 -0700780 SkISize size() const override { return SkISize::Make(16, 16); }
mtkleine0effd62015-07-29 06:37:28 -0700781 Name name() const override { return "justOneRect"; }
782 } justOneRect;
mtklein114c3cd2015-01-15 10:15:02 -0800783
mtklein748ca3b2015-01-15 10:56:12 -0800784 SkBitmap bitmap;
785 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800786 SkString log;
mtkleine0effd62015-07-29 06:37:28 -0700787 Error err = sink->draw(justOneRect, &bitmap, &stream, &log);
mtklein4089ef72015-03-05 08:40:28 -0800788 if (err.isFatal()) {
mtkleinc41fd922016-03-08 09:01:39 -0800789 info("Could not run %s: %s\n", config.getTag().c_str(), err.c_str());
mtklein05641a52015-04-21 10:49:13 -0700790 exit(1);
mtklein114c3cd2015-01-15 10:15:02 -0800791 }
792
mtkleine0effd62015-07-29 06:37:28 -0700793 TaggedSink& ts = gSinks.push_back();
mtklein18300a32016-03-16 13:53:35 -0700794 ts.reset(sink.release());
kkinnunen3e980c32015-12-23 01:33:00 -0800795 ts.tag = config.getTag();
mtklein748ca3b2015-01-15 10:56:12 -0800796}
797
798static bool gpu_supported() {
799#if SK_SUPPORT_GPU
800 return FLAGS_gpu;
801#else
802 return false;
803#endif
804}
kkinnunen9ebc3f02015-12-21 23:48:13 -0800805
kkinnunen3e980c32015-12-23 01:33:00 -0800806static Sink* create_sink(const SkCommandLineConfig* config) {
807#if SK_SUPPORT_GPU
808 if (gpu_supported()) {
809 if (const SkCommandLineConfigGpu* gpuConfig = config->asConfigGpu()) {
810 GrContextFactory::GLContextType contextType = gpuConfig->getContextType();
811 GrContextFactory::GLContextOptions contextOptions =
812 GrContextFactory::kNone_GLContextOptions;
813 if (gpuConfig->getUseNVPR()) {
814 contextOptions = static_cast<GrContextFactory::GLContextOptions>(
815 contextOptions | GrContextFactory::kEnableNVPR_GLContextOptions);
816 }
817 GrContextFactory testFactory;
818 if (!testFactory.get(contextType, contextOptions)) {
mtkleinc41fd922016-03-08 09:01:39 -0800819 info("WARNING: can not create GPU context for config '%s'. "
820 "GM tests will be skipped.\n", gpuConfig->getTag().c_str());
kkinnunen3e980c32015-12-23 01:33:00 -0800821 return nullptr;
822 }
823 return new GPUSink(contextType, contextOptions, gpuConfig->getSamples(),
brianosmand93c1202016-03-10 07:49:08 -0800824 gpuConfig->getUseDIText(), gpuConfig->getColorType(),
825 gpuConfig->getProfileType(), FLAGS_gpu_threading);
kkinnunen3e980c32015-12-23 01:33:00 -0800826 }
827 }
828#endif
829
830#define SINK(t, sink, ...) if (config->getBackend().equals(t)) { return new sink(__VA_ARGS__); }
831
tomhudsoneebc39a2015-02-23 12:18:05 -0800832#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
833 SINK("hwui", HWUISink);
834#endif
835
mtklein748ca3b2015-01-15 10:56:12 -0800836 if (FLAGS_cpu) {
837 SINK("565", RasterSink, kRGB_565_SkColorType);
838 SINK("8888", RasterSink, kN32_SkColorType);
mtklein27c3fdd2016-02-26 14:43:21 -0800839 SINK("srgb", RasterSink, kN32_SkColorType, kSRGB_SkColorProfileType);
840 SINK("f16", RasterSink, kRGBA_F16_SkColorType);
halcanaryc11c62f2015-09-28 11:51:54 -0700841 SINK("pdf", PDFSink, "Pdfium");
842 SINK("pdf_poppler", PDFSink, "Poppler");
mtklein9c3f17d2015-01-28 11:35:18 -0800843 SINK("skp", SKPSink);
mtklein8a4527e2015-01-31 20:00:58 -0800844 SINK("svg", SVGSink);
845 SINK("null", NullSink);
halcanary47ef4d52015-03-03 09:13:09 -0800846 SINK("xps", XPSSink);
mtklein748ca3b2015-01-15 10:56:12 -0800847 }
848#undef SINK
halcanary96fcdcc2015-08-27 07:41:13 -0700849 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800850}
851
kkinnunen3e980c32015-12-23 01:33:00 -0800852static Sink* create_via(const SkString& tag, Sink* wrapped) {
853#define VIA(t, via, ...) if (tag.equals(t)) { return new via(__VA_ARGS__); }
halcanary96fcdcc2015-08-27 07:41:13 -0700854 VIA("twice", ViaTwice, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700855 VIA("serialize", ViaSerialization, wrapped);
mtklein4a34ecb2016-01-08 10:19:35 -0800856 VIA("pic", ViaPicture, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700857 VIA("2ndpic", ViaSecondPicture, wrapped);
mtkleind31c13d2015-05-05 12:59:56 -0700858 VIA("sp", ViaSingletonPictures, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700859 VIA("tiles", ViaTiles, 256, 256, nullptr, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800860 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
halcanary7a76f9c2016-02-03 11:53:18 -0800861 VIA("mojo", ViaMojo, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800862
mtkleind603b222015-02-17 11:13:33 -0800863 if (FLAGS_matrix.count() == 4) {
mtklein748ca3b2015-01-15 10:56:12 -0800864 SkMatrix m;
mtkleind603b222015-02-17 11:13:33 -0800865 m.reset();
866 m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
867 m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
868 m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
869 m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
870 VIA("matrix", ViaMatrix, m, wrapped);
871 VIA("upright", ViaUpright, m, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800872 }
tomhudson64de1e12015-03-05 08:01:07 -0800873
874#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
875 VIA("androidsdk", ViaAndroidSDK, wrapped);
876#endif
877
mtklein748ca3b2015-01-15 10:56:12 -0800878#undef VIA
halcanary96fcdcc2015-08-27 07:41:13 -0700879 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800880}
881
mtklein748ca3b2015-01-15 10:56:12 -0800882static void gather_sinks() {
kkinnunen3e980c32015-12-23 01:33:00 -0800883 SkCommandLineConfigArray configs;
884 ParseConfigs(FLAGS_config, &configs);
885 for (int i = 0; i < configs.count(); i++) {
886 const SkCommandLineConfig& config = *configs[i];
887 Sink* sink = create_sink(&config);
888 if (sink == nullptr) {
mtkleinc41fd922016-03-08 09:01:39 -0800889 info("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
890 config.getTag().c_str());
kkinnunen3e980c32015-12-23 01:33:00 -0800891 continue;
892 }
mtklein748ca3b2015-01-15 10:56:12 -0800893
kkinnunen3e980c32015-12-23 01:33:00 -0800894 const SkTArray<SkString>& parts = config.getViaParts();
895 for (int j = parts.count(); j-- > 0;) {
896 const SkString& part = parts[j];
897 Sink* next = create_via(part, sink);
halcanary96fcdcc2015-08-27 07:41:13 -0700898 if (next == nullptr) {
mtkleinc41fd922016-03-08 09:01:39 -0800899 info("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
900 part.c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800901 delete sink;
halcanary96fcdcc2015-08-27 07:41:13 -0700902 sink = nullptr;
mtklein748ca3b2015-01-15 10:56:12 -0800903 break;
904 }
905 sink = next;
906 }
907 if (sink) {
908 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800909 }
910 }
911}
mtklein709d2c32015-01-15 08:30:25 -0800912
mtkleina5114d72015-08-24 13:27:01 -0700913static bool dump_png(SkBitmap bitmap, const char* path, const char* md5) {
914 const int w = bitmap.width(),
915 h = bitmap.height();
mtklein27c3fdd2016-02-26 14:43:21 -0800916 // PNG wants unpremultiplied 8-bit RGBA pixels (16-bit could work fine too).
917 // We leave the gamma of these bytes unspecified, to continue the status quo,
918 // which we think generally is to interpret them as sRGB.
mtkleina5114d72015-08-24 13:27:01 -0700919
mtklein27c3fdd2016-02-26 14:43:21 -0800920 SkAutoTMalloc<uint32_t> rgba(w*h);
921
922 if (bitmap. colorType() == kN32_SkColorType &&
923 bitmap.profileType() == kSRGB_SkColorProfileType) {
924 // These are premul sRGB 8-bit pixels in SkPMColor order.
925 // We want unpremul sRGB 8-bit pixels in RGBA order. We'll get there via floats.
926 bitmap.lockPixels();
927 auto px = (const uint32_t*)bitmap.getPixels();
928 if (!px) {
mtkleina5114d72015-08-24 13:27:01 -0700929 return false;
930 }
mtklein27c3fdd2016-02-26 14:43:21 -0800931 for (int i = 0; i < w*h; i++) {
932 Sk4f fs = Sk4f_fromS32(px[i]); // Convert up to linear floats.
933 #if defined(SK_PMCOLOR_IS_BGRA)
934 fs = SkNx_shuffle<2,1,0,3>(fs); // Shuffle to RGBA, if not there already.
935 #endif
936 float invA = 1.0f / fs[3];
937 fs = fs * Sk4f(invA, invA, invA, 1); // Unpremultiply.
938 rgba[i] = Sk4f_toS32(fs); // Pack down to sRGB bytes.
939 }
mtkleina5114d72015-08-24 13:27:01 -0700940
mtklein27c3fdd2016-02-26 14:43:21 -0800941 } else if (bitmap.colorType() == kRGBA_F16_SkColorType) {
942 // These are premul linear half-float pixels in RGBA order.
943 // We want unpremul sRGB 8-bit pixels in RGBA order. We'll get there via floats.
944 bitmap.lockPixels();
945 auto px = (const uint64_t*)bitmap.getPixels();
946 if (!px) {
947 return false;
948 }
949 for (int i = 0; i < w*h; i++) {
950 Sk4f fs = SkHalfToFloat_01(px[i]); // Convert up to linear floats.
951 float invA = 1.0f / fs[3];
952 fs = fs * Sk4f(invA, invA, invA, 1); // Unpremultiply.
953 rgba[i] = Sk4f_toS32(fs); // Pack down to sRGB bytes.
954 }
955
956
957 } else {
958 // We "should" gamma correct in here but we don't.
959 // We want Gold to show exactly what our clients are seeing, broken gamma.
960
961 // Convert smaller formats up to premul linear 8-bit (in SkPMColor order).
962 if (bitmap.colorType() != kN32_SkColorType) {
963 SkBitmap n32;
964 if (!bitmap.copyTo(&n32, kN32_SkColorType)) {
965 return false;
966 }
967 bitmap = n32;
968 }
969
970 // Convert premul linear 8-bit to unpremul linear 8-bit RGBA.
971 if (!bitmap.readPixels(SkImageInfo::Make(w,h, kRGBA_8888_SkColorType,
972 kUnpremul_SkAlphaType),
973 rgba, 4*w, 0,0)) {
974 return false;
975 }
mtkleina5114d72015-08-24 13:27:01 -0700976 }
977
978 // We don't need bitmap anymore. Might as well drop our ref.
mtkleinfccb77d2015-08-24 14:13:29 -0700979 bitmap.reset();
mtkleina5114d72015-08-24 13:27:01 -0700980
mtkleinc64137c2015-08-25 10:56:08 -0700981 FILE* f = fopen(path, "wb");
mtkleina5114d72015-08-24 13:27:01 -0700982 if (!f) { return false; }
983
984 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
985 if (!png) {
986 fclose(f);
987 return false;
988 }
989
990 png_infop info = png_create_info_struct(png);
991 if (!info) {
992 png_destroy_write_struct(&png, &info);
993 fclose(f);
994 return false;
995 }
996
mtkleind69ece62015-09-10 10:37:44 -0700997 SkString description;
998 description.append("Key: ");
999 for (int i = 0; i < FLAGS_key.count(); i++) {
1000 description.appendf("%s ", FLAGS_key[i]);
1001 }
1002 description.append("Properties: ");
1003 for (int i = 0; i < FLAGS_properties.count(); i++) {
1004 description.appendf("%s ", FLAGS_properties[i]);
1005 }
1006 description.appendf("MD5: %s", md5);
1007
mtkleina5114d72015-08-24 13:27:01 -07001008 png_text text[2];
1009 text[0].key = (png_charp)"Author";
1010 text[0].text = (png_charp)"DM dump_png()";
1011 text[0].compression = PNG_TEXT_COMPRESSION_NONE;
1012 text[1].key = (png_charp)"Description";
mtkleind69ece62015-09-10 10:37:44 -07001013 text[1].text = (png_charp)description.c_str();
mtkleina5114d72015-08-24 13:27:01 -07001014 text[1].compression = PNG_TEXT_COMPRESSION_NONE;
1015 png_set_text(png, info, text, 2);
1016
1017 png_init_io(png, f);
1018 png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8,
1019 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
1020 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
1021 png_write_info(png, info);
1022 for (int j = 0; j < h; j++) {
1023 png_bytep row = (png_bytep)(rgba.get() + w*j);
1024 png_write_rows(png, &row, 1);
1025 }
1026 png_write_end(png, info);
1027
1028 png_destroy_write_struct(&png, &info);
1029 fclose(f);
1030 return true;
1031}
1032
mtkleina2ef6422015-01-15 13:44:22 -08001033static bool match(const char* needle, const char* haystack) {
halcanary96fcdcc2015-08-27 07:41:13 -07001034 return 0 == strcmp("_", needle) || nullptr != strstr(haystack, needle);
mtkleina2ef6422015-01-15 13:44:22 -08001035}
1036
mtklein3eed7dd2016-02-24 19:07:07 -08001037static bool is_blacklisted(const char* sink, const char* src,
1038 const char* srcOptions, const char* name) {
mtklein0d243ff2015-04-03 07:51:00 -07001039 for (int i = 0; i < FLAGS_blacklist.count() - 3; i += 4) {
mtkleina2ef6422015-01-15 13:44:22 -08001040 if (match(FLAGS_blacklist[i+0], sink) &&
djsollen54416de2015-04-03 07:24:48 -07001041 match(FLAGS_blacklist[i+1], src) &&
1042 match(FLAGS_blacklist[i+2], srcOptions) &&
1043 match(FLAGS_blacklist[i+3], name)) {
mtklein3eed7dd2016-02-24 19:07:07 -08001044 return true;
mtkleina2ef6422015-01-15 13:44:22 -08001045 }
1046 }
mtklein3eed7dd2016-02-24 19:07:07 -08001047 return false;
mtkleina2ef6422015-01-15 13:44:22 -08001048}
1049
mtkleincd50bca2016-01-05 06:20:20 -08001050// Even when a Task Sink reports to be non-threadsafe (e.g. GPU), we know things like
1051// .png encoding are definitely thread safe. This lets us offload that work to CPU threads.
1052static SkTaskGroup gDefinitelyThreadSafeWork;
1053
mtklein748ca3b2015-01-15 10:56:12 -08001054// The finest-grained unit of work we can run: draw a single Src into a single Sink,
1055// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
1056struct Task {
mtkleine0effd62015-07-29 06:37:28 -07001057 Task(const TaggedSrc& src, const TaggedSink& sink) : src(src), sink(sink) {}
1058 const TaggedSrc& src;
1059 const TaggedSink& sink;
mtklein748ca3b2015-01-15 10:56:12 -08001060
mtklein21eaf3b2016-02-08 12:39:59 -08001061 static void Run(const Task& task) {
1062 SkString name = task.src->name();
mtkleine0effd62015-07-29 06:37:28 -07001063
mtkleinb9eb4ac2015-02-02 18:26:03 -08001064 SkString log;
mtklein3eed7dd2016-02-24 19:07:07 -08001065 if (!FLAGS_dryRun) {
mtklein748ca3b2015-01-15 10:56:12 -08001066 SkBitmap bitmap;
1067 SkDynamicMemoryWStream stream;
mtklein3eed7dd2016-02-24 19:07:07 -08001068 start(task.sink.tag.c_str(), task.src.tag.c_str(),
1069 task.src.options.c_str(), name.c_str());
mtklein21eaf3b2016-02-08 12:39:59 -08001070 Error err = task.sink->draw(*task.src, &bitmap, &stream, &log);
mtkleinb3b13b72016-03-07 13:20:52 -08001071 if (!log.isEmpty()) {
mtkleinc41fd922016-03-08 09:01:39 -08001072 info("%s %s %s %s:\n%s\n", task.sink.tag.c_str()
1073 , task.src.tag.c_str()
1074 , task.src.options.c_str()
1075 , name.c_str()
1076 , log.c_str());
mtkleinb3b13b72016-03-07 13:20:52 -08001077 }
mtklein748ca3b2015-01-15 10:56:12 -08001078 if (!err.isEmpty()) {
mtklein4089ef72015-03-05 08:40:28 -08001079 if (err.isFatal()) {
djsollen54416de2015-04-03 07:24:48 -07001080 fail(SkStringPrintf("%s %s %s %s: %s",
mtklein21eaf3b2016-02-08 12:39:59 -08001081 task.sink.tag.c_str(),
1082 task.src.tag.c_str(),
1083 task.src.options.c_str(),
mtklein4089ef72015-03-05 08:40:28 -08001084 name.c_str(),
1085 err.c_str()));
mtkleinb37cb412015-03-18 05:27:14 -07001086 } else {
mtklein3eed7dd2016-02-24 19:07:07 -08001087 done(task.sink.tag.c_str(), task.src.tag.c_str(),
1088 task.src.options.c_str(), name.c_str());
mtkleinba6ada72016-01-21 09:39:35 -08001089 return;
mtklein4089ef72015-03-05 08:40:28 -08001090 }
mtklein748ca3b2015-01-15 10:56:12 -08001091 }
mtklein62bd1a62015-01-27 14:46:26 -08001092
mtkleincd50bca2016-01-05 06:20:20 -08001093 // We're likely switching threads here, so we must capture by value, [=] or [foo,bar].
1094 SkStreamAsset* data = stream.detachAsStream();
1095 gDefinitelyThreadSafeWork.add([task,name,bitmap,data]{
1096 SkAutoTDelete<SkStreamAsset> ownedData(data);
1097
1098 // Why doesn't the copy constructor do this when we have pre-locked pixels?
1099 bitmap.lockPixels();
1100
1101 SkString md5;
1102 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
1103 SkMD5 hash;
1104 if (data->getLength()) {
1105 hash.writeStream(data, data->getLength());
1106 data->rewind();
mtklein38408462015-07-08 07:25:27 -07001107 } else {
mtkleincd50bca2016-01-05 06:20:20 -08001108 // If we're BGRA (Linux, Windows), swizzle over to RGBA (Mac, Android).
1109 // This helps eliminate multiple 0-pixel-diff hashes on gold.skia.org.
1110 // (Android's general slow speed breaks the tie arbitrarily in RGBA's favor.)
1111 // We might consider promoting 565 to RGBA too.
1112 if (bitmap.colorType() == kBGRA_8888_SkColorType) {
1113 SkBitmap swizzle;
1114 SkAssertResult(bitmap.copyTo(&swizzle, kRGBA_8888_SkColorType));
1115 hash.write(swizzle.getPixels(), swizzle.getSize());
1116 } else {
1117 hash.write(bitmap.getPixels(), bitmap.getSize());
1118 }
1119 }
1120 SkMD5::Digest digest;
1121 hash.finish(digest);
1122 for (int i = 0; i < 16; i++) {
1123 md5.appendf("%02x", digest.data[i]);
mtklein38408462015-07-08 07:25:27 -07001124 }
mtklein62bd1a62015-01-27 14:46:26 -08001125 }
mtklein62bd1a62015-01-27 14:46:26 -08001126
mtkleincd50bca2016-01-05 06:20:20 -08001127 if (!FLAGS_readPath.isEmpty() &&
mtklein3eed7dd2016-02-24 19:07:07 -08001128 !gGold.contains(Gold(task.sink.tag, task.src.tag,
1129 task.src.options, name, md5))) {
mtkleincd50bca2016-01-05 06:20:20 -08001130 fail(SkStringPrintf("%s not found for %s %s %s %s in %s",
1131 md5.c_str(),
mtklein21eaf3b2016-02-08 12:39:59 -08001132 task.sink.tag.c_str(),
1133 task.src.tag.c_str(),
1134 task.src.options.c_str(),
mtkleincd50bca2016-01-05 06:20:20 -08001135 name.c_str(),
1136 FLAGS_readPath[0]));
mtklein748ca3b2015-01-15 10:56:12 -08001137 }
mtkleincd50bca2016-01-05 06:20:20 -08001138
1139 if (!FLAGS_writePath.isEmpty()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001140 const char* ext = task.sink->fileExtension();
mtkleincd50bca2016-01-05 06:20:20 -08001141 if (data->getLength()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001142 WriteToDisk(task, md5, ext, data, data->getLength(), nullptr);
mtkleincd50bca2016-01-05 06:20:20 -08001143 SkASSERT(bitmap.drawsNothing());
1144 } else if (!bitmap.drawsNothing()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001145 WriteToDisk(task, md5, ext, nullptr, 0, &bitmap);
mtkleincd50bca2016-01-05 06:20:20 -08001146 }
1147 }
1148 });
mtklein748ca3b2015-01-15 10:56:12 -08001149 }
mtklein3eed7dd2016-02-24 19:07:07 -08001150 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 -08001151 }
1152
1153 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -08001154 SkString md5,
1155 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -08001156 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -08001157 const SkBitmap* bitmap) {
mtklein409d4702016-02-29 07:38:01 -08001158 bool gammaCorrect = false;
1159 if (bitmap) {
1160 gammaCorrect = bitmap->profileType() == kSRGB_SkColorProfileType
1161 || bitmap-> colorType() == kRGBA_F16_SkColorType;
1162 }
1163
mtklein748ca3b2015-01-15 10:56:12 -08001164 JsonWriter::BitmapResult result;
djsollen54416de2015-04-03 07:24:48 -07001165 result.name = task.src->name();
mtklein3eed7dd2016-02-24 19:07:07 -08001166 result.config = task.sink.tag;
djsollen54416de2015-04-03 07:24:48 -07001167 result.sourceType = task.src.tag;
1168 result.sourceOptions = task.src.options;
1169 result.ext = ext;
mtklein409d4702016-02-29 07:38:01 -08001170 result.gammaCorrect = gammaCorrect;
djsollen54416de2015-04-03 07:24:48 -07001171 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -08001172 JsonWriter::AddBitmapResult(result);
1173
mtkleinb0531a72015-04-07 13:38:48 -07001174 // If an MD5 is uninteresting, we want it noted in the JSON file,
1175 // but don't want to dump it out as a .png (or whatever ext is).
1176 if (gUninterestingHashes.contains(md5)) {
1177 return;
1178 }
1179
mtklein748ca3b2015-01-15 10:56:12 -08001180 const char* dir = FLAGS_writePath[0];
1181 if (0 == strcmp(dir, "@")) { // Needed for iOS.
1182 dir = FLAGS_resourcePath[0];
1183 }
1184 sk_mkdir(dir);
1185
1186 SkString path;
1187 if (FLAGS_nameByHash) {
1188 path = SkOSPath::Join(dir, result.md5.c_str());
1189 path.append(".");
1190 path.append(ext);
1191 if (sk_exists(path.c_str())) {
1192 return; // Content-addressed. If it exists already, we're done.
1193 }
1194 } else {
kkinnunen3e980c32015-12-23 01:33:00 -08001195 path = SkOSPath::Join(dir, task.sink.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001196 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001197 path = SkOSPath::Join(path.c_str(), task.src.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001198 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001199 if (strcmp(task.src.options.c_str(), "") != 0) {
1200 path = SkOSPath::Join(path.c_str(), task.src.options.c_str());
djsollen54416de2015-04-03 07:24:48 -07001201 sk_mkdir(path.c_str());
1202 }
mtklein748ca3b2015-01-15 10:56:12 -08001203 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
1204 path.append(".");
1205 path.append(ext);
1206 }
1207
mtklein748ca3b2015-01-15 10:56:12 -08001208 if (bitmap) {
mtkleina5114d72015-08-24 13:27:01 -07001209 if (!dump_png(*bitmap, path.c_str(), result.md5.c_str())) {
mtklein748ca3b2015-01-15 10:56:12 -08001210 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
1211 return;
1212 }
1213 } else {
mtkleina5114d72015-08-24 13:27:01 -07001214 SkFILEWStream file(path.c_str());
1215 if (!file.isValid()) {
1216 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
1217 return;
1218 }
mtklein748ca3b2015-01-15 10:56:12 -08001219 if (!file.writeStream(data, len)) {
1220 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
1221 return;
1222 }
1223 }
1224 }
1225};
1226
mtklein748ca3b2015-01-15 10:56:12 -08001227/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1228
1229// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
1230
mtklein21eaf3b2016-02-08 12:39:59 -08001231static SkTDArray<skiatest::Test> gParallelTests, gSerialTests;
mtklein748ca3b2015-01-15 10:56:12 -08001232
1233static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -08001234 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -08001235 return;
1236 }
mtklein6393c062015-04-27 08:45:01 -07001237 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r; r = r->next()) {
1238 if (!in_shard()) {
1239 continue;
1240 }
halcanary87f3ba42015-01-20 09:30:20 -08001241 // Despite its name, factory() is returning a reference to
1242 // link-time static const POD data.
1243 const skiatest::Test& test = r->factory();
1244 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -08001245 continue;
1246 }
halcanary87f3ba42015-01-20 09:30:20 -08001247 if (test.needsGpu && gpu_supported()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001248 (FLAGS_gpu_threading ? gParallelTests : gSerialTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -08001249 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein21eaf3b2016-02-08 12:39:59 -08001250 gParallelTests.push(test);
mtklein82d28432015-01-15 12:46:02 -08001251 }
mtklein748ca3b2015-01-15 10:56:12 -08001252 }
1253}
1254
mtklein21eaf3b2016-02-08 12:39:59 -08001255static void run_test(skiatest::Test test) {
halcanary87f3ba42015-01-20 09:30:20 -08001256 struct : public skiatest::Reporter {
mtklein36352bf2015-03-25 18:17:31 -07001257 void reportFailed(const skiatest::Failure& failure) override {
halcanary87f3ba42015-01-20 09:30:20 -08001258 fail(failure.toString());
1259 JsonWriter::AddTestFailure(failure);
1260 }
mtklein36352bf2015-03-25 18:17:31 -07001261 bool allowExtendedTest() const override {
halcanary87f3ba42015-01-20 09:30:20 -08001262 return FLAGS_pathOpsExtended;
1263 }
mtklein36352bf2015-03-25 18:17:31 -07001264 bool verbose() const override { return FLAGS_veryVerbose; }
halcanary87f3ba42015-01-20 09:30:20 -08001265 } reporter;
djsollen824996a2015-06-12 12:06:22 -07001266
mtklein3eed7dd2016-02-24 19:07:07 -08001267 if (!FLAGS_dryRun && !is_blacklisted("_", "tests", "_", test.name)) {
mtklein21eaf3b2016-02-08 12:39:59 -08001268 start("unit", "test", "", test.name);
mtklein55e88b22015-01-21 15:50:13 -08001269 GrContextFactory factory;
mtklein21eaf3b2016-02-08 12:39:59 -08001270 test.proc(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -08001271 }
mtklein3eed7dd2016-02-24 19:07:07 -08001272 done("unit", "test", "", test.name);
mtklein748ca3b2015-01-15 10:56:12 -08001273}
1274
1275/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1276
mtkleinafae30a2016-02-24 12:28:32 -08001277DEFINE_int32(status_sec, 15, "Print status this often (and if we crash).");
1278
1279SkThread* start_status_thread() {
1280 auto thread = new SkThread([] (void*) {
1281 for (;;) {
1282 print_status();
1283 #if defined(SK_BUILD_FOR_WIN)
1284 Sleep(FLAGS_status_sec * 1000);
1285 #else
1286 sleep(FLAGS_status_sec);
1287 #endif
mtklein2e1c47e2015-03-12 07:16:56 -07001288 }
mtkleinafae30a2016-02-24 12:28:32 -08001289 });
1290 thread->start();
1291 return thread;
mtkleinde6fc2b2015-03-12 06:28:54 -07001292}
1293
caryclark83ca6282015-06-10 09:31:09 -07001294#define PORTABLE_FONT_PREFIX "Toy Liberation "
1295
1296static SkTypeface* create_from_name(const char familyName[], SkTypeface::Style style) {
1297 if (familyName && strlen(familyName) > sizeof(PORTABLE_FONT_PREFIX)
1298 && !strncmp(familyName, PORTABLE_FONT_PREFIX, sizeof(PORTABLE_FONT_PREFIX) - 1)) {
caryclark1818acb2015-07-24 12:09:25 -07001299 return sk_tool_utils::create_portable_typeface(familyName, style);
caryclark83ca6282015-06-10 09:31:09 -07001300 }
halcanary96fcdcc2015-08-27 07:41:13 -07001301 return nullptr;
caryclark83ca6282015-06-10 09:31:09 -07001302}
1303
1304#undef PORTABLE_FONT_PREFIX
1305
1306extern SkTypeface* (*gCreateTypefaceDelegate)(const char [], SkTypeface::Style );
1307
jcgregorio3b27ade2014-11-13 08:06:40 -08001308int dm_main();
caryclark17f0b6d2014-07-22 10:15:34 -07001309int dm_main() {
mtklein246ba3a2016-02-23 10:39:36 -08001310 setup_crash_handler();
mtkleinafae30a2016-02-24 12:28:32 -08001311
mtklein51c8cfc2016-03-11 12:59:09 -08001312 if (FLAGS_verbose && !FLAGS_writePath.isEmpty()) {
1313 sk_mkdir(FLAGS_writePath[0]);
1314 gVLog = freopen(SkOSPath::Join(FLAGS_writePath[0], "verbose.log").c_str(), "w", stderr);
mtkleinc41fd922016-03-08 09:01:39 -08001315 }
1316
mtklein5286f022016-01-22 08:18:14 -08001317 JsonWriter::DumpJson(); // It's handy for the bots to assume this is ~never missing.
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +00001318 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -07001319 SkTaskGroup::Enabler enabled(FLAGS_threads);
caryclark83ca6282015-06-10 09:31:09 -07001320 gCreateTypefaceDelegate = &create_from_name;
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +00001321
halcanary7d571242016-02-24 17:59:16 -08001322 {
1323 SkString testResourcePath = GetResourcePath("color_wheel.png");
1324 SkFILEStream testResource(testResourcePath.c_str());
1325 if (!testResource.isValid()) {
mtkleinc41fd922016-03-08 09:01:39 -08001326 info("Some resources are missing. Do you need to set --resourcePath?\n");
halcanary7d571242016-02-24 17:59:16 -08001327 }
1328 }
mtklein62bd1a62015-01-27 14:46:26 -08001329 gather_gold();
borenet09ed4802015-04-03 14:15:33 -07001330 gather_uninteresting_hashes();
mtklein62bd1a62015-01-27 14:46:26 -08001331
scroggo86737142016-02-03 12:19:11 -08001332 if (!gather_srcs()) {
1333 return 1;
1334 }
mtklein748ca3b2015-01-15 10:56:12 -08001335 gather_sinks();
1336 gather_tests();
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +00001337
mtklein21eaf3b2016-02-08 12:39:59 -08001338 gPending = gSrcs.count() * gSinks.count() + gParallelTests.count() + gSerialTests.count();
mtkleinc41fd922016-03-08 09:01:39 -08001339 info("%d srcs * %d sinks + %d tests == %d tasks",
1340 gSrcs.count(), gSinks.count(), gParallelTests.count() + gSerialTests.count(), gPending);
mtkleinafae30a2016-02-24 12:28:32 -08001341 SkAutoTDelete<SkThread> statusThread(start_status_thread());
mtklein748ca3b2015-01-15 10:56:12 -08001342
mtklein21eaf3b2016-02-08 12:39:59 -08001343 // Kick off as much parallel work as we can, making note of any serial work we'll need to do.
1344 SkTaskGroup parallel;
1345 SkTArray<Task> serial;
1346
1347 for (auto& sink : gSinks)
1348 for (auto& src : gSrcs) {
mtklein3eed7dd2016-02-24 19:07:07 -08001349 if (src->veto(sink->flags()) ||
1350 is_blacklisted(sink.tag.c_str(), src.tag.c_str(),
1351 src.options.c_str(), src->name().c_str())) {
mtklein15923c92016-02-29 10:14:38 -08001352 SkAutoTAcquire<SkSpinlock> lock(gMutex);
mtklein3eed7dd2016-02-24 19:07:07 -08001353 gPending--;
1354 continue;
1355 }
1356
mtklein21eaf3b2016-02-08 12:39:59 -08001357 Task task(src, sink);
1358 if (src->serial() || sink->serial()) {
1359 serial.push_back(task);
1360 } else {
1361 parallel.add([task] { Task::Run(task); });
mtklein748ca3b2015-01-15 10:56:12 -08001362 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001363 }
mtklein21eaf3b2016-02-08 12:39:59 -08001364 for (auto test : gParallelTests) {
1365 parallel.add([test] { run_test(test); });
mtklein55e88b22015-01-21 15:50:13 -08001366 }
mtklein21eaf3b2016-02-08 12:39:59 -08001367
1368 // With the parallel work running, run serial tasks and tests here on main thread.
1369 for (auto task : serial) { Task::Run(task); }
1370 for (auto test : gSerialTests) { run_test(test); }
1371
1372 // Wait for any remaining parallel work to complete (including any spun off of serial tasks).
1373 parallel.wait();
mtkleincd50bca2016-01-05 06:20:20 -08001374 gDefinitelyThreadSafeWork.wait();
1375
mtkleinda884c42016-02-24 18:00:23 -08001376 // We'd better have run everything.
1377 SkASSERT(gPending == 0);
mtkleine027f172016-02-26 15:53:06 -08001378 // Make sure we've flushed all our results to disk.
1379 JsonWriter::DumpJson();
mtkleinda884c42016-02-24 18:00:23 -08001380
mtklein748ca3b2015-01-15 10:56:12 -08001381 // At this point we're back in single-threaded land.
caryclarkf53ce802015-06-15 06:48:30 -07001382 sk_tool_utils::release_portable_typefaces();
mtklein@google.comd36522d2013-10-16 13:02:15 +00001383
mtklein748ca3b2015-01-15 10:56:12 -08001384 if (gFailures.count() > 0) {
mtkleinc41fd922016-03-08 09:01:39 -08001385 info("Failures:\n");
mtklein748ca3b2015-01-15 10:56:12 -08001386 for (int i = 0; i < gFailures.count(); i++) {
mtkleinc41fd922016-03-08 09:01:39 -08001387 info("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001388 }
mtkleinc41fd922016-03-08 09:01:39 -08001389 info("%d failures\n", gFailures.count());
mtklein748ca3b2015-01-15 10:56:12 -08001390 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001391 }
mtkleinafae30a2016-02-24 12:28:32 -08001392
1393#ifdef SK_PDF_IMAGE_STATS
halcanary7a14b312015-10-01 07:28:13 -07001394 SkPDFImageDumpStats();
mtkleinafae30a2016-02-24 12:28:32 -08001395#endif // SK_PDF_IMAGE_STATS
1396
1397 print_status();
mtkleinc41fd922016-03-08 09:01:39 -08001398 info("Finished!\n");
mtklein748ca3b2015-01-15 10:56:12 -08001399 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +00001400}
jcgregorio3b27ade2014-11-13 08:06:40 -08001401
kkinnunen179a8f52015-11-20 13:32:24 -08001402// TODO: currently many GPU tests are declared outside SK_SUPPORT_GPU guards.
1403// Thus we export the empty RunWithGPUTestContexts when SK_SUPPORT_GPU=0.
1404namespace skiatest {
1405namespace {
1406typedef void(*TestWithGrContext)(skiatest::Reporter*, GrContext*);
1407typedef void(*TestWithGrContextAndGLContext)(skiatest::Reporter*, GrContext*, SkGLContext*);
1408#if SK_SUPPORT_GPU
1409template<typename T>
kkinnunen34058002016-01-06 23:49:30 -08001410void call_test(T test, skiatest::Reporter* reporter, const GrContextFactory::ContextInfo& context);
kkinnunen179a8f52015-11-20 13:32:24 -08001411template<>
1412void call_test(TestWithGrContext test, skiatest::Reporter* reporter,
kkinnunen34058002016-01-06 23:49:30 -08001413 const GrContextFactory::ContextInfo& context) {
1414 test(reporter, context.fGrContext);
kkinnunen179a8f52015-11-20 13:32:24 -08001415}
1416template<>
1417void call_test(TestWithGrContextAndGLContext test, skiatest::Reporter* reporter,
kkinnunen34058002016-01-06 23:49:30 -08001418 const GrContextFactory::ContextInfo& context) {
1419 test(reporter, context.fGrContext, context.fGLContext);
kkinnunen179a8f52015-11-20 13:32:24 -08001420}
1421#endif
1422} // namespace
1423
kkinnunen179a8f52015-11-20 13:32:24 -08001424template<typename T>
1425void RunWithGPUTestContexts(T test, GPUTestContexts testContexts, Reporter* reporter,
1426 GrContextFactory* factory) {
1427#if SK_SUPPORT_GPU
kkinnunen3e980c32015-12-23 01:33:00 -08001428 // Iterate over context types, except use "native" instead of explicitly trying OpenGL and
1429 // OpenGL ES. Do not use GLES on desktop, since tests do not account for not fixing
1430 // http://skbug.com/2809
1431 GrContextFactory::GLContextType contextTypes[] = {
1432 GrContextFactory::kNative_GLContextType,
1433#if SK_ANGLE
1434#ifdef SK_BUILD_FOR_WIN
1435 GrContextFactory::kANGLE_GLContextType,
1436#endif
1437 GrContextFactory::kANGLE_GL_GLContextType,
1438#endif
1439#if SK_COMMAND_BUFFER
kkinnunenf655e932016-03-03 07:39:48 -08001440 GrContextFactory::kCommandBuffer_GLContextType,
kkinnunen3e980c32015-12-23 01:33:00 -08001441#endif
1442#if SK_MESA
1443 GrContextFactory::kMESA_GLContextType,
1444#endif
1445 GrContextFactory::kNull_GLContextType,
1446 GrContextFactory::kDebug_GLContextType,
1447 };
1448 static_assert(SK_ARRAY_COUNT(contextTypes) == GrContextFactory::kGLContextTypeCnt - 2,
1449 "Skipping unexpected GLContextType for GPU tests");
1450
1451 for (auto& contextType : contextTypes) {
kkinnunen179a8f52015-11-20 13:32:24 -08001452 int contextSelector = kNone_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001453 if (GrContextFactory::IsRenderingGLContext(contextType)) {
kkinnunen179a8f52015-11-20 13:32:24 -08001454 contextSelector |= kAllRendering_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001455 } else if (contextType == GrContextFactory::kNative_GLContextType) {
kkinnunen179a8f52015-11-20 13:32:24 -08001456 contextSelector |= kNative_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001457 } else if (contextType == GrContextFactory::kNull_GLContextType) {
kkinnunen179a8f52015-11-20 13:32:24 -08001458 contextSelector |= kNull_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001459 } else if (contextType == GrContextFactory::kDebug_GLContextType) {
kkinnunen55eeae92015-12-10 23:19:29 -08001460 contextSelector |= kDebug_GPUTestContexts;
kkinnunen179a8f52015-11-20 13:32:24 -08001461 }
1462 if ((testContexts & contextSelector) == 0) {
1463 continue;
1464 }
kkinnunen34058002016-01-06 23:49:30 -08001465 GrContextFactory::ContextInfo context = factory->getContextInfo(contextType);
1466 if (context.fGrContext) {
kkinnunen5219fd92015-12-10 06:28:13 -08001467 call_test(test, reporter, context);
1468 }
kkinnunen34058002016-01-06 23:49:30 -08001469 context = factory->getContextInfo(contextType,
1470 GrContextFactory::kEnableNVPR_GLContextOptions);
1471 if (context.fGrContext) {
kkinnunen179a8f52015-11-20 13:32:24 -08001472 call_test(test, reporter, context);
1473 }
1474 }
1475#endif
1476}
1477
1478template
1479void RunWithGPUTestContexts<TestWithGrContext>(TestWithGrContext test,
1480 GPUTestContexts testContexts,
1481 Reporter* reporter,
1482 GrContextFactory* factory);
1483template
1484void RunWithGPUTestContexts<TestWithGrContextAndGLContext>(TestWithGrContextAndGLContext test,
1485 GPUTestContexts testContexts,
1486 Reporter* reporter,
1487 GrContextFactory* factory);
1488} // namespace skiatest
1489
borenet48087572015-04-02 12:16:36 -07001490#if !defined(SK_BUILD_FOR_IOS)
jcgregorio3b27ade2014-11-13 08:06:40 -08001491int main(int argc, char** argv) {
1492 SkCommandLineFlags::Parse(argc, argv);
1493 return dm_main();
1494}
1495#endif