blob: 7497161369b42788dac022dc8f9eedb534801135 [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;
msarett9e707a02015-09-01 14:57:57 -0700233 case CodecSrc::kStripe_Mode:
234 folder.append("stripe");
235 break;
236 case CodecSrc::kSubset_Mode:
msarett36c37962015-09-02 13:20:52 -0700237 folder.append("codec_subset");
msarett9e707a02015-09-01 14:57:57 -0700238 break;
239 }
240
241 switch (dstColorType) {
242 case CodecSrc::kGrayscale_Always_DstColorType:
243 folder.append("_kGray8");
244 break;
245 case CodecSrc::kIndex8_Always_DstColorType:
246 folder.append("_kIndex8");
247 break;
248 default:
249 break;
250 }
251
252 if (1.0f != scale) {
253 folder.appendf("_%.3f", scale);
254 }
255
256 CodecSrc* src = new CodecSrc(path, mode, dstColorType, scale);
257 push_src("image", folder, src);
258}
259
msarett3d9d7a72015-10-21 10:27:10 -0700260static void push_android_codec_src(Path path, AndroidCodecSrc::Mode mode,
261 CodecSrc::DstColorType dstColorType, int sampleSize) {
262 SkString folder;
263 switch (mode) {
264 case AndroidCodecSrc::kFullImage_Mode:
265 folder.append("scaled_codec");
266 break;
267 case AndroidCodecSrc::kDivisor_Mode:
268 folder.append("scaled_codec_divisor");
269 break;
270 }
271
272 switch (dstColorType) {
273 case CodecSrc::kGrayscale_Always_DstColorType:
274 folder.append("_kGray8");
275 break;
276 case CodecSrc::kIndex8_Always_DstColorType:
277 folder.append("_kIndex8");
278 break;
279 default:
280 break;
281 }
282
283 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800284 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700285 }
286
287 AndroidCodecSrc* src = new AndroidCodecSrc(path, mode, dstColorType, sampleSize);
288 push_src("image", folder, src);
289}
290
msarett438b2ad2015-04-09 12:43:10 -0700291static void push_codec_srcs(Path path) {
292 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
293 if (!encoded) {
294 SkDebugf("Couldn't read %s.", path.c_str());
295 return;
296 }
297 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
halcanary96fcdcc2015-08-27 07:41:13 -0700298 if (nullptr == codec.get()) {
msarett438b2ad2015-04-09 12:43:10 -0700299 SkDebugf("Couldn't create codec for %s.", path.c_str());
300 return;
301 }
302
msarett36c37962015-09-02 13:20:52 -0700303 // Native Scales
msarett0a242972015-06-11 14:27:27 -0700304 // TODO (msarett): Implement scaling tests for SkImageDecoder in order to compare with these
305 // tests. SkImageDecoder supports downscales by integer factors.
emmaleer8f4ba762015-08-14 07:44:46 -0700306 // 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 -0700307 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 -0700308
msarett36c37962015-09-02 13:20:52 -0700309 const CodecSrc::Mode nativeModes[] = { CodecSrc::kCodec_Mode, CodecSrc::kScanline_Mode,
msarett5af4e0b2015-11-17 11:18:03 -0800310 CodecSrc::kStripe_Mode, CodecSrc::kSubset_Mode };
msarett9e707a02015-09-01 14:57:57 -0700311
msarett36c37962015-09-02 13:20:52 -0700312 CodecSrc::DstColorType colorTypes[3];
313 uint32_t numColorTypes;
314 switch (codec->getInfo().colorType()) {
315 case kGray_8_SkColorType:
316 // FIXME: Is this a long term solution for testing wbmps decodes to kIndex8?
halcanary6950de62015-11-07 05:29:00 -0800317 // Further discussion on this topic is at https://bug.skia.org/3683 .
msarett3d9d7a72015-10-21 10:27:10 -0700318 // This causes us to try to convert grayscale jpegs to kIndex8. We currently
319 // fail non-fatally in this case.
msarett36c37962015-09-02 13:20:52 -0700320 colorTypes[0] = CodecSrc::kGetFromCanvas_DstColorType;
321 colorTypes[1] = CodecSrc::kGrayscale_Always_DstColorType;
322 colorTypes[2] = CodecSrc::kIndex8_Always_DstColorType;
323 numColorTypes = 3;
324 break;
325 case kIndex_8_SkColorType:
326 colorTypes[0] = CodecSrc::kGetFromCanvas_DstColorType;
327 colorTypes[1] = CodecSrc::kIndex8_Always_DstColorType;
328 numColorTypes = 2;
329 break;
330 default:
331 colorTypes[0] = CodecSrc::kGetFromCanvas_DstColorType;
332 numColorTypes = 1;
333 break;
334 }
msarett9e707a02015-09-01 14:57:57 -0700335
msarett36c37962015-09-02 13:20:52 -0700336 for (float scale : nativeScales) {
msarett36c37962015-09-02 13:20:52 -0700337 for (CodecSrc::Mode mode : nativeModes) {
338 for (uint32_t i = 0; i < numColorTypes; i++) {
339 push_codec_src(path, mode, colorTypes[i], scale);
msarett9e707a02015-09-01 14:57:57 -0700340 }
341 }
msarett0a242972015-06-11 14:27:27 -0700342 }
msarett36c37962015-09-02 13:20:52 -0700343
halcanary6950de62015-11-07 05:29:00 -0800344 // https://bug.skia.org/4428
msarett33c76232015-11-16 13:43:40 -0800345 bool subset = false;
346 // The following image types are supported by BitmapRegionDecoder,
347 // so we will test full image decodes and subset decodes.
348 static const char* const subsetExts[] = {
msarett3d9d7a72015-10-21 10:27:10 -0700349 "jpg", "jpeg", "png", "webp",
350 "JPG", "JPEG", "PNG", "WEBP",
351 };
msarett33c76232015-11-16 13:43:40 -0800352 for (const char* ext : subsetExts) {
msarett3d9d7a72015-10-21 10:27:10 -0700353 if (path.endsWith(ext)) {
msarett33c76232015-11-16 13:43:40 -0800354 subset = true;
msarett3d9d7a72015-10-21 10:27:10 -0700355 break;
356 }
357 }
msarett33c76232015-11-16 13:43:40 -0800358
359 bool full = false;
360 // The following image types are only supported by BitmapFactory,
361 // so we only need to test full image decodes.
362 static const char* fullExts[] = {
msarett5af4e0b2015-11-17 11:18:03 -0800363 "wbmp", "bmp", "gif",
364 "WBMP", "BMP", "GIF",
msarett33c76232015-11-16 13:43:40 -0800365 };
366 for (const char* ext : fullExts) {
367 if (path.endsWith(ext)) {
368 full = true;
369 break;
370 }
371 }
372
373 if (!full && !subset) {
scroggo46c57472015-09-30 08:57:13 -0700374 return;
375 }
376
msarett3d9d7a72015-10-21 10:27:10 -0700377 const int sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
msarett36c37962015-09-02 13:20:52 -0700378
msarett3d9d7a72015-10-21 10:27:10 -0700379 for (int sampleSize : sampleSizes) {
msarett33c76232015-11-16 13:43:40 -0800380 for (uint32_t i = 0; i < numColorTypes; i++) {
381 push_android_codec_src(path, AndroidCodecSrc::kFullImage_Mode, colorTypes[i],
382 sampleSize);
383 if (subset) {
384 push_android_codec_src(path, AndroidCodecSrc::kDivisor_Mode, colorTypes[i],
385 sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700386 }
msarett36c37962015-09-02 13:20:52 -0700387 }
388 }
msarett438b2ad2015-04-09 12:43:10 -0700389}
390
msarett5cb48852015-11-06 08:56:32 -0800391static bool brd_color_type_supported(SkBitmapRegionDecoder::Strategy strategy,
msaretta5783ae2015-09-08 15:35:32 -0700392 CodecSrc::DstColorType dstColorType) {
393 switch (strategy) {
msarett5cb48852015-11-06 08:56:32 -0800394 case SkBitmapRegionDecoder::kCanvas_Strategy:
msaretta5783ae2015-09-08 15:35:32 -0700395 if (CodecSrc::kGetFromCanvas_DstColorType == dstColorType) {
396 return true;
397 }
398 return false;
msarett5cb48852015-11-06 08:56:32 -0800399 case SkBitmapRegionDecoder::kAndroidCodec_Strategy:
msarett26ad17b2015-10-22 07:29:19 -0700400 switch (dstColorType) {
401 case CodecSrc::kGetFromCanvas_DstColorType:
402 case CodecSrc::kIndex8_Always_DstColorType:
403 case CodecSrc::kGrayscale_Always_DstColorType:
404 return true;
405 default:
406 return false;
407 }
msaretta5783ae2015-09-08 15:35:32 -0700408 default:
409 SkASSERT(false);
410 return false;
411 }
412}
413
msarett5cb48852015-11-06 08:56:32 -0800414static void push_brd_src(Path path, SkBitmapRegionDecoder::Strategy strategy,
msaretta5783ae2015-09-08 15:35:32 -0700415 CodecSrc::DstColorType dstColorType, BRDSrc::Mode mode, uint32_t sampleSize) {
416 SkString folder;
417 switch (strategy) {
msarett5cb48852015-11-06 08:56:32 -0800418 case SkBitmapRegionDecoder::kCanvas_Strategy:
msaretta5783ae2015-09-08 15:35:32 -0700419 folder.append("brd_canvas");
420 break;
msarett5cb48852015-11-06 08:56:32 -0800421 case SkBitmapRegionDecoder::kAndroidCodec_Strategy:
msarett26ad17b2015-10-22 07:29:19 -0700422 folder.append("brd_android_codec");
423 break;
msaretta5783ae2015-09-08 15:35:32 -0700424 default:
425 SkASSERT(false);
426 return;
427 }
428
429 switch (mode) {
430 case BRDSrc::kFullImage_Mode:
431 break;
432 case BRDSrc::kDivisor_Mode:
433 folder.append("_divisor");
434 break;
435 default:
436 SkASSERT(false);
437 return;
438 }
439
440 switch (dstColorType) {
441 case CodecSrc::kGetFromCanvas_DstColorType:
442 break;
443 case CodecSrc::kIndex8_Always_DstColorType:
444 folder.append("_kIndex");
445 break;
446 case CodecSrc::kGrayscale_Always_DstColorType:
447 folder.append("_kGray");
448 break;
449 default:
450 SkASSERT(false);
451 return;
452 }
453
454 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800455 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700456 }
457
458 BRDSrc* src = new BRDSrc(path, strategy, mode, dstColorType, sampleSize);
459 push_src("image", folder, src);
460}
461
462static void push_brd_srcs(Path path) {
463
msarett5cb48852015-11-06 08:56:32 -0800464 const SkBitmapRegionDecoder::Strategy strategies[] = {
465 SkBitmapRegionDecoder::kCanvas_Strategy,
msarett5cb48852015-11-06 08:56:32 -0800466 SkBitmapRegionDecoder::kAndroidCodec_Strategy,
msaretta5783ae2015-09-08 15:35:32 -0700467 };
468
scroggo501b7342015-11-03 07:55:11 -0800469 // Test on a variety of sampleSizes, making sure to include:
470 // - 2, 4, and 8, which are natively supported by jpeg
471 // - multiples of 2 which are not divisible by 4 (analogous for 4)
472 // - larger powers of two, since BRD clients generally use powers of 2
473 // We will only produce output for the larger sizes on large images.
474 const uint32_t sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 24, 32, 64 };
msarett6efbe052015-09-11 09:01:16 -0700475
msaretta5783ae2015-09-08 15:35:32 -0700476 // We will only test to one backend (8888), but we will test all of the
477 // color types that we need to decode to on this backend.
478 const CodecSrc::DstColorType dstColorTypes[] = {
msarett26ad17b2015-10-22 07:29:19 -0700479 CodecSrc::kGetFromCanvas_DstColorType,
480 CodecSrc::kIndex8_Always_DstColorType,
481 CodecSrc::kGrayscale_Always_DstColorType,
msaretta5783ae2015-09-08 15:35:32 -0700482 };
483
484 const BRDSrc::Mode modes[] = {
msarett26ad17b2015-10-22 07:29:19 -0700485 BRDSrc::kFullImage_Mode,
486 BRDSrc::kDivisor_Mode,
msaretta5783ae2015-09-08 15:35:32 -0700487 };
488
msarett5cb48852015-11-06 08:56:32 -0800489 for (SkBitmapRegionDecoder::Strategy strategy : strategies) {
msarett6efbe052015-09-11 09:01:16 -0700490 for (uint32_t sampleSize : sampleSizes) {
msarett6efbe052015-09-11 09:01:16 -0700491 for (CodecSrc::DstColorType dstColorType : dstColorTypes) {
492 if (brd_color_type_supported(strategy, dstColorType)) {
493 for (BRDSrc::Mode mode : modes) {
msaretta5783ae2015-09-08 15:35:32 -0700494 push_brd_src(path, strategy, dstColorType, mode, sampleSize);
495 }
496 }
497 }
498 }
499 }
500}
501
502static bool brd_supported(const char* ext) {
msarett8c8f22a2015-04-01 06:58:48 -0700503 static const char* const exts[] = {
msaretta5783ae2015-09-08 15:35:32 -0700504 "jpg", "jpeg", "png", "webp",
505 "JPG", "JPEG", "PNG", "WEBP",
msarett8c8f22a2015-04-01 06:58:48 -0700506 };
507
508 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
509 if (0 == strcmp(exts[i], ext)) {
510 return true;
511 }
512 }
513 return false;
scroggo9b77ddd2015-03-19 06:03:39 -0700514}
515
mtklein748ca3b2015-01-15 10:56:12 -0800516static void gather_srcs() {
517 for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) {
djsollen54416de2015-04-03 07:24:48 -0700518 push_src("gm", "", new GMSrc(r->factory()));
mtklein748ca3b2015-01-15 10:56:12 -0800519 }
halcanaryfc37ad12015-01-30 07:31:19 -0800520 for (int i = 0; i < FLAGS_skps.count(); i++) {
521 const char* path = FLAGS_skps[i];
522 if (sk_isdir(path)) {
523 SkOSFile::Iter it(path, "skp");
524 for (SkString file; it.next(&file); ) {
djsollen54416de2015-04-03 07:24:48 -0700525 push_src("skp", "", new SKPSrc(SkOSPath::Join(path, file.c_str())));
halcanaryfc37ad12015-01-30 07:31:19 -0800526 }
527 } else {
djsollen54416de2015-04-03 07:24:48 -0700528 push_src("skp", "", new SKPSrc(path));
mtklein114c3cd2015-01-15 10:15:02 -0800529 }
530 }
halcanary23b03c32015-01-30 09:58:58 -0800531 static const char* const exts[] = {
532 "bmp", "gif", "jpg", "jpeg", "png", "webp", "ktx", "astc", "wbmp", "ico",
533 "BMP", "GIF", "JPG", "JPEG", "PNG", "WEBP", "KTX", "ASTC", "WBMP", "ICO",
534 };
535 for (int i = 0; i < FLAGS_images.count(); i++) {
536 const char* flag = FLAGS_images[i];
537 if (sk_isdir(flag)) {
538 for (size_t j = 0; j < SK_ARRAY_COUNT(exts); j++) {
539 SkOSFile::Iter it(flag, exts[j]);
540 for (SkString file; it.next(&file); ) {
541 SkString path = SkOSPath::Join(flag, file.c_str());
djsollen54416de2015-04-03 07:24:48 -0700542 push_src("image", "decode", new ImageSrc(path)); // Decode entire image
msaretta5783ae2015-09-08 15:35:32 -0700543 push_codec_srcs(path);
544 if (brd_supported(exts[j])) {
545 push_brd_srcs(path);
scroggo9b77ddd2015-03-19 06:03:39 -0700546 }
halcanary23b03c32015-01-30 09:58:58 -0800547 }
mtklein114c3cd2015-01-15 10:15:02 -0800548 }
halcanary23b03c32015-01-30 09:58:58 -0800549 } else if (sk_exists(flag)) {
550 // assume that FLAGS_images[i] is a valid image if it is a file.
djsollen54416de2015-04-03 07:24:48 -0700551 push_src("image", "decode", new ImageSrc(flag)); // Decode entire image.
msarett438b2ad2015-04-09 12:43:10 -0700552 push_codec_srcs(flag);
msaretta5783ae2015-09-08 15:35:32 -0700553 push_brd_srcs(flag);
mtklein709d2c32015-01-15 08:30:25 -0800554 }
mtklein709d2c32015-01-15 08:30:25 -0800555 }
556}
557
mtklein748ca3b2015-01-15 10:56:12 -0800558static GrGLStandard get_gpu_api() {
559 if (FLAGS_gpuAPI.contains("gl")) { return kGL_GrGLStandard; }
560 if (FLAGS_gpuAPI.contains("gles")) { return kGLES_GrGLStandard; }
561 return kNone_GrGLStandard;
mtklein709d2c32015-01-15 08:30:25 -0800562}
563
mtklein748ca3b2015-01-15 10:56:12 -0800564static void push_sink(const char* tag, Sink* s) {
565 SkAutoTDelete<Sink> sink(s);
566 if (!FLAGS_config.contains(tag)) {
567 return;
mtklein114c3cd2015-01-15 10:15:02 -0800568 }
mtkleine0effd62015-07-29 06:37:28 -0700569 // Try a simple Src as a canary. If it fails, skip this sink.
mtklein748ca3b2015-01-15 10:56:12 -0800570 struct : public Src {
mtkleine0effd62015-07-29 06:37:28 -0700571 Error draw(SkCanvas* c) const override {
572 c->drawRect(SkRect::MakeWH(1,1), SkPaint());
573 return "";
574 }
mtklein36352bf2015-03-25 18:17:31 -0700575 SkISize size() const override { return SkISize::Make(16, 16); }
mtkleine0effd62015-07-29 06:37:28 -0700576 Name name() const override { return "justOneRect"; }
577 } justOneRect;
mtklein114c3cd2015-01-15 10:15:02 -0800578
mtklein748ca3b2015-01-15 10:56:12 -0800579 SkBitmap bitmap;
580 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800581 SkString log;
mtkleine0effd62015-07-29 06:37:28 -0700582 Error err = sink->draw(justOneRect, &bitmap, &stream, &log);
mtklein4089ef72015-03-05 08:40:28 -0800583 if (err.isFatal()) {
mtklein05641a52015-04-21 10:49:13 -0700584 SkDebugf("Could not run %s: %s\n", tag, err.c_str());
585 exit(1);
mtklein114c3cd2015-01-15 10:15:02 -0800586 }
587
mtkleine0effd62015-07-29 06:37:28 -0700588 TaggedSink& ts = gSinks.push_back();
mtklein748ca3b2015-01-15 10:56:12 -0800589 ts.reset(sink.detach());
590 ts.tag = tag;
591}
592
593static bool gpu_supported() {
594#if SK_SUPPORT_GPU
595 return FLAGS_gpu;
596#else
597 return false;
598#endif
599}
600
601static Sink* create_sink(const char* tag) {
602#define SINK(t, sink, ...) if (0 == strcmp(t, tag)) { return new sink(__VA_ARGS__); }
603 if (gpu_supported()) {
mtklein82d28432015-01-15 12:46:02 -0800604 typedef GrContextFactory Gr;
mtklein748ca3b2015-01-15 10:56:12 -0800605 const GrGLStandard api = get_gpu_api();
hendrikw885bf092015-08-27 10:38:39 -0700606 SINK("gpunull", GPUSink, Gr::kNull_GLContextType, api, 0, false, FLAGS_gpu_threading);
607 SINK("gpudebug", GPUSink, Gr::kDebug_GLContextType, api, 0, false, FLAGS_gpu_threading);
608 SINK("gpu", GPUSink, Gr::kNative_GLContextType, api, 0, false, FLAGS_gpu_threading);
609 SINK("gpudft", GPUSink, Gr::kNative_GLContextType, api, 0, true, FLAGS_gpu_threading);
610 SINK("msaa4", GPUSink, Gr::kNative_GLContextType, api, 4, false, FLAGS_gpu_threading);
611 SINK("msaa16", GPUSink, Gr::kNative_GLContextType, api, 16, false, FLAGS_gpu_threading);
bsalomon90e8ab72015-09-08 10:26:51 -0700612 SINK("nvprmsaa4", GPUSink, Gr::kNVPR_GLContextType, api, 4, true, FLAGS_gpu_threading);
613 SINK("nvprmsaa16", GPUSink, Gr::kNVPR_GLContextType, api, 16, true, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800614 #if SK_ANGLE
hendrikw885bf092015-08-27 10:38:39 -0700615 SINK("angle", GPUSink, Gr::kANGLE_GLContextType, api, 0, false, FLAGS_gpu_threading);
hendrikweddbefb2015-09-11 13:07:29 -0700616 SINK("angle-gl", GPUSink, Gr::kANGLE_GL_GLContextType, api, 0, false, FLAGS_gpu_threading);
hendrikw885bf092015-08-27 10:38:39 -0700617 #endif
618 #if SK_COMMAND_BUFFER
619 SINK("commandbuffer", GPUSink, Gr::kCommandBuffer_GLContextType, api, 0, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800620 #endif
621 #if SK_MESA
hendrikw885bf092015-08-27 10:38:39 -0700622 SINK("mesa", GPUSink, Gr::kMESA_GLContextType, api, 0, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800623 #endif
mtklein114c3cd2015-01-15 10:15:02 -0800624 }
mtklein748ca3b2015-01-15 10:56:12 -0800625
tomhudsoneebc39a2015-02-23 12:18:05 -0800626#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
627 SINK("hwui", HWUISink);
628#endif
629
mtklein748ca3b2015-01-15 10:56:12 -0800630 if (FLAGS_cpu) {
631 SINK("565", RasterSink, kRGB_565_SkColorType);
632 SINK("8888", RasterSink, kN32_SkColorType);
halcanaryc11c62f2015-09-28 11:51:54 -0700633 SINK("pdf", PDFSink, "Pdfium");
634 SINK("pdf_poppler", PDFSink, "Poppler");
mtklein9c3f17d2015-01-28 11:35:18 -0800635 SINK("skp", SKPSink);
mtklein8a4527e2015-01-31 20:00:58 -0800636 SINK("svg", SVGSink);
637 SINK("null", NullSink);
halcanary47ef4d52015-03-03 09:13:09 -0800638 SINK("xps", XPSSink);
mtklein748ca3b2015-01-15 10:56:12 -0800639 }
640#undef SINK
halcanary96fcdcc2015-08-27 07:41:13 -0700641 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800642}
643
mtklein748ca3b2015-01-15 10:56:12 -0800644static Sink* create_via(const char* tag, Sink* wrapped) {
645#define VIA(t, via, ...) if (0 == strcmp(t, tag)) { return new via(__VA_ARGS__); }
halcanary96fcdcc2015-08-27 07:41:13 -0700646 VIA("twice", ViaTwice, wrapped);
647 VIA("pipe", ViaPipe, wrapped);
648 VIA("serialize", ViaSerialization, wrapped);
649 VIA("2ndpic", ViaSecondPicture, wrapped);
mtkleind31c13d2015-05-05 12:59:56 -0700650 VIA("sp", ViaSingletonPictures, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700651 VIA("tiles", ViaTiles, 256, 256, nullptr, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800652 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
mtklein2e2ea382015-10-16 10:29:41 -0700653 VIA("remote", ViaRemote, false, wrapped);
654 VIA("remote_cache", ViaRemote, true, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800655
mtkleind603b222015-02-17 11:13:33 -0800656 if (FLAGS_matrix.count() == 4) {
mtklein748ca3b2015-01-15 10:56:12 -0800657 SkMatrix m;
mtkleind603b222015-02-17 11:13:33 -0800658 m.reset();
659 m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
660 m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
661 m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
662 m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
663 VIA("matrix", ViaMatrix, m, wrapped);
664 VIA("upright", ViaUpright, m, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800665 }
tomhudson64de1e12015-03-05 08:01:07 -0800666
667#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
668 VIA("androidsdk", ViaAndroidSDK, wrapped);
669#endif
670
mtklein748ca3b2015-01-15 10:56:12 -0800671#undef VIA
halcanary96fcdcc2015-08-27 07:41:13 -0700672 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800673}
674
mtklein748ca3b2015-01-15 10:56:12 -0800675static void gather_sinks() {
676 for (int i = 0; i < FLAGS_config.count(); i++) {
677 const char* config = FLAGS_config[i];
678 SkTArray<SkString> parts;
679 SkStrSplit(config, "-", &parts);
680
halcanary96fcdcc2015-08-27 07:41:13 -0700681 Sink* sink = nullptr;
mtklein748ca3b2015-01-15 10:56:12 -0800682 for (int i = parts.count(); i-- > 0;) {
683 const char* part = parts[i].c_str();
halcanary96fcdcc2015-08-27 07:41:13 -0700684 Sink* next = (sink == nullptr) ? create_sink(part) : create_via(part, sink);
685 if (next == nullptr) {
mtklein748ca3b2015-01-15 10:56:12 -0800686 SkDebugf("Skipping %s: Don't understand '%s'.\n", config, part);
687 delete sink;
halcanary96fcdcc2015-08-27 07:41:13 -0700688 sink = nullptr;
mtklein748ca3b2015-01-15 10:56:12 -0800689 break;
690 }
691 sink = next;
692 }
693 if (sink) {
694 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800695 }
696 }
697}
mtklein709d2c32015-01-15 08:30:25 -0800698
mtkleina5114d72015-08-24 13:27:01 -0700699static bool dump_png(SkBitmap bitmap, const char* path, const char* md5) {
700 const int w = bitmap.width(),
701 h = bitmap.height();
702
703 // First get the bitmap into N32 color format. The next step will work only there.
704 if (bitmap.colorType() != kN32_SkColorType) {
705 SkBitmap n32;
706 if (!bitmap.copyTo(&n32, kN32_SkColorType)) {
707 return false;
708 }
709 bitmap = n32;
710 }
711
712 // Convert our N32 bitmap into unpremul RGBA for libpng.
713 SkAutoTMalloc<uint32_t> rgba(w*h);
714 if (!bitmap.readPixels(SkImageInfo::Make(w,h, kRGBA_8888_SkColorType, kUnpremul_SkAlphaType),
715 rgba, 4*w, 0,0)) {
716 return false;
717 }
718
719 // We don't need bitmap anymore. Might as well drop our ref.
mtkleinfccb77d2015-08-24 14:13:29 -0700720 bitmap.reset();
mtkleina5114d72015-08-24 13:27:01 -0700721
mtkleinc64137c2015-08-25 10:56:08 -0700722 FILE* f = fopen(path, "wb");
mtkleina5114d72015-08-24 13:27:01 -0700723 if (!f) { return false; }
724
725 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
726 if (!png) {
727 fclose(f);
728 return false;
729 }
730
731 png_infop info = png_create_info_struct(png);
732 if (!info) {
733 png_destroy_write_struct(&png, &info);
734 fclose(f);
735 return false;
736 }
737
mtkleind69ece62015-09-10 10:37:44 -0700738 SkString description;
739 description.append("Key: ");
740 for (int i = 0; i < FLAGS_key.count(); i++) {
741 description.appendf("%s ", FLAGS_key[i]);
742 }
743 description.append("Properties: ");
744 for (int i = 0; i < FLAGS_properties.count(); i++) {
745 description.appendf("%s ", FLAGS_properties[i]);
746 }
747 description.appendf("MD5: %s", md5);
748
mtkleina5114d72015-08-24 13:27:01 -0700749 png_text text[2];
750 text[0].key = (png_charp)"Author";
751 text[0].text = (png_charp)"DM dump_png()";
752 text[0].compression = PNG_TEXT_COMPRESSION_NONE;
753 text[1].key = (png_charp)"Description";
mtkleind69ece62015-09-10 10:37:44 -0700754 text[1].text = (png_charp)description.c_str();
mtkleina5114d72015-08-24 13:27:01 -0700755 text[1].compression = PNG_TEXT_COMPRESSION_NONE;
756 png_set_text(png, info, text, 2);
757
758 png_init_io(png, f);
759 png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8,
760 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
761 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
762 png_write_info(png, info);
763 for (int j = 0; j < h; j++) {
764 png_bytep row = (png_bytep)(rgba.get() + w*j);
765 png_write_rows(png, &row, 1);
766 }
767 png_write_end(png, info);
768
769 png_destroy_write_struct(&png, &info);
770 fclose(f);
771 return true;
772}
773
mtkleina2ef6422015-01-15 13:44:22 -0800774static bool match(const char* needle, const char* haystack) {
halcanary96fcdcc2015-08-27 07:41:13 -0700775 return 0 == strcmp("_", needle) || nullptr != strstr(haystack, needle);
mtkleina2ef6422015-01-15 13:44:22 -0800776}
777
djsollen54416de2015-04-03 07:24:48 -0700778static ImplicitString is_blacklisted(const char* sink, const char* src,
779 const char* srcOptions, const char* name) {
mtklein0d243ff2015-04-03 07:51:00 -0700780 for (int i = 0; i < FLAGS_blacklist.count() - 3; i += 4) {
mtkleina2ef6422015-01-15 13:44:22 -0800781 if (match(FLAGS_blacklist[i+0], sink) &&
djsollen54416de2015-04-03 07:24:48 -0700782 match(FLAGS_blacklist[i+1], src) &&
783 match(FLAGS_blacklist[i+2], srcOptions) &&
784 match(FLAGS_blacklist[i+3], name)) {
785 return SkStringPrintf("%s %s %s %s",
786 FLAGS_blacklist[i+0], FLAGS_blacklist[i+1],
787 FLAGS_blacklist[i+2], FLAGS_blacklist[i+3]);
mtkleina2ef6422015-01-15 13:44:22 -0800788 }
789 }
790 return "";
791}
792
mtklein748ca3b2015-01-15 10:56:12 -0800793// The finest-grained unit of work we can run: draw a single Src into a single Sink,
794// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
795struct Task {
mtkleine0effd62015-07-29 06:37:28 -0700796 Task(const TaggedSrc& src, const TaggedSink& sink) : src(src), sink(sink) {}
797 const TaggedSrc& src;
798 const TaggedSink& sink;
mtklein748ca3b2015-01-15 10:56:12 -0800799
800 static void Run(Task* task) {
mtkleina2ef6422015-01-15 13:44:22 -0800801 SkString name = task->src->name();
mtkleine0effd62015-07-29 06:37:28 -0700802
803 // We'll skip drawing this Src/Sink pair if:
804 // - the Src vetoes the Sink;
805 // - this Src / Sink combination is on the blacklist;
806 // - it's a dry run.
mtklein99cab4e2015-07-31 06:43:04 -0700807 SkString note(task->src->veto(task->sink->flags()) ? " (veto)" : "");
msarett9e707a02015-09-01 14:57:57 -0700808 SkString whyBlacklisted = is_blacklisted(task->sink.tag, task->src.tag.c_str(),
809 task->src.options.c_str(), name.c_str());
mtkleinb37cb412015-03-18 05:27:14 -0700810 if (!whyBlacklisted.isEmpty()) {
811 note.appendf(" (--blacklist %s)", whyBlacklisted.c_str());
812 }
mtkleine0effd62015-07-29 06:37:28 -0700813
mtkleinb9eb4ac2015-02-02 18:26:03 -0800814 SkString log;
mtkleinf27f08b2015-11-03 06:54:24 -0800815 auto timerStart = now_ms();
mtkleine0effd62015-07-29 06:37:28 -0700816 if (!FLAGS_dryRun && note.isEmpty()) {
mtklein748ca3b2015-01-15 10:56:12 -0800817 SkBitmap bitmap;
818 SkDynamicMemoryWStream stream;
bsalomon821e10e2015-06-04 14:15:33 -0700819 if (FLAGS_pre_log) {
820 SkDebugf("\nRunning %s->%s", name.c_str(), task->sink.tag);
821 }
djsollen54416de2015-04-03 07:24:48 -0700822 start(task->sink.tag, task->src.tag, task->src.options, name.c_str());
mtkleinb9eb4ac2015-02-02 18:26:03 -0800823 Error err = task->sink->draw(*task->src, &bitmap, &stream, &log);
mtklein748ca3b2015-01-15 10:56:12 -0800824 if (!err.isEmpty()) {
mtkleinf27f08b2015-11-03 06:54:24 -0800825 auto elapsed = now_ms() - timerStart;
mtklein4089ef72015-03-05 08:40:28 -0800826 if (err.isFatal()) {
djsollen54416de2015-04-03 07:24:48 -0700827 fail(SkStringPrintf("%s %s %s %s: %s",
mtklein4089ef72015-03-05 08:40:28 -0800828 task->sink.tag,
msarett9e707a02015-09-01 14:57:57 -0700829 task->src.tag.c_str(),
830 task->src.options.c_str(),
mtklein4089ef72015-03-05 08:40:28 -0800831 name.c_str(),
832 err.c_str()));
mtkleinb37cb412015-03-18 05:27:14 -0700833 } else {
834 note.appendf(" (skipped: %s)", err.c_str());
mtklein4089ef72015-03-05 08:40:28 -0800835 }
mtkleinf27f08b2015-11-03 06:54:24 -0800836 done(elapsed, task->sink.tag, task->src.tag, task->src.options,
djsollen54416de2015-04-03 07:24:48 -0700837 name, note, log);
mtklein4089ef72015-03-05 08:40:28 -0800838 return;
mtklein748ca3b2015-01-15 10:56:12 -0800839 }
mtklein62bd1a62015-01-27 14:46:26 -0800840 SkAutoTDelete<SkStreamAsset> data(stream.detachAsStream());
841
842 SkString md5;
843 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
844 SkMD5 hash;
845 if (data->getLength()) {
846 hash.writeStream(data, data->getLength());
847 data->rewind();
848 } else {
mtklein38408462015-07-08 07:25:27 -0700849 // If we're BGRA (Linux, Windows), swizzle over to RGBA (Mac, Android).
850 // This helps eliminate multiple 0-pixel-diff hashes on gold.skia.org.
851 // (Android's general slow speed breaks the tie arbitrarily in RGBA's favor.)
852 // We might consider promoting 565 to RGBA too.
853 if (bitmap.colorType() == kBGRA_8888_SkColorType) {
854 SkBitmap swizzle;
855 SkAssertResult(bitmap.copyTo(&swizzle, kRGBA_8888_SkColorType));
856 hash.write(swizzle.getPixels(), swizzle.getSize());
857 } else {
858 hash.write(bitmap.getPixels(), bitmap.getSize());
859 }
mtklein62bd1a62015-01-27 14:46:26 -0800860 }
861 SkMD5::Digest digest;
862 hash.finish(digest);
863 for (int i = 0; i < 16; i++) {
864 md5.appendf("%02x", digest.data[i]);
865 }
866 }
867
868 if (!FLAGS_readPath.isEmpty() &&
msarett9e707a02015-09-01 14:57:57 -0700869 !gGold.contains(Gold(task->sink.tag, task->src.tag.c_str(),
870 task->src.options.c_str(), name, md5))) {
djsollen54416de2015-04-03 07:24:48 -0700871 fail(SkStringPrintf("%s not found for %s %s %s %s in %s",
mtklein62bd1a62015-01-27 14:46:26 -0800872 md5.c_str(),
873 task->sink.tag,
msarett9e707a02015-09-01 14:57:57 -0700874 task->src.tag.c_str(),
875 task->src.options.c_str(),
mtklein62bd1a62015-01-27 14:46:26 -0800876 name.c_str(),
877 FLAGS_readPath[0]));
878 }
879
mtkleinb0531a72015-04-07 13:38:48 -0700880 if (!FLAGS_writePath.isEmpty()) {
mtklein748ca3b2015-01-15 10:56:12 -0800881 const char* ext = task->sink->fileExtension();
mtklein62bd1a62015-01-27 14:46:26 -0800882 if (data->getLength()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700883 WriteToDisk(*task, md5, ext, data, data->getLength(), nullptr);
halcanary022afb82015-01-30 11:00:12 -0800884 SkASSERT(bitmap.drawsNothing());
885 } else if (!bitmap.drawsNothing()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700886 WriteToDisk(*task, md5, ext, nullptr, 0, &bitmap);
mtklein748ca3b2015-01-15 10:56:12 -0800887 }
888 }
889 }
mtkleinf27f08b2015-11-03 06:54:24 -0800890 done(now_ms()-timerStart, task->sink.tag, task->src.tag.c_str(), task->src.options.c_str(),
891 name, note, log);
mtklein748ca3b2015-01-15 10:56:12 -0800892 }
893
894 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -0800895 SkString md5,
896 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -0800897 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -0800898 const SkBitmap* bitmap) {
mtklein748ca3b2015-01-15 10:56:12 -0800899 JsonWriter::BitmapResult result;
djsollen54416de2015-04-03 07:24:48 -0700900 result.name = task.src->name();
901 result.config = task.sink.tag;
902 result.sourceType = task.src.tag;
903 result.sourceOptions = task.src.options;
904 result.ext = ext;
905 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -0800906 JsonWriter::AddBitmapResult(result);
907
mtkleinb0531a72015-04-07 13:38:48 -0700908 // If an MD5 is uninteresting, we want it noted in the JSON file,
909 // but don't want to dump it out as a .png (or whatever ext is).
910 if (gUninterestingHashes.contains(md5)) {
911 return;
912 }
913
mtklein748ca3b2015-01-15 10:56:12 -0800914 const char* dir = FLAGS_writePath[0];
915 if (0 == strcmp(dir, "@")) { // Needed for iOS.
916 dir = FLAGS_resourcePath[0];
917 }
918 sk_mkdir(dir);
919
920 SkString path;
921 if (FLAGS_nameByHash) {
922 path = SkOSPath::Join(dir, result.md5.c_str());
923 path.append(".");
924 path.append(ext);
925 if (sk_exists(path.c_str())) {
926 return; // Content-addressed. If it exists already, we're done.
927 }
928 } else {
929 path = SkOSPath::Join(dir, task.sink.tag);
930 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -0700931 path = SkOSPath::Join(path.c_str(), task.src.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800932 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -0700933 if (strcmp(task.src.options.c_str(), "") != 0) {
934 path = SkOSPath::Join(path.c_str(), task.src.options.c_str());
djsollen54416de2015-04-03 07:24:48 -0700935 sk_mkdir(path.c_str());
936 }
mtklein748ca3b2015-01-15 10:56:12 -0800937 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
938 path.append(".");
939 path.append(ext);
940 }
941
mtklein748ca3b2015-01-15 10:56:12 -0800942 if (bitmap) {
mtkleina5114d72015-08-24 13:27:01 -0700943 if (!dump_png(*bitmap, path.c_str(), result.md5.c_str())) {
mtklein748ca3b2015-01-15 10:56:12 -0800944 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
945 return;
946 }
947 } else {
mtkleina5114d72015-08-24 13:27:01 -0700948 SkFILEWStream file(path.c_str());
949 if (!file.isValid()) {
950 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
951 return;
952 }
mtklein748ca3b2015-01-15 10:56:12 -0800953 if (!file.writeStream(data, len)) {
954 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
955 return;
956 }
957 }
958 }
959};
960
961// Run all tasks in the same enclave serially on the same thread.
962// They can't possibly run concurrently with each other.
963static void run_enclave(SkTArray<Task>* tasks) {
964 for (int i = 0; i < tasks->count(); i++) {
965 Task::Run(tasks->begin() + i);
966 }
967}
968
969/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
970
971// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
972
mtklein55e88b22015-01-21 15:50:13 -0800973static SkTDArray<skiatest::Test> gThreadedTests, gGPUTests;
mtklein748ca3b2015-01-15 10:56:12 -0800974
975static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -0800976 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -0800977 return;
978 }
mtklein6393c062015-04-27 08:45:01 -0700979 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r; r = r->next()) {
980 if (!in_shard()) {
981 continue;
982 }
halcanary87f3ba42015-01-20 09:30:20 -0800983 // Despite its name, factory() is returning a reference to
984 // link-time static const POD data.
985 const skiatest::Test& test = r->factory();
986 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -0800987 continue;
988 }
halcanary87f3ba42015-01-20 09:30:20 -0800989 if (test.needsGpu && gpu_supported()) {
mtklein55e88b22015-01-21 15:50:13 -0800990 (FLAGS_gpu_threading ? gThreadedTests : gGPUTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -0800991 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein55e88b22015-01-21 15:50:13 -0800992 gThreadedTests.push(test);
mtklein82d28432015-01-15 12:46:02 -0800993 }
mtklein748ca3b2015-01-15 10:56:12 -0800994 }
995}
996
halcanary87f3ba42015-01-20 09:30:20 -0800997static void run_test(skiatest::Test* test) {
998 struct : public skiatest::Reporter {
mtklein36352bf2015-03-25 18:17:31 -0700999 void reportFailed(const skiatest::Failure& failure) override {
halcanary87f3ba42015-01-20 09:30:20 -08001000 fail(failure.toString());
1001 JsonWriter::AddTestFailure(failure);
1002 }
mtklein36352bf2015-03-25 18:17:31 -07001003 bool allowExtendedTest() const override {
halcanary87f3ba42015-01-20 09:30:20 -08001004 return FLAGS_pathOpsExtended;
1005 }
mtklein36352bf2015-03-25 18:17:31 -07001006 bool verbose() const override { return FLAGS_veryVerbose; }
halcanary87f3ba42015-01-20 09:30:20 -08001007 } reporter;
djsollen824996a2015-06-12 12:06:22 -07001008
1009 SkString note;
1010 SkString whyBlacklisted = is_blacklisted("_", "tests", "_", test->name);
1011 if (!whyBlacklisted.isEmpty()) {
1012 note.appendf(" (--blacklist %s)", whyBlacklisted.c_str());
1013 }
1014
mtkleinf27f08b2015-11-03 06:54:24 -08001015 auto timerStart = now_ms();
djsollen824996a2015-06-12 12:06:22 -07001016 if (!FLAGS_dryRun && whyBlacklisted.isEmpty()) {
djsollen54416de2015-04-03 07:24:48 -07001017 start("unit", "test", "", test->name);
mtklein55e88b22015-01-21 15:50:13 -08001018 GrContextFactory factory;
bsalomon821e10e2015-06-04 14:15:33 -07001019 if (FLAGS_pre_log) {
1020 SkDebugf("\nRunning test %s", test->name);
1021 }
mtklein55e88b22015-01-21 15:50:13 -08001022 test->proc(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -08001023 }
mtkleinf27f08b2015-11-03 06:54:24 -08001024 done(now_ms()-timerStart, "unit", "test", "", test->name, note, "");
mtklein748ca3b2015-01-15 10:56:12 -08001025}
1026
1027/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1028
mtklein55e88b22015-01-21 15:50:13 -08001029// If we're isolating all GPU-bound work to one thread (the default), this function runs all that.
1030static void run_enclave_and_gpu_tests(SkTArray<Task>* tasks) {
1031 run_enclave(tasks);
1032 for (int i = 0; i < gGPUTests.count(); i++) {
1033 run_test(&gGPUTests[i]);
1034 }
1035}
1036
mtkleinde6fc2b2015-03-12 06:28:54 -07001037// Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung.
1038// This prints something every once in a while so that it knows we're still working.
mtklein2e1c47e2015-03-12 07:16:56 -07001039static void start_keepalive() {
1040 struct Loop {
1041 static void forever(void*) {
1042 for (;;) {
1043 static const int kSec = 300;
1044 #if defined(SK_BUILD_FOR_WIN)
1045 Sleep(kSec * 1000);
1046 #else
1047 sleep(kSec);
1048 #endif
mtkleinb37cb412015-03-18 05:27:14 -07001049 SkString running;
1050 {
1051 SkAutoMutexAcquire lock(gRunningMutex);
1052 for (int i = 0; i < gRunning.count(); i++) {
1053 running.appendf("\n\t%s", gRunning[i].c_str());
1054 }
1055 }
1056 SkDebugf("\nCurrently running:%s\n", running.c_str());
mtklein2e1c47e2015-03-12 07:16:56 -07001057 }
1058 }
1059 };
1060 static SkThread* intentionallyLeaked = new SkThread(Loop::forever);
1061 intentionallyLeaked->start();
mtkleinde6fc2b2015-03-12 06:28:54 -07001062}
1063
caryclark83ca6282015-06-10 09:31:09 -07001064#define PORTABLE_FONT_PREFIX "Toy Liberation "
1065
1066static SkTypeface* create_from_name(const char familyName[], SkTypeface::Style style) {
1067 if (familyName && strlen(familyName) > sizeof(PORTABLE_FONT_PREFIX)
1068 && !strncmp(familyName, PORTABLE_FONT_PREFIX, sizeof(PORTABLE_FONT_PREFIX) - 1)) {
caryclark1818acb2015-07-24 12:09:25 -07001069 return sk_tool_utils::create_portable_typeface(familyName, style);
caryclark83ca6282015-06-10 09:31:09 -07001070 }
halcanary96fcdcc2015-08-27 07:41:13 -07001071 return nullptr;
caryclark83ca6282015-06-10 09:31:09 -07001072}
1073
1074#undef PORTABLE_FONT_PREFIX
1075
1076extern SkTypeface* (*gCreateTypefaceDelegate)(const char [], SkTypeface::Style );
1077
jcgregorio3b27ade2014-11-13 08:06:40 -08001078int dm_main();
caryclark17f0b6d2014-07-22 10:15:34 -07001079int dm_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -08001080 SetupCrashHandler();
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +00001081 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -07001082 SkTaskGroup::Enabler enabled(FLAGS_threads);
caryclark83ca6282015-06-10 09:31:09 -07001083 gCreateTypefaceDelegate = &create_from_name;
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +00001084
mtklein2e1c47e2015-03-12 07:16:56 -07001085 start_keepalive();
mtkleinde6fc2b2015-03-12 06:28:54 -07001086
mtklein62bd1a62015-01-27 14:46:26 -08001087 gather_gold();
borenet09ed4802015-04-03 14:15:33 -07001088 gather_uninteresting_hashes();
mtklein62bd1a62015-01-27 14:46:26 -08001089
mtklein748ca3b2015-01-15 10:56:12 -08001090 gather_srcs();
1091 gather_sinks();
1092 gather_tests();
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +00001093
mtklein55e88b22015-01-21 15:50:13 -08001094 gPending = gSrcs.count() * gSinks.count() + gThreadedTests.count() + gGPUTests.count();
mtklein748ca3b2015-01-15 10:56:12 -08001095 SkDebugf("%d srcs * %d sinks + %d tests == %d tasks\n",
mtklein55e88b22015-01-21 15:50:13 -08001096 gSrcs.count(), gSinks.count(), gThreadedTests.count() + gGPUTests.count(), gPending);
mtklein748ca3b2015-01-15 10:56:12 -08001097
1098 // We try to exploit as much parallelism as is safe. Most Src/Sink pairs run on any thread,
1099 // but Sinks that identify as part of a particular enclave run serially on a single thread.
mtklein82d28432015-01-15 12:46:02 -08001100 // CPU tests run on any thread. GPU tests depend on --gpu_threading.
mtklein748ca3b2015-01-15 10:56:12 -08001101 SkTArray<Task> enclaves[kNumEnclaves];
1102 for (int j = 0; j < gSinks.count(); j++) {
1103 SkTArray<Task>& tasks = enclaves[gSinks[j]->enclave()];
1104 for (int i = 0; i < gSrcs.count(); i++) {
1105 tasks.push_back(Task(gSrcs[i], gSinks[j]));
1106 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001107 }
1108
mtklein748ca3b2015-01-15 10:56:12 -08001109 SkTaskGroup tg;
mtklein55e88b22015-01-21 15:50:13 -08001110 tg.batch(run_test, gThreadedTests.begin(), gThreadedTests.count());
1111 for (int i = 0; i < kNumEnclaves; i++) {
1112 switch(i) {
1113 case kAnyThread_Enclave:
1114 tg.batch(Task::Run, enclaves[i].begin(), enclaves[i].count());
1115 break;
1116 case kGPU_Enclave:
1117 tg.add(run_enclave_and_gpu_tests, &enclaves[i]);
1118 break;
1119 default:
1120 tg.add(run_enclave, &enclaves[i]);
1121 break;
mtklein82d28432015-01-15 12:46:02 -08001122 }
mtklein55e88b22015-01-21 15:50:13 -08001123 }
mtklein748ca3b2015-01-15 10:56:12 -08001124 tg.wait();
mtklein748ca3b2015-01-15 10:56:12 -08001125 // At this point we're back in single-threaded land.
caryclarkf53ce802015-06-15 06:48:30 -07001126 sk_tool_utils::release_portable_typefaces();
mtklein@google.comd36522d2013-10-16 13:02:15 +00001127
mtklein2f64eec2015-01-15 14:20:41 -08001128 SkDebugf("\n");
mtklein748ca3b2015-01-15 10:56:12 -08001129 if (gFailures.count() > 0) {
1130 SkDebugf("Failures:\n");
1131 for (int i = 0; i < gFailures.count(); i++) {
mtklein9dc09102015-01-15 15:47:33 -08001132 SkDebugf("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001133 }
1134 SkDebugf("%d failures\n", gFailures.count());
1135 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001136 }
mtklein748ca3b2015-01-15 10:56:12 -08001137 if (gPending > 0) {
1138 SkDebugf("Hrm, we didn't seem to run everything we intended to! Please file a bug.\n");
1139 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001140 }
halcanary7a14b312015-10-01 07:28:13 -07001141 #ifdef SK_PDF_IMAGE_STATS
1142 SkPDFImageDumpStats();
1143 #endif // SK_PDF_IMAGE_STATS
mtklein748ca3b2015-01-15 10:56:12 -08001144 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +00001145}
jcgregorio3b27ade2014-11-13 08:06:40 -08001146
kkinnunen179a8f52015-11-20 13:32:24 -08001147// TODO: currently many GPU tests are declared outside SK_SUPPORT_GPU guards.
1148// Thus we export the empty RunWithGPUTestContexts when SK_SUPPORT_GPU=0.
1149namespace skiatest {
1150namespace {
1151typedef void(*TestWithGrContext)(skiatest::Reporter*, GrContext*);
1152typedef void(*TestWithGrContextAndGLContext)(skiatest::Reporter*, GrContext*, SkGLContext*);
1153#if SK_SUPPORT_GPU
1154template<typename T>
1155void call_test(T test, skiatest::Reporter* reporter, GrContextFactory::ContextInfo* context);
1156template<>
1157void call_test(TestWithGrContext test, skiatest::Reporter* reporter,
1158 GrContextFactory::ContextInfo* context) {
1159 test(reporter, context->fGrContext);
1160}
1161template<>
1162void call_test(TestWithGrContextAndGLContext test, skiatest::Reporter* reporter,
1163 GrContextFactory::ContextInfo* context) {
1164 test(reporter, context->fGrContext, context->fGLContext);
1165}
1166#endif
1167} // namespace
1168
1169
1170template<typename T>
1171void RunWithGPUTestContexts(T test, GPUTestContexts testContexts, Reporter* reporter,
1172 GrContextFactory* factory) {
1173#if SK_SUPPORT_GPU
1174 for (int i = 0; i < GrContextFactory::kGLContextTypeCnt; ++i) {
1175 GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLContextType) i;
1176 int contextSelector = kNone_GPUTestContexts;
1177 if (GrContextFactory::IsRenderingGLContext(glCtxType)) {
1178 contextSelector |= kAllRendering_GPUTestContexts;
1179 }
1180 if (glCtxType == GrContextFactory::kNative_GLContextType) {
1181 contextSelector |= kNative_GPUTestContexts;
1182 }
1183 if (glCtxType == GrContextFactory::kNull_GLContextType) {
1184 contextSelector |= kNull_GPUTestContexts;
1185 }
1186 if ((testContexts & contextSelector) == 0) {
1187 continue;
1188 }
1189 if (GrContextFactory::ContextInfo* context = factory->getContextInfo(glCtxType)) {
1190 call_test(test, reporter, context);
1191 }
1192 }
1193#endif
1194}
1195
1196template
1197void RunWithGPUTestContexts<TestWithGrContext>(TestWithGrContext test,
1198 GPUTestContexts testContexts,
1199 Reporter* reporter,
1200 GrContextFactory* factory);
1201template
1202void RunWithGPUTestContexts<TestWithGrContextAndGLContext>(TestWithGrContextAndGLContext test,
1203 GPUTestContexts testContexts,
1204 Reporter* reporter,
1205 GrContextFactory* factory);
1206} // namespace skiatest
1207
borenet48087572015-04-02 12:16:36 -07001208#if !defined(SK_BUILD_FOR_IOS)
jcgregorio3b27ade2014-11-13 08:06:40 -08001209int main(int argc, char** argv) {
1210 SkCommandLineFlags::Parse(argc, argv);
1211 return dm_main();
1212}
1213#endif