blob: 59c7def4d67d0dbee83fd9d3db10686f5a490a6b [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"
halcanary7d571242016-02-24 17:59:16 -080013#include "Resources.h"
mtklein748ca3b2015-01-15 10:56:12 -080014#include "SkBBHFactory.h"
mtklein62bd1a62015-01-27 14:46:26 -080015#include "SkChecksum.h"
scroggocc2feb12015-08-14 08:32:46 -070016#include "SkCodec.h"
mtklein27c3fdd2016-02-26 14:43:21 -080017#include "SkColorPriv.h"
caryclark17f0b6d2014-07-22 10:15:34 -070018#include "SkCommonFlags.h"
kkinnunen3e980c32015-12-23 01:33:00 -080019#include "SkCommonFlagsConfig.h"
caryclark83ca6282015-06-10 09:31:09 -070020#include "SkFontMgr.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +000021#include "SkGraphics.h"
mtklein27c3fdd2016-02-26 14:43:21 -080022#include "SkHalf.h"
mtklein748ca3b2015-01-15 10:56:12 -080023#include "SkMD5.h"
mtklein1b249332015-07-07 12:21:21 -070024#include "SkMutex.h"
mtklein1d0f1642014-09-08 08:05:18 -070025#include "SkOSFile.h"
mtklein27c3fdd2016-02-26 14:43:21 -080026#include "SkPM4fPriv.h"
mtklein246ba3a2016-02-23 10:39:36 -080027#include "SkSpinlock.h"
mtkleina82f5622015-02-20 12:30:19 -080028#include "SkTHash.h"
mtklein406654b2014-09-03 15:34:37 -070029#include "SkTaskGroup.h"
mtkleinde6fc2b2015-03-12 06:28:54 -070030#include "SkThreadUtils.h"
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +000031#include "Test.h"
mtklein748ca3b2015-01-15 10:56:12 -080032#include "Timer.h"
caryclark83ca6282015-06-10 09:31:09 -070033#include "sk_tool_utils.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +000034
halcanary7a14b312015-10-01 07:28:13 -070035#ifdef SK_PDF_IMAGE_STATS
36extern void SkPDFImageDumpStats();
37#endif
38
mtkleina5114d72015-08-24 13:27:01 -070039#include "png.h"
40
bungeman60e0fee2015-08-26 05:15:46 -070041#include <stdlib.h>
42
scroggo27a58342015-10-28 08:56:41 -070043#ifndef SK_BUILD_FOR_WIN32
44 #include <unistd.h>
45#endif
46
djsollen54416de2015-04-03 07:24:48 -070047DEFINE_string(src, "tests gm skp image", "Source types to test.");
mtklein748ca3b2015-01-15 10:56:12 -080048DEFINE_bool(nameByHash, false,
49 "If true, write to FLAGS_writePath[0]/<hash>.png instead of "
djsollen54416de2015-04-03 07:24:48 -070050 "to FLAGS_writePath[0]/<config>/<sourceType>/<sourceOptions>/<name>.png");
mtklein748ca3b2015-01-15 10:56:12 -080051DEFINE_bool2(pathOpsExtended, x, false, "Run extended pathOps tests.");
mtkleind603b222015-02-17 11:13:33 -080052DEFINE_string(matrix, "1 0 0 1",
53 "2x2 scale+skew matrix to apply or upright when using "
54 "'matrix' or 'upright' in config.");
mtklein82d28432015-01-15 12:46:02 -080055DEFINE_bool(gpu_threading, false, "Allow GPU work to run on multiple threads?");
mtklein@google.comd36522d2013-10-16 13:02:15 +000056
mtkleina2ef6422015-01-15 13:44:22 -080057DEFINE_string(blacklist, "",
djsollen54416de2015-04-03 07:24:48 -070058 "Space-separated config/src/srcOptions/name quadruples to blacklist. '_' matches anything. E.g. \n"
59 "'--blacklist gpu skp _ _' will blacklist all SKPs drawn into the gpu config.\n"
60 "'--blacklist gpu skp _ _ 8888 gm _ aarects' will also blacklist the aarects GM on 8888.");
mtkleina2ef6422015-01-15 13:44:22 -080061
mtklein62bd1a62015-01-27 14:46:26 -080062DEFINE_string2(readPath, r, "", "If set check for equality with golden results in this directory.");
63
borenet09ed4802015-04-03 14:15:33 -070064DEFINE_string(uninterestingHashesFile, "",
65 "File containing a list of uninteresting hashes. If a result hashes to something in "
66 "this list, no image is written for that result.");
67
mtklein6393c062015-04-27 08:45:01 -070068DEFINE_int32(shards, 1, "We're splitting source data into this many shards.");
69DEFINE_int32(shard, 0, "Which shard do I run?");
scroggoe4499842016-02-25 11:03:47 -080070DEFINE_bool(simpleCodec, false, "Only decode images to native scale");
mtklein6393c062015-04-27 08:45:01 -070071
mtklein748ca3b2015-01-15 10:56:12 -080072using namespace DM;
mtklein@google.comd36522d2013-10-16 13:02:15 +000073
mtklein748ca3b2015-01-15 10:56:12 -080074/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
75
76SK_DECLARE_STATIC_MUTEX(gFailuresMutex);
77static SkTArray<SkString> gFailures;
78
mtklein3eed7dd2016-02-24 19:07:07 -080079static void fail(const SkString& err) {
mtklein748ca3b2015-01-15 10:56:12 -080080 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 -070085
mtklein246ba3a2016-02-23 10:39:36 -080086// We use a spinlock to make locking this in a signal handler _somewhat_ safe.
mtkleinafae30a2016-02-24 12:28:32 -080087SK_DECLARE_STATIC_SPINLOCK(gMutex);
mtklein3eed7dd2016-02-24 19:07:07 -080088static int32_t gPending;
89static SkTArray<SkString> gRunning;
mtklein748ca3b2015-01-15 10:56:12 -080090
mtklein3eed7dd2016-02-24 19:07:07 -080091static void done(const char* config, const char* src, const char* srcOptions, const char* name) {
92 SkString id = SkStringPrintf("%s %s %s %s", config, src, srcOptions, name);
mtkleinafae30a2016-02-24 12:28:32 -080093 int pending;
mtkleinb37cb412015-03-18 05:27:14 -070094 {
mtkleinafae30a2016-02-24 12:28:32 -080095 SkAutoTAcquire<SkPODSpinlock> lock(gMutex);
mtkleinb37cb412015-03-18 05:27:14 -070096 for (int i = 0; i < gRunning.count(); i++) {
97 if (gRunning[i] == id) {
98 gRunning.removeShuffle(i);
99 break;
100 }
101 }
mtkleinafae30a2016-02-24 12:28:32 -0800102 pending = --gPending;
reed50bc0512015-05-19 14:13:31 -0700103 }
mtkleina17241b2015-01-23 05:48:00 -0800104 // We write our dm.json file every once in a while in case we crash.
105 // Notice this also handles the final dm.json when pending == 0.
106 if (pending % 500 == 0) {
107 JsonWriter::DumpJson();
108 }
mtklein@google.comd36522d2013-10-16 13:02:15 +0000109}
110
mtklein3eed7dd2016-02-24 19:07:07 -0800111static void start(const char* config, const char* src, const char* srcOptions, const char* name) {
112 SkString id = SkStringPrintf("%s %s %s %s", config, src, srcOptions, name);
mtkleinafae30a2016-02-24 12:28:32 -0800113 SkAutoTAcquire<SkPODSpinlock> lock(gMutex);
mtkleinb37cb412015-03-18 05:27:14 -0700114 gRunning.push_back(id);
115}
116
mtkleinafae30a2016-02-24 12:28:32 -0800117static void print_status() {
118 static SkMSec start_ms = SkTime::GetMSecs();
119
120 int curr = sk_tools::getCurrResidentSetSizeMB(),
121 peak = sk_tools::getMaxResidentSetSizeMB();
122 SkString elapsed = HumanizeMs(SkTime::GetMSecs() - start_ms);
123
124 SkAutoTAcquire<SkPODSpinlock> lock(gMutex);
125 SkDebugf("\n%s elapsed, %d active, %d queued, %dMB RAM, %dMB peak\n",
126 elapsed.c_str(), gRunning.count(), gPending - gRunning.count(), curr, peak);
127 for (auto& task : gRunning) {
128 SkDebugf("\t%s\n", task.c_str());
129 }
130}
131
mtklein246ba3a2016-02-23 10:39:36 -0800132#if defined(SK_BUILD_FOR_WIN32)
133 static void setup_crash_handler() {
134 // TODO: custom crash handler like below to print out what was running
135 SetupCrashHandler();
136 }
137
138#else
139 #include <signal.h>
140 static void setup_crash_handler() {
141 const int kSignals[] = { SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGSEGV };
142 for (int sig : kSignals) {
143 signal(sig, [](int sig) {
mtkleinafae30a2016-02-24 12:28:32 -0800144 SkDebugf("\nCaught signal %d [%s].\n", sig, strsignal(sig));
145 print_status();
mtklein246ba3a2016-02-23 10:39:36 -0800146 });
147 }
148 }
149#endif
150
mtklein748ca3b2015-01-15 10:56:12 -0800151/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
mtklein114c3cd2015-01-15 10:15:02 -0800152
mtklein62bd1a62015-01-27 14:46:26 -0800153struct Gold : public SkString {
mtkleina82f5622015-02-20 12:30:19 -0800154 Gold() : SkString("") {}
mtklein3eed7dd2016-02-24 19:07:07 -0800155 Gold(const SkString& sink, const SkString& src,
156 const SkString& srcOptions, const SkString& name,
157 const SkString& md5)
mtklein62bd1a62015-01-27 14:46:26 -0800158 : SkString("") {
159 this->append(sink);
160 this->append(src);
djsollen54416de2015-04-03 07:24:48 -0700161 this->append(srcOptions);
mtklein62bd1a62015-01-27 14:46:26 -0800162 this->append(name);
163 this->append(md5);
mtklein62bd1a62015-01-27 14:46:26 -0800164 }
mtkleinc8d1dd42015-10-15 12:23:01 -0700165 struct Hash {
166 uint32_t operator()(const Gold& g) const {
167 return SkGoodHash()((const SkString&)g);
168 }
169 };
mtklein62bd1a62015-01-27 14:46:26 -0800170};
mtkleina82f5622015-02-20 12:30:19 -0800171static SkTHashSet<Gold, Gold::Hash> gGold;
mtklein62bd1a62015-01-27 14:46:26 -0800172
173static void add_gold(JsonWriter::BitmapResult r) {
djsollen54416de2015-04-03 07:24:48 -0700174 gGold.add(Gold(r.config, r.sourceType, r.sourceOptions, r.name, r.md5));
mtklein62bd1a62015-01-27 14:46:26 -0800175}
176
177static void gather_gold() {
178 if (!FLAGS_readPath.isEmpty()) {
179 SkString path(FLAGS_readPath[0]);
180 path.append("/dm.json");
181 if (!JsonWriter::ReadJson(path.c_str(), add_gold)) {
182 fail(SkStringPrintf("Couldn't read %s for golden results.", path.c_str()));
183 }
184 }
185}
186
187/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
188
borenet09ed4802015-04-03 14:15:33 -0700189static SkTHashSet<SkString> gUninterestingHashes;
190
191static void gather_uninteresting_hashes() {
192 if (!FLAGS_uninterestingHashesFile.isEmpty()) {
193 SkAutoTUnref<SkData> data(SkData::NewFromFileName(FLAGS_uninterestingHashesFile[0]));
mtkleincc334b32015-09-22 11:43:53 -0700194 if (!data) {
195 SkDebugf("WARNING: unable to read uninteresting hashes from %s\n",
196 FLAGS_uninterestingHashesFile[0]);
197 return;
198 }
borenet09ed4802015-04-03 14:15:33 -0700199 SkTArray<SkString> hashes;
200 SkStrSplit((const char*)data->data(), "\n", &hashes);
201 for (const SkString& hash : hashes) {
202 gUninterestingHashes.add(hash);
203 }
mtklein136baaa2016-02-03 11:21:45 -0800204 SkDebugf("FYI: loaded %d distinct uninteresting hashes from %d lines\n",
205 gUninterestingHashes.count(), hashes.count());
borenet09ed4802015-04-03 14:15:33 -0700206 }
207}
208
209/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
210
mtkleine0effd62015-07-29 06:37:28 -0700211struct TaggedSrc : public SkAutoTDelete<Src> {
mtklein3eed7dd2016-02-24 19:07:07 -0800212 SkString tag;
213 SkString options;
mtkleine0effd62015-07-29 06:37:28 -0700214};
215
216struct TaggedSink : public SkAutoTDelete<Sink> {
kkinnunen3e980c32015-12-23 01:33:00 -0800217 SkString tag;
djsollen54416de2015-04-03 07:24:48 -0700218};
mtklein748ca3b2015-01-15 10:56:12 -0800219
220static const bool kMemcpyOK = true;
221
mtkleine0effd62015-07-29 06:37:28 -0700222static SkTArray<TaggedSrc, kMemcpyOK> gSrcs;
223static SkTArray<TaggedSink, kMemcpyOK> gSinks;
mtklein748ca3b2015-01-15 10:56:12 -0800224
mtklein6393c062015-04-27 08:45:01 -0700225static bool in_shard() {
226 static int N = 0;
227 return N++ % FLAGS_shards == FLAGS_shard;
228}
229
mtklein3eed7dd2016-02-24 19:07:07 -0800230static void push_src(const char* tag, ImplicitString options, Src* s) {
mtklein748ca3b2015-01-15 10:56:12 -0800231 SkAutoTDelete<Src> src(s);
mtklein6393c062015-04-27 08:45:01 -0700232 if (in_shard() &&
mtklein3eed7dd2016-02-24 19:07:07 -0800233 FLAGS_src.contains(tag) &&
mtklein748ca3b2015-01-15 10:56:12 -0800234 !SkCommandLineFlags::ShouldSkip(FLAGS_match, src->name().c_str())) {
mtkleine0effd62015-07-29 06:37:28 -0700235 TaggedSrc& s = gSrcs.push_back();
mtklein748ca3b2015-01-15 10:56:12 -0800236 s.reset(src.detach());
237 s.tag = tag;
djsollen54416de2015-04-03 07:24:48 -0700238 s.options = options;
mtklein114c3cd2015-01-15 10:15:02 -0800239 }
mtklein748ca3b2015-01-15 10:56:12 -0800240}
mtklein114c3cd2015-01-15 10:15:02 -0800241
msarett9e707a02015-09-01 14:57:57 -0700242static void push_codec_src(Path path, CodecSrc::Mode mode, CodecSrc::DstColorType dstColorType,
scroggoc5560be2016-02-03 09:42:42 -0800243 SkAlphaType dstAlphaType, float scale) {
scroggoe4499842016-02-25 11:03:47 -0800244 if (FLAGS_simpleCodec) {
245 if (mode != CodecSrc::kCodec_Mode || dstColorType != CodecSrc::kGetFromCanvas_DstColorType
246 || scale != 1.0f)
247 // Only decode in the simple case.
248 return;
249 }
msarett9e707a02015-09-01 14:57:57 -0700250 SkString folder;
251 switch (mode) {
252 case CodecSrc::kCodec_Mode:
253 folder.append("codec");
254 break;
msarettbb25b532016-01-13 09:31:39 -0800255 case CodecSrc::kCodecZeroInit_Mode:
256 folder.append("codec_zero_init");
257 break;
msarett9e707a02015-09-01 14:57:57 -0700258 case CodecSrc::kScanline_Mode:
259 folder.append("scanline");
260 break;
msarett9e707a02015-09-01 14:57:57 -0700261 case CodecSrc::kStripe_Mode:
262 folder.append("stripe");
263 break;
msarett91c22b22016-02-22 12:27:46 -0800264 case CodecSrc::kCroppedScanline_Mode:
265 folder.append("crop");
266 break;
msarett9e707a02015-09-01 14:57:57 -0700267 case CodecSrc::kSubset_Mode:
msarett36c37962015-09-02 13:20:52 -0700268 folder.append("codec_subset");
msarett9e707a02015-09-01 14:57:57 -0700269 break;
msarettb714fb02016-01-22 14:46:42 -0800270 case CodecSrc::kGen_Mode:
271 folder.append("gen");
272 break;
msarett9e707a02015-09-01 14:57:57 -0700273 }
274
275 switch (dstColorType) {
276 case CodecSrc::kGrayscale_Always_DstColorType:
277 folder.append("_kGray8");
278 break;
279 case CodecSrc::kIndex8_Always_DstColorType:
280 folder.append("_kIndex8");
281 break;
282 default:
283 break;
284 }
285
scroggoc5560be2016-02-03 09:42:42 -0800286 switch (dstAlphaType) {
287 case kOpaque_SkAlphaType:
288 folder.append("_opaque");
289 break;
290 case kPremul_SkAlphaType:
291 folder.append("_premul");
292 break;
293 case kUnpremul_SkAlphaType:
294 folder.append("_unpremul");
295 break;
296 default:
297 break;
298 }
299
msarett9e707a02015-09-01 14:57:57 -0700300 if (1.0f != scale) {
301 folder.appendf("_%.3f", scale);
302 }
303
scroggoc5560be2016-02-03 09:42:42 -0800304 CodecSrc* src = new CodecSrc(path, mode, dstColorType, dstAlphaType, scale);
msarett9e707a02015-09-01 14:57:57 -0700305 push_src("image", folder, src);
306}
307
msarett3d9d7a72015-10-21 10:27:10 -0700308static void push_android_codec_src(Path path, AndroidCodecSrc::Mode mode,
scroggoc5560be2016-02-03 09:42:42 -0800309 CodecSrc::DstColorType dstColorType, SkAlphaType dstAlphaType, int sampleSize) {
msarett3d9d7a72015-10-21 10:27:10 -0700310 SkString folder;
311 switch (mode) {
312 case AndroidCodecSrc::kFullImage_Mode:
313 folder.append("scaled_codec");
314 break;
315 case AndroidCodecSrc::kDivisor_Mode:
316 folder.append("scaled_codec_divisor");
317 break;
318 }
319
320 switch (dstColorType) {
321 case CodecSrc::kGrayscale_Always_DstColorType:
322 folder.append("_kGray8");
323 break;
324 case CodecSrc::kIndex8_Always_DstColorType:
325 folder.append("_kIndex8");
326 break;
327 default:
328 break;
329 }
330
scroggoc5560be2016-02-03 09:42:42 -0800331 switch (dstAlphaType) {
332 case kOpaque_SkAlphaType:
333 folder.append("_opaque");
334 break;
335 case kPremul_SkAlphaType:
336 folder.append("_premul");
337 break;
msarett9e9444c2016-02-03 12:39:10 -0800338 case kUnpremul_SkAlphaType:
339 folder.append("_unpremul");
340 break;
scroggoc5560be2016-02-03 09:42:42 -0800341 default:
342 break;
343 }
344
msarett3d9d7a72015-10-21 10:27:10 -0700345 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800346 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700347 }
348
scroggoc5560be2016-02-03 09:42:42 -0800349 AndroidCodecSrc* src = new AndroidCodecSrc(path, mode, dstColorType, dstAlphaType, sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700350 push_src("image", folder, src);
351}
352
msarett438b2ad2015-04-09 12:43:10 -0700353static void push_codec_srcs(Path path) {
354 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
355 if (!encoded) {
356 SkDebugf("Couldn't read %s.", path.c_str());
357 return;
358 }
359 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
halcanary96fcdcc2015-08-27 07:41:13 -0700360 if (nullptr == codec.get()) {
msarett438b2ad2015-04-09 12:43:10 -0700361 SkDebugf("Couldn't create codec for %s.", path.c_str());
362 return;
363 }
364
msarett36c37962015-09-02 13:20:52 -0700365 // Native Scales
emmaleer8f4ba762015-08-14 07:44:46 -0700366 // 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 -0700367 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 -0700368
msarett91c22b22016-02-22 12:27:46 -0800369 SkTArray<CodecSrc::Mode> nativeModes;
370 nativeModes.push_back(CodecSrc::kCodec_Mode);
371 nativeModes.push_back(CodecSrc::kCodecZeroInit_Mode);
372 nativeModes.push_back(CodecSrc::kGen_Mode);
373 switch (codec->getEncodedFormat()) {
374 case SkEncodedFormat::kJPEG_SkEncodedFormat:
375 nativeModes.push_back(CodecSrc::kScanline_Mode);
376 nativeModes.push_back(CodecSrc::kStripe_Mode);
377 nativeModes.push_back(CodecSrc::kCroppedScanline_Mode);
378 break;
379 case SkEncodedFormat::kWEBP_SkEncodedFormat:
380 nativeModes.push_back(CodecSrc::kSubset_Mode);
381 break;
msarettb65e6042016-02-23 05:37:25 -0800382 case SkEncodedFormat::kRAW_SkEncodedFormat:
383 break;
msarett91c22b22016-02-22 12:27:46 -0800384 default:
385 nativeModes.push_back(CodecSrc::kScanline_Mode);
386 nativeModes.push_back(CodecSrc::kStripe_Mode);
387 break;
388 }
msarett9e707a02015-09-01 14:57:57 -0700389
msarett91c22b22016-02-22 12:27:46 -0800390 SkTArray<CodecSrc::DstColorType> colorTypes;
391 colorTypes.push_back(CodecSrc::kGetFromCanvas_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700392 switch (codec->getInfo().colorType()) {
393 case kGray_8_SkColorType:
msarett91c22b22016-02-22 12:27:46 -0800394 colorTypes.push_back(CodecSrc::kGrayscale_Always_DstColorType);
msarett55f7bdd2016-02-16 13:24:54 -0800395 if (kWBMP_SkEncodedFormat == codec->getEncodedFormat()) {
msarett91c22b22016-02-22 12:27:46 -0800396 colorTypes.push_back(CodecSrc::kIndex8_Always_DstColorType);
msarett55f7bdd2016-02-16 13:24:54 -0800397 }
msarett36c37962015-09-02 13:20:52 -0700398 break;
399 case kIndex_8_SkColorType:
msarett91c22b22016-02-22 12:27:46 -0800400 colorTypes.push_back(CodecSrc::kIndex8_Always_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700401 break;
402 default:
msarett36c37962015-09-02 13:20:52 -0700403 break;
404 }
msarett9e707a02015-09-01 14:57:57 -0700405
scroggoc5560be2016-02-03 09:42:42 -0800406 SkTArray<SkAlphaType> alphaModes;
407 alphaModes.push_back(kPremul_SkAlphaType);
msarett9e9444c2016-02-03 12:39:10 -0800408 alphaModes.push_back(kUnpremul_SkAlphaType);
scroggoc5560be2016-02-03 09:42:42 -0800409 if (codec->getInfo().alphaType() == kOpaque_SkAlphaType) {
410 alphaModes.push_back(kOpaque_SkAlphaType);
411 }
msarettb714fb02016-01-22 14:46:42 -0800412
413 for (CodecSrc::Mode mode : nativeModes) {
414 // SkCodecImageGenerator only runs for the default colorType
415 // recommended by SkCodec. There is no need to generate multiple
416 // tests for different colorTypes.
417 // TODO (msarett): Add scaling support to SkCodecImageGenerator.
418 if (CodecSrc::kGen_Mode == mode) {
419 // FIXME: The gpu backend does not draw kGray sources correctly. (skbug.com/4822)
420 if (kGray_8_SkColorType != codec->getInfo().colorType()) {
scroggoc5560be2016-02-03 09:42:42 -0800421 push_codec_src(path, mode, CodecSrc::kGetFromCanvas_DstColorType,
422 codec->getInfo().alphaType(), 1.0f);
msarettb714fb02016-01-22 14:46:42 -0800423 }
424 continue;
425 }
426
427 for (float scale : nativeScales) {
msarett91c22b22016-02-22 12:27:46 -0800428 for (CodecSrc::DstColorType colorType : colorTypes) {
scroggoc5560be2016-02-03 09:42:42 -0800429 for (SkAlphaType alphaType : alphaModes) {
msarett91c22b22016-02-22 12:27:46 -0800430 // Only test kCroppedScanline_Mode when the alpha type is opaque. The test is
431 // slow and won't be interestingly different with different alpha types.
432 if (CodecSrc::kCroppedScanline_Mode == mode &&
433 kOpaque_SkAlphaType != alphaType) {
434 continue;
435 }
436
437 push_codec_src(path, mode, colorType, alphaType, scale);
scroggoc5560be2016-02-03 09:42:42 -0800438 }
msarett9e707a02015-09-01 14:57:57 -0700439 }
440 }
msarett0a242972015-06-11 14:27:27 -0700441 }
msarett36c37962015-09-02 13:20:52 -0700442
scroggoe4499842016-02-25 11:03:47 -0800443 if (FLAGS_simpleCodec) {
444 return;
445 }
446
halcanary6950de62015-11-07 05:29:00 -0800447 // https://bug.skia.org/4428
msarett33c76232015-11-16 13:43:40 -0800448 bool subset = false;
449 // The following image types are supported by BitmapRegionDecoder,
450 // so we will test full image decodes and subset decodes.
msarettbe8216a2015-12-04 08:00:50 -0800451 static const char* const exts[] = {
msarett3d9d7a72015-10-21 10:27:10 -0700452 "jpg", "jpeg", "png", "webp",
453 "JPG", "JPEG", "PNG", "WEBP",
454 };
msarettbe8216a2015-12-04 08:00:50 -0800455 for (const char* ext : exts) {
msarett3d9d7a72015-10-21 10:27:10 -0700456 if (path.endsWith(ext)) {
msarett33c76232015-11-16 13:43:40 -0800457 subset = true;
msarett3d9d7a72015-10-21 10:27:10 -0700458 break;
459 }
460 }
msarett33c76232015-11-16 13:43:40 -0800461
msarett3d9d7a72015-10-21 10:27:10 -0700462 const int sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
msarett36c37962015-09-02 13:20:52 -0700463
msarett3d9d7a72015-10-21 10:27:10 -0700464 for (int sampleSize : sampleSizes) {
msarett91c22b22016-02-22 12:27:46 -0800465 for (CodecSrc::DstColorType colorType : colorTypes) {
scroggoc5560be2016-02-03 09:42:42 -0800466 for (SkAlphaType alphaType : alphaModes) {
msarett91c22b22016-02-22 12:27:46 -0800467 push_android_codec_src(path, AndroidCodecSrc::kFullImage_Mode, colorType,
scroggoc5560be2016-02-03 09:42:42 -0800468 alphaType, sampleSize);
469 if (subset) {
msarett91c22b22016-02-22 12:27:46 -0800470 push_android_codec_src(path, AndroidCodecSrc::kDivisor_Mode, colorType,
scroggoc5560be2016-02-03 09:42:42 -0800471 alphaType, sampleSize);
472 }
msarett3d9d7a72015-10-21 10:27:10 -0700473 }
msarett36c37962015-09-02 13:20:52 -0700474 }
475 }
msarett438b2ad2015-04-09 12:43:10 -0700476}
477
msarett5cb48852015-11-06 08:56:32 -0800478static bool brd_color_type_supported(SkBitmapRegionDecoder::Strategy strategy,
msaretta5783ae2015-09-08 15:35:32 -0700479 CodecSrc::DstColorType dstColorType) {
480 switch (strategy) {
msarett5cb48852015-11-06 08:56:32 -0800481 case SkBitmapRegionDecoder::kCanvas_Strategy:
msaretta5783ae2015-09-08 15:35:32 -0700482 if (CodecSrc::kGetFromCanvas_DstColorType == dstColorType) {
483 return true;
484 }
485 return false;
msarett5cb48852015-11-06 08:56:32 -0800486 case SkBitmapRegionDecoder::kAndroidCodec_Strategy:
msarett26ad17b2015-10-22 07:29:19 -0700487 switch (dstColorType) {
488 case CodecSrc::kGetFromCanvas_DstColorType:
489 case CodecSrc::kIndex8_Always_DstColorType:
490 case CodecSrc::kGrayscale_Always_DstColorType:
491 return true;
492 default:
493 return false;
494 }
msaretta5783ae2015-09-08 15:35:32 -0700495 default:
496 SkASSERT(false);
497 return false;
498 }
499}
500
msarett5cb48852015-11-06 08:56:32 -0800501static void push_brd_src(Path path, SkBitmapRegionDecoder::Strategy strategy,
msaretta5783ae2015-09-08 15:35:32 -0700502 CodecSrc::DstColorType dstColorType, BRDSrc::Mode mode, uint32_t sampleSize) {
503 SkString folder;
504 switch (strategy) {
msarett5cb48852015-11-06 08:56:32 -0800505 case SkBitmapRegionDecoder::kCanvas_Strategy:
msaretta5783ae2015-09-08 15:35:32 -0700506 folder.append("brd_canvas");
507 break;
msarett5cb48852015-11-06 08:56:32 -0800508 case SkBitmapRegionDecoder::kAndroidCodec_Strategy:
msarett26ad17b2015-10-22 07:29:19 -0700509 folder.append("brd_android_codec");
510 break;
msaretta5783ae2015-09-08 15:35:32 -0700511 default:
512 SkASSERT(false);
513 return;
514 }
515
516 switch (mode) {
517 case BRDSrc::kFullImage_Mode:
518 break;
519 case BRDSrc::kDivisor_Mode:
520 folder.append("_divisor");
521 break;
522 default:
523 SkASSERT(false);
524 return;
525 }
526
527 switch (dstColorType) {
528 case CodecSrc::kGetFromCanvas_DstColorType:
529 break;
530 case CodecSrc::kIndex8_Always_DstColorType:
531 folder.append("_kIndex");
532 break;
533 case CodecSrc::kGrayscale_Always_DstColorType:
534 folder.append("_kGray");
535 break;
536 default:
537 SkASSERT(false);
538 return;
539 }
540
541 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800542 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700543 }
544
545 BRDSrc* src = new BRDSrc(path, strategy, mode, dstColorType, sampleSize);
546 push_src("image", folder, src);
547}
548
549static void push_brd_srcs(Path path) {
550
msarett5cb48852015-11-06 08:56:32 -0800551 const SkBitmapRegionDecoder::Strategy strategies[] = {
552 SkBitmapRegionDecoder::kCanvas_Strategy,
msarett5cb48852015-11-06 08:56:32 -0800553 SkBitmapRegionDecoder::kAndroidCodec_Strategy,
msaretta5783ae2015-09-08 15:35:32 -0700554 };
555
scroggo501b7342015-11-03 07:55:11 -0800556 // Test on a variety of sampleSizes, making sure to include:
557 // - 2, 4, and 8, which are natively supported by jpeg
558 // - multiples of 2 which are not divisible by 4 (analogous for 4)
559 // - larger powers of two, since BRD clients generally use powers of 2
560 // We will only produce output for the larger sizes on large images.
561 const uint32_t sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 24, 32, 64 };
msarett6efbe052015-09-11 09:01:16 -0700562
msaretta5783ae2015-09-08 15:35:32 -0700563 // We will only test to one backend (8888), but we will test all of the
564 // color types that we need to decode to on this backend.
565 const CodecSrc::DstColorType dstColorTypes[] = {
msarett26ad17b2015-10-22 07:29:19 -0700566 CodecSrc::kGetFromCanvas_DstColorType,
567 CodecSrc::kIndex8_Always_DstColorType,
568 CodecSrc::kGrayscale_Always_DstColorType,
msaretta5783ae2015-09-08 15:35:32 -0700569 };
570
571 const BRDSrc::Mode modes[] = {
msarett26ad17b2015-10-22 07:29:19 -0700572 BRDSrc::kFullImage_Mode,
573 BRDSrc::kDivisor_Mode,
msaretta5783ae2015-09-08 15:35:32 -0700574 };
575
msarett5cb48852015-11-06 08:56:32 -0800576 for (SkBitmapRegionDecoder::Strategy strategy : strategies) {
msarett6efbe052015-09-11 09:01:16 -0700577 for (uint32_t sampleSize : sampleSizes) {
msarett6efbe052015-09-11 09:01:16 -0700578 for (CodecSrc::DstColorType dstColorType : dstColorTypes) {
579 if (brd_color_type_supported(strategy, dstColorType)) {
580 for (BRDSrc::Mode mode : modes) {
msaretta5783ae2015-09-08 15:35:32 -0700581 push_brd_src(path, strategy, dstColorType, mode, sampleSize);
582 }
583 }
584 }
585 }
586 }
587}
588
589static bool brd_supported(const char* ext) {
msarett8c8f22a2015-04-01 06:58:48 -0700590 static const char* const exts[] = {
msaretta5783ae2015-09-08 15:35:32 -0700591 "jpg", "jpeg", "png", "webp",
592 "JPG", "JPEG", "PNG", "WEBP",
msarett8c8f22a2015-04-01 06:58:48 -0700593 };
594
595 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
596 if (0 == strcmp(exts[i], ext)) {
597 return true;
598 }
599 }
600 return false;
scroggo9b77ddd2015-03-19 06:03:39 -0700601}
602
scroggo86737142016-02-03 12:19:11 -0800603static bool gather_srcs() {
mtklein748ca3b2015-01-15 10:56:12 -0800604 for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) {
djsollen54416de2015-04-03 07:24:48 -0700605 push_src("gm", "", new GMSrc(r->factory()));
mtklein748ca3b2015-01-15 10:56:12 -0800606 }
halcanaryfc37ad12015-01-30 07:31:19 -0800607 for (int i = 0; i < FLAGS_skps.count(); i++) {
608 const char* path = FLAGS_skps[i];
609 if (sk_isdir(path)) {
610 SkOSFile::Iter it(path, "skp");
611 for (SkString file; it.next(&file); ) {
djsollen54416de2015-04-03 07:24:48 -0700612 push_src("skp", "", new SKPSrc(SkOSPath::Join(path, file.c_str())));
halcanaryfc37ad12015-01-30 07:31:19 -0800613 }
614 } else {
djsollen54416de2015-04-03 07:24:48 -0700615 push_src("skp", "", new SKPSrc(path));
mtklein114c3cd2015-01-15 10:15:02 -0800616 }
617 }
scroggo86737142016-02-03 12:19:11 -0800618
619 SkTArray<SkString> images;
620 if (!CollectImages(&images)) {
621 return false;
622 }
623
624 for (auto image : images) {
625 push_codec_srcs(image);
scroggoe4499842016-02-25 11:03:47 -0800626 if (FLAGS_simpleCodec) {
627 continue;
628 }
629
mtklein21eaf3b2016-02-08 12:39:59 -0800630 const char* ext = strrchr(image.c_str(), '.');
631 if (ext && brd_supported(ext+1)) {
scroggo86737142016-02-03 12:19:11 -0800632 push_brd_srcs(image);
mtklein709d2c32015-01-15 08:30:25 -0800633 }
mtklein709d2c32015-01-15 08:30:25 -0800634 }
scroggo86737142016-02-03 12:19:11 -0800635
636 return true;
mtklein709d2c32015-01-15 08:30:25 -0800637}
638
kkinnunen3e980c32015-12-23 01:33:00 -0800639static void push_sink(const SkCommandLineConfig& config, Sink* s) {
rmistry0f515bd2015-12-22 10:22:26 -0800640 SkAutoTDelete<Sink> sink(s);
kkinnunen3e980c32015-12-23 01:33:00 -0800641
mtkleine0effd62015-07-29 06:37:28 -0700642 // Try a simple Src as a canary. If it fails, skip this sink.
mtklein748ca3b2015-01-15 10:56:12 -0800643 struct : public Src {
mtkleine0effd62015-07-29 06:37:28 -0700644 Error draw(SkCanvas* c) const override {
645 c->drawRect(SkRect::MakeWH(1,1), SkPaint());
646 return "";
647 }
mtklein36352bf2015-03-25 18:17:31 -0700648 SkISize size() const override { return SkISize::Make(16, 16); }
mtkleine0effd62015-07-29 06:37:28 -0700649 Name name() const override { return "justOneRect"; }
650 } justOneRect;
mtklein114c3cd2015-01-15 10:15:02 -0800651
mtklein748ca3b2015-01-15 10:56:12 -0800652 SkBitmap bitmap;
653 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800654 SkString log;
mtkleine0effd62015-07-29 06:37:28 -0700655 Error err = sink->draw(justOneRect, &bitmap, &stream, &log);
mtklein4089ef72015-03-05 08:40:28 -0800656 if (err.isFatal()) {
kkinnunen3e980c32015-12-23 01:33:00 -0800657 SkDebugf("Could not run %s: %s\n", config.getTag().c_str(), err.c_str());
mtklein05641a52015-04-21 10:49:13 -0700658 exit(1);
mtklein114c3cd2015-01-15 10:15:02 -0800659 }
660
mtkleine0effd62015-07-29 06:37:28 -0700661 TaggedSink& ts = gSinks.push_back();
mtklein748ca3b2015-01-15 10:56:12 -0800662 ts.reset(sink.detach());
kkinnunen3e980c32015-12-23 01:33:00 -0800663 ts.tag = config.getTag();
mtklein748ca3b2015-01-15 10:56:12 -0800664}
665
666static bool gpu_supported() {
667#if SK_SUPPORT_GPU
668 return FLAGS_gpu;
669#else
670 return false;
671#endif
672}
kkinnunen9ebc3f02015-12-21 23:48:13 -0800673
kkinnunen3e980c32015-12-23 01:33:00 -0800674static Sink* create_sink(const SkCommandLineConfig* config) {
675#if SK_SUPPORT_GPU
676 if (gpu_supported()) {
677 if (const SkCommandLineConfigGpu* gpuConfig = config->asConfigGpu()) {
678 GrContextFactory::GLContextType contextType = gpuConfig->getContextType();
679 GrContextFactory::GLContextOptions contextOptions =
680 GrContextFactory::kNone_GLContextOptions;
681 if (gpuConfig->getUseNVPR()) {
682 contextOptions = static_cast<GrContextFactory::GLContextOptions>(
683 contextOptions | GrContextFactory::kEnableNVPR_GLContextOptions);
684 }
685 GrContextFactory testFactory;
686 if (!testFactory.get(contextType, contextOptions)) {
687 SkDebugf("WARNING: can not create GPU context for config '%s'. "
688 "GM tests will be skipped.\n", gpuConfig->getTag().c_str());
689 return nullptr;
690 }
691 return new GPUSink(contextType, contextOptions, gpuConfig->getSamples(),
692 gpuConfig->getUseDIText(), FLAGS_gpu_threading);
693 }
694 }
695#endif
696
697#define SINK(t, sink, ...) if (config->getBackend().equals(t)) { return new sink(__VA_ARGS__); }
698
tomhudsoneebc39a2015-02-23 12:18:05 -0800699#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
700 SINK("hwui", HWUISink);
701#endif
702
mtklein748ca3b2015-01-15 10:56:12 -0800703 if (FLAGS_cpu) {
704 SINK("565", RasterSink, kRGB_565_SkColorType);
705 SINK("8888", RasterSink, kN32_SkColorType);
mtklein27c3fdd2016-02-26 14:43:21 -0800706 SINK("srgb", RasterSink, kN32_SkColorType, kSRGB_SkColorProfileType);
707 SINK("f16", RasterSink, kRGBA_F16_SkColorType);
halcanaryc11c62f2015-09-28 11:51:54 -0700708 SINK("pdf", PDFSink, "Pdfium");
709 SINK("pdf_poppler", PDFSink, "Poppler");
mtklein9c3f17d2015-01-28 11:35:18 -0800710 SINK("skp", SKPSink);
mtklein8a4527e2015-01-31 20:00:58 -0800711 SINK("svg", SVGSink);
712 SINK("null", NullSink);
halcanary47ef4d52015-03-03 09:13:09 -0800713 SINK("xps", XPSSink);
mtklein748ca3b2015-01-15 10:56:12 -0800714 }
715#undef SINK
halcanary96fcdcc2015-08-27 07:41:13 -0700716 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800717}
718
kkinnunen3e980c32015-12-23 01:33:00 -0800719static Sink* create_via(const SkString& tag, Sink* wrapped) {
720#define VIA(t, via, ...) if (tag.equals(t)) { return new via(__VA_ARGS__); }
halcanary96fcdcc2015-08-27 07:41:13 -0700721 VIA("twice", ViaTwice, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700722 VIA("serialize", ViaSerialization, wrapped);
mtklein4a34ecb2016-01-08 10:19:35 -0800723 VIA("pic", ViaPicture, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700724 VIA("2ndpic", ViaSecondPicture, wrapped);
mtkleind31c13d2015-05-05 12:59:56 -0700725 VIA("sp", ViaSingletonPictures, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700726 VIA("tiles", ViaTiles, 256, 256, nullptr, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800727 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
mtklein2e2ea382015-10-16 10:29:41 -0700728 VIA("remote", ViaRemote, false, wrapped);
729 VIA("remote_cache", ViaRemote, true, wrapped);
halcanary7a76f9c2016-02-03 11:53:18 -0800730 VIA("mojo", ViaMojo, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800731
mtkleind603b222015-02-17 11:13:33 -0800732 if (FLAGS_matrix.count() == 4) {
mtklein748ca3b2015-01-15 10:56:12 -0800733 SkMatrix m;
mtkleind603b222015-02-17 11:13:33 -0800734 m.reset();
735 m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
736 m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
737 m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
738 m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
739 VIA("matrix", ViaMatrix, m, wrapped);
740 VIA("upright", ViaUpright, m, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800741 }
tomhudson64de1e12015-03-05 08:01:07 -0800742
743#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
744 VIA("androidsdk", ViaAndroidSDK, wrapped);
745#endif
746
mtklein748ca3b2015-01-15 10:56:12 -0800747#undef VIA
halcanary96fcdcc2015-08-27 07:41:13 -0700748 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800749}
750
mtklein748ca3b2015-01-15 10:56:12 -0800751static void gather_sinks() {
kkinnunen3e980c32015-12-23 01:33:00 -0800752 SkCommandLineConfigArray configs;
753 ParseConfigs(FLAGS_config, &configs);
754 for (int i = 0; i < configs.count(); i++) {
755 const SkCommandLineConfig& config = *configs[i];
756 Sink* sink = create_sink(&config);
757 if (sink == nullptr) {
758 SkDebugf("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
759 config.getTag().c_str());
760 continue;
761 }
mtklein748ca3b2015-01-15 10:56:12 -0800762
kkinnunen3e980c32015-12-23 01:33:00 -0800763 const SkTArray<SkString>& parts = config.getViaParts();
764 for (int j = parts.count(); j-- > 0;) {
765 const SkString& part = parts[j];
766 Sink* next = create_via(part, sink);
halcanary96fcdcc2015-08-27 07:41:13 -0700767 if (next == nullptr) {
kkinnunen3e980c32015-12-23 01:33:00 -0800768 SkDebugf("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
769 part.c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800770 delete sink;
halcanary96fcdcc2015-08-27 07:41:13 -0700771 sink = nullptr;
mtklein748ca3b2015-01-15 10:56:12 -0800772 break;
773 }
774 sink = next;
775 }
776 if (sink) {
777 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800778 }
779 }
780}
mtklein709d2c32015-01-15 08:30:25 -0800781
mtkleina5114d72015-08-24 13:27:01 -0700782static bool dump_png(SkBitmap bitmap, const char* path, const char* md5) {
783 const int w = bitmap.width(),
784 h = bitmap.height();
mtklein27c3fdd2016-02-26 14:43:21 -0800785 // PNG wants unpremultiplied 8-bit RGBA pixels (16-bit could work fine too).
786 // We leave the gamma of these bytes unspecified, to continue the status quo,
787 // which we think generally is to interpret them as sRGB.
mtkleina5114d72015-08-24 13:27:01 -0700788
mtklein27c3fdd2016-02-26 14:43:21 -0800789 SkAutoTMalloc<uint32_t> rgba(w*h);
790
791 if (bitmap. colorType() == kN32_SkColorType &&
792 bitmap.profileType() == kSRGB_SkColorProfileType) {
793 // These are premul sRGB 8-bit pixels in SkPMColor order.
794 // We want unpremul sRGB 8-bit pixels in RGBA order. We'll get there via floats.
795 bitmap.lockPixels();
796 auto px = (const uint32_t*)bitmap.getPixels();
797 if (!px) {
mtkleina5114d72015-08-24 13:27:01 -0700798 return false;
799 }
mtklein27c3fdd2016-02-26 14:43:21 -0800800 for (int i = 0; i < w*h; i++) {
801 Sk4f fs = Sk4f_fromS32(px[i]); // Convert up to linear floats.
802 #if defined(SK_PMCOLOR_IS_BGRA)
803 fs = SkNx_shuffle<2,1,0,3>(fs); // Shuffle to RGBA, if not there already.
804 #endif
805 float invA = 1.0f / fs[3];
806 fs = fs * Sk4f(invA, invA, invA, 1); // Unpremultiply.
807 rgba[i] = Sk4f_toS32(fs); // Pack down to sRGB bytes.
808 }
mtkleina5114d72015-08-24 13:27:01 -0700809
mtklein27c3fdd2016-02-26 14:43:21 -0800810 } else if (bitmap.colorType() == kRGBA_F16_SkColorType) {
811 // These are premul linear half-float pixels in RGBA order.
812 // We want unpremul sRGB 8-bit pixels in RGBA order. We'll get there via floats.
813 bitmap.lockPixels();
814 auto px = (const uint64_t*)bitmap.getPixels();
815 if (!px) {
816 return false;
817 }
818 for (int i = 0; i < w*h; i++) {
819 Sk4f fs = SkHalfToFloat_01(px[i]); // Convert up to linear floats.
820 float invA = 1.0f / fs[3];
821 fs = fs * Sk4f(invA, invA, invA, 1); // Unpremultiply.
822 rgba[i] = Sk4f_toS32(fs); // Pack down to sRGB bytes.
823 }
824
825
826 } else {
827 // We "should" gamma correct in here but we don't.
828 // We want Gold to show exactly what our clients are seeing, broken gamma.
829
830 // Convert smaller formats up to premul linear 8-bit (in SkPMColor order).
831 if (bitmap.colorType() != kN32_SkColorType) {
832 SkBitmap n32;
833 if (!bitmap.copyTo(&n32, kN32_SkColorType)) {
834 return false;
835 }
836 bitmap = n32;
837 }
838
839 // Convert premul linear 8-bit to unpremul linear 8-bit RGBA.
840 if (!bitmap.readPixels(SkImageInfo::Make(w,h, kRGBA_8888_SkColorType,
841 kUnpremul_SkAlphaType),
842 rgba, 4*w, 0,0)) {
843 return false;
844 }
mtkleina5114d72015-08-24 13:27:01 -0700845 }
846
847 // We don't need bitmap anymore. Might as well drop our ref.
mtkleinfccb77d2015-08-24 14:13:29 -0700848 bitmap.reset();
mtkleina5114d72015-08-24 13:27:01 -0700849
mtkleinc64137c2015-08-25 10:56:08 -0700850 FILE* f = fopen(path, "wb");
mtkleina5114d72015-08-24 13:27:01 -0700851 if (!f) { return false; }
852
853 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
854 if (!png) {
855 fclose(f);
856 return false;
857 }
858
859 png_infop info = png_create_info_struct(png);
860 if (!info) {
861 png_destroy_write_struct(&png, &info);
862 fclose(f);
863 return false;
864 }
865
mtkleind69ece62015-09-10 10:37:44 -0700866 SkString description;
867 description.append("Key: ");
868 for (int i = 0; i < FLAGS_key.count(); i++) {
869 description.appendf("%s ", FLAGS_key[i]);
870 }
871 description.append("Properties: ");
872 for (int i = 0; i < FLAGS_properties.count(); i++) {
873 description.appendf("%s ", FLAGS_properties[i]);
874 }
875 description.appendf("MD5: %s", md5);
876
mtkleina5114d72015-08-24 13:27:01 -0700877 png_text text[2];
878 text[0].key = (png_charp)"Author";
879 text[0].text = (png_charp)"DM dump_png()";
880 text[0].compression = PNG_TEXT_COMPRESSION_NONE;
881 text[1].key = (png_charp)"Description";
mtkleind69ece62015-09-10 10:37:44 -0700882 text[1].text = (png_charp)description.c_str();
mtkleina5114d72015-08-24 13:27:01 -0700883 text[1].compression = PNG_TEXT_COMPRESSION_NONE;
884 png_set_text(png, info, text, 2);
885
886 png_init_io(png, f);
887 png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8,
888 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
889 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
890 png_write_info(png, info);
891 for (int j = 0; j < h; j++) {
892 png_bytep row = (png_bytep)(rgba.get() + w*j);
893 png_write_rows(png, &row, 1);
894 }
895 png_write_end(png, info);
896
897 png_destroy_write_struct(&png, &info);
898 fclose(f);
899 return true;
900}
901
mtkleina2ef6422015-01-15 13:44:22 -0800902static bool match(const char* needle, const char* haystack) {
halcanary96fcdcc2015-08-27 07:41:13 -0700903 return 0 == strcmp("_", needle) || nullptr != strstr(haystack, needle);
mtkleina2ef6422015-01-15 13:44:22 -0800904}
905
mtklein3eed7dd2016-02-24 19:07:07 -0800906static bool is_blacklisted(const char* sink, const char* src,
907 const char* srcOptions, const char* name) {
mtklein0d243ff2015-04-03 07:51:00 -0700908 for (int i = 0; i < FLAGS_blacklist.count() - 3; i += 4) {
mtkleina2ef6422015-01-15 13:44:22 -0800909 if (match(FLAGS_blacklist[i+0], sink) &&
djsollen54416de2015-04-03 07:24:48 -0700910 match(FLAGS_blacklist[i+1], src) &&
911 match(FLAGS_blacklist[i+2], srcOptions) &&
912 match(FLAGS_blacklist[i+3], name)) {
mtklein3eed7dd2016-02-24 19:07:07 -0800913 return true;
mtkleina2ef6422015-01-15 13:44:22 -0800914 }
915 }
mtklein3eed7dd2016-02-24 19:07:07 -0800916 return false;
mtkleina2ef6422015-01-15 13:44:22 -0800917}
918
mtkleincd50bca2016-01-05 06:20:20 -0800919// Even when a Task Sink reports to be non-threadsafe (e.g. GPU), we know things like
920// .png encoding are definitely thread safe. This lets us offload that work to CPU threads.
921static SkTaskGroup gDefinitelyThreadSafeWork;
922
mtklein748ca3b2015-01-15 10:56:12 -0800923// The finest-grained unit of work we can run: draw a single Src into a single Sink,
924// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
925struct Task {
mtkleine0effd62015-07-29 06:37:28 -0700926 Task(const TaggedSrc& src, const TaggedSink& sink) : src(src), sink(sink) {}
927 const TaggedSrc& src;
928 const TaggedSink& sink;
mtklein748ca3b2015-01-15 10:56:12 -0800929
mtklein21eaf3b2016-02-08 12:39:59 -0800930 static void Run(const Task& task) {
931 SkString name = task.src->name();
mtkleine0effd62015-07-29 06:37:28 -0700932
mtkleinb9eb4ac2015-02-02 18:26:03 -0800933 SkString log;
mtklein3eed7dd2016-02-24 19:07:07 -0800934 if (!FLAGS_dryRun) {
mtklein748ca3b2015-01-15 10:56:12 -0800935 SkBitmap bitmap;
936 SkDynamicMemoryWStream stream;
mtklein3eed7dd2016-02-24 19:07:07 -0800937 start(task.sink.tag.c_str(), task.src.tag.c_str(),
938 task.src.options.c_str(), name.c_str());
mtklein21eaf3b2016-02-08 12:39:59 -0800939 Error err = task.sink->draw(*task.src, &bitmap, &stream, &log);
mtklein748ca3b2015-01-15 10:56:12 -0800940 if (!err.isEmpty()) {
mtklein4089ef72015-03-05 08:40:28 -0800941 if (err.isFatal()) {
djsollen54416de2015-04-03 07:24:48 -0700942 fail(SkStringPrintf("%s %s %s %s: %s",
mtklein21eaf3b2016-02-08 12:39:59 -0800943 task.sink.tag.c_str(),
944 task.src.tag.c_str(),
945 task.src.options.c_str(),
mtklein4089ef72015-03-05 08:40:28 -0800946 name.c_str(),
947 err.c_str()));
mtkleinb37cb412015-03-18 05:27:14 -0700948 } else {
mtklein3eed7dd2016-02-24 19:07:07 -0800949 done(task.sink.tag.c_str(), task.src.tag.c_str(),
950 task.src.options.c_str(), name.c_str());
mtkleinba6ada72016-01-21 09:39:35 -0800951 return;
mtklein4089ef72015-03-05 08:40:28 -0800952 }
mtklein748ca3b2015-01-15 10:56:12 -0800953 }
mtklein62bd1a62015-01-27 14:46:26 -0800954
mtkleincd50bca2016-01-05 06:20:20 -0800955 // We're likely switching threads here, so we must capture by value, [=] or [foo,bar].
956 SkStreamAsset* data = stream.detachAsStream();
957 gDefinitelyThreadSafeWork.add([task,name,bitmap,data]{
958 SkAutoTDelete<SkStreamAsset> ownedData(data);
959
960 // Why doesn't the copy constructor do this when we have pre-locked pixels?
961 bitmap.lockPixels();
962
963 SkString md5;
964 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
965 SkMD5 hash;
966 if (data->getLength()) {
967 hash.writeStream(data, data->getLength());
968 data->rewind();
mtklein38408462015-07-08 07:25:27 -0700969 } else {
mtkleincd50bca2016-01-05 06:20:20 -0800970 // If we're BGRA (Linux, Windows), swizzle over to RGBA (Mac, Android).
971 // This helps eliminate multiple 0-pixel-diff hashes on gold.skia.org.
972 // (Android's general slow speed breaks the tie arbitrarily in RGBA's favor.)
973 // We might consider promoting 565 to RGBA too.
974 if (bitmap.colorType() == kBGRA_8888_SkColorType) {
975 SkBitmap swizzle;
976 SkAssertResult(bitmap.copyTo(&swizzle, kRGBA_8888_SkColorType));
977 hash.write(swizzle.getPixels(), swizzle.getSize());
978 } else {
979 hash.write(bitmap.getPixels(), bitmap.getSize());
980 }
981 }
982 SkMD5::Digest digest;
983 hash.finish(digest);
984 for (int i = 0; i < 16; i++) {
985 md5.appendf("%02x", digest.data[i]);
mtklein38408462015-07-08 07:25:27 -0700986 }
mtklein62bd1a62015-01-27 14:46:26 -0800987 }
mtklein62bd1a62015-01-27 14:46:26 -0800988
mtkleincd50bca2016-01-05 06:20:20 -0800989 if (!FLAGS_readPath.isEmpty() &&
mtklein3eed7dd2016-02-24 19:07:07 -0800990 !gGold.contains(Gold(task.sink.tag, task.src.tag,
991 task.src.options, name, md5))) {
mtkleincd50bca2016-01-05 06:20:20 -0800992 fail(SkStringPrintf("%s not found for %s %s %s %s in %s",
993 md5.c_str(),
mtklein21eaf3b2016-02-08 12:39:59 -0800994 task.sink.tag.c_str(),
995 task.src.tag.c_str(),
996 task.src.options.c_str(),
mtkleincd50bca2016-01-05 06:20:20 -0800997 name.c_str(),
998 FLAGS_readPath[0]));
mtklein748ca3b2015-01-15 10:56:12 -0800999 }
mtkleincd50bca2016-01-05 06:20:20 -08001000
1001 if (!FLAGS_writePath.isEmpty()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001002 const char* ext = task.sink->fileExtension();
mtkleincd50bca2016-01-05 06:20:20 -08001003 if (data->getLength()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001004 WriteToDisk(task, md5, ext, data, data->getLength(), nullptr);
mtkleincd50bca2016-01-05 06:20:20 -08001005 SkASSERT(bitmap.drawsNothing());
1006 } else if (!bitmap.drawsNothing()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001007 WriteToDisk(task, md5, ext, nullptr, 0, &bitmap);
mtkleincd50bca2016-01-05 06:20:20 -08001008 }
1009 }
1010 });
mtklein748ca3b2015-01-15 10:56:12 -08001011 }
mtklein3eed7dd2016-02-24 19:07:07 -08001012 done(task.sink.tag.c_str(), task.src.tag.c_str(), task.src.options.c_str(), name.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001013 }
1014
1015 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -08001016 SkString md5,
1017 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -08001018 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -08001019 const SkBitmap* bitmap) {
mtklein748ca3b2015-01-15 10:56:12 -08001020 JsonWriter::BitmapResult result;
djsollen54416de2015-04-03 07:24:48 -07001021 result.name = task.src->name();
mtklein3eed7dd2016-02-24 19:07:07 -08001022 result.config = task.sink.tag;
djsollen54416de2015-04-03 07:24:48 -07001023 result.sourceType = task.src.tag;
1024 result.sourceOptions = task.src.options;
1025 result.ext = ext;
1026 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -08001027 JsonWriter::AddBitmapResult(result);
1028
mtkleinb0531a72015-04-07 13:38:48 -07001029 // If an MD5 is uninteresting, we want it noted in the JSON file,
1030 // but don't want to dump it out as a .png (or whatever ext is).
1031 if (gUninterestingHashes.contains(md5)) {
1032 return;
1033 }
1034
mtklein748ca3b2015-01-15 10:56:12 -08001035 const char* dir = FLAGS_writePath[0];
1036 if (0 == strcmp(dir, "@")) { // Needed for iOS.
1037 dir = FLAGS_resourcePath[0];
1038 }
1039 sk_mkdir(dir);
1040
1041 SkString path;
1042 if (FLAGS_nameByHash) {
1043 path = SkOSPath::Join(dir, result.md5.c_str());
1044 path.append(".");
1045 path.append(ext);
1046 if (sk_exists(path.c_str())) {
1047 return; // Content-addressed. If it exists already, we're done.
1048 }
1049 } else {
kkinnunen3e980c32015-12-23 01:33:00 -08001050 path = SkOSPath::Join(dir, task.sink.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001051 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001052 path = SkOSPath::Join(path.c_str(), task.src.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001053 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001054 if (strcmp(task.src.options.c_str(), "") != 0) {
1055 path = SkOSPath::Join(path.c_str(), task.src.options.c_str());
djsollen54416de2015-04-03 07:24:48 -07001056 sk_mkdir(path.c_str());
1057 }
mtklein748ca3b2015-01-15 10:56:12 -08001058 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
1059 path.append(".");
1060 path.append(ext);
1061 }
1062
mtklein748ca3b2015-01-15 10:56:12 -08001063 if (bitmap) {
mtkleina5114d72015-08-24 13:27:01 -07001064 if (!dump_png(*bitmap, path.c_str(), result.md5.c_str())) {
mtklein748ca3b2015-01-15 10:56:12 -08001065 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
1066 return;
1067 }
1068 } else {
mtkleina5114d72015-08-24 13:27:01 -07001069 SkFILEWStream file(path.c_str());
1070 if (!file.isValid()) {
1071 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
1072 return;
1073 }
mtklein748ca3b2015-01-15 10:56:12 -08001074 if (!file.writeStream(data, len)) {
1075 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
1076 return;
1077 }
1078 }
1079 }
1080};
1081
mtklein748ca3b2015-01-15 10:56:12 -08001082/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1083
1084// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
1085
mtklein21eaf3b2016-02-08 12:39:59 -08001086static SkTDArray<skiatest::Test> gParallelTests, gSerialTests;
mtklein748ca3b2015-01-15 10:56:12 -08001087
1088static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -08001089 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -08001090 return;
1091 }
mtklein6393c062015-04-27 08:45:01 -07001092 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r; r = r->next()) {
1093 if (!in_shard()) {
1094 continue;
1095 }
halcanary87f3ba42015-01-20 09:30:20 -08001096 // Despite its name, factory() is returning a reference to
1097 // link-time static const POD data.
1098 const skiatest::Test& test = r->factory();
1099 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -08001100 continue;
1101 }
halcanary87f3ba42015-01-20 09:30:20 -08001102 if (test.needsGpu && gpu_supported()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001103 (FLAGS_gpu_threading ? gParallelTests : gSerialTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -08001104 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein21eaf3b2016-02-08 12:39:59 -08001105 gParallelTests.push(test);
mtklein82d28432015-01-15 12:46:02 -08001106 }
mtklein748ca3b2015-01-15 10:56:12 -08001107 }
1108}
1109
mtklein21eaf3b2016-02-08 12:39:59 -08001110static void run_test(skiatest::Test test) {
halcanary87f3ba42015-01-20 09:30:20 -08001111 struct : public skiatest::Reporter {
mtklein36352bf2015-03-25 18:17:31 -07001112 void reportFailed(const skiatest::Failure& failure) override {
halcanary87f3ba42015-01-20 09:30:20 -08001113 fail(failure.toString());
1114 JsonWriter::AddTestFailure(failure);
1115 }
mtklein36352bf2015-03-25 18:17:31 -07001116 bool allowExtendedTest() const override {
halcanary87f3ba42015-01-20 09:30:20 -08001117 return FLAGS_pathOpsExtended;
1118 }
mtklein36352bf2015-03-25 18:17:31 -07001119 bool verbose() const override { return FLAGS_veryVerbose; }
halcanary87f3ba42015-01-20 09:30:20 -08001120 } reporter;
djsollen824996a2015-06-12 12:06:22 -07001121
mtklein3eed7dd2016-02-24 19:07:07 -08001122 if (!FLAGS_dryRun && !is_blacklisted("_", "tests", "_", test.name)) {
mtklein21eaf3b2016-02-08 12:39:59 -08001123 start("unit", "test", "", test.name);
mtklein55e88b22015-01-21 15:50:13 -08001124 GrContextFactory factory;
mtklein21eaf3b2016-02-08 12:39:59 -08001125 test.proc(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -08001126 }
mtklein3eed7dd2016-02-24 19:07:07 -08001127 done("unit", "test", "", test.name);
mtklein748ca3b2015-01-15 10:56:12 -08001128}
1129
1130/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1131
mtkleinafae30a2016-02-24 12:28:32 -08001132DEFINE_int32(status_sec, 15, "Print status this often (and if we crash).");
1133
1134SkThread* start_status_thread() {
1135 auto thread = new SkThread([] (void*) {
1136 for (;;) {
1137 print_status();
1138 #if defined(SK_BUILD_FOR_WIN)
1139 Sleep(FLAGS_status_sec * 1000);
1140 #else
1141 sleep(FLAGS_status_sec);
1142 #endif
mtklein2e1c47e2015-03-12 07:16:56 -07001143 }
mtkleinafae30a2016-02-24 12:28:32 -08001144 });
1145 thread->start();
1146 return thread;
mtkleinde6fc2b2015-03-12 06:28:54 -07001147}
1148
caryclark83ca6282015-06-10 09:31:09 -07001149#define PORTABLE_FONT_PREFIX "Toy Liberation "
1150
1151static SkTypeface* create_from_name(const char familyName[], SkTypeface::Style style) {
1152 if (familyName && strlen(familyName) > sizeof(PORTABLE_FONT_PREFIX)
1153 && !strncmp(familyName, PORTABLE_FONT_PREFIX, sizeof(PORTABLE_FONT_PREFIX) - 1)) {
caryclark1818acb2015-07-24 12:09:25 -07001154 return sk_tool_utils::create_portable_typeface(familyName, style);
caryclark83ca6282015-06-10 09:31:09 -07001155 }
halcanary96fcdcc2015-08-27 07:41:13 -07001156 return nullptr;
caryclark83ca6282015-06-10 09:31:09 -07001157}
1158
1159#undef PORTABLE_FONT_PREFIX
1160
1161extern SkTypeface* (*gCreateTypefaceDelegate)(const char [], SkTypeface::Style );
1162
jcgregorio3b27ade2014-11-13 08:06:40 -08001163int dm_main();
caryclark17f0b6d2014-07-22 10:15:34 -07001164int dm_main() {
mtklein246ba3a2016-02-23 10:39:36 -08001165 setup_crash_handler();
mtkleinafae30a2016-02-24 12:28:32 -08001166
mtklein5286f022016-01-22 08:18:14 -08001167 JsonWriter::DumpJson(); // It's handy for the bots to assume this is ~never missing.
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +00001168 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -07001169 SkTaskGroup::Enabler enabled(FLAGS_threads);
caryclark83ca6282015-06-10 09:31:09 -07001170 gCreateTypefaceDelegate = &create_from_name;
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +00001171
halcanary7d571242016-02-24 17:59:16 -08001172 {
1173 SkString testResourcePath = GetResourcePath("color_wheel.png");
1174 SkFILEStream testResource(testResourcePath.c_str());
1175 if (!testResource.isValid()) {
1176 SkDebugf("Some resources are missing. Do you need to set --resourcePath?\n");
1177 }
1178 }
mtklein62bd1a62015-01-27 14:46:26 -08001179 gather_gold();
borenet09ed4802015-04-03 14:15:33 -07001180 gather_uninteresting_hashes();
mtklein62bd1a62015-01-27 14:46:26 -08001181
scroggo86737142016-02-03 12:19:11 -08001182 if (!gather_srcs()) {
1183 return 1;
1184 }
mtklein748ca3b2015-01-15 10:56:12 -08001185 gather_sinks();
1186 gather_tests();
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +00001187
mtklein21eaf3b2016-02-08 12:39:59 -08001188 gPending = gSrcs.count() * gSinks.count() + gParallelTests.count() + gSerialTests.count();
mtkleinafae30a2016-02-24 12:28:32 -08001189 SkDebugf("%d srcs * %d sinks + %d tests == %d tasks",
mtklein21eaf3b2016-02-08 12:39:59 -08001190 gSrcs.count(), gSinks.count(), gParallelTests.count() + gSerialTests.count(), gPending);
mtkleinafae30a2016-02-24 12:28:32 -08001191 SkAutoTDelete<SkThread> statusThread(start_status_thread());
mtklein748ca3b2015-01-15 10:56:12 -08001192
mtklein21eaf3b2016-02-08 12:39:59 -08001193 // Kick off as much parallel work as we can, making note of any serial work we'll need to do.
1194 SkTaskGroup parallel;
1195 SkTArray<Task> serial;
1196
1197 for (auto& sink : gSinks)
1198 for (auto& src : gSrcs) {
mtklein3eed7dd2016-02-24 19:07:07 -08001199 if (src->veto(sink->flags()) ||
1200 is_blacklisted(sink.tag.c_str(), src.tag.c_str(),
1201 src.options.c_str(), src->name().c_str())) {
1202 SkAutoTAcquire<SkPODSpinlock> lock(gMutex);
1203 gPending--;
1204 continue;
1205 }
1206
mtklein21eaf3b2016-02-08 12:39:59 -08001207 Task task(src, sink);
1208 if (src->serial() || sink->serial()) {
1209 serial.push_back(task);
1210 } else {
1211 parallel.add([task] { Task::Run(task); });
mtklein748ca3b2015-01-15 10:56:12 -08001212 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001213 }
mtklein21eaf3b2016-02-08 12:39:59 -08001214 for (auto test : gParallelTests) {
1215 parallel.add([test] { run_test(test); });
mtklein55e88b22015-01-21 15:50:13 -08001216 }
mtklein21eaf3b2016-02-08 12:39:59 -08001217
1218 // With the parallel work running, run serial tasks and tests here on main thread.
1219 for (auto task : serial) { Task::Run(task); }
1220 for (auto test : gSerialTests) { run_test(test); }
1221
1222 // Wait for any remaining parallel work to complete (including any spun off of serial tasks).
1223 parallel.wait();
mtkleincd50bca2016-01-05 06:20:20 -08001224 gDefinitelyThreadSafeWork.wait();
1225
mtkleinda884c42016-02-24 18:00:23 -08001226 // We'd better have run everything.
1227 SkASSERT(gPending == 0);
1228
mtklein748ca3b2015-01-15 10:56:12 -08001229 // At this point we're back in single-threaded land.
caryclarkf53ce802015-06-15 06:48:30 -07001230 sk_tool_utils::release_portable_typefaces();
mtklein@google.comd36522d2013-10-16 13:02:15 +00001231
mtklein748ca3b2015-01-15 10:56:12 -08001232 if (gFailures.count() > 0) {
1233 SkDebugf("Failures:\n");
1234 for (int i = 0; i < gFailures.count(); i++) {
mtklein9dc09102015-01-15 15:47:33 -08001235 SkDebugf("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001236 }
1237 SkDebugf("%d failures\n", gFailures.count());
1238 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001239 }
mtkleinafae30a2016-02-24 12:28:32 -08001240
1241#ifdef SK_PDF_IMAGE_STATS
halcanary7a14b312015-10-01 07:28:13 -07001242 SkPDFImageDumpStats();
mtkleinafae30a2016-02-24 12:28:32 -08001243#endif // SK_PDF_IMAGE_STATS
1244
1245 print_status();
scroggo4d9eaea2016-01-29 07:48:33 -08001246 SkDebugf("Finished!\n");
mtklein748ca3b2015-01-15 10:56:12 -08001247 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +00001248}
jcgregorio3b27ade2014-11-13 08:06:40 -08001249
kkinnunen179a8f52015-11-20 13:32:24 -08001250// TODO: currently many GPU tests are declared outside SK_SUPPORT_GPU guards.
1251// Thus we export the empty RunWithGPUTestContexts when SK_SUPPORT_GPU=0.
1252namespace skiatest {
1253namespace {
1254typedef void(*TestWithGrContext)(skiatest::Reporter*, GrContext*);
1255typedef void(*TestWithGrContextAndGLContext)(skiatest::Reporter*, GrContext*, SkGLContext*);
1256#if SK_SUPPORT_GPU
1257template<typename T>
kkinnunen34058002016-01-06 23:49:30 -08001258void call_test(T test, skiatest::Reporter* reporter, const GrContextFactory::ContextInfo& context);
kkinnunen179a8f52015-11-20 13:32:24 -08001259template<>
1260void call_test(TestWithGrContext test, skiatest::Reporter* reporter,
kkinnunen34058002016-01-06 23:49:30 -08001261 const GrContextFactory::ContextInfo& context) {
1262 test(reporter, context.fGrContext);
kkinnunen179a8f52015-11-20 13:32:24 -08001263}
1264template<>
1265void call_test(TestWithGrContextAndGLContext test, skiatest::Reporter* reporter,
kkinnunen34058002016-01-06 23:49:30 -08001266 const GrContextFactory::ContextInfo& context) {
1267 test(reporter, context.fGrContext, context.fGLContext);
kkinnunen179a8f52015-11-20 13:32:24 -08001268}
1269#endif
1270} // namespace
1271
kkinnunen179a8f52015-11-20 13:32:24 -08001272template<typename T>
1273void RunWithGPUTestContexts(T test, GPUTestContexts testContexts, Reporter* reporter,
1274 GrContextFactory* factory) {
1275#if SK_SUPPORT_GPU
kkinnunen3e980c32015-12-23 01:33:00 -08001276 // Iterate over context types, except use "native" instead of explicitly trying OpenGL and
1277 // OpenGL ES. Do not use GLES on desktop, since tests do not account for not fixing
1278 // http://skbug.com/2809
1279 GrContextFactory::GLContextType contextTypes[] = {
1280 GrContextFactory::kNative_GLContextType,
1281#if SK_ANGLE
1282#ifdef SK_BUILD_FOR_WIN
1283 GrContextFactory::kANGLE_GLContextType,
1284#endif
1285 GrContextFactory::kANGLE_GL_GLContextType,
1286#endif
1287#if SK_COMMAND_BUFFER
kkinnunen45c2c812016-02-25 02:03:43 -08001288 GrContextFactory::kCommandBufferES2_GLContextType,
1289 GrContextFactory::kCommandBufferES3_GLContextType,
kkinnunen3e980c32015-12-23 01:33:00 -08001290#endif
1291#if SK_MESA
1292 GrContextFactory::kMESA_GLContextType,
1293#endif
1294 GrContextFactory::kNull_GLContextType,
1295 GrContextFactory::kDebug_GLContextType,
1296 };
1297 static_assert(SK_ARRAY_COUNT(contextTypes) == GrContextFactory::kGLContextTypeCnt - 2,
1298 "Skipping unexpected GLContextType for GPU tests");
1299
1300 for (auto& contextType : contextTypes) {
kkinnunen179a8f52015-11-20 13:32:24 -08001301 int contextSelector = kNone_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001302 if (GrContextFactory::IsRenderingGLContext(contextType)) {
kkinnunen179a8f52015-11-20 13:32:24 -08001303 contextSelector |= kAllRendering_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001304 } else if (contextType == GrContextFactory::kNative_GLContextType) {
kkinnunen179a8f52015-11-20 13:32:24 -08001305 contextSelector |= kNative_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001306 } else if (contextType == GrContextFactory::kNull_GLContextType) {
kkinnunen179a8f52015-11-20 13:32:24 -08001307 contextSelector |= kNull_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001308 } else if (contextType == GrContextFactory::kDebug_GLContextType) {
kkinnunen55eeae92015-12-10 23:19:29 -08001309 contextSelector |= kDebug_GPUTestContexts;
kkinnunen179a8f52015-11-20 13:32:24 -08001310 }
1311 if ((testContexts & contextSelector) == 0) {
1312 continue;
1313 }
kkinnunen34058002016-01-06 23:49:30 -08001314 GrContextFactory::ContextInfo context = factory->getContextInfo(contextType);
1315 if (context.fGrContext) {
kkinnunen5219fd92015-12-10 06:28:13 -08001316 call_test(test, reporter, context);
1317 }
kkinnunen34058002016-01-06 23:49:30 -08001318 context = factory->getContextInfo(contextType,
1319 GrContextFactory::kEnableNVPR_GLContextOptions);
1320 if (context.fGrContext) {
kkinnunen179a8f52015-11-20 13:32:24 -08001321 call_test(test, reporter, context);
1322 }
1323 }
1324#endif
1325}
1326
1327template
1328void RunWithGPUTestContexts<TestWithGrContext>(TestWithGrContext test,
1329 GPUTestContexts testContexts,
1330 Reporter* reporter,
1331 GrContextFactory* factory);
1332template
1333void RunWithGPUTestContexts<TestWithGrContextAndGLContext>(TestWithGrContextAndGLContext test,
1334 GPUTestContexts testContexts,
1335 Reporter* reporter,
1336 GrContextFactory* factory);
1337} // namespace skiatest
1338
borenet48087572015-04-02 12:16:36 -07001339#if !defined(SK_BUILD_FOR_IOS)
jcgregorio3b27ade2014-11-13 08:06:40 -08001340int main(int argc, char** argv) {
1341 SkCommandLineFlags::Parse(argc, argv);
1342 return dm_main();
1343}
1344#endif