blob: 236fce870808b2b220c3378c2711d84e8a35d13f [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 "ProcStats.h"
13#include "SkBBHFactory.h"
mtklein62bd1a62015-01-27 14:46:26 -080014#include "SkChecksum.h"
scroggocc2feb12015-08-14 08:32:46 -070015#include "SkCodec.h"
caryclark17f0b6d2014-07-22 10:15:34 -070016#include "SkCommonFlags.h"
kkinnunen3e980c32015-12-23 01:33:00 -080017#include "SkCommonFlagsConfig.h"
caryclark83ca6282015-06-10 09:31:09 -070018#include "SkFontMgr.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +000019#include "SkForceLinking.h"
20#include "SkGraphics.h"
mtklein748ca3b2015-01-15 10:56:12 -080021#include "SkMD5.h"
mtklein1b249332015-07-07 12:21:21 -070022#include "SkMutex.h"
mtklein1d0f1642014-09-08 08:05:18 -070023#include "SkOSFile.h"
mtklein246ba3a2016-02-23 10:39:36 -080024#include "SkSpinlock.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 -070084
mtklein246ba3a2016-02-23 10:39:36 -080085// We use a spinlock to make locking this in a signal handler _somewhat_ safe.
mtkleinafae30a2016-02-24 12:28:32 -080086SK_DECLARE_STATIC_SPINLOCK(gMutex);
87static int32_t gPending;
mtkleinbc4e0032015-12-09 08:37:35 -080088static 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());
mtkleinafae30a2016-02-24 12:28:32 -080096 int pending;
mtkleinb37cb412015-03-18 05:27:14 -070097 {
mtkleinafae30a2016-02-24 12:28:32 -080098 SkAutoTAcquire<SkPODSpinlock> lock(gMutex);
mtkleinb37cb412015-03-18 05:27:14 -070099 for (int i = 0; i < gRunning.count(); i++) {
100 if (gRunning[i] == id) {
101 gRunning.removeShuffle(i);
102 break;
103 }
104 }
mtkleinbc4e0032015-12-09 08:37:35 -0800105 if (!note.isEmpty()) {
106 if (int* tally = gNoteTally.find(note)) {
107 *tally += 1;
108 } else {
109 gNoteTally.set(note, 1);
110 }
111 }
mtkleinafae30a2016-02-24 12:28:32 -0800112 pending = --gPending;
reed50bc0512015-05-19 14:13:31 -0700113 }
mtkleina17241b2015-01-23 05:48:00 -0800114 // We write our dm.json file every once in a while in case we crash.
115 // Notice this also handles the final dm.json when pending == 0.
116 if (pending % 500 == 0) {
117 JsonWriter::DumpJson();
118 }
mtklein@google.comd36522d2013-10-16 13:02:15 +0000119}
120
djsollen54416de2015-04-03 07:24:48 -0700121static void start(ImplicitString config, ImplicitString src,
122 ImplicitString srcOptions, ImplicitString name) {
123 SkString id = SkStringPrintf("%s %s %s %s", config.c_str(), src.c_str(),
124 srcOptions.c_str(), name.c_str());
mtkleinafae30a2016-02-24 12:28:32 -0800125 SkAutoTAcquire<SkPODSpinlock> lock(gMutex);
mtkleinb37cb412015-03-18 05:27:14 -0700126 gRunning.push_back(id);
127}
128
mtkleinafae30a2016-02-24 12:28:32 -0800129static void print_status() {
130 static SkMSec start_ms = SkTime::GetMSecs();
131
132 int curr = sk_tools::getCurrResidentSetSizeMB(),
133 peak = sk_tools::getMaxResidentSetSizeMB();
134 SkString elapsed = HumanizeMs(SkTime::GetMSecs() - start_ms);
135
136 SkAutoTAcquire<SkPODSpinlock> lock(gMutex);
137 SkDebugf("\n%s elapsed, %d active, %d queued, %dMB RAM, %dMB peak\n",
138 elapsed.c_str(), gRunning.count(), gPending - gRunning.count(), curr, peak);
139 for (auto& task : gRunning) {
140 SkDebugf("\t%s\n", task.c_str());
141 }
142}
143
mtklein246ba3a2016-02-23 10:39:36 -0800144#if defined(SK_BUILD_FOR_WIN32)
145 static void setup_crash_handler() {
146 // TODO: custom crash handler like below to print out what was running
147 SetupCrashHandler();
148 }
149
150#else
151 #include <signal.h>
152 static void setup_crash_handler() {
153 const int kSignals[] = { SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGSEGV };
154 for (int sig : kSignals) {
155 signal(sig, [](int sig) {
mtkleinafae30a2016-02-24 12:28:32 -0800156 SkDebugf("\nCaught signal %d [%s].\n", sig, strsignal(sig));
157 print_status();
mtklein246ba3a2016-02-23 10:39:36 -0800158 });
159 }
160 }
161#endif
162
mtklein748ca3b2015-01-15 10:56:12 -0800163/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
mtklein114c3cd2015-01-15 10:15:02 -0800164
mtklein62bd1a62015-01-27 14:46:26 -0800165struct Gold : public SkString {
mtkleina82f5622015-02-20 12:30:19 -0800166 Gold() : SkString("") {}
djsollen54416de2015-04-03 07:24:48 -0700167 Gold(ImplicitString sink, ImplicitString src, ImplicitString srcOptions,
168 ImplicitString name, ImplicitString md5)
mtklein62bd1a62015-01-27 14:46:26 -0800169 : SkString("") {
170 this->append(sink);
171 this->append(src);
djsollen54416de2015-04-03 07:24:48 -0700172 this->append(srcOptions);
mtklein62bd1a62015-01-27 14:46:26 -0800173 this->append(name);
174 this->append(md5);
mtklein62bd1a62015-01-27 14:46:26 -0800175 }
mtkleinc8d1dd42015-10-15 12:23:01 -0700176 struct Hash {
177 uint32_t operator()(const Gold& g) const {
178 return SkGoodHash()((const SkString&)g);
179 }
180 };
mtklein62bd1a62015-01-27 14:46:26 -0800181};
mtkleina82f5622015-02-20 12:30:19 -0800182static SkTHashSet<Gold, Gold::Hash> gGold;
mtklein62bd1a62015-01-27 14:46:26 -0800183
184static void add_gold(JsonWriter::BitmapResult r) {
djsollen54416de2015-04-03 07:24:48 -0700185 gGold.add(Gold(r.config, r.sourceType, r.sourceOptions, r.name, r.md5));
mtklein62bd1a62015-01-27 14:46:26 -0800186}
187
188static void gather_gold() {
189 if (!FLAGS_readPath.isEmpty()) {
190 SkString path(FLAGS_readPath[0]);
191 path.append("/dm.json");
192 if (!JsonWriter::ReadJson(path.c_str(), add_gold)) {
193 fail(SkStringPrintf("Couldn't read %s for golden results.", path.c_str()));
194 }
195 }
196}
197
198/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
199
borenet09ed4802015-04-03 14:15:33 -0700200static SkTHashSet<SkString> gUninterestingHashes;
201
202static void gather_uninteresting_hashes() {
203 if (!FLAGS_uninterestingHashesFile.isEmpty()) {
204 SkAutoTUnref<SkData> data(SkData::NewFromFileName(FLAGS_uninterestingHashesFile[0]));
mtkleincc334b32015-09-22 11:43:53 -0700205 if (!data) {
206 SkDebugf("WARNING: unable to read uninteresting hashes from %s\n",
207 FLAGS_uninterestingHashesFile[0]);
208 return;
209 }
borenet09ed4802015-04-03 14:15:33 -0700210 SkTArray<SkString> hashes;
211 SkStrSplit((const char*)data->data(), "\n", &hashes);
212 for (const SkString& hash : hashes) {
213 gUninterestingHashes.add(hash);
214 }
mtklein136baaa2016-02-03 11:21:45 -0800215 SkDebugf("FYI: loaded %d distinct uninteresting hashes from %d lines\n",
216 gUninterestingHashes.count(), hashes.count());
borenet09ed4802015-04-03 14:15:33 -0700217 }
218}
219
220/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
221
mtkleine0effd62015-07-29 06:37:28 -0700222struct TaggedSrc : public SkAutoTDelete<Src> {
msarett9e707a02015-09-01 14:57:57 -0700223 ImplicitString tag;
224 ImplicitString options;
mtkleine0effd62015-07-29 06:37:28 -0700225};
226
227struct TaggedSink : public SkAutoTDelete<Sink> {
kkinnunen3e980c32015-12-23 01:33:00 -0800228 SkString tag;
djsollen54416de2015-04-03 07:24:48 -0700229};
mtklein748ca3b2015-01-15 10:56:12 -0800230
231static const bool kMemcpyOK = true;
232
mtkleine0effd62015-07-29 06:37:28 -0700233static SkTArray<TaggedSrc, kMemcpyOK> gSrcs;
234static SkTArray<TaggedSink, kMemcpyOK> gSinks;
mtklein748ca3b2015-01-15 10:56:12 -0800235
mtklein6393c062015-04-27 08:45:01 -0700236static bool in_shard() {
237 static int N = 0;
238 return N++ % FLAGS_shards == FLAGS_shard;
239}
240
msarett9e707a02015-09-01 14:57:57 -0700241static void push_src(ImplicitString tag, ImplicitString options, Src* s) {
mtklein748ca3b2015-01-15 10:56:12 -0800242 SkAutoTDelete<Src> src(s);
mtklein6393c062015-04-27 08:45:01 -0700243 if (in_shard() &&
msarett9e707a02015-09-01 14:57:57 -0700244 FLAGS_src.contains(tag.c_str()) &&
mtklein748ca3b2015-01-15 10:56:12 -0800245 !SkCommandLineFlags::ShouldSkip(FLAGS_match, src->name().c_str())) {
mtkleine0effd62015-07-29 06:37:28 -0700246 TaggedSrc& s = gSrcs.push_back();
mtklein748ca3b2015-01-15 10:56:12 -0800247 s.reset(src.detach());
248 s.tag = tag;
djsollen54416de2015-04-03 07:24:48 -0700249 s.options = options;
mtklein114c3cd2015-01-15 10:15:02 -0800250 }
mtklein748ca3b2015-01-15 10:56:12 -0800251}
mtklein114c3cd2015-01-15 10:15:02 -0800252
msarett9e707a02015-09-01 14:57:57 -0700253static void push_codec_src(Path path, CodecSrc::Mode mode, CodecSrc::DstColorType dstColorType,
scroggoc5560be2016-02-03 09:42:42 -0800254 SkAlphaType dstAlphaType, float scale) {
msarett9e707a02015-09-01 14:57:57 -0700255 SkString folder;
256 switch (mode) {
257 case CodecSrc::kCodec_Mode:
258 folder.append("codec");
259 break;
msarettbb25b532016-01-13 09:31:39 -0800260 case CodecSrc::kCodecZeroInit_Mode:
261 folder.append("codec_zero_init");
262 break;
msarett9e707a02015-09-01 14:57:57 -0700263 case CodecSrc::kScanline_Mode:
264 folder.append("scanline");
265 break;
msarett9e707a02015-09-01 14:57:57 -0700266 case CodecSrc::kStripe_Mode:
267 folder.append("stripe");
268 break;
msarett91c22b22016-02-22 12:27:46 -0800269 case CodecSrc::kCroppedScanline_Mode:
270 folder.append("crop");
271 break;
msarett9e707a02015-09-01 14:57:57 -0700272 case CodecSrc::kSubset_Mode:
msarett36c37962015-09-02 13:20:52 -0700273 folder.append("codec_subset");
msarett9e707a02015-09-01 14:57:57 -0700274 break;
msarettb714fb02016-01-22 14:46:42 -0800275 case CodecSrc::kGen_Mode:
276 folder.append("gen");
277 break;
msarett9e707a02015-09-01 14:57:57 -0700278 }
279
280 switch (dstColorType) {
281 case CodecSrc::kGrayscale_Always_DstColorType:
282 folder.append("_kGray8");
283 break;
284 case CodecSrc::kIndex8_Always_DstColorType:
285 folder.append("_kIndex8");
286 break;
287 default:
288 break;
289 }
290
scroggoc5560be2016-02-03 09:42:42 -0800291 switch (dstAlphaType) {
292 case kOpaque_SkAlphaType:
293 folder.append("_opaque");
294 break;
295 case kPremul_SkAlphaType:
296 folder.append("_premul");
297 break;
298 case kUnpremul_SkAlphaType:
299 folder.append("_unpremul");
300 break;
301 default:
302 break;
303 }
304
msarett9e707a02015-09-01 14:57:57 -0700305 if (1.0f != scale) {
306 folder.appendf("_%.3f", scale);
307 }
308
scroggoc5560be2016-02-03 09:42:42 -0800309 CodecSrc* src = new CodecSrc(path, mode, dstColorType, dstAlphaType, scale);
msarett9e707a02015-09-01 14:57:57 -0700310 push_src("image", folder, src);
311}
312
msarett3d9d7a72015-10-21 10:27:10 -0700313static void push_android_codec_src(Path path, AndroidCodecSrc::Mode mode,
scroggoc5560be2016-02-03 09:42:42 -0800314 CodecSrc::DstColorType dstColorType, SkAlphaType dstAlphaType, int sampleSize) {
msarett3d9d7a72015-10-21 10:27:10 -0700315 SkString folder;
316 switch (mode) {
317 case AndroidCodecSrc::kFullImage_Mode:
318 folder.append("scaled_codec");
319 break;
320 case AndroidCodecSrc::kDivisor_Mode:
321 folder.append("scaled_codec_divisor");
322 break;
323 }
324
325 switch (dstColorType) {
326 case CodecSrc::kGrayscale_Always_DstColorType:
327 folder.append("_kGray8");
328 break;
329 case CodecSrc::kIndex8_Always_DstColorType:
330 folder.append("_kIndex8");
331 break;
332 default:
333 break;
334 }
335
scroggoc5560be2016-02-03 09:42:42 -0800336 switch (dstAlphaType) {
337 case kOpaque_SkAlphaType:
338 folder.append("_opaque");
339 break;
340 case kPremul_SkAlphaType:
341 folder.append("_premul");
342 break;
msarett9e9444c2016-02-03 12:39:10 -0800343 case kUnpremul_SkAlphaType:
344 folder.append("_unpremul");
345 break;
scroggoc5560be2016-02-03 09:42:42 -0800346 default:
347 break;
348 }
349
msarett3d9d7a72015-10-21 10:27:10 -0700350 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800351 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700352 }
353
scroggoc5560be2016-02-03 09:42:42 -0800354 AndroidCodecSrc* src = new AndroidCodecSrc(path, mode, dstColorType, dstAlphaType, sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700355 push_src("image", folder, src);
356}
357
msarett438b2ad2015-04-09 12:43:10 -0700358static void push_codec_srcs(Path path) {
359 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
360 if (!encoded) {
361 SkDebugf("Couldn't read %s.", path.c_str());
362 return;
363 }
364 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
halcanary96fcdcc2015-08-27 07:41:13 -0700365 if (nullptr == codec.get()) {
msarett438b2ad2015-04-09 12:43:10 -0700366 SkDebugf("Couldn't create codec for %s.", path.c_str());
367 return;
368 }
369
msarett36c37962015-09-02 13:20:52 -0700370 // Native Scales
msarett0a242972015-06-11 14:27:27 -0700371 // TODO (msarett): Implement scaling tests for SkImageDecoder in order to compare with these
372 // tests. SkImageDecoder supports downscales by integer factors.
emmaleer8f4ba762015-08-14 07:44:46 -0700373 // 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 -0700374 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 -0700375
msarett91c22b22016-02-22 12:27:46 -0800376 SkTArray<CodecSrc::Mode> nativeModes;
377 nativeModes.push_back(CodecSrc::kCodec_Mode);
378 nativeModes.push_back(CodecSrc::kCodecZeroInit_Mode);
379 nativeModes.push_back(CodecSrc::kGen_Mode);
380 switch (codec->getEncodedFormat()) {
381 case SkEncodedFormat::kJPEG_SkEncodedFormat:
382 nativeModes.push_back(CodecSrc::kScanline_Mode);
383 nativeModes.push_back(CodecSrc::kStripe_Mode);
384 nativeModes.push_back(CodecSrc::kCroppedScanline_Mode);
385 break;
386 case SkEncodedFormat::kWEBP_SkEncodedFormat:
387 nativeModes.push_back(CodecSrc::kSubset_Mode);
388 break;
msarettb65e6042016-02-23 05:37:25 -0800389 case SkEncodedFormat::kRAW_SkEncodedFormat:
390 break;
msarett91c22b22016-02-22 12:27:46 -0800391 default:
392 nativeModes.push_back(CodecSrc::kScanline_Mode);
393 nativeModes.push_back(CodecSrc::kStripe_Mode);
394 break;
395 }
msarett9e707a02015-09-01 14:57:57 -0700396
msarett91c22b22016-02-22 12:27:46 -0800397 SkTArray<CodecSrc::DstColorType> colorTypes;
398 colorTypes.push_back(CodecSrc::kGetFromCanvas_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700399 switch (codec->getInfo().colorType()) {
400 case kGray_8_SkColorType:
msarett91c22b22016-02-22 12:27:46 -0800401 colorTypes.push_back(CodecSrc::kGrayscale_Always_DstColorType);
msarett55f7bdd2016-02-16 13:24:54 -0800402 if (kWBMP_SkEncodedFormat == codec->getEncodedFormat()) {
msarett91c22b22016-02-22 12:27:46 -0800403 colorTypes.push_back(CodecSrc::kIndex8_Always_DstColorType);
msarett55f7bdd2016-02-16 13:24:54 -0800404 }
msarett36c37962015-09-02 13:20:52 -0700405 break;
406 case kIndex_8_SkColorType:
msarett91c22b22016-02-22 12:27:46 -0800407 colorTypes.push_back(CodecSrc::kIndex8_Always_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700408 break;
409 default:
msarett36c37962015-09-02 13:20:52 -0700410 break;
411 }
msarett9e707a02015-09-01 14:57:57 -0700412
scroggoc5560be2016-02-03 09:42:42 -0800413 SkTArray<SkAlphaType> alphaModes;
414 alphaModes.push_back(kPremul_SkAlphaType);
msarett9e9444c2016-02-03 12:39:10 -0800415 alphaModes.push_back(kUnpremul_SkAlphaType);
scroggoc5560be2016-02-03 09:42:42 -0800416 if (codec->getInfo().alphaType() == kOpaque_SkAlphaType) {
417 alphaModes.push_back(kOpaque_SkAlphaType);
418 }
msarettb714fb02016-01-22 14:46:42 -0800419
420 for (CodecSrc::Mode mode : nativeModes) {
421 // SkCodecImageGenerator only runs for the default colorType
422 // recommended by SkCodec. There is no need to generate multiple
423 // tests for different colorTypes.
424 // TODO (msarett): Add scaling support to SkCodecImageGenerator.
425 if (CodecSrc::kGen_Mode == mode) {
426 // FIXME: The gpu backend does not draw kGray sources correctly. (skbug.com/4822)
427 if (kGray_8_SkColorType != codec->getInfo().colorType()) {
scroggoc5560be2016-02-03 09:42:42 -0800428 push_codec_src(path, mode, CodecSrc::kGetFromCanvas_DstColorType,
429 codec->getInfo().alphaType(), 1.0f);
msarettb714fb02016-01-22 14:46:42 -0800430 }
431 continue;
432 }
433
434 for (float scale : nativeScales) {
msarett91c22b22016-02-22 12:27:46 -0800435 for (CodecSrc::DstColorType colorType : colorTypes) {
scroggoc5560be2016-02-03 09:42:42 -0800436 for (SkAlphaType alphaType : alphaModes) {
msarett91c22b22016-02-22 12:27:46 -0800437 // Only test kCroppedScanline_Mode when the alpha type is opaque. The test is
438 // slow and won't be interestingly different with different alpha types.
439 if (CodecSrc::kCroppedScanline_Mode == mode &&
440 kOpaque_SkAlphaType != alphaType) {
441 continue;
442 }
443
444 push_codec_src(path, mode, colorType, alphaType, scale);
scroggoc5560be2016-02-03 09:42:42 -0800445 }
msarett9e707a02015-09-01 14:57:57 -0700446 }
447 }
msarett0a242972015-06-11 14:27:27 -0700448 }
msarett36c37962015-09-02 13:20:52 -0700449
halcanary6950de62015-11-07 05:29:00 -0800450 // https://bug.skia.org/4428
msarett33c76232015-11-16 13:43:40 -0800451 bool subset = false;
452 // The following image types are supported by BitmapRegionDecoder,
453 // so we will test full image decodes and subset decodes.
msarettbe8216a2015-12-04 08:00:50 -0800454 static const char* const exts[] = {
msarett3d9d7a72015-10-21 10:27:10 -0700455 "jpg", "jpeg", "png", "webp",
456 "JPG", "JPEG", "PNG", "WEBP",
457 };
msarettbe8216a2015-12-04 08:00:50 -0800458 for (const char* ext : exts) {
msarett3d9d7a72015-10-21 10:27:10 -0700459 if (path.endsWith(ext)) {
msarett33c76232015-11-16 13:43:40 -0800460 subset = true;
msarett3d9d7a72015-10-21 10:27:10 -0700461 break;
462 }
463 }
msarett33c76232015-11-16 13:43:40 -0800464
msarett3d9d7a72015-10-21 10:27:10 -0700465 const int sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
msarett36c37962015-09-02 13:20:52 -0700466
msarett3d9d7a72015-10-21 10:27:10 -0700467 for (int sampleSize : sampleSizes) {
msarett91c22b22016-02-22 12:27:46 -0800468 for (CodecSrc::DstColorType colorType : colorTypes) {
scroggoc5560be2016-02-03 09:42:42 -0800469 for (SkAlphaType alphaType : alphaModes) {
msarett91c22b22016-02-22 12:27:46 -0800470 push_android_codec_src(path, AndroidCodecSrc::kFullImage_Mode, colorType,
scroggoc5560be2016-02-03 09:42:42 -0800471 alphaType, sampleSize);
472 if (subset) {
msarett91c22b22016-02-22 12:27:46 -0800473 push_android_codec_src(path, AndroidCodecSrc::kDivisor_Mode, colorType,
scroggoc5560be2016-02-03 09:42:42 -0800474 alphaType, sampleSize);
475 }
msarett3d9d7a72015-10-21 10:27:10 -0700476 }
msarett36c37962015-09-02 13:20:52 -0700477 }
478 }
msarett438b2ad2015-04-09 12:43:10 -0700479}
480
msarett5cb48852015-11-06 08:56:32 -0800481static bool brd_color_type_supported(SkBitmapRegionDecoder::Strategy strategy,
msaretta5783ae2015-09-08 15:35:32 -0700482 CodecSrc::DstColorType dstColorType) {
483 switch (strategy) {
msarett5cb48852015-11-06 08:56:32 -0800484 case SkBitmapRegionDecoder::kCanvas_Strategy:
msaretta5783ae2015-09-08 15:35:32 -0700485 if (CodecSrc::kGetFromCanvas_DstColorType == dstColorType) {
486 return true;
487 }
488 return false;
msarett5cb48852015-11-06 08:56:32 -0800489 case SkBitmapRegionDecoder::kAndroidCodec_Strategy:
msarett26ad17b2015-10-22 07:29:19 -0700490 switch (dstColorType) {
491 case CodecSrc::kGetFromCanvas_DstColorType:
492 case CodecSrc::kIndex8_Always_DstColorType:
493 case CodecSrc::kGrayscale_Always_DstColorType:
494 return true;
495 default:
496 return false;
497 }
msaretta5783ae2015-09-08 15:35:32 -0700498 default:
499 SkASSERT(false);
500 return false;
501 }
502}
503
msarett5cb48852015-11-06 08:56:32 -0800504static void push_brd_src(Path path, SkBitmapRegionDecoder::Strategy strategy,
msaretta5783ae2015-09-08 15:35:32 -0700505 CodecSrc::DstColorType dstColorType, BRDSrc::Mode mode, uint32_t sampleSize) {
506 SkString folder;
507 switch (strategy) {
msarett5cb48852015-11-06 08:56:32 -0800508 case SkBitmapRegionDecoder::kCanvas_Strategy:
msaretta5783ae2015-09-08 15:35:32 -0700509 folder.append("brd_canvas");
510 break;
msarett5cb48852015-11-06 08:56:32 -0800511 case SkBitmapRegionDecoder::kAndroidCodec_Strategy:
msarett26ad17b2015-10-22 07:29:19 -0700512 folder.append("brd_android_codec");
513 break;
msaretta5783ae2015-09-08 15:35:32 -0700514 default:
515 SkASSERT(false);
516 return;
517 }
518
519 switch (mode) {
520 case BRDSrc::kFullImage_Mode:
521 break;
522 case BRDSrc::kDivisor_Mode:
523 folder.append("_divisor");
524 break;
525 default:
526 SkASSERT(false);
527 return;
528 }
529
530 switch (dstColorType) {
531 case CodecSrc::kGetFromCanvas_DstColorType:
532 break;
533 case CodecSrc::kIndex8_Always_DstColorType:
534 folder.append("_kIndex");
535 break;
536 case CodecSrc::kGrayscale_Always_DstColorType:
537 folder.append("_kGray");
538 break;
539 default:
540 SkASSERT(false);
541 return;
542 }
543
544 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800545 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700546 }
547
548 BRDSrc* src = new BRDSrc(path, strategy, mode, dstColorType, sampleSize);
549 push_src("image", folder, src);
550}
551
552static void push_brd_srcs(Path path) {
553
msarett5cb48852015-11-06 08:56:32 -0800554 const SkBitmapRegionDecoder::Strategy strategies[] = {
555 SkBitmapRegionDecoder::kCanvas_Strategy,
msarett5cb48852015-11-06 08:56:32 -0800556 SkBitmapRegionDecoder::kAndroidCodec_Strategy,
msaretta5783ae2015-09-08 15:35:32 -0700557 };
558
scroggo501b7342015-11-03 07:55:11 -0800559 // Test on a variety of sampleSizes, making sure to include:
560 // - 2, 4, and 8, which are natively supported by jpeg
561 // - multiples of 2 which are not divisible by 4 (analogous for 4)
562 // - larger powers of two, since BRD clients generally use powers of 2
563 // We will only produce output for the larger sizes on large images.
564 const uint32_t sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 24, 32, 64 };
msarett6efbe052015-09-11 09:01:16 -0700565
msaretta5783ae2015-09-08 15:35:32 -0700566 // We will only test to one backend (8888), but we will test all of the
567 // color types that we need to decode to on this backend.
568 const CodecSrc::DstColorType dstColorTypes[] = {
msarett26ad17b2015-10-22 07:29:19 -0700569 CodecSrc::kGetFromCanvas_DstColorType,
570 CodecSrc::kIndex8_Always_DstColorType,
571 CodecSrc::kGrayscale_Always_DstColorType,
msaretta5783ae2015-09-08 15:35:32 -0700572 };
573
574 const BRDSrc::Mode modes[] = {
msarett26ad17b2015-10-22 07:29:19 -0700575 BRDSrc::kFullImage_Mode,
576 BRDSrc::kDivisor_Mode,
msaretta5783ae2015-09-08 15:35:32 -0700577 };
578
msarett5cb48852015-11-06 08:56:32 -0800579 for (SkBitmapRegionDecoder::Strategy strategy : strategies) {
msarett6efbe052015-09-11 09:01:16 -0700580 for (uint32_t sampleSize : sampleSizes) {
msarett6efbe052015-09-11 09:01:16 -0700581 for (CodecSrc::DstColorType dstColorType : dstColorTypes) {
582 if (brd_color_type_supported(strategy, dstColorType)) {
583 for (BRDSrc::Mode mode : modes) {
msaretta5783ae2015-09-08 15:35:32 -0700584 push_brd_src(path, strategy, dstColorType, mode, sampleSize);
585 }
586 }
587 }
588 }
589 }
590}
591
592static bool brd_supported(const char* ext) {
msarett8c8f22a2015-04-01 06:58:48 -0700593 static const char* const exts[] = {
msaretta5783ae2015-09-08 15:35:32 -0700594 "jpg", "jpeg", "png", "webp",
595 "JPG", "JPEG", "PNG", "WEBP",
msarett8c8f22a2015-04-01 06:58:48 -0700596 };
597
598 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
599 if (0 == strcmp(exts[i], ext)) {
600 return true;
601 }
602 }
603 return false;
scroggo9b77ddd2015-03-19 06:03:39 -0700604}
605
scroggo86737142016-02-03 12:19:11 -0800606static bool gather_srcs() {
mtklein748ca3b2015-01-15 10:56:12 -0800607 for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) {
djsollen54416de2015-04-03 07:24:48 -0700608 push_src("gm", "", new GMSrc(r->factory()));
mtklein748ca3b2015-01-15 10:56:12 -0800609 }
halcanaryfc37ad12015-01-30 07:31:19 -0800610 for (int i = 0; i < FLAGS_skps.count(); i++) {
611 const char* path = FLAGS_skps[i];
612 if (sk_isdir(path)) {
613 SkOSFile::Iter it(path, "skp");
614 for (SkString file; it.next(&file); ) {
djsollen54416de2015-04-03 07:24:48 -0700615 push_src("skp", "", new SKPSrc(SkOSPath::Join(path, file.c_str())));
halcanaryfc37ad12015-01-30 07:31:19 -0800616 }
617 } else {
djsollen54416de2015-04-03 07:24:48 -0700618 push_src("skp", "", new SKPSrc(path));
mtklein114c3cd2015-01-15 10:15:02 -0800619 }
620 }
scroggo86737142016-02-03 12:19:11 -0800621
622 SkTArray<SkString> images;
623 if (!CollectImages(&images)) {
624 return false;
625 }
626
627 for (auto image : images) {
628 push_codec_srcs(image);
mtklein21eaf3b2016-02-08 12:39:59 -0800629 const char* ext = strrchr(image.c_str(), '.');
630 if (ext && brd_supported(ext+1)) {
scroggo86737142016-02-03 12:19:11 -0800631 push_brd_srcs(image);
mtklein709d2c32015-01-15 08:30:25 -0800632 }
mtklein709d2c32015-01-15 08:30:25 -0800633 }
scroggo86737142016-02-03 12:19:11 -0800634
635 return true;
mtklein709d2c32015-01-15 08:30:25 -0800636}
637
kkinnunen3e980c32015-12-23 01:33:00 -0800638static void push_sink(const SkCommandLineConfig& config, Sink* s) {
rmistry0f515bd2015-12-22 10:22:26 -0800639 SkAutoTDelete<Sink> sink(s);
kkinnunen3e980c32015-12-23 01:33:00 -0800640
mtkleine0effd62015-07-29 06:37:28 -0700641 // Try a simple Src as a canary. If it fails, skip this sink.
mtklein748ca3b2015-01-15 10:56:12 -0800642 struct : public Src {
mtkleine0effd62015-07-29 06:37:28 -0700643 Error draw(SkCanvas* c) const override {
644 c->drawRect(SkRect::MakeWH(1,1), SkPaint());
645 return "";
646 }
mtklein36352bf2015-03-25 18:17:31 -0700647 SkISize size() const override { return SkISize::Make(16, 16); }
mtkleine0effd62015-07-29 06:37:28 -0700648 Name name() const override { return "justOneRect"; }
649 } justOneRect;
mtklein114c3cd2015-01-15 10:15:02 -0800650
mtklein748ca3b2015-01-15 10:56:12 -0800651 SkBitmap bitmap;
652 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800653 SkString log;
mtkleine0effd62015-07-29 06:37:28 -0700654 Error err = sink->draw(justOneRect, &bitmap, &stream, &log);
mtklein4089ef72015-03-05 08:40:28 -0800655 if (err.isFatal()) {
kkinnunen3e980c32015-12-23 01:33:00 -0800656 SkDebugf("Could not run %s: %s\n", config.getTag().c_str(), err.c_str());
mtklein05641a52015-04-21 10:49:13 -0700657 exit(1);
mtklein114c3cd2015-01-15 10:15:02 -0800658 }
659
mtkleine0effd62015-07-29 06:37:28 -0700660 TaggedSink& ts = gSinks.push_back();
mtklein748ca3b2015-01-15 10:56:12 -0800661 ts.reset(sink.detach());
kkinnunen3e980c32015-12-23 01:33:00 -0800662 ts.tag = config.getTag();
mtklein748ca3b2015-01-15 10:56:12 -0800663}
664
665static bool gpu_supported() {
666#if SK_SUPPORT_GPU
667 return FLAGS_gpu;
668#else
669 return false;
670#endif
671}
kkinnunen9ebc3f02015-12-21 23:48:13 -0800672
kkinnunen3e980c32015-12-23 01:33:00 -0800673static Sink* create_sink(const SkCommandLineConfig* config) {
674#if SK_SUPPORT_GPU
675 if (gpu_supported()) {
676 if (const SkCommandLineConfigGpu* gpuConfig = config->asConfigGpu()) {
677 GrContextFactory::GLContextType contextType = gpuConfig->getContextType();
678 GrContextFactory::GLContextOptions contextOptions =
679 GrContextFactory::kNone_GLContextOptions;
680 if (gpuConfig->getUseNVPR()) {
681 contextOptions = static_cast<GrContextFactory::GLContextOptions>(
682 contextOptions | GrContextFactory::kEnableNVPR_GLContextOptions);
683 }
684 GrContextFactory testFactory;
685 if (!testFactory.get(contextType, contextOptions)) {
686 SkDebugf("WARNING: can not create GPU context for config '%s'. "
687 "GM tests will be skipped.\n", gpuConfig->getTag().c_str());
688 return nullptr;
689 }
690 return new GPUSink(contextType, contextOptions, gpuConfig->getSamples(),
691 gpuConfig->getUseDIText(), FLAGS_gpu_threading);
692 }
693 }
694#endif
695
696#define SINK(t, sink, ...) if (config->getBackend().equals(t)) { return new sink(__VA_ARGS__); }
697
tomhudsoneebc39a2015-02-23 12:18:05 -0800698#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
699 SINK("hwui", HWUISink);
700#endif
701
mtklein748ca3b2015-01-15 10:56:12 -0800702 if (FLAGS_cpu) {
703 SINK("565", RasterSink, kRGB_565_SkColorType);
704 SINK("8888", RasterSink, kN32_SkColorType);
halcanaryc11c62f2015-09-28 11:51:54 -0700705 SINK("pdf", PDFSink, "Pdfium");
706 SINK("pdf_poppler", PDFSink, "Poppler");
mtklein9c3f17d2015-01-28 11:35:18 -0800707 SINK("skp", SKPSink);
mtklein8a4527e2015-01-31 20:00:58 -0800708 SINK("svg", SVGSink);
709 SINK("null", NullSink);
halcanary47ef4d52015-03-03 09:13:09 -0800710 SINK("xps", XPSSink);
mtklein748ca3b2015-01-15 10:56:12 -0800711 }
712#undef SINK
halcanary96fcdcc2015-08-27 07:41:13 -0700713 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800714}
715
kkinnunen3e980c32015-12-23 01:33:00 -0800716static Sink* create_via(const SkString& tag, Sink* wrapped) {
717#define VIA(t, via, ...) if (tag.equals(t)) { return new via(__VA_ARGS__); }
halcanary96fcdcc2015-08-27 07:41:13 -0700718 VIA("twice", ViaTwice, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700719 VIA("serialize", ViaSerialization, wrapped);
mtklein4a34ecb2016-01-08 10:19:35 -0800720 VIA("pic", ViaPicture, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700721 VIA("2ndpic", ViaSecondPicture, wrapped);
mtkleind31c13d2015-05-05 12:59:56 -0700722 VIA("sp", ViaSingletonPictures, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700723 VIA("tiles", ViaTiles, 256, 256, nullptr, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800724 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
mtklein2e2ea382015-10-16 10:29:41 -0700725 VIA("remote", ViaRemote, false, wrapped);
726 VIA("remote_cache", ViaRemote, true, wrapped);
halcanary7a76f9c2016-02-03 11:53:18 -0800727 VIA("mojo", ViaMojo, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800728
mtkleind603b222015-02-17 11:13:33 -0800729 if (FLAGS_matrix.count() == 4) {
mtklein748ca3b2015-01-15 10:56:12 -0800730 SkMatrix m;
mtkleind603b222015-02-17 11:13:33 -0800731 m.reset();
732 m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
733 m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
734 m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
735 m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
736 VIA("matrix", ViaMatrix, m, wrapped);
737 VIA("upright", ViaUpright, m, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800738 }
tomhudson64de1e12015-03-05 08:01:07 -0800739
740#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
741 VIA("androidsdk", ViaAndroidSDK, wrapped);
742#endif
743
mtklein748ca3b2015-01-15 10:56:12 -0800744#undef VIA
halcanary96fcdcc2015-08-27 07:41:13 -0700745 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800746}
747
mtklein748ca3b2015-01-15 10:56:12 -0800748static void gather_sinks() {
kkinnunen3e980c32015-12-23 01:33:00 -0800749 SkCommandLineConfigArray configs;
750 ParseConfigs(FLAGS_config, &configs);
751 for (int i = 0; i < configs.count(); i++) {
752 const SkCommandLineConfig& config = *configs[i];
753 Sink* sink = create_sink(&config);
754 if (sink == nullptr) {
755 SkDebugf("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
756 config.getTag().c_str());
757 continue;
758 }
mtklein748ca3b2015-01-15 10:56:12 -0800759
kkinnunen3e980c32015-12-23 01:33:00 -0800760 const SkTArray<SkString>& parts = config.getViaParts();
761 for (int j = parts.count(); j-- > 0;) {
762 const SkString& part = parts[j];
763 Sink* next = create_via(part, sink);
halcanary96fcdcc2015-08-27 07:41:13 -0700764 if (next == nullptr) {
kkinnunen3e980c32015-12-23 01:33:00 -0800765 SkDebugf("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
766 part.c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800767 delete sink;
halcanary96fcdcc2015-08-27 07:41:13 -0700768 sink = nullptr;
mtklein748ca3b2015-01-15 10:56:12 -0800769 break;
770 }
771 sink = next;
772 }
773 if (sink) {
774 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800775 }
776 }
777}
mtklein709d2c32015-01-15 08:30:25 -0800778
mtkleina5114d72015-08-24 13:27:01 -0700779static bool dump_png(SkBitmap bitmap, const char* path, const char* md5) {
780 const int w = bitmap.width(),
781 h = bitmap.height();
782
783 // First get the bitmap into N32 color format. The next step will work only there.
784 if (bitmap.colorType() != kN32_SkColorType) {
785 SkBitmap n32;
786 if (!bitmap.copyTo(&n32, kN32_SkColorType)) {
787 return false;
788 }
789 bitmap = n32;
790 }
791
792 // Convert our N32 bitmap into unpremul RGBA for libpng.
793 SkAutoTMalloc<uint32_t> rgba(w*h);
794 if (!bitmap.readPixels(SkImageInfo::Make(w,h, kRGBA_8888_SkColorType, kUnpremul_SkAlphaType),
795 rgba, 4*w, 0,0)) {
796 return false;
797 }
798
799 // We don't need bitmap anymore. Might as well drop our ref.
mtkleinfccb77d2015-08-24 14:13:29 -0700800 bitmap.reset();
mtkleina5114d72015-08-24 13:27:01 -0700801
mtkleinc64137c2015-08-25 10:56:08 -0700802 FILE* f = fopen(path, "wb");
mtkleina5114d72015-08-24 13:27:01 -0700803 if (!f) { return false; }
804
805 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
806 if (!png) {
807 fclose(f);
808 return false;
809 }
810
811 png_infop info = png_create_info_struct(png);
812 if (!info) {
813 png_destroy_write_struct(&png, &info);
814 fclose(f);
815 return false;
816 }
817
mtkleind69ece62015-09-10 10:37:44 -0700818 SkString description;
819 description.append("Key: ");
820 for (int i = 0; i < FLAGS_key.count(); i++) {
821 description.appendf("%s ", FLAGS_key[i]);
822 }
823 description.append("Properties: ");
824 for (int i = 0; i < FLAGS_properties.count(); i++) {
825 description.appendf("%s ", FLAGS_properties[i]);
826 }
827 description.appendf("MD5: %s", md5);
828
mtkleina5114d72015-08-24 13:27:01 -0700829 png_text text[2];
830 text[0].key = (png_charp)"Author";
831 text[0].text = (png_charp)"DM dump_png()";
832 text[0].compression = PNG_TEXT_COMPRESSION_NONE;
833 text[1].key = (png_charp)"Description";
mtkleind69ece62015-09-10 10:37:44 -0700834 text[1].text = (png_charp)description.c_str();
mtkleina5114d72015-08-24 13:27:01 -0700835 text[1].compression = PNG_TEXT_COMPRESSION_NONE;
836 png_set_text(png, info, text, 2);
837
838 png_init_io(png, f);
839 png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8,
840 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
841 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
842 png_write_info(png, info);
843 for (int j = 0; j < h; j++) {
844 png_bytep row = (png_bytep)(rgba.get() + w*j);
845 png_write_rows(png, &row, 1);
846 }
847 png_write_end(png, info);
848
849 png_destroy_write_struct(&png, &info);
850 fclose(f);
851 return true;
852}
853
mtkleina2ef6422015-01-15 13:44:22 -0800854static bool match(const char* needle, const char* haystack) {
halcanary96fcdcc2015-08-27 07:41:13 -0700855 return 0 == strcmp("_", needle) || nullptr != strstr(haystack, needle);
mtkleina2ef6422015-01-15 13:44:22 -0800856}
857
djsollen54416de2015-04-03 07:24:48 -0700858static ImplicitString is_blacklisted(const char* sink, const char* src,
859 const char* srcOptions, const char* name) {
mtklein0d243ff2015-04-03 07:51:00 -0700860 for (int i = 0; i < FLAGS_blacklist.count() - 3; i += 4) {
mtkleina2ef6422015-01-15 13:44:22 -0800861 if (match(FLAGS_blacklist[i+0], sink) &&
djsollen54416de2015-04-03 07:24:48 -0700862 match(FLAGS_blacklist[i+1], src) &&
863 match(FLAGS_blacklist[i+2], srcOptions) &&
864 match(FLAGS_blacklist[i+3], name)) {
865 return SkStringPrintf("%s %s %s %s",
866 FLAGS_blacklist[i+0], FLAGS_blacklist[i+1],
867 FLAGS_blacklist[i+2], FLAGS_blacklist[i+3]);
mtkleina2ef6422015-01-15 13:44:22 -0800868 }
869 }
870 return "";
871}
872
mtkleincd50bca2016-01-05 06:20:20 -0800873// Even when a Task Sink reports to be non-threadsafe (e.g. GPU), we know things like
874// .png encoding are definitely thread safe. This lets us offload that work to CPU threads.
875static SkTaskGroup gDefinitelyThreadSafeWork;
876
mtklein748ca3b2015-01-15 10:56:12 -0800877// The finest-grained unit of work we can run: draw a single Src into a single Sink,
878// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
879struct Task {
mtkleine0effd62015-07-29 06:37:28 -0700880 Task(const TaggedSrc& src, const TaggedSink& sink) : src(src), sink(sink) {}
881 const TaggedSrc& src;
882 const TaggedSink& sink;
mtklein748ca3b2015-01-15 10:56:12 -0800883
mtklein21eaf3b2016-02-08 12:39:59 -0800884 static void Run(const Task& task) {
885 SkString name = task.src->name();
mtkleine0effd62015-07-29 06:37:28 -0700886
887 // We'll skip drawing this Src/Sink pair if:
888 // - the Src vetoes the Sink;
889 // - this Src / Sink combination is on the blacklist;
890 // - it's a dry run.
mtklein21eaf3b2016-02-08 12:39:59 -0800891 SkString note(task.src->veto(task.sink->flags()) ? " (veto)" : "");
892 SkString whyBlacklisted = is_blacklisted(task.sink.tag.c_str(), task.src.tag.c_str(),
893 task.src.options.c_str(), name.c_str());
mtkleinb37cb412015-03-18 05:27:14 -0700894 if (!whyBlacklisted.isEmpty()) {
895 note.appendf(" (--blacklist %s)", whyBlacklisted.c_str());
896 }
mtkleine0effd62015-07-29 06:37:28 -0700897
mtkleinb9eb4ac2015-02-02 18:26:03 -0800898 SkString log;
mtkleinf27f08b2015-11-03 06:54:24 -0800899 auto timerStart = now_ms();
mtkleine0effd62015-07-29 06:37:28 -0700900 if (!FLAGS_dryRun && note.isEmpty()) {
mtklein748ca3b2015-01-15 10:56:12 -0800901 SkBitmap bitmap;
902 SkDynamicMemoryWStream stream;
mtklein21eaf3b2016-02-08 12:39:59 -0800903 start(task.sink.tag.c_str(), task.src.tag, task.src.options, name.c_str());
904 Error err = task.sink->draw(*task.src, &bitmap, &stream, &log);
mtklein748ca3b2015-01-15 10:56:12 -0800905 if (!err.isEmpty()) {
mtklein4089ef72015-03-05 08:40:28 -0800906 if (err.isFatal()) {
djsollen54416de2015-04-03 07:24:48 -0700907 fail(SkStringPrintf("%s %s %s %s: %s",
mtklein21eaf3b2016-02-08 12:39:59 -0800908 task.sink.tag.c_str(),
909 task.src.tag.c_str(),
910 task.src.options.c_str(),
mtklein4089ef72015-03-05 08:40:28 -0800911 name.c_str(),
912 err.c_str()));
mtkleinb37cb412015-03-18 05:27:14 -0700913 } else {
914 note.appendf(" (skipped: %s)", err.c_str());
mtkleinba6ada72016-01-21 09:39:35 -0800915 auto elapsed = now_ms() - timerStart;
mtklein21eaf3b2016-02-08 12:39:59 -0800916 done(elapsed, task.sink.tag.c_str(), task.src.tag, task.src.options,
mtkleinba6ada72016-01-21 09:39:35 -0800917 name, note, log);
918 return;
mtklein4089ef72015-03-05 08:40:28 -0800919 }
mtklein748ca3b2015-01-15 10:56:12 -0800920 }
mtklein62bd1a62015-01-27 14:46:26 -0800921
mtkleincd50bca2016-01-05 06:20:20 -0800922 // We're likely switching threads here, so we must capture by value, [=] or [foo,bar].
923 SkStreamAsset* data = stream.detachAsStream();
924 gDefinitelyThreadSafeWork.add([task,name,bitmap,data]{
925 SkAutoTDelete<SkStreamAsset> ownedData(data);
926
927 // Why doesn't the copy constructor do this when we have pre-locked pixels?
928 bitmap.lockPixels();
929
930 SkString md5;
931 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
932 SkMD5 hash;
933 if (data->getLength()) {
934 hash.writeStream(data, data->getLength());
935 data->rewind();
mtklein38408462015-07-08 07:25:27 -0700936 } else {
mtkleincd50bca2016-01-05 06:20:20 -0800937 // If we're BGRA (Linux, Windows), swizzle over to RGBA (Mac, Android).
938 // This helps eliminate multiple 0-pixel-diff hashes on gold.skia.org.
939 // (Android's general slow speed breaks the tie arbitrarily in RGBA's favor.)
940 // We might consider promoting 565 to RGBA too.
941 if (bitmap.colorType() == kBGRA_8888_SkColorType) {
942 SkBitmap swizzle;
943 SkAssertResult(bitmap.copyTo(&swizzle, kRGBA_8888_SkColorType));
944 hash.write(swizzle.getPixels(), swizzle.getSize());
945 } else {
946 hash.write(bitmap.getPixels(), bitmap.getSize());
947 }
948 }
949 SkMD5::Digest digest;
950 hash.finish(digest);
951 for (int i = 0; i < 16; i++) {
952 md5.appendf("%02x", digest.data[i]);
mtklein38408462015-07-08 07:25:27 -0700953 }
mtklein62bd1a62015-01-27 14:46:26 -0800954 }
mtklein62bd1a62015-01-27 14:46:26 -0800955
mtkleincd50bca2016-01-05 06:20:20 -0800956 if (!FLAGS_readPath.isEmpty() &&
mtklein21eaf3b2016-02-08 12:39:59 -0800957 !gGold.contains(Gold(task.sink.tag.c_str(), task.src.tag.c_str(),
958 task.src.options.c_str(), name, md5))) {
mtkleincd50bca2016-01-05 06:20:20 -0800959 fail(SkStringPrintf("%s not found for %s %s %s %s in %s",
960 md5.c_str(),
mtklein21eaf3b2016-02-08 12:39:59 -0800961 task.sink.tag.c_str(),
962 task.src.tag.c_str(),
963 task.src.options.c_str(),
mtkleincd50bca2016-01-05 06:20:20 -0800964 name.c_str(),
965 FLAGS_readPath[0]));
mtklein748ca3b2015-01-15 10:56:12 -0800966 }
mtkleincd50bca2016-01-05 06:20:20 -0800967
968 if (!FLAGS_writePath.isEmpty()) {
mtklein21eaf3b2016-02-08 12:39:59 -0800969 const char* ext = task.sink->fileExtension();
mtkleincd50bca2016-01-05 06:20:20 -0800970 if (data->getLength()) {
mtklein21eaf3b2016-02-08 12:39:59 -0800971 WriteToDisk(task, md5, ext, data, data->getLength(), nullptr);
mtkleincd50bca2016-01-05 06:20:20 -0800972 SkASSERT(bitmap.drawsNothing());
973 } else if (!bitmap.drawsNothing()) {
mtklein21eaf3b2016-02-08 12:39:59 -0800974 WriteToDisk(task, md5, ext, nullptr, 0, &bitmap);
mtkleincd50bca2016-01-05 06:20:20 -0800975 }
976 }
977 });
mtklein748ca3b2015-01-15 10:56:12 -0800978 }
mtkleinba6ada72016-01-21 09:39:35 -0800979 auto elapsed = now_ms() - timerStart;
mtklein21eaf3b2016-02-08 12:39:59 -0800980 done(elapsed, task.sink.tag.c_str(), task.src.tag.c_str(), task.src.options.c_str(),
mtkleinf27f08b2015-11-03 06:54:24 -0800981 name, note, log);
mtklein748ca3b2015-01-15 10:56:12 -0800982 }
983
984 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -0800985 SkString md5,
986 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -0800987 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -0800988 const SkBitmap* bitmap) {
mtklein748ca3b2015-01-15 10:56:12 -0800989 JsonWriter::BitmapResult result;
djsollen54416de2015-04-03 07:24:48 -0700990 result.name = task.src->name();
kkinnunen3e980c32015-12-23 01:33:00 -0800991 result.config = task.sink.tag.c_str();
djsollen54416de2015-04-03 07:24:48 -0700992 result.sourceType = task.src.tag;
993 result.sourceOptions = task.src.options;
994 result.ext = ext;
995 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -0800996 JsonWriter::AddBitmapResult(result);
997
mtkleinb0531a72015-04-07 13:38:48 -0700998 // If an MD5 is uninteresting, we want it noted in the JSON file,
999 // but don't want to dump it out as a .png (or whatever ext is).
1000 if (gUninterestingHashes.contains(md5)) {
1001 return;
1002 }
1003
mtklein748ca3b2015-01-15 10:56:12 -08001004 const char* dir = FLAGS_writePath[0];
1005 if (0 == strcmp(dir, "@")) { // Needed for iOS.
1006 dir = FLAGS_resourcePath[0];
1007 }
1008 sk_mkdir(dir);
1009
1010 SkString path;
1011 if (FLAGS_nameByHash) {
1012 path = SkOSPath::Join(dir, result.md5.c_str());
1013 path.append(".");
1014 path.append(ext);
1015 if (sk_exists(path.c_str())) {
1016 return; // Content-addressed. If it exists already, we're done.
1017 }
1018 } else {
kkinnunen3e980c32015-12-23 01:33:00 -08001019 path = SkOSPath::Join(dir, task.sink.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001020 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001021 path = SkOSPath::Join(path.c_str(), task.src.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001022 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001023 if (strcmp(task.src.options.c_str(), "") != 0) {
1024 path = SkOSPath::Join(path.c_str(), task.src.options.c_str());
djsollen54416de2015-04-03 07:24:48 -07001025 sk_mkdir(path.c_str());
1026 }
mtklein748ca3b2015-01-15 10:56:12 -08001027 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
1028 path.append(".");
1029 path.append(ext);
1030 }
1031
mtklein748ca3b2015-01-15 10:56:12 -08001032 if (bitmap) {
mtkleina5114d72015-08-24 13:27:01 -07001033 if (!dump_png(*bitmap, path.c_str(), result.md5.c_str())) {
mtklein748ca3b2015-01-15 10:56:12 -08001034 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
1035 return;
1036 }
1037 } else {
mtkleina5114d72015-08-24 13:27:01 -07001038 SkFILEWStream file(path.c_str());
1039 if (!file.isValid()) {
1040 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
1041 return;
1042 }
mtklein748ca3b2015-01-15 10:56:12 -08001043 if (!file.writeStream(data, len)) {
1044 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
1045 return;
1046 }
1047 }
1048 }
1049};
1050
mtklein748ca3b2015-01-15 10:56:12 -08001051/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1052
1053// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
1054
mtklein21eaf3b2016-02-08 12:39:59 -08001055static SkTDArray<skiatest::Test> gParallelTests, gSerialTests;
mtklein748ca3b2015-01-15 10:56:12 -08001056
1057static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -08001058 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -08001059 return;
1060 }
mtklein6393c062015-04-27 08:45:01 -07001061 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r; r = r->next()) {
1062 if (!in_shard()) {
1063 continue;
1064 }
halcanary87f3ba42015-01-20 09:30:20 -08001065 // Despite its name, factory() is returning a reference to
1066 // link-time static const POD data.
1067 const skiatest::Test& test = r->factory();
1068 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -08001069 continue;
1070 }
halcanary87f3ba42015-01-20 09:30:20 -08001071 if (test.needsGpu && gpu_supported()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001072 (FLAGS_gpu_threading ? gParallelTests : gSerialTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -08001073 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein21eaf3b2016-02-08 12:39:59 -08001074 gParallelTests.push(test);
mtklein82d28432015-01-15 12:46:02 -08001075 }
mtklein748ca3b2015-01-15 10:56:12 -08001076 }
1077}
1078
mtklein21eaf3b2016-02-08 12:39:59 -08001079static void run_test(skiatest::Test test) {
halcanary87f3ba42015-01-20 09:30:20 -08001080 struct : public skiatest::Reporter {
mtklein36352bf2015-03-25 18:17:31 -07001081 void reportFailed(const skiatest::Failure& failure) override {
halcanary87f3ba42015-01-20 09:30:20 -08001082 fail(failure.toString());
1083 JsonWriter::AddTestFailure(failure);
1084 }
mtklein36352bf2015-03-25 18:17:31 -07001085 bool allowExtendedTest() const override {
halcanary87f3ba42015-01-20 09:30:20 -08001086 return FLAGS_pathOpsExtended;
1087 }
mtklein36352bf2015-03-25 18:17:31 -07001088 bool verbose() const override { return FLAGS_veryVerbose; }
halcanary87f3ba42015-01-20 09:30:20 -08001089 } reporter;
djsollen824996a2015-06-12 12:06:22 -07001090
1091 SkString note;
mtklein21eaf3b2016-02-08 12:39:59 -08001092 SkString whyBlacklisted = is_blacklisted("_", "tests", "_", test.name);
djsollen824996a2015-06-12 12:06:22 -07001093 if (!whyBlacklisted.isEmpty()) {
1094 note.appendf(" (--blacklist %s)", whyBlacklisted.c_str());
1095 }
1096
mtkleinf27f08b2015-11-03 06:54:24 -08001097 auto timerStart = now_ms();
djsollen824996a2015-06-12 12:06:22 -07001098 if (!FLAGS_dryRun && whyBlacklisted.isEmpty()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001099 start("unit", "test", "", test.name);
mtklein55e88b22015-01-21 15:50:13 -08001100 GrContextFactory factory;
mtklein21eaf3b2016-02-08 12:39:59 -08001101 test.proc(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -08001102 }
mtklein21eaf3b2016-02-08 12:39:59 -08001103 done(now_ms()-timerStart, "unit", "test", "", test.name, note, "");
mtklein748ca3b2015-01-15 10:56:12 -08001104}
1105
1106/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1107
mtkleinafae30a2016-02-24 12:28:32 -08001108DEFINE_int32(status_sec, 15, "Print status this often (and if we crash).");
1109
1110SkThread* start_status_thread() {
1111 auto thread = new SkThread([] (void*) {
1112 for (;;) {
1113 print_status();
1114 #if defined(SK_BUILD_FOR_WIN)
1115 Sleep(FLAGS_status_sec * 1000);
1116 #else
1117 sleep(FLAGS_status_sec);
1118 #endif
mtklein2e1c47e2015-03-12 07:16:56 -07001119 }
mtkleinafae30a2016-02-24 12:28:32 -08001120 });
1121 thread->start();
1122 return thread;
mtkleinde6fc2b2015-03-12 06:28:54 -07001123}
1124
caryclark83ca6282015-06-10 09:31:09 -07001125#define PORTABLE_FONT_PREFIX "Toy Liberation "
1126
1127static SkTypeface* create_from_name(const char familyName[], SkTypeface::Style style) {
1128 if (familyName && strlen(familyName) > sizeof(PORTABLE_FONT_PREFIX)
1129 && !strncmp(familyName, PORTABLE_FONT_PREFIX, sizeof(PORTABLE_FONT_PREFIX) - 1)) {
caryclark1818acb2015-07-24 12:09:25 -07001130 return sk_tool_utils::create_portable_typeface(familyName, style);
caryclark83ca6282015-06-10 09:31:09 -07001131 }
halcanary96fcdcc2015-08-27 07:41:13 -07001132 return nullptr;
caryclark83ca6282015-06-10 09:31:09 -07001133}
1134
1135#undef PORTABLE_FONT_PREFIX
1136
1137extern SkTypeface* (*gCreateTypefaceDelegate)(const char [], SkTypeface::Style );
1138
jcgregorio3b27ade2014-11-13 08:06:40 -08001139int dm_main();
caryclark17f0b6d2014-07-22 10:15:34 -07001140int dm_main() {
mtklein246ba3a2016-02-23 10:39:36 -08001141 setup_crash_handler();
mtkleinafae30a2016-02-24 12:28:32 -08001142
mtklein5286f022016-01-22 08:18:14 -08001143 JsonWriter::DumpJson(); // It's handy for the bots to assume this is ~never missing.
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +00001144 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -07001145 SkTaskGroup::Enabler enabled(FLAGS_threads);
caryclark83ca6282015-06-10 09:31:09 -07001146 gCreateTypefaceDelegate = &create_from_name;
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +00001147
mtklein62bd1a62015-01-27 14:46:26 -08001148 gather_gold();
borenet09ed4802015-04-03 14:15:33 -07001149 gather_uninteresting_hashes();
mtklein62bd1a62015-01-27 14:46:26 -08001150
scroggo86737142016-02-03 12:19:11 -08001151 if (!gather_srcs()) {
1152 return 1;
1153 }
mtklein748ca3b2015-01-15 10:56:12 -08001154 gather_sinks();
1155 gather_tests();
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +00001156
mtklein21eaf3b2016-02-08 12:39:59 -08001157 gPending = gSrcs.count() * gSinks.count() + gParallelTests.count() + gSerialTests.count();
mtkleinafae30a2016-02-24 12:28:32 -08001158 SkDebugf("%d srcs * %d sinks + %d tests == %d tasks",
mtklein21eaf3b2016-02-08 12:39:59 -08001159 gSrcs.count(), gSinks.count(), gParallelTests.count() + gSerialTests.count(), gPending);
mtkleinafae30a2016-02-24 12:28:32 -08001160 SkAutoTDelete<SkThread> statusThread(start_status_thread());
mtklein748ca3b2015-01-15 10:56:12 -08001161
mtklein21eaf3b2016-02-08 12:39:59 -08001162 // Kick off as much parallel work as we can, making note of any serial work we'll need to do.
1163 SkTaskGroup parallel;
1164 SkTArray<Task> serial;
1165
1166 for (auto& sink : gSinks)
1167 for (auto& src : gSrcs) {
1168 Task task(src, sink);
1169 if (src->serial() || sink->serial()) {
1170 serial.push_back(task);
1171 } else {
1172 parallel.add([task] { Task::Run(task); });
mtklein748ca3b2015-01-15 10:56:12 -08001173 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001174 }
mtklein21eaf3b2016-02-08 12:39:59 -08001175 for (auto test : gParallelTests) {
1176 parallel.add([test] { run_test(test); });
mtklein55e88b22015-01-21 15:50:13 -08001177 }
mtklein21eaf3b2016-02-08 12:39:59 -08001178
1179 // With the parallel work running, run serial tasks and tests here on main thread.
1180 for (auto task : serial) { Task::Run(task); }
1181 for (auto test : gSerialTests) { run_test(test); }
1182
1183 // Wait for any remaining parallel work to complete (including any spun off of serial tasks).
1184 parallel.wait();
mtkleincd50bca2016-01-05 06:20:20 -08001185 gDefinitelyThreadSafeWork.wait();
1186
mtklein748ca3b2015-01-15 10:56:12 -08001187 // At this point we're back in single-threaded land.
caryclarkf53ce802015-06-15 06:48:30 -07001188 sk_tool_utils::release_portable_typefaces();
mtklein@google.comd36522d2013-10-16 13:02:15 +00001189
mtkleinbc4e0032015-12-09 08:37:35 -08001190 if (FLAGS_verbose && gNoteTally.count() > 0) {
1191 SkDebugf("\nNote tally:\n");
1192 gNoteTally.foreach([](const SkString& note, int* tally) {
1193 SkDebugf("%dx\t%s\n", *tally, note.c_str());
1194 });
1195 }
1196
mtklein2f64eec2015-01-15 14:20:41 -08001197 SkDebugf("\n");
mtklein748ca3b2015-01-15 10:56:12 -08001198 if (gFailures.count() > 0) {
1199 SkDebugf("Failures:\n");
1200 for (int i = 0; i < gFailures.count(); i++) {
mtklein9dc09102015-01-15 15:47:33 -08001201 SkDebugf("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001202 }
1203 SkDebugf("%d failures\n", gFailures.count());
1204 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001205 }
mtkleinafae30a2016-02-24 12:28:32 -08001206
1207#ifdef SK_PDF_IMAGE_STATS
halcanary7a14b312015-10-01 07:28:13 -07001208 SkPDFImageDumpStats();
mtkleinafae30a2016-02-24 12:28:32 -08001209#endif // SK_PDF_IMAGE_STATS
1210
1211 print_status();
scroggo4d9eaea2016-01-29 07:48:33 -08001212 SkDebugf("Finished!\n");
mtklein748ca3b2015-01-15 10:56:12 -08001213 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +00001214}
jcgregorio3b27ade2014-11-13 08:06:40 -08001215
kkinnunen179a8f52015-11-20 13:32:24 -08001216// TODO: currently many GPU tests are declared outside SK_SUPPORT_GPU guards.
1217// Thus we export the empty RunWithGPUTestContexts when SK_SUPPORT_GPU=0.
1218namespace skiatest {
1219namespace {
1220typedef void(*TestWithGrContext)(skiatest::Reporter*, GrContext*);
1221typedef void(*TestWithGrContextAndGLContext)(skiatest::Reporter*, GrContext*, SkGLContext*);
1222#if SK_SUPPORT_GPU
1223template<typename T>
kkinnunen34058002016-01-06 23:49:30 -08001224void call_test(T test, skiatest::Reporter* reporter, const GrContextFactory::ContextInfo& context);
kkinnunen179a8f52015-11-20 13:32:24 -08001225template<>
1226void call_test(TestWithGrContext test, skiatest::Reporter* reporter,
kkinnunen34058002016-01-06 23:49:30 -08001227 const GrContextFactory::ContextInfo& context) {
1228 test(reporter, context.fGrContext);
kkinnunen179a8f52015-11-20 13:32:24 -08001229}
1230template<>
1231void call_test(TestWithGrContextAndGLContext test, skiatest::Reporter* reporter,
kkinnunen34058002016-01-06 23:49:30 -08001232 const GrContextFactory::ContextInfo& context) {
1233 test(reporter, context.fGrContext, context.fGLContext);
kkinnunen179a8f52015-11-20 13:32:24 -08001234}
1235#endif
1236} // namespace
1237
kkinnunen179a8f52015-11-20 13:32:24 -08001238template<typename T>
1239void RunWithGPUTestContexts(T test, GPUTestContexts testContexts, Reporter* reporter,
1240 GrContextFactory* factory) {
1241#if SK_SUPPORT_GPU
kkinnunen3e980c32015-12-23 01:33:00 -08001242 // Iterate over context types, except use "native" instead of explicitly trying OpenGL and
1243 // OpenGL ES. Do not use GLES on desktop, since tests do not account for not fixing
1244 // http://skbug.com/2809
1245 GrContextFactory::GLContextType contextTypes[] = {
1246 GrContextFactory::kNative_GLContextType,
1247#if SK_ANGLE
1248#ifdef SK_BUILD_FOR_WIN
1249 GrContextFactory::kANGLE_GLContextType,
1250#endif
1251 GrContextFactory::kANGLE_GL_GLContextType,
1252#endif
1253#if SK_COMMAND_BUFFER
1254 GrContextFactory::kCommandBuffer_GLContextType,
1255#endif
1256#if SK_MESA
1257 GrContextFactory::kMESA_GLContextType,
1258#endif
1259 GrContextFactory::kNull_GLContextType,
1260 GrContextFactory::kDebug_GLContextType,
1261 };
1262 static_assert(SK_ARRAY_COUNT(contextTypes) == GrContextFactory::kGLContextTypeCnt - 2,
1263 "Skipping unexpected GLContextType for GPU tests");
1264
1265 for (auto& contextType : contextTypes) {
kkinnunen179a8f52015-11-20 13:32:24 -08001266 int contextSelector = kNone_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001267 if (GrContextFactory::IsRenderingGLContext(contextType)) {
kkinnunen179a8f52015-11-20 13:32:24 -08001268 contextSelector |= kAllRendering_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001269 } else if (contextType == GrContextFactory::kNative_GLContextType) {
kkinnunen179a8f52015-11-20 13:32:24 -08001270 contextSelector |= kNative_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001271 } else if (contextType == GrContextFactory::kNull_GLContextType) {
kkinnunen179a8f52015-11-20 13:32:24 -08001272 contextSelector |= kNull_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001273 } else if (contextType == GrContextFactory::kDebug_GLContextType) {
kkinnunen55eeae92015-12-10 23:19:29 -08001274 contextSelector |= kDebug_GPUTestContexts;
kkinnunen179a8f52015-11-20 13:32:24 -08001275 }
1276 if ((testContexts & contextSelector) == 0) {
1277 continue;
1278 }
kkinnunen34058002016-01-06 23:49:30 -08001279 GrContextFactory::ContextInfo context = factory->getContextInfo(contextType);
1280 if (context.fGrContext) {
kkinnunen5219fd92015-12-10 06:28:13 -08001281 call_test(test, reporter, context);
1282 }
kkinnunen34058002016-01-06 23:49:30 -08001283 context = factory->getContextInfo(contextType,
1284 GrContextFactory::kEnableNVPR_GLContextOptions);
1285 if (context.fGrContext) {
kkinnunen179a8f52015-11-20 13:32:24 -08001286 call_test(test, reporter, context);
1287 }
1288 }
1289#endif
1290}
1291
1292template
1293void RunWithGPUTestContexts<TestWithGrContext>(TestWithGrContext test,
1294 GPUTestContexts testContexts,
1295 Reporter* reporter,
1296 GrContextFactory* factory);
1297template
1298void RunWithGPUTestContexts<TestWithGrContextAndGLContext>(TestWithGrContextAndGLContext test,
1299 GPUTestContexts testContexts,
1300 Reporter* reporter,
1301 GrContextFactory* factory);
1302} // namespace skiatest
1303
borenet48087572015-04-02 12:16:36 -07001304#if !defined(SK_BUILD_FOR_IOS)
jcgregorio3b27ade2014-11-13 08:06:40 -08001305int main(int argc, char** argv) {
1306 SkCommandLineFlags::Parse(argc, argv);
1307 return dm_main();
1308}
1309#endif