blob: b8aed5f166705fac670b6ef6590f163560147eee [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"
brianosman3c579dc2016-04-19 09:18:11 -070020#include "SkData.h"
caryclark83ca6282015-06-10 09:31:09 -070021#include "SkFontMgr.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +000022#include "SkGraphics.h"
mtklein27c3fdd2016-02-26 14:43:21 -080023#include "SkHalf.h"
mtklein748ca3b2015-01-15 10:56:12 -080024#include "SkMD5.h"
mtklein1b249332015-07-07 12:21:21 -070025#include "SkMutex.h"
mtklein1d0f1642014-09-08 08:05:18 -070026#include "SkOSFile.h"
mtklein27c3fdd2016-02-26 14:43:21 -080027#include "SkPM4fPriv.h"
mtklein246ba3a2016-02-23 10:39:36 -080028#include "SkSpinlock.h"
mtkleina82f5622015-02-20 12:30:19 -080029#include "SkTHash.h"
mtklein406654b2014-09-03 15:34:37 -070030#include "SkTaskGroup.h"
mtkleinde6fc2b2015-03-12 06:28:54 -070031#include "SkThreadUtils.h"
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +000032#include "Test.h"
mtklein748ca3b2015-01-15 10:56:12 -080033#include "Timer.h"
brianosman3c579dc2016-04-19 09:18:11 -070034#include "picture_utils.h"
caryclark83ca6282015-06-10 09:31:09 -070035#include "sk_tool_utils.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +000036
halcanary7a14b312015-10-01 07:28:13 -070037#ifdef SK_PDF_IMAGE_STATS
38extern void SkPDFImageDumpStats();
39#endif
40
mtkleina5114d72015-08-24 13:27:01 -070041#include "png.h"
42
bungeman60e0fee2015-08-26 05:15:46 -070043#include <stdlib.h>
44
scroggo27a58342015-10-28 08:56:41 -070045#ifndef SK_BUILD_FOR_WIN32
46 #include <unistd.h>
47#endif
48
djsollen54416de2015-04-03 07:24:48 -070049DEFINE_string(src, "tests gm skp image", "Source types to test.");
mtklein748ca3b2015-01-15 10:56:12 -080050DEFINE_bool(nameByHash, false,
51 "If true, write to FLAGS_writePath[0]/<hash>.png instead of "
djsollen54416de2015-04-03 07:24:48 -070052 "to FLAGS_writePath[0]/<config>/<sourceType>/<sourceOptions>/<name>.png");
mtklein748ca3b2015-01-15 10:56:12 -080053DEFINE_bool2(pathOpsExtended, x, false, "Run extended pathOps tests.");
mtkleind603b222015-02-17 11:13:33 -080054DEFINE_string(matrix, "1 0 0 1",
55 "2x2 scale+skew matrix to apply or upright when using "
56 "'matrix' or 'upright' in config.");
mtklein82d28432015-01-15 12:46:02 -080057DEFINE_bool(gpu_threading, false, "Allow GPU work to run on multiple threads?");
mtklein@google.comd36522d2013-10-16 13:02:15 +000058
mtkleina2ef6422015-01-15 13:44:22 -080059DEFINE_string(blacklist, "",
djsollen54416de2015-04-03 07:24:48 -070060 "Space-separated config/src/srcOptions/name quadruples to blacklist. '_' matches anything. E.g. \n"
61 "'--blacklist gpu skp _ _' will blacklist all SKPs drawn into the gpu config.\n"
62 "'--blacklist gpu skp _ _ 8888 gm _ aarects' will also blacklist the aarects GM on 8888.");
mtkleina2ef6422015-01-15 13:44:22 -080063
mtklein62bd1a62015-01-27 14:46:26 -080064DEFINE_string2(readPath, r, "", "If set check for equality with golden results in this directory.");
65
borenet09ed4802015-04-03 14:15:33 -070066DEFINE_string(uninterestingHashesFile, "",
67 "File containing a list of uninteresting hashes. If a result hashes to something in "
68 "this list, no image is written for that result.");
69
mtklein6393c062015-04-27 08:45:01 -070070DEFINE_int32(shards, 1, "We're splitting source data into this many shards.");
71DEFINE_int32(shard, 0, "Which shard do I run?");
scroggoe4499842016-02-25 11:03:47 -080072DEFINE_bool(simpleCodec, false, "Only decode images to native scale");
mtklein6393c062015-04-27 08:45:01 -070073
mtklein748ca3b2015-01-15 10:56:12 -080074using namespace DM;
bsalomon3724e572016-03-30 18:56:19 -070075using sk_gpu_test::GrContextFactory;
bsalomon273c0f52016-03-31 10:59:06 -070076using sk_gpu_test::GLTestContext;
bsalomonf2f1c172016-04-05 12:59:06 -070077using sk_gpu_test::ContextInfo;
mtklein@google.comd36522d2013-10-16 13:02:15 +000078
mtklein748ca3b2015-01-15 10:56:12 -080079/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
80
benjaminwagnerec4d4d72016-03-25 12:59:53 -070081static const double kStartMs = SkTime::GetMSecs();
mtkleinc41fd922016-03-08 09:01:39 -080082
83static FILE* gVLog;
84
85template <typename... Args>
86static void vlog(const char* fmt, Args&&... args) {
87 if (gVLog) {
88 fprintf(gVLog, "%s\t", HumanizeMs(SkTime::GetMSecs() - kStartMs).c_str());
89 fprintf(gVLog, fmt, args...);
90 fflush(gVLog);
91 }
92}
93
94template <typename... Args>
95static void info(const char* fmt, Args&&... args) {
96 vlog(fmt, args...);
97 if (!FLAGS_quiet) {
98 printf(fmt, args...);
99 }
100}
101static void info(const char* fmt) {
102 if (!FLAGS_quiet) {
103 printf("%s", fmt); // Clang warns printf(fmt) is insecure.
104 }
105}
106
reed086eea92016-05-04 17:12:46 -0700107SK_DECLARE_STATIC_MUTEX(gFailuresMutex);
mtklein748ca3b2015-01-15 10:56:12 -0800108static SkTArray<SkString> gFailures;
109
mtklein3eed7dd2016-02-24 19:07:07 -0800110static void fail(const SkString& err) {
mtklein748ca3b2015-01-15 10:56:12 -0800111 SkAutoMutexAcquire lock(gFailuresMutex);
112 SkDebugf("\n\nFAILURE: %s\n\n", err.c_str());
113 gFailures.push_back(err);
halcanary9e398f72015-01-10 11:18:04 -0800114}
115
mtkleinb37cb412015-03-18 05:27:14 -0700116
mtklein246ba3a2016-02-23 10:39:36 -0800117// We use a spinlock to make locking this in a signal handler _somewhat_ safe.
mtklein15923c92016-02-29 10:14:38 -0800118static SkSpinlock gMutex;
mtklein3eed7dd2016-02-24 19:07:07 -0800119static int32_t gPending;
120static SkTArray<SkString> gRunning;
mtklein748ca3b2015-01-15 10:56:12 -0800121
mtklein3eed7dd2016-02-24 19:07:07 -0800122static void done(const char* config, const char* src, const char* srcOptions, const char* name) {
123 SkString id = SkStringPrintf("%s %s %s %s", config, src, srcOptions, name);
mtkleinc41fd922016-03-08 09:01:39 -0800124 vlog("done %s\n", id.c_str());
mtkleinafae30a2016-02-24 12:28:32 -0800125 int pending;
mtkleinb37cb412015-03-18 05:27:14 -0700126 {
mtklein15923c92016-02-29 10:14:38 -0800127 SkAutoTAcquire<SkSpinlock> lock(gMutex);
mtkleinb37cb412015-03-18 05:27:14 -0700128 for (int i = 0; i < gRunning.count(); i++) {
129 if (gRunning[i] == id) {
130 gRunning.removeShuffle(i);
131 break;
132 }
133 }
mtkleinafae30a2016-02-24 12:28:32 -0800134 pending = --gPending;
reed50bc0512015-05-19 14:13:31 -0700135 }
mtkleina17241b2015-01-23 05:48:00 -0800136 // We write our dm.json file every once in a while in case we crash.
137 // Notice this also handles the final dm.json when pending == 0.
138 if (pending % 500 == 0) {
139 JsonWriter::DumpJson();
140 }
mtklein@google.comd36522d2013-10-16 13:02:15 +0000141}
142
mtklein3eed7dd2016-02-24 19:07:07 -0800143static void start(const char* config, const char* src, const char* srcOptions, const char* name) {
144 SkString id = SkStringPrintf("%s %s %s %s", config, src, srcOptions, name);
mtkleinc41fd922016-03-08 09:01:39 -0800145 vlog("start %s\n", id.c_str());
mtklein15923c92016-02-29 10:14:38 -0800146 SkAutoTAcquire<SkSpinlock> lock(gMutex);
mtkleinb37cb412015-03-18 05:27:14 -0700147 gRunning.push_back(id);
148}
149
mtkleinafae30a2016-02-24 12:28:32 -0800150static void print_status() {
mtkleinafae30a2016-02-24 12:28:32 -0800151 int curr = sk_tools::getCurrResidentSetSizeMB(),
152 peak = sk_tools::getMaxResidentSetSizeMB();
mtkleinc41fd922016-03-08 09:01:39 -0800153 SkString elapsed = HumanizeMs(SkTime::GetMSecs() - kStartMs);
mtkleinafae30a2016-02-24 12:28:32 -0800154
mtklein15923c92016-02-29 10:14:38 -0800155 SkAutoTAcquire<SkSpinlock> lock(gMutex);
mtkleinc41fd922016-03-08 09:01:39 -0800156 info("\n%s elapsed, %d active, %d queued, %dMB RAM, %dMB peak\n",
157 elapsed.c_str(), gRunning.count(), gPending - gRunning.count(), curr, peak);
mtkleinafae30a2016-02-24 12:28:32 -0800158 for (auto& task : gRunning) {
mtkleinc41fd922016-03-08 09:01:39 -0800159 info("\t%s\n", task.c_str());
mtkleinafae30a2016-02-24 12:28:32 -0800160 }
161}
162
mtkleinf8557292016-02-29 06:35:28 -0800163// Yo dawg, I heard you like signals so I caught a signal in your
164// signal handler so you can handle signals while you handle signals.
165// Let's not get into that situation. Only print if we're the first ones to get a crash signal.
166static std::atomic<bool> in_signal_handler{false};
167
mtklein246ba3a2016-02-23 10:39:36 -0800168#if defined(SK_BUILD_FOR_WIN32)
mtkleinf8557292016-02-29 06:35:28 -0800169 static LONG WINAPI handler(EXCEPTION_POINTERS* e) {
170 static const struct {
171 const char* name;
robertphillips75467862016-03-01 14:10:23 -0800172 DWORD code;
mtkleinf8557292016-02-29 06:35:28 -0800173 } kExceptions[] = {
174 #define _(E) {#E, E}
175 _(EXCEPTION_ACCESS_VIOLATION),
176 _(EXCEPTION_BREAKPOINT),
177 _(EXCEPTION_INT_DIVIDE_BY_ZERO),
178 _(EXCEPTION_STACK_OVERFLOW),
179 // TODO: more?
180 #undef _
181 };
182
183 if (!in_signal_handler.exchange(true)) {
184 const DWORD code = e->ExceptionRecord->ExceptionCode;
mtkleinc41fd922016-03-08 09:01:39 -0800185 info("\nCaught exception %u", code);
mtkleinf8557292016-02-29 06:35:28 -0800186 for (const auto& exception : kExceptions) {
187 if (exception.code == code) {
mtkleinc41fd922016-03-08 09:01:39 -0800188 info(" %s", exception.name);
mtkleinf8557292016-02-29 06:35:28 -0800189 }
190 }
mtkleinc41fd922016-03-08 09:01:39 -0800191 info("\n");
mtkleinf8557292016-02-29 06:35:28 -0800192 print_status();
mtklein13bf3392016-03-17 10:16:12 -0700193 fflush(stdout);
mtkleinf8557292016-02-29 06:35:28 -0800194 }
195 // Execute default exception handler... hopefully, exit.
196 return EXCEPTION_EXECUTE_HANDLER;
mtklein246ba3a2016-02-23 10:39:36 -0800197 }
mtkleinf8557292016-02-29 06:35:28 -0800198 static void setup_crash_handler() { SetUnhandledExceptionFilter(handler); }
mtklein246ba3a2016-02-23 10:39:36 -0800199
mtklein819ab102016-02-29 17:02:52 -0800200#elif !defined(SK_BUILD_FOR_ANDROID)
201 #include <execinfo.h>
mtklein246ba3a2016-02-23 10:39:36 -0800202 #include <signal.h>
mtklein819ab102016-02-29 17:02:52 -0800203 #include <stdlib.h>
mtkleinf8557292016-02-29 06:35:28 -0800204
mtklein246ba3a2016-02-23 10:39:36 -0800205 static void setup_crash_handler() {
206 const int kSignals[] = { SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGSEGV };
207 for (int sig : kSignals) {
208 signal(sig, [](int sig) {
mtkleinf8557292016-02-29 06:35:28 -0800209 if (!in_signal_handler.exchange(true)) {
mtklein819ab102016-02-29 17:02:52 -0800210 SkAutoTAcquire<SkSpinlock> lock(gMutex);
mtkleinc41fd922016-03-08 09:01:39 -0800211 info("\nCaught signal %d [%s], was running:\n", sig, strsignal(sig));
mtklein819ab102016-02-29 17:02:52 -0800212 for (auto& task : gRunning) {
mtkleinc41fd922016-03-08 09:01:39 -0800213 info("\t%s\n", task.c_str());
mtklein819ab102016-02-29 17:02:52 -0800214 }
215
216 void* stack[64];
217 int count = backtrace(stack, SK_ARRAY_COUNT(stack));
218 char** symbols = backtrace_symbols(stack, count);
mtkleinc41fd922016-03-08 09:01:39 -0800219 info("\nStack trace:\n");
mtklein819ab102016-02-29 17:02:52 -0800220 for (int i = 0; i < count; i++) {
mtkleinc41fd922016-03-08 09:01:39 -0800221 info(" %s\n", symbols[i]);
mtklein819ab102016-02-29 17:02:52 -0800222 }
mtklein13bf3392016-03-17 10:16:12 -0700223 fflush(stdout);
mtkleinf8557292016-02-29 06:35:28 -0800224 }
mtklein819ab102016-02-29 17:02:52 -0800225 _Exit(sig);
mtklein246ba3a2016-02-23 10:39:36 -0800226 });
227 }
228 }
mtklein819ab102016-02-29 17:02:52 -0800229
230#else // Android
231 static void setup_crash_handler() {}
mtklein246ba3a2016-02-23 10:39:36 -0800232#endif
233
mtklein748ca3b2015-01-15 10:56:12 -0800234/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
mtklein114c3cd2015-01-15 10:15:02 -0800235
mtklein62bd1a62015-01-27 14:46:26 -0800236struct Gold : public SkString {
mtkleina82f5622015-02-20 12:30:19 -0800237 Gold() : SkString("") {}
mtklein3eed7dd2016-02-24 19:07:07 -0800238 Gold(const SkString& sink, const SkString& src,
239 const SkString& srcOptions, const SkString& name,
240 const SkString& md5)
mtklein62bd1a62015-01-27 14:46:26 -0800241 : SkString("") {
242 this->append(sink);
243 this->append(src);
djsollen54416de2015-04-03 07:24:48 -0700244 this->append(srcOptions);
mtklein62bd1a62015-01-27 14:46:26 -0800245 this->append(name);
246 this->append(md5);
mtklein62bd1a62015-01-27 14:46:26 -0800247 }
mtkleinc8d1dd42015-10-15 12:23:01 -0700248 struct Hash {
249 uint32_t operator()(const Gold& g) const {
250 return SkGoodHash()((const SkString&)g);
251 }
252 };
mtklein62bd1a62015-01-27 14:46:26 -0800253};
mtkleina82f5622015-02-20 12:30:19 -0800254static SkTHashSet<Gold, Gold::Hash> gGold;
mtklein62bd1a62015-01-27 14:46:26 -0800255
256static void add_gold(JsonWriter::BitmapResult r) {
djsollen54416de2015-04-03 07:24:48 -0700257 gGold.add(Gold(r.config, r.sourceType, r.sourceOptions, r.name, r.md5));
mtklein62bd1a62015-01-27 14:46:26 -0800258}
259
260static void gather_gold() {
261 if (!FLAGS_readPath.isEmpty()) {
262 SkString path(FLAGS_readPath[0]);
263 path.append("/dm.json");
264 if (!JsonWriter::ReadJson(path.c_str(), add_gold)) {
265 fail(SkStringPrintf("Couldn't read %s for golden results.", path.c_str()));
266 }
267 }
268}
269
270/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
271
mtklein935f1b12016-03-16 08:37:19 -0700272#if defined(SK_BUILD_FOR_WIN32)
273 static const char* kNewline = "\r\n";
274#else
275 static const char* kNewline = "\n";
276#endif
277
borenet09ed4802015-04-03 14:15:33 -0700278static SkTHashSet<SkString> gUninterestingHashes;
279
280static void gather_uninteresting_hashes() {
281 if (!FLAGS_uninterestingHashesFile.isEmpty()) {
282 SkAutoTUnref<SkData> data(SkData::NewFromFileName(FLAGS_uninterestingHashesFile[0]));
mtkleincc334b32015-09-22 11:43:53 -0700283 if (!data) {
mtkleinc41fd922016-03-08 09:01:39 -0800284 info("WARNING: unable to read uninteresting hashes from %s\n",
285 FLAGS_uninterestingHashesFile[0]);
mtkleincc334b32015-09-22 11:43:53 -0700286 return;
287 }
borenet09ed4802015-04-03 14:15:33 -0700288 SkTArray<SkString> hashes;
mtklein935f1b12016-03-16 08:37:19 -0700289 SkStrSplit((const char*)data->data(), kNewline, &hashes);
borenet09ed4802015-04-03 14:15:33 -0700290 for (const SkString& hash : hashes) {
291 gUninterestingHashes.add(hash);
292 }
mtkleinc41fd922016-03-08 09:01:39 -0800293 info("FYI: loaded %d distinct uninteresting hashes from %d lines\n",
294 gUninterestingHashes.count(), hashes.count());
borenet09ed4802015-04-03 14:15:33 -0700295 }
296}
297
298/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
299
mtkleine0effd62015-07-29 06:37:28 -0700300struct TaggedSrc : public SkAutoTDelete<Src> {
mtklein3eed7dd2016-02-24 19:07:07 -0800301 SkString tag;
302 SkString options;
mtkleine0effd62015-07-29 06:37:28 -0700303};
304
305struct TaggedSink : public SkAutoTDelete<Sink> {
kkinnunen3e980c32015-12-23 01:33:00 -0800306 SkString tag;
djsollen54416de2015-04-03 07:24:48 -0700307};
mtklein748ca3b2015-01-15 10:56:12 -0800308
309static const bool kMemcpyOK = true;
310
mtkleine0effd62015-07-29 06:37:28 -0700311static SkTArray<TaggedSrc, kMemcpyOK> gSrcs;
312static SkTArray<TaggedSink, kMemcpyOK> gSinks;
mtklein748ca3b2015-01-15 10:56:12 -0800313
mtklein6393c062015-04-27 08:45:01 -0700314static bool in_shard() {
315 static int N = 0;
316 return N++ % FLAGS_shards == FLAGS_shard;
317}
318
mtklein3eed7dd2016-02-24 19:07:07 -0800319static void push_src(const char* tag, ImplicitString options, Src* s) {
mtklein748ca3b2015-01-15 10:56:12 -0800320 SkAutoTDelete<Src> src(s);
mtklein6393c062015-04-27 08:45:01 -0700321 if (in_shard() &&
mtklein3eed7dd2016-02-24 19:07:07 -0800322 FLAGS_src.contains(tag) &&
mtklein748ca3b2015-01-15 10:56:12 -0800323 !SkCommandLineFlags::ShouldSkip(FLAGS_match, src->name().c_str())) {
mtkleine0effd62015-07-29 06:37:28 -0700324 TaggedSrc& s = gSrcs.push_back();
mtklein18300a32016-03-16 13:53:35 -0700325 s.reset(src.release());
mtklein748ca3b2015-01-15 10:56:12 -0800326 s.tag = tag;
djsollen54416de2015-04-03 07:24:48 -0700327 s.options = options;
mtklein114c3cd2015-01-15 10:15:02 -0800328 }
mtklein748ca3b2015-01-15 10:56:12 -0800329}
mtklein114c3cd2015-01-15 10:15:02 -0800330
msarett9e707a02015-09-01 14:57:57 -0700331static void push_codec_src(Path path, CodecSrc::Mode mode, CodecSrc::DstColorType dstColorType,
scroggoc5560be2016-02-03 09:42:42 -0800332 SkAlphaType dstAlphaType, float scale) {
scroggoe4499842016-02-25 11:03:47 -0800333 if (FLAGS_simpleCodec) {
334 if (mode != CodecSrc::kCodec_Mode || dstColorType != CodecSrc::kGetFromCanvas_DstColorType
335 || scale != 1.0f)
336 // Only decode in the simple case.
337 return;
338 }
msarett9e707a02015-09-01 14:57:57 -0700339 SkString folder;
340 switch (mode) {
341 case CodecSrc::kCodec_Mode:
342 folder.append("codec");
343 break;
msarettbb25b532016-01-13 09:31:39 -0800344 case CodecSrc::kCodecZeroInit_Mode:
345 folder.append("codec_zero_init");
346 break;
msarett9e707a02015-09-01 14:57:57 -0700347 case CodecSrc::kScanline_Mode:
348 folder.append("scanline");
349 break;
msarett9e707a02015-09-01 14:57:57 -0700350 case CodecSrc::kStripe_Mode:
351 folder.append("stripe");
352 break;
msarett91c22b22016-02-22 12:27:46 -0800353 case CodecSrc::kCroppedScanline_Mode:
354 folder.append("crop");
355 break;
msarett9e707a02015-09-01 14:57:57 -0700356 case CodecSrc::kSubset_Mode:
msarett36c37962015-09-02 13:20:52 -0700357 folder.append("codec_subset");
msarett9e707a02015-09-01 14:57:57 -0700358 break;
359 }
360
361 switch (dstColorType) {
362 case CodecSrc::kGrayscale_Always_DstColorType:
363 folder.append("_kGray8");
364 break;
365 case CodecSrc::kIndex8_Always_DstColorType:
366 folder.append("_kIndex8");
367 break;
msarett34e0ec42016-04-22 16:27:24 -0700368 case CodecSrc::kNonNative8888_Always_DstColorType:
369 folder.append("_kNonNative");
370 break;
msarett9e707a02015-09-01 14:57:57 -0700371 default:
372 break;
373 }
374
scroggoc5560be2016-02-03 09:42:42 -0800375 switch (dstAlphaType) {
376 case kOpaque_SkAlphaType:
377 folder.append("_opaque");
378 break;
379 case kPremul_SkAlphaType:
380 folder.append("_premul");
381 break;
382 case kUnpremul_SkAlphaType:
383 folder.append("_unpremul");
384 break;
385 default:
386 break;
387 }
388
msarett9e707a02015-09-01 14:57:57 -0700389 if (1.0f != scale) {
390 folder.appendf("_%.3f", scale);
391 }
392
scroggoc5560be2016-02-03 09:42:42 -0800393 CodecSrc* src = new CodecSrc(path, mode, dstColorType, dstAlphaType, scale);
msarett9e707a02015-09-01 14:57:57 -0700394 push_src("image", folder, src);
395}
396
scroggof8dc9df2016-05-16 09:04:13 -0700397static void push_android_codec_src(Path path, CodecSrc::DstColorType dstColorType,
398 SkAlphaType dstAlphaType, int sampleSize) {
msarett3d9d7a72015-10-21 10:27:10 -0700399 SkString folder;
scroggof8dc9df2016-05-16 09:04:13 -0700400 folder.append("scaled_codec");
msarett3d9d7a72015-10-21 10:27:10 -0700401
402 switch (dstColorType) {
403 case CodecSrc::kGrayscale_Always_DstColorType:
404 folder.append("_kGray8");
405 break;
406 case CodecSrc::kIndex8_Always_DstColorType:
407 folder.append("_kIndex8");
408 break;
msarett34e0ec42016-04-22 16:27:24 -0700409 case CodecSrc::kNonNative8888_Always_DstColorType:
410 folder.append("_kNonNative");
411 break;
msarett3d9d7a72015-10-21 10:27:10 -0700412 default:
413 break;
414 }
415
scroggoc5560be2016-02-03 09:42:42 -0800416 switch (dstAlphaType) {
417 case kOpaque_SkAlphaType:
418 folder.append("_opaque");
419 break;
420 case kPremul_SkAlphaType:
421 folder.append("_premul");
422 break;
msarett9e9444c2016-02-03 12:39:10 -0800423 case kUnpremul_SkAlphaType:
424 folder.append("_unpremul");
425 break;
scroggoc5560be2016-02-03 09:42:42 -0800426 default:
427 break;
428 }
429
msarett3d9d7a72015-10-21 10:27:10 -0700430 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800431 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700432 }
433
scroggof8dc9df2016-05-16 09:04:13 -0700434 AndroidCodecSrc* src = new AndroidCodecSrc(path, dstColorType, dstAlphaType, sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700435 push_src("image", folder, src);
436}
437
msarett18976312016-03-09 14:20:58 -0800438static void push_image_gen_src(Path path, ImageGenSrc::Mode mode, SkAlphaType alphaType, bool isGpu)
439{
440 SkString folder;
441 switch (mode) {
442 case ImageGenSrc::kCodec_Mode:
443 folder.append("gen_codec");
444 break;
445 case ImageGenSrc::kPlatform_Mode:
446 folder.append("gen_platform");
447 break;
448 }
449
450 if (isGpu) {
451 folder.append("_gpu");
452 } else {
453 switch (alphaType) {
454 case kOpaque_SkAlphaType:
455 folder.append("_opaque");
456 break;
457 case kPremul_SkAlphaType:
458 folder.append("_premul");
459 break;
460 case kUnpremul_SkAlphaType:
461 folder.append("_unpremul");
462 break;
463 default:
464 break;
465 }
466 }
467
468 ImageGenSrc* src = new ImageGenSrc(path, mode, alphaType, isGpu);
469 push_src("image", folder, src);
470}
471
msarett438b2ad2015-04-09 12:43:10 -0700472static void push_codec_srcs(Path path) {
473 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
474 if (!encoded) {
mtkleinc41fd922016-03-08 09:01:39 -0800475 info("Couldn't read %s.", path.c_str());
msarett438b2ad2015-04-09 12:43:10 -0700476 return;
477 }
478 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
halcanary96fcdcc2015-08-27 07:41:13 -0700479 if (nullptr == codec.get()) {
mtkleinc41fd922016-03-08 09:01:39 -0800480 info("Couldn't create codec for %s.", path.c_str());
msarett438b2ad2015-04-09 12:43:10 -0700481 return;
482 }
483
msarett36c37962015-09-02 13:20:52 -0700484 // Native Scales
emmaleer8f4ba762015-08-14 07:44:46 -0700485 // 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 -0700486 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 -0700487
msarett91c22b22016-02-22 12:27:46 -0800488 SkTArray<CodecSrc::Mode> nativeModes;
489 nativeModes.push_back(CodecSrc::kCodec_Mode);
490 nativeModes.push_back(CodecSrc::kCodecZeroInit_Mode);
msarett91c22b22016-02-22 12:27:46 -0800491 switch (codec->getEncodedFormat()) {
492 case SkEncodedFormat::kJPEG_SkEncodedFormat:
493 nativeModes.push_back(CodecSrc::kScanline_Mode);
494 nativeModes.push_back(CodecSrc::kStripe_Mode);
495 nativeModes.push_back(CodecSrc::kCroppedScanline_Mode);
496 break;
497 case SkEncodedFormat::kWEBP_SkEncodedFormat:
498 nativeModes.push_back(CodecSrc::kSubset_Mode);
499 break;
yujieqin7a307df2016-03-11 07:32:33 -0800500 case SkEncodedFormat::kDNG_SkEncodedFormat:
msarettb65e6042016-02-23 05:37:25 -0800501 break;
msarett91c22b22016-02-22 12:27:46 -0800502 default:
503 nativeModes.push_back(CodecSrc::kScanline_Mode);
504 nativeModes.push_back(CodecSrc::kStripe_Mode);
505 break;
506 }
msarett9e707a02015-09-01 14:57:57 -0700507
msarett91c22b22016-02-22 12:27:46 -0800508 SkTArray<CodecSrc::DstColorType> colorTypes;
509 colorTypes.push_back(CodecSrc::kGetFromCanvas_DstColorType);
msarett34e0ec42016-04-22 16:27:24 -0700510 colorTypes.push_back(CodecSrc::kNonNative8888_Always_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700511 switch (codec->getInfo().colorType()) {
512 case kGray_8_SkColorType:
msarett91c22b22016-02-22 12:27:46 -0800513 colorTypes.push_back(CodecSrc::kGrayscale_Always_DstColorType);
msarett55f7bdd2016-02-16 13:24:54 -0800514 if (kWBMP_SkEncodedFormat == codec->getEncodedFormat()) {
msarett91c22b22016-02-22 12:27:46 -0800515 colorTypes.push_back(CodecSrc::kIndex8_Always_DstColorType);
msarett55f7bdd2016-02-16 13:24:54 -0800516 }
msarett36c37962015-09-02 13:20:52 -0700517 break;
518 case kIndex_8_SkColorType:
msarett91c22b22016-02-22 12:27:46 -0800519 colorTypes.push_back(CodecSrc::kIndex8_Always_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700520 break;
521 default:
msarett36c37962015-09-02 13:20:52 -0700522 break;
523 }
msarett9e707a02015-09-01 14:57:57 -0700524
scroggoc5560be2016-02-03 09:42:42 -0800525 SkTArray<SkAlphaType> alphaModes;
526 alphaModes.push_back(kPremul_SkAlphaType);
msarett9e9444c2016-02-03 12:39:10 -0800527 alphaModes.push_back(kUnpremul_SkAlphaType);
scroggoc5560be2016-02-03 09:42:42 -0800528 if (codec->getInfo().alphaType() == kOpaque_SkAlphaType) {
529 alphaModes.push_back(kOpaque_SkAlphaType);
530 }
msarettb714fb02016-01-22 14:46:42 -0800531
532 for (CodecSrc::Mode mode : nativeModes) {
msarettb714fb02016-01-22 14:46:42 -0800533 for (float scale : nativeScales) {
msarett91c22b22016-02-22 12:27:46 -0800534 for (CodecSrc::DstColorType colorType : colorTypes) {
scroggoc5560be2016-02-03 09:42:42 -0800535 for (SkAlphaType alphaType : alphaModes) {
msarett91c22b22016-02-22 12:27:46 -0800536 // Only test kCroppedScanline_Mode when the alpha type is opaque. The test is
537 // slow and won't be interestingly different with different alpha types.
538 if (CodecSrc::kCroppedScanline_Mode == mode &&
539 kOpaque_SkAlphaType != alphaType) {
540 continue;
541 }
542
msarettf3dc1882016-04-26 13:06:38 -0700543 // Skip kNonNative on different native scales. It won't be interestingly
544 // different.
545 if (CodecSrc::kNonNative8888_Always_DstColorType == colorType && 1.0f != scale)
546 {
547 continue;
548 }
549
msarett91c22b22016-02-22 12:27:46 -0800550 push_codec_src(path, mode, colorType, alphaType, scale);
scroggoc5560be2016-02-03 09:42:42 -0800551 }
msarett9e707a02015-09-01 14:57:57 -0700552 }
553 }
msarett0a242972015-06-11 14:27:27 -0700554 }
msarett36c37962015-09-02 13:20:52 -0700555
scroggoe4499842016-02-25 11:03:47 -0800556 if (FLAGS_simpleCodec) {
557 return;
558 }
559
msarett3d9d7a72015-10-21 10:27:10 -0700560 const int sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
msarett36c37962015-09-02 13:20:52 -0700561
msarett3d9d7a72015-10-21 10:27:10 -0700562 for (int sampleSize : sampleSizes) {
msarett91c22b22016-02-22 12:27:46 -0800563 for (CodecSrc::DstColorType colorType : colorTypes) {
scroggoc5560be2016-02-03 09:42:42 -0800564 for (SkAlphaType alphaType : alphaModes) {
msarettf3dc1882016-04-26 13:06:38 -0700565 // We can exercise all of the kNonNative support code in the swizzler with just a
566 // few sample sizes. Skip the rest.
567 if (CodecSrc::kNonNative8888_Always_DstColorType == colorType && sampleSize > 3) {
568 continue;
569 }
570
scroggof8dc9df2016-05-16 09:04:13 -0700571 push_android_codec_src(path, colorType, alphaType, sampleSize);
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.
brianosmanc9ced392016-03-24 07:27:43 -0700590 push_image_gen_src(path, ImageGenSrc::kCodec_Mode, codec->getInfo().alphaType(), true);
msarett18976312016-03-09 14:20:58 -0800591
592 // Push image generator CPU tests.
593 for (SkAlphaType alphaType : alphaModes) {
594 push_image_gen_src(path, ImageGenSrc::kCodec_Mode, alphaType, false);
595
596#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
597 if (kWEBP_SkEncodedFormat != codec->getEncodedFormat() &&
598 kWBMP_SkEncodedFormat != codec->getEncodedFormat() &&
599 kUnpremul_SkAlphaType != alphaType)
600 {
601 push_image_gen_src(path, ImageGenSrc::kPlatform_Mode, alphaType, false);
602 }
msarettfc0b6d12016-03-17 13:50:17 -0700603#elif defined(SK_BUILD_FOR_WIN)
604 if (kWEBP_SkEncodedFormat != codec->getEncodedFormat() &&
605 kWBMP_SkEncodedFormat != codec->getEncodedFormat())
606 {
607 push_image_gen_src(path, ImageGenSrc::kPlatform_Mode, alphaType, false);
608 }
msarett18976312016-03-09 14:20:58 -0800609#endif
610 }
msarett438b2ad2015-04-09 12:43:10 -0700611}
612
msarett5cb48852015-11-06 08:56:32 -0800613static bool brd_color_type_supported(SkBitmapRegionDecoder::Strategy strategy,
msaretta5783ae2015-09-08 15:35:32 -0700614 CodecSrc::DstColorType dstColorType) {
615 switch (strategy) {
msarett5cb48852015-11-06 08:56:32 -0800616 case SkBitmapRegionDecoder::kCanvas_Strategy:
msaretta5783ae2015-09-08 15:35:32 -0700617 if (CodecSrc::kGetFromCanvas_DstColorType == dstColorType) {
618 return true;
619 }
620 return false;
msarett5cb48852015-11-06 08:56:32 -0800621 case SkBitmapRegionDecoder::kAndroidCodec_Strategy:
msarett26ad17b2015-10-22 07:29:19 -0700622 switch (dstColorType) {
623 case CodecSrc::kGetFromCanvas_DstColorType:
624 case CodecSrc::kIndex8_Always_DstColorType:
625 case CodecSrc::kGrayscale_Always_DstColorType:
626 return true;
627 default:
628 return false;
629 }
msaretta5783ae2015-09-08 15:35:32 -0700630 default:
631 SkASSERT(false);
632 return false;
633 }
634}
635
msarett5cb48852015-11-06 08:56:32 -0800636static void push_brd_src(Path path, SkBitmapRegionDecoder::Strategy strategy,
msaretta5783ae2015-09-08 15:35:32 -0700637 CodecSrc::DstColorType dstColorType, BRDSrc::Mode mode, uint32_t sampleSize) {
638 SkString folder;
639 switch (strategy) {
msarett5cb48852015-11-06 08:56:32 -0800640 case SkBitmapRegionDecoder::kCanvas_Strategy:
msaretta5783ae2015-09-08 15:35:32 -0700641 folder.append("brd_canvas");
642 break;
msarett5cb48852015-11-06 08:56:32 -0800643 case SkBitmapRegionDecoder::kAndroidCodec_Strategy:
msarett26ad17b2015-10-22 07:29:19 -0700644 folder.append("brd_android_codec");
645 break;
msaretta5783ae2015-09-08 15:35:32 -0700646 default:
647 SkASSERT(false);
648 return;
649 }
650
651 switch (mode) {
652 case BRDSrc::kFullImage_Mode:
653 break;
654 case BRDSrc::kDivisor_Mode:
655 folder.append("_divisor");
656 break;
657 default:
658 SkASSERT(false);
659 return;
660 }
661
662 switch (dstColorType) {
663 case CodecSrc::kGetFromCanvas_DstColorType:
664 break;
665 case CodecSrc::kIndex8_Always_DstColorType:
666 folder.append("_kIndex");
667 break;
668 case CodecSrc::kGrayscale_Always_DstColorType:
669 folder.append("_kGray");
670 break;
671 default:
672 SkASSERT(false);
673 return;
674 }
675
676 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800677 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700678 }
679
680 BRDSrc* src = new BRDSrc(path, strategy, mode, dstColorType, sampleSize);
681 push_src("image", folder, src);
682}
683
684static void push_brd_srcs(Path path) {
685
msarett5cb48852015-11-06 08:56:32 -0800686 const SkBitmapRegionDecoder::Strategy strategies[] = {
687 SkBitmapRegionDecoder::kCanvas_Strategy,
msarett5cb48852015-11-06 08:56:32 -0800688 SkBitmapRegionDecoder::kAndroidCodec_Strategy,
msaretta5783ae2015-09-08 15:35:32 -0700689 };
690
scroggo501b7342015-11-03 07:55:11 -0800691 // Test on a variety of sampleSizes, making sure to include:
692 // - 2, 4, and 8, which are natively supported by jpeg
693 // - multiples of 2 which are not divisible by 4 (analogous for 4)
694 // - larger powers of two, since BRD clients generally use powers of 2
695 // We will only produce output for the larger sizes on large images.
696 const uint32_t sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 24, 32, 64 };
msarett6efbe052015-09-11 09:01:16 -0700697
msaretta5783ae2015-09-08 15:35:32 -0700698 // We will only test to one backend (8888), but we will test all of the
699 // color types that we need to decode to on this backend.
700 const CodecSrc::DstColorType dstColorTypes[] = {
msarett26ad17b2015-10-22 07:29:19 -0700701 CodecSrc::kGetFromCanvas_DstColorType,
702 CodecSrc::kIndex8_Always_DstColorType,
703 CodecSrc::kGrayscale_Always_DstColorType,
msaretta5783ae2015-09-08 15:35:32 -0700704 };
705
706 const BRDSrc::Mode modes[] = {
msarett26ad17b2015-10-22 07:29:19 -0700707 BRDSrc::kFullImage_Mode,
708 BRDSrc::kDivisor_Mode,
msaretta5783ae2015-09-08 15:35:32 -0700709 };
710
msarett5cb48852015-11-06 08:56:32 -0800711 for (SkBitmapRegionDecoder::Strategy strategy : strategies) {
msarett6efbe052015-09-11 09:01:16 -0700712 for (uint32_t sampleSize : sampleSizes) {
msarett6efbe052015-09-11 09:01:16 -0700713 for (CodecSrc::DstColorType dstColorType : dstColorTypes) {
714 if (brd_color_type_supported(strategy, dstColorType)) {
715 for (BRDSrc::Mode mode : modes) {
msaretta5783ae2015-09-08 15:35:32 -0700716 push_brd_src(path, strategy, dstColorType, mode, sampleSize);
717 }
718 }
719 }
720 }
721 }
722}
723
724static bool brd_supported(const char* ext) {
msarett8c8f22a2015-04-01 06:58:48 -0700725 static const char* const exts[] = {
msaretta5783ae2015-09-08 15:35:32 -0700726 "jpg", "jpeg", "png", "webp",
727 "JPG", "JPEG", "PNG", "WEBP",
msarett8c8f22a2015-04-01 06:58:48 -0700728 };
729
730 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
731 if (0 == strcmp(exts[i], ext)) {
732 return true;
733 }
734 }
735 return false;
scroggo9b77ddd2015-03-19 06:03:39 -0700736}
737
scroggo86737142016-02-03 12:19:11 -0800738static bool gather_srcs() {
mtklein748ca3b2015-01-15 10:56:12 -0800739 for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) {
djsollen54416de2015-04-03 07:24:48 -0700740 push_src("gm", "", new GMSrc(r->factory()));
mtklein748ca3b2015-01-15 10:56:12 -0800741 }
halcanaryfc37ad12015-01-30 07:31:19 -0800742 for (int i = 0; i < FLAGS_skps.count(); i++) {
743 const char* path = FLAGS_skps[i];
744 if (sk_isdir(path)) {
745 SkOSFile::Iter it(path, "skp");
746 for (SkString file; it.next(&file); ) {
djsollen54416de2015-04-03 07:24:48 -0700747 push_src("skp", "", new SKPSrc(SkOSPath::Join(path, file.c_str())));
halcanaryfc37ad12015-01-30 07:31:19 -0800748 }
749 } else {
djsollen54416de2015-04-03 07:24:48 -0700750 push_src("skp", "", new SKPSrc(path));
mtklein114c3cd2015-01-15 10:15:02 -0800751 }
752 }
scroggo86737142016-02-03 12:19:11 -0800753
754 SkTArray<SkString> images;
msarett69deca82016-04-29 09:38:40 -0700755 if (!CollectImages(FLAGS_images, &images)) {
scroggo86737142016-02-03 12:19:11 -0800756 return false;
757 }
758
759 for (auto image : images) {
760 push_codec_srcs(image);
scroggoe4499842016-02-25 11:03:47 -0800761 if (FLAGS_simpleCodec) {
762 continue;
763 }
764
mtklein21eaf3b2016-02-08 12:39:59 -0800765 const char* ext = strrchr(image.c_str(), '.');
766 if (ext && brd_supported(ext+1)) {
scroggo86737142016-02-03 12:19:11 -0800767 push_brd_srcs(image);
mtklein709d2c32015-01-15 08:30:25 -0800768 }
mtklein709d2c32015-01-15 08:30:25 -0800769 }
scroggo86737142016-02-03 12:19:11 -0800770
msarett69deca82016-04-29 09:38:40 -0700771 SkTArray<SkString> colorImages;
772 if (!CollectImages(FLAGS_colorImages, &colorImages)) {
773 return false;
774 }
775
776 for (auto colorImage : colorImages) {
777 ColorCodecSrc* src = new ColorCodecSrc(colorImage, ColorCodecSrc::kBaseline_Mode);
778 push_src("image", "color_codec_baseline", src);
779 }
780
scroggo86737142016-02-03 12:19:11 -0800781 return true;
mtklein709d2c32015-01-15 08:30:25 -0800782}
783
kkinnunen3e980c32015-12-23 01:33:00 -0800784static void push_sink(const SkCommandLineConfig& config, Sink* s) {
rmistry0f515bd2015-12-22 10:22:26 -0800785 SkAutoTDelete<Sink> sink(s);
kkinnunen3e980c32015-12-23 01:33:00 -0800786
mtkleine0effd62015-07-29 06:37:28 -0700787 // Try a simple Src as a canary. If it fails, skip this sink.
mtklein748ca3b2015-01-15 10:56:12 -0800788 struct : public Src {
mtkleine0effd62015-07-29 06:37:28 -0700789 Error draw(SkCanvas* c) const override {
790 c->drawRect(SkRect::MakeWH(1,1), SkPaint());
791 return "";
792 }
mtklein36352bf2015-03-25 18:17:31 -0700793 SkISize size() const override { return SkISize::Make(16, 16); }
mtkleine0effd62015-07-29 06:37:28 -0700794 Name name() const override { return "justOneRect"; }
795 } justOneRect;
mtklein114c3cd2015-01-15 10:15:02 -0800796
mtklein748ca3b2015-01-15 10:56:12 -0800797 SkBitmap bitmap;
798 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800799 SkString log;
mtkleine0effd62015-07-29 06:37:28 -0700800 Error err = sink->draw(justOneRect, &bitmap, &stream, &log);
mtklein4089ef72015-03-05 08:40:28 -0800801 if (err.isFatal()) {
mtkleinc41fd922016-03-08 09:01:39 -0800802 info("Could not run %s: %s\n", config.getTag().c_str(), err.c_str());
mtklein05641a52015-04-21 10:49:13 -0700803 exit(1);
mtklein114c3cd2015-01-15 10:15:02 -0800804 }
805
mtkleine0effd62015-07-29 06:37:28 -0700806 TaggedSink& ts = gSinks.push_back();
mtklein18300a32016-03-16 13:53:35 -0700807 ts.reset(sink.release());
kkinnunen3e980c32015-12-23 01:33:00 -0800808 ts.tag = config.getTag();
mtklein748ca3b2015-01-15 10:56:12 -0800809}
810
811static bool gpu_supported() {
812#if SK_SUPPORT_GPU
813 return FLAGS_gpu;
814#else
815 return false;
816#endif
817}
kkinnunen9ebc3f02015-12-21 23:48:13 -0800818
kkinnunen3e980c32015-12-23 01:33:00 -0800819static Sink* create_sink(const SkCommandLineConfig* config) {
820#if SK_SUPPORT_GPU
821 if (gpu_supported()) {
822 if (const SkCommandLineConfigGpu* gpuConfig = config->asConfigGpu()) {
bsalomon85b4b532016-04-05 11:06:27 -0700823 GrContextFactory::ContextType contextType = gpuConfig->getContextType();
824 GrContextFactory::ContextOptions contextOptions =
825 GrContextFactory::kNone_ContextOptions;
kkinnunen3e980c32015-12-23 01:33:00 -0800826 if (gpuConfig->getUseNVPR()) {
bsalomon85b4b532016-04-05 11:06:27 -0700827 contextOptions = static_cast<GrContextFactory::ContextOptions>(
828 contextOptions | GrContextFactory::kEnableNVPR_ContextOptions);
kkinnunen3e980c32015-12-23 01:33:00 -0800829 }
brianosman898235c2016-04-06 07:38:23 -0700830 if (SkColorAndProfileAreGammaCorrect(gpuConfig->getColorType(),
831 gpuConfig->getProfileType())) {
bsalomon85b4b532016-04-05 11:06:27 -0700832 contextOptions = static_cast<GrContextFactory::ContextOptions>(
833 contextOptions | GrContextFactory::kRequireSRGBSupport_ContextOptions);
brianosman61d3b082016-03-30 11:19:36 -0700834 }
kkinnunen3e980c32015-12-23 01:33:00 -0800835 GrContextFactory testFactory;
836 if (!testFactory.get(contextType, contextOptions)) {
mtkleinc41fd922016-03-08 09:01:39 -0800837 info("WARNING: can not create GPU context for config '%s'. "
838 "GM tests will be skipped.\n", gpuConfig->getTag().c_str());
kkinnunen3e980c32015-12-23 01:33:00 -0800839 return nullptr;
840 }
841 return new GPUSink(contextType, contextOptions, gpuConfig->getSamples(),
brianosmand93c1202016-03-10 07:49:08 -0800842 gpuConfig->getUseDIText(), gpuConfig->getColorType(),
843 gpuConfig->getProfileType(), FLAGS_gpu_threading);
kkinnunen3e980c32015-12-23 01:33:00 -0800844 }
845 }
846#endif
847
848#define SINK(t, sink, ...) if (config->getBackend().equals(t)) { return new sink(__VA_ARGS__); }
849
tomhudsoneebc39a2015-02-23 12:18:05 -0800850#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
851 SINK("hwui", HWUISink);
852#endif
853
mtklein748ca3b2015-01-15 10:56:12 -0800854 if (FLAGS_cpu) {
855 SINK("565", RasterSink, kRGB_565_SkColorType);
856 SINK("8888", RasterSink, kN32_SkColorType);
mtklein27c3fdd2016-02-26 14:43:21 -0800857 SINK("srgb", RasterSink, kN32_SkColorType, kSRGB_SkColorProfileType);
858 SINK("f16", RasterSink, kRGBA_F16_SkColorType);
halcanary94b87bd2016-04-04 06:12:15 -0700859 SINK("pdf", PDFSink);
mtklein9c3f17d2015-01-28 11:35:18 -0800860 SINK("skp", SKPSink);
mtklein8a4527e2015-01-31 20:00:58 -0800861 SINK("svg", SVGSink);
862 SINK("null", NullSink);
halcanary47ef4d52015-03-03 09:13:09 -0800863 SINK("xps", XPSSink);
halcanary4b656662016-04-27 07:45:18 -0700864 SINK("pdfa", PDFSink, true);
mtklein748ca3b2015-01-15 10:56:12 -0800865 }
866#undef SINK
halcanary96fcdcc2015-08-27 07:41:13 -0700867 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800868}
869
kkinnunen3e980c32015-12-23 01:33:00 -0800870static Sink* create_via(const SkString& tag, Sink* wrapped) {
871#define VIA(t, via, ...) if (tag.equals(t)) { return new via(__VA_ARGS__); }
halcanary96fcdcc2015-08-27 07:41:13 -0700872 VIA("twice", ViaTwice, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700873 VIA("serialize", ViaSerialization, wrapped);
mtklein4a34ecb2016-01-08 10:19:35 -0800874 VIA("pic", ViaPicture, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700875 VIA("2ndpic", ViaSecondPicture, wrapped);
mtkleind31c13d2015-05-05 12:59:56 -0700876 VIA("sp", ViaSingletonPictures, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700877 VIA("tiles", ViaTiles, 256, 256, nullptr, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800878 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800879
mtkleind603b222015-02-17 11:13:33 -0800880 if (FLAGS_matrix.count() == 4) {
mtklein748ca3b2015-01-15 10:56:12 -0800881 SkMatrix m;
mtkleind603b222015-02-17 11:13:33 -0800882 m.reset();
883 m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
884 m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
885 m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
886 m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
887 VIA("matrix", ViaMatrix, m, wrapped);
888 VIA("upright", ViaUpright, m, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800889 }
tomhudson64de1e12015-03-05 08:01:07 -0800890
891#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
892 VIA("androidsdk", ViaAndroidSDK, wrapped);
893#endif
894
mtklein748ca3b2015-01-15 10:56:12 -0800895#undef VIA
halcanary96fcdcc2015-08-27 07:41:13 -0700896 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800897}
898
brianosman05c987c2016-05-09 13:42:16 -0700899static bool gather_sinks() {
kkinnunen3e980c32015-12-23 01:33:00 -0800900 SkCommandLineConfigArray configs;
901 ParseConfigs(FLAGS_config, &configs);
902 for (int i = 0; i < configs.count(); i++) {
903 const SkCommandLineConfig& config = *configs[i];
904 Sink* sink = create_sink(&config);
905 if (sink == nullptr) {
mtkleinc41fd922016-03-08 09:01:39 -0800906 info("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
907 config.getTag().c_str());
kkinnunen3e980c32015-12-23 01:33:00 -0800908 continue;
909 }
mtklein748ca3b2015-01-15 10:56:12 -0800910
kkinnunen3e980c32015-12-23 01:33:00 -0800911 const SkTArray<SkString>& parts = config.getViaParts();
912 for (int j = parts.count(); j-- > 0;) {
913 const SkString& part = parts[j];
914 Sink* next = create_via(part, sink);
halcanary96fcdcc2015-08-27 07:41:13 -0700915 if (next == nullptr) {
mtkleinc41fd922016-03-08 09:01:39 -0800916 info("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
917 part.c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800918 delete sink;
halcanary96fcdcc2015-08-27 07:41:13 -0700919 sink = nullptr;
mtklein748ca3b2015-01-15 10:56:12 -0800920 break;
921 }
922 sink = next;
923 }
924 if (sink) {
925 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800926 }
927 }
brianosman05c987c2016-05-09 13:42:16 -0700928
929 // If no configs were requested (just running tests, perhaps?), then we're okay.
930 // Otherwise, make sure that at least one sink was constructed correctly. This catches
931 // the case of bots without a GPU being assigned GPU configs.
932 return (configs.count() == 0) || (gSinks.count() > 0);
mtklein114c3cd2015-01-15 10:15:02 -0800933}
mtklein709d2c32015-01-15 08:30:25 -0800934
mtkleina5114d72015-08-24 13:27:01 -0700935static bool dump_png(SkBitmap bitmap, const char* path, const char* md5) {
936 const int w = bitmap.width(),
937 h = bitmap.height();
938
brianosman3c579dc2016-04-19 09:18:11 -0700939 sk_sp<SkData> encodedBitmap = sk_tools::encode_bitmap_for_png(bitmap);
940 if (encodedBitmap.get() == nullptr) {
941 return false;
mtkleina5114d72015-08-24 13:27:01 -0700942 }
brianosman3c579dc2016-04-19 09:18:11 -0700943 uint32_t* rgba = static_cast<uint32_t*>(encodedBitmap.get()->writable_data());
mtkleina5114d72015-08-24 13:27:01 -0700944
945 // We don't need bitmap anymore. Might as well drop our ref.
mtkleinfccb77d2015-08-24 14:13:29 -0700946 bitmap.reset();
mtkleina5114d72015-08-24 13:27:01 -0700947
mtkleinc64137c2015-08-25 10:56:08 -0700948 FILE* f = fopen(path, "wb");
mtkleina5114d72015-08-24 13:27:01 -0700949 if (!f) { return false; }
950
951 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
952 if (!png) {
953 fclose(f);
954 return false;
955 }
956
957 png_infop info = png_create_info_struct(png);
958 if (!info) {
959 png_destroy_write_struct(&png, &info);
960 fclose(f);
961 return false;
962 }
963
mtkleind69ece62015-09-10 10:37:44 -0700964 SkString description;
965 description.append("Key: ");
966 for (int i = 0; i < FLAGS_key.count(); i++) {
967 description.appendf("%s ", FLAGS_key[i]);
968 }
969 description.append("Properties: ");
970 for (int i = 0; i < FLAGS_properties.count(); i++) {
971 description.appendf("%s ", FLAGS_properties[i]);
972 }
973 description.appendf("MD5: %s", md5);
974
mtkleina5114d72015-08-24 13:27:01 -0700975 png_text text[2];
976 text[0].key = (png_charp)"Author";
977 text[0].text = (png_charp)"DM dump_png()";
978 text[0].compression = PNG_TEXT_COMPRESSION_NONE;
979 text[1].key = (png_charp)"Description";
mtkleind69ece62015-09-10 10:37:44 -0700980 text[1].text = (png_charp)description.c_str();
mtkleina5114d72015-08-24 13:27:01 -0700981 text[1].compression = PNG_TEXT_COMPRESSION_NONE;
982 png_set_text(png, info, text, 2);
983
984 png_init_io(png, f);
985 png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8,
986 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
987 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
988 png_write_info(png, info);
989 for (int j = 0; j < h; j++) {
brianosman3c579dc2016-04-19 09:18:11 -0700990 png_bytep row = (png_bytep)(rgba + w*j);
mtkleina5114d72015-08-24 13:27:01 -0700991 png_write_rows(png, &row, 1);
992 }
993 png_write_end(png, info);
994
995 png_destroy_write_struct(&png, &info);
996 fclose(f);
997 return true;
998}
999
mtkleina2ef6422015-01-15 13:44:22 -08001000static bool match(const char* needle, const char* haystack) {
halcanary96fcdcc2015-08-27 07:41:13 -07001001 return 0 == strcmp("_", needle) || nullptr != strstr(haystack, needle);
mtkleina2ef6422015-01-15 13:44:22 -08001002}
1003
mtklein3eed7dd2016-02-24 19:07:07 -08001004static bool is_blacklisted(const char* sink, const char* src,
1005 const char* srcOptions, const char* name) {
mtklein0d243ff2015-04-03 07:51:00 -07001006 for (int i = 0; i < FLAGS_blacklist.count() - 3; i += 4) {
mtkleina2ef6422015-01-15 13:44:22 -08001007 if (match(FLAGS_blacklist[i+0], sink) &&
djsollen54416de2015-04-03 07:24:48 -07001008 match(FLAGS_blacklist[i+1], src) &&
1009 match(FLAGS_blacklist[i+2], srcOptions) &&
1010 match(FLAGS_blacklist[i+3], name)) {
mtklein3eed7dd2016-02-24 19:07:07 -08001011 return true;
mtkleina2ef6422015-01-15 13:44:22 -08001012 }
1013 }
mtklein3eed7dd2016-02-24 19:07:07 -08001014 return false;
mtkleina2ef6422015-01-15 13:44:22 -08001015}
1016
mtkleincd50bca2016-01-05 06:20:20 -08001017// Even when a Task Sink reports to be non-threadsafe (e.g. GPU), we know things like
1018// .png encoding are definitely thread safe. This lets us offload that work to CPU threads.
1019static SkTaskGroup gDefinitelyThreadSafeWork;
1020
mtklein748ca3b2015-01-15 10:56:12 -08001021// The finest-grained unit of work we can run: draw a single Src into a single Sink,
1022// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
1023struct Task {
mtkleine0effd62015-07-29 06:37:28 -07001024 Task(const TaggedSrc& src, const TaggedSink& sink) : src(src), sink(sink) {}
1025 const TaggedSrc& src;
1026 const TaggedSink& sink;
mtklein748ca3b2015-01-15 10:56:12 -08001027
mtklein21eaf3b2016-02-08 12:39:59 -08001028 static void Run(const Task& task) {
1029 SkString name = task.src->name();
mtkleine0effd62015-07-29 06:37:28 -07001030
mtkleinb9eb4ac2015-02-02 18:26:03 -08001031 SkString log;
mtklein3eed7dd2016-02-24 19:07:07 -08001032 if (!FLAGS_dryRun) {
mtklein748ca3b2015-01-15 10:56:12 -08001033 SkBitmap bitmap;
1034 SkDynamicMemoryWStream stream;
mtklein3eed7dd2016-02-24 19:07:07 -08001035 start(task.sink.tag.c_str(), task.src.tag.c_str(),
1036 task.src.options.c_str(), name.c_str());
mtklein21eaf3b2016-02-08 12:39:59 -08001037 Error err = task.sink->draw(*task.src, &bitmap, &stream, &log);
mtkleinb3b13b72016-03-07 13:20:52 -08001038 if (!log.isEmpty()) {
mtkleinc41fd922016-03-08 09:01:39 -08001039 info("%s %s %s %s:\n%s\n", task.sink.tag.c_str()
1040 , task.src.tag.c_str()
1041 , task.src.options.c_str()
1042 , name.c_str()
1043 , log.c_str());
mtkleinb3b13b72016-03-07 13:20:52 -08001044 }
mtklein748ca3b2015-01-15 10:56:12 -08001045 if (!err.isEmpty()) {
mtklein4089ef72015-03-05 08:40:28 -08001046 if (err.isFatal()) {
djsollen54416de2015-04-03 07:24:48 -07001047 fail(SkStringPrintf("%s %s %s %s: %s",
mtklein21eaf3b2016-02-08 12:39:59 -08001048 task.sink.tag.c_str(),
1049 task.src.tag.c_str(),
1050 task.src.options.c_str(),
mtklein4089ef72015-03-05 08:40:28 -08001051 name.c_str(),
1052 err.c_str()));
mtkleinb37cb412015-03-18 05:27:14 -07001053 } else {
mtklein3eed7dd2016-02-24 19:07:07 -08001054 done(task.sink.tag.c_str(), task.src.tag.c_str(),
1055 task.src.options.c_str(), name.c_str());
mtkleinba6ada72016-01-21 09:39:35 -08001056 return;
mtklein4089ef72015-03-05 08:40:28 -08001057 }
mtklein748ca3b2015-01-15 10:56:12 -08001058 }
mtklein62bd1a62015-01-27 14:46:26 -08001059
mtkleincd50bca2016-01-05 06:20:20 -08001060 // We're likely switching threads here, so we must capture by value, [=] or [foo,bar].
1061 SkStreamAsset* data = stream.detachAsStream();
1062 gDefinitelyThreadSafeWork.add([task,name,bitmap,data]{
1063 SkAutoTDelete<SkStreamAsset> ownedData(data);
1064
1065 // Why doesn't the copy constructor do this when we have pre-locked pixels?
1066 bitmap.lockPixels();
1067
1068 SkString md5;
1069 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
1070 SkMD5 hash;
1071 if (data->getLength()) {
1072 hash.writeStream(data, data->getLength());
1073 data->rewind();
mtklein38408462015-07-08 07:25:27 -07001074 } else {
mtkleincd50bca2016-01-05 06:20:20 -08001075 // If we're BGRA (Linux, Windows), swizzle over to RGBA (Mac, Android).
1076 // This helps eliminate multiple 0-pixel-diff hashes on gold.skia.org.
1077 // (Android's general slow speed breaks the tie arbitrarily in RGBA's favor.)
1078 // We might consider promoting 565 to RGBA too.
1079 if (bitmap.colorType() == kBGRA_8888_SkColorType) {
1080 SkBitmap swizzle;
1081 SkAssertResult(bitmap.copyTo(&swizzle, kRGBA_8888_SkColorType));
1082 hash.write(swizzle.getPixels(), swizzle.getSize());
1083 } else {
1084 hash.write(bitmap.getPixels(), bitmap.getSize());
1085 }
1086 }
1087 SkMD5::Digest digest;
1088 hash.finish(digest);
1089 for (int i = 0; i < 16; i++) {
1090 md5.appendf("%02x", digest.data[i]);
mtklein38408462015-07-08 07:25:27 -07001091 }
mtklein62bd1a62015-01-27 14:46:26 -08001092 }
mtklein62bd1a62015-01-27 14:46:26 -08001093
mtkleincd50bca2016-01-05 06:20:20 -08001094 if (!FLAGS_readPath.isEmpty() &&
mtklein3eed7dd2016-02-24 19:07:07 -08001095 !gGold.contains(Gold(task.sink.tag, task.src.tag,
1096 task.src.options, name, md5))) {
mtkleincd50bca2016-01-05 06:20:20 -08001097 fail(SkStringPrintf("%s not found for %s %s %s %s in %s",
1098 md5.c_str(),
mtklein21eaf3b2016-02-08 12:39:59 -08001099 task.sink.tag.c_str(),
1100 task.src.tag.c_str(),
1101 task.src.options.c_str(),
mtkleincd50bca2016-01-05 06:20:20 -08001102 name.c_str(),
1103 FLAGS_readPath[0]));
mtklein748ca3b2015-01-15 10:56:12 -08001104 }
mtkleincd50bca2016-01-05 06:20:20 -08001105
1106 if (!FLAGS_writePath.isEmpty()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001107 const char* ext = task.sink->fileExtension();
mtkleincd50bca2016-01-05 06:20:20 -08001108 if (data->getLength()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001109 WriteToDisk(task, md5, ext, data, data->getLength(), nullptr);
mtkleincd50bca2016-01-05 06:20:20 -08001110 SkASSERT(bitmap.drawsNothing());
1111 } else if (!bitmap.drawsNothing()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001112 WriteToDisk(task, md5, ext, nullptr, 0, &bitmap);
mtkleincd50bca2016-01-05 06:20:20 -08001113 }
1114 }
1115 });
mtklein748ca3b2015-01-15 10:56:12 -08001116 }
mtklein3eed7dd2016-02-24 19:07:07 -08001117 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 -08001118 }
1119
1120 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -08001121 SkString md5,
1122 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -08001123 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -08001124 const SkBitmap* bitmap) {
mtklein409d4702016-02-29 07:38:01 -08001125 bool gammaCorrect = false;
1126 if (bitmap) {
brianosman898235c2016-04-06 07:38:23 -07001127 gammaCorrect = SkImageInfoIsGammaCorrect(bitmap->info());
mtklein409d4702016-02-29 07:38:01 -08001128 }
1129
mtklein748ca3b2015-01-15 10:56:12 -08001130 JsonWriter::BitmapResult result;
djsollen54416de2015-04-03 07:24:48 -07001131 result.name = task.src->name();
mtklein3eed7dd2016-02-24 19:07:07 -08001132 result.config = task.sink.tag;
djsollen54416de2015-04-03 07:24:48 -07001133 result.sourceType = task.src.tag;
1134 result.sourceOptions = task.src.options;
1135 result.ext = ext;
mtklein409d4702016-02-29 07:38:01 -08001136 result.gammaCorrect = gammaCorrect;
djsollen54416de2015-04-03 07:24:48 -07001137 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -08001138 JsonWriter::AddBitmapResult(result);
1139
mtkleinb0531a72015-04-07 13:38:48 -07001140 // If an MD5 is uninteresting, we want it noted in the JSON file,
1141 // but don't want to dump it out as a .png (or whatever ext is).
1142 if (gUninterestingHashes.contains(md5)) {
1143 return;
1144 }
1145
mtklein748ca3b2015-01-15 10:56:12 -08001146 const char* dir = FLAGS_writePath[0];
1147 if (0 == strcmp(dir, "@")) { // Needed for iOS.
1148 dir = FLAGS_resourcePath[0];
1149 }
1150 sk_mkdir(dir);
1151
1152 SkString path;
1153 if (FLAGS_nameByHash) {
1154 path = SkOSPath::Join(dir, result.md5.c_str());
1155 path.append(".");
1156 path.append(ext);
1157 if (sk_exists(path.c_str())) {
1158 return; // Content-addressed. If it exists already, we're done.
1159 }
1160 } else {
kkinnunen3e980c32015-12-23 01:33:00 -08001161 path = SkOSPath::Join(dir, task.sink.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001162 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001163 path = SkOSPath::Join(path.c_str(), task.src.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001164 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001165 if (strcmp(task.src.options.c_str(), "") != 0) {
1166 path = SkOSPath::Join(path.c_str(), task.src.options.c_str());
djsollen54416de2015-04-03 07:24:48 -07001167 sk_mkdir(path.c_str());
1168 }
mtklein748ca3b2015-01-15 10:56:12 -08001169 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
1170 path.append(".");
1171 path.append(ext);
1172 }
1173
mtklein748ca3b2015-01-15 10:56:12 -08001174 if (bitmap) {
mtkleina5114d72015-08-24 13:27:01 -07001175 if (!dump_png(*bitmap, path.c_str(), result.md5.c_str())) {
mtklein748ca3b2015-01-15 10:56:12 -08001176 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
1177 return;
1178 }
1179 } else {
mtkleina5114d72015-08-24 13:27:01 -07001180 SkFILEWStream file(path.c_str());
1181 if (!file.isValid()) {
1182 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
1183 return;
1184 }
mtklein748ca3b2015-01-15 10:56:12 -08001185 if (!file.writeStream(data, len)) {
1186 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
1187 return;
1188 }
1189 }
1190 }
1191};
1192
mtklein748ca3b2015-01-15 10:56:12 -08001193/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1194
1195// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
1196
mtklein21eaf3b2016-02-08 12:39:59 -08001197static SkTDArray<skiatest::Test> gParallelTests, gSerialTests;
mtklein748ca3b2015-01-15 10:56:12 -08001198
1199static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -08001200 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -08001201 return;
1202 }
mtklein6393c062015-04-27 08:45:01 -07001203 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r; r = r->next()) {
1204 if (!in_shard()) {
1205 continue;
1206 }
halcanary87f3ba42015-01-20 09:30:20 -08001207 // Despite its name, factory() is returning a reference to
1208 // link-time static const POD data.
1209 const skiatest::Test& test = r->factory();
1210 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -08001211 continue;
1212 }
halcanary87f3ba42015-01-20 09:30:20 -08001213 if (test.needsGpu && gpu_supported()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001214 (FLAGS_gpu_threading ? gParallelTests : gSerialTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -08001215 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein21eaf3b2016-02-08 12:39:59 -08001216 gParallelTests.push(test);
mtklein82d28432015-01-15 12:46:02 -08001217 }
mtklein748ca3b2015-01-15 10:56:12 -08001218 }
1219}
1220
mtklein21eaf3b2016-02-08 12:39:59 -08001221static void run_test(skiatest::Test test) {
halcanary87f3ba42015-01-20 09:30:20 -08001222 struct : public skiatest::Reporter {
mtklein36352bf2015-03-25 18:17:31 -07001223 void reportFailed(const skiatest::Failure& failure) override {
halcanary87f3ba42015-01-20 09:30:20 -08001224 fail(failure.toString());
1225 JsonWriter::AddTestFailure(failure);
1226 }
mtklein36352bf2015-03-25 18:17:31 -07001227 bool allowExtendedTest() const override {
halcanary87f3ba42015-01-20 09:30:20 -08001228 return FLAGS_pathOpsExtended;
1229 }
mtklein36352bf2015-03-25 18:17:31 -07001230 bool verbose() const override { return FLAGS_veryVerbose; }
halcanary87f3ba42015-01-20 09:30:20 -08001231 } reporter;
djsollen824996a2015-06-12 12:06:22 -07001232
mtklein3eed7dd2016-02-24 19:07:07 -08001233 if (!FLAGS_dryRun && !is_blacklisted("_", "tests", "_", test.name)) {
mtklein21eaf3b2016-02-08 12:39:59 -08001234 start("unit", "test", "", test.name);
mtklein55e88b22015-01-21 15:50:13 -08001235 GrContextFactory factory;
mtklein21eaf3b2016-02-08 12:39:59 -08001236 test.proc(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -08001237 }
mtklein3eed7dd2016-02-24 19:07:07 -08001238 done("unit", "test", "", test.name);
mtklein748ca3b2015-01-15 10:56:12 -08001239}
1240
1241/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1242
mtkleinafae30a2016-02-24 12:28:32 -08001243DEFINE_int32(status_sec, 15, "Print status this often (and if we crash).");
1244
1245SkThread* start_status_thread() {
1246 auto thread = new SkThread([] (void*) {
1247 for (;;) {
1248 print_status();
1249 #if defined(SK_BUILD_FOR_WIN)
1250 Sleep(FLAGS_status_sec * 1000);
1251 #else
1252 sleep(FLAGS_status_sec);
1253 #endif
mtklein2e1c47e2015-03-12 07:16:56 -07001254 }
mtkleinafae30a2016-02-24 12:28:32 -08001255 });
1256 thread->start();
1257 return thread;
mtkleinde6fc2b2015-03-12 06:28:54 -07001258}
1259
caryclark83ca6282015-06-10 09:31:09 -07001260#define PORTABLE_FONT_PREFIX "Toy Liberation "
1261
bungeman13b9c952016-05-12 10:09:30 -07001262static sk_sp<SkTypeface> create_from_name(const char familyName[], SkTypeface::Style style) {
caryclark83ca6282015-06-10 09:31:09 -07001263 if (familyName && strlen(familyName) > sizeof(PORTABLE_FONT_PREFIX)
1264 && !strncmp(familyName, PORTABLE_FONT_PREFIX, sizeof(PORTABLE_FONT_PREFIX) - 1)) {
caryclark1818acb2015-07-24 12:09:25 -07001265 return sk_tool_utils::create_portable_typeface(familyName, style);
caryclark83ca6282015-06-10 09:31:09 -07001266 }
halcanary96fcdcc2015-08-27 07:41:13 -07001267 return nullptr;
caryclark83ca6282015-06-10 09:31:09 -07001268}
1269
1270#undef PORTABLE_FONT_PREFIX
1271
bungeman13b9c952016-05-12 10:09:30 -07001272extern sk_sp<SkTypeface> (*gCreateTypefaceDelegate)(const char [], SkTypeface::Style );
caryclark83ca6282015-06-10 09:31:09 -07001273
jcgregorio3b27ade2014-11-13 08:06:40 -08001274int dm_main();
caryclark17f0b6d2014-07-22 10:15:34 -07001275int dm_main() {
bsalomon71de3532016-04-26 14:27:21 -07001276 setbuf(stdout, nullptr);
mtklein246ba3a2016-02-23 10:39:36 -08001277 setup_crash_handler();
mtkleinafae30a2016-02-24 12:28:32 -08001278
mtkleina483da32016-03-17 12:53:36 -07001279 if (FLAGS_verbose) {
1280 gVLog = stderr;
1281 } else if (!FLAGS_writePath.isEmpty()) {
mtklein51c8cfc2016-03-11 12:59:09 -08001282 sk_mkdir(FLAGS_writePath[0]);
1283 gVLog = freopen(SkOSPath::Join(FLAGS_writePath[0], "verbose.log").c_str(), "w", stderr);
mtkleinc41fd922016-03-08 09:01:39 -08001284 }
1285
mtklein5286f022016-01-22 08:18:14 -08001286 JsonWriter::DumpJson(); // It's handy for the bots to assume this is ~never missing.
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +00001287 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -07001288 SkTaskGroup::Enabler enabled(FLAGS_threads);
caryclark83ca6282015-06-10 09:31:09 -07001289 gCreateTypefaceDelegate = &create_from_name;
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +00001290
halcanary7d571242016-02-24 17:59:16 -08001291 {
1292 SkString testResourcePath = GetResourcePath("color_wheel.png");
1293 SkFILEStream testResource(testResourcePath.c_str());
1294 if (!testResource.isValid()) {
mtkleinc41fd922016-03-08 09:01:39 -08001295 info("Some resources are missing. Do you need to set --resourcePath?\n");
halcanary7d571242016-02-24 17:59:16 -08001296 }
1297 }
mtklein62bd1a62015-01-27 14:46:26 -08001298 gather_gold();
borenet09ed4802015-04-03 14:15:33 -07001299 gather_uninteresting_hashes();
mtklein62bd1a62015-01-27 14:46:26 -08001300
scroggo86737142016-02-03 12:19:11 -08001301 if (!gather_srcs()) {
1302 return 1;
1303 }
brianosman05c987c2016-05-09 13:42:16 -07001304 if (!gather_sinks()) {
1305 return 1;
1306 }
mtklein748ca3b2015-01-15 10:56:12 -08001307 gather_tests();
mtklein21eaf3b2016-02-08 12:39:59 -08001308 gPending = gSrcs.count() * gSinks.count() + gParallelTests.count() + gSerialTests.count();
mtkleinc41fd922016-03-08 09:01:39 -08001309 info("%d srcs * %d sinks + %d tests == %d tasks",
1310 gSrcs.count(), gSinks.count(), gParallelTests.count() + gSerialTests.count(), gPending);
mtkleinafae30a2016-02-24 12:28:32 -08001311 SkAutoTDelete<SkThread> statusThread(start_status_thread());
mtklein748ca3b2015-01-15 10:56:12 -08001312
mtklein21eaf3b2016-02-08 12:39:59 -08001313 // Kick off as much parallel work as we can, making note of any serial work we'll need to do.
1314 SkTaskGroup parallel;
1315 SkTArray<Task> serial;
1316
1317 for (auto& sink : gSinks)
1318 for (auto& src : gSrcs) {
mtklein3eed7dd2016-02-24 19:07:07 -08001319 if (src->veto(sink->flags()) ||
1320 is_blacklisted(sink.tag.c_str(), src.tag.c_str(),
1321 src.options.c_str(), src->name().c_str())) {
mtklein15923c92016-02-29 10:14:38 -08001322 SkAutoTAcquire<SkSpinlock> lock(gMutex);
mtklein3eed7dd2016-02-24 19:07:07 -08001323 gPending--;
1324 continue;
1325 }
1326
mtklein21eaf3b2016-02-08 12:39:59 -08001327 Task task(src, sink);
1328 if (src->serial() || sink->serial()) {
1329 serial.push_back(task);
1330 } else {
1331 parallel.add([task] { Task::Run(task); });
mtklein748ca3b2015-01-15 10:56:12 -08001332 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001333 }
mtklein21eaf3b2016-02-08 12:39:59 -08001334 for (auto test : gParallelTests) {
1335 parallel.add([test] { run_test(test); });
mtklein55e88b22015-01-21 15:50:13 -08001336 }
mtklein21eaf3b2016-02-08 12:39:59 -08001337
1338 // With the parallel work running, run serial tasks and tests here on main thread.
1339 for (auto task : serial) { Task::Run(task); }
1340 for (auto test : gSerialTests) { run_test(test); }
1341
1342 // Wait for any remaining parallel work to complete (including any spun off of serial tasks).
1343 parallel.wait();
mtkleincd50bca2016-01-05 06:20:20 -08001344 gDefinitelyThreadSafeWork.wait();
1345
mtkleinda884c42016-02-24 18:00:23 -08001346 // We'd better have run everything.
1347 SkASSERT(gPending == 0);
mtkleine027f172016-02-26 15:53:06 -08001348 // Make sure we've flushed all our results to disk.
1349 JsonWriter::DumpJson();
mtkleinda884c42016-02-24 18:00:23 -08001350
mtklein748ca3b2015-01-15 10:56:12 -08001351 // At this point we're back in single-threaded land.
caryclarkf53ce802015-06-15 06:48:30 -07001352 sk_tool_utils::release_portable_typefaces();
mtklein@google.comd36522d2013-10-16 13:02:15 +00001353
mtklein748ca3b2015-01-15 10:56:12 -08001354 if (gFailures.count() > 0) {
mtkleinc41fd922016-03-08 09:01:39 -08001355 info("Failures:\n");
mtklein748ca3b2015-01-15 10:56:12 -08001356 for (int i = 0; i < gFailures.count(); i++) {
mtkleinc41fd922016-03-08 09:01:39 -08001357 info("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001358 }
mtkleinc41fd922016-03-08 09:01:39 -08001359 info("%d failures\n", gFailures.count());
mtklein748ca3b2015-01-15 10:56:12 -08001360 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001361 }
mtkleinafae30a2016-02-24 12:28:32 -08001362
1363#ifdef SK_PDF_IMAGE_STATS
halcanary7a14b312015-10-01 07:28:13 -07001364 SkPDFImageDumpStats();
mtkleinafae30a2016-02-24 12:28:32 -08001365#endif // SK_PDF_IMAGE_STATS
1366
1367 print_status();
mtkleinc41fd922016-03-08 09:01:39 -08001368 info("Finished!\n");
mtklein748ca3b2015-01-15 10:56:12 -08001369 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +00001370}
jcgregorio3b27ade2014-11-13 08:06:40 -08001371
kkinnunen179a8f52015-11-20 13:32:24 -08001372// TODO: currently many GPU tests are declared outside SK_SUPPORT_GPU guards.
1373// Thus we export the empty RunWithGPUTestContexts when SK_SUPPORT_GPU=0.
1374namespace skiatest {
bsalomon758586c2016-04-06 14:02:39 -07001375
1376#if SK_SUPPORT_GPU
1377bool IsGLContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1378 return kOpenGL_GrBackend == GrContextFactory::ContextTypeBackend(type);
1379}
bsalomondc0fcd42016-04-11 14:21:33 -07001380bool IsVulkanContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1381 return kVulkan_GrBackend == GrContextFactory::ContextTypeBackend(type);
1382}
bsalomon758586c2016-04-06 14:02:39 -07001383bool IsRenderingGLContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1384 return IsGLContextType(type) && GrContextFactory::IsRenderingContext(type);
1385}
1386bool IsNullGLContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1387 return type == GrContextFactory::kNullGL_ContextType;
1388}
1389#else
1390bool IsGLContextType(int) { return false; }
bsalomondc0fcd42016-04-11 14:21:33 -07001391bool IsVulkanContextType(int) { return false; }
bsalomon758586c2016-04-06 14:02:39 -07001392bool IsRenderingGLContextType(int) { return false; }
1393bool IsNullGLContextType(int) { return false; }
1394#endif
1395
1396void RunWithGPUTestContexts(GrContextTestFn* test, GrContextTypeFilterFn* contextTypeFilter,
bsalomonf2f1c172016-04-05 12:59:06 -07001397 Reporter* reporter, GrContextFactory* factory) {
kkinnunen179a8f52015-11-20 13:32:24 -08001398#if SK_SUPPORT_GPU
kkinnunen3e980c32015-12-23 01:33:00 -08001399
bsalomon758586c2016-04-06 14:02:39 -07001400 for (int typeInt = 0; typeInt < GrContextFactory::kContextTypeCnt; ++typeInt) {
1401 GrContextFactory::ContextType contextType = (GrContextFactory::ContextType) typeInt;
1402 ContextInfo ctxInfo = factory->getContextInfo(contextType);
bsalomonfda88072016-04-11 14:40:50 -07001403 if (contextTypeFilter && !(*contextTypeFilter)(contextType)) {
kkinnunen179a8f52015-11-20 13:32:24 -08001404 continue;
1405 }
bsalomon758586c2016-04-06 14:02:39 -07001406 // Use "native" instead of explicitly trying OpenGL and OpenGL ES. Do not use GLES on,
1407 // desktop since tests do not account for not fixing http://skbug.com/2809
1408 if (contextType == GrContextFactory::kGL_ContextType ||
1409 contextType == GrContextFactory::kGLES_ContextType) {
1410 if (contextType != GrContextFactory::kNativeGL_ContextType) {
1411 continue;
1412 }
1413 }
bsalomon8b7451a2016-05-11 06:33:06 -07001414 if (ctxInfo.grContext()) {
bsalomonf2f1c172016-04-05 12:59:06 -07001415 (*test)(reporter, ctxInfo);
kkinnunen5219fd92015-12-10 06:28:13 -08001416 }
bsalomonf2f1c172016-04-05 12:59:06 -07001417 ctxInfo = factory->getContextInfo(contextType,
bsalomon85b4b532016-04-05 11:06:27 -07001418 GrContextFactory::kEnableNVPR_ContextOptions);
bsalomon8b7451a2016-05-11 06:33:06 -07001419 if (ctxInfo.grContext()) {
bsalomonf2f1c172016-04-05 12:59:06 -07001420 (*test)(reporter, ctxInfo);
kkinnunen179a8f52015-11-20 13:32:24 -08001421 }
1422 }
1423#endif
1424}
kkinnunen179a8f52015-11-20 13:32:24 -08001425} // namespace skiatest
1426
borenet48087572015-04-02 12:16:36 -07001427#if !defined(SK_BUILD_FOR_IOS)
jcgregorio3b27ade2014-11-13 08:06:40 -08001428int main(int argc, char** argv) {
1429 SkCommandLineFlags::Parse(argc, argv);
1430 return dm_main();
1431}
1432#endif