blob: f37fc8b7fff8eeab49ac62ce5f0a098580aacb03 [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"
caryclark17f0b6d2014-07-22 10:15:34 -070017#include "SkCommonFlags.h"
kkinnunen3e980c32015-12-23 01:33:00 -080018#include "SkCommonFlagsConfig.h"
caryclark83ca6282015-06-10 09:31:09 -070019#include "SkFontMgr.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +000020#include "SkForceLinking.h"
21#include "SkGraphics.h"
mtklein748ca3b2015-01-15 10:56:12 -080022#include "SkMD5.h"
mtklein1b249332015-07-07 12:21:21 -070023#include "SkMutex.h"
mtklein1d0f1642014-09-08 08:05:18 -070024#include "SkOSFile.h"
mtklein246ba3a2016-02-23 10:39:36 -080025#include "SkSpinlock.h"
mtkleina82f5622015-02-20 12:30:19 -080026#include "SkTHash.h"
mtklein406654b2014-09-03 15:34:37 -070027#include "SkTaskGroup.h"
mtkleinde6fc2b2015-03-12 06:28:54 -070028#include "SkThreadUtils.h"
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +000029#include "Test.h"
mtklein748ca3b2015-01-15 10:56:12 -080030#include "Timer.h"
caryclark83ca6282015-06-10 09:31:09 -070031#include "sk_tool_utils.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +000032
halcanary7a14b312015-10-01 07:28:13 -070033#ifdef SK_PDF_IMAGE_STATS
34extern void SkPDFImageDumpStats();
35#endif
36
mtkleina5114d72015-08-24 13:27:01 -070037#include "png.h"
38
bungeman60e0fee2015-08-26 05:15:46 -070039#include <stdlib.h>
40
scroggo27a58342015-10-28 08:56:41 -070041#ifndef SK_BUILD_FOR_WIN32
42 #include <unistd.h>
43#endif
44
djsollen54416de2015-04-03 07:24:48 -070045DEFINE_string(src, "tests gm skp image", "Source types to test.");
mtklein748ca3b2015-01-15 10:56:12 -080046DEFINE_bool(nameByHash, false,
47 "If true, write to FLAGS_writePath[0]/<hash>.png instead of "
djsollen54416de2015-04-03 07:24:48 -070048 "to FLAGS_writePath[0]/<config>/<sourceType>/<sourceOptions>/<name>.png");
mtklein748ca3b2015-01-15 10:56:12 -080049DEFINE_bool2(pathOpsExtended, x, false, "Run extended pathOps tests.");
mtkleind603b222015-02-17 11:13:33 -080050DEFINE_string(matrix, "1 0 0 1",
51 "2x2 scale+skew matrix to apply or upright when using "
52 "'matrix' or 'upright' in config.");
mtklein82d28432015-01-15 12:46:02 -080053DEFINE_bool(gpu_threading, false, "Allow GPU work to run on multiple threads?");
mtklein@google.comd36522d2013-10-16 13:02:15 +000054
mtkleina2ef6422015-01-15 13:44:22 -080055DEFINE_string(blacklist, "",
djsollen54416de2015-04-03 07:24:48 -070056 "Space-separated config/src/srcOptions/name quadruples to blacklist. '_' matches anything. E.g. \n"
57 "'--blacklist gpu skp _ _' will blacklist all SKPs drawn into the gpu config.\n"
58 "'--blacklist gpu skp _ _ 8888 gm _ aarects' will also blacklist the aarects GM on 8888.");
mtkleina2ef6422015-01-15 13:44:22 -080059
mtklein62bd1a62015-01-27 14:46:26 -080060DEFINE_string2(readPath, r, "", "If set check for equality with golden results in this directory.");
61
borenet09ed4802015-04-03 14:15:33 -070062DEFINE_string(uninterestingHashesFile, "",
63 "File containing a list of uninteresting hashes. If a result hashes to something in "
64 "this list, no image is written for that result.");
65
mtklein6393c062015-04-27 08:45:01 -070066DEFINE_int32(shards, 1, "We're splitting source data into this many shards.");
67DEFINE_int32(shard, 0, "Which shard do I run?");
68
mtklein@google.comd36522d2013-10-16 13:02:15 +000069__SK_FORCE_IMAGE_DECODER_LINKING;
mtklein748ca3b2015-01-15 10:56:12 -080070using namespace DM;
mtklein@google.comd36522d2013-10-16 13:02:15 +000071
mtklein748ca3b2015-01-15 10:56:12 -080072/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
73
74SK_DECLARE_STATIC_MUTEX(gFailuresMutex);
75static SkTArray<SkString> gFailures;
76
mtklein3eed7dd2016-02-24 19:07:07 -080077static void fail(const SkString& err) {
mtklein748ca3b2015-01-15 10:56:12 -080078 SkAutoMutexAcquire lock(gFailuresMutex);
79 SkDebugf("\n\nFAILURE: %s\n\n", err.c_str());
80 gFailures.push_back(err);
halcanary9e398f72015-01-10 11:18:04 -080081}
82
mtkleinb37cb412015-03-18 05:27:14 -070083
mtklein246ba3a2016-02-23 10:39:36 -080084// We use a spinlock to make locking this in a signal handler _somewhat_ safe.
mtkleinafae30a2016-02-24 12:28:32 -080085SK_DECLARE_STATIC_SPINLOCK(gMutex);
mtklein3eed7dd2016-02-24 19:07:07 -080086static int32_t gPending;
87static SkTArray<SkString> gRunning;
mtklein748ca3b2015-01-15 10:56:12 -080088
mtklein3eed7dd2016-02-24 19:07:07 -080089static void done(const char* config, const char* src, const char* srcOptions, const char* name) {
90 SkString id = SkStringPrintf("%s %s %s %s", config, src, srcOptions, name);
mtkleinafae30a2016-02-24 12:28:32 -080091 int pending;
mtkleinb37cb412015-03-18 05:27:14 -070092 {
mtkleinafae30a2016-02-24 12:28:32 -080093 SkAutoTAcquire<SkPODSpinlock> lock(gMutex);
mtkleinb37cb412015-03-18 05:27:14 -070094 for (int i = 0; i < gRunning.count(); i++) {
95 if (gRunning[i] == id) {
96 gRunning.removeShuffle(i);
97 break;
98 }
99 }
mtkleinafae30a2016-02-24 12:28:32 -0800100 pending = --gPending;
reed50bc0512015-05-19 14:13:31 -0700101 }
mtkleina17241b2015-01-23 05:48:00 -0800102 // We write our dm.json file every once in a while in case we crash.
103 // Notice this also handles the final dm.json when pending == 0.
104 if (pending % 500 == 0) {
105 JsonWriter::DumpJson();
106 }
mtklein@google.comd36522d2013-10-16 13:02:15 +0000107}
108
mtklein3eed7dd2016-02-24 19:07:07 -0800109static void start(const char* config, const char* src, const char* srcOptions, const char* name) {
110 SkString id = SkStringPrintf("%s %s %s %s", config, src, srcOptions, name);
mtkleinafae30a2016-02-24 12:28:32 -0800111 SkAutoTAcquire<SkPODSpinlock> lock(gMutex);
mtkleinb37cb412015-03-18 05:27:14 -0700112 gRunning.push_back(id);
113}
114
mtkleinafae30a2016-02-24 12:28:32 -0800115static void print_status() {
116 static SkMSec start_ms = SkTime::GetMSecs();
117
118 int curr = sk_tools::getCurrResidentSetSizeMB(),
119 peak = sk_tools::getMaxResidentSetSizeMB();
120 SkString elapsed = HumanizeMs(SkTime::GetMSecs() - start_ms);
121
122 SkAutoTAcquire<SkPODSpinlock> lock(gMutex);
123 SkDebugf("\n%s elapsed, %d active, %d queued, %dMB RAM, %dMB peak\n",
124 elapsed.c_str(), gRunning.count(), gPending - gRunning.count(), curr, peak);
125 for (auto& task : gRunning) {
126 SkDebugf("\t%s\n", task.c_str());
127 }
128}
129
mtklein246ba3a2016-02-23 10:39:36 -0800130#if defined(SK_BUILD_FOR_WIN32)
131 static void setup_crash_handler() {
132 // TODO: custom crash handler like below to print out what was running
133 SetupCrashHandler();
134 }
135
136#else
137 #include <signal.h>
138 static void setup_crash_handler() {
139 const int kSignals[] = { SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGSEGV };
140 for (int sig : kSignals) {
141 signal(sig, [](int sig) {
mtkleinafae30a2016-02-24 12:28:32 -0800142 SkDebugf("\nCaught signal %d [%s].\n", sig, strsignal(sig));
143 print_status();
mtklein246ba3a2016-02-23 10:39:36 -0800144 });
145 }
146 }
147#endif
148
mtklein748ca3b2015-01-15 10:56:12 -0800149/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
mtklein114c3cd2015-01-15 10:15:02 -0800150
mtklein62bd1a62015-01-27 14:46:26 -0800151struct Gold : public SkString {
mtkleina82f5622015-02-20 12:30:19 -0800152 Gold() : SkString("") {}
mtklein3eed7dd2016-02-24 19:07:07 -0800153 Gold(const SkString& sink, const SkString& src,
154 const SkString& srcOptions, const SkString& name,
155 const SkString& md5)
mtklein62bd1a62015-01-27 14:46:26 -0800156 : SkString("") {
157 this->append(sink);
158 this->append(src);
djsollen54416de2015-04-03 07:24:48 -0700159 this->append(srcOptions);
mtklein62bd1a62015-01-27 14:46:26 -0800160 this->append(name);
161 this->append(md5);
mtklein62bd1a62015-01-27 14:46:26 -0800162 }
mtkleinc8d1dd42015-10-15 12:23:01 -0700163 struct Hash {
164 uint32_t operator()(const Gold& g) const {
165 return SkGoodHash()((const SkString&)g);
166 }
167 };
mtklein62bd1a62015-01-27 14:46:26 -0800168};
mtkleina82f5622015-02-20 12:30:19 -0800169static SkTHashSet<Gold, Gold::Hash> gGold;
mtklein62bd1a62015-01-27 14:46:26 -0800170
171static void add_gold(JsonWriter::BitmapResult r) {
djsollen54416de2015-04-03 07:24:48 -0700172 gGold.add(Gold(r.config, r.sourceType, r.sourceOptions, r.name, r.md5));
mtklein62bd1a62015-01-27 14:46:26 -0800173}
174
175static void gather_gold() {
176 if (!FLAGS_readPath.isEmpty()) {
177 SkString path(FLAGS_readPath[0]);
178 path.append("/dm.json");
179 if (!JsonWriter::ReadJson(path.c_str(), add_gold)) {
180 fail(SkStringPrintf("Couldn't read %s for golden results.", path.c_str()));
181 }
182 }
183}
184
185/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
186
borenet09ed4802015-04-03 14:15:33 -0700187static SkTHashSet<SkString> gUninterestingHashes;
188
189static void gather_uninteresting_hashes() {
190 if (!FLAGS_uninterestingHashesFile.isEmpty()) {
191 SkAutoTUnref<SkData> data(SkData::NewFromFileName(FLAGS_uninterestingHashesFile[0]));
mtkleincc334b32015-09-22 11:43:53 -0700192 if (!data) {
193 SkDebugf("WARNING: unable to read uninteresting hashes from %s\n",
194 FLAGS_uninterestingHashesFile[0]);
195 return;
196 }
borenet09ed4802015-04-03 14:15:33 -0700197 SkTArray<SkString> hashes;
198 SkStrSplit((const char*)data->data(), "\n", &hashes);
199 for (const SkString& hash : hashes) {
200 gUninterestingHashes.add(hash);
201 }
mtklein136baaa2016-02-03 11:21:45 -0800202 SkDebugf("FYI: loaded %d distinct uninteresting hashes from %d lines\n",
203 gUninterestingHashes.count(), hashes.count());
borenet09ed4802015-04-03 14:15:33 -0700204 }
205}
206
207/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
208
mtkleine0effd62015-07-29 06:37:28 -0700209struct TaggedSrc : public SkAutoTDelete<Src> {
mtklein3eed7dd2016-02-24 19:07:07 -0800210 SkString tag;
211 SkString options;
mtkleine0effd62015-07-29 06:37:28 -0700212};
213
214struct TaggedSink : public SkAutoTDelete<Sink> {
kkinnunen3e980c32015-12-23 01:33:00 -0800215 SkString tag;
djsollen54416de2015-04-03 07:24:48 -0700216};
mtklein748ca3b2015-01-15 10:56:12 -0800217
218static const bool kMemcpyOK = true;
219
mtkleine0effd62015-07-29 06:37:28 -0700220static SkTArray<TaggedSrc, kMemcpyOK> gSrcs;
221static SkTArray<TaggedSink, kMemcpyOK> gSinks;
mtklein748ca3b2015-01-15 10:56:12 -0800222
mtklein6393c062015-04-27 08:45:01 -0700223static bool in_shard() {
224 static int N = 0;
225 return N++ % FLAGS_shards == FLAGS_shard;
226}
227
mtklein3eed7dd2016-02-24 19:07:07 -0800228static void push_src(const char* tag, ImplicitString options, Src* s) {
mtklein748ca3b2015-01-15 10:56:12 -0800229 SkAutoTDelete<Src> src(s);
mtklein6393c062015-04-27 08:45:01 -0700230 if (in_shard() &&
mtklein3eed7dd2016-02-24 19:07:07 -0800231 FLAGS_src.contains(tag) &&
mtklein748ca3b2015-01-15 10:56:12 -0800232 !SkCommandLineFlags::ShouldSkip(FLAGS_match, src->name().c_str())) {
mtkleine0effd62015-07-29 06:37:28 -0700233 TaggedSrc& s = gSrcs.push_back();
mtklein748ca3b2015-01-15 10:56:12 -0800234 s.reset(src.detach());
235 s.tag = tag;
djsollen54416de2015-04-03 07:24:48 -0700236 s.options = options;
mtklein114c3cd2015-01-15 10:15:02 -0800237 }
mtklein748ca3b2015-01-15 10:56:12 -0800238}
mtklein114c3cd2015-01-15 10:15:02 -0800239
msarett9e707a02015-09-01 14:57:57 -0700240static void push_codec_src(Path path, CodecSrc::Mode mode, CodecSrc::DstColorType dstColorType,
scroggoc5560be2016-02-03 09:42:42 -0800241 SkAlphaType dstAlphaType, float scale) {
msarett9e707a02015-09-01 14:57:57 -0700242 SkString folder;
243 switch (mode) {
244 case CodecSrc::kCodec_Mode:
245 folder.append("codec");
246 break;
msarettbb25b532016-01-13 09:31:39 -0800247 case CodecSrc::kCodecZeroInit_Mode:
248 folder.append("codec_zero_init");
249 break;
msarett9e707a02015-09-01 14:57:57 -0700250 case CodecSrc::kScanline_Mode:
251 folder.append("scanline");
252 break;
msarett9e707a02015-09-01 14:57:57 -0700253 case CodecSrc::kStripe_Mode:
254 folder.append("stripe");
255 break;
msarett91c22b22016-02-22 12:27:46 -0800256 case CodecSrc::kCroppedScanline_Mode:
257 folder.append("crop");
258 break;
msarett9e707a02015-09-01 14:57:57 -0700259 case CodecSrc::kSubset_Mode:
msarett36c37962015-09-02 13:20:52 -0700260 folder.append("codec_subset");
msarett9e707a02015-09-01 14:57:57 -0700261 break;
msarettb714fb02016-01-22 14:46:42 -0800262 case CodecSrc::kGen_Mode:
263 folder.append("gen");
264 break;
msarett9e707a02015-09-01 14:57:57 -0700265 }
266
267 switch (dstColorType) {
268 case CodecSrc::kGrayscale_Always_DstColorType:
269 folder.append("_kGray8");
270 break;
271 case CodecSrc::kIndex8_Always_DstColorType:
272 folder.append("_kIndex8");
273 break;
274 default:
275 break;
276 }
277
scroggoc5560be2016-02-03 09:42:42 -0800278 switch (dstAlphaType) {
279 case kOpaque_SkAlphaType:
280 folder.append("_opaque");
281 break;
282 case kPremul_SkAlphaType:
283 folder.append("_premul");
284 break;
285 case kUnpremul_SkAlphaType:
286 folder.append("_unpremul");
287 break;
288 default:
289 break;
290 }
291
msarett9e707a02015-09-01 14:57:57 -0700292 if (1.0f != scale) {
293 folder.appendf("_%.3f", scale);
294 }
295
scroggoc5560be2016-02-03 09:42:42 -0800296 CodecSrc* src = new CodecSrc(path, mode, dstColorType, dstAlphaType, scale);
msarett9e707a02015-09-01 14:57:57 -0700297 push_src("image", folder, src);
298}
299
msarett3d9d7a72015-10-21 10:27:10 -0700300static void push_android_codec_src(Path path, AndroidCodecSrc::Mode mode,
scroggoc5560be2016-02-03 09:42:42 -0800301 CodecSrc::DstColorType dstColorType, SkAlphaType dstAlphaType, int sampleSize) {
msarett3d9d7a72015-10-21 10:27:10 -0700302 SkString folder;
303 switch (mode) {
304 case AndroidCodecSrc::kFullImage_Mode:
305 folder.append("scaled_codec");
306 break;
307 case AndroidCodecSrc::kDivisor_Mode:
308 folder.append("scaled_codec_divisor");
309 break;
310 }
311
312 switch (dstColorType) {
313 case CodecSrc::kGrayscale_Always_DstColorType:
314 folder.append("_kGray8");
315 break;
316 case CodecSrc::kIndex8_Always_DstColorType:
317 folder.append("_kIndex8");
318 break;
319 default:
320 break;
321 }
322
scroggoc5560be2016-02-03 09:42:42 -0800323 switch (dstAlphaType) {
324 case kOpaque_SkAlphaType:
325 folder.append("_opaque");
326 break;
327 case kPremul_SkAlphaType:
328 folder.append("_premul");
329 break;
msarett9e9444c2016-02-03 12:39:10 -0800330 case kUnpremul_SkAlphaType:
331 folder.append("_unpremul");
332 break;
scroggoc5560be2016-02-03 09:42:42 -0800333 default:
334 break;
335 }
336
msarett3d9d7a72015-10-21 10:27:10 -0700337 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800338 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700339 }
340
scroggoc5560be2016-02-03 09:42:42 -0800341 AndroidCodecSrc* src = new AndroidCodecSrc(path, mode, dstColorType, dstAlphaType, sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700342 push_src("image", folder, src);
343}
344
msarett438b2ad2015-04-09 12:43:10 -0700345static void push_codec_srcs(Path path) {
346 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
347 if (!encoded) {
348 SkDebugf("Couldn't read %s.", path.c_str());
349 return;
350 }
351 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
halcanary96fcdcc2015-08-27 07:41:13 -0700352 if (nullptr == codec.get()) {
msarett438b2ad2015-04-09 12:43:10 -0700353 SkDebugf("Couldn't create codec for %s.", path.c_str());
354 return;
355 }
356
msarett36c37962015-09-02 13:20:52 -0700357 // Native Scales
msarett0a242972015-06-11 14:27:27 -0700358 // TODO (msarett): Implement scaling tests for SkImageDecoder in order to compare with these
359 // tests. SkImageDecoder supports downscales by integer factors.
emmaleer8f4ba762015-08-14 07:44:46 -0700360 // 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 -0700361 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 -0700362
msarett91c22b22016-02-22 12:27:46 -0800363 SkTArray<CodecSrc::Mode> nativeModes;
364 nativeModes.push_back(CodecSrc::kCodec_Mode);
365 nativeModes.push_back(CodecSrc::kCodecZeroInit_Mode);
366 nativeModes.push_back(CodecSrc::kGen_Mode);
367 switch (codec->getEncodedFormat()) {
368 case SkEncodedFormat::kJPEG_SkEncodedFormat:
369 nativeModes.push_back(CodecSrc::kScanline_Mode);
370 nativeModes.push_back(CodecSrc::kStripe_Mode);
371 nativeModes.push_back(CodecSrc::kCroppedScanline_Mode);
372 break;
373 case SkEncodedFormat::kWEBP_SkEncodedFormat:
374 nativeModes.push_back(CodecSrc::kSubset_Mode);
375 break;
msarettb65e6042016-02-23 05:37:25 -0800376 case SkEncodedFormat::kRAW_SkEncodedFormat:
377 break;
msarett91c22b22016-02-22 12:27:46 -0800378 default:
379 nativeModes.push_back(CodecSrc::kScanline_Mode);
380 nativeModes.push_back(CodecSrc::kStripe_Mode);
381 break;
382 }
msarett9e707a02015-09-01 14:57:57 -0700383
msarett91c22b22016-02-22 12:27:46 -0800384 SkTArray<CodecSrc::DstColorType> colorTypes;
385 colorTypes.push_back(CodecSrc::kGetFromCanvas_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700386 switch (codec->getInfo().colorType()) {
387 case kGray_8_SkColorType:
msarett91c22b22016-02-22 12:27:46 -0800388 colorTypes.push_back(CodecSrc::kGrayscale_Always_DstColorType);
msarett55f7bdd2016-02-16 13:24:54 -0800389 if (kWBMP_SkEncodedFormat == codec->getEncodedFormat()) {
msarett91c22b22016-02-22 12:27:46 -0800390 colorTypes.push_back(CodecSrc::kIndex8_Always_DstColorType);
msarett55f7bdd2016-02-16 13:24:54 -0800391 }
msarett36c37962015-09-02 13:20:52 -0700392 break;
393 case kIndex_8_SkColorType:
msarett91c22b22016-02-22 12:27:46 -0800394 colorTypes.push_back(CodecSrc::kIndex8_Always_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700395 break;
396 default:
msarett36c37962015-09-02 13:20:52 -0700397 break;
398 }
msarett9e707a02015-09-01 14:57:57 -0700399
scroggoc5560be2016-02-03 09:42:42 -0800400 SkTArray<SkAlphaType> alphaModes;
401 alphaModes.push_back(kPremul_SkAlphaType);
msarett9e9444c2016-02-03 12:39:10 -0800402 alphaModes.push_back(kUnpremul_SkAlphaType);
scroggoc5560be2016-02-03 09:42:42 -0800403 if (codec->getInfo().alphaType() == kOpaque_SkAlphaType) {
404 alphaModes.push_back(kOpaque_SkAlphaType);
405 }
msarettb714fb02016-01-22 14:46:42 -0800406
407 for (CodecSrc::Mode mode : nativeModes) {
408 // SkCodecImageGenerator only runs for the default colorType
409 // recommended by SkCodec. There is no need to generate multiple
410 // tests for different colorTypes.
411 // TODO (msarett): Add scaling support to SkCodecImageGenerator.
412 if (CodecSrc::kGen_Mode == mode) {
413 // FIXME: The gpu backend does not draw kGray sources correctly. (skbug.com/4822)
414 if (kGray_8_SkColorType != codec->getInfo().colorType()) {
scroggoc5560be2016-02-03 09:42:42 -0800415 push_codec_src(path, mode, CodecSrc::kGetFromCanvas_DstColorType,
416 codec->getInfo().alphaType(), 1.0f);
msarettb714fb02016-01-22 14:46:42 -0800417 }
418 continue;
419 }
420
421 for (float scale : nativeScales) {
msarett91c22b22016-02-22 12:27:46 -0800422 for (CodecSrc::DstColorType colorType : colorTypes) {
scroggoc5560be2016-02-03 09:42:42 -0800423 for (SkAlphaType alphaType : alphaModes) {
msarett91c22b22016-02-22 12:27:46 -0800424 // Only test kCroppedScanline_Mode when the alpha type is opaque. The test is
425 // slow and won't be interestingly different with different alpha types.
426 if (CodecSrc::kCroppedScanline_Mode == mode &&
427 kOpaque_SkAlphaType != alphaType) {
428 continue;
429 }
430
431 push_codec_src(path, mode, colorType, alphaType, scale);
scroggoc5560be2016-02-03 09:42:42 -0800432 }
msarett9e707a02015-09-01 14:57:57 -0700433 }
434 }
msarett0a242972015-06-11 14:27:27 -0700435 }
msarett36c37962015-09-02 13:20:52 -0700436
halcanary6950de62015-11-07 05:29:00 -0800437 // https://bug.skia.org/4428
msarett33c76232015-11-16 13:43:40 -0800438 bool subset = false;
439 // The following image types are supported by BitmapRegionDecoder,
440 // so we will test full image decodes and subset decodes.
msarettbe8216a2015-12-04 08:00:50 -0800441 static const char* const exts[] = {
msarett3d9d7a72015-10-21 10:27:10 -0700442 "jpg", "jpeg", "png", "webp",
443 "JPG", "JPEG", "PNG", "WEBP",
444 };
msarettbe8216a2015-12-04 08:00:50 -0800445 for (const char* ext : exts) {
msarett3d9d7a72015-10-21 10:27:10 -0700446 if (path.endsWith(ext)) {
msarett33c76232015-11-16 13:43:40 -0800447 subset = true;
msarett3d9d7a72015-10-21 10:27:10 -0700448 break;
449 }
450 }
msarett33c76232015-11-16 13:43:40 -0800451
msarett3d9d7a72015-10-21 10:27:10 -0700452 const int sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
msarett36c37962015-09-02 13:20:52 -0700453
msarett3d9d7a72015-10-21 10:27:10 -0700454 for (int sampleSize : sampleSizes) {
msarett91c22b22016-02-22 12:27:46 -0800455 for (CodecSrc::DstColorType colorType : colorTypes) {
scroggoc5560be2016-02-03 09:42:42 -0800456 for (SkAlphaType alphaType : alphaModes) {
msarett91c22b22016-02-22 12:27:46 -0800457 push_android_codec_src(path, AndroidCodecSrc::kFullImage_Mode, colorType,
scroggoc5560be2016-02-03 09:42:42 -0800458 alphaType, sampleSize);
459 if (subset) {
msarett91c22b22016-02-22 12:27:46 -0800460 push_android_codec_src(path, AndroidCodecSrc::kDivisor_Mode, colorType,
scroggoc5560be2016-02-03 09:42:42 -0800461 alphaType, sampleSize);
462 }
msarett3d9d7a72015-10-21 10:27:10 -0700463 }
msarett36c37962015-09-02 13:20:52 -0700464 }
465 }
msarett438b2ad2015-04-09 12:43:10 -0700466}
467
msarett5cb48852015-11-06 08:56:32 -0800468static bool brd_color_type_supported(SkBitmapRegionDecoder::Strategy strategy,
msaretta5783ae2015-09-08 15:35:32 -0700469 CodecSrc::DstColorType dstColorType) {
470 switch (strategy) {
msarett5cb48852015-11-06 08:56:32 -0800471 case SkBitmapRegionDecoder::kCanvas_Strategy:
msaretta5783ae2015-09-08 15:35:32 -0700472 if (CodecSrc::kGetFromCanvas_DstColorType == dstColorType) {
473 return true;
474 }
475 return false;
msarett5cb48852015-11-06 08:56:32 -0800476 case SkBitmapRegionDecoder::kAndroidCodec_Strategy:
msarett26ad17b2015-10-22 07:29:19 -0700477 switch (dstColorType) {
478 case CodecSrc::kGetFromCanvas_DstColorType:
479 case CodecSrc::kIndex8_Always_DstColorType:
480 case CodecSrc::kGrayscale_Always_DstColorType:
481 return true;
482 default:
483 return false;
484 }
msaretta5783ae2015-09-08 15:35:32 -0700485 default:
486 SkASSERT(false);
487 return false;
488 }
489}
490
msarett5cb48852015-11-06 08:56:32 -0800491static void push_brd_src(Path path, SkBitmapRegionDecoder::Strategy strategy,
msaretta5783ae2015-09-08 15:35:32 -0700492 CodecSrc::DstColorType dstColorType, BRDSrc::Mode mode, uint32_t sampleSize) {
493 SkString folder;
494 switch (strategy) {
msarett5cb48852015-11-06 08:56:32 -0800495 case SkBitmapRegionDecoder::kCanvas_Strategy:
msaretta5783ae2015-09-08 15:35:32 -0700496 folder.append("brd_canvas");
497 break;
msarett5cb48852015-11-06 08:56:32 -0800498 case SkBitmapRegionDecoder::kAndroidCodec_Strategy:
msarett26ad17b2015-10-22 07:29:19 -0700499 folder.append("brd_android_codec");
500 break;
msaretta5783ae2015-09-08 15:35:32 -0700501 default:
502 SkASSERT(false);
503 return;
504 }
505
506 switch (mode) {
507 case BRDSrc::kFullImage_Mode:
508 break;
509 case BRDSrc::kDivisor_Mode:
510 folder.append("_divisor");
511 break;
512 default:
513 SkASSERT(false);
514 return;
515 }
516
517 switch (dstColorType) {
518 case CodecSrc::kGetFromCanvas_DstColorType:
519 break;
520 case CodecSrc::kIndex8_Always_DstColorType:
521 folder.append("_kIndex");
522 break;
523 case CodecSrc::kGrayscale_Always_DstColorType:
524 folder.append("_kGray");
525 break;
526 default:
527 SkASSERT(false);
528 return;
529 }
530
531 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800532 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700533 }
534
535 BRDSrc* src = new BRDSrc(path, strategy, mode, dstColorType, sampleSize);
536 push_src("image", folder, src);
537}
538
539static void push_brd_srcs(Path path) {
540
msarett5cb48852015-11-06 08:56:32 -0800541 const SkBitmapRegionDecoder::Strategy strategies[] = {
542 SkBitmapRegionDecoder::kCanvas_Strategy,
msarett5cb48852015-11-06 08:56:32 -0800543 SkBitmapRegionDecoder::kAndroidCodec_Strategy,
msaretta5783ae2015-09-08 15:35:32 -0700544 };
545
scroggo501b7342015-11-03 07:55:11 -0800546 // Test on a variety of sampleSizes, making sure to include:
547 // - 2, 4, and 8, which are natively supported by jpeg
548 // - multiples of 2 which are not divisible by 4 (analogous for 4)
549 // - larger powers of two, since BRD clients generally use powers of 2
550 // We will only produce output for the larger sizes on large images.
551 const uint32_t sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 24, 32, 64 };
msarett6efbe052015-09-11 09:01:16 -0700552
msaretta5783ae2015-09-08 15:35:32 -0700553 // We will only test to one backend (8888), but we will test all of the
554 // color types that we need to decode to on this backend.
555 const CodecSrc::DstColorType dstColorTypes[] = {
msarett26ad17b2015-10-22 07:29:19 -0700556 CodecSrc::kGetFromCanvas_DstColorType,
557 CodecSrc::kIndex8_Always_DstColorType,
558 CodecSrc::kGrayscale_Always_DstColorType,
msaretta5783ae2015-09-08 15:35:32 -0700559 };
560
561 const BRDSrc::Mode modes[] = {
msarett26ad17b2015-10-22 07:29:19 -0700562 BRDSrc::kFullImage_Mode,
563 BRDSrc::kDivisor_Mode,
msaretta5783ae2015-09-08 15:35:32 -0700564 };
565
msarett5cb48852015-11-06 08:56:32 -0800566 for (SkBitmapRegionDecoder::Strategy strategy : strategies) {
msarett6efbe052015-09-11 09:01:16 -0700567 for (uint32_t sampleSize : sampleSizes) {
msarett6efbe052015-09-11 09:01:16 -0700568 for (CodecSrc::DstColorType dstColorType : dstColorTypes) {
569 if (brd_color_type_supported(strategy, dstColorType)) {
570 for (BRDSrc::Mode mode : modes) {
msaretta5783ae2015-09-08 15:35:32 -0700571 push_brd_src(path, strategy, dstColorType, mode, sampleSize);
572 }
573 }
574 }
575 }
576 }
577}
578
579static bool brd_supported(const char* ext) {
msarett8c8f22a2015-04-01 06:58:48 -0700580 static const char* const exts[] = {
msaretta5783ae2015-09-08 15:35:32 -0700581 "jpg", "jpeg", "png", "webp",
582 "JPG", "JPEG", "PNG", "WEBP",
msarett8c8f22a2015-04-01 06:58:48 -0700583 };
584
585 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
586 if (0 == strcmp(exts[i], ext)) {
587 return true;
588 }
589 }
590 return false;
scroggo9b77ddd2015-03-19 06:03:39 -0700591}
592
scroggo86737142016-02-03 12:19:11 -0800593static bool gather_srcs() {
mtklein748ca3b2015-01-15 10:56:12 -0800594 for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) {
djsollen54416de2015-04-03 07:24:48 -0700595 push_src("gm", "", new GMSrc(r->factory()));
mtklein748ca3b2015-01-15 10:56:12 -0800596 }
halcanaryfc37ad12015-01-30 07:31:19 -0800597 for (int i = 0; i < FLAGS_skps.count(); i++) {
598 const char* path = FLAGS_skps[i];
599 if (sk_isdir(path)) {
600 SkOSFile::Iter it(path, "skp");
601 for (SkString file; it.next(&file); ) {
djsollen54416de2015-04-03 07:24:48 -0700602 push_src("skp", "", new SKPSrc(SkOSPath::Join(path, file.c_str())));
halcanaryfc37ad12015-01-30 07:31:19 -0800603 }
604 } else {
djsollen54416de2015-04-03 07:24:48 -0700605 push_src("skp", "", new SKPSrc(path));
mtklein114c3cd2015-01-15 10:15:02 -0800606 }
607 }
scroggo86737142016-02-03 12:19:11 -0800608
609 SkTArray<SkString> images;
610 if (!CollectImages(&images)) {
611 return false;
612 }
613
614 for (auto image : images) {
615 push_codec_srcs(image);
mtklein21eaf3b2016-02-08 12:39:59 -0800616 const char* ext = strrchr(image.c_str(), '.');
617 if (ext && brd_supported(ext+1)) {
scroggo86737142016-02-03 12:19:11 -0800618 push_brd_srcs(image);
mtklein709d2c32015-01-15 08:30:25 -0800619 }
mtklein709d2c32015-01-15 08:30:25 -0800620 }
scroggo86737142016-02-03 12:19:11 -0800621
622 return true;
mtklein709d2c32015-01-15 08:30:25 -0800623}
624
kkinnunen3e980c32015-12-23 01:33:00 -0800625static void push_sink(const SkCommandLineConfig& config, Sink* s) {
rmistry0f515bd2015-12-22 10:22:26 -0800626 SkAutoTDelete<Sink> sink(s);
kkinnunen3e980c32015-12-23 01:33:00 -0800627
mtkleine0effd62015-07-29 06:37:28 -0700628 // Try a simple Src as a canary. If it fails, skip this sink.
mtklein748ca3b2015-01-15 10:56:12 -0800629 struct : public Src {
mtkleine0effd62015-07-29 06:37:28 -0700630 Error draw(SkCanvas* c) const override {
631 c->drawRect(SkRect::MakeWH(1,1), SkPaint());
632 return "";
633 }
mtklein36352bf2015-03-25 18:17:31 -0700634 SkISize size() const override { return SkISize::Make(16, 16); }
mtkleine0effd62015-07-29 06:37:28 -0700635 Name name() const override { return "justOneRect"; }
636 } justOneRect;
mtklein114c3cd2015-01-15 10:15:02 -0800637
mtklein748ca3b2015-01-15 10:56:12 -0800638 SkBitmap bitmap;
639 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800640 SkString log;
mtkleine0effd62015-07-29 06:37:28 -0700641 Error err = sink->draw(justOneRect, &bitmap, &stream, &log);
mtklein4089ef72015-03-05 08:40:28 -0800642 if (err.isFatal()) {
kkinnunen3e980c32015-12-23 01:33:00 -0800643 SkDebugf("Could not run %s: %s\n", config.getTag().c_str(), err.c_str());
mtklein05641a52015-04-21 10:49:13 -0700644 exit(1);
mtklein114c3cd2015-01-15 10:15:02 -0800645 }
646
mtkleine0effd62015-07-29 06:37:28 -0700647 TaggedSink& ts = gSinks.push_back();
mtklein748ca3b2015-01-15 10:56:12 -0800648 ts.reset(sink.detach());
kkinnunen3e980c32015-12-23 01:33:00 -0800649 ts.tag = config.getTag();
mtklein748ca3b2015-01-15 10:56:12 -0800650}
651
652static bool gpu_supported() {
653#if SK_SUPPORT_GPU
654 return FLAGS_gpu;
655#else
656 return false;
657#endif
658}
kkinnunen9ebc3f02015-12-21 23:48:13 -0800659
kkinnunen3e980c32015-12-23 01:33:00 -0800660static Sink* create_sink(const SkCommandLineConfig* config) {
661#if SK_SUPPORT_GPU
662 if (gpu_supported()) {
663 if (const SkCommandLineConfigGpu* gpuConfig = config->asConfigGpu()) {
664 GrContextFactory::GLContextType contextType = gpuConfig->getContextType();
665 GrContextFactory::GLContextOptions contextOptions =
666 GrContextFactory::kNone_GLContextOptions;
667 if (gpuConfig->getUseNVPR()) {
668 contextOptions = static_cast<GrContextFactory::GLContextOptions>(
669 contextOptions | GrContextFactory::kEnableNVPR_GLContextOptions);
670 }
671 GrContextFactory testFactory;
672 if (!testFactory.get(contextType, contextOptions)) {
673 SkDebugf("WARNING: can not create GPU context for config '%s'. "
674 "GM tests will be skipped.\n", gpuConfig->getTag().c_str());
675 return nullptr;
676 }
677 return new GPUSink(contextType, contextOptions, gpuConfig->getSamples(),
678 gpuConfig->getUseDIText(), FLAGS_gpu_threading);
679 }
680 }
681#endif
682
683#define SINK(t, sink, ...) if (config->getBackend().equals(t)) { return new sink(__VA_ARGS__); }
684
tomhudsoneebc39a2015-02-23 12:18:05 -0800685#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
686 SINK("hwui", HWUISink);
687#endif
688
mtklein748ca3b2015-01-15 10:56:12 -0800689 if (FLAGS_cpu) {
690 SINK("565", RasterSink, kRGB_565_SkColorType);
691 SINK("8888", RasterSink, kN32_SkColorType);
halcanaryc11c62f2015-09-28 11:51:54 -0700692 SINK("pdf", PDFSink, "Pdfium");
693 SINK("pdf_poppler", PDFSink, "Poppler");
mtklein9c3f17d2015-01-28 11:35:18 -0800694 SINK("skp", SKPSink);
mtklein8a4527e2015-01-31 20:00:58 -0800695 SINK("svg", SVGSink);
696 SINK("null", NullSink);
halcanary47ef4d52015-03-03 09:13:09 -0800697 SINK("xps", XPSSink);
mtklein748ca3b2015-01-15 10:56:12 -0800698 }
699#undef SINK
halcanary96fcdcc2015-08-27 07:41:13 -0700700 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800701}
702
kkinnunen3e980c32015-12-23 01:33:00 -0800703static Sink* create_via(const SkString& tag, Sink* wrapped) {
704#define VIA(t, via, ...) if (tag.equals(t)) { return new via(__VA_ARGS__); }
halcanary96fcdcc2015-08-27 07:41:13 -0700705 VIA("twice", ViaTwice, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700706 VIA("serialize", ViaSerialization, wrapped);
mtklein4a34ecb2016-01-08 10:19:35 -0800707 VIA("pic", ViaPicture, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700708 VIA("2ndpic", ViaSecondPicture, wrapped);
mtkleind31c13d2015-05-05 12:59:56 -0700709 VIA("sp", ViaSingletonPictures, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700710 VIA("tiles", ViaTiles, 256, 256, nullptr, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800711 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
mtklein2e2ea382015-10-16 10:29:41 -0700712 VIA("remote", ViaRemote, false, wrapped);
713 VIA("remote_cache", ViaRemote, true, wrapped);
halcanary7a76f9c2016-02-03 11:53:18 -0800714 VIA("mojo", ViaMojo, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800715
mtkleind603b222015-02-17 11:13:33 -0800716 if (FLAGS_matrix.count() == 4) {
mtklein748ca3b2015-01-15 10:56:12 -0800717 SkMatrix m;
mtkleind603b222015-02-17 11:13:33 -0800718 m.reset();
719 m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
720 m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
721 m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
722 m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
723 VIA("matrix", ViaMatrix, m, wrapped);
724 VIA("upright", ViaUpright, m, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800725 }
tomhudson64de1e12015-03-05 08:01:07 -0800726
727#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
728 VIA("androidsdk", ViaAndroidSDK, wrapped);
729#endif
730
mtklein748ca3b2015-01-15 10:56:12 -0800731#undef VIA
halcanary96fcdcc2015-08-27 07:41:13 -0700732 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800733}
734
mtklein748ca3b2015-01-15 10:56:12 -0800735static void gather_sinks() {
kkinnunen3e980c32015-12-23 01:33:00 -0800736 SkCommandLineConfigArray configs;
737 ParseConfigs(FLAGS_config, &configs);
738 for (int i = 0; i < configs.count(); i++) {
739 const SkCommandLineConfig& config = *configs[i];
740 Sink* sink = create_sink(&config);
741 if (sink == nullptr) {
742 SkDebugf("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
743 config.getTag().c_str());
744 continue;
745 }
mtklein748ca3b2015-01-15 10:56:12 -0800746
kkinnunen3e980c32015-12-23 01:33:00 -0800747 const SkTArray<SkString>& parts = config.getViaParts();
748 for (int j = parts.count(); j-- > 0;) {
749 const SkString& part = parts[j];
750 Sink* next = create_via(part, sink);
halcanary96fcdcc2015-08-27 07:41:13 -0700751 if (next == nullptr) {
kkinnunen3e980c32015-12-23 01:33:00 -0800752 SkDebugf("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
753 part.c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800754 delete sink;
halcanary96fcdcc2015-08-27 07:41:13 -0700755 sink = nullptr;
mtklein748ca3b2015-01-15 10:56:12 -0800756 break;
757 }
758 sink = next;
759 }
760 if (sink) {
761 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800762 }
763 }
764}
mtklein709d2c32015-01-15 08:30:25 -0800765
mtkleina5114d72015-08-24 13:27:01 -0700766static bool dump_png(SkBitmap bitmap, const char* path, const char* md5) {
767 const int w = bitmap.width(),
768 h = bitmap.height();
769
770 // First get the bitmap into N32 color format. The next step will work only there.
771 if (bitmap.colorType() != kN32_SkColorType) {
772 SkBitmap n32;
773 if (!bitmap.copyTo(&n32, kN32_SkColorType)) {
774 return false;
775 }
776 bitmap = n32;
777 }
778
779 // Convert our N32 bitmap into unpremul RGBA for libpng.
780 SkAutoTMalloc<uint32_t> rgba(w*h);
781 if (!bitmap.readPixels(SkImageInfo::Make(w,h, kRGBA_8888_SkColorType, kUnpremul_SkAlphaType),
782 rgba, 4*w, 0,0)) {
783 return false;
784 }
785
786 // We don't need bitmap anymore. Might as well drop our ref.
mtkleinfccb77d2015-08-24 14:13:29 -0700787 bitmap.reset();
mtkleina5114d72015-08-24 13:27:01 -0700788
mtkleinc64137c2015-08-25 10:56:08 -0700789 FILE* f = fopen(path, "wb");
mtkleina5114d72015-08-24 13:27:01 -0700790 if (!f) { return false; }
791
792 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
793 if (!png) {
794 fclose(f);
795 return false;
796 }
797
798 png_infop info = png_create_info_struct(png);
799 if (!info) {
800 png_destroy_write_struct(&png, &info);
801 fclose(f);
802 return false;
803 }
804
mtkleind69ece62015-09-10 10:37:44 -0700805 SkString description;
806 description.append("Key: ");
807 for (int i = 0; i < FLAGS_key.count(); i++) {
808 description.appendf("%s ", FLAGS_key[i]);
809 }
810 description.append("Properties: ");
811 for (int i = 0; i < FLAGS_properties.count(); i++) {
812 description.appendf("%s ", FLAGS_properties[i]);
813 }
814 description.appendf("MD5: %s", md5);
815
mtkleina5114d72015-08-24 13:27:01 -0700816 png_text text[2];
817 text[0].key = (png_charp)"Author";
818 text[0].text = (png_charp)"DM dump_png()";
819 text[0].compression = PNG_TEXT_COMPRESSION_NONE;
820 text[1].key = (png_charp)"Description";
mtkleind69ece62015-09-10 10:37:44 -0700821 text[1].text = (png_charp)description.c_str();
mtkleina5114d72015-08-24 13:27:01 -0700822 text[1].compression = PNG_TEXT_COMPRESSION_NONE;
823 png_set_text(png, info, text, 2);
824
825 png_init_io(png, f);
826 png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8,
827 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
828 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
829 png_write_info(png, info);
830 for (int j = 0; j < h; j++) {
831 png_bytep row = (png_bytep)(rgba.get() + w*j);
832 png_write_rows(png, &row, 1);
833 }
834 png_write_end(png, info);
835
836 png_destroy_write_struct(&png, &info);
837 fclose(f);
838 return true;
839}
840
mtkleina2ef6422015-01-15 13:44:22 -0800841static bool match(const char* needle, const char* haystack) {
halcanary96fcdcc2015-08-27 07:41:13 -0700842 return 0 == strcmp("_", needle) || nullptr != strstr(haystack, needle);
mtkleina2ef6422015-01-15 13:44:22 -0800843}
844
mtklein3eed7dd2016-02-24 19:07:07 -0800845static bool is_blacklisted(const char* sink, const char* src,
846 const char* srcOptions, const char* name) {
mtklein0d243ff2015-04-03 07:51:00 -0700847 for (int i = 0; i < FLAGS_blacklist.count() - 3; i += 4) {
mtkleina2ef6422015-01-15 13:44:22 -0800848 if (match(FLAGS_blacklist[i+0], sink) &&
djsollen54416de2015-04-03 07:24:48 -0700849 match(FLAGS_blacklist[i+1], src) &&
850 match(FLAGS_blacklist[i+2], srcOptions) &&
851 match(FLAGS_blacklist[i+3], name)) {
mtklein3eed7dd2016-02-24 19:07:07 -0800852 return true;
mtkleina2ef6422015-01-15 13:44:22 -0800853 }
854 }
mtklein3eed7dd2016-02-24 19:07:07 -0800855 return false;
mtkleina2ef6422015-01-15 13:44:22 -0800856}
857
mtkleincd50bca2016-01-05 06:20:20 -0800858// Even when a Task Sink reports to be non-threadsafe (e.g. GPU), we know things like
859// .png encoding are definitely thread safe. This lets us offload that work to CPU threads.
860static SkTaskGroup gDefinitelyThreadSafeWork;
861
mtklein748ca3b2015-01-15 10:56:12 -0800862// The finest-grained unit of work we can run: draw a single Src into a single Sink,
863// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
864struct Task {
mtkleine0effd62015-07-29 06:37:28 -0700865 Task(const TaggedSrc& src, const TaggedSink& sink) : src(src), sink(sink) {}
866 const TaggedSrc& src;
867 const TaggedSink& sink;
mtklein748ca3b2015-01-15 10:56:12 -0800868
mtklein21eaf3b2016-02-08 12:39:59 -0800869 static void Run(const Task& task) {
870 SkString name = task.src->name();
mtkleine0effd62015-07-29 06:37:28 -0700871
mtkleinb9eb4ac2015-02-02 18:26:03 -0800872 SkString log;
mtklein3eed7dd2016-02-24 19:07:07 -0800873 if (!FLAGS_dryRun) {
mtklein748ca3b2015-01-15 10:56:12 -0800874 SkBitmap bitmap;
875 SkDynamicMemoryWStream stream;
mtklein3eed7dd2016-02-24 19:07:07 -0800876 start(task.sink.tag.c_str(), task.src.tag.c_str(),
877 task.src.options.c_str(), name.c_str());
mtklein21eaf3b2016-02-08 12:39:59 -0800878 Error err = task.sink->draw(*task.src, &bitmap, &stream, &log);
mtklein748ca3b2015-01-15 10:56:12 -0800879 if (!err.isEmpty()) {
mtklein4089ef72015-03-05 08:40:28 -0800880 if (err.isFatal()) {
djsollen54416de2015-04-03 07:24:48 -0700881 fail(SkStringPrintf("%s %s %s %s: %s",
mtklein21eaf3b2016-02-08 12:39:59 -0800882 task.sink.tag.c_str(),
883 task.src.tag.c_str(),
884 task.src.options.c_str(),
mtklein4089ef72015-03-05 08:40:28 -0800885 name.c_str(),
886 err.c_str()));
mtkleinb37cb412015-03-18 05:27:14 -0700887 } else {
mtklein3eed7dd2016-02-24 19:07:07 -0800888 done(task.sink.tag.c_str(), task.src.tag.c_str(),
889 task.src.options.c_str(), name.c_str());
mtkleinba6ada72016-01-21 09:39:35 -0800890 return;
mtklein4089ef72015-03-05 08:40:28 -0800891 }
mtklein748ca3b2015-01-15 10:56:12 -0800892 }
mtklein62bd1a62015-01-27 14:46:26 -0800893
mtkleincd50bca2016-01-05 06:20:20 -0800894 // We're likely switching threads here, so we must capture by value, [=] or [foo,bar].
895 SkStreamAsset* data = stream.detachAsStream();
896 gDefinitelyThreadSafeWork.add([task,name,bitmap,data]{
897 SkAutoTDelete<SkStreamAsset> ownedData(data);
898
899 // Why doesn't the copy constructor do this when we have pre-locked pixels?
900 bitmap.lockPixels();
901
902 SkString md5;
903 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
904 SkMD5 hash;
905 if (data->getLength()) {
906 hash.writeStream(data, data->getLength());
907 data->rewind();
mtklein38408462015-07-08 07:25:27 -0700908 } else {
mtkleincd50bca2016-01-05 06:20:20 -0800909 // If we're BGRA (Linux, Windows), swizzle over to RGBA (Mac, Android).
910 // This helps eliminate multiple 0-pixel-diff hashes on gold.skia.org.
911 // (Android's general slow speed breaks the tie arbitrarily in RGBA's favor.)
912 // We might consider promoting 565 to RGBA too.
913 if (bitmap.colorType() == kBGRA_8888_SkColorType) {
914 SkBitmap swizzle;
915 SkAssertResult(bitmap.copyTo(&swizzle, kRGBA_8888_SkColorType));
916 hash.write(swizzle.getPixels(), swizzle.getSize());
917 } else {
918 hash.write(bitmap.getPixels(), bitmap.getSize());
919 }
920 }
921 SkMD5::Digest digest;
922 hash.finish(digest);
923 for (int i = 0; i < 16; i++) {
924 md5.appendf("%02x", digest.data[i]);
mtklein38408462015-07-08 07:25:27 -0700925 }
mtklein62bd1a62015-01-27 14:46:26 -0800926 }
mtklein62bd1a62015-01-27 14:46:26 -0800927
mtkleincd50bca2016-01-05 06:20:20 -0800928 if (!FLAGS_readPath.isEmpty() &&
mtklein3eed7dd2016-02-24 19:07:07 -0800929 !gGold.contains(Gold(task.sink.tag, task.src.tag,
930 task.src.options, name, md5))) {
mtkleincd50bca2016-01-05 06:20:20 -0800931 fail(SkStringPrintf("%s not found for %s %s %s %s in %s",
932 md5.c_str(),
mtklein21eaf3b2016-02-08 12:39:59 -0800933 task.sink.tag.c_str(),
934 task.src.tag.c_str(),
935 task.src.options.c_str(),
mtkleincd50bca2016-01-05 06:20:20 -0800936 name.c_str(),
937 FLAGS_readPath[0]));
mtklein748ca3b2015-01-15 10:56:12 -0800938 }
mtkleincd50bca2016-01-05 06:20:20 -0800939
940 if (!FLAGS_writePath.isEmpty()) {
mtklein21eaf3b2016-02-08 12:39:59 -0800941 const char* ext = task.sink->fileExtension();
mtkleincd50bca2016-01-05 06:20:20 -0800942 if (data->getLength()) {
mtklein21eaf3b2016-02-08 12:39:59 -0800943 WriteToDisk(task, md5, ext, data, data->getLength(), nullptr);
mtkleincd50bca2016-01-05 06:20:20 -0800944 SkASSERT(bitmap.drawsNothing());
945 } else if (!bitmap.drawsNothing()) {
mtklein21eaf3b2016-02-08 12:39:59 -0800946 WriteToDisk(task, md5, ext, nullptr, 0, &bitmap);
mtkleincd50bca2016-01-05 06:20:20 -0800947 }
948 }
949 });
mtklein748ca3b2015-01-15 10:56:12 -0800950 }
mtklein3eed7dd2016-02-24 19:07:07 -0800951 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 -0800952 }
953
954 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -0800955 SkString md5,
956 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -0800957 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -0800958 const SkBitmap* bitmap) {
mtklein748ca3b2015-01-15 10:56:12 -0800959 JsonWriter::BitmapResult result;
djsollen54416de2015-04-03 07:24:48 -0700960 result.name = task.src->name();
mtklein3eed7dd2016-02-24 19:07:07 -0800961 result.config = task.sink.tag;
djsollen54416de2015-04-03 07:24:48 -0700962 result.sourceType = task.src.tag;
963 result.sourceOptions = task.src.options;
964 result.ext = ext;
965 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -0800966 JsonWriter::AddBitmapResult(result);
967
mtkleinb0531a72015-04-07 13:38:48 -0700968 // If an MD5 is uninteresting, we want it noted in the JSON file,
969 // but don't want to dump it out as a .png (or whatever ext is).
970 if (gUninterestingHashes.contains(md5)) {
971 return;
972 }
973
mtklein748ca3b2015-01-15 10:56:12 -0800974 const char* dir = FLAGS_writePath[0];
975 if (0 == strcmp(dir, "@")) { // Needed for iOS.
976 dir = FLAGS_resourcePath[0];
977 }
978 sk_mkdir(dir);
979
980 SkString path;
981 if (FLAGS_nameByHash) {
982 path = SkOSPath::Join(dir, result.md5.c_str());
983 path.append(".");
984 path.append(ext);
985 if (sk_exists(path.c_str())) {
986 return; // Content-addressed. If it exists already, we're done.
987 }
988 } else {
kkinnunen3e980c32015-12-23 01:33:00 -0800989 path = SkOSPath::Join(dir, task.sink.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800990 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -0700991 path = SkOSPath::Join(path.c_str(), task.src.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800992 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -0700993 if (strcmp(task.src.options.c_str(), "") != 0) {
994 path = SkOSPath::Join(path.c_str(), task.src.options.c_str());
djsollen54416de2015-04-03 07:24:48 -0700995 sk_mkdir(path.c_str());
996 }
mtklein748ca3b2015-01-15 10:56:12 -0800997 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
998 path.append(".");
999 path.append(ext);
1000 }
1001
mtklein748ca3b2015-01-15 10:56:12 -08001002 if (bitmap) {
mtkleina5114d72015-08-24 13:27:01 -07001003 if (!dump_png(*bitmap, path.c_str(), result.md5.c_str())) {
mtklein748ca3b2015-01-15 10:56:12 -08001004 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
1005 return;
1006 }
1007 } else {
mtkleina5114d72015-08-24 13:27:01 -07001008 SkFILEWStream file(path.c_str());
1009 if (!file.isValid()) {
1010 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
1011 return;
1012 }
mtklein748ca3b2015-01-15 10:56:12 -08001013 if (!file.writeStream(data, len)) {
1014 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
1015 return;
1016 }
1017 }
1018 }
1019};
1020
mtklein748ca3b2015-01-15 10:56:12 -08001021/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1022
1023// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
1024
mtklein21eaf3b2016-02-08 12:39:59 -08001025static SkTDArray<skiatest::Test> gParallelTests, gSerialTests;
mtklein748ca3b2015-01-15 10:56:12 -08001026
1027static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -08001028 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -08001029 return;
1030 }
mtklein6393c062015-04-27 08:45:01 -07001031 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r; r = r->next()) {
1032 if (!in_shard()) {
1033 continue;
1034 }
halcanary87f3ba42015-01-20 09:30:20 -08001035 // Despite its name, factory() is returning a reference to
1036 // link-time static const POD data.
1037 const skiatest::Test& test = r->factory();
1038 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -08001039 continue;
1040 }
halcanary87f3ba42015-01-20 09:30:20 -08001041 if (test.needsGpu && gpu_supported()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001042 (FLAGS_gpu_threading ? gParallelTests : gSerialTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -08001043 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein21eaf3b2016-02-08 12:39:59 -08001044 gParallelTests.push(test);
mtklein82d28432015-01-15 12:46:02 -08001045 }
mtklein748ca3b2015-01-15 10:56:12 -08001046 }
1047}
1048
mtklein21eaf3b2016-02-08 12:39:59 -08001049static void run_test(skiatest::Test test) {
halcanary87f3ba42015-01-20 09:30:20 -08001050 struct : public skiatest::Reporter {
mtklein36352bf2015-03-25 18:17:31 -07001051 void reportFailed(const skiatest::Failure& failure) override {
halcanary87f3ba42015-01-20 09:30:20 -08001052 fail(failure.toString());
1053 JsonWriter::AddTestFailure(failure);
1054 }
mtklein36352bf2015-03-25 18:17:31 -07001055 bool allowExtendedTest() const override {
halcanary87f3ba42015-01-20 09:30:20 -08001056 return FLAGS_pathOpsExtended;
1057 }
mtklein36352bf2015-03-25 18:17:31 -07001058 bool verbose() const override { return FLAGS_veryVerbose; }
halcanary87f3ba42015-01-20 09:30:20 -08001059 } reporter;
djsollen824996a2015-06-12 12:06:22 -07001060
mtklein3eed7dd2016-02-24 19:07:07 -08001061 if (!FLAGS_dryRun && !is_blacklisted("_", "tests", "_", test.name)) {
mtklein21eaf3b2016-02-08 12:39:59 -08001062 start("unit", "test", "", test.name);
mtklein55e88b22015-01-21 15:50:13 -08001063 GrContextFactory factory;
mtklein21eaf3b2016-02-08 12:39:59 -08001064 test.proc(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -08001065 }
mtklein3eed7dd2016-02-24 19:07:07 -08001066 done("unit", "test", "", test.name);
mtklein748ca3b2015-01-15 10:56:12 -08001067}
1068
1069/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1070
mtkleinafae30a2016-02-24 12:28:32 -08001071DEFINE_int32(status_sec, 15, "Print status this often (and if we crash).");
1072
1073SkThread* start_status_thread() {
1074 auto thread = new SkThread([] (void*) {
1075 for (;;) {
1076 print_status();
1077 #if defined(SK_BUILD_FOR_WIN)
1078 Sleep(FLAGS_status_sec * 1000);
1079 #else
1080 sleep(FLAGS_status_sec);
1081 #endif
mtklein2e1c47e2015-03-12 07:16:56 -07001082 }
mtkleinafae30a2016-02-24 12:28:32 -08001083 });
1084 thread->start();
1085 return thread;
mtkleinde6fc2b2015-03-12 06:28:54 -07001086}
1087
caryclark83ca6282015-06-10 09:31:09 -07001088#define PORTABLE_FONT_PREFIX "Toy Liberation "
1089
1090static SkTypeface* create_from_name(const char familyName[], SkTypeface::Style style) {
1091 if (familyName && strlen(familyName) > sizeof(PORTABLE_FONT_PREFIX)
1092 && !strncmp(familyName, PORTABLE_FONT_PREFIX, sizeof(PORTABLE_FONT_PREFIX) - 1)) {
caryclark1818acb2015-07-24 12:09:25 -07001093 return sk_tool_utils::create_portable_typeface(familyName, style);
caryclark83ca6282015-06-10 09:31:09 -07001094 }
halcanary96fcdcc2015-08-27 07:41:13 -07001095 return nullptr;
caryclark83ca6282015-06-10 09:31:09 -07001096}
1097
1098#undef PORTABLE_FONT_PREFIX
1099
1100extern SkTypeface* (*gCreateTypefaceDelegate)(const char [], SkTypeface::Style );
1101
jcgregorio3b27ade2014-11-13 08:06:40 -08001102int dm_main();
caryclark17f0b6d2014-07-22 10:15:34 -07001103int dm_main() {
mtklein246ba3a2016-02-23 10:39:36 -08001104 setup_crash_handler();
mtkleinafae30a2016-02-24 12:28:32 -08001105
mtklein5286f022016-01-22 08:18:14 -08001106 JsonWriter::DumpJson(); // It's handy for the bots to assume this is ~never missing.
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +00001107 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -07001108 SkTaskGroup::Enabler enabled(FLAGS_threads);
caryclark83ca6282015-06-10 09:31:09 -07001109 gCreateTypefaceDelegate = &create_from_name;
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +00001110
halcanary7d571242016-02-24 17:59:16 -08001111 {
1112 SkString testResourcePath = GetResourcePath("color_wheel.png");
1113 SkFILEStream testResource(testResourcePath.c_str());
1114 if (!testResource.isValid()) {
1115 SkDebugf("Some resources are missing. Do you need to set --resourcePath?\n");
1116 }
1117 }
mtklein62bd1a62015-01-27 14:46:26 -08001118 gather_gold();
borenet09ed4802015-04-03 14:15:33 -07001119 gather_uninteresting_hashes();
mtklein62bd1a62015-01-27 14:46:26 -08001120
scroggo86737142016-02-03 12:19:11 -08001121 if (!gather_srcs()) {
1122 return 1;
1123 }
mtklein748ca3b2015-01-15 10:56:12 -08001124 gather_sinks();
1125 gather_tests();
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +00001126
mtklein21eaf3b2016-02-08 12:39:59 -08001127 gPending = gSrcs.count() * gSinks.count() + gParallelTests.count() + gSerialTests.count();
mtkleinafae30a2016-02-24 12:28:32 -08001128 SkDebugf("%d srcs * %d sinks + %d tests == %d tasks",
mtklein21eaf3b2016-02-08 12:39:59 -08001129 gSrcs.count(), gSinks.count(), gParallelTests.count() + gSerialTests.count(), gPending);
mtkleinafae30a2016-02-24 12:28:32 -08001130 SkAutoTDelete<SkThread> statusThread(start_status_thread());
mtklein748ca3b2015-01-15 10:56:12 -08001131
mtklein21eaf3b2016-02-08 12:39:59 -08001132 // Kick off as much parallel work as we can, making note of any serial work we'll need to do.
1133 SkTaskGroup parallel;
1134 SkTArray<Task> serial;
1135
1136 for (auto& sink : gSinks)
1137 for (auto& src : gSrcs) {
mtklein3eed7dd2016-02-24 19:07:07 -08001138 if (src->veto(sink->flags()) ||
1139 is_blacklisted(sink.tag.c_str(), src.tag.c_str(),
1140 src.options.c_str(), src->name().c_str())) {
1141 SkAutoTAcquire<SkPODSpinlock> lock(gMutex);
1142 gPending--;
1143 continue;
1144 }
1145
mtklein21eaf3b2016-02-08 12:39:59 -08001146 Task task(src, sink);
1147 if (src->serial() || sink->serial()) {
1148 serial.push_back(task);
1149 } else {
1150 parallel.add([task] { Task::Run(task); });
mtklein748ca3b2015-01-15 10:56:12 -08001151 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001152 }
mtklein21eaf3b2016-02-08 12:39:59 -08001153 for (auto test : gParallelTests) {
1154 parallel.add([test] { run_test(test); });
mtklein55e88b22015-01-21 15:50:13 -08001155 }
mtklein21eaf3b2016-02-08 12:39:59 -08001156
1157 // With the parallel work running, run serial tasks and tests here on main thread.
1158 for (auto task : serial) { Task::Run(task); }
1159 for (auto test : gSerialTests) { run_test(test); }
1160
1161 // Wait for any remaining parallel work to complete (including any spun off of serial tasks).
1162 parallel.wait();
mtkleincd50bca2016-01-05 06:20:20 -08001163 gDefinitelyThreadSafeWork.wait();
1164
mtkleinda884c42016-02-24 18:00:23 -08001165 // We'd better have run everything.
1166 SkASSERT(gPending == 0);
1167
mtklein748ca3b2015-01-15 10:56:12 -08001168 // At this point we're back in single-threaded land.
caryclarkf53ce802015-06-15 06:48:30 -07001169 sk_tool_utils::release_portable_typefaces();
mtklein@google.comd36522d2013-10-16 13:02:15 +00001170
mtklein748ca3b2015-01-15 10:56:12 -08001171 if (gFailures.count() > 0) {
1172 SkDebugf("Failures:\n");
1173 for (int i = 0; i < gFailures.count(); i++) {
mtklein9dc09102015-01-15 15:47:33 -08001174 SkDebugf("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001175 }
1176 SkDebugf("%d failures\n", gFailures.count());
1177 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001178 }
mtkleinafae30a2016-02-24 12:28:32 -08001179
1180#ifdef SK_PDF_IMAGE_STATS
halcanary7a14b312015-10-01 07:28:13 -07001181 SkPDFImageDumpStats();
mtkleinafae30a2016-02-24 12:28:32 -08001182#endif // SK_PDF_IMAGE_STATS
1183
1184 print_status();
scroggo4d9eaea2016-01-29 07:48:33 -08001185 SkDebugf("Finished!\n");
mtklein748ca3b2015-01-15 10:56:12 -08001186 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +00001187}
jcgregorio3b27ade2014-11-13 08:06:40 -08001188
kkinnunen179a8f52015-11-20 13:32:24 -08001189// TODO: currently many GPU tests are declared outside SK_SUPPORT_GPU guards.
1190// Thus we export the empty RunWithGPUTestContexts when SK_SUPPORT_GPU=0.
1191namespace skiatest {
1192namespace {
1193typedef void(*TestWithGrContext)(skiatest::Reporter*, GrContext*);
1194typedef void(*TestWithGrContextAndGLContext)(skiatest::Reporter*, GrContext*, SkGLContext*);
1195#if SK_SUPPORT_GPU
1196template<typename T>
kkinnunen34058002016-01-06 23:49:30 -08001197void call_test(T test, skiatest::Reporter* reporter, const GrContextFactory::ContextInfo& context);
kkinnunen179a8f52015-11-20 13:32:24 -08001198template<>
1199void call_test(TestWithGrContext test, skiatest::Reporter* reporter,
kkinnunen34058002016-01-06 23:49:30 -08001200 const GrContextFactory::ContextInfo& context) {
1201 test(reporter, context.fGrContext);
kkinnunen179a8f52015-11-20 13:32:24 -08001202}
1203template<>
1204void call_test(TestWithGrContextAndGLContext test, skiatest::Reporter* reporter,
kkinnunen34058002016-01-06 23:49:30 -08001205 const GrContextFactory::ContextInfo& context) {
1206 test(reporter, context.fGrContext, context.fGLContext);
kkinnunen179a8f52015-11-20 13:32:24 -08001207}
1208#endif
1209} // namespace
1210
kkinnunen179a8f52015-11-20 13:32:24 -08001211template<typename T>
1212void RunWithGPUTestContexts(T test, GPUTestContexts testContexts, Reporter* reporter,
1213 GrContextFactory* factory) {
1214#if SK_SUPPORT_GPU
kkinnunen3e980c32015-12-23 01:33:00 -08001215 // Iterate over context types, except use "native" instead of explicitly trying OpenGL and
1216 // OpenGL ES. Do not use GLES on desktop, since tests do not account for not fixing
1217 // http://skbug.com/2809
1218 GrContextFactory::GLContextType contextTypes[] = {
1219 GrContextFactory::kNative_GLContextType,
1220#if SK_ANGLE
1221#ifdef SK_BUILD_FOR_WIN
1222 GrContextFactory::kANGLE_GLContextType,
1223#endif
1224 GrContextFactory::kANGLE_GL_GLContextType,
1225#endif
1226#if SK_COMMAND_BUFFER
1227 GrContextFactory::kCommandBuffer_GLContextType,
1228#endif
1229#if SK_MESA
1230 GrContextFactory::kMESA_GLContextType,
1231#endif
1232 GrContextFactory::kNull_GLContextType,
1233 GrContextFactory::kDebug_GLContextType,
1234 };
1235 static_assert(SK_ARRAY_COUNT(contextTypes) == GrContextFactory::kGLContextTypeCnt - 2,
1236 "Skipping unexpected GLContextType for GPU tests");
1237
1238 for (auto& contextType : contextTypes) {
kkinnunen179a8f52015-11-20 13:32:24 -08001239 int contextSelector = kNone_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001240 if (GrContextFactory::IsRenderingGLContext(contextType)) {
kkinnunen179a8f52015-11-20 13:32:24 -08001241 contextSelector |= kAllRendering_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001242 } else if (contextType == GrContextFactory::kNative_GLContextType) {
kkinnunen179a8f52015-11-20 13:32:24 -08001243 contextSelector |= kNative_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001244 } else if (contextType == GrContextFactory::kNull_GLContextType) {
kkinnunen179a8f52015-11-20 13:32:24 -08001245 contextSelector |= kNull_GPUTestContexts;
kkinnunen3e980c32015-12-23 01:33:00 -08001246 } else if (contextType == GrContextFactory::kDebug_GLContextType) {
kkinnunen55eeae92015-12-10 23:19:29 -08001247 contextSelector |= kDebug_GPUTestContexts;
kkinnunen179a8f52015-11-20 13:32:24 -08001248 }
1249 if ((testContexts & contextSelector) == 0) {
1250 continue;
1251 }
kkinnunen34058002016-01-06 23:49:30 -08001252 GrContextFactory::ContextInfo context = factory->getContextInfo(contextType);
1253 if (context.fGrContext) {
kkinnunen5219fd92015-12-10 06:28:13 -08001254 call_test(test, reporter, context);
1255 }
kkinnunen34058002016-01-06 23:49:30 -08001256 context = factory->getContextInfo(contextType,
1257 GrContextFactory::kEnableNVPR_GLContextOptions);
1258 if (context.fGrContext) {
kkinnunen179a8f52015-11-20 13:32:24 -08001259 call_test(test, reporter, context);
1260 }
1261 }
1262#endif
1263}
1264
1265template
1266void RunWithGPUTestContexts<TestWithGrContext>(TestWithGrContext test,
1267 GPUTestContexts testContexts,
1268 Reporter* reporter,
1269 GrContextFactory* factory);
1270template
1271void RunWithGPUTestContexts<TestWithGrContextAndGLContext>(TestWithGrContextAndGLContext test,
1272 GPUTestContexts testContexts,
1273 Reporter* reporter,
1274 GrContextFactory* factory);
1275} // namespace skiatest
1276
borenet48087572015-04-02 12:16:36 -07001277#if !defined(SK_BUILD_FOR_IOS)
jcgregorio3b27ade2014-11-13 08:06:40 -08001278int main(int argc, char** argv) {
1279 SkCommandLineFlags::Parse(argc, argv);
1280 return dm_main();
1281}
1282#endif