blob: 41e4c8dc3eae4a6c56b399bb963414b15f4c0197 [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.
msarettbe8216a2015-12-04 08:00:50 -0800348 static const char* const exts[] = {
msarett3d9d7a72015-10-21 10:27:10 -0700349 "jpg", "jpeg", "png", "webp",
350 "JPG", "JPEG", "PNG", "WEBP",
351 };
msarettbe8216a2015-12-04 08:00:50 -0800352 for (const char* ext : exts) {
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
msarett3d9d7a72015-10-21 10:27:10 -0700359 const int sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
msarett36c37962015-09-02 13:20:52 -0700360
msarett3d9d7a72015-10-21 10:27:10 -0700361 for (int sampleSize : sampleSizes) {
msarett33c76232015-11-16 13:43:40 -0800362 for (uint32_t i = 0; i < numColorTypes; i++) {
363 push_android_codec_src(path, AndroidCodecSrc::kFullImage_Mode, colorTypes[i],
364 sampleSize);
365 if (subset) {
366 push_android_codec_src(path, AndroidCodecSrc::kDivisor_Mode, colorTypes[i],
367 sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700368 }
msarett36c37962015-09-02 13:20:52 -0700369 }
370 }
msarett438b2ad2015-04-09 12:43:10 -0700371}
372
msarett5cb48852015-11-06 08:56:32 -0800373static bool brd_color_type_supported(SkBitmapRegionDecoder::Strategy strategy,
msaretta5783ae2015-09-08 15:35:32 -0700374 CodecSrc::DstColorType dstColorType) {
375 switch (strategy) {
msarett5cb48852015-11-06 08:56:32 -0800376 case SkBitmapRegionDecoder::kCanvas_Strategy:
msaretta5783ae2015-09-08 15:35:32 -0700377 if (CodecSrc::kGetFromCanvas_DstColorType == dstColorType) {
378 return true;
379 }
380 return false;
msarett5cb48852015-11-06 08:56:32 -0800381 case SkBitmapRegionDecoder::kAndroidCodec_Strategy:
msarett26ad17b2015-10-22 07:29:19 -0700382 switch (dstColorType) {
383 case CodecSrc::kGetFromCanvas_DstColorType:
384 case CodecSrc::kIndex8_Always_DstColorType:
385 case CodecSrc::kGrayscale_Always_DstColorType:
386 return true;
387 default:
388 return false;
389 }
msaretta5783ae2015-09-08 15:35:32 -0700390 default:
391 SkASSERT(false);
392 return false;
393 }
394}
395
msarett5cb48852015-11-06 08:56:32 -0800396static void push_brd_src(Path path, SkBitmapRegionDecoder::Strategy strategy,
msaretta5783ae2015-09-08 15:35:32 -0700397 CodecSrc::DstColorType dstColorType, BRDSrc::Mode mode, uint32_t sampleSize) {
398 SkString folder;
399 switch (strategy) {
msarett5cb48852015-11-06 08:56:32 -0800400 case SkBitmapRegionDecoder::kCanvas_Strategy:
msaretta5783ae2015-09-08 15:35:32 -0700401 folder.append("brd_canvas");
402 break;
msarett5cb48852015-11-06 08:56:32 -0800403 case SkBitmapRegionDecoder::kAndroidCodec_Strategy:
msarett26ad17b2015-10-22 07:29:19 -0700404 folder.append("brd_android_codec");
405 break;
msaretta5783ae2015-09-08 15:35:32 -0700406 default:
407 SkASSERT(false);
408 return;
409 }
410
411 switch (mode) {
412 case BRDSrc::kFullImage_Mode:
413 break;
414 case BRDSrc::kDivisor_Mode:
415 folder.append("_divisor");
416 break;
417 default:
418 SkASSERT(false);
419 return;
420 }
421
422 switch (dstColorType) {
423 case CodecSrc::kGetFromCanvas_DstColorType:
424 break;
425 case CodecSrc::kIndex8_Always_DstColorType:
426 folder.append("_kIndex");
427 break;
428 case CodecSrc::kGrayscale_Always_DstColorType:
429 folder.append("_kGray");
430 break;
431 default:
432 SkASSERT(false);
433 return;
434 }
435
436 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800437 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700438 }
439
440 BRDSrc* src = new BRDSrc(path, strategy, mode, dstColorType, sampleSize);
441 push_src("image", folder, src);
442}
443
444static void push_brd_srcs(Path path) {
445
msarett5cb48852015-11-06 08:56:32 -0800446 const SkBitmapRegionDecoder::Strategy strategies[] = {
447 SkBitmapRegionDecoder::kCanvas_Strategy,
msarett5cb48852015-11-06 08:56:32 -0800448 SkBitmapRegionDecoder::kAndroidCodec_Strategy,
msaretta5783ae2015-09-08 15:35:32 -0700449 };
450
scroggo501b7342015-11-03 07:55:11 -0800451 // Test on a variety of sampleSizes, making sure to include:
452 // - 2, 4, and 8, which are natively supported by jpeg
453 // - multiples of 2 which are not divisible by 4 (analogous for 4)
454 // - larger powers of two, since BRD clients generally use powers of 2
455 // We will only produce output for the larger sizes on large images.
456 const uint32_t sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 24, 32, 64 };
msarett6efbe052015-09-11 09:01:16 -0700457
msaretta5783ae2015-09-08 15:35:32 -0700458 // We will only test to one backend (8888), but we will test all of the
459 // color types that we need to decode to on this backend.
460 const CodecSrc::DstColorType dstColorTypes[] = {
msarett26ad17b2015-10-22 07:29:19 -0700461 CodecSrc::kGetFromCanvas_DstColorType,
462 CodecSrc::kIndex8_Always_DstColorType,
463 CodecSrc::kGrayscale_Always_DstColorType,
msaretta5783ae2015-09-08 15:35:32 -0700464 };
465
466 const BRDSrc::Mode modes[] = {
msarett26ad17b2015-10-22 07:29:19 -0700467 BRDSrc::kFullImage_Mode,
468 BRDSrc::kDivisor_Mode,
msaretta5783ae2015-09-08 15:35:32 -0700469 };
470
msarett5cb48852015-11-06 08:56:32 -0800471 for (SkBitmapRegionDecoder::Strategy strategy : strategies) {
msarett6efbe052015-09-11 09:01:16 -0700472 for (uint32_t sampleSize : sampleSizes) {
msarett6efbe052015-09-11 09:01:16 -0700473 for (CodecSrc::DstColorType dstColorType : dstColorTypes) {
474 if (brd_color_type_supported(strategy, dstColorType)) {
475 for (BRDSrc::Mode mode : modes) {
msaretta5783ae2015-09-08 15:35:32 -0700476 push_brd_src(path, strategy, dstColorType, mode, sampleSize);
477 }
478 }
479 }
480 }
481 }
482}
483
484static bool brd_supported(const char* ext) {
msarett8c8f22a2015-04-01 06:58:48 -0700485 static const char* const exts[] = {
msaretta5783ae2015-09-08 15:35:32 -0700486 "jpg", "jpeg", "png", "webp",
487 "JPG", "JPEG", "PNG", "WEBP",
msarett8c8f22a2015-04-01 06:58:48 -0700488 };
489
490 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
491 if (0 == strcmp(exts[i], ext)) {
492 return true;
493 }
494 }
495 return false;
scroggo9b77ddd2015-03-19 06:03:39 -0700496}
497
mtklein748ca3b2015-01-15 10:56:12 -0800498static void gather_srcs() {
499 for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) {
djsollen54416de2015-04-03 07:24:48 -0700500 push_src("gm", "", new GMSrc(r->factory()));
mtklein748ca3b2015-01-15 10:56:12 -0800501 }
halcanaryfc37ad12015-01-30 07:31:19 -0800502 for (int i = 0; i < FLAGS_skps.count(); i++) {
503 const char* path = FLAGS_skps[i];
504 if (sk_isdir(path)) {
505 SkOSFile::Iter it(path, "skp");
506 for (SkString file; it.next(&file); ) {
djsollen54416de2015-04-03 07:24:48 -0700507 push_src("skp", "", new SKPSrc(SkOSPath::Join(path, file.c_str())));
halcanaryfc37ad12015-01-30 07:31:19 -0800508 }
509 } else {
djsollen54416de2015-04-03 07:24:48 -0700510 push_src("skp", "", new SKPSrc(path));
mtklein114c3cd2015-01-15 10:15:02 -0800511 }
512 }
halcanary23b03c32015-01-30 09:58:58 -0800513 static const char* const exts[] = {
514 "bmp", "gif", "jpg", "jpeg", "png", "webp", "ktx", "astc", "wbmp", "ico",
515 "BMP", "GIF", "JPG", "JPEG", "PNG", "WEBP", "KTX", "ASTC", "WBMP", "ICO",
516 };
517 for (int i = 0; i < FLAGS_images.count(); i++) {
518 const char* flag = FLAGS_images[i];
519 if (sk_isdir(flag)) {
520 for (size_t j = 0; j < SK_ARRAY_COUNT(exts); j++) {
521 SkOSFile::Iter it(flag, exts[j]);
522 for (SkString file; it.next(&file); ) {
523 SkString path = SkOSPath::Join(flag, file.c_str());
djsollen54416de2015-04-03 07:24:48 -0700524 push_src("image", "decode", new ImageSrc(path)); // Decode entire image
msaretta5783ae2015-09-08 15:35:32 -0700525 push_codec_srcs(path);
526 if (brd_supported(exts[j])) {
527 push_brd_srcs(path);
scroggo9b77ddd2015-03-19 06:03:39 -0700528 }
halcanary23b03c32015-01-30 09:58:58 -0800529 }
mtklein114c3cd2015-01-15 10:15:02 -0800530 }
halcanary23b03c32015-01-30 09:58:58 -0800531 } else if (sk_exists(flag)) {
532 // assume that FLAGS_images[i] is a valid image if it is a file.
djsollen54416de2015-04-03 07:24:48 -0700533 push_src("image", "decode", new ImageSrc(flag)); // Decode entire image.
msarett438b2ad2015-04-09 12:43:10 -0700534 push_codec_srcs(flag);
msaretta5783ae2015-09-08 15:35:32 -0700535 push_brd_srcs(flag);
mtklein709d2c32015-01-15 08:30:25 -0800536 }
mtklein709d2c32015-01-15 08:30:25 -0800537 }
538}
539
mtklein748ca3b2015-01-15 10:56:12 -0800540static GrGLStandard get_gpu_api() {
541 if (FLAGS_gpuAPI.contains("gl")) { return kGL_GrGLStandard; }
542 if (FLAGS_gpuAPI.contains("gles")) { return kGLES_GrGLStandard; }
543 return kNone_GrGLStandard;
mtklein709d2c32015-01-15 08:30:25 -0800544}
545
mtklein748ca3b2015-01-15 10:56:12 -0800546static void push_sink(const char* tag, Sink* s) {
547 SkAutoTDelete<Sink> sink(s);
548 if (!FLAGS_config.contains(tag)) {
549 return;
mtklein114c3cd2015-01-15 10:15:02 -0800550 }
mtkleine0effd62015-07-29 06:37:28 -0700551 // Try a simple Src as a canary. If it fails, skip this sink.
mtklein748ca3b2015-01-15 10:56:12 -0800552 struct : public Src {
mtkleine0effd62015-07-29 06:37:28 -0700553 Error draw(SkCanvas* c) const override {
554 c->drawRect(SkRect::MakeWH(1,1), SkPaint());
555 return "";
556 }
mtklein36352bf2015-03-25 18:17:31 -0700557 SkISize size() const override { return SkISize::Make(16, 16); }
mtkleine0effd62015-07-29 06:37:28 -0700558 Name name() const override { return "justOneRect"; }
559 } justOneRect;
mtklein114c3cd2015-01-15 10:15:02 -0800560
mtklein748ca3b2015-01-15 10:56:12 -0800561 SkBitmap bitmap;
562 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800563 SkString log;
mtkleine0effd62015-07-29 06:37:28 -0700564 Error err = sink->draw(justOneRect, &bitmap, &stream, &log);
mtklein4089ef72015-03-05 08:40:28 -0800565 if (err.isFatal()) {
mtklein05641a52015-04-21 10:49:13 -0700566 SkDebugf("Could not run %s: %s\n", tag, err.c_str());
567 exit(1);
mtklein114c3cd2015-01-15 10:15:02 -0800568 }
569
mtkleine0effd62015-07-29 06:37:28 -0700570 TaggedSink& ts = gSinks.push_back();
mtklein748ca3b2015-01-15 10:56:12 -0800571 ts.reset(sink.detach());
572 ts.tag = tag;
573}
574
575static bool gpu_supported() {
576#if SK_SUPPORT_GPU
577 return FLAGS_gpu;
578#else
579 return false;
580#endif
581}
kkinnunena18a8bc2015-12-03 23:04:50 -0800582static Sink* create_gpu_sink(const char* tag, GrContextFactory::GLContextType contextType, int samples, bool diText, bool threaded) {
583#if SK_SUPPORT_GPU
584 GrContextFactory testFactory;
585 const GrGLStandard api = get_gpu_api();
586 if (testFactory.get(contextType, api)) {
587 return new GPUSink(contextType, api, samples, diText, threaded);
588 }
589 SkDebugf("WARNING: can not create GPU context for config '%s'. GM tests will be skipped.\n", tag);
590#endif
591 return nullptr;
592}
mtklein748ca3b2015-01-15 10:56:12 -0800593static Sink* create_sink(const char* tag) {
kkinnunena18a8bc2015-12-03 23:04:50 -0800594#define GPU_SINK(t, ...) if (0 == strcmp(t, tag)) { return create_gpu_sink(tag, __VA_ARGS__); }
mtklein748ca3b2015-01-15 10:56:12 -0800595 if (gpu_supported()) {
mtklein82d28432015-01-15 12:46:02 -0800596 typedef GrContextFactory Gr;
kkinnunena18a8bc2015-12-03 23:04:50 -0800597 GPU_SINK("gpunull", Gr::kNull_GLContextType, 0, false, FLAGS_gpu_threading);
598 GPU_SINK("gpudebug", Gr::kDebug_GLContextType, 0, false, FLAGS_gpu_threading);
599 GPU_SINK("gpu", Gr::kNative_GLContextType, 0, false, FLAGS_gpu_threading);
600 GPU_SINK("gpudft", Gr::kNative_GLContextType, 0, true, FLAGS_gpu_threading);
601 GPU_SINK("msaa4", Gr::kNative_GLContextType, 4, false, FLAGS_gpu_threading);
602 GPU_SINK("msaa16", Gr::kNative_GLContextType, 16, false, FLAGS_gpu_threading);
603 GPU_SINK("nvprmsaa4", Gr::kNVPR_GLContextType, 4, true, FLAGS_gpu_threading);
604 GPU_SINK("nvprmsaa16", Gr::kNVPR_GLContextType, 16, true, FLAGS_gpu_threading);
605#if SK_ANGLE
606 GPU_SINK("angle", Gr::kANGLE_GLContextType, 0, false, FLAGS_gpu_threading);
607 GPU_SINK("angle-gl", Gr::kANGLE_GL_GLContextType, 0, false, FLAGS_gpu_threading);
608#endif
609#if SK_COMMAND_BUFFER
610 GPU_SINK("commandbuffer", Gr::kCommandBuffer_GLContextType, 0, false, FLAGS_gpu_threading);
611#endif
612#if SK_MESA
613 GPU_SINK("mesa", Gr::kMESA_GLContextType, 0, false, FLAGS_gpu_threading);
614#endif
mtklein114c3cd2015-01-15 10:15:02 -0800615 }
kkinnunena18a8bc2015-12-03 23:04:50 -0800616#undef GPU_SINK
mtklein748ca3b2015-01-15 10:56:12 -0800617
kkinnunena18a8bc2015-12-03 23:04:50 -0800618#define SINK(t, sink, ...) if (0 == strcmp(t, tag)) { return new sink(__VA_ARGS__); }
tomhudsoneebc39a2015-02-23 12:18:05 -0800619#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
620 SINK("hwui", HWUISink);
621#endif
622
mtklein748ca3b2015-01-15 10:56:12 -0800623 if (FLAGS_cpu) {
624 SINK("565", RasterSink, kRGB_565_SkColorType);
625 SINK("8888", RasterSink, kN32_SkColorType);
halcanaryc11c62f2015-09-28 11:51:54 -0700626 SINK("pdf", PDFSink, "Pdfium");
627 SINK("pdf_poppler", PDFSink, "Poppler");
mtklein9c3f17d2015-01-28 11:35:18 -0800628 SINK("skp", SKPSink);
mtklein8a4527e2015-01-31 20:00:58 -0800629 SINK("svg", SVGSink);
630 SINK("null", NullSink);
halcanary47ef4d52015-03-03 09:13:09 -0800631 SINK("xps", XPSSink);
mtklein748ca3b2015-01-15 10:56:12 -0800632 }
633#undef SINK
halcanary96fcdcc2015-08-27 07:41:13 -0700634 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800635}
636
mtklein748ca3b2015-01-15 10:56:12 -0800637static Sink* create_via(const char* tag, Sink* wrapped) {
638#define VIA(t, via, ...) if (0 == strcmp(t, tag)) { return new via(__VA_ARGS__); }
halcanary96fcdcc2015-08-27 07:41:13 -0700639 VIA("twice", ViaTwice, wrapped);
640 VIA("pipe", ViaPipe, wrapped);
641 VIA("serialize", ViaSerialization, wrapped);
642 VIA("2ndpic", ViaSecondPicture, wrapped);
mtkleind31c13d2015-05-05 12:59:56 -0700643 VIA("sp", ViaSingletonPictures, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700644 VIA("tiles", ViaTiles, 256, 256, nullptr, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800645 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
mtklein2e2ea382015-10-16 10:29:41 -0700646 VIA("remote", ViaRemote, false, wrapped);
647 VIA("remote_cache", ViaRemote, true, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800648
mtkleind603b222015-02-17 11:13:33 -0800649 if (FLAGS_matrix.count() == 4) {
mtklein748ca3b2015-01-15 10:56:12 -0800650 SkMatrix m;
mtkleind603b222015-02-17 11:13:33 -0800651 m.reset();
652 m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
653 m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
654 m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
655 m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
656 VIA("matrix", ViaMatrix, m, wrapped);
657 VIA("upright", ViaUpright, m, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800658 }
tomhudson64de1e12015-03-05 08:01:07 -0800659
660#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
661 VIA("androidsdk", ViaAndroidSDK, wrapped);
662#endif
663
mtklein748ca3b2015-01-15 10:56:12 -0800664#undef VIA
halcanary96fcdcc2015-08-27 07:41:13 -0700665 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800666}
667
mtklein748ca3b2015-01-15 10:56:12 -0800668static void gather_sinks() {
669 for (int i = 0; i < FLAGS_config.count(); i++) {
670 const char* config = FLAGS_config[i];
671 SkTArray<SkString> parts;
672 SkStrSplit(config, "-", &parts);
673
halcanary96fcdcc2015-08-27 07:41:13 -0700674 Sink* sink = nullptr;
mtklein748ca3b2015-01-15 10:56:12 -0800675 for (int i = parts.count(); i-- > 0;) {
676 const char* part = parts[i].c_str();
halcanary96fcdcc2015-08-27 07:41:13 -0700677 Sink* next = (sink == nullptr) ? create_sink(part) : create_via(part, sink);
678 if (next == nullptr) {
mtklein748ca3b2015-01-15 10:56:12 -0800679 SkDebugf("Skipping %s: Don't understand '%s'.\n", config, part);
680 delete sink;
halcanary96fcdcc2015-08-27 07:41:13 -0700681 sink = nullptr;
mtklein748ca3b2015-01-15 10:56:12 -0800682 break;
683 }
684 sink = next;
685 }
686 if (sink) {
687 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800688 }
689 }
690}
mtklein709d2c32015-01-15 08:30:25 -0800691
mtkleina5114d72015-08-24 13:27:01 -0700692static bool dump_png(SkBitmap bitmap, const char* path, const char* md5) {
693 const int w = bitmap.width(),
694 h = bitmap.height();
695
696 // First get the bitmap into N32 color format. The next step will work only there.
697 if (bitmap.colorType() != kN32_SkColorType) {
698 SkBitmap n32;
699 if (!bitmap.copyTo(&n32, kN32_SkColorType)) {
700 return false;
701 }
702 bitmap = n32;
703 }
704
705 // Convert our N32 bitmap into unpremul RGBA for libpng.
706 SkAutoTMalloc<uint32_t> rgba(w*h);
707 if (!bitmap.readPixels(SkImageInfo::Make(w,h, kRGBA_8888_SkColorType, kUnpremul_SkAlphaType),
708 rgba, 4*w, 0,0)) {
709 return false;
710 }
711
712 // We don't need bitmap anymore. Might as well drop our ref.
mtkleinfccb77d2015-08-24 14:13:29 -0700713 bitmap.reset();
mtkleina5114d72015-08-24 13:27:01 -0700714
mtkleinc64137c2015-08-25 10:56:08 -0700715 FILE* f = fopen(path, "wb");
mtkleina5114d72015-08-24 13:27:01 -0700716 if (!f) { return false; }
717
718 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
719 if (!png) {
720 fclose(f);
721 return false;
722 }
723
724 png_infop info = png_create_info_struct(png);
725 if (!info) {
726 png_destroy_write_struct(&png, &info);
727 fclose(f);
728 return false;
729 }
730
mtkleind69ece62015-09-10 10:37:44 -0700731 SkString description;
732 description.append("Key: ");
733 for (int i = 0; i < FLAGS_key.count(); i++) {
734 description.appendf("%s ", FLAGS_key[i]);
735 }
736 description.append("Properties: ");
737 for (int i = 0; i < FLAGS_properties.count(); i++) {
738 description.appendf("%s ", FLAGS_properties[i]);
739 }
740 description.appendf("MD5: %s", md5);
741
mtkleina5114d72015-08-24 13:27:01 -0700742 png_text text[2];
743 text[0].key = (png_charp)"Author";
744 text[0].text = (png_charp)"DM dump_png()";
745 text[0].compression = PNG_TEXT_COMPRESSION_NONE;
746 text[1].key = (png_charp)"Description";
mtkleind69ece62015-09-10 10:37:44 -0700747 text[1].text = (png_charp)description.c_str();
mtkleina5114d72015-08-24 13:27:01 -0700748 text[1].compression = PNG_TEXT_COMPRESSION_NONE;
749 png_set_text(png, info, text, 2);
750
751 png_init_io(png, f);
752 png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8,
753 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
754 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
755 png_write_info(png, info);
756 for (int j = 0; j < h; j++) {
757 png_bytep row = (png_bytep)(rgba.get() + w*j);
758 png_write_rows(png, &row, 1);
759 }
760 png_write_end(png, info);
761
762 png_destroy_write_struct(&png, &info);
763 fclose(f);
764 return true;
765}
766
mtkleina2ef6422015-01-15 13:44:22 -0800767static bool match(const char* needle, const char* haystack) {
halcanary96fcdcc2015-08-27 07:41:13 -0700768 return 0 == strcmp("_", needle) || nullptr != strstr(haystack, needle);
mtkleina2ef6422015-01-15 13:44:22 -0800769}
770
djsollen54416de2015-04-03 07:24:48 -0700771static ImplicitString is_blacklisted(const char* sink, const char* src,
772 const char* srcOptions, const char* name) {
mtklein0d243ff2015-04-03 07:51:00 -0700773 for (int i = 0; i < FLAGS_blacklist.count() - 3; i += 4) {
mtkleina2ef6422015-01-15 13:44:22 -0800774 if (match(FLAGS_blacklist[i+0], sink) &&
djsollen54416de2015-04-03 07:24:48 -0700775 match(FLAGS_blacklist[i+1], src) &&
776 match(FLAGS_blacklist[i+2], srcOptions) &&
777 match(FLAGS_blacklist[i+3], name)) {
778 return SkStringPrintf("%s %s %s %s",
779 FLAGS_blacklist[i+0], FLAGS_blacklist[i+1],
780 FLAGS_blacklist[i+2], FLAGS_blacklist[i+3]);
mtkleina2ef6422015-01-15 13:44:22 -0800781 }
782 }
783 return "";
784}
785
mtklein748ca3b2015-01-15 10:56:12 -0800786// The finest-grained unit of work we can run: draw a single Src into a single Sink,
787// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
788struct Task {
mtkleine0effd62015-07-29 06:37:28 -0700789 Task(const TaggedSrc& src, const TaggedSink& sink) : src(src), sink(sink) {}
790 const TaggedSrc& src;
791 const TaggedSink& sink;
mtklein748ca3b2015-01-15 10:56:12 -0800792
793 static void Run(Task* task) {
mtkleina2ef6422015-01-15 13:44:22 -0800794 SkString name = task->src->name();
mtkleine0effd62015-07-29 06:37:28 -0700795
796 // We'll skip drawing this Src/Sink pair if:
797 // - the Src vetoes the Sink;
798 // - this Src / Sink combination is on the blacklist;
799 // - it's a dry run.
mtklein99cab4e2015-07-31 06:43:04 -0700800 SkString note(task->src->veto(task->sink->flags()) ? " (veto)" : "");
msarett9e707a02015-09-01 14:57:57 -0700801 SkString whyBlacklisted = is_blacklisted(task->sink.tag, task->src.tag.c_str(),
802 task->src.options.c_str(), name.c_str());
mtkleinb37cb412015-03-18 05:27:14 -0700803 if (!whyBlacklisted.isEmpty()) {
804 note.appendf(" (--blacklist %s)", whyBlacklisted.c_str());
805 }
mtkleine0effd62015-07-29 06:37:28 -0700806
mtkleinb9eb4ac2015-02-02 18:26:03 -0800807 SkString log;
mtkleinf27f08b2015-11-03 06:54:24 -0800808 auto timerStart = now_ms();
mtkleine0effd62015-07-29 06:37:28 -0700809 if (!FLAGS_dryRun && note.isEmpty()) {
mtklein748ca3b2015-01-15 10:56:12 -0800810 SkBitmap bitmap;
811 SkDynamicMemoryWStream stream;
bsalomon821e10e2015-06-04 14:15:33 -0700812 if (FLAGS_pre_log) {
813 SkDebugf("\nRunning %s->%s", name.c_str(), task->sink.tag);
814 }
djsollen54416de2015-04-03 07:24:48 -0700815 start(task->sink.tag, task->src.tag, task->src.options, name.c_str());
mtkleinb9eb4ac2015-02-02 18:26:03 -0800816 Error err = task->sink->draw(*task->src, &bitmap, &stream, &log);
mtklein748ca3b2015-01-15 10:56:12 -0800817 if (!err.isEmpty()) {
mtkleinf27f08b2015-11-03 06:54:24 -0800818 auto elapsed = now_ms() - timerStart;
mtklein4089ef72015-03-05 08:40:28 -0800819 if (err.isFatal()) {
djsollen54416de2015-04-03 07:24:48 -0700820 fail(SkStringPrintf("%s %s %s %s: %s",
mtklein4089ef72015-03-05 08:40:28 -0800821 task->sink.tag,
msarett9e707a02015-09-01 14:57:57 -0700822 task->src.tag.c_str(),
823 task->src.options.c_str(),
mtklein4089ef72015-03-05 08:40:28 -0800824 name.c_str(),
825 err.c_str()));
mtkleinb37cb412015-03-18 05:27:14 -0700826 } else {
827 note.appendf(" (skipped: %s)", err.c_str());
mtklein4089ef72015-03-05 08:40:28 -0800828 }
mtkleinf27f08b2015-11-03 06:54:24 -0800829 done(elapsed, task->sink.tag, task->src.tag, task->src.options,
djsollen54416de2015-04-03 07:24:48 -0700830 name, note, log);
mtklein4089ef72015-03-05 08:40:28 -0800831 return;
mtklein748ca3b2015-01-15 10:56:12 -0800832 }
mtklein62bd1a62015-01-27 14:46:26 -0800833 SkAutoTDelete<SkStreamAsset> data(stream.detachAsStream());
834
835 SkString md5;
836 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
837 SkMD5 hash;
838 if (data->getLength()) {
839 hash.writeStream(data, data->getLength());
840 data->rewind();
841 } else {
mtklein38408462015-07-08 07:25:27 -0700842 // If we're BGRA (Linux, Windows), swizzle over to RGBA (Mac, Android).
843 // This helps eliminate multiple 0-pixel-diff hashes on gold.skia.org.
844 // (Android's general slow speed breaks the tie arbitrarily in RGBA's favor.)
845 // We might consider promoting 565 to RGBA too.
846 if (bitmap.colorType() == kBGRA_8888_SkColorType) {
847 SkBitmap swizzle;
848 SkAssertResult(bitmap.copyTo(&swizzle, kRGBA_8888_SkColorType));
849 hash.write(swizzle.getPixels(), swizzle.getSize());
850 } else {
851 hash.write(bitmap.getPixels(), bitmap.getSize());
852 }
mtklein62bd1a62015-01-27 14:46:26 -0800853 }
854 SkMD5::Digest digest;
855 hash.finish(digest);
856 for (int i = 0; i < 16; i++) {
857 md5.appendf("%02x", digest.data[i]);
858 }
859 }
860
861 if (!FLAGS_readPath.isEmpty() &&
msarett9e707a02015-09-01 14:57:57 -0700862 !gGold.contains(Gold(task->sink.tag, task->src.tag.c_str(),
863 task->src.options.c_str(), name, md5))) {
djsollen54416de2015-04-03 07:24:48 -0700864 fail(SkStringPrintf("%s not found for %s %s %s %s in %s",
mtklein62bd1a62015-01-27 14:46:26 -0800865 md5.c_str(),
866 task->sink.tag,
msarett9e707a02015-09-01 14:57:57 -0700867 task->src.tag.c_str(),
868 task->src.options.c_str(),
mtklein62bd1a62015-01-27 14:46:26 -0800869 name.c_str(),
870 FLAGS_readPath[0]));
871 }
872
mtkleinb0531a72015-04-07 13:38:48 -0700873 if (!FLAGS_writePath.isEmpty()) {
mtklein748ca3b2015-01-15 10:56:12 -0800874 const char* ext = task->sink->fileExtension();
mtklein62bd1a62015-01-27 14:46:26 -0800875 if (data->getLength()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700876 WriteToDisk(*task, md5, ext, data, data->getLength(), nullptr);
halcanary022afb82015-01-30 11:00:12 -0800877 SkASSERT(bitmap.drawsNothing());
878 } else if (!bitmap.drawsNothing()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700879 WriteToDisk(*task, md5, ext, nullptr, 0, &bitmap);
mtklein748ca3b2015-01-15 10:56:12 -0800880 }
881 }
882 }
mtkleinf27f08b2015-11-03 06:54:24 -0800883 done(now_ms()-timerStart, task->sink.tag, task->src.tag.c_str(), task->src.options.c_str(),
884 name, note, log);
mtklein748ca3b2015-01-15 10:56:12 -0800885 }
886
887 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -0800888 SkString md5,
889 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -0800890 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -0800891 const SkBitmap* bitmap) {
mtklein748ca3b2015-01-15 10:56:12 -0800892 JsonWriter::BitmapResult result;
djsollen54416de2015-04-03 07:24:48 -0700893 result.name = task.src->name();
894 result.config = task.sink.tag;
895 result.sourceType = task.src.tag;
896 result.sourceOptions = task.src.options;
897 result.ext = ext;
898 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -0800899 JsonWriter::AddBitmapResult(result);
900
mtkleinb0531a72015-04-07 13:38:48 -0700901 // If an MD5 is uninteresting, we want it noted in the JSON file,
902 // but don't want to dump it out as a .png (or whatever ext is).
903 if (gUninterestingHashes.contains(md5)) {
904 return;
905 }
906
mtklein748ca3b2015-01-15 10:56:12 -0800907 const char* dir = FLAGS_writePath[0];
908 if (0 == strcmp(dir, "@")) { // Needed for iOS.
909 dir = FLAGS_resourcePath[0];
910 }
911 sk_mkdir(dir);
912
913 SkString path;
914 if (FLAGS_nameByHash) {
915 path = SkOSPath::Join(dir, result.md5.c_str());
916 path.append(".");
917 path.append(ext);
918 if (sk_exists(path.c_str())) {
919 return; // Content-addressed. If it exists already, we're done.
920 }
921 } else {
922 path = SkOSPath::Join(dir, task.sink.tag);
923 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -0700924 path = SkOSPath::Join(path.c_str(), task.src.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800925 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -0700926 if (strcmp(task.src.options.c_str(), "") != 0) {
927 path = SkOSPath::Join(path.c_str(), task.src.options.c_str());
djsollen54416de2015-04-03 07:24:48 -0700928 sk_mkdir(path.c_str());
929 }
mtklein748ca3b2015-01-15 10:56:12 -0800930 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
931 path.append(".");
932 path.append(ext);
933 }
934
mtklein748ca3b2015-01-15 10:56:12 -0800935 if (bitmap) {
mtkleina5114d72015-08-24 13:27:01 -0700936 if (!dump_png(*bitmap, path.c_str(), result.md5.c_str())) {
mtklein748ca3b2015-01-15 10:56:12 -0800937 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
938 return;
939 }
940 } else {
mtkleina5114d72015-08-24 13:27:01 -0700941 SkFILEWStream file(path.c_str());
942 if (!file.isValid()) {
943 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
944 return;
945 }
mtklein748ca3b2015-01-15 10:56:12 -0800946 if (!file.writeStream(data, len)) {
947 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
948 return;
949 }
950 }
951 }
952};
953
954// Run all tasks in the same enclave serially on the same thread.
955// They can't possibly run concurrently with each other.
956static void run_enclave(SkTArray<Task>* tasks) {
957 for (int i = 0; i < tasks->count(); i++) {
958 Task::Run(tasks->begin() + i);
959 }
960}
961
962/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
963
964// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
965
mtklein55e88b22015-01-21 15:50:13 -0800966static SkTDArray<skiatest::Test> gThreadedTests, gGPUTests;
mtklein748ca3b2015-01-15 10:56:12 -0800967
968static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -0800969 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -0800970 return;
971 }
mtklein6393c062015-04-27 08:45:01 -0700972 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r; r = r->next()) {
973 if (!in_shard()) {
974 continue;
975 }
halcanary87f3ba42015-01-20 09:30:20 -0800976 // Despite its name, factory() is returning a reference to
977 // link-time static const POD data.
978 const skiatest::Test& test = r->factory();
979 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -0800980 continue;
981 }
halcanary87f3ba42015-01-20 09:30:20 -0800982 if (test.needsGpu && gpu_supported()) {
mtklein55e88b22015-01-21 15:50:13 -0800983 (FLAGS_gpu_threading ? gThreadedTests : gGPUTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -0800984 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein55e88b22015-01-21 15:50:13 -0800985 gThreadedTests.push(test);
mtklein82d28432015-01-15 12:46:02 -0800986 }
mtklein748ca3b2015-01-15 10:56:12 -0800987 }
988}
989
halcanary87f3ba42015-01-20 09:30:20 -0800990static void run_test(skiatest::Test* test) {
991 struct : public skiatest::Reporter {
mtklein36352bf2015-03-25 18:17:31 -0700992 void reportFailed(const skiatest::Failure& failure) override {
halcanary87f3ba42015-01-20 09:30:20 -0800993 fail(failure.toString());
994 JsonWriter::AddTestFailure(failure);
995 }
mtklein36352bf2015-03-25 18:17:31 -0700996 bool allowExtendedTest() const override {
halcanary87f3ba42015-01-20 09:30:20 -0800997 return FLAGS_pathOpsExtended;
998 }
mtklein36352bf2015-03-25 18:17:31 -0700999 bool verbose() const override { return FLAGS_veryVerbose; }
halcanary87f3ba42015-01-20 09:30:20 -08001000 } reporter;
djsollen824996a2015-06-12 12:06:22 -07001001
1002 SkString note;
1003 SkString whyBlacklisted = is_blacklisted("_", "tests", "_", test->name);
1004 if (!whyBlacklisted.isEmpty()) {
1005 note.appendf(" (--blacklist %s)", whyBlacklisted.c_str());
1006 }
1007
mtkleinf27f08b2015-11-03 06:54:24 -08001008 auto timerStart = now_ms();
djsollen824996a2015-06-12 12:06:22 -07001009 if (!FLAGS_dryRun && whyBlacklisted.isEmpty()) {
djsollen54416de2015-04-03 07:24:48 -07001010 start("unit", "test", "", test->name);
mtklein55e88b22015-01-21 15:50:13 -08001011 GrContextFactory factory;
bsalomon821e10e2015-06-04 14:15:33 -07001012 if (FLAGS_pre_log) {
1013 SkDebugf("\nRunning test %s", test->name);
1014 }
mtklein55e88b22015-01-21 15:50:13 -08001015 test->proc(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -08001016 }
mtkleinf27f08b2015-11-03 06:54:24 -08001017 done(now_ms()-timerStart, "unit", "test", "", test->name, note, "");
mtklein748ca3b2015-01-15 10:56:12 -08001018}
1019
1020/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1021
mtklein55e88b22015-01-21 15:50:13 -08001022// If we're isolating all GPU-bound work to one thread (the default), this function runs all that.
1023static void run_enclave_and_gpu_tests(SkTArray<Task>* tasks) {
1024 run_enclave(tasks);
1025 for (int i = 0; i < gGPUTests.count(); i++) {
1026 run_test(&gGPUTests[i]);
1027 }
1028}
1029
mtkleinde6fc2b2015-03-12 06:28:54 -07001030// Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung.
1031// This prints something every once in a while so that it knows we're still working.
mtklein2e1c47e2015-03-12 07:16:56 -07001032static void start_keepalive() {
1033 struct Loop {
1034 static void forever(void*) {
1035 for (;;) {
1036 static const int kSec = 300;
1037 #if defined(SK_BUILD_FOR_WIN)
1038 Sleep(kSec * 1000);
1039 #else
1040 sleep(kSec);
1041 #endif
mtkleinb37cb412015-03-18 05:27:14 -07001042 SkString running;
1043 {
1044 SkAutoMutexAcquire lock(gRunningMutex);
1045 for (int i = 0; i < gRunning.count(); i++) {
1046 running.appendf("\n\t%s", gRunning[i].c_str());
1047 }
1048 }
1049 SkDebugf("\nCurrently running:%s\n", running.c_str());
mtklein2e1c47e2015-03-12 07:16:56 -07001050 }
1051 }
1052 };
1053 static SkThread* intentionallyLeaked = new SkThread(Loop::forever);
1054 intentionallyLeaked->start();
mtkleinde6fc2b2015-03-12 06:28:54 -07001055}
1056
caryclark83ca6282015-06-10 09:31:09 -07001057#define PORTABLE_FONT_PREFIX "Toy Liberation "
1058
1059static SkTypeface* create_from_name(const char familyName[], SkTypeface::Style style) {
1060 if (familyName && strlen(familyName) > sizeof(PORTABLE_FONT_PREFIX)
1061 && !strncmp(familyName, PORTABLE_FONT_PREFIX, sizeof(PORTABLE_FONT_PREFIX) - 1)) {
caryclark1818acb2015-07-24 12:09:25 -07001062 return sk_tool_utils::create_portable_typeface(familyName, style);
caryclark83ca6282015-06-10 09:31:09 -07001063 }
halcanary96fcdcc2015-08-27 07:41:13 -07001064 return nullptr;
caryclark83ca6282015-06-10 09:31:09 -07001065}
1066
1067#undef PORTABLE_FONT_PREFIX
1068
1069extern SkTypeface* (*gCreateTypefaceDelegate)(const char [], SkTypeface::Style );
1070
jcgregorio3b27ade2014-11-13 08:06:40 -08001071int dm_main();
caryclark17f0b6d2014-07-22 10:15:34 -07001072int dm_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -08001073 SetupCrashHandler();
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +00001074 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -07001075 SkTaskGroup::Enabler enabled(FLAGS_threads);
caryclark83ca6282015-06-10 09:31:09 -07001076 gCreateTypefaceDelegate = &create_from_name;
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +00001077
mtklein2e1c47e2015-03-12 07:16:56 -07001078 start_keepalive();
mtkleinde6fc2b2015-03-12 06:28:54 -07001079
mtklein62bd1a62015-01-27 14:46:26 -08001080 gather_gold();
borenet09ed4802015-04-03 14:15:33 -07001081 gather_uninteresting_hashes();
mtklein62bd1a62015-01-27 14:46:26 -08001082
mtklein748ca3b2015-01-15 10:56:12 -08001083 gather_srcs();
1084 gather_sinks();
1085 gather_tests();
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +00001086
mtklein55e88b22015-01-21 15:50:13 -08001087 gPending = gSrcs.count() * gSinks.count() + gThreadedTests.count() + gGPUTests.count();
mtklein748ca3b2015-01-15 10:56:12 -08001088 SkDebugf("%d srcs * %d sinks + %d tests == %d tasks\n",
mtklein55e88b22015-01-21 15:50:13 -08001089 gSrcs.count(), gSinks.count(), gThreadedTests.count() + gGPUTests.count(), gPending);
mtklein748ca3b2015-01-15 10:56:12 -08001090
1091 // We try to exploit as much parallelism as is safe. Most Src/Sink pairs run on any thread,
1092 // but Sinks that identify as part of a particular enclave run serially on a single thread.
mtklein82d28432015-01-15 12:46:02 -08001093 // CPU tests run on any thread. GPU tests depend on --gpu_threading.
mtklein748ca3b2015-01-15 10:56:12 -08001094 SkTArray<Task> enclaves[kNumEnclaves];
1095 for (int j = 0; j < gSinks.count(); j++) {
1096 SkTArray<Task>& tasks = enclaves[gSinks[j]->enclave()];
1097 for (int i = 0; i < gSrcs.count(); i++) {
1098 tasks.push_back(Task(gSrcs[i], gSinks[j]));
1099 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001100 }
1101
mtklein748ca3b2015-01-15 10:56:12 -08001102 SkTaskGroup tg;
mtklein55e88b22015-01-21 15:50:13 -08001103 tg.batch(run_test, gThreadedTests.begin(), gThreadedTests.count());
1104 for (int i = 0; i < kNumEnclaves; i++) {
1105 switch(i) {
1106 case kAnyThread_Enclave:
1107 tg.batch(Task::Run, enclaves[i].begin(), enclaves[i].count());
1108 break;
1109 case kGPU_Enclave:
1110 tg.add(run_enclave_and_gpu_tests, &enclaves[i]);
1111 break;
1112 default:
1113 tg.add(run_enclave, &enclaves[i]);
1114 break;
mtklein82d28432015-01-15 12:46:02 -08001115 }
mtklein55e88b22015-01-21 15:50:13 -08001116 }
mtklein748ca3b2015-01-15 10:56:12 -08001117 tg.wait();
mtklein748ca3b2015-01-15 10:56:12 -08001118 // At this point we're back in single-threaded land.
caryclarkf53ce802015-06-15 06:48:30 -07001119 sk_tool_utils::release_portable_typefaces();
mtklein@google.comd36522d2013-10-16 13:02:15 +00001120
mtklein2f64eec2015-01-15 14:20:41 -08001121 SkDebugf("\n");
mtklein748ca3b2015-01-15 10:56:12 -08001122 if (gFailures.count() > 0) {
1123 SkDebugf("Failures:\n");
1124 for (int i = 0; i < gFailures.count(); i++) {
mtklein9dc09102015-01-15 15:47:33 -08001125 SkDebugf("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001126 }
1127 SkDebugf("%d failures\n", gFailures.count());
1128 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001129 }
mtklein748ca3b2015-01-15 10:56:12 -08001130 if (gPending > 0) {
1131 SkDebugf("Hrm, we didn't seem to run everything we intended to! Please file a bug.\n");
1132 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001133 }
halcanary7a14b312015-10-01 07:28:13 -07001134 #ifdef SK_PDF_IMAGE_STATS
1135 SkPDFImageDumpStats();
1136 #endif // SK_PDF_IMAGE_STATS
mtklein748ca3b2015-01-15 10:56:12 -08001137 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +00001138}
jcgregorio3b27ade2014-11-13 08:06:40 -08001139
kkinnunen179a8f52015-11-20 13:32:24 -08001140// TODO: currently many GPU tests are declared outside SK_SUPPORT_GPU guards.
1141// Thus we export the empty RunWithGPUTestContexts when SK_SUPPORT_GPU=0.
1142namespace skiatest {
1143namespace {
1144typedef void(*TestWithGrContext)(skiatest::Reporter*, GrContext*);
1145typedef void(*TestWithGrContextAndGLContext)(skiatest::Reporter*, GrContext*, SkGLContext*);
1146#if SK_SUPPORT_GPU
1147template<typename T>
1148void call_test(T test, skiatest::Reporter* reporter, GrContextFactory::ContextInfo* context);
1149template<>
1150void call_test(TestWithGrContext test, skiatest::Reporter* reporter,
1151 GrContextFactory::ContextInfo* context) {
1152 test(reporter, context->fGrContext);
1153}
1154template<>
1155void call_test(TestWithGrContextAndGLContext test, skiatest::Reporter* reporter,
1156 GrContextFactory::ContextInfo* context) {
1157 test(reporter, context->fGrContext, context->fGLContext);
1158}
1159#endif
1160} // namespace
1161
1162
1163template<typename T>
1164void RunWithGPUTestContexts(T test, GPUTestContexts testContexts, Reporter* reporter,
1165 GrContextFactory* factory) {
1166#if SK_SUPPORT_GPU
1167 for (int i = 0; i < GrContextFactory::kGLContextTypeCnt; ++i) {
1168 GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLContextType) i;
1169 int contextSelector = kNone_GPUTestContexts;
1170 if (GrContextFactory::IsRenderingGLContext(glCtxType)) {
1171 contextSelector |= kAllRendering_GPUTestContexts;
1172 }
1173 if (glCtxType == GrContextFactory::kNative_GLContextType) {
1174 contextSelector |= kNative_GPUTestContexts;
1175 }
1176 if (glCtxType == GrContextFactory::kNull_GLContextType) {
1177 contextSelector |= kNull_GPUTestContexts;
1178 }
1179 if ((testContexts & contextSelector) == 0) {
1180 continue;
1181 }
bsalomona0a024e2015-12-01 07:58:44 -08001182 if (GrContextFactory::ContextInfo* context = factory->getContextInfo(glCtxType)) {
kkinnunen179a8f52015-11-20 13:32:24 -08001183 call_test(test, reporter, context);
1184 }
1185 }
1186#endif
1187}
1188
1189template
1190void RunWithGPUTestContexts<TestWithGrContext>(TestWithGrContext test,
1191 GPUTestContexts testContexts,
1192 Reporter* reporter,
1193 GrContextFactory* factory);
1194template
1195void RunWithGPUTestContexts<TestWithGrContextAndGLContext>(TestWithGrContextAndGLContext test,
1196 GPUTestContexts testContexts,
1197 Reporter* reporter,
1198 GrContextFactory* factory);
1199} // namespace skiatest
1200
borenet48087572015-04-02 12:16:36 -07001201#if !defined(SK_BUILD_FOR_IOS)
jcgregorio3b27ade2014-11-13 08:06:40 -08001202int main(int argc, char** argv) {
1203 SkCommandLineFlags::Parse(argc, argv);
1204 return dm_main();
1205}
1206#endif