blob: 79b9e8d62bc32d3740c114f38543d9d9ac15995c [file] [log] [blame]
scroggo478652e2015-03-25 07:11:02 -07001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
jcgregorio3b27ade2014-11-13 08:06:40 -08008#include "CrashHandler.h"
mtklein748ca3b2015-01-15 10:56:12 -08009#include "DMJsonWriter.h"
10#include "DMSrcSink.h"
tomhudsoneebc39a2015-02-23 12:18:05 -080011#include "DMSrcSinkAndroid.h"
mtklein748ca3b2015-01-15 10:56:12 -080012#include "ProcStats.h"
halcanary7d571242016-02-24 17:59:16 -080013#include "Resources.h"
mtklein748ca3b2015-01-15 10:56:12 -080014#include "SkBBHFactory.h"
mtklein62bd1a62015-01-27 14:46:26 -080015#include "SkChecksum.h"
scroggocc2feb12015-08-14 08:32:46 -070016#include "SkCodec.h"
mtklein27c3fdd2016-02-26 14:43:21 -080017#include "SkColorPriv.h"
caryclark17f0b6d2014-07-22 10:15:34 -070018#include "SkCommonFlags.h"
kkinnunen3e980c32015-12-23 01:33:00 -080019#include "SkCommonFlagsConfig.h"
caryclark83ca6282015-06-10 09:31:09 -070020#include "SkFontMgr.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +000021#include "SkGraphics.h"
mtklein27c3fdd2016-02-26 14:43:21 -080022#include "SkHalf.h"
mtklein748ca3b2015-01-15 10:56:12 -080023#include "SkMD5.h"
mtklein1b249332015-07-07 12:21:21 -070024#include "SkMutex.h"
mtklein1d0f1642014-09-08 08:05:18 -070025#include "SkOSFile.h"
mtklein27c3fdd2016-02-26 14:43:21 -080026#include "SkPM4fPriv.h"
mtklein246ba3a2016-02-23 10:39:36 -080027#include "SkSpinlock.h"
mtkleina82f5622015-02-20 12:30:19 -080028#include "SkTHash.h"
mtklein406654b2014-09-03 15:34:37 -070029#include "SkTaskGroup.h"
mtkleinde6fc2b2015-03-12 06:28:54 -070030#include "SkThreadUtils.h"
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +000031#include "Test.h"
mtklein748ca3b2015-01-15 10:56:12 -080032#include "Timer.h"
caryclark83ca6282015-06-10 09:31:09 -070033#include "sk_tool_utils.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +000034
halcanary7a14b312015-10-01 07:28:13 -070035#ifdef SK_PDF_IMAGE_STATS
36extern void SkPDFImageDumpStats();
37#endif
38
mtkleina5114d72015-08-24 13:27:01 -070039#include "png.h"
40
bungeman60e0fee2015-08-26 05:15:46 -070041#include <stdlib.h>
42
scroggo27a58342015-10-28 08:56:41 -070043#ifndef SK_BUILD_FOR_WIN32
44 #include <unistd.h>
45#endif
46
djsollen54416de2015-04-03 07:24:48 -070047DEFINE_string(src, "tests gm skp image", "Source types to test.");
mtklein748ca3b2015-01-15 10:56:12 -080048DEFINE_bool(nameByHash, false,
49 "If true, write to FLAGS_writePath[0]/<hash>.png instead of "
djsollen54416de2015-04-03 07:24:48 -070050 "to FLAGS_writePath[0]/<config>/<sourceType>/<sourceOptions>/<name>.png");
mtklein748ca3b2015-01-15 10:56:12 -080051DEFINE_bool2(pathOpsExtended, x, false, "Run extended pathOps tests.");
mtkleind603b222015-02-17 11:13:33 -080052DEFINE_string(matrix, "1 0 0 1",
53 "2x2 scale+skew matrix to apply or upright when using "
54 "'matrix' or 'upright' in config.");
mtklein82d28432015-01-15 12:46:02 -080055DEFINE_bool(gpu_threading, false, "Allow GPU work to run on multiple threads?");
mtklein@google.comd36522d2013-10-16 13:02:15 +000056
mtkleina2ef6422015-01-15 13:44:22 -080057DEFINE_string(blacklist, "",
djsollen54416de2015-04-03 07:24:48 -070058 "Space-separated config/src/srcOptions/name quadruples to blacklist. '_' matches anything. E.g. \n"
59 "'--blacklist gpu skp _ _' will blacklist all SKPs drawn into the gpu config.\n"
60 "'--blacklist gpu skp _ _ 8888 gm _ aarects' will also blacklist the aarects GM on 8888.");
mtkleina2ef6422015-01-15 13:44:22 -080061
mtklein62bd1a62015-01-27 14:46:26 -080062DEFINE_string2(readPath, r, "", "If set check for equality with golden results in this directory.");
63
borenet09ed4802015-04-03 14:15:33 -070064DEFINE_string(uninterestingHashesFile, "",
65 "File containing a list of uninteresting hashes. If a result hashes to something in "
66 "this list, no image is written for that result.");
67
mtklein6393c062015-04-27 08:45:01 -070068DEFINE_int32(shards, 1, "We're splitting source data into this many shards.");
69DEFINE_int32(shard, 0, "Which shard do I run?");
scroggoe4499842016-02-25 11:03:47 -080070DEFINE_bool(simpleCodec, false, "Only decode images to native scale");
mtklein6393c062015-04-27 08:45:01 -070071
mtklein748ca3b2015-01-15 10:56:12 -080072using namespace DM;
bsalomon3724e572016-03-30 18:56:19 -070073using sk_gpu_test::GrContextFactory;
74using sk_gpu_test::GLContext;
mtklein@google.comd36522d2013-10-16 13:02:15 +000075
mtklein748ca3b2015-01-15 10:56:12 -080076/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
77
benjaminwagnerec4d4d72016-03-25 12:59:53 -070078static const double kStartMs = SkTime::GetMSecs();
mtkleinc41fd922016-03-08 09:01:39 -080079
80static FILE* gVLog;
81
82template <typename... Args>
83static void vlog(const char* fmt, Args&&... args) {
84 if (gVLog) {
85 fprintf(gVLog, "%s\t", HumanizeMs(SkTime::GetMSecs() - kStartMs).c_str());
86 fprintf(gVLog, fmt, args...);
87 fflush(gVLog);
88 }
89}
90
91template <typename... Args>
92static void info(const char* fmt, Args&&... args) {
93 vlog(fmt, args...);
94 if (!FLAGS_quiet) {
95 printf(fmt, args...);
96 }
97}
98static void info(const char* fmt) {
99 if (!FLAGS_quiet) {
100 printf("%s", fmt); // Clang warns printf(fmt) is insecure.
101 }
102}
103
mtklein748ca3b2015-01-15 10:56:12 -0800104SK_DECLARE_STATIC_MUTEX(gFailuresMutex);
105static SkTArray<SkString> gFailures;
106
mtklein3eed7dd2016-02-24 19:07:07 -0800107static void fail(const SkString& err) {
mtklein748ca3b2015-01-15 10:56:12 -0800108 SkAutoMutexAcquire lock(gFailuresMutex);
109 SkDebugf("\n\nFAILURE: %s\n\n", err.c_str());
110 gFailures.push_back(err);
halcanary9e398f72015-01-10 11:18:04 -0800111}
112
mtkleinb37cb412015-03-18 05:27:14 -0700113
mtklein246ba3a2016-02-23 10:39:36 -0800114// We use a spinlock to make locking this in a signal handler _somewhat_ safe.
mtklein15923c92016-02-29 10:14:38 -0800115static SkSpinlock gMutex;
mtklein3eed7dd2016-02-24 19:07:07 -0800116static int32_t gPending;
117static SkTArray<SkString> gRunning;
mtklein748ca3b2015-01-15 10:56:12 -0800118
mtklein3eed7dd2016-02-24 19:07:07 -0800119static void done(const char* config, const char* src, const char* srcOptions, const char* name) {
120 SkString id = SkStringPrintf("%s %s %s %s", config, src, srcOptions, name);
mtkleinc41fd922016-03-08 09:01:39 -0800121 vlog("done %s\n", id.c_str());
mtkleinafae30a2016-02-24 12:28:32 -0800122 int pending;
mtkleinb37cb412015-03-18 05:27:14 -0700123 {
mtklein15923c92016-02-29 10:14:38 -0800124 SkAutoTAcquire<SkSpinlock> lock(gMutex);
mtkleinb37cb412015-03-18 05:27:14 -0700125 for (int i = 0; i < gRunning.count(); i++) {
126 if (gRunning[i] == id) {
127 gRunning.removeShuffle(i);
128 break;
129 }
130 }
mtkleinafae30a2016-02-24 12:28:32 -0800131 pending = --gPending;
reed50bc0512015-05-19 14:13:31 -0700132 }
mtkleina17241b2015-01-23 05:48:00 -0800133 // We write our dm.json file every once in a while in case we crash.
134 // Notice this also handles the final dm.json when pending == 0.
135 if (pending % 500 == 0) {
136 JsonWriter::DumpJson();
137 }
mtklein@google.comd36522d2013-10-16 13:02:15 +0000138}
139
mtklein3eed7dd2016-02-24 19:07:07 -0800140static void start(const char* config, const char* src, const char* srcOptions, const char* name) {
141 SkString id = SkStringPrintf("%s %s %s %s", config, src, srcOptions, name);
mtkleinc41fd922016-03-08 09:01:39 -0800142 vlog("start %s\n", id.c_str());
mtklein15923c92016-02-29 10:14:38 -0800143 SkAutoTAcquire<SkSpinlock> lock(gMutex);
mtkleinb37cb412015-03-18 05:27:14 -0700144 gRunning.push_back(id);
145}
146
mtkleinafae30a2016-02-24 12:28:32 -0800147static void print_status() {
mtkleinafae30a2016-02-24 12:28:32 -0800148 int curr = sk_tools::getCurrResidentSetSizeMB(),
149 peak = sk_tools::getMaxResidentSetSizeMB();
mtkleinc41fd922016-03-08 09:01:39 -0800150 SkString elapsed = HumanizeMs(SkTime::GetMSecs() - kStartMs);
mtkleinafae30a2016-02-24 12:28:32 -0800151
mtklein15923c92016-02-29 10:14:38 -0800152 SkAutoTAcquire<SkSpinlock> lock(gMutex);
mtkleinc41fd922016-03-08 09:01:39 -0800153 info("\n%s elapsed, %d active, %d queued, %dMB RAM, %dMB peak\n",
154 elapsed.c_str(), gRunning.count(), gPending - gRunning.count(), curr, peak);
mtkleinafae30a2016-02-24 12:28:32 -0800155 for (auto& task : gRunning) {
mtkleinc41fd922016-03-08 09:01:39 -0800156 info("\t%s\n", task.c_str());
mtkleinafae30a2016-02-24 12:28:32 -0800157 }
158}
159
mtkleinf8557292016-02-29 06:35:28 -0800160// Yo dawg, I heard you like signals so I caught a signal in your
161// signal handler so you can handle signals while you handle signals.
162// Let's not get into that situation. Only print if we're the first ones to get a crash signal.
163static std::atomic<bool> in_signal_handler{false};
164
mtklein246ba3a2016-02-23 10:39:36 -0800165#if defined(SK_BUILD_FOR_WIN32)
mtkleinf8557292016-02-29 06:35:28 -0800166 static LONG WINAPI handler(EXCEPTION_POINTERS* e) {
167 static const struct {
168 const char* name;
robertphillips75467862016-03-01 14:10:23 -0800169 DWORD code;
mtkleinf8557292016-02-29 06:35:28 -0800170 } kExceptions[] = {
171 #define _(E) {#E, E}
172 _(EXCEPTION_ACCESS_VIOLATION),
173 _(EXCEPTION_BREAKPOINT),
174 _(EXCEPTION_INT_DIVIDE_BY_ZERO),
175 _(EXCEPTION_STACK_OVERFLOW),
176 // TODO: more?
177 #undef _
178 };
179
180 if (!in_signal_handler.exchange(true)) {
181 const DWORD code = e->ExceptionRecord->ExceptionCode;
mtkleinc41fd922016-03-08 09:01:39 -0800182 info("\nCaught exception %u", code);
mtkleinf8557292016-02-29 06:35:28 -0800183 for (const auto& exception : kExceptions) {
184 if (exception.code == code) {
mtkleinc41fd922016-03-08 09:01:39 -0800185 info(" %s", exception.name);
mtkleinf8557292016-02-29 06:35:28 -0800186 }
187 }
mtkleinc41fd922016-03-08 09:01:39 -0800188 info("\n");
mtkleinf8557292016-02-29 06:35:28 -0800189 print_status();
mtklein13bf3392016-03-17 10:16:12 -0700190 fflush(stdout);
mtkleinf8557292016-02-29 06:35:28 -0800191 }
192 // Execute default exception handler... hopefully, exit.
193 return EXCEPTION_EXECUTE_HANDLER;
mtklein246ba3a2016-02-23 10:39:36 -0800194 }
mtkleinf8557292016-02-29 06:35:28 -0800195 static void setup_crash_handler() { SetUnhandledExceptionFilter(handler); }
mtklein246ba3a2016-02-23 10:39:36 -0800196
mtklein819ab102016-02-29 17:02:52 -0800197#elif !defined(SK_BUILD_FOR_ANDROID)
198 #include <execinfo.h>
mtklein246ba3a2016-02-23 10:39:36 -0800199 #include <signal.h>
mtklein819ab102016-02-29 17:02:52 -0800200 #include <stdlib.h>
mtkleinf8557292016-02-29 06:35:28 -0800201
mtklein246ba3a2016-02-23 10:39:36 -0800202 static void setup_crash_handler() {
203 const int kSignals[] = { SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGSEGV };
204 for (int sig : kSignals) {
205 signal(sig, [](int sig) {
mtkleinf8557292016-02-29 06:35:28 -0800206 if (!in_signal_handler.exchange(true)) {
mtklein819ab102016-02-29 17:02:52 -0800207 SkAutoTAcquire<SkSpinlock> lock(gMutex);
mtkleinc41fd922016-03-08 09:01:39 -0800208 info("\nCaught signal %d [%s], was running:\n", sig, strsignal(sig));
mtklein819ab102016-02-29 17:02:52 -0800209 for (auto& task : gRunning) {
mtkleinc41fd922016-03-08 09:01:39 -0800210 info("\t%s\n", task.c_str());
mtklein819ab102016-02-29 17:02:52 -0800211 }
212
213 void* stack[64];
214 int count = backtrace(stack, SK_ARRAY_COUNT(stack));
215 char** symbols = backtrace_symbols(stack, count);
mtkleinc41fd922016-03-08 09:01:39 -0800216 info("\nStack trace:\n");
mtklein819ab102016-02-29 17:02:52 -0800217 for (int i = 0; i < count; i++) {
mtkleinc41fd922016-03-08 09:01:39 -0800218 info(" %s\n", symbols[i]);
mtklein819ab102016-02-29 17:02:52 -0800219 }
mtklein13bf3392016-03-17 10:16:12 -0700220 fflush(stdout);
mtkleinf8557292016-02-29 06:35:28 -0800221 }
mtklein819ab102016-02-29 17:02:52 -0800222 _Exit(sig);
mtklein246ba3a2016-02-23 10:39:36 -0800223 });
224 }
225 }
mtklein819ab102016-02-29 17:02:52 -0800226
227#else // Android
228 static void setup_crash_handler() {}
mtklein246ba3a2016-02-23 10:39:36 -0800229#endif
230
mtklein748ca3b2015-01-15 10:56:12 -0800231/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
mtklein114c3cd2015-01-15 10:15:02 -0800232
mtklein62bd1a62015-01-27 14:46:26 -0800233struct Gold : public SkString {
mtkleina82f5622015-02-20 12:30:19 -0800234 Gold() : SkString("") {}
mtklein3eed7dd2016-02-24 19:07:07 -0800235 Gold(const SkString& sink, const SkString& src,
236 const SkString& srcOptions, const SkString& name,
237 const SkString& md5)
mtklein62bd1a62015-01-27 14:46:26 -0800238 : SkString("") {
239 this->append(sink);
240 this->append(src);
djsollen54416de2015-04-03 07:24:48 -0700241 this->append(srcOptions);
mtklein62bd1a62015-01-27 14:46:26 -0800242 this->append(name);
243 this->append(md5);
mtklein62bd1a62015-01-27 14:46:26 -0800244 }
mtkleinc8d1dd42015-10-15 12:23:01 -0700245 struct Hash {
246 uint32_t operator()(const Gold& g) const {
247 return SkGoodHash()((const SkString&)g);
248 }
249 };
mtklein62bd1a62015-01-27 14:46:26 -0800250};
mtkleina82f5622015-02-20 12:30:19 -0800251static SkTHashSet<Gold, Gold::Hash> gGold;
mtklein62bd1a62015-01-27 14:46:26 -0800252
253static void add_gold(JsonWriter::BitmapResult r) {
djsollen54416de2015-04-03 07:24:48 -0700254 gGold.add(Gold(r.config, r.sourceType, r.sourceOptions, r.name, r.md5));
mtklein62bd1a62015-01-27 14:46:26 -0800255}
256
257static void gather_gold() {
258 if (!FLAGS_readPath.isEmpty()) {
259 SkString path(FLAGS_readPath[0]);
260 path.append("/dm.json");
261 if (!JsonWriter::ReadJson(path.c_str(), add_gold)) {
262 fail(SkStringPrintf("Couldn't read %s for golden results.", path.c_str()));
263 }
264 }
265}
266
267/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
268
mtklein935f1b12016-03-16 08:37:19 -0700269#if defined(SK_BUILD_FOR_WIN32)
270 static const char* kNewline = "\r\n";
271#else
272 static const char* kNewline = "\n";
273#endif
274
borenet09ed4802015-04-03 14:15:33 -0700275static SkTHashSet<SkString> gUninterestingHashes;
276
277static void gather_uninteresting_hashes() {
278 if (!FLAGS_uninterestingHashesFile.isEmpty()) {
279 SkAutoTUnref<SkData> data(SkData::NewFromFileName(FLAGS_uninterestingHashesFile[0]));
mtkleincc334b32015-09-22 11:43:53 -0700280 if (!data) {
mtkleinc41fd922016-03-08 09:01:39 -0800281 info("WARNING: unable to read uninteresting hashes from %s\n",
282 FLAGS_uninterestingHashesFile[0]);
mtkleincc334b32015-09-22 11:43:53 -0700283 return;
284 }
borenet09ed4802015-04-03 14:15:33 -0700285 SkTArray<SkString> hashes;
mtklein935f1b12016-03-16 08:37:19 -0700286 SkStrSplit((const char*)data->data(), kNewline, &hashes);
borenet09ed4802015-04-03 14:15:33 -0700287 for (const SkString& hash : hashes) {
288 gUninterestingHashes.add(hash);
289 }
mtkleinc41fd922016-03-08 09:01:39 -0800290 info("FYI: loaded %d distinct uninteresting hashes from %d lines\n",
291 gUninterestingHashes.count(), hashes.count());
borenet09ed4802015-04-03 14:15:33 -0700292 }
293}
294
295/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
296
mtkleine0effd62015-07-29 06:37:28 -0700297struct TaggedSrc : public SkAutoTDelete<Src> {
mtklein3eed7dd2016-02-24 19:07:07 -0800298 SkString tag;
299 SkString options;
mtkleine0effd62015-07-29 06:37:28 -0700300};
301
302struct TaggedSink : public SkAutoTDelete<Sink> {
kkinnunen3e980c32015-12-23 01:33:00 -0800303 SkString tag;
djsollen54416de2015-04-03 07:24:48 -0700304};
mtklein748ca3b2015-01-15 10:56:12 -0800305
306static const bool kMemcpyOK = true;
307
mtkleine0effd62015-07-29 06:37:28 -0700308static SkTArray<TaggedSrc, kMemcpyOK> gSrcs;
309static SkTArray<TaggedSink, kMemcpyOK> gSinks;
mtklein748ca3b2015-01-15 10:56:12 -0800310
mtklein6393c062015-04-27 08:45:01 -0700311static bool in_shard() {
312 static int N = 0;
313 return N++ % FLAGS_shards == FLAGS_shard;
314}
315
mtklein3eed7dd2016-02-24 19:07:07 -0800316static void push_src(const char* tag, ImplicitString options, Src* s) {
mtklein748ca3b2015-01-15 10:56:12 -0800317 SkAutoTDelete<Src> src(s);
mtklein6393c062015-04-27 08:45:01 -0700318 if (in_shard() &&
mtklein3eed7dd2016-02-24 19:07:07 -0800319 FLAGS_src.contains(tag) &&
mtklein748ca3b2015-01-15 10:56:12 -0800320 !SkCommandLineFlags::ShouldSkip(FLAGS_match, src->name().c_str())) {
mtkleine0effd62015-07-29 06:37:28 -0700321 TaggedSrc& s = gSrcs.push_back();
mtklein18300a32016-03-16 13:53:35 -0700322 s.reset(src.release());
mtklein748ca3b2015-01-15 10:56:12 -0800323 s.tag = tag;
djsollen54416de2015-04-03 07:24:48 -0700324 s.options = options;
mtklein114c3cd2015-01-15 10:15:02 -0800325 }
mtklein748ca3b2015-01-15 10:56:12 -0800326}
mtklein114c3cd2015-01-15 10:15:02 -0800327
msarett9e707a02015-09-01 14:57:57 -0700328static void push_codec_src(Path path, CodecSrc::Mode mode, CodecSrc::DstColorType dstColorType,
scroggoc5560be2016-02-03 09:42:42 -0800329 SkAlphaType dstAlphaType, float scale) {
scroggoe4499842016-02-25 11:03:47 -0800330 if (FLAGS_simpleCodec) {
331 if (mode != CodecSrc::kCodec_Mode || dstColorType != CodecSrc::kGetFromCanvas_DstColorType
332 || scale != 1.0f)
333 // Only decode in the simple case.
334 return;
335 }
msarett9e707a02015-09-01 14:57:57 -0700336 SkString folder;
337 switch (mode) {
338 case CodecSrc::kCodec_Mode:
339 folder.append("codec");
340 break;
msarettbb25b532016-01-13 09:31:39 -0800341 case CodecSrc::kCodecZeroInit_Mode:
342 folder.append("codec_zero_init");
343 break;
msarett9e707a02015-09-01 14:57:57 -0700344 case CodecSrc::kScanline_Mode:
345 folder.append("scanline");
346 break;
msarett9e707a02015-09-01 14:57:57 -0700347 case CodecSrc::kStripe_Mode:
348 folder.append("stripe");
349 break;
msarett91c22b22016-02-22 12:27:46 -0800350 case CodecSrc::kCroppedScanline_Mode:
351 folder.append("crop");
352 break;
msarett9e707a02015-09-01 14:57:57 -0700353 case CodecSrc::kSubset_Mode:
msarett36c37962015-09-02 13:20:52 -0700354 folder.append("codec_subset");
msarett9e707a02015-09-01 14:57:57 -0700355 break;
356 }
357
358 switch (dstColorType) {
359 case CodecSrc::kGrayscale_Always_DstColorType:
360 folder.append("_kGray8");
361 break;
362 case CodecSrc::kIndex8_Always_DstColorType:
363 folder.append("_kIndex8");
364 break;
365 default:
366 break;
367 }
368
scroggoc5560be2016-02-03 09:42:42 -0800369 switch (dstAlphaType) {
370 case kOpaque_SkAlphaType:
371 folder.append("_opaque");
372 break;
373 case kPremul_SkAlphaType:
374 folder.append("_premul");
375 break;
376 case kUnpremul_SkAlphaType:
377 folder.append("_unpremul");
378 break;
379 default:
380 break;
381 }
382
msarett9e707a02015-09-01 14:57:57 -0700383 if (1.0f != scale) {
384 folder.appendf("_%.3f", scale);
385 }
386
scroggoc5560be2016-02-03 09:42:42 -0800387 CodecSrc* src = new CodecSrc(path, mode, dstColorType, dstAlphaType, scale);
msarett9e707a02015-09-01 14:57:57 -0700388 push_src("image", folder, src);
389}
390
msarett3d9d7a72015-10-21 10:27:10 -0700391static void push_android_codec_src(Path path, AndroidCodecSrc::Mode mode,
scroggoc5560be2016-02-03 09:42:42 -0800392 CodecSrc::DstColorType dstColorType, SkAlphaType dstAlphaType, int sampleSize) {
msarett3d9d7a72015-10-21 10:27:10 -0700393 SkString folder;
394 switch (mode) {
395 case AndroidCodecSrc::kFullImage_Mode:
396 folder.append("scaled_codec");
397 break;
398 case AndroidCodecSrc::kDivisor_Mode:
399 folder.append("scaled_codec_divisor");
400 break;
401 }
402
403 switch (dstColorType) {
404 case CodecSrc::kGrayscale_Always_DstColorType:
405 folder.append("_kGray8");
406 break;
407 case CodecSrc::kIndex8_Always_DstColorType:
408 folder.append("_kIndex8");
409 break;
410 default:
411 break;
412 }
413
scroggoc5560be2016-02-03 09:42:42 -0800414 switch (dstAlphaType) {
415 case kOpaque_SkAlphaType:
416 folder.append("_opaque");
417 break;
418 case kPremul_SkAlphaType:
419 folder.append("_premul");
420 break;
msarett9e9444c2016-02-03 12:39:10 -0800421 case kUnpremul_SkAlphaType:
422 folder.append("_unpremul");
423 break;
scroggoc5560be2016-02-03 09:42:42 -0800424 default:
425 break;
426 }
427
msarett3d9d7a72015-10-21 10:27:10 -0700428 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800429 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700430 }
431
scroggoc5560be2016-02-03 09:42:42 -0800432 AndroidCodecSrc* src = new AndroidCodecSrc(path, mode, dstColorType, dstAlphaType, sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700433 push_src("image", folder, src);
434}
435
msarett18976312016-03-09 14:20:58 -0800436static void push_image_gen_src(Path path, ImageGenSrc::Mode mode, SkAlphaType alphaType, bool isGpu)
437{
438 SkString folder;
439 switch (mode) {
440 case ImageGenSrc::kCodec_Mode:
441 folder.append("gen_codec");
442 break;
443 case ImageGenSrc::kPlatform_Mode:
444 folder.append("gen_platform");
445 break;
446 }
447
448 if (isGpu) {
449 folder.append("_gpu");
450 } else {
451 switch (alphaType) {
452 case kOpaque_SkAlphaType:
453 folder.append("_opaque");
454 break;
455 case kPremul_SkAlphaType:
456 folder.append("_premul");
457 break;
458 case kUnpremul_SkAlphaType:
459 folder.append("_unpremul");
460 break;
461 default:
462 break;
463 }
464 }
465
466 ImageGenSrc* src = new ImageGenSrc(path, mode, alphaType, isGpu);
467 push_src("image", folder, src);
468}
469
msarett438b2ad2015-04-09 12:43:10 -0700470static void push_codec_srcs(Path path) {
471 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
472 if (!encoded) {
mtkleinc41fd922016-03-08 09:01:39 -0800473 info("Couldn't read %s.", path.c_str());
msarett438b2ad2015-04-09 12:43:10 -0700474 return;
475 }
476 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
halcanary96fcdcc2015-08-27 07:41:13 -0700477 if (nullptr == codec.get()) {
mtkleinc41fd922016-03-08 09:01:39 -0800478 info("Couldn't create codec for %s.", path.c_str());
msarett438b2ad2015-04-09 12:43:10 -0700479 return;
480 }
481
msarett36c37962015-09-02 13:20:52 -0700482 // Native Scales
emmaleer8f4ba762015-08-14 07:44:46 -0700483 // 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 -0700484 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 -0700485
msarett91c22b22016-02-22 12:27:46 -0800486 SkTArray<CodecSrc::Mode> nativeModes;
487 nativeModes.push_back(CodecSrc::kCodec_Mode);
488 nativeModes.push_back(CodecSrc::kCodecZeroInit_Mode);
msarett91c22b22016-02-22 12:27:46 -0800489 switch (codec->getEncodedFormat()) {
490 case SkEncodedFormat::kJPEG_SkEncodedFormat:
491 nativeModes.push_back(CodecSrc::kScanline_Mode);
492 nativeModes.push_back(CodecSrc::kStripe_Mode);
493 nativeModes.push_back(CodecSrc::kCroppedScanline_Mode);
494 break;
495 case SkEncodedFormat::kWEBP_SkEncodedFormat:
496 nativeModes.push_back(CodecSrc::kSubset_Mode);
497 break;
yujieqin7a307df2016-03-11 07:32:33 -0800498 case SkEncodedFormat::kDNG_SkEncodedFormat:
msarettb65e6042016-02-23 05:37:25 -0800499 break;
msarett91c22b22016-02-22 12:27:46 -0800500 default:
501 nativeModes.push_back(CodecSrc::kScanline_Mode);
502 nativeModes.push_back(CodecSrc::kStripe_Mode);
503 break;
504 }
msarett9e707a02015-09-01 14:57:57 -0700505
msarett91c22b22016-02-22 12:27:46 -0800506 SkTArray<CodecSrc::DstColorType> colorTypes;
507 colorTypes.push_back(CodecSrc::kGetFromCanvas_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700508 switch (codec->getInfo().colorType()) {
509 case kGray_8_SkColorType:
msarett91c22b22016-02-22 12:27:46 -0800510 colorTypes.push_back(CodecSrc::kGrayscale_Always_DstColorType);
msarett55f7bdd2016-02-16 13:24:54 -0800511 if (kWBMP_SkEncodedFormat == codec->getEncodedFormat()) {
msarett91c22b22016-02-22 12:27:46 -0800512 colorTypes.push_back(CodecSrc::kIndex8_Always_DstColorType);
msarett55f7bdd2016-02-16 13:24:54 -0800513 }
msarett36c37962015-09-02 13:20:52 -0700514 break;
515 case kIndex_8_SkColorType:
msarett91c22b22016-02-22 12:27:46 -0800516 colorTypes.push_back(CodecSrc::kIndex8_Always_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700517 break;
518 default:
msarett36c37962015-09-02 13:20:52 -0700519 break;
520 }
msarett9e707a02015-09-01 14:57:57 -0700521
scroggoc5560be2016-02-03 09:42:42 -0800522 SkTArray<SkAlphaType> alphaModes;
523 alphaModes.push_back(kPremul_SkAlphaType);
msarett9e9444c2016-02-03 12:39:10 -0800524 alphaModes.push_back(kUnpremul_SkAlphaType);
scroggoc5560be2016-02-03 09:42:42 -0800525 if (codec->getInfo().alphaType() == kOpaque_SkAlphaType) {
526 alphaModes.push_back(kOpaque_SkAlphaType);
527 }
msarettb714fb02016-01-22 14:46:42 -0800528
529 for (CodecSrc::Mode mode : nativeModes) {
msarettb714fb02016-01-22 14:46:42 -0800530 for (float scale : nativeScales) {
msarett91c22b22016-02-22 12:27:46 -0800531 for (CodecSrc::DstColorType colorType : colorTypes) {
scroggoc5560be2016-02-03 09:42:42 -0800532 for (SkAlphaType alphaType : alphaModes) {
msarett91c22b22016-02-22 12:27:46 -0800533 // Only test kCroppedScanline_Mode when the alpha type is opaque. The test is
534 // slow and won't be interestingly different with different alpha types.
535 if (CodecSrc::kCroppedScanline_Mode == mode &&
536 kOpaque_SkAlphaType != alphaType) {
537 continue;
538 }
539
540 push_codec_src(path, mode, colorType, alphaType, scale);
scroggoc5560be2016-02-03 09:42:42 -0800541 }
msarett9e707a02015-09-01 14:57:57 -0700542 }
543 }
msarett0a242972015-06-11 14:27:27 -0700544 }
msarett36c37962015-09-02 13:20:52 -0700545
scroggoe4499842016-02-25 11:03:47 -0800546 if (FLAGS_simpleCodec) {
547 return;
548 }
549
halcanary6950de62015-11-07 05:29:00 -0800550 // https://bug.skia.org/4428
msarett33c76232015-11-16 13:43:40 -0800551 bool subset = false;
552 // The following image types are supported by BitmapRegionDecoder,
553 // so we will test full image decodes and subset decodes.
msarettbe8216a2015-12-04 08:00:50 -0800554 static const char* const exts[] = {
msarett3d9d7a72015-10-21 10:27:10 -0700555 "jpg", "jpeg", "png", "webp",
556 "JPG", "JPEG", "PNG", "WEBP",
557 };
msarettbe8216a2015-12-04 08:00:50 -0800558 for (const char* ext : exts) {
msarett3d9d7a72015-10-21 10:27:10 -0700559 if (path.endsWith(ext)) {
msarett33c76232015-11-16 13:43:40 -0800560 subset = true;
msarett3d9d7a72015-10-21 10:27:10 -0700561 break;
562 }
563 }
msarett33c76232015-11-16 13:43:40 -0800564
msarett3d9d7a72015-10-21 10:27:10 -0700565 const int sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
msarett36c37962015-09-02 13:20:52 -0700566
msarett3d9d7a72015-10-21 10:27:10 -0700567 for (int sampleSize : sampleSizes) {
msarett91c22b22016-02-22 12:27:46 -0800568 for (CodecSrc::DstColorType colorType : colorTypes) {
scroggoc5560be2016-02-03 09:42:42 -0800569 for (SkAlphaType alphaType : alphaModes) {
msarett91c22b22016-02-22 12:27:46 -0800570 push_android_codec_src(path, AndroidCodecSrc::kFullImage_Mode, colorType,
scroggoc5560be2016-02-03 09:42:42 -0800571 alphaType, sampleSize);
572 if (subset) {
msarett91c22b22016-02-22 12:27:46 -0800573 push_android_codec_src(path, AndroidCodecSrc::kDivisor_Mode, colorType,
scroggoc5560be2016-02-03 09:42:42 -0800574 alphaType, sampleSize);
575 }
msarett3d9d7a72015-10-21 10:27:10 -0700576 }
msarett36c37962015-09-02 13:20:52 -0700577 }
578 }
msarett18976312016-03-09 14:20:58 -0800579
580 static const char* const rawExts[] = {
581 "arw", "cr2", "dng", "nef", "nrw", "orf", "raf", "rw2", "pef", "srw",
582 "ARW", "CR2", "DNG", "NEF", "NRW", "ORF", "RAF", "RW2", "PEF", "SRW",
583 };
584
585 // There is not currently a reason to test RAW images on image generator.
586 // If we want to enable these tests, we will need to fix skbug.com/5079.
587 for (const char* ext : rawExts) {
588 if (path.endsWith(ext)) {
589 return;
590 }
591 }
592
593 // Push image generator GPU test.
brianosmanc9ced392016-03-24 07:27:43 -0700594 push_image_gen_src(path, ImageGenSrc::kCodec_Mode, codec->getInfo().alphaType(), true);
msarett18976312016-03-09 14:20:58 -0800595
596 // Push image generator CPU tests.
597 for (SkAlphaType alphaType : alphaModes) {
598 push_image_gen_src(path, ImageGenSrc::kCodec_Mode, alphaType, false);
599
600#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
601 if (kWEBP_SkEncodedFormat != codec->getEncodedFormat() &&
602 kWBMP_SkEncodedFormat != codec->getEncodedFormat() &&
603 kUnpremul_SkAlphaType != alphaType)
604 {
605 push_image_gen_src(path, ImageGenSrc::kPlatform_Mode, alphaType, false);
606 }
msarettfc0b6d12016-03-17 13:50:17 -0700607#elif defined(SK_BUILD_FOR_WIN)
608 if (kWEBP_SkEncodedFormat != codec->getEncodedFormat() &&
609 kWBMP_SkEncodedFormat != codec->getEncodedFormat())
610 {
611 push_image_gen_src(path, ImageGenSrc::kPlatform_Mode, alphaType, false);
612 }
msarett18976312016-03-09 14:20:58 -0800613#endif
614 }
msarett438b2ad2015-04-09 12:43:10 -0700615}
616
msarett5cb48852015-11-06 08:56:32 -0800617static bool brd_color_type_supported(SkBitmapRegionDecoder::Strategy strategy,
msaretta5783ae2015-09-08 15:35:32 -0700618 CodecSrc::DstColorType dstColorType) {
619 switch (strategy) {
msarett5cb48852015-11-06 08:56:32 -0800620 case SkBitmapRegionDecoder::kCanvas_Strategy:
msaretta5783ae2015-09-08 15:35:32 -0700621 if (CodecSrc::kGetFromCanvas_DstColorType == dstColorType) {
622 return true;
623 }
624 return false;
msarett5cb48852015-11-06 08:56:32 -0800625 case SkBitmapRegionDecoder::kAndroidCodec_Strategy:
msarett26ad17b2015-10-22 07:29:19 -0700626 switch (dstColorType) {
627 case CodecSrc::kGetFromCanvas_DstColorType:
628 case CodecSrc::kIndex8_Always_DstColorType:
629 case CodecSrc::kGrayscale_Always_DstColorType:
630 return true;
631 default:
632 return false;
633 }
msaretta5783ae2015-09-08 15:35:32 -0700634 default:
635 SkASSERT(false);
636 return false;
637 }
638}
639
msarett5cb48852015-11-06 08:56:32 -0800640static void push_brd_src(Path path, SkBitmapRegionDecoder::Strategy strategy,
msaretta5783ae2015-09-08 15:35:32 -0700641 CodecSrc::DstColorType dstColorType, BRDSrc::Mode mode, uint32_t sampleSize) {
642 SkString folder;
643 switch (strategy) {
msarett5cb48852015-11-06 08:56:32 -0800644 case SkBitmapRegionDecoder::kCanvas_Strategy:
msaretta5783ae2015-09-08 15:35:32 -0700645 folder.append("brd_canvas");
646 break;
msarett5cb48852015-11-06 08:56:32 -0800647 case SkBitmapRegionDecoder::kAndroidCodec_Strategy:
msarett26ad17b2015-10-22 07:29:19 -0700648 folder.append("brd_android_codec");
649 break;
msaretta5783ae2015-09-08 15:35:32 -0700650 default:
651 SkASSERT(false);
652 return;
653 }
654
655 switch (mode) {
656 case BRDSrc::kFullImage_Mode:
657 break;
658 case BRDSrc::kDivisor_Mode:
659 folder.append("_divisor");
660 break;
661 default:
662 SkASSERT(false);
663 return;
664 }
665
666 switch (dstColorType) {
667 case CodecSrc::kGetFromCanvas_DstColorType:
668 break;
669 case CodecSrc::kIndex8_Always_DstColorType:
670 folder.append("_kIndex");
671 break;
672 case CodecSrc::kGrayscale_Always_DstColorType:
673 folder.append("_kGray");
674 break;
675 default:
676 SkASSERT(false);
677 return;
678 }
679
680 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800681 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700682 }
683
684 BRDSrc* src = new BRDSrc(path, strategy, mode, dstColorType, sampleSize);
685 push_src("image", folder, src);
686}
687
688static void push_brd_srcs(Path path) {
689
msarett5cb48852015-11-06 08:56:32 -0800690 const SkBitmapRegionDecoder::Strategy strategies[] = {
691 SkBitmapRegionDecoder::kCanvas_Strategy,
msarett5cb48852015-11-06 08:56:32 -0800692 SkBitmapRegionDecoder::kAndroidCodec_Strategy,
msaretta5783ae2015-09-08 15:35:32 -0700693 };
694
scroggo501b7342015-11-03 07:55:11 -0800695 // Test on a variety of sampleSizes, making sure to include:
696 // - 2, 4, and 8, which are natively supported by jpeg
697 // - multiples of 2 which are not divisible by 4 (analogous for 4)
698 // - larger powers of two, since BRD clients generally use powers of 2
699 // We will only produce output for the larger sizes on large images.
700 const uint32_t sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 24, 32, 64 };
msarett6efbe052015-09-11 09:01:16 -0700701
msaretta5783ae2015-09-08 15:35:32 -0700702 // We will only test to one backend (8888), but we will test all of the
703 // color types that we need to decode to on this backend.
704 const CodecSrc::DstColorType dstColorTypes[] = {
msarett26ad17b2015-10-22 07:29:19 -0700705 CodecSrc::kGetFromCanvas_DstColorType,
706 CodecSrc::kIndex8_Always_DstColorType,
707 CodecSrc::kGrayscale_Always_DstColorType,
msaretta5783ae2015-09-08 15:35:32 -0700708 };
709
710 const BRDSrc::Mode modes[] = {
msarett26ad17b2015-10-22 07:29:19 -0700711 BRDSrc::kFullImage_Mode,
712 BRDSrc::kDivisor_Mode,
msaretta5783ae2015-09-08 15:35:32 -0700713 };
714
msarett5cb48852015-11-06 08:56:32 -0800715 for (SkBitmapRegionDecoder::Strategy strategy : strategies) {
msarett6efbe052015-09-11 09:01:16 -0700716 for (uint32_t sampleSize : sampleSizes) {
msarett6efbe052015-09-11 09:01:16 -0700717 for (CodecSrc::DstColorType dstColorType : dstColorTypes) {
718 if (brd_color_type_supported(strategy, dstColorType)) {
719 for (BRDSrc::Mode mode : modes) {
msaretta5783ae2015-09-08 15:35:32 -0700720 push_brd_src(path, strategy, dstColorType, mode, sampleSize);
721 }
722 }
723 }
724 }
725 }
726}
727
728static bool brd_supported(const char* ext) {
msarett8c8f22a2015-04-01 06:58:48 -0700729 static const char* const exts[] = {
msaretta5783ae2015-09-08 15:35:32 -0700730 "jpg", "jpeg", "png", "webp",
731 "JPG", "JPEG", "PNG", "WEBP",
msarett8c8f22a2015-04-01 06:58:48 -0700732 };
733
734 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
735 if (0 == strcmp(exts[i], ext)) {
736 return true;
737 }
738 }
739 return false;
scroggo9b77ddd2015-03-19 06:03:39 -0700740}
741
scroggo86737142016-02-03 12:19:11 -0800742static bool gather_srcs() {
mtklein748ca3b2015-01-15 10:56:12 -0800743 for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) {
djsollen54416de2015-04-03 07:24:48 -0700744 push_src("gm", "", new GMSrc(r->factory()));
mtklein748ca3b2015-01-15 10:56:12 -0800745 }
halcanaryfc37ad12015-01-30 07:31:19 -0800746 for (int i = 0; i < FLAGS_skps.count(); i++) {
747 const char* path = FLAGS_skps[i];
748 if (sk_isdir(path)) {
749 SkOSFile::Iter it(path, "skp");
750 for (SkString file; it.next(&file); ) {
djsollen54416de2015-04-03 07:24:48 -0700751 push_src("skp", "", new SKPSrc(SkOSPath::Join(path, file.c_str())));
halcanaryfc37ad12015-01-30 07:31:19 -0800752 }
753 } else {
djsollen54416de2015-04-03 07:24:48 -0700754 push_src("skp", "", new SKPSrc(path));
mtklein114c3cd2015-01-15 10:15:02 -0800755 }
756 }
scroggo86737142016-02-03 12:19:11 -0800757
758 SkTArray<SkString> images;
759 if (!CollectImages(&images)) {
760 return false;
761 }
762
763 for (auto image : images) {
764 push_codec_srcs(image);
scroggoe4499842016-02-25 11:03:47 -0800765 if (FLAGS_simpleCodec) {
766 continue;
767 }
768
mtklein21eaf3b2016-02-08 12:39:59 -0800769 const char* ext = strrchr(image.c_str(), '.');
770 if (ext && brd_supported(ext+1)) {
scroggo86737142016-02-03 12:19:11 -0800771 push_brd_srcs(image);
mtklein709d2c32015-01-15 08:30:25 -0800772 }
mtklein709d2c32015-01-15 08:30:25 -0800773 }
scroggo86737142016-02-03 12:19:11 -0800774
775 return true;
mtklein709d2c32015-01-15 08:30:25 -0800776}
777
kkinnunen3e980c32015-12-23 01:33:00 -0800778static void push_sink(const SkCommandLineConfig& config, Sink* s) {
rmistry0f515bd2015-12-22 10:22:26 -0800779 SkAutoTDelete<Sink> sink(s);
kkinnunen3e980c32015-12-23 01:33:00 -0800780
mtkleine0effd62015-07-29 06:37:28 -0700781 // Try a simple Src as a canary. If it fails, skip this sink.
mtklein748ca3b2015-01-15 10:56:12 -0800782 struct : public Src {
mtkleine0effd62015-07-29 06:37:28 -0700783 Error draw(SkCanvas* c) const override {
784 c->drawRect(SkRect::MakeWH(1,1), SkPaint());
785 return "";
786 }
mtklein36352bf2015-03-25 18:17:31 -0700787 SkISize size() const override { return SkISize::Make(16, 16); }
mtkleine0effd62015-07-29 06:37:28 -0700788 Name name() const override { return "justOneRect"; }
789 } justOneRect;
mtklein114c3cd2015-01-15 10:15:02 -0800790
mtklein748ca3b2015-01-15 10:56:12 -0800791 SkBitmap bitmap;
792 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800793 SkString log;
mtkleine0effd62015-07-29 06:37:28 -0700794 Error err = sink->draw(justOneRect, &bitmap, &stream, &log);
mtklein4089ef72015-03-05 08:40:28 -0800795 if (err.isFatal()) {
mtkleinc41fd922016-03-08 09:01:39 -0800796 info("Could not run %s: %s\n", config.getTag().c_str(), err.c_str());
mtklein05641a52015-04-21 10:49:13 -0700797 exit(1);
mtklein114c3cd2015-01-15 10:15:02 -0800798 }
799
mtkleine0effd62015-07-29 06:37:28 -0700800 TaggedSink& ts = gSinks.push_back();
mtklein18300a32016-03-16 13:53:35 -0700801 ts.reset(sink.release());
kkinnunen3e980c32015-12-23 01:33:00 -0800802 ts.tag = config.getTag();
mtklein748ca3b2015-01-15 10:56:12 -0800803}
804
805static bool gpu_supported() {
806#if SK_SUPPORT_GPU
807 return FLAGS_gpu;
808#else
809 return false;
810#endif
811}
kkinnunen9ebc3f02015-12-21 23:48:13 -0800812
kkinnunen3e980c32015-12-23 01:33:00 -0800813static Sink* create_sink(const SkCommandLineConfig* config) {
814#if SK_SUPPORT_GPU
815 if (gpu_supported()) {
816 if (const SkCommandLineConfigGpu* gpuConfig = config->asConfigGpu()) {
817 GrContextFactory::GLContextType contextType = gpuConfig->getContextType();
818 GrContextFactory::GLContextOptions contextOptions =
819 GrContextFactory::kNone_GLContextOptions;
820 if (gpuConfig->getUseNVPR()) {
821 contextOptions = static_cast<GrContextFactory::GLContextOptions>(
822 contextOptions | GrContextFactory::kEnableNVPR_GLContextOptions);
823 }
brianosman61d3b082016-03-30 11:19:36 -0700824 if (kSRGB_SkColorProfileType == gpuConfig->getProfileType() ||
825 kRGBA_F16_SkColorType == gpuConfig->getColorType()) {
826 contextOptions = static_cast<GrContextFactory::GLContextOptions>(
827 contextOptions | GrContextFactory::kRequireSRGBSupport_GLContextOptions);
828 }
kkinnunen3e980c32015-12-23 01:33:00 -0800829 GrContextFactory testFactory;
830 if (!testFactory.get(contextType, contextOptions)) {
mtkleinc41fd922016-03-08 09:01:39 -0800831 info("WARNING: can not create GPU context for config '%s'. "
832 "GM tests will be skipped.\n", gpuConfig->getTag().c_str());
kkinnunen3e980c32015-12-23 01:33:00 -0800833 return nullptr;
834 }
835 return new GPUSink(contextType, contextOptions, gpuConfig->getSamples(),
brianosmand93c1202016-03-10 07:49:08 -0800836 gpuConfig->getUseDIText(), gpuConfig->getColorType(),
837 gpuConfig->getProfileType(), FLAGS_gpu_threading);
kkinnunen3e980c32015-12-23 01:33:00 -0800838 }
839 }
840#endif
841
842#define SINK(t, sink, ...) if (config->getBackend().equals(t)) { return new sink(__VA_ARGS__); }
843
tomhudsoneebc39a2015-02-23 12:18:05 -0800844#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
845 SINK("hwui", HWUISink);
846#endif
847
mtklein748ca3b2015-01-15 10:56:12 -0800848 if (FLAGS_cpu) {
849 SINK("565", RasterSink, kRGB_565_SkColorType);
850 SINK("8888", RasterSink, kN32_SkColorType);
mtklein27c3fdd2016-02-26 14:43:21 -0800851 SINK("srgb", RasterSink, kN32_SkColorType, kSRGB_SkColorProfileType);
852 SINK("f16", RasterSink, kRGBA_F16_SkColorType);
halcanaryc11c62f2015-09-28 11:51:54 -0700853 SINK("pdf", PDFSink, "Pdfium");
854 SINK("pdf_poppler", PDFSink, "Poppler");
mtklein9c3f17d2015-01-28 11:35:18 -0800855 SINK("skp", SKPSink);
mtklein8a4527e2015-01-31 20:00:58 -0800856 SINK("svg", SVGSink);
857 SINK("null", NullSink);
halcanary47ef4d52015-03-03 09:13:09 -0800858 SINK("xps", XPSSink);
mtklein748ca3b2015-01-15 10:56:12 -0800859 }
860#undef SINK
halcanary96fcdcc2015-08-27 07:41:13 -0700861 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800862}
863
kkinnunen3e980c32015-12-23 01:33:00 -0800864static Sink* create_via(const SkString& tag, Sink* wrapped) {
865#define VIA(t, via, ...) if (tag.equals(t)) { return new via(__VA_ARGS__); }
halcanary96fcdcc2015-08-27 07:41:13 -0700866 VIA("twice", ViaTwice, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700867 VIA("serialize", ViaSerialization, wrapped);
mtklein4a34ecb2016-01-08 10:19:35 -0800868 VIA("pic", ViaPicture, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700869 VIA("2ndpic", ViaSecondPicture, wrapped);
mtkleind31c13d2015-05-05 12:59:56 -0700870 VIA("sp", ViaSingletonPictures, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700871 VIA("tiles", ViaTiles, 256, 256, nullptr, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800872 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
halcanary7a76f9c2016-02-03 11:53:18 -0800873 VIA("mojo", ViaMojo, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800874
mtkleind603b222015-02-17 11:13:33 -0800875 if (FLAGS_matrix.count() == 4) {
mtklein748ca3b2015-01-15 10:56:12 -0800876 SkMatrix m;
mtkleind603b222015-02-17 11:13:33 -0800877 m.reset();
878 m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
879 m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
880 m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
881 m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
882 VIA("matrix", ViaMatrix, m, wrapped);
883 VIA("upright", ViaUpright, m, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800884 }
tomhudson64de1e12015-03-05 08:01:07 -0800885
886#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
887 VIA("androidsdk", ViaAndroidSDK, wrapped);
888#endif
889
mtklein748ca3b2015-01-15 10:56:12 -0800890#undef VIA
halcanary96fcdcc2015-08-27 07:41:13 -0700891 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800892}
893
mtklein748ca3b2015-01-15 10:56:12 -0800894static void gather_sinks() {
kkinnunen3e980c32015-12-23 01:33:00 -0800895 SkCommandLineConfigArray configs;
896 ParseConfigs(FLAGS_config, &configs);
897 for (int i = 0; i < configs.count(); i++) {
898 const SkCommandLineConfig& config = *configs[i];
899 Sink* sink = create_sink(&config);
900 if (sink == nullptr) {
mtkleinc41fd922016-03-08 09:01:39 -0800901 info("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
902 config.getTag().c_str());
kkinnunen3e980c32015-12-23 01:33:00 -0800903 continue;
904 }
mtklein748ca3b2015-01-15 10:56:12 -0800905
kkinnunen3e980c32015-12-23 01:33:00 -0800906 const SkTArray<SkString>& parts = config.getViaParts();
907 for (int j = parts.count(); j-- > 0;) {
908 const SkString& part = parts[j];
909 Sink* next = create_via(part, sink);
halcanary96fcdcc2015-08-27 07:41:13 -0700910 if (next == nullptr) {
mtkleinc41fd922016-03-08 09:01:39 -0800911 info("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
912 part.c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800913 delete sink;
halcanary96fcdcc2015-08-27 07:41:13 -0700914 sink = nullptr;
mtklein748ca3b2015-01-15 10:56:12 -0800915 break;
916 }
917 sink = next;
918 }
919 if (sink) {
920 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800921 }
922 }
923}
mtklein709d2c32015-01-15 08:30:25 -0800924
mtkleina5114d72015-08-24 13:27:01 -0700925static bool dump_png(SkBitmap bitmap, const char* path, const char* md5) {
926 const int w = bitmap.width(),
927 h = bitmap.height();
mtklein27c3fdd2016-02-26 14:43:21 -0800928 // PNG wants unpremultiplied 8-bit RGBA pixels (16-bit could work fine too).
929 // We leave the gamma of these bytes unspecified, to continue the status quo,
930 // which we think generally is to interpret them as sRGB.
mtkleina5114d72015-08-24 13:27:01 -0700931
mtklein27c3fdd2016-02-26 14:43:21 -0800932 SkAutoTMalloc<uint32_t> rgba(w*h);
933
934 if (bitmap. colorType() == kN32_SkColorType &&
935 bitmap.profileType() == kSRGB_SkColorProfileType) {
936 // These are premul sRGB 8-bit pixels in SkPMColor order.
937 // We want unpremul sRGB 8-bit pixels in RGBA order. We'll get there via floats.
938 bitmap.lockPixels();
939 auto px = (const uint32_t*)bitmap.getPixels();
940 if (!px) {
mtkleina5114d72015-08-24 13:27:01 -0700941 return false;
942 }
mtklein27c3fdd2016-02-26 14:43:21 -0800943 for (int i = 0; i < w*h; i++) {
944 Sk4f fs = Sk4f_fromS32(px[i]); // Convert up to linear floats.
945 #if defined(SK_PMCOLOR_IS_BGRA)
946 fs = SkNx_shuffle<2,1,0,3>(fs); // Shuffle to RGBA, if not there already.
947 #endif
948 float invA = 1.0f / fs[3];
949 fs = fs * Sk4f(invA, invA, invA, 1); // Unpremultiply.
950 rgba[i] = Sk4f_toS32(fs); // Pack down to sRGB bytes.
951 }
mtkleina5114d72015-08-24 13:27:01 -0700952
mtklein27c3fdd2016-02-26 14:43:21 -0800953 } else if (bitmap.colorType() == kRGBA_F16_SkColorType) {
954 // These are premul linear half-float pixels in RGBA order.
955 // We want unpremul sRGB 8-bit pixels in RGBA order. We'll get there via floats.
956 bitmap.lockPixels();
957 auto px = (const uint64_t*)bitmap.getPixels();
958 if (!px) {
959 return false;
960 }
961 for (int i = 0; i < w*h; i++) {
962 Sk4f fs = SkHalfToFloat_01(px[i]); // Convert up to linear floats.
963 float invA = 1.0f / fs[3];
964 fs = fs * Sk4f(invA, invA, invA, 1); // Unpremultiply.
965 rgba[i] = Sk4f_toS32(fs); // Pack down to sRGB bytes.
966 }
967
968
969 } else {
970 // We "should" gamma correct in here but we don't.
971 // We want Gold to show exactly what our clients are seeing, broken gamma.
972
973 // Convert smaller formats up to premul linear 8-bit (in SkPMColor order).
974 if (bitmap.colorType() != kN32_SkColorType) {
975 SkBitmap n32;
976 if (!bitmap.copyTo(&n32, kN32_SkColorType)) {
977 return false;
978 }
979 bitmap = n32;
980 }
981
982 // Convert premul linear 8-bit to unpremul linear 8-bit RGBA.
983 if (!bitmap.readPixels(SkImageInfo::Make(w,h, kRGBA_8888_SkColorType,
984 kUnpremul_SkAlphaType),
985 rgba, 4*w, 0,0)) {
986 return false;
987 }
mtkleina5114d72015-08-24 13:27:01 -0700988 }
989
990 // We don't need bitmap anymore. Might as well drop our ref.
mtkleinfccb77d2015-08-24 14:13:29 -0700991 bitmap.reset();
mtkleina5114d72015-08-24 13:27:01 -0700992
mtkleinc64137c2015-08-25 10:56:08 -0700993 FILE* f = fopen(path, "wb");
mtkleina5114d72015-08-24 13:27:01 -0700994 if (!f) { return false; }
995
996 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
997 if (!png) {
998 fclose(f);
999 return false;
1000 }
1001
1002 png_infop info = png_create_info_struct(png);
1003 if (!info) {
1004 png_destroy_write_struct(&png, &info);
1005 fclose(f);
1006 return false;
1007 }
1008
mtkleind69ece62015-09-10 10:37:44 -07001009 SkString description;
1010 description.append("Key: ");
1011 for (int i = 0; i < FLAGS_key.count(); i++) {
1012 description.appendf("%s ", FLAGS_key[i]);
1013 }
1014 description.append("Properties: ");
1015 for (int i = 0; i < FLAGS_properties.count(); i++) {
1016 description.appendf("%s ", FLAGS_properties[i]);
1017 }
1018 description.appendf("MD5: %s", md5);
1019
mtkleina5114d72015-08-24 13:27:01 -07001020 png_text text[2];
1021 text[0].key = (png_charp)"Author";
1022 text[0].text = (png_charp)"DM dump_png()";
1023 text[0].compression = PNG_TEXT_COMPRESSION_NONE;
1024 text[1].key = (png_charp)"Description";
mtkleind69ece62015-09-10 10:37:44 -07001025 text[1].text = (png_charp)description.c_str();
mtkleina5114d72015-08-24 13:27:01 -07001026 text[1].compression = PNG_TEXT_COMPRESSION_NONE;
1027 png_set_text(png, info, text, 2);
1028
1029 png_init_io(png, f);
1030 png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8,
1031 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
1032 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
1033 png_write_info(png, info);
1034 for (int j = 0; j < h; j++) {
1035 png_bytep row = (png_bytep)(rgba.get() + w*j);
1036 png_write_rows(png, &row, 1);
1037 }
1038 png_write_end(png, info);
1039
1040 png_destroy_write_struct(&png, &info);
1041 fclose(f);
1042 return true;
1043}
1044
mtkleina2ef6422015-01-15 13:44:22 -08001045static bool match(const char* needle, const char* haystack) {
halcanary96fcdcc2015-08-27 07:41:13 -07001046 return 0 == strcmp("_", needle) || nullptr != strstr(haystack, needle);
mtkleina2ef6422015-01-15 13:44:22 -08001047}
1048
mtklein3eed7dd2016-02-24 19:07:07 -08001049static bool is_blacklisted(const char* sink, const char* src,
1050 const char* srcOptions, const char* name) {
mtklein0d243ff2015-04-03 07:51:00 -07001051 for (int i = 0; i < FLAGS_blacklist.count() - 3; i += 4) {
mtkleina2ef6422015-01-15 13:44:22 -08001052 if (match(FLAGS_blacklist[i+0], sink) &&
djsollen54416de2015-04-03 07:24:48 -07001053 match(FLAGS_blacklist[i+1], src) &&
1054 match(FLAGS_blacklist[i+2], srcOptions) &&
1055 match(FLAGS_blacklist[i+3], name)) {
mtklein3eed7dd2016-02-24 19:07:07 -08001056 return true;
mtkleina2ef6422015-01-15 13:44:22 -08001057 }
1058 }
mtklein3eed7dd2016-02-24 19:07:07 -08001059 return false;
mtkleina2ef6422015-01-15 13:44:22 -08001060}
1061
mtkleincd50bca2016-01-05 06:20:20 -08001062// Even when a Task Sink reports to be non-threadsafe (e.g. GPU), we know things like
1063// .png encoding are definitely thread safe. This lets us offload that work to CPU threads.
1064static SkTaskGroup gDefinitelyThreadSafeWork;
1065
mtklein748ca3b2015-01-15 10:56:12 -08001066// The finest-grained unit of work we can run: draw a single Src into a single Sink,
1067// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
1068struct Task {
mtkleine0effd62015-07-29 06:37:28 -07001069 Task(const TaggedSrc& src, const TaggedSink& sink) : src(src), sink(sink) {}
1070 const TaggedSrc& src;
1071 const TaggedSink& sink;
mtklein748ca3b2015-01-15 10:56:12 -08001072
mtklein21eaf3b2016-02-08 12:39:59 -08001073 static void Run(const Task& task) {
1074 SkString name = task.src->name();
mtkleine0effd62015-07-29 06:37:28 -07001075
mtkleinb9eb4ac2015-02-02 18:26:03 -08001076 SkString log;
mtklein3eed7dd2016-02-24 19:07:07 -08001077 if (!FLAGS_dryRun) {
mtklein748ca3b2015-01-15 10:56:12 -08001078 SkBitmap bitmap;
1079 SkDynamicMemoryWStream stream;
mtklein3eed7dd2016-02-24 19:07:07 -08001080 start(task.sink.tag.c_str(), task.src.tag.c_str(),
1081 task.src.options.c_str(), name.c_str());
mtklein21eaf3b2016-02-08 12:39:59 -08001082 Error err = task.sink->draw(*task.src, &bitmap, &stream, &log);
mtkleinb3b13b72016-03-07 13:20:52 -08001083 if (!log.isEmpty()) {
mtkleinc41fd922016-03-08 09:01:39 -08001084 info("%s %s %s %s:\n%s\n", task.sink.tag.c_str()
1085 , task.src.tag.c_str()
1086 , task.src.options.c_str()
1087 , name.c_str()
1088 , log.c_str());
mtkleinb3b13b72016-03-07 13:20:52 -08001089 }
mtklein748ca3b2015-01-15 10:56:12 -08001090 if (!err.isEmpty()) {
mtklein4089ef72015-03-05 08:40:28 -08001091 if (err.isFatal()) {
djsollen54416de2015-04-03 07:24:48 -07001092 fail(SkStringPrintf("%s %s %s %s: %s",
mtklein21eaf3b2016-02-08 12:39:59 -08001093 task.sink.tag.c_str(),
1094 task.src.tag.c_str(),
1095 task.src.options.c_str(),
mtklein4089ef72015-03-05 08:40:28 -08001096 name.c_str(),
1097 err.c_str()));
mtkleinb37cb412015-03-18 05:27:14 -07001098 } else {
mtklein3eed7dd2016-02-24 19:07:07 -08001099 done(task.sink.tag.c_str(), task.src.tag.c_str(),
1100 task.src.options.c_str(), name.c_str());
mtkleinba6ada72016-01-21 09:39:35 -08001101 return;
mtklein4089ef72015-03-05 08:40:28 -08001102 }
mtklein748ca3b2015-01-15 10:56:12 -08001103 }
mtklein62bd1a62015-01-27 14:46:26 -08001104
mtkleincd50bca2016-01-05 06:20:20 -08001105 // We're likely switching threads here, so we must capture by value, [=] or [foo,bar].
1106 SkStreamAsset* data = stream.detachAsStream();
1107 gDefinitelyThreadSafeWork.add([task,name,bitmap,data]{
1108 SkAutoTDelete<SkStreamAsset> ownedData(data);
1109
1110 // Why doesn't the copy constructor do this when we have pre-locked pixels?
1111 bitmap.lockPixels();
1112
1113 SkString md5;
1114 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
1115 SkMD5 hash;
1116 if (data->getLength()) {
1117 hash.writeStream(data, data->getLength());
1118 data->rewind();
mtklein38408462015-07-08 07:25:27 -07001119 } else {
mtkleincd50bca2016-01-05 06:20:20 -08001120 // If we're BGRA (Linux, Windows), swizzle over to RGBA (Mac, Android).
1121 // This helps eliminate multiple 0-pixel-diff hashes on gold.skia.org.
1122 // (Android's general slow speed breaks the tie arbitrarily in RGBA's favor.)
1123 // We might consider promoting 565 to RGBA too.
1124 if (bitmap.colorType() == kBGRA_8888_SkColorType) {
1125 SkBitmap swizzle;
1126 SkAssertResult(bitmap.copyTo(&swizzle, kRGBA_8888_SkColorType));
1127 hash.write(swizzle.getPixels(), swizzle.getSize());
1128 } else {
1129 hash.write(bitmap.getPixels(), bitmap.getSize());
1130 }
1131 }
1132 SkMD5::Digest digest;
1133 hash.finish(digest);
1134 for (int i = 0; i < 16; i++) {
1135 md5.appendf("%02x", digest.data[i]);
mtklein38408462015-07-08 07:25:27 -07001136 }
mtklein62bd1a62015-01-27 14:46:26 -08001137 }
mtklein62bd1a62015-01-27 14:46:26 -08001138
mtkleincd50bca2016-01-05 06:20:20 -08001139 if (!FLAGS_readPath.isEmpty() &&
mtklein3eed7dd2016-02-24 19:07:07 -08001140 !gGold.contains(Gold(task.sink.tag, task.src.tag,
1141 task.src.options, name, md5))) {
mtkleincd50bca2016-01-05 06:20:20 -08001142 fail(SkStringPrintf("%s not found for %s %s %s %s in %s",
1143 md5.c_str(),
mtklein21eaf3b2016-02-08 12:39:59 -08001144 task.sink.tag.c_str(),
1145 task.src.tag.c_str(),
1146 task.src.options.c_str(),
mtkleincd50bca2016-01-05 06:20:20 -08001147 name.c_str(),
1148 FLAGS_readPath[0]));
mtklein748ca3b2015-01-15 10:56:12 -08001149 }
mtkleincd50bca2016-01-05 06:20:20 -08001150
1151 if (!FLAGS_writePath.isEmpty()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001152 const char* ext = task.sink->fileExtension();
mtkleincd50bca2016-01-05 06:20:20 -08001153 if (data->getLength()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001154 WriteToDisk(task, md5, ext, data, data->getLength(), nullptr);
mtkleincd50bca2016-01-05 06:20:20 -08001155 SkASSERT(bitmap.drawsNothing());
1156 } else if (!bitmap.drawsNothing()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001157 WriteToDisk(task, md5, ext, nullptr, 0, &bitmap);
mtkleincd50bca2016-01-05 06:20:20 -08001158 }
1159 }
1160 });
mtklein748ca3b2015-01-15 10:56:12 -08001161 }
mtklein3eed7dd2016-02-24 19:07:07 -08001162 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 -08001163 }
1164
1165 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -08001166 SkString md5,
1167 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -08001168 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -08001169 const SkBitmap* bitmap) {
mtklein409d4702016-02-29 07:38:01 -08001170 bool gammaCorrect = false;
1171 if (bitmap) {
1172 gammaCorrect = bitmap->profileType() == kSRGB_SkColorProfileType
1173 || bitmap-> colorType() == kRGBA_F16_SkColorType;
1174 }
1175
mtklein748ca3b2015-01-15 10:56:12 -08001176 JsonWriter::BitmapResult result;
djsollen54416de2015-04-03 07:24:48 -07001177 result.name = task.src->name();
mtklein3eed7dd2016-02-24 19:07:07 -08001178 result.config = task.sink.tag;
djsollen54416de2015-04-03 07:24:48 -07001179 result.sourceType = task.src.tag;
1180 result.sourceOptions = task.src.options;
1181 result.ext = ext;
mtklein409d4702016-02-29 07:38:01 -08001182 result.gammaCorrect = gammaCorrect;
djsollen54416de2015-04-03 07:24:48 -07001183 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -08001184 JsonWriter::AddBitmapResult(result);
1185
mtkleinb0531a72015-04-07 13:38:48 -07001186 // If an MD5 is uninteresting, we want it noted in the JSON file,
1187 // but don't want to dump it out as a .png (or whatever ext is).
1188 if (gUninterestingHashes.contains(md5)) {
1189 return;
1190 }
1191
mtklein748ca3b2015-01-15 10:56:12 -08001192 const char* dir = FLAGS_writePath[0];
1193 if (0 == strcmp(dir, "@")) { // Needed for iOS.
1194 dir = FLAGS_resourcePath[0];
1195 }
1196 sk_mkdir(dir);
1197
1198 SkString path;
1199 if (FLAGS_nameByHash) {
1200 path = SkOSPath::Join(dir, result.md5.c_str());
1201 path.append(".");
1202 path.append(ext);
1203 if (sk_exists(path.c_str())) {
1204 return; // Content-addressed. If it exists already, we're done.
1205 }
1206 } else {
kkinnunen3e980c32015-12-23 01:33:00 -08001207 path = SkOSPath::Join(dir, task.sink.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001208 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001209 path = SkOSPath::Join(path.c_str(), task.src.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001210 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001211 if (strcmp(task.src.options.c_str(), "") != 0) {
1212 path = SkOSPath::Join(path.c_str(), task.src.options.c_str());
djsollen54416de2015-04-03 07:24:48 -07001213 sk_mkdir(path.c_str());
1214 }
mtklein748ca3b2015-01-15 10:56:12 -08001215 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
1216 path.append(".");
1217 path.append(ext);
1218 }
1219
mtklein748ca3b2015-01-15 10:56:12 -08001220 if (bitmap) {
mtkleina5114d72015-08-24 13:27:01 -07001221 if (!dump_png(*bitmap, path.c_str(), result.md5.c_str())) {
mtklein748ca3b2015-01-15 10:56:12 -08001222 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
1223 return;
1224 }
1225 } else {
mtkleina5114d72015-08-24 13:27:01 -07001226 SkFILEWStream file(path.c_str());
1227 if (!file.isValid()) {
1228 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
1229 return;
1230 }
mtklein748ca3b2015-01-15 10:56:12 -08001231 if (!file.writeStream(data, len)) {
1232 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
1233 return;
1234 }
1235 }
1236 }
1237};
1238
mtklein748ca3b2015-01-15 10:56:12 -08001239/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1240
1241// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
1242
mtklein21eaf3b2016-02-08 12:39:59 -08001243static SkTDArray<skiatest::Test> gParallelTests, gSerialTests;
mtklein748ca3b2015-01-15 10:56:12 -08001244
1245static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -08001246 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -08001247 return;
1248 }
mtklein6393c062015-04-27 08:45:01 -07001249 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r; r = r->next()) {
1250 if (!in_shard()) {
1251 continue;
1252 }
halcanary87f3ba42015-01-20 09:30:20 -08001253 // Despite its name, factory() is returning a reference to
1254 // link-time static const POD data.
1255 const skiatest::Test& test = r->factory();
1256 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -08001257 continue;
1258 }
halcanary87f3ba42015-01-20 09:30:20 -08001259 if (test.needsGpu && gpu_supported()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001260 (FLAGS_gpu_threading ? gParallelTests : gSerialTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -08001261 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein21eaf3b2016-02-08 12:39:59 -08001262 gParallelTests.push(test);
mtklein82d28432015-01-15 12:46:02 -08001263 }
mtklein748ca3b2015-01-15 10:56:12 -08001264 }
1265}
1266
mtklein21eaf3b2016-02-08 12:39:59 -08001267static void run_test(skiatest::Test test) {
halcanary87f3ba42015-01-20 09:30:20 -08001268 struct : public skiatest::Reporter {
mtklein36352bf2015-03-25 18:17:31 -07001269 void reportFailed(const skiatest::Failure& failure) override {
halcanary87f3ba42015-01-20 09:30:20 -08001270 fail(failure.toString());
1271 JsonWriter::AddTestFailure(failure);
1272 }
mtklein36352bf2015-03-25 18:17:31 -07001273 bool allowExtendedTest() const override {
halcanary87f3ba42015-01-20 09:30:20 -08001274 return FLAGS_pathOpsExtended;
1275 }
mtklein36352bf2015-03-25 18:17:31 -07001276 bool verbose() const override { return FLAGS_veryVerbose; }
halcanary87f3ba42015-01-20 09:30:20 -08001277 } reporter;
djsollen824996a2015-06-12 12:06:22 -07001278
mtklein3eed7dd2016-02-24 19:07:07 -08001279 if (!FLAGS_dryRun && !is_blacklisted("_", "tests", "_", test.name)) {
mtklein21eaf3b2016-02-08 12:39:59 -08001280 start("unit", "test", "", test.name);
mtklein55e88b22015-01-21 15:50:13 -08001281 GrContextFactory factory;
mtklein21eaf3b2016-02-08 12:39:59 -08001282 test.proc(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -08001283 }
mtklein3eed7dd2016-02-24 19:07:07 -08001284 done("unit", "test", "", test.name);
mtklein748ca3b2015-01-15 10:56:12 -08001285}
1286
1287/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1288
mtkleinafae30a2016-02-24 12:28:32 -08001289DEFINE_int32(status_sec, 15, "Print status this often (and if we crash).");
1290
1291SkThread* start_status_thread() {
1292 auto thread = new SkThread([] (void*) {
1293 for (;;) {
1294 print_status();
1295 #if defined(SK_BUILD_FOR_WIN)
1296 Sleep(FLAGS_status_sec * 1000);
1297 #else
1298 sleep(FLAGS_status_sec);
1299 #endif
mtklein2e1c47e2015-03-12 07:16:56 -07001300 }
mtkleinafae30a2016-02-24 12:28:32 -08001301 });
1302 thread->start();
1303 return thread;
mtkleinde6fc2b2015-03-12 06:28:54 -07001304}
1305
caryclark83ca6282015-06-10 09:31:09 -07001306#define PORTABLE_FONT_PREFIX "Toy Liberation "
1307
1308static SkTypeface* create_from_name(const char familyName[], SkTypeface::Style style) {
1309 if (familyName && strlen(familyName) > sizeof(PORTABLE_FONT_PREFIX)
1310 && !strncmp(familyName, PORTABLE_FONT_PREFIX, sizeof(PORTABLE_FONT_PREFIX) - 1)) {
caryclark1818acb2015-07-24 12:09:25 -07001311 return sk_tool_utils::create_portable_typeface(familyName, style);
caryclark83ca6282015-06-10 09:31:09 -07001312 }
halcanary96fcdcc2015-08-27 07:41:13 -07001313 return nullptr;
caryclark83ca6282015-06-10 09:31:09 -07001314}
1315
1316#undef PORTABLE_FONT_PREFIX
1317
1318extern SkTypeface* (*gCreateTypefaceDelegate)(const char [], SkTypeface::Style );
1319
jcgregorio3b27ade2014-11-13 08:06:40 -08001320int dm_main();
caryclark17f0b6d2014-07-22 10:15:34 -07001321int dm_main() {
mtklein246ba3a2016-02-23 10:39:36 -08001322 setup_crash_handler();
mtkleinafae30a2016-02-24 12:28:32 -08001323
mtkleina483da32016-03-17 12:53:36 -07001324 if (FLAGS_verbose) {
1325 gVLog = stderr;
1326 } else if (!FLAGS_writePath.isEmpty()) {
mtklein51c8cfc2016-03-11 12:59:09 -08001327 sk_mkdir(FLAGS_writePath[0]);
1328 gVLog = freopen(SkOSPath::Join(FLAGS_writePath[0], "verbose.log").c_str(), "w", stderr);
mtkleinc41fd922016-03-08 09:01:39 -08001329 }
1330
mtklein5286f022016-01-22 08:18:14 -08001331 JsonWriter::DumpJson(); // It's handy for the bots to assume this is ~never missing.
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +00001332 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -07001333 SkTaskGroup::Enabler enabled(FLAGS_threads);
caryclark83ca6282015-06-10 09:31:09 -07001334 gCreateTypefaceDelegate = &create_from_name;
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +00001335
halcanary7d571242016-02-24 17:59:16 -08001336 {
1337 SkString testResourcePath = GetResourcePath("color_wheel.png");
1338 SkFILEStream testResource(testResourcePath.c_str());
1339 if (!testResource.isValid()) {
mtkleinc41fd922016-03-08 09:01:39 -08001340 info("Some resources are missing. Do you need to set --resourcePath?\n");
halcanary7d571242016-02-24 17:59:16 -08001341 }
1342 }
mtklein62bd1a62015-01-27 14:46:26 -08001343 gather_gold();
borenet09ed4802015-04-03 14:15:33 -07001344 gather_uninteresting_hashes();
mtklein62bd1a62015-01-27 14:46:26 -08001345
scroggo86737142016-02-03 12:19:11 -08001346 if (!gather_srcs()) {
1347 return 1;
1348 }
mtklein748ca3b2015-01-15 10:56:12 -08001349 gather_sinks();
1350 gather_tests();
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +00001351
mtklein21eaf3b2016-02-08 12:39:59 -08001352 gPending = gSrcs.count() * gSinks.count() + gParallelTests.count() + gSerialTests.count();
mtkleinc41fd922016-03-08 09:01:39 -08001353 info("%d srcs * %d sinks + %d tests == %d tasks",
1354 gSrcs.count(), gSinks.count(), gParallelTests.count() + gSerialTests.count(), gPending);
mtkleinafae30a2016-02-24 12:28:32 -08001355 SkAutoTDelete<SkThread> statusThread(start_status_thread());
mtklein748ca3b2015-01-15 10:56:12 -08001356
mtklein21eaf3b2016-02-08 12:39:59 -08001357 // Kick off as much parallel work as we can, making note of any serial work we'll need to do.
1358 SkTaskGroup parallel;
1359 SkTArray<Task> serial;
1360
1361 for (auto& sink : gSinks)
1362 for (auto& src : gSrcs) {
mtklein3eed7dd2016-02-24 19:07:07 -08001363 if (src->veto(sink->flags()) ||
1364 is_blacklisted(sink.tag.c_str(), src.tag.c_str(),
1365 src.options.c_str(), src->name().c_str())) {
mtklein15923c92016-02-29 10:14:38 -08001366 SkAutoTAcquire<SkSpinlock> lock(gMutex);
mtklein3eed7dd2016-02-24 19:07:07 -08001367 gPending--;
1368 continue;
1369 }
1370
mtklein21eaf3b2016-02-08 12:39:59 -08001371 Task task(src, sink);
1372 if (src->serial() || sink->serial()) {
1373 serial.push_back(task);
1374 } else {
1375 parallel.add([task] { Task::Run(task); });
mtklein748ca3b2015-01-15 10:56:12 -08001376 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001377 }
mtklein21eaf3b2016-02-08 12:39:59 -08001378 for (auto test : gParallelTests) {
1379 parallel.add([test] { run_test(test); });
mtklein55e88b22015-01-21 15:50:13 -08001380 }
mtklein21eaf3b2016-02-08 12:39:59 -08001381
1382 // With the parallel work running, run serial tasks and tests here on main thread.
1383 for (auto task : serial) { Task::Run(task); }
1384 for (auto test : gSerialTests) { run_test(test); }
1385
1386 // Wait for any remaining parallel work to complete (including any spun off of serial tasks).
1387 parallel.wait();
mtkleincd50bca2016-01-05 06:20:20 -08001388 gDefinitelyThreadSafeWork.wait();
1389
mtkleinda884c42016-02-24 18:00:23 -08001390 // We'd better have run everything.
1391 SkASSERT(gPending == 0);
mtkleine027f172016-02-26 15:53:06 -08001392 // Make sure we've flushed all our results to disk.
1393 JsonWriter::DumpJson();
mtkleinda884c42016-02-24 18:00:23 -08001394
mtklein748ca3b2015-01-15 10:56:12 -08001395 // At this point we're back in single-threaded land.
caryclarkf53ce802015-06-15 06:48:30 -07001396 sk_tool_utils::release_portable_typefaces();
mtklein@google.comd36522d2013-10-16 13:02:15 +00001397
mtklein748ca3b2015-01-15 10:56:12 -08001398 if (gFailures.count() > 0) {
mtkleinc41fd922016-03-08 09:01:39 -08001399 info("Failures:\n");
mtklein748ca3b2015-01-15 10:56:12 -08001400 for (int i = 0; i < gFailures.count(); i++) {
mtkleinc41fd922016-03-08 09:01:39 -08001401 info("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001402 }
mtkleinc41fd922016-03-08 09:01:39 -08001403 info("%d failures\n", gFailures.count());
mtklein748ca3b2015-01-15 10:56:12 -08001404 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001405 }
mtkleinafae30a2016-02-24 12:28:32 -08001406
1407#ifdef SK_PDF_IMAGE_STATS
halcanary7a14b312015-10-01 07:28:13 -07001408 SkPDFImageDumpStats();
mtkleinafae30a2016-02-24 12:28:32 -08001409#endif // SK_PDF_IMAGE_STATS
1410
1411 print_status();
mtkleinc41fd922016-03-08 09:01:39 -08001412 info("Finished!\n");
mtklein748ca3b2015-01-15 10:56:12 -08001413 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +00001414}
jcgregorio3b27ade2014-11-13 08:06:40 -08001415
kkinnunen179a8f52015-11-20 13:32:24 -08001416// TODO: currently many GPU tests are declared outside SK_SUPPORT_GPU guards.
1417// Thus we export the empty RunWithGPUTestContexts when SK_SUPPORT_GPU=0.
1418namespace skiatest {
1419namespace {
1420typedef void(*TestWithGrContext)(skiatest::Reporter*, GrContext*);
bsalomon3724e572016-03-30 18:56:19 -07001421typedef void(*TestWithGrContextAndGLContext)(skiatest::Reporter*, GrContext*, GLContext*);
kkinnunen179a8f52015-11-20 13:32:24 -08001422#if SK_SUPPORT_GPU
1423template<typename T>
kkinnunen34058002016-01-06 23:49:30 -08001424void call_test(T test, skiatest::Reporter* reporter, const GrContextFactory::ContextInfo& context);
kkinnunen179a8f52015-11-20 13:32:24 -08001425template<>
1426void call_test(TestWithGrContext test, skiatest::Reporter* reporter,
kkinnunen34058002016-01-06 23:49:30 -08001427 const GrContextFactory::ContextInfo& context) {
1428 test(reporter, context.fGrContext);
kkinnunen179a8f52015-11-20 13:32:24 -08001429}
1430template<>
1431void call_test(TestWithGrContextAndGLContext test, skiatest::Reporter* reporter,
kkinnunen34058002016-01-06 23:49:30 -08001432 const GrContextFactory::ContextInfo& context) {
1433 test(reporter, context.fGrContext, context.fGLContext);
kkinnunen179a8f52015-11-20 13:32:24 -08001434}
1435#endif
1436} // namespace
1437
kkinnunen179a8f52015-11-20 13:32:24 -08001438template<typename T>
1439void RunWithGPUTestContexts(T test, GPUTestContexts testContexts, Reporter* reporter,
1440 GrContextFactory* factory) {
1441#if SK_SUPPORT_GPU
kkinnunen3e980c32015-12-23 01:33:00 -08001442 // Iterate over context types, except use "native" instead of explicitly trying OpenGL and
1443 // OpenGL ES. Do not use GLES on desktop, since tests do not account for not fixing
1444 // http://skbug.com/2809
1445 GrContextFactory::GLContextType contextTypes[] = {
1446 GrContextFactory::kNative_GLContextType,
1447#if SK_ANGLE
1448#ifdef SK_BUILD_FOR_WIN
1449 GrContextFactory::kANGLE_GLContextType,
1450#endif
1451 GrContextFactory::kANGLE_GL_GLContextType,
1452#endif
1453#if SK_COMMAND_BUFFER
kkinnunenf655e932016-03-03 07:39:48 -08001454 GrContextFactory::kCommandBuffer_GLContextType,
kkinnunen3e980c32015-12-23 01:33:00 -08001455#endif
1456#if SK_MESA
1457 GrContextFactory::kMESA_GLContextType,
1458#endif
1459 GrContextFactory::kNull_GLContextType,
1460 GrContextFactory::kDebug_GLContextType,
1461 };
1462 static_assert(SK_ARRAY_COUNT(contextTypes) == GrContextFactory::kGLContextTypeCnt - 2,
1463 "Skipping unexpected GLContextType for GPU tests");
1464
1465 for (auto& contextType : contextTypes) {
kkinnunen179a8f52015-11-20 13:32:24 -08001466 int contextSelector = kNone_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001467 if (GrContextFactory::IsRenderingGLContext(contextType)) {
kkinnunen179a8f52015-11-20 13:32:24 -08001468 contextSelector |= kAllRendering_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001469 } else if (contextType == GrContextFactory::kNative_GLContextType) {
kkinnunen179a8f52015-11-20 13:32:24 -08001470 contextSelector |= kNative_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001471 } else if (contextType == GrContextFactory::kNull_GLContextType) {
kkinnunen179a8f52015-11-20 13:32:24 -08001472 contextSelector |= kNull_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001473 } else if (contextType == GrContextFactory::kDebug_GLContextType) {
kkinnunen55eeae92015-12-10 23:19:29 -08001474 contextSelector |= kDebug_GPUTestContexts;
kkinnunen179a8f52015-11-20 13:32:24 -08001475 }
1476 if ((testContexts & contextSelector) == 0) {
1477 continue;
1478 }
kkinnunen34058002016-01-06 23:49:30 -08001479 GrContextFactory::ContextInfo context = factory->getContextInfo(contextType);
1480 if (context.fGrContext) {
kkinnunen5219fd92015-12-10 06:28:13 -08001481 call_test(test, reporter, context);
1482 }
kkinnunen34058002016-01-06 23:49:30 -08001483 context = factory->getContextInfo(contextType,
1484 GrContextFactory::kEnableNVPR_GLContextOptions);
1485 if (context.fGrContext) {
kkinnunen179a8f52015-11-20 13:32:24 -08001486 call_test(test, reporter, context);
1487 }
1488 }
1489#endif
1490}
1491
1492template
1493void RunWithGPUTestContexts<TestWithGrContext>(TestWithGrContext test,
1494 GPUTestContexts testContexts,
1495 Reporter* reporter,
1496 GrContextFactory* factory);
1497template
1498void RunWithGPUTestContexts<TestWithGrContextAndGLContext>(TestWithGrContextAndGLContext test,
1499 GPUTestContexts testContexts,
1500 Reporter* reporter,
1501 GrContextFactory* factory);
1502} // namespace skiatest
1503
borenet48087572015-04-02 12:16:36 -07001504#if !defined(SK_BUILD_FOR_IOS)
jcgregorio3b27ade2014-11-13 08:06:40 -08001505int main(int argc, char** argv) {
1506 SkCommandLineFlags::Parse(argc, argv);
1507 return dm_main();
1508}
1509#endif