blob: 57de4c1d7f234748b2fd0884fb116731a349f054 [file] [log] [blame]
scroggo478652e2015-03-25 07:11:02 -07001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
jcgregorio3b27ade2014-11-13 08:06:40 -08008#include "CrashHandler.h"
mtklein748ca3b2015-01-15 10:56:12 -08009#include "DMJsonWriter.h"
10#include "DMSrcSink.h"
tomhudsoneebc39a2015-02-23 12:18:05 -080011#include "DMSrcSinkAndroid.h"
mtklein748ca3b2015-01-15 10:56:12 -080012#include "OverwriteLine.h"
13#include "ProcStats.h"
14#include "SkBBHFactory.h"
mtklein62bd1a62015-01-27 14:46:26 -080015#include "SkChecksum.h"
caryclark17f0b6d2014-07-22 10:15:34 -070016#include "SkCommonFlags.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +000017#include "SkForceLinking.h"
18#include "SkGraphics.h"
mtkleine67164d2015-02-02 13:24:37 -080019#include "SkInstCnt.h"
mtklein748ca3b2015-01-15 10:56:12 -080020#include "SkMD5.h"
mtklein1d0f1642014-09-08 08:05:18 -070021#include "SkOSFile.h"
mtkleina82f5622015-02-20 12:30:19 -080022#include "SkTHash.h"
mtklein406654b2014-09-03 15:34:37 -070023#include "SkTaskGroup.h"
mtkleinde6fc2b2015-03-12 06:28:54 -070024#include "SkThreadUtils.h"
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +000025#include "Test.h"
mtklein748ca3b2015-01-15 10:56:12 -080026#include "Timer.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +000027
djsollen54416de2015-04-03 07:24:48 -070028DEFINE_string(src, "tests gm skp image", "Source types to test.");
mtklein748ca3b2015-01-15 10:56:12 -080029DEFINE_bool(nameByHash, false,
30 "If true, write to FLAGS_writePath[0]/<hash>.png instead of "
djsollen54416de2015-04-03 07:24:48 -070031 "to FLAGS_writePath[0]/<config>/<sourceType>/<sourceOptions>/<name>.png");
mtklein748ca3b2015-01-15 10:56:12 -080032DEFINE_bool2(pathOpsExtended, x, false, "Run extended pathOps tests.");
mtkleind603b222015-02-17 11:13:33 -080033DEFINE_string(matrix, "1 0 0 1",
34 "2x2 scale+skew matrix to apply or upright when using "
35 "'matrix' or 'upright' in config.");
mtklein82d28432015-01-15 12:46:02 -080036DEFINE_bool(gpu_threading, false, "Allow GPU work to run on multiple threads?");
mtklein@google.comd36522d2013-10-16 13:02:15 +000037
mtkleina2ef6422015-01-15 13:44:22 -080038DEFINE_string(blacklist, "",
djsollen54416de2015-04-03 07:24:48 -070039 "Space-separated config/src/srcOptions/name quadruples to blacklist. '_' matches anything. E.g. \n"
40 "'--blacklist gpu skp _ _' will blacklist all SKPs drawn into the gpu config.\n"
41 "'--blacklist gpu skp _ _ 8888 gm _ aarects' will also blacklist the aarects GM on 8888.");
mtkleina2ef6422015-01-15 13:44:22 -080042
mtklein62bd1a62015-01-27 14:46:26 -080043DEFINE_string2(readPath, r, "", "If set check for equality with golden results in this directory.");
44
borenet09ed4802015-04-03 14:15:33 -070045DEFINE_string(uninterestingHashesFile, "",
46 "File containing a list of uninteresting hashes. If a result hashes to something in "
47 "this list, no image is written for that result.");
48
mtklein6393c062015-04-27 08:45:01 -070049DEFINE_int32(shards, 1, "We're splitting source data into this many shards.");
50DEFINE_int32(shard, 0, "Which shard do I run?");
51
mtklein@google.comd36522d2013-10-16 13:02:15 +000052__SK_FORCE_IMAGE_DECODER_LINKING;
mtklein748ca3b2015-01-15 10:56:12 -080053using namespace DM;
mtklein@google.comd36522d2013-10-16 13:02:15 +000054
mtklein748ca3b2015-01-15 10:56:12 -080055/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
56
57SK_DECLARE_STATIC_MUTEX(gFailuresMutex);
58static SkTArray<SkString> gFailures;
59
60static void fail(ImplicitString err) {
61 SkAutoMutexAcquire lock(gFailuresMutex);
62 SkDebugf("\n\nFAILURE: %s\n\n", err.c_str());
63 gFailures.push_back(err);
halcanary9e398f72015-01-10 11:18:04 -080064}
65
mtkleinb37cb412015-03-18 05:27:14 -070066static int32_t gPending = 0; // Atomic. Total number of running and queued tasks.
67
68SK_DECLARE_STATIC_MUTEX(gRunningMutex);
69static SkTArray<SkString> gRunning;
mtklein748ca3b2015-01-15 10:56:12 -080070
mtkleinb9eb4ac2015-02-02 18:26:03 -080071static void done(double ms,
djsollen54416de2015-04-03 07:24:48 -070072 ImplicitString config, ImplicitString src, ImplicitString srcOptions,
73 ImplicitString name, ImplicitString note, ImplicitString log) {
74 SkString id = SkStringPrintf("%s %s %s %s", config.c_str(), src.c_str(),
75 srcOptions.c_str(), name.c_str());
mtkleinb37cb412015-03-18 05:27:14 -070076 {
77 SkAutoMutexAcquire lock(gRunningMutex);
78 for (int i = 0; i < gRunning.count(); i++) {
79 if (gRunning[i] == id) {
80 gRunning.removeShuffle(i);
81 break;
82 }
83 }
84 }
85 if (!FLAGS_verbose) {
86 note = "";
87 }
mtkleinb9eb4ac2015-02-02 18:26:03 -080088 if (!log.isEmpty()) {
89 log.prepend("\n");
90 }
mtklein6dee2ad2015-02-05 09:53:44 -080091 auto pending = sk_atomic_dec(&gPending)-1;
mtkleinb37cb412015-03-18 05:27:14 -070092 SkDebugf("%s(%4dMB %5d) %s\t%s%s%s", FLAGS_verbose ? "\n" : kSkOverwriteLine
93 , sk_tools::getBestResidentSetSizeMB()
94 , pending
95 , HumanizeMs(ms).c_str()
96 , id.c_str()
97 , note.c_str()
98 , log.c_str());
mtkleina17241b2015-01-23 05:48:00 -080099 // We write our dm.json file every once in a while in case we crash.
100 // Notice this also handles the final dm.json when pending == 0.
101 if (pending % 500 == 0) {
102 JsonWriter::DumpJson();
103 }
mtklein@google.comd36522d2013-10-16 13:02:15 +0000104}
105
djsollen54416de2015-04-03 07:24:48 -0700106static void start(ImplicitString config, ImplicitString src,
107 ImplicitString srcOptions, ImplicitString name) {
108 SkString id = SkStringPrintf("%s %s %s %s", config.c_str(), src.c_str(),
109 srcOptions.c_str(), name.c_str());
mtkleinb37cb412015-03-18 05:27:14 -0700110 SkAutoMutexAcquire lock(gRunningMutex);
111 gRunning.push_back(id);
112}
113
mtklein748ca3b2015-01-15 10:56:12 -0800114/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
mtklein114c3cd2015-01-15 10:15:02 -0800115
mtklein62bd1a62015-01-27 14:46:26 -0800116struct Gold : public SkString {
mtkleina82f5622015-02-20 12:30:19 -0800117 Gold() : SkString("") {}
djsollen54416de2015-04-03 07:24:48 -0700118 Gold(ImplicitString sink, ImplicitString src, ImplicitString srcOptions,
119 ImplicitString name, ImplicitString md5)
mtklein62bd1a62015-01-27 14:46:26 -0800120 : SkString("") {
121 this->append(sink);
122 this->append(src);
djsollen54416de2015-04-03 07:24:48 -0700123 this->append(srcOptions);
mtklein62bd1a62015-01-27 14:46:26 -0800124 this->append(name);
125 this->append(md5);
mtklein62bd1a62015-01-27 14:46:26 -0800126 }
mtklein02f46cf2015-03-20 13:48:42 -0700127 static uint32_t Hash(const Gold& g) { return SkGoodHash((const SkString&)g); }
mtklein62bd1a62015-01-27 14:46:26 -0800128};
mtkleina82f5622015-02-20 12:30:19 -0800129static SkTHashSet<Gold, Gold::Hash> gGold;
mtklein62bd1a62015-01-27 14:46:26 -0800130
131static void add_gold(JsonWriter::BitmapResult r) {
djsollen54416de2015-04-03 07:24:48 -0700132 gGold.add(Gold(r.config, r.sourceType, r.sourceOptions, r.name, r.md5));
mtklein62bd1a62015-01-27 14:46:26 -0800133}
134
135static void gather_gold() {
136 if (!FLAGS_readPath.isEmpty()) {
137 SkString path(FLAGS_readPath[0]);
138 path.append("/dm.json");
139 if (!JsonWriter::ReadJson(path.c_str(), add_gold)) {
140 fail(SkStringPrintf("Couldn't read %s for golden results.", path.c_str()));
141 }
142 }
143}
144
145/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
146
borenet09ed4802015-04-03 14:15:33 -0700147static SkTHashSet<SkString> gUninterestingHashes;
148
149static void gather_uninteresting_hashes() {
150 if (!FLAGS_uninterestingHashesFile.isEmpty()) {
151 SkAutoTUnref<SkData> data(SkData::NewFromFileName(FLAGS_uninterestingHashesFile[0]));
152 SkTArray<SkString> hashes;
153 SkStrSplit((const char*)data->data(), "\n", &hashes);
154 for (const SkString& hash : hashes) {
155 gUninterestingHashes.add(hash);
156 }
157 }
158}
159
160/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
161
mtklein748ca3b2015-01-15 10:56:12 -0800162template <typename T>
djsollen54416de2015-04-03 07:24:48 -0700163struct Tagged : public SkAutoTDelete<T> {
164 const char* tag;
165 const char* options;
166};
mtklein748ca3b2015-01-15 10:56:12 -0800167
168static const bool kMemcpyOK = true;
169
170static SkTArray<Tagged<Src>, kMemcpyOK> gSrcs;
171static SkTArray<Tagged<Sink>, kMemcpyOK> gSinks;
172
mtklein6393c062015-04-27 08:45:01 -0700173static bool in_shard() {
174 static int N = 0;
175 return N++ % FLAGS_shards == FLAGS_shard;
176}
177
djsollen54416de2015-04-03 07:24:48 -0700178static void push_src(const char* tag, const char* options, Src* s) {
mtklein748ca3b2015-01-15 10:56:12 -0800179 SkAutoTDelete<Src> src(s);
mtklein6393c062015-04-27 08:45:01 -0700180 if (in_shard() &&
181 FLAGS_src.contains(tag) &&
mtklein748ca3b2015-01-15 10:56:12 -0800182 !SkCommandLineFlags::ShouldSkip(FLAGS_match, src->name().c_str())) {
183 Tagged<Src>& s = gSrcs.push_back();
184 s.reset(src.detach());
185 s.tag = tag;
djsollen54416de2015-04-03 07:24:48 -0700186 s.options = options;
mtklein114c3cd2015-01-15 10:15:02 -0800187 }
mtklein748ca3b2015-01-15 10:56:12 -0800188}
mtklein114c3cd2015-01-15 10:15:02 -0800189
msarett438b2ad2015-04-09 12:43:10 -0700190static void push_codec_srcs(Path path) {
191 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
192 if (!encoded) {
193 SkDebugf("Couldn't read %s.", path.c_str());
194 return;
195 }
196 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
197 if (NULL == codec.get()) {
198 SkDebugf("Couldn't create codec for %s.", path.c_str());
199 return;
200 }
201
202 // Build additional test cases for images that decode natively to non-canvas types
203 switch(codec->getInfo().colorType()) {
204 case kGray_8_SkColorType:
djsollena669bc32015-04-14 13:47:51 -0700205 push_src("image", "codec_kGray8", new CodecSrc(path, CodecSrc::kNormal_Mode,
msarett438b2ad2015-04-09 12:43:10 -0700206 CodecSrc::kGrayscale_Always_DstColorType));
djsollena669bc32015-04-14 13:47:51 -0700207 push_src("image", "scanline_kGray8", new CodecSrc(path, CodecSrc::kScanline_Mode,
msarett438b2ad2015-04-09 12:43:10 -0700208 CodecSrc::kGrayscale_Always_DstColorType));
209 // Intentional fall through
210 // FIXME: Is this a long term solution for testing wbmps decodes to kIndex8?
211 // Further discussion on this topic is at skbug.com/3683
212 case kIndex_8_SkColorType:
djsollena669bc32015-04-14 13:47:51 -0700213 push_src("image", "codec_kIndex8", new CodecSrc(path, CodecSrc::kNormal_Mode,
msarett438b2ad2015-04-09 12:43:10 -0700214 CodecSrc::kIndex8_Always_DstColorType));
msarett164d5b02015-04-14 05:37:36 -0700215 // FIXME: Need to implement scanline decoding for kIndex8.
216 //push_src("image", "scanline kIndex8", new CodecSrc(path, CodecSrc::kScanline_Mode,
217 // CodecSrc::kIndex8_Always_DstColorType));
msarett438b2ad2015-04-09 12:43:10 -0700218 break;
219 default:
220 // Do nothing
221 break;
222 }
223
224 // Decode all images to the canvas color type
225 push_src("image", "codec", new CodecSrc(path, CodecSrc::kNormal_Mode,
226 CodecSrc::kGetFromCanvas_DstColorType));
227 push_src("image", "scanline", new CodecSrc(path, CodecSrc::kScanline_Mode,
228 CodecSrc::kGetFromCanvas_DstColorType));
229}
230
scroggo9b77ddd2015-03-19 06:03:39 -0700231static bool codec_supported(const char* ext) {
232 // FIXME: Once other versions of SkCodec are available, we can add them to this
233 // list (and eventually we can remove this check once they are all supported).
msarett8c8f22a2015-04-01 06:58:48 -0700234 static const char* const exts[] = {
msarette16b04a2015-04-15 07:32:19 -0700235 "bmp", "gif", "jpg", "jpeg", "png", "ico", "wbmp",
236 "BMP", "GIF", "JPG", "JPEG", "PNG", "ICO", "WBMP"
msarett8c8f22a2015-04-01 06:58:48 -0700237 };
238
239 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
240 if (0 == strcmp(exts[i], ext)) {
241 return true;
242 }
243 }
244 return false;
scroggo9b77ddd2015-03-19 06:03:39 -0700245}
246
mtklein748ca3b2015-01-15 10:56:12 -0800247static void gather_srcs() {
248 for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) {
djsollen54416de2015-04-03 07:24:48 -0700249 push_src("gm", "", new GMSrc(r->factory()));
mtklein748ca3b2015-01-15 10:56:12 -0800250 }
halcanaryfc37ad12015-01-30 07:31:19 -0800251 for (int i = 0; i < FLAGS_skps.count(); i++) {
252 const char* path = FLAGS_skps[i];
253 if (sk_isdir(path)) {
254 SkOSFile::Iter it(path, "skp");
255 for (SkString file; it.next(&file); ) {
djsollen54416de2015-04-03 07:24:48 -0700256 push_src("skp", "", new SKPSrc(SkOSPath::Join(path, file.c_str())));
halcanaryfc37ad12015-01-30 07:31:19 -0800257 }
258 } else {
djsollen54416de2015-04-03 07:24:48 -0700259 push_src("skp", "", new SKPSrc(path));
mtklein114c3cd2015-01-15 10:15:02 -0800260 }
261 }
halcanary23b03c32015-01-30 09:58:58 -0800262 static const char* const exts[] = {
263 "bmp", "gif", "jpg", "jpeg", "png", "webp", "ktx", "astc", "wbmp", "ico",
264 "BMP", "GIF", "JPG", "JPEG", "PNG", "WEBP", "KTX", "ASTC", "WBMP", "ICO",
265 };
266 for (int i = 0; i < FLAGS_images.count(); i++) {
267 const char* flag = FLAGS_images[i];
268 if (sk_isdir(flag)) {
269 for (size_t j = 0; j < SK_ARRAY_COUNT(exts); j++) {
270 SkOSFile::Iter it(flag, exts[j]);
271 for (SkString file; it.next(&file); ) {
272 SkString path = SkOSPath::Join(flag, file.c_str());
djsollen54416de2015-04-03 07:24:48 -0700273 push_src("image", "decode", new ImageSrc(path)); // Decode entire image
274 push_src("image", "subset", new ImageSrc(path, 2)); // Decode into 2x2 subsets
scroggo9b77ddd2015-03-19 06:03:39 -0700275 if (codec_supported(exts[j])) {
msarett438b2ad2015-04-09 12:43:10 -0700276 push_codec_srcs(path);
scroggo9b77ddd2015-03-19 06:03:39 -0700277 }
halcanary23b03c32015-01-30 09:58:58 -0800278 }
mtklein114c3cd2015-01-15 10:15:02 -0800279 }
halcanary23b03c32015-01-30 09:58:58 -0800280 } else if (sk_exists(flag)) {
281 // assume that FLAGS_images[i] is a valid image if it is a file.
djsollen54416de2015-04-03 07:24:48 -0700282 push_src("image", "decode", new ImageSrc(flag)); // Decode entire image.
283 push_src("image", "subset", new ImageSrc(flag, 2)); // Decode into 2 x 2 subsets
msarett438b2ad2015-04-09 12:43:10 -0700284 push_codec_srcs(flag);
mtklein709d2c32015-01-15 08:30:25 -0800285 }
mtklein709d2c32015-01-15 08:30:25 -0800286 }
287}
288
mtklein748ca3b2015-01-15 10:56:12 -0800289static GrGLStandard get_gpu_api() {
290 if (FLAGS_gpuAPI.contains("gl")) { return kGL_GrGLStandard; }
291 if (FLAGS_gpuAPI.contains("gles")) { return kGLES_GrGLStandard; }
292 return kNone_GrGLStandard;
mtklein709d2c32015-01-15 08:30:25 -0800293}
294
mtklein748ca3b2015-01-15 10:56:12 -0800295static void push_sink(const char* tag, Sink* s) {
296 SkAutoTDelete<Sink> sink(s);
297 if (!FLAGS_config.contains(tag)) {
298 return;
mtklein114c3cd2015-01-15 10:15:02 -0800299 }
mtklein748ca3b2015-01-15 10:56:12 -0800300 // Try a noop Src as a canary. If it fails, skip this sink.
301 struct : public Src {
mtklein36352bf2015-03-25 18:17:31 -0700302 Error draw(SkCanvas*) const override { return ""; }
303 SkISize size() const override { return SkISize::Make(16, 16); }
304 Name name() const override { return "noop"; }
mtklein748ca3b2015-01-15 10:56:12 -0800305 } noop;
mtklein114c3cd2015-01-15 10:15:02 -0800306
mtklein748ca3b2015-01-15 10:56:12 -0800307 SkBitmap bitmap;
308 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800309 SkString log;
310 Error err = sink->draw(noop, &bitmap, &stream, &log);
mtklein4089ef72015-03-05 08:40:28 -0800311 if (err.isFatal()) {
mtklein05641a52015-04-21 10:49:13 -0700312 SkDebugf("Could not run %s: %s\n", tag, err.c_str());
313 exit(1);
mtklein114c3cd2015-01-15 10:15:02 -0800314 }
315
mtklein748ca3b2015-01-15 10:56:12 -0800316 Tagged<Sink>& ts = gSinks.push_back();
317 ts.reset(sink.detach());
318 ts.tag = tag;
319}
320
321static bool gpu_supported() {
322#if SK_SUPPORT_GPU
323 return FLAGS_gpu;
324#else
325 return false;
326#endif
327}
328
329static Sink* create_sink(const char* tag) {
330#define SINK(t, sink, ...) if (0 == strcmp(t, tag)) { return new sink(__VA_ARGS__); }
331 if (gpu_supported()) {
mtklein82d28432015-01-15 12:46:02 -0800332 typedef GrContextFactory Gr;
mtklein748ca3b2015-01-15 10:56:12 -0800333 const GrGLStandard api = get_gpu_api();
mtklein82d28432015-01-15 12:46:02 -0800334 SINK("gpunull", GPUSink, Gr::kNull_GLContextType, api, 0, false, FLAGS_gpu_threading);
335 SINK("gpudebug", GPUSink, Gr::kDebug_GLContextType, api, 0, false, FLAGS_gpu_threading);
336 SINK("gpu", GPUSink, Gr::kNative_GLContextType, api, 0, false, FLAGS_gpu_threading);
337 SINK("gpudft", GPUSink, Gr::kNative_GLContextType, api, 0, true, FLAGS_gpu_threading);
338 SINK("msaa4", GPUSink, Gr::kNative_GLContextType, api, 4, false, FLAGS_gpu_threading);
339 SINK("msaa16", GPUSink, Gr::kNative_GLContextType, api, 16, false, FLAGS_gpu_threading);
340 SINK("nvprmsaa4", GPUSink, Gr::kNVPR_GLContextType, api, 4, false, FLAGS_gpu_threading);
341 SINK("nvprmsaa16", GPUSink, Gr::kNVPR_GLContextType, api, 16, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800342 #if SK_ANGLE
mtklein82d28432015-01-15 12:46:02 -0800343 SINK("angle", GPUSink, Gr::kANGLE_GLContextType, api, 0, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800344 #endif
345 #if SK_MESA
mtklein82d28432015-01-15 12:46:02 -0800346 SINK("mesa", GPUSink, Gr::kMESA_GLContextType, api, 0, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800347 #endif
mtklein114c3cd2015-01-15 10:15:02 -0800348 }
mtklein748ca3b2015-01-15 10:56:12 -0800349
tomhudsoneebc39a2015-02-23 12:18:05 -0800350#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
351 SINK("hwui", HWUISink);
352#endif
353
mtklein748ca3b2015-01-15 10:56:12 -0800354 if (FLAGS_cpu) {
355 SINK("565", RasterSink, kRGB_565_SkColorType);
356 SINK("8888", RasterSink, kN32_SkColorType);
mtklein19f30602015-01-20 13:34:39 -0800357 SINK("pdf", PDFSink);
mtklein9c3f17d2015-01-28 11:35:18 -0800358 SINK("skp", SKPSink);
mtklein8a4527e2015-01-31 20:00:58 -0800359 SINK("svg", SVGSink);
360 SINK("null", NullSink);
halcanary47ef4d52015-03-03 09:13:09 -0800361 SINK("xps", XPSSink);
mtklein748ca3b2015-01-15 10:56:12 -0800362 }
363#undef SINK
364 return NULL;
mtklein114c3cd2015-01-15 10:15:02 -0800365}
366
mtklein748ca3b2015-01-15 10:56:12 -0800367static Sink* create_via(const char* tag, Sink* wrapped) {
368#define VIA(t, via, ...) if (0 == strcmp(t, tag)) { return new via(__VA_ARGS__); }
mtklein7edca212015-01-21 13:18:51 -0800369 VIA("pipe", ViaPipe, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800370 VIA("serialize", ViaSerialization, wrapped);
mtkleinb7e8d692015-04-07 08:30:32 -0700371 VIA("2ndpic", ViaSecondPicture, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800372 VIA("tiles", ViaTiles, 256, 256, NULL, wrapped);
373 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800374
mtkleind603b222015-02-17 11:13:33 -0800375 if (FLAGS_matrix.count() == 4) {
mtklein748ca3b2015-01-15 10:56:12 -0800376 SkMatrix m;
mtkleind603b222015-02-17 11:13:33 -0800377 m.reset();
378 m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
379 m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
380 m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
381 m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
382 VIA("matrix", ViaMatrix, m, wrapped);
383 VIA("upright", ViaUpright, m, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800384 }
tomhudson64de1e12015-03-05 08:01:07 -0800385
386#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
387 VIA("androidsdk", ViaAndroidSDK, wrapped);
388#endif
389
mtklein748ca3b2015-01-15 10:56:12 -0800390#undef VIA
391 return NULL;
mtklein114c3cd2015-01-15 10:15:02 -0800392}
393
mtklein748ca3b2015-01-15 10:56:12 -0800394static void gather_sinks() {
395 for (int i = 0; i < FLAGS_config.count(); i++) {
396 const char* config = FLAGS_config[i];
397 SkTArray<SkString> parts;
398 SkStrSplit(config, "-", &parts);
399
400 Sink* sink = NULL;
401 for (int i = parts.count(); i-- > 0;) {
402 const char* part = parts[i].c_str();
403 Sink* next = (sink == NULL) ? create_sink(part) : create_via(part, sink);
404 if (next == NULL) {
405 SkDebugf("Skipping %s: Don't understand '%s'.\n", config, part);
406 delete sink;
407 sink = NULL;
408 break;
409 }
410 sink = next;
411 }
412 if (sink) {
413 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800414 }
415 }
416}
mtklein709d2c32015-01-15 08:30:25 -0800417
mtkleina2ef6422015-01-15 13:44:22 -0800418static bool match(const char* needle, const char* haystack) {
419 return 0 == strcmp("_", needle) || NULL != strstr(haystack, needle);
420}
421
djsollen54416de2015-04-03 07:24:48 -0700422static ImplicitString is_blacklisted(const char* sink, const char* src,
423 const char* srcOptions, const char* name) {
mtklein0d243ff2015-04-03 07:51:00 -0700424 for (int i = 0; i < FLAGS_blacklist.count() - 3; i += 4) {
mtkleina2ef6422015-01-15 13:44:22 -0800425 if (match(FLAGS_blacklist[i+0], sink) &&
djsollen54416de2015-04-03 07:24:48 -0700426 match(FLAGS_blacklist[i+1], src) &&
427 match(FLAGS_blacklist[i+2], srcOptions) &&
428 match(FLAGS_blacklist[i+3], name)) {
429 return SkStringPrintf("%s %s %s %s",
430 FLAGS_blacklist[i+0], FLAGS_blacklist[i+1],
431 FLAGS_blacklist[i+2], FLAGS_blacklist[i+3]);
mtkleina2ef6422015-01-15 13:44:22 -0800432 }
433 }
434 return "";
435}
436
mtklein748ca3b2015-01-15 10:56:12 -0800437// The finest-grained unit of work we can run: draw a single Src into a single Sink,
438// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
439struct Task {
440 Task(const Tagged<Src>& src, const Tagged<Sink>& sink) : src(src), sink(sink) {}
441 const Tagged<Src>& src;
442 const Tagged<Sink>& sink;
443
444 static void Run(Task* task) {
mtkleina2ef6422015-01-15 13:44:22 -0800445 SkString name = task->src->name();
mtkleinb37cb412015-03-18 05:27:14 -0700446 SkString note;
djsollen54416de2015-04-03 07:24:48 -0700447 SkString whyBlacklisted = is_blacklisted(task->sink.tag, task->src.tag,
448 task->src.options, name.c_str());
mtkleinb37cb412015-03-18 05:27:14 -0700449 if (!whyBlacklisted.isEmpty()) {
450 note.appendf(" (--blacklist %s)", whyBlacklisted.c_str());
451 }
mtkleinb9eb4ac2015-02-02 18:26:03 -0800452 SkString log;
mtklein748ca3b2015-01-15 10:56:12 -0800453 WallTimer timer;
454 timer.start();
mtkleina2ef6422015-01-15 13:44:22 -0800455 if (!FLAGS_dryRun && whyBlacklisted.isEmpty()) {
mtklein748ca3b2015-01-15 10:56:12 -0800456 SkBitmap bitmap;
457 SkDynamicMemoryWStream stream;
djsollen54416de2015-04-03 07:24:48 -0700458 start(task->sink.tag, task->src.tag, task->src.options, name.c_str());
mtkleinb9eb4ac2015-02-02 18:26:03 -0800459 Error err = task->sink->draw(*task->src, &bitmap, &stream, &log);
mtklein748ca3b2015-01-15 10:56:12 -0800460 if (!err.isEmpty()) {
mtklein4089ef72015-03-05 08:40:28 -0800461 timer.end();
462 if (err.isFatal()) {
djsollen54416de2015-04-03 07:24:48 -0700463 fail(SkStringPrintf("%s %s %s %s: %s",
mtklein4089ef72015-03-05 08:40:28 -0800464 task->sink.tag,
465 task->src.tag,
djsollen54416de2015-04-03 07:24:48 -0700466 task->src.options,
mtklein4089ef72015-03-05 08:40:28 -0800467 name.c_str(),
468 err.c_str()));
mtkleinb37cb412015-03-18 05:27:14 -0700469 } else {
470 note.appendf(" (skipped: %s)", err.c_str());
mtklein4089ef72015-03-05 08:40:28 -0800471 }
djsollen54416de2015-04-03 07:24:48 -0700472 done(timer.fWall, task->sink.tag, task->src.tag, task->src.options,
473 name, note, log);
mtklein4089ef72015-03-05 08:40:28 -0800474 return;
mtklein748ca3b2015-01-15 10:56:12 -0800475 }
mtklein62bd1a62015-01-27 14:46:26 -0800476 SkAutoTDelete<SkStreamAsset> data(stream.detachAsStream());
477
478 SkString md5;
479 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
480 SkMD5 hash;
481 if (data->getLength()) {
482 hash.writeStream(data, data->getLength());
483 data->rewind();
484 } else {
485 hash.write(bitmap.getPixels(), bitmap.getSize());
486 }
487 SkMD5::Digest digest;
488 hash.finish(digest);
489 for (int i = 0; i < 16; i++) {
490 md5.appendf("%02x", digest.data[i]);
491 }
492 }
493
494 if (!FLAGS_readPath.isEmpty() &&
djsollen54416de2015-04-03 07:24:48 -0700495 !gGold.contains(Gold(task->sink.tag, task->src.tag,
496 task->src.options, name, md5))) {
497 fail(SkStringPrintf("%s not found for %s %s %s %s in %s",
mtklein62bd1a62015-01-27 14:46:26 -0800498 md5.c_str(),
499 task->sink.tag,
500 task->src.tag,
djsollen54416de2015-04-03 07:24:48 -0700501 task->src.options,
mtklein62bd1a62015-01-27 14:46:26 -0800502 name.c_str(),
503 FLAGS_readPath[0]));
504 }
505
mtkleinb0531a72015-04-07 13:38:48 -0700506 if (!FLAGS_writePath.isEmpty()) {
mtklein748ca3b2015-01-15 10:56:12 -0800507 const char* ext = task->sink->fileExtension();
mtklein62bd1a62015-01-27 14:46:26 -0800508 if (data->getLength()) {
509 WriteToDisk(*task, md5, ext, data, data->getLength(), NULL);
halcanary022afb82015-01-30 11:00:12 -0800510 SkASSERT(bitmap.drawsNothing());
511 } else if (!bitmap.drawsNothing()) {
mtklein62bd1a62015-01-27 14:46:26 -0800512 WriteToDisk(*task, md5, ext, NULL, 0, &bitmap);
mtklein748ca3b2015-01-15 10:56:12 -0800513 }
514 }
515 }
516 timer.end();
djsollen54416de2015-04-03 07:24:48 -0700517 done(timer.fWall, task->sink.tag, task->src.tag, task->src.options, name, note, log);
mtklein748ca3b2015-01-15 10:56:12 -0800518 }
519
520 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -0800521 SkString md5,
522 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -0800523 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -0800524 const SkBitmap* bitmap) {
mtklein748ca3b2015-01-15 10:56:12 -0800525 JsonWriter::BitmapResult result;
djsollen54416de2015-04-03 07:24:48 -0700526 result.name = task.src->name();
527 result.config = task.sink.tag;
528 result.sourceType = task.src.tag;
529 result.sourceOptions = task.src.options;
530 result.ext = ext;
531 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -0800532 JsonWriter::AddBitmapResult(result);
533
mtkleinb0531a72015-04-07 13:38:48 -0700534 // If an MD5 is uninteresting, we want it noted in the JSON file,
535 // but don't want to dump it out as a .png (or whatever ext is).
536 if (gUninterestingHashes.contains(md5)) {
537 return;
538 }
539
mtklein748ca3b2015-01-15 10:56:12 -0800540 const char* dir = FLAGS_writePath[0];
541 if (0 == strcmp(dir, "@")) { // Needed for iOS.
542 dir = FLAGS_resourcePath[0];
543 }
544 sk_mkdir(dir);
545
546 SkString path;
547 if (FLAGS_nameByHash) {
548 path = SkOSPath::Join(dir, result.md5.c_str());
549 path.append(".");
550 path.append(ext);
551 if (sk_exists(path.c_str())) {
552 return; // Content-addressed. If it exists already, we're done.
553 }
554 } else {
555 path = SkOSPath::Join(dir, task.sink.tag);
556 sk_mkdir(path.c_str());
557 path = SkOSPath::Join(path.c_str(), task.src.tag);
558 sk_mkdir(path.c_str());
djsollen54416de2015-04-03 07:24:48 -0700559 if (strcmp(task.src.options, "") != 0) {
560 path = SkOSPath::Join(path.c_str(), task.src.options);
561 sk_mkdir(path.c_str());
562 }
mtklein748ca3b2015-01-15 10:56:12 -0800563 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
564 path.append(".");
565 path.append(ext);
566 }
567
568 SkFILEWStream file(path.c_str());
569 if (!file.isValid()) {
570 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
571 return;
572 }
573
mtklein748ca3b2015-01-15 10:56:12 -0800574 if (bitmap) {
575 // We can't encode A8 bitmaps as PNGs. Convert them to 8888 first.
576 SkBitmap converted;
577 if (bitmap->info().colorType() == kAlpha_8_SkColorType) {
578 if (!bitmap->copyTo(&converted, kN32_SkColorType)) {
579 fail("Can't convert A8 to 8888.\n");
580 return;
581 }
582 bitmap = &converted;
583 }
584 if (!SkImageEncoder::EncodeStream(&file, *bitmap, SkImageEncoder::kPNG_Type, 100)) {
585 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
586 return;
587 }
588 } else {
589 if (!file.writeStream(data, len)) {
590 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
591 return;
592 }
593 }
594 }
595};
596
597// Run all tasks in the same enclave serially on the same thread.
598// They can't possibly run concurrently with each other.
599static void run_enclave(SkTArray<Task>* tasks) {
600 for (int i = 0; i < tasks->count(); i++) {
601 Task::Run(tasks->begin() + i);
602 }
603}
604
605/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
606
607// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
608
mtklein55e88b22015-01-21 15:50:13 -0800609static SkTDArray<skiatest::Test> gThreadedTests, gGPUTests;
mtklein748ca3b2015-01-15 10:56:12 -0800610
611static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -0800612 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -0800613 return;
614 }
mtklein6393c062015-04-27 08:45:01 -0700615 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r; r = r->next()) {
616 if (!in_shard()) {
617 continue;
618 }
halcanary87f3ba42015-01-20 09:30:20 -0800619 // Despite its name, factory() is returning a reference to
620 // link-time static const POD data.
621 const skiatest::Test& test = r->factory();
622 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -0800623 continue;
624 }
halcanary87f3ba42015-01-20 09:30:20 -0800625 if (test.needsGpu && gpu_supported()) {
mtklein55e88b22015-01-21 15:50:13 -0800626 (FLAGS_gpu_threading ? gThreadedTests : gGPUTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -0800627 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein55e88b22015-01-21 15:50:13 -0800628 gThreadedTests.push(test);
mtklein82d28432015-01-15 12:46:02 -0800629 }
mtklein748ca3b2015-01-15 10:56:12 -0800630 }
631}
632
halcanary87f3ba42015-01-20 09:30:20 -0800633static void run_test(skiatest::Test* test) {
634 struct : public skiatest::Reporter {
mtklein36352bf2015-03-25 18:17:31 -0700635 void reportFailed(const skiatest::Failure& failure) override {
halcanary87f3ba42015-01-20 09:30:20 -0800636 fail(failure.toString());
637 JsonWriter::AddTestFailure(failure);
638 }
mtklein36352bf2015-03-25 18:17:31 -0700639 bool allowExtendedTest() const override {
halcanary87f3ba42015-01-20 09:30:20 -0800640 return FLAGS_pathOpsExtended;
641 }
mtklein36352bf2015-03-25 18:17:31 -0700642 bool verbose() const override { return FLAGS_veryVerbose; }
halcanary87f3ba42015-01-20 09:30:20 -0800643 } reporter;
mtklein748ca3b2015-01-15 10:56:12 -0800644 WallTimer timer;
645 timer.start();
mtklein748ca3b2015-01-15 10:56:12 -0800646 if (!FLAGS_dryRun) {
djsollen54416de2015-04-03 07:24:48 -0700647 start("unit", "test", "", test->name);
mtklein55e88b22015-01-21 15:50:13 -0800648 GrContextFactory factory;
649 test->proc(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -0800650 }
651 timer.end();
djsollen54416de2015-04-03 07:24:48 -0700652 done(timer.fWall, "unit", "test", "", test->name, "", "");
mtklein748ca3b2015-01-15 10:56:12 -0800653}
654
655/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
656
mtklein55e88b22015-01-21 15:50:13 -0800657// If we're isolating all GPU-bound work to one thread (the default), this function runs all that.
658static void run_enclave_and_gpu_tests(SkTArray<Task>* tasks) {
659 run_enclave(tasks);
660 for (int i = 0; i < gGPUTests.count(); i++) {
661 run_test(&gGPUTests[i]);
662 }
663}
664
mtkleinde6fc2b2015-03-12 06:28:54 -0700665// Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung.
666// This prints something every once in a while so that it knows we're still working.
mtklein2e1c47e2015-03-12 07:16:56 -0700667static void start_keepalive() {
668 struct Loop {
669 static void forever(void*) {
670 for (;;) {
671 static const int kSec = 300;
672 #if defined(SK_BUILD_FOR_WIN)
673 Sleep(kSec * 1000);
674 #else
675 sleep(kSec);
676 #endif
mtkleinb37cb412015-03-18 05:27:14 -0700677 SkString running;
678 {
679 SkAutoMutexAcquire lock(gRunningMutex);
680 for (int i = 0; i < gRunning.count(); i++) {
681 running.appendf("\n\t%s", gRunning[i].c_str());
682 }
683 }
684 SkDebugf("\nCurrently running:%s\n", running.c_str());
mtklein2e1c47e2015-03-12 07:16:56 -0700685 }
686 }
687 };
688 static SkThread* intentionallyLeaked = new SkThread(Loop::forever);
689 intentionallyLeaked->start();
mtkleinde6fc2b2015-03-12 06:28:54 -0700690}
691
jcgregorio3b27ade2014-11-13 08:06:40 -0800692int dm_main();
caryclark17f0b6d2014-07-22 10:15:34 -0700693int dm_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -0800694 SetupCrashHandler();
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +0000695 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -0700696 SkTaskGroup::Enabler enabled(FLAGS_threads);
mtkleine67164d2015-02-02 13:24:37 -0800697 if (FLAGS_leaks) {
698 SkInstCountPrintLeaksOnExit();
699 }
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +0000700
mtklein2e1c47e2015-03-12 07:16:56 -0700701 start_keepalive();
mtkleinde6fc2b2015-03-12 06:28:54 -0700702
mtklein62bd1a62015-01-27 14:46:26 -0800703 gather_gold();
borenet09ed4802015-04-03 14:15:33 -0700704 gather_uninteresting_hashes();
mtklein62bd1a62015-01-27 14:46:26 -0800705
mtklein748ca3b2015-01-15 10:56:12 -0800706 gather_srcs();
707 gather_sinks();
708 gather_tests();
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +0000709
mtklein55e88b22015-01-21 15:50:13 -0800710 gPending = gSrcs.count() * gSinks.count() + gThreadedTests.count() + gGPUTests.count();
mtklein748ca3b2015-01-15 10:56:12 -0800711 SkDebugf("%d srcs * %d sinks + %d tests == %d tasks\n",
mtklein55e88b22015-01-21 15:50:13 -0800712 gSrcs.count(), gSinks.count(), gThreadedTests.count() + gGPUTests.count(), gPending);
mtklein748ca3b2015-01-15 10:56:12 -0800713
714 // We try to exploit as much parallelism as is safe. Most Src/Sink pairs run on any thread,
715 // but Sinks that identify as part of a particular enclave run serially on a single thread.
mtklein82d28432015-01-15 12:46:02 -0800716 // CPU tests run on any thread. GPU tests depend on --gpu_threading.
mtklein748ca3b2015-01-15 10:56:12 -0800717 SkTArray<Task> enclaves[kNumEnclaves];
718 for (int j = 0; j < gSinks.count(); j++) {
719 SkTArray<Task>& tasks = enclaves[gSinks[j]->enclave()];
720 for (int i = 0; i < gSrcs.count(); i++) {
721 tasks.push_back(Task(gSrcs[i], gSinks[j]));
722 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +0000723 }
724
mtklein748ca3b2015-01-15 10:56:12 -0800725 SkTaskGroup tg;
mtklein55e88b22015-01-21 15:50:13 -0800726 tg.batch(run_test, gThreadedTests.begin(), gThreadedTests.count());
727 for (int i = 0; i < kNumEnclaves; i++) {
728 switch(i) {
729 case kAnyThread_Enclave:
730 tg.batch(Task::Run, enclaves[i].begin(), enclaves[i].count());
731 break;
732 case kGPU_Enclave:
733 tg.add(run_enclave_and_gpu_tests, &enclaves[i]);
734 break;
735 default:
736 tg.add(run_enclave, &enclaves[i]);
737 break;
mtklein82d28432015-01-15 12:46:02 -0800738 }
mtklein55e88b22015-01-21 15:50:13 -0800739 }
mtklein748ca3b2015-01-15 10:56:12 -0800740 tg.wait();
mtklein748ca3b2015-01-15 10:56:12 -0800741 // At this point we're back in single-threaded land.
mtklein@google.comd36522d2013-10-16 13:02:15 +0000742
mtklein2f64eec2015-01-15 14:20:41 -0800743 SkDebugf("\n");
mtklein748ca3b2015-01-15 10:56:12 -0800744 if (gFailures.count() > 0) {
745 SkDebugf("Failures:\n");
746 for (int i = 0; i < gFailures.count(); i++) {
mtklein9dc09102015-01-15 15:47:33 -0800747 SkDebugf("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800748 }
749 SkDebugf("%d failures\n", gFailures.count());
750 return 1;
mtklein114c3cd2015-01-15 10:15:02 -0800751 }
mtklein748ca3b2015-01-15 10:56:12 -0800752 if (gPending > 0) {
753 SkDebugf("Hrm, we didn't seem to run everything we intended to! Please file a bug.\n");
754 return 1;
mtklein114c3cd2015-01-15 10:15:02 -0800755 }
mtklein748ca3b2015-01-15 10:56:12 -0800756 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +0000757}
jcgregorio3b27ade2014-11-13 08:06:40 -0800758
borenet48087572015-04-02 12:16:36 -0700759#if !defined(SK_BUILD_FOR_IOS)
jcgregorio3b27ade2014-11-13 08:06:40 -0800760int main(int argc, char** argv) {
761 SkCommandLineFlags::Parse(argc, argv);
762 return dm_main();
763}
764#endif