blob: 109a00de442e9f277fcd0d8276ed2e8ee659ac62 [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
scroggo9b77ddd2015-03-19 06:03:39 -070021DEFINE_string(src, "tests gm skp image subset codec", "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
scroggo9b77ddd2015-03-19 06:03:39 -0700151static bool codec_supported(const char* ext) {
152 // FIXME: Once other versions of SkCodec are available, we can add them to this
153 // list (and eventually we can remove this check once they are all supported).
154 return strcmp(ext, "png") == 0 || strcmp(ext, "PNG") == 0;
155}
156
mtklein748ca3b2015-01-15 10:56:12 -0800157static void gather_srcs() {
158 for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) {
159 push_src("gm", new GMSrc(r->factory()));
160 }
halcanaryfc37ad12015-01-30 07:31:19 -0800161 for (int i = 0; i < FLAGS_skps.count(); i++) {
162 const char* path = FLAGS_skps[i];
163 if (sk_isdir(path)) {
164 SkOSFile::Iter it(path, "skp");
165 for (SkString file; it.next(&file); ) {
166 push_src("skp", new SKPSrc(SkOSPath::Join(path, file.c_str())));
167 }
168 } else {
mtklein8d17a132015-01-30 11:42:31 -0800169 push_src("skp", new SKPSrc(path));
mtklein114c3cd2015-01-15 10:15:02 -0800170 }
171 }
halcanary23b03c32015-01-30 09:58:58 -0800172 static const char* const exts[] = {
173 "bmp", "gif", "jpg", "jpeg", "png", "webp", "ktx", "astc", "wbmp", "ico",
174 "BMP", "GIF", "JPG", "JPEG", "PNG", "WEBP", "KTX", "ASTC", "WBMP", "ICO",
175 };
176 for (int i = 0; i < FLAGS_images.count(); i++) {
177 const char* flag = FLAGS_images[i];
178 if (sk_isdir(flag)) {
179 for (size_t j = 0; j < SK_ARRAY_COUNT(exts); j++) {
180 SkOSFile::Iter it(flag, exts[j]);
181 for (SkString file; it.next(&file); ) {
182 SkString path = SkOSPath::Join(flag, file.c_str());
183 push_src("image", new ImageSrc(path)); // Decode entire image.
mtkleinedc93bc2015-01-30 13:22:23 -0800184 push_src("subset", new ImageSrc(path, 2)); // Decode into 2 x 2 subsets
scroggo9b77ddd2015-03-19 06:03:39 -0700185 if (codec_supported(exts[j])) {
186 push_src("codec", new CodecSrc(path)); // Decode with SkCodec.
187 }
halcanary23b03c32015-01-30 09:58:58 -0800188 }
mtklein114c3cd2015-01-15 10:15:02 -0800189 }
halcanary23b03c32015-01-30 09:58:58 -0800190 } else if (sk_exists(flag)) {
191 // assume that FLAGS_images[i] is a valid image if it is a file.
mtklein8d17a132015-01-30 11:42:31 -0800192 push_src("image", new ImageSrc(flag)); // Decode entire image.
mtkleinedc93bc2015-01-30 13:22:23 -0800193 push_src("subset", new ImageSrc(flag, 2)); // Decode into 2 x 2 subsets
scroggo9b77ddd2015-03-19 06:03:39 -0700194 push_src("codec", new CodecSrc(flag)); // Decode with SkCodec.
mtklein709d2c32015-01-15 08:30:25 -0800195 }
mtklein709d2c32015-01-15 08:30:25 -0800196 }
197}
198
mtklein748ca3b2015-01-15 10:56:12 -0800199static GrGLStandard get_gpu_api() {
200 if (FLAGS_gpuAPI.contains("gl")) { return kGL_GrGLStandard; }
201 if (FLAGS_gpuAPI.contains("gles")) { return kGLES_GrGLStandard; }
202 return kNone_GrGLStandard;
mtklein709d2c32015-01-15 08:30:25 -0800203}
204
mtklein748ca3b2015-01-15 10:56:12 -0800205static void push_sink(const char* tag, Sink* s) {
206 SkAutoTDelete<Sink> sink(s);
207 if (!FLAGS_config.contains(tag)) {
208 return;
mtklein114c3cd2015-01-15 10:15:02 -0800209 }
mtklein748ca3b2015-01-15 10:56:12 -0800210 // Try a noop Src as a canary. If it fails, skip this sink.
211 struct : public Src {
212 Error draw(SkCanvas*) const SK_OVERRIDE { return ""; }
213 SkISize size() const SK_OVERRIDE { return SkISize::Make(16, 16); }
214 Name name() const SK_OVERRIDE { return "noop"; }
215 } noop;
mtklein114c3cd2015-01-15 10:15:02 -0800216
mtklein748ca3b2015-01-15 10:56:12 -0800217 SkBitmap bitmap;
218 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800219 SkString log;
220 Error err = sink->draw(noop, &bitmap, &stream, &log);
mtklein4089ef72015-03-05 08:40:28 -0800221 if (err.isFatal()) {
mtklein748ca3b2015-01-15 10:56:12 -0800222 SkDebugf("Skipping %s: %s\n", tag, err.c_str());
mtklein114c3cd2015-01-15 10:15:02 -0800223 return;
224 }
225
mtklein748ca3b2015-01-15 10:56:12 -0800226 Tagged<Sink>& ts = gSinks.push_back();
227 ts.reset(sink.detach());
228 ts.tag = tag;
229}
230
231static bool gpu_supported() {
232#if SK_SUPPORT_GPU
233 return FLAGS_gpu;
234#else
235 return false;
236#endif
237}
238
239static Sink* create_sink(const char* tag) {
240#define SINK(t, sink, ...) if (0 == strcmp(t, tag)) { return new sink(__VA_ARGS__); }
241 if (gpu_supported()) {
mtklein82d28432015-01-15 12:46:02 -0800242 typedef GrContextFactory Gr;
mtklein748ca3b2015-01-15 10:56:12 -0800243 const GrGLStandard api = get_gpu_api();
mtklein82d28432015-01-15 12:46:02 -0800244 SINK("gpunull", GPUSink, Gr::kNull_GLContextType, api, 0, false, FLAGS_gpu_threading);
245 SINK("gpudebug", GPUSink, Gr::kDebug_GLContextType, api, 0, false, FLAGS_gpu_threading);
246 SINK("gpu", GPUSink, Gr::kNative_GLContextType, api, 0, false, FLAGS_gpu_threading);
247 SINK("gpudft", GPUSink, Gr::kNative_GLContextType, api, 0, true, FLAGS_gpu_threading);
248 SINK("msaa4", GPUSink, Gr::kNative_GLContextType, api, 4, false, FLAGS_gpu_threading);
249 SINK("msaa16", GPUSink, Gr::kNative_GLContextType, api, 16, false, FLAGS_gpu_threading);
250 SINK("nvprmsaa4", GPUSink, Gr::kNVPR_GLContextType, api, 4, false, FLAGS_gpu_threading);
251 SINK("nvprmsaa16", GPUSink, Gr::kNVPR_GLContextType, api, 16, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800252 #if SK_ANGLE
mtklein82d28432015-01-15 12:46:02 -0800253 SINK("angle", GPUSink, Gr::kANGLE_GLContextType, api, 0, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800254 #endif
255 #if SK_MESA
mtklein82d28432015-01-15 12:46:02 -0800256 SINK("mesa", GPUSink, Gr::kMESA_GLContextType, api, 0, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800257 #endif
mtklein114c3cd2015-01-15 10:15:02 -0800258 }
mtklein748ca3b2015-01-15 10:56:12 -0800259
tomhudsoneebc39a2015-02-23 12:18:05 -0800260#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
261 SINK("hwui", HWUISink);
262#endif
263
mtklein748ca3b2015-01-15 10:56:12 -0800264 if (FLAGS_cpu) {
265 SINK("565", RasterSink, kRGB_565_SkColorType);
266 SINK("8888", RasterSink, kN32_SkColorType);
mtklein19f30602015-01-20 13:34:39 -0800267 SINK("pdf", PDFSink);
mtklein9c3f17d2015-01-28 11:35:18 -0800268 SINK("skp", SKPSink);
mtklein8a4527e2015-01-31 20:00:58 -0800269 SINK("svg", SVGSink);
270 SINK("null", NullSink);
halcanary47ef4d52015-03-03 09:13:09 -0800271 SINK("xps", XPSSink);
mtklein748ca3b2015-01-15 10:56:12 -0800272 }
273#undef SINK
274 return NULL;
mtklein114c3cd2015-01-15 10:15:02 -0800275}
276
mtklein748ca3b2015-01-15 10:56:12 -0800277static Sink* create_via(const char* tag, Sink* wrapped) {
278#define VIA(t, via, ...) if (0 == strcmp(t, tag)) { return new via(__VA_ARGS__); }
mtklein7edca212015-01-21 13:18:51 -0800279 VIA("pipe", ViaPipe, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800280 VIA("serialize", ViaSerialization, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800281 VIA("tiles", ViaTiles, 256, 256, NULL, wrapped);
282 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800283
mtkleind603b222015-02-17 11:13:33 -0800284 if (FLAGS_matrix.count() == 4) {
mtklein748ca3b2015-01-15 10:56:12 -0800285 SkMatrix m;
mtkleind603b222015-02-17 11:13:33 -0800286 m.reset();
287 m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
288 m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
289 m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
290 m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
291 VIA("matrix", ViaMatrix, m, wrapped);
292 VIA("upright", ViaUpright, m, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800293 }
tomhudson64de1e12015-03-05 08:01:07 -0800294
295#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
296 VIA("androidsdk", ViaAndroidSDK, wrapped);
297#endif
298
mtklein748ca3b2015-01-15 10:56:12 -0800299#undef VIA
300 return NULL;
mtklein114c3cd2015-01-15 10:15:02 -0800301}
302
mtklein748ca3b2015-01-15 10:56:12 -0800303static void gather_sinks() {
304 for (int i = 0; i < FLAGS_config.count(); i++) {
305 const char* config = FLAGS_config[i];
306 SkTArray<SkString> parts;
307 SkStrSplit(config, "-", &parts);
308
309 Sink* sink = NULL;
310 for (int i = parts.count(); i-- > 0;) {
311 const char* part = parts[i].c_str();
312 Sink* next = (sink == NULL) ? create_sink(part) : create_via(part, sink);
313 if (next == NULL) {
314 SkDebugf("Skipping %s: Don't understand '%s'.\n", config, part);
315 delete sink;
316 sink = NULL;
317 break;
318 }
319 sink = next;
320 }
321 if (sink) {
322 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800323 }
324 }
325}
mtklein709d2c32015-01-15 08:30:25 -0800326
mtkleina2ef6422015-01-15 13:44:22 -0800327static bool match(const char* needle, const char* haystack) {
328 return 0 == strcmp("_", needle) || NULL != strstr(haystack, needle);
329}
330
331static ImplicitString is_blacklisted(const char* sink, const char* src, const char* name) {
332 for (int i = 0; i < FLAGS_blacklist.count() - 2; i += 3) {
333 if (match(FLAGS_blacklist[i+0], sink) &&
334 match(FLAGS_blacklist[i+1], src) &&
335 match(FLAGS_blacklist[i+2], name)) {
336 return SkStringPrintf("%s %s %s",
337 FLAGS_blacklist[i+0], FLAGS_blacklist[i+1], FLAGS_blacklist[i+2]);
338 }
339 }
340 return "";
341}
342
mtklein748ca3b2015-01-15 10:56:12 -0800343// The finest-grained unit of work we can run: draw a single Src into a single Sink,
344// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
345struct Task {
346 Task(const Tagged<Src>& src, const Tagged<Sink>& sink) : src(src), sink(sink) {}
347 const Tagged<Src>& src;
348 const Tagged<Sink>& sink;
349
350 static void Run(Task* task) {
mtkleina2ef6422015-01-15 13:44:22 -0800351 SkString name = task->src->name();
mtkleinb37cb412015-03-18 05:27:14 -0700352 SkString note;
mtkleina2ef6422015-01-15 13:44:22 -0800353 SkString whyBlacklisted = is_blacklisted(task->sink.tag, task->src.tag, name.c_str());
mtkleinb37cb412015-03-18 05:27:14 -0700354 if (!whyBlacklisted.isEmpty()) {
355 note.appendf(" (--blacklist %s)", whyBlacklisted.c_str());
356 }
mtkleinb9eb4ac2015-02-02 18:26:03 -0800357 SkString log;
mtklein748ca3b2015-01-15 10:56:12 -0800358 WallTimer timer;
359 timer.start();
mtkleina2ef6422015-01-15 13:44:22 -0800360 if (!FLAGS_dryRun && whyBlacklisted.isEmpty()) {
mtklein748ca3b2015-01-15 10:56:12 -0800361 SkBitmap bitmap;
362 SkDynamicMemoryWStream stream;
mtkleinb37cb412015-03-18 05:27:14 -0700363 start(task->sink.tag, task->src.tag, name.c_str());
mtkleinb9eb4ac2015-02-02 18:26:03 -0800364 Error err = task->sink->draw(*task->src, &bitmap, &stream, &log);
mtklein748ca3b2015-01-15 10:56:12 -0800365 if (!err.isEmpty()) {
mtklein4089ef72015-03-05 08:40:28 -0800366 timer.end();
367 if (err.isFatal()) {
368 fail(SkStringPrintf("%s %s %s: %s",
369 task->sink.tag,
370 task->src.tag,
371 name.c_str(),
372 err.c_str()));
mtkleinb37cb412015-03-18 05:27:14 -0700373 } else {
374 note.appendf(" (skipped: %s)", err.c_str());
mtklein4089ef72015-03-05 08:40:28 -0800375 }
mtkleinb37cb412015-03-18 05:27:14 -0700376 done(timer.fWall, task->sink.tag, task->src.tag, name, note, log);
mtklein4089ef72015-03-05 08:40:28 -0800377 return;
mtklein748ca3b2015-01-15 10:56:12 -0800378 }
mtklein62bd1a62015-01-27 14:46:26 -0800379 SkAutoTDelete<SkStreamAsset> data(stream.detachAsStream());
380
381 SkString md5;
382 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
383 SkMD5 hash;
384 if (data->getLength()) {
385 hash.writeStream(data, data->getLength());
386 data->rewind();
387 } else {
388 hash.write(bitmap.getPixels(), bitmap.getSize());
389 }
390 SkMD5::Digest digest;
391 hash.finish(digest);
392 for (int i = 0; i < 16; i++) {
393 md5.appendf("%02x", digest.data[i]);
394 }
395 }
396
397 if (!FLAGS_readPath.isEmpty() &&
mtkleina82f5622015-02-20 12:30:19 -0800398 !gGold.contains(Gold(task->sink.tag, task->src.tag, name, md5))) {
mtklein62bd1a62015-01-27 14:46:26 -0800399 fail(SkStringPrintf("%s not found for %s %s %s in %s",
400 md5.c_str(),
401 task->sink.tag,
402 task->src.tag,
403 name.c_str(),
404 FLAGS_readPath[0]));
405 }
406
mtklein748ca3b2015-01-15 10:56:12 -0800407 if (!FLAGS_writePath.isEmpty()) {
408 const char* ext = task->sink->fileExtension();
mtklein62bd1a62015-01-27 14:46:26 -0800409 if (data->getLength()) {
410 WriteToDisk(*task, md5, ext, data, data->getLength(), NULL);
halcanary022afb82015-01-30 11:00:12 -0800411 SkASSERT(bitmap.drawsNothing());
412 } else if (!bitmap.drawsNothing()) {
mtklein62bd1a62015-01-27 14:46:26 -0800413 WriteToDisk(*task, md5, ext, NULL, 0, &bitmap);
mtklein748ca3b2015-01-15 10:56:12 -0800414 }
415 }
416 }
417 timer.end();
mtkleinb37cb412015-03-18 05:27:14 -0700418 done(timer.fWall, task->sink.tag, task->src.tag, name, note, log);
mtklein748ca3b2015-01-15 10:56:12 -0800419 }
420
421 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -0800422 SkString md5,
423 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -0800424 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -0800425 const SkBitmap* bitmap) {
mtklein748ca3b2015-01-15 10:56:12 -0800426 JsonWriter::BitmapResult result;
427 result.name = task.src->name();
428 result.config = task.sink.tag;
429 result.sourceType = task.src.tag;
430 result.ext = ext;
mtklein62bd1a62015-01-27 14:46:26 -0800431 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -0800432 JsonWriter::AddBitmapResult(result);
433
434 const char* dir = FLAGS_writePath[0];
435 if (0 == strcmp(dir, "@")) { // Needed for iOS.
436 dir = FLAGS_resourcePath[0];
437 }
438 sk_mkdir(dir);
439
440 SkString path;
441 if (FLAGS_nameByHash) {
442 path = SkOSPath::Join(dir, result.md5.c_str());
443 path.append(".");
444 path.append(ext);
445 if (sk_exists(path.c_str())) {
446 return; // Content-addressed. If it exists already, we're done.
447 }
448 } else {
449 path = SkOSPath::Join(dir, task.sink.tag);
450 sk_mkdir(path.c_str());
451 path = SkOSPath::Join(path.c_str(), task.src.tag);
452 sk_mkdir(path.c_str());
453 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
454 path.append(".");
455 path.append(ext);
456 }
457
458 SkFILEWStream file(path.c_str());
459 if (!file.isValid()) {
460 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
461 return;
462 }
463
mtklein748ca3b2015-01-15 10:56:12 -0800464 if (bitmap) {
465 // We can't encode A8 bitmaps as PNGs. Convert them to 8888 first.
466 SkBitmap converted;
467 if (bitmap->info().colorType() == kAlpha_8_SkColorType) {
468 if (!bitmap->copyTo(&converted, kN32_SkColorType)) {
469 fail("Can't convert A8 to 8888.\n");
470 return;
471 }
472 bitmap = &converted;
473 }
474 if (!SkImageEncoder::EncodeStream(&file, *bitmap, SkImageEncoder::kPNG_Type, 100)) {
475 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
476 return;
477 }
478 } else {
479 if (!file.writeStream(data, len)) {
480 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
481 return;
482 }
483 }
484 }
485};
486
487// Run all tasks in the same enclave serially on the same thread.
488// They can't possibly run concurrently with each other.
489static void run_enclave(SkTArray<Task>* tasks) {
490 for (int i = 0; i < tasks->count(); i++) {
491 Task::Run(tasks->begin() + i);
492 }
493}
494
495/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
496
497// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
498
mtklein55e88b22015-01-21 15:50:13 -0800499static SkTDArray<skiatest::Test> gThreadedTests, gGPUTests;
mtklein748ca3b2015-01-15 10:56:12 -0800500
501static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -0800502 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -0800503 return;
504 }
halcanary87f3ba42015-01-20 09:30:20 -0800505 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r;
506 r = r->next()) {
507 // Despite its name, factory() is returning a reference to
508 // link-time static const POD data.
509 const skiatest::Test& test = r->factory();
510 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -0800511 continue;
512 }
halcanary87f3ba42015-01-20 09:30:20 -0800513 if (test.needsGpu && gpu_supported()) {
mtklein55e88b22015-01-21 15:50:13 -0800514 (FLAGS_gpu_threading ? gThreadedTests : gGPUTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -0800515 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein55e88b22015-01-21 15:50:13 -0800516 gThreadedTests.push(test);
mtklein82d28432015-01-15 12:46:02 -0800517 }
mtklein748ca3b2015-01-15 10:56:12 -0800518 }
519}
520
halcanary87f3ba42015-01-20 09:30:20 -0800521static void run_test(skiatest::Test* test) {
522 struct : public skiatest::Reporter {
523 void reportFailed(const skiatest::Failure& failure) SK_OVERRIDE {
524 fail(failure.toString());
525 JsonWriter::AddTestFailure(failure);
526 }
527 bool allowExtendedTest() const SK_OVERRIDE {
528 return FLAGS_pathOpsExtended;
529 }
530 bool verbose() const SK_OVERRIDE { return FLAGS_veryVerbose; }
531 } reporter;
mtklein748ca3b2015-01-15 10:56:12 -0800532 WallTimer timer;
533 timer.start();
mtklein748ca3b2015-01-15 10:56:12 -0800534 if (!FLAGS_dryRun) {
mtkleinb37cb412015-03-18 05:27:14 -0700535 start("unit", "test", test->name);
mtklein55e88b22015-01-21 15:50:13 -0800536 GrContextFactory factory;
537 test->proc(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -0800538 }
539 timer.end();
mtkleinb37cb412015-03-18 05:27:14 -0700540 done(timer.fWall, "unit", "test", test->name, "", "");
mtklein748ca3b2015-01-15 10:56:12 -0800541}
542
543/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
544
mtklein55e88b22015-01-21 15:50:13 -0800545// If we're isolating all GPU-bound work to one thread (the default), this function runs all that.
546static void run_enclave_and_gpu_tests(SkTArray<Task>* tasks) {
547 run_enclave(tasks);
548 for (int i = 0; i < gGPUTests.count(); i++) {
549 run_test(&gGPUTests[i]);
550 }
551}
552
mtkleinde6fc2b2015-03-12 06:28:54 -0700553// Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung.
554// This prints something every once in a while so that it knows we're still working.
mtklein2e1c47e2015-03-12 07:16:56 -0700555static void start_keepalive() {
556 struct Loop {
557 static void forever(void*) {
558 for (;;) {
559 static const int kSec = 300;
560 #if defined(SK_BUILD_FOR_WIN)
561 Sleep(kSec * 1000);
562 #else
563 sleep(kSec);
564 #endif
mtkleinb37cb412015-03-18 05:27:14 -0700565 SkString running;
566 {
567 SkAutoMutexAcquire lock(gRunningMutex);
568 for (int i = 0; i < gRunning.count(); i++) {
569 running.appendf("\n\t%s", gRunning[i].c_str());
570 }
571 }
572 SkDebugf("\nCurrently running:%s\n", running.c_str());
mtklein2e1c47e2015-03-12 07:16:56 -0700573 }
574 }
575 };
576 static SkThread* intentionallyLeaked = new SkThread(Loop::forever);
577 intentionallyLeaked->start();
mtkleinde6fc2b2015-03-12 06:28:54 -0700578}
579
jcgregorio3b27ade2014-11-13 08:06:40 -0800580int dm_main();
caryclark17f0b6d2014-07-22 10:15:34 -0700581int dm_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -0800582 SetupCrashHandler();
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +0000583 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -0700584 SkTaskGroup::Enabler enabled(FLAGS_threads);
mtkleine67164d2015-02-02 13:24:37 -0800585 if (FLAGS_leaks) {
586 SkInstCountPrintLeaksOnExit();
587 }
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +0000588
mtklein2e1c47e2015-03-12 07:16:56 -0700589 start_keepalive();
mtkleinde6fc2b2015-03-12 06:28:54 -0700590
mtklein62bd1a62015-01-27 14:46:26 -0800591 gather_gold();
592
mtklein748ca3b2015-01-15 10:56:12 -0800593 gather_srcs();
594 gather_sinks();
595 gather_tests();
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +0000596
mtklein55e88b22015-01-21 15:50:13 -0800597 gPending = gSrcs.count() * gSinks.count() + gThreadedTests.count() + gGPUTests.count();
mtklein748ca3b2015-01-15 10:56:12 -0800598 SkDebugf("%d srcs * %d sinks + %d tests == %d tasks\n",
mtklein55e88b22015-01-21 15:50:13 -0800599 gSrcs.count(), gSinks.count(), gThreadedTests.count() + gGPUTests.count(), gPending);
mtklein748ca3b2015-01-15 10:56:12 -0800600
601 // We try to exploit as much parallelism as is safe. Most Src/Sink pairs run on any thread,
602 // but Sinks that identify as part of a particular enclave run serially on a single thread.
mtklein82d28432015-01-15 12:46:02 -0800603 // CPU tests run on any thread. GPU tests depend on --gpu_threading.
mtklein748ca3b2015-01-15 10:56:12 -0800604 SkTArray<Task> enclaves[kNumEnclaves];
605 for (int j = 0; j < gSinks.count(); j++) {
606 SkTArray<Task>& tasks = enclaves[gSinks[j]->enclave()];
607 for (int i = 0; i < gSrcs.count(); i++) {
608 tasks.push_back(Task(gSrcs[i], gSinks[j]));
609 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +0000610 }
611
mtklein748ca3b2015-01-15 10:56:12 -0800612 SkTaskGroup tg;
mtklein55e88b22015-01-21 15:50:13 -0800613 tg.batch(run_test, gThreadedTests.begin(), gThreadedTests.count());
614 for (int i = 0; i < kNumEnclaves; i++) {
615 switch(i) {
616 case kAnyThread_Enclave:
617 tg.batch(Task::Run, enclaves[i].begin(), enclaves[i].count());
618 break;
619 case kGPU_Enclave:
620 tg.add(run_enclave_and_gpu_tests, &enclaves[i]);
621 break;
622 default:
623 tg.add(run_enclave, &enclaves[i]);
624 break;
mtklein82d28432015-01-15 12:46:02 -0800625 }
mtklein55e88b22015-01-21 15:50:13 -0800626 }
mtklein748ca3b2015-01-15 10:56:12 -0800627 tg.wait();
mtklein748ca3b2015-01-15 10:56:12 -0800628 // At this point we're back in single-threaded land.
mtklein@google.comd36522d2013-10-16 13:02:15 +0000629
mtklein2f64eec2015-01-15 14:20:41 -0800630 SkDebugf("\n");
mtklein748ca3b2015-01-15 10:56:12 -0800631 if (gFailures.count() > 0) {
632 SkDebugf("Failures:\n");
633 for (int i = 0; i < gFailures.count(); i++) {
mtklein9dc09102015-01-15 15:47:33 -0800634 SkDebugf("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800635 }
636 SkDebugf("%d failures\n", gFailures.count());
637 return 1;
mtklein114c3cd2015-01-15 10:15:02 -0800638 }
mtklein748ca3b2015-01-15 10:56:12 -0800639 if (gPending > 0) {
640 SkDebugf("Hrm, we didn't seem to run everything we intended to! Please file a bug.\n");
641 return 1;
mtklein114c3cd2015-01-15 10:15:02 -0800642 }
mtklein748ca3b2015-01-15 10:56:12 -0800643 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +0000644}
jcgregorio3b27ade2014-11-13 08:06:40 -0800645
646#if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL)
647int main(int argc, char** argv) {
648 SkCommandLineFlags::Parse(argc, argv);
649 return dm_main();
650}
651#endif