blob: c6e2ab98c71cbfd792baaa4bc0ca508f01d06270 [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
mtklein748ca3b2015-01-15 10:56:12 -08008#include "DMJsonWriter.h"
9#include "DMSrcSink.h"
tomhudsoneebc39a2015-02-23 12:18:05 -080010#include "DMSrcSinkAndroid.h"
mtklein748ca3b2015-01-15 10:56:12 -080011#include "ProcStats.h"
halcanary7d571242016-02-24 17:59:16 -080012#include "Resources.h"
mtklein748ca3b2015-01-15 10:56:12 -080013#include "SkBBHFactory.h"
mtklein62bd1a62015-01-27 14:46:26 -080014#include "SkChecksum.h"
scroggocc2feb12015-08-14 08:32:46 -070015#include "SkCodec.h"
mtklein27c3fdd2016-02-26 14:43:21 -080016#include "SkColorPriv.h"
msarett888dc162016-05-23 10:21:17 -070017#include "SkColorSpace.h"
caryclark17f0b6d2014-07-22 10:15:34 -070018#include "SkCommonFlags.h"
kkinnunen3e980c32015-12-23 01:33:00 -080019#include "SkCommonFlagsConfig.h"
brianosman3c579dc2016-04-19 09:18:11 -070020#include "SkData.h"
caryclark83ca6282015-06-10 09:31:09 -070021#include "SkFontMgr.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +000022#include "SkGraphics.h"
mtklein27c3fdd2016-02-26 14:43:21 -080023#include "SkHalf.h"
halcanary4dbbd042016-06-07 17:21:10 -070024#include "SkLeanWindows.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"
liyuqian38911a72016-10-04 11:23:22 -070037#include "SkScan.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?");
74
halcanary45420a92016-06-02 12:41:14 -070075DEFINE_string(mskps, "", "Directory to read mskps from, or a single mskp file.");
76
mtklein748ca3b2015-01-15 10:56:12 -080077using namespace DM;
bsalomon3724e572016-03-30 18:56:19 -070078using sk_gpu_test::GrContextFactory;
bsalomon273c0f52016-03-31 10:59:06 -070079using sk_gpu_test::GLTestContext;
bsalomonf2f1c172016-04-05 12:59:06 -070080using sk_gpu_test::ContextInfo;
mtklein@google.comd36522d2013-10-16 13:02:15 +000081
mtklein748ca3b2015-01-15 10:56:12 -080082/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
83
benjaminwagnerec4d4d72016-03-25 12:59:53 -070084static const double kStartMs = SkTime::GetMSecs();
mtkleinc41fd922016-03-08 09:01:39 -080085
86static FILE* gVLog;
87
88template <typename... Args>
89static void vlog(const char* fmt, Args&&... args) {
90 if (gVLog) {
91 fprintf(gVLog, "%s\t", HumanizeMs(SkTime::GetMSecs() - kStartMs).c_str());
92 fprintf(gVLog, fmt, args...);
93 fflush(gVLog);
94 }
95}
96
97template <typename... Args>
98static void info(const char* fmt, Args&&... args) {
99 vlog(fmt, args...);
100 if (!FLAGS_quiet) {
101 printf(fmt, args...);
102 }
103}
104static void info(const char* fmt) {
105 if (!FLAGS_quiet) {
106 printf("%s", fmt); // Clang warns printf(fmt) is insecure.
107 }
108}
109
reed086eea92016-05-04 17:12:46 -0700110SK_DECLARE_STATIC_MUTEX(gFailuresMutex);
mtklein748ca3b2015-01-15 10:56:12 -0800111static SkTArray<SkString> gFailures;
112
mtklein3eed7dd2016-02-24 19:07:07 -0800113static void fail(const SkString& err) {
mtklein748ca3b2015-01-15 10:56:12 -0800114 SkAutoMutexAcquire lock(gFailuresMutex);
115 SkDebugf("\n\nFAILURE: %s\n\n", err.c_str());
116 gFailures.push_back(err);
halcanary9e398f72015-01-10 11:18:04 -0800117}
118
mtklein0f7748a2016-07-25 15:27:29 -0700119struct Running {
120 SkString id;
121 SkThreadID thread;
122
123 void dump() const {
124 info("\t%s\n", id.c_str());
125 }
126};
mtkleinb37cb412015-03-18 05:27:14 -0700127
mtklein246ba3a2016-02-23 10:39:36 -0800128// We use a spinlock to make locking this in a signal handler _somewhat_ safe.
mtklein15923c92016-02-29 10:14:38 -0800129static SkSpinlock gMutex;
mtklein0f7748a2016-07-25 15:27:29 -0700130static int32_t gPending;
131static SkTArray<Running> gRunning;
mtklein748ca3b2015-01-15 10:56:12 -0800132
mtklein3eed7dd2016-02-24 19:07:07 -0800133static void done(const char* config, const char* src, const char* srcOptions, const char* name) {
134 SkString id = SkStringPrintf("%s %s %s %s", config, src, srcOptions, name);
mtkleinc41fd922016-03-08 09:01:39 -0800135 vlog("done %s\n", id.c_str());
mtkleinafae30a2016-02-24 12:28:32 -0800136 int pending;
mtkleinb37cb412015-03-18 05:27:14 -0700137 {
mtkleinf10637f2016-06-10 13:56:35 -0700138 SkAutoMutexAcquire lock(gMutex);
mtkleinb37cb412015-03-18 05:27:14 -0700139 for (int i = 0; i < gRunning.count(); i++) {
mtklein0f7748a2016-07-25 15:27:29 -0700140 if (gRunning[i].id == id) {
mtkleinb37cb412015-03-18 05:27:14 -0700141 gRunning.removeShuffle(i);
142 break;
143 }
144 }
mtkleinafae30a2016-02-24 12:28:32 -0800145 pending = --gPending;
reed50bc0512015-05-19 14:13:31 -0700146 }
mtkleina17241b2015-01-23 05:48:00 -0800147 // We write our dm.json file every once in a while in case we crash.
148 // Notice this also handles the final dm.json when pending == 0.
149 if (pending % 500 == 0) {
150 JsonWriter::DumpJson();
151 }
mtklein@google.comd36522d2013-10-16 13:02:15 +0000152}
153
mtklein3eed7dd2016-02-24 19:07:07 -0800154static void start(const char* config, const char* src, const char* srcOptions, const char* name) {
155 SkString id = SkStringPrintf("%s %s %s %s", config, src, srcOptions, name);
mtkleinc41fd922016-03-08 09:01:39 -0800156 vlog("start %s\n", id.c_str());
mtkleinf10637f2016-06-10 13:56:35 -0700157 SkAutoMutexAcquire lock(gMutex);
mtklein0f7748a2016-07-25 15:27:29 -0700158 gRunning.push_back({id,SkGetThreadID()});
mtkleinb37cb412015-03-18 05:27:14 -0700159}
160
mtkleinafae30a2016-02-24 12:28:32 -0800161static void print_status() {
mtkleinafae30a2016-02-24 12:28:32 -0800162 int curr = sk_tools::getCurrResidentSetSizeMB(),
163 peak = sk_tools::getMaxResidentSetSizeMB();
mtkleinc41fd922016-03-08 09:01:39 -0800164 SkString elapsed = HumanizeMs(SkTime::GetMSecs() - kStartMs);
mtkleinafae30a2016-02-24 12:28:32 -0800165
mtkleinf10637f2016-06-10 13:56:35 -0700166 SkAutoMutexAcquire lock(gMutex);
mtkleinc41fd922016-03-08 09:01:39 -0800167 info("\n%s elapsed, %d active, %d queued, %dMB RAM, %dMB peak\n",
168 elapsed.c_str(), gRunning.count(), gPending - gRunning.count(), curr, peak);
mtkleinafae30a2016-02-24 12:28:32 -0800169 for (auto& task : gRunning) {
mtklein0f7748a2016-07-25 15:27:29 -0700170 task.dump();
mtkleinafae30a2016-02-24 12:28:32 -0800171 }
172}
173
mtklein0f7748a2016-07-25 15:27:29 -0700174#if !defined(SK_BUILD_FOR_ANDROID)
175 static void find_culprit() {
176 // Assumes gMutex is locked.
177 SkThreadID thisThread = SkGetThreadID();
178 for (auto& task : gRunning) {
179 if (task.thread == thisThread) {
180 info("Likely culprit:\n");
181 task.dump();
182 }
183 }
184 }
185#endif
186
mtklein246ba3a2016-02-23 10:39:36 -0800187#if defined(SK_BUILD_FOR_WIN32)
mtklein5b64dab2016-06-09 08:59:48 -0700188 static LONG WINAPI crash_handler(EXCEPTION_POINTERS* e) {
mtkleinf8557292016-02-29 06:35:28 -0800189 static const struct {
190 const char* name;
robertphillips75467862016-03-01 14:10:23 -0800191 DWORD code;
mtkleinf8557292016-02-29 06:35:28 -0800192 } kExceptions[] = {
193 #define _(E) {#E, E}
194 _(EXCEPTION_ACCESS_VIOLATION),
195 _(EXCEPTION_BREAKPOINT),
196 _(EXCEPTION_INT_DIVIDE_BY_ZERO),
197 _(EXCEPTION_STACK_OVERFLOW),
198 // TODO: more?
199 #undef _
200 };
201
mtkleinf10637f2016-06-10 13:56:35 -0700202 SkAutoMutexAcquire lock(gMutex);
mtklein5b64dab2016-06-09 08:59:48 -0700203
204 const DWORD code = e->ExceptionRecord->ExceptionCode;
205 info("\nCaught exception %u", code);
206 for (const auto& exception : kExceptions) {
207 if (exception.code == code) {
208 info(" %s", exception.name);
mtkleinf8557292016-02-29 06:35:28 -0800209 }
mtkleinf8557292016-02-29 06:35:28 -0800210 }
mtklein5b64dab2016-06-09 08:59:48 -0700211 info(", was running:\n");
212 for (auto& task : gRunning) {
mtklein0f7748a2016-07-25 15:27:29 -0700213 task.dump();
mtklein5b64dab2016-06-09 08:59:48 -0700214 }
mtklein0f7748a2016-07-25 15:27:29 -0700215 find_culprit();
mtklein5b64dab2016-06-09 08:59:48 -0700216 fflush(stdout);
217
mtkleinf8557292016-02-29 06:35:28 -0800218 // Execute default exception handler... hopefully, exit.
219 return EXCEPTION_EXECUTE_HANDLER;
mtklein246ba3a2016-02-23 10:39:36 -0800220 }
mtklein5b64dab2016-06-09 08:59:48 -0700221 static void setup_crash_handler() { SetUnhandledExceptionFilter(crash_handler); }
mtklein246ba3a2016-02-23 10:39:36 -0800222
mtklein819ab102016-02-29 17:02:52 -0800223#elif !defined(SK_BUILD_FOR_ANDROID)
224 #include <execinfo.h>
mtklein246ba3a2016-02-23 10:39:36 -0800225 #include <signal.h>
mtklein819ab102016-02-29 17:02:52 -0800226 #include <stdlib.h>
mtkleinf8557292016-02-29 06:35:28 -0800227
mtklein5b64dab2016-06-09 08:59:48 -0700228 static void crash_handler(int sig) {
mtkleinf10637f2016-06-10 13:56:35 -0700229 SkAutoMutexAcquire lock(gMutex);
mtklein5b64dab2016-06-09 08:59:48 -0700230
231 info("\nCaught signal %d [%s], was running:\n", sig, strsignal(sig));
232 for (auto& task : gRunning) {
mtklein0f7748a2016-07-25 15:27:29 -0700233 task.dump();
mtklein5b64dab2016-06-09 08:59:48 -0700234 }
mtklein0f7748a2016-07-25 15:27:29 -0700235 find_culprit();
mtklein5b64dab2016-06-09 08:59:48 -0700236
237 void* stack[64];
238 int count = backtrace(stack, SK_ARRAY_COUNT(stack));
239 char** symbols = backtrace_symbols(stack, count);
240 info("\nStack trace:\n");
241 for (int i = 0; i < count; i++) {
242 info(" %s\n", symbols[i]);
243 }
244 fflush(stdout);
245
246 _Exit(sig);
247 }
248
mtklein246ba3a2016-02-23 10:39:36 -0800249 static void setup_crash_handler() {
250 const int kSignals[] = { SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGSEGV };
251 for (int sig : kSignals) {
mtklein5b64dab2016-06-09 08:59:48 -0700252 signal(sig, crash_handler);
mtklein246ba3a2016-02-23 10:39:36 -0800253 }
254 }
mtklein819ab102016-02-29 17:02:52 -0800255
256#else // Android
257 static void setup_crash_handler() {}
mtklein246ba3a2016-02-23 10:39:36 -0800258#endif
259
mtklein748ca3b2015-01-15 10:56:12 -0800260/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
mtklein114c3cd2015-01-15 10:15:02 -0800261
mtklein62bd1a62015-01-27 14:46:26 -0800262struct Gold : public SkString {
mtkleina82f5622015-02-20 12:30:19 -0800263 Gold() : SkString("") {}
mtklein3eed7dd2016-02-24 19:07:07 -0800264 Gold(const SkString& sink, const SkString& src,
265 const SkString& srcOptions, const SkString& name,
266 const SkString& md5)
mtklein62bd1a62015-01-27 14:46:26 -0800267 : SkString("") {
268 this->append(sink);
269 this->append(src);
djsollen54416de2015-04-03 07:24:48 -0700270 this->append(srcOptions);
mtklein62bd1a62015-01-27 14:46:26 -0800271 this->append(name);
272 this->append(md5);
mtklein62bd1a62015-01-27 14:46:26 -0800273 }
mtkleinc8d1dd42015-10-15 12:23:01 -0700274 struct Hash {
275 uint32_t operator()(const Gold& g) const {
276 return SkGoodHash()((const SkString&)g);
277 }
278 };
mtklein62bd1a62015-01-27 14:46:26 -0800279};
mtkleina82f5622015-02-20 12:30:19 -0800280static SkTHashSet<Gold, Gold::Hash> gGold;
mtklein62bd1a62015-01-27 14:46:26 -0800281
282static void add_gold(JsonWriter::BitmapResult r) {
djsollen54416de2015-04-03 07:24:48 -0700283 gGold.add(Gold(r.config, r.sourceType, r.sourceOptions, r.name, r.md5));
mtklein62bd1a62015-01-27 14:46:26 -0800284}
285
286static void gather_gold() {
287 if (!FLAGS_readPath.isEmpty()) {
288 SkString path(FLAGS_readPath[0]);
289 path.append("/dm.json");
290 if (!JsonWriter::ReadJson(path.c_str(), add_gold)) {
291 fail(SkStringPrintf("Couldn't read %s for golden results.", path.c_str()));
292 }
293 }
294}
295
296/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
297
mtklein935f1b12016-03-16 08:37:19 -0700298#if defined(SK_BUILD_FOR_WIN32)
299 static const char* kNewline = "\r\n";
300#else
301 static const char* kNewline = "\n";
302#endif
303
borenet09ed4802015-04-03 14:15:33 -0700304static SkTHashSet<SkString> gUninterestingHashes;
305
306static void gather_uninteresting_hashes() {
307 if (!FLAGS_uninterestingHashesFile.isEmpty()) {
bungeman38d909e2016-08-02 14:40:46 -0700308 sk_sp<SkData> data(SkData::MakeFromFileName(FLAGS_uninterestingHashesFile[0]));
mtkleincc334b32015-09-22 11:43:53 -0700309 if (!data) {
mtkleinc41fd922016-03-08 09:01:39 -0800310 info("WARNING: unable to read uninteresting hashes from %s\n",
311 FLAGS_uninterestingHashesFile[0]);
mtkleincc334b32015-09-22 11:43:53 -0700312 return;
313 }
borenet09ed4802015-04-03 14:15:33 -0700314 SkTArray<SkString> hashes;
mtklein935f1b12016-03-16 08:37:19 -0700315 SkStrSplit((const char*)data->data(), kNewline, &hashes);
borenet09ed4802015-04-03 14:15:33 -0700316 for (const SkString& hash : hashes) {
317 gUninterestingHashes.add(hash);
318 }
mtkleinc41fd922016-03-08 09:01:39 -0800319 info("FYI: loaded %d distinct uninteresting hashes from %d lines\n",
320 gUninterestingHashes.count(), hashes.count());
borenet09ed4802015-04-03 14:15:33 -0700321 }
322}
323
324/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
325
mtkleine0effd62015-07-29 06:37:28 -0700326struct TaggedSrc : public SkAutoTDelete<Src> {
mtklein3eed7dd2016-02-24 19:07:07 -0800327 SkString tag;
328 SkString options;
mtkleine0effd62015-07-29 06:37:28 -0700329};
330
331struct TaggedSink : public SkAutoTDelete<Sink> {
kkinnunen3e980c32015-12-23 01:33:00 -0800332 SkString tag;
djsollen54416de2015-04-03 07:24:48 -0700333};
mtklein748ca3b2015-01-15 10:56:12 -0800334
335static const bool kMemcpyOK = true;
336
mtkleine0effd62015-07-29 06:37:28 -0700337static SkTArray<TaggedSrc, kMemcpyOK> gSrcs;
338static SkTArray<TaggedSink, kMemcpyOK> gSinks;
mtklein748ca3b2015-01-15 10:56:12 -0800339
mtklein6393c062015-04-27 08:45:01 -0700340static bool in_shard() {
341 static int N = 0;
342 return N++ % FLAGS_shards == FLAGS_shard;
343}
344
mtklein3eed7dd2016-02-24 19:07:07 -0800345static void push_src(const char* tag, ImplicitString options, Src* s) {
mtklein748ca3b2015-01-15 10:56:12 -0800346 SkAutoTDelete<Src> src(s);
mtklein6393c062015-04-27 08:45:01 -0700347 if (in_shard() &&
mtklein3eed7dd2016-02-24 19:07:07 -0800348 FLAGS_src.contains(tag) &&
mtklein748ca3b2015-01-15 10:56:12 -0800349 !SkCommandLineFlags::ShouldSkip(FLAGS_match, src->name().c_str())) {
mtkleine0effd62015-07-29 06:37:28 -0700350 TaggedSrc& s = gSrcs.push_back();
mtklein18300a32016-03-16 13:53:35 -0700351 s.reset(src.release());
mtklein748ca3b2015-01-15 10:56:12 -0800352 s.tag = tag;
djsollen54416de2015-04-03 07:24:48 -0700353 s.options = options;
mtklein114c3cd2015-01-15 10:15:02 -0800354 }
mtklein748ca3b2015-01-15 10:56:12 -0800355}
mtklein114c3cd2015-01-15 10:15:02 -0800356
msarett9e707a02015-09-01 14:57:57 -0700357static void push_codec_src(Path path, CodecSrc::Mode mode, CodecSrc::DstColorType dstColorType,
scroggoc5560be2016-02-03 09:42:42 -0800358 SkAlphaType dstAlphaType, float scale) {
scroggoe4499842016-02-25 11:03:47 -0800359 if (FLAGS_simpleCodec) {
360 if (mode != CodecSrc::kCodec_Mode || dstColorType != CodecSrc::kGetFromCanvas_DstColorType
361 || scale != 1.0f)
362 // Only decode in the simple case.
363 return;
364 }
msarett9e707a02015-09-01 14:57:57 -0700365 SkString folder;
366 switch (mode) {
367 case CodecSrc::kCodec_Mode:
368 folder.append("codec");
369 break;
msarettbb25b532016-01-13 09:31:39 -0800370 case CodecSrc::kCodecZeroInit_Mode:
371 folder.append("codec_zero_init");
372 break;
msarett9e707a02015-09-01 14:57:57 -0700373 case CodecSrc::kScanline_Mode:
374 folder.append("scanline");
375 break;
msarett9e707a02015-09-01 14:57:57 -0700376 case CodecSrc::kStripe_Mode:
377 folder.append("stripe");
378 break;
msarett91c22b22016-02-22 12:27:46 -0800379 case CodecSrc::kCroppedScanline_Mode:
380 folder.append("crop");
381 break;
msarett9e707a02015-09-01 14:57:57 -0700382 case CodecSrc::kSubset_Mode:
msarett36c37962015-09-02 13:20:52 -0700383 folder.append("codec_subset");
msarett9e707a02015-09-01 14:57:57 -0700384 break;
385 }
386
387 switch (dstColorType) {
388 case CodecSrc::kGrayscale_Always_DstColorType:
389 folder.append("_kGray8");
390 break;
391 case CodecSrc::kIndex8_Always_DstColorType:
392 folder.append("_kIndex8");
393 break;
msarett34e0ec42016-04-22 16:27:24 -0700394 case CodecSrc::kNonNative8888_Always_DstColorType:
395 folder.append("_kNonNative");
396 break;
msarett9e707a02015-09-01 14:57:57 -0700397 default:
398 break;
399 }
400
scroggoc5560be2016-02-03 09:42:42 -0800401 switch (dstAlphaType) {
scroggoc5560be2016-02-03 09:42:42 -0800402 case kPremul_SkAlphaType:
403 folder.append("_premul");
404 break;
405 case kUnpremul_SkAlphaType:
406 folder.append("_unpremul");
407 break;
408 default:
409 break;
410 }
411
msarett9e707a02015-09-01 14:57:57 -0700412 if (1.0f != scale) {
413 folder.appendf("_%.3f", scale);
414 }
415
scroggoc5560be2016-02-03 09:42:42 -0800416 CodecSrc* src = new CodecSrc(path, mode, dstColorType, dstAlphaType, scale);
msarett9e707a02015-09-01 14:57:57 -0700417 push_src("image", folder, src);
418}
419
scroggof8dc9df2016-05-16 09:04:13 -0700420static void push_android_codec_src(Path path, CodecSrc::DstColorType dstColorType,
421 SkAlphaType dstAlphaType, int sampleSize) {
msarett3d9d7a72015-10-21 10:27:10 -0700422 SkString folder;
scroggof8dc9df2016-05-16 09:04:13 -0700423 folder.append("scaled_codec");
msarett3d9d7a72015-10-21 10:27:10 -0700424
425 switch (dstColorType) {
426 case CodecSrc::kGrayscale_Always_DstColorType:
427 folder.append("_kGray8");
428 break;
429 case CodecSrc::kIndex8_Always_DstColorType:
430 folder.append("_kIndex8");
431 break;
msarett34e0ec42016-04-22 16:27:24 -0700432 case CodecSrc::kNonNative8888_Always_DstColorType:
433 folder.append("_kNonNative");
434 break;
msarett3d9d7a72015-10-21 10:27:10 -0700435 default:
436 break;
437 }
438
scroggoc5560be2016-02-03 09:42:42 -0800439 switch (dstAlphaType) {
scroggoc5560be2016-02-03 09:42:42 -0800440 case kPremul_SkAlphaType:
441 folder.append("_premul");
442 break;
msarett9e9444c2016-02-03 12:39:10 -0800443 case kUnpremul_SkAlphaType:
444 folder.append("_unpremul");
445 break;
scroggoc5560be2016-02-03 09:42:42 -0800446 default:
447 break;
448 }
449
msarett3d9d7a72015-10-21 10:27:10 -0700450 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800451 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700452 }
453
scroggof8dc9df2016-05-16 09:04:13 -0700454 AndroidCodecSrc* src = new AndroidCodecSrc(path, dstColorType, dstAlphaType, sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700455 push_src("image", folder, src);
456}
457
msarett18976312016-03-09 14:20:58 -0800458static void push_image_gen_src(Path path, ImageGenSrc::Mode mode, SkAlphaType alphaType, bool isGpu)
459{
460 SkString folder;
461 switch (mode) {
462 case ImageGenSrc::kCodec_Mode:
463 folder.append("gen_codec");
464 break;
465 case ImageGenSrc::kPlatform_Mode:
466 folder.append("gen_platform");
467 break;
468 }
469
470 if (isGpu) {
471 folder.append("_gpu");
472 } else {
473 switch (alphaType) {
474 case kOpaque_SkAlphaType:
475 folder.append("_opaque");
476 break;
477 case kPremul_SkAlphaType:
478 folder.append("_premul");
479 break;
480 case kUnpremul_SkAlphaType:
481 folder.append("_unpremul");
482 break;
483 default:
484 break;
485 }
486 }
487
488 ImageGenSrc* src = new ImageGenSrc(path, mode, alphaType, isGpu);
489 push_src("image", folder, src);
490}
491
msarett438b2ad2015-04-09 12:43:10 -0700492static void push_codec_srcs(Path path) {
bungeman38d909e2016-08-02 14:40:46 -0700493 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
msarett438b2ad2015-04-09 12:43:10 -0700494 if (!encoded) {
mtkleinc41fd922016-03-08 09:01:39 -0800495 info("Couldn't read %s.", path.c_str());
msarett438b2ad2015-04-09 12:43:10 -0700496 return;
497 }
reed42943c82016-09-12 12:01:44 -0700498 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
halcanary96fcdcc2015-08-27 07:41:13 -0700499 if (nullptr == codec.get()) {
mtkleinc41fd922016-03-08 09:01:39 -0800500 info("Couldn't create codec for %s.", path.c_str());
msarett438b2ad2015-04-09 12:43:10 -0700501 return;
502 }
503
scroggo9366aff2016-05-20 08:38:54 -0700504 // native scaling is only supported by WEBP and JPEG
505 bool supportsNativeScaling = false;
msarett438b2ad2015-04-09 12:43:10 -0700506
msarett91c22b22016-02-22 12:27:46 -0800507 SkTArray<CodecSrc::Mode> nativeModes;
508 nativeModes.push_back(CodecSrc::kCodec_Mode);
509 nativeModes.push_back(CodecSrc::kCodecZeroInit_Mode);
msarett91c22b22016-02-22 12:27:46 -0800510 switch (codec->getEncodedFormat()) {
511 case SkEncodedFormat::kJPEG_SkEncodedFormat:
512 nativeModes.push_back(CodecSrc::kScanline_Mode);
513 nativeModes.push_back(CodecSrc::kStripe_Mode);
514 nativeModes.push_back(CodecSrc::kCroppedScanline_Mode);
scroggo9366aff2016-05-20 08:38:54 -0700515 supportsNativeScaling = true;
msarett91c22b22016-02-22 12:27:46 -0800516 break;
517 case SkEncodedFormat::kWEBP_SkEncodedFormat:
518 nativeModes.push_back(CodecSrc::kSubset_Mode);
scroggo9366aff2016-05-20 08:38:54 -0700519 supportsNativeScaling = true;
msarett91c22b22016-02-22 12:27:46 -0800520 break;
yujieqin7a307df2016-03-11 07:32:33 -0800521 case SkEncodedFormat::kDNG_SkEncodedFormat:
msarettb65e6042016-02-23 05:37:25 -0800522 break;
msarett91c22b22016-02-22 12:27:46 -0800523 default:
524 nativeModes.push_back(CodecSrc::kScanline_Mode);
msarett91c22b22016-02-22 12:27:46 -0800525 break;
526 }
msarett9e707a02015-09-01 14:57:57 -0700527
msarett91c22b22016-02-22 12:27:46 -0800528 SkTArray<CodecSrc::DstColorType> colorTypes;
529 colorTypes.push_back(CodecSrc::kGetFromCanvas_DstColorType);
msarett34e0ec42016-04-22 16:27:24 -0700530 colorTypes.push_back(CodecSrc::kNonNative8888_Always_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700531 switch (codec->getInfo().colorType()) {
532 case kGray_8_SkColorType:
msarett91c22b22016-02-22 12:27:46 -0800533 colorTypes.push_back(CodecSrc::kGrayscale_Always_DstColorType);
msarett55f7bdd2016-02-16 13:24:54 -0800534 if (kWBMP_SkEncodedFormat == codec->getEncodedFormat()) {
msarett91c22b22016-02-22 12:27:46 -0800535 colorTypes.push_back(CodecSrc::kIndex8_Always_DstColorType);
msarett55f7bdd2016-02-16 13:24:54 -0800536 }
msarett36c37962015-09-02 13:20:52 -0700537 break;
538 case kIndex_8_SkColorType:
msarett91c22b22016-02-22 12:27:46 -0800539 colorTypes.push_back(CodecSrc::kIndex8_Always_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700540 break;
541 default:
msarett36c37962015-09-02 13:20:52 -0700542 break;
543 }
msarett9e707a02015-09-01 14:57:57 -0700544
scroggoc5560be2016-02-03 09:42:42 -0800545 SkTArray<SkAlphaType> alphaModes;
546 alphaModes.push_back(kPremul_SkAlphaType);
scroggof2c96a22016-05-20 11:08:27 -0700547 if (codec->getInfo().alphaType() != kOpaque_SkAlphaType) {
548 alphaModes.push_back(kUnpremul_SkAlphaType);
scroggoc5560be2016-02-03 09:42:42 -0800549 }
msarettb714fb02016-01-22 14:46:42 -0800550
551 for (CodecSrc::Mode mode : nativeModes) {
scroggo9366aff2016-05-20 08:38:54 -0700552 for (CodecSrc::DstColorType colorType : colorTypes) {
553 for (SkAlphaType alphaType : alphaModes) {
scroggof2c96a22016-05-20 11:08:27 -0700554 // Only test kCroppedScanline_Mode when the alpha type is premul. The test is
scroggo9366aff2016-05-20 08:38:54 -0700555 // slow and won't be interestingly different with different alpha types.
556 if (CodecSrc::kCroppedScanline_Mode == mode &&
scroggof2c96a22016-05-20 11:08:27 -0700557 kPremul_SkAlphaType != alphaType) {
scroggo9366aff2016-05-20 08:38:54 -0700558 continue;
559 }
msarett91c22b22016-02-22 12:27:46 -0800560
scroggo9366aff2016-05-20 08:38:54 -0700561 push_codec_src(path, mode, colorType, alphaType, 1.0f);
msarettf3dc1882016-04-26 13:06:38 -0700562
scroggo9366aff2016-05-20 08:38:54 -0700563 // Skip kNonNative on different native scales. It won't be interestingly
564 // different.
565 if (supportsNativeScaling &&
566 CodecSrc::kNonNative8888_Always_DstColorType == colorType) {
567 // Native Scales
568 // SkJpegCodec natively supports scaling to the following:
569 for (auto scale : { 0.125f, 0.25f, 0.375f, 0.5f, 0.625f, 0.750f, 0.875f }) {
570 push_codec_src(path, mode, colorType, alphaType, scale);
571 }
scroggoc5560be2016-02-03 09:42:42 -0800572 }
msarett9e707a02015-09-01 14:57:57 -0700573 }
574 }
msarett0a242972015-06-11 14:27:27 -0700575 }
msarett36c37962015-09-02 13:20:52 -0700576
scroggoe4499842016-02-25 11:03:47 -0800577 if (FLAGS_simpleCodec) {
578 return;
579 }
580
msarett3d9d7a72015-10-21 10:27:10 -0700581 const int sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
msarett36c37962015-09-02 13:20:52 -0700582
msarett3d9d7a72015-10-21 10:27:10 -0700583 for (int sampleSize : sampleSizes) {
msarett91c22b22016-02-22 12:27:46 -0800584 for (CodecSrc::DstColorType colorType : colorTypes) {
scroggoc5560be2016-02-03 09:42:42 -0800585 for (SkAlphaType alphaType : alphaModes) {
msarettf3dc1882016-04-26 13:06:38 -0700586 // We can exercise all of the kNonNative support code in the swizzler with just a
587 // few sample sizes. Skip the rest.
588 if (CodecSrc::kNonNative8888_Always_DstColorType == colorType && sampleSize > 3) {
589 continue;
590 }
591
scroggof8dc9df2016-05-16 09:04:13 -0700592 push_android_codec_src(path, colorType, alphaType, sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700593 }
msarett36c37962015-09-02 13:20:52 -0700594 }
595 }
msarett18976312016-03-09 14:20:58 -0800596
597 static const char* const rawExts[] = {
598 "arw", "cr2", "dng", "nef", "nrw", "orf", "raf", "rw2", "pef", "srw",
599 "ARW", "CR2", "DNG", "NEF", "NRW", "ORF", "RAF", "RW2", "PEF", "SRW",
600 };
601
602 // There is not currently a reason to test RAW images on image generator.
603 // If we want to enable these tests, we will need to fix skbug.com/5079.
604 for (const char* ext : rawExts) {
605 if (path.endsWith(ext)) {
606 return;
607 }
608 }
609
610 // Push image generator GPU test.
brianosmanc9ced392016-03-24 07:27:43 -0700611 push_image_gen_src(path, ImageGenSrc::kCodec_Mode, codec->getInfo().alphaType(), true);
msarett18976312016-03-09 14:20:58 -0800612
613 // Push image generator CPU tests.
614 for (SkAlphaType alphaType : alphaModes) {
615 push_image_gen_src(path, ImageGenSrc::kCodec_Mode, alphaType, false);
616
617#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
618 if (kWEBP_SkEncodedFormat != codec->getEncodedFormat() &&
619 kWBMP_SkEncodedFormat != codec->getEncodedFormat() &&
620 kUnpremul_SkAlphaType != alphaType)
621 {
622 push_image_gen_src(path, ImageGenSrc::kPlatform_Mode, alphaType, false);
623 }
msarettfc0b6d12016-03-17 13:50:17 -0700624#elif defined(SK_BUILD_FOR_WIN)
625 if (kWEBP_SkEncodedFormat != codec->getEncodedFormat() &&
626 kWBMP_SkEncodedFormat != codec->getEncodedFormat())
627 {
628 push_image_gen_src(path, ImageGenSrc::kPlatform_Mode, alphaType, false);
629 }
msarett18976312016-03-09 14:20:58 -0800630#endif
631 }
msarett438b2ad2015-04-09 12:43:10 -0700632}
633
msarettd1227a72016-05-18 06:23:57 -0700634static void push_brd_src(Path path, CodecSrc::DstColorType dstColorType, BRDSrc::Mode mode,
635 uint32_t sampleSize) {
636 SkString folder("brd_android_codec");
msaretta5783ae2015-09-08 15:35:32 -0700637 switch (mode) {
638 case BRDSrc::kFullImage_Mode:
639 break;
640 case BRDSrc::kDivisor_Mode:
641 folder.append("_divisor");
642 break;
643 default:
644 SkASSERT(false);
645 return;
646 }
647
648 switch (dstColorType) {
649 case CodecSrc::kGetFromCanvas_DstColorType:
650 break;
651 case CodecSrc::kIndex8_Always_DstColorType:
652 folder.append("_kIndex");
653 break;
654 case CodecSrc::kGrayscale_Always_DstColorType:
655 folder.append("_kGray");
656 break;
657 default:
658 SkASSERT(false);
659 return;
660 }
661
662 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800663 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700664 }
665
msarettd1227a72016-05-18 06:23:57 -0700666 BRDSrc* src = new BRDSrc(path, mode, dstColorType, sampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700667 push_src("image", folder, src);
668}
669
670static void push_brd_srcs(Path path) {
scroggo24a9bd72016-05-20 06:02:42 -0700671 // Only run Index8 and grayscale to one sampleSize and Mode. Though interesting
672 // to test these color types, they should not reveal anything across various
673 // sampleSizes and Modes
674 for (auto type : { CodecSrc::kIndex8_Always_DstColorType,
675 CodecSrc::kGrayscale_Always_DstColorType }) {
676 // Arbitrarily choose Mode and sampleSize.
677 push_brd_src(path, type, BRDSrc::kFullImage_Mode, 2);
678 }
679
680
scroggo501b7342015-11-03 07:55:11 -0800681 // Test on a variety of sampleSizes, making sure to include:
682 // - 2, 4, and 8, which are natively supported by jpeg
683 // - multiples of 2 which are not divisible by 4 (analogous for 4)
684 // - larger powers of two, since BRD clients generally use powers of 2
685 // We will only produce output for the larger sizes on large images.
686 const uint32_t sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 24, 32, 64 };
msarett6efbe052015-09-11 09:01:16 -0700687
msaretta5783ae2015-09-08 15:35:32 -0700688 const BRDSrc::Mode modes[] = {
msarett26ad17b2015-10-22 07:29:19 -0700689 BRDSrc::kFullImage_Mode,
690 BRDSrc::kDivisor_Mode,
msaretta5783ae2015-09-08 15:35:32 -0700691 };
692
msarettd1227a72016-05-18 06:23:57 -0700693 for (uint32_t sampleSize : sampleSizes) {
scroggo24a9bd72016-05-20 06:02:42 -0700694 for (BRDSrc::Mode mode : modes) {
695 push_brd_src(path, CodecSrc::kGetFromCanvas_DstColorType, mode, sampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700696 }
697 }
698}
699
700static bool brd_supported(const char* ext) {
msarett8c8f22a2015-04-01 06:58:48 -0700701 static const char* const exts[] = {
msaretta5783ae2015-09-08 15:35:32 -0700702 "jpg", "jpeg", "png", "webp",
703 "JPG", "JPEG", "PNG", "WEBP",
msarett8c8f22a2015-04-01 06:58:48 -0700704 };
705
706 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
707 if (0 == strcmp(exts[i], ext)) {
708 return true;
709 }
710 }
711 return false;
scroggo9b77ddd2015-03-19 06:03:39 -0700712}
713
fmalitaa2b9fdf2016-08-03 19:53:36 -0700714template <typename T>
715void gather_file_srcs(const SkCommandLineFlags::StringArray& flags, const char* ext) {
716 for (int i = 0; i < flags.count(); i++) {
717 const char* path = flags[i];
718 if (sk_isdir(path)) {
719 SkOSFile::Iter it(path, ext);
720 for (SkString file; it.next(&file); ) {
721 push_src(ext, "", new T(SkOSPath::Join(path, file.c_str())));
722 }
723 } else {
724 push_src(ext, "", new T(path));
725 }
726 }
727}
728
scroggo86737142016-02-03 12:19:11 -0800729static bool gather_srcs() {
mtklein748ca3b2015-01-15 10:56:12 -0800730 for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) {
djsollen54416de2015-04-03 07:24:48 -0700731 push_src("gm", "", new GMSrc(r->factory()));
mtklein748ca3b2015-01-15 10:56:12 -0800732 }
scroggo86737142016-02-03 12:19:11 -0800733
fmalitaa2b9fdf2016-08-03 19:53:36 -0700734 gather_file_srcs<SKPSrc>(FLAGS_skps, "skp");
735 gather_file_srcs<MSKPSrc>(FLAGS_mskps, "mskp");
736#if defined(SK_XML)
737 gather_file_srcs<SVGSrc>(FLAGS_svgs, "svg");
738#endif
halcanary45420a92016-06-02 12:41:14 -0700739
scroggo86737142016-02-03 12:19:11 -0800740 SkTArray<SkString> images;
msarett69deca82016-04-29 09:38:40 -0700741 if (!CollectImages(FLAGS_images, &images)) {
scroggo86737142016-02-03 12:19:11 -0800742 return false;
743 }
744
745 for (auto image : images) {
746 push_codec_srcs(image);
scroggoe4499842016-02-25 11:03:47 -0800747 if (FLAGS_simpleCodec) {
748 continue;
749 }
750
mtklein21eaf3b2016-02-08 12:39:59 -0800751 const char* ext = strrchr(image.c_str(), '.');
752 if (ext && brd_supported(ext+1)) {
scroggo86737142016-02-03 12:19:11 -0800753 push_brd_srcs(image);
mtklein709d2c32015-01-15 08:30:25 -0800754 }
mtklein709d2c32015-01-15 08:30:25 -0800755 }
scroggo86737142016-02-03 12:19:11 -0800756
msarett69deca82016-04-29 09:38:40 -0700757 SkTArray<SkString> colorImages;
758 if (!CollectImages(FLAGS_colorImages, &colorImages)) {
759 return false;
760 }
761
762 for (auto colorImage : colorImages) {
msarett9ce3a542016-07-15 13:54:38 -0700763 ColorCodecSrc* src = new ColorCodecSrc(colorImage, ColorCodecSrc::kBaseline_Mode,
764 kN32_SkColorType);
msarettb8d1aac2016-08-04 12:22:06 -0700765 push_src("colorImage", "color_codec_baseline", src);
msarett888dc162016-05-23 10:21:17 -0700766
msarett9ce3a542016-07-15 13:54:38 -0700767 src = new ColorCodecSrc(colorImage, ColorCodecSrc::kDst_HPZR30w_Mode, kN32_SkColorType);
msarettb8d1aac2016-08-04 12:22:06 -0700768 push_src("colorImage", "color_codec_HPZR30w", src);
msarett9ce3a542016-07-15 13:54:38 -0700769 // TODO (msarett):
770 // Should we test this Dst in F16 mode (even though the Dst gamma is 2.2 instead of sRGB)?
msarett9876ac52016-06-01 14:47:18 -0700771
msarett9ce3a542016-07-15 13:54:38 -0700772 src = new ColorCodecSrc(colorImage, ColorCodecSrc::kDst_sRGB_Mode, kN32_SkColorType);
msarettb8d1aac2016-08-04 12:22:06 -0700773 push_src("colorImage", "color_codec_sRGB_kN32", src);
msarett9ce3a542016-07-15 13:54:38 -0700774 src = new ColorCodecSrc(colorImage, ColorCodecSrc::kDst_sRGB_Mode, kRGBA_F16_SkColorType);
msarettb8d1aac2016-08-04 12:22:06 -0700775 push_src("colorImage", "color_codec_sRGB_kF16", src);
msarettd2809572016-06-20 06:07:45 -0700776
msarett469f1c52016-06-06 08:20:37 -0700777#if defined(SK_TEST_QCMS)
msarett50ce1f22016-07-29 06:23:33 -0700778 src = new ColorCodecSrc(colorImage, ColorCodecSrc::kQCMS_HPZR30w_Mode,
779 kRGBA_8888_SkColorType);
msarettb8d1aac2016-08-04 12:22:06 -0700780 push_src("colorImage", "color_codec_QCMS_HPZR30w", src);
msarett9876ac52016-06-01 14:47:18 -0700781#endif
msarett69deca82016-04-29 09:38:40 -0700782 }
783
scroggo86737142016-02-03 12:19:11 -0800784 return true;
mtklein709d2c32015-01-15 08:30:25 -0800785}
786
kkinnunen3e980c32015-12-23 01:33:00 -0800787static void push_sink(const SkCommandLineConfig& config, Sink* s) {
rmistry0f515bd2015-12-22 10:22:26 -0800788 SkAutoTDelete<Sink> sink(s);
kkinnunen3e980c32015-12-23 01:33:00 -0800789
mtkleine0effd62015-07-29 06:37:28 -0700790 // Try a simple Src as a canary. If it fails, skip this sink.
mtklein748ca3b2015-01-15 10:56:12 -0800791 struct : public Src {
mtkleine0effd62015-07-29 06:37:28 -0700792 Error draw(SkCanvas* c) const override {
793 c->drawRect(SkRect::MakeWH(1,1), SkPaint());
794 return "";
795 }
mtklein36352bf2015-03-25 18:17:31 -0700796 SkISize size() const override { return SkISize::Make(16, 16); }
mtkleine0effd62015-07-29 06:37:28 -0700797 Name name() const override { return "justOneRect"; }
798 } justOneRect;
mtklein114c3cd2015-01-15 10:15:02 -0800799
mtklein748ca3b2015-01-15 10:56:12 -0800800 SkBitmap bitmap;
801 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800802 SkString log;
mtkleine0effd62015-07-29 06:37:28 -0700803 Error err = sink->draw(justOneRect, &bitmap, &stream, &log);
mtklein4089ef72015-03-05 08:40:28 -0800804 if (err.isFatal()) {
mtkleinc41fd922016-03-08 09:01:39 -0800805 info("Could not run %s: %s\n", config.getTag().c_str(), err.c_str());
mtklein05641a52015-04-21 10:49:13 -0700806 exit(1);
mtklein114c3cd2015-01-15 10:15:02 -0800807 }
808
mtkleine0effd62015-07-29 06:37:28 -0700809 TaggedSink& ts = gSinks.push_back();
mtklein18300a32016-03-16 13:53:35 -0700810 ts.reset(sink.release());
kkinnunen3e980c32015-12-23 01:33:00 -0800811 ts.tag = config.getTag();
mtklein748ca3b2015-01-15 10:56:12 -0800812}
813
814static bool gpu_supported() {
815#if SK_SUPPORT_GPU
816 return FLAGS_gpu;
817#else
818 return false;
819#endif
820}
kkinnunen9ebc3f02015-12-21 23:48:13 -0800821
kkinnunen3e980c32015-12-23 01:33:00 -0800822static Sink* create_sink(const SkCommandLineConfig* config) {
823#if SK_SUPPORT_GPU
824 if (gpu_supported()) {
825 if (const SkCommandLineConfigGpu* gpuConfig = config->asConfigGpu()) {
bsalomon85b4b532016-04-05 11:06:27 -0700826 GrContextFactory::ContextType contextType = gpuConfig->getContextType();
csmartdalton6270e552016-09-13 10:41:49 -0700827 GrContextFactory::ContextOptions contextOptions = gpuConfig->getContextOptions();
kkinnunen3e980c32015-12-23 01:33:00 -0800828 GrContextFactory testFactory;
829 if (!testFactory.get(contextType, contextOptions)) {
mtkleinc41fd922016-03-08 09:01:39 -0800830 info("WARNING: can not create GPU context for config '%s'. "
831 "GM tests will be skipped.\n", gpuConfig->getTag().c_str());
kkinnunen3e980c32015-12-23 01:33:00 -0800832 return nullptr;
833 }
834 return new GPUSink(contextType, contextOptions, gpuConfig->getSamples(),
brianosmand93c1202016-03-10 07:49:08 -0800835 gpuConfig->getUseDIText(), gpuConfig->getColorType(),
brianosmanb109b8c2016-06-16 13:03:24 -0700836 sk_ref_sp(gpuConfig->getColorSpace()), FLAGS_gpu_threading);
kkinnunen3e980c32015-12-23 01:33:00 -0800837 }
838 }
839#endif
840
841#define SINK(t, sink, ...) if (config->getBackend().equals(t)) { return new sink(__VA_ARGS__); }
842
tomhudsoneebc39a2015-02-23 12:18:05 -0800843#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
844 SINK("hwui", HWUISink);
845#endif
846
mtklein748ca3b2015-01-15 10:56:12 -0800847 if (FLAGS_cpu) {
brianosmanb109b8c2016-06-16 13:03:24 -0700848 auto srgbColorSpace = SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named);
849
mtklein748ca3b2015-01-15 10:56:12 -0800850 SINK("565", RasterSink, kRGB_565_SkColorType);
851 SINK("8888", RasterSink, kN32_SkColorType);
brianosmanb109b8c2016-06-16 13:03:24 -0700852 SINK("srgb", RasterSink, kN32_SkColorType, srgbColorSpace);
brianosman0e22eb82016-08-30 07:07:59 -0700853 SINK("f16", RasterSink, kRGBA_F16_SkColorType, srgbColorSpace->makeLinearGamma());
halcanary94b87bd2016-04-04 06:12:15 -0700854 SINK("pdf", PDFSink);
mtklein9c3f17d2015-01-28 11:35:18 -0800855 SINK("skp", SKPSink);
reed54dc4872016-09-13 08:09:45 -0700856 SINK("pipe", PipeSink);
mtklein8a4527e2015-01-31 20:00:58 -0800857 SINK("svg", SVGSink);
858 SINK("null", NullSink);
halcanary47ef4d52015-03-03 09:13:09 -0800859 SINK("xps", XPSSink);
halcanary4b656662016-04-27 07:45:18 -0700860 SINK("pdfa", PDFSink, true);
mtklein748ca3b2015-01-15 10:56:12 -0800861 }
862#undef SINK
halcanary96fcdcc2015-08-27 07:41:13 -0700863 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800864}
865
kkinnunen3e980c32015-12-23 01:33:00 -0800866static Sink* create_via(const SkString& tag, Sink* wrapped) {
867#define VIA(t, via, ...) if (tag.equals(t)) { return new via(__VA_ARGS__); }
mtklein9c5052f2016-08-06 12:51:51 -0700868 VIA("lite", ViaLite, wrapped);
reed54dc4872016-09-13 08:09:45 -0700869 VIA("pipe", ViaPipe, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700870 VIA("twice", ViaTwice, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700871 VIA("serialize", ViaSerialization, wrapped);
mtklein4a34ecb2016-01-08 10:19:35 -0800872 VIA("pic", ViaPicture, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700873 VIA("2ndpic", ViaSecondPicture, wrapped);
mtkleind31c13d2015-05-05 12:59:56 -0700874 VIA("sp", ViaSingletonPictures, wrapped);
reedbabc3de2016-07-08 08:43:27 -0700875 VIA("defer", ViaDefer, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700876 VIA("tiles", ViaTiles, 256, 256, nullptr, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800877 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800878
mtkleind603b222015-02-17 11:13:33 -0800879 if (FLAGS_matrix.count() == 4) {
mtklein748ca3b2015-01-15 10:56:12 -0800880 SkMatrix m;
mtkleind603b222015-02-17 11:13:33 -0800881 m.reset();
882 m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
883 m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
884 m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
885 m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
886 VIA("matrix", ViaMatrix, m, wrapped);
887 VIA("upright", ViaUpright, m, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800888 }
tomhudson64de1e12015-03-05 08:01:07 -0800889
890#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
891 VIA("androidsdk", ViaAndroidSDK, wrapped);
892#endif
893
mtklein748ca3b2015-01-15 10:56:12 -0800894#undef VIA
halcanary96fcdcc2015-08-27 07:41:13 -0700895 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800896}
897
brianosman05c987c2016-05-09 13:42:16 -0700898static bool gather_sinks() {
kkinnunen3e980c32015-12-23 01:33:00 -0800899 SkCommandLineConfigArray configs;
900 ParseConfigs(FLAGS_config, &configs);
901 for (int i = 0; i < configs.count(); i++) {
902 const SkCommandLineConfig& config = *configs[i];
903 Sink* sink = create_sink(&config);
904 if (sink == nullptr) {
mtkleinc41fd922016-03-08 09:01:39 -0800905 info("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
906 config.getTag().c_str());
kkinnunen3e980c32015-12-23 01:33:00 -0800907 continue;
908 }
mtklein748ca3b2015-01-15 10:56:12 -0800909
kkinnunen3e980c32015-12-23 01:33:00 -0800910 const SkTArray<SkString>& parts = config.getViaParts();
911 for (int j = parts.count(); j-- > 0;) {
912 const SkString& part = parts[j];
913 Sink* next = create_via(part, sink);
halcanary96fcdcc2015-08-27 07:41:13 -0700914 if (next == nullptr) {
mtkleinc41fd922016-03-08 09:01:39 -0800915 info("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
916 part.c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800917 delete sink;
halcanary96fcdcc2015-08-27 07:41:13 -0700918 sink = nullptr;
mtklein748ca3b2015-01-15 10:56:12 -0800919 break;
920 }
921 sink = next;
922 }
923 if (sink) {
924 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800925 }
926 }
brianosman05c987c2016-05-09 13:42:16 -0700927
928 // If no configs were requested (just running tests, perhaps?), then we're okay.
929 // Otherwise, make sure that at least one sink was constructed correctly. This catches
930 // the case of bots without a GPU being assigned GPU configs.
931 return (configs.count() == 0) || (gSinks.count() > 0);
mtklein114c3cd2015-01-15 10:15:02 -0800932}
mtklein709d2c32015-01-15 08:30:25 -0800933
mtkleina5114d72015-08-24 13:27:01 -0700934static bool dump_png(SkBitmap bitmap, const char* path, const char* md5) {
935 const int w = bitmap.width(),
936 h = bitmap.height();
937
brianosman3c579dc2016-04-19 09:18:11 -0700938 sk_sp<SkData> encodedBitmap = sk_tools::encode_bitmap_for_png(bitmap);
939 if (encodedBitmap.get() == nullptr) {
940 return false;
mtkleina5114d72015-08-24 13:27:01 -0700941 }
brianosman3c579dc2016-04-19 09:18:11 -0700942 uint32_t* rgba = static_cast<uint32_t*>(encodedBitmap.get()->writable_data());
mtkleina5114d72015-08-24 13:27:01 -0700943
944 // We don't need bitmap anymore. Might as well drop our ref.
mtkleinfccb77d2015-08-24 14:13:29 -0700945 bitmap.reset();
mtkleina5114d72015-08-24 13:27:01 -0700946
mtkleinc64137c2015-08-25 10:56:08 -0700947 FILE* f = fopen(path, "wb");
mtkleina5114d72015-08-24 13:27:01 -0700948 if (!f) { return false; }
949
950 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
951 if (!png) {
952 fclose(f);
953 return false;
954 }
955
956 png_infop info = png_create_info_struct(png);
957 if (!info) {
958 png_destroy_write_struct(&png, &info);
959 fclose(f);
960 return false;
961 }
962
mtkleind69ece62015-09-10 10:37:44 -0700963 SkString description;
964 description.append("Key: ");
965 for (int i = 0; i < FLAGS_key.count(); i++) {
966 description.appendf("%s ", FLAGS_key[i]);
967 }
968 description.append("Properties: ");
969 for (int i = 0; i < FLAGS_properties.count(); i++) {
970 description.appendf("%s ", FLAGS_properties[i]);
971 }
972 description.appendf("MD5: %s", md5);
973
mtkleina5114d72015-08-24 13:27:01 -0700974 png_text text[2];
975 text[0].key = (png_charp)"Author";
976 text[0].text = (png_charp)"DM dump_png()";
977 text[0].compression = PNG_TEXT_COMPRESSION_NONE;
978 text[1].key = (png_charp)"Description";
mtkleind69ece62015-09-10 10:37:44 -0700979 text[1].text = (png_charp)description.c_str();
mtkleina5114d72015-08-24 13:27:01 -0700980 text[1].compression = PNG_TEXT_COMPRESSION_NONE;
981 png_set_text(png, info, text, 2);
982
983 png_init_io(png, f);
984 png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8,
985 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
986 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
987 png_write_info(png, info);
988 for (int j = 0; j < h; j++) {
brianosman3c579dc2016-04-19 09:18:11 -0700989 png_bytep row = (png_bytep)(rgba + w*j);
mtkleina5114d72015-08-24 13:27:01 -0700990 png_write_rows(png, &row, 1);
991 }
992 png_write_end(png, info);
993
994 png_destroy_write_struct(&png, &info);
995 fclose(f);
996 return true;
997}
998
mtkleina2ef6422015-01-15 13:44:22 -0800999static bool match(const char* needle, const char* haystack) {
halcanary96fcdcc2015-08-27 07:41:13 -07001000 return 0 == strcmp("_", needle) || nullptr != strstr(haystack, needle);
mtkleina2ef6422015-01-15 13:44:22 -08001001}
1002
mtklein3eed7dd2016-02-24 19:07:07 -08001003static bool is_blacklisted(const char* sink, const char* src,
1004 const char* srcOptions, const char* name) {
mtklein0d243ff2015-04-03 07:51:00 -07001005 for (int i = 0; i < FLAGS_blacklist.count() - 3; i += 4) {
mtkleina2ef6422015-01-15 13:44:22 -08001006 if (match(FLAGS_blacklist[i+0], sink) &&
djsollen54416de2015-04-03 07:24:48 -07001007 match(FLAGS_blacklist[i+1], src) &&
1008 match(FLAGS_blacklist[i+2], srcOptions) &&
1009 match(FLAGS_blacklist[i+3], name)) {
mtklein3eed7dd2016-02-24 19:07:07 -08001010 return true;
mtkleina2ef6422015-01-15 13:44:22 -08001011 }
1012 }
mtklein3eed7dd2016-02-24 19:07:07 -08001013 return false;
mtkleina2ef6422015-01-15 13:44:22 -08001014}
1015
mtkleincd50bca2016-01-05 06:20:20 -08001016// Even when a Task Sink reports to be non-threadsafe (e.g. GPU), we know things like
1017// .png encoding are definitely thread safe. This lets us offload that work to CPU threads.
1018static SkTaskGroup gDefinitelyThreadSafeWork;
1019
mtklein748ca3b2015-01-15 10:56:12 -08001020// The finest-grained unit of work we can run: draw a single Src into a single Sink,
1021// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
1022struct Task {
mtkleine0effd62015-07-29 06:37:28 -07001023 Task(const TaggedSrc& src, const TaggedSink& sink) : src(src), sink(sink) {}
1024 const TaggedSrc& src;
1025 const TaggedSink& sink;
mtklein748ca3b2015-01-15 10:56:12 -08001026
mtklein21eaf3b2016-02-08 12:39:59 -08001027 static void Run(const Task& task) {
1028 SkString name = task.src->name();
mtkleine0effd62015-07-29 06:37:28 -07001029
mtkleinb9eb4ac2015-02-02 18:26:03 -08001030 SkString log;
mtklein3eed7dd2016-02-24 19:07:07 -08001031 if (!FLAGS_dryRun) {
mtklein748ca3b2015-01-15 10:56:12 -08001032 SkBitmap bitmap;
1033 SkDynamicMemoryWStream stream;
mtklein3eed7dd2016-02-24 19:07:07 -08001034 start(task.sink.tag.c_str(), task.src.tag.c_str(),
1035 task.src.options.c_str(), name.c_str());
mtklein21eaf3b2016-02-08 12:39:59 -08001036 Error err = task.sink->draw(*task.src, &bitmap, &stream, &log);
mtkleinb3b13b72016-03-07 13:20:52 -08001037 if (!log.isEmpty()) {
mtkleinc41fd922016-03-08 09:01:39 -08001038 info("%s %s %s %s:\n%s\n", task.sink.tag.c_str()
1039 , task.src.tag.c_str()
1040 , task.src.options.c_str()
1041 , name.c_str()
1042 , log.c_str());
mtkleinb3b13b72016-03-07 13:20:52 -08001043 }
mtklein748ca3b2015-01-15 10:56:12 -08001044 if (!err.isEmpty()) {
mtklein4089ef72015-03-05 08:40:28 -08001045 if (err.isFatal()) {
djsollen54416de2015-04-03 07:24:48 -07001046 fail(SkStringPrintf("%s %s %s %s: %s",
mtklein21eaf3b2016-02-08 12:39:59 -08001047 task.sink.tag.c_str(),
1048 task.src.tag.c_str(),
1049 task.src.options.c_str(),
mtklein4089ef72015-03-05 08:40:28 -08001050 name.c_str(),
1051 err.c_str()));
mtkleinb37cb412015-03-18 05:27:14 -07001052 } else {
mtklein3eed7dd2016-02-24 19:07:07 -08001053 done(task.sink.tag.c_str(), task.src.tag.c_str(),
1054 task.src.options.c_str(), name.c_str());
mtkleinba6ada72016-01-21 09:39:35 -08001055 return;
mtklein4089ef72015-03-05 08:40:28 -08001056 }
mtklein748ca3b2015-01-15 10:56:12 -08001057 }
mtklein62bd1a62015-01-27 14:46:26 -08001058
mtkleincd50bca2016-01-05 06:20:20 -08001059 // We're likely switching threads here, so we must capture by value, [=] or [foo,bar].
1060 SkStreamAsset* data = stream.detachAsStream();
1061 gDefinitelyThreadSafeWork.add([task,name,bitmap,data]{
1062 SkAutoTDelete<SkStreamAsset> ownedData(data);
1063
1064 // Why doesn't the copy constructor do this when we have pre-locked pixels?
1065 bitmap.lockPixels();
1066
1067 SkString md5;
1068 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
1069 SkMD5 hash;
1070 if (data->getLength()) {
1071 hash.writeStream(data, data->getLength());
1072 data->rewind();
mtklein38408462015-07-08 07:25:27 -07001073 } else {
mtkleincd50bca2016-01-05 06:20:20 -08001074 // If we're BGRA (Linux, Windows), swizzle over to RGBA (Mac, Android).
1075 // This helps eliminate multiple 0-pixel-diff hashes on gold.skia.org.
1076 // (Android's general slow speed breaks the tie arbitrarily in RGBA's favor.)
1077 // We might consider promoting 565 to RGBA too.
1078 if (bitmap.colorType() == kBGRA_8888_SkColorType) {
1079 SkBitmap swizzle;
1080 SkAssertResult(bitmap.copyTo(&swizzle, kRGBA_8888_SkColorType));
1081 hash.write(swizzle.getPixels(), swizzle.getSize());
1082 } else {
1083 hash.write(bitmap.getPixels(), bitmap.getSize());
1084 }
1085 }
1086 SkMD5::Digest digest;
1087 hash.finish(digest);
1088 for (int i = 0; i < 16; i++) {
1089 md5.appendf("%02x", digest.data[i]);
mtklein38408462015-07-08 07:25:27 -07001090 }
mtklein62bd1a62015-01-27 14:46:26 -08001091 }
mtklein62bd1a62015-01-27 14:46:26 -08001092
mtkleincd50bca2016-01-05 06:20:20 -08001093 if (!FLAGS_readPath.isEmpty() &&
mtklein3eed7dd2016-02-24 19:07:07 -08001094 !gGold.contains(Gold(task.sink.tag, task.src.tag,
1095 task.src.options, name, md5))) {
mtkleincd50bca2016-01-05 06:20:20 -08001096 fail(SkStringPrintf("%s not found for %s %s %s %s in %s",
1097 md5.c_str(),
mtklein21eaf3b2016-02-08 12:39:59 -08001098 task.sink.tag.c_str(),
1099 task.src.tag.c_str(),
1100 task.src.options.c_str(),
mtkleincd50bca2016-01-05 06:20:20 -08001101 name.c_str(),
1102 FLAGS_readPath[0]));
mtklein748ca3b2015-01-15 10:56:12 -08001103 }
mtkleincd50bca2016-01-05 06:20:20 -08001104
1105 if (!FLAGS_writePath.isEmpty()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001106 const char* ext = task.sink->fileExtension();
mtkleincd50bca2016-01-05 06:20:20 -08001107 if (data->getLength()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001108 WriteToDisk(task, md5, ext, data, data->getLength(), nullptr);
mtkleincd50bca2016-01-05 06:20:20 -08001109 SkASSERT(bitmap.drawsNothing());
1110 } else if (!bitmap.drawsNothing()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001111 WriteToDisk(task, md5, ext, nullptr, 0, &bitmap);
mtkleincd50bca2016-01-05 06:20:20 -08001112 }
1113 }
1114 });
mtklein748ca3b2015-01-15 10:56:12 -08001115 }
mtklein3eed7dd2016-02-24 19:07:07 -08001116 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 -08001117 }
1118
1119 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -08001120 SkString md5,
1121 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -08001122 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -08001123 const SkBitmap* bitmap) {
mtklein409d4702016-02-29 07:38:01 -08001124 bool gammaCorrect = false;
1125 if (bitmap) {
brianosman898235c2016-04-06 07:38:23 -07001126 gammaCorrect = SkImageInfoIsGammaCorrect(bitmap->info());
mtklein409d4702016-02-29 07:38:01 -08001127 }
1128
mtklein748ca3b2015-01-15 10:56:12 -08001129 JsonWriter::BitmapResult result;
djsollen54416de2015-04-03 07:24:48 -07001130 result.name = task.src->name();
mtklein3eed7dd2016-02-24 19:07:07 -08001131 result.config = task.sink.tag;
djsollen54416de2015-04-03 07:24:48 -07001132 result.sourceType = task.src.tag;
1133 result.sourceOptions = task.src.options;
1134 result.ext = ext;
mtklein409d4702016-02-29 07:38:01 -08001135 result.gammaCorrect = gammaCorrect;
djsollen54416de2015-04-03 07:24:48 -07001136 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -08001137 JsonWriter::AddBitmapResult(result);
1138
mtkleinb0531a72015-04-07 13:38:48 -07001139 // If an MD5 is uninteresting, we want it noted in the JSON file,
1140 // but don't want to dump it out as a .png (or whatever ext is).
1141 if (gUninterestingHashes.contains(md5)) {
1142 return;
1143 }
1144
mtklein748ca3b2015-01-15 10:56:12 -08001145 const char* dir = FLAGS_writePath[0];
1146 if (0 == strcmp(dir, "@")) { // Needed for iOS.
1147 dir = FLAGS_resourcePath[0];
1148 }
1149 sk_mkdir(dir);
1150
1151 SkString path;
1152 if (FLAGS_nameByHash) {
1153 path = SkOSPath::Join(dir, result.md5.c_str());
1154 path.append(".");
1155 path.append(ext);
1156 if (sk_exists(path.c_str())) {
1157 return; // Content-addressed. If it exists already, we're done.
1158 }
1159 } else {
kkinnunen3e980c32015-12-23 01:33:00 -08001160 path = SkOSPath::Join(dir, task.sink.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001161 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001162 path = SkOSPath::Join(path.c_str(), task.src.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001163 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001164 if (strcmp(task.src.options.c_str(), "") != 0) {
1165 path = SkOSPath::Join(path.c_str(), task.src.options.c_str());
djsollen54416de2015-04-03 07:24:48 -07001166 sk_mkdir(path.c_str());
1167 }
mtklein748ca3b2015-01-15 10:56:12 -08001168 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
1169 path.append(".");
1170 path.append(ext);
1171 }
1172
mtklein748ca3b2015-01-15 10:56:12 -08001173 if (bitmap) {
mtkleina5114d72015-08-24 13:27:01 -07001174 if (!dump_png(*bitmap, path.c_str(), result.md5.c_str())) {
mtklein748ca3b2015-01-15 10:56:12 -08001175 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
1176 return;
1177 }
1178 } else {
mtkleina5114d72015-08-24 13:27:01 -07001179 SkFILEWStream file(path.c_str());
1180 if (!file.isValid()) {
1181 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
1182 return;
1183 }
mtklein748ca3b2015-01-15 10:56:12 -08001184 if (!file.writeStream(data, len)) {
1185 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
1186 return;
1187 }
1188 }
1189 }
1190};
1191
mtklein748ca3b2015-01-15 10:56:12 -08001192/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1193
1194// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
1195
mtklein21eaf3b2016-02-08 12:39:59 -08001196static SkTDArray<skiatest::Test> gParallelTests, gSerialTests;
mtklein748ca3b2015-01-15 10:56:12 -08001197
1198static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -08001199 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -08001200 return;
1201 }
mtklein6393c062015-04-27 08:45:01 -07001202 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r; r = r->next()) {
1203 if (!in_shard()) {
1204 continue;
1205 }
halcanary87f3ba42015-01-20 09:30:20 -08001206 // Despite its name, factory() is returning a reference to
1207 // link-time static const POD data.
1208 const skiatest::Test& test = r->factory();
1209 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -08001210 continue;
1211 }
halcanary87f3ba42015-01-20 09:30:20 -08001212 if (test.needsGpu && gpu_supported()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001213 (FLAGS_gpu_threading ? gParallelTests : gSerialTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -08001214 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein21eaf3b2016-02-08 12:39:59 -08001215 gParallelTests.push(test);
mtklein82d28432015-01-15 12:46:02 -08001216 }
mtklein748ca3b2015-01-15 10:56:12 -08001217 }
1218}
1219
mtklein21eaf3b2016-02-08 12:39:59 -08001220static void run_test(skiatest::Test test) {
halcanary87f3ba42015-01-20 09:30:20 -08001221 struct : public skiatest::Reporter {
mtklein36352bf2015-03-25 18:17:31 -07001222 void reportFailed(const skiatest::Failure& failure) override {
halcanary87f3ba42015-01-20 09:30:20 -08001223 fail(failure.toString());
1224 JsonWriter::AddTestFailure(failure);
1225 }
mtklein36352bf2015-03-25 18:17:31 -07001226 bool allowExtendedTest() const override {
halcanary87f3ba42015-01-20 09:30:20 -08001227 return FLAGS_pathOpsExtended;
1228 }
mtklein36352bf2015-03-25 18:17:31 -07001229 bool verbose() const override { return FLAGS_veryVerbose; }
halcanary87f3ba42015-01-20 09:30:20 -08001230 } reporter;
djsollen824996a2015-06-12 12:06:22 -07001231
mtklein3eed7dd2016-02-24 19:07:07 -08001232 if (!FLAGS_dryRun && !is_blacklisted("_", "tests", "_", test.name)) {
mtklein21eaf3b2016-02-08 12:39:59 -08001233 start("unit", "test", "", test.name);
mtklein55e88b22015-01-21 15:50:13 -08001234 GrContextFactory factory;
mtklein21eaf3b2016-02-08 12:39:59 -08001235 test.proc(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -08001236 }
mtklein3eed7dd2016-02-24 19:07:07 -08001237 done("unit", "test", "", test.name);
mtklein748ca3b2015-01-15 10:56:12 -08001238}
1239
1240/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1241
mtkleinafae30a2016-02-24 12:28:32 -08001242DEFINE_int32(status_sec, 15, "Print status this often (and if we crash).");
1243
1244SkThread* start_status_thread() {
1245 auto thread = new SkThread([] (void*) {
1246 for (;;) {
1247 print_status();
1248 #if defined(SK_BUILD_FOR_WIN)
1249 Sleep(FLAGS_status_sec * 1000);
1250 #else
1251 sleep(FLAGS_status_sec);
1252 #endif
mtklein2e1c47e2015-03-12 07:16:56 -07001253 }
mtkleinafae30a2016-02-24 12:28:32 -08001254 });
1255 thread->start();
1256 return thread;
mtkleinde6fc2b2015-03-12 06:28:54 -07001257}
1258
caryclark83ca6282015-06-10 09:31:09 -07001259#define PORTABLE_FONT_PREFIX "Toy Liberation "
1260
mbocee6a9912016-05-31 11:42:36 -07001261static sk_sp<SkTypeface> create_from_name(const char familyName[], SkFontStyle style) {
caryclark83ca6282015-06-10 09:31:09 -07001262 if (familyName && strlen(familyName) > sizeof(PORTABLE_FONT_PREFIX)
1263 && !strncmp(familyName, PORTABLE_FONT_PREFIX, sizeof(PORTABLE_FONT_PREFIX) - 1)) {
caryclark1818acb2015-07-24 12:09:25 -07001264 return sk_tool_utils::create_portable_typeface(familyName, style);
caryclark83ca6282015-06-10 09:31:09 -07001265 }
halcanary96fcdcc2015-08-27 07:41:13 -07001266 return nullptr;
caryclark83ca6282015-06-10 09:31:09 -07001267}
1268
1269#undef PORTABLE_FONT_PREFIX
1270
mbocee6a9912016-05-31 11:42:36 -07001271extern sk_sp<SkTypeface> (*gCreateTypefaceDelegate)(const char [], SkFontStyle );
caryclark83ca6282015-06-10 09:31:09 -07001272
jcgregorio3b27ade2014-11-13 08:06:40 -08001273int dm_main();
caryclark17f0b6d2014-07-22 10:15:34 -07001274int dm_main() {
bsalomon71de3532016-04-26 14:27:21 -07001275 setbuf(stdout, nullptr);
mtklein246ba3a2016-02-23 10:39:36 -08001276 setup_crash_handler();
mtkleinafae30a2016-02-24 12:28:32 -08001277
liyuqian38911a72016-10-04 11:23:22 -07001278 if (FLAGS_analyticAA) {
1279 GlobalAAConfig::getInstance().fUseAnalyticAA = true;
1280 }
1281
mtkleina483da32016-03-17 12:53:36 -07001282 if (FLAGS_verbose) {
1283 gVLog = stderr;
1284 } else if (!FLAGS_writePath.isEmpty()) {
mtklein51c8cfc2016-03-11 12:59:09 -08001285 sk_mkdir(FLAGS_writePath[0]);
mtkleina1ce2162016-07-22 12:23:46 -07001286 gVLog = fopen(SkOSPath::Join(FLAGS_writePath[0], "verbose.log").c_str(), "w");
mtkleinc41fd922016-03-08 09:01:39 -08001287 }
1288
mtklein5286f022016-01-22 08:18:14 -08001289 JsonWriter::DumpJson(); // It's handy for the bots to assume this is ~never missing.
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +00001290 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -07001291 SkTaskGroup::Enabler enabled(FLAGS_threads);
caryclark83ca6282015-06-10 09:31:09 -07001292 gCreateTypefaceDelegate = &create_from_name;
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +00001293
halcanary7d571242016-02-24 17:59:16 -08001294 {
1295 SkString testResourcePath = GetResourcePath("color_wheel.png");
1296 SkFILEStream testResource(testResourcePath.c_str());
1297 if (!testResource.isValid()) {
mtkleinc41fd922016-03-08 09:01:39 -08001298 info("Some resources are missing. Do you need to set --resourcePath?\n");
halcanary7d571242016-02-24 17:59:16 -08001299 }
1300 }
mtklein62bd1a62015-01-27 14:46:26 -08001301 gather_gold();
borenet09ed4802015-04-03 14:15:33 -07001302 gather_uninteresting_hashes();
mtklein62bd1a62015-01-27 14:46:26 -08001303
scroggo86737142016-02-03 12:19:11 -08001304 if (!gather_srcs()) {
1305 return 1;
1306 }
brianosman05c987c2016-05-09 13:42:16 -07001307 if (!gather_sinks()) {
1308 return 1;
1309 }
mtklein748ca3b2015-01-15 10:56:12 -08001310 gather_tests();
mtklein21eaf3b2016-02-08 12:39:59 -08001311 gPending = gSrcs.count() * gSinks.count() + gParallelTests.count() + gSerialTests.count();
mtkleinc41fd922016-03-08 09:01:39 -08001312 info("%d srcs * %d sinks + %d tests == %d tasks",
1313 gSrcs.count(), gSinks.count(), gParallelTests.count() + gSerialTests.count(), gPending);
mtkleinafae30a2016-02-24 12:28:32 -08001314 SkAutoTDelete<SkThread> statusThread(start_status_thread());
mtklein748ca3b2015-01-15 10:56:12 -08001315
mtklein21eaf3b2016-02-08 12:39:59 -08001316 // Kick off as much parallel work as we can, making note of any serial work we'll need to do.
1317 SkTaskGroup parallel;
1318 SkTArray<Task> serial;
1319
1320 for (auto& sink : gSinks)
1321 for (auto& src : gSrcs) {
mtklein3eed7dd2016-02-24 19:07:07 -08001322 if (src->veto(sink->flags()) ||
1323 is_blacklisted(sink.tag.c_str(), src.tag.c_str(),
1324 src.options.c_str(), src->name().c_str())) {
mtkleinf10637f2016-06-10 13:56:35 -07001325 SkAutoMutexAcquire lock(gMutex);
mtklein3eed7dd2016-02-24 19:07:07 -08001326 gPending--;
1327 continue;
1328 }
1329
mtklein21eaf3b2016-02-08 12:39:59 -08001330 Task task(src, sink);
1331 if (src->serial() || sink->serial()) {
1332 serial.push_back(task);
1333 } else {
1334 parallel.add([task] { Task::Run(task); });
mtklein748ca3b2015-01-15 10:56:12 -08001335 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001336 }
mtklein21eaf3b2016-02-08 12:39:59 -08001337 for (auto test : gParallelTests) {
1338 parallel.add([test] { run_test(test); });
mtklein55e88b22015-01-21 15:50:13 -08001339 }
mtklein21eaf3b2016-02-08 12:39:59 -08001340
1341 // With the parallel work running, run serial tasks and tests here on main thread.
1342 for (auto task : serial) { Task::Run(task); }
1343 for (auto test : gSerialTests) { run_test(test); }
1344
1345 // Wait for any remaining parallel work to complete (including any spun off of serial tasks).
1346 parallel.wait();
mtkleincd50bca2016-01-05 06:20:20 -08001347 gDefinitelyThreadSafeWork.wait();
1348
mtkleinda884c42016-02-24 18:00:23 -08001349 // We'd better have run everything.
1350 SkASSERT(gPending == 0);
mtkleine027f172016-02-26 15:53:06 -08001351 // Make sure we've flushed all our results to disk.
1352 JsonWriter::DumpJson();
mtkleinda884c42016-02-24 18:00:23 -08001353
mtklein748ca3b2015-01-15 10:56:12 -08001354 // At this point we're back in single-threaded land.
caryclarkf53ce802015-06-15 06:48:30 -07001355 sk_tool_utils::release_portable_typefaces();
mtklein@google.comd36522d2013-10-16 13:02:15 +00001356
mtklein748ca3b2015-01-15 10:56:12 -08001357 if (gFailures.count() > 0) {
mtkleinc41fd922016-03-08 09:01:39 -08001358 info("Failures:\n");
mtklein748ca3b2015-01-15 10:56:12 -08001359 for (int i = 0; i < gFailures.count(); i++) {
mtkleinc41fd922016-03-08 09:01:39 -08001360 info("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001361 }
mtkleinc41fd922016-03-08 09:01:39 -08001362 info("%d failures\n", gFailures.count());
mtklein748ca3b2015-01-15 10:56:12 -08001363 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001364 }
mtkleinafae30a2016-02-24 12:28:32 -08001365
1366#ifdef SK_PDF_IMAGE_STATS
halcanary7a14b312015-10-01 07:28:13 -07001367 SkPDFImageDumpStats();
mtkleinafae30a2016-02-24 12:28:32 -08001368#endif // SK_PDF_IMAGE_STATS
1369
1370 print_status();
mtkleinc41fd922016-03-08 09:01:39 -08001371 info("Finished!\n");
mtklein748ca3b2015-01-15 10:56:12 -08001372 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +00001373}
jcgregorio3b27ade2014-11-13 08:06:40 -08001374
kkinnunen179a8f52015-11-20 13:32:24 -08001375// TODO: currently many GPU tests are declared outside SK_SUPPORT_GPU guards.
1376// Thus we export the empty RunWithGPUTestContexts when SK_SUPPORT_GPU=0.
1377namespace skiatest {
bsalomon758586c2016-04-06 14:02:39 -07001378
1379#if SK_SUPPORT_GPU
1380bool IsGLContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1381 return kOpenGL_GrBackend == GrContextFactory::ContextTypeBackend(type);
1382}
bsalomondc0fcd42016-04-11 14:21:33 -07001383bool IsVulkanContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1384 return kVulkan_GrBackend == GrContextFactory::ContextTypeBackend(type);
1385}
bsalomon758586c2016-04-06 14:02:39 -07001386bool IsRenderingGLContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1387 return IsGLContextType(type) && GrContextFactory::IsRenderingContext(type);
1388}
1389bool IsNullGLContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1390 return type == GrContextFactory::kNullGL_ContextType;
1391}
1392#else
1393bool IsGLContextType(int) { return false; }
bsalomondc0fcd42016-04-11 14:21:33 -07001394bool IsVulkanContextType(int) { return false; }
bsalomon758586c2016-04-06 14:02:39 -07001395bool IsRenderingGLContextType(int) { return false; }
1396bool IsNullGLContextType(int) { return false; }
1397#endif
1398
1399void RunWithGPUTestContexts(GrContextTestFn* test, GrContextTypeFilterFn* contextTypeFilter,
bsalomonf2f1c172016-04-05 12:59:06 -07001400 Reporter* reporter, GrContextFactory* factory) {
kkinnunen179a8f52015-11-20 13:32:24 -08001401#if SK_SUPPORT_GPU
kkinnunen3e980c32015-12-23 01:33:00 -08001402
bsalomon758586c2016-04-06 14:02:39 -07001403 for (int typeInt = 0; typeInt < GrContextFactory::kContextTypeCnt; ++typeInt) {
1404 GrContextFactory::ContextType contextType = (GrContextFactory::ContextType) typeInt;
1405 ContextInfo ctxInfo = factory->getContextInfo(contextType);
bsalomonfda88072016-04-11 14:40:50 -07001406 if (contextTypeFilter && !(*contextTypeFilter)(contextType)) {
kkinnunen179a8f52015-11-20 13:32:24 -08001407 continue;
1408 }
bsalomon758586c2016-04-06 14:02:39 -07001409 // Use "native" instead of explicitly trying OpenGL and OpenGL ES. Do not use GLES on,
1410 // desktop since tests do not account for not fixing http://skbug.com/2809
1411 if (contextType == GrContextFactory::kGL_ContextType ||
1412 contextType == GrContextFactory::kGLES_ContextType) {
1413 if (contextType != GrContextFactory::kNativeGL_ContextType) {
1414 continue;
1415 }
1416 }
bsalomon8b7451a2016-05-11 06:33:06 -07001417 if (ctxInfo.grContext()) {
bsalomonf2f1c172016-04-05 12:59:06 -07001418 (*test)(reporter, ctxInfo);
kkinnunen5219fd92015-12-10 06:28:13 -08001419 }
bsalomonf2f1c172016-04-05 12:59:06 -07001420 ctxInfo = factory->getContextInfo(contextType,
csmartdalton6270e552016-09-13 10:41:49 -07001421 GrContextFactory::ContextOptions::kEnableNVPR);
bsalomon8b7451a2016-05-11 06:33:06 -07001422 if (ctxInfo.grContext()) {
bsalomonf2f1c172016-04-05 12:59:06 -07001423 (*test)(reporter, ctxInfo);
kkinnunen179a8f52015-11-20 13:32:24 -08001424 }
1425 }
1426#endif
1427}
kkinnunen179a8f52015-11-20 13:32:24 -08001428} // namespace skiatest
1429
borenet48087572015-04-02 12:16:36 -07001430#if !defined(SK_BUILD_FOR_IOS)
jcgregorio3b27ade2014-11-13 08:06:40 -08001431int main(int argc, char** argv) {
1432 SkCommandLineFlags::Parse(argc, argv);
1433 return dm_main();
1434}
1435#endif