blob: 86baa00701c86ab5ac44c62f27773c491f6bae75 [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;
bsalomon3724e572016-03-30 18:56:19 -070073using sk_gpu_test::GrContextFactory;
bsalomon273c0f52016-03-31 10:59:06 -070074using sk_gpu_test::GLTestContext;
bsalomonf2f1c172016-04-05 12:59:06 -070075using sk_gpu_test::ContextInfo;
mtklein@google.comd36522d2013-10-16 13:02:15 +000076
mtklein748ca3b2015-01-15 10:56:12 -080077/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
78
benjaminwagnerec4d4d72016-03-25 12:59:53 -070079static const double kStartMs = SkTime::GetMSecs();
mtkleinc41fd922016-03-08 09:01:39 -080080
81static FILE* gVLog;
82
83template <typename... Args>
84static void vlog(const char* fmt, Args&&... args) {
85 if (gVLog) {
86 fprintf(gVLog, "%s\t", HumanizeMs(SkTime::GetMSecs() - kStartMs).c_str());
87 fprintf(gVLog, fmt, args...);
88 fflush(gVLog);
89 }
90}
91
92template <typename... Args>
93static void info(const char* fmt, Args&&... args) {
94 vlog(fmt, args...);
95 if (!FLAGS_quiet) {
96 printf(fmt, args...);
97 }
98}
99static void info(const char* fmt) {
100 if (!FLAGS_quiet) {
101 printf("%s", fmt); // Clang warns printf(fmt) is insecure.
102 }
103}
104
mtklein748ca3b2015-01-15 10:56:12 -0800105SK_DECLARE_STATIC_MUTEX(gFailuresMutex);
106static SkTArray<SkString> gFailures;
107
mtklein3eed7dd2016-02-24 19:07:07 -0800108static void fail(const SkString& err) {
mtklein748ca3b2015-01-15 10:56:12 -0800109 SkAutoMutexAcquire lock(gFailuresMutex);
110 SkDebugf("\n\nFAILURE: %s\n\n", err.c_str());
111 gFailures.push_back(err);
halcanary9e398f72015-01-10 11:18:04 -0800112}
113
mtkleinb37cb412015-03-18 05:27:14 -0700114
mtklein246ba3a2016-02-23 10:39:36 -0800115// We use a spinlock to make locking this in a signal handler _somewhat_ safe.
mtklein15923c92016-02-29 10:14:38 -0800116static SkSpinlock gMutex;
mtklein3eed7dd2016-02-24 19:07:07 -0800117static int32_t gPending;
118static SkTArray<SkString> gRunning;
mtklein748ca3b2015-01-15 10:56:12 -0800119
mtklein3eed7dd2016-02-24 19:07:07 -0800120static void done(const char* config, const char* src, const char* srcOptions, const char* name) {
121 SkString id = SkStringPrintf("%s %s %s %s", config, src, srcOptions, name);
mtkleinc41fd922016-03-08 09:01:39 -0800122 vlog("done %s\n", id.c_str());
mtkleinafae30a2016-02-24 12:28:32 -0800123 int pending;
mtkleinb37cb412015-03-18 05:27:14 -0700124 {
mtklein15923c92016-02-29 10:14:38 -0800125 SkAutoTAcquire<SkSpinlock> lock(gMutex);
mtkleinb37cb412015-03-18 05:27:14 -0700126 for (int i = 0; i < gRunning.count(); i++) {
127 if (gRunning[i] == id) {
128 gRunning.removeShuffle(i);
129 break;
130 }
131 }
mtkleinafae30a2016-02-24 12:28:32 -0800132 pending = --gPending;
reed50bc0512015-05-19 14:13:31 -0700133 }
mtkleina17241b2015-01-23 05:48:00 -0800134 // We write our dm.json file every once in a while in case we crash.
135 // Notice this also handles the final dm.json when pending == 0.
136 if (pending % 500 == 0) {
137 JsonWriter::DumpJson();
138 }
mtklein@google.comd36522d2013-10-16 13:02:15 +0000139}
140
mtklein3eed7dd2016-02-24 19:07:07 -0800141static void start(const char* config, const char* src, const char* srcOptions, const char* name) {
142 SkString id = SkStringPrintf("%s %s %s %s", config, src, srcOptions, name);
mtkleinc41fd922016-03-08 09:01:39 -0800143 vlog("start %s\n", id.c_str());
mtklein15923c92016-02-29 10:14:38 -0800144 SkAutoTAcquire<SkSpinlock> lock(gMutex);
mtkleinb37cb412015-03-18 05:27:14 -0700145 gRunning.push_back(id);
146}
147
mtkleinafae30a2016-02-24 12:28:32 -0800148static void print_status() {
mtkleinafae30a2016-02-24 12:28:32 -0800149 int curr = sk_tools::getCurrResidentSetSizeMB(),
150 peak = sk_tools::getMaxResidentSetSizeMB();
mtkleinc41fd922016-03-08 09:01:39 -0800151 SkString elapsed = HumanizeMs(SkTime::GetMSecs() - kStartMs);
mtkleinafae30a2016-02-24 12:28:32 -0800152
mtklein15923c92016-02-29 10:14:38 -0800153 SkAutoTAcquire<SkSpinlock> lock(gMutex);
mtkleinc41fd922016-03-08 09:01:39 -0800154 info("\n%s elapsed, %d active, %d queued, %dMB RAM, %dMB peak\n",
155 elapsed.c_str(), gRunning.count(), gPending - gRunning.count(), curr, peak);
mtkleinafae30a2016-02-24 12:28:32 -0800156 for (auto& task : gRunning) {
mtkleinc41fd922016-03-08 09:01:39 -0800157 info("\t%s\n", task.c_str());
mtkleinafae30a2016-02-24 12:28:32 -0800158 }
159}
160
mtkleinf8557292016-02-29 06:35:28 -0800161// Yo dawg, I heard you like signals so I caught a signal in your
162// signal handler so you can handle signals while you handle signals.
163// Let's not get into that situation. Only print if we're the first ones to get a crash signal.
164static std::atomic<bool> in_signal_handler{false};
165
mtklein246ba3a2016-02-23 10:39:36 -0800166#if defined(SK_BUILD_FOR_WIN32)
mtkleinf8557292016-02-29 06:35:28 -0800167 static LONG WINAPI handler(EXCEPTION_POINTERS* e) {
168 static const struct {
169 const char* name;
robertphillips75467862016-03-01 14:10:23 -0800170 DWORD code;
mtkleinf8557292016-02-29 06:35:28 -0800171 } kExceptions[] = {
172 #define _(E) {#E, E}
173 _(EXCEPTION_ACCESS_VIOLATION),
174 _(EXCEPTION_BREAKPOINT),
175 _(EXCEPTION_INT_DIVIDE_BY_ZERO),
176 _(EXCEPTION_STACK_OVERFLOW),
177 // TODO: more?
178 #undef _
179 };
180
181 if (!in_signal_handler.exchange(true)) {
182 const DWORD code = e->ExceptionRecord->ExceptionCode;
mtkleinc41fd922016-03-08 09:01:39 -0800183 info("\nCaught exception %u", code);
mtkleinf8557292016-02-29 06:35:28 -0800184 for (const auto& exception : kExceptions) {
185 if (exception.code == code) {
mtkleinc41fd922016-03-08 09:01:39 -0800186 info(" %s", exception.name);
mtkleinf8557292016-02-29 06:35:28 -0800187 }
188 }
mtkleinc41fd922016-03-08 09:01:39 -0800189 info("\n");
mtkleinf8557292016-02-29 06:35:28 -0800190 print_status();
mtklein13bf3392016-03-17 10:16:12 -0700191 fflush(stdout);
mtkleinf8557292016-02-29 06:35:28 -0800192 }
193 // Execute default exception handler... hopefully, exit.
194 return EXCEPTION_EXECUTE_HANDLER;
mtklein246ba3a2016-02-23 10:39:36 -0800195 }
mtkleinf8557292016-02-29 06:35:28 -0800196 static void setup_crash_handler() { SetUnhandledExceptionFilter(handler); }
mtklein246ba3a2016-02-23 10:39:36 -0800197
mtklein819ab102016-02-29 17:02:52 -0800198#elif !defined(SK_BUILD_FOR_ANDROID)
199 #include <execinfo.h>
mtklein246ba3a2016-02-23 10:39:36 -0800200 #include <signal.h>
mtklein819ab102016-02-29 17:02:52 -0800201 #include <stdlib.h>
mtkleinf8557292016-02-29 06:35:28 -0800202
mtklein246ba3a2016-02-23 10:39:36 -0800203 static void setup_crash_handler() {
204 const int kSignals[] = { SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGSEGV };
205 for (int sig : kSignals) {
206 signal(sig, [](int sig) {
mtkleinf8557292016-02-29 06:35:28 -0800207 if (!in_signal_handler.exchange(true)) {
mtklein819ab102016-02-29 17:02:52 -0800208 SkAutoTAcquire<SkSpinlock> lock(gMutex);
mtkleinc41fd922016-03-08 09:01:39 -0800209 info("\nCaught signal %d [%s], was running:\n", sig, strsignal(sig));
mtklein819ab102016-02-29 17:02:52 -0800210 for (auto& task : gRunning) {
mtkleinc41fd922016-03-08 09:01:39 -0800211 info("\t%s\n", task.c_str());
mtklein819ab102016-02-29 17:02:52 -0800212 }
213
214 void* stack[64];
215 int count = backtrace(stack, SK_ARRAY_COUNT(stack));
216 char** symbols = backtrace_symbols(stack, count);
mtkleinc41fd922016-03-08 09:01:39 -0800217 info("\nStack trace:\n");
mtklein819ab102016-02-29 17:02:52 -0800218 for (int i = 0; i < count; i++) {
mtkleinc41fd922016-03-08 09:01:39 -0800219 info(" %s\n", symbols[i]);
mtklein819ab102016-02-29 17:02:52 -0800220 }
mtklein13bf3392016-03-17 10:16:12 -0700221 fflush(stdout);
mtkleinf8557292016-02-29 06:35:28 -0800222 }
mtklein819ab102016-02-29 17:02:52 -0800223 _Exit(sig);
mtklein246ba3a2016-02-23 10:39:36 -0800224 });
225 }
226 }
mtklein819ab102016-02-29 17:02:52 -0800227
228#else // Android
229 static void setup_crash_handler() {}
mtklein246ba3a2016-02-23 10:39:36 -0800230#endif
231
mtklein748ca3b2015-01-15 10:56:12 -0800232/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
mtklein114c3cd2015-01-15 10:15:02 -0800233
mtklein62bd1a62015-01-27 14:46:26 -0800234struct Gold : public SkString {
mtkleina82f5622015-02-20 12:30:19 -0800235 Gold() : SkString("") {}
mtklein3eed7dd2016-02-24 19:07:07 -0800236 Gold(const SkString& sink, const SkString& src,
237 const SkString& srcOptions, const SkString& name,
238 const SkString& md5)
mtklein62bd1a62015-01-27 14:46:26 -0800239 : SkString("") {
240 this->append(sink);
241 this->append(src);
djsollen54416de2015-04-03 07:24:48 -0700242 this->append(srcOptions);
mtklein62bd1a62015-01-27 14:46:26 -0800243 this->append(name);
244 this->append(md5);
mtklein62bd1a62015-01-27 14:46:26 -0800245 }
mtkleinc8d1dd42015-10-15 12:23:01 -0700246 struct Hash {
247 uint32_t operator()(const Gold& g) const {
248 return SkGoodHash()((const SkString&)g);
249 }
250 };
mtklein62bd1a62015-01-27 14:46:26 -0800251};
mtkleina82f5622015-02-20 12:30:19 -0800252static SkTHashSet<Gold, Gold::Hash> gGold;
mtklein62bd1a62015-01-27 14:46:26 -0800253
254static void add_gold(JsonWriter::BitmapResult r) {
djsollen54416de2015-04-03 07:24:48 -0700255 gGold.add(Gold(r.config, r.sourceType, r.sourceOptions, r.name, r.md5));
mtklein62bd1a62015-01-27 14:46:26 -0800256}
257
258static void gather_gold() {
259 if (!FLAGS_readPath.isEmpty()) {
260 SkString path(FLAGS_readPath[0]);
261 path.append("/dm.json");
262 if (!JsonWriter::ReadJson(path.c_str(), add_gold)) {
263 fail(SkStringPrintf("Couldn't read %s for golden results.", path.c_str()));
264 }
265 }
266}
267
268/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
269
mtklein935f1b12016-03-16 08:37:19 -0700270#if defined(SK_BUILD_FOR_WIN32)
271 static const char* kNewline = "\r\n";
272#else
273 static const char* kNewline = "\n";
274#endif
275
borenet09ed4802015-04-03 14:15:33 -0700276static SkTHashSet<SkString> gUninterestingHashes;
277
278static void gather_uninteresting_hashes() {
279 if (!FLAGS_uninterestingHashesFile.isEmpty()) {
280 SkAutoTUnref<SkData> data(SkData::NewFromFileName(FLAGS_uninterestingHashesFile[0]));
mtkleincc334b32015-09-22 11:43:53 -0700281 if (!data) {
mtkleinc41fd922016-03-08 09:01:39 -0800282 info("WARNING: unable to read uninteresting hashes from %s\n",
283 FLAGS_uninterestingHashesFile[0]);
mtkleincc334b32015-09-22 11:43:53 -0700284 return;
285 }
borenet09ed4802015-04-03 14:15:33 -0700286 SkTArray<SkString> hashes;
mtklein935f1b12016-03-16 08:37:19 -0700287 SkStrSplit((const char*)data->data(), kNewline, &hashes);
borenet09ed4802015-04-03 14:15:33 -0700288 for (const SkString& hash : hashes) {
289 gUninterestingHashes.add(hash);
290 }
mtkleinc41fd922016-03-08 09:01:39 -0800291 info("FYI: loaded %d distinct uninteresting hashes from %d lines\n",
292 gUninterestingHashes.count(), hashes.count());
borenet09ed4802015-04-03 14:15:33 -0700293 }
294}
295
296/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
297
mtkleine0effd62015-07-29 06:37:28 -0700298struct TaggedSrc : public SkAutoTDelete<Src> {
mtklein3eed7dd2016-02-24 19:07:07 -0800299 SkString tag;
300 SkString options;
mtkleine0effd62015-07-29 06:37:28 -0700301};
302
303struct TaggedSink : public SkAutoTDelete<Sink> {
kkinnunen3e980c32015-12-23 01:33:00 -0800304 SkString tag;
djsollen54416de2015-04-03 07:24:48 -0700305};
mtklein748ca3b2015-01-15 10:56:12 -0800306
307static const bool kMemcpyOK = true;
308
mtkleine0effd62015-07-29 06:37:28 -0700309static SkTArray<TaggedSrc, kMemcpyOK> gSrcs;
310static SkTArray<TaggedSink, kMemcpyOK> gSinks;
mtklein748ca3b2015-01-15 10:56:12 -0800311
mtklein6393c062015-04-27 08:45:01 -0700312static bool in_shard() {
313 static int N = 0;
314 return N++ % FLAGS_shards == FLAGS_shard;
315}
316
mtklein3eed7dd2016-02-24 19:07:07 -0800317static void push_src(const char* tag, ImplicitString options, Src* s) {
mtklein748ca3b2015-01-15 10:56:12 -0800318 SkAutoTDelete<Src> src(s);
mtklein6393c062015-04-27 08:45:01 -0700319 if (in_shard() &&
mtklein3eed7dd2016-02-24 19:07:07 -0800320 FLAGS_src.contains(tag) &&
mtklein748ca3b2015-01-15 10:56:12 -0800321 !SkCommandLineFlags::ShouldSkip(FLAGS_match, src->name().c_str())) {
mtkleine0effd62015-07-29 06:37:28 -0700322 TaggedSrc& s = gSrcs.push_back();
mtklein18300a32016-03-16 13:53:35 -0700323 s.reset(src.release());
mtklein748ca3b2015-01-15 10:56:12 -0800324 s.tag = tag;
djsollen54416de2015-04-03 07:24:48 -0700325 s.options = options;
mtklein114c3cd2015-01-15 10:15:02 -0800326 }
mtklein748ca3b2015-01-15 10:56:12 -0800327}
mtklein114c3cd2015-01-15 10:15:02 -0800328
msarett9e707a02015-09-01 14:57:57 -0700329static void push_codec_src(Path path, CodecSrc::Mode mode, CodecSrc::DstColorType dstColorType,
scroggoc5560be2016-02-03 09:42:42 -0800330 SkAlphaType dstAlphaType, float scale) {
scroggoe4499842016-02-25 11:03:47 -0800331 if (FLAGS_simpleCodec) {
332 if (mode != CodecSrc::kCodec_Mode || dstColorType != CodecSrc::kGetFromCanvas_DstColorType
333 || scale != 1.0f)
334 // Only decode in the simple case.
335 return;
336 }
msarett9e707a02015-09-01 14:57:57 -0700337 SkString folder;
338 switch (mode) {
339 case CodecSrc::kCodec_Mode:
340 folder.append("codec");
341 break;
msarettbb25b532016-01-13 09:31:39 -0800342 case CodecSrc::kCodecZeroInit_Mode:
343 folder.append("codec_zero_init");
344 break;
msarett9e707a02015-09-01 14:57:57 -0700345 case CodecSrc::kScanline_Mode:
346 folder.append("scanline");
347 break;
msarett9e707a02015-09-01 14:57:57 -0700348 case CodecSrc::kStripe_Mode:
349 folder.append("stripe");
350 break;
msarett91c22b22016-02-22 12:27:46 -0800351 case CodecSrc::kCroppedScanline_Mode:
352 folder.append("crop");
353 break;
msarett9e707a02015-09-01 14:57:57 -0700354 case CodecSrc::kSubset_Mode:
msarett36c37962015-09-02 13:20:52 -0700355 folder.append("codec_subset");
msarett9e707a02015-09-01 14:57:57 -0700356 break;
357 }
358
359 switch (dstColorType) {
360 case CodecSrc::kGrayscale_Always_DstColorType:
361 folder.append("_kGray8");
362 break;
363 case CodecSrc::kIndex8_Always_DstColorType:
364 folder.append("_kIndex8");
365 break;
366 default:
367 break;
368 }
369
scroggoc5560be2016-02-03 09:42:42 -0800370 switch (dstAlphaType) {
371 case kOpaque_SkAlphaType:
372 folder.append("_opaque");
373 break;
374 case kPremul_SkAlphaType:
375 folder.append("_premul");
376 break;
377 case kUnpremul_SkAlphaType:
378 folder.append("_unpremul");
379 break;
380 default:
381 break;
382 }
383
msarett9e707a02015-09-01 14:57:57 -0700384 if (1.0f != scale) {
385 folder.appendf("_%.3f", scale);
386 }
387
scroggoc5560be2016-02-03 09:42:42 -0800388 CodecSrc* src = new CodecSrc(path, mode, dstColorType, dstAlphaType, scale);
msarett9e707a02015-09-01 14:57:57 -0700389 push_src("image", folder, src);
390}
391
msarett3d9d7a72015-10-21 10:27:10 -0700392static void push_android_codec_src(Path path, AndroidCodecSrc::Mode mode,
scroggoc5560be2016-02-03 09:42:42 -0800393 CodecSrc::DstColorType dstColorType, SkAlphaType dstAlphaType, int sampleSize) {
msarett3d9d7a72015-10-21 10:27:10 -0700394 SkString folder;
395 switch (mode) {
396 case AndroidCodecSrc::kFullImage_Mode:
397 folder.append("scaled_codec");
398 break;
399 case AndroidCodecSrc::kDivisor_Mode:
400 folder.append("scaled_codec_divisor");
401 break;
402 }
403
404 switch (dstColorType) {
405 case CodecSrc::kGrayscale_Always_DstColorType:
406 folder.append("_kGray8");
407 break;
408 case CodecSrc::kIndex8_Always_DstColorType:
409 folder.append("_kIndex8");
410 break;
411 default:
412 break;
413 }
414
scroggoc5560be2016-02-03 09:42:42 -0800415 switch (dstAlphaType) {
416 case kOpaque_SkAlphaType:
417 folder.append("_opaque");
418 break;
419 case kPremul_SkAlphaType:
420 folder.append("_premul");
421 break;
msarett9e9444c2016-02-03 12:39:10 -0800422 case kUnpremul_SkAlphaType:
423 folder.append("_unpremul");
424 break;
scroggoc5560be2016-02-03 09:42:42 -0800425 default:
426 break;
427 }
428
msarett3d9d7a72015-10-21 10:27:10 -0700429 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800430 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700431 }
432
scroggoc5560be2016-02-03 09:42:42 -0800433 AndroidCodecSrc* src = new AndroidCodecSrc(path, mode, dstColorType, dstAlphaType, sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700434 push_src("image", folder, src);
435}
436
msarett18976312016-03-09 14:20:58 -0800437static void push_image_gen_src(Path path, ImageGenSrc::Mode mode, SkAlphaType alphaType, bool isGpu)
438{
439 SkString folder;
440 switch (mode) {
441 case ImageGenSrc::kCodec_Mode:
442 folder.append("gen_codec");
443 break;
444 case ImageGenSrc::kPlatform_Mode:
445 folder.append("gen_platform");
446 break;
447 }
448
449 if (isGpu) {
450 folder.append("_gpu");
451 } else {
452 switch (alphaType) {
453 case kOpaque_SkAlphaType:
454 folder.append("_opaque");
455 break;
456 case kPremul_SkAlphaType:
457 folder.append("_premul");
458 break;
459 case kUnpremul_SkAlphaType:
460 folder.append("_unpremul");
461 break;
462 default:
463 break;
464 }
465 }
466
467 ImageGenSrc* src = new ImageGenSrc(path, mode, alphaType, isGpu);
468 push_src("image", folder, src);
469}
470
msarett438b2ad2015-04-09 12:43:10 -0700471static void push_codec_srcs(Path path) {
472 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
473 if (!encoded) {
mtkleinc41fd922016-03-08 09:01:39 -0800474 info("Couldn't read %s.", path.c_str());
msarett438b2ad2015-04-09 12:43:10 -0700475 return;
476 }
477 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
halcanary96fcdcc2015-08-27 07:41:13 -0700478 if (nullptr == codec.get()) {
mtkleinc41fd922016-03-08 09:01:39 -0800479 info("Couldn't create codec for %s.", path.c_str());
msarett438b2ad2015-04-09 12:43:10 -0700480 return;
481 }
482
msarett36c37962015-09-02 13:20:52 -0700483 // Native Scales
emmaleer8f4ba762015-08-14 07:44:46 -0700484 // 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 -0700485 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 -0700486
msarett91c22b22016-02-22 12:27:46 -0800487 SkTArray<CodecSrc::Mode> nativeModes;
488 nativeModes.push_back(CodecSrc::kCodec_Mode);
489 nativeModes.push_back(CodecSrc::kCodecZeroInit_Mode);
msarett91c22b22016-02-22 12:27:46 -0800490 switch (codec->getEncodedFormat()) {
491 case SkEncodedFormat::kJPEG_SkEncodedFormat:
492 nativeModes.push_back(CodecSrc::kScanline_Mode);
493 nativeModes.push_back(CodecSrc::kStripe_Mode);
494 nativeModes.push_back(CodecSrc::kCroppedScanline_Mode);
495 break;
496 case SkEncodedFormat::kWEBP_SkEncodedFormat:
497 nativeModes.push_back(CodecSrc::kSubset_Mode);
498 break;
yujieqin7a307df2016-03-11 07:32:33 -0800499 case SkEncodedFormat::kDNG_SkEncodedFormat:
msarettb65e6042016-02-23 05:37:25 -0800500 break;
msarett91c22b22016-02-22 12:27:46 -0800501 default:
502 nativeModes.push_back(CodecSrc::kScanline_Mode);
503 nativeModes.push_back(CodecSrc::kStripe_Mode);
504 break;
505 }
msarett9e707a02015-09-01 14:57:57 -0700506
msarett91c22b22016-02-22 12:27:46 -0800507 SkTArray<CodecSrc::DstColorType> colorTypes;
508 colorTypes.push_back(CodecSrc::kGetFromCanvas_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700509 switch (codec->getInfo().colorType()) {
510 case kGray_8_SkColorType:
msarett91c22b22016-02-22 12:27:46 -0800511 colorTypes.push_back(CodecSrc::kGrayscale_Always_DstColorType);
msarett55f7bdd2016-02-16 13:24:54 -0800512 if (kWBMP_SkEncodedFormat == codec->getEncodedFormat()) {
msarett91c22b22016-02-22 12:27:46 -0800513 colorTypes.push_back(CodecSrc::kIndex8_Always_DstColorType);
msarett55f7bdd2016-02-16 13:24:54 -0800514 }
msarett36c37962015-09-02 13:20:52 -0700515 break;
516 case kIndex_8_SkColorType:
msarett91c22b22016-02-22 12:27:46 -0800517 colorTypes.push_back(CodecSrc::kIndex8_Always_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700518 break;
519 default:
msarett36c37962015-09-02 13:20:52 -0700520 break;
521 }
msarett9e707a02015-09-01 14:57:57 -0700522
scroggoc5560be2016-02-03 09:42:42 -0800523 SkTArray<SkAlphaType> alphaModes;
524 alphaModes.push_back(kPremul_SkAlphaType);
msarett9e9444c2016-02-03 12:39:10 -0800525 alphaModes.push_back(kUnpremul_SkAlphaType);
scroggoc5560be2016-02-03 09:42:42 -0800526 if (codec->getInfo().alphaType() == kOpaque_SkAlphaType) {
527 alphaModes.push_back(kOpaque_SkAlphaType);
528 }
msarettb714fb02016-01-22 14:46:42 -0800529
530 for (CodecSrc::Mode mode : nativeModes) {
msarettb714fb02016-01-22 14:46:42 -0800531 for (float scale : nativeScales) {
msarett91c22b22016-02-22 12:27:46 -0800532 for (CodecSrc::DstColorType colorType : colorTypes) {
scroggoc5560be2016-02-03 09:42:42 -0800533 for (SkAlphaType alphaType : alphaModes) {
msarett91c22b22016-02-22 12:27:46 -0800534 // Only test kCroppedScanline_Mode when the alpha type is opaque. The test is
535 // slow and won't be interestingly different with different alpha types.
536 if (CodecSrc::kCroppedScanline_Mode == mode &&
537 kOpaque_SkAlphaType != alphaType) {
538 continue;
539 }
540
541 push_codec_src(path, mode, colorType, alphaType, scale);
scroggoc5560be2016-02-03 09:42:42 -0800542 }
msarett9e707a02015-09-01 14:57:57 -0700543 }
544 }
msarett0a242972015-06-11 14:27:27 -0700545 }
msarett36c37962015-09-02 13:20:52 -0700546
scroggoe4499842016-02-25 11:03:47 -0800547 if (FLAGS_simpleCodec) {
548 return;
549 }
550
halcanary6950de62015-11-07 05:29:00 -0800551 // https://bug.skia.org/4428
msarett33c76232015-11-16 13:43:40 -0800552 bool subset = false;
553 // The following image types are supported by BitmapRegionDecoder,
554 // so we will test full image decodes and subset decodes.
msarettbe8216a2015-12-04 08:00:50 -0800555 static const char* const exts[] = {
msarett3d9d7a72015-10-21 10:27:10 -0700556 "jpg", "jpeg", "png", "webp",
557 "JPG", "JPEG", "PNG", "WEBP",
558 };
msarettbe8216a2015-12-04 08:00:50 -0800559 for (const char* ext : exts) {
msarett3d9d7a72015-10-21 10:27:10 -0700560 if (path.endsWith(ext)) {
msarett33c76232015-11-16 13:43:40 -0800561 subset = true;
msarett3d9d7a72015-10-21 10:27:10 -0700562 break;
563 }
564 }
msarett33c76232015-11-16 13:43:40 -0800565
msarett3d9d7a72015-10-21 10:27:10 -0700566 const int sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
msarett36c37962015-09-02 13:20:52 -0700567
msarett3d9d7a72015-10-21 10:27:10 -0700568 for (int sampleSize : sampleSizes) {
msarett91c22b22016-02-22 12:27:46 -0800569 for (CodecSrc::DstColorType colorType : colorTypes) {
scroggoc5560be2016-02-03 09:42:42 -0800570 for (SkAlphaType alphaType : alphaModes) {
msarett91c22b22016-02-22 12:27:46 -0800571 push_android_codec_src(path, AndroidCodecSrc::kFullImage_Mode, colorType,
scroggoc5560be2016-02-03 09:42:42 -0800572 alphaType, sampleSize);
573 if (subset) {
msarett91c22b22016-02-22 12:27:46 -0800574 push_android_codec_src(path, AndroidCodecSrc::kDivisor_Mode, colorType,
scroggoc5560be2016-02-03 09:42:42 -0800575 alphaType, sampleSize);
576 }
msarett3d9d7a72015-10-21 10:27:10 -0700577 }
msarett36c37962015-09-02 13:20:52 -0700578 }
579 }
msarett18976312016-03-09 14:20:58 -0800580
581 static const char* const rawExts[] = {
582 "arw", "cr2", "dng", "nef", "nrw", "orf", "raf", "rw2", "pef", "srw",
583 "ARW", "CR2", "DNG", "NEF", "NRW", "ORF", "RAF", "RW2", "PEF", "SRW",
584 };
585
586 // There is not currently a reason to test RAW images on image generator.
587 // If we want to enable these tests, we will need to fix skbug.com/5079.
588 for (const char* ext : rawExts) {
589 if (path.endsWith(ext)) {
590 return;
591 }
592 }
593
594 // Push image generator GPU test.
brianosmanc9ced392016-03-24 07:27:43 -0700595 push_image_gen_src(path, ImageGenSrc::kCodec_Mode, codec->getInfo().alphaType(), true);
msarett18976312016-03-09 14:20:58 -0800596
597 // Push image generator CPU tests.
598 for (SkAlphaType alphaType : alphaModes) {
599 push_image_gen_src(path, ImageGenSrc::kCodec_Mode, alphaType, false);
600
601#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
602 if (kWEBP_SkEncodedFormat != codec->getEncodedFormat() &&
603 kWBMP_SkEncodedFormat != codec->getEncodedFormat() &&
604 kUnpremul_SkAlphaType != alphaType)
605 {
606 push_image_gen_src(path, ImageGenSrc::kPlatform_Mode, alphaType, false);
607 }
msarettfc0b6d12016-03-17 13:50:17 -0700608#elif defined(SK_BUILD_FOR_WIN)
609 if (kWEBP_SkEncodedFormat != codec->getEncodedFormat() &&
610 kWBMP_SkEncodedFormat != codec->getEncodedFormat())
611 {
612 push_image_gen_src(path, ImageGenSrc::kPlatform_Mode, alphaType, false);
613 }
msarett18976312016-03-09 14:20:58 -0800614#endif
615 }
msarett438b2ad2015-04-09 12:43:10 -0700616}
617
msarett5cb48852015-11-06 08:56:32 -0800618static bool brd_color_type_supported(SkBitmapRegionDecoder::Strategy strategy,
msaretta5783ae2015-09-08 15:35:32 -0700619 CodecSrc::DstColorType dstColorType) {
620 switch (strategy) {
msarett5cb48852015-11-06 08:56:32 -0800621 case SkBitmapRegionDecoder::kCanvas_Strategy:
msaretta5783ae2015-09-08 15:35:32 -0700622 if (CodecSrc::kGetFromCanvas_DstColorType == dstColorType) {
623 return true;
624 }
625 return false;
msarett5cb48852015-11-06 08:56:32 -0800626 case SkBitmapRegionDecoder::kAndroidCodec_Strategy:
msarett26ad17b2015-10-22 07:29:19 -0700627 switch (dstColorType) {
628 case CodecSrc::kGetFromCanvas_DstColorType:
629 case CodecSrc::kIndex8_Always_DstColorType:
630 case CodecSrc::kGrayscale_Always_DstColorType:
631 return true;
632 default:
633 return false;
634 }
msaretta5783ae2015-09-08 15:35:32 -0700635 default:
636 SkASSERT(false);
637 return false;
638 }
639}
640
msarett5cb48852015-11-06 08:56:32 -0800641static void push_brd_src(Path path, SkBitmapRegionDecoder::Strategy strategy,
msaretta5783ae2015-09-08 15:35:32 -0700642 CodecSrc::DstColorType dstColorType, BRDSrc::Mode mode, uint32_t sampleSize) {
643 SkString folder;
644 switch (strategy) {
msarett5cb48852015-11-06 08:56:32 -0800645 case SkBitmapRegionDecoder::kCanvas_Strategy:
msaretta5783ae2015-09-08 15:35:32 -0700646 folder.append("brd_canvas");
647 break;
msarett5cb48852015-11-06 08:56:32 -0800648 case SkBitmapRegionDecoder::kAndroidCodec_Strategy:
msarett26ad17b2015-10-22 07:29:19 -0700649 folder.append("brd_android_codec");
650 break;
msaretta5783ae2015-09-08 15:35:32 -0700651 default:
652 SkASSERT(false);
653 return;
654 }
655
656 switch (mode) {
657 case BRDSrc::kFullImage_Mode:
658 break;
659 case BRDSrc::kDivisor_Mode:
660 folder.append("_divisor");
661 break;
662 default:
663 SkASSERT(false);
664 return;
665 }
666
667 switch (dstColorType) {
668 case CodecSrc::kGetFromCanvas_DstColorType:
669 break;
670 case CodecSrc::kIndex8_Always_DstColorType:
671 folder.append("_kIndex");
672 break;
673 case CodecSrc::kGrayscale_Always_DstColorType:
674 folder.append("_kGray");
675 break;
676 default:
677 SkASSERT(false);
678 return;
679 }
680
681 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800682 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700683 }
684
685 BRDSrc* src = new BRDSrc(path, strategy, mode, dstColorType, sampleSize);
686 push_src("image", folder, src);
687}
688
689static void push_brd_srcs(Path path) {
690
msarett5cb48852015-11-06 08:56:32 -0800691 const SkBitmapRegionDecoder::Strategy strategies[] = {
692 SkBitmapRegionDecoder::kCanvas_Strategy,
msarett5cb48852015-11-06 08:56:32 -0800693 SkBitmapRegionDecoder::kAndroidCodec_Strategy,
msaretta5783ae2015-09-08 15:35:32 -0700694 };
695
scroggo501b7342015-11-03 07:55:11 -0800696 // Test on a variety of sampleSizes, making sure to include:
697 // - 2, 4, and 8, which are natively supported by jpeg
698 // - multiples of 2 which are not divisible by 4 (analogous for 4)
699 // - larger powers of two, since BRD clients generally use powers of 2
700 // We will only produce output for the larger sizes on large images.
701 const uint32_t sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 24, 32, 64 };
msarett6efbe052015-09-11 09:01:16 -0700702
msaretta5783ae2015-09-08 15:35:32 -0700703 // We will only test to one backend (8888), but we will test all of the
704 // color types that we need to decode to on this backend.
705 const CodecSrc::DstColorType dstColorTypes[] = {
msarett26ad17b2015-10-22 07:29:19 -0700706 CodecSrc::kGetFromCanvas_DstColorType,
707 CodecSrc::kIndex8_Always_DstColorType,
708 CodecSrc::kGrayscale_Always_DstColorType,
msaretta5783ae2015-09-08 15:35:32 -0700709 };
710
711 const BRDSrc::Mode modes[] = {
msarett26ad17b2015-10-22 07:29:19 -0700712 BRDSrc::kFullImage_Mode,
713 BRDSrc::kDivisor_Mode,
msaretta5783ae2015-09-08 15:35:32 -0700714 };
715
msarett5cb48852015-11-06 08:56:32 -0800716 for (SkBitmapRegionDecoder::Strategy strategy : strategies) {
msarett6efbe052015-09-11 09:01:16 -0700717 for (uint32_t sampleSize : sampleSizes) {
msarett6efbe052015-09-11 09:01:16 -0700718 for (CodecSrc::DstColorType dstColorType : dstColorTypes) {
719 if (brd_color_type_supported(strategy, dstColorType)) {
720 for (BRDSrc::Mode mode : modes) {
msaretta5783ae2015-09-08 15:35:32 -0700721 push_brd_src(path, strategy, dstColorType, mode, sampleSize);
722 }
723 }
724 }
725 }
726 }
727}
728
729static bool brd_supported(const char* ext) {
msarett8c8f22a2015-04-01 06:58:48 -0700730 static const char* const exts[] = {
msaretta5783ae2015-09-08 15:35:32 -0700731 "jpg", "jpeg", "png", "webp",
732 "JPG", "JPEG", "PNG", "WEBP",
msarett8c8f22a2015-04-01 06:58:48 -0700733 };
734
735 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
736 if (0 == strcmp(exts[i], ext)) {
737 return true;
738 }
739 }
740 return false;
scroggo9b77ddd2015-03-19 06:03:39 -0700741}
742
scroggo86737142016-02-03 12:19:11 -0800743static bool gather_srcs() {
mtklein748ca3b2015-01-15 10:56:12 -0800744 for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) {
djsollen54416de2015-04-03 07:24:48 -0700745 push_src("gm", "", new GMSrc(r->factory()));
mtklein748ca3b2015-01-15 10:56:12 -0800746 }
halcanaryfc37ad12015-01-30 07:31:19 -0800747 for (int i = 0; i < FLAGS_skps.count(); i++) {
748 const char* path = FLAGS_skps[i];
749 if (sk_isdir(path)) {
750 SkOSFile::Iter it(path, "skp");
751 for (SkString file; it.next(&file); ) {
djsollen54416de2015-04-03 07:24:48 -0700752 push_src("skp", "", new SKPSrc(SkOSPath::Join(path, file.c_str())));
halcanaryfc37ad12015-01-30 07:31:19 -0800753 }
754 } else {
djsollen54416de2015-04-03 07:24:48 -0700755 push_src("skp", "", new SKPSrc(path));
mtklein114c3cd2015-01-15 10:15:02 -0800756 }
757 }
scroggo86737142016-02-03 12:19:11 -0800758
759 SkTArray<SkString> images;
760 if (!CollectImages(&images)) {
761 return false;
762 }
763
764 for (auto image : images) {
765 push_codec_srcs(image);
scroggoe4499842016-02-25 11:03:47 -0800766 if (FLAGS_simpleCodec) {
767 continue;
768 }
769
mtklein21eaf3b2016-02-08 12:39:59 -0800770 const char* ext = strrchr(image.c_str(), '.');
771 if (ext && brd_supported(ext+1)) {
scroggo86737142016-02-03 12:19:11 -0800772 push_brd_srcs(image);
mtklein709d2c32015-01-15 08:30:25 -0800773 }
mtklein709d2c32015-01-15 08:30:25 -0800774 }
scroggo86737142016-02-03 12:19:11 -0800775
776 return true;
mtklein709d2c32015-01-15 08:30:25 -0800777}
778
kkinnunen3e980c32015-12-23 01:33:00 -0800779static void push_sink(const SkCommandLineConfig& config, Sink* s) {
rmistry0f515bd2015-12-22 10:22:26 -0800780 SkAutoTDelete<Sink> sink(s);
kkinnunen3e980c32015-12-23 01:33:00 -0800781
mtkleine0effd62015-07-29 06:37:28 -0700782 // Try a simple Src as a canary. If it fails, skip this sink.
mtklein748ca3b2015-01-15 10:56:12 -0800783 struct : public Src {
mtkleine0effd62015-07-29 06:37:28 -0700784 Error draw(SkCanvas* c) const override {
785 c->drawRect(SkRect::MakeWH(1,1), SkPaint());
786 return "";
787 }
mtklein36352bf2015-03-25 18:17:31 -0700788 SkISize size() const override { return SkISize::Make(16, 16); }
mtkleine0effd62015-07-29 06:37:28 -0700789 Name name() const override { return "justOneRect"; }
790 } justOneRect;
mtklein114c3cd2015-01-15 10:15:02 -0800791
mtklein748ca3b2015-01-15 10:56:12 -0800792 SkBitmap bitmap;
793 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800794 SkString log;
mtkleine0effd62015-07-29 06:37:28 -0700795 Error err = sink->draw(justOneRect, &bitmap, &stream, &log);
mtklein4089ef72015-03-05 08:40:28 -0800796 if (err.isFatal()) {
mtkleinc41fd922016-03-08 09:01:39 -0800797 info("Could not run %s: %s\n", config.getTag().c_str(), err.c_str());
mtklein05641a52015-04-21 10:49:13 -0700798 exit(1);
mtklein114c3cd2015-01-15 10:15:02 -0800799 }
800
mtkleine0effd62015-07-29 06:37:28 -0700801 TaggedSink& ts = gSinks.push_back();
mtklein18300a32016-03-16 13:53:35 -0700802 ts.reset(sink.release());
kkinnunen3e980c32015-12-23 01:33:00 -0800803 ts.tag = config.getTag();
mtklein748ca3b2015-01-15 10:56:12 -0800804}
805
806static bool gpu_supported() {
807#if SK_SUPPORT_GPU
808 return FLAGS_gpu;
809#else
810 return false;
811#endif
812}
kkinnunen9ebc3f02015-12-21 23:48:13 -0800813
kkinnunen3e980c32015-12-23 01:33:00 -0800814static Sink* create_sink(const SkCommandLineConfig* config) {
815#if SK_SUPPORT_GPU
816 if (gpu_supported()) {
817 if (const SkCommandLineConfigGpu* gpuConfig = config->asConfigGpu()) {
bsalomon85b4b532016-04-05 11:06:27 -0700818 GrContextFactory::ContextType contextType = gpuConfig->getContextType();
819 GrContextFactory::ContextOptions contextOptions =
820 GrContextFactory::kNone_ContextOptions;
kkinnunen3e980c32015-12-23 01:33:00 -0800821 if (gpuConfig->getUseNVPR()) {
bsalomon85b4b532016-04-05 11:06:27 -0700822 contextOptions = static_cast<GrContextFactory::ContextOptions>(
823 contextOptions | GrContextFactory::kEnableNVPR_ContextOptions);
kkinnunen3e980c32015-12-23 01:33:00 -0800824 }
brianosman898235c2016-04-06 07:38:23 -0700825 if (SkColorAndProfileAreGammaCorrect(gpuConfig->getColorType(),
826 gpuConfig->getProfileType())) {
bsalomon85b4b532016-04-05 11:06:27 -0700827 contextOptions = static_cast<GrContextFactory::ContextOptions>(
828 contextOptions | GrContextFactory::kRequireSRGBSupport_ContextOptions);
brianosman61d3b082016-03-30 11:19:36 -0700829 }
kkinnunen3e980c32015-12-23 01:33:00 -0800830 GrContextFactory testFactory;
831 if (!testFactory.get(contextType, contextOptions)) {
mtkleinc41fd922016-03-08 09:01:39 -0800832 info("WARNING: can not create GPU context for config '%s'. "
833 "GM tests will be skipped.\n", gpuConfig->getTag().c_str());
kkinnunen3e980c32015-12-23 01:33:00 -0800834 return nullptr;
835 }
836 return new GPUSink(contextType, contextOptions, gpuConfig->getSamples(),
brianosmand93c1202016-03-10 07:49:08 -0800837 gpuConfig->getUseDIText(), gpuConfig->getColorType(),
838 gpuConfig->getProfileType(), FLAGS_gpu_threading);
kkinnunen3e980c32015-12-23 01:33:00 -0800839 }
840 }
841#endif
842
843#define SINK(t, sink, ...) if (config->getBackend().equals(t)) { return new sink(__VA_ARGS__); }
844
tomhudsoneebc39a2015-02-23 12:18:05 -0800845#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
846 SINK("hwui", HWUISink);
847#endif
848
mtklein748ca3b2015-01-15 10:56:12 -0800849 if (FLAGS_cpu) {
850 SINK("565", RasterSink, kRGB_565_SkColorType);
851 SINK("8888", RasterSink, kN32_SkColorType);
mtklein27c3fdd2016-02-26 14:43:21 -0800852 SINK("srgb", RasterSink, kN32_SkColorType, kSRGB_SkColorProfileType);
853 SINK("f16", RasterSink, kRGBA_F16_SkColorType);
halcanary94b87bd2016-04-04 06:12:15 -0700854 SINK("pdf", PDFSink);
mtklein9c3f17d2015-01-28 11:35:18 -0800855 SINK("skp", SKPSink);
mtklein8a4527e2015-01-31 20:00:58 -0800856 SINK("svg", SVGSink);
857 SINK("null", NullSink);
halcanary47ef4d52015-03-03 09:13:09 -0800858 SINK("xps", XPSSink);
mtklein748ca3b2015-01-15 10:56:12 -0800859 }
860#undef SINK
halcanary96fcdcc2015-08-27 07:41:13 -0700861 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800862}
863
kkinnunen3e980c32015-12-23 01:33:00 -0800864static Sink* create_via(const SkString& tag, Sink* wrapped) {
865#define VIA(t, via, ...) if (tag.equals(t)) { return new via(__VA_ARGS__); }
halcanary96fcdcc2015-08-27 07:41:13 -0700866 VIA("twice", ViaTwice, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700867 VIA("serialize", ViaSerialization, wrapped);
mtklein4a34ecb2016-01-08 10:19:35 -0800868 VIA("pic", ViaPicture, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700869 VIA("2ndpic", ViaSecondPicture, wrapped);
mtkleind31c13d2015-05-05 12:59:56 -0700870 VIA("sp", ViaSingletonPictures, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700871 VIA("tiles", ViaTiles, 256, 256, nullptr, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800872 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800873
mtkleind603b222015-02-17 11:13:33 -0800874 if (FLAGS_matrix.count() == 4) {
mtklein748ca3b2015-01-15 10:56:12 -0800875 SkMatrix m;
mtkleind603b222015-02-17 11:13:33 -0800876 m.reset();
877 m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
878 m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
879 m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
880 m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
881 VIA("matrix", ViaMatrix, m, wrapped);
882 VIA("upright", ViaUpright, m, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800883 }
tomhudson64de1e12015-03-05 08:01:07 -0800884
885#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
886 VIA("androidsdk", ViaAndroidSDK, wrapped);
887#endif
888
mtklein748ca3b2015-01-15 10:56:12 -0800889#undef VIA
halcanary96fcdcc2015-08-27 07:41:13 -0700890 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800891}
892
mtklein748ca3b2015-01-15 10:56:12 -0800893static void gather_sinks() {
kkinnunen3e980c32015-12-23 01:33:00 -0800894 SkCommandLineConfigArray configs;
895 ParseConfigs(FLAGS_config, &configs);
896 for (int i = 0; i < configs.count(); i++) {
897 const SkCommandLineConfig& config = *configs[i];
898 Sink* sink = create_sink(&config);
899 if (sink == nullptr) {
mtkleinc41fd922016-03-08 09:01:39 -0800900 info("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
901 config.getTag().c_str());
kkinnunen3e980c32015-12-23 01:33:00 -0800902 continue;
903 }
mtklein748ca3b2015-01-15 10:56:12 -0800904
kkinnunen3e980c32015-12-23 01:33:00 -0800905 const SkTArray<SkString>& parts = config.getViaParts();
906 for (int j = parts.count(); j-- > 0;) {
907 const SkString& part = parts[j];
908 Sink* next = create_via(part, sink);
halcanary96fcdcc2015-08-27 07:41:13 -0700909 if (next == nullptr) {
mtkleinc41fd922016-03-08 09:01:39 -0800910 info("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
911 part.c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800912 delete sink;
halcanary96fcdcc2015-08-27 07:41:13 -0700913 sink = nullptr;
mtklein748ca3b2015-01-15 10:56:12 -0800914 break;
915 }
916 sink = next;
917 }
918 if (sink) {
919 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800920 }
921 }
922}
mtklein709d2c32015-01-15 08:30:25 -0800923
mtkleina5114d72015-08-24 13:27:01 -0700924static bool dump_png(SkBitmap bitmap, const char* path, const char* md5) {
925 const int w = bitmap.width(),
926 h = bitmap.height();
mtklein27c3fdd2016-02-26 14:43:21 -0800927 // PNG wants unpremultiplied 8-bit RGBA pixels (16-bit could work fine too).
928 // We leave the gamma of these bytes unspecified, to continue the status quo,
929 // which we think generally is to interpret them as sRGB.
mtkleina5114d72015-08-24 13:27:01 -0700930
mtklein27c3fdd2016-02-26 14:43:21 -0800931 SkAutoTMalloc<uint32_t> rgba(w*h);
932
933 if (bitmap. colorType() == kN32_SkColorType &&
934 bitmap.profileType() == kSRGB_SkColorProfileType) {
935 // These are premul sRGB 8-bit pixels in SkPMColor order.
936 // We want unpremul sRGB 8-bit pixels in RGBA order. We'll get there via floats.
937 bitmap.lockPixels();
938 auto px = (const uint32_t*)bitmap.getPixels();
939 if (!px) {
mtkleina5114d72015-08-24 13:27:01 -0700940 return false;
941 }
mtklein27c3fdd2016-02-26 14:43:21 -0800942 for (int i = 0; i < w*h; i++) {
943 Sk4f fs = Sk4f_fromS32(px[i]); // Convert up to linear floats.
944 #if defined(SK_PMCOLOR_IS_BGRA)
945 fs = SkNx_shuffle<2,1,0,3>(fs); // Shuffle to RGBA, if not there already.
946 #endif
947 float invA = 1.0f / fs[3];
948 fs = fs * Sk4f(invA, invA, invA, 1); // Unpremultiply.
949 rgba[i] = Sk4f_toS32(fs); // Pack down to sRGB bytes.
950 }
mtkleina5114d72015-08-24 13:27:01 -0700951
mtklein27c3fdd2016-02-26 14:43:21 -0800952 } else if (bitmap.colorType() == kRGBA_F16_SkColorType) {
953 // These are premul linear half-float pixels in RGBA order.
954 // We want unpremul sRGB 8-bit pixels in RGBA order. We'll get there via floats.
955 bitmap.lockPixels();
956 auto px = (const uint64_t*)bitmap.getPixels();
957 if (!px) {
958 return false;
959 }
960 for (int i = 0; i < w*h; i++) {
961 Sk4f fs = SkHalfToFloat_01(px[i]); // Convert up to linear floats.
962 float invA = 1.0f / fs[3];
963 fs = fs * Sk4f(invA, invA, invA, 1); // Unpremultiply.
964 rgba[i] = Sk4f_toS32(fs); // Pack down to sRGB bytes.
965 }
966
967
968 } else {
969 // We "should" gamma correct in here but we don't.
970 // We want Gold to show exactly what our clients are seeing, broken gamma.
971
972 // Convert smaller formats up to premul linear 8-bit (in SkPMColor order).
973 if (bitmap.colorType() != kN32_SkColorType) {
974 SkBitmap n32;
975 if (!bitmap.copyTo(&n32, kN32_SkColorType)) {
976 return false;
977 }
978 bitmap = n32;
979 }
980
981 // Convert premul linear 8-bit to unpremul linear 8-bit RGBA.
982 if (!bitmap.readPixels(SkImageInfo::Make(w,h, kRGBA_8888_SkColorType,
983 kUnpremul_SkAlphaType),
984 rgba, 4*w, 0,0)) {
985 return false;
986 }
mtkleina5114d72015-08-24 13:27:01 -0700987 }
988
989 // We don't need bitmap anymore. Might as well drop our ref.
mtkleinfccb77d2015-08-24 14:13:29 -0700990 bitmap.reset();
mtkleina5114d72015-08-24 13:27:01 -0700991
mtkleinc64137c2015-08-25 10:56:08 -0700992 FILE* f = fopen(path, "wb");
mtkleina5114d72015-08-24 13:27:01 -0700993 if (!f) { return false; }
994
995 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
996 if (!png) {
997 fclose(f);
998 return false;
999 }
1000
1001 png_infop info = png_create_info_struct(png);
1002 if (!info) {
1003 png_destroy_write_struct(&png, &info);
1004 fclose(f);
1005 return false;
1006 }
1007
mtkleind69ece62015-09-10 10:37:44 -07001008 SkString description;
1009 description.append("Key: ");
1010 for (int i = 0; i < FLAGS_key.count(); i++) {
1011 description.appendf("%s ", FLAGS_key[i]);
1012 }
1013 description.append("Properties: ");
1014 for (int i = 0; i < FLAGS_properties.count(); i++) {
1015 description.appendf("%s ", FLAGS_properties[i]);
1016 }
1017 description.appendf("MD5: %s", md5);
1018
mtkleina5114d72015-08-24 13:27:01 -07001019 png_text text[2];
1020 text[0].key = (png_charp)"Author";
1021 text[0].text = (png_charp)"DM dump_png()";
1022 text[0].compression = PNG_TEXT_COMPRESSION_NONE;
1023 text[1].key = (png_charp)"Description";
mtkleind69ece62015-09-10 10:37:44 -07001024 text[1].text = (png_charp)description.c_str();
mtkleina5114d72015-08-24 13:27:01 -07001025 text[1].compression = PNG_TEXT_COMPRESSION_NONE;
1026 png_set_text(png, info, text, 2);
1027
1028 png_init_io(png, f);
1029 png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8,
1030 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
1031 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
1032 png_write_info(png, info);
1033 for (int j = 0; j < h; j++) {
1034 png_bytep row = (png_bytep)(rgba.get() + w*j);
1035 png_write_rows(png, &row, 1);
1036 }
1037 png_write_end(png, info);
1038
1039 png_destroy_write_struct(&png, &info);
1040 fclose(f);
1041 return true;
1042}
1043
mtkleina2ef6422015-01-15 13:44:22 -08001044static bool match(const char* needle, const char* haystack) {
halcanary96fcdcc2015-08-27 07:41:13 -07001045 return 0 == strcmp("_", needle) || nullptr != strstr(haystack, needle);
mtkleina2ef6422015-01-15 13:44:22 -08001046}
1047
mtklein3eed7dd2016-02-24 19:07:07 -08001048static bool is_blacklisted(const char* sink, const char* src,
1049 const char* srcOptions, const char* name) {
mtklein0d243ff2015-04-03 07:51:00 -07001050 for (int i = 0; i < FLAGS_blacklist.count() - 3; i += 4) {
mtkleina2ef6422015-01-15 13:44:22 -08001051 if (match(FLAGS_blacklist[i+0], sink) &&
djsollen54416de2015-04-03 07:24:48 -07001052 match(FLAGS_blacklist[i+1], src) &&
1053 match(FLAGS_blacklist[i+2], srcOptions) &&
1054 match(FLAGS_blacklist[i+3], name)) {
mtklein3eed7dd2016-02-24 19:07:07 -08001055 return true;
mtkleina2ef6422015-01-15 13:44:22 -08001056 }
1057 }
mtklein3eed7dd2016-02-24 19:07:07 -08001058 return false;
mtkleina2ef6422015-01-15 13:44:22 -08001059}
1060
mtkleincd50bca2016-01-05 06:20:20 -08001061// Even when a Task Sink reports to be non-threadsafe (e.g. GPU), we know things like
1062// .png encoding are definitely thread safe. This lets us offload that work to CPU threads.
1063static SkTaskGroup gDefinitelyThreadSafeWork;
1064
mtklein748ca3b2015-01-15 10:56:12 -08001065// The finest-grained unit of work we can run: draw a single Src into a single Sink,
1066// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
1067struct Task {
mtkleine0effd62015-07-29 06:37:28 -07001068 Task(const TaggedSrc& src, const TaggedSink& sink) : src(src), sink(sink) {}
1069 const TaggedSrc& src;
1070 const TaggedSink& sink;
mtklein748ca3b2015-01-15 10:56:12 -08001071
mtklein21eaf3b2016-02-08 12:39:59 -08001072 static void Run(const Task& task) {
1073 SkString name = task.src->name();
mtkleine0effd62015-07-29 06:37:28 -07001074
mtkleinb9eb4ac2015-02-02 18:26:03 -08001075 SkString log;
mtklein3eed7dd2016-02-24 19:07:07 -08001076 if (!FLAGS_dryRun) {
mtklein748ca3b2015-01-15 10:56:12 -08001077 SkBitmap bitmap;
1078 SkDynamicMemoryWStream stream;
mtklein3eed7dd2016-02-24 19:07:07 -08001079 start(task.sink.tag.c_str(), task.src.tag.c_str(),
1080 task.src.options.c_str(), name.c_str());
mtklein21eaf3b2016-02-08 12:39:59 -08001081 Error err = task.sink->draw(*task.src, &bitmap, &stream, &log);
mtkleinb3b13b72016-03-07 13:20:52 -08001082 if (!log.isEmpty()) {
mtkleinc41fd922016-03-08 09:01:39 -08001083 info("%s %s %s %s:\n%s\n", task.sink.tag.c_str()
1084 , task.src.tag.c_str()
1085 , task.src.options.c_str()
1086 , name.c_str()
1087 , log.c_str());
mtkleinb3b13b72016-03-07 13:20:52 -08001088 }
mtklein748ca3b2015-01-15 10:56:12 -08001089 if (!err.isEmpty()) {
mtklein4089ef72015-03-05 08:40:28 -08001090 if (err.isFatal()) {
djsollen54416de2015-04-03 07:24:48 -07001091 fail(SkStringPrintf("%s %s %s %s: %s",
mtklein21eaf3b2016-02-08 12:39:59 -08001092 task.sink.tag.c_str(),
1093 task.src.tag.c_str(),
1094 task.src.options.c_str(),
mtklein4089ef72015-03-05 08:40:28 -08001095 name.c_str(),
1096 err.c_str()));
mtkleinb37cb412015-03-18 05:27:14 -07001097 } else {
mtklein3eed7dd2016-02-24 19:07:07 -08001098 done(task.sink.tag.c_str(), task.src.tag.c_str(),
1099 task.src.options.c_str(), name.c_str());
mtkleinba6ada72016-01-21 09:39:35 -08001100 return;
mtklein4089ef72015-03-05 08:40:28 -08001101 }
mtklein748ca3b2015-01-15 10:56:12 -08001102 }
mtklein62bd1a62015-01-27 14:46:26 -08001103
mtkleincd50bca2016-01-05 06:20:20 -08001104 // We're likely switching threads here, so we must capture by value, [=] or [foo,bar].
1105 SkStreamAsset* data = stream.detachAsStream();
1106 gDefinitelyThreadSafeWork.add([task,name,bitmap,data]{
1107 SkAutoTDelete<SkStreamAsset> ownedData(data);
1108
1109 // Why doesn't the copy constructor do this when we have pre-locked pixels?
1110 bitmap.lockPixels();
1111
1112 SkString md5;
1113 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
1114 SkMD5 hash;
1115 if (data->getLength()) {
1116 hash.writeStream(data, data->getLength());
1117 data->rewind();
mtklein38408462015-07-08 07:25:27 -07001118 } else {
mtkleincd50bca2016-01-05 06:20:20 -08001119 // If we're BGRA (Linux, Windows), swizzle over to RGBA (Mac, Android).
1120 // This helps eliminate multiple 0-pixel-diff hashes on gold.skia.org.
1121 // (Android's general slow speed breaks the tie arbitrarily in RGBA's favor.)
1122 // We might consider promoting 565 to RGBA too.
1123 if (bitmap.colorType() == kBGRA_8888_SkColorType) {
1124 SkBitmap swizzle;
1125 SkAssertResult(bitmap.copyTo(&swizzle, kRGBA_8888_SkColorType));
1126 hash.write(swizzle.getPixels(), swizzle.getSize());
1127 } else {
1128 hash.write(bitmap.getPixels(), bitmap.getSize());
1129 }
1130 }
1131 SkMD5::Digest digest;
1132 hash.finish(digest);
1133 for (int i = 0; i < 16; i++) {
1134 md5.appendf("%02x", digest.data[i]);
mtklein38408462015-07-08 07:25:27 -07001135 }
mtklein62bd1a62015-01-27 14:46:26 -08001136 }
mtklein62bd1a62015-01-27 14:46:26 -08001137
mtkleincd50bca2016-01-05 06:20:20 -08001138 if (!FLAGS_readPath.isEmpty() &&
mtklein3eed7dd2016-02-24 19:07:07 -08001139 !gGold.contains(Gold(task.sink.tag, task.src.tag,
1140 task.src.options, name, md5))) {
mtkleincd50bca2016-01-05 06:20:20 -08001141 fail(SkStringPrintf("%s not found for %s %s %s %s in %s",
1142 md5.c_str(),
mtklein21eaf3b2016-02-08 12:39:59 -08001143 task.sink.tag.c_str(),
1144 task.src.tag.c_str(),
1145 task.src.options.c_str(),
mtkleincd50bca2016-01-05 06:20:20 -08001146 name.c_str(),
1147 FLAGS_readPath[0]));
mtklein748ca3b2015-01-15 10:56:12 -08001148 }
mtkleincd50bca2016-01-05 06:20:20 -08001149
1150 if (!FLAGS_writePath.isEmpty()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001151 const char* ext = task.sink->fileExtension();
mtkleincd50bca2016-01-05 06:20:20 -08001152 if (data->getLength()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001153 WriteToDisk(task, md5, ext, data, data->getLength(), nullptr);
mtkleincd50bca2016-01-05 06:20:20 -08001154 SkASSERT(bitmap.drawsNothing());
1155 } else if (!bitmap.drawsNothing()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001156 WriteToDisk(task, md5, ext, nullptr, 0, &bitmap);
mtkleincd50bca2016-01-05 06:20:20 -08001157 }
1158 }
1159 });
mtklein748ca3b2015-01-15 10:56:12 -08001160 }
mtklein3eed7dd2016-02-24 19:07:07 -08001161 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 -08001162 }
1163
1164 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -08001165 SkString md5,
1166 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -08001167 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -08001168 const SkBitmap* bitmap) {
mtklein409d4702016-02-29 07:38:01 -08001169 bool gammaCorrect = false;
1170 if (bitmap) {
brianosman898235c2016-04-06 07:38:23 -07001171 gammaCorrect = SkImageInfoIsGammaCorrect(bitmap->info());
mtklein409d4702016-02-29 07:38:01 -08001172 }
1173
mtklein748ca3b2015-01-15 10:56:12 -08001174 JsonWriter::BitmapResult result;
djsollen54416de2015-04-03 07:24:48 -07001175 result.name = task.src->name();
mtklein3eed7dd2016-02-24 19:07:07 -08001176 result.config = task.sink.tag;
djsollen54416de2015-04-03 07:24:48 -07001177 result.sourceType = task.src.tag;
1178 result.sourceOptions = task.src.options;
1179 result.ext = ext;
mtklein409d4702016-02-29 07:38:01 -08001180 result.gammaCorrect = gammaCorrect;
djsollen54416de2015-04-03 07:24:48 -07001181 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -08001182 JsonWriter::AddBitmapResult(result);
1183
mtkleinb0531a72015-04-07 13:38:48 -07001184 // If an MD5 is uninteresting, we want it noted in the JSON file,
1185 // but don't want to dump it out as a .png (or whatever ext is).
1186 if (gUninterestingHashes.contains(md5)) {
1187 return;
1188 }
1189
mtklein748ca3b2015-01-15 10:56:12 -08001190 const char* dir = FLAGS_writePath[0];
1191 if (0 == strcmp(dir, "@")) { // Needed for iOS.
1192 dir = FLAGS_resourcePath[0];
1193 }
1194 sk_mkdir(dir);
1195
1196 SkString path;
1197 if (FLAGS_nameByHash) {
1198 path = SkOSPath::Join(dir, result.md5.c_str());
1199 path.append(".");
1200 path.append(ext);
1201 if (sk_exists(path.c_str())) {
1202 return; // Content-addressed. If it exists already, we're done.
1203 }
1204 } else {
kkinnunen3e980c32015-12-23 01:33:00 -08001205 path = SkOSPath::Join(dir, task.sink.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001206 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001207 path = SkOSPath::Join(path.c_str(), task.src.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001208 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001209 if (strcmp(task.src.options.c_str(), "") != 0) {
1210 path = SkOSPath::Join(path.c_str(), task.src.options.c_str());
djsollen54416de2015-04-03 07:24:48 -07001211 sk_mkdir(path.c_str());
1212 }
mtklein748ca3b2015-01-15 10:56:12 -08001213 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
1214 path.append(".");
1215 path.append(ext);
1216 }
1217
mtklein748ca3b2015-01-15 10:56:12 -08001218 if (bitmap) {
mtkleina5114d72015-08-24 13:27:01 -07001219 if (!dump_png(*bitmap, path.c_str(), result.md5.c_str())) {
mtklein748ca3b2015-01-15 10:56:12 -08001220 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
1221 return;
1222 }
1223 } else {
mtkleina5114d72015-08-24 13:27:01 -07001224 SkFILEWStream file(path.c_str());
1225 if (!file.isValid()) {
1226 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
1227 return;
1228 }
mtklein748ca3b2015-01-15 10:56:12 -08001229 if (!file.writeStream(data, len)) {
1230 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
1231 return;
1232 }
1233 }
1234 }
1235};
1236
mtklein748ca3b2015-01-15 10:56:12 -08001237/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1238
1239// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
1240
mtklein21eaf3b2016-02-08 12:39:59 -08001241static SkTDArray<skiatest::Test> gParallelTests, gSerialTests;
mtklein748ca3b2015-01-15 10:56:12 -08001242
1243static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -08001244 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -08001245 return;
1246 }
mtklein6393c062015-04-27 08:45:01 -07001247 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r; r = r->next()) {
1248 if (!in_shard()) {
1249 continue;
1250 }
halcanary87f3ba42015-01-20 09:30:20 -08001251 // Despite its name, factory() is returning a reference to
1252 // link-time static const POD data.
1253 const skiatest::Test& test = r->factory();
1254 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -08001255 continue;
1256 }
halcanary87f3ba42015-01-20 09:30:20 -08001257 if (test.needsGpu && gpu_supported()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001258 (FLAGS_gpu_threading ? gParallelTests : gSerialTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -08001259 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein21eaf3b2016-02-08 12:39:59 -08001260 gParallelTests.push(test);
mtklein82d28432015-01-15 12:46:02 -08001261 }
mtklein748ca3b2015-01-15 10:56:12 -08001262 }
1263}
1264
mtklein21eaf3b2016-02-08 12:39:59 -08001265static void run_test(skiatest::Test test) {
halcanary87f3ba42015-01-20 09:30:20 -08001266 struct : public skiatest::Reporter {
mtklein36352bf2015-03-25 18:17:31 -07001267 void reportFailed(const skiatest::Failure& failure) override {
halcanary87f3ba42015-01-20 09:30:20 -08001268 fail(failure.toString());
1269 JsonWriter::AddTestFailure(failure);
1270 }
mtklein36352bf2015-03-25 18:17:31 -07001271 bool allowExtendedTest() const override {
halcanary87f3ba42015-01-20 09:30:20 -08001272 return FLAGS_pathOpsExtended;
1273 }
mtklein36352bf2015-03-25 18:17:31 -07001274 bool verbose() const override { return FLAGS_veryVerbose; }
halcanary87f3ba42015-01-20 09:30:20 -08001275 } reporter;
djsollen824996a2015-06-12 12:06:22 -07001276
mtklein3eed7dd2016-02-24 19:07:07 -08001277 if (!FLAGS_dryRun && !is_blacklisted("_", "tests", "_", test.name)) {
mtklein21eaf3b2016-02-08 12:39:59 -08001278 start("unit", "test", "", test.name);
mtklein55e88b22015-01-21 15:50:13 -08001279 GrContextFactory factory;
mtklein21eaf3b2016-02-08 12:39:59 -08001280 test.proc(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -08001281 }
mtklein3eed7dd2016-02-24 19:07:07 -08001282 done("unit", "test", "", test.name);
mtklein748ca3b2015-01-15 10:56:12 -08001283}
1284
1285/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1286
mtkleinafae30a2016-02-24 12:28:32 -08001287DEFINE_int32(status_sec, 15, "Print status this often (and if we crash).");
1288
1289SkThread* start_status_thread() {
1290 auto thread = new SkThread([] (void*) {
1291 for (;;) {
1292 print_status();
1293 #if defined(SK_BUILD_FOR_WIN)
1294 Sleep(FLAGS_status_sec * 1000);
1295 #else
1296 sleep(FLAGS_status_sec);
1297 #endif
mtklein2e1c47e2015-03-12 07:16:56 -07001298 }
mtkleinafae30a2016-02-24 12:28:32 -08001299 });
1300 thread->start();
1301 return thread;
mtkleinde6fc2b2015-03-12 06:28:54 -07001302}
1303
caryclark83ca6282015-06-10 09:31:09 -07001304#define PORTABLE_FONT_PREFIX "Toy Liberation "
1305
1306static SkTypeface* create_from_name(const char familyName[], SkTypeface::Style style) {
1307 if (familyName && strlen(familyName) > sizeof(PORTABLE_FONT_PREFIX)
1308 && !strncmp(familyName, PORTABLE_FONT_PREFIX, sizeof(PORTABLE_FONT_PREFIX) - 1)) {
caryclark1818acb2015-07-24 12:09:25 -07001309 return sk_tool_utils::create_portable_typeface(familyName, style);
caryclark83ca6282015-06-10 09:31:09 -07001310 }
halcanary96fcdcc2015-08-27 07:41:13 -07001311 return nullptr;
caryclark83ca6282015-06-10 09:31:09 -07001312}
1313
1314#undef PORTABLE_FONT_PREFIX
1315
1316extern SkTypeface* (*gCreateTypefaceDelegate)(const char [], SkTypeface::Style );
1317
jcgregorio3b27ade2014-11-13 08:06:40 -08001318int dm_main();
caryclark17f0b6d2014-07-22 10:15:34 -07001319int dm_main() {
mtklein246ba3a2016-02-23 10:39:36 -08001320 setup_crash_handler();
mtkleinafae30a2016-02-24 12:28:32 -08001321
mtkleina483da32016-03-17 12:53:36 -07001322 if (FLAGS_verbose) {
1323 gVLog = stderr;
1324 } else if (!FLAGS_writePath.isEmpty()) {
mtklein51c8cfc2016-03-11 12:59:09 -08001325 sk_mkdir(FLAGS_writePath[0]);
1326 gVLog = freopen(SkOSPath::Join(FLAGS_writePath[0], "verbose.log").c_str(), "w", stderr);
mtkleinc41fd922016-03-08 09:01:39 -08001327 }
1328
mtklein5286f022016-01-22 08:18:14 -08001329 JsonWriter::DumpJson(); // It's handy for the bots to assume this is ~never missing.
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +00001330 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -07001331 SkTaskGroup::Enabler enabled(FLAGS_threads);
caryclark83ca6282015-06-10 09:31:09 -07001332 gCreateTypefaceDelegate = &create_from_name;
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +00001333
halcanary7d571242016-02-24 17:59:16 -08001334 {
1335 SkString testResourcePath = GetResourcePath("color_wheel.png");
1336 SkFILEStream testResource(testResourcePath.c_str());
1337 if (!testResource.isValid()) {
mtkleinc41fd922016-03-08 09:01:39 -08001338 info("Some resources are missing. Do you need to set --resourcePath?\n");
halcanary7d571242016-02-24 17:59:16 -08001339 }
1340 }
mtklein62bd1a62015-01-27 14:46:26 -08001341 gather_gold();
borenet09ed4802015-04-03 14:15:33 -07001342 gather_uninteresting_hashes();
mtklein62bd1a62015-01-27 14:46:26 -08001343
scroggo86737142016-02-03 12:19:11 -08001344 if (!gather_srcs()) {
1345 return 1;
1346 }
mtklein748ca3b2015-01-15 10:56:12 -08001347 gather_sinks();
1348 gather_tests();
mtklein21eaf3b2016-02-08 12:39:59 -08001349 gPending = gSrcs.count() * gSinks.count() + gParallelTests.count() + gSerialTests.count();
mtkleinc41fd922016-03-08 09:01:39 -08001350 info("%d srcs * %d sinks + %d tests == %d tasks",
1351 gSrcs.count(), gSinks.count(), gParallelTests.count() + gSerialTests.count(), gPending);
mtkleinafae30a2016-02-24 12:28:32 -08001352 SkAutoTDelete<SkThread> statusThread(start_status_thread());
mtklein748ca3b2015-01-15 10:56:12 -08001353
mtklein21eaf3b2016-02-08 12:39:59 -08001354 // Kick off as much parallel work as we can, making note of any serial work we'll need to do.
1355 SkTaskGroup parallel;
1356 SkTArray<Task> serial;
1357
1358 for (auto& sink : gSinks)
1359 for (auto& src : gSrcs) {
mtklein3eed7dd2016-02-24 19:07:07 -08001360 if (src->veto(sink->flags()) ||
1361 is_blacklisted(sink.tag.c_str(), src.tag.c_str(),
1362 src.options.c_str(), src->name().c_str())) {
mtklein15923c92016-02-29 10:14:38 -08001363 SkAutoTAcquire<SkSpinlock> lock(gMutex);
mtklein3eed7dd2016-02-24 19:07:07 -08001364 gPending--;
1365 continue;
1366 }
1367
mtklein21eaf3b2016-02-08 12:39:59 -08001368 Task task(src, sink);
1369 if (src->serial() || sink->serial()) {
1370 serial.push_back(task);
1371 } else {
1372 parallel.add([task] { Task::Run(task); });
mtklein748ca3b2015-01-15 10:56:12 -08001373 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001374 }
mtklein21eaf3b2016-02-08 12:39:59 -08001375 for (auto test : gParallelTests) {
1376 parallel.add([test] { run_test(test); });
mtklein55e88b22015-01-21 15:50:13 -08001377 }
mtklein21eaf3b2016-02-08 12:39:59 -08001378
1379 // With the parallel work running, run serial tasks and tests here on main thread.
1380 for (auto task : serial) { Task::Run(task); }
1381 for (auto test : gSerialTests) { run_test(test); }
1382
1383 // Wait for any remaining parallel work to complete (including any spun off of serial tasks).
1384 parallel.wait();
mtkleincd50bca2016-01-05 06:20:20 -08001385 gDefinitelyThreadSafeWork.wait();
1386
mtkleinda884c42016-02-24 18:00:23 -08001387 // We'd better have run everything.
1388 SkASSERT(gPending == 0);
mtkleine027f172016-02-26 15:53:06 -08001389 // Make sure we've flushed all our results to disk.
1390 JsonWriter::DumpJson();
mtkleinda884c42016-02-24 18:00:23 -08001391
mtklein748ca3b2015-01-15 10:56:12 -08001392 // At this point we're back in single-threaded land.
caryclarkf53ce802015-06-15 06:48:30 -07001393 sk_tool_utils::release_portable_typefaces();
mtklein@google.comd36522d2013-10-16 13:02:15 +00001394
mtklein748ca3b2015-01-15 10:56:12 -08001395 if (gFailures.count() > 0) {
mtkleinc41fd922016-03-08 09:01:39 -08001396 info("Failures:\n");
mtklein748ca3b2015-01-15 10:56:12 -08001397 for (int i = 0; i < gFailures.count(); i++) {
mtkleinc41fd922016-03-08 09:01:39 -08001398 info("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001399 }
mtkleinc41fd922016-03-08 09:01:39 -08001400 info("%d failures\n", gFailures.count());
mtklein748ca3b2015-01-15 10:56:12 -08001401 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001402 }
mtkleinafae30a2016-02-24 12:28:32 -08001403
1404#ifdef SK_PDF_IMAGE_STATS
halcanary7a14b312015-10-01 07:28:13 -07001405 SkPDFImageDumpStats();
mtkleinafae30a2016-02-24 12:28:32 -08001406#endif // SK_PDF_IMAGE_STATS
1407
1408 print_status();
mtkleinc41fd922016-03-08 09:01:39 -08001409 info("Finished!\n");
mtklein748ca3b2015-01-15 10:56:12 -08001410 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +00001411}
jcgregorio3b27ade2014-11-13 08:06:40 -08001412
kkinnunen179a8f52015-11-20 13:32:24 -08001413// TODO: currently many GPU tests are declared outside SK_SUPPORT_GPU guards.
1414// Thus we export the empty RunWithGPUTestContexts when SK_SUPPORT_GPU=0.
1415namespace skiatest {
bsalomon758586c2016-04-06 14:02:39 -07001416
1417#if SK_SUPPORT_GPU
1418bool IsGLContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1419 return kOpenGL_GrBackend == GrContextFactory::ContextTypeBackend(type);
1420}
bsalomondc0fcd42016-04-11 14:21:33 -07001421bool IsVulkanContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1422 return kVulkan_GrBackend == GrContextFactory::ContextTypeBackend(type);
1423}
bsalomon758586c2016-04-06 14:02:39 -07001424bool IsRenderingGLContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1425 return IsGLContextType(type) && GrContextFactory::IsRenderingContext(type);
1426}
1427bool IsNullGLContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1428 return type == GrContextFactory::kNullGL_ContextType;
1429}
1430#else
1431bool IsGLContextType(int) { return false; }
bsalomondc0fcd42016-04-11 14:21:33 -07001432bool IsVulkanContextType(int) { return false; }
bsalomon758586c2016-04-06 14:02:39 -07001433bool IsRenderingGLContextType(int) { return false; }
1434bool IsNullGLContextType(int) { return false; }
1435#endif
1436
1437void RunWithGPUTestContexts(GrContextTestFn* test, GrContextTypeFilterFn* contextTypeFilter,
bsalomonf2f1c172016-04-05 12:59:06 -07001438 Reporter* reporter, GrContextFactory* factory) {
kkinnunen179a8f52015-11-20 13:32:24 -08001439#if SK_SUPPORT_GPU
kkinnunen3e980c32015-12-23 01:33:00 -08001440
bsalomon758586c2016-04-06 14:02:39 -07001441 for (int typeInt = 0; typeInt < GrContextFactory::kContextTypeCnt; ++typeInt) {
1442 GrContextFactory::ContextType contextType = (GrContextFactory::ContextType) typeInt;
1443 ContextInfo ctxInfo = factory->getContextInfo(contextType);
bsalomonfda88072016-04-11 14:40:50 -07001444 if (contextTypeFilter && !(*contextTypeFilter)(contextType)) {
kkinnunen179a8f52015-11-20 13:32:24 -08001445 continue;
1446 }
bsalomon758586c2016-04-06 14:02:39 -07001447 // Use "native" instead of explicitly trying OpenGL and OpenGL ES. Do not use GLES on,
1448 // desktop since tests do not account for not fixing http://skbug.com/2809
1449 if (contextType == GrContextFactory::kGL_ContextType ||
1450 contextType == GrContextFactory::kGLES_ContextType) {
1451 if (contextType != GrContextFactory::kNativeGL_ContextType) {
1452 continue;
1453 }
1454 }
bsalomonf2f1c172016-04-05 12:59:06 -07001455 if (ctxInfo.fGrContext) {
1456 (*test)(reporter, ctxInfo);
kkinnunen5219fd92015-12-10 06:28:13 -08001457 }
bsalomonf2f1c172016-04-05 12:59:06 -07001458 ctxInfo = factory->getContextInfo(contextType,
bsalomon85b4b532016-04-05 11:06:27 -07001459 GrContextFactory::kEnableNVPR_ContextOptions);
bsalomonf2f1c172016-04-05 12:59:06 -07001460 if (ctxInfo.fGrContext) {
1461 (*test)(reporter, ctxInfo);
kkinnunen179a8f52015-11-20 13:32:24 -08001462 }
1463 }
1464#endif
1465}
kkinnunen179a8f52015-11-20 13:32:24 -08001466} // namespace skiatest
1467
borenet48087572015-04-02 12:16:36 -07001468#if !defined(SK_BUILD_FOR_IOS)
jcgregorio3b27ade2014-11-13 08:06:40 -08001469int main(int argc, char** argv) {
1470 SkCommandLineFlags::Parse(argc, argv);
1471 return dm_main();
1472}
1473#endif