blob: 2eb5593e6c2b87447af89d222e8e12f4a4a22777 [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"
caryclark17f0b6d2014-07-22 10:15:34 -070018#include "SkCommonFlags.h"
kkinnunen3e980c32015-12-23 01:33:00 -080019#include "SkCommonFlagsConfig.h"
caryclark83ca6282015-06-10 09:31:09 -070020#include "SkFontMgr.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +000021#include "SkGraphics.h"
mtklein27c3fdd2016-02-26 14:43:21 -080022#include "SkHalf.h"
mtklein748ca3b2015-01-15 10:56:12 -080023#include "SkMD5.h"
mtklein1b249332015-07-07 12:21:21 -070024#include "SkMutex.h"
mtklein1d0f1642014-09-08 08:05:18 -070025#include "SkOSFile.h"
mtklein27c3fdd2016-02-26 14:43:21 -080026#include "SkPM4fPriv.h"
mtklein246ba3a2016-02-23 10:39:36 -080027#include "SkSpinlock.h"
mtkleina82f5622015-02-20 12:30:19 -080028#include "SkTHash.h"
mtklein406654b2014-09-03 15:34:37 -070029#include "SkTaskGroup.h"
mtkleinde6fc2b2015-03-12 06:28:54 -070030#include "SkThreadUtils.h"
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +000031#include "Test.h"
mtklein748ca3b2015-01-15 10:56:12 -080032#include "Timer.h"
caryclark83ca6282015-06-10 09:31:09 -070033#include "sk_tool_utils.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +000034
halcanary7a14b312015-10-01 07:28:13 -070035#ifdef SK_PDF_IMAGE_STATS
36extern void SkPDFImageDumpStats();
37#endif
38
mtkleina5114d72015-08-24 13:27:01 -070039#include "png.h"
40
bungeman60e0fee2015-08-26 05:15:46 -070041#include <stdlib.h>
42
scroggo27a58342015-10-28 08:56:41 -070043#ifndef SK_BUILD_FOR_WIN32
44 #include <unistd.h>
45#endif
46
djsollen54416de2015-04-03 07:24:48 -070047DEFINE_string(src, "tests gm skp image", "Source types to test.");
mtklein748ca3b2015-01-15 10:56:12 -080048DEFINE_bool(nameByHash, false,
49 "If true, write to FLAGS_writePath[0]/<hash>.png instead of "
djsollen54416de2015-04-03 07:24:48 -070050 "to FLAGS_writePath[0]/<config>/<sourceType>/<sourceOptions>/<name>.png");
mtklein748ca3b2015-01-15 10:56:12 -080051DEFINE_bool2(pathOpsExtended, x, false, "Run extended pathOps tests.");
mtkleind603b222015-02-17 11:13:33 -080052DEFINE_string(matrix, "1 0 0 1",
53 "2x2 scale+skew matrix to apply or upright when using "
54 "'matrix' or 'upright' in config.");
mtklein82d28432015-01-15 12:46:02 -080055DEFINE_bool(gpu_threading, false, "Allow GPU work to run on multiple threads?");
mtklein@google.comd36522d2013-10-16 13:02:15 +000056
mtkleina2ef6422015-01-15 13:44:22 -080057DEFINE_string(blacklist, "",
djsollen54416de2015-04-03 07:24:48 -070058 "Space-separated config/src/srcOptions/name quadruples to blacklist. '_' matches anything. E.g. \n"
59 "'--blacklist gpu skp _ _' will blacklist all SKPs drawn into the gpu config.\n"
60 "'--blacklist gpu skp _ _ 8888 gm _ aarects' will also blacklist the aarects GM on 8888.");
mtkleina2ef6422015-01-15 13:44:22 -080061
mtklein62bd1a62015-01-27 14:46:26 -080062DEFINE_string2(readPath, r, "", "If set check for equality with golden results in this directory.");
63
borenet09ed4802015-04-03 14:15:33 -070064DEFINE_string(uninterestingHashesFile, "",
65 "File containing a list of uninteresting hashes. If a result hashes to something in "
66 "this list, no image is written for that result.");
67
mtklein6393c062015-04-27 08:45:01 -070068DEFINE_int32(shards, 1, "We're splitting source data into this many shards.");
69DEFINE_int32(shard, 0, "Which shard do I run?");
scroggoe4499842016-02-25 11:03:47 -080070DEFINE_bool(simpleCodec, false, "Only decode images to native scale");
mtklein6393c062015-04-27 08:45:01 -070071
mtklein748ca3b2015-01-15 10:56:12 -080072using namespace DM;
mtklein@google.comd36522d2013-10-16 13:02:15 +000073
mtklein748ca3b2015-01-15 10:56:12 -080074/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
75
76SK_DECLARE_STATIC_MUTEX(gFailuresMutex);
77static SkTArray<SkString> gFailures;
78
mtklein3eed7dd2016-02-24 19:07:07 -080079static void fail(const SkString& err) {
mtklein748ca3b2015-01-15 10:56:12 -080080 SkAutoMutexAcquire lock(gFailuresMutex);
81 SkDebugf("\n\nFAILURE: %s\n\n", err.c_str());
82 gFailures.push_back(err);
halcanary9e398f72015-01-10 11:18:04 -080083}
84
mtkleinb37cb412015-03-18 05:27:14 -070085
mtklein246ba3a2016-02-23 10:39:36 -080086// We use a spinlock to make locking this in a signal handler _somewhat_ safe.
mtklein15923c92016-02-29 10:14:38 -080087static SkSpinlock gMutex;
mtklein3eed7dd2016-02-24 19:07:07 -080088static int32_t gPending;
89static SkTArray<SkString> gRunning;
mtklein748ca3b2015-01-15 10:56:12 -080090
mtklein3eed7dd2016-02-24 19:07:07 -080091static void done(const char* config, const char* src, const char* srcOptions, const char* name) {
92 SkString id = SkStringPrintf("%s %s %s %s", config, src, srcOptions, name);
mtkleinafae30a2016-02-24 12:28:32 -080093 int pending;
mtkleinb37cb412015-03-18 05:27:14 -070094 {
mtklein15923c92016-02-29 10:14:38 -080095 SkAutoTAcquire<SkSpinlock> lock(gMutex);
mtkleinb37cb412015-03-18 05:27:14 -070096 for (int i = 0; i < gRunning.count(); i++) {
97 if (gRunning[i] == id) {
98 gRunning.removeShuffle(i);
99 break;
100 }
101 }
mtkleinafae30a2016-02-24 12:28:32 -0800102 pending = --gPending;
reed50bc0512015-05-19 14:13:31 -0700103 }
mtkleina17241b2015-01-23 05:48:00 -0800104 // We write our dm.json file every once in a while in case we crash.
105 // Notice this also handles the final dm.json when pending == 0.
106 if (pending % 500 == 0) {
107 JsonWriter::DumpJson();
108 }
mtklein@google.comd36522d2013-10-16 13:02:15 +0000109}
110
mtklein3eed7dd2016-02-24 19:07:07 -0800111static void start(const char* config, const char* src, const char* srcOptions, const char* name) {
112 SkString id = SkStringPrintf("%s %s %s %s", config, src, srcOptions, name);
mtklein15923c92016-02-29 10:14:38 -0800113 SkAutoTAcquire<SkSpinlock> lock(gMutex);
mtkleinb37cb412015-03-18 05:27:14 -0700114 gRunning.push_back(id);
115}
116
mtkleinafae30a2016-02-24 12:28:32 -0800117static void print_status() {
118 static SkMSec start_ms = SkTime::GetMSecs();
119
120 int curr = sk_tools::getCurrResidentSetSizeMB(),
121 peak = sk_tools::getMaxResidentSetSizeMB();
122 SkString elapsed = HumanizeMs(SkTime::GetMSecs() - start_ms);
123
mtklein15923c92016-02-29 10:14:38 -0800124 SkAutoTAcquire<SkSpinlock> lock(gMutex);
mtkleinafae30a2016-02-24 12:28:32 -0800125 SkDebugf("\n%s elapsed, %d active, %d queued, %dMB RAM, %dMB peak\n",
126 elapsed.c_str(), gRunning.count(), gPending - gRunning.count(), curr, peak);
127 for (auto& task : gRunning) {
128 SkDebugf("\t%s\n", task.c_str());
129 }
130}
131
mtkleinf8557292016-02-29 06:35:28 -0800132// Yo dawg, I heard you like signals so I caught a signal in your
133// signal handler so you can handle signals while you handle signals.
134// Let's not get into that situation. Only print if we're the first ones to get a crash signal.
135static std::atomic<bool> in_signal_handler{false};
136
mtklein246ba3a2016-02-23 10:39:36 -0800137#if defined(SK_BUILD_FOR_WIN32)
mtkleinf8557292016-02-29 06:35:28 -0800138 static LONG WINAPI handler(EXCEPTION_POINTERS* e) {
139 static const struct {
140 const char* name;
robertphillips75467862016-03-01 14:10:23 -0800141 DWORD code;
mtkleinf8557292016-02-29 06:35:28 -0800142 } kExceptions[] = {
143 #define _(E) {#E, E}
144 _(EXCEPTION_ACCESS_VIOLATION),
145 _(EXCEPTION_BREAKPOINT),
146 _(EXCEPTION_INT_DIVIDE_BY_ZERO),
147 _(EXCEPTION_STACK_OVERFLOW),
148 // TODO: more?
149 #undef _
150 };
151
152 if (!in_signal_handler.exchange(true)) {
153 const DWORD code = e->ExceptionRecord->ExceptionCode;
154 SkDebugf("\nCaught exception %u", code);
155 for (const auto& exception : kExceptions) {
156 if (exception.code == code) {
157 SkDebugf(" %s", exception.name);
158 }
159 }
160 SkDebugf("\n");
161 print_status();
162 }
163 // Execute default exception handler... hopefully, exit.
164 return EXCEPTION_EXECUTE_HANDLER;
mtklein246ba3a2016-02-23 10:39:36 -0800165 }
mtkleinf8557292016-02-29 06:35:28 -0800166 static void setup_crash_handler() { SetUnhandledExceptionFilter(handler); }
mtklein246ba3a2016-02-23 10:39:36 -0800167
mtklein819ab102016-02-29 17:02:52 -0800168#elif !defined(SK_BUILD_FOR_ANDROID)
169 #include <execinfo.h>
mtklein246ba3a2016-02-23 10:39:36 -0800170 #include <signal.h>
mtklein819ab102016-02-29 17:02:52 -0800171 #include <stdlib.h>
mtkleinf8557292016-02-29 06:35:28 -0800172
mtklein246ba3a2016-02-23 10:39:36 -0800173 static void setup_crash_handler() {
174 const int kSignals[] = { SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGSEGV };
175 for (int sig : kSignals) {
176 signal(sig, [](int sig) {
mtkleinf8557292016-02-29 06:35:28 -0800177 if (!in_signal_handler.exchange(true)) {
mtklein819ab102016-02-29 17:02:52 -0800178 SkAutoTAcquire<SkSpinlock> lock(gMutex);
179 SkDebugf("\nCaught signal %d [%s], was running:\n", sig, strsignal(sig));
180 for (auto& task : gRunning) {
181 SkDebugf("\t%s\n", task.c_str());
182 }
183
184 void* stack[64];
185 int count = backtrace(stack, SK_ARRAY_COUNT(stack));
186 char** symbols = backtrace_symbols(stack, count);
187 SkDebugf("\nStack trace:\n");
188 for (int i = 0; i < count; i++) {
189 SkDebugf(" %s\n", symbols[i]);
190 }
mtkleinf8557292016-02-29 06:35:28 -0800191 }
mtklein819ab102016-02-29 17:02:52 -0800192 _Exit(sig);
mtklein246ba3a2016-02-23 10:39:36 -0800193 });
194 }
195 }
mtklein819ab102016-02-29 17:02:52 -0800196
197#else // Android
198 static void setup_crash_handler() {}
mtklein246ba3a2016-02-23 10:39:36 -0800199#endif
200
mtklein748ca3b2015-01-15 10:56:12 -0800201/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
mtklein114c3cd2015-01-15 10:15:02 -0800202
mtklein62bd1a62015-01-27 14:46:26 -0800203struct Gold : public SkString {
mtkleina82f5622015-02-20 12:30:19 -0800204 Gold() : SkString("") {}
mtklein3eed7dd2016-02-24 19:07:07 -0800205 Gold(const SkString& sink, const SkString& src,
206 const SkString& srcOptions, const SkString& name,
207 const SkString& md5)
mtklein62bd1a62015-01-27 14:46:26 -0800208 : SkString("") {
209 this->append(sink);
210 this->append(src);
djsollen54416de2015-04-03 07:24:48 -0700211 this->append(srcOptions);
mtklein62bd1a62015-01-27 14:46:26 -0800212 this->append(name);
213 this->append(md5);
mtklein62bd1a62015-01-27 14:46:26 -0800214 }
mtkleinc8d1dd42015-10-15 12:23:01 -0700215 struct Hash {
216 uint32_t operator()(const Gold& g) const {
217 return SkGoodHash()((const SkString&)g);
218 }
219 };
mtklein62bd1a62015-01-27 14:46:26 -0800220};
mtkleina82f5622015-02-20 12:30:19 -0800221static SkTHashSet<Gold, Gold::Hash> gGold;
mtklein62bd1a62015-01-27 14:46:26 -0800222
223static void add_gold(JsonWriter::BitmapResult r) {
djsollen54416de2015-04-03 07:24:48 -0700224 gGold.add(Gold(r.config, r.sourceType, r.sourceOptions, r.name, r.md5));
mtklein62bd1a62015-01-27 14:46:26 -0800225}
226
227static void gather_gold() {
228 if (!FLAGS_readPath.isEmpty()) {
229 SkString path(FLAGS_readPath[0]);
230 path.append("/dm.json");
231 if (!JsonWriter::ReadJson(path.c_str(), add_gold)) {
232 fail(SkStringPrintf("Couldn't read %s for golden results.", path.c_str()));
233 }
234 }
235}
236
237/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
238
borenet09ed4802015-04-03 14:15:33 -0700239static SkTHashSet<SkString> gUninterestingHashes;
240
241static void gather_uninteresting_hashes() {
242 if (!FLAGS_uninterestingHashesFile.isEmpty()) {
243 SkAutoTUnref<SkData> data(SkData::NewFromFileName(FLAGS_uninterestingHashesFile[0]));
mtkleincc334b32015-09-22 11:43:53 -0700244 if (!data) {
245 SkDebugf("WARNING: unable to read uninteresting hashes from %s\n",
246 FLAGS_uninterestingHashesFile[0]);
247 return;
248 }
borenet09ed4802015-04-03 14:15:33 -0700249 SkTArray<SkString> hashes;
250 SkStrSplit((const char*)data->data(), "\n", &hashes);
251 for (const SkString& hash : hashes) {
252 gUninterestingHashes.add(hash);
253 }
mtklein136baaa2016-02-03 11:21:45 -0800254 SkDebugf("FYI: loaded %d distinct uninteresting hashes from %d lines\n",
255 gUninterestingHashes.count(), hashes.count());
borenet09ed4802015-04-03 14:15:33 -0700256 }
257}
258
259/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
260
mtkleine0effd62015-07-29 06:37:28 -0700261struct TaggedSrc : public SkAutoTDelete<Src> {
mtklein3eed7dd2016-02-24 19:07:07 -0800262 SkString tag;
263 SkString options;
mtkleine0effd62015-07-29 06:37:28 -0700264};
265
266struct TaggedSink : public SkAutoTDelete<Sink> {
kkinnunen3e980c32015-12-23 01:33:00 -0800267 SkString tag;
djsollen54416de2015-04-03 07:24:48 -0700268};
mtklein748ca3b2015-01-15 10:56:12 -0800269
270static const bool kMemcpyOK = true;
271
mtkleine0effd62015-07-29 06:37:28 -0700272static SkTArray<TaggedSrc, kMemcpyOK> gSrcs;
273static SkTArray<TaggedSink, kMemcpyOK> gSinks;
mtklein748ca3b2015-01-15 10:56:12 -0800274
mtklein6393c062015-04-27 08:45:01 -0700275static bool in_shard() {
276 static int N = 0;
277 return N++ % FLAGS_shards == FLAGS_shard;
278}
279
mtklein3eed7dd2016-02-24 19:07:07 -0800280static void push_src(const char* tag, ImplicitString options, Src* s) {
mtklein748ca3b2015-01-15 10:56:12 -0800281 SkAutoTDelete<Src> src(s);
mtklein6393c062015-04-27 08:45:01 -0700282 if (in_shard() &&
mtklein3eed7dd2016-02-24 19:07:07 -0800283 FLAGS_src.contains(tag) &&
mtklein748ca3b2015-01-15 10:56:12 -0800284 !SkCommandLineFlags::ShouldSkip(FLAGS_match, src->name().c_str())) {
mtkleine0effd62015-07-29 06:37:28 -0700285 TaggedSrc& s = gSrcs.push_back();
mtklein748ca3b2015-01-15 10:56:12 -0800286 s.reset(src.detach());
287 s.tag = tag;
djsollen54416de2015-04-03 07:24:48 -0700288 s.options = options;
mtklein114c3cd2015-01-15 10:15:02 -0800289 }
mtklein748ca3b2015-01-15 10:56:12 -0800290}
mtklein114c3cd2015-01-15 10:15:02 -0800291
msarett9e707a02015-09-01 14:57:57 -0700292static void push_codec_src(Path path, CodecSrc::Mode mode, CodecSrc::DstColorType dstColorType,
scroggoc5560be2016-02-03 09:42:42 -0800293 SkAlphaType dstAlphaType, float scale) {
scroggoe4499842016-02-25 11:03:47 -0800294 if (FLAGS_simpleCodec) {
295 if (mode != CodecSrc::kCodec_Mode || dstColorType != CodecSrc::kGetFromCanvas_DstColorType
296 || scale != 1.0f)
297 // Only decode in the simple case.
298 return;
299 }
msarett9e707a02015-09-01 14:57:57 -0700300 SkString folder;
301 switch (mode) {
302 case CodecSrc::kCodec_Mode:
303 folder.append("codec");
304 break;
msarettbb25b532016-01-13 09:31:39 -0800305 case CodecSrc::kCodecZeroInit_Mode:
306 folder.append("codec_zero_init");
307 break;
msarett9e707a02015-09-01 14:57:57 -0700308 case CodecSrc::kScanline_Mode:
309 folder.append("scanline");
310 break;
msarett9e707a02015-09-01 14:57:57 -0700311 case CodecSrc::kStripe_Mode:
312 folder.append("stripe");
313 break;
msarett91c22b22016-02-22 12:27:46 -0800314 case CodecSrc::kCroppedScanline_Mode:
315 folder.append("crop");
316 break;
msarett9e707a02015-09-01 14:57:57 -0700317 case CodecSrc::kSubset_Mode:
msarett36c37962015-09-02 13:20:52 -0700318 folder.append("codec_subset");
msarett9e707a02015-09-01 14:57:57 -0700319 break;
msarettb714fb02016-01-22 14:46:42 -0800320 case CodecSrc::kGen_Mode:
321 folder.append("gen");
322 break;
msarett9e707a02015-09-01 14:57:57 -0700323 }
324
325 switch (dstColorType) {
326 case CodecSrc::kGrayscale_Always_DstColorType:
327 folder.append("_kGray8");
328 break;
329 case CodecSrc::kIndex8_Always_DstColorType:
330 folder.append("_kIndex8");
331 break;
332 default:
333 break;
334 }
335
scroggoc5560be2016-02-03 09:42:42 -0800336 switch (dstAlphaType) {
337 case kOpaque_SkAlphaType:
338 folder.append("_opaque");
339 break;
340 case kPremul_SkAlphaType:
341 folder.append("_premul");
342 break;
343 case kUnpremul_SkAlphaType:
344 folder.append("_unpremul");
345 break;
346 default:
347 break;
348 }
349
msarett9e707a02015-09-01 14:57:57 -0700350 if (1.0f != scale) {
351 folder.appendf("_%.3f", scale);
352 }
353
scroggoc5560be2016-02-03 09:42:42 -0800354 CodecSrc* src = new CodecSrc(path, mode, dstColorType, dstAlphaType, scale);
msarett9e707a02015-09-01 14:57:57 -0700355 push_src("image", folder, src);
356}
357
msarett3d9d7a72015-10-21 10:27:10 -0700358static void push_android_codec_src(Path path, AndroidCodecSrc::Mode mode,
scroggoc5560be2016-02-03 09:42:42 -0800359 CodecSrc::DstColorType dstColorType, SkAlphaType dstAlphaType, int sampleSize) {
msarett3d9d7a72015-10-21 10:27:10 -0700360 SkString folder;
361 switch (mode) {
362 case AndroidCodecSrc::kFullImage_Mode:
363 folder.append("scaled_codec");
364 break;
365 case AndroidCodecSrc::kDivisor_Mode:
366 folder.append("scaled_codec_divisor");
367 break;
368 }
369
370 switch (dstColorType) {
371 case CodecSrc::kGrayscale_Always_DstColorType:
372 folder.append("_kGray8");
373 break;
374 case CodecSrc::kIndex8_Always_DstColorType:
375 folder.append("_kIndex8");
376 break;
377 default:
378 break;
379 }
380
scroggoc5560be2016-02-03 09:42:42 -0800381 switch (dstAlphaType) {
382 case kOpaque_SkAlphaType:
383 folder.append("_opaque");
384 break;
385 case kPremul_SkAlphaType:
386 folder.append("_premul");
387 break;
msarett9e9444c2016-02-03 12:39:10 -0800388 case kUnpremul_SkAlphaType:
389 folder.append("_unpremul");
390 break;
scroggoc5560be2016-02-03 09:42:42 -0800391 default:
392 break;
393 }
394
msarett3d9d7a72015-10-21 10:27:10 -0700395 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800396 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700397 }
398
scroggoc5560be2016-02-03 09:42:42 -0800399 AndroidCodecSrc* src = new AndroidCodecSrc(path, mode, dstColorType, dstAlphaType, sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700400 push_src("image", folder, src);
401}
402
msarett438b2ad2015-04-09 12:43:10 -0700403static void push_codec_srcs(Path path) {
404 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
405 if (!encoded) {
406 SkDebugf("Couldn't read %s.", path.c_str());
407 return;
408 }
409 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
halcanary96fcdcc2015-08-27 07:41:13 -0700410 if (nullptr == codec.get()) {
msarett438b2ad2015-04-09 12:43:10 -0700411 SkDebugf("Couldn't create codec for %s.", path.c_str());
412 return;
413 }
414
msarett36c37962015-09-02 13:20:52 -0700415 // Native Scales
emmaleer8f4ba762015-08-14 07:44:46 -0700416 // SkJpegCodec natively supports scaling to: 0.125, 0.25, 0.375, 0.5, 0.625, 0.75, 0.875
msarett36c37962015-09-02 13:20:52 -0700417 const float nativeScales[] = { 0.125f, 0.25f, 0.375f, 0.5f, 0.625f, 0.750f, 0.875f, 1.0f };
msarett438b2ad2015-04-09 12:43:10 -0700418
msarett91c22b22016-02-22 12:27:46 -0800419 SkTArray<CodecSrc::Mode> nativeModes;
420 nativeModes.push_back(CodecSrc::kCodec_Mode);
421 nativeModes.push_back(CodecSrc::kCodecZeroInit_Mode);
422 nativeModes.push_back(CodecSrc::kGen_Mode);
423 switch (codec->getEncodedFormat()) {
424 case SkEncodedFormat::kJPEG_SkEncodedFormat:
425 nativeModes.push_back(CodecSrc::kScanline_Mode);
426 nativeModes.push_back(CodecSrc::kStripe_Mode);
427 nativeModes.push_back(CodecSrc::kCroppedScanline_Mode);
428 break;
429 case SkEncodedFormat::kWEBP_SkEncodedFormat:
430 nativeModes.push_back(CodecSrc::kSubset_Mode);
431 break;
msarettb65e6042016-02-23 05:37:25 -0800432 case SkEncodedFormat::kRAW_SkEncodedFormat:
433 break;
msarett91c22b22016-02-22 12:27:46 -0800434 default:
435 nativeModes.push_back(CodecSrc::kScanline_Mode);
436 nativeModes.push_back(CodecSrc::kStripe_Mode);
437 break;
438 }
msarett9e707a02015-09-01 14:57:57 -0700439
msarett91c22b22016-02-22 12:27:46 -0800440 SkTArray<CodecSrc::DstColorType> colorTypes;
441 colorTypes.push_back(CodecSrc::kGetFromCanvas_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700442 switch (codec->getInfo().colorType()) {
443 case kGray_8_SkColorType:
msarett91c22b22016-02-22 12:27:46 -0800444 colorTypes.push_back(CodecSrc::kGrayscale_Always_DstColorType);
msarett55f7bdd2016-02-16 13:24:54 -0800445 if (kWBMP_SkEncodedFormat == codec->getEncodedFormat()) {
msarett91c22b22016-02-22 12:27:46 -0800446 colorTypes.push_back(CodecSrc::kIndex8_Always_DstColorType);
msarett55f7bdd2016-02-16 13:24:54 -0800447 }
msarett36c37962015-09-02 13:20:52 -0700448 break;
449 case kIndex_8_SkColorType:
msarett91c22b22016-02-22 12:27:46 -0800450 colorTypes.push_back(CodecSrc::kIndex8_Always_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700451 break;
452 default:
msarett36c37962015-09-02 13:20:52 -0700453 break;
454 }
msarett9e707a02015-09-01 14:57:57 -0700455
scroggoc5560be2016-02-03 09:42:42 -0800456 SkTArray<SkAlphaType> alphaModes;
457 alphaModes.push_back(kPremul_SkAlphaType);
msarett9e9444c2016-02-03 12:39:10 -0800458 alphaModes.push_back(kUnpremul_SkAlphaType);
scroggoc5560be2016-02-03 09:42:42 -0800459 if (codec->getInfo().alphaType() == kOpaque_SkAlphaType) {
460 alphaModes.push_back(kOpaque_SkAlphaType);
461 }
msarettb714fb02016-01-22 14:46:42 -0800462
463 for (CodecSrc::Mode mode : nativeModes) {
464 // SkCodecImageGenerator only runs for the default colorType
465 // recommended by SkCodec. There is no need to generate multiple
466 // tests for different colorTypes.
467 // TODO (msarett): Add scaling support to SkCodecImageGenerator.
468 if (CodecSrc::kGen_Mode == mode) {
469 // FIXME: The gpu backend does not draw kGray sources correctly. (skbug.com/4822)
470 if (kGray_8_SkColorType != codec->getInfo().colorType()) {
scroggoc5560be2016-02-03 09:42:42 -0800471 push_codec_src(path, mode, CodecSrc::kGetFromCanvas_DstColorType,
472 codec->getInfo().alphaType(), 1.0f);
msarettb714fb02016-01-22 14:46:42 -0800473 }
474 continue;
475 }
476
477 for (float scale : nativeScales) {
msarett91c22b22016-02-22 12:27:46 -0800478 for (CodecSrc::DstColorType colorType : colorTypes) {
scroggoc5560be2016-02-03 09:42:42 -0800479 for (SkAlphaType alphaType : alphaModes) {
msarett91c22b22016-02-22 12:27:46 -0800480 // Only test kCroppedScanline_Mode when the alpha type is opaque. The test is
481 // slow and won't be interestingly different with different alpha types.
482 if (CodecSrc::kCroppedScanline_Mode == mode &&
483 kOpaque_SkAlphaType != alphaType) {
484 continue;
485 }
486
487 push_codec_src(path, mode, colorType, alphaType, scale);
scroggoc5560be2016-02-03 09:42:42 -0800488 }
msarett9e707a02015-09-01 14:57:57 -0700489 }
490 }
msarett0a242972015-06-11 14:27:27 -0700491 }
msarett36c37962015-09-02 13:20:52 -0700492
scroggoe4499842016-02-25 11:03:47 -0800493 if (FLAGS_simpleCodec) {
494 return;
495 }
496
halcanary6950de62015-11-07 05:29:00 -0800497 // https://bug.skia.org/4428
msarett33c76232015-11-16 13:43:40 -0800498 bool subset = false;
499 // The following image types are supported by BitmapRegionDecoder,
500 // so we will test full image decodes and subset decodes.
msarettbe8216a2015-12-04 08:00:50 -0800501 static const char* const exts[] = {
msarett3d9d7a72015-10-21 10:27:10 -0700502 "jpg", "jpeg", "png", "webp",
503 "JPG", "JPEG", "PNG", "WEBP",
504 };
msarettbe8216a2015-12-04 08:00:50 -0800505 for (const char* ext : exts) {
msarett3d9d7a72015-10-21 10:27:10 -0700506 if (path.endsWith(ext)) {
msarett33c76232015-11-16 13:43:40 -0800507 subset = true;
msarett3d9d7a72015-10-21 10:27:10 -0700508 break;
509 }
510 }
msarett33c76232015-11-16 13:43:40 -0800511
msarett3d9d7a72015-10-21 10:27:10 -0700512 const int sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
msarett36c37962015-09-02 13:20:52 -0700513
msarett3d9d7a72015-10-21 10:27:10 -0700514 for (int sampleSize : sampleSizes) {
msarett91c22b22016-02-22 12:27:46 -0800515 for (CodecSrc::DstColorType colorType : colorTypes) {
scroggoc5560be2016-02-03 09:42:42 -0800516 for (SkAlphaType alphaType : alphaModes) {
msarett91c22b22016-02-22 12:27:46 -0800517 push_android_codec_src(path, AndroidCodecSrc::kFullImage_Mode, colorType,
scroggoc5560be2016-02-03 09:42:42 -0800518 alphaType, sampleSize);
519 if (subset) {
msarett91c22b22016-02-22 12:27:46 -0800520 push_android_codec_src(path, AndroidCodecSrc::kDivisor_Mode, colorType,
scroggoc5560be2016-02-03 09:42:42 -0800521 alphaType, sampleSize);
522 }
msarett3d9d7a72015-10-21 10:27:10 -0700523 }
msarett36c37962015-09-02 13:20:52 -0700524 }
525 }
msarett438b2ad2015-04-09 12:43:10 -0700526}
527
msarett5cb48852015-11-06 08:56:32 -0800528static bool brd_color_type_supported(SkBitmapRegionDecoder::Strategy strategy,
msaretta5783ae2015-09-08 15:35:32 -0700529 CodecSrc::DstColorType dstColorType) {
530 switch (strategy) {
msarett5cb48852015-11-06 08:56:32 -0800531 case SkBitmapRegionDecoder::kCanvas_Strategy:
msaretta5783ae2015-09-08 15:35:32 -0700532 if (CodecSrc::kGetFromCanvas_DstColorType == dstColorType) {
533 return true;
534 }
535 return false;
msarett5cb48852015-11-06 08:56:32 -0800536 case SkBitmapRegionDecoder::kAndroidCodec_Strategy:
msarett26ad17b2015-10-22 07:29:19 -0700537 switch (dstColorType) {
538 case CodecSrc::kGetFromCanvas_DstColorType:
539 case CodecSrc::kIndex8_Always_DstColorType:
540 case CodecSrc::kGrayscale_Always_DstColorType:
541 return true;
542 default:
543 return false;
544 }
msaretta5783ae2015-09-08 15:35:32 -0700545 default:
546 SkASSERT(false);
547 return false;
548 }
549}
550
msarett5cb48852015-11-06 08:56:32 -0800551static void push_brd_src(Path path, SkBitmapRegionDecoder::Strategy strategy,
msaretta5783ae2015-09-08 15:35:32 -0700552 CodecSrc::DstColorType dstColorType, BRDSrc::Mode mode, uint32_t sampleSize) {
553 SkString folder;
554 switch (strategy) {
msarett5cb48852015-11-06 08:56:32 -0800555 case SkBitmapRegionDecoder::kCanvas_Strategy:
msaretta5783ae2015-09-08 15:35:32 -0700556 folder.append("brd_canvas");
557 break;
msarett5cb48852015-11-06 08:56:32 -0800558 case SkBitmapRegionDecoder::kAndroidCodec_Strategy:
msarett26ad17b2015-10-22 07:29:19 -0700559 folder.append("brd_android_codec");
560 break;
msaretta5783ae2015-09-08 15:35:32 -0700561 default:
562 SkASSERT(false);
563 return;
564 }
565
566 switch (mode) {
567 case BRDSrc::kFullImage_Mode:
568 break;
569 case BRDSrc::kDivisor_Mode:
570 folder.append("_divisor");
571 break;
572 default:
573 SkASSERT(false);
574 return;
575 }
576
577 switch (dstColorType) {
578 case CodecSrc::kGetFromCanvas_DstColorType:
579 break;
580 case CodecSrc::kIndex8_Always_DstColorType:
581 folder.append("_kIndex");
582 break;
583 case CodecSrc::kGrayscale_Always_DstColorType:
584 folder.append("_kGray");
585 break;
586 default:
587 SkASSERT(false);
588 return;
589 }
590
591 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800592 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700593 }
594
595 BRDSrc* src = new BRDSrc(path, strategy, mode, dstColorType, sampleSize);
596 push_src("image", folder, src);
597}
598
599static void push_brd_srcs(Path path) {
600
msarett5cb48852015-11-06 08:56:32 -0800601 const SkBitmapRegionDecoder::Strategy strategies[] = {
602 SkBitmapRegionDecoder::kCanvas_Strategy,
msarett5cb48852015-11-06 08:56:32 -0800603 SkBitmapRegionDecoder::kAndroidCodec_Strategy,
msaretta5783ae2015-09-08 15:35:32 -0700604 };
605
scroggo501b7342015-11-03 07:55:11 -0800606 // Test on a variety of sampleSizes, making sure to include:
607 // - 2, 4, and 8, which are natively supported by jpeg
608 // - multiples of 2 which are not divisible by 4 (analogous for 4)
609 // - larger powers of two, since BRD clients generally use powers of 2
610 // We will only produce output for the larger sizes on large images.
611 const uint32_t sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 24, 32, 64 };
msarett6efbe052015-09-11 09:01:16 -0700612
msaretta5783ae2015-09-08 15:35:32 -0700613 // We will only test to one backend (8888), but we will test all of the
614 // color types that we need to decode to on this backend.
615 const CodecSrc::DstColorType dstColorTypes[] = {
msarett26ad17b2015-10-22 07:29:19 -0700616 CodecSrc::kGetFromCanvas_DstColorType,
617 CodecSrc::kIndex8_Always_DstColorType,
618 CodecSrc::kGrayscale_Always_DstColorType,
msaretta5783ae2015-09-08 15:35:32 -0700619 };
620
621 const BRDSrc::Mode modes[] = {
msarett26ad17b2015-10-22 07:29:19 -0700622 BRDSrc::kFullImage_Mode,
623 BRDSrc::kDivisor_Mode,
msaretta5783ae2015-09-08 15:35:32 -0700624 };
625
msarett5cb48852015-11-06 08:56:32 -0800626 for (SkBitmapRegionDecoder::Strategy strategy : strategies) {
msarett6efbe052015-09-11 09:01:16 -0700627 for (uint32_t sampleSize : sampleSizes) {
msarett6efbe052015-09-11 09:01:16 -0700628 for (CodecSrc::DstColorType dstColorType : dstColorTypes) {
629 if (brd_color_type_supported(strategy, dstColorType)) {
630 for (BRDSrc::Mode mode : modes) {
msaretta5783ae2015-09-08 15:35:32 -0700631 push_brd_src(path, strategy, dstColorType, mode, sampleSize);
632 }
633 }
634 }
635 }
636 }
637}
638
639static bool brd_supported(const char* ext) {
msarett8c8f22a2015-04-01 06:58:48 -0700640 static const char* const exts[] = {
msaretta5783ae2015-09-08 15:35:32 -0700641 "jpg", "jpeg", "png", "webp",
642 "JPG", "JPEG", "PNG", "WEBP",
msarett8c8f22a2015-04-01 06:58:48 -0700643 };
644
645 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
646 if (0 == strcmp(exts[i], ext)) {
647 return true;
648 }
649 }
650 return false;
scroggo9b77ddd2015-03-19 06:03:39 -0700651}
652
scroggo86737142016-02-03 12:19:11 -0800653static bool gather_srcs() {
mtklein748ca3b2015-01-15 10:56:12 -0800654 for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) {
djsollen54416de2015-04-03 07:24:48 -0700655 push_src("gm", "", new GMSrc(r->factory()));
mtklein748ca3b2015-01-15 10:56:12 -0800656 }
halcanaryfc37ad12015-01-30 07:31:19 -0800657 for (int i = 0; i < FLAGS_skps.count(); i++) {
658 const char* path = FLAGS_skps[i];
659 if (sk_isdir(path)) {
660 SkOSFile::Iter it(path, "skp");
661 for (SkString file; it.next(&file); ) {
djsollen54416de2015-04-03 07:24:48 -0700662 push_src("skp", "", new SKPSrc(SkOSPath::Join(path, file.c_str())));
halcanaryfc37ad12015-01-30 07:31:19 -0800663 }
664 } else {
djsollen54416de2015-04-03 07:24:48 -0700665 push_src("skp", "", new SKPSrc(path));
mtklein114c3cd2015-01-15 10:15:02 -0800666 }
667 }
scroggo86737142016-02-03 12:19:11 -0800668
669 SkTArray<SkString> images;
670 if (!CollectImages(&images)) {
671 return false;
672 }
673
674 for (auto image : images) {
675 push_codec_srcs(image);
scroggoe4499842016-02-25 11:03:47 -0800676 if (FLAGS_simpleCodec) {
677 continue;
678 }
679
mtklein21eaf3b2016-02-08 12:39:59 -0800680 const char* ext = strrchr(image.c_str(), '.');
681 if (ext && brd_supported(ext+1)) {
scroggo86737142016-02-03 12:19:11 -0800682 push_brd_srcs(image);
mtklein709d2c32015-01-15 08:30:25 -0800683 }
mtklein709d2c32015-01-15 08:30:25 -0800684 }
scroggo86737142016-02-03 12:19:11 -0800685
686 return true;
mtklein709d2c32015-01-15 08:30:25 -0800687}
688
kkinnunen3e980c32015-12-23 01:33:00 -0800689static void push_sink(const SkCommandLineConfig& config, Sink* s) {
rmistry0f515bd2015-12-22 10:22:26 -0800690 SkAutoTDelete<Sink> sink(s);
kkinnunen3e980c32015-12-23 01:33:00 -0800691
mtkleine0effd62015-07-29 06:37:28 -0700692 // Try a simple Src as a canary. If it fails, skip this sink.
mtklein748ca3b2015-01-15 10:56:12 -0800693 struct : public Src {
mtkleine0effd62015-07-29 06:37:28 -0700694 Error draw(SkCanvas* c) const override {
695 c->drawRect(SkRect::MakeWH(1,1), SkPaint());
696 return "";
697 }
mtklein36352bf2015-03-25 18:17:31 -0700698 SkISize size() const override { return SkISize::Make(16, 16); }
mtkleine0effd62015-07-29 06:37:28 -0700699 Name name() const override { return "justOneRect"; }
700 } justOneRect;
mtklein114c3cd2015-01-15 10:15:02 -0800701
mtklein748ca3b2015-01-15 10:56:12 -0800702 SkBitmap bitmap;
703 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800704 SkString log;
mtkleine0effd62015-07-29 06:37:28 -0700705 Error err = sink->draw(justOneRect, &bitmap, &stream, &log);
mtklein4089ef72015-03-05 08:40:28 -0800706 if (err.isFatal()) {
kkinnunen3e980c32015-12-23 01:33:00 -0800707 SkDebugf("Could not run %s: %s\n", config.getTag().c_str(), err.c_str());
mtklein05641a52015-04-21 10:49:13 -0700708 exit(1);
mtklein114c3cd2015-01-15 10:15:02 -0800709 }
710
mtkleine0effd62015-07-29 06:37:28 -0700711 TaggedSink& ts = gSinks.push_back();
mtklein748ca3b2015-01-15 10:56:12 -0800712 ts.reset(sink.detach());
kkinnunen3e980c32015-12-23 01:33:00 -0800713 ts.tag = config.getTag();
mtklein748ca3b2015-01-15 10:56:12 -0800714}
715
716static bool gpu_supported() {
717#if SK_SUPPORT_GPU
718 return FLAGS_gpu;
719#else
720 return false;
721#endif
722}
kkinnunen9ebc3f02015-12-21 23:48:13 -0800723
kkinnunen3e980c32015-12-23 01:33:00 -0800724static Sink* create_sink(const SkCommandLineConfig* config) {
725#if SK_SUPPORT_GPU
726 if (gpu_supported()) {
727 if (const SkCommandLineConfigGpu* gpuConfig = config->asConfigGpu()) {
728 GrContextFactory::GLContextType contextType = gpuConfig->getContextType();
729 GrContextFactory::GLContextOptions contextOptions =
730 GrContextFactory::kNone_GLContextOptions;
731 if (gpuConfig->getUseNVPR()) {
732 contextOptions = static_cast<GrContextFactory::GLContextOptions>(
733 contextOptions | GrContextFactory::kEnableNVPR_GLContextOptions);
734 }
735 GrContextFactory testFactory;
736 if (!testFactory.get(contextType, contextOptions)) {
737 SkDebugf("WARNING: can not create GPU context for config '%s'. "
738 "GM tests will be skipped.\n", gpuConfig->getTag().c_str());
739 return nullptr;
740 }
741 return new GPUSink(contextType, contextOptions, gpuConfig->getSamples(),
brianosman744898a2016-03-01 13:44:28 -0800742 gpuConfig->getUseDIText(), FLAGS_gpu_threading);
kkinnunen3e980c32015-12-23 01:33:00 -0800743 }
744 }
745#endif
746
747#define SINK(t, sink, ...) if (config->getBackend().equals(t)) { return new sink(__VA_ARGS__); }
748
tomhudsoneebc39a2015-02-23 12:18:05 -0800749#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
750 SINK("hwui", HWUISink);
751#endif
752
mtklein748ca3b2015-01-15 10:56:12 -0800753 if (FLAGS_cpu) {
754 SINK("565", RasterSink, kRGB_565_SkColorType);
755 SINK("8888", RasterSink, kN32_SkColorType);
mtklein27c3fdd2016-02-26 14:43:21 -0800756 SINK("srgb", RasterSink, kN32_SkColorType, kSRGB_SkColorProfileType);
757 SINK("f16", RasterSink, kRGBA_F16_SkColorType);
halcanaryc11c62f2015-09-28 11:51:54 -0700758 SINK("pdf", PDFSink, "Pdfium");
759 SINK("pdf_poppler", PDFSink, "Poppler");
mtklein9c3f17d2015-01-28 11:35:18 -0800760 SINK("skp", SKPSink);
mtklein8a4527e2015-01-31 20:00:58 -0800761 SINK("svg", SVGSink);
762 SINK("null", NullSink);
halcanary47ef4d52015-03-03 09:13:09 -0800763 SINK("xps", XPSSink);
mtklein748ca3b2015-01-15 10:56:12 -0800764 }
765#undef SINK
halcanary96fcdcc2015-08-27 07:41:13 -0700766 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800767}
768
kkinnunen3e980c32015-12-23 01:33:00 -0800769static Sink* create_via(const SkString& tag, Sink* wrapped) {
770#define VIA(t, via, ...) if (tag.equals(t)) { return new via(__VA_ARGS__); }
halcanary96fcdcc2015-08-27 07:41:13 -0700771 VIA("twice", ViaTwice, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700772 VIA("serialize", ViaSerialization, wrapped);
mtklein4a34ecb2016-01-08 10:19:35 -0800773 VIA("pic", ViaPicture, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700774 VIA("2ndpic", ViaSecondPicture, wrapped);
mtkleind31c13d2015-05-05 12:59:56 -0700775 VIA("sp", ViaSingletonPictures, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700776 VIA("tiles", ViaTiles, 256, 256, nullptr, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800777 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
mtklein2e2ea382015-10-16 10:29:41 -0700778 VIA("remote", ViaRemote, false, wrapped);
779 VIA("remote_cache", ViaRemote, true, wrapped);
halcanary7a76f9c2016-02-03 11:53:18 -0800780 VIA("mojo", ViaMojo, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800781
mtkleind603b222015-02-17 11:13:33 -0800782 if (FLAGS_matrix.count() == 4) {
mtklein748ca3b2015-01-15 10:56:12 -0800783 SkMatrix m;
mtkleind603b222015-02-17 11:13:33 -0800784 m.reset();
785 m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
786 m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
787 m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
788 m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
789 VIA("matrix", ViaMatrix, m, wrapped);
790 VIA("upright", ViaUpright, m, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800791 }
tomhudson64de1e12015-03-05 08:01:07 -0800792
793#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
794 VIA("androidsdk", ViaAndroidSDK, wrapped);
795#endif
796
mtklein748ca3b2015-01-15 10:56:12 -0800797#undef VIA
halcanary96fcdcc2015-08-27 07:41:13 -0700798 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800799}
800
mtklein748ca3b2015-01-15 10:56:12 -0800801static void gather_sinks() {
kkinnunen3e980c32015-12-23 01:33:00 -0800802 SkCommandLineConfigArray configs;
803 ParseConfigs(FLAGS_config, &configs);
804 for (int i = 0; i < configs.count(); i++) {
805 const SkCommandLineConfig& config = *configs[i];
806 Sink* sink = create_sink(&config);
807 if (sink == nullptr) {
808 SkDebugf("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
809 config.getTag().c_str());
810 continue;
811 }
mtklein748ca3b2015-01-15 10:56:12 -0800812
kkinnunen3e980c32015-12-23 01:33:00 -0800813 const SkTArray<SkString>& parts = config.getViaParts();
814 for (int j = parts.count(); j-- > 0;) {
815 const SkString& part = parts[j];
816 Sink* next = create_via(part, sink);
halcanary96fcdcc2015-08-27 07:41:13 -0700817 if (next == nullptr) {
kkinnunen3e980c32015-12-23 01:33:00 -0800818 SkDebugf("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
819 part.c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800820 delete sink;
halcanary96fcdcc2015-08-27 07:41:13 -0700821 sink = nullptr;
mtklein748ca3b2015-01-15 10:56:12 -0800822 break;
823 }
824 sink = next;
825 }
826 if (sink) {
827 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800828 }
829 }
830}
mtklein709d2c32015-01-15 08:30:25 -0800831
mtkleina5114d72015-08-24 13:27:01 -0700832static bool dump_png(SkBitmap bitmap, const char* path, const char* md5) {
833 const int w = bitmap.width(),
834 h = bitmap.height();
mtklein27c3fdd2016-02-26 14:43:21 -0800835 // PNG wants unpremultiplied 8-bit RGBA pixels (16-bit could work fine too).
836 // We leave the gamma of these bytes unspecified, to continue the status quo,
837 // which we think generally is to interpret them as sRGB.
mtkleina5114d72015-08-24 13:27:01 -0700838
mtklein27c3fdd2016-02-26 14:43:21 -0800839 SkAutoTMalloc<uint32_t> rgba(w*h);
840
841 if (bitmap. colorType() == kN32_SkColorType &&
842 bitmap.profileType() == kSRGB_SkColorProfileType) {
843 // These are premul sRGB 8-bit pixels in SkPMColor order.
844 // We want unpremul sRGB 8-bit pixels in RGBA order. We'll get there via floats.
845 bitmap.lockPixels();
846 auto px = (const uint32_t*)bitmap.getPixels();
847 if (!px) {
mtkleina5114d72015-08-24 13:27:01 -0700848 return false;
849 }
mtklein27c3fdd2016-02-26 14:43:21 -0800850 for (int i = 0; i < w*h; i++) {
851 Sk4f fs = Sk4f_fromS32(px[i]); // Convert up to linear floats.
852 #if defined(SK_PMCOLOR_IS_BGRA)
853 fs = SkNx_shuffle<2,1,0,3>(fs); // Shuffle to RGBA, if not there already.
854 #endif
855 float invA = 1.0f / fs[3];
856 fs = fs * Sk4f(invA, invA, invA, 1); // Unpremultiply.
857 rgba[i] = Sk4f_toS32(fs); // Pack down to sRGB bytes.
858 }
mtkleina5114d72015-08-24 13:27:01 -0700859
mtklein27c3fdd2016-02-26 14:43:21 -0800860 } else if (bitmap.colorType() == kRGBA_F16_SkColorType) {
861 // These are premul linear half-float pixels in RGBA order.
862 // We want unpremul sRGB 8-bit pixels in RGBA order. We'll get there via floats.
863 bitmap.lockPixels();
864 auto px = (const uint64_t*)bitmap.getPixels();
865 if (!px) {
866 return false;
867 }
868 for (int i = 0; i < w*h; i++) {
869 Sk4f fs = SkHalfToFloat_01(px[i]); // Convert up to linear floats.
870 float invA = 1.0f / fs[3];
871 fs = fs * Sk4f(invA, invA, invA, 1); // Unpremultiply.
872 rgba[i] = Sk4f_toS32(fs); // Pack down to sRGB bytes.
873 }
874
875
876 } else {
877 // We "should" gamma correct in here but we don't.
878 // We want Gold to show exactly what our clients are seeing, broken gamma.
879
880 // Convert smaller formats up to premul linear 8-bit (in SkPMColor order).
881 if (bitmap.colorType() != kN32_SkColorType) {
882 SkBitmap n32;
883 if (!bitmap.copyTo(&n32, kN32_SkColorType)) {
884 return false;
885 }
886 bitmap = n32;
887 }
888
889 // Convert premul linear 8-bit to unpremul linear 8-bit RGBA.
890 if (!bitmap.readPixels(SkImageInfo::Make(w,h, kRGBA_8888_SkColorType,
891 kUnpremul_SkAlphaType),
892 rgba, 4*w, 0,0)) {
893 return false;
894 }
mtkleina5114d72015-08-24 13:27:01 -0700895 }
896
897 // We don't need bitmap anymore. Might as well drop our ref.
mtkleinfccb77d2015-08-24 14:13:29 -0700898 bitmap.reset();
mtkleina5114d72015-08-24 13:27:01 -0700899
mtkleinc64137c2015-08-25 10:56:08 -0700900 FILE* f = fopen(path, "wb");
mtkleina5114d72015-08-24 13:27:01 -0700901 if (!f) { return false; }
902
903 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
904 if (!png) {
905 fclose(f);
906 return false;
907 }
908
909 png_infop info = png_create_info_struct(png);
910 if (!info) {
911 png_destroy_write_struct(&png, &info);
912 fclose(f);
913 return false;
914 }
915
mtkleind69ece62015-09-10 10:37:44 -0700916 SkString description;
917 description.append("Key: ");
918 for (int i = 0; i < FLAGS_key.count(); i++) {
919 description.appendf("%s ", FLAGS_key[i]);
920 }
921 description.append("Properties: ");
922 for (int i = 0; i < FLAGS_properties.count(); i++) {
923 description.appendf("%s ", FLAGS_properties[i]);
924 }
925 description.appendf("MD5: %s", md5);
926
mtkleina5114d72015-08-24 13:27:01 -0700927 png_text text[2];
928 text[0].key = (png_charp)"Author";
929 text[0].text = (png_charp)"DM dump_png()";
930 text[0].compression = PNG_TEXT_COMPRESSION_NONE;
931 text[1].key = (png_charp)"Description";
mtkleind69ece62015-09-10 10:37:44 -0700932 text[1].text = (png_charp)description.c_str();
mtkleina5114d72015-08-24 13:27:01 -0700933 text[1].compression = PNG_TEXT_COMPRESSION_NONE;
934 png_set_text(png, info, text, 2);
935
936 png_init_io(png, f);
937 png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8,
938 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
939 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
940 png_write_info(png, info);
941 for (int j = 0; j < h; j++) {
942 png_bytep row = (png_bytep)(rgba.get() + w*j);
943 png_write_rows(png, &row, 1);
944 }
945 png_write_end(png, info);
946
947 png_destroy_write_struct(&png, &info);
948 fclose(f);
949 return true;
950}
951
mtkleina2ef6422015-01-15 13:44:22 -0800952static bool match(const char* needle, const char* haystack) {
halcanary96fcdcc2015-08-27 07:41:13 -0700953 return 0 == strcmp("_", needle) || nullptr != strstr(haystack, needle);
mtkleina2ef6422015-01-15 13:44:22 -0800954}
955
mtklein3eed7dd2016-02-24 19:07:07 -0800956static bool is_blacklisted(const char* sink, const char* src,
957 const char* srcOptions, const char* name) {
mtklein0d243ff2015-04-03 07:51:00 -0700958 for (int i = 0; i < FLAGS_blacklist.count() - 3; i += 4) {
mtkleina2ef6422015-01-15 13:44:22 -0800959 if (match(FLAGS_blacklist[i+0], sink) &&
djsollen54416de2015-04-03 07:24:48 -0700960 match(FLAGS_blacklist[i+1], src) &&
961 match(FLAGS_blacklist[i+2], srcOptions) &&
962 match(FLAGS_blacklist[i+3], name)) {
mtklein3eed7dd2016-02-24 19:07:07 -0800963 return true;
mtkleina2ef6422015-01-15 13:44:22 -0800964 }
965 }
mtklein3eed7dd2016-02-24 19:07:07 -0800966 return false;
mtkleina2ef6422015-01-15 13:44:22 -0800967}
968
mtkleincd50bca2016-01-05 06:20:20 -0800969// Even when a Task Sink reports to be non-threadsafe (e.g. GPU), we know things like
970// .png encoding are definitely thread safe. This lets us offload that work to CPU threads.
971static SkTaskGroup gDefinitelyThreadSafeWork;
972
mtklein748ca3b2015-01-15 10:56:12 -0800973// The finest-grained unit of work we can run: draw a single Src into a single Sink,
974// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
975struct Task {
mtkleine0effd62015-07-29 06:37:28 -0700976 Task(const TaggedSrc& src, const TaggedSink& sink) : src(src), sink(sink) {}
977 const TaggedSrc& src;
978 const TaggedSink& sink;
mtklein748ca3b2015-01-15 10:56:12 -0800979
mtklein21eaf3b2016-02-08 12:39:59 -0800980 static void Run(const Task& task) {
981 SkString name = task.src->name();
mtkleine0effd62015-07-29 06:37:28 -0700982
mtkleinb9eb4ac2015-02-02 18:26:03 -0800983 SkString log;
mtklein3eed7dd2016-02-24 19:07:07 -0800984 if (!FLAGS_dryRun) {
mtklein748ca3b2015-01-15 10:56:12 -0800985 SkBitmap bitmap;
986 SkDynamicMemoryWStream stream;
mtklein3eed7dd2016-02-24 19:07:07 -0800987 start(task.sink.tag.c_str(), task.src.tag.c_str(),
988 task.src.options.c_str(), name.c_str());
mtklein21eaf3b2016-02-08 12:39:59 -0800989 Error err = task.sink->draw(*task.src, &bitmap, &stream, &log);
mtklein748ca3b2015-01-15 10:56:12 -0800990 if (!err.isEmpty()) {
mtklein4089ef72015-03-05 08:40:28 -0800991 if (err.isFatal()) {
djsollen54416de2015-04-03 07:24:48 -0700992 fail(SkStringPrintf("%s %s %s %s: %s",
mtklein21eaf3b2016-02-08 12:39:59 -0800993 task.sink.tag.c_str(),
994 task.src.tag.c_str(),
995 task.src.options.c_str(),
mtklein4089ef72015-03-05 08:40:28 -0800996 name.c_str(),
997 err.c_str()));
mtkleinb37cb412015-03-18 05:27:14 -0700998 } else {
mtklein3eed7dd2016-02-24 19:07:07 -0800999 done(task.sink.tag.c_str(), task.src.tag.c_str(),
1000 task.src.options.c_str(), name.c_str());
mtkleinba6ada72016-01-21 09:39:35 -08001001 return;
mtklein4089ef72015-03-05 08:40:28 -08001002 }
mtklein748ca3b2015-01-15 10:56:12 -08001003 }
mtklein62bd1a62015-01-27 14:46:26 -08001004
mtkleincd50bca2016-01-05 06:20:20 -08001005 // We're likely switching threads here, so we must capture by value, [=] or [foo,bar].
1006 SkStreamAsset* data = stream.detachAsStream();
1007 gDefinitelyThreadSafeWork.add([task,name,bitmap,data]{
1008 SkAutoTDelete<SkStreamAsset> ownedData(data);
1009
1010 // Why doesn't the copy constructor do this when we have pre-locked pixels?
1011 bitmap.lockPixels();
1012
1013 SkString md5;
1014 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
1015 SkMD5 hash;
1016 if (data->getLength()) {
1017 hash.writeStream(data, data->getLength());
1018 data->rewind();
mtklein38408462015-07-08 07:25:27 -07001019 } else {
mtkleincd50bca2016-01-05 06:20:20 -08001020 // If we're BGRA (Linux, Windows), swizzle over to RGBA (Mac, Android).
1021 // This helps eliminate multiple 0-pixel-diff hashes on gold.skia.org.
1022 // (Android's general slow speed breaks the tie arbitrarily in RGBA's favor.)
1023 // We might consider promoting 565 to RGBA too.
1024 if (bitmap.colorType() == kBGRA_8888_SkColorType) {
1025 SkBitmap swizzle;
1026 SkAssertResult(bitmap.copyTo(&swizzle, kRGBA_8888_SkColorType));
1027 hash.write(swizzle.getPixels(), swizzle.getSize());
1028 } else {
1029 hash.write(bitmap.getPixels(), bitmap.getSize());
1030 }
1031 }
1032 SkMD5::Digest digest;
1033 hash.finish(digest);
1034 for (int i = 0; i < 16; i++) {
1035 md5.appendf("%02x", digest.data[i]);
mtklein38408462015-07-08 07:25:27 -07001036 }
mtklein62bd1a62015-01-27 14:46:26 -08001037 }
mtklein62bd1a62015-01-27 14:46:26 -08001038
mtkleincd50bca2016-01-05 06:20:20 -08001039 if (!FLAGS_readPath.isEmpty() &&
mtklein3eed7dd2016-02-24 19:07:07 -08001040 !gGold.contains(Gold(task.sink.tag, task.src.tag,
1041 task.src.options, name, md5))) {
mtkleincd50bca2016-01-05 06:20:20 -08001042 fail(SkStringPrintf("%s not found for %s %s %s %s in %s",
1043 md5.c_str(),
mtklein21eaf3b2016-02-08 12:39:59 -08001044 task.sink.tag.c_str(),
1045 task.src.tag.c_str(),
1046 task.src.options.c_str(),
mtkleincd50bca2016-01-05 06:20:20 -08001047 name.c_str(),
1048 FLAGS_readPath[0]));
mtklein748ca3b2015-01-15 10:56:12 -08001049 }
mtkleincd50bca2016-01-05 06:20:20 -08001050
1051 if (!FLAGS_writePath.isEmpty()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001052 const char* ext = task.sink->fileExtension();
mtkleincd50bca2016-01-05 06:20:20 -08001053 if (data->getLength()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001054 WriteToDisk(task, md5, ext, data, data->getLength(), nullptr);
mtkleincd50bca2016-01-05 06:20:20 -08001055 SkASSERT(bitmap.drawsNothing());
1056 } else if (!bitmap.drawsNothing()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001057 WriteToDisk(task, md5, ext, nullptr, 0, &bitmap);
mtkleincd50bca2016-01-05 06:20:20 -08001058 }
1059 }
1060 });
mtklein748ca3b2015-01-15 10:56:12 -08001061 }
mtklein3eed7dd2016-02-24 19:07:07 -08001062 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 -08001063 }
1064
1065 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -08001066 SkString md5,
1067 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -08001068 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -08001069 const SkBitmap* bitmap) {
mtklein409d4702016-02-29 07:38:01 -08001070 bool gammaCorrect = false;
1071 if (bitmap) {
1072 gammaCorrect = bitmap->profileType() == kSRGB_SkColorProfileType
1073 || bitmap-> colorType() == kRGBA_F16_SkColorType;
1074 }
1075
mtklein748ca3b2015-01-15 10:56:12 -08001076 JsonWriter::BitmapResult result;
djsollen54416de2015-04-03 07:24:48 -07001077 result.name = task.src->name();
mtklein3eed7dd2016-02-24 19:07:07 -08001078 result.config = task.sink.tag;
djsollen54416de2015-04-03 07:24:48 -07001079 result.sourceType = task.src.tag;
1080 result.sourceOptions = task.src.options;
1081 result.ext = ext;
mtklein409d4702016-02-29 07:38:01 -08001082 result.gammaCorrect = gammaCorrect;
djsollen54416de2015-04-03 07:24:48 -07001083 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -08001084 JsonWriter::AddBitmapResult(result);
1085
mtkleinb0531a72015-04-07 13:38:48 -07001086 // If an MD5 is uninteresting, we want it noted in the JSON file,
1087 // but don't want to dump it out as a .png (or whatever ext is).
1088 if (gUninterestingHashes.contains(md5)) {
1089 return;
1090 }
1091
mtklein748ca3b2015-01-15 10:56:12 -08001092 const char* dir = FLAGS_writePath[0];
1093 if (0 == strcmp(dir, "@")) { // Needed for iOS.
1094 dir = FLAGS_resourcePath[0];
1095 }
1096 sk_mkdir(dir);
1097
1098 SkString path;
1099 if (FLAGS_nameByHash) {
1100 path = SkOSPath::Join(dir, result.md5.c_str());
1101 path.append(".");
1102 path.append(ext);
1103 if (sk_exists(path.c_str())) {
1104 return; // Content-addressed. If it exists already, we're done.
1105 }
1106 } else {
kkinnunen3e980c32015-12-23 01:33:00 -08001107 path = SkOSPath::Join(dir, task.sink.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001108 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001109 path = SkOSPath::Join(path.c_str(), task.src.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001110 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001111 if (strcmp(task.src.options.c_str(), "") != 0) {
1112 path = SkOSPath::Join(path.c_str(), task.src.options.c_str());
djsollen54416de2015-04-03 07:24:48 -07001113 sk_mkdir(path.c_str());
1114 }
mtklein748ca3b2015-01-15 10:56:12 -08001115 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
1116 path.append(".");
1117 path.append(ext);
1118 }
1119
mtklein748ca3b2015-01-15 10:56:12 -08001120 if (bitmap) {
mtkleina5114d72015-08-24 13:27:01 -07001121 if (!dump_png(*bitmap, path.c_str(), result.md5.c_str())) {
mtklein748ca3b2015-01-15 10:56:12 -08001122 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
1123 return;
1124 }
1125 } else {
mtkleina5114d72015-08-24 13:27:01 -07001126 SkFILEWStream file(path.c_str());
1127 if (!file.isValid()) {
1128 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
1129 return;
1130 }
mtklein748ca3b2015-01-15 10:56:12 -08001131 if (!file.writeStream(data, len)) {
1132 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
1133 return;
1134 }
1135 }
1136 }
1137};
1138
mtklein748ca3b2015-01-15 10:56:12 -08001139/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1140
1141// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
1142
mtklein21eaf3b2016-02-08 12:39:59 -08001143static SkTDArray<skiatest::Test> gParallelTests, gSerialTests;
mtklein748ca3b2015-01-15 10:56:12 -08001144
1145static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -08001146 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -08001147 return;
1148 }
mtklein6393c062015-04-27 08:45:01 -07001149 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r; r = r->next()) {
1150 if (!in_shard()) {
1151 continue;
1152 }
halcanary87f3ba42015-01-20 09:30:20 -08001153 // Despite its name, factory() is returning a reference to
1154 // link-time static const POD data.
1155 const skiatest::Test& test = r->factory();
1156 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -08001157 continue;
1158 }
halcanary87f3ba42015-01-20 09:30:20 -08001159 if (test.needsGpu && gpu_supported()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001160 (FLAGS_gpu_threading ? gParallelTests : gSerialTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -08001161 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein21eaf3b2016-02-08 12:39:59 -08001162 gParallelTests.push(test);
mtklein82d28432015-01-15 12:46:02 -08001163 }
mtklein748ca3b2015-01-15 10:56:12 -08001164 }
1165}
1166
mtklein21eaf3b2016-02-08 12:39:59 -08001167static void run_test(skiatest::Test test) {
halcanary87f3ba42015-01-20 09:30:20 -08001168 struct : public skiatest::Reporter {
mtklein36352bf2015-03-25 18:17:31 -07001169 void reportFailed(const skiatest::Failure& failure) override {
halcanary87f3ba42015-01-20 09:30:20 -08001170 fail(failure.toString());
1171 JsonWriter::AddTestFailure(failure);
1172 }
mtklein36352bf2015-03-25 18:17:31 -07001173 bool allowExtendedTest() const override {
halcanary87f3ba42015-01-20 09:30:20 -08001174 return FLAGS_pathOpsExtended;
1175 }
mtklein36352bf2015-03-25 18:17:31 -07001176 bool verbose() const override { return FLAGS_veryVerbose; }
halcanary87f3ba42015-01-20 09:30:20 -08001177 } reporter;
djsollen824996a2015-06-12 12:06:22 -07001178
mtklein3eed7dd2016-02-24 19:07:07 -08001179 if (!FLAGS_dryRun && !is_blacklisted("_", "tests", "_", test.name)) {
mtklein21eaf3b2016-02-08 12:39:59 -08001180 start("unit", "test", "", test.name);
mtklein55e88b22015-01-21 15:50:13 -08001181 GrContextFactory factory;
mtklein21eaf3b2016-02-08 12:39:59 -08001182 test.proc(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -08001183 }
mtklein3eed7dd2016-02-24 19:07:07 -08001184 done("unit", "test", "", test.name);
mtklein748ca3b2015-01-15 10:56:12 -08001185}
1186
1187/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1188
mtkleinafae30a2016-02-24 12:28:32 -08001189DEFINE_int32(status_sec, 15, "Print status this often (and if we crash).");
1190
1191SkThread* start_status_thread() {
1192 auto thread = new SkThread([] (void*) {
1193 for (;;) {
1194 print_status();
1195 #if defined(SK_BUILD_FOR_WIN)
1196 Sleep(FLAGS_status_sec * 1000);
1197 #else
1198 sleep(FLAGS_status_sec);
1199 #endif
mtklein2e1c47e2015-03-12 07:16:56 -07001200 }
mtkleinafae30a2016-02-24 12:28:32 -08001201 });
1202 thread->start();
1203 return thread;
mtkleinde6fc2b2015-03-12 06:28:54 -07001204}
1205
caryclark83ca6282015-06-10 09:31:09 -07001206#define PORTABLE_FONT_PREFIX "Toy Liberation "
1207
1208static SkTypeface* create_from_name(const char familyName[], SkTypeface::Style style) {
1209 if (familyName && strlen(familyName) > sizeof(PORTABLE_FONT_PREFIX)
1210 && !strncmp(familyName, PORTABLE_FONT_PREFIX, sizeof(PORTABLE_FONT_PREFIX) - 1)) {
caryclark1818acb2015-07-24 12:09:25 -07001211 return sk_tool_utils::create_portable_typeface(familyName, style);
caryclark83ca6282015-06-10 09:31:09 -07001212 }
halcanary96fcdcc2015-08-27 07:41:13 -07001213 return nullptr;
caryclark83ca6282015-06-10 09:31:09 -07001214}
1215
1216#undef PORTABLE_FONT_PREFIX
1217
1218extern SkTypeface* (*gCreateTypefaceDelegate)(const char [], SkTypeface::Style );
1219
jcgregorio3b27ade2014-11-13 08:06:40 -08001220int dm_main();
caryclark17f0b6d2014-07-22 10:15:34 -07001221int dm_main() {
mtklein246ba3a2016-02-23 10:39:36 -08001222 setup_crash_handler();
mtkleinafae30a2016-02-24 12:28:32 -08001223
mtklein5286f022016-01-22 08:18:14 -08001224 JsonWriter::DumpJson(); // It's handy for the bots to assume this is ~never missing.
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +00001225 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -07001226 SkTaskGroup::Enabler enabled(FLAGS_threads);
caryclark83ca6282015-06-10 09:31:09 -07001227 gCreateTypefaceDelegate = &create_from_name;
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +00001228
halcanary7d571242016-02-24 17:59:16 -08001229 {
1230 SkString testResourcePath = GetResourcePath("color_wheel.png");
1231 SkFILEStream testResource(testResourcePath.c_str());
1232 if (!testResource.isValid()) {
1233 SkDebugf("Some resources are missing. Do you need to set --resourcePath?\n");
1234 }
1235 }
mtklein62bd1a62015-01-27 14:46:26 -08001236 gather_gold();
borenet09ed4802015-04-03 14:15:33 -07001237 gather_uninteresting_hashes();
mtklein62bd1a62015-01-27 14:46:26 -08001238
scroggo86737142016-02-03 12:19:11 -08001239 if (!gather_srcs()) {
1240 return 1;
1241 }
mtklein748ca3b2015-01-15 10:56:12 -08001242 gather_sinks();
1243 gather_tests();
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +00001244
mtklein21eaf3b2016-02-08 12:39:59 -08001245 gPending = gSrcs.count() * gSinks.count() + gParallelTests.count() + gSerialTests.count();
mtkleinafae30a2016-02-24 12:28:32 -08001246 SkDebugf("%d srcs * %d sinks + %d tests == %d tasks",
mtklein21eaf3b2016-02-08 12:39:59 -08001247 gSrcs.count(), gSinks.count(), gParallelTests.count() + gSerialTests.count(), gPending);
mtkleinafae30a2016-02-24 12:28:32 -08001248 SkAutoTDelete<SkThread> statusThread(start_status_thread());
mtklein748ca3b2015-01-15 10:56:12 -08001249
mtklein21eaf3b2016-02-08 12:39:59 -08001250 // Kick off as much parallel work as we can, making note of any serial work we'll need to do.
1251 SkTaskGroup parallel;
1252 SkTArray<Task> serial;
1253
1254 for (auto& sink : gSinks)
1255 for (auto& src : gSrcs) {
mtklein3eed7dd2016-02-24 19:07:07 -08001256 if (src->veto(sink->flags()) ||
1257 is_blacklisted(sink.tag.c_str(), src.tag.c_str(),
1258 src.options.c_str(), src->name().c_str())) {
mtklein15923c92016-02-29 10:14:38 -08001259 SkAutoTAcquire<SkSpinlock> lock(gMutex);
mtklein3eed7dd2016-02-24 19:07:07 -08001260 gPending--;
1261 continue;
1262 }
1263
mtklein21eaf3b2016-02-08 12:39:59 -08001264 Task task(src, sink);
1265 if (src->serial() || sink->serial()) {
1266 serial.push_back(task);
1267 } else {
1268 parallel.add([task] { Task::Run(task); });
mtklein748ca3b2015-01-15 10:56:12 -08001269 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001270 }
mtklein21eaf3b2016-02-08 12:39:59 -08001271 for (auto test : gParallelTests) {
1272 parallel.add([test] { run_test(test); });
mtklein55e88b22015-01-21 15:50:13 -08001273 }
mtklein21eaf3b2016-02-08 12:39:59 -08001274
1275 // With the parallel work running, run serial tasks and tests here on main thread.
1276 for (auto task : serial) { Task::Run(task); }
1277 for (auto test : gSerialTests) { run_test(test); }
1278
1279 // Wait for any remaining parallel work to complete (including any spun off of serial tasks).
1280 parallel.wait();
mtkleincd50bca2016-01-05 06:20:20 -08001281 gDefinitelyThreadSafeWork.wait();
1282
mtkleinda884c42016-02-24 18:00:23 -08001283 // We'd better have run everything.
1284 SkASSERT(gPending == 0);
mtkleine027f172016-02-26 15:53:06 -08001285 // Make sure we've flushed all our results to disk.
1286 JsonWriter::DumpJson();
mtkleinda884c42016-02-24 18:00:23 -08001287
mtklein748ca3b2015-01-15 10:56:12 -08001288 // At this point we're back in single-threaded land.
caryclarkf53ce802015-06-15 06:48:30 -07001289 sk_tool_utils::release_portable_typefaces();
mtklein@google.comd36522d2013-10-16 13:02:15 +00001290
mtklein748ca3b2015-01-15 10:56:12 -08001291 if (gFailures.count() > 0) {
1292 SkDebugf("Failures:\n");
1293 for (int i = 0; i < gFailures.count(); i++) {
mtklein9dc09102015-01-15 15:47:33 -08001294 SkDebugf("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001295 }
1296 SkDebugf("%d failures\n", gFailures.count());
1297 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001298 }
mtkleinafae30a2016-02-24 12:28:32 -08001299
1300#ifdef SK_PDF_IMAGE_STATS
halcanary7a14b312015-10-01 07:28:13 -07001301 SkPDFImageDumpStats();
mtkleinafae30a2016-02-24 12:28:32 -08001302#endif // SK_PDF_IMAGE_STATS
1303
1304 print_status();
scroggo4d9eaea2016-01-29 07:48:33 -08001305 SkDebugf("Finished!\n");
mtklein748ca3b2015-01-15 10:56:12 -08001306 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +00001307}
jcgregorio3b27ade2014-11-13 08:06:40 -08001308
kkinnunen179a8f52015-11-20 13:32:24 -08001309// TODO: currently many GPU tests are declared outside SK_SUPPORT_GPU guards.
1310// Thus we export the empty RunWithGPUTestContexts when SK_SUPPORT_GPU=0.
1311namespace skiatest {
1312namespace {
1313typedef void(*TestWithGrContext)(skiatest::Reporter*, GrContext*);
1314typedef void(*TestWithGrContextAndGLContext)(skiatest::Reporter*, GrContext*, SkGLContext*);
1315#if SK_SUPPORT_GPU
1316template<typename T>
kkinnunen34058002016-01-06 23:49:30 -08001317void call_test(T test, skiatest::Reporter* reporter, const GrContextFactory::ContextInfo& context);
kkinnunen179a8f52015-11-20 13:32:24 -08001318template<>
1319void call_test(TestWithGrContext test, skiatest::Reporter* reporter,
kkinnunen34058002016-01-06 23:49:30 -08001320 const GrContextFactory::ContextInfo& context) {
1321 test(reporter, context.fGrContext);
kkinnunen179a8f52015-11-20 13:32:24 -08001322}
1323template<>
1324void call_test(TestWithGrContextAndGLContext test, skiatest::Reporter* reporter,
kkinnunen34058002016-01-06 23:49:30 -08001325 const GrContextFactory::ContextInfo& context) {
1326 test(reporter, context.fGrContext, context.fGLContext);
kkinnunen179a8f52015-11-20 13:32:24 -08001327}
1328#endif
1329} // namespace
1330
kkinnunen179a8f52015-11-20 13:32:24 -08001331template<typename T>
1332void RunWithGPUTestContexts(T test, GPUTestContexts testContexts, Reporter* reporter,
1333 GrContextFactory* factory) {
1334#if SK_SUPPORT_GPU
kkinnunen3e980c32015-12-23 01:33:00 -08001335 // Iterate over context types, except use "native" instead of explicitly trying OpenGL and
1336 // OpenGL ES. Do not use GLES on desktop, since tests do not account for not fixing
1337 // http://skbug.com/2809
1338 GrContextFactory::GLContextType contextTypes[] = {
1339 GrContextFactory::kNative_GLContextType,
1340#if SK_ANGLE
1341#ifdef SK_BUILD_FOR_WIN
1342 GrContextFactory::kANGLE_GLContextType,
1343#endif
1344 GrContextFactory::kANGLE_GL_GLContextType,
1345#endif
1346#if SK_COMMAND_BUFFER
kkinnunen45c2c812016-02-25 02:03:43 -08001347 GrContextFactory::kCommandBufferES2_GLContextType,
1348 GrContextFactory::kCommandBufferES3_GLContextType,
kkinnunen3e980c32015-12-23 01:33:00 -08001349#endif
1350#if SK_MESA
1351 GrContextFactory::kMESA_GLContextType,
1352#endif
1353 GrContextFactory::kNull_GLContextType,
1354 GrContextFactory::kDebug_GLContextType,
1355 };
1356 static_assert(SK_ARRAY_COUNT(contextTypes) == GrContextFactory::kGLContextTypeCnt - 2,
1357 "Skipping unexpected GLContextType for GPU tests");
1358
1359 for (auto& contextType : contextTypes) {
kkinnunen179a8f52015-11-20 13:32:24 -08001360 int contextSelector = kNone_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001361 if (GrContextFactory::IsRenderingGLContext(contextType)) {
kkinnunen179a8f52015-11-20 13:32:24 -08001362 contextSelector |= kAllRendering_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001363 } else if (contextType == GrContextFactory::kNative_GLContextType) {
kkinnunen179a8f52015-11-20 13:32:24 -08001364 contextSelector |= kNative_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001365 } else if (contextType == GrContextFactory::kNull_GLContextType) {
kkinnunen179a8f52015-11-20 13:32:24 -08001366 contextSelector |= kNull_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001367 } else if (contextType == GrContextFactory::kDebug_GLContextType) {
kkinnunen55eeae92015-12-10 23:19:29 -08001368 contextSelector |= kDebug_GPUTestContexts;
kkinnunen179a8f52015-11-20 13:32:24 -08001369 }
1370 if ((testContexts & contextSelector) == 0) {
1371 continue;
1372 }
kkinnunen34058002016-01-06 23:49:30 -08001373 GrContextFactory::ContextInfo context = factory->getContextInfo(contextType);
1374 if (context.fGrContext) {
kkinnunen5219fd92015-12-10 06:28:13 -08001375 call_test(test, reporter, context);
1376 }
kkinnunen34058002016-01-06 23:49:30 -08001377 context = factory->getContextInfo(contextType,
1378 GrContextFactory::kEnableNVPR_GLContextOptions);
1379 if (context.fGrContext) {
kkinnunen179a8f52015-11-20 13:32:24 -08001380 call_test(test, reporter, context);
1381 }
1382 }
1383#endif
1384}
1385
1386template
1387void RunWithGPUTestContexts<TestWithGrContext>(TestWithGrContext test,
1388 GPUTestContexts testContexts,
1389 Reporter* reporter,
1390 GrContextFactory* factory);
1391template
1392void RunWithGPUTestContexts<TestWithGrContextAndGLContext>(TestWithGrContextAndGLContext test,
1393 GPUTestContexts testContexts,
1394 Reporter* reporter,
1395 GrContextFactory* factory);
1396} // namespace skiatest
1397
borenet48087572015-04-02 12:16:36 -07001398#if !defined(SK_BUILD_FOR_IOS)
jcgregorio3b27ade2014-11-13 08:06:40 -08001399int main(int argc, char** argv) {
1400 SkCommandLineFlags::Parse(argc, argv);
1401 return dm_main();
1402}
1403#endif