blob: 69044080b58d3cbdcfe71cbe598226a03c38f517 [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"
msarett888dc162016-05-23 10:21:17 -070018#include "SkColorSpace.h"
caryclark17f0b6d2014-07-22 10:15:34 -070019#include "SkCommonFlags.h"
kkinnunen3e980c32015-12-23 01:33:00 -080020#include "SkCommonFlagsConfig.h"
brianosman3c579dc2016-04-19 09:18:11 -070021#include "SkData.h"
caryclark83ca6282015-06-10 09:31:09 -070022#include "SkFontMgr.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +000023#include "SkGraphics.h"
mtklein27c3fdd2016-02-26 14:43:21 -080024#include "SkHalf.h"
halcanary4dbbd042016-06-07 17:21:10 -070025#include "SkLeanWindows.h"
mtklein748ca3b2015-01-15 10:56:12 -080026#include "SkMD5.h"
mtklein1b249332015-07-07 12:21:21 -070027#include "SkMutex.h"
mtklein1d0f1642014-09-08 08:05:18 -070028#include "SkOSFile.h"
mtklein27c3fdd2016-02-26 14:43:21 -080029#include "SkPM4fPriv.h"
mtklein246ba3a2016-02-23 10:39:36 -080030#include "SkSpinlock.h"
mtkleina82f5622015-02-20 12:30:19 -080031#include "SkTHash.h"
mtklein406654b2014-09-03 15:34:37 -070032#include "SkTaskGroup.h"
mtkleinde6fc2b2015-03-12 06:28:54 -070033#include "SkThreadUtils.h"
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +000034#include "Test.h"
mtklein748ca3b2015-01-15 10:56:12 -080035#include "Timer.h"
brianosman3c579dc2016-04-19 09:18:11 -070036#include "picture_utils.h"
caryclark83ca6282015-06-10 09:31:09 -070037#include "sk_tool_utils.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +000038
halcanary7a14b312015-10-01 07:28:13 -070039#ifdef SK_PDF_IMAGE_STATS
40extern void SkPDFImageDumpStats();
41#endif
42
mtkleina5114d72015-08-24 13:27:01 -070043#include "png.h"
44
bungeman60e0fee2015-08-26 05:15:46 -070045#include <stdlib.h>
46
scroggo27a58342015-10-28 08:56:41 -070047#ifndef SK_BUILD_FOR_WIN32
48 #include <unistd.h>
49#endif
50
djsollen54416de2015-04-03 07:24:48 -070051DEFINE_string(src, "tests gm skp image", "Source types to test.");
mtklein748ca3b2015-01-15 10:56:12 -080052DEFINE_bool(nameByHash, false,
53 "If true, write to FLAGS_writePath[0]/<hash>.png instead of "
djsollen54416de2015-04-03 07:24:48 -070054 "to FLAGS_writePath[0]/<config>/<sourceType>/<sourceOptions>/<name>.png");
mtklein748ca3b2015-01-15 10:56:12 -080055DEFINE_bool2(pathOpsExtended, x, false, "Run extended pathOps tests.");
mtkleind603b222015-02-17 11:13:33 -080056DEFINE_string(matrix, "1 0 0 1",
57 "2x2 scale+skew matrix to apply or upright when using "
58 "'matrix' or 'upright' in config.");
mtklein82d28432015-01-15 12:46:02 -080059DEFINE_bool(gpu_threading, false, "Allow GPU work to run on multiple threads?");
mtklein@google.comd36522d2013-10-16 13:02:15 +000060
mtkleina2ef6422015-01-15 13:44:22 -080061DEFINE_string(blacklist, "",
djsollen54416de2015-04-03 07:24:48 -070062 "Space-separated config/src/srcOptions/name quadruples to blacklist. '_' matches anything. E.g. \n"
63 "'--blacklist gpu skp _ _' will blacklist all SKPs drawn into the gpu config.\n"
64 "'--blacklist gpu skp _ _ 8888 gm _ aarects' will also blacklist the aarects GM on 8888.");
mtkleina2ef6422015-01-15 13:44:22 -080065
mtklein62bd1a62015-01-27 14:46:26 -080066DEFINE_string2(readPath, r, "", "If set check for equality with golden results in this directory.");
67
borenet09ed4802015-04-03 14:15:33 -070068DEFINE_string(uninterestingHashesFile, "",
69 "File containing a list of uninteresting hashes. If a result hashes to something in "
70 "this list, no image is written for that result.");
71
mtklein6393c062015-04-27 08:45:01 -070072DEFINE_int32(shards, 1, "We're splitting source data into this many shards.");
73DEFINE_int32(shard, 0, "Which shard do I run?");
scroggoe4499842016-02-25 11:03:47 -080074DEFINE_bool(simpleCodec, false, "Only decode images to native scale");
mtklein6393c062015-04-27 08:45:01 -070075
halcanary45420a92016-06-02 12:41:14 -070076DEFINE_string(mskps, "", "Directory to read mskps from, or a single mskp file.");
77
mtklein748ca3b2015-01-15 10:56:12 -080078using namespace DM;
bsalomon3724e572016-03-30 18:56:19 -070079using sk_gpu_test::GrContextFactory;
bsalomon273c0f52016-03-31 10:59:06 -070080using sk_gpu_test::GLTestContext;
bsalomonf2f1c172016-04-05 12:59:06 -070081using sk_gpu_test::ContextInfo;
mtklein@google.comd36522d2013-10-16 13:02:15 +000082
mtklein748ca3b2015-01-15 10:56:12 -080083/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
84
benjaminwagnerec4d4d72016-03-25 12:59:53 -070085static const double kStartMs = SkTime::GetMSecs();
mtkleinc41fd922016-03-08 09:01:39 -080086
87static FILE* gVLog;
88
89template <typename... Args>
90static void vlog(const char* fmt, Args&&... args) {
91 if (gVLog) {
92 fprintf(gVLog, "%s\t", HumanizeMs(SkTime::GetMSecs() - kStartMs).c_str());
93 fprintf(gVLog, fmt, args...);
94 fflush(gVLog);
95 }
96}
97
98template <typename... Args>
99static void info(const char* fmt, Args&&... args) {
100 vlog(fmt, args...);
101 if (!FLAGS_quiet) {
102 printf(fmt, args...);
103 }
104}
105static void info(const char* fmt) {
106 if (!FLAGS_quiet) {
107 printf("%s", fmt); // Clang warns printf(fmt) is insecure.
108 }
109}
110
reed086eea92016-05-04 17:12:46 -0700111SK_DECLARE_STATIC_MUTEX(gFailuresMutex);
mtklein748ca3b2015-01-15 10:56:12 -0800112static SkTArray<SkString> gFailures;
113
mtklein3eed7dd2016-02-24 19:07:07 -0800114static void fail(const SkString& err) {
mtklein748ca3b2015-01-15 10:56:12 -0800115 SkAutoMutexAcquire lock(gFailuresMutex);
116 SkDebugf("\n\nFAILURE: %s\n\n", err.c_str());
117 gFailures.push_back(err);
halcanary9e398f72015-01-10 11:18:04 -0800118}
119
mtkleinb37cb412015-03-18 05:27:14 -0700120
mtklein246ba3a2016-02-23 10:39:36 -0800121// We use a spinlock to make locking this in a signal handler _somewhat_ safe.
mtklein15923c92016-02-29 10:14:38 -0800122static SkSpinlock gMutex;
mtklein3eed7dd2016-02-24 19:07:07 -0800123static int32_t gPending;
124static SkTArray<SkString> gRunning;
mtklein748ca3b2015-01-15 10:56:12 -0800125
mtklein3eed7dd2016-02-24 19:07:07 -0800126static void done(const char* config, const char* src, const char* srcOptions, const char* name) {
127 SkString id = SkStringPrintf("%s %s %s %s", config, src, srcOptions, name);
mtkleinc41fd922016-03-08 09:01:39 -0800128 vlog("done %s\n", id.c_str());
mtkleinafae30a2016-02-24 12:28:32 -0800129 int pending;
mtkleinb37cb412015-03-18 05:27:14 -0700130 {
mtkleinf10637f2016-06-10 13:56:35 -0700131 SkAutoMutexAcquire lock(gMutex);
mtkleinb37cb412015-03-18 05:27:14 -0700132 for (int i = 0; i < gRunning.count(); i++) {
133 if (gRunning[i] == id) {
134 gRunning.removeShuffle(i);
135 break;
136 }
137 }
mtkleinafae30a2016-02-24 12:28:32 -0800138 pending = --gPending;
reed50bc0512015-05-19 14:13:31 -0700139 }
mtkleina17241b2015-01-23 05:48:00 -0800140 // We write our dm.json file every once in a while in case we crash.
141 // Notice this also handles the final dm.json when pending == 0.
142 if (pending % 500 == 0) {
143 JsonWriter::DumpJson();
144 }
mtklein@google.comd36522d2013-10-16 13:02:15 +0000145}
146
mtklein3eed7dd2016-02-24 19:07:07 -0800147static void start(const char* config, const char* src, const char* srcOptions, const char* name) {
148 SkString id = SkStringPrintf("%s %s %s %s", config, src, srcOptions, name);
mtkleinc41fd922016-03-08 09:01:39 -0800149 vlog("start %s\n", id.c_str());
mtkleinf10637f2016-06-10 13:56:35 -0700150 SkAutoMutexAcquire lock(gMutex);
mtkleinb37cb412015-03-18 05:27:14 -0700151 gRunning.push_back(id);
152}
153
mtkleinafae30a2016-02-24 12:28:32 -0800154static void print_status() {
mtkleinafae30a2016-02-24 12:28:32 -0800155 int curr = sk_tools::getCurrResidentSetSizeMB(),
156 peak = sk_tools::getMaxResidentSetSizeMB();
mtkleinc41fd922016-03-08 09:01:39 -0800157 SkString elapsed = HumanizeMs(SkTime::GetMSecs() - kStartMs);
mtkleinafae30a2016-02-24 12:28:32 -0800158
mtkleinf10637f2016-06-10 13:56:35 -0700159 SkAutoMutexAcquire lock(gMutex);
mtkleinc41fd922016-03-08 09:01:39 -0800160 info("\n%s elapsed, %d active, %d queued, %dMB RAM, %dMB peak\n",
161 elapsed.c_str(), gRunning.count(), gPending - gRunning.count(), curr, peak);
mtkleinafae30a2016-02-24 12:28:32 -0800162 for (auto& task : gRunning) {
mtkleinc41fd922016-03-08 09:01:39 -0800163 info("\t%s\n", task.c_str());
mtkleinafae30a2016-02-24 12:28:32 -0800164 }
165}
166
mtklein246ba3a2016-02-23 10:39:36 -0800167#if defined(SK_BUILD_FOR_WIN32)
mtklein5b64dab2016-06-09 08:59:48 -0700168 static LONG WINAPI crash_handler(EXCEPTION_POINTERS* e) {
mtkleinf8557292016-02-29 06:35:28 -0800169 static const struct {
170 const char* name;
robertphillips75467862016-03-01 14:10:23 -0800171 DWORD code;
mtkleinf8557292016-02-29 06:35:28 -0800172 } kExceptions[] = {
173 #define _(E) {#E, E}
174 _(EXCEPTION_ACCESS_VIOLATION),
175 _(EXCEPTION_BREAKPOINT),
176 _(EXCEPTION_INT_DIVIDE_BY_ZERO),
177 _(EXCEPTION_STACK_OVERFLOW),
178 // TODO: more?
179 #undef _
180 };
181
mtkleinf10637f2016-06-10 13:56:35 -0700182 SkAutoMutexAcquire lock(gMutex);
mtklein5b64dab2016-06-09 08:59:48 -0700183
184 const DWORD code = e->ExceptionRecord->ExceptionCode;
185 info("\nCaught exception %u", code);
186 for (const auto& exception : kExceptions) {
187 if (exception.code == code) {
188 info(" %s", exception.name);
mtkleinf8557292016-02-29 06:35:28 -0800189 }
mtkleinf8557292016-02-29 06:35:28 -0800190 }
mtklein5b64dab2016-06-09 08:59:48 -0700191 info(", was running:\n");
192 for (auto& task : gRunning) {
193 info("\t%s\n", task.c_str());
194 }
195 fflush(stdout);
196
mtkleinf8557292016-02-29 06:35:28 -0800197 // Execute default exception handler... hopefully, exit.
198 return EXCEPTION_EXECUTE_HANDLER;
mtklein246ba3a2016-02-23 10:39:36 -0800199 }
mtklein5b64dab2016-06-09 08:59:48 -0700200 static void setup_crash_handler() { SetUnhandledExceptionFilter(crash_handler); }
mtklein246ba3a2016-02-23 10:39:36 -0800201
mtklein819ab102016-02-29 17:02:52 -0800202#elif !defined(SK_BUILD_FOR_ANDROID)
203 #include <execinfo.h>
mtklein246ba3a2016-02-23 10:39:36 -0800204 #include <signal.h>
mtklein819ab102016-02-29 17:02:52 -0800205 #include <stdlib.h>
mtkleinf8557292016-02-29 06:35:28 -0800206
mtklein5b64dab2016-06-09 08:59:48 -0700207 static void crash_handler(int sig) {
mtkleinf10637f2016-06-10 13:56:35 -0700208 SkAutoMutexAcquire lock(gMutex);
mtklein5b64dab2016-06-09 08:59:48 -0700209
210 info("\nCaught signal %d [%s], was running:\n", sig, strsignal(sig));
211 for (auto& task : gRunning) {
212 info("\t%s\n", task.c_str());
213 }
214
215 void* stack[64];
216 int count = backtrace(stack, SK_ARRAY_COUNT(stack));
217 char** symbols = backtrace_symbols(stack, count);
218 info("\nStack trace:\n");
219 for (int i = 0; i < count; i++) {
220 info(" %s\n", symbols[i]);
221 }
222 fflush(stdout);
223
224 _Exit(sig);
225 }
226
mtklein246ba3a2016-02-23 10:39:36 -0800227 static void setup_crash_handler() {
228 const int kSignals[] = { SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGSEGV };
229 for (int sig : kSignals) {
mtklein5b64dab2016-06-09 08:59:48 -0700230 signal(sig, crash_handler);
mtklein246ba3a2016-02-23 10:39:36 -0800231 }
232 }
mtklein819ab102016-02-29 17:02:52 -0800233
234#else // Android
235 static void setup_crash_handler() {}
mtklein246ba3a2016-02-23 10:39:36 -0800236#endif
237
mtklein748ca3b2015-01-15 10:56:12 -0800238/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
mtklein114c3cd2015-01-15 10:15:02 -0800239
mtklein62bd1a62015-01-27 14:46:26 -0800240struct Gold : public SkString {
mtkleina82f5622015-02-20 12:30:19 -0800241 Gold() : SkString("") {}
mtklein3eed7dd2016-02-24 19:07:07 -0800242 Gold(const SkString& sink, const SkString& src,
243 const SkString& srcOptions, const SkString& name,
244 const SkString& md5)
mtklein62bd1a62015-01-27 14:46:26 -0800245 : SkString("") {
246 this->append(sink);
247 this->append(src);
djsollen54416de2015-04-03 07:24:48 -0700248 this->append(srcOptions);
mtklein62bd1a62015-01-27 14:46:26 -0800249 this->append(name);
250 this->append(md5);
mtklein62bd1a62015-01-27 14:46:26 -0800251 }
mtkleinc8d1dd42015-10-15 12:23:01 -0700252 struct Hash {
253 uint32_t operator()(const Gold& g) const {
254 return SkGoodHash()((const SkString&)g);
255 }
256 };
mtklein62bd1a62015-01-27 14:46:26 -0800257};
mtkleina82f5622015-02-20 12:30:19 -0800258static SkTHashSet<Gold, Gold::Hash> gGold;
mtklein62bd1a62015-01-27 14:46:26 -0800259
260static void add_gold(JsonWriter::BitmapResult r) {
djsollen54416de2015-04-03 07:24:48 -0700261 gGold.add(Gold(r.config, r.sourceType, r.sourceOptions, r.name, r.md5));
mtklein62bd1a62015-01-27 14:46:26 -0800262}
263
264static void gather_gold() {
265 if (!FLAGS_readPath.isEmpty()) {
266 SkString path(FLAGS_readPath[0]);
267 path.append("/dm.json");
268 if (!JsonWriter::ReadJson(path.c_str(), add_gold)) {
269 fail(SkStringPrintf("Couldn't read %s for golden results.", path.c_str()));
270 }
271 }
272}
273
274/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
275
mtklein935f1b12016-03-16 08:37:19 -0700276#if defined(SK_BUILD_FOR_WIN32)
277 static const char* kNewline = "\r\n";
278#else
279 static const char* kNewline = "\n";
280#endif
281
borenet09ed4802015-04-03 14:15:33 -0700282static SkTHashSet<SkString> gUninterestingHashes;
283
284static void gather_uninteresting_hashes() {
285 if (!FLAGS_uninterestingHashesFile.isEmpty()) {
286 SkAutoTUnref<SkData> data(SkData::NewFromFileName(FLAGS_uninterestingHashesFile[0]));
mtkleincc334b32015-09-22 11:43:53 -0700287 if (!data) {
mtkleinc41fd922016-03-08 09:01:39 -0800288 info("WARNING: unable to read uninteresting hashes from %s\n",
289 FLAGS_uninterestingHashesFile[0]);
mtkleincc334b32015-09-22 11:43:53 -0700290 return;
291 }
borenet09ed4802015-04-03 14:15:33 -0700292 SkTArray<SkString> hashes;
mtklein935f1b12016-03-16 08:37:19 -0700293 SkStrSplit((const char*)data->data(), kNewline, &hashes);
borenet09ed4802015-04-03 14:15:33 -0700294 for (const SkString& hash : hashes) {
295 gUninterestingHashes.add(hash);
296 }
mtkleinc41fd922016-03-08 09:01:39 -0800297 info("FYI: loaded %d distinct uninteresting hashes from %d lines\n",
298 gUninterestingHashes.count(), hashes.count());
borenet09ed4802015-04-03 14:15:33 -0700299 }
300}
301
302/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
303
mtkleine0effd62015-07-29 06:37:28 -0700304struct TaggedSrc : public SkAutoTDelete<Src> {
mtklein3eed7dd2016-02-24 19:07:07 -0800305 SkString tag;
306 SkString options;
mtkleine0effd62015-07-29 06:37:28 -0700307};
308
309struct TaggedSink : public SkAutoTDelete<Sink> {
kkinnunen3e980c32015-12-23 01:33:00 -0800310 SkString tag;
djsollen54416de2015-04-03 07:24:48 -0700311};
mtklein748ca3b2015-01-15 10:56:12 -0800312
313static const bool kMemcpyOK = true;
314
mtkleine0effd62015-07-29 06:37:28 -0700315static SkTArray<TaggedSrc, kMemcpyOK> gSrcs;
316static SkTArray<TaggedSink, kMemcpyOK> gSinks;
mtklein748ca3b2015-01-15 10:56:12 -0800317
mtklein6393c062015-04-27 08:45:01 -0700318static bool in_shard() {
319 static int N = 0;
320 return N++ % FLAGS_shards == FLAGS_shard;
321}
322
mtklein3eed7dd2016-02-24 19:07:07 -0800323static void push_src(const char* tag, ImplicitString options, Src* s) {
mtklein748ca3b2015-01-15 10:56:12 -0800324 SkAutoTDelete<Src> src(s);
mtklein6393c062015-04-27 08:45:01 -0700325 if (in_shard() &&
mtklein3eed7dd2016-02-24 19:07:07 -0800326 FLAGS_src.contains(tag) &&
mtklein748ca3b2015-01-15 10:56:12 -0800327 !SkCommandLineFlags::ShouldSkip(FLAGS_match, src->name().c_str())) {
mtkleine0effd62015-07-29 06:37:28 -0700328 TaggedSrc& s = gSrcs.push_back();
mtklein18300a32016-03-16 13:53:35 -0700329 s.reset(src.release());
mtklein748ca3b2015-01-15 10:56:12 -0800330 s.tag = tag;
djsollen54416de2015-04-03 07:24:48 -0700331 s.options = options;
mtklein114c3cd2015-01-15 10:15:02 -0800332 }
mtklein748ca3b2015-01-15 10:56:12 -0800333}
mtklein114c3cd2015-01-15 10:15:02 -0800334
msarett9e707a02015-09-01 14:57:57 -0700335static void push_codec_src(Path path, CodecSrc::Mode mode, CodecSrc::DstColorType dstColorType,
scroggoc5560be2016-02-03 09:42:42 -0800336 SkAlphaType dstAlphaType, float scale) {
scroggoe4499842016-02-25 11:03:47 -0800337 if (FLAGS_simpleCodec) {
338 if (mode != CodecSrc::kCodec_Mode || dstColorType != CodecSrc::kGetFromCanvas_DstColorType
339 || scale != 1.0f)
340 // Only decode in the simple case.
341 return;
342 }
msarett9e707a02015-09-01 14:57:57 -0700343 SkString folder;
344 switch (mode) {
345 case CodecSrc::kCodec_Mode:
346 folder.append("codec");
347 break;
msarettbb25b532016-01-13 09:31:39 -0800348 case CodecSrc::kCodecZeroInit_Mode:
349 folder.append("codec_zero_init");
350 break;
msarett9e707a02015-09-01 14:57:57 -0700351 case CodecSrc::kScanline_Mode:
352 folder.append("scanline");
353 break;
msarett9e707a02015-09-01 14:57:57 -0700354 case CodecSrc::kStripe_Mode:
355 folder.append("stripe");
356 break;
msarett91c22b22016-02-22 12:27:46 -0800357 case CodecSrc::kCroppedScanline_Mode:
358 folder.append("crop");
359 break;
msarett9e707a02015-09-01 14:57:57 -0700360 case CodecSrc::kSubset_Mode:
msarett36c37962015-09-02 13:20:52 -0700361 folder.append("codec_subset");
msarett9e707a02015-09-01 14:57:57 -0700362 break;
363 }
364
365 switch (dstColorType) {
366 case CodecSrc::kGrayscale_Always_DstColorType:
367 folder.append("_kGray8");
368 break;
369 case CodecSrc::kIndex8_Always_DstColorType:
370 folder.append("_kIndex8");
371 break;
msarett34e0ec42016-04-22 16:27:24 -0700372 case CodecSrc::kNonNative8888_Always_DstColorType:
373 folder.append("_kNonNative");
374 break;
msarett9e707a02015-09-01 14:57:57 -0700375 default:
376 break;
377 }
378
scroggoc5560be2016-02-03 09:42:42 -0800379 switch (dstAlphaType) {
scroggoc5560be2016-02-03 09:42:42 -0800380 case kPremul_SkAlphaType:
381 folder.append("_premul");
382 break;
383 case kUnpremul_SkAlphaType:
384 folder.append("_unpremul");
385 break;
386 default:
387 break;
388 }
389
msarett9e707a02015-09-01 14:57:57 -0700390 if (1.0f != scale) {
391 folder.appendf("_%.3f", scale);
392 }
393
scroggoc5560be2016-02-03 09:42:42 -0800394 CodecSrc* src = new CodecSrc(path, mode, dstColorType, dstAlphaType, scale);
msarett9e707a02015-09-01 14:57:57 -0700395 push_src("image", folder, src);
396}
397
scroggof8dc9df2016-05-16 09:04:13 -0700398static void push_android_codec_src(Path path, CodecSrc::DstColorType dstColorType,
399 SkAlphaType dstAlphaType, int sampleSize) {
msarett3d9d7a72015-10-21 10:27:10 -0700400 SkString folder;
scroggof8dc9df2016-05-16 09:04:13 -0700401 folder.append("scaled_codec");
msarett3d9d7a72015-10-21 10:27:10 -0700402
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;
msarett34e0ec42016-04-22 16:27:24 -0700410 case CodecSrc::kNonNative8888_Always_DstColorType:
411 folder.append("_kNonNative");
412 break;
msarett3d9d7a72015-10-21 10:27:10 -0700413 default:
414 break;
415 }
416
scroggoc5560be2016-02-03 09:42:42 -0800417 switch (dstAlphaType) {
scroggoc5560be2016-02-03 09:42:42 -0800418 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
scroggof8dc9df2016-05-16 09:04:13 -0700432 AndroidCodecSrc* src = new AndroidCodecSrc(path, 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
scroggo9366aff2016-05-20 08:38:54 -0700482 // native scaling is only supported by WEBP and JPEG
483 bool supportsNativeScaling = false;
msarett438b2ad2015-04-09 12:43:10 -0700484
msarett91c22b22016-02-22 12:27:46 -0800485 SkTArray<CodecSrc::Mode> nativeModes;
486 nativeModes.push_back(CodecSrc::kCodec_Mode);
487 nativeModes.push_back(CodecSrc::kCodecZeroInit_Mode);
msarett91c22b22016-02-22 12:27:46 -0800488 switch (codec->getEncodedFormat()) {
489 case SkEncodedFormat::kJPEG_SkEncodedFormat:
490 nativeModes.push_back(CodecSrc::kScanline_Mode);
491 nativeModes.push_back(CodecSrc::kStripe_Mode);
492 nativeModes.push_back(CodecSrc::kCroppedScanline_Mode);
scroggo9366aff2016-05-20 08:38:54 -0700493 supportsNativeScaling = true;
msarett91c22b22016-02-22 12:27:46 -0800494 break;
495 case SkEncodedFormat::kWEBP_SkEncodedFormat:
496 nativeModes.push_back(CodecSrc::kSubset_Mode);
scroggo9366aff2016-05-20 08:38:54 -0700497 supportsNativeScaling = true;
msarett91c22b22016-02-22 12:27:46 -0800498 break;
yujieqin7a307df2016-03-11 07:32:33 -0800499 case SkEncodedFormat::kDNG_SkEncodedFormat:
msarettb65e6042016-02-23 05:37:25 -0800500 break;
msarett91c22b22016-02-22 12:27:46 -0800501 default:
502 nativeModes.push_back(CodecSrc::kScanline_Mode);
msarett91c22b22016-02-22 12:27:46 -0800503 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);
msarett34e0ec42016-04-22 16:27:24 -0700508 colorTypes.push_back(CodecSrc::kNonNative8888_Always_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700509 switch (codec->getInfo().colorType()) {
510 case kGray_8_SkColorType:
msarett91c22b22016-02-22 12:27:46 -0800511 colorTypes.push_back(CodecSrc::kGrayscale_Always_DstColorType);
msarett55f7bdd2016-02-16 13:24:54 -0800512 if (kWBMP_SkEncodedFormat == codec->getEncodedFormat()) {
msarett91c22b22016-02-22 12:27:46 -0800513 colorTypes.push_back(CodecSrc::kIndex8_Always_DstColorType);
msarett55f7bdd2016-02-16 13:24:54 -0800514 }
msarett36c37962015-09-02 13:20:52 -0700515 break;
516 case kIndex_8_SkColorType:
msarett91c22b22016-02-22 12:27:46 -0800517 colorTypes.push_back(CodecSrc::kIndex8_Always_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700518 break;
519 default:
msarett36c37962015-09-02 13:20:52 -0700520 break;
521 }
msarett9e707a02015-09-01 14:57:57 -0700522
scroggoc5560be2016-02-03 09:42:42 -0800523 SkTArray<SkAlphaType> alphaModes;
524 alphaModes.push_back(kPremul_SkAlphaType);
scroggof2c96a22016-05-20 11:08:27 -0700525 if (codec->getInfo().alphaType() != kOpaque_SkAlphaType) {
526 alphaModes.push_back(kUnpremul_SkAlphaType);
scroggoc5560be2016-02-03 09:42:42 -0800527 }
msarettb714fb02016-01-22 14:46:42 -0800528
529 for (CodecSrc::Mode mode : nativeModes) {
scroggo9366aff2016-05-20 08:38:54 -0700530 for (CodecSrc::DstColorType colorType : colorTypes) {
531 for (SkAlphaType alphaType : alphaModes) {
scroggof2c96a22016-05-20 11:08:27 -0700532 // Only test kCroppedScanline_Mode when the alpha type is premul. The test is
scroggo9366aff2016-05-20 08:38:54 -0700533 // slow and won't be interestingly different with different alpha types.
534 if (CodecSrc::kCroppedScanline_Mode == mode &&
scroggof2c96a22016-05-20 11:08:27 -0700535 kPremul_SkAlphaType != alphaType) {
scroggo9366aff2016-05-20 08:38:54 -0700536 continue;
537 }
msarett91c22b22016-02-22 12:27:46 -0800538
scroggo9366aff2016-05-20 08:38:54 -0700539 push_codec_src(path, mode, colorType, alphaType, 1.0f);
msarettf3dc1882016-04-26 13:06:38 -0700540
scroggo9366aff2016-05-20 08:38:54 -0700541 // Skip kNonNative on different native scales. It won't be interestingly
542 // different.
543 if (supportsNativeScaling &&
544 CodecSrc::kNonNative8888_Always_DstColorType == colorType) {
545 // Native Scales
546 // SkJpegCodec natively supports scaling to the following:
547 for (auto scale : { 0.125f, 0.25f, 0.375f, 0.5f, 0.625f, 0.750f, 0.875f }) {
548 push_codec_src(path, mode, colorType, alphaType, scale);
549 }
scroggoc5560be2016-02-03 09:42:42 -0800550 }
msarett9e707a02015-09-01 14:57:57 -0700551 }
552 }
msarett0a242972015-06-11 14:27:27 -0700553 }
msarett36c37962015-09-02 13:20:52 -0700554
scroggoe4499842016-02-25 11:03:47 -0800555 if (FLAGS_simpleCodec) {
556 return;
557 }
558
msarett3d9d7a72015-10-21 10:27:10 -0700559 const int sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
msarett36c37962015-09-02 13:20:52 -0700560
msarett3d9d7a72015-10-21 10:27:10 -0700561 for (int sampleSize : sampleSizes) {
msarett91c22b22016-02-22 12:27:46 -0800562 for (CodecSrc::DstColorType colorType : colorTypes) {
scroggoc5560be2016-02-03 09:42:42 -0800563 for (SkAlphaType alphaType : alphaModes) {
msarettf3dc1882016-04-26 13:06:38 -0700564 // We can exercise all of the kNonNative support code in the swizzler with just a
565 // few sample sizes. Skip the rest.
566 if (CodecSrc::kNonNative8888_Always_DstColorType == colorType && sampleSize > 3) {
567 continue;
568 }
569
scroggof8dc9df2016-05-16 09:04:13 -0700570 push_android_codec_src(path, colorType, alphaType, sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700571 }
msarett36c37962015-09-02 13:20:52 -0700572 }
573 }
msarett18976312016-03-09 14:20:58 -0800574
575 static const char* const rawExts[] = {
576 "arw", "cr2", "dng", "nef", "nrw", "orf", "raf", "rw2", "pef", "srw",
577 "ARW", "CR2", "DNG", "NEF", "NRW", "ORF", "RAF", "RW2", "PEF", "SRW",
578 };
579
580 // There is not currently a reason to test RAW images on image generator.
581 // If we want to enable these tests, we will need to fix skbug.com/5079.
582 for (const char* ext : rawExts) {
583 if (path.endsWith(ext)) {
584 return;
585 }
586 }
587
588 // Push image generator GPU test.
brianosmanc9ced392016-03-24 07:27:43 -0700589 push_image_gen_src(path, ImageGenSrc::kCodec_Mode, codec->getInfo().alphaType(), true);
msarett18976312016-03-09 14:20:58 -0800590
591 // Push image generator CPU tests.
592 for (SkAlphaType alphaType : alphaModes) {
593 push_image_gen_src(path, ImageGenSrc::kCodec_Mode, alphaType, false);
594
595#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
596 if (kWEBP_SkEncodedFormat != codec->getEncodedFormat() &&
597 kWBMP_SkEncodedFormat != codec->getEncodedFormat() &&
598 kUnpremul_SkAlphaType != alphaType)
599 {
600 push_image_gen_src(path, ImageGenSrc::kPlatform_Mode, alphaType, false);
601 }
msarettfc0b6d12016-03-17 13:50:17 -0700602#elif defined(SK_BUILD_FOR_WIN)
603 if (kWEBP_SkEncodedFormat != codec->getEncodedFormat() &&
604 kWBMP_SkEncodedFormat != codec->getEncodedFormat())
605 {
606 push_image_gen_src(path, ImageGenSrc::kPlatform_Mode, alphaType, false);
607 }
msarett18976312016-03-09 14:20:58 -0800608#endif
609 }
msarett438b2ad2015-04-09 12:43:10 -0700610}
611
msarettd1227a72016-05-18 06:23:57 -0700612static void push_brd_src(Path path, CodecSrc::DstColorType dstColorType, BRDSrc::Mode mode,
613 uint32_t sampleSize) {
614 SkString folder("brd_android_codec");
msaretta5783ae2015-09-08 15:35:32 -0700615 switch (mode) {
616 case BRDSrc::kFullImage_Mode:
617 break;
618 case BRDSrc::kDivisor_Mode:
619 folder.append("_divisor");
620 break;
621 default:
622 SkASSERT(false);
623 return;
624 }
625
626 switch (dstColorType) {
627 case CodecSrc::kGetFromCanvas_DstColorType:
628 break;
629 case CodecSrc::kIndex8_Always_DstColorType:
630 folder.append("_kIndex");
631 break;
632 case CodecSrc::kGrayscale_Always_DstColorType:
633 folder.append("_kGray");
634 break;
635 default:
636 SkASSERT(false);
637 return;
638 }
639
640 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800641 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700642 }
643
msarettd1227a72016-05-18 06:23:57 -0700644 BRDSrc* src = new BRDSrc(path, mode, dstColorType, sampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700645 push_src("image", folder, src);
646}
647
648static void push_brd_srcs(Path path) {
scroggo24a9bd72016-05-20 06:02:42 -0700649 // Only run Index8 and grayscale to one sampleSize and Mode. Though interesting
650 // to test these color types, they should not reveal anything across various
651 // sampleSizes and Modes
652 for (auto type : { CodecSrc::kIndex8_Always_DstColorType,
653 CodecSrc::kGrayscale_Always_DstColorType }) {
654 // Arbitrarily choose Mode and sampleSize.
655 push_brd_src(path, type, BRDSrc::kFullImage_Mode, 2);
656 }
657
658
scroggo501b7342015-11-03 07:55:11 -0800659 // Test on a variety of sampleSizes, making sure to include:
660 // - 2, 4, and 8, which are natively supported by jpeg
661 // - multiples of 2 which are not divisible by 4 (analogous for 4)
662 // - larger powers of two, since BRD clients generally use powers of 2
663 // We will only produce output for the larger sizes on large images.
664 const uint32_t sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 24, 32, 64 };
msarett6efbe052015-09-11 09:01:16 -0700665
msaretta5783ae2015-09-08 15:35:32 -0700666 const BRDSrc::Mode modes[] = {
msarett26ad17b2015-10-22 07:29:19 -0700667 BRDSrc::kFullImage_Mode,
668 BRDSrc::kDivisor_Mode,
msaretta5783ae2015-09-08 15:35:32 -0700669 };
670
msarettd1227a72016-05-18 06:23:57 -0700671 for (uint32_t sampleSize : sampleSizes) {
scroggo24a9bd72016-05-20 06:02:42 -0700672 for (BRDSrc::Mode mode : modes) {
673 push_brd_src(path, CodecSrc::kGetFromCanvas_DstColorType, mode, sampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700674 }
675 }
676}
677
678static bool brd_supported(const char* ext) {
msarett8c8f22a2015-04-01 06:58:48 -0700679 static const char* const exts[] = {
msaretta5783ae2015-09-08 15:35:32 -0700680 "jpg", "jpeg", "png", "webp",
681 "JPG", "JPEG", "PNG", "WEBP",
msarett8c8f22a2015-04-01 06:58:48 -0700682 };
683
684 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
685 if (0 == strcmp(exts[i], ext)) {
686 return true;
687 }
688 }
689 return false;
scroggo9b77ddd2015-03-19 06:03:39 -0700690}
691
scroggo86737142016-02-03 12:19:11 -0800692static bool gather_srcs() {
mtklein748ca3b2015-01-15 10:56:12 -0800693 for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) {
djsollen54416de2015-04-03 07:24:48 -0700694 push_src("gm", "", new GMSrc(r->factory()));
mtklein748ca3b2015-01-15 10:56:12 -0800695 }
halcanaryfc37ad12015-01-30 07:31:19 -0800696 for (int i = 0; i < FLAGS_skps.count(); i++) {
697 const char* path = FLAGS_skps[i];
698 if (sk_isdir(path)) {
699 SkOSFile::Iter it(path, "skp");
700 for (SkString file; it.next(&file); ) {
djsollen54416de2015-04-03 07:24:48 -0700701 push_src("skp", "", new SKPSrc(SkOSPath::Join(path, file.c_str())));
halcanaryfc37ad12015-01-30 07:31:19 -0800702 }
703 } else {
djsollen54416de2015-04-03 07:24:48 -0700704 push_src("skp", "", new SKPSrc(path));
mtklein114c3cd2015-01-15 10:15:02 -0800705 }
706 }
scroggo86737142016-02-03 12:19:11 -0800707
halcanary45420a92016-06-02 12:41:14 -0700708 for (int i = 0; i < FLAGS_mskps.count(); i++) {
709 const char* path = FLAGS_mskps[i];
710 if (sk_isdir(path)) {
711 SkOSFile::Iter it(path, "mskp");
712 for (SkString file; it.next(&file);) {
713 push_src("mskp", "",
714 new MSKPSrc(SkOSPath::Join(path, file.c_str())));
715 }
716 } else {
717 push_src("mskp", "", new MSKPSrc(path));
718 }
719 }
720
scroggo86737142016-02-03 12:19:11 -0800721 SkTArray<SkString> images;
msarett69deca82016-04-29 09:38:40 -0700722 if (!CollectImages(FLAGS_images, &images)) {
scroggo86737142016-02-03 12:19:11 -0800723 return false;
724 }
725
726 for (auto image : images) {
727 push_codec_srcs(image);
scroggoe4499842016-02-25 11:03:47 -0800728 if (FLAGS_simpleCodec) {
729 continue;
730 }
731
mtklein21eaf3b2016-02-08 12:39:59 -0800732 const char* ext = strrchr(image.c_str(), '.');
733 if (ext && brd_supported(ext+1)) {
scroggo86737142016-02-03 12:19:11 -0800734 push_brd_srcs(image);
mtklein709d2c32015-01-15 08:30:25 -0800735 }
mtklein709d2c32015-01-15 08:30:25 -0800736 }
scroggo86737142016-02-03 12:19:11 -0800737
msarett69deca82016-04-29 09:38:40 -0700738 SkTArray<SkString> colorImages;
739 if (!CollectImages(FLAGS_colorImages, &colorImages)) {
740 return false;
741 }
742
743 for (auto colorImage : colorImages) {
msarett9876ac52016-06-01 14:47:18 -0700744 ColorCodecSrc* src = new ColorCodecSrc(colorImage, ColorCodecSrc::kBaseline_Mode);
msarett69deca82016-04-29 09:38:40 -0700745 push_src("image", "color_codec_baseline", src);
msarett888dc162016-05-23 10:21:17 -0700746
msarett9876ac52016-06-01 14:47:18 -0700747 src = new ColorCodecSrc(colorImage, ColorCodecSrc::kDst_HPZR30w_Mode);
msarett888dc162016-05-23 10:21:17 -0700748 push_src("image", "color_codec_HPZR30w", src);
msarett9876ac52016-06-01 14:47:18 -0700749
msarett469f1c52016-06-06 08:20:37 -0700750#if defined(SK_TEST_QCMS)
msarett9876ac52016-06-01 14:47:18 -0700751 src = new ColorCodecSrc(colorImage, ColorCodecSrc::kQCMS_HPZR30w_Mode);
752 push_src("image", "color_codec_QCMS_HPZR30w", src);
753#endif
msarett69deca82016-04-29 09:38:40 -0700754 }
755
scroggo86737142016-02-03 12:19:11 -0800756 return true;
mtklein709d2c32015-01-15 08:30:25 -0800757}
758
kkinnunen3e980c32015-12-23 01:33:00 -0800759static void push_sink(const SkCommandLineConfig& config, Sink* s) {
rmistry0f515bd2015-12-22 10:22:26 -0800760 SkAutoTDelete<Sink> sink(s);
kkinnunen3e980c32015-12-23 01:33:00 -0800761
mtkleine0effd62015-07-29 06:37:28 -0700762 // Try a simple Src as a canary. If it fails, skip this sink.
mtklein748ca3b2015-01-15 10:56:12 -0800763 struct : public Src {
mtkleine0effd62015-07-29 06:37:28 -0700764 Error draw(SkCanvas* c) const override {
765 c->drawRect(SkRect::MakeWH(1,1), SkPaint());
766 return "";
767 }
mtklein36352bf2015-03-25 18:17:31 -0700768 SkISize size() const override { return SkISize::Make(16, 16); }
mtkleine0effd62015-07-29 06:37:28 -0700769 Name name() const override { return "justOneRect"; }
770 } justOneRect;
mtklein114c3cd2015-01-15 10:15:02 -0800771
mtklein748ca3b2015-01-15 10:56:12 -0800772 SkBitmap bitmap;
773 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800774 SkString log;
mtkleine0effd62015-07-29 06:37:28 -0700775 Error err = sink->draw(justOneRect, &bitmap, &stream, &log);
mtklein4089ef72015-03-05 08:40:28 -0800776 if (err.isFatal()) {
mtkleinc41fd922016-03-08 09:01:39 -0800777 info("Could not run %s: %s\n", config.getTag().c_str(), err.c_str());
mtklein05641a52015-04-21 10:49:13 -0700778 exit(1);
mtklein114c3cd2015-01-15 10:15:02 -0800779 }
780
mtkleine0effd62015-07-29 06:37:28 -0700781 TaggedSink& ts = gSinks.push_back();
mtklein18300a32016-03-16 13:53:35 -0700782 ts.reset(sink.release());
kkinnunen3e980c32015-12-23 01:33:00 -0800783 ts.tag = config.getTag();
mtklein748ca3b2015-01-15 10:56:12 -0800784}
785
786static bool gpu_supported() {
787#if SK_SUPPORT_GPU
788 return FLAGS_gpu;
789#else
790 return false;
791#endif
792}
kkinnunen9ebc3f02015-12-21 23:48:13 -0800793
kkinnunen3e980c32015-12-23 01:33:00 -0800794static Sink* create_sink(const SkCommandLineConfig* config) {
795#if SK_SUPPORT_GPU
796 if (gpu_supported()) {
797 if (const SkCommandLineConfigGpu* gpuConfig = config->asConfigGpu()) {
bsalomon85b4b532016-04-05 11:06:27 -0700798 GrContextFactory::ContextType contextType = gpuConfig->getContextType();
799 GrContextFactory::ContextOptions contextOptions =
800 GrContextFactory::kNone_ContextOptions;
kkinnunen3e980c32015-12-23 01:33:00 -0800801 if (gpuConfig->getUseNVPR()) {
bsalomon85b4b532016-04-05 11:06:27 -0700802 contextOptions = static_cast<GrContextFactory::ContextOptions>(
803 contextOptions | GrContextFactory::kEnableNVPR_ContextOptions);
kkinnunen3e980c32015-12-23 01:33:00 -0800804 }
brianosmanb109b8c2016-06-16 13:03:24 -0700805 if (SkColorAndColorSpaceAreGammaCorrect(gpuConfig->getColorType(),
806 gpuConfig->getColorSpace())) {
bsalomon85b4b532016-04-05 11:06:27 -0700807 contextOptions = static_cast<GrContextFactory::ContextOptions>(
808 contextOptions | GrContextFactory::kRequireSRGBSupport_ContextOptions);
brianosman61d3b082016-03-30 11:19:36 -0700809 }
kkinnunen3e980c32015-12-23 01:33:00 -0800810 GrContextFactory testFactory;
811 if (!testFactory.get(contextType, contextOptions)) {
mtkleinc41fd922016-03-08 09:01:39 -0800812 info("WARNING: can not create GPU context for config '%s'. "
813 "GM tests will be skipped.\n", gpuConfig->getTag().c_str());
kkinnunen3e980c32015-12-23 01:33:00 -0800814 return nullptr;
815 }
816 return new GPUSink(contextType, contextOptions, gpuConfig->getSamples(),
brianosmand93c1202016-03-10 07:49:08 -0800817 gpuConfig->getUseDIText(), gpuConfig->getColorType(),
brianosmanb109b8c2016-06-16 13:03:24 -0700818 sk_ref_sp(gpuConfig->getColorSpace()), FLAGS_gpu_threading);
kkinnunen3e980c32015-12-23 01:33:00 -0800819 }
820 }
821#endif
822
823#define SINK(t, sink, ...) if (config->getBackend().equals(t)) { return new sink(__VA_ARGS__); }
824
tomhudsoneebc39a2015-02-23 12:18:05 -0800825#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
826 SINK("hwui", HWUISink);
827#endif
828
mtklein748ca3b2015-01-15 10:56:12 -0800829 if (FLAGS_cpu) {
brianosmanb109b8c2016-06-16 13:03:24 -0700830 auto srgbColorSpace = SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named);
831
mtklein748ca3b2015-01-15 10:56:12 -0800832 SINK("565", RasterSink, kRGB_565_SkColorType);
833 SINK("8888", RasterSink, kN32_SkColorType);
brianosmanb109b8c2016-06-16 13:03:24 -0700834 SINK("srgb", RasterSink, kN32_SkColorType, srgbColorSpace);
mtklein27c3fdd2016-02-26 14:43:21 -0800835 SINK("f16", RasterSink, kRGBA_F16_SkColorType);
halcanary94b87bd2016-04-04 06:12:15 -0700836 SINK("pdf", PDFSink);
mtklein9c3f17d2015-01-28 11:35:18 -0800837 SINK("skp", SKPSink);
mtklein8a4527e2015-01-31 20:00:58 -0800838 SINK("svg", SVGSink);
839 SINK("null", NullSink);
halcanary47ef4d52015-03-03 09:13:09 -0800840 SINK("xps", XPSSink);
halcanary4b656662016-04-27 07:45:18 -0700841 SINK("pdfa", PDFSink, true);
mtklein748ca3b2015-01-15 10:56:12 -0800842 }
843#undef SINK
halcanary96fcdcc2015-08-27 07:41:13 -0700844 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800845}
846
kkinnunen3e980c32015-12-23 01:33:00 -0800847static Sink* create_via(const SkString& tag, Sink* wrapped) {
848#define VIA(t, via, ...) if (tag.equals(t)) { return new via(__VA_ARGS__); }
halcanary96fcdcc2015-08-27 07:41:13 -0700849 VIA("twice", ViaTwice, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700850 VIA("serialize", ViaSerialization, wrapped);
mtklein4a34ecb2016-01-08 10:19:35 -0800851 VIA("pic", ViaPicture, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700852 VIA("2ndpic", ViaSecondPicture, wrapped);
mtkleind31c13d2015-05-05 12:59:56 -0700853 VIA("sp", ViaSingletonPictures, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700854 VIA("tiles", ViaTiles, 256, 256, nullptr, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800855 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800856
mtkleind603b222015-02-17 11:13:33 -0800857 if (FLAGS_matrix.count() == 4) {
mtklein748ca3b2015-01-15 10:56:12 -0800858 SkMatrix m;
mtkleind603b222015-02-17 11:13:33 -0800859 m.reset();
860 m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
861 m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
862 m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
863 m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
864 VIA("matrix", ViaMatrix, m, wrapped);
865 VIA("upright", ViaUpright, m, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800866 }
tomhudson64de1e12015-03-05 08:01:07 -0800867
868#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
869 VIA("androidsdk", ViaAndroidSDK, wrapped);
870#endif
871
mtklein748ca3b2015-01-15 10:56:12 -0800872#undef VIA
halcanary96fcdcc2015-08-27 07:41:13 -0700873 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800874}
875
brianosman05c987c2016-05-09 13:42:16 -0700876static bool gather_sinks() {
kkinnunen3e980c32015-12-23 01:33:00 -0800877 SkCommandLineConfigArray configs;
878 ParseConfigs(FLAGS_config, &configs);
879 for (int i = 0; i < configs.count(); i++) {
880 const SkCommandLineConfig& config = *configs[i];
881 Sink* sink = create_sink(&config);
882 if (sink == nullptr) {
mtkleinc41fd922016-03-08 09:01:39 -0800883 info("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
884 config.getTag().c_str());
kkinnunen3e980c32015-12-23 01:33:00 -0800885 continue;
886 }
mtklein748ca3b2015-01-15 10:56:12 -0800887
kkinnunen3e980c32015-12-23 01:33:00 -0800888 const SkTArray<SkString>& parts = config.getViaParts();
889 for (int j = parts.count(); j-- > 0;) {
890 const SkString& part = parts[j];
891 Sink* next = create_via(part, sink);
halcanary96fcdcc2015-08-27 07:41:13 -0700892 if (next == nullptr) {
mtkleinc41fd922016-03-08 09:01:39 -0800893 info("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
894 part.c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800895 delete sink;
halcanary96fcdcc2015-08-27 07:41:13 -0700896 sink = nullptr;
mtklein748ca3b2015-01-15 10:56:12 -0800897 break;
898 }
899 sink = next;
900 }
901 if (sink) {
902 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800903 }
904 }
brianosman05c987c2016-05-09 13:42:16 -0700905
906 // If no configs were requested (just running tests, perhaps?), then we're okay.
907 // Otherwise, make sure that at least one sink was constructed correctly. This catches
908 // the case of bots without a GPU being assigned GPU configs.
909 return (configs.count() == 0) || (gSinks.count() > 0);
mtklein114c3cd2015-01-15 10:15:02 -0800910}
mtklein709d2c32015-01-15 08:30:25 -0800911
mtkleina5114d72015-08-24 13:27:01 -0700912static bool dump_png(SkBitmap bitmap, const char* path, const char* md5) {
913 const int w = bitmap.width(),
914 h = bitmap.height();
915
brianosman3c579dc2016-04-19 09:18:11 -0700916 sk_sp<SkData> encodedBitmap = sk_tools::encode_bitmap_for_png(bitmap);
917 if (encodedBitmap.get() == nullptr) {
918 return false;
mtkleina5114d72015-08-24 13:27:01 -0700919 }
brianosman3c579dc2016-04-19 09:18:11 -0700920 uint32_t* rgba = static_cast<uint32_t*>(encodedBitmap.get()->writable_data());
mtkleina5114d72015-08-24 13:27:01 -0700921
922 // We don't need bitmap anymore. Might as well drop our ref.
mtkleinfccb77d2015-08-24 14:13:29 -0700923 bitmap.reset();
mtkleina5114d72015-08-24 13:27:01 -0700924
mtkleinc64137c2015-08-25 10:56:08 -0700925 FILE* f = fopen(path, "wb");
mtkleina5114d72015-08-24 13:27:01 -0700926 if (!f) { return false; }
927
928 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
929 if (!png) {
930 fclose(f);
931 return false;
932 }
933
934 png_infop info = png_create_info_struct(png);
935 if (!info) {
936 png_destroy_write_struct(&png, &info);
937 fclose(f);
938 return false;
939 }
940
mtkleind69ece62015-09-10 10:37:44 -0700941 SkString description;
942 description.append("Key: ");
943 for (int i = 0; i < FLAGS_key.count(); i++) {
944 description.appendf("%s ", FLAGS_key[i]);
945 }
946 description.append("Properties: ");
947 for (int i = 0; i < FLAGS_properties.count(); i++) {
948 description.appendf("%s ", FLAGS_properties[i]);
949 }
950 description.appendf("MD5: %s", md5);
951
mtkleina5114d72015-08-24 13:27:01 -0700952 png_text text[2];
953 text[0].key = (png_charp)"Author";
954 text[0].text = (png_charp)"DM dump_png()";
955 text[0].compression = PNG_TEXT_COMPRESSION_NONE;
956 text[1].key = (png_charp)"Description";
mtkleind69ece62015-09-10 10:37:44 -0700957 text[1].text = (png_charp)description.c_str();
mtkleina5114d72015-08-24 13:27:01 -0700958 text[1].compression = PNG_TEXT_COMPRESSION_NONE;
959 png_set_text(png, info, text, 2);
960
961 png_init_io(png, f);
962 png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8,
963 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
964 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
965 png_write_info(png, info);
966 for (int j = 0; j < h; j++) {
brianosman3c579dc2016-04-19 09:18:11 -0700967 png_bytep row = (png_bytep)(rgba + w*j);
mtkleina5114d72015-08-24 13:27:01 -0700968 png_write_rows(png, &row, 1);
969 }
970 png_write_end(png, info);
971
972 png_destroy_write_struct(&png, &info);
973 fclose(f);
974 return true;
975}
976
mtkleina2ef6422015-01-15 13:44:22 -0800977static bool match(const char* needle, const char* haystack) {
halcanary96fcdcc2015-08-27 07:41:13 -0700978 return 0 == strcmp("_", needle) || nullptr != strstr(haystack, needle);
mtkleina2ef6422015-01-15 13:44:22 -0800979}
980
mtklein3eed7dd2016-02-24 19:07:07 -0800981static bool is_blacklisted(const char* sink, const char* src,
982 const char* srcOptions, const char* name) {
mtklein0d243ff2015-04-03 07:51:00 -0700983 for (int i = 0; i < FLAGS_blacklist.count() - 3; i += 4) {
mtkleina2ef6422015-01-15 13:44:22 -0800984 if (match(FLAGS_blacklist[i+0], sink) &&
djsollen54416de2015-04-03 07:24:48 -0700985 match(FLAGS_blacklist[i+1], src) &&
986 match(FLAGS_blacklist[i+2], srcOptions) &&
987 match(FLAGS_blacklist[i+3], name)) {
mtklein3eed7dd2016-02-24 19:07:07 -0800988 return true;
mtkleina2ef6422015-01-15 13:44:22 -0800989 }
990 }
mtklein3eed7dd2016-02-24 19:07:07 -0800991 return false;
mtkleina2ef6422015-01-15 13:44:22 -0800992}
993
mtkleincd50bca2016-01-05 06:20:20 -0800994// Even when a Task Sink reports to be non-threadsafe (e.g. GPU), we know things like
995// .png encoding are definitely thread safe. This lets us offload that work to CPU threads.
996static SkTaskGroup gDefinitelyThreadSafeWork;
997
mtklein748ca3b2015-01-15 10:56:12 -0800998// The finest-grained unit of work we can run: draw a single Src into a single Sink,
999// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
1000struct Task {
mtkleine0effd62015-07-29 06:37:28 -07001001 Task(const TaggedSrc& src, const TaggedSink& sink) : src(src), sink(sink) {}
1002 const TaggedSrc& src;
1003 const TaggedSink& sink;
mtklein748ca3b2015-01-15 10:56:12 -08001004
mtklein21eaf3b2016-02-08 12:39:59 -08001005 static void Run(const Task& task) {
1006 SkString name = task.src->name();
mtkleine0effd62015-07-29 06:37:28 -07001007
mtkleinb9eb4ac2015-02-02 18:26:03 -08001008 SkString log;
mtklein3eed7dd2016-02-24 19:07:07 -08001009 if (!FLAGS_dryRun) {
mtklein748ca3b2015-01-15 10:56:12 -08001010 SkBitmap bitmap;
1011 SkDynamicMemoryWStream stream;
mtklein3eed7dd2016-02-24 19:07:07 -08001012 start(task.sink.tag.c_str(), task.src.tag.c_str(),
1013 task.src.options.c_str(), name.c_str());
mtklein21eaf3b2016-02-08 12:39:59 -08001014 Error err = task.sink->draw(*task.src, &bitmap, &stream, &log);
mtkleinb3b13b72016-03-07 13:20:52 -08001015 if (!log.isEmpty()) {
mtkleinc41fd922016-03-08 09:01:39 -08001016 info("%s %s %s %s:\n%s\n", task.sink.tag.c_str()
1017 , task.src.tag.c_str()
1018 , task.src.options.c_str()
1019 , name.c_str()
1020 , log.c_str());
mtkleinb3b13b72016-03-07 13:20:52 -08001021 }
mtklein748ca3b2015-01-15 10:56:12 -08001022 if (!err.isEmpty()) {
mtklein4089ef72015-03-05 08:40:28 -08001023 if (err.isFatal()) {
djsollen54416de2015-04-03 07:24:48 -07001024 fail(SkStringPrintf("%s %s %s %s: %s",
mtklein21eaf3b2016-02-08 12:39:59 -08001025 task.sink.tag.c_str(),
1026 task.src.tag.c_str(),
1027 task.src.options.c_str(),
mtklein4089ef72015-03-05 08:40:28 -08001028 name.c_str(),
1029 err.c_str()));
mtkleinb37cb412015-03-18 05:27:14 -07001030 } else {
mtklein3eed7dd2016-02-24 19:07:07 -08001031 done(task.sink.tag.c_str(), task.src.tag.c_str(),
1032 task.src.options.c_str(), name.c_str());
mtkleinba6ada72016-01-21 09:39:35 -08001033 return;
mtklein4089ef72015-03-05 08:40:28 -08001034 }
mtklein748ca3b2015-01-15 10:56:12 -08001035 }
mtklein62bd1a62015-01-27 14:46:26 -08001036
mtkleincd50bca2016-01-05 06:20:20 -08001037 // We're likely switching threads here, so we must capture by value, [=] or [foo,bar].
1038 SkStreamAsset* data = stream.detachAsStream();
1039 gDefinitelyThreadSafeWork.add([task,name,bitmap,data]{
1040 SkAutoTDelete<SkStreamAsset> ownedData(data);
1041
1042 // Why doesn't the copy constructor do this when we have pre-locked pixels?
1043 bitmap.lockPixels();
1044
1045 SkString md5;
1046 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
1047 SkMD5 hash;
1048 if (data->getLength()) {
1049 hash.writeStream(data, data->getLength());
1050 data->rewind();
mtklein38408462015-07-08 07:25:27 -07001051 } else {
mtkleincd50bca2016-01-05 06:20:20 -08001052 // If we're BGRA (Linux, Windows), swizzle over to RGBA (Mac, Android).
1053 // This helps eliminate multiple 0-pixel-diff hashes on gold.skia.org.
1054 // (Android's general slow speed breaks the tie arbitrarily in RGBA's favor.)
1055 // We might consider promoting 565 to RGBA too.
1056 if (bitmap.colorType() == kBGRA_8888_SkColorType) {
1057 SkBitmap swizzle;
1058 SkAssertResult(bitmap.copyTo(&swizzle, kRGBA_8888_SkColorType));
1059 hash.write(swizzle.getPixels(), swizzle.getSize());
1060 } else {
1061 hash.write(bitmap.getPixels(), bitmap.getSize());
1062 }
1063 }
1064 SkMD5::Digest digest;
1065 hash.finish(digest);
1066 for (int i = 0; i < 16; i++) {
1067 md5.appendf("%02x", digest.data[i]);
mtklein38408462015-07-08 07:25:27 -07001068 }
mtklein62bd1a62015-01-27 14:46:26 -08001069 }
mtklein62bd1a62015-01-27 14:46:26 -08001070
mtkleincd50bca2016-01-05 06:20:20 -08001071 if (!FLAGS_readPath.isEmpty() &&
mtklein3eed7dd2016-02-24 19:07:07 -08001072 !gGold.contains(Gold(task.sink.tag, task.src.tag,
1073 task.src.options, name, md5))) {
mtkleincd50bca2016-01-05 06:20:20 -08001074 fail(SkStringPrintf("%s not found for %s %s %s %s in %s",
1075 md5.c_str(),
mtklein21eaf3b2016-02-08 12:39:59 -08001076 task.sink.tag.c_str(),
1077 task.src.tag.c_str(),
1078 task.src.options.c_str(),
mtkleincd50bca2016-01-05 06:20:20 -08001079 name.c_str(),
1080 FLAGS_readPath[0]));
mtklein748ca3b2015-01-15 10:56:12 -08001081 }
mtkleincd50bca2016-01-05 06:20:20 -08001082
1083 if (!FLAGS_writePath.isEmpty()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001084 const char* ext = task.sink->fileExtension();
mtkleincd50bca2016-01-05 06:20:20 -08001085 if (data->getLength()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001086 WriteToDisk(task, md5, ext, data, data->getLength(), nullptr);
mtkleincd50bca2016-01-05 06:20:20 -08001087 SkASSERT(bitmap.drawsNothing());
1088 } else if (!bitmap.drawsNothing()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001089 WriteToDisk(task, md5, ext, nullptr, 0, &bitmap);
mtkleincd50bca2016-01-05 06:20:20 -08001090 }
1091 }
1092 });
mtklein748ca3b2015-01-15 10:56:12 -08001093 }
mtklein3eed7dd2016-02-24 19:07:07 -08001094 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 -08001095 }
1096
1097 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -08001098 SkString md5,
1099 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -08001100 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -08001101 const SkBitmap* bitmap) {
mtklein409d4702016-02-29 07:38:01 -08001102 bool gammaCorrect = false;
1103 if (bitmap) {
brianosman898235c2016-04-06 07:38:23 -07001104 gammaCorrect = SkImageInfoIsGammaCorrect(bitmap->info());
mtklein409d4702016-02-29 07:38:01 -08001105 }
1106
mtklein748ca3b2015-01-15 10:56:12 -08001107 JsonWriter::BitmapResult result;
djsollen54416de2015-04-03 07:24:48 -07001108 result.name = task.src->name();
mtklein3eed7dd2016-02-24 19:07:07 -08001109 result.config = task.sink.tag;
djsollen54416de2015-04-03 07:24:48 -07001110 result.sourceType = task.src.tag;
1111 result.sourceOptions = task.src.options;
1112 result.ext = ext;
mtklein409d4702016-02-29 07:38:01 -08001113 result.gammaCorrect = gammaCorrect;
djsollen54416de2015-04-03 07:24:48 -07001114 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -08001115 JsonWriter::AddBitmapResult(result);
1116
mtkleinb0531a72015-04-07 13:38:48 -07001117 // If an MD5 is uninteresting, we want it noted in the JSON file,
1118 // but don't want to dump it out as a .png (or whatever ext is).
1119 if (gUninterestingHashes.contains(md5)) {
1120 return;
1121 }
1122
mtklein748ca3b2015-01-15 10:56:12 -08001123 const char* dir = FLAGS_writePath[0];
1124 if (0 == strcmp(dir, "@")) { // Needed for iOS.
1125 dir = FLAGS_resourcePath[0];
1126 }
1127 sk_mkdir(dir);
1128
1129 SkString path;
1130 if (FLAGS_nameByHash) {
1131 path = SkOSPath::Join(dir, result.md5.c_str());
1132 path.append(".");
1133 path.append(ext);
1134 if (sk_exists(path.c_str())) {
1135 return; // Content-addressed. If it exists already, we're done.
1136 }
1137 } else {
kkinnunen3e980c32015-12-23 01:33:00 -08001138 path = SkOSPath::Join(dir, task.sink.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001139 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001140 path = SkOSPath::Join(path.c_str(), task.src.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001141 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001142 if (strcmp(task.src.options.c_str(), "") != 0) {
1143 path = SkOSPath::Join(path.c_str(), task.src.options.c_str());
djsollen54416de2015-04-03 07:24:48 -07001144 sk_mkdir(path.c_str());
1145 }
mtklein748ca3b2015-01-15 10:56:12 -08001146 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
1147 path.append(".");
1148 path.append(ext);
1149 }
1150
mtklein748ca3b2015-01-15 10:56:12 -08001151 if (bitmap) {
mtkleina5114d72015-08-24 13:27:01 -07001152 if (!dump_png(*bitmap, path.c_str(), result.md5.c_str())) {
mtklein748ca3b2015-01-15 10:56:12 -08001153 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
1154 return;
1155 }
1156 } else {
mtkleina5114d72015-08-24 13:27:01 -07001157 SkFILEWStream file(path.c_str());
1158 if (!file.isValid()) {
1159 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
1160 return;
1161 }
mtklein748ca3b2015-01-15 10:56:12 -08001162 if (!file.writeStream(data, len)) {
1163 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
1164 return;
1165 }
1166 }
1167 }
1168};
1169
mtklein748ca3b2015-01-15 10:56:12 -08001170/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1171
1172// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
1173
mtklein21eaf3b2016-02-08 12:39:59 -08001174static SkTDArray<skiatest::Test> gParallelTests, gSerialTests;
mtklein748ca3b2015-01-15 10:56:12 -08001175
1176static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -08001177 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -08001178 return;
1179 }
mtklein6393c062015-04-27 08:45:01 -07001180 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r; r = r->next()) {
1181 if (!in_shard()) {
1182 continue;
1183 }
halcanary87f3ba42015-01-20 09:30:20 -08001184 // Despite its name, factory() is returning a reference to
1185 // link-time static const POD data.
1186 const skiatest::Test& test = r->factory();
1187 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -08001188 continue;
1189 }
halcanary87f3ba42015-01-20 09:30:20 -08001190 if (test.needsGpu && gpu_supported()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001191 (FLAGS_gpu_threading ? gParallelTests : gSerialTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -08001192 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein21eaf3b2016-02-08 12:39:59 -08001193 gParallelTests.push(test);
mtklein82d28432015-01-15 12:46:02 -08001194 }
mtklein748ca3b2015-01-15 10:56:12 -08001195 }
1196}
1197
mtklein21eaf3b2016-02-08 12:39:59 -08001198static void run_test(skiatest::Test test) {
halcanary87f3ba42015-01-20 09:30:20 -08001199 struct : public skiatest::Reporter {
mtklein36352bf2015-03-25 18:17:31 -07001200 void reportFailed(const skiatest::Failure& failure) override {
halcanary87f3ba42015-01-20 09:30:20 -08001201 fail(failure.toString());
1202 JsonWriter::AddTestFailure(failure);
1203 }
mtklein36352bf2015-03-25 18:17:31 -07001204 bool allowExtendedTest() const override {
halcanary87f3ba42015-01-20 09:30:20 -08001205 return FLAGS_pathOpsExtended;
1206 }
mtklein36352bf2015-03-25 18:17:31 -07001207 bool verbose() const override { return FLAGS_veryVerbose; }
halcanary87f3ba42015-01-20 09:30:20 -08001208 } reporter;
djsollen824996a2015-06-12 12:06:22 -07001209
mtklein3eed7dd2016-02-24 19:07:07 -08001210 if (!FLAGS_dryRun && !is_blacklisted("_", "tests", "_", test.name)) {
mtklein21eaf3b2016-02-08 12:39:59 -08001211 start("unit", "test", "", test.name);
mtklein55e88b22015-01-21 15:50:13 -08001212 GrContextFactory factory;
mtklein21eaf3b2016-02-08 12:39:59 -08001213 test.proc(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -08001214 }
mtklein3eed7dd2016-02-24 19:07:07 -08001215 done("unit", "test", "", test.name);
mtklein748ca3b2015-01-15 10:56:12 -08001216}
1217
1218/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1219
mtkleinafae30a2016-02-24 12:28:32 -08001220DEFINE_int32(status_sec, 15, "Print status this often (and if we crash).");
1221
1222SkThread* start_status_thread() {
1223 auto thread = new SkThread([] (void*) {
1224 for (;;) {
1225 print_status();
1226 #if defined(SK_BUILD_FOR_WIN)
1227 Sleep(FLAGS_status_sec * 1000);
1228 #else
1229 sleep(FLAGS_status_sec);
1230 #endif
mtklein2e1c47e2015-03-12 07:16:56 -07001231 }
mtkleinafae30a2016-02-24 12:28:32 -08001232 });
1233 thread->start();
1234 return thread;
mtkleinde6fc2b2015-03-12 06:28:54 -07001235}
1236
caryclark83ca6282015-06-10 09:31:09 -07001237#define PORTABLE_FONT_PREFIX "Toy Liberation "
1238
mbocee6a9912016-05-31 11:42:36 -07001239static sk_sp<SkTypeface> create_from_name(const char familyName[], SkFontStyle style) {
caryclark83ca6282015-06-10 09:31:09 -07001240 if (familyName && strlen(familyName) > sizeof(PORTABLE_FONT_PREFIX)
1241 && !strncmp(familyName, PORTABLE_FONT_PREFIX, sizeof(PORTABLE_FONT_PREFIX) - 1)) {
caryclark1818acb2015-07-24 12:09:25 -07001242 return sk_tool_utils::create_portable_typeface(familyName, style);
caryclark83ca6282015-06-10 09:31:09 -07001243 }
halcanary96fcdcc2015-08-27 07:41:13 -07001244 return nullptr;
caryclark83ca6282015-06-10 09:31:09 -07001245}
1246
1247#undef PORTABLE_FONT_PREFIX
1248
mbocee6a9912016-05-31 11:42:36 -07001249extern sk_sp<SkTypeface> (*gCreateTypefaceDelegate)(const char [], SkFontStyle );
caryclark83ca6282015-06-10 09:31:09 -07001250
herb3f996092016-05-27 08:15:05 -07001251extern bool gDefaultProfileIsSRGB;
1252
jcgregorio3b27ade2014-11-13 08:06:40 -08001253int dm_main();
caryclark17f0b6d2014-07-22 10:15:34 -07001254int dm_main() {
bsalomon71de3532016-04-26 14:27:21 -07001255 setbuf(stdout, nullptr);
mtklein246ba3a2016-02-23 10:39:36 -08001256 setup_crash_handler();
mtkleinafae30a2016-02-24 12:28:32 -08001257
mtkleina483da32016-03-17 12:53:36 -07001258 if (FLAGS_verbose) {
1259 gVLog = stderr;
1260 } else if (!FLAGS_writePath.isEmpty()) {
mtklein51c8cfc2016-03-11 12:59:09 -08001261 sk_mkdir(FLAGS_writePath[0]);
1262 gVLog = freopen(SkOSPath::Join(FLAGS_writePath[0], "verbose.log").c_str(), "w", stderr);
mtkleinc41fd922016-03-08 09:01:39 -08001263 }
1264
herb3f996092016-05-27 08:15:05 -07001265 if (FLAGS_forceSRGB) {
1266 gDefaultProfileIsSRGB = true;
1267 }
1268
mtklein5286f022016-01-22 08:18:14 -08001269 JsonWriter::DumpJson(); // It's handy for the bots to assume this is ~never missing.
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +00001270 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -07001271 SkTaskGroup::Enabler enabled(FLAGS_threads);
caryclark83ca6282015-06-10 09:31:09 -07001272 gCreateTypefaceDelegate = &create_from_name;
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +00001273
halcanary7d571242016-02-24 17:59:16 -08001274 {
1275 SkString testResourcePath = GetResourcePath("color_wheel.png");
1276 SkFILEStream testResource(testResourcePath.c_str());
1277 if (!testResource.isValid()) {
mtkleinc41fd922016-03-08 09:01:39 -08001278 info("Some resources are missing. Do you need to set --resourcePath?\n");
halcanary7d571242016-02-24 17:59:16 -08001279 }
1280 }
mtklein62bd1a62015-01-27 14:46:26 -08001281 gather_gold();
borenet09ed4802015-04-03 14:15:33 -07001282 gather_uninteresting_hashes();
mtklein62bd1a62015-01-27 14:46:26 -08001283
scroggo86737142016-02-03 12:19:11 -08001284 if (!gather_srcs()) {
1285 return 1;
1286 }
brianosman05c987c2016-05-09 13:42:16 -07001287 if (!gather_sinks()) {
1288 return 1;
1289 }
mtklein748ca3b2015-01-15 10:56:12 -08001290 gather_tests();
mtklein21eaf3b2016-02-08 12:39:59 -08001291 gPending = gSrcs.count() * gSinks.count() + gParallelTests.count() + gSerialTests.count();
mtkleinc41fd922016-03-08 09:01:39 -08001292 info("%d srcs * %d sinks + %d tests == %d tasks",
1293 gSrcs.count(), gSinks.count(), gParallelTests.count() + gSerialTests.count(), gPending);
mtkleinafae30a2016-02-24 12:28:32 -08001294 SkAutoTDelete<SkThread> statusThread(start_status_thread());
mtklein748ca3b2015-01-15 10:56:12 -08001295
mtklein21eaf3b2016-02-08 12:39:59 -08001296 // Kick off as much parallel work as we can, making note of any serial work we'll need to do.
1297 SkTaskGroup parallel;
1298 SkTArray<Task> serial;
1299
1300 for (auto& sink : gSinks)
1301 for (auto& src : gSrcs) {
mtklein3eed7dd2016-02-24 19:07:07 -08001302 if (src->veto(sink->flags()) ||
1303 is_blacklisted(sink.tag.c_str(), src.tag.c_str(),
1304 src.options.c_str(), src->name().c_str())) {
mtkleinf10637f2016-06-10 13:56:35 -07001305 SkAutoMutexAcquire lock(gMutex);
mtklein3eed7dd2016-02-24 19:07:07 -08001306 gPending--;
1307 continue;
1308 }
1309
mtklein21eaf3b2016-02-08 12:39:59 -08001310 Task task(src, sink);
1311 if (src->serial() || sink->serial()) {
1312 serial.push_back(task);
1313 } else {
1314 parallel.add([task] { Task::Run(task); });
mtklein748ca3b2015-01-15 10:56:12 -08001315 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001316 }
mtklein21eaf3b2016-02-08 12:39:59 -08001317 for (auto test : gParallelTests) {
1318 parallel.add([test] { run_test(test); });
mtklein55e88b22015-01-21 15:50:13 -08001319 }
mtklein21eaf3b2016-02-08 12:39:59 -08001320
1321 // With the parallel work running, run serial tasks and tests here on main thread.
1322 for (auto task : serial) { Task::Run(task); }
1323 for (auto test : gSerialTests) { run_test(test); }
1324
1325 // Wait for any remaining parallel work to complete (including any spun off of serial tasks).
1326 parallel.wait();
mtkleincd50bca2016-01-05 06:20:20 -08001327 gDefinitelyThreadSafeWork.wait();
1328
mtkleinda884c42016-02-24 18:00:23 -08001329 // We'd better have run everything.
1330 SkASSERT(gPending == 0);
mtkleine027f172016-02-26 15:53:06 -08001331 // Make sure we've flushed all our results to disk.
1332 JsonWriter::DumpJson();
mtkleinda884c42016-02-24 18:00:23 -08001333
mtklein748ca3b2015-01-15 10:56:12 -08001334 // At this point we're back in single-threaded land.
caryclarkf53ce802015-06-15 06:48:30 -07001335 sk_tool_utils::release_portable_typefaces();
mtklein@google.comd36522d2013-10-16 13:02:15 +00001336
mtklein748ca3b2015-01-15 10:56:12 -08001337 if (gFailures.count() > 0) {
mtkleinc41fd922016-03-08 09:01:39 -08001338 info("Failures:\n");
mtklein748ca3b2015-01-15 10:56:12 -08001339 for (int i = 0; i < gFailures.count(); i++) {
mtkleinc41fd922016-03-08 09:01:39 -08001340 info("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001341 }
mtkleinc41fd922016-03-08 09:01:39 -08001342 info("%d failures\n", gFailures.count());
mtklein748ca3b2015-01-15 10:56:12 -08001343 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001344 }
mtkleinafae30a2016-02-24 12:28:32 -08001345
1346#ifdef SK_PDF_IMAGE_STATS
halcanary7a14b312015-10-01 07:28:13 -07001347 SkPDFImageDumpStats();
mtkleinafae30a2016-02-24 12:28:32 -08001348#endif // SK_PDF_IMAGE_STATS
1349
1350 print_status();
mtkleinc41fd922016-03-08 09:01:39 -08001351 info("Finished!\n");
mtklein748ca3b2015-01-15 10:56:12 -08001352 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +00001353}
jcgregorio3b27ade2014-11-13 08:06:40 -08001354
kkinnunen179a8f52015-11-20 13:32:24 -08001355// TODO: currently many GPU tests are declared outside SK_SUPPORT_GPU guards.
1356// Thus we export the empty RunWithGPUTestContexts when SK_SUPPORT_GPU=0.
1357namespace skiatest {
bsalomon758586c2016-04-06 14:02:39 -07001358
1359#if SK_SUPPORT_GPU
1360bool IsGLContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1361 return kOpenGL_GrBackend == GrContextFactory::ContextTypeBackend(type);
1362}
bsalomondc0fcd42016-04-11 14:21:33 -07001363bool IsVulkanContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1364 return kVulkan_GrBackend == GrContextFactory::ContextTypeBackend(type);
1365}
bsalomon758586c2016-04-06 14:02:39 -07001366bool IsRenderingGLContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1367 return IsGLContextType(type) && GrContextFactory::IsRenderingContext(type);
1368}
1369bool IsNullGLContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1370 return type == GrContextFactory::kNullGL_ContextType;
1371}
1372#else
1373bool IsGLContextType(int) { return false; }
bsalomondc0fcd42016-04-11 14:21:33 -07001374bool IsVulkanContextType(int) { return false; }
bsalomon758586c2016-04-06 14:02:39 -07001375bool IsRenderingGLContextType(int) { return false; }
1376bool IsNullGLContextType(int) { return false; }
1377#endif
1378
1379void RunWithGPUTestContexts(GrContextTestFn* test, GrContextTypeFilterFn* contextTypeFilter,
bsalomonf2f1c172016-04-05 12:59:06 -07001380 Reporter* reporter, GrContextFactory* factory) {
kkinnunen179a8f52015-11-20 13:32:24 -08001381#if SK_SUPPORT_GPU
kkinnunen3e980c32015-12-23 01:33:00 -08001382
bsalomon758586c2016-04-06 14:02:39 -07001383 for (int typeInt = 0; typeInt < GrContextFactory::kContextTypeCnt; ++typeInt) {
1384 GrContextFactory::ContextType contextType = (GrContextFactory::ContextType) typeInt;
1385 ContextInfo ctxInfo = factory->getContextInfo(contextType);
bsalomonfda88072016-04-11 14:40:50 -07001386 if (contextTypeFilter && !(*contextTypeFilter)(contextType)) {
kkinnunen179a8f52015-11-20 13:32:24 -08001387 continue;
1388 }
bsalomon758586c2016-04-06 14:02:39 -07001389 // Use "native" instead of explicitly trying OpenGL and OpenGL ES. Do not use GLES on,
1390 // desktop since tests do not account for not fixing http://skbug.com/2809
1391 if (contextType == GrContextFactory::kGL_ContextType ||
1392 contextType == GrContextFactory::kGLES_ContextType) {
1393 if (contextType != GrContextFactory::kNativeGL_ContextType) {
1394 continue;
1395 }
1396 }
bsalomon8b7451a2016-05-11 06:33:06 -07001397 if (ctxInfo.grContext()) {
bsalomonf2f1c172016-04-05 12:59:06 -07001398 (*test)(reporter, ctxInfo);
kkinnunen5219fd92015-12-10 06:28:13 -08001399 }
bsalomonf2f1c172016-04-05 12:59:06 -07001400 ctxInfo = factory->getContextInfo(contextType,
bsalomon85b4b532016-04-05 11:06:27 -07001401 GrContextFactory::kEnableNVPR_ContextOptions);
bsalomon8b7451a2016-05-11 06:33:06 -07001402 if (ctxInfo.grContext()) {
bsalomonf2f1c172016-04-05 12:59:06 -07001403 (*test)(reporter, ctxInfo);
kkinnunen179a8f52015-11-20 13:32:24 -08001404 }
1405 }
1406#endif
1407}
kkinnunen179a8f52015-11-20 13:32:24 -08001408} // namespace skiatest
1409
borenet48087572015-04-02 12:16:36 -07001410#if !defined(SK_BUILD_FOR_IOS)
jcgregorio3b27ade2014-11-13 08:06:40 -08001411int main(int argc, char** argv) {
1412 SkCommandLineFlags::Parse(argc, argv);
1413 return dm_main();
1414}
1415#endif