blob: b41a77f8ade05aa36a1220d455056f7cbc6759ca [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
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.");
mtkleind603b222015-02-17 11:13:33 -080024DEFINE_string(matrix, "1 0 0 1",
25 "2x2 scale+skew matrix to apply or upright when using "
26 "'matrix' or 'upright' 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
mtkleinb9eb4ac2015-02-02 18:26:03 -080052static void done(double ms,
53 ImplicitString config, ImplicitString src, ImplicitString name,
54 ImplicitString log) {
55 if (!log.isEmpty()) {
56 log.prepend("\n");
57 }
mtklein6dee2ad2015-02-05 09:53:44 -080058 auto pending = sk_atomic_dec(&gPending)-1;
mtkleinb9eb4ac2015-02-02 18:26:03 -080059 SkDebugf("%s(%4dMB %5d) %s\t%s %s %s%s", FLAGS_verbose ? "\n" : kSkOverwriteLine
mtklein748ca3b2015-01-15 10:56:12 -080060 , sk_tools::getMaxResidentSetSizeMB()
mtkleina17241b2015-01-23 05:48:00 -080061 , pending
mtklein748ca3b2015-01-15 10:56:12 -080062 , HumanizeMs(ms).c_str()
63 , config.c_str()
64 , src.c_str()
mtkleinb9eb4ac2015-02-02 18:26:03 -080065 , name.c_str()
66 , log.c_str());
mtkleina17241b2015-01-23 05:48:00 -080067 // We write our dm.json file every once in a while in case we crash.
68 // Notice this also handles the final dm.json when pending == 0.
69 if (pending % 500 == 0) {
70 JsonWriter::DumpJson();
71 }
mtklein@google.comd36522d2013-10-16 13:02:15 +000072}
73
mtklein748ca3b2015-01-15 10:56:12 -080074/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
mtklein114c3cd2015-01-15 10:15:02 -080075
mtklein62bd1a62015-01-27 14:46:26 -080076struct Gold : public SkString {
77 Gold(ImplicitString sink, ImplicitString src, ImplicitString name, ImplicitString md5)
78 : SkString("") {
79 this->append(sink);
80 this->append(src);
81 this->append(name);
82 this->append(md5);
83 while (this->size() % 4) {
84 this->append("!"); // Pad out if needed so we can pass this to Murmur3.
85 }
86 }
87 static const Gold& GetKey(const Gold& g) { return g; }
88 static uint32_t Hash(const Gold& g) {
89 return SkChecksum::Murmur3((const uint32_t*)g.c_str(), g.size());
90 }
91};
92static SkTDynamicHash<Gold, Gold> gGold;
93
94static void add_gold(JsonWriter::BitmapResult r) {
95 gGold.add(new Gold(r.config, r.sourceType, r.name, r.md5)); // We'll let these leak. Lazybones.
96}
97
98static void gather_gold() {
99 if (!FLAGS_readPath.isEmpty()) {
100 SkString path(FLAGS_readPath[0]);
101 path.append("/dm.json");
102 if (!JsonWriter::ReadJson(path.c_str(), add_gold)) {
103 fail(SkStringPrintf("Couldn't read %s for golden results.", path.c_str()));
104 }
105 }
106}
107
108/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
109
mtklein748ca3b2015-01-15 10:56:12 -0800110template <typename T>
111struct Tagged : public SkAutoTDelete<T> { const char* tag; };
112
113static const bool kMemcpyOK = true;
114
115static SkTArray<Tagged<Src>, kMemcpyOK> gSrcs;
116static SkTArray<Tagged<Sink>, kMemcpyOK> gSinks;
117
118static void push_src(const char* tag, Src* s) {
119 SkAutoTDelete<Src> src(s);
120 if (FLAGS_src.contains(tag) &&
121 !SkCommandLineFlags::ShouldSkip(FLAGS_match, src->name().c_str())) {
122 Tagged<Src>& s = gSrcs.push_back();
123 s.reset(src.detach());
124 s.tag = tag;
mtklein114c3cd2015-01-15 10:15:02 -0800125 }
mtklein748ca3b2015-01-15 10:56:12 -0800126}
mtklein114c3cd2015-01-15 10:15:02 -0800127
mtklein748ca3b2015-01-15 10:56:12 -0800128static void gather_srcs() {
129 for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) {
130 push_src("gm", new GMSrc(r->factory()));
131 }
halcanaryfc37ad12015-01-30 07:31:19 -0800132 for (int i = 0; i < FLAGS_skps.count(); i++) {
133 const char* path = FLAGS_skps[i];
134 if (sk_isdir(path)) {
135 SkOSFile::Iter it(path, "skp");
136 for (SkString file; it.next(&file); ) {
137 push_src("skp", new SKPSrc(SkOSPath::Join(path, file.c_str())));
138 }
139 } else {
mtklein8d17a132015-01-30 11:42:31 -0800140 push_src("skp", new SKPSrc(path));
mtklein114c3cd2015-01-15 10:15:02 -0800141 }
142 }
halcanary23b03c32015-01-30 09:58:58 -0800143 static const char* const exts[] = {
144 "bmp", "gif", "jpg", "jpeg", "png", "webp", "ktx", "astc", "wbmp", "ico",
145 "BMP", "GIF", "JPG", "JPEG", "PNG", "WEBP", "KTX", "ASTC", "WBMP", "ICO",
146 };
147 for (int i = 0; i < FLAGS_images.count(); i++) {
148 const char* flag = FLAGS_images[i];
149 if (sk_isdir(flag)) {
150 for (size_t j = 0; j < SK_ARRAY_COUNT(exts); j++) {
151 SkOSFile::Iter it(flag, exts[j]);
152 for (SkString file; it.next(&file); ) {
153 SkString path = SkOSPath::Join(flag, file.c_str());
154 push_src("image", new ImageSrc(path)); // Decode entire image.
mtkleinedc93bc2015-01-30 13:22:23 -0800155 push_src("subset", new ImageSrc(path, 2)); // Decode into 2 x 2 subsets
halcanary23b03c32015-01-30 09:58:58 -0800156 }
mtklein114c3cd2015-01-15 10:15:02 -0800157 }
halcanary23b03c32015-01-30 09:58:58 -0800158 } else if (sk_exists(flag)) {
159 // assume that FLAGS_images[i] is a valid image if it is a file.
mtklein8d17a132015-01-30 11:42:31 -0800160 push_src("image", new ImageSrc(flag)); // Decode entire image.
mtkleinedc93bc2015-01-30 13:22:23 -0800161 push_src("subset", new ImageSrc(flag, 2)); // Decode into 2 x 2 subsets
mtklein709d2c32015-01-15 08:30:25 -0800162 }
mtklein709d2c32015-01-15 08:30:25 -0800163 }
164}
165
mtklein748ca3b2015-01-15 10:56:12 -0800166static GrGLStandard get_gpu_api() {
167 if (FLAGS_gpuAPI.contains("gl")) { return kGL_GrGLStandard; }
168 if (FLAGS_gpuAPI.contains("gles")) { return kGLES_GrGLStandard; }
169 return kNone_GrGLStandard;
mtklein709d2c32015-01-15 08:30:25 -0800170}
171
mtklein748ca3b2015-01-15 10:56:12 -0800172static void push_sink(const char* tag, Sink* s) {
173 SkAutoTDelete<Sink> sink(s);
174 if (!FLAGS_config.contains(tag)) {
175 return;
mtklein114c3cd2015-01-15 10:15:02 -0800176 }
mtklein748ca3b2015-01-15 10:56:12 -0800177 // Try a noop Src as a canary. If it fails, skip this sink.
178 struct : public Src {
179 Error draw(SkCanvas*) const SK_OVERRIDE { return ""; }
180 SkISize size() const SK_OVERRIDE { return SkISize::Make(16, 16); }
181 Name name() const SK_OVERRIDE { return "noop"; }
182 } noop;
mtklein114c3cd2015-01-15 10:15:02 -0800183
mtklein748ca3b2015-01-15 10:56:12 -0800184 SkBitmap bitmap;
185 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800186 SkString log;
187 Error err = sink->draw(noop, &bitmap, &stream, &log);
mtklein748ca3b2015-01-15 10:56:12 -0800188 if (!err.isEmpty()) {
189 SkDebugf("Skipping %s: %s\n", tag, err.c_str());
mtklein114c3cd2015-01-15 10:15:02 -0800190 return;
191 }
192
mtklein748ca3b2015-01-15 10:56:12 -0800193 Tagged<Sink>& ts = gSinks.push_back();
194 ts.reset(sink.detach());
195 ts.tag = tag;
196}
197
198static bool gpu_supported() {
199#if SK_SUPPORT_GPU
200 return FLAGS_gpu;
201#else
202 return false;
203#endif
204}
205
206static Sink* create_sink(const char* tag) {
207#define SINK(t, sink, ...) if (0 == strcmp(t, tag)) { return new sink(__VA_ARGS__); }
208 if (gpu_supported()) {
mtklein82d28432015-01-15 12:46:02 -0800209 typedef GrContextFactory Gr;
mtklein748ca3b2015-01-15 10:56:12 -0800210 const GrGLStandard api = get_gpu_api();
mtklein82d28432015-01-15 12:46:02 -0800211 SINK("gpunull", GPUSink, Gr::kNull_GLContextType, api, 0, false, FLAGS_gpu_threading);
212 SINK("gpudebug", GPUSink, Gr::kDebug_GLContextType, api, 0, false, FLAGS_gpu_threading);
213 SINK("gpu", GPUSink, Gr::kNative_GLContextType, api, 0, false, FLAGS_gpu_threading);
214 SINK("gpudft", GPUSink, Gr::kNative_GLContextType, api, 0, true, FLAGS_gpu_threading);
215 SINK("msaa4", GPUSink, Gr::kNative_GLContextType, api, 4, false, FLAGS_gpu_threading);
216 SINK("msaa16", GPUSink, Gr::kNative_GLContextType, api, 16, false, FLAGS_gpu_threading);
217 SINK("nvprmsaa4", GPUSink, Gr::kNVPR_GLContextType, api, 4, false, FLAGS_gpu_threading);
218 SINK("nvprmsaa16", GPUSink, Gr::kNVPR_GLContextType, api, 16, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800219 #if SK_ANGLE
mtklein82d28432015-01-15 12:46:02 -0800220 SINK("angle", GPUSink, Gr::kANGLE_GLContextType, api, 0, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800221 #endif
222 #if SK_MESA
mtklein82d28432015-01-15 12:46:02 -0800223 SINK("mesa", GPUSink, Gr::kMESA_GLContextType, api, 0, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800224 #endif
mtklein114c3cd2015-01-15 10:15:02 -0800225 }
mtklein748ca3b2015-01-15 10:56:12 -0800226
227 if (FLAGS_cpu) {
228 SINK("565", RasterSink, kRGB_565_SkColorType);
229 SINK("8888", RasterSink, kN32_SkColorType);
mtklein19f30602015-01-20 13:34:39 -0800230 SINK("pdf", PDFSink);
mtklein9c3f17d2015-01-28 11:35:18 -0800231 SINK("skp", SKPSink);
mtklein8a4527e2015-01-31 20:00:58 -0800232 SINK("svg", SVGSink);
233 SINK("null", NullSink);
mtklein748ca3b2015-01-15 10:56:12 -0800234 }
235#undef SINK
236 return NULL;
mtklein114c3cd2015-01-15 10:15:02 -0800237}
238
mtklein748ca3b2015-01-15 10:56:12 -0800239static Sink* create_via(const char* tag, Sink* wrapped) {
240#define VIA(t, via, ...) if (0 == strcmp(t, tag)) { return new via(__VA_ARGS__); }
mtklein7edca212015-01-21 13:18:51 -0800241 VIA("pipe", ViaPipe, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800242 VIA("serialize", ViaSerialization, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800243 VIA("tiles", ViaTiles, 256, 256, NULL, wrapped);
244 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800245
mtkleind603b222015-02-17 11:13:33 -0800246 if (FLAGS_matrix.count() == 4) {
mtklein748ca3b2015-01-15 10:56:12 -0800247 SkMatrix m;
mtkleind603b222015-02-17 11:13:33 -0800248 m.reset();
249 m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
250 m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
251 m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
252 m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
253 VIA("matrix", ViaMatrix, m, wrapped);
254 VIA("upright", ViaUpright, m, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800255 }
256#undef VIA
257 return NULL;
mtklein114c3cd2015-01-15 10:15:02 -0800258}
259
mtklein748ca3b2015-01-15 10:56:12 -0800260static void gather_sinks() {
261 for (int i = 0; i < FLAGS_config.count(); i++) {
262 const char* config = FLAGS_config[i];
263 SkTArray<SkString> parts;
264 SkStrSplit(config, "-", &parts);
265
266 Sink* sink = NULL;
267 for (int i = parts.count(); i-- > 0;) {
268 const char* part = parts[i].c_str();
269 Sink* next = (sink == NULL) ? create_sink(part) : create_via(part, sink);
270 if (next == NULL) {
271 SkDebugf("Skipping %s: Don't understand '%s'.\n", config, part);
272 delete sink;
273 sink = NULL;
274 break;
275 }
276 sink = next;
277 }
278 if (sink) {
279 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800280 }
281 }
282}
mtklein709d2c32015-01-15 08:30:25 -0800283
mtkleina2ef6422015-01-15 13:44:22 -0800284static bool match(const char* needle, const char* haystack) {
285 return 0 == strcmp("_", needle) || NULL != strstr(haystack, needle);
286}
287
288static ImplicitString is_blacklisted(const char* sink, const char* src, const char* name) {
289 for (int i = 0; i < FLAGS_blacklist.count() - 2; i += 3) {
290 if (match(FLAGS_blacklist[i+0], sink) &&
291 match(FLAGS_blacklist[i+1], src) &&
292 match(FLAGS_blacklist[i+2], name)) {
293 return SkStringPrintf("%s %s %s",
294 FLAGS_blacklist[i+0], FLAGS_blacklist[i+1], FLAGS_blacklist[i+2]);
295 }
296 }
297 return "";
298}
299
mtklein748ca3b2015-01-15 10:56:12 -0800300// The finest-grained unit of work we can run: draw a single Src into a single Sink,
301// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
302struct Task {
303 Task(const Tagged<Src>& src, const Tagged<Sink>& sink) : src(src), sink(sink) {}
304 const Tagged<Src>& src;
305 const Tagged<Sink>& sink;
306
307 static void Run(Task* task) {
mtkleina2ef6422015-01-15 13:44:22 -0800308 SkString name = task->src->name();
309 SkString whyBlacklisted = is_blacklisted(task->sink.tag, task->src.tag, name.c_str());
mtkleinb9eb4ac2015-02-02 18:26:03 -0800310 SkString log;
mtklein748ca3b2015-01-15 10:56:12 -0800311 WallTimer timer;
312 timer.start();
mtkleina2ef6422015-01-15 13:44:22 -0800313 if (!FLAGS_dryRun && whyBlacklisted.isEmpty()) {
mtklein748ca3b2015-01-15 10:56:12 -0800314 SkBitmap bitmap;
315 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800316 Error err = task->sink->draw(*task->src, &bitmap, &stream, &log);
mtklein748ca3b2015-01-15 10:56:12 -0800317 if (!err.isEmpty()) {
318 fail(SkStringPrintf("%s %s %s: %s",
319 task->sink.tag,
320 task->src.tag,
mtkleina2ef6422015-01-15 13:44:22 -0800321 name.c_str(),
mtklein748ca3b2015-01-15 10:56:12 -0800322 err.c_str()));
323 }
mtklein62bd1a62015-01-27 14:46:26 -0800324 SkAutoTDelete<SkStreamAsset> data(stream.detachAsStream());
325
326 SkString md5;
327 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
328 SkMD5 hash;
329 if (data->getLength()) {
330 hash.writeStream(data, data->getLength());
331 data->rewind();
332 } else {
333 hash.write(bitmap.getPixels(), bitmap.getSize());
334 }
335 SkMD5::Digest digest;
336 hash.finish(digest);
337 for (int i = 0; i < 16; i++) {
338 md5.appendf("%02x", digest.data[i]);
339 }
340 }
341
342 if (!FLAGS_readPath.isEmpty() &&
343 !gGold.find(Gold(task->sink.tag, task->src.tag, name, md5))) {
344 fail(SkStringPrintf("%s not found for %s %s %s in %s",
345 md5.c_str(),
346 task->sink.tag,
347 task->src.tag,
348 name.c_str(),
349 FLAGS_readPath[0]));
350 }
351
mtklein748ca3b2015-01-15 10:56:12 -0800352 if (!FLAGS_writePath.isEmpty()) {
353 const char* ext = task->sink->fileExtension();
mtklein62bd1a62015-01-27 14:46:26 -0800354 if (data->getLength()) {
355 WriteToDisk(*task, md5, ext, data, data->getLength(), NULL);
halcanary022afb82015-01-30 11:00:12 -0800356 SkASSERT(bitmap.drawsNothing());
357 } else if (!bitmap.drawsNothing()) {
mtklein62bd1a62015-01-27 14:46:26 -0800358 WriteToDisk(*task, md5, ext, NULL, 0, &bitmap);
mtklein748ca3b2015-01-15 10:56:12 -0800359 }
360 }
361 }
362 timer.end();
mtkleina2ef6422015-01-15 13:44:22 -0800363 if (!whyBlacklisted.isEmpty()) {
364 name.appendf(" (--blacklist, %s)", whyBlacklisted.c_str());
365 }
mtkleinb9eb4ac2015-02-02 18:26:03 -0800366 done(timer.fWall, task->sink.tag, task->src.tag, name, log);
mtklein748ca3b2015-01-15 10:56:12 -0800367 }
368
369 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -0800370 SkString md5,
371 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -0800372 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -0800373 const SkBitmap* bitmap) {
mtklein748ca3b2015-01-15 10:56:12 -0800374 JsonWriter::BitmapResult result;
375 result.name = task.src->name();
376 result.config = task.sink.tag;
377 result.sourceType = task.src.tag;
378 result.ext = ext;
mtklein62bd1a62015-01-27 14:46:26 -0800379 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -0800380 JsonWriter::AddBitmapResult(result);
381
382 const char* dir = FLAGS_writePath[0];
383 if (0 == strcmp(dir, "@")) { // Needed for iOS.
384 dir = FLAGS_resourcePath[0];
385 }
386 sk_mkdir(dir);
387
388 SkString path;
389 if (FLAGS_nameByHash) {
390 path = SkOSPath::Join(dir, result.md5.c_str());
391 path.append(".");
392 path.append(ext);
393 if (sk_exists(path.c_str())) {
394 return; // Content-addressed. If it exists already, we're done.
395 }
396 } else {
397 path = SkOSPath::Join(dir, task.sink.tag);
398 sk_mkdir(path.c_str());
399 path = SkOSPath::Join(path.c_str(), task.src.tag);
400 sk_mkdir(path.c_str());
401 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
402 path.append(".");
403 path.append(ext);
404 }
405
406 SkFILEWStream file(path.c_str());
407 if (!file.isValid()) {
408 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
409 return;
410 }
411
mtklein748ca3b2015-01-15 10:56:12 -0800412 if (bitmap) {
413 // We can't encode A8 bitmaps as PNGs. Convert them to 8888 first.
414 SkBitmap converted;
415 if (bitmap->info().colorType() == kAlpha_8_SkColorType) {
416 if (!bitmap->copyTo(&converted, kN32_SkColorType)) {
417 fail("Can't convert A8 to 8888.\n");
418 return;
419 }
420 bitmap = &converted;
421 }
422 if (!SkImageEncoder::EncodeStream(&file, *bitmap, SkImageEncoder::kPNG_Type, 100)) {
423 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
424 return;
425 }
426 } else {
427 if (!file.writeStream(data, len)) {
428 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
429 return;
430 }
431 }
432 }
433};
434
435// Run all tasks in the same enclave serially on the same thread.
436// They can't possibly run concurrently with each other.
437static void run_enclave(SkTArray<Task>* tasks) {
438 for (int i = 0; i < tasks->count(); i++) {
439 Task::Run(tasks->begin() + i);
440 }
441}
442
443/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
444
445// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
446
mtklein55e88b22015-01-21 15:50:13 -0800447static SkTDArray<skiatest::Test> gThreadedTests, gGPUTests;
mtklein748ca3b2015-01-15 10:56:12 -0800448
449static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -0800450 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -0800451 return;
452 }
halcanary87f3ba42015-01-20 09:30:20 -0800453 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r;
454 r = r->next()) {
455 // Despite its name, factory() is returning a reference to
456 // link-time static const POD data.
457 const skiatest::Test& test = r->factory();
458 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -0800459 continue;
460 }
halcanary87f3ba42015-01-20 09:30:20 -0800461 if (test.needsGpu && gpu_supported()) {
mtklein55e88b22015-01-21 15:50:13 -0800462 (FLAGS_gpu_threading ? gThreadedTests : gGPUTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -0800463 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein55e88b22015-01-21 15:50:13 -0800464 gThreadedTests.push(test);
mtklein82d28432015-01-15 12:46:02 -0800465 }
mtklein748ca3b2015-01-15 10:56:12 -0800466 }
467}
468
halcanary87f3ba42015-01-20 09:30:20 -0800469static void run_test(skiatest::Test* test) {
470 struct : public skiatest::Reporter {
471 void reportFailed(const skiatest::Failure& failure) SK_OVERRIDE {
472 fail(failure.toString());
473 JsonWriter::AddTestFailure(failure);
474 }
475 bool allowExtendedTest() const SK_OVERRIDE {
476 return FLAGS_pathOpsExtended;
477 }
478 bool verbose() const SK_OVERRIDE { return FLAGS_veryVerbose; }
479 } reporter;
mtklein748ca3b2015-01-15 10:56:12 -0800480 WallTimer timer;
481 timer.start();
mtklein748ca3b2015-01-15 10:56:12 -0800482 if (!FLAGS_dryRun) {
mtklein55e88b22015-01-21 15:50:13 -0800483 GrContextFactory factory;
484 test->proc(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -0800485 }
486 timer.end();
mtkleinb9eb4ac2015-02-02 18:26:03 -0800487 done(timer.fWall, "unit", "test", test->name, "");
mtklein748ca3b2015-01-15 10:56:12 -0800488}
489
490/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
491
mtklein55e88b22015-01-21 15:50:13 -0800492// If we're isolating all GPU-bound work to one thread (the default), this function runs all that.
493static void run_enclave_and_gpu_tests(SkTArray<Task>* tasks) {
494 run_enclave(tasks);
495 for (int i = 0; i < gGPUTests.count(); i++) {
496 run_test(&gGPUTests[i]);
497 }
498}
499
jcgregorio3b27ade2014-11-13 08:06:40 -0800500int dm_main();
caryclark17f0b6d2014-07-22 10:15:34 -0700501int dm_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -0800502 SetupCrashHandler();
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +0000503 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -0700504 SkTaskGroup::Enabler enabled(FLAGS_threads);
mtkleine67164d2015-02-02 13:24:37 -0800505 if (FLAGS_leaks) {
506 SkInstCountPrintLeaksOnExit();
507 }
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +0000508
mtklein62bd1a62015-01-27 14:46:26 -0800509 gather_gold();
510
mtklein748ca3b2015-01-15 10:56:12 -0800511 gather_srcs();
512 gather_sinks();
513 gather_tests();
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +0000514
mtklein55e88b22015-01-21 15:50:13 -0800515 gPending = gSrcs.count() * gSinks.count() + gThreadedTests.count() + gGPUTests.count();
mtklein748ca3b2015-01-15 10:56:12 -0800516 SkDebugf("%d srcs * %d sinks + %d tests == %d tasks\n",
mtklein55e88b22015-01-21 15:50:13 -0800517 gSrcs.count(), gSinks.count(), gThreadedTests.count() + gGPUTests.count(), gPending);
mtklein748ca3b2015-01-15 10:56:12 -0800518
519 // We try to exploit as much parallelism as is safe. Most Src/Sink pairs run on any thread,
520 // but Sinks that identify as part of a particular enclave run serially on a single thread.
mtklein82d28432015-01-15 12:46:02 -0800521 // CPU tests run on any thread. GPU tests depend on --gpu_threading.
mtklein748ca3b2015-01-15 10:56:12 -0800522 SkTArray<Task> enclaves[kNumEnclaves];
523 for (int j = 0; j < gSinks.count(); j++) {
524 SkTArray<Task>& tasks = enclaves[gSinks[j]->enclave()];
525 for (int i = 0; i < gSrcs.count(); i++) {
526 tasks.push_back(Task(gSrcs[i], gSinks[j]));
527 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +0000528 }
529
mtklein748ca3b2015-01-15 10:56:12 -0800530 SkTaskGroup tg;
mtklein55e88b22015-01-21 15:50:13 -0800531 tg.batch(run_test, gThreadedTests.begin(), gThreadedTests.count());
532 for (int i = 0; i < kNumEnclaves; i++) {
533 switch(i) {
534 case kAnyThread_Enclave:
535 tg.batch(Task::Run, enclaves[i].begin(), enclaves[i].count());
536 break;
537 case kGPU_Enclave:
538 tg.add(run_enclave_and_gpu_tests, &enclaves[i]);
539 break;
540 default:
541 tg.add(run_enclave, &enclaves[i]);
542 break;
mtklein82d28432015-01-15 12:46:02 -0800543 }
mtklein55e88b22015-01-21 15:50:13 -0800544 }
mtklein748ca3b2015-01-15 10:56:12 -0800545 tg.wait();
mtklein748ca3b2015-01-15 10:56:12 -0800546 // At this point we're back in single-threaded land.
mtklein@google.comd36522d2013-10-16 13:02:15 +0000547
mtklein2f64eec2015-01-15 14:20:41 -0800548 SkDebugf("\n");
mtklein748ca3b2015-01-15 10:56:12 -0800549 if (gFailures.count() > 0) {
550 SkDebugf("Failures:\n");
551 for (int i = 0; i < gFailures.count(); i++) {
mtklein9dc09102015-01-15 15:47:33 -0800552 SkDebugf("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800553 }
554 SkDebugf("%d failures\n", gFailures.count());
555 return 1;
mtklein114c3cd2015-01-15 10:15:02 -0800556 }
mtklein748ca3b2015-01-15 10:56:12 -0800557 if (gPending > 0) {
558 SkDebugf("Hrm, we didn't seem to run everything we intended to! Please file a bug.\n");
559 return 1;
mtklein114c3cd2015-01-15 10:15:02 -0800560 }
mtklein748ca3b2015-01-15 10:56:12 -0800561 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +0000562}
jcgregorio3b27ade2014-11-13 08:06:40 -0800563
564#if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL)
565int main(int argc, char** argv) {
566 SkCommandLineFlags::Parse(argc, argv);
567 return dm_main();
568}
569#endif