blob: 021b411ffe912c576eeb15e6ae4d5a8746fcd4ca [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"
Ben Wagnerbf111d72016-11-07 18:05:29 -050028#include "SkOSPath.h"
mtklein27c3fdd2016-02-26 14:43:21 -080029#include "SkPM4fPriv.h"
mtklein246ba3a2016-02-23 10:39:36 -080030#include "SkSpinlock.h"
mtkleina82f5622015-02-20 12:30:19 -080031#include "SkTHash.h"
mtklein406654b2014-09-03 15:34:37 -070032#include "SkTaskGroup.h"
mtkleinde6fc2b2015-03-12 06:28:54 -070033#include "SkThreadUtils.h"
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +000034#include "Test.h"
mtklein748ca3b2015-01-15 10:56:12 -080035#include "Timer.h"
brianosman3c579dc2016-04-19 09:18:11 -070036#include "picture_utils.h"
caryclark83ca6282015-06-10 09:31:09 -070037#include "sk_tool_utils.h"
liyuqian38911a72016-10-04 11:23:22 -070038#include "SkScan.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +000039
scroggo19b91532016-10-24 09:03:26 -070040#include <vector>
41
halcanary7a14b312015-10-01 07:28:13 -070042#ifdef SK_PDF_IMAGE_STATS
43extern void SkPDFImageDumpStats();
44#endif
45
mtkleina5114d72015-08-24 13:27:01 -070046#include "png.h"
47
bungeman60e0fee2015-08-26 05:15:46 -070048#include <stdlib.h>
49
scroggo27a58342015-10-28 08:56:41 -070050#ifndef SK_BUILD_FOR_WIN32
51 #include <unistd.h>
52#endif
53
djsollen54416de2015-04-03 07:24:48 -070054DEFINE_string(src, "tests gm skp image", "Source types to test.");
mtklein748ca3b2015-01-15 10:56:12 -080055DEFINE_bool(nameByHash, false,
56 "If true, write to FLAGS_writePath[0]/<hash>.png instead of "
djsollen54416de2015-04-03 07:24:48 -070057 "to FLAGS_writePath[0]/<config>/<sourceType>/<sourceOptions>/<name>.png");
mtklein748ca3b2015-01-15 10:56:12 -080058DEFINE_bool2(pathOpsExtended, x, false, "Run extended pathOps tests.");
mtkleind603b222015-02-17 11:13:33 -080059DEFINE_string(matrix, "1 0 0 1",
60 "2x2 scale+skew matrix to apply or upright when using "
61 "'matrix' or 'upright' in config.");
mtklein82d28432015-01-15 12:46:02 -080062DEFINE_bool(gpu_threading, false, "Allow GPU work to run on multiple threads?");
mtklein@google.comd36522d2013-10-16 13:02:15 +000063
mtkleina2ef6422015-01-15 13:44:22 -080064DEFINE_string(blacklist, "",
djsollen54416de2015-04-03 07:24:48 -070065 "Space-separated config/src/srcOptions/name quadruples to blacklist. '_' matches anything. E.g. \n"
66 "'--blacklist gpu skp _ _' will blacklist all SKPs drawn into the gpu config.\n"
67 "'--blacklist gpu skp _ _ 8888 gm _ aarects' will also blacklist the aarects GM on 8888.");
mtkleina2ef6422015-01-15 13:44:22 -080068
mtklein62bd1a62015-01-27 14:46:26 -080069DEFINE_string2(readPath, r, "", "If set check for equality with golden results in this directory.");
70
borenet09ed4802015-04-03 14:15:33 -070071DEFINE_string(uninterestingHashesFile, "",
72 "File containing a list of uninteresting hashes. If a result hashes to something in "
73 "this list, no image is written for that result.");
74
mtklein6393c062015-04-27 08:45:01 -070075DEFINE_int32(shards, 1, "We're splitting source data into this many shards.");
76DEFINE_int32(shard, 0, "Which shard do I run?");
77
halcanary45420a92016-06-02 12:41:14 -070078DEFINE_string(mskps, "", "Directory to read mskps from, or a single mskp file.");
79
mtklein748ca3b2015-01-15 10:56:12 -080080using namespace DM;
bsalomon3724e572016-03-30 18:56:19 -070081using sk_gpu_test::GrContextFactory;
bsalomon273c0f52016-03-31 10:59:06 -070082using sk_gpu_test::GLTestContext;
bsalomonf2f1c172016-04-05 12:59:06 -070083using sk_gpu_test::ContextInfo;
mtklein@google.comd36522d2013-10-16 13:02:15 +000084
mtklein748ca3b2015-01-15 10:56:12 -080085/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
86
benjaminwagnerec4d4d72016-03-25 12:59:53 -070087static const double kStartMs = SkTime::GetMSecs();
mtkleinc41fd922016-03-08 09:01:39 -080088
89static FILE* gVLog;
90
91template <typename... Args>
92static void vlog(const char* fmt, Args&&... args) {
93 if (gVLog) {
94 fprintf(gVLog, "%s\t", HumanizeMs(SkTime::GetMSecs() - kStartMs).c_str());
95 fprintf(gVLog, fmt, args...);
96 fflush(gVLog);
97 }
98}
99
100template <typename... Args>
101static void info(const char* fmt, Args&&... args) {
102 vlog(fmt, args...);
103 if (!FLAGS_quiet) {
104 printf(fmt, args...);
105 }
106}
107static void info(const char* fmt) {
108 if (!FLAGS_quiet) {
109 printf("%s", fmt); // Clang warns printf(fmt) is insecure.
110 }
111}
112
reed086eea92016-05-04 17:12:46 -0700113SK_DECLARE_STATIC_MUTEX(gFailuresMutex);
mtklein748ca3b2015-01-15 10:56:12 -0800114static SkTArray<SkString> gFailures;
115
mtklein3eed7dd2016-02-24 19:07:07 -0800116static void fail(const SkString& err) {
mtklein748ca3b2015-01-15 10:56:12 -0800117 SkAutoMutexAcquire lock(gFailuresMutex);
118 SkDebugf("\n\nFAILURE: %s\n\n", err.c_str());
119 gFailures.push_back(err);
halcanary9e398f72015-01-10 11:18:04 -0800120}
121
mtklein0f7748a2016-07-25 15:27:29 -0700122struct Running {
123 SkString id;
124 SkThreadID thread;
125
126 void dump() const {
127 info("\t%s\n", id.c_str());
128 }
129};
mtkleinb37cb412015-03-18 05:27:14 -0700130
mtklein246ba3a2016-02-23 10:39:36 -0800131// We use a spinlock to make locking this in a signal handler _somewhat_ safe.
mtklein15923c92016-02-29 10:14:38 -0800132static SkSpinlock gMutex;
mtklein0f7748a2016-07-25 15:27:29 -0700133static int32_t gPending;
134static SkTArray<Running> gRunning;
mtklein748ca3b2015-01-15 10:56:12 -0800135
mtklein3eed7dd2016-02-24 19:07:07 -0800136static void done(const char* config, const char* src, const char* srcOptions, const char* name) {
137 SkString id = SkStringPrintf("%s %s %s %s", config, src, srcOptions, name);
mtkleinc41fd922016-03-08 09:01:39 -0800138 vlog("done %s\n", id.c_str());
mtkleinafae30a2016-02-24 12:28:32 -0800139 int pending;
mtkleinb37cb412015-03-18 05:27:14 -0700140 {
mtkleinf10637f2016-06-10 13:56:35 -0700141 SkAutoMutexAcquire lock(gMutex);
mtkleinb37cb412015-03-18 05:27:14 -0700142 for (int i = 0; i < gRunning.count(); i++) {
mtklein0f7748a2016-07-25 15:27:29 -0700143 if (gRunning[i].id == id) {
mtkleinb37cb412015-03-18 05:27:14 -0700144 gRunning.removeShuffle(i);
145 break;
146 }
147 }
mtkleinafae30a2016-02-24 12:28:32 -0800148 pending = --gPending;
reed50bc0512015-05-19 14:13:31 -0700149 }
mtkleina17241b2015-01-23 05:48:00 -0800150 // We write our dm.json file every once in a while in case we crash.
151 // Notice this also handles the final dm.json when pending == 0.
152 if (pending % 500 == 0) {
153 JsonWriter::DumpJson();
154 }
mtklein@google.comd36522d2013-10-16 13:02:15 +0000155}
156
mtklein3eed7dd2016-02-24 19:07:07 -0800157static void start(const char* config, const char* src, const char* srcOptions, const char* name) {
158 SkString id = SkStringPrintf("%s %s %s %s", config, src, srcOptions, name);
mtkleinc41fd922016-03-08 09:01:39 -0800159 vlog("start %s\n", id.c_str());
mtkleinf10637f2016-06-10 13:56:35 -0700160 SkAutoMutexAcquire lock(gMutex);
mtklein0f7748a2016-07-25 15:27:29 -0700161 gRunning.push_back({id,SkGetThreadID()});
mtkleinb37cb412015-03-18 05:27:14 -0700162}
163
mtkleinafae30a2016-02-24 12:28:32 -0800164static void print_status() {
mtkleinafae30a2016-02-24 12:28:32 -0800165 int curr = sk_tools::getCurrResidentSetSizeMB(),
166 peak = sk_tools::getMaxResidentSetSizeMB();
mtkleinc41fd922016-03-08 09:01:39 -0800167 SkString elapsed = HumanizeMs(SkTime::GetMSecs() - kStartMs);
mtkleinafae30a2016-02-24 12:28:32 -0800168
mtkleinf10637f2016-06-10 13:56:35 -0700169 SkAutoMutexAcquire lock(gMutex);
mtkleinc41fd922016-03-08 09:01:39 -0800170 info("\n%s elapsed, %d active, %d queued, %dMB RAM, %dMB peak\n",
171 elapsed.c_str(), gRunning.count(), gPending - gRunning.count(), curr, peak);
mtkleinafae30a2016-02-24 12:28:32 -0800172 for (auto& task : gRunning) {
mtklein0f7748a2016-07-25 15:27:29 -0700173 task.dump();
mtkleinafae30a2016-02-24 12:28:32 -0800174 }
175}
176
Mike Klein3cc2d202016-10-24 11:39:43 -0400177static void find_culprit() {
178 // Assumes gMutex is locked.
179 SkThreadID thisThread = SkGetThreadID();
180 for (auto& task : gRunning) {
181 if (task.thread == thisThread) {
182 info("Likely culprit:\n");
183 task.dump();
mtklein0f7748a2016-07-25 15:27:29 -0700184 }
185 }
Mike Klein3cc2d202016-10-24 11:39:43 -0400186}
mtklein0f7748a2016-07-25 15:27:29 -0700187
mtklein246ba3a2016-02-23 10:39:36 -0800188#if defined(SK_BUILD_FOR_WIN32)
mtklein5b64dab2016-06-09 08:59:48 -0700189 static LONG WINAPI crash_handler(EXCEPTION_POINTERS* e) {
mtkleinf8557292016-02-29 06:35:28 -0800190 static const struct {
191 const char* name;
robertphillips75467862016-03-01 14:10:23 -0800192 DWORD code;
mtkleinf8557292016-02-29 06:35:28 -0800193 } kExceptions[] = {
194 #define _(E) {#E, E}
195 _(EXCEPTION_ACCESS_VIOLATION),
196 _(EXCEPTION_BREAKPOINT),
197 _(EXCEPTION_INT_DIVIDE_BY_ZERO),
198 _(EXCEPTION_STACK_OVERFLOW),
199 // TODO: more?
200 #undef _
201 };
202
mtkleinf10637f2016-06-10 13:56:35 -0700203 SkAutoMutexAcquire lock(gMutex);
mtklein5b64dab2016-06-09 08:59:48 -0700204
205 const DWORD code = e->ExceptionRecord->ExceptionCode;
206 info("\nCaught exception %u", code);
207 for (const auto& exception : kExceptions) {
208 if (exception.code == code) {
209 info(" %s", exception.name);
mtkleinf8557292016-02-29 06:35:28 -0800210 }
mtkleinf8557292016-02-29 06:35:28 -0800211 }
mtklein5b64dab2016-06-09 08:59:48 -0700212 info(", was running:\n");
213 for (auto& task : gRunning) {
mtklein0f7748a2016-07-25 15:27:29 -0700214 task.dump();
mtklein5b64dab2016-06-09 08:59:48 -0700215 }
mtklein0f7748a2016-07-25 15:27:29 -0700216 find_culprit();
mtklein5b64dab2016-06-09 08:59:48 -0700217 fflush(stdout);
218
mtkleinf8557292016-02-29 06:35:28 -0800219 // Execute default exception handler... hopefully, exit.
220 return EXCEPTION_EXECUTE_HANDLER;
mtklein246ba3a2016-02-23 10:39:36 -0800221 }
222
Mike Klein3cc2d202016-10-24 11:39:43 -0400223 static void setup_crash_handler() {
224 SetUnhandledExceptionFilter(crash_handler);
225 }
226#else
mtklein246ba3a2016-02-23 10:39:36 -0800227 #include <signal.h>
Mike Klein3cc2d202016-10-24 11:39:43 -0400228 #if !defined(SK_BUILD_FOR_ANDROID)
229 #include <execinfo.h>
230 #endif
231
232 static constexpr int max_of() { return 0; }
233 template <typename... Rest>
234 static constexpr int max_of(int x, Rest... rest) {
235 return x > max_of(rest...) ? x : max_of(rest...);
236 }
237
238 static void (*previous_handler[max_of(SIGABRT,SIGBUS,SIGFPE,SIGILL,SIGSEGV)+1])(int);
mtkleinf8557292016-02-29 06:35:28 -0800239
mtklein5b64dab2016-06-09 08:59:48 -0700240 static void crash_handler(int sig) {
mtkleinf10637f2016-06-10 13:56:35 -0700241 SkAutoMutexAcquire lock(gMutex);
mtklein5b64dab2016-06-09 08:59:48 -0700242
243 info("\nCaught signal %d [%s], was running:\n", sig, strsignal(sig));
244 for (auto& task : gRunning) {
mtklein0f7748a2016-07-25 15:27:29 -0700245 task.dump();
mtklein5b64dab2016-06-09 08:59:48 -0700246 }
mtklein0f7748a2016-07-25 15:27:29 -0700247 find_culprit();
mtklein5b64dab2016-06-09 08:59:48 -0700248
Mike Klein3cc2d202016-10-24 11:39:43 -0400249 #if !defined(SK_BUILD_FOR_ANDROID)
mtklein5b64dab2016-06-09 08:59:48 -0700250 void* stack[64];
251 int count = backtrace(stack, SK_ARRAY_COUNT(stack));
252 char** symbols = backtrace_symbols(stack, count);
253 info("\nStack trace:\n");
254 for (int i = 0; i < count; i++) {
255 info(" %s\n", symbols[i]);
256 }
Mike Klein3cc2d202016-10-24 11:39:43 -0400257 #endif
mtklein5b64dab2016-06-09 08:59:48 -0700258 fflush(stdout);
259
Mike Klein3cc2d202016-10-24 11:39:43 -0400260 signal(sig, previous_handler[sig]);
261 raise(sig);
mtklein5b64dab2016-06-09 08:59:48 -0700262 }
263
mtklein246ba3a2016-02-23 10:39:36 -0800264 static void setup_crash_handler() {
265 const int kSignals[] = { SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGSEGV };
266 for (int sig : kSignals) {
Mike Klein3cc2d202016-10-24 11:39:43 -0400267 previous_handler[sig] = signal(sig, crash_handler);
mtklein246ba3a2016-02-23 10:39:36 -0800268 }
269 }
270#endif
271
mtklein748ca3b2015-01-15 10:56:12 -0800272/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
mtklein114c3cd2015-01-15 10:15:02 -0800273
mtklein62bd1a62015-01-27 14:46:26 -0800274struct Gold : public SkString {
mtkleina82f5622015-02-20 12:30:19 -0800275 Gold() : SkString("") {}
mtklein3eed7dd2016-02-24 19:07:07 -0800276 Gold(const SkString& sink, const SkString& src,
277 const SkString& srcOptions, const SkString& name,
278 const SkString& md5)
mtklein62bd1a62015-01-27 14:46:26 -0800279 : SkString("") {
280 this->append(sink);
281 this->append(src);
djsollen54416de2015-04-03 07:24:48 -0700282 this->append(srcOptions);
mtklein62bd1a62015-01-27 14:46:26 -0800283 this->append(name);
284 this->append(md5);
mtklein62bd1a62015-01-27 14:46:26 -0800285 }
mtkleinc8d1dd42015-10-15 12:23:01 -0700286 struct Hash {
287 uint32_t operator()(const Gold& g) const {
288 return SkGoodHash()((const SkString&)g);
289 }
290 };
mtklein62bd1a62015-01-27 14:46:26 -0800291};
mtkleina82f5622015-02-20 12:30:19 -0800292static SkTHashSet<Gold, Gold::Hash> gGold;
mtklein62bd1a62015-01-27 14:46:26 -0800293
294static void add_gold(JsonWriter::BitmapResult r) {
djsollen54416de2015-04-03 07:24:48 -0700295 gGold.add(Gold(r.config, r.sourceType, r.sourceOptions, r.name, r.md5));
mtklein62bd1a62015-01-27 14:46:26 -0800296}
297
298static void gather_gold() {
299 if (!FLAGS_readPath.isEmpty()) {
300 SkString path(FLAGS_readPath[0]);
301 path.append("/dm.json");
302 if (!JsonWriter::ReadJson(path.c_str(), add_gold)) {
303 fail(SkStringPrintf("Couldn't read %s for golden results.", path.c_str()));
304 }
305 }
306}
307
308/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
309
mtklein935f1b12016-03-16 08:37:19 -0700310#if defined(SK_BUILD_FOR_WIN32)
311 static const char* kNewline = "\r\n";
312#else
313 static const char* kNewline = "\n";
314#endif
315
borenet09ed4802015-04-03 14:15:33 -0700316static SkTHashSet<SkString> gUninterestingHashes;
317
318static void gather_uninteresting_hashes() {
319 if (!FLAGS_uninterestingHashesFile.isEmpty()) {
bungeman38d909e2016-08-02 14:40:46 -0700320 sk_sp<SkData> data(SkData::MakeFromFileName(FLAGS_uninterestingHashesFile[0]));
mtkleincc334b32015-09-22 11:43:53 -0700321 if (!data) {
mtkleinc41fd922016-03-08 09:01:39 -0800322 info("WARNING: unable to read uninteresting hashes from %s\n",
323 FLAGS_uninterestingHashesFile[0]);
mtkleincc334b32015-09-22 11:43:53 -0700324 return;
325 }
borenet09ed4802015-04-03 14:15:33 -0700326 SkTArray<SkString> hashes;
mtklein935f1b12016-03-16 08:37:19 -0700327 SkStrSplit((const char*)data->data(), kNewline, &hashes);
borenet09ed4802015-04-03 14:15:33 -0700328 for (const SkString& hash : hashes) {
329 gUninterestingHashes.add(hash);
330 }
mtkleinc41fd922016-03-08 09:01:39 -0800331 info("FYI: loaded %d distinct uninteresting hashes from %d lines\n",
332 gUninterestingHashes.count(), hashes.count());
borenet09ed4802015-04-03 14:15:33 -0700333 }
334}
335
336/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
337
Ben Wagner145dbcd2016-11-03 14:40:50 -0400338struct TaggedSrc : public std::unique_ptr<Src> {
mtklein3eed7dd2016-02-24 19:07:07 -0800339 SkString tag;
340 SkString options;
mtkleine0effd62015-07-29 06:37:28 -0700341};
342
Ben Wagner145dbcd2016-11-03 14:40:50 -0400343struct TaggedSink : public std::unique_ptr<Sink> {
kkinnunen3e980c32015-12-23 01:33:00 -0800344 SkString tag;
djsollen54416de2015-04-03 07:24:48 -0700345};
mtklein748ca3b2015-01-15 10:56:12 -0800346
347static const bool kMemcpyOK = true;
348
mtkleine0effd62015-07-29 06:37:28 -0700349static SkTArray<TaggedSrc, kMemcpyOK> gSrcs;
350static SkTArray<TaggedSink, kMemcpyOK> gSinks;
mtklein748ca3b2015-01-15 10:56:12 -0800351
mtklein6393c062015-04-27 08:45:01 -0700352static bool in_shard() {
353 static int N = 0;
354 return N++ % FLAGS_shards == FLAGS_shard;
355}
356
mtklein3eed7dd2016-02-24 19:07:07 -0800357static void push_src(const char* tag, ImplicitString options, Src* s) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400358 std::unique_ptr<Src> src(s);
mtklein6393c062015-04-27 08:45:01 -0700359 if (in_shard() &&
mtklein3eed7dd2016-02-24 19:07:07 -0800360 FLAGS_src.contains(tag) &&
mtklein748ca3b2015-01-15 10:56:12 -0800361 !SkCommandLineFlags::ShouldSkip(FLAGS_match, src->name().c_str())) {
mtkleine0effd62015-07-29 06:37:28 -0700362 TaggedSrc& s = gSrcs.push_back();
mtklein18300a32016-03-16 13:53:35 -0700363 s.reset(src.release());
mtklein748ca3b2015-01-15 10:56:12 -0800364 s.tag = tag;
djsollen54416de2015-04-03 07:24:48 -0700365 s.options = options;
mtklein114c3cd2015-01-15 10:15:02 -0800366 }
mtklein748ca3b2015-01-15 10:56:12 -0800367}
mtklein114c3cd2015-01-15 10:15:02 -0800368
msarett9e707a02015-09-01 14:57:57 -0700369static void push_codec_src(Path path, CodecSrc::Mode mode, CodecSrc::DstColorType dstColorType,
scroggoc5560be2016-02-03 09:42:42 -0800370 SkAlphaType dstAlphaType, float scale) {
scroggoe4499842016-02-25 11:03:47 -0800371 if (FLAGS_simpleCodec) {
scroggo19b91532016-10-24 09:03:26 -0700372 const bool simple = CodecSrc::kCodec_Mode == mode || CodecSrc::kAnimated_Mode == mode;
373 if (!simple || dstColorType != CodecSrc::kGetFromCanvas_DstColorType || scale != 1.0f) {
scroggoe4499842016-02-25 11:03:47 -0800374 // Only decode in the simple case.
375 return;
scroggo19b91532016-10-24 09:03:26 -0700376 }
scroggoe4499842016-02-25 11:03:47 -0800377 }
msarett9e707a02015-09-01 14:57:57 -0700378 SkString folder;
379 switch (mode) {
380 case CodecSrc::kCodec_Mode:
381 folder.append("codec");
382 break;
msarettbb25b532016-01-13 09:31:39 -0800383 case CodecSrc::kCodecZeroInit_Mode:
384 folder.append("codec_zero_init");
385 break;
msarett9e707a02015-09-01 14:57:57 -0700386 case CodecSrc::kScanline_Mode:
387 folder.append("scanline");
388 break;
msarett9e707a02015-09-01 14:57:57 -0700389 case CodecSrc::kStripe_Mode:
390 folder.append("stripe");
391 break;
msarett91c22b22016-02-22 12:27:46 -0800392 case CodecSrc::kCroppedScanline_Mode:
393 folder.append("crop");
394 break;
msarett9e707a02015-09-01 14:57:57 -0700395 case CodecSrc::kSubset_Mode:
msarett36c37962015-09-02 13:20:52 -0700396 folder.append("codec_subset");
msarett9e707a02015-09-01 14:57:57 -0700397 break;
scroggo19b91532016-10-24 09:03:26 -0700398 case CodecSrc::kAnimated_Mode:
399 folder.append("codec_animated");
400 break;
msarett9e707a02015-09-01 14:57:57 -0700401 }
402
403 switch (dstColorType) {
404 case CodecSrc::kGrayscale_Always_DstColorType:
405 folder.append("_kGray8");
406 break;
407 case CodecSrc::kIndex8_Always_DstColorType:
408 folder.append("_kIndex8");
409 break;
msarett34e0ec42016-04-22 16:27:24 -0700410 case CodecSrc::kNonNative8888_Always_DstColorType:
411 folder.append("_kNonNative");
412 break;
msarett9e707a02015-09-01 14:57:57 -0700413 default:
414 break;
415 }
416
scroggoc5560be2016-02-03 09:42:42 -0800417 switch (dstAlphaType) {
scroggoc5560be2016-02-03 09:42:42 -0800418 case kPremul_SkAlphaType:
419 folder.append("_premul");
420 break;
421 case kUnpremul_SkAlphaType:
422 folder.append("_unpremul");
423 break;
424 default:
425 break;
426 }
427
msarett9e707a02015-09-01 14:57:57 -0700428 if (1.0f != scale) {
429 folder.appendf("_%.3f", scale);
430 }
431
scroggoc5560be2016-02-03 09:42:42 -0800432 CodecSrc* src = new CodecSrc(path, mode, dstColorType, dstAlphaType, scale);
msarett9e707a02015-09-01 14:57:57 -0700433 push_src("image", folder, src);
434}
435
scroggof8dc9df2016-05-16 09:04:13 -0700436static void push_android_codec_src(Path path, CodecSrc::DstColorType dstColorType,
437 SkAlphaType dstAlphaType, int sampleSize) {
msarett3d9d7a72015-10-21 10:27:10 -0700438 SkString folder;
scroggof8dc9df2016-05-16 09:04:13 -0700439 folder.append("scaled_codec");
msarett3d9d7a72015-10-21 10:27:10 -0700440
441 switch (dstColorType) {
442 case CodecSrc::kGrayscale_Always_DstColorType:
443 folder.append("_kGray8");
444 break;
445 case CodecSrc::kIndex8_Always_DstColorType:
446 folder.append("_kIndex8");
447 break;
msarett34e0ec42016-04-22 16:27:24 -0700448 case CodecSrc::kNonNative8888_Always_DstColorType:
449 folder.append("_kNonNative");
450 break;
msarett3d9d7a72015-10-21 10:27:10 -0700451 default:
452 break;
453 }
454
scroggoc5560be2016-02-03 09:42:42 -0800455 switch (dstAlphaType) {
scroggoc5560be2016-02-03 09:42:42 -0800456 case kPremul_SkAlphaType:
457 folder.append("_premul");
458 break;
msarett9e9444c2016-02-03 12:39:10 -0800459 case kUnpremul_SkAlphaType:
460 folder.append("_unpremul");
461 break;
scroggoc5560be2016-02-03 09:42:42 -0800462 default:
463 break;
464 }
465
msarett3d9d7a72015-10-21 10:27:10 -0700466 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800467 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700468 }
469
scroggof8dc9df2016-05-16 09:04:13 -0700470 AndroidCodecSrc* src = new AndroidCodecSrc(path, dstColorType, dstAlphaType, sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700471 push_src("image", folder, src);
472}
473
msarett18976312016-03-09 14:20:58 -0800474static void push_image_gen_src(Path path, ImageGenSrc::Mode mode, SkAlphaType alphaType, bool isGpu)
475{
476 SkString folder;
477 switch (mode) {
478 case ImageGenSrc::kCodec_Mode:
479 folder.append("gen_codec");
480 break;
481 case ImageGenSrc::kPlatform_Mode:
482 folder.append("gen_platform");
483 break;
484 }
485
486 if (isGpu) {
487 folder.append("_gpu");
488 } else {
489 switch (alphaType) {
490 case kOpaque_SkAlphaType:
491 folder.append("_opaque");
492 break;
493 case kPremul_SkAlphaType:
494 folder.append("_premul");
495 break;
496 case kUnpremul_SkAlphaType:
497 folder.append("_unpremul");
498 break;
499 default:
500 break;
501 }
502 }
503
504 ImageGenSrc* src = new ImageGenSrc(path, mode, alphaType, isGpu);
505 push_src("image", folder, src);
506}
507
msarett438b2ad2015-04-09 12:43:10 -0700508static void push_codec_srcs(Path path) {
bungeman38d909e2016-08-02 14:40:46 -0700509 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
msarett438b2ad2015-04-09 12:43:10 -0700510 if (!encoded) {
mtkleinc41fd922016-03-08 09:01:39 -0800511 info("Couldn't read %s.", path.c_str());
msarett438b2ad2015-04-09 12:43:10 -0700512 return;
513 }
Ben Wagner145dbcd2016-11-03 14:40:50 -0400514 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(encoded));
halcanary96fcdcc2015-08-27 07:41:13 -0700515 if (nullptr == codec.get()) {
mtkleinc41fd922016-03-08 09:01:39 -0800516 info("Couldn't create codec for %s.", path.c_str());
msarett438b2ad2015-04-09 12:43:10 -0700517 return;
518 }
519
scroggo9366aff2016-05-20 08:38:54 -0700520 // native scaling is only supported by WEBP and JPEG
521 bool supportsNativeScaling = false;
msarett438b2ad2015-04-09 12:43:10 -0700522
msarett91c22b22016-02-22 12:27:46 -0800523 SkTArray<CodecSrc::Mode> nativeModes;
524 nativeModes.push_back(CodecSrc::kCodec_Mode);
525 nativeModes.push_back(CodecSrc::kCodecZeroInit_Mode);
msarett91c22b22016-02-22 12:27:46 -0800526 switch (codec->getEncodedFormat()) {
Hal Canarydb683012016-11-23 08:55:18 -0700527 case SkEncodedImageFormat::kJPEG:
msarett91c22b22016-02-22 12:27:46 -0800528 nativeModes.push_back(CodecSrc::kScanline_Mode);
529 nativeModes.push_back(CodecSrc::kStripe_Mode);
530 nativeModes.push_back(CodecSrc::kCroppedScanline_Mode);
scroggo9366aff2016-05-20 08:38:54 -0700531 supportsNativeScaling = true;
msarett91c22b22016-02-22 12:27:46 -0800532 break;
Hal Canarydb683012016-11-23 08:55:18 -0700533 case SkEncodedImageFormat::kWEBP:
msarett91c22b22016-02-22 12:27:46 -0800534 nativeModes.push_back(CodecSrc::kSubset_Mode);
scroggo9366aff2016-05-20 08:38:54 -0700535 supportsNativeScaling = true;
msarett91c22b22016-02-22 12:27:46 -0800536 break;
Hal Canarydb683012016-11-23 08:55:18 -0700537 case SkEncodedImageFormat::kDNG:
msarettb65e6042016-02-23 05:37:25 -0800538 break;
msarett91c22b22016-02-22 12:27:46 -0800539 default:
540 nativeModes.push_back(CodecSrc::kScanline_Mode);
msarett91c22b22016-02-22 12:27:46 -0800541 break;
542 }
msarett9e707a02015-09-01 14:57:57 -0700543
msarett91c22b22016-02-22 12:27:46 -0800544 SkTArray<CodecSrc::DstColorType> colorTypes;
545 colorTypes.push_back(CodecSrc::kGetFromCanvas_DstColorType);
msarett34e0ec42016-04-22 16:27:24 -0700546 colorTypes.push_back(CodecSrc::kNonNative8888_Always_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700547 switch (codec->getInfo().colorType()) {
548 case kGray_8_SkColorType:
msarett91c22b22016-02-22 12:27:46 -0800549 colorTypes.push_back(CodecSrc::kGrayscale_Always_DstColorType);
Hal Canarydb683012016-11-23 08:55:18 -0700550 if (SkEncodedImageFormat::kWBMP == codec->getEncodedFormat()) {
msarett91c22b22016-02-22 12:27:46 -0800551 colorTypes.push_back(CodecSrc::kIndex8_Always_DstColorType);
msarett55f7bdd2016-02-16 13:24:54 -0800552 }
msarett36c37962015-09-02 13:20:52 -0700553 break;
554 case kIndex_8_SkColorType:
msarett91c22b22016-02-22 12:27:46 -0800555 colorTypes.push_back(CodecSrc::kIndex8_Always_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700556 break;
557 default:
msarett36c37962015-09-02 13:20:52 -0700558 break;
559 }
msarett9e707a02015-09-01 14:57:57 -0700560
scroggoc5560be2016-02-03 09:42:42 -0800561 SkTArray<SkAlphaType> alphaModes;
562 alphaModes.push_back(kPremul_SkAlphaType);
scroggof2c96a22016-05-20 11:08:27 -0700563 if (codec->getInfo().alphaType() != kOpaque_SkAlphaType) {
564 alphaModes.push_back(kUnpremul_SkAlphaType);
scroggoc5560be2016-02-03 09:42:42 -0800565 }
msarettb714fb02016-01-22 14:46:42 -0800566
567 for (CodecSrc::Mode mode : nativeModes) {
scroggo9366aff2016-05-20 08:38:54 -0700568 for (CodecSrc::DstColorType colorType : colorTypes) {
569 for (SkAlphaType alphaType : alphaModes) {
scroggof2c96a22016-05-20 11:08:27 -0700570 // Only test kCroppedScanline_Mode when the alpha type is premul. The test is
scroggo9366aff2016-05-20 08:38:54 -0700571 // slow and won't be interestingly different with different alpha types.
572 if (CodecSrc::kCroppedScanline_Mode == mode &&
scroggof2c96a22016-05-20 11:08:27 -0700573 kPremul_SkAlphaType != alphaType) {
scroggo9366aff2016-05-20 08:38:54 -0700574 continue;
575 }
msarett91c22b22016-02-22 12:27:46 -0800576
scroggo9366aff2016-05-20 08:38:54 -0700577 push_codec_src(path, mode, colorType, alphaType, 1.0f);
msarettf3dc1882016-04-26 13:06:38 -0700578
scroggo9366aff2016-05-20 08:38:54 -0700579 // Skip kNonNative on different native scales. It won't be interestingly
580 // different.
581 if (supportsNativeScaling &&
582 CodecSrc::kNonNative8888_Always_DstColorType == colorType) {
583 // Native Scales
584 // SkJpegCodec natively supports scaling to the following:
585 for (auto scale : { 0.125f, 0.25f, 0.375f, 0.5f, 0.625f, 0.750f, 0.875f }) {
586 push_codec_src(path, mode, colorType, alphaType, scale);
587 }
scroggoc5560be2016-02-03 09:42:42 -0800588 }
msarett9e707a02015-09-01 14:57:57 -0700589 }
590 }
msarett0a242972015-06-11 14:27:27 -0700591 }
msarett36c37962015-09-02 13:20:52 -0700592
scroggo19b91532016-10-24 09:03:26 -0700593 {
594 std::vector<SkCodec::FrameInfo> frameInfos = codec->getFrameInfo();
595 if (frameInfos.size() > 1) {
596 push_codec_src(path, CodecSrc::kAnimated_Mode, CodecSrc::kGetFromCanvas_DstColorType,
597 kPremul_SkAlphaType, 1.0f);
598 }
599
600 }
601
scroggoe4499842016-02-25 11:03:47 -0800602 if (FLAGS_simpleCodec) {
603 return;
604 }
605
msarett3d9d7a72015-10-21 10:27:10 -0700606 const int sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
msarett36c37962015-09-02 13:20:52 -0700607
msarett3d9d7a72015-10-21 10:27:10 -0700608 for (int sampleSize : sampleSizes) {
msarett91c22b22016-02-22 12:27:46 -0800609 for (CodecSrc::DstColorType colorType : colorTypes) {
scroggoc5560be2016-02-03 09:42:42 -0800610 for (SkAlphaType alphaType : alphaModes) {
msarettf3dc1882016-04-26 13:06:38 -0700611 // We can exercise all of the kNonNative support code in the swizzler with just a
612 // few sample sizes. Skip the rest.
613 if (CodecSrc::kNonNative8888_Always_DstColorType == colorType && sampleSize > 3) {
614 continue;
615 }
616
scroggof8dc9df2016-05-16 09:04:13 -0700617 push_android_codec_src(path, colorType, alphaType, sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700618 }
msarett36c37962015-09-02 13:20:52 -0700619 }
620 }
msarett18976312016-03-09 14:20:58 -0800621
622 static const char* const rawExts[] = {
623 "arw", "cr2", "dng", "nef", "nrw", "orf", "raf", "rw2", "pef", "srw",
624 "ARW", "CR2", "DNG", "NEF", "NRW", "ORF", "RAF", "RW2", "PEF", "SRW",
625 };
626
627 // There is not currently a reason to test RAW images on image generator.
628 // If we want to enable these tests, we will need to fix skbug.com/5079.
629 for (const char* ext : rawExts) {
630 if (path.endsWith(ext)) {
631 return;
632 }
633 }
634
635 // Push image generator GPU test.
brianosmanc9ced392016-03-24 07:27:43 -0700636 push_image_gen_src(path, ImageGenSrc::kCodec_Mode, codec->getInfo().alphaType(), true);
msarett18976312016-03-09 14:20:58 -0800637
638 // Push image generator CPU tests.
639 for (SkAlphaType alphaType : alphaModes) {
640 push_image_gen_src(path, ImageGenSrc::kCodec_Mode, alphaType, false);
641
642#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
Hal Canarydb683012016-11-23 08:55:18 -0700643 if (SkEncodedImageFormat::kWEBP != codec->getEncodedFormat() &&
644 SkEncodedImageFormat::kWBMP != codec->getEncodedFormat() &&
msarett18976312016-03-09 14:20:58 -0800645 kUnpremul_SkAlphaType != alphaType)
646 {
647 push_image_gen_src(path, ImageGenSrc::kPlatform_Mode, alphaType, false);
648 }
msarettfc0b6d12016-03-17 13:50:17 -0700649#elif defined(SK_BUILD_FOR_WIN)
Hal Canarydb683012016-11-23 08:55:18 -0700650 if (SkEncodedImageFormat::kWEBP != codec->getEncodedFormat() &&
651 SkEncodedImageFormat::kWBMP != codec->getEncodedFormat())
msarettfc0b6d12016-03-17 13:50:17 -0700652 {
653 push_image_gen_src(path, ImageGenSrc::kPlatform_Mode, alphaType, false);
654 }
msarett18976312016-03-09 14:20:58 -0800655#endif
656 }
msarett438b2ad2015-04-09 12:43:10 -0700657}
658
msarettd1227a72016-05-18 06:23:57 -0700659static void push_brd_src(Path path, CodecSrc::DstColorType dstColorType, BRDSrc::Mode mode,
660 uint32_t sampleSize) {
661 SkString folder("brd_android_codec");
msaretta5783ae2015-09-08 15:35:32 -0700662 switch (mode) {
663 case BRDSrc::kFullImage_Mode:
664 break;
665 case BRDSrc::kDivisor_Mode:
666 folder.append("_divisor");
667 break;
668 default:
669 SkASSERT(false);
670 return;
671 }
672
673 switch (dstColorType) {
674 case CodecSrc::kGetFromCanvas_DstColorType:
675 break;
676 case CodecSrc::kIndex8_Always_DstColorType:
677 folder.append("_kIndex");
678 break;
679 case CodecSrc::kGrayscale_Always_DstColorType:
680 folder.append("_kGray");
681 break;
682 default:
683 SkASSERT(false);
684 return;
685 }
686
687 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800688 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700689 }
690
msarettd1227a72016-05-18 06:23:57 -0700691 BRDSrc* src = new BRDSrc(path, mode, dstColorType, sampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700692 push_src("image", folder, src);
693}
694
695static void push_brd_srcs(Path path) {
scroggo24a9bd72016-05-20 06:02:42 -0700696 // Only run Index8 and grayscale to one sampleSize and Mode. Though interesting
697 // to test these color types, they should not reveal anything across various
698 // sampleSizes and Modes
699 for (auto type : { CodecSrc::kIndex8_Always_DstColorType,
700 CodecSrc::kGrayscale_Always_DstColorType }) {
701 // Arbitrarily choose Mode and sampleSize.
702 push_brd_src(path, type, BRDSrc::kFullImage_Mode, 2);
703 }
704
705
scroggo501b7342015-11-03 07:55:11 -0800706 // Test on a variety of sampleSizes, making sure to include:
707 // - 2, 4, and 8, which are natively supported by jpeg
708 // - multiples of 2 which are not divisible by 4 (analogous for 4)
709 // - larger powers of two, since BRD clients generally use powers of 2
710 // We will only produce output for the larger sizes on large images.
711 const uint32_t sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 24, 32, 64 };
msarett6efbe052015-09-11 09:01:16 -0700712
msaretta5783ae2015-09-08 15:35:32 -0700713 const BRDSrc::Mode modes[] = {
msarett26ad17b2015-10-22 07:29:19 -0700714 BRDSrc::kFullImage_Mode,
715 BRDSrc::kDivisor_Mode,
msaretta5783ae2015-09-08 15:35:32 -0700716 };
717
msarettd1227a72016-05-18 06:23:57 -0700718 for (uint32_t sampleSize : sampleSizes) {
scroggo24a9bd72016-05-20 06:02:42 -0700719 for (BRDSrc::Mode mode : modes) {
720 push_brd_src(path, CodecSrc::kGetFromCanvas_DstColorType, mode, sampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700721 }
722 }
723}
724
725static bool brd_supported(const char* ext) {
msarett8c8f22a2015-04-01 06:58:48 -0700726 static const char* const exts[] = {
msaretta5783ae2015-09-08 15:35:32 -0700727 "jpg", "jpeg", "png", "webp",
728 "JPG", "JPEG", "PNG", "WEBP",
msarett8c8f22a2015-04-01 06:58:48 -0700729 };
730
731 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
732 if (0 == strcmp(exts[i], ext)) {
733 return true;
734 }
735 }
736 return false;
scroggo9b77ddd2015-03-19 06:03:39 -0700737}
738
fmalitaa2b9fdf2016-08-03 19:53:36 -0700739template <typename T>
740void gather_file_srcs(const SkCommandLineFlags::StringArray& flags, const char* ext) {
741 for (int i = 0; i < flags.count(); i++) {
742 const char* path = flags[i];
743 if (sk_isdir(path)) {
744 SkOSFile::Iter it(path, ext);
745 for (SkString file; it.next(&file); ) {
746 push_src(ext, "", new T(SkOSPath::Join(path, file.c_str())));
747 }
748 } else {
749 push_src(ext, "", new T(path));
750 }
751 }
752}
753
scroggo86737142016-02-03 12:19:11 -0800754static bool gather_srcs() {
mtklein748ca3b2015-01-15 10:56:12 -0800755 for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) {
djsollen54416de2015-04-03 07:24:48 -0700756 push_src("gm", "", new GMSrc(r->factory()));
mtklein748ca3b2015-01-15 10:56:12 -0800757 }
scroggo86737142016-02-03 12:19:11 -0800758
fmalitaa2b9fdf2016-08-03 19:53:36 -0700759 gather_file_srcs<SKPSrc>(FLAGS_skps, "skp");
760 gather_file_srcs<MSKPSrc>(FLAGS_mskps, "mskp");
761#if defined(SK_XML)
762 gather_file_srcs<SVGSrc>(FLAGS_svgs, "svg");
763#endif
halcanary45420a92016-06-02 12:41:14 -0700764
scroggo86737142016-02-03 12:19:11 -0800765 SkTArray<SkString> images;
msarett69deca82016-04-29 09:38:40 -0700766 if (!CollectImages(FLAGS_images, &images)) {
scroggo86737142016-02-03 12:19:11 -0800767 return false;
768 }
769
770 for (auto image : images) {
771 push_codec_srcs(image);
scroggoe4499842016-02-25 11:03:47 -0800772 if (FLAGS_simpleCodec) {
773 continue;
774 }
775
mtklein21eaf3b2016-02-08 12:39:59 -0800776 const char* ext = strrchr(image.c_str(), '.');
777 if (ext && brd_supported(ext+1)) {
scroggo86737142016-02-03 12:19:11 -0800778 push_brd_srcs(image);
mtklein709d2c32015-01-15 08:30:25 -0800779 }
mtklein709d2c32015-01-15 08:30:25 -0800780 }
scroggo86737142016-02-03 12:19:11 -0800781
msarett69deca82016-04-29 09:38:40 -0700782 SkTArray<SkString> colorImages;
783 if (!CollectImages(FLAGS_colorImages, &colorImages)) {
784 return false;
785 }
786
787 for (auto colorImage : colorImages) {
msarett9ce3a542016-07-15 13:54:38 -0700788 ColorCodecSrc* src = new ColorCodecSrc(colorImage, ColorCodecSrc::kBaseline_Mode,
789 kN32_SkColorType);
msarettb8d1aac2016-08-04 12:22:06 -0700790 push_src("colorImage", "color_codec_baseline", src);
msarett888dc162016-05-23 10:21:17 -0700791
msarett9ce3a542016-07-15 13:54:38 -0700792 src = new ColorCodecSrc(colorImage, ColorCodecSrc::kDst_HPZR30w_Mode, kN32_SkColorType);
msarettb8d1aac2016-08-04 12:22:06 -0700793 push_src("colorImage", "color_codec_HPZR30w", src);
msarett9ce3a542016-07-15 13:54:38 -0700794 // TODO (msarett):
795 // 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 -0700796
msarett9ce3a542016-07-15 13:54:38 -0700797 src = new ColorCodecSrc(colorImage, ColorCodecSrc::kDst_sRGB_Mode, kN32_SkColorType);
msarettb8d1aac2016-08-04 12:22:06 -0700798 push_src("colorImage", "color_codec_sRGB_kN32", src);
msarett9ce3a542016-07-15 13:54:38 -0700799 src = new ColorCodecSrc(colorImage, ColorCodecSrc::kDst_sRGB_Mode, kRGBA_F16_SkColorType);
msarettb8d1aac2016-08-04 12:22:06 -0700800 push_src("colorImage", "color_codec_sRGB_kF16", src);
msarett69deca82016-04-29 09:38:40 -0700801 }
802
scroggo86737142016-02-03 12:19:11 -0800803 return true;
mtklein709d2c32015-01-15 08:30:25 -0800804}
805
kkinnunen3e980c32015-12-23 01:33:00 -0800806static void push_sink(const SkCommandLineConfig& config, Sink* s) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400807 std::unique_ptr<Sink> sink(s);
kkinnunen3e980c32015-12-23 01:33:00 -0800808
mtkleine0effd62015-07-29 06:37:28 -0700809 // Try a simple Src as a canary. If it fails, skip this sink.
mtklein748ca3b2015-01-15 10:56:12 -0800810 struct : public Src {
mtkleine0effd62015-07-29 06:37:28 -0700811 Error draw(SkCanvas* c) const override {
812 c->drawRect(SkRect::MakeWH(1,1), SkPaint());
813 return "";
814 }
mtklein36352bf2015-03-25 18:17:31 -0700815 SkISize size() const override { return SkISize::Make(16, 16); }
mtkleine0effd62015-07-29 06:37:28 -0700816 Name name() const override { return "justOneRect"; }
817 } justOneRect;
mtklein114c3cd2015-01-15 10:15:02 -0800818
mtklein748ca3b2015-01-15 10:56:12 -0800819 SkBitmap bitmap;
820 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800821 SkString log;
mtkleine0effd62015-07-29 06:37:28 -0700822 Error err = sink->draw(justOneRect, &bitmap, &stream, &log);
mtklein4089ef72015-03-05 08:40:28 -0800823 if (err.isFatal()) {
mtkleinc41fd922016-03-08 09:01:39 -0800824 info("Could not run %s: %s\n", config.getTag().c_str(), err.c_str());
mtklein05641a52015-04-21 10:49:13 -0700825 exit(1);
mtklein114c3cd2015-01-15 10:15:02 -0800826 }
827
mtkleine0effd62015-07-29 06:37:28 -0700828 TaggedSink& ts = gSinks.push_back();
mtklein18300a32016-03-16 13:53:35 -0700829 ts.reset(sink.release());
kkinnunen3e980c32015-12-23 01:33:00 -0800830 ts.tag = config.getTag();
mtklein748ca3b2015-01-15 10:56:12 -0800831}
832
833static bool gpu_supported() {
834#if SK_SUPPORT_GPU
835 return FLAGS_gpu;
836#else
837 return false;
838#endif
839}
kkinnunen9ebc3f02015-12-21 23:48:13 -0800840
kkinnunen3e980c32015-12-23 01:33:00 -0800841static Sink* create_sink(const SkCommandLineConfig* config) {
842#if SK_SUPPORT_GPU
843 if (gpu_supported()) {
844 if (const SkCommandLineConfigGpu* gpuConfig = config->asConfigGpu()) {
bsalomon85b4b532016-04-05 11:06:27 -0700845 GrContextFactory::ContextType contextType = gpuConfig->getContextType();
csmartdalton6270e552016-09-13 10:41:49 -0700846 GrContextFactory::ContextOptions contextOptions = gpuConfig->getContextOptions();
kkinnunen3e980c32015-12-23 01:33:00 -0800847 GrContextFactory testFactory;
848 if (!testFactory.get(contextType, contextOptions)) {
mtkleinc41fd922016-03-08 09:01:39 -0800849 info("WARNING: can not create GPU context for config '%s'. "
850 "GM tests will be skipped.\n", gpuConfig->getTag().c_str());
kkinnunen3e980c32015-12-23 01:33:00 -0800851 return nullptr;
852 }
853 return new GPUSink(contextType, contextOptions, gpuConfig->getSamples(),
brianosmand93c1202016-03-10 07:49:08 -0800854 gpuConfig->getUseDIText(), gpuConfig->getColorType(),
brianosmanb109b8c2016-06-16 13:03:24 -0700855 sk_ref_sp(gpuConfig->getColorSpace()), FLAGS_gpu_threading);
kkinnunen3e980c32015-12-23 01:33:00 -0800856 }
857 }
858#endif
859
860#define SINK(t, sink, ...) if (config->getBackend().equals(t)) { return new sink(__VA_ARGS__); }
861
tomhudsoneebc39a2015-02-23 12:18:05 -0800862#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
863 SINK("hwui", HWUISink);
864#endif
865
mtklein748ca3b2015-01-15 10:56:12 -0800866 if (FLAGS_cpu) {
Brian Osman526972e2016-10-24 09:24:02 -0400867 auto srgbColorSpace = SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named);
868 auto srgbLinearColorSpace = SkColorSpace::MakeNamed(SkColorSpace::kSRGBLinear_Named);
brianosmanb109b8c2016-06-16 13:03:24 -0700869
mtklein748ca3b2015-01-15 10:56:12 -0800870 SINK("565", RasterSink, kRGB_565_SkColorType);
871 SINK("8888", RasterSink, kN32_SkColorType);
brianosmanb109b8c2016-06-16 13:03:24 -0700872 SINK("srgb", RasterSink, kN32_SkColorType, srgbColorSpace);
raftias7c602de2016-10-13 10:45:44 -0700873 SINK("f16", RasterSink, kRGBA_F16_SkColorType, srgbLinearColorSpace);
halcanary94b87bd2016-04-04 06:12:15 -0700874 SINK("pdf", PDFSink);
mtklein9c3f17d2015-01-28 11:35:18 -0800875 SINK("skp", SKPSink);
reed54dc4872016-09-13 08:09:45 -0700876 SINK("pipe", PipeSink);
mtklein8a4527e2015-01-31 20:00:58 -0800877 SINK("svg", SVGSink);
878 SINK("null", NullSink);
halcanary47ef4d52015-03-03 09:13:09 -0800879 SINK("xps", XPSSink);
halcanary4b656662016-04-27 07:45:18 -0700880 SINK("pdfa", PDFSink, true);
Hal Canary85c7fe82016-10-25 10:33:27 -0400881 SINK("jsdebug", DebugSink);
mtklein748ca3b2015-01-15 10:56:12 -0800882 }
883#undef SINK
halcanary96fcdcc2015-08-27 07:41:13 -0700884 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800885}
886
kkinnunen3e980c32015-12-23 01:33:00 -0800887static Sink* create_via(const SkString& tag, Sink* wrapped) {
888#define VIA(t, via, ...) if (tag.equals(t)) { return new via(__VA_ARGS__); }
mtklein9c5052f2016-08-06 12:51:51 -0700889 VIA("lite", ViaLite, wrapped);
reed54dc4872016-09-13 08:09:45 -0700890 VIA("pipe", ViaPipe, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700891 VIA("twice", ViaTwice, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700892 VIA("serialize", ViaSerialization, wrapped);
mtklein4a34ecb2016-01-08 10:19:35 -0800893 VIA("pic", ViaPicture, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700894 VIA("2ndpic", ViaSecondPicture, wrapped);
mtkleind31c13d2015-05-05 12:59:56 -0700895 VIA("sp", ViaSingletonPictures, wrapped);
reedbabc3de2016-07-08 08:43:27 -0700896 VIA("defer", ViaDefer, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700897 VIA("tiles", ViaTiles, 256, 256, nullptr, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800898 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800899
mtkleind603b222015-02-17 11:13:33 -0800900 if (FLAGS_matrix.count() == 4) {
mtklein748ca3b2015-01-15 10:56:12 -0800901 SkMatrix m;
mtkleind603b222015-02-17 11:13:33 -0800902 m.reset();
903 m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
904 m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
905 m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
906 m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
907 VIA("matrix", ViaMatrix, m, wrapped);
908 VIA("upright", ViaUpright, m, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800909 }
tomhudson64de1e12015-03-05 08:01:07 -0800910
911#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
912 VIA("androidsdk", ViaAndroidSDK, wrapped);
913#endif
914
mtklein748ca3b2015-01-15 10:56:12 -0800915#undef VIA
halcanary96fcdcc2015-08-27 07:41:13 -0700916 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800917}
918
brianosman05c987c2016-05-09 13:42:16 -0700919static bool gather_sinks() {
kkinnunen3e980c32015-12-23 01:33:00 -0800920 SkCommandLineConfigArray configs;
921 ParseConfigs(FLAGS_config, &configs);
922 for (int i = 0; i < configs.count(); i++) {
923 const SkCommandLineConfig& config = *configs[i];
924 Sink* sink = create_sink(&config);
925 if (sink == nullptr) {
mtkleinc41fd922016-03-08 09:01:39 -0800926 info("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
927 config.getTag().c_str());
kkinnunen3e980c32015-12-23 01:33:00 -0800928 continue;
929 }
mtklein748ca3b2015-01-15 10:56:12 -0800930
kkinnunen3e980c32015-12-23 01:33:00 -0800931 const SkTArray<SkString>& parts = config.getViaParts();
932 for (int j = parts.count(); j-- > 0;) {
933 const SkString& part = parts[j];
934 Sink* next = create_via(part, sink);
halcanary96fcdcc2015-08-27 07:41:13 -0700935 if (next == nullptr) {
mtkleinc41fd922016-03-08 09:01:39 -0800936 info("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
937 part.c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800938 delete sink;
halcanary96fcdcc2015-08-27 07:41:13 -0700939 sink = nullptr;
mtklein748ca3b2015-01-15 10:56:12 -0800940 break;
941 }
942 sink = next;
943 }
944 if (sink) {
945 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800946 }
947 }
brianosman05c987c2016-05-09 13:42:16 -0700948
949 // If no configs were requested (just running tests, perhaps?), then we're okay.
950 // Otherwise, make sure that at least one sink was constructed correctly. This catches
951 // the case of bots without a GPU being assigned GPU configs.
952 return (configs.count() == 0) || (gSinks.count() > 0);
mtklein114c3cd2015-01-15 10:15:02 -0800953}
mtklein709d2c32015-01-15 08:30:25 -0800954
mtkleina5114d72015-08-24 13:27:01 -0700955static bool dump_png(SkBitmap bitmap, const char* path, const char* md5) {
956 const int w = bitmap.width(),
957 h = bitmap.height();
958
brianosman3c579dc2016-04-19 09:18:11 -0700959 sk_sp<SkData> encodedBitmap = sk_tools::encode_bitmap_for_png(bitmap);
960 if (encodedBitmap.get() == nullptr) {
961 return false;
mtkleina5114d72015-08-24 13:27:01 -0700962 }
brianosman3c579dc2016-04-19 09:18:11 -0700963 uint32_t* rgba = static_cast<uint32_t*>(encodedBitmap.get()->writable_data());
mtkleina5114d72015-08-24 13:27:01 -0700964
965 // We don't need bitmap anymore. Might as well drop our ref.
mtkleinfccb77d2015-08-24 14:13:29 -0700966 bitmap.reset();
mtkleina5114d72015-08-24 13:27:01 -0700967
mtkleinc64137c2015-08-25 10:56:08 -0700968 FILE* f = fopen(path, "wb");
mtkleina5114d72015-08-24 13:27:01 -0700969 if (!f) { return false; }
970
971 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
972 if (!png) {
973 fclose(f);
974 return false;
975 }
976
977 png_infop info = png_create_info_struct(png);
978 if (!info) {
979 png_destroy_write_struct(&png, &info);
980 fclose(f);
981 return false;
982 }
983
mtkleind69ece62015-09-10 10:37:44 -0700984 SkString description;
985 description.append("Key: ");
986 for (int i = 0; i < FLAGS_key.count(); i++) {
987 description.appendf("%s ", FLAGS_key[i]);
988 }
989 description.append("Properties: ");
990 for (int i = 0; i < FLAGS_properties.count(); i++) {
991 description.appendf("%s ", FLAGS_properties[i]);
992 }
993 description.appendf("MD5: %s", md5);
994
mtkleina5114d72015-08-24 13:27:01 -0700995 png_text text[2];
996 text[0].key = (png_charp)"Author";
997 text[0].text = (png_charp)"DM dump_png()";
998 text[0].compression = PNG_TEXT_COMPRESSION_NONE;
999 text[1].key = (png_charp)"Description";
mtkleind69ece62015-09-10 10:37:44 -07001000 text[1].text = (png_charp)description.c_str();
mtkleina5114d72015-08-24 13:27:01 -07001001 text[1].compression = PNG_TEXT_COMPRESSION_NONE;
1002 png_set_text(png, info, text, 2);
1003
1004 png_init_io(png, f);
1005 png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8,
1006 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
1007 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
1008 png_write_info(png, info);
1009 for (int j = 0; j < h; j++) {
brianosman3c579dc2016-04-19 09:18:11 -07001010 png_bytep row = (png_bytep)(rgba + w*j);
mtkleina5114d72015-08-24 13:27:01 -07001011 png_write_rows(png, &row, 1);
1012 }
1013 png_write_end(png, info);
1014
1015 png_destroy_write_struct(&png, &info);
1016 fclose(f);
1017 return true;
1018}
1019
mtkleina2ef6422015-01-15 13:44:22 -08001020static bool match(const char* needle, const char* haystack) {
halcanary96fcdcc2015-08-27 07:41:13 -07001021 return 0 == strcmp("_", needle) || nullptr != strstr(haystack, needle);
mtkleina2ef6422015-01-15 13:44:22 -08001022}
1023
mtklein3eed7dd2016-02-24 19:07:07 -08001024static bool is_blacklisted(const char* sink, const char* src,
1025 const char* srcOptions, const char* name) {
mtklein0d243ff2015-04-03 07:51:00 -07001026 for (int i = 0; i < FLAGS_blacklist.count() - 3; i += 4) {
mtkleina2ef6422015-01-15 13:44:22 -08001027 if (match(FLAGS_blacklist[i+0], sink) &&
djsollen54416de2015-04-03 07:24:48 -07001028 match(FLAGS_blacklist[i+1], src) &&
1029 match(FLAGS_blacklist[i+2], srcOptions) &&
1030 match(FLAGS_blacklist[i+3], name)) {
mtklein3eed7dd2016-02-24 19:07:07 -08001031 return true;
mtkleina2ef6422015-01-15 13:44:22 -08001032 }
1033 }
mtklein3eed7dd2016-02-24 19:07:07 -08001034 return false;
mtkleina2ef6422015-01-15 13:44:22 -08001035}
1036
mtkleincd50bca2016-01-05 06:20:20 -08001037// Even when a Task Sink reports to be non-threadsafe (e.g. GPU), we know things like
1038// .png encoding are definitely thread safe. This lets us offload that work to CPU threads.
1039static SkTaskGroup gDefinitelyThreadSafeWork;
1040
mtklein748ca3b2015-01-15 10:56:12 -08001041// The finest-grained unit of work we can run: draw a single Src into a single Sink,
1042// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
1043struct Task {
mtkleine0effd62015-07-29 06:37:28 -07001044 Task(const TaggedSrc& src, const TaggedSink& sink) : src(src), sink(sink) {}
1045 const TaggedSrc& src;
1046 const TaggedSink& sink;
mtklein748ca3b2015-01-15 10:56:12 -08001047
mtklein21eaf3b2016-02-08 12:39:59 -08001048 static void Run(const Task& task) {
1049 SkString name = task.src->name();
mtkleine0effd62015-07-29 06:37:28 -07001050
mtkleinb9eb4ac2015-02-02 18:26:03 -08001051 SkString log;
mtklein3eed7dd2016-02-24 19:07:07 -08001052 if (!FLAGS_dryRun) {
mtklein748ca3b2015-01-15 10:56:12 -08001053 SkBitmap bitmap;
1054 SkDynamicMemoryWStream stream;
mtklein3eed7dd2016-02-24 19:07:07 -08001055 start(task.sink.tag.c_str(), task.src.tag.c_str(),
1056 task.src.options.c_str(), name.c_str());
mtklein21eaf3b2016-02-08 12:39:59 -08001057 Error err = task.sink->draw(*task.src, &bitmap, &stream, &log);
mtkleinb3b13b72016-03-07 13:20:52 -08001058 if (!log.isEmpty()) {
mtkleinc41fd922016-03-08 09:01:39 -08001059 info("%s %s %s %s:\n%s\n", task.sink.tag.c_str()
1060 , task.src.tag.c_str()
1061 , task.src.options.c_str()
1062 , name.c_str()
1063 , log.c_str());
mtkleinb3b13b72016-03-07 13:20:52 -08001064 }
mtklein748ca3b2015-01-15 10:56:12 -08001065 if (!err.isEmpty()) {
mtklein4089ef72015-03-05 08:40:28 -08001066 if (err.isFatal()) {
djsollen54416de2015-04-03 07:24:48 -07001067 fail(SkStringPrintf("%s %s %s %s: %s",
mtklein21eaf3b2016-02-08 12:39:59 -08001068 task.sink.tag.c_str(),
1069 task.src.tag.c_str(),
1070 task.src.options.c_str(),
mtklein4089ef72015-03-05 08:40:28 -08001071 name.c_str(),
1072 err.c_str()));
mtkleinb37cb412015-03-18 05:27:14 -07001073 } else {
mtklein3eed7dd2016-02-24 19:07:07 -08001074 done(task.sink.tag.c_str(), task.src.tag.c_str(),
1075 task.src.options.c_str(), name.c_str());
mtkleinba6ada72016-01-21 09:39:35 -08001076 return;
mtklein4089ef72015-03-05 08:40:28 -08001077 }
mtklein748ca3b2015-01-15 10:56:12 -08001078 }
mtklein62bd1a62015-01-27 14:46:26 -08001079
mtkleincd50bca2016-01-05 06:20:20 -08001080 // We're likely switching threads here, so we must capture by value, [=] or [foo,bar].
1081 SkStreamAsset* data = stream.detachAsStream();
1082 gDefinitelyThreadSafeWork.add([task,name,bitmap,data]{
Ben Wagner145dbcd2016-11-03 14:40:50 -04001083 std::unique_ptr<SkStreamAsset> ownedData(data);
mtkleincd50bca2016-01-05 06:20:20 -08001084
1085 // Why doesn't the copy constructor do this when we have pre-locked pixels?
1086 bitmap.lockPixels();
1087
1088 SkString md5;
1089 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
1090 SkMD5 hash;
1091 if (data->getLength()) {
1092 hash.writeStream(data, data->getLength());
1093 data->rewind();
mtklein38408462015-07-08 07:25:27 -07001094 } else {
mtkleincd50bca2016-01-05 06:20:20 -08001095 // If we're BGRA (Linux, Windows), swizzle over to RGBA (Mac, Android).
1096 // This helps eliminate multiple 0-pixel-diff hashes on gold.skia.org.
1097 // (Android's general slow speed breaks the tie arbitrarily in RGBA's favor.)
1098 // We might consider promoting 565 to RGBA too.
1099 if (bitmap.colorType() == kBGRA_8888_SkColorType) {
1100 SkBitmap swizzle;
1101 SkAssertResult(bitmap.copyTo(&swizzle, kRGBA_8888_SkColorType));
1102 hash.write(swizzle.getPixels(), swizzle.getSize());
1103 } else {
1104 hash.write(bitmap.getPixels(), bitmap.getSize());
1105 }
1106 }
1107 SkMD5::Digest digest;
1108 hash.finish(digest);
1109 for (int i = 0; i < 16; i++) {
1110 md5.appendf("%02x", digest.data[i]);
mtklein38408462015-07-08 07:25:27 -07001111 }
mtklein62bd1a62015-01-27 14:46:26 -08001112 }
mtklein62bd1a62015-01-27 14:46:26 -08001113
mtkleincd50bca2016-01-05 06:20:20 -08001114 if (!FLAGS_readPath.isEmpty() &&
mtklein3eed7dd2016-02-24 19:07:07 -08001115 !gGold.contains(Gold(task.sink.tag, task.src.tag,
1116 task.src.options, name, md5))) {
mtkleincd50bca2016-01-05 06:20:20 -08001117 fail(SkStringPrintf("%s not found for %s %s %s %s in %s",
1118 md5.c_str(),
mtklein21eaf3b2016-02-08 12:39:59 -08001119 task.sink.tag.c_str(),
1120 task.src.tag.c_str(),
1121 task.src.options.c_str(),
mtkleincd50bca2016-01-05 06:20:20 -08001122 name.c_str(),
1123 FLAGS_readPath[0]));
mtklein748ca3b2015-01-15 10:56:12 -08001124 }
mtkleincd50bca2016-01-05 06:20:20 -08001125
1126 if (!FLAGS_writePath.isEmpty()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001127 const char* ext = task.sink->fileExtension();
mtkleincd50bca2016-01-05 06:20:20 -08001128 if (data->getLength()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001129 WriteToDisk(task, md5, ext, data, data->getLength(), nullptr);
mtkleincd50bca2016-01-05 06:20:20 -08001130 SkASSERT(bitmap.drawsNothing());
1131 } else if (!bitmap.drawsNothing()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001132 WriteToDisk(task, md5, ext, nullptr, 0, &bitmap);
mtkleincd50bca2016-01-05 06:20:20 -08001133 }
1134 }
1135 });
mtklein748ca3b2015-01-15 10:56:12 -08001136 }
mtklein3eed7dd2016-02-24 19:07:07 -08001137 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 -08001138 }
1139
1140 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -08001141 SkString md5,
1142 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -08001143 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -08001144 const SkBitmap* bitmap) {
mtklein409d4702016-02-29 07:38:01 -08001145 bool gammaCorrect = false;
1146 if (bitmap) {
Brian Osman7039f742016-11-01 15:56:16 -04001147 gammaCorrect = SkToBool(bitmap->info().colorSpace());
mtklein409d4702016-02-29 07:38:01 -08001148 }
1149
mtklein748ca3b2015-01-15 10:56:12 -08001150 JsonWriter::BitmapResult result;
djsollen54416de2015-04-03 07:24:48 -07001151 result.name = task.src->name();
mtklein3eed7dd2016-02-24 19:07:07 -08001152 result.config = task.sink.tag;
djsollen54416de2015-04-03 07:24:48 -07001153 result.sourceType = task.src.tag;
1154 result.sourceOptions = task.src.options;
1155 result.ext = ext;
mtklein409d4702016-02-29 07:38:01 -08001156 result.gammaCorrect = gammaCorrect;
djsollen54416de2015-04-03 07:24:48 -07001157 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -08001158 JsonWriter::AddBitmapResult(result);
1159
mtkleinb0531a72015-04-07 13:38:48 -07001160 // If an MD5 is uninteresting, we want it noted in the JSON file,
1161 // but don't want to dump it out as a .png (or whatever ext is).
1162 if (gUninterestingHashes.contains(md5)) {
1163 return;
1164 }
1165
mtklein748ca3b2015-01-15 10:56:12 -08001166 const char* dir = FLAGS_writePath[0];
1167 if (0 == strcmp(dir, "@")) { // Needed for iOS.
1168 dir = FLAGS_resourcePath[0];
1169 }
1170 sk_mkdir(dir);
1171
1172 SkString path;
1173 if (FLAGS_nameByHash) {
1174 path = SkOSPath::Join(dir, result.md5.c_str());
1175 path.append(".");
1176 path.append(ext);
1177 if (sk_exists(path.c_str())) {
1178 return; // Content-addressed. If it exists already, we're done.
1179 }
1180 } else {
kkinnunen3e980c32015-12-23 01:33:00 -08001181 path = SkOSPath::Join(dir, task.sink.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001182 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001183 path = SkOSPath::Join(path.c_str(), task.src.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001184 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001185 if (strcmp(task.src.options.c_str(), "") != 0) {
1186 path = SkOSPath::Join(path.c_str(), task.src.options.c_str());
djsollen54416de2015-04-03 07:24:48 -07001187 sk_mkdir(path.c_str());
1188 }
mtklein748ca3b2015-01-15 10:56:12 -08001189 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
1190 path.append(".");
1191 path.append(ext);
1192 }
1193
mtklein748ca3b2015-01-15 10:56:12 -08001194 if (bitmap) {
mtkleina5114d72015-08-24 13:27:01 -07001195 if (!dump_png(*bitmap, path.c_str(), result.md5.c_str())) {
mtklein748ca3b2015-01-15 10:56:12 -08001196 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
1197 return;
1198 }
1199 } else {
mtkleina5114d72015-08-24 13:27:01 -07001200 SkFILEWStream file(path.c_str());
1201 if (!file.isValid()) {
1202 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
1203 return;
1204 }
mtklein748ca3b2015-01-15 10:56:12 -08001205 if (!file.writeStream(data, len)) {
1206 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
1207 return;
1208 }
1209 }
1210 }
1211};
1212
mtklein748ca3b2015-01-15 10:56:12 -08001213/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1214
1215// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
1216
mtklein21eaf3b2016-02-08 12:39:59 -08001217static SkTDArray<skiatest::Test> gParallelTests, gSerialTests;
mtklein748ca3b2015-01-15 10:56:12 -08001218
1219static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -08001220 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -08001221 return;
1222 }
mtklein6393c062015-04-27 08:45:01 -07001223 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r; r = r->next()) {
1224 if (!in_shard()) {
1225 continue;
1226 }
halcanary87f3ba42015-01-20 09:30:20 -08001227 // Despite its name, factory() is returning a reference to
1228 // link-time static const POD data.
1229 const skiatest::Test& test = r->factory();
1230 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -08001231 continue;
1232 }
halcanary87f3ba42015-01-20 09:30:20 -08001233 if (test.needsGpu && gpu_supported()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001234 (FLAGS_gpu_threading ? gParallelTests : gSerialTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -08001235 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein21eaf3b2016-02-08 12:39:59 -08001236 gParallelTests.push(test);
mtklein82d28432015-01-15 12:46:02 -08001237 }
mtklein748ca3b2015-01-15 10:56:12 -08001238 }
1239}
1240
mtklein21eaf3b2016-02-08 12:39:59 -08001241static void run_test(skiatest::Test test) {
halcanary87f3ba42015-01-20 09:30:20 -08001242 struct : public skiatest::Reporter {
mtklein36352bf2015-03-25 18:17:31 -07001243 void reportFailed(const skiatest::Failure& failure) override {
halcanary87f3ba42015-01-20 09:30:20 -08001244 fail(failure.toString());
1245 JsonWriter::AddTestFailure(failure);
1246 }
mtklein36352bf2015-03-25 18:17:31 -07001247 bool allowExtendedTest() const override {
halcanary87f3ba42015-01-20 09:30:20 -08001248 return FLAGS_pathOpsExtended;
1249 }
mtklein36352bf2015-03-25 18:17:31 -07001250 bool verbose() const override { return FLAGS_veryVerbose; }
halcanary87f3ba42015-01-20 09:30:20 -08001251 } reporter;
djsollen824996a2015-06-12 12:06:22 -07001252
mtklein3eed7dd2016-02-24 19:07:07 -08001253 if (!FLAGS_dryRun && !is_blacklisted("_", "tests", "_", test.name)) {
mtklein21eaf3b2016-02-08 12:39:59 -08001254 start("unit", "test", "", test.name);
mtklein55e88b22015-01-21 15:50:13 -08001255 GrContextFactory factory;
mtklein21eaf3b2016-02-08 12:39:59 -08001256 test.proc(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -08001257 }
mtklein3eed7dd2016-02-24 19:07:07 -08001258 done("unit", "test", "", test.name);
mtklein748ca3b2015-01-15 10:56:12 -08001259}
1260
1261/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1262
mtkleinafae30a2016-02-24 12:28:32 -08001263DEFINE_int32(status_sec, 15, "Print status this often (and if we crash).");
1264
1265SkThread* start_status_thread() {
1266 auto thread = new SkThread([] (void*) {
1267 for (;;) {
1268 print_status();
1269 #if defined(SK_BUILD_FOR_WIN)
1270 Sleep(FLAGS_status_sec * 1000);
1271 #else
1272 sleep(FLAGS_status_sec);
1273 #endif
mtklein2e1c47e2015-03-12 07:16:56 -07001274 }
mtkleinafae30a2016-02-24 12:28:32 -08001275 });
1276 thread->start();
1277 return thread;
mtkleinde6fc2b2015-03-12 06:28:54 -07001278}
1279
caryclark83ca6282015-06-10 09:31:09 -07001280#define PORTABLE_FONT_PREFIX "Toy Liberation "
1281
mbocee6a9912016-05-31 11:42:36 -07001282static sk_sp<SkTypeface> create_from_name(const char familyName[], SkFontStyle style) {
caryclark83ca6282015-06-10 09:31:09 -07001283 if (familyName && strlen(familyName) > sizeof(PORTABLE_FONT_PREFIX)
1284 && !strncmp(familyName, PORTABLE_FONT_PREFIX, sizeof(PORTABLE_FONT_PREFIX) - 1)) {
caryclark1818acb2015-07-24 12:09:25 -07001285 return sk_tool_utils::create_portable_typeface(familyName, style);
caryclark83ca6282015-06-10 09:31:09 -07001286 }
halcanary96fcdcc2015-08-27 07:41:13 -07001287 return nullptr;
caryclark83ca6282015-06-10 09:31:09 -07001288}
1289
1290#undef PORTABLE_FONT_PREFIX
1291
mbocee6a9912016-05-31 11:42:36 -07001292extern sk_sp<SkTypeface> (*gCreateTypefaceDelegate)(const char [], SkFontStyle );
caryclark83ca6282015-06-10 09:31:09 -07001293
jcgregorio3b27ade2014-11-13 08:06:40 -08001294int dm_main();
caryclark17f0b6d2014-07-22 10:15:34 -07001295int dm_main() {
bsalomon71de3532016-04-26 14:27:21 -07001296 setbuf(stdout, nullptr);
mtklein246ba3a2016-02-23 10:39:36 -08001297 setup_crash_handler();
mtkleinafae30a2016-02-24 12:28:32 -08001298
Yuqian Liba62b4a2016-12-14 13:46:53 -05001299 gSkUseAnalyticAA = FLAGS_analyticAA;
liyuqian38911a72016-10-04 11:23:22 -07001300
mtkleina483da32016-03-17 12:53:36 -07001301 if (FLAGS_verbose) {
1302 gVLog = stderr;
1303 } else if (!FLAGS_writePath.isEmpty()) {
mtklein51c8cfc2016-03-11 12:59:09 -08001304 sk_mkdir(FLAGS_writePath[0]);
mtkleina1ce2162016-07-22 12:23:46 -07001305 gVLog = fopen(SkOSPath::Join(FLAGS_writePath[0], "verbose.log").c_str(), "w");
mtkleinc41fd922016-03-08 09:01:39 -08001306 }
1307
mtklein5286f022016-01-22 08:18:14 -08001308 JsonWriter::DumpJson(); // It's handy for the bots to assume this is ~never missing.
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +00001309 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -07001310 SkTaskGroup::Enabler enabled(FLAGS_threads);
caryclark83ca6282015-06-10 09:31:09 -07001311 gCreateTypefaceDelegate = &create_from_name;
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +00001312
halcanary7d571242016-02-24 17:59:16 -08001313 {
1314 SkString testResourcePath = GetResourcePath("color_wheel.png");
1315 SkFILEStream testResource(testResourcePath.c_str());
1316 if (!testResource.isValid()) {
mtkleinc41fd922016-03-08 09:01:39 -08001317 info("Some resources are missing. Do you need to set --resourcePath?\n");
halcanary7d571242016-02-24 17:59:16 -08001318 }
1319 }
mtklein62bd1a62015-01-27 14:46:26 -08001320 gather_gold();
borenet09ed4802015-04-03 14:15:33 -07001321 gather_uninteresting_hashes();
mtklein62bd1a62015-01-27 14:46:26 -08001322
scroggo86737142016-02-03 12:19:11 -08001323 if (!gather_srcs()) {
1324 return 1;
1325 }
brianosman05c987c2016-05-09 13:42:16 -07001326 if (!gather_sinks()) {
1327 return 1;
1328 }
mtklein748ca3b2015-01-15 10:56:12 -08001329 gather_tests();
mtklein21eaf3b2016-02-08 12:39:59 -08001330 gPending = gSrcs.count() * gSinks.count() + gParallelTests.count() + gSerialTests.count();
mtkleinc41fd922016-03-08 09:01:39 -08001331 info("%d srcs * %d sinks + %d tests == %d tasks",
1332 gSrcs.count(), gSinks.count(), gParallelTests.count() + gSerialTests.count(), gPending);
Ben Wagner145dbcd2016-11-03 14:40:50 -04001333 std::unique_ptr<SkThread> statusThread(start_status_thread());
mtklein748ca3b2015-01-15 10:56:12 -08001334
mtklein21eaf3b2016-02-08 12:39:59 -08001335 // Kick off as much parallel work as we can, making note of any serial work we'll need to do.
1336 SkTaskGroup parallel;
1337 SkTArray<Task> serial;
1338
1339 for (auto& sink : gSinks)
1340 for (auto& src : gSrcs) {
mtklein3eed7dd2016-02-24 19:07:07 -08001341 if (src->veto(sink->flags()) ||
1342 is_blacklisted(sink.tag.c_str(), src.tag.c_str(),
1343 src.options.c_str(), src->name().c_str())) {
mtkleinf10637f2016-06-10 13:56:35 -07001344 SkAutoMutexAcquire lock(gMutex);
mtklein3eed7dd2016-02-24 19:07:07 -08001345 gPending--;
1346 continue;
1347 }
1348
mtklein21eaf3b2016-02-08 12:39:59 -08001349 Task task(src, sink);
1350 if (src->serial() || sink->serial()) {
1351 serial.push_back(task);
1352 } else {
1353 parallel.add([task] { Task::Run(task); });
mtklein748ca3b2015-01-15 10:56:12 -08001354 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001355 }
mtklein21eaf3b2016-02-08 12:39:59 -08001356 for (auto test : gParallelTests) {
1357 parallel.add([test] { run_test(test); });
mtklein55e88b22015-01-21 15:50:13 -08001358 }
mtklein21eaf3b2016-02-08 12:39:59 -08001359
1360 // With the parallel work running, run serial tasks and tests here on main thread.
1361 for (auto task : serial) { Task::Run(task); }
1362 for (auto test : gSerialTests) { run_test(test); }
1363
1364 // Wait for any remaining parallel work to complete (including any spun off of serial tasks).
1365 parallel.wait();
mtkleincd50bca2016-01-05 06:20:20 -08001366 gDefinitelyThreadSafeWork.wait();
1367
mtkleinda884c42016-02-24 18:00:23 -08001368 // We'd better have run everything.
1369 SkASSERT(gPending == 0);
mtkleine027f172016-02-26 15:53:06 -08001370 // Make sure we've flushed all our results to disk.
1371 JsonWriter::DumpJson();
mtkleinda884c42016-02-24 18:00:23 -08001372
mtklein748ca3b2015-01-15 10:56:12 -08001373 // At this point we're back in single-threaded land.
caryclarkf53ce802015-06-15 06:48:30 -07001374 sk_tool_utils::release_portable_typefaces();
mtklein@google.comd36522d2013-10-16 13:02:15 +00001375
mtklein748ca3b2015-01-15 10:56:12 -08001376 if (gFailures.count() > 0) {
mtkleinc41fd922016-03-08 09:01:39 -08001377 info("Failures:\n");
mtklein748ca3b2015-01-15 10:56:12 -08001378 for (int i = 0; i < gFailures.count(); i++) {
mtkleinc41fd922016-03-08 09:01:39 -08001379 info("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001380 }
mtkleinc41fd922016-03-08 09:01:39 -08001381 info("%d failures\n", gFailures.count());
mtklein748ca3b2015-01-15 10:56:12 -08001382 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001383 }
mtkleinafae30a2016-02-24 12:28:32 -08001384
1385#ifdef SK_PDF_IMAGE_STATS
halcanary7a14b312015-10-01 07:28:13 -07001386 SkPDFImageDumpStats();
mtkleinafae30a2016-02-24 12:28:32 -08001387#endif // SK_PDF_IMAGE_STATS
1388
1389 print_status();
mtkleinc41fd922016-03-08 09:01:39 -08001390 info("Finished!\n");
mtklein748ca3b2015-01-15 10:56:12 -08001391 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +00001392}
jcgregorio3b27ade2014-11-13 08:06:40 -08001393
kkinnunen179a8f52015-11-20 13:32:24 -08001394// TODO: currently many GPU tests are declared outside SK_SUPPORT_GPU guards.
1395// Thus we export the empty RunWithGPUTestContexts when SK_SUPPORT_GPU=0.
1396namespace skiatest {
bsalomon758586c2016-04-06 14:02:39 -07001397
1398#if SK_SUPPORT_GPU
1399bool IsGLContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1400 return kOpenGL_GrBackend == GrContextFactory::ContextTypeBackend(type);
1401}
bsalomondc0fcd42016-04-11 14:21:33 -07001402bool IsVulkanContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1403 return kVulkan_GrBackend == GrContextFactory::ContextTypeBackend(type);
1404}
bsalomon758586c2016-04-06 14:02:39 -07001405bool IsRenderingGLContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1406 return IsGLContextType(type) && GrContextFactory::IsRenderingContext(type);
1407}
1408bool IsNullGLContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1409 return type == GrContextFactory::kNullGL_ContextType;
1410}
Brian Osman287f6512016-12-05 11:35:07 -05001411const char* ContextTypeName(GrContextFactory::ContextType contextType) {
1412 switch (contextType) {
1413 case GrContextFactory::kGL_ContextType:
1414 return "OpenGL";
1415 case GrContextFactory::kGLES_ContextType:
1416 return "OpenGLES";
1417 case GrContextFactory::kANGLE_D3D9_ES2_ContextType:
1418 return "ANGLE D3D9 ES2";
1419 case GrContextFactory::kANGLE_D3D11_ES2_ContextType:
1420 return "ANGLE D3D11 ES2";
1421 case GrContextFactory::kANGLE_D3D11_ES3_ContextType:
1422 return "ANGLE D3D11 ES3";
1423 case GrContextFactory::kANGLE_GL_ES2_ContextType:
1424 return "ANGLE GL ES2";
1425 case GrContextFactory::kANGLE_GL_ES3_ContextType:
1426 return "ANGLE GL ES3";
1427 case GrContextFactory::kCommandBuffer_ContextType:
1428 return "Command Buffer";
1429 case GrContextFactory::kMESA_ContextType:
1430 return "Mesa";
1431 case GrContextFactory::kNullGL_ContextType:
1432 return "Null GL";
1433 case GrContextFactory::kDebugGL_ContextType:
1434 return "Debug GL";
1435 case GrContextFactory::kVulkan_ContextType:
1436 return "Vulkan";
1437 }
1438 SkDEBUGFAIL("Unreachable");
1439 return "Unknown";
1440}
bsalomon758586c2016-04-06 14:02:39 -07001441#else
1442bool IsGLContextType(int) { return false; }
bsalomondc0fcd42016-04-11 14:21:33 -07001443bool IsVulkanContextType(int) { return false; }
bsalomon758586c2016-04-06 14:02:39 -07001444bool IsRenderingGLContextType(int) { return false; }
1445bool IsNullGLContextType(int) { return false; }
1446#endif
1447
1448void RunWithGPUTestContexts(GrContextTestFn* test, GrContextTypeFilterFn* contextTypeFilter,
bsalomonf2f1c172016-04-05 12:59:06 -07001449 Reporter* reporter, GrContextFactory* factory) {
kkinnunen179a8f52015-11-20 13:32:24 -08001450#if SK_SUPPORT_GPU
kkinnunen3e980c32015-12-23 01:33:00 -08001451
bsalomon758586c2016-04-06 14:02:39 -07001452 for (int typeInt = 0; typeInt < GrContextFactory::kContextTypeCnt; ++typeInt) {
1453 GrContextFactory::ContextType contextType = (GrContextFactory::ContextType) typeInt;
Brian Osmanaa2219c2016-12-05 10:12:17 -05001454 // Use "native" instead of explicitly trying OpenGL and OpenGL ES. Do not use GLES on
bsalomon758586c2016-04-06 14:02:39 -07001455 // desktop since tests do not account for not fixing http://skbug.com/2809
1456 if (contextType == GrContextFactory::kGL_ContextType ||
1457 contextType == GrContextFactory::kGLES_ContextType) {
1458 if (contextType != GrContextFactory::kNativeGL_ContextType) {
1459 continue;
1460 }
1461 }
Brian Osmanaa2219c2016-12-05 10:12:17 -05001462 ContextInfo ctxInfo = factory->getContextInfo(contextType);
1463 if (contextTypeFilter && !(*contextTypeFilter)(contextType)) {
1464 continue;
1465 }
Brian Osman287f6512016-12-05 11:35:07 -05001466 ReporterContext ctx(reporter, SkString(ContextTypeName(contextType)));
bsalomon8b7451a2016-05-11 06:33:06 -07001467 if (ctxInfo.grContext()) {
bsalomonf2f1c172016-04-05 12:59:06 -07001468 (*test)(reporter, ctxInfo);
kkinnunen5219fd92015-12-10 06:28:13 -08001469 }
bsalomonf2f1c172016-04-05 12:59:06 -07001470 ctxInfo = factory->getContextInfo(contextType,
csmartdalton6270e552016-09-13 10:41:49 -07001471 GrContextFactory::ContextOptions::kEnableNVPR);
bsalomon8b7451a2016-05-11 06:33:06 -07001472 if (ctxInfo.grContext()) {
bsalomonf2f1c172016-04-05 12:59:06 -07001473 (*test)(reporter, ctxInfo);
kkinnunen179a8f52015-11-20 13:32:24 -08001474 }
1475 }
1476#endif
1477}
kkinnunen179a8f52015-11-20 13:32:24 -08001478} // namespace skiatest
1479
borenet48087572015-04-02 12:16:36 -07001480#if !defined(SK_BUILD_FOR_IOS)
jcgregorio3b27ade2014-11-13 08:06:40 -08001481int main(int argc, char** argv) {
1482 SkCommandLineFlags::Parse(argc, argv);
1483 return dm_main();
1484}
1485#endif