blob: c7c3628d8a82e8236c83a6991a402f5ee194cb2b [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;
msarettb65e6042016-02-23 05:37:25 -0800365 case SkEncodedFormat::kRAW_SkEncodedFormat:
366 break;
msarett91c22b22016-02-22 12:27:46 -0800367 default:
368 nativeModes.push_back(CodecSrc::kScanline_Mode);
369 nativeModes.push_back(CodecSrc::kStripe_Mode);
370 break;
371 }
msarett9e707a02015-09-01 14:57:57 -0700372
msarett91c22b22016-02-22 12:27:46 -0800373 SkTArray<CodecSrc::DstColorType> colorTypes;
374 colorTypes.push_back(CodecSrc::kGetFromCanvas_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700375 switch (codec->getInfo().colorType()) {
376 case kGray_8_SkColorType:
msarett91c22b22016-02-22 12:27:46 -0800377 colorTypes.push_back(CodecSrc::kGrayscale_Always_DstColorType);
msarett55f7bdd2016-02-16 13:24:54 -0800378 if (kWBMP_SkEncodedFormat == codec->getEncodedFormat()) {
msarett91c22b22016-02-22 12:27:46 -0800379 colorTypes.push_back(CodecSrc::kIndex8_Always_DstColorType);
msarett55f7bdd2016-02-16 13:24:54 -0800380 }
msarett36c37962015-09-02 13:20:52 -0700381 break;
382 case kIndex_8_SkColorType:
msarett91c22b22016-02-22 12:27:46 -0800383 colorTypes.push_back(CodecSrc::kIndex8_Always_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700384 break;
385 default:
msarett36c37962015-09-02 13:20:52 -0700386 break;
387 }
msarett9e707a02015-09-01 14:57:57 -0700388
scroggoc5560be2016-02-03 09:42:42 -0800389 SkTArray<SkAlphaType> alphaModes;
390 alphaModes.push_back(kPremul_SkAlphaType);
msarett9e9444c2016-02-03 12:39:10 -0800391 alphaModes.push_back(kUnpremul_SkAlphaType);
scroggoc5560be2016-02-03 09:42:42 -0800392 if (codec->getInfo().alphaType() == kOpaque_SkAlphaType) {
393 alphaModes.push_back(kOpaque_SkAlphaType);
394 }
msarettb714fb02016-01-22 14:46:42 -0800395
396 for (CodecSrc::Mode mode : nativeModes) {
397 // SkCodecImageGenerator only runs for the default colorType
398 // recommended by SkCodec. There is no need to generate multiple
399 // tests for different colorTypes.
400 // TODO (msarett): Add scaling support to SkCodecImageGenerator.
401 if (CodecSrc::kGen_Mode == mode) {
402 // FIXME: The gpu backend does not draw kGray sources correctly. (skbug.com/4822)
403 if (kGray_8_SkColorType != codec->getInfo().colorType()) {
scroggoc5560be2016-02-03 09:42:42 -0800404 push_codec_src(path, mode, CodecSrc::kGetFromCanvas_DstColorType,
405 codec->getInfo().alphaType(), 1.0f);
msarettb714fb02016-01-22 14:46:42 -0800406 }
407 continue;
408 }
409
410 for (float scale : nativeScales) {
msarett91c22b22016-02-22 12:27:46 -0800411 for (CodecSrc::DstColorType colorType : colorTypes) {
scroggoc5560be2016-02-03 09:42:42 -0800412 for (SkAlphaType alphaType : alphaModes) {
msarett91c22b22016-02-22 12:27:46 -0800413 // Only test kCroppedScanline_Mode when the alpha type is opaque. The test is
414 // slow and won't be interestingly different with different alpha types.
415 if (CodecSrc::kCroppedScanline_Mode == mode &&
416 kOpaque_SkAlphaType != alphaType) {
417 continue;
418 }
419
420 push_codec_src(path, mode, colorType, alphaType, scale);
scroggoc5560be2016-02-03 09:42:42 -0800421 }
msarett9e707a02015-09-01 14:57:57 -0700422 }
423 }
msarett0a242972015-06-11 14:27:27 -0700424 }
msarett36c37962015-09-02 13:20:52 -0700425
halcanary6950de62015-11-07 05:29:00 -0800426 // https://bug.skia.org/4428
msarett33c76232015-11-16 13:43:40 -0800427 bool subset = false;
428 // The following image types are supported by BitmapRegionDecoder,
429 // so we will test full image decodes and subset decodes.
msarettbe8216a2015-12-04 08:00:50 -0800430 static const char* const exts[] = {
msarett3d9d7a72015-10-21 10:27:10 -0700431 "jpg", "jpeg", "png", "webp",
432 "JPG", "JPEG", "PNG", "WEBP",
433 };
msarettbe8216a2015-12-04 08:00:50 -0800434 for (const char* ext : exts) {
msarett3d9d7a72015-10-21 10:27:10 -0700435 if (path.endsWith(ext)) {
msarett33c76232015-11-16 13:43:40 -0800436 subset = true;
msarett3d9d7a72015-10-21 10:27:10 -0700437 break;
438 }
439 }
msarett33c76232015-11-16 13:43:40 -0800440
msarett3d9d7a72015-10-21 10:27:10 -0700441 const int sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
msarett36c37962015-09-02 13:20:52 -0700442
msarett3d9d7a72015-10-21 10:27:10 -0700443 for (int sampleSize : sampleSizes) {
msarett91c22b22016-02-22 12:27:46 -0800444 for (CodecSrc::DstColorType colorType : colorTypes) {
scroggoc5560be2016-02-03 09:42:42 -0800445 for (SkAlphaType alphaType : alphaModes) {
msarett91c22b22016-02-22 12:27:46 -0800446 push_android_codec_src(path, AndroidCodecSrc::kFullImage_Mode, colorType,
scroggoc5560be2016-02-03 09:42:42 -0800447 alphaType, sampleSize);
448 if (subset) {
msarett91c22b22016-02-22 12:27:46 -0800449 push_android_codec_src(path, AndroidCodecSrc::kDivisor_Mode, colorType,
scroggoc5560be2016-02-03 09:42:42 -0800450 alphaType, sampleSize);
451 }
msarett3d9d7a72015-10-21 10:27:10 -0700452 }
msarett36c37962015-09-02 13:20:52 -0700453 }
454 }
msarett438b2ad2015-04-09 12:43:10 -0700455}
456
msarett5cb48852015-11-06 08:56:32 -0800457static bool brd_color_type_supported(SkBitmapRegionDecoder::Strategy strategy,
msaretta5783ae2015-09-08 15:35:32 -0700458 CodecSrc::DstColorType dstColorType) {
459 switch (strategy) {
msarett5cb48852015-11-06 08:56:32 -0800460 case SkBitmapRegionDecoder::kCanvas_Strategy:
msaretta5783ae2015-09-08 15:35:32 -0700461 if (CodecSrc::kGetFromCanvas_DstColorType == dstColorType) {
462 return true;
463 }
464 return false;
msarett5cb48852015-11-06 08:56:32 -0800465 case SkBitmapRegionDecoder::kAndroidCodec_Strategy:
msarett26ad17b2015-10-22 07:29:19 -0700466 switch (dstColorType) {
467 case CodecSrc::kGetFromCanvas_DstColorType:
468 case CodecSrc::kIndex8_Always_DstColorType:
469 case CodecSrc::kGrayscale_Always_DstColorType:
470 return true;
471 default:
472 return false;
473 }
msaretta5783ae2015-09-08 15:35:32 -0700474 default:
475 SkASSERT(false);
476 return false;
477 }
478}
479
msarett5cb48852015-11-06 08:56:32 -0800480static void push_brd_src(Path path, SkBitmapRegionDecoder::Strategy strategy,
msaretta5783ae2015-09-08 15:35:32 -0700481 CodecSrc::DstColorType dstColorType, BRDSrc::Mode mode, uint32_t sampleSize) {
482 SkString folder;
483 switch (strategy) {
msarett5cb48852015-11-06 08:56:32 -0800484 case SkBitmapRegionDecoder::kCanvas_Strategy:
msaretta5783ae2015-09-08 15:35:32 -0700485 folder.append("brd_canvas");
486 break;
msarett5cb48852015-11-06 08:56:32 -0800487 case SkBitmapRegionDecoder::kAndroidCodec_Strategy:
msarett26ad17b2015-10-22 07:29:19 -0700488 folder.append("brd_android_codec");
489 break;
msaretta5783ae2015-09-08 15:35:32 -0700490 default:
491 SkASSERT(false);
492 return;
493 }
494
495 switch (mode) {
496 case BRDSrc::kFullImage_Mode:
497 break;
498 case BRDSrc::kDivisor_Mode:
499 folder.append("_divisor");
500 break;
501 default:
502 SkASSERT(false);
503 return;
504 }
505
506 switch (dstColorType) {
507 case CodecSrc::kGetFromCanvas_DstColorType:
508 break;
509 case CodecSrc::kIndex8_Always_DstColorType:
510 folder.append("_kIndex");
511 break;
512 case CodecSrc::kGrayscale_Always_DstColorType:
513 folder.append("_kGray");
514 break;
515 default:
516 SkASSERT(false);
517 return;
518 }
519
520 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800521 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700522 }
523
524 BRDSrc* src = new BRDSrc(path, strategy, mode, dstColorType, sampleSize);
525 push_src("image", folder, src);
526}
527
528static void push_brd_srcs(Path path) {
529
msarett5cb48852015-11-06 08:56:32 -0800530 const SkBitmapRegionDecoder::Strategy strategies[] = {
531 SkBitmapRegionDecoder::kCanvas_Strategy,
msarett5cb48852015-11-06 08:56:32 -0800532 SkBitmapRegionDecoder::kAndroidCodec_Strategy,
msaretta5783ae2015-09-08 15:35:32 -0700533 };
534
scroggo501b7342015-11-03 07:55:11 -0800535 // Test on a variety of sampleSizes, making sure to include:
536 // - 2, 4, and 8, which are natively supported by jpeg
537 // - multiples of 2 which are not divisible by 4 (analogous for 4)
538 // - larger powers of two, since BRD clients generally use powers of 2
539 // We will only produce output for the larger sizes on large images.
540 const uint32_t sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 24, 32, 64 };
msarett6efbe052015-09-11 09:01:16 -0700541
msaretta5783ae2015-09-08 15:35:32 -0700542 // We will only test to one backend (8888), but we will test all of the
543 // color types that we need to decode to on this backend.
544 const CodecSrc::DstColorType dstColorTypes[] = {
msarett26ad17b2015-10-22 07:29:19 -0700545 CodecSrc::kGetFromCanvas_DstColorType,
546 CodecSrc::kIndex8_Always_DstColorType,
547 CodecSrc::kGrayscale_Always_DstColorType,
msaretta5783ae2015-09-08 15:35:32 -0700548 };
549
550 const BRDSrc::Mode modes[] = {
msarett26ad17b2015-10-22 07:29:19 -0700551 BRDSrc::kFullImage_Mode,
552 BRDSrc::kDivisor_Mode,
msaretta5783ae2015-09-08 15:35:32 -0700553 };
554
msarett5cb48852015-11-06 08:56:32 -0800555 for (SkBitmapRegionDecoder::Strategy strategy : strategies) {
msarett6efbe052015-09-11 09:01:16 -0700556 for (uint32_t sampleSize : sampleSizes) {
msarett6efbe052015-09-11 09:01:16 -0700557 for (CodecSrc::DstColorType dstColorType : dstColorTypes) {
558 if (brd_color_type_supported(strategy, dstColorType)) {
559 for (BRDSrc::Mode mode : modes) {
msaretta5783ae2015-09-08 15:35:32 -0700560 push_brd_src(path, strategy, dstColorType, mode, sampleSize);
561 }
562 }
563 }
564 }
565 }
566}
567
568static bool brd_supported(const char* ext) {
msarett8c8f22a2015-04-01 06:58:48 -0700569 static const char* const exts[] = {
msaretta5783ae2015-09-08 15:35:32 -0700570 "jpg", "jpeg", "png", "webp",
571 "JPG", "JPEG", "PNG", "WEBP",
msarett8c8f22a2015-04-01 06:58:48 -0700572 };
573
574 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
575 if (0 == strcmp(exts[i], ext)) {
576 return true;
577 }
578 }
579 return false;
scroggo9b77ddd2015-03-19 06:03:39 -0700580}
581
scroggo86737142016-02-03 12:19:11 -0800582static bool gather_srcs() {
mtklein748ca3b2015-01-15 10:56:12 -0800583 for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) {
djsollen54416de2015-04-03 07:24:48 -0700584 push_src("gm", "", new GMSrc(r->factory()));
mtklein748ca3b2015-01-15 10:56:12 -0800585 }
halcanaryfc37ad12015-01-30 07:31:19 -0800586 for (int i = 0; i < FLAGS_skps.count(); i++) {
587 const char* path = FLAGS_skps[i];
588 if (sk_isdir(path)) {
589 SkOSFile::Iter it(path, "skp");
590 for (SkString file; it.next(&file); ) {
djsollen54416de2015-04-03 07:24:48 -0700591 push_src("skp", "", new SKPSrc(SkOSPath::Join(path, file.c_str())));
halcanaryfc37ad12015-01-30 07:31:19 -0800592 }
593 } else {
djsollen54416de2015-04-03 07:24:48 -0700594 push_src("skp", "", new SKPSrc(path));
mtklein114c3cd2015-01-15 10:15:02 -0800595 }
596 }
scroggo86737142016-02-03 12:19:11 -0800597
598 SkTArray<SkString> images;
599 if (!CollectImages(&images)) {
600 return false;
601 }
602
603 for (auto image : images) {
604 push_codec_srcs(image);
mtklein21eaf3b2016-02-08 12:39:59 -0800605 const char* ext = strrchr(image.c_str(), '.');
606 if (ext && brd_supported(ext+1)) {
scroggo86737142016-02-03 12:19:11 -0800607 push_brd_srcs(image);
mtklein709d2c32015-01-15 08:30:25 -0800608 }
mtklein709d2c32015-01-15 08:30:25 -0800609 }
scroggo86737142016-02-03 12:19:11 -0800610
611 return true;
mtklein709d2c32015-01-15 08:30:25 -0800612}
613
kkinnunen3e980c32015-12-23 01:33:00 -0800614static void push_sink(const SkCommandLineConfig& config, Sink* s) {
rmistry0f515bd2015-12-22 10:22:26 -0800615 SkAutoTDelete<Sink> sink(s);
kkinnunen3e980c32015-12-23 01:33:00 -0800616
mtkleine0effd62015-07-29 06:37:28 -0700617 // Try a simple Src as a canary. If it fails, skip this sink.
mtklein748ca3b2015-01-15 10:56:12 -0800618 struct : public Src {
mtkleine0effd62015-07-29 06:37:28 -0700619 Error draw(SkCanvas* c) const override {
620 c->drawRect(SkRect::MakeWH(1,1), SkPaint());
621 return "";
622 }
mtklein36352bf2015-03-25 18:17:31 -0700623 SkISize size() const override { return SkISize::Make(16, 16); }
mtkleine0effd62015-07-29 06:37:28 -0700624 Name name() const override { return "justOneRect"; }
625 } justOneRect;
mtklein114c3cd2015-01-15 10:15:02 -0800626
mtklein748ca3b2015-01-15 10:56:12 -0800627 SkBitmap bitmap;
628 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800629 SkString log;
mtkleine0effd62015-07-29 06:37:28 -0700630 Error err = sink->draw(justOneRect, &bitmap, &stream, &log);
mtklein4089ef72015-03-05 08:40:28 -0800631 if (err.isFatal()) {
kkinnunen3e980c32015-12-23 01:33:00 -0800632 SkDebugf("Could not run %s: %s\n", config.getTag().c_str(), err.c_str());
mtklein05641a52015-04-21 10:49:13 -0700633 exit(1);
mtklein114c3cd2015-01-15 10:15:02 -0800634 }
635
mtkleine0effd62015-07-29 06:37:28 -0700636 TaggedSink& ts = gSinks.push_back();
mtklein748ca3b2015-01-15 10:56:12 -0800637 ts.reset(sink.detach());
kkinnunen3e980c32015-12-23 01:33:00 -0800638 ts.tag = config.getTag();
mtklein748ca3b2015-01-15 10:56:12 -0800639}
640
641static bool gpu_supported() {
642#if SK_SUPPORT_GPU
643 return FLAGS_gpu;
644#else
645 return false;
646#endif
647}
kkinnunen9ebc3f02015-12-21 23:48:13 -0800648
kkinnunen3e980c32015-12-23 01:33:00 -0800649static Sink* create_sink(const SkCommandLineConfig* config) {
650#if SK_SUPPORT_GPU
651 if (gpu_supported()) {
652 if (const SkCommandLineConfigGpu* gpuConfig = config->asConfigGpu()) {
653 GrContextFactory::GLContextType contextType = gpuConfig->getContextType();
654 GrContextFactory::GLContextOptions contextOptions =
655 GrContextFactory::kNone_GLContextOptions;
656 if (gpuConfig->getUseNVPR()) {
657 contextOptions = static_cast<GrContextFactory::GLContextOptions>(
658 contextOptions | GrContextFactory::kEnableNVPR_GLContextOptions);
659 }
660 GrContextFactory testFactory;
661 if (!testFactory.get(contextType, contextOptions)) {
662 SkDebugf("WARNING: can not create GPU context for config '%s'. "
663 "GM tests will be skipped.\n", gpuConfig->getTag().c_str());
664 return nullptr;
665 }
666 return new GPUSink(contextType, contextOptions, gpuConfig->getSamples(),
667 gpuConfig->getUseDIText(), FLAGS_gpu_threading);
668 }
669 }
670#endif
671
672#define SINK(t, sink, ...) if (config->getBackend().equals(t)) { return new sink(__VA_ARGS__); }
673
tomhudsoneebc39a2015-02-23 12:18:05 -0800674#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
675 SINK("hwui", HWUISink);
676#endif
677
mtklein748ca3b2015-01-15 10:56:12 -0800678 if (FLAGS_cpu) {
679 SINK("565", RasterSink, kRGB_565_SkColorType);
680 SINK("8888", RasterSink, kN32_SkColorType);
halcanaryc11c62f2015-09-28 11:51:54 -0700681 SINK("pdf", PDFSink, "Pdfium");
682 SINK("pdf_poppler", PDFSink, "Poppler");
mtklein9c3f17d2015-01-28 11:35:18 -0800683 SINK("skp", SKPSink);
mtklein8a4527e2015-01-31 20:00:58 -0800684 SINK("svg", SVGSink);
685 SINK("null", NullSink);
halcanary47ef4d52015-03-03 09:13:09 -0800686 SINK("xps", XPSSink);
mtklein748ca3b2015-01-15 10:56:12 -0800687 }
688#undef SINK
halcanary96fcdcc2015-08-27 07:41:13 -0700689 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800690}
691
kkinnunen3e980c32015-12-23 01:33:00 -0800692static Sink* create_via(const SkString& tag, Sink* wrapped) {
693#define VIA(t, via, ...) if (tag.equals(t)) { return new via(__VA_ARGS__); }
halcanary96fcdcc2015-08-27 07:41:13 -0700694 VIA("twice", ViaTwice, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700695 VIA("serialize", ViaSerialization, wrapped);
mtklein4a34ecb2016-01-08 10:19:35 -0800696 VIA("pic", ViaPicture, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700697 VIA("2ndpic", ViaSecondPicture, wrapped);
mtkleind31c13d2015-05-05 12:59:56 -0700698 VIA("sp", ViaSingletonPictures, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700699 VIA("tiles", ViaTiles, 256, 256, nullptr, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800700 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
mtklein2e2ea382015-10-16 10:29:41 -0700701 VIA("remote", ViaRemote, false, wrapped);
702 VIA("remote_cache", ViaRemote, true, wrapped);
halcanary7a76f9c2016-02-03 11:53:18 -0800703 VIA("mojo", ViaMojo, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800704
mtkleind603b222015-02-17 11:13:33 -0800705 if (FLAGS_matrix.count() == 4) {
mtklein748ca3b2015-01-15 10:56:12 -0800706 SkMatrix m;
mtkleind603b222015-02-17 11:13:33 -0800707 m.reset();
708 m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
709 m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
710 m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
711 m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
712 VIA("matrix", ViaMatrix, m, wrapped);
713 VIA("upright", ViaUpright, m, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800714 }
tomhudson64de1e12015-03-05 08:01:07 -0800715
716#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
717 VIA("androidsdk", ViaAndroidSDK, wrapped);
718#endif
719
mtklein748ca3b2015-01-15 10:56:12 -0800720#undef VIA
halcanary96fcdcc2015-08-27 07:41:13 -0700721 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800722}
723
mtklein748ca3b2015-01-15 10:56:12 -0800724static void gather_sinks() {
kkinnunen3e980c32015-12-23 01:33:00 -0800725 SkCommandLineConfigArray configs;
726 ParseConfigs(FLAGS_config, &configs);
727 for (int i = 0; i < configs.count(); i++) {
728 const SkCommandLineConfig& config = *configs[i];
729 Sink* sink = create_sink(&config);
730 if (sink == nullptr) {
731 SkDebugf("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
732 config.getTag().c_str());
733 continue;
734 }
mtklein748ca3b2015-01-15 10:56:12 -0800735
kkinnunen3e980c32015-12-23 01:33:00 -0800736 const SkTArray<SkString>& parts = config.getViaParts();
737 for (int j = parts.count(); j-- > 0;) {
738 const SkString& part = parts[j];
739 Sink* next = create_via(part, sink);
halcanary96fcdcc2015-08-27 07:41:13 -0700740 if (next == nullptr) {
kkinnunen3e980c32015-12-23 01:33:00 -0800741 SkDebugf("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
742 part.c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800743 delete sink;
halcanary96fcdcc2015-08-27 07:41:13 -0700744 sink = nullptr;
mtklein748ca3b2015-01-15 10:56:12 -0800745 break;
746 }
747 sink = next;
748 }
749 if (sink) {
750 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800751 }
752 }
753}
mtklein709d2c32015-01-15 08:30:25 -0800754
mtkleina5114d72015-08-24 13:27:01 -0700755static bool dump_png(SkBitmap bitmap, const char* path, const char* md5) {
756 const int w = bitmap.width(),
757 h = bitmap.height();
758
759 // First get the bitmap into N32 color format. The next step will work only there.
760 if (bitmap.colorType() != kN32_SkColorType) {
761 SkBitmap n32;
762 if (!bitmap.copyTo(&n32, kN32_SkColorType)) {
763 return false;
764 }
765 bitmap = n32;
766 }
767
768 // Convert our N32 bitmap into unpremul RGBA for libpng.
769 SkAutoTMalloc<uint32_t> rgba(w*h);
770 if (!bitmap.readPixels(SkImageInfo::Make(w,h, kRGBA_8888_SkColorType, kUnpremul_SkAlphaType),
771 rgba, 4*w, 0,0)) {
772 return false;
773 }
774
775 // We don't need bitmap anymore. Might as well drop our ref.
mtkleinfccb77d2015-08-24 14:13:29 -0700776 bitmap.reset();
mtkleina5114d72015-08-24 13:27:01 -0700777
mtkleinc64137c2015-08-25 10:56:08 -0700778 FILE* f = fopen(path, "wb");
mtkleina5114d72015-08-24 13:27:01 -0700779 if (!f) { return false; }
780
781 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
782 if (!png) {
783 fclose(f);
784 return false;
785 }
786
787 png_infop info = png_create_info_struct(png);
788 if (!info) {
789 png_destroy_write_struct(&png, &info);
790 fclose(f);
791 return false;
792 }
793
mtkleind69ece62015-09-10 10:37:44 -0700794 SkString description;
795 description.append("Key: ");
796 for (int i = 0; i < FLAGS_key.count(); i++) {
797 description.appendf("%s ", FLAGS_key[i]);
798 }
799 description.append("Properties: ");
800 for (int i = 0; i < FLAGS_properties.count(); i++) {
801 description.appendf("%s ", FLAGS_properties[i]);
802 }
803 description.appendf("MD5: %s", md5);
804
mtkleina5114d72015-08-24 13:27:01 -0700805 png_text text[2];
806 text[0].key = (png_charp)"Author";
807 text[0].text = (png_charp)"DM dump_png()";
808 text[0].compression = PNG_TEXT_COMPRESSION_NONE;
809 text[1].key = (png_charp)"Description";
mtkleind69ece62015-09-10 10:37:44 -0700810 text[1].text = (png_charp)description.c_str();
mtkleina5114d72015-08-24 13:27:01 -0700811 text[1].compression = PNG_TEXT_COMPRESSION_NONE;
812 png_set_text(png, info, text, 2);
813
814 png_init_io(png, f);
815 png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8,
816 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
817 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
818 png_write_info(png, info);
819 for (int j = 0; j < h; j++) {
820 png_bytep row = (png_bytep)(rgba.get() + w*j);
821 png_write_rows(png, &row, 1);
822 }
823 png_write_end(png, info);
824
825 png_destroy_write_struct(&png, &info);
826 fclose(f);
827 return true;
828}
829
mtkleina2ef6422015-01-15 13:44:22 -0800830static bool match(const char* needle, const char* haystack) {
halcanary96fcdcc2015-08-27 07:41:13 -0700831 return 0 == strcmp("_", needle) || nullptr != strstr(haystack, needle);
mtkleina2ef6422015-01-15 13:44:22 -0800832}
833
djsollen54416de2015-04-03 07:24:48 -0700834static ImplicitString is_blacklisted(const char* sink, const char* src,
835 const char* srcOptions, const char* name) {
mtklein0d243ff2015-04-03 07:51:00 -0700836 for (int i = 0; i < FLAGS_blacklist.count() - 3; i += 4) {
mtkleina2ef6422015-01-15 13:44:22 -0800837 if (match(FLAGS_blacklist[i+0], sink) &&
djsollen54416de2015-04-03 07:24:48 -0700838 match(FLAGS_blacklist[i+1], src) &&
839 match(FLAGS_blacklist[i+2], srcOptions) &&
840 match(FLAGS_blacklist[i+3], name)) {
841 return SkStringPrintf("%s %s %s %s",
842 FLAGS_blacklist[i+0], FLAGS_blacklist[i+1],
843 FLAGS_blacklist[i+2], FLAGS_blacklist[i+3]);
mtkleina2ef6422015-01-15 13:44:22 -0800844 }
845 }
846 return "";
847}
848
mtkleincd50bca2016-01-05 06:20:20 -0800849// Even when a Task Sink reports to be non-threadsafe (e.g. GPU), we know things like
850// .png encoding are definitely thread safe. This lets us offload that work to CPU threads.
851static SkTaskGroup gDefinitelyThreadSafeWork;
852
mtklein748ca3b2015-01-15 10:56:12 -0800853// The finest-grained unit of work we can run: draw a single Src into a single Sink,
854// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
855struct Task {
mtkleine0effd62015-07-29 06:37:28 -0700856 Task(const TaggedSrc& src, const TaggedSink& sink) : src(src), sink(sink) {}
857 const TaggedSrc& src;
858 const TaggedSink& sink;
mtklein748ca3b2015-01-15 10:56:12 -0800859
mtklein21eaf3b2016-02-08 12:39:59 -0800860 static void Run(const Task& task) {
861 SkString name = task.src->name();
mtkleine0effd62015-07-29 06:37:28 -0700862
863 // We'll skip drawing this Src/Sink pair if:
864 // - the Src vetoes the Sink;
865 // - this Src / Sink combination is on the blacklist;
866 // - it's a dry run.
mtklein21eaf3b2016-02-08 12:39:59 -0800867 SkString note(task.src->veto(task.sink->flags()) ? " (veto)" : "");
868 SkString whyBlacklisted = is_blacklisted(task.sink.tag.c_str(), task.src.tag.c_str(),
869 task.src.options.c_str(), name.c_str());
mtkleinb37cb412015-03-18 05:27:14 -0700870 if (!whyBlacklisted.isEmpty()) {
871 note.appendf(" (--blacklist %s)", whyBlacklisted.c_str());
872 }
mtkleine0effd62015-07-29 06:37:28 -0700873
mtkleinb9eb4ac2015-02-02 18:26:03 -0800874 SkString log;
mtkleinf27f08b2015-11-03 06:54:24 -0800875 auto timerStart = now_ms();
mtkleine0effd62015-07-29 06:37:28 -0700876 if (!FLAGS_dryRun && note.isEmpty()) {
mtklein748ca3b2015-01-15 10:56:12 -0800877 SkBitmap bitmap;
878 SkDynamicMemoryWStream stream;
bsalomon821e10e2015-06-04 14:15:33 -0700879 if (FLAGS_pre_log) {
mtklein21eaf3b2016-02-08 12:39:59 -0800880 SkDebugf("\nRunning %s->%s", name.c_str(), task.sink.tag.c_str());
bsalomon821e10e2015-06-04 14:15:33 -0700881 }
mtklein21eaf3b2016-02-08 12:39:59 -0800882 start(task.sink.tag.c_str(), task.src.tag, task.src.options, name.c_str());
883 Error err = task.sink->draw(*task.src, &bitmap, &stream, &log);
mtklein748ca3b2015-01-15 10:56:12 -0800884 if (!err.isEmpty()) {
mtklein4089ef72015-03-05 08:40:28 -0800885 if (err.isFatal()) {
djsollen54416de2015-04-03 07:24:48 -0700886 fail(SkStringPrintf("%s %s %s %s: %s",
mtklein21eaf3b2016-02-08 12:39:59 -0800887 task.sink.tag.c_str(),
888 task.src.tag.c_str(),
889 task.src.options.c_str(),
mtklein4089ef72015-03-05 08:40:28 -0800890 name.c_str(),
891 err.c_str()));
mtkleinb37cb412015-03-18 05:27:14 -0700892 } else {
893 note.appendf(" (skipped: %s)", err.c_str());
mtkleinba6ada72016-01-21 09:39:35 -0800894 auto elapsed = now_ms() - timerStart;
mtklein21eaf3b2016-02-08 12:39:59 -0800895 done(elapsed, task.sink.tag.c_str(), task.src.tag, task.src.options,
mtkleinba6ada72016-01-21 09:39:35 -0800896 name, note, log);
897 return;
mtklein4089ef72015-03-05 08:40:28 -0800898 }
mtklein748ca3b2015-01-15 10:56:12 -0800899 }
mtklein62bd1a62015-01-27 14:46:26 -0800900
mtkleincd50bca2016-01-05 06:20:20 -0800901 // We're likely switching threads here, so we must capture by value, [=] or [foo,bar].
902 SkStreamAsset* data = stream.detachAsStream();
903 gDefinitelyThreadSafeWork.add([task,name,bitmap,data]{
904 SkAutoTDelete<SkStreamAsset> ownedData(data);
905
906 // Why doesn't the copy constructor do this when we have pre-locked pixels?
907 bitmap.lockPixels();
908
909 SkString md5;
910 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
911 SkMD5 hash;
912 if (data->getLength()) {
913 hash.writeStream(data, data->getLength());
914 data->rewind();
mtklein38408462015-07-08 07:25:27 -0700915 } else {
mtkleincd50bca2016-01-05 06:20:20 -0800916 // If we're BGRA (Linux, Windows), swizzle over to RGBA (Mac, Android).
917 // This helps eliminate multiple 0-pixel-diff hashes on gold.skia.org.
918 // (Android's general slow speed breaks the tie arbitrarily in RGBA's favor.)
919 // We might consider promoting 565 to RGBA too.
920 if (bitmap.colorType() == kBGRA_8888_SkColorType) {
921 SkBitmap swizzle;
922 SkAssertResult(bitmap.copyTo(&swizzle, kRGBA_8888_SkColorType));
923 hash.write(swizzle.getPixels(), swizzle.getSize());
924 } else {
925 hash.write(bitmap.getPixels(), bitmap.getSize());
926 }
927 }
928 SkMD5::Digest digest;
929 hash.finish(digest);
930 for (int i = 0; i < 16; i++) {
931 md5.appendf("%02x", digest.data[i]);
mtklein38408462015-07-08 07:25:27 -0700932 }
mtklein62bd1a62015-01-27 14:46:26 -0800933 }
mtklein62bd1a62015-01-27 14:46:26 -0800934
mtkleincd50bca2016-01-05 06:20:20 -0800935 if (!FLAGS_readPath.isEmpty() &&
mtklein21eaf3b2016-02-08 12:39:59 -0800936 !gGold.contains(Gold(task.sink.tag.c_str(), task.src.tag.c_str(),
937 task.src.options.c_str(), name, md5))) {
mtkleincd50bca2016-01-05 06:20:20 -0800938 fail(SkStringPrintf("%s not found for %s %s %s %s in %s",
939 md5.c_str(),
mtklein21eaf3b2016-02-08 12:39:59 -0800940 task.sink.tag.c_str(),
941 task.src.tag.c_str(),
942 task.src.options.c_str(),
mtkleincd50bca2016-01-05 06:20:20 -0800943 name.c_str(),
944 FLAGS_readPath[0]));
mtklein748ca3b2015-01-15 10:56:12 -0800945 }
mtkleincd50bca2016-01-05 06:20:20 -0800946
947 if (!FLAGS_writePath.isEmpty()) {
mtklein21eaf3b2016-02-08 12:39:59 -0800948 const char* ext = task.sink->fileExtension();
mtkleincd50bca2016-01-05 06:20:20 -0800949 if (data->getLength()) {
mtklein21eaf3b2016-02-08 12:39:59 -0800950 WriteToDisk(task, md5, ext, data, data->getLength(), nullptr);
mtkleincd50bca2016-01-05 06:20:20 -0800951 SkASSERT(bitmap.drawsNothing());
952 } else if (!bitmap.drawsNothing()) {
mtklein21eaf3b2016-02-08 12:39:59 -0800953 WriteToDisk(task, md5, ext, nullptr, 0, &bitmap);
mtkleincd50bca2016-01-05 06:20:20 -0800954 }
955 }
956 });
mtklein748ca3b2015-01-15 10:56:12 -0800957 }
mtkleinba6ada72016-01-21 09:39:35 -0800958 auto elapsed = now_ms() - timerStart;
mtklein21eaf3b2016-02-08 12:39:59 -0800959 done(elapsed, task.sink.tag.c_str(), task.src.tag.c_str(), task.src.options.c_str(),
mtkleinf27f08b2015-11-03 06:54:24 -0800960 name, note, log);
mtklein748ca3b2015-01-15 10:56:12 -0800961 }
962
963 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -0800964 SkString md5,
965 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -0800966 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -0800967 const SkBitmap* bitmap) {
mtklein748ca3b2015-01-15 10:56:12 -0800968 JsonWriter::BitmapResult result;
djsollen54416de2015-04-03 07:24:48 -0700969 result.name = task.src->name();
kkinnunen3e980c32015-12-23 01:33:00 -0800970 result.config = task.sink.tag.c_str();
djsollen54416de2015-04-03 07:24:48 -0700971 result.sourceType = task.src.tag;
972 result.sourceOptions = task.src.options;
973 result.ext = ext;
974 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -0800975 JsonWriter::AddBitmapResult(result);
976
mtkleinb0531a72015-04-07 13:38:48 -0700977 // If an MD5 is uninteresting, we want it noted in the JSON file,
978 // but don't want to dump it out as a .png (or whatever ext is).
979 if (gUninterestingHashes.contains(md5)) {
980 return;
981 }
982
mtklein748ca3b2015-01-15 10:56:12 -0800983 const char* dir = FLAGS_writePath[0];
984 if (0 == strcmp(dir, "@")) { // Needed for iOS.
985 dir = FLAGS_resourcePath[0];
986 }
987 sk_mkdir(dir);
988
989 SkString path;
990 if (FLAGS_nameByHash) {
991 path = SkOSPath::Join(dir, result.md5.c_str());
992 path.append(".");
993 path.append(ext);
994 if (sk_exists(path.c_str())) {
995 return; // Content-addressed. If it exists already, we're done.
996 }
997 } else {
kkinnunen3e980c32015-12-23 01:33:00 -0800998 path = SkOSPath::Join(dir, task.sink.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800999 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001000 path = SkOSPath::Join(path.c_str(), task.src.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001001 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001002 if (strcmp(task.src.options.c_str(), "") != 0) {
1003 path = SkOSPath::Join(path.c_str(), task.src.options.c_str());
djsollen54416de2015-04-03 07:24:48 -07001004 sk_mkdir(path.c_str());
1005 }
mtklein748ca3b2015-01-15 10:56:12 -08001006 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
1007 path.append(".");
1008 path.append(ext);
1009 }
1010
mtklein748ca3b2015-01-15 10:56:12 -08001011 if (bitmap) {
mtkleina5114d72015-08-24 13:27:01 -07001012 if (!dump_png(*bitmap, path.c_str(), result.md5.c_str())) {
mtklein748ca3b2015-01-15 10:56:12 -08001013 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
1014 return;
1015 }
1016 } else {
mtkleina5114d72015-08-24 13:27:01 -07001017 SkFILEWStream file(path.c_str());
1018 if (!file.isValid()) {
1019 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
1020 return;
1021 }
mtklein748ca3b2015-01-15 10:56:12 -08001022 if (!file.writeStream(data, len)) {
1023 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
1024 return;
1025 }
1026 }
1027 }
1028};
1029
mtklein748ca3b2015-01-15 10:56:12 -08001030/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1031
1032// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
1033
mtklein21eaf3b2016-02-08 12:39:59 -08001034static SkTDArray<skiatest::Test> gParallelTests, gSerialTests;
mtklein748ca3b2015-01-15 10:56:12 -08001035
1036static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -08001037 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -08001038 return;
1039 }
mtklein6393c062015-04-27 08:45:01 -07001040 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r; r = r->next()) {
1041 if (!in_shard()) {
1042 continue;
1043 }
halcanary87f3ba42015-01-20 09:30:20 -08001044 // Despite its name, factory() is returning a reference to
1045 // link-time static const POD data.
1046 const skiatest::Test& test = r->factory();
1047 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -08001048 continue;
1049 }
halcanary87f3ba42015-01-20 09:30:20 -08001050 if (test.needsGpu && gpu_supported()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001051 (FLAGS_gpu_threading ? gParallelTests : gSerialTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -08001052 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein21eaf3b2016-02-08 12:39:59 -08001053 gParallelTests.push(test);
mtklein82d28432015-01-15 12:46:02 -08001054 }
mtklein748ca3b2015-01-15 10:56:12 -08001055 }
1056}
1057
mtklein21eaf3b2016-02-08 12:39:59 -08001058static void run_test(skiatest::Test test) {
halcanary87f3ba42015-01-20 09:30:20 -08001059 struct : public skiatest::Reporter {
mtklein36352bf2015-03-25 18:17:31 -07001060 void reportFailed(const skiatest::Failure& failure) override {
halcanary87f3ba42015-01-20 09:30:20 -08001061 fail(failure.toString());
1062 JsonWriter::AddTestFailure(failure);
1063 }
mtklein36352bf2015-03-25 18:17:31 -07001064 bool allowExtendedTest() const override {
halcanary87f3ba42015-01-20 09:30:20 -08001065 return FLAGS_pathOpsExtended;
1066 }
mtklein36352bf2015-03-25 18:17:31 -07001067 bool verbose() const override { return FLAGS_veryVerbose; }
halcanary87f3ba42015-01-20 09:30:20 -08001068 } reporter;
djsollen824996a2015-06-12 12:06:22 -07001069
1070 SkString note;
mtklein21eaf3b2016-02-08 12:39:59 -08001071 SkString whyBlacklisted = is_blacklisted("_", "tests", "_", test.name);
djsollen824996a2015-06-12 12:06:22 -07001072 if (!whyBlacklisted.isEmpty()) {
1073 note.appendf(" (--blacklist %s)", whyBlacklisted.c_str());
1074 }
1075
mtkleinf27f08b2015-11-03 06:54:24 -08001076 auto timerStart = now_ms();
djsollen824996a2015-06-12 12:06:22 -07001077 if (!FLAGS_dryRun && whyBlacklisted.isEmpty()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001078 start("unit", "test", "", test.name);
mtklein55e88b22015-01-21 15:50:13 -08001079 GrContextFactory factory;
bsalomon821e10e2015-06-04 14:15:33 -07001080 if (FLAGS_pre_log) {
mtklein21eaf3b2016-02-08 12:39:59 -08001081 SkDebugf("\nRunning test %s", test.name);
bsalomon821e10e2015-06-04 14:15:33 -07001082 }
mtklein21eaf3b2016-02-08 12:39:59 -08001083 test.proc(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -08001084 }
mtklein21eaf3b2016-02-08 12:39:59 -08001085 done(now_ms()-timerStart, "unit", "test", "", test.name, note, "");
mtklein748ca3b2015-01-15 10:56:12 -08001086}
1087
1088/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1089
mtkleinde6fc2b2015-03-12 06:28:54 -07001090// Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung.
1091// This prints something every once in a while so that it knows we're still working.
mtklein2e1c47e2015-03-12 07:16:56 -07001092static void start_keepalive() {
1093 struct Loop {
1094 static void forever(void*) {
1095 for (;;) {
1096 static const int kSec = 300;
1097 #if defined(SK_BUILD_FOR_WIN)
1098 Sleep(kSec * 1000);
1099 #else
1100 sleep(kSec);
1101 #endif
mtkleinb37cb412015-03-18 05:27:14 -07001102 SkString running;
1103 {
mtkleinbc4e0032015-12-09 08:37:35 -08001104 SkAutoMutexAcquire lock(gRunningAndTallyMutex);
mtkleinb37cb412015-03-18 05:27:14 -07001105 for (int i = 0; i < gRunning.count(); i++) {
1106 running.appendf("\n\t%s", gRunning[i].c_str());
1107 }
1108 }
1109 SkDebugf("\nCurrently running:%s\n", running.c_str());
mtklein2e1c47e2015-03-12 07:16:56 -07001110 }
1111 }
1112 };
1113 static SkThread* intentionallyLeaked = new SkThread(Loop::forever);
1114 intentionallyLeaked->start();
mtkleinde6fc2b2015-03-12 06:28:54 -07001115}
1116
caryclark83ca6282015-06-10 09:31:09 -07001117#define PORTABLE_FONT_PREFIX "Toy Liberation "
1118
1119static SkTypeface* create_from_name(const char familyName[], SkTypeface::Style style) {
1120 if (familyName && strlen(familyName) > sizeof(PORTABLE_FONT_PREFIX)
1121 && !strncmp(familyName, PORTABLE_FONT_PREFIX, sizeof(PORTABLE_FONT_PREFIX) - 1)) {
caryclark1818acb2015-07-24 12:09:25 -07001122 return sk_tool_utils::create_portable_typeface(familyName, style);
caryclark83ca6282015-06-10 09:31:09 -07001123 }
halcanary96fcdcc2015-08-27 07:41:13 -07001124 return nullptr;
caryclark83ca6282015-06-10 09:31:09 -07001125}
1126
1127#undef PORTABLE_FONT_PREFIX
1128
1129extern SkTypeface* (*gCreateTypefaceDelegate)(const char [], SkTypeface::Style );
1130
jcgregorio3b27ade2014-11-13 08:06:40 -08001131int dm_main();
caryclark17f0b6d2014-07-22 10:15:34 -07001132int dm_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -08001133 SetupCrashHandler();
mtklein5286f022016-01-22 08:18:14 -08001134 JsonWriter::DumpJson(); // It's handy for the bots to assume this is ~never missing.
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +00001135 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -07001136 SkTaskGroup::Enabler enabled(FLAGS_threads);
caryclark83ca6282015-06-10 09:31:09 -07001137 gCreateTypefaceDelegate = &create_from_name;
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +00001138
mtklein2e1c47e2015-03-12 07:16:56 -07001139 start_keepalive();
mtkleinde6fc2b2015-03-12 06:28:54 -07001140
mtklein62bd1a62015-01-27 14:46:26 -08001141 gather_gold();
borenet09ed4802015-04-03 14:15:33 -07001142 gather_uninteresting_hashes();
mtklein62bd1a62015-01-27 14:46:26 -08001143
scroggo86737142016-02-03 12:19:11 -08001144 if (!gather_srcs()) {
1145 return 1;
1146 }
mtklein748ca3b2015-01-15 10:56:12 -08001147 gather_sinks();
1148 gather_tests();
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +00001149
mtklein21eaf3b2016-02-08 12:39:59 -08001150 gPending = gSrcs.count() * gSinks.count() + gParallelTests.count() + gSerialTests.count();
mtklein748ca3b2015-01-15 10:56:12 -08001151 SkDebugf("%d srcs * %d sinks + %d tests == %d tasks\n",
mtklein21eaf3b2016-02-08 12:39:59 -08001152 gSrcs.count(), gSinks.count(), gParallelTests.count() + gSerialTests.count(), gPending);
mtklein748ca3b2015-01-15 10:56:12 -08001153
mtklein21eaf3b2016-02-08 12:39:59 -08001154 // Kick off as much parallel work as we can, making note of any serial work we'll need to do.
1155 SkTaskGroup parallel;
1156 SkTArray<Task> serial;
1157
1158 for (auto& sink : gSinks)
1159 for (auto& src : gSrcs) {
1160 Task task(src, sink);
1161 if (src->serial() || sink->serial()) {
1162 serial.push_back(task);
1163 } else {
1164 parallel.add([task] { Task::Run(task); });
mtklein748ca3b2015-01-15 10:56:12 -08001165 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001166 }
mtklein21eaf3b2016-02-08 12:39:59 -08001167 for (auto test : gParallelTests) {
1168 parallel.add([test] { run_test(test); });
mtklein55e88b22015-01-21 15:50:13 -08001169 }
mtklein21eaf3b2016-02-08 12:39:59 -08001170
1171 // With the parallel work running, run serial tasks and tests here on main thread.
1172 for (auto task : serial) { Task::Run(task); }
1173 for (auto test : gSerialTests) { run_test(test); }
1174
1175 // Wait for any remaining parallel work to complete (including any spun off of serial tasks).
1176 parallel.wait();
mtkleincd50bca2016-01-05 06:20:20 -08001177 gDefinitelyThreadSafeWork.wait();
1178
mtklein748ca3b2015-01-15 10:56:12 -08001179 // At this point we're back in single-threaded land.
caryclarkf53ce802015-06-15 06:48:30 -07001180 sk_tool_utils::release_portable_typefaces();
mtklein@google.comd36522d2013-10-16 13:02:15 +00001181
mtkleinbc4e0032015-12-09 08:37:35 -08001182 if (FLAGS_verbose && gNoteTally.count() > 0) {
1183 SkDebugf("\nNote tally:\n");
1184 gNoteTally.foreach([](const SkString& note, int* tally) {
1185 SkDebugf("%dx\t%s\n", *tally, note.c_str());
1186 });
1187 }
1188
mtklein2f64eec2015-01-15 14:20:41 -08001189 SkDebugf("\n");
mtklein748ca3b2015-01-15 10:56:12 -08001190 if (gFailures.count() > 0) {
1191 SkDebugf("Failures:\n");
1192 for (int i = 0; i < gFailures.count(); i++) {
mtklein9dc09102015-01-15 15:47:33 -08001193 SkDebugf("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001194 }
1195 SkDebugf("%d failures\n", gFailures.count());
1196 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001197 }
mtklein748ca3b2015-01-15 10:56:12 -08001198 if (gPending > 0) {
1199 SkDebugf("Hrm, we didn't seem to run everything we intended to! Please file a bug.\n");
1200 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001201 }
halcanary7a14b312015-10-01 07:28:13 -07001202 #ifdef SK_PDF_IMAGE_STATS
1203 SkPDFImageDumpStats();
1204 #endif // SK_PDF_IMAGE_STATS
scroggo4d9eaea2016-01-29 07:48:33 -08001205 SkDebugf("Finished!\n");
mtklein748ca3b2015-01-15 10:56:12 -08001206 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +00001207}
jcgregorio3b27ade2014-11-13 08:06:40 -08001208
kkinnunen179a8f52015-11-20 13:32:24 -08001209// TODO: currently many GPU tests are declared outside SK_SUPPORT_GPU guards.
1210// Thus we export the empty RunWithGPUTestContexts when SK_SUPPORT_GPU=0.
1211namespace skiatest {
1212namespace {
1213typedef void(*TestWithGrContext)(skiatest::Reporter*, GrContext*);
1214typedef void(*TestWithGrContextAndGLContext)(skiatest::Reporter*, GrContext*, SkGLContext*);
1215#if SK_SUPPORT_GPU
1216template<typename T>
kkinnunen34058002016-01-06 23:49:30 -08001217void call_test(T test, skiatest::Reporter* reporter, const GrContextFactory::ContextInfo& context);
kkinnunen179a8f52015-11-20 13:32:24 -08001218template<>
1219void call_test(TestWithGrContext test, skiatest::Reporter* reporter,
kkinnunen34058002016-01-06 23:49:30 -08001220 const GrContextFactory::ContextInfo& context) {
1221 test(reporter, context.fGrContext);
kkinnunen179a8f52015-11-20 13:32:24 -08001222}
1223template<>
1224void call_test(TestWithGrContextAndGLContext test, skiatest::Reporter* reporter,
kkinnunen34058002016-01-06 23:49:30 -08001225 const GrContextFactory::ContextInfo& context) {
1226 test(reporter, context.fGrContext, context.fGLContext);
kkinnunen179a8f52015-11-20 13:32:24 -08001227}
1228#endif
1229} // namespace
1230
kkinnunen179a8f52015-11-20 13:32:24 -08001231template<typename T>
1232void RunWithGPUTestContexts(T test, GPUTestContexts testContexts, Reporter* reporter,
1233 GrContextFactory* factory) {
1234#if SK_SUPPORT_GPU
kkinnunen3e980c32015-12-23 01:33:00 -08001235 // Iterate over context types, except use "native" instead of explicitly trying OpenGL and
1236 // OpenGL ES. Do not use GLES on desktop, since tests do not account for not fixing
1237 // http://skbug.com/2809
1238 GrContextFactory::GLContextType contextTypes[] = {
1239 GrContextFactory::kNative_GLContextType,
1240#if SK_ANGLE
1241#ifdef SK_BUILD_FOR_WIN
1242 GrContextFactory::kANGLE_GLContextType,
1243#endif
1244 GrContextFactory::kANGLE_GL_GLContextType,
1245#endif
1246#if SK_COMMAND_BUFFER
1247 GrContextFactory::kCommandBuffer_GLContextType,
1248#endif
1249#if SK_MESA
1250 GrContextFactory::kMESA_GLContextType,
1251#endif
1252 GrContextFactory::kNull_GLContextType,
1253 GrContextFactory::kDebug_GLContextType,
1254 };
1255 static_assert(SK_ARRAY_COUNT(contextTypes) == GrContextFactory::kGLContextTypeCnt - 2,
1256 "Skipping unexpected GLContextType for GPU tests");
1257
1258 for (auto& contextType : contextTypes) {
kkinnunen179a8f52015-11-20 13:32:24 -08001259 int contextSelector = kNone_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001260 if (GrContextFactory::IsRenderingGLContext(contextType)) {
kkinnunen179a8f52015-11-20 13:32:24 -08001261 contextSelector |= kAllRendering_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001262 } else if (contextType == GrContextFactory::kNative_GLContextType) {
kkinnunen179a8f52015-11-20 13:32:24 -08001263 contextSelector |= kNative_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001264 } else if (contextType == GrContextFactory::kNull_GLContextType) {
kkinnunen179a8f52015-11-20 13:32:24 -08001265 contextSelector |= kNull_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001266 } else if (contextType == GrContextFactory::kDebug_GLContextType) {
kkinnunen55eeae92015-12-10 23:19:29 -08001267 contextSelector |= kDebug_GPUTestContexts;
kkinnunen179a8f52015-11-20 13:32:24 -08001268 }
1269 if ((testContexts & contextSelector) == 0) {
1270 continue;
1271 }
kkinnunen34058002016-01-06 23:49:30 -08001272 GrContextFactory::ContextInfo context = factory->getContextInfo(contextType);
1273 if (context.fGrContext) {
kkinnunen5219fd92015-12-10 06:28:13 -08001274 call_test(test, reporter, context);
1275 }
kkinnunen34058002016-01-06 23:49:30 -08001276 context = factory->getContextInfo(contextType,
1277 GrContextFactory::kEnableNVPR_GLContextOptions);
1278 if (context.fGrContext) {
kkinnunen179a8f52015-11-20 13:32:24 -08001279 call_test(test, reporter, context);
1280 }
1281 }
1282#endif
1283}
1284
1285template
1286void RunWithGPUTestContexts<TestWithGrContext>(TestWithGrContext test,
1287 GPUTestContexts testContexts,
1288 Reporter* reporter,
1289 GrContextFactory* factory);
1290template
1291void RunWithGPUTestContexts<TestWithGrContextAndGLContext>(TestWithGrContextAndGLContext test,
1292 GPUTestContexts testContexts,
1293 Reporter* reporter,
1294 GrContextFactory* factory);
1295} // namespace skiatest
1296
borenet48087572015-04-02 12:16:36 -07001297#if !defined(SK_BUILD_FOR_IOS)
jcgregorio3b27ade2014-11-13 08:06:40 -08001298int main(int argc, char** argv) {
1299 SkCommandLineFlags::Parse(argc, argv);
1300 return dm_main();
1301}
1302#endif