blob: 27231032a3bedec4dadfd5e097a5700a3d4b9493 [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"
msarett7f691442015-09-22 11:56:16 -070017#include "SkCodecTools.h"
caryclark17f0b6d2014-07-22 10:15:34 -070018#include "SkCommonFlags.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"
mtkleina82f5622015-02-20 12:30:19 -080025#include "SkTHash.h"
mtklein406654b2014-09-03 15:34:37 -070026#include "SkTaskGroup.h"
mtkleinde6fc2b2015-03-12 06:28:54 -070027#include "SkThreadUtils.h"
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +000028#include "Test.h"
mtklein748ca3b2015-01-15 10:56:12 -080029#include "Timer.h"
caryclark83ca6282015-06-10 09:31:09 -070030#include "sk_tool_utils.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +000031
halcanary7a14b312015-10-01 07:28:13 -070032#ifdef SK_PDF_IMAGE_STATS
33extern void SkPDFImageDumpStats();
34#endif
35
mtkleina5114d72015-08-24 13:27:01 -070036#ifdef SKIA_PNG_PREFIXED
37 // this must proceed png.h
38 #include "pngprefix.h"
39#endif
40#include "png.h"
41
bungeman60e0fee2015-08-26 05:15:46 -070042#include <stdlib.h>
43
scroggo27a58342015-10-28 08:56:41 -070044#ifndef SK_BUILD_FOR_WIN32
45 #include <unistd.h>
46#endif
47
djsollen54416de2015-04-03 07:24:48 -070048DEFINE_string(src, "tests gm skp image", "Source types to test.");
mtklein748ca3b2015-01-15 10:56:12 -080049DEFINE_bool(nameByHash, false,
50 "If true, write to FLAGS_writePath[0]/<hash>.png instead of "
djsollen54416de2015-04-03 07:24:48 -070051 "to FLAGS_writePath[0]/<config>/<sourceType>/<sourceOptions>/<name>.png");
mtklein748ca3b2015-01-15 10:56:12 -080052DEFINE_bool2(pathOpsExtended, x, false, "Run extended pathOps tests.");
mtkleind603b222015-02-17 11:13:33 -080053DEFINE_string(matrix, "1 0 0 1",
54 "2x2 scale+skew matrix to apply or upright when using "
55 "'matrix' or 'upright' in config.");
mtklein82d28432015-01-15 12:46:02 -080056DEFINE_bool(gpu_threading, false, "Allow GPU work to run on multiple threads?");
mtklein@google.comd36522d2013-10-16 13:02:15 +000057
mtkleina2ef6422015-01-15 13:44:22 -080058DEFINE_string(blacklist, "",
djsollen54416de2015-04-03 07:24:48 -070059 "Space-separated config/src/srcOptions/name quadruples to blacklist. '_' matches anything. E.g. \n"
60 "'--blacklist gpu skp _ _' will blacklist all SKPs drawn into the gpu config.\n"
61 "'--blacklist gpu skp _ _ 8888 gm _ aarects' will also blacklist the aarects GM on 8888.");
mtkleina2ef6422015-01-15 13:44:22 -080062
mtklein62bd1a62015-01-27 14:46:26 -080063DEFINE_string2(readPath, r, "", "If set check for equality with golden results in this directory.");
64
borenet09ed4802015-04-03 14:15:33 -070065DEFINE_string(uninterestingHashesFile, "",
66 "File containing a list of uninteresting hashes. If a result hashes to something in "
67 "this list, no image is written for that result.");
68
mtklein6393c062015-04-27 08:45:01 -070069DEFINE_int32(shards, 1, "We're splitting source data into this many shards.");
70DEFINE_int32(shard, 0, "Which shard do I run?");
bsalomon821e10e2015-06-04 14:15:33 -070071DEFINE_bool2(pre_log, p, false, "Log before running each test. May be incomprehensible when threading");
mtklein6393c062015-04-27 08:45:01 -070072
mtklein@google.comd36522d2013-10-16 13:02:15 +000073__SK_FORCE_IMAGE_DECODER_LINKING;
mtklein748ca3b2015-01-15 10:56:12 -080074using namespace DM;
mtklein@google.comd36522d2013-10-16 13:02:15 +000075
mtklein748ca3b2015-01-15 10:56:12 -080076/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
77
78SK_DECLARE_STATIC_MUTEX(gFailuresMutex);
79static SkTArray<SkString> gFailures;
80
81static void fail(ImplicitString err) {
82 SkAutoMutexAcquire lock(gFailuresMutex);
83 SkDebugf("\n\nFAILURE: %s\n\n", err.c_str());
84 gFailures.push_back(err);
halcanary9e398f72015-01-10 11:18:04 -080085}
86
mtkleinb37cb412015-03-18 05:27:14 -070087static int32_t gPending = 0; // Atomic. Total number of running and queued tasks.
88
89SK_DECLARE_STATIC_MUTEX(gRunningMutex);
90static SkTArray<SkString> gRunning;
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 {
98 SkAutoMutexAcquire lock(gRunningMutex);
99 for (int i = 0; i < gRunning.count(); i++) {
100 if (gRunning[i] == id) {
101 gRunning.removeShuffle(i);
102 break;
103 }
104 }
105 }
106 if (!FLAGS_verbose) {
107 note = "";
108 }
mtkleinb9eb4ac2015-02-02 18:26:03 -0800109 if (!log.isEmpty()) {
110 log.prepend("\n");
111 }
mtklein6dee2ad2015-02-05 09:53:44 -0800112 auto pending = sk_atomic_dec(&gPending)-1;
reed50bc0512015-05-19 14:13:31 -0700113 if (!FLAGS_quiet) {
mtklein711a2452015-07-08 08:49:20 -0700114 SkDebugf("%s(%4d/%-4dMB %6d) %s\t%s%s%s", FLAGS_verbose ? "\n" : kSkOverwriteLine
reed50bc0512015-05-19 14:13:31 -0700115 , sk_tools::getCurrResidentSetSizeMB()
116 , sk_tools::getMaxResidentSetSizeMB()
117 , pending
118 , HumanizeMs(ms).c_str()
119 , id.c_str()
120 , note.c_str()
121 , log.c_str());
122 }
mtkleina17241b2015-01-23 05:48:00 -0800123 // We write our dm.json file every once in a while in case we crash.
124 // Notice this also handles the final dm.json when pending == 0.
125 if (pending % 500 == 0) {
126 JsonWriter::DumpJson();
127 }
mtklein@google.comd36522d2013-10-16 13:02:15 +0000128}
129
djsollen54416de2015-04-03 07:24:48 -0700130static void start(ImplicitString config, ImplicitString src,
131 ImplicitString srcOptions, ImplicitString name) {
132 SkString id = SkStringPrintf("%s %s %s %s", config.c_str(), src.c_str(),
133 srcOptions.c_str(), name.c_str());
mtkleinb37cb412015-03-18 05:27:14 -0700134 SkAutoMutexAcquire lock(gRunningMutex);
135 gRunning.push_back(id);
136}
137
mtklein748ca3b2015-01-15 10:56:12 -0800138/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
mtklein114c3cd2015-01-15 10:15:02 -0800139
mtklein62bd1a62015-01-27 14:46:26 -0800140struct Gold : public SkString {
mtkleina82f5622015-02-20 12:30:19 -0800141 Gold() : SkString("") {}
djsollen54416de2015-04-03 07:24:48 -0700142 Gold(ImplicitString sink, ImplicitString src, ImplicitString srcOptions,
143 ImplicitString name, ImplicitString md5)
mtklein62bd1a62015-01-27 14:46:26 -0800144 : SkString("") {
145 this->append(sink);
146 this->append(src);
djsollen54416de2015-04-03 07:24:48 -0700147 this->append(srcOptions);
mtklein62bd1a62015-01-27 14:46:26 -0800148 this->append(name);
149 this->append(md5);
mtklein62bd1a62015-01-27 14:46:26 -0800150 }
mtkleinc8d1dd42015-10-15 12:23:01 -0700151 struct Hash {
152 uint32_t operator()(const Gold& g) const {
153 return SkGoodHash()((const SkString&)g);
154 }
155 };
mtklein62bd1a62015-01-27 14:46:26 -0800156};
mtkleina82f5622015-02-20 12:30:19 -0800157static SkTHashSet<Gold, Gold::Hash> gGold;
mtklein62bd1a62015-01-27 14:46:26 -0800158
159static void add_gold(JsonWriter::BitmapResult r) {
djsollen54416de2015-04-03 07:24:48 -0700160 gGold.add(Gold(r.config, r.sourceType, r.sourceOptions, r.name, r.md5));
mtklein62bd1a62015-01-27 14:46:26 -0800161}
162
163static void gather_gold() {
164 if (!FLAGS_readPath.isEmpty()) {
165 SkString path(FLAGS_readPath[0]);
166 path.append("/dm.json");
167 if (!JsonWriter::ReadJson(path.c_str(), add_gold)) {
168 fail(SkStringPrintf("Couldn't read %s for golden results.", path.c_str()));
169 }
170 }
171}
172
173/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
174
borenet09ed4802015-04-03 14:15:33 -0700175static SkTHashSet<SkString> gUninterestingHashes;
176
177static void gather_uninteresting_hashes() {
178 if (!FLAGS_uninterestingHashesFile.isEmpty()) {
179 SkAutoTUnref<SkData> data(SkData::NewFromFileName(FLAGS_uninterestingHashesFile[0]));
mtkleincc334b32015-09-22 11:43:53 -0700180 if (!data) {
181 SkDebugf("WARNING: unable to read uninteresting hashes from %s\n",
182 FLAGS_uninterestingHashesFile[0]);
183 return;
184 }
borenet09ed4802015-04-03 14:15:33 -0700185 SkTArray<SkString> hashes;
186 SkStrSplit((const char*)data->data(), "\n", &hashes);
187 for (const SkString& hash : hashes) {
188 gUninterestingHashes.add(hash);
189 }
190 }
191}
192
193/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
194
mtkleine0effd62015-07-29 06:37:28 -0700195struct TaggedSrc : public SkAutoTDelete<Src> {
msarett9e707a02015-09-01 14:57:57 -0700196 ImplicitString tag;
197 ImplicitString options;
mtkleine0effd62015-07-29 06:37:28 -0700198};
199
200struct TaggedSink : public SkAutoTDelete<Sink> {
201 const char* tag;
djsollen54416de2015-04-03 07:24:48 -0700202};
mtklein748ca3b2015-01-15 10:56:12 -0800203
204static const bool kMemcpyOK = true;
205
mtkleine0effd62015-07-29 06:37:28 -0700206static SkTArray<TaggedSrc, kMemcpyOK> gSrcs;
207static SkTArray<TaggedSink, kMemcpyOK> gSinks;
mtklein748ca3b2015-01-15 10:56:12 -0800208
mtklein6393c062015-04-27 08:45:01 -0700209static bool in_shard() {
210 static int N = 0;
211 return N++ % FLAGS_shards == FLAGS_shard;
212}
213
msarett9e707a02015-09-01 14:57:57 -0700214static void push_src(ImplicitString tag, ImplicitString options, Src* s) {
mtklein748ca3b2015-01-15 10:56:12 -0800215 SkAutoTDelete<Src> src(s);
mtklein6393c062015-04-27 08:45:01 -0700216 if (in_shard() &&
msarett9e707a02015-09-01 14:57:57 -0700217 FLAGS_src.contains(tag.c_str()) &&
mtklein748ca3b2015-01-15 10:56:12 -0800218 !SkCommandLineFlags::ShouldSkip(FLAGS_match, src->name().c_str())) {
mtkleine0effd62015-07-29 06:37:28 -0700219 TaggedSrc& s = gSrcs.push_back();
mtklein748ca3b2015-01-15 10:56:12 -0800220 s.reset(src.detach());
221 s.tag = tag;
djsollen54416de2015-04-03 07:24:48 -0700222 s.options = options;
mtklein114c3cd2015-01-15 10:15:02 -0800223 }
mtklein748ca3b2015-01-15 10:56:12 -0800224}
mtklein114c3cd2015-01-15 10:15:02 -0800225
msarett9e707a02015-09-01 14:57:57 -0700226static void push_codec_src(Path path, CodecSrc::Mode mode, CodecSrc::DstColorType dstColorType,
227 float scale) {
228 SkString folder;
229 switch (mode) {
230 case CodecSrc::kCodec_Mode:
231 folder.append("codec");
232 break;
msarett9e707a02015-09-01 14:57:57 -0700233 case CodecSrc::kScanline_Mode:
234 folder.append("scanline");
235 break;
236 case CodecSrc::kScanline_Subset_Mode:
237 folder.append("scanline_subset");
238 break;
239 case CodecSrc::kStripe_Mode:
240 folder.append("stripe");
241 break;
242 case CodecSrc::kSubset_Mode:
msarett36c37962015-09-02 13:20:52 -0700243 folder.append("codec_subset");
msarett9e707a02015-09-01 14:57:57 -0700244 break;
245 }
246
247 switch (dstColorType) {
248 case CodecSrc::kGrayscale_Always_DstColorType:
249 folder.append("_kGray8");
250 break;
251 case CodecSrc::kIndex8_Always_DstColorType:
252 folder.append("_kIndex8");
253 break;
254 default:
255 break;
256 }
257
258 if (1.0f != scale) {
259 folder.appendf("_%.3f", scale);
260 }
261
262 CodecSrc* src = new CodecSrc(path, mode, dstColorType, scale);
263 push_src("image", folder, src);
264}
265
msarett3d9d7a72015-10-21 10:27:10 -0700266static void push_android_codec_src(Path path, AndroidCodecSrc::Mode mode,
267 CodecSrc::DstColorType dstColorType, int sampleSize) {
268 SkString folder;
269 switch (mode) {
270 case AndroidCodecSrc::kFullImage_Mode:
271 folder.append("scaled_codec");
272 break;
273 case AndroidCodecSrc::kDivisor_Mode:
274 folder.append("scaled_codec_divisor");
275 break;
276 }
277
278 switch (dstColorType) {
279 case CodecSrc::kGrayscale_Always_DstColorType:
280 folder.append("_kGray8");
281 break;
282 case CodecSrc::kIndex8_Always_DstColorType:
283 folder.append("_kIndex8");
284 break;
285 default:
286 break;
287 }
288
289 if (1 != sampleSize) {
290 folder.appendf("_%.3f", get_scale_from_sample_size(sampleSize));
291 }
292
293 AndroidCodecSrc* src = new AndroidCodecSrc(path, mode, dstColorType, sampleSize);
294 push_src("image", folder, src);
295}
296
msarett438b2ad2015-04-09 12:43:10 -0700297static void push_codec_srcs(Path path) {
298 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
299 if (!encoded) {
300 SkDebugf("Couldn't read %s.", path.c_str());
301 return;
302 }
303 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
halcanary96fcdcc2015-08-27 07:41:13 -0700304 if (nullptr == codec.get()) {
msarett438b2ad2015-04-09 12:43:10 -0700305 SkDebugf("Couldn't create codec for %s.", path.c_str());
306 return;
307 }
308
msarett36c37962015-09-02 13:20:52 -0700309 // Native Scales
msarett0a242972015-06-11 14:27:27 -0700310 // TODO (msarett): Implement scaling tests for SkImageDecoder in order to compare with these
311 // tests. SkImageDecoder supports downscales by integer factors.
emmaleer8f4ba762015-08-14 07:44:46 -0700312 // 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 -0700313 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 -0700314
msarett36c37962015-09-02 13:20:52 -0700315 const CodecSrc::Mode nativeModes[] = { CodecSrc::kCodec_Mode, CodecSrc::kScanline_Mode,
316 CodecSrc::kScanline_Subset_Mode, CodecSrc::kStripe_Mode, CodecSrc::kSubset_Mode };
msarett9e707a02015-09-01 14:57:57 -0700317
msarett36c37962015-09-02 13:20:52 -0700318 CodecSrc::DstColorType colorTypes[3];
319 uint32_t numColorTypes;
320 switch (codec->getInfo().colorType()) {
321 case kGray_8_SkColorType:
322 // FIXME: Is this a long term solution for testing wbmps decodes to kIndex8?
msarett3d9d7a72015-10-21 10:27:10 -0700323 // Further discussion on this topic is at skbug.com/3683.
324 // This causes us to try to convert grayscale jpegs to kIndex8. We currently
325 // fail non-fatally in this case.
msarett36c37962015-09-02 13:20:52 -0700326 colorTypes[0] = CodecSrc::kGetFromCanvas_DstColorType;
327 colorTypes[1] = CodecSrc::kGrayscale_Always_DstColorType;
328 colorTypes[2] = CodecSrc::kIndex8_Always_DstColorType;
329 numColorTypes = 3;
330 break;
331 case kIndex_8_SkColorType:
332 colorTypes[0] = CodecSrc::kGetFromCanvas_DstColorType;
333 colorTypes[1] = CodecSrc::kIndex8_Always_DstColorType;
334 numColorTypes = 2;
335 break;
336 default:
337 colorTypes[0] = CodecSrc::kGetFromCanvas_DstColorType;
338 numColorTypes = 1;
339 break;
340 }
msarett9e707a02015-09-01 14:57:57 -0700341
msarett36c37962015-09-02 13:20:52 -0700342 for (float scale : nativeScales) {
msarett36c37962015-09-02 13:20:52 -0700343 for (CodecSrc::Mode mode : nativeModes) {
344 for (uint32_t i = 0; i < numColorTypes; i++) {
345 push_codec_src(path, mode, colorTypes[i], scale);
msarett9e707a02015-09-01 14:57:57 -0700346 }
347 }
msarett0a242972015-06-11 14:27:27 -0700348 }
msarett36c37962015-09-02 13:20:52 -0700349
msarett3d9d7a72015-10-21 10:27:10 -0700350 // skbug.com/4428
351 static const char* const exts[] = {
352 "jpg", "jpeg", "png", "webp",
353 "JPG", "JPEG", "PNG", "WEBP",
354 };
355 bool supported = false;
356 for (const char* ext : exts) {
357 if (path.endsWith(ext)) {
358 supported = true;
359 break;
360 }
361 }
362 if (!supported) {
scroggo46c57472015-09-30 08:57:13 -0700363 return;
364 }
365
msarett3d9d7a72015-10-21 10:27:10 -0700366 const int sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
msarett36c37962015-09-02 13:20:52 -0700367
msarett3d9d7a72015-10-21 10:27:10 -0700368 const AndroidCodecSrc::Mode androidModes[] = {
369 AndroidCodecSrc::kFullImage_Mode,
370 AndroidCodecSrc::kDivisor_Mode,
371 };
372
373 for (int sampleSize : sampleSizes) {
374 for (AndroidCodecSrc::Mode mode : androidModes) {
375 for (uint32_t i = 0; i < numColorTypes; i++) {
376 push_android_codec_src(path, mode, colorTypes[i], sampleSize);
377 }
msarett36c37962015-09-02 13:20:52 -0700378 }
379 }
msarett438b2ad2015-04-09 12:43:10 -0700380}
381
msaretta5783ae2015-09-08 15:35:32 -0700382static bool brd_color_type_supported(SkBitmapRegionDecoderInterface::Strategy strategy,
383 CodecSrc::DstColorType dstColorType) {
384 switch (strategy) {
385 case SkBitmapRegionDecoderInterface::kCanvas_Strategy:
386 if (CodecSrc::kGetFromCanvas_DstColorType == dstColorType) {
387 return true;
388 }
389 return false;
390 case SkBitmapRegionDecoderInterface::kOriginal_Strategy:
391 switch (dstColorType) {
392 case CodecSrc::kGetFromCanvas_DstColorType:
393 case CodecSrc::kIndex8_Always_DstColorType:
394 case CodecSrc::kGrayscale_Always_DstColorType:
395 return true;
396 default:
397 return false;
398 }
msarett26ad17b2015-10-22 07:29:19 -0700399 case SkBitmapRegionDecoderInterface::kAndroidCodec_Strategy:
400 switch (dstColorType) {
401 case CodecSrc::kGetFromCanvas_DstColorType:
402 case CodecSrc::kIndex8_Always_DstColorType:
403 case CodecSrc::kGrayscale_Always_DstColorType:
404 return true;
405 default:
406 return false;
407 }
msaretta5783ae2015-09-08 15:35:32 -0700408 default:
409 SkASSERT(false);
410 return false;
411 }
412}
413
414static void push_brd_src(Path path, SkBitmapRegionDecoderInterface::Strategy strategy,
415 CodecSrc::DstColorType dstColorType, BRDSrc::Mode mode, uint32_t sampleSize) {
416 SkString folder;
417 switch (strategy) {
418 case SkBitmapRegionDecoderInterface::kCanvas_Strategy:
419 folder.append("brd_canvas");
420 break;
421 case SkBitmapRegionDecoderInterface::kOriginal_Strategy:
422 folder.append("brd_sample");
423 break;
msarett26ad17b2015-10-22 07:29:19 -0700424 case SkBitmapRegionDecoderInterface::kAndroidCodec_Strategy:
425 folder.append("brd_android_codec");
426 break;
msaretta5783ae2015-09-08 15:35:32 -0700427 default:
428 SkASSERT(false);
429 return;
430 }
431
432 switch (mode) {
433 case BRDSrc::kFullImage_Mode:
434 break;
435 case BRDSrc::kDivisor_Mode:
436 folder.append("_divisor");
437 break;
438 default:
439 SkASSERT(false);
440 return;
441 }
442
443 switch (dstColorType) {
444 case CodecSrc::kGetFromCanvas_DstColorType:
445 break;
446 case CodecSrc::kIndex8_Always_DstColorType:
447 folder.append("_kIndex");
448 break;
449 case CodecSrc::kGrayscale_Always_DstColorType:
450 folder.append("_kGray");
451 break;
452 default:
453 SkASSERT(false);
454 return;
455 }
456
457 if (1 != sampleSize) {
msarett7f691442015-09-22 11:56:16 -0700458 folder.appendf("_%.3f", get_scale_from_sample_size(sampleSize));
msaretta5783ae2015-09-08 15:35:32 -0700459 }
460
461 BRDSrc* src = new BRDSrc(path, strategy, mode, dstColorType, sampleSize);
462 push_src("image", folder, src);
463}
464
465static void push_brd_srcs(Path path) {
466
467 const SkBitmapRegionDecoderInterface::Strategy strategies[] = {
468 SkBitmapRegionDecoderInterface::kCanvas_Strategy,
msarett26ad17b2015-10-22 07:29:19 -0700469 SkBitmapRegionDecoderInterface::kOriginal_Strategy,
470 SkBitmapRegionDecoderInterface::kAndroidCodec_Strategy,
msaretta5783ae2015-09-08 15:35:32 -0700471 };
472
msarett6efbe052015-09-11 09:01:16 -0700473 const uint32_t sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
474
msaretta5783ae2015-09-08 15:35:32 -0700475 // We will only test to one backend (8888), but we will test all of the
476 // color types that we need to decode to on this backend.
477 const CodecSrc::DstColorType dstColorTypes[] = {
msarett26ad17b2015-10-22 07:29:19 -0700478 CodecSrc::kGetFromCanvas_DstColorType,
479 CodecSrc::kIndex8_Always_DstColorType,
480 CodecSrc::kGrayscale_Always_DstColorType,
msaretta5783ae2015-09-08 15:35:32 -0700481 };
482
483 const BRDSrc::Mode modes[] = {
msarett26ad17b2015-10-22 07:29:19 -0700484 BRDSrc::kFullImage_Mode,
485 BRDSrc::kDivisor_Mode,
msaretta5783ae2015-09-08 15:35:32 -0700486 };
487
msaretta5783ae2015-09-08 15:35:32 -0700488 for (SkBitmapRegionDecoderInterface::Strategy strategy : strategies) {
msarett6efbe052015-09-11 09:01:16 -0700489
msaretta5783ae2015-09-08 15:35:32 -0700490 // We disable png testing for kOriginal_Strategy because the implementation leaks
491 // memory in our forked libpng.
492 // TODO (msarett): Decide if we want to test pngs in this mode and how we might do this.
493 if (SkBitmapRegionDecoderInterface::kOriginal_Strategy == strategy &&
494 (path.endsWith(".png") || path.endsWith(".PNG"))) {
495 continue;
496 }
msarett6efbe052015-09-11 09:01:16 -0700497 for (uint32_t sampleSize : sampleSizes) {
498
499 // kOriginal_Strategy does not work for jpegs that are scaled to non-powers of two.
500 // We don't need to test this. We know it doesn't work, and it causes images with
501 // uninitialized memory to show up on Gold.
502 if (SkBitmapRegionDecoderInterface::kOriginal_Strategy == strategy &&
503 (path.endsWith(".jpg") || path.endsWith(".JPG") ||
504 path.endsWith(".jpeg") || path.endsWith(".JPEG")) && !SkIsPow2(sampleSize)) {
505 continue;
506 }
507 for (CodecSrc::DstColorType dstColorType : dstColorTypes) {
508 if (brd_color_type_supported(strategy, dstColorType)) {
509 for (BRDSrc::Mode mode : modes) {
msaretta5783ae2015-09-08 15:35:32 -0700510 push_brd_src(path, strategy, dstColorType, mode, sampleSize);
511 }
512 }
513 }
514 }
515 }
516}
517
518static bool brd_supported(const char* ext) {
msarett8c8f22a2015-04-01 06:58:48 -0700519 static const char* const exts[] = {
msaretta5783ae2015-09-08 15:35:32 -0700520 "jpg", "jpeg", "png", "webp",
521 "JPG", "JPEG", "PNG", "WEBP",
msarett8c8f22a2015-04-01 06:58:48 -0700522 };
523
524 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
525 if (0 == strcmp(exts[i], ext)) {
526 return true;
527 }
528 }
529 return false;
scroggo9b77ddd2015-03-19 06:03:39 -0700530}
531
mtklein748ca3b2015-01-15 10:56:12 -0800532static void gather_srcs() {
533 for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) {
djsollen54416de2015-04-03 07:24:48 -0700534 push_src("gm", "", new GMSrc(r->factory()));
mtklein748ca3b2015-01-15 10:56:12 -0800535 }
halcanaryfc37ad12015-01-30 07:31:19 -0800536 for (int i = 0; i < FLAGS_skps.count(); i++) {
537 const char* path = FLAGS_skps[i];
538 if (sk_isdir(path)) {
539 SkOSFile::Iter it(path, "skp");
540 for (SkString file; it.next(&file); ) {
djsollen54416de2015-04-03 07:24:48 -0700541 push_src("skp", "", new SKPSrc(SkOSPath::Join(path, file.c_str())));
halcanaryfc37ad12015-01-30 07:31:19 -0800542 }
543 } else {
djsollen54416de2015-04-03 07:24:48 -0700544 push_src("skp", "", new SKPSrc(path));
mtklein114c3cd2015-01-15 10:15:02 -0800545 }
546 }
halcanary23b03c32015-01-30 09:58:58 -0800547 static const char* const exts[] = {
548 "bmp", "gif", "jpg", "jpeg", "png", "webp", "ktx", "astc", "wbmp", "ico",
549 "BMP", "GIF", "JPG", "JPEG", "PNG", "WEBP", "KTX", "ASTC", "WBMP", "ICO",
550 };
551 for (int i = 0; i < FLAGS_images.count(); i++) {
552 const char* flag = FLAGS_images[i];
553 if (sk_isdir(flag)) {
554 for (size_t j = 0; j < SK_ARRAY_COUNT(exts); j++) {
555 SkOSFile::Iter it(flag, exts[j]);
556 for (SkString file; it.next(&file); ) {
557 SkString path = SkOSPath::Join(flag, file.c_str());
djsollen54416de2015-04-03 07:24:48 -0700558 push_src("image", "decode", new ImageSrc(path)); // Decode entire image
559 push_src("image", "subset", new ImageSrc(path, 2)); // Decode into 2x2 subsets
msaretta5783ae2015-09-08 15:35:32 -0700560 push_codec_srcs(path);
561 if (brd_supported(exts[j])) {
562 push_brd_srcs(path);
scroggo9b77ddd2015-03-19 06:03:39 -0700563 }
halcanary23b03c32015-01-30 09:58:58 -0800564 }
mtklein114c3cd2015-01-15 10:15:02 -0800565 }
halcanary23b03c32015-01-30 09:58:58 -0800566 } else if (sk_exists(flag)) {
567 // assume that FLAGS_images[i] is a valid image if it is a file.
djsollen54416de2015-04-03 07:24:48 -0700568 push_src("image", "decode", new ImageSrc(flag)); // Decode entire image.
569 push_src("image", "subset", new ImageSrc(flag, 2)); // Decode into 2 x 2 subsets
msarett438b2ad2015-04-09 12:43:10 -0700570 push_codec_srcs(flag);
msaretta5783ae2015-09-08 15:35:32 -0700571 push_brd_srcs(flag);
mtklein709d2c32015-01-15 08:30:25 -0800572 }
mtklein709d2c32015-01-15 08:30:25 -0800573 }
574}
575
mtklein748ca3b2015-01-15 10:56:12 -0800576static GrGLStandard get_gpu_api() {
577 if (FLAGS_gpuAPI.contains("gl")) { return kGL_GrGLStandard; }
578 if (FLAGS_gpuAPI.contains("gles")) { return kGLES_GrGLStandard; }
579 return kNone_GrGLStandard;
mtklein709d2c32015-01-15 08:30:25 -0800580}
581
mtklein748ca3b2015-01-15 10:56:12 -0800582static void push_sink(const char* tag, Sink* s) {
583 SkAutoTDelete<Sink> sink(s);
584 if (!FLAGS_config.contains(tag)) {
585 return;
mtklein114c3cd2015-01-15 10:15:02 -0800586 }
mtkleine0effd62015-07-29 06:37:28 -0700587 // Try a simple Src as a canary. If it fails, skip this sink.
mtklein748ca3b2015-01-15 10:56:12 -0800588 struct : public Src {
mtkleine0effd62015-07-29 06:37:28 -0700589 Error draw(SkCanvas* c) const override {
590 c->drawRect(SkRect::MakeWH(1,1), SkPaint());
591 return "";
592 }
mtklein36352bf2015-03-25 18:17:31 -0700593 SkISize size() const override { return SkISize::Make(16, 16); }
mtkleine0effd62015-07-29 06:37:28 -0700594 Name name() const override { return "justOneRect"; }
595 } justOneRect;
mtklein114c3cd2015-01-15 10:15:02 -0800596
mtklein748ca3b2015-01-15 10:56:12 -0800597 SkBitmap bitmap;
598 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800599 SkString log;
mtkleine0effd62015-07-29 06:37:28 -0700600 Error err = sink->draw(justOneRect, &bitmap, &stream, &log);
mtklein4089ef72015-03-05 08:40:28 -0800601 if (err.isFatal()) {
mtklein05641a52015-04-21 10:49:13 -0700602 SkDebugf("Could not run %s: %s\n", tag, err.c_str());
603 exit(1);
mtklein114c3cd2015-01-15 10:15:02 -0800604 }
605
mtkleine0effd62015-07-29 06:37:28 -0700606 TaggedSink& ts = gSinks.push_back();
mtklein748ca3b2015-01-15 10:56:12 -0800607 ts.reset(sink.detach());
608 ts.tag = tag;
609}
610
611static bool gpu_supported() {
612#if SK_SUPPORT_GPU
613 return FLAGS_gpu;
614#else
615 return false;
616#endif
617}
618
619static Sink* create_sink(const char* tag) {
620#define SINK(t, sink, ...) if (0 == strcmp(t, tag)) { return new sink(__VA_ARGS__); }
621 if (gpu_supported()) {
mtklein82d28432015-01-15 12:46:02 -0800622 typedef GrContextFactory Gr;
mtklein748ca3b2015-01-15 10:56:12 -0800623 const GrGLStandard api = get_gpu_api();
hendrikw885bf092015-08-27 10:38:39 -0700624 SINK("gpunull", GPUSink, Gr::kNull_GLContextType, api, 0, false, FLAGS_gpu_threading);
625 SINK("gpudebug", GPUSink, Gr::kDebug_GLContextType, api, 0, false, FLAGS_gpu_threading);
626 SINK("gpu", GPUSink, Gr::kNative_GLContextType, api, 0, false, FLAGS_gpu_threading);
627 SINK("gpudft", GPUSink, Gr::kNative_GLContextType, api, 0, true, FLAGS_gpu_threading);
628 SINK("msaa4", GPUSink, Gr::kNative_GLContextType, api, 4, false, FLAGS_gpu_threading);
629 SINK("msaa16", GPUSink, Gr::kNative_GLContextType, api, 16, false, FLAGS_gpu_threading);
bsalomon90e8ab72015-09-08 10:26:51 -0700630 SINK("nvprmsaa4", GPUSink, Gr::kNVPR_GLContextType, api, 4, true, FLAGS_gpu_threading);
631 SINK("nvprmsaa16", GPUSink, Gr::kNVPR_GLContextType, api, 16, true, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800632 #if SK_ANGLE
hendrikw885bf092015-08-27 10:38:39 -0700633 SINK("angle", GPUSink, Gr::kANGLE_GLContextType, api, 0, false, FLAGS_gpu_threading);
hendrikweddbefb2015-09-11 13:07:29 -0700634 SINK("angle-gl", GPUSink, Gr::kANGLE_GL_GLContextType, api, 0, false, FLAGS_gpu_threading);
hendrikw885bf092015-08-27 10:38:39 -0700635 #endif
636 #if SK_COMMAND_BUFFER
637 SINK("commandbuffer", GPUSink, Gr::kCommandBuffer_GLContextType, api, 0, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800638 #endif
639 #if SK_MESA
hendrikw885bf092015-08-27 10:38:39 -0700640 SINK("mesa", GPUSink, Gr::kMESA_GLContextType, api, 0, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800641 #endif
mtklein114c3cd2015-01-15 10:15:02 -0800642 }
mtklein748ca3b2015-01-15 10:56:12 -0800643
tomhudsoneebc39a2015-02-23 12:18:05 -0800644#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
645 SINK("hwui", HWUISink);
646#endif
647
mtklein748ca3b2015-01-15 10:56:12 -0800648 if (FLAGS_cpu) {
649 SINK("565", RasterSink, kRGB_565_SkColorType);
650 SINK("8888", RasterSink, kN32_SkColorType);
halcanaryc11c62f2015-09-28 11:51:54 -0700651 SINK("pdf", PDFSink, "Pdfium");
652 SINK("pdf_poppler", PDFSink, "Poppler");
mtklein9c3f17d2015-01-28 11:35:18 -0800653 SINK("skp", SKPSink);
mtklein8a4527e2015-01-31 20:00:58 -0800654 SINK("svg", SVGSink);
655 SINK("null", NullSink);
halcanary47ef4d52015-03-03 09:13:09 -0800656 SINK("xps", XPSSink);
mtklein748ca3b2015-01-15 10:56:12 -0800657 }
658#undef SINK
halcanary96fcdcc2015-08-27 07:41:13 -0700659 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800660}
661
mtklein748ca3b2015-01-15 10:56:12 -0800662static Sink* create_via(const char* tag, Sink* wrapped) {
663#define VIA(t, via, ...) if (0 == strcmp(t, tag)) { return new via(__VA_ARGS__); }
halcanary96fcdcc2015-08-27 07:41:13 -0700664 VIA("twice", ViaTwice, wrapped);
665 VIA("pipe", ViaPipe, wrapped);
666 VIA("serialize", ViaSerialization, wrapped);
667 VIA("2ndpic", ViaSecondPicture, wrapped);
mtkleind31c13d2015-05-05 12:59:56 -0700668 VIA("sp", ViaSingletonPictures, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700669 VIA("tiles", ViaTiles, 256, 256, nullptr, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800670 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
mtklein2e2ea382015-10-16 10:29:41 -0700671 VIA("remote", ViaRemote, false, wrapped);
672 VIA("remote_cache", ViaRemote, true, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800673
mtkleind603b222015-02-17 11:13:33 -0800674 if (FLAGS_matrix.count() == 4) {
mtklein748ca3b2015-01-15 10:56:12 -0800675 SkMatrix m;
mtkleind603b222015-02-17 11:13:33 -0800676 m.reset();
677 m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
678 m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
679 m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
680 m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
681 VIA("matrix", ViaMatrix, m, wrapped);
682 VIA("upright", ViaUpright, m, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800683 }
tomhudson64de1e12015-03-05 08:01:07 -0800684
685#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
686 VIA("androidsdk", ViaAndroidSDK, wrapped);
687#endif
688
mtklein748ca3b2015-01-15 10:56:12 -0800689#undef VIA
halcanary96fcdcc2015-08-27 07:41:13 -0700690 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800691}
692
mtklein748ca3b2015-01-15 10:56:12 -0800693static void gather_sinks() {
694 for (int i = 0; i < FLAGS_config.count(); i++) {
695 const char* config = FLAGS_config[i];
696 SkTArray<SkString> parts;
697 SkStrSplit(config, "-", &parts);
698
halcanary96fcdcc2015-08-27 07:41:13 -0700699 Sink* sink = nullptr;
mtklein748ca3b2015-01-15 10:56:12 -0800700 for (int i = parts.count(); i-- > 0;) {
701 const char* part = parts[i].c_str();
halcanary96fcdcc2015-08-27 07:41:13 -0700702 Sink* next = (sink == nullptr) ? create_sink(part) : create_via(part, sink);
703 if (next == nullptr) {
mtklein748ca3b2015-01-15 10:56:12 -0800704 SkDebugf("Skipping %s: Don't understand '%s'.\n", config, part);
705 delete sink;
halcanary96fcdcc2015-08-27 07:41:13 -0700706 sink = nullptr;
mtklein748ca3b2015-01-15 10:56:12 -0800707 break;
708 }
709 sink = next;
710 }
711 if (sink) {
712 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800713 }
714 }
715}
mtklein709d2c32015-01-15 08:30:25 -0800716
mtkleina5114d72015-08-24 13:27:01 -0700717static bool dump_png(SkBitmap bitmap, const char* path, const char* md5) {
718 const int w = bitmap.width(),
719 h = bitmap.height();
720
721 // First get the bitmap into N32 color format. The next step will work only there.
722 if (bitmap.colorType() != kN32_SkColorType) {
723 SkBitmap n32;
724 if (!bitmap.copyTo(&n32, kN32_SkColorType)) {
725 return false;
726 }
727 bitmap = n32;
728 }
729
730 // Convert our N32 bitmap into unpremul RGBA for libpng.
731 SkAutoTMalloc<uint32_t> rgba(w*h);
732 if (!bitmap.readPixels(SkImageInfo::Make(w,h, kRGBA_8888_SkColorType, kUnpremul_SkAlphaType),
733 rgba, 4*w, 0,0)) {
734 return false;
735 }
736
737 // We don't need bitmap anymore. Might as well drop our ref.
mtkleinfccb77d2015-08-24 14:13:29 -0700738 bitmap.reset();
mtkleina5114d72015-08-24 13:27:01 -0700739
mtkleinc64137c2015-08-25 10:56:08 -0700740 FILE* f = fopen(path, "wb");
mtkleina5114d72015-08-24 13:27:01 -0700741 if (!f) { return false; }
742
743 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
744 if (!png) {
745 fclose(f);
746 return false;
747 }
748
749 png_infop info = png_create_info_struct(png);
750 if (!info) {
751 png_destroy_write_struct(&png, &info);
752 fclose(f);
753 return false;
754 }
755
mtkleind69ece62015-09-10 10:37:44 -0700756 SkString description;
757 description.append("Key: ");
758 for (int i = 0; i < FLAGS_key.count(); i++) {
759 description.appendf("%s ", FLAGS_key[i]);
760 }
761 description.append("Properties: ");
762 for (int i = 0; i < FLAGS_properties.count(); i++) {
763 description.appendf("%s ", FLAGS_properties[i]);
764 }
765 description.appendf("MD5: %s", md5);
766
mtkleina5114d72015-08-24 13:27:01 -0700767 png_text text[2];
768 text[0].key = (png_charp)"Author";
769 text[0].text = (png_charp)"DM dump_png()";
770 text[0].compression = PNG_TEXT_COMPRESSION_NONE;
771 text[1].key = (png_charp)"Description";
mtkleind69ece62015-09-10 10:37:44 -0700772 text[1].text = (png_charp)description.c_str();
mtkleina5114d72015-08-24 13:27:01 -0700773 text[1].compression = PNG_TEXT_COMPRESSION_NONE;
774 png_set_text(png, info, text, 2);
775
776 png_init_io(png, f);
777 png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8,
778 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
779 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
780 png_write_info(png, info);
781 for (int j = 0; j < h; j++) {
782 png_bytep row = (png_bytep)(rgba.get() + w*j);
783 png_write_rows(png, &row, 1);
784 }
785 png_write_end(png, info);
786
787 png_destroy_write_struct(&png, &info);
788 fclose(f);
789 return true;
790}
791
mtkleina2ef6422015-01-15 13:44:22 -0800792static bool match(const char* needle, const char* haystack) {
halcanary96fcdcc2015-08-27 07:41:13 -0700793 return 0 == strcmp("_", needle) || nullptr != strstr(haystack, needle);
mtkleina2ef6422015-01-15 13:44:22 -0800794}
795
djsollen54416de2015-04-03 07:24:48 -0700796static ImplicitString is_blacklisted(const char* sink, const char* src,
797 const char* srcOptions, const char* name) {
mtklein0d243ff2015-04-03 07:51:00 -0700798 for (int i = 0; i < FLAGS_blacklist.count() - 3; i += 4) {
mtkleina2ef6422015-01-15 13:44:22 -0800799 if (match(FLAGS_blacklist[i+0], sink) &&
djsollen54416de2015-04-03 07:24:48 -0700800 match(FLAGS_blacklist[i+1], src) &&
801 match(FLAGS_blacklist[i+2], srcOptions) &&
802 match(FLAGS_blacklist[i+3], name)) {
803 return SkStringPrintf("%s %s %s %s",
804 FLAGS_blacklist[i+0], FLAGS_blacklist[i+1],
805 FLAGS_blacklist[i+2], FLAGS_blacklist[i+3]);
mtkleina2ef6422015-01-15 13:44:22 -0800806 }
807 }
808 return "";
809}
810
mtklein748ca3b2015-01-15 10:56:12 -0800811// The finest-grained unit of work we can run: draw a single Src into a single Sink,
812// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
813struct Task {
mtkleine0effd62015-07-29 06:37:28 -0700814 Task(const TaggedSrc& src, const TaggedSink& sink) : src(src), sink(sink) {}
815 const TaggedSrc& src;
816 const TaggedSink& sink;
mtklein748ca3b2015-01-15 10:56:12 -0800817
818 static void Run(Task* task) {
mtkleina2ef6422015-01-15 13:44:22 -0800819 SkString name = task->src->name();
mtkleine0effd62015-07-29 06:37:28 -0700820
821 // We'll skip drawing this Src/Sink pair if:
822 // - the Src vetoes the Sink;
823 // - this Src / Sink combination is on the blacklist;
824 // - it's a dry run.
mtklein99cab4e2015-07-31 06:43:04 -0700825 SkString note(task->src->veto(task->sink->flags()) ? " (veto)" : "");
msarett9e707a02015-09-01 14:57:57 -0700826 SkString whyBlacklisted = is_blacklisted(task->sink.tag, task->src.tag.c_str(),
827 task->src.options.c_str(), name.c_str());
mtkleinb37cb412015-03-18 05:27:14 -0700828 if (!whyBlacklisted.isEmpty()) {
829 note.appendf(" (--blacklist %s)", whyBlacklisted.c_str());
830 }
mtkleine0effd62015-07-29 06:37:28 -0700831
mtkleinb9eb4ac2015-02-02 18:26:03 -0800832 SkString log;
mtklein748ca3b2015-01-15 10:56:12 -0800833 WallTimer timer;
834 timer.start();
mtkleine0effd62015-07-29 06:37:28 -0700835 if (!FLAGS_dryRun && note.isEmpty()) {
mtklein748ca3b2015-01-15 10:56:12 -0800836 SkBitmap bitmap;
837 SkDynamicMemoryWStream stream;
bsalomon821e10e2015-06-04 14:15:33 -0700838 if (FLAGS_pre_log) {
839 SkDebugf("\nRunning %s->%s", name.c_str(), task->sink.tag);
840 }
djsollen54416de2015-04-03 07:24:48 -0700841 start(task->sink.tag, task->src.tag, task->src.options, name.c_str());
mtkleinb9eb4ac2015-02-02 18:26:03 -0800842 Error err = task->sink->draw(*task->src, &bitmap, &stream, &log);
mtklein748ca3b2015-01-15 10:56:12 -0800843 if (!err.isEmpty()) {
mtklein4089ef72015-03-05 08:40:28 -0800844 timer.end();
845 if (err.isFatal()) {
djsollen54416de2015-04-03 07:24:48 -0700846 fail(SkStringPrintf("%s %s %s %s: %s",
mtklein4089ef72015-03-05 08:40:28 -0800847 task->sink.tag,
msarett9e707a02015-09-01 14:57:57 -0700848 task->src.tag.c_str(),
849 task->src.options.c_str(),
mtklein4089ef72015-03-05 08:40:28 -0800850 name.c_str(),
851 err.c_str()));
mtkleinb37cb412015-03-18 05:27:14 -0700852 } else {
853 note.appendf(" (skipped: %s)", err.c_str());
mtklein4089ef72015-03-05 08:40:28 -0800854 }
djsollen54416de2015-04-03 07:24:48 -0700855 done(timer.fWall, task->sink.tag, task->src.tag, task->src.options,
856 name, note, log);
mtklein4089ef72015-03-05 08:40:28 -0800857 return;
mtklein748ca3b2015-01-15 10:56:12 -0800858 }
mtklein62bd1a62015-01-27 14:46:26 -0800859 SkAutoTDelete<SkStreamAsset> data(stream.detachAsStream());
860
861 SkString md5;
862 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
863 SkMD5 hash;
864 if (data->getLength()) {
865 hash.writeStream(data, data->getLength());
866 data->rewind();
867 } else {
mtklein38408462015-07-08 07:25:27 -0700868 // If we're BGRA (Linux, Windows), swizzle over to RGBA (Mac, Android).
869 // This helps eliminate multiple 0-pixel-diff hashes on gold.skia.org.
870 // (Android's general slow speed breaks the tie arbitrarily in RGBA's favor.)
871 // We might consider promoting 565 to RGBA too.
872 if (bitmap.colorType() == kBGRA_8888_SkColorType) {
873 SkBitmap swizzle;
874 SkAssertResult(bitmap.copyTo(&swizzle, kRGBA_8888_SkColorType));
875 hash.write(swizzle.getPixels(), swizzle.getSize());
876 } else {
877 hash.write(bitmap.getPixels(), bitmap.getSize());
878 }
mtklein62bd1a62015-01-27 14:46:26 -0800879 }
880 SkMD5::Digest digest;
881 hash.finish(digest);
882 for (int i = 0; i < 16; i++) {
883 md5.appendf("%02x", digest.data[i]);
884 }
885 }
886
887 if (!FLAGS_readPath.isEmpty() &&
msarett9e707a02015-09-01 14:57:57 -0700888 !gGold.contains(Gold(task->sink.tag, task->src.tag.c_str(),
889 task->src.options.c_str(), name, md5))) {
djsollen54416de2015-04-03 07:24:48 -0700890 fail(SkStringPrintf("%s not found for %s %s %s %s in %s",
mtklein62bd1a62015-01-27 14:46:26 -0800891 md5.c_str(),
892 task->sink.tag,
msarett9e707a02015-09-01 14:57:57 -0700893 task->src.tag.c_str(),
894 task->src.options.c_str(),
mtklein62bd1a62015-01-27 14:46:26 -0800895 name.c_str(),
896 FLAGS_readPath[0]));
897 }
898
mtkleinb0531a72015-04-07 13:38:48 -0700899 if (!FLAGS_writePath.isEmpty()) {
mtklein748ca3b2015-01-15 10:56:12 -0800900 const char* ext = task->sink->fileExtension();
mtklein62bd1a62015-01-27 14:46:26 -0800901 if (data->getLength()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700902 WriteToDisk(*task, md5, ext, data, data->getLength(), nullptr);
halcanary022afb82015-01-30 11:00:12 -0800903 SkASSERT(bitmap.drawsNothing());
904 } else if (!bitmap.drawsNothing()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700905 WriteToDisk(*task, md5, ext, nullptr, 0, &bitmap);
mtklein748ca3b2015-01-15 10:56:12 -0800906 }
907 }
908 }
909 timer.end();
msarett9e707a02015-09-01 14:57:57 -0700910 done(timer.fWall, task->sink.tag, task->src.tag.c_str(), task->src.options.c_str(), name,
911 note, log);
mtklein748ca3b2015-01-15 10:56:12 -0800912 }
913
914 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -0800915 SkString md5,
916 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -0800917 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -0800918 const SkBitmap* bitmap) {
mtklein748ca3b2015-01-15 10:56:12 -0800919 JsonWriter::BitmapResult result;
djsollen54416de2015-04-03 07:24:48 -0700920 result.name = task.src->name();
921 result.config = task.sink.tag;
922 result.sourceType = task.src.tag;
923 result.sourceOptions = task.src.options;
924 result.ext = ext;
925 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -0800926 JsonWriter::AddBitmapResult(result);
927
mtkleinb0531a72015-04-07 13:38:48 -0700928 // If an MD5 is uninteresting, we want it noted in the JSON file,
929 // but don't want to dump it out as a .png (or whatever ext is).
930 if (gUninterestingHashes.contains(md5)) {
931 return;
932 }
933
mtklein748ca3b2015-01-15 10:56:12 -0800934 const char* dir = FLAGS_writePath[0];
935 if (0 == strcmp(dir, "@")) { // Needed for iOS.
936 dir = FLAGS_resourcePath[0];
937 }
938 sk_mkdir(dir);
939
940 SkString path;
941 if (FLAGS_nameByHash) {
942 path = SkOSPath::Join(dir, result.md5.c_str());
943 path.append(".");
944 path.append(ext);
945 if (sk_exists(path.c_str())) {
946 return; // Content-addressed. If it exists already, we're done.
947 }
948 } else {
949 path = SkOSPath::Join(dir, task.sink.tag);
950 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -0700951 path = SkOSPath::Join(path.c_str(), task.src.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800952 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -0700953 if (strcmp(task.src.options.c_str(), "") != 0) {
954 path = SkOSPath::Join(path.c_str(), task.src.options.c_str());
djsollen54416de2015-04-03 07:24:48 -0700955 sk_mkdir(path.c_str());
956 }
mtklein748ca3b2015-01-15 10:56:12 -0800957 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
958 path.append(".");
959 path.append(ext);
960 }
961
mtklein748ca3b2015-01-15 10:56:12 -0800962 if (bitmap) {
mtkleina5114d72015-08-24 13:27:01 -0700963 if (!dump_png(*bitmap, path.c_str(), result.md5.c_str())) {
mtklein748ca3b2015-01-15 10:56:12 -0800964 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
965 return;
966 }
967 } else {
mtkleina5114d72015-08-24 13:27:01 -0700968 SkFILEWStream file(path.c_str());
969 if (!file.isValid()) {
970 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
971 return;
972 }
mtklein748ca3b2015-01-15 10:56:12 -0800973 if (!file.writeStream(data, len)) {
974 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
975 return;
976 }
977 }
978 }
979};
980
981// Run all tasks in the same enclave serially on the same thread.
982// They can't possibly run concurrently with each other.
983static void run_enclave(SkTArray<Task>* tasks) {
984 for (int i = 0; i < tasks->count(); i++) {
985 Task::Run(tasks->begin() + i);
986 }
987}
988
989/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
990
991// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
992
mtklein55e88b22015-01-21 15:50:13 -0800993static SkTDArray<skiatest::Test> gThreadedTests, gGPUTests;
mtklein748ca3b2015-01-15 10:56:12 -0800994
995static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -0800996 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -0800997 return;
998 }
mtklein6393c062015-04-27 08:45:01 -0700999 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r; r = r->next()) {
1000 if (!in_shard()) {
1001 continue;
1002 }
halcanary87f3ba42015-01-20 09:30:20 -08001003 // Despite its name, factory() is returning a reference to
1004 // link-time static const POD data.
1005 const skiatest::Test& test = r->factory();
1006 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -08001007 continue;
1008 }
halcanary87f3ba42015-01-20 09:30:20 -08001009 if (test.needsGpu && gpu_supported()) {
mtklein55e88b22015-01-21 15:50:13 -08001010 (FLAGS_gpu_threading ? gThreadedTests : gGPUTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -08001011 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein55e88b22015-01-21 15:50:13 -08001012 gThreadedTests.push(test);
mtklein82d28432015-01-15 12:46:02 -08001013 }
mtklein748ca3b2015-01-15 10:56:12 -08001014 }
1015}
1016
halcanary87f3ba42015-01-20 09:30:20 -08001017static void run_test(skiatest::Test* test) {
1018 struct : public skiatest::Reporter {
mtklein36352bf2015-03-25 18:17:31 -07001019 void reportFailed(const skiatest::Failure& failure) override {
halcanary87f3ba42015-01-20 09:30:20 -08001020 fail(failure.toString());
1021 JsonWriter::AddTestFailure(failure);
1022 }
mtklein36352bf2015-03-25 18:17:31 -07001023 bool allowExtendedTest() const override {
halcanary87f3ba42015-01-20 09:30:20 -08001024 return FLAGS_pathOpsExtended;
1025 }
mtklein36352bf2015-03-25 18:17:31 -07001026 bool verbose() const override { return FLAGS_veryVerbose; }
halcanary87f3ba42015-01-20 09:30:20 -08001027 } reporter;
djsollen824996a2015-06-12 12:06:22 -07001028
1029 SkString note;
1030 SkString whyBlacklisted = is_blacklisted("_", "tests", "_", test->name);
1031 if (!whyBlacklisted.isEmpty()) {
1032 note.appendf(" (--blacklist %s)", whyBlacklisted.c_str());
1033 }
1034
mtklein748ca3b2015-01-15 10:56:12 -08001035 WallTimer timer;
1036 timer.start();
djsollen824996a2015-06-12 12:06:22 -07001037 if (!FLAGS_dryRun && whyBlacklisted.isEmpty()) {
djsollen54416de2015-04-03 07:24:48 -07001038 start("unit", "test", "", test->name);
mtklein55e88b22015-01-21 15:50:13 -08001039 GrContextFactory factory;
bsalomon821e10e2015-06-04 14:15:33 -07001040 if (FLAGS_pre_log) {
1041 SkDebugf("\nRunning test %s", test->name);
1042 }
mtklein55e88b22015-01-21 15:50:13 -08001043 test->proc(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -08001044 }
1045 timer.end();
djsollen824996a2015-06-12 12:06:22 -07001046 done(timer.fWall, "unit", "test", "", test->name, note, "");
mtklein748ca3b2015-01-15 10:56:12 -08001047}
1048
1049/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1050
mtklein55e88b22015-01-21 15:50:13 -08001051// If we're isolating all GPU-bound work to one thread (the default), this function runs all that.
1052static void run_enclave_and_gpu_tests(SkTArray<Task>* tasks) {
1053 run_enclave(tasks);
1054 for (int i = 0; i < gGPUTests.count(); i++) {
1055 run_test(&gGPUTests[i]);
1056 }
1057}
1058
mtkleinde6fc2b2015-03-12 06:28:54 -07001059// Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung.
1060// This prints something every once in a while so that it knows we're still working.
mtklein2e1c47e2015-03-12 07:16:56 -07001061static void start_keepalive() {
1062 struct Loop {
1063 static void forever(void*) {
1064 for (;;) {
1065 static const int kSec = 300;
1066 #if defined(SK_BUILD_FOR_WIN)
1067 Sleep(kSec * 1000);
1068 #else
1069 sleep(kSec);
1070 #endif
mtkleinb37cb412015-03-18 05:27:14 -07001071 SkString running;
1072 {
1073 SkAutoMutexAcquire lock(gRunningMutex);
1074 for (int i = 0; i < gRunning.count(); i++) {
1075 running.appendf("\n\t%s", gRunning[i].c_str());
1076 }
1077 }
1078 SkDebugf("\nCurrently running:%s\n", running.c_str());
mtklein2e1c47e2015-03-12 07:16:56 -07001079 }
1080 }
1081 };
1082 static SkThread* intentionallyLeaked = new SkThread(Loop::forever);
1083 intentionallyLeaked->start();
mtkleinde6fc2b2015-03-12 06:28:54 -07001084}
1085
caryclark83ca6282015-06-10 09:31:09 -07001086#define PORTABLE_FONT_PREFIX "Toy Liberation "
1087
1088static SkTypeface* create_from_name(const char familyName[], SkTypeface::Style style) {
1089 if (familyName && strlen(familyName) > sizeof(PORTABLE_FONT_PREFIX)
1090 && !strncmp(familyName, PORTABLE_FONT_PREFIX, sizeof(PORTABLE_FONT_PREFIX) - 1)) {
caryclark1818acb2015-07-24 12:09:25 -07001091 return sk_tool_utils::create_portable_typeface(familyName, style);
caryclark83ca6282015-06-10 09:31:09 -07001092 }
halcanary96fcdcc2015-08-27 07:41:13 -07001093 return nullptr;
caryclark83ca6282015-06-10 09:31:09 -07001094}
1095
1096#undef PORTABLE_FONT_PREFIX
1097
1098extern SkTypeface* (*gCreateTypefaceDelegate)(const char [], SkTypeface::Style );
1099
jcgregorio3b27ade2014-11-13 08:06:40 -08001100int dm_main();
caryclark17f0b6d2014-07-22 10:15:34 -07001101int dm_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -08001102 SetupCrashHandler();
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +00001103 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -07001104 SkTaskGroup::Enabler enabled(FLAGS_threads);
caryclark83ca6282015-06-10 09:31:09 -07001105 gCreateTypefaceDelegate = &create_from_name;
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +00001106
mtklein2e1c47e2015-03-12 07:16:56 -07001107 start_keepalive();
mtkleinde6fc2b2015-03-12 06:28:54 -07001108
mtklein62bd1a62015-01-27 14:46:26 -08001109 gather_gold();
borenet09ed4802015-04-03 14:15:33 -07001110 gather_uninteresting_hashes();
mtklein62bd1a62015-01-27 14:46:26 -08001111
mtklein748ca3b2015-01-15 10:56:12 -08001112 gather_srcs();
1113 gather_sinks();
1114 gather_tests();
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +00001115
mtklein55e88b22015-01-21 15:50:13 -08001116 gPending = gSrcs.count() * gSinks.count() + gThreadedTests.count() + gGPUTests.count();
mtklein748ca3b2015-01-15 10:56:12 -08001117 SkDebugf("%d srcs * %d sinks + %d tests == %d tasks\n",
mtklein55e88b22015-01-21 15:50:13 -08001118 gSrcs.count(), gSinks.count(), gThreadedTests.count() + gGPUTests.count(), gPending);
mtklein748ca3b2015-01-15 10:56:12 -08001119
1120 // We try to exploit as much parallelism as is safe. Most Src/Sink pairs run on any thread,
1121 // but Sinks that identify as part of a particular enclave run serially on a single thread.
mtklein82d28432015-01-15 12:46:02 -08001122 // CPU tests run on any thread. GPU tests depend on --gpu_threading.
mtklein748ca3b2015-01-15 10:56:12 -08001123 SkTArray<Task> enclaves[kNumEnclaves];
1124 for (int j = 0; j < gSinks.count(); j++) {
1125 SkTArray<Task>& tasks = enclaves[gSinks[j]->enclave()];
1126 for (int i = 0; i < gSrcs.count(); i++) {
1127 tasks.push_back(Task(gSrcs[i], gSinks[j]));
1128 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001129 }
1130
mtklein748ca3b2015-01-15 10:56:12 -08001131 SkTaskGroup tg;
mtklein55e88b22015-01-21 15:50:13 -08001132 tg.batch(run_test, gThreadedTests.begin(), gThreadedTests.count());
1133 for (int i = 0; i < kNumEnclaves; i++) {
1134 switch(i) {
1135 case kAnyThread_Enclave:
1136 tg.batch(Task::Run, enclaves[i].begin(), enclaves[i].count());
1137 break;
1138 case kGPU_Enclave:
1139 tg.add(run_enclave_and_gpu_tests, &enclaves[i]);
1140 break;
1141 default:
1142 tg.add(run_enclave, &enclaves[i]);
1143 break;
mtklein82d28432015-01-15 12:46:02 -08001144 }
mtklein55e88b22015-01-21 15:50:13 -08001145 }
mtklein748ca3b2015-01-15 10:56:12 -08001146 tg.wait();
mtklein748ca3b2015-01-15 10:56:12 -08001147 // At this point we're back in single-threaded land.
caryclarkf53ce802015-06-15 06:48:30 -07001148 sk_tool_utils::release_portable_typefaces();
mtklein@google.comd36522d2013-10-16 13:02:15 +00001149
mtklein2f64eec2015-01-15 14:20:41 -08001150 SkDebugf("\n");
mtklein748ca3b2015-01-15 10:56:12 -08001151 if (gFailures.count() > 0) {
1152 SkDebugf("Failures:\n");
1153 for (int i = 0; i < gFailures.count(); i++) {
mtklein9dc09102015-01-15 15:47:33 -08001154 SkDebugf("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001155 }
1156 SkDebugf("%d failures\n", gFailures.count());
1157 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001158 }
mtklein748ca3b2015-01-15 10:56:12 -08001159 if (gPending > 0) {
1160 SkDebugf("Hrm, we didn't seem to run everything we intended to! Please file a bug.\n");
1161 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001162 }
halcanary7a14b312015-10-01 07:28:13 -07001163 #ifdef SK_PDF_IMAGE_STATS
1164 SkPDFImageDumpStats();
1165 #endif // SK_PDF_IMAGE_STATS
mtklein748ca3b2015-01-15 10:56:12 -08001166 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +00001167}
jcgregorio3b27ade2014-11-13 08:06:40 -08001168
borenet48087572015-04-02 12:16:36 -07001169#if !defined(SK_BUILD_FOR_IOS)
jcgregorio3b27ade2014-11-13 08:06:40 -08001170int main(int argc, char** argv) {
1171 SkCommandLineFlags::Parse(argc, argv);
1172 return dm_main();
1173}
1174#endif