blob: 403252b03d7e3d205d086df4dc5839dc6dd99fbc [file] [log] [blame]
scroggo478652e2015-03-25 07:11:02 -07001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
jcgregorio3b27ade2014-11-13 08:06:40 -08008#include "CrashHandler.h"
mtklein748ca3b2015-01-15 10:56:12 -08009#include "DMJsonWriter.h"
10#include "DMSrcSink.h"
tomhudsoneebc39a2015-02-23 12:18:05 -080011#include "DMSrcSinkAndroid.h"
mtklein748ca3b2015-01-15 10:56:12 -080012#include "OverwriteLine.h"
13#include "ProcStats.h"
14#include "SkBBHFactory.h"
mtklein62bd1a62015-01-27 14:46:26 -080015#include "SkChecksum.h"
scroggocc2feb12015-08-14 08:32:46 -070016#include "SkCodec.h"
caryclark17f0b6d2014-07-22 10:15:34 -070017#include "SkCommonFlags.h"
caryclark83ca6282015-06-10 09:31:09 -070018#include "SkFontMgr.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +000019#include "SkForceLinking.h"
20#include "SkGraphics.h"
mtklein748ca3b2015-01-15 10:56:12 -080021#include "SkMD5.h"
mtklein1b249332015-07-07 12:21:21 -070022#include "SkMutex.h"
mtklein1d0f1642014-09-08 08:05:18 -070023#include "SkOSFile.h"
mtkleina82f5622015-02-20 12:30:19 -080024#include "SkTHash.h"
mtklein406654b2014-09-03 15:34:37 -070025#include "SkTaskGroup.h"
mtkleinde6fc2b2015-03-12 06:28:54 -070026#include "SkThreadUtils.h"
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +000027#include "Test.h"
mtklein748ca3b2015-01-15 10:56:12 -080028#include "Timer.h"
caryclark83ca6282015-06-10 09:31:09 -070029#include "sk_tool_utils.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +000030
halcanary7a14b312015-10-01 07:28:13 -070031#ifdef SK_PDF_IMAGE_STATS
32extern void SkPDFImageDumpStats();
33#endif
34
mtkleina5114d72015-08-24 13:27:01 -070035#include "png.h"
36
bungeman60e0fee2015-08-26 05:15:46 -070037#include <stdlib.h>
38
scroggo27a58342015-10-28 08:56:41 -070039#ifndef SK_BUILD_FOR_WIN32
40 #include <unistd.h>
41#endif
42
djsollen54416de2015-04-03 07:24:48 -070043DEFINE_string(src, "tests gm skp image", "Source types to test.");
mtklein748ca3b2015-01-15 10:56:12 -080044DEFINE_bool(nameByHash, false,
45 "If true, write to FLAGS_writePath[0]/<hash>.png instead of "
djsollen54416de2015-04-03 07:24:48 -070046 "to FLAGS_writePath[0]/<config>/<sourceType>/<sourceOptions>/<name>.png");
mtklein748ca3b2015-01-15 10:56:12 -080047DEFINE_bool2(pathOpsExtended, x, false, "Run extended pathOps tests.");
mtkleind603b222015-02-17 11:13:33 -080048DEFINE_string(matrix, "1 0 0 1",
49 "2x2 scale+skew matrix to apply or upright when using "
50 "'matrix' or 'upright' in config.");
mtklein82d28432015-01-15 12:46:02 -080051DEFINE_bool(gpu_threading, false, "Allow GPU work to run on multiple threads?");
mtklein@google.comd36522d2013-10-16 13:02:15 +000052
mtkleina2ef6422015-01-15 13:44:22 -080053DEFINE_string(blacklist, "",
djsollen54416de2015-04-03 07:24:48 -070054 "Space-separated config/src/srcOptions/name quadruples to blacklist. '_' matches anything. E.g. \n"
55 "'--blacklist gpu skp _ _' will blacklist all SKPs drawn into the gpu config.\n"
56 "'--blacklist gpu skp _ _ 8888 gm _ aarects' will also blacklist the aarects GM on 8888.");
mtkleina2ef6422015-01-15 13:44:22 -080057
mtklein62bd1a62015-01-27 14:46:26 -080058DEFINE_string2(readPath, r, "", "If set check for equality with golden results in this directory.");
59
borenet09ed4802015-04-03 14:15:33 -070060DEFINE_string(uninterestingHashesFile, "",
61 "File containing a list of uninteresting hashes. If a result hashes to something in "
62 "this list, no image is written for that result.");
63
mtklein6393c062015-04-27 08:45:01 -070064DEFINE_int32(shards, 1, "We're splitting source data into this many shards.");
65DEFINE_int32(shard, 0, "Which shard do I run?");
bsalomon821e10e2015-06-04 14:15:33 -070066DEFINE_bool2(pre_log, p, false, "Log before running each test. May be incomprehensible when threading");
mtklein6393c062015-04-27 08:45:01 -070067
mtklein@google.comd36522d2013-10-16 13:02:15 +000068__SK_FORCE_IMAGE_DECODER_LINKING;
mtklein748ca3b2015-01-15 10:56:12 -080069using namespace DM;
mtklein@google.comd36522d2013-10-16 13:02:15 +000070
mtklein748ca3b2015-01-15 10:56:12 -080071/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
72
mtkleinf27f08b2015-11-03 06:54:24 -080073static double now_ms() { return SkTime::GetNSecs() * 1e-6; }
74
mtklein748ca3b2015-01-15 10:56:12 -080075SK_DECLARE_STATIC_MUTEX(gFailuresMutex);
76static SkTArray<SkString> gFailures;
77
78static void fail(ImplicitString err) {
79 SkAutoMutexAcquire lock(gFailuresMutex);
80 SkDebugf("\n\nFAILURE: %s\n\n", err.c_str());
81 gFailures.push_back(err);
halcanary9e398f72015-01-10 11:18:04 -080082}
83
mtkleinb37cb412015-03-18 05:27:14 -070084static int32_t gPending = 0; // Atomic. Total number of running and queued tasks.
85
86SK_DECLARE_STATIC_MUTEX(gRunningMutex);
87static SkTArray<SkString> gRunning;
mtklein748ca3b2015-01-15 10:56:12 -080088
mtkleinb9eb4ac2015-02-02 18:26:03 -080089static void done(double ms,
djsollen54416de2015-04-03 07:24:48 -070090 ImplicitString config, ImplicitString src, ImplicitString srcOptions,
91 ImplicitString name, ImplicitString note, ImplicitString log) {
92 SkString id = SkStringPrintf("%s %s %s %s", config.c_str(), src.c_str(),
93 srcOptions.c_str(), name.c_str());
mtkleinb37cb412015-03-18 05:27:14 -070094 {
95 SkAutoMutexAcquire lock(gRunningMutex);
96 for (int i = 0; i < gRunning.count(); i++) {
97 if (gRunning[i] == id) {
98 gRunning.removeShuffle(i);
99 break;
100 }
101 }
102 }
103 if (!FLAGS_verbose) {
104 note = "";
105 }
mtkleinb9eb4ac2015-02-02 18:26:03 -0800106 if (!log.isEmpty()) {
107 log.prepend("\n");
108 }
mtklein6dee2ad2015-02-05 09:53:44 -0800109 auto pending = sk_atomic_dec(&gPending)-1;
reed50bc0512015-05-19 14:13:31 -0700110 if (!FLAGS_quiet) {
mtklein711a2452015-07-08 08:49:20 -0700111 SkDebugf("%s(%4d/%-4dMB %6d) %s\t%s%s%s", FLAGS_verbose ? "\n" : kSkOverwriteLine
reed50bc0512015-05-19 14:13:31 -0700112 , sk_tools::getCurrResidentSetSizeMB()
113 , sk_tools::getMaxResidentSetSizeMB()
114 , pending
115 , HumanizeMs(ms).c_str()
116 , id.c_str()
117 , note.c_str()
118 , log.c_str());
119 }
mtkleina17241b2015-01-23 05:48:00 -0800120 // We write our dm.json file every once in a while in case we crash.
121 // Notice this also handles the final dm.json when pending == 0.
122 if (pending % 500 == 0) {
123 JsonWriter::DumpJson();
124 }
mtklein@google.comd36522d2013-10-16 13:02:15 +0000125}
126
djsollen54416de2015-04-03 07:24:48 -0700127static void start(ImplicitString config, ImplicitString src,
128 ImplicitString srcOptions, ImplicitString name) {
129 SkString id = SkStringPrintf("%s %s %s %s", config.c_str(), src.c_str(),
130 srcOptions.c_str(), name.c_str());
mtkleinb37cb412015-03-18 05:27:14 -0700131 SkAutoMutexAcquire lock(gRunningMutex);
132 gRunning.push_back(id);
133}
134
mtklein748ca3b2015-01-15 10:56:12 -0800135/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
mtklein114c3cd2015-01-15 10:15:02 -0800136
mtklein62bd1a62015-01-27 14:46:26 -0800137struct Gold : public SkString {
mtkleina82f5622015-02-20 12:30:19 -0800138 Gold() : SkString("") {}
djsollen54416de2015-04-03 07:24:48 -0700139 Gold(ImplicitString sink, ImplicitString src, ImplicitString srcOptions,
140 ImplicitString name, ImplicitString md5)
mtklein62bd1a62015-01-27 14:46:26 -0800141 : SkString("") {
142 this->append(sink);
143 this->append(src);
djsollen54416de2015-04-03 07:24:48 -0700144 this->append(srcOptions);
mtklein62bd1a62015-01-27 14:46:26 -0800145 this->append(name);
146 this->append(md5);
mtklein62bd1a62015-01-27 14:46:26 -0800147 }
mtkleinc8d1dd42015-10-15 12:23:01 -0700148 struct Hash {
149 uint32_t operator()(const Gold& g) const {
150 return SkGoodHash()((const SkString&)g);
151 }
152 };
mtklein62bd1a62015-01-27 14:46:26 -0800153};
mtkleina82f5622015-02-20 12:30:19 -0800154static SkTHashSet<Gold, Gold::Hash> gGold;
mtklein62bd1a62015-01-27 14:46:26 -0800155
156static void add_gold(JsonWriter::BitmapResult r) {
djsollen54416de2015-04-03 07:24:48 -0700157 gGold.add(Gold(r.config, r.sourceType, r.sourceOptions, r.name, r.md5));
mtklein62bd1a62015-01-27 14:46:26 -0800158}
159
160static void gather_gold() {
161 if (!FLAGS_readPath.isEmpty()) {
162 SkString path(FLAGS_readPath[0]);
163 path.append("/dm.json");
164 if (!JsonWriter::ReadJson(path.c_str(), add_gold)) {
165 fail(SkStringPrintf("Couldn't read %s for golden results.", path.c_str()));
166 }
167 }
168}
169
170/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
171
borenet09ed4802015-04-03 14:15:33 -0700172static SkTHashSet<SkString> gUninterestingHashes;
173
174static void gather_uninteresting_hashes() {
175 if (!FLAGS_uninterestingHashesFile.isEmpty()) {
176 SkAutoTUnref<SkData> data(SkData::NewFromFileName(FLAGS_uninterestingHashesFile[0]));
mtkleincc334b32015-09-22 11:43:53 -0700177 if (!data) {
178 SkDebugf("WARNING: unable to read uninteresting hashes from %s\n",
179 FLAGS_uninterestingHashesFile[0]);
180 return;
181 }
borenet09ed4802015-04-03 14:15:33 -0700182 SkTArray<SkString> hashes;
183 SkStrSplit((const char*)data->data(), "\n", &hashes);
184 for (const SkString& hash : hashes) {
185 gUninterestingHashes.add(hash);
186 }
187 }
188}
189
190/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
191
mtkleine0effd62015-07-29 06:37:28 -0700192struct TaggedSrc : public SkAutoTDelete<Src> {
msarett9e707a02015-09-01 14:57:57 -0700193 ImplicitString tag;
194 ImplicitString options;
mtkleine0effd62015-07-29 06:37:28 -0700195};
196
197struct TaggedSink : public SkAutoTDelete<Sink> {
198 const char* tag;
djsollen54416de2015-04-03 07:24:48 -0700199};
mtklein748ca3b2015-01-15 10:56:12 -0800200
201static const bool kMemcpyOK = true;
202
mtkleine0effd62015-07-29 06:37:28 -0700203static SkTArray<TaggedSrc, kMemcpyOK> gSrcs;
204static SkTArray<TaggedSink, kMemcpyOK> gSinks;
mtklein748ca3b2015-01-15 10:56:12 -0800205
mtklein6393c062015-04-27 08:45:01 -0700206static bool in_shard() {
207 static int N = 0;
208 return N++ % FLAGS_shards == FLAGS_shard;
209}
210
msarett9e707a02015-09-01 14:57:57 -0700211static void push_src(ImplicitString tag, ImplicitString options, Src* s) {
mtklein748ca3b2015-01-15 10:56:12 -0800212 SkAutoTDelete<Src> src(s);
mtklein6393c062015-04-27 08:45:01 -0700213 if (in_shard() &&
msarett9e707a02015-09-01 14:57:57 -0700214 FLAGS_src.contains(tag.c_str()) &&
mtklein748ca3b2015-01-15 10:56:12 -0800215 !SkCommandLineFlags::ShouldSkip(FLAGS_match, src->name().c_str())) {
mtkleine0effd62015-07-29 06:37:28 -0700216 TaggedSrc& s = gSrcs.push_back();
mtklein748ca3b2015-01-15 10:56:12 -0800217 s.reset(src.detach());
218 s.tag = tag;
djsollen54416de2015-04-03 07:24:48 -0700219 s.options = options;
mtklein114c3cd2015-01-15 10:15:02 -0800220 }
mtklein748ca3b2015-01-15 10:56:12 -0800221}
mtklein114c3cd2015-01-15 10:15:02 -0800222
msarett9e707a02015-09-01 14:57:57 -0700223static void push_codec_src(Path path, CodecSrc::Mode mode, CodecSrc::DstColorType dstColorType,
224 float scale) {
225 SkString folder;
226 switch (mode) {
227 case CodecSrc::kCodec_Mode:
228 folder.append("codec");
229 break;
msarett9e707a02015-09-01 14:57:57 -0700230 case CodecSrc::kScanline_Mode:
231 folder.append("scanline");
232 break;
233 case CodecSrc::kScanline_Subset_Mode:
234 folder.append("scanline_subset");
235 break;
236 case CodecSrc::kStripe_Mode:
237 folder.append("stripe");
238 break;
239 case CodecSrc::kSubset_Mode:
msarett36c37962015-09-02 13:20:52 -0700240 folder.append("codec_subset");
msarett9e707a02015-09-01 14:57:57 -0700241 break;
242 }
243
244 switch (dstColorType) {
245 case CodecSrc::kGrayscale_Always_DstColorType:
246 folder.append("_kGray8");
247 break;
248 case CodecSrc::kIndex8_Always_DstColorType:
249 folder.append("_kIndex8");
250 break;
251 default:
252 break;
253 }
254
255 if (1.0f != scale) {
256 folder.appendf("_%.3f", scale);
257 }
258
259 CodecSrc* src = new CodecSrc(path, mode, dstColorType, scale);
260 push_src("image", folder, src);
261}
262
msarett3d9d7a72015-10-21 10:27:10 -0700263static void push_android_codec_src(Path path, AndroidCodecSrc::Mode mode,
264 CodecSrc::DstColorType dstColorType, int sampleSize) {
265 SkString folder;
266 switch (mode) {
267 case AndroidCodecSrc::kFullImage_Mode:
268 folder.append("scaled_codec");
269 break;
270 case AndroidCodecSrc::kDivisor_Mode:
271 folder.append("scaled_codec_divisor");
272 break;
273 }
274
275 switch (dstColorType) {
276 case CodecSrc::kGrayscale_Always_DstColorType:
277 folder.append("_kGray8");
278 break;
279 case CodecSrc::kIndex8_Always_DstColorType:
280 folder.append("_kIndex8");
281 break;
282 default:
283 break;
284 }
285
286 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800287 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700288 }
289
290 AndroidCodecSrc* src = new AndroidCodecSrc(path, mode, dstColorType, sampleSize);
291 push_src("image", folder, src);
292}
293
msarett438b2ad2015-04-09 12:43:10 -0700294static void push_codec_srcs(Path path) {
295 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
296 if (!encoded) {
297 SkDebugf("Couldn't read %s.", path.c_str());
298 return;
299 }
300 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
halcanary96fcdcc2015-08-27 07:41:13 -0700301 if (nullptr == codec.get()) {
msarett438b2ad2015-04-09 12:43:10 -0700302 SkDebugf("Couldn't create codec for %s.", path.c_str());
303 return;
304 }
305
msarett36c37962015-09-02 13:20:52 -0700306 // Native Scales
msarett0a242972015-06-11 14:27:27 -0700307 // TODO (msarett): Implement scaling tests for SkImageDecoder in order to compare with these
308 // tests. SkImageDecoder supports downscales by integer factors.
emmaleer8f4ba762015-08-14 07:44:46 -0700309 // 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 -0700310 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 -0700311
msarett36c37962015-09-02 13:20:52 -0700312 const CodecSrc::Mode nativeModes[] = { CodecSrc::kCodec_Mode, CodecSrc::kScanline_Mode,
313 CodecSrc::kScanline_Subset_Mode, CodecSrc::kStripe_Mode, CodecSrc::kSubset_Mode };
msarett9e707a02015-09-01 14:57:57 -0700314
msarett36c37962015-09-02 13:20:52 -0700315 CodecSrc::DstColorType colorTypes[3];
316 uint32_t numColorTypes;
317 switch (codec->getInfo().colorType()) {
318 case kGray_8_SkColorType:
319 // FIXME: Is this a long term solution for testing wbmps decodes to kIndex8?
halcanary6950de62015-11-07 05:29:00 -0800320 // Further discussion on this topic is at https://bug.skia.org/3683 .
msarett3d9d7a72015-10-21 10:27:10 -0700321 // This causes us to try to convert grayscale jpegs to kIndex8. We currently
322 // fail non-fatally in this case.
msarett36c37962015-09-02 13:20:52 -0700323 colorTypes[0] = CodecSrc::kGetFromCanvas_DstColorType;
324 colorTypes[1] = CodecSrc::kGrayscale_Always_DstColorType;
325 colorTypes[2] = CodecSrc::kIndex8_Always_DstColorType;
326 numColorTypes = 3;
327 break;
328 case kIndex_8_SkColorType:
329 colorTypes[0] = CodecSrc::kGetFromCanvas_DstColorType;
330 colorTypes[1] = CodecSrc::kIndex8_Always_DstColorType;
331 numColorTypes = 2;
332 break;
333 default:
334 colorTypes[0] = CodecSrc::kGetFromCanvas_DstColorType;
335 numColorTypes = 1;
336 break;
337 }
msarett9e707a02015-09-01 14:57:57 -0700338
msarett36c37962015-09-02 13:20:52 -0700339 for (float scale : nativeScales) {
msarett36c37962015-09-02 13:20:52 -0700340 for (CodecSrc::Mode mode : nativeModes) {
341 for (uint32_t i = 0; i < numColorTypes; i++) {
342 push_codec_src(path, mode, colorTypes[i], scale);
msarett9e707a02015-09-01 14:57:57 -0700343 }
344 }
msarett0a242972015-06-11 14:27:27 -0700345 }
msarett36c37962015-09-02 13:20:52 -0700346
halcanary6950de62015-11-07 05:29:00 -0800347 // https://bug.skia.org/4428
msarett3d9d7a72015-10-21 10:27:10 -0700348 static const char* const exts[] = {
349 "jpg", "jpeg", "png", "webp",
350 "JPG", "JPEG", "PNG", "WEBP",
351 };
352 bool supported = false;
353 for (const char* ext : exts) {
354 if (path.endsWith(ext)) {
355 supported = true;
356 break;
357 }
358 }
359 if (!supported) {
scroggo46c57472015-09-30 08:57:13 -0700360 return;
361 }
362
msarett3d9d7a72015-10-21 10:27:10 -0700363 const int sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
msarett36c37962015-09-02 13:20:52 -0700364
msarett3d9d7a72015-10-21 10:27:10 -0700365 const AndroidCodecSrc::Mode androidModes[] = {
366 AndroidCodecSrc::kFullImage_Mode,
367 AndroidCodecSrc::kDivisor_Mode,
368 };
369
370 for (int sampleSize : sampleSizes) {
371 for (AndroidCodecSrc::Mode mode : androidModes) {
372 for (uint32_t i = 0; i < numColorTypes; i++) {
373 push_android_codec_src(path, mode, colorTypes[i], sampleSize);
374 }
msarett36c37962015-09-02 13:20:52 -0700375 }
376 }
msarett438b2ad2015-04-09 12:43:10 -0700377}
378
msarett5cb48852015-11-06 08:56:32 -0800379static bool brd_color_type_supported(SkBitmapRegionDecoder::Strategy strategy,
msaretta5783ae2015-09-08 15:35:32 -0700380 CodecSrc::DstColorType dstColorType) {
381 switch (strategy) {
msarett5cb48852015-11-06 08:56:32 -0800382 case SkBitmapRegionDecoder::kCanvas_Strategy:
msaretta5783ae2015-09-08 15:35:32 -0700383 if (CodecSrc::kGetFromCanvas_DstColorType == dstColorType) {
384 return true;
385 }
386 return false;
msarett5cb48852015-11-06 08:56:32 -0800387 case SkBitmapRegionDecoder::kAndroidCodec_Strategy:
msarett26ad17b2015-10-22 07:29:19 -0700388 switch (dstColorType) {
389 case CodecSrc::kGetFromCanvas_DstColorType:
390 case CodecSrc::kIndex8_Always_DstColorType:
391 case CodecSrc::kGrayscale_Always_DstColorType:
392 return true;
393 default:
394 return false;
395 }
msaretta5783ae2015-09-08 15:35:32 -0700396 default:
397 SkASSERT(false);
398 return false;
399 }
400}
401
msarett5cb48852015-11-06 08:56:32 -0800402static void push_brd_src(Path path, SkBitmapRegionDecoder::Strategy strategy,
msaretta5783ae2015-09-08 15:35:32 -0700403 CodecSrc::DstColorType dstColorType, BRDSrc::Mode mode, uint32_t sampleSize) {
404 SkString folder;
405 switch (strategy) {
msarett5cb48852015-11-06 08:56:32 -0800406 case SkBitmapRegionDecoder::kCanvas_Strategy:
msaretta5783ae2015-09-08 15:35:32 -0700407 folder.append("brd_canvas");
408 break;
msarett5cb48852015-11-06 08:56:32 -0800409 case SkBitmapRegionDecoder::kAndroidCodec_Strategy:
msarett26ad17b2015-10-22 07:29:19 -0700410 folder.append("brd_android_codec");
411 break;
msaretta5783ae2015-09-08 15:35:32 -0700412 default:
413 SkASSERT(false);
414 return;
415 }
416
417 switch (mode) {
418 case BRDSrc::kFullImage_Mode:
419 break;
420 case BRDSrc::kDivisor_Mode:
421 folder.append("_divisor");
422 break;
423 default:
424 SkASSERT(false);
425 return;
426 }
427
428 switch (dstColorType) {
429 case CodecSrc::kGetFromCanvas_DstColorType:
430 break;
431 case CodecSrc::kIndex8_Always_DstColorType:
432 folder.append("_kIndex");
433 break;
434 case CodecSrc::kGrayscale_Always_DstColorType:
435 folder.append("_kGray");
436 break;
437 default:
438 SkASSERT(false);
439 return;
440 }
441
442 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800443 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700444 }
445
446 BRDSrc* src = new BRDSrc(path, strategy, mode, dstColorType, sampleSize);
447 push_src("image", folder, src);
448}
449
450static void push_brd_srcs(Path path) {
451
msarett5cb48852015-11-06 08:56:32 -0800452 const SkBitmapRegionDecoder::Strategy strategies[] = {
453 SkBitmapRegionDecoder::kCanvas_Strategy,
msarett5cb48852015-11-06 08:56:32 -0800454 SkBitmapRegionDecoder::kAndroidCodec_Strategy,
msaretta5783ae2015-09-08 15:35:32 -0700455 };
456
scroggo501b7342015-11-03 07:55:11 -0800457 // Test on a variety of sampleSizes, making sure to include:
458 // - 2, 4, and 8, which are natively supported by jpeg
459 // - multiples of 2 which are not divisible by 4 (analogous for 4)
460 // - larger powers of two, since BRD clients generally use powers of 2
461 // We will only produce output for the larger sizes on large images.
462 const uint32_t sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 24, 32, 64 };
msarett6efbe052015-09-11 09:01:16 -0700463
msaretta5783ae2015-09-08 15:35:32 -0700464 // We will only test to one backend (8888), but we will test all of the
465 // color types that we need to decode to on this backend.
466 const CodecSrc::DstColorType dstColorTypes[] = {
msarett26ad17b2015-10-22 07:29:19 -0700467 CodecSrc::kGetFromCanvas_DstColorType,
468 CodecSrc::kIndex8_Always_DstColorType,
469 CodecSrc::kGrayscale_Always_DstColorType,
msaretta5783ae2015-09-08 15:35:32 -0700470 };
471
472 const BRDSrc::Mode modes[] = {
msarett26ad17b2015-10-22 07:29:19 -0700473 BRDSrc::kFullImage_Mode,
474 BRDSrc::kDivisor_Mode,
msaretta5783ae2015-09-08 15:35:32 -0700475 };
476
msarett5cb48852015-11-06 08:56:32 -0800477 for (SkBitmapRegionDecoder::Strategy strategy : strategies) {
msarett6efbe052015-09-11 09:01:16 -0700478 for (uint32_t sampleSize : sampleSizes) {
msarett6efbe052015-09-11 09:01:16 -0700479 for (CodecSrc::DstColorType dstColorType : dstColorTypes) {
480 if (brd_color_type_supported(strategy, dstColorType)) {
481 for (BRDSrc::Mode mode : modes) {
msaretta5783ae2015-09-08 15:35:32 -0700482 push_brd_src(path, strategy, dstColorType, mode, sampleSize);
483 }
484 }
485 }
486 }
487 }
488}
489
490static bool brd_supported(const char* ext) {
msarett8c8f22a2015-04-01 06:58:48 -0700491 static const char* const exts[] = {
msaretta5783ae2015-09-08 15:35:32 -0700492 "jpg", "jpeg", "png", "webp",
493 "JPG", "JPEG", "PNG", "WEBP",
msarett8c8f22a2015-04-01 06:58:48 -0700494 };
495
496 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
497 if (0 == strcmp(exts[i], ext)) {
498 return true;
499 }
500 }
501 return false;
scroggo9b77ddd2015-03-19 06:03:39 -0700502}
503
mtklein748ca3b2015-01-15 10:56:12 -0800504static void gather_srcs() {
505 for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) {
djsollen54416de2015-04-03 07:24:48 -0700506 push_src("gm", "", new GMSrc(r->factory()));
mtklein748ca3b2015-01-15 10:56:12 -0800507 }
halcanaryfc37ad12015-01-30 07:31:19 -0800508 for (int i = 0; i < FLAGS_skps.count(); i++) {
509 const char* path = FLAGS_skps[i];
510 if (sk_isdir(path)) {
511 SkOSFile::Iter it(path, "skp");
512 for (SkString file; it.next(&file); ) {
djsollen54416de2015-04-03 07:24:48 -0700513 push_src("skp", "", new SKPSrc(SkOSPath::Join(path, file.c_str())));
halcanaryfc37ad12015-01-30 07:31:19 -0800514 }
515 } else {
djsollen54416de2015-04-03 07:24:48 -0700516 push_src("skp", "", new SKPSrc(path));
mtklein114c3cd2015-01-15 10:15:02 -0800517 }
518 }
halcanary23b03c32015-01-30 09:58:58 -0800519 static const char* const exts[] = {
520 "bmp", "gif", "jpg", "jpeg", "png", "webp", "ktx", "astc", "wbmp", "ico",
521 "BMP", "GIF", "JPG", "JPEG", "PNG", "WEBP", "KTX", "ASTC", "WBMP", "ICO",
522 };
523 for (int i = 0; i < FLAGS_images.count(); i++) {
524 const char* flag = FLAGS_images[i];
525 if (sk_isdir(flag)) {
526 for (size_t j = 0; j < SK_ARRAY_COUNT(exts); j++) {
527 SkOSFile::Iter it(flag, exts[j]);
528 for (SkString file; it.next(&file); ) {
529 SkString path = SkOSPath::Join(flag, file.c_str());
djsollen54416de2015-04-03 07:24:48 -0700530 push_src("image", "decode", new ImageSrc(path)); // Decode entire image
msaretta5783ae2015-09-08 15:35:32 -0700531 push_codec_srcs(path);
532 if (brd_supported(exts[j])) {
533 push_brd_srcs(path);
scroggo9b77ddd2015-03-19 06:03:39 -0700534 }
halcanary23b03c32015-01-30 09:58:58 -0800535 }
mtklein114c3cd2015-01-15 10:15:02 -0800536 }
halcanary23b03c32015-01-30 09:58:58 -0800537 } else if (sk_exists(flag)) {
538 // assume that FLAGS_images[i] is a valid image if it is a file.
djsollen54416de2015-04-03 07:24:48 -0700539 push_src("image", "decode", new ImageSrc(flag)); // Decode entire image.
msarett438b2ad2015-04-09 12:43:10 -0700540 push_codec_srcs(flag);
msaretta5783ae2015-09-08 15:35:32 -0700541 push_brd_srcs(flag);
mtklein709d2c32015-01-15 08:30:25 -0800542 }
mtklein709d2c32015-01-15 08:30:25 -0800543 }
544}
545
mtklein748ca3b2015-01-15 10:56:12 -0800546static GrGLStandard get_gpu_api() {
547 if (FLAGS_gpuAPI.contains("gl")) { return kGL_GrGLStandard; }
548 if (FLAGS_gpuAPI.contains("gles")) { return kGLES_GrGLStandard; }
549 return kNone_GrGLStandard;
mtklein709d2c32015-01-15 08:30:25 -0800550}
551
mtklein748ca3b2015-01-15 10:56:12 -0800552static void push_sink(const char* tag, Sink* s) {
553 SkAutoTDelete<Sink> sink(s);
554 if (!FLAGS_config.contains(tag)) {
555 return;
mtklein114c3cd2015-01-15 10:15:02 -0800556 }
mtkleine0effd62015-07-29 06:37:28 -0700557 // Try a simple Src as a canary. If it fails, skip this sink.
mtklein748ca3b2015-01-15 10:56:12 -0800558 struct : public Src {
mtkleine0effd62015-07-29 06:37:28 -0700559 Error draw(SkCanvas* c) const override {
560 c->drawRect(SkRect::MakeWH(1,1), SkPaint());
561 return "";
562 }
mtklein36352bf2015-03-25 18:17:31 -0700563 SkISize size() const override { return SkISize::Make(16, 16); }
mtkleine0effd62015-07-29 06:37:28 -0700564 Name name() const override { return "justOneRect"; }
565 } justOneRect;
mtklein114c3cd2015-01-15 10:15:02 -0800566
mtklein748ca3b2015-01-15 10:56:12 -0800567 SkBitmap bitmap;
568 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800569 SkString log;
mtkleine0effd62015-07-29 06:37:28 -0700570 Error err = sink->draw(justOneRect, &bitmap, &stream, &log);
mtklein4089ef72015-03-05 08:40:28 -0800571 if (err.isFatal()) {
mtklein05641a52015-04-21 10:49:13 -0700572 SkDebugf("Could not run %s: %s\n", tag, err.c_str());
573 exit(1);
mtklein114c3cd2015-01-15 10:15:02 -0800574 }
575
mtkleine0effd62015-07-29 06:37:28 -0700576 TaggedSink& ts = gSinks.push_back();
mtklein748ca3b2015-01-15 10:56:12 -0800577 ts.reset(sink.detach());
578 ts.tag = tag;
579}
580
581static bool gpu_supported() {
582#if SK_SUPPORT_GPU
583 return FLAGS_gpu;
584#else
585 return false;
586#endif
587}
588
589static Sink* create_sink(const char* tag) {
590#define SINK(t, sink, ...) if (0 == strcmp(t, tag)) { return new sink(__VA_ARGS__); }
591 if (gpu_supported()) {
mtklein82d28432015-01-15 12:46:02 -0800592 typedef GrContextFactory Gr;
mtklein748ca3b2015-01-15 10:56:12 -0800593 const GrGLStandard api = get_gpu_api();
hendrikw885bf092015-08-27 10:38:39 -0700594 SINK("gpunull", GPUSink, Gr::kNull_GLContextType, api, 0, false, FLAGS_gpu_threading);
595 SINK("gpudebug", GPUSink, Gr::kDebug_GLContextType, api, 0, false, FLAGS_gpu_threading);
596 SINK("gpu", GPUSink, Gr::kNative_GLContextType, api, 0, false, FLAGS_gpu_threading);
597 SINK("gpudft", GPUSink, Gr::kNative_GLContextType, api, 0, true, FLAGS_gpu_threading);
598 SINK("msaa4", GPUSink, Gr::kNative_GLContextType, api, 4, false, FLAGS_gpu_threading);
599 SINK("msaa16", GPUSink, Gr::kNative_GLContextType, api, 16, false, FLAGS_gpu_threading);
bsalomon90e8ab72015-09-08 10:26:51 -0700600 SINK("nvprmsaa4", GPUSink, Gr::kNVPR_GLContextType, api, 4, true, FLAGS_gpu_threading);
601 SINK("nvprmsaa16", GPUSink, Gr::kNVPR_GLContextType, api, 16, true, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800602 #if SK_ANGLE
hendrikw885bf092015-08-27 10:38:39 -0700603 SINK("angle", GPUSink, Gr::kANGLE_GLContextType, api, 0, false, FLAGS_gpu_threading);
hendrikweddbefb2015-09-11 13:07:29 -0700604 SINK("angle-gl", GPUSink, Gr::kANGLE_GL_GLContextType, api, 0, false, FLAGS_gpu_threading);
hendrikw885bf092015-08-27 10:38:39 -0700605 #endif
606 #if SK_COMMAND_BUFFER
607 SINK("commandbuffer", GPUSink, Gr::kCommandBuffer_GLContextType, api, 0, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800608 #endif
609 #if SK_MESA
hendrikw885bf092015-08-27 10:38:39 -0700610 SINK("mesa", GPUSink, Gr::kMESA_GLContextType, api, 0, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800611 #endif
mtklein114c3cd2015-01-15 10:15:02 -0800612 }
mtklein748ca3b2015-01-15 10:56:12 -0800613
tomhudsoneebc39a2015-02-23 12:18:05 -0800614#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
615 SINK("hwui", HWUISink);
616#endif
617
mtklein748ca3b2015-01-15 10:56:12 -0800618 if (FLAGS_cpu) {
619 SINK("565", RasterSink, kRGB_565_SkColorType);
620 SINK("8888", RasterSink, kN32_SkColorType);
halcanaryc11c62f2015-09-28 11:51:54 -0700621 SINK("pdf", PDFSink, "Pdfium");
622 SINK("pdf_poppler", PDFSink, "Poppler");
mtklein9c3f17d2015-01-28 11:35:18 -0800623 SINK("skp", SKPSink);
mtklein8a4527e2015-01-31 20:00:58 -0800624 SINK("svg", SVGSink);
625 SINK("null", NullSink);
halcanary47ef4d52015-03-03 09:13:09 -0800626 SINK("xps", XPSSink);
mtklein748ca3b2015-01-15 10:56:12 -0800627 }
628#undef SINK
halcanary96fcdcc2015-08-27 07:41:13 -0700629 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800630}
631
mtklein748ca3b2015-01-15 10:56:12 -0800632static Sink* create_via(const char* tag, Sink* wrapped) {
633#define VIA(t, via, ...) if (0 == strcmp(t, tag)) { return new via(__VA_ARGS__); }
halcanary96fcdcc2015-08-27 07:41:13 -0700634 VIA("twice", ViaTwice, wrapped);
635 VIA("pipe", ViaPipe, wrapped);
636 VIA("serialize", ViaSerialization, wrapped);
637 VIA("2ndpic", ViaSecondPicture, wrapped);
mtkleind31c13d2015-05-05 12:59:56 -0700638 VIA("sp", ViaSingletonPictures, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700639 VIA("tiles", ViaTiles, 256, 256, nullptr, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800640 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
mtklein2e2ea382015-10-16 10:29:41 -0700641 VIA("remote", ViaRemote, false, wrapped);
642 VIA("remote_cache", ViaRemote, true, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800643
mtkleind603b222015-02-17 11:13:33 -0800644 if (FLAGS_matrix.count() == 4) {
mtklein748ca3b2015-01-15 10:56:12 -0800645 SkMatrix m;
mtkleind603b222015-02-17 11:13:33 -0800646 m.reset();
647 m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
648 m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
649 m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
650 m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
651 VIA("matrix", ViaMatrix, m, wrapped);
652 VIA("upright", ViaUpright, m, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800653 }
tomhudson64de1e12015-03-05 08:01:07 -0800654
655#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
656 VIA("androidsdk", ViaAndroidSDK, wrapped);
657#endif
658
mtklein748ca3b2015-01-15 10:56:12 -0800659#undef VIA
halcanary96fcdcc2015-08-27 07:41:13 -0700660 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800661}
662
mtklein748ca3b2015-01-15 10:56:12 -0800663static void gather_sinks() {
664 for (int i = 0; i < FLAGS_config.count(); i++) {
665 const char* config = FLAGS_config[i];
666 SkTArray<SkString> parts;
667 SkStrSplit(config, "-", &parts);
668
halcanary96fcdcc2015-08-27 07:41:13 -0700669 Sink* sink = nullptr;
mtklein748ca3b2015-01-15 10:56:12 -0800670 for (int i = parts.count(); i-- > 0;) {
671 const char* part = parts[i].c_str();
halcanary96fcdcc2015-08-27 07:41:13 -0700672 Sink* next = (sink == nullptr) ? create_sink(part) : create_via(part, sink);
673 if (next == nullptr) {
mtklein748ca3b2015-01-15 10:56:12 -0800674 SkDebugf("Skipping %s: Don't understand '%s'.\n", config, part);
675 delete sink;
halcanary96fcdcc2015-08-27 07:41:13 -0700676 sink = nullptr;
mtklein748ca3b2015-01-15 10:56:12 -0800677 break;
678 }
679 sink = next;
680 }
681 if (sink) {
682 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800683 }
684 }
685}
mtklein709d2c32015-01-15 08:30:25 -0800686
mtkleina5114d72015-08-24 13:27:01 -0700687static bool dump_png(SkBitmap bitmap, const char* path, const char* md5) {
688 const int w = bitmap.width(),
689 h = bitmap.height();
690
691 // First get the bitmap into N32 color format. The next step will work only there.
692 if (bitmap.colorType() != kN32_SkColorType) {
693 SkBitmap n32;
694 if (!bitmap.copyTo(&n32, kN32_SkColorType)) {
695 return false;
696 }
697 bitmap = n32;
698 }
699
700 // Convert our N32 bitmap into unpremul RGBA for libpng.
701 SkAutoTMalloc<uint32_t> rgba(w*h);
702 if (!bitmap.readPixels(SkImageInfo::Make(w,h, kRGBA_8888_SkColorType, kUnpremul_SkAlphaType),
703 rgba, 4*w, 0,0)) {
704 return false;
705 }
706
707 // We don't need bitmap anymore. Might as well drop our ref.
mtkleinfccb77d2015-08-24 14:13:29 -0700708 bitmap.reset();
mtkleina5114d72015-08-24 13:27:01 -0700709
mtkleinc64137c2015-08-25 10:56:08 -0700710 FILE* f = fopen(path, "wb");
mtkleina5114d72015-08-24 13:27:01 -0700711 if (!f) { return false; }
712
713 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
714 if (!png) {
715 fclose(f);
716 return false;
717 }
718
719 png_infop info = png_create_info_struct(png);
720 if (!info) {
721 png_destroy_write_struct(&png, &info);
722 fclose(f);
723 return false;
724 }
725
mtkleind69ece62015-09-10 10:37:44 -0700726 SkString description;
727 description.append("Key: ");
728 for (int i = 0; i < FLAGS_key.count(); i++) {
729 description.appendf("%s ", FLAGS_key[i]);
730 }
731 description.append("Properties: ");
732 for (int i = 0; i < FLAGS_properties.count(); i++) {
733 description.appendf("%s ", FLAGS_properties[i]);
734 }
735 description.appendf("MD5: %s", md5);
736
mtkleina5114d72015-08-24 13:27:01 -0700737 png_text text[2];
738 text[0].key = (png_charp)"Author";
739 text[0].text = (png_charp)"DM dump_png()";
740 text[0].compression = PNG_TEXT_COMPRESSION_NONE;
741 text[1].key = (png_charp)"Description";
mtkleind69ece62015-09-10 10:37:44 -0700742 text[1].text = (png_charp)description.c_str();
mtkleina5114d72015-08-24 13:27:01 -0700743 text[1].compression = PNG_TEXT_COMPRESSION_NONE;
744 png_set_text(png, info, text, 2);
745
746 png_init_io(png, f);
747 png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8,
748 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
749 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
750 png_write_info(png, info);
751 for (int j = 0; j < h; j++) {
752 png_bytep row = (png_bytep)(rgba.get() + w*j);
753 png_write_rows(png, &row, 1);
754 }
755 png_write_end(png, info);
756
757 png_destroy_write_struct(&png, &info);
758 fclose(f);
759 return true;
760}
761
mtkleina2ef6422015-01-15 13:44:22 -0800762static bool match(const char* needle, const char* haystack) {
halcanary96fcdcc2015-08-27 07:41:13 -0700763 return 0 == strcmp("_", needle) || nullptr != strstr(haystack, needle);
mtkleina2ef6422015-01-15 13:44:22 -0800764}
765
djsollen54416de2015-04-03 07:24:48 -0700766static ImplicitString is_blacklisted(const char* sink, const char* src,
767 const char* srcOptions, const char* name) {
mtklein0d243ff2015-04-03 07:51:00 -0700768 for (int i = 0; i < FLAGS_blacklist.count() - 3; i += 4) {
mtkleina2ef6422015-01-15 13:44:22 -0800769 if (match(FLAGS_blacklist[i+0], sink) &&
djsollen54416de2015-04-03 07:24:48 -0700770 match(FLAGS_blacklist[i+1], src) &&
771 match(FLAGS_blacklist[i+2], srcOptions) &&
772 match(FLAGS_blacklist[i+3], name)) {
773 return SkStringPrintf("%s %s %s %s",
774 FLAGS_blacklist[i+0], FLAGS_blacklist[i+1],
775 FLAGS_blacklist[i+2], FLAGS_blacklist[i+3]);
mtkleina2ef6422015-01-15 13:44:22 -0800776 }
777 }
778 return "";
779}
780
mtklein748ca3b2015-01-15 10:56:12 -0800781// The finest-grained unit of work we can run: draw a single Src into a single Sink,
782// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
783struct Task {
mtkleine0effd62015-07-29 06:37:28 -0700784 Task(const TaggedSrc& src, const TaggedSink& sink) : src(src), sink(sink) {}
785 const TaggedSrc& src;
786 const TaggedSink& sink;
mtklein748ca3b2015-01-15 10:56:12 -0800787
788 static void Run(Task* task) {
mtkleina2ef6422015-01-15 13:44:22 -0800789 SkString name = task->src->name();
mtkleine0effd62015-07-29 06:37:28 -0700790
791 // We'll skip drawing this Src/Sink pair if:
792 // - the Src vetoes the Sink;
793 // - this Src / Sink combination is on the blacklist;
794 // - it's a dry run.
mtklein99cab4e2015-07-31 06:43:04 -0700795 SkString note(task->src->veto(task->sink->flags()) ? " (veto)" : "");
msarett9e707a02015-09-01 14:57:57 -0700796 SkString whyBlacklisted = is_blacklisted(task->sink.tag, task->src.tag.c_str(),
797 task->src.options.c_str(), name.c_str());
mtkleinb37cb412015-03-18 05:27:14 -0700798 if (!whyBlacklisted.isEmpty()) {
799 note.appendf(" (--blacklist %s)", whyBlacklisted.c_str());
800 }
mtkleine0effd62015-07-29 06:37:28 -0700801
mtkleinb9eb4ac2015-02-02 18:26:03 -0800802 SkString log;
mtkleinf27f08b2015-11-03 06:54:24 -0800803 auto timerStart = now_ms();
mtkleine0effd62015-07-29 06:37:28 -0700804 if (!FLAGS_dryRun && note.isEmpty()) {
mtklein748ca3b2015-01-15 10:56:12 -0800805 SkBitmap bitmap;
806 SkDynamicMemoryWStream stream;
bsalomon821e10e2015-06-04 14:15:33 -0700807 if (FLAGS_pre_log) {
808 SkDebugf("\nRunning %s->%s", name.c_str(), task->sink.tag);
809 }
djsollen54416de2015-04-03 07:24:48 -0700810 start(task->sink.tag, task->src.tag, task->src.options, name.c_str());
mtkleinb9eb4ac2015-02-02 18:26:03 -0800811 Error err = task->sink->draw(*task->src, &bitmap, &stream, &log);
mtklein748ca3b2015-01-15 10:56:12 -0800812 if (!err.isEmpty()) {
mtkleinf27f08b2015-11-03 06:54:24 -0800813 auto elapsed = now_ms() - timerStart;
mtklein4089ef72015-03-05 08:40:28 -0800814 if (err.isFatal()) {
djsollen54416de2015-04-03 07:24:48 -0700815 fail(SkStringPrintf("%s %s %s %s: %s",
mtklein4089ef72015-03-05 08:40:28 -0800816 task->sink.tag,
msarett9e707a02015-09-01 14:57:57 -0700817 task->src.tag.c_str(),
818 task->src.options.c_str(),
mtklein4089ef72015-03-05 08:40:28 -0800819 name.c_str(),
820 err.c_str()));
mtkleinb37cb412015-03-18 05:27:14 -0700821 } else {
822 note.appendf(" (skipped: %s)", err.c_str());
mtklein4089ef72015-03-05 08:40:28 -0800823 }
mtkleinf27f08b2015-11-03 06:54:24 -0800824 done(elapsed, task->sink.tag, task->src.tag, task->src.options,
djsollen54416de2015-04-03 07:24:48 -0700825 name, note, log);
mtklein4089ef72015-03-05 08:40:28 -0800826 return;
mtklein748ca3b2015-01-15 10:56:12 -0800827 }
mtklein62bd1a62015-01-27 14:46:26 -0800828 SkAutoTDelete<SkStreamAsset> data(stream.detachAsStream());
829
830 SkString md5;
831 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
832 SkMD5 hash;
833 if (data->getLength()) {
834 hash.writeStream(data, data->getLength());
835 data->rewind();
836 } else {
mtklein38408462015-07-08 07:25:27 -0700837 // If we're BGRA (Linux, Windows), swizzle over to RGBA (Mac, Android).
838 // This helps eliminate multiple 0-pixel-diff hashes on gold.skia.org.
839 // (Android's general slow speed breaks the tie arbitrarily in RGBA's favor.)
840 // We might consider promoting 565 to RGBA too.
841 if (bitmap.colorType() == kBGRA_8888_SkColorType) {
842 SkBitmap swizzle;
843 SkAssertResult(bitmap.copyTo(&swizzle, kRGBA_8888_SkColorType));
844 hash.write(swizzle.getPixels(), swizzle.getSize());
845 } else {
846 hash.write(bitmap.getPixels(), bitmap.getSize());
847 }
mtklein62bd1a62015-01-27 14:46:26 -0800848 }
849 SkMD5::Digest digest;
850 hash.finish(digest);
851 for (int i = 0; i < 16; i++) {
852 md5.appendf("%02x", digest.data[i]);
853 }
854 }
855
856 if (!FLAGS_readPath.isEmpty() &&
msarett9e707a02015-09-01 14:57:57 -0700857 !gGold.contains(Gold(task->sink.tag, task->src.tag.c_str(),
858 task->src.options.c_str(), name, md5))) {
djsollen54416de2015-04-03 07:24:48 -0700859 fail(SkStringPrintf("%s not found for %s %s %s %s in %s",
mtklein62bd1a62015-01-27 14:46:26 -0800860 md5.c_str(),
861 task->sink.tag,
msarett9e707a02015-09-01 14:57:57 -0700862 task->src.tag.c_str(),
863 task->src.options.c_str(),
mtklein62bd1a62015-01-27 14:46:26 -0800864 name.c_str(),
865 FLAGS_readPath[0]));
866 }
867
mtkleinb0531a72015-04-07 13:38:48 -0700868 if (!FLAGS_writePath.isEmpty()) {
mtklein748ca3b2015-01-15 10:56:12 -0800869 const char* ext = task->sink->fileExtension();
mtklein62bd1a62015-01-27 14:46:26 -0800870 if (data->getLength()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700871 WriteToDisk(*task, md5, ext, data, data->getLength(), nullptr);
halcanary022afb82015-01-30 11:00:12 -0800872 SkASSERT(bitmap.drawsNothing());
873 } else if (!bitmap.drawsNothing()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700874 WriteToDisk(*task, md5, ext, nullptr, 0, &bitmap);
mtklein748ca3b2015-01-15 10:56:12 -0800875 }
876 }
877 }
mtkleinf27f08b2015-11-03 06:54:24 -0800878 done(now_ms()-timerStart, task->sink.tag, task->src.tag.c_str(), task->src.options.c_str(),
879 name, note, log);
mtklein748ca3b2015-01-15 10:56:12 -0800880 }
881
882 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -0800883 SkString md5,
884 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -0800885 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -0800886 const SkBitmap* bitmap) {
mtklein748ca3b2015-01-15 10:56:12 -0800887 JsonWriter::BitmapResult result;
djsollen54416de2015-04-03 07:24:48 -0700888 result.name = task.src->name();
889 result.config = task.sink.tag;
890 result.sourceType = task.src.tag;
891 result.sourceOptions = task.src.options;
892 result.ext = ext;
893 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -0800894 JsonWriter::AddBitmapResult(result);
895
mtkleinb0531a72015-04-07 13:38:48 -0700896 // If an MD5 is uninteresting, we want it noted in the JSON file,
897 // but don't want to dump it out as a .png (or whatever ext is).
898 if (gUninterestingHashes.contains(md5)) {
899 return;
900 }
901
mtklein748ca3b2015-01-15 10:56:12 -0800902 const char* dir = FLAGS_writePath[0];
903 if (0 == strcmp(dir, "@")) { // Needed for iOS.
904 dir = FLAGS_resourcePath[0];
905 }
906 sk_mkdir(dir);
907
908 SkString path;
909 if (FLAGS_nameByHash) {
910 path = SkOSPath::Join(dir, result.md5.c_str());
911 path.append(".");
912 path.append(ext);
913 if (sk_exists(path.c_str())) {
914 return; // Content-addressed. If it exists already, we're done.
915 }
916 } else {
917 path = SkOSPath::Join(dir, task.sink.tag);
918 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -0700919 path = SkOSPath::Join(path.c_str(), task.src.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800920 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -0700921 if (strcmp(task.src.options.c_str(), "") != 0) {
922 path = SkOSPath::Join(path.c_str(), task.src.options.c_str());
djsollen54416de2015-04-03 07:24:48 -0700923 sk_mkdir(path.c_str());
924 }
mtklein748ca3b2015-01-15 10:56:12 -0800925 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
926 path.append(".");
927 path.append(ext);
928 }
929
mtklein748ca3b2015-01-15 10:56:12 -0800930 if (bitmap) {
mtkleina5114d72015-08-24 13:27:01 -0700931 if (!dump_png(*bitmap, path.c_str(), result.md5.c_str())) {
mtklein748ca3b2015-01-15 10:56:12 -0800932 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
933 return;
934 }
935 } else {
mtkleina5114d72015-08-24 13:27:01 -0700936 SkFILEWStream file(path.c_str());
937 if (!file.isValid()) {
938 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
939 return;
940 }
mtklein748ca3b2015-01-15 10:56:12 -0800941 if (!file.writeStream(data, len)) {
942 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
943 return;
944 }
945 }
946 }
947};
948
949// Run all tasks in the same enclave serially on the same thread.
950// They can't possibly run concurrently with each other.
951static void run_enclave(SkTArray<Task>* tasks) {
952 for (int i = 0; i < tasks->count(); i++) {
953 Task::Run(tasks->begin() + i);
954 }
955}
956
957/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
958
959// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
960
mtklein55e88b22015-01-21 15:50:13 -0800961static SkTDArray<skiatest::Test> gThreadedTests, gGPUTests;
mtklein748ca3b2015-01-15 10:56:12 -0800962
963static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -0800964 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -0800965 return;
966 }
mtklein6393c062015-04-27 08:45:01 -0700967 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r; r = r->next()) {
968 if (!in_shard()) {
969 continue;
970 }
halcanary87f3ba42015-01-20 09:30:20 -0800971 // Despite its name, factory() is returning a reference to
972 // link-time static const POD data.
973 const skiatest::Test& test = r->factory();
974 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -0800975 continue;
976 }
halcanary87f3ba42015-01-20 09:30:20 -0800977 if (test.needsGpu && gpu_supported()) {
mtklein55e88b22015-01-21 15:50:13 -0800978 (FLAGS_gpu_threading ? gThreadedTests : gGPUTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -0800979 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein55e88b22015-01-21 15:50:13 -0800980 gThreadedTests.push(test);
mtklein82d28432015-01-15 12:46:02 -0800981 }
mtklein748ca3b2015-01-15 10:56:12 -0800982 }
983}
984
halcanary87f3ba42015-01-20 09:30:20 -0800985static void run_test(skiatest::Test* test) {
986 struct : public skiatest::Reporter {
mtklein36352bf2015-03-25 18:17:31 -0700987 void reportFailed(const skiatest::Failure& failure) override {
halcanary87f3ba42015-01-20 09:30:20 -0800988 fail(failure.toString());
989 JsonWriter::AddTestFailure(failure);
990 }
mtklein36352bf2015-03-25 18:17:31 -0700991 bool allowExtendedTest() const override {
halcanary87f3ba42015-01-20 09:30:20 -0800992 return FLAGS_pathOpsExtended;
993 }
mtklein36352bf2015-03-25 18:17:31 -0700994 bool verbose() const override { return FLAGS_veryVerbose; }
halcanary87f3ba42015-01-20 09:30:20 -0800995 } reporter;
djsollen824996a2015-06-12 12:06:22 -0700996
997 SkString note;
998 SkString whyBlacklisted = is_blacklisted("_", "tests", "_", test->name);
999 if (!whyBlacklisted.isEmpty()) {
1000 note.appendf(" (--blacklist %s)", whyBlacklisted.c_str());
1001 }
1002
mtkleinf27f08b2015-11-03 06:54:24 -08001003 auto timerStart = now_ms();
djsollen824996a2015-06-12 12:06:22 -07001004 if (!FLAGS_dryRun && whyBlacklisted.isEmpty()) {
djsollen54416de2015-04-03 07:24:48 -07001005 start("unit", "test", "", test->name);
mtklein55e88b22015-01-21 15:50:13 -08001006 GrContextFactory factory;
bsalomon821e10e2015-06-04 14:15:33 -07001007 if (FLAGS_pre_log) {
1008 SkDebugf("\nRunning test %s", test->name);
1009 }
mtklein55e88b22015-01-21 15:50:13 -08001010 test->proc(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -08001011 }
mtkleinf27f08b2015-11-03 06:54:24 -08001012 done(now_ms()-timerStart, "unit", "test", "", test->name, note, "");
mtklein748ca3b2015-01-15 10:56:12 -08001013}
1014
1015/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1016
mtklein55e88b22015-01-21 15:50:13 -08001017// If we're isolating all GPU-bound work to one thread (the default), this function runs all that.
1018static void run_enclave_and_gpu_tests(SkTArray<Task>* tasks) {
1019 run_enclave(tasks);
1020 for (int i = 0; i < gGPUTests.count(); i++) {
1021 run_test(&gGPUTests[i]);
1022 }
1023}
1024
mtkleinde6fc2b2015-03-12 06:28:54 -07001025// Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung.
1026// This prints something every once in a while so that it knows we're still working.
mtklein2e1c47e2015-03-12 07:16:56 -07001027static void start_keepalive() {
1028 struct Loop {
1029 static void forever(void*) {
1030 for (;;) {
1031 static const int kSec = 300;
1032 #if defined(SK_BUILD_FOR_WIN)
1033 Sleep(kSec * 1000);
1034 #else
1035 sleep(kSec);
1036 #endif
mtkleinb37cb412015-03-18 05:27:14 -07001037 SkString running;
1038 {
1039 SkAutoMutexAcquire lock(gRunningMutex);
1040 for (int i = 0; i < gRunning.count(); i++) {
1041 running.appendf("\n\t%s", gRunning[i].c_str());
1042 }
1043 }
1044 SkDebugf("\nCurrently running:%s\n", running.c_str());
mtklein2e1c47e2015-03-12 07:16:56 -07001045 }
1046 }
1047 };
1048 static SkThread* intentionallyLeaked = new SkThread(Loop::forever);
1049 intentionallyLeaked->start();
mtkleinde6fc2b2015-03-12 06:28:54 -07001050}
1051
caryclark83ca6282015-06-10 09:31:09 -07001052#define PORTABLE_FONT_PREFIX "Toy Liberation "
1053
1054static SkTypeface* create_from_name(const char familyName[], SkTypeface::Style style) {
1055 if (familyName && strlen(familyName) > sizeof(PORTABLE_FONT_PREFIX)
1056 && !strncmp(familyName, PORTABLE_FONT_PREFIX, sizeof(PORTABLE_FONT_PREFIX) - 1)) {
caryclark1818acb2015-07-24 12:09:25 -07001057 return sk_tool_utils::create_portable_typeface(familyName, style);
caryclark83ca6282015-06-10 09:31:09 -07001058 }
halcanary96fcdcc2015-08-27 07:41:13 -07001059 return nullptr;
caryclark83ca6282015-06-10 09:31:09 -07001060}
1061
1062#undef PORTABLE_FONT_PREFIX
1063
1064extern SkTypeface* (*gCreateTypefaceDelegate)(const char [], SkTypeface::Style );
1065
jcgregorio3b27ade2014-11-13 08:06:40 -08001066int dm_main();
caryclark17f0b6d2014-07-22 10:15:34 -07001067int dm_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -08001068 SetupCrashHandler();
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +00001069 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -07001070 SkTaskGroup::Enabler enabled(FLAGS_threads);
caryclark83ca6282015-06-10 09:31:09 -07001071 gCreateTypefaceDelegate = &create_from_name;
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +00001072
mtklein2e1c47e2015-03-12 07:16:56 -07001073 start_keepalive();
mtkleinde6fc2b2015-03-12 06:28:54 -07001074
mtklein62bd1a62015-01-27 14:46:26 -08001075 gather_gold();
borenet09ed4802015-04-03 14:15:33 -07001076 gather_uninteresting_hashes();
mtklein62bd1a62015-01-27 14:46:26 -08001077
mtklein748ca3b2015-01-15 10:56:12 -08001078 gather_srcs();
1079 gather_sinks();
1080 gather_tests();
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +00001081
mtklein55e88b22015-01-21 15:50:13 -08001082 gPending = gSrcs.count() * gSinks.count() + gThreadedTests.count() + gGPUTests.count();
mtklein748ca3b2015-01-15 10:56:12 -08001083 SkDebugf("%d srcs * %d sinks + %d tests == %d tasks\n",
mtklein55e88b22015-01-21 15:50:13 -08001084 gSrcs.count(), gSinks.count(), gThreadedTests.count() + gGPUTests.count(), gPending);
mtklein748ca3b2015-01-15 10:56:12 -08001085
1086 // We try to exploit as much parallelism as is safe. Most Src/Sink pairs run on any thread,
1087 // but Sinks that identify as part of a particular enclave run serially on a single thread.
mtklein82d28432015-01-15 12:46:02 -08001088 // CPU tests run on any thread. GPU tests depend on --gpu_threading.
mtklein748ca3b2015-01-15 10:56:12 -08001089 SkTArray<Task> enclaves[kNumEnclaves];
1090 for (int j = 0; j < gSinks.count(); j++) {
1091 SkTArray<Task>& tasks = enclaves[gSinks[j]->enclave()];
1092 for (int i = 0; i < gSrcs.count(); i++) {
1093 tasks.push_back(Task(gSrcs[i], gSinks[j]));
1094 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001095 }
1096
mtklein748ca3b2015-01-15 10:56:12 -08001097 SkTaskGroup tg;
mtklein55e88b22015-01-21 15:50:13 -08001098 tg.batch(run_test, gThreadedTests.begin(), gThreadedTests.count());
1099 for (int i = 0; i < kNumEnclaves; i++) {
1100 switch(i) {
1101 case kAnyThread_Enclave:
1102 tg.batch(Task::Run, enclaves[i].begin(), enclaves[i].count());
1103 break;
1104 case kGPU_Enclave:
1105 tg.add(run_enclave_and_gpu_tests, &enclaves[i]);
1106 break;
1107 default:
1108 tg.add(run_enclave, &enclaves[i]);
1109 break;
mtklein82d28432015-01-15 12:46:02 -08001110 }
mtklein55e88b22015-01-21 15:50:13 -08001111 }
mtklein748ca3b2015-01-15 10:56:12 -08001112 tg.wait();
mtklein748ca3b2015-01-15 10:56:12 -08001113 // At this point we're back in single-threaded land.
caryclarkf53ce802015-06-15 06:48:30 -07001114 sk_tool_utils::release_portable_typefaces();
mtklein@google.comd36522d2013-10-16 13:02:15 +00001115
mtklein2f64eec2015-01-15 14:20:41 -08001116 SkDebugf("\n");
mtklein748ca3b2015-01-15 10:56:12 -08001117 if (gFailures.count() > 0) {
1118 SkDebugf("Failures:\n");
1119 for (int i = 0; i < gFailures.count(); i++) {
mtklein9dc09102015-01-15 15:47:33 -08001120 SkDebugf("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001121 }
1122 SkDebugf("%d failures\n", gFailures.count());
1123 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001124 }
mtklein748ca3b2015-01-15 10:56:12 -08001125 if (gPending > 0) {
1126 SkDebugf("Hrm, we didn't seem to run everything we intended to! Please file a bug.\n");
1127 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001128 }
halcanary7a14b312015-10-01 07:28:13 -07001129 #ifdef SK_PDF_IMAGE_STATS
1130 SkPDFImageDumpStats();
1131 #endif // SK_PDF_IMAGE_STATS
mtklein748ca3b2015-01-15 10:56:12 -08001132 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +00001133}
jcgregorio3b27ade2014-11-13 08:06:40 -08001134
borenet48087572015-04-02 12:16:36 -07001135#if !defined(SK_BUILD_FOR_IOS)
jcgregorio3b27ade2014-11-13 08:06:40 -08001136int main(int argc, char** argv) {
1137 SkCommandLineFlags::Parse(argc, argv);
1138 return dm_main();
1139}
1140#endif