blob: c87f578babf22df5bf86aec95ace2b86d27ca205 [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;
245 case CodecSrc::kSubset_Mode:
msarett36c37962015-09-02 13:20:52 -0700246 folder.append("codec_subset");
msarett9e707a02015-09-01 14:57:57 -0700247 break;
msarettb714fb02016-01-22 14:46:42 -0800248 case CodecSrc::kGen_Mode:
249 folder.append("gen");
250 break;
msarett9e707a02015-09-01 14:57:57 -0700251 }
252
253 switch (dstColorType) {
254 case CodecSrc::kGrayscale_Always_DstColorType:
255 folder.append("_kGray8");
256 break;
257 case CodecSrc::kIndex8_Always_DstColorType:
258 folder.append("_kIndex8");
259 break;
260 default:
261 break;
262 }
263
scroggoc5560be2016-02-03 09:42:42 -0800264 switch (dstAlphaType) {
265 case kOpaque_SkAlphaType:
266 folder.append("_opaque");
267 break;
268 case kPremul_SkAlphaType:
269 folder.append("_premul");
270 break;
271 case kUnpremul_SkAlphaType:
272 folder.append("_unpremul");
273 break;
274 default:
275 break;
276 }
277
msarett9e707a02015-09-01 14:57:57 -0700278 if (1.0f != scale) {
279 folder.appendf("_%.3f", scale);
280 }
281
scroggoc5560be2016-02-03 09:42:42 -0800282 CodecSrc* src = new CodecSrc(path, mode, dstColorType, dstAlphaType, scale);
msarett9e707a02015-09-01 14:57:57 -0700283 push_src("image", folder, src);
284}
285
msarett3d9d7a72015-10-21 10:27:10 -0700286static void push_android_codec_src(Path path, AndroidCodecSrc::Mode mode,
scroggoc5560be2016-02-03 09:42:42 -0800287 CodecSrc::DstColorType dstColorType, SkAlphaType dstAlphaType, int sampleSize) {
msarett3d9d7a72015-10-21 10:27:10 -0700288 SkString folder;
289 switch (mode) {
290 case AndroidCodecSrc::kFullImage_Mode:
291 folder.append("scaled_codec");
292 break;
293 case AndroidCodecSrc::kDivisor_Mode:
294 folder.append("scaled_codec_divisor");
295 break;
296 }
297
298 switch (dstColorType) {
299 case CodecSrc::kGrayscale_Always_DstColorType:
300 folder.append("_kGray8");
301 break;
302 case CodecSrc::kIndex8_Always_DstColorType:
303 folder.append("_kIndex8");
304 break;
305 default:
306 break;
307 }
308
scroggoc5560be2016-02-03 09:42:42 -0800309 switch (dstAlphaType) {
310 case kOpaque_SkAlphaType:
311 folder.append("_opaque");
312 break;
313 case kPremul_SkAlphaType:
314 folder.append("_premul");
315 break;
msarett9e9444c2016-02-03 12:39:10 -0800316 case kUnpremul_SkAlphaType:
317 folder.append("_unpremul");
318 break;
scroggoc5560be2016-02-03 09:42:42 -0800319 default:
320 break;
321 }
322
msarett3d9d7a72015-10-21 10:27:10 -0700323 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800324 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700325 }
326
scroggoc5560be2016-02-03 09:42:42 -0800327 AndroidCodecSrc* src = new AndroidCodecSrc(path, mode, dstColorType, dstAlphaType, sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700328 push_src("image", folder, src);
329}
330
msarett438b2ad2015-04-09 12:43:10 -0700331static void push_codec_srcs(Path path) {
332 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
333 if (!encoded) {
334 SkDebugf("Couldn't read %s.", path.c_str());
335 return;
336 }
337 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
halcanary96fcdcc2015-08-27 07:41:13 -0700338 if (nullptr == codec.get()) {
msarett438b2ad2015-04-09 12:43:10 -0700339 SkDebugf("Couldn't create codec for %s.", path.c_str());
340 return;
341 }
342
msarett36c37962015-09-02 13:20:52 -0700343 // Native Scales
msarett0a242972015-06-11 14:27:27 -0700344 // TODO (msarett): Implement scaling tests for SkImageDecoder in order to compare with these
345 // tests. SkImageDecoder supports downscales by integer factors.
emmaleer8f4ba762015-08-14 07:44:46 -0700346 // 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 -0700347 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 -0700348
msarettbb25b532016-01-13 09:31:39 -0800349 const CodecSrc::Mode nativeModes[] = { CodecSrc::kCodec_Mode, CodecSrc::kCodecZeroInit_Mode,
msarettb714fb02016-01-22 14:46:42 -0800350 CodecSrc::kScanline_Mode, CodecSrc::kStripe_Mode, CodecSrc::kSubset_Mode,
351 CodecSrc::kGen_Mode };
msarett9e707a02015-09-01 14:57:57 -0700352
msarett36c37962015-09-02 13:20:52 -0700353 CodecSrc::DstColorType colorTypes[3];
354 uint32_t numColorTypes;
355 switch (codec->getInfo().colorType()) {
356 case kGray_8_SkColorType:
357 // FIXME: Is this a long term solution for testing wbmps decodes to kIndex8?
halcanary6950de62015-11-07 05:29:00 -0800358 // Further discussion on this topic is at https://bug.skia.org/3683 .
msarett3d9d7a72015-10-21 10:27:10 -0700359 // This causes us to try to convert grayscale jpegs to kIndex8. We currently
360 // fail non-fatally in this case.
msarett36c37962015-09-02 13:20:52 -0700361 colorTypes[0] = CodecSrc::kGetFromCanvas_DstColorType;
362 colorTypes[1] = CodecSrc::kGrayscale_Always_DstColorType;
363 colorTypes[2] = CodecSrc::kIndex8_Always_DstColorType;
364 numColorTypes = 3;
365 break;
366 case kIndex_8_SkColorType:
367 colorTypes[0] = CodecSrc::kGetFromCanvas_DstColorType;
368 colorTypes[1] = CodecSrc::kIndex8_Always_DstColorType;
369 numColorTypes = 2;
370 break;
371 default:
372 colorTypes[0] = CodecSrc::kGetFromCanvas_DstColorType;
373 numColorTypes = 1;
374 break;
375 }
msarett9e707a02015-09-01 14:57:57 -0700376
scroggoc5560be2016-02-03 09:42:42 -0800377 SkTArray<SkAlphaType> alphaModes;
378 alphaModes.push_back(kPremul_SkAlphaType);
msarett9e9444c2016-02-03 12:39:10 -0800379 alphaModes.push_back(kUnpremul_SkAlphaType);
scroggoc5560be2016-02-03 09:42:42 -0800380 if (codec->getInfo().alphaType() == kOpaque_SkAlphaType) {
381 alphaModes.push_back(kOpaque_SkAlphaType);
382 }
msarettb714fb02016-01-22 14:46:42 -0800383
384 for (CodecSrc::Mode mode : nativeModes) {
385 // SkCodecImageGenerator only runs for the default colorType
386 // recommended by SkCodec. There is no need to generate multiple
387 // tests for different colorTypes.
388 // TODO (msarett): Add scaling support to SkCodecImageGenerator.
389 if (CodecSrc::kGen_Mode == mode) {
390 // FIXME: The gpu backend does not draw kGray sources correctly. (skbug.com/4822)
391 if (kGray_8_SkColorType != codec->getInfo().colorType()) {
scroggoc5560be2016-02-03 09:42:42 -0800392 push_codec_src(path, mode, CodecSrc::kGetFromCanvas_DstColorType,
393 codec->getInfo().alphaType(), 1.0f);
msarettb714fb02016-01-22 14:46:42 -0800394 }
395 continue;
396 }
397
398 for (float scale : nativeScales) {
msarett36c37962015-09-02 13:20:52 -0700399 for (uint32_t i = 0; i < numColorTypes; i++) {
scroggoc5560be2016-02-03 09:42:42 -0800400 for (SkAlphaType alphaType : alphaModes) {
401 push_codec_src(path, mode, colorTypes[i], alphaType, scale);
402 }
msarett9e707a02015-09-01 14:57:57 -0700403 }
404 }
msarett0a242972015-06-11 14:27:27 -0700405 }
msarett36c37962015-09-02 13:20:52 -0700406
halcanary6950de62015-11-07 05:29:00 -0800407 // https://bug.skia.org/4428
msarett33c76232015-11-16 13:43:40 -0800408 bool subset = false;
409 // The following image types are supported by BitmapRegionDecoder,
410 // so we will test full image decodes and subset decodes.
msarettbe8216a2015-12-04 08:00:50 -0800411 static const char* const exts[] = {
msarett3d9d7a72015-10-21 10:27:10 -0700412 "jpg", "jpeg", "png", "webp",
413 "JPG", "JPEG", "PNG", "WEBP",
414 };
msarettbe8216a2015-12-04 08:00:50 -0800415 for (const char* ext : exts) {
msarett3d9d7a72015-10-21 10:27:10 -0700416 if (path.endsWith(ext)) {
msarett33c76232015-11-16 13:43:40 -0800417 subset = true;
msarett3d9d7a72015-10-21 10:27:10 -0700418 break;
419 }
420 }
msarett33c76232015-11-16 13:43:40 -0800421
msarett3d9d7a72015-10-21 10:27:10 -0700422 const int sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
msarett36c37962015-09-02 13:20:52 -0700423
msarett3d9d7a72015-10-21 10:27:10 -0700424 for (int sampleSize : sampleSizes) {
msarett33c76232015-11-16 13:43:40 -0800425 for (uint32_t i = 0; i < numColorTypes; i++) {
scroggoc5560be2016-02-03 09:42:42 -0800426 for (SkAlphaType alphaType : alphaModes) {
427 push_android_codec_src(path, AndroidCodecSrc::kFullImage_Mode, colorTypes[i],
428 alphaType, sampleSize);
429 if (subset) {
430 push_android_codec_src(path, AndroidCodecSrc::kDivisor_Mode, colorTypes[i],
431 alphaType, sampleSize);
432 }
msarett3d9d7a72015-10-21 10:27:10 -0700433 }
msarett36c37962015-09-02 13:20:52 -0700434 }
435 }
msarett438b2ad2015-04-09 12:43:10 -0700436}
437
msarett5cb48852015-11-06 08:56:32 -0800438static bool brd_color_type_supported(SkBitmapRegionDecoder::Strategy strategy,
msaretta5783ae2015-09-08 15:35:32 -0700439 CodecSrc::DstColorType dstColorType) {
440 switch (strategy) {
msarett5cb48852015-11-06 08:56:32 -0800441 case SkBitmapRegionDecoder::kCanvas_Strategy:
msaretta5783ae2015-09-08 15:35:32 -0700442 if (CodecSrc::kGetFromCanvas_DstColorType == dstColorType) {
443 return true;
444 }
445 return false;
msarett5cb48852015-11-06 08:56:32 -0800446 case SkBitmapRegionDecoder::kAndroidCodec_Strategy:
msarett26ad17b2015-10-22 07:29:19 -0700447 switch (dstColorType) {
448 case CodecSrc::kGetFromCanvas_DstColorType:
449 case CodecSrc::kIndex8_Always_DstColorType:
450 case CodecSrc::kGrayscale_Always_DstColorType:
451 return true;
452 default:
453 return false;
454 }
msaretta5783ae2015-09-08 15:35:32 -0700455 default:
456 SkASSERT(false);
457 return false;
458 }
459}
460
msarett5cb48852015-11-06 08:56:32 -0800461static void push_brd_src(Path path, SkBitmapRegionDecoder::Strategy strategy,
msaretta5783ae2015-09-08 15:35:32 -0700462 CodecSrc::DstColorType dstColorType, BRDSrc::Mode mode, uint32_t sampleSize) {
463 SkString folder;
464 switch (strategy) {
msarett5cb48852015-11-06 08:56:32 -0800465 case SkBitmapRegionDecoder::kCanvas_Strategy:
msaretta5783ae2015-09-08 15:35:32 -0700466 folder.append("brd_canvas");
467 break;
msarett5cb48852015-11-06 08:56:32 -0800468 case SkBitmapRegionDecoder::kAndroidCodec_Strategy:
msarett26ad17b2015-10-22 07:29:19 -0700469 folder.append("brd_android_codec");
470 break;
msaretta5783ae2015-09-08 15:35:32 -0700471 default:
472 SkASSERT(false);
473 return;
474 }
475
476 switch (mode) {
477 case BRDSrc::kFullImage_Mode:
478 break;
479 case BRDSrc::kDivisor_Mode:
480 folder.append("_divisor");
481 break;
482 default:
483 SkASSERT(false);
484 return;
485 }
486
487 switch (dstColorType) {
488 case CodecSrc::kGetFromCanvas_DstColorType:
489 break;
490 case CodecSrc::kIndex8_Always_DstColorType:
491 folder.append("_kIndex");
492 break;
493 case CodecSrc::kGrayscale_Always_DstColorType:
494 folder.append("_kGray");
495 break;
496 default:
497 SkASSERT(false);
498 return;
499 }
500
501 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800502 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700503 }
504
505 BRDSrc* src = new BRDSrc(path, strategy, mode, dstColorType, sampleSize);
506 push_src("image", folder, src);
507}
508
509static void push_brd_srcs(Path path) {
510
msarett5cb48852015-11-06 08:56:32 -0800511 const SkBitmapRegionDecoder::Strategy strategies[] = {
512 SkBitmapRegionDecoder::kCanvas_Strategy,
msarett5cb48852015-11-06 08:56:32 -0800513 SkBitmapRegionDecoder::kAndroidCodec_Strategy,
msaretta5783ae2015-09-08 15:35:32 -0700514 };
515
scroggo501b7342015-11-03 07:55:11 -0800516 // Test on a variety of sampleSizes, making sure to include:
517 // - 2, 4, and 8, which are natively supported by jpeg
518 // - multiples of 2 which are not divisible by 4 (analogous for 4)
519 // - larger powers of two, since BRD clients generally use powers of 2
520 // We will only produce output for the larger sizes on large images.
521 const uint32_t sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 24, 32, 64 };
msarett6efbe052015-09-11 09:01:16 -0700522
msaretta5783ae2015-09-08 15:35:32 -0700523 // We will only test to one backend (8888), but we will test all of the
524 // color types that we need to decode to on this backend.
525 const CodecSrc::DstColorType dstColorTypes[] = {
msarett26ad17b2015-10-22 07:29:19 -0700526 CodecSrc::kGetFromCanvas_DstColorType,
527 CodecSrc::kIndex8_Always_DstColorType,
528 CodecSrc::kGrayscale_Always_DstColorType,
msaretta5783ae2015-09-08 15:35:32 -0700529 };
530
531 const BRDSrc::Mode modes[] = {
msarett26ad17b2015-10-22 07:29:19 -0700532 BRDSrc::kFullImage_Mode,
533 BRDSrc::kDivisor_Mode,
msaretta5783ae2015-09-08 15:35:32 -0700534 };
535
msarett5cb48852015-11-06 08:56:32 -0800536 for (SkBitmapRegionDecoder::Strategy strategy : strategies) {
msarett6efbe052015-09-11 09:01:16 -0700537 for (uint32_t sampleSize : sampleSizes) {
msarett6efbe052015-09-11 09:01:16 -0700538 for (CodecSrc::DstColorType dstColorType : dstColorTypes) {
539 if (brd_color_type_supported(strategy, dstColorType)) {
540 for (BRDSrc::Mode mode : modes) {
msaretta5783ae2015-09-08 15:35:32 -0700541 push_brd_src(path, strategy, dstColorType, mode, sampleSize);
542 }
543 }
544 }
545 }
546 }
547}
548
549static bool brd_supported(const char* ext) {
msarett8c8f22a2015-04-01 06:58:48 -0700550 static const char* const exts[] = {
msaretta5783ae2015-09-08 15:35:32 -0700551 "jpg", "jpeg", "png", "webp",
552 "JPG", "JPEG", "PNG", "WEBP",
msarett8c8f22a2015-04-01 06:58:48 -0700553 };
554
555 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
556 if (0 == strcmp(exts[i], ext)) {
557 return true;
558 }
559 }
560 return false;
scroggo9b77ddd2015-03-19 06:03:39 -0700561}
562
scroggo86737142016-02-03 12:19:11 -0800563static bool gather_srcs() {
mtklein748ca3b2015-01-15 10:56:12 -0800564 for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) {
djsollen54416de2015-04-03 07:24:48 -0700565 push_src("gm", "", new GMSrc(r->factory()));
mtklein748ca3b2015-01-15 10:56:12 -0800566 }
halcanaryfc37ad12015-01-30 07:31:19 -0800567 for (int i = 0; i < FLAGS_skps.count(); i++) {
568 const char* path = FLAGS_skps[i];
569 if (sk_isdir(path)) {
570 SkOSFile::Iter it(path, "skp");
571 for (SkString file; it.next(&file); ) {
djsollen54416de2015-04-03 07:24:48 -0700572 push_src("skp", "", new SKPSrc(SkOSPath::Join(path, file.c_str())));
halcanaryfc37ad12015-01-30 07:31:19 -0800573 }
574 } else {
djsollen54416de2015-04-03 07:24:48 -0700575 push_src("skp", "", new SKPSrc(path));
mtklein114c3cd2015-01-15 10:15:02 -0800576 }
577 }
scroggo86737142016-02-03 12:19:11 -0800578
579 SkTArray<SkString> images;
580 if (!CollectImages(&images)) {
581 return false;
582 }
583
584 for (auto image : images) {
585 push_codec_srcs(image);
mtklein21eaf3b2016-02-08 12:39:59 -0800586 const char* ext = strrchr(image.c_str(), '.');
587 if (ext && brd_supported(ext+1)) {
scroggo86737142016-02-03 12:19:11 -0800588 push_brd_srcs(image);
mtklein709d2c32015-01-15 08:30:25 -0800589 }
mtklein709d2c32015-01-15 08:30:25 -0800590 }
scroggo86737142016-02-03 12:19:11 -0800591
592 return true;
mtklein709d2c32015-01-15 08:30:25 -0800593}
594
kkinnunen3e980c32015-12-23 01:33:00 -0800595static void push_sink(const SkCommandLineConfig& config, Sink* s) {
rmistry0f515bd2015-12-22 10:22:26 -0800596 SkAutoTDelete<Sink> sink(s);
kkinnunen3e980c32015-12-23 01:33:00 -0800597
mtkleine0effd62015-07-29 06:37:28 -0700598 // Try a simple Src as a canary. If it fails, skip this sink.
mtklein748ca3b2015-01-15 10:56:12 -0800599 struct : public Src {
mtkleine0effd62015-07-29 06:37:28 -0700600 Error draw(SkCanvas* c) const override {
601 c->drawRect(SkRect::MakeWH(1,1), SkPaint());
602 return "";
603 }
mtklein36352bf2015-03-25 18:17:31 -0700604 SkISize size() const override { return SkISize::Make(16, 16); }
mtkleine0effd62015-07-29 06:37:28 -0700605 Name name() const override { return "justOneRect"; }
606 } justOneRect;
mtklein114c3cd2015-01-15 10:15:02 -0800607
mtklein748ca3b2015-01-15 10:56:12 -0800608 SkBitmap bitmap;
609 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800610 SkString log;
mtkleine0effd62015-07-29 06:37:28 -0700611 Error err = sink->draw(justOneRect, &bitmap, &stream, &log);
mtklein4089ef72015-03-05 08:40:28 -0800612 if (err.isFatal()) {
kkinnunen3e980c32015-12-23 01:33:00 -0800613 SkDebugf("Could not run %s: %s\n", config.getTag().c_str(), err.c_str());
mtklein05641a52015-04-21 10:49:13 -0700614 exit(1);
mtklein114c3cd2015-01-15 10:15:02 -0800615 }
616
mtkleine0effd62015-07-29 06:37:28 -0700617 TaggedSink& ts = gSinks.push_back();
mtklein748ca3b2015-01-15 10:56:12 -0800618 ts.reset(sink.detach());
kkinnunen3e980c32015-12-23 01:33:00 -0800619 ts.tag = config.getTag();
mtklein748ca3b2015-01-15 10:56:12 -0800620}
621
622static bool gpu_supported() {
623#if SK_SUPPORT_GPU
624 return FLAGS_gpu;
625#else
626 return false;
627#endif
628}
kkinnunen9ebc3f02015-12-21 23:48:13 -0800629
kkinnunen3e980c32015-12-23 01:33:00 -0800630static Sink* create_sink(const SkCommandLineConfig* config) {
631#if SK_SUPPORT_GPU
632 if (gpu_supported()) {
633 if (const SkCommandLineConfigGpu* gpuConfig = config->asConfigGpu()) {
634 GrContextFactory::GLContextType contextType = gpuConfig->getContextType();
635 GrContextFactory::GLContextOptions contextOptions =
636 GrContextFactory::kNone_GLContextOptions;
637 if (gpuConfig->getUseNVPR()) {
638 contextOptions = static_cast<GrContextFactory::GLContextOptions>(
639 contextOptions | GrContextFactory::kEnableNVPR_GLContextOptions);
640 }
641 GrContextFactory testFactory;
642 if (!testFactory.get(contextType, contextOptions)) {
643 SkDebugf("WARNING: can not create GPU context for config '%s'. "
644 "GM tests will be skipped.\n", gpuConfig->getTag().c_str());
645 return nullptr;
646 }
647 return new GPUSink(contextType, contextOptions, gpuConfig->getSamples(),
648 gpuConfig->getUseDIText(), FLAGS_gpu_threading);
649 }
650 }
651#endif
652
653#define SINK(t, sink, ...) if (config->getBackend().equals(t)) { return new sink(__VA_ARGS__); }
654
tomhudsoneebc39a2015-02-23 12:18:05 -0800655#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
656 SINK("hwui", HWUISink);
657#endif
658
mtklein748ca3b2015-01-15 10:56:12 -0800659 if (FLAGS_cpu) {
660 SINK("565", RasterSink, kRGB_565_SkColorType);
661 SINK("8888", RasterSink, kN32_SkColorType);
halcanaryc11c62f2015-09-28 11:51:54 -0700662 SINK("pdf", PDFSink, "Pdfium");
663 SINK("pdf_poppler", PDFSink, "Poppler");
mtklein9c3f17d2015-01-28 11:35:18 -0800664 SINK("skp", SKPSink);
mtklein8a4527e2015-01-31 20:00:58 -0800665 SINK("svg", SVGSink);
666 SINK("null", NullSink);
halcanary47ef4d52015-03-03 09:13:09 -0800667 SINK("xps", XPSSink);
mtklein748ca3b2015-01-15 10:56:12 -0800668 }
669#undef SINK
halcanary96fcdcc2015-08-27 07:41:13 -0700670 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800671}
672
kkinnunen3e980c32015-12-23 01:33:00 -0800673static Sink* create_via(const SkString& tag, Sink* wrapped) {
674#define VIA(t, via, ...) if (tag.equals(t)) { return new via(__VA_ARGS__); }
halcanary96fcdcc2015-08-27 07:41:13 -0700675 VIA("twice", ViaTwice, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700676 VIA("serialize", ViaSerialization, wrapped);
mtklein4a34ecb2016-01-08 10:19:35 -0800677 VIA("pic", ViaPicture, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700678 VIA("2ndpic", ViaSecondPicture, wrapped);
mtkleind31c13d2015-05-05 12:59:56 -0700679 VIA("sp", ViaSingletonPictures, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700680 VIA("tiles", ViaTiles, 256, 256, nullptr, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800681 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
mtklein2e2ea382015-10-16 10:29:41 -0700682 VIA("remote", ViaRemote, false, wrapped);
683 VIA("remote_cache", ViaRemote, true, wrapped);
halcanary7a76f9c2016-02-03 11:53:18 -0800684 VIA("mojo", ViaMojo, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800685
mtkleind603b222015-02-17 11:13:33 -0800686 if (FLAGS_matrix.count() == 4) {
mtklein748ca3b2015-01-15 10:56:12 -0800687 SkMatrix m;
mtkleind603b222015-02-17 11:13:33 -0800688 m.reset();
689 m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
690 m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
691 m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
692 m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
693 VIA("matrix", ViaMatrix, m, wrapped);
694 VIA("upright", ViaUpright, m, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800695 }
tomhudson64de1e12015-03-05 08:01:07 -0800696
697#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
698 VIA("androidsdk", ViaAndroidSDK, wrapped);
699#endif
700
mtklein748ca3b2015-01-15 10:56:12 -0800701#undef VIA
halcanary96fcdcc2015-08-27 07:41:13 -0700702 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800703}
704
mtklein748ca3b2015-01-15 10:56:12 -0800705static void gather_sinks() {
kkinnunen3e980c32015-12-23 01:33:00 -0800706 SkCommandLineConfigArray configs;
707 ParseConfigs(FLAGS_config, &configs);
708 for (int i = 0; i < configs.count(); i++) {
709 const SkCommandLineConfig& config = *configs[i];
710 Sink* sink = create_sink(&config);
711 if (sink == nullptr) {
712 SkDebugf("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
713 config.getTag().c_str());
714 continue;
715 }
mtklein748ca3b2015-01-15 10:56:12 -0800716
kkinnunen3e980c32015-12-23 01:33:00 -0800717 const SkTArray<SkString>& parts = config.getViaParts();
718 for (int j = parts.count(); j-- > 0;) {
719 const SkString& part = parts[j];
720 Sink* next = create_via(part, sink);
halcanary96fcdcc2015-08-27 07:41:13 -0700721 if (next == nullptr) {
kkinnunen3e980c32015-12-23 01:33:00 -0800722 SkDebugf("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
723 part.c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800724 delete sink;
halcanary96fcdcc2015-08-27 07:41:13 -0700725 sink = nullptr;
mtklein748ca3b2015-01-15 10:56:12 -0800726 break;
727 }
728 sink = next;
729 }
730 if (sink) {
731 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800732 }
733 }
734}
mtklein709d2c32015-01-15 08:30:25 -0800735
mtkleina5114d72015-08-24 13:27:01 -0700736static bool dump_png(SkBitmap bitmap, const char* path, const char* md5) {
737 const int w = bitmap.width(),
738 h = bitmap.height();
739
740 // First get the bitmap into N32 color format. The next step will work only there.
741 if (bitmap.colorType() != kN32_SkColorType) {
742 SkBitmap n32;
743 if (!bitmap.copyTo(&n32, kN32_SkColorType)) {
744 return false;
745 }
746 bitmap = n32;
747 }
748
749 // Convert our N32 bitmap into unpremul RGBA for libpng.
750 SkAutoTMalloc<uint32_t> rgba(w*h);
751 if (!bitmap.readPixels(SkImageInfo::Make(w,h, kRGBA_8888_SkColorType, kUnpremul_SkAlphaType),
752 rgba, 4*w, 0,0)) {
753 return false;
754 }
755
756 // We don't need bitmap anymore. Might as well drop our ref.
mtkleinfccb77d2015-08-24 14:13:29 -0700757 bitmap.reset();
mtkleina5114d72015-08-24 13:27:01 -0700758
mtkleinc64137c2015-08-25 10:56:08 -0700759 FILE* f = fopen(path, "wb");
mtkleina5114d72015-08-24 13:27:01 -0700760 if (!f) { return false; }
761
762 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
763 if (!png) {
764 fclose(f);
765 return false;
766 }
767
768 png_infop info = png_create_info_struct(png);
769 if (!info) {
770 png_destroy_write_struct(&png, &info);
771 fclose(f);
772 return false;
773 }
774
mtkleind69ece62015-09-10 10:37:44 -0700775 SkString description;
776 description.append("Key: ");
777 for (int i = 0; i < FLAGS_key.count(); i++) {
778 description.appendf("%s ", FLAGS_key[i]);
779 }
780 description.append("Properties: ");
781 for (int i = 0; i < FLAGS_properties.count(); i++) {
782 description.appendf("%s ", FLAGS_properties[i]);
783 }
784 description.appendf("MD5: %s", md5);
785
mtkleina5114d72015-08-24 13:27:01 -0700786 png_text text[2];
787 text[0].key = (png_charp)"Author";
788 text[0].text = (png_charp)"DM dump_png()";
789 text[0].compression = PNG_TEXT_COMPRESSION_NONE;
790 text[1].key = (png_charp)"Description";
mtkleind69ece62015-09-10 10:37:44 -0700791 text[1].text = (png_charp)description.c_str();
mtkleina5114d72015-08-24 13:27:01 -0700792 text[1].compression = PNG_TEXT_COMPRESSION_NONE;
793 png_set_text(png, info, text, 2);
794
795 png_init_io(png, f);
796 png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8,
797 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
798 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
799 png_write_info(png, info);
800 for (int j = 0; j < h; j++) {
801 png_bytep row = (png_bytep)(rgba.get() + w*j);
802 png_write_rows(png, &row, 1);
803 }
804 png_write_end(png, info);
805
806 png_destroy_write_struct(&png, &info);
807 fclose(f);
808 return true;
809}
810
mtkleina2ef6422015-01-15 13:44:22 -0800811static bool match(const char* needle, const char* haystack) {
halcanary96fcdcc2015-08-27 07:41:13 -0700812 return 0 == strcmp("_", needle) || nullptr != strstr(haystack, needle);
mtkleina2ef6422015-01-15 13:44:22 -0800813}
814
djsollen54416de2015-04-03 07:24:48 -0700815static ImplicitString is_blacklisted(const char* sink, const char* src,
816 const char* srcOptions, const char* name) {
mtklein0d243ff2015-04-03 07:51:00 -0700817 for (int i = 0; i < FLAGS_blacklist.count() - 3; i += 4) {
mtkleina2ef6422015-01-15 13:44:22 -0800818 if (match(FLAGS_blacklist[i+0], sink) &&
djsollen54416de2015-04-03 07:24:48 -0700819 match(FLAGS_blacklist[i+1], src) &&
820 match(FLAGS_blacklist[i+2], srcOptions) &&
821 match(FLAGS_blacklist[i+3], name)) {
822 return SkStringPrintf("%s %s %s %s",
823 FLAGS_blacklist[i+0], FLAGS_blacklist[i+1],
824 FLAGS_blacklist[i+2], FLAGS_blacklist[i+3]);
mtkleina2ef6422015-01-15 13:44:22 -0800825 }
826 }
827 return "";
828}
829
mtkleincd50bca2016-01-05 06:20:20 -0800830// Even when a Task Sink reports to be non-threadsafe (e.g. GPU), we know things like
831// .png encoding are definitely thread safe. This lets us offload that work to CPU threads.
832static SkTaskGroup gDefinitelyThreadSafeWork;
833
mtklein748ca3b2015-01-15 10:56:12 -0800834// The finest-grained unit of work we can run: draw a single Src into a single Sink,
835// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
836struct Task {
mtkleine0effd62015-07-29 06:37:28 -0700837 Task(const TaggedSrc& src, const TaggedSink& sink) : src(src), sink(sink) {}
838 const TaggedSrc& src;
839 const TaggedSink& sink;
mtklein748ca3b2015-01-15 10:56:12 -0800840
mtklein21eaf3b2016-02-08 12:39:59 -0800841 static void Run(const Task& task) {
842 SkString name = task.src->name();
mtkleine0effd62015-07-29 06:37:28 -0700843
844 // We'll skip drawing this Src/Sink pair if:
845 // - the Src vetoes the Sink;
846 // - this Src / Sink combination is on the blacklist;
847 // - it's a dry run.
mtklein21eaf3b2016-02-08 12:39:59 -0800848 SkString note(task.src->veto(task.sink->flags()) ? " (veto)" : "");
849 SkString whyBlacklisted = is_blacklisted(task.sink.tag.c_str(), task.src.tag.c_str(),
850 task.src.options.c_str(), name.c_str());
mtkleinb37cb412015-03-18 05:27:14 -0700851 if (!whyBlacklisted.isEmpty()) {
852 note.appendf(" (--blacklist %s)", whyBlacklisted.c_str());
853 }
mtkleine0effd62015-07-29 06:37:28 -0700854
mtkleinb9eb4ac2015-02-02 18:26:03 -0800855 SkString log;
mtkleinf27f08b2015-11-03 06:54:24 -0800856 auto timerStart = now_ms();
mtkleine0effd62015-07-29 06:37:28 -0700857 if (!FLAGS_dryRun && note.isEmpty()) {
mtklein748ca3b2015-01-15 10:56:12 -0800858 SkBitmap bitmap;
859 SkDynamicMemoryWStream stream;
bsalomon821e10e2015-06-04 14:15:33 -0700860 if (FLAGS_pre_log) {
mtklein21eaf3b2016-02-08 12:39:59 -0800861 SkDebugf("\nRunning %s->%s", name.c_str(), task.sink.tag.c_str());
bsalomon821e10e2015-06-04 14:15:33 -0700862 }
mtklein21eaf3b2016-02-08 12:39:59 -0800863 start(task.sink.tag.c_str(), task.src.tag, task.src.options, name.c_str());
864 Error err = task.sink->draw(*task.src, &bitmap, &stream, &log);
mtklein748ca3b2015-01-15 10:56:12 -0800865 if (!err.isEmpty()) {
mtklein4089ef72015-03-05 08:40:28 -0800866 if (err.isFatal()) {
djsollen54416de2015-04-03 07:24:48 -0700867 fail(SkStringPrintf("%s %s %s %s: %s",
mtklein21eaf3b2016-02-08 12:39:59 -0800868 task.sink.tag.c_str(),
869 task.src.tag.c_str(),
870 task.src.options.c_str(),
mtklein4089ef72015-03-05 08:40:28 -0800871 name.c_str(),
872 err.c_str()));
mtkleinb37cb412015-03-18 05:27:14 -0700873 } else {
874 note.appendf(" (skipped: %s)", err.c_str());
mtkleinba6ada72016-01-21 09:39:35 -0800875 auto elapsed = now_ms() - timerStart;
mtklein21eaf3b2016-02-08 12:39:59 -0800876 done(elapsed, task.sink.tag.c_str(), task.src.tag, task.src.options,
mtkleinba6ada72016-01-21 09:39:35 -0800877 name, note, log);
878 return;
mtklein4089ef72015-03-05 08:40:28 -0800879 }
mtklein748ca3b2015-01-15 10:56:12 -0800880 }
mtklein62bd1a62015-01-27 14:46:26 -0800881
mtkleincd50bca2016-01-05 06:20:20 -0800882 // We're likely switching threads here, so we must capture by value, [=] or [foo,bar].
883 SkStreamAsset* data = stream.detachAsStream();
884 gDefinitelyThreadSafeWork.add([task,name,bitmap,data]{
885 SkAutoTDelete<SkStreamAsset> ownedData(data);
886
887 // Why doesn't the copy constructor do this when we have pre-locked pixels?
888 bitmap.lockPixels();
889
890 SkString md5;
891 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
892 SkMD5 hash;
893 if (data->getLength()) {
894 hash.writeStream(data, data->getLength());
895 data->rewind();
mtklein38408462015-07-08 07:25:27 -0700896 } else {
mtkleincd50bca2016-01-05 06:20:20 -0800897 // If we're BGRA (Linux, Windows), swizzle over to RGBA (Mac, Android).
898 // This helps eliminate multiple 0-pixel-diff hashes on gold.skia.org.
899 // (Android's general slow speed breaks the tie arbitrarily in RGBA's favor.)
900 // We might consider promoting 565 to RGBA too.
901 if (bitmap.colorType() == kBGRA_8888_SkColorType) {
902 SkBitmap swizzle;
903 SkAssertResult(bitmap.copyTo(&swizzle, kRGBA_8888_SkColorType));
904 hash.write(swizzle.getPixels(), swizzle.getSize());
905 } else {
906 hash.write(bitmap.getPixels(), bitmap.getSize());
907 }
908 }
909 SkMD5::Digest digest;
910 hash.finish(digest);
911 for (int i = 0; i < 16; i++) {
912 md5.appendf("%02x", digest.data[i]);
mtklein38408462015-07-08 07:25:27 -0700913 }
mtklein62bd1a62015-01-27 14:46:26 -0800914 }
mtklein62bd1a62015-01-27 14:46:26 -0800915
mtkleincd50bca2016-01-05 06:20:20 -0800916 if (!FLAGS_readPath.isEmpty() &&
mtklein21eaf3b2016-02-08 12:39:59 -0800917 !gGold.contains(Gold(task.sink.tag.c_str(), task.src.tag.c_str(),
918 task.src.options.c_str(), name, md5))) {
mtkleincd50bca2016-01-05 06:20:20 -0800919 fail(SkStringPrintf("%s not found for %s %s %s %s in %s",
920 md5.c_str(),
mtklein21eaf3b2016-02-08 12:39:59 -0800921 task.sink.tag.c_str(),
922 task.src.tag.c_str(),
923 task.src.options.c_str(),
mtkleincd50bca2016-01-05 06:20:20 -0800924 name.c_str(),
925 FLAGS_readPath[0]));
mtklein748ca3b2015-01-15 10:56:12 -0800926 }
mtkleincd50bca2016-01-05 06:20:20 -0800927
928 if (!FLAGS_writePath.isEmpty()) {
mtklein21eaf3b2016-02-08 12:39:59 -0800929 const char* ext = task.sink->fileExtension();
mtkleincd50bca2016-01-05 06:20:20 -0800930 if (data->getLength()) {
mtklein21eaf3b2016-02-08 12:39:59 -0800931 WriteToDisk(task, md5, ext, data, data->getLength(), nullptr);
mtkleincd50bca2016-01-05 06:20:20 -0800932 SkASSERT(bitmap.drawsNothing());
933 } else if (!bitmap.drawsNothing()) {
mtklein21eaf3b2016-02-08 12:39:59 -0800934 WriteToDisk(task, md5, ext, nullptr, 0, &bitmap);
mtkleincd50bca2016-01-05 06:20:20 -0800935 }
936 }
937 });
mtklein748ca3b2015-01-15 10:56:12 -0800938 }
mtkleinba6ada72016-01-21 09:39:35 -0800939 auto elapsed = now_ms() - timerStart;
mtklein21eaf3b2016-02-08 12:39:59 -0800940 done(elapsed, task.sink.tag.c_str(), task.src.tag.c_str(), task.src.options.c_str(),
mtkleinf27f08b2015-11-03 06:54:24 -0800941 name, note, log);
mtklein748ca3b2015-01-15 10:56:12 -0800942 }
943
944 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -0800945 SkString md5,
946 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -0800947 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -0800948 const SkBitmap* bitmap) {
mtklein748ca3b2015-01-15 10:56:12 -0800949 JsonWriter::BitmapResult result;
djsollen54416de2015-04-03 07:24:48 -0700950 result.name = task.src->name();
kkinnunen3e980c32015-12-23 01:33:00 -0800951 result.config = task.sink.tag.c_str();
djsollen54416de2015-04-03 07:24:48 -0700952 result.sourceType = task.src.tag;
953 result.sourceOptions = task.src.options;
954 result.ext = ext;
955 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -0800956 JsonWriter::AddBitmapResult(result);
957
mtkleinb0531a72015-04-07 13:38:48 -0700958 // If an MD5 is uninteresting, we want it noted in the JSON file,
959 // but don't want to dump it out as a .png (or whatever ext is).
960 if (gUninterestingHashes.contains(md5)) {
961 return;
962 }
963
mtklein748ca3b2015-01-15 10:56:12 -0800964 const char* dir = FLAGS_writePath[0];
965 if (0 == strcmp(dir, "@")) { // Needed for iOS.
966 dir = FLAGS_resourcePath[0];
967 }
968 sk_mkdir(dir);
969
970 SkString path;
971 if (FLAGS_nameByHash) {
972 path = SkOSPath::Join(dir, result.md5.c_str());
973 path.append(".");
974 path.append(ext);
975 if (sk_exists(path.c_str())) {
976 return; // Content-addressed. If it exists already, we're done.
977 }
978 } else {
kkinnunen3e980c32015-12-23 01:33:00 -0800979 path = SkOSPath::Join(dir, task.sink.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800980 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -0700981 path = SkOSPath::Join(path.c_str(), task.src.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800982 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -0700983 if (strcmp(task.src.options.c_str(), "") != 0) {
984 path = SkOSPath::Join(path.c_str(), task.src.options.c_str());
djsollen54416de2015-04-03 07:24:48 -0700985 sk_mkdir(path.c_str());
986 }
mtklein748ca3b2015-01-15 10:56:12 -0800987 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
988 path.append(".");
989 path.append(ext);
990 }
991
mtklein748ca3b2015-01-15 10:56:12 -0800992 if (bitmap) {
mtkleina5114d72015-08-24 13:27:01 -0700993 if (!dump_png(*bitmap, path.c_str(), result.md5.c_str())) {
mtklein748ca3b2015-01-15 10:56:12 -0800994 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
995 return;
996 }
997 } else {
mtkleina5114d72015-08-24 13:27:01 -0700998 SkFILEWStream file(path.c_str());
999 if (!file.isValid()) {
1000 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
1001 return;
1002 }
mtklein748ca3b2015-01-15 10:56:12 -08001003 if (!file.writeStream(data, len)) {
1004 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
1005 return;
1006 }
1007 }
1008 }
1009};
1010
mtklein748ca3b2015-01-15 10:56:12 -08001011/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1012
1013// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
1014
mtklein21eaf3b2016-02-08 12:39:59 -08001015static SkTDArray<skiatest::Test> gParallelTests, gSerialTests;
mtklein748ca3b2015-01-15 10:56:12 -08001016
1017static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -08001018 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -08001019 return;
1020 }
mtklein6393c062015-04-27 08:45:01 -07001021 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r; r = r->next()) {
1022 if (!in_shard()) {
1023 continue;
1024 }
halcanary87f3ba42015-01-20 09:30:20 -08001025 // Despite its name, factory() is returning a reference to
1026 // link-time static const POD data.
1027 const skiatest::Test& test = r->factory();
1028 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -08001029 continue;
1030 }
halcanary87f3ba42015-01-20 09:30:20 -08001031 if (test.needsGpu && gpu_supported()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001032 (FLAGS_gpu_threading ? gParallelTests : gSerialTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -08001033 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein21eaf3b2016-02-08 12:39:59 -08001034 gParallelTests.push(test);
mtklein82d28432015-01-15 12:46:02 -08001035 }
mtklein748ca3b2015-01-15 10:56:12 -08001036 }
1037}
1038
mtklein21eaf3b2016-02-08 12:39:59 -08001039static void run_test(skiatest::Test test) {
halcanary87f3ba42015-01-20 09:30:20 -08001040 struct : public skiatest::Reporter {
mtklein36352bf2015-03-25 18:17:31 -07001041 void reportFailed(const skiatest::Failure& failure) override {
halcanary87f3ba42015-01-20 09:30:20 -08001042 fail(failure.toString());
1043 JsonWriter::AddTestFailure(failure);
1044 }
mtklein36352bf2015-03-25 18:17:31 -07001045 bool allowExtendedTest() const override {
halcanary87f3ba42015-01-20 09:30:20 -08001046 return FLAGS_pathOpsExtended;
1047 }
mtklein36352bf2015-03-25 18:17:31 -07001048 bool verbose() const override { return FLAGS_veryVerbose; }
halcanary87f3ba42015-01-20 09:30:20 -08001049 } reporter;
djsollen824996a2015-06-12 12:06:22 -07001050
1051 SkString note;
mtklein21eaf3b2016-02-08 12:39:59 -08001052 SkString whyBlacklisted = is_blacklisted("_", "tests", "_", test.name);
djsollen824996a2015-06-12 12:06:22 -07001053 if (!whyBlacklisted.isEmpty()) {
1054 note.appendf(" (--blacklist %s)", whyBlacklisted.c_str());
1055 }
1056
mtkleinf27f08b2015-11-03 06:54:24 -08001057 auto timerStart = now_ms();
djsollen824996a2015-06-12 12:06:22 -07001058 if (!FLAGS_dryRun && whyBlacklisted.isEmpty()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001059 start("unit", "test", "", test.name);
mtklein55e88b22015-01-21 15:50:13 -08001060 GrContextFactory factory;
bsalomon821e10e2015-06-04 14:15:33 -07001061 if (FLAGS_pre_log) {
mtklein21eaf3b2016-02-08 12:39:59 -08001062 SkDebugf("\nRunning test %s", test.name);
bsalomon821e10e2015-06-04 14:15:33 -07001063 }
mtklein21eaf3b2016-02-08 12:39:59 -08001064 test.proc(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -08001065 }
mtklein21eaf3b2016-02-08 12:39:59 -08001066 done(now_ms()-timerStart, "unit", "test", "", test.name, note, "");
mtklein748ca3b2015-01-15 10:56:12 -08001067}
1068
1069/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1070
mtkleinde6fc2b2015-03-12 06:28:54 -07001071// Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung.
1072// This prints something every once in a while so that it knows we're still working.
mtklein2e1c47e2015-03-12 07:16:56 -07001073static void start_keepalive() {
1074 struct Loop {
1075 static void forever(void*) {
1076 for (;;) {
1077 static const int kSec = 300;
1078 #if defined(SK_BUILD_FOR_WIN)
1079 Sleep(kSec * 1000);
1080 #else
1081 sleep(kSec);
1082 #endif
mtkleinb37cb412015-03-18 05:27:14 -07001083 SkString running;
1084 {
mtkleinbc4e0032015-12-09 08:37:35 -08001085 SkAutoMutexAcquire lock(gRunningAndTallyMutex);
mtkleinb37cb412015-03-18 05:27:14 -07001086 for (int i = 0; i < gRunning.count(); i++) {
1087 running.appendf("\n\t%s", gRunning[i].c_str());
1088 }
1089 }
1090 SkDebugf("\nCurrently running:%s\n", running.c_str());
mtklein2e1c47e2015-03-12 07:16:56 -07001091 }
1092 }
1093 };
1094 static SkThread* intentionallyLeaked = new SkThread(Loop::forever);
1095 intentionallyLeaked->start();
mtkleinde6fc2b2015-03-12 06:28:54 -07001096}
1097
caryclark83ca6282015-06-10 09:31:09 -07001098#define PORTABLE_FONT_PREFIX "Toy Liberation "
1099
1100static SkTypeface* create_from_name(const char familyName[], SkTypeface::Style style) {
1101 if (familyName && strlen(familyName) > sizeof(PORTABLE_FONT_PREFIX)
1102 && !strncmp(familyName, PORTABLE_FONT_PREFIX, sizeof(PORTABLE_FONT_PREFIX) - 1)) {
caryclark1818acb2015-07-24 12:09:25 -07001103 return sk_tool_utils::create_portable_typeface(familyName, style);
caryclark83ca6282015-06-10 09:31:09 -07001104 }
halcanary96fcdcc2015-08-27 07:41:13 -07001105 return nullptr;
caryclark83ca6282015-06-10 09:31:09 -07001106}
1107
1108#undef PORTABLE_FONT_PREFIX
1109
1110extern SkTypeface* (*gCreateTypefaceDelegate)(const char [], SkTypeface::Style );
1111
jcgregorio3b27ade2014-11-13 08:06:40 -08001112int dm_main();
caryclark17f0b6d2014-07-22 10:15:34 -07001113int dm_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -08001114 SetupCrashHandler();
mtklein5286f022016-01-22 08:18:14 -08001115 JsonWriter::DumpJson(); // It's handy for the bots to assume this is ~never missing.
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +00001116 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -07001117 SkTaskGroup::Enabler enabled(FLAGS_threads);
caryclark83ca6282015-06-10 09:31:09 -07001118 gCreateTypefaceDelegate = &create_from_name;
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +00001119
mtklein2e1c47e2015-03-12 07:16:56 -07001120 start_keepalive();
mtkleinde6fc2b2015-03-12 06:28:54 -07001121
mtklein62bd1a62015-01-27 14:46:26 -08001122 gather_gold();
borenet09ed4802015-04-03 14:15:33 -07001123 gather_uninteresting_hashes();
mtklein62bd1a62015-01-27 14:46:26 -08001124
scroggo86737142016-02-03 12:19:11 -08001125 if (!gather_srcs()) {
1126 return 1;
1127 }
mtklein748ca3b2015-01-15 10:56:12 -08001128 gather_sinks();
1129 gather_tests();
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +00001130
mtklein21eaf3b2016-02-08 12:39:59 -08001131 gPending = gSrcs.count() * gSinks.count() + gParallelTests.count() + gSerialTests.count();
mtklein748ca3b2015-01-15 10:56:12 -08001132 SkDebugf("%d srcs * %d sinks + %d tests == %d tasks\n",
mtklein21eaf3b2016-02-08 12:39:59 -08001133 gSrcs.count(), gSinks.count(), gParallelTests.count() + gSerialTests.count(), gPending);
mtklein748ca3b2015-01-15 10:56:12 -08001134
mtklein21eaf3b2016-02-08 12:39:59 -08001135 // Kick off as much parallel work as we can, making note of any serial work we'll need to do.
1136 SkTaskGroup parallel;
1137 SkTArray<Task> serial;
1138
1139 for (auto& sink : gSinks)
1140 for (auto& src : gSrcs) {
1141 Task task(src, sink);
1142 if (src->serial() || sink->serial()) {
1143 serial.push_back(task);
1144 } else {
1145 parallel.add([task] { Task::Run(task); });
mtklein748ca3b2015-01-15 10:56:12 -08001146 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001147 }
mtklein21eaf3b2016-02-08 12:39:59 -08001148 for (auto test : gParallelTests) {
1149 parallel.add([test] { run_test(test); });
mtklein55e88b22015-01-21 15:50:13 -08001150 }
mtklein21eaf3b2016-02-08 12:39:59 -08001151
1152 // With the parallel work running, run serial tasks and tests here on main thread.
1153 for (auto task : serial) { Task::Run(task); }
1154 for (auto test : gSerialTests) { run_test(test); }
1155
1156 // Wait for any remaining parallel work to complete (including any spun off of serial tasks).
1157 parallel.wait();
mtkleincd50bca2016-01-05 06:20:20 -08001158 gDefinitelyThreadSafeWork.wait();
1159
mtklein748ca3b2015-01-15 10:56:12 -08001160 // At this point we're back in single-threaded land.
caryclarkf53ce802015-06-15 06:48:30 -07001161 sk_tool_utils::release_portable_typefaces();
mtklein@google.comd36522d2013-10-16 13:02:15 +00001162
mtkleinbc4e0032015-12-09 08:37:35 -08001163 if (FLAGS_verbose && gNoteTally.count() > 0) {
1164 SkDebugf("\nNote tally:\n");
1165 gNoteTally.foreach([](const SkString& note, int* tally) {
1166 SkDebugf("%dx\t%s\n", *tally, note.c_str());
1167 });
1168 }
1169
mtklein2f64eec2015-01-15 14:20:41 -08001170 SkDebugf("\n");
mtklein748ca3b2015-01-15 10:56:12 -08001171 if (gFailures.count() > 0) {
1172 SkDebugf("Failures:\n");
1173 for (int i = 0; i < gFailures.count(); i++) {
mtklein9dc09102015-01-15 15:47:33 -08001174 SkDebugf("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001175 }
1176 SkDebugf("%d failures\n", gFailures.count());
1177 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001178 }
mtklein748ca3b2015-01-15 10:56:12 -08001179 if (gPending > 0) {
1180 SkDebugf("Hrm, we didn't seem to run everything we intended to! Please file a bug.\n");
1181 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001182 }
halcanary7a14b312015-10-01 07:28:13 -07001183 #ifdef SK_PDF_IMAGE_STATS
1184 SkPDFImageDumpStats();
1185 #endif // SK_PDF_IMAGE_STATS
scroggo4d9eaea2016-01-29 07:48:33 -08001186 SkDebugf("Finished!\n");
mtklein748ca3b2015-01-15 10:56:12 -08001187 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +00001188}
jcgregorio3b27ade2014-11-13 08:06:40 -08001189
kkinnunen179a8f52015-11-20 13:32:24 -08001190// TODO: currently many GPU tests are declared outside SK_SUPPORT_GPU guards.
1191// Thus we export the empty RunWithGPUTestContexts when SK_SUPPORT_GPU=0.
1192namespace skiatest {
1193namespace {
1194typedef void(*TestWithGrContext)(skiatest::Reporter*, GrContext*);
1195typedef void(*TestWithGrContextAndGLContext)(skiatest::Reporter*, GrContext*, SkGLContext*);
1196#if SK_SUPPORT_GPU
1197template<typename T>
kkinnunen34058002016-01-06 23:49:30 -08001198void call_test(T test, skiatest::Reporter* reporter, const GrContextFactory::ContextInfo& context);
kkinnunen179a8f52015-11-20 13:32:24 -08001199template<>
1200void call_test(TestWithGrContext test, skiatest::Reporter* reporter,
kkinnunen34058002016-01-06 23:49:30 -08001201 const GrContextFactory::ContextInfo& context) {
1202 test(reporter, context.fGrContext);
kkinnunen179a8f52015-11-20 13:32:24 -08001203}
1204template<>
1205void call_test(TestWithGrContextAndGLContext test, skiatest::Reporter* reporter,
kkinnunen34058002016-01-06 23:49:30 -08001206 const GrContextFactory::ContextInfo& context) {
1207 test(reporter, context.fGrContext, context.fGLContext);
kkinnunen179a8f52015-11-20 13:32:24 -08001208}
1209#endif
1210} // namespace
1211
kkinnunen179a8f52015-11-20 13:32:24 -08001212template<typename T>
1213void RunWithGPUTestContexts(T test, GPUTestContexts testContexts, Reporter* reporter,
1214 GrContextFactory* factory) {
1215#if SK_SUPPORT_GPU
kkinnunen3e980c32015-12-23 01:33:00 -08001216 // Iterate over context types, except use "native" instead of explicitly trying OpenGL and
1217 // OpenGL ES. Do not use GLES on desktop, since tests do not account for not fixing
1218 // http://skbug.com/2809
1219 GrContextFactory::GLContextType contextTypes[] = {
1220 GrContextFactory::kNative_GLContextType,
1221#if SK_ANGLE
1222#ifdef SK_BUILD_FOR_WIN
1223 GrContextFactory::kANGLE_GLContextType,
1224#endif
1225 GrContextFactory::kANGLE_GL_GLContextType,
1226#endif
1227#if SK_COMMAND_BUFFER
1228 GrContextFactory::kCommandBuffer_GLContextType,
1229#endif
1230#if SK_MESA
1231 GrContextFactory::kMESA_GLContextType,
1232#endif
1233 GrContextFactory::kNull_GLContextType,
1234 GrContextFactory::kDebug_GLContextType,
1235 };
1236 static_assert(SK_ARRAY_COUNT(contextTypes) == GrContextFactory::kGLContextTypeCnt - 2,
1237 "Skipping unexpected GLContextType for GPU tests");
1238
1239 for (auto& contextType : contextTypes) {
kkinnunen179a8f52015-11-20 13:32:24 -08001240 int contextSelector = kNone_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001241 if (GrContextFactory::IsRenderingGLContext(contextType)) {
kkinnunen179a8f52015-11-20 13:32:24 -08001242 contextSelector |= kAllRendering_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001243 } else if (contextType == GrContextFactory::kNative_GLContextType) {
kkinnunen179a8f52015-11-20 13:32:24 -08001244 contextSelector |= kNative_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001245 } else if (contextType == GrContextFactory::kNull_GLContextType) {
kkinnunen179a8f52015-11-20 13:32:24 -08001246 contextSelector |= kNull_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001247 } else if (contextType == GrContextFactory::kDebug_GLContextType) {
kkinnunen55eeae92015-12-10 23:19:29 -08001248 contextSelector |= kDebug_GPUTestContexts;
kkinnunen179a8f52015-11-20 13:32:24 -08001249 }
1250 if ((testContexts & contextSelector) == 0) {
1251 continue;
1252 }
kkinnunen34058002016-01-06 23:49:30 -08001253 GrContextFactory::ContextInfo context = factory->getContextInfo(contextType);
1254 if (context.fGrContext) {
kkinnunen5219fd92015-12-10 06:28:13 -08001255 call_test(test, reporter, context);
1256 }
kkinnunen34058002016-01-06 23:49:30 -08001257 context = factory->getContextInfo(contextType,
1258 GrContextFactory::kEnableNVPR_GLContextOptions);
1259 if (context.fGrContext) {
kkinnunen179a8f52015-11-20 13:32:24 -08001260 call_test(test, reporter, context);
1261 }
1262 }
1263#endif
1264}
1265
1266template
1267void RunWithGPUTestContexts<TestWithGrContext>(TestWithGrContext test,
1268 GPUTestContexts testContexts,
1269 Reporter* reporter,
1270 GrContextFactory* factory);
1271template
1272void RunWithGPUTestContexts<TestWithGrContextAndGLContext>(TestWithGrContextAndGLContext test,
1273 GPUTestContexts testContexts,
1274 Reporter* reporter,
1275 GrContextFactory* factory);
1276} // namespace skiatest
1277
borenet48087572015-04-02 12:16:36 -07001278#if !defined(SK_BUILD_FOR_IOS)
jcgregorio3b27ade2014-11-13 08:06:40 -08001279int main(int argc, char** argv) {
1280 SkCommandLineFlags::Parse(argc, argv);
1281 return dm_main();
1282}
1283#endif