blob: 73dbc27e56f3ec553b6df0e5dbdb0438253d48ce [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"
caryclark17f0b6d2014-07-22 10:15:34 -070017#include "SkCommonFlags.h"
caryclark83ca6282015-06-10 09:31:09 -070018#include "SkFontMgr.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +000019#include "SkForceLinking.h"
20#include "SkGraphics.h"
mtklein748ca3b2015-01-15 10:56:12 -080021#include "SkMD5.h"
mtklein1b249332015-07-07 12:21:21 -070022#include "SkMutex.h"
mtklein1d0f1642014-09-08 08:05:18 -070023#include "SkOSFile.h"
mtkleina82f5622015-02-20 12:30:19 -080024#include "SkTHash.h"
mtklein406654b2014-09-03 15:34:37 -070025#include "SkTaskGroup.h"
mtkleinde6fc2b2015-03-12 06:28:54 -070026#include "SkThreadUtils.h"
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +000027#include "Test.h"
mtklein748ca3b2015-01-15 10:56:12 -080028#include "Timer.h"
caryclark83ca6282015-06-10 09:31:09 -070029#include "sk_tool_utils.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +000030
mtkleina5114d72015-08-24 13:27:01 -070031#ifdef SKIA_PNG_PREFIXED
32 // this must proceed png.h
33 #include "pngprefix.h"
34#endif
35#include "png.h"
36
bungeman60e0fee2015-08-26 05:15:46 -070037#include <stdlib.h>
38
djsollen54416de2015-04-03 07:24:48 -070039DEFINE_string(src, "tests gm skp image", "Source types to test.");
mtklein748ca3b2015-01-15 10:56:12 -080040DEFINE_bool(nameByHash, false,
41 "If true, write to FLAGS_writePath[0]/<hash>.png instead of "
djsollen54416de2015-04-03 07:24:48 -070042 "to FLAGS_writePath[0]/<config>/<sourceType>/<sourceOptions>/<name>.png");
mtklein748ca3b2015-01-15 10:56:12 -080043DEFINE_bool2(pathOpsExtended, x, false, "Run extended pathOps tests.");
mtkleind603b222015-02-17 11:13:33 -080044DEFINE_string(matrix, "1 0 0 1",
45 "2x2 scale+skew matrix to apply or upright when using "
46 "'matrix' or 'upright' in config.");
mtklein82d28432015-01-15 12:46:02 -080047DEFINE_bool(gpu_threading, false, "Allow GPU work to run on multiple threads?");
mtklein@google.comd36522d2013-10-16 13:02:15 +000048
mtkleina2ef6422015-01-15 13:44:22 -080049DEFINE_string(blacklist, "",
djsollen54416de2015-04-03 07:24:48 -070050 "Space-separated config/src/srcOptions/name quadruples to blacklist. '_' matches anything. E.g. \n"
51 "'--blacklist gpu skp _ _' will blacklist all SKPs drawn into the gpu config.\n"
52 "'--blacklist gpu skp _ _ 8888 gm _ aarects' will also blacklist the aarects GM on 8888.");
mtkleina2ef6422015-01-15 13:44:22 -080053
mtklein62bd1a62015-01-27 14:46:26 -080054DEFINE_string2(readPath, r, "", "If set check for equality with golden results in this directory.");
55
borenet09ed4802015-04-03 14:15:33 -070056DEFINE_string(uninterestingHashesFile, "",
57 "File containing a list of uninteresting hashes. If a result hashes to something in "
58 "this list, no image is written for that result.");
59
mtklein6393c062015-04-27 08:45:01 -070060DEFINE_int32(shards, 1, "We're splitting source data into this many shards.");
61DEFINE_int32(shard, 0, "Which shard do I run?");
bsalomon821e10e2015-06-04 14:15:33 -070062DEFINE_bool2(pre_log, p, false, "Log before running each test. May be incomprehensible when threading");
mtklein6393c062015-04-27 08:45:01 -070063
mtklein@google.comd36522d2013-10-16 13:02:15 +000064__SK_FORCE_IMAGE_DECODER_LINKING;
mtklein748ca3b2015-01-15 10:56:12 -080065using namespace DM;
mtklein@google.comd36522d2013-10-16 13:02:15 +000066
mtklein748ca3b2015-01-15 10:56:12 -080067/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
68
69SK_DECLARE_STATIC_MUTEX(gFailuresMutex);
70static SkTArray<SkString> gFailures;
71
72static void fail(ImplicitString err) {
73 SkAutoMutexAcquire lock(gFailuresMutex);
74 SkDebugf("\n\nFAILURE: %s\n\n", err.c_str());
75 gFailures.push_back(err);
halcanary9e398f72015-01-10 11:18:04 -080076}
77
mtkleinb37cb412015-03-18 05:27:14 -070078static int32_t gPending = 0; // Atomic. Total number of running and queued tasks.
79
80SK_DECLARE_STATIC_MUTEX(gRunningMutex);
81static SkTArray<SkString> gRunning;
mtklein748ca3b2015-01-15 10:56:12 -080082
mtkleinb9eb4ac2015-02-02 18:26:03 -080083static void done(double ms,
djsollen54416de2015-04-03 07:24:48 -070084 ImplicitString config, ImplicitString src, ImplicitString srcOptions,
85 ImplicitString name, ImplicitString note, ImplicitString log) {
86 SkString id = SkStringPrintf("%s %s %s %s", config.c_str(), src.c_str(),
87 srcOptions.c_str(), name.c_str());
mtkleinb37cb412015-03-18 05:27:14 -070088 {
89 SkAutoMutexAcquire lock(gRunningMutex);
90 for (int i = 0; i < gRunning.count(); i++) {
91 if (gRunning[i] == id) {
92 gRunning.removeShuffle(i);
93 break;
94 }
95 }
96 }
97 if (!FLAGS_verbose) {
98 note = "";
99 }
mtkleinb9eb4ac2015-02-02 18:26:03 -0800100 if (!log.isEmpty()) {
101 log.prepend("\n");
102 }
mtklein6dee2ad2015-02-05 09:53:44 -0800103 auto pending = sk_atomic_dec(&gPending)-1;
reed50bc0512015-05-19 14:13:31 -0700104 if (!FLAGS_quiet) {
mtklein711a2452015-07-08 08:49:20 -0700105 SkDebugf("%s(%4d/%-4dMB %6d) %s\t%s%s%s", FLAGS_verbose ? "\n" : kSkOverwriteLine
reed50bc0512015-05-19 14:13:31 -0700106 , sk_tools::getCurrResidentSetSizeMB()
107 , sk_tools::getMaxResidentSetSizeMB()
108 , pending
109 , HumanizeMs(ms).c_str()
110 , id.c_str()
111 , note.c_str()
112 , log.c_str());
113 }
mtkleina17241b2015-01-23 05:48:00 -0800114 // We write our dm.json file every once in a while in case we crash.
115 // Notice this also handles the final dm.json when pending == 0.
116 if (pending % 500 == 0) {
117 JsonWriter::DumpJson();
118 }
mtklein@google.comd36522d2013-10-16 13:02:15 +0000119}
120
djsollen54416de2015-04-03 07:24:48 -0700121static void start(ImplicitString config, ImplicitString src,
122 ImplicitString srcOptions, ImplicitString name) {
123 SkString id = SkStringPrintf("%s %s %s %s", config.c_str(), src.c_str(),
124 srcOptions.c_str(), name.c_str());
mtkleinb37cb412015-03-18 05:27:14 -0700125 SkAutoMutexAcquire lock(gRunningMutex);
126 gRunning.push_back(id);
127}
128
mtklein748ca3b2015-01-15 10:56:12 -0800129/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
mtklein114c3cd2015-01-15 10:15:02 -0800130
mtklein62bd1a62015-01-27 14:46:26 -0800131struct Gold : public SkString {
mtkleina82f5622015-02-20 12:30:19 -0800132 Gold() : SkString("") {}
djsollen54416de2015-04-03 07:24:48 -0700133 Gold(ImplicitString sink, ImplicitString src, ImplicitString srcOptions,
134 ImplicitString name, ImplicitString md5)
mtklein62bd1a62015-01-27 14:46:26 -0800135 : SkString("") {
136 this->append(sink);
137 this->append(src);
djsollen54416de2015-04-03 07:24:48 -0700138 this->append(srcOptions);
mtklein62bd1a62015-01-27 14:46:26 -0800139 this->append(name);
140 this->append(md5);
mtklein62bd1a62015-01-27 14:46:26 -0800141 }
mtklein02f46cf2015-03-20 13:48:42 -0700142 static uint32_t Hash(const Gold& g) { return SkGoodHash((const SkString&)g); }
mtklein62bd1a62015-01-27 14:46:26 -0800143};
mtkleina82f5622015-02-20 12:30:19 -0800144static SkTHashSet<Gold, Gold::Hash> gGold;
mtklein62bd1a62015-01-27 14:46:26 -0800145
146static void add_gold(JsonWriter::BitmapResult r) {
djsollen54416de2015-04-03 07:24:48 -0700147 gGold.add(Gold(r.config, r.sourceType, r.sourceOptions, r.name, r.md5));
mtklein62bd1a62015-01-27 14:46:26 -0800148}
149
150static void gather_gold() {
151 if (!FLAGS_readPath.isEmpty()) {
152 SkString path(FLAGS_readPath[0]);
153 path.append("/dm.json");
154 if (!JsonWriter::ReadJson(path.c_str(), add_gold)) {
155 fail(SkStringPrintf("Couldn't read %s for golden results.", path.c_str()));
156 }
157 }
158}
159
160/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
161
borenet09ed4802015-04-03 14:15:33 -0700162static SkTHashSet<SkString> gUninterestingHashes;
163
164static void gather_uninteresting_hashes() {
165 if (!FLAGS_uninterestingHashesFile.isEmpty()) {
166 SkAutoTUnref<SkData> data(SkData::NewFromFileName(FLAGS_uninterestingHashesFile[0]));
mtkleincc334b32015-09-22 11:43:53 -0700167 if (!data) {
168 SkDebugf("WARNING: unable to read uninteresting hashes from %s\n",
169 FLAGS_uninterestingHashesFile[0]);
170 return;
171 }
borenet09ed4802015-04-03 14:15:33 -0700172 SkTArray<SkString> hashes;
173 SkStrSplit((const char*)data->data(), "\n", &hashes);
174 for (const SkString& hash : hashes) {
175 gUninterestingHashes.add(hash);
176 }
177 }
178}
179
180/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
181
mtkleine0effd62015-07-29 06:37:28 -0700182struct TaggedSrc : public SkAutoTDelete<Src> {
msarett9e707a02015-09-01 14:57:57 -0700183 ImplicitString tag;
184 ImplicitString options;
mtkleine0effd62015-07-29 06:37:28 -0700185};
186
187struct TaggedSink : public SkAutoTDelete<Sink> {
188 const char* tag;
djsollen54416de2015-04-03 07:24:48 -0700189};
mtklein748ca3b2015-01-15 10:56:12 -0800190
191static const bool kMemcpyOK = true;
192
mtkleine0effd62015-07-29 06:37:28 -0700193static SkTArray<TaggedSrc, kMemcpyOK> gSrcs;
194static SkTArray<TaggedSink, kMemcpyOK> gSinks;
mtklein748ca3b2015-01-15 10:56:12 -0800195
mtklein6393c062015-04-27 08:45:01 -0700196static bool in_shard() {
197 static int N = 0;
198 return N++ % FLAGS_shards == FLAGS_shard;
199}
200
msarett9e707a02015-09-01 14:57:57 -0700201static void push_src(ImplicitString tag, ImplicitString options, Src* s) {
mtklein748ca3b2015-01-15 10:56:12 -0800202 SkAutoTDelete<Src> src(s);
mtklein6393c062015-04-27 08:45:01 -0700203 if (in_shard() &&
msarett9e707a02015-09-01 14:57:57 -0700204 FLAGS_src.contains(tag.c_str()) &&
mtklein748ca3b2015-01-15 10:56:12 -0800205 !SkCommandLineFlags::ShouldSkip(FLAGS_match, src->name().c_str())) {
mtkleine0effd62015-07-29 06:37:28 -0700206 TaggedSrc& s = gSrcs.push_back();
mtklein748ca3b2015-01-15 10:56:12 -0800207 s.reset(src.detach());
208 s.tag = tag;
djsollen54416de2015-04-03 07:24:48 -0700209 s.options = options;
mtklein114c3cd2015-01-15 10:15:02 -0800210 }
mtklein748ca3b2015-01-15 10:56:12 -0800211}
mtklein114c3cd2015-01-15 10:15:02 -0800212
msarett9e707a02015-09-01 14:57:57 -0700213static void push_codec_src(Path path, CodecSrc::Mode mode, CodecSrc::DstColorType dstColorType,
214 float scale) {
215 SkString folder;
216 switch (mode) {
217 case CodecSrc::kCodec_Mode:
218 folder.append("codec");
219 break;
220 case CodecSrc::kScaledCodec_Mode:
221 folder.append("scaled_codec");
222 break;
223 case CodecSrc::kScanline_Mode:
224 folder.append("scanline");
225 break;
226 case CodecSrc::kScanline_Subset_Mode:
227 folder.append("scanline_subset");
228 break;
229 case CodecSrc::kStripe_Mode:
230 folder.append("stripe");
231 break;
232 case CodecSrc::kSubset_Mode:
msarett36c37962015-09-02 13:20:52 -0700233 folder.append("codec_subset");
msarett9e707a02015-09-01 14:57:57 -0700234 break;
235 }
236
237 switch (dstColorType) {
238 case CodecSrc::kGrayscale_Always_DstColorType:
239 folder.append("_kGray8");
240 break;
241 case CodecSrc::kIndex8_Always_DstColorType:
242 folder.append("_kIndex8");
243 break;
244 default:
245 break;
246 }
247
248 if (1.0f != scale) {
249 folder.appendf("_%.3f", scale);
250 }
251
252 CodecSrc* src = new CodecSrc(path, mode, dstColorType, scale);
253 push_src("image", folder, src);
254}
255
msarett438b2ad2015-04-09 12:43:10 -0700256static void push_codec_srcs(Path path) {
257 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
258 if (!encoded) {
259 SkDebugf("Couldn't read %s.", path.c_str());
260 return;
261 }
262 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
halcanary96fcdcc2015-08-27 07:41:13 -0700263 if (nullptr == codec.get()) {
msarett438b2ad2015-04-09 12:43:10 -0700264 SkDebugf("Couldn't create codec for %s.", path.c_str());
265 return;
266 }
267
msarett36c37962015-09-02 13:20:52 -0700268 // Native Scales
msarett0a242972015-06-11 14:27:27 -0700269 // TODO (msarett): Implement scaling tests for SkImageDecoder in order to compare with these
270 // tests. SkImageDecoder supports downscales by integer factors.
emmaleer8f4ba762015-08-14 07:44:46 -0700271 // 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 -0700272 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 -0700273
msarett36c37962015-09-02 13:20:52 -0700274 const CodecSrc::Mode nativeModes[] = { CodecSrc::kCodec_Mode, CodecSrc::kScanline_Mode,
275 CodecSrc::kScanline_Subset_Mode, CodecSrc::kStripe_Mode, CodecSrc::kSubset_Mode };
msarett9e707a02015-09-01 14:57:57 -0700276
msarett36c37962015-09-02 13:20:52 -0700277 CodecSrc::DstColorType colorTypes[3];
278 uint32_t numColorTypes;
279 switch (codec->getInfo().colorType()) {
280 case kGray_8_SkColorType:
281 // FIXME: Is this a long term solution for testing wbmps decodes to kIndex8?
282 // Further discussion on this topic is at skbug.com/3683
283 colorTypes[0] = CodecSrc::kGetFromCanvas_DstColorType;
284 colorTypes[1] = CodecSrc::kGrayscale_Always_DstColorType;
285 colorTypes[2] = CodecSrc::kIndex8_Always_DstColorType;
286 numColorTypes = 3;
287 break;
288 case kIndex_8_SkColorType:
289 colorTypes[0] = CodecSrc::kGetFromCanvas_DstColorType;
290 colorTypes[1] = CodecSrc::kIndex8_Always_DstColorType;
291 numColorTypes = 2;
292 break;
293 default:
294 colorTypes[0] = CodecSrc::kGetFromCanvas_DstColorType;
295 numColorTypes = 1;
296 break;
297 }
msarett9e707a02015-09-01 14:57:57 -0700298
msarett36c37962015-09-02 13:20:52 -0700299 for (float scale : nativeScales) {
scroggo0df0e432015-08-07 12:53:25 -0700300 if (scale != 1.0f && (path.endsWith(".webp") || path.endsWith(".WEBP"))) {
301 // FIXME: skbug.com/4038 Scaling webp seems to leave some pixels uninitialized/
302 // compute their colors based on uninitialized values.
303 continue;
304 }
msarett0a242972015-06-11 14:27:27 -0700305
msarett36c37962015-09-02 13:20:52 -0700306 for (CodecSrc::Mode mode : nativeModes) {
307 for (uint32_t i = 0; i < numColorTypes; i++) {
308 push_codec_src(path, mode, colorTypes[i], scale);
msarett9e707a02015-09-01 14:57:57 -0700309 }
310 }
msarett0a242972015-06-11 14:27:27 -0700311 }
msarett36c37962015-09-02 13:20:52 -0700312
313 // SkScaledCodec Scales
314 // The native scales are included to make sure that SkScaledCodec defaults to the native
315 // scaling strategy when possible.
316 // 0.1, 0.16, 0.2 etc allow us to test SkScaledCodec with sampleSize 10, 6, 5, etc.
317 // 0.4, 0.7 etc allow to test what happens when the client requests a scale that
318 // does not exactly match a sampleSize or native scaling capability.
msarett6c50a8f2015-09-18 11:24:44 -0700319 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 -0700320 0.6f, 0.625f, 0.750f, 0.8f, 0.875f, 1.0f };
321
322 for (float scale : samplingScales) {
323 if (scale != 1.0f && (path.endsWith(".webp") || path.endsWith(".WEBP"))) {
324 // FIXME: skbug.com/4038 Scaling webp seems to leave some pixels uninitialized/
325 // compute their colors based on uninitialized values.
326 continue;
327 }
328
329 for (uint32_t i = 0; i < numColorTypes; i++) {
330 push_codec_src(path, CodecSrc::kScaledCodec_Mode, colorTypes[i], scale);
331 }
332 }
msarett438b2ad2015-04-09 12:43:10 -0700333}
334
msaretta5783ae2015-09-08 15:35:32 -0700335static bool brd_color_type_supported(SkBitmapRegionDecoderInterface::Strategy strategy,
336 CodecSrc::DstColorType dstColorType) {
337 switch (strategy) {
338 case SkBitmapRegionDecoderInterface::kCanvas_Strategy:
339 if (CodecSrc::kGetFromCanvas_DstColorType == dstColorType) {
340 return true;
341 }
342 return false;
343 case SkBitmapRegionDecoderInterface::kOriginal_Strategy:
344 switch (dstColorType) {
345 case CodecSrc::kGetFromCanvas_DstColorType:
346 case CodecSrc::kIndex8_Always_DstColorType:
347 case CodecSrc::kGrayscale_Always_DstColorType:
348 return true;
349 default:
350 return false;
351 }
352 default:
353 SkASSERT(false);
354 return false;
355 }
356}
357
358static void push_brd_src(Path path, SkBitmapRegionDecoderInterface::Strategy strategy,
359 CodecSrc::DstColorType dstColorType, BRDSrc::Mode mode, uint32_t sampleSize) {
360 SkString folder;
361 switch (strategy) {
362 case SkBitmapRegionDecoderInterface::kCanvas_Strategy:
363 folder.append("brd_canvas");
364 break;
365 case SkBitmapRegionDecoderInterface::kOriginal_Strategy:
366 folder.append("brd_sample");
367 break;
368 default:
369 SkASSERT(false);
370 return;
371 }
372
373 switch (mode) {
374 case BRDSrc::kFullImage_Mode:
375 break;
376 case BRDSrc::kDivisor_Mode:
377 folder.append("_divisor");
378 break;
379 default:
380 SkASSERT(false);
381 return;
382 }
383
384 switch (dstColorType) {
385 case CodecSrc::kGetFromCanvas_DstColorType:
386 break;
387 case CodecSrc::kIndex8_Always_DstColorType:
388 folder.append("_kIndex");
389 break;
390 case CodecSrc::kGrayscale_Always_DstColorType:
391 folder.append("_kGray");
392 break;
393 default:
394 SkASSERT(false);
395 return;
396 }
397
398 if (1 != sampleSize) {
399 folder.appendf("_%.3f", BRDSrc::GetScale(sampleSize));
400 }
401
402 BRDSrc* src = new BRDSrc(path, strategy, mode, dstColorType, sampleSize);
403 push_src("image", folder, src);
404}
405
406static void push_brd_srcs(Path path) {
407
408 const SkBitmapRegionDecoderInterface::Strategy strategies[] = {
409 SkBitmapRegionDecoderInterface::kCanvas_Strategy,
410 SkBitmapRegionDecoderInterface::kOriginal_Strategy
411 };
412
msarett6efbe052015-09-11 09:01:16 -0700413 const uint32_t sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
414
msaretta5783ae2015-09-08 15:35:32 -0700415 // We will only test to one backend (8888), but we will test all of the
416 // color types that we need to decode to on this backend.
417 const CodecSrc::DstColorType dstColorTypes[] = {
418 CodecSrc::kGetFromCanvas_DstColorType,
419 CodecSrc::kIndex8_Always_DstColorType,
420 CodecSrc::kGrayscale_Always_DstColorType,
421 };
422
423 const BRDSrc::Mode modes[] = {
424 BRDSrc::kFullImage_Mode,
425 BRDSrc::kDivisor_Mode
426 };
427
msaretta5783ae2015-09-08 15:35:32 -0700428 for (SkBitmapRegionDecoderInterface::Strategy strategy : strategies) {
msarett6efbe052015-09-11 09:01:16 -0700429
msaretta5783ae2015-09-08 15:35:32 -0700430 // We disable png testing for kOriginal_Strategy because the implementation leaks
431 // memory in our forked libpng.
432 // TODO (msarett): Decide if we want to test pngs in this mode and how we might do this.
433 if (SkBitmapRegionDecoderInterface::kOriginal_Strategy == strategy &&
434 (path.endsWith(".png") || path.endsWith(".PNG"))) {
435 continue;
436 }
msarett6efbe052015-09-11 09:01:16 -0700437 for (uint32_t sampleSize : sampleSizes) {
438
439 // kOriginal_Strategy does not work for jpegs that are scaled to non-powers of two.
440 // We don't need to test this. We know it doesn't work, and it causes images with
441 // uninitialized memory to show up on Gold.
442 if (SkBitmapRegionDecoderInterface::kOriginal_Strategy == strategy &&
443 (path.endsWith(".jpg") || path.endsWith(".JPG") ||
444 path.endsWith(".jpeg") || path.endsWith(".JPEG")) && !SkIsPow2(sampleSize)) {
445 continue;
446 }
447 for (CodecSrc::DstColorType dstColorType : dstColorTypes) {
448 if (brd_color_type_supported(strategy, dstColorType)) {
449 for (BRDSrc::Mode mode : modes) {
msaretta5783ae2015-09-08 15:35:32 -0700450 push_brd_src(path, strategy, dstColorType, mode, sampleSize);
451 }
452 }
453 }
454 }
455 }
456}
457
458static bool brd_supported(const char* ext) {
msarett8c8f22a2015-04-01 06:58:48 -0700459 static const char* const exts[] = {
msaretta5783ae2015-09-08 15:35:32 -0700460 "jpg", "jpeg", "png", "webp",
461 "JPG", "JPEG", "PNG", "WEBP",
msarett8c8f22a2015-04-01 06:58:48 -0700462 };
463
464 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
465 if (0 == strcmp(exts[i], ext)) {
466 return true;
467 }
468 }
469 return false;
scroggo9b77ddd2015-03-19 06:03:39 -0700470}
471
mtklein748ca3b2015-01-15 10:56:12 -0800472static void gather_srcs() {
473 for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) {
djsollen54416de2015-04-03 07:24:48 -0700474 push_src("gm", "", new GMSrc(r->factory()));
mtklein748ca3b2015-01-15 10:56:12 -0800475 }
halcanaryfc37ad12015-01-30 07:31:19 -0800476 for (int i = 0; i < FLAGS_skps.count(); i++) {
477 const char* path = FLAGS_skps[i];
478 if (sk_isdir(path)) {
479 SkOSFile::Iter it(path, "skp");
480 for (SkString file; it.next(&file); ) {
djsollen54416de2015-04-03 07:24:48 -0700481 push_src("skp", "", new SKPSrc(SkOSPath::Join(path, file.c_str())));
halcanaryfc37ad12015-01-30 07:31:19 -0800482 }
483 } else {
djsollen54416de2015-04-03 07:24:48 -0700484 push_src("skp", "", new SKPSrc(path));
mtklein114c3cd2015-01-15 10:15:02 -0800485 }
486 }
halcanary23b03c32015-01-30 09:58:58 -0800487 static const char* const exts[] = {
488 "bmp", "gif", "jpg", "jpeg", "png", "webp", "ktx", "astc", "wbmp", "ico",
489 "BMP", "GIF", "JPG", "JPEG", "PNG", "WEBP", "KTX", "ASTC", "WBMP", "ICO",
490 };
491 for (int i = 0; i < FLAGS_images.count(); i++) {
492 const char* flag = FLAGS_images[i];
493 if (sk_isdir(flag)) {
494 for (size_t j = 0; j < SK_ARRAY_COUNT(exts); j++) {
495 SkOSFile::Iter it(flag, exts[j]);
496 for (SkString file; it.next(&file); ) {
497 SkString path = SkOSPath::Join(flag, file.c_str());
djsollen54416de2015-04-03 07:24:48 -0700498 push_src("image", "decode", new ImageSrc(path)); // Decode entire image
499 push_src("image", "subset", new ImageSrc(path, 2)); // Decode into 2x2 subsets
msaretta5783ae2015-09-08 15:35:32 -0700500 push_codec_srcs(path);
501 if (brd_supported(exts[j])) {
502 push_brd_srcs(path);
scroggo9b77ddd2015-03-19 06:03:39 -0700503 }
halcanary23b03c32015-01-30 09:58:58 -0800504 }
mtklein114c3cd2015-01-15 10:15:02 -0800505 }
halcanary23b03c32015-01-30 09:58:58 -0800506 } else if (sk_exists(flag)) {
507 // assume that FLAGS_images[i] is a valid image if it is a file.
djsollen54416de2015-04-03 07:24:48 -0700508 push_src("image", "decode", new ImageSrc(flag)); // Decode entire image.
509 push_src("image", "subset", new ImageSrc(flag, 2)); // Decode into 2 x 2 subsets
msarett438b2ad2015-04-09 12:43:10 -0700510 push_codec_srcs(flag);
msaretta5783ae2015-09-08 15:35:32 -0700511 push_brd_srcs(flag);
mtklein709d2c32015-01-15 08:30:25 -0800512 }
mtklein709d2c32015-01-15 08:30:25 -0800513 }
514}
515
mtklein748ca3b2015-01-15 10:56:12 -0800516static GrGLStandard get_gpu_api() {
517 if (FLAGS_gpuAPI.contains("gl")) { return kGL_GrGLStandard; }
518 if (FLAGS_gpuAPI.contains("gles")) { return kGLES_GrGLStandard; }
519 return kNone_GrGLStandard;
mtklein709d2c32015-01-15 08:30:25 -0800520}
521
mtklein748ca3b2015-01-15 10:56:12 -0800522static void push_sink(const char* tag, Sink* s) {
523 SkAutoTDelete<Sink> sink(s);
524 if (!FLAGS_config.contains(tag)) {
525 return;
mtklein114c3cd2015-01-15 10:15:02 -0800526 }
mtkleine0effd62015-07-29 06:37:28 -0700527 // Try a simple Src as a canary. If it fails, skip this sink.
mtklein748ca3b2015-01-15 10:56:12 -0800528 struct : public Src {
mtkleine0effd62015-07-29 06:37:28 -0700529 Error draw(SkCanvas* c) const override {
530 c->drawRect(SkRect::MakeWH(1,1), SkPaint());
531 return "";
532 }
mtklein36352bf2015-03-25 18:17:31 -0700533 SkISize size() const override { return SkISize::Make(16, 16); }
mtkleine0effd62015-07-29 06:37:28 -0700534 Name name() const override { return "justOneRect"; }
535 } justOneRect;
mtklein114c3cd2015-01-15 10:15:02 -0800536
mtklein748ca3b2015-01-15 10:56:12 -0800537 SkBitmap bitmap;
538 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800539 SkString log;
mtkleine0effd62015-07-29 06:37:28 -0700540 Error err = sink->draw(justOneRect, &bitmap, &stream, &log);
mtklein4089ef72015-03-05 08:40:28 -0800541 if (err.isFatal()) {
mtklein05641a52015-04-21 10:49:13 -0700542 SkDebugf("Could not run %s: %s\n", tag, err.c_str());
543 exit(1);
mtklein114c3cd2015-01-15 10:15:02 -0800544 }
545
mtkleine0effd62015-07-29 06:37:28 -0700546 TaggedSink& ts = gSinks.push_back();
mtklein748ca3b2015-01-15 10:56:12 -0800547 ts.reset(sink.detach());
548 ts.tag = tag;
549}
550
551static bool gpu_supported() {
552#if SK_SUPPORT_GPU
553 return FLAGS_gpu;
554#else
555 return false;
556#endif
557}
558
559static Sink* create_sink(const char* tag) {
560#define SINK(t, sink, ...) if (0 == strcmp(t, tag)) { return new sink(__VA_ARGS__); }
561 if (gpu_supported()) {
mtklein82d28432015-01-15 12:46:02 -0800562 typedef GrContextFactory Gr;
mtklein748ca3b2015-01-15 10:56:12 -0800563 const GrGLStandard api = get_gpu_api();
hendrikw885bf092015-08-27 10:38:39 -0700564 SINK("gpunull", GPUSink, Gr::kNull_GLContextType, api, 0, false, FLAGS_gpu_threading);
565 SINK("gpudebug", GPUSink, Gr::kDebug_GLContextType, api, 0, false, FLAGS_gpu_threading);
566 SINK("gpu", GPUSink, Gr::kNative_GLContextType, api, 0, false, FLAGS_gpu_threading);
567 SINK("gpudft", GPUSink, Gr::kNative_GLContextType, api, 0, true, FLAGS_gpu_threading);
568 SINK("msaa4", GPUSink, Gr::kNative_GLContextType, api, 4, false, FLAGS_gpu_threading);
569 SINK("msaa16", GPUSink, Gr::kNative_GLContextType, api, 16, false, FLAGS_gpu_threading);
bsalomon90e8ab72015-09-08 10:26:51 -0700570 SINK("nvprmsaa4", GPUSink, Gr::kNVPR_GLContextType, api, 4, true, FLAGS_gpu_threading);
571 SINK("nvprmsaa16", GPUSink, Gr::kNVPR_GLContextType, api, 16, true, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800572 #if SK_ANGLE
hendrikw885bf092015-08-27 10:38:39 -0700573 SINK("angle", GPUSink, Gr::kANGLE_GLContextType, api, 0, false, FLAGS_gpu_threading);
hendrikweddbefb2015-09-11 13:07:29 -0700574 SINK("angle-gl", GPUSink, Gr::kANGLE_GL_GLContextType, api, 0, false, FLAGS_gpu_threading);
hendrikw885bf092015-08-27 10:38:39 -0700575 #endif
576 #if SK_COMMAND_BUFFER
577 SINK("commandbuffer", GPUSink, Gr::kCommandBuffer_GLContextType, api, 0, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800578 #endif
579 #if SK_MESA
hendrikw885bf092015-08-27 10:38:39 -0700580 SINK("mesa", GPUSink, Gr::kMESA_GLContextType, api, 0, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800581 #endif
mtklein114c3cd2015-01-15 10:15:02 -0800582 }
mtklein748ca3b2015-01-15 10:56:12 -0800583
tomhudsoneebc39a2015-02-23 12:18:05 -0800584#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
585 SINK("hwui", HWUISink);
586#endif
587
mtklein748ca3b2015-01-15 10:56:12 -0800588 if (FLAGS_cpu) {
589 SINK("565", RasterSink, kRGB_565_SkColorType);
590 SINK("8888", RasterSink, kN32_SkColorType);
mtklein19f30602015-01-20 13:34:39 -0800591 SINK("pdf", PDFSink);
mtklein9c3f17d2015-01-28 11:35:18 -0800592 SINK("skp", SKPSink);
mtklein8a4527e2015-01-31 20:00:58 -0800593 SINK("svg", SVGSink);
594 SINK("null", NullSink);
halcanary47ef4d52015-03-03 09:13:09 -0800595 SINK("xps", XPSSink);
mtklein748ca3b2015-01-15 10:56:12 -0800596 }
597#undef SINK
halcanary96fcdcc2015-08-27 07:41:13 -0700598 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800599}
600
mtklein748ca3b2015-01-15 10:56:12 -0800601static Sink* create_via(const char* tag, Sink* wrapped) {
602#define VIA(t, via, ...) if (0 == strcmp(t, tag)) { return new via(__VA_ARGS__); }
halcanary96fcdcc2015-08-27 07:41:13 -0700603 VIA("twice", ViaTwice, wrapped);
604 VIA("pipe", ViaPipe, wrapped);
605 VIA("serialize", ViaSerialization, wrapped);
606 VIA("2ndpic", ViaSecondPicture, wrapped);
mtkleind31c13d2015-05-05 12:59:56 -0700607 VIA("sp", ViaSingletonPictures, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700608 VIA("tiles", ViaTiles, 256, 256, nullptr, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800609 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800610
mtkleind603b222015-02-17 11:13:33 -0800611 if (FLAGS_matrix.count() == 4) {
mtklein748ca3b2015-01-15 10:56:12 -0800612 SkMatrix m;
mtkleind603b222015-02-17 11:13:33 -0800613 m.reset();
614 m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
615 m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
616 m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
617 m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
618 VIA("matrix", ViaMatrix, m, wrapped);
619 VIA("upright", ViaUpright, m, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800620 }
tomhudson64de1e12015-03-05 08:01:07 -0800621
622#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
623 VIA("androidsdk", ViaAndroidSDK, wrapped);
624#endif
625
mtklein748ca3b2015-01-15 10:56:12 -0800626#undef VIA
halcanary96fcdcc2015-08-27 07:41:13 -0700627 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800628}
629
mtklein748ca3b2015-01-15 10:56:12 -0800630static void gather_sinks() {
631 for (int i = 0; i < FLAGS_config.count(); i++) {
632 const char* config = FLAGS_config[i];
633 SkTArray<SkString> parts;
634 SkStrSplit(config, "-", &parts);
635
halcanary96fcdcc2015-08-27 07:41:13 -0700636 Sink* sink = nullptr;
mtklein748ca3b2015-01-15 10:56:12 -0800637 for (int i = parts.count(); i-- > 0;) {
638 const char* part = parts[i].c_str();
halcanary96fcdcc2015-08-27 07:41:13 -0700639 Sink* next = (sink == nullptr) ? create_sink(part) : create_via(part, sink);
640 if (next == nullptr) {
mtklein748ca3b2015-01-15 10:56:12 -0800641 SkDebugf("Skipping %s: Don't understand '%s'.\n", config, part);
642 delete sink;
halcanary96fcdcc2015-08-27 07:41:13 -0700643 sink = nullptr;
mtklein748ca3b2015-01-15 10:56:12 -0800644 break;
645 }
646 sink = next;
647 }
648 if (sink) {
649 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800650 }
651 }
652}
mtklein709d2c32015-01-15 08:30:25 -0800653
mtkleina5114d72015-08-24 13:27:01 -0700654static bool dump_png(SkBitmap bitmap, const char* path, const char* md5) {
655 const int w = bitmap.width(),
656 h = bitmap.height();
657
658 // First get the bitmap into N32 color format. The next step will work only there.
659 if (bitmap.colorType() != kN32_SkColorType) {
660 SkBitmap n32;
661 if (!bitmap.copyTo(&n32, kN32_SkColorType)) {
662 return false;
663 }
664 bitmap = n32;
665 }
666
667 // Convert our N32 bitmap into unpremul RGBA for libpng.
668 SkAutoTMalloc<uint32_t> rgba(w*h);
669 if (!bitmap.readPixels(SkImageInfo::Make(w,h, kRGBA_8888_SkColorType, kUnpremul_SkAlphaType),
670 rgba, 4*w, 0,0)) {
671 return false;
672 }
673
674 // We don't need bitmap anymore. Might as well drop our ref.
mtkleinfccb77d2015-08-24 14:13:29 -0700675 bitmap.reset();
mtkleina5114d72015-08-24 13:27:01 -0700676
mtkleinc64137c2015-08-25 10:56:08 -0700677 FILE* f = fopen(path, "wb");
mtkleina5114d72015-08-24 13:27:01 -0700678 if (!f) { return false; }
679
680 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
681 if (!png) {
682 fclose(f);
683 return false;
684 }
685
686 png_infop info = png_create_info_struct(png);
687 if (!info) {
688 png_destroy_write_struct(&png, &info);
689 fclose(f);
690 return false;
691 }
692
mtkleind69ece62015-09-10 10:37:44 -0700693 SkString description;
694 description.append("Key: ");
695 for (int i = 0; i < FLAGS_key.count(); i++) {
696 description.appendf("%s ", FLAGS_key[i]);
697 }
698 description.append("Properties: ");
699 for (int i = 0; i < FLAGS_properties.count(); i++) {
700 description.appendf("%s ", FLAGS_properties[i]);
701 }
702 description.appendf("MD5: %s", md5);
703
mtkleina5114d72015-08-24 13:27:01 -0700704 png_text text[2];
705 text[0].key = (png_charp)"Author";
706 text[0].text = (png_charp)"DM dump_png()";
707 text[0].compression = PNG_TEXT_COMPRESSION_NONE;
708 text[1].key = (png_charp)"Description";
mtkleind69ece62015-09-10 10:37:44 -0700709 text[1].text = (png_charp)description.c_str();
mtkleina5114d72015-08-24 13:27:01 -0700710 text[1].compression = PNG_TEXT_COMPRESSION_NONE;
711 png_set_text(png, info, text, 2);
712
713 png_init_io(png, f);
714 png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8,
715 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
716 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
717 png_write_info(png, info);
718 for (int j = 0; j < h; j++) {
719 png_bytep row = (png_bytep)(rgba.get() + w*j);
720 png_write_rows(png, &row, 1);
721 }
722 png_write_end(png, info);
723
724 png_destroy_write_struct(&png, &info);
725 fclose(f);
726 return true;
727}
728
mtkleina2ef6422015-01-15 13:44:22 -0800729static bool match(const char* needle, const char* haystack) {
halcanary96fcdcc2015-08-27 07:41:13 -0700730 return 0 == strcmp("_", needle) || nullptr != strstr(haystack, needle);
mtkleina2ef6422015-01-15 13:44:22 -0800731}
732
djsollen54416de2015-04-03 07:24:48 -0700733static ImplicitString is_blacklisted(const char* sink, const char* src,
734 const char* srcOptions, const char* name) {
mtklein0d243ff2015-04-03 07:51:00 -0700735 for (int i = 0; i < FLAGS_blacklist.count() - 3; i += 4) {
mtkleina2ef6422015-01-15 13:44:22 -0800736 if (match(FLAGS_blacklist[i+0], sink) &&
djsollen54416de2015-04-03 07:24:48 -0700737 match(FLAGS_blacklist[i+1], src) &&
738 match(FLAGS_blacklist[i+2], srcOptions) &&
739 match(FLAGS_blacklist[i+3], name)) {
740 return SkStringPrintf("%s %s %s %s",
741 FLAGS_blacklist[i+0], FLAGS_blacklist[i+1],
742 FLAGS_blacklist[i+2], FLAGS_blacklist[i+3]);
mtkleina2ef6422015-01-15 13:44:22 -0800743 }
744 }
745 return "";
746}
747
mtklein748ca3b2015-01-15 10:56:12 -0800748// The finest-grained unit of work we can run: draw a single Src into a single Sink,
749// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
750struct Task {
mtkleine0effd62015-07-29 06:37:28 -0700751 Task(const TaggedSrc& src, const TaggedSink& sink) : src(src), sink(sink) {}
752 const TaggedSrc& src;
753 const TaggedSink& sink;
mtklein748ca3b2015-01-15 10:56:12 -0800754
755 static void Run(Task* task) {
mtkleina2ef6422015-01-15 13:44:22 -0800756 SkString name = task->src->name();
mtkleine0effd62015-07-29 06:37:28 -0700757
758 // We'll skip drawing this Src/Sink pair if:
759 // - the Src vetoes the Sink;
760 // - this Src / Sink combination is on the blacklist;
761 // - it's a dry run.
mtklein99cab4e2015-07-31 06:43:04 -0700762 SkString note(task->src->veto(task->sink->flags()) ? " (veto)" : "");
msarett9e707a02015-09-01 14:57:57 -0700763 SkString whyBlacklisted = is_blacklisted(task->sink.tag, task->src.tag.c_str(),
764 task->src.options.c_str(), name.c_str());
mtkleinb37cb412015-03-18 05:27:14 -0700765 if (!whyBlacklisted.isEmpty()) {
766 note.appendf(" (--blacklist %s)", whyBlacklisted.c_str());
767 }
mtkleine0effd62015-07-29 06:37:28 -0700768
mtkleinb9eb4ac2015-02-02 18:26:03 -0800769 SkString log;
mtklein748ca3b2015-01-15 10:56:12 -0800770 WallTimer timer;
771 timer.start();
mtkleine0effd62015-07-29 06:37:28 -0700772 if (!FLAGS_dryRun && note.isEmpty()) {
mtklein748ca3b2015-01-15 10:56:12 -0800773 SkBitmap bitmap;
774 SkDynamicMemoryWStream stream;
bsalomon821e10e2015-06-04 14:15:33 -0700775 if (FLAGS_pre_log) {
776 SkDebugf("\nRunning %s->%s", name.c_str(), task->sink.tag);
777 }
djsollen54416de2015-04-03 07:24:48 -0700778 start(task->sink.tag, task->src.tag, task->src.options, name.c_str());
mtkleinb9eb4ac2015-02-02 18:26:03 -0800779 Error err = task->sink->draw(*task->src, &bitmap, &stream, &log);
mtklein748ca3b2015-01-15 10:56:12 -0800780 if (!err.isEmpty()) {
mtklein4089ef72015-03-05 08:40:28 -0800781 timer.end();
782 if (err.isFatal()) {
djsollen54416de2015-04-03 07:24:48 -0700783 fail(SkStringPrintf("%s %s %s %s: %s",
mtklein4089ef72015-03-05 08:40:28 -0800784 task->sink.tag,
msarett9e707a02015-09-01 14:57:57 -0700785 task->src.tag.c_str(),
786 task->src.options.c_str(),
mtklein4089ef72015-03-05 08:40:28 -0800787 name.c_str(),
788 err.c_str()));
mtkleinb37cb412015-03-18 05:27:14 -0700789 } else {
790 note.appendf(" (skipped: %s)", err.c_str());
mtklein4089ef72015-03-05 08:40:28 -0800791 }
djsollen54416de2015-04-03 07:24:48 -0700792 done(timer.fWall, task->sink.tag, task->src.tag, task->src.options,
793 name, note, log);
mtklein4089ef72015-03-05 08:40:28 -0800794 return;
mtklein748ca3b2015-01-15 10:56:12 -0800795 }
mtklein62bd1a62015-01-27 14:46:26 -0800796 SkAutoTDelete<SkStreamAsset> data(stream.detachAsStream());
797
798 SkString md5;
799 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
800 SkMD5 hash;
801 if (data->getLength()) {
802 hash.writeStream(data, data->getLength());
803 data->rewind();
804 } else {
mtklein38408462015-07-08 07:25:27 -0700805 // If we're BGRA (Linux, Windows), swizzle over to RGBA (Mac, Android).
806 // This helps eliminate multiple 0-pixel-diff hashes on gold.skia.org.
807 // (Android's general slow speed breaks the tie arbitrarily in RGBA's favor.)
808 // We might consider promoting 565 to RGBA too.
809 if (bitmap.colorType() == kBGRA_8888_SkColorType) {
810 SkBitmap swizzle;
811 SkAssertResult(bitmap.copyTo(&swizzle, kRGBA_8888_SkColorType));
812 hash.write(swizzle.getPixels(), swizzle.getSize());
813 } else {
814 hash.write(bitmap.getPixels(), bitmap.getSize());
815 }
mtklein62bd1a62015-01-27 14:46:26 -0800816 }
817 SkMD5::Digest digest;
818 hash.finish(digest);
819 for (int i = 0; i < 16; i++) {
820 md5.appendf("%02x", digest.data[i]);
821 }
822 }
823
824 if (!FLAGS_readPath.isEmpty() &&
msarett9e707a02015-09-01 14:57:57 -0700825 !gGold.contains(Gold(task->sink.tag, task->src.tag.c_str(),
826 task->src.options.c_str(), name, md5))) {
djsollen54416de2015-04-03 07:24:48 -0700827 fail(SkStringPrintf("%s not found for %s %s %s %s in %s",
mtklein62bd1a62015-01-27 14:46:26 -0800828 md5.c_str(),
829 task->sink.tag,
msarett9e707a02015-09-01 14:57:57 -0700830 task->src.tag.c_str(),
831 task->src.options.c_str(),
mtklein62bd1a62015-01-27 14:46:26 -0800832 name.c_str(),
833 FLAGS_readPath[0]));
834 }
835
mtkleinb0531a72015-04-07 13:38:48 -0700836 if (!FLAGS_writePath.isEmpty()) {
mtklein748ca3b2015-01-15 10:56:12 -0800837 const char* ext = task->sink->fileExtension();
mtklein62bd1a62015-01-27 14:46:26 -0800838 if (data->getLength()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700839 WriteToDisk(*task, md5, ext, data, data->getLength(), nullptr);
halcanary022afb82015-01-30 11:00:12 -0800840 SkASSERT(bitmap.drawsNothing());
841 } else if (!bitmap.drawsNothing()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700842 WriteToDisk(*task, md5, ext, nullptr, 0, &bitmap);
mtklein748ca3b2015-01-15 10:56:12 -0800843 }
844 }
845 }
846 timer.end();
msarett9e707a02015-09-01 14:57:57 -0700847 done(timer.fWall, task->sink.tag, task->src.tag.c_str(), task->src.options.c_str(), name,
848 note, log);
mtklein748ca3b2015-01-15 10:56:12 -0800849 }
850
851 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -0800852 SkString md5,
853 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -0800854 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -0800855 const SkBitmap* bitmap) {
mtklein748ca3b2015-01-15 10:56:12 -0800856 JsonWriter::BitmapResult result;
djsollen54416de2015-04-03 07:24:48 -0700857 result.name = task.src->name();
858 result.config = task.sink.tag;
859 result.sourceType = task.src.tag;
860 result.sourceOptions = task.src.options;
861 result.ext = ext;
862 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -0800863 JsonWriter::AddBitmapResult(result);
864
mtkleinb0531a72015-04-07 13:38:48 -0700865 // If an MD5 is uninteresting, we want it noted in the JSON file,
866 // but don't want to dump it out as a .png (or whatever ext is).
867 if (gUninterestingHashes.contains(md5)) {
868 return;
869 }
870
mtklein748ca3b2015-01-15 10:56:12 -0800871 const char* dir = FLAGS_writePath[0];
872 if (0 == strcmp(dir, "@")) { // Needed for iOS.
873 dir = FLAGS_resourcePath[0];
874 }
875 sk_mkdir(dir);
876
877 SkString path;
878 if (FLAGS_nameByHash) {
879 path = SkOSPath::Join(dir, result.md5.c_str());
880 path.append(".");
881 path.append(ext);
882 if (sk_exists(path.c_str())) {
883 return; // Content-addressed. If it exists already, we're done.
884 }
885 } else {
886 path = SkOSPath::Join(dir, task.sink.tag);
887 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -0700888 path = SkOSPath::Join(path.c_str(), task.src.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800889 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -0700890 if (strcmp(task.src.options.c_str(), "") != 0) {
891 path = SkOSPath::Join(path.c_str(), task.src.options.c_str());
djsollen54416de2015-04-03 07:24:48 -0700892 sk_mkdir(path.c_str());
893 }
mtklein748ca3b2015-01-15 10:56:12 -0800894 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
895 path.append(".");
896 path.append(ext);
897 }
898
mtklein748ca3b2015-01-15 10:56:12 -0800899 if (bitmap) {
mtkleina5114d72015-08-24 13:27:01 -0700900 if (!dump_png(*bitmap, path.c_str(), result.md5.c_str())) {
mtklein748ca3b2015-01-15 10:56:12 -0800901 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
902 return;
903 }
904 } else {
mtkleina5114d72015-08-24 13:27:01 -0700905 SkFILEWStream file(path.c_str());
906 if (!file.isValid()) {
907 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
908 return;
909 }
mtklein748ca3b2015-01-15 10:56:12 -0800910 if (!file.writeStream(data, len)) {
911 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
912 return;
913 }
914 }
915 }
916};
917
918// Run all tasks in the same enclave serially on the same thread.
919// They can't possibly run concurrently with each other.
920static void run_enclave(SkTArray<Task>* tasks) {
921 for (int i = 0; i < tasks->count(); i++) {
922 Task::Run(tasks->begin() + i);
923 }
924}
925
926/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
927
928// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
929
mtklein55e88b22015-01-21 15:50:13 -0800930static SkTDArray<skiatest::Test> gThreadedTests, gGPUTests;
mtklein748ca3b2015-01-15 10:56:12 -0800931
932static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -0800933 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -0800934 return;
935 }
mtklein6393c062015-04-27 08:45:01 -0700936 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r; r = r->next()) {
937 if (!in_shard()) {
938 continue;
939 }
halcanary87f3ba42015-01-20 09:30:20 -0800940 // Despite its name, factory() is returning a reference to
941 // link-time static const POD data.
942 const skiatest::Test& test = r->factory();
943 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -0800944 continue;
945 }
halcanary87f3ba42015-01-20 09:30:20 -0800946 if (test.needsGpu && gpu_supported()) {
mtklein55e88b22015-01-21 15:50:13 -0800947 (FLAGS_gpu_threading ? gThreadedTests : gGPUTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -0800948 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein55e88b22015-01-21 15:50:13 -0800949 gThreadedTests.push(test);
mtklein82d28432015-01-15 12:46:02 -0800950 }
mtklein748ca3b2015-01-15 10:56:12 -0800951 }
952}
953
halcanary87f3ba42015-01-20 09:30:20 -0800954static void run_test(skiatest::Test* test) {
955 struct : public skiatest::Reporter {
mtklein36352bf2015-03-25 18:17:31 -0700956 void reportFailed(const skiatest::Failure& failure) override {
halcanary87f3ba42015-01-20 09:30:20 -0800957 fail(failure.toString());
958 JsonWriter::AddTestFailure(failure);
959 }
mtklein36352bf2015-03-25 18:17:31 -0700960 bool allowExtendedTest() const override {
halcanary87f3ba42015-01-20 09:30:20 -0800961 return FLAGS_pathOpsExtended;
962 }
mtklein36352bf2015-03-25 18:17:31 -0700963 bool verbose() const override { return FLAGS_veryVerbose; }
halcanary87f3ba42015-01-20 09:30:20 -0800964 } reporter;
djsollen824996a2015-06-12 12:06:22 -0700965
966 SkString note;
967 SkString whyBlacklisted = is_blacklisted("_", "tests", "_", test->name);
968 if (!whyBlacklisted.isEmpty()) {
969 note.appendf(" (--blacklist %s)", whyBlacklisted.c_str());
970 }
971
mtklein748ca3b2015-01-15 10:56:12 -0800972 WallTimer timer;
973 timer.start();
djsollen824996a2015-06-12 12:06:22 -0700974 if (!FLAGS_dryRun && whyBlacklisted.isEmpty()) {
djsollen54416de2015-04-03 07:24:48 -0700975 start("unit", "test", "", test->name);
mtklein55e88b22015-01-21 15:50:13 -0800976 GrContextFactory factory;
bsalomon821e10e2015-06-04 14:15:33 -0700977 if (FLAGS_pre_log) {
978 SkDebugf("\nRunning test %s", test->name);
979 }
mtklein55e88b22015-01-21 15:50:13 -0800980 test->proc(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -0800981 }
982 timer.end();
djsollen824996a2015-06-12 12:06:22 -0700983 done(timer.fWall, "unit", "test", "", test->name, note, "");
mtklein748ca3b2015-01-15 10:56:12 -0800984}
985
986/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
987
mtklein55e88b22015-01-21 15:50:13 -0800988// If we're isolating all GPU-bound work to one thread (the default), this function runs all that.
989static void run_enclave_and_gpu_tests(SkTArray<Task>* tasks) {
990 run_enclave(tasks);
991 for (int i = 0; i < gGPUTests.count(); i++) {
992 run_test(&gGPUTests[i]);
993 }
994}
995
mtkleinde6fc2b2015-03-12 06:28:54 -0700996// Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung.
997// This prints something every once in a while so that it knows we're still working.
mtklein2e1c47e2015-03-12 07:16:56 -0700998static void start_keepalive() {
999 struct Loop {
1000 static void forever(void*) {
1001 for (;;) {
1002 static const int kSec = 300;
1003 #if defined(SK_BUILD_FOR_WIN)
1004 Sleep(kSec * 1000);
1005 #else
1006 sleep(kSec);
1007 #endif
mtkleinb37cb412015-03-18 05:27:14 -07001008 SkString running;
1009 {
1010 SkAutoMutexAcquire lock(gRunningMutex);
1011 for (int i = 0; i < gRunning.count(); i++) {
1012 running.appendf("\n\t%s", gRunning[i].c_str());
1013 }
1014 }
1015 SkDebugf("\nCurrently running:%s\n", running.c_str());
mtklein2e1c47e2015-03-12 07:16:56 -07001016 }
1017 }
1018 };
1019 static SkThread* intentionallyLeaked = new SkThread(Loop::forever);
1020 intentionallyLeaked->start();
mtkleinde6fc2b2015-03-12 06:28:54 -07001021}
1022
caryclark83ca6282015-06-10 09:31:09 -07001023#define PORTABLE_FONT_PREFIX "Toy Liberation "
1024
1025static SkTypeface* create_from_name(const char familyName[], SkTypeface::Style style) {
1026 if (familyName && strlen(familyName) > sizeof(PORTABLE_FONT_PREFIX)
1027 && !strncmp(familyName, PORTABLE_FONT_PREFIX, sizeof(PORTABLE_FONT_PREFIX) - 1)) {
caryclark1818acb2015-07-24 12:09:25 -07001028 return sk_tool_utils::create_portable_typeface(familyName, style);
caryclark83ca6282015-06-10 09:31:09 -07001029 }
halcanary96fcdcc2015-08-27 07:41:13 -07001030 return nullptr;
caryclark83ca6282015-06-10 09:31:09 -07001031}
1032
1033#undef PORTABLE_FONT_PREFIX
1034
1035extern SkTypeface* (*gCreateTypefaceDelegate)(const char [], SkTypeface::Style );
1036
jcgregorio3b27ade2014-11-13 08:06:40 -08001037int dm_main();
caryclark17f0b6d2014-07-22 10:15:34 -07001038int dm_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -08001039 SetupCrashHandler();
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +00001040 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -07001041 SkTaskGroup::Enabler enabled(FLAGS_threads);
caryclark83ca6282015-06-10 09:31:09 -07001042 gCreateTypefaceDelegate = &create_from_name;
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +00001043
mtklein2e1c47e2015-03-12 07:16:56 -07001044 start_keepalive();
mtkleinde6fc2b2015-03-12 06:28:54 -07001045
mtklein62bd1a62015-01-27 14:46:26 -08001046 gather_gold();
borenet09ed4802015-04-03 14:15:33 -07001047 gather_uninteresting_hashes();
mtklein62bd1a62015-01-27 14:46:26 -08001048
mtklein748ca3b2015-01-15 10:56:12 -08001049 gather_srcs();
1050 gather_sinks();
1051 gather_tests();
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +00001052
mtklein55e88b22015-01-21 15:50:13 -08001053 gPending = gSrcs.count() * gSinks.count() + gThreadedTests.count() + gGPUTests.count();
mtklein748ca3b2015-01-15 10:56:12 -08001054 SkDebugf("%d srcs * %d sinks + %d tests == %d tasks\n",
mtklein55e88b22015-01-21 15:50:13 -08001055 gSrcs.count(), gSinks.count(), gThreadedTests.count() + gGPUTests.count(), gPending);
mtklein748ca3b2015-01-15 10:56:12 -08001056
1057 // We try to exploit as much parallelism as is safe. Most Src/Sink pairs run on any thread,
1058 // but Sinks that identify as part of a particular enclave run serially on a single thread.
mtklein82d28432015-01-15 12:46:02 -08001059 // CPU tests run on any thread. GPU tests depend on --gpu_threading.
mtklein748ca3b2015-01-15 10:56:12 -08001060 SkTArray<Task> enclaves[kNumEnclaves];
1061 for (int j = 0; j < gSinks.count(); j++) {
1062 SkTArray<Task>& tasks = enclaves[gSinks[j]->enclave()];
1063 for (int i = 0; i < gSrcs.count(); i++) {
1064 tasks.push_back(Task(gSrcs[i], gSinks[j]));
1065 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001066 }
1067
mtklein748ca3b2015-01-15 10:56:12 -08001068 SkTaskGroup tg;
mtklein55e88b22015-01-21 15:50:13 -08001069 tg.batch(run_test, gThreadedTests.begin(), gThreadedTests.count());
1070 for (int i = 0; i < kNumEnclaves; i++) {
1071 switch(i) {
1072 case kAnyThread_Enclave:
1073 tg.batch(Task::Run, enclaves[i].begin(), enclaves[i].count());
1074 break;
1075 case kGPU_Enclave:
1076 tg.add(run_enclave_and_gpu_tests, &enclaves[i]);
1077 break;
1078 default:
1079 tg.add(run_enclave, &enclaves[i]);
1080 break;
mtklein82d28432015-01-15 12:46:02 -08001081 }
mtklein55e88b22015-01-21 15:50:13 -08001082 }
mtklein748ca3b2015-01-15 10:56:12 -08001083 tg.wait();
mtklein748ca3b2015-01-15 10:56:12 -08001084 // At this point we're back in single-threaded land.
caryclarkf53ce802015-06-15 06:48:30 -07001085 sk_tool_utils::release_portable_typefaces();
mtklein@google.comd36522d2013-10-16 13:02:15 +00001086
mtklein2f64eec2015-01-15 14:20:41 -08001087 SkDebugf("\n");
mtklein748ca3b2015-01-15 10:56:12 -08001088 if (gFailures.count() > 0) {
1089 SkDebugf("Failures:\n");
1090 for (int i = 0; i < gFailures.count(); i++) {
mtklein9dc09102015-01-15 15:47:33 -08001091 SkDebugf("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001092 }
1093 SkDebugf("%d failures\n", gFailures.count());
1094 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001095 }
mtklein748ca3b2015-01-15 10:56:12 -08001096 if (gPending > 0) {
1097 SkDebugf("Hrm, we didn't seem to run everything we intended to! Please file a bug.\n");
1098 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001099 }
mtklein748ca3b2015-01-15 10:56:12 -08001100 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +00001101}
jcgregorio3b27ade2014-11-13 08:06:40 -08001102
borenet48087572015-04-02 12:16:36 -07001103#if !defined(SK_BUILD_FOR_IOS)
jcgregorio3b27ade2014-11-13 08:06:40 -08001104int main(int argc, char** argv) {
1105 SkCommandLineFlags::Parse(argc, argv);
1106 return dm_main();
1107}
1108#endif