blob: 3efc5c141a4d50af4f762f74546cf30756153882 [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
mtklein748ca3b2015-01-15 10:56:12 -080019DEFINE_string(images, "resources", "Images to decode.");
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.");
25DEFINE_string(matrix, "1 0 0 0 1 0 0 0 1",
26 "Matrix to apply when using 'matrix' in config.");
mtklein82d28432015-01-15 12:46:02 -080027DEFINE_bool(gpu_threading, false, "Allow GPU work to run on multiple threads?");
mtklein@google.comd36522d2013-10-16 13:02:15 +000028
mtkleina2ef6422015-01-15 13:44:22 -080029DEFINE_string(blacklist, "",
30 "Space-separated config/src/name triples to blacklist. '_' matches anything. E.g. \n"
31 "'--blacklist gpu skp _' will blacklist all SKPs drawn into the gpu config.\n"
32 "'--blacklist gpu skp _ 8888 gm aarects' will also blacklist the aarects GM on 8888.");
33
mtklein62bd1a62015-01-27 14:46:26 -080034DEFINE_string2(readPath, r, "", "If set check for equality with golden results in this directory.");
35
mtklein@google.comd36522d2013-10-16 13:02:15 +000036__SK_FORCE_IMAGE_DECODER_LINKING;
mtklein748ca3b2015-01-15 10:56:12 -080037using namespace DM;
mtklein@google.comd36522d2013-10-16 13:02:15 +000038
mtklein748ca3b2015-01-15 10:56:12 -080039/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
40
41SK_DECLARE_STATIC_MUTEX(gFailuresMutex);
42static SkTArray<SkString> gFailures;
43
44static void fail(ImplicitString err) {
45 SkAutoMutexAcquire lock(gFailuresMutex);
46 SkDebugf("\n\nFAILURE: %s\n\n", err.c_str());
47 gFailures.push_back(err);
halcanary9e398f72015-01-10 11:18:04 -080048}
49
mtklein748ca3b2015-01-15 10:56:12 -080050static int32_t gPending = 0; // Atomic.
51
mtkleinb9eb4ac2015-02-02 18:26:03 -080052static void done(double ms,
53 ImplicitString config, ImplicitString src, ImplicitString name,
54 ImplicitString log) {
55 if (!log.isEmpty()) {
56 log.prepend("\n");
57 }
mtkleina17241b2015-01-23 05:48:00 -080058 int32_t pending = sk_atomic_dec(&gPending)-1;
mtkleinb9eb4ac2015-02-02 18:26:03 -080059 SkDebugf("%s(%4dMB %5d) %s\t%s %s %s%s", FLAGS_verbose ? "\n" : kSkOverwriteLine
mtklein748ca3b2015-01-15 10:56:12 -080060 , sk_tools::getMaxResidentSetSizeMB()
mtkleina17241b2015-01-23 05:48:00 -080061 , pending
mtklein748ca3b2015-01-15 10:56:12 -080062 , HumanizeMs(ms).c_str()
63 , config.c_str()
64 , src.c_str()
mtkleinb9eb4ac2015-02-02 18:26:03 -080065 , name.c_str()
66 , log.c_str());
mtkleina17241b2015-01-23 05:48:00 -080067 // We write our dm.json file every once in a while in case we crash.
68 // Notice this also handles the final dm.json when pending == 0.
69 if (pending % 500 == 0) {
70 JsonWriter::DumpJson();
71 }
mtklein@google.comd36522d2013-10-16 13:02:15 +000072}
73
mtklein748ca3b2015-01-15 10:56:12 -080074/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
mtklein114c3cd2015-01-15 10:15:02 -080075
mtklein62bd1a62015-01-27 14:46:26 -080076struct Gold : public SkString {
77 Gold(ImplicitString sink, ImplicitString src, ImplicitString name, ImplicitString md5)
78 : SkString("") {
79 this->append(sink);
80 this->append(src);
81 this->append(name);
82 this->append(md5);
83 while (this->size() % 4) {
84 this->append("!"); // Pad out if needed so we can pass this to Murmur3.
85 }
86 }
87 static const Gold& GetKey(const Gold& g) { return g; }
88 static uint32_t Hash(const Gold& g) {
89 return SkChecksum::Murmur3((const uint32_t*)g.c_str(), g.size());
90 }
91};
92static SkTDynamicHash<Gold, Gold> gGold;
93
94static void add_gold(JsonWriter::BitmapResult r) {
95 gGold.add(new Gold(r.config, r.sourceType, r.name, r.md5)); // We'll let these leak. Lazybones.
96}
97
98static void gather_gold() {
99 if (!FLAGS_readPath.isEmpty()) {
100 SkString path(FLAGS_readPath[0]);
101 path.append("/dm.json");
102 if (!JsonWriter::ReadJson(path.c_str(), add_gold)) {
103 fail(SkStringPrintf("Couldn't read %s for golden results.", path.c_str()));
104 }
105 }
106}
107
108/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
109
mtklein748ca3b2015-01-15 10:56:12 -0800110template <typename T>
111struct Tagged : public SkAutoTDelete<T> { const char* tag; };
112
113static const bool kMemcpyOK = true;
114
115static SkTArray<Tagged<Src>, kMemcpyOK> gSrcs;
116static SkTArray<Tagged<Sink>, kMemcpyOK> gSinks;
117
118static void push_src(const char* tag, Src* s) {
119 SkAutoTDelete<Src> src(s);
120 if (FLAGS_src.contains(tag) &&
121 !SkCommandLineFlags::ShouldSkip(FLAGS_match, src->name().c_str())) {
122 Tagged<Src>& s = gSrcs.push_back();
123 s.reset(src.detach());
124 s.tag = tag;
mtklein114c3cd2015-01-15 10:15:02 -0800125 }
mtklein748ca3b2015-01-15 10:56:12 -0800126}
mtklein114c3cd2015-01-15 10:15:02 -0800127
mtklein748ca3b2015-01-15 10:56:12 -0800128static void gather_srcs() {
129 for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) {
130 push_src("gm", new GMSrc(r->factory()));
131 }
halcanaryfc37ad12015-01-30 07:31:19 -0800132 for (int i = 0; i < FLAGS_skps.count(); i++) {
133 const char* path = FLAGS_skps[i];
134 if (sk_isdir(path)) {
135 SkOSFile::Iter it(path, "skp");
136 for (SkString file; it.next(&file); ) {
137 push_src("skp", new SKPSrc(SkOSPath::Join(path, file.c_str())));
138 }
139 } else {
mtklein8d17a132015-01-30 11:42:31 -0800140 push_src("skp", new SKPSrc(path));
mtklein114c3cd2015-01-15 10:15:02 -0800141 }
142 }
halcanary23b03c32015-01-30 09:58:58 -0800143 static const char* const exts[] = {
144 "bmp", "gif", "jpg", "jpeg", "png", "webp", "ktx", "astc", "wbmp", "ico",
145 "BMP", "GIF", "JPG", "JPEG", "PNG", "WEBP", "KTX", "ASTC", "WBMP", "ICO",
146 };
147 for (int i = 0; i < FLAGS_images.count(); i++) {
148 const char* flag = FLAGS_images[i];
149 if (sk_isdir(flag)) {
150 for (size_t j = 0; j < SK_ARRAY_COUNT(exts); j++) {
151 SkOSFile::Iter it(flag, exts[j]);
152 for (SkString file; it.next(&file); ) {
153 SkString path = SkOSPath::Join(flag, file.c_str());
154 push_src("image", new ImageSrc(path)); // Decode entire image.
mtkleinedc93bc2015-01-30 13:22:23 -0800155 push_src("subset", new ImageSrc(path, 2)); // Decode into 2 x 2 subsets
halcanary23b03c32015-01-30 09:58:58 -0800156 }
mtklein114c3cd2015-01-15 10:15:02 -0800157 }
halcanary23b03c32015-01-30 09:58:58 -0800158 } else if (sk_exists(flag)) {
159 // assume that FLAGS_images[i] is a valid image if it is a file.
mtklein8d17a132015-01-30 11:42:31 -0800160 push_src("image", new ImageSrc(flag)); // Decode entire image.
mtkleinedc93bc2015-01-30 13:22:23 -0800161 push_src("subset", new ImageSrc(flag, 2)); // Decode into 2 x 2 subsets
mtklein709d2c32015-01-15 08:30:25 -0800162 }
mtklein709d2c32015-01-15 08:30:25 -0800163 }
164}
165
mtklein748ca3b2015-01-15 10:56:12 -0800166static GrGLStandard get_gpu_api() {
167 if (FLAGS_gpuAPI.contains("gl")) { return kGL_GrGLStandard; }
168 if (FLAGS_gpuAPI.contains("gles")) { return kGLES_GrGLStandard; }
169 return kNone_GrGLStandard;
mtklein709d2c32015-01-15 08:30:25 -0800170}
171
mtklein748ca3b2015-01-15 10:56:12 -0800172static void push_sink(const char* tag, Sink* s) {
173 SkAutoTDelete<Sink> sink(s);
174 if (!FLAGS_config.contains(tag)) {
175 return;
mtklein114c3cd2015-01-15 10:15:02 -0800176 }
mtklein748ca3b2015-01-15 10:56:12 -0800177 // Try a noop Src as a canary. If it fails, skip this sink.
178 struct : public Src {
179 Error draw(SkCanvas*) const SK_OVERRIDE { return ""; }
180 SkISize size() const SK_OVERRIDE { return SkISize::Make(16, 16); }
181 Name name() const SK_OVERRIDE { return "noop"; }
182 } noop;
mtklein114c3cd2015-01-15 10:15:02 -0800183
mtklein748ca3b2015-01-15 10:56:12 -0800184 SkBitmap bitmap;
185 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800186 SkString log;
187 Error err = sink->draw(noop, &bitmap, &stream, &log);
mtklein748ca3b2015-01-15 10:56:12 -0800188 if (!err.isEmpty()) {
189 SkDebugf("Skipping %s: %s\n", tag, err.c_str());
mtklein114c3cd2015-01-15 10:15:02 -0800190 return;
191 }
192
mtklein748ca3b2015-01-15 10:56:12 -0800193 Tagged<Sink>& ts = gSinks.push_back();
194 ts.reset(sink.detach());
195 ts.tag = tag;
196}
197
198static bool gpu_supported() {
199#if SK_SUPPORT_GPU
200 return FLAGS_gpu;
201#else
202 return false;
203#endif
204}
205
206static Sink* create_sink(const char* tag) {
207#define SINK(t, sink, ...) if (0 == strcmp(t, tag)) { return new sink(__VA_ARGS__); }
208 if (gpu_supported()) {
mtklein82d28432015-01-15 12:46:02 -0800209 typedef GrContextFactory Gr;
mtklein748ca3b2015-01-15 10:56:12 -0800210 const GrGLStandard api = get_gpu_api();
mtklein82d28432015-01-15 12:46:02 -0800211 SINK("gpunull", GPUSink, Gr::kNull_GLContextType, api, 0, false, FLAGS_gpu_threading);
212 SINK("gpudebug", GPUSink, Gr::kDebug_GLContextType, api, 0, false, FLAGS_gpu_threading);
213 SINK("gpu", GPUSink, Gr::kNative_GLContextType, api, 0, false, FLAGS_gpu_threading);
214 SINK("gpudft", GPUSink, Gr::kNative_GLContextType, api, 0, true, FLAGS_gpu_threading);
215 SINK("msaa4", GPUSink, Gr::kNative_GLContextType, api, 4, false, FLAGS_gpu_threading);
216 SINK("msaa16", GPUSink, Gr::kNative_GLContextType, api, 16, false, FLAGS_gpu_threading);
217 SINK("nvprmsaa4", GPUSink, Gr::kNVPR_GLContextType, api, 4, false, FLAGS_gpu_threading);
218 SINK("nvprmsaa16", GPUSink, Gr::kNVPR_GLContextType, api, 16, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800219 #if SK_ANGLE
mtklein82d28432015-01-15 12:46:02 -0800220 SINK("angle", GPUSink, Gr::kANGLE_GLContextType, api, 0, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800221 #endif
222 #if SK_MESA
mtklein82d28432015-01-15 12:46:02 -0800223 SINK("mesa", GPUSink, Gr::kMESA_GLContextType, api, 0, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800224 #endif
mtklein114c3cd2015-01-15 10:15:02 -0800225 }
mtklein748ca3b2015-01-15 10:56:12 -0800226
227 if (FLAGS_cpu) {
228 SINK("565", RasterSink, kRGB_565_SkColorType);
229 SINK("8888", RasterSink, kN32_SkColorType);
mtklein19f30602015-01-20 13:34:39 -0800230 SINK("pdf", PDFSink);
mtklein9c3f17d2015-01-28 11:35:18 -0800231 SINK("skp", SKPSink);
mtklein8a4527e2015-01-31 20:00:58 -0800232 SINK("svg", SVGSink);
233 SINK("null", NullSink);
mtklein748ca3b2015-01-15 10:56:12 -0800234 }
235#undef SINK
236 return NULL;
mtklein114c3cd2015-01-15 10:15:02 -0800237}
238
mtklein748ca3b2015-01-15 10:56:12 -0800239static Sink* create_via(const char* tag, Sink* wrapped) {
240#define VIA(t, via, ...) if (0 == strcmp(t, tag)) { return new via(__VA_ARGS__); }
mtklein7edca212015-01-21 13:18:51 -0800241 VIA("pipe", ViaPipe, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800242 VIA("serialize", ViaSerialization, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800243 VIA("tiles", ViaTiles, 256, 256, NULL, wrapped);
244 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800245
246 if (FLAGS_matrix.count() == 9) {
247 SkMatrix m;
248 for (int i = 0; i < 9; i++) {
249 m[i] = (SkScalar)atof(FLAGS_matrix[i]);
250 }
251 VIA("matrix", ViaMatrix, m, wrapped);
252 }
253#undef VIA
254 return NULL;
mtklein114c3cd2015-01-15 10:15:02 -0800255}
256
mtklein748ca3b2015-01-15 10:56:12 -0800257static void gather_sinks() {
258 for (int i = 0; i < FLAGS_config.count(); i++) {
259 const char* config = FLAGS_config[i];
260 SkTArray<SkString> parts;
261 SkStrSplit(config, "-", &parts);
262
263 Sink* sink = NULL;
264 for (int i = parts.count(); i-- > 0;) {
265 const char* part = parts[i].c_str();
266 Sink* next = (sink == NULL) ? create_sink(part) : create_via(part, sink);
267 if (next == NULL) {
268 SkDebugf("Skipping %s: Don't understand '%s'.\n", config, part);
269 delete sink;
270 sink = NULL;
271 break;
272 }
273 sink = next;
274 }
275 if (sink) {
276 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800277 }
278 }
279}
mtklein709d2c32015-01-15 08:30:25 -0800280
mtkleina2ef6422015-01-15 13:44:22 -0800281static bool match(const char* needle, const char* haystack) {
282 return 0 == strcmp("_", needle) || NULL != strstr(haystack, needle);
283}
284
285static ImplicitString is_blacklisted(const char* sink, const char* src, const char* name) {
286 for (int i = 0; i < FLAGS_blacklist.count() - 2; i += 3) {
287 if (match(FLAGS_blacklist[i+0], sink) &&
288 match(FLAGS_blacklist[i+1], src) &&
289 match(FLAGS_blacklist[i+2], name)) {
290 return SkStringPrintf("%s %s %s",
291 FLAGS_blacklist[i+0], FLAGS_blacklist[i+1], FLAGS_blacklist[i+2]);
292 }
293 }
294 return "";
295}
296
mtklein748ca3b2015-01-15 10:56:12 -0800297// The finest-grained unit of work we can run: draw a single Src into a single Sink,
298// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
299struct Task {
300 Task(const Tagged<Src>& src, const Tagged<Sink>& sink) : src(src), sink(sink) {}
301 const Tagged<Src>& src;
302 const Tagged<Sink>& sink;
303
304 static void Run(Task* task) {
mtkleina2ef6422015-01-15 13:44:22 -0800305 SkString name = task->src->name();
306 SkString whyBlacklisted = is_blacklisted(task->sink.tag, task->src.tag, name.c_str());
mtkleinb9eb4ac2015-02-02 18:26:03 -0800307 SkString log;
mtklein748ca3b2015-01-15 10:56:12 -0800308 WallTimer timer;
309 timer.start();
mtkleina2ef6422015-01-15 13:44:22 -0800310 if (!FLAGS_dryRun && whyBlacklisted.isEmpty()) {
mtklein748ca3b2015-01-15 10:56:12 -0800311 SkBitmap bitmap;
312 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800313 Error err = task->sink->draw(*task->src, &bitmap, &stream, &log);
mtklein748ca3b2015-01-15 10:56:12 -0800314 if (!err.isEmpty()) {
315 fail(SkStringPrintf("%s %s %s: %s",
316 task->sink.tag,
317 task->src.tag,
mtkleina2ef6422015-01-15 13:44:22 -0800318 name.c_str(),
mtklein748ca3b2015-01-15 10:56:12 -0800319 err.c_str()));
320 }
mtklein62bd1a62015-01-27 14:46:26 -0800321 SkAutoTDelete<SkStreamAsset> data(stream.detachAsStream());
322
323 SkString md5;
324 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
325 SkMD5 hash;
326 if (data->getLength()) {
327 hash.writeStream(data, data->getLength());
328 data->rewind();
329 } else {
330 hash.write(bitmap.getPixels(), bitmap.getSize());
331 }
332 SkMD5::Digest digest;
333 hash.finish(digest);
334 for (int i = 0; i < 16; i++) {
335 md5.appendf("%02x", digest.data[i]);
336 }
337 }
338
339 if (!FLAGS_readPath.isEmpty() &&
340 !gGold.find(Gold(task->sink.tag, task->src.tag, name, md5))) {
341 fail(SkStringPrintf("%s not found for %s %s %s in %s",
342 md5.c_str(),
343 task->sink.tag,
344 task->src.tag,
345 name.c_str(),
346 FLAGS_readPath[0]));
347 }
348
mtklein748ca3b2015-01-15 10:56:12 -0800349 if (!FLAGS_writePath.isEmpty()) {
350 const char* ext = task->sink->fileExtension();
mtklein62bd1a62015-01-27 14:46:26 -0800351 if (data->getLength()) {
352 WriteToDisk(*task, md5, ext, data, data->getLength(), NULL);
halcanary022afb82015-01-30 11:00:12 -0800353 SkASSERT(bitmap.drawsNothing());
354 } else if (!bitmap.drawsNothing()) {
mtklein62bd1a62015-01-27 14:46:26 -0800355 WriteToDisk(*task, md5, ext, NULL, 0, &bitmap);
mtklein748ca3b2015-01-15 10:56:12 -0800356 }
357 }
358 }
359 timer.end();
mtkleina2ef6422015-01-15 13:44:22 -0800360 if (!whyBlacklisted.isEmpty()) {
361 name.appendf(" (--blacklist, %s)", whyBlacklisted.c_str());
362 }
mtkleinb9eb4ac2015-02-02 18:26:03 -0800363 done(timer.fWall, task->sink.tag, task->src.tag, name, log);
mtklein748ca3b2015-01-15 10:56:12 -0800364 }
365
366 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -0800367 SkString md5,
368 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -0800369 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -0800370 const SkBitmap* bitmap) {
mtklein748ca3b2015-01-15 10:56:12 -0800371 JsonWriter::BitmapResult result;
372 result.name = task.src->name();
373 result.config = task.sink.tag;
374 result.sourceType = task.src.tag;
375 result.ext = ext;
mtklein62bd1a62015-01-27 14:46:26 -0800376 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -0800377 JsonWriter::AddBitmapResult(result);
378
379 const char* dir = FLAGS_writePath[0];
380 if (0 == strcmp(dir, "@")) { // Needed for iOS.
381 dir = FLAGS_resourcePath[0];
382 }
383 sk_mkdir(dir);
384
385 SkString path;
386 if (FLAGS_nameByHash) {
387 path = SkOSPath::Join(dir, result.md5.c_str());
388 path.append(".");
389 path.append(ext);
390 if (sk_exists(path.c_str())) {
391 return; // Content-addressed. If it exists already, we're done.
392 }
393 } else {
394 path = SkOSPath::Join(dir, task.sink.tag);
395 sk_mkdir(path.c_str());
396 path = SkOSPath::Join(path.c_str(), task.src.tag);
397 sk_mkdir(path.c_str());
398 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
399 path.append(".");
400 path.append(ext);
401 }
402
403 SkFILEWStream file(path.c_str());
404 if (!file.isValid()) {
405 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
406 return;
407 }
408
mtklein748ca3b2015-01-15 10:56:12 -0800409 if (bitmap) {
410 // We can't encode A8 bitmaps as PNGs. Convert them to 8888 first.
411 SkBitmap converted;
412 if (bitmap->info().colorType() == kAlpha_8_SkColorType) {
413 if (!bitmap->copyTo(&converted, kN32_SkColorType)) {
414 fail("Can't convert A8 to 8888.\n");
415 return;
416 }
417 bitmap = &converted;
418 }
419 if (!SkImageEncoder::EncodeStream(&file, *bitmap, SkImageEncoder::kPNG_Type, 100)) {
420 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
421 return;
422 }
423 } else {
424 if (!file.writeStream(data, len)) {
425 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
426 return;
427 }
428 }
429 }
430};
431
432// Run all tasks in the same enclave serially on the same thread.
433// They can't possibly run concurrently with each other.
434static void run_enclave(SkTArray<Task>* tasks) {
435 for (int i = 0; i < tasks->count(); i++) {
436 Task::Run(tasks->begin() + i);
437 }
438}
439
440/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
441
442// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
443
mtklein55e88b22015-01-21 15:50:13 -0800444static SkTDArray<skiatest::Test> gThreadedTests, gGPUTests;
mtklein748ca3b2015-01-15 10:56:12 -0800445
446static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -0800447 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -0800448 return;
449 }
halcanary87f3ba42015-01-20 09:30:20 -0800450 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r;
451 r = r->next()) {
452 // Despite its name, factory() is returning a reference to
453 // link-time static const POD data.
454 const skiatest::Test& test = r->factory();
455 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -0800456 continue;
457 }
halcanary87f3ba42015-01-20 09:30:20 -0800458 if (test.needsGpu && gpu_supported()) {
mtklein55e88b22015-01-21 15:50:13 -0800459 (FLAGS_gpu_threading ? gThreadedTests : gGPUTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -0800460 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein55e88b22015-01-21 15:50:13 -0800461 gThreadedTests.push(test);
mtklein82d28432015-01-15 12:46:02 -0800462 }
mtklein748ca3b2015-01-15 10:56:12 -0800463 }
464}
465
halcanary87f3ba42015-01-20 09:30:20 -0800466static void run_test(skiatest::Test* test) {
467 struct : public skiatest::Reporter {
468 void reportFailed(const skiatest::Failure& failure) SK_OVERRIDE {
469 fail(failure.toString());
470 JsonWriter::AddTestFailure(failure);
471 }
472 bool allowExtendedTest() const SK_OVERRIDE {
473 return FLAGS_pathOpsExtended;
474 }
475 bool verbose() const SK_OVERRIDE { return FLAGS_veryVerbose; }
476 } reporter;
mtklein748ca3b2015-01-15 10:56:12 -0800477 WallTimer timer;
478 timer.start();
mtklein748ca3b2015-01-15 10:56:12 -0800479 if (!FLAGS_dryRun) {
mtklein55e88b22015-01-21 15:50:13 -0800480 GrContextFactory factory;
481 test->proc(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -0800482 }
483 timer.end();
mtkleinb9eb4ac2015-02-02 18:26:03 -0800484 done(timer.fWall, "unit", "test", test->name, "");
mtklein748ca3b2015-01-15 10:56:12 -0800485}
486
487/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
488
mtklein55e88b22015-01-21 15:50:13 -0800489// If we're isolating all GPU-bound work to one thread (the default), this function runs all that.
490static void run_enclave_and_gpu_tests(SkTArray<Task>* tasks) {
491 run_enclave(tasks);
492 for (int i = 0; i < gGPUTests.count(); i++) {
493 run_test(&gGPUTests[i]);
494 }
495}
496
jcgregorio3b27ade2014-11-13 08:06:40 -0800497int dm_main();
caryclark17f0b6d2014-07-22 10:15:34 -0700498int dm_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -0800499 SetupCrashHandler();
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +0000500 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -0700501 SkTaskGroup::Enabler enabled(FLAGS_threads);
mtkleine67164d2015-02-02 13:24:37 -0800502 if (FLAGS_leaks) {
503 SkInstCountPrintLeaksOnExit();
504 }
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +0000505
mtklein62bd1a62015-01-27 14:46:26 -0800506 gather_gold();
507
mtklein748ca3b2015-01-15 10:56:12 -0800508 gather_srcs();
509 gather_sinks();
510 gather_tests();
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +0000511
mtklein55e88b22015-01-21 15:50:13 -0800512 gPending = gSrcs.count() * gSinks.count() + gThreadedTests.count() + gGPUTests.count();
mtklein748ca3b2015-01-15 10:56:12 -0800513 SkDebugf("%d srcs * %d sinks + %d tests == %d tasks\n",
mtklein55e88b22015-01-21 15:50:13 -0800514 gSrcs.count(), gSinks.count(), gThreadedTests.count() + gGPUTests.count(), gPending);
mtklein748ca3b2015-01-15 10:56:12 -0800515
516 // We try to exploit as much parallelism as is safe. Most Src/Sink pairs run on any thread,
517 // but Sinks that identify as part of a particular enclave run serially on a single thread.
mtklein82d28432015-01-15 12:46:02 -0800518 // CPU tests run on any thread. GPU tests depend on --gpu_threading.
mtklein748ca3b2015-01-15 10:56:12 -0800519 SkTArray<Task> enclaves[kNumEnclaves];
520 for (int j = 0; j < gSinks.count(); j++) {
521 SkTArray<Task>& tasks = enclaves[gSinks[j]->enclave()];
522 for (int i = 0; i < gSrcs.count(); i++) {
523 tasks.push_back(Task(gSrcs[i], gSinks[j]));
524 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +0000525 }
526
mtklein748ca3b2015-01-15 10:56:12 -0800527 SkTaskGroup tg;
mtklein55e88b22015-01-21 15:50:13 -0800528 tg.batch(run_test, gThreadedTests.begin(), gThreadedTests.count());
529 for (int i = 0; i < kNumEnclaves; i++) {
530 switch(i) {
531 case kAnyThread_Enclave:
532 tg.batch(Task::Run, enclaves[i].begin(), enclaves[i].count());
533 break;
534 case kGPU_Enclave:
535 tg.add(run_enclave_and_gpu_tests, &enclaves[i]);
536 break;
537 default:
538 tg.add(run_enclave, &enclaves[i]);
539 break;
mtklein82d28432015-01-15 12:46:02 -0800540 }
mtklein55e88b22015-01-21 15:50:13 -0800541 }
mtklein748ca3b2015-01-15 10:56:12 -0800542 tg.wait();
mtklein748ca3b2015-01-15 10:56:12 -0800543 // At this point we're back in single-threaded land.
mtklein@google.comd36522d2013-10-16 13:02:15 +0000544
mtklein2f64eec2015-01-15 14:20:41 -0800545 SkDebugf("\n");
mtklein748ca3b2015-01-15 10:56:12 -0800546 if (gFailures.count() > 0) {
547 SkDebugf("Failures:\n");
548 for (int i = 0; i < gFailures.count(); i++) {
mtklein9dc09102015-01-15 15:47:33 -0800549 SkDebugf("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800550 }
551 SkDebugf("%d failures\n", gFailures.count());
552 return 1;
mtklein114c3cd2015-01-15 10:15:02 -0800553 }
mtklein748ca3b2015-01-15 10:56:12 -0800554 if (gPending > 0) {
555 SkDebugf("Hrm, we didn't seem to run everything we intended to! Please file a bug.\n");
556 return 1;
mtklein114c3cd2015-01-15 10:15:02 -0800557 }
mtklein748ca3b2015-01-15 10:56:12 -0800558 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +0000559}
jcgregorio3b27ade2014-11-13 08:06:40 -0800560
561#if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL)
562int main(int argc, char** argv) {
563 SkCommandLineFlags::Parse(argc, argv);
564 return dm_main();
565}
566#endif