blob: 2a96a9e630f5f00198cf4d696c6cd68c38964dab [file] [log] [blame]
scroggo478652e2015-03-25 07:11:02 -07001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
jcgregorio3b27ade2014-11-13 08:06:40 -08008#include "CrashHandler.h"
mtklein748ca3b2015-01-15 10:56:12 -08009#include "DMJsonWriter.h"
10#include "DMSrcSink.h"
tomhudsoneebc39a2015-02-23 12:18:05 -080011#include "DMSrcSinkAndroid.h"
mtklein748ca3b2015-01-15 10:56:12 -080012#include "OverwriteLine.h"
13#include "ProcStats.h"
14#include "SkBBHFactory.h"
mtklein62bd1a62015-01-27 14:46:26 -080015#include "SkChecksum.h"
scroggocc2feb12015-08-14 08:32:46 -070016#include "SkCodec.h"
msarett7f691442015-09-22 11:56:16 -070017#include "SkCodecTools.h"
caryclark17f0b6d2014-07-22 10:15:34 -070018#include "SkCommonFlags.h"
caryclark83ca6282015-06-10 09:31:09 -070019#include "SkFontMgr.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +000020#include "SkForceLinking.h"
21#include "SkGraphics.h"
mtklein748ca3b2015-01-15 10:56:12 -080022#include "SkMD5.h"
mtklein1b249332015-07-07 12:21:21 -070023#include "SkMutex.h"
mtklein1d0f1642014-09-08 08:05:18 -070024#include "SkOSFile.h"
mtkleina82f5622015-02-20 12:30:19 -080025#include "SkTHash.h"
mtklein406654b2014-09-03 15:34:37 -070026#include "SkTaskGroup.h"
mtkleinde6fc2b2015-03-12 06:28:54 -070027#include "SkThreadUtils.h"
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +000028#include "Test.h"
mtklein748ca3b2015-01-15 10:56:12 -080029#include "Timer.h"
caryclark83ca6282015-06-10 09:31:09 -070030#include "sk_tool_utils.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +000031
halcanary7a14b312015-10-01 07:28:13 -070032#ifdef SK_PDF_IMAGE_STATS
33extern void SkPDFImageDumpStats();
34#endif
35
mtkleina5114d72015-08-24 13:27:01 -070036#ifdef SKIA_PNG_PREFIXED
37 // this must proceed png.h
38 #include "pngprefix.h"
39#endif
40#include "png.h"
41
bungeman60e0fee2015-08-26 05:15:46 -070042#include <stdlib.h>
43
djsollen54416de2015-04-03 07:24:48 -070044DEFINE_string(src, "tests gm skp image", "Source types to test.");
mtklein748ca3b2015-01-15 10:56:12 -080045DEFINE_bool(nameByHash, false,
46 "If true, write to FLAGS_writePath[0]/<hash>.png instead of "
djsollen54416de2015-04-03 07:24:48 -070047 "to FLAGS_writePath[0]/<config>/<sourceType>/<sourceOptions>/<name>.png");
mtklein748ca3b2015-01-15 10:56:12 -080048DEFINE_bool2(pathOpsExtended, x, false, "Run extended pathOps tests.");
mtkleind603b222015-02-17 11:13:33 -080049DEFINE_string(matrix, "1 0 0 1",
50 "2x2 scale+skew matrix to apply or upright when using "
51 "'matrix' or 'upright' in config.");
mtklein82d28432015-01-15 12:46:02 -080052DEFINE_bool(gpu_threading, false, "Allow GPU work to run on multiple threads?");
mtklein@google.comd36522d2013-10-16 13:02:15 +000053
mtkleina2ef6422015-01-15 13:44:22 -080054DEFINE_string(blacklist, "",
djsollen54416de2015-04-03 07:24:48 -070055 "Space-separated config/src/srcOptions/name quadruples to blacklist. '_' matches anything. E.g. \n"
56 "'--blacklist gpu skp _ _' will blacklist all SKPs drawn into the gpu config.\n"
57 "'--blacklist gpu skp _ _ 8888 gm _ aarects' will also blacklist the aarects GM on 8888.");
mtkleina2ef6422015-01-15 13:44:22 -080058
mtklein62bd1a62015-01-27 14:46:26 -080059DEFINE_string2(readPath, r, "", "If set check for equality with golden results in this directory.");
60
borenet09ed4802015-04-03 14:15:33 -070061DEFINE_string(uninterestingHashesFile, "",
62 "File containing a list of uninteresting hashes. If a result hashes to something in "
63 "this list, no image is written for that result.");
64
mtklein6393c062015-04-27 08:45:01 -070065DEFINE_int32(shards, 1, "We're splitting source data into this many shards.");
66DEFINE_int32(shard, 0, "Which shard do I run?");
bsalomon821e10e2015-06-04 14:15:33 -070067DEFINE_bool2(pre_log, p, false, "Log before running each test. May be incomprehensible when threading");
mtklein6393c062015-04-27 08:45:01 -070068
mtklein@google.comd36522d2013-10-16 13:02:15 +000069__SK_FORCE_IMAGE_DECODER_LINKING;
mtklein748ca3b2015-01-15 10:56:12 -080070using namespace DM;
mtklein@google.comd36522d2013-10-16 13:02:15 +000071
mtklein748ca3b2015-01-15 10:56:12 -080072/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
73
74SK_DECLARE_STATIC_MUTEX(gFailuresMutex);
75static SkTArray<SkString> gFailures;
76
77static void fail(ImplicitString err) {
78 SkAutoMutexAcquire lock(gFailuresMutex);
79 SkDebugf("\n\nFAILURE: %s\n\n", err.c_str());
80 gFailures.push_back(err);
halcanary9e398f72015-01-10 11:18:04 -080081}
82
mtkleinb37cb412015-03-18 05:27:14 -070083static int32_t gPending = 0; // Atomic. Total number of running and queued tasks.
84
85SK_DECLARE_STATIC_MUTEX(gRunningMutex);
86static SkTArray<SkString> gRunning;
mtklein748ca3b2015-01-15 10:56:12 -080087
mtkleinb9eb4ac2015-02-02 18:26:03 -080088static void done(double ms,
djsollen54416de2015-04-03 07:24:48 -070089 ImplicitString config, ImplicitString src, ImplicitString srcOptions,
90 ImplicitString name, ImplicitString note, ImplicitString log) {
91 SkString id = SkStringPrintf("%s %s %s %s", config.c_str(), src.c_str(),
92 srcOptions.c_str(), name.c_str());
mtkleinb37cb412015-03-18 05:27:14 -070093 {
94 SkAutoMutexAcquire lock(gRunningMutex);
95 for (int i = 0; i < gRunning.count(); i++) {
96 if (gRunning[i] == id) {
97 gRunning.removeShuffle(i);
98 break;
99 }
100 }
101 }
102 if (!FLAGS_verbose) {
103 note = "";
104 }
mtkleinb9eb4ac2015-02-02 18:26:03 -0800105 if (!log.isEmpty()) {
106 log.prepend("\n");
107 }
mtklein6dee2ad2015-02-05 09:53:44 -0800108 auto pending = sk_atomic_dec(&gPending)-1;
reed50bc0512015-05-19 14:13:31 -0700109 if (!FLAGS_quiet) {
mtklein711a2452015-07-08 08:49:20 -0700110 SkDebugf("%s(%4d/%-4dMB %6d) %s\t%s%s%s", FLAGS_verbose ? "\n" : kSkOverwriteLine
reed50bc0512015-05-19 14:13:31 -0700111 , sk_tools::getCurrResidentSetSizeMB()
112 , sk_tools::getMaxResidentSetSizeMB()
113 , pending
114 , HumanizeMs(ms).c_str()
115 , id.c_str()
116 , note.c_str()
117 , log.c_str());
118 }
mtkleina17241b2015-01-23 05:48:00 -0800119 // We write our dm.json file every once in a while in case we crash.
120 // Notice this also handles the final dm.json when pending == 0.
121 if (pending % 500 == 0) {
122 JsonWriter::DumpJson();
123 }
mtklein@google.comd36522d2013-10-16 13:02:15 +0000124}
125
djsollen54416de2015-04-03 07:24:48 -0700126static void start(ImplicitString config, ImplicitString src,
127 ImplicitString srcOptions, ImplicitString name) {
128 SkString id = SkStringPrintf("%s %s %s %s", config.c_str(), src.c_str(),
129 srcOptions.c_str(), name.c_str());
mtkleinb37cb412015-03-18 05:27:14 -0700130 SkAutoMutexAcquire lock(gRunningMutex);
131 gRunning.push_back(id);
132}
133
mtklein748ca3b2015-01-15 10:56:12 -0800134/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
mtklein114c3cd2015-01-15 10:15:02 -0800135
mtklein62bd1a62015-01-27 14:46:26 -0800136struct Gold : public SkString {
mtkleina82f5622015-02-20 12:30:19 -0800137 Gold() : SkString("") {}
djsollen54416de2015-04-03 07:24:48 -0700138 Gold(ImplicitString sink, ImplicitString src, ImplicitString srcOptions,
139 ImplicitString name, ImplicitString md5)
mtklein62bd1a62015-01-27 14:46:26 -0800140 : SkString("") {
141 this->append(sink);
142 this->append(src);
djsollen54416de2015-04-03 07:24:48 -0700143 this->append(srcOptions);
mtklein62bd1a62015-01-27 14:46:26 -0800144 this->append(name);
145 this->append(md5);
mtklein62bd1a62015-01-27 14:46:26 -0800146 }
mtklein02f46cf2015-03-20 13:48:42 -0700147 static uint32_t Hash(const Gold& g) { return SkGoodHash((const SkString&)g); }
mtklein62bd1a62015-01-27 14:46:26 -0800148};
mtkleina82f5622015-02-20 12:30:19 -0800149static SkTHashSet<Gold, Gold::Hash> gGold;
mtklein62bd1a62015-01-27 14:46:26 -0800150
151static void add_gold(JsonWriter::BitmapResult r) {
djsollen54416de2015-04-03 07:24:48 -0700152 gGold.add(Gold(r.config, r.sourceType, r.sourceOptions, r.name, r.md5));
mtklein62bd1a62015-01-27 14:46:26 -0800153}
154
155static void gather_gold() {
156 if (!FLAGS_readPath.isEmpty()) {
157 SkString path(FLAGS_readPath[0]);
158 path.append("/dm.json");
159 if (!JsonWriter::ReadJson(path.c_str(), add_gold)) {
160 fail(SkStringPrintf("Couldn't read %s for golden results.", path.c_str()));
161 }
162 }
163}
164
165/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
166
borenet09ed4802015-04-03 14:15:33 -0700167static SkTHashSet<SkString> gUninterestingHashes;
168
169static void gather_uninteresting_hashes() {
170 if (!FLAGS_uninterestingHashesFile.isEmpty()) {
171 SkAutoTUnref<SkData> data(SkData::NewFromFileName(FLAGS_uninterestingHashesFile[0]));
mtkleincc334b32015-09-22 11:43:53 -0700172 if (!data) {
173 SkDebugf("WARNING: unable to read uninteresting hashes from %s\n",
174 FLAGS_uninterestingHashesFile[0]);
175 return;
176 }
borenet09ed4802015-04-03 14:15:33 -0700177 SkTArray<SkString> hashes;
178 SkStrSplit((const char*)data->data(), "\n", &hashes);
179 for (const SkString& hash : hashes) {
180 gUninterestingHashes.add(hash);
181 }
182 }
183}
184
185/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
186
mtkleine0effd62015-07-29 06:37:28 -0700187struct TaggedSrc : public SkAutoTDelete<Src> {
msarett9e707a02015-09-01 14:57:57 -0700188 ImplicitString tag;
189 ImplicitString options;
mtkleine0effd62015-07-29 06:37:28 -0700190};
191
192struct TaggedSink : public SkAutoTDelete<Sink> {
193 const char* tag;
djsollen54416de2015-04-03 07:24:48 -0700194};
mtklein748ca3b2015-01-15 10:56:12 -0800195
196static const bool kMemcpyOK = true;
197
mtkleine0effd62015-07-29 06:37:28 -0700198static SkTArray<TaggedSrc, kMemcpyOK> gSrcs;
199static SkTArray<TaggedSink, kMemcpyOK> gSinks;
mtklein748ca3b2015-01-15 10:56:12 -0800200
mtklein6393c062015-04-27 08:45:01 -0700201static bool in_shard() {
202 static int N = 0;
203 return N++ % FLAGS_shards == FLAGS_shard;
204}
205
msarett9e707a02015-09-01 14:57:57 -0700206static void push_src(ImplicitString tag, ImplicitString options, Src* s) {
mtklein748ca3b2015-01-15 10:56:12 -0800207 SkAutoTDelete<Src> src(s);
mtklein6393c062015-04-27 08:45:01 -0700208 if (in_shard() &&
msarett9e707a02015-09-01 14:57:57 -0700209 FLAGS_src.contains(tag.c_str()) &&
mtklein748ca3b2015-01-15 10:56:12 -0800210 !SkCommandLineFlags::ShouldSkip(FLAGS_match, src->name().c_str())) {
mtkleine0effd62015-07-29 06:37:28 -0700211 TaggedSrc& s = gSrcs.push_back();
mtklein748ca3b2015-01-15 10:56:12 -0800212 s.reset(src.detach());
213 s.tag = tag;
djsollen54416de2015-04-03 07:24:48 -0700214 s.options = options;
mtklein114c3cd2015-01-15 10:15:02 -0800215 }
mtklein748ca3b2015-01-15 10:56:12 -0800216}
mtklein114c3cd2015-01-15 10:15:02 -0800217
msarett9e707a02015-09-01 14:57:57 -0700218static void push_codec_src(Path path, CodecSrc::Mode mode, CodecSrc::DstColorType dstColorType,
219 float scale) {
220 SkString folder;
221 switch (mode) {
222 case CodecSrc::kCodec_Mode:
223 folder.append("codec");
224 break;
225 case CodecSrc::kScaledCodec_Mode:
226 folder.append("scaled_codec");
227 break;
228 case CodecSrc::kScanline_Mode:
229 folder.append("scanline");
230 break;
231 case CodecSrc::kScanline_Subset_Mode:
232 folder.append("scanline_subset");
233 break;
234 case CodecSrc::kStripe_Mode:
235 folder.append("stripe");
236 break;
237 case CodecSrc::kSubset_Mode:
msarett36c37962015-09-02 13:20:52 -0700238 folder.append("codec_subset");
msarett9e707a02015-09-01 14:57:57 -0700239 break;
240 }
241
242 switch (dstColorType) {
243 case CodecSrc::kGrayscale_Always_DstColorType:
244 folder.append("_kGray8");
245 break;
246 case CodecSrc::kIndex8_Always_DstColorType:
247 folder.append("_kIndex8");
248 break;
249 default:
250 break;
251 }
252
253 if (1.0f != scale) {
254 folder.appendf("_%.3f", scale);
255 }
256
257 CodecSrc* src = new CodecSrc(path, mode, dstColorType, scale);
258 push_src("image", folder, src);
259}
260
msarett438b2ad2015-04-09 12:43:10 -0700261static void push_codec_srcs(Path path) {
262 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
263 if (!encoded) {
264 SkDebugf("Couldn't read %s.", path.c_str());
265 return;
266 }
267 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
halcanary96fcdcc2015-08-27 07:41:13 -0700268 if (nullptr == codec.get()) {
msarett438b2ad2015-04-09 12:43:10 -0700269 SkDebugf("Couldn't create codec for %s.", path.c_str());
270 return;
271 }
272
msarett36c37962015-09-02 13:20:52 -0700273 // Native Scales
msarett0a242972015-06-11 14:27:27 -0700274 // TODO (msarett): Implement scaling tests for SkImageDecoder in order to compare with these
275 // tests. SkImageDecoder supports downscales by integer factors.
emmaleer8f4ba762015-08-14 07:44:46 -0700276 // 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 -0700277 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 -0700278
msarett36c37962015-09-02 13:20:52 -0700279 const CodecSrc::Mode nativeModes[] = { CodecSrc::kCodec_Mode, CodecSrc::kScanline_Mode,
280 CodecSrc::kScanline_Subset_Mode, CodecSrc::kStripe_Mode, CodecSrc::kSubset_Mode };
msarett9e707a02015-09-01 14:57:57 -0700281
msarett36c37962015-09-02 13:20:52 -0700282 CodecSrc::DstColorType colorTypes[3];
283 uint32_t numColorTypes;
284 switch (codec->getInfo().colorType()) {
285 case kGray_8_SkColorType:
286 // FIXME: Is this a long term solution for testing wbmps decodes to kIndex8?
287 // Further discussion on this topic is at skbug.com/3683
288 colorTypes[0] = CodecSrc::kGetFromCanvas_DstColorType;
289 colorTypes[1] = CodecSrc::kGrayscale_Always_DstColorType;
290 colorTypes[2] = CodecSrc::kIndex8_Always_DstColorType;
291 numColorTypes = 3;
292 break;
293 case kIndex_8_SkColorType:
294 colorTypes[0] = CodecSrc::kGetFromCanvas_DstColorType;
295 colorTypes[1] = CodecSrc::kIndex8_Always_DstColorType;
296 numColorTypes = 2;
297 break;
298 default:
299 colorTypes[0] = CodecSrc::kGetFromCanvas_DstColorType;
300 numColorTypes = 1;
301 break;
302 }
msarett9e707a02015-09-01 14:57:57 -0700303
msarett36c37962015-09-02 13:20:52 -0700304 for (float scale : nativeScales) {
scroggo0df0e432015-08-07 12:53:25 -0700305 if (scale != 1.0f && (path.endsWith(".webp") || path.endsWith(".WEBP"))) {
306 // FIXME: skbug.com/4038 Scaling webp seems to leave some pixels uninitialized/
307 // compute their colors based on uninitialized values.
308 continue;
309 }
msarett0a242972015-06-11 14:27:27 -0700310
msarett36c37962015-09-02 13:20:52 -0700311 for (CodecSrc::Mode mode : nativeModes) {
312 for (uint32_t i = 0; i < numColorTypes; i++) {
313 push_codec_src(path, mode, colorTypes[i], scale);
msarett9e707a02015-09-01 14:57:57 -0700314 }
315 }
msarett0a242972015-06-11 14:27:27 -0700316 }
msarett36c37962015-09-02 13:20:52 -0700317
scroggo46c57472015-09-30 08:57:13 -0700318 if (path.endsWith(".ico") || path.endsWith(".ICO")) {
319 // FIXME: skbug.com/4404: ICO does not have the ability to decode scanlines, so we cannot
320 // use SkScaledCodec with it.
321 return;
322 }
323
msarett36c37962015-09-02 13:20:52 -0700324 // SkScaledCodec Scales
325 // The native scales are included to make sure that SkScaledCodec defaults to the native
326 // scaling strategy when possible.
327 // 0.1, 0.16, 0.2 etc allow us to test SkScaledCodec with sampleSize 10, 6, 5, etc.
328 // 0.4, 0.7 etc allow to test what happens when the client requests a scale that
329 // does not exactly match a sampleSize or native scaling capability.
msarett6c50a8f2015-09-18 11:24:44 -0700330 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 -0700331 0.6f, 0.625f, 0.750f, 0.8f, 0.875f, 1.0f };
332
333 for (float scale : samplingScales) {
334 if (scale != 1.0f && (path.endsWith(".webp") || path.endsWith(".WEBP"))) {
335 // FIXME: skbug.com/4038 Scaling webp seems to leave some pixels uninitialized/
336 // compute their colors based on uninitialized values.
337 continue;
338 }
339
340 for (uint32_t i = 0; i < numColorTypes; i++) {
341 push_codec_src(path, CodecSrc::kScaledCodec_Mode, colorTypes[i], scale);
342 }
343 }
msarett438b2ad2015-04-09 12:43:10 -0700344}
345
msaretta5783ae2015-09-08 15:35:32 -0700346static bool brd_color_type_supported(SkBitmapRegionDecoderInterface::Strategy strategy,
347 CodecSrc::DstColorType dstColorType) {
348 switch (strategy) {
349 case SkBitmapRegionDecoderInterface::kCanvas_Strategy:
350 if (CodecSrc::kGetFromCanvas_DstColorType == dstColorType) {
351 return true;
352 }
353 return false;
354 case SkBitmapRegionDecoderInterface::kOriginal_Strategy:
355 switch (dstColorType) {
356 case CodecSrc::kGetFromCanvas_DstColorType:
357 case CodecSrc::kIndex8_Always_DstColorType:
358 case CodecSrc::kGrayscale_Always_DstColorType:
359 return true;
360 default:
361 return false;
362 }
363 default:
364 SkASSERT(false);
365 return false;
366 }
367}
368
369static void push_brd_src(Path path, SkBitmapRegionDecoderInterface::Strategy strategy,
370 CodecSrc::DstColorType dstColorType, BRDSrc::Mode mode, uint32_t sampleSize) {
371 SkString folder;
372 switch (strategy) {
373 case SkBitmapRegionDecoderInterface::kCanvas_Strategy:
374 folder.append("brd_canvas");
375 break;
376 case SkBitmapRegionDecoderInterface::kOriginal_Strategy:
377 folder.append("brd_sample");
378 break;
379 default:
380 SkASSERT(false);
381 return;
382 }
383
384 switch (mode) {
385 case BRDSrc::kFullImage_Mode:
386 break;
387 case BRDSrc::kDivisor_Mode:
388 folder.append("_divisor");
389 break;
390 default:
391 SkASSERT(false);
392 return;
393 }
394
395 switch (dstColorType) {
396 case CodecSrc::kGetFromCanvas_DstColorType:
397 break;
398 case CodecSrc::kIndex8_Always_DstColorType:
399 folder.append("_kIndex");
400 break;
401 case CodecSrc::kGrayscale_Always_DstColorType:
402 folder.append("_kGray");
403 break;
404 default:
405 SkASSERT(false);
406 return;
407 }
408
409 if (1 != sampleSize) {
msarett7f691442015-09-22 11:56:16 -0700410 folder.appendf("_%.3f", get_scale_from_sample_size(sampleSize));
msaretta5783ae2015-09-08 15:35:32 -0700411 }
412
413 BRDSrc* src = new BRDSrc(path, strategy, mode, dstColorType, sampleSize);
414 push_src("image", folder, src);
415}
416
417static void push_brd_srcs(Path path) {
418
419 const SkBitmapRegionDecoderInterface::Strategy strategies[] = {
420 SkBitmapRegionDecoderInterface::kCanvas_Strategy,
421 SkBitmapRegionDecoderInterface::kOriginal_Strategy
422 };
423
msarett6efbe052015-09-11 09:01:16 -0700424 const uint32_t sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
425
msaretta5783ae2015-09-08 15:35:32 -0700426 // We will only test to one backend (8888), but we will test all of the
427 // color types that we need to decode to on this backend.
428 const CodecSrc::DstColorType dstColorTypes[] = {
429 CodecSrc::kGetFromCanvas_DstColorType,
430 CodecSrc::kIndex8_Always_DstColorType,
431 CodecSrc::kGrayscale_Always_DstColorType,
432 };
433
434 const BRDSrc::Mode modes[] = {
435 BRDSrc::kFullImage_Mode,
436 BRDSrc::kDivisor_Mode
437 };
438
msaretta5783ae2015-09-08 15:35:32 -0700439 for (SkBitmapRegionDecoderInterface::Strategy strategy : strategies) {
msarett6efbe052015-09-11 09:01:16 -0700440
msaretta5783ae2015-09-08 15:35:32 -0700441 // We disable png testing for kOriginal_Strategy because the implementation leaks
442 // memory in our forked libpng.
443 // TODO (msarett): Decide if we want to test pngs in this mode and how we might do this.
444 if (SkBitmapRegionDecoderInterface::kOriginal_Strategy == strategy &&
445 (path.endsWith(".png") || path.endsWith(".PNG"))) {
446 continue;
447 }
msarett6efbe052015-09-11 09:01:16 -0700448 for (uint32_t sampleSize : sampleSizes) {
449
450 // kOriginal_Strategy does not work for jpegs that are scaled to non-powers of two.
451 // We don't need to test this. We know it doesn't work, and it causes images with
452 // uninitialized memory to show up on Gold.
453 if (SkBitmapRegionDecoderInterface::kOriginal_Strategy == strategy &&
454 (path.endsWith(".jpg") || path.endsWith(".JPG") ||
455 path.endsWith(".jpeg") || path.endsWith(".JPEG")) && !SkIsPow2(sampleSize)) {
456 continue;
457 }
458 for (CodecSrc::DstColorType dstColorType : dstColorTypes) {
459 if (brd_color_type_supported(strategy, dstColorType)) {
460 for (BRDSrc::Mode mode : modes) {
msaretta5783ae2015-09-08 15:35:32 -0700461 push_brd_src(path, strategy, dstColorType, mode, sampleSize);
462 }
463 }
464 }
465 }
466 }
467}
468
469static bool brd_supported(const char* ext) {
msarett8c8f22a2015-04-01 06:58:48 -0700470 static const char* const exts[] = {
msaretta5783ae2015-09-08 15:35:32 -0700471 "jpg", "jpeg", "png", "webp",
472 "JPG", "JPEG", "PNG", "WEBP",
msarett8c8f22a2015-04-01 06:58:48 -0700473 };
474
475 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
476 if (0 == strcmp(exts[i], ext)) {
477 return true;
478 }
479 }
480 return false;
scroggo9b77ddd2015-03-19 06:03:39 -0700481}
482
mtklein748ca3b2015-01-15 10:56:12 -0800483static void gather_srcs() {
484 for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) {
djsollen54416de2015-04-03 07:24:48 -0700485 push_src("gm", "", new GMSrc(r->factory()));
mtklein748ca3b2015-01-15 10:56:12 -0800486 }
halcanaryfc37ad12015-01-30 07:31:19 -0800487 for (int i = 0; i < FLAGS_skps.count(); i++) {
488 const char* path = FLAGS_skps[i];
489 if (sk_isdir(path)) {
490 SkOSFile::Iter it(path, "skp");
491 for (SkString file; it.next(&file); ) {
djsollen54416de2015-04-03 07:24:48 -0700492 push_src("skp", "", new SKPSrc(SkOSPath::Join(path, file.c_str())));
halcanaryfc37ad12015-01-30 07:31:19 -0800493 }
494 } else {
djsollen54416de2015-04-03 07:24:48 -0700495 push_src("skp", "", new SKPSrc(path));
mtklein114c3cd2015-01-15 10:15:02 -0800496 }
497 }
halcanary23b03c32015-01-30 09:58:58 -0800498 static const char* const exts[] = {
499 "bmp", "gif", "jpg", "jpeg", "png", "webp", "ktx", "astc", "wbmp", "ico",
500 "BMP", "GIF", "JPG", "JPEG", "PNG", "WEBP", "KTX", "ASTC", "WBMP", "ICO",
501 };
502 for (int i = 0; i < FLAGS_images.count(); i++) {
503 const char* flag = FLAGS_images[i];
504 if (sk_isdir(flag)) {
505 for (size_t j = 0; j < SK_ARRAY_COUNT(exts); j++) {
506 SkOSFile::Iter it(flag, exts[j]);
507 for (SkString file; it.next(&file); ) {
508 SkString path = SkOSPath::Join(flag, file.c_str());
djsollen54416de2015-04-03 07:24:48 -0700509 push_src("image", "decode", new ImageSrc(path)); // Decode entire image
510 push_src("image", "subset", new ImageSrc(path, 2)); // Decode into 2x2 subsets
msaretta5783ae2015-09-08 15:35:32 -0700511 push_codec_srcs(path);
512 if (brd_supported(exts[j])) {
513 push_brd_srcs(path);
scroggo9b77ddd2015-03-19 06:03:39 -0700514 }
halcanary23b03c32015-01-30 09:58:58 -0800515 }
mtklein114c3cd2015-01-15 10:15:02 -0800516 }
halcanary23b03c32015-01-30 09:58:58 -0800517 } else if (sk_exists(flag)) {
518 // assume that FLAGS_images[i] is a valid image if it is a file.
djsollen54416de2015-04-03 07:24:48 -0700519 push_src("image", "decode", new ImageSrc(flag)); // Decode entire image.
520 push_src("image", "subset", new ImageSrc(flag, 2)); // Decode into 2 x 2 subsets
msarett438b2ad2015-04-09 12:43:10 -0700521 push_codec_srcs(flag);
msaretta5783ae2015-09-08 15:35:32 -0700522 push_brd_srcs(flag);
mtklein709d2c32015-01-15 08:30:25 -0800523 }
mtklein709d2c32015-01-15 08:30:25 -0800524 }
525}
526
mtklein748ca3b2015-01-15 10:56:12 -0800527static GrGLStandard get_gpu_api() {
528 if (FLAGS_gpuAPI.contains("gl")) { return kGL_GrGLStandard; }
529 if (FLAGS_gpuAPI.contains("gles")) { return kGLES_GrGLStandard; }
530 return kNone_GrGLStandard;
mtklein709d2c32015-01-15 08:30:25 -0800531}
532
mtklein748ca3b2015-01-15 10:56:12 -0800533static void push_sink(const char* tag, Sink* s) {
534 SkAutoTDelete<Sink> sink(s);
535 if (!FLAGS_config.contains(tag)) {
536 return;
mtklein114c3cd2015-01-15 10:15:02 -0800537 }
mtkleine0effd62015-07-29 06:37:28 -0700538 // Try a simple Src as a canary. If it fails, skip this sink.
mtklein748ca3b2015-01-15 10:56:12 -0800539 struct : public Src {
mtkleine0effd62015-07-29 06:37:28 -0700540 Error draw(SkCanvas* c) const override {
541 c->drawRect(SkRect::MakeWH(1,1), SkPaint());
542 return "";
543 }
mtklein36352bf2015-03-25 18:17:31 -0700544 SkISize size() const override { return SkISize::Make(16, 16); }
mtkleine0effd62015-07-29 06:37:28 -0700545 Name name() const override { return "justOneRect"; }
546 } justOneRect;
mtklein114c3cd2015-01-15 10:15:02 -0800547
mtklein748ca3b2015-01-15 10:56:12 -0800548 SkBitmap bitmap;
549 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800550 SkString log;
mtkleine0effd62015-07-29 06:37:28 -0700551 Error err = sink->draw(justOneRect, &bitmap, &stream, &log);
mtklein4089ef72015-03-05 08:40:28 -0800552 if (err.isFatal()) {
mtklein05641a52015-04-21 10:49:13 -0700553 SkDebugf("Could not run %s: %s\n", tag, err.c_str());
554 exit(1);
mtklein114c3cd2015-01-15 10:15:02 -0800555 }
556
mtkleine0effd62015-07-29 06:37:28 -0700557 TaggedSink& ts = gSinks.push_back();
mtklein748ca3b2015-01-15 10:56:12 -0800558 ts.reset(sink.detach());
559 ts.tag = tag;
560}
561
562static bool gpu_supported() {
563#if SK_SUPPORT_GPU
564 return FLAGS_gpu;
565#else
566 return false;
567#endif
568}
569
570static Sink* create_sink(const char* tag) {
571#define SINK(t, sink, ...) if (0 == strcmp(t, tag)) { return new sink(__VA_ARGS__); }
572 if (gpu_supported()) {
mtklein82d28432015-01-15 12:46:02 -0800573 typedef GrContextFactory Gr;
mtklein748ca3b2015-01-15 10:56:12 -0800574 const GrGLStandard api = get_gpu_api();
hendrikw885bf092015-08-27 10:38:39 -0700575 SINK("gpunull", GPUSink, Gr::kNull_GLContextType, api, 0, false, FLAGS_gpu_threading);
576 SINK("gpudebug", GPUSink, Gr::kDebug_GLContextType, api, 0, false, FLAGS_gpu_threading);
577 SINK("gpu", GPUSink, Gr::kNative_GLContextType, api, 0, false, FLAGS_gpu_threading);
578 SINK("gpudft", GPUSink, Gr::kNative_GLContextType, api, 0, true, FLAGS_gpu_threading);
579 SINK("msaa4", GPUSink, Gr::kNative_GLContextType, api, 4, false, FLAGS_gpu_threading);
580 SINK("msaa16", GPUSink, Gr::kNative_GLContextType, api, 16, false, FLAGS_gpu_threading);
bsalomon90e8ab72015-09-08 10:26:51 -0700581 SINK("nvprmsaa4", GPUSink, Gr::kNVPR_GLContextType, api, 4, true, FLAGS_gpu_threading);
582 SINK("nvprmsaa16", GPUSink, Gr::kNVPR_GLContextType, api, 16, true, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800583 #if SK_ANGLE
hendrikw885bf092015-08-27 10:38:39 -0700584 SINK("angle", GPUSink, Gr::kANGLE_GLContextType, api, 0, false, FLAGS_gpu_threading);
hendrikweddbefb2015-09-11 13:07:29 -0700585 SINK("angle-gl", GPUSink, Gr::kANGLE_GL_GLContextType, api, 0, false, FLAGS_gpu_threading);
hendrikw885bf092015-08-27 10:38:39 -0700586 #endif
587 #if SK_COMMAND_BUFFER
588 SINK("commandbuffer", GPUSink, Gr::kCommandBuffer_GLContextType, api, 0, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800589 #endif
590 #if SK_MESA
hendrikw885bf092015-08-27 10:38:39 -0700591 SINK("mesa", GPUSink, Gr::kMESA_GLContextType, api, 0, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800592 #endif
mtklein114c3cd2015-01-15 10:15:02 -0800593 }
mtklein748ca3b2015-01-15 10:56:12 -0800594
tomhudsoneebc39a2015-02-23 12:18:05 -0800595#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
596 SINK("hwui", HWUISink);
597#endif
598
mtklein748ca3b2015-01-15 10:56:12 -0800599 if (FLAGS_cpu) {
600 SINK("565", RasterSink, kRGB_565_SkColorType);
601 SINK("8888", RasterSink, kN32_SkColorType);
halcanaryc11c62f2015-09-28 11:51:54 -0700602 SINK("pdf", PDFSink, "Pdfium");
603 SINK("pdf_poppler", PDFSink, "Poppler");
mtklein9c3f17d2015-01-28 11:35:18 -0800604 SINK("skp", SKPSink);
mtklein8a4527e2015-01-31 20:00:58 -0800605 SINK("svg", SVGSink);
606 SINK("null", NullSink);
halcanary47ef4d52015-03-03 09:13:09 -0800607 SINK("xps", XPSSink);
mtklein748ca3b2015-01-15 10:56:12 -0800608 }
609#undef SINK
halcanary96fcdcc2015-08-27 07:41:13 -0700610 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800611}
612
mtklein748ca3b2015-01-15 10:56:12 -0800613static Sink* create_via(const char* tag, Sink* wrapped) {
614#define VIA(t, via, ...) if (0 == strcmp(t, tag)) { return new via(__VA_ARGS__); }
halcanary96fcdcc2015-08-27 07:41:13 -0700615 VIA("twice", ViaTwice, wrapped);
616 VIA("pipe", ViaPipe, wrapped);
617 VIA("serialize", ViaSerialization, wrapped);
618 VIA("2ndpic", ViaSecondPicture, wrapped);
mtkleind31c13d2015-05-05 12:59:56 -0700619 VIA("sp", ViaSingletonPictures, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700620 VIA("tiles", ViaTiles, 256, 256, nullptr, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800621 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800622
mtkleind603b222015-02-17 11:13:33 -0800623 if (FLAGS_matrix.count() == 4) {
mtklein748ca3b2015-01-15 10:56:12 -0800624 SkMatrix m;
mtkleind603b222015-02-17 11:13:33 -0800625 m.reset();
626 m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
627 m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
628 m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
629 m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
630 VIA("matrix", ViaMatrix, m, wrapped);
631 VIA("upright", ViaUpright, m, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800632 }
tomhudson64de1e12015-03-05 08:01:07 -0800633
634#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
635 VIA("androidsdk", ViaAndroidSDK, wrapped);
636#endif
637
mtklein748ca3b2015-01-15 10:56:12 -0800638#undef VIA
halcanary96fcdcc2015-08-27 07:41:13 -0700639 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800640}
641
mtklein748ca3b2015-01-15 10:56:12 -0800642static void gather_sinks() {
643 for (int i = 0; i < FLAGS_config.count(); i++) {
644 const char* config = FLAGS_config[i];
645 SkTArray<SkString> parts;
646 SkStrSplit(config, "-", &parts);
647
halcanary96fcdcc2015-08-27 07:41:13 -0700648 Sink* sink = nullptr;
mtklein748ca3b2015-01-15 10:56:12 -0800649 for (int i = parts.count(); i-- > 0;) {
650 const char* part = parts[i].c_str();
halcanary96fcdcc2015-08-27 07:41:13 -0700651 Sink* next = (sink == nullptr) ? create_sink(part) : create_via(part, sink);
652 if (next == nullptr) {
mtklein748ca3b2015-01-15 10:56:12 -0800653 SkDebugf("Skipping %s: Don't understand '%s'.\n", config, part);
654 delete sink;
halcanary96fcdcc2015-08-27 07:41:13 -0700655 sink = nullptr;
mtklein748ca3b2015-01-15 10:56:12 -0800656 break;
657 }
658 sink = next;
659 }
660 if (sink) {
661 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800662 }
663 }
664}
mtklein709d2c32015-01-15 08:30:25 -0800665
mtkleina5114d72015-08-24 13:27:01 -0700666static bool dump_png(SkBitmap bitmap, const char* path, const char* md5) {
667 const int w = bitmap.width(),
668 h = bitmap.height();
669
670 // First get the bitmap into N32 color format. The next step will work only there.
671 if (bitmap.colorType() != kN32_SkColorType) {
672 SkBitmap n32;
673 if (!bitmap.copyTo(&n32, kN32_SkColorType)) {
674 return false;
675 }
676 bitmap = n32;
677 }
678
679 // Convert our N32 bitmap into unpremul RGBA for libpng.
680 SkAutoTMalloc<uint32_t> rgba(w*h);
681 if (!bitmap.readPixels(SkImageInfo::Make(w,h, kRGBA_8888_SkColorType, kUnpremul_SkAlphaType),
682 rgba, 4*w, 0,0)) {
683 return false;
684 }
685
686 // We don't need bitmap anymore. Might as well drop our ref.
mtkleinfccb77d2015-08-24 14:13:29 -0700687 bitmap.reset();
mtkleina5114d72015-08-24 13:27:01 -0700688
mtkleinc64137c2015-08-25 10:56:08 -0700689 FILE* f = fopen(path, "wb");
mtkleina5114d72015-08-24 13:27:01 -0700690 if (!f) { return false; }
691
692 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
693 if (!png) {
694 fclose(f);
695 return false;
696 }
697
698 png_infop info = png_create_info_struct(png);
699 if (!info) {
700 png_destroy_write_struct(&png, &info);
701 fclose(f);
702 return false;
703 }
704
mtkleind69ece62015-09-10 10:37:44 -0700705 SkString description;
706 description.append("Key: ");
707 for (int i = 0; i < FLAGS_key.count(); i++) {
708 description.appendf("%s ", FLAGS_key[i]);
709 }
710 description.append("Properties: ");
711 for (int i = 0; i < FLAGS_properties.count(); i++) {
712 description.appendf("%s ", FLAGS_properties[i]);
713 }
714 description.appendf("MD5: %s", md5);
715
mtkleina5114d72015-08-24 13:27:01 -0700716 png_text text[2];
717 text[0].key = (png_charp)"Author";
718 text[0].text = (png_charp)"DM dump_png()";
719 text[0].compression = PNG_TEXT_COMPRESSION_NONE;
720 text[1].key = (png_charp)"Description";
mtkleind69ece62015-09-10 10:37:44 -0700721 text[1].text = (png_charp)description.c_str();
mtkleina5114d72015-08-24 13:27:01 -0700722 text[1].compression = PNG_TEXT_COMPRESSION_NONE;
723 png_set_text(png, info, text, 2);
724
725 png_init_io(png, f);
726 png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8,
727 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
728 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
729 png_write_info(png, info);
730 for (int j = 0; j < h; j++) {
731 png_bytep row = (png_bytep)(rgba.get() + w*j);
732 png_write_rows(png, &row, 1);
733 }
734 png_write_end(png, info);
735
736 png_destroy_write_struct(&png, &info);
737 fclose(f);
738 return true;
739}
740
mtkleina2ef6422015-01-15 13:44:22 -0800741static bool match(const char* needle, const char* haystack) {
halcanary96fcdcc2015-08-27 07:41:13 -0700742 return 0 == strcmp("_", needle) || nullptr != strstr(haystack, needle);
mtkleina2ef6422015-01-15 13:44:22 -0800743}
744
djsollen54416de2015-04-03 07:24:48 -0700745static ImplicitString is_blacklisted(const char* sink, const char* src,
746 const char* srcOptions, const char* name) {
mtklein0d243ff2015-04-03 07:51:00 -0700747 for (int i = 0; i < FLAGS_blacklist.count() - 3; i += 4) {
mtkleina2ef6422015-01-15 13:44:22 -0800748 if (match(FLAGS_blacklist[i+0], sink) &&
djsollen54416de2015-04-03 07:24:48 -0700749 match(FLAGS_blacklist[i+1], src) &&
750 match(FLAGS_blacklist[i+2], srcOptions) &&
751 match(FLAGS_blacklist[i+3], name)) {
752 return SkStringPrintf("%s %s %s %s",
753 FLAGS_blacklist[i+0], FLAGS_blacklist[i+1],
754 FLAGS_blacklist[i+2], FLAGS_blacklist[i+3]);
mtkleina2ef6422015-01-15 13:44:22 -0800755 }
756 }
757 return "";
758}
759
mtklein748ca3b2015-01-15 10:56:12 -0800760// The finest-grained unit of work we can run: draw a single Src into a single Sink,
761// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
762struct Task {
mtkleine0effd62015-07-29 06:37:28 -0700763 Task(const TaggedSrc& src, const TaggedSink& sink) : src(src), sink(sink) {}
764 const TaggedSrc& src;
765 const TaggedSink& sink;
mtklein748ca3b2015-01-15 10:56:12 -0800766
767 static void Run(Task* task) {
mtkleina2ef6422015-01-15 13:44:22 -0800768 SkString name = task->src->name();
mtkleine0effd62015-07-29 06:37:28 -0700769
770 // We'll skip drawing this Src/Sink pair if:
771 // - the Src vetoes the Sink;
772 // - this Src / Sink combination is on the blacklist;
773 // - it's a dry run.
mtklein99cab4e2015-07-31 06:43:04 -0700774 SkString note(task->src->veto(task->sink->flags()) ? " (veto)" : "");
msarett9e707a02015-09-01 14:57:57 -0700775 SkString whyBlacklisted = is_blacklisted(task->sink.tag, task->src.tag.c_str(),
776 task->src.options.c_str(), name.c_str());
mtkleinb37cb412015-03-18 05:27:14 -0700777 if (!whyBlacklisted.isEmpty()) {
778 note.appendf(" (--blacklist %s)", whyBlacklisted.c_str());
779 }
mtkleine0effd62015-07-29 06:37:28 -0700780
mtkleinb9eb4ac2015-02-02 18:26:03 -0800781 SkString log;
mtklein748ca3b2015-01-15 10:56:12 -0800782 WallTimer timer;
783 timer.start();
mtkleine0effd62015-07-29 06:37:28 -0700784 if (!FLAGS_dryRun && note.isEmpty()) {
mtklein748ca3b2015-01-15 10:56:12 -0800785 SkBitmap bitmap;
786 SkDynamicMemoryWStream stream;
bsalomon821e10e2015-06-04 14:15:33 -0700787 if (FLAGS_pre_log) {
788 SkDebugf("\nRunning %s->%s", name.c_str(), task->sink.tag);
789 }
djsollen54416de2015-04-03 07:24:48 -0700790 start(task->sink.tag, task->src.tag, task->src.options, name.c_str());
mtkleinb9eb4ac2015-02-02 18:26:03 -0800791 Error err = task->sink->draw(*task->src, &bitmap, &stream, &log);
mtklein748ca3b2015-01-15 10:56:12 -0800792 if (!err.isEmpty()) {
mtklein4089ef72015-03-05 08:40:28 -0800793 timer.end();
794 if (err.isFatal()) {
djsollen54416de2015-04-03 07:24:48 -0700795 fail(SkStringPrintf("%s %s %s %s: %s",
mtklein4089ef72015-03-05 08:40:28 -0800796 task->sink.tag,
msarett9e707a02015-09-01 14:57:57 -0700797 task->src.tag.c_str(),
798 task->src.options.c_str(),
mtklein4089ef72015-03-05 08:40:28 -0800799 name.c_str(),
800 err.c_str()));
mtkleinb37cb412015-03-18 05:27:14 -0700801 } else {
802 note.appendf(" (skipped: %s)", err.c_str());
mtklein4089ef72015-03-05 08:40:28 -0800803 }
djsollen54416de2015-04-03 07:24:48 -0700804 done(timer.fWall, task->sink.tag, task->src.tag, task->src.options,
805 name, note, log);
mtklein4089ef72015-03-05 08:40:28 -0800806 return;
mtklein748ca3b2015-01-15 10:56:12 -0800807 }
mtklein62bd1a62015-01-27 14:46:26 -0800808 SkAutoTDelete<SkStreamAsset> data(stream.detachAsStream());
809
810 SkString md5;
811 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
812 SkMD5 hash;
813 if (data->getLength()) {
814 hash.writeStream(data, data->getLength());
815 data->rewind();
816 } else {
mtklein38408462015-07-08 07:25:27 -0700817 // If we're BGRA (Linux, Windows), swizzle over to RGBA (Mac, Android).
818 // This helps eliminate multiple 0-pixel-diff hashes on gold.skia.org.
819 // (Android's general slow speed breaks the tie arbitrarily in RGBA's favor.)
820 // We might consider promoting 565 to RGBA too.
821 if (bitmap.colorType() == kBGRA_8888_SkColorType) {
822 SkBitmap swizzle;
823 SkAssertResult(bitmap.copyTo(&swizzle, kRGBA_8888_SkColorType));
824 hash.write(swizzle.getPixels(), swizzle.getSize());
825 } else {
826 hash.write(bitmap.getPixels(), bitmap.getSize());
827 }
mtklein62bd1a62015-01-27 14:46:26 -0800828 }
829 SkMD5::Digest digest;
830 hash.finish(digest);
831 for (int i = 0; i < 16; i++) {
832 md5.appendf("%02x", digest.data[i]);
833 }
834 }
835
836 if (!FLAGS_readPath.isEmpty() &&
msarett9e707a02015-09-01 14:57:57 -0700837 !gGold.contains(Gold(task->sink.tag, task->src.tag.c_str(),
838 task->src.options.c_str(), name, md5))) {
djsollen54416de2015-04-03 07:24:48 -0700839 fail(SkStringPrintf("%s not found for %s %s %s %s in %s",
mtklein62bd1a62015-01-27 14:46:26 -0800840 md5.c_str(),
841 task->sink.tag,
msarett9e707a02015-09-01 14:57:57 -0700842 task->src.tag.c_str(),
843 task->src.options.c_str(),
mtklein62bd1a62015-01-27 14:46:26 -0800844 name.c_str(),
845 FLAGS_readPath[0]));
846 }
847
mtkleinb0531a72015-04-07 13:38:48 -0700848 if (!FLAGS_writePath.isEmpty()) {
mtklein748ca3b2015-01-15 10:56:12 -0800849 const char* ext = task->sink->fileExtension();
mtklein62bd1a62015-01-27 14:46:26 -0800850 if (data->getLength()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700851 WriteToDisk(*task, md5, ext, data, data->getLength(), nullptr);
halcanary022afb82015-01-30 11:00:12 -0800852 SkASSERT(bitmap.drawsNothing());
853 } else if (!bitmap.drawsNothing()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700854 WriteToDisk(*task, md5, ext, nullptr, 0, &bitmap);
mtklein748ca3b2015-01-15 10:56:12 -0800855 }
856 }
857 }
858 timer.end();
msarett9e707a02015-09-01 14:57:57 -0700859 done(timer.fWall, task->sink.tag, task->src.tag.c_str(), task->src.options.c_str(), name,
860 note, log);
mtklein748ca3b2015-01-15 10:56:12 -0800861 }
862
863 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -0800864 SkString md5,
865 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -0800866 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -0800867 const SkBitmap* bitmap) {
mtklein748ca3b2015-01-15 10:56:12 -0800868 JsonWriter::BitmapResult result;
djsollen54416de2015-04-03 07:24:48 -0700869 result.name = task.src->name();
870 result.config = task.sink.tag;
871 result.sourceType = task.src.tag;
872 result.sourceOptions = task.src.options;
873 result.ext = ext;
874 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -0800875 JsonWriter::AddBitmapResult(result);
876
mtkleinb0531a72015-04-07 13:38:48 -0700877 // If an MD5 is uninteresting, we want it noted in the JSON file,
878 // but don't want to dump it out as a .png (or whatever ext is).
879 if (gUninterestingHashes.contains(md5)) {
880 return;
881 }
882
mtklein748ca3b2015-01-15 10:56:12 -0800883 const char* dir = FLAGS_writePath[0];
884 if (0 == strcmp(dir, "@")) { // Needed for iOS.
885 dir = FLAGS_resourcePath[0];
886 }
887 sk_mkdir(dir);
888
889 SkString path;
890 if (FLAGS_nameByHash) {
891 path = SkOSPath::Join(dir, result.md5.c_str());
892 path.append(".");
893 path.append(ext);
894 if (sk_exists(path.c_str())) {
895 return; // Content-addressed. If it exists already, we're done.
896 }
897 } else {
898 path = SkOSPath::Join(dir, task.sink.tag);
899 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -0700900 path = SkOSPath::Join(path.c_str(), task.src.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800901 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -0700902 if (strcmp(task.src.options.c_str(), "") != 0) {
903 path = SkOSPath::Join(path.c_str(), task.src.options.c_str());
djsollen54416de2015-04-03 07:24:48 -0700904 sk_mkdir(path.c_str());
905 }
mtklein748ca3b2015-01-15 10:56:12 -0800906 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
907 path.append(".");
908 path.append(ext);
909 }
910
mtklein748ca3b2015-01-15 10:56:12 -0800911 if (bitmap) {
mtkleina5114d72015-08-24 13:27:01 -0700912 if (!dump_png(*bitmap, path.c_str(), result.md5.c_str())) {
mtklein748ca3b2015-01-15 10:56:12 -0800913 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
914 return;
915 }
916 } else {
mtkleina5114d72015-08-24 13:27:01 -0700917 SkFILEWStream file(path.c_str());
918 if (!file.isValid()) {
919 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
920 return;
921 }
mtklein748ca3b2015-01-15 10:56:12 -0800922 if (!file.writeStream(data, len)) {
923 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
924 return;
925 }
926 }
927 }
928};
929
930// Run all tasks in the same enclave serially on the same thread.
931// They can't possibly run concurrently with each other.
932static void run_enclave(SkTArray<Task>* tasks) {
933 for (int i = 0; i < tasks->count(); i++) {
934 Task::Run(tasks->begin() + i);
935 }
936}
937
938/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
939
940// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
941
mtklein55e88b22015-01-21 15:50:13 -0800942static SkTDArray<skiatest::Test> gThreadedTests, gGPUTests;
mtklein748ca3b2015-01-15 10:56:12 -0800943
944static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -0800945 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -0800946 return;
947 }
mtklein6393c062015-04-27 08:45:01 -0700948 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r; r = r->next()) {
949 if (!in_shard()) {
950 continue;
951 }
halcanary87f3ba42015-01-20 09:30:20 -0800952 // Despite its name, factory() is returning a reference to
953 // link-time static const POD data.
954 const skiatest::Test& test = r->factory();
955 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -0800956 continue;
957 }
halcanary87f3ba42015-01-20 09:30:20 -0800958 if (test.needsGpu && gpu_supported()) {
mtklein55e88b22015-01-21 15:50:13 -0800959 (FLAGS_gpu_threading ? gThreadedTests : gGPUTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -0800960 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein55e88b22015-01-21 15:50:13 -0800961 gThreadedTests.push(test);
mtklein82d28432015-01-15 12:46:02 -0800962 }
mtklein748ca3b2015-01-15 10:56:12 -0800963 }
964}
965
halcanary87f3ba42015-01-20 09:30:20 -0800966static void run_test(skiatest::Test* test) {
967 struct : public skiatest::Reporter {
mtklein36352bf2015-03-25 18:17:31 -0700968 void reportFailed(const skiatest::Failure& failure) override {
halcanary87f3ba42015-01-20 09:30:20 -0800969 fail(failure.toString());
970 JsonWriter::AddTestFailure(failure);
971 }
mtklein36352bf2015-03-25 18:17:31 -0700972 bool allowExtendedTest() const override {
halcanary87f3ba42015-01-20 09:30:20 -0800973 return FLAGS_pathOpsExtended;
974 }
mtklein36352bf2015-03-25 18:17:31 -0700975 bool verbose() const override { return FLAGS_veryVerbose; }
halcanary87f3ba42015-01-20 09:30:20 -0800976 } reporter;
djsollen824996a2015-06-12 12:06:22 -0700977
978 SkString note;
979 SkString whyBlacklisted = is_blacklisted("_", "tests", "_", test->name);
980 if (!whyBlacklisted.isEmpty()) {
981 note.appendf(" (--blacklist %s)", whyBlacklisted.c_str());
982 }
983
mtklein748ca3b2015-01-15 10:56:12 -0800984 WallTimer timer;
985 timer.start();
djsollen824996a2015-06-12 12:06:22 -0700986 if (!FLAGS_dryRun && whyBlacklisted.isEmpty()) {
djsollen54416de2015-04-03 07:24:48 -0700987 start("unit", "test", "", test->name);
mtklein55e88b22015-01-21 15:50:13 -0800988 GrContextFactory factory;
bsalomon821e10e2015-06-04 14:15:33 -0700989 if (FLAGS_pre_log) {
990 SkDebugf("\nRunning test %s", test->name);
991 }
mtklein55e88b22015-01-21 15:50:13 -0800992 test->proc(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -0800993 }
994 timer.end();
djsollen824996a2015-06-12 12:06:22 -0700995 done(timer.fWall, "unit", "test", "", test->name, note, "");
mtklein748ca3b2015-01-15 10:56:12 -0800996}
997
998/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
999
mtklein55e88b22015-01-21 15:50:13 -08001000// If we're isolating all GPU-bound work to one thread (the default), this function runs all that.
1001static void run_enclave_and_gpu_tests(SkTArray<Task>* tasks) {
1002 run_enclave(tasks);
1003 for (int i = 0; i < gGPUTests.count(); i++) {
1004 run_test(&gGPUTests[i]);
1005 }
1006}
1007
mtkleinde6fc2b2015-03-12 06:28:54 -07001008// Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung.
1009// This prints something every once in a while so that it knows we're still working.
mtklein2e1c47e2015-03-12 07:16:56 -07001010static void start_keepalive() {
1011 struct Loop {
1012 static void forever(void*) {
1013 for (;;) {
1014 static const int kSec = 300;
1015 #if defined(SK_BUILD_FOR_WIN)
1016 Sleep(kSec * 1000);
1017 #else
1018 sleep(kSec);
1019 #endif
mtkleinb37cb412015-03-18 05:27:14 -07001020 SkString running;
1021 {
1022 SkAutoMutexAcquire lock(gRunningMutex);
1023 for (int i = 0; i < gRunning.count(); i++) {
1024 running.appendf("\n\t%s", gRunning[i].c_str());
1025 }
1026 }
1027 SkDebugf("\nCurrently running:%s\n", running.c_str());
mtklein2e1c47e2015-03-12 07:16:56 -07001028 }
1029 }
1030 };
1031 static SkThread* intentionallyLeaked = new SkThread(Loop::forever);
1032 intentionallyLeaked->start();
mtkleinde6fc2b2015-03-12 06:28:54 -07001033}
1034
caryclark83ca6282015-06-10 09:31:09 -07001035#define PORTABLE_FONT_PREFIX "Toy Liberation "
1036
1037static SkTypeface* create_from_name(const char familyName[], SkTypeface::Style style) {
1038 if (familyName && strlen(familyName) > sizeof(PORTABLE_FONT_PREFIX)
1039 && !strncmp(familyName, PORTABLE_FONT_PREFIX, sizeof(PORTABLE_FONT_PREFIX) - 1)) {
caryclark1818acb2015-07-24 12:09:25 -07001040 return sk_tool_utils::create_portable_typeface(familyName, style);
caryclark83ca6282015-06-10 09:31:09 -07001041 }
halcanary96fcdcc2015-08-27 07:41:13 -07001042 return nullptr;
caryclark83ca6282015-06-10 09:31:09 -07001043}
1044
1045#undef PORTABLE_FONT_PREFIX
1046
1047extern SkTypeface* (*gCreateTypefaceDelegate)(const char [], SkTypeface::Style );
1048
jcgregorio3b27ade2014-11-13 08:06:40 -08001049int dm_main();
caryclark17f0b6d2014-07-22 10:15:34 -07001050int dm_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -08001051 SetupCrashHandler();
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +00001052 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -07001053 SkTaskGroup::Enabler enabled(FLAGS_threads);
caryclark83ca6282015-06-10 09:31:09 -07001054 gCreateTypefaceDelegate = &create_from_name;
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +00001055
mtklein2e1c47e2015-03-12 07:16:56 -07001056 start_keepalive();
mtkleinde6fc2b2015-03-12 06:28:54 -07001057
mtklein62bd1a62015-01-27 14:46:26 -08001058 gather_gold();
borenet09ed4802015-04-03 14:15:33 -07001059 gather_uninteresting_hashes();
mtklein62bd1a62015-01-27 14:46:26 -08001060
mtklein748ca3b2015-01-15 10:56:12 -08001061 gather_srcs();
1062 gather_sinks();
1063 gather_tests();
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +00001064
mtklein55e88b22015-01-21 15:50:13 -08001065 gPending = gSrcs.count() * gSinks.count() + gThreadedTests.count() + gGPUTests.count();
mtklein748ca3b2015-01-15 10:56:12 -08001066 SkDebugf("%d srcs * %d sinks + %d tests == %d tasks\n",
mtklein55e88b22015-01-21 15:50:13 -08001067 gSrcs.count(), gSinks.count(), gThreadedTests.count() + gGPUTests.count(), gPending);
mtklein748ca3b2015-01-15 10:56:12 -08001068
1069 // We try to exploit as much parallelism as is safe. Most Src/Sink pairs run on any thread,
1070 // but Sinks that identify as part of a particular enclave run serially on a single thread.
mtklein82d28432015-01-15 12:46:02 -08001071 // CPU tests run on any thread. GPU tests depend on --gpu_threading.
mtklein748ca3b2015-01-15 10:56:12 -08001072 SkTArray<Task> enclaves[kNumEnclaves];
1073 for (int j = 0; j < gSinks.count(); j++) {
1074 SkTArray<Task>& tasks = enclaves[gSinks[j]->enclave()];
1075 for (int i = 0; i < gSrcs.count(); i++) {
1076 tasks.push_back(Task(gSrcs[i], gSinks[j]));
1077 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001078 }
1079
mtklein748ca3b2015-01-15 10:56:12 -08001080 SkTaskGroup tg;
mtklein55e88b22015-01-21 15:50:13 -08001081 tg.batch(run_test, gThreadedTests.begin(), gThreadedTests.count());
1082 for (int i = 0; i < kNumEnclaves; i++) {
1083 switch(i) {
1084 case kAnyThread_Enclave:
1085 tg.batch(Task::Run, enclaves[i].begin(), enclaves[i].count());
1086 break;
1087 case kGPU_Enclave:
1088 tg.add(run_enclave_and_gpu_tests, &enclaves[i]);
1089 break;
1090 default:
1091 tg.add(run_enclave, &enclaves[i]);
1092 break;
mtklein82d28432015-01-15 12:46:02 -08001093 }
mtklein55e88b22015-01-21 15:50:13 -08001094 }
mtklein748ca3b2015-01-15 10:56:12 -08001095 tg.wait();
mtklein748ca3b2015-01-15 10:56:12 -08001096 // At this point we're back in single-threaded land.
caryclarkf53ce802015-06-15 06:48:30 -07001097 sk_tool_utils::release_portable_typefaces();
mtklein@google.comd36522d2013-10-16 13:02:15 +00001098
mtklein2f64eec2015-01-15 14:20:41 -08001099 SkDebugf("\n");
mtklein748ca3b2015-01-15 10:56:12 -08001100 if (gFailures.count() > 0) {
1101 SkDebugf("Failures:\n");
1102 for (int i = 0; i < gFailures.count(); i++) {
mtklein9dc09102015-01-15 15:47:33 -08001103 SkDebugf("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001104 }
1105 SkDebugf("%d failures\n", gFailures.count());
1106 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001107 }
mtklein748ca3b2015-01-15 10:56:12 -08001108 if (gPending > 0) {
1109 SkDebugf("Hrm, we didn't seem to run everything we intended to! Please file a bug.\n");
1110 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001111 }
halcanary7a14b312015-10-01 07:28:13 -07001112 #ifdef SK_PDF_IMAGE_STATS
1113 SkPDFImageDumpStats();
1114 #endif // SK_PDF_IMAGE_STATS
mtklein748ca3b2015-01-15 10:56:12 -08001115 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +00001116}
jcgregorio3b27ade2014-11-13 08:06:40 -08001117
borenet48087572015-04-02 12:16:36 -07001118#if !defined(SK_BUILD_FOR_IOS)
jcgregorio3b27ade2014-11-13 08:06:40 -08001119int main(int argc, char** argv) {
1120 SkCommandLineFlags::Parse(argc, argv);
1121 return dm_main();
1122}
1123#endif