blob: 6810dc5a8082ced5ab18034dc39334491bac5fcc [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"
kkinnunen3e980c32015-12-23 01:33:00 -080018#include "SkCommonFlagsConfig.h"
caryclark83ca6282015-06-10 09:31:09 -070019#include "SkFontMgr.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +000020#include "SkForceLinking.h"
21#include "SkGraphics.h"
mtklein748ca3b2015-01-15 10:56:12 -080022#include "SkMD5.h"
mtklein1b249332015-07-07 12:21:21 -070023#include "SkMutex.h"
mtklein1d0f1642014-09-08 08:05:18 -070024#include "SkOSFile.h"
mtkleina82f5622015-02-20 12:30:19 -080025#include "SkTHash.h"
mtklein406654b2014-09-03 15:34:37 -070026#include "SkTaskGroup.h"
mtkleinde6fc2b2015-03-12 06:28:54 -070027#include "SkThreadUtils.h"
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +000028#include "Test.h"
mtklein748ca3b2015-01-15 10:56:12 -080029#include "Timer.h"
caryclark83ca6282015-06-10 09:31:09 -070030#include "sk_tool_utils.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +000031
halcanary7a14b312015-10-01 07:28:13 -070032#ifdef SK_PDF_IMAGE_STATS
33extern void SkPDFImageDumpStats();
34#endif
35
mtkleina5114d72015-08-24 13:27:01 -070036#include "png.h"
37
bungeman60e0fee2015-08-26 05:15:46 -070038#include <stdlib.h>
39
scroggo27a58342015-10-28 08:56:41 -070040#ifndef SK_BUILD_FOR_WIN32
41 #include <unistd.h>
42#endif
43
djsollen54416de2015-04-03 07:24:48 -070044DEFINE_string(src, "tests gm skp image", "Source types to test.");
mtklein748ca3b2015-01-15 10:56:12 -080045DEFINE_bool(nameByHash, false,
46 "If true, write to FLAGS_writePath[0]/<hash>.png instead of "
djsollen54416de2015-04-03 07:24:48 -070047 "to FLAGS_writePath[0]/<config>/<sourceType>/<sourceOptions>/<name>.png");
mtklein748ca3b2015-01-15 10:56:12 -080048DEFINE_bool2(pathOpsExtended, x, false, "Run extended pathOps tests.");
mtkleind603b222015-02-17 11:13:33 -080049DEFINE_string(matrix, "1 0 0 1",
50 "2x2 scale+skew matrix to apply or upright when using "
51 "'matrix' or 'upright' in config.");
mtklein82d28432015-01-15 12:46:02 -080052DEFINE_bool(gpu_threading, false, "Allow GPU work to run on multiple threads?");
mtklein@google.comd36522d2013-10-16 13:02:15 +000053
mtkleina2ef6422015-01-15 13:44:22 -080054DEFINE_string(blacklist, "",
djsollen54416de2015-04-03 07:24:48 -070055 "Space-separated config/src/srcOptions/name quadruples to blacklist. '_' matches anything. E.g. \n"
56 "'--blacklist gpu skp _ _' will blacklist all SKPs drawn into the gpu config.\n"
57 "'--blacklist gpu skp _ _ 8888 gm _ aarects' will also blacklist the aarects GM on 8888.");
mtkleina2ef6422015-01-15 13:44:22 -080058
mtklein62bd1a62015-01-27 14:46:26 -080059DEFINE_string2(readPath, r, "", "If set check for equality with golden results in this directory.");
60
borenet09ed4802015-04-03 14:15:33 -070061DEFINE_string(uninterestingHashesFile, "",
62 "File containing a list of uninteresting hashes. If a result hashes to something in "
63 "this list, no image is written for that result.");
64
mtklein6393c062015-04-27 08:45:01 -070065DEFINE_int32(shards, 1, "We're splitting source data into this many shards.");
66DEFINE_int32(shard, 0, "Which shard do I run?");
67
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
mtkleinbc4e0032015-12-09 08:37:35 -080086SK_DECLARE_STATIC_MUTEX(gRunningAndTallyMutex);
87static SkTArray<SkString> gRunning;
88static SkTHashMap<SkString, int> gNoteTally;
mtklein748ca3b2015-01-15 10:56:12 -080089
mtkleinb9eb4ac2015-02-02 18:26:03 -080090static void done(double ms,
djsollen54416de2015-04-03 07:24:48 -070091 ImplicitString config, ImplicitString src, ImplicitString srcOptions,
92 ImplicitString name, ImplicitString note, ImplicitString log) {
93 SkString id = SkStringPrintf("%s %s %s %s", config.c_str(), src.c_str(),
94 srcOptions.c_str(), name.c_str());
mtkleinb37cb412015-03-18 05:27:14 -070095 {
mtkleinbc4e0032015-12-09 08:37:35 -080096 SkAutoMutexAcquire lock(gRunningAndTallyMutex);
mtkleinb37cb412015-03-18 05:27:14 -070097 for (int i = 0; i < gRunning.count(); i++) {
98 if (gRunning[i] == id) {
99 gRunning.removeShuffle(i);
100 break;
101 }
102 }
mtkleinbc4e0032015-12-09 08:37:35 -0800103 if (!note.isEmpty()) {
104 if (int* tally = gNoteTally.find(note)) {
105 *tally += 1;
106 } else {
107 gNoteTally.set(note, 1);
108 }
109 }
mtkleinb37cb412015-03-18 05:27:14 -0700110 }
mtkleinb9eb4ac2015-02-02 18:26:03 -0800111 if (!log.isEmpty()) {
112 log.prepend("\n");
113 }
mtklein6dee2ad2015-02-05 09:53:44 -0800114 auto pending = sk_atomic_dec(&gPending)-1;
mtkleinbc4e0032015-12-09 08:37:35 -0800115 if (!FLAGS_quiet && note.isEmpty()) {
116 SkDebugf("%s(%4d/%-4dMB %6d) %s\t%s%s", FLAGS_verbose ? "\n" : kSkOverwriteLine
reed50bc0512015-05-19 14:13:31 -0700117 , sk_tools::getCurrResidentSetSizeMB()
118 , sk_tools::getMaxResidentSetSizeMB()
119 , pending
120 , HumanizeMs(ms).c_str()
121 , id.c_str()
reed50bc0512015-05-19 14:13:31 -0700122 , log.c_str());
123 }
mtkleina17241b2015-01-23 05:48:00 -0800124 // We write our dm.json file every once in a while in case we crash.
125 // Notice this also handles the final dm.json when pending == 0.
126 if (pending % 500 == 0) {
127 JsonWriter::DumpJson();
128 }
mtklein@google.comd36522d2013-10-16 13:02:15 +0000129}
130
djsollen54416de2015-04-03 07:24:48 -0700131static void start(ImplicitString config, ImplicitString src,
132 ImplicitString srcOptions, ImplicitString name) {
133 SkString id = SkStringPrintf("%s %s %s %s", config.c_str(), src.c_str(),
134 srcOptions.c_str(), name.c_str());
mtkleinbc4e0032015-12-09 08:37:35 -0800135 SkAutoMutexAcquire lock(gRunningAndTallyMutex);
mtkleinb37cb412015-03-18 05:27:14 -0700136 gRunning.push_back(id);
137}
138
mtklein748ca3b2015-01-15 10:56:12 -0800139/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
mtklein114c3cd2015-01-15 10:15:02 -0800140
mtklein62bd1a62015-01-27 14:46:26 -0800141struct Gold : public SkString {
mtkleina82f5622015-02-20 12:30:19 -0800142 Gold() : SkString("") {}
djsollen54416de2015-04-03 07:24:48 -0700143 Gold(ImplicitString sink, ImplicitString src, ImplicitString srcOptions,
144 ImplicitString name, ImplicitString md5)
mtklein62bd1a62015-01-27 14:46:26 -0800145 : SkString("") {
146 this->append(sink);
147 this->append(src);
djsollen54416de2015-04-03 07:24:48 -0700148 this->append(srcOptions);
mtklein62bd1a62015-01-27 14:46:26 -0800149 this->append(name);
150 this->append(md5);
mtklein62bd1a62015-01-27 14:46:26 -0800151 }
mtkleinc8d1dd42015-10-15 12:23:01 -0700152 struct Hash {
153 uint32_t operator()(const Gold& g) const {
154 return SkGoodHash()((const SkString&)g);
155 }
156 };
mtklein62bd1a62015-01-27 14:46:26 -0800157};
mtkleina82f5622015-02-20 12:30:19 -0800158static SkTHashSet<Gold, Gold::Hash> gGold;
mtklein62bd1a62015-01-27 14:46:26 -0800159
160static void add_gold(JsonWriter::BitmapResult r) {
djsollen54416de2015-04-03 07:24:48 -0700161 gGold.add(Gold(r.config, r.sourceType, r.sourceOptions, r.name, r.md5));
mtklein62bd1a62015-01-27 14:46:26 -0800162}
163
164static void gather_gold() {
165 if (!FLAGS_readPath.isEmpty()) {
166 SkString path(FLAGS_readPath[0]);
167 path.append("/dm.json");
168 if (!JsonWriter::ReadJson(path.c_str(), add_gold)) {
169 fail(SkStringPrintf("Couldn't read %s for golden results.", path.c_str()));
170 }
171 }
172}
173
174/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
175
borenet09ed4802015-04-03 14:15:33 -0700176static SkTHashSet<SkString> gUninterestingHashes;
177
178static void gather_uninteresting_hashes() {
179 if (!FLAGS_uninterestingHashesFile.isEmpty()) {
180 SkAutoTUnref<SkData> data(SkData::NewFromFileName(FLAGS_uninterestingHashesFile[0]));
mtkleincc334b32015-09-22 11:43:53 -0700181 if (!data) {
182 SkDebugf("WARNING: unable to read uninteresting hashes from %s\n",
183 FLAGS_uninterestingHashesFile[0]);
184 return;
185 }
borenet09ed4802015-04-03 14:15:33 -0700186 SkTArray<SkString> hashes;
187 SkStrSplit((const char*)data->data(), "\n", &hashes);
188 for (const SkString& hash : hashes) {
189 gUninterestingHashes.add(hash);
190 }
mtklein136baaa2016-02-03 11:21:45 -0800191 SkDebugf("FYI: loaded %d distinct uninteresting hashes from %d lines\n",
192 gUninterestingHashes.count(), hashes.count());
borenet09ed4802015-04-03 14:15:33 -0700193 }
194}
195
196/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
197
mtkleine0effd62015-07-29 06:37:28 -0700198struct TaggedSrc : public SkAutoTDelete<Src> {
msarett9e707a02015-09-01 14:57:57 -0700199 ImplicitString tag;
200 ImplicitString options;
mtkleine0effd62015-07-29 06:37:28 -0700201};
202
203struct TaggedSink : public SkAutoTDelete<Sink> {
kkinnunen3e980c32015-12-23 01:33:00 -0800204 SkString tag;
djsollen54416de2015-04-03 07:24:48 -0700205};
mtklein748ca3b2015-01-15 10:56:12 -0800206
207static const bool kMemcpyOK = true;
208
mtkleine0effd62015-07-29 06:37:28 -0700209static SkTArray<TaggedSrc, kMemcpyOK> gSrcs;
210static SkTArray<TaggedSink, kMemcpyOK> gSinks;
mtklein748ca3b2015-01-15 10:56:12 -0800211
mtklein6393c062015-04-27 08:45:01 -0700212static bool in_shard() {
213 static int N = 0;
214 return N++ % FLAGS_shards == FLAGS_shard;
215}
216
msarett9e707a02015-09-01 14:57:57 -0700217static void push_src(ImplicitString tag, ImplicitString options, Src* s) {
mtklein748ca3b2015-01-15 10:56:12 -0800218 SkAutoTDelete<Src> src(s);
mtklein6393c062015-04-27 08:45:01 -0700219 if (in_shard() &&
msarett9e707a02015-09-01 14:57:57 -0700220 FLAGS_src.contains(tag.c_str()) &&
mtklein748ca3b2015-01-15 10:56:12 -0800221 !SkCommandLineFlags::ShouldSkip(FLAGS_match, src->name().c_str())) {
mtkleine0effd62015-07-29 06:37:28 -0700222 TaggedSrc& s = gSrcs.push_back();
mtklein748ca3b2015-01-15 10:56:12 -0800223 s.reset(src.detach());
224 s.tag = tag;
djsollen54416de2015-04-03 07:24:48 -0700225 s.options = options;
mtklein114c3cd2015-01-15 10:15:02 -0800226 }
mtklein748ca3b2015-01-15 10:56:12 -0800227}
mtklein114c3cd2015-01-15 10:15:02 -0800228
msarett9e707a02015-09-01 14:57:57 -0700229static void push_codec_src(Path path, CodecSrc::Mode mode, CodecSrc::DstColorType dstColorType,
scroggoc5560be2016-02-03 09:42:42 -0800230 SkAlphaType dstAlphaType, float scale) {
msarett9e707a02015-09-01 14:57:57 -0700231 SkString folder;
232 switch (mode) {
233 case CodecSrc::kCodec_Mode:
234 folder.append("codec");
235 break;
msarettbb25b532016-01-13 09:31:39 -0800236 case CodecSrc::kCodecZeroInit_Mode:
237 folder.append("codec_zero_init");
238 break;
msarett9e707a02015-09-01 14:57:57 -0700239 case CodecSrc::kScanline_Mode:
240 folder.append("scanline");
241 break;
msarett9e707a02015-09-01 14:57:57 -0700242 case CodecSrc::kStripe_Mode:
243 folder.append("stripe");
244 break;
msarett91c22b22016-02-22 12:27:46 -0800245 case CodecSrc::kCroppedScanline_Mode:
246 folder.append("crop");
247 break;
msarett9e707a02015-09-01 14:57:57 -0700248 case CodecSrc::kSubset_Mode:
msarett36c37962015-09-02 13:20:52 -0700249 folder.append("codec_subset");
msarett9e707a02015-09-01 14:57:57 -0700250 break;
msarettb714fb02016-01-22 14:46:42 -0800251 case CodecSrc::kGen_Mode:
252 folder.append("gen");
253 break;
msarett9e707a02015-09-01 14:57:57 -0700254 }
255
256 switch (dstColorType) {
257 case CodecSrc::kGrayscale_Always_DstColorType:
258 folder.append("_kGray8");
259 break;
260 case CodecSrc::kIndex8_Always_DstColorType:
261 folder.append("_kIndex8");
262 break;
263 default:
264 break;
265 }
266
scroggoc5560be2016-02-03 09:42:42 -0800267 switch (dstAlphaType) {
268 case kOpaque_SkAlphaType:
269 folder.append("_opaque");
270 break;
271 case kPremul_SkAlphaType:
272 folder.append("_premul");
273 break;
274 case kUnpremul_SkAlphaType:
275 folder.append("_unpremul");
276 break;
277 default:
278 break;
279 }
280
msarett9e707a02015-09-01 14:57:57 -0700281 if (1.0f != scale) {
282 folder.appendf("_%.3f", scale);
283 }
284
scroggoc5560be2016-02-03 09:42:42 -0800285 CodecSrc* src = new CodecSrc(path, mode, dstColorType, dstAlphaType, scale);
msarett9e707a02015-09-01 14:57:57 -0700286 push_src("image", folder, src);
287}
288
msarett3d9d7a72015-10-21 10:27:10 -0700289static void push_android_codec_src(Path path, AndroidCodecSrc::Mode mode,
scroggoc5560be2016-02-03 09:42:42 -0800290 CodecSrc::DstColorType dstColorType, SkAlphaType dstAlphaType, int sampleSize) {
msarett3d9d7a72015-10-21 10:27:10 -0700291 SkString folder;
292 switch (mode) {
293 case AndroidCodecSrc::kFullImage_Mode:
294 folder.append("scaled_codec");
295 break;
296 case AndroidCodecSrc::kDivisor_Mode:
297 folder.append("scaled_codec_divisor");
298 break;
299 }
300
301 switch (dstColorType) {
302 case CodecSrc::kGrayscale_Always_DstColorType:
303 folder.append("_kGray8");
304 break;
305 case CodecSrc::kIndex8_Always_DstColorType:
306 folder.append("_kIndex8");
307 break;
308 default:
309 break;
310 }
311
scroggoc5560be2016-02-03 09:42:42 -0800312 switch (dstAlphaType) {
313 case kOpaque_SkAlphaType:
314 folder.append("_opaque");
315 break;
316 case kPremul_SkAlphaType:
317 folder.append("_premul");
318 break;
msarett9e9444c2016-02-03 12:39:10 -0800319 case kUnpremul_SkAlphaType:
320 folder.append("_unpremul");
321 break;
scroggoc5560be2016-02-03 09:42:42 -0800322 default:
323 break;
324 }
325
msarett3d9d7a72015-10-21 10:27:10 -0700326 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800327 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700328 }
329
scroggoc5560be2016-02-03 09:42:42 -0800330 AndroidCodecSrc* src = new AndroidCodecSrc(path, mode, dstColorType, dstAlphaType, sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700331 push_src("image", folder, src);
332}
333
msarett438b2ad2015-04-09 12:43:10 -0700334static void push_codec_srcs(Path path) {
335 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
336 if (!encoded) {
337 SkDebugf("Couldn't read %s.", path.c_str());
338 return;
339 }
340 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
halcanary96fcdcc2015-08-27 07:41:13 -0700341 if (nullptr == codec.get()) {
msarett438b2ad2015-04-09 12:43:10 -0700342 SkDebugf("Couldn't create codec for %s.", path.c_str());
343 return;
344 }
345
msarett36c37962015-09-02 13:20:52 -0700346 // Native Scales
msarett0a242972015-06-11 14:27:27 -0700347 // TODO (msarett): Implement scaling tests for SkImageDecoder in order to compare with these
348 // tests. SkImageDecoder supports downscales by integer factors.
emmaleer8f4ba762015-08-14 07:44:46 -0700349 // 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 -0700350 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 -0700351
msarett91c22b22016-02-22 12:27:46 -0800352 SkTArray<CodecSrc::Mode> nativeModes;
353 nativeModes.push_back(CodecSrc::kCodec_Mode);
354 nativeModes.push_back(CodecSrc::kCodecZeroInit_Mode);
355 nativeModes.push_back(CodecSrc::kGen_Mode);
356 switch (codec->getEncodedFormat()) {
357 case SkEncodedFormat::kJPEG_SkEncodedFormat:
358 nativeModes.push_back(CodecSrc::kScanline_Mode);
359 nativeModes.push_back(CodecSrc::kStripe_Mode);
360 nativeModes.push_back(CodecSrc::kCroppedScanline_Mode);
361 break;
362 case SkEncodedFormat::kWEBP_SkEncodedFormat:
363 nativeModes.push_back(CodecSrc::kSubset_Mode);
364 break;
365 default:
366 nativeModes.push_back(CodecSrc::kScanline_Mode);
367 nativeModes.push_back(CodecSrc::kStripe_Mode);
368 break;
369 }
msarett9e707a02015-09-01 14:57:57 -0700370
msarett91c22b22016-02-22 12:27:46 -0800371 SkTArray<CodecSrc::DstColorType> colorTypes;
372 colorTypes.push_back(CodecSrc::kGetFromCanvas_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700373 switch (codec->getInfo().colorType()) {
374 case kGray_8_SkColorType:
msarett91c22b22016-02-22 12:27:46 -0800375 colorTypes.push_back(CodecSrc::kGrayscale_Always_DstColorType);
msarett55f7bdd2016-02-16 13:24:54 -0800376 if (kWBMP_SkEncodedFormat == codec->getEncodedFormat()) {
msarett91c22b22016-02-22 12:27:46 -0800377 colorTypes.push_back(CodecSrc::kIndex8_Always_DstColorType);
msarett55f7bdd2016-02-16 13:24:54 -0800378 }
msarett36c37962015-09-02 13:20:52 -0700379 break;
380 case kIndex_8_SkColorType:
msarett91c22b22016-02-22 12:27:46 -0800381 colorTypes.push_back(CodecSrc::kIndex8_Always_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700382 break;
383 default:
msarett36c37962015-09-02 13:20:52 -0700384 break;
385 }
msarett9e707a02015-09-01 14:57:57 -0700386
scroggoc5560be2016-02-03 09:42:42 -0800387 SkTArray<SkAlphaType> alphaModes;
388 alphaModes.push_back(kPremul_SkAlphaType);
msarett9e9444c2016-02-03 12:39:10 -0800389 alphaModes.push_back(kUnpremul_SkAlphaType);
scroggoc5560be2016-02-03 09:42:42 -0800390 if (codec->getInfo().alphaType() == kOpaque_SkAlphaType) {
391 alphaModes.push_back(kOpaque_SkAlphaType);
392 }
msarettb714fb02016-01-22 14:46:42 -0800393
394 for (CodecSrc::Mode mode : nativeModes) {
395 // SkCodecImageGenerator only runs for the default colorType
396 // recommended by SkCodec. There is no need to generate multiple
397 // tests for different colorTypes.
398 // TODO (msarett): Add scaling support to SkCodecImageGenerator.
399 if (CodecSrc::kGen_Mode == mode) {
400 // FIXME: The gpu backend does not draw kGray sources correctly. (skbug.com/4822)
401 if (kGray_8_SkColorType != codec->getInfo().colorType()) {
scroggoc5560be2016-02-03 09:42:42 -0800402 push_codec_src(path, mode, CodecSrc::kGetFromCanvas_DstColorType,
403 codec->getInfo().alphaType(), 1.0f);
msarettb714fb02016-01-22 14:46:42 -0800404 }
405 continue;
406 }
407
408 for (float scale : nativeScales) {
msarett91c22b22016-02-22 12:27:46 -0800409 for (CodecSrc::DstColorType colorType : colorTypes) {
scroggoc5560be2016-02-03 09:42:42 -0800410 for (SkAlphaType alphaType : alphaModes) {
msarett91c22b22016-02-22 12:27:46 -0800411 // Only test kCroppedScanline_Mode when the alpha type is opaque. The test is
412 // slow and won't be interestingly different with different alpha types.
413 if (CodecSrc::kCroppedScanline_Mode == mode &&
414 kOpaque_SkAlphaType != alphaType) {
415 continue;
416 }
417
418 push_codec_src(path, mode, colorType, alphaType, scale);
scroggoc5560be2016-02-03 09:42:42 -0800419 }
msarett9e707a02015-09-01 14:57:57 -0700420 }
421 }
msarett0a242972015-06-11 14:27:27 -0700422 }
msarett36c37962015-09-02 13:20:52 -0700423
halcanary6950de62015-11-07 05:29:00 -0800424 // https://bug.skia.org/4428
msarett33c76232015-11-16 13:43:40 -0800425 bool subset = false;
426 // The following image types are supported by BitmapRegionDecoder,
427 // so we will test full image decodes and subset decodes.
msarettbe8216a2015-12-04 08:00:50 -0800428 static const char* const exts[] = {
msarett3d9d7a72015-10-21 10:27:10 -0700429 "jpg", "jpeg", "png", "webp",
430 "JPG", "JPEG", "PNG", "WEBP",
431 };
msarettbe8216a2015-12-04 08:00:50 -0800432 for (const char* ext : exts) {
msarett3d9d7a72015-10-21 10:27:10 -0700433 if (path.endsWith(ext)) {
msarett33c76232015-11-16 13:43:40 -0800434 subset = true;
msarett3d9d7a72015-10-21 10:27:10 -0700435 break;
436 }
437 }
msarett33c76232015-11-16 13:43:40 -0800438
msarett3d9d7a72015-10-21 10:27:10 -0700439 const int sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
msarett36c37962015-09-02 13:20:52 -0700440
msarett3d9d7a72015-10-21 10:27:10 -0700441 for (int sampleSize : sampleSizes) {
msarett91c22b22016-02-22 12:27:46 -0800442 for (CodecSrc::DstColorType colorType : colorTypes) {
scroggoc5560be2016-02-03 09:42:42 -0800443 for (SkAlphaType alphaType : alphaModes) {
msarett91c22b22016-02-22 12:27:46 -0800444 push_android_codec_src(path, AndroidCodecSrc::kFullImage_Mode, colorType,
scroggoc5560be2016-02-03 09:42:42 -0800445 alphaType, sampleSize);
446 if (subset) {
msarett91c22b22016-02-22 12:27:46 -0800447 push_android_codec_src(path, AndroidCodecSrc::kDivisor_Mode, colorType,
scroggoc5560be2016-02-03 09:42:42 -0800448 alphaType, sampleSize);
449 }
msarett3d9d7a72015-10-21 10:27:10 -0700450 }
msarett36c37962015-09-02 13:20:52 -0700451 }
452 }
msarett438b2ad2015-04-09 12:43:10 -0700453}
454
msarett5cb48852015-11-06 08:56:32 -0800455static bool brd_color_type_supported(SkBitmapRegionDecoder::Strategy strategy,
msaretta5783ae2015-09-08 15:35:32 -0700456 CodecSrc::DstColorType dstColorType) {
457 switch (strategy) {
msarett5cb48852015-11-06 08:56:32 -0800458 case SkBitmapRegionDecoder::kCanvas_Strategy:
msaretta5783ae2015-09-08 15:35:32 -0700459 if (CodecSrc::kGetFromCanvas_DstColorType == dstColorType) {
460 return true;
461 }
462 return false;
msarett5cb48852015-11-06 08:56:32 -0800463 case SkBitmapRegionDecoder::kAndroidCodec_Strategy:
msarett26ad17b2015-10-22 07:29:19 -0700464 switch (dstColorType) {
465 case CodecSrc::kGetFromCanvas_DstColorType:
466 case CodecSrc::kIndex8_Always_DstColorType:
467 case CodecSrc::kGrayscale_Always_DstColorType:
468 return true;
469 default:
470 return false;
471 }
msaretta5783ae2015-09-08 15:35:32 -0700472 default:
473 SkASSERT(false);
474 return false;
475 }
476}
477
msarett5cb48852015-11-06 08:56:32 -0800478static void push_brd_src(Path path, SkBitmapRegionDecoder::Strategy strategy,
msaretta5783ae2015-09-08 15:35:32 -0700479 CodecSrc::DstColorType dstColorType, BRDSrc::Mode mode, uint32_t sampleSize) {
480 SkString folder;
481 switch (strategy) {
msarett5cb48852015-11-06 08:56:32 -0800482 case SkBitmapRegionDecoder::kCanvas_Strategy:
msaretta5783ae2015-09-08 15:35:32 -0700483 folder.append("brd_canvas");
484 break;
msarett5cb48852015-11-06 08:56:32 -0800485 case SkBitmapRegionDecoder::kAndroidCodec_Strategy:
msarett26ad17b2015-10-22 07:29:19 -0700486 folder.append("brd_android_codec");
487 break;
msaretta5783ae2015-09-08 15:35:32 -0700488 default:
489 SkASSERT(false);
490 return;
491 }
492
493 switch (mode) {
494 case BRDSrc::kFullImage_Mode:
495 break;
496 case BRDSrc::kDivisor_Mode:
497 folder.append("_divisor");
498 break;
499 default:
500 SkASSERT(false);
501 return;
502 }
503
504 switch (dstColorType) {
505 case CodecSrc::kGetFromCanvas_DstColorType:
506 break;
507 case CodecSrc::kIndex8_Always_DstColorType:
508 folder.append("_kIndex");
509 break;
510 case CodecSrc::kGrayscale_Always_DstColorType:
511 folder.append("_kGray");
512 break;
513 default:
514 SkASSERT(false);
515 return;
516 }
517
518 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800519 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700520 }
521
522 BRDSrc* src = new BRDSrc(path, strategy, mode, dstColorType, sampleSize);
523 push_src("image", folder, src);
524}
525
526static void push_brd_srcs(Path path) {
527
msarett5cb48852015-11-06 08:56:32 -0800528 const SkBitmapRegionDecoder::Strategy strategies[] = {
529 SkBitmapRegionDecoder::kCanvas_Strategy,
msarett5cb48852015-11-06 08:56:32 -0800530 SkBitmapRegionDecoder::kAndroidCodec_Strategy,
msaretta5783ae2015-09-08 15:35:32 -0700531 };
532
scroggo501b7342015-11-03 07:55:11 -0800533 // Test on a variety of sampleSizes, making sure to include:
534 // - 2, 4, and 8, which are natively supported by jpeg
535 // - multiples of 2 which are not divisible by 4 (analogous for 4)
536 // - larger powers of two, since BRD clients generally use powers of 2
537 // We will only produce output for the larger sizes on large images.
538 const uint32_t sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 24, 32, 64 };
msarett6efbe052015-09-11 09:01:16 -0700539
msaretta5783ae2015-09-08 15:35:32 -0700540 // We will only test to one backend (8888), but we will test all of the
541 // color types that we need to decode to on this backend.
542 const CodecSrc::DstColorType dstColorTypes[] = {
msarett26ad17b2015-10-22 07:29:19 -0700543 CodecSrc::kGetFromCanvas_DstColorType,
544 CodecSrc::kIndex8_Always_DstColorType,
545 CodecSrc::kGrayscale_Always_DstColorType,
msaretta5783ae2015-09-08 15:35:32 -0700546 };
547
548 const BRDSrc::Mode modes[] = {
msarett26ad17b2015-10-22 07:29:19 -0700549 BRDSrc::kFullImage_Mode,
550 BRDSrc::kDivisor_Mode,
msaretta5783ae2015-09-08 15:35:32 -0700551 };
552
msarett5cb48852015-11-06 08:56:32 -0800553 for (SkBitmapRegionDecoder::Strategy strategy : strategies) {
msarett6efbe052015-09-11 09:01:16 -0700554 for (uint32_t sampleSize : sampleSizes) {
msarett6efbe052015-09-11 09:01:16 -0700555 for (CodecSrc::DstColorType dstColorType : dstColorTypes) {
556 if (brd_color_type_supported(strategy, dstColorType)) {
557 for (BRDSrc::Mode mode : modes) {
msaretta5783ae2015-09-08 15:35:32 -0700558 push_brd_src(path, strategy, dstColorType, mode, sampleSize);
559 }
560 }
561 }
562 }
563 }
564}
565
566static bool brd_supported(const char* ext) {
msarett8c8f22a2015-04-01 06:58:48 -0700567 static const char* const exts[] = {
msaretta5783ae2015-09-08 15:35:32 -0700568 "jpg", "jpeg", "png", "webp",
569 "JPG", "JPEG", "PNG", "WEBP",
msarett8c8f22a2015-04-01 06:58:48 -0700570 };
571
572 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
573 if (0 == strcmp(exts[i], ext)) {
574 return true;
575 }
576 }
577 return false;
scroggo9b77ddd2015-03-19 06:03:39 -0700578}
579
scroggo86737142016-02-03 12:19:11 -0800580static bool gather_srcs() {
mtklein748ca3b2015-01-15 10:56:12 -0800581 for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) {
djsollen54416de2015-04-03 07:24:48 -0700582 push_src("gm", "", new GMSrc(r->factory()));
mtklein748ca3b2015-01-15 10:56:12 -0800583 }
halcanaryfc37ad12015-01-30 07:31:19 -0800584 for (int i = 0; i < FLAGS_skps.count(); i++) {
585 const char* path = FLAGS_skps[i];
586 if (sk_isdir(path)) {
587 SkOSFile::Iter it(path, "skp");
588 for (SkString file; it.next(&file); ) {
djsollen54416de2015-04-03 07:24:48 -0700589 push_src("skp", "", new SKPSrc(SkOSPath::Join(path, file.c_str())));
halcanaryfc37ad12015-01-30 07:31:19 -0800590 }
591 } else {
djsollen54416de2015-04-03 07:24:48 -0700592 push_src("skp", "", new SKPSrc(path));
mtklein114c3cd2015-01-15 10:15:02 -0800593 }
594 }
scroggo86737142016-02-03 12:19:11 -0800595
596 SkTArray<SkString> images;
597 if (!CollectImages(&images)) {
598 return false;
599 }
600
601 for (auto image : images) {
602 push_codec_srcs(image);
mtklein21eaf3b2016-02-08 12:39:59 -0800603 const char* ext = strrchr(image.c_str(), '.');
604 if (ext && brd_supported(ext+1)) {
scroggo86737142016-02-03 12:19:11 -0800605 push_brd_srcs(image);
mtklein709d2c32015-01-15 08:30:25 -0800606 }
mtklein709d2c32015-01-15 08:30:25 -0800607 }
scroggo86737142016-02-03 12:19:11 -0800608
609 return true;
mtklein709d2c32015-01-15 08:30:25 -0800610}
611
kkinnunen3e980c32015-12-23 01:33:00 -0800612static void push_sink(const SkCommandLineConfig& config, Sink* s) {
rmistry0f515bd2015-12-22 10:22:26 -0800613 SkAutoTDelete<Sink> sink(s);
kkinnunen3e980c32015-12-23 01:33:00 -0800614
mtkleine0effd62015-07-29 06:37:28 -0700615 // Try a simple Src as a canary. If it fails, skip this sink.
mtklein748ca3b2015-01-15 10:56:12 -0800616 struct : public Src {
mtkleine0effd62015-07-29 06:37:28 -0700617 Error draw(SkCanvas* c) const override {
618 c->drawRect(SkRect::MakeWH(1,1), SkPaint());
619 return "";
620 }
mtklein36352bf2015-03-25 18:17:31 -0700621 SkISize size() const override { return SkISize::Make(16, 16); }
mtkleine0effd62015-07-29 06:37:28 -0700622 Name name() const override { return "justOneRect"; }
623 } justOneRect;
mtklein114c3cd2015-01-15 10:15:02 -0800624
mtklein748ca3b2015-01-15 10:56:12 -0800625 SkBitmap bitmap;
626 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800627 SkString log;
mtkleine0effd62015-07-29 06:37:28 -0700628 Error err = sink->draw(justOneRect, &bitmap, &stream, &log);
mtklein4089ef72015-03-05 08:40:28 -0800629 if (err.isFatal()) {
kkinnunen3e980c32015-12-23 01:33:00 -0800630 SkDebugf("Could not run %s: %s\n", config.getTag().c_str(), err.c_str());
mtklein05641a52015-04-21 10:49:13 -0700631 exit(1);
mtklein114c3cd2015-01-15 10:15:02 -0800632 }
633
mtkleine0effd62015-07-29 06:37:28 -0700634 TaggedSink& ts = gSinks.push_back();
mtklein748ca3b2015-01-15 10:56:12 -0800635 ts.reset(sink.detach());
kkinnunen3e980c32015-12-23 01:33:00 -0800636 ts.tag = config.getTag();
mtklein748ca3b2015-01-15 10:56:12 -0800637}
638
639static bool gpu_supported() {
640#if SK_SUPPORT_GPU
641 return FLAGS_gpu;
642#else
643 return false;
644#endif
645}
kkinnunen9ebc3f02015-12-21 23:48:13 -0800646
kkinnunen3e980c32015-12-23 01:33:00 -0800647static Sink* create_sink(const SkCommandLineConfig* config) {
648#if SK_SUPPORT_GPU
649 if (gpu_supported()) {
650 if (const SkCommandLineConfigGpu* gpuConfig = config->asConfigGpu()) {
651 GrContextFactory::GLContextType contextType = gpuConfig->getContextType();
652 GrContextFactory::GLContextOptions contextOptions =
653 GrContextFactory::kNone_GLContextOptions;
654 if (gpuConfig->getUseNVPR()) {
655 contextOptions = static_cast<GrContextFactory::GLContextOptions>(
656 contextOptions | GrContextFactory::kEnableNVPR_GLContextOptions);
657 }
658 GrContextFactory testFactory;
659 if (!testFactory.get(contextType, contextOptions)) {
660 SkDebugf("WARNING: can not create GPU context for config '%s'. "
661 "GM tests will be skipped.\n", gpuConfig->getTag().c_str());
662 return nullptr;
663 }
664 return new GPUSink(contextType, contextOptions, gpuConfig->getSamples(),
665 gpuConfig->getUseDIText(), FLAGS_gpu_threading);
666 }
667 }
668#endif
669
670#define SINK(t, sink, ...) if (config->getBackend().equals(t)) { return new sink(__VA_ARGS__); }
671
tomhudsoneebc39a2015-02-23 12:18:05 -0800672#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
673 SINK("hwui", HWUISink);
674#endif
675
mtklein748ca3b2015-01-15 10:56:12 -0800676 if (FLAGS_cpu) {
677 SINK("565", RasterSink, kRGB_565_SkColorType);
678 SINK("8888", RasterSink, kN32_SkColorType);
halcanaryc11c62f2015-09-28 11:51:54 -0700679 SINK("pdf", PDFSink, "Pdfium");
680 SINK("pdf_poppler", PDFSink, "Poppler");
mtklein9c3f17d2015-01-28 11:35:18 -0800681 SINK("skp", SKPSink);
mtklein8a4527e2015-01-31 20:00:58 -0800682 SINK("svg", SVGSink);
683 SINK("null", NullSink);
halcanary47ef4d52015-03-03 09:13:09 -0800684 SINK("xps", XPSSink);
mtklein748ca3b2015-01-15 10:56:12 -0800685 }
686#undef SINK
halcanary96fcdcc2015-08-27 07:41:13 -0700687 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800688}
689
kkinnunen3e980c32015-12-23 01:33:00 -0800690static Sink* create_via(const SkString& tag, Sink* wrapped) {
691#define VIA(t, via, ...) if (tag.equals(t)) { return new via(__VA_ARGS__); }
halcanary96fcdcc2015-08-27 07:41:13 -0700692 VIA("twice", ViaTwice, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700693 VIA("serialize", ViaSerialization, wrapped);
mtklein4a34ecb2016-01-08 10:19:35 -0800694 VIA("pic", ViaPicture, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700695 VIA("2ndpic", ViaSecondPicture, wrapped);
mtkleind31c13d2015-05-05 12:59:56 -0700696 VIA("sp", ViaSingletonPictures, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700697 VIA("tiles", ViaTiles, 256, 256, nullptr, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800698 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
mtklein2e2ea382015-10-16 10:29:41 -0700699 VIA("remote", ViaRemote, false, wrapped);
700 VIA("remote_cache", ViaRemote, true, wrapped);
halcanary7a76f9c2016-02-03 11:53:18 -0800701 VIA("mojo", ViaMojo, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800702
mtkleind603b222015-02-17 11:13:33 -0800703 if (FLAGS_matrix.count() == 4) {
mtklein748ca3b2015-01-15 10:56:12 -0800704 SkMatrix m;
mtkleind603b222015-02-17 11:13:33 -0800705 m.reset();
706 m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
707 m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
708 m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
709 m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
710 VIA("matrix", ViaMatrix, m, wrapped);
711 VIA("upright", ViaUpright, m, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800712 }
tomhudson64de1e12015-03-05 08:01:07 -0800713
714#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
715 VIA("androidsdk", ViaAndroidSDK, wrapped);
716#endif
717
mtklein748ca3b2015-01-15 10:56:12 -0800718#undef VIA
halcanary96fcdcc2015-08-27 07:41:13 -0700719 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800720}
721
mtklein748ca3b2015-01-15 10:56:12 -0800722static void gather_sinks() {
kkinnunen3e980c32015-12-23 01:33:00 -0800723 SkCommandLineConfigArray configs;
724 ParseConfigs(FLAGS_config, &configs);
725 for (int i = 0; i < configs.count(); i++) {
726 const SkCommandLineConfig& config = *configs[i];
727 Sink* sink = create_sink(&config);
728 if (sink == nullptr) {
729 SkDebugf("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
730 config.getTag().c_str());
731 continue;
732 }
mtklein748ca3b2015-01-15 10:56:12 -0800733
kkinnunen3e980c32015-12-23 01:33:00 -0800734 const SkTArray<SkString>& parts = config.getViaParts();
735 for (int j = parts.count(); j-- > 0;) {
736 const SkString& part = parts[j];
737 Sink* next = create_via(part, sink);
halcanary96fcdcc2015-08-27 07:41:13 -0700738 if (next == nullptr) {
kkinnunen3e980c32015-12-23 01:33:00 -0800739 SkDebugf("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
740 part.c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800741 delete sink;
halcanary96fcdcc2015-08-27 07:41:13 -0700742 sink = nullptr;
mtklein748ca3b2015-01-15 10:56:12 -0800743 break;
744 }
745 sink = next;
746 }
747 if (sink) {
748 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800749 }
750 }
751}
mtklein709d2c32015-01-15 08:30:25 -0800752
mtkleina5114d72015-08-24 13:27:01 -0700753static bool dump_png(SkBitmap bitmap, const char* path, const char* md5) {
754 const int w = bitmap.width(),
755 h = bitmap.height();
756
757 // First get the bitmap into N32 color format. The next step will work only there.
758 if (bitmap.colorType() != kN32_SkColorType) {
759 SkBitmap n32;
760 if (!bitmap.copyTo(&n32, kN32_SkColorType)) {
761 return false;
762 }
763 bitmap = n32;
764 }
765
766 // Convert our N32 bitmap into unpremul RGBA for libpng.
767 SkAutoTMalloc<uint32_t> rgba(w*h);
768 if (!bitmap.readPixels(SkImageInfo::Make(w,h, kRGBA_8888_SkColorType, kUnpremul_SkAlphaType),
769 rgba, 4*w, 0,0)) {
770 return false;
771 }
772
773 // We don't need bitmap anymore. Might as well drop our ref.
mtkleinfccb77d2015-08-24 14:13:29 -0700774 bitmap.reset();
mtkleina5114d72015-08-24 13:27:01 -0700775
mtkleinc64137c2015-08-25 10:56:08 -0700776 FILE* f = fopen(path, "wb");
mtkleina5114d72015-08-24 13:27:01 -0700777 if (!f) { return false; }
778
779 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
780 if (!png) {
781 fclose(f);
782 return false;
783 }
784
785 png_infop info = png_create_info_struct(png);
786 if (!info) {
787 png_destroy_write_struct(&png, &info);
788 fclose(f);
789 return false;
790 }
791
mtkleind69ece62015-09-10 10:37:44 -0700792 SkString description;
793 description.append("Key: ");
794 for (int i = 0; i < FLAGS_key.count(); i++) {
795 description.appendf("%s ", FLAGS_key[i]);
796 }
797 description.append("Properties: ");
798 for (int i = 0; i < FLAGS_properties.count(); i++) {
799 description.appendf("%s ", FLAGS_properties[i]);
800 }
801 description.appendf("MD5: %s", md5);
802
mtkleina5114d72015-08-24 13:27:01 -0700803 png_text text[2];
804 text[0].key = (png_charp)"Author";
805 text[0].text = (png_charp)"DM dump_png()";
806 text[0].compression = PNG_TEXT_COMPRESSION_NONE;
807 text[1].key = (png_charp)"Description";
mtkleind69ece62015-09-10 10:37:44 -0700808 text[1].text = (png_charp)description.c_str();
mtkleina5114d72015-08-24 13:27:01 -0700809 text[1].compression = PNG_TEXT_COMPRESSION_NONE;
810 png_set_text(png, info, text, 2);
811
812 png_init_io(png, f);
813 png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8,
814 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
815 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
816 png_write_info(png, info);
817 for (int j = 0; j < h; j++) {
818 png_bytep row = (png_bytep)(rgba.get() + w*j);
819 png_write_rows(png, &row, 1);
820 }
821 png_write_end(png, info);
822
823 png_destroy_write_struct(&png, &info);
824 fclose(f);
825 return true;
826}
827
mtkleina2ef6422015-01-15 13:44:22 -0800828static bool match(const char* needle, const char* haystack) {
halcanary96fcdcc2015-08-27 07:41:13 -0700829 return 0 == strcmp("_", needle) || nullptr != strstr(haystack, needle);
mtkleina2ef6422015-01-15 13:44:22 -0800830}
831
djsollen54416de2015-04-03 07:24:48 -0700832static ImplicitString is_blacklisted(const char* sink, const char* src,
833 const char* srcOptions, const char* name) {
mtklein0d243ff2015-04-03 07:51:00 -0700834 for (int i = 0; i < FLAGS_blacklist.count() - 3; i += 4) {
mtkleina2ef6422015-01-15 13:44:22 -0800835 if (match(FLAGS_blacklist[i+0], sink) &&
djsollen54416de2015-04-03 07:24:48 -0700836 match(FLAGS_blacklist[i+1], src) &&
837 match(FLAGS_blacklist[i+2], srcOptions) &&
838 match(FLAGS_blacklist[i+3], name)) {
839 return SkStringPrintf("%s %s %s %s",
840 FLAGS_blacklist[i+0], FLAGS_blacklist[i+1],
841 FLAGS_blacklist[i+2], FLAGS_blacklist[i+3]);
mtkleina2ef6422015-01-15 13:44:22 -0800842 }
843 }
844 return "";
845}
846
mtkleincd50bca2016-01-05 06:20:20 -0800847// Even when a Task Sink reports to be non-threadsafe (e.g. GPU), we know things like
848// .png encoding are definitely thread safe. This lets us offload that work to CPU threads.
849static SkTaskGroup gDefinitelyThreadSafeWork;
850
mtklein748ca3b2015-01-15 10:56:12 -0800851// The finest-grained unit of work we can run: draw a single Src into a single Sink,
852// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
853struct Task {
mtkleine0effd62015-07-29 06:37:28 -0700854 Task(const TaggedSrc& src, const TaggedSink& sink) : src(src), sink(sink) {}
855 const TaggedSrc& src;
856 const TaggedSink& sink;
mtklein748ca3b2015-01-15 10:56:12 -0800857
mtklein21eaf3b2016-02-08 12:39:59 -0800858 static void Run(const Task& task) {
859 SkString name = task.src->name();
mtkleine0effd62015-07-29 06:37:28 -0700860
861 // We'll skip drawing this Src/Sink pair if:
862 // - the Src vetoes the Sink;
863 // - this Src / Sink combination is on the blacklist;
864 // - it's a dry run.
mtklein21eaf3b2016-02-08 12:39:59 -0800865 SkString note(task.src->veto(task.sink->flags()) ? " (veto)" : "");
866 SkString whyBlacklisted = is_blacklisted(task.sink.tag.c_str(), task.src.tag.c_str(),
867 task.src.options.c_str(), name.c_str());
mtkleinb37cb412015-03-18 05:27:14 -0700868 if (!whyBlacklisted.isEmpty()) {
869 note.appendf(" (--blacklist %s)", whyBlacklisted.c_str());
870 }
mtkleine0effd62015-07-29 06:37:28 -0700871
mtkleinb9eb4ac2015-02-02 18:26:03 -0800872 SkString log;
mtkleinf27f08b2015-11-03 06:54:24 -0800873 auto timerStart = now_ms();
mtkleine0effd62015-07-29 06:37:28 -0700874 if (!FLAGS_dryRun && note.isEmpty()) {
mtklein748ca3b2015-01-15 10:56:12 -0800875 SkBitmap bitmap;
876 SkDynamicMemoryWStream stream;
bsalomon821e10e2015-06-04 14:15:33 -0700877 if (FLAGS_pre_log) {
mtklein21eaf3b2016-02-08 12:39:59 -0800878 SkDebugf("\nRunning %s->%s", name.c_str(), task.sink.tag.c_str());
bsalomon821e10e2015-06-04 14:15:33 -0700879 }
mtklein21eaf3b2016-02-08 12:39:59 -0800880 start(task.sink.tag.c_str(), task.src.tag, task.src.options, name.c_str());
881 Error err = task.sink->draw(*task.src, &bitmap, &stream, &log);
mtklein748ca3b2015-01-15 10:56:12 -0800882 if (!err.isEmpty()) {
mtklein4089ef72015-03-05 08:40:28 -0800883 if (err.isFatal()) {
djsollen54416de2015-04-03 07:24:48 -0700884 fail(SkStringPrintf("%s %s %s %s: %s",
mtklein21eaf3b2016-02-08 12:39:59 -0800885 task.sink.tag.c_str(),
886 task.src.tag.c_str(),
887 task.src.options.c_str(),
mtklein4089ef72015-03-05 08:40:28 -0800888 name.c_str(),
889 err.c_str()));
mtkleinb37cb412015-03-18 05:27:14 -0700890 } else {
891 note.appendf(" (skipped: %s)", err.c_str());
mtkleinba6ada72016-01-21 09:39:35 -0800892 auto elapsed = now_ms() - timerStart;
mtklein21eaf3b2016-02-08 12:39:59 -0800893 done(elapsed, task.sink.tag.c_str(), task.src.tag, task.src.options,
mtkleinba6ada72016-01-21 09:39:35 -0800894 name, note, log);
895 return;
mtklein4089ef72015-03-05 08:40:28 -0800896 }
mtklein748ca3b2015-01-15 10:56:12 -0800897 }
mtklein62bd1a62015-01-27 14:46:26 -0800898
mtkleincd50bca2016-01-05 06:20:20 -0800899 // We're likely switching threads here, so we must capture by value, [=] or [foo,bar].
900 SkStreamAsset* data = stream.detachAsStream();
901 gDefinitelyThreadSafeWork.add([task,name,bitmap,data]{
902 SkAutoTDelete<SkStreamAsset> ownedData(data);
903
904 // Why doesn't the copy constructor do this when we have pre-locked pixels?
905 bitmap.lockPixels();
906
907 SkString md5;
908 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
909 SkMD5 hash;
910 if (data->getLength()) {
911 hash.writeStream(data, data->getLength());
912 data->rewind();
mtklein38408462015-07-08 07:25:27 -0700913 } else {
mtkleincd50bca2016-01-05 06:20:20 -0800914 // If we're BGRA (Linux, Windows), swizzle over to RGBA (Mac, Android).
915 // This helps eliminate multiple 0-pixel-diff hashes on gold.skia.org.
916 // (Android's general slow speed breaks the tie arbitrarily in RGBA's favor.)
917 // We might consider promoting 565 to RGBA too.
918 if (bitmap.colorType() == kBGRA_8888_SkColorType) {
919 SkBitmap swizzle;
920 SkAssertResult(bitmap.copyTo(&swizzle, kRGBA_8888_SkColorType));
921 hash.write(swizzle.getPixels(), swizzle.getSize());
922 } else {
923 hash.write(bitmap.getPixels(), bitmap.getSize());
924 }
925 }
926 SkMD5::Digest digest;
927 hash.finish(digest);
928 for (int i = 0; i < 16; i++) {
929 md5.appendf("%02x", digest.data[i]);
mtklein38408462015-07-08 07:25:27 -0700930 }
mtklein62bd1a62015-01-27 14:46:26 -0800931 }
mtklein62bd1a62015-01-27 14:46:26 -0800932
mtkleincd50bca2016-01-05 06:20:20 -0800933 if (!FLAGS_readPath.isEmpty() &&
mtklein21eaf3b2016-02-08 12:39:59 -0800934 !gGold.contains(Gold(task.sink.tag.c_str(), task.src.tag.c_str(),
935 task.src.options.c_str(), name, md5))) {
mtkleincd50bca2016-01-05 06:20:20 -0800936 fail(SkStringPrintf("%s not found for %s %s %s %s in %s",
937 md5.c_str(),
mtklein21eaf3b2016-02-08 12:39:59 -0800938 task.sink.tag.c_str(),
939 task.src.tag.c_str(),
940 task.src.options.c_str(),
mtkleincd50bca2016-01-05 06:20:20 -0800941 name.c_str(),
942 FLAGS_readPath[0]));
mtklein748ca3b2015-01-15 10:56:12 -0800943 }
mtkleincd50bca2016-01-05 06:20:20 -0800944
945 if (!FLAGS_writePath.isEmpty()) {
mtklein21eaf3b2016-02-08 12:39:59 -0800946 const char* ext = task.sink->fileExtension();
mtkleincd50bca2016-01-05 06:20:20 -0800947 if (data->getLength()) {
mtklein21eaf3b2016-02-08 12:39:59 -0800948 WriteToDisk(task, md5, ext, data, data->getLength(), nullptr);
mtkleincd50bca2016-01-05 06:20:20 -0800949 SkASSERT(bitmap.drawsNothing());
950 } else if (!bitmap.drawsNothing()) {
mtklein21eaf3b2016-02-08 12:39:59 -0800951 WriteToDisk(task, md5, ext, nullptr, 0, &bitmap);
mtkleincd50bca2016-01-05 06:20:20 -0800952 }
953 }
954 });
mtklein748ca3b2015-01-15 10:56:12 -0800955 }
mtkleinba6ada72016-01-21 09:39:35 -0800956 auto elapsed = now_ms() - timerStart;
mtklein21eaf3b2016-02-08 12:39:59 -0800957 done(elapsed, task.sink.tag.c_str(), task.src.tag.c_str(), task.src.options.c_str(),
mtkleinf27f08b2015-11-03 06:54:24 -0800958 name, note, log);
mtklein748ca3b2015-01-15 10:56:12 -0800959 }
960
961 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -0800962 SkString md5,
963 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -0800964 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -0800965 const SkBitmap* bitmap) {
mtklein748ca3b2015-01-15 10:56:12 -0800966 JsonWriter::BitmapResult result;
djsollen54416de2015-04-03 07:24:48 -0700967 result.name = task.src->name();
kkinnunen3e980c32015-12-23 01:33:00 -0800968 result.config = task.sink.tag.c_str();
djsollen54416de2015-04-03 07:24:48 -0700969 result.sourceType = task.src.tag;
970 result.sourceOptions = task.src.options;
971 result.ext = ext;
972 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -0800973 JsonWriter::AddBitmapResult(result);
974
mtkleinb0531a72015-04-07 13:38:48 -0700975 // If an MD5 is uninteresting, we want it noted in the JSON file,
976 // but don't want to dump it out as a .png (or whatever ext is).
977 if (gUninterestingHashes.contains(md5)) {
978 return;
979 }
980
mtklein748ca3b2015-01-15 10:56:12 -0800981 const char* dir = FLAGS_writePath[0];
982 if (0 == strcmp(dir, "@")) { // Needed for iOS.
983 dir = FLAGS_resourcePath[0];
984 }
985 sk_mkdir(dir);
986
987 SkString path;
988 if (FLAGS_nameByHash) {
989 path = SkOSPath::Join(dir, result.md5.c_str());
990 path.append(".");
991 path.append(ext);
992 if (sk_exists(path.c_str())) {
993 return; // Content-addressed. If it exists already, we're done.
994 }
995 } else {
kkinnunen3e980c32015-12-23 01:33:00 -0800996 path = SkOSPath::Join(dir, task.sink.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800997 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -0700998 path = SkOSPath::Join(path.c_str(), task.src.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800999 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001000 if (strcmp(task.src.options.c_str(), "") != 0) {
1001 path = SkOSPath::Join(path.c_str(), task.src.options.c_str());
djsollen54416de2015-04-03 07:24:48 -07001002 sk_mkdir(path.c_str());
1003 }
mtklein748ca3b2015-01-15 10:56:12 -08001004 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
1005 path.append(".");
1006 path.append(ext);
1007 }
1008
mtklein748ca3b2015-01-15 10:56:12 -08001009 if (bitmap) {
mtkleina5114d72015-08-24 13:27:01 -07001010 if (!dump_png(*bitmap, path.c_str(), result.md5.c_str())) {
mtklein748ca3b2015-01-15 10:56:12 -08001011 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
1012 return;
1013 }
1014 } else {
mtkleina5114d72015-08-24 13:27:01 -07001015 SkFILEWStream file(path.c_str());
1016 if (!file.isValid()) {
1017 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
1018 return;
1019 }
mtklein748ca3b2015-01-15 10:56:12 -08001020 if (!file.writeStream(data, len)) {
1021 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
1022 return;
1023 }
1024 }
1025 }
1026};
1027
mtklein748ca3b2015-01-15 10:56:12 -08001028/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1029
1030// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
1031
mtklein21eaf3b2016-02-08 12:39:59 -08001032static SkTDArray<skiatest::Test> gParallelTests, gSerialTests;
mtklein748ca3b2015-01-15 10:56:12 -08001033
1034static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -08001035 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -08001036 return;
1037 }
mtklein6393c062015-04-27 08:45:01 -07001038 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r; r = r->next()) {
1039 if (!in_shard()) {
1040 continue;
1041 }
halcanary87f3ba42015-01-20 09:30:20 -08001042 // Despite its name, factory() is returning a reference to
1043 // link-time static const POD data.
1044 const skiatest::Test& test = r->factory();
1045 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -08001046 continue;
1047 }
halcanary87f3ba42015-01-20 09:30:20 -08001048 if (test.needsGpu && gpu_supported()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001049 (FLAGS_gpu_threading ? gParallelTests : gSerialTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -08001050 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein21eaf3b2016-02-08 12:39:59 -08001051 gParallelTests.push(test);
mtklein82d28432015-01-15 12:46:02 -08001052 }
mtklein748ca3b2015-01-15 10:56:12 -08001053 }
1054}
1055
mtklein21eaf3b2016-02-08 12:39:59 -08001056static void run_test(skiatest::Test test) {
halcanary87f3ba42015-01-20 09:30:20 -08001057 struct : public skiatest::Reporter {
mtklein36352bf2015-03-25 18:17:31 -07001058 void reportFailed(const skiatest::Failure& failure) override {
halcanary87f3ba42015-01-20 09:30:20 -08001059 fail(failure.toString());
1060 JsonWriter::AddTestFailure(failure);
1061 }
mtklein36352bf2015-03-25 18:17:31 -07001062 bool allowExtendedTest() const override {
halcanary87f3ba42015-01-20 09:30:20 -08001063 return FLAGS_pathOpsExtended;
1064 }
mtklein36352bf2015-03-25 18:17:31 -07001065 bool verbose() const override { return FLAGS_veryVerbose; }
halcanary87f3ba42015-01-20 09:30:20 -08001066 } reporter;
djsollen824996a2015-06-12 12:06:22 -07001067
1068 SkString note;
mtklein21eaf3b2016-02-08 12:39:59 -08001069 SkString whyBlacklisted = is_blacklisted("_", "tests", "_", test.name);
djsollen824996a2015-06-12 12:06:22 -07001070 if (!whyBlacklisted.isEmpty()) {
1071 note.appendf(" (--blacklist %s)", whyBlacklisted.c_str());
1072 }
1073
mtkleinf27f08b2015-11-03 06:54:24 -08001074 auto timerStart = now_ms();
djsollen824996a2015-06-12 12:06:22 -07001075 if (!FLAGS_dryRun && whyBlacklisted.isEmpty()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001076 start("unit", "test", "", test.name);
mtklein55e88b22015-01-21 15:50:13 -08001077 GrContextFactory factory;
bsalomon821e10e2015-06-04 14:15:33 -07001078 if (FLAGS_pre_log) {
mtklein21eaf3b2016-02-08 12:39:59 -08001079 SkDebugf("\nRunning test %s", test.name);
bsalomon821e10e2015-06-04 14:15:33 -07001080 }
mtklein21eaf3b2016-02-08 12:39:59 -08001081 test.proc(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -08001082 }
mtklein21eaf3b2016-02-08 12:39:59 -08001083 done(now_ms()-timerStart, "unit", "test", "", test.name, note, "");
mtklein748ca3b2015-01-15 10:56:12 -08001084}
1085
1086/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1087
mtkleinde6fc2b2015-03-12 06:28:54 -07001088// Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung.
1089// This prints something every once in a while so that it knows we're still working.
mtklein2e1c47e2015-03-12 07:16:56 -07001090static void start_keepalive() {
1091 struct Loop {
1092 static void forever(void*) {
1093 for (;;) {
1094 static const int kSec = 300;
1095 #if defined(SK_BUILD_FOR_WIN)
1096 Sleep(kSec * 1000);
1097 #else
1098 sleep(kSec);
1099 #endif
mtkleinb37cb412015-03-18 05:27:14 -07001100 SkString running;
1101 {
mtkleinbc4e0032015-12-09 08:37:35 -08001102 SkAutoMutexAcquire lock(gRunningAndTallyMutex);
mtkleinb37cb412015-03-18 05:27:14 -07001103 for (int i = 0; i < gRunning.count(); i++) {
1104 running.appendf("\n\t%s", gRunning[i].c_str());
1105 }
1106 }
1107 SkDebugf("\nCurrently running:%s\n", running.c_str());
mtklein2e1c47e2015-03-12 07:16:56 -07001108 }
1109 }
1110 };
1111 static SkThread* intentionallyLeaked = new SkThread(Loop::forever);
1112 intentionallyLeaked->start();
mtkleinde6fc2b2015-03-12 06:28:54 -07001113}
1114
caryclark83ca6282015-06-10 09:31:09 -07001115#define PORTABLE_FONT_PREFIX "Toy Liberation "
1116
1117static SkTypeface* create_from_name(const char familyName[], SkTypeface::Style style) {
1118 if (familyName && strlen(familyName) > sizeof(PORTABLE_FONT_PREFIX)
1119 && !strncmp(familyName, PORTABLE_FONT_PREFIX, sizeof(PORTABLE_FONT_PREFIX) - 1)) {
caryclark1818acb2015-07-24 12:09:25 -07001120 return sk_tool_utils::create_portable_typeface(familyName, style);
caryclark83ca6282015-06-10 09:31:09 -07001121 }
halcanary96fcdcc2015-08-27 07:41:13 -07001122 return nullptr;
caryclark83ca6282015-06-10 09:31:09 -07001123}
1124
1125#undef PORTABLE_FONT_PREFIX
1126
1127extern SkTypeface* (*gCreateTypefaceDelegate)(const char [], SkTypeface::Style );
1128
jcgregorio3b27ade2014-11-13 08:06:40 -08001129int dm_main();
caryclark17f0b6d2014-07-22 10:15:34 -07001130int dm_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -08001131 SetupCrashHandler();
mtklein5286f022016-01-22 08:18:14 -08001132 JsonWriter::DumpJson(); // It's handy for the bots to assume this is ~never missing.
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +00001133 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -07001134 SkTaskGroup::Enabler enabled(FLAGS_threads);
caryclark83ca6282015-06-10 09:31:09 -07001135 gCreateTypefaceDelegate = &create_from_name;
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +00001136
mtklein2e1c47e2015-03-12 07:16:56 -07001137 start_keepalive();
mtkleinde6fc2b2015-03-12 06:28:54 -07001138
mtklein62bd1a62015-01-27 14:46:26 -08001139 gather_gold();
borenet09ed4802015-04-03 14:15:33 -07001140 gather_uninteresting_hashes();
mtklein62bd1a62015-01-27 14:46:26 -08001141
scroggo86737142016-02-03 12:19:11 -08001142 if (!gather_srcs()) {
1143 return 1;
1144 }
mtklein748ca3b2015-01-15 10:56:12 -08001145 gather_sinks();
1146 gather_tests();
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +00001147
mtklein21eaf3b2016-02-08 12:39:59 -08001148 gPending = gSrcs.count() * gSinks.count() + gParallelTests.count() + gSerialTests.count();
mtklein748ca3b2015-01-15 10:56:12 -08001149 SkDebugf("%d srcs * %d sinks + %d tests == %d tasks\n",
mtklein21eaf3b2016-02-08 12:39:59 -08001150 gSrcs.count(), gSinks.count(), gParallelTests.count() + gSerialTests.count(), gPending);
mtklein748ca3b2015-01-15 10:56:12 -08001151
mtklein21eaf3b2016-02-08 12:39:59 -08001152 // Kick off as much parallel work as we can, making note of any serial work we'll need to do.
1153 SkTaskGroup parallel;
1154 SkTArray<Task> serial;
1155
1156 for (auto& sink : gSinks)
1157 for (auto& src : gSrcs) {
1158 Task task(src, sink);
1159 if (src->serial() || sink->serial()) {
1160 serial.push_back(task);
1161 } else {
1162 parallel.add([task] { Task::Run(task); });
mtklein748ca3b2015-01-15 10:56:12 -08001163 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001164 }
mtklein21eaf3b2016-02-08 12:39:59 -08001165 for (auto test : gParallelTests) {
1166 parallel.add([test] { run_test(test); });
mtklein55e88b22015-01-21 15:50:13 -08001167 }
mtklein21eaf3b2016-02-08 12:39:59 -08001168
1169 // With the parallel work running, run serial tasks and tests here on main thread.
1170 for (auto task : serial) { Task::Run(task); }
1171 for (auto test : gSerialTests) { run_test(test); }
1172
1173 // Wait for any remaining parallel work to complete (including any spun off of serial tasks).
1174 parallel.wait();
mtkleincd50bca2016-01-05 06:20:20 -08001175 gDefinitelyThreadSafeWork.wait();
1176
mtklein748ca3b2015-01-15 10:56:12 -08001177 // At this point we're back in single-threaded land.
caryclarkf53ce802015-06-15 06:48:30 -07001178 sk_tool_utils::release_portable_typefaces();
mtklein@google.comd36522d2013-10-16 13:02:15 +00001179
mtkleinbc4e0032015-12-09 08:37:35 -08001180 if (FLAGS_verbose && gNoteTally.count() > 0) {
1181 SkDebugf("\nNote tally:\n");
1182 gNoteTally.foreach([](const SkString& note, int* tally) {
1183 SkDebugf("%dx\t%s\n", *tally, note.c_str());
1184 });
1185 }
1186
mtklein2f64eec2015-01-15 14:20:41 -08001187 SkDebugf("\n");
mtklein748ca3b2015-01-15 10:56:12 -08001188 if (gFailures.count() > 0) {
1189 SkDebugf("Failures:\n");
1190 for (int i = 0; i < gFailures.count(); i++) {
mtklein9dc09102015-01-15 15:47:33 -08001191 SkDebugf("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001192 }
1193 SkDebugf("%d failures\n", gFailures.count());
1194 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001195 }
mtklein748ca3b2015-01-15 10:56:12 -08001196 if (gPending > 0) {
1197 SkDebugf("Hrm, we didn't seem to run everything we intended to! Please file a bug.\n");
1198 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001199 }
halcanary7a14b312015-10-01 07:28:13 -07001200 #ifdef SK_PDF_IMAGE_STATS
1201 SkPDFImageDumpStats();
1202 #endif // SK_PDF_IMAGE_STATS
scroggo4d9eaea2016-01-29 07:48:33 -08001203 SkDebugf("Finished!\n");
mtklein748ca3b2015-01-15 10:56:12 -08001204 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +00001205}
jcgregorio3b27ade2014-11-13 08:06:40 -08001206
kkinnunen179a8f52015-11-20 13:32:24 -08001207// TODO: currently many GPU tests are declared outside SK_SUPPORT_GPU guards.
1208// Thus we export the empty RunWithGPUTestContexts when SK_SUPPORT_GPU=0.
1209namespace skiatest {
1210namespace {
1211typedef void(*TestWithGrContext)(skiatest::Reporter*, GrContext*);
1212typedef void(*TestWithGrContextAndGLContext)(skiatest::Reporter*, GrContext*, SkGLContext*);
1213#if SK_SUPPORT_GPU
1214template<typename T>
kkinnunen34058002016-01-06 23:49:30 -08001215void call_test(T test, skiatest::Reporter* reporter, const GrContextFactory::ContextInfo& context);
kkinnunen179a8f52015-11-20 13:32:24 -08001216template<>
1217void call_test(TestWithGrContext test, skiatest::Reporter* reporter,
kkinnunen34058002016-01-06 23:49:30 -08001218 const GrContextFactory::ContextInfo& context) {
1219 test(reporter, context.fGrContext);
kkinnunen179a8f52015-11-20 13:32:24 -08001220}
1221template<>
1222void call_test(TestWithGrContextAndGLContext test, skiatest::Reporter* reporter,
kkinnunen34058002016-01-06 23:49:30 -08001223 const GrContextFactory::ContextInfo& context) {
1224 test(reporter, context.fGrContext, context.fGLContext);
kkinnunen179a8f52015-11-20 13:32:24 -08001225}
1226#endif
1227} // namespace
1228
kkinnunen179a8f52015-11-20 13:32:24 -08001229template<typename T>
1230void RunWithGPUTestContexts(T test, GPUTestContexts testContexts, Reporter* reporter,
1231 GrContextFactory* factory) {
1232#if SK_SUPPORT_GPU
kkinnunen3e980c32015-12-23 01:33:00 -08001233 // Iterate over context types, except use "native" instead of explicitly trying OpenGL and
1234 // OpenGL ES. Do not use GLES on desktop, since tests do not account for not fixing
1235 // http://skbug.com/2809
1236 GrContextFactory::GLContextType contextTypes[] = {
1237 GrContextFactory::kNative_GLContextType,
1238#if SK_ANGLE
1239#ifdef SK_BUILD_FOR_WIN
1240 GrContextFactory::kANGLE_GLContextType,
1241#endif
1242 GrContextFactory::kANGLE_GL_GLContextType,
1243#endif
1244#if SK_COMMAND_BUFFER
1245 GrContextFactory::kCommandBuffer_GLContextType,
1246#endif
1247#if SK_MESA
1248 GrContextFactory::kMESA_GLContextType,
1249#endif
1250 GrContextFactory::kNull_GLContextType,
1251 GrContextFactory::kDebug_GLContextType,
1252 };
1253 static_assert(SK_ARRAY_COUNT(contextTypes) == GrContextFactory::kGLContextTypeCnt - 2,
1254 "Skipping unexpected GLContextType for GPU tests");
1255
1256 for (auto& contextType : contextTypes) {
kkinnunen179a8f52015-11-20 13:32:24 -08001257 int contextSelector = kNone_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001258 if (GrContextFactory::IsRenderingGLContext(contextType)) {
kkinnunen179a8f52015-11-20 13:32:24 -08001259 contextSelector |= kAllRendering_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001260 } else if (contextType == GrContextFactory::kNative_GLContextType) {
kkinnunen179a8f52015-11-20 13:32:24 -08001261 contextSelector |= kNative_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001262 } else if (contextType == GrContextFactory::kNull_GLContextType) {
kkinnunen179a8f52015-11-20 13:32:24 -08001263 contextSelector |= kNull_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001264 } else if (contextType == GrContextFactory::kDebug_GLContextType) {
kkinnunen55eeae92015-12-10 23:19:29 -08001265 contextSelector |= kDebug_GPUTestContexts;
kkinnunen179a8f52015-11-20 13:32:24 -08001266 }
1267 if ((testContexts & contextSelector) == 0) {
1268 continue;
1269 }
kkinnunen34058002016-01-06 23:49:30 -08001270 GrContextFactory::ContextInfo context = factory->getContextInfo(contextType);
1271 if (context.fGrContext) {
kkinnunen5219fd92015-12-10 06:28:13 -08001272 call_test(test, reporter, context);
1273 }
kkinnunen34058002016-01-06 23:49:30 -08001274 context = factory->getContextInfo(contextType,
1275 GrContextFactory::kEnableNVPR_GLContextOptions);
1276 if (context.fGrContext) {
kkinnunen179a8f52015-11-20 13:32:24 -08001277 call_test(test, reporter, context);
1278 }
1279 }
1280#endif
1281}
1282
1283template
1284void RunWithGPUTestContexts<TestWithGrContext>(TestWithGrContext test,
1285 GPUTestContexts testContexts,
1286 Reporter* reporter,
1287 GrContextFactory* factory);
1288template
1289void RunWithGPUTestContexts<TestWithGrContextAndGLContext>(TestWithGrContextAndGLContext test,
1290 GPUTestContexts testContexts,
1291 Reporter* reporter,
1292 GrContextFactory* factory);
1293} // namespace skiatest
1294
borenet48087572015-04-02 12:16:36 -07001295#if !defined(SK_BUILD_FOR_IOS)
jcgregorio3b27ade2014-11-13 08:06:40 -08001296int main(int argc, char** argv) {
1297 SkCommandLineFlags::Parse(argc, argv);
1298 return dm_main();
1299}
1300#endif