blob: bceaff7e8d8071003cae6e5da9c77d3e9d24fd02 [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
Dan Albert6c50b7a2015-10-23 10:47:24 -07008#include <unistd.h>
9
jcgregorio3b27ade2014-11-13 08:06:40 -080010#include "CrashHandler.h"
mtklein748ca3b2015-01-15 10:56:12 -080011#include "DMJsonWriter.h"
12#include "DMSrcSink.h"
tomhudsoneebc39a2015-02-23 12:18:05 -080013#include "DMSrcSinkAndroid.h"
mtklein748ca3b2015-01-15 10:56:12 -080014#include "ProcStats.h"
halcanary7d571242016-02-24 17:59:16 -080015#include "Resources.h"
mtklein748ca3b2015-01-15 10:56:12 -080016#include "SkBBHFactory.h"
mtklein62bd1a62015-01-27 14:46:26 -080017#include "SkChecksum.h"
Tom Hudson2880df22015-10-29 09:55:42 -040018#include "SkCodec.h"
caryclark17f0b6d2014-07-22 10:15:34 -070019#include "SkCommonFlags.h"
kkinnunen3e980c32015-12-23 01:33:00 -080020#include "SkCommonFlagsConfig.h"
caryclark83ca6282015-06-10 09:31:09 -070021#include "SkFontMgr.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +000022#include "SkGraphics.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"
mtklein246ba3a2016-02-23 10:39:36 -080026#include "SkSpinlock.h"
mtkleina82f5622015-02-20 12:30:19 -080027#include "SkTHash.h"
mtklein406654b2014-09-03 15:34:37 -070028#include "SkTaskGroup.h"
mtkleinde6fc2b2015-03-12 06:28:54 -070029#include "SkThreadUtils.h"
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +000030#include "Test.h"
mtklein748ca3b2015-01-15 10:56:12 -080031#include "Timer.h"
caryclark83ca6282015-06-10 09:31:09 -070032#include "sk_tool_utils.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +000033
Tom Hudson2880df22015-10-29 09:55:42 -040034#ifdef SK_PDF_IMAGE_STATS
35extern void SkPDFImageDumpStats();
36#endif
37
Tom Hudson2880df22015-10-29 09:55:42 -040038#include "png.h"
39
40#include <stdlib.h>
41
scroggo27a58342015-10-28 08:56:41 -070042#ifndef SK_BUILD_FOR_WIN32
43 #include <unistd.h>
44#endif
45
djsollen54416de2015-04-03 07:24:48 -070046DEFINE_string(src, "tests gm skp image", "Source types to test.");
mtklein748ca3b2015-01-15 10:56:12 -080047DEFINE_bool(nameByHash, false,
48 "If true, write to FLAGS_writePath[0]/<hash>.png instead of "
djsollen54416de2015-04-03 07:24:48 -070049 "to FLAGS_writePath[0]/<config>/<sourceType>/<sourceOptions>/<name>.png");
mtklein748ca3b2015-01-15 10:56:12 -080050DEFINE_bool2(pathOpsExtended, x, false, "Run extended pathOps tests.");
mtkleind603b222015-02-17 11:13:33 -080051DEFINE_string(matrix, "1 0 0 1",
52 "2x2 scale+skew matrix to apply or upright when using "
53 "'matrix' or 'upright' in config.");
mtklein82d28432015-01-15 12:46:02 -080054DEFINE_bool(gpu_threading, false, "Allow GPU work to run on multiple threads?");
mtklein@google.comd36522d2013-10-16 13:02:15 +000055
mtkleina2ef6422015-01-15 13:44:22 -080056DEFINE_string(blacklist, "",
djsollen54416de2015-04-03 07:24:48 -070057 "Space-separated config/src/srcOptions/name quadruples to blacklist. '_' matches anything. E.g. \n"
58 "'--blacklist gpu skp _ _' will blacklist all SKPs drawn into the gpu config.\n"
59 "'--blacklist gpu skp _ _ 8888 gm _ aarects' will also blacklist the aarects GM on 8888.");
mtkleina2ef6422015-01-15 13:44:22 -080060
mtklein62bd1a62015-01-27 14:46:26 -080061DEFINE_string2(readPath, r, "", "If set check for equality with golden results in this directory.");
62
borenet09ed4802015-04-03 14:15:33 -070063DEFINE_string(uninterestingHashesFile, "",
64 "File containing a list of uninteresting hashes. If a result hashes to something in "
65 "this list, no image is written for that result.");
66
mtklein6393c062015-04-27 08:45:01 -070067DEFINE_int32(shards, 1, "We're splitting source data into this many shards.");
68DEFINE_int32(shard, 0, "Which shard do I run?");
scroggoe4499842016-02-25 11:03:47 -080069DEFINE_bool(simpleCodec, false, "Only decode images to native scale");
mtklein6393c062015-04-27 08:45:01 -070070
mtklein748ca3b2015-01-15 10:56:12 -080071using namespace DM;
mtklein@google.comd36522d2013-10-16 13:02:15 +000072
mtklein748ca3b2015-01-15 10:56:12 -080073/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
74
75SK_DECLARE_STATIC_MUTEX(gFailuresMutex);
76static SkTArray<SkString> gFailures;
77
mtklein3eed7dd2016-02-24 19:07:07 -080078static void fail(const SkString& err) {
mtklein748ca3b2015-01-15 10:56:12 -080079 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);
mtklein3eed7dd2016-02-24 19:07:07 -080087static int32_t gPending;
88static SkTArray<SkString> gRunning;
mtklein748ca3b2015-01-15 10:56:12 -080089
mtklein3eed7dd2016-02-24 19:07:07 -080090static void done(const char* config, const char* src, const char* srcOptions, const char* name) {
91 SkString id = SkStringPrintf("%s %s %s %s", config, src, srcOptions, name);
mtkleinafae30a2016-02-24 12:28:32 -080092 int pending;
mtkleinb37cb412015-03-18 05:27:14 -070093 {
mtkleinafae30a2016-02-24 12:28:32 -080094 SkAutoTAcquire<SkPODSpinlock> lock(gMutex);
mtkleinb37cb412015-03-18 05:27:14 -070095 for (int i = 0; i < gRunning.count(); i++) {
96 if (gRunning[i] == id) {
97 gRunning.removeShuffle(i);
98 break;
99 }
100 }
mtkleinafae30a2016-02-24 12:28:32 -0800101 pending = --gPending;
reed50bc0512015-05-19 14:13:31 -0700102 }
mtkleina17241b2015-01-23 05:48:00 -0800103 // We write our dm.json file every once in a while in case we crash.
104 // Notice this also handles the final dm.json when pending == 0.
105 if (pending % 500 == 0) {
106 JsonWriter::DumpJson();
107 }
mtklein@google.comd36522d2013-10-16 13:02:15 +0000108}
109
mtklein3eed7dd2016-02-24 19:07:07 -0800110static void start(const char* config, const char* src, const char* srcOptions, const char* name) {
111 SkString id = SkStringPrintf("%s %s %s %s", config, src, srcOptions, name);
mtkleinafae30a2016-02-24 12:28:32 -0800112 SkAutoTAcquire<SkPODSpinlock> lock(gMutex);
mtkleinb37cb412015-03-18 05:27:14 -0700113 gRunning.push_back(id);
114}
115
mtkleinafae30a2016-02-24 12:28:32 -0800116static void print_status() {
117 static SkMSec start_ms = SkTime::GetMSecs();
118
119 int curr = sk_tools::getCurrResidentSetSizeMB(),
120 peak = sk_tools::getMaxResidentSetSizeMB();
121 SkString elapsed = HumanizeMs(SkTime::GetMSecs() - start_ms);
122
123 SkAutoTAcquire<SkPODSpinlock> lock(gMutex);
124 SkDebugf("\n%s elapsed, %d active, %d queued, %dMB RAM, %dMB peak\n",
125 elapsed.c_str(), gRunning.count(), gPending - gRunning.count(), curr, peak);
126 for (auto& task : gRunning) {
127 SkDebugf("\t%s\n", task.c_str());
128 }
129}
130
mtklein246ba3a2016-02-23 10:39:36 -0800131#if defined(SK_BUILD_FOR_WIN32)
132 static void setup_crash_handler() {
133 // TODO: custom crash handler like below to print out what was running
134 SetupCrashHandler();
135 }
136
137#else
138 #include <signal.h>
139 static void setup_crash_handler() {
140 const int kSignals[] = { SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGSEGV };
141 for (int sig : kSignals) {
142 signal(sig, [](int sig) {
mtkleinafae30a2016-02-24 12:28:32 -0800143 SkDebugf("\nCaught signal %d [%s].\n", sig, strsignal(sig));
144 print_status();
mtklein246ba3a2016-02-23 10:39:36 -0800145 });
146 }
147 }
148#endif
149
mtklein748ca3b2015-01-15 10:56:12 -0800150/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
mtklein114c3cd2015-01-15 10:15:02 -0800151
mtklein62bd1a62015-01-27 14:46:26 -0800152struct Gold : public SkString {
mtkleina82f5622015-02-20 12:30:19 -0800153 Gold() : SkString("") {}
mtklein3eed7dd2016-02-24 19:07:07 -0800154 Gold(const SkString& sink, const SkString& src,
155 const SkString& srcOptions, const SkString& name,
156 const SkString& md5)
mtklein62bd1a62015-01-27 14:46:26 -0800157 : SkString("") {
158 this->append(sink);
159 this->append(src);
djsollen54416de2015-04-03 07:24:48 -0700160 this->append(srcOptions);
mtklein62bd1a62015-01-27 14:46:26 -0800161 this->append(name);
162 this->append(md5);
mtklein62bd1a62015-01-27 14:46:26 -0800163 }
Tom Hudson2880df22015-10-29 09:55:42 -0400164 struct Hash {
165 uint32_t operator()(const Gold& g) const {
166 return SkGoodHash()((const SkString&)g);
167 }
168 };
mtklein62bd1a62015-01-27 14:46:26 -0800169};
mtkleina82f5622015-02-20 12:30:19 -0800170static SkTHashSet<Gold, Gold::Hash> gGold;
mtklein62bd1a62015-01-27 14:46:26 -0800171
172static void add_gold(JsonWriter::BitmapResult r) {
djsollen54416de2015-04-03 07:24:48 -0700173 gGold.add(Gold(r.config, r.sourceType, r.sourceOptions, r.name, r.md5));
mtklein62bd1a62015-01-27 14:46:26 -0800174}
175
176static void gather_gold() {
177 if (!FLAGS_readPath.isEmpty()) {
178 SkString path(FLAGS_readPath[0]);
179 path.append("/dm.json");
180 if (!JsonWriter::ReadJson(path.c_str(), add_gold)) {
181 fail(SkStringPrintf("Couldn't read %s for golden results.", path.c_str()));
182 }
183 }
184}
185
186/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
187
borenet09ed4802015-04-03 14:15:33 -0700188static SkTHashSet<SkString> gUninterestingHashes;
189
190static void gather_uninteresting_hashes() {
191 if (!FLAGS_uninterestingHashesFile.isEmpty()) {
192 SkAutoTUnref<SkData> data(SkData::NewFromFileName(FLAGS_uninterestingHashesFile[0]));
Tom Hudson2880df22015-10-29 09:55:42 -0400193 if (!data) {
194 SkDebugf("WARNING: unable to read uninteresting hashes from %s\n",
195 FLAGS_uninterestingHashesFile[0]);
196 return;
197 }
borenet09ed4802015-04-03 14:15:33 -0700198 SkTArray<SkString> hashes;
199 SkStrSplit((const char*)data->data(), "\n", &hashes);
200 for (const SkString& hash : hashes) {
201 gUninterestingHashes.add(hash);
202 }
mtklein136baaa2016-02-03 11:21:45 -0800203 SkDebugf("FYI: loaded %d distinct uninteresting hashes from %d lines\n",
204 gUninterestingHashes.count(), hashes.count());
borenet09ed4802015-04-03 14:15:33 -0700205 }
206}
207
208/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
209
Tom Hudson2880df22015-10-29 09:55:42 -0400210struct TaggedSrc : public SkAutoTDelete<Src> {
mtklein3eed7dd2016-02-24 19:07:07 -0800211 SkString tag;
212 SkString options;
Tom Hudson2880df22015-10-29 09:55:42 -0400213};
214
215struct TaggedSink : public SkAutoTDelete<Sink> {
kkinnunen3e980c32015-12-23 01:33:00 -0800216 SkString tag;
djsollen54416de2015-04-03 07:24:48 -0700217};
mtklein748ca3b2015-01-15 10:56:12 -0800218
219static const bool kMemcpyOK = true;
220
Tom Hudson2880df22015-10-29 09:55:42 -0400221static SkTArray<TaggedSrc, kMemcpyOK> gSrcs;
222static SkTArray<TaggedSink, kMemcpyOK> gSinks;
mtklein748ca3b2015-01-15 10:56:12 -0800223
mtklein6393c062015-04-27 08:45:01 -0700224static bool in_shard() {
225 static int N = 0;
226 return N++ % FLAGS_shards == FLAGS_shard;
227}
228
mtklein3eed7dd2016-02-24 19:07:07 -0800229static void push_src(const char* tag, ImplicitString options, Src* s) {
mtklein748ca3b2015-01-15 10:56:12 -0800230 SkAutoTDelete<Src> src(s);
mtklein6393c062015-04-27 08:45:01 -0700231 if (in_shard() &&
mtklein3eed7dd2016-02-24 19:07:07 -0800232 FLAGS_src.contains(tag) &&
mtklein748ca3b2015-01-15 10:56:12 -0800233 !SkCommandLineFlags::ShouldSkip(FLAGS_match, src->name().c_str())) {
Tom Hudson2880df22015-10-29 09:55:42 -0400234 TaggedSrc& s = gSrcs.push_back();
mtklein748ca3b2015-01-15 10:56:12 -0800235 s.reset(src.detach());
236 s.tag = tag;
djsollen54416de2015-04-03 07:24:48 -0700237 s.options = options;
mtklein114c3cd2015-01-15 10:15:02 -0800238 }
mtklein748ca3b2015-01-15 10:56:12 -0800239}
mtklein114c3cd2015-01-15 10:15:02 -0800240
Tom Hudson2880df22015-10-29 09:55:42 -0400241static void push_codec_src(Path path, CodecSrc::Mode mode, CodecSrc::DstColorType dstColorType,
scroggoc5560be2016-02-03 09:42:42 -0800242 SkAlphaType dstAlphaType, float scale) {
scroggoe4499842016-02-25 11:03:47 -0800243 if (FLAGS_simpleCodec) {
244 if (mode != CodecSrc::kCodec_Mode || dstColorType != CodecSrc::kGetFromCanvas_DstColorType
245 || scale != 1.0f)
246 // Only decode in the simple case.
247 return;
248 }
Tom Hudson2880df22015-10-29 09:55:42 -0400249 SkString folder;
250 switch (mode) {
251 case CodecSrc::kCodec_Mode:
252 folder.append("codec");
253 break;
msarettbb25b532016-01-13 09:31:39 -0800254 case CodecSrc::kCodecZeroInit_Mode:
255 folder.append("codec_zero_init");
256 break;
Tom Hudson2880df22015-10-29 09:55:42 -0400257 case CodecSrc::kScanline_Mode:
258 folder.append("scanline");
259 break;
Tom Hudson2880df22015-10-29 09:55:42 -0400260 case CodecSrc::kStripe_Mode:
261 folder.append("stripe");
262 break;
msarett91c22b22016-02-22 12:27:46 -0800263 case CodecSrc::kCroppedScanline_Mode:
264 folder.append("crop");
265 break;
Tom Hudson2880df22015-10-29 09:55:42 -0400266 case CodecSrc::kSubset_Mode:
267 folder.append("codec_subset");
268 break;
msarettb714fb02016-01-22 14:46:42 -0800269 case CodecSrc::kGen_Mode:
270 folder.append("gen");
271 break;
Tom Hudson2880df22015-10-29 09:55:42 -0400272 }
273
274 switch (dstColorType) {
275 case CodecSrc::kGrayscale_Always_DstColorType:
276 folder.append("_kGray8");
277 break;
278 case CodecSrc::kIndex8_Always_DstColorType:
279 folder.append("_kIndex8");
280 break;
281 default:
282 break;
283 }
284
scroggoc5560be2016-02-03 09:42:42 -0800285 switch (dstAlphaType) {
286 case kOpaque_SkAlphaType:
287 folder.append("_opaque");
288 break;
289 case kPremul_SkAlphaType:
290 folder.append("_premul");
291 break;
292 case kUnpremul_SkAlphaType:
293 folder.append("_unpremul");
294 break;
295 default:
296 break;
297 }
298
Tom Hudson2880df22015-10-29 09:55:42 -0400299 if (1.0f != scale) {
300 folder.appendf("_%.3f", scale);
301 }
302
scroggoc5560be2016-02-03 09:42:42 -0800303 CodecSrc* src = new CodecSrc(path, mode, dstColorType, dstAlphaType, scale);
Tom Hudson2880df22015-10-29 09:55:42 -0400304 push_src("image", folder, src);
305}
306
307static void push_android_codec_src(Path path, AndroidCodecSrc::Mode mode,
scroggoc5560be2016-02-03 09:42:42 -0800308 CodecSrc::DstColorType dstColorType, SkAlphaType dstAlphaType, int sampleSize) {
Tom Hudson2880df22015-10-29 09:55:42 -0400309 SkString folder;
310 switch (mode) {
311 case AndroidCodecSrc::kFullImage_Mode:
312 folder.append("scaled_codec");
313 break;
314 case AndroidCodecSrc::kDivisor_Mode:
315 folder.append("scaled_codec_divisor");
316 break;
317 }
318
319 switch (dstColorType) {
320 case CodecSrc::kGrayscale_Always_DstColorType:
321 folder.append("_kGray8");
322 break;
323 case CodecSrc::kIndex8_Always_DstColorType:
324 folder.append("_kIndex8");
325 break;
326 default:
327 break;
328 }
329
scroggoc5560be2016-02-03 09:42:42 -0800330 switch (dstAlphaType) {
331 case kOpaque_SkAlphaType:
332 folder.append("_opaque");
333 break;
334 case kPremul_SkAlphaType:
335 folder.append("_premul");
336 break;
msarett9e9444c2016-02-03 12:39:10 -0800337 case kUnpremul_SkAlphaType:
338 folder.append("_unpremul");
339 break;
scroggoc5560be2016-02-03 09:42:42 -0800340 default:
341 break;
342 }
343
Tom Hudson2880df22015-10-29 09:55:42 -0400344 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800345 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
Tom Hudson2880df22015-10-29 09:55:42 -0400346 }
347
scroggoc5560be2016-02-03 09:42:42 -0800348 AndroidCodecSrc* src = new AndroidCodecSrc(path, mode, dstColorType, dstAlphaType, sampleSize);
Tom Hudson2880df22015-10-29 09:55:42 -0400349 push_src("image", folder, src);
350}
351
msarett438b2ad2015-04-09 12:43:10 -0700352static void push_codec_srcs(Path path) {
353 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
354 if (!encoded) {
355 SkDebugf("Couldn't read %s.", path.c_str());
356 return;
357 }
358 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
Tom Hudson2880df22015-10-29 09:55:42 -0400359 if (nullptr == codec.get()) {
msarett438b2ad2015-04-09 12:43:10 -0700360 SkDebugf("Couldn't create codec for %s.", path.c_str());
361 return;
362 }
363
Tom Hudson2880df22015-10-29 09:55:42 -0400364 // Native Scales
Tom Hudson2880df22015-10-29 09:55:42 -0400365 // SkJpegCodec natively supports scaling to: 0.125, 0.25, 0.375, 0.5, 0.625, 0.75, 0.875
366 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 -0700367
msarett91c22b22016-02-22 12:27:46 -0800368 SkTArray<CodecSrc::Mode> nativeModes;
369 nativeModes.push_back(CodecSrc::kCodec_Mode);
370 nativeModes.push_back(CodecSrc::kCodecZeroInit_Mode);
371 nativeModes.push_back(CodecSrc::kGen_Mode);
372 switch (codec->getEncodedFormat()) {
373 case SkEncodedFormat::kJPEG_SkEncodedFormat:
374 nativeModes.push_back(CodecSrc::kScanline_Mode);
375 nativeModes.push_back(CodecSrc::kStripe_Mode);
376 nativeModes.push_back(CodecSrc::kCroppedScanline_Mode);
Tom Hudson2880df22015-10-29 09:55:42 -0400377 break;
msarett91c22b22016-02-22 12:27:46 -0800378 case SkEncodedFormat::kWEBP_SkEncodedFormat:
379 nativeModes.push_back(CodecSrc::kSubset_Mode);
380 break;
msarettb65e6042016-02-23 05:37:25 -0800381 case SkEncodedFormat::kRAW_SkEncodedFormat:
Tom Hudson2880df22015-10-29 09:55:42 -0400382 break;
383 default:
msarett91c22b22016-02-22 12:27:46 -0800384 nativeModes.push_back(CodecSrc::kScanline_Mode);
385 nativeModes.push_back(CodecSrc::kStripe_Mode);
386 break;
387 }
msarett9e707a02015-09-01 14:57:57 -0700388
msarett91c22b22016-02-22 12:27:46 -0800389 SkTArray<CodecSrc::DstColorType> colorTypes;
390 colorTypes.push_back(CodecSrc::kGetFromCanvas_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700391 switch (codec->getInfo().colorType()) {
392 case kGray_8_SkColorType:
msarett91c22b22016-02-22 12:27:46 -0800393 colorTypes.push_back(CodecSrc::kGrayscale_Always_DstColorType);
msarett55f7bdd2016-02-16 13:24:54 -0800394 if (kWBMP_SkEncodedFormat == codec->getEncodedFormat()) {
msarett91c22b22016-02-22 12:27:46 -0800395 colorTypes.push_back(CodecSrc::kIndex8_Always_DstColorType);
msarett55f7bdd2016-02-16 13:24:54 -0800396 }
msarett36c37962015-09-02 13:20:52 -0700397 break;
398 case kIndex_8_SkColorType:
msarett91c22b22016-02-22 12:27:46 -0800399 colorTypes.push_back(CodecSrc::kIndex8_Always_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700400 break;
401 default:
Tom Hudson2880df22015-10-29 09:55:42 -0400402 break;
403 }
404
scroggoc5560be2016-02-03 09:42:42 -0800405 SkTArray<SkAlphaType> alphaModes;
406 alphaModes.push_back(kPremul_SkAlphaType);
msarett9e9444c2016-02-03 12:39:10 -0800407 alphaModes.push_back(kUnpremul_SkAlphaType);
scroggoc5560be2016-02-03 09:42:42 -0800408 if (codec->getInfo().alphaType() == kOpaque_SkAlphaType) {
409 alphaModes.push_back(kOpaque_SkAlphaType);
410 }
msarettb714fb02016-01-22 14:46:42 -0800411
412 for (CodecSrc::Mode mode : nativeModes) {
413 // SkCodecImageGenerator only runs for the default colorType
414 // recommended by SkCodec. There is no need to generate multiple
415 // tests for different colorTypes.
416 // TODO (msarett): Add scaling support to SkCodecImageGenerator.
417 if (CodecSrc::kGen_Mode == mode) {
418 // FIXME: The gpu backend does not draw kGray sources correctly. (skbug.com/4822)
419 if (kGray_8_SkColorType != codec->getInfo().colorType()) {
scroggoc5560be2016-02-03 09:42:42 -0800420 push_codec_src(path, mode, CodecSrc::kGetFromCanvas_DstColorType,
421 codec->getInfo().alphaType(), 1.0f);
msarettb714fb02016-01-22 14:46:42 -0800422 }
423 continue;
424 }
425
426 for (float scale : nativeScales) {
msarett91c22b22016-02-22 12:27:46 -0800427 for (CodecSrc::DstColorType colorType : colorTypes) {
scroggoc5560be2016-02-03 09:42:42 -0800428 for (SkAlphaType alphaType : alphaModes) {
msarett91c22b22016-02-22 12:27:46 -0800429 // Only test kCroppedScanline_Mode when the alpha type is opaque. The test is
430 // slow and won't be interestingly different with different alpha types.
431 if (CodecSrc::kCroppedScanline_Mode == mode &&
432 kOpaque_SkAlphaType != alphaType) {
433 continue;
434 }
435
436 push_codec_src(path, mode, colorType, alphaType, scale);
scroggoc5560be2016-02-03 09:42:42 -0800437 }
Tom Hudson2880df22015-10-29 09:55:42 -0400438 }
439 }
440 }
441
scroggoe4499842016-02-25 11:03:47 -0800442 if (FLAGS_simpleCodec) {
443 return;
444 }
445
halcanary6950de62015-11-07 05:29:00 -0800446 // https://bug.skia.org/4428
msarett33c76232015-11-16 13:43:40 -0800447 bool subset = false;
448 // The following image types are supported by BitmapRegionDecoder,
449 // so we will test full image decodes and subset decodes.
msarettbe8216a2015-12-04 08:00:50 -0800450 static const char* const exts[] = {
Tom Hudson2880df22015-10-29 09:55:42 -0400451 "jpg", "jpeg", "png", "webp",
452 "JPG", "JPEG", "PNG", "WEBP",
453 };
msarettbe8216a2015-12-04 08:00:50 -0800454 for (const char* ext : exts) {
Tom Hudson2880df22015-10-29 09:55:42 -0400455 if (path.endsWith(ext)) {
msarett33c76232015-11-16 13:43:40 -0800456 subset = true;
Tom Hudson2880df22015-10-29 09:55:42 -0400457 break;
458 }
459 }
msarett33c76232015-11-16 13:43:40 -0800460
Tom Hudson2880df22015-10-29 09:55:42 -0400461 const int sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
462
Tom Hudson2880df22015-10-29 09:55:42 -0400463 for (int sampleSize : sampleSizes) {
msarett91c22b22016-02-22 12:27:46 -0800464 for (CodecSrc::DstColorType colorType : colorTypes) {
scroggoc5560be2016-02-03 09:42:42 -0800465 for (SkAlphaType alphaType : alphaModes) {
msarett91c22b22016-02-22 12:27:46 -0800466 push_android_codec_src(path, AndroidCodecSrc::kFullImage_Mode, colorType,
scroggoc5560be2016-02-03 09:42:42 -0800467 alphaType, sampleSize);
468 if (subset) {
msarett91c22b22016-02-22 12:27:46 -0800469 push_android_codec_src(path, AndroidCodecSrc::kDivisor_Mode, colorType,
scroggoc5560be2016-02-03 09:42:42 -0800470 alphaType, sampleSize);
471 }
Tom Hudson2880df22015-10-29 09:55:42 -0400472 }
473 }
msaretta5783ae2015-09-08 15:35:32 -0700474 }
475}
476
msarett5cb48852015-11-06 08:56:32 -0800477static bool brd_color_type_supported(SkBitmapRegionDecoder::Strategy strategy,
Tom Hudson2880df22015-10-29 09:55:42 -0400478 CodecSrc::DstColorType dstColorType) {
479 switch (strategy) {
msarett5cb48852015-11-06 08:56:32 -0800480 case SkBitmapRegionDecoder::kCanvas_Strategy:
Tom Hudson2880df22015-10-29 09:55:42 -0400481 if (CodecSrc::kGetFromCanvas_DstColorType == dstColorType) {
482 return true;
483 }
484 return false;
msarett5cb48852015-11-06 08:56:32 -0800485 case SkBitmapRegionDecoder::kAndroidCodec_Strategy:
Tom Hudson2880df22015-10-29 09:55:42 -0400486 switch (dstColorType) {
487 case CodecSrc::kGetFromCanvas_DstColorType:
488 case CodecSrc::kIndex8_Always_DstColorType:
489 case CodecSrc::kGrayscale_Always_DstColorType:
490 return true;
491 default:
492 return false;
493 }
494 default:
495 SkASSERT(false);
496 return false;
497 }
498}
499
msarett5cb48852015-11-06 08:56:32 -0800500static void push_brd_src(Path path, SkBitmapRegionDecoder::Strategy strategy,
Tom Hudson2880df22015-10-29 09:55:42 -0400501 CodecSrc::DstColorType dstColorType, BRDSrc::Mode mode, uint32_t sampleSize) {
502 SkString folder;
503 switch (strategy) {
msarett5cb48852015-11-06 08:56:32 -0800504 case SkBitmapRegionDecoder::kCanvas_Strategy:
Tom Hudson2880df22015-10-29 09:55:42 -0400505 folder.append("brd_canvas");
506 break;
msarett5cb48852015-11-06 08:56:32 -0800507 case SkBitmapRegionDecoder::kAndroidCodec_Strategy:
Tom Hudson2880df22015-10-29 09:55:42 -0400508 folder.append("brd_android_codec");
509 break;
510 default:
511 SkASSERT(false);
512 return;
513 }
514
515 switch (mode) {
516 case BRDSrc::kFullImage_Mode:
517 break;
518 case BRDSrc::kDivisor_Mode:
519 folder.append("_divisor");
520 break;
521 default:
522 SkASSERT(false);
523 return;
524 }
525
526 switch (dstColorType) {
527 case CodecSrc::kGetFromCanvas_DstColorType:
528 break;
529 case CodecSrc::kIndex8_Always_DstColorType:
530 folder.append("_kIndex");
531 break;
532 case CodecSrc::kGrayscale_Always_DstColorType:
533 folder.append("_kGray");
534 break;
535 default:
536 SkASSERT(false);
537 return;
538 }
539
540 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800541 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
Tom Hudson2880df22015-10-29 09:55:42 -0400542 }
543
544 BRDSrc* src = new BRDSrc(path, strategy, mode, dstColorType, sampleSize);
545 push_src("image", folder, src);
546}
547
548static void push_brd_srcs(Path path) {
549
msarett5cb48852015-11-06 08:56:32 -0800550 const SkBitmapRegionDecoder::Strategy strategies[] = {
551 SkBitmapRegionDecoder::kCanvas_Strategy,
msarett5cb48852015-11-06 08:56:32 -0800552 SkBitmapRegionDecoder::kAndroidCodec_Strategy,
Tom Hudson2880df22015-10-29 09:55:42 -0400553 };
554
scroggo501b7342015-11-03 07:55:11 -0800555 // Test on a variety of sampleSizes, making sure to include:
556 // - 2, 4, and 8, which are natively supported by jpeg
557 // - multiples of 2 which are not divisible by 4 (analogous for 4)
558 // - larger powers of two, since BRD clients generally use powers of 2
559 // We will only produce output for the larger sizes on large images.
560 const uint32_t sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 24, 32, 64 };
Tom Hudson2880df22015-10-29 09:55:42 -0400561
562 // We will only test to one backend (8888), but we will test all of the
563 // color types that we need to decode to on this backend.
564 const CodecSrc::DstColorType dstColorTypes[] = {
565 CodecSrc::kGetFromCanvas_DstColorType,
566 CodecSrc::kIndex8_Always_DstColorType,
567 CodecSrc::kGrayscale_Always_DstColorType,
568 };
569
570 const BRDSrc::Mode modes[] = {
571 BRDSrc::kFullImage_Mode,
572 BRDSrc::kDivisor_Mode,
573 };
574
msarett5cb48852015-11-06 08:56:32 -0800575 for (SkBitmapRegionDecoder::Strategy strategy : strategies) {
Tom Hudson2880df22015-10-29 09:55:42 -0400576 for (uint32_t sampleSize : sampleSizes) {
Tom Hudson2880df22015-10-29 09:55:42 -0400577 for (CodecSrc::DstColorType dstColorType : dstColorTypes) {
578 if (brd_color_type_supported(strategy, dstColorType)) {
579 for (BRDSrc::Mode mode : modes) {
580 push_brd_src(path, strategy, dstColorType, mode, sampleSize);
581 }
582 }
583 }
584 }
585 }
586}
587
588static bool brd_supported(const char* ext) {
msarett8c8f22a2015-04-01 06:58:48 -0700589 static const char* const exts[] = {
Tom Hudson2880df22015-10-29 09:55:42 -0400590 "jpg", "jpeg", "png", "webp",
591 "JPG", "JPEG", "PNG", "WEBP",
msarett8c8f22a2015-04-01 06:58:48 -0700592 };
593
594 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
595 if (0 == strcmp(exts[i], ext)) {
596 return true;
597 }
598 }
599 return false;
scroggo9b77ddd2015-03-19 06:03:39 -0700600}
601
scroggo86737142016-02-03 12:19:11 -0800602static bool gather_srcs() {
mtklein748ca3b2015-01-15 10:56:12 -0800603 for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) {
djsollen54416de2015-04-03 07:24:48 -0700604 push_src("gm", "", new GMSrc(r->factory()));
mtklein748ca3b2015-01-15 10:56:12 -0800605 }
halcanaryfc37ad12015-01-30 07:31:19 -0800606 for (int i = 0; i < FLAGS_skps.count(); i++) {
607 const char* path = FLAGS_skps[i];
608 if (sk_isdir(path)) {
609 SkOSFile::Iter it(path, "skp");
610 for (SkString file; it.next(&file); ) {
djsollen54416de2015-04-03 07:24:48 -0700611 push_src("skp", "", new SKPSrc(SkOSPath::Join(path, file.c_str())));
halcanaryfc37ad12015-01-30 07:31:19 -0800612 }
613 } else {
djsollen54416de2015-04-03 07:24:48 -0700614 push_src("skp", "", new SKPSrc(path));
mtklein114c3cd2015-01-15 10:15:02 -0800615 }
616 }
scroggo86737142016-02-03 12:19:11 -0800617
618 SkTArray<SkString> images;
619 if (!CollectImages(&images)) {
620 return false;
621 }
622
623 for (auto image : images) {
624 push_codec_srcs(image);
scroggoe4499842016-02-25 11:03:47 -0800625 if (FLAGS_simpleCodec) {
626 continue;
627 }
628
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) {
mtklein748ca3b2015-01-15 10:56:12 -0800639 SkAutoTDelete<Sink> sink(s);
kkinnunen3e980c32015-12-23 01:33:00 -0800640
Tom Hudson2880df22015-10-29 09:55:42 -0400641 // Try a simple Src as a canary. If it fails, skip this sink.
mtklein748ca3b2015-01-15 10:56:12 -0800642 struct : public Src {
Tom Hudson2880df22015-10-29 09:55:42 -0400643 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); }
Tom Hudson2880df22015-10-29 09:55:42 -0400648 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;
Tom Hudson2880df22015-10-29 09:55:42 -0400654 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
Tom Hudson2880df22015-10-29 09:55:42 -0400660 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}
mtklein748ca3b2015-01-15 10:56:12 -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);
Tom Hudson2880df22015-10-29 09:55:42 -0400705 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
Tom Hudson2880df22015-10-29 09:55:42 -0400713 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__); }
Tom Hudson2880df22015-10-29 09:55:42 -0400718 VIA("twice", ViaTwice, wrapped);
Tom Hudson2880df22015-10-29 09:55:42 -0400719 VIA("serialize", ViaSerialization, wrapped);
mtklein4a34ecb2016-01-08 10:19:35 -0800720 VIA("pic", ViaPicture, wrapped);
Tom Hudson2880df22015-10-29 09:55:42 -0400721 VIA("2ndpic", ViaSecondPicture, wrapped);
mtkleind31c13d2015-05-05 12:59:56 -0700722 VIA("sp", ViaSingletonPictures, wrapped);
Tom Hudson2880df22015-10-29 09:55:42 -0400723 VIA("tiles", ViaTiles, 256, 256, nullptr, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800724 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
Tom Hudson2880df22015-10-29 09:55:42 -0400725 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
Tom Hudson2880df22015-10-29 09:55:42 -0400745 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);
Tom Hudson2880df22015-10-29 09:55:42 -0400764 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;
Tom Hudson2880df22015-10-29 09:55:42 -0400768 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
Tom Hudson2880df22015-10-29 09:55:42 -0400779static 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.
800 bitmap.reset();
801
802 FILE* f = fopen(path, "wb");
803 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
818 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
829 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";
834 text[1].text = (png_charp)description.c_str();
835 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) {
Tom Hudson2880df22015-10-29 09:55:42 -0400855 return 0 == strcmp("_", needle) || nullptr != strstr(haystack, needle);
mtkleina2ef6422015-01-15 13:44:22 -0800856}
857
mtklein3eed7dd2016-02-24 19:07:07 -0800858static bool 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)) {
mtklein3eed7dd2016-02-24 19:07:07 -0800865 return true;
mtkleina2ef6422015-01-15 13:44:22 -0800866 }
867 }
mtklein3eed7dd2016-02-24 19:07:07 -0800868 return false;
mtkleina2ef6422015-01-15 13:44:22 -0800869}
870
mtkleincd50bca2016-01-05 06:20:20 -0800871// Even when a Task Sink reports to be non-threadsafe (e.g. GPU), we know things like
872// .png encoding are definitely thread safe. This lets us offload that work to CPU threads.
873static SkTaskGroup gDefinitelyThreadSafeWork;
874
mtklein748ca3b2015-01-15 10:56:12 -0800875// The finest-grained unit of work we can run: draw a single Src into a single Sink,
876// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
877struct Task {
Tom Hudson2880df22015-10-29 09:55:42 -0400878 Task(const TaggedSrc& src, const TaggedSink& sink) : src(src), sink(sink) {}
879 const TaggedSrc& src;
880 const TaggedSink& sink;
mtklein748ca3b2015-01-15 10:56:12 -0800881
mtklein21eaf3b2016-02-08 12:39:59 -0800882 static void Run(const Task& task) {
883 SkString name = task.src->name();
Tom Hudson2880df22015-10-29 09:55:42 -0400884
mtkleinb9eb4ac2015-02-02 18:26:03 -0800885 SkString log;
mtklein3eed7dd2016-02-24 19:07:07 -0800886 if (!FLAGS_dryRun) {
mtklein748ca3b2015-01-15 10:56:12 -0800887 SkBitmap bitmap;
888 SkDynamicMemoryWStream stream;
mtklein3eed7dd2016-02-24 19:07:07 -0800889 start(task.sink.tag.c_str(), task.src.tag.c_str(),
890 task.src.options.c_str(), name.c_str());
mtklein21eaf3b2016-02-08 12:39:59 -0800891 Error err = task.sink->draw(*task.src, &bitmap, &stream, &log);
mtklein748ca3b2015-01-15 10:56:12 -0800892 if (!err.isEmpty()) {
mtklein4089ef72015-03-05 08:40:28 -0800893 if (err.isFatal()) {
djsollen54416de2015-04-03 07:24:48 -0700894 fail(SkStringPrintf("%s %s %s %s: %s",
mtklein21eaf3b2016-02-08 12:39:59 -0800895 task.sink.tag.c_str(),
896 task.src.tag.c_str(),
897 task.src.options.c_str(),
mtklein4089ef72015-03-05 08:40:28 -0800898 name.c_str(),
899 err.c_str()));
mtkleinb37cb412015-03-18 05:27:14 -0700900 } else {
mtklein3eed7dd2016-02-24 19:07:07 -0800901 done(task.sink.tag.c_str(), task.src.tag.c_str(),
902 task.src.options.c_str(), name.c_str());
mtkleinba6ada72016-01-21 09:39:35 -0800903 return;
mtklein4089ef72015-03-05 08:40:28 -0800904 }
mtklein748ca3b2015-01-15 10:56:12 -0800905 }
mtklein62bd1a62015-01-27 14:46:26 -0800906
mtkleincd50bca2016-01-05 06:20:20 -0800907 // We're likely switching threads here, so we must capture by value, [=] or [foo,bar].
908 SkStreamAsset* data = stream.detachAsStream();
909 gDefinitelyThreadSafeWork.add([task,name,bitmap,data]{
910 SkAutoTDelete<SkStreamAsset> ownedData(data);
911
912 // Why doesn't the copy constructor do this when we have pre-locked pixels?
913 bitmap.lockPixels();
914
915 SkString md5;
916 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
917 SkMD5 hash;
918 if (data->getLength()) {
919 hash.writeStream(data, data->getLength());
920 data->rewind();
mtklein38408462015-07-08 07:25:27 -0700921 } else {
mtkleincd50bca2016-01-05 06:20:20 -0800922 // If we're BGRA (Linux, Windows), swizzle over to RGBA (Mac, Android).
923 // This helps eliminate multiple 0-pixel-diff hashes on gold.skia.org.
924 // (Android's general slow speed breaks the tie arbitrarily in RGBA's favor.)
925 // We might consider promoting 565 to RGBA too.
926 if (bitmap.colorType() == kBGRA_8888_SkColorType) {
927 SkBitmap swizzle;
928 SkAssertResult(bitmap.copyTo(&swizzle, kRGBA_8888_SkColorType));
929 hash.write(swizzle.getPixels(), swizzle.getSize());
930 } else {
931 hash.write(bitmap.getPixels(), bitmap.getSize());
932 }
933 }
934 SkMD5::Digest digest;
935 hash.finish(digest);
936 for (int i = 0; i < 16; i++) {
937 md5.appendf("%02x", digest.data[i]);
mtklein38408462015-07-08 07:25:27 -0700938 }
mtklein62bd1a62015-01-27 14:46:26 -0800939 }
mtklein62bd1a62015-01-27 14:46:26 -0800940
mtkleincd50bca2016-01-05 06:20:20 -0800941 if (!FLAGS_readPath.isEmpty() &&
mtklein3eed7dd2016-02-24 19:07:07 -0800942 !gGold.contains(Gold(task.sink.tag, task.src.tag,
943 task.src.options, name, md5))) {
mtkleincd50bca2016-01-05 06:20:20 -0800944 fail(SkStringPrintf("%s not found for %s %s %s %s in %s",
945 md5.c_str(),
mtklein21eaf3b2016-02-08 12:39:59 -0800946 task.sink.tag.c_str(),
947 task.src.tag.c_str(),
948 task.src.options.c_str(),
mtkleincd50bca2016-01-05 06:20:20 -0800949 name.c_str(),
950 FLAGS_readPath[0]));
mtklein748ca3b2015-01-15 10:56:12 -0800951 }
mtkleincd50bca2016-01-05 06:20:20 -0800952
953 if (!FLAGS_writePath.isEmpty()) {
mtklein21eaf3b2016-02-08 12:39:59 -0800954 const char* ext = task.sink->fileExtension();
mtkleincd50bca2016-01-05 06:20:20 -0800955 if (data->getLength()) {
mtklein21eaf3b2016-02-08 12:39:59 -0800956 WriteToDisk(task, md5, ext, data, data->getLength(), nullptr);
mtkleincd50bca2016-01-05 06:20:20 -0800957 SkASSERT(bitmap.drawsNothing());
958 } else if (!bitmap.drawsNothing()) {
mtklein21eaf3b2016-02-08 12:39:59 -0800959 WriteToDisk(task, md5, ext, nullptr, 0, &bitmap);
mtkleincd50bca2016-01-05 06:20:20 -0800960 }
961 }
962 });
mtklein748ca3b2015-01-15 10:56:12 -0800963 }
mtklein3eed7dd2016-02-24 19:07:07 -0800964 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 -0800965 }
966
967 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -0800968 SkString md5,
969 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -0800970 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -0800971 const SkBitmap* bitmap) {
mtklein748ca3b2015-01-15 10:56:12 -0800972 JsonWriter::BitmapResult result;
djsollen54416de2015-04-03 07:24:48 -0700973 result.name = task.src->name();
mtklein3eed7dd2016-02-24 19:07:07 -0800974 result.config = task.sink.tag;
djsollen54416de2015-04-03 07:24:48 -0700975 result.sourceType = task.src.tag;
976 result.sourceOptions = task.src.options;
977 result.ext = ext;
978 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -0800979 JsonWriter::AddBitmapResult(result);
980
mtkleinb0531a72015-04-07 13:38:48 -0700981 // If an MD5 is uninteresting, we want it noted in the JSON file,
982 // but don't want to dump it out as a .png (or whatever ext is).
983 if (gUninterestingHashes.contains(md5)) {
984 return;
985 }
986
mtklein748ca3b2015-01-15 10:56:12 -0800987 const char* dir = FLAGS_writePath[0];
988 if (0 == strcmp(dir, "@")) { // Needed for iOS.
989 dir = FLAGS_resourcePath[0];
990 }
991 sk_mkdir(dir);
992
993 SkString path;
994 if (FLAGS_nameByHash) {
995 path = SkOSPath::Join(dir, result.md5.c_str());
996 path.append(".");
997 path.append(ext);
998 if (sk_exists(path.c_str())) {
999 return; // Content-addressed. If it exists already, we're done.
1000 }
1001 } else {
kkinnunen3e980c32015-12-23 01:33:00 -08001002 path = SkOSPath::Join(dir, task.sink.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001003 sk_mkdir(path.c_str());
Tom Hudson2880df22015-10-29 09:55:42 -04001004 path = SkOSPath::Join(path.c_str(), task.src.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001005 sk_mkdir(path.c_str());
Tom Hudson2880df22015-10-29 09:55:42 -04001006 if (strcmp(task.src.options.c_str(), "") != 0) {
1007 path = SkOSPath::Join(path.c_str(), task.src.options.c_str());
djsollen54416de2015-04-03 07:24:48 -07001008 sk_mkdir(path.c_str());
1009 }
mtklein748ca3b2015-01-15 10:56:12 -08001010 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
1011 path.append(".");
1012 path.append(ext);
1013 }
1014
mtklein748ca3b2015-01-15 10:56:12 -08001015 if (bitmap) {
Tom Hudson2880df22015-10-29 09:55:42 -04001016 if (!dump_png(*bitmap, path.c_str(), result.md5.c_str())) {
mtklein748ca3b2015-01-15 10:56:12 -08001017 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
1018 return;
1019 }
1020 } else {
Tom Hudson2880df22015-10-29 09:55:42 -04001021 SkFILEWStream file(path.c_str());
1022 if (!file.isValid()) {
1023 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
1024 return;
1025 }
mtklein748ca3b2015-01-15 10:56:12 -08001026 if (!file.writeStream(data, len)) {
1027 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
1028 return;
1029 }
1030 }
1031 }
1032};
1033
mtklein748ca3b2015-01-15 10:56:12 -08001034/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1035
1036// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
1037
mtklein21eaf3b2016-02-08 12:39:59 -08001038static SkTDArray<skiatest::Test> gParallelTests, gSerialTests;
mtklein748ca3b2015-01-15 10:56:12 -08001039
1040static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -08001041 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -08001042 return;
1043 }
mtklein6393c062015-04-27 08:45:01 -07001044 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r; r = r->next()) {
1045 if (!in_shard()) {
1046 continue;
1047 }
halcanary87f3ba42015-01-20 09:30:20 -08001048 // Despite its name, factory() is returning a reference to
1049 // link-time static const POD data.
1050 const skiatest::Test& test = r->factory();
1051 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -08001052 continue;
1053 }
halcanary87f3ba42015-01-20 09:30:20 -08001054 if (test.needsGpu && gpu_supported()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001055 (FLAGS_gpu_threading ? gParallelTests : gSerialTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -08001056 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein21eaf3b2016-02-08 12:39:59 -08001057 gParallelTests.push(test);
mtklein82d28432015-01-15 12:46:02 -08001058 }
mtklein748ca3b2015-01-15 10:56:12 -08001059 }
1060}
1061
mtklein21eaf3b2016-02-08 12:39:59 -08001062static void run_test(skiatest::Test test) {
halcanary87f3ba42015-01-20 09:30:20 -08001063 struct : public skiatest::Reporter {
mtklein36352bf2015-03-25 18:17:31 -07001064 void reportFailed(const skiatest::Failure& failure) override {
halcanary87f3ba42015-01-20 09:30:20 -08001065 fail(failure.toString());
1066 JsonWriter::AddTestFailure(failure);
1067 }
mtklein36352bf2015-03-25 18:17:31 -07001068 bool allowExtendedTest() const override {
halcanary87f3ba42015-01-20 09:30:20 -08001069 return FLAGS_pathOpsExtended;
1070 }
mtklein36352bf2015-03-25 18:17:31 -07001071 bool verbose() const override { return FLAGS_veryVerbose; }
halcanary87f3ba42015-01-20 09:30:20 -08001072 } reporter;
djsollen824996a2015-06-12 12:06:22 -07001073
mtklein3eed7dd2016-02-24 19:07:07 -08001074 if (!FLAGS_dryRun && !is_blacklisted("_", "tests", "_", test.name)) {
mtklein21eaf3b2016-02-08 12:39:59 -08001075 start("unit", "test", "", test.name);
mtklein55e88b22015-01-21 15:50:13 -08001076 GrContextFactory factory;
mtklein21eaf3b2016-02-08 12:39:59 -08001077 test.proc(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -08001078 }
mtklein3eed7dd2016-02-24 19:07:07 -08001079 done("unit", "test", "", test.name);
mtklein748ca3b2015-01-15 10:56:12 -08001080}
1081
1082/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1083
mtkleinafae30a2016-02-24 12:28:32 -08001084DEFINE_int32(status_sec, 15, "Print status this often (and if we crash).");
1085
1086SkThread* start_status_thread() {
1087 auto thread = new SkThread([] (void*) {
1088 for (;;) {
1089 print_status();
1090 #if defined(SK_BUILD_FOR_WIN)
1091 Sleep(FLAGS_status_sec * 1000);
1092 #else
1093 sleep(FLAGS_status_sec);
1094 #endif
mtklein2e1c47e2015-03-12 07:16:56 -07001095 }
mtkleinafae30a2016-02-24 12:28:32 -08001096 });
1097 thread->start();
1098 return thread;
mtkleinde6fc2b2015-03-12 06:28:54 -07001099}
1100
caryclark83ca6282015-06-10 09:31:09 -07001101#define PORTABLE_FONT_PREFIX "Toy Liberation "
1102
1103static SkTypeface* create_from_name(const char familyName[], SkTypeface::Style style) {
1104 if (familyName && strlen(familyName) > sizeof(PORTABLE_FONT_PREFIX)
1105 && !strncmp(familyName, PORTABLE_FONT_PREFIX, sizeof(PORTABLE_FONT_PREFIX) - 1)) {
Tom Hudson2880df22015-10-29 09:55:42 -04001106 return sk_tool_utils::create_portable_typeface(familyName, style);
caryclark83ca6282015-06-10 09:31:09 -07001107 }
Tom Hudson2880df22015-10-29 09:55:42 -04001108 return nullptr;
caryclark83ca6282015-06-10 09:31:09 -07001109}
1110
1111#undef PORTABLE_FONT_PREFIX
1112
1113extern SkTypeface* (*gCreateTypefaceDelegate)(const char [], SkTypeface::Style );
1114
jcgregorio3b27ade2014-11-13 08:06:40 -08001115int dm_main();
caryclark17f0b6d2014-07-22 10:15:34 -07001116int dm_main() {
mtklein246ba3a2016-02-23 10:39:36 -08001117 setup_crash_handler();
mtkleinafae30a2016-02-24 12:28:32 -08001118
mtklein5286f022016-01-22 08:18:14 -08001119 JsonWriter::DumpJson(); // It's handy for the bots to assume this is ~never missing.
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +00001120 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -07001121 SkTaskGroup::Enabler enabled(FLAGS_threads);
caryclark83ca6282015-06-10 09:31:09 -07001122 gCreateTypefaceDelegate = &create_from_name;
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +00001123
halcanary7d571242016-02-24 17:59:16 -08001124 {
1125 SkString testResourcePath = GetResourcePath("color_wheel.png");
1126 SkFILEStream testResource(testResourcePath.c_str());
1127 if (!testResource.isValid()) {
1128 SkDebugf("Some resources are missing. Do you need to set --resourcePath?\n");
1129 }
1130 }
mtklein62bd1a62015-01-27 14:46:26 -08001131 gather_gold();
borenet09ed4802015-04-03 14:15:33 -07001132 gather_uninteresting_hashes();
mtklein62bd1a62015-01-27 14:46:26 -08001133
scroggo86737142016-02-03 12:19:11 -08001134 if (!gather_srcs()) {
1135 return 1;
1136 }
mtklein748ca3b2015-01-15 10:56:12 -08001137 gather_sinks();
1138 gather_tests();
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +00001139
mtklein21eaf3b2016-02-08 12:39:59 -08001140 gPending = gSrcs.count() * gSinks.count() + gParallelTests.count() + gSerialTests.count();
mtkleinafae30a2016-02-24 12:28:32 -08001141 SkDebugf("%d srcs * %d sinks + %d tests == %d tasks",
mtklein21eaf3b2016-02-08 12:39:59 -08001142 gSrcs.count(), gSinks.count(), gParallelTests.count() + gSerialTests.count(), gPending);
mtkleinafae30a2016-02-24 12:28:32 -08001143 SkAutoTDelete<SkThread> statusThread(start_status_thread());
mtklein748ca3b2015-01-15 10:56:12 -08001144
mtklein21eaf3b2016-02-08 12:39:59 -08001145 // Kick off as much parallel work as we can, making note of any serial work we'll need to do.
1146 SkTaskGroup parallel;
1147 SkTArray<Task> serial;
1148
1149 for (auto& sink : gSinks)
1150 for (auto& src : gSrcs) {
mtklein3eed7dd2016-02-24 19:07:07 -08001151 if (src->veto(sink->flags()) ||
1152 is_blacklisted(sink.tag.c_str(), src.tag.c_str(),
1153 src.options.c_str(), src->name().c_str())) {
1154 SkAutoTAcquire<SkPODSpinlock> lock(gMutex);
1155 gPending--;
1156 continue;
1157 }
1158
mtklein21eaf3b2016-02-08 12:39:59 -08001159 Task task(src, sink);
1160 if (src->serial() || sink->serial()) {
1161 serial.push_back(task);
1162 } else {
1163 parallel.add([task] { Task::Run(task); });
mtklein748ca3b2015-01-15 10:56:12 -08001164 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001165 }
mtklein21eaf3b2016-02-08 12:39:59 -08001166 for (auto test : gParallelTests) {
1167 parallel.add([test] { run_test(test); });
mtklein55e88b22015-01-21 15:50:13 -08001168 }
mtklein21eaf3b2016-02-08 12:39:59 -08001169
1170 // With the parallel work running, run serial tasks and tests here on main thread.
1171 for (auto task : serial) { Task::Run(task); }
1172 for (auto test : gSerialTests) { run_test(test); }
1173
1174 // Wait for any remaining parallel work to complete (including any spun off of serial tasks).
1175 parallel.wait();
mtkleincd50bca2016-01-05 06:20:20 -08001176 gDefinitelyThreadSafeWork.wait();
1177
mtkleinda884c42016-02-24 18:00:23 -08001178 // We'd better have run everything.
1179 SkASSERT(gPending == 0);
1180
mtklein748ca3b2015-01-15 10:56:12 -08001181 // At this point we're back in single-threaded land.
caryclarkf53ce802015-06-15 06:48:30 -07001182 sk_tool_utils::release_portable_typefaces();
mtklein@google.comd36522d2013-10-16 13:02:15 +00001183
mtklein748ca3b2015-01-15 10:56:12 -08001184 if (gFailures.count() > 0) {
1185 SkDebugf("Failures:\n");
1186 for (int i = 0; i < gFailures.count(); i++) {
mtklein9dc09102015-01-15 15:47:33 -08001187 SkDebugf("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001188 }
1189 SkDebugf("%d failures\n", gFailures.count());
1190 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001191 }
mtkleinafae30a2016-02-24 12:28:32 -08001192
1193#ifdef SK_PDF_IMAGE_STATS
Tom Hudson2880df22015-10-29 09:55:42 -04001194 SkPDFImageDumpStats();
mtkleinafae30a2016-02-24 12:28:32 -08001195#endif // SK_PDF_IMAGE_STATS
1196
1197 print_status();
scroggo4d9eaea2016-01-29 07:48:33 -08001198 SkDebugf("Finished!\n");
mtklein748ca3b2015-01-15 10:56:12 -08001199 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +00001200}
jcgregorio3b27ade2014-11-13 08:06:40 -08001201
kkinnunen179a8f52015-11-20 13:32:24 -08001202// TODO: currently many GPU tests are declared outside SK_SUPPORT_GPU guards.
1203// Thus we export the empty RunWithGPUTestContexts when SK_SUPPORT_GPU=0.
1204namespace skiatest {
1205namespace {
1206typedef void(*TestWithGrContext)(skiatest::Reporter*, GrContext*);
1207typedef void(*TestWithGrContextAndGLContext)(skiatest::Reporter*, GrContext*, SkGLContext*);
1208#if SK_SUPPORT_GPU
1209template<typename T>
kkinnunen34058002016-01-06 23:49:30 -08001210void call_test(T test, skiatest::Reporter* reporter, const GrContextFactory::ContextInfo& context);
kkinnunen179a8f52015-11-20 13:32:24 -08001211template<>
1212void call_test(TestWithGrContext test, skiatest::Reporter* reporter,
kkinnunen34058002016-01-06 23:49:30 -08001213 const GrContextFactory::ContextInfo& context) {
1214 test(reporter, context.fGrContext);
kkinnunen179a8f52015-11-20 13:32:24 -08001215}
1216template<>
1217void call_test(TestWithGrContextAndGLContext test, skiatest::Reporter* reporter,
kkinnunen34058002016-01-06 23:49:30 -08001218 const GrContextFactory::ContextInfo& context) {
1219 test(reporter, context.fGrContext, context.fGLContext);
kkinnunen179a8f52015-11-20 13:32:24 -08001220}
1221#endif
1222} // namespace
1223
kkinnunen179a8f52015-11-20 13:32:24 -08001224template<typename T>
1225void RunWithGPUTestContexts(T test, GPUTestContexts testContexts, Reporter* reporter,
1226 GrContextFactory* factory) {
1227#if SK_SUPPORT_GPU
kkinnunen3e980c32015-12-23 01:33:00 -08001228 // Iterate over context types, except use "native" instead of explicitly trying OpenGL and
1229 // OpenGL ES. Do not use GLES on desktop, since tests do not account for not fixing
1230 // http://skbug.com/2809
1231 GrContextFactory::GLContextType contextTypes[] = {
1232 GrContextFactory::kNative_GLContextType,
1233#if SK_ANGLE
1234#ifdef SK_BUILD_FOR_WIN
1235 GrContextFactory::kANGLE_GLContextType,
1236#endif
1237 GrContextFactory::kANGLE_GL_GLContextType,
1238#endif
1239#if SK_COMMAND_BUFFER
kkinnunen45c2c812016-02-25 02:03:43 -08001240 GrContextFactory::kCommandBufferES2_GLContextType,
1241 GrContextFactory::kCommandBufferES3_GLContextType,
kkinnunen3e980c32015-12-23 01:33:00 -08001242#endif
1243#if SK_MESA
1244 GrContextFactory::kMESA_GLContextType,
1245#endif
1246 GrContextFactory::kNull_GLContextType,
1247 GrContextFactory::kDebug_GLContextType,
1248 };
1249 static_assert(SK_ARRAY_COUNT(contextTypes) == GrContextFactory::kGLContextTypeCnt - 2,
1250 "Skipping unexpected GLContextType for GPU tests");
1251
1252 for (auto& contextType : contextTypes) {
kkinnunen179a8f52015-11-20 13:32:24 -08001253 int contextSelector = kNone_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001254 if (GrContextFactory::IsRenderingGLContext(contextType)) {
kkinnunen179a8f52015-11-20 13:32:24 -08001255 contextSelector |= kAllRendering_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001256 } else if (contextType == GrContextFactory::kNative_GLContextType) {
kkinnunen179a8f52015-11-20 13:32:24 -08001257 contextSelector |= kNative_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001258 } else if (contextType == GrContextFactory::kNull_GLContextType) {
kkinnunen179a8f52015-11-20 13:32:24 -08001259 contextSelector |= kNull_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001260 } else if (contextType == GrContextFactory::kDebug_GLContextType) {
kkinnunen55eeae92015-12-10 23:19:29 -08001261 contextSelector |= kDebug_GPUTestContexts;
kkinnunen179a8f52015-11-20 13:32:24 -08001262 }
1263 if ((testContexts & contextSelector) == 0) {
1264 continue;
1265 }
kkinnunen34058002016-01-06 23:49:30 -08001266 GrContextFactory::ContextInfo context = factory->getContextInfo(contextType);
1267 if (context.fGrContext) {
kkinnunen5219fd92015-12-10 06:28:13 -08001268 call_test(test, reporter, context);
1269 }
kkinnunen34058002016-01-06 23:49:30 -08001270 context = factory->getContextInfo(contextType,
1271 GrContextFactory::kEnableNVPR_GLContextOptions);
1272 if (context.fGrContext) {
kkinnunen179a8f52015-11-20 13:32:24 -08001273 call_test(test, reporter, context);
1274 }
1275 }
1276#endif
1277}
1278
1279template
1280void RunWithGPUTestContexts<TestWithGrContext>(TestWithGrContext test,
1281 GPUTestContexts testContexts,
1282 Reporter* reporter,
1283 GrContextFactory* factory);
1284template
1285void RunWithGPUTestContexts<TestWithGrContextAndGLContext>(TestWithGrContextAndGLContext test,
1286 GPUTestContexts testContexts,
1287 Reporter* reporter,
1288 GrContextFactory* factory);
1289} // namespace skiatest
1290
borenet48087572015-04-02 12:16:36 -07001291#if !defined(SK_BUILD_FOR_IOS)
jcgregorio3b27ade2014-11-13 08:06:40 -08001292int main(int argc, char** argv) {
1293 SkCommandLineFlags::Parse(argc, argv);
1294 return dm_main();
1295}
1296#endif