blob: f087a327afd3951aa3c54f7576fa45bbdc63ee0d [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"
mtklein748ca3b2015-01-15 10:56:12 -080011#include "SkMD5.h"
mtklein1d0f1642014-09-08 08:05:18 -070012#include "SkOSFile.h"
mtklein62bd1a62015-01-27 14:46:26 -080013#include "SkTDynamicHash.h"
mtklein406654b2014-09-03 15:34:37 -070014#include "SkTaskGroup.h"
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +000015#include "Test.h"
mtklein748ca3b2015-01-15 10:56:12 -080016#include "Timer.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +000017
mtklein748ca3b2015-01-15 10:56:12 -080018DEFINE_string(images, "resources", "Images to decode.");
mtkleinedc93bc2015-01-30 13:22:23 -080019DEFINE_string(src, "tests gm skp image subset", "Source types to test.");
mtklein748ca3b2015-01-15 10:56:12 -080020DEFINE_bool(nameByHash, false,
21 "If true, write to FLAGS_writePath[0]/<hash>.png instead of "
22 "to FLAGS_writePath[0]/<config>/<sourceType>/<name>.png");
23DEFINE_bool2(pathOpsExtended, x, false, "Run extended pathOps tests.");
24DEFINE_string(matrix, "1 0 0 0 1 0 0 0 1",
25 "Matrix to apply when using 'matrix' in config.");
mtklein82d28432015-01-15 12:46:02 -080026DEFINE_bool(gpu_threading, false, "Allow GPU work to run on multiple threads?");
mtklein@google.comd36522d2013-10-16 13:02:15 +000027
mtkleina2ef6422015-01-15 13:44:22 -080028DEFINE_string(blacklist, "",
29 "Space-separated config/src/name triples to blacklist. '_' matches anything. E.g. \n"
30 "'--blacklist gpu skp _' will blacklist all SKPs drawn into the gpu config.\n"
31 "'--blacklist gpu skp _ 8888 gm aarects' will also blacklist the aarects GM on 8888.");
32
mtklein62bd1a62015-01-27 14:46:26 -080033DEFINE_string2(readPath, r, "", "If set check for equality with golden results in this directory.");
34
mtklein@google.comd36522d2013-10-16 13:02:15 +000035__SK_FORCE_IMAGE_DECODER_LINKING;
mtklein748ca3b2015-01-15 10:56:12 -080036using namespace DM;
mtklein@google.comd36522d2013-10-16 13:02:15 +000037
mtklein748ca3b2015-01-15 10:56:12 -080038/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
39
40SK_DECLARE_STATIC_MUTEX(gFailuresMutex);
41static SkTArray<SkString> gFailures;
42
43static void fail(ImplicitString err) {
44 SkAutoMutexAcquire lock(gFailuresMutex);
45 SkDebugf("\n\nFAILURE: %s\n\n", err.c_str());
46 gFailures.push_back(err);
halcanary9e398f72015-01-10 11:18:04 -080047}
48
mtklein748ca3b2015-01-15 10:56:12 -080049static int32_t gPending = 0; // Atomic.
50
51static void done(double ms, ImplicitString config, ImplicitString src, ImplicitString name) {
mtkleina17241b2015-01-23 05:48:00 -080052 int32_t pending = sk_atomic_dec(&gPending)-1;
mtklein748ca3b2015-01-15 10:56:12 -080053 SkDebugf("%s(%4dMB %5d) %s\t%s %s %s ", FLAGS_verbose ? "\n" : kSkOverwriteLine
54 , sk_tools::getMaxResidentSetSizeMB()
mtkleina17241b2015-01-23 05:48:00 -080055 , pending
mtklein748ca3b2015-01-15 10:56:12 -080056 , HumanizeMs(ms).c_str()
57 , config.c_str()
58 , src.c_str()
59 , name.c_str());
mtkleina17241b2015-01-23 05:48:00 -080060 // We write our dm.json file every once in a while in case we crash.
61 // Notice this also handles the final dm.json when pending == 0.
62 if (pending % 500 == 0) {
63 JsonWriter::DumpJson();
64 }
mtklein@google.comd36522d2013-10-16 13:02:15 +000065}
66
mtklein748ca3b2015-01-15 10:56:12 -080067/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
mtklein114c3cd2015-01-15 10:15:02 -080068
mtklein62bd1a62015-01-27 14:46:26 -080069struct Gold : public SkString {
70 Gold(ImplicitString sink, ImplicitString src, ImplicitString name, ImplicitString md5)
71 : SkString("") {
72 this->append(sink);
73 this->append(src);
74 this->append(name);
75 this->append(md5);
76 while (this->size() % 4) {
77 this->append("!"); // Pad out if needed so we can pass this to Murmur3.
78 }
79 }
80 static const Gold& GetKey(const Gold& g) { return g; }
81 static uint32_t Hash(const Gold& g) {
82 return SkChecksum::Murmur3((const uint32_t*)g.c_str(), g.size());
83 }
84};
85static SkTDynamicHash<Gold, Gold> gGold;
86
87static void add_gold(JsonWriter::BitmapResult r) {
88 gGold.add(new Gold(r.config, r.sourceType, r.name, r.md5)); // We'll let these leak. Lazybones.
89}
90
91static void gather_gold() {
92 if (!FLAGS_readPath.isEmpty()) {
93 SkString path(FLAGS_readPath[0]);
94 path.append("/dm.json");
95 if (!JsonWriter::ReadJson(path.c_str(), add_gold)) {
96 fail(SkStringPrintf("Couldn't read %s for golden results.", path.c_str()));
97 }
98 }
99}
100
101/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
102
mtklein748ca3b2015-01-15 10:56:12 -0800103template <typename T>
104struct Tagged : public SkAutoTDelete<T> { const char* tag; };
105
106static const bool kMemcpyOK = true;
107
108static SkTArray<Tagged<Src>, kMemcpyOK> gSrcs;
109static SkTArray<Tagged<Sink>, kMemcpyOK> gSinks;
110
111static void push_src(const char* tag, Src* s) {
112 SkAutoTDelete<Src> src(s);
113 if (FLAGS_src.contains(tag) &&
114 !SkCommandLineFlags::ShouldSkip(FLAGS_match, src->name().c_str())) {
115 Tagged<Src>& s = gSrcs.push_back();
116 s.reset(src.detach());
117 s.tag = tag;
mtklein114c3cd2015-01-15 10:15:02 -0800118 }
mtklein748ca3b2015-01-15 10:56:12 -0800119}
mtklein114c3cd2015-01-15 10:15:02 -0800120
mtklein748ca3b2015-01-15 10:56:12 -0800121static void gather_srcs() {
122 for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) {
123 push_src("gm", new GMSrc(r->factory()));
124 }
halcanaryfc37ad12015-01-30 07:31:19 -0800125 for (int i = 0; i < FLAGS_skps.count(); i++) {
126 const char* path = FLAGS_skps[i];
127 if (sk_isdir(path)) {
128 SkOSFile::Iter it(path, "skp");
129 for (SkString file; it.next(&file); ) {
130 push_src("skp", new SKPSrc(SkOSPath::Join(path, file.c_str())));
131 }
132 } else {
mtklein8d17a132015-01-30 11:42:31 -0800133 push_src("skp", new SKPSrc(path));
mtklein114c3cd2015-01-15 10:15:02 -0800134 }
135 }
halcanary23b03c32015-01-30 09:58:58 -0800136 static const char* const exts[] = {
137 "bmp", "gif", "jpg", "jpeg", "png", "webp", "ktx", "astc", "wbmp", "ico",
138 "BMP", "GIF", "JPG", "JPEG", "PNG", "WEBP", "KTX", "ASTC", "WBMP", "ICO",
139 };
140 for (int i = 0; i < FLAGS_images.count(); i++) {
141 const char* flag = FLAGS_images[i];
142 if (sk_isdir(flag)) {
143 for (size_t j = 0; j < SK_ARRAY_COUNT(exts); j++) {
144 SkOSFile::Iter it(flag, exts[j]);
145 for (SkString file; it.next(&file); ) {
146 SkString path = SkOSPath::Join(flag, file.c_str());
147 push_src("image", new ImageSrc(path)); // Decode entire image.
mtkleinedc93bc2015-01-30 13:22:23 -0800148 push_src("subset", new ImageSrc(path, 2)); // Decode into 2 x 2 subsets
halcanary23b03c32015-01-30 09:58:58 -0800149 }
mtklein114c3cd2015-01-15 10:15:02 -0800150 }
halcanary23b03c32015-01-30 09:58:58 -0800151 } else if (sk_exists(flag)) {
152 // assume that FLAGS_images[i] is a valid image if it is a file.
mtklein8d17a132015-01-30 11:42:31 -0800153 push_src("image", new ImageSrc(flag)); // Decode entire image.
mtkleinedc93bc2015-01-30 13:22:23 -0800154 push_src("subset", new ImageSrc(flag, 2)); // Decode into 2 x 2 subsets
mtklein709d2c32015-01-15 08:30:25 -0800155 }
mtklein709d2c32015-01-15 08:30:25 -0800156 }
157}
158
mtklein748ca3b2015-01-15 10:56:12 -0800159static GrGLStandard get_gpu_api() {
160 if (FLAGS_gpuAPI.contains("gl")) { return kGL_GrGLStandard; }
161 if (FLAGS_gpuAPI.contains("gles")) { return kGLES_GrGLStandard; }
162 return kNone_GrGLStandard;
mtklein709d2c32015-01-15 08:30:25 -0800163}
164
mtklein748ca3b2015-01-15 10:56:12 -0800165static void push_sink(const char* tag, Sink* s) {
166 SkAutoTDelete<Sink> sink(s);
167 if (!FLAGS_config.contains(tag)) {
168 return;
mtklein114c3cd2015-01-15 10:15:02 -0800169 }
mtklein748ca3b2015-01-15 10:56:12 -0800170 // Try a noop Src as a canary. If it fails, skip this sink.
171 struct : public Src {
172 Error draw(SkCanvas*) const SK_OVERRIDE { return ""; }
173 SkISize size() const SK_OVERRIDE { return SkISize::Make(16, 16); }
174 Name name() const SK_OVERRIDE { return "noop"; }
175 } noop;
mtklein114c3cd2015-01-15 10:15:02 -0800176
mtklein748ca3b2015-01-15 10:56:12 -0800177 SkBitmap bitmap;
178 SkDynamicMemoryWStream stream;
179 Error err = sink->draw(noop, &bitmap, &stream);
180 if (!err.isEmpty()) {
181 SkDebugf("Skipping %s: %s\n", tag, err.c_str());
mtklein114c3cd2015-01-15 10:15:02 -0800182 return;
183 }
184
mtklein748ca3b2015-01-15 10:56:12 -0800185 Tagged<Sink>& ts = gSinks.push_back();
186 ts.reset(sink.detach());
187 ts.tag = tag;
188}
189
190static bool gpu_supported() {
191#if SK_SUPPORT_GPU
192 return FLAGS_gpu;
193#else
194 return false;
195#endif
196}
197
198static Sink* create_sink(const char* tag) {
199#define SINK(t, sink, ...) if (0 == strcmp(t, tag)) { return new sink(__VA_ARGS__); }
200 if (gpu_supported()) {
mtklein82d28432015-01-15 12:46:02 -0800201 typedef GrContextFactory Gr;
mtklein748ca3b2015-01-15 10:56:12 -0800202 const GrGLStandard api = get_gpu_api();
mtklein82d28432015-01-15 12:46:02 -0800203 SINK("gpunull", GPUSink, Gr::kNull_GLContextType, api, 0, false, FLAGS_gpu_threading);
204 SINK("gpudebug", GPUSink, Gr::kDebug_GLContextType, api, 0, false, FLAGS_gpu_threading);
205 SINK("gpu", GPUSink, Gr::kNative_GLContextType, api, 0, false, FLAGS_gpu_threading);
206 SINK("gpudft", GPUSink, Gr::kNative_GLContextType, api, 0, true, FLAGS_gpu_threading);
207 SINK("msaa4", GPUSink, Gr::kNative_GLContextType, api, 4, false, FLAGS_gpu_threading);
208 SINK("msaa16", GPUSink, Gr::kNative_GLContextType, api, 16, false, FLAGS_gpu_threading);
209 SINK("nvprmsaa4", GPUSink, Gr::kNVPR_GLContextType, api, 4, false, FLAGS_gpu_threading);
210 SINK("nvprmsaa16", GPUSink, Gr::kNVPR_GLContextType, api, 16, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800211 #if SK_ANGLE
mtklein82d28432015-01-15 12:46:02 -0800212 SINK("angle", GPUSink, Gr::kANGLE_GLContextType, api, 0, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800213 #endif
214 #if SK_MESA
mtklein82d28432015-01-15 12:46:02 -0800215 SINK("mesa", GPUSink, Gr::kMESA_GLContextType, api, 0, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800216 #endif
mtklein114c3cd2015-01-15 10:15:02 -0800217 }
mtklein748ca3b2015-01-15 10:56:12 -0800218
219 if (FLAGS_cpu) {
220 SINK("565", RasterSink, kRGB_565_SkColorType);
221 SINK("8888", RasterSink, kN32_SkColorType);
mtklein19f30602015-01-20 13:34:39 -0800222 SINK("pdf", PDFSink);
mtklein9c3f17d2015-01-28 11:35:18 -0800223 SINK("skp", SKPSink);
halcanary022afb82015-01-30 11:00:12 -0800224 SINK("null", NullSink);
mtklein748ca3b2015-01-15 10:56:12 -0800225 }
226#undef SINK
227 return NULL;
mtklein114c3cd2015-01-15 10:15:02 -0800228}
229
mtklein748ca3b2015-01-15 10:56:12 -0800230static Sink* create_via(const char* tag, Sink* wrapped) {
231#define VIA(t, via, ...) if (0 == strcmp(t, tag)) { return new via(__VA_ARGS__); }
mtklein7edca212015-01-21 13:18:51 -0800232 VIA("pipe", ViaPipe, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800233 VIA("serialize", ViaSerialization, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800234 VIA("tiles", ViaTiles, 256, 256, NULL, wrapped);
235 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800236
237 if (FLAGS_matrix.count() == 9) {
238 SkMatrix m;
239 for (int i = 0; i < 9; i++) {
240 m[i] = (SkScalar)atof(FLAGS_matrix[i]);
241 }
242 VIA("matrix", ViaMatrix, m, wrapped);
243 }
244#undef VIA
245 return NULL;
mtklein114c3cd2015-01-15 10:15:02 -0800246}
247
mtklein748ca3b2015-01-15 10:56:12 -0800248static void gather_sinks() {
249 for (int i = 0; i < FLAGS_config.count(); i++) {
250 const char* config = FLAGS_config[i];
251 SkTArray<SkString> parts;
252 SkStrSplit(config, "-", &parts);
253
254 Sink* sink = NULL;
255 for (int i = parts.count(); i-- > 0;) {
256 const char* part = parts[i].c_str();
257 Sink* next = (sink == NULL) ? create_sink(part) : create_via(part, sink);
258 if (next == NULL) {
259 SkDebugf("Skipping %s: Don't understand '%s'.\n", config, part);
260 delete sink;
261 sink = NULL;
262 break;
263 }
264 sink = next;
265 }
266 if (sink) {
267 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800268 }
269 }
270}
mtklein709d2c32015-01-15 08:30:25 -0800271
mtkleina2ef6422015-01-15 13:44:22 -0800272static bool match(const char* needle, const char* haystack) {
273 return 0 == strcmp("_", needle) || NULL != strstr(haystack, needle);
274}
275
276static ImplicitString is_blacklisted(const char* sink, const char* src, const char* name) {
277 for (int i = 0; i < FLAGS_blacklist.count() - 2; i += 3) {
278 if (match(FLAGS_blacklist[i+0], sink) &&
279 match(FLAGS_blacklist[i+1], src) &&
280 match(FLAGS_blacklist[i+2], name)) {
281 return SkStringPrintf("%s %s %s",
282 FLAGS_blacklist[i+0], FLAGS_blacklist[i+1], FLAGS_blacklist[i+2]);
283 }
284 }
285 return "";
286}
287
mtklein748ca3b2015-01-15 10:56:12 -0800288// The finest-grained unit of work we can run: draw a single Src into a single Sink,
289// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
290struct Task {
291 Task(const Tagged<Src>& src, const Tagged<Sink>& sink) : src(src), sink(sink) {}
292 const Tagged<Src>& src;
293 const Tagged<Sink>& sink;
294
295 static void Run(Task* task) {
mtkleina2ef6422015-01-15 13:44:22 -0800296 SkString name = task->src->name();
297 SkString whyBlacklisted = is_blacklisted(task->sink.tag, task->src.tag, name.c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800298 WallTimer timer;
299 timer.start();
mtkleina2ef6422015-01-15 13:44:22 -0800300 if (!FLAGS_dryRun && whyBlacklisted.isEmpty()) {
mtklein748ca3b2015-01-15 10:56:12 -0800301 SkBitmap bitmap;
302 SkDynamicMemoryWStream stream;
303 Error err = task->sink->draw(*task->src, &bitmap, &stream);
304 if (!err.isEmpty()) {
305 fail(SkStringPrintf("%s %s %s: %s",
306 task->sink.tag,
307 task->src.tag,
mtkleina2ef6422015-01-15 13:44:22 -0800308 name.c_str(),
mtklein748ca3b2015-01-15 10:56:12 -0800309 err.c_str()));
310 }
mtklein62bd1a62015-01-27 14:46:26 -0800311 SkAutoTDelete<SkStreamAsset> data(stream.detachAsStream());
312
313 SkString md5;
314 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
315 SkMD5 hash;
316 if (data->getLength()) {
317 hash.writeStream(data, data->getLength());
318 data->rewind();
319 } else {
320 hash.write(bitmap.getPixels(), bitmap.getSize());
321 }
322 SkMD5::Digest digest;
323 hash.finish(digest);
324 for (int i = 0; i < 16; i++) {
325 md5.appendf("%02x", digest.data[i]);
326 }
327 }
328
329 if (!FLAGS_readPath.isEmpty() &&
330 !gGold.find(Gold(task->sink.tag, task->src.tag, name, md5))) {
331 fail(SkStringPrintf("%s not found for %s %s %s in %s",
332 md5.c_str(),
333 task->sink.tag,
334 task->src.tag,
335 name.c_str(),
336 FLAGS_readPath[0]));
337 }
338
mtklein748ca3b2015-01-15 10:56:12 -0800339 if (!FLAGS_writePath.isEmpty()) {
340 const char* ext = task->sink->fileExtension();
mtklein62bd1a62015-01-27 14:46:26 -0800341 if (data->getLength()) {
342 WriteToDisk(*task, md5, ext, data, data->getLength(), NULL);
halcanary022afb82015-01-30 11:00:12 -0800343 SkASSERT(bitmap.drawsNothing());
344 } else if (!bitmap.drawsNothing()) {
mtklein62bd1a62015-01-27 14:46:26 -0800345 WriteToDisk(*task, md5, ext, NULL, 0, &bitmap);
mtklein748ca3b2015-01-15 10:56:12 -0800346 }
347 }
348 }
349 timer.end();
mtkleina2ef6422015-01-15 13:44:22 -0800350 if (!whyBlacklisted.isEmpty()) {
351 name.appendf(" (--blacklist, %s)", whyBlacklisted.c_str());
352 }
353 done(timer.fWall, task->sink.tag, task->src.tag, name);
mtklein748ca3b2015-01-15 10:56:12 -0800354 }
355
356 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -0800357 SkString md5,
358 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -0800359 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -0800360 const SkBitmap* bitmap) {
mtklein748ca3b2015-01-15 10:56:12 -0800361 JsonWriter::BitmapResult result;
362 result.name = task.src->name();
363 result.config = task.sink.tag;
364 result.sourceType = task.src.tag;
365 result.ext = ext;
mtklein62bd1a62015-01-27 14:46:26 -0800366 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -0800367 JsonWriter::AddBitmapResult(result);
368
369 const char* dir = FLAGS_writePath[0];
370 if (0 == strcmp(dir, "@")) { // Needed for iOS.
371 dir = FLAGS_resourcePath[0];
372 }
373 sk_mkdir(dir);
374
375 SkString path;
376 if (FLAGS_nameByHash) {
377 path = SkOSPath::Join(dir, result.md5.c_str());
378 path.append(".");
379 path.append(ext);
380 if (sk_exists(path.c_str())) {
381 return; // Content-addressed. If it exists already, we're done.
382 }
383 } else {
384 path = SkOSPath::Join(dir, task.sink.tag);
385 sk_mkdir(path.c_str());
386 path = SkOSPath::Join(path.c_str(), task.src.tag);
387 sk_mkdir(path.c_str());
388 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
389 path.append(".");
390 path.append(ext);
391 }
392
393 SkFILEWStream file(path.c_str());
394 if (!file.isValid()) {
395 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
396 return;
397 }
398
mtklein748ca3b2015-01-15 10:56:12 -0800399 if (bitmap) {
400 // We can't encode A8 bitmaps as PNGs. Convert them to 8888 first.
401 SkBitmap converted;
402 if (bitmap->info().colorType() == kAlpha_8_SkColorType) {
403 if (!bitmap->copyTo(&converted, kN32_SkColorType)) {
404 fail("Can't convert A8 to 8888.\n");
405 return;
406 }
407 bitmap = &converted;
408 }
409 if (!SkImageEncoder::EncodeStream(&file, *bitmap, SkImageEncoder::kPNG_Type, 100)) {
410 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
411 return;
412 }
413 } else {
414 if (!file.writeStream(data, len)) {
415 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
416 return;
417 }
418 }
419 }
420};
421
422// Run all tasks in the same enclave serially on the same thread.
423// They can't possibly run concurrently with each other.
424static void run_enclave(SkTArray<Task>* tasks) {
425 for (int i = 0; i < tasks->count(); i++) {
426 Task::Run(tasks->begin() + i);
427 }
428}
429
430/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
431
432// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
433
mtklein55e88b22015-01-21 15:50:13 -0800434static SkTDArray<skiatest::Test> gThreadedTests, gGPUTests;
mtklein748ca3b2015-01-15 10:56:12 -0800435
436static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -0800437 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -0800438 return;
439 }
halcanary87f3ba42015-01-20 09:30:20 -0800440 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r;
441 r = r->next()) {
442 // Despite its name, factory() is returning a reference to
443 // link-time static const POD data.
444 const skiatest::Test& test = r->factory();
445 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -0800446 continue;
447 }
halcanary87f3ba42015-01-20 09:30:20 -0800448 if (test.needsGpu && gpu_supported()) {
mtklein55e88b22015-01-21 15:50:13 -0800449 (FLAGS_gpu_threading ? gThreadedTests : gGPUTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -0800450 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein55e88b22015-01-21 15:50:13 -0800451 gThreadedTests.push(test);
mtklein82d28432015-01-15 12:46:02 -0800452 }
mtklein748ca3b2015-01-15 10:56:12 -0800453 }
454}
455
halcanary87f3ba42015-01-20 09:30:20 -0800456static void run_test(skiatest::Test* test) {
457 struct : public skiatest::Reporter {
458 void reportFailed(const skiatest::Failure& failure) SK_OVERRIDE {
459 fail(failure.toString());
460 JsonWriter::AddTestFailure(failure);
461 }
462 bool allowExtendedTest() const SK_OVERRIDE {
463 return FLAGS_pathOpsExtended;
464 }
465 bool verbose() const SK_OVERRIDE { return FLAGS_veryVerbose; }
466 } reporter;
mtklein748ca3b2015-01-15 10:56:12 -0800467 WallTimer timer;
468 timer.start();
mtklein748ca3b2015-01-15 10:56:12 -0800469 if (!FLAGS_dryRun) {
mtklein55e88b22015-01-21 15:50:13 -0800470 GrContextFactory factory;
471 test->proc(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -0800472 }
473 timer.end();
halcanary87f3ba42015-01-20 09:30:20 -0800474 done(timer.fWall, "unit", "test", test->name);
mtklein748ca3b2015-01-15 10:56:12 -0800475}
476
477/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
478
mtklein55e88b22015-01-21 15:50:13 -0800479// If we're isolating all GPU-bound work to one thread (the default), this function runs all that.
480static void run_enclave_and_gpu_tests(SkTArray<Task>* tasks) {
481 run_enclave(tasks);
482 for (int i = 0; i < gGPUTests.count(); i++) {
483 run_test(&gGPUTests[i]);
484 }
485}
486
jcgregorio3b27ade2014-11-13 08:06:40 -0800487int dm_main();
caryclark17f0b6d2014-07-22 10:15:34 -0700488int dm_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -0800489 SetupCrashHandler();
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +0000490 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -0700491 SkTaskGroup::Enabler enabled(FLAGS_threads);
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +0000492
mtklein62bd1a62015-01-27 14:46:26 -0800493 gather_gold();
494
mtklein748ca3b2015-01-15 10:56:12 -0800495 gather_srcs();
496 gather_sinks();
497 gather_tests();
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +0000498
mtklein55e88b22015-01-21 15:50:13 -0800499 gPending = gSrcs.count() * gSinks.count() + gThreadedTests.count() + gGPUTests.count();
mtklein748ca3b2015-01-15 10:56:12 -0800500 SkDebugf("%d srcs * %d sinks + %d tests == %d tasks\n",
mtklein55e88b22015-01-21 15:50:13 -0800501 gSrcs.count(), gSinks.count(), gThreadedTests.count() + gGPUTests.count(), gPending);
mtklein748ca3b2015-01-15 10:56:12 -0800502
503 // We try to exploit as much parallelism as is safe. Most Src/Sink pairs run on any thread,
504 // but Sinks that identify as part of a particular enclave run serially on a single thread.
mtklein82d28432015-01-15 12:46:02 -0800505 // CPU tests run on any thread. GPU tests depend on --gpu_threading.
mtklein748ca3b2015-01-15 10:56:12 -0800506 SkTArray<Task> enclaves[kNumEnclaves];
507 for (int j = 0; j < gSinks.count(); j++) {
508 SkTArray<Task>& tasks = enclaves[gSinks[j]->enclave()];
509 for (int i = 0; i < gSrcs.count(); i++) {
510 tasks.push_back(Task(gSrcs[i], gSinks[j]));
511 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +0000512 }
513
mtklein748ca3b2015-01-15 10:56:12 -0800514 SkTaskGroup tg;
mtklein55e88b22015-01-21 15:50:13 -0800515 tg.batch(run_test, gThreadedTests.begin(), gThreadedTests.count());
516 for (int i = 0; i < kNumEnclaves; i++) {
517 switch(i) {
518 case kAnyThread_Enclave:
519 tg.batch(Task::Run, enclaves[i].begin(), enclaves[i].count());
520 break;
521 case kGPU_Enclave:
522 tg.add(run_enclave_and_gpu_tests, &enclaves[i]);
523 break;
524 default:
525 tg.add(run_enclave, &enclaves[i]);
526 break;
mtklein82d28432015-01-15 12:46:02 -0800527 }
mtklein55e88b22015-01-21 15:50:13 -0800528 }
mtklein748ca3b2015-01-15 10:56:12 -0800529 tg.wait();
mtklein748ca3b2015-01-15 10:56:12 -0800530 // At this point we're back in single-threaded land.
mtklein@google.comd36522d2013-10-16 13:02:15 +0000531
mtklein2f64eec2015-01-15 14:20:41 -0800532 SkDebugf("\n");
mtklein748ca3b2015-01-15 10:56:12 -0800533 if (gFailures.count() > 0) {
534 SkDebugf("Failures:\n");
535 for (int i = 0; i < gFailures.count(); i++) {
mtklein9dc09102015-01-15 15:47:33 -0800536 SkDebugf("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800537 }
538 SkDebugf("%d failures\n", gFailures.count());
539 return 1;
mtklein114c3cd2015-01-15 10:15:02 -0800540 }
mtklein748ca3b2015-01-15 10:56:12 -0800541 if (gPending > 0) {
542 SkDebugf("Hrm, we didn't seem to run everything we intended to! Please file a bug.\n");
543 return 1;
mtklein114c3cd2015-01-15 10:15:02 -0800544 }
mtklein748ca3b2015-01-15 10:56:12 -0800545 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +0000546}
jcgregorio3b27ade2014-11-13 08:06:40 -0800547
548#if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL)
549int main(int argc, char** argv) {
550 SkCommandLineFlags::Parse(argc, argv);
551 return dm_main();
552}
553#endif