blob: 58cb773e032a5f1bb9182dc3db4b0b08043fb8c6 [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
mtklein748ca3b2015-01-15 10:56:12 -0800107SK_DECLARE_STATIC_MUTEX(gFailuresMutex);
108static 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;
368 default:
369 break;
370 }
371
scroggoc5560be2016-02-03 09:42:42 -0800372 switch (dstAlphaType) {
373 case kOpaque_SkAlphaType:
374 folder.append("_opaque");
375 break;
376 case kPremul_SkAlphaType:
377 folder.append("_premul");
378 break;
379 case kUnpremul_SkAlphaType:
380 folder.append("_unpremul");
381 break;
382 default:
383 break;
384 }
385
msarett9e707a02015-09-01 14:57:57 -0700386 if (1.0f != scale) {
387 folder.appendf("_%.3f", scale);
388 }
389
scroggoc5560be2016-02-03 09:42:42 -0800390 CodecSrc* src = new CodecSrc(path, mode, dstColorType, dstAlphaType, scale);
msarett9e707a02015-09-01 14:57:57 -0700391 push_src("image", folder, src);
392}
393
msarett3d9d7a72015-10-21 10:27:10 -0700394static void push_android_codec_src(Path path, AndroidCodecSrc::Mode mode,
scroggoc5560be2016-02-03 09:42:42 -0800395 CodecSrc::DstColorType dstColorType, SkAlphaType dstAlphaType, int sampleSize) {
msarett3d9d7a72015-10-21 10:27:10 -0700396 SkString folder;
397 switch (mode) {
398 case AndroidCodecSrc::kFullImage_Mode:
399 folder.append("scaled_codec");
400 break;
401 case AndroidCodecSrc::kDivisor_Mode:
402 folder.append("scaled_codec_divisor");
403 break;
404 }
405
406 switch (dstColorType) {
407 case CodecSrc::kGrayscale_Always_DstColorType:
408 folder.append("_kGray8");
409 break;
410 case CodecSrc::kIndex8_Always_DstColorType:
411 folder.append("_kIndex8");
412 break;
413 default:
414 break;
415 }
416
scroggoc5560be2016-02-03 09:42:42 -0800417 switch (dstAlphaType) {
418 case kOpaque_SkAlphaType:
419 folder.append("_opaque");
420 break;
421 case kPremul_SkAlphaType:
422 folder.append("_premul");
423 break;
msarett9e9444c2016-02-03 12:39:10 -0800424 case kUnpremul_SkAlphaType:
425 folder.append("_unpremul");
426 break;
scroggoc5560be2016-02-03 09:42:42 -0800427 default:
428 break;
429 }
430
msarett3d9d7a72015-10-21 10:27:10 -0700431 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800432 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700433 }
434
scroggoc5560be2016-02-03 09:42:42 -0800435 AndroidCodecSrc* src = new AndroidCodecSrc(path, mode, dstColorType, dstAlphaType, sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700436 push_src("image", folder, src);
437}
438
msarett18976312016-03-09 14:20:58 -0800439static void push_image_gen_src(Path path, ImageGenSrc::Mode mode, SkAlphaType alphaType, bool isGpu)
440{
441 SkString folder;
442 switch (mode) {
443 case ImageGenSrc::kCodec_Mode:
444 folder.append("gen_codec");
445 break;
446 case ImageGenSrc::kPlatform_Mode:
447 folder.append("gen_platform");
448 break;
449 }
450
451 if (isGpu) {
452 folder.append("_gpu");
453 } else {
454 switch (alphaType) {
455 case kOpaque_SkAlphaType:
456 folder.append("_opaque");
457 break;
458 case kPremul_SkAlphaType:
459 folder.append("_premul");
460 break;
461 case kUnpremul_SkAlphaType:
462 folder.append("_unpremul");
463 break;
464 default:
465 break;
466 }
467 }
468
469 ImageGenSrc* src = new ImageGenSrc(path, mode, alphaType, isGpu);
470 push_src("image", folder, src);
471}
472
msarett438b2ad2015-04-09 12:43:10 -0700473static void push_codec_srcs(Path path) {
474 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
475 if (!encoded) {
mtkleinc41fd922016-03-08 09:01:39 -0800476 info("Couldn't read %s.", path.c_str());
msarett438b2ad2015-04-09 12:43:10 -0700477 return;
478 }
479 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
halcanary96fcdcc2015-08-27 07:41:13 -0700480 if (nullptr == codec.get()) {
mtkleinc41fd922016-03-08 09:01:39 -0800481 info("Couldn't create codec for %s.", path.c_str());
msarett438b2ad2015-04-09 12:43:10 -0700482 return;
483 }
484
msarett36c37962015-09-02 13:20:52 -0700485 // Native Scales
emmaleer8f4ba762015-08-14 07:44:46 -0700486 // 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 -0700487 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 -0700488
msarett91c22b22016-02-22 12:27:46 -0800489 SkTArray<CodecSrc::Mode> nativeModes;
490 nativeModes.push_back(CodecSrc::kCodec_Mode);
491 nativeModes.push_back(CodecSrc::kCodecZeroInit_Mode);
msarett91c22b22016-02-22 12:27:46 -0800492 switch (codec->getEncodedFormat()) {
493 case SkEncodedFormat::kJPEG_SkEncodedFormat:
494 nativeModes.push_back(CodecSrc::kScanline_Mode);
495 nativeModes.push_back(CodecSrc::kStripe_Mode);
496 nativeModes.push_back(CodecSrc::kCroppedScanline_Mode);
497 break;
498 case SkEncodedFormat::kWEBP_SkEncodedFormat:
499 nativeModes.push_back(CodecSrc::kSubset_Mode);
500 break;
yujieqin7a307df2016-03-11 07:32:33 -0800501 case SkEncodedFormat::kDNG_SkEncodedFormat:
msarettb65e6042016-02-23 05:37:25 -0800502 break;
msarett91c22b22016-02-22 12:27:46 -0800503 default:
504 nativeModes.push_back(CodecSrc::kScanline_Mode);
505 nativeModes.push_back(CodecSrc::kStripe_Mode);
506 break;
507 }
msarett9e707a02015-09-01 14:57:57 -0700508
msarett91c22b22016-02-22 12:27:46 -0800509 SkTArray<CodecSrc::DstColorType> colorTypes;
510 colorTypes.push_back(CodecSrc::kGetFromCanvas_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
543 push_codec_src(path, mode, colorType, alphaType, scale);
scroggoc5560be2016-02-03 09:42:42 -0800544 }
msarett9e707a02015-09-01 14:57:57 -0700545 }
546 }
msarett0a242972015-06-11 14:27:27 -0700547 }
msarett36c37962015-09-02 13:20:52 -0700548
scroggoe4499842016-02-25 11:03:47 -0800549 if (FLAGS_simpleCodec) {
550 return;
551 }
552
halcanary6950de62015-11-07 05:29:00 -0800553 // https://bug.skia.org/4428
msarett33c76232015-11-16 13:43:40 -0800554 bool subset = false;
555 // The following image types are supported by BitmapRegionDecoder,
556 // so we will test full image decodes and subset decodes.
msarettbe8216a2015-12-04 08:00:50 -0800557 static const char* const exts[] = {
msarett3d9d7a72015-10-21 10:27:10 -0700558 "jpg", "jpeg", "png", "webp",
559 "JPG", "JPEG", "PNG", "WEBP",
560 };
msarettbe8216a2015-12-04 08:00:50 -0800561 for (const char* ext : exts) {
msarett3d9d7a72015-10-21 10:27:10 -0700562 if (path.endsWith(ext)) {
msarett33c76232015-11-16 13:43:40 -0800563 subset = true;
msarett3d9d7a72015-10-21 10:27:10 -0700564 break;
565 }
566 }
msarett33c76232015-11-16 13:43:40 -0800567
msarett3d9d7a72015-10-21 10:27:10 -0700568 const int sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
msarett36c37962015-09-02 13:20:52 -0700569
msarett3d9d7a72015-10-21 10:27:10 -0700570 for (int sampleSize : sampleSizes) {
msarett91c22b22016-02-22 12:27:46 -0800571 for (CodecSrc::DstColorType colorType : colorTypes) {
scroggoc5560be2016-02-03 09:42:42 -0800572 for (SkAlphaType alphaType : alphaModes) {
msarett91c22b22016-02-22 12:27:46 -0800573 push_android_codec_src(path, AndroidCodecSrc::kFullImage_Mode, colorType,
scroggoc5560be2016-02-03 09:42:42 -0800574 alphaType, sampleSize);
575 if (subset) {
msarett91c22b22016-02-22 12:27:46 -0800576 push_android_codec_src(path, AndroidCodecSrc::kDivisor_Mode, colorType,
scroggoc5560be2016-02-03 09:42:42 -0800577 alphaType, sampleSize);
578 }
msarett3d9d7a72015-10-21 10:27:10 -0700579 }
msarett36c37962015-09-02 13:20:52 -0700580 }
581 }
msarett18976312016-03-09 14:20:58 -0800582
583 static const char* const rawExts[] = {
584 "arw", "cr2", "dng", "nef", "nrw", "orf", "raf", "rw2", "pef", "srw",
585 "ARW", "CR2", "DNG", "NEF", "NRW", "ORF", "RAF", "RW2", "PEF", "SRW",
586 };
587
588 // There is not currently a reason to test RAW images on image generator.
589 // If we want to enable these tests, we will need to fix skbug.com/5079.
590 for (const char* ext : rawExts) {
591 if (path.endsWith(ext)) {
592 return;
593 }
594 }
595
596 // Push image generator GPU test.
brianosmanc9ced392016-03-24 07:27:43 -0700597 push_image_gen_src(path, ImageGenSrc::kCodec_Mode, codec->getInfo().alphaType(), true);
msarett18976312016-03-09 14:20:58 -0800598
599 // Push image generator CPU tests.
600 for (SkAlphaType alphaType : alphaModes) {
601 push_image_gen_src(path, ImageGenSrc::kCodec_Mode, alphaType, false);
602
603#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
604 if (kWEBP_SkEncodedFormat != codec->getEncodedFormat() &&
605 kWBMP_SkEncodedFormat != codec->getEncodedFormat() &&
606 kUnpremul_SkAlphaType != alphaType)
607 {
608 push_image_gen_src(path, ImageGenSrc::kPlatform_Mode, alphaType, false);
609 }
msarettfc0b6d12016-03-17 13:50:17 -0700610#elif defined(SK_BUILD_FOR_WIN)
611 if (kWEBP_SkEncodedFormat != codec->getEncodedFormat() &&
612 kWBMP_SkEncodedFormat != codec->getEncodedFormat())
613 {
614 push_image_gen_src(path, ImageGenSrc::kPlatform_Mode, alphaType, false);
615 }
msarett18976312016-03-09 14:20:58 -0800616#endif
617 }
msarett438b2ad2015-04-09 12:43:10 -0700618}
619
msarett5cb48852015-11-06 08:56:32 -0800620static bool brd_color_type_supported(SkBitmapRegionDecoder::Strategy strategy,
msaretta5783ae2015-09-08 15:35:32 -0700621 CodecSrc::DstColorType dstColorType) {
622 switch (strategy) {
msarett5cb48852015-11-06 08:56:32 -0800623 case SkBitmapRegionDecoder::kCanvas_Strategy:
msaretta5783ae2015-09-08 15:35:32 -0700624 if (CodecSrc::kGetFromCanvas_DstColorType == dstColorType) {
625 return true;
626 }
627 return false;
msarett5cb48852015-11-06 08:56:32 -0800628 case SkBitmapRegionDecoder::kAndroidCodec_Strategy:
msarett26ad17b2015-10-22 07:29:19 -0700629 switch (dstColorType) {
630 case CodecSrc::kGetFromCanvas_DstColorType:
631 case CodecSrc::kIndex8_Always_DstColorType:
632 case CodecSrc::kGrayscale_Always_DstColorType:
633 return true;
634 default:
635 return false;
636 }
msaretta5783ae2015-09-08 15:35:32 -0700637 default:
638 SkASSERT(false);
639 return false;
640 }
641}
642
msarett5cb48852015-11-06 08:56:32 -0800643static void push_brd_src(Path path, SkBitmapRegionDecoder::Strategy strategy,
msaretta5783ae2015-09-08 15:35:32 -0700644 CodecSrc::DstColorType dstColorType, BRDSrc::Mode mode, uint32_t sampleSize) {
645 SkString folder;
646 switch (strategy) {
msarett5cb48852015-11-06 08:56:32 -0800647 case SkBitmapRegionDecoder::kCanvas_Strategy:
msaretta5783ae2015-09-08 15:35:32 -0700648 folder.append("brd_canvas");
649 break;
msarett5cb48852015-11-06 08:56:32 -0800650 case SkBitmapRegionDecoder::kAndroidCodec_Strategy:
msarett26ad17b2015-10-22 07:29:19 -0700651 folder.append("brd_android_codec");
652 break;
msaretta5783ae2015-09-08 15:35:32 -0700653 default:
654 SkASSERT(false);
655 return;
656 }
657
658 switch (mode) {
659 case BRDSrc::kFullImage_Mode:
660 break;
661 case BRDSrc::kDivisor_Mode:
662 folder.append("_divisor");
663 break;
664 default:
665 SkASSERT(false);
666 return;
667 }
668
669 switch (dstColorType) {
670 case CodecSrc::kGetFromCanvas_DstColorType:
671 break;
672 case CodecSrc::kIndex8_Always_DstColorType:
673 folder.append("_kIndex");
674 break;
675 case CodecSrc::kGrayscale_Always_DstColorType:
676 folder.append("_kGray");
677 break;
678 default:
679 SkASSERT(false);
680 return;
681 }
682
683 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800684 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700685 }
686
687 BRDSrc* src = new BRDSrc(path, strategy, mode, dstColorType, sampleSize);
688 push_src("image", folder, src);
689}
690
691static void push_brd_srcs(Path path) {
692
msarett5cb48852015-11-06 08:56:32 -0800693 const SkBitmapRegionDecoder::Strategy strategies[] = {
694 SkBitmapRegionDecoder::kCanvas_Strategy,
msarett5cb48852015-11-06 08:56:32 -0800695 SkBitmapRegionDecoder::kAndroidCodec_Strategy,
msaretta5783ae2015-09-08 15:35:32 -0700696 };
697
scroggo501b7342015-11-03 07:55:11 -0800698 // Test on a variety of sampleSizes, making sure to include:
699 // - 2, 4, and 8, which are natively supported by jpeg
700 // - multiples of 2 which are not divisible by 4 (analogous for 4)
701 // - larger powers of two, since BRD clients generally use powers of 2
702 // We will only produce output for the larger sizes on large images.
703 const uint32_t sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 24, 32, 64 };
msarett6efbe052015-09-11 09:01:16 -0700704
msaretta5783ae2015-09-08 15:35:32 -0700705 // We will only test to one backend (8888), but we will test all of the
706 // color types that we need to decode to on this backend.
707 const CodecSrc::DstColorType dstColorTypes[] = {
msarett26ad17b2015-10-22 07:29:19 -0700708 CodecSrc::kGetFromCanvas_DstColorType,
709 CodecSrc::kIndex8_Always_DstColorType,
710 CodecSrc::kGrayscale_Always_DstColorType,
msaretta5783ae2015-09-08 15:35:32 -0700711 };
712
713 const BRDSrc::Mode modes[] = {
msarett26ad17b2015-10-22 07:29:19 -0700714 BRDSrc::kFullImage_Mode,
715 BRDSrc::kDivisor_Mode,
msaretta5783ae2015-09-08 15:35:32 -0700716 };
717
msarett5cb48852015-11-06 08:56:32 -0800718 for (SkBitmapRegionDecoder::Strategy strategy : strategies) {
msarett6efbe052015-09-11 09:01:16 -0700719 for (uint32_t sampleSize : sampleSizes) {
msarett6efbe052015-09-11 09:01:16 -0700720 for (CodecSrc::DstColorType dstColorType : dstColorTypes) {
721 if (brd_color_type_supported(strategy, dstColorType)) {
722 for (BRDSrc::Mode mode : modes) {
msaretta5783ae2015-09-08 15:35:32 -0700723 push_brd_src(path, strategy, dstColorType, mode, sampleSize);
724 }
725 }
726 }
727 }
728 }
729}
730
731static bool brd_supported(const char* ext) {
msarett8c8f22a2015-04-01 06:58:48 -0700732 static const char* const exts[] = {
msaretta5783ae2015-09-08 15:35:32 -0700733 "jpg", "jpeg", "png", "webp",
734 "JPG", "JPEG", "PNG", "WEBP",
msarett8c8f22a2015-04-01 06:58:48 -0700735 };
736
737 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
738 if (0 == strcmp(exts[i], ext)) {
739 return true;
740 }
741 }
742 return false;
scroggo9b77ddd2015-03-19 06:03:39 -0700743}
744
scroggo86737142016-02-03 12:19:11 -0800745static bool gather_srcs() {
mtklein748ca3b2015-01-15 10:56:12 -0800746 for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) {
djsollen54416de2015-04-03 07:24:48 -0700747 push_src("gm", "", new GMSrc(r->factory()));
mtklein748ca3b2015-01-15 10:56:12 -0800748 }
halcanaryfc37ad12015-01-30 07:31:19 -0800749 for (int i = 0; i < FLAGS_skps.count(); i++) {
750 const char* path = FLAGS_skps[i];
751 if (sk_isdir(path)) {
752 SkOSFile::Iter it(path, "skp");
753 for (SkString file; it.next(&file); ) {
djsollen54416de2015-04-03 07:24:48 -0700754 push_src("skp", "", new SKPSrc(SkOSPath::Join(path, file.c_str())));
halcanaryfc37ad12015-01-30 07:31:19 -0800755 }
756 } else {
djsollen54416de2015-04-03 07:24:48 -0700757 push_src("skp", "", new SKPSrc(path));
mtklein114c3cd2015-01-15 10:15:02 -0800758 }
759 }
scroggo86737142016-02-03 12:19:11 -0800760
761 SkTArray<SkString> images;
762 if (!CollectImages(&images)) {
763 return false;
764 }
765
766 for (auto image : images) {
767 push_codec_srcs(image);
scroggoe4499842016-02-25 11:03:47 -0800768 if (FLAGS_simpleCodec) {
769 continue;
770 }
771
mtklein21eaf3b2016-02-08 12:39:59 -0800772 const char* ext = strrchr(image.c_str(), '.');
773 if (ext && brd_supported(ext+1)) {
scroggo86737142016-02-03 12:19:11 -0800774 push_brd_srcs(image);
mtklein709d2c32015-01-15 08:30:25 -0800775 }
mtklein709d2c32015-01-15 08:30:25 -0800776 }
scroggo86737142016-02-03 12:19:11 -0800777
778 return true;
mtklein709d2c32015-01-15 08:30:25 -0800779}
780
kkinnunen3e980c32015-12-23 01:33:00 -0800781static void push_sink(const SkCommandLineConfig& config, Sink* s) {
rmistry0f515bd2015-12-22 10:22:26 -0800782 SkAutoTDelete<Sink> sink(s);
kkinnunen3e980c32015-12-23 01:33:00 -0800783
mtkleine0effd62015-07-29 06:37:28 -0700784 // Try a simple Src as a canary. If it fails, skip this sink.
mtklein748ca3b2015-01-15 10:56:12 -0800785 struct : public Src {
mtkleine0effd62015-07-29 06:37:28 -0700786 Error draw(SkCanvas* c) const override {
787 c->drawRect(SkRect::MakeWH(1,1), SkPaint());
788 return "";
789 }
mtklein36352bf2015-03-25 18:17:31 -0700790 SkISize size() const override { return SkISize::Make(16, 16); }
mtkleine0effd62015-07-29 06:37:28 -0700791 Name name() const override { return "justOneRect"; }
792 } justOneRect;
mtklein114c3cd2015-01-15 10:15:02 -0800793
mtklein748ca3b2015-01-15 10:56:12 -0800794 SkBitmap bitmap;
795 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800796 SkString log;
mtkleine0effd62015-07-29 06:37:28 -0700797 Error err = sink->draw(justOneRect, &bitmap, &stream, &log);
mtklein4089ef72015-03-05 08:40:28 -0800798 if (err.isFatal()) {
mtkleinc41fd922016-03-08 09:01:39 -0800799 info("Could not run %s: %s\n", config.getTag().c_str(), err.c_str());
mtklein05641a52015-04-21 10:49:13 -0700800 exit(1);
mtklein114c3cd2015-01-15 10:15:02 -0800801 }
802
mtkleine0effd62015-07-29 06:37:28 -0700803 TaggedSink& ts = gSinks.push_back();
mtklein18300a32016-03-16 13:53:35 -0700804 ts.reset(sink.release());
kkinnunen3e980c32015-12-23 01:33:00 -0800805 ts.tag = config.getTag();
mtklein748ca3b2015-01-15 10:56:12 -0800806}
807
808static bool gpu_supported() {
809#if SK_SUPPORT_GPU
810 return FLAGS_gpu;
811#else
812 return false;
813#endif
814}
kkinnunen9ebc3f02015-12-21 23:48:13 -0800815
kkinnunen3e980c32015-12-23 01:33:00 -0800816static Sink* create_sink(const SkCommandLineConfig* config) {
817#if SK_SUPPORT_GPU
818 if (gpu_supported()) {
819 if (const SkCommandLineConfigGpu* gpuConfig = config->asConfigGpu()) {
bsalomon85b4b532016-04-05 11:06:27 -0700820 GrContextFactory::ContextType contextType = gpuConfig->getContextType();
821 GrContextFactory::ContextOptions contextOptions =
822 GrContextFactory::kNone_ContextOptions;
kkinnunen3e980c32015-12-23 01:33:00 -0800823 if (gpuConfig->getUseNVPR()) {
bsalomon85b4b532016-04-05 11:06:27 -0700824 contextOptions = static_cast<GrContextFactory::ContextOptions>(
825 contextOptions | GrContextFactory::kEnableNVPR_ContextOptions);
kkinnunen3e980c32015-12-23 01:33:00 -0800826 }
brianosman898235c2016-04-06 07:38:23 -0700827 if (SkColorAndProfileAreGammaCorrect(gpuConfig->getColorType(),
828 gpuConfig->getProfileType())) {
bsalomon85b4b532016-04-05 11:06:27 -0700829 contextOptions = static_cast<GrContextFactory::ContextOptions>(
830 contextOptions | GrContextFactory::kRequireSRGBSupport_ContextOptions);
brianosman61d3b082016-03-30 11:19:36 -0700831 }
kkinnunen3e980c32015-12-23 01:33:00 -0800832 GrContextFactory testFactory;
833 if (!testFactory.get(contextType, contextOptions)) {
mtkleinc41fd922016-03-08 09:01:39 -0800834 info("WARNING: can not create GPU context for config '%s'. "
835 "GM tests will be skipped.\n", gpuConfig->getTag().c_str());
kkinnunen3e980c32015-12-23 01:33:00 -0800836 return nullptr;
837 }
838 return new GPUSink(contextType, contextOptions, gpuConfig->getSamples(),
brianosmand93c1202016-03-10 07:49:08 -0800839 gpuConfig->getUseDIText(), gpuConfig->getColorType(),
840 gpuConfig->getProfileType(), FLAGS_gpu_threading);
kkinnunen3e980c32015-12-23 01:33:00 -0800841 }
842 }
843#endif
844
845#define SINK(t, sink, ...) if (config->getBackend().equals(t)) { return new sink(__VA_ARGS__); }
846
tomhudsoneebc39a2015-02-23 12:18:05 -0800847#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
848 SINK("hwui", HWUISink);
849#endif
850
mtklein748ca3b2015-01-15 10:56:12 -0800851 if (FLAGS_cpu) {
852 SINK("565", RasterSink, kRGB_565_SkColorType);
853 SINK("8888", RasterSink, kN32_SkColorType);
mtklein27c3fdd2016-02-26 14:43:21 -0800854 SINK("srgb", RasterSink, kN32_SkColorType, kSRGB_SkColorProfileType);
855 SINK("f16", RasterSink, kRGBA_F16_SkColorType);
halcanary94b87bd2016-04-04 06:12:15 -0700856 SINK("pdf", PDFSink);
mtklein9c3f17d2015-01-28 11:35:18 -0800857 SINK("skp", SKPSink);
mtklein8a4527e2015-01-31 20:00:58 -0800858 SINK("svg", SVGSink);
859 SINK("null", NullSink);
halcanary47ef4d52015-03-03 09:13:09 -0800860 SINK("xps", XPSSink);
mtklein748ca3b2015-01-15 10:56:12 -0800861 }
862#undef SINK
halcanary96fcdcc2015-08-27 07:41:13 -0700863 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800864}
865
kkinnunen3e980c32015-12-23 01:33:00 -0800866static Sink* create_via(const SkString& tag, Sink* wrapped) {
867#define VIA(t, via, ...) if (tag.equals(t)) { return new via(__VA_ARGS__); }
halcanary96fcdcc2015-08-27 07:41:13 -0700868 VIA("twice", ViaTwice, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700869 VIA("serialize", ViaSerialization, wrapped);
mtklein4a34ecb2016-01-08 10:19:35 -0800870 VIA("pic", ViaPicture, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700871 VIA("2ndpic", ViaSecondPicture, wrapped);
mtkleind31c13d2015-05-05 12:59:56 -0700872 VIA("sp", ViaSingletonPictures, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700873 VIA("tiles", ViaTiles, 256, 256, nullptr, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800874 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800875
mtkleind603b222015-02-17 11:13:33 -0800876 if (FLAGS_matrix.count() == 4) {
mtklein748ca3b2015-01-15 10:56:12 -0800877 SkMatrix m;
mtkleind603b222015-02-17 11:13:33 -0800878 m.reset();
879 m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
880 m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
881 m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
882 m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
883 VIA("matrix", ViaMatrix, m, wrapped);
884 VIA("upright", ViaUpright, m, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800885 }
tomhudson64de1e12015-03-05 08:01:07 -0800886
887#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
888 VIA("androidsdk", ViaAndroidSDK, wrapped);
889#endif
890
mtklein748ca3b2015-01-15 10:56:12 -0800891#undef VIA
halcanary96fcdcc2015-08-27 07:41:13 -0700892 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800893}
894
mtklein748ca3b2015-01-15 10:56:12 -0800895static void gather_sinks() {
kkinnunen3e980c32015-12-23 01:33:00 -0800896 SkCommandLineConfigArray configs;
897 ParseConfigs(FLAGS_config, &configs);
898 for (int i = 0; i < configs.count(); i++) {
899 const SkCommandLineConfig& config = *configs[i];
900 Sink* sink = create_sink(&config);
901 if (sink == nullptr) {
mtkleinc41fd922016-03-08 09:01:39 -0800902 info("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
903 config.getTag().c_str());
kkinnunen3e980c32015-12-23 01:33:00 -0800904 continue;
905 }
mtklein748ca3b2015-01-15 10:56:12 -0800906
kkinnunen3e980c32015-12-23 01:33:00 -0800907 const SkTArray<SkString>& parts = config.getViaParts();
908 for (int j = parts.count(); j-- > 0;) {
909 const SkString& part = parts[j];
910 Sink* next = create_via(part, sink);
halcanary96fcdcc2015-08-27 07:41:13 -0700911 if (next == nullptr) {
mtkleinc41fd922016-03-08 09:01:39 -0800912 info("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
913 part.c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800914 delete sink;
halcanary96fcdcc2015-08-27 07:41:13 -0700915 sink = nullptr;
mtklein748ca3b2015-01-15 10:56:12 -0800916 break;
917 }
918 sink = next;
919 }
920 if (sink) {
921 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800922 }
923 }
924}
mtklein709d2c32015-01-15 08:30:25 -0800925
mtkleina5114d72015-08-24 13:27:01 -0700926static bool dump_png(SkBitmap bitmap, const char* path, const char* md5) {
927 const int w = bitmap.width(),
928 h = bitmap.height();
929
brianosman3c579dc2016-04-19 09:18:11 -0700930 sk_sp<SkData> encodedBitmap = sk_tools::encode_bitmap_for_png(bitmap);
931 if (encodedBitmap.get() == nullptr) {
932 return false;
mtkleina5114d72015-08-24 13:27:01 -0700933 }
brianosman3c579dc2016-04-19 09:18:11 -0700934 uint32_t* rgba = static_cast<uint32_t*>(encodedBitmap.get()->writable_data());
mtkleina5114d72015-08-24 13:27:01 -0700935
936 // We don't need bitmap anymore. Might as well drop our ref.
mtkleinfccb77d2015-08-24 14:13:29 -0700937 bitmap.reset();
mtkleina5114d72015-08-24 13:27:01 -0700938
mtkleinc64137c2015-08-25 10:56:08 -0700939 FILE* f = fopen(path, "wb");
mtkleina5114d72015-08-24 13:27:01 -0700940 if (!f) { return false; }
941
942 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
943 if (!png) {
944 fclose(f);
945 return false;
946 }
947
948 png_infop info = png_create_info_struct(png);
949 if (!info) {
950 png_destroy_write_struct(&png, &info);
951 fclose(f);
952 return false;
953 }
954
mtkleind69ece62015-09-10 10:37:44 -0700955 SkString description;
956 description.append("Key: ");
957 for (int i = 0; i < FLAGS_key.count(); i++) {
958 description.appendf("%s ", FLAGS_key[i]);
959 }
960 description.append("Properties: ");
961 for (int i = 0; i < FLAGS_properties.count(); i++) {
962 description.appendf("%s ", FLAGS_properties[i]);
963 }
964 description.appendf("MD5: %s", md5);
965
mtkleina5114d72015-08-24 13:27:01 -0700966 png_text text[2];
967 text[0].key = (png_charp)"Author";
968 text[0].text = (png_charp)"DM dump_png()";
969 text[0].compression = PNG_TEXT_COMPRESSION_NONE;
970 text[1].key = (png_charp)"Description";
mtkleind69ece62015-09-10 10:37:44 -0700971 text[1].text = (png_charp)description.c_str();
mtkleina5114d72015-08-24 13:27:01 -0700972 text[1].compression = PNG_TEXT_COMPRESSION_NONE;
973 png_set_text(png, info, text, 2);
974
975 png_init_io(png, f);
976 png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8,
977 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
978 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
979 png_write_info(png, info);
980 for (int j = 0; j < h; j++) {
brianosman3c579dc2016-04-19 09:18:11 -0700981 png_bytep row = (png_bytep)(rgba + w*j);
mtkleina5114d72015-08-24 13:27:01 -0700982 png_write_rows(png, &row, 1);
983 }
984 png_write_end(png, info);
985
986 png_destroy_write_struct(&png, &info);
987 fclose(f);
988 return true;
989}
990
mtkleina2ef6422015-01-15 13:44:22 -0800991static bool match(const char* needle, const char* haystack) {
halcanary96fcdcc2015-08-27 07:41:13 -0700992 return 0 == strcmp("_", needle) || nullptr != strstr(haystack, needle);
mtkleina2ef6422015-01-15 13:44:22 -0800993}
994
mtklein3eed7dd2016-02-24 19:07:07 -0800995static bool is_blacklisted(const char* sink, const char* src,
996 const char* srcOptions, const char* name) {
mtklein0d243ff2015-04-03 07:51:00 -0700997 for (int i = 0; i < FLAGS_blacklist.count() - 3; i += 4) {
mtkleina2ef6422015-01-15 13:44:22 -0800998 if (match(FLAGS_blacklist[i+0], sink) &&
djsollen54416de2015-04-03 07:24:48 -0700999 match(FLAGS_blacklist[i+1], src) &&
1000 match(FLAGS_blacklist[i+2], srcOptions) &&
1001 match(FLAGS_blacklist[i+3], name)) {
mtklein3eed7dd2016-02-24 19:07:07 -08001002 return true;
mtkleina2ef6422015-01-15 13:44:22 -08001003 }
1004 }
mtklein3eed7dd2016-02-24 19:07:07 -08001005 return false;
mtkleina2ef6422015-01-15 13:44:22 -08001006}
1007
mtkleincd50bca2016-01-05 06:20:20 -08001008// Even when a Task Sink reports to be non-threadsafe (e.g. GPU), we know things like
1009// .png encoding are definitely thread safe. This lets us offload that work to CPU threads.
1010static SkTaskGroup gDefinitelyThreadSafeWork;
1011
mtklein748ca3b2015-01-15 10:56:12 -08001012// The finest-grained unit of work we can run: draw a single Src into a single Sink,
1013// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
1014struct Task {
mtkleine0effd62015-07-29 06:37:28 -07001015 Task(const TaggedSrc& src, const TaggedSink& sink) : src(src), sink(sink) {}
1016 const TaggedSrc& src;
1017 const TaggedSink& sink;
mtklein748ca3b2015-01-15 10:56:12 -08001018
mtklein21eaf3b2016-02-08 12:39:59 -08001019 static void Run(const Task& task) {
1020 SkString name = task.src->name();
mtkleine0effd62015-07-29 06:37:28 -07001021
mtkleinb9eb4ac2015-02-02 18:26:03 -08001022 SkString log;
mtklein3eed7dd2016-02-24 19:07:07 -08001023 if (!FLAGS_dryRun) {
mtklein748ca3b2015-01-15 10:56:12 -08001024 SkBitmap bitmap;
1025 SkDynamicMemoryWStream stream;
mtklein3eed7dd2016-02-24 19:07:07 -08001026 start(task.sink.tag.c_str(), task.src.tag.c_str(),
1027 task.src.options.c_str(), name.c_str());
mtklein21eaf3b2016-02-08 12:39:59 -08001028 Error err = task.sink->draw(*task.src, &bitmap, &stream, &log);
mtkleinb3b13b72016-03-07 13:20:52 -08001029 if (!log.isEmpty()) {
mtkleinc41fd922016-03-08 09:01:39 -08001030 info("%s %s %s %s:\n%s\n", task.sink.tag.c_str()
1031 , task.src.tag.c_str()
1032 , task.src.options.c_str()
1033 , name.c_str()
1034 , log.c_str());
mtkleinb3b13b72016-03-07 13:20:52 -08001035 }
mtklein748ca3b2015-01-15 10:56:12 -08001036 if (!err.isEmpty()) {
mtklein4089ef72015-03-05 08:40:28 -08001037 if (err.isFatal()) {
djsollen54416de2015-04-03 07:24:48 -07001038 fail(SkStringPrintf("%s %s %s %s: %s",
mtklein21eaf3b2016-02-08 12:39:59 -08001039 task.sink.tag.c_str(),
1040 task.src.tag.c_str(),
1041 task.src.options.c_str(),
mtklein4089ef72015-03-05 08:40:28 -08001042 name.c_str(),
1043 err.c_str()));
mtkleinb37cb412015-03-18 05:27:14 -07001044 } else {
mtklein3eed7dd2016-02-24 19:07:07 -08001045 done(task.sink.tag.c_str(), task.src.tag.c_str(),
1046 task.src.options.c_str(), name.c_str());
mtkleinba6ada72016-01-21 09:39:35 -08001047 return;
mtklein4089ef72015-03-05 08:40:28 -08001048 }
mtklein748ca3b2015-01-15 10:56:12 -08001049 }
mtklein62bd1a62015-01-27 14:46:26 -08001050
mtkleincd50bca2016-01-05 06:20:20 -08001051 // We're likely switching threads here, so we must capture by value, [=] or [foo,bar].
1052 SkStreamAsset* data = stream.detachAsStream();
1053 gDefinitelyThreadSafeWork.add([task,name,bitmap,data]{
1054 SkAutoTDelete<SkStreamAsset> ownedData(data);
1055
1056 // Why doesn't the copy constructor do this when we have pre-locked pixels?
1057 bitmap.lockPixels();
1058
1059 SkString md5;
1060 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
1061 SkMD5 hash;
1062 if (data->getLength()) {
1063 hash.writeStream(data, data->getLength());
1064 data->rewind();
mtklein38408462015-07-08 07:25:27 -07001065 } else {
mtkleincd50bca2016-01-05 06:20:20 -08001066 // If we're BGRA (Linux, Windows), swizzle over to RGBA (Mac, Android).
1067 // This helps eliminate multiple 0-pixel-diff hashes on gold.skia.org.
1068 // (Android's general slow speed breaks the tie arbitrarily in RGBA's favor.)
1069 // We might consider promoting 565 to RGBA too.
1070 if (bitmap.colorType() == kBGRA_8888_SkColorType) {
1071 SkBitmap swizzle;
1072 SkAssertResult(bitmap.copyTo(&swizzle, kRGBA_8888_SkColorType));
1073 hash.write(swizzle.getPixels(), swizzle.getSize());
1074 } else {
1075 hash.write(bitmap.getPixels(), bitmap.getSize());
1076 }
1077 }
1078 SkMD5::Digest digest;
1079 hash.finish(digest);
1080 for (int i = 0; i < 16; i++) {
1081 md5.appendf("%02x", digest.data[i]);
mtklein38408462015-07-08 07:25:27 -07001082 }
mtklein62bd1a62015-01-27 14:46:26 -08001083 }
mtklein62bd1a62015-01-27 14:46:26 -08001084
mtkleincd50bca2016-01-05 06:20:20 -08001085 if (!FLAGS_readPath.isEmpty() &&
mtklein3eed7dd2016-02-24 19:07:07 -08001086 !gGold.contains(Gold(task.sink.tag, task.src.tag,
1087 task.src.options, name, md5))) {
mtkleincd50bca2016-01-05 06:20:20 -08001088 fail(SkStringPrintf("%s not found for %s %s %s %s in %s",
1089 md5.c_str(),
mtklein21eaf3b2016-02-08 12:39:59 -08001090 task.sink.tag.c_str(),
1091 task.src.tag.c_str(),
1092 task.src.options.c_str(),
mtkleincd50bca2016-01-05 06:20:20 -08001093 name.c_str(),
1094 FLAGS_readPath[0]));
mtklein748ca3b2015-01-15 10:56:12 -08001095 }
mtkleincd50bca2016-01-05 06:20:20 -08001096
1097 if (!FLAGS_writePath.isEmpty()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001098 const char* ext = task.sink->fileExtension();
mtkleincd50bca2016-01-05 06:20:20 -08001099 if (data->getLength()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001100 WriteToDisk(task, md5, ext, data, data->getLength(), nullptr);
mtkleincd50bca2016-01-05 06:20:20 -08001101 SkASSERT(bitmap.drawsNothing());
1102 } else if (!bitmap.drawsNothing()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001103 WriteToDisk(task, md5, ext, nullptr, 0, &bitmap);
mtkleincd50bca2016-01-05 06:20:20 -08001104 }
1105 }
1106 });
mtklein748ca3b2015-01-15 10:56:12 -08001107 }
mtklein3eed7dd2016-02-24 19:07:07 -08001108 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 -08001109 }
1110
1111 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -08001112 SkString md5,
1113 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -08001114 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -08001115 const SkBitmap* bitmap) {
mtklein409d4702016-02-29 07:38:01 -08001116 bool gammaCorrect = false;
1117 if (bitmap) {
brianosman898235c2016-04-06 07:38:23 -07001118 gammaCorrect = SkImageInfoIsGammaCorrect(bitmap->info());
mtklein409d4702016-02-29 07:38:01 -08001119 }
1120
mtklein748ca3b2015-01-15 10:56:12 -08001121 JsonWriter::BitmapResult result;
djsollen54416de2015-04-03 07:24:48 -07001122 result.name = task.src->name();
mtklein3eed7dd2016-02-24 19:07:07 -08001123 result.config = task.sink.tag;
djsollen54416de2015-04-03 07:24:48 -07001124 result.sourceType = task.src.tag;
1125 result.sourceOptions = task.src.options;
1126 result.ext = ext;
mtklein409d4702016-02-29 07:38:01 -08001127 result.gammaCorrect = gammaCorrect;
djsollen54416de2015-04-03 07:24:48 -07001128 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -08001129 JsonWriter::AddBitmapResult(result);
1130
mtkleinb0531a72015-04-07 13:38:48 -07001131 // If an MD5 is uninteresting, we want it noted in the JSON file,
1132 // but don't want to dump it out as a .png (or whatever ext is).
1133 if (gUninterestingHashes.contains(md5)) {
1134 return;
1135 }
1136
mtklein748ca3b2015-01-15 10:56:12 -08001137 const char* dir = FLAGS_writePath[0];
1138 if (0 == strcmp(dir, "@")) { // Needed for iOS.
1139 dir = FLAGS_resourcePath[0];
1140 }
1141 sk_mkdir(dir);
1142
1143 SkString path;
1144 if (FLAGS_nameByHash) {
1145 path = SkOSPath::Join(dir, result.md5.c_str());
1146 path.append(".");
1147 path.append(ext);
1148 if (sk_exists(path.c_str())) {
1149 return; // Content-addressed. If it exists already, we're done.
1150 }
1151 } else {
kkinnunen3e980c32015-12-23 01:33:00 -08001152 path = SkOSPath::Join(dir, task.sink.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001153 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001154 path = SkOSPath::Join(path.c_str(), task.src.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001155 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001156 if (strcmp(task.src.options.c_str(), "") != 0) {
1157 path = SkOSPath::Join(path.c_str(), task.src.options.c_str());
djsollen54416de2015-04-03 07:24:48 -07001158 sk_mkdir(path.c_str());
1159 }
mtklein748ca3b2015-01-15 10:56:12 -08001160 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
1161 path.append(".");
1162 path.append(ext);
1163 }
1164
mtklein748ca3b2015-01-15 10:56:12 -08001165 if (bitmap) {
mtkleina5114d72015-08-24 13:27:01 -07001166 if (!dump_png(*bitmap, path.c_str(), result.md5.c_str())) {
mtklein748ca3b2015-01-15 10:56:12 -08001167 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
1168 return;
1169 }
1170 } else {
mtkleina5114d72015-08-24 13:27:01 -07001171 SkFILEWStream file(path.c_str());
1172 if (!file.isValid()) {
1173 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
1174 return;
1175 }
mtklein748ca3b2015-01-15 10:56:12 -08001176 if (!file.writeStream(data, len)) {
1177 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
1178 return;
1179 }
1180 }
1181 }
1182};
1183
mtklein748ca3b2015-01-15 10:56:12 -08001184/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1185
1186// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
1187
mtklein21eaf3b2016-02-08 12:39:59 -08001188static SkTDArray<skiatest::Test> gParallelTests, gSerialTests;
mtklein748ca3b2015-01-15 10:56:12 -08001189
1190static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -08001191 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -08001192 return;
1193 }
mtklein6393c062015-04-27 08:45:01 -07001194 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r; r = r->next()) {
1195 if (!in_shard()) {
1196 continue;
1197 }
halcanary87f3ba42015-01-20 09:30:20 -08001198 // Despite its name, factory() is returning a reference to
1199 // link-time static const POD data.
1200 const skiatest::Test& test = r->factory();
1201 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -08001202 continue;
1203 }
halcanary87f3ba42015-01-20 09:30:20 -08001204 if (test.needsGpu && gpu_supported()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001205 (FLAGS_gpu_threading ? gParallelTests : gSerialTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -08001206 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein21eaf3b2016-02-08 12:39:59 -08001207 gParallelTests.push(test);
mtklein82d28432015-01-15 12:46:02 -08001208 }
mtklein748ca3b2015-01-15 10:56:12 -08001209 }
1210}
1211
mtklein21eaf3b2016-02-08 12:39:59 -08001212static void run_test(skiatest::Test test) {
halcanary87f3ba42015-01-20 09:30:20 -08001213 struct : public skiatest::Reporter {
mtklein36352bf2015-03-25 18:17:31 -07001214 void reportFailed(const skiatest::Failure& failure) override {
halcanary87f3ba42015-01-20 09:30:20 -08001215 fail(failure.toString());
1216 JsonWriter::AddTestFailure(failure);
1217 }
mtklein36352bf2015-03-25 18:17:31 -07001218 bool allowExtendedTest() const override {
halcanary87f3ba42015-01-20 09:30:20 -08001219 return FLAGS_pathOpsExtended;
1220 }
mtklein36352bf2015-03-25 18:17:31 -07001221 bool verbose() const override { return FLAGS_veryVerbose; }
halcanary87f3ba42015-01-20 09:30:20 -08001222 } reporter;
djsollen824996a2015-06-12 12:06:22 -07001223
mtklein3eed7dd2016-02-24 19:07:07 -08001224 if (!FLAGS_dryRun && !is_blacklisted("_", "tests", "_", test.name)) {
mtklein21eaf3b2016-02-08 12:39:59 -08001225 start("unit", "test", "", test.name);
mtklein55e88b22015-01-21 15:50:13 -08001226 GrContextFactory factory;
mtklein21eaf3b2016-02-08 12:39:59 -08001227 test.proc(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -08001228 }
mtklein3eed7dd2016-02-24 19:07:07 -08001229 done("unit", "test", "", test.name);
mtklein748ca3b2015-01-15 10:56:12 -08001230}
1231
1232/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1233
mtkleinafae30a2016-02-24 12:28:32 -08001234DEFINE_int32(status_sec, 15, "Print status this often (and if we crash).");
1235
1236SkThread* start_status_thread() {
1237 auto thread = new SkThread([] (void*) {
1238 for (;;) {
1239 print_status();
1240 #if defined(SK_BUILD_FOR_WIN)
1241 Sleep(FLAGS_status_sec * 1000);
1242 #else
1243 sleep(FLAGS_status_sec);
1244 #endif
mtklein2e1c47e2015-03-12 07:16:56 -07001245 }
mtkleinafae30a2016-02-24 12:28:32 -08001246 });
1247 thread->start();
1248 return thread;
mtkleinde6fc2b2015-03-12 06:28:54 -07001249}
1250
caryclark83ca6282015-06-10 09:31:09 -07001251#define PORTABLE_FONT_PREFIX "Toy Liberation "
1252
1253static SkTypeface* create_from_name(const char familyName[], SkTypeface::Style style) {
1254 if (familyName && strlen(familyName) > sizeof(PORTABLE_FONT_PREFIX)
1255 && !strncmp(familyName, PORTABLE_FONT_PREFIX, sizeof(PORTABLE_FONT_PREFIX) - 1)) {
caryclark1818acb2015-07-24 12:09:25 -07001256 return sk_tool_utils::create_portable_typeface(familyName, style);
caryclark83ca6282015-06-10 09:31:09 -07001257 }
halcanary96fcdcc2015-08-27 07:41:13 -07001258 return nullptr;
caryclark83ca6282015-06-10 09:31:09 -07001259}
1260
1261#undef PORTABLE_FONT_PREFIX
1262
1263extern SkTypeface* (*gCreateTypefaceDelegate)(const char [], SkTypeface::Style );
1264
jcgregorio3b27ade2014-11-13 08:06:40 -08001265int dm_main();
caryclark17f0b6d2014-07-22 10:15:34 -07001266int dm_main() {
mtklein246ba3a2016-02-23 10:39:36 -08001267 setup_crash_handler();
mtkleinafae30a2016-02-24 12:28:32 -08001268
mtkleina483da32016-03-17 12:53:36 -07001269 if (FLAGS_verbose) {
1270 gVLog = stderr;
1271 } else if (!FLAGS_writePath.isEmpty()) {
mtklein51c8cfc2016-03-11 12:59:09 -08001272 sk_mkdir(FLAGS_writePath[0]);
1273 gVLog = freopen(SkOSPath::Join(FLAGS_writePath[0], "verbose.log").c_str(), "w", stderr);
mtkleinc41fd922016-03-08 09:01:39 -08001274 }
1275
mtklein5286f022016-01-22 08:18:14 -08001276 JsonWriter::DumpJson(); // It's handy for the bots to assume this is ~never missing.
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +00001277 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -07001278 SkTaskGroup::Enabler enabled(FLAGS_threads);
caryclark83ca6282015-06-10 09:31:09 -07001279 gCreateTypefaceDelegate = &create_from_name;
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +00001280
halcanary7d571242016-02-24 17:59:16 -08001281 {
1282 SkString testResourcePath = GetResourcePath("color_wheel.png");
1283 SkFILEStream testResource(testResourcePath.c_str());
1284 if (!testResource.isValid()) {
mtkleinc41fd922016-03-08 09:01:39 -08001285 info("Some resources are missing. Do you need to set --resourcePath?\n");
halcanary7d571242016-02-24 17:59:16 -08001286 }
1287 }
mtklein62bd1a62015-01-27 14:46:26 -08001288 gather_gold();
borenet09ed4802015-04-03 14:15:33 -07001289 gather_uninteresting_hashes();
mtklein62bd1a62015-01-27 14:46:26 -08001290
scroggo86737142016-02-03 12:19:11 -08001291 if (!gather_srcs()) {
1292 return 1;
1293 }
mtklein748ca3b2015-01-15 10:56:12 -08001294 gather_sinks();
1295 gather_tests();
mtklein21eaf3b2016-02-08 12:39:59 -08001296 gPending = gSrcs.count() * gSinks.count() + gParallelTests.count() + gSerialTests.count();
mtkleinc41fd922016-03-08 09:01:39 -08001297 info("%d srcs * %d sinks + %d tests == %d tasks",
1298 gSrcs.count(), gSinks.count(), gParallelTests.count() + gSerialTests.count(), gPending);
mtkleinafae30a2016-02-24 12:28:32 -08001299 SkAutoTDelete<SkThread> statusThread(start_status_thread());
mtklein748ca3b2015-01-15 10:56:12 -08001300
mtklein21eaf3b2016-02-08 12:39:59 -08001301 // Kick off as much parallel work as we can, making note of any serial work we'll need to do.
1302 SkTaskGroup parallel;
1303 SkTArray<Task> serial;
1304
1305 for (auto& sink : gSinks)
1306 for (auto& src : gSrcs) {
mtklein3eed7dd2016-02-24 19:07:07 -08001307 if (src->veto(sink->flags()) ||
1308 is_blacklisted(sink.tag.c_str(), src.tag.c_str(),
1309 src.options.c_str(), src->name().c_str())) {
mtklein15923c92016-02-29 10:14:38 -08001310 SkAutoTAcquire<SkSpinlock> lock(gMutex);
mtklein3eed7dd2016-02-24 19:07:07 -08001311 gPending--;
1312 continue;
1313 }
1314
mtklein21eaf3b2016-02-08 12:39:59 -08001315 Task task(src, sink);
1316 if (src->serial() || sink->serial()) {
1317 serial.push_back(task);
1318 } else {
1319 parallel.add([task] { Task::Run(task); });
mtklein748ca3b2015-01-15 10:56:12 -08001320 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001321 }
mtklein21eaf3b2016-02-08 12:39:59 -08001322 for (auto test : gParallelTests) {
1323 parallel.add([test] { run_test(test); });
mtklein55e88b22015-01-21 15:50:13 -08001324 }
mtklein21eaf3b2016-02-08 12:39:59 -08001325
1326 // With the parallel work running, run serial tasks and tests here on main thread.
1327 for (auto task : serial) { Task::Run(task); }
1328 for (auto test : gSerialTests) { run_test(test); }
1329
1330 // Wait for any remaining parallel work to complete (including any spun off of serial tasks).
1331 parallel.wait();
mtkleincd50bca2016-01-05 06:20:20 -08001332 gDefinitelyThreadSafeWork.wait();
1333
mtkleinda884c42016-02-24 18:00:23 -08001334 // We'd better have run everything.
1335 SkASSERT(gPending == 0);
mtkleine027f172016-02-26 15:53:06 -08001336 // Make sure we've flushed all our results to disk.
1337 JsonWriter::DumpJson();
mtkleinda884c42016-02-24 18:00:23 -08001338
mtklein748ca3b2015-01-15 10:56:12 -08001339 // At this point we're back in single-threaded land.
caryclarkf53ce802015-06-15 06:48:30 -07001340 sk_tool_utils::release_portable_typefaces();
mtklein@google.comd36522d2013-10-16 13:02:15 +00001341
mtklein748ca3b2015-01-15 10:56:12 -08001342 if (gFailures.count() > 0) {
mtkleinc41fd922016-03-08 09:01:39 -08001343 info("Failures:\n");
mtklein748ca3b2015-01-15 10:56:12 -08001344 for (int i = 0; i < gFailures.count(); i++) {
mtkleinc41fd922016-03-08 09:01:39 -08001345 info("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001346 }
mtkleinc41fd922016-03-08 09:01:39 -08001347 info("%d failures\n", gFailures.count());
mtklein748ca3b2015-01-15 10:56:12 -08001348 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001349 }
mtkleinafae30a2016-02-24 12:28:32 -08001350
1351#ifdef SK_PDF_IMAGE_STATS
halcanary7a14b312015-10-01 07:28:13 -07001352 SkPDFImageDumpStats();
mtkleinafae30a2016-02-24 12:28:32 -08001353#endif // SK_PDF_IMAGE_STATS
1354
1355 print_status();
mtkleinc41fd922016-03-08 09:01:39 -08001356 info("Finished!\n");
mtklein748ca3b2015-01-15 10:56:12 -08001357 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +00001358}
jcgregorio3b27ade2014-11-13 08:06:40 -08001359
kkinnunen179a8f52015-11-20 13:32:24 -08001360// TODO: currently many GPU tests are declared outside SK_SUPPORT_GPU guards.
1361// Thus we export the empty RunWithGPUTestContexts when SK_SUPPORT_GPU=0.
1362namespace skiatest {
bsalomon758586c2016-04-06 14:02:39 -07001363
1364#if SK_SUPPORT_GPU
1365bool IsGLContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1366 return kOpenGL_GrBackend == GrContextFactory::ContextTypeBackend(type);
1367}
bsalomondc0fcd42016-04-11 14:21:33 -07001368bool IsVulkanContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1369 return kVulkan_GrBackend == GrContextFactory::ContextTypeBackend(type);
1370}
bsalomon758586c2016-04-06 14:02:39 -07001371bool IsRenderingGLContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1372 return IsGLContextType(type) && GrContextFactory::IsRenderingContext(type);
1373}
1374bool IsNullGLContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1375 return type == GrContextFactory::kNullGL_ContextType;
1376}
1377#else
1378bool IsGLContextType(int) { return false; }
bsalomondc0fcd42016-04-11 14:21:33 -07001379bool IsVulkanContextType(int) { return false; }
bsalomon758586c2016-04-06 14:02:39 -07001380bool IsRenderingGLContextType(int) { return false; }
1381bool IsNullGLContextType(int) { return false; }
1382#endif
1383
1384void RunWithGPUTestContexts(GrContextTestFn* test, GrContextTypeFilterFn* contextTypeFilter,
bsalomonf2f1c172016-04-05 12:59:06 -07001385 Reporter* reporter, GrContextFactory* factory) {
kkinnunen179a8f52015-11-20 13:32:24 -08001386#if SK_SUPPORT_GPU
kkinnunen3e980c32015-12-23 01:33:00 -08001387
bsalomon758586c2016-04-06 14:02:39 -07001388 for (int typeInt = 0; typeInt < GrContextFactory::kContextTypeCnt; ++typeInt) {
1389 GrContextFactory::ContextType contextType = (GrContextFactory::ContextType) typeInt;
1390 ContextInfo ctxInfo = factory->getContextInfo(contextType);
bsalomonfda88072016-04-11 14:40:50 -07001391 if (contextTypeFilter && !(*contextTypeFilter)(contextType)) {
kkinnunen179a8f52015-11-20 13:32:24 -08001392 continue;
1393 }
bsalomon758586c2016-04-06 14:02:39 -07001394 // Use "native" instead of explicitly trying OpenGL and OpenGL ES. Do not use GLES on,
1395 // desktop since tests do not account for not fixing http://skbug.com/2809
1396 if (contextType == GrContextFactory::kGL_ContextType ||
1397 contextType == GrContextFactory::kGLES_ContextType) {
1398 if (contextType != GrContextFactory::kNativeGL_ContextType) {
1399 continue;
1400 }
1401 }
bsalomonf2f1c172016-04-05 12:59:06 -07001402 if (ctxInfo.fGrContext) {
1403 (*test)(reporter, ctxInfo);
kkinnunen5219fd92015-12-10 06:28:13 -08001404 }
bsalomonf2f1c172016-04-05 12:59:06 -07001405 ctxInfo = factory->getContextInfo(contextType,
bsalomon85b4b532016-04-05 11:06:27 -07001406 GrContextFactory::kEnableNVPR_ContextOptions);
bsalomonf2f1c172016-04-05 12:59:06 -07001407 if (ctxInfo.fGrContext) {
1408 (*test)(reporter, ctxInfo);
kkinnunen179a8f52015-11-20 13:32:24 -08001409 }
1410 }
1411#endif
1412}
kkinnunen179a8f52015-11-20 13:32:24 -08001413} // namespace skiatest
1414
borenet48087572015-04-02 12:16:36 -07001415#if !defined(SK_BUILD_FOR_IOS)
jcgregorio3b27ade2014-11-13 08:06:40 -08001416int main(int argc, char** argv) {
1417 SkCommandLineFlags::Parse(argc, argv);
1418 return dm_main();
1419}
1420#endif