blob: a046421908e7ee6be0694c2bbb57b653c26fc53f [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 "OverwriteLine.h"
13#include "ProcStats.h"
14#include "SkBBHFactory.h"
mtklein62bd1a62015-01-27 14:46:26 -080015#include "SkChecksum.h"
scroggocc2feb12015-08-14 08:32:46 -070016#include "SkCodec.h"
caryclark17f0b6d2014-07-22 10:15:34 -070017#include "SkCommonFlags.h"
kkinnunen3e980c32015-12-23 01:33:00 -080018#include "SkCommonFlagsConfig.h"
caryclark83ca6282015-06-10 09:31:09 -070019#include "SkFontMgr.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +000020#include "SkForceLinking.h"
21#include "SkGraphics.h"
mtklein748ca3b2015-01-15 10:56:12 -080022#include "SkMD5.h"
mtklein1b249332015-07-07 12:21:21 -070023#include "SkMutex.h"
mtklein1d0f1642014-09-08 08:05:18 -070024#include "SkOSFile.h"
mtklein246ba3a2016-02-23 10:39:36 -080025#include "SkSpinlock.h"
mtkleina82f5622015-02-20 12:30:19 -080026#include "SkTHash.h"
mtklein406654b2014-09-03 15:34:37 -070027#include "SkTaskGroup.h"
mtkleinde6fc2b2015-03-12 06:28:54 -070028#include "SkThreadUtils.h"
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +000029#include "Test.h"
mtklein748ca3b2015-01-15 10:56:12 -080030#include "Timer.h"
caryclark83ca6282015-06-10 09:31:09 -070031#include "sk_tool_utils.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +000032
halcanary7a14b312015-10-01 07:28:13 -070033#ifdef SK_PDF_IMAGE_STATS
34extern void SkPDFImageDumpStats();
35#endif
36
mtkleina5114d72015-08-24 13:27:01 -070037#include "png.h"
38
bungeman60e0fee2015-08-26 05:15:46 -070039#include <stdlib.h>
40
scroggo27a58342015-10-28 08:56:41 -070041#ifndef SK_BUILD_FOR_WIN32
42 #include <unistd.h>
43#endif
44
djsollen54416de2015-04-03 07:24:48 -070045DEFINE_string(src, "tests gm skp image", "Source types to test.");
mtklein748ca3b2015-01-15 10:56:12 -080046DEFINE_bool(nameByHash, false,
47 "If true, write to FLAGS_writePath[0]/<hash>.png instead of "
djsollen54416de2015-04-03 07:24:48 -070048 "to FLAGS_writePath[0]/<config>/<sourceType>/<sourceOptions>/<name>.png");
mtklein748ca3b2015-01-15 10:56:12 -080049DEFINE_bool2(pathOpsExtended, x, false, "Run extended pathOps tests.");
mtkleind603b222015-02-17 11:13:33 -080050DEFINE_string(matrix, "1 0 0 1",
51 "2x2 scale+skew matrix to apply or upright when using "
52 "'matrix' or 'upright' in config.");
mtklein82d28432015-01-15 12:46:02 -080053DEFINE_bool(gpu_threading, false, "Allow GPU work to run on multiple threads?");
mtklein@google.comd36522d2013-10-16 13:02:15 +000054
mtkleina2ef6422015-01-15 13:44:22 -080055DEFINE_string(blacklist, "",
djsollen54416de2015-04-03 07:24:48 -070056 "Space-separated config/src/srcOptions/name quadruples to blacklist. '_' matches anything. E.g. \n"
57 "'--blacklist gpu skp _ _' will blacklist all SKPs drawn into the gpu config.\n"
58 "'--blacklist gpu skp _ _ 8888 gm _ aarects' will also blacklist the aarects GM on 8888.");
mtkleina2ef6422015-01-15 13:44:22 -080059
mtklein62bd1a62015-01-27 14:46:26 -080060DEFINE_string2(readPath, r, "", "If set check for equality with golden results in this directory.");
61
borenet09ed4802015-04-03 14:15:33 -070062DEFINE_string(uninterestingHashesFile, "",
63 "File containing a list of uninteresting hashes. If a result hashes to something in "
64 "this list, no image is written for that result.");
65
mtklein6393c062015-04-27 08:45:01 -070066DEFINE_int32(shards, 1, "We're splitting source data into this many shards.");
67DEFINE_int32(shard, 0, "Which shard do I run?");
68
mtklein@google.comd36522d2013-10-16 13:02:15 +000069__SK_FORCE_IMAGE_DECODER_LINKING;
mtklein748ca3b2015-01-15 10:56:12 -080070using namespace DM;
mtklein@google.comd36522d2013-10-16 13:02:15 +000071
mtklein748ca3b2015-01-15 10:56:12 -080072/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
73
mtkleinf27f08b2015-11-03 06:54:24 -080074static double now_ms() { return SkTime::GetNSecs() * 1e-6; }
75
mtklein748ca3b2015-01-15 10:56:12 -080076SK_DECLARE_STATIC_MUTEX(gFailuresMutex);
77static SkTArray<SkString> gFailures;
78
79static void fail(ImplicitString err) {
80 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 -070085static int32_t gPending = 0; // Atomic. Total number of running and queued tasks.
86
mtklein246ba3a2016-02-23 10:39:36 -080087// We use a spinlock to make locking this in a signal handler _somewhat_ safe.
88SK_DECLARE_STATIC_SPINLOCK(gRunningAndTallyMutex);
mtkleinbc4e0032015-12-09 08:37:35 -080089static SkTArray<SkString> gRunning;
90static SkTHashMap<SkString, int> gNoteTally;
mtklein748ca3b2015-01-15 10:56:12 -080091
mtkleinb9eb4ac2015-02-02 18:26:03 -080092static void done(double ms,
djsollen54416de2015-04-03 07:24:48 -070093 ImplicitString config, ImplicitString src, ImplicitString srcOptions,
94 ImplicitString name, ImplicitString note, ImplicitString log) {
95 SkString id = SkStringPrintf("%s %s %s %s", config.c_str(), src.c_str(),
96 srcOptions.c_str(), name.c_str());
mtkleinb37cb412015-03-18 05:27:14 -070097 {
mtklein246ba3a2016-02-23 10:39:36 -080098 SkAutoTAcquire<SkPODSpinlock> lock(gRunningAndTallyMutex);
mtkleinb37cb412015-03-18 05:27:14 -070099 for (int i = 0; i < gRunning.count(); i++) {
100 if (gRunning[i] == id) {
101 gRunning.removeShuffle(i);
102 break;
103 }
104 }
mtkleinbc4e0032015-12-09 08:37:35 -0800105 if (!note.isEmpty()) {
106 if (int* tally = gNoteTally.find(note)) {
107 *tally += 1;
108 } else {
109 gNoteTally.set(note, 1);
110 }
111 }
mtkleinb37cb412015-03-18 05:27:14 -0700112 }
mtkleinb9eb4ac2015-02-02 18:26:03 -0800113 if (!log.isEmpty()) {
114 log.prepend("\n");
115 }
mtklein6dee2ad2015-02-05 09:53:44 -0800116 auto pending = sk_atomic_dec(&gPending)-1;
mtkleinbc4e0032015-12-09 08:37:35 -0800117 if (!FLAGS_quiet && note.isEmpty()) {
118 SkDebugf("%s(%4d/%-4dMB %6d) %s\t%s%s", FLAGS_verbose ? "\n" : kSkOverwriteLine
reed50bc0512015-05-19 14:13:31 -0700119 , sk_tools::getCurrResidentSetSizeMB()
120 , sk_tools::getMaxResidentSetSizeMB()
121 , pending
122 , HumanizeMs(ms).c_str()
123 , id.c_str()
reed50bc0512015-05-19 14:13:31 -0700124 , log.c_str());
125 }
mtkleina17241b2015-01-23 05:48:00 -0800126 // We write our dm.json file every once in a while in case we crash.
127 // Notice this also handles the final dm.json when pending == 0.
128 if (pending % 500 == 0) {
129 JsonWriter::DumpJson();
130 }
mtklein@google.comd36522d2013-10-16 13:02:15 +0000131}
132
djsollen54416de2015-04-03 07:24:48 -0700133static void start(ImplicitString config, ImplicitString src,
134 ImplicitString srcOptions, ImplicitString name) {
135 SkString id = SkStringPrintf("%s %s %s %s", config.c_str(), src.c_str(),
136 srcOptions.c_str(), name.c_str());
mtklein246ba3a2016-02-23 10:39:36 -0800137 SkAutoTAcquire<SkPODSpinlock> lock(gRunningAndTallyMutex);
mtkleinb37cb412015-03-18 05:27:14 -0700138 gRunning.push_back(id);
139}
140
mtklein246ba3a2016-02-23 10:39:36 -0800141#if defined(SK_BUILD_FOR_WIN32)
142 static void setup_crash_handler() {
143 // TODO: custom crash handler like below to print out what was running
144 SetupCrashHandler();
145 }
146
147#else
148 #include <signal.h>
149 static void setup_crash_handler() {
150 const int kSignals[] = { SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGSEGV };
151 for (int sig : kSignals) {
152 signal(sig, [](int sig) {
153 SkAutoTAcquire<SkPODSpinlock> lock(gRunningAndTallyMutex);
154 SkDebugf("\nCaught signal %d [%s] while running %d tasks:\n",
155 sig, strsignal(sig), gRunning.count());
156 for (auto& task : gRunning) {
157 SkDebugf("\t%s\n", task.c_str());
158 }
159 });
160 }
161 }
162#endif
163
mtklein748ca3b2015-01-15 10:56:12 -0800164/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
mtklein114c3cd2015-01-15 10:15:02 -0800165
mtklein62bd1a62015-01-27 14:46:26 -0800166struct Gold : public SkString {
mtkleina82f5622015-02-20 12:30:19 -0800167 Gold() : SkString("") {}
djsollen54416de2015-04-03 07:24:48 -0700168 Gold(ImplicitString sink, ImplicitString src, ImplicitString srcOptions,
169 ImplicitString name, ImplicitString md5)
mtklein62bd1a62015-01-27 14:46:26 -0800170 : SkString("") {
171 this->append(sink);
172 this->append(src);
djsollen54416de2015-04-03 07:24:48 -0700173 this->append(srcOptions);
mtklein62bd1a62015-01-27 14:46:26 -0800174 this->append(name);
175 this->append(md5);
mtklein62bd1a62015-01-27 14:46:26 -0800176 }
mtkleinc8d1dd42015-10-15 12:23:01 -0700177 struct Hash {
178 uint32_t operator()(const Gold& g) const {
179 return SkGoodHash()((const SkString&)g);
180 }
181 };
mtklein62bd1a62015-01-27 14:46:26 -0800182};
mtkleina82f5622015-02-20 12:30:19 -0800183static SkTHashSet<Gold, Gold::Hash> gGold;
mtklein62bd1a62015-01-27 14:46:26 -0800184
185static void add_gold(JsonWriter::BitmapResult r) {
djsollen54416de2015-04-03 07:24:48 -0700186 gGold.add(Gold(r.config, r.sourceType, r.sourceOptions, r.name, r.md5));
mtklein62bd1a62015-01-27 14:46:26 -0800187}
188
189static void gather_gold() {
190 if (!FLAGS_readPath.isEmpty()) {
191 SkString path(FLAGS_readPath[0]);
192 path.append("/dm.json");
193 if (!JsonWriter::ReadJson(path.c_str(), add_gold)) {
194 fail(SkStringPrintf("Couldn't read %s for golden results.", path.c_str()));
195 }
196 }
197}
198
199/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
200
borenet09ed4802015-04-03 14:15:33 -0700201static SkTHashSet<SkString> gUninterestingHashes;
202
203static void gather_uninteresting_hashes() {
204 if (!FLAGS_uninterestingHashesFile.isEmpty()) {
205 SkAutoTUnref<SkData> data(SkData::NewFromFileName(FLAGS_uninterestingHashesFile[0]));
mtkleincc334b32015-09-22 11:43:53 -0700206 if (!data) {
207 SkDebugf("WARNING: unable to read uninteresting hashes from %s\n",
208 FLAGS_uninterestingHashesFile[0]);
209 return;
210 }
borenet09ed4802015-04-03 14:15:33 -0700211 SkTArray<SkString> hashes;
212 SkStrSplit((const char*)data->data(), "\n", &hashes);
213 for (const SkString& hash : hashes) {
214 gUninterestingHashes.add(hash);
215 }
mtklein136baaa2016-02-03 11:21:45 -0800216 SkDebugf("FYI: loaded %d distinct uninteresting hashes from %d lines\n",
217 gUninterestingHashes.count(), hashes.count());
borenet09ed4802015-04-03 14:15:33 -0700218 }
219}
220
221/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
222
mtkleine0effd62015-07-29 06:37:28 -0700223struct TaggedSrc : public SkAutoTDelete<Src> {
msarett9e707a02015-09-01 14:57:57 -0700224 ImplicitString tag;
225 ImplicitString options;
mtkleine0effd62015-07-29 06:37:28 -0700226};
227
228struct TaggedSink : public SkAutoTDelete<Sink> {
kkinnunen3e980c32015-12-23 01:33:00 -0800229 SkString tag;
djsollen54416de2015-04-03 07:24:48 -0700230};
mtklein748ca3b2015-01-15 10:56:12 -0800231
232static const bool kMemcpyOK = true;
233
mtkleine0effd62015-07-29 06:37:28 -0700234static SkTArray<TaggedSrc, kMemcpyOK> gSrcs;
235static SkTArray<TaggedSink, kMemcpyOK> gSinks;
mtklein748ca3b2015-01-15 10:56:12 -0800236
mtklein6393c062015-04-27 08:45:01 -0700237static bool in_shard() {
238 static int N = 0;
239 return N++ % FLAGS_shards == FLAGS_shard;
240}
241
msarett9e707a02015-09-01 14:57:57 -0700242static void push_src(ImplicitString tag, ImplicitString options, Src* s) {
mtklein748ca3b2015-01-15 10:56:12 -0800243 SkAutoTDelete<Src> src(s);
mtklein6393c062015-04-27 08:45:01 -0700244 if (in_shard() &&
msarett9e707a02015-09-01 14:57:57 -0700245 FLAGS_src.contains(tag.c_str()) &&
mtklein748ca3b2015-01-15 10:56:12 -0800246 !SkCommandLineFlags::ShouldSkip(FLAGS_match, src->name().c_str())) {
mtkleine0effd62015-07-29 06:37:28 -0700247 TaggedSrc& s = gSrcs.push_back();
mtklein748ca3b2015-01-15 10:56:12 -0800248 s.reset(src.detach());
249 s.tag = tag;
djsollen54416de2015-04-03 07:24:48 -0700250 s.options = options;
mtklein114c3cd2015-01-15 10:15:02 -0800251 }
mtklein748ca3b2015-01-15 10:56:12 -0800252}
mtklein114c3cd2015-01-15 10:15:02 -0800253
msarett9e707a02015-09-01 14:57:57 -0700254static void push_codec_src(Path path, CodecSrc::Mode mode, CodecSrc::DstColorType dstColorType,
scroggoc5560be2016-02-03 09:42:42 -0800255 SkAlphaType dstAlphaType, float scale) {
msarett9e707a02015-09-01 14:57:57 -0700256 SkString folder;
257 switch (mode) {
258 case CodecSrc::kCodec_Mode:
259 folder.append("codec");
260 break;
msarettbb25b532016-01-13 09:31:39 -0800261 case CodecSrc::kCodecZeroInit_Mode:
262 folder.append("codec_zero_init");
263 break;
msarett9e707a02015-09-01 14:57:57 -0700264 case CodecSrc::kScanline_Mode:
265 folder.append("scanline");
266 break;
msarett9e707a02015-09-01 14:57:57 -0700267 case CodecSrc::kStripe_Mode:
268 folder.append("stripe");
269 break;
msarett91c22b22016-02-22 12:27:46 -0800270 case CodecSrc::kCroppedScanline_Mode:
271 folder.append("crop");
272 break;
msarett9e707a02015-09-01 14:57:57 -0700273 case CodecSrc::kSubset_Mode:
msarett36c37962015-09-02 13:20:52 -0700274 folder.append("codec_subset");
msarett9e707a02015-09-01 14:57:57 -0700275 break;
msarettb714fb02016-01-22 14:46:42 -0800276 case CodecSrc::kGen_Mode:
277 folder.append("gen");
278 break;
msarett9e707a02015-09-01 14:57:57 -0700279 }
280
281 switch (dstColorType) {
282 case CodecSrc::kGrayscale_Always_DstColorType:
283 folder.append("_kGray8");
284 break;
285 case CodecSrc::kIndex8_Always_DstColorType:
286 folder.append("_kIndex8");
287 break;
288 default:
289 break;
290 }
291
scroggoc5560be2016-02-03 09:42:42 -0800292 switch (dstAlphaType) {
293 case kOpaque_SkAlphaType:
294 folder.append("_opaque");
295 break;
296 case kPremul_SkAlphaType:
297 folder.append("_premul");
298 break;
299 case kUnpremul_SkAlphaType:
300 folder.append("_unpremul");
301 break;
302 default:
303 break;
304 }
305
msarett9e707a02015-09-01 14:57:57 -0700306 if (1.0f != scale) {
307 folder.appendf("_%.3f", scale);
308 }
309
scroggoc5560be2016-02-03 09:42:42 -0800310 CodecSrc* src = new CodecSrc(path, mode, dstColorType, dstAlphaType, scale);
msarett9e707a02015-09-01 14:57:57 -0700311 push_src("image", folder, src);
312}
313
msarett3d9d7a72015-10-21 10:27:10 -0700314static void push_android_codec_src(Path path, AndroidCodecSrc::Mode mode,
scroggoc5560be2016-02-03 09:42:42 -0800315 CodecSrc::DstColorType dstColorType, SkAlphaType dstAlphaType, int sampleSize) {
msarett3d9d7a72015-10-21 10:27:10 -0700316 SkString folder;
317 switch (mode) {
318 case AndroidCodecSrc::kFullImage_Mode:
319 folder.append("scaled_codec");
320 break;
321 case AndroidCodecSrc::kDivisor_Mode:
322 folder.append("scaled_codec_divisor");
323 break;
324 }
325
326 switch (dstColorType) {
327 case CodecSrc::kGrayscale_Always_DstColorType:
328 folder.append("_kGray8");
329 break;
330 case CodecSrc::kIndex8_Always_DstColorType:
331 folder.append("_kIndex8");
332 break;
333 default:
334 break;
335 }
336
scroggoc5560be2016-02-03 09:42:42 -0800337 switch (dstAlphaType) {
338 case kOpaque_SkAlphaType:
339 folder.append("_opaque");
340 break;
341 case kPremul_SkAlphaType:
342 folder.append("_premul");
343 break;
msarett9e9444c2016-02-03 12:39:10 -0800344 case kUnpremul_SkAlphaType:
345 folder.append("_unpremul");
346 break;
scroggoc5560be2016-02-03 09:42:42 -0800347 default:
348 break;
349 }
350
msarett3d9d7a72015-10-21 10:27:10 -0700351 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800352 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700353 }
354
scroggoc5560be2016-02-03 09:42:42 -0800355 AndroidCodecSrc* src = new AndroidCodecSrc(path, mode, dstColorType, dstAlphaType, sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700356 push_src("image", folder, src);
357}
358
msarett438b2ad2015-04-09 12:43:10 -0700359static void push_codec_srcs(Path path) {
360 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
361 if (!encoded) {
362 SkDebugf("Couldn't read %s.", path.c_str());
363 return;
364 }
365 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
halcanary96fcdcc2015-08-27 07:41:13 -0700366 if (nullptr == codec.get()) {
msarett438b2ad2015-04-09 12:43:10 -0700367 SkDebugf("Couldn't create codec for %s.", path.c_str());
368 return;
369 }
370
msarett36c37962015-09-02 13:20:52 -0700371 // Native Scales
msarett0a242972015-06-11 14:27:27 -0700372 // TODO (msarett): Implement scaling tests for SkImageDecoder in order to compare with these
373 // tests. SkImageDecoder supports downscales by integer factors.
emmaleer8f4ba762015-08-14 07:44:46 -0700374 // 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 -0700375 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 -0700376
msarett91c22b22016-02-22 12:27:46 -0800377 SkTArray<CodecSrc::Mode> nativeModes;
378 nativeModes.push_back(CodecSrc::kCodec_Mode);
379 nativeModes.push_back(CodecSrc::kCodecZeroInit_Mode);
380 nativeModes.push_back(CodecSrc::kGen_Mode);
381 switch (codec->getEncodedFormat()) {
382 case SkEncodedFormat::kJPEG_SkEncodedFormat:
383 nativeModes.push_back(CodecSrc::kScanline_Mode);
384 nativeModes.push_back(CodecSrc::kStripe_Mode);
385 nativeModes.push_back(CodecSrc::kCroppedScanline_Mode);
386 break;
387 case SkEncodedFormat::kWEBP_SkEncodedFormat:
388 nativeModes.push_back(CodecSrc::kSubset_Mode);
389 break;
msarettb65e6042016-02-23 05:37:25 -0800390 case SkEncodedFormat::kRAW_SkEncodedFormat:
391 break;
msarett91c22b22016-02-22 12:27:46 -0800392 default:
393 nativeModes.push_back(CodecSrc::kScanline_Mode);
394 nativeModes.push_back(CodecSrc::kStripe_Mode);
395 break;
396 }
msarett9e707a02015-09-01 14:57:57 -0700397
msarett91c22b22016-02-22 12:27:46 -0800398 SkTArray<CodecSrc::DstColorType> colorTypes;
399 colorTypes.push_back(CodecSrc::kGetFromCanvas_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700400 switch (codec->getInfo().colorType()) {
401 case kGray_8_SkColorType:
msarett91c22b22016-02-22 12:27:46 -0800402 colorTypes.push_back(CodecSrc::kGrayscale_Always_DstColorType);
msarett55f7bdd2016-02-16 13:24:54 -0800403 if (kWBMP_SkEncodedFormat == codec->getEncodedFormat()) {
msarett91c22b22016-02-22 12:27:46 -0800404 colorTypes.push_back(CodecSrc::kIndex8_Always_DstColorType);
msarett55f7bdd2016-02-16 13:24:54 -0800405 }
msarett36c37962015-09-02 13:20:52 -0700406 break;
407 case kIndex_8_SkColorType:
msarett91c22b22016-02-22 12:27:46 -0800408 colorTypes.push_back(CodecSrc::kIndex8_Always_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700409 break;
410 default:
msarett36c37962015-09-02 13:20:52 -0700411 break;
412 }
msarett9e707a02015-09-01 14:57:57 -0700413
scroggoc5560be2016-02-03 09:42:42 -0800414 SkTArray<SkAlphaType> alphaModes;
415 alphaModes.push_back(kPremul_SkAlphaType);
msarett9e9444c2016-02-03 12:39:10 -0800416 alphaModes.push_back(kUnpremul_SkAlphaType);
scroggoc5560be2016-02-03 09:42:42 -0800417 if (codec->getInfo().alphaType() == kOpaque_SkAlphaType) {
418 alphaModes.push_back(kOpaque_SkAlphaType);
419 }
msarettb714fb02016-01-22 14:46:42 -0800420
421 for (CodecSrc::Mode mode : nativeModes) {
422 // SkCodecImageGenerator only runs for the default colorType
423 // recommended by SkCodec. There is no need to generate multiple
424 // tests for different colorTypes.
425 // TODO (msarett): Add scaling support to SkCodecImageGenerator.
426 if (CodecSrc::kGen_Mode == mode) {
427 // FIXME: The gpu backend does not draw kGray sources correctly. (skbug.com/4822)
428 if (kGray_8_SkColorType != codec->getInfo().colorType()) {
scroggoc5560be2016-02-03 09:42:42 -0800429 push_codec_src(path, mode, CodecSrc::kGetFromCanvas_DstColorType,
430 codec->getInfo().alphaType(), 1.0f);
msarettb714fb02016-01-22 14:46:42 -0800431 }
432 continue;
433 }
434
435 for (float scale : nativeScales) {
msarett91c22b22016-02-22 12:27:46 -0800436 for (CodecSrc::DstColorType colorType : colorTypes) {
scroggoc5560be2016-02-03 09:42:42 -0800437 for (SkAlphaType alphaType : alphaModes) {
msarett91c22b22016-02-22 12:27:46 -0800438 // Only test kCroppedScanline_Mode when the alpha type is opaque. The test is
439 // slow and won't be interestingly different with different alpha types.
440 if (CodecSrc::kCroppedScanline_Mode == mode &&
441 kOpaque_SkAlphaType != alphaType) {
442 continue;
443 }
444
445 push_codec_src(path, mode, colorType, alphaType, scale);
scroggoc5560be2016-02-03 09:42:42 -0800446 }
msarett9e707a02015-09-01 14:57:57 -0700447 }
448 }
msarett0a242972015-06-11 14:27:27 -0700449 }
msarett36c37962015-09-02 13:20:52 -0700450
halcanary6950de62015-11-07 05:29:00 -0800451 // https://bug.skia.org/4428
msarett33c76232015-11-16 13:43:40 -0800452 bool subset = false;
453 // The following image types are supported by BitmapRegionDecoder,
454 // so we will test full image decodes and subset decodes.
msarettbe8216a2015-12-04 08:00:50 -0800455 static const char* const exts[] = {
msarett3d9d7a72015-10-21 10:27:10 -0700456 "jpg", "jpeg", "png", "webp",
457 "JPG", "JPEG", "PNG", "WEBP",
458 };
msarettbe8216a2015-12-04 08:00:50 -0800459 for (const char* ext : exts) {
msarett3d9d7a72015-10-21 10:27:10 -0700460 if (path.endsWith(ext)) {
msarett33c76232015-11-16 13:43:40 -0800461 subset = true;
msarett3d9d7a72015-10-21 10:27:10 -0700462 break;
463 }
464 }
msarett33c76232015-11-16 13:43:40 -0800465
msarett3d9d7a72015-10-21 10:27:10 -0700466 const int sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
msarett36c37962015-09-02 13:20:52 -0700467
msarett3d9d7a72015-10-21 10:27:10 -0700468 for (int sampleSize : sampleSizes) {
msarett91c22b22016-02-22 12:27:46 -0800469 for (CodecSrc::DstColorType colorType : colorTypes) {
scroggoc5560be2016-02-03 09:42:42 -0800470 for (SkAlphaType alphaType : alphaModes) {
msarett91c22b22016-02-22 12:27:46 -0800471 push_android_codec_src(path, AndroidCodecSrc::kFullImage_Mode, colorType,
scroggoc5560be2016-02-03 09:42:42 -0800472 alphaType, sampleSize);
473 if (subset) {
msarett91c22b22016-02-22 12:27:46 -0800474 push_android_codec_src(path, AndroidCodecSrc::kDivisor_Mode, colorType,
scroggoc5560be2016-02-03 09:42:42 -0800475 alphaType, sampleSize);
476 }
msarett3d9d7a72015-10-21 10:27:10 -0700477 }
msarett36c37962015-09-02 13:20:52 -0700478 }
479 }
msarett438b2ad2015-04-09 12:43:10 -0700480}
481
msarett5cb48852015-11-06 08:56:32 -0800482static bool brd_color_type_supported(SkBitmapRegionDecoder::Strategy strategy,
msaretta5783ae2015-09-08 15:35:32 -0700483 CodecSrc::DstColorType dstColorType) {
484 switch (strategy) {
msarett5cb48852015-11-06 08:56:32 -0800485 case SkBitmapRegionDecoder::kCanvas_Strategy:
msaretta5783ae2015-09-08 15:35:32 -0700486 if (CodecSrc::kGetFromCanvas_DstColorType == dstColorType) {
487 return true;
488 }
489 return false;
msarett5cb48852015-11-06 08:56:32 -0800490 case SkBitmapRegionDecoder::kAndroidCodec_Strategy:
msarett26ad17b2015-10-22 07:29:19 -0700491 switch (dstColorType) {
492 case CodecSrc::kGetFromCanvas_DstColorType:
493 case CodecSrc::kIndex8_Always_DstColorType:
494 case CodecSrc::kGrayscale_Always_DstColorType:
495 return true;
496 default:
497 return false;
498 }
msaretta5783ae2015-09-08 15:35:32 -0700499 default:
500 SkASSERT(false);
501 return false;
502 }
503}
504
msarett5cb48852015-11-06 08:56:32 -0800505static void push_brd_src(Path path, SkBitmapRegionDecoder::Strategy strategy,
msaretta5783ae2015-09-08 15:35:32 -0700506 CodecSrc::DstColorType dstColorType, BRDSrc::Mode mode, uint32_t sampleSize) {
507 SkString folder;
508 switch (strategy) {
msarett5cb48852015-11-06 08:56:32 -0800509 case SkBitmapRegionDecoder::kCanvas_Strategy:
msaretta5783ae2015-09-08 15:35:32 -0700510 folder.append("brd_canvas");
511 break;
msarett5cb48852015-11-06 08:56:32 -0800512 case SkBitmapRegionDecoder::kAndroidCodec_Strategy:
msarett26ad17b2015-10-22 07:29:19 -0700513 folder.append("brd_android_codec");
514 break;
msaretta5783ae2015-09-08 15:35:32 -0700515 default:
516 SkASSERT(false);
517 return;
518 }
519
520 switch (mode) {
521 case BRDSrc::kFullImage_Mode:
522 break;
523 case BRDSrc::kDivisor_Mode:
524 folder.append("_divisor");
525 break;
526 default:
527 SkASSERT(false);
528 return;
529 }
530
531 switch (dstColorType) {
532 case CodecSrc::kGetFromCanvas_DstColorType:
533 break;
534 case CodecSrc::kIndex8_Always_DstColorType:
535 folder.append("_kIndex");
536 break;
537 case CodecSrc::kGrayscale_Always_DstColorType:
538 folder.append("_kGray");
539 break;
540 default:
541 SkASSERT(false);
542 return;
543 }
544
545 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800546 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700547 }
548
549 BRDSrc* src = new BRDSrc(path, strategy, mode, dstColorType, sampleSize);
550 push_src("image", folder, src);
551}
552
553static void push_brd_srcs(Path path) {
554
msarett5cb48852015-11-06 08:56:32 -0800555 const SkBitmapRegionDecoder::Strategy strategies[] = {
556 SkBitmapRegionDecoder::kCanvas_Strategy,
msarett5cb48852015-11-06 08:56:32 -0800557 SkBitmapRegionDecoder::kAndroidCodec_Strategy,
msaretta5783ae2015-09-08 15:35:32 -0700558 };
559
scroggo501b7342015-11-03 07:55:11 -0800560 // Test on a variety of sampleSizes, making sure to include:
561 // - 2, 4, and 8, which are natively supported by jpeg
562 // - multiples of 2 which are not divisible by 4 (analogous for 4)
563 // - larger powers of two, since BRD clients generally use powers of 2
564 // We will only produce output for the larger sizes on large images.
565 const uint32_t sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 24, 32, 64 };
msarett6efbe052015-09-11 09:01:16 -0700566
msaretta5783ae2015-09-08 15:35:32 -0700567 // We will only test to one backend (8888), but we will test all of the
568 // color types that we need to decode to on this backend.
569 const CodecSrc::DstColorType dstColorTypes[] = {
msarett26ad17b2015-10-22 07:29:19 -0700570 CodecSrc::kGetFromCanvas_DstColorType,
571 CodecSrc::kIndex8_Always_DstColorType,
572 CodecSrc::kGrayscale_Always_DstColorType,
msaretta5783ae2015-09-08 15:35:32 -0700573 };
574
575 const BRDSrc::Mode modes[] = {
msarett26ad17b2015-10-22 07:29:19 -0700576 BRDSrc::kFullImage_Mode,
577 BRDSrc::kDivisor_Mode,
msaretta5783ae2015-09-08 15:35:32 -0700578 };
579
msarett5cb48852015-11-06 08:56:32 -0800580 for (SkBitmapRegionDecoder::Strategy strategy : strategies) {
msarett6efbe052015-09-11 09:01:16 -0700581 for (uint32_t sampleSize : sampleSizes) {
msarett6efbe052015-09-11 09:01:16 -0700582 for (CodecSrc::DstColorType dstColorType : dstColorTypes) {
583 if (brd_color_type_supported(strategy, dstColorType)) {
584 for (BRDSrc::Mode mode : modes) {
msaretta5783ae2015-09-08 15:35:32 -0700585 push_brd_src(path, strategy, dstColorType, mode, sampleSize);
586 }
587 }
588 }
589 }
590 }
591}
592
593static bool brd_supported(const char* ext) {
msarett8c8f22a2015-04-01 06:58:48 -0700594 static const char* const exts[] = {
msaretta5783ae2015-09-08 15:35:32 -0700595 "jpg", "jpeg", "png", "webp",
596 "JPG", "JPEG", "PNG", "WEBP",
msarett8c8f22a2015-04-01 06:58:48 -0700597 };
598
599 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
600 if (0 == strcmp(exts[i], ext)) {
601 return true;
602 }
603 }
604 return false;
scroggo9b77ddd2015-03-19 06:03:39 -0700605}
606
scroggo86737142016-02-03 12:19:11 -0800607static bool gather_srcs() {
mtklein748ca3b2015-01-15 10:56:12 -0800608 for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) {
djsollen54416de2015-04-03 07:24:48 -0700609 push_src("gm", "", new GMSrc(r->factory()));
mtklein748ca3b2015-01-15 10:56:12 -0800610 }
halcanaryfc37ad12015-01-30 07:31:19 -0800611 for (int i = 0; i < FLAGS_skps.count(); i++) {
612 const char* path = FLAGS_skps[i];
613 if (sk_isdir(path)) {
614 SkOSFile::Iter it(path, "skp");
615 for (SkString file; it.next(&file); ) {
djsollen54416de2015-04-03 07:24:48 -0700616 push_src("skp", "", new SKPSrc(SkOSPath::Join(path, file.c_str())));
halcanaryfc37ad12015-01-30 07:31:19 -0800617 }
618 } else {
djsollen54416de2015-04-03 07:24:48 -0700619 push_src("skp", "", new SKPSrc(path));
mtklein114c3cd2015-01-15 10:15:02 -0800620 }
621 }
scroggo86737142016-02-03 12:19:11 -0800622
623 SkTArray<SkString> images;
624 if (!CollectImages(&images)) {
625 return false;
626 }
627
628 for (auto image : images) {
629 push_codec_srcs(image);
mtklein21eaf3b2016-02-08 12:39:59 -0800630 const char* ext = strrchr(image.c_str(), '.');
631 if (ext && brd_supported(ext+1)) {
scroggo86737142016-02-03 12:19:11 -0800632 push_brd_srcs(image);
mtklein709d2c32015-01-15 08:30:25 -0800633 }
mtklein709d2c32015-01-15 08:30:25 -0800634 }
scroggo86737142016-02-03 12:19:11 -0800635
636 return true;
mtklein709d2c32015-01-15 08:30:25 -0800637}
638
kkinnunen3e980c32015-12-23 01:33:00 -0800639static void push_sink(const SkCommandLineConfig& config, Sink* s) {
rmistry0f515bd2015-12-22 10:22:26 -0800640 SkAutoTDelete<Sink> sink(s);
kkinnunen3e980c32015-12-23 01:33:00 -0800641
mtkleine0effd62015-07-29 06:37:28 -0700642 // Try a simple Src as a canary. If it fails, skip this sink.
mtklein748ca3b2015-01-15 10:56:12 -0800643 struct : public Src {
mtkleine0effd62015-07-29 06:37:28 -0700644 Error draw(SkCanvas* c) const override {
645 c->drawRect(SkRect::MakeWH(1,1), SkPaint());
646 return "";
647 }
mtklein36352bf2015-03-25 18:17:31 -0700648 SkISize size() const override { return SkISize::Make(16, 16); }
mtkleine0effd62015-07-29 06:37:28 -0700649 Name name() const override { return "justOneRect"; }
650 } justOneRect;
mtklein114c3cd2015-01-15 10:15:02 -0800651
mtklein748ca3b2015-01-15 10:56:12 -0800652 SkBitmap bitmap;
653 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800654 SkString log;
mtkleine0effd62015-07-29 06:37:28 -0700655 Error err = sink->draw(justOneRect, &bitmap, &stream, &log);
mtklein4089ef72015-03-05 08:40:28 -0800656 if (err.isFatal()) {
kkinnunen3e980c32015-12-23 01:33:00 -0800657 SkDebugf("Could not run %s: %s\n", config.getTag().c_str(), err.c_str());
mtklein05641a52015-04-21 10:49:13 -0700658 exit(1);
mtklein114c3cd2015-01-15 10:15:02 -0800659 }
660
mtkleine0effd62015-07-29 06:37:28 -0700661 TaggedSink& ts = gSinks.push_back();
mtklein748ca3b2015-01-15 10:56:12 -0800662 ts.reset(sink.detach());
kkinnunen3e980c32015-12-23 01:33:00 -0800663 ts.tag = config.getTag();
mtklein748ca3b2015-01-15 10:56:12 -0800664}
665
666static bool gpu_supported() {
667#if SK_SUPPORT_GPU
668 return FLAGS_gpu;
669#else
670 return false;
671#endif
672}
kkinnunen9ebc3f02015-12-21 23:48:13 -0800673
kkinnunen3e980c32015-12-23 01:33:00 -0800674static Sink* create_sink(const SkCommandLineConfig* config) {
675#if SK_SUPPORT_GPU
676 if (gpu_supported()) {
677 if (const SkCommandLineConfigGpu* gpuConfig = config->asConfigGpu()) {
678 GrContextFactory::GLContextType contextType = gpuConfig->getContextType();
679 GrContextFactory::GLContextOptions contextOptions =
680 GrContextFactory::kNone_GLContextOptions;
681 if (gpuConfig->getUseNVPR()) {
682 contextOptions = static_cast<GrContextFactory::GLContextOptions>(
683 contextOptions | GrContextFactory::kEnableNVPR_GLContextOptions);
684 }
685 GrContextFactory testFactory;
686 if (!testFactory.get(contextType, contextOptions)) {
687 SkDebugf("WARNING: can not create GPU context for config '%s'. "
688 "GM tests will be skipped.\n", gpuConfig->getTag().c_str());
689 return nullptr;
690 }
691 return new GPUSink(contextType, contextOptions, gpuConfig->getSamples(),
692 gpuConfig->getUseDIText(), FLAGS_gpu_threading);
693 }
694 }
695#endif
696
697#define SINK(t, sink, ...) if (config->getBackend().equals(t)) { return new sink(__VA_ARGS__); }
698
tomhudsoneebc39a2015-02-23 12:18:05 -0800699#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
700 SINK("hwui", HWUISink);
701#endif
702
mtklein748ca3b2015-01-15 10:56:12 -0800703 if (FLAGS_cpu) {
704 SINK("565", RasterSink, kRGB_565_SkColorType);
705 SINK("8888", RasterSink, kN32_SkColorType);
halcanaryc11c62f2015-09-28 11:51:54 -0700706 SINK("pdf", PDFSink, "Pdfium");
707 SINK("pdf_poppler", PDFSink, "Poppler");
mtklein9c3f17d2015-01-28 11:35:18 -0800708 SINK("skp", SKPSink);
mtklein8a4527e2015-01-31 20:00:58 -0800709 SINK("svg", SVGSink);
710 SINK("null", NullSink);
halcanary47ef4d52015-03-03 09:13:09 -0800711 SINK("xps", XPSSink);
mtklein748ca3b2015-01-15 10:56:12 -0800712 }
713#undef SINK
halcanary96fcdcc2015-08-27 07:41:13 -0700714 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800715}
716
kkinnunen3e980c32015-12-23 01:33:00 -0800717static Sink* create_via(const SkString& tag, Sink* wrapped) {
718#define VIA(t, via, ...) if (tag.equals(t)) { return new via(__VA_ARGS__); }
halcanary96fcdcc2015-08-27 07:41:13 -0700719 VIA("twice", ViaTwice, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700720 VIA("serialize", ViaSerialization, wrapped);
mtklein4a34ecb2016-01-08 10:19:35 -0800721 VIA("pic", ViaPicture, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700722 VIA("2ndpic", ViaSecondPicture, wrapped);
mtkleind31c13d2015-05-05 12:59:56 -0700723 VIA("sp", ViaSingletonPictures, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700724 VIA("tiles", ViaTiles, 256, 256, nullptr, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800725 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
mtklein2e2ea382015-10-16 10:29:41 -0700726 VIA("remote", ViaRemote, false, wrapped);
727 VIA("remote_cache", ViaRemote, true, wrapped);
halcanary7a76f9c2016-02-03 11:53:18 -0800728 VIA("mojo", ViaMojo, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800729
mtkleind603b222015-02-17 11:13:33 -0800730 if (FLAGS_matrix.count() == 4) {
mtklein748ca3b2015-01-15 10:56:12 -0800731 SkMatrix m;
mtkleind603b222015-02-17 11:13:33 -0800732 m.reset();
733 m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
734 m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
735 m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
736 m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
737 VIA("matrix", ViaMatrix, m, wrapped);
738 VIA("upright", ViaUpright, m, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800739 }
tomhudson64de1e12015-03-05 08:01:07 -0800740
741#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
742 VIA("androidsdk", ViaAndroidSDK, wrapped);
743#endif
744
mtklein748ca3b2015-01-15 10:56:12 -0800745#undef VIA
halcanary96fcdcc2015-08-27 07:41:13 -0700746 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800747}
748
mtklein748ca3b2015-01-15 10:56:12 -0800749static void gather_sinks() {
kkinnunen3e980c32015-12-23 01:33:00 -0800750 SkCommandLineConfigArray configs;
751 ParseConfigs(FLAGS_config, &configs);
752 for (int i = 0; i < configs.count(); i++) {
753 const SkCommandLineConfig& config = *configs[i];
754 Sink* sink = create_sink(&config);
755 if (sink == nullptr) {
756 SkDebugf("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
757 config.getTag().c_str());
758 continue;
759 }
mtklein748ca3b2015-01-15 10:56:12 -0800760
kkinnunen3e980c32015-12-23 01:33:00 -0800761 const SkTArray<SkString>& parts = config.getViaParts();
762 for (int j = parts.count(); j-- > 0;) {
763 const SkString& part = parts[j];
764 Sink* next = create_via(part, sink);
halcanary96fcdcc2015-08-27 07:41:13 -0700765 if (next == nullptr) {
kkinnunen3e980c32015-12-23 01:33:00 -0800766 SkDebugf("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
767 part.c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800768 delete sink;
halcanary96fcdcc2015-08-27 07:41:13 -0700769 sink = nullptr;
mtklein748ca3b2015-01-15 10:56:12 -0800770 break;
771 }
772 sink = next;
773 }
774 if (sink) {
775 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800776 }
777 }
778}
mtklein709d2c32015-01-15 08:30:25 -0800779
mtkleina5114d72015-08-24 13:27:01 -0700780static bool dump_png(SkBitmap bitmap, const char* path, const char* md5) {
781 const int w = bitmap.width(),
782 h = bitmap.height();
783
784 // First get the bitmap into N32 color format. The next step will work only there.
785 if (bitmap.colorType() != kN32_SkColorType) {
786 SkBitmap n32;
787 if (!bitmap.copyTo(&n32, kN32_SkColorType)) {
788 return false;
789 }
790 bitmap = n32;
791 }
792
793 // Convert our N32 bitmap into unpremul RGBA for libpng.
794 SkAutoTMalloc<uint32_t> rgba(w*h);
795 if (!bitmap.readPixels(SkImageInfo::Make(w,h, kRGBA_8888_SkColorType, kUnpremul_SkAlphaType),
796 rgba, 4*w, 0,0)) {
797 return false;
798 }
799
800 // We don't need bitmap anymore. Might as well drop our ref.
mtkleinfccb77d2015-08-24 14:13:29 -0700801 bitmap.reset();
mtkleina5114d72015-08-24 13:27:01 -0700802
mtkleinc64137c2015-08-25 10:56:08 -0700803 FILE* f = fopen(path, "wb");
mtkleina5114d72015-08-24 13:27:01 -0700804 if (!f) { return false; }
805
806 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
807 if (!png) {
808 fclose(f);
809 return false;
810 }
811
812 png_infop info = png_create_info_struct(png);
813 if (!info) {
814 png_destroy_write_struct(&png, &info);
815 fclose(f);
816 return false;
817 }
818
mtkleind69ece62015-09-10 10:37:44 -0700819 SkString description;
820 description.append("Key: ");
821 for (int i = 0; i < FLAGS_key.count(); i++) {
822 description.appendf("%s ", FLAGS_key[i]);
823 }
824 description.append("Properties: ");
825 for (int i = 0; i < FLAGS_properties.count(); i++) {
826 description.appendf("%s ", FLAGS_properties[i]);
827 }
828 description.appendf("MD5: %s", md5);
829
mtkleina5114d72015-08-24 13:27:01 -0700830 png_text text[2];
831 text[0].key = (png_charp)"Author";
832 text[0].text = (png_charp)"DM dump_png()";
833 text[0].compression = PNG_TEXT_COMPRESSION_NONE;
834 text[1].key = (png_charp)"Description";
mtkleind69ece62015-09-10 10:37:44 -0700835 text[1].text = (png_charp)description.c_str();
mtkleina5114d72015-08-24 13:27:01 -0700836 text[1].compression = PNG_TEXT_COMPRESSION_NONE;
837 png_set_text(png, info, text, 2);
838
839 png_init_io(png, f);
840 png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8,
841 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
842 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
843 png_write_info(png, info);
844 for (int j = 0; j < h; j++) {
845 png_bytep row = (png_bytep)(rgba.get() + w*j);
846 png_write_rows(png, &row, 1);
847 }
848 png_write_end(png, info);
849
850 png_destroy_write_struct(&png, &info);
851 fclose(f);
852 return true;
853}
854
mtkleina2ef6422015-01-15 13:44:22 -0800855static bool match(const char* needle, const char* haystack) {
halcanary96fcdcc2015-08-27 07:41:13 -0700856 return 0 == strcmp("_", needle) || nullptr != strstr(haystack, needle);
mtkleina2ef6422015-01-15 13:44:22 -0800857}
858
djsollen54416de2015-04-03 07:24:48 -0700859static ImplicitString is_blacklisted(const char* sink, const char* src,
860 const char* srcOptions, const char* name) {
mtklein0d243ff2015-04-03 07:51:00 -0700861 for (int i = 0; i < FLAGS_blacklist.count() - 3; i += 4) {
mtkleina2ef6422015-01-15 13:44:22 -0800862 if (match(FLAGS_blacklist[i+0], sink) &&
djsollen54416de2015-04-03 07:24:48 -0700863 match(FLAGS_blacklist[i+1], src) &&
864 match(FLAGS_blacklist[i+2], srcOptions) &&
865 match(FLAGS_blacklist[i+3], name)) {
866 return SkStringPrintf("%s %s %s %s",
867 FLAGS_blacklist[i+0], FLAGS_blacklist[i+1],
868 FLAGS_blacklist[i+2], FLAGS_blacklist[i+3]);
mtkleina2ef6422015-01-15 13:44:22 -0800869 }
870 }
871 return "";
872}
873
mtkleincd50bca2016-01-05 06:20:20 -0800874// Even when a Task Sink reports to be non-threadsafe (e.g. GPU), we know things like
875// .png encoding are definitely thread safe. This lets us offload that work to CPU threads.
876static SkTaskGroup gDefinitelyThreadSafeWork;
877
mtklein748ca3b2015-01-15 10:56:12 -0800878// The finest-grained unit of work we can run: draw a single Src into a single Sink,
879// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
880struct Task {
mtkleine0effd62015-07-29 06:37:28 -0700881 Task(const TaggedSrc& src, const TaggedSink& sink) : src(src), sink(sink) {}
882 const TaggedSrc& src;
883 const TaggedSink& sink;
mtklein748ca3b2015-01-15 10:56:12 -0800884
mtklein21eaf3b2016-02-08 12:39:59 -0800885 static void Run(const Task& task) {
886 SkString name = task.src->name();
mtkleine0effd62015-07-29 06:37:28 -0700887
888 // We'll skip drawing this Src/Sink pair if:
889 // - the Src vetoes the Sink;
890 // - this Src / Sink combination is on the blacklist;
891 // - it's a dry run.
mtklein21eaf3b2016-02-08 12:39:59 -0800892 SkString note(task.src->veto(task.sink->flags()) ? " (veto)" : "");
893 SkString whyBlacklisted = is_blacklisted(task.sink.tag.c_str(), task.src.tag.c_str(),
894 task.src.options.c_str(), name.c_str());
mtkleinb37cb412015-03-18 05:27:14 -0700895 if (!whyBlacklisted.isEmpty()) {
896 note.appendf(" (--blacklist %s)", whyBlacklisted.c_str());
897 }
mtkleine0effd62015-07-29 06:37:28 -0700898
mtkleinb9eb4ac2015-02-02 18:26:03 -0800899 SkString log;
mtkleinf27f08b2015-11-03 06:54:24 -0800900 auto timerStart = now_ms();
mtkleine0effd62015-07-29 06:37:28 -0700901 if (!FLAGS_dryRun && note.isEmpty()) {
mtklein748ca3b2015-01-15 10:56:12 -0800902 SkBitmap bitmap;
903 SkDynamicMemoryWStream stream;
bsalomon821e10e2015-06-04 14:15:33 -0700904 if (FLAGS_pre_log) {
mtklein21eaf3b2016-02-08 12:39:59 -0800905 SkDebugf("\nRunning %s->%s", name.c_str(), task.sink.tag.c_str());
bsalomon821e10e2015-06-04 14:15:33 -0700906 }
mtklein21eaf3b2016-02-08 12:39:59 -0800907 start(task.sink.tag.c_str(), task.src.tag, task.src.options, name.c_str());
908 Error err = task.sink->draw(*task.src, &bitmap, &stream, &log);
mtklein748ca3b2015-01-15 10:56:12 -0800909 if (!err.isEmpty()) {
mtklein4089ef72015-03-05 08:40:28 -0800910 if (err.isFatal()) {
djsollen54416de2015-04-03 07:24:48 -0700911 fail(SkStringPrintf("%s %s %s %s: %s",
mtklein21eaf3b2016-02-08 12:39:59 -0800912 task.sink.tag.c_str(),
913 task.src.tag.c_str(),
914 task.src.options.c_str(),
mtklein4089ef72015-03-05 08:40:28 -0800915 name.c_str(),
916 err.c_str()));
mtkleinb37cb412015-03-18 05:27:14 -0700917 } else {
918 note.appendf(" (skipped: %s)", err.c_str());
mtkleinba6ada72016-01-21 09:39:35 -0800919 auto elapsed = now_ms() - timerStart;
mtklein21eaf3b2016-02-08 12:39:59 -0800920 done(elapsed, task.sink.tag.c_str(), task.src.tag, task.src.options,
mtkleinba6ada72016-01-21 09:39:35 -0800921 name, note, log);
922 return;
mtklein4089ef72015-03-05 08:40:28 -0800923 }
mtklein748ca3b2015-01-15 10:56:12 -0800924 }
mtklein62bd1a62015-01-27 14:46:26 -0800925
mtkleincd50bca2016-01-05 06:20:20 -0800926 // We're likely switching threads here, so we must capture by value, [=] or [foo,bar].
927 SkStreamAsset* data = stream.detachAsStream();
928 gDefinitelyThreadSafeWork.add([task,name,bitmap,data]{
929 SkAutoTDelete<SkStreamAsset> ownedData(data);
930
931 // Why doesn't the copy constructor do this when we have pre-locked pixels?
932 bitmap.lockPixels();
933
934 SkString md5;
935 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
936 SkMD5 hash;
937 if (data->getLength()) {
938 hash.writeStream(data, data->getLength());
939 data->rewind();
mtklein38408462015-07-08 07:25:27 -0700940 } else {
mtkleincd50bca2016-01-05 06:20:20 -0800941 // If we're BGRA (Linux, Windows), swizzle over to RGBA (Mac, Android).
942 // This helps eliminate multiple 0-pixel-diff hashes on gold.skia.org.
943 // (Android's general slow speed breaks the tie arbitrarily in RGBA's favor.)
944 // We might consider promoting 565 to RGBA too.
945 if (bitmap.colorType() == kBGRA_8888_SkColorType) {
946 SkBitmap swizzle;
947 SkAssertResult(bitmap.copyTo(&swizzle, kRGBA_8888_SkColorType));
948 hash.write(swizzle.getPixels(), swizzle.getSize());
949 } else {
950 hash.write(bitmap.getPixels(), bitmap.getSize());
951 }
952 }
953 SkMD5::Digest digest;
954 hash.finish(digest);
955 for (int i = 0; i < 16; i++) {
956 md5.appendf("%02x", digest.data[i]);
mtklein38408462015-07-08 07:25:27 -0700957 }
mtklein62bd1a62015-01-27 14:46:26 -0800958 }
mtklein62bd1a62015-01-27 14:46:26 -0800959
mtkleincd50bca2016-01-05 06:20:20 -0800960 if (!FLAGS_readPath.isEmpty() &&
mtklein21eaf3b2016-02-08 12:39:59 -0800961 !gGold.contains(Gold(task.sink.tag.c_str(), task.src.tag.c_str(),
962 task.src.options.c_str(), name, md5))) {
mtkleincd50bca2016-01-05 06:20:20 -0800963 fail(SkStringPrintf("%s not found for %s %s %s %s in %s",
964 md5.c_str(),
mtklein21eaf3b2016-02-08 12:39:59 -0800965 task.sink.tag.c_str(),
966 task.src.tag.c_str(),
967 task.src.options.c_str(),
mtkleincd50bca2016-01-05 06:20:20 -0800968 name.c_str(),
969 FLAGS_readPath[0]));
mtklein748ca3b2015-01-15 10:56:12 -0800970 }
mtkleincd50bca2016-01-05 06:20:20 -0800971
972 if (!FLAGS_writePath.isEmpty()) {
mtklein21eaf3b2016-02-08 12:39:59 -0800973 const char* ext = task.sink->fileExtension();
mtkleincd50bca2016-01-05 06:20:20 -0800974 if (data->getLength()) {
mtklein21eaf3b2016-02-08 12:39:59 -0800975 WriteToDisk(task, md5, ext, data, data->getLength(), nullptr);
mtkleincd50bca2016-01-05 06:20:20 -0800976 SkASSERT(bitmap.drawsNothing());
977 } else if (!bitmap.drawsNothing()) {
mtklein21eaf3b2016-02-08 12:39:59 -0800978 WriteToDisk(task, md5, ext, nullptr, 0, &bitmap);
mtkleincd50bca2016-01-05 06:20:20 -0800979 }
980 }
981 });
mtklein748ca3b2015-01-15 10:56:12 -0800982 }
mtkleinba6ada72016-01-21 09:39:35 -0800983 auto elapsed = now_ms() - timerStart;
mtklein21eaf3b2016-02-08 12:39:59 -0800984 done(elapsed, task.sink.tag.c_str(), task.src.tag.c_str(), task.src.options.c_str(),
mtkleinf27f08b2015-11-03 06:54:24 -0800985 name, note, log);
mtklein748ca3b2015-01-15 10:56:12 -0800986 }
987
988 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -0800989 SkString md5,
990 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -0800991 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -0800992 const SkBitmap* bitmap) {
mtklein748ca3b2015-01-15 10:56:12 -0800993 JsonWriter::BitmapResult result;
djsollen54416de2015-04-03 07:24:48 -0700994 result.name = task.src->name();
kkinnunen3e980c32015-12-23 01:33:00 -0800995 result.config = task.sink.tag.c_str();
djsollen54416de2015-04-03 07:24:48 -0700996 result.sourceType = task.src.tag;
997 result.sourceOptions = task.src.options;
998 result.ext = ext;
999 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -08001000 JsonWriter::AddBitmapResult(result);
1001
mtkleinb0531a72015-04-07 13:38:48 -07001002 // If an MD5 is uninteresting, we want it noted in the JSON file,
1003 // but don't want to dump it out as a .png (or whatever ext is).
1004 if (gUninterestingHashes.contains(md5)) {
1005 return;
1006 }
1007
mtklein748ca3b2015-01-15 10:56:12 -08001008 const char* dir = FLAGS_writePath[0];
1009 if (0 == strcmp(dir, "@")) { // Needed for iOS.
1010 dir = FLAGS_resourcePath[0];
1011 }
1012 sk_mkdir(dir);
1013
1014 SkString path;
1015 if (FLAGS_nameByHash) {
1016 path = SkOSPath::Join(dir, result.md5.c_str());
1017 path.append(".");
1018 path.append(ext);
1019 if (sk_exists(path.c_str())) {
1020 return; // Content-addressed. If it exists already, we're done.
1021 }
1022 } else {
kkinnunen3e980c32015-12-23 01:33:00 -08001023 path = SkOSPath::Join(dir, task.sink.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001024 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001025 path = SkOSPath::Join(path.c_str(), task.src.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001026 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001027 if (strcmp(task.src.options.c_str(), "") != 0) {
1028 path = SkOSPath::Join(path.c_str(), task.src.options.c_str());
djsollen54416de2015-04-03 07:24:48 -07001029 sk_mkdir(path.c_str());
1030 }
mtklein748ca3b2015-01-15 10:56:12 -08001031 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
1032 path.append(".");
1033 path.append(ext);
1034 }
1035
mtklein748ca3b2015-01-15 10:56:12 -08001036 if (bitmap) {
mtkleina5114d72015-08-24 13:27:01 -07001037 if (!dump_png(*bitmap, path.c_str(), result.md5.c_str())) {
mtklein748ca3b2015-01-15 10:56:12 -08001038 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
1039 return;
1040 }
1041 } else {
mtkleina5114d72015-08-24 13:27:01 -07001042 SkFILEWStream file(path.c_str());
1043 if (!file.isValid()) {
1044 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
1045 return;
1046 }
mtklein748ca3b2015-01-15 10:56:12 -08001047 if (!file.writeStream(data, len)) {
1048 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
1049 return;
1050 }
1051 }
1052 }
1053};
1054
mtklein748ca3b2015-01-15 10:56:12 -08001055/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1056
1057// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
1058
mtklein21eaf3b2016-02-08 12:39:59 -08001059static SkTDArray<skiatest::Test> gParallelTests, gSerialTests;
mtklein748ca3b2015-01-15 10:56:12 -08001060
1061static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -08001062 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -08001063 return;
1064 }
mtklein6393c062015-04-27 08:45:01 -07001065 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r; r = r->next()) {
1066 if (!in_shard()) {
1067 continue;
1068 }
halcanary87f3ba42015-01-20 09:30:20 -08001069 // Despite its name, factory() is returning a reference to
1070 // link-time static const POD data.
1071 const skiatest::Test& test = r->factory();
1072 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -08001073 continue;
1074 }
halcanary87f3ba42015-01-20 09:30:20 -08001075 if (test.needsGpu && gpu_supported()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001076 (FLAGS_gpu_threading ? gParallelTests : gSerialTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -08001077 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein21eaf3b2016-02-08 12:39:59 -08001078 gParallelTests.push(test);
mtklein82d28432015-01-15 12:46:02 -08001079 }
mtklein748ca3b2015-01-15 10:56:12 -08001080 }
1081}
1082
mtklein21eaf3b2016-02-08 12:39:59 -08001083static void run_test(skiatest::Test test) {
halcanary87f3ba42015-01-20 09:30:20 -08001084 struct : public skiatest::Reporter {
mtklein36352bf2015-03-25 18:17:31 -07001085 void reportFailed(const skiatest::Failure& failure) override {
halcanary87f3ba42015-01-20 09:30:20 -08001086 fail(failure.toString());
1087 JsonWriter::AddTestFailure(failure);
1088 }
mtklein36352bf2015-03-25 18:17:31 -07001089 bool allowExtendedTest() const override {
halcanary87f3ba42015-01-20 09:30:20 -08001090 return FLAGS_pathOpsExtended;
1091 }
mtklein36352bf2015-03-25 18:17:31 -07001092 bool verbose() const override { return FLAGS_veryVerbose; }
halcanary87f3ba42015-01-20 09:30:20 -08001093 } reporter;
djsollen824996a2015-06-12 12:06:22 -07001094
1095 SkString note;
mtklein21eaf3b2016-02-08 12:39:59 -08001096 SkString whyBlacklisted = is_blacklisted("_", "tests", "_", test.name);
djsollen824996a2015-06-12 12:06:22 -07001097 if (!whyBlacklisted.isEmpty()) {
1098 note.appendf(" (--blacklist %s)", whyBlacklisted.c_str());
1099 }
1100
mtkleinf27f08b2015-11-03 06:54:24 -08001101 auto timerStart = now_ms();
djsollen824996a2015-06-12 12:06:22 -07001102 if (!FLAGS_dryRun && whyBlacklisted.isEmpty()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001103 start("unit", "test", "", test.name);
mtklein55e88b22015-01-21 15:50:13 -08001104 GrContextFactory factory;
bsalomon821e10e2015-06-04 14:15:33 -07001105 if (FLAGS_pre_log) {
mtklein21eaf3b2016-02-08 12:39:59 -08001106 SkDebugf("\nRunning test %s", test.name);
bsalomon821e10e2015-06-04 14:15:33 -07001107 }
mtklein21eaf3b2016-02-08 12:39:59 -08001108 test.proc(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -08001109 }
mtklein21eaf3b2016-02-08 12:39:59 -08001110 done(now_ms()-timerStart, "unit", "test", "", test.name, note, "");
mtklein748ca3b2015-01-15 10:56:12 -08001111}
1112
1113/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1114
mtkleinde6fc2b2015-03-12 06:28:54 -07001115// Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung.
1116// This prints something every once in a while so that it knows we're still working.
mtklein2e1c47e2015-03-12 07:16:56 -07001117static void start_keepalive() {
1118 struct Loop {
1119 static void forever(void*) {
1120 for (;;) {
1121 static const int kSec = 300;
1122 #if defined(SK_BUILD_FOR_WIN)
1123 Sleep(kSec * 1000);
1124 #else
1125 sleep(kSec);
1126 #endif
mtkleinb37cb412015-03-18 05:27:14 -07001127 SkString running;
1128 {
mtklein246ba3a2016-02-23 10:39:36 -08001129 SkAutoTAcquire<SkPODSpinlock> lock(gRunningAndTallyMutex);
mtkleinb37cb412015-03-18 05:27:14 -07001130 for (int i = 0; i < gRunning.count(); i++) {
1131 running.appendf("\n\t%s", gRunning[i].c_str());
1132 }
1133 }
1134 SkDebugf("\nCurrently running:%s\n", running.c_str());
mtklein2e1c47e2015-03-12 07:16:56 -07001135 }
1136 }
1137 };
1138 static SkThread* intentionallyLeaked = new SkThread(Loop::forever);
1139 intentionallyLeaked->start();
mtkleinde6fc2b2015-03-12 06:28:54 -07001140}
1141
caryclark83ca6282015-06-10 09:31:09 -07001142#define PORTABLE_FONT_PREFIX "Toy Liberation "
1143
1144static SkTypeface* create_from_name(const char familyName[], SkTypeface::Style style) {
1145 if (familyName && strlen(familyName) > sizeof(PORTABLE_FONT_PREFIX)
1146 && !strncmp(familyName, PORTABLE_FONT_PREFIX, sizeof(PORTABLE_FONT_PREFIX) - 1)) {
caryclark1818acb2015-07-24 12:09:25 -07001147 return sk_tool_utils::create_portable_typeface(familyName, style);
caryclark83ca6282015-06-10 09:31:09 -07001148 }
halcanary96fcdcc2015-08-27 07:41:13 -07001149 return nullptr;
caryclark83ca6282015-06-10 09:31:09 -07001150}
1151
1152#undef PORTABLE_FONT_PREFIX
1153
1154extern SkTypeface* (*gCreateTypefaceDelegate)(const char [], SkTypeface::Style );
1155
jcgregorio3b27ade2014-11-13 08:06:40 -08001156int dm_main();
caryclark17f0b6d2014-07-22 10:15:34 -07001157int dm_main() {
mtklein246ba3a2016-02-23 10:39:36 -08001158 setup_crash_handler();
mtklein5286f022016-01-22 08:18:14 -08001159 JsonWriter::DumpJson(); // It's handy for the bots to assume this is ~never missing.
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +00001160 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -07001161 SkTaskGroup::Enabler enabled(FLAGS_threads);
caryclark83ca6282015-06-10 09:31:09 -07001162 gCreateTypefaceDelegate = &create_from_name;
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +00001163
mtklein2e1c47e2015-03-12 07:16:56 -07001164 start_keepalive();
mtkleinde6fc2b2015-03-12 06:28:54 -07001165
mtklein62bd1a62015-01-27 14:46:26 -08001166 gather_gold();
borenet09ed4802015-04-03 14:15:33 -07001167 gather_uninteresting_hashes();
mtklein62bd1a62015-01-27 14:46:26 -08001168
scroggo86737142016-02-03 12:19:11 -08001169 if (!gather_srcs()) {
1170 return 1;
1171 }
mtklein748ca3b2015-01-15 10:56:12 -08001172 gather_sinks();
1173 gather_tests();
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +00001174
mtklein21eaf3b2016-02-08 12:39:59 -08001175 gPending = gSrcs.count() * gSinks.count() + gParallelTests.count() + gSerialTests.count();
mtklein748ca3b2015-01-15 10:56:12 -08001176 SkDebugf("%d srcs * %d sinks + %d tests == %d tasks\n",
mtklein21eaf3b2016-02-08 12:39:59 -08001177 gSrcs.count(), gSinks.count(), gParallelTests.count() + gSerialTests.count(), gPending);
mtklein748ca3b2015-01-15 10:56:12 -08001178
mtklein21eaf3b2016-02-08 12:39:59 -08001179 // Kick off as much parallel work as we can, making note of any serial work we'll need to do.
1180 SkTaskGroup parallel;
1181 SkTArray<Task> serial;
1182
1183 for (auto& sink : gSinks)
1184 for (auto& src : gSrcs) {
1185 Task task(src, sink);
1186 if (src->serial() || sink->serial()) {
1187 serial.push_back(task);
1188 } else {
1189 parallel.add([task] { Task::Run(task); });
mtklein748ca3b2015-01-15 10:56:12 -08001190 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001191 }
mtklein21eaf3b2016-02-08 12:39:59 -08001192 for (auto test : gParallelTests) {
1193 parallel.add([test] { run_test(test); });
mtklein55e88b22015-01-21 15:50:13 -08001194 }
mtklein21eaf3b2016-02-08 12:39:59 -08001195
1196 // With the parallel work running, run serial tasks and tests here on main thread.
1197 for (auto task : serial) { Task::Run(task); }
1198 for (auto test : gSerialTests) { run_test(test); }
1199
1200 // Wait for any remaining parallel work to complete (including any spun off of serial tasks).
1201 parallel.wait();
mtkleincd50bca2016-01-05 06:20:20 -08001202 gDefinitelyThreadSafeWork.wait();
1203
mtklein748ca3b2015-01-15 10:56:12 -08001204 // At this point we're back in single-threaded land.
caryclarkf53ce802015-06-15 06:48:30 -07001205 sk_tool_utils::release_portable_typefaces();
mtklein@google.comd36522d2013-10-16 13:02:15 +00001206
mtkleinbc4e0032015-12-09 08:37:35 -08001207 if (FLAGS_verbose && gNoteTally.count() > 0) {
1208 SkDebugf("\nNote tally:\n");
1209 gNoteTally.foreach([](const SkString& note, int* tally) {
1210 SkDebugf("%dx\t%s\n", *tally, note.c_str());
1211 });
1212 }
1213
mtklein2f64eec2015-01-15 14:20:41 -08001214 SkDebugf("\n");
mtklein748ca3b2015-01-15 10:56:12 -08001215 if (gFailures.count() > 0) {
1216 SkDebugf("Failures:\n");
1217 for (int i = 0; i < gFailures.count(); i++) {
mtklein9dc09102015-01-15 15:47:33 -08001218 SkDebugf("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001219 }
1220 SkDebugf("%d failures\n", gFailures.count());
1221 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001222 }
mtklein748ca3b2015-01-15 10:56:12 -08001223 if (gPending > 0) {
1224 SkDebugf("Hrm, we didn't seem to run everything we intended to! Please file a bug.\n");
1225 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001226 }
halcanary7a14b312015-10-01 07:28:13 -07001227 #ifdef SK_PDF_IMAGE_STATS
1228 SkPDFImageDumpStats();
1229 #endif // SK_PDF_IMAGE_STATS
scroggo4d9eaea2016-01-29 07:48:33 -08001230 SkDebugf("Finished!\n");
mtklein748ca3b2015-01-15 10:56:12 -08001231 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +00001232}
jcgregorio3b27ade2014-11-13 08:06:40 -08001233
kkinnunen179a8f52015-11-20 13:32:24 -08001234// TODO: currently many GPU tests are declared outside SK_SUPPORT_GPU guards.
1235// Thus we export the empty RunWithGPUTestContexts when SK_SUPPORT_GPU=0.
1236namespace skiatest {
1237namespace {
1238typedef void(*TestWithGrContext)(skiatest::Reporter*, GrContext*);
1239typedef void(*TestWithGrContextAndGLContext)(skiatest::Reporter*, GrContext*, SkGLContext*);
1240#if SK_SUPPORT_GPU
1241template<typename T>
kkinnunen34058002016-01-06 23:49:30 -08001242void call_test(T test, skiatest::Reporter* reporter, const GrContextFactory::ContextInfo& context);
kkinnunen179a8f52015-11-20 13:32:24 -08001243template<>
1244void call_test(TestWithGrContext test, skiatest::Reporter* reporter,
kkinnunen34058002016-01-06 23:49:30 -08001245 const GrContextFactory::ContextInfo& context) {
1246 test(reporter, context.fGrContext);
kkinnunen179a8f52015-11-20 13:32:24 -08001247}
1248template<>
1249void call_test(TestWithGrContextAndGLContext test, skiatest::Reporter* reporter,
kkinnunen34058002016-01-06 23:49:30 -08001250 const GrContextFactory::ContextInfo& context) {
1251 test(reporter, context.fGrContext, context.fGLContext);
kkinnunen179a8f52015-11-20 13:32:24 -08001252}
1253#endif
1254} // namespace
1255
kkinnunen179a8f52015-11-20 13:32:24 -08001256template<typename T>
1257void RunWithGPUTestContexts(T test, GPUTestContexts testContexts, Reporter* reporter,
1258 GrContextFactory* factory) {
1259#if SK_SUPPORT_GPU
kkinnunen3e980c32015-12-23 01:33:00 -08001260 // Iterate over context types, except use "native" instead of explicitly trying OpenGL and
1261 // OpenGL ES. Do not use GLES on desktop, since tests do not account for not fixing
1262 // http://skbug.com/2809
1263 GrContextFactory::GLContextType contextTypes[] = {
1264 GrContextFactory::kNative_GLContextType,
1265#if SK_ANGLE
1266#ifdef SK_BUILD_FOR_WIN
1267 GrContextFactory::kANGLE_GLContextType,
1268#endif
1269 GrContextFactory::kANGLE_GL_GLContextType,
1270#endif
1271#if SK_COMMAND_BUFFER
1272 GrContextFactory::kCommandBuffer_GLContextType,
1273#endif
1274#if SK_MESA
1275 GrContextFactory::kMESA_GLContextType,
1276#endif
1277 GrContextFactory::kNull_GLContextType,
1278 GrContextFactory::kDebug_GLContextType,
1279 };
1280 static_assert(SK_ARRAY_COUNT(contextTypes) == GrContextFactory::kGLContextTypeCnt - 2,
1281 "Skipping unexpected GLContextType for GPU tests");
1282
1283 for (auto& contextType : contextTypes) {
kkinnunen179a8f52015-11-20 13:32:24 -08001284 int contextSelector = kNone_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001285 if (GrContextFactory::IsRenderingGLContext(contextType)) {
kkinnunen179a8f52015-11-20 13:32:24 -08001286 contextSelector |= kAllRendering_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001287 } else if (contextType == GrContextFactory::kNative_GLContextType) {
kkinnunen179a8f52015-11-20 13:32:24 -08001288 contextSelector |= kNative_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001289 } else if (contextType == GrContextFactory::kNull_GLContextType) {
kkinnunen179a8f52015-11-20 13:32:24 -08001290 contextSelector |= kNull_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001291 } else if (contextType == GrContextFactory::kDebug_GLContextType) {
kkinnunen55eeae92015-12-10 23:19:29 -08001292 contextSelector |= kDebug_GPUTestContexts;
kkinnunen179a8f52015-11-20 13:32:24 -08001293 }
1294 if ((testContexts & contextSelector) == 0) {
1295 continue;
1296 }
kkinnunen34058002016-01-06 23:49:30 -08001297 GrContextFactory::ContextInfo context = factory->getContextInfo(contextType);
1298 if (context.fGrContext) {
kkinnunen5219fd92015-12-10 06:28:13 -08001299 call_test(test, reporter, context);
1300 }
kkinnunen34058002016-01-06 23:49:30 -08001301 context = factory->getContextInfo(contextType,
1302 GrContextFactory::kEnableNVPR_GLContextOptions);
1303 if (context.fGrContext) {
kkinnunen179a8f52015-11-20 13:32:24 -08001304 call_test(test, reporter, context);
1305 }
1306 }
1307#endif
1308}
1309
1310template
1311void RunWithGPUTestContexts<TestWithGrContext>(TestWithGrContext test,
1312 GPUTestContexts testContexts,
1313 Reporter* reporter,
1314 GrContextFactory* factory);
1315template
1316void RunWithGPUTestContexts<TestWithGrContextAndGLContext>(TestWithGrContextAndGLContext test,
1317 GPUTestContexts testContexts,
1318 Reporter* reporter,
1319 GrContextFactory* factory);
1320} // namespace skiatest
1321
borenet48087572015-04-02 12:16:36 -07001322#if !defined(SK_BUILD_FOR_IOS)
jcgregorio3b27ade2014-11-13 08:06:40 -08001323int main(int argc, char** argv) {
1324 SkCommandLineFlags::Parse(argc, argv);
1325 return dm_main();
1326}
1327#endif