blob: c51a6fb8aab4156c522c034368b0e25240722671 [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
djsollen54416de2015-04-03 07:24:48 -070044DEFINE_string(src, "tests gm skp image", "Source types to test.");
mtklein748ca3b2015-01-15 10:56:12 -080045DEFINE_bool(nameByHash, false,
46 "If true, write to FLAGS_writePath[0]/<hash>.png instead of "
djsollen54416de2015-04-03 07:24:48 -070047 "to FLAGS_writePath[0]/<config>/<sourceType>/<sourceOptions>/<name>.png");
mtklein748ca3b2015-01-15 10:56:12 -080048DEFINE_bool2(pathOpsExtended, x, false, "Run extended pathOps tests.");
mtkleind603b222015-02-17 11:13:33 -080049DEFINE_string(matrix, "1 0 0 1",
50 "2x2 scale+skew matrix to apply or upright when using "
51 "'matrix' or 'upright' in config.");
mtklein82d28432015-01-15 12:46:02 -080052DEFINE_bool(gpu_threading, false, "Allow GPU work to run on multiple threads?");
mtklein@google.comd36522d2013-10-16 13:02:15 +000053
mtkleina2ef6422015-01-15 13:44:22 -080054DEFINE_string(blacklist, "",
djsollen54416de2015-04-03 07:24:48 -070055 "Space-separated config/src/srcOptions/name quadruples to blacklist. '_' matches anything. E.g. \n"
56 "'--blacklist gpu skp _ _' will blacklist all SKPs drawn into the gpu config.\n"
57 "'--blacklist gpu skp _ _ 8888 gm _ aarects' will also blacklist the aarects GM on 8888.");
mtkleina2ef6422015-01-15 13:44:22 -080058
mtklein62bd1a62015-01-27 14:46:26 -080059DEFINE_string2(readPath, r, "", "If set check for equality with golden results in this directory.");
60
borenet09ed4802015-04-03 14:15:33 -070061DEFINE_string(uninterestingHashesFile, "",
62 "File containing a list of uninteresting hashes. If a result hashes to something in "
63 "this list, no image is written for that result.");
64
mtklein6393c062015-04-27 08:45:01 -070065DEFINE_int32(shards, 1, "We're splitting source data into this many shards.");
66DEFINE_int32(shard, 0, "Which shard do I run?");
bsalomon821e10e2015-06-04 14:15:33 -070067DEFINE_bool2(pre_log, p, false, "Log before running each test. May be incomprehensible when threading");
mtklein6393c062015-04-27 08:45:01 -070068
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
74SK_DECLARE_STATIC_MUTEX(gFailuresMutex);
75static SkTArray<SkString> gFailures;
76
77static void fail(ImplicitString err) {
78 SkAutoMutexAcquire lock(gFailuresMutex);
79 SkDebugf("\n\nFAILURE: %s\n\n", err.c_str());
80 gFailures.push_back(err);
halcanary9e398f72015-01-10 11:18:04 -080081}
82
mtkleinb37cb412015-03-18 05:27:14 -070083static int32_t gPending = 0; // Atomic. Total number of running and queued tasks.
84
85SK_DECLARE_STATIC_MUTEX(gRunningMutex);
86static SkTArray<SkString> gRunning;
mtklein748ca3b2015-01-15 10:56:12 -080087
mtkleinb9eb4ac2015-02-02 18:26:03 -080088static void done(double ms,
djsollen54416de2015-04-03 07:24:48 -070089 ImplicitString config, ImplicitString src, ImplicitString srcOptions,
90 ImplicitString name, ImplicitString note, ImplicitString log) {
91 SkString id = SkStringPrintf("%s %s %s %s", config.c_str(), src.c_str(),
92 srcOptions.c_str(), name.c_str());
mtkleinb37cb412015-03-18 05:27:14 -070093 {
94 SkAutoMutexAcquire lock(gRunningMutex);
95 for (int i = 0; i < gRunning.count(); i++) {
96 if (gRunning[i] == id) {
97 gRunning.removeShuffle(i);
98 break;
99 }
100 }
101 }
102 if (!FLAGS_verbose) {
103 note = "";
104 }
mtkleinb9eb4ac2015-02-02 18:26:03 -0800105 if (!log.isEmpty()) {
106 log.prepend("\n");
107 }
mtklein6dee2ad2015-02-05 09:53:44 -0800108 auto pending = sk_atomic_dec(&gPending)-1;
reed50bc0512015-05-19 14:13:31 -0700109 if (!FLAGS_quiet) {
mtklein711a2452015-07-08 08:49:20 -0700110 SkDebugf("%s(%4d/%-4dMB %6d) %s\t%s%s%s", FLAGS_verbose ? "\n" : kSkOverwriteLine
reed50bc0512015-05-19 14:13:31 -0700111 , sk_tools::getCurrResidentSetSizeMB()
112 , sk_tools::getMaxResidentSetSizeMB()
113 , pending
114 , HumanizeMs(ms).c_str()
115 , id.c_str()
116 , note.c_str()
117 , log.c_str());
118 }
mtkleina17241b2015-01-23 05:48:00 -0800119 // We write our dm.json file every once in a while in case we crash.
120 // Notice this also handles the final dm.json when pending == 0.
121 if (pending % 500 == 0) {
122 JsonWriter::DumpJson();
123 }
mtklein@google.comd36522d2013-10-16 13:02:15 +0000124}
125
djsollen54416de2015-04-03 07:24:48 -0700126static void start(ImplicitString config, ImplicitString src,
127 ImplicitString srcOptions, ImplicitString name) {
128 SkString id = SkStringPrintf("%s %s %s %s", config.c_str(), src.c_str(),
129 srcOptions.c_str(), name.c_str());
mtkleinb37cb412015-03-18 05:27:14 -0700130 SkAutoMutexAcquire lock(gRunningMutex);
131 gRunning.push_back(id);
132}
133
mtklein748ca3b2015-01-15 10:56:12 -0800134/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
mtklein114c3cd2015-01-15 10:15:02 -0800135
mtklein62bd1a62015-01-27 14:46:26 -0800136struct Gold : public SkString {
mtkleina82f5622015-02-20 12:30:19 -0800137 Gold() : SkString("") {}
djsollen54416de2015-04-03 07:24:48 -0700138 Gold(ImplicitString sink, ImplicitString src, ImplicitString srcOptions,
139 ImplicitString name, ImplicitString md5)
mtklein62bd1a62015-01-27 14:46:26 -0800140 : SkString("") {
141 this->append(sink);
142 this->append(src);
djsollen54416de2015-04-03 07:24:48 -0700143 this->append(srcOptions);
mtklein62bd1a62015-01-27 14:46:26 -0800144 this->append(name);
145 this->append(md5);
mtklein62bd1a62015-01-27 14:46:26 -0800146 }
mtkleinc8d1dd42015-10-15 12:23:01 -0700147 struct Hash {
148 uint32_t operator()(const Gold& g) const {
149 return SkGoodHash()((const SkString&)g);
150 }
151 };
mtklein62bd1a62015-01-27 14:46:26 -0800152};
mtkleina82f5622015-02-20 12:30:19 -0800153static SkTHashSet<Gold, Gold::Hash> gGold;
mtklein62bd1a62015-01-27 14:46:26 -0800154
155static void add_gold(JsonWriter::BitmapResult r) {
djsollen54416de2015-04-03 07:24:48 -0700156 gGold.add(Gold(r.config, r.sourceType, r.sourceOptions, r.name, r.md5));
mtklein62bd1a62015-01-27 14:46:26 -0800157}
158
159static void gather_gold() {
160 if (!FLAGS_readPath.isEmpty()) {
161 SkString path(FLAGS_readPath[0]);
162 path.append("/dm.json");
163 if (!JsonWriter::ReadJson(path.c_str(), add_gold)) {
164 fail(SkStringPrintf("Couldn't read %s for golden results.", path.c_str()));
165 }
166 }
167}
168
169/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
170
borenet09ed4802015-04-03 14:15:33 -0700171static SkTHashSet<SkString> gUninterestingHashes;
172
173static void gather_uninteresting_hashes() {
174 if (!FLAGS_uninterestingHashesFile.isEmpty()) {
175 SkAutoTUnref<SkData> data(SkData::NewFromFileName(FLAGS_uninterestingHashesFile[0]));
mtkleincc334b32015-09-22 11:43:53 -0700176 if (!data) {
177 SkDebugf("WARNING: unable to read uninteresting hashes from %s\n",
178 FLAGS_uninterestingHashesFile[0]);
179 return;
180 }
borenet09ed4802015-04-03 14:15:33 -0700181 SkTArray<SkString> hashes;
182 SkStrSplit((const char*)data->data(), "\n", &hashes);
183 for (const SkString& hash : hashes) {
184 gUninterestingHashes.add(hash);
185 }
186 }
187}
188
189/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
190
mtkleine0effd62015-07-29 06:37:28 -0700191struct TaggedSrc : public SkAutoTDelete<Src> {
msarett9e707a02015-09-01 14:57:57 -0700192 ImplicitString tag;
193 ImplicitString options;
mtkleine0effd62015-07-29 06:37:28 -0700194};
195
196struct TaggedSink : public SkAutoTDelete<Sink> {
197 const char* tag;
djsollen54416de2015-04-03 07:24:48 -0700198};
mtklein748ca3b2015-01-15 10:56:12 -0800199
200static const bool kMemcpyOK = true;
201
mtkleine0effd62015-07-29 06:37:28 -0700202static SkTArray<TaggedSrc, kMemcpyOK> gSrcs;
203static SkTArray<TaggedSink, kMemcpyOK> gSinks;
mtklein748ca3b2015-01-15 10:56:12 -0800204
mtklein6393c062015-04-27 08:45:01 -0700205static bool in_shard() {
206 static int N = 0;
207 return N++ % FLAGS_shards == FLAGS_shard;
208}
209
msarett9e707a02015-09-01 14:57:57 -0700210static void push_src(ImplicitString tag, ImplicitString options, Src* s) {
mtklein748ca3b2015-01-15 10:56:12 -0800211 SkAutoTDelete<Src> src(s);
mtklein6393c062015-04-27 08:45:01 -0700212 if (in_shard() &&
msarett9e707a02015-09-01 14:57:57 -0700213 FLAGS_src.contains(tag.c_str()) &&
mtklein748ca3b2015-01-15 10:56:12 -0800214 !SkCommandLineFlags::ShouldSkip(FLAGS_match, src->name().c_str())) {
mtkleine0effd62015-07-29 06:37:28 -0700215 TaggedSrc& s = gSrcs.push_back();
mtklein748ca3b2015-01-15 10:56:12 -0800216 s.reset(src.detach());
217 s.tag = tag;
djsollen54416de2015-04-03 07:24:48 -0700218 s.options = options;
mtklein114c3cd2015-01-15 10:15:02 -0800219 }
mtklein748ca3b2015-01-15 10:56:12 -0800220}
mtklein114c3cd2015-01-15 10:15:02 -0800221
msarett9e707a02015-09-01 14:57:57 -0700222static void push_codec_src(Path path, CodecSrc::Mode mode, CodecSrc::DstColorType dstColorType,
223 float scale) {
224 SkString folder;
225 switch (mode) {
226 case CodecSrc::kCodec_Mode:
227 folder.append("codec");
228 break;
msarett9e707a02015-09-01 14:57:57 -0700229 case CodecSrc::kScanline_Mode:
230 folder.append("scanline");
231 break;
232 case CodecSrc::kScanline_Subset_Mode:
233 folder.append("scanline_subset");
234 break;
235 case CodecSrc::kStripe_Mode:
236 folder.append("stripe");
237 break;
238 case CodecSrc::kSubset_Mode:
msarett36c37962015-09-02 13:20:52 -0700239 folder.append("codec_subset");
msarett9e707a02015-09-01 14:57:57 -0700240 break;
241 }
242
243 switch (dstColorType) {
244 case CodecSrc::kGrayscale_Always_DstColorType:
245 folder.append("_kGray8");
246 break;
247 case CodecSrc::kIndex8_Always_DstColorType:
248 folder.append("_kIndex8");
249 break;
250 default:
251 break;
252 }
253
254 if (1.0f != scale) {
255 folder.appendf("_%.3f", scale);
256 }
257
258 CodecSrc* src = new CodecSrc(path, mode, dstColorType, scale);
259 push_src("image", folder, src);
260}
261
msarett3d9d7a72015-10-21 10:27:10 -0700262static void push_android_codec_src(Path path, AndroidCodecSrc::Mode mode,
263 CodecSrc::DstColorType dstColorType, int sampleSize) {
264 SkString folder;
265 switch (mode) {
266 case AndroidCodecSrc::kFullImage_Mode:
267 folder.append("scaled_codec");
268 break;
269 case AndroidCodecSrc::kDivisor_Mode:
270 folder.append("scaled_codec_divisor");
271 break;
272 }
273
274 switch (dstColorType) {
275 case CodecSrc::kGrayscale_Always_DstColorType:
276 folder.append("_kGray8");
277 break;
278 case CodecSrc::kIndex8_Always_DstColorType:
279 folder.append("_kIndex8");
280 break;
281 default:
282 break;
283 }
284
285 if (1 != sampleSize) {
286 folder.appendf("_%.3f", get_scale_from_sample_size(sampleSize));
287 }
288
289 AndroidCodecSrc* src = new AndroidCodecSrc(path, mode, dstColorType, sampleSize);
290 push_src("image", folder, src);
291}
292
msarett438b2ad2015-04-09 12:43:10 -0700293static void push_codec_srcs(Path path) {
294 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
295 if (!encoded) {
296 SkDebugf("Couldn't read %s.", path.c_str());
297 return;
298 }
299 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
halcanary96fcdcc2015-08-27 07:41:13 -0700300 if (nullptr == codec.get()) {
msarett438b2ad2015-04-09 12:43:10 -0700301 SkDebugf("Couldn't create codec for %s.", path.c_str());
302 return;
303 }
304
msarett36c37962015-09-02 13:20:52 -0700305 // Native Scales
msarett0a242972015-06-11 14:27:27 -0700306 // TODO (msarett): Implement scaling tests for SkImageDecoder in order to compare with these
307 // tests. SkImageDecoder supports downscales by integer factors.
emmaleer8f4ba762015-08-14 07:44:46 -0700308 // 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 -0700309 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 -0700310
msarett36c37962015-09-02 13:20:52 -0700311 const CodecSrc::Mode nativeModes[] = { CodecSrc::kCodec_Mode, CodecSrc::kScanline_Mode,
312 CodecSrc::kScanline_Subset_Mode, CodecSrc::kStripe_Mode, CodecSrc::kSubset_Mode };
msarett9e707a02015-09-01 14:57:57 -0700313
msarett36c37962015-09-02 13:20:52 -0700314 CodecSrc::DstColorType colorTypes[3];
315 uint32_t numColorTypes;
316 switch (codec->getInfo().colorType()) {
317 case kGray_8_SkColorType:
318 // FIXME: Is this a long term solution for testing wbmps decodes to kIndex8?
msarett3d9d7a72015-10-21 10:27:10 -0700319 // Further discussion on this topic is at skbug.com/3683.
320 // This causes us to try to convert grayscale jpegs to kIndex8. We currently
321 // fail non-fatally in this case.
msarett36c37962015-09-02 13:20:52 -0700322 colorTypes[0] = CodecSrc::kGetFromCanvas_DstColorType;
323 colorTypes[1] = CodecSrc::kGrayscale_Always_DstColorType;
324 colorTypes[2] = CodecSrc::kIndex8_Always_DstColorType;
325 numColorTypes = 3;
326 break;
327 case kIndex_8_SkColorType:
328 colorTypes[0] = CodecSrc::kGetFromCanvas_DstColorType;
329 colorTypes[1] = CodecSrc::kIndex8_Always_DstColorType;
330 numColorTypes = 2;
331 break;
332 default:
333 colorTypes[0] = CodecSrc::kGetFromCanvas_DstColorType;
334 numColorTypes = 1;
335 break;
336 }
msarett9e707a02015-09-01 14:57:57 -0700337
msarett36c37962015-09-02 13:20:52 -0700338 for (float scale : nativeScales) {
msarett36c37962015-09-02 13:20:52 -0700339 for (CodecSrc::Mode mode : nativeModes) {
340 for (uint32_t i = 0; i < numColorTypes; i++) {
341 push_codec_src(path, mode, colorTypes[i], scale);
msarett9e707a02015-09-01 14:57:57 -0700342 }
343 }
msarett0a242972015-06-11 14:27:27 -0700344 }
msarett36c37962015-09-02 13:20:52 -0700345
msarett3d9d7a72015-10-21 10:27:10 -0700346 // skbug.com/4428
347 static const char* const exts[] = {
348 "jpg", "jpeg", "png", "webp",
349 "JPG", "JPEG", "PNG", "WEBP",
350 };
351 bool supported = false;
352 for (const char* ext : exts) {
353 if (path.endsWith(ext)) {
354 supported = true;
355 break;
356 }
357 }
358 if (!supported) {
scroggo46c57472015-09-30 08:57:13 -0700359 return;
360 }
361
msarett3d9d7a72015-10-21 10:27:10 -0700362 const int sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
msarett36c37962015-09-02 13:20:52 -0700363
msarett3d9d7a72015-10-21 10:27:10 -0700364 const AndroidCodecSrc::Mode androidModes[] = {
365 AndroidCodecSrc::kFullImage_Mode,
366 AndroidCodecSrc::kDivisor_Mode,
367 };
368
369 for (int sampleSize : sampleSizes) {
370 for (AndroidCodecSrc::Mode mode : androidModes) {
371 for (uint32_t i = 0; i < numColorTypes; i++) {
372 push_android_codec_src(path, mode, colorTypes[i], sampleSize);
373 }
msarett36c37962015-09-02 13:20:52 -0700374 }
375 }
msarett438b2ad2015-04-09 12:43:10 -0700376}
377
msaretta5783ae2015-09-08 15:35:32 -0700378static bool brd_color_type_supported(SkBitmapRegionDecoderInterface::Strategy strategy,
379 CodecSrc::DstColorType dstColorType) {
380 switch (strategy) {
381 case SkBitmapRegionDecoderInterface::kCanvas_Strategy:
382 if (CodecSrc::kGetFromCanvas_DstColorType == dstColorType) {
383 return true;
384 }
385 return false;
386 case SkBitmapRegionDecoderInterface::kOriginal_Strategy:
387 switch (dstColorType) {
388 case CodecSrc::kGetFromCanvas_DstColorType:
389 case CodecSrc::kIndex8_Always_DstColorType:
390 case CodecSrc::kGrayscale_Always_DstColorType:
391 return true;
392 default:
393 return false;
394 }
395 default:
396 SkASSERT(false);
397 return false;
398 }
399}
400
401static void push_brd_src(Path path, SkBitmapRegionDecoderInterface::Strategy strategy,
402 CodecSrc::DstColorType dstColorType, BRDSrc::Mode mode, uint32_t sampleSize) {
403 SkString folder;
404 switch (strategy) {
405 case SkBitmapRegionDecoderInterface::kCanvas_Strategy:
406 folder.append("brd_canvas");
407 break;
408 case SkBitmapRegionDecoderInterface::kOriginal_Strategy:
409 folder.append("brd_sample");
410 break;
411 default:
412 SkASSERT(false);
413 return;
414 }
415
416 switch (mode) {
417 case BRDSrc::kFullImage_Mode:
418 break;
419 case BRDSrc::kDivisor_Mode:
420 folder.append("_divisor");
421 break;
422 default:
423 SkASSERT(false);
424 return;
425 }
426
427 switch (dstColorType) {
428 case CodecSrc::kGetFromCanvas_DstColorType:
429 break;
430 case CodecSrc::kIndex8_Always_DstColorType:
431 folder.append("_kIndex");
432 break;
433 case CodecSrc::kGrayscale_Always_DstColorType:
434 folder.append("_kGray");
435 break;
436 default:
437 SkASSERT(false);
438 return;
439 }
440
441 if (1 != sampleSize) {
msarett7f691442015-09-22 11:56:16 -0700442 folder.appendf("_%.3f", get_scale_from_sample_size(sampleSize));
msaretta5783ae2015-09-08 15:35:32 -0700443 }
444
445 BRDSrc* src = new BRDSrc(path, strategy, mode, dstColorType, sampleSize);
446 push_src("image", folder, src);
447}
448
449static void push_brd_srcs(Path path) {
450
451 const SkBitmapRegionDecoderInterface::Strategy strategies[] = {
452 SkBitmapRegionDecoderInterface::kCanvas_Strategy,
453 SkBitmapRegionDecoderInterface::kOriginal_Strategy
454 };
455
msarett6efbe052015-09-11 09:01:16 -0700456 const uint32_t sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
457
msaretta5783ae2015-09-08 15:35:32 -0700458 // We will only test to one backend (8888), but we will test all of the
459 // color types that we need to decode to on this backend.
460 const CodecSrc::DstColorType dstColorTypes[] = {
461 CodecSrc::kGetFromCanvas_DstColorType,
462 CodecSrc::kIndex8_Always_DstColorType,
463 CodecSrc::kGrayscale_Always_DstColorType,
464 };
465
466 const BRDSrc::Mode modes[] = {
467 BRDSrc::kFullImage_Mode,
468 BRDSrc::kDivisor_Mode
469 };
470
msaretta5783ae2015-09-08 15:35:32 -0700471 for (SkBitmapRegionDecoderInterface::Strategy strategy : strategies) {
msarett6efbe052015-09-11 09:01:16 -0700472
msaretta5783ae2015-09-08 15:35:32 -0700473 // We disable png testing for kOriginal_Strategy because the implementation leaks
474 // memory in our forked libpng.
475 // TODO (msarett): Decide if we want to test pngs in this mode and how we might do this.
476 if (SkBitmapRegionDecoderInterface::kOriginal_Strategy == strategy &&
477 (path.endsWith(".png") || path.endsWith(".PNG"))) {
478 continue;
479 }
msarett6efbe052015-09-11 09:01:16 -0700480 for (uint32_t sampleSize : sampleSizes) {
481
482 // kOriginal_Strategy does not work for jpegs that are scaled to non-powers of two.
483 // We don't need to test this. We know it doesn't work, and it causes images with
484 // uninitialized memory to show up on Gold.
485 if (SkBitmapRegionDecoderInterface::kOriginal_Strategy == strategy &&
486 (path.endsWith(".jpg") || path.endsWith(".JPG") ||
487 path.endsWith(".jpeg") || path.endsWith(".JPEG")) && !SkIsPow2(sampleSize)) {
488 continue;
489 }
490 for (CodecSrc::DstColorType dstColorType : dstColorTypes) {
491 if (brd_color_type_supported(strategy, dstColorType)) {
492 for (BRDSrc::Mode mode : modes) {
msaretta5783ae2015-09-08 15:35:32 -0700493 push_brd_src(path, strategy, dstColorType, mode, sampleSize);
494 }
495 }
496 }
497 }
498 }
499}
500
501static bool brd_supported(const char* ext) {
msarett8c8f22a2015-04-01 06:58:48 -0700502 static const char* const exts[] = {
msaretta5783ae2015-09-08 15:35:32 -0700503 "jpg", "jpeg", "png", "webp",
504 "JPG", "JPEG", "PNG", "WEBP",
msarett8c8f22a2015-04-01 06:58:48 -0700505 };
506
507 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
508 if (0 == strcmp(exts[i], ext)) {
509 return true;
510 }
511 }
512 return false;
scroggo9b77ddd2015-03-19 06:03:39 -0700513}
514
mtklein748ca3b2015-01-15 10:56:12 -0800515static void gather_srcs() {
516 for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) {
djsollen54416de2015-04-03 07:24:48 -0700517 push_src("gm", "", new GMSrc(r->factory()));
mtklein748ca3b2015-01-15 10:56:12 -0800518 }
halcanaryfc37ad12015-01-30 07:31:19 -0800519 for (int i = 0; i < FLAGS_skps.count(); i++) {
520 const char* path = FLAGS_skps[i];
521 if (sk_isdir(path)) {
522 SkOSFile::Iter it(path, "skp");
523 for (SkString file; it.next(&file); ) {
djsollen54416de2015-04-03 07:24:48 -0700524 push_src("skp", "", new SKPSrc(SkOSPath::Join(path, file.c_str())));
halcanaryfc37ad12015-01-30 07:31:19 -0800525 }
526 } else {
djsollen54416de2015-04-03 07:24:48 -0700527 push_src("skp", "", new SKPSrc(path));
mtklein114c3cd2015-01-15 10:15:02 -0800528 }
529 }
halcanary23b03c32015-01-30 09:58:58 -0800530 static const char* const exts[] = {
531 "bmp", "gif", "jpg", "jpeg", "png", "webp", "ktx", "astc", "wbmp", "ico",
532 "BMP", "GIF", "JPG", "JPEG", "PNG", "WEBP", "KTX", "ASTC", "WBMP", "ICO",
533 };
534 for (int i = 0; i < FLAGS_images.count(); i++) {
535 const char* flag = FLAGS_images[i];
536 if (sk_isdir(flag)) {
537 for (size_t j = 0; j < SK_ARRAY_COUNT(exts); j++) {
538 SkOSFile::Iter it(flag, exts[j]);
539 for (SkString file; it.next(&file); ) {
540 SkString path = SkOSPath::Join(flag, file.c_str());
djsollen54416de2015-04-03 07:24:48 -0700541 push_src("image", "decode", new ImageSrc(path)); // Decode entire image
542 push_src("image", "subset", new ImageSrc(path, 2)); // Decode into 2x2 subsets
msaretta5783ae2015-09-08 15:35:32 -0700543 push_codec_srcs(path);
544 if (brd_supported(exts[j])) {
545 push_brd_srcs(path);
scroggo9b77ddd2015-03-19 06:03:39 -0700546 }
halcanary23b03c32015-01-30 09:58:58 -0800547 }
mtklein114c3cd2015-01-15 10:15:02 -0800548 }
halcanary23b03c32015-01-30 09:58:58 -0800549 } else if (sk_exists(flag)) {
550 // assume that FLAGS_images[i] is a valid image if it is a file.
djsollen54416de2015-04-03 07:24:48 -0700551 push_src("image", "decode", new ImageSrc(flag)); // Decode entire image.
552 push_src("image", "subset", new ImageSrc(flag, 2)); // Decode into 2 x 2 subsets
msarett438b2ad2015-04-09 12:43:10 -0700553 push_codec_srcs(flag);
msaretta5783ae2015-09-08 15:35:32 -0700554 push_brd_srcs(flag);
mtklein709d2c32015-01-15 08:30:25 -0800555 }
mtklein709d2c32015-01-15 08:30:25 -0800556 }
557}
558
mtklein748ca3b2015-01-15 10:56:12 -0800559static GrGLStandard get_gpu_api() {
560 if (FLAGS_gpuAPI.contains("gl")) { return kGL_GrGLStandard; }
561 if (FLAGS_gpuAPI.contains("gles")) { return kGLES_GrGLStandard; }
562 return kNone_GrGLStandard;
mtklein709d2c32015-01-15 08:30:25 -0800563}
564
mtklein748ca3b2015-01-15 10:56:12 -0800565static void push_sink(const char* tag, Sink* s) {
566 SkAutoTDelete<Sink> sink(s);
567 if (!FLAGS_config.contains(tag)) {
568 return;
mtklein114c3cd2015-01-15 10:15:02 -0800569 }
mtkleine0effd62015-07-29 06:37:28 -0700570 // Try a simple Src as a canary. If it fails, skip this sink.
mtklein748ca3b2015-01-15 10:56:12 -0800571 struct : public Src {
mtkleine0effd62015-07-29 06:37:28 -0700572 Error draw(SkCanvas* c) const override {
573 c->drawRect(SkRect::MakeWH(1,1), SkPaint());
574 return "";
575 }
mtklein36352bf2015-03-25 18:17:31 -0700576 SkISize size() const override { return SkISize::Make(16, 16); }
mtkleine0effd62015-07-29 06:37:28 -0700577 Name name() const override { return "justOneRect"; }
578 } justOneRect;
mtklein114c3cd2015-01-15 10:15:02 -0800579
mtklein748ca3b2015-01-15 10:56:12 -0800580 SkBitmap bitmap;
581 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800582 SkString log;
mtkleine0effd62015-07-29 06:37:28 -0700583 Error err = sink->draw(justOneRect, &bitmap, &stream, &log);
mtklein4089ef72015-03-05 08:40:28 -0800584 if (err.isFatal()) {
mtklein05641a52015-04-21 10:49:13 -0700585 SkDebugf("Could not run %s: %s\n", tag, err.c_str());
586 exit(1);
mtklein114c3cd2015-01-15 10:15:02 -0800587 }
588
mtkleine0effd62015-07-29 06:37:28 -0700589 TaggedSink& ts = gSinks.push_back();
mtklein748ca3b2015-01-15 10:56:12 -0800590 ts.reset(sink.detach());
591 ts.tag = tag;
592}
593
594static bool gpu_supported() {
595#if SK_SUPPORT_GPU
596 return FLAGS_gpu;
597#else
598 return false;
599#endif
600}
601
602static Sink* create_sink(const char* tag) {
603#define SINK(t, sink, ...) if (0 == strcmp(t, tag)) { return new sink(__VA_ARGS__); }
604 if (gpu_supported()) {
mtklein82d28432015-01-15 12:46:02 -0800605 typedef GrContextFactory Gr;
mtklein748ca3b2015-01-15 10:56:12 -0800606 const GrGLStandard api = get_gpu_api();
hendrikw885bf092015-08-27 10:38:39 -0700607 SINK("gpunull", GPUSink, Gr::kNull_GLContextType, api, 0, false, FLAGS_gpu_threading);
608 SINK("gpudebug", GPUSink, Gr::kDebug_GLContextType, api, 0, false, FLAGS_gpu_threading);
609 SINK("gpu", GPUSink, Gr::kNative_GLContextType, api, 0, false, FLAGS_gpu_threading);
610 SINK("gpudft", GPUSink, Gr::kNative_GLContextType, api, 0, true, FLAGS_gpu_threading);
611 SINK("msaa4", GPUSink, Gr::kNative_GLContextType, api, 4, false, FLAGS_gpu_threading);
612 SINK("msaa16", GPUSink, Gr::kNative_GLContextType, api, 16, false, FLAGS_gpu_threading);
bsalomon90e8ab72015-09-08 10:26:51 -0700613 SINK("nvprmsaa4", GPUSink, Gr::kNVPR_GLContextType, api, 4, true, FLAGS_gpu_threading);
614 SINK("nvprmsaa16", GPUSink, Gr::kNVPR_GLContextType, api, 16, true, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800615 #if SK_ANGLE
hendrikw885bf092015-08-27 10:38:39 -0700616 SINK("angle", GPUSink, Gr::kANGLE_GLContextType, api, 0, false, FLAGS_gpu_threading);
hendrikweddbefb2015-09-11 13:07:29 -0700617 SINK("angle-gl", GPUSink, Gr::kANGLE_GL_GLContextType, api, 0, false, FLAGS_gpu_threading);
hendrikw885bf092015-08-27 10:38:39 -0700618 #endif
619 #if SK_COMMAND_BUFFER
620 SINK("commandbuffer", GPUSink, Gr::kCommandBuffer_GLContextType, api, 0, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800621 #endif
622 #if SK_MESA
hendrikw885bf092015-08-27 10:38:39 -0700623 SINK("mesa", GPUSink, Gr::kMESA_GLContextType, api, 0, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800624 #endif
mtklein114c3cd2015-01-15 10:15:02 -0800625 }
mtklein748ca3b2015-01-15 10:56:12 -0800626
tomhudsoneebc39a2015-02-23 12:18:05 -0800627#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
628 SINK("hwui", HWUISink);
629#endif
630
mtklein748ca3b2015-01-15 10:56:12 -0800631 if (FLAGS_cpu) {
632 SINK("565", RasterSink, kRGB_565_SkColorType);
633 SINK("8888", RasterSink, kN32_SkColorType);
halcanaryc11c62f2015-09-28 11:51:54 -0700634 SINK("pdf", PDFSink, "Pdfium");
635 SINK("pdf_poppler", PDFSink, "Poppler");
mtklein9c3f17d2015-01-28 11:35:18 -0800636 SINK("skp", SKPSink);
mtklein8a4527e2015-01-31 20:00:58 -0800637 SINK("svg", SVGSink);
638 SINK("null", NullSink);
halcanary47ef4d52015-03-03 09:13:09 -0800639 SINK("xps", XPSSink);
mtklein748ca3b2015-01-15 10:56:12 -0800640 }
641#undef SINK
halcanary96fcdcc2015-08-27 07:41:13 -0700642 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800643}
644
mtklein748ca3b2015-01-15 10:56:12 -0800645static Sink* create_via(const char* tag, Sink* wrapped) {
646#define VIA(t, via, ...) if (0 == strcmp(t, tag)) { return new via(__VA_ARGS__); }
halcanary96fcdcc2015-08-27 07:41:13 -0700647 VIA("twice", ViaTwice, wrapped);
648 VIA("pipe", ViaPipe, wrapped);
649 VIA("serialize", ViaSerialization, wrapped);
650 VIA("2ndpic", ViaSecondPicture, wrapped);
mtkleind31c13d2015-05-05 12:59:56 -0700651 VIA("sp", ViaSingletonPictures, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700652 VIA("tiles", ViaTiles, 256, 256, nullptr, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800653 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
mtklein2e2ea382015-10-16 10:29:41 -0700654 VIA("remote", ViaRemote, false, wrapped);
655 VIA("remote_cache", ViaRemote, true, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800656
mtkleind603b222015-02-17 11:13:33 -0800657 if (FLAGS_matrix.count() == 4) {
mtklein748ca3b2015-01-15 10:56:12 -0800658 SkMatrix m;
mtkleind603b222015-02-17 11:13:33 -0800659 m.reset();
660 m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
661 m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
662 m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
663 m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
664 VIA("matrix", ViaMatrix, m, wrapped);
665 VIA("upright", ViaUpright, m, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800666 }
tomhudson64de1e12015-03-05 08:01:07 -0800667
668#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
669 VIA("androidsdk", ViaAndroidSDK, wrapped);
670#endif
671
mtklein748ca3b2015-01-15 10:56:12 -0800672#undef VIA
halcanary96fcdcc2015-08-27 07:41:13 -0700673 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800674}
675
mtklein748ca3b2015-01-15 10:56:12 -0800676static void gather_sinks() {
677 for (int i = 0; i < FLAGS_config.count(); i++) {
678 const char* config = FLAGS_config[i];
679 SkTArray<SkString> parts;
680 SkStrSplit(config, "-", &parts);
681
halcanary96fcdcc2015-08-27 07:41:13 -0700682 Sink* sink = nullptr;
mtklein748ca3b2015-01-15 10:56:12 -0800683 for (int i = parts.count(); i-- > 0;) {
684 const char* part = parts[i].c_str();
halcanary96fcdcc2015-08-27 07:41:13 -0700685 Sink* next = (sink == nullptr) ? create_sink(part) : create_via(part, sink);
686 if (next == nullptr) {
mtklein748ca3b2015-01-15 10:56:12 -0800687 SkDebugf("Skipping %s: Don't understand '%s'.\n", config, part);
688 delete sink;
halcanary96fcdcc2015-08-27 07:41:13 -0700689 sink = nullptr;
mtklein748ca3b2015-01-15 10:56:12 -0800690 break;
691 }
692 sink = next;
693 }
694 if (sink) {
695 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800696 }
697 }
698}
mtklein709d2c32015-01-15 08:30:25 -0800699
mtkleina5114d72015-08-24 13:27:01 -0700700static bool dump_png(SkBitmap bitmap, const char* path, const char* md5) {
701 const int w = bitmap.width(),
702 h = bitmap.height();
703
704 // First get the bitmap into N32 color format. The next step will work only there.
705 if (bitmap.colorType() != kN32_SkColorType) {
706 SkBitmap n32;
707 if (!bitmap.copyTo(&n32, kN32_SkColorType)) {
708 return false;
709 }
710 bitmap = n32;
711 }
712
713 // Convert our N32 bitmap into unpremul RGBA for libpng.
714 SkAutoTMalloc<uint32_t> rgba(w*h);
715 if (!bitmap.readPixels(SkImageInfo::Make(w,h, kRGBA_8888_SkColorType, kUnpremul_SkAlphaType),
716 rgba, 4*w, 0,0)) {
717 return false;
718 }
719
720 // We don't need bitmap anymore. Might as well drop our ref.
mtkleinfccb77d2015-08-24 14:13:29 -0700721 bitmap.reset();
mtkleina5114d72015-08-24 13:27:01 -0700722
mtkleinc64137c2015-08-25 10:56:08 -0700723 FILE* f = fopen(path, "wb");
mtkleina5114d72015-08-24 13:27:01 -0700724 if (!f) { return false; }
725
726 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
727 if (!png) {
728 fclose(f);
729 return false;
730 }
731
732 png_infop info = png_create_info_struct(png);
733 if (!info) {
734 png_destroy_write_struct(&png, &info);
735 fclose(f);
736 return false;
737 }
738
mtkleind69ece62015-09-10 10:37:44 -0700739 SkString description;
740 description.append("Key: ");
741 for (int i = 0; i < FLAGS_key.count(); i++) {
742 description.appendf("%s ", FLAGS_key[i]);
743 }
744 description.append("Properties: ");
745 for (int i = 0; i < FLAGS_properties.count(); i++) {
746 description.appendf("%s ", FLAGS_properties[i]);
747 }
748 description.appendf("MD5: %s", md5);
749
mtkleina5114d72015-08-24 13:27:01 -0700750 png_text text[2];
751 text[0].key = (png_charp)"Author";
752 text[0].text = (png_charp)"DM dump_png()";
753 text[0].compression = PNG_TEXT_COMPRESSION_NONE;
754 text[1].key = (png_charp)"Description";
mtkleind69ece62015-09-10 10:37:44 -0700755 text[1].text = (png_charp)description.c_str();
mtkleina5114d72015-08-24 13:27:01 -0700756 text[1].compression = PNG_TEXT_COMPRESSION_NONE;
757 png_set_text(png, info, text, 2);
758
759 png_init_io(png, f);
760 png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8,
761 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
762 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
763 png_write_info(png, info);
764 for (int j = 0; j < h; j++) {
765 png_bytep row = (png_bytep)(rgba.get() + w*j);
766 png_write_rows(png, &row, 1);
767 }
768 png_write_end(png, info);
769
770 png_destroy_write_struct(&png, &info);
771 fclose(f);
772 return true;
773}
774
mtkleina2ef6422015-01-15 13:44:22 -0800775static bool match(const char* needle, const char* haystack) {
halcanary96fcdcc2015-08-27 07:41:13 -0700776 return 0 == strcmp("_", needle) || nullptr != strstr(haystack, needle);
mtkleina2ef6422015-01-15 13:44:22 -0800777}
778
djsollen54416de2015-04-03 07:24:48 -0700779static ImplicitString is_blacklisted(const char* sink, const char* src,
780 const char* srcOptions, const char* name) {
mtklein0d243ff2015-04-03 07:51:00 -0700781 for (int i = 0; i < FLAGS_blacklist.count() - 3; i += 4) {
mtkleina2ef6422015-01-15 13:44:22 -0800782 if (match(FLAGS_blacklist[i+0], sink) &&
djsollen54416de2015-04-03 07:24:48 -0700783 match(FLAGS_blacklist[i+1], src) &&
784 match(FLAGS_blacklist[i+2], srcOptions) &&
785 match(FLAGS_blacklist[i+3], name)) {
786 return SkStringPrintf("%s %s %s %s",
787 FLAGS_blacklist[i+0], FLAGS_blacklist[i+1],
788 FLAGS_blacklist[i+2], FLAGS_blacklist[i+3]);
mtkleina2ef6422015-01-15 13:44:22 -0800789 }
790 }
791 return "";
792}
793
mtklein748ca3b2015-01-15 10:56:12 -0800794// The finest-grained unit of work we can run: draw a single Src into a single Sink,
795// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
796struct Task {
mtkleine0effd62015-07-29 06:37:28 -0700797 Task(const TaggedSrc& src, const TaggedSink& sink) : src(src), sink(sink) {}
798 const TaggedSrc& src;
799 const TaggedSink& sink;
mtklein748ca3b2015-01-15 10:56:12 -0800800
801 static void Run(Task* task) {
mtkleina2ef6422015-01-15 13:44:22 -0800802 SkString name = task->src->name();
mtkleine0effd62015-07-29 06:37:28 -0700803
804 // We'll skip drawing this Src/Sink pair if:
805 // - the Src vetoes the Sink;
806 // - this Src / Sink combination is on the blacklist;
807 // - it's a dry run.
mtklein99cab4e2015-07-31 06:43:04 -0700808 SkString note(task->src->veto(task->sink->flags()) ? " (veto)" : "");
msarett9e707a02015-09-01 14:57:57 -0700809 SkString whyBlacklisted = is_blacklisted(task->sink.tag, task->src.tag.c_str(),
810 task->src.options.c_str(), name.c_str());
mtkleinb37cb412015-03-18 05:27:14 -0700811 if (!whyBlacklisted.isEmpty()) {
812 note.appendf(" (--blacklist %s)", whyBlacklisted.c_str());
813 }
mtkleine0effd62015-07-29 06:37:28 -0700814
mtkleinb9eb4ac2015-02-02 18:26:03 -0800815 SkString log;
mtklein748ca3b2015-01-15 10:56:12 -0800816 WallTimer timer;
817 timer.start();
mtkleine0effd62015-07-29 06:37:28 -0700818 if (!FLAGS_dryRun && note.isEmpty()) {
mtklein748ca3b2015-01-15 10:56:12 -0800819 SkBitmap bitmap;
820 SkDynamicMemoryWStream stream;
bsalomon821e10e2015-06-04 14:15:33 -0700821 if (FLAGS_pre_log) {
822 SkDebugf("\nRunning %s->%s", name.c_str(), task->sink.tag);
823 }
djsollen54416de2015-04-03 07:24:48 -0700824 start(task->sink.tag, task->src.tag, task->src.options, name.c_str());
mtkleinb9eb4ac2015-02-02 18:26:03 -0800825 Error err = task->sink->draw(*task->src, &bitmap, &stream, &log);
mtklein748ca3b2015-01-15 10:56:12 -0800826 if (!err.isEmpty()) {
mtklein4089ef72015-03-05 08:40:28 -0800827 timer.end();
828 if (err.isFatal()) {
djsollen54416de2015-04-03 07:24:48 -0700829 fail(SkStringPrintf("%s %s %s %s: %s",
mtklein4089ef72015-03-05 08:40:28 -0800830 task->sink.tag,
msarett9e707a02015-09-01 14:57:57 -0700831 task->src.tag.c_str(),
832 task->src.options.c_str(),
mtklein4089ef72015-03-05 08:40:28 -0800833 name.c_str(),
834 err.c_str()));
mtkleinb37cb412015-03-18 05:27:14 -0700835 } else {
836 note.appendf(" (skipped: %s)", err.c_str());
mtklein4089ef72015-03-05 08:40:28 -0800837 }
djsollen54416de2015-04-03 07:24:48 -0700838 done(timer.fWall, task->sink.tag, task->src.tag, task->src.options,
839 name, note, log);
mtklein4089ef72015-03-05 08:40:28 -0800840 return;
mtklein748ca3b2015-01-15 10:56:12 -0800841 }
mtklein62bd1a62015-01-27 14:46:26 -0800842 SkAutoTDelete<SkStreamAsset> data(stream.detachAsStream());
843
844 SkString md5;
845 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
846 SkMD5 hash;
847 if (data->getLength()) {
848 hash.writeStream(data, data->getLength());
849 data->rewind();
850 } else {
mtklein38408462015-07-08 07:25:27 -0700851 // If we're BGRA (Linux, Windows), swizzle over to RGBA (Mac, Android).
852 // This helps eliminate multiple 0-pixel-diff hashes on gold.skia.org.
853 // (Android's general slow speed breaks the tie arbitrarily in RGBA's favor.)
854 // We might consider promoting 565 to RGBA too.
855 if (bitmap.colorType() == kBGRA_8888_SkColorType) {
856 SkBitmap swizzle;
857 SkAssertResult(bitmap.copyTo(&swizzle, kRGBA_8888_SkColorType));
858 hash.write(swizzle.getPixels(), swizzle.getSize());
859 } else {
860 hash.write(bitmap.getPixels(), bitmap.getSize());
861 }
mtklein62bd1a62015-01-27 14:46:26 -0800862 }
863 SkMD5::Digest digest;
864 hash.finish(digest);
865 for (int i = 0; i < 16; i++) {
866 md5.appendf("%02x", digest.data[i]);
867 }
868 }
869
870 if (!FLAGS_readPath.isEmpty() &&
msarett9e707a02015-09-01 14:57:57 -0700871 !gGold.contains(Gold(task->sink.tag, task->src.tag.c_str(),
872 task->src.options.c_str(), name, md5))) {
djsollen54416de2015-04-03 07:24:48 -0700873 fail(SkStringPrintf("%s not found for %s %s %s %s in %s",
mtklein62bd1a62015-01-27 14:46:26 -0800874 md5.c_str(),
875 task->sink.tag,
msarett9e707a02015-09-01 14:57:57 -0700876 task->src.tag.c_str(),
877 task->src.options.c_str(),
mtklein62bd1a62015-01-27 14:46:26 -0800878 name.c_str(),
879 FLAGS_readPath[0]));
880 }
881
mtkleinb0531a72015-04-07 13:38:48 -0700882 if (!FLAGS_writePath.isEmpty()) {
mtklein748ca3b2015-01-15 10:56:12 -0800883 const char* ext = task->sink->fileExtension();
mtklein62bd1a62015-01-27 14:46:26 -0800884 if (data->getLength()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700885 WriteToDisk(*task, md5, ext, data, data->getLength(), nullptr);
halcanary022afb82015-01-30 11:00:12 -0800886 SkASSERT(bitmap.drawsNothing());
887 } else if (!bitmap.drawsNothing()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700888 WriteToDisk(*task, md5, ext, nullptr, 0, &bitmap);
mtklein748ca3b2015-01-15 10:56:12 -0800889 }
890 }
891 }
892 timer.end();
msarett9e707a02015-09-01 14:57:57 -0700893 done(timer.fWall, task->sink.tag, task->src.tag.c_str(), task->src.options.c_str(), name,
894 note, log);
mtklein748ca3b2015-01-15 10:56:12 -0800895 }
896
897 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -0800898 SkString md5,
899 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -0800900 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -0800901 const SkBitmap* bitmap) {
mtklein748ca3b2015-01-15 10:56:12 -0800902 JsonWriter::BitmapResult result;
djsollen54416de2015-04-03 07:24:48 -0700903 result.name = task.src->name();
904 result.config = task.sink.tag;
905 result.sourceType = task.src.tag;
906 result.sourceOptions = task.src.options;
907 result.ext = ext;
908 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -0800909 JsonWriter::AddBitmapResult(result);
910
mtkleinb0531a72015-04-07 13:38:48 -0700911 // If an MD5 is uninteresting, we want it noted in the JSON file,
912 // but don't want to dump it out as a .png (or whatever ext is).
913 if (gUninterestingHashes.contains(md5)) {
914 return;
915 }
916
mtklein748ca3b2015-01-15 10:56:12 -0800917 const char* dir = FLAGS_writePath[0];
918 if (0 == strcmp(dir, "@")) { // Needed for iOS.
919 dir = FLAGS_resourcePath[0];
920 }
921 sk_mkdir(dir);
922
923 SkString path;
924 if (FLAGS_nameByHash) {
925 path = SkOSPath::Join(dir, result.md5.c_str());
926 path.append(".");
927 path.append(ext);
928 if (sk_exists(path.c_str())) {
929 return; // Content-addressed. If it exists already, we're done.
930 }
931 } else {
932 path = SkOSPath::Join(dir, task.sink.tag);
933 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -0700934 path = SkOSPath::Join(path.c_str(), task.src.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800935 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -0700936 if (strcmp(task.src.options.c_str(), "") != 0) {
937 path = SkOSPath::Join(path.c_str(), task.src.options.c_str());
djsollen54416de2015-04-03 07:24:48 -0700938 sk_mkdir(path.c_str());
939 }
mtklein748ca3b2015-01-15 10:56:12 -0800940 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
941 path.append(".");
942 path.append(ext);
943 }
944
mtklein748ca3b2015-01-15 10:56:12 -0800945 if (bitmap) {
mtkleina5114d72015-08-24 13:27:01 -0700946 if (!dump_png(*bitmap, path.c_str(), result.md5.c_str())) {
mtklein748ca3b2015-01-15 10:56:12 -0800947 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
948 return;
949 }
950 } else {
mtkleina5114d72015-08-24 13:27:01 -0700951 SkFILEWStream file(path.c_str());
952 if (!file.isValid()) {
953 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
954 return;
955 }
mtklein748ca3b2015-01-15 10:56:12 -0800956 if (!file.writeStream(data, len)) {
957 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
958 return;
959 }
960 }
961 }
962};
963
964// Run all tasks in the same enclave serially on the same thread.
965// They can't possibly run concurrently with each other.
966static void run_enclave(SkTArray<Task>* tasks) {
967 for (int i = 0; i < tasks->count(); i++) {
968 Task::Run(tasks->begin() + i);
969 }
970}
971
972/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
973
974// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
975
mtklein55e88b22015-01-21 15:50:13 -0800976static SkTDArray<skiatest::Test> gThreadedTests, gGPUTests;
mtklein748ca3b2015-01-15 10:56:12 -0800977
978static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -0800979 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -0800980 return;
981 }
mtklein6393c062015-04-27 08:45:01 -0700982 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r; r = r->next()) {
983 if (!in_shard()) {
984 continue;
985 }
halcanary87f3ba42015-01-20 09:30:20 -0800986 // Despite its name, factory() is returning a reference to
987 // link-time static const POD data.
988 const skiatest::Test& test = r->factory();
989 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -0800990 continue;
991 }
halcanary87f3ba42015-01-20 09:30:20 -0800992 if (test.needsGpu && gpu_supported()) {
mtklein55e88b22015-01-21 15:50:13 -0800993 (FLAGS_gpu_threading ? gThreadedTests : gGPUTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -0800994 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein55e88b22015-01-21 15:50:13 -0800995 gThreadedTests.push(test);
mtklein82d28432015-01-15 12:46:02 -0800996 }
mtklein748ca3b2015-01-15 10:56:12 -0800997 }
998}
999
halcanary87f3ba42015-01-20 09:30:20 -08001000static void run_test(skiatest::Test* test) {
1001 struct : public skiatest::Reporter {
mtklein36352bf2015-03-25 18:17:31 -07001002 void reportFailed(const skiatest::Failure& failure) override {
halcanary87f3ba42015-01-20 09:30:20 -08001003 fail(failure.toString());
1004 JsonWriter::AddTestFailure(failure);
1005 }
mtklein36352bf2015-03-25 18:17:31 -07001006 bool allowExtendedTest() const override {
halcanary87f3ba42015-01-20 09:30:20 -08001007 return FLAGS_pathOpsExtended;
1008 }
mtklein36352bf2015-03-25 18:17:31 -07001009 bool verbose() const override { return FLAGS_veryVerbose; }
halcanary87f3ba42015-01-20 09:30:20 -08001010 } reporter;
djsollen824996a2015-06-12 12:06:22 -07001011
1012 SkString note;
1013 SkString whyBlacklisted = is_blacklisted("_", "tests", "_", test->name);
1014 if (!whyBlacklisted.isEmpty()) {
1015 note.appendf(" (--blacklist %s)", whyBlacklisted.c_str());
1016 }
1017
mtklein748ca3b2015-01-15 10:56:12 -08001018 WallTimer timer;
1019 timer.start();
djsollen824996a2015-06-12 12:06:22 -07001020 if (!FLAGS_dryRun && whyBlacklisted.isEmpty()) {
djsollen54416de2015-04-03 07:24:48 -07001021 start("unit", "test", "", test->name);
mtklein55e88b22015-01-21 15:50:13 -08001022 GrContextFactory factory;
bsalomon821e10e2015-06-04 14:15:33 -07001023 if (FLAGS_pre_log) {
1024 SkDebugf("\nRunning test %s", test->name);
1025 }
mtklein55e88b22015-01-21 15:50:13 -08001026 test->proc(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -08001027 }
1028 timer.end();
djsollen824996a2015-06-12 12:06:22 -07001029 done(timer.fWall, "unit", "test", "", test->name, note, "");
mtklein748ca3b2015-01-15 10:56:12 -08001030}
1031
1032/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1033
mtklein55e88b22015-01-21 15:50:13 -08001034// If we're isolating all GPU-bound work to one thread (the default), this function runs all that.
1035static void run_enclave_and_gpu_tests(SkTArray<Task>* tasks) {
1036 run_enclave(tasks);
1037 for (int i = 0; i < gGPUTests.count(); i++) {
1038 run_test(&gGPUTests[i]);
1039 }
1040}
1041
mtkleinde6fc2b2015-03-12 06:28:54 -07001042// Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung.
1043// This prints something every once in a while so that it knows we're still working.
mtklein2e1c47e2015-03-12 07:16:56 -07001044static void start_keepalive() {
1045 struct Loop {
1046 static void forever(void*) {
1047 for (;;) {
1048 static const int kSec = 300;
1049 #if defined(SK_BUILD_FOR_WIN)
1050 Sleep(kSec * 1000);
1051 #else
1052 sleep(kSec);
1053 #endif
mtkleinb37cb412015-03-18 05:27:14 -07001054 SkString running;
1055 {
1056 SkAutoMutexAcquire lock(gRunningMutex);
1057 for (int i = 0; i < gRunning.count(); i++) {
1058 running.appendf("\n\t%s", gRunning[i].c_str());
1059 }
1060 }
1061 SkDebugf("\nCurrently running:%s\n", running.c_str());
mtklein2e1c47e2015-03-12 07:16:56 -07001062 }
1063 }
1064 };
1065 static SkThread* intentionallyLeaked = new SkThread(Loop::forever);
1066 intentionallyLeaked->start();
mtkleinde6fc2b2015-03-12 06:28:54 -07001067}
1068
caryclark83ca6282015-06-10 09:31:09 -07001069#define PORTABLE_FONT_PREFIX "Toy Liberation "
1070
1071static SkTypeface* create_from_name(const char familyName[], SkTypeface::Style style) {
1072 if (familyName && strlen(familyName) > sizeof(PORTABLE_FONT_PREFIX)
1073 && !strncmp(familyName, PORTABLE_FONT_PREFIX, sizeof(PORTABLE_FONT_PREFIX) - 1)) {
caryclark1818acb2015-07-24 12:09:25 -07001074 return sk_tool_utils::create_portable_typeface(familyName, style);
caryclark83ca6282015-06-10 09:31:09 -07001075 }
halcanary96fcdcc2015-08-27 07:41:13 -07001076 return nullptr;
caryclark83ca6282015-06-10 09:31:09 -07001077}
1078
1079#undef PORTABLE_FONT_PREFIX
1080
1081extern SkTypeface* (*gCreateTypefaceDelegate)(const char [], SkTypeface::Style );
1082
jcgregorio3b27ade2014-11-13 08:06:40 -08001083int dm_main();
caryclark17f0b6d2014-07-22 10:15:34 -07001084int dm_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -08001085 SetupCrashHandler();
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +00001086 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -07001087 SkTaskGroup::Enabler enabled(FLAGS_threads);
caryclark83ca6282015-06-10 09:31:09 -07001088 gCreateTypefaceDelegate = &create_from_name;
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +00001089
mtklein2e1c47e2015-03-12 07:16:56 -07001090 start_keepalive();
mtkleinde6fc2b2015-03-12 06:28:54 -07001091
mtklein62bd1a62015-01-27 14:46:26 -08001092 gather_gold();
borenet09ed4802015-04-03 14:15:33 -07001093 gather_uninteresting_hashes();
mtklein62bd1a62015-01-27 14:46:26 -08001094
mtklein748ca3b2015-01-15 10:56:12 -08001095 gather_srcs();
1096 gather_sinks();
1097 gather_tests();
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +00001098
mtklein55e88b22015-01-21 15:50:13 -08001099 gPending = gSrcs.count() * gSinks.count() + gThreadedTests.count() + gGPUTests.count();
mtklein748ca3b2015-01-15 10:56:12 -08001100 SkDebugf("%d srcs * %d sinks + %d tests == %d tasks\n",
mtklein55e88b22015-01-21 15:50:13 -08001101 gSrcs.count(), gSinks.count(), gThreadedTests.count() + gGPUTests.count(), gPending);
mtklein748ca3b2015-01-15 10:56:12 -08001102
1103 // We try to exploit as much parallelism as is safe. Most Src/Sink pairs run on any thread,
1104 // but Sinks that identify as part of a particular enclave run serially on a single thread.
mtklein82d28432015-01-15 12:46:02 -08001105 // CPU tests run on any thread. GPU tests depend on --gpu_threading.
mtklein748ca3b2015-01-15 10:56:12 -08001106 SkTArray<Task> enclaves[kNumEnclaves];
1107 for (int j = 0; j < gSinks.count(); j++) {
1108 SkTArray<Task>& tasks = enclaves[gSinks[j]->enclave()];
1109 for (int i = 0; i < gSrcs.count(); i++) {
1110 tasks.push_back(Task(gSrcs[i], gSinks[j]));
1111 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001112 }
1113
mtklein748ca3b2015-01-15 10:56:12 -08001114 SkTaskGroup tg;
mtklein55e88b22015-01-21 15:50:13 -08001115 tg.batch(run_test, gThreadedTests.begin(), gThreadedTests.count());
1116 for (int i = 0; i < kNumEnclaves; i++) {
1117 switch(i) {
1118 case kAnyThread_Enclave:
1119 tg.batch(Task::Run, enclaves[i].begin(), enclaves[i].count());
1120 break;
1121 case kGPU_Enclave:
1122 tg.add(run_enclave_and_gpu_tests, &enclaves[i]);
1123 break;
1124 default:
1125 tg.add(run_enclave, &enclaves[i]);
1126 break;
mtklein82d28432015-01-15 12:46:02 -08001127 }
mtklein55e88b22015-01-21 15:50:13 -08001128 }
mtklein748ca3b2015-01-15 10:56:12 -08001129 tg.wait();
mtklein748ca3b2015-01-15 10:56:12 -08001130 // At this point we're back in single-threaded land.
caryclarkf53ce802015-06-15 06:48:30 -07001131 sk_tool_utils::release_portable_typefaces();
mtklein@google.comd36522d2013-10-16 13:02:15 +00001132
mtklein2f64eec2015-01-15 14:20:41 -08001133 SkDebugf("\n");
mtklein748ca3b2015-01-15 10:56:12 -08001134 if (gFailures.count() > 0) {
1135 SkDebugf("Failures:\n");
1136 for (int i = 0; i < gFailures.count(); i++) {
mtklein9dc09102015-01-15 15:47:33 -08001137 SkDebugf("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001138 }
1139 SkDebugf("%d failures\n", gFailures.count());
1140 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001141 }
mtklein748ca3b2015-01-15 10:56:12 -08001142 if (gPending > 0) {
1143 SkDebugf("Hrm, we didn't seem to run everything we intended to! Please file a bug.\n");
1144 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001145 }
halcanary7a14b312015-10-01 07:28:13 -07001146 #ifdef SK_PDF_IMAGE_STATS
1147 SkPDFImageDumpStats();
1148 #endif // SK_PDF_IMAGE_STATS
mtklein748ca3b2015-01-15 10:56:12 -08001149 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +00001150}
jcgregorio3b27ade2014-11-13 08:06:40 -08001151
borenet48087572015-04-02 12:16:36 -07001152#if !defined(SK_BUILD_FOR_IOS)
jcgregorio3b27ade2014-11-13 08:06:40 -08001153int main(int argc, char** argv) {
1154 SkCommandLineFlags::Parse(argc, argv);
1155 return dm_main();
1156}
1157#endif