blob: e616c187047350bd673a243116ce1aab5a8822e1 [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) {
msarett36c37962015-09-02 13:20:52 -0700305 for (CodecSrc::Mode mode : nativeModes) {
306 for (uint32_t i = 0; i < numColorTypes; i++) {
307 push_codec_src(path, mode, colorTypes[i], scale);
msarett9e707a02015-09-01 14:57:57 -0700308 }
309 }
msarett0a242972015-06-11 14:27:27 -0700310 }
msarett36c37962015-09-02 13:20:52 -0700311
scroggo46c57472015-09-30 08:57:13 -0700312 if (path.endsWith(".ico") || path.endsWith(".ICO")) {
313 // FIXME: skbug.com/4404: ICO does not have the ability to decode scanlines, so we cannot
314 // use SkScaledCodec with it.
315 return;
316 }
317
msarett36c37962015-09-02 13:20:52 -0700318 // SkScaledCodec Scales
319 // The native scales are included to make sure that SkScaledCodec defaults to the native
320 // scaling strategy when possible.
321 // 0.1, 0.16, 0.2 etc allow us to test SkScaledCodec with sampleSize 10, 6, 5, etc.
322 // 0.4, 0.7 etc allow to test what happens when the client requests a scale that
323 // does not exactly match a sampleSize or native scaling capability.
msarett6c50a8f2015-09-18 11:24:44 -0700324 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 -0700325 0.6f, 0.625f, 0.750f, 0.8f, 0.875f, 1.0f };
326
327 for (float scale : samplingScales) {
328 if (scale != 1.0f && (path.endsWith(".webp") || path.endsWith(".WEBP"))) {
329 // FIXME: skbug.com/4038 Scaling webp seems to leave some pixels uninitialized/
330 // compute their colors based on uninitialized values.
331 continue;
332 }
333
334 for (uint32_t i = 0; i < numColorTypes; i++) {
335 push_codec_src(path, CodecSrc::kScaledCodec_Mode, colorTypes[i], scale);
336 }
337 }
msarett438b2ad2015-04-09 12:43:10 -0700338}
339
msaretta5783ae2015-09-08 15:35:32 -0700340static bool brd_color_type_supported(SkBitmapRegionDecoderInterface::Strategy strategy,
341 CodecSrc::DstColorType dstColorType) {
342 switch (strategy) {
343 case SkBitmapRegionDecoderInterface::kCanvas_Strategy:
344 if (CodecSrc::kGetFromCanvas_DstColorType == dstColorType) {
345 return true;
346 }
347 return false;
348 case SkBitmapRegionDecoderInterface::kOriginal_Strategy:
349 switch (dstColorType) {
350 case CodecSrc::kGetFromCanvas_DstColorType:
351 case CodecSrc::kIndex8_Always_DstColorType:
352 case CodecSrc::kGrayscale_Always_DstColorType:
353 return true;
354 default:
355 return false;
356 }
357 default:
358 SkASSERT(false);
359 return false;
360 }
361}
362
363static void push_brd_src(Path path, SkBitmapRegionDecoderInterface::Strategy strategy,
364 CodecSrc::DstColorType dstColorType, BRDSrc::Mode mode, uint32_t sampleSize) {
365 SkString folder;
366 switch (strategy) {
367 case SkBitmapRegionDecoderInterface::kCanvas_Strategy:
368 folder.append("brd_canvas");
369 break;
370 case SkBitmapRegionDecoderInterface::kOriginal_Strategy:
371 folder.append("brd_sample");
372 break;
373 default:
374 SkASSERT(false);
375 return;
376 }
377
378 switch (mode) {
379 case BRDSrc::kFullImage_Mode:
380 break;
381 case BRDSrc::kDivisor_Mode:
382 folder.append("_divisor");
383 break;
384 default:
385 SkASSERT(false);
386 return;
387 }
388
389 switch (dstColorType) {
390 case CodecSrc::kGetFromCanvas_DstColorType:
391 break;
392 case CodecSrc::kIndex8_Always_DstColorType:
393 folder.append("_kIndex");
394 break;
395 case CodecSrc::kGrayscale_Always_DstColorType:
396 folder.append("_kGray");
397 break;
398 default:
399 SkASSERT(false);
400 return;
401 }
402
403 if (1 != sampleSize) {
msarett7f691442015-09-22 11:56:16 -0700404 folder.appendf("_%.3f", get_scale_from_sample_size(sampleSize));
msaretta5783ae2015-09-08 15:35:32 -0700405 }
406
407 BRDSrc* src = new BRDSrc(path, strategy, mode, dstColorType, sampleSize);
408 push_src("image", folder, src);
409}
410
411static void push_brd_srcs(Path path) {
412
413 const SkBitmapRegionDecoderInterface::Strategy strategies[] = {
414 SkBitmapRegionDecoderInterface::kCanvas_Strategy,
415 SkBitmapRegionDecoderInterface::kOriginal_Strategy
416 };
417
msarett6efbe052015-09-11 09:01:16 -0700418 const uint32_t sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
419
msaretta5783ae2015-09-08 15:35:32 -0700420 // We will only test to one backend (8888), but we will test all of the
421 // color types that we need to decode to on this backend.
422 const CodecSrc::DstColorType dstColorTypes[] = {
423 CodecSrc::kGetFromCanvas_DstColorType,
424 CodecSrc::kIndex8_Always_DstColorType,
425 CodecSrc::kGrayscale_Always_DstColorType,
426 };
427
428 const BRDSrc::Mode modes[] = {
429 BRDSrc::kFullImage_Mode,
430 BRDSrc::kDivisor_Mode
431 };
432
msaretta5783ae2015-09-08 15:35:32 -0700433 for (SkBitmapRegionDecoderInterface::Strategy strategy : strategies) {
msarett6efbe052015-09-11 09:01:16 -0700434
msaretta5783ae2015-09-08 15:35:32 -0700435 // We disable png testing for kOriginal_Strategy because the implementation leaks
436 // memory in our forked libpng.
437 // TODO (msarett): Decide if we want to test pngs in this mode and how we might do this.
438 if (SkBitmapRegionDecoderInterface::kOriginal_Strategy == strategy &&
439 (path.endsWith(".png") || path.endsWith(".PNG"))) {
440 continue;
441 }
msarett6efbe052015-09-11 09:01:16 -0700442 for (uint32_t sampleSize : sampleSizes) {
443
444 // kOriginal_Strategy does not work for jpegs that are scaled to non-powers of two.
445 // We don't need to test this. We know it doesn't work, and it causes images with
446 // uninitialized memory to show up on Gold.
447 if (SkBitmapRegionDecoderInterface::kOriginal_Strategy == strategy &&
448 (path.endsWith(".jpg") || path.endsWith(".JPG") ||
449 path.endsWith(".jpeg") || path.endsWith(".JPEG")) && !SkIsPow2(sampleSize)) {
450 continue;
451 }
452 for (CodecSrc::DstColorType dstColorType : dstColorTypes) {
453 if (brd_color_type_supported(strategy, dstColorType)) {
454 for (BRDSrc::Mode mode : modes) {
msaretta5783ae2015-09-08 15:35:32 -0700455 push_brd_src(path, strategy, dstColorType, mode, sampleSize);
456 }
457 }
458 }
459 }
460 }
461}
462
463static bool brd_supported(const char* ext) {
msarett8c8f22a2015-04-01 06:58:48 -0700464 static const char* const exts[] = {
msaretta5783ae2015-09-08 15:35:32 -0700465 "jpg", "jpeg", "png", "webp",
466 "JPG", "JPEG", "PNG", "WEBP",
msarett8c8f22a2015-04-01 06:58:48 -0700467 };
468
469 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
470 if (0 == strcmp(exts[i], ext)) {
471 return true;
472 }
473 }
474 return false;
scroggo9b77ddd2015-03-19 06:03:39 -0700475}
476
mtklein748ca3b2015-01-15 10:56:12 -0800477static void gather_srcs() {
478 for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) {
djsollen54416de2015-04-03 07:24:48 -0700479 push_src("gm", "", new GMSrc(r->factory()));
mtklein748ca3b2015-01-15 10:56:12 -0800480 }
halcanaryfc37ad12015-01-30 07:31:19 -0800481 for (int i = 0; i < FLAGS_skps.count(); i++) {
482 const char* path = FLAGS_skps[i];
483 if (sk_isdir(path)) {
484 SkOSFile::Iter it(path, "skp");
485 for (SkString file; it.next(&file); ) {
djsollen54416de2015-04-03 07:24:48 -0700486 push_src("skp", "", new SKPSrc(SkOSPath::Join(path, file.c_str())));
halcanaryfc37ad12015-01-30 07:31:19 -0800487 }
488 } else {
djsollen54416de2015-04-03 07:24:48 -0700489 push_src("skp", "", new SKPSrc(path));
mtklein114c3cd2015-01-15 10:15:02 -0800490 }
491 }
halcanary23b03c32015-01-30 09:58:58 -0800492 static const char* const exts[] = {
493 "bmp", "gif", "jpg", "jpeg", "png", "webp", "ktx", "astc", "wbmp", "ico",
494 "BMP", "GIF", "JPG", "JPEG", "PNG", "WEBP", "KTX", "ASTC", "WBMP", "ICO",
495 };
496 for (int i = 0; i < FLAGS_images.count(); i++) {
497 const char* flag = FLAGS_images[i];
498 if (sk_isdir(flag)) {
499 for (size_t j = 0; j < SK_ARRAY_COUNT(exts); j++) {
500 SkOSFile::Iter it(flag, exts[j]);
501 for (SkString file; it.next(&file); ) {
502 SkString path = SkOSPath::Join(flag, file.c_str());
djsollen54416de2015-04-03 07:24:48 -0700503 push_src("image", "decode", new ImageSrc(path)); // Decode entire image
504 push_src("image", "subset", new ImageSrc(path, 2)); // Decode into 2x2 subsets
msaretta5783ae2015-09-08 15:35:32 -0700505 push_codec_srcs(path);
506 if (brd_supported(exts[j])) {
507 push_brd_srcs(path);
scroggo9b77ddd2015-03-19 06:03:39 -0700508 }
halcanary23b03c32015-01-30 09:58:58 -0800509 }
mtklein114c3cd2015-01-15 10:15:02 -0800510 }
halcanary23b03c32015-01-30 09:58:58 -0800511 } else if (sk_exists(flag)) {
512 // assume that FLAGS_images[i] is a valid image if it is a file.
djsollen54416de2015-04-03 07:24:48 -0700513 push_src("image", "decode", new ImageSrc(flag)); // Decode entire image.
514 push_src("image", "subset", new ImageSrc(flag, 2)); // Decode into 2 x 2 subsets
msarett438b2ad2015-04-09 12:43:10 -0700515 push_codec_srcs(flag);
msaretta5783ae2015-09-08 15:35:32 -0700516 push_brd_srcs(flag);
mtklein709d2c32015-01-15 08:30:25 -0800517 }
mtklein709d2c32015-01-15 08:30:25 -0800518 }
519}
520
mtklein748ca3b2015-01-15 10:56:12 -0800521static GrGLStandard get_gpu_api() {
522 if (FLAGS_gpuAPI.contains("gl")) { return kGL_GrGLStandard; }
523 if (FLAGS_gpuAPI.contains("gles")) { return kGLES_GrGLStandard; }
524 return kNone_GrGLStandard;
mtklein709d2c32015-01-15 08:30:25 -0800525}
526
mtklein748ca3b2015-01-15 10:56:12 -0800527static void push_sink(const char* tag, Sink* s) {
528 SkAutoTDelete<Sink> sink(s);
529 if (!FLAGS_config.contains(tag)) {
530 return;
mtklein114c3cd2015-01-15 10:15:02 -0800531 }
mtkleine0effd62015-07-29 06:37:28 -0700532 // Try a simple Src as a canary. If it fails, skip this sink.
mtklein748ca3b2015-01-15 10:56:12 -0800533 struct : public Src {
mtkleine0effd62015-07-29 06:37:28 -0700534 Error draw(SkCanvas* c) const override {
535 c->drawRect(SkRect::MakeWH(1,1), SkPaint());
536 return "";
537 }
mtklein36352bf2015-03-25 18:17:31 -0700538 SkISize size() const override { return SkISize::Make(16, 16); }
mtkleine0effd62015-07-29 06:37:28 -0700539 Name name() const override { return "justOneRect"; }
540 } justOneRect;
mtklein114c3cd2015-01-15 10:15:02 -0800541
mtklein748ca3b2015-01-15 10:56:12 -0800542 SkBitmap bitmap;
543 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800544 SkString log;
mtkleine0effd62015-07-29 06:37:28 -0700545 Error err = sink->draw(justOneRect, &bitmap, &stream, &log);
mtklein4089ef72015-03-05 08:40:28 -0800546 if (err.isFatal()) {
mtklein05641a52015-04-21 10:49:13 -0700547 SkDebugf("Could not run %s: %s\n", tag, err.c_str());
548 exit(1);
mtklein114c3cd2015-01-15 10:15:02 -0800549 }
550
mtkleine0effd62015-07-29 06:37:28 -0700551 TaggedSink& ts = gSinks.push_back();
mtklein748ca3b2015-01-15 10:56:12 -0800552 ts.reset(sink.detach());
553 ts.tag = tag;
554}
555
556static bool gpu_supported() {
557#if SK_SUPPORT_GPU
558 return FLAGS_gpu;
559#else
560 return false;
561#endif
562}
563
564static Sink* create_sink(const char* tag) {
565#define SINK(t, sink, ...) if (0 == strcmp(t, tag)) { return new sink(__VA_ARGS__); }
566 if (gpu_supported()) {
mtklein82d28432015-01-15 12:46:02 -0800567 typedef GrContextFactory Gr;
mtklein748ca3b2015-01-15 10:56:12 -0800568 const GrGLStandard api = get_gpu_api();
hendrikw885bf092015-08-27 10:38:39 -0700569 SINK("gpunull", GPUSink, Gr::kNull_GLContextType, api, 0, false, FLAGS_gpu_threading);
570 SINK("gpudebug", GPUSink, Gr::kDebug_GLContextType, api, 0, false, FLAGS_gpu_threading);
571 SINK("gpu", GPUSink, Gr::kNative_GLContextType, api, 0, false, FLAGS_gpu_threading);
572 SINK("gpudft", GPUSink, Gr::kNative_GLContextType, api, 0, true, FLAGS_gpu_threading);
573 SINK("msaa4", GPUSink, Gr::kNative_GLContextType, api, 4, false, FLAGS_gpu_threading);
574 SINK("msaa16", GPUSink, Gr::kNative_GLContextType, api, 16, false, FLAGS_gpu_threading);
bsalomon90e8ab72015-09-08 10:26:51 -0700575 SINK("nvprmsaa4", GPUSink, Gr::kNVPR_GLContextType, api, 4, true, FLAGS_gpu_threading);
576 SINK("nvprmsaa16", GPUSink, Gr::kNVPR_GLContextType, api, 16, true, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800577 #if SK_ANGLE
hendrikw885bf092015-08-27 10:38:39 -0700578 SINK("angle", GPUSink, Gr::kANGLE_GLContextType, api, 0, false, FLAGS_gpu_threading);
hendrikweddbefb2015-09-11 13:07:29 -0700579 SINK("angle-gl", GPUSink, Gr::kANGLE_GL_GLContextType, api, 0, false, FLAGS_gpu_threading);
hendrikw885bf092015-08-27 10:38:39 -0700580 #endif
581 #if SK_COMMAND_BUFFER
582 SINK("commandbuffer", GPUSink, Gr::kCommandBuffer_GLContextType, api, 0, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800583 #endif
584 #if SK_MESA
hendrikw885bf092015-08-27 10:38:39 -0700585 SINK("mesa", GPUSink, Gr::kMESA_GLContextType, api, 0, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800586 #endif
mtklein114c3cd2015-01-15 10:15:02 -0800587 }
mtklein748ca3b2015-01-15 10:56:12 -0800588
tomhudsoneebc39a2015-02-23 12:18:05 -0800589#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
590 SINK("hwui", HWUISink);
591#endif
592
mtklein748ca3b2015-01-15 10:56:12 -0800593 if (FLAGS_cpu) {
594 SINK("565", RasterSink, kRGB_565_SkColorType);
595 SINK("8888", RasterSink, kN32_SkColorType);
halcanaryc11c62f2015-09-28 11:51:54 -0700596 SINK("pdf", PDFSink, "Pdfium");
597 SINK("pdf_poppler", PDFSink, "Poppler");
mtklein9c3f17d2015-01-28 11:35:18 -0800598 SINK("skp", SKPSink);
mtklein8a4527e2015-01-31 20:00:58 -0800599 SINK("svg", SVGSink);
600 SINK("null", NullSink);
halcanary47ef4d52015-03-03 09:13:09 -0800601 SINK("xps", XPSSink);
mtklein748ca3b2015-01-15 10:56:12 -0800602 }
603#undef SINK
halcanary96fcdcc2015-08-27 07:41:13 -0700604 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800605}
606
mtklein748ca3b2015-01-15 10:56:12 -0800607static Sink* create_via(const char* tag, Sink* wrapped) {
608#define VIA(t, via, ...) if (0 == strcmp(t, tag)) { return new via(__VA_ARGS__); }
halcanary96fcdcc2015-08-27 07:41:13 -0700609 VIA("twice", ViaTwice, wrapped);
610 VIA("pipe", ViaPipe, wrapped);
611 VIA("serialize", ViaSerialization, wrapped);
612 VIA("2ndpic", ViaSecondPicture, wrapped);
mtkleind31c13d2015-05-05 12:59:56 -0700613 VIA("sp", ViaSingletonPictures, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700614 VIA("tiles", ViaTiles, 256, 256, nullptr, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800615 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800616
mtkleind603b222015-02-17 11:13:33 -0800617 if (FLAGS_matrix.count() == 4) {
mtklein748ca3b2015-01-15 10:56:12 -0800618 SkMatrix m;
mtkleind603b222015-02-17 11:13:33 -0800619 m.reset();
620 m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
621 m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
622 m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
623 m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
624 VIA("matrix", ViaMatrix, m, wrapped);
625 VIA("upright", ViaUpright, m, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800626 }
tomhudson64de1e12015-03-05 08:01:07 -0800627
628#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
629 VIA("androidsdk", ViaAndroidSDK, wrapped);
630#endif
631
mtklein748ca3b2015-01-15 10:56:12 -0800632#undef VIA
halcanary96fcdcc2015-08-27 07:41:13 -0700633 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800634}
635
mtklein748ca3b2015-01-15 10:56:12 -0800636static void gather_sinks() {
637 for (int i = 0; i < FLAGS_config.count(); i++) {
638 const char* config = FLAGS_config[i];
639 SkTArray<SkString> parts;
640 SkStrSplit(config, "-", &parts);
641
halcanary96fcdcc2015-08-27 07:41:13 -0700642 Sink* sink = nullptr;
mtklein748ca3b2015-01-15 10:56:12 -0800643 for (int i = parts.count(); i-- > 0;) {
644 const char* part = parts[i].c_str();
halcanary96fcdcc2015-08-27 07:41:13 -0700645 Sink* next = (sink == nullptr) ? create_sink(part) : create_via(part, sink);
646 if (next == nullptr) {
mtklein748ca3b2015-01-15 10:56:12 -0800647 SkDebugf("Skipping %s: Don't understand '%s'.\n", config, part);
648 delete sink;
halcanary96fcdcc2015-08-27 07:41:13 -0700649 sink = nullptr;
mtklein748ca3b2015-01-15 10:56:12 -0800650 break;
651 }
652 sink = next;
653 }
654 if (sink) {
655 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800656 }
657 }
658}
mtklein709d2c32015-01-15 08:30:25 -0800659
mtkleina5114d72015-08-24 13:27:01 -0700660static bool dump_png(SkBitmap bitmap, const char* path, const char* md5) {
661 const int w = bitmap.width(),
662 h = bitmap.height();
663
664 // First get the bitmap into N32 color format. The next step will work only there.
665 if (bitmap.colorType() != kN32_SkColorType) {
666 SkBitmap n32;
667 if (!bitmap.copyTo(&n32, kN32_SkColorType)) {
668 return false;
669 }
670 bitmap = n32;
671 }
672
673 // Convert our N32 bitmap into unpremul RGBA for libpng.
674 SkAutoTMalloc<uint32_t> rgba(w*h);
675 if (!bitmap.readPixels(SkImageInfo::Make(w,h, kRGBA_8888_SkColorType, kUnpremul_SkAlphaType),
676 rgba, 4*w, 0,0)) {
677 return false;
678 }
679
680 // We don't need bitmap anymore. Might as well drop our ref.
mtkleinfccb77d2015-08-24 14:13:29 -0700681 bitmap.reset();
mtkleina5114d72015-08-24 13:27:01 -0700682
mtkleinc64137c2015-08-25 10:56:08 -0700683 FILE* f = fopen(path, "wb");
mtkleina5114d72015-08-24 13:27:01 -0700684 if (!f) { return false; }
685
686 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
687 if (!png) {
688 fclose(f);
689 return false;
690 }
691
692 png_infop info = png_create_info_struct(png);
693 if (!info) {
694 png_destroy_write_struct(&png, &info);
695 fclose(f);
696 return false;
697 }
698
mtkleind69ece62015-09-10 10:37:44 -0700699 SkString description;
700 description.append("Key: ");
701 for (int i = 0; i < FLAGS_key.count(); i++) {
702 description.appendf("%s ", FLAGS_key[i]);
703 }
704 description.append("Properties: ");
705 for (int i = 0; i < FLAGS_properties.count(); i++) {
706 description.appendf("%s ", FLAGS_properties[i]);
707 }
708 description.appendf("MD5: %s", md5);
709
mtkleina5114d72015-08-24 13:27:01 -0700710 png_text text[2];
711 text[0].key = (png_charp)"Author";
712 text[0].text = (png_charp)"DM dump_png()";
713 text[0].compression = PNG_TEXT_COMPRESSION_NONE;
714 text[1].key = (png_charp)"Description";
mtkleind69ece62015-09-10 10:37:44 -0700715 text[1].text = (png_charp)description.c_str();
mtkleina5114d72015-08-24 13:27:01 -0700716 text[1].compression = PNG_TEXT_COMPRESSION_NONE;
717 png_set_text(png, info, text, 2);
718
719 png_init_io(png, f);
720 png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8,
721 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
722 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
723 png_write_info(png, info);
724 for (int j = 0; j < h; j++) {
725 png_bytep row = (png_bytep)(rgba.get() + w*j);
726 png_write_rows(png, &row, 1);
727 }
728 png_write_end(png, info);
729
730 png_destroy_write_struct(&png, &info);
731 fclose(f);
732 return true;
733}
734
mtkleina2ef6422015-01-15 13:44:22 -0800735static bool match(const char* needle, const char* haystack) {
halcanary96fcdcc2015-08-27 07:41:13 -0700736 return 0 == strcmp("_", needle) || nullptr != strstr(haystack, needle);
mtkleina2ef6422015-01-15 13:44:22 -0800737}
738
djsollen54416de2015-04-03 07:24:48 -0700739static ImplicitString is_blacklisted(const char* sink, const char* src,
740 const char* srcOptions, const char* name) {
mtklein0d243ff2015-04-03 07:51:00 -0700741 for (int i = 0; i < FLAGS_blacklist.count() - 3; i += 4) {
mtkleina2ef6422015-01-15 13:44:22 -0800742 if (match(FLAGS_blacklist[i+0], sink) &&
djsollen54416de2015-04-03 07:24:48 -0700743 match(FLAGS_blacklist[i+1], src) &&
744 match(FLAGS_blacklist[i+2], srcOptions) &&
745 match(FLAGS_blacklist[i+3], name)) {
746 return SkStringPrintf("%s %s %s %s",
747 FLAGS_blacklist[i+0], FLAGS_blacklist[i+1],
748 FLAGS_blacklist[i+2], FLAGS_blacklist[i+3]);
mtkleina2ef6422015-01-15 13:44:22 -0800749 }
750 }
751 return "";
752}
753
mtklein748ca3b2015-01-15 10:56:12 -0800754// The finest-grained unit of work we can run: draw a single Src into a single Sink,
755// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
756struct Task {
mtkleine0effd62015-07-29 06:37:28 -0700757 Task(const TaggedSrc& src, const TaggedSink& sink) : src(src), sink(sink) {}
758 const TaggedSrc& src;
759 const TaggedSink& sink;
mtklein748ca3b2015-01-15 10:56:12 -0800760
761 static void Run(Task* task) {
mtkleina2ef6422015-01-15 13:44:22 -0800762 SkString name = task->src->name();
mtkleine0effd62015-07-29 06:37:28 -0700763
764 // We'll skip drawing this Src/Sink pair if:
765 // - the Src vetoes the Sink;
766 // - this Src / Sink combination is on the blacklist;
767 // - it's a dry run.
mtklein99cab4e2015-07-31 06:43:04 -0700768 SkString note(task->src->veto(task->sink->flags()) ? " (veto)" : "");
msarett9e707a02015-09-01 14:57:57 -0700769 SkString whyBlacklisted = is_blacklisted(task->sink.tag, task->src.tag.c_str(),
770 task->src.options.c_str(), name.c_str());
mtkleinb37cb412015-03-18 05:27:14 -0700771 if (!whyBlacklisted.isEmpty()) {
772 note.appendf(" (--blacklist %s)", whyBlacklisted.c_str());
773 }
mtkleine0effd62015-07-29 06:37:28 -0700774
mtkleinb9eb4ac2015-02-02 18:26:03 -0800775 SkString log;
mtklein748ca3b2015-01-15 10:56:12 -0800776 WallTimer timer;
777 timer.start();
mtkleine0effd62015-07-29 06:37:28 -0700778 if (!FLAGS_dryRun && note.isEmpty()) {
mtklein748ca3b2015-01-15 10:56:12 -0800779 SkBitmap bitmap;
780 SkDynamicMemoryWStream stream;
bsalomon821e10e2015-06-04 14:15:33 -0700781 if (FLAGS_pre_log) {
782 SkDebugf("\nRunning %s->%s", name.c_str(), task->sink.tag);
783 }
djsollen54416de2015-04-03 07:24:48 -0700784 start(task->sink.tag, task->src.tag, task->src.options, name.c_str());
mtkleinb9eb4ac2015-02-02 18:26:03 -0800785 Error err = task->sink->draw(*task->src, &bitmap, &stream, &log);
mtklein748ca3b2015-01-15 10:56:12 -0800786 if (!err.isEmpty()) {
mtklein4089ef72015-03-05 08:40:28 -0800787 timer.end();
788 if (err.isFatal()) {
djsollen54416de2015-04-03 07:24:48 -0700789 fail(SkStringPrintf("%s %s %s %s: %s",
mtklein4089ef72015-03-05 08:40:28 -0800790 task->sink.tag,
msarett9e707a02015-09-01 14:57:57 -0700791 task->src.tag.c_str(),
792 task->src.options.c_str(),
mtklein4089ef72015-03-05 08:40:28 -0800793 name.c_str(),
794 err.c_str()));
mtkleinb37cb412015-03-18 05:27:14 -0700795 } else {
796 note.appendf(" (skipped: %s)", err.c_str());
mtklein4089ef72015-03-05 08:40:28 -0800797 }
djsollen54416de2015-04-03 07:24:48 -0700798 done(timer.fWall, task->sink.tag, task->src.tag, task->src.options,
799 name, note, log);
mtklein4089ef72015-03-05 08:40:28 -0800800 return;
mtklein748ca3b2015-01-15 10:56:12 -0800801 }
mtklein62bd1a62015-01-27 14:46:26 -0800802 SkAutoTDelete<SkStreamAsset> data(stream.detachAsStream());
803
804 SkString md5;
805 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
806 SkMD5 hash;
807 if (data->getLength()) {
808 hash.writeStream(data, data->getLength());
809 data->rewind();
810 } else {
mtklein38408462015-07-08 07:25:27 -0700811 // If we're BGRA (Linux, Windows), swizzle over to RGBA (Mac, Android).
812 // This helps eliminate multiple 0-pixel-diff hashes on gold.skia.org.
813 // (Android's general slow speed breaks the tie arbitrarily in RGBA's favor.)
814 // We might consider promoting 565 to RGBA too.
815 if (bitmap.colorType() == kBGRA_8888_SkColorType) {
816 SkBitmap swizzle;
817 SkAssertResult(bitmap.copyTo(&swizzle, kRGBA_8888_SkColorType));
818 hash.write(swizzle.getPixels(), swizzle.getSize());
819 } else {
820 hash.write(bitmap.getPixels(), bitmap.getSize());
821 }
mtklein62bd1a62015-01-27 14:46:26 -0800822 }
823 SkMD5::Digest digest;
824 hash.finish(digest);
825 for (int i = 0; i < 16; i++) {
826 md5.appendf("%02x", digest.data[i]);
827 }
828 }
829
830 if (!FLAGS_readPath.isEmpty() &&
msarett9e707a02015-09-01 14:57:57 -0700831 !gGold.contains(Gold(task->sink.tag, task->src.tag.c_str(),
832 task->src.options.c_str(), name, md5))) {
djsollen54416de2015-04-03 07:24:48 -0700833 fail(SkStringPrintf("%s not found for %s %s %s %s in %s",
mtklein62bd1a62015-01-27 14:46:26 -0800834 md5.c_str(),
835 task->sink.tag,
msarett9e707a02015-09-01 14:57:57 -0700836 task->src.tag.c_str(),
837 task->src.options.c_str(),
mtklein62bd1a62015-01-27 14:46:26 -0800838 name.c_str(),
839 FLAGS_readPath[0]));
840 }
841
mtkleinb0531a72015-04-07 13:38:48 -0700842 if (!FLAGS_writePath.isEmpty()) {
mtklein748ca3b2015-01-15 10:56:12 -0800843 const char* ext = task->sink->fileExtension();
mtklein62bd1a62015-01-27 14:46:26 -0800844 if (data->getLength()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700845 WriteToDisk(*task, md5, ext, data, data->getLength(), nullptr);
halcanary022afb82015-01-30 11:00:12 -0800846 SkASSERT(bitmap.drawsNothing());
847 } else if (!bitmap.drawsNothing()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700848 WriteToDisk(*task, md5, ext, nullptr, 0, &bitmap);
mtklein748ca3b2015-01-15 10:56:12 -0800849 }
850 }
851 }
852 timer.end();
msarett9e707a02015-09-01 14:57:57 -0700853 done(timer.fWall, task->sink.tag, task->src.tag.c_str(), task->src.options.c_str(), name,
854 note, log);
mtklein748ca3b2015-01-15 10:56:12 -0800855 }
856
857 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -0800858 SkString md5,
859 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -0800860 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -0800861 const SkBitmap* bitmap) {
mtklein748ca3b2015-01-15 10:56:12 -0800862 JsonWriter::BitmapResult result;
djsollen54416de2015-04-03 07:24:48 -0700863 result.name = task.src->name();
864 result.config = task.sink.tag;
865 result.sourceType = task.src.tag;
866 result.sourceOptions = task.src.options;
867 result.ext = ext;
868 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -0800869 JsonWriter::AddBitmapResult(result);
870
mtkleinb0531a72015-04-07 13:38:48 -0700871 // If an MD5 is uninteresting, we want it noted in the JSON file,
872 // but don't want to dump it out as a .png (or whatever ext is).
873 if (gUninterestingHashes.contains(md5)) {
874 return;
875 }
876
mtklein748ca3b2015-01-15 10:56:12 -0800877 const char* dir = FLAGS_writePath[0];
878 if (0 == strcmp(dir, "@")) { // Needed for iOS.
879 dir = FLAGS_resourcePath[0];
880 }
881 sk_mkdir(dir);
882
883 SkString path;
884 if (FLAGS_nameByHash) {
885 path = SkOSPath::Join(dir, result.md5.c_str());
886 path.append(".");
887 path.append(ext);
888 if (sk_exists(path.c_str())) {
889 return; // Content-addressed. If it exists already, we're done.
890 }
891 } else {
892 path = SkOSPath::Join(dir, task.sink.tag);
893 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -0700894 path = SkOSPath::Join(path.c_str(), task.src.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800895 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -0700896 if (strcmp(task.src.options.c_str(), "") != 0) {
897 path = SkOSPath::Join(path.c_str(), task.src.options.c_str());
djsollen54416de2015-04-03 07:24:48 -0700898 sk_mkdir(path.c_str());
899 }
mtklein748ca3b2015-01-15 10:56:12 -0800900 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
901 path.append(".");
902 path.append(ext);
903 }
904
mtklein748ca3b2015-01-15 10:56:12 -0800905 if (bitmap) {
mtkleina5114d72015-08-24 13:27:01 -0700906 if (!dump_png(*bitmap, path.c_str(), result.md5.c_str())) {
mtklein748ca3b2015-01-15 10:56:12 -0800907 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
908 return;
909 }
910 } else {
mtkleina5114d72015-08-24 13:27:01 -0700911 SkFILEWStream file(path.c_str());
912 if (!file.isValid()) {
913 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
914 return;
915 }
mtklein748ca3b2015-01-15 10:56:12 -0800916 if (!file.writeStream(data, len)) {
917 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
918 return;
919 }
920 }
921 }
922};
923
924// Run all tasks in the same enclave serially on the same thread.
925// They can't possibly run concurrently with each other.
926static void run_enclave(SkTArray<Task>* tasks) {
927 for (int i = 0; i < tasks->count(); i++) {
928 Task::Run(tasks->begin() + i);
929 }
930}
931
932/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
933
934// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
935
mtklein55e88b22015-01-21 15:50:13 -0800936static SkTDArray<skiatest::Test> gThreadedTests, gGPUTests;
mtklein748ca3b2015-01-15 10:56:12 -0800937
938static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -0800939 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -0800940 return;
941 }
mtklein6393c062015-04-27 08:45:01 -0700942 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r; r = r->next()) {
943 if (!in_shard()) {
944 continue;
945 }
halcanary87f3ba42015-01-20 09:30:20 -0800946 // Despite its name, factory() is returning a reference to
947 // link-time static const POD data.
948 const skiatest::Test& test = r->factory();
949 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -0800950 continue;
951 }
halcanary87f3ba42015-01-20 09:30:20 -0800952 if (test.needsGpu && gpu_supported()) {
mtklein55e88b22015-01-21 15:50:13 -0800953 (FLAGS_gpu_threading ? gThreadedTests : gGPUTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -0800954 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein55e88b22015-01-21 15:50:13 -0800955 gThreadedTests.push(test);
mtklein82d28432015-01-15 12:46:02 -0800956 }
mtklein748ca3b2015-01-15 10:56:12 -0800957 }
958}
959
halcanary87f3ba42015-01-20 09:30:20 -0800960static void run_test(skiatest::Test* test) {
961 struct : public skiatest::Reporter {
mtklein36352bf2015-03-25 18:17:31 -0700962 void reportFailed(const skiatest::Failure& failure) override {
halcanary87f3ba42015-01-20 09:30:20 -0800963 fail(failure.toString());
964 JsonWriter::AddTestFailure(failure);
965 }
mtklein36352bf2015-03-25 18:17:31 -0700966 bool allowExtendedTest() const override {
halcanary87f3ba42015-01-20 09:30:20 -0800967 return FLAGS_pathOpsExtended;
968 }
mtklein36352bf2015-03-25 18:17:31 -0700969 bool verbose() const override { return FLAGS_veryVerbose; }
halcanary87f3ba42015-01-20 09:30:20 -0800970 } reporter;
djsollen824996a2015-06-12 12:06:22 -0700971
972 SkString note;
973 SkString whyBlacklisted = is_blacklisted("_", "tests", "_", test->name);
974 if (!whyBlacklisted.isEmpty()) {
975 note.appendf(" (--blacklist %s)", whyBlacklisted.c_str());
976 }
977
mtklein748ca3b2015-01-15 10:56:12 -0800978 WallTimer timer;
979 timer.start();
djsollen824996a2015-06-12 12:06:22 -0700980 if (!FLAGS_dryRun && whyBlacklisted.isEmpty()) {
djsollen54416de2015-04-03 07:24:48 -0700981 start("unit", "test", "", test->name);
mtklein55e88b22015-01-21 15:50:13 -0800982 GrContextFactory factory;
bsalomon821e10e2015-06-04 14:15:33 -0700983 if (FLAGS_pre_log) {
984 SkDebugf("\nRunning test %s", test->name);
985 }
mtklein55e88b22015-01-21 15:50:13 -0800986 test->proc(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -0800987 }
988 timer.end();
djsollen824996a2015-06-12 12:06:22 -0700989 done(timer.fWall, "unit", "test", "", test->name, note, "");
mtklein748ca3b2015-01-15 10:56:12 -0800990}
991
992/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
993
mtklein55e88b22015-01-21 15:50:13 -0800994// If we're isolating all GPU-bound work to one thread (the default), this function runs all that.
995static void run_enclave_and_gpu_tests(SkTArray<Task>* tasks) {
996 run_enclave(tasks);
997 for (int i = 0; i < gGPUTests.count(); i++) {
998 run_test(&gGPUTests[i]);
999 }
1000}
1001
mtkleinde6fc2b2015-03-12 06:28:54 -07001002// Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung.
1003// This prints something every once in a while so that it knows we're still working.
mtklein2e1c47e2015-03-12 07:16:56 -07001004static void start_keepalive() {
1005 struct Loop {
1006 static void forever(void*) {
1007 for (;;) {
1008 static const int kSec = 300;
1009 #if defined(SK_BUILD_FOR_WIN)
1010 Sleep(kSec * 1000);
1011 #else
1012 sleep(kSec);
1013 #endif
mtkleinb37cb412015-03-18 05:27:14 -07001014 SkString running;
1015 {
1016 SkAutoMutexAcquire lock(gRunningMutex);
1017 for (int i = 0; i < gRunning.count(); i++) {
1018 running.appendf("\n\t%s", gRunning[i].c_str());
1019 }
1020 }
1021 SkDebugf("\nCurrently running:%s\n", running.c_str());
mtklein2e1c47e2015-03-12 07:16:56 -07001022 }
1023 }
1024 };
1025 static SkThread* intentionallyLeaked = new SkThread(Loop::forever);
1026 intentionallyLeaked->start();
mtkleinde6fc2b2015-03-12 06:28:54 -07001027}
1028
caryclark83ca6282015-06-10 09:31:09 -07001029#define PORTABLE_FONT_PREFIX "Toy Liberation "
1030
1031static SkTypeface* create_from_name(const char familyName[], SkTypeface::Style style) {
1032 if (familyName && strlen(familyName) > sizeof(PORTABLE_FONT_PREFIX)
1033 && !strncmp(familyName, PORTABLE_FONT_PREFIX, sizeof(PORTABLE_FONT_PREFIX) - 1)) {
caryclark1818acb2015-07-24 12:09:25 -07001034 return sk_tool_utils::create_portable_typeface(familyName, style);
caryclark83ca6282015-06-10 09:31:09 -07001035 }
halcanary96fcdcc2015-08-27 07:41:13 -07001036 return nullptr;
caryclark83ca6282015-06-10 09:31:09 -07001037}
1038
1039#undef PORTABLE_FONT_PREFIX
1040
1041extern SkTypeface* (*gCreateTypefaceDelegate)(const char [], SkTypeface::Style );
1042
jcgregorio3b27ade2014-11-13 08:06:40 -08001043int dm_main();
caryclark17f0b6d2014-07-22 10:15:34 -07001044int dm_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -08001045 SetupCrashHandler();
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +00001046 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -07001047 SkTaskGroup::Enabler enabled(FLAGS_threads);
caryclark83ca6282015-06-10 09:31:09 -07001048 gCreateTypefaceDelegate = &create_from_name;
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +00001049
mtklein2e1c47e2015-03-12 07:16:56 -07001050 start_keepalive();
mtkleinde6fc2b2015-03-12 06:28:54 -07001051
mtklein62bd1a62015-01-27 14:46:26 -08001052 gather_gold();
borenet09ed4802015-04-03 14:15:33 -07001053 gather_uninteresting_hashes();
mtklein62bd1a62015-01-27 14:46:26 -08001054
mtklein748ca3b2015-01-15 10:56:12 -08001055 gather_srcs();
1056 gather_sinks();
1057 gather_tests();
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +00001058
mtklein55e88b22015-01-21 15:50:13 -08001059 gPending = gSrcs.count() * gSinks.count() + gThreadedTests.count() + gGPUTests.count();
mtklein748ca3b2015-01-15 10:56:12 -08001060 SkDebugf("%d srcs * %d sinks + %d tests == %d tasks\n",
mtklein55e88b22015-01-21 15:50:13 -08001061 gSrcs.count(), gSinks.count(), gThreadedTests.count() + gGPUTests.count(), gPending);
mtklein748ca3b2015-01-15 10:56:12 -08001062
1063 // We try to exploit as much parallelism as is safe. Most Src/Sink pairs run on any thread,
1064 // but Sinks that identify as part of a particular enclave run serially on a single thread.
mtklein82d28432015-01-15 12:46:02 -08001065 // CPU tests run on any thread. GPU tests depend on --gpu_threading.
mtklein748ca3b2015-01-15 10:56:12 -08001066 SkTArray<Task> enclaves[kNumEnclaves];
1067 for (int j = 0; j < gSinks.count(); j++) {
1068 SkTArray<Task>& tasks = enclaves[gSinks[j]->enclave()];
1069 for (int i = 0; i < gSrcs.count(); i++) {
1070 tasks.push_back(Task(gSrcs[i], gSinks[j]));
1071 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001072 }
1073
mtklein748ca3b2015-01-15 10:56:12 -08001074 SkTaskGroup tg;
mtklein55e88b22015-01-21 15:50:13 -08001075 tg.batch(run_test, gThreadedTests.begin(), gThreadedTests.count());
1076 for (int i = 0; i < kNumEnclaves; i++) {
1077 switch(i) {
1078 case kAnyThread_Enclave:
1079 tg.batch(Task::Run, enclaves[i].begin(), enclaves[i].count());
1080 break;
1081 case kGPU_Enclave:
1082 tg.add(run_enclave_and_gpu_tests, &enclaves[i]);
1083 break;
1084 default:
1085 tg.add(run_enclave, &enclaves[i]);
1086 break;
mtklein82d28432015-01-15 12:46:02 -08001087 }
mtklein55e88b22015-01-21 15:50:13 -08001088 }
mtklein748ca3b2015-01-15 10:56:12 -08001089 tg.wait();
mtklein748ca3b2015-01-15 10:56:12 -08001090 // At this point we're back in single-threaded land.
caryclarkf53ce802015-06-15 06:48:30 -07001091 sk_tool_utils::release_portable_typefaces();
mtklein@google.comd36522d2013-10-16 13:02:15 +00001092
mtklein2f64eec2015-01-15 14:20:41 -08001093 SkDebugf("\n");
mtklein748ca3b2015-01-15 10:56:12 -08001094 if (gFailures.count() > 0) {
1095 SkDebugf("Failures:\n");
1096 for (int i = 0; i < gFailures.count(); i++) {
mtklein9dc09102015-01-15 15:47:33 -08001097 SkDebugf("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001098 }
1099 SkDebugf("%d failures\n", gFailures.count());
1100 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001101 }
mtklein748ca3b2015-01-15 10:56:12 -08001102 if (gPending > 0) {
1103 SkDebugf("Hrm, we didn't seem to run everything we intended to! Please file a bug.\n");
1104 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001105 }
halcanary7a14b312015-10-01 07:28:13 -07001106 #ifdef SK_PDF_IMAGE_STATS
1107 SkPDFImageDumpStats();
1108 #endif // SK_PDF_IMAGE_STATS
mtklein748ca3b2015-01-15 10:56:12 -08001109 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +00001110}
jcgregorio3b27ade2014-11-13 08:06:40 -08001111
borenet48087572015-04-02 12:16:36 -07001112#if !defined(SK_BUILD_FOR_IOS)
jcgregorio3b27ade2014-11-13 08:06:40 -08001113int main(int argc, char** argv) {
1114 SkCommandLineFlags::Parse(argc, argv);
1115 return dm_main();
1116}
1117#endif