blob: f9c2ff776f02122f8ef042874db278b1ac606a74 [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 }
tomhudson64de1e12015-03-05 08:01:07 -0800262
263#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
264 VIA("androidsdk", ViaAndroidSDK, wrapped);
265#endif
266
mtklein748ca3b2015-01-15 10:56:12 -0800267#undef VIA
268 return NULL;
mtklein114c3cd2015-01-15 10:15:02 -0800269}
270
mtklein748ca3b2015-01-15 10:56:12 -0800271static void gather_sinks() {
272 for (int i = 0; i < FLAGS_config.count(); i++) {
273 const char* config = FLAGS_config[i];
274 SkTArray<SkString> parts;
275 SkStrSplit(config, "-", &parts);
276
277 Sink* sink = NULL;
278 for (int i = parts.count(); i-- > 0;) {
279 const char* part = parts[i].c_str();
280 Sink* next = (sink == NULL) ? create_sink(part) : create_via(part, sink);
281 if (next == NULL) {
282 SkDebugf("Skipping %s: Don't understand '%s'.\n", config, part);
283 delete sink;
284 sink = NULL;
285 break;
286 }
287 sink = next;
288 }
289 if (sink) {
290 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800291 }
292 }
293}
mtklein709d2c32015-01-15 08:30:25 -0800294
mtkleina2ef6422015-01-15 13:44:22 -0800295static bool match(const char* needle, const char* haystack) {
296 return 0 == strcmp("_", needle) || NULL != strstr(haystack, needle);
297}
298
299static ImplicitString is_blacklisted(const char* sink, const char* src, const char* name) {
300 for (int i = 0; i < FLAGS_blacklist.count() - 2; i += 3) {
301 if (match(FLAGS_blacklist[i+0], sink) &&
302 match(FLAGS_blacklist[i+1], src) &&
303 match(FLAGS_blacklist[i+2], name)) {
304 return SkStringPrintf("%s %s %s",
305 FLAGS_blacklist[i+0], FLAGS_blacklist[i+1], FLAGS_blacklist[i+2]);
306 }
307 }
308 return "";
309}
310
mtklein748ca3b2015-01-15 10:56:12 -0800311// The finest-grained unit of work we can run: draw a single Src into a single Sink,
312// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
313struct Task {
314 Task(const Tagged<Src>& src, const Tagged<Sink>& sink) : src(src), sink(sink) {}
315 const Tagged<Src>& src;
316 const Tagged<Sink>& sink;
317
318 static void Run(Task* task) {
mtkleina2ef6422015-01-15 13:44:22 -0800319 SkString name = task->src->name();
320 SkString whyBlacklisted = is_blacklisted(task->sink.tag, task->src.tag, name.c_str());
mtkleinb9eb4ac2015-02-02 18:26:03 -0800321 SkString log;
mtklein748ca3b2015-01-15 10:56:12 -0800322 WallTimer timer;
323 timer.start();
mtkleina2ef6422015-01-15 13:44:22 -0800324 if (!FLAGS_dryRun && whyBlacklisted.isEmpty()) {
mtklein748ca3b2015-01-15 10:56:12 -0800325 SkBitmap bitmap;
326 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800327 Error err = task->sink->draw(*task->src, &bitmap, &stream, &log);
mtklein748ca3b2015-01-15 10:56:12 -0800328 if (!err.isEmpty()) {
329 fail(SkStringPrintf("%s %s %s: %s",
330 task->sink.tag,
331 task->src.tag,
mtkleina2ef6422015-01-15 13:44:22 -0800332 name.c_str(),
mtklein748ca3b2015-01-15 10:56:12 -0800333 err.c_str()));
334 }
mtklein62bd1a62015-01-27 14:46:26 -0800335 SkAutoTDelete<SkStreamAsset> data(stream.detachAsStream());
336
337 SkString md5;
338 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
339 SkMD5 hash;
340 if (data->getLength()) {
341 hash.writeStream(data, data->getLength());
342 data->rewind();
343 } else {
344 hash.write(bitmap.getPixels(), bitmap.getSize());
345 }
346 SkMD5::Digest digest;
347 hash.finish(digest);
348 for (int i = 0; i < 16; i++) {
349 md5.appendf("%02x", digest.data[i]);
350 }
351 }
352
353 if (!FLAGS_readPath.isEmpty() &&
mtkleina82f5622015-02-20 12:30:19 -0800354 !gGold.contains(Gold(task->sink.tag, task->src.tag, name, md5))) {
mtklein62bd1a62015-01-27 14:46:26 -0800355 fail(SkStringPrintf("%s not found for %s %s %s in %s",
356 md5.c_str(),
357 task->sink.tag,
358 task->src.tag,
359 name.c_str(),
360 FLAGS_readPath[0]));
361 }
362
mtklein748ca3b2015-01-15 10:56:12 -0800363 if (!FLAGS_writePath.isEmpty()) {
364 const char* ext = task->sink->fileExtension();
mtklein62bd1a62015-01-27 14:46:26 -0800365 if (data->getLength()) {
366 WriteToDisk(*task, md5, ext, data, data->getLength(), NULL);
halcanary022afb82015-01-30 11:00:12 -0800367 SkASSERT(bitmap.drawsNothing());
368 } else if (!bitmap.drawsNothing()) {
mtklein62bd1a62015-01-27 14:46:26 -0800369 WriteToDisk(*task, md5, ext, NULL, 0, &bitmap);
mtklein748ca3b2015-01-15 10:56:12 -0800370 }
371 }
372 }
373 timer.end();
mtkleina2ef6422015-01-15 13:44:22 -0800374 if (!whyBlacklisted.isEmpty()) {
375 name.appendf(" (--blacklist, %s)", whyBlacklisted.c_str());
376 }
mtkleinb9eb4ac2015-02-02 18:26:03 -0800377 done(timer.fWall, task->sink.tag, task->src.tag, name, log);
mtklein748ca3b2015-01-15 10:56:12 -0800378 }
379
380 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -0800381 SkString md5,
382 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -0800383 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -0800384 const SkBitmap* bitmap) {
mtklein748ca3b2015-01-15 10:56:12 -0800385 JsonWriter::BitmapResult result;
386 result.name = task.src->name();
387 result.config = task.sink.tag;
388 result.sourceType = task.src.tag;
389 result.ext = ext;
mtklein62bd1a62015-01-27 14:46:26 -0800390 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -0800391 JsonWriter::AddBitmapResult(result);
392
393 const char* dir = FLAGS_writePath[0];
394 if (0 == strcmp(dir, "@")) { // Needed for iOS.
395 dir = FLAGS_resourcePath[0];
396 }
397 sk_mkdir(dir);
398
399 SkString path;
400 if (FLAGS_nameByHash) {
401 path = SkOSPath::Join(dir, result.md5.c_str());
402 path.append(".");
403 path.append(ext);
404 if (sk_exists(path.c_str())) {
405 return; // Content-addressed. If it exists already, we're done.
406 }
407 } else {
408 path = SkOSPath::Join(dir, task.sink.tag);
409 sk_mkdir(path.c_str());
410 path = SkOSPath::Join(path.c_str(), task.src.tag);
411 sk_mkdir(path.c_str());
412 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
413 path.append(".");
414 path.append(ext);
415 }
416
417 SkFILEWStream file(path.c_str());
418 if (!file.isValid()) {
419 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
420 return;
421 }
422
mtklein748ca3b2015-01-15 10:56:12 -0800423 if (bitmap) {
424 // We can't encode A8 bitmaps as PNGs. Convert them to 8888 first.
425 SkBitmap converted;
426 if (bitmap->info().colorType() == kAlpha_8_SkColorType) {
427 if (!bitmap->copyTo(&converted, kN32_SkColorType)) {
428 fail("Can't convert A8 to 8888.\n");
429 return;
430 }
431 bitmap = &converted;
432 }
433 if (!SkImageEncoder::EncodeStream(&file, *bitmap, SkImageEncoder::kPNG_Type, 100)) {
434 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
435 return;
436 }
437 } else {
438 if (!file.writeStream(data, len)) {
439 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
440 return;
441 }
442 }
443 }
444};
445
446// Run all tasks in the same enclave serially on the same thread.
447// They can't possibly run concurrently with each other.
448static void run_enclave(SkTArray<Task>* tasks) {
449 for (int i = 0; i < tasks->count(); i++) {
450 Task::Run(tasks->begin() + i);
451 }
452}
453
454/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
455
456// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
457
mtklein55e88b22015-01-21 15:50:13 -0800458static SkTDArray<skiatest::Test> gThreadedTests, gGPUTests;
mtklein748ca3b2015-01-15 10:56:12 -0800459
460static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -0800461 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -0800462 return;
463 }
halcanary87f3ba42015-01-20 09:30:20 -0800464 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r;
465 r = r->next()) {
466 // Despite its name, factory() is returning a reference to
467 // link-time static const POD data.
468 const skiatest::Test& test = r->factory();
469 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -0800470 continue;
471 }
halcanary87f3ba42015-01-20 09:30:20 -0800472 if (test.needsGpu && gpu_supported()) {
mtklein55e88b22015-01-21 15:50:13 -0800473 (FLAGS_gpu_threading ? gThreadedTests : gGPUTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -0800474 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein55e88b22015-01-21 15:50:13 -0800475 gThreadedTests.push(test);
mtklein82d28432015-01-15 12:46:02 -0800476 }
mtklein748ca3b2015-01-15 10:56:12 -0800477 }
478}
479
halcanary87f3ba42015-01-20 09:30:20 -0800480static void run_test(skiatest::Test* test) {
481 struct : public skiatest::Reporter {
482 void reportFailed(const skiatest::Failure& failure) SK_OVERRIDE {
483 fail(failure.toString());
484 JsonWriter::AddTestFailure(failure);
485 }
486 bool allowExtendedTest() const SK_OVERRIDE {
487 return FLAGS_pathOpsExtended;
488 }
489 bool verbose() const SK_OVERRIDE { return FLAGS_veryVerbose; }
490 } reporter;
mtklein748ca3b2015-01-15 10:56:12 -0800491 WallTimer timer;
492 timer.start();
mtklein748ca3b2015-01-15 10:56:12 -0800493 if (!FLAGS_dryRun) {
mtklein55e88b22015-01-21 15:50:13 -0800494 GrContextFactory factory;
495 test->proc(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -0800496 }
497 timer.end();
mtkleinb9eb4ac2015-02-02 18:26:03 -0800498 done(timer.fWall, "unit", "test", test->name, "");
mtklein748ca3b2015-01-15 10:56:12 -0800499}
500
501/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
502
mtklein55e88b22015-01-21 15:50:13 -0800503// If we're isolating all GPU-bound work to one thread (the default), this function runs all that.
504static void run_enclave_and_gpu_tests(SkTArray<Task>* tasks) {
505 run_enclave(tasks);
506 for (int i = 0; i < gGPUTests.count(); i++) {
507 run_test(&gGPUTests[i]);
508 }
509}
510
jcgregorio3b27ade2014-11-13 08:06:40 -0800511int dm_main();
caryclark17f0b6d2014-07-22 10:15:34 -0700512int dm_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -0800513 SetupCrashHandler();
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +0000514 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -0700515 SkTaskGroup::Enabler enabled(FLAGS_threads);
mtkleine67164d2015-02-02 13:24:37 -0800516 if (FLAGS_leaks) {
517 SkInstCountPrintLeaksOnExit();
518 }
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +0000519
mtklein62bd1a62015-01-27 14:46:26 -0800520 gather_gold();
521
mtklein748ca3b2015-01-15 10:56:12 -0800522 gather_srcs();
523 gather_sinks();
524 gather_tests();
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +0000525
mtklein55e88b22015-01-21 15:50:13 -0800526 gPending = gSrcs.count() * gSinks.count() + gThreadedTests.count() + gGPUTests.count();
mtklein748ca3b2015-01-15 10:56:12 -0800527 SkDebugf("%d srcs * %d sinks + %d tests == %d tasks\n",
mtklein55e88b22015-01-21 15:50:13 -0800528 gSrcs.count(), gSinks.count(), gThreadedTests.count() + gGPUTests.count(), gPending);
mtklein748ca3b2015-01-15 10:56:12 -0800529
530 // We try to exploit as much parallelism as is safe. Most Src/Sink pairs run on any thread,
531 // but Sinks that identify as part of a particular enclave run serially on a single thread.
mtklein82d28432015-01-15 12:46:02 -0800532 // CPU tests run on any thread. GPU tests depend on --gpu_threading.
mtklein748ca3b2015-01-15 10:56:12 -0800533 SkTArray<Task> enclaves[kNumEnclaves];
534 for (int j = 0; j < gSinks.count(); j++) {
535 SkTArray<Task>& tasks = enclaves[gSinks[j]->enclave()];
536 for (int i = 0; i < gSrcs.count(); i++) {
537 tasks.push_back(Task(gSrcs[i], gSinks[j]));
538 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +0000539 }
540
mtklein748ca3b2015-01-15 10:56:12 -0800541 SkTaskGroup tg;
mtklein55e88b22015-01-21 15:50:13 -0800542 tg.batch(run_test, gThreadedTests.begin(), gThreadedTests.count());
543 for (int i = 0; i < kNumEnclaves; i++) {
544 switch(i) {
545 case kAnyThread_Enclave:
546 tg.batch(Task::Run, enclaves[i].begin(), enclaves[i].count());
547 break;
548 case kGPU_Enclave:
549 tg.add(run_enclave_and_gpu_tests, &enclaves[i]);
550 break;
551 default:
552 tg.add(run_enclave, &enclaves[i]);
553 break;
mtklein82d28432015-01-15 12:46:02 -0800554 }
mtklein55e88b22015-01-21 15:50:13 -0800555 }
mtklein748ca3b2015-01-15 10:56:12 -0800556 tg.wait();
mtklein748ca3b2015-01-15 10:56:12 -0800557 // At this point we're back in single-threaded land.
mtklein@google.comd36522d2013-10-16 13:02:15 +0000558
mtklein2f64eec2015-01-15 14:20:41 -0800559 SkDebugf("\n");
mtklein748ca3b2015-01-15 10:56:12 -0800560 if (gFailures.count() > 0) {
561 SkDebugf("Failures:\n");
562 for (int i = 0; i < gFailures.count(); i++) {
mtklein9dc09102015-01-15 15:47:33 -0800563 SkDebugf("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800564 }
565 SkDebugf("%d failures\n", gFailures.count());
566 return 1;
mtklein114c3cd2015-01-15 10:15:02 -0800567 }
mtklein748ca3b2015-01-15 10:56:12 -0800568 if (gPending > 0) {
569 SkDebugf("Hrm, we didn't seem to run everything we intended to! Please file a bug.\n");
570 return 1;
mtklein114c3cd2015-01-15 10:15:02 -0800571 }
mtklein748ca3b2015-01-15 10:56:12 -0800572 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +0000573}
jcgregorio3b27ade2014-11-13 08:06:40 -0800574
575#if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL)
576int main(int argc, char** argv) {
577 SkCommandLineFlags::Parse(argc, argv);
578 return dm_main();
579}
580#endif