blob: ccff84c906710ab4d9185c8feb3ebf82b818cf82 [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;
229 case CodecSrc::kScaledCodec_Mode:
230 folder.append("scaled_codec");
231 break;
232 case CodecSrc::kScanline_Mode:
233 folder.append("scanline");
234 break;
235 case CodecSrc::kScanline_Subset_Mode:
236 folder.append("scanline_subset");
237 break;
238 case CodecSrc::kStripe_Mode:
239 folder.append("stripe");
240 break;
241 case CodecSrc::kSubset_Mode:
msarett36c37962015-09-02 13:20:52 -0700242 folder.append("codec_subset");
msarett9e707a02015-09-01 14:57:57 -0700243 break;
244 }
245
246 switch (dstColorType) {
247 case CodecSrc::kGrayscale_Always_DstColorType:
248 folder.append("_kGray8");
249 break;
250 case CodecSrc::kIndex8_Always_DstColorType:
251 folder.append("_kIndex8");
252 break;
253 default:
254 break;
255 }
256
257 if (1.0f != scale) {
258 folder.appendf("_%.3f", scale);
259 }
260
261 CodecSrc* src = new CodecSrc(path, mode, dstColorType, scale);
262 push_src("image", folder, src);
263}
264
msarett438b2ad2015-04-09 12:43:10 -0700265static void push_codec_srcs(Path path) {
266 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
267 if (!encoded) {
268 SkDebugf("Couldn't read %s.", path.c_str());
269 return;
270 }
271 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
halcanary96fcdcc2015-08-27 07:41:13 -0700272 if (nullptr == codec.get()) {
msarett438b2ad2015-04-09 12:43:10 -0700273 SkDebugf("Couldn't create codec for %s.", path.c_str());
274 return;
275 }
276
msarett36c37962015-09-02 13:20:52 -0700277 // Native Scales
msarett0a242972015-06-11 14:27:27 -0700278 // TODO (msarett): Implement scaling tests for SkImageDecoder in order to compare with these
279 // tests. SkImageDecoder supports downscales by integer factors.
emmaleer8f4ba762015-08-14 07:44:46 -0700280 // 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 -0700281 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 -0700282
msarett36c37962015-09-02 13:20:52 -0700283 const CodecSrc::Mode nativeModes[] = { CodecSrc::kCodec_Mode, CodecSrc::kScanline_Mode,
284 CodecSrc::kScanline_Subset_Mode, CodecSrc::kStripe_Mode, CodecSrc::kSubset_Mode };
msarett9e707a02015-09-01 14:57:57 -0700285
msarett36c37962015-09-02 13:20:52 -0700286 CodecSrc::DstColorType colorTypes[3];
287 uint32_t numColorTypes;
288 switch (codec->getInfo().colorType()) {
289 case kGray_8_SkColorType:
290 // FIXME: Is this a long term solution for testing wbmps decodes to kIndex8?
291 // Further discussion on this topic is at skbug.com/3683
292 colorTypes[0] = CodecSrc::kGetFromCanvas_DstColorType;
293 colorTypes[1] = CodecSrc::kGrayscale_Always_DstColorType;
294 colorTypes[2] = CodecSrc::kIndex8_Always_DstColorType;
295 numColorTypes = 3;
296 break;
297 case kIndex_8_SkColorType:
298 colorTypes[0] = CodecSrc::kGetFromCanvas_DstColorType;
299 colorTypes[1] = CodecSrc::kIndex8_Always_DstColorType;
300 numColorTypes = 2;
301 break;
302 default:
303 colorTypes[0] = CodecSrc::kGetFromCanvas_DstColorType;
304 numColorTypes = 1;
305 break;
306 }
msarett9e707a02015-09-01 14:57:57 -0700307
msarett36c37962015-09-02 13:20:52 -0700308 for (float scale : nativeScales) {
msarett36c37962015-09-02 13:20:52 -0700309 for (CodecSrc::Mode mode : nativeModes) {
310 for (uint32_t i = 0; i < numColorTypes; i++) {
311 push_codec_src(path, mode, colorTypes[i], scale);
msarett9e707a02015-09-01 14:57:57 -0700312 }
313 }
msarett0a242972015-06-11 14:27:27 -0700314 }
msarett36c37962015-09-02 13:20:52 -0700315
scroggo46c57472015-09-30 08:57:13 -0700316 if (path.endsWith(".ico") || path.endsWith(".ICO")) {
317 // FIXME: skbug.com/4404: ICO does not have the ability to decode scanlines, so we cannot
318 // use SkScaledCodec with it.
319 return;
320 }
321
msarett36c37962015-09-02 13:20:52 -0700322 // SkScaledCodec Scales
323 // The native scales are included to make sure that SkScaledCodec defaults to the native
324 // scaling strategy when possible.
325 // 0.1, 0.16, 0.2 etc allow us to test SkScaledCodec with sampleSize 10, 6, 5, etc.
326 // 0.4, 0.7 etc allow to test what happens when the client requests a scale that
327 // does not exactly match a sampleSize or native scaling capability.
msarett6c50a8f2015-09-18 11:24:44 -0700328 const float samplingScales[] = { 0.1f, 0.125f, 0.167f, 0.2f, 0.25f, 0.333f, 0.375f, 0.4f, 0.5f,
msarett36c37962015-09-02 13:20:52 -0700329 0.6f, 0.625f, 0.750f, 0.8f, 0.875f, 1.0f };
330
331 for (float scale : samplingScales) {
msarett36c37962015-09-02 13:20:52 -0700332 for (uint32_t i = 0; i < numColorTypes; i++) {
333 push_codec_src(path, CodecSrc::kScaledCodec_Mode, colorTypes[i], scale);
334 }
335 }
msarett438b2ad2015-04-09 12:43:10 -0700336}
337
msaretta5783ae2015-09-08 15:35:32 -0700338static bool brd_color_type_supported(SkBitmapRegionDecoderInterface::Strategy strategy,
339 CodecSrc::DstColorType dstColorType) {
340 switch (strategy) {
341 case SkBitmapRegionDecoderInterface::kCanvas_Strategy:
342 if (CodecSrc::kGetFromCanvas_DstColorType == dstColorType) {
343 return true;
344 }
345 return false;
346 case SkBitmapRegionDecoderInterface::kOriginal_Strategy:
347 switch (dstColorType) {
348 case CodecSrc::kGetFromCanvas_DstColorType:
349 case CodecSrc::kIndex8_Always_DstColorType:
350 case CodecSrc::kGrayscale_Always_DstColorType:
351 return true;
352 default:
353 return false;
354 }
355 default:
356 SkASSERT(false);
357 return false;
358 }
359}
360
361static void push_brd_src(Path path, SkBitmapRegionDecoderInterface::Strategy strategy,
362 CodecSrc::DstColorType dstColorType, BRDSrc::Mode mode, uint32_t sampleSize) {
363 SkString folder;
364 switch (strategy) {
365 case SkBitmapRegionDecoderInterface::kCanvas_Strategy:
366 folder.append("brd_canvas");
367 break;
368 case SkBitmapRegionDecoderInterface::kOriginal_Strategy:
369 folder.append("brd_sample");
370 break;
371 default:
372 SkASSERT(false);
373 return;
374 }
375
376 switch (mode) {
377 case BRDSrc::kFullImage_Mode:
378 break;
379 case BRDSrc::kDivisor_Mode:
380 folder.append("_divisor");
381 break;
382 default:
383 SkASSERT(false);
384 return;
385 }
386
387 switch (dstColorType) {
388 case CodecSrc::kGetFromCanvas_DstColorType:
389 break;
390 case CodecSrc::kIndex8_Always_DstColorType:
391 folder.append("_kIndex");
392 break;
393 case CodecSrc::kGrayscale_Always_DstColorType:
394 folder.append("_kGray");
395 break;
396 default:
397 SkASSERT(false);
398 return;
399 }
400
401 if (1 != sampleSize) {
msarett7f691442015-09-22 11:56:16 -0700402 folder.appendf("_%.3f", get_scale_from_sample_size(sampleSize));
msaretta5783ae2015-09-08 15:35:32 -0700403 }
404
405 BRDSrc* src = new BRDSrc(path, strategy, mode, dstColorType, sampleSize);
406 push_src("image", folder, src);
407}
408
409static void push_brd_srcs(Path path) {
410
411 const SkBitmapRegionDecoderInterface::Strategy strategies[] = {
412 SkBitmapRegionDecoderInterface::kCanvas_Strategy,
413 SkBitmapRegionDecoderInterface::kOriginal_Strategy
414 };
415
msarett6efbe052015-09-11 09:01:16 -0700416 const uint32_t sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
417
msaretta5783ae2015-09-08 15:35:32 -0700418 // We will only test to one backend (8888), but we will test all of the
419 // color types that we need to decode to on this backend.
420 const CodecSrc::DstColorType dstColorTypes[] = {
421 CodecSrc::kGetFromCanvas_DstColorType,
422 CodecSrc::kIndex8_Always_DstColorType,
423 CodecSrc::kGrayscale_Always_DstColorType,
424 };
425
426 const BRDSrc::Mode modes[] = {
427 BRDSrc::kFullImage_Mode,
428 BRDSrc::kDivisor_Mode
429 };
430
msaretta5783ae2015-09-08 15:35:32 -0700431 for (SkBitmapRegionDecoderInterface::Strategy strategy : strategies) {
msarett6efbe052015-09-11 09:01:16 -0700432
msaretta5783ae2015-09-08 15:35:32 -0700433 // We disable png testing for kOriginal_Strategy because the implementation leaks
434 // memory in our forked libpng.
435 // TODO (msarett): Decide if we want to test pngs in this mode and how we might do this.
436 if (SkBitmapRegionDecoderInterface::kOriginal_Strategy == strategy &&
437 (path.endsWith(".png") || path.endsWith(".PNG"))) {
438 continue;
439 }
msarett6efbe052015-09-11 09:01:16 -0700440 for (uint32_t sampleSize : sampleSizes) {
441
442 // kOriginal_Strategy does not work for jpegs that are scaled to non-powers of two.
443 // We don't need to test this. We know it doesn't work, and it causes images with
444 // uninitialized memory to show up on Gold.
445 if (SkBitmapRegionDecoderInterface::kOriginal_Strategy == strategy &&
446 (path.endsWith(".jpg") || path.endsWith(".JPG") ||
447 path.endsWith(".jpeg") || path.endsWith(".JPEG")) && !SkIsPow2(sampleSize)) {
448 continue;
449 }
450 for (CodecSrc::DstColorType dstColorType : dstColorTypes) {
451 if (brd_color_type_supported(strategy, dstColorType)) {
452 for (BRDSrc::Mode mode : modes) {
msaretta5783ae2015-09-08 15:35:32 -0700453 push_brd_src(path, strategy, dstColorType, mode, sampleSize);
454 }
455 }
456 }
457 }
458 }
459}
460
461static bool brd_supported(const char* ext) {
msarett8c8f22a2015-04-01 06:58:48 -0700462 static const char* const exts[] = {
msaretta5783ae2015-09-08 15:35:32 -0700463 "jpg", "jpeg", "png", "webp",
464 "JPG", "JPEG", "PNG", "WEBP",
msarett8c8f22a2015-04-01 06:58:48 -0700465 };
466
467 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
468 if (0 == strcmp(exts[i], ext)) {
469 return true;
470 }
471 }
472 return false;
scroggo9b77ddd2015-03-19 06:03:39 -0700473}
474
mtklein748ca3b2015-01-15 10:56:12 -0800475static void gather_srcs() {
476 for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) {
djsollen54416de2015-04-03 07:24:48 -0700477 push_src("gm", "", new GMSrc(r->factory()));
mtklein748ca3b2015-01-15 10:56:12 -0800478 }
halcanaryfc37ad12015-01-30 07:31:19 -0800479 for (int i = 0; i < FLAGS_skps.count(); i++) {
480 const char* path = FLAGS_skps[i];
481 if (sk_isdir(path)) {
482 SkOSFile::Iter it(path, "skp");
483 for (SkString file; it.next(&file); ) {
djsollen54416de2015-04-03 07:24:48 -0700484 push_src("skp", "", new SKPSrc(SkOSPath::Join(path, file.c_str())));
halcanaryfc37ad12015-01-30 07:31:19 -0800485 }
486 } else {
djsollen54416de2015-04-03 07:24:48 -0700487 push_src("skp", "", new SKPSrc(path));
mtklein114c3cd2015-01-15 10:15:02 -0800488 }
489 }
halcanary23b03c32015-01-30 09:58:58 -0800490 static const char* const exts[] = {
491 "bmp", "gif", "jpg", "jpeg", "png", "webp", "ktx", "astc", "wbmp", "ico",
492 "BMP", "GIF", "JPG", "JPEG", "PNG", "WEBP", "KTX", "ASTC", "WBMP", "ICO",
493 };
494 for (int i = 0; i < FLAGS_images.count(); i++) {
495 const char* flag = FLAGS_images[i];
496 if (sk_isdir(flag)) {
497 for (size_t j = 0; j < SK_ARRAY_COUNT(exts); j++) {
498 SkOSFile::Iter it(flag, exts[j]);
499 for (SkString file; it.next(&file); ) {
500 SkString path = SkOSPath::Join(flag, file.c_str());
djsollen54416de2015-04-03 07:24:48 -0700501 push_src("image", "decode", new ImageSrc(path)); // Decode entire image
502 push_src("image", "subset", new ImageSrc(path, 2)); // Decode into 2x2 subsets
msaretta5783ae2015-09-08 15:35:32 -0700503 push_codec_srcs(path);
504 if (brd_supported(exts[j])) {
505 push_brd_srcs(path);
scroggo9b77ddd2015-03-19 06:03:39 -0700506 }
halcanary23b03c32015-01-30 09:58:58 -0800507 }
mtklein114c3cd2015-01-15 10:15:02 -0800508 }
halcanary23b03c32015-01-30 09:58:58 -0800509 } else if (sk_exists(flag)) {
510 // assume that FLAGS_images[i] is a valid image if it is a file.
djsollen54416de2015-04-03 07:24:48 -0700511 push_src("image", "decode", new ImageSrc(flag)); // Decode entire image.
512 push_src("image", "subset", new ImageSrc(flag, 2)); // Decode into 2 x 2 subsets
msarett438b2ad2015-04-09 12:43:10 -0700513 push_codec_srcs(flag);
msaretta5783ae2015-09-08 15:35:32 -0700514 push_brd_srcs(flag);
mtklein709d2c32015-01-15 08:30:25 -0800515 }
mtklein709d2c32015-01-15 08:30:25 -0800516 }
517}
518
mtklein748ca3b2015-01-15 10:56:12 -0800519static GrGLStandard get_gpu_api() {
520 if (FLAGS_gpuAPI.contains("gl")) { return kGL_GrGLStandard; }
521 if (FLAGS_gpuAPI.contains("gles")) { return kGLES_GrGLStandard; }
522 return kNone_GrGLStandard;
mtklein709d2c32015-01-15 08:30:25 -0800523}
524
mtklein748ca3b2015-01-15 10:56:12 -0800525static void push_sink(const char* tag, Sink* s) {
526 SkAutoTDelete<Sink> sink(s);
527 if (!FLAGS_config.contains(tag)) {
528 return;
mtklein114c3cd2015-01-15 10:15:02 -0800529 }
mtkleine0effd62015-07-29 06:37:28 -0700530 // Try a simple Src as a canary. If it fails, skip this sink.
mtklein748ca3b2015-01-15 10:56:12 -0800531 struct : public Src {
mtkleine0effd62015-07-29 06:37:28 -0700532 Error draw(SkCanvas* c) const override {
533 c->drawRect(SkRect::MakeWH(1,1), SkPaint());
534 return "";
535 }
mtklein36352bf2015-03-25 18:17:31 -0700536 SkISize size() const override { return SkISize::Make(16, 16); }
mtkleine0effd62015-07-29 06:37:28 -0700537 Name name() const override { return "justOneRect"; }
538 } justOneRect;
mtklein114c3cd2015-01-15 10:15:02 -0800539
mtklein748ca3b2015-01-15 10:56:12 -0800540 SkBitmap bitmap;
541 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800542 SkString log;
mtkleine0effd62015-07-29 06:37:28 -0700543 Error err = sink->draw(justOneRect, &bitmap, &stream, &log);
mtklein4089ef72015-03-05 08:40:28 -0800544 if (err.isFatal()) {
mtklein05641a52015-04-21 10:49:13 -0700545 SkDebugf("Could not run %s: %s\n", tag, err.c_str());
546 exit(1);
mtklein114c3cd2015-01-15 10:15:02 -0800547 }
548
mtkleine0effd62015-07-29 06:37:28 -0700549 TaggedSink& ts = gSinks.push_back();
mtklein748ca3b2015-01-15 10:56:12 -0800550 ts.reset(sink.detach());
551 ts.tag = tag;
552}
553
554static bool gpu_supported() {
555#if SK_SUPPORT_GPU
556 return FLAGS_gpu;
557#else
558 return false;
559#endif
560}
561
562static Sink* create_sink(const char* tag) {
563#define SINK(t, sink, ...) if (0 == strcmp(t, tag)) { return new sink(__VA_ARGS__); }
564 if (gpu_supported()) {
mtklein82d28432015-01-15 12:46:02 -0800565 typedef GrContextFactory Gr;
mtklein748ca3b2015-01-15 10:56:12 -0800566 const GrGLStandard api = get_gpu_api();
hendrikw885bf092015-08-27 10:38:39 -0700567 SINK("gpunull", GPUSink, Gr::kNull_GLContextType, api, 0, false, FLAGS_gpu_threading);
568 SINK("gpudebug", GPUSink, Gr::kDebug_GLContextType, api, 0, false, FLAGS_gpu_threading);
569 SINK("gpu", GPUSink, Gr::kNative_GLContextType, api, 0, false, FLAGS_gpu_threading);
570 SINK("gpudft", GPUSink, Gr::kNative_GLContextType, api, 0, true, FLAGS_gpu_threading);
571 SINK("msaa4", GPUSink, Gr::kNative_GLContextType, api, 4, false, FLAGS_gpu_threading);
572 SINK("msaa16", GPUSink, Gr::kNative_GLContextType, api, 16, false, FLAGS_gpu_threading);
bsalomon90e8ab72015-09-08 10:26:51 -0700573 SINK("nvprmsaa4", GPUSink, Gr::kNVPR_GLContextType, api, 4, true, FLAGS_gpu_threading);
574 SINK("nvprmsaa16", GPUSink, Gr::kNVPR_GLContextType, api, 16, true, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800575 #if SK_ANGLE
hendrikw885bf092015-08-27 10:38:39 -0700576 SINK("angle", GPUSink, Gr::kANGLE_GLContextType, api, 0, false, FLAGS_gpu_threading);
hendrikweddbefb2015-09-11 13:07:29 -0700577 SINK("angle-gl", GPUSink, Gr::kANGLE_GL_GLContextType, api, 0, false, FLAGS_gpu_threading);
hendrikw885bf092015-08-27 10:38:39 -0700578 #endif
579 #if SK_COMMAND_BUFFER
580 SINK("commandbuffer", GPUSink, Gr::kCommandBuffer_GLContextType, api, 0, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800581 #endif
582 #if SK_MESA
hendrikw885bf092015-08-27 10:38:39 -0700583 SINK("mesa", GPUSink, Gr::kMESA_GLContextType, api, 0, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800584 #endif
mtklein114c3cd2015-01-15 10:15:02 -0800585 }
mtklein748ca3b2015-01-15 10:56:12 -0800586
tomhudsoneebc39a2015-02-23 12:18:05 -0800587#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
588 SINK("hwui", HWUISink);
589#endif
590
mtklein748ca3b2015-01-15 10:56:12 -0800591 if (FLAGS_cpu) {
592 SINK("565", RasterSink, kRGB_565_SkColorType);
593 SINK("8888", RasterSink, kN32_SkColorType);
halcanaryc11c62f2015-09-28 11:51:54 -0700594 SINK("pdf", PDFSink, "Pdfium");
595 SINK("pdf_poppler", PDFSink, "Poppler");
mtklein9c3f17d2015-01-28 11:35:18 -0800596 SINK("skp", SKPSink);
mtklein8a4527e2015-01-31 20:00:58 -0800597 SINK("svg", SVGSink);
598 SINK("null", NullSink);
halcanary47ef4d52015-03-03 09:13:09 -0800599 SINK("xps", XPSSink);
mtklein748ca3b2015-01-15 10:56:12 -0800600 }
601#undef SINK
halcanary96fcdcc2015-08-27 07:41:13 -0700602 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800603}
604
mtklein748ca3b2015-01-15 10:56:12 -0800605static Sink* create_via(const char* tag, Sink* wrapped) {
606#define VIA(t, via, ...) if (0 == strcmp(t, tag)) { return new via(__VA_ARGS__); }
halcanary96fcdcc2015-08-27 07:41:13 -0700607 VIA("twice", ViaTwice, wrapped);
608 VIA("pipe", ViaPipe, wrapped);
609 VIA("serialize", ViaSerialization, wrapped);
610 VIA("2ndpic", ViaSecondPicture, wrapped);
mtkleind31c13d2015-05-05 12:59:56 -0700611 VIA("sp", ViaSingletonPictures, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700612 VIA("tiles", ViaTiles, 256, 256, nullptr, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800613 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800614
mtkleind603b222015-02-17 11:13:33 -0800615 if (FLAGS_matrix.count() == 4) {
mtklein748ca3b2015-01-15 10:56:12 -0800616 SkMatrix m;
mtkleind603b222015-02-17 11:13:33 -0800617 m.reset();
618 m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
619 m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
620 m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
621 m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
622 VIA("matrix", ViaMatrix, m, wrapped);
623 VIA("upright", ViaUpright, m, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800624 }
tomhudson64de1e12015-03-05 08:01:07 -0800625
626#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
627 VIA("androidsdk", ViaAndroidSDK, wrapped);
628#endif
629
mtklein748ca3b2015-01-15 10:56:12 -0800630#undef VIA
halcanary96fcdcc2015-08-27 07:41:13 -0700631 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800632}
633
mtklein748ca3b2015-01-15 10:56:12 -0800634static void gather_sinks() {
635 for (int i = 0; i < FLAGS_config.count(); i++) {
636 const char* config = FLAGS_config[i];
637 SkTArray<SkString> parts;
638 SkStrSplit(config, "-", &parts);
639
halcanary96fcdcc2015-08-27 07:41:13 -0700640 Sink* sink = nullptr;
mtklein748ca3b2015-01-15 10:56:12 -0800641 for (int i = parts.count(); i-- > 0;) {
642 const char* part = parts[i].c_str();
halcanary96fcdcc2015-08-27 07:41:13 -0700643 Sink* next = (sink == nullptr) ? create_sink(part) : create_via(part, sink);
644 if (next == nullptr) {
mtklein748ca3b2015-01-15 10:56:12 -0800645 SkDebugf("Skipping %s: Don't understand '%s'.\n", config, part);
646 delete sink;
halcanary96fcdcc2015-08-27 07:41:13 -0700647 sink = nullptr;
mtklein748ca3b2015-01-15 10:56:12 -0800648 break;
649 }
650 sink = next;
651 }
652 if (sink) {
653 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800654 }
655 }
656}
mtklein709d2c32015-01-15 08:30:25 -0800657
mtkleina5114d72015-08-24 13:27:01 -0700658static bool dump_png(SkBitmap bitmap, const char* path, const char* md5) {
659 const int w = bitmap.width(),
660 h = bitmap.height();
661
662 // First get the bitmap into N32 color format. The next step will work only there.
663 if (bitmap.colorType() != kN32_SkColorType) {
664 SkBitmap n32;
665 if (!bitmap.copyTo(&n32, kN32_SkColorType)) {
666 return false;
667 }
668 bitmap = n32;
669 }
670
671 // Convert our N32 bitmap into unpremul RGBA for libpng.
672 SkAutoTMalloc<uint32_t> rgba(w*h);
673 if (!bitmap.readPixels(SkImageInfo::Make(w,h, kRGBA_8888_SkColorType, kUnpremul_SkAlphaType),
674 rgba, 4*w, 0,0)) {
675 return false;
676 }
677
678 // We don't need bitmap anymore. Might as well drop our ref.
mtkleinfccb77d2015-08-24 14:13:29 -0700679 bitmap.reset();
mtkleina5114d72015-08-24 13:27:01 -0700680
mtkleinc64137c2015-08-25 10:56:08 -0700681 FILE* f = fopen(path, "wb");
mtkleina5114d72015-08-24 13:27:01 -0700682 if (!f) { return false; }
683
684 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
685 if (!png) {
686 fclose(f);
687 return false;
688 }
689
690 png_infop info = png_create_info_struct(png);
691 if (!info) {
692 png_destroy_write_struct(&png, &info);
693 fclose(f);
694 return false;
695 }
696
mtkleind69ece62015-09-10 10:37:44 -0700697 SkString description;
698 description.append("Key: ");
699 for (int i = 0; i < FLAGS_key.count(); i++) {
700 description.appendf("%s ", FLAGS_key[i]);
701 }
702 description.append("Properties: ");
703 for (int i = 0; i < FLAGS_properties.count(); i++) {
704 description.appendf("%s ", FLAGS_properties[i]);
705 }
706 description.appendf("MD5: %s", md5);
707
mtkleina5114d72015-08-24 13:27:01 -0700708 png_text text[2];
709 text[0].key = (png_charp)"Author";
710 text[0].text = (png_charp)"DM dump_png()";
711 text[0].compression = PNG_TEXT_COMPRESSION_NONE;
712 text[1].key = (png_charp)"Description";
mtkleind69ece62015-09-10 10:37:44 -0700713 text[1].text = (png_charp)description.c_str();
mtkleina5114d72015-08-24 13:27:01 -0700714 text[1].compression = PNG_TEXT_COMPRESSION_NONE;
715 png_set_text(png, info, text, 2);
716
717 png_init_io(png, f);
718 png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8,
719 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
720 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
721 png_write_info(png, info);
722 for (int j = 0; j < h; j++) {
723 png_bytep row = (png_bytep)(rgba.get() + w*j);
724 png_write_rows(png, &row, 1);
725 }
726 png_write_end(png, info);
727
728 png_destroy_write_struct(&png, &info);
729 fclose(f);
730 return true;
731}
732
mtkleina2ef6422015-01-15 13:44:22 -0800733static bool match(const char* needle, const char* haystack) {
halcanary96fcdcc2015-08-27 07:41:13 -0700734 return 0 == strcmp("_", needle) || nullptr != strstr(haystack, needle);
mtkleina2ef6422015-01-15 13:44:22 -0800735}
736
djsollen54416de2015-04-03 07:24:48 -0700737static ImplicitString is_blacklisted(const char* sink, const char* src,
738 const char* srcOptions, const char* name) {
mtklein0d243ff2015-04-03 07:51:00 -0700739 for (int i = 0; i < FLAGS_blacklist.count() - 3; i += 4) {
mtkleina2ef6422015-01-15 13:44:22 -0800740 if (match(FLAGS_blacklist[i+0], sink) &&
djsollen54416de2015-04-03 07:24:48 -0700741 match(FLAGS_blacklist[i+1], src) &&
742 match(FLAGS_blacklist[i+2], srcOptions) &&
743 match(FLAGS_blacklist[i+3], name)) {
744 return SkStringPrintf("%s %s %s %s",
745 FLAGS_blacklist[i+0], FLAGS_blacklist[i+1],
746 FLAGS_blacklist[i+2], FLAGS_blacklist[i+3]);
mtkleina2ef6422015-01-15 13:44:22 -0800747 }
748 }
749 return "";
750}
751
mtklein748ca3b2015-01-15 10:56:12 -0800752// The finest-grained unit of work we can run: draw a single Src into a single Sink,
753// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
754struct Task {
mtkleine0effd62015-07-29 06:37:28 -0700755 Task(const TaggedSrc& src, const TaggedSink& sink) : src(src), sink(sink) {}
756 const TaggedSrc& src;
757 const TaggedSink& sink;
mtklein748ca3b2015-01-15 10:56:12 -0800758
759 static void Run(Task* task) {
mtkleina2ef6422015-01-15 13:44:22 -0800760 SkString name = task->src->name();
mtkleine0effd62015-07-29 06:37:28 -0700761
762 // We'll skip drawing this Src/Sink pair if:
763 // - the Src vetoes the Sink;
764 // - this Src / Sink combination is on the blacklist;
765 // - it's a dry run.
mtklein99cab4e2015-07-31 06:43:04 -0700766 SkString note(task->src->veto(task->sink->flags()) ? " (veto)" : "");
msarett9e707a02015-09-01 14:57:57 -0700767 SkString whyBlacklisted = is_blacklisted(task->sink.tag, task->src.tag.c_str(),
768 task->src.options.c_str(), name.c_str());
mtkleinb37cb412015-03-18 05:27:14 -0700769 if (!whyBlacklisted.isEmpty()) {
770 note.appendf(" (--blacklist %s)", whyBlacklisted.c_str());
771 }
mtkleine0effd62015-07-29 06:37:28 -0700772
mtkleinb9eb4ac2015-02-02 18:26:03 -0800773 SkString log;
mtklein748ca3b2015-01-15 10:56:12 -0800774 WallTimer timer;
775 timer.start();
mtkleine0effd62015-07-29 06:37:28 -0700776 if (!FLAGS_dryRun && note.isEmpty()) {
mtklein748ca3b2015-01-15 10:56:12 -0800777 SkBitmap bitmap;
778 SkDynamicMemoryWStream stream;
bsalomon821e10e2015-06-04 14:15:33 -0700779 if (FLAGS_pre_log) {
780 SkDebugf("\nRunning %s->%s", name.c_str(), task->sink.tag);
781 }
djsollen54416de2015-04-03 07:24:48 -0700782 start(task->sink.tag, task->src.tag, task->src.options, name.c_str());
mtkleinb9eb4ac2015-02-02 18:26:03 -0800783 Error err = task->sink->draw(*task->src, &bitmap, &stream, &log);
mtklein748ca3b2015-01-15 10:56:12 -0800784 if (!err.isEmpty()) {
mtklein4089ef72015-03-05 08:40:28 -0800785 timer.end();
786 if (err.isFatal()) {
djsollen54416de2015-04-03 07:24:48 -0700787 fail(SkStringPrintf("%s %s %s %s: %s",
mtklein4089ef72015-03-05 08:40:28 -0800788 task->sink.tag,
msarett9e707a02015-09-01 14:57:57 -0700789 task->src.tag.c_str(),
790 task->src.options.c_str(),
mtklein4089ef72015-03-05 08:40:28 -0800791 name.c_str(),
792 err.c_str()));
mtkleinb37cb412015-03-18 05:27:14 -0700793 } else {
794 note.appendf(" (skipped: %s)", err.c_str());
mtklein4089ef72015-03-05 08:40:28 -0800795 }
djsollen54416de2015-04-03 07:24:48 -0700796 done(timer.fWall, task->sink.tag, task->src.tag, task->src.options,
797 name, note, log);
mtklein4089ef72015-03-05 08:40:28 -0800798 return;
mtklein748ca3b2015-01-15 10:56:12 -0800799 }
mtklein62bd1a62015-01-27 14:46:26 -0800800 SkAutoTDelete<SkStreamAsset> data(stream.detachAsStream());
801
802 SkString md5;
803 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
804 SkMD5 hash;
805 if (data->getLength()) {
806 hash.writeStream(data, data->getLength());
807 data->rewind();
808 } else {
mtklein38408462015-07-08 07:25:27 -0700809 // If we're BGRA (Linux, Windows), swizzle over to RGBA (Mac, Android).
810 // This helps eliminate multiple 0-pixel-diff hashes on gold.skia.org.
811 // (Android's general slow speed breaks the tie arbitrarily in RGBA's favor.)
812 // We might consider promoting 565 to RGBA too.
813 if (bitmap.colorType() == kBGRA_8888_SkColorType) {
814 SkBitmap swizzle;
815 SkAssertResult(bitmap.copyTo(&swizzle, kRGBA_8888_SkColorType));
816 hash.write(swizzle.getPixels(), swizzle.getSize());
817 } else {
818 hash.write(bitmap.getPixels(), bitmap.getSize());
819 }
mtklein62bd1a62015-01-27 14:46:26 -0800820 }
821 SkMD5::Digest digest;
822 hash.finish(digest);
823 for (int i = 0; i < 16; i++) {
824 md5.appendf("%02x", digest.data[i]);
825 }
826 }
827
828 if (!FLAGS_readPath.isEmpty() &&
msarett9e707a02015-09-01 14:57:57 -0700829 !gGold.contains(Gold(task->sink.tag, task->src.tag.c_str(),
830 task->src.options.c_str(), name, md5))) {
djsollen54416de2015-04-03 07:24:48 -0700831 fail(SkStringPrintf("%s not found for %s %s %s %s in %s",
mtklein62bd1a62015-01-27 14:46:26 -0800832 md5.c_str(),
833 task->sink.tag,
msarett9e707a02015-09-01 14:57:57 -0700834 task->src.tag.c_str(),
835 task->src.options.c_str(),
mtklein62bd1a62015-01-27 14:46:26 -0800836 name.c_str(),
837 FLAGS_readPath[0]));
838 }
839
mtkleinb0531a72015-04-07 13:38:48 -0700840 if (!FLAGS_writePath.isEmpty()) {
mtklein748ca3b2015-01-15 10:56:12 -0800841 const char* ext = task->sink->fileExtension();
mtklein62bd1a62015-01-27 14:46:26 -0800842 if (data->getLength()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700843 WriteToDisk(*task, md5, ext, data, data->getLength(), nullptr);
halcanary022afb82015-01-30 11:00:12 -0800844 SkASSERT(bitmap.drawsNothing());
845 } else if (!bitmap.drawsNothing()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700846 WriteToDisk(*task, md5, ext, nullptr, 0, &bitmap);
mtklein748ca3b2015-01-15 10:56:12 -0800847 }
848 }
849 }
850 timer.end();
msarett9e707a02015-09-01 14:57:57 -0700851 done(timer.fWall, task->sink.tag, task->src.tag.c_str(), task->src.options.c_str(), name,
852 note, log);
mtklein748ca3b2015-01-15 10:56:12 -0800853 }
854
855 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -0800856 SkString md5,
857 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -0800858 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -0800859 const SkBitmap* bitmap) {
mtklein748ca3b2015-01-15 10:56:12 -0800860 JsonWriter::BitmapResult result;
djsollen54416de2015-04-03 07:24:48 -0700861 result.name = task.src->name();
862 result.config = task.sink.tag;
863 result.sourceType = task.src.tag;
864 result.sourceOptions = task.src.options;
865 result.ext = ext;
866 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -0800867 JsonWriter::AddBitmapResult(result);
868
mtkleinb0531a72015-04-07 13:38:48 -0700869 // If an MD5 is uninteresting, we want it noted in the JSON file,
870 // but don't want to dump it out as a .png (or whatever ext is).
871 if (gUninterestingHashes.contains(md5)) {
872 return;
873 }
874
mtklein748ca3b2015-01-15 10:56:12 -0800875 const char* dir = FLAGS_writePath[0];
876 if (0 == strcmp(dir, "@")) { // Needed for iOS.
877 dir = FLAGS_resourcePath[0];
878 }
879 sk_mkdir(dir);
880
881 SkString path;
882 if (FLAGS_nameByHash) {
883 path = SkOSPath::Join(dir, result.md5.c_str());
884 path.append(".");
885 path.append(ext);
886 if (sk_exists(path.c_str())) {
887 return; // Content-addressed. If it exists already, we're done.
888 }
889 } else {
890 path = SkOSPath::Join(dir, task.sink.tag);
891 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -0700892 path = SkOSPath::Join(path.c_str(), task.src.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800893 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -0700894 if (strcmp(task.src.options.c_str(), "") != 0) {
895 path = SkOSPath::Join(path.c_str(), task.src.options.c_str());
djsollen54416de2015-04-03 07:24:48 -0700896 sk_mkdir(path.c_str());
897 }
mtklein748ca3b2015-01-15 10:56:12 -0800898 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
899 path.append(".");
900 path.append(ext);
901 }
902
mtklein748ca3b2015-01-15 10:56:12 -0800903 if (bitmap) {
mtkleina5114d72015-08-24 13:27:01 -0700904 if (!dump_png(*bitmap, path.c_str(), result.md5.c_str())) {
mtklein748ca3b2015-01-15 10:56:12 -0800905 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
906 return;
907 }
908 } else {
mtkleina5114d72015-08-24 13:27:01 -0700909 SkFILEWStream file(path.c_str());
910 if (!file.isValid()) {
911 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
912 return;
913 }
mtklein748ca3b2015-01-15 10:56:12 -0800914 if (!file.writeStream(data, len)) {
915 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
916 return;
917 }
918 }
919 }
920};
921
922// Run all tasks in the same enclave serially on the same thread.
923// They can't possibly run concurrently with each other.
924static void run_enclave(SkTArray<Task>* tasks) {
925 for (int i = 0; i < tasks->count(); i++) {
926 Task::Run(tasks->begin() + i);
927 }
928}
929
930/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
931
932// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
933
mtklein55e88b22015-01-21 15:50:13 -0800934static SkTDArray<skiatest::Test> gThreadedTests, gGPUTests;
mtklein748ca3b2015-01-15 10:56:12 -0800935
936static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -0800937 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -0800938 return;
939 }
mtklein6393c062015-04-27 08:45:01 -0700940 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r; r = r->next()) {
941 if (!in_shard()) {
942 continue;
943 }
halcanary87f3ba42015-01-20 09:30:20 -0800944 // Despite its name, factory() is returning a reference to
945 // link-time static const POD data.
946 const skiatest::Test& test = r->factory();
947 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -0800948 continue;
949 }
halcanary87f3ba42015-01-20 09:30:20 -0800950 if (test.needsGpu && gpu_supported()) {
mtklein55e88b22015-01-21 15:50:13 -0800951 (FLAGS_gpu_threading ? gThreadedTests : gGPUTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -0800952 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein55e88b22015-01-21 15:50:13 -0800953 gThreadedTests.push(test);
mtklein82d28432015-01-15 12:46:02 -0800954 }
mtklein748ca3b2015-01-15 10:56:12 -0800955 }
956}
957
halcanary87f3ba42015-01-20 09:30:20 -0800958static void run_test(skiatest::Test* test) {
959 struct : public skiatest::Reporter {
mtklein36352bf2015-03-25 18:17:31 -0700960 void reportFailed(const skiatest::Failure& failure) override {
halcanary87f3ba42015-01-20 09:30:20 -0800961 fail(failure.toString());
962 JsonWriter::AddTestFailure(failure);
963 }
mtklein36352bf2015-03-25 18:17:31 -0700964 bool allowExtendedTest() const override {
halcanary87f3ba42015-01-20 09:30:20 -0800965 return FLAGS_pathOpsExtended;
966 }
mtklein36352bf2015-03-25 18:17:31 -0700967 bool verbose() const override { return FLAGS_veryVerbose; }
halcanary87f3ba42015-01-20 09:30:20 -0800968 } reporter;
djsollen824996a2015-06-12 12:06:22 -0700969
970 SkString note;
971 SkString whyBlacklisted = is_blacklisted("_", "tests", "_", test->name);
972 if (!whyBlacklisted.isEmpty()) {
973 note.appendf(" (--blacklist %s)", whyBlacklisted.c_str());
974 }
975
mtklein748ca3b2015-01-15 10:56:12 -0800976 WallTimer timer;
977 timer.start();
djsollen824996a2015-06-12 12:06:22 -0700978 if (!FLAGS_dryRun && whyBlacklisted.isEmpty()) {
djsollen54416de2015-04-03 07:24:48 -0700979 start("unit", "test", "", test->name);
mtklein55e88b22015-01-21 15:50:13 -0800980 GrContextFactory factory;
bsalomon821e10e2015-06-04 14:15:33 -0700981 if (FLAGS_pre_log) {
982 SkDebugf("\nRunning test %s", test->name);
983 }
mtklein55e88b22015-01-21 15:50:13 -0800984 test->proc(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -0800985 }
986 timer.end();
djsollen824996a2015-06-12 12:06:22 -0700987 done(timer.fWall, "unit", "test", "", test->name, note, "");
mtklein748ca3b2015-01-15 10:56:12 -0800988}
989
990/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
991
mtklein55e88b22015-01-21 15:50:13 -0800992// If we're isolating all GPU-bound work to one thread (the default), this function runs all that.
993static void run_enclave_and_gpu_tests(SkTArray<Task>* tasks) {
994 run_enclave(tasks);
995 for (int i = 0; i < gGPUTests.count(); i++) {
996 run_test(&gGPUTests[i]);
997 }
998}
999
mtkleinde6fc2b2015-03-12 06:28:54 -07001000// Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung.
1001// This prints something every once in a while so that it knows we're still working.
mtklein2e1c47e2015-03-12 07:16:56 -07001002static void start_keepalive() {
1003 struct Loop {
1004 static void forever(void*) {
1005 for (;;) {
1006 static const int kSec = 300;
1007 #if defined(SK_BUILD_FOR_WIN)
1008 Sleep(kSec * 1000);
1009 #else
1010 sleep(kSec);
1011 #endif
mtkleinb37cb412015-03-18 05:27:14 -07001012 SkString running;
1013 {
1014 SkAutoMutexAcquire lock(gRunningMutex);
1015 for (int i = 0; i < gRunning.count(); i++) {
1016 running.appendf("\n\t%s", gRunning[i].c_str());
1017 }
1018 }
1019 SkDebugf("\nCurrently running:%s\n", running.c_str());
mtklein2e1c47e2015-03-12 07:16:56 -07001020 }
1021 }
1022 };
1023 static SkThread* intentionallyLeaked = new SkThread(Loop::forever);
1024 intentionallyLeaked->start();
mtkleinde6fc2b2015-03-12 06:28:54 -07001025}
1026
caryclark83ca6282015-06-10 09:31:09 -07001027#define PORTABLE_FONT_PREFIX "Toy Liberation "
1028
1029static SkTypeface* create_from_name(const char familyName[], SkTypeface::Style style) {
1030 if (familyName && strlen(familyName) > sizeof(PORTABLE_FONT_PREFIX)
1031 && !strncmp(familyName, PORTABLE_FONT_PREFIX, sizeof(PORTABLE_FONT_PREFIX) - 1)) {
caryclark1818acb2015-07-24 12:09:25 -07001032 return sk_tool_utils::create_portable_typeface(familyName, style);
caryclark83ca6282015-06-10 09:31:09 -07001033 }
halcanary96fcdcc2015-08-27 07:41:13 -07001034 return nullptr;
caryclark83ca6282015-06-10 09:31:09 -07001035}
1036
1037#undef PORTABLE_FONT_PREFIX
1038
1039extern SkTypeface* (*gCreateTypefaceDelegate)(const char [], SkTypeface::Style );
1040
jcgregorio3b27ade2014-11-13 08:06:40 -08001041int dm_main();
caryclark17f0b6d2014-07-22 10:15:34 -07001042int dm_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -08001043 SetupCrashHandler();
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +00001044 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -07001045 SkTaskGroup::Enabler enabled(FLAGS_threads);
caryclark83ca6282015-06-10 09:31:09 -07001046 gCreateTypefaceDelegate = &create_from_name;
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +00001047
mtklein2e1c47e2015-03-12 07:16:56 -07001048 start_keepalive();
mtkleinde6fc2b2015-03-12 06:28:54 -07001049
mtklein62bd1a62015-01-27 14:46:26 -08001050 gather_gold();
borenet09ed4802015-04-03 14:15:33 -07001051 gather_uninteresting_hashes();
mtklein62bd1a62015-01-27 14:46:26 -08001052
mtklein748ca3b2015-01-15 10:56:12 -08001053 gather_srcs();
1054 gather_sinks();
1055 gather_tests();
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +00001056
mtklein55e88b22015-01-21 15:50:13 -08001057 gPending = gSrcs.count() * gSinks.count() + gThreadedTests.count() + gGPUTests.count();
mtklein748ca3b2015-01-15 10:56:12 -08001058 SkDebugf("%d srcs * %d sinks + %d tests == %d tasks\n",
mtklein55e88b22015-01-21 15:50:13 -08001059 gSrcs.count(), gSinks.count(), gThreadedTests.count() + gGPUTests.count(), gPending);
mtklein748ca3b2015-01-15 10:56:12 -08001060
1061 // We try to exploit as much parallelism as is safe. Most Src/Sink pairs run on any thread,
1062 // but Sinks that identify as part of a particular enclave run serially on a single thread.
mtklein82d28432015-01-15 12:46:02 -08001063 // CPU tests run on any thread. GPU tests depend on --gpu_threading.
mtklein748ca3b2015-01-15 10:56:12 -08001064 SkTArray<Task> enclaves[kNumEnclaves];
1065 for (int j = 0; j < gSinks.count(); j++) {
1066 SkTArray<Task>& tasks = enclaves[gSinks[j]->enclave()];
1067 for (int i = 0; i < gSrcs.count(); i++) {
1068 tasks.push_back(Task(gSrcs[i], gSinks[j]));
1069 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001070 }
1071
mtklein748ca3b2015-01-15 10:56:12 -08001072 SkTaskGroup tg;
mtklein55e88b22015-01-21 15:50:13 -08001073 tg.batch(run_test, gThreadedTests.begin(), gThreadedTests.count());
1074 for (int i = 0; i < kNumEnclaves; i++) {
1075 switch(i) {
1076 case kAnyThread_Enclave:
1077 tg.batch(Task::Run, enclaves[i].begin(), enclaves[i].count());
1078 break;
1079 case kGPU_Enclave:
1080 tg.add(run_enclave_and_gpu_tests, &enclaves[i]);
1081 break;
1082 default:
1083 tg.add(run_enclave, &enclaves[i]);
1084 break;
mtklein82d28432015-01-15 12:46:02 -08001085 }
mtklein55e88b22015-01-21 15:50:13 -08001086 }
mtklein748ca3b2015-01-15 10:56:12 -08001087 tg.wait();
mtklein748ca3b2015-01-15 10:56:12 -08001088 // At this point we're back in single-threaded land.
caryclarkf53ce802015-06-15 06:48:30 -07001089 sk_tool_utils::release_portable_typefaces();
mtklein@google.comd36522d2013-10-16 13:02:15 +00001090
mtklein2f64eec2015-01-15 14:20:41 -08001091 SkDebugf("\n");
mtklein748ca3b2015-01-15 10:56:12 -08001092 if (gFailures.count() > 0) {
1093 SkDebugf("Failures:\n");
1094 for (int i = 0; i < gFailures.count(); i++) {
mtklein9dc09102015-01-15 15:47:33 -08001095 SkDebugf("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001096 }
1097 SkDebugf("%d failures\n", gFailures.count());
1098 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001099 }
mtklein748ca3b2015-01-15 10:56:12 -08001100 if (gPending > 0) {
1101 SkDebugf("Hrm, we didn't seem to run everything we intended to! Please file a bug.\n");
1102 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001103 }
halcanary7a14b312015-10-01 07:28:13 -07001104 #ifdef SK_PDF_IMAGE_STATS
1105 SkPDFImageDumpStats();
1106 #endif // SK_PDF_IMAGE_STATS
mtklein748ca3b2015-01-15 10:56:12 -08001107 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +00001108}
jcgregorio3b27ade2014-11-13 08:06:40 -08001109
borenet48087572015-04-02 12:16:36 -07001110#if !defined(SK_BUILD_FOR_IOS)
jcgregorio3b27ade2014-11-13 08:06:40 -08001111int main(int argc, char** argv) {
1112 SkCommandLineFlags::Parse(argc, argv);
1113 return dm_main();
1114}
1115#endif