blob: 9739210bc2e4eb3584a4f08ede0e3506a6130230 [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"
mtkleinde6fc2b2015-03-12 06:28:54 -070017#include "SkThreadUtils.h"
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +000018#include "Test.h"
mtklein748ca3b2015-01-15 10:56:12 -080019#include "Timer.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +000020
mtkleinedc93bc2015-01-30 13:22:23 -080021DEFINE_string(src, "tests gm skp image subset", "Source types to test.");
mtklein748ca3b2015-01-15 10:56:12 -080022DEFINE_bool(nameByHash, false,
23 "If true, write to FLAGS_writePath[0]/<hash>.png instead of "
24 "to FLAGS_writePath[0]/<config>/<sourceType>/<name>.png");
25DEFINE_bool2(pathOpsExtended, x, false, "Run extended pathOps tests.");
mtkleind603b222015-02-17 11:13:33 -080026DEFINE_string(matrix, "1 0 0 1",
27 "2x2 scale+skew matrix to apply or upright when using "
28 "'matrix' or 'upright' in config.");
mtklein82d28432015-01-15 12:46:02 -080029DEFINE_bool(gpu_threading, false, "Allow GPU work to run on multiple threads?");
mtklein@google.comd36522d2013-10-16 13:02:15 +000030
mtkleina2ef6422015-01-15 13:44:22 -080031DEFINE_string(blacklist, "",
32 "Space-separated config/src/name triples to blacklist. '_' matches anything. E.g. \n"
33 "'--blacklist gpu skp _' will blacklist all SKPs drawn into the gpu config.\n"
34 "'--blacklist gpu skp _ 8888 gm aarects' will also blacklist the aarects GM on 8888.");
35
mtklein62bd1a62015-01-27 14:46:26 -080036DEFINE_string2(readPath, r, "", "If set check for equality with golden results in this directory.");
37
mtklein@google.comd36522d2013-10-16 13:02:15 +000038__SK_FORCE_IMAGE_DECODER_LINKING;
mtklein748ca3b2015-01-15 10:56:12 -080039using namespace DM;
mtklein@google.comd36522d2013-10-16 13:02:15 +000040
mtklein748ca3b2015-01-15 10:56:12 -080041/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
42
43SK_DECLARE_STATIC_MUTEX(gFailuresMutex);
44static SkTArray<SkString> gFailures;
45
46static void fail(ImplicitString err) {
47 SkAutoMutexAcquire lock(gFailuresMutex);
48 SkDebugf("\n\nFAILURE: %s\n\n", err.c_str());
49 gFailures.push_back(err);
halcanary9e398f72015-01-10 11:18:04 -080050}
51
mtklein748ca3b2015-01-15 10:56:12 -080052static int32_t gPending = 0; // Atomic.
53
mtkleinb9eb4ac2015-02-02 18:26:03 -080054static void done(double ms,
55 ImplicitString config, ImplicitString src, ImplicitString name,
56 ImplicitString log) {
57 if (!log.isEmpty()) {
58 log.prepend("\n");
59 }
mtklein6dee2ad2015-02-05 09:53:44 -080060 auto pending = sk_atomic_dec(&gPending)-1;
mtkleinb9eb4ac2015-02-02 18:26:03 -080061 SkDebugf("%s(%4dMB %5d) %s\t%s %s %s%s", FLAGS_verbose ? "\n" : kSkOverwriteLine
mtklein748ca3b2015-01-15 10:56:12 -080062 , sk_tools::getMaxResidentSetSizeMB()
mtkleina17241b2015-01-23 05:48:00 -080063 , pending
mtklein748ca3b2015-01-15 10:56:12 -080064 , HumanizeMs(ms).c_str()
65 , config.c_str()
66 , src.c_str()
mtkleinb9eb4ac2015-02-02 18:26:03 -080067 , name.c_str()
68 , log.c_str());
mtkleina17241b2015-01-23 05:48:00 -080069 // We write our dm.json file every once in a while in case we crash.
70 // Notice this also handles the final dm.json when pending == 0.
71 if (pending % 500 == 0) {
72 JsonWriter::DumpJson();
73 }
mtklein@google.comd36522d2013-10-16 13:02:15 +000074}
75
mtklein748ca3b2015-01-15 10:56:12 -080076/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
mtklein114c3cd2015-01-15 10:15:02 -080077
mtklein62bd1a62015-01-27 14:46:26 -080078struct Gold : public SkString {
mtkleina82f5622015-02-20 12:30:19 -080079 Gold() : SkString("") {}
mtklein62bd1a62015-01-27 14:46:26 -080080 Gold(ImplicitString sink, ImplicitString src, ImplicitString name, ImplicitString md5)
81 : SkString("") {
82 this->append(sink);
83 this->append(src);
84 this->append(name);
85 this->append(md5);
86 while (this->size() % 4) {
87 this->append("!"); // Pad out if needed so we can pass this to Murmur3.
88 }
89 }
mtklein62bd1a62015-01-27 14:46:26 -080090 static uint32_t Hash(const Gold& g) {
91 return SkChecksum::Murmur3((const uint32_t*)g.c_str(), g.size());
92 }
93};
mtkleina82f5622015-02-20 12:30:19 -080094static SkTHashSet<Gold, Gold::Hash> gGold;
mtklein62bd1a62015-01-27 14:46:26 -080095
96static void add_gold(JsonWriter::BitmapResult r) {
mtkleina82f5622015-02-20 12:30:19 -080097 gGold.add(Gold(r.config, r.sourceType, r.name, r.md5));
mtklein62bd1a62015-01-27 14:46:26 -080098}
99
100static void gather_gold() {
101 if (!FLAGS_readPath.isEmpty()) {
102 SkString path(FLAGS_readPath[0]);
103 path.append("/dm.json");
104 if (!JsonWriter::ReadJson(path.c_str(), add_gold)) {
105 fail(SkStringPrintf("Couldn't read %s for golden results.", path.c_str()));
106 }
107 }
108}
109
110/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
111
mtklein748ca3b2015-01-15 10:56:12 -0800112template <typename T>
113struct Tagged : public SkAutoTDelete<T> { const char* tag; };
114
115static const bool kMemcpyOK = true;
116
117static SkTArray<Tagged<Src>, kMemcpyOK> gSrcs;
118static SkTArray<Tagged<Sink>, kMemcpyOK> gSinks;
119
120static void push_src(const char* tag, Src* s) {
121 SkAutoTDelete<Src> src(s);
122 if (FLAGS_src.contains(tag) &&
123 !SkCommandLineFlags::ShouldSkip(FLAGS_match, src->name().c_str())) {
124 Tagged<Src>& s = gSrcs.push_back();
125 s.reset(src.detach());
126 s.tag = tag;
mtklein114c3cd2015-01-15 10:15:02 -0800127 }
mtklein748ca3b2015-01-15 10:56:12 -0800128}
mtklein114c3cd2015-01-15 10:15:02 -0800129
mtklein748ca3b2015-01-15 10:56:12 -0800130static void gather_srcs() {
131 for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) {
132 push_src("gm", new GMSrc(r->factory()));
133 }
halcanaryfc37ad12015-01-30 07:31:19 -0800134 for (int i = 0; i < FLAGS_skps.count(); i++) {
135 const char* path = FLAGS_skps[i];
136 if (sk_isdir(path)) {
137 SkOSFile::Iter it(path, "skp");
138 for (SkString file; it.next(&file); ) {
139 push_src("skp", new SKPSrc(SkOSPath::Join(path, file.c_str())));
140 }
141 } else {
mtklein8d17a132015-01-30 11:42:31 -0800142 push_src("skp", new SKPSrc(path));
mtklein114c3cd2015-01-15 10:15:02 -0800143 }
144 }
halcanary23b03c32015-01-30 09:58:58 -0800145 static const char* const exts[] = {
146 "bmp", "gif", "jpg", "jpeg", "png", "webp", "ktx", "astc", "wbmp", "ico",
147 "BMP", "GIF", "JPG", "JPEG", "PNG", "WEBP", "KTX", "ASTC", "WBMP", "ICO",
148 };
149 for (int i = 0; i < FLAGS_images.count(); i++) {
150 const char* flag = FLAGS_images[i];
151 if (sk_isdir(flag)) {
152 for (size_t j = 0; j < SK_ARRAY_COUNT(exts); j++) {
153 SkOSFile::Iter it(flag, exts[j]);
154 for (SkString file; it.next(&file); ) {
155 SkString path = SkOSPath::Join(flag, file.c_str());
156 push_src("image", new ImageSrc(path)); // Decode entire image.
mtkleinedc93bc2015-01-30 13:22:23 -0800157 push_src("subset", new ImageSrc(path, 2)); // Decode into 2 x 2 subsets
halcanary23b03c32015-01-30 09:58:58 -0800158 }
mtklein114c3cd2015-01-15 10:15:02 -0800159 }
halcanary23b03c32015-01-30 09:58:58 -0800160 } else if (sk_exists(flag)) {
161 // assume that FLAGS_images[i] is a valid image if it is a file.
mtklein8d17a132015-01-30 11:42:31 -0800162 push_src("image", new ImageSrc(flag)); // Decode entire image.
mtkleinedc93bc2015-01-30 13:22:23 -0800163 push_src("subset", new ImageSrc(flag, 2)); // Decode into 2 x 2 subsets
mtklein709d2c32015-01-15 08:30:25 -0800164 }
mtklein709d2c32015-01-15 08:30:25 -0800165 }
166}
167
mtklein748ca3b2015-01-15 10:56:12 -0800168static GrGLStandard get_gpu_api() {
169 if (FLAGS_gpuAPI.contains("gl")) { return kGL_GrGLStandard; }
170 if (FLAGS_gpuAPI.contains("gles")) { return kGLES_GrGLStandard; }
171 return kNone_GrGLStandard;
mtklein709d2c32015-01-15 08:30:25 -0800172}
173
mtklein748ca3b2015-01-15 10:56:12 -0800174static void push_sink(const char* tag, Sink* s) {
175 SkAutoTDelete<Sink> sink(s);
176 if (!FLAGS_config.contains(tag)) {
177 return;
mtklein114c3cd2015-01-15 10:15:02 -0800178 }
mtklein748ca3b2015-01-15 10:56:12 -0800179 // Try a noop Src as a canary. If it fails, skip this sink.
180 struct : public Src {
181 Error draw(SkCanvas*) const SK_OVERRIDE { return ""; }
182 SkISize size() const SK_OVERRIDE { return SkISize::Make(16, 16); }
183 Name name() const SK_OVERRIDE { return "noop"; }
184 } noop;
mtklein114c3cd2015-01-15 10:15:02 -0800185
mtklein748ca3b2015-01-15 10:56:12 -0800186 SkBitmap bitmap;
187 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800188 SkString log;
189 Error err = sink->draw(noop, &bitmap, &stream, &log);
mtklein4089ef72015-03-05 08:40:28 -0800190 if (err.isFatal()) {
mtklein748ca3b2015-01-15 10:56:12 -0800191 SkDebugf("Skipping %s: %s\n", tag, err.c_str());
mtklein114c3cd2015-01-15 10:15:02 -0800192 return;
193 }
194
mtklein748ca3b2015-01-15 10:56:12 -0800195 Tagged<Sink>& ts = gSinks.push_back();
196 ts.reset(sink.detach());
197 ts.tag = tag;
198}
199
200static bool gpu_supported() {
201#if SK_SUPPORT_GPU
202 return FLAGS_gpu;
203#else
204 return false;
205#endif
206}
207
208static Sink* create_sink(const char* tag) {
209#define SINK(t, sink, ...) if (0 == strcmp(t, tag)) { return new sink(__VA_ARGS__); }
210 if (gpu_supported()) {
mtklein82d28432015-01-15 12:46:02 -0800211 typedef GrContextFactory Gr;
mtklein748ca3b2015-01-15 10:56:12 -0800212 const GrGLStandard api = get_gpu_api();
mtklein82d28432015-01-15 12:46:02 -0800213 SINK("gpunull", GPUSink, Gr::kNull_GLContextType, api, 0, false, FLAGS_gpu_threading);
214 SINK("gpudebug", GPUSink, Gr::kDebug_GLContextType, api, 0, false, FLAGS_gpu_threading);
215 SINK("gpu", GPUSink, Gr::kNative_GLContextType, api, 0, false, FLAGS_gpu_threading);
216 SINK("gpudft", GPUSink, Gr::kNative_GLContextType, api, 0, true, FLAGS_gpu_threading);
217 SINK("msaa4", GPUSink, Gr::kNative_GLContextType, api, 4, false, FLAGS_gpu_threading);
218 SINK("msaa16", GPUSink, Gr::kNative_GLContextType, api, 16, false, FLAGS_gpu_threading);
219 SINK("nvprmsaa4", GPUSink, Gr::kNVPR_GLContextType, api, 4, false, FLAGS_gpu_threading);
220 SINK("nvprmsaa16", GPUSink, Gr::kNVPR_GLContextType, api, 16, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800221 #if SK_ANGLE
mtklein82d28432015-01-15 12:46:02 -0800222 SINK("angle", GPUSink, Gr::kANGLE_GLContextType, api, 0, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800223 #endif
224 #if SK_MESA
mtklein82d28432015-01-15 12:46:02 -0800225 SINK("mesa", GPUSink, Gr::kMESA_GLContextType, api, 0, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800226 #endif
mtklein114c3cd2015-01-15 10:15:02 -0800227 }
mtklein748ca3b2015-01-15 10:56:12 -0800228
tomhudsoneebc39a2015-02-23 12:18:05 -0800229#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
230 SINK("hwui", HWUISink);
231#endif
232
mtklein748ca3b2015-01-15 10:56:12 -0800233 if (FLAGS_cpu) {
234 SINK("565", RasterSink, kRGB_565_SkColorType);
235 SINK("8888", RasterSink, kN32_SkColorType);
mtklein19f30602015-01-20 13:34:39 -0800236 SINK("pdf", PDFSink);
mtklein9c3f17d2015-01-28 11:35:18 -0800237 SINK("skp", SKPSink);
mtklein8a4527e2015-01-31 20:00:58 -0800238 SINK("svg", SVGSink);
239 SINK("null", NullSink);
halcanary47ef4d52015-03-03 09:13:09 -0800240 SINK("xps", XPSSink);
mtklein748ca3b2015-01-15 10:56:12 -0800241 }
242#undef SINK
243 return NULL;
mtklein114c3cd2015-01-15 10:15:02 -0800244}
245
mtklein748ca3b2015-01-15 10:56:12 -0800246static Sink* create_via(const char* tag, Sink* wrapped) {
247#define VIA(t, via, ...) if (0 == strcmp(t, tag)) { return new via(__VA_ARGS__); }
mtklein7edca212015-01-21 13:18:51 -0800248 VIA("pipe", ViaPipe, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800249 VIA("serialize", ViaSerialization, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800250 VIA("tiles", ViaTiles, 256, 256, NULL, wrapped);
251 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800252
mtkleind603b222015-02-17 11:13:33 -0800253 if (FLAGS_matrix.count() == 4) {
mtklein748ca3b2015-01-15 10:56:12 -0800254 SkMatrix m;
mtkleind603b222015-02-17 11:13:33 -0800255 m.reset();
256 m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
257 m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
258 m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
259 m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
260 VIA("matrix", ViaMatrix, m, wrapped);
261 VIA("upright", ViaUpright, m, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800262 }
tomhudson64de1e12015-03-05 08:01:07 -0800263
264#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
265 VIA("androidsdk", ViaAndroidSDK, wrapped);
266#endif
267
mtklein748ca3b2015-01-15 10:56:12 -0800268#undef VIA
269 return NULL;
mtklein114c3cd2015-01-15 10:15:02 -0800270}
271
mtklein748ca3b2015-01-15 10:56:12 -0800272static void gather_sinks() {
273 for (int i = 0; i < FLAGS_config.count(); i++) {
274 const char* config = FLAGS_config[i];
275 SkTArray<SkString> parts;
276 SkStrSplit(config, "-", &parts);
277
278 Sink* sink = NULL;
279 for (int i = parts.count(); i-- > 0;) {
280 const char* part = parts[i].c_str();
281 Sink* next = (sink == NULL) ? create_sink(part) : create_via(part, sink);
282 if (next == NULL) {
283 SkDebugf("Skipping %s: Don't understand '%s'.\n", config, part);
284 delete sink;
285 sink = NULL;
286 break;
287 }
288 sink = next;
289 }
290 if (sink) {
291 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800292 }
293 }
294}
mtklein709d2c32015-01-15 08:30:25 -0800295
mtkleina2ef6422015-01-15 13:44:22 -0800296static bool match(const char* needle, const char* haystack) {
297 return 0 == strcmp("_", needle) || NULL != strstr(haystack, needle);
298}
299
300static ImplicitString is_blacklisted(const char* sink, const char* src, const char* name) {
301 for (int i = 0; i < FLAGS_blacklist.count() - 2; i += 3) {
302 if (match(FLAGS_blacklist[i+0], sink) &&
303 match(FLAGS_blacklist[i+1], src) &&
304 match(FLAGS_blacklist[i+2], name)) {
305 return SkStringPrintf("%s %s %s",
306 FLAGS_blacklist[i+0], FLAGS_blacklist[i+1], FLAGS_blacklist[i+2]);
307 }
308 }
309 return "";
310}
311
mtklein748ca3b2015-01-15 10:56:12 -0800312// The finest-grained unit of work we can run: draw a single Src into a single Sink,
313// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
314struct Task {
315 Task(const Tagged<Src>& src, const Tagged<Sink>& sink) : src(src), sink(sink) {}
316 const Tagged<Src>& src;
317 const Tagged<Sink>& sink;
318
319 static void Run(Task* task) {
mtkleina2ef6422015-01-15 13:44:22 -0800320 SkString name = task->src->name();
321 SkString whyBlacklisted = is_blacklisted(task->sink.tag, task->src.tag, name.c_str());
mtkleinb9eb4ac2015-02-02 18:26:03 -0800322 SkString log;
mtklein748ca3b2015-01-15 10:56:12 -0800323 WallTimer timer;
324 timer.start();
mtkleina2ef6422015-01-15 13:44:22 -0800325 if (!FLAGS_dryRun && whyBlacklisted.isEmpty()) {
mtklein748ca3b2015-01-15 10:56:12 -0800326 SkBitmap bitmap;
327 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800328 Error err = task->sink->draw(*task->src, &bitmap, &stream, &log);
mtklein748ca3b2015-01-15 10:56:12 -0800329 if (!err.isEmpty()) {
mtklein4089ef72015-03-05 08:40:28 -0800330 timer.end();
331 if (err.isFatal()) {
332 fail(SkStringPrintf("%s %s %s: %s",
333 task->sink.tag,
334 task->src.tag,
335 name.c_str(),
336 err.c_str()));
mtkleina6def472015-03-11 07:19:15 -0700337 } else if (FLAGS_verbose) {
mtklein4089ef72015-03-05 08:40:28 -0800338 name.appendf(" (skipped: %s)", err.c_str());
339 }
340 done(timer.fWall, task->sink.tag, task->src.tag, name, log);
341 return;
mtklein748ca3b2015-01-15 10:56:12 -0800342 }
mtklein62bd1a62015-01-27 14:46:26 -0800343 SkAutoTDelete<SkStreamAsset> data(stream.detachAsStream());
344
345 SkString md5;
346 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
347 SkMD5 hash;
348 if (data->getLength()) {
349 hash.writeStream(data, data->getLength());
350 data->rewind();
351 } else {
352 hash.write(bitmap.getPixels(), bitmap.getSize());
353 }
354 SkMD5::Digest digest;
355 hash.finish(digest);
356 for (int i = 0; i < 16; i++) {
357 md5.appendf("%02x", digest.data[i]);
358 }
359 }
360
361 if (!FLAGS_readPath.isEmpty() &&
mtkleina82f5622015-02-20 12:30:19 -0800362 !gGold.contains(Gold(task->sink.tag, task->src.tag, name, md5))) {
mtklein62bd1a62015-01-27 14:46:26 -0800363 fail(SkStringPrintf("%s not found for %s %s %s in %s",
364 md5.c_str(),
365 task->sink.tag,
366 task->src.tag,
367 name.c_str(),
368 FLAGS_readPath[0]));
369 }
370
mtklein748ca3b2015-01-15 10:56:12 -0800371 if (!FLAGS_writePath.isEmpty()) {
372 const char* ext = task->sink->fileExtension();
mtklein62bd1a62015-01-27 14:46:26 -0800373 if (data->getLength()) {
374 WriteToDisk(*task, md5, ext, data, data->getLength(), NULL);
halcanary022afb82015-01-30 11:00:12 -0800375 SkASSERT(bitmap.drawsNothing());
376 } else if (!bitmap.drawsNothing()) {
mtklein62bd1a62015-01-27 14:46:26 -0800377 WriteToDisk(*task, md5, ext, NULL, 0, &bitmap);
mtklein748ca3b2015-01-15 10:56:12 -0800378 }
379 }
380 }
381 timer.end();
mtkleina6def472015-03-11 07:19:15 -0700382 if (FLAGS_verbose && !whyBlacklisted.isEmpty()) {
mtkleina2ef6422015-01-15 13:44:22 -0800383 name.appendf(" (--blacklist, %s)", whyBlacklisted.c_str());
384 }
mtkleinb9eb4ac2015-02-02 18:26:03 -0800385 done(timer.fWall, task->sink.tag, task->src.tag, name, log);
mtklein748ca3b2015-01-15 10:56:12 -0800386 }
387
388 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -0800389 SkString md5,
390 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -0800391 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -0800392 const SkBitmap* bitmap) {
mtklein748ca3b2015-01-15 10:56:12 -0800393 JsonWriter::BitmapResult result;
394 result.name = task.src->name();
395 result.config = task.sink.tag;
396 result.sourceType = task.src.tag;
397 result.ext = ext;
mtklein62bd1a62015-01-27 14:46:26 -0800398 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -0800399 JsonWriter::AddBitmapResult(result);
400
401 const char* dir = FLAGS_writePath[0];
402 if (0 == strcmp(dir, "@")) { // Needed for iOS.
403 dir = FLAGS_resourcePath[0];
404 }
405 sk_mkdir(dir);
406
407 SkString path;
408 if (FLAGS_nameByHash) {
409 path = SkOSPath::Join(dir, result.md5.c_str());
410 path.append(".");
411 path.append(ext);
412 if (sk_exists(path.c_str())) {
413 return; // Content-addressed. If it exists already, we're done.
414 }
415 } else {
416 path = SkOSPath::Join(dir, task.sink.tag);
417 sk_mkdir(path.c_str());
418 path = SkOSPath::Join(path.c_str(), task.src.tag);
419 sk_mkdir(path.c_str());
420 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
421 path.append(".");
422 path.append(ext);
423 }
424
425 SkFILEWStream file(path.c_str());
426 if (!file.isValid()) {
427 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
428 return;
429 }
430
mtklein748ca3b2015-01-15 10:56:12 -0800431 if (bitmap) {
432 // We can't encode A8 bitmaps as PNGs. Convert them to 8888 first.
433 SkBitmap converted;
434 if (bitmap->info().colorType() == kAlpha_8_SkColorType) {
435 if (!bitmap->copyTo(&converted, kN32_SkColorType)) {
436 fail("Can't convert A8 to 8888.\n");
437 return;
438 }
439 bitmap = &converted;
440 }
441 if (!SkImageEncoder::EncodeStream(&file, *bitmap, SkImageEncoder::kPNG_Type, 100)) {
442 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
443 return;
444 }
445 } else {
446 if (!file.writeStream(data, len)) {
447 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
448 return;
449 }
450 }
451 }
452};
453
454// Run all tasks in the same enclave serially on the same thread.
455// They can't possibly run concurrently with each other.
456static void run_enclave(SkTArray<Task>* tasks) {
457 for (int i = 0; i < tasks->count(); i++) {
458 Task::Run(tasks->begin() + i);
459 }
460}
461
462/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
463
464// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
465
mtklein55e88b22015-01-21 15:50:13 -0800466static SkTDArray<skiatest::Test> gThreadedTests, gGPUTests;
mtklein748ca3b2015-01-15 10:56:12 -0800467
468static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -0800469 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -0800470 return;
471 }
halcanary87f3ba42015-01-20 09:30:20 -0800472 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r;
473 r = r->next()) {
474 // Despite its name, factory() is returning a reference to
475 // link-time static const POD data.
476 const skiatest::Test& test = r->factory();
477 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -0800478 continue;
479 }
halcanary87f3ba42015-01-20 09:30:20 -0800480 if (test.needsGpu && gpu_supported()) {
mtklein55e88b22015-01-21 15:50:13 -0800481 (FLAGS_gpu_threading ? gThreadedTests : gGPUTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -0800482 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein55e88b22015-01-21 15:50:13 -0800483 gThreadedTests.push(test);
mtklein82d28432015-01-15 12:46:02 -0800484 }
mtklein748ca3b2015-01-15 10:56:12 -0800485 }
486}
487
halcanary87f3ba42015-01-20 09:30:20 -0800488static void run_test(skiatest::Test* test) {
489 struct : public skiatest::Reporter {
490 void reportFailed(const skiatest::Failure& failure) SK_OVERRIDE {
491 fail(failure.toString());
492 JsonWriter::AddTestFailure(failure);
493 }
494 bool allowExtendedTest() const SK_OVERRIDE {
495 return FLAGS_pathOpsExtended;
496 }
497 bool verbose() const SK_OVERRIDE { return FLAGS_veryVerbose; }
498 } reporter;
mtklein748ca3b2015-01-15 10:56:12 -0800499 WallTimer timer;
500 timer.start();
mtklein748ca3b2015-01-15 10:56:12 -0800501 if (!FLAGS_dryRun) {
mtklein55e88b22015-01-21 15:50:13 -0800502 GrContextFactory factory;
503 test->proc(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -0800504 }
505 timer.end();
mtkleinb9eb4ac2015-02-02 18:26:03 -0800506 done(timer.fWall, "unit", "test", test->name, "");
mtklein748ca3b2015-01-15 10:56:12 -0800507}
508
509/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
510
mtklein55e88b22015-01-21 15:50:13 -0800511// If we're isolating all GPU-bound work to one thread (the default), this function runs all that.
512static void run_enclave_and_gpu_tests(SkTArray<Task>* tasks) {
513 run_enclave(tasks);
514 for (int i = 0; i < gGPUTests.count(); i++) {
515 run_test(&gGPUTests[i]);
516 }
517}
518
mtkleinde6fc2b2015-03-12 06:28:54 -0700519// Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung.
520// This prints something every once in a while so that it knows we're still working.
521static void keep_alive(void*) {
522 for (;;) {
523 static const int kSec = 300;
524#if defined(SK_BUILD_FOR_WIN)
525 Sleep(kSec * 1000);
526#else
527 sleep(kSec);
528#endif
529 SkDebugf("\nStill alive: doing science, reticulating splines...\n");
530 }
531}
532
jcgregorio3b27ade2014-11-13 08:06:40 -0800533int dm_main();
caryclark17f0b6d2014-07-22 10:15:34 -0700534int dm_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -0800535 SetupCrashHandler();
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +0000536 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -0700537 SkTaskGroup::Enabler enabled(FLAGS_threads);
mtkleine67164d2015-02-02 13:24:37 -0800538 if (FLAGS_leaks) {
539 SkInstCountPrintLeaksOnExit();
540 }
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +0000541
mtkleinde6fc2b2015-03-12 06:28:54 -0700542 SkThread keepAlive(keep_alive); // This thread will just be killed by processes shutdown.
543 keepAlive.start();
544
mtklein62bd1a62015-01-27 14:46:26 -0800545 gather_gold();
546
mtklein748ca3b2015-01-15 10:56:12 -0800547 gather_srcs();
548 gather_sinks();
549 gather_tests();
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +0000550
mtklein55e88b22015-01-21 15:50:13 -0800551 gPending = gSrcs.count() * gSinks.count() + gThreadedTests.count() + gGPUTests.count();
mtklein748ca3b2015-01-15 10:56:12 -0800552 SkDebugf("%d srcs * %d sinks + %d tests == %d tasks\n",
mtklein55e88b22015-01-21 15:50:13 -0800553 gSrcs.count(), gSinks.count(), gThreadedTests.count() + gGPUTests.count(), gPending);
mtklein748ca3b2015-01-15 10:56:12 -0800554
555 // We try to exploit as much parallelism as is safe. Most Src/Sink pairs run on any thread,
556 // but Sinks that identify as part of a particular enclave run serially on a single thread.
mtklein82d28432015-01-15 12:46:02 -0800557 // CPU tests run on any thread. GPU tests depend on --gpu_threading.
mtklein748ca3b2015-01-15 10:56:12 -0800558 SkTArray<Task> enclaves[kNumEnclaves];
559 for (int j = 0; j < gSinks.count(); j++) {
560 SkTArray<Task>& tasks = enclaves[gSinks[j]->enclave()];
561 for (int i = 0; i < gSrcs.count(); i++) {
562 tasks.push_back(Task(gSrcs[i], gSinks[j]));
563 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +0000564 }
565
mtklein748ca3b2015-01-15 10:56:12 -0800566 SkTaskGroup tg;
mtklein55e88b22015-01-21 15:50:13 -0800567 tg.batch(run_test, gThreadedTests.begin(), gThreadedTests.count());
568 for (int i = 0; i < kNumEnclaves; i++) {
569 switch(i) {
570 case kAnyThread_Enclave:
571 tg.batch(Task::Run, enclaves[i].begin(), enclaves[i].count());
572 break;
573 case kGPU_Enclave:
574 tg.add(run_enclave_and_gpu_tests, &enclaves[i]);
575 break;
576 default:
577 tg.add(run_enclave, &enclaves[i]);
578 break;
mtklein82d28432015-01-15 12:46:02 -0800579 }
mtklein55e88b22015-01-21 15:50:13 -0800580 }
mtklein748ca3b2015-01-15 10:56:12 -0800581 tg.wait();
mtklein748ca3b2015-01-15 10:56:12 -0800582 // At this point we're back in single-threaded land.
mtklein@google.comd36522d2013-10-16 13:02:15 +0000583
mtklein2f64eec2015-01-15 14:20:41 -0800584 SkDebugf("\n");
mtklein748ca3b2015-01-15 10:56:12 -0800585 if (gFailures.count() > 0) {
586 SkDebugf("Failures:\n");
587 for (int i = 0; i < gFailures.count(); i++) {
mtklein9dc09102015-01-15 15:47:33 -0800588 SkDebugf("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800589 }
590 SkDebugf("%d failures\n", gFailures.count());
591 return 1;
mtklein114c3cd2015-01-15 10:15:02 -0800592 }
mtklein748ca3b2015-01-15 10:56:12 -0800593 if (gPending > 0) {
594 SkDebugf("Hrm, we didn't seem to run everything we intended to! Please file a bug.\n");
595 return 1;
mtklein114c3cd2015-01-15 10:15:02 -0800596 }
mtklein748ca3b2015-01-15 10:56:12 -0800597 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +0000598}
jcgregorio3b27ade2014-11-13 08:06:40 -0800599
600#if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL)
601int main(int argc, char** argv) {
602 SkCommandLineFlags::Parse(argc, argv);
603 return dm_main();
604}
605#endif