blob: cccc92b9faf4b63b5a48187e63b563f709f32cbe [file] [log] [blame]
scroggo478652e2015-03-25 07:11:02 -07001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
jcgregorio3b27ade2014-11-13 08:06:40 -08008#include "CrashHandler.h"
mtklein748ca3b2015-01-15 10:56:12 -08009#include "DMJsonWriter.h"
10#include "DMSrcSink.h"
tomhudsoneebc39a2015-02-23 12:18:05 -080011#include "DMSrcSinkAndroid.h"
mtklein748ca3b2015-01-15 10:56:12 -080012#include "ProcStats.h"
halcanary7d571242016-02-24 17:59:16 -080013#include "Resources.h"
mtklein748ca3b2015-01-15 10:56:12 -080014#include "SkBBHFactory.h"
mtklein62bd1a62015-01-27 14:46:26 -080015#include "SkChecksum.h"
scroggocc2feb12015-08-14 08:32:46 -070016#include "SkCodec.h"
mtklein27c3fdd2016-02-26 14:43:21 -080017#include "SkColorPriv.h"
msarett888dc162016-05-23 10:21:17 -070018#include "SkColorSpace.h"
caryclark17f0b6d2014-07-22 10:15:34 -070019#include "SkCommonFlags.h"
kkinnunen3e980c32015-12-23 01:33:00 -080020#include "SkCommonFlagsConfig.h"
brianosman3c579dc2016-04-19 09:18:11 -070021#include "SkData.h"
caryclark83ca6282015-06-10 09:31:09 -070022#include "SkFontMgr.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +000023#include "SkGraphics.h"
mtklein27c3fdd2016-02-26 14:43:21 -080024#include "SkHalf.h"
halcanary4dbbd042016-06-07 17:21:10 -070025#include "SkLeanWindows.h"
mtklein748ca3b2015-01-15 10:56:12 -080026#include "SkMD5.h"
mtklein1b249332015-07-07 12:21:21 -070027#include "SkMutex.h"
mtklein1d0f1642014-09-08 08:05:18 -070028#include "SkOSFile.h"
mtklein27c3fdd2016-02-26 14:43:21 -080029#include "SkPM4fPriv.h"
mtklein246ba3a2016-02-23 10:39:36 -080030#include "SkSpinlock.h"
mtkleina82f5622015-02-20 12:30:19 -080031#include "SkTHash.h"
mtklein406654b2014-09-03 15:34:37 -070032#include "SkTaskGroup.h"
mtkleinde6fc2b2015-03-12 06:28:54 -070033#include "SkThreadUtils.h"
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +000034#include "Test.h"
mtklein748ca3b2015-01-15 10:56:12 -080035#include "Timer.h"
brianosman3c579dc2016-04-19 09:18:11 -070036#include "picture_utils.h"
caryclark83ca6282015-06-10 09:31:09 -070037#include "sk_tool_utils.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +000038
halcanary7a14b312015-10-01 07:28:13 -070039#ifdef SK_PDF_IMAGE_STATS
40extern void SkPDFImageDumpStats();
41#endif
42
mtkleina5114d72015-08-24 13:27:01 -070043#include "png.h"
44
bungeman60e0fee2015-08-26 05:15:46 -070045#include <stdlib.h>
46
scroggo27a58342015-10-28 08:56:41 -070047#ifndef SK_BUILD_FOR_WIN32
48 #include <unistd.h>
49#endif
50
djsollen54416de2015-04-03 07:24:48 -070051DEFINE_string(src, "tests gm skp image", "Source types to test.");
mtklein748ca3b2015-01-15 10:56:12 -080052DEFINE_bool(nameByHash, false,
53 "If true, write to FLAGS_writePath[0]/<hash>.png instead of "
djsollen54416de2015-04-03 07:24:48 -070054 "to FLAGS_writePath[0]/<config>/<sourceType>/<sourceOptions>/<name>.png");
mtklein748ca3b2015-01-15 10:56:12 -080055DEFINE_bool2(pathOpsExtended, x, false, "Run extended pathOps tests.");
mtkleind603b222015-02-17 11:13:33 -080056DEFINE_string(matrix, "1 0 0 1",
57 "2x2 scale+skew matrix to apply or upright when using "
58 "'matrix' or 'upright' in config.");
mtklein82d28432015-01-15 12:46:02 -080059DEFINE_bool(gpu_threading, false, "Allow GPU work to run on multiple threads?");
mtklein@google.comd36522d2013-10-16 13:02:15 +000060
mtkleina2ef6422015-01-15 13:44:22 -080061DEFINE_string(blacklist, "",
djsollen54416de2015-04-03 07:24:48 -070062 "Space-separated config/src/srcOptions/name quadruples to blacklist. '_' matches anything. E.g. \n"
63 "'--blacklist gpu skp _ _' will blacklist all SKPs drawn into the gpu config.\n"
64 "'--blacklist gpu skp _ _ 8888 gm _ aarects' will also blacklist the aarects GM on 8888.");
mtkleina2ef6422015-01-15 13:44:22 -080065
mtklein62bd1a62015-01-27 14:46:26 -080066DEFINE_string2(readPath, r, "", "If set check for equality with golden results in this directory.");
67
borenet09ed4802015-04-03 14:15:33 -070068DEFINE_string(uninterestingHashesFile, "",
69 "File containing a list of uninteresting hashes. If a result hashes to something in "
70 "this list, no image is written for that result.");
71
mtklein6393c062015-04-27 08:45:01 -070072DEFINE_int32(shards, 1, "We're splitting source data into this many shards.");
73DEFINE_int32(shard, 0, "Which shard do I run?");
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
mtkleinb37cb412015-03-18 05:27:14 -0700119
mtklein246ba3a2016-02-23 10:39:36 -0800120// We use a spinlock to make locking this in a signal handler _somewhat_ safe.
mtklein15923c92016-02-29 10:14:38 -0800121static SkSpinlock gMutex;
mtklein3eed7dd2016-02-24 19:07:07 -0800122static int32_t gPending;
123static SkTArray<SkString> gRunning;
mtklein748ca3b2015-01-15 10:56:12 -0800124
mtklein3eed7dd2016-02-24 19:07:07 -0800125static void done(const char* config, const char* src, const char* srcOptions, const char* name) {
126 SkString id = SkStringPrintf("%s %s %s %s", config, src, srcOptions, name);
mtkleinc41fd922016-03-08 09:01:39 -0800127 vlog("done %s\n", id.c_str());
mtkleinafae30a2016-02-24 12:28:32 -0800128 int pending;
mtkleinb37cb412015-03-18 05:27:14 -0700129 {
mtkleinf10637f2016-06-10 13:56:35 -0700130 SkAutoMutexAcquire lock(gMutex);
mtkleinb37cb412015-03-18 05:27:14 -0700131 for (int i = 0; i < gRunning.count(); i++) {
132 if (gRunning[i] == id) {
133 gRunning.removeShuffle(i);
134 break;
135 }
136 }
mtkleinafae30a2016-02-24 12:28:32 -0800137 pending = --gPending;
reed50bc0512015-05-19 14:13:31 -0700138 }
mtkleina17241b2015-01-23 05:48:00 -0800139 // We write our dm.json file every once in a while in case we crash.
140 // Notice this also handles the final dm.json when pending == 0.
141 if (pending % 500 == 0) {
142 JsonWriter::DumpJson();
143 }
mtklein@google.comd36522d2013-10-16 13:02:15 +0000144}
145
mtklein3eed7dd2016-02-24 19:07:07 -0800146static void start(const char* config, const char* src, const char* srcOptions, const char* name) {
147 SkString id = SkStringPrintf("%s %s %s %s", config, src, srcOptions, name);
mtkleinc41fd922016-03-08 09:01:39 -0800148 vlog("start %s\n", id.c_str());
mtkleinf10637f2016-06-10 13:56:35 -0700149 SkAutoMutexAcquire lock(gMutex);
mtkleinb37cb412015-03-18 05:27:14 -0700150 gRunning.push_back(id);
151}
152
mtkleinafae30a2016-02-24 12:28:32 -0800153static void print_status() {
mtkleinafae30a2016-02-24 12:28:32 -0800154 int curr = sk_tools::getCurrResidentSetSizeMB(),
155 peak = sk_tools::getMaxResidentSetSizeMB();
mtkleinc41fd922016-03-08 09:01:39 -0800156 SkString elapsed = HumanizeMs(SkTime::GetMSecs() - kStartMs);
mtkleinafae30a2016-02-24 12:28:32 -0800157
mtkleinf10637f2016-06-10 13:56:35 -0700158 SkAutoMutexAcquire lock(gMutex);
mtkleinc41fd922016-03-08 09:01:39 -0800159 info("\n%s elapsed, %d active, %d queued, %dMB RAM, %dMB peak\n",
160 elapsed.c_str(), gRunning.count(), gPending - gRunning.count(), curr, peak);
mtkleinafae30a2016-02-24 12:28:32 -0800161 for (auto& task : gRunning) {
mtkleinc41fd922016-03-08 09:01:39 -0800162 info("\t%s\n", task.c_str());
mtkleinafae30a2016-02-24 12:28:32 -0800163 }
164}
165
mtklein246ba3a2016-02-23 10:39:36 -0800166#if defined(SK_BUILD_FOR_WIN32)
mtklein5b64dab2016-06-09 08:59:48 -0700167 static LONG WINAPI crash_handler(EXCEPTION_POINTERS* e) {
mtkleinf8557292016-02-29 06:35:28 -0800168 static const struct {
169 const char* name;
robertphillips75467862016-03-01 14:10:23 -0800170 DWORD code;
mtkleinf8557292016-02-29 06:35:28 -0800171 } kExceptions[] = {
172 #define _(E) {#E, E}
173 _(EXCEPTION_ACCESS_VIOLATION),
174 _(EXCEPTION_BREAKPOINT),
175 _(EXCEPTION_INT_DIVIDE_BY_ZERO),
176 _(EXCEPTION_STACK_OVERFLOW),
177 // TODO: more?
178 #undef _
179 };
180
mtkleinf10637f2016-06-10 13:56:35 -0700181 SkAutoMutexAcquire lock(gMutex);
mtklein5b64dab2016-06-09 08:59:48 -0700182
183 const DWORD code = e->ExceptionRecord->ExceptionCode;
184 info("\nCaught exception %u", code);
185 for (const auto& exception : kExceptions) {
186 if (exception.code == code) {
187 info(" %s", exception.name);
mtkleinf8557292016-02-29 06:35:28 -0800188 }
mtkleinf8557292016-02-29 06:35:28 -0800189 }
mtklein5b64dab2016-06-09 08:59:48 -0700190 info(", was running:\n");
191 for (auto& task : gRunning) {
192 info("\t%s\n", task.c_str());
193 }
194 fflush(stdout);
195
mtkleinf8557292016-02-29 06:35:28 -0800196 // Execute default exception handler... hopefully, exit.
197 return EXCEPTION_EXECUTE_HANDLER;
mtklein246ba3a2016-02-23 10:39:36 -0800198 }
mtklein5b64dab2016-06-09 08:59:48 -0700199 static void setup_crash_handler() { SetUnhandledExceptionFilter(crash_handler); }
mtklein246ba3a2016-02-23 10:39:36 -0800200
mtklein819ab102016-02-29 17:02:52 -0800201#elif !defined(SK_BUILD_FOR_ANDROID)
202 #include <execinfo.h>
mtklein246ba3a2016-02-23 10:39:36 -0800203 #include <signal.h>
mtklein819ab102016-02-29 17:02:52 -0800204 #include <stdlib.h>
mtkleinf8557292016-02-29 06:35:28 -0800205
mtklein5b64dab2016-06-09 08:59:48 -0700206 static void crash_handler(int sig) {
mtkleinf10637f2016-06-10 13:56:35 -0700207 SkAutoMutexAcquire lock(gMutex);
mtklein5b64dab2016-06-09 08:59:48 -0700208
209 info("\nCaught signal %d [%s], was running:\n", sig, strsignal(sig));
210 for (auto& task : gRunning) {
211 info("\t%s\n", task.c_str());
212 }
213
214 void* stack[64];
215 int count = backtrace(stack, SK_ARRAY_COUNT(stack));
216 char** symbols = backtrace_symbols(stack, count);
217 info("\nStack trace:\n");
218 for (int i = 0; i < count; i++) {
219 info(" %s\n", symbols[i]);
220 }
221 fflush(stdout);
222
223 _Exit(sig);
224 }
225
mtklein246ba3a2016-02-23 10:39:36 -0800226 static void setup_crash_handler() {
227 const int kSignals[] = { SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGSEGV };
228 for (int sig : kSignals) {
mtklein5b64dab2016-06-09 08:59:48 -0700229 signal(sig, crash_handler);
mtklein246ba3a2016-02-23 10:39:36 -0800230 }
231 }
mtklein819ab102016-02-29 17:02:52 -0800232
233#else // Android
234 static void setup_crash_handler() {}
mtklein246ba3a2016-02-23 10:39:36 -0800235#endif
236
mtklein748ca3b2015-01-15 10:56:12 -0800237/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
mtklein114c3cd2015-01-15 10:15:02 -0800238
mtklein62bd1a62015-01-27 14:46:26 -0800239struct Gold : public SkString {
mtkleina82f5622015-02-20 12:30:19 -0800240 Gold() : SkString("") {}
mtklein3eed7dd2016-02-24 19:07:07 -0800241 Gold(const SkString& sink, const SkString& src,
242 const SkString& srcOptions, const SkString& name,
243 const SkString& md5)
mtklein62bd1a62015-01-27 14:46:26 -0800244 : SkString("") {
245 this->append(sink);
246 this->append(src);
djsollen54416de2015-04-03 07:24:48 -0700247 this->append(srcOptions);
mtklein62bd1a62015-01-27 14:46:26 -0800248 this->append(name);
249 this->append(md5);
mtklein62bd1a62015-01-27 14:46:26 -0800250 }
mtkleinc8d1dd42015-10-15 12:23:01 -0700251 struct Hash {
252 uint32_t operator()(const Gold& g) const {
253 return SkGoodHash()((const SkString&)g);
254 }
255 };
mtklein62bd1a62015-01-27 14:46:26 -0800256};
mtkleina82f5622015-02-20 12:30:19 -0800257static SkTHashSet<Gold, Gold::Hash> gGold;
mtklein62bd1a62015-01-27 14:46:26 -0800258
259static void add_gold(JsonWriter::BitmapResult r) {
djsollen54416de2015-04-03 07:24:48 -0700260 gGold.add(Gold(r.config, r.sourceType, r.sourceOptions, r.name, r.md5));
mtklein62bd1a62015-01-27 14:46:26 -0800261}
262
263static void gather_gold() {
264 if (!FLAGS_readPath.isEmpty()) {
265 SkString path(FLAGS_readPath[0]);
266 path.append("/dm.json");
267 if (!JsonWriter::ReadJson(path.c_str(), add_gold)) {
268 fail(SkStringPrintf("Couldn't read %s for golden results.", path.c_str()));
269 }
270 }
271}
272
273/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
274
mtklein935f1b12016-03-16 08:37:19 -0700275#if defined(SK_BUILD_FOR_WIN32)
276 static const char* kNewline = "\r\n";
277#else
278 static const char* kNewline = "\n";
279#endif
280
borenet09ed4802015-04-03 14:15:33 -0700281static SkTHashSet<SkString> gUninterestingHashes;
282
283static void gather_uninteresting_hashes() {
284 if (!FLAGS_uninterestingHashesFile.isEmpty()) {
285 SkAutoTUnref<SkData> data(SkData::NewFromFileName(FLAGS_uninterestingHashesFile[0]));
mtkleincc334b32015-09-22 11:43:53 -0700286 if (!data) {
mtkleinc41fd922016-03-08 09:01:39 -0800287 info("WARNING: unable to read uninteresting hashes from %s\n",
288 FLAGS_uninterestingHashesFile[0]);
mtkleincc334b32015-09-22 11:43:53 -0700289 return;
290 }
borenet09ed4802015-04-03 14:15:33 -0700291 SkTArray<SkString> hashes;
mtklein935f1b12016-03-16 08:37:19 -0700292 SkStrSplit((const char*)data->data(), kNewline, &hashes);
borenet09ed4802015-04-03 14:15:33 -0700293 for (const SkString& hash : hashes) {
294 gUninterestingHashes.add(hash);
295 }
mtkleinc41fd922016-03-08 09:01:39 -0800296 info("FYI: loaded %d distinct uninteresting hashes from %d lines\n",
297 gUninterestingHashes.count(), hashes.count());
borenet09ed4802015-04-03 14:15:33 -0700298 }
299}
300
301/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
302
mtkleine0effd62015-07-29 06:37:28 -0700303struct TaggedSrc : public SkAutoTDelete<Src> {
mtklein3eed7dd2016-02-24 19:07:07 -0800304 SkString tag;
305 SkString options;
mtkleine0effd62015-07-29 06:37:28 -0700306};
307
308struct TaggedSink : public SkAutoTDelete<Sink> {
kkinnunen3e980c32015-12-23 01:33:00 -0800309 SkString tag;
djsollen54416de2015-04-03 07:24:48 -0700310};
mtklein748ca3b2015-01-15 10:56:12 -0800311
312static const bool kMemcpyOK = true;
313
mtkleine0effd62015-07-29 06:37:28 -0700314static SkTArray<TaggedSrc, kMemcpyOK> gSrcs;
315static SkTArray<TaggedSink, kMemcpyOK> gSinks;
mtklein748ca3b2015-01-15 10:56:12 -0800316
mtklein6393c062015-04-27 08:45:01 -0700317static bool in_shard() {
318 static int N = 0;
319 return N++ % FLAGS_shards == FLAGS_shard;
320}
321
mtklein3eed7dd2016-02-24 19:07:07 -0800322static void push_src(const char* tag, ImplicitString options, Src* s) {
mtklein748ca3b2015-01-15 10:56:12 -0800323 SkAutoTDelete<Src> src(s);
mtklein6393c062015-04-27 08:45:01 -0700324 if (in_shard() &&
mtklein3eed7dd2016-02-24 19:07:07 -0800325 FLAGS_src.contains(tag) &&
mtklein748ca3b2015-01-15 10:56:12 -0800326 !SkCommandLineFlags::ShouldSkip(FLAGS_match, src->name().c_str())) {
mtkleine0effd62015-07-29 06:37:28 -0700327 TaggedSrc& s = gSrcs.push_back();
mtklein18300a32016-03-16 13:53:35 -0700328 s.reset(src.release());
mtklein748ca3b2015-01-15 10:56:12 -0800329 s.tag = tag;
djsollen54416de2015-04-03 07:24:48 -0700330 s.options = options;
mtklein114c3cd2015-01-15 10:15:02 -0800331 }
mtklein748ca3b2015-01-15 10:56:12 -0800332}
mtklein114c3cd2015-01-15 10:15:02 -0800333
msarett9e707a02015-09-01 14:57:57 -0700334static void push_codec_src(Path path, CodecSrc::Mode mode, CodecSrc::DstColorType dstColorType,
scroggoc5560be2016-02-03 09:42:42 -0800335 SkAlphaType dstAlphaType, float scale) {
scroggoe4499842016-02-25 11:03:47 -0800336 if (FLAGS_simpleCodec) {
337 if (mode != CodecSrc::kCodec_Mode || dstColorType != CodecSrc::kGetFromCanvas_DstColorType
338 || scale != 1.0f)
339 // Only decode in the simple case.
340 return;
341 }
msarett9e707a02015-09-01 14:57:57 -0700342 SkString folder;
343 switch (mode) {
344 case CodecSrc::kCodec_Mode:
345 folder.append("codec");
346 break;
msarettbb25b532016-01-13 09:31:39 -0800347 case CodecSrc::kCodecZeroInit_Mode:
348 folder.append("codec_zero_init");
349 break;
msarett9e707a02015-09-01 14:57:57 -0700350 case CodecSrc::kScanline_Mode:
351 folder.append("scanline");
352 break;
msarett9e707a02015-09-01 14:57:57 -0700353 case CodecSrc::kStripe_Mode:
354 folder.append("stripe");
355 break;
msarett91c22b22016-02-22 12:27:46 -0800356 case CodecSrc::kCroppedScanline_Mode:
357 folder.append("crop");
358 break;
msarett9e707a02015-09-01 14:57:57 -0700359 case CodecSrc::kSubset_Mode:
msarett36c37962015-09-02 13:20:52 -0700360 folder.append("codec_subset");
msarett9e707a02015-09-01 14:57:57 -0700361 break;
362 }
363
364 switch (dstColorType) {
365 case CodecSrc::kGrayscale_Always_DstColorType:
366 folder.append("_kGray8");
367 break;
368 case CodecSrc::kIndex8_Always_DstColorType:
369 folder.append("_kIndex8");
370 break;
msarett34e0ec42016-04-22 16:27:24 -0700371 case CodecSrc::kNonNative8888_Always_DstColorType:
372 folder.append("_kNonNative");
373 break;
msarett9e707a02015-09-01 14:57:57 -0700374 default:
375 break;
376 }
377
scroggoc5560be2016-02-03 09:42:42 -0800378 switch (dstAlphaType) {
scroggoc5560be2016-02-03 09:42:42 -0800379 case kPremul_SkAlphaType:
380 folder.append("_premul");
381 break;
382 case kUnpremul_SkAlphaType:
383 folder.append("_unpremul");
384 break;
385 default:
386 break;
387 }
388
msarett9e707a02015-09-01 14:57:57 -0700389 if (1.0f != scale) {
390 folder.appendf("_%.3f", scale);
391 }
392
scroggoc5560be2016-02-03 09:42:42 -0800393 CodecSrc* src = new CodecSrc(path, mode, dstColorType, dstAlphaType, scale);
msarett9e707a02015-09-01 14:57:57 -0700394 push_src("image", folder, src);
395}
396
scroggof8dc9df2016-05-16 09:04:13 -0700397static void push_android_codec_src(Path path, CodecSrc::DstColorType dstColorType,
398 SkAlphaType dstAlphaType, int sampleSize) {
msarett3d9d7a72015-10-21 10:27:10 -0700399 SkString folder;
scroggof8dc9df2016-05-16 09:04:13 -0700400 folder.append("scaled_codec");
msarett3d9d7a72015-10-21 10:27:10 -0700401
402 switch (dstColorType) {
403 case CodecSrc::kGrayscale_Always_DstColorType:
404 folder.append("_kGray8");
405 break;
406 case CodecSrc::kIndex8_Always_DstColorType:
407 folder.append("_kIndex8");
408 break;
msarett34e0ec42016-04-22 16:27:24 -0700409 case CodecSrc::kNonNative8888_Always_DstColorType:
410 folder.append("_kNonNative");
411 break;
msarett3d9d7a72015-10-21 10:27:10 -0700412 default:
413 break;
414 }
415
scroggoc5560be2016-02-03 09:42:42 -0800416 switch (dstAlphaType) {
scroggoc5560be2016-02-03 09:42:42 -0800417 case kPremul_SkAlphaType:
418 folder.append("_premul");
419 break;
msarett9e9444c2016-02-03 12:39:10 -0800420 case kUnpremul_SkAlphaType:
421 folder.append("_unpremul");
422 break;
scroggoc5560be2016-02-03 09:42:42 -0800423 default:
424 break;
425 }
426
msarett3d9d7a72015-10-21 10:27:10 -0700427 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800428 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700429 }
430
scroggof8dc9df2016-05-16 09:04:13 -0700431 AndroidCodecSrc* src = new AndroidCodecSrc(path, dstColorType, dstAlphaType, sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700432 push_src("image", folder, src);
433}
434
msarett18976312016-03-09 14:20:58 -0800435static void push_image_gen_src(Path path, ImageGenSrc::Mode mode, SkAlphaType alphaType, bool isGpu)
436{
437 SkString folder;
438 switch (mode) {
439 case ImageGenSrc::kCodec_Mode:
440 folder.append("gen_codec");
441 break;
442 case ImageGenSrc::kPlatform_Mode:
443 folder.append("gen_platform");
444 break;
445 }
446
447 if (isGpu) {
448 folder.append("_gpu");
449 } else {
450 switch (alphaType) {
451 case kOpaque_SkAlphaType:
452 folder.append("_opaque");
453 break;
454 case kPremul_SkAlphaType:
455 folder.append("_premul");
456 break;
457 case kUnpremul_SkAlphaType:
458 folder.append("_unpremul");
459 break;
460 default:
461 break;
462 }
463 }
464
465 ImageGenSrc* src = new ImageGenSrc(path, mode, alphaType, isGpu);
466 push_src("image", folder, src);
467}
468
msarett438b2ad2015-04-09 12:43:10 -0700469static void push_codec_srcs(Path path) {
470 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
471 if (!encoded) {
mtkleinc41fd922016-03-08 09:01:39 -0800472 info("Couldn't read %s.", path.c_str());
msarett438b2ad2015-04-09 12:43:10 -0700473 return;
474 }
475 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
halcanary96fcdcc2015-08-27 07:41:13 -0700476 if (nullptr == codec.get()) {
mtkleinc41fd922016-03-08 09:01:39 -0800477 info("Couldn't create codec for %s.", path.c_str());
msarett438b2ad2015-04-09 12:43:10 -0700478 return;
479 }
480
scroggo9366aff2016-05-20 08:38:54 -0700481 // native scaling is only supported by WEBP and JPEG
482 bool supportsNativeScaling = false;
msarett438b2ad2015-04-09 12:43:10 -0700483
msarett91c22b22016-02-22 12:27:46 -0800484 SkTArray<CodecSrc::Mode> nativeModes;
485 nativeModes.push_back(CodecSrc::kCodec_Mode);
486 nativeModes.push_back(CodecSrc::kCodecZeroInit_Mode);
msarett91c22b22016-02-22 12:27:46 -0800487 switch (codec->getEncodedFormat()) {
488 case SkEncodedFormat::kJPEG_SkEncodedFormat:
489 nativeModes.push_back(CodecSrc::kScanline_Mode);
490 nativeModes.push_back(CodecSrc::kStripe_Mode);
491 nativeModes.push_back(CodecSrc::kCroppedScanline_Mode);
scroggo9366aff2016-05-20 08:38:54 -0700492 supportsNativeScaling = true;
msarett91c22b22016-02-22 12:27:46 -0800493 break;
494 case SkEncodedFormat::kWEBP_SkEncodedFormat:
495 nativeModes.push_back(CodecSrc::kSubset_Mode);
scroggo9366aff2016-05-20 08:38:54 -0700496 supportsNativeScaling = true;
msarett91c22b22016-02-22 12:27:46 -0800497 break;
yujieqin7a307df2016-03-11 07:32:33 -0800498 case SkEncodedFormat::kDNG_SkEncodedFormat:
msarettb65e6042016-02-23 05:37:25 -0800499 break;
msarett91c22b22016-02-22 12:27:46 -0800500 default:
501 nativeModes.push_back(CodecSrc::kScanline_Mode);
msarett91c22b22016-02-22 12:27:46 -0800502 break;
503 }
msarett9e707a02015-09-01 14:57:57 -0700504
msarett91c22b22016-02-22 12:27:46 -0800505 SkTArray<CodecSrc::DstColorType> colorTypes;
506 colorTypes.push_back(CodecSrc::kGetFromCanvas_DstColorType);
msarett34e0ec42016-04-22 16:27:24 -0700507 colorTypes.push_back(CodecSrc::kNonNative8888_Always_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700508 switch (codec->getInfo().colorType()) {
509 case kGray_8_SkColorType:
msarett91c22b22016-02-22 12:27:46 -0800510 colorTypes.push_back(CodecSrc::kGrayscale_Always_DstColorType);
msarett55f7bdd2016-02-16 13:24:54 -0800511 if (kWBMP_SkEncodedFormat == codec->getEncodedFormat()) {
msarett91c22b22016-02-22 12:27:46 -0800512 colorTypes.push_back(CodecSrc::kIndex8_Always_DstColorType);
msarett55f7bdd2016-02-16 13:24:54 -0800513 }
msarett36c37962015-09-02 13:20:52 -0700514 break;
515 case kIndex_8_SkColorType:
msarett91c22b22016-02-22 12:27:46 -0800516 colorTypes.push_back(CodecSrc::kIndex8_Always_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700517 break;
518 default:
msarett36c37962015-09-02 13:20:52 -0700519 break;
520 }
msarett9e707a02015-09-01 14:57:57 -0700521
scroggoc5560be2016-02-03 09:42:42 -0800522 SkTArray<SkAlphaType> alphaModes;
523 alphaModes.push_back(kPremul_SkAlphaType);
scroggof2c96a22016-05-20 11:08:27 -0700524 if (codec->getInfo().alphaType() != kOpaque_SkAlphaType) {
525 alphaModes.push_back(kUnpremul_SkAlphaType);
scroggoc5560be2016-02-03 09:42:42 -0800526 }
msarettb714fb02016-01-22 14:46:42 -0800527
528 for (CodecSrc::Mode mode : nativeModes) {
scroggo9366aff2016-05-20 08:38:54 -0700529 for (CodecSrc::DstColorType colorType : colorTypes) {
530 for (SkAlphaType alphaType : alphaModes) {
scroggof2c96a22016-05-20 11:08:27 -0700531 // Only test kCroppedScanline_Mode when the alpha type is premul. The test is
scroggo9366aff2016-05-20 08:38:54 -0700532 // slow and won't be interestingly different with different alpha types.
533 if (CodecSrc::kCroppedScanline_Mode == mode &&
scroggof2c96a22016-05-20 11:08:27 -0700534 kPremul_SkAlphaType != alphaType) {
scroggo9366aff2016-05-20 08:38:54 -0700535 continue;
536 }
msarett91c22b22016-02-22 12:27:46 -0800537
scroggo9366aff2016-05-20 08:38:54 -0700538 push_codec_src(path, mode, colorType, alphaType, 1.0f);
msarettf3dc1882016-04-26 13:06:38 -0700539
scroggo9366aff2016-05-20 08:38:54 -0700540 // Skip kNonNative on different native scales. It won't be interestingly
541 // different.
542 if (supportsNativeScaling &&
543 CodecSrc::kNonNative8888_Always_DstColorType == colorType) {
544 // Native Scales
545 // SkJpegCodec natively supports scaling to the following:
546 for (auto scale : { 0.125f, 0.25f, 0.375f, 0.5f, 0.625f, 0.750f, 0.875f }) {
547 push_codec_src(path, mode, colorType, alphaType, scale);
548 }
scroggoc5560be2016-02-03 09:42:42 -0800549 }
msarett9e707a02015-09-01 14:57:57 -0700550 }
551 }
msarett0a242972015-06-11 14:27:27 -0700552 }
msarett36c37962015-09-02 13:20:52 -0700553
scroggoe4499842016-02-25 11:03:47 -0800554 if (FLAGS_simpleCodec) {
555 return;
556 }
557
msarett3d9d7a72015-10-21 10:27:10 -0700558 const int sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
msarett36c37962015-09-02 13:20:52 -0700559
msarett3d9d7a72015-10-21 10:27:10 -0700560 for (int sampleSize : sampleSizes) {
msarett91c22b22016-02-22 12:27:46 -0800561 for (CodecSrc::DstColorType colorType : colorTypes) {
scroggoc5560be2016-02-03 09:42:42 -0800562 for (SkAlphaType alphaType : alphaModes) {
msarettf3dc1882016-04-26 13:06:38 -0700563 // We can exercise all of the kNonNative support code in the swizzler with just a
564 // few sample sizes. Skip the rest.
565 if (CodecSrc::kNonNative8888_Always_DstColorType == colorType && sampleSize > 3) {
566 continue;
567 }
568
scroggof8dc9df2016-05-16 09:04:13 -0700569 push_android_codec_src(path, colorType, alphaType, sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700570 }
msarett36c37962015-09-02 13:20:52 -0700571 }
572 }
msarett18976312016-03-09 14:20:58 -0800573
574 static const char* const rawExts[] = {
575 "arw", "cr2", "dng", "nef", "nrw", "orf", "raf", "rw2", "pef", "srw",
576 "ARW", "CR2", "DNG", "NEF", "NRW", "ORF", "RAF", "RW2", "PEF", "SRW",
577 };
578
579 // There is not currently a reason to test RAW images on image generator.
580 // If we want to enable these tests, we will need to fix skbug.com/5079.
581 for (const char* ext : rawExts) {
582 if (path.endsWith(ext)) {
583 return;
584 }
585 }
586
587 // Push image generator GPU test.
brianosmanc9ced392016-03-24 07:27:43 -0700588 push_image_gen_src(path, ImageGenSrc::kCodec_Mode, codec->getInfo().alphaType(), true);
msarett18976312016-03-09 14:20:58 -0800589
590 // Push image generator CPU tests.
591 for (SkAlphaType alphaType : alphaModes) {
592 push_image_gen_src(path, ImageGenSrc::kCodec_Mode, alphaType, false);
593
594#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
595 if (kWEBP_SkEncodedFormat != codec->getEncodedFormat() &&
596 kWBMP_SkEncodedFormat != codec->getEncodedFormat() &&
597 kUnpremul_SkAlphaType != alphaType)
598 {
599 push_image_gen_src(path, ImageGenSrc::kPlatform_Mode, alphaType, false);
600 }
msarettfc0b6d12016-03-17 13:50:17 -0700601#elif defined(SK_BUILD_FOR_WIN)
602 if (kWEBP_SkEncodedFormat != codec->getEncodedFormat() &&
603 kWBMP_SkEncodedFormat != codec->getEncodedFormat())
604 {
605 push_image_gen_src(path, ImageGenSrc::kPlatform_Mode, alphaType, false);
606 }
msarett18976312016-03-09 14:20:58 -0800607#endif
608 }
msarett438b2ad2015-04-09 12:43:10 -0700609}
610
msarettd1227a72016-05-18 06:23:57 -0700611static void push_brd_src(Path path, CodecSrc::DstColorType dstColorType, BRDSrc::Mode mode,
612 uint32_t sampleSize) {
613 SkString folder("brd_android_codec");
msaretta5783ae2015-09-08 15:35:32 -0700614 switch (mode) {
615 case BRDSrc::kFullImage_Mode:
616 break;
617 case BRDSrc::kDivisor_Mode:
618 folder.append("_divisor");
619 break;
620 default:
621 SkASSERT(false);
622 return;
623 }
624
625 switch (dstColorType) {
626 case CodecSrc::kGetFromCanvas_DstColorType:
627 break;
628 case CodecSrc::kIndex8_Always_DstColorType:
629 folder.append("_kIndex");
630 break;
631 case CodecSrc::kGrayscale_Always_DstColorType:
632 folder.append("_kGray");
633 break;
634 default:
635 SkASSERT(false);
636 return;
637 }
638
639 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800640 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700641 }
642
msarettd1227a72016-05-18 06:23:57 -0700643 BRDSrc* src = new BRDSrc(path, mode, dstColorType, sampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700644 push_src("image", folder, src);
645}
646
647static void push_brd_srcs(Path path) {
scroggo24a9bd72016-05-20 06:02:42 -0700648 // Only run Index8 and grayscale to one sampleSize and Mode. Though interesting
649 // to test these color types, they should not reveal anything across various
650 // sampleSizes and Modes
651 for (auto type : { CodecSrc::kIndex8_Always_DstColorType,
652 CodecSrc::kGrayscale_Always_DstColorType }) {
653 // Arbitrarily choose Mode and sampleSize.
654 push_brd_src(path, type, BRDSrc::kFullImage_Mode, 2);
655 }
656
657
scroggo501b7342015-11-03 07:55:11 -0800658 // Test on a variety of sampleSizes, making sure to include:
659 // - 2, 4, and 8, which are natively supported by jpeg
660 // - multiples of 2 which are not divisible by 4 (analogous for 4)
661 // - larger powers of two, since BRD clients generally use powers of 2
662 // We will only produce output for the larger sizes on large images.
663 const uint32_t sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 24, 32, 64 };
msarett6efbe052015-09-11 09:01:16 -0700664
msaretta5783ae2015-09-08 15:35:32 -0700665 const BRDSrc::Mode modes[] = {
msarett26ad17b2015-10-22 07:29:19 -0700666 BRDSrc::kFullImage_Mode,
667 BRDSrc::kDivisor_Mode,
msaretta5783ae2015-09-08 15:35:32 -0700668 };
669
msarettd1227a72016-05-18 06:23:57 -0700670 for (uint32_t sampleSize : sampleSizes) {
scroggo24a9bd72016-05-20 06:02:42 -0700671 for (BRDSrc::Mode mode : modes) {
672 push_brd_src(path, CodecSrc::kGetFromCanvas_DstColorType, mode, sampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700673 }
674 }
675}
676
677static bool brd_supported(const char* ext) {
msarett8c8f22a2015-04-01 06:58:48 -0700678 static const char* const exts[] = {
msaretta5783ae2015-09-08 15:35:32 -0700679 "jpg", "jpeg", "png", "webp",
680 "JPG", "JPEG", "PNG", "WEBP",
msarett8c8f22a2015-04-01 06:58:48 -0700681 };
682
683 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
684 if (0 == strcmp(exts[i], ext)) {
685 return true;
686 }
687 }
688 return false;
scroggo9b77ddd2015-03-19 06:03:39 -0700689}
690
scroggo86737142016-02-03 12:19:11 -0800691static bool gather_srcs() {
mtklein748ca3b2015-01-15 10:56:12 -0800692 for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) {
djsollen54416de2015-04-03 07:24:48 -0700693 push_src("gm", "", new GMSrc(r->factory()));
mtklein748ca3b2015-01-15 10:56:12 -0800694 }
halcanaryfc37ad12015-01-30 07:31:19 -0800695 for (int i = 0; i < FLAGS_skps.count(); i++) {
696 const char* path = FLAGS_skps[i];
697 if (sk_isdir(path)) {
698 SkOSFile::Iter it(path, "skp");
699 for (SkString file; it.next(&file); ) {
djsollen54416de2015-04-03 07:24:48 -0700700 push_src("skp", "", new SKPSrc(SkOSPath::Join(path, file.c_str())));
halcanaryfc37ad12015-01-30 07:31:19 -0800701 }
702 } else {
djsollen54416de2015-04-03 07:24:48 -0700703 push_src("skp", "", new SKPSrc(path));
mtklein114c3cd2015-01-15 10:15:02 -0800704 }
705 }
scroggo86737142016-02-03 12:19:11 -0800706
halcanary45420a92016-06-02 12:41:14 -0700707 for (int i = 0; i < FLAGS_mskps.count(); i++) {
708 const char* path = FLAGS_mskps[i];
709 if (sk_isdir(path)) {
710 SkOSFile::Iter it(path, "mskp");
711 for (SkString file; it.next(&file);) {
712 push_src("mskp", "",
713 new MSKPSrc(SkOSPath::Join(path, file.c_str())));
714 }
715 } else {
716 push_src("mskp", "", new MSKPSrc(path));
717 }
718 }
719
scroggo86737142016-02-03 12:19:11 -0800720 SkTArray<SkString> images;
msarett69deca82016-04-29 09:38:40 -0700721 if (!CollectImages(FLAGS_images, &images)) {
scroggo86737142016-02-03 12:19:11 -0800722 return false;
723 }
724
725 for (auto image : images) {
726 push_codec_srcs(image);
scroggoe4499842016-02-25 11:03:47 -0800727 if (FLAGS_simpleCodec) {
728 continue;
729 }
730
mtklein21eaf3b2016-02-08 12:39:59 -0800731 const char* ext = strrchr(image.c_str(), '.');
732 if (ext && brd_supported(ext+1)) {
scroggo86737142016-02-03 12:19:11 -0800733 push_brd_srcs(image);
mtklein709d2c32015-01-15 08:30:25 -0800734 }
mtklein709d2c32015-01-15 08:30:25 -0800735 }
scroggo86737142016-02-03 12:19:11 -0800736
msarett69deca82016-04-29 09:38:40 -0700737 SkTArray<SkString> colorImages;
738 if (!CollectImages(FLAGS_colorImages, &colorImages)) {
739 return false;
740 }
741
742 for (auto colorImage : colorImages) {
msarett9ce3a542016-07-15 13:54:38 -0700743 ColorCodecSrc* src = new ColorCodecSrc(colorImage, ColorCodecSrc::kBaseline_Mode,
744 kN32_SkColorType);
msarett69deca82016-04-29 09:38:40 -0700745 push_src("image", "color_codec_baseline", src);
msarett888dc162016-05-23 10:21:17 -0700746
msarett9ce3a542016-07-15 13:54:38 -0700747 src = new ColorCodecSrc(colorImage, ColorCodecSrc::kDst_HPZR30w_Mode, kN32_SkColorType);
msarett888dc162016-05-23 10:21:17 -0700748 push_src("image", "color_codec_HPZR30w", src);
msarett9ce3a542016-07-15 13:54:38 -0700749 // TODO (msarett):
750 // 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 -0700751
msarett9ce3a542016-07-15 13:54:38 -0700752 src = new ColorCodecSrc(colorImage, ColorCodecSrc::kDst_sRGB_Mode, kN32_SkColorType);
753 push_src("image", "color_codec_sRGB_kN32", src);
754 src = new ColorCodecSrc(colorImage, ColorCodecSrc::kDst_sRGB_Mode, kRGBA_F16_SkColorType);
755 push_src("image", "color_codec_sRGB_kF16", src);
msarettd2809572016-06-20 06:07:45 -0700756
msarett469f1c52016-06-06 08:20:37 -0700757#if defined(SK_TEST_QCMS)
msarett9ce3a542016-07-15 13:54:38 -0700758 src = new ColorCodecSrc(colorImage, ColorCodecSrc::kQCMS_HPZR30w_Mode, kN32_SkColorType);
msarett9876ac52016-06-01 14:47:18 -0700759 push_src("image", "color_codec_QCMS_HPZR30w", src);
760#endif
msarett69deca82016-04-29 09:38:40 -0700761 }
762
scroggo86737142016-02-03 12:19:11 -0800763 return true;
mtklein709d2c32015-01-15 08:30:25 -0800764}
765
kkinnunen3e980c32015-12-23 01:33:00 -0800766static void push_sink(const SkCommandLineConfig& config, Sink* s) {
rmistry0f515bd2015-12-22 10:22:26 -0800767 SkAutoTDelete<Sink> sink(s);
kkinnunen3e980c32015-12-23 01:33:00 -0800768
mtkleine0effd62015-07-29 06:37:28 -0700769 // Try a simple Src as a canary. If it fails, skip this sink.
mtklein748ca3b2015-01-15 10:56:12 -0800770 struct : public Src {
mtkleine0effd62015-07-29 06:37:28 -0700771 Error draw(SkCanvas* c) const override {
772 c->drawRect(SkRect::MakeWH(1,1), SkPaint());
773 return "";
774 }
mtklein36352bf2015-03-25 18:17:31 -0700775 SkISize size() const override { return SkISize::Make(16, 16); }
mtkleine0effd62015-07-29 06:37:28 -0700776 Name name() const override { return "justOneRect"; }
777 } justOneRect;
mtklein114c3cd2015-01-15 10:15:02 -0800778
mtklein748ca3b2015-01-15 10:56:12 -0800779 SkBitmap bitmap;
780 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800781 SkString log;
mtkleine0effd62015-07-29 06:37:28 -0700782 Error err = sink->draw(justOneRect, &bitmap, &stream, &log);
mtklein4089ef72015-03-05 08:40:28 -0800783 if (err.isFatal()) {
mtkleinc41fd922016-03-08 09:01:39 -0800784 info("Could not run %s: %s\n", config.getTag().c_str(), err.c_str());
mtklein05641a52015-04-21 10:49:13 -0700785 exit(1);
mtklein114c3cd2015-01-15 10:15:02 -0800786 }
787
mtkleine0effd62015-07-29 06:37:28 -0700788 TaggedSink& ts = gSinks.push_back();
mtklein18300a32016-03-16 13:53:35 -0700789 ts.reset(sink.release());
kkinnunen3e980c32015-12-23 01:33:00 -0800790 ts.tag = config.getTag();
mtklein748ca3b2015-01-15 10:56:12 -0800791}
792
793static bool gpu_supported() {
794#if SK_SUPPORT_GPU
795 return FLAGS_gpu;
796#else
797 return false;
798#endif
799}
kkinnunen9ebc3f02015-12-21 23:48:13 -0800800
kkinnunen3e980c32015-12-23 01:33:00 -0800801static Sink* create_sink(const SkCommandLineConfig* config) {
802#if SK_SUPPORT_GPU
803 if (gpu_supported()) {
804 if (const SkCommandLineConfigGpu* gpuConfig = config->asConfigGpu()) {
bsalomon85b4b532016-04-05 11:06:27 -0700805 GrContextFactory::ContextType contextType = gpuConfig->getContextType();
806 GrContextFactory::ContextOptions contextOptions =
807 GrContextFactory::kNone_ContextOptions;
kkinnunen3e980c32015-12-23 01:33:00 -0800808 if (gpuConfig->getUseNVPR()) {
bsalomon85b4b532016-04-05 11:06:27 -0700809 contextOptions = static_cast<GrContextFactory::ContextOptions>(
810 contextOptions | GrContextFactory::kEnableNVPR_ContextOptions);
kkinnunen3e980c32015-12-23 01:33:00 -0800811 }
brianosmanb109b8c2016-06-16 13:03:24 -0700812 if (SkColorAndColorSpaceAreGammaCorrect(gpuConfig->getColorType(),
813 gpuConfig->getColorSpace())) {
bsalomon85b4b532016-04-05 11:06:27 -0700814 contextOptions = static_cast<GrContextFactory::ContextOptions>(
815 contextOptions | GrContextFactory::kRequireSRGBSupport_ContextOptions);
brianosman61d3b082016-03-30 11:19:36 -0700816 }
kkinnunen3e980c32015-12-23 01:33:00 -0800817 GrContextFactory testFactory;
818 if (!testFactory.get(contextType, contextOptions)) {
mtkleinc41fd922016-03-08 09:01:39 -0800819 info("WARNING: can not create GPU context for config '%s'. "
820 "GM tests will be skipped.\n", gpuConfig->getTag().c_str());
kkinnunen3e980c32015-12-23 01:33:00 -0800821 return nullptr;
822 }
823 return new GPUSink(contextType, contextOptions, gpuConfig->getSamples(),
brianosmand93c1202016-03-10 07:49:08 -0800824 gpuConfig->getUseDIText(), gpuConfig->getColorType(),
brianosmanb109b8c2016-06-16 13:03:24 -0700825 sk_ref_sp(gpuConfig->getColorSpace()), FLAGS_gpu_threading);
kkinnunen3e980c32015-12-23 01:33:00 -0800826 }
827 }
828#endif
829
830#define SINK(t, sink, ...) if (config->getBackend().equals(t)) { return new sink(__VA_ARGS__); }
831
tomhudsoneebc39a2015-02-23 12:18:05 -0800832#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
833 SINK("hwui", HWUISink);
834#endif
835
mtklein748ca3b2015-01-15 10:56:12 -0800836 if (FLAGS_cpu) {
brianosmanb109b8c2016-06-16 13:03:24 -0700837 auto srgbColorSpace = SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named);
838
mtklein748ca3b2015-01-15 10:56:12 -0800839 SINK("565", RasterSink, kRGB_565_SkColorType);
840 SINK("8888", RasterSink, kN32_SkColorType);
brianosmanb109b8c2016-06-16 13:03:24 -0700841 SINK("srgb", RasterSink, kN32_SkColorType, srgbColorSpace);
mtklein27c3fdd2016-02-26 14:43:21 -0800842 SINK("f16", RasterSink, kRGBA_F16_SkColorType);
halcanary94b87bd2016-04-04 06:12:15 -0700843 SINK("pdf", PDFSink);
mtklein9c3f17d2015-01-28 11:35:18 -0800844 SINK("skp", SKPSink);
mtklein8a4527e2015-01-31 20:00:58 -0800845 SINK("svg", SVGSink);
846 SINK("null", NullSink);
halcanary47ef4d52015-03-03 09:13:09 -0800847 SINK("xps", XPSSink);
halcanary4b656662016-04-27 07:45:18 -0700848 SINK("pdfa", PDFSink, true);
mtklein748ca3b2015-01-15 10:56:12 -0800849 }
850#undef SINK
halcanary96fcdcc2015-08-27 07:41:13 -0700851 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800852}
853
kkinnunen3e980c32015-12-23 01:33:00 -0800854static Sink* create_via(const SkString& tag, Sink* wrapped) {
855#define VIA(t, via, ...) if (tag.equals(t)) { return new via(__VA_ARGS__); }
halcanary96fcdcc2015-08-27 07:41:13 -0700856 VIA("twice", ViaTwice, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700857 VIA("serialize", ViaSerialization, wrapped);
mtklein4a34ecb2016-01-08 10:19:35 -0800858 VIA("pic", ViaPicture, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700859 VIA("2ndpic", ViaSecondPicture, wrapped);
mtkleind31c13d2015-05-05 12:59:56 -0700860 VIA("sp", ViaSingletonPictures, wrapped);
reedbabc3de2016-07-08 08:43:27 -0700861 VIA("defer", ViaDefer, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700862 VIA("tiles", ViaTiles, 256, 256, nullptr, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800863 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800864
mtkleind603b222015-02-17 11:13:33 -0800865 if (FLAGS_matrix.count() == 4) {
mtklein748ca3b2015-01-15 10:56:12 -0800866 SkMatrix m;
mtkleind603b222015-02-17 11:13:33 -0800867 m.reset();
868 m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
869 m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
870 m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
871 m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
872 VIA("matrix", ViaMatrix, m, wrapped);
873 VIA("upright", ViaUpright, m, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800874 }
tomhudson64de1e12015-03-05 08:01:07 -0800875
876#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
877 VIA("androidsdk", ViaAndroidSDK, wrapped);
878#endif
879
mtklein748ca3b2015-01-15 10:56:12 -0800880#undef VIA
halcanary96fcdcc2015-08-27 07:41:13 -0700881 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800882}
883
brianosman05c987c2016-05-09 13:42:16 -0700884static bool gather_sinks() {
kkinnunen3e980c32015-12-23 01:33:00 -0800885 SkCommandLineConfigArray configs;
886 ParseConfigs(FLAGS_config, &configs);
887 for (int i = 0; i < configs.count(); i++) {
888 const SkCommandLineConfig& config = *configs[i];
889 Sink* sink = create_sink(&config);
890 if (sink == nullptr) {
mtkleinc41fd922016-03-08 09:01:39 -0800891 info("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
892 config.getTag().c_str());
kkinnunen3e980c32015-12-23 01:33:00 -0800893 continue;
894 }
mtklein748ca3b2015-01-15 10:56:12 -0800895
kkinnunen3e980c32015-12-23 01:33:00 -0800896 const SkTArray<SkString>& parts = config.getViaParts();
897 for (int j = parts.count(); j-- > 0;) {
898 const SkString& part = parts[j];
899 Sink* next = create_via(part, sink);
halcanary96fcdcc2015-08-27 07:41:13 -0700900 if (next == nullptr) {
mtkleinc41fd922016-03-08 09:01:39 -0800901 info("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
902 part.c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800903 delete sink;
halcanary96fcdcc2015-08-27 07:41:13 -0700904 sink = nullptr;
mtklein748ca3b2015-01-15 10:56:12 -0800905 break;
906 }
907 sink = next;
908 }
909 if (sink) {
910 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800911 }
912 }
brianosman05c987c2016-05-09 13:42:16 -0700913
914 // If no configs were requested (just running tests, perhaps?), then we're okay.
915 // Otherwise, make sure that at least one sink was constructed correctly. This catches
916 // the case of bots without a GPU being assigned GPU configs.
917 return (configs.count() == 0) || (gSinks.count() > 0);
mtklein114c3cd2015-01-15 10:15:02 -0800918}
mtklein709d2c32015-01-15 08:30:25 -0800919
mtkleina5114d72015-08-24 13:27:01 -0700920static bool dump_png(SkBitmap bitmap, const char* path, const char* md5) {
921 const int w = bitmap.width(),
922 h = bitmap.height();
923
brianosman3c579dc2016-04-19 09:18:11 -0700924 sk_sp<SkData> encodedBitmap = sk_tools::encode_bitmap_for_png(bitmap);
925 if (encodedBitmap.get() == nullptr) {
926 return false;
mtkleina5114d72015-08-24 13:27:01 -0700927 }
brianosman3c579dc2016-04-19 09:18:11 -0700928 uint32_t* rgba = static_cast<uint32_t*>(encodedBitmap.get()->writable_data());
mtkleina5114d72015-08-24 13:27:01 -0700929
930 // We don't need bitmap anymore. Might as well drop our ref.
mtkleinfccb77d2015-08-24 14:13:29 -0700931 bitmap.reset();
mtkleina5114d72015-08-24 13:27:01 -0700932
mtkleinc64137c2015-08-25 10:56:08 -0700933 FILE* f = fopen(path, "wb");
mtkleina5114d72015-08-24 13:27:01 -0700934 if (!f) { return false; }
935
936 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
937 if (!png) {
938 fclose(f);
939 return false;
940 }
941
942 png_infop info = png_create_info_struct(png);
943 if (!info) {
944 png_destroy_write_struct(&png, &info);
945 fclose(f);
946 return false;
947 }
948
mtkleind69ece62015-09-10 10:37:44 -0700949 SkString description;
950 description.append("Key: ");
951 for (int i = 0; i < FLAGS_key.count(); i++) {
952 description.appendf("%s ", FLAGS_key[i]);
953 }
954 description.append("Properties: ");
955 for (int i = 0; i < FLAGS_properties.count(); i++) {
956 description.appendf("%s ", FLAGS_properties[i]);
957 }
958 description.appendf("MD5: %s", md5);
959
mtkleina5114d72015-08-24 13:27:01 -0700960 png_text text[2];
961 text[0].key = (png_charp)"Author";
962 text[0].text = (png_charp)"DM dump_png()";
963 text[0].compression = PNG_TEXT_COMPRESSION_NONE;
964 text[1].key = (png_charp)"Description";
mtkleind69ece62015-09-10 10:37:44 -0700965 text[1].text = (png_charp)description.c_str();
mtkleina5114d72015-08-24 13:27:01 -0700966 text[1].compression = PNG_TEXT_COMPRESSION_NONE;
967 png_set_text(png, info, text, 2);
968
969 png_init_io(png, f);
970 png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8,
971 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
972 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
973 png_write_info(png, info);
974 for (int j = 0; j < h; j++) {
brianosman3c579dc2016-04-19 09:18:11 -0700975 png_bytep row = (png_bytep)(rgba + w*j);
mtkleina5114d72015-08-24 13:27:01 -0700976 png_write_rows(png, &row, 1);
977 }
978 png_write_end(png, info);
979
980 png_destroy_write_struct(&png, &info);
981 fclose(f);
982 return true;
983}
984
mtkleina2ef6422015-01-15 13:44:22 -0800985static bool match(const char* needle, const char* haystack) {
halcanary96fcdcc2015-08-27 07:41:13 -0700986 return 0 == strcmp("_", needle) || nullptr != strstr(haystack, needle);
mtkleina2ef6422015-01-15 13:44:22 -0800987}
988
mtklein3eed7dd2016-02-24 19:07:07 -0800989static bool is_blacklisted(const char* sink, const char* src,
990 const char* srcOptions, const char* name) {
mtklein0d243ff2015-04-03 07:51:00 -0700991 for (int i = 0; i < FLAGS_blacklist.count() - 3; i += 4) {
mtkleina2ef6422015-01-15 13:44:22 -0800992 if (match(FLAGS_blacklist[i+0], sink) &&
djsollen54416de2015-04-03 07:24:48 -0700993 match(FLAGS_blacklist[i+1], src) &&
994 match(FLAGS_blacklist[i+2], srcOptions) &&
995 match(FLAGS_blacklist[i+3], name)) {
mtklein3eed7dd2016-02-24 19:07:07 -0800996 return true;
mtkleina2ef6422015-01-15 13:44:22 -0800997 }
998 }
mtklein3eed7dd2016-02-24 19:07:07 -0800999 return false;
mtkleina2ef6422015-01-15 13:44:22 -08001000}
1001
mtkleincd50bca2016-01-05 06:20:20 -08001002// Even when a Task Sink reports to be non-threadsafe (e.g. GPU), we know things like
1003// .png encoding are definitely thread safe. This lets us offload that work to CPU threads.
1004static SkTaskGroup gDefinitelyThreadSafeWork;
1005
mtklein748ca3b2015-01-15 10:56:12 -08001006// The finest-grained unit of work we can run: draw a single Src into a single Sink,
1007// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
1008struct Task {
mtkleine0effd62015-07-29 06:37:28 -07001009 Task(const TaggedSrc& src, const TaggedSink& sink) : src(src), sink(sink) {}
1010 const TaggedSrc& src;
1011 const TaggedSink& sink;
mtklein748ca3b2015-01-15 10:56:12 -08001012
mtklein21eaf3b2016-02-08 12:39:59 -08001013 static void Run(const Task& task) {
1014 SkString name = task.src->name();
mtkleine0effd62015-07-29 06:37:28 -07001015
mtkleinb9eb4ac2015-02-02 18:26:03 -08001016 SkString log;
mtklein3eed7dd2016-02-24 19:07:07 -08001017 if (!FLAGS_dryRun) {
mtklein748ca3b2015-01-15 10:56:12 -08001018 SkBitmap bitmap;
1019 SkDynamicMemoryWStream stream;
mtklein3eed7dd2016-02-24 19:07:07 -08001020 start(task.sink.tag.c_str(), task.src.tag.c_str(),
1021 task.src.options.c_str(), name.c_str());
mtklein21eaf3b2016-02-08 12:39:59 -08001022 Error err = task.sink->draw(*task.src, &bitmap, &stream, &log);
mtkleinb3b13b72016-03-07 13:20:52 -08001023 if (!log.isEmpty()) {
mtkleinc41fd922016-03-08 09:01:39 -08001024 info("%s %s %s %s:\n%s\n", task.sink.tag.c_str()
1025 , task.src.tag.c_str()
1026 , task.src.options.c_str()
1027 , name.c_str()
1028 , log.c_str());
mtkleinb3b13b72016-03-07 13:20:52 -08001029 }
mtklein748ca3b2015-01-15 10:56:12 -08001030 if (!err.isEmpty()) {
mtklein4089ef72015-03-05 08:40:28 -08001031 if (err.isFatal()) {
djsollen54416de2015-04-03 07:24:48 -07001032 fail(SkStringPrintf("%s %s %s %s: %s",
mtklein21eaf3b2016-02-08 12:39:59 -08001033 task.sink.tag.c_str(),
1034 task.src.tag.c_str(),
1035 task.src.options.c_str(),
mtklein4089ef72015-03-05 08:40:28 -08001036 name.c_str(),
1037 err.c_str()));
mtkleinb37cb412015-03-18 05:27:14 -07001038 } else {
mtklein3eed7dd2016-02-24 19:07:07 -08001039 done(task.sink.tag.c_str(), task.src.tag.c_str(),
1040 task.src.options.c_str(), name.c_str());
mtkleinba6ada72016-01-21 09:39:35 -08001041 return;
mtklein4089ef72015-03-05 08:40:28 -08001042 }
mtklein748ca3b2015-01-15 10:56:12 -08001043 }
mtklein62bd1a62015-01-27 14:46:26 -08001044
mtkleincd50bca2016-01-05 06:20:20 -08001045 // We're likely switching threads here, so we must capture by value, [=] or [foo,bar].
1046 SkStreamAsset* data = stream.detachAsStream();
1047 gDefinitelyThreadSafeWork.add([task,name,bitmap,data]{
1048 SkAutoTDelete<SkStreamAsset> ownedData(data);
1049
1050 // Why doesn't the copy constructor do this when we have pre-locked pixels?
1051 bitmap.lockPixels();
1052
1053 SkString md5;
1054 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
1055 SkMD5 hash;
1056 if (data->getLength()) {
1057 hash.writeStream(data, data->getLength());
1058 data->rewind();
mtklein38408462015-07-08 07:25:27 -07001059 } else {
mtkleincd50bca2016-01-05 06:20:20 -08001060 // If we're BGRA (Linux, Windows), swizzle over to RGBA (Mac, Android).
1061 // This helps eliminate multiple 0-pixel-diff hashes on gold.skia.org.
1062 // (Android's general slow speed breaks the tie arbitrarily in RGBA's favor.)
1063 // We might consider promoting 565 to RGBA too.
1064 if (bitmap.colorType() == kBGRA_8888_SkColorType) {
1065 SkBitmap swizzle;
1066 SkAssertResult(bitmap.copyTo(&swizzle, kRGBA_8888_SkColorType));
1067 hash.write(swizzle.getPixels(), swizzle.getSize());
1068 } else {
1069 hash.write(bitmap.getPixels(), bitmap.getSize());
1070 }
1071 }
1072 SkMD5::Digest digest;
1073 hash.finish(digest);
1074 for (int i = 0; i < 16; i++) {
1075 md5.appendf("%02x", digest.data[i]);
mtklein38408462015-07-08 07:25:27 -07001076 }
mtklein62bd1a62015-01-27 14:46:26 -08001077 }
mtklein62bd1a62015-01-27 14:46:26 -08001078
mtkleincd50bca2016-01-05 06:20:20 -08001079 if (!FLAGS_readPath.isEmpty() &&
mtklein3eed7dd2016-02-24 19:07:07 -08001080 !gGold.contains(Gold(task.sink.tag, task.src.tag,
1081 task.src.options, name, md5))) {
mtkleincd50bca2016-01-05 06:20:20 -08001082 fail(SkStringPrintf("%s not found for %s %s %s %s in %s",
1083 md5.c_str(),
mtklein21eaf3b2016-02-08 12:39:59 -08001084 task.sink.tag.c_str(),
1085 task.src.tag.c_str(),
1086 task.src.options.c_str(),
mtkleincd50bca2016-01-05 06:20:20 -08001087 name.c_str(),
1088 FLAGS_readPath[0]));
mtklein748ca3b2015-01-15 10:56:12 -08001089 }
mtkleincd50bca2016-01-05 06:20:20 -08001090
1091 if (!FLAGS_writePath.isEmpty()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001092 const char* ext = task.sink->fileExtension();
mtkleincd50bca2016-01-05 06:20:20 -08001093 if (data->getLength()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001094 WriteToDisk(task, md5, ext, data, data->getLength(), nullptr);
mtkleincd50bca2016-01-05 06:20:20 -08001095 SkASSERT(bitmap.drawsNothing());
1096 } else if (!bitmap.drawsNothing()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001097 WriteToDisk(task, md5, ext, nullptr, 0, &bitmap);
mtkleincd50bca2016-01-05 06:20:20 -08001098 }
1099 }
1100 });
mtklein748ca3b2015-01-15 10:56:12 -08001101 }
mtklein3eed7dd2016-02-24 19:07:07 -08001102 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 -08001103 }
1104
1105 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -08001106 SkString md5,
1107 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -08001108 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -08001109 const SkBitmap* bitmap) {
mtklein409d4702016-02-29 07:38:01 -08001110 bool gammaCorrect = false;
1111 if (bitmap) {
brianosman898235c2016-04-06 07:38:23 -07001112 gammaCorrect = SkImageInfoIsGammaCorrect(bitmap->info());
mtklein409d4702016-02-29 07:38:01 -08001113 }
1114
mtklein748ca3b2015-01-15 10:56:12 -08001115 JsonWriter::BitmapResult result;
djsollen54416de2015-04-03 07:24:48 -07001116 result.name = task.src->name();
mtklein3eed7dd2016-02-24 19:07:07 -08001117 result.config = task.sink.tag;
djsollen54416de2015-04-03 07:24:48 -07001118 result.sourceType = task.src.tag;
1119 result.sourceOptions = task.src.options;
1120 result.ext = ext;
mtklein409d4702016-02-29 07:38:01 -08001121 result.gammaCorrect = gammaCorrect;
djsollen54416de2015-04-03 07:24:48 -07001122 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -08001123 JsonWriter::AddBitmapResult(result);
1124
mtkleinb0531a72015-04-07 13:38:48 -07001125 // If an MD5 is uninteresting, we want it noted in the JSON file,
1126 // but don't want to dump it out as a .png (or whatever ext is).
1127 if (gUninterestingHashes.contains(md5)) {
1128 return;
1129 }
1130
mtklein748ca3b2015-01-15 10:56:12 -08001131 const char* dir = FLAGS_writePath[0];
1132 if (0 == strcmp(dir, "@")) { // Needed for iOS.
1133 dir = FLAGS_resourcePath[0];
1134 }
1135 sk_mkdir(dir);
1136
1137 SkString path;
1138 if (FLAGS_nameByHash) {
1139 path = SkOSPath::Join(dir, result.md5.c_str());
1140 path.append(".");
1141 path.append(ext);
1142 if (sk_exists(path.c_str())) {
1143 return; // Content-addressed. If it exists already, we're done.
1144 }
1145 } else {
kkinnunen3e980c32015-12-23 01:33:00 -08001146 path = SkOSPath::Join(dir, task.sink.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001147 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001148 path = SkOSPath::Join(path.c_str(), task.src.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001149 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001150 if (strcmp(task.src.options.c_str(), "") != 0) {
1151 path = SkOSPath::Join(path.c_str(), task.src.options.c_str());
djsollen54416de2015-04-03 07:24:48 -07001152 sk_mkdir(path.c_str());
1153 }
mtklein748ca3b2015-01-15 10:56:12 -08001154 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
1155 path.append(".");
1156 path.append(ext);
1157 }
1158
mtklein748ca3b2015-01-15 10:56:12 -08001159 if (bitmap) {
mtkleina5114d72015-08-24 13:27:01 -07001160 if (!dump_png(*bitmap, path.c_str(), result.md5.c_str())) {
mtklein748ca3b2015-01-15 10:56:12 -08001161 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
1162 return;
1163 }
1164 } else {
mtkleina5114d72015-08-24 13:27:01 -07001165 SkFILEWStream file(path.c_str());
1166 if (!file.isValid()) {
1167 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
1168 return;
1169 }
mtklein748ca3b2015-01-15 10:56:12 -08001170 if (!file.writeStream(data, len)) {
1171 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
1172 return;
1173 }
1174 }
1175 }
1176};
1177
mtklein748ca3b2015-01-15 10:56:12 -08001178/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1179
1180// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
1181
mtklein21eaf3b2016-02-08 12:39:59 -08001182static SkTDArray<skiatest::Test> gParallelTests, gSerialTests;
mtklein748ca3b2015-01-15 10:56:12 -08001183
1184static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -08001185 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -08001186 return;
1187 }
mtklein6393c062015-04-27 08:45:01 -07001188 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r; r = r->next()) {
1189 if (!in_shard()) {
1190 continue;
1191 }
halcanary87f3ba42015-01-20 09:30:20 -08001192 // Despite its name, factory() is returning a reference to
1193 // link-time static const POD data.
1194 const skiatest::Test& test = r->factory();
1195 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -08001196 continue;
1197 }
halcanary87f3ba42015-01-20 09:30:20 -08001198 if (test.needsGpu && gpu_supported()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001199 (FLAGS_gpu_threading ? gParallelTests : gSerialTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -08001200 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein21eaf3b2016-02-08 12:39:59 -08001201 gParallelTests.push(test);
mtklein82d28432015-01-15 12:46:02 -08001202 }
mtklein748ca3b2015-01-15 10:56:12 -08001203 }
1204}
1205
mtklein21eaf3b2016-02-08 12:39:59 -08001206static void run_test(skiatest::Test test) {
halcanary87f3ba42015-01-20 09:30:20 -08001207 struct : public skiatest::Reporter {
mtklein36352bf2015-03-25 18:17:31 -07001208 void reportFailed(const skiatest::Failure& failure) override {
halcanary87f3ba42015-01-20 09:30:20 -08001209 fail(failure.toString());
1210 JsonWriter::AddTestFailure(failure);
1211 }
mtklein36352bf2015-03-25 18:17:31 -07001212 bool allowExtendedTest() const override {
halcanary87f3ba42015-01-20 09:30:20 -08001213 return FLAGS_pathOpsExtended;
1214 }
mtklein36352bf2015-03-25 18:17:31 -07001215 bool verbose() const override { return FLAGS_veryVerbose; }
halcanary87f3ba42015-01-20 09:30:20 -08001216 } reporter;
djsollen824996a2015-06-12 12:06:22 -07001217
mtklein3eed7dd2016-02-24 19:07:07 -08001218 if (!FLAGS_dryRun && !is_blacklisted("_", "tests", "_", test.name)) {
mtklein21eaf3b2016-02-08 12:39:59 -08001219 start("unit", "test", "", test.name);
mtklein55e88b22015-01-21 15:50:13 -08001220 GrContextFactory factory;
mtklein21eaf3b2016-02-08 12:39:59 -08001221 test.proc(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -08001222 }
mtklein3eed7dd2016-02-24 19:07:07 -08001223 done("unit", "test", "", test.name);
mtklein748ca3b2015-01-15 10:56:12 -08001224}
1225
1226/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1227
mtkleinafae30a2016-02-24 12:28:32 -08001228DEFINE_int32(status_sec, 15, "Print status this often (and if we crash).");
1229
1230SkThread* start_status_thread() {
1231 auto thread = new SkThread([] (void*) {
1232 for (;;) {
1233 print_status();
1234 #if defined(SK_BUILD_FOR_WIN)
1235 Sleep(FLAGS_status_sec * 1000);
1236 #else
1237 sleep(FLAGS_status_sec);
1238 #endif
mtklein2e1c47e2015-03-12 07:16:56 -07001239 }
mtkleinafae30a2016-02-24 12:28:32 -08001240 });
1241 thread->start();
1242 return thread;
mtkleinde6fc2b2015-03-12 06:28:54 -07001243}
1244
caryclark83ca6282015-06-10 09:31:09 -07001245#define PORTABLE_FONT_PREFIX "Toy Liberation "
1246
mbocee6a9912016-05-31 11:42:36 -07001247static sk_sp<SkTypeface> create_from_name(const char familyName[], SkFontStyle style) {
caryclark83ca6282015-06-10 09:31:09 -07001248 if (familyName && strlen(familyName) > sizeof(PORTABLE_FONT_PREFIX)
1249 && !strncmp(familyName, PORTABLE_FONT_PREFIX, sizeof(PORTABLE_FONT_PREFIX) - 1)) {
caryclark1818acb2015-07-24 12:09:25 -07001250 return sk_tool_utils::create_portable_typeface(familyName, style);
caryclark83ca6282015-06-10 09:31:09 -07001251 }
halcanary96fcdcc2015-08-27 07:41:13 -07001252 return nullptr;
caryclark83ca6282015-06-10 09:31:09 -07001253}
1254
1255#undef PORTABLE_FONT_PREFIX
1256
mbocee6a9912016-05-31 11:42:36 -07001257extern sk_sp<SkTypeface> (*gCreateTypefaceDelegate)(const char [], SkFontStyle );
caryclark83ca6282015-06-10 09:31:09 -07001258
jcgregorio3b27ade2014-11-13 08:06:40 -08001259int dm_main();
caryclark17f0b6d2014-07-22 10:15:34 -07001260int dm_main() {
bsalomon71de3532016-04-26 14:27:21 -07001261 setbuf(stdout, nullptr);
mtklein246ba3a2016-02-23 10:39:36 -08001262 setup_crash_handler();
mtkleinafae30a2016-02-24 12:28:32 -08001263
mtkleina483da32016-03-17 12:53:36 -07001264 if (FLAGS_verbose) {
1265 gVLog = stderr;
1266 } else if (!FLAGS_writePath.isEmpty()) {
mtklein51c8cfc2016-03-11 12:59:09 -08001267 sk_mkdir(FLAGS_writePath[0]);
1268 gVLog = freopen(SkOSPath::Join(FLAGS_writePath[0], "verbose.log").c_str(), "w", stderr);
mtkleinc41fd922016-03-08 09:01:39 -08001269 }
1270
mtklein5286f022016-01-22 08:18:14 -08001271 JsonWriter::DumpJson(); // It's handy for the bots to assume this is ~never missing.
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +00001272 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -07001273 SkTaskGroup::Enabler enabled(FLAGS_threads);
caryclark83ca6282015-06-10 09:31:09 -07001274 gCreateTypefaceDelegate = &create_from_name;
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +00001275
halcanary7d571242016-02-24 17:59:16 -08001276 {
1277 SkString testResourcePath = GetResourcePath("color_wheel.png");
1278 SkFILEStream testResource(testResourcePath.c_str());
1279 if (!testResource.isValid()) {
mtkleinc41fd922016-03-08 09:01:39 -08001280 info("Some resources are missing. Do you need to set --resourcePath?\n");
halcanary7d571242016-02-24 17:59:16 -08001281 }
1282 }
mtklein62bd1a62015-01-27 14:46:26 -08001283 gather_gold();
borenet09ed4802015-04-03 14:15:33 -07001284 gather_uninteresting_hashes();
mtklein62bd1a62015-01-27 14:46:26 -08001285
scroggo86737142016-02-03 12:19:11 -08001286 if (!gather_srcs()) {
1287 return 1;
1288 }
brianosman05c987c2016-05-09 13:42:16 -07001289 if (!gather_sinks()) {
1290 return 1;
1291 }
mtklein748ca3b2015-01-15 10:56:12 -08001292 gather_tests();
mtklein21eaf3b2016-02-08 12:39:59 -08001293 gPending = gSrcs.count() * gSinks.count() + gParallelTests.count() + gSerialTests.count();
mtkleinc41fd922016-03-08 09:01:39 -08001294 info("%d srcs * %d sinks + %d tests == %d tasks",
1295 gSrcs.count(), gSinks.count(), gParallelTests.count() + gSerialTests.count(), gPending);
mtkleinafae30a2016-02-24 12:28:32 -08001296 SkAutoTDelete<SkThread> statusThread(start_status_thread());
mtklein748ca3b2015-01-15 10:56:12 -08001297
mtklein21eaf3b2016-02-08 12:39:59 -08001298 // Kick off as much parallel work as we can, making note of any serial work we'll need to do.
1299 SkTaskGroup parallel;
1300 SkTArray<Task> serial;
1301
1302 for (auto& sink : gSinks)
1303 for (auto& src : gSrcs) {
mtklein3eed7dd2016-02-24 19:07:07 -08001304 if (src->veto(sink->flags()) ||
1305 is_blacklisted(sink.tag.c_str(), src.tag.c_str(),
1306 src.options.c_str(), src->name().c_str())) {
mtkleinf10637f2016-06-10 13:56:35 -07001307 SkAutoMutexAcquire lock(gMutex);
mtklein3eed7dd2016-02-24 19:07:07 -08001308 gPending--;
1309 continue;
1310 }
1311
mtklein21eaf3b2016-02-08 12:39:59 -08001312 Task task(src, sink);
1313 if (src->serial() || sink->serial()) {
1314 serial.push_back(task);
1315 } else {
1316 parallel.add([task] { Task::Run(task); });
mtklein748ca3b2015-01-15 10:56:12 -08001317 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001318 }
mtklein21eaf3b2016-02-08 12:39:59 -08001319 for (auto test : gParallelTests) {
1320 parallel.add([test] { run_test(test); });
mtklein55e88b22015-01-21 15:50:13 -08001321 }
mtklein21eaf3b2016-02-08 12:39:59 -08001322
1323 // With the parallel work running, run serial tasks and tests here on main thread.
1324 for (auto task : serial) { Task::Run(task); }
1325 for (auto test : gSerialTests) { run_test(test); }
1326
1327 // Wait for any remaining parallel work to complete (including any spun off of serial tasks).
1328 parallel.wait();
mtkleincd50bca2016-01-05 06:20:20 -08001329 gDefinitelyThreadSafeWork.wait();
1330
mtkleinda884c42016-02-24 18:00:23 -08001331 // We'd better have run everything.
1332 SkASSERT(gPending == 0);
mtkleine027f172016-02-26 15:53:06 -08001333 // Make sure we've flushed all our results to disk.
1334 JsonWriter::DumpJson();
mtkleinda884c42016-02-24 18:00:23 -08001335
mtklein748ca3b2015-01-15 10:56:12 -08001336 // At this point we're back in single-threaded land.
caryclarkf53ce802015-06-15 06:48:30 -07001337 sk_tool_utils::release_portable_typefaces();
mtklein@google.comd36522d2013-10-16 13:02:15 +00001338
mtklein748ca3b2015-01-15 10:56:12 -08001339 if (gFailures.count() > 0) {
mtkleinc41fd922016-03-08 09:01:39 -08001340 info("Failures:\n");
mtklein748ca3b2015-01-15 10:56:12 -08001341 for (int i = 0; i < gFailures.count(); i++) {
mtkleinc41fd922016-03-08 09:01:39 -08001342 info("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001343 }
mtkleinc41fd922016-03-08 09:01:39 -08001344 info("%d failures\n", gFailures.count());
mtklein748ca3b2015-01-15 10:56:12 -08001345 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001346 }
mtkleinafae30a2016-02-24 12:28:32 -08001347
1348#ifdef SK_PDF_IMAGE_STATS
halcanary7a14b312015-10-01 07:28:13 -07001349 SkPDFImageDumpStats();
mtkleinafae30a2016-02-24 12:28:32 -08001350#endif // SK_PDF_IMAGE_STATS
1351
1352 print_status();
mtkleinc41fd922016-03-08 09:01:39 -08001353 info("Finished!\n");
mtklein748ca3b2015-01-15 10:56:12 -08001354 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +00001355}
jcgregorio3b27ade2014-11-13 08:06:40 -08001356
kkinnunen179a8f52015-11-20 13:32:24 -08001357// TODO: currently many GPU tests are declared outside SK_SUPPORT_GPU guards.
1358// Thus we export the empty RunWithGPUTestContexts when SK_SUPPORT_GPU=0.
1359namespace skiatest {
bsalomon758586c2016-04-06 14:02:39 -07001360
1361#if SK_SUPPORT_GPU
1362bool IsGLContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1363 return kOpenGL_GrBackend == GrContextFactory::ContextTypeBackend(type);
1364}
bsalomondc0fcd42016-04-11 14:21:33 -07001365bool IsVulkanContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1366 return kVulkan_GrBackend == GrContextFactory::ContextTypeBackend(type);
1367}
bsalomon758586c2016-04-06 14:02:39 -07001368bool IsRenderingGLContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1369 return IsGLContextType(type) && GrContextFactory::IsRenderingContext(type);
1370}
1371bool IsNullGLContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1372 return type == GrContextFactory::kNullGL_ContextType;
1373}
1374#else
1375bool IsGLContextType(int) { return false; }
bsalomondc0fcd42016-04-11 14:21:33 -07001376bool IsVulkanContextType(int) { return false; }
bsalomon758586c2016-04-06 14:02:39 -07001377bool IsRenderingGLContextType(int) { return false; }
1378bool IsNullGLContextType(int) { return false; }
1379#endif
1380
1381void RunWithGPUTestContexts(GrContextTestFn* test, GrContextTypeFilterFn* contextTypeFilter,
bsalomonf2f1c172016-04-05 12:59:06 -07001382 Reporter* reporter, GrContextFactory* factory) {
kkinnunen179a8f52015-11-20 13:32:24 -08001383#if SK_SUPPORT_GPU
kkinnunen3e980c32015-12-23 01:33:00 -08001384
bsalomon758586c2016-04-06 14:02:39 -07001385 for (int typeInt = 0; typeInt < GrContextFactory::kContextTypeCnt; ++typeInt) {
1386 GrContextFactory::ContextType contextType = (GrContextFactory::ContextType) typeInt;
1387 ContextInfo ctxInfo = factory->getContextInfo(contextType);
bsalomonfda88072016-04-11 14:40:50 -07001388 if (contextTypeFilter && !(*contextTypeFilter)(contextType)) {
kkinnunen179a8f52015-11-20 13:32:24 -08001389 continue;
1390 }
bsalomon758586c2016-04-06 14:02:39 -07001391 // Use "native" instead of explicitly trying OpenGL and OpenGL ES. Do not use GLES on,
1392 // desktop since tests do not account for not fixing http://skbug.com/2809
1393 if (contextType == GrContextFactory::kGL_ContextType ||
1394 contextType == GrContextFactory::kGLES_ContextType) {
1395 if (contextType != GrContextFactory::kNativeGL_ContextType) {
1396 continue;
1397 }
1398 }
bsalomon8b7451a2016-05-11 06:33:06 -07001399 if (ctxInfo.grContext()) {
bsalomonf2f1c172016-04-05 12:59:06 -07001400 (*test)(reporter, ctxInfo);
kkinnunen5219fd92015-12-10 06:28:13 -08001401 }
bsalomonf2f1c172016-04-05 12:59:06 -07001402 ctxInfo = factory->getContextInfo(contextType,
bsalomon85b4b532016-04-05 11:06:27 -07001403 GrContextFactory::kEnableNVPR_ContextOptions);
bsalomon8b7451a2016-05-11 06:33:06 -07001404 if (ctxInfo.grContext()) {
bsalomonf2f1c172016-04-05 12:59:06 -07001405 (*test)(reporter, ctxInfo);
kkinnunen179a8f52015-11-20 13:32:24 -08001406 }
1407 }
1408#endif
1409}
kkinnunen179a8f52015-11-20 13:32:24 -08001410} // namespace skiatest
1411
borenet48087572015-04-02 12:16:36 -07001412#if !defined(SK_BUILD_FOR_IOS)
jcgregorio3b27ade2014-11-13 08:06:40 -08001413int main(int argc, char** argv) {
1414 SkCommandLineFlags::Parse(argc, argv);
1415 return dm_main();
1416}
1417#endif