blob: faee2ccb1b64538e6f70df785efef8f924934811 [file] [log] [blame]
jcgregorio3b27ade2014-11-13 08:06:40 -08001#include "CrashHandler.h"
mtklein748ca3b2015-01-15 10:56:12 -08002#include "DMJsonWriter.h"
3#include "DMSrcSink.h"
4#include "OverwriteLine.h"
5#include "ProcStats.h"
6#include "SkBBHFactory.h"
mtklein62bd1a62015-01-27 14:46:26 -08007#include "SkChecksum.h"
caryclark17f0b6d2014-07-22 10:15:34 -07008#include "SkCommonFlags.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +00009#include "SkForceLinking.h"
10#include "SkGraphics.h"
mtkleine67164d2015-02-02 13:24:37 -080011#include "SkInstCnt.h"
mtklein748ca3b2015-01-15 10:56:12 -080012#include "SkMD5.h"
mtklein1d0f1642014-09-08 08:05:18 -070013#include "SkOSFile.h"
mtklein62bd1a62015-01-27 14:46:26 -080014#include "SkTDynamicHash.h"
mtklein406654b2014-09-03 15:34:37 -070015#include "SkTaskGroup.h"
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +000016#include "Test.h"
mtklein748ca3b2015-01-15 10:56:12 -080017#include "Timer.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +000018
mtklein748ca3b2015-01-15 10:56:12 -080019DEFINE_string(images, "resources", "Images to decode.");
mtkleinedc93bc2015-01-30 13:22:23 -080020DEFINE_string(src, "tests gm skp image subset", "Source types to test.");
mtklein748ca3b2015-01-15 10:56:12 -080021DEFINE_bool(nameByHash, false,
22 "If true, write to FLAGS_writePath[0]/<hash>.png instead of "
23 "to FLAGS_writePath[0]/<config>/<sourceType>/<name>.png");
24DEFINE_bool2(pathOpsExtended, x, false, "Run extended pathOps tests.");
25DEFINE_string(matrix, "1 0 0 0 1 0 0 0 1",
26 "Matrix to apply when using 'matrix' in config.");
mtklein82d28432015-01-15 12:46:02 -080027DEFINE_bool(gpu_threading, false, "Allow GPU work to run on multiple threads?");
mtklein@google.comd36522d2013-10-16 13:02:15 +000028
mtkleina2ef6422015-01-15 13:44:22 -080029DEFINE_string(blacklist, "",
30 "Space-separated config/src/name triples to blacklist. '_' matches anything. E.g. \n"
31 "'--blacklist gpu skp _' will blacklist all SKPs drawn into the gpu config.\n"
32 "'--blacklist gpu skp _ 8888 gm aarects' will also blacklist the aarects GM on 8888.");
33
mtklein62bd1a62015-01-27 14:46:26 -080034DEFINE_string2(readPath, r, "", "If set check for equality with golden results in this directory.");
35
mtklein@google.comd36522d2013-10-16 13:02:15 +000036__SK_FORCE_IMAGE_DECODER_LINKING;
mtklein748ca3b2015-01-15 10:56:12 -080037using namespace DM;
mtklein@google.comd36522d2013-10-16 13:02:15 +000038
mtklein748ca3b2015-01-15 10:56:12 -080039/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
40
41SK_DECLARE_STATIC_MUTEX(gFailuresMutex);
42static SkTArray<SkString> gFailures;
43
44static void fail(ImplicitString err) {
45 SkAutoMutexAcquire lock(gFailuresMutex);
46 SkDebugf("\n\nFAILURE: %s\n\n", err.c_str());
47 gFailures.push_back(err);
halcanary9e398f72015-01-10 11:18:04 -080048}
49
mtklein748ca3b2015-01-15 10:56:12 -080050static int32_t gPending = 0; // Atomic.
51
52static void done(double ms, ImplicitString config, ImplicitString src, ImplicitString name) {
mtkleina17241b2015-01-23 05:48:00 -080053 int32_t pending = sk_atomic_dec(&gPending)-1;
mtklein748ca3b2015-01-15 10:56:12 -080054 SkDebugf("%s(%4dMB %5d) %s\t%s %s %s ", FLAGS_verbose ? "\n" : kSkOverwriteLine
55 , sk_tools::getMaxResidentSetSizeMB()
mtkleina17241b2015-01-23 05:48:00 -080056 , pending
mtklein748ca3b2015-01-15 10:56:12 -080057 , HumanizeMs(ms).c_str()
58 , config.c_str()
59 , src.c_str()
60 , name.c_str());
mtkleina17241b2015-01-23 05:48:00 -080061 // We write our dm.json file every once in a while in case we crash.
62 // Notice this also handles the final dm.json when pending == 0.
63 if (pending % 500 == 0) {
64 JsonWriter::DumpJson();
65 }
mtklein@google.comd36522d2013-10-16 13:02:15 +000066}
67
mtklein748ca3b2015-01-15 10:56:12 -080068/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
mtklein114c3cd2015-01-15 10:15:02 -080069
mtklein62bd1a62015-01-27 14:46:26 -080070struct Gold : public SkString {
71 Gold(ImplicitString sink, ImplicitString src, ImplicitString name, ImplicitString md5)
72 : SkString("") {
73 this->append(sink);
74 this->append(src);
75 this->append(name);
76 this->append(md5);
77 while (this->size() % 4) {
78 this->append("!"); // Pad out if needed so we can pass this to Murmur3.
79 }
80 }
81 static const Gold& GetKey(const Gold& g) { return g; }
82 static uint32_t Hash(const Gold& g) {
83 return SkChecksum::Murmur3((const uint32_t*)g.c_str(), g.size());
84 }
85};
86static SkTDynamicHash<Gold, Gold> gGold;
87
88static void add_gold(JsonWriter::BitmapResult r) {
89 gGold.add(new Gold(r.config, r.sourceType, r.name, r.md5)); // We'll let these leak. Lazybones.
90}
91
92static void gather_gold() {
93 if (!FLAGS_readPath.isEmpty()) {
94 SkString path(FLAGS_readPath[0]);
95 path.append("/dm.json");
96 if (!JsonWriter::ReadJson(path.c_str(), add_gold)) {
97 fail(SkStringPrintf("Couldn't read %s for golden results.", path.c_str()));
98 }
99 }
100}
101
102/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
103
mtklein748ca3b2015-01-15 10:56:12 -0800104template <typename T>
105struct Tagged : public SkAutoTDelete<T> { const char* tag; };
106
107static const bool kMemcpyOK = true;
108
109static SkTArray<Tagged<Src>, kMemcpyOK> gSrcs;
110static SkTArray<Tagged<Sink>, kMemcpyOK> gSinks;
111
112static void push_src(const char* tag, Src* s) {
113 SkAutoTDelete<Src> src(s);
114 if (FLAGS_src.contains(tag) &&
115 !SkCommandLineFlags::ShouldSkip(FLAGS_match, src->name().c_str())) {
116 Tagged<Src>& s = gSrcs.push_back();
117 s.reset(src.detach());
118 s.tag = tag;
mtklein114c3cd2015-01-15 10:15:02 -0800119 }
mtklein748ca3b2015-01-15 10:56:12 -0800120}
mtklein114c3cd2015-01-15 10:15:02 -0800121
mtklein748ca3b2015-01-15 10:56:12 -0800122static void gather_srcs() {
123 for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) {
124 push_src("gm", new GMSrc(r->factory()));
125 }
halcanaryfc37ad12015-01-30 07:31:19 -0800126 for (int i = 0; i < FLAGS_skps.count(); i++) {
127 const char* path = FLAGS_skps[i];
128 if (sk_isdir(path)) {
129 SkOSFile::Iter it(path, "skp");
130 for (SkString file; it.next(&file); ) {
131 push_src("skp", new SKPSrc(SkOSPath::Join(path, file.c_str())));
132 }
133 } else {
mtklein8d17a132015-01-30 11:42:31 -0800134 push_src("skp", new SKPSrc(path));
mtklein114c3cd2015-01-15 10:15:02 -0800135 }
136 }
halcanary23b03c32015-01-30 09:58:58 -0800137 static const char* const exts[] = {
138 "bmp", "gif", "jpg", "jpeg", "png", "webp", "ktx", "astc", "wbmp", "ico",
139 "BMP", "GIF", "JPG", "JPEG", "PNG", "WEBP", "KTX", "ASTC", "WBMP", "ICO",
140 };
141 for (int i = 0; i < FLAGS_images.count(); i++) {
142 const char* flag = FLAGS_images[i];
143 if (sk_isdir(flag)) {
144 for (size_t j = 0; j < SK_ARRAY_COUNT(exts); j++) {
145 SkOSFile::Iter it(flag, exts[j]);
146 for (SkString file; it.next(&file); ) {
147 SkString path = SkOSPath::Join(flag, file.c_str());
148 push_src("image", new ImageSrc(path)); // Decode entire image.
mtkleinedc93bc2015-01-30 13:22:23 -0800149 push_src("subset", new ImageSrc(path, 2)); // Decode into 2 x 2 subsets
halcanary23b03c32015-01-30 09:58:58 -0800150 }
mtklein114c3cd2015-01-15 10:15:02 -0800151 }
halcanary23b03c32015-01-30 09:58:58 -0800152 } else if (sk_exists(flag)) {
153 // assume that FLAGS_images[i] is a valid image if it is a file.
mtklein8d17a132015-01-30 11:42:31 -0800154 push_src("image", new ImageSrc(flag)); // Decode entire image.
mtkleinedc93bc2015-01-30 13:22:23 -0800155 push_src("subset", new ImageSrc(flag, 2)); // Decode into 2 x 2 subsets
mtklein709d2c32015-01-15 08:30:25 -0800156 }
mtklein709d2c32015-01-15 08:30:25 -0800157 }
158}
159
mtklein748ca3b2015-01-15 10:56:12 -0800160static GrGLStandard get_gpu_api() {
161 if (FLAGS_gpuAPI.contains("gl")) { return kGL_GrGLStandard; }
162 if (FLAGS_gpuAPI.contains("gles")) { return kGLES_GrGLStandard; }
163 return kNone_GrGLStandard;
mtklein709d2c32015-01-15 08:30:25 -0800164}
165
mtklein748ca3b2015-01-15 10:56:12 -0800166static void push_sink(const char* tag, Sink* s) {
167 SkAutoTDelete<Sink> sink(s);
168 if (!FLAGS_config.contains(tag)) {
169 return;
mtklein114c3cd2015-01-15 10:15:02 -0800170 }
mtklein748ca3b2015-01-15 10:56:12 -0800171 // Try a noop Src as a canary. If it fails, skip this sink.
172 struct : public Src {
173 Error draw(SkCanvas*) const SK_OVERRIDE { return ""; }
174 SkISize size() const SK_OVERRIDE { return SkISize::Make(16, 16); }
175 Name name() const SK_OVERRIDE { return "noop"; }
176 } noop;
mtklein114c3cd2015-01-15 10:15:02 -0800177
mtklein748ca3b2015-01-15 10:56:12 -0800178 SkBitmap bitmap;
179 SkDynamicMemoryWStream stream;
180 Error err = sink->draw(noop, &bitmap, &stream);
181 if (!err.isEmpty()) {
182 SkDebugf("Skipping %s: %s\n", tag, err.c_str());
mtklein114c3cd2015-01-15 10:15:02 -0800183 return;
184 }
185
mtklein748ca3b2015-01-15 10:56:12 -0800186 Tagged<Sink>& ts = gSinks.push_back();
187 ts.reset(sink.detach());
188 ts.tag = tag;
189}
190
191static bool gpu_supported() {
192#if SK_SUPPORT_GPU
193 return FLAGS_gpu;
194#else
195 return false;
196#endif
197}
198
199static Sink* create_sink(const char* tag) {
200#define SINK(t, sink, ...) if (0 == strcmp(t, tag)) { return new sink(__VA_ARGS__); }
201 if (gpu_supported()) {
mtklein82d28432015-01-15 12:46:02 -0800202 typedef GrContextFactory Gr;
mtklein748ca3b2015-01-15 10:56:12 -0800203 const GrGLStandard api = get_gpu_api();
mtklein82d28432015-01-15 12:46:02 -0800204 SINK("gpunull", GPUSink, Gr::kNull_GLContextType, api, 0, false, FLAGS_gpu_threading);
205 SINK("gpudebug", GPUSink, Gr::kDebug_GLContextType, api, 0, false, FLAGS_gpu_threading);
206 SINK("gpu", GPUSink, Gr::kNative_GLContextType, api, 0, false, FLAGS_gpu_threading);
207 SINK("gpudft", GPUSink, Gr::kNative_GLContextType, api, 0, true, FLAGS_gpu_threading);
208 SINK("msaa4", GPUSink, Gr::kNative_GLContextType, api, 4, false, FLAGS_gpu_threading);
209 SINK("msaa16", GPUSink, Gr::kNative_GLContextType, api, 16, false, FLAGS_gpu_threading);
210 SINK("nvprmsaa4", GPUSink, Gr::kNVPR_GLContextType, api, 4, false, FLAGS_gpu_threading);
211 SINK("nvprmsaa16", GPUSink, Gr::kNVPR_GLContextType, api, 16, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800212 #if SK_ANGLE
mtklein82d28432015-01-15 12:46:02 -0800213 SINK("angle", GPUSink, Gr::kANGLE_GLContextType, api, 0, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800214 #endif
215 #if SK_MESA
mtklein82d28432015-01-15 12:46:02 -0800216 SINK("mesa", GPUSink, Gr::kMESA_GLContextType, api, 0, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800217 #endif
mtklein114c3cd2015-01-15 10:15:02 -0800218 }
mtklein748ca3b2015-01-15 10:56:12 -0800219
220 if (FLAGS_cpu) {
221 SINK("565", RasterSink, kRGB_565_SkColorType);
222 SINK("8888", RasterSink, kN32_SkColorType);
mtklein19f30602015-01-20 13:34:39 -0800223 SINK("pdf", PDFSink);
mtklein9c3f17d2015-01-28 11:35:18 -0800224 SINK("skp", SKPSink);
mtklein8a4527e2015-01-31 20:00:58 -0800225 SINK("svg", SVGSink);
226 SINK("null", NullSink);
mtklein748ca3b2015-01-15 10:56:12 -0800227 }
228#undef SINK
229 return NULL;
mtklein114c3cd2015-01-15 10:15:02 -0800230}
231
mtklein748ca3b2015-01-15 10:56:12 -0800232static Sink* create_via(const char* tag, Sink* wrapped) {
233#define VIA(t, via, ...) if (0 == strcmp(t, tag)) { return new via(__VA_ARGS__); }
mtklein7edca212015-01-21 13:18:51 -0800234 VIA("pipe", ViaPipe, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800235 VIA("serialize", ViaSerialization, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800236 VIA("tiles", ViaTiles, 256, 256, NULL, wrapped);
237 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800238
239 if (FLAGS_matrix.count() == 9) {
240 SkMatrix m;
241 for (int i = 0; i < 9; i++) {
242 m[i] = (SkScalar)atof(FLAGS_matrix[i]);
243 }
244 VIA("matrix", ViaMatrix, m, wrapped);
245 }
246#undef VIA
247 return NULL;
mtklein114c3cd2015-01-15 10:15:02 -0800248}
249
mtklein748ca3b2015-01-15 10:56:12 -0800250static void gather_sinks() {
251 for (int i = 0; i < FLAGS_config.count(); i++) {
252 const char* config = FLAGS_config[i];
253 SkTArray<SkString> parts;
254 SkStrSplit(config, "-", &parts);
255
256 Sink* sink = NULL;
257 for (int i = parts.count(); i-- > 0;) {
258 const char* part = parts[i].c_str();
259 Sink* next = (sink == NULL) ? create_sink(part) : create_via(part, sink);
260 if (next == NULL) {
261 SkDebugf("Skipping %s: Don't understand '%s'.\n", config, part);
262 delete sink;
263 sink = NULL;
264 break;
265 }
266 sink = next;
267 }
268 if (sink) {
269 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800270 }
271 }
272}
mtklein709d2c32015-01-15 08:30:25 -0800273
mtkleina2ef6422015-01-15 13:44:22 -0800274static bool match(const char* needle, const char* haystack) {
275 return 0 == strcmp("_", needle) || NULL != strstr(haystack, needle);
276}
277
278static ImplicitString is_blacklisted(const char* sink, const char* src, const char* name) {
279 for (int i = 0; i < FLAGS_blacklist.count() - 2; i += 3) {
280 if (match(FLAGS_blacklist[i+0], sink) &&
281 match(FLAGS_blacklist[i+1], src) &&
282 match(FLAGS_blacklist[i+2], name)) {
283 return SkStringPrintf("%s %s %s",
284 FLAGS_blacklist[i+0], FLAGS_blacklist[i+1], FLAGS_blacklist[i+2]);
285 }
286 }
287 return "";
288}
289
mtklein748ca3b2015-01-15 10:56:12 -0800290// The finest-grained unit of work we can run: draw a single Src into a single Sink,
291// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
292struct Task {
293 Task(const Tagged<Src>& src, const Tagged<Sink>& sink) : src(src), sink(sink) {}
294 const Tagged<Src>& src;
295 const Tagged<Sink>& sink;
296
297 static void Run(Task* task) {
mtkleina2ef6422015-01-15 13:44:22 -0800298 SkString name = task->src->name();
299 SkString whyBlacklisted = is_blacklisted(task->sink.tag, task->src.tag, name.c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800300 WallTimer timer;
301 timer.start();
mtkleina2ef6422015-01-15 13:44:22 -0800302 if (!FLAGS_dryRun && whyBlacklisted.isEmpty()) {
mtklein748ca3b2015-01-15 10:56:12 -0800303 SkBitmap bitmap;
304 SkDynamicMemoryWStream stream;
305 Error err = task->sink->draw(*task->src, &bitmap, &stream);
306 if (!err.isEmpty()) {
307 fail(SkStringPrintf("%s %s %s: %s",
308 task->sink.tag,
309 task->src.tag,
mtkleina2ef6422015-01-15 13:44:22 -0800310 name.c_str(),
mtklein748ca3b2015-01-15 10:56:12 -0800311 err.c_str()));
312 }
mtklein62bd1a62015-01-27 14:46:26 -0800313 SkAutoTDelete<SkStreamAsset> data(stream.detachAsStream());
314
315 SkString md5;
316 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
317 SkMD5 hash;
318 if (data->getLength()) {
319 hash.writeStream(data, data->getLength());
320 data->rewind();
321 } else {
322 hash.write(bitmap.getPixels(), bitmap.getSize());
323 }
324 SkMD5::Digest digest;
325 hash.finish(digest);
326 for (int i = 0; i < 16; i++) {
327 md5.appendf("%02x", digest.data[i]);
328 }
329 }
330
331 if (!FLAGS_readPath.isEmpty() &&
332 !gGold.find(Gold(task->sink.tag, task->src.tag, name, md5))) {
333 fail(SkStringPrintf("%s not found for %s %s %s in %s",
334 md5.c_str(),
335 task->sink.tag,
336 task->src.tag,
337 name.c_str(),
338 FLAGS_readPath[0]));
339 }
340
mtklein748ca3b2015-01-15 10:56:12 -0800341 if (!FLAGS_writePath.isEmpty()) {
342 const char* ext = task->sink->fileExtension();
mtklein62bd1a62015-01-27 14:46:26 -0800343 if (data->getLength()) {
344 WriteToDisk(*task, md5, ext, data, data->getLength(), NULL);
halcanary022afb82015-01-30 11:00:12 -0800345 SkASSERT(bitmap.drawsNothing());
346 } else if (!bitmap.drawsNothing()) {
mtklein62bd1a62015-01-27 14:46:26 -0800347 WriteToDisk(*task, md5, ext, NULL, 0, &bitmap);
mtklein748ca3b2015-01-15 10:56:12 -0800348 }
349 }
350 }
351 timer.end();
mtkleina2ef6422015-01-15 13:44:22 -0800352 if (!whyBlacklisted.isEmpty()) {
353 name.appendf(" (--blacklist, %s)", whyBlacklisted.c_str());
354 }
355 done(timer.fWall, task->sink.tag, task->src.tag, name);
mtklein748ca3b2015-01-15 10:56:12 -0800356 }
357
358 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -0800359 SkString md5,
360 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -0800361 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -0800362 const SkBitmap* bitmap) {
mtklein748ca3b2015-01-15 10:56:12 -0800363 JsonWriter::BitmapResult result;
364 result.name = task.src->name();
365 result.config = task.sink.tag;
366 result.sourceType = task.src.tag;
367 result.ext = ext;
mtklein62bd1a62015-01-27 14:46:26 -0800368 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -0800369 JsonWriter::AddBitmapResult(result);
370
371 const char* dir = FLAGS_writePath[0];
372 if (0 == strcmp(dir, "@")) { // Needed for iOS.
373 dir = FLAGS_resourcePath[0];
374 }
375 sk_mkdir(dir);
376
377 SkString path;
378 if (FLAGS_nameByHash) {
379 path = SkOSPath::Join(dir, result.md5.c_str());
380 path.append(".");
381 path.append(ext);
382 if (sk_exists(path.c_str())) {
383 return; // Content-addressed. If it exists already, we're done.
384 }
385 } else {
386 path = SkOSPath::Join(dir, task.sink.tag);
387 sk_mkdir(path.c_str());
388 path = SkOSPath::Join(path.c_str(), task.src.tag);
389 sk_mkdir(path.c_str());
390 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
391 path.append(".");
392 path.append(ext);
393 }
394
395 SkFILEWStream file(path.c_str());
396 if (!file.isValid()) {
397 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
398 return;
399 }
400
mtklein748ca3b2015-01-15 10:56:12 -0800401 if (bitmap) {
402 // We can't encode A8 bitmaps as PNGs. Convert them to 8888 first.
403 SkBitmap converted;
404 if (bitmap->info().colorType() == kAlpha_8_SkColorType) {
405 if (!bitmap->copyTo(&converted, kN32_SkColorType)) {
406 fail("Can't convert A8 to 8888.\n");
407 return;
408 }
409 bitmap = &converted;
410 }
411 if (!SkImageEncoder::EncodeStream(&file, *bitmap, SkImageEncoder::kPNG_Type, 100)) {
412 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
413 return;
414 }
415 } else {
416 if (!file.writeStream(data, len)) {
417 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
418 return;
419 }
420 }
421 }
422};
423
424// Run all tasks in the same enclave serially on the same thread.
425// They can't possibly run concurrently with each other.
426static void run_enclave(SkTArray<Task>* tasks) {
427 for (int i = 0; i < tasks->count(); i++) {
428 Task::Run(tasks->begin() + i);
429 }
430}
431
432/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
433
434// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
435
mtklein55e88b22015-01-21 15:50:13 -0800436static SkTDArray<skiatest::Test> gThreadedTests, gGPUTests;
mtklein748ca3b2015-01-15 10:56:12 -0800437
438static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -0800439 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -0800440 return;
441 }
halcanary87f3ba42015-01-20 09:30:20 -0800442 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r;
443 r = r->next()) {
444 // Despite its name, factory() is returning a reference to
445 // link-time static const POD data.
446 const skiatest::Test& test = r->factory();
447 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -0800448 continue;
449 }
halcanary87f3ba42015-01-20 09:30:20 -0800450 if (test.needsGpu && gpu_supported()) {
mtklein55e88b22015-01-21 15:50:13 -0800451 (FLAGS_gpu_threading ? gThreadedTests : gGPUTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -0800452 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein55e88b22015-01-21 15:50:13 -0800453 gThreadedTests.push(test);
mtklein82d28432015-01-15 12:46:02 -0800454 }
mtklein748ca3b2015-01-15 10:56:12 -0800455 }
456}
457
halcanary87f3ba42015-01-20 09:30:20 -0800458static void run_test(skiatest::Test* test) {
459 struct : public skiatest::Reporter {
460 void reportFailed(const skiatest::Failure& failure) SK_OVERRIDE {
461 fail(failure.toString());
462 JsonWriter::AddTestFailure(failure);
463 }
464 bool allowExtendedTest() const SK_OVERRIDE {
465 return FLAGS_pathOpsExtended;
466 }
467 bool verbose() const SK_OVERRIDE { return FLAGS_veryVerbose; }
468 } reporter;
mtklein748ca3b2015-01-15 10:56:12 -0800469 WallTimer timer;
470 timer.start();
mtklein748ca3b2015-01-15 10:56:12 -0800471 if (!FLAGS_dryRun) {
mtklein55e88b22015-01-21 15:50:13 -0800472 GrContextFactory factory;
473 test->proc(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -0800474 }
475 timer.end();
halcanary87f3ba42015-01-20 09:30:20 -0800476 done(timer.fWall, "unit", "test", test->name);
mtklein748ca3b2015-01-15 10:56:12 -0800477}
478
479/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
480
mtklein55e88b22015-01-21 15:50:13 -0800481// If we're isolating all GPU-bound work to one thread (the default), this function runs all that.
482static void run_enclave_and_gpu_tests(SkTArray<Task>* tasks) {
483 run_enclave(tasks);
484 for (int i = 0; i < gGPUTests.count(); i++) {
485 run_test(&gGPUTests[i]);
486 }
487}
488
jcgregorio3b27ade2014-11-13 08:06:40 -0800489int dm_main();
caryclark17f0b6d2014-07-22 10:15:34 -0700490int dm_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -0800491 SetupCrashHandler();
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +0000492 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -0700493 SkTaskGroup::Enabler enabled(FLAGS_threads);
mtkleine67164d2015-02-02 13:24:37 -0800494 if (FLAGS_leaks) {
495 SkInstCountPrintLeaksOnExit();
496 }
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +0000497
mtklein62bd1a62015-01-27 14:46:26 -0800498 gather_gold();
499
mtklein748ca3b2015-01-15 10:56:12 -0800500 gather_srcs();
501 gather_sinks();
502 gather_tests();
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +0000503
mtklein55e88b22015-01-21 15:50:13 -0800504 gPending = gSrcs.count() * gSinks.count() + gThreadedTests.count() + gGPUTests.count();
mtklein748ca3b2015-01-15 10:56:12 -0800505 SkDebugf("%d srcs * %d sinks + %d tests == %d tasks\n",
mtklein55e88b22015-01-21 15:50:13 -0800506 gSrcs.count(), gSinks.count(), gThreadedTests.count() + gGPUTests.count(), gPending);
mtklein748ca3b2015-01-15 10:56:12 -0800507
508 // We try to exploit as much parallelism as is safe. Most Src/Sink pairs run on any thread,
509 // but Sinks that identify as part of a particular enclave run serially on a single thread.
mtklein82d28432015-01-15 12:46:02 -0800510 // CPU tests run on any thread. GPU tests depend on --gpu_threading.
mtklein748ca3b2015-01-15 10:56:12 -0800511 SkTArray<Task> enclaves[kNumEnclaves];
512 for (int j = 0; j < gSinks.count(); j++) {
513 SkTArray<Task>& tasks = enclaves[gSinks[j]->enclave()];
514 for (int i = 0; i < gSrcs.count(); i++) {
515 tasks.push_back(Task(gSrcs[i], gSinks[j]));
516 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +0000517 }
518
mtklein748ca3b2015-01-15 10:56:12 -0800519 SkTaskGroup tg;
mtklein55e88b22015-01-21 15:50:13 -0800520 tg.batch(run_test, gThreadedTests.begin(), gThreadedTests.count());
521 for (int i = 0; i < kNumEnclaves; i++) {
522 switch(i) {
523 case kAnyThread_Enclave:
524 tg.batch(Task::Run, enclaves[i].begin(), enclaves[i].count());
525 break;
526 case kGPU_Enclave:
527 tg.add(run_enclave_and_gpu_tests, &enclaves[i]);
528 break;
529 default:
530 tg.add(run_enclave, &enclaves[i]);
531 break;
mtklein82d28432015-01-15 12:46:02 -0800532 }
mtklein55e88b22015-01-21 15:50:13 -0800533 }
mtklein748ca3b2015-01-15 10:56:12 -0800534 tg.wait();
mtklein748ca3b2015-01-15 10:56:12 -0800535 // At this point we're back in single-threaded land.
mtklein@google.comd36522d2013-10-16 13:02:15 +0000536
mtklein2f64eec2015-01-15 14:20:41 -0800537 SkDebugf("\n");
mtklein748ca3b2015-01-15 10:56:12 -0800538 if (gFailures.count() > 0) {
539 SkDebugf("Failures:\n");
540 for (int i = 0; i < gFailures.count(); i++) {
mtklein9dc09102015-01-15 15:47:33 -0800541 SkDebugf("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800542 }
543 SkDebugf("%d failures\n", gFailures.count());
544 return 1;
mtklein114c3cd2015-01-15 10:15:02 -0800545 }
mtklein748ca3b2015-01-15 10:56:12 -0800546 if (gPending > 0) {
547 SkDebugf("Hrm, we didn't seem to run everything we intended to! Please file a bug.\n");
548 return 1;
mtklein114c3cd2015-01-15 10:15:02 -0800549 }
mtklein748ca3b2015-01-15 10:56:12 -0800550 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +0000551}
jcgregorio3b27ade2014-11-13 08:06:40 -0800552
553#if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL)
554int main(int argc, char** argv) {
555 SkCommandLineFlags::Parse(argc, argv);
556 return dm_main();
557}
558#endif