blob: 5745113096b4433e473ada6793e56380c58537cf [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.");
24DEFINE_string(matrix, "1 0 0 0 1 0 0 0 1",
25 "Matrix to apply when using 'matrix' in config.");
mtklein82d28432015-01-15 12:46:02 -080026DEFINE_bool(gpu_threading, false, "Allow GPU work to run on multiple threads?");
mtklein@google.comd36522d2013-10-16 13:02:15 +000027
mtkleina2ef6422015-01-15 13:44:22 -080028DEFINE_string(blacklist, "",
29 "Space-separated config/src/name triples to blacklist. '_' matches anything. E.g. \n"
30 "'--blacklist gpu skp _' will blacklist all SKPs drawn into the gpu config.\n"
31 "'--blacklist gpu skp _ 8888 gm aarects' will also blacklist the aarects GM on 8888.");
32
mtklein62bd1a62015-01-27 14:46:26 -080033DEFINE_string2(readPath, r, "", "If set check for equality with golden results in this directory.");
34
mtklein@google.comd36522d2013-10-16 13:02:15 +000035__SK_FORCE_IMAGE_DECODER_LINKING;
mtklein748ca3b2015-01-15 10:56:12 -080036using namespace DM;
mtklein@google.comd36522d2013-10-16 13:02:15 +000037
mtklein748ca3b2015-01-15 10:56:12 -080038/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
39
40SK_DECLARE_STATIC_MUTEX(gFailuresMutex);
41static SkTArray<SkString> gFailures;
42
43static void fail(ImplicitString err) {
44 SkAutoMutexAcquire lock(gFailuresMutex);
45 SkDebugf("\n\nFAILURE: %s\n\n", err.c_str());
46 gFailures.push_back(err);
halcanary9e398f72015-01-10 11:18:04 -080047}
48
mtklein748ca3b2015-01-15 10:56:12 -080049static int32_t gPending = 0; // Atomic.
50
mtkleinb9eb4ac2015-02-02 18:26:03 -080051static void done(double ms,
52 ImplicitString config, ImplicitString src, ImplicitString name,
53 ImplicitString log) {
54 if (!log.isEmpty()) {
55 log.prepend("\n");
56 }
mtklein6dee2ad2015-02-05 09:53:44 -080057 auto pending = sk_atomic_dec(&gPending)-1;
mtkleinb9eb4ac2015-02-02 18:26:03 -080058 SkDebugf("%s(%4dMB %5d) %s\t%s %s %s%s", FLAGS_verbose ? "\n" : kSkOverwriteLine
mtklein748ca3b2015-01-15 10:56:12 -080059 , sk_tools::getMaxResidentSetSizeMB()
mtkleina17241b2015-01-23 05:48:00 -080060 , pending
mtklein748ca3b2015-01-15 10:56:12 -080061 , HumanizeMs(ms).c_str()
62 , config.c_str()
63 , src.c_str()
mtkleinb9eb4ac2015-02-02 18:26:03 -080064 , name.c_str()
65 , log.c_str());
mtkleina17241b2015-01-23 05:48:00 -080066 // We write our dm.json file every once in a while in case we crash.
67 // Notice this also handles the final dm.json when pending == 0.
68 if (pending % 500 == 0) {
69 JsonWriter::DumpJson();
70 }
mtklein@google.comd36522d2013-10-16 13:02:15 +000071}
72
mtklein748ca3b2015-01-15 10:56:12 -080073/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
mtklein114c3cd2015-01-15 10:15:02 -080074
mtklein62bd1a62015-01-27 14:46:26 -080075struct Gold : public SkString {
76 Gold(ImplicitString sink, ImplicitString src, ImplicitString name, ImplicitString md5)
77 : SkString("") {
78 this->append(sink);
79 this->append(src);
80 this->append(name);
81 this->append(md5);
82 while (this->size() % 4) {
83 this->append("!"); // Pad out if needed so we can pass this to Murmur3.
84 }
85 }
86 static const Gold& GetKey(const Gold& g) { return g; }
87 static uint32_t Hash(const Gold& g) {
88 return SkChecksum::Murmur3((const uint32_t*)g.c_str(), g.size());
89 }
90};
91static SkTDynamicHash<Gold, Gold> gGold;
92
93static void add_gold(JsonWriter::BitmapResult r) {
94 gGold.add(new Gold(r.config, r.sourceType, r.name, r.md5)); // We'll let these leak. Lazybones.
95}
96
97static void gather_gold() {
98 if (!FLAGS_readPath.isEmpty()) {
99 SkString path(FLAGS_readPath[0]);
100 path.append("/dm.json");
101 if (!JsonWriter::ReadJson(path.c_str(), add_gold)) {
102 fail(SkStringPrintf("Couldn't read %s for golden results.", path.c_str()));
103 }
104 }
105}
106
107/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
108
mtklein748ca3b2015-01-15 10:56:12 -0800109template <typename T>
110struct Tagged : public SkAutoTDelete<T> { const char* tag; };
111
112static const bool kMemcpyOK = true;
113
114static SkTArray<Tagged<Src>, kMemcpyOK> gSrcs;
115static SkTArray<Tagged<Sink>, kMemcpyOK> gSinks;
116
117static void push_src(const char* tag, Src* s) {
118 SkAutoTDelete<Src> src(s);
119 if (FLAGS_src.contains(tag) &&
120 !SkCommandLineFlags::ShouldSkip(FLAGS_match, src->name().c_str())) {
121 Tagged<Src>& s = gSrcs.push_back();
122 s.reset(src.detach());
123 s.tag = tag;
mtklein114c3cd2015-01-15 10:15:02 -0800124 }
mtklein748ca3b2015-01-15 10:56:12 -0800125}
mtklein114c3cd2015-01-15 10:15:02 -0800126
mtklein748ca3b2015-01-15 10:56:12 -0800127static void gather_srcs() {
128 for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) {
129 push_src("gm", new GMSrc(r->factory()));
130 }
halcanaryfc37ad12015-01-30 07:31:19 -0800131 for (int i = 0; i < FLAGS_skps.count(); i++) {
132 const char* path = FLAGS_skps[i];
133 if (sk_isdir(path)) {
134 SkOSFile::Iter it(path, "skp");
135 for (SkString file; it.next(&file); ) {
136 push_src("skp", new SKPSrc(SkOSPath::Join(path, file.c_str())));
137 }
138 } else {
mtklein8d17a132015-01-30 11:42:31 -0800139 push_src("skp", new SKPSrc(path));
mtklein114c3cd2015-01-15 10:15:02 -0800140 }
141 }
halcanary23b03c32015-01-30 09:58:58 -0800142 static const char* const exts[] = {
143 "bmp", "gif", "jpg", "jpeg", "png", "webp", "ktx", "astc", "wbmp", "ico",
144 "BMP", "GIF", "JPG", "JPEG", "PNG", "WEBP", "KTX", "ASTC", "WBMP", "ICO",
145 };
146 for (int i = 0; i < FLAGS_images.count(); i++) {
147 const char* flag = FLAGS_images[i];
148 if (sk_isdir(flag)) {
149 for (size_t j = 0; j < SK_ARRAY_COUNT(exts); j++) {
150 SkOSFile::Iter it(flag, exts[j]);
151 for (SkString file; it.next(&file); ) {
152 SkString path = SkOSPath::Join(flag, file.c_str());
153 push_src("image", new ImageSrc(path)); // Decode entire image.
mtkleinedc93bc2015-01-30 13:22:23 -0800154 push_src("subset", new ImageSrc(path, 2)); // Decode into 2 x 2 subsets
halcanary23b03c32015-01-30 09:58:58 -0800155 }
mtklein114c3cd2015-01-15 10:15:02 -0800156 }
halcanary23b03c32015-01-30 09:58:58 -0800157 } else if (sk_exists(flag)) {
158 // assume that FLAGS_images[i] is a valid image if it is a file.
mtklein8d17a132015-01-30 11:42:31 -0800159 push_src("image", new ImageSrc(flag)); // Decode entire image.
mtkleinedc93bc2015-01-30 13:22:23 -0800160 push_src("subset", new ImageSrc(flag, 2)); // Decode into 2 x 2 subsets
mtklein709d2c32015-01-15 08:30:25 -0800161 }
mtklein709d2c32015-01-15 08:30:25 -0800162 }
163}
164
mtklein748ca3b2015-01-15 10:56:12 -0800165static GrGLStandard get_gpu_api() {
166 if (FLAGS_gpuAPI.contains("gl")) { return kGL_GrGLStandard; }
167 if (FLAGS_gpuAPI.contains("gles")) { return kGLES_GrGLStandard; }
168 return kNone_GrGLStandard;
mtklein709d2c32015-01-15 08:30:25 -0800169}
170
mtklein748ca3b2015-01-15 10:56:12 -0800171static void push_sink(const char* tag, Sink* s) {
172 SkAutoTDelete<Sink> sink(s);
173 if (!FLAGS_config.contains(tag)) {
174 return;
mtklein114c3cd2015-01-15 10:15:02 -0800175 }
mtklein748ca3b2015-01-15 10:56:12 -0800176 // Try a noop Src as a canary. If it fails, skip this sink.
177 struct : public Src {
178 Error draw(SkCanvas*) const SK_OVERRIDE { return ""; }
179 SkISize size() const SK_OVERRIDE { return SkISize::Make(16, 16); }
180 Name name() const SK_OVERRIDE { return "noop"; }
181 } noop;
mtklein114c3cd2015-01-15 10:15:02 -0800182
mtklein748ca3b2015-01-15 10:56:12 -0800183 SkBitmap bitmap;
184 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800185 SkString log;
186 Error err = sink->draw(noop, &bitmap, &stream, &log);
mtklein748ca3b2015-01-15 10:56:12 -0800187 if (!err.isEmpty()) {
188 SkDebugf("Skipping %s: %s\n", tag, err.c_str());
mtklein114c3cd2015-01-15 10:15:02 -0800189 return;
190 }
191
mtklein748ca3b2015-01-15 10:56:12 -0800192 Tagged<Sink>& ts = gSinks.push_back();
193 ts.reset(sink.detach());
194 ts.tag = tag;
195}
196
197static bool gpu_supported() {
198#if SK_SUPPORT_GPU
199 return FLAGS_gpu;
200#else
201 return false;
202#endif
203}
204
205static Sink* create_sink(const char* tag) {
206#define SINK(t, sink, ...) if (0 == strcmp(t, tag)) { return new sink(__VA_ARGS__); }
207 if (gpu_supported()) {
mtklein82d28432015-01-15 12:46:02 -0800208 typedef GrContextFactory Gr;
mtklein748ca3b2015-01-15 10:56:12 -0800209 const GrGLStandard api = get_gpu_api();
mtklein82d28432015-01-15 12:46:02 -0800210 SINK("gpunull", GPUSink, Gr::kNull_GLContextType, api, 0, false, FLAGS_gpu_threading);
211 SINK("gpudebug", GPUSink, Gr::kDebug_GLContextType, api, 0, false, FLAGS_gpu_threading);
212 SINK("gpu", GPUSink, Gr::kNative_GLContextType, api, 0, false, FLAGS_gpu_threading);
213 SINK("gpudft", GPUSink, Gr::kNative_GLContextType, api, 0, true, FLAGS_gpu_threading);
214 SINK("msaa4", GPUSink, Gr::kNative_GLContextType, api, 4, false, FLAGS_gpu_threading);
215 SINK("msaa16", GPUSink, Gr::kNative_GLContextType, api, 16, false, FLAGS_gpu_threading);
216 SINK("nvprmsaa4", GPUSink, Gr::kNVPR_GLContextType, api, 4, false, FLAGS_gpu_threading);
217 SINK("nvprmsaa16", GPUSink, Gr::kNVPR_GLContextType, api, 16, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800218 #if SK_ANGLE
mtklein82d28432015-01-15 12:46:02 -0800219 SINK("angle", GPUSink, Gr::kANGLE_GLContextType, api, 0, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800220 #endif
221 #if SK_MESA
mtklein82d28432015-01-15 12:46:02 -0800222 SINK("mesa", GPUSink, Gr::kMESA_GLContextType, api, 0, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800223 #endif
mtklein114c3cd2015-01-15 10:15:02 -0800224 }
mtklein748ca3b2015-01-15 10:56:12 -0800225
226 if (FLAGS_cpu) {
227 SINK("565", RasterSink, kRGB_565_SkColorType);
228 SINK("8888", RasterSink, kN32_SkColorType);
mtklein19f30602015-01-20 13:34:39 -0800229 SINK("pdf", PDFSink);
mtklein9c3f17d2015-01-28 11:35:18 -0800230 SINK("skp", SKPSink);
mtklein8a4527e2015-01-31 20:00:58 -0800231 SINK("svg", SVGSink);
232 SINK("null", NullSink);
mtklein748ca3b2015-01-15 10:56:12 -0800233 }
234#undef SINK
235 return NULL;
mtklein114c3cd2015-01-15 10:15:02 -0800236}
237
mtklein748ca3b2015-01-15 10:56:12 -0800238static Sink* create_via(const char* tag, Sink* wrapped) {
239#define VIA(t, via, ...) if (0 == strcmp(t, tag)) { return new via(__VA_ARGS__); }
mtklein7edca212015-01-21 13:18:51 -0800240 VIA("pipe", ViaPipe, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800241 VIA("serialize", ViaSerialization, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800242 VIA("tiles", ViaTiles, 256, 256, NULL, wrapped);
243 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800244
245 if (FLAGS_matrix.count() == 9) {
246 SkMatrix m;
247 for (int i = 0; i < 9; i++) {
248 m[i] = (SkScalar)atof(FLAGS_matrix[i]);
249 }
250 VIA("matrix", ViaMatrix, m, wrapped);
251 }
252#undef VIA
253 return NULL;
mtklein114c3cd2015-01-15 10:15:02 -0800254}
255
mtklein748ca3b2015-01-15 10:56:12 -0800256static void gather_sinks() {
257 for (int i = 0; i < FLAGS_config.count(); i++) {
258 const char* config = FLAGS_config[i];
259 SkTArray<SkString> parts;
260 SkStrSplit(config, "-", &parts);
261
262 Sink* sink = NULL;
263 for (int i = parts.count(); i-- > 0;) {
264 const char* part = parts[i].c_str();
265 Sink* next = (sink == NULL) ? create_sink(part) : create_via(part, sink);
266 if (next == NULL) {
267 SkDebugf("Skipping %s: Don't understand '%s'.\n", config, part);
268 delete sink;
269 sink = NULL;
270 break;
271 }
272 sink = next;
273 }
274 if (sink) {
275 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800276 }
277 }
278}
mtklein709d2c32015-01-15 08:30:25 -0800279
mtkleina2ef6422015-01-15 13:44:22 -0800280static bool match(const char* needle, const char* haystack) {
281 return 0 == strcmp("_", needle) || NULL != strstr(haystack, needle);
282}
283
284static ImplicitString is_blacklisted(const char* sink, const char* src, const char* name) {
285 for (int i = 0; i < FLAGS_blacklist.count() - 2; i += 3) {
286 if (match(FLAGS_blacklist[i+0], sink) &&
287 match(FLAGS_blacklist[i+1], src) &&
288 match(FLAGS_blacklist[i+2], name)) {
289 return SkStringPrintf("%s %s %s",
290 FLAGS_blacklist[i+0], FLAGS_blacklist[i+1], FLAGS_blacklist[i+2]);
291 }
292 }
293 return "";
294}
295
mtklein748ca3b2015-01-15 10:56:12 -0800296// The finest-grained unit of work we can run: draw a single Src into a single Sink,
297// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
298struct Task {
299 Task(const Tagged<Src>& src, const Tagged<Sink>& sink) : src(src), sink(sink) {}
300 const Tagged<Src>& src;
301 const Tagged<Sink>& sink;
302
303 static void Run(Task* task) {
mtkleina2ef6422015-01-15 13:44:22 -0800304 SkString name = task->src->name();
305 SkString whyBlacklisted = is_blacklisted(task->sink.tag, task->src.tag, name.c_str());
mtkleinb9eb4ac2015-02-02 18:26:03 -0800306 SkString log;
mtklein748ca3b2015-01-15 10:56:12 -0800307 WallTimer timer;
308 timer.start();
mtkleina2ef6422015-01-15 13:44:22 -0800309 if (!FLAGS_dryRun && whyBlacklisted.isEmpty()) {
mtklein748ca3b2015-01-15 10:56:12 -0800310 SkBitmap bitmap;
311 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800312 Error err = task->sink->draw(*task->src, &bitmap, &stream, &log);
mtklein748ca3b2015-01-15 10:56:12 -0800313 if (!err.isEmpty()) {
314 fail(SkStringPrintf("%s %s %s: %s",
315 task->sink.tag,
316 task->src.tag,
mtkleina2ef6422015-01-15 13:44:22 -0800317 name.c_str(),
mtklein748ca3b2015-01-15 10:56:12 -0800318 err.c_str()));
319 }
mtklein62bd1a62015-01-27 14:46:26 -0800320 SkAutoTDelete<SkStreamAsset> data(stream.detachAsStream());
321
322 SkString md5;
323 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
324 SkMD5 hash;
325 if (data->getLength()) {
326 hash.writeStream(data, data->getLength());
327 data->rewind();
328 } else {
329 hash.write(bitmap.getPixels(), bitmap.getSize());
330 }
331 SkMD5::Digest digest;
332 hash.finish(digest);
333 for (int i = 0; i < 16; i++) {
334 md5.appendf("%02x", digest.data[i]);
335 }
336 }
337
338 if (!FLAGS_readPath.isEmpty() &&
339 !gGold.find(Gold(task->sink.tag, task->src.tag, name, md5))) {
340 fail(SkStringPrintf("%s not found for %s %s %s in %s",
341 md5.c_str(),
342 task->sink.tag,
343 task->src.tag,
344 name.c_str(),
345 FLAGS_readPath[0]));
346 }
347
mtklein748ca3b2015-01-15 10:56:12 -0800348 if (!FLAGS_writePath.isEmpty()) {
349 const char* ext = task->sink->fileExtension();
mtklein62bd1a62015-01-27 14:46:26 -0800350 if (data->getLength()) {
351 WriteToDisk(*task, md5, ext, data, data->getLength(), NULL);
halcanary022afb82015-01-30 11:00:12 -0800352 SkASSERT(bitmap.drawsNothing());
353 } else if (!bitmap.drawsNothing()) {
mtklein62bd1a62015-01-27 14:46:26 -0800354 WriteToDisk(*task, md5, ext, NULL, 0, &bitmap);
mtklein748ca3b2015-01-15 10:56:12 -0800355 }
356 }
357 }
358 timer.end();
mtkleina2ef6422015-01-15 13:44:22 -0800359 if (!whyBlacklisted.isEmpty()) {
360 name.appendf(" (--blacklist, %s)", whyBlacklisted.c_str());
361 }
mtkleinb9eb4ac2015-02-02 18:26:03 -0800362 done(timer.fWall, task->sink.tag, task->src.tag, name, log);
mtklein748ca3b2015-01-15 10:56:12 -0800363 }
364
365 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -0800366 SkString md5,
367 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -0800368 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -0800369 const SkBitmap* bitmap) {
mtklein748ca3b2015-01-15 10:56:12 -0800370 JsonWriter::BitmapResult result;
371 result.name = task.src->name();
372 result.config = task.sink.tag;
373 result.sourceType = task.src.tag;
374 result.ext = ext;
mtklein62bd1a62015-01-27 14:46:26 -0800375 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -0800376 JsonWriter::AddBitmapResult(result);
377
378 const char* dir = FLAGS_writePath[0];
379 if (0 == strcmp(dir, "@")) { // Needed for iOS.
380 dir = FLAGS_resourcePath[0];
381 }
382 sk_mkdir(dir);
383
384 SkString path;
385 if (FLAGS_nameByHash) {
386 path = SkOSPath::Join(dir, result.md5.c_str());
387 path.append(".");
388 path.append(ext);
389 if (sk_exists(path.c_str())) {
390 return; // Content-addressed. If it exists already, we're done.
391 }
392 } else {
393 path = SkOSPath::Join(dir, task.sink.tag);
394 sk_mkdir(path.c_str());
395 path = SkOSPath::Join(path.c_str(), task.src.tag);
396 sk_mkdir(path.c_str());
397 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
398 path.append(".");
399 path.append(ext);
400 }
401
402 SkFILEWStream file(path.c_str());
403 if (!file.isValid()) {
404 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
405 return;
406 }
407
mtklein748ca3b2015-01-15 10:56:12 -0800408 if (bitmap) {
409 // We can't encode A8 bitmaps as PNGs. Convert them to 8888 first.
410 SkBitmap converted;
411 if (bitmap->info().colorType() == kAlpha_8_SkColorType) {
412 if (!bitmap->copyTo(&converted, kN32_SkColorType)) {
413 fail("Can't convert A8 to 8888.\n");
414 return;
415 }
416 bitmap = &converted;
417 }
418 if (!SkImageEncoder::EncodeStream(&file, *bitmap, SkImageEncoder::kPNG_Type, 100)) {
419 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
420 return;
421 }
422 } else {
423 if (!file.writeStream(data, len)) {
424 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
425 return;
426 }
427 }
428 }
429};
430
431// Run all tasks in the same enclave serially on the same thread.
432// They can't possibly run concurrently with each other.
433static void run_enclave(SkTArray<Task>* tasks) {
434 for (int i = 0; i < tasks->count(); i++) {
435 Task::Run(tasks->begin() + i);
436 }
437}
438
439/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
440
441// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
442
mtklein55e88b22015-01-21 15:50:13 -0800443static SkTDArray<skiatest::Test> gThreadedTests, gGPUTests;
mtklein748ca3b2015-01-15 10:56:12 -0800444
445static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -0800446 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -0800447 return;
448 }
halcanary87f3ba42015-01-20 09:30:20 -0800449 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r;
450 r = r->next()) {
451 // Despite its name, factory() is returning a reference to
452 // link-time static const POD data.
453 const skiatest::Test& test = r->factory();
454 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -0800455 continue;
456 }
halcanary87f3ba42015-01-20 09:30:20 -0800457 if (test.needsGpu && gpu_supported()) {
mtklein55e88b22015-01-21 15:50:13 -0800458 (FLAGS_gpu_threading ? gThreadedTests : gGPUTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -0800459 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein55e88b22015-01-21 15:50:13 -0800460 gThreadedTests.push(test);
mtklein82d28432015-01-15 12:46:02 -0800461 }
mtklein748ca3b2015-01-15 10:56:12 -0800462 }
463}
464
halcanary87f3ba42015-01-20 09:30:20 -0800465static void run_test(skiatest::Test* test) {
466 struct : public skiatest::Reporter {
467 void reportFailed(const skiatest::Failure& failure) SK_OVERRIDE {
468 fail(failure.toString());
469 JsonWriter::AddTestFailure(failure);
470 }
471 bool allowExtendedTest() const SK_OVERRIDE {
472 return FLAGS_pathOpsExtended;
473 }
474 bool verbose() const SK_OVERRIDE { return FLAGS_veryVerbose; }
475 } reporter;
mtklein748ca3b2015-01-15 10:56:12 -0800476 WallTimer timer;
477 timer.start();
mtklein748ca3b2015-01-15 10:56:12 -0800478 if (!FLAGS_dryRun) {
mtklein55e88b22015-01-21 15:50:13 -0800479 GrContextFactory factory;
480 test->proc(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -0800481 }
482 timer.end();
mtkleinb9eb4ac2015-02-02 18:26:03 -0800483 done(timer.fWall, "unit", "test", test->name, "");
mtklein748ca3b2015-01-15 10:56:12 -0800484}
485
486/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
487
mtklein55e88b22015-01-21 15:50:13 -0800488// If we're isolating all GPU-bound work to one thread (the default), this function runs all that.
489static void run_enclave_and_gpu_tests(SkTArray<Task>* tasks) {
490 run_enclave(tasks);
491 for (int i = 0; i < gGPUTests.count(); i++) {
492 run_test(&gGPUTests[i]);
493 }
494}
495
jcgregorio3b27ade2014-11-13 08:06:40 -0800496int dm_main();
caryclark17f0b6d2014-07-22 10:15:34 -0700497int dm_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -0800498 SetupCrashHandler();
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +0000499 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -0700500 SkTaskGroup::Enabler enabled(FLAGS_threads);
mtkleine67164d2015-02-02 13:24:37 -0800501 if (FLAGS_leaks) {
502 SkInstCountPrintLeaksOnExit();
503 }
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +0000504
mtklein62bd1a62015-01-27 14:46:26 -0800505 gather_gold();
506
mtklein748ca3b2015-01-15 10:56:12 -0800507 gather_srcs();
508 gather_sinks();
509 gather_tests();
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +0000510
mtklein55e88b22015-01-21 15:50:13 -0800511 gPending = gSrcs.count() * gSinks.count() + gThreadedTests.count() + gGPUTests.count();
mtklein748ca3b2015-01-15 10:56:12 -0800512 SkDebugf("%d srcs * %d sinks + %d tests == %d tasks\n",
mtklein55e88b22015-01-21 15:50:13 -0800513 gSrcs.count(), gSinks.count(), gThreadedTests.count() + gGPUTests.count(), gPending);
mtklein748ca3b2015-01-15 10:56:12 -0800514
515 // We try to exploit as much parallelism as is safe. Most Src/Sink pairs run on any thread,
516 // but Sinks that identify as part of a particular enclave run serially on a single thread.
mtklein82d28432015-01-15 12:46:02 -0800517 // CPU tests run on any thread. GPU tests depend on --gpu_threading.
mtklein748ca3b2015-01-15 10:56:12 -0800518 SkTArray<Task> enclaves[kNumEnclaves];
519 for (int j = 0; j < gSinks.count(); j++) {
520 SkTArray<Task>& tasks = enclaves[gSinks[j]->enclave()];
521 for (int i = 0; i < gSrcs.count(); i++) {
522 tasks.push_back(Task(gSrcs[i], gSinks[j]));
523 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +0000524 }
525
mtklein748ca3b2015-01-15 10:56:12 -0800526 SkTaskGroup tg;
mtklein55e88b22015-01-21 15:50:13 -0800527 tg.batch(run_test, gThreadedTests.begin(), gThreadedTests.count());
528 for (int i = 0; i < kNumEnclaves; i++) {
529 switch(i) {
530 case kAnyThread_Enclave:
531 tg.batch(Task::Run, enclaves[i].begin(), enclaves[i].count());
532 break;
533 case kGPU_Enclave:
534 tg.add(run_enclave_and_gpu_tests, &enclaves[i]);
535 break;
536 default:
537 tg.add(run_enclave, &enclaves[i]);
538 break;
mtklein82d28432015-01-15 12:46:02 -0800539 }
mtklein55e88b22015-01-21 15:50:13 -0800540 }
mtklein748ca3b2015-01-15 10:56:12 -0800541 tg.wait();
mtklein748ca3b2015-01-15 10:56:12 -0800542 // At this point we're back in single-threaded land.
mtklein@google.comd36522d2013-10-16 13:02:15 +0000543
mtklein2f64eec2015-01-15 14:20:41 -0800544 SkDebugf("\n");
mtklein748ca3b2015-01-15 10:56:12 -0800545 if (gFailures.count() > 0) {
546 SkDebugf("Failures:\n");
547 for (int i = 0; i < gFailures.count(); i++) {
mtklein9dc09102015-01-15 15:47:33 -0800548 SkDebugf("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800549 }
550 SkDebugf("%d failures\n", gFailures.count());
551 return 1;
mtklein114c3cd2015-01-15 10:15:02 -0800552 }
mtklein748ca3b2015-01-15 10:56:12 -0800553 if (gPending > 0) {
554 SkDebugf("Hrm, we didn't seem to run everything we intended to! Please file a bug.\n");
555 return 1;
mtklein114c3cd2015-01-15 10:15:02 -0800556 }
mtklein748ca3b2015-01-15 10:56:12 -0800557 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +0000558}
jcgregorio3b27ade2014-11-13 08:06:40 -0800559
560#if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL)
561int main(int argc, char** argv) {
562 SkCommandLineFlags::Parse(argc, argv);
563 return dm_main();
564}
565#endif