blob: f2b0dfdfcbd7c1e22e0ca761622ba833fdf78246 [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
mtkleinb37cb412015-03-18 05:27:14 -070052static int32_t gPending = 0; // Atomic. Total number of running and queued tasks.
53
54SK_DECLARE_STATIC_MUTEX(gRunningMutex);
55static SkTArray<SkString> gRunning;
mtklein748ca3b2015-01-15 10:56:12 -080056
mtkleinb9eb4ac2015-02-02 18:26:03 -080057static void done(double ms,
58 ImplicitString config, ImplicitString src, ImplicitString name,
mtkleinb37cb412015-03-18 05:27:14 -070059 ImplicitString note, ImplicitString log) {
60 SkString id = SkStringPrintf("%s %s %s", config.c_str(), src.c_str(), name.c_str());
61 {
62 SkAutoMutexAcquire lock(gRunningMutex);
63 for (int i = 0; i < gRunning.count(); i++) {
64 if (gRunning[i] == id) {
65 gRunning.removeShuffle(i);
66 break;
67 }
68 }
69 }
70 if (!FLAGS_verbose) {
71 note = "";
72 }
mtkleinb9eb4ac2015-02-02 18:26:03 -080073 if (!log.isEmpty()) {
74 log.prepend("\n");
75 }
mtklein6dee2ad2015-02-05 09:53:44 -080076 auto pending = sk_atomic_dec(&gPending)-1;
mtkleinb37cb412015-03-18 05:27:14 -070077 SkDebugf("%s(%4dMB %5d) %s\t%s%s%s", FLAGS_verbose ? "\n" : kSkOverwriteLine
78 , sk_tools::getBestResidentSetSizeMB()
79 , pending
80 , HumanizeMs(ms).c_str()
81 , id.c_str()
82 , note.c_str()
83 , log.c_str());
mtkleina17241b2015-01-23 05:48:00 -080084 // We write our dm.json file every once in a while in case we crash.
85 // Notice this also handles the final dm.json when pending == 0.
86 if (pending % 500 == 0) {
87 JsonWriter::DumpJson();
88 }
mtklein@google.comd36522d2013-10-16 13:02:15 +000089}
90
mtkleinb37cb412015-03-18 05:27:14 -070091static void start(ImplicitString config, ImplicitString src, ImplicitString name) {
92 SkString id = SkStringPrintf("%s %s %s", config.c_str(), src.c_str(), name.c_str());
93 SkAutoMutexAcquire lock(gRunningMutex);
94 gRunning.push_back(id);
95}
96
mtklein748ca3b2015-01-15 10:56:12 -080097/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
mtklein114c3cd2015-01-15 10:15:02 -080098
mtklein62bd1a62015-01-27 14:46:26 -080099struct Gold : public SkString {
mtkleina82f5622015-02-20 12:30:19 -0800100 Gold() : SkString("") {}
mtklein62bd1a62015-01-27 14:46:26 -0800101 Gold(ImplicitString sink, ImplicitString src, ImplicitString name, ImplicitString md5)
102 : SkString("") {
103 this->append(sink);
104 this->append(src);
105 this->append(name);
106 this->append(md5);
107 while (this->size() % 4) {
108 this->append("!"); // Pad out if needed so we can pass this to Murmur3.
109 }
110 }
mtklein62bd1a62015-01-27 14:46:26 -0800111 static uint32_t Hash(const Gold& g) {
112 return SkChecksum::Murmur3((const uint32_t*)g.c_str(), g.size());
113 }
114};
mtkleina82f5622015-02-20 12:30:19 -0800115static SkTHashSet<Gold, Gold::Hash> gGold;
mtklein62bd1a62015-01-27 14:46:26 -0800116
117static void add_gold(JsonWriter::BitmapResult r) {
mtkleina82f5622015-02-20 12:30:19 -0800118 gGold.add(Gold(r.config, r.sourceType, r.name, r.md5));
mtklein62bd1a62015-01-27 14:46:26 -0800119}
120
121static void gather_gold() {
122 if (!FLAGS_readPath.isEmpty()) {
123 SkString path(FLAGS_readPath[0]);
124 path.append("/dm.json");
125 if (!JsonWriter::ReadJson(path.c_str(), add_gold)) {
126 fail(SkStringPrintf("Couldn't read %s for golden results.", path.c_str()));
127 }
128 }
129}
130
131/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
132
mtklein748ca3b2015-01-15 10:56:12 -0800133template <typename T>
134struct Tagged : public SkAutoTDelete<T> { const char* tag; };
135
136static const bool kMemcpyOK = true;
137
138static SkTArray<Tagged<Src>, kMemcpyOK> gSrcs;
139static SkTArray<Tagged<Sink>, kMemcpyOK> gSinks;
140
141static void push_src(const char* tag, Src* s) {
142 SkAutoTDelete<Src> src(s);
143 if (FLAGS_src.contains(tag) &&
144 !SkCommandLineFlags::ShouldSkip(FLAGS_match, src->name().c_str())) {
145 Tagged<Src>& s = gSrcs.push_back();
146 s.reset(src.detach());
147 s.tag = tag;
mtklein114c3cd2015-01-15 10:15:02 -0800148 }
mtklein748ca3b2015-01-15 10:56:12 -0800149}
mtklein114c3cd2015-01-15 10:15:02 -0800150
mtklein748ca3b2015-01-15 10:56:12 -0800151static void gather_srcs() {
152 for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) {
153 push_src("gm", new GMSrc(r->factory()));
154 }
halcanaryfc37ad12015-01-30 07:31:19 -0800155 for (int i = 0; i < FLAGS_skps.count(); i++) {
156 const char* path = FLAGS_skps[i];
157 if (sk_isdir(path)) {
158 SkOSFile::Iter it(path, "skp");
159 for (SkString file; it.next(&file); ) {
160 push_src("skp", new SKPSrc(SkOSPath::Join(path, file.c_str())));
161 }
162 } else {
mtklein8d17a132015-01-30 11:42:31 -0800163 push_src("skp", new SKPSrc(path));
mtklein114c3cd2015-01-15 10:15:02 -0800164 }
165 }
halcanary23b03c32015-01-30 09:58:58 -0800166 static const char* const exts[] = {
167 "bmp", "gif", "jpg", "jpeg", "png", "webp", "ktx", "astc", "wbmp", "ico",
168 "BMP", "GIF", "JPG", "JPEG", "PNG", "WEBP", "KTX", "ASTC", "WBMP", "ICO",
169 };
170 for (int i = 0; i < FLAGS_images.count(); i++) {
171 const char* flag = FLAGS_images[i];
172 if (sk_isdir(flag)) {
173 for (size_t j = 0; j < SK_ARRAY_COUNT(exts); j++) {
174 SkOSFile::Iter it(flag, exts[j]);
175 for (SkString file; it.next(&file); ) {
176 SkString path = SkOSPath::Join(flag, file.c_str());
177 push_src("image", new ImageSrc(path)); // Decode entire image.
mtkleinedc93bc2015-01-30 13:22:23 -0800178 push_src("subset", new ImageSrc(path, 2)); // Decode into 2 x 2 subsets
halcanary23b03c32015-01-30 09:58:58 -0800179 }
mtklein114c3cd2015-01-15 10:15:02 -0800180 }
halcanary23b03c32015-01-30 09:58:58 -0800181 } else if (sk_exists(flag)) {
182 // assume that FLAGS_images[i] is a valid image if it is a file.
mtklein8d17a132015-01-30 11:42:31 -0800183 push_src("image", new ImageSrc(flag)); // Decode entire image.
mtkleinedc93bc2015-01-30 13:22:23 -0800184 push_src("subset", new ImageSrc(flag, 2)); // Decode into 2 x 2 subsets
mtklein709d2c32015-01-15 08:30:25 -0800185 }
mtklein709d2c32015-01-15 08:30:25 -0800186 }
187}
188
mtklein748ca3b2015-01-15 10:56:12 -0800189static GrGLStandard get_gpu_api() {
190 if (FLAGS_gpuAPI.contains("gl")) { return kGL_GrGLStandard; }
191 if (FLAGS_gpuAPI.contains("gles")) { return kGLES_GrGLStandard; }
192 return kNone_GrGLStandard;
mtklein709d2c32015-01-15 08:30:25 -0800193}
194
mtklein748ca3b2015-01-15 10:56:12 -0800195static void push_sink(const char* tag, Sink* s) {
196 SkAutoTDelete<Sink> sink(s);
197 if (!FLAGS_config.contains(tag)) {
198 return;
mtklein114c3cd2015-01-15 10:15:02 -0800199 }
mtklein748ca3b2015-01-15 10:56:12 -0800200 // Try a noop Src as a canary. If it fails, skip this sink.
201 struct : public Src {
202 Error draw(SkCanvas*) const SK_OVERRIDE { return ""; }
203 SkISize size() const SK_OVERRIDE { return SkISize::Make(16, 16); }
204 Name name() const SK_OVERRIDE { return "noop"; }
205 } noop;
mtklein114c3cd2015-01-15 10:15:02 -0800206
mtklein748ca3b2015-01-15 10:56:12 -0800207 SkBitmap bitmap;
208 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800209 SkString log;
210 Error err = sink->draw(noop, &bitmap, &stream, &log);
mtklein4089ef72015-03-05 08:40:28 -0800211 if (err.isFatal()) {
mtklein748ca3b2015-01-15 10:56:12 -0800212 SkDebugf("Skipping %s: %s\n", tag, err.c_str());
mtklein114c3cd2015-01-15 10:15:02 -0800213 return;
214 }
215
mtklein748ca3b2015-01-15 10:56:12 -0800216 Tagged<Sink>& ts = gSinks.push_back();
217 ts.reset(sink.detach());
218 ts.tag = tag;
219}
220
221static bool gpu_supported() {
222#if SK_SUPPORT_GPU
223 return FLAGS_gpu;
224#else
225 return false;
226#endif
227}
228
229static Sink* create_sink(const char* tag) {
230#define SINK(t, sink, ...) if (0 == strcmp(t, tag)) { return new sink(__VA_ARGS__); }
231 if (gpu_supported()) {
mtklein82d28432015-01-15 12:46:02 -0800232 typedef GrContextFactory Gr;
mtklein748ca3b2015-01-15 10:56:12 -0800233 const GrGLStandard api = get_gpu_api();
mtklein82d28432015-01-15 12:46:02 -0800234 SINK("gpunull", GPUSink, Gr::kNull_GLContextType, api, 0, false, FLAGS_gpu_threading);
235 SINK("gpudebug", GPUSink, Gr::kDebug_GLContextType, api, 0, false, FLAGS_gpu_threading);
236 SINK("gpu", GPUSink, Gr::kNative_GLContextType, api, 0, false, FLAGS_gpu_threading);
237 SINK("gpudft", GPUSink, Gr::kNative_GLContextType, api, 0, true, FLAGS_gpu_threading);
238 SINK("msaa4", GPUSink, Gr::kNative_GLContextType, api, 4, false, FLAGS_gpu_threading);
239 SINK("msaa16", GPUSink, Gr::kNative_GLContextType, api, 16, false, FLAGS_gpu_threading);
240 SINK("nvprmsaa4", GPUSink, Gr::kNVPR_GLContextType, api, 4, false, FLAGS_gpu_threading);
241 SINK("nvprmsaa16", GPUSink, Gr::kNVPR_GLContextType, api, 16, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800242 #if SK_ANGLE
mtklein82d28432015-01-15 12:46:02 -0800243 SINK("angle", GPUSink, Gr::kANGLE_GLContextType, api, 0, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800244 #endif
245 #if SK_MESA
mtklein82d28432015-01-15 12:46:02 -0800246 SINK("mesa", GPUSink, Gr::kMESA_GLContextType, api, 0, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800247 #endif
mtklein114c3cd2015-01-15 10:15:02 -0800248 }
mtklein748ca3b2015-01-15 10:56:12 -0800249
tomhudsoneebc39a2015-02-23 12:18:05 -0800250#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
251 SINK("hwui", HWUISink);
252#endif
253
mtklein748ca3b2015-01-15 10:56:12 -0800254 if (FLAGS_cpu) {
255 SINK("565", RasterSink, kRGB_565_SkColorType);
256 SINK("8888", RasterSink, kN32_SkColorType);
mtklein19f30602015-01-20 13:34:39 -0800257 SINK("pdf", PDFSink);
mtklein9c3f17d2015-01-28 11:35:18 -0800258 SINK("skp", SKPSink);
mtklein8a4527e2015-01-31 20:00:58 -0800259 SINK("svg", SVGSink);
260 SINK("null", NullSink);
halcanary47ef4d52015-03-03 09:13:09 -0800261 SINK("xps", XPSSink);
mtklein748ca3b2015-01-15 10:56:12 -0800262 }
263#undef SINK
264 return NULL;
mtklein114c3cd2015-01-15 10:15:02 -0800265}
266
mtklein748ca3b2015-01-15 10:56:12 -0800267static Sink* create_via(const char* tag, Sink* wrapped) {
268#define VIA(t, via, ...) if (0 == strcmp(t, tag)) { return new via(__VA_ARGS__); }
mtklein7edca212015-01-21 13:18:51 -0800269 VIA("pipe", ViaPipe, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800270 VIA("serialize", ViaSerialization, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800271 VIA("tiles", ViaTiles, 256, 256, NULL, wrapped);
272 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800273
mtkleind603b222015-02-17 11:13:33 -0800274 if (FLAGS_matrix.count() == 4) {
mtklein748ca3b2015-01-15 10:56:12 -0800275 SkMatrix m;
mtkleind603b222015-02-17 11:13:33 -0800276 m.reset();
277 m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
278 m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
279 m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
280 m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
281 VIA("matrix", ViaMatrix, m, wrapped);
282 VIA("upright", ViaUpright, m, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800283 }
tomhudson64de1e12015-03-05 08:01:07 -0800284
285#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
286 VIA("androidsdk", ViaAndroidSDK, wrapped);
287#endif
288
mtklein748ca3b2015-01-15 10:56:12 -0800289#undef VIA
290 return NULL;
mtklein114c3cd2015-01-15 10:15:02 -0800291}
292
mtklein748ca3b2015-01-15 10:56:12 -0800293static void gather_sinks() {
294 for (int i = 0; i < FLAGS_config.count(); i++) {
295 const char* config = FLAGS_config[i];
296 SkTArray<SkString> parts;
297 SkStrSplit(config, "-", &parts);
298
299 Sink* sink = NULL;
300 for (int i = parts.count(); i-- > 0;) {
301 const char* part = parts[i].c_str();
302 Sink* next = (sink == NULL) ? create_sink(part) : create_via(part, sink);
303 if (next == NULL) {
304 SkDebugf("Skipping %s: Don't understand '%s'.\n", config, part);
305 delete sink;
306 sink = NULL;
307 break;
308 }
309 sink = next;
310 }
311 if (sink) {
312 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800313 }
314 }
315}
mtklein709d2c32015-01-15 08:30:25 -0800316
mtkleina2ef6422015-01-15 13:44:22 -0800317static bool match(const char* needle, const char* haystack) {
318 return 0 == strcmp("_", needle) || NULL != strstr(haystack, needle);
319}
320
321static ImplicitString is_blacklisted(const char* sink, const char* src, const char* name) {
322 for (int i = 0; i < FLAGS_blacklist.count() - 2; i += 3) {
323 if (match(FLAGS_blacklist[i+0], sink) &&
324 match(FLAGS_blacklist[i+1], src) &&
325 match(FLAGS_blacklist[i+2], name)) {
326 return SkStringPrintf("%s %s %s",
327 FLAGS_blacklist[i+0], FLAGS_blacklist[i+1], FLAGS_blacklist[i+2]);
328 }
329 }
330 return "";
331}
332
mtklein748ca3b2015-01-15 10:56:12 -0800333// The finest-grained unit of work we can run: draw a single Src into a single Sink,
334// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
335struct Task {
336 Task(const Tagged<Src>& src, const Tagged<Sink>& sink) : src(src), sink(sink) {}
337 const Tagged<Src>& src;
338 const Tagged<Sink>& sink;
339
340 static void Run(Task* task) {
mtkleina2ef6422015-01-15 13:44:22 -0800341 SkString name = task->src->name();
mtkleinb37cb412015-03-18 05:27:14 -0700342 SkString note;
mtkleina2ef6422015-01-15 13:44:22 -0800343 SkString whyBlacklisted = is_blacklisted(task->sink.tag, task->src.tag, name.c_str());
mtkleinb37cb412015-03-18 05:27:14 -0700344 if (!whyBlacklisted.isEmpty()) {
345 note.appendf(" (--blacklist %s)", whyBlacklisted.c_str());
346 }
mtkleinb9eb4ac2015-02-02 18:26:03 -0800347 SkString log;
mtklein748ca3b2015-01-15 10:56:12 -0800348 WallTimer timer;
349 timer.start();
mtkleina2ef6422015-01-15 13:44:22 -0800350 if (!FLAGS_dryRun && whyBlacklisted.isEmpty()) {
mtklein748ca3b2015-01-15 10:56:12 -0800351 SkBitmap bitmap;
352 SkDynamicMemoryWStream stream;
mtkleinb37cb412015-03-18 05:27:14 -0700353 start(task->sink.tag, task->src.tag, name.c_str());
mtkleinb9eb4ac2015-02-02 18:26:03 -0800354 Error err = task->sink->draw(*task->src, &bitmap, &stream, &log);
mtklein748ca3b2015-01-15 10:56:12 -0800355 if (!err.isEmpty()) {
mtklein4089ef72015-03-05 08:40:28 -0800356 timer.end();
357 if (err.isFatal()) {
358 fail(SkStringPrintf("%s %s %s: %s",
359 task->sink.tag,
360 task->src.tag,
361 name.c_str(),
362 err.c_str()));
mtkleinb37cb412015-03-18 05:27:14 -0700363 } else {
364 note.appendf(" (skipped: %s)", err.c_str());
mtklein4089ef72015-03-05 08:40:28 -0800365 }
mtkleinb37cb412015-03-18 05:27:14 -0700366 done(timer.fWall, task->sink.tag, task->src.tag, name, note, log);
mtklein4089ef72015-03-05 08:40:28 -0800367 return;
mtklein748ca3b2015-01-15 10:56:12 -0800368 }
mtklein62bd1a62015-01-27 14:46:26 -0800369 SkAutoTDelete<SkStreamAsset> data(stream.detachAsStream());
370
371 SkString md5;
372 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
373 SkMD5 hash;
374 if (data->getLength()) {
375 hash.writeStream(data, data->getLength());
376 data->rewind();
377 } else {
378 hash.write(bitmap.getPixels(), bitmap.getSize());
379 }
380 SkMD5::Digest digest;
381 hash.finish(digest);
382 for (int i = 0; i < 16; i++) {
383 md5.appendf("%02x", digest.data[i]);
384 }
385 }
386
387 if (!FLAGS_readPath.isEmpty() &&
mtkleina82f5622015-02-20 12:30:19 -0800388 !gGold.contains(Gold(task->sink.tag, task->src.tag, name, md5))) {
mtklein62bd1a62015-01-27 14:46:26 -0800389 fail(SkStringPrintf("%s not found for %s %s %s in %s",
390 md5.c_str(),
391 task->sink.tag,
392 task->src.tag,
393 name.c_str(),
394 FLAGS_readPath[0]));
395 }
396
mtklein748ca3b2015-01-15 10:56:12 -0800397 if (!FLAGS_writePath.isEmpty()) {
398 const char* ext = task->sink->fileExtension();
mtklein62bd1a62015-01-27 14:46:26 -0800399 if (data->getLength()) {
400 WriteToDisk(*task, md5, ext, data, data->getLength(), NULL);
halcanary022afb82015-01-30 11:00:12 -0800401 SkASSERT(bitmap.drawsNothing());
402 } else if (!bitmap.drawsNothing()) {
mtklein62bd1a62015-01-27 14:46:26 -0800403 WriteToDisk(*task, md5, ext, NULL, 0, &bitmap);
mtklein748ca3b2015-01-15 10:56:12 -0800404 }
405 }
406 }
407 timer.end();
mtkleinb37cb412015-03-18 05:27:14 -0700408 done(timer.fWall, task->sink.tag, task->src.tag, name, note, log);
mtklein748ca3b2015-01-15 10:56:12 -0800409 }
410
411 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -0800412 SkString md5,
413 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -0800414 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -0800415 const SkBitmap* bitmap) {
mtklein748ca3b2015-01-15 10:56:12 -0800416 JsonWriter::BitmapResult result;
417 result.name = task.src->name();
418 result.config = task.sink.tag;
419 result.sourceType = task.src.tag;
420 result.ext = ext;
mtklein62bd1a62015-01-27 14:46:26 -0800421 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -0800422 JsonWriter::AddBitmapResult(result);
423
424 const char* dir = FLAGS_writePath[0];
425 if (0 == strcmp(dir, "@")) { // Needed for iOS.
426 dir = FLAGS_resourcePath[0];
427 }
428 sk_mkdir(dir);
429
430 SkString path;
431 if (FLAGS_nameByHash) {
432 path = SkOSPath::Join(dir, result.md5.c_str());
433 path.append(".");
434 path.append(ext);
435 if (sk_exists(path.c_str())) {
436 return; // Content-addressed. If it exists already, we're done.
437 }
438 } else {
439 path = SkOSPath::Join(dir, task.sink.tag);
440 sk_mkdir(path.c_str());
441 path = SkOSPath::Join(path.c_str(), task.src.tag);
442 sk_mkdir(path.c_str());
443 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
444 path.append(".");
445 path.append(ext);
446 }
447
448 SkFILEWStream file(path.c_str());
449 if (!file.isValid()) {
450 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
451 return;
452 }
453
mtklein748ca3b2015-01-15 10:56:12 -0800454 if (bitmap) {
455 // We can't encode A8 bitmaps as PNGs. Convert them to 8888 first.
456 SkBitmap converted;
457 if (bitmap->info().colorType() == kAlpha_8_SkColorType) {
458 if (!bitmap->copyTo(&converted, kN32_SkColorType)) {
459 fail("Can't convert A8 to 8888.\n");
460 return;
461 }
462 bitmap = &converted;
463 }
464 if (!SkImageEncoder::EncodeStream(&file, *bitmap, SkImageEncoder::kPNG_Type, 100)) {
465 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
466 return;
467 }
468 } else {
469 if (!file.writeStream(data, len)) {
470 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
471 return;
472 }
473 }
474 }
475};
476
477// Run all tasks in the same enclave serially on the same thread.
478// They can't possibly run concurrently with each other.
479static void run_enclave(SkTArray<Task>* tasks) {
480 for (int i = 0; i < tasks->count(); i++) {
481 Task::Run(tasks->begin() + i);
482 }
483}
484
485/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
486
487// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
488
mtklein55e88b22015-01-21 15:50:13 -0800489static SkTDArray<skiatest::Test> gThreadedTests, gGPUTests;
mtklein748ca3b2015-01-15 10:56:12 -0800490
491static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -0800492 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -0800493 return;
494 }
halcanary87f3ba42015-01-20 09:30:20 -0800495 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r;
496 r = r->next()) {
497 // Despite its name, factory() is returning a reference to
498 // link-time static const POD data.
499 const skiatest::Test& test = r->factory();
500 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -0800501 continue;
502 }
halcanary87f3ba42015-01-20 09:30:20 -0800503 if (test.needsGpu && gpu_supported()) {
mtklein55e88b22015-01-21 15:50:13 -0800504 (FLAGS_gpu_threading ? gThreadedTests : gGPUTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -0800505 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein55e88b22015-01-21 15:50:13 -0800506 gThreadedTests.push(test);
mtklein82d28432015-01-15 12:46:02 -0800507 }
mtklein748ca3b2015-01-15 10:56:12 -0800508 }
509}
510
halcanary87f3ba42015-01-20 09:30:20 -0800511static void run_test(skiatest::Test* test) {
512 struct : public skiatest::Reporter {
513 void reportFailed(const skiatest::Failure& failure) SK_OVERRIDE {
514 fail(failure.toString());
515 JsonWriter::AddTestFailure(failure);
516 }
517 bool allowExtendedTest() const SK_OVERRIDE {
518 return FLAGS_pathOpsExtended;
519 }
520 bool verbose() const SK_OVERRIDE { return FLAGS_veryVerbose; }
521 } reporter;
mtklein748ca3b2015-01-15 10:56:12 -0800522 WallTimer timer;
523 timer.start();
mtklein748ca3b2015-01-15 10:56:12 -0800524 if (!FLAGS_dryRun) {
mtkleinb37cb412015-03-18 05:27:14 -0700525 start("unit", "test", test->name);
mtklein55e88b22015-01-21 15:50:13 -0800526 GrContextFactory factory;
527 test->proc(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -0800528 }
529 timer.end();
mtkleinb37cb412015-03-18 05:27:14 -0700530 done(timer.fWall, "unit", "test", test->name, "", "");
mtklein748ca3b2015-01-15 10:56:12 -0800531}
532
533/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
534
mtklein55e88b22015-01-21 15:50:13 -0800535// If we're isolating all GPU-bound work to one thread (the default), this function runs all that.
536static void run_enclave_and_gpu_tests(SkTArray<Task>* tasks) {
537 run_enclave(tasks);
538 for (int i = 0; i < gGPUTests.count(); i++) {
539 run_test(&gGPUTests[i]);
540 }
541}
542
mtkleinde6fc2b2015-03-12 06:28:54 -0700543// Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung.
544// This prints something every once in a while so that it knows we're still working.
mtklein2e1c47e2015-03-12 07:16:56 -0700545static void start_keepalive() {
546 struct Loop {
547 static void forever(void*) {
548 for (;;) {
549 static const int kSec = 300;
550 #if defined(SK_BUILD_FOR_WIN)
551 Sleep(kSec * 1000);
552 #else
553 sleep(kSec);
554 #endif
mtkleinb37cb412015-03-18 05:27:14 -0700555 SkString running;
556 {
557 SkAutoMutexAcquire lock(gRunningMutex);
558 for (int i = 0; i < gRunning.count(); i++) {
559 running.appendf("\n\t%s", gRunning[i].c_str());
560 }
561 }
562 SkDebugf("\nCurrently running:%s\n", running.c_str());
mtklein2e1c47e2015-03-12 07:16:56 -0700563 }
564 }
565 };
566 static SkThread* intentionallyLeaked = new SkThread(Loop::forever);
567 intentionallyLeaked->start();
mtkleinde6fc2b2015-03-12 06:28:54 -0700568}
569
jcgregorio3b27ade2014-11-13 08:06:40 -0800570int dm_main();
caryclark17f0b6d2014-07-22 10:15:34 -0700571int dm_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -0800572 SetupCrashHandler();
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +0000573 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -0700574 SkTaskGroup::Enabler enabled(FLAGS_threads);
mtkleine67164d2015-02-02 13:24:37 -0800575 if (FLAGS_leaks) {
576 SkInstCountPrintLeaksOnExit();
577 }
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +0000578
mtklein2e1c47e2015-03-12 07:16:56 -0700579 start_keepalive();
mtkleinde6fc2b2015-03-12 06:28:54 -0700580
mtklein62bd1a62015-01-27 14:46:26 -0800581 gather_gold();
582
mtklein748ca3b2015-01-15 10:56:12 -0800583 gather_srcs();
584 gather_sinks();
585 gather_tests();
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +0000586
mtklein55e88b22015-01-21 15:50:13 -0800587 gPending = gSrcs.count() * gSinks.count() + gThreadedTests.count() + gGPUTests.count();
mtklein748ca3b2015-01-15 10:56:12 -0800588 SkDebugf("%d srcs * %d sinks + %d tests == %d tasks\n",
mtklein55e88b22015-01-21 15:50:13 -0800589 gSrcs.count(), gSinks.count(), gThreadedTests.count() + gGPUTests.count(), gPending);
mtklein748ca3b2015-01-15 10:56:12 -0800590
591 // We try to exploit as much parallelism as is safe. Most Src/Sink pairs run on any thread,
592 // but Sinks that identify as part of a particular enclave run serially on a single thread.
mtklein82d28432015-01-15 12:46:02 -0800593 // CPU tests run on any thread. GPU tests depend on --gpu_threading.
mtklein748ca3b2015-01-15 10:56:12 -0800594 SkTArray<Task> enclaves[kNumEnclaves];
595 for (int j = 0; j < gSinks.count(); j++) {
596 SkTArray<Task>& tasks = enclaves[gSinks[j]->enclave()];
597 for (int i = 0; i < gSrcs.count(); i++) {
598 tasks.push_back(Task(gSrcs[i], gSinks[j]));
599 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +0000600 }
601
mtklein748ca3b2015-01-15 10:56:12 -0800602 SkTaskGroup tg;
mtklein55e88b22015-01-21 15:50:13 -0800603 tg.batch(run_test, gThreadedTests.begin(), gThreadedTests.count());
604 for (int i = 0; i < kNumEnclaves; i++) {
605 switch(i) {
606 case kAnyThread_Enclave:
607 tg.batch(Task::Run, enclaves[i].begin(), enclaves[i].count());
608 break;
609 case kGPU_Enclave:
610 tg.add(run_enclave_and_gpu_tests, &enclaves[i]);
611 break;
612 default:
613 tg.add(run_enclave, &enclaves[i]);
614 break;
mtklein82d28432015-01-15 12:46:02 -0800615 }
mtklein55e88b22015-01-21 15:50:13 -0800616 }
mtklein748ca3b2015-01-15 10:56:12 -0800617 tg.wait();
mtklein748ca3b2015-01-15 10:56:12 -0800618 // At this point we're back in single-threaded land.
mtklein@google.comd36522d2013-10-16 13:02:15 +0000619
mtklein2f64eec2015-01-15 14:20:41 -0800620 SkDebugf("\n");
mtklein748ca3b2015-01-15 10:56:12 -0800621 if (gFailures.count() > 0) {
622 SkDebugf("Failures:\n");
623 for (int i = 0; i < gFailures.count(); i++) {
mtklein9dc09102015-01-15 15:47:33 -0800624 SkDebugf("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800625 }
626 SkDebugf("%d failures\n", gFailures.count());
627 return 1;
mtklein114c3cd2015-01-15 10:15:02 -0800628 }
mtklein748ca3b2015-01-15 10:56:12 -0800629 if (gPending > 0) {
630 SkDebugf("Hrm, we didn't seem to run everything we intended to! Please file a bug.\n");
631 return 1;
mtklein114c3cd2015-01-15 10:15:02 -0800632 }
mtklein748ca3b2015-01-15 10:56:12 -0800633 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +0000634}
jcgregorio3b27ade2014-11-13 08:06:40 -0800635
636#if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL)
637int main(int argc, char** argv) {
638 SkCommandLineFlags::Parse(argc, argv);
639 return dm_main();
640}
641#endif