blob: 1c6d3222c2c32d2f93e8bf86c9828ac55643cf2c [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"
tomhudsoneebc39a2015-02-23 12:18:05 -08004#include "DMSrcSinkAndroid.h"
mtklein748ca3b2015-01-15 10:56:12 -08005#include "OverwriteLine.h"
6#include "ProcStats.h"
7#include "SkBBHFactory.h"
mtklein62bd1a62015-01-27 14:46:26 -08008#include "SkChecksum.h"
caryclark17f0b6d2014-07-22 10:15:34 -07009#include "SkCommonFlags.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +000010#include "SkForceLinking.h"
11#include "SkGraphics.h"
mtkleine67164d2015-02-02 13:24:37 -080012#include "SkInstCnt.h"
mtklein748ca3b2015-01-15 10:56:12 -080013#include "SkMD5.h"
mtklein1d0f1642014-09-08 08:05:18 -070014#include "SkOSFile.h"
mtkleina82f5622015-02-20 12:30:19 -080015#include "SkTHash.h"
mtklein406654b2014-09-03 15:34:37 -070016#include "SkTaskGroup.h"
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +000017#include "Test.h"
mtklein748ca3b2015-01-15 10:56:12 -080018#include "Timer.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +000019
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.");
mtkleind603b222015-02-17 11:13:33 -080025DEFINE_string(matrix, "1 0 0 1",
26 "2x2 scale+skew matrix to apply or upright when using "
27 "'matrix' or 'upright' in config.");
mtklein82d28432015-01-15 12:46:02 -080028DEFINE_bool(gpu_threading, false, "Allow GPU work to run on multiple threads?");
mtklein@google.comd36522d2013-10-16 13:02:15 +000029
mtkleina2ef6422015-01-15 13:44:22 -080030DEFINE_string(blacklist, "",
31 "Space-separated config/src/name triples to blacklist. '_' matches anything. E.g. \n"
32 "'--blacklist gpu skp _' will blacklist all SKPs drawn into the gpu config.\n"
33 "'--blacklist gpu skp _ 8888 gm aarects' will also blacklist the aarects GM on 8888.");
34
mtklein62bd1a62015-01-27 14:46:26 -080035DEFINE_string2(readPath, r, "", "If set check for equality with golden results in this directory.");
36
mtklein@google.comd36522d2013-10-16 13:02:15 +000037__SK_FORCE_IMAGE_DECODER_LINKING;
mtklein748ca3b2015-01-15 10:56:12 -080038using namespace DM;
mtklein@google.comd36522d2013-10-16 13:02:15 +000039
mtklein748ca3b2015-01-15 10:56:12 -080040/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
41
42SK_DECLARE_STATIC_MUTEX(gFailuresMutex);
43static SkTArray<SkString> gFailures;
44
45static void fail(ImplicitString err) {
46 SkAutoMutexAcquire lock(gFailuresMutex);
47 SkDebugf("\n\nFAILURE: %s\n\n", err.c_str());
48 gFailures.push_back(err);
halcanary9e398f72015-01-10 11:18:04 -080049}
50
mtklein748ca3b2015-01-15 10:56:12 -080051static int32_t gPending = 0; // Atomic.
52
mtkleinb9eb4ac2015-02-02 18:26:03 -080053static void done(double ms,
54 ImplicitString config, ImplicitString src, ImplicitString name,
55 ImplicitString log) {
56 if (!log.isEmpty()) {
57 log.prepend("\n");
58 }
mtklein6dee2ad2015-02-05 09:53:44 -080059 auto pending = sk_atomic_dec(&gPending)-1;
mtkleinb9eb4ac2015-02-02 18:26:03 -080060 SkDebugf("%s(%4dMB %5d) %s\t%s %s %s%s", FLAGS_verbose ? "\n" : kSkOverwriteLine
mtklein748ca3b2015-01-15 10:56:12 -080061 , sk_tools::getMaxResidentSetSizeMB()
mtkleina17241b2015-01-23 05:48:00 -080062 , pending
mtklein748ca3b2015-01-15 10:56:12 -080063 , HumanizeMs(ms).c_str()
64 , config.c_str()
65 , src.c_str()
mtkleinb9eb4ac2015-02-02 18:26:03 -080066 , name.c_str()
67 , log.c_str());
mtkleina17241b2015-01-23 05:48:00 -080068 // We write our dm.json file every once in a while in case we crash.
69 // Notice this also handles the final dm.json when pending == 0.
70 if (pending % 500 == 0) {
71 JsonWriter::DumpJson();
72 }
mtklein@google.comd36522d2013-10-16 13:02:15 +000073}
74
mtklein748ca3b2015-01-15 10:56:12 -080075/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
mtklein114c3cd2015-01-15 10:15:02 -080076
mtklein62bd1a62015-01-27 14:46:26 -080077struct Gold : public SkString {
mtkleina82f5622015-02-20 12:30:19 -080078 Gold() : SkString("") {}
mtklein62bd1a62015-01-27 14:46:26 -080079 Gold(ImplicitString sink, ImplicitString src, ImplicitString name, ImplicitString md5)
80 : SkString("") {
81 this->append(sink);
82 this->append(src);
83 this->append(name);
84 this->append(md5);
85 while (this->size() % 4) {
86 this->append("!"); // Pad out if needed so we can pass this to Murmur3.
87 }
88 }
mtklein62bd1a62015-01-27 14:46:26 -080089 static uint32_t Hash(const Gold& g) {
90 return SkChecksum::Murmur3((const uint32_t*)g.c_str(), g.size());
91 }
92};
mtkleina82f5622015-02-20 12:30:19 -080093static SkTHashSet<Gold, Gold::Hash> gGold;
mtklein62bd1a62015-01-27 14:46:26 -080094
95static void add_gold(JsonWriter::BitmapResult r) {
mtkleina82f5622015-02-20 12:30:19 -080096 gGold.add(Gold(r.config, r.sourceType, r.name, r.md5));
mtklein62bd1a62015-01-27 14:46:26 -080097}
98
99static void gather_gold() {
100 if (!FLAGS_readPath.isEmpty()) {
101 SkString path(FLAGS_readPath[0]);
102 path.append("/dm.json");
103 if (!JsonWriter::ReadJson(path.c_str(), add_gold)) {
104 fail(SkStringPrintf("Couldn't read %s for golden results.", path.c_str()));
105 }
106 }
107}
108
109/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
110
mtklein748ca3b2015-01-15 10:56:12 -0800111template <typename T>
112struct Tagged : public SkAutoTDelete<T> { const char* tag; };
113
114static const bool kMemcpyOK = true;
115
116static SkTArray<Tagged<Src>, kMemcpyOK> gSrcs;
117static SkTArray<Tagged<Sink>, kMemcpyOK> gSinks;
118
119static void push_src(const char* tag, Src* s) {
120 SkAutoTDelete<Src> src(s);
121 if (FLAGS_src.contains(tag) &&
122 !SkCommandLineFlags::ShouldSkip(FLAGS_match, src->name().c_str())) {
123 Tagged<Src>& s = gSrcs.push_back();
124 s.reset(src.detach());
125 s.tag = tag;
mtklein114c3cd2015-01-15 10:15:02 -0800126 }
mtklein748ca3b2015-01-15 10:56:12 -0800127}
mtklein114c3cd2015-01-15 10:15:02 -0800128
mtklein748ca3b2015-01-15 10:56:12 -0800129static void gather_srcs() {
130 for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) {
131 push_src("gm", new GMSrc(r->factory()));
132 }
halcanaryfc37ad12015-01-30 07:31:19 -0800133 for (int i = 0; i < FLAGS_skps.count(); i++) {
134 const char* path = FLAGS_skps[i];
135 if (sk_isdir(path)) {
136 SkOSFile::Iter it(path, "skp");
137 for (SkString file; it.next(&file); ) {
138 push_src("skp", new SKPSrc(SkOSPath::Join(path, file.c_str())));
139 }
140 } else {
mtklein8d17a132015-01-30 11:42:31 -0800141 push_src("skp", new SKPSrc(path));
mtklein114c3cd2015-01-15 10:15:02 -0800142 }
143 }
halcanary23b03c32015-01-30 09:58:58 -0800144 static const char* const exts[] = {
145 "bmp", "gif", "jpg", "jpeg", "png", "webp", "ktx", "astc", "wbmp", "ico",
146 "BMP", "GIF", "JPG", "JPEG", "PNG", "WEBP", "KTX", "ASTC", "WBMP", "ICO",
147 };
148 for (int i = 0; i < FLAGS_images.count(); i++) {
149 const char* flag = FLAGS_images[i];
150 if (sk_isdir(flag)) {
151 for (size_t j = 0; j < SK_ARRAY_COUNT(exts); j++) {
152 SkOSFile::Iter it(flag, exts[j]);
153 for (SkString file; it.next(&file); ) {
154 SkString path = SkOSPath::Join(flag, file.c_str());
155 push_src("image", new ImageSrc(path)); // Decode entire image.
mtkleinedc93bc2015-01-30 13:22:23 -0800156 push_src("subset", new ImageSrc(path, 2)); // Decode into 2 x 2 subsets
halcanary23b03c32015-01-30 09:58:58 -0800157 }
mtklein114c3cd2015-01-15 10:15:02 -0800158 }
halcanary23b03c32015-01-30 09:58:58 -0800159 } else if (sk_exists(flag)) {
160 // assume that FLAGS_images[i] is a valid image if it is a file.
mtklein8d17a132015-01-30 11:42:31 -0800161 push_src("image", new ImageSrc(flag)); // Decode entire image.
mtkleinedc93bc2015-01-30 13:22:23 -0800162 push_src("subset", new ImageSrc(flag, 2)); // Decode into 2 x 2 subsets
mtklein709d2c32015-01-15 08:30:25 -0800163 }
mtklein709d2c32015-01-15 08:30:25 -0800164 }
165}
166
mtklein748ca3b2015-01-15 10:56:12 -0800167static GrGLStandard get_gpu_api() {
168 if (FLAGS_gpuAPI.contains("gl")) { return kGL_GrGLStandard; }
169 if (FLAGS_gpuAPI.contains("gles")) { return kGLES_GrGLStandard; }
170 return kNone_GrGLStandard;
mtklein709d2c32015-01-15 08:30:25 -0800171}
172
mtklein748ca3b2015-01-15 10:56:12 -0800173static void push_sink(const char* tag, Sink* s) {
174 SkAutoTDelete<Sink> sink(s);
175 if (!FLAGS_config.contains(tag)) {
176 return;
mtklein114c3cd2015-01-15 10:15:02 -0800177 }
mtklein748ca3b2015-01-15 10:56:12 -0800178 // Try a noop Src as a canary. If it fails, skip this sink.
179 struct : public Src {
180 Error draw(SkCanvas*) const SK_OVERRIDE { return ""; }
181 SkISize size() const SK_OVERRIDE { return SkISize::Make(16, 16); }
182 Name name() const SK_OVERRIDE { return "noop"; }
183 } noop;
mtklein114c3cd2015-01-15 10:15:02 -0800184
mtklein748ca3b2015-01-15 10:56:12 -0800185 SkBitmap bitmap;
186 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800187 SkString log;
188 Error err = sink->draw(noop, &bitmap, &stream, &log);
mtklein748ca3b2015-01-15 10:56:12 -0800189 if (!err.isEmpty()) {
190 SkDebugf("Skipping %s: %s\n", tag, err.c_str());
mtklein114c3cd2015-01-15 10:15:02 -0800191 return;
192 }
193
mtklein748ca3b2015-01-15 10:56:12 -0800194 Tagged<Sink>& ts = gSinks.push_back();
195 ts.reset(sink.detach());
196 ts.tag = tag;
197}
198
199static bool gpu_supported() {
200#if SK_SUPPORT_GPU
201 return FLAGS_gpu;
202#else
203 return false;
204#endif
205}
206
207static Sink* create_sink(const char* tag) {
208#define SINK(t, sink, ...) if (0 == strcmp(t, tag)) { return new sink(__VA_ARGS__); }
209 if (gpu_supported()) {
mtklein82d28432015-01-15 12:46:02 -0800210 typedef GrContextFactory Gr;
mtklein748ca3b2015-01-15 10:56:12 -0800211 const GrGLStandard api = get_gpu_api();
mtklein82d28432015-01-15 12:46:02 -0800212 SINK("gpunull", GPUSink, Gr::kNull_GLContextType, api, 0, false, FLAGS_gpu_threading);
213 SINK("gpudebug", GPUSink, Gr::kDebug_GLContextType, api, 0, false, FLAGS_gpu_threading);
214 SINK("gpu", GPUSink, Gr::kNative_GLContextType, api, 0, false, FLAGS_gpu_threading);
215 SINK("gpudft", GPUSink, Gr::kNative_GLContextType, api, 0, true, FLAGS_gpu_threading);
216 SINK("msaa4", GPUSink, Gr::kNative_GLContextType, api, 4, false, FLAGS_gpu_threading);
217 SINK("msaa16", GPUSink, Gr::kNative_GLContextType, api, 16, false, FLAGS_gpu_threading);
218 SINK("nvprmsaa4", GPUSink, Gr::kNVPR_GLContextType, api, 4, false, FLAGS_gpu_threading);
219 SINK("nvprmsaa16", GPUSink, Gr::kNVPR_GLContextType, api, 16, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800220 #if SK_ANGLE
mtklein82d28432015-01-15 12:46:02 -0800221 SINK("angle", GPUSink, Gr::kANGLE_GLContextType, api, 0, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800222 #endif
223 #if SK_MESA
mtklein82d28432015-01-15 12:46:02 -0800224 SINK("mesa", GPUSink, Gr::kMESA_GLContextType, api, 0, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800225 #endif
mtklein114c3cd2015-01-15 10:15:02 -0800226 }
mtklein748ca3b2015-01-15 10:56:12 -0800227
tomhudsoneebc39a2015-02-23 12:18:05 -0800228#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
229 SINK("hwui", HWUISink);
230#endif
231
mtklein748ca3b2015-01-15 10:56:12 -0800232 if (FLAGS_cpu) {
233 SINK("565", RasterSink, kRGB_565_SkColorType);
234 SINK("8888", RasterSink, kN32_SkColorType);
mtklein19f30602015-01-20 13:34:39 -0800235 SINK("pdf", PDFSink);
mtklein9c3f17d2015-01-28 11:35:18 -0800236 SINK("skp", SKPSink);
mtklein8a4527e2015-01-31 20:00:58 -0800237 SINK("svg", SVGSink);
238 SINK("null", NullSink);
mtklein748ca3b2015-01-15 10:56:12 -0800239 }
240#undef SINK
241 return NULL;
mtklein114c3cd2015-01-15 10:15:02 -0800242}
243
mtklein748ca3b2015-01-15 10:56:12 -0800244static Sink* create_via(const char* tag, Sink* wrapped) {
245#define VIA(t, via, ...) if (0 == strcmp(t, tag)) { return new via(__VA_ARGS__); }
mtklein7edca212015-01-21 13:18:51 -0800246 VIA("pipe", ViaPipe, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800247 VIA("serialize", ViaSerialization, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800248 VIA("tiles", ViaTiles, 256, 256, NULL, wrapped);
249 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800250
mtkleind603b222015-02-17 11:13:33 -0800251 if (FLAGS_matrix.count() == 4) {
mtklein748ca3b2015-01-15 10:56:12 -0800252 SkMatrix m;
mtkleind603b222015-02-17 11:13:33 -0800253 m.reset();
254 m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
255 m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
256 m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
257 m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
258 VIA("matrix", ViaMatrix, m, wrapped);
259 VIA("upright", ViaUpright, m, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800260 }
261#undef VIA
262 return NULL;
mtklein114c3cd2015-01-15 10:15:02 -0800263}
264
mtklein748ca3b2015-01-15 10:56:12 -0800265static void gather_sinks() {
266 for (int i = 0; i < FLAGS_config.count(); i++) {
267 const char* config = FLAGS_config[i];
268 SkTArray<SkString> parts;
269 SkStrSplit(config, "-", &parts);
270
271 Sink* sink = NULL;
272 for (int i = parts.count(); i-- > 0;) {
273 const char* part = parts[i].c_str();
274 Sink* next = (sink == NULL) ? create_sink(part) : create_via(part, sink);
275 if (next == NULL) {
276 SkDebugf("Skipping %s: Don't understand '%s'.\n", config, part);
277 delete sink;
278 sink = NULL;
279 break;
280 }
281 sink = next;
282 }
283 if (sink) {
284 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800285 }
286 }
287}
mtklein709d2c32015-01-15 08:30:25 -0800288
mtkleina2ef6422015-01-15 13:44:22 -0800289static bool match(const char* needle, const char* haystack) {
290 return 0 == strcmp("_", needle) || NULL != strstr(haystack, needle);
291}
292
293static ImplicitString is_blacklisted(const char* sink, const char* src, const char* name) {
294 for (int i = 0; i < FLAGS_blacklist.count() - 2; i += 3) {
295 if (match(FLAGS_blacklist[i+0], sink) &&
296 match(FLAGS_blacklist[i+1], src) &&
297 match(FLAGS_blacklist[i+2], name)) {
298 return SkStringPrintf("%s %s %s",
299 FLAGS_blacklist[i+0], FLAGS_blacklist[i+1], FLAGS_blacklist[i+2]);
300 }
301 }
302 return "";
303}
304
mtklein748ca3b2015-01-15 10:56:12 -0800305// The finest-grained unit of work we can run: draw a single Src into a single Sink,
306// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
307struct Task {
308 Task(const Tagged<Src>& src, const Tagged<Sink>& sink) : src(src), sink(sink) {}
309 const Tagged<Src>& src;
310 const Tagged<Sink>& sink;
311
312 static void Run(Task* task) {
mtkleina2ef6422015-01-15 13:44:22 -0800313 SkString name = task->src->name();
314 SkString whyBlacklisted = is_blacklisted(task->sink.tag, task->src.tag, name.c_str());
mtkleinb9eb4ac2015-02-02 18:26:03 -0800315 SkString log;
mtklein748ca3b2015-01-15 10:56:12 -0800316 WallTimer timer;
317 timer.start();
mtkleina2ef6422015-01-15 13:44:22 -0800318 if (!FLAGS_dryRun && whyBlacklisted.isEmpty()) {
mtklein748ca3b2015-01-15 10:56:12 -0800319 SkBitmap bitmap;
320 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800321 Error err = task->sink->draw(*task->src, &bitmap, &stream, &log);
mtklein748ca3b2015-01-15 10:56:12 -0800322 if (!err.isEmpty()) {
323 fail(SkStringPrintf("%s %s %s: %s",
324 task->sink.tag,
325 task->src.tag,
mtkleina2ef6422015-01-15 13:44:22 -0800326 name.c_str(),
mtklein748ca3b2015-01-15 10:56:12 -0800327 err.c_str()));
328 }
mtklein62bd1a62015-01-27 14:46:26 -0800329 SkAutoTDelete<SkStreamAsset> data(stream.detachAsStream());
330
331 SkString md5;
332 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
333 SkMD5 hash;
334 if (data->getLength()) {
335 hash.writeStream(data, data->getLength());
336 data->rewind();
337 } else {
338 hash.write(bitmap.getPixels(), bitmap.getSize());
339 }
340 SkMD5::Digest digest;
341 hash.finish(digest);
342 for (int i = 0; i < 16; i++) {
343 md5.appendf("%02x", digest.data[i]);
344 }
345 }
346
347 if (!FLAGS_readPath.isEmpty() &&
mtkleina82f5622015-02-20 12:30:19 -0800348 !gGold.contains(Gold(task->sink.tag, task->src.tag, name, md5))) {
mtklein62bd1a62015-01-27 14:46:26 -0800349 fail(SkStringPrintf("%s not found for %s %s %s in %s",
350 md5.c_str(),
351 task->sink.tag,
352 task->src.tag,
353 name.c_str(),
354 FLAGS_readPath[0]));
355 }
356
mtklein748ca3b2015-01-15 10:56:12 -0800357 if (!FLAGS_writePath.isEmpty()) {
358 const char* ext = task->sink->fileExtension();
mtklein62bd1a62015-01-27 14:46:26 -0800359 if (data->getLength()) {
360 WriteToDisk(*task, md5, ext, data, data->getLength(), NULL);
halcanary022afb82015-01-30 11:00:12 -0800361 SkASSERT(bitmap.drawsNothing());
362 } else if (!bitmap.drawsNothing()) {
mtklein62bd1a62015-01-27 14:46:26 -0800363 WriteToDisk(*task, md5, ext, NULL, 0, &bitmap);
mtklein748ca3b2015-01-15 10:56:12 -0800364 }
365 }
366 }
367 timer.end();
mtkleina2ef6422015-01-15 13:44:22 -0800368 if (!whyBlacklisted.isEmpty()) {
369 name.appendf(" (--blacklist, %s)", whyBlacklisted.c_str());
370 }
mtkleinb9eb4ac2015-02-02 18:26:03 -0800371 done(timer.fWall, task->sink.tag, task->src.tag, name, log);
mtklein748ca3b2015-01-15 10:56:12 -0800372 }
373
374 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -0800375 SkString md5,
376 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -0800377 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -0800378 const SkBitmap* bitmap) {
mtklein748ca3b2015-01-15 10:56:12 -0800379 JsonWriter::BitmapResult result;
380 result.name = task.src->name();
381 result.config = task.sink.tag;
382 result.sourceType = task.src.tag;
383 result.ext = ext;
mtklein62bd1a62015-01-27 14:46:26 -0800384 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -0800385 JsonWriter::AddBitmapResult(result);
386
387 const char* dir = FLAGS_writePath[0];
388 if (0 == strcmp(dir, "@")) { // Needed for iOS.
389 dir = FLAGS_resourcePath[0];
390 }
391 sk_mkdir(dir);
392
393 SkString path;
394 if (FLAGS_nameByHash) {
395 path = SkOSPath::Join(dir, result.md5.c_str());
396 path.append(".");
397 path.append(ext);
398 if (sk_exists(path.c_str())) {
399 return; // Content-addressed. If it exists already, we're done.
400 }
401 } else {
402 path = SkOSPath::Join(dir, task.sink.tag);
403 sk_mkdir(path.c_str());
404 path = SkOSPath::Join(path.c_str(), task.src.tag);
405 sk_mkdir(path.c_str());
406 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
407 path.append(".");
408 path.append(ext);
409 }
410
411 SkFILEWStream file(path.c_str());
412 if (!file.isValid()) {
413 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
414 return;
415 }
416
mtklein748ca3b2015-01-15 10:56:12 -0800417 if (bitmap) {
418 // We can't encode A8 bitmaps as PNGs. Convert them to 8888 first.
419 SkBitmap converted;
420 if (bitmap->info().colorType() == kAlpha_8_SkColorType) {
421 if (!bitmap->copyTo(&converted, kN32_SkColorType)) {
422 fail("Can't convert A8 to 8888.\n");
423 return;
424 }
425 bitmap = &converted;
426 }
427 if (!SkImageEncoder::EncodeStream(&file, *bitmap, SkImageEncoder::kPNG_Type, 100)) {
428 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
429 return;
430 }
431 } else {
432 if (!file.writeStream(data, len)) {
433 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
434 return;
435 }
436 }
437 }
438};
439
440// Run all tasks in the same enclave serially on the same thread.
441// They can't possibly run concurrently with each other.
442static void run_enclave(SkTArray<Task>* tasks) {
443 for (int i = 0; i < tasks->count(); i++) {
444 Task::Run(tasks->begin() + i);
445 }
446}
447
448/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
449
450// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
451
mtklein55e88b22015-01-21 15:50:13 -0800452static SkTDArray<skiatest::Test> gThreadedTests, gGPUTests;
mtklein748ca3b2015-01-15 10:56:12 -0800453
454static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -0800455 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -0800456 return;
457 }
halcanary87f3ba42015-01-20 09:30:20 -0800458 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r;
459 r = r->next()) {
460 // Despite its name, factory() is returning a reference to
461 // link-time static const POD data.
462 const skiatest::Test& test = r->factory();
463 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -0800464 continue;
465 }
halcanary87f3ba42015-01-20 09:30:20 -0800466 if (test.needsGpu && gpu_supported()) {
mtklein55e88b22015-01-21 15:50:13 -0800467 (FLAGS_gpu_threading ? gThreadedTests : gGPUTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -0800468 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein55e88b22015-01-21 15:50:13 -0800469 gThreadedTests.push(test);
mtklein82d28432015-01-15 12:46:02 -0800470 }
mtklein748ca3b2015-01-15 10:56:12 -0800471 }
472}
473
halcanary87f3ba42015-01-20 09:30:20 -0800474static void run_test(skiatest::Test* test) {
475 struct : public skiatest::Reporter {
476 void reportFailed(const skiatest::Failure& failure) SK_OVERRIDE {
477 fail(failure.toString());
478 JsonWriter::AddTestFailure(failure);
479 }
480 bool allowExtendedTest() const SK_OVERRIDE {
481 return FLAGS_pathOpsExtended;
482 }
483 bool verbose() const SK_OVERRIDE { return FLAGS_veryVerbose; }
484 } reporter;
mtklein748ca3b2015-01-15 10:56:12 -0800485 WallTimer timer;
486 timer.start();
mtklein748ca3b2015-01-15 10:56:12 -0800487 if (!FLAGS_dryRun) {
mtklein55e88b22015-01-21 15:50:13 -0800488 GrContextFactory factory;
489 test->proc(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -0800490 }
491 timer.end();
mtkleinb9eb4ac2015-02-02 18:26:03 -0800492 done(timer.fWall, "unit", "test", test->name, "");
mtklein748ca3b2015-01-15 10:56:12 -0800493}
494
495/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
496
mtklein55e88b22015-01-21 15:50:13 -0800497// If we're isolating all GPU-bound work to one thread (the default), this function runs all that.
498static void run_enclave_and_gpu_tests(SkTArray<Task>* tasks) {
499 run_enclave(tasks);
500 for (int i = 0; i < gGPUTests.count(); i++) {
501 run_test(&gGPUTests[i]);
502 }
503}
504
jcgregorio3b27ade2014-11-13 08:06:40 -0800505int dm_main();
caryclark17f0b6d2014-07-22 10:15:34 -0700506int dm_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -0800507 SetupCrashHandler();
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +0000508 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -0700509 SkTaskGroup::Enabler enabled(FLAGS_threads);
mtkleine67164d2015-02-02 13:24:37 -0800510 if (FLAGS_leaks) {
511 SkInstCountPrintLeaksOnExit();
512 }
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +0000513
mtklein62bd1a62015-01-27 14:46:26 -0800514 gather_gold();
515
mtklein748ca3b2015-01-15 10:56:12 -0800516 gather_srcs();
517 gather_sinks();
518 gather_tests();
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +0000519
mtklein55e88b22015-01-21 15:50:13 -0800520 gPending = gSrcs.count() * gSinks.count() + gThreadedTests.count() + gGPUTests.count();
mtklein748ca3b2015-01-15 10:56:12 -0800521 SkDebugf("%d srcs * %d sinks + %d tests == %d tasks\n",
mtklein55e88b22015-01-21 15:50:13 -0800522 gSrcs.count(), gSinks.count(), gThreadedTests.count() + gGPUTests.count(), gPending);
mtklein748ca3b2015-01-15 10:56:12 -0800523
524 // We try to exploit as much parallelism as is safe. Most Src/Sink pairs run on any thread,
525 // but Sinks that identify as part of a particular enclave run serially on a single thread.
mtklein82d28432015-01-15 12:46:02 -0800526 // CPU tests run on any thread. GPU tests depend on --gpu_threading.
mtklein748ca3b2015-01-15 10:56:12 -0800527 SkTArray<Task> enclaves[kNumEnclaves];
528 for (int j = 0; j < gSinks.count(); j++) {
529 SkTArray<Task>& tasks = enclaves[gSinks[j]->enclave()];
530 for (int i = 0; i < gSrcs.count(); i++) {
531 tasks.push_back(Task(gSrcs[i], gSinks[j]));
532 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +0000533 }
534
mtklein748ca3b2015-01-15 10:56:12 -0800535 SkTaskGroup tg;
mtklein55e88b22015-01-21 15:50:13 -0800536 tg.batch(run_test, gThreadedTests.begin(), gThreadedTests.count());
537 for (int i = 0; i < kNumEnclaves; i++) {
538 switch(i) {
539 case kAnyThread_Enclave:
540 tg.batch(Task::Run, enclaves[i].begin(), enclaves[i].count());
541 break;
542 case kGPU_Enclave:
543 tg.add(run_enclave_and_gpu_tests, &enclaves[i]);
544 break;
545 default:
546 tg.add(run_enclave, &enclaves[i]);
547 break;
mtklein82d28432015-01-15 12:46:02 -0800548 }
mtklein55e88b22015-01-21 15:50:13 -0800549 }
mtklein748ca3b2015-01-15 10:56:12 -0800550 tg.wait();
mtklein748ca3b2015-01-15 10:56:12 -0800551 // At this point we're back in single-threaded land.
mtklein@google.comd36522d2013-10-16 13:02:15 +0000552
mtklein2f64eec2015-01-15 14:20:41 -0800553 SkDebugf("\n");
mtklein748ca3b2015-01-15 10:56:12 -0800554 if (gFailures.count() > 0) {
555 SkDebugf("Failures:\n");
556 for (int i = 0; i < gFailures.count(); i++) {
mtklein9dc09102015-01-15 15:47:33 -0800557 SkDebugf("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800558 }
559 SkDebugf("%d failures\n", gFailures.count());
560 return 1;
mtklein114c3cd2015-01-15 10:15:02 -0800561 }
mtklein748ca3b2015-01-15 10:56:12 -0800562 if (gPending > 0) {
563 SkDebugf("Hrm, we didn't seem to run everything we intended to! Please file a bug.\n");
564 return 1;
mtklein114c3cd2015-01-15 10:15:02 -0800565 }
mtklein748ca3b2015-01-15 10:56:12 -0800566 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +0000567}
jcgregorio3b27ade2014-11-13 08:06:40 -0800568
569#if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL)
570int main(int argc, char** argv) {
571 SkCommandLineFlags::Parse(argc, argv);
572 return dm_main();
573}
574#endif