blob: 9ddff282f386f6b2ec300454c8e1eb8829da682c [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 {
mtklein15923c92016-02-29 10:14:38 -0800131 SkAutoTAcquire<SkSpinlock> 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());
mtklein15923c92016-02-29 10:14:38 -0800150 SkAutoTAcquire<SkSpinlock> 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
mtklein15923c92016-02-29 10:14:38 -0800159 SkAutoTAcquire<SkSpinlock> 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
mtklein5b64dab2016-06-09 08:59:48 -0700182 SkAutoTAcquire<SkSpinlock> lock(gMutex);
183
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) {
208 SkAutoTAcquire<SkSpinlock> lock(gMutex);
209
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 }
brianosman898235c2016-04-06 07:38:23 -0700805 if (SkColorAndProfileAreGammaCorrect(gpuConfig->getColorType(),
806 gpuConfig->getProfileType())) {
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(),
818 gpuConfig->getProfileType(), 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) {
830 SINK("565", RasterSink, kRGB_565_SkColorType);
831 SINK("8888", RasterSink, kN32_SkColorType);
mtklein27c3fdd2016-02-26 14:43:21 -0800832 SINK("srgb", RasterSink, kN32_SkColorType, kSRGB_SkColorProfileType);
833 SINK("f16", RasterSink, kRGBA_F16_SkColorType);
halcanary94b87bd2016-04-04 06:12:15 -0700834 SINK("pdf", PDFSink);
mtklein9c3f17d2015-01-28 11:35:18 -0800835 SINK("skp", SKPSink);
mtklein8a4527e2015-01-31 20:00:58 -0800836 SINK("svg", SVGSink);
837 SINK("null", NullSink);
halcanary47ef4d52015-03-03 09:13:09 -0800838 SINK("xps", XPSSink);
halcanary4b656662016-04-27 07:45:18 -0700839 SINK("pdfa", PDFSink, true);
mtklein748ca3b2015-01-15 10:56:12 -0800840 }
841#undef SINK
halcanary96fcdcc2015-08-27 07:41:13 -0700842 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800843}
844
kkinnunen3e980c32015-12-23 01:33:00 -0800845static Sink* create_via(const SkString& tag, Sink* wrapped) {
846#define VIA(t, via, ...) if (tag.equals(t)) { return new via(__VA_ARGS__); }
halcanary96fcdcc2015-08-27 07:41:13 -0700847 VIA("twice", ViaTwice, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700848 VIA("serialize", ViaSerialization, wrapped);
mtklein4a34ecb2016-01-08 10:19:35 -0800849 VIA("pic", ViaPicture, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700850 VIA("2ndpic", ViaSecondPicture, wrapped);
mtkleind31c13d2015-05-05 12:59:56 -0700851 VIA("sp", ViaSingletonPictures, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700852 VIA("tiles", ViaTiles, 256, 256, nullptr, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800853 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800854
mtkleind603b222015-02-17 11:13:33 -0800855 if (FLAGS_matrix.count() == 4) {
mtklein748ca3b2015-01-15 10:56:12 -0800856 SkMatrix m;
mtkleind603b222015-02-17 11:13:33 -0800857 m.reset();
858 m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
859 m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
860 m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
861 m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
862 VIA("matrix", ViaMatrix, m, wrapped);
863 VIA("upright", ViaUpright, m, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800864 }
tomhudson64de1e12015-03-05 08:01:07 -0800865
866#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
867 VIA("androidsdk", ViaAndroidSDK, wrapped);
868#endif
869
mtklein748ca3b2015-01-15 10:56:12 -0800870#undef VIA
halcanary96fcdcc2015-08-27 07:41:13 -0700871 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800872}
873
brianosman05c987c2016-05-09 13:42:16 -0700874static bool gather_sinks() {
kkinnunen3e980c32015-12-23 01:33:00 -0800875 SkCommandLineConfigArray configs;
876 ParseConfigs(FLAGS_config, &configs);
877 for (int i = 0; i < configs.count(); i++) {
878 const SkCommandLineConfig& config = *configs[i];
879 Sink* sink = create_sink(&config);
880 if (sink == nullptr) {
mtkleinc41fd922016-03-08 09:01:39 -0800881 info("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
882 config.getTag().c_str());
kkinnunen3e980c32015-12-23 01:33:00 -0800883 continue;
884 }
mtklein748ca3b2015-01-15 10:56:12 -0800885
kkinnunen3e980c32015-12-23 01:33:00 -0800886 const SkTArray<SkString>& parts = config.getViaParts();
887 for (int j = parts.count(); j-- > 0;) {
888 const SkString& part = parts[j];
889 Sink* next = create_via(part, sink);
halcanary96fcdcc2015-08-27 07:41:13 -0700890 if (next == nullptr) {
mtkleinc41fd922016-03-08 09:01:39 -0800891 info("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
892 part.c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800893 delete sink;
halcanary96fcdcc2015-08-27 07:41:13 -0700894 sink = nullptr;
mtklein748ca3b2015-01-15 10:56:12 -0800895 break;
896 }
897 sink = next;
898 }
899 if (sink) {
900 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800901 }
902 }
brianosman05c987c2016-05-09 13:42:16 -0700903
904 // If no configs were requested (just running tests, perhaps?), then we're okay.
905 // Otherwise, make sure that at least one sink was constructed correctly. This catches
906 // the case of bots without a GPU being assigned GPU configs.
907 return (configs.count() == 0) || (gSinks.count() > 0);
mtklein114c3cd2015-01-15 10:15:02 -0800908}
mtklein709d2c32015-01-15 08:30:25 -0800909
mtkleina5114d72015-08-24 13:27:01 -0700910static bool dump_png(SkBitmap bitmap, const char* path, const char* md5) {
911 const int w = bitmap.width(),
912 h = bitmap.height();
913
brianosman3c579dc2016-04-19 09:18:11 -0700914 sk_sp<SkData> encodedBitmap = sk_tools::encode_bitmap_for_png(bitmap);
915 if (encodedBitmap.get() == nullptr) {
916 return false;
mtkleina5114d72015-08-24 13:27:01 -0700917 }
brianosman3c579dc2016-04-19 09:18:11 -0700918 uint32_t* rgba = static_cast<uint32_t*>(encodedBitmap.get()->writable_data());
mtkleina5114d72015-08-24 13:27:01 -0700919
920 // We don't need bitmap anymore. Might as well drop our ref.
mtkleinfccb77d2015-08-24 14:13:29 -0700921 bitmap.reset();
mtkleina5114d72015-08-24 13:27:01 -0700922
mtkleinc64137c2015-08-25 10:56:08 -0700923 FILE* f = fopen(path, "wb");
mtkleina5114d72015-08-24 13:27:01 -0700924 if (!f) { return false; }
925
926 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
927 if (!png) {
928 fclose(f);
929 return false;
930 }
931
932 png_infop info = png_create_info_struct(png);
933 if (!info) {
934 png_destroy_write_struct(&png, &info);
935 fclose(f);
936 return false;
937 }
938
mtkleind69ece62015-09-10 10:37:44 -0700939 SkString description;
940 description.append("Key: ");
941 for (int i = 0; i < FLAGS_key.count(); i++) {
942 description.appendf("%s ", FLAGS_key[i]);
943 }
944 description.append("Properties: ");
945 for (int i = 0; i < FLAGS_properties.count(); i++) {
946 description.appendf("%s ", FLAGS_properties[i]);
947 }
948 description.appendf("MD5: %s", md5);
949
mtkleina5114d72015-08-24 13:27:01 -0700950 png_text text[2];
951 text[0].key = (png_charp)"Author";
952 text[0].text = (png_charp)"DM dump_png()";
953 text[0].compression = PNG_TEXT_COMPRESSION_NONE;
954 text[1].key = (png_charp)"Description";
mtkleind69ece62015-09-10 10:37:44 -0700955 text[1].text = (png_charp)description.c_str();
mtkleina5114d72015-08-24 13:27:01 -0700956 text[1].compression = PNG_TEXT_COMPRESSION_NONE;
957 png_set_text(png, info, text, 2);
958
959 png_init_io(png, f);
960 png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8,
961 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
962 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
963 png_write_info(png, info);
964 for (int j = 0; j < h; j++) {
brianosman3c579dc2016-04-19 09:18:11 -0700965 png_bytep row = (png_bytep)(rgba + w*j);
mtkleina5114d72015-08-24 13:27:01 -0700966 png_write_rows(png, &row, 1);
967 }
968 png_write_end(png, info);
969
970 png_destroy_write_struct(&png, &info);
971 fclose(f);
972 return true;
973}
974
mtkleina2ef6422015-01-15 13:44:22 -0800975static bool match(const char* needle, const char* haystack) {
halcanary96fcdcc2015-08-27 07:41:13 -0700976 return 0 == strcmp("_", needle) || nullptr != strstr(haystack, needle);
mtkleina2ef6422015-01-15 13:44:22 -0800977}
978
mtklein3eed7dd2016-02-24 19:07:07 -0800979static bool is_blacklisted(const char* sink, const char* src,
980 const char* srcOptions, const char* name) {
mtklein0d243ff2015-04-03 07:51:00 -0700981 for (int i = 0; i < FLAGS_blacklist.count() - 3; i += 4) {
mtkleina2ef6422015-01-15 13:44:22 -0800982 if (match(FLAGS_blacklist[i+0], sink) &&
djsollen54416de2015-04-03 07:24:48 -0700983 match(FLAGS_blacklist[i+1], src) &&
984 match(FLAGS_blacklist[i+2], srcOptions) &&
985 match(FLAGS_blacklist[i+3], name)) {
mtklein3eed7dd2016-02-24 19:07:07 -0800986 return true;
mtkleina2ef6422015-01-15 13:44:22 -0800987 }
988 }
mtklein3eed7dd2016-02-24 19:07:07 -0800989 return false;
mtkleina2ef6422015-01-15 13:44:22 -0800990}
991
mtkleincd50bca2016-01-05 06:20:20 -0800992// Even when a Task Sink reports to be non-threadsafe (e.g. GPU), we know things like
993// .png encoding are definitely thread safe. This lets us offload that work to CPU threads.
994static SkTaskGroup gDefinitelyThreadSafeWork;
995
mtklein748ca3b2015-01-15 10:56:12 -0800996// The finest-grained unit of work we can run: draw a single Src into a single Sink,
997// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
998struct Task {
mtkleine0effd62015-07-29 06:37:28 -0700999 Task(const TaggedSrc& src, const TaggedSink& sink) : src(src), sink(sink) {}
1000 const TaggedSrc& src;
1001 const TaggedSink& sink;
mtklein748ca3b2015-01-15 10:56:12 -08001002
mtklein21eaf3b2016-02-08 12:39:59 -08001003 static void Run(const Task& task) {
1004 SkString name = task.src->name();
mtkleine0effd62015-07-29 06:37:28 -07001005
mtkleinb9eb4ac2015-02-02 18:26:03 -08001006 SkString log;
mtklein3eed7dd2016-02-24 19:07:07 -08001007 if (!FLAGS_dryRun) {
mtklein748ca3b2015-01-15 10:56:12 -08001008 SkBitmap bitmap;
1009 SkDynamicMemoryWStream stream;
mtklein3eed7dd2016-02-24 19:07:07 -08001010 start(task.sink.tag.c_str(), task.src.tag.c_str(),
1011 task.src.options.c_str(), name.c_str());
mtklein21eaf3b2016-02-08 12:39:59 -08001012 Error err = task.sink->draw(*task.src, &bitmap, &stream, &log);
mtkleinb3b13b72016-03-07 13:20:52 -08001013 if (!log.isEmpty()) {
mtkleinc41fd922016-03-08 09:01:39 -08001014 info("%s %s %s %s:\n%s\n", task.sink.tag.c_str()
1015 , task.src.tag.c_str()
1016 , task.src.options.c_str()
1017 , name.c_str()
1018 , log.c_str());
mtkleinb3b13b72016-03-07 13:20:52 -08001019 }
mtklein748ca3b2015-01-15 10:56:12 -08001020 if (!err.isEmpty()) {
mtklein4089ef72015-03-05 08:40:28 -08001021 if (err.isFatal()) {
djsollen54416de2015-04-03 07:24:48 -07001022 fail(SkStringPrintf("%s %s %s %s: %s",
mtklein21eaf3b2016-02-08 12:39:59 -08001023 task.sink.tag.c_str(),
1024 task.src.tag.c_str(),
1025 task.src.options.c_str(),
mtklein4089ef72015-03-05 08:40:28 -08001026 name.c_str(),
1027 err.c_str()));
mtkleinb37cb412015-03-18 05:27:14 -07001028 } else {
mtklein3eed7dd2016-02-24 19:07:07 -08001029 done(task.sink.tag.c_str(), task.src.tag.c_str(),
1030 task.src.options.c_str(), name.c_str());
mtkleinba6ada72016-01-21 09:39:35 -08001031 return;
mtklein4089ef72015-03-05 08:40:28 -08001032 }
mtklein748ca3b2015-01-15 10:56:12 -08001033 }
mtklein62bd1a62015-01-27 14:46:26 -08001034
mtkleincd50bca2016-01-05 06:20:20 -08001035 // We're likely switching threads here, so we must capture by value, [=] or [foo,bar].
1036 SkStreamAsset* data = stream.detachAsStream();
1037 gDefinitelyThreadSafeWork.add([task,name,bitmap,data]{
1038 SkAutoTDelete<SkStreamAsset> ownedData(data);
1039
1040 // Why doesn't the copy constructor do this when we have pre-locked pixels?
1041 bitmap.lockPixels();
1042
1043 SkString md5;
1044 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
1045 SkMD5 hash;
1046 if (data->getLength()) {
1047 hash.writeStream(data, data->getLength());
1048 data->rewind();
mtklein38408462015-07-08 07:25:27 -07001049 } else {
mtkleincd50bca2016-01-05 06:20:20 -08001050 // If we're BGRA (Linux, Windows), swizzle over to RGBA (Mac, Android).
1051 // This helps eliminate multiple 0-pixel-diff hashes on gold.skia.org.
1052 // (Android's general slow speed breaks the tie arbitrarily in RGBA's favor.)
1053 // We might consider promoting 565 to RGBA too.
1054 if (bitmap.colorType() == kBGRA_8888_SkColorType) {
1055 SkBitmap swizzle;
1056 SkAssertResult(bitmap.copyTo(&swizzle, kRGBA_8888_SkColorType));
1057 hash.write(swizzle.getPixels(), swizzle.getSize());
1058 } else {
1059 hash.write(bitmap.getPixels(), bitmap.getSize());
1060 }
1061 }
1062 SkMD5::Digest digest;
1063 hash.finish(digest);
1064 for (int i = 0; i < 16; i++) {
1065 md5.appendf("%02x", digest.data[i]);
mtklein38408462015-07-08 07:25:27 -07001066 }
mtklein62bd1a62015-01-27 14:46:26 -08001067 }
mtklein62bd1a62015-01-27 14:46:26 -08001068
mtkleincd50bca2016-01-05 06:20:20 -08001069 if (!FLAGS_readPath.isEmpty() &&
mtklein3eed7dd2016-02-24 19:07:07 -08001070 !gGold.contains(Gold(task.sink.tag, task.src.tag,
1071 task.src.options, name, md5))) {
mtkleincd50bca2016-01-05 06:20:20 -08001072 fail(SkStringPrintf("%s not found for %s %s %s %s in %s",
1073 md5.c_str(),
mtklein21eaf3b2016-02-08 12:39:59 -08001074 task.sink.tag.c_str(),
1075 task.src.tag.c_str(),
1076 task.src.options.c_str(),
mtkleincd50bca2016-01-05 06:20:20 -08001077 name.c_str(),
1078 FLAGS_readPath[0]));
mtklein748ca3b2015-01-15 10:56:12 -08001079 }
mtkleincd50bca2016-01-05 06:20:20 -08001080
1081 if (!FLAGS_writePath.isEmpty()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001082 const char* ext = task.sink->fileExtension();
mtkleincd50bca2016-01-05 06:20:20 -08001083 if (data->getLength()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001084 WriteToDisk(task, md5, ext, data, data->getLength(), nullptr);
mtkleincd50bca2016-01-05 06:20:20 -08001085 SkASSERT(bitmap.drawsNothing());
1086 } else if (!bitmap.drawsNothing()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001087 WriteToDisk(task, md5, ext, nullptr, 0, &bitmap);
mtkleincd50bca2016-01-05 06:20:20 -08001088 }
1089 }
1090 });
mtklein748ca3b2015-01-15 10:56:12 -08001091 }
mtklein3eed7dd2016-02-24 19:07:07 -08001092 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 -08001093 }
1094
1095 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -08001096 SkString md5,
1097 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -08001098 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -08001099 const SkBitmap* bitmap) {
mtklein409d4702016-02-29 07:38:01 -08001100 bool gammaCorrect = false;
1101 if (bitmap) {
brianosman898235c2016-04-06 07:38:23 -07001102 gammaCorrect = SkImageInfoIsGammaCorrect(bitmap->info());
mtklein409d4702016-02-29 07:38:01 -08001103 }
1104
mtklein748ca3b2015-01-15 10:56:12 -08001105 JsonWriter::BitmapResult result;
djsollen54416de2015-04-03 07:24:48 -07001106 result.name = task.src->name();
mtklein3eed7dd2016-02-24 19:07:07 -08001107 result.config = task.sink.tag;
djsollen54416de2015-04-03 07:24:48 -07001108 result.sourceType = task.src.tag;
1109 result.sourceOptions = task.src.options;
1110 result.ext = ext;
mtklein409d4702016-02-29 07:38:01 -08001111 result.gammaCorrect = gammaCorrect;
djsollen54416de2015-04-03 07:24:48 -07001112 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -08001113 JsonWriter::AddBitmapResult(result);
1114
mtkleinb0531a72015-04-07 13:38:48 -07001115 // If an MD5 is uninteresting, we want it noted in the JSON file,
1116 // but don't want to dump it out as a .png (or whatever ext is).
1117 if (gUninterestingHashes.contains(md5)) {
1118 return;
1119 }
1120
mtklein748ca3b2015-01-15 10:56:12 -08001121 const char* dir = FLAGS_writePath[0];
1122 if (0 == strcmp(dir, "@")) { // Needed for iOS.
1123 dir = FLAGS_resourcePath[0];
1124 }
1125 sk_mkdir(dir);
1126
1127 SkString path;
1128 if (FLAGS_nameByHash) {
1129 path = SkOSPath::Join(dir, result.md5.c_str());
1130 path.append(".");
1131 path.append(ext);
1132 if (sk_exists(path.c_str())) {
1133 return; // Content-addressed. If it exists already, we're done.
1134 }
1135 } else {
kkinnunen3e980c32015-12-23 01:33:00 -08001136 path = SkOSPath::Join(dir, task.sink.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001137 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001138 path = SkOSPath::Join(path.c_str(), task.src.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001139 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001140 if (strcmp(task.src.options.c_str(), "") != 0) {
1141 path = SkOSPath::Join(path.c_str(), task.src.options.c_str());
djsollen54416de2015-04-03 07:24:48 -07001142 sk_mkdir(path.c_str());
1143 }
mtklein748ca3b2015-01-15 10:56:12 -08001144 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
1145 path.append(".");
1146 path.append(ext);
1147 }
1148
mtklein748ca3b2015-01-15 10:56:12 -08001149 if (bitmap) {
mtkleina5114d72015-08-24 13:27:01 -07001150 if (!dump_png(*bitmap, path.c_str(), result.md5.c_str())) {
mtklein748ca3b2015-01-15 10:56:12 -08001151 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
1152 return;
1153 }
1154 } else {
mtkleina5114d72015-08-24 13:27:01 -07001155 SkFILEWStream file(path.c_str());
1156 if (!file.isValid()) {
1157 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
1158 return;
1159 }
mtklein748ca3b2015-01-15 10:56:12 -08001160 if (!file.writeStream(data, len)) {
1161 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
1162 return;
1163 }
1164 }
1165 }
1166};
1167
mtklein748ca3b2015-01-15 10:56:12 -08001168/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1169
1170// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
1171
mtklein21eaf3b2016-02-08 12:39:59 -08001172static SkTDArray<skiatest::Test> gParallelTests, gSerialTests;
mtklein748ca3b2015-01-15 10:56:12 -08001173
1174static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -08001175 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -08001176 return;
1177 }
mtklein6393c062015-04-27 08:45:01 -07001178 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r; r = r->next()) {
1179 if (!in_shard()) {
1180 continue;
1181 }
halcanary87f3ba42015-01-20 09:30:20 -08001182 // Despite its name, factory() is returning a reference to
1183 // link-time static const POD data.
1184 const skiatest::Test& test = r->factory();
1185 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -08001186 continue;
1187 }
halcanary87f3ba42015-01-20 09:30:20 -08001188 if (test.needsGpu && gpu_supported()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001189 (FLAGS_gpu_threading ? gParallelTests : gSerialTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -08001190 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein21eaf3b2016-02-08 12:39:59 -08001191 gParallelTests.push(test);
mtklein82d28432015-01-15 12:46:02 -08001192 }
mtklein748ca3b2015-01-15 10:56:12 -08001193 }
1194}
1195
mtklein21eaf3b2016-02-08 12:39:59 -08001196static void run_test(skiatest::Test test) {
halcanary87f3ba42015-01-20 09:30:20 -08001197 struct : public skiatest::Reporter {
mtklein36352bf2015-03-25 18:17:31 -07001198 void reportFailed(const skiatest::Failure& failure) override {
halcanary87f3ba42015-01-20 09:30:20 -08001199 fail(failure.toString());
1200 JsonWriter::AddTestFailure(failure);
1201 }
mtklein36352bf2015-03-25 18:17:31 -07001202 bool allowExtendedTest() const override {
halcanary87f3ba42015-01-20 09:30:20 -08001203 return FLAGS_pathOpsExtended;
1204 }
mtklein36352bf2015-03-25 18:17:31 -07001205 bool verbose() const override { return FLAGS_veryVerbose; }
halcanary87f3ba42015-01-20 09:30:20 -08001206 } reporter;
djsollen824996a2015-06-12 12:06:22 -07001207
mtklein3eed7dd2016-02-24 19:07:07 -08001208 if (!FLAGS_dryRun && !is_blacklisted("_", "tests", "_", test.name)) {
mtklein21eaf3b2016-02-08 12:39:59 -08001209 start("unit", "test", "", test.name);
mtklein55e88b22015-01-21 15:50:13 -08001210 GrContextFactory factory;
mtklein21eaf3b2016-02-08 12:39:59 -08001211 test.proc(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -08001212 }
mtklein3eed7dd2016-02-24 19:07:07 -08001213 done("unit", "test", "", test.name);
mtklein748ca3b2015-01-15 10:56:12 -08001214}
1215
1216/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1217
mtkleinafae30a2016-02-24 12:28:32 -08001218DEFINE_int32(status_sec, 15, "Print status this often (and if we crash).");
1219
1220SkThread* start_status_thread() {
1221 auto thread = new SkThread([] (void*) {
1222 for (;;) {
1223 print_status();
1224 #if defined(SK_BUILD_FOR_WIN)
1225 Sleep(FLAGS_status_sec * 1000);
1226 #else
1227 sleep(FLAGS_status_sec);
1228 #endif
mtklein2e1c47e2015-03-12 07:16:56 -07001229 }
mtkleinafae30a2016-02-24 12:28:32 -08001230 });
1231 thread->start();
1232 return thread;
mtkleinde6fc2b2015-03-12 06:28:54 -07001233}
1234
caryclark83ca6282015-06-10 09:31:09 -07001235#define PORTABLE_FONT_PREFIX "Toy Liberation "
1236
mbocee6a9912016-05-31 11:42:36 -07001237static sk_sp<SkTypeface> create_from_name(const char familyName[], SkFontStyle style) {
caryclark83ca6282015-06-10 09:31:09 -07001238 if (familyName && strlen(familyName) > sizeof(PORTABLE_FONT_PREFIX)
1239 && !strncmp(familyName, PORTABLE_FONT_PREFIX, sizeof(PORTABLE_FONT_PREFIX) - 1)) {
caryclark1818acb2015-07-24 12:09:25 -07001240 return sk_tool_utils::create_portable_typeface(familyName, style);
caryclark83ca6282015-06-10 09:31:09 -07001241 }
halcanary96fcdcc2015-08-27 07:41:13 -07001242 return nullptr;
caryclark83ca6282015-06-10 09:31:09 -07001243}
1244
1245#undef PORTABLE_FONT_PREFIX
1246
mbocee6a9912016-05-31 11:42:36 -07001247extern sk_sp<SkTypeface> (*gCreateTypefaceDelegate)(const char [], SkFontStyle );
caryclark83ca6282015-06-10 09:31:09 -07001248
herb3f996092016-05-27 08:15:05 -07001249extern bool gDefaultProfileIsSRGB;
1250
jcgregorio3b27ade2014-11-13 08:06:40 -08001251int dm_main();
caryclark17f0b6d2014-07-22 10:15:34 -07001252int dm_main() {
bsalomon71de3532016-04-26 14:27:21 -07001253 setbuf(stdout, nullptr);
mtklein246ba3a2016-02-23 10:39:36 -08001254 setup_crash_handler();
mtkleinafae30a2016-02-24 12:28:32 -08001255
mtkleina483da32016-03-17 12:53:36 -07001256 if (FLAGS_verbose) {
1257 gVLog = stderr;
1258 } else if (!FLAGS_writePath.isEmpty()) {
mtklein51c8cfc2016-03-11 12:59:09 -08001259 sk_mkdir(FLAGS_writePath[0]);
1260 gVLog = freopen(SkOSPath::Join(FLAGS_writePath[0], "verbose.log").c_str(), "w", stderr);
mtkleinc41fd922016-03-08 09:01:39 -08001261 }
1262
herb3f996092016-05-27 08:15:05 -07001263 if (FLAGS_forceSRGB) {
1264 gDefaultProfileIsSRGB = true;
1265 }
1266
mtklein5286f022016-01-22 08:18:14 -08001267 JsonWriter::DumpJson(); // It's handy for the bots to assume this is ~never missing.
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +00001268 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -07001269 SkTaskGroup::Enabler enabled(FLAGS_threads);
caryclark83ca6282015-06-10 09:31:09 -07001270 gCreateTypefaceDelegate = &create_from_name;
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +00001271
halcanary7d571242016-02-24 17:59:16 -08001272 {
1273 SkString testResourcePath = GetResourcePath("color_wheel.png");
1274 SkFILEStream testResource(testResourcePath.c_str());
1275 if (!testResource.isValid()) {
mtkleinc41fd922016-03-08 09:01:39 -08001276 info("Some resources are missing. Do you need to set --resourcePath?\n");
halcanary7d571242016-02-24 17:59:16 -08001277 }
1278 }
mtklein62bd1a62015-01-27 14:46:26 -08001279 gather_gold();
borenet09ed4802015-04-03 14:15:33 -07001280 gather_uninteresting_hashes();
mtklein62bd1a62015-01-27 14:46:26 -08001281
scroggo86737142016-02-03 12:19:11 -08001282 if (!gather_srcs()) {
1283 return 1;
1284 }
brianosman05c987c2016-05-09 13:42:16 -07001285 if (!gather_sinks()) {
1286 return 1;
1287 }
mtklein748ca3b2015-01-15 10:56:12 -08001288 gather_tests();
mtklein21eaf3b2016-02-08 12:39:59 -08001289 gPending = gSrcs.count() * gSinks.count() + gParallelTests.count() + gSerialTests.count();
mtkleinc41fd922016-03-08 09:01:39 -08001290 info("%d srcs * %d sinks + %d tests == %d tasks",
1291 gSrcs.count(), gSinks.count(), gParallelTests.count() + gSerialTests.count(), gPending);
mtkleinafae30a2016-02-24 12:28:32 -08001292 SkAutoTDelete<SkThread> statusThread(start_status_thread());
mtklein748ca3b2015-01-15 10:56:12 -08001293
mtklein21eaf3b2016-02-08 12:39:59 -08001294 // Kick off as much parallel work as we can, making note of any serial work we'll need to do.
1295 SkTaskGroup parallel;
1296 SkTArray<Task> serial;
1297
1298 for (auto& sink : gSinks)
1299 for (auto& src : gSrcs) {
mtklein3eed7dd2016-02-24 19:07:07 -08001300 if (src->veto(sink->flags()) ||
1301 is_blacklisted(sink.tag.c_str(), src.tag.c_str(),
1302 src.options.c_str(), src->name().c_str())) {
mtklein15923c92016-02-29 10:14:38 -08001303 SkAutoTAcquire<SkSpinlock> lock(gMutex);
mtklein3eed7dd2016-02-24 19:07:07 -08001304 gPending--;
1305 continue;
1306 }
1307
mtklein21eaf3b2016-02-08 12:39:59 -08001308 Task task(src, sink);
1309 if (src->serial() || sink->serial()) {
1310 serial.push_back(task);
1311 } else {
1312 parallel.add([task] { Task::Run(task); });
mtklein748ca3b2015-01-15 10:56:12 -08001313 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001314 }
mtklein21eaf3b2016-02-08 12:39:59 -08001315 for (auto test : gParallelTests) {
1316 parallel.add([test] { run_test(test); });
mtklein55e88b22015-01-21 15:50:13 -08001317 }
mtklein21eaf3b2016-02-08 12:39:59 -08001318
1319 // With the parallel work running, run serial tasks and tests here on main thread.
1320 for (auto task : serial) { Task::Run(task); }
1321 for (auto test : gSerialTests) { run_test(test); }
1322
1323 // Wait for any remaining parallel work to complete (including any spun off of serial tasks).
1324 parallel.wait();
mtkleincd50bca2016-01-05 06:20:20 -08001325 gDefinitelyThreadSafeWork.wait();
1326
mtkleinda884c42016-02-24 18:00:23 -08001327 // We'd better have run everything.
1328 SkASSERT(gPending == 0);
mtkleine027f172016-02-26 15:53:06 -08001329 // Make sure we've flushed all our results to disk.
1330 JsonWriter::DumpJson();
mtkleinda884c42016-02-24 18:00:23 -08001331
mtklein748ca3b2015-01-15 10:56:12 -08001332 // At this point we're back in single-threaded land.
caryclarkf53ce802015-06-15 06:48:30 -07001333 sk_tool_utils::release_portable_typefaces();
mtklein@google.comd36522d2013-10-16 13:02:15 +00001334
mtklein748ca3b2015-01-15 10:56:12 -08001335 if (gFailures.count() > 0) {
mtkleinc41fd922016-03-08 09:01:39 -08001336 info("Failures:\n");
mtklein748ca3b2015-01-15 10:56:12 -08001337 for (int i = 0; i < gFailures.count(); i++) {
mtkleinc41fd922016-03-08 09:01:39 -08001338 info("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001339 }
mtkleinc41fd922016-03-08 09:01:39 -08001340 info("%d failures\n", gFailures.count());
mtklein748ca3b2015-01-15 10:56:12 -08001341 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001342 }
mtkleinafae30a2016-02-24 12:28:32 -08001343
1344#ifdef SK_PDF_IMAGE_STATS
halcanary7a14b312015-10-01 07:28:13 -07001345 SkPDFImageDumpStats();
mtkleinafae30a2016-02-24 12:28:32 -08001346#endif // SK_PDF_IMAGE_STATS
1347
1348 print_status();
mtkleinc41fd922016-03-08 09:01:39 -08001349 info("Finished!\n");
mtklein748ca3b2015-01-15 10:56:12 -08001350 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +00001351}
jcgregorio3b27ade2014-11-13 08:06:40 -08001352
kkinnunen179a8f52015-11-20 13:32:24 -08001353// TODO: currently many GPU tests are declared outside SK_SUPPORT_GPU guards.
1354// Thus we export the empty RunWithGPUTestContexts when SK_SUPPORT_GPU=0.
1355namespace skiatest {
bsalomon758586c2016-04-06 14:02:39 -07001356
1357#if SK_SUPPORT_GPU
1358bool IsGLContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1359 return kOpenGL_GrBackend == GrContextFactory::ContextTypeBackend(type);
1360}
bsalomondc0fcd42016-04-11 14:21:33 -07001361bool IsVulkanContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1362 return kVulkan_GrBackend == GrContextFactory::ContextTypeBackend(type);
1363}
bsalomon758586c2016-04-06 14:02:39 -07001364bool IsRenderingGLContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1365 return IsGLContextType(type) && GrContextFactory::IsRenderingContext(type);
1366}
1367bool IsNullGLContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1368 return type == GrContextFactory::kNullGL_ContextType;
1369}
1370#else
1371bool IsGLContextType(int) { return false; }
bsalomondc0fcd42016-04-11 14:21:33 -07001372bool IsVulkanContextType(int) { return false; }
bsalomon758586c2016-04-06 14:02:39 -07001373bool IsRenderingGLContextType(int) { return false; }
1374bool IsNullGLContextType(int) { return false; }
1375#endif
1376
1377void RunWithGPUTestContexts(GrContextTestFn* test, GrContextTypeFilterFn* contextTypeFilter,
bsalomonf2f1c172016-04-05 12:59:06 -07001378 Reporter* reporter, GrContextFactory* factory) {
kkinnunen179a8f52015-11-20 13:32:24 -08001379#if SK_SUPPORT_GPU
kkinnunen3e980c32015-12-23 01:33:00 -08001380
bsalomon758586c2016-04-06 14:02:39 -07001381 for (int typeInt = 0; typeInt < GrContextFactory::kContextTypeCnt; ++typeInt) {
1382 GrContextFactory::ContextType contextType = (GrContextFactory::ContextType) typeInt;
1383 ContextInfo ctxInfo = factory->getContextInfo(contextType);
bsalomonfda88072016-04-11 14:40:50 -07001384 if (contextTypeFilter && !(*contextTypeFilter)(contextType)) {
kkinnunen179a8f52015-11-20 13:32:24 -08001385 continue;
1386 }
bsalomon758586c2016-04-06 14:02:39 -07001387 // Use "native" instead of explicitly trying OpenGL and OpenGL ES. Do not use GLES on,
1388 // desktop since tests do not account for not fixing http://skbug.com/2809
1389 if (contextType == GrContextFactory::kGL_ContextType ||
1390 contextType == GrContextFactory::kGLES_ContextType) {
1391 if (contextType != GrContextFactory::kNativeGL_ContextType) {
1392 continue;
1393 }
1394 }
bsalomon8b7451a2016-05-11 06:33:06 -07001395 if (ctxInfo.grContext()) {
bsalomonf2f1c172016-04-05 12:59:06 -07001396 (*test)(reporter, ctxInfo);
kkinnunen5219fd92015-12-10 06:28:13 -08001397 }
bsalomonf2f1c172016-04-05 12:59:06 -07001398 ctxInfo = factory->getContextInfo(contextType,
bsalomon85b4b532016-04-05 11:06:27 -07001399 GrContextFactory::kEnableNVPR_ContextOptions);
bsalomon8b7451a2016-05-11 06:33:06 -07001400 if (ctxInfo.grContext()) {
bsalomonf2f1c172016-04-05 12:59:06 -07001401 (*test)(reporter, ctxInfo);
kkinnunen179a8f52015-11-20 13:32:24 -08001402 }
1403 }
1404#endif
1405}
kkinnunen179a8f52015-11-20 13:32:24 -08001406} // namespace skiatest
1407
borenet48087572015-04-02 12:16:36 -07001408#if !defined(SK_BUILD_FOR_IOS)
jcgregorio3b27ade2014-11-13 08:06:40 -08001409int main(int argc, char** argv) {
1410 SkCommandLineFlags::Parse(argc, argv);
1411 return dm_main();
1412}
1413#endif