blob: eb61fd786b543087a88885759d92b22ff9c5307e [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) {
msarett9876ac52016-06-01 14:47:18 -0700743 ColorCodecSrc* src = new ColorCodecSrc(colorImage, ColorCodecSrc::kBaseline_Mode);
msarett69deca82016-04-29 09:38:40 -0700744 push_src("image", "color_codec_baseline", src);
msarett888dc162016-05-23 10:21:17 -0700745
msarett9876ac52016-06-01 14:47:18 -0700746 src = new ColorCodecSrc(colorImage, ColorCodecSrc::kDst_HPZR30w_Mode);
msarett888dc162016-05-23 10:21:17 -0700747 push_src("image", "color_codec_HPZR30w", src);
msarett9876ac52016-06-01 14:47:18 -0700748
msarettd2809572016-06-20 06:07:45 -0700749 src = new ColorCodecSrc(colorImage, ColorCodecSrc::kDst_sRGB_Mode);
750 push_src("image", "color_codec_sRGB", src);
751
msarett469f1c52016-06-06 08:20:37 -0700752#if defined(SK_TEST_QCMS)
msarett9876ac52016-06-01 14:47:18 -0700753 src = new ColorCodecSrc(colorImage, ColorCodecSrc::kQCMS_HPZR30w_Mode);
754 push_src("image", "color_codec_QCMS_HPZR30w", src);
755#endif
msarett69deca82016-04-29 09:38:40 -0700756 }
757
scroggo86737142016-02-03 12:19:11 -0800758 return true;
mtklein709d2c32015-01-15 08:30:25 -0800759}
760
kkinnunen3e980c32015-12-23 01:33:00 -0800761static void push_sink(const SkCommandLineConfig& config, Sink* s) {
rmistry0f515bd2015-12-22 10:22:26 -0800762 SkAutoTDelete<Sink> sink(s);
kkinnunen3e980c32015-12-23 01:33:00 -0800763
mtkleine0effd62015-07-29 06:37:28 -0700764 // Try a simple Src as a canary. If it fails, skip this sink.
mtklein748ca3b2015-01-15 10:56:12 -0800765 struct : public Src {
mtkleine0effd62015-07-29 06:37:28 -0700766 Error draw(SkCanvas* c) const override {
767 c->drawRect(SkRect::MakeWH(1,1), SkPaint());
768 return "";
769 }
mtklein36352bf2015-03-25 18:17:31 -0700770 SkISize size() const override { return SkISize::Make(16, 16); }
mtkleine0effd62015-07-29 06:37:28 -0700771 Name name() const override { return "justOneRect"; }
772 } justOneRect;
mtklein114c3cd2015-01-15 10:15:02 -0800773
mtklein748ca3b2015-01-15 10:56:12 -0800774 SkBitmap bitmap;
775 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800776 SkString log;
mtkleine0effd62015-07-29 06:37:28 -0700777 Error err = sink->draw(justOneRect, &bitmap, &stream, &log);
mtklein4089ef72015-03-05 08:40:28 -0800778 if (err.isFatal()) {
mtkleinc41fd922016-03-08 09:01:39 -0800779 info("Could not run %s: %s\n", config.getTag().c_str(), err.c_str());
mtklein05641a52015-04-21 10:49:13 -0700780 exit(1);
mtklein114c3cd2015-01-15 10:15:02 -0800781 }
782
mtkleine0effd62015-07-29 06:37:28 -0700783 TaggedSink& ts = gSinks.push_back();
mtklein18300a32016-03-16 13:53:35 -0700784 ts.reset(sink.release());
kkinnunen3e980c32015-12-23 01:33:00 -0800785 ts.tag = config.getTag();
mtklein748ca3b2015-01-15 10:56:12 -0800786}
787
788static bool gpu_supported() {
789#if SK_SUPPORT_GPU
790 return FLAGS_gpu;
791#else
792 return false;
793#endif
794}
kkinnunen9ebc3f02015-12-21 23:48:13 -0800795
kkinnunen3e980c32015-12-23 01:33:00 -0800796static Sink* create_sink(const SkCommandLineConfig* config) {
797#if SK_SUPPORT_GPU
798 if (gpu_supported()) {
799 if (const SkCommandLineConfigGpu* gpuConfig = config->asConfigGpu()) {
bsalomon85b4b532016-04-05 11:06:27 -0700800 GrContextFactory::ContextType contextType = gpuConfig->getContextType();
801 GrContextFactory::ContextOptions contextOptions =
802 GrContextFactory::kNone_ContextOptions;
kkinnunen3e980c32015-12-23 01:33:00 -0800803 if (gpuConfig->getUseNVPR()) {
bsalomon85b4b532016-04-05 11:06:27 -0700804 contextOptions = static_cast<GrContextFactory::ContextOptions>(
805 contextOptions | GrContextFactory::kEnableNVPR_ContextOptions);
kkinnunen3e980c32015-12-23 01:33:00 -0800806 }
brianosmanb109b8c2016-06-16 13:03:24 -0700807 if (SkColorAndColorSpaceAreGammaCorrect(gpuConfig->getColorType(),
808 gpuConfig->getColorSpace())) {
bsalomon85b4b532016-04-05 11:06:27 -0700809 contextOptions = static_cast<GrContextFactory::ContextOptions>(
810 contextOptions | GrContextFactory::kRequireSRGBSupport_ContextOptions);
brianosman61d3b082016-03-30 11:19:36 -0700811 }
kkinnunen3e980c32015-12-23 01:33:00 -0800812 GrContextFactory testFactory;
813 if (!testFactory.get(contextType, contextOptions)) {
mtkleinc41fd922016-03-08 09:01:39 -0800814 info("WARNING: can not create GPU context for config '%s'. "
815 "GM tests will be skipped.\n", gpuConfig->getTag().c_str());
kkinnunen3e980c32015-12-23 01:33:00 -0800816 return nullptr;
817 }
818 return new GPUSink(contextType, contextOptions, gpuConfig->getSamples(),
brianosmand93c1202016-03-10 07:49:08 -0800819 gpuConfig->getUseDIText(), gpuConfig->getColorType(),
brianosmanb109b8c2016-06-16 13:03:24 -0700820 sk_ref_sp(gpuConfig->getColorSpace()), FLAGS_gpu_threading);
kkinnunen3e980c32015-12-23 01:33:00 -0800821 }
822 }
823#endif
824
825#define SINK(t, sink, ...) if (config->getBackend().equals(t)) { return new sink(__VA_ARGS__); }
826
tomhudsoneebc39a2015-02-23 12:18:05 -0800827#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
828 SINK("hwui", HWUISink);
829#endif
830
mtklein748ca3b2015-01-15 10:56:12 -0800831 if (FLAGS_cpu) {
brianosmanb109b8c2016-06-16 13:03:24 -0700832 auto srgbColorSpace = SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named);
833
mtklein748ca3b2015-01-15 10:56:12 -0800834 SINK("565", RasterSink, kRGB_565_SkColorType);
835 SINK("8888", RasterSink, kN32_SkColorType);
brianosmanb109b8c2016-06-16 13:03:24 -0700836 SINK("srgb", RasterSink, kN32_SkColorType, srgbColorSpace);
mtklein27c3fdd2016-02-26 14:43:21 -0800837 SINK("f16", RasterSink, kRGBA_F16_SkColorType);
halcanary94b87bd2016-04-04 06:12:15 -0700838 SINK("pdf", PDFSink);
mtklein9c3f17d2015-01-28 11:35:18 -0800839 SINK("skp", SKPSink);
mtklein8a4527e2015-01-31 20:00:58 -0800840 SINK("svg", SVGSink);
841 SINK("null", NullSink);
halcanary47ef4d52015-03-03 09:13:09 -0800842 SINK("xps", XPSSink);
halcanary4b656662016-04-27 07:45:18 -0700843 SINK("pdfa", PDFSink, true);
mtklein748ca3b2015-01-15 10:56:12 -0800844 }
845#undef SINK
halcanary96fcdcc2015-08-27 07:41:13 -0700846 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800847}
848
kkinnunen3e980c32015-12-23 01:33:00 -0800849static Sink* create_via(const SkString& tag, Sink* wrapped) {
850#define VIA(t, via, ...) if (tag.equals(t)) { return new via(__VA_ARGS__); }
halcanary96fcdcc2015-08-27 07:41:13 -0700851 VIA("twice", ViaTwice, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700852 VIA("serialize", ViaSerialization, wrapped);
mtklein4a34ecb2016-01-08 10:19:35 -0800853 VIA("pic", ViaPicture, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700854 VIA("2ndpic", ViaSecondPicture, wrapped);
mtkleind31c13d2015-05-05 12:59:56 -0700855 VIA("sp", ViaSingletonPictures, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700856 VIA("tiles", ViaTiles, 256, 256, nullptr, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800857 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800858
mtkleind603b222015-02-17 11:13:33 -0800859 if (FLAGS_matrix.count() == 4) {
mtklein748ca3b2015-01-15 10:56:12 -0800860 SkMatrix m;
mtkleind603b222015-02-17 11:13:33 -0800861 m.reset();
862 m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
863 m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
864 m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
865 m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
866 VIA("matrix", ViaMatrix, m, wrapped);
867 VIA("upright", ViaUpright, m, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800868 }
tomhudson64de1e12015-03-05 08:01:07 -0800869
870#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
871 VIA("androidsdk", ViaAndroidSDK, wrapped);
872#endif
873
mtklein748ca3b2015-01-15 10:56:12 -0800874#undef VIA
halcanary96fcdcc2015-08-27 07:41:13 -0700875 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800876}
877
brianosman05c987c2016-05-09 13:42:16 -0700878static bool gather_sinks() {
kkinnunen3e980c32015-12-23 01:33:00 -0800879 SkCommandLineConfigArray configs;
880 ParseConfigs(FLAGS_config, &configs);
881 for (int i = 0; i < configs.count(); i++) {
882 const SkCommandLineConfig& config = *configs[i];
883 Sink* sink = create_sink(&config);
884 if (sink == nullptr) {
mtkleinc41fd922016-03-08 09:01:39 -0800885 info("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
886 config.getTag().c_str());
kkinnunen3e980c32015-12-23 01:33:00 -0800887 continue;
888 }
mtklein748ca3b2015-01-15 10:56:12 -0800889
kkinnunen3e980c32015-12-23 01:33:00 -0800890 const SkTArray<SkString>& parts = config.getViaParts();
891 for (int j = parts.count(); j-- > 0;) {
892 const SkString& part = parts[j];
893 Sink* next = create_via(part, sink);
halcanary96fcdcc2015-08-27 07:41:13 -0700894 if (next == nullptr) {
mtkleinc41fd922016-03-08 09:01:39 -0800895 info("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
896 part.c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800897 delete sink;
halcanary96fcdcc2015-08-27 07:41:13 -0700898 sink = nullptr;
mtklein748ca3b2015-01-15 10:56:12 -0800899 break;
900 }
901 sink = next;
902 }
903 if (sink) {
904 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800905 }
906 }
brianosman05c987c2016-05-09 13:42:16 -0700907
908 // If no configs were requested (just running tests, perhaps?), then we're okay.
909 // Otherwise, make sure that at least one sink was constructed correctly. This catches
910 // the case of bots without a GPU being assigned GPU configs.
911 return (configs.count() == 0) || (gSinks.count() > 0);
mtklein114c3cd2015-01-15 10:15:02 -0800912}
mtklein709d2c32015-01-15 08:30:25 -0800913
mtkleina5114d72015-08-24 13:27:01 -0700914static bool dump_png(SkBitmap bitmap, const char* path, const char* md5) {
915 const int w = bitmap.width(),
916 h = bitmap.height();
917
brianosman3c579dc2016-04-19 09:18:11 -0700918 sk_sp<SkData> encodedBitmap = sk_tools::encode_bitmap_for_png(bitmap);
919 if (encodedBitmap.get() == nullptr) {
920 return false;
mtkleina5114d72015-08-24 13:27:01 -0700921 }
brianosman3c579dc2016-04-19 09:18:11 -0700922 uint32_t* rgba = static_cast<uint32_t*>(encodedBitmap.get()->writable_data());
mtkleina5114d72015-08-24 13:27:01 -0700923
924 // We don't need bitmap anymore. Might as well drop our ref.
mtkleinfccb77d2015-08-24 14:13:29 -0700925 bitmap.reset();
mtkleina5114d72015-08-24 13:27:01 -0700926
mtkleinc64137c2015-08-25 10:56:08 -0700927 FILE* f = fopen(path, "wb");
mtkleina5114d72015-08-24 13:27:01 -0700928 if (!f) { return false; }
929
930 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
931 if (!png) {
932 fclose(f);
933 return false;
934 }
935
936 png_infop info = png_create_info_struct(png);
937 if (!info) {
938 png_destroy_write_struct(&png, &info);
939 fclose(f);
940 return false;
941 }
942
mtkleind69ece62015-09-10 10:37:44 -0700943 SkString description;
944 description.append("Key: ");
945 for (int i = 0; i < FLAGS_key.count(); i++) {
946 description.appendf("%s ", FLAGS_key[i]);
947 }
948 description.append("Properties: ");
949 for (int i = 0; i < FLAGS_properties.count(); i++) {
950 description.appendf("%s ", FLAGS_properties[i]);
951 }
952 description.appendf("MD5: %s", md5);
953
mtkleina5114d72015-08-24 13:27:01 -0700954 png_text text[2];
955 text[0].key = (png_charp)"Author";
956 text[0].text = (png_charp)"DM dump_png()";
957 text[0].compression = PNG_TEXT_COMPRESSION_NONE;
958 text[1].key = (png_charp)"Description";
mtkleind69ece62015-09-10 10:37:44 -0700959 text[1].text = (png_charp)description.c_str();
mtkleina5114d72015-08-24 13:27:01 -0700960 text[1].compression = PNG_TEXT_COMPRESSION_NONE;
961 png_set_text(png, info, text, 2);
962
963 png_init_io(png, f);
964 png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8,
965 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
966 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
967 png_write_info(png, info);
968 for (int j = 0; j < h; j++) {
brianosman3c579dc2016-04-19 09:18:11 -0700969 png_bytep row = (png_bytep)(rgba + w*j);
mtkleina5114d72015-08-24 13:27:01 -0700970 png_write_rows(png, &row, 1);
971 }
972 png_write_end(png, info);
973
974 png_destroy_write_struct(&png, &info);
975 fclose(f);
976 return true;
977}
978
mtkleina2ef6422015-01-15 13:44:22 -0800979static bool match(const char* needle, const char* haystack) {
halcanary96fcdcc2015-08-27 07:41:13 -0700980 return 0 == strcmp("_", needle) || nullptr != strstr(haystack, needle);
mtkleina2ef6422015-01-15 13:44:22 -0800981}
982
mtklein3eed7dd2016-02-24 19:07:07 -0800983static bool is_blacklisted(const char* sink, const char* src,
984 const char* srcOptions, const char* name) {
mtklein0d243ff2015-04-03 07:51:00 -0700985 for (int i = 0; i < FLAGS_blacklist.count() - 3; i += 4) {
mtkleina2ef6422015-01-15 13:44:22 -0800986 if (match(FLAGS_blacklist[i+0], sink) &&
djsollen54416de2015-04-03 07:24:48 -0700987 match(FLAGS_blacklist[i+1], src) &&
988 match(FLAGS_blacklist[i+2], srcOptions) &&
989 match(FLAGS_blacklist[i+3], name)) {
mtklein3eed7dd2016-02-24 19:07:07 -0800990 return true;
mtkleina2ef6422015-01-15 13:44:22 -0800991 }
992 }
mtklein3eed7dd2016-02-24 19:07:07 -0800993 return false;
mtkleina2ef6422015-01-15 13:44:22 -0800994}
995
mtkleincd50bca2016-01-05 06:20:20 -0800996// Even when a Task Sink reports to be non-threadsafe (e.g. GPU), we know things like
997// .png encoding are definitely thread safe. This lets us offload that work to CPU threads.
998static SkTaskGroup gDefinitelyThreadSafeWork;
999
mtklein748ca3b2015-01-15 10:56:12 -08001000// The finest-grained unit of work we can run: draw a single Src into a single Sink,
1001// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
1002struct Task {
mtkleine0effd62015-07-29 06:37:28 -07001003 Task(const TaggedSrc& src, const TaggedSink& sink) : src(src), sink(sink) {}
1004 const TaggedSrc& src;
1005 const TaggedSink& sink;
mtklein748ca3b2015-01-15 10:56:12 -08001006
mtklein21eaf3b2016-02-08 12:39:59 -08001007 static void Run(const Task& task) {
1008 SkString name = task.src->name();
mtkleine0effd62015-07-29 06:37:28 -07001009
mtkleinb9eb4ac2015-02-02 18:26:03 -08001010 SkString log;
mtklein3eed7dd2016-02-24 19:07:07 -08001011 if (!FLAGS_dryRun) {
mtklein748ca3b2015-01-15 10:56:12 -08001012 SkBitmap bitmap;
1013 SkDynamicMemoryWStream stream;
mtklein3eed7dd2016-02-24 19:07:07 -08001014 start(task.sink.tag.c_str(), task.src.tag.c_str(),
1015 task.src.options.c_str(), name.c_str());
mtklein21eaf3b2016-02-08 12:39:59 -08001016 Error err = task.sink->draw(*task.src, &bitmap, &stream, &log);
mtkleinb3b13b72016-03-07 13:20:52 -08001017 if (!log.isEmpty()) {
mtkleinc41fd922016-03-08 09:01:39 -08001018 info("%s %s %s %s:\n%s\n", task.sink.tag.c_str()
1019 , task.src.tag.c_str()
1020 , task.src.options.c_str()
1021 , name.c_str()
1022 , log.c_str());
mtkleinb3b13b72016-03-07 13:20:52 -08001023 }
mtklein748ca3b2015-01-15 10:56:12 -08001024 if (!err.isEmpty()) {
mtklein4089ef72015-03-05 08:40:28 -08001025 if (err.isFatal()) {
djsollen54416de2015-04-03 07:24:48 -07001026 fail(SkStringPrintf("%s %s %s %s: %s",
mtklein21eaf3b2016-02-08 12:39:59 -08001027 task.sink.tag.c_str(),
1028 task.src.tag.c_str(),
1029 task.src.options.c_str(),
mtklein4089ef72015-03-05 08:40:28 -08001030 name.c_str(),
1031 err.c_str()));
mtkleinb37cb412015-03-18 05:27:14 -07001032 } else {
mtklein3eed7dd2016-02-24 19:07:07 -08001033 done(task.sink.tag.c_str(), task.src.tag.c_str(),
1034 task.src.options.c_str(), name.c_str());
mtkleinba6ada72016-01-21 09:39:35 -08001035 return;
mtklein4089ef72015-03-05 08:40:28 -08001036 }
mtklein748ca3b2015-01-15 10:56:12 -08001037 }
mtklein62bd1a62015-01-27 14:46:26 -08001038
mtkleincd50bca2016-01-05 06:20:20 -08001039 // We're likely switching threads here, so we must capture by value, [=] or [foo,bar].
1040 SkStreamAsset* data = stream.detachAsStream();
1041 gDefinitelyThreadSafeWork.add([task,name,bitmap,data]{
1042 SkAutoTDelete<SkStreamAsset> ownedData(data);
1043
1044 // Why doesn't the copy constructor do this when we have pre-locked pixels?
1045 bitmap.lockPixels();
1046
1047 SkString md5;
1048 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
1049 SkMD5 hash;
1050 if (data->getLength()) {
1051 hash.writeStream(data, data->getLength());
1052 data->rewind();
mtklein38408462015-07-08 07:25:27 -07001053 } else {
mtkleincd50bca2016-01-05 06:20:20 -08001054 // If we're BGRA (Linux, Windows), swizzle over to RGBA (Mac, Android).
1055 // This helps eliminate multiple 0-pixel-diff hashes on gold.skia.org.
1056 // (Android's general slow speed breaks the tie arbitrarily in RGBA's favor.)
1057 // We might consider promoting 565 to RGBA too.
1058 if (bitmap.colorType() == kBGRA_8888_SkColorType) {
1059 SkBitmap swizzle;
1060 SkAssertResult(bitmap.copyTo(&swizzle, kRGBA_8888_SkColorType));
1061 hash.write(swizzle.getPixels(), swizzle.getSize());
1062 } else {
1063 hash.write(bitmap.getPixels(), bitmap.getSize());
1064 }
1065 }
1066 SkMD5::Digest digest;
1067 hash.finish(digest);
1068 for (int i = 0; i < 16; i++) {
1069 md5.appendf("%02x", digest.data[i]);
mtklein38408462015-07-08 07:25:27 -07001070 }
mtklein62bd1a62015-01-27 14:46:26 -08001071 }
mtklein62bd1a62015-01-27 14:46:26 -08001072
mtkleincd50bca2016-01-05 06:20:20 -08001073 if (!FLAGS_readPath.isEmpty() &&
mtklein3eed7dd2016-02-24 19:07:07 -08001074 !gGold.contains(Gold(task.sink.tag, task.src.tag,
1075 task.src.options, name, md5))) {
mtkleincd50bca2016-01-05 06:20:20 -08001076 fail(SkStringPrintf("%s not found for %s %s %s %s in %s",
1077 md5.c_str(),
mtklein21eaf3b2016-02-08 12:39:59 -08001078 task.sink.tag.c_str(),
1079 task.src.tag.c_str(),
1080 task.src.options.c_str(),
mtkleincd50bca2016-01-05 06:20:20 -08001081 name.c_str(),
1082 FLAGS_readPath[0]));
mtklein748ca3b2015-01-15 10:56:12 -08001083 }
mtkleincd50bca2016-01-05 06:20:20 -08001084
1085 if (!FLAGS_writePath.isEmpty()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001086 const char* ext = task.sink->fileExtension();
mtkleincd50bca2016-01-05 06:20:20 -08001087 if (data->getLength()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001088 WriteToDisk(task, md5, ext, data, data->getLength(), nullptr);
mtkleincd50bca2016-01-05 06:20:20 -08001089 SkASSERT(bitmap.drawsNothing());
1090 } else if (!bitmap.drawsNothing()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001091 WriteToDisk(task, md5, ext, nullptr, 0, &bitmap);
mtkleincd50bca2016-01-05 06:20:20 -08001092 }
1093 }
1094 });
mtklein748ca3b2015-01-15 10:56:12 -08001095 }
mtklein3eed7dd2016-02-24 19:07:07 -08001096 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 -08001097 }
1098
1099 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -08001100 SkString md5,
1101 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -08001102 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -08001103 const SkBitmap* bitmap) {
mtklein409d4702016-02-29 07:38:01 -08001104 bool gammaCorrect = false;
1105 if (bitmap) {
brianosman898235c2016-04-06 07:38:23 -07001106 gammaCorrect = SkImageInfoIsGammaCorrect(bitmap->info());
mtklein409d4702016-02-29 07:38:01 -08001107 }
1108
mtklein748ca3b2015-01-15 10:56:12 -08001109 JsonWriter::BitmapResult result;
djsollen54416de2015-04-03 07:24:48 -07001110 result.name = task.src->name();
mtklein3eed7dd2016-02-24 19:07:07 -08001111 result.config = task.sink.tag;
djsollen54416de2015-04-03 07:24:48 -07001112 result.sourceType = task.src.tag;
1113 result.sourceOptions = task.src.options;
1114 result.ext = ext;
mtklein409d4702016-02-29 07:38:01 -08001115 result.gammaCorrect = gammaCorrect;
djsollen54416de2015-04-03 07:24:48 -07001116 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -08001117 JsonWriter::AddBitmapResult(result);
1118
mtkleinb0531a72015-04-07 13:38:48 -07001119 // If an MD5 is uninteresting, we want it noted in the JSON file,
1120 // but don't want to dump it out as a .png (or whatever ext is).
1121 if (gUninterestingHashes.contains(md5)) {
1122 return;
1123 }
1124
mtklein748ca3b2015-01-15 10:56:12 -08001125 const char* dir = FLAGS_writePath[0];
1126 if (0 == strcmp(dir, "@")) { // Needed for iOS.
1127 dir = FLAGS_resourcePath[0];
1128 }
1129 sk_mkdir(dir);
1130
1131 SkString path;
1132 if (FLAGS_nameByHash) {
1133 path = SkOSPath::Join(dir, result.md5.c_str());
1134 path.append(".");
1135 path.append(ext);
1136 if (sk_exists(path.c_str())) {
1137 return; // Content-addressed. If it exists already, we're done.
1138 }
1139 } else {
kkinnunen3e980c32015-12-23 01:33:00 -08001140 path = SkOSPath::Join(dir, task.sink.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001141 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001142 path = SkOSPath::Join(path.c_str(), task.src.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001143 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001144 if (strcmp(task.src.options.c_str(), "") != 0) {
1145 path = SkOSPath::Join(path.c_str(), task.src.options.c_str());
djsollen54416de2015-04-03 07:24:48 -07001146 sk_mkdir(path.c_str());
1147 }
mtklein748ca3b2015-01-15 10:56:12 -08001148 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
1149 path.append(".");
1150 path.append(ext);
1151 }
1152
mtklein748ca3b2015-01-15 10:56:12 -08001153 if (bitmap) {
mtkleina5114d72015-08-24 13:27:01 -07001154 if (!dump_png(*bitmap, path.c_str(), result.md5.c_str())) {
mtklein748ca3b2015-01-15 10:56:12 -08001155 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
1156 return;
1157 }
1158 } else {
mtkleina5114d72015-08-24 13:27:01 -07001159 SkFILEWStream file(path.c_str());
1160 if (!file.isValid()) {
1161 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
1162 return;
1163 }
mtklein748ca3b2015-01-15 10:56:12 -08001164 if (!file.writeStream(data, len)) {
1165 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
1166 return;
1167 }
1168 }
1169 }
1170};
1171
mtklein748ca3b2015-01-15 10:56:12 -08001172/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1173
1174// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
1175
mtklein21eaf3b2016-02-08 12:39:59 -08001176static SkTDArray<skiatest::Test> gParallelTests, gSerialTests;
mtklein748ca3b2015-01-15 10:56:12 -08001177
1178static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -08001179 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -08001180 return;
1181 }
mtklein6393c062015-04-27 08:45:01 -07001182 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r; r = r->next()) {
1183 if (!in_shard()) {
1184 continue;
1185 }
halcanary87f3ba42015-01-20 09:30:20 -08001186 // Despite its name, factory() is returning a reference to
1187 // link-time static const POD data.
1188 const skiatest::Test& test = r->factory();
1189 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -08001190 continue;
1191 }
halcanary87f3ba42015-01-20 09:30:20 -08001192 if (test.needsGpu && gpu_supported()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001193 (FLAGS_gpu_threading ? gParallelTests : gSerialTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -08001194 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein21eaf3b2016-02-08 12:39:59 -08001195 gParallelTests.push(test);
mtklein82d28432015-01-15 12:46:02 -08001196 }
mtklein748ca3b2015-01-15 10:56:12 -08001197 }
1198}
1199
mtklein21eaf3b2016-02-08 12:39:59 -08001200static void run_test(skiatest::Test test) {
halcanary87f3ba42015-01-20 09:30:20 -08001201 struct : public skiatest::Reporter {
mtklein36352bf2015-03-25 18:17:31 -07001202 void reportFailed(const skiatest::Failure& failure) override {
halcanary87f3ba42015-01-20 09:30:20 -08001203 fail(failure.toString());
1204 JsonWriter::AddTestFailure(failure);
1205 }
mtklein36352bf2015-03-25 18:17:31 -07001206 bool allowExtendedTest() const override {
halcanary87f3ba42015-01-20 09:30:20 -08001207 return FLAGS_pathOpsExtended;
1208 }
mtklein36352bf2015-03-25 18:17:31 -07001209 bool verbose() const override { return FLAGS_veryVerbose; }
halcanary87f3ba42015-01-20 09:30:20 -08001210 } reporter;
djsollen824996a2015-06-12 12:06:22 -07001211
mtklein3eed7dd2016-02-24 19:07:07 -08001212 if (!FLAGS_dryRun && !is_blacklisted("_", "tests", "_", test.name)) {
mtklein21eaf3b2016-02-08 12:39:59 -08001213 start("unit", "test", "", test.name);
mtklein55e88b22015-01-21 15:50:13 -08001214 GrContextFactory factory;
mtklein21eaf3b2016-02-08 12:39:59 -08001215 test.proc(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -08001216 }
mtklein3eed7dd2016-02-24 19:07:07 -08001217 done("unit", "test", "", test.name);
mtklein748ca3b2015-01-15 10:56:12 -08001218}
1219
1220/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1221
mtkleinafae30a2016-02-24 12:28:32 -08001222DEFINE_int32(status_sec, 15, "Print status this often (and if we crash).");
1223
1224SkThread* start_status_thread() {
1225 auto thread = new SkThread([] (void*) {
1226 for (;;) {
1227 print_status();
1228 #if defined(SK_BUILD_FOR_WIN)
1229 Sleep(FLAGS_status_sec * 1000);
1230 #else
1231 sleep(FLAGS_status_sec);
1232 #endif
mtklein2e1c47e2015-03-12 07:16:56 -07001233 }
mtkleinafae30a2016-02-24 12:28:32 -08001234 });
1235 thread->start();
1236 return thread;
mtkleinde6fc2b2015-03-12 06:28:54 -07001237}
1238
caryclark83ca6282015-06-10 09:31:09 -07001239#define PORTABLE_FONT_PREFIX "Toy Liberation "
1240
mbocee6a9912016-05-31 11:42:36 -07001241static sk_sp<SkTypeface> create_from_name(const char familyName[], SkFontStyle style) {
caryclark83ca6282015-06-10 09:31:09 -07001242 if (familyName && strlen(familyName) > sizeof(PORTABLE_FONT_PREFIX)
1243 && !strncmp(familyName, PORTABLE_FONT_PREFIX, sizeof(PORTABLE_FONT_PREFIX) - 1)) {
caryclark1818acb2015-07-24 12:09:25 -07001244 return sk_tool_utils::create_portable_typeface(familyName, style);
caryclark83ca6282015-06-10 09:31:09 -07001245 }
halcanary96fcdcc2015-08-27 07:41:13 -07001246 return nullptr;
caryclark83ca6282015-06-10 09:31:09 -07001247}
1248
1249#undef PORTABLE_FONT_PREFIX
1250
mbocee6a9912016-05-31 11:42:36 -07001251extern sk_sp<SkTypeface> (*gCreateTypefaceDelegate)(const char [], SkFontStyle );
caryclark83ca6282015-06-10 09:31:09 -07001252
jcgregorio3b27ade2014-11-13 08:06:40 -08001253int dm_main();
caryclark17f0b6d2014-07-22 10:15:34 -07001254int dm_main() {
bsalomon71de3532016-04-26 14:27:21 -07001255 setbuf(stdout, nullptr);
mtklein246ba3a2016-02-23 10:39:36 -08001256 setup_crash_handler();
mtkleinafae30a2016-02-24 12:28:32 -08001257
mtkleina483da32016-03-17 12:53:36 -07001258 if (FLAGS_verbose) {
1259 gVLog = stderr;
1260 } else if (!FLAGS_writePath.isEmpty()) {
mtklein51c8cfc2016-03-11 12:59:09 -08001261 sk_mkdir(FLAGS_writePath[0]);
1262 gVLog = freopen(SkOSPath::Join(FLAGS_writePath[0], "verbose.log").c_str(), "w", stderr);
mtkleinc41fd922016-03-08 09:01:39 -08001263 }
1264
mtklein5286f022016-01-22 08:18:14 -08001265 JsonWriter::DumpJson(); // It's handy for the bots to assume this is ~never missing.
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +00001266 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -07001267 SkTaskGroup::Enabler enabled(FLAGS_threads);
caryclark83ca6282015-06-10 09:31:09 -07001268 gCreateTypefaceDelegate = &create_from_name;
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +00001269
halcanary7d571242016-02-24 17:59:16 -08001270 {
1271 SkString testResourcePath = GetResourcePath("color_wheel.png");
1272 SkFILEStream testResource(testResourcePath.c_str());
1273 if (!testResource.isValid()) {
mtkleinc41fd922016-03-08 09:01:39 -08001274 info("Some resources are missing. Do you need to set --resourcePath?\n");
halcanary7d571242016-02-24 17:59:16 -08001275 }
1276 }
mtklein62bd1a62015-01-27 14:46:26 -08001277 gather_gold();
borenet09ed4802015-04-03 14:15:33 -07001278 gather_uninteresting_hashes();
mtklein62bd1a62015-01-27 14:46:26 -08001279
scroggo86737142016-02-03 12:19:11 -08001280 if (!gather_srcs()) {
1281 return 1;
1282 }
brianosman05c987c2016-05-09 13:42:16 -07001283 if (!gather_sinks()) {
1284 return 1;
1285 }
mtklein748ca3b2015-01-15 10:56:12 -08001286 gather_tests();
mtklein21eaf3b2016-02-08 12:39:59 -08001287 gPending = gSrcs.count() * gSinks.count() + gParallelTests.count() + gSerialTests.count();
mtkleinc41fd922016-03-08 09:01:39 -08001288 info("%d srcs * %d sinks + %d tests == %d tasks",
1289 gSrcs.count(), gSinks.count(), gParallelTests.count() + gSerialTests.count(), gPending);
mtkleinafae30a2016-02-24 12:28:32 -08001290 SkAutoTDelete<SkThread> statusThread(start_status_thread());
mtklein748ca3b2015-01-15 10:56:12 -08001291
mtklein21eaf3b2016-02-08 12:39:59 -08001292 // Kick off as much parallel work as we can, making note of any serial work we'll need to do.
1293 SkTaskGroup parallel;
1294 SkTArray<Task> serial;
1295
1296 for (auto& sink : gSinks)
1297 for (auto& src : gSrcs) {
mtklein3eed7dd2016-02-24 19:07:07 -08001298 if (src->veto(sink->flags()) ||
1299 is_blacklisted(sink.tag.c_str(), src.tag.c_str(),
1300 src.options.c_str(), src->name().c_str())) {
mtkleinf10637f2016-06-10 13:56:35 -07001301 SkAutoMutexAcquire lock(gMutex);
mtklein3eed7dd2016-02-24 19:07:07 -08001302 gPending--;
1303 continue;
1304 }
1305
mtklein21eaf3b2016-02-08 12:39:59 -08001306 Task task(src, sink);
1307 if (src->serial() || sink->serial()) {
1308 serial.push_back(task);
1309 } else {
1310 parallel.add([task] { Task::Run(task); });
mtklein748ca3b2015-01-15 10:56:12 -08001311 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001312 }
mtklein21eaf3b2016-02-08 12:39:59 -08001313 for (auto test : gParallelTests) {
1314 parallel.add([test] { run_test(test); });
mtklein55e88b22015-01-21 15:50:13 -08001315 }
mtklein21eaf3b2016-02-08 12:39:59 -08001316
1317 // With the parallel work running, run serial tasks and tests here on main thread.
1318 for (auto task : serial) { Task::Run(task); }
1319 for (auto test : gSerialTests) { run_test(test); }
1320
1321 // Wait for any remaining parallel work to complete (including any spun off of serial tasks).
1322 parallel.wait();
mtkleincd50bca2016-01-05 06:20:20 -08001323 gDefinitelyThreadSafeWork.wait();
1324
mtkleinda884c42016-02-24 18:00:23 -08001325 // We'd better have run everything.
1326 SkASSERT(gPending == 0);
mtkleine027f172016-02-26 15:53:06 -08001327 // Make sure we've flushed all our results to disk.
1328 JsonWriter::DumpJson();
mtkleinda884c42016-02-24 18:00:23 -08001329
mtklein748ca3b2015-01-15 10:56:12 -08001330 // At this point we're back in single-threaded land.
caryclarkf53ce802015-06-15 06:48:30 -07001331 sk_tool_utils::release_portable_typefaces();
mtklein@google.comd36522d2013-10-16 13:02:15 +00001332
mtklein748ca3b2015-01-15 10:56:12 -08001333 if (gFailures.count() > 0) {
mtkleinc41fd922016-03-08 09:01:39 -08001334 info("Failures:\n");
mtklein748ca3b2015-01-15 10:56:12 -08001335 for (int i = 0; i < gFailures.count(); i++) {
mtkleinc41fd922016-03-08 09:01:39 -08001336 info("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001337 }
mtkleinc41fd922016-03-08 09:01:39 -08001338 info("%d failures\n", gFailures.count());
mtklein748ca3b2015-01-15 10:56:12 -08001339 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001340 }
mtkleinafae30a2016-02-24 12:28:32 -08001341
1342#ifdef SK_PDF_IMAGE_STATS
halcanary7a14b312015-10-01 07:28:13 -07001343 SkPDFImageDumpStats();
mtkleinafae30a2016-02-24 12:28:32 -08001344#endif // SK_PDF_IMAGE_STATS
1345
1346 print_status();
mtkleinc41fd922016-03-08 09:01:39 -08001347 info("Finished!\n");
mtklein748ca3b2015-01-15 10:56:12 -08001348 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +00001349}
jcgregorio3b27ade2014-11-13 08:06:40 -08001350
kkinnunen179a8f52015-11-20 13:32:24 -08001351// TODO: currently many GPU tests are declared outside SK_SUPPORT_GPU guards.
1352// Thus we export the empty RunWithGPUTestContexts when SK_SUPPORT_GPU=0.
1353namespace skiatest {
bsalomon758586c2016-04-06 14:02:39 -07001354
1355#if SK_SUPPORT_GPU
1356bool IsGLContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1357 return kOpenGL_GrBackend == GrContextFactory::ContextTypeBackend(type);
1358}
bsalomondc0fcd42016-04-11 14:21:33 -07001359bool IsVulkanContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1360 return kVulkan_GrBackend == GrContextFactory::ContextTypeBackend(type);
1361}
bsalomon758586c2016-04-06 14:02:39 -07001362bool IsRenderingGLContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1363 return IsGLContextType(type) && GrContextFactory::IsRenderingContext(type);
1364}
1365bool IsNullGLContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1366 return type == GrContextFactory::kNullGL_ContextType;
1367}
1368#else
1369bool IsGLContextType(int) { return false; }
bsalomondc0fcd42016-04-11 14:21:33 -07001370bool IsVulkanContextType(int) { return false; }
bsalomon758586c2016-04-06 14:02:39 -07001371bool IsRenderingGLContextType(int) { return false; }
1372bool IsNullGLContextType(int) { return false; }
1373#endif
1374
1375void RunWithGPUTestContexts(GrContextTestFn* test, GrContextTypeFilterFn* contextTypeFilter,
bsalomonf2f1c172016-04-05 12:59:06 -07001376 Reporter* reporter, GrContextFactory* factory) {
kkinnunen179a8f52015-11-20 13:32:24 -08001377#if SK_SUPPORT_GPU
kkinnunen3e980c32015-12-23 01:33:00 -08001378
bsalomon758586c2016-04-06 14:02:39 -07001379 for (int typeInt = 0; typeInt < GrContextFactory::kContextTypeCnt; ++typeInt) {
1380 GrContextFactory::ContextType contextType = (GrContextFactory::ContextType) typeInt;
1381 ContextInfo ctxInfo = factory->getContextInfo(contextType);
bsalomonfda88072016-04-11 14:40:50 -07001382 if (contextTypeFilter && !(*contextTypeFilter)(contextType)) {
kkinnunen179a8f52015-11-20 13:32:24 -08001383 continue;
1384 }
bsalomon758586c2016-04-06 14:02:39 -07001385 // Use "native" instead of explicitly trying OpenGL and OpenGL ES. Do not use GLES on,
1386 // desktop since tests do not account for not fixing http://skbug.com/2809
1387 if (contextType == GrContextFactory::kGL_ContextType ||
1388 contextType == GrContextFactory::kGLES_ContextType) {
1389 if (contextType != GrContextFactory::kNativeGL_ContextType) {
1390 continue;
1391 }
1392 }
bsalomon8b7451a2016-05-11 06:33:06 -07001393 if (ctxInfo.grContext()) {
bsalomonf2f1c172016-04-05 12:59:06 -07001394 (*test)(reporter, ctxInfo);
kkinnunen5219fd92015-12-10 06:28:13 -08001395 }
bsalomonf2f1c172016-04-05 12:59:06 -07001396 ctxInfo = factory->getContextInfo(contextType,
bsalomon85b4b532016-04-05 11:06:27 -07001397 GrContextFactory::kEnableNVPR_ContextOptions);
bsalomon8b7451a2016-05-11 06:33:06 -07001398 if (ctxInfo.grContext()) {
bsalomonf2f1c172016-04-05 12:59:06 -07001399 (*test)(reporter, ctxInfo);
kkinnunen179a8f52015-11-20 13:32:24 -08001400 }
1401 }
1402#endif
1403}
kkinnunen179a8f52015-11-20 13:32:24 -08001404} // namespace skiatest
1405
borenet48087572015-04-02 12:16:36 -07001406#if !defined(SK_BUILD_FOR_IOS)
jcgregorio3b27ade2014-11-13 08:06:40 -08001407int main(int argc, char** argv) {
1408 SkCommandLineFlags::Parse(argc, argv);
1409 return dm_main();
1410}
1411#endif