blob: 24808cf33ada4ff3de8b32a2331885381fd9d189 [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);
halcanary47ef4d52015-03-03 09:13:09 -0800239 SINK("xps", XPSSink);
mtklein748ca3b2015-01-15 10:56:12 -0800240 }
241#undef SINK
242 return NULL;
mtklein114c3cd2015-01-15 10:15:02 -0800243}
244
mtklein748ca3b2015-01-15 10:56:12 -0800245static Sink* create_via(const char* tag, Sink* wrapped) {
246#define VIA(t, via, ...) if (0 == strcmp(t, tag)) { return new via(__VA_ARGS__); }
mtklein7edca212015-01-21 13:18:51 -0800247 VIA("pipe", ViaPipe, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800248 VIA("serialize", ViaSerialization, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800249 VIA("tiles", ViaTiles, 256, 256, NULL, wrapped);
250 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800251
mtkleind603b222015-02-17 11:13:33 -0800252 if (FLAGS_matrix.count() == 4) {
mtklein748ca3b2015-01-15 10:56:12 -0800253 SkMatrix m;
mtkleind603b222015-02-17 11:13:33 -0800254 m.reset();
255 m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
256 m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
257 m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
258 m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
259 VIA("matrix", ViaMatrix, m, wrapped);
260 VIA("upright", ViaUpright, m, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800261 }
262#undef VIA
263 return NULL;
mtklein114c3cd2015-01-15 10:15:02 -0800264}
265
mtklein748ca3b2015-01-15 10:56:12 -0800266static void gather_sinks() {
267 for (int i = 0; i < FLAGS_config.count(); i++) {
268 const char* config = FLAGS_config[i];
269 SkTArray<SkString> parts;
270 SkStrSplit(config, "-", &parts);
271
272 Sink* sink = NULL;
273 for (int i = parts.count(); i-- > 0;) {
274 const char* part = parts[i].c_str();
275 Sink* next = (sink == NULL) ? create_sink(part) : create_via(part, sink);
276 if (next == NULL) {
277 SkDebugf("Skipping %s: Don't understand '%s'.\n", config, part);
278 delete sink;
279 sink = NULL;
280 break;
281 }
282 sink = next;
283 }
284 if (sink) {
285 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800286 }
287 }
288}
mtklein709d2c32015-01-15 08:30:25 -0800289
mtkleina2ef6422015-01-15 13:44:22 -0800290static bool match(const char* needle, const char* haystack) {
291 return 0 == strcmp("_", needle) || NULL != strstr(haystack, needle);
292}
293
294static ImplicitString is_blacklisted(const char* sink, const char* src, const char* name) {
295 for (int i = 0; i < FLAGS_blacklist.count() - 2; i += 3) {
296 if (match(FLAGS_blacklist[i+0], sink) &&
297 match(FLAGS_blacklist[i+1], src) &&
298 match(FLAGS_blacklist[i+2], name)) {
299 return SkStringPrintf("%s %s %s",
300 FLAGS_blacklist[i+0], FLAGS_blacklist[i+1], FLAGS_blacklist[i+2]);
301 }
302 }
303 return "";
304}
305
mtklein748ca3b2015-01-15 10:56:12 -0800306// The finest-grained unit of work we can run: draw a single Src into a single Sink,
307// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
308struct Task {
309 Task(const Tagged<Src>& src, const Tagged<Sink>& sink) : src(src), sink(sink) {}
310 const Tagged<Src>& src;
311 const Tagged<Sink>& sink;
312
313 static void Run(Task* task) {
mtkleina2ef6422015-01-15 13:44:22 -0800314 SkString name = task->src->name();
315 SkString whyBlacklisted = is_blacklisted(task->sink.tag, task->src.tag, name.c_str());
mtkleinb9eb4ac2015-02-02 18:26:03 -0800316 SkString log;
mtklein748ca3b2015-01-15 10:56:12 -0800317 WallTimer timer;
318 timer.start();
mtkleina2ef6422015-01-15 13:44:22 -0800319 if (!FLAGS_dryRun && whyBlacklisted.isEmpty()) {
mtklein748ca3b2015-01-15 10:56:12 -0800320 SkBitmap bitmap;
321 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800322 Error err = task->sink->draw(*task->src, &bitmap, &stream, &log);
mtklein748ca3b2015-01-15 10:56:12 -0800323 if (!err.isEmpty()) {
324 fail(SkStringPrintf("%s %s %s: %s",
325 task->sink.tag,
326 task->src.tag,
mtkleina2ef6422015-01-15 13:44:22 -0800327 name.c_str(),
mtklein748ca3b2015-01-15 10:56:12 -0800328 err.c_str()));
329 }
mtklein62bd1a62015-01-27 14:46:26 -0800330 SkAutoTDelete<SkStreamAsset> data(stream.detachAsStream());
331
332 SkString md5;
333 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
334 SkMD5 hash;
335 if (data->getLength()) {
336 hash.writeStream(data, data->getLength());
337 data->rewind();
338 } else {
339 hash.write(bitmap.getPixels(), bitmap.getSize());
340 }
341 SkMD5::Digest digest;
342 hash.finish(digest);
343 for (int i = 0; i < 16; i++) {
344 md5.appendf("%02x", digest.data[i]);
345 }
346 }
347
348 if (!FLAGS_readPath.isEmpty() &&
mtkleina82f5622015-02-20 12:30:19 -0800349 !gGold.contains(Gold(task->sink.tag, task->src.tag, name, md5))) {
mtklein62bd1a62015-01-27 14:46:26 -0800350 fail(SkStringPrintf("%s not found for %s %s %s in %s",
351 md5.c_str(),
352 task->sink.tag,
353 task->src.tag,
354 name.c_str(),
355 FLAGS_readPath[0]));
356 }
357
mtklein748ca3b2015-01-15 10:56:12 -0800358 if (!FLAGS_writePath.isEmpty()) {
359 const char* ext = task->sink->fileExtension();
mtklein62bd1a62015-01-27 14:46:26 -0800360 if (data->getLength()) {
361 WriteToDisk(*task, md5, ext, data, data->getLength(), NULL);
halcanary022afb82015-01-30 11:00:12 -0800362 SkASSERT(bitmap.drawsNothing());
363 } else if (!bitmap.drawsNothing()) {
mtklein62bd1a62015-01-27 14:46:26 -0800364 WriteToDisk(*task, md5, ext, NULL, 0, &bitmap);
mtklein748ca3b2015-01-15 10:56:12 -0800365 }
366 }
367 }
368 timer.end();
mtkleina2ef6422015-01-15 13:44:22 -0800369 if (!whyBlacklisted.isEmpty()) {
370 name.appendf(" (--blacklist, %s)", whyBlacklisted.c_str());
371 }
mtkleinb9eb4ac2015-02-02 18:26:03 -0800372 done(timer.fWall, task->sink.tag, task->src.tag, name, log);
mtklein748ca3b2015-01-15 10:56:12 -0800373 }
374
375 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -0800376 SkString md5,
377 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -0800378 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -0800379 const SkBitmap* bitmap) {
mtklein748ca3b2015-01-15 10:56:12 -0800380 JsonWriter::BitmapResult result;
381 result.name = task.src->name();
382 result.config = task.sink.tag;
383 result.sourceType = task.src.tag;
384 result.ext = ext;
mtklein62bd1a62015-01-27 14:46:26 -0800385 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -0800386 JsonWriter::AddBitmapResult(result);
387
388 const char* dir = FLAGS_writePath[0];
389 if (0 == strcmp(dir, "@")) { // Needed for iOS.
390 dir = FLAGS_resourcePath[0];
391 }
392 sk_mkdir(dir);
393
394 SkString path;
395 if (FLAGS_nameByHash) {
396 path = SkOSPath::Join(dir, result.md5.c_str());
397 path.append(".");
398 path.append(ext);
399 if (sk_exists(path.c_str())) {
400 return; // Content-addressed. If it exists already, we're done.
401 }
402 } else {
403 path = SkOSPath::Join(dir, task.sink.tag);
404 sk_mkdir(path.c_str());
405 path = SkOSPath::Join(path.c_str(), task.src.tag);
406 sk_mkdir(path.c_str());
407 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
408 path.append(".");
409 path.append(ext);
410 }
411
412 SkFILEWStream file(path.c_str());
413 if (!file.isValid()) {
414 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
415 return;
416 }
417
mtklein748ca3b2015-01-15 10:56:12 -0800418 if (bitmap) {
419 // We can't encode A8 bitmaps as PNGs. Convert them to 8888 first.
420 SkBitmap converted;
421 if (bitmap->info().colorType() == kAlpha_8_SkColorType) {
422 if (!bitmap->copyTo(&converted, kN32_SkColorType)) {
423 fail("Can't convert A8 to 8888.\n");
424 return;
425 }
426 bitmap = &converted;
427 }
428 if (!SkImageEncoder::EncodeStream(&file, *bitmap, SkImageEncoder::kPNG_Type, 100)) {
429 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
430 return;
431 }
432 } else {
433 if (!file.writeStream(data, len)) {
434 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
435 return;
436 }
437 }
438 }
439};
440
441// Run all tasks in the same enclave serially on the same thread.
442// They can't possibly run concurrently with each other.
443static void run_enclave(SkTArray<Task>* tasks) {
444 for (int i = 0; i < tasks->count(); i++) {
445 Task::Run(tasks->begin() + i);
446 }
447}
448
449/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
450
451// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
452
mtklein55e88b22015-01-21 15:50:13 -0800453static SkTDArray<skiatest::Test> gThreadedTests, gGPUTests;
mtklein748ca3b2015-01-15 10:56:12 -0800454
455static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -0800456 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -0800457 return;
458 }
halcanary87f3ba42015-01-20 09:30:20 -0800459 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r;
460 r = r->next()) {
461 // Despite its name, factory() is returning a reference to
462 // link-time static const POD data.
463 const skiatest::Test& test = r->factory();
464 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -0800465 continue;
466 }
halcanary87f3ba42015-01-20 09:30:20 -0800467 if (test.needsGpu && gpu_supported()) {
mtklein55e88b22015-01-21 15:50:13 -0800468 (FLAGS_gpu_threading ? gThreadedTests : gGPUTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -0800469 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein55e88b22015-01-21 15:50:13 -0800470 gThreadedTests.push(test);
mtklein82d28432015-01-15 12:46:02 -0800471 }
mtklein748ca3b2015-01-15 10:56:12 -0800472 }
473}
474
halcanary87f3ba42015-01-20 09:30:20 -0800475static void run_test(skiatest::Test* test) {
476 struct : public skiatest::Reporter {
477 void reportFailed(const skiatest::Failure& failure) SK_OVERRIDE {
478 fail(failure.toString());
479 JsonWriter::AddTestFailure(failure);
480 }
481 bool allowExtendedTest() const SK_OVERRIDE {
482 return FLAGS_pathOpsExtended;
483 }
484 bool verbose() const SK_OVERRIDE { return FLAGS_veryVerbose; }
485 } reporter;
mtklein748ca3b2015-01-15 10:56:12 -0800486 WallTimer timer;
487 timer.start();
mtklein748ca3b2015-01-15 10:56:12 -0800488 if (!FLAGS_dryRun) {
mtklein55e88b22015-01-21 15:50:13 -0800489 GrContextFactory factory;
490 test->proc(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -0800491 }
492 timer.end();
mtkleinb9eb4ac2015-02-02 18:26:03 -0800493 done(timer.fWall, "unit", "test", test->name, "");
mtklein748ca3b2015-01-15 10:56:12 -0800494}
495
496/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
497
mtklein55e88b22015-01-21 15:50:13 -0800498// If we're isolating all GPU-bound work to one thread (the default), this function runs all that.
499static void run_enclave_and_gpu_tests(SkTArray<Task>* tasks) {
500 run_enclave(tasks);
501 for (int i = 0; i < gGPUTests.count(); i++) {
502 run_test(&gGPUTests[i]);
503 }
504}
505
jcgregorio3b27ade2014-11-13 08:06:40 -0800506int dm_main();
caryclark17f0b6d2014-07-22 10:15:34 -0700507int dm_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -0800508 SetupCrashHandler();
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +0000509 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -0700510 SkTaskGroup::Enabler enabled(FLAGS_threads);
mtkleine67164d2015-02-02 13:24:37 -0800511 if (FLAGS_leaks) {
512 SkInstCountPrintLeaksOnExit();
513 }
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +0000514
mtklein62bd1a62015-01-27 14:46:26 -0800515 gather_gold();
516
mtklein748ca3b2015-01-15 10:56:12 -0800517 gather_srcs();
518 gather_sinks();
519 gather_tests();
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +0000520
mtklein55e88b22015-01-21 15:50:13 -0800521 gPending = gSrcs.count() * gSinks.count() + gThreadedTests.count() + gGPUTests.count();
mtklein748ca3b2015-01-15 10:56:12 -0800522 SkDebugf("%d srcs * %d sinks + %d tests == %d tasks\n",
mtklein55e88b22015-01-21 15:50:13 -0800523 gSrcs.count(), gSinks.count(), gThreadedTests.count() + gGPUTests.count(), gPending);
mtklein748ca3b2015-01-15 10:56:12 -0800524
525 // We try to exploit as much parallelism as is safe. Most Src/Sink pairs run on any thread,
526 // but Sinks that identify as part of a particular enclave run serially on a single thread.
mtklein82d28432015-01-15 12:46:02 -0800527 // CPU tests run on any thread. GPU tests depend on --gpu_threading.
mtklein748ca3b2015-01-15 10:56:12 -0800528 SkTArray<Task> enclaves[kNumEnclaves];
529 for (int j = 0; j < gSinks.count(); j++) {
530 SkTArray<Task>& tasks = enclaves[gSinks[j]->enclave()];
531 for (int i = 0; i < gSrcs.count(); i++) {
532 tasks.push_back(Task(gSrcs[i], gSinks[j]));
533 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +0000534 }
535
mtklein748ca3b2015-01-15 10:56:12 -0800536 SkTaskGroup tg;
mtklein55e88b22015-01-21 15:50:13 -0800537 tg.batch(run_test, gThreadedTests.begin(), gThreadedTests.count());
538 for (int i = 0; i < kNumEnclaves; i++) {
539 switch(i) {
540 case kAnyThread_Enclave:
541 tg.batch(Task::Run, enclaves[i].begin(), enclaves[i].count());
542 break;
543 case kGPU_Enclave:
544 tg.add(run_enclave_and_gpu_tests, &enclaves[i]);
545 break;
546 default:
547 tg.add(run_enclave, &enclaves[i]);
548 break;
mtklein82d28432015-01-15 12:46:02 -0800549 }
mtklein55e88b22015-01-21 15:50:13 -0800550 }
mtklein748ca3b2015-01-15 10:56:12 -0800551 tg.wait();
mtklein748ca3b2015-01-15 10:56:12 -0800552 // At this point we're back in single-threaded land.
mtklein@google.comd36522d2013-10-16 13:02:15 +0000553
mtklein2f64eec2015-01-15 14:20:41 -0800554 SkDebugf("\n");
mtklein748ca3b2015-01-15 10:56:12 -0800555 if (gFailures.count() > 0) {
556 SkDebugf("Failures:\n");
557 for (int i = 0; i < gFailures.count(); i++) {
mtklein9dc09102015-01-15 15:47:33 -0800558 SkDebugf("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800559 }
560 SkDebugf("%d failures\n", gFailures.count());
561 return 1;
mtklein114c3cd2015-01-15 10:15:02 -0800562 }
mtklein748ca3b2015-01-15 10:56:12 -0800563 if (gPending > 0) {
564 SkDebugf("Hrm, we didn't seem to run everything we intended to! Please file a bug.\n");
565 return 1;
mtklein114c3cd2015-01-15 10:15:02 -0800566 }
mtklein748ca3b2015-01-15 10:56:12 -0800567 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +0000568}
jcgregorio3b27ade2014-11-13 08:06:40 -0800569
570#if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL)
571int main(int argc, char** argv) {
572 SkCommandLineFlags::Parse(argc, argv);
573 return dm_main();
574}
575#endif