blob: f1f54d3f4c4b6579e48fb64973c6474fb24a5502 [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 }
msarett26ad17b2015-10-22 07:29:19 -0700395 case SkBitmapRegionDecoderInterface::kAndroidCodec_Strategy:
396 switch (dstColorType) {
397 case CodecSrc::kGetFromCanvas_DstColorType:
398 case CodecSrc::kIndex8_Always_DstColorType:
399 case CodecSrc::kGrayscale_Always_DstColorType:
400 return true;
401 default:
402 return false;
403 }
msaretta5783ae2015-09-08 15:35:32 -0700404 default:
405 SkASSERT(false);
406 return false;
407 }
408}
409
410static void push_brd_src(Path path, SkBitmapRegionDecoderInterface::Strategy strategy,
411 CodecSrc::DstColorType dstColorType, BRDSrc::Mode mode, uint32_t sampleSize) {
412 SkString folder;
413 switch (strategy) {
414 case SkBitmapRegionDecoderInterface::kCanvas_Strategy:
415 folder.append("brd_canvas");
416 break;
417 case SkBitmapRegionDecoderInterface::kOriginal_Strategy:
418 folder.append("brd_sample");
419 break;
msarett26ad17b2015-10-22 07:29:19 -0700420 case SkBitmapRegionDecoderInterface::kAndroidCodec_Strategy:
421 folder.append("brd_android_codec");
422 break;
msaretta5783ae2015-09-08 15:35:32 -0700423 default:
424 SkASSERT(false);
425 return;
426 }
427
428 switch (mode) {
429 case BRDSrc::kFullImage_Mode:
430 break;
431 case BRDSrc::kDivisor_Mode:
432 folder.append("_divisor");
433 break;
434 default:
435 SkASSERT(false);
436 return;
437 }
438
439 switch (dstColorType) {
440 case CodecSrc::kGetFromCanvas_DstColorType:
441 break;
442 case CodecSrc::kIndex8_Always_DstColorType:
443 folder.append("_kIndex");
444 break;
445 case CodecSrc::kGrayscale_Always_DstColorType:
446 folder.append("_kGray");
447 break;
448 default:
449 SkASSERT(false);
450 return;
451 }
452
453 if (1 != sampleSize) {
msarett7f691442015-09-22 11:56:16 -0700454 folder.appendf("_%.3f", get_scale_from_sample_size(sampleSize));
msaretta5783ae2015-09-08 15:35:32 -0700455 }
456
457 BRDSrc* src = new BRDSrc(path, strategy, mode, dstColorType, sampleSize);
458 push_src("image", folder, src);
459}
460
461static void push_brd_srcs(Path path) {
462
463 const SkBitmapRegionDecoderInterface::Strategy strategies[] = {
464 SkBitmapRegionDecoderInterface::kCanvas_Strategy,
msarett26ad17b2015-10-22 07:29:19 -0700465 SkBitmapRegionDecoderInterface::kOriginal_Strategy,
466 SkBitmapRegionDecoderInterface::kAndroidCodec_Strategy,
msaretta5783ae2015-09-08 15:35:32 -0700467 };
468
msarett6efbe052015-09-11 09:01:16 -0700469 const uint32_t sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
470
msaretta5783ae2015-09-08 15:35:32 -0700471 // We will only test to one backend (8888), but we will test all of the
472 // color types that we need to decode to on this backend.
473 const CodecSrc::DstColorType dstColorTypes[] = {
msarett26ad17b2015-10-22 07:29:19 -0700474 CodecSrc::kGetFromCanvas_DstColorType,
475 CodecSrc::kIndex8_Always_DstColorType,
476 CodecSrc::kGrayscale_Always_DstColorType,
msaretta5783ae2015-09-08 15:35:32 -0700477 };
478
479 const BRDSrc::Mode modes[] = {
msarett26ad17b2015-10-22 07:29:19 -0700480 BRDSrc::kFullImage_Mode,
481 BRDSrc::kDivisor_Mode,
msaretta5783ae2015-09-08 15:35:32 -0700482 };
483
msaretta5783ae2015-09-08 15:35:32 -0700484 for (SkBitmapRegionDecoderInterface::Strategy strategy : strategies) {
msarett6efbe052015-09-11 09:01:16 -0700485
msaretta5783ae2015-09-08 15:35:32 -0700486 // We disable png testing for kOriginal_Strategy because the implementation leaks
487 // memory in our forked libpng.
488 // TODO (msarett): Decide if we want to test pngs in this mode and how we might do this.
489 if (SkBitmapRegionDecoderInterface::kOriginal_Strategy == strategy &&
490 (path.endsWith(".png") || path.endsWith(".PNG"))) {
491 continue;
492 }
msarett6efbe052015-09-11 09:01:16 -0700493 for (uint32_t sampleSize : sampleSizes) {
494
495 // kOriginal_Strategy does not work for jpegs that are scaled to non-powers of two.
496 // We don't need to test this. We know it doesn't work, and it causes images with
497 // uninitialized memory to show up on Gold.
498 if (SkBitmapRegionDecoderInterface::kOriginal_Strategy == strategy &&
499 (path.endsWith(".jpg") || path.endsWith(".JPG") ||
500 path.endsWith(".jpeg") || path.endsWith(".JPEG")) && !SkIsPow2(sampleSize)) {
501 continue;
502 }
503 for (CodecSrc::DstColorType dstColorType : dstColorTypes) {
504 if (brd_color_type_supported(strategy, dstColorType)) {
505 for (BRDSrc::Mode mode : modes) {
msaretta5783ae2015-09-08 15:35:32 -0700506 push_brd_src(path, strategy, dstColorType, mode, sampleSize);
507 }
508 }
509 }
510 }
511 }
512}
513
514static bool brd_supported(const char* ext) {
msarett8c8f22a2015-04-01 06:58:48 -0700515 static const char* const exts[] = {
msaretta5783ae2015-09-08 15:35:32 -0700516 "jpg", "jpeg", "png", "webp",
517 "JPG", "JPEG", "PNG", "WEBP",
msarett8c8f22a2015-04-01 06:58:48 -0700518 };
519
520 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
521 if (0 == strcmp(exts[i], ext)) {
522 return true;
523 }
524 }
525 return false;
scroggo9b77ddd2015-03-19 06:03:39 -0700526}
527
mtklein748ca3b2015-01-15 10:56:12 -0800528static void gather_srcs() {
529 for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) {
djsollen54416de2015-04-03 07:24:48 -0700530 push_src("gm", "", new GMSrc(r->factory()));
mtklein748ca3b2015-01-15 10:56:12 -0800531 }
halcanaryfc37ad12015-01-30 07:31:19 -0800532 for (int i = 0; i < FLAGS_skps.count(); i++) {
533 const char* path = FLAGS_skps[i];
534 if (sk_isdir(path)) {
535 SkOSFile::Iter it(path, "skp");
536 for (SkString file; it.next(&file); ) {
djsollen54416de2015-04-03 07:24:48 -0700537 push_src("skp", "", new SKPSrc(SkOSPath::Join(path, file.c_str())));
halcanaryfc37ad12015-01-30 07:31:19 -0800538 }
539 } else {
djsollen54416de2015-04-03 07:24:48 -0700540 push_src("skp", "", new SKPSrc(path));
mtklein114c3cd2015-01-15 10:15:02 -0800541 }
542 }
halcanary23b03c32015-01-30 09:58:58 -0800543 static const char* const exts[] = {
544 "bmp", "gif", "jpg", "jpeg", "png", "webp", "ktx", "astc", "wbmp", "ico",
545 "BMP", "GIF", "JPG", "JPEG", "PNG", "WEBP", "KTX", "ASTC", "WBMP", "ICO",
546 };
547 for (int i = 0; i < FLAGS_images.count(); i++) {
548 const char* flag = FLAGS_images[i];
549 if (sk_isdir(flag)) {
550 for (size_t j = 0; j < SK_ARRAY_COUNT(exts); j++) {
551 SkOSFile::Iter it(flag, exts[j]);
552 for (SkString file; it.next(&file); ) {
553 SkString path = SkOSPath::Join(flag, file.c_str());
djsollen54416de2015-04-03 07:24:48 -0700554 push_src("image", "decode", new ImageSrc(path)); // Decode entire image
555 push_src("image", "subset", new ImageSrc(path, 2)); // Decode into 2x2 subsets
msaretta5783ae2015-09-08 15:35:32 -0700556 push_codec_srcs(path);
557 if (brd_supported(exts[j])) {
558 push_brd_srcs(path);
scroggo9b77ddd2015-03-19 06:03:39 -0700559 }
halcanary23b03c32015-01-30 09:58:58 -0800560 }
mtklein114c3cd2015-01-15 10:15:02 -0800561 }
halcanary23b03c32015-01-30 09:58:58 -0800562 } else if (sk_exists(flag)) {
563 // assume that FLAGS_images[i] is a valid image if it is a file.
djsollen54416de2015-04-03 07:24:48 -0700564 push_src("image", "decode", new ImageSrc(flag)); // Decode entire image.
565 push_src("image", "subset", new ImageSrc(flag, 2)); // Decode into 2 x 2 subsets
msarett438b2ad2015-04-09 12:43:10 -0700566 push_codec_srcs(flag);
msaretta5783ae2015-09-08 15:35:32 -0700567 push_brd_srcs(flag);
mtklein709d2c32015-01-15 08:30:25 -0800568 }
mtklein709d2c32015-01-15 08:30:25 -0800569 }
570}
571
mtklein748ca3b2015-01-15 10:56:12 -0800572static GrGLStandard get_gpu_api() {
573 if (FLAGS_gpuAPI.contains("gl")) { return kGL_GrGLStandard; }
574 if (FLAGS_gpuAPI.contains("gles")) { return kGLES_GrGLStandard; }
575 return kNone_GrGLStandard;
mtklein709d2c32015-01-15 08:30:25 -0800576}
577
mtklein748ca3b2015-01-15 10:56:12 -0800578static void push_sink(const char* tag, Sink* s) {
579 SkAutoTDelete<Sink> sink(s);
580 if (!FLAGS_config.contains(tag)) {
581 return;
mtklein114c3cd2015-01-15 10:15:02 -0800582 }
mtkleine0effd62015-07-29 06:37:28 -0700583 // Try a simple Src as a canary. If it fails, skip this sink.
mtklein748ca3b2015-01-15 10:56:12 -0800584 struct : public Src {
mtkleine0effd62015-07-29 06:37:28 -0700585 Error draw(SkCanvas* c) const override {
586 c->drawRect(SkRect::MakeWH(1,1), SkPaint());
587 return "";
588 }
mtklein36352bf2015-03-25 18:17:31 -0700589 SkISize size() const override { return SkISize::Make(16, 16); }
mtkleine0effd62015-07-29 06:37:28 -0700590 Name name() const override { return "justOneRect"; }
591 } justOneRect;
mtklein114c3cd2015-01-15 10:15:02 -0800592
mtklein748ca3b2015-01-15 10:56:12 -0800593 SkBitmap bitmap;
594 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800595 SkString log;
mtkleine0effd62015-07-29 06:37:28 -0700596 Error err = sink->draw(justOneRect, &bitmap, &stream, &log);
mtklein4089ef72015-03-05 08:40:28 -0800597 if (err.isFatal()) {
mtklein05641a52015-04-21 10:49:13 -0700598 SkDebugf("Could not run %s: %s\n", tag, err.c_str());
599 exit(1);
mtklein114c3cd2015-01-15 10:15:02 -0800600 }
601
mtkleine0effd62015-07-29 06:37:28 -0700602 TaggedSink& ts = gSinks.push_back();
mtklein748ca3b2015-01-15 10:56:12 -0800603 ts.reset(sink.detach());
604 ts.tag = tag;
605}
606
607static bool gpu_supported() {
608#if SK_SUPPORT_GPU
609 return FLAGS_gpu;
610#else
611 return false;
612#endif
613}
614
615static Sink* create_sink(const char* tag) {
616#define SINK(t, sink, ...) if (0 == strcmp(t, tag)) { return new sink(__VA_ARGS__); }
617 if (gpu_supported()) {
mtklein82d28432015-01-15 12:46:02 -0800618 typedef GrContextFactory Gr;
mtklein748ca3b2015-01-15 10:56:12 -0800619 const GrGLStandard api = get_gpu_api();
hendrikw885bf092015-08-27 10:38:39 -0700620 SINK("gpunull", GPUSink, Gr::kNull_GLContextType, api, 0, false, FLAGS_gpu_threading);
621 SINK("gpudebug", GPUSink, Gr::kDebug_GLContextType, api, 0, false, FLAGS_gpu_threading);
622 SINK("gpu", GPUSink, Gr::kNative_GLContextType, api, 0, false, FLAGS_gpu_threading);
623 SINK("gpudft", GPUSink, Gr::kNative_GLContextType, api, 0, true, FLAGS_gpu_threading);
624 SINK("msaa4", GPUSink, Gr::kNative_GLContextType, api, 4, false, FLAGS_gpu_threading);
625 SINK("msaa16", GPUSink, Gr::kNative_GLContextType, api, 16, false, FLAGS_gpu_threading);
bsalomon90e8ab72015-09-08 10:26:51 -0700626 SINK("nvprmsaa4", GPUSink, Gr::kNVPR_GLContextType, api, 4, true, FLAGS_gpu_threading);
627 SINK("nvprmsaa16", GPUSink, Gr::kNVPR_GLContextType, api, 16, true, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800628 #if SK_ANGLE
hendrikw885bf092015-08-27 10:38:39 -0700629 SINK("angle", GPUSink, Gr::kANGLE_GLContextType, api, 0, false, FLAGS_gpu_threading);
hendrikweddbefb2015-09-11 13:07:29 -0700630 SINK("angle-gl", GPUSink, Gr::kANGLE_GL_GLContextType, api, 0, false, FLAGS_gpu_threading);
hendrikw885bf092015-08-27 10:38:39 -0700631 #endif
632 #if SK_COMMAND_BUFFER
633 SINK("commandbuffer", GPUSink, Gr::kCommandBuffer_GLContextType, api, 0, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800634 #endif
635 #if SK_MESA
hendrikw885bf092015-08-27 10:38:39 -0700636 SINK("mesa", GPUSink, Gr::kMESA_GLContextType, api, 0, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800637 #endif
mtklein114c3cd2015-01-15 10:15:02 -0800638 }
mtklein748ca3b2015-01-15 10:56:12 -0800639
tomhudsoneebc39a2015-02-23 12:18:05 -0800640#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
641 SINK("hwui", HWUISink);
642#endif
643
mtklein748ca3b2015-01-15 10:56:12 -0800644 if (FLAGS_cpu) {
645 SINK("565", RasterSink, kRGB_565_SkColorType);
646 SINK("8888", RasterSink, kN32_SkColorType);
halcanaryc11c62f2015-09-28 11:51:54 -0700647 SINK("pdf", PDFSink, "Pdfium");
648 SINK("pdf_poppler", PDFSink, "Poppler");
mtklein9c3f17d2015-01-28 11:35:18 -0800649 SINK("skp", SKPSink);
mtklein8a4527e2015-01-31 20:00:58 -0800650 SINK("svg", SVGSink);
651 SINK("null", NullSink);
halcanary47ef4d52015-03-03 09:13:09 -0800652 SINK("xps", XPSSink);
mtklein748ca3b2015-01-15 10:56:12 -0800653 }
654#undef SINK
halcanary96fcdcc2015-08-27 07:41:13 -0700655 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800656}
657
mtklein748ca3b2015-01-15 10:56:12 -0800658static Sink* create_via(const char* tag, Sink* wrapped) {
659#define VIA(t, via, ...) if (0 == strcmp(t, tag)) { return new via(__VA_ARGS__); }
halcanary96fcdcc2015-08-27 07:41:13 -0700660 VIA("twice", ViaTwice, wrapped);
661 VIA("pipe", ViaPipe, wrapped);
662 VIA("serialize", ViaSerialization, wrapped);
663 VIA("2ndpic", ViaSecondPicture, wrapped);
mtkleind31c13d2015-05-05 12:59:56 -0700664 VIA("sp", ViaSingletonPictures, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700665 VIA("tiles", ViaTiles, 256, 256, nullptr, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800666 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
mtklein2e2ea382015-10-16 10:29:41 -0700667 VIA("remote", ViaRemote, false, wrapped);
668 VIA("remote_cache", ViaRemote, true, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800669
mtkleind603b222015-02-17 11:13:33 -0800670 if (FLAGS_matrix.count() == 4) {
mtklein748ca3b2015-01-15 10:56:12 -0800671 SkMatrix m;
mtkleind603b222015-02-17 11:13:33 -0800672 m.reset();
673 m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
674 m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
675 m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
676 m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
677 VIA("matrix", ViaMatrix, m, wrapped);
678 VIA("upright", ViaUpright, m, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800679 }
tomhudson64de1e12015-03-05 08:01:07 -0800680
681#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
682 VIA("androidsdk", ViaAndroidSDK, wrapped);
683#endif
684
mtklein748ca3b2015-01-15 10:56:12 -0800685#undef VIA
halcanary96fcdcc2015-08-27 07:41:13 -0700686 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800687}
688
mtklein748ca3b2015-01-15 10:56:12 -0800689static void gather_sinks() {
690 for (int i = 0; i < FLAGS_config.count(); i++) {
691 const char* config = FLAGS_config[i];
692 SkTArray<SkString> parts;
693 SkStrSplit(config, "-", &parts);
694
halcanary96fcdcc2015-08-27 07:41:13 -0700695 Sink* sink = nullptr;
mtklein748ca3b2015-01-15 10:56:12 -0800696 for (int i = parts.count(); i-- > 0;) {
697 const char* part = parts[i].c_str();
halcanary96fcdcc2015-08-27 07:41:13 -0700698 Sink* next = (sink == nullptr) ? create_sink(part) : create_via(part, sink);
699 if (next == nullptr) {
mtklein748ca3b2015-01-15 10:56:12 -0800700 SkDebugf("Skipping %s: Don't understand '%s'.\n", config, part);
701 delete sink;
halcanary96fcdcc2015-08-27 07:41:13 -0700702 sink = nullptr;
mtklein748ca3b2015-01-15 10:56:12 -0800703 break;
704 }
705 sink = next;
706 }
707 if (sink) {
708 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800709 }
710 }
711}
mtklein709d2c32015-01-15 08:30:25 -0800712
mtkleina5114d72015-08-24 13:27:01 -0700713static bool dump_png(SkBitmap bitmap, const char* path, const char* md5) {
714 const int w = bitmap.width(),
715 h = bitmap.height();
716
717 // First get the bitmap into N32 color format. The next step will work only there.
718 if (bitmap.colorType() != kN32_SkColorType) {
719 SkBitmap n32;
720 if (!bitmap.copyTo(&n32, kN32_SkColorType)) {
721 return false;
722 }
723 bitmap = n32;
724 }
725
726 // Convert our N32 bitmap into unpremul RGBA for libpng.
727 SkAutoTMalloc<uint32_t> rgba(w*h);
728 if (!bitmap.readPixels(SkImageInfo::Make(w,h, kRGBA_8888_SkColorType, kUnpremul_SkAlphaType),
729 rgba, 4*w, 0,0)) {
730 return false;
731 }
732
733 // We don't need bitmap anymore. Might as well drop our ref.
mtkleinfccb77d2015-08-24 14:13:29 -0700734 bitmap.reset();
mtkleina5114d72015-08-24 13:27:01 -0700735
mtkleinc64137c2015-08-25 10:56:08 -0700736 FILE* f = fopen(path, "wb");
mtkleina5114d72015-08-24 13:27:01 -0700737 if (!f) { return false; }
738
739 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
740 if (!png) {
741 fclose(f);
742 return false;
743 }
744
745 png_infop info = png_create_info_struct(png);
746 if (!info) {
747 png_destroy_write_struct(&png, &info);
748 fclose(f);
749 return false;
750 }
751
mtkleind69ece62015-09-10 10:37:44 -0700752 SkString description;
753 description.append("Key: ");
754 for (int i = 0; i < FLAGS_key.count(); i++) {
755 description.appendf("%s ", FLAGS_key[i]);
756 }
757 description.append("Properties: ");
758 for (int i = 0; i < FLAGS_properties.count(); i++) {
759 description.appendf("%s ", FLAGS_properties[i]);
760 }
761 description.appendf("MD5: %s", md5);
762
mtkleina5114d72015-08-24 13:27:01 -0700763 png_text text[2];
764 text[0].key = (png_charp)"Author";
765 text[0].text = (png_charp)"DM dump_png()";
766 text[0].compression = PNG_TEXT_COMPRESSION_NONE;
767 text[1].key = (png_charp)"Description";
mtkleind69ece62015-09-10 10:37:44 -0700768 text[1].text = (png_charp)description.c_str();
mtkleina5114d72015-08-24 13:27:01 -0700769 text[1].compression = PNG_TEXT_COMPRESSION_NONE;
770 png_set_text(png, info, text, 2);
771
772 png_init_io(png, f);
773 png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8,
774 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
775 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
776 png_write_info(png, info);
777 for (int j = 0; j < h; j++) {
778 png_bytep row = (png_bytep)(rgba.get() + w*j);
779 png_write_rows(png, &row, 1);
780 }
781 png_write_end(png, info);
782
783 png_destroy_write_struct(&png, &info);
784 fclose(f);
785 return true;
786}
787
mtkleina2ef6422015-01-15 13:44:22 -0800788static bool match(const char* needle, const char* haystack) {
halcanary96fcdcc2015-08-27 07:41:13 -0700789 return 0 == strcmp("_", needle) || nullptr != strstr(haystack, needle);
mtkleina2ef6422015-01-15 13:44:22 -0800790}
791
djsollen54416de2015-04-03 07:24:48 -0700792static ImplicitString is_blacklisted(const char* sink, const char* src,
793 const char* srcOptions, const char* name) {
mtklein0d243ff2015-04-03 07:51:00 -0700794 for (int i = 0; i < FLAGS_blacklist.count() - 3; i += 4) {
mtkleina2ef6422015-01-15 13:44:22 -0800795 if (match(FLAGS_blacklist[i+0], sink) &&
djsollen54416de2015-04-03 07:24:48 -0700796 match(FLAGS_blacklist[i+1], src) &&
797 match(FLAGS_blacklist[i+2], srcOptions) &&
798 match(FLAGS_blacklist[i+3], name)) {
799 return SkStringPrintf("%s %s %s %s",
800 FLAGS_blacklist[i+0], FLAGS_blacklist[i+1],
801 FLAGS_blacklist[i+2], FLAGS_blacklist[i+3]);
mtkleina2ef6422015-01-15 13:44:22 -0800802 }
803 }
804 return "";
805}
806
mtklein748ca3b2015-01-15 10:56:12 -0800807// The finest-grained unit of work we can run: draw a single Src into a single Sink,
808// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
809struct Task {
mtkleine0effd62015-07-29 06:37:28 -0700810 Task(const TaggedSrc& src, const TaggedSink& sink) : src(src), sink(sink) {}
811 const TaggedSrc& src;
812 const TaggedSink& sink;
mtklein748ca3b2015-01-15 10:56:12 -0800813
814 static void Run(Task* task) {
mtkleina2ef6422015-01-15 13:44:22 -0800815 SkString name = task->src->name();
mtkleine0effd62015-07-29 06:37:28 -0700816
817 // We'll skip drawing this Src/Sink pair if:
818 // - the Src vetoes the Sink;
819 // - this Src / Sink combination is on the blacklist;
820 // - it's a dry run.
mtklein99cab4e2015-07-31 06:43:04 -0700821 SkString note(task->src->veto(task->sink->flags()) ? " (veto)" : "");
msarett9e707a02015-09-01 14:57:57 -0700822 SkString whyBlacklisted = is_blacklisted(task->sink.tag, task->src.tag.c_str(),
823 task->src.options.c_str(), name.c_str());
mtkleinb37cb412015-03-18 05:27:14 -0700824 if (!whyBlacklisted.isEmpty()) {
825 note.appendf(" (--blacklist %s)", whyBlacklisted.c_str());
826 }
mtkleine0effd62015-07-29 06:37:28 -0700827
mtkleinb9eb4ac2015-02-02 18:26:03 -0800828 SkString log;
mtklein748ca3b2015-01-15 10:56:12 -0800829 WallTimer timer;
830 timer.start();
mtkleine0effd62015-07-29 06:37:28 -0700831 if (!FLAGS_dryRun && note.isEmpty()) {
mtklein748ca3b2015-01-15 10:56:12 -0800832 SkBitmap bitmap;
833 SkDynamicMemoryWStream stream;
bsalomon821e10e2015-06-04 14:15:33 -0700834 if (FLAGS_pre_log) {
835 SkDebugf("\nRunning %s->%s", name.c_str(), task->sink.tag);
836 }
djsollen54416de2015-04-03 07:24:48 -0700837 start(task->sink.tag, task->src.tag, task->src.options, name.c_str());
mtkleinb9eb4ac2015-02-02 18:26:03 -0800838 Error err = task->sink->draw(*task->src, &bitmap, &stream, &log);
mtklein748ca3b2015-01-15 10:56:12 -0800839 if (!err.isEmpty()) {
mtklein4089ef72015-03-05 08:40:28 -0800840 timer.end();
841 if (err.isFatal()) {
djsollen54416de2015-04-03 07:24:48 -0700842 fail(SkStringPrintf("%s %s %s %s: %s",
mtklein4089ef72015-03-05 08:40:28 -0800843 task->sink.tag,
msarett9e707a02015-09-01 14:57:57 -0700844 task->src.tag.c_str(),
845 task->src.options.c_str(),
mtklein4089ef72015-03-05 08:40:28 -0800846 name.c_str(),
847 err.c_str()));
mtkleinb37cb412015-03-18 05:27:14 -0700848 } else {
849 note.appendf(" (skipped: %s)", err.c_str());
mtklein4089ef72015-03-05 08:40:28 -0800850 }
djsollen54416de2015-04-03 07:24:48 -0700851 done(timer.fWall, task->sink.tag, task->src.tag, task->src.options,
852 name, note, log);
mtklein4089ef72015-03-05 08:40:28 -0800853 return;
mtklein748ca3b2015-01-15 10:56:12 -0800854 }
mtklein62bd1a62015-01-27 14:46:26 -0800855 SkAutoTDelete<SkStreamAsset> data(stream.detachAsStream());
856
857 SkString md5;
858 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
859 SkMD5 hash;
860 if (data->getLength()) {
861 hash.writeStream(data, data->getLength());
862 data->rewind();
863 } else {
mtklein38408462015-07-08 07:25:27 -0700864 // If we're BGRA (Linux, Windows), swizzle over to RGBA (Mac, Android).
865 // This helps eliminate multiple 0-pixel-diff hashes on gold.skia.org.
866 // (Android's general slow speed breaks the tie arbitrarily in RGBA's favor.)
867 // We might consider promoting 565 to RGBA too.
868 if (bitmap.colorType() == kBGRA_8888_SkColorType) {
869 SkBitmap swizzle;
870 SkAssertResult(bitmap.copyTo(&swizzle, kRGBA_8888_SkColorType));
871 hash.write(swizzle.getPixels(), swizzle.getSize());
872 } else {
873 hash.write(bitmap.getPixels(), bitmap.getSize());
874 }
mtklein62bd1a62015-01-27 14:46:26 -0800875 }
876 SkMD5::Digest digest;
877 hash.finish(digest);
878 for (int i = 0; i < 16; i++) {
879 md5.appendf("%02x", digest.data[i]);
880 }
881 }
882
883 if (!FLAGS_readPath.isEmpty() &&
msarett9e707a02015-09-01 14:57:57 -0700884 !gGold.contains(Gold(task->sink.tag, task->src.tag.c_str(),
885 task->src.options.c_str(), name, md5))) {
djsollen54416de2015-04-03 07:24:48 -0700886 fail(SkStringPrintf("%s not found for %s %s %s %s in %s",
mtklein62bd1a62015-01-27 14:46:26 -0800887 md5.c_str(),
888 task->sink.tag,
msarett9e707a02015-09-01 14:57:57 -0700889 task->src.tag.c_str(),
890 task->src.options.c_str(),
mtklein62bd1a62015-01-27 14:46:26 -0800891 name.c_str(),
892 FLAGS_readPath[0]));
893 }
894
mtkleinb0531a72015-04-07 13:38:48 -0700895 if (!FLAGS_writePath.isEmpty()) {
mtklein748ca3b2015-01-15 10:56:12 -0800896 const char* ext = task->sink->fileExtension();
mtklein62bd1a62015-01-27 14:46:26 -0800897 if (data->getLength()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700898 WriteToDisk(*task, md5, ext, data, data->getLength(), nullptr);
halcanary022afb82015-01-30 11:00:12 -0800899 SkASSERT(bitmap.drawsNothing());
900 } else if (!bitmap.drawsNothing()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700901 WriteToDisk(*task, md5, ext, nullptr, 0, &bitmap);
mtklein748ca3b2015-01-15 10:56:12 -0800902 }
903 }
904 }
905 timer.end();
msarett9e707a02015-09-01 14:57:57 -0700906 done(timer.fWall, task->sink.tag, task->src.tag.c_str(), task->src.options.c_str(), name,
907 note, log);
mtklein748ca3b2015-01-15 10:56:12 -0800908 }
909
910 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -0800911 SkString md5,
912 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -0800913 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -0800914 const SkBitmap* bitmap) {
mtklein748ca3b2015-01-15 10:56:12 -0800915 JsonWriter::BitmapResult result;
djsollen54416de2015-04-03 07:24:48 -0700916 result.name = task.src->name();
917 result.config = task.sink.tag;
918 result.sourceType = task.src.tag;
919 result.sourceOptions = task.src.options;
920 result.ext = ext;
921 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -0800922 JsonWriter::AddBitmapResult(result);
923
mtkleinb0531a72015-04-07 13:38:48 -0700924 // If an MD5 is uninteresting, we want it noted in the JSON file,
925 // but don't want to dump it out as a .png (or whatever ext is).
926 if (gUninterestingHashes.contains(md5)) {
927 return;
928 }
929
mtklein748ca3b2015-01-15 10:56:12 -0800930 const char* dir = FLAGS_writePath[0];
931 if (0 == strcmp(dir, "@")) { // Needed for iOS.
932 dir = FLAGS_resourcePath[0];
933 }
934 sk_mkdir(dir);
935
936 SkString path;
937 if (FLAGS_nameByHash) {
938 path = SkOSPath::Join(dir, result.md5.c_str());
939 path.append(".");
940 path.append(ext);
941 if (sk_exists(path.c_str())) {
942 return; // Content-addressed. If it exists already, we're done.
943 }
944 } else {
945 path = SkOSPath::Join(dir, task.sink.tag);
946 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -0700947 path = SkOSPath::Join(path.c_str(), task.src.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800948 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -0700949 if (strcmp(task.src.options.c_str(), "") != 0) {
950 path = SkOSPath::Join(path.c_str(), task.src.options.c_str());
djsollen54416de2015-04-03 07:24:48 -0700951 sk_mkdir(path.c_str());
952 }
mtklein748ca3b2015-01-15 10:56:12 -0800953 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
954 path.append(".");
955 path.append(ext);
956 }
957
mtklein748ca3b2015-01-15 10:56:12 -0800958 if (bitmap) {
mtkleina5114d72015-08-24 13:27:01 -0700959 if (!dump_png(*bitmap, path.c_str(), result.md5.c_str())) {
mtklein748ca3b2015-01-15 10:56:12 -0800960 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
961 return;
962 }
963 } else {
mtkleina5114d72015-08-24 13:27:01 -0700964 SkFILEWStream file(path.c_str());
965 if (!file.isValid()) {
966 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
967 return;
968 }
mtklein748ca3b2015-01-15 10:56:12 -0800969 if (!file.writeStream(data, len)) {
970 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
971 return;
972 }
973 }
974 }
975};
976
977// Run all tasks in the same enclave serially on the same thread.
978// They can't possibly run concurrently with each other.
979static void run_enclave(SkTArray<Task>* tasks) {
980 for (int i = 0; i < tasks->count(); i++) {
981 Task::Run(tasks->begin() + i);
982 }
983}
984
985/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
986
987// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
988
mtklein55e88b22015-01-21 15:50:13 -0800989static SkTDArray<skiatest::Test> gThreadedTests, gGPUTests;
mtklein748ca3b2015-01-15 10:56:12 -0800990
991static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -0800992 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -0800993 return;
994 }
mtklein6393c062015-04-27 08:45:01 -0700995 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r; r = r->next()) {
996 if (!in_shard()) {
997 continue;
998 }
halcanary87f3ba42015-01-20 09:30:20 -0800999 // Despite its name, factory() is returning a reference to
1000 // link-time static const POD data.
1001 const skiatest::Test& test = r->factory();
1002 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -08001003 continue;
1004 }
halcanary87f3ba42015-01-20 09:30:20 -08001005 if (test.needsGpu && gpu_supported()) {
mtklein55e88b22015-01-21 15:50:13 -08001006 (FLAGS_gpu_threading ? gThreadedTests : gGPUTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -08001007 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein55e88b22015-01-21 15:50:13 -08001008 gThreadedTests.push(test);
mtklein82d28432015-01-15 12:46:02 -08001009 }
mtklein748ca3b2015-01-15 10:56:12 -08001010 }
1011}
1012
halcanary87f3ba42015-01-20 09:30:20 -08001013static void run_test(skiatest::Test* test) {
1014 struct : public skiatest::Reporter {
mtklein36352bf2015-03-25 18:17:31 -07001015 void reportFailed(const skiatest::Failure& failure) override {
halcanary87f3ba42015-01-20 09:30:20 -08001016 fail(failure.toString());
1017 JsonWriter::AddTestFailure(failure);
1018 }
mtklein36352bf2015-03-25 18:17:31 -07001019 bool allowExtendedTest() const override {
halcanary87f3ba42015-01-20 09:30:20 -08001020 return FLAGS_pathOpsExtended;
1021 }
mtklein36352bf2015-03-25 18:17:31 -07001022 bool verbose() const override { return FLAGS_veryVerbose; }
halcanary87f3ba42015-01-20 09:30:20 -08001023 } reporter;
djsollen824996a2015-06-12 12:06:22 -07001024
1025 SkString note;
1026 SkString whyBlacklisted = is_blacklisted("_", "tests", "_", test->name);
1027 if (!whyBlacklisted.isEmpty()) {
1028 note.appendf(" (--blacklist %s)", whyBlacklisted.c_str());
1029 }
1030
mtklein748ca3b2015-01-15 10:56:12 -08001031 WallTimer timer;
1032 timer.start();
djsollen824996a2015-06-12 12:06:22 -07001033 if (!FLAGS_dryRun && whyBlacklisted.isEmpty()) {
djsollen54416de2015-04-03 07:24:48 -07001034 start("unit", "test", "", test->name);
mtklein55e88b22015-01-21 15:50:13 -08001035 GrContextFactory factory;
bsalomon821e10e2015-06-04 14:15:33 -07001036 if (FLAGS_pre_log) {
1037 SkDebugf("\nRunning test %s", test->name);
1038 }
mtklein55e88b22015-01-21 15:50:13 -08001039 test->proc(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -08001040 }
1041 timer.end();
djsollen824996a2015-06-12 12:06:22 -07001042 done(timer.fWall, "unit", "test", "", test->name, note, "");
mtklein748ca3b2015-01-15 10:56:12 -08001043}
1044
1045/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1046
mtklein55e88b22015-01-21 15:50:13 -08001047// If we're isolating all GPU-bound work to one thread (the default), this function runs all that.
1048static void run_enclave_and_gpu_tests(SkTArray<Task>* tasks) {
1049 run_enclave(tasks);
1050 for (int i = 0; i < gGPUTests.count(); i++) {
1051 run_test(&gGPUTests[i]);
1052 }
1053}
1054
mtkleinde6fc2b2015-03-12 06:28:54 -07001055// Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung.
1056// This prints something every once in a while so that it knows we're still working.
mtklein2e1c47e2015-03-12 07:16:56 -07001057static void start_keepalive() {
1058 struct Loop {
1059 static void forever(void*) {
1060 for (;;) {
1061 static const int kSec = 300;
1062 #if defined(SK_BUILD_FOR_WIN)
1063 Sleep(kSec * 1000);
1064 #else
1065 sleep(kSec);
1066 #endif
mtkleinb37cb412015-03-18 05:27:14 -07001067 SkString running;
1068 {
1069 SkAutoMutexAcquire lock(gRunningMutex);
1070 for (int i = 0; i < gRunning.count(); i++) {
1071 running.appendf("\n\t%s", gRunning[i].c_str());
1072 }
1073 }
1074 SkDebugf("\nCurrently running:%s\n", running.c_str());
mtklein2e1c47e2015-03-12 07:16:56 -07001075 }
1076 }
1077 };
1078 static SkThread* intentionallyLeaked = new SkThread(Loop::forever);
1079 intentionallyLeaked->start();
mtkleinde6fc2b2015-03-12 06:28:54 -07001080}
1081
caryclark83ca6282015-06-10 09:31:09 -07001082#define PORTABLE_FONT_PREFIX "Toy Liberation "
1083
1084static SkTypeface* create_from_name(const char familyName[], SkTypeface::Style style) {
1085 if (familyName && strlen(familyName) > sizeof(PORTABLE_FONT_PREFIX)
1086 && !strncmp(familyName, PORTABLE_FONT_PREFIX, sizeof(PORTABLE_FONT_PREFIX) - 1)) {
caryclark1818acb2015-07-24 12:09:25 -07001087 return sk_tool_utils::create_portable_typeface(familyName, style);
caryclark83ca6282015-06-10 09:31:09 -07001088 }
halcanary96fcdcc2015-08-27 07:41:13 -07001089 return nullptr;
caryclark83ca6282015-06-10 09:31:09 -07001090}
1091
1092#undef PORTABLE_FONT_PREFIX
1093
1094extern SkTypeface* (*gCreateTypefaceDelegate)(const char [], SkTypeface::Style );
1095
jcgregorio3b27ade2014-11-13 08:06:40 -08001096int dm_main();
caryclark17f0b6d2014-07-22 10:15:34 -07001097int dm_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -08001098 SetupCrashHandler();
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +00001099 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -07001100 SkTaskGroup::Enabler enabled(FLAGS_threads);
caryclark83ca6282015-06-10 09:31:09 -07001101 gCreateTypefaceDelegate = &create_from_name;
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +00001102
mtklein2e1c47e2015-03-12 07:16:56 -07001103 start_keepalive();
mtkleinde6fc2b2015-03-12 06:28:54 -07001104
mtklein62bd1a62015-01-27 14:46:26 -08001105 gather_gold();
borenet09ed4802015-04-03 14:15:33 -07001106 gather_uninteresting_hashes();
mtklein62bd1a62015-01-27 14:46:26 -08001107
mtklein748ca3b2015-01-15 10:56:12 -08001108 gather_srcs();
1109 gather_sinks();
1110 gather_tests();
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +00001111
mtklein55e88b22015-01-21 15:50:13 -08001112 gPending = gSrcs.count() * gSinks.count() + gThreadedTests.count() + gGPUTests.count();
mtklein748ca3b2015-01-15 10:56:12 -08001113 SkDebugf("%d srcs * %d sinks + %d tests == %d tasks\n",
mtklein55e88b22015-01-21 15:50:13 -08001114 gSrcs.count(), gSinks.count(), gThreadedTests.count() + gGPUTests.count(), gPending);
mtklein748ca3b2015-01-15 10:56:12 -08001115
1116 // We try to exploit as much parallelism as is safe. Most Src/Sink pairs run on any thread,
1117 // but Sinks that identify as part of a particular enclave run serially on a single thread.
mtklein82d28432015-01-15 12:46:02 -08001118 // CPU tests run on any thread. GPU tests depend on --gpu_threading.
mtklein748ca3b2015-01-15 10:56:12 -08001119 SkTArray<Task> enclaves[kNumEnclaves];
1120 for (int j = 0; j < gSinks.count(); j++) {
1121 SkTArray<Task>& tasks = enclaves[gSinks[j]->enclave()];
1122 for (int i = 0; i < gSrcs.count(); i++) {
1123 tasks.push_back(Task(gSrcs[i], gSinks[j]));
1124 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001125 }
1126
mtklein748ca3b2015-01-15 10:56:12 -08001127 SkTaskGroup tg;
mtklein55e88b22015-01-21 15:50:13 -08001128 tg.batch(run_test, gThreadedTests.begin(), gThreadedTests.count());
1129 for (int i = 0; i < kNumEnclaves; i++) {
1130 switch(i) {
1131 case kAnyThread_Enclave:
1132 tg.batch(Task::Run, enclaves[i].begin(), enclaves[i].count());
1133 break;
1134 case kGPU_Enclave:
1135 tg.add(run_enclave_and_gpu_tests, &enclaves[i]);
1136 break;
1137 default:
1138 tg.add(run_enclave, &enclaves[i]);
1139 break;
mtklein82d28432015-01-15 12:46:02 -08001140 }
mtklein55e88b22015-01-21 15:50:13 -08001141 }
mtklein748ca3b2015-01-15 10:56:12 -08001142 tg.wait();
mtklein748ca3b2015-01-15 10:56:12 -08001143 // At this point we're back in single-threaded land.
caryclarkf53ce802015-06-15 06:48:30 -07001144 sk_tool_utils::release_portable_typefaces();
mtklein@google.comd36522d2013-10-16 13:02:15 +00001145
mtklein2f64eec2015-01-15 14:20:41 -08001146 SkDebugf("\n");
mtklein748ca3b2015-01-15 10:56:12 -08001147 if (gFailures.count() > 0) {
1148 SkDebugf("Failures:\n");
1149 for (int i = 0; i < gFailures.count(); i++) {
mtklein9dc09102015-01-15 15:47:33 -08001150 SkDebugf("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001151 }
1152 SkDebugf("%d failures\n", gFailures.count());
1153 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001154 }
mtklein748ca3b2015-01-15 10:56:12 -08001155 if (gPending > 0) {
1156 SkDebugf("Hrm, we didn't seem to run everything we intended to! Please file a bug.\n");
1157 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001158 }
halcanary7a14b312015-10-01 07:28:13 -07001159 #ifdef SK_PDF_IMAGE_STATS
1160 SkPDFImageDumpStats();
1161 #endif // SK_PDF_IMAGE_STATS
mtklein748ca3b2015-01-15 10:56:12 -08001162 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +00001163}
jcgregorio3b27ade2014-11-13 08:06:40 -08001164
borenet48087572015-04-02 12:16:36 -07001165#if !defined(SK_BUILD_FOR_IOS)
jcgregorio3b27ade2014-11-13 08:06:40 -08001166int main(int argc, char** argv) {
1167 SkCommandLineFlags::Parse(argc, argv);
1168 return dm_main();
1169}
1170#endif