blob: 5124442a20abb623fef0f4682a3ae3c28f4d2bcc [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?");
bsalomon821e10e2015-06-04 14:15:33 -070067DEFINE_bool2(pre_log, p, false, "Log before running each test. May be incomprehensible when threading");
mtklein6393c062015-04-27 08:45:01 -070068
mtklein@google.comd36522d2013-10-16 13:02:15 +000069__SK_FORCE_IMAGE_DECODER_LINKING;
mtklein748ca3b2015-01-15 10:56:12 -080070using namespace DM;
mtklein@google.comd36522d2013-10-16 13:02:15 +000071
mtklein748ca3b2015-01-15 10:56:12 -080072/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
73
mtkleinf27f08b2015-11-03 06:54:24 -080074static double now_ms() { return SkTime::GetNSecs() * 1e-6; }
75
mtklein748ca3b2015-01-15 10:56:12 -080076SK_DECLARE_STATIC_MUTEX(gFailuresMutex);
77static SkTArray<SkString> gFailures;
78
79static void fail(ImplicitString err) {
80 SkAutoMutexAcquire lock(gFailuresMutex);
81 SkDebugf("\n\nFAILURE: %s\n\n", err.c_str());
82 gFailures.push_back(err);
halcanary9e398f72015-01-10 11:18:04 -080083}
84
mtkleinb37cb412015-03-18 05:27:14 -070085static int32_t gPending = 0; // Atomic. Total number of running and queued tasks.
86
mtkleinbc4e0032015-12-09 08:37:35 -080087SK_DECLARE_STATIC_MUTEX(gRunningAndTallyMutex);
88static SkTArray<SkString> gRunning;
89static SkTHashMap<SkString, int> gNoteTally;
mtklein748ca3b2015-01-15 10:56:12 -080090
mtkleinb9eb4ac2015-02-02 18:26:03 -080091static void done(double ms,
djsollen54416de2015-04-03 07:24:48 -070092 ImplicitString config, ImplicitString src, ImplicitString srcOptions,
93 ImplicitString name, ImplicitString note, ImplicitString log) {
94 SkString id = SkStringPrintf("%s %s %s %s", config.c_str(), src.c_str(),
95 srcOptions.c_str(), name.c_str());
mtkleinb37cb412015-03-18 05:27:14 -070096 {
mtkleinbc4e0032015-12-09 08:37:35 -080097 SkAutoMutexAcquire lock(gRunningAndTallyMutex);
mtkleinb37cb412015-03-18 05:27:14 -070098 for (int i = 0; i < gRunning.count(); i++) {
99 if (gRunning[i] == id) {
100 gRunning.removeShuffle(i);
101 break;
102 }
103 }
mtkleinbc4e0032015-12-09 08:37:35 -0800104 if (!note.isEmpty()) {
105 if (int* tally = gNoteTally.find(note)) {
106 *tally += 1;
107 } else {
108 gNoteTally.set(note, 1);
109 }
110 }
mtkleinb37cb412015-03-18 05:27:14 -0700111 }
mtkleinb9eb4ac2015-02-02 18:26:03 -0800112 if (!log.isEmpty()) {
113 log.prepend("\n");
114 }
mtklein6dee2ad2015-02-05 09:53:44 -0800115 auto pending = sk_atomic_dec(&gPending)-1;
mtkleinbc4e0032015-12-09 08:37:35 -0800116 if (!FLAGS_quiet && note.isEmpty()) {
117 SkDebugf("%s(%4d/%-4dMB %6d) %s\t%s%s", FLAGS_verbose ? "\n" : kSkOverwriteLine
reed50bc0512015-05-19 14:13:31 -0700118 , sk_tools::getCurrResidentSetSizeMB()
119 , sk_tools::getMaxResidentSetSizeMB()
120 , pending
121 , HumanizeMs(ms).c_str()
122 , id.c_str()
reed50bc0512015-05-19 14:13:31 -0700123 , log.c_str());
124 }
mtkleina17241b2015-01-23 05:48:00 -0800125 // We write our dm.json file every once in a while in case we crash.
126 // Notice this also handles the final dm.json when pending == 0.
127 if (pending % 500 == 0) {
128 JsonWriter::DumpJson();
129 }
mtklein@google.comd36522d2013-10-16 13:02:15 +0000130}
131
djsollen54416de2015-04-03 07:24:48 -0700132static void start(ImplicitString config, ImplicitString src,
133 ImplicitString srcOptions, ImplicitString name) {
134 SkString id = SkStringPrintf("%s %s %s %s", config.c_str(), src.c_str(),
135 srcOptions.c_str(), name.c_str());
mtkleinbc4e0032015-12-09 08:37:35 -0800136 SkAutoMutexAcquire lock(gRunningAndTallyMutex);
mtkleinb37cb412015-03-18 05:27:14 -0700137 gRunning.push_back(id);
138}
139
mtklein748ca3b2015-01-15 10:56:12 -0800140/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
mtklein114c3cd2015-01-15 10:15:02 -0800141
mtklein62bd1a62015-01-27 14:46:26 -0800142struct Gold : public SkString {
mtkleina82f5622015-02-20 12:30:19 -0800143 Gold() : SkString("") {}
djsollen54416de2015-04-03 07:24:48 -0700144 Gold(ImplicitString sink, ImplicitString src, ImplicitString srcOptions,
145 ImplicitString name, ImplicitString md5)
mtklein62bd1a62015-01-27 14:46:26 -0800146 : SkString("") {
147 this->append(sink);
148 this->append(src);
djsollen54416de2015-04-03 07:24:48 -0700149 this->append(srcOptions);
mtklein62bd1a62015-01-27 14:46:26 -0800150 this->append(name);
151 this->append(md5);
mtklein62bd1a62015-01-27 14:46:26 -0800152 }
mtkleinc8d1dd42015-10-15 12:23:01 -0700153 struct Hash {
154 uint32_t operator()(const Gold& g) const {
155 return SkGoodHash()((const SkString&)g);
156 }
157 };
mtklein62bd1a62015-01-27 14:46:26 -0800158};
mtkleina82f5622015-02-20 12:30:19 -0800159static SkTHashSet<Gold, Gold::Hash> gGold;
mtklein62bd1a62015-01-27 14:46:26 -0800160
161static void add_gold(JsonWriter::BitmapResult r) {
djsollen54416de2015-04-03 07:24:48 -0700162 gGold.add(Gold(r.config, r.sourceType, r.sourceOptions, r.name, r.md5));
mtklein62bd1a62015-01-27 14:46:26 -0800163}
164
165static void gather_gold() {
166 if (!FLAGS_readPath.isEmpty()) {
167 SkString path(FLAGS_readPath[0]);
168 path.append("/dm.json");
169 if (!JsonWriter::ReadJson(path.c_str(), add_gold)) {
170 fail(SkStringPrintf("Couldn't read %s for golden results.", path.c_str()));
171 }
172 }
173}
174
175/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
176
borenet09ed4802015-04-03 14:15:33 -0700177static SkTHashSet<SkString> gUninterestingHashes;
178
179static void gather_uninteresting_hashes() {
180 if (!FLAGS_uninterestingHashesFile.isEmpty()) {
181 SkAutoTUnref<SkData> data(SkData::NewFromFileName(FLAGS_uninterestingHashesFile[0]));
mtkleincc334b32015-09-22 11:43:53 -0700182 if (!data) {
183 SkDebugf("WARNING: unable to read uninteresting hashes from %s\n",
184 FLAGS_uninterestingHashesFile[0]);
185 return;
186 }
borenet09ed4802015-04-03 14:15:33 -0700187 SkTArray<SkString> hashes;
188 SkStrSplit((const char*)data->data(), "\n", &hashes);
189 for (const SkString& hash : hashes) {
190 gUninterestingHashes.add(hash);
191 }
192 }
193}
194
195/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
196
mtkleine0effd62015-07-29 06:37:28 -0700197struct TaggedSrc : public SkAutoTDelete<Src> {
msarett9e707a02015-09-01 14:57:57 -0700198 ImplicitString tag;
199 ImplicitString options;
mtkleine0effd62015-07-29 06:37:28 -0700200};
201
202struct TaggedSink : public SkAutoTDelete<Sink> {
kkinnunen3e980c32015-12-23 01:33:00 -0800203 SkString tag;
djsollen54416de2015-04-03 07:24:48 -0700204};
mtklein748ca3b2015-01-15 10:56:12 -0800205
206static const bool kMemcpyOK = true;
207
mtkleine0effd62015-07-29 06:37:28 -0700208static SkTArray<TaggedSrc, kMemcpyOK> gSrcs;
209static SkTArray<TaggedSink, kMemcpyOK> gSinks;
mtklein748ca3b2015-01-15 10:56:12 -0800210
mtklein6393c062015-04-27 08:45:01 -0700211static bool in_shard() {
212 static int N = 0;
213 return N++ % FLAGS_shards == FLAGS_shard;
214}
215
msarett9e707a02015-09-01 14:57:57 -0700216static void push_src(ImplicitString tag, ImplicitString options, Src* s) {
mtklein748ca3b2015-01-15 10:56:12 -0800217 SkAutoTDelete<Src> src(s);
mtklein6393c062015-04-27 08:45:01 -0700218 if (in_shard() &&
msarett9e707a02015-09-01 14:57:57 -0700219 FLAGS_src.contains(tag.c_str()) &&
mtklein748ca3b2015-01-15 10:56:12 -0800220 !SkCommandLineFlags::ShouldSkip(FLAGS_match, src->name().c_str())) {
mtkleine0effd62015-07-29 06:37:28 -0700221 TaggedSrc& s = gSrcs.push_back();
mtklein748ca3b2015-01-15 10:56:12 -0800222 s.reset(src.detach());
223 s.tag = tag;
djsollen54416de2015-04-03 07:24:48 -0700224 s.options = options;
mtklein114c3cd2015-01-15 10:15:02 -0800225 }
mtklein748ca3b2015-01-15 10:56:12 -0800226}
mtklein114c3cd2015-01-15 10:15:02 -0800227
msarett9e707a02015-09-01 14:57:57 -0700228static void push_codec_src(Path path, CodecSrc::Mode mode, CodecSrc::DstColorType dstColorType,
229 float scale) {
230 SkString folder;
231 switch (mode) {
232 case CodecSrc::kCodec_Mode:
233 folder.append("codec");
234 break;
msarettbb25b532016-01-13 09:31:39 -0800235 case CodecSrc::kCodecZeroInit_Mode:
236 folder.append("codec_zero_init");
237 break;
msarett9e707a02015-09-01 14:57:57 -0700238 case CodecSrc::kScanline_Mode:
239 folder.append("scanline");
240 break;
msarett9e707a02015-09-01 14:57:57 -0700241 case CodecSrc::kStripe_Mode:
242 folder.append("stripe");
243 break;
244 case CodecSrc::kSubset_Mode:
msarett36c37962015-09-02 13:20:52 -0700245 folder.append("codec_subset");
msarett9e707a02015-09-01 14:57:57 -0700246 break;
msarettb714fb02016-01-22 14:46:42 -0800247 case CodecSrc::kGen_Mode:
248 folder.append("gen");
249 break;
msarett9e707a02015-09-01 14:57:57 -0700250 }
251
252 switch (dstColorType) {
253 case CodecSrc::kGrayscale_Always_DstColorType:
254 folder.append("_kGray8");
255 break;
256 case CodecSrc::kIndex8_Always_DstColorType:
257 folder.append("_kIndex8");
258 break;
259 default:
260 break;
261 }
262
263 if (1.0f != scale) {
264 folder.appendf("_%.3f", scale);
265 }
266
267 CodecSrc* src = new CodecSrc(path, mode, dstColorType, scale);
268 push_src("image", folder, src);
269}
270
msarett3d9d7a72015-10-21 10:27:10 -0700271static void push_android_codec_src(Path path, AndroidCodecSrc::Mode mode,
272 CodecSrc::DstColorType dstColorType, int sampleSize) {
273 SkString folder;
274 switch (mode) {
275 case AndroidCodecSrc::kFullImage_Mode:
276 folder.append("scaled_codec");
277 break;
278 case AndroidCodecSrc::kDivisor_Mode:
279 folder.append("scaled_codec_divisor");
280 break;
281 }
282
283 switch (dstColorType) {
284 case CodecSrc::kGrayscale_Always_DstColorType:
285 folder.append("_kGray8");
286 break;
287 case CodecSrc::kIndex8_Always_DstColorType:
288 folder.append("_kIndex8");
289 break;
290 default:
291 break;
292 }
293
294 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800295 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700296 }
297
298 AndroidCodecSrc* src = new AndroidCodecSrc(path, mode, dstColorType, sampleSize);
299 push_src("image", folder, src);
300}
301
msarett438b2ad2015-04-09 12:43:10 -0700302static void push_codec_srcs(Path path) {
303 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
304 if (!encoded) {
305 SkDebugf("Couldn't read %s.", path.c_str());
306 return;
307 }
308 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
halcanary96fcdcc2015-08-27 07:41:13 -0700309 if (nullptr == codec.get()) {
msarett438b2ad2015-04-09 12:43:10 -0700310 SkDebugf("Couldn't create codec for %s.", path.c_str());
311 return;
312 }
313
msarett36c37962015-09-02 13:20:52 -0700314 // Native Scales
msarett0a242972015-06-11 14:27:27 -0700315 // TODO (msarett): Implement scaling tests for SkImageDecoder in order to compare with these
316 // tests. SkImageDecoder supports downscales by integer factors.
emmaleer8f4ba762015-08-14 07:44:46 -0700317 // 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 -0700318 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 -0700319
msarettbb25b532016-01-13 09:31:39 -0800320 const CodecSrc::Mode nativeModes[] = { CodecSrc::kCodec_Mode, CodecSrc::kCodecZeroInit_Mode,
msarettb714fb02016-01-22 14:46:42 -0800321 CodecSrc::kScanline_Mode, CodecSrc::kStripe_Mode, CodecSrc::kSubset_Mode,
322 CodecSrc::kGen_Mode };
msarett9e707a02015-09-01 14:57:57 -0700323
msarett36c37962015-09-02 13:20:52 -0700324 CodecSrc::DstColorType colorTypes[3];
325 uint32_t numColorTypes;
326 switch (codec->getInfo().colorType()) {
327 case kGray_8_SkColorType:
328 // FIXME: Is this a long term solution for testing wbmps decodes to kIndex8?
halcanary6950de62015-11-07 05:29:00 -0800329 // Further discussion on this topic is at https://bug.skia.org/3683 .
msarett3d9d7a72015-10-21 10:27:10 -0700330 // This causes us to try to convert grayscale jpegs to kIndex8. We currently
331 // fail non-fatally in this case.
msarett36c37962015-09-02 13:20:52 -0700332 colorTypes[0] = CodecSrc::kGetFromCanvas_DstColorType;
333 colorTypes[1] = CodecSrc::kGrayscale_Always_DstColorType;
334 colorTypes[2] = CodecSrc::kIndex8_Always_DstColorType;
335 numColorTypes = 3;
336 break;
337 case kIndex_8_SkColorType:
338 colorTypes[0] = CodecSrc::kGetFromCanvas_DstColorType;
339 colorTypes[1] = CodecSrc::kIndex8_Always_DstColorType;
340 numColorTypes = 2;
341 break;
342 default:
343 colorTypes[0] = CodecSrc::kGetFromCanvas_DstColorType;
344 numColorTypes = 1;
345 break;
346 }
msarett9e707a02015-09-01 14:57:57 -0700347
msarettb714fb02016-01-22 14:46:42 -0800348
349 for (CodecSrc::Mode mode : nativeModes) {
350 // SkCodecImageGenerator only runs for the default colorType
351 // recommended by SkCodec. There is no need to generate multiple
352 // tests for different colorTypes.
353 // TODO (msarett): Add scaling support to SkCodecImageGenerator.
354 if (CodecSrc::kGen_Mode == mode) {
355 // FIXME: The gpu backend does not draw kGray sources correctly. (skbug.com/4822)
356 if (kGray_8_SkColorType != codec->getInfo().colorType()) {
357 push_codec_src(path, mode, CodecSrc::kGetFromCanvas_DstColorType, 1.0f);
358 }
359 continue;
360 }
361
362 for (float scale : nativeScales) {
msarett36c37962015-09-02 13:20:52 -0700363 for (uint32_t i = 0; i < numColorTypes; i++) {
364 push_codec_src(path, mode, colorTypes[i], scale);
msarett9e707a02015-09-01 14:57:57 -0700365 }
366 }
msarett0a242972015-06-11 14:27:27 -0700367 }
msarett36c37962015-09-02 13:20:52 -0700368
halcanary6950de62015-11-07 05:29:00 -0800369 // https://bug.skia.org/4428
msarett33c76232015-11-16 13:43:40 -0800370 bool subset = false;
371 // The following image types are supported by BitmapRegionDecoder,
372 // so we will test full image decodes and subset decodes.
msarettbe8216a2015-12-04 08:00:50 -0800373 static const char* const exts[] = {
msarett3d9d7a72015-10-21 10:27:10 -0700374 "jpg", "jpeg", "png", "webp",
375 "JPG", "JPEG", "PNG", "WEBP",
376 };
msarettbe8216a2015-12-04 08:00:50 -0800377 for (const char* ext : exts) {
msarett3d9d7a72015-10-21 10:27:10 -0700378 if (path.endsWith(ext)) {
msarett33c76232015-11-16 13:43:40 -0800379 subset = true;
msarett3d9d7a72015-10-21 10:27:10 -0700380 break;
381 }
382 }
msarett33c76232015-11-16 13:43:40 -0800383
msarett3d9d7a72015-10-21 10:27:10 -0700384 const int sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
msarett36c37962015-09-02 13:20:52 -0700385
msarett3d9d7a72015-10-21 10:27:10 -0700386 for (int sampleSize : sampleSizes) {
msarett33c76232015-11-16 13:43:40 -0800387 for (uint32_t i = 0; i < numColorTypes; i++) {
388 push_android_codec_src(path, AndroidCodecSrc::kFullImage_Mode, colorTypes[i],
389 sampleSize);
390 if (subset) {
391 push_android_codec_src(path, AndroidCodecSrc::kDivisor_Mode, colorTypes[i],
392 sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700393 }
msarett36c37962015-09-02 13:20:52 -0700394 }
395 }
msarett438b2ad2015-04-09 12:43:10 -0700396}
397
msarett5cb48852015-11-06 08:56:32 -0800398static bool brd_color_type_supported(SkBitmapRegionDecoder::Strategy strategy,
msaretta5783ae2015-09-08 15:35:32 -0700399 CodecSrc::DstColorType dstColorType) {
400 switch (strategy) {
msarett5cb48852015-11-06 08:56:32 -0800401 case SkBitmapRegionDecoder::kCanvas_Strategy:
msaretta5783ae2015-09-08 15:35:32 -0700402 if (CodecSrc::kGetFromCanvas_DstColorType == dstColorType) {
403 return true;
404 }
405 return false;
msarett5cb48852015-11-06 08:56:32 -0800406 case SkBitmapRegionDecoder::kAndroidCodec_Strategy:
msarett26ad17b2015-10-22 07:29:19 -0700407 switch (dstColorType) {
408 case CodecSrc::kGetFromCanvas_DstColorType:
409 case CodecSrc::kIndex8_Always_DstColorType:
410 case CodecSrc::kGrayscale_Always_DstColorType:
411 return true;
412 default:
413 return false;
414 }
msaretta5783ae2015-09-08 15:35:32 -0700415 default:
416 SkASSERT(false);
417 return false;
418 }
419}
420
msarett5cb48852015-11-06 08:56:32 -0800421static void push_brd_src(Path path, SkBitmapRegionDecoder::Strategy strategy,
msaretta5783ae2015-09-08 15:35:32 -0700422 CodecSrc::DstColorType dstColorType, BRDSrc::Mode mode, uint32_t sampleSize) {
423 SkString folder;
424 switch (strategy) {
msarett5cb48852015-11-06 08:56:32 -0800425 case SkBitmapRegionDecoder::kCanvas_Strategy:
msaretta5783ae2015-09-08 15:35:32 -0700426 folder.append("brd_canvas");
427 break;
msarett5cb48852015-11-06 08:56:32 -0800428 case SkBitmapRegionDecoder::kAndroidCodec_Strategy:
msarett26ad17b2015-10-22 07:29:19 -0700429 folder.append("brd_android_codec");
430 break;
msaretta5783ae2015-09-08 15:35:32 -0700431 default:
432 SkASSERT(false);
433 return;
434 }
435
436 switch (mode) {
437 case BRDSrc::kFullImage_Mode:
438 break;
439 case BRDSrc::kDivisor_Mode:
440 folder.append("_divisor");
441 break;
442 default:
443 SkASSERT(false);
444 return;
445 }
446
447 switch (dstColorType) {
448 case CodecSrc::kGetFromCanvas_DstColorType:
449 break;
450 case CodecSrc::kIndex8_Always_DstColorType:
451 folder.append("_kIndex");
452 break;
453 case CodecSrc::kGrayscale_Always_DstColorType:
454 folder.append("_kGray");
455 break;
456 default:
457 SkASSERT(false);
458 return;
459 }
460
461 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800462 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700463 }
464
465 BRDSrc* src = new BRDSrc(path, strategy, mode, dstColorType, sampleSize);
466 push_src("image", folder, src);
467}
468
469static void push_brd_srcs(Path path) {
470
msarett5cb48852015-11-06 08:56:32 -0800471 const SkBitmapRegionDecoder::Strategy strategies[] = {
472 SkBitmapRegionDecoder::kCanvas_Strategy,
msarett5cb48852015-11-06 08:56:32 -0800473 SkBitmapRegionDecoder::kAndroidCodec_Strategy,
msaretta5783ae2015-09-08 15:35:32 -0700474 };
475
scroggo501b7342015-11-03 07:55:11 -0800476 // Test on a variety of sampleSizes, making sure to include:
477 // - 2, 4, and 8, which are natively supported by jpeg
478 // - multiples of 2 which are not divisible by 4 (analogous for 4)
479 // - larger powers of two, since BRD clients generally use powers of 2
480 // We will only produce output for the larger sizes on large images.
481 const uint32_t sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 24, 32, 64 };
msarett6efbe052015-09-11 09:01:16 -0700482
msaretta5783ae2015-09-08 15:35:32 -0700483 // We will only test to one backend (8888), but we will test all of the
484 // color types that we need to decode to on this backend.
485 const CodecSrc::DstColorType dstColorTypes[] = {
msarett26ad17b2015-10-22 07:29:19 -0700486 CodecSrc::kGetFromCanvas_DstColorType,
487 CodecSrc::kIndex8_Always_DstColorType,
488 CodecSrc::kGrayscale_Always_DstColorType,
msaretta5783ae2015-09-08 15:35:32 -0700489 };
490
491 const BRDSrc::Mode modes[] = {
msarett26ad17b2015-10-22 07:29:19 -0700492 BRDSrc::kFullImage_Mode,
493 BRDSrc::kDivisor_Mode,
msaretta5783ae2015-09-08 15:35:32 -0700494 };
495
msarett5cb48852015-11-06 08:56:32 -0800496 for (SkBitmapRegionDecoder::Strategy strategy : strategies) {
msarett6efbe052015-09-11 09:01:16 -0700497 for (uint32_t sampleSize : sampleSizes) {
msarett6efbe052015-09-11 09:01:16 -0700498 for (CodecSrc::DstColorType dstColorType : dstColorTypes) {
499 if (brd_color_type_supported(strategy, dstColorType)) {
500 for (BRDSrc::Mode mode : modes) {
msaretta5783ae2015-09-08 15:35:32 -0700501 push_brd_src(path, strategy, dstColorType, mode, sampleSize);
502 }
503 }
504 }
505 }
506 }
507}
508
509static bool brd_supported(const char* ext) {
msarett8c8f22a2015-04-01 06:58:48 -0700510 static const char* const exts[] = {
msaretta5783ae2015-09-08 15:35:32 -0700511 "jpg", "jpeg", "png", "webp",
512 "JPG", "JPEG", "PNG", "WEBP",
msarett8c8f22a2015-04-01 06:58:48 -0700513 };
514
515 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
516 if (0 == strcmp(exts[i], ext)) {
517 return true;
518 }
519 }
520 return false;
scroggo9b77ddd2015-03-19 06:03:39 -0700521}
522
mtklein748ca3b2015-01-15 10:56:12 -0800523static void gather_srcs() {
524 for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) {
djsollen54416de2015-04-03 07:24:48 -0700525 push_src("gm", "", new GMSrc(r->factory()));
mtklein748ca3b2015-01-15 10:56:12 -0800526 }
halcanaryfc37ad12015-01-30 07:31:19 -0800527 for (int i = 0; i < FLAGS_skps.count(); i++) {
528 const char* path = FLAGS_skps[i];
529 if (sk_isdir(path)) {
530 SkOSFile::Iter it(path, "skp");
531 for (SkString file; it.next(&file); ) {
djsollen54416de2015-04-03 07:24:48 -0700532 push_src("skp", "", new SKPSrc(SkOSPath::Join(path, file.c_str())));
halcanaryfc37ad12015-01-30 07:31:19 -0800533 }
534 } else {
djsollen54416de2015-04-03 07:24:48 -0700535 push_src("skp", "", new SKPSrc(path));
mtklein114c3cd2015-01-15 10:15:02 -0800536 }
537 }
halcanary23b03c32015-01-30 09:58:58 -0800538 static const char* const exts[] = {
539 "bmp", "gif", "jpg", "jpeg", "png", "webp", "ktx", "astc", "wbmp", "ico",
540 "BMP", "GIF", "JPG", "JPEG", "PNG", "WEBP", "KTX", "ASTC", "WBMP", "ICO",
541 };
542 for (int i = 0; i < FLAGS_images.count(); i++) {
543 const char* flag = FLAGS_images[i];
544 if (sk_isdir(flag)) {
545 for (size_t j = 0; j < SK_ARRAY_COUNT(exts); j++) {
546 SkOSFile::Iter it(flag, exts[j]);
547 for (SkString file; it.next(&file); ) {
548 SkString path = SkOSPath::Join(flag, file.c_str());
djsollen54416de2015-04-03 07:24:48 -0700549 push_src("image", "decode", new ImageSrc(path)); // Decode entire image
msaretta5783ae2015-09-08 15:35:32 -0700550 push_codec_srcs(path);
551 if (brd_supported(exts[j])) {
552 push_brd_srcs(path);
scroggo9b77ddd2015-03-19 06:03:39 -0700553 }
halcanary23b03c32015-01-30 09:58:58 -0800554 }
mtklein114c3cd2015-01-15 10:15:02 -0800555 }
halcanary23b03c32015-01-30 09:58:58 -0800556 } else if (sk_exists(flag)) {
557 // assume that FLAGS_images[i] is a valid image if it is a file.
djsollen54416de2015-04-03 07:24:48 -0700558 push_src("image", "decode", new ImageSrc(flag)); // Decode entire image.
msarett438b2ad2015-04-09 12:43:10 -0700559 push_codec_srcs(flag);
msaretta5783ae2015-09-08 15:35:32 -0700560 push_brd_srcs(flag);
mtklein709d2c32015-01-15 08:30:25 -0800561 }
mtklein709d2c32015-01-15 08:30:25 -0800562 }
563}
564
kkinnunen3e980c32015-12-23 01:33:00 -0800565static void push_sink(const SkCommandLineConfig& config, Sink* s) {
rmistry0f515bd2015-12-22 10:22:26 -0800566 SkAutoTDelete<Sink> sink(s);
kkinnunen3e980c32015-12-23 01:33:00 -0800567
mtkleine0effd62015-07-29 06:37:28 -0700568 // Try a simple Src as a canary. If it fails, skip this sink.
mtklein748ca3b2015-01-15 10:56:12 -0800569 struct : public Src {
mtkleine0effd62015-07-29 06:37:28 -0700570 Error draw(SkCanvas* c) const override {
571 c->drawRect(SkRect::MakeWH(1,1), SkPaint());
572 return "";
573 }
mtklein36352bf2015-03-25 18:17:31 -0700574 SkISize size() const override { return SkISize::Make(16, 16); }
mtkleine0effd62015-07-29 06:37:28 -0700575 Name name() const override { return "justOneRect"; }
576 } justOneRect;
mtklein114c3cd2015-01-15 10:15:02 -0800577
mtklein748ca3b2015-01-15 10:56:12 -0800578 SkBitmap bitmap;
579 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800580 SkString log;
mtkleine0effd62015-07-29 06:37:28 -0700581 Error err = sink->draw(justOneRect, &bitmap, &stream, &log);
mtklein4089ef72015-03-05 08:40:28 -0800582 if (err.isFatal()) {
kkinnunen3e980c32015-12-23 01:33:00 -0800583 SkDebugf("Could not run %s: %s\n", config.getTag().c_str(), err.c_str());
mtklein05641a52015-04-21 10:49:13 -0700584 exit(1);
mtklein114c3cd2015-01-15 10:15:02 -0800585 }
586
mtkleine0effd62015-07-29 06:37:28 -0700587 TaggedSink& ts = gSinks.push_back();
mtklein748ca3b2015-01-15 10:56:12 -0800588 ts.reset(sink.detach());
kkinnunen3e980c32015-12-23 01:33:00 -0800589 ts.tag = config.getTag();
mtklein748ca3b2015-01-15 10:56:12 -0800590}
591
592static bool gpu_supported() {
593#if SK_SUPPORT_GPU
594 return FLAGS_gpu;
595#else
596 return false;
597#endif
598}
kkinnunen9ebc3f02015-12-21 23:48:13 -0800599
kkinnunen3e980c32015-12-23 01:33:00 -0800600static Sink* create_sink(const SkCommandLineConfig* config) {
601#if SK_SUPPORT_GPU
602 if (gpu_supported()) {
603 if (const SkCommandLineConfigGpu* gpuConfig = config->asConfigGpu()) {
604 GrContextFactory::GLContextType contextType = gpuConfig->getContextType();
605 GrContextFactory::GLContextOptions contextOptions =
606 GrContextFactory::kNone_GLContextOptions;
607 if (gpuConfig->getUseNVPR()) {
608 contextOptions = static_cast<GrContextFactory::GLContextOptions>(
609 contextOptions | GrContextFactory::kEnableNVPR_GLContextOptions);
610 }
611 GrContextFactory testFactory;
612 if (!testFactory.get(contextType, contextOptions)) {
613 SkDebugf("WARNING: can not create GPU context for config '%s'. "
614 "GM tests will be skipped.\n", gpuConfig->getTag().c_str());
615 return nullptr;
616 }
617 return new GPUSink(contextType, contextOptions, gpuConfig->getSamples(),
618 gpuConfig->getUseDIText(), FLAGS_gpu_threading);
619 }
620 }
621#endif
622
623#define SINK(t, sink, ...) if (config->getBackend().equals(t)) { return new sink(__VA_ARGS__); }
624
tomhudsoneebc39a2015-02-23 12:18:05 -0800625#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
626 SINK("hwui", HWUISink);
627#endif
628
mtklein748ca3b2015-01-15 10:56:12 -0800629 if (FLAGS_cpu) {
630 SINK("565", RasterSink, kRGB_565_SkColorType);
631 SINK("8888", RasterSink, kN32_SkColorType);
halcanaryc11c62f2015-09-28 11:51:54 -0700632 SINK("pdf", PDFSink, "Pdfium");
633 SINK("pdf_poppler", PDFSink, "Poppler");
mtklein9c3f17d2015-01-28 11:35:18 -0800634 SINK("skp", SKPSink);
mtklein8a4527e2015-01-31 20:00:58 -0800635 SINK("svg", SVGSink);
636 SINK("null", NullSink);
halcanary47ef4d52015-03-03 09:13:09 -0800637 SINK("xps", XPSSink);
mtklein748ca3b2015-01-15 10:56:12 -0800638 }
639#undef SINK
halcanary96fcdcc2015-08-27 07:41:13 -0700640 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800641}
642
kkinnunen3e980c32015-12-23 01:33:00 -0800643static Sink* create_via(const SkString& tag, Sink* wrapped) {
644#define VIA(t, via, ...) if (tag.equals(t)) { return new via(__VA_ARGS__); }
halcanary96fcdcc2015-08-27 07:41:13 -0700645 VIA("twice", ViaTwice, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700646 VIA("serialize", ViaSerialization, wrapped);
mtklein4a34ecb2016-01-08 10:19:35 -0800647 VIA("pic", ViaPicture, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700648 VIA("2ndpic", ViaSecondPicture, wrapped);
mtkleind31c13d2015-05-05 12:59:56 -0700649 VIA("sp", ViaSingletonPictures, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700650 VIA("tiles", ViaTiles, 256, 256, nullptr, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800651 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
mtklein2e2ea382015-10-16 10:29:41 -0700652 VIA("remote", ViaRemote, false, wrapped);
653 VIA("remote_cache", ViaRemote, true, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800654
mtkleind603b222015-02-17 11:13:33 -0800655 if (FLAGS_matrix.count() == 4) {
mtklein748ca3b2015-01-15 10:56:12 -0800656 SkMatrix m;
mtkleind603b222015-02-17 11:13:33 -0800657 m.reset();
658 m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
659 m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
660 m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
661 m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
662 VIA("matrix", ViaMatrix, m, wrapped);
663 VIA("upright", ViaUpright, m, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800664 }
tomhudson64de1e12015-03-05 08:01:07 -0800665
666#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
667 VIA("androidsdk", ViaAndroidSDK, wrapped);
668#endif
669
mtklein748ca3b2015-01-15 10:56:12 -0800670#undef VIA
halcanary96fcdcc2015-08-27 07:41:13 -0700671 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800672}
673
mtklein748ca3b2015-01-15 10:56:12 -0800674static void gather_sinks() {
kkinnunen3e980c32015-12-23 01:33:00 -0800675 SkCommandLineConfigArray configs;
676 ParseConfigs(FLAGS_config, &configs);
677 for (int i = 0; i < configs.count(); i++) {
678 const SkCommandLineConfig& config = *configs[i];
679 Sink* sink = create_sink(&config);
680 if (sink == nullptr) {
681 SkDebugf("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
682 config.getTag().c_str());
683 continue;
684 }
mtklein748ca3b2015-01-15 10:56:12 -0800685
kkinnunen3e980c32015-12-23 01:33:00 -0800686 const SkTArray<SkString>& parts = config.getViaParts();
687 for (int j = parts.count(); j-- > 0;) {
688 const SkString& part = parts[j];
689 Sink* next = create_via(part, sink);
halcanary96fcdcc2015-08-27 07:41:13 -0700690 if (next == nullptr) {
kkinnunen3e980c32015-12-23 01:33:00 -0800691 SkDebugf("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
692 part.c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800693 delete sink;
halcanary96fcdcc2015-08-27 07:41:13 -0700694 sink = nullptr;
mtklein748ca3b2015-01-15 10:56:12 -0800695 break;
696 }
697 sink = next;
698 }
699 if (sink) {
700 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800701 }
702 }
703}
mtklein709d2c32015-01-15 08:30:25 -0800704
mtkleina5114d72015-08-24 13:27:01 -0700705static bool dump_png(SkBitmap bitmap, const char* path, const char* md5) {
706 const int w = bitmap.width(),
707 h = bitmap.height();
708
709 // First get the bitmap into N32 color format. The next step will work only there.
710 if (bitmap.colorType() != kN32_SkColorType) {
711 SkBitmap n32;
712 if (!bitmap.copyTo(&n32, kN32_SkColorType)) {
713 return false;
714 }
715 bitmap = n32;
716 }
717
718 // Convert our N32 bitmap into unpremul RGBA for libpng.
719 SkAutoTMalloc<uint32_t> rgba(w*h);
720 if (!bitmap.readPixels(SkImageInfo::Make(w,h, kRGBA_8888_SkColorType, kUnpremul_SkAlphaType),
721 rgba, 4*w, 0,0)) {
722 return false;
723 }
724
725 // We don't need bitmap anymore. Might as well drop our ref.
mtkleinfccb77d2015-08-24 14:13:29 -0700726 bitmap.reset();
mtkleina5114d72015-08-24 13:27:01 -0700727
mtkleinc64137c2015-08-25 10:56:08 -0700728 FILE* f = fopen(path, "wb");
mtkleina5114d72015-08-24 13:27:01 -0700729 if (!f) { return false; }
730
731 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
732 if (!png) {
733 fclose(f);
734 return false;
735 }
736
737 png_infop info = png_create_info_struct(png);
738 if (!info) {
739 png_destroy_write_struct(&png, &info);
740 fclose(f);
741 return false;
742 }
743
mtkleind69ece62015-09-10 10:37:44 -0700744 SkString description;
745 description.append("Key: ");
746 for (int i = 0; i < FLAGS_key.count(); i++) {
747 description.appendf("%s ", FLAGS_key[i]);
748 }
749 description.append("Properties: ");
750 for (int i = 0; i < FLAGS_properties.count(); i++) {
751 description.appendf("%s ", FLAGS_properties[i]);
752 }
753 description.appendf("MD5: %s", md5);
754
mtkleina5114d72015-08-24 13:27:01 -0700755 png_text text[2];
756 text[0].key = (png_charp)"Author";
757 text[0].text = (png_charp)"DM dump_png()";
758 text[0].compression = PNG_TEXT_COMPRESSION_NONE;
759 text[1].key = (png_charp)"Description";
mtkleind69ece62015-09-10 10:37:44 -0700760 text[1].text = (png_charp)description.c_str();
mtkleina5114d72015-08-24 13:27:01 -0700761 text[1].compression = PNG_TEXT_COMPRESSION_NONE;
762 png_set_text(png, info, text, 2);
763
764 png_init_io(png, f);
765 png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8,
766 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
767 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
768 png_write_info(png, info);
769 for (int j = 0; j < h; j++) {
770 png_bytep row = (png_bytep)(rgba.get() + w*j);
771 png_write_rows(png, &row, 1);
772 }
773 png_write_end(png, info);
774
775 png_destroy_write_struct(&png, &info);
776 fclose(f);
777 return true;
778}
779
mtkleina2ef6422015-01-15 13:44:22 -0800780static bool match(const char* needle, const char* haystack) {
halcanary96fcdcc2015-08-27 07:41:13 -0700781 return 0 == strcmp("_", needle) || nullptr != strstr(haystack, needle);
mtkleina2ef6422015-01-15 13:44:22 -0800782}
783
djsollen54416de2015-04-03 07:24:48 -0700784static ImplicitString is_blacklisted(const char* sink, const char* src,
785 const char* srcOptions, const char* name) {
mtklein0d243ff2015-04-03 07:51:00 -0700786 for (int i = 0; i < FLAGS_blacklist.count() - 3; i += 4) {
mtkleina2ef6422015-01-15 13:44:22 -0800787 if (match(FLAGS_blacklist[i+0], sink) &&
djsollen54416de2015-04-03 07:24:48 -0700788 match(FLAGS_blacklist[i+1], src) &&
789 match(FLAGS_blacklist[i+2], srcOptions) &&
790 match(FLAGS_blacklist[i+3], name)) {
791 return SkStringPrintf("%s %s %s %s",
792 FLAGS_blacklist[i+0], FLAGS_blacklist[i+1],
793 FLAGS_blacklist[i+2], FLAGS_blacklist[i+3]);
mtkleina2ef6422015-01-15 13:44:22 -0800794 }
795 }
796 return "";
797}
798
mtkleincd50bca2016-01-05 06:20:20 -0800799// Even when a Task Sink reports to be non-threadsafe (e.g. GPU), we know things like
800// .png encoding are definitely thread safe. This lets us offload that work to CPU threads.
801static SkTaskGroup gDefinitelyThreadSafeWork;
802
mtklein748ca3b2015-01-15 10:56:12 -0800803// The finest-grained unit of work we can run: draw a single Src into a single Sink,
804// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
805struct Task {
mtkleine0effd62015-07-29 06:37:28 -0700806 Task(const TaggedSrc& src, const TaggedSink& sink) : src(src), sink(sink) {}
807 const TaggedSrc& src;
808 const TaggedSink& sink;
mtklein748ca3b2015-01-15 10:56:12 -0800809
810 static void Run(Task* task) {
mtkleina2ef6422015-01-15 13:44:22 -0800811 SkString name = task->src->name();
mtkleine0effd62015-07-29 06:37:28 -0700812
813 // We'll skip drawing this Src/Sink pair if:
814 // - the Src vetoes the Sink;
815 // - this Src / Sink combination is on the blacklist;
816 // - it's a dry run.
mtklein99cab4e2015-07-31 06:43:04 -0700817 SkString note(task->src->veto(task->sink->flags()) ? " (veto)" : "");
kkinnunen3e980c32015-12-23 01:33:00 -0800818 SkString whyBlacklisted = is_blacklisted(task->sink.tag.c_str(), task->src.tag.c_str(),
msarett9e707a02015-09-01 14:57:57 -0700819 task->src.options.c_str(), name.c_str());
mtkleinb37cb412015-03-18 05:27:14 -0700820 if (!whyBlacklisted.isEmpty()) {
821 note.appendf(" (--blacklist %s)", whyBlacklisted.c_str());
822 }
mtkleine0effd62015-07-29 06:37:28 -0700823
mtkleinb9eb4ac2015-02-02 18:26:03 -0800824 SkString log;
mtkleinf27f08b2015-11-03 06:54:24 -0800825 auto timerStart = now_ms();
mtkleine0effd62015-07-29 06:37:28 -0700826 if (!FLAGS_dryRun && note.isEmpty()) {
mtklein748ca3b2015-01-15 10:56:12 -0800827 SkBitmap bitmap;
828 SkDynamicMemoryWStream stream;
bsalomon821e10e2015-06-04 14:15:33 -0700829 if (FLAGS_pre_log) {
kkinnunen3e980c32015-12-23 01:33:00 -0800830 SkDebugf("\nRunning %s->%s", name.c_str(), task->sink.tag.c_str());
bsalomon821e10e2015-06-04 14:15:33 -0700831 }
kkinnunen3e980c32015-12-23 01:33:00 -0800832 start(task->sink.tag.c_str(), task->src.tag, task->src.options, name.c_str());
mtkleinb9eb4ac2015-02-02 18:26:03 -0800833 Error err = task->sink->draw(*task->src, &bitmap, &stream, &log);
mtklein748ca3b2015-01-15 10:56:12 -0800834 if (!err.isEmpty()) {
mtklein4089ef72015-03-05 08:40:28 -0800835 if (err.isFatal()) {
djsollen54416de2015-04-03 07:24:48 -0700836 fail(SkStringPrintf("%s %s %s %s: %s",
kkinnunen3e980c32015-12-23 01:33:00 -0800837 task->sink.tag.c_str(),
msarett9e707a02015-09-01 14:57:57 -0700838 task->src.tag.c_str(),
839 task->src.options.c_str(),
mtklein4089ef72015-03-05 08:40:28 -0800840 name.c_str(),
841 err.c_str()));
mtkleinb37cb412015-03-18 05:27:14 -0700842 } else {
843 note.appendf(" (skipped: %s)", err.c_str());
mtkleinba6ada72016-01-21 09:39:35 -0800844 auto elapsed = now_ms() - timerStart;
845 done(elapsed, task->sink.tag.c_str(), task->src.tag, task->src.options,
846 name, note, log);
847 return;
mtklein4089ef72015-03-05 08:40:28 -0800848 }
mtklein748ca3b2015-01-15 10:56:12 -0800849 }
mtklein62bd1a62015-01-27 14:46:26 -0800850
mtkleincd50bca2016-01-05 06:20:20 -0800851 // We're likely switching threads here, so we must capture by value, [=] or [foo,bar].
852 SkStreamAsset* data = stream.detachAsStream();
853 gDefinitelyThreadSafeWork.add([task,name,bitmap,data]{
854 SkAutoTDelete<SkStreamAsset> ownedData(data);
855
856 // Why doesn't the copy constructor do this when we have pre-locked pixels?
857 bitmap.lockPixels();
858
859 SkString md5;
860 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
861 SkMD5 hash;
862 if (data->getLength()) {
863 hash.writeStream(data, data->getLength());
864 data->rewind();
mtklein38408462015-07-08 07:25:27 -0700865 } else {
mtkleincd50bca2016-01-05 06:20:20 -0800866 // If we're BGRA (Linux, Windows), swizzle over to RGBA (Mac, Android).
867 // This helps eliminate multiple 0-pixel-diff hashes on gold.skia.org.
868 // (Android's general slow speed breaks the tie arbitrarily in RGBA's favor.)
869 // We might consider promoting 565 to RGBA too.
870 if (bitmap.colorType() == kBGRA_8888_SkColorType) {
871 SkBitmap swizzle;
872 SkAssertResult(bitmap.copyTo(&swizzle, kRGBA_8888_SkColorType));
873 hash.write(swizzle.getPixels(), swizzle.getSize());
874 } else {
875 hash.write(bitmap.getPixels(), bitmap.getSize());
876 }
877 }
878 SkMD5::Digest digest;
879 hash.finish(digest);
880 for (int i = 0; i < 16; i++) {
881 md5.appendf("%02x", digest.data[i]);
mtklein38408462015-07-08 07:25:27 -0700882 }
mtklein62bd1a62015-01-27 14:46:26 -0800883 }
mtklein62bd1a62015-01-27 14:46:26 -0800884
mtkleincd50bca2016-01-05 06:20:20 -0800885 if (!FLAGS_readPath.isEmpty() &&
886 !gGold.contains(Gold(task->sink.tag.c_str(), task->src.tag.c_str(),
887 task->src.options.c_str(), name, md5))) {
888 fail(SkStringPrintf("%s not found for %s %s %s %s in %s",
889 md5.c_str(),
890 task->sink.tag.c_str(),
891 task->src.tag.c_str(),
892 task->src.options.c_str(),
893 name.c_str(),
894 FLAGS_readPath[0]));
mtklein748ca3b2015-01-15 10:56:12 -0800895 }
mtkleincd50bca2016-01-05 06:20:20 -0800896
897 if (!FLAGS_writePath.isEmpty()) {
898 const char* ext = task->sink->fileExtension();
899 if (data->getLength()) {
900 WriteToDisk(*task, md5, ext, data, data->getLength(), nullptr);
901 SkASSERT(bitmap.drawsNothing());
902 } else if (!bitmap.drawsNothing()) {
903 WriteToDisk(*task, md5, ext, nullptr, 0, &bitmap);
904 }
905 }
906 });
mtklein748ca3b2015-01-15 10:56:12 -0800907 }
mtkleinba6ada72016-01-21 09:39:35 -0800908 auto elapsed = now_ms() - timerStart;
909 done(elapsed, task->sink.tag.c_str(), task->src.tag.c_str(), task->src.options.c_str(),
mtkleinf27f08b2015-11-03 06:54:24 -0800910 name, note, log);
mtklein748ca3b2015-01-15 10:56:12 -0800911 }
912
913 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -0800914 SkString md5,
915 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -0800916 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -0800917 const SkBitmap* bitmap) {
mtklein748ca3b2015-01-15 10:56:12 -0800918 JsonWriter::BitmapResult result;
djsollen54416de2015-04-03 07:24:48 -0700919 result.name = task.src->name();
kkinnunen3e980c32015-12-23 01:33:00 -0800920 result.config = task.sink.tag.c_str();
djsollen54416de2015-04-03 07:24:48 -0700921 result.sourceType = task.src.tag;
922 result.sourceOptions = task.src.options;
923 result.ext = ext;
924 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -0800925 JsonWriter::AddBitmapResult(result);
926
mtkleinb0531a72015-04-07 13:38:48 -0700927 // If an MD5 is uninteresting, we want it noted in the JSON file,
928 // but don't want to dump it out as a .png (or whatever ext is).
929 if (gUninterestingHashes.contains(md5)) {
930 return;
931 }
932
mtklein748ca3b2015-01-15 10:56:12 -0800933 const char* dir = FLAGS_writePath[0];
934 if (0 == strcmp(dir, "@")) { // Needed for iOS.
935 dir = FLAGS_resourcePath[0];
936 }
937 sk_mkdir(dir);
938
939 SkString path;
940 if (FLAGS_nameByHash) {
941 path = SkOSPath::Join(dir, result.md5.c_str());
942 path.append(".");
943 path.append(ext);
944 if (sk_exists(path.c_str())) {
945 return; // Content-addressed. If it exists already, we're done.
946 }
947 } else {
kkinnunen3e980c32015-12-23 01:33:00 -0800948 path = SkOSPath::Join(dir, task.sink.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800949 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -0700950 path = SkOSPath::Join(path.c_str(), task.src.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800951 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -0700952 if (strcmp(task.src.options.c_str(), "") != 0) {
953 path = SkOSPath::Join(path.c_str(), task.src.options.c_str());
djsollen54416de2015-04-03 07:24:48 -0700954 sk_mkdir(path.c_str());
955 }
mtklein748ca3b2015-01-15 10:56:12 -0800956 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
957 path.append(".");
958 path.append(ext);
959 }
960
mtklein748ca3b2015-01-15 10:56:12 -0800961 if (bitmap) {
mtkleina5114d72015-08-24 13:27:01 -0700962 if (!dump_png(*bitmap, path.c_str(), result.md5.c_str())) {
mtklein748ca3b2015-01-15 10:56:12 -0800963 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
964 return;
965 }
966 } else {
mtkleina5114d72015-08-24 13:27:01 -0700967 SkFILEWStream file(path.c_str());
968 if (!file.isValid()) {
969 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
970 return;
971 }
mtklein748ca3b2015-01-15 10:56:12 -0800972 if (!file.writeStream(data, len)) {
973 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
974 return;
975 }
976 }
977 }
978};
979
980// Run all tasks in the same enclave serially on the same thread.
981// They can't possibly run concurrently with each other.
982static void run_enclave(SkTArray<Task>* tasks) {
983 for (int i = 0; i < tasks->count(); i++) {
984 Task::Run(tasks->begin() + i);
985 }
986}
987
988/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
989
990// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
991
mtklein55e88b22015-01-21 15:50:13 -0800992static SkTDArray<skiatest::Test> gThreadedTests, gGPUTests;
mtklein748ca3b2015-01-15 10:56:12 -0800993
994static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -0800995 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -0800996 return;
997 }
mtklein6393c062015-04-27 08:45:01 -0700998 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r; r = r->next()) {
999 if (!in_shard()) {
1000 continue;
1001 }
halcanary87f3ba42015-01-20 09:30:20 -08001002 // Despite its name, factory() is returning a reference to
1003 // link-time static const POD data.
1004 const skiatest::Test& test = r->factory();
1005 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -08001006 continue;
1007 }
halcanary87f3ba42015-01-20 09:30:20 -08001008 if (test.needsGpu && gpu_supported()) {
mtklein55e88b22015-01-21 15:50:13 -08001009 (FLAGS_gpu_threading ? gThreadedTests : gGPUTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -08001010 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein55e88b22015-01-21 15:50:13 -08001011 gThreadedTests.push(test);
mtklein82d28432015-01-15 12:46:02 -08001012 }
mtklein748ca3b2015-01-15 10:56:12 -08001013 }
1014}
1015
halcanary87f3ba42015-01-20 09:30:20 -08001016static void run_test(skiatest::Test* test) {
1017 struct : public skiatest::Reporter {
mtklein36352bf2015-03-25 18:17:31 -07001018 void reportFailed(const skiatest::Failure& failure) override {
halcanary87f3ba42015-01-20 09:30:20 -08001019 fail(failure.toString());
1020 JsonWriter::AddTestFailure(failure);
1021 }
mtklein36352bf2015-03-25 18:17:31 -07001022 bool allowExtendedTest() const override {
halcanary87f3ba42015-01-20 09:30:20 -08001023 return FLAGS_pathOpsExtended;
1024 }
mtklein36352bf2015-03-25 18:17:31 -07001025 bool verbose() const override { return FLAGS_veryVerbose; }
halcanary87f3ba42015-01-20 09:30:20 -08001026 } reporter;
djsollen824996a2015-06-12 12:06:22 -07001027
1028 SkString note;
1029 SkString whyBlacklisted = is_blacklisted("_", "tests", "_", test->name);
1030 if (!whyBlacklisted.isEmpty()) {
1031 note.appendf(" (--blacklist %s)", whyBlacklisted.c_str());
1032 }
1033
mtkleinf27f08b2015-11-03 06:54:24 -08001034 auto timerStart = now_ms();
djsollen824996a2015-06-12 12:06:22 -07001035 if (!FLAGS_dryRun && whyBlacklisted.isEmpty()) {
djsollen54416de2015-04-03 07:24:48 -07001036 start("unit", "test", "", test->name);
mtklein55e88b22015-01-21 15:50:13 -08001037 GrContextFactory factory;
bsalomon821e10e2015-06-04 14:15:33 -07001038 if (FLAGS_pre_log) {
1039 SkDebugf("\nRunning test %s", test->name);
1040 }
mtklein55e88b22015-01-21 15:50:13 -08001041 test->proc(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -08001042 }
mtkleinf27f08b2015-11-03 06:54:24 -08001043 done(now_ms()-timerStart, "unit", "test", "", test->name, note, "");
mtklein748ca3b2015-01-15 10:56:12 -08001044}
1045
1046/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1047
mtklein55e88b22015-01-21 15:50:13 -08001048// If we're isolating all GPU-bound work to one thread (the default), this function runs all that.
1049static void run_enclave_and_gpu_tests(SkTArray<Task>* tasks) {
1050 run_enclave(tasks);
1051 for (int i = 0; i < gGPUTests.count(); i++) {
1052 run_test(&gGPUTests[i]);
1053 }
1054}
1055
mtkleinde6fc2b2015-03-12 06:28:54 -07001056// Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung.
1057// This prints something every once in a while so that it knows we're still working.
mtklein2e1c47e2015-03-12 07:16:56 -07001058static void start_keepalive() {
1059 struct Loop {
1060 static void forever(void*) {
1061 for (;;) {
1062 static const int kSec = 300;
1063 #if defined(SK_BUILD_FOR_WIN)
1064 Sleep(kSec * 1000);
1065 #else
1066 sleep(kSec);
1067 #endif
mtkleinb37cb412015-03-18 05:27:14 -07001068 SkString running;
1069 {
mtkleinbc4e0032015-12-09 08:37:35 -08001070 SkAutoMutexAcquire lock(gRunningAndTallyMutex);
mtkleinb37cb412015-03-18 05:27:14 -07001071 for (int i = 0; i < gRunning.count(); i++) {
1072 running.appendf("\n\t%s", gRunning[i].c_str());
1073 }
1074 }
1075 SkDebugf("\nCurrently running:%s\n", running.c_str());
mtklein2e1c47e2015-03-12 07:16:56 -07001076 }
1077 }
1078 };
1079 static SkThread* intentionallyLeaked = new SkThread(Loop::forever);
1080 intentionallyLeaked->start();
mtkleinde6fc2b2015-03-12 06:28:54 -07001081}
1082
caryclark83ca6282015-06-10 09:31:09 -07001083#define PORTABLE_FONT_PREFIX "Toy Liberation "
1084
1085static SkTypeface* create_from_name(const char familyName[], SkTypeface::Style style) {
1086 if (familyName && strlen(familyName) > sizeof(PORTABLE_FONT_PREFIX)
1087 && !strncmp(familyName, PORTABLE_FONT_PREFIX, sizeof(PORTABLE_FONT_PREFIX) - 1)) {
caryclark1818acb2015-07-24 12:09:25 -07001088 return sk_tool_utils::create_portable_typeface(familyName, style);
caryclark83ca6282015-06-10 09:31:09 -07001089 }
halcanary96fcdcc2015-08-27 07:41:13 -07001090 return nullptr;
caryclark83ca6282015-06-10 09:31:09 -07001091}
1092
1093#undef PORTABLE_FONT_PREFIX
1094
1095extern SkTypeface* (*gCreateTypefaceDelegate)(const char [], SkTypeface::Style );
1096
jcgregorio3b27ade2014-11-13 08:06:40 -08001097int dm_main();
caryclark17f0b6d2014-07-22 10:15:34 -07001098int dm_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -08001099 SetupCrashHandler();
mtklein5286f022016-01-22 08:18:14 -08001100 JsonWriter::DumpJson(); // It's handy for the bots to assume this is ~never missing.
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +00001101 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -07001102 SkTaskGroup::Enabler enabled(FLAGS_threads);
caryclark83ca6282015-06-10 09:31:09 -07001103 gCreateTypefaceDelegate = &create_from_name;
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +00001104
mtklein2e1c47e2015-03-12 07:16:56 -07001105 start_keepalive();
mtkleinde6fc2b2015-03-12 06:28:54 -07001106
mtklein62bd1a62015-01-27 14:46:26 -08001107 gather_gold();
borenet09ed4802015-04-03 14:15:33 -07001108 gather_uninteresting_hashes();
mtklein62bd1a62015-01-27 14:46:26 -08001109
mtklein748ca3b2015-01-15 10:56:12 -08001110 gather_srcs();
1111 gather_sinks();
1112 gather_tests();
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +00001113
mtklein55e88b22015-01-21 15:50:13 -08001114 gPending = gSrcs.count() * gSinks.count() + gThreadedTests.count() + gGPUTests.count();
mtklein748ca3b2015-01-15 10:56:12 -08001115 SkDebugf("%d srcs * %d sinks + %d tests == %d tasks\n",
mtklein55e88b22015-01-21 15:50:13 -08001116 gSrcs.count(), gSinks.count(), gThreadedTests.count() + gGPUTests.count(), gPending);
mtklein748ca3b2015-01-15 10:56:12 -08001117
1118 // We try to exploit as much parallelism as is safe. Most Src/Sink pairs run on any thread,
1119 // but Sinks that identify as part of a particular enclave run serially on a single thread.
mtklein82d28432015-01-15 12:46:02 -08001120 // CPU tests run on any thread. GPU tests depend on --gpu_threading.
mtklein748ca3b2015-01-15 10:56:12 -08001121 SkTArray<Task> enclaves[kNumEnclaves];
1122 for (int j = 0; j < gSinks.count(); j++) {
1123 SkTArray<Task>& tasks = enclaves[gSinks[j]->enclave()];
1124 for (int i = 0; i < gSrcs.count(); i++) {
1125 tasks.push_back(Task(gSrcs[i], gSinks[j]));
1126 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001127 }
1128
mtklein748ca3b2015-01-15 10:56:12 -08001129 SkTaskGroup tg;
mtklein279c7862016-01-04 19:13:19 -08001130 tg.batch(gThreadedTests.count(), [](int i){ run_test(&gThreadedTests[i]); });
mtklein55e88b22015-01-21 15:50:13 -08001131 for (int i = 0; i < kNumEnclaves; i++) {
herb30da5f72015-12-10 14:12:33 -08001132 SkTArray<Task>* currentEnclave = &enclaves[i];
mtklein55e88b22015-01-21 15:50:13 -08001133 switch(i) {
1134 case kAnyThread_Enclave:
mtklein279c7862016-01-04 19:13:19 -08001135 tg.batch(currentEnclave->count(),
1136 [currentEnclave](int j) { Task::Run(&(*currentEnclave)[j]); });
mtklein55e88b22015-01-21 15:50:13 -08001137 break;
1138 case kGPU_Enclave:
herb30da5f72015-12-10 14:12:33 -08001139 tg.add([currentEnclave](){ run_enclave_and_gpu_tests(currentEnclave); });
mtklein55e88b22015-01-21 15:50:13 -08001140 break;
1141 default:
herb30da5f72015-12-10 14:12:33 -08001142 tg.add([currentEnclave](){ run_enclave(currentEnclave); });
mtklein55e88b22015-01-21 15:50:13 -08001143 break;
mtklein82d28432015-01-15 12:46:02 -08001144 }
mtklein55e88b22015-01-21 15:50:13 -08001145 }
mtklein748ca3b2015-01-15 10:56:12 -08001146 tg.wait();
mtkleincd50bca2016-01-05 06:20:20 -08001147 gDefinitelyThreadSafeWork.wait();
1148
mtklein748ca3b2015-01-15 10:56:12 -08001149 // At this point we're back in single-threaded land.
caryclarkf53ce802015-06-15 06:48:30 -07001150 sk_tool_utils::release_portable_typefaces();
mtklein@google.comd36522d2013-10-16 13:02:15 +00001151
mtkleinbc4e0032015-12-09 08:37:35 -08001152 if (FLAGS_verbose && gNoteTally.count() > 0) {
1153 SkDebugf("\nNote tally:\n");
1154 gNoteTally.foreach([](const SkString& note, int* tally) {
1155 SkDebugf("%dx\t%s\n", *tally, note.c_str());
1156 });
1157 }
1158
mtklein2f64eec2015-01-15 14:20:41 -08001159 SkDebugf("\n");
mtklein748ca3b2015-01-15 10:56:12 -08001160 if (gFailures.count() > 0) {
1161 SkDebugf("Failures:\n");
1162 for (int i = 0; i < gFailures.count(); i++) {
mtklein9dc09102015-01-15 15:47:33 -08001163 SkDebugf("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001164 }
1165 SkDebugf("%d failures\n", gFailures.count());
1166 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001167 }
mtklein748ca3b2015-01-15 10:56:12 -08001168 if (gPending > 0) {
1169 SkDebugf("Hrm, we didn't seem to run everything we intended to! Please file a bug.\n");
1170 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001171 }
halcanary7a14b312015-10-01 07:28:13 -07001172 #ifdef SK_PDF_IMAGE_STATS
1173 SkPDFImageDumpStats();
1174 #endif // SK_PDF_IMAGE_STATS
mtklein748ca3b2015-01-15 10:56:12 -08001175 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +00001176}
jcgregorio3b27ade2014-11-13 08:06:40 -08001177
kkinnunen179a8f52015-11-20 13:32:24 -08001178// TODO: currently many GPU tests are declared outside SK_SUPPORT_GPU guards.
1179// Thus we export the empty RunWithGPUTestContexts when SK_SUPPORT_GPU=0.
1180namespace skiatest {
1181namespace {
1182typedef void(*TestWithGrContext)(skiatest::Reporter*, GrContext*);
1183typedef void(*TestWithGrContextAndGLContext)(skiatest::Reporter*, GrContext*, SkGLContext*);
1184#if SK_SUPPORT_GPU
1185template<typename T>
kkinnunen34058002016-01-06 23:49:30 -08001186void call_test(T test, skiatest::Reporter* reporter, const GrContextFactory::ContextInfo& context);
kkinnunen179a8f52015-11-20 13:32:24 -08001187template<>
1188void call_test(TestWithGrContext test, skiatest::Reporter* reporter,
kkinnunen34058002016-01-06 23:49:30 -08001189 const GrContextFactory::ContextInfo& context) {
1190 test(reporter, context.fGrContext);
kkinnunen179a8f52015-11-20 13:32:24 -08001191}
1192template<>
1193void call_test(TestWithGrContextAndGLContext test, skiatest::Reporter* reporter,
kkinnunen34058002016-01-06 23:49:30 -08001194 const GrContextFactory::ContextInfo& context) {
1195 test(reporter, context.fGrContext, context.fGLContext);
kkinnunen179a8f52015-11-20 13:32:24 -08001196}
1197#endif
1198} // namespace
1199
kkinnunen179a8f52015-11-20 13:32:24 -08001200template<typename T>
1201void RunWithGPUTestContexts(T test, GPUTestContexts testContexts, Reporter* reporter,
1202 GrContextFactory* factory) {
1203#if SK_SUPPORT_GPU
kkinnunen3e980c32015-12-23 01:33:00 -08001204 // Iterate over context types, except use "native" instead of explicitly trying OpenGL and
1205 // OpenGL ES. Do not use GLES on desktop, since tests do not account for not fixing
1206 // http://skbug.com/2809
1207 GrContextFactory::GLContextType contextTypes[] = {
1208 GrContextFactory::kNative_GLContextType,
1209#if SK_ANGLE
1210#ifdef SK_BUILD_FOR_WIN
1211 GrContextFactory::kANGLE_GLContextType,
1212#endif
1213 GrContextFactory::kANGLE_GL_GLContextType,
1214#endif
1215#if SK_COMMAND_BUFFER
1216 GrContextFactory::kCommandBuffer_GLContextType,
1217#endif
1218#if SK_MESA
1219 GrContextFactory::kMESA_GLContextType,
1220#endif
1221 GrContextFactory::kNull_GLContextType,
1222 GrContextFactory::kDebug_GLContextType,
1223 };
1224 static_assert(SK_ARRAY_COUNT(contextTypes) == GrContextFactory::kGLContextTypeCnt - 2,
1225 "Skipping unexpected GLContextType for GPU tests");
1226
1227 for (auto& contextType : contextTypes) {
kkinnunen179a8f52015-11-20 13:32:24 -08001228 int contextSelector = kNone_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001229 if (GrContextFactory::IsRenderingGLContext(contextType)) {
kkinnunen179a8f52015-11-20 13:32:24 -08001230 contextSelector |= kAllRendering_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001231 } else if (contextType == GrContextFactory::kNative_GLContextType) {
kkinnunen179a8f52015-11-20 13:32:24 -08001232 contextSelector |= kNative_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001233 } else if (contextType == GrContextFactory::kNull_GLContextType) {
kkinnunen179a8f52015-11-20 13:32:24 -08001234 contextSelector |= kNull_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001235 } else if (contextType == GrContextFactory::kDebug_GLContextType) {
kkinnunen55eeae92015-12-10 23:19:29 -08001236 contextSelector |= kDebug_GPUTestContexts;
kkinnunen179a8f52015-11-20 13:32:24 -08001237 }
1238 if ((testContexts & contextSelector) == 0) {
1239 continue;
1240 }
kkinnunen34058002016-01-06 23:49:30 -08001241 GrContextFactory::ContextInfo context = factory->getContextInfo(contextType);
1242 if (context.fGrContext) {
kkinnunen5219fd92015-12-10 06:28:13 -08001243 call_test(test, reporter, context);
1244 }
kkinnunen34058002016-01-06 23:49:30 -08001245 context = factory->getContextInfo(contextType,
1246 GrContextFactory::kEnableNVPR_GLContextOptions);
1247 if (context.fGrContext) {
kkinnunen179a8f52015-11-20 13:32:24 -08001248 call_test(test, reporter, context);
1249 }
1250 }
1251#endif
1252}
1253
1254template
1255void RunWithGPUTestContexts<TestWithGrContext>(TestWithGrContext test,
1256 GPUTestContexts testContexts,
1257 Reporter* reporter,
1258 GrContextFactory* factory);
1259template
1260void RunWithGPUTestContexts<TestWithGrContextAndGLContext>(TestWithGrContextAndGLContext test,
1261 GPUTestContexts testContexts,
1262 Reporter* reporter,
1263 GrContextFactory* factory);
1264} // namespace skiatest
1265
borenet48087572015-04-02 12:16:36 -07001266#if !defined(SK_BUILD_FOR_IOS)
jcgregorio3b27ade2014-11-13 08:06:40 -08001267int main(int argc, char** argv) {
1268 SkCommandLineFlags::Parse(argc, argv);
1269 return dm_main();
1270}
1271#endif