blob: 11581fda81fc71e7d30a2aa9a226fe2b45b3385a [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
mtkleina5114d72015-08-24 13:27:01 -070032#ifdef SKIA_PNG_PREFIXED
33 // this must proceed png.h
34 #include "pngprefix.h"
35#endif
36#include "png.h"
37
bungeman60e0fee2015-08-26 05:15:46 -070038#include <stdlib.h>
39
djsollen54416de2015-04-03 07:24:48 -070040DEFINE_string(src, "tests gm skp image", "Source types to test.");
mtklein748ca3b2015-01-15 10:56:12 -080041DEFINE_bool(nameByHash, false,
42 "If true, write to FLAGS_writePath[0]/<hash>.png instead of "
djsollen54416de2015-04-03 07:24:48 -070043 "to FLAGS_writePath[0]/<config>/<sourceType>/<sourceOptions>/<name>.png");
mtklein748ca3b2015-01-15 10:56:12 -080044DEFINE_bool2(pathOpsExtended, x, false, "Run extended pathOps tests.");
mtkleind603b222015-02-17 11:13:33 -080045DEFINE_string(matrix, "1 0 0 1",
46 "2x2 scale+skew matrix to apply or upright when using "
47 "'matrix' or 'upright' in config.");
mtklein82d28432015-01-15 12:46:02 -080048DEFINE_bool(gpu_threading, false, "Allow GPU work to run on multiple threads?");
mtklein@google.comd36522d2013-10-16 13:02:15 +000049
mtkleina2ef6422015-01-15 13:44:22 -080050DEFINE_string(blacklist, "",
djsollen54416de2015-04-03 07:24:48 -070051 "Space-separated config/src/srcOptions/name quadruples to blacklist. '_' matches anything. E.g. \n"
52 "'--blacklist gpu skp _ _' will blacklist all SKPs drawn into the gpu config.\n"
53 "'--blacklist gpu skp _ _ 8888 gm _ aarects' will also blacklist the aarects GM on 8888.");
mtkleina2ef6422015-01-15 13:44:22 -080054
mtklein62bd1a62015-01-27 14:46:26 -080055DEFINE_string2(readPath, r, "", "If set check for equality with golden results in this directory.");
56
borenet09ed4802015-04-03 14:15:33 -070057DEFINE_string(uninterestingHashesFile, "",
58 "File containing a list of uninteresting hashes. If a result hashes to something in "
59 "this list, no image is written for that result.");
60
mtklein6393c062015-04-27 08:45:01 -070061DEFINE_int32(shards, 1, "We're splitting source data into this many shards.");
62DEFINE_int32(shard, 0, "Which shard do I run?");
bsalomon821e10e2015-06-04 14:15:33 -070063DEFINE_bool2(pre_log, p, false, "Log before running each test. May be incomprehensible when threading");
mtklein6393c062015-04-27 08:45:01 -070064
mtklein@google.comd36522d2013-10-16 13:02:15 +000065__SK_FORCE_IMAGE_DECODER_LINKING;
mtklein748ca3b2015-01-15 10:56:12 -080066using namespace DM;
mtklein@google.comd36522d2013-10-16 13:02:15 +000067
mtklein748ca3b2015-01-15 10:56:12 -080068/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
69
70SK_DECLARE_STATIC_MUTEX(gFailuresMutex);
71static SkTArray<SkString> gFailures;
72
73static void fail(ImplicitString err) {
74 SkAutoMutexAcquire lock(gFailuresMutex);
75 SkDebugf("\n\nFAILURE: %s\n\n", err.c_str());
76 gFailures.push_back(err);
halcanary9e398f72015-01-10 11:18:04 -080077}
78
mtkleinb37cb412015-03-18 05:27:14 -070079static int32_t gPending = 0; // Atomic. Total number of running and queued tasks.
80
81SK_DECLARE_STATIC_MUTEX(gRunningMutex);
82static SkTArray<SkString> gRunning;
mtklein748ca3b2015-01-15 10:56:12 -080083
mtkleinb9eb4ac2015-02-02 18:26:03 -080084static void done(double ms,
djsollen54416de2015-04-03 07:24:48 -070085 ImplicitString config, ImplicitString src, ImplicitString srcOptions,
86 ImplicitString name, ImplicitString note, ImplicitString log) {
87 SkString id = SkStringPrintf("%s %s %s %s", config.c_str(), src.c_str(),
88 srcOptions.c_str(), name.c_str());
mtkleinb37cb412015-03-18 05:27:14 -070089 {
90 SkAutoMutexAcquire lock(gRunningMutex);
91 for (int i = 0; i < gRunning.count(); i++) {
92 if (gRunning[i] == id) {
93 gRunning.removeShuffle(i);
94 break;
95 }
96 }
97 }
98 if (!FLAGS_verbose) {
99 note = "";
100 }
mtkleinb9eb4ac2015-02-02 18:26:03 -0800101 if (!log.isEmpty()) {
102 log.prepend("\n");
103 }
mtklein6dee2ad2015-02-05 09:53:44 -0800104 auto pending = sk_atomic_dec(&gPending)-1;
reed50bc0512015-05-19 14:13:31 -0700105 if (!FLAGS_quiet) {
mtklein711a2452015-07-08 08:49:20 -0700106 SkDebugf("%s(%4d/%-4dMB %6d) %s\t%s%s%s", FLAGS_verbose ? "\n" : kSkOverwriteLine
reed50bc0512015-05-19 14:13:31 -0700107 , sk_tools::getCurrResidentSetSizeMB()
108 , sk_tools::getMaxResidentSetSizeMB()
109 , pending
110 , HumanizeMs(ms).c_str()
111 , id.c_str()
112 , note.c_str()
113 , log.c_str());
114 }
mtkleina17241b2015-01-23 05:48:00 -0800115 // We write our dm.json file every once in a while in case we crash.
116 // Notice this also handles the final dm.json when pending == 0.
117 if (pending % 500 == 0) {
118 JsonWriter::DumpJson();
119 }
mtklein@google.comd36522d2013-10-16 13:02:15 +0000120}
121
djsollen54416de2015-04-03 07:24:48 -0700122static void start(ImplicitString config, ImplicitString src,
123 ImplicitString srcOptions, ImplicitString name) {
124 SkString id = SkStringPrintf("%s %s %s %s", config.c_str(), src.c_str(),
125 srcOptions.c_str(), name.c_str());
mtkleinb37cb412015-03-18 05:27:14 -0700126 SkAutoMutexAcquire lock(gRunningMutex);
127 gRunning.push_back(id);
128}
129
mtklein748ca3b2015-01-15 10:56:12 -0800130/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
mtklein114c3cd2015-01-15 10:15:02 -0800131
mtklein62bd1a62015-01-27 14:46:26 -0800132struct Gold : public SkString {
mtkleina82f5622015-02-20 12:30:19 -0800133 Gold() : SkString("") {}
djsollen54416de2015-04-03 07:24:48 -0700134 Gold(ImplicitString sink, ImplicitString src, ImplicitString srcOptions,
135 ImplicitString name, ImplicitString md5)
mtklein62bd1a62015-01-27 14:46:26 -0800136 : SkString("") {
137 this->append(sink);
138 this->append(src);
djsollen54416de2015-04-03 07:24:48 -0700139 this->append(srcOptions);
mtklein62bd1a62015-01-27 14:46:26 -0800140 this->append(name);
141 this->append(md5);
mtklein62bd1a62015-01-27 14:46:26 -0800142 }
mtklein02f46cf2015-03-20 13:48:42 -0700143 static uint32_t Hash(const Gold& g) { return SkGoodHash((const SkString&)g); }
mtklein62bd1a62015-01-27 14:46:26 -0800144};
mtkleina82f5622015-02-20 12:30:19 -0800145static SkTHashSet<Gold, Gold::Hash> gGold;
mtklein62bd1a62015-01-27 14:46:26 -0800146
147static void add_gold(JsonWriter::BitmapResult r) {
djsollen54416de2015-04-03 07:24:48 -0700148 gGold.add(Gold(r.config, r.sourceType, r.sourceOptions, r.name, r.md5));
mtklein62bd1a62015-01-27 14:46:26 -0800149}
150
151static void gather_gold() {
152 if (!FLAGS_readPath.isEmpty()) {
153 SkString path(FLAGS_readPath[0]);
154 path.append("/dm.json");
155 if (!JsonWriter::ReadJson(path.c_str(), add_gold)) {
156 fail(SkStringPrintf("Couldn't read %s for golden results.", path.c_str()));
157 }
158 }
159}
160
161/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
162
borenet09ed4802015-04-03 14:15:33 -0700163static SkTHashSet<SkString> gUninterestingHashes;
164
165static void gather_uninteresting_hashes() {
166 if (!FLAGS_uninterestingHashesFile.isEmpty()) {
167 SkAutoTUnref<SkData> data(SkData::NewFromFileName(FLAGS_uninterestingHashesFile[0]));
mtkleincc334b32015-09-22 11:43:53 -0700168 if (!data) {
169 SkDebugf("WARNING: unable to read uninteresting hashes from %s\n",
170 FLAGS_uninterestingHashesFile[0]);
171 return;
172 }
borenet09ed4802015-04-03 14:15:33 -0700173 SkTArray<SkString> hashes;
174 SkStrSplit((const char*)data->data(), "\n", &hashes);
175 for (const SkString& hash : hashes) {
176 gUninterestingHashes.add(hash);
177 }
178 }
179}
180
181/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
182
mtkleine0effd62015-07-29 06:37:28 -0700183struct TaggedSrc : public SkAutoTDelete<Src> {
msarett9e707a02015-09-01 14:57:57 -0700184 ImplicitString tag;
185 ImplicitString options;
mtkleine0effd62015-07-29 06:37:28 -0700186};
187
188struct TaggedSink : public SkAutoTDelete<Sink> {
189 const char* tag;
djsollen54416de2015-04-03 07:24:48 -0700190};
mtklein748ca3b2015-01-15 10:56:12 -0800191
192static const bool kMemcpyOK = true;
193
mtkleine0effd62015-07-29 06:37:28 -0700194static SkTArray<TaggedSrc, kMemcpyOK> gSrcs;
195static SkTArray<TaggedSink, kMemcpyOK> gSinks;
mtklein748ca3b2015-01-15 10:56:12 -0800196
mtklein6393c062015-04-27 08:45:01 -0700197static bool in_shard() {
198 static int N = 0;
199 return N++ % FLAGS_shards == FLAGS_shard;
200}
201
msarett9e707a02015-09-01 14:57:57 -0700202static void push_src(ImplicitString tag, ImplicitString options, Src* s) {
mtklein748ca3b2015-01-15 10:56:12 -0800203 SkAutoTDelete<Src> src(s);
mtklein6393c062015-04-27 08:45:01 -0700204 if (in_shard() &&
msarett9e707a02015-09-01 14:57:57 -0700205 FLAGS_src.contains(tag.c_str()) &&
mtklein748ca3b2015-01-15 10:56:12 -0800206 !SkCommandLineFlags::ShouldSkip(FLAGS_match, src->name().c_str())) {
mtkleine0effd62015-07-29 06:37:28 -0700207 TaggedSrc& s = gSrcs.push_back();
mtklein748ca3b2015-01-15 10:56:12 -0800208 s.reset(src.detach());
209 s.tag = tag;
djsollen54416de2015-04-03 07:24:48 -0700210 s.options = options;
mtklein114c3cd2015-01-15 10:15:02 -0800211 }
mtklein748ca3b2015-01-15 10:56:12 -0800212}
mtklein114c3cd2015-01-15 10:15:02 -0800213
msarett9e707a02015-09-01 14:57:57 -0700214static void push_codec_src(Path path, CodecSrc::Mode mode, CodecSrc::DstColorType dstColorType,
215 float scale) {
216 SkString folder;
217 switch (mode) {
218 case CodecSrc::kCodec_Mode:
219 folder.append("codec");
220 break;
221 case CodecSrc::kScaledCodec_Mode:
222 folder.append("scaled_codec");
223 break;
224 case CodecSrc::kScanline_Mode:
225 folder.append("scanline");
226 break;
227 case CodecSrc::kScanline_Subset_Mode:
228 folder.append("scanline_subset");
229 break;
230 case CodecSrc::kStripe_Mode:
231 folder.append("stripe");
232 break;
233 case CodecSrc::kSubset_Mode:
msarett36c37962015-09-02 13:20:52 -0700234 folder.append("codec_subset");
msarett9e707a02015-09-01 14:57:57 -0700235 break;
236 }
237
238 switch (dstColorType) {
239 case CodecSrc::kGrayscale_Always_DstColorType:
240 folder.append("_kGray8");
241 break;
242 case CodecSrc::kIndex8_Always_DstColorType:
243 folder.append("_kIndex8");
244 break;
245 default:
246 break;
247 }
248
249 if (1.0f != scale) {
250 folder.appendf("_%.3f", scale);
251 }
252
253 CodecSrc* src = new CodecSrc(path, mode, dstColorType, scale);
254 push_src("image", folder, src);
255}
256
msarett438b2ad2015-04-09 12:43:10 -0700257static void push_codec_srcs(Path path) {
258 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
259 if (!encoded) {
260 SkDebugf("Couldn't read %s.", path.c_str());
261 return;
262 }
263 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
halcanary96fcdcc2015-08-27 07:41:13 -0700264 if (nullptr == codec.get()) {
msarett438b2ad2015-04-09 12:43:10 -0700265 SkDebugf("Couldn't create codec for %s.", path.c_str());
266 return;
267 }
268
msarett36c37962015-09-02 13:20:52 -0700269 // Native Scales
msarett0a242972015-06-11 14:27:27 -0700270 // TODO (msarett): Implement scaling tests for SkImageDecoder in order to compare with these
271 // tests. SkImageDecoder supports downscales by integer factors.
emmaleer8f4ba762015-08-14 07:44:46 -0700272 // 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 -0700273 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 -0700274
msarett36c37962015-09-02 13:20:52 -0700275 const CodecSrc::Mode nativeModes[] = { CodecSrc::kCodec_Mode, CodecSrc::kScanline_Mode,
276 CodecSrc::kScanline_Subset_Mode, CodecSrc::kStripe_Mode, CodecSrc::kSubset_Mode };
msarett9e707a02015-09-01 14:57:57 -0700277
msarett36c37962015-09-02 13:20:52 -0700278 CodecSrc::DstColorType colorTypes[3];
279 uint32_t numColorTypes;
280 switch (codec->getInfo().colorType()) {
281 case kGray_8_SkColorType:
282 // FIXME: Is this a long term solution for testing wbmps decodes to kIndex8?
283 // Further discussion on this topic is at skbug.com/3683
284 colorTypes[0] = CodecSrc::kGetFromCanvas_DstColorType;
285 colorTypes[1] = CodecSrc::kGrayscale_Always_DstColorType;
286 colorTypes[2] = CodecSrc::kIndex8_Always_DstColorType;
287 numColorTypes = 3;
288 break;
289 case kIndex_8_SkColorType:
290 colorTypes[0] = CodecSrc::kGetFromCanvas_DstColorType;
291 colorTypes[1] = CodecSrc::kIndex8_Always_DstColorType;
292 numColorTypes = 2;
293 break;
294 default:
295 colorTypes[0] = CodecSrc::kGetFromCanvas_DstColorType;
296 numColorTypes = 1;
297 break;
298 }
msarett9e707a02015-09-01 14:57:57 -0700299
msarett36c37962015-09-02 13:20:52 -0700300 for (float scale : nativeScales) {
scroggo0df0e432015-08-07 12:53:25 -0700301 if (scale != 1.0f && (path.endsWith(".webp") || path.endsWith(".WEBP"))) {
302 // FIXME: skbug.com/4038 Scaling webp seems to leave some pixels uninitialized/
303 // compute their colors based on uninitialized values.
304 continue;
305 }
msarett0a242972015-06-11 14:27:27 -0700306
msarett36c37962015-09-02 13:20:52 -0700307 for (CodecSrc::Mode mode : nativeModes) {
308 for (uint32_t i = 0; i < numColorTypes; i++) {
309 push_codec_src(path, mode, colorTypes[i], scale);
msarett9e707a02015-09-01 14:57:57 -0700310 }
311 }
msarett0a242972015-06-11 14:27:27 -0700312 }
msarett36c37962015-09-02 13:20:52 -0700313
314 // SkScaledCodec Scales
315 // The native scales are included to make sure that SkScaledCodec defaults to the native
316 // scaling strategy when possible.
317 // 0.1, 0.16, 0.2 etc allow us to test SkScaledCodec with sampleSize 10, 6, 5, etc.
318 // 0.4, 0.7 etc allow to test what happens when the client requests a scale that
319 // does not exactly match a sampleSize or native scaling capability.
msarett6c50a8f2015-09-18 11:24:44 -0700320 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 -0700321 0.6f, 0.625f, 0.750f, 0.8f, 0.875f, 1.0f };
322
323 for (float scale : samplingScales) {
324 if (scale != 1.0f && (path.endsWith(".webp") || path.endsWith(".WEBP"))) {
325 // FIXME: skbug.com/4038 Scaling webp seems to leave some pixels uninitialized/
326 // compute their colors based on uninitialized values.
327 continue;
328 }
329
330 for (uint32_t i = 0; i < numColorTypes; i++) {
331 push_codec_src(path, CodecSrc::kScaledCodec_Mode, colorTypes[i], scale);
332 }
333 }
msarett438b2ad2015-04-09 12:43:10 -0700334}
335
msaretta5783ae2015-09-08 15:35:32 -0700336static bool brd_color_type_supported(SkBitmapRegionDecoderInterface::Strategy strategy,
337 CodecSrc::DstColorType dstColorType) {
338 switch (strategy) {
339 case SkBitmapRegionDecoderInterface::kCanvas_Strategy:
340 if (CodecSrc::kGetFromCanvas_DstColorType == dstColorType) {
341 return true;
342 }
343 return false;
344 case SkBitmapRegionDecoderInterface::kOriginal_Strategy:
345 switch (dstColorType) {
346 case CodecSrc::kGetFromCanvas_DstColorType:
347 case CodecSrc::kIndex8_Always_DstColorType:
348 case CodecSrc::kGrayscale_Always_DstColorType:
349 return true;
350 default:
351 return false;
352 }
353 default:
354 SkASSERT(false);
355 return false;
356 }
357}
358
359static void push_brd_src(Path path, SkBitmapRegionDecoderInterface::Strategy strategy,
360 CodecSrc::DstColorType dstColorType, BRDSrc::Mode mode, uint32_t sampleSize) {
361 SkString folder;
362 switch (strategy) {
363 case SkBitmapRegionDecoderInterface::kCanvas_Strategy:
364 folder.append("brd_canvas");
365 break;
366 case SkBitmapRegionDecoderInterface::kOriginal_Strategy:
367 folder.append("brd_sample");
368 break;
369 default:
370 SkASSERT(false);
371 return;
372 }
373
374 switch (mode) {
375 case BRDSrc::kFullImage_Mode:
376 break;
377 case BRDSrc::kDivisor_Mode:
378 folder.append("_divisor");
379 break;
380 default:
381 SkASSERT(false);
382 return;
383 }
384
385 switch (dstColorType) {
386 case CodecSrc::kGetFromCanvas_DstColorType:
387 break;
388 case CodecSrc::kIndex8_Always_DstColorType:
389 folder.append("_kIndex");
390 break;
391 case CodecSrc::kGrayscale_Always_DstColorType:
392 folder.append("_kGray");
393 break;
394 default:
395 SkASSERT(false);
396 return;
397 }
398
399 if (1 != sampleSize) {
msarett7f691442015-09-22 11:56:16 -0700400 folder.appendf("_%.3f", get_scale_from_sample_size(sampleSize));
msaretta5783ae2015-09-08 15:35:32 -0700401 }
402
403 BRDSrc* src = new BRDSrc(path, strategy, mode, dstColorType, sampleSize);
404 push_src("image", folder, src);
405}
406
407static void push_brd_srcs(Path path) {
408
409 const SkBitmapRegionDecoderInterface::Strategy strategies[] = {
410 SkBitmapRegionDecoderInterface::kCanvas_Strategy,
411 SkBitmapRegionDecoderInterface::kOriginal_Strategy
412 };
413
msarett6efbe052015-09-11 09:01:16 -0700414 const uint32_t sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
415
msaretta5783ae2015-09-08 15:35:32 -0700416 // We will only test to one backend (8888), but we will test all of the
417 // color types that we need to decode to on this backend.
418 const CodecSrc::DstColorType dstColorTypes[] = {
419 CodecSrc::kGetFromCanvas_DstColorType,
420 CodecSrc::kIndex8_Always_DstColorType,
421 CodecSrc::kGrayscale_Always_DstColorType,
422 };
423
424 const BRDSrc::Mode modes[] = {
425 BRDSrc::kFullImage_Mode,
426 BRDSrc::kDivisor_Mode
427 };
428
msaretta5783ae2015-09-08 15:35:32 -0700429 for (SkBitmapRegionDecoderInterface::Strategy strategy : strategies) {
msarett6efbe052015-09-11 09:01:16 -0700430
msaretta5783ae2015-09-08 15:35:32 -0700431 // We disable png testing for kOriginal_Strategy because the implementation leaks
432 // memory in our forked libpng.
433 // TODO (msarett): Decide if we want to test pngs in this mode and how we might do this.
434 if (SkBitmapRegionDecoderInterface::kOriginal_Strategy == strategy &&
435 (path.endsWith(".png") || path.endsWith(".PNG"))) {
436 continue;
437 }
msarett6efbe052015-09-11 09:01:16 -0700438 for (uint32_t sampleSize : sampleSizes) {
439
440 // kOriginal_Strategy does not work for jpegs that are scaled to non-powers of two.
441 // We don't need to test this. We know it doesn't work, and it causes images with
442 // uninitialized memory to show up on Gold.
443 if (SkBitmapRegionDecoderInterface::kOriginal_Strategy == strategy &&
444 (path.endsWith(".jpg") || path.endsWith(".JPG") ||
445 path.endsWith(".jpeg") || path.endsWith(".JPEG")) && !SkIsPow2(sampleSize)) {
446 continue;
447 }
448 for (CodecSrc::DstColorType dstColorType : dstColorTypes) {
449 if (brd_color_type_supported(strategy, dstColorType)) {
450 for (BRDSrc::Mode mode : modes) {
msaretta5783ae2015-09-08 15:35:32 -0700451 push_brd_src(path, strategy, dstColorType, mode, sampleSize);
452 }
453 }
454 }
455 }
456 }
457}
458
459static bool brd_supported(const char* ext) {
msarett8c8f22a2015-04-01 06:58:48 -0700460 static const char* const exts[] = {
msaretta5783ae2015-09-08 15:35:32 -0700461 "jpg", "jpeg", "png", "webp",
462 "JPG", "JPEG", "PNG", "WEBP",
msarett8c8f22a2015-04-01 06:58:48 -0700463 };
464
465 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
466 if (0 == strcmp(exts[i], ext)) {
467 return true;
468 }
469 }
470 return false;
scroggo9b77ddd2015-03-19 06:03:39 -0700471}
472
mtklein748ca3b2015-01-15 10:56:12 -0800473static void gather_srcs() {
474 for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) {
djsollen54416de2015-04-03 07:24:48 -0700475 push_src("gm", "", new GMSrc(r->factory()));
mtklein748ca3b2015-01-15 10:56:12 -0800476 }
halcanaryfc37ad12015-01-30 07:31:19 -0800477 for (int i = 0; i < FLAGS_skps.count(); i++) {
478 const char* path = FLAGS_skps[i];
479 if (sk_isdir(path)) {
480 SkOSFile::Iter it(path, "skp");
481 for (SkString file; it.next(&file); ) {
djsollen54416de2015-04-03 07:24:48 -0700482 push_src("skp", "", new SKPSrc(SkOSPath::Join(path, file.c_str())));
halcanaryfc37ad12015-01-30 07:31:19 -0800483 }
484 } else {
djsollen54416de2015-04-03 07:24:48 -0700485 push_src("skp", "", new SKPSrc(path));
mtklein114c3cd2015-01-15 10:15:02 -0800486 }
487 }
halcanary23b03c32015-01-30 09:58:58 -0800488 static const char* const exts[] = {
489 "bmp", "gif", "jpg", "jpeg", "png", "webp", "ktx", "astc", "wbmp", "ico",
490 "BMP", "GIF", "JPG", "JPEG", "PNG", "WEBP", "KTX", "ASTC", "WBMP", "ICO",
491 };
492 for (int i = 0; i < FLAGS_images.count(); i++) {
493 const char* flag = FLAGS_images[i];
494 if (sk_isdir(flag)) {
495 for (size_t j = 0; j < SK_ARRAY_COUNT(exts); j++) {
496 SkOSFile::Iter it(flag, exts[j]);
497 for (SkString file; it.next(&file); ) {
498 SkString path = SkOSPath::Join(flag, file.c_str());
djsollen54416de2015-04-03 07:24:48 -0700499 push_src("image", "decode", new ImageSrc(path)); // Decode entire image
500 push_src("image", "subset", new ImageSrc(path, 2)); // Decode into 2x2 subsets
msaretta5783ae2015-09-08 15:35:32 -0700501 push_codec_srcs(path);
502 if (brd_supported(exts[j])) {
503 push_brd_srcs(path);
scroggo9b77ddd2015-03-19 06:03:39 -0700504 }
halcanary23b03c32015-01-30 09:58:58 -0800505 }
mtklein114c3cd2015-01-15 10:15:02 -0800506 }
halcanary23b03c32015-01-30 09:58:58 -0800507 } else if (sk_exists(flag)) {
508 // assume that FLAGS_images[i] is a valid image if it is a file.
djsollen54416de2015-04-03 07:24:48 -0700509 push_src("image", "decode", new ImageSrc(flag)); // Decode entire image.
510 push_src("image", "subset", new ImageSrc(flag, 2)); // Decode into 2 x 2 subsets
msarett438b2ad2015-04-09 12:43:10 -0700511 push_codec_srcs(flag);
msaretta5783ae2015-09-08 15:35:32 -0700512 push_brd_srcs(flag);
mtklein709d2c32015-01-15 08:30:25 -0800513 }
mtklein709d2c32015-01-15 08:30:25 -0800514 }
515}
516
mtklein748ca3b2015-01-15 10:56:12 -0800517static GrGLStandard get_gpu_api() {
518 if (FLAGS_gpuAPI.contains("gl")) { return kGL_GrGLStandard; }
519 if (FLAGS_gpuAPI.contains("gles")) { return kGLES_GrGLStandard; }
520 return kNone_GrGLStandard;
mtklein709d2c32015-01-15 08:30:25 -0800521}
522
mtklein748ca3b2015-01-15 10:56:12 -0800523static void push_sink(const char* tag, Sink* s) {
524 SkAutoTDelete<Sink> sink(s);
525 if (!FLAGS_config.contains(tag)) {
526 return;
mtklein114c3cd2015-01-15 10:15:02 -0800527 }
mtkleine0effd62015-07-29 06:37:28 -0700528 // Try a simple Src as a canary. If it fails, skip this sink.
mtklein748ca3b2015-01-15 10:56:12 -0800529 struct : public Src {
mtkleine0effd62015-07-29 06:37:28 -0700530 Error draw(SkCanvas* c) const override {
531 c->drawRect(SkRect::MakeWH(1,1), SkPaint());
532 return "";
533 }
mtklein36352bf2015-03-25 18:17:31 -0700534 SkISize size() const override { return SkISize::Make(16, 16); }
mtkleine0effd62015-07-29 06:37:28 -0700535 Name name() const override { return "justOneRect"; }
536 } justOneRect;
mtklein114c3cd2015-01-15 10:15:02 -0800537
mtklein748ca3b2015-01-15 10:56:12 -0800538 SkBitmap bitmap;
539 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800540 SkString log;
mtkleine0effd62015-07-29 06:37:28 -0700541 Error err = sink->draw(justOneRect, &bitmap, &stream, &log);
mtklein4089ef72015-03-05 08:40:28 -0800542 if (err.isFatal()) {
mtklein05641a52015-04-21 10:49:13 -0700543 SkDebugf("Could not run %s: %s\n", tag, err.c_str());
544 exit(1);
mtklein114c3cd2015-01-15 10:15:02 -0800545 }
546
mtkleine0effd62015-07-29 06:37:28 -0700547 TaggedSink& ts = gSinks.push_back();
mtklein748ca3b2015-01-15 10:56:12 -0800548 ts.reset(sink.detach());
549 ts.tag = tag;
550}
551
552static bool gpu_supported() {
553#if SK_SUPPORT_GPU
554 return FLAGS_gpu;
555#else
556 return false;
557#endif
558}
559
560static Sink* create_sink(const char* tag) {
561#define SINK(t, sink, ...) if (0 == strcmp(t, tag)) { return new sink(__VA_ARGS__); }
562 if (gpu_supported()) {
mtklein82d28432015-01-15 12:46:02 -0800563 typedef GrContextFactory Gr;
mtklein748ca3b2015-01-15 10:56:12 -0800564 const GrGLStandard api = get_gpu_api();
hendrikw885bf092015-08-27 10:38:39 -0700565 SINK("gpunull", GPUSink, Gr::kNull_GLContextType, api, 0, false, FLAGS_gpu_threading);
566 SINK("gpudebug", GPUSink, Gr::kDebug_GLContextType, api, 0, false, FLAGS_gpu_threading);
567 SINK("gpu", GPUSink, Gr::kNative_GLContextType, api, 0, false, FLAGS_gpu_threading);
568 SINK("gpudft", GPUSink, Gr::kNative_GLContextType, api, 0, true, FLAGS_gpu_threading);
569 SINK("msaa4", GPUSink, Gr::kNative_GLContextType, api, 4, false, FLAGS_gpu_threading);
570 SINK("msaa16", GPUSink, Gr::kNative_GLContextType, api, 16, false, FLAGS_gpu_threading);
bsalomon90e8ab72015-09-08 10:26:51 -0700571 SINK("nvprmsaa4", GPUSink, Gr::kNVPR_GLContextType, api, 4, true, FLAGS_gpu_threading);
572 SINK("nvprmsaa16", GPUSink, Gr::kNVPR_GLContextType, api, 16, true, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800573 #if SK_ANGLE
hendrikw885bf092015-08-27 10:38:39 -0700574 SINK("angle", GPUSink, Gr::kANGLE_GLContextType, api, 0, false, FLAGS_gpu_threading);
hendrikweddbefb2015-09-11 13:07:29 -0700575 SINK("angle-gl", GPUSink, Gr::kANGLE_GL_GLContextType, api, 0, false, FLAGS_gpu_threading);
hendrikw885bf092015-08-27 10:38:39 -0700576 #endif
577 #if SK_COMMAND_BUFFER
578 SINK("commandbuffer", GPUSink, Gr::kCommandBuffer_GLContextType, api, 0, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800579 #endif
580 #if SK_MESA
hendrikw885bf092015-08-27 10:38:39 -0700581 SINK("mesa", GPUSink, Gr::kMESA_GLContextType, api, 0, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800582 #endif
mtklein114c3cd2015-01-15 10:15:02 -0800583 }
mtklein748ca3b2015-01-15 10:56:12 -0800584
tomhudsoneebc39a2015-02-23 12:18:05 -0800585#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
586 SINK("hwui", HWUISink);
587#endif
588
mtklein748ca3b2015-01-15 10:56:12 -0800589 if (FLAGS_cpu) {
590 SINK("565", RasterSink, kRGB_565_SkColorType);
591 SINK("8888", RasterSink, kN32_SkColorType);
halcanaryc11c62f2015-09-28 11:51:54 -0700592 SINK("pdf", PDFSink, "Pdfium");
593 SINK("pdf_poppler", PDFSink, "Poppler");
mtklein9c3f17d2015-01-28 11:35:18 -0800594 SINK("skp", SKPSink);
mtklein8a4527e2015-01-31 20:00:58 -0800595 SINK("svg", SVGSink);
596 SINK("null", NullSink);
halcanary47ef4d52015-03-03 09:13:09 -0800597 SINK("xps", XPSSink);
mtklein748ca3b2015-01-15 10:56:12 -0800598 }
599#undef SINK
halcanary96fcdcc2015-08-27 07:41:13 -0700600 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800601}
602
mtklein748ca3b2015-01-15 10:56:12 -0800603static Sink* create_via(const char* tag, Sink* wrapped) {
604#define VIA(t, via, ...) if (0 == strcmp(t, tag)) { return new via(__VA_ARGS__); }
halcanary96fcdcc2015-08-27 07:41:13 -0700605 VIA("twice", ViaTwice, wrapped);
606 VIA("pipe", ViaPipe, wrapped);
607 VIA("serialize", ViaSerialization, wrapped);
608 VIA("2ndpic", ViaSecondPicture, wrapped);
mtkleind31c13d2015-05-05 12:59:56 -0700609 VIA("sp", ViaSingletonPictures, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700610 VIA("tiles", ViaTiles, 256, 256, nullptr, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800611 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800612
mtkleind603b222015-02-17 11:13:33 -0800613 if (FLAGS_matrix.count() == 4) {
mtklein748ca3b2015-01-15 10:56:12 -0800614 SkMatrix m;
mtkleind603b222015-02-17 11:13:33 -0800615 m.reset();
616 m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
617 m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
618 m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
619 m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
620 VIA("matrix", ViaMatrix, m, wrapped);
621 VIA("upright", ViaUpright, m, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800622 }
tomhudson64de1e12015-03-05 08:01:07 -0800623
624#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
625 VIA("androidsdk", ViaAndroidSDK, wrapped);
626#endif
627
mtklein748ca3b2015-01-15 10:56:12 -0800628#undef VIA
halcanary96fcdcc2015-08-27 07:41:13 -0700629 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800630}
631
mtklein748ca3b2015-01-15 10:56:12 -0800632static void gather_sinks() {
633 for (int i = 0; i < FLAGS_config.count(); i++) {
634 const char* config = FLAGS_config[i];
635 SkTArray<SkString> parts;
636 SkStrSplit(config, "-", &parts);
637
halcanary96fcdcc2015-08-27 07:41:13 -0700638 Sink* sink = nullptr;
mtklein748ca3b2015-01-15 10:56:12 -0800639 for (int i = parts.count(); i-- > 0;) {
640 const char* part = parts[i].c_str();
halcanary96fcdcc2015-08-27 07:41:13 -0700641 Sink* next = (sink == nullptr) ? create_sink(part) : create_via(part, sink);
642 if (next == nullptr) {
mtklein748ca3b2015-01-15 10:56:12 -0800643 SkDebugf("Skipping %s: Don't understand '%s'.\n", config, part);
644 delete sink;
halcanary96fcdcc2015-08-27 07:41:13 -0700645 sink = nullptr;
mtklein748ca3b2015-01-15 10:56:12 -0800646 break;
647 }
648 sink = next;
649 }
650 if (sink) {
651 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800652 }
653 }
654}
mtklein709d2c32015-01-15 08:30:25 -0800655
mtkleina5114d72015-08-24 13:27:01 -0700656static bool dump_png(SkBitmap bitmap, const char* path, const char* md5) {
657 const int w = bitmap.width(),
658 h = bitmap.height();
659
660 // First get the bitmap into N32 color format. The next step will work only there.
661 if (bitmap.colorType() != kN32_SkColorType) {
662 SkBitmap n32;
663 if (!bitmap.copyTo(&n32, kN32_SkColorType)) {
664 return false;
665 }
666 bitmap = n32;
667 }
668
669 // Convert our N32 bitmap into unpremul RGBA for libpng.
670 SkAutoTMalloc<uint32_t> rgba(w*h);
671 if (!bitmap.readPixels(SkImageInfo::Make(w,h, kRGBA_8888_SkColorType, kUnpremul_SkAlphaType),
672 rgba, 4*w, 0,0)) {
673 return false;
674 }
675
676 // We don't need bitmap anymore. Might as well drop our ref.
mtkleinfccb77d2015-08-24 14:13:29 -0700677 bitmap.reset();
mtkleina5114d72015-08-24 13:27:01 -0700678
mtkleinc64137c2015-08-25 10:56:08 -0700679 FILE* f = fopen(path, "wb");
mtkleina5114d72015-08-24 13:27:01 -0700680 if (!f) { return false; }
681
682 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
683 if (!png) {
684 fclose(f);
685 return false;
686 }
687
688 png_infop info = png_create_info_struct(png);
689 if (!info) {
690 png_destroy_write_struct(&png, &info);
691 fclose(f);
692 return false;
693 }
694
mtkleind69ece62015-09-10 10:37:44 -0700695 SkString description;
696 description.append("Key: ");
697 for (int i = 0; i < FLAGS_key.count(); i++) {
698 description.appendf("%s ", FLAGS_key[i]);
699 }
700 description.append("Properties: ");
701 for (int i = 0; i < FLAGS_properties.count(); i++) {
702 description.appendf("%s ", FLAGS_properties[i]);
703 }
704 description.appendf("MD5: %s", md5);
705
mtkleina5114d72015-08-24 13:27:01 -0700706 png_text text[2];
707 text[0].key = (png_charp)"Author";
708 text[0].text = (png_charp)"DM dump_png()";
709 text[0].compression = PNG_TEXT_COMPRESSION_NONE;
710 text[1].key = (png_charp)"Description";
mtkleind69ece62015-09-10 10:37:44 -0700711 text[1].text = (png_charp)description.c_str();
mtkleina5114d72015-08-24 13:27:01 -0700712 text[1].compression = PNG_TEXT_COMPRESSION_NONE;
713 png_set_text(png, info, text, 2);
714
715 png_init_io(png, f);
716 png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8,
717 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
718 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
719 png_write_info(png, info);
720 for (int j = 0; j < h; j++) {
721 png_bytep row = (png_bytep)(rgba.get() + w*j);
722 png_write_rows(png, &row, 1);
723 }
724 png_write_end(png, info);
725
726 png_destroy_write_struct(&png, &info);
727 fclose(f);
728 return true;
729}
730
mtkleina2ef6422015-01-15 13:44:22 -0800731static bool match(const char* needle, const char* haystack) {
halcanary96fcdcc2015-08-27 07:41:13 -0700732 return 0 == strcmp("_", needle) || nullptr != strstr(haystack, needle);
mtkleina2ef6422015-01-15 13:44:22 -0800733}
734
djsollen54416de2015-04-03 07:24:48 -0700735static ImplicitString is_blacklisted(const char* sink, const char* src,
736 const char* srcOptions, const char* name) {
mtklein0d243ff2015-04-03 07:51:00 -0700737 for (int i = 0; i < FLAGS_blacklist.count() - 3; i += 4) {
mtkleina2ef6422015-01-15 13:44:22 -0800738 if (match(FLAGS_blacklist[i+0], sink) &&
djsollen54416de2015-04-03 07:24:48 -0700739 match(FLAGS_blacklist[i+1], src) &&
740 match(FLAGS_blacklist[i+2], srcOptions) &&
741 match(FLAGS_blacklist[i+3], name)) {
742 return SkStringPrintf("%s %s %s %s",
743 FLAGS_blacklist[i+0], FLAGS_blacklist[i+1],
744 FLAGS_blacklist[i+2], FLAGS_blacklist[i+3]);
mtkleina2ef6422015-01-15 13:44:22 -0800745 }
746 }
747 return "";
748}
749
mtklein748ca3b2015-01-15 10:56:12 -0800750// The finest-grained unit of work we can run: draw a single Src into a single Sink,
751// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
752struct Task {
mtkleine0effd62015-07-29 06:37:28 -0700753 Task(const TaggedSrc& src, const TaggedSink& sink) : src(src), sink(sink) {}
754 const TaggedSrc& src;
755 const TaggedSink& sink;
mtklein748ca3b2015-01-15 10:56:12 -0800756
757 static void Run(Task* task) {
mtkleina2ef6422015-01-15 13:44:22 -0800758 SkString name = task->src->name();
mtkleine0effd62015-07-29 06:37:28 -0700759
760 // We'll skip drawing this Src/Sink pair if:
761 // - the Src vetoes the Sink;
762 // - this Src / Sink combination is on the blacklist;
763 // - it's a dry run.
mtklein99cab4e2015-07-31 06:43:04 -0700764 SkString note(task->src->veto(task->sink->flags()) ? " (veto)" : "");
msarett9e707a02015-09-01 14:57:57 -0700765 SkString whyBlacklisted = is_blacklisted(task->sink.tag, task->src.tag.c_str(),
766 task->src.options.c_str(), name.c_str());
mtkleinb37cb412015-03-18 05:27:14 -0700767 if (!whyBlacklisted.isEmpty()) {
768 note.appendf(" (--blacklist %s)", whyBlacklisted.c_str());
769 }
mtkleine0effd62015-07-29 06:37:28 -0700770
mtkleinb9eb4ac2015-02-02 18:26:03 -0800771 SkString log;
mtklein748ca3b2015-01-15 10:56:12 -0800772 WallTimer timer;
773 timer.start();
mtkleine0effd62015-07-29 06:37:28 -0700774 if (!FLAGS_dryRun && note.isEmpty()) {
mtklein748ca3b2015-01-15 10:56:12 -0800775 SkBitmap bitmap;
776 SkDynamicMemoryWStream stream;
bsalomon821e10e2015-06-04 14:15:33 -0700777 if (FLAGS_pre_log) {
778 SkDebugf("\nRunning %s->%s", name.c_str(), task->sink.tag);
779 }
djsollen54416de2015-04-03 07:24:48 -0700780 start(task->sink.tag, task->src.tag, task->src.options, name.c_str());
mtkleinb9eb4ac2015-02-02 18:26:03 -0800781 Error err = task->sink->draw(*task->src, &bitmap, &stream, &log);
mtklein748ca3b2015-01-15 10:56:12 -0800782 if (!err.isEmpty()) {
mtklein4089ef72015-03-05 08:40:28 -0800783 timer.end();
784 if (err.isFatal()) {
djsollen54416de2015-04-03 07:24:48 -0700785 fail(SkStringPrintf("%s %s %s %s: %s",
mtklein4089ef72015-03-05 08:40:28 -0800786 task->sink.tag,
msarett9e707a02015-09-01 14:57:57 -0700787 task->src.tag.c_str(),
788 task->src.options.c_str(),
mtklein4089ef72015-03-05 08:40:28 -0800789 name.c_str(),
790 err.c_str()));
mtkleinb37cb412015-03-18 05:27:14 -0700791 } else {
792 note.appendf(" (skipped: %s)", err.c_str());
mtklein4089ef72015-03-05 08:40:28 -0800793 }
djsollen54416de2015-04-03 07:24:48 -0700794 done(timer.fWall, task->sink.tag, task->src.tag, task->src.options,
795 name, note, log);
mtklein4089ef72015-03-05 08:40:28 -0800796 return;
mtklein748ca3b2015-01-15 10:56:12 -0800797 }
mtklein62bd1a62015-01-27 14:46:26 -0800798 SkAutoTDelete<SkStreamAsset> data(stream.detachAsStream());
799
800 SkString md5;
801 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
802 SkMD5 hash;
803 if (data->getLength()) {
804 hash.writeStream(data, data->getLength());
805 data->rewind();
806 } else {
mtklein38408462015-07-08 07:25:27 -0700807 // If we're BGRA (Linux, Windows), swizzle over to RGBA (Mac, Android).
808 // This helps eliminate multiple 0-pixel-diff hashes on gold.skia.org.
809 // (Android's general slow speed breaks the tie arbitrarily in RGBA's favor.)
810 // We might consider promoting 565 to RGBA too.
811 if (bitmap.colorType() == kBGRA_8888_SkColorType) {
812 SkBitmap swizzle;
813 SkAssertResult(bitmap.copyTo(&swizzle, kRGBA_8888_SkColorType));
814 hash.write(swizzle.getPixels(), swizzle.getSize());
815 } else {
816 hash.write(bitmap.getPixels(), bitmap.getSize());
817 }
mtklein62bd1a62015-01-27 14:46:26 -0800818 }
819 SkMD5::Digest digest;
820 hash.finish(digest);
821 for (int i = 0; i < 16; i++) {
822 md5.appendf("%02x", digest.data[i]);
823 }
824 }
825
826 if (!FLAGS_readPath.isEmpty() &&
msarett9e707a02015-09-01 14:57:57 -0700827 !gGold.contains(Gold(task->sink.tag, task->src.tag.c_str(),
828 task->src.options.c_str(), name, md5))) {
djsollen54416de2015-04-03 07:24:48 -0700829 fail(SkStringPrintf("%s not found for %s %s %s %s in %s",
mtklein62bd1a62015-01-27 14:46:26 -0800830 md5.c_str(),
831 task->sink.tag,
msarett9e707a02015-09-01 14:57:57 -0700832 task->src.tag.c_str(),
833 task->src.options.c_str(),
mtklein62bd1a62015-01-27 14:46:26 -0800834 name.c_str(),
835 FLAGS_readPath[0]));
836 }
837
mtkleinb0531a72015-04-07 13:38:48 -0700838 if (!FLAGS_writePath.isEmpty()) {
mtklein748ca3b2015-01-15 10:56:12 -0800839 const char* ext = task->sink->fileExtension();
mtklein62bd1a62015-01-27 14:46:26 -0800840 if (data->getLength()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700841 WriteToDisk(*task, md5, ext, data, data->getLength(), nullptr);
halcanary022afb82015-01-30 11:00:12 -0800842 SkASSERT(bitmap.drawsNothing());
843 } else if (!bitmap.drawsNothing()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700844 WriteToDisk(*task, md5, ext, nullptr, 0, &bitmap);
mtklein748ca3b2015-01-15 10:56:12 -0800845 }
846 }
847 }
848 timer.end();
msarett9e707a02015-09-01 14:57:57 -0700849 done(timer.fWall, task->sink.tag, task->src.tag.c_str(), task->src.options.c_str(), name,
850 note, log);
mtklein748ca3b2015-01-15 10:56:12 -0800851 }
852
853 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -0800854 SkString md5,
855 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -0800856 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -0800857 const SkBitmap* bitmap) {
mtklein748ca3b2015-01-15 10:56:12 -0800858 JsonWriter::BitmapResult result;
djsollen54416de2015-04-03 07:24:48 -0700859 result.name = task.src->name();
860 result.config = task.sink.tag;
861 result.sourceType = task.src.tag;
862 result.sourceOptions = task.src.options;
863 result.ext = ext;
864 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -0800865 JsonWriter::AddBitmapResult(result);
866
mtkleinb0531a72015-04-07 13:38:48 -0700867 // If an MD5 is uninteresting, we want it noted in the JSON file,
868 // but don't want to dump it out as a .png (or whatever ext is).
869 if (gUninterestingHashes.contains(md5)) {
870 return;
871 }
872
mtklein748ca3b2015-01-15 10:56:12 -0800873 const char* dir = FLAGS_writePath[0];
874 if (0 == strcmp(dir, "@")) { // Needed for iOS.
875 dir = FLAGS_resourcePath[0];
876 }
877 sk_mkdir(dir);
878
879 SkString path;
880 if (FLAGS_nameByHash) {
881 path = SkOSPath::Join(dir, result.md5.c_str());
882 path.append(".");
883 path.append(ext);
884 if (sk_exists(path.c_str())) {
885 return; // Content-addressed. If it exists already, we're done.
886 }
887 } else {
888 path = SkOSPath::Join(dir, task.sink.tag);
889 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -0700890 path = SkOSPath::Join(path.c_str(), task.src.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800891 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -0700892 if (strcmp(task.src.options.c_str(), "") != 0) {
893 path = SkOSPath::Join(path.c_str(), task.src.options.c_str());
djsollen54416de2015-04-03 07:24:48 -0700894 sk_mkdir(path.c_str());
895 }
mtklein748ca3b2015-01-15 10:56:12 -0800896 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
897 path.append(".");
898 path.append(ext);
899 }
900
mtklein748ca3b2015-01-15 10:56:12 -0800901 if (bitmap) {
mtkleina5114d72015-08-24 13:27:01 -0700902 if (!dump_png(*bitmap, path.c_str(), result.md5.c_str())) {
mtklein748ca3b2015-01-15 10:56:12 -0800903 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
904 return;
905 }
906 } else {
mtkleina5114d72015-08-24 13:27:01 -0700907 SkFILEWStream file(path.c_str());
908 if (!file.isValid()) {
909 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
910 return;
911 }
mtklein748ca3b2015-01-15 10:56:12 -0800912 if (!file.writeStream(data, len)) {
913 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
914 return;
915 }
916 }
917 }
918};
919
920// Run all tasks in the same enclave serially on the same thread.
921// They can't possibly run concurrently with each other.
922static void run_enclave(SkTArray<Task>* tasks) {
923 for (int i = 0; i < tasks->count(); i++) {
924 Task::Run(tasks->begin() + i);
925 }
926}
927
928/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
929
930// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
931
mtklein55e88b22015-01-21 15:50:13 -0800932static SkTDArray<skiatest::Test> gThreadedTests, gGPUTests;
mtklein748ca3b2015-01-15 10:56:12 -0800933
934static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -0800935 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -0800936 return;
937 }
mtklein6393c062015-04-27 08:45:01 -0700938 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r; r = r->next()) {
939 if (!in_shard()) {
940 continue;
941 }
halcanary87f3ba42015-01-20 09:30:20 -0800942 // Despite its name, factory() is returning a reference to
943 // link-time static const POD data.
944 const skiatest::Test& test = r->factory();
945 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -0800946 continue;
947 }
halcanary87f3ba42015-01-20 09:30:20 -0800948 if (test.needsGpu && gpu_supported()) {
mtklein55e88b22015-01-21 15:50:13 -0800949 (FLAGS_gpu_threading ? gThreadedTests : gGPUTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -0800950 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein55e88b22015-01-21 15:50:13 -0800951 gThreadedTests.push(test);
mtklein82d28432015-01-15 12:46:02 -0800952 }
mtklein748ca3b2015-01-15 10:56:12 -0800953 }
954}
955
halcanary87f3ba42015-01-20 09:30:20 -0800956static void run_test(skiatest::Test* test) {
957 struct : public skiatest::Reporter {
mtklein36352bf2015-03-25 18:17:31 -0700958 void reportFailed(const skiatest::Failure& failure) override {
halcanary87f3ba42015-01-20 09:30:20 -0800959 fail(failure.toString());
960 JsonWriter::AddTestFailure(failure);
961 }
mtklein36352bf2015-03-25 18:17:31 -0700962 bool allowExtendedTest() const override {
halcanary87f3ba42015-01-20 09:30:20 -0800963 return FLAGS_pathOpsExtended;
964 }
mtklein36352bf2015-03-25 18:17:31 -0700965 bool verbose() const override { return FLAGS_veryVerbose; }
halcanary87f3ba42015-01-20 09:30:20 -0800966 } reporter;
djsollen824996a2015-06-12 12:06:22 -0700967
968 SkString note;
969 SkString whyBlacklisted = is_blacklisted("_", "tests", "_", test->name);
970 if (!whyBlacklisted.isEmpty()) {
971 note.appendf(" (--blacklist %s)", whyBlacklisted.c_str());
972 }
973
mtklein748ca3b2015-01-15 10:56:12 -0800974 WallTimer timer;
975 timer.start();
djsollen824996a2015-06-12 12:06:22 -0700976 if (!FLAGS_dryRun && whyBlacklisted.isEmpty()) {
djsollen54416de2015-04-03 07:24:48 -0700977 start("unit", "test", "", test->name);
mtklein55e88b22015-01-21 15:50:13 -0800978 GrContextFactory factory;
bsalomon821e10e2015-06-04 14:15:33 -0700979 if (FLAGS_pre_log) {
980 SkDebugf("\nRunning test %s", test->name);
981 }
mtklein55e88b22015-01-21 15:50:13 -0800982 test->proc(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -0800983 }
984 timer.end();
djsollen824996a2015-06-12 12:06:22 -0700985 done(timer.fWall, "unit", "test", "", test->name, note, "");
mtklein748ca3b2015-01-15 10:56:12 -0800986}
987
988/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
989
mtklein55e88b22015-01-21 15:50:13 -0800990// If we're isolating all GPU-bound work to one thread (the default), this function runs all that.
991static void run_enclave_and_gpu_tests(SkTArray<Task>* tasks) {
992 run_enclave(tasks);
993 for (int i = 0; i < gGPUTests.count(); i++) {
994 run_test(&gGPUTests[i]);
995 }
996}
997
mtkleinde6fc2b2015-03-12 06:28:54 -0700998// Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung.
999// This prints something every once in a while so that it knows we're still working.
mtklein2e1c47e2015-03-12 07:16:56 -07001000static void start_keepalive() {
1001 struct Loop {
1002 static void forever(void*) {
1003 for (;;) {
1004 static const int kSec = 300;
1005 #if defined(SK_BUILD_FOR_WIN)
1006 Sleep(kSec * 1000);
1007 #else
1008 sleep(kSec);
1009 #endif
mtkleinb37cb412015-03-18 05:27:14 -07001010 SkString running;
1011 {
1012 SkAutoMutexAcquire lock(gRunningMutex);
1013 for (int i = 0; i < gRunning.count(); i++) {
1014 running.appendf("\n\t%s", gRunning[i].c_str());
1015 }
1016 }
1017 SkDebugf("\nCurrently running:%s\n", running.c_str());
mtklein2e1c47e2015-03-12 07:16:56 -07001018 }
1019 }
1020 };
1021 static SkThread* intentionallyLeaked = new SkThread(Loop::forever);
1022 intentionallyLeaked->start();
mtkleinde6fc2b2015-03-12 06:28:54 -07001023}
1024
caryclark83ca6282015-06-10 09:31:09 -07001025#define PORTABLE_FONT_PREFIX "Toy Liberation "
1026
1027static SkTypeface* create_from_name(const char familyName[], SkTypeface::Style style) {
1028 if (familyName && strlen(familyName) > sizeof(PORTABLE_FONT_PREFIX)
1029 && !strncmp(familyName, PORTABLE_FONT_PREFIX, sizeof(PORTABLE_FONT_PREFIX) - 1)) {
caryclark1818acb2015-07-24 12:09:25 -07001030 return sk_tool_utils::create_portable_typeface(familyName, style);
caryclark83ca6282015-06-10 09:31:09 -07001031 }
halcanary96fcdcc2015-08-27 07:41:13 -07001032 return nullptr;
caryclark83ca6282015-06-10 09:31:09 -07001033}
1034
1035#undef PORTABLE_FONT_PREFIX
1036
1037extern SkTypeface* (*gCreateTypefaceDelegate)(const char [], SkTypeface::Style );
1038
jcgregorio3b27ade2014-11-13 08:06:40 -08001039int dm_main();
caryclark17f0b6d2014-07-22 10:15:34 -07001040int dm_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -08001041 SetupCrashHandler();
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +00001042 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -07001043 SkTaskGroup::Enabler enabled(FLAGS_threads);
caryclark83ca6282015-06-10 09:31:09 -07001044 gCreateTypefaceDelegate = &create_from_name;
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +00001045
mtklein2e1c47e2015-03-12 07:16:56 -07001046 start_keepalive();
mtkleinde6fc2b2015-03-12 06:28:54 -07001047
mtklein62bd1a62015-01-27 14:46:26 -08001048 gather_gold();
borenet09ed4802015-04-03 14:15:33 -07001049 gather_uninteresting_hashes();
mtklein62bd1a62015-01-27 14:46:26 -08001050
mtklein748ca3b2015-01-15 10:56:12 -08001051 gather_srcs();
1052 gather_sinks();
1053 gather_tests();
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +00001054
mtklein55e88b22015-01-21 15:50:13 -08001055 gPending = gSrcs.count() * gSinks.count() + gThreadedTests.count() + gGPUTests.count();
mtklein748ca3b2015-01-15 10:56:12 -08001056 SkDebugf("%d srcs * %d sinks + %d tests == %d tasks\n",
mtklein55e88b22015-01-21 15:50:13 -08001057 gSrcs.count(), gSinks.count(), gThreadedTests.count() + gGPUTests.count(), gPending);
mtklein748ca3b2015-01-15 10:56:12 -08001058
1059 // We try to exploit as much parallelism as is safe. Most Src/Sink pairs run on any thread,
1060 // but Sinks that identify as part of a particular enclave run serially on a single thread.
mtklein82d28432015-01-15 12:46:02 -08001061 // CPU tests run on any thread. GPU tests depend on --gpu_threading.
mtklein748ca3b2015-01-15 10:56:12 -08001062 SkTArray<Task> enclaves[kNumEnclaves];
1063 for (int j = 0; j < gSinks.count(); j++) {
1064 SkTArray<Task>& tasks = enclaves[gSinks[j]->enclave()];
1065 for (int i = 0; i < gSrcs.count(); i++) {
1066 tasks.push_back(Task(gSrcs[i], gSinks[j]));
1067 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001068 }
1069
mtklein748ca3b2015-01-15 10:56:12 -08001070 SkTaskGroup tg;
mtklein55e88b22015-01-21 15:50:13 -08001071 tg.batch(run_test, gThreadedTests.begin(), gThreadedTests.count());
1072 for (int i = 0; i < kNumEnclaves; i++) {
1073 switch(i) {
1074 case kAnyThread_Enclave:
1075 tg.batch(Task::Run, enclaves[i].begin(), enclaves[i].count());
1076 break;
1077 case kGPU_Enclave:
1078 tg.add(run_enclave_and_gpu_tests, &enclaves[i]);
1079 break;
1080 default:
1081 tg.add(run_enclave, &enclaves[i]);
1082 break;
mtklein82d28432015-01-15 12:46:02 -08001083 }
mtklein55e88b22015-01-21 15:50:13 -08001084 }
mtklein748ca3b2015-01-15 10:56:12 -08001085 tg.wait();
mtklein748ca3b2015-01-15 10:56:12 -08001086 // At this point we're back in single-threaded land.
caryclarkf53ce802015-06-15 06:48:30 -07001087 sk_tool_utils::release_portable_typefaces();
mtklein@google.comd36522d2013-10-16 13:02:15 +00001088
mtklein2f64eec2015-01-15 14:20:41 -08001089 SkDebugf("\n");
mtklein748ca3b2015-01-15 10:56:12 -08001090 if (gFailures.count() > 0) {
1091 SkDebugf("Failures:\n");
1092 for (int i = 0; i < gFailures.count(); i++) {
mtklein9dc09102015-01-15 15:47:33 -08001093 SkDebugf("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001094 }
1095 SkDebugf("%d failures\n", gFailures.count());
1096 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001097 }
mtklein748ca3b2015-01-15 10:56:12 -08001098 if (gPending > 0) {
1099 SkDebugf("Hrm, we didn't seem to run everything we intended to! Please file a bug.\n");
1100 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001101 }
mtklein748ca3b2015-01-15 10:56:12 -08001102 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +00001103}
jcgregorio3b27ade2014-11-13 08:06:40 -08001104
borenet48087572015-04-02 12:16:36 -07001105#if !defined(SK_BUILD_FOR_IOS)
jcgregorio3b27ade2014-11-13 08:06:40 -08001106int main(int argc, char** argv) {
1107 SkCommandLineFlags::Parse(argc, argv);
1108 return dm_main();
1109}
1110#endif