blob: 9173bb785826ffda36ed1e780bbc3c8689ed3e8f [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.
mtkleinafae30a2016-02-24 12:28:32 -080087SK_DECLARE_STATIC_SPINLOCK(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 {
mtkleinafae30a2016-02-24 12:28:32 -080095 SkAutoTAcquire<SkPODSpinlock> 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);
mtkleinafae30a2016-02-24 12:28:32 -0800113 SkAutoTAcquire<SkPODSpinlock> 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
124 SkAutoTAcquire<SkPODSpinlock> lock(gMutex);
125 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;
141 int code;
142 } 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
168#else
169 #include <signal.h>
mtkleinf8557292016-02-29 06:35:28 -0800170
mtklein246ba3a2016-02-23 10:39:36 -0800171 static void setup_crash_handler() {
172 const int kSignals[] = { SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGSEGV };
173 for (int sig : kSignals) {
174 signal(sig, [](int sig) {
mtkleinf8557292016-02-29 06:35:28 -0800175 if (!in_signal_handler.exchange(true)) {
176 SkDebugf("\nCaught signal %d [%s].\n", sig, strsignal(sig));
177 print_status();
178 }
179 // Reraise this signal to the default handler... hopefully, exit.
180 signal(sig, SIG_DFL);
181 raise(sig);
mtklein246ba3a2016-02-23 10:39:36 -0800182 });
183 }
184 }
185#endif
186
mtklein748ca3b2015-01-15 10:56:12 -0800187/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
mtklein114c3cd2015-01-15 10:15:02 -0800188
mtklein62bd1a62015-01-27 14:46:26 -0800189struct Gold : public SkString {
mtkleina82f5622015-02-20 12:30:19 -0800190 Gold() : SkString("") {}
mtklein3eed7dd2016-02-24 19:07:07 -0800191 Gold(const SkString& sink, const SkString& src,
192 const SkString& srcOptions, const SkString& name,
193 const SkString& md5)
mtklein62bd1a62015-01-27 14:46:26 -0800194 : SkString("") {
195 this->append(sink);
196 this->append(src);
djsollen54416de2015-04-03 07:24:48 -0700197 this->append(srcOptions);
mtklein62bd1a62015-01-27 14:46:26 -0800198 this->append(name);
199 this->append(md5);
mtklein62bd1a62015-01-27 14:46:26 -0800200 }
mtkleinc8d1dd42015-10-15 12:23:01 -0700201 struct Hash {
202 uint32_t operator()(const Gold& g) const {
203 return SkGoodHash()((const SkString&)g);
204 }
205 };
mtklein62bd1a62015-01-27 14:46:26 -0800206};
mtkleina82f5622015-02-20 12:30:19 -0800207static SkTHashSet<Gold, Gold::Hash> gGold;
mtklein62bd1a62015-01-27 14:46:26 -0800208
209static void add_gold(JsonWriter::BitmapResult r) {
djsollen54416de2015-04-03 07:24:48 -0700210 gGold.add(Gold(r.config, r.sourceType, r.sourceOptions, r.name, r.md5));
mtklein62bd1a62015-01-27 14:46:26 -0800211}
212
213static void gather_gold() {
214 if (!FLAGS_readPath.isEmpty()) {
215 SkString path(FLAGS_readPath[0]);
216 path.append("/dm.json");
217 if (!JsonWriter::ReadJson(path.c_str(), add_gold)) {
218 fail(SkStringPrintf("Couldn't read %s for golden results.", path.c_str()));
219 }
220 }
221}
222
223/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
224
borenet09ed4802015-04-03 14:15:33 -0700225static SkTHashSet<SkString> gUninterestingHashes;
226
227static void gather_uninteresting_hashes() {
228 if (!FLAGS_uninterestingHashesFile.isEmpty()) {
229 SkAutoTUnref<SkData> data(SkData::NewFromFileName(FLAGS_uninterestingHashesFile[0]));
mtkleincc334b32015-09-22 11:43:53 -0700230 if (!data) {
231 SkDebugf("WARNING: unable to read uninteresting hashes from %s\n",
232 FLAGS_uninterestingHashesFile[0]);
233 return;
234 }
borenet09ed4802015-04-03 14:15:33 -0700235 SkTArray<SkString> hashes;
236 SkStrSplit((const char*)data->data(), "\n", &hashes);
237 for (const SkString& hash : hashes) {
238 gUninterestingHashes.add(hash);
239 }
mtklein136baaa2016-02-03 11:21:45 -0800240 SkDebugf("FYI: loaded %d distinct uninteresting hashes from %d lines\n",
241 gUninterestingHashes.count(), hashes.count());
borenet09ed4802015-04-03 14:15:33 -0700242 }
243}
244
245/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
246
mtkleine0effd62015-07-29 06:37:28 -0700247struct TaggedSrc : public SkAutoTDelete<Src> {
mtklein3eed7dd2016-02-24 19:07:07 -0800248 SkString tag;
249 SkString options;
mtkleine0effd62015-07-29 06:37:28 -0700250};
251
252struct TaggedSink : public SkAutoTDelete<Sink> {
kkinnunen3e980c32015-12-23 01:33:00 -0800253 SkString tag;
djsollen54416de2015-04-03 07:24:48 -0700254};
mtklein748ca3b2015-01-15 10:56:12 -0800255
256static const bool kMemcpyOK = true;
257
mtkleine0effd62015-07-29 06:37:28 -0700258static SkTArray<TaggedSrc, kMemcpyOK> gSrcs;
259static SkTArray<TaggedSink, kMemcpyOK> gSinks;
mtklein748ca3b2015-01-15 10:56:12 -0800260
mtklein6393c062015-04-27 08:45:01 -0700261static bool in_shard() {
262 static int N = 0;
263 return N++ % FLAGS_shards == FLAGS_shard;
264}
265
mtklein3eed7dd2016-02-24 19:07:07 -0800266static void push_src(const char* tag, ImplicitString options, Src* s) {
mtklein748ca3b2015-01-15 10:56:12 -0800267 SkAutoTDelete<Src> src(s);
mtklein6393c062015-04-27 08:45:01 -0700268 if (in_shard() &&
mtklein3eed7dd2016-02-24 19:07:07 -0800269 FLAGS_src.contains(tag) &&
mtklein748ca3b2015-01-15 10:56:12 -0800270 !SkCommandLineFlags::ShouldSkip(FLAGS_match, src->name().c_str())) {
mtkleine0effd62015-07-29 06:37:28 -0700271 TaggedSrc& s = gSrcs.push_back();
mtklein748ca3b2015-01-15 10:56:12 -0800272 s.reset(src.detach());
273 s.tag = tag;
djsollen54416de2015-04-03 07:24:48 -0700274 s.options = options;
mtklein114c3cd2015-01-15 10:15:02 -0800275 }
mtklein748ca3b2015-01-15 10:56:12 -0800276}
mtklein114c3cd2015-01-15 10:15:02 -0800277
msarett9e707a02015-09-01 14:57:57 -0700278static void push_codec_src(Path path, CodecSrc::Mode mode, CodecSrc::DstColorType dstColorType,
scroggoc5560be2016-02-03 09:42:42 -0800279 SkAlphaType dstAlphaType, float scale) {
scroggoe4499842016-02-25 11:03:47 -0800280 if (FLAGS_simpleCodec) {
281 if (mode != CodecSrc::kCodec_Mode || dstColorType != CodecSrc::kGetFromCanvas_DstColorType
282 || scale != 1.0f)
283 // Only decode in the simple case.
284 return;
285 }
msarett9e707a02015-09-01 14:57:57 -0700286 SkString folder;
287 switch (mode) {
288 case CodecSrc::kCodec_Mode:
289 folder.append("codec");
290 break;
msarettbb25b532016-01-13 09:31:39 -0800291 case CodecSrc::kCodecZeroInit_Mode:
292 folder.append("codec_zero_init");
293 break;
msarett9e707a02015-09-01 14:57:57 -0700294 case CodecSrc::kScanline_Mode:
295 folder.append("scanline");
296 break;
msarett9e707a02015-09-01 14:57:57 -0700297 case CodecSrc::kStripe_Mode:
298 folder.append("stripe");
299 break;
msarett91c22b22016-02-22 12:27:46 -0800300 case CodecSrc::kCroppedScanline_Mode:
301 folder.append("crop");
302 break;
msarett9e707a02015-09-01 14:57:57 -0700303 case CodecSrc::kSubset_Mode:
msarett36c37962015-09-02 13:20:52 -0700304 folder.append("codec_subset");
msarett9e707a02015-09-01 14:57:57 -0700305 break;
msarettb714fb02016-01-22 14:46:42 -0800306 case CodecSrc::kGen_Mode:
307 folder.append("gen");
308 break;
msarett9e707a02015-09-01 14:57:57 -0700309 }
310
311 switch (dstColorType) {
312 case CodecSrc::kGrayscale_Always_DstColorType:
313 folder.append("_kGray8");
314 break;
315 case CodecSrc::kIndex8_Always_DstColorType:
316 folder.append("_kIndex8");
317 break;
318 default:
319 break;
320 }
321
scroggoc5560be2016-02-03 09:42:42 -0800322 switch (dstAlphaType) {
323 case kOpaque_SkAlphaType:
324 folder.append("_opaque");
325 break;
326 case kPremul_SkAlphaType:
327 folder.append("_premul");
328 break;
329 case kUnpremul_SkAlphaType:
330 folder.append("_unpremul");
331 break;
332 default:
333 break;
334 }
335
msarett9e707a02015-09-01 14:57:57 -0700336 if (1.0f != scale) {
337 folder.appendf("_%.3f", scale);
338 }
339
scroggoc5560be2016-02-03 09:42:42 -0800340 CodecSrc* src = new CodecSrc(path, mode, dstColorType, dstAlphaType, scale);
msarett9e707a02015-09-01 14:57:57 -0700341 push_src("image", folder, src);
342}
343
msarett3d9d7a72015-10-21 10:27:10 -0700344static void push_android_codec_src(Path path, AndroidCodecSrc::Mode mode,
scroggoc5560be2016-02-03 09:42:42 -0800345 CodecSrc::DstColorType dstColorType, SkAlphaType dstAlphaType, int sampleSize) {
msarett3d9d7a72015-10-21 10:27:10 -0700346 SkString folder;
347 switch (mode) {
348 case AndroidCodecSrc::kFullImage_Mode:
349 folder.append("scaled_codec");
350 break;
351 case AndroidCodecSrc::kDivisor_Mode:
352 folder.append("scaled_codec_divisor");
353 break;
354 }
355
356 switch (dstColorType) {
357 case CodecSrc::kGrayscale_Always_DstColorType:
358 folder.append("_kGray8");
359 break;
360 case CodecSrc::kIndex8_Always_DstColorType:
361 folder.append("_kIndex8");
362 break;
363 default:
364 break;
365 }
366
scroggoc5560be2016-02-03 09:42:42 -0800367 switch (dstAlphaType) {
368 case kOpaque_SkAlphaType:
369 folder.append("_opaque");
370 break;
371 case kPremul_SkAlphaType:
372 folder.append("_premul");
373 break;
msarett9e9444c2016-02-03 12:39:10 -0800374 case kUnpremul_SkAlphaType:
375 folder.append("_unpremul");
376 break;
scroggoc5560be2016-02-03 09:42:42 -0800377 default:
378 break;
379 }
380
msarett3d9d7a72015-10-21 10:27:10 -0700381 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800382 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700383 }
384
scroggoc5560be2016-02-03 09:42:42 -0800385 AndroidCodecSrc* src = new AndroidCodecSrc(path, mode, dstColorType, dstAlphaType, sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700386 push_src("image", folder, src);
387}
388
msarett438b2ad2015-04-09 12:43:10 -0700389static void push_codec_srcs(Path path) {
390 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
391 if (!encoded) {
392 SkDebugf("Couldn't read %s.", path.c_str());
393 return;
394 }
395 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
halcanary96fcdcc2015-08-27 07:41:13 -0700396 if (nullptr == codec.get()) {
msarett438b2ad2015-04-09 12:43:10 -0700397 SkDebugf("Couldn't create codec for %s.", path.c_str());
398 return;
399 }
400
msarett36c37962015-09-02 13:20:52 -0700401 // Native Scales
emmaleer8f4ba762015-08-14 07:44:46 -0700402 // 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 -0700403 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 -0700404
msarett91c22b22016-02-22 12:27:46 -0800405 SkTArray<CodecSrc::Mode> nativeModes;
406 nativeModes.push_back(CodecSrc::kCodec_Mode);
407 nativeModes.push_back(CodecSrc::kCodecZeroInit_Mode);
408 nativeModes.push_back(CodecSrc::kGen_Mode);
409 switch (codec->getEncodedFormat()) {
410 case SkEncodedFormat::kJPEG_SkEncodedFormat:
411 nativeModes.push_back(CodecSrc::kScanline_Mode);
412 nativeModes.push_back(CodecSrc::kStripe_Mode);
413 nativeModes.push_back(CodecSrc::kCroppedScanline_Mode);
414 break;
415 case SkEncodedFormat::kWEBP_SkEncodedFormat:
416 nativeModes.push_back(CodecSrc::kSubset_Mode);
417 break;
msarettb65e6042016-02-23 05:37:25 -0800418 case SkEncodedFormat::kRAW_SkEncodedFormat:
419 break;
msarett91c22b22016-02-22 12:27:46 -0800420 default:
421 nativeModes.push_back(CodecSrc::kScanline_Mode);
422 nativeModes.push_back(CodecSrc::kStripe_Mode);
423 break;
424 }
msarett9e707a02015-09-01 14:57:57 -0700425
msarett91c22b22016-02-22 12:27:46 -0800426 SkTArray<CodecSrc::DstColorType> colorTypes;
427 colorTypes.push_back(CodecSrc::kGetFromCanvas_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700428 switch (codec->getInfo().colorType()) {
429 case kGray_8_SkColorType:
msarett91c22b22016-02-22 12:27:46 -0800430 colorTypes.push_back(CodecSrc::kGrayscale_Always_DstColorType);
msarett55f7bdd2016-02-16 13:24:54 -0800431 if (kWBMP_SkEncodedFormat == codec->getEncodedFormat()) {
msarett91c22b22016-02-22 12:27:46 -0800432 colorTypes.push_back(CodecSrc::kIndex8_Always_DstColorType);
msarett55f7bdd2016-02-16 13:24:54 -0800433 }
msarett36c37962015-09-02 13:20:52 -0700434 break;
435 case kIndex_8_SkColorType:
msarett91c22b22016-02-22 12:27:46 -0800436 colorTypes.push_back(CodecSrc::kIndex8_Always_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700437 break;
438 default:
msarett36c37962015-09-02 13:20:52 -0700439 break;
440 }
msarett9e707a02015-09-01 14:57:57 -0700441
scroggoc5560be2016-02-03 09:42:42 -0800442 SkTArray<SkAlphaType> alphaModes;
443 alphaModes.push_back(kPremul_SkAlphaType);
msarett9e9444c2016-02-03 12:39:10 -0800444 alphaModes.push_back(kUnpremul_SkAlphaType);
scroggoc5560be2016-02-03 09:42:42 -0800445 if (codec->getInfo().alphaType() == kOpaque_SkAlphaType) {
446 alphaModes.push_back(kOpaque_SkAlphaType);
447 }
msarettb714fb02016-01-22 14:46:42 -0800448
449 for (CodecSrc::Mode mode : nativeModes) {
450 // SkCodecImageGenerator only runs for the default colorType
451 // recommended by SkCodec. There is no need to generate multiple
452 // tests for different colorTypes.
453 // TODO (msarett): Add scaling support to SkCodecImageGenerator.
454 if (CodecSrc::kGen_Mode == mode) {
455 // FIXME: The gpu backend does not draw kGray sources correctly. (skbug.com/4822)
456 if (kGray_8_SkColorType != codec->getInfo().colorType()) {
scroggoc5560be2016-02-03 09:42:42 -0800457 push_codec_src(path, mode, CodecSrc::kGetFromCanvas_DstColorType,
458 codec->getInfo().alphaType(), 1.0f);
msarettb714fb02016-01-22 14:46:42 -0800459 }
460 continue;
461 }
462
463 for (float scale : nativeScales) {
msarett91c22b22016-02-22 12:27:46 -0800464 for (CodecSrc::DstColorType colorType : colorTypes) {
scroggoc5560be2016-02-03 09:42:42 -0800465 for (SkAlphaType alphaType : alphaModes) {
msarett91c22b22016-02-22 12:27:46 -0800466 // Only test kCroppedScanline_Mode when the alpha type is opaque. The test is
467 // slow and won't be interestingly different with different alpha types.
468 if (CodecSrc::kCroppedScanline_Mode == mode &&
469 kOpaque_SkAlphaType != alphaType) {
470 continue;
471 }
472
473 push_codec_src(path, mode, colorType, alphaType, scale);
scroggoc5560be2016-02-03 09:42:42 -0800474 }
msarett9e707a02015-09-01 14:57:57 -0700475 }
476 }
msarett0a242972015-06-11 14:27:27 -0700477 }
msarett36c37962015-09-02 13:20:52 -0700478
scroggoe4499842016-02-25 11:03:47 -0800479 if (FLAGS_simpleCodec) {
480 return;
481 }
482
halcanary6950de62015-11-07 05:29:00 -0800483 // https://bug.skia.org/4428
msarett33c76232015-11-16 13:43:40 -0800484 bool subset = false;
485 // The following image types are supported by BitmapRegionDecoder,
486 // so we will test full image decodes and subset decodes.
msarettbe8216a2015-12-04 08:00:50 -0800487 static const char* const exts[] = {
msarett3d9d7a72015-10-21 10:27:10 -0700488 "jpg", "jpeg", "png", "webp",
489 "JPG", "JPEG", "PNG", "WEBP",
490 };
msarettbe8216a2015-12-04 08:00:50 -0800491 for (const char* ext : exts) {
msarett3d9d7a72015-10-21 10:27:10 -0700492 if (path.endsWith(ext)) {
msarett33c76232015-11-16 13:43:40 -0800493 subset = true;
msarett3d9d7a72015-10-21 10:27:10 -0700494 break;
495 }
496 }
msarett33c76232015-11-16 13:43:40 -0800497
msarett3d9d7a72015-10-21 10:27:10 -0700498 const int sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
msarett36c37962015-09-02 13:20:52 -0700499
msarett3d9d7a72015-10-21 10:27:10 -0700500 for (int sampleSize : sampleSizes) {
msarett91c22b22016-02-22 12:27:46 -0800501 for (CodecSrc::DstColorType colorType : colorTypes) {
scroggoc5560be2016-02-03 09:42:42 -0800502 for (SkAlphaType alphaType : alphaModes) {
msarett91c22b22016-02-22 12:27:46 -0800503 push_android_codec_src(path, AndroidCodecSrc::kFullImage_Mode, colorType,
scroggoc5560be2016-02-03 09:42:42 -0800504 alphaType, sampleSize);
505 if (subset) {
msarett91c22b22016-02-22 12:27:46 -0800506 push_android_codec_src(path, AndroidCodecSrc::kDivisor_Mode, colorType,
scroggoc5560be2016-02-03 09:42:42 -0800507 alphaType, sampleSize);
508 }
msarett3d9d7a72015-10-21 10:27:10 -0700509 }
msarett36c37962015-09-02 13:20:52 -0700510 }
511 }
msarett438b2ad2015-04-09 12:43:10 -0700512}
513
msarett5cb48852015-11-06 08:56:32 -0800514static bool brd_color_type_supported(SkBitmapRegionDecoder::Strategy strategy,
msaretta5783ae2015-09-08 15:35:32 -0700515 CodecSrc::DstColorType dstColorType) {
516 switch (strategy) {
msarett5cb48852015-11-06 08:56:32 -0800517 case SkBitmapRegionDecoder::kCanvas_Strategy:
msaretta5783ae2015-09-08 15:35:32 -0700518 if (CodecSrc::kGetFromCanvas_DstColorType == dstColorType) {
519 return true;
520 }
521 return false;
msarett5cb48852015-11-06 08:56:32 -0800522 case SkBitmapRegionDecoder::kAndroidCodec_Strategy:
msarett26ad17b2015-10-22 07:29:19 -0700523 switch (dstColorType) {
524 case CodecSrc::kGetFromCanvas_DstColorType:
525 case CodecSrc::kIndex8_Always_DstColorType:
526 case CodecSrc::kGrayscale_Always_DstColorType:
527 return true;
528 default:
529 return false;
530 }
msaretta5783ae2015-09-08 15:35:32 -0700531 default:
532 SkASSERT(false);
533 return false;
534 }
535}
536
msarett5cb48852015-11-06 08:56:32 -0800537static void push_brd_src(Path path, SkBitmapRegionDecoder::Strategy strategy,
msaretta5783ae2015-09-08 15:35:32 -0700538 CodecSrc::DstColorType dstColorType, BRDSrc::Mode mode, uint32_t sampleSize) {
539 SkString folder;
540 switch (strategy) {
msarett5cb48852015-11-06 08:56:32 -0800541 case SkBitmapRegionDecoder::kCanvas_Strategy:
msaretta5783ae2015-09-08 15:35:32 -0700542 folder.append("brd_canvas");
543 break;
msarett5cb48852015-11-06 08:56:32 -0800544 case SkBitmapRegionDecoder::kAndroidCodec_Strategy:
msarett26ad17b2015-10-22 07:29:19 -0700545 folder.append("brd_android_codec");
546 break;
msaretta5783ae2015-09-08 15:35:32 -0700547 default:
548 SkASSERT(false);
549 return;
550 }
551
552 switch (mode) {
553 case BRDSrc::kFullImage_Mode:
554 break;
555 case BRDSrc::kDivisor_Mode:
556 folder.append("_divisor");
557 break;
558 default:
559 SkASSERT(false);
560 return;
561 }
562
563 switch (dstColorType) {
564 case CodecSrc::kGetFromCanvas_DstColorType:
565 break;
566 case CodecSrc::kIndex8_Always_DstColorType:
567 folder.append("_kIndex");
568 break;
569 case CodecSrc::kGrayscale_Always_DstColorType:
570 folder.append("_kGray");
571 break;
572 default:
573 SkASSERT(false);
574 return;
575 }
576
577 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800578 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700579 }
580
581 BRDSrc* src = new BRDSrc(path, strategy, mode, dstColorType, sampleSize);
582 push_src("image", folder, src);
583}
584
585static void push_brd_srcs(Path path) {
586
msarett5cb48852015-11-06 08:56:32 -0800587 const SkBitmapRegionDecoder::Strategy strategies[] = {
588 SkBitmapRegionDecoder::kCanvas_Strategy,
msarett5cb48852015-11-06 08:56:32 -0800589 SkBitmapRegionDecoder::kAndroidCodec_Strategy,
msaretta5783ae2015-09-08 15:35:32 -0700590 };
591
scroggo501b7342015-11-03 07:55:11 -0800592 // Test on a variety of sampleSizes, making sure to include:
593 // - 2, 4, and 8, which are natively supported by jpeg
594 // - multiples of 2 which are not divisible by 4 (analogous for 4)
595 // - larger powers of two, since BRD clients generally use powers of 2
596 // We will only produce output for the larger sizes on large images.
597 const uint32_t sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 24, 32, 64 };
msarett6efbe052015-09-11 09:01:16 -0700598
msaretta5783ae2015-09-08 15:35:32 -0700599 // We will only test to one backend (8888), but we will test all of the
600 // color types that we need to decode to on this backend.
601 const CodecSrc::DstColorType dstColorTypes[] = {
msarett26ad17b2015-10-22 07:29:19 -0700602 CodecSrc::kGetFromCanvas_DstColorType,
603 CodecSrc::kIndex8_Always_DstColorType,
604 CodecSrc::kGrayscale_Always_DstColorType,
msaretta5783ae2015-09-08 15:35:32 -0700605 };
606
607 const BRDSrc::Mode modes[] = {
msarett26ad17b2015-10-22 07:29:19 -0700608 BRDSrc::kFullImage_Mode,
609 BRDSrc::kDivisor_Mode,
msaretta5783ae2015-09-08 15:35:32 -0700610 };
611
msarett5cb48852015-11-06 08:56:32 -0800612 for (SkBitmapRegionDecoder::Strategy strategy : strategies) {
msarett6efbe052015-09-11 09:01:16 -0700613 for (uint32_t sampleSize : sampleSizes) {
msarett6efbe052015-09-11 09:01:16 -0700614 for (CodecSrc::DstColorType dstColorType : dstColorTypes) {
615 if (brd_color_type_supported(strategy, dstColorType)) {
616 for (BRDSrc::Mode mode : modes) {
msaretta5783ae2015-09-08 15:35:32 -0700617 push_brd_src(path, strategy, dstColorType, mode, sampleSize);
618 }
619 }
620 }
621 }
622 }
623}
624
625static bool brd_supported(const char* ext) {
msarett8c8f22a2015-04-01 06:58:48 -0700626 static const char* const exts[] = {
msaretta5783ae2015-09-08 15:35:32 -0700627 "jpg", "jpeg", "png", "webp",
628 "JPG", "JPEG", "PNG", "WEBP",
msarett8c8f22a2015-04-01 06:58:48 -0700629 };
630
631 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
632 if (0 == strcmp(exts[i], ext)) {
633 return true;
634 }
635 }
636 return false;
scroggo9b77ddd2015-03-19 06:03:39 -0700637}
638
scroggo86737142016-02-03 12:19:11 -0800639static bool gather_srcs() {
mtklein748ca3b2015-01-15 10:56:12 -0800640 for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) {
djsollen54416de2015-04-03 07:24:48 -0700641 push_src("gm", "", new GMSrc(r->factory()));
mtklein748ca3b2015-01-15 10:56:12 -0800642 }
halcanaryfc37ad12015-01-30 07:31:19 -0800643 for (int i = 0; i < FLAGS_skps.count(); i++) {
644 const char* path = FLAGS_skps[i];
645 if (sk_isdir(path)) {
646 SkOSFile::Iter it(path, "skp");
647 for (SkString file; it.next(&file); ) {
djsollen54416de2015-04-03 07:24:48 -0700648 push_src("skp", "", new SKPSrc(SkOSPath::Join(path, file.c_str())));
halcanaryfc37ad12015-01-30 07:31:19 -0800649 }
650 } else {
djsollen54416de2015-04-03 07:24:48 -0700651 push_src("skp", "", new SKPSrc(path));
mtklein114c3cd2015-01-15 10:15:02 -0800652 }
653 }
scroggo86737142016-02-03 12:19:11 -0800654
655 SkTArray<SkString> images;
656 if (!CollectImages(&images)) {
657 return false;
658 }
659
660 for (auto image : images) {
661 push_codec_srcs(image);
scroggoe4499842016-02-25 11:03:47 -0800662 if (FLAGS_simpleCodec) {
663 continue;
664 }
665
mtklein21eaf3b2016-02-08 12:39:59 -0800666 const char* ext = strrchr(image.c_str(), '.');
667 if (ext && brd_supported(ext+1)) {
scroggo86737142016-02-03 12:19:11 -0800668 push_brd_srcs(image);
mtklein709d2c32015-01-15 08:30:25 -0800669 }
mtklein709d2c32015-01-15 08:30:25 -0800670 }
scroggo86737142016-02-03 12:19:11 -0800671
672 return true;
mtklein709d2c32015-01-15 08:30:25 -0800673}
674
kkinnunen3e980c32015-12-23 01:33:00 -0800675static void push_sink(const SkCommandLineConfig& config, Sink* s) {
rmistry0f515bd2015-12-22 10:22:26 -0800676 SkAutoTDelete<Sink> sink(s);
kkinnunen3e980c32015-12-23 01:33:00 -0800677
mtkleine0effd62015-07-29 06:37:28 -0700678 // Try a simple Src as a canary. If it fails, skip this sink.
mtklein748ca3b2015-01-15 10:56:12 -0800679 struct : public Src {
mtkleine0effd62015-07-29 06:37:28 -0700680 Error draw(SkCanvas* c) const override {
681 c->drawRect(SkRect::MakeWH(1,1), SkPaint());
682 return "";
683 }
mtklein36352bf2015-03-25 18:17:31 -0700684 SkISize size() const override { return SkISize::Make(16, 16); }
mtkleine0effd62015-07-29 06:37:28 -0700685 Name name() const override { return "justOneRect"; }
686 } justOneRect;
mtklein114c3cd2015-01-15 10:15:02 -0800687
mtklein748ca3b2015-01-15 10:56:12 -0800688 SkBitmap bitmap;
689 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800690 SkString log;
mtkleine0effd62015-07-29 06:37:28 -0700691 Error err = sink->draw(justOneRect, &bitmap, &stream, &log);
mtklein4089ef72015-03-05 08:40:28 -0800692 if (err.isFatal()) {
kkinnunen3e980c32015-12-23 01:33:00 -0800693 SkDebugf("Could not run %s: %s\n", config.getTag().c_str(), err.c_str());
mtklein05641a52015-04-21 10:49:13 -0700694 exit(1);
mtklein114c3cd2015-01-15 10:15:02 -0800695 }
696
mtkleine0effd62015-07-29 06:37:28 -0700697 TaggedSink& ts = gSinks.push_back();
mtklein748ca3b2015-01-15 10:56:12 -0800698 ts.reset(sink.detach());
kkinnunen3e980c32015-12-23 01:33:00 -0800699 ts.tag = config.getTag();
mtklein748ca3b2015-01-15 10:56:12 -0800700}
701
702static bool gpu_supported() {
703#if SK_SUPPORT_GPU
704 return FLAGS_gpu;
705#else
706 return false;
707#endif
708}
kkinnunen9ebc3f02015-12-21 23:48:13 -0800709
kkinnunen3e980c32015-12-23 01:33:00 -0800710static Sink* create_sink(const SkCommandLineConfig* config) {
711#if SK_SUPPORT_GPU
712 if (gpu_supported()) {
713 if (const SkCommandLineConfigGpu* gpuConfig = config->asConfigGpu()) {
714 GrContextFactory::GLContextType contextType = gpuConfig->getContextType();
715 GrContextFactory::GLContextOptions contextOptions =
716 GrContextFactory::kNone_GLContextOptions;
717 if (gpuConfig->getUseNVPR()) {
718 contextOptions = static_cast<GrContextFactory::GLContextOptions>(
719 contextOptions | GrContextFactory::kEnableNVPR_GLContextOptions);
720 }
721 GrContextFactory testFactory;
722 if (!testFactory.get(contextType, contextOptions)) {
723 SkDebugf("WARNING: can not create GPU context for config '%s'. "
724 "GM tests will be skipped.\n", gpuConfig->getTag().c_str());
725 return nullptr;
726 }
727 return new GPUSink(contextType, contextOptions, gpuConfig->getSamples(),
728 gpuConfig->getUseDIText(), FLAGS_gpu_threading);
729 }
730 }
731#endif
732
733#define SINK(t, sink, ...) if (config->getBackend().equals(t)) { return new sink(__VA_ARGS__); }
734
tomhudsoneebc39a2015-02-23 12:18:05 -0800735#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
736 SINK("hwui", HWUISink);
737#endif
738
mtklein748ca3b2015-01-15 10:56:12 -0800739 if (FLAGS_cpu) {
740 SINK("565", RasterSink, kRGB_565_SkColorType);
741 SINK("8888", RasterSink, kN32_SkColorType);
mtklein27c3fdd2016-02-26 14:43:21 -0800742 SINK("srgb", RasterSink, kN32_SkColorType, kSRGB_SkColorProfileType);
743 SINK("f16", RasterSink, kRGBA_F16_SkColorType);
halcanaryc11c62f2015-09-28 11:51:54 -0700744 SINK("pdf", PDFSink, "Pdfium");
745 SINK("pdf_poppler", PDFSink, "Poppler");
mtklein9c3f17d2015-01-28 11:35:18 -0800746 SINK("skp", SKPSink);
mtklein8a4527e2015-01-31 20:00:58 -0800747 SINK("svg", SVGSink);
748 SINK("null", NullSink);
halcanary47ef4d52015-03-03 09:13:09 -0800749 SINK("xps", XPSSink);
mtklein748ca3b2015-01-15 10:56:12 -0800750 }
751#undef SINK
halcanary96fcdcc2015-08-27 07:41:13 -0700752 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800753}
754
kkinnunen3e980c32015-12-23 01:33:00 -0800755static Sink* create_via(const SkString& tag, Sink* wrapped) {
756#define VIA(t, via, ...) if (tag.equals(t)) { return new via(__VA_ARGS__); }
halcanary96fcdcc2015-08-27 07:41:13 -0700757 VIA("twice", ViaTwice, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700758 VIA("serialize", ViaSerialization, wrapped);
mtklein4a34ecb2016-01-08 10:19:35 -0800759 VIA("pic", ViaPicture, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700760 VIA("2ndpic", ViaSecondPicture, wrapped);
mtkleind31c13d2015-05-05 12:59:56 -0700761 VIA("sp", ViaSingletonPictures, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700762 VIA("tiles", ViaTiles, 256, 256, nullptr, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800763 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
mtklein2e2ea382015-10-16 10:29:41 -0700764 VIA("remote", ViaRemote, false, wrapped);
765 VIA("remote_cache", ViaRemote, true, wrapped);
halcanary7a76f9c2016-02-03 11:53:18 -0800766 VIA("mojo", ViaMojo, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800767
mtkleind603b222015-02-17 11:13:33 -0800768 if (FLAGS_matrix.count() == 4) {
mtklein748ca3b2015-01-15 10:56:12 -0800769 SkMatrix m;
mtkleind603b222015-02-17 11:13:33 -0800770 m.reset();
771 m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
772 m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
773 m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
774 m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
775 VIA("matrix", ViaMatrix, m, wrapped);
776 VIA("upright", ViaUpright, m, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800777 }
tomhudson64de1e12015-03-05 08:01:07 -0800778
779#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
780 VIA("androidsdk", ViaAndroidSDK, wrapped);
781#endif
782
mtklein748ca3b2015-01-15 10:56:12 -0800783#undef VIA
halcanary96fcdcc2015-08-27 07:41:13 -0700784 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800785}
786
mtklein748ca3b2015-01-15 10:56:12 -0800787static void gather_sinks() {
kkinnunen3e980c32015-12-23 01:33:00 -0800788 SkCommandLineConfigArray configs;
789 ParseConfigs(FLAGS_config, &configs);
790 for (int i = 0; i < configs.count(); i++) {
791 const SkCommandLineConfig& config = *configs[i];
792 Sink* sink = create_sink(&config);
793 if (sink == nullptr) {
794 SkDebugf("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
795 config.getTag().c_str());
796 continue;
797 }
mtklein748ca3b2015-01-15 10:56:12 -0800798
kkinnunen3e980c32015-12-23 01:33:00 -0800799 const SkTArray<SkString>& parts = config.getViaParts();
800 for (int j = parts.count(); j-- > 0;) {
801 const SkString& part = parts[j];
802 Sink* next = create_via(part, sink);
halcanary96fcdcc2015-08-27 07:41:13 -0700803 if (next == nullptr) {
kkinnunen3e980c32015-12-23 01:33:00 -0800804 SkDebugf("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
805 part.c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800806 delete sink;
halcanary96fcdcc2015-08-27 07:41:13 -0700807 sink = nullptr;
mtklein748ca3b2015-01-15 10:56:12 -0800808 break;
809 }
810 sink = next;
811 }
812 if (sink) {
813 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800814 }
815 }
816}
mtklein709d2c32015-01-15 08:30:25 -0800817
mtkleina5114d72015-08-24 13:27:01 -0700818static bool dump_png(SkBitmap bitmap, const char* path, const char* md5) {
819 const int w = bitmap.width(),
820 h = bitmap.height();
mtklein27c3fdd2016-02-26 14:43:21 -0800821 // PNG wants unpremultiplied 8-bit RGBA pixels (16-bit could work fine too).
822 // We leave the gamma of these bytes unspecified, to continue the status quo,
823 // which we think generally is to interpret them as sRGB.
mtkleina5114d72015-08-24 13:27:01 -0700824
mtklein27c3fdd2016-02-26 14:43:21 -0800825 SkAutoTMalloc<uint32_t> rgba(w*h);
826
827 if (bitmap. colorType() == kN32_SkColorType &&
828 bitmap.profileType() == kSRGB_SkColorProfileType) {
829 // These are premul sRGB 8-bit pixels in SkPMColor order.
830 // We want unpremul sRGB 8-bit pixels in RGBA order. We'll get there via floats.
831 bitmap.lockPixels();
832 auto px = (const uint32_t*)bitmap.getPixels();
833 if (!px) {
mtkleina5114d72015-08-24 13:27:01 -0700834 return false;
835 }
mtklein27c3fdd2016-02-26 14:43:21 -0800836 for (int i = 0; i < w*h; i++) {
837 Sk4f fs = Sk4f_fromS32(px[i]); // Convert up to linear floats.
838 #if defined(SK_PMCOLOR_IS_BGRA)
839 fs = SkNx_shuffle<2,1,0,3>(fs); // Shuffle to RGBA, if not there already.
840 #endif
841 float invA = 1.0f / fs[3];
842 fs = fs * Sk4f(invA, invA, invA, 1); // Unpremultiply.
843 rgba[i] = Sk4f_toS32(fs); // Pack down to sRGB bytes.
844 }
mtkleina5114d72015-08-24 13:27:01 -0700845
mtklein27c3fdd2016-02-26 14:43:21 -0800846 } else if (bitmap.colorType() == kRGBA_F16_SkColorType) {
847 // These are premul linear half-float pixels in RGBA order.
848 // We want unpremul sRGB 8-bit pixels in RGBA order. We'll get there via floats.
849 bitmap.lockPixels();
850 auto px = (const uint64_t*)bitmap.getPixels();
851 if (!px) {
852 return false;
853 }
854 for (int i = 0; i < w*h; i++) {
855 Sk4f fs = SkHalfToFloat_01(px[i]); // Convert up to linear floats.
856 float invA = 1.0f / fs[3];
857 fs = fs * Sk4f(invA, invA, invA, 1); // Unpremultiply.
858 rgba[i] = Sk4f_toS32(fs); // Pack down to sRGB bytes.
859 }
860
861
862 } else {
863 // We "should" gamma correct in here but we don't.
864 // We want Gold to show exactly what our clients are seeing, broken gamma.
865
866 // Convert smaller formats up to premul linear 8-bit (in SkPMColor order).
867 if (bitmap.colorType() != kN32_SkColorType) {
868 SkBitmap n32;
869 if (!bitmap.copyTo(&n32, kN32_SkColorType)) {
870 return false;
871 }
872 bitmap = n32;
873 }
874
875 // Convert premul linear 8-bit to unpremul linear 8-bit RGBA.
876 if (!bitmap.readPixels(SkImageInfo::Make(w,h, kRGBA_8888_SkColorType,
877 kUnpremul_SkAlphaType),
878 rgba, 4*w, 0,0)) {
879 return false;
880 }
mtkleina5114d72015-08-24 13:27:01 -0700881 }
882
883 // We don't need bitmap anymore. Might as well drop our ref.
mtkleinfccb77d2015-08-24 14:13:29 -0700884 bitmap.reset();
mtkleina5114d72015-08-24 13:27:01 -0700885
mtkleinc64137c2015-08-25 10:56:08 -0700886 FILE* f = fopen(path, "wb");
mtkleina5114d72015-08-24 13:27:01 -0700887 if (!f) { return false; }
888
889 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
890 if (!png) {
891 fclose(f);
892 return false;
893 }
894
895 png_infop info = png_create_info_struct(png);
896 if (!info) {
897 png_destroy_write_struct(&png, &info);
898 fclose(f);
899 return false;
900 }
901
mtkleind69ece62015-09-10 10:37:44 -0700902 SkString description;
903 description.append("Key: ");
904 for (int i = 0; i < FLAGS_key.count(); i++) {
905 description.appendf("%s ", FLAGS_key[i]);
906 }
907 description.append("Properties: ");
908 for (int i = 0; i < FLAGS_properties.count(); i++) {
909 description.appendf("%s ", FLAGS_properties[i]);
910 }
911 description.appendf("MD5: %s", md5);
912
mtkleina5114d72015-08-24 13:27:01 -0700913 png_text text[2];
914 text[0].key = (png_charp)"Author";
915 text[0].text = (png_charp)"DM dump_png()";
916 text[0].compression = PNG_TEXT_COMPRESSION_NONE;
917 text[1].key = (png_charp)"Description";
mtkleind69ece62015-09-10 10:37:44 -0700918 text[1].text = (png_charp)description.c_str();
mtkleina5114d72015-08-24 13:27:01 -0700919 text[1].compression = PNG_TEXT_COMPRESSION_NONE;
920 png_set_text(png, info, text, 2);
921
922 png_init_io(png, f);
923 png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8,
924 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
925 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
926 png_write_info(png, info);
927 for (int j = 0; j < h; j++) {
928 png_bytep row = (png_bytep)(rgba.get() + w*j);
929 png_write_rows(png, &row, 1);
930 }
931 png_write_end(png, info);
932
933 png_destroy_write_struct(&png, &info);
934 fclose(f);
935 return true;
936}
937
mtkleina2ef6422015-01-15 13:44:22 -0800938static bool match(const char* needle, const char* haystack) {
halcanary96fcdcc2015-08-27 07:41:13 -0700939 return 0 == strcmp("_", needle) || nullptr != strstr(haystack, needle);
mtkleina2ef6422015-01-15 13:44:22 -0800940}
941
mtklein3eed7dd2016-02-24 19:07:07 -0800942static bool is_blacklisted(const char* sink, const char* src,
943 const char* srcOptions, const char* name) {
mtklein0d243ff2015-04-03 07:51:00 -0700944 for (int i = 0; i < FLAGS_blacklist.count() - 3; i += 4) {
mtkleina2ef6422015-01-15 13:44:22 -0800945 if (match(FLAGS_blacklist[i+0], sink) &&
djsollen54416de2015-04-03 07:24:48 -0700946 match(FLAGS_blacklist[i+1], src) &&
947 match(FLAGS_blacklist[i+2], srcOptions) &&
948 match(FLAGS_blacklist[i+3], name)) {
mtklein3eed7dd2016-02-24 19:07:07 -0800949 return true;
mtkleina2ef6422015-01-15 13:44:22 -0800950 }
951 }
mtklein3eed7dd2016-02-24 19:07:07 -0800952 return false;
mtkleina2ef6422015-01-15 13:44:22 -0800953}
954
mtkleincd50bca2016-01-05 06:20:20 -0800955// Even when a Task Sink reports to be non-threadsafe (e.g. GPU), we know things like
956// .png encoding are definitely thread safe. This lets us offload that work to CPU threads.
957static SkTaskGroup gDefinitelyThreadSafeWork;
958
mtklein748ca3b2015-01-15 10:56:12 -0800959// The finest-grained unit of work we can run: draw a single Src into a single Sink,
960// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
961struct Task {
mtkleine0effd62015-07-29 06:37:28 -0700962 Task(const TaggedSrc& src, const TaggedSink& sink) : src(src), sink(sink) {}
963 const TaggedSrc& src;
964 const TaggedSink& sink;
mtklein748ca3b2015-01-15 10:56:12 -0800965
mtklein21eaf3b2016-02-08 12:39:59 -0800966 static void Run(const Task& task) {
967 SkString name = task.src->name();
mtkleine0effd62015-07-29 06:37:28 -0700968
mtkleinb9eb4ac2015-02-02 18:26:03 -0800969 SkString log;
mtklein3eed7dd2016-02-24 19:07:07 -0800970 if (!FLAGS_dryRun) {
mtklein748ca3b2015-01-15 10:56:12 -0800971 SkBitmap bitmap;
972 SkDynamicMemoryWStream stream;
mtklein3eed7dd2016-02-24 19:07:07 -0800973 start(task.sink.tag.c_str(), task.src.tag.c_str(),
974 task.src.options.c_str(), name.c_str());
mtklein21eaf3b2016-02-08 12:39:59 -0800975 Error err = task.sink->draw(*task.src, &bitmap, &stream, &log);
mtklein748ca3b2015-01-15 10:56:12 -0800976 if (!err.isEmpty()) {
mtklein4089ef72015-03-05 08:40:28 -0800977 if (err.isFatal()) {
djsollen54416de2015-04-03 07:24:48 -0700978 fail(SkStringPrintf("%s %s %s %s: %s",
mtklein21eaf3b2016-02-08 12:39:59 -0800979 task.sink.tag.c_str(),
980 task.src.tag.c_str(),
981 task.src.options.c_str(),
mtklein4089ef72015-03-05 08:40:28 -0800982 name.c_str(),
983 err.c_str()));
mtkleinb37cb412015-03-18 05:27:14 -0700984 } else {
mtklein3eed7dd2016-02-24 19:07:07 -0800985 done(task.sink.tag.c_str(), task.src.tag.c_str(),
986 task.src.options.c_str(), name.c_str());
mtkleinba6ada72016-01-21 09:39:35 -0800987 return;
mtklein4089ef72015-03-05 08:40:28 -0800988 }
mtklein748ca3b2015-01-15 10:56:12 -0800989 }
mtklein62bd1a62015-01-27 14:46:26 -0800990
mtkleincd50bca2016-01-05 06:20:20 -0800991 // We're likely switching threads here, so we must capture by value, [=] or [foo,bar].
992 SkStreamAsset* data = stream.detachAsStream();
993 gDefinitelyThreadSafeWork.add([task,name,bitmap,data]{
994 SkAutoTDelete<SkStreamAsset> ownedData(data);
995
996 // Why doesn't the copy constructor do this when we have pre-locked pixels?
997 bitmap.lockPixels();
998
999 SkString md5;
1000 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
1001 SkMD5 hash;
1002 if (data->getLength()) {
1003 hash.writeStream(data, data->getLength());
1004 data->rewind();
mtklein38408462015-07-08 07:25:27 -07001005 } else {
mtkleincd50bca2016-01-05 06:20:20 -08001006 // If we're BGRA (Linux, Windows), swizzle over to RGBA (Mac, Android).
1007 // This helps eliminate multiple 0-pixel-diff hashes on gold.skia.org.
1008 // (Android's general slow speed breaks the tie arbitrarily in RGBA's favor.)
1009 // We might consider promoting 565 to RGBA too.
1010 if (bitmap.colorType() == kBGRA_8888_SkColorType) {
1011 SkBitmap swizzle;
1012 SkAssertResult(bitmap.copyTo(&swizzle, kRGBA_8888_SkColorType));
1013 hash.write(swizzle.getPixels(), swizzle.getSize());
1014 } else {
1015 hash.write(bitmap.getPixels(), bitmap.getSize());
1016 }
1017 }
1018 SkMD5::Digest digest;
1019 hash.finish(digest);
1020 for (int i = 0; i < 16; i++) {
1021 md5.appendf("%02x", digest.data[i]);
mtklein38408462015-07-08 07:25:27 -07001022 }
mtklein62bd1a62015-01-27 14:46:26 -08001023 }
mtklein62bd1a62015-01-27 14:46:26 -08001024
mtkleincd50bca2016-01-05 06:20:20 -08001025 if (!FLAGS_readPath.isEmpty() &&
mtklein3eed7dd2016-02-24 19:07:07 -08001026 !gGold.contains(Gold(task.sink.tag, task.src.tag,
1027 task.src.options, name, md5))) {
mtkleincd50bca2016-01-05 06:20:20 -08001028 fail(SkStringPrintf("%s not found for %s %s %s %s in %s",
1029 md5.c_str(),
mtklein21eaf3b2016-02-08 12:39:59 -08001030 task.sink.tag.c_str(),
1031 task.src.tag.c_str(),
1032 task.src.options.c_str(),
mtkleincd50bca2016-01-05 06:20:20 -08001033 name.c_str(),
1034 FLAGS_readPath[0]));
mtklein748ca3b2015-01-15 10:56:12 -08001035 }
mtkleincd50bca2016-01-05 06:20:20 -08001036
1037 if (!FLAGS_writePath.isEmpty()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001038 const char* ext = task.sink->fileExtension();
mtkleincd50bca2016-01-05 06:20:20 -08001039 if (data->getLength()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001040 WriteToDisk(task, md5, ext, data, data->getLength(), nullptr);
mtkleincd50bca2016-01-05 06:20:20 -08001041 SkASSERT(bitmap.drawsNothing());
1042 } else if (!bitmap.drawsNothing()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001043 WriteToDisk(task, md5, ext, nullptr, 0, &bitmap);
mtkleincd50bca2016-01-05 06:20:20 -08001044 }
1045 }
1046 });
mtklein748ca3b2015-01-15 10:56:12 -08001047 }
mtklein3eed7dd2016-02-24 19:07:07 -08001048 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 -08001049 }
1050
1051 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -08001052 SkString md5,
1053 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -08001054 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -08001055 const SkBitmap* bitmap) {
mtklein409d4702016-02-29 07:38:01 -08001056 bool gammaCorrect = false;
1057 if (bitmap) {
1058 gammaCorrect = bitmap->profileType() == kSRGB_SkColorProfileType
1059 || bitmap-> colorType() == kRGBA_F16_SkColorType;
1060 }
1061
mtklein748ca3b2015-01-15 10:56:12 -08001062 JsonWriter::BitmapResult result;
djsollen54416de2015-04-03 07:24:48 -07001063 result.name = task.src->name();
mtklein3eed7dd2016-02-24 19:07:07 -08001064 result.config = task.sink.tag;
djsollen54416de2015-04-03 07:24:48 -07001065 result.sourceType = task.src.tag;
1066 result.sourceOptions = task.src.options;
1067 result.ext = ext;
mtklein409d4702016-02-29 07:38:01 -08001068 result.gammaCorrect = gammaCorrect;
djsollen54416de2015-04-03 07:24:48 -07001069 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -08001070 JsonWriter::AddBitmapResult(result);
1071
mtkleinb0531a72015-04-07 13:38:48 -07001072 // If an MD5 is uninteresting, we want it noted in the JSON file,
1073 // but don't want to dump it out as a .png (or whatever ext is).
1074 if (gUninterestingHashes.contains(md5)) {
1075 return;
1076 }
1077
mtklein748ca3b2015-01-15 10:56:12 -08001078 const char* dir = FLAGS_writePath[0];
1079 if (0 == strcmp(dir, "@")) { // Needed for iOS.
1080 dir = FLAGS_resourcePath[0];
1081 }
1082 sk_mkdir(dir);
1083
1084 SkString path;
1085 if (FLAGS_nameByHash) {
1086 path = SkOSPath::Join(dir, result.md5.c_str());
1087 path.append(".");
1088 path.append(ext);
1089 if (sk_exists(path.c_str())) {
1090 return; // Content-addressed. If it exists already, we're done.
1091 }
1092 } else {
kkinnunen3e980c32015-12-23 01:33:00 -08001093 path = SkOSPath::Join(dir, task.sink.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001094 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001095 path = SkOSPath::Join(path.c_str(), task.src.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001096 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001097 if (strcmp(task.src.options.c_str(), "") != 0) {
1098 path = SkOSPath::Join(path.c_str(), task.src.options.c_str());
djsollen54416de2015-04-03 07:24:48 -07001099 sk_mkdir(path.c_str());
1100 }
mtklein748ca3b2015-01-15 10:56:12 -08001101 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
1102 path.append(".");
1103 path.append(ext);
1104 }
1105
mtklein748ca3b2015-01-15 10:56:12 -08001106 if (bitmap) {
mtkleina5114d72015-08-24 13:27:01 -07001107 if (!dump_png(*bitmap, path.c_str(), result.md5.c_str())) {
mtklein748ca3b2015-01-15 10:56:12 -08001108 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
1109 return;
1110 }
1111 } else {
mtkleina5114d72015-08-24 13:27:01 -07001112 SkFILEWStream file(path.c_str());
1113 if (!file.isValid()) {
1114 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
1115 return;
1116 }
mtklein748ca3b2015-01-15 10:56:12 -08001117 if (!file.writeStream(data, len)) {
1118 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
1119 return;
1120 }
1121 }
1122 }
1123};
1124
mtklein748ca3b2015-01-15 10:56:12 -08001125/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1126
1127// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
1128
mtklein21eaf3b2016-02-08 12:39:59 -08001129static SkTDArray<skiatest::Test> gParallelTests, gSerialTests;
mtklein748ca3b2015-01-15 10:56:12 -08001130
1131static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -08001132 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -08001133 return;
1134 }
mtklein6393c062015-04-27 08:45:01 -07001135 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r; r = r->next()) {
1136 if (!in_shard()) {
1137 continue;
1138 }
halcanary87f3ba42015-01-20 09:30:20 -08001139 // Despite its name, factory() is returning a reference to
1140 // link-time static const POD data.
1141 const skiatest::Test& test = r->factory();
1142 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -08001143 continue;
1144 }
halcanary87f3ba42015-01-20 09:30:20 -08001145 if (test.needsGpu && gpu_supported()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001146 (FLAGS_gpu_threading ? gParallelTests : gSerialTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -08001147 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein21eaf3b2016-02-08 12:39:59 -08001148 gParallelTests.push(test);
mtklein82d28432015-01-15 12:46:02 -08001149 }
mtklein748ca3b2015-01-15 10:56:12 -08001150 }
1151}
1152
mtklein21eaf3b2016-02-08 12:39:59 -08001153static void run_test(skiatest::Test test) {
halcanary87f3ba42015-01-20 09:30:20 -08001154 struct : public skiatest::Reporter {
mtklein36352bf2015-03-25 18:17:31 -07001155 void reportFailed(const skiatest::Failure& failure) override {
halcanary87f3ba42015-01-20 09:30:20 -08001156 fail(failure.toString());
1157 JsonWriter::AddTestFailure(failure);
1158 }
mtklein36352bf2015-03-25 18:17:31 -07001159 bool allowExtendedTest() const override {
halcanary87f3ba42015-01-20 09:30:20 -08001160 return FLAGS_pathOpsExtended;
1161 }
mtklein36352bf2015-03-25 18:17:31 -07001162 bool verbose() const override { return FLAGS_veryVerbose; }
halcanary87f3ba42015-01-20 09:30:20 -08001163 } reporter;
djsollen824996a2015-06-12 12:06:22 -07001164
mtklein3eed7dd2016-02-24 19:07:07 -08001165 if (!FLAGS_dryRun && !is_blacklisted("_", "tests", "_", test.name)) {
mtklein21eaf3b2016-02-08 12:39:59 -08001166 start("unit", "test", "", test.name);
mtklein55e88b22015-01-21 15:50:13 -08001167 GrContextFactory factory;
mtklein21eaf3b2016-02-08 12:39:59 -08001168 test.proc(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -08001169 }
mtklein3eed7dd2016-02-24 19:07:07 -08001170 done("unit", "test", "", test.name);
mtklein748ca3b2015-01-15 10:56:12 -08001171}
1172
1173/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1174
mtkleinafae30a2016-02-24 12:28:32 -08001175DEFINE_int32(status_sec, 15, "Print status this often (and if we crash).");
1176
1177SkThread* start_status_thread() {
1178 auto thread = new SkThread([] (void*) {
1179 for (;;) {
1180 print_status();
1181 #if defined(SK_BUILD_FOR_WIN)
1182 Sleep(FLAGS_status_sec * 1000);
1183 #else
1184 sleep(FLAGS_status_sec);
1185 #endif
mtklein2e1c47e2015-03-12 07:16:56 -07001186 }
mtkleinafae30a2016-02-24 12:28:32 -08001187 });
1188 thread->start();
1189 return thread;
mtkleinde6fc2b2015-03-12 06:28:54 -07001190}
1191
caryclark83ca6282015-06-10 09:31:09 -07001192#define PORTABLE_FONT_PREFIX "Toy Liberation "
1193
1194static SkTypeface* create_from_name(const char familyName[], SkTypeface::Style style) {
1195 if (familyName && strlen(familyName) > sizeof(PORTABLE_FONT_PREFIX)
1196 && !strncmp(familyName, PORTABLE_FONT_PREFIX, sizeof(PORTABLE_FONT_PREFIX) - 1)) {
caryclark1818acb2015-07-24 12:09:25 -07001197 return sk_tool_utils::create_portable_typeface(familyName, style);
caryclark83ca6282015-06-10 09:31:09 -07001198 }
halcanary96fcdcc2015-08-27 07:41:13 -07001199 return nullptr;
caryclark83ca6282015-06-10 09:31:09 -07001200}
1201
1202#undef PORTABLE_FONT_PREFIX
1203
1204extern SkTypeface* (*gCreateTypefaceDelegate)(const char [], SkTypeface::Style );
1205
jcgregorio3b27ade2014-11-13 08:06:40 -08001206int dm_main();
caryclark17f0b6d2014-07-22 10:15:34 -07001207int dm_main() {
mtklein246ba3a2016-02-23 10:39:36 -08001208 setup_crash_handler();
mtkleinafae30a2016-02-24 12:28:32 -08001209
mtklein5286f022016-01-22 08:18:14 -08001210 JsonWriter::DumpJson(); // It's handy for the bots to assume this is ~never missing.
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +00001211 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -07001212 SkTaskGroup::Enabler enabled(FLAGS_threads);
caryclark83ca6282015-06-10 09:31:09 -07001213 gCreateTypefaceDelegate = &create_from_name;
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +00001214
halcanary7d571242016-02-24 17:59:16 -08001215 {
1216 SkString testResourcePath = GetResourcePath("color_wheel.png");
1217 SkFILEStream testResource(testResourcePath.c_str());
1218 if (!testResource.isValid()) {
1219 SkDebugf("Some resources are missing. Do you need to set --resourcePath?\n");
1220 }
1221 }
mtklein62bd1a62015-01-27 14:46:26 -08001222 gather_gold();
borenet09ed4802015-04-03 14:15:33 -07001223 gather_uninteresting_hashes();
mtklein62bd1a62015-01-27 14:46:26 -08001224
scroggo86737142016-02-03 12:19:11 -08001225 if (!gather_srcs()) {
1226 return 1;
1227 }
mtklein748ca3b2015-01-15 10:56:12 -08001228 gather_sinks();
1229 gather_tests();
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +00001230
mtklein21eaf3b2016-02-08 12:39:59 -08001231 gPending = gSrcs.count() * gSinks.count() + gParallelTests.count() + gSerialTests.count();
mtkleinafae30a2016-02-24 12:28:32 -08001232 SkDebugf("%d srcs * %d sinks + %d tests == %d tasks",
mtklein21eaf3b2016-02-08 12:39:59 -08001233 gSrcs.count(), gSinks.count(), gParallelTests.count() + gSerialTests.count(), gPending);
mtkleinafae30a2016-02-24 12:28:32 -08001234 SkAutoTDelete<SkThread> statusThread(start_status_thread());
mtklein748ca3b2015-01-15 10:56:12 -08001235
mtklein21eaf3b2016-02-08 12:39:59 -08001236 // Kick off as much parallel work as we can, making note of any serial work we'll need to do.
1237 SkTaskGroup parallel;
1238 SkTArray<Task> serial;
1239
1240 for (auto& sink : gSinks)
1241 for (auto& src : gSrcs) {
mtklein3eed7dd2016-02-24 19:07:07 -08001242 if (src->veto(sink->flags()) ||
1243 is_blacklisted(sink.tag.c_str(), src.tag.c_str(),
1244 src.options.c_str(), src->name().c_str())) {
1245 SkAutoTAcquire<SkPODSpinlock> lock(gMutex);
1246 gPending--;
1247 continue;
1248 }
1249
mtklein21eaf3b2016-02-08 12:39:59 -08001250 Task task(src, sink);
1251 if (src->serial() || sink->serial()) {
1252 serial.push_back(task);
1253 } else {
1254 parallel.add([task] { Task::Run(task); });
mtklein748ca3b2015-01-15 10:56:12 -08001255 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001256 }
mtklein21eaf3b2016-02-08 12:39:59 -08001257 for (auto test : gParallelTests) {
1258 parallel.add([test] { run_test(test); });
mtklein55e88b22015-01-21 15:50:13 -08001259 }
mtklein21eaf3b2016-02-08 12:39:59 -08001260
1261 // With the parallel work running, run serial tasks and tests here on main thread.
1262 for (auto task : serial) { Task::Run(task); }
1263 for (auto test : gSerialTests) { run_test(test); }
1264
1265 // Wait for any remaining parallel work to complete (including any spun off of serial tasks).
1266 parallel.wait();
mtkleincd50bca2016-01-05 06:20:20 -08001267 gDefinitelyThreadSafeWork.wait();
1268
mtkleinda884c42016-02-24 18:00:23 -08001269 // We'd better have run everything.
1270 SkASSERT(gPending == 0);
mtkleine027f172016-02-26 15:53:06 -08001271 // Make sure we've flushed all our results to disk.
1272 JsonWriter::DumpJson();
mtkleinda884c42016-02-24 18:00:23 -08001273
mtklein748ca3b2015-01-15 10:56:12 -08001274 // At this point we're back in single-threaded land.
caryclarkf53ce802015-06-15 06:48:30 -07001275 sk_tool_utils::release_portable_typefaces();
mtklein@google.comd36522d2013-10-16 13:02:15 +00001276
mtklein748ca3b2015-01-15 10:56:12 -08001277 if (gFailures.count() > 0) {
1278 SkDebugf("Failures:\n");
1279 for (int i = 0; i < gFailures.count(); i++) {
mtklein9dc09102015-01-15 15:47:33 -08001280 SkDebugf("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001281 }
1282 SkDebugf("%d failures\n", gFailures.count());
1283 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001284 }
mtkleinafae30a2016-02-24 12:28:32 -08001285
1286#ifdef SK_PDF_IMAGE_STATS
halcanary7a14b312015-10-01 07:28:13 -07001287 SkPDFImageDumpStats();
mtkleinafae30a2016-02-24 12:28:32 -08001288#endif // SK_PDF_IMAGE_STATS
1289
1290 print_status();
scroggo4d9eaea2016-01-29 07:48:33 -08001291 SkDebugf("Finished!\n");
mtklein748ca3b2015-01-15 10:56:12 -08001292 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +00001293}
jcgregorio3b27ade2014-11-13 08:06:40 -08001294
kkinnunen179a8f52015-11-20 13:32:24 -08001295// TODO: currently many GPU tests are declared outside SK_SUPPORT_GPU guards.
1296// Thus we export the empty RunWithGPUTestContexts when SK_SUPPORT_GPU=0.
1297namespace skiatest {
1298namespace {
1299typedef void(*TestWithGrContext)(skiatest::Reporter*, GrContext*);
1300typedef void(*TestWithGrContextAndGLContext)(skiatest::Reporter*, GrContext*, SkGLContext*);
1301#if SK_SUPPORT_GPU
1302template<typename T>
kkinnunen34058002016-01-06 23:49:30 -08001303void call_test(T test, skiatest::Reporter* reporter, const GrContextFactory::ContextInfo& context);
kkinnunen179a8f52015-11-20 13:32:24 -08001304template<>
1305void call_test(TestWithGrContext test, skiatest::Reporter* reporter,
kkinnunen34058002016-01-06 23:49:30 -08001306 const GrContextFactory::ContextInfo& context) {
1307 test(reporter, context.fGrContext);
kkinnunen179a8f52015-11-20 13:32:24 -08001308}
1309template<>
1310void call_test(TestWithGrContextAndGLContext test, skiatest::Reporter* reporter,
kkinnunen34058002016-01-06 23:49:30 -08001311 const GrContextFactory::ContextInfo& context) {
1312 test(reporter, context.fGrContext, context.fGLContext);
kkinnunen179a8f52015-11-20 13:32:24 -08001313}
1314#endif
1315} // namespace
1316
kkinnunen179a8f52015-11-20 13:32:24 -08001317template<typename T>
1318void RunWithGPUTestContexts(T test, GPUTestContexts testContexts, Reporter* reporter,
1319 GrContextFactory* factory) {
1320#if SK_SUPPORT_GPU
kkinnunen3e980c32015-12-23 01:33:00 -08001321 // Iterate over context types, except use "native" instead of explicitly trying OpenGL and
1322 // OpenGL ES. Do not use GLES on desktop, since tests do not account for not fixing
1323 // http://skbug.com/2809
1324 GrContextFactory::GLContextType contextTypes[] = {
1325 GrContextFactory::kNative_GLContextType,
1326#if SK_ANGLE
1327#ifdef SK_BUILD_FOR_WIN
1328 GrContextFactory::kANGLE_GLContextType,
1329#endif
1330 GrContextFactory::kANGLE_GL_GLContextType,
1331#endif
1332#if SK_COMMAND_BUFFER
kkinnunen45c2c812016-02-25 02:03:43 -08001333 GrContextFactory::kCommandBufferES2_GLContextType,
1334 GrContextFactory::kCommandBufferES3_GLContextType,
kkinnunen3e980c32015-12-23 01:33:00 -08001335#endif
1336#if SK_MESA
1337 GrContextFactory::kMESA_GLContextType,
1338#endif
1339 GrContextFactory::kNull_GLContextType,
1340 GrContextFactory::kDebug_GLContextType,
1341 };
1342 static_assert(SK_ARRAY_COUNT(contextTypes) == GrContextFactory::kGLContextTypeCnt - 2,
1343 "Skipping unexpected GLContextType for GPU tests");
1344
1345 for (auto& contextType : contextTypes) {
kkinnunen179a8f52015-11-20 13:32:24 -08001346 int contextSelector = kNone_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001347 if (GrContextFactory::IsRenderingGLContext(contextType)) {
kkinnunen179a8f52015-11-20 13:32:24 -08001348 contextSelector |= kAllRendering_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001349 } else if (contextType == GrContextFactory::kNative_GLContextType) {
kkinnunen179a8f52015-11-20 13:32:24 -08001350 contextSelector |= kNative_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001351 } else if (contextType == GrContextFactory::kNull_GLContextType) {
kkinnunen179a8f52015-11-20 13:32:24 -08001352 contextSelector |= kNull_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001353 } else if (contextType == GrContextFactory::kDebug_GLContextType) {
kkinnunen55eeae92015-12-10 23:19:29 -08001354 contextSelector |= kDebug_GPUTestContexts;
kkinnunen179a8f52015-11-20 13:32:24 -08001355 }
1356 if ((testContexts & contextSelector) == 0) {
1357 continue;
1358 }
kkinnunen34058002016-01-06 23:49:30 -08001359 GrContextFactory::ContextInfo context = factory->getContextInfo(contextType);
1360 if (context.fGrContext) {
kkinnunen5219fd92015-12-10 06:28:13 -08001361 call_test(test, reporter, context);
1362 }
kkinnunen34058002016-01-06 23:49:30 -08001363 context = factory->getContextInfo(contextType,
1364 GrContextFactory::kEnableNVPR_GLContextOptions);
1365 if (context.fGrContext) {
kkinnunen179a8f52015-11-20 13:32:24 -08001366 call_test(test, reporter, context);
1367 }
1368 }
1369#endif
1370}
1371
1372template
1373void RunWithGPUTestContexts<TestWithGrContext>(TestWithGrContext test,
1374 GPUTestContexts testContexts,
1375 Reporter* reporter,
1376 GrContextFactory* factory);
1377template
1378void RunWithGPUTestContexts<TestWithGrContextAndGLContext>(TestWithGrContextAndGLContext test,
1379 GPUTestContexts testContexts,
1380 Reporter* reporter,
1381 GrContextFactory* factory);
1382} // namespace skiatest
1383
borenet48087572015-04-02 12:16:36 -07001384#if !defined(SK_BUILD_FOR_IOS)
jcgregorio3b27ade2014-11-13 08:06:40 -08001385int main(int argc, char** argv) {
1386 SkCommandLineFlags::Parse(argc, argv);
1387 return dm_main();
1388}
1389#endif