blob: 56c3b66be00ec6aa29a81dedd6082d44d4acf40a [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"
mtklein748ca3b2015-01-15 10:56:12 -080025#include "SkMD5.h"
mtklein1b249332015-07-07 12:21:21 -070026#include "SkMutex.h"
mtklein1d0f1642014-09-08 08:05:18 -070027#include "SkOSFile.h"
mtklein27c3fdd2016-02-26 14:43:21 -080028#include "SkPM4fPriv.h"
mtklein246ba3a2016-02-23 10:39:36 -080029#include "SkSpinlock.h"
mtkleina82f5622015-02-20 12:30:19 -080030#include "SkTHash.h"
mtklein406654b2014-09-03 15:34:37 -070031#include "SkTaskGroup.h"
mtkleinde6fc2b2015-03-12 06:28:54 -070032#include "SkThreadUtils.h"
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +000033#include "Test.h"
mtklein748ca3b2015-01-15 10:56:12 -080034#include "Timer.h"
brianosman3c579dc2016-04-19 09:18:11 -070035#include "picture_utils.h"
caryclark83ca6282015-06-10 09:31:09 -070036#include "sk_tool_utils.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +000037
halcanary7a14b312015-10-01 07:28:13 -070038#ifdef SK_PDF_IMAGE_STATS
39extern void SkPDFImageDumpStats();
40#endif
41
mtkleina5114d72015-08-24 13:27:01 -070042#include "png.h"
43
bungeman60e0fee2015-08-26 05:15:46 -070044#include <stdlib.h>
45
scroggo27a58342015-10-28 08:56:41 -070046#ifndef SK_BUILD_FOR_WIN32
47 #include <unistd.h>
48#endif
49
djsollen54416de2015-04-03 07:24:48 -070050DEFINE_string(src, "tests gm skp image", "Source types to test.");
mtklein748ca3b2015-01-15 10:56:12 -080051DEFINE_bool(nameByHash, false,
52 "If true, write to FLAGS_writePath[0]/<hash>.png instead of "
djsollen54416de2015-04-03 07:24:48 -070053 "to FLAGS_writePath[0]/<config>/<sourceType>/<sourceOptions>/<name>.png");
mtklein748ca3b2015-01-15 10:56:12 -080054DEFINE_bool2(pathOpsExtended, x, false, "Run extended pathOps tests.");
mtkleind603b222015-02-17 11:13:33 -080055DEFINE_string(matrix, "1 0 0 1",
56 "2x2 scale+skew matrix to apply or upright when using "
57 "'matrix' or 'upright' in config.");
mtklein82d28432015-01-15 12:46:02 -080058DEFINE_bool(gpu_threading, false, "Allow GPU work to run on multiple threads?");
mtklein@google.comd36522d2013-10-16 13:02:15 +000059
mtkleina2ef6422015-01-15 13:44:22 -080060DEFINE_string(blacklist, "",
djsollen54416de2015-04-03 07:24:48 -070061 "Space-separated config/src/srcOptions/name quadruples to blacklist. '_' matches anything. E.g. \n"
62 "'--blacklist gpu skp _ _' will blacklist all SKPs drawn into the gpu config.\n"
63 "'--blacklist gpu skp _ _ 8888 gm _ aarects' will also blacklist the aarects GM on 8888.");
mtkleina2ef6422015-01-15 13:44:22 -080064
mtklein62bd1a62015-01-27 14:46:26 -080065DEFINE_string2(readPath, r, "", "If set check for equality with golden results in this directory.");
66
borenet09ed4802015-04-03 14:15:33 -070067DEFINE_string(uninterestingHashesFile, "",
68 "File containing a list of uninteresting hashes. If a result hashes to something in "
69 "this list, no image is written for that result.");
70
mtklein6393c062015-04-27 08:45:01 -070071DEFINE_int32(shards, 1, "We're splitting source data into this many shards.");
72DEFINE_int32(shard, 0, "Which shard do I run?");
scroggoe4499842016-02-25 11:03:47 -080073DEFINE_bool(simpleCodec, false, "Only decode images to native scale");
mtklein6393c062015-04-27 08:45:01 -070074
mtklein748ca3b2015-01-15 10:56:12 -080075using namespace DM;
bsalomon3724e572016-03-30 18:56:19 -070076using sk_gpu_test::GrContextFactory;
bsalomon273c0f52016-03-31 10:59:06 -070077using sk_gpu_test::GLTestContext;
bsalomonf2f1c172016-04-05 12:59:06 -070078using sk_gpu_test::ContextInfo;
mtklein@google.comd36522d2013-10-16 13:02:15 +000079
mtklein748ca3b2015-01-15 10:56:12 -080080/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
81
benjaminwagnerec4d4d72016-03-25 12:59:53 -070082static const double kStartMs = SkTime::GetMSecs();
mtkleinc41fd922016-03-08 09:01:39 -080083
84static FILE* gVLog;
85
86template <typename... Args>
87static void vlog(const char* fmt, Args&&... args) {
88 if (gVLog) {
89 fprintf(gVLog, "%s\t", HumanizeMs(SkTime::GetMSecs() - kStartMs).c_str());
90 fprintf(gVLog, fmt, args...);
91 fflush(gVLog);
92 }
93}
94
95template <typename... Args>
96static void info(const char* fmt, Args&&... args) {
97 vlog(fmt, args...);
98 if (!FLAGS_quiet) {
99 printf(fmt, args...);
100 }
101}
102static void info(const char* fmt) {
103 if (!FLAGS_quiet) {
104 printf("%s", fmt); // Clang warns printf(fmt) is insecure.
105 }
106}
107
reed086eea92016-05-04 17:12:46 -0700108SK_DECLARE_STATIC_MUTEX(gFailuresMutex);
mtklein748ca3b2015-01-15 10:56:12 -0800109static SkTArray<SkString> gFailures;
110
mtklein3eed7dd2016-02-24 19:07:07 -0800111static void fail(const SkString& err) {
mtklein748ca3b2015-01-15 10:56:12 -0800112 SkAutoMutexAcquire lock(gFailuresMutex);
113 SkDebugf("\n\nFAILURE: %s\n\n", err.c_str());
114 gFailures.push_back(err);
halcanary9e398f72015-01-10 11:18:04 -0800115}
116
mtkleinb37cb412015-03-18 05:27:14 -0700117
mtklein246ba3a2016-02-23 10:39:36 -0800118// We use a spinlock to make locking this in a signal handler _somewhat_ safe.
mtklein15923c92016-02-29 10:14:38 -0800119static SkSpinlock gMutex;
mtklein3eed7dd2016-02-24 19:07:07 -0800120static int32_t gPending;
121static SkTArray<SkString> gRunning;
mtklein748ca3b2015-01-15 10:56:12 -0800122
mtklein3eed7dd2016-02-24 19:07:07 -0800123static void done(const char* config, const char* src, const char* srcOptions, const char* name) {
124 SkString id = SkStringPrintf("%s %s %s %s", config, src, srcOptions, name);
mtkleinc41fd922016-03-08 09:01:39 -0800125 vlog("done %s\n", id.c_str());
mtkleinafae30a2016-02-24 12:28:32 -0800126 int pending;
mtkleinb37cb412015-03-18 05:27:14 -0700127 {
mtklein15923c92016-02-29 10:14:38 -0800128 SkAutoTAcquire<SkSpinlock> lock(gMutex);
mtkleinb37cb412015-03-18 05:27:14 -0700129 for (int i = 0; i < gRunning.count(); i++) {
130 if (gRunning[i] == id) {
131 gRunning.removeShuffle(i);
132 break;
133 }
134 }
mtkleinafae30a2016-02-24 12:28:32 -0800135 pending = --gPending;
reed50bc0512015-05-19 14:13:31 -0700136 }
mtkleina17241b2015-01-23 05:48:00 -0800137 // We write our dm.json file every once in a while in case we crash.
138 // Notice this also handles the final dm.json when pending == 0.
139 if (pending % 500 == 0) {
140 JsonWriter::DumpJson();
141 }
mtklein@google.comd36522d2013-10-16 13:02:15 +0000142}
143
mtklein3eed7dd2016-02-24 19:07:07 -0800144static void start(const char* config, const char* src, const char* srcOptions, const char* name) {
145 SkString id = SkStringPrintf("%s %s %s %s", config, src, srcOptions, name);
mtkleinc41fd922016-03-08 09:01:39 -0800146 vlog("start %s\n", id.c_str());
mtklein15923c92016-02-29 10:14:38 -0800147 SkAutoTAcquire<SkSpinlock> lock(gMutex);
mtkleinb37cb412015-03-18 05:27:14 -0700148 gRunning.push_back(id);
149}
150
mtkleinafae30a2016-02-24 12:28:32 -0800151static void print_status() {
mtkleinafae30a2016-02-24 12:28:32 -0800152 int curr = sk_tools::getCurrResidentSetSizeMB(),
153 peak = sk_tools::getMaxResidentSetSizeMB();
mtkleinc41fd922016-03-08 09:01:39 -0800154 SkString elapsed = HumanizeMs(SkTime::GetMSecs() - kStartMs);
mtkleinafae30a2016-02-24 12:28:32 -0800155
mtklein15923c92016-02-29 10:14:38 -0800156 SkAutoTAcquire<SkSpinlock> lock(gMutex);
mtkleinc41fd922016-03-08 09:01:39 -0800157 info("\n%s elapsed, %d active, %d queued, %dMB RAM, %dMB peak\n",
158 elapsed.c_str(), gRunning.count(), gPending - gRunning.count(), curr, peak);
mtkleinafae30a2016-02-24 12:28:32 -0800159 for (auto& task : gRunning) {
mtkleinc41fd922016-03-08 09:01:39 -0800160 info("\t%s\n", task.c_str());
mtkleinafae30a2016-02-24 12:28:32 -0800161 }
162}
163
mtkleinf8557292016-02-29 06:35:28 -0800164// Yo dawg, I heard you like signals so I caught a signal in your
165// signal handler so you can handle signals while you handle signals.
166// Let's not get into that situation. Only print if we're the first ones to get a crash signal.
167static std::atomic<bool> in_signal_handler{false};
168
mtklein246ba3a2016-02-23 10:39:36 -0800169#if defined(SK_BUILD_FOR_WIN32)
mtkleinf8557292016-02-29 06:35:28 -0800170 static LONG WINAPI handler(EXCEPTION_POINTERS* e) {
171 static const struct {
172 const char* name;
robertphillips75467862016-03-01 14:10:23 -0800173 DWORD code;
mtkleinf8557292016-02-29 06:35:28 -0800174 } kExceptions[] = {
175 #define _(E) {#E, E}
176 _(EXCEPTION_ACCESS_VIOLATION),
177 _(EXCEPTION_BREAKPOINT),
178 _(EXCEPTION_INT_DIVIDE_BY_ZERO),
179 _(EXCEPTION_STACK_OVERFLOW),
180 // TODO: more?
181 #undef _
182 };
183
184 if (!in_signal_handler.exchange(true)) {
185 const DWORD code = e->ExceptionRecord->ExceptionCode;
mtkleinc41fd922016-03-08 09:01:39 -0800186 info("\nCaught exception %u", code);
mtkleinf8557292016-02-29 06:35:28 -0800187 for (const auto& exception : kExceptions) {
188 if (exception.code == code) {
mtkleinc41fd922016-03-08 09:01:39 -0800189 info(" %s", exception.name);
mtkleinf8557292016-02-29 06:35:28 -0800190 }
191 }
mtkleinc41fd922016-03-08 09:01:39 -0800192 info("\n");
mtkleinf8557292016-02-29 06:35:28 -0800193 print_status();
mtklein13bf3392016-03-17 10:16:12 -0700194 fflush(stdout);
mtkleinf8557292016-02-29 06:35:28 -0800195 }
196 // Execute default exception handler... hopefully, exit.
197 return EXCEPTION_EXECUTE_HANDLER;
mtklein246ba3a2016-02-23 10:39:36 -0800198 }
mtkleinf8557292016-02-29 06:35:28 -0800199 static void setup_crash_handler() { SetUnhandledExceptionFilter(handler); }
mtklein246ba3a2016-02-23 10:39:36 -0800200
mtklein819ab102016-02-29 17:02:52 -0800201#elif !defined(SK_BUILD_FOR_ANDROID)
202 #include <execinfo.h>
mtklein246ba3a2016-02-23 10:39:36 -0800203 #include <signal.h>
mtklein819ab102016-02-29 17:02:52 -0800204 #include <stdlib.h>
mtkleinf8557292016-02-29 06:35:28 -0800205
mtklein246ba3a2016-02-23 10:39:36 -0800206 static void setup_crash_handler() {
207 const int kSignals[] = { SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGSEGV };
208 for (int sig : kSignals) {
209 signal(sig, [](int sig) {
mtkleinf8557292016-02-29 06:35:28 -0800210 if (!in_signal_handler.exchange(true)) {
mtklein819ab102016-02-29 17:02:52 -0800211 SkAutoTAcquire<SkSpinlock> lock(gMutex);
mtkleinc41fd922016-03-08 09:01:39 -0800212 info("\nCaught signal %d [%s], was running:\n", sig, strsignal(sig));
mtklein819ab102016-02-29 17:02:52 -0800213 for (auto& task : gRunning) {
mtkleinc41fd922016-03-08 09:01:39 -0800214 info("\t%s\n", task.c_str());
mtklein819ab102016-02-29 17:02:52 -0800215 }
216
217 void* stack[64];
218 int count = backtrace(stack, SK_ARRAY_COUNT(stack));
219 char** symbols = backtrace_symbols(stack, count);
mtkleinc41fd922016-03-08 09:01:39 -0800220 info("\nStack trace:\n");
mtklein819ab102016-02-29 17:02:52 -0800221 for (int i = 0; i < count; i++) {
mtkleinc41fd922016-03-08 09:01:39 -0800222 info(" %s\n", symbols[i]);
mtklein819ab102016-02-29 17:02:52 -0800223 }
mtklein13bf3392016-03-17 10:16:12 -0700224 fflush(stdout);
mtkleinf8557292016-02-29 06:35:28 -0800225 }
mtklein819ab102016-02-29 17:02:52 -0800226 _Exit(sig);
mtklein246ba3a2016-02-23 10:39:36 -0800227 });
228 }
229 }
mtklein819ab102016-02-29 17:02:52 -0800230
231#else // Android
232 static void setup_crash_handler() {}
mtklein246ba3a2016-02-23 10:39:36 -0800233#endif
234
mtklein748ca3b2015-01-15 10:56:12 -0800235/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
mtklein114c3cd2015-01-15 10:15:02 -0800236
mtklein62bd1a62015-01-27 14:46:26 -0800237struct Gold : public SkString {
mtkleina82f5622015-02-20 12:30:19 -0800238 Gold() : SkString("") {}
mtklein3eed7dd2016-02-24 19:07:07 -0800239 Gold(const SkString& sink, const SkString& src,
240 const SkString& srcOptions, const SkString& name,
241 const SkString& md5)
mtklein62bd1a62015-01-27 14:46:26 -0800242 : SkString("") {
243 this->append(sink);
244 this->append(src);
djsollen54416de2015-04-03 07:24:48 -0700245 this->append(srcOptions);
mtklein62bd1a62015-01-27 14:46:26 -0800246 this->append(name);
247 this->append(md5);
mtklein62bd1a62015-01-27 14:46:26 -0800248 }
mtkleinc8d1dd42015-10-15 12:23:01 -0700249 struct Hash {
250 uint32_t operator()(const Gold& g) const {
251 return SkGoodHash()((const SkString&)g);
252 }
253 };
mtklein62bd1a62015-01-27 14:46:26 -0800254};
mtkleina82f5622015-02-20 12:30:19 -0800255static SkTHashSet<Gold, Gold::Hash> gGold;
mtklein62bd1a62015-01-27 14:46:26 -0800256
257static void add_gold(JsonWriter::BitmapResult r) {
djsollen54416de2015-04-03 07:24:48 -0700258 gGold.add(Gold(r.config, r.sourceType, r.sourceOptions, r.name, r.md5));
mtklein62bd1a62015-01-27 14:46:26 -0800259}
260
261static void gather_gold() {
262 if (!FLAGS_readPath.isEmpty()) {
263 SkString path(FLAGS_readPath[0]);
264 path.append("/dm.json");
265 if (!JsonWriter::ReadJson(path.c_str(), add_gold)) {
266 fail(SkStringPrintf("Couldn't read %s for golden results.", path.c_str()));
267 }
268 }
269}
270
271/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
272
mtklein935f1b12016-03-16 08:37:19 -0700273#if defined(SK_BUILD_FOR_WIN32)
274 static const char* kNewline = "\r\n";
275#else
276 static const char* kNewline = "\n";
277#endif
278
borenet09ed4802015-04-03 14:15:33 -0700279static SkTHashSet<SkString> gUninterestingHashes;
280
281static void gather_uninteresting_hashes() {
282 if (!FLAGS_uninterestingHashesFile.isEmpty()) {
283 SkAutoTUnref<SkData> data(SkData::NewFromFileName(FLAGS_uninterestingHashesFile[0]));
mtkleincc334b32015-09-22 11:43:53 -0700284 if (!data) {
mtkleinc41fd922016-03-08 09:01:39 -0800285 info("WARNING: unable to read uninteresting hashes from %s\n",
286 FLAGS_uninterestingHashesFile[0]);
mtkleincc334b32015-09-22 11:43:53 -0700287 return;
288 }
borenet09ed4802015-04-03 14:15:33 -0700289 SkTArray<SkString> hashes;
mtklein935f1b12016-03-16 08:37:19 -0700290 SkStrSplit((const char*)data->data(), kNewline, &hashes);
borenet09ed4802015-04-03 14:15:33 -0700291 for (const SkString& hash : hashes) {
292 gUninterestingHashes.add(hash);
293 }
mtkleinc41fd922016-03-08 09:01:39 -0800294 info("FYI: loaded %d distinct uninteresting hashes from %d lines\n",
295 gUninterestingHashes.count(), hashes.count());
borenet09ed4802015-04-03 14:15:33 -0700296 }
297}
298
299/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
300
mtkleine0effd62015-07-29 06:37:28 -0700301struct TaggedSrc : public SkAutoTDelete<Src> {
mtklein3eed7dd2016-02-24 19:07:07 -0800302 SkString tag;
303 SkString options;
mtkleine0effd62015-07-29 06:37:28 -0700304};
305
306struct TaggedSink : public SkAutoTDelete<Sink> {
kkinnunen3e980c32015-12-23 01:33:00 -0800307 SkString tag;
djsollen54416de2015-04-03 07:24:48 -0700308};
mtklein748ca3b2015-01-15 10:56:12 -0800309
310static const bool kMemcpyOK = true;
311
mtkleine0effd62015-07-29 06:37:28 -0700312static SkTArray<TaggedSrc, kMemcpyOK> gSrcs;
313static SkTArray<TaggedSink, kMemcpyOK> gSinks;
mtklein748ca3b2015-01-15 10:56:12 -0800314
mtklein6393c062015-04-27 08:45:01 -0700315static bool in_shard() {
316 static int N = 0;
317 return N++ % FLAGS_shards == FLAGS_shard;
318}
319
mtklein3eed7dd2016-02-24 19:07:07 -0800320static void push_src(const char* tag, ImplicitString options, Src* s) {
mtklein748ca3b2015-01-15 10:56:12 -0800321 SkAutoTDelete<Src> src(s);
mtklein6393c062015-04-27 08:45:01 -0700322 if (in_shard() &&
mtklein3eed7dd2016-02-24 19:07:07 -0800323 FLAGS_src.contains(tag) &&
mtklein748ca3b2015-01-15 10:56:12 -0800324 !SkCommandLineFlags::ShouldSkip(FLAGS_match, src->name().c_str())) {
mtkleine0effd62015-07-29 06:37:28 -0700325 TaggedSrc& s = gSrcs.push_back();
mtklein18300a32016-03-16 13:53:35 -0700326 s.reset(src.release());
mtklein748ca3b2015-01-15 10:56:12 -0800327 s.tag = tag;
djsollen54416de2015-04-03 07:24:48 -0700328 s.options = options;
mtklein114c3cd2015-01-15 10:15:02 -0800329 }
mtklein748ca3b2015-01-15 10:56:12 -0800330}
mtklein114c3cd2015-01-15 10:15:02 -0800331
msarett9e707a02015-09-01 14:57:57 -0700332static void push_codec_src(Path path, CodecSrc::Mode mode, CodecSrc::DstColorType dstColorType,
scroggoc5560be2016-02-03 09:42:42 -0800333 SkAlphaType dstAlphaType, float scale) {
scroggoe4499842016-02-25 11:03:47 -0800334 if (FLAGS_simpleCodec) {
335 if (mode != CodecSrc::kCodec_Mode || dstColorType != CodecSrc::kGetFromCanvas_DstColorType
336 || scale != 1.0f)
337 // Only decode in the simple case.
338 return;
339 }
msarett9e707a02015-09-01 14:57:57 -0700340 SkString folder;
341 switch (mode) {
342 case CodecSrc::kCodec_Mode:
343 folder.append("codec");
344 break;
msarettbb25b532016-01-13 09:31:39 -0800345 case CodecSrc::kCodecZeroInit_Mode:
346 folder.append("codec_zero_init");
347 break;
msarett9e707a02015-09-01 14:57:57 -0700348 case CodecSrc::kScanline_Mode:
349 folder.append("scanline");
350 break;
msarett9e707a02015-09-01 14:57:57 -0700351 case CodecSrc::kStripe_Mode:
352 folder.append("stripe");
353 break;
msarett91c22b22016-02-22 12:27:46 -0800354 case CodecSrc::kCroppedScanline_Mode:
355 folder.append("crop");
356 break;
msarett9e707a02015-09-01 14:57:57 -0700357 case CodecSrc::kSubset_Mode:
msarett36c37962015-09-02 13:20:52 -0700358 folder.append("codec_subset");
msarett9e707a02015-09-01 14:57:57 -0700359 break;
360 }
361
362 switch (dstColorType) {
363 case CodecSrc::kGrayscale_Always_DstColorType:
364 folder.append("_kGray8");
365 break;
366 case CodecSrc::kIndex8_Always_DstColorType:
367 folder.append("_kIndex8");
368 break;
msarett34e0ec42016-04-22 16:27:24 -0700369 case CodecSrc::kNonNative8888_Always_DstColorType:
370 folder.append("_kNonNative");
371 break;
msarett9e707a02015-09-01 14:57:57 -0700372 default:
373 break;
374 }
375
scroggoc5560be2016-02-03 09:42:42 -0800376 switch (dstAlphaType) {
scroggoc5560be2016-02-03 09:42:42 -0800377 case kPremul_SkAlphaType:
378 folder.append("_premul");
379 break;
380 case kUnpremul_SkAlphaType:
381 folder.append("_unpremul");
382 break;
383 default:
384 break;
385 }
386
msarett9e707a02015-09-01 14:57:57 -0700387 if (1.0f != scale) {
388 folder.appendf("_%.3f", scale);
389 }
390
scroggoc5560be2016-02-03 09:42:42 -0800391 CodecSrc* src = new CodecSrc(path, mode, dstColorType, dstAlphaType, scale);
msarett9e707a02015-09-01 14:57:57 -0700392 push_src("image", folder, src);
393}
394
scroggof8dc9df2016-05-16 09:04:13 -0700395static void push_android_codec_src(Path path, CodecSrc::DstColorType dstColorType,
396 SkAlphaType dstAlphaType, int sampleSize) {
msarett3d9d7a72015-10-21 10:27:10 -0700397 SkString folder;
scroggof8dc9df2016-05-16 09:04:13 -0700398 folder.append("scaled_codec");
msarett3d9d7a72015-10-21 10:27:10 -0700399
400 switch (dstColorType) {
401 case CodecSrc::kGrayscale_Always_DstColorType:
402 folder.append("_kGray8");
403 break;
404 case CodecSrc::kIndex8_Always_DstColorType:
405 folder.append("_kIndex8");
406 break;
msarett34e0ec42016-04-22 16:27:24 -0700407 case CodecSrc::kNonNative8888_Always_DstColorType:
408 folder.append("_kNonNative");
409 break;
msarett3d9d7a72015-10-21 10:27:10 -0700410 default:
411 break;
412 }
413
scroggoc5560be2016-02-03 09:42:42 -0800414 switch (dstAlphaType) {
scroggoc5560be2016-02-03 09:42:42 -0800415 case kPremul_SkAlphaType:
416 folder.append("_premul");
417 break;
msarett9e9444c2016-02-03 12:39:10 -0800418 case kUnpremul_SkAlphaType:
419 folder.append("_unpremul");
420 break;
scroggoc5560be2016-02-03 09:42:42 -0800421 default:
422 break;
423 }
424
msarett3d9d7a72015-10-21 10:27:10 -0700425 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800426 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700427 }
428
scroggof8dc9df2016-05-16 09:04:13 -0700429 AndroidCodecSrc* src = new AndroidCodecSrc(path, dstColorType, dstAlphaType, sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700430 push_src("image", folder, src);
431}
432
msarett18976312016-03-09 14:20:58 -0800433static void push_image_gen_src(Path path, ImageGenSrc::Mode mode, SkAlphaType alphaType, bool isGpu)
434{
435 SkString folder;
436 switch (mode) {
437 case ImageGenSrc::kCodec_Mode:
438 folder.append("gen_codec");
439 break;
440 case ImageGenSrc::kPlatform_Mode:
441 folder.append("gen_platform");
442 break;
443 }
444
445 if (isGpu) {
446 folder.append("_gpu");
447 } else {
448 switch (alphaType) {
449 case kOpaque_SkAlphaType:
450 folder.append("_opaque");
451 break;
452 case kPremul_SkAlphaType:
453 folder.append("_premul");
454 break;
455 case kUnpremul_SkAlphaType:
456 folder.append("_unpremul");
457 break;
458 default:
459 break;
460 }
461 }
462
463 ImageGenSrc* src = new ImageGenSrc(path, mode, alphaType, isGpu);
464 push_src("image", folder, src);
465}
466
msarett438b2ad2015-04-09 12:43:10 -0700467static void push_codec_srcs(Path path) {
468 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
469 if (!encoded) {
mtkleinc41fd922016-03-08 09:01:39 -0800470 info("Couldn't read %s.", path.c_str());
msarett438b2ad2015-04-09 12:43:10 -0700471 return;
472 }
473 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
halcanary96fcdcc2015-08-27 07:41:13 -0700474 if (nullptr == codec.get()) {
mtkleinc41fd922016-03-08 09:01:39 -0800475 info("Couldn't create codec for %s.", path.c_str());
msarett438b2ad2015-04-09 12:43:10 -0700476 return;
477 }
478
scroggo9366aff2016-05-20 08:38:54 -0700479 // native scaling is only supported by WEBP and JPEG
480 bool supportsNativeScaling = false;
msarett438b2ad2015-04-09 12:43:10 -0700481
msarett91c22b22016-02-22 12:27:46 -0800482 SkTArray<CodecSrc::Mode> nativeModes;
483 nativeModes.push_back(CodecSrc::kCodec_Mode);
484 nativeModes.push_back(CodecSrc::kCodecZeroInit_Mode);
msarett91c22b22016-02-22 12:27:46 -0800485 switch (codec->getEncodedFormat()) {
486 case SkEncodedFormat::kJPEG_SkEncodedFormat:
487 nativeModes.push_back(CodecSrc::kScanline_Mode);
488 nativeModes.push_back(CodecSrc::kStripe_Mode);
489 nativeModes.push_back(CodecSrc::kCroppedScanline_Mode);
scroggo9366aff2016-05-20 08:38:54 -0700490 supportsNativeScaling = true;
msarett91c22b22016-02-22 12:27:46 -0800491 break;
492 case SkEncodedFormat::kWEBP_SkEncodedFormat:
493 nativeModes.push_back(CodecSrc::kSubset_Mode);
scroggo9366aff2016-05-20 08:38:54 -0700494 supportsNativeScaling = true;
msarett91c22b22016-02-22 12:27:46 -0800495 break;
yujieqin7a307df2016-03-11 07:32:33 -0800496 case SkEncodedFormat::kDNG_SkEncodedFormat:
msarettb65e6042016-02-23 05:37:25 -0800497 break;
msarett91c22b22016-02-22 12:27:46 -0800498 default:
499 nativeModes.push_back(CodecSrc::kScanline_Mode);
msarett91c22b22016-02-22 12:27:46 -0800500 break;
501 }
msarett9e707a02015-09-01 14:57:57 -0700502
msarett91c22b22016-02-22 12:27:46 -0800503 SkTArray<CodecSrc::DstColorType> colorTypes;
504 colorTypes.push_back(CodecSrc::kGetFromCanvas_DstColorType);
msarett34e0ec42016-04-22 16:27:24 -0700505 colorTypes.push_back(CodecSrc::kNonNative8888_Always_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700506 switch (codec->getInfo().colorType()) {
507 case kGray_8_SkColorType:
msarett91c22b22016-02-22 12:27:46 -0800508 colorTypes.push_back(CodecSrc::kGrayscale_Always_DstColorType);
msarett55f7bdd2016-02-16 13:24:54 -0800509 if (kWBMP_SkEncodedFormat == codec->getEncodedFormat()) {
msarett91c22b22016-02-22 12:27:46 -0800510 colorTypes.push_back(CodecSrc::kIndex8_Always_DstColorType);
msarett55f7bdd2016-02-16 13:24:54 -0800511 }
msarett36c37962015-09-02 13:20:52 -0700512 break;
513 case kIndex_8_SkColorType:
msarett91c22b22016-02-22 12:27:46 -0800514 colorTypes.push_back(CodecSrc::kIndex8_Always_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700515 break;
516 default:
msarett36c37962015-09-02 13:20:52 -0700517 break;
518 }
msarett9e707a02015-09-01 14:57:57 -0700519
scroggoc5560be2016-02-03 09:42:42 -0800520 SkTArray<SkAlphaType> alphaModes;
521 alphaModes.push_back(kPremul_SkAlphaType);
scroggof2c96a22016-05-20 11:08:27 -0700522 if (codec->getInfo().alphaType() != kOpaque_SkAlphaType) {
523 alphaModes.push_back(kUnpremul_SkAlphaType);
scroggoc5560be2016-02-03 09:42:42 -0800524 }
msarettb714fb02016-01-22 14:46:42 -0800525
526 for (CodecSrc::Mode mode : nativeModes) {
scroggo9366aff2016-05-20 08:38:54 -0700527 for (CodecSrc::DstColorType colorType : colorTypes) {
528 for (SkAlphaType alphaType : alphaModes) {
scroggof2c96a22016-05-20 11:08:27 -0700529 // Only test kCroppedScanline_Mode when the alpha type is premul. The test is
scroggo9366aff2016-05-20 08:38:54 -0700530 // slow and won't be interestingly different with different alpha types.
531 if (CodecSrc::kCroppedScanline_Mode == mode &&
scroggof2c96a22016-05-20 11:08:27 -0700532 kPremul_SkAlphaType != alphaType) {
scroggo9366aff2016-05-20 08:38:54 -0700533 continue;
534 }
msarett91c22b22016-02-22 12:27:46 -0800535
scroggo9366aff2016-05-20 08:38:54 -0700536 push_codec_src(path, mode, colorType, alphaType, 1.0f);
msarettf3dc1882016-04-26 13:06:38 -0700537
scroggo9366aff2016-05-20 08:38:54 -0700538 // Skip kNonNative on different native scales. It won't be interestingly
539 // different.
540 if (supportsNativeScaling &&
541 CodecSrc::kNonNative8888_Always_DstColorType == colorType) {
542 // Native Scales
543 // SkJpegCodec natively supports scaling to the following:
544 for (auto scale : { 0.125f, 0.25f, 0.375f, 0.5f, 0.625f, 0.750f, 0.875f }) {
545 push_codec_src(path, mode, colorType, alphaType, scale);
546 }
scroggoc5560be2016-02-03 09:42:42 -0800547 }
msarett9e707a02015-09-01 14:57:57 -0700548 }
549 }
msarett0a242972015-06-11 14:27:27 -0700550 }
msarett36c37962015-09-02 13:20:52 -0700551
scroggoe4499842016-02-25 11:03:47 -0800552 if (FLAGS_simpleCodec) {
553 return;
554 }
555
msarett3d9d7a72015-10-21 10:27:10 -0700556 const int sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
msarett36c37962015-09-02 13:20:52 -0700557
msarett3d9d7a72015-10-21 10:27:10 -0700558 for (int sampleSize : sampleSizes) {
msarett91c22b22016-02-22 12:27:46 -0800559 for (CodecSrc::DstColorType colorType : colorTypes) {
scroggoc5560be2016-02-03 09:42:42 -0800560 for (SkAlphaType alphaType : alphaModes) {
msarettf3dc1882016-04-26 13:06:38 -0700561 // We can exercise all of the kNonNative support code in the swizzler with just a
562 // few sample sizes. Skip the rest.
563 if (CodecSrc::kNonNative8888_Always_DstColorType == colorType && sampleSize > 3) {
564 continue;
565 }
566
scroggof8dc9df2016-05-16 09:04:13 -0700567 push_android_codec_src(path, colorType, alphaType, sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700568 }
msarett36c37962015-09-02 13:20:52 -0700569 }
570 }
msarett18976312016-03-09 14:20:58 -0800571
572 static const char* const rawExts[] = {
573 "arw", "cr2", "dng", "nef", "nrw", "orf", "raf", "rw2", "pef", "srw",
574 "ARW", "CR2", "DNG", "NEF", "NRW", "ORF", "RAF", "RW2", "PEF", "SRW",
575 };
576
577 // There is not currently a reason to test RAW images on image generator.
578 // If we want to enable these tests, we will need to fix skbug.com/5079.
579 for (const char* ext : rawExts) {
580 if (path.endsWith(ext)) {
581 return;
582 }
583 }
584
585 // Push image generator GPU test.
brianosmanc9ced392016-03-24 07:27:43 -0700586 push_image_gen_src(path, ImageGenSrc::kCodec_Mode, codec->getInfo().alphaType(), true);
msarett18976312016-03-09 14:20:58 -0800587
588 // Push image generator CPU tests.
589 for (SkAlphaType alphaType : alphaModes) {
590 push_image_gen_src(path, ImageGenSrc::kCodec_Mode, alphaType, false);
591
592#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
593 if (kWEBP_SkEncodedFormat != codec->getEncodedFormat() &&
594 kWBMP_SkEncodedFormat != codec->getEncodedFormat() &&
595 kUnpremul_SkAlphaType != alphaType)
596 {
597 push_image_gen_src(path, ImageGenSrc::kPlatform_Mode, alphaType, false);
598 }
msarettfc0b6d12016-03-17 13:50:17 -0700599#elif defined(SK_BUILD_FOR_WIN)
600 if (kWEBP_SkEncodedFormat != codec->getEncodedFormat() &&
601 kWBMP_SkEncodedFormat != codec->getEncodedFormat())
602 {
603 push_image_gen_src(path, ImageGenSrc::kPlatform_Mode, alphaType, false);
604 }
msarett18976312016-03-09 14:20:58 -0800605#endif
606 }
msarett438b2ad2015-04-09 12:43:10 -0700607}
608
msarettd1227a72016-05-18 06:23:57 -0700609static void push_brd_src(Path path, CodecSrc::DstColorType dstColorType, BRDSrc::Mode mode,
610 uint32_t sampleSize) {
611 SkString folder("brd_android_codec");
msaretta5783ae2015-09-08 15:35:32 -0700612 switch (mode) {
613 case BRDSrc::kFullImage_Mode:
614 break;
615 case BRDSrc::kDivisor_Mode:
616 folder.append("_divisor");
617 break;
618 default:
619 SkASSERT(false);
620 return;
621 }
622
623 switch (dstColorType) {
624 case CodecSrc::kGetFromCanvas_DstColorType:
625 break;
626 case CodecSrc::kIndex8_Always_DstColorType:
627 folder.append("_kIndex");
628 break;
629 case CodecSrc::kGrayscale_Always_DstColorType:
630 folder.append("_kGray");
631 break;
632 default:
633 SkASSERT(false);
634 return;
635 }
636
637 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800638 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700639 }
640
msarettd1227a72016-05-18 06:23:57 -0700641 BRDSrc* src = new BRDSrc(path, mode, dstColorType, sampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700642 push_src("image", folder, src);
643}
644
645static void push_brd_srcs(Path path) {
scroggo24a9bd72016-05-20 06:02:42 -0700646 // Only run Index8 and grayscale to one sampleSize and Mode. Though interesting
647 // to test these color types, they should not reveal anything across various
648 // sampleSizes and Modes
649 for (auto type : { CodecSrc::kIndex8_Always_DstColorType,
650 CodecSrc::kGrayscale_Always_DstColorType }) {
651 // Arbitrarily choose Mode and sampleSize.
652 push_brd_src(path, type, BRDSrc::kFullImage_Mode, 2);
653 }
654
655
scroggo501b7342015-11-03 07:55:11 -0800656 // Test on a variety of sampleSizes, making sure to include:
657 // - 2, 4, and 8, which are natively supported by jpeg
658 // - multiples of 2 which are not divisible by 4 (analogous for 4)
659 // - larger powers of two, since BRD clients generally use powers of 2
660 // We will only produce output for the larger sizes on large images.
661 const uint32_t sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 24, 32, 64 };
msarett6efbe052015-09-11 09:01:16 -0700662
msaretta5783ae2015-09-08 15:35:32 -0700663 const BRDSrc::Mode modes[] = {
msarett26ad17b2015-10-22 07:29:19 -0700664 BRDSrc::kFullImage_Mode,
665 BRDSrc::kDivisor_Mode,
msaretta5783ae2015-09-08 15:35:32 -0700666 };
667
msarettd1227a72016-05-18 06:23:57 -0700668 for (uint32_t sampleSize : sampleSizes) {
scroggo24a9bd72016-05-20 06:02:42 -0700669 for (BRDSrc::Mode mode : modes) {
670 push_brd_src(path, CodecSrc::kGetFromCanvas_DstColorType, mode, sampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700671 }
672 }
673}
674
675static bool brd_supported(const char* ext) {
msarett8c8f22a2015-04-01 06:58:48 -0700676 static const char* const exts[] = {
msaretta5783ae2015-09-08 15:35:32 -0700677 "jpg", "jpeg", "png", "webp",
678 "JPG", "JPEG", "PNG", "WEBP",
msarett8c8f22a2015-04-01 06:58:48 -0700679 };
680
681 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
682 if (0 == strcmp(exts[i], ext)) {
683 return true;
684 }
685 }
686 return false;
scroggo9b77ddd2015-03-19 06:03:39 -0700687}
688
scroggo86737142016-02-03 12:19:11 -0800689static bool gather_srcs() {
mtklein748ca3b2015-01-15 10:56:12 -0800690 for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) {
djsollen54416de2015-04-03 07:24:48 -0700691 push_src("gm", "", new GMSrc(r->factory()));
mtklein748ca3b2015-01-15 10:56:12 -0800692 }
halcanaryfc37ad12015-01-30 07:31:19 -0800693 for (int i = 0; i < FLAGS_skps.count(); i++) {
694 const char* path = FLAGS_skps[i];
695 if (sk_isdir(path)) {
696 SkOSFile::Iter it(path, "skp");
697 for (SkString file; it.next(&file); ) {
djsollen54416de2015-04-03 07:24:48 -0700698 push_src("skp", "", new SKPSrc(SkOSPath::Join(path, file.c_str())));
halcanaryfc37ad12015-01-30 07:31:19 -0800699 }
700 } else {
djsollen54416de2015-04-03 07:24:48 -0700701 push_src("skp", "", new SKPSrc(path));
mtklein114c3cd2015-01-15 10:15:02 -0800702 }
703 }
scroggo86737142016-02-03 12:19:11 -0800704
705 SkTArray<SkString> images;
msarett69deca82016-04-29 09:38:40 -0700706 if (!CollectImages(FLAGS_images, &images)) {
scroggo86737142016-02-03 12:19:11 -0800707 return false;
708 }
709
710 for (auto image : images) {
711 push_codec_srcs(image);
scroggoe4499842016-02-25 11:03:47 -0800712 if (FLAGS_simpleCodec) {
713 continue;
714 }
715
mtklein21eaf3b2016-02-08 12:39:59 -0800716 const char* ext = strrchr(image.c_str(), '.');
717 if (ext && brd_supported(ext+1)) {
scroggo86737142016-02-03 12:19:11 -0800718 push_brd_srcs(image);
mtklein709d2c32015-01-15 08:30:25 -0800719 }
mtklein709d2c32015-01-15 08:30:25 -0800720 }
scroggo86737142016-02-03 12:19:11 -0800721
msarett69deca82016-04-29 09:38:40 -0700722 SkTArray<SkString> colorImages;
723 if (!CollectImages(FLAGS_colorImages, &colorImages)) {
724 return false;
725 }
726
727 for (auto colorImage : colorImages) {
msarett9876ac52016-06-01 14:47:18 -0700728 ColorCodecSrc* src = new ColorCodecSrc(colorImage, ColorCodecSrc::kBaseline_Mode);
msarett69deca82016-04-29 09:38:40 -0700729 push_src("image", "color_codec_baseline", src);
msarett888dc162016-05-23 10:21:17 -0700730
msarett9876ac52016-06-01 14:47:18 -0700731 src = new ColorCodecSrc(colorImage, ColorCodecSrc::kDst_HPZR30w_Mode);
msarett888dc162016-05-23 10:21:17 -0700732 push_src("image", "color_codec_HPZR30w", src);
msarett9876ac52016-06-01 14:47:18 -0700733
734#if !defined(GOOGLE3)
735 src = new ColorCodecSrc(colorImage, ColorCodecSrc::kQCMS_HPZR30w_Mode);
736 push_src("image", "color_codec_QCMS_HPZR30w", src);
737#endif
msarett69deca82016-04-29 09:38:40 -0700738 }
739
scroggo86737142016-02-03 12:19:11 -0800740 return true;
mtklein709d2c32015-01-15 08:30:25 -0800741}
742
kkinnunen3e980c32015-12-23 01:33:00 -0800743static void push_sink(const SkCommandLineConfig& config, Sink* s) {
rmistry0f515bd2015-12-22 10:22:26 -0800744 SkAutoTDelete<Sink> sink(s);
kkinnunen3e980c32015-12-23 01:33:00 -0800745
mtkleine0effd62015-07-29 06:37:28 -0700746 // Try a simple Src as a canary. If it fails, skip this sink.
mtklein748ca3b2015-01-15 10:56:12 -0800747 struct : public Src {
mtkleine0effd62015-07-29 06:37:28 -0700748 Error draw(SkCanvas* c) const override {
749 c->drawRect(SkRect::MakeWH(1,1), SkPaint());
750 return "";
751 }
mtklein36352bf2015-03-25 18:17:31 -0700752 SkISize size() const override { return SkISize::Make(16, 16); }
mtkleine0effd62015-07-29 06:37:28 -0700753 Name name() const override { return "justOneRect"; }
754 } justOneRect;
mtklein114c3cd2015-01-15 10:15:02 -0800755
mtklein748ca3b2015-01-15 10:56:12 -0800756 SkBitmap bitmap;
757 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800758 SkString log;
mtkleine0effd62015-07-29 06:37:28 -0700759 Error err = sink->draw(justOneRect, &bitmap, &stream, &log);
mtklein4089ef72015-03-05 08:40:28 -0800760 if (err.isFatal()) {
mtkleinc41fd922016-03-08 09:01:39 -0800761 info("Could not run %s: %s\n", config.getTag().c_str(), err.c_str());
mtklein05641a52015-04-21 10:49:13 -0700762 exit(1);
mtklein114c3cd2015-01-15 10:15:02 -0800763 }
764
mtkleine0effd62015-07-29 06:37:28 -0700765 TaggedSink& ts = gSinks.push_back();
mtklein18300a32016-03-16 13:53:35 -0700766 ts.reset(sink.release());
kkinnunen3e980c32015-12-23 01:33:00 -0800767 ts.tag = config.getTag();
mtklein748ca3b2015-01-15 10:56:12 -0800768}
769
770static bool gpu_supported() {
771#if SK_SUPPORT_GPU
772 return FLAGS_gpu;
773#else
774 return false;
775#endif
776}
kkinnunen9ebc3f02015-12-21 23:48:13 -0800777
kkinnunen3e980c32015-12-23 01:33:00 -0800778static Sink* create_sink(const SkCommandLineConfig* config) {
779#if SK_SUPPORT_GPU
780 if (gpu_supported()) {
781 if (const SkCommandLineConfigGpu* gpuConfig = config->asConfigGpu()) {
bsalomon85b4b532016-04-05 11:06:27 -0700782 GrContextFactory::ContextType contextType = gpuConfig->getContextType();
783 GrContextFactory::ContextOptions contextOptions =
784 GrContextFactory::kNone_ContextOptions;
kkinnunen3e980c32015-12-23 01:33:00 -0800785 if (gpuConfig->getUseNVPR()) {
bsalomon85b4b532016-04-05 11:06:27 -0700786 contextOptions = static_cast<GrContextFactory::ContextOptions>(
787 contextOptions | GrContextFactory::kEnableNVPR_ContextOptions);
kkinnunen3e980c32015-12-23 01:33:00 -0800788 }
brianosman898235c2016-04-06 07:38:23 -0700789 if (SkColorAndProfileAreGammaCorrect(gpuConfig->getColorType(),
790 gpuConfig->getProfileType())) {
bsalomon85b4b532016-04-05 11:06:27 -0700791 contextOptions = static_cast<GrContextFactory::ContextOptions>(
792 contextOptions | GrContextFactory::kRequireSRGBSupport_ContextOptions);
brianosman61d3b082016-03-30 11:19:36 -0700793 }
kkinnunen3e980c32015-12-23 01:33:00 -0800794 GrContextFactory testFactory;
795 if (!testFactory.get(contextType, contextOptions)) {
mtkleinc41fd922016-03-08 09:01:39 -0800796 info("WARNING: can not create GPU context for config '%s'. "
797 "GM tests will be skipped.\n", gpuConfig->getTag().c_str());
kkinnunen3e980c32015-12-23 01:33:00 -0800798 return nullptr;
799 }
800 return new GPUSink(contextType, contextOptions, gpuConfig->getSamples(),
brianosmand93c1202016-03-10 07:49:08 -0800801 gpuConfig->getUseDIText(), gpuConfig->getColorType(),
802 gpuConfig->getProfileType(), FLAGS_gpu_threading);
kkinnunen3e980c32015-12-23 01:33:00 -0800803 }
804 }
805#endif
806
807#define SINK(t, sink, ...) if (config->getBackend().equals(t)) { return new sink(__VA_ARGS__); }
808
tomhudsoneebc39a2015-02-23 12:18:05 -0800809#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
810 SINK("hwui", HWUISink);
811#endif
812
mtklein748ca3b2015-01-15 10:56:12 -0800813 if (FLAGS_cpu) {
814 SINK("565", RasterSink, kRGB_565_SkColorType);
815 SINK("8888", RasterSink, kN32_SkColorType);
mtklein27c3fdd2016-02-26 14:43:21 -0800816 SINK("srgb", RasterSink, kN32_SkColorType, kSRGB_SkColorProfileType);
817 SINK("f16", RasterSink, kRGBA_F16_SkColorType);
halcanary94b87bd2016-04-04 06:12:15 -0700818 SINK("pdf", PDFSink);
mtklein9c3f17d2015-01-28 11:35:18 -0800819 SINK("skp", SKPSink);
mtklein8a4527e2015-01-31 20:00:58 -0800820 SINK("svg", SVGSink);
821 SINK("null", NullSink);
halcanary47ef4d52015-03-03 09:13:09 -0800822 SINK("xps", XPSSink);
halcanary4b656662016-04-27 07:45:18 -0700823 SINK("pdfa", PDFSink, true);
mtklein748ca3b2015-01-15 10:56:12 -0800824 }
825#undef SINK
halcanary96fcdcc2015-08-27 07:41:13 -0700826 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800827}
828
kkinnunen3e980c32015-12-23 01:33:00 -0800829static Sink* create_via(const SkString& tag, Sink* wrapped) {
830#define VIA(t, via, ...) if (tag.equals(t)) { return new via(__VA_ARGS__); }
halcanary96fcdcc2015-08-27 07:41:13 -0700831 VIA("twice", ViaTwice, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700832 VIA("serialize", ViaSerialization, wrapped);
mtklein4a34ecb2016-01-08 10:19:35 -0800833 VIA("pic", ViaPicture, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700834 VIA("2ndpic", ViaSecondPicture, wrapped);
mtkleind31c13d2015-05-05 12:59:56 -0700835 VIA("sp", ViaSingletonPictures, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700836 VIA("tiles", ViaTiles, 256, 256, nullptr, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800837 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800838
mtkleind603b222015-02-17 11:13:33 -0800839 if (FLAGS_matrix.count() == 4) {
mtklein748ca3b2015-01-15 10:56:12 -0800840 SkMatrix m;
mtkleind603b222015-02-17 11:13:33 -0800841 m.reset();
842 m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
843 m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
844 m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
845 m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
846 VIA("matrix", ViaMatrix, m, wrapped);
847 VIA("upright", ViaUpright, m, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800848 }
tomhudson64de1e12015-03-05 08:01:07 -0800849
850#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
851 VIA("androidsdk", ViaAndroidSDK, wrapped);
852#endif
853
mtklein748ca3b2015-01-15 10:56:12 -0800854#undef VIA
halcanary96fcdcc2015-08-27 07:41:13 -0700855 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800856}
857
brianosman05c987c2016-05-09 13:42:16 -0700858static bool gather_sinks() {
kkinnunen3e980c32015-12-23 01:33:00 -0800859 SkCommandLineConfigArray configs;
860 ParseConfigs(FLAGS_config, &configs);
861 for (int i = 0; i < configs.count(); i++) {
862 const SkCommandLineConfig& config = *configs[i];
863 Sink* sink = create_sink(&config);
864 if (sink == nullptr) {
mtkleinc41fd922016-03-08 09:01:39 -0800865 info("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
866 config.getTag().c_str());
kkinnunen3e980c32015-12-23 01:33:00 -0800867 continue;
868 }
mtklein748ca3b2015-01-15 10:56:12 -0800869
kkinnunen3e980c32015-12-23 01:33:00 -0800870 const SkTArray<SkString>& parts = config.getViaParts();
871 for (int j = parts.count(); j-- > 0;) {
872 const SkString& part = parts[j];
873 Sink* next = create_via(part, sink);
halcanary96fcdcc2015-08-27 07:41:13 -0700874 if (next == nullptr) {
mtkleinc41fd922016-03-08 09:01:39 -0800875 info("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
876 part.c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800877 delete sink;
halcanary96fcdcc2015-08-27 07:41:13 -0700878 sink = nullptr;
mtklein748ca3b2015-01-15 10:56:12 -0800879 break;
880 }
881 sink = next;
882 }
883 if (sink) {
884 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800885 }
886 }
brianosman05c987c2016-05-09 13:42:16 -0700887
888 // If no configs were requested (just running tests, perhaps?), then we're okay.
889 // Otherwise, make sure that at least one sink was constructed correctly. This catches
890 // the case of bots without a GPU being assigned GPU configs.
891 return (configs.count() == 0) || (gSinks.count() > 0);
mtklein114c3cd2015-01-15 10:15:02 -0800892}
mtklein709d2c32015-01-15 08:30:25 -0800893
mtkleina5114d72015-08-24 13:27:01 -0700894static bool dump_png(SkBitmap bitmap, const char* path, const char* md5) {
895 const int w = bitmap.width(),
896 h = bitmap.height();
897
brianosman3c579dc2016-04-19 09:18:11 -0700898 sk_sp<SkData> encodedBitmap = sk_tools::encode_bitmap_for_png(bitmap);
899 if (encodedBitmap.get() == nullptr) {
900 return false;
mtkleina5114d72015-08-24 13:27:01 -0700901 }
brianosman3c579dc2016-04-19 09:18:11 -0700902 uint32_t* rgba = static_cast<uint32_t*>(encodedBitmap.get()->writable_data());
mtkleina5114d72015-08-24 13:27:01 -0700903
904 // We don't need bitmap anymore. Might as well drop our ref.
mtkleinfccb77d2015-08-24 14:13:29 -0700905 bitmap.reset();
mtkleina5114d72015-08-24 13:27:01 -0700906
mtkleinc64137c2015-08-25 10:56:08 -0700907 FILE* f = fopen(path, "wb");
mtkleina5114d72015-08-24 13:27:01 -0700908 if (!f) { return false; }
909
910 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
911 if (!png) {
912 fclose(f);
913 return false;
914 }
915
916 png_infop info = png_create_info_struct(png);
917 if (!info) {
918 png_destroy_write_struct(&png, &info);
919 fclose(f);
920 return false;
921 }
922
mtkleind69ece62015-09-10 10:37:44 -0700923 SkString description;
924 description.append("Key: ");
925 for (int i = 0; i < FLAGS_key.count(); i++) {
926 description.appendf("%s ", FLAGS_key[i]);
927 }
928 description.append("Properties: ");
929 for (int i = 0; i < FLAGS_properties.count(); i++) {
930 description.appendf("%s ", FLAGS_properties[i]);
931 }
932 description.appendf("MD5: %s", md5);
933
mtkleina5114d72015-08-24 13:27:01 -0700934 png_text text[2];
935 text[0].key = (png_charp)"Author";
936 text[0].text = (png_charp)"DM dump_png()";
937 text[0].compression = PNG_TEXT_COMPRESSION_NONE;
938 text[1].key = (png_charp)"Description";
mtkleind69ece62015-09-10 10:37:44 -0700939 text[1].text = (png_charp)description.c_str();
mtkleina5114d72015-08-24 13:27:01 -0700940 text[1].compression = PNG_TEXT_COMPRESSION_NONE;
941 png_set_text(png, info, text, 2);
942
943 png_init_io(png, f);
944 png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8,
945 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
946 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
947 png_write_info(png, info);
948 for (int j = 0; j < h; j++) {
brianosman3c579dc2016-04-19 09:18:11 -0700949 png_bytep row = (png_bytep)(rgba + w*j);
mtkleina5114d72015-08-24 13:27:01 -0700950 png_write_rows(png, &row, 1);
951 }
952 png_write_end(png, info);
953
954 png_destroy_write_struct(&png, &info);
955 fclose(f);
956 return true;
957}
958
mtkleina2ef6422015-01-15 13:44:22 -0800959static bool match(const char* needle, const char* haystack) {
halcanary96fcdcc2015-08-27 07:41:13 -0700960 return 0 == strcmp("_", needle) || nullptr != strstr(haystack, needle);
mtkleina2ef6422015-01-15 13:44:22 -0800961}
962
mtklein3eed7dd2016-02-24 19:07:07 -0800963static bool is_blacklisted(const char* sink, const char* src,
964 const char* srcOptions, const char* name) {
mtklein0d243ff2015-04-03 07:51:00 -0700965 for (int i = 0; i < FLAGS_blacklist.count() - 3; i += 4) {
mtkleina2ef6422015-01-15 13:44:22 -0800966 if (match(FLAGS_blacklist[i+0], sink) &&
djsollen54416de2015-04-03 07:24:48 -0700967 match(FLAGS_blacklist[i+1], src) &&
968 match(FLAGS_blacklist[i+2], srcOptions) &&
969 match(FLAGS_blacklist[i+3], name)) {
mtklein3eed7dd2016-02-24 19:07:07 -0800970 return true;
mtkleina2ef6422015-01-15 13:44:22 -0800971 }
972 }
mtklein3eed7dd2016-02-24 19:07:07 -0800973 return false;
mtkleina2ef6422015-01-15 13:44:22 -0800974}
975
mtkleincd50bca2016-01-05 06:20:20 -0800976// Even when a Task Sink reports to be non-threadsafe (e.g. GPU), we know things like
977// .png encoding are definitely thread safe. This lets us offload that work to CPU threads.
978static SkTaskGroup gDefinitelyThreadSafeWork;
979
mtklein748ca3b2015-01-15 10:56:12 -0800980// The finest-grained unit of work we can run: draw a single Src into a single Sink,
981// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
982struct Task {
mtkleine0effd62015-07-29 06:37:28 -0700983 Task(const TaggedSrc& src, const TaggedSink& sink) : src(src), sink(sink) {}
984 const TaggedSrc& src;
985 const TaggedSink& sink;
mtklein748ca3b2015-01-15 10:56:12 -0800986
mtklein21eaf3b2016-02-08 12:39:59 -0800987 static void Run(const Task& task) {
988 SkString name = task.src->name();
mtkleine0effd62015-07-29 06:37:28 -0700989
mtkleinb9eb4ac2015-02-02 18:26:03 -0800990 SkString log;
mtklein3eed7dd2016-02-24 19:07:07 -0800991 if (!FLAGS_dryRun) {
mtklein748ca3b2015-01-15 10:56:12 -0800992 SkBitmap bitmap;
993 SkDynamicMemoryWStream stream;
mtklein3eed7dd2016-02-24 19:07:07 -0800994 start(task.sink.tag.c_str(), task.src.tag.c_str(),
995 task.src.options.c_str(), name.c_str());
mtklein21eaf3b2016-02-08 12:39:59 -0800996 Error err = task.sink->draw(*task.src, &bitmap, &stream, &log);
mtkleinb3b13b72016-03-07 13:20:52 -0800997 if (!log.isEmpty()) {
mtkleinc41fd922016-03-08 09:01:39 -0800998 info("%s %s %s %s:\n%s\n", task.sink.tag.c_str()
999 , task.src.tag.c_str()
1000 , task.src.options.c_str()
1001 , name.c_str()
1002 , log.c_str());
mtkleinb3b13b72016-03-07 13:20:52 -08001003 }
mtklein748ca3b2015-01-15 10:56:12 -08001004 if (!err.isEmpty()) {
mtklein4089ef72015-03-05 08:40:28 -08001005 if (err.isFatal()) {
djsollen54416de2015-04-03 07:24:48 -07001006 fail(SkStringPrintf("%s %s %s %s: %s",
mtklein21eaf3b2016-02-08 12:39:59 -08001007 task.sink.tag.c_str(),
1008 task.src.tag.c_str(),
1009 task.src.options.c_str(),
mtklein4089ef72015-03-05 08:40:28 -08001010 name.c_str(),
1011 err.c_str()));
mtkleinb37cb412015-03-18 05:27:14 -07001012 } else {
mtklein3eed7dd2016-02-24 19:07:07 -08001013 done(task.sink.tag.c_str(), task.src.tag.c_str(),
1014 task.src.options.c_str(), name.c_str());
mtkleinba6ada72016-01-21 09:39:35 -08001015 return;
mtklein4089ef72015-03-05 08:40:28 -08001016 }
mtklein748ca3b2015-01-15 10:56:12 -08001017 }
mtklein62bd1a62015-01-27 14:46:26 -08001018
mtkleincd50bca2016-01-05 06:20:20 -08001019 // We're likely switching threads here, so we must capture by value, [=] or [foo,bar].
1020 SkStreamAsset* data = stream.detachAsStream();
1021 gDefinitelyThreadSafeWork.add([task,name,bitmap,data]{
1022 SkAutoTDelete<SkStreamAsset> ownedData(data);
1023
1024 // Why doesn't the copy constructor do this when we have pre-locked pixels?
1025 bitmap.lockPixels();
1026
1027 SkString md5;
1028 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
1029 SkMD5 hash;
1030 if (data->getLength()) {
1031 hash.writeStream(data, data->getLength());
1032 data->rewind();
mtklein38408462015-07-08 07:25:27 -07001033 } else {
mtkleincd50bca2016-01-05 06:20:20 -08001034 // If we're BGRA (Linux, Windows), swizzle over to RGBA (Mac, Android).
1035 // This helps eliminate multiple 0-pixel-diff hashes on gold.skia.org.
1036 // (Android's general slow speed breaks the tie arbitrarily in RGBA's favor.)
1037 // We might consider promoting 565 to RGBA too.
1038 if (bitmap.colorType() == kBGRA_8888_SkColorType) {
1039 SkBitmap swizzle;
1040 SkAssertResult(bitmap.copyTo(&swizzle, kRGBA_8888_SkColorType));
1041 hash.write(swizzle.getPixels(), swizzle.getSize());
1042 } else {
1043 hash.write(bitmap.getPixels(), bitmap.getSize());
1044 }
1045 }
1046 SkMD5::Digest digest;
1047 hash.finish(digest);
1048 for (int i = 0; i < 16; i++) {
1049 md5.appendf("%02x", digest.data[i]);
mtklein38408462015-07-08 07:25:27 -07001050 }
mtklein62bd1a62015-01-27 14:46:26 -08001051 }
mtklein62bd1a62015-01-27 14:46:26 -08001052
mtkleincd50bca2016-01-05 06:20:20 -08001053 if (!FLAGS_readPath.isEmpty() &&
mtklein3eed7dd2016-02-24 19:07:07 -08001054 !gGold.contains(Gold(task.sink.tag, task.src.tag,
1055 task.src.options, name, md5))) {
mtkleincd50bca2016-01-05 06:20:20 -08001056 fail(SkStringPrintf("%s not found for %s %s %s %s in %s",
1057 md5.c_str(),
mtklein21eaf3b2016-02-08 12:39:59 -08001058 task.sink.tag.c_str(),
1059 task.src.tag.c_str(),
1060 task.src.options.c_str(),
mtkleincd50bca2016-01-05 06:20:20 -08001061 name.c_str(),
1062 FLAGS_readPath[0]));
mtklein748ca3b2015-01-15 10:56:12 -08001063 }
mtkleincd50bca2016-01-05 06:20:20 -08001064
1065 if (!FLAGS_writePath.isEmpty()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001066 const char* ext = task.sink->fileExtension();
mtkleincd50bca2016-01-05 06:20:20 -08001067 if (data->getLength()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001068 WriteToDisk(task, md5, ext, data, data->getLength(), nullptr);
mtkleincd50bca2016-01-05 06:20:20 -08001069 SkASSERT(bitmap.drawsNothing());
1070 } else if (!bitmap.drawsNothing()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001071 WriteToDisk(task, md5, ext, nullptr, 0, &bitmap);
mtkleincd50bca2016-01-05 06:20:20 -08001072 }
1073 }
1074 });
mtklein748ca3b2015-01-15 10:56:12 -08001075 }
mtklein3eed7dd2016-02-24 19:07:07 -08001076 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 -08001077 }
1078
1079 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -08001080 SkString md5,
1081 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -08001082 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -08001083 const SkBitmap* bitmap) {
mtklein409d4702016-02-29 07:38:01 -08001084 bool gammaCorrect = false;
1085 if (bitmap) {
brianosman898235c2016-04-06 07:38:23 -07001086 gammaCorrect = SkImageInfoIsGammaCorrect(bitmap->info());
mtklein409d4702016-02-29 07:38:01 -08001087 }
1088
mtklein748ca3b2015-01-15 10:56:12 -08001089 JsonWriter::BitmapResult result;
djsollen54416de2015-04-03 07:24:48 -07001090 result.name = task.src->name();
mtklein3eed7dd2016-02-24 19:07:07 -08001091 result.config = task.sink.tag;
djsollen54416de2015-04-03 07:24:48 -07001092 result.sourceType = task.src.tag;
1093 result.sourceOptions = task.src.options;
1094 result.ext = ext;
mtklein409d4702016-02-29 07:38:01 -08001095 result.gammaCorrect = gammaCorrect;
djsollen54416de2015-04-03 07:24:48 -07001096 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -08001097 JsonWriter::AddBitmapResult(result);
1098
mtkleinb0531a72015-04-07 13:38:48 -07001099 // If an MD5 is uninteresting, we want it noted in the JSON file,
1100 // but don't want to dump it out as a .png (or whatever ext is).
1101 if (gUninterestingHashes.contains(md5)) {
1102 return;
1103 }
1104
mtklein748ca3b2015-01-15 10:56:12 -08001105 const char* dir = FLAGS_writePath[0];
1106 if (0 == strcmp(dir, "@")) { // Needed for iOS.
1107 dir = FLAGS_resourcePath[0];
1108 }
1109 sk_mkdir(dir);
1110
1111 SkString path;
1112 if (FLAGS_nameByHash) {
1113 path = SkOSPath::Join(dir, result.md5.c_str());
1114 path.append(".");
1115 path.append(ext);
1116 if (sk_exists(path.c_str())) {
1117 return; // Content-addressed. If it exists already, we're done.
1118 }
1119 } else {
kkinnunen3e980c32015-12-23 01:33:00 -08001120 path = SkOSPath::Join(dir, task.sink.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001121 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001122 path = SkOSPath::Join(path.c_str(), task.src.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001123 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001124 if (strcmp(task.src.options.c_str(), "") != 0) {
1125 path = SkOSPath::Join(path.c_str(), task.src.options.c_str());
djsollen54416de2015-04-03 07:24:48 -07001126 sk_mkdir(path.c_str());
1127 }
mtklein748ca3b2015-01-15 10:56:12 -08001128 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
1129 path.append(".");
1130 path.append(ext);
1131 }
1132
mtklein748ca3b2015-01-15 10:56:12 -08001133 if (bitmap) {
mtkleina5114d72015-08-24 13:27:01 -07001134 if (!dump_png(*bitmap, path.c_str(), result.md5.c_str())) {
mtklein748ca3b2015-01-15 10:56:12 -08001135 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
1136 return;
1137 }
1138 } else {
mtkleina5114d72015-08-24 13:27:01 -07001139 SkFILEWStream file(path.c_str());
1140 if (!file.isValid()) {
1141 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
1142 return;
1143 }
mtklein748ca3b2015-01-15 10:56:12 -08001144 if (!file.writeStream(data, len)) {
1145 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
1146 return;
1147 }
1148 }
1149 }
1150};
1151
mtklein748ca3b2015-01-15 10:56:12 -08001152/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1153
1154// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
1155
mtklein21eaf3b2016-02-08 12:39:59 -08001156static SkTDArray<skiatest::Test> gParallelTests, gSerialTests;
mtklein748ca3b2015-01-15 10:56:12 -08001157
1158static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -08001159 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -08001160 return;
1161 }
mtklein6393c062015-04-27 08:45:01 -07001162 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r; r = r->next()) {
1163 if (!in_shard()) {
1164 continue;
1165 }
halcanary87f3ba42015-01-20 09:30:20 -08001166 // Despite its name, factory() is returning a reference to
1167 // link-time static const POD data.
1168 const skiatest::Test& test = r->factory();
1169 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -08001170 continue;
1171 }
halcanary87f3ba42015-01-20 09:30:20 -08001172 if (test.needsGpu && gpu_supported()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001173 (FLAGS_gpu_threading ? gParallelTests : gSerialTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -08001174 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein21eaf3b2016-02-08 12:39:59 -08001175 gParallelTests.push(test);
mtklein82d28432015-01-15 12:46:02 -08001176 }
mtklein748ca3b2015-01-15 10:56:12 -08001177 }
1178}
1179
mtklein21eaf3b2016-02-08 12:39:59 -08001180static void run_test(skiatest::Test test) {
halcanary87f3ba42015-01-20 09:30:20 -08001181 struct : public skiatest::Reporter {
mtklein36352bf2015-03-25 18:17:31 -07001182 void reportFailed(const skiatest::Failure& failure) override {
halcanary87f3ba42015-01-20 09:30:20 -08001183 fail(failure.toString());
1184 JsonWriter::AddTestFailure(failure);
1185 }
mtklein36352bf2015-03-25 18:17:31 -07001186 bool allowExtendedTest() const override {
halcanary87f3ba42015-01-20 09:30:20 -08001187 return FLAGS_pathOpsExtended;
1188 }
mtklein36352bf2015-03-25 18:17:31 -07001189 bool verbose() const override { return FLAGS_veryVerbose; }
halcanary87f3ba42015-01-20 09:30:20 -08001190 } reporter;
djsollen824996a2015-06-12 12:06:22 -07001191
mtklein3eed7dd2016-02-24 19:07:07 -08001192 if (!FLAGS_dryRun && !is_blacklisted("_", "tests", "_", test.name)) {
mtklein21eaf3b2016-02-08 12:39:59 -08001193 start("unit", "test", "", test.name);
mtklein55e88b22015-01-21 15:50:13 -08001194 GrContextFactory factory;
mtklein21eaf3b2016-02-08 12:39:59 -08001195 test.proc(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -08001196 }
mtklein3eed7dd2016-02-24 19:07:07 -08001197 done("unit", "test", "", test.name);
mtklein748ca3b2015-01-15 10:56:12 -08001198}
1199
1200/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1201
mtkleinafae30a2016-02-24 12:28:32 -08001202DEFINE_int32(status_sec, 15, "Print status this often (and if we crash).");
1203
1204SkThread* start_status_thread() {
1205 auto thread = new SkThread([] (void*) {
1206 for (;;) {
1207 print_status();
1208 #if defined(SK_BUILD_FOR_WIN)
1209 Sleep(FLAGS_status_sec * 1000);
1210 #else
1211 sleep(FLAGS_status_sec);
1212 #endif
mtklein2e1c47e2015-03-12 07:16:56 -07001213 }
mtkleinafae30a2016-02-24 12:28:32 -08001214 });
1215 thread->start();
1216 return thread;
mtkleinde6fc2b2015-03-12 06:28:54 -07001217}
1218
caryclark83ca6282015-06-10 09:31:09 -07001219#define PORTABLE_FONT_PREFIX "Toy Liberation "
1220
mbocee6a9912016-05-31 11:42:36 -07001221static sk_sp<SkTypeface> create_from_name(const char familyName[], SkFontStyle style) {
caryclark83ca6282015-06-10 09:31:09 -07001222 if (familyName && strlen(familyName) > sizeof(PORTABLE_FONT_PREFIX)
1223 && !strncmp(familyName, PORTABLE_FONT_PREFIX, sizeof(PORTABLE_FONT_PREFIX) - 1)) {
caryclark1818acb2015-07-24 12:09:25 -07001224 return sk_tool_utils::create_portable_typeface(familyName, style);
caryclark83ca6282015-06-10 09:31:09 -07001225 }
halcanary96fcdcc2015-08-27 07:41:13 -07001226 return nullptr;
caryclark83ca6282015-06-10 09:31:09 -07001227}
1228
1229#undef PORTABLE_FONT_PREFIX
1230
mbocee6a9912016-05-31 11:42:36 -07001231extern sk_sp<SkTypeface> (*gCreateTypefaceDelegate)(const char [], SkFontStyle );
caryclark83ca6282015-06-10 09:31:09 -07001232
herb3f996092016-05-27 08:15:05 -07001233extern bool gDefaultProfileIsSRGB;
1234
jcgregorio3b27ade2014-11-13 08:06:40 -08001235int dm_main();
caryclark17f0b6d2014-07-22 10:15:34 -07001236int dm_main() {
bsalomon71de3532016-04-26 14:27:21 -07001237 setbuf(stdout, nullptr);
mtklein246ba3a2016-02-23 10:39:36 -08001238 setup_crash_handler();
mtkleinafae30a2016-02-24 12:28:32 -08001239
mtkleina483da32016-03-17 12:53:36 -07001240 if (FLAGS_verbose) {
1241 gVLog = stderr;
1242 } else if (!FLAGS_writePath.isEmpty()) {
mtklein51c8cfc2016-03-11 12:59:09 -08001243 sk_mkdir(FLAGS_writePath[0]);
1244 gVLog = freopen(SkOSPath::Join(FLAGS_writePath[0], "verbose.log").c_str(), "w", stderr);
mtkleinc41fd922016-03-08 09:01:39 -08001245 }
1246
herb3f996092016-05-27 08:15:05 -07001247 if (FLAGS_forceSRGB) {
1248 gDefaultProfileIsSRGB = true;
1249 }
1250
mtklein5286f022016-01-22 08:18:14 -08001251 JsonWriter::DumpJson(); // It's handy for the bots to assume this is ~never missing.
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +00001252 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -07001253 SkTaskGroup::Enabler enabled(FLAGS_threads);
caryclark83ca6282015-06-10 09:31:09 -07001254 gCreateTypefaceDelegate = &create_from_name;
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +00001255
halcanary7d571242016-02-24 17:59:16 -08001256 {
1257 SkString testResourcePath = GetResourcePath("color_wheel.png");
1258 SkFILEStream testResource(testResourcePath.c_str());
1259 if (!testResource.isValid()) {
mtkleinc41fd922016-03-08 09:01:39 -08001260 info("Some resources are missing. Do you need to set --resourcePath?\n");
halcanary7d571242016-02-24 17:59:16 -08001261 }
1262 }
mtklein62bd1a62015-01-27 14:46:26 -08001263 gather_gold();
borenet09ed4802015-04-03 14:15:33 -07001264 gather_uninteresting_hashes();
mtklein62bd1a62015-01-27 14:46:26 -08001265
scroggo86737142016-02-03 12:19:11 -08001266 if (!gather_srcs()) {
1267 return 1;
1268 }
brianosman05c987c2016-05-09 13:42:16 -07001269 if (!gather_sinks()) {
1270 return 1;
1271 }
mtklein748ca3b2015-01-15 10:56:12 -08001272 gather_tests();
mtklein21eaf3b2016-02-08 12:39:59 -08001273 gPending = gSrcs.count() * gSinks.count() + gParallelTests.count() + gSerialTests.count();
mtkleinc41fd922016-03-08 09:01:39 -08001274 info("%d srcs * %d sinks + %d tests == %d tasks",
1275 gSrcs.count(), gSinks.count(), gParallelTests.count() + gSerialTests.count(), gPending);
mtkleinafae30a2016-02-24 12:28:32 -08001276 SkAutoTDelete<SkThread> statusThread(start_status_thread());
mtklein748ca3b2015-01-15 10:56:12 -08001277
mtklein21eaf3b2016-02-08 12:39:59 -08001278 // Kick off as much parallel work as we can, making note of any serial work we'll need to do.
1279 SkTaskGroup parallel;
1280 SkTArray<Task> serial;
1281
1282 for (auto& sink : gSinks)
1283 for (auto& src : gSrcs) {
mtklein3eed7dd2016-02-24 19:07:07 -08001284 if (src->veto(sink->flags()) ||
1285 is_blacklisted(sink.tag.c_str(), src.tag.c_str(),
1286 src.options.c_str(), src->name().c_str())) {
mtklein15923c92016-02-29 10:14:38 -08001287 SkAutoTAcquire<SkSpinlock> lock(gMutex);
mtklein3eed7dd2016-02-24 19:07:07 -08001288 gPending--;
1289 continue;
1290 }
1291
mtklein21eaf3b2016-02-08 12:39:59 -08001292 Task task(src, sink);
1293 if (src->serial() || sink->serial()) {
1294 serial.push_back(task);
1295 } else {
1296 parallel.add([task] { Task::Run(task); });
mtklein748ca3b2015-01-15 10:56:12 -08001297 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001298 }
mtklein21eaf3b2016-02-08 12:39:59 -08001299 for (auto test : gParallelTests) {
1300 parallel.add([test] { run_test(test); });
mtklein55e88b22015-01-21 15:50:13 -08001301 }
mtklein21eaf3b2016-02-08 12:39:59 -08001302
1303 // With the parallel work running, run serial tasks and tests here on main thread.
1304 for (auto task : serial) { Task::Run(task); }
1305 for (auto test : gSerialTests) { run_test(test); }
1306
1307 // Wait for any remaining parallel work to complete (including any spun off of serial tasks).
1308 parallel.wait();
mtkleincd50bca2016-01-05 06:20:20 -08001309 gDefinitelyThreadSafeWork.wait();
1310
mtkleinda884c42016-02-24 18:00:23 -08001311 // We'd better have run everything.
1312 SkASSERT(gPending == 0);
mtkleine027f172016-02-26 15:53:06 -08001313 // Make sure we've flushed all our results to disk.
1314 JsonWriter::DumpJson();
mtkleinda884c42016-02-24 18:00:23 -08001315
mtklein748ca3b2015-01-15 10:56:12 -08001316 // At this point we're back in single-threaded land.
caryclarkf53ce802015-06-15 06:48:30 -07001317 sk_tool_utils::release_portable_typefaces();
mtklein@google.comd36522d2013-10-16 13:02:15 +00001318
mtklein748ca3b2015-01-15 10:56:12 -08001319 if (gFailures.count() > 0) {
mtkleinc41fd922016-03-08 09:01:39 -08001320 info("Failures:\n");
mtklein748ca3b2015-01-15 10:56:12 -08001321 for (int i = 0; i < gFailures.count(); i++) {
mtkleinc41fd922016-03-08 09:01:39 -08001322 info("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001323 }
mtkleinc41fd922016-03-08 09:01:39 -08001324 info("%d failures\n", gFailures.count());
mtklein748ca3b2015-01-15 10:56:12 -08001325 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001326 }
mtkleinafae30a2016-02-24 12:28:32 -08001327
1328#ifdef SK_PDF_IMAGE_STATS
halcanary7a14b312015-10-01 07:28:13 -07001329 SkPDFImageDumpStats();
mtkleinafae30a2016-02-24 12:28:32 -08001330#endif // SK_PDF_IMAGE_STATS
1331
1332 print_status();
mtkleinc41fd922016-03-08 09:01:39 -08001333 info("Finished!\n");
mtklein748ca3b2015-01-15 10:56:12 -08001334 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +00001335}
jcgregorio3b27ade2014-11-13 08:06:40 -08001336
kkinnunen179a8f52015-11-20 13:32:24 -08001337// TODO: currently many GPU tests are declared outside SK_SUPPORT_GPU guards.
1338// Thus we export the empty RunWithGPUTestContexts when SK_SUPPORT_GPU=0.
1339namespace skiatest {
bsalomon758586c2016-04-06 14:02:39 -07001340
1341#if SK_SUPPORT_GPU
1342bool IsGLContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1343 return kOpenGL_GrBackend == GrContextFactory::ContextTypeBackend(type);
1344}
bsalomondc0fcd42016-04-11 14:21:33 -07001345bool IsVulkanContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1346 return kVulkan_GrBackend == GrContextFactory::ContextTypeBackend(type);
1347}
bsalomon758586c2016-04-06 14:02:39 -07001348bool IsRenderingGLContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1349 return IsGLContextType(type) && GrContextFactory::IsRenderingContext(type);
1350}
1351bool IsNullGLContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1352 return type == GrContextFactory::kNullGL_ContextType;
1353}
1354#else
1355bool IsGLContextType(int) { return false; }
bsalomondc0fcd42016-04-11 14:21:33 -07001356bool IsVulkanContextType(int) { return false; }
bsalomon758586c2016-04-06 14:02:39 -07001357bool IsRenderingGLContextType(int) { return false; }
1358bool IsNullGLContextType(int) { return false; }
1359#endif
1360
1361void RunWithGPUTestContexts(GrContextTestFn* test, GrContextTypeFilterFn* contextTypeFilter,
bsalomonf2f1c172016-04-05 12:59:06 -07001362 Reporter* reporter, GrContextFactory* factory) {
kkinnunen179a8f52015-11-20 13:32:24 -08001363#if SK_SUPPORT_GPU
kkinnunen3e980c32015-12-23 01:33:00 -08001364
bsalomon758586c2016-04-06 14:02:39 -07001365 for (int typeInt = 0; typeInt < GrContextFactory::kContextTypeCnt; ++typeInt) {
1366 GrContextFactory::ContextType contextType = (GrContextFactory::ContextType) typeInt;
1367 ContextInfo ctxInfo = factory->getContextInfo(contextType);
bsalomonfda88072016-04-11 14:40:50 -07001368 if (contextTypeFilter && !(*contextTypeFilter)(contextType)) {
kkinnunen179a8f52015-11-20 13:32:24 -08001369 continue;
1370 }
bsalomon758586c2016-04-06 14:02:39 -07001371 // Use "native" instead of explicitly trying OpenGL and OpenGL ES. Do not use GLES on,
1372 // desktop since tests do not account for not fixing http://skbug.com/2809
1373 if (contextType == GrContextFactory::kGL_ContextType ||
1374 contextType == GrContextFactory::kGLES_ContextType) {
1375 if (contextType != GrContextFactory::kNativeGL_ContextType) {
1376 continue;
1377 }
1378 }
bsalomon8b7451a2016-05-11 06:33:06 -07001379 if (ctxInfo.grContext()) {
bsalomonf2f1c172016-04-05 12:59:06 -07001380 (*test)(reporter, ctxInfo);
kkinnunen5219fd92015-12-10 06:28:13 -08001381 }
bsalomonf2f1c172016-04-05 12:59:06 -07001382 ctxInfo = factory->getContextInfo(contextType,
bsalomon85b4b532016-04-05 11:06:27 -07001383 GrContextFactory::kEnableNVPR_ContextOptions);
bsalomon8b7451a2016-05-11 06:33:06 -07001384 if (ctxInfo.grContext()) {
bsalomonf2f1c172016-04-05 12:59:06 -07001385 (*test)(reporter, ctxInfo);
kkinnunen179a8f52015-11-20 13:32:24 -08001386 }
1387 }
1388#endif
1389}
kkinnunen179a8f52015-11-20 13:32:24 -08001390} // namespace skiatest
1391
borenet48087572015-04-02 12:16:36 -07001392#if !defined(SK_BUILD_FOR_IOS)
jcgregorio3b27ade2014-11-13 08:06:40 -08001393int main(int argc, char** argv) {
1394 SkCommandLineFlags::Parse(argc, argv);
1395 return dm_main();
1396}
1397#endif