blob: 833af0edbaa267c8ac9be4314a79be0443051e0a [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);
mtklein62bd1a62015-01-27 14:46:26 -0800107 }
mtklein02f46cf2015-03-20 13:48:42 -0700108 static uint32_t Hash(const Gold& g) { return SkGoodHash((const SkString&)g); }
mtklein62bd1a62015-01-27 14:46:26 -0800109};
mtkleina82f5622015-02-20 12:30:19 -0800110static SkTHashSet<Gold, Gold::Hash> gGold;
mtklein62bd1a62015-01-27 14:46:26 -0800111
112static void add_gold(JsonWriter::BitmapResult r) {
mtkleina82f5622015-02-20 12:30:19 -0800113 gGold.add(Gold(r.config, r.sourceType, r.name, r.md5));
mtklein62bd1a62015-01-27 14:46:26 -0800114}
115
116static void gather_gold() {
117 if (!FLAGS_readPath.isEmpty()) {
118 SkString path(FLAGS_readPath[0]);
119 path.append("/dm.json");
120 if (!JsonWriter::ReadJson(path.c_str(), add_gold)) {
121 fail(SkStringPrintf("Couldn't read %s for golden results.", path.c_str()));
122 }
123 }
124}
125
126/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
127
mtklein748ca3b2015-01-15 10:56:12 -0800128template <typename T>
129struct Tagged : public SkAutoTDelete<T> { const char* tag; };
130
131static const bool kMemcpyOK = true;
132
133static SkTArray<Tagged<Src>, kMemcpyOK> gSrcs;
134static SkTArray<Tagged<Sink>, kMemcpyOK> gSinks;
135
136static void push_src(const char* tag, Src* s) {
137 SkAutoTDelete<Src> src(s);
138 if (FLAGS_src.contains(tag) &&
139 !SkCommandLineFlags::ShouldSkip(FLAGS_match, src->name().c_str())) {
140 Tagged<Src>& s = gSrcs.push_back();
141 s.reset(src.detach());
142 s.tag = tag;
mtklein114c3cd2015-01-15 10:15:02 -0800143 }
mtklein748ca3b2015-01-15 10:56:12 -0800144}
mtklein114c3cd2015-01-15 10:15:02 -0800145
scroggo9b77ddd2015-03-19 06:03:39 -0700146static bool codec_supported(const char* ext) {
147 // FIXME: Once other versions of SkCodec are available, we can add them to this
148 // list (and eventually we can remove this check once they are all supported).
149 return strcmp(ext, "png") == 0 || strcmp(ext, "PNG") == 0;
150}
151
mtklein748ca3b2015-01-15 10:56:12 -0800152static void gather_srcs() {
153 for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) {
154 push_src("gm", new GMSrc(r->factory()));
155 }
halcanaryfc37ad12015-01-30 07:31:19 -0800156 for (int i = 0; i < FLAGS_skps.count(); i++) {
157 const char* path = FLAGS_skps[i];
158 if (sk_isdir(path)) {
159 SkOSFile::Iter it(path, "skp");
160 for (SkString file; it.next(&file); ) {
161 push_src("skp", new SKPSrc(SkOSPath::Join(path, file.c_str())));
162 }
163 } else {
mtklein8d17a132015-01-30 11:42:31 -0800164 push_src("skp", new SKPSrc(path));
mtklein114c3cd2015-01-15 10:15:02 -0800165 }
166 }
halcanary23b03c32015-01-30 09:58:58 -0800167 static const char* const exts[] = {
168 "bmp", "gif", "jpg", "jpeg", "png", "webp", "ktx", "astc", "wbmp", "ico",
169 "BMP", "GIF", "JPG", "JPEG", "PNG", "WEBP", "KTX", "ASTC", "WBMP", "ICO",
170 };
171 for (int i = 0; i < FLAGS_images.count(); i++) {
172 const char* flag = FLAGS_images[i];
173 if (sk_isdir(flag)) {
174 for (size_t j = 0; j < SK_ARRAY_COUNT(exts); j++) {
175 SkOSFile::Iter it(flag, exts[j]);
176 for (SkString file; it.next(&file); ) {
177 SkString path = SkOSPath::Join(flag, file.c_str());
178 push_src("image", new ImageSrc(path)); // Decode entire image.
mtkleinedc93bc2015-01-30 13:22:23 -0800179 push_src("subset", new ImageSrc(path, 2)); // Decode into 2 x 2 subsets
scroggo9b77ddd2015-03-19 06:03:39 -0700180 if (codec_supported(exts[j])) {
181 push_src("codec", new CodecSrc(path)); // Decode with SkCodec.
182 }
halcanary23b03c32015-01-30 09:58:58 -0800183 }
mtklein114c3cd2015-01-15 10:15:02 -0800184 }
halcanary23b03c32015-01-30 09:58:58 -0800185 } else if (sk_exists(flag)) {
186 // assume that FLAGS_images[i] is a valid image if it is a file.
mtklein8d17a132015-01-30 11:42:31 -0800187 push_src("image", new ImageSrc(flag)); // Decode entire image.
mtkleinedc93bc2015-01-30 13:22:23 -0800188 push_src("subset", new ImageSrc(flag, 2)); // Decode into 2 x 2 subsets
scroggo9b77ddd2015-03-19 06:03:39 -0700189 push_src("codec", new CodecSrc(flag)); // Decode with SkCodec.
mtklein709d2c32015-01-15 08:30:25 -0800190 }
mtklein709d2c32015-01-15 08:30:25 -0800191 }
192}
193
mtklein748ca3b2015-01-15 10:56:12 -0800194static GrGLStandard get_gpu_api() {
195 if (FLAGS_gpuAPI.contains("gl")) { return kGL_GrGLStandard; }
196 if (FLAGS_gpuAPI.contains("gles")) { return kGLES_GrGLStandard; }
197 return kNone_GrGLStandard;
mtklein709d2c32015-01-15 08:30:25 -0800198}
199
mtklein748ca3b2015-01-15 10:56:12 -0800200static void push_sink(const char* tag, Sink* s) {
201 SkAutoTDelete<Sink> sink(s);
202 if (!FLAGS_config.contains(tag)) {
203 return;
mtklein114c3cd2015-01-15 10:15:02 -0800204 }
mtklein748ca3b2015-01-15 10:56:12 -0800205 // Try a noop Src as a canary. If it fails, skip this sink.
206 struct : public Src {
207 Error draw(SkCanvas*) const SK_OVERRIDE { return ""; }
208 SkISize size() const SK_OVERRIDE { return SkISize::Make(16, 16); }
209 Name name() const SK_OVERRIDE { return "noop"; }
210 } noop;
mtklein114c3cd2015-01-15 10:15:02 -0800211
mtklein748ca3b2015-01-15 10:56:12 -0800212 SkBitmap bitmap;
213 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800214 SkString log;
215 Error err = sink->draw(noop, &bitmap, &stream, &log);
mtklein4089ef72015-03-05 08:40:28 -0800216 if (err.isFatal()) {
mtklein748ca3b2015-01-15 10:56:12 -0800217 SkDebugf("Skipping %s: %s\n", tag, err.c_str());
mtklein114c3cd2015-01-15 10:15:02 -0800218 return;
219 }
220
mtklein748ca3b2015-01-15 10:56:12 -0800221 Tagged<Sink>& ts = gSinks.push_back();
222 ts.reset(sink.detach());
223 ts.tag = tag;
224}
225
226static bool gpu_supported() {
227#if SK_SUPPORT_GPU
228 return FLAGS_gpu;
229#else
230 return false;
231#endif
232}
233
234static Sink* create_sink(const char* tag) {
235#define SINK(t, sink, ...) if (0 == strcmp(t, tag)) { return new sink(__VA_ARGS__); }
236 if (gpu_supported()) {
mtklein82d28432015-01-15 12:46:02 -0800237 typedef GrContextFactory Gr;
mtklein748ca3b2015-01-15 10:56:12 -0800238 const GrGLStandard api = get_gpu_api();
mtklein82d28432015-01-15 12:46:02 -0800239 SINK("gpunull", GPUSink, Gr::kNull_GLContextType, api, 0, false, FLAGS_gpu_threading);
240 SINK("gpudebug", GPUSink, Gr::kDebug_GLContextType, api, 0, false, FLAGS_gpu_threading);
241 SINK("gpu", GPUSink, Gr::kNative_GLContextType, api, 0, false, FLAGS_gpu_threading);
242 SINK("gpudft", GPUSink, Gr::kNative_GLContextType, api, 0, true, FLAGS_gpu_threading);
243 SINK("msaa4", GPUSink, Gr::kNative_GLContextType, api, 4, false, FLAGS_gpu_threading);
244 SINK("msaa16", GPUSink, Gr::kNative_GLContextType, api, 16, false, FLAGS_gpu_threading);
245 SINK("nvprmsaa4", GPUSink, Gr::kNVPR_GLContextType, api, 4, false, FLAGS_gpu_threading);
246 SINK("nvprmsaa16", GPUSink, Gr::kNVPR_GLContextType, api, 16, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800247 #if SK_ANGLE
mtklein82d28432015-01-15 12:46:02 -0800248 SINK("angle", GPUSink, Gr::kANGLE_GLContextType, api, 0, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800249 #endif
250 #if SK_MESA
mtklein82d28432015-01-15 12:46:02 -0800251 SINK("mesa", GPUSink, Gr::kMESA_GLContextType, api, 0, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800252 #endif
mtklein114c3cd2015-01-15 10:15:02 -0800253 }
mtklein748ca3b2015-01-15 10:56:12 -0800254
tomhudsoneebc39a2015-02-23 12:18:05 -0800255#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
256 SINK("hwui", HWUISink);
257#endif
258
mtklein748ca3b2015-01-15 10:56:12 -0800259 if (FLAGS_cpu) {
260 SINK("565", RasterSink, kRGB_565_SkColorType);
261 SINK("8888", RasterSink, kN32_SkColorType);
mtklein19f30602015-01-20 13:34:39 -0800262 SINK("pdf", PDFSink);
mtklein9c3f17d2015-01-28 11:35:18 -0800263 SINK("skp", SKPSink);
mtklein8a4527e2015-01-31 20:00:58 -0800264 SINK("svg", SVGSink);
265 SINK("null", NullSink);
halcanary47ef4d52015-03-03 09:13:09 -0800266 SINK("xps", XPSSink);
mtklein748ca3b2015-01-15 10:56:12 -0800267 }
268#undef SINK
269 return NULL;
mtklein114c3cd2015-01-15 10:15:02 -0800270}
271
mtklein748ca3b2015-01-15 10:56:12 -0800272static Sink* create_via(const char* tag, Sink* wrapped) {
273#define VIA(t, via, ...) if (0 == strcmp(t, tag)) { return new via(__VA_ARGS__); }
mtklein7edca212015-01-21 13:18:51 -0800274 VIA("pipe", ViaPipe, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800275 VIA("serialize", ViaSerialization, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800276 VIA("tiles", ViaTiles, 256, 256, NULL, wrapped);
277 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800278
mtkleind603b222015-02-17 11:13:33 -0800279 if (FLAGS_matrix.count() == 4) {
mtklein748ca3b2015-01-15 10:56:12 -0800280 SkMatrix m;
mtkleind603b222015-02-17 11:13:33 -0800281 m.reset();
282 m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
283 m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
284 m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
285 m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
286 VIA("matrix", ViaMatrix, m, wrapped);
287 VIA("upright", ViaUpright, m, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800288 }
tomhudson64de1e12015-03-05 08:01:07 -0800289
290#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
291 VIA("androidsdk", ViaAndroidSDK, wrapped);
292#endif
293
mtklein748ca3b2015-01-15 10:56:12 -0800294#undef VIA
295 return NULL;
mtklein114c3cd2015-01-15 10:15:02 -0800296}
297
mtklein748ca3b2015-01-15 10:56:12 -0800298static void gather_sinks() {
299 for (int i = 0; i < FLAGS_config.count(); i++) {
300 const char* config = FLAGS_config[i];
301 SkTArray<SkString> parts;
302 SkStrSplit(config, "-", &parts);
303
304 Sink* sink = NULL;
305 for (int i = parts.count(); i-- > 0;) {
306 const char* part = parts[i].c_str();
307 Sink* next = (sink == NULL) ? create_sink(part) : create_via(part, sink);
308 if (next == NULL) {
309 SkDebugf("Skipping %s: Don't understand '%s'.\n", config, part);
310 delete sink;
311 sink = NULL;
312 break;
313 }
314 sink = next;
315 }
316 if (sink) {
317 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800318 }
319 }
320}
mtklein709d2c32015-01-15 08:30:25 -0800321
mtkleina2ef6422015-01-15 13:44:22 -0800322static bool match(const char* needle, const char* haystack) {
323 return 0 == strcmp("_", needle) || NULL != strstr(haystack, needle);
324}
325
326static ImplicitString is_blacklisted(const char* sink, const char* src, const char* name) {
327 for (int i = 0; i < FLAGS_blacklist.count() - 2; i += 3) {
328 if (match(FLAGS_blacklist[i+0], sink) &&
329 match(FLAGS_blacklist[i+1], src) &&
330 match(FLAGS_blacklist[i+2], name)) {
331 return SkStringPrintf("%s %s %s",
332 FLAGS_blacklist[i+0], FLAGS_blacklist[i+1], FLAGS_blacklist[i+2]);
333 }
334 }
335 return "";
336}
337
mtklein748ca3b2015-01-15 10:56:12 -0800338// The finest-grained unit of work we can run: draw a single Src into a single Sink,
339// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
340struct Task {
341 Task(const Tagged<Src>& src, const Tagged<Sink>& sink) : src(src), sink(sink) {}
342 const Tagged<Src>& src;
343 const Tagged<Sink>& sink;
344
345 static void Run(Task* task) {
mtkleina2ef6422015-01-15 13:44:22 -0800346 SkString name = task->src->name();
mtkleinb37cb412015-03-18 05:27:14 -0700347 SkString note;
mtkleina2ef6422015-01-15 13:44:22 -0800348 SkString whyBlacklisted = is_blacklisted(task->sink.tag, task->src.tag, name.c_str());
mtkleinb37cb412015-03-18 05:27:14 -0700349 if (!whyBlacklisted.isEmpty()) {
350 note.appendf(" (--blacklist %s)", whyBlacklisted.c_str());
351 }
mtkleinb9eb4ac2015-02-02 18:26:03 -0800352 SkString log;
mtklein748ca3b2015-01-15 10:56:12 -0800353 WallTimer timer;
354 timer.start();
mtkleina2ef6422015-01-15 13:44:22 -0800355 if (!FLAGS_dryRun && whyBlacklisted.isEmpty()) {
mtklein748ca3b2015-01-15 10:56:12 -0800356 SkBitmap bitmap;
357 SkDynamicMemoryWStream stream;
mtkleinb37cb412015-03-18 05:27:14 -0700358 start(task->sink.tag, task->src.tag, name.c_str());
mtkleinb9eb4ac2015-02-02 18:26:03 -0800359 Error err = task->sink->draw(*task->src, &bitmap, &stream, &log);
mtklein748ca3b2015-01-15 10:56:12 -0800360 if (!err.isEmpty()) {
mtklein4089ef72015-03-05 08:40:28 -0800361 timer.end();
362 if (err.isFatal()) {
363 fail(SkStringPrintf("%s %s %s: %s",
364 task->sink.tag,
365 task->src.tag,
366 name.c_str(),
367 err.c_str()));
mtkleinb37cb412015-03-18 05:27:14 -0700368 } else {
369 note.appendf(" (skipped: %s)", err.c_str());
mtklein4089ef72015-03-05 08:40:28 -0800370 }
mtkleinb37cb412015-03-18 05:27:14 -0700371 done(timer.fWall, task->sink.tag, task->src.tag, name, note, log);
mtklein4089ef72015-03-05 08:40:28 -0800372 return;
mtklein748ca3b2015-01-15 10:56:12 -0800373 }
mtklein62bd1a62015-01-27 14:46:26 -0800374 SkAutoTDelete<SkStreamAsset> data(stream.detachAsStream());
375
376 SkString md5;
377 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
378 SkMD5 hash;
379 if (data->getLength()) {
380 hash.writeStream(data, data->getLength());
381 data->rewind();
382 } else {
383 hash.write(bitmap.getPixels(), bitmap.getSize());
384 }
385 SkMD5::Digest digest;
386 hash.finish(digest);
387 for (int i = 0; i < 16; i++) {
388 md5.appendf("%02x", digest.data[i]);
389 }
390 }
391
392 if (!FLAGS_readPath.isEmpty() &&
mtkleina82f5622015-02-20 12:30:19 -0800393 !gGold.contains(Gold(task->sink.tag, task->src.tag, name, md5))) {
mtklein62bd1a62015-01-27 14:46:26 -0800394 fail(SkStringPrintf("%s not found for %s %s %s in %s",
395 md5.c_str(),
396 task->sink.tag,
397 task->src.tag,
398 name.c_str(),
399 FLAGS_readPath[0]));
400 }
401
mtklein748ca3b2015-01-15 10:56:12 -0800402 if (!FLAGS_writePath.isEmpty()) {
403 const char* ext = task->sink->fileExtension();
mtklein62bd1a62015-01-27 14:46:26 -0800404 if (data->getLength()) {
405 WriteToDisk(*task, md5, ext, data, data->getLength(), NULL);
halcanary022afb82015-01-30 11:00:12 -0800406 SkASSERT(bitmap.drawsNothing());
407 } else if (!bitmap.drawsNothing()) {
mtklein62bd1a62015-01-27 14:46:26 -0800408 WriteToDisk(*task, md5, ext, NULL, 0, &bitmap);
mtklein748ca3b2015-01-15 10:56:12 -0800409 }
410 }
411 }
412 timer.end();
mtkleinb37cb412015-03-18 05:27:14 -0700413 done(timer.fWall, task->sink.tag, task->src.tag, name, note, log);
mtklein748ca3b2015-01-15 10:56:12 -0800414 }
415
416 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -0800417 SkString md5,
418 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -0800419 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -0800420 const SkBitmap* bitmap) {
mtklein748ca3b2015-01-15 10:56:12 -0800421 JsonWriter::BitmapResult result;
422 result.name = task.src->name();
423 result.config = task.sink.tag;
424 result.sourceType = task.src.tag;
425 result.ext = ext;
mtklein62bd1a62015-01-27 14:46:26 -0800426 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -0800427 JsonWriter::AddBitmapResult(result);
428
429 const char* dir = FLAGS_writePath[0];
430 if (0 == strcmp(dir, "@")) { // Needed for iOS.
431 dir = FLAGS_resourcePath[0];
432 }
433 sk_mkdir(dir);
434
435 SkString path;
436 if (FLAGS_nameByHash) {
437 path = SkOSPath::Join(dir, result.md5.c_str());
438 path.append(".");
439 path.append(ext);
440 if (sk_exists(path.c_str())) {
441 return; // Content-addressed. If it exists already, we're done.
442 }
443 } else {
444 path = SkOSPath::Join(dir, task.sink.tag);
445 sk_mkdir(path.c_str());
446 path = SkOSPath::Join(path.c_str(), task.src.tag);
447 sk_mkdir(path.c_str());
448 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
449 path.append(".");
450 path.append(ext);
451 }
452
453 SkFILEWStream file(path.c_str());
454 if (!file.isValid()) {
455 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
456 return;
457 }
458
mtklein748ca3b2015-01-15 10:56:12 -0800459 if (bitmap) {
460 // We can't encode A8 bitmaps as PNGs. Convert them to 8888 first.
461 SkBitmap converted;
462 if (bitmap->info().colorType() == kAlpha_8_SkColorType) {
463 if (!bitmap->copyTo(&converted, kN32_SkColorType)) {
464 fail("Can't convert A8 to 8888.\n");
465 return;
466 }
467 bitmap = &converted;
468 }
469 if (!SkImageEncoder::EncodeStream(&file, *bitmap, SkImageEncoder::kPNG_Type, 100)) {
470 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
471 return;
472 }
473 } else {
474 if (!file.writeStream(data, len)) {
475 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
476 return;
477 }
478 }
479 }
480};
481
482// Run all tasks in the same enclave serially on the same thread.
483// They can't possibly run concurrently with each other.
484static void run_enclave(SkTArray<Task>* tasks) {
485 for (int i = 0; i < tasks->count(); i++) {
486 Task::Run(tasks->begin() + i);
487 }
488}
489
490/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
491
492// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
493
mtklein55e88b22015-01-21 15:50:13 -0800494static SkTDArray<skiatest::Test> gThreadedTests, gGPUTests;
mtklein748ca3b2015-01-15 10:56:12 -0800495
496static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -0800497 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -0800498 return;
499 }
halcanary87f3ba42015-01-20 09:30:20 -0800500 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r;
501 r = r->next()) {
502 // Despite its name, factory() is returning a reference to
503 // link-time static const POD data.
504 const skiatest::Test& test = r->factory();
505 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -0800506 continue;
507 }
halcanary87f3ba42015-01-20 09:30:20 -0800508 if (test.needsGpu && gpu_supported()) {
mtklein55e88b22015-01-21 15:50:13 -0800509 (FLAGS_gpu_threading ? gThreadedTests : gGPUTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -0800510 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein55e88b22015-01-21 15:50:13 -0800511 gThreadedTests.push(test);
mtklein82d28432015-01-15 12:46:02 -0800512 }
mtklein748ca3b2015-01-15 10:56:12 -0800513 }
514}
515
halcanary87f3ba42015-01-20 09:30:20 -0800516static void run_test(skiatest::Test* test) {
517 struct : public skiatest::Reporter {
518 void reportFailed(const skiatest::Failure& failure) SK_OVERRIDE {
519 fail(failure.toString());
520 JsonWriter::AddTestFailure(failure);
521 }
522 bool allowExtendedTest() const SK_OVERRIDE {
523 return FLAGS_pathOpsExtended;
524 }
525 bool verbose() const SK_OVERRIDE { return FLAGS_veryVerbose; }
526 } reporter;
mtklein748ca3b2015-01-15 10:56:12 -0800527 WallTimer timer;
528 timer.start();
mtklein748ca3b2015-01-15 10:56:12 -0800529 if (!FLAGS_dryRun) {
mtkleinb37cb412015-03-18 05:27:14 -0700530 start("unit", "test", test->name);
mtklein55e88b22015-01-21 15:50:13 -0800531 GrContextFactory factory;
532 test->proc(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -0800533 }
534 timer.end();
mtkleinb37cb412015-03-18 05:27:14 -0700535 done(timer.fWall, "unit", "test", test->name, "", "");
mtklein748ca3b2015-01-15 10:56:12 -0800536}
537
538/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
539
mtklein55e88b22015-01-21 15:50:13 -0800540// If we're isolating all GPU-bound work to one thread (the default), this function runs all that.
541static void run_enclave_and_gpu_tests(SkTArray<Task>* tasks) {
542 run_enclave(tasks);
543 for (int i = 0; i < gGPUTests.count(); i++) {
544 run_test(&gGPUTests[i]);
545 }
546}
547
mtkleinde6fc2b2015-03-12 06:28:54 -0700548// Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung.
549// This prints something every once in a while so that it knows we're still working.
mtklein2e1c47e2015-03-12 07:16:56 -0700550static void start_keepalive() {
551 struct Loop {
552 static void forever(void*) {
553 for (;;) {
554 static const int kSec = 300;
555 #if defined(SK_BUILD_FOR_WIN)
556 Sleep(kSec * 1000);
557 #else
558 sleep(kSec);
559 #endif
mtkleinb37cb412015-03-18 05:27:14 -0700560 SkString running;
561 {
562 SkAutoMutexAcquire lock(gRunningMutex);
563 for (int i = 0; i < gRunning.count(); i++) {
564 running.appendf("\n\t%s", gRunning[i].c_str());
565 }
566 }
567 SkDebugf("\nCurrently running:%s\n", running.c_str());
mtklein2e1c47e2015-03-12 07:16:56 -0700568 }
569 }
570 };
571 static SkThread* intentionallyLeaked = new SkThread(Loop::forever);
572 intentionallyLeaked->start();
mtkleinde6fc2b2015-03-12 06:28:54 -0700573}
574
jcgregorio3b27ade2014-11-13 08:06:40 -0800575int dm_main();
caryclark17f0b6d2014-07-22 10:15:34 -0700576int dm_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -0800577 SetupCrashHandler();
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +0000578 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -0700579 SkTaskGroup::Enabler enabled(FLAGS_threads);
mtkleine67164d2015-02-02 13:24:37 -0800580 if (FLAGS_leaks) {
581 SkInstCountPrintLeaksOnExit();
582 }
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +0000583
mtklein2e1c47e2015-03-12 07:16:56 -0700584 start_keepalive();
mtkleinde6fc2b2015-03-12 06:28:54 -0700585
mtklein62bd1a62015-01-27 14:46:26 -0800586 gather_gold();
587
mtklein748ca3b2015-01-15 10:56:12 -0800588 gather_srcs();
589 gather_sinks();
590 gather_tests();
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +0000591
mtklein55e88b22015-01-21 15:50:13 -0800592 gPending = gSrcs.count() * gSinks.count() + gThreadedTests.count() + gGPUTests.count();
mtklein748ca3b2015-01-15 10:56:12 -0800593 SkDebugf("%d srcs * %d sinks + %d tests == %d tasks\n",
mtklein55e88b22015-01-21 15:50:13 -0800594 gSrcs.count(), gSinks.count(), gThreadedTests.count() + gGPUTests.count(), gPending);
mtklein748ca3b2015-01-15 10:56:12 -0800595
596 // We try to exploit as much parallelism as is safe. Most Src/Sink pairs run on any thread,
597 // but Sinks that identify as part of a particular enclave run serially on a single thread.
mtklein82d28432015-01-15 12:46:02 -0800598 // CPU tests run on any thread. GPU tests depend on --gpu_threading.
mtklein748ca3b2015-01-15 10:56:12 -0800599 SkTArray<Task> enclaves[kNumEnclaves];
600 for (int j = 0; j < gSinks.count(); j++) {
601 SkTArray<Task>& tasks = enclaves[gSinks[j]->enclave()];
602 for (int i = 0; i < gSrcs.count(); i++) {
603 tasks.push_back(Task(gSrcs[i], gSinks[j]));
604 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +0000605 }
606
mtklein748ca3b2015-01-15 10:56:12 -0800607 SkTaskGroup tg;
mtklein55e88b22015-01-21 15:50:13 -0800608 tg.batch(run_test, gThreadedTests.begin(), gThreadedTests.count());
609 for (int i = 0; i < kNumEnclaves; i++) {
610 switch(i) {
611 case kAnyThread_Enclave:
612 tg.batch(Task::Run, enclaves[i].begin(), enclaves[i].count());
613 break;
614 case kGPU_Enclave:
615 tg.add(run_enclave_and_gpu_tests, &enclaves[i]);
616 break;
617 default:
618 tg.add(run_enclave, &enclaves[i]);
619 break;
mtklein82d28432015-01-15 12:46:02 -0800620 }
mtklein55e88b22015-01-21 15:50:13 -0800621 }
mtklein748ca3b2015-01-15 10:56:12 -0800622 tg.wait();
mtklein748ca3b2015-01-15 10:56:12 -0800623 // At this point we're back in single-threaded land.
mtklein@google.comd36522d2013-10-16 13:02:15 +0000624
mtklein2f64eec2015-01-15 14:20:41 -0800625 SkDebugf("\n");
mtklein748ca3b2015-01-15 10:56:12 -0800626 if (gFailures.count() > 0) {
627 SkDebugf("Failures:\n");
628 for (int i = 0; i < gFailures.count(); i++) {
mtklein9dc09102015-01-15 15:47:33 -0800629 SkDebugf("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800630 }
631 SkDebugf("%d failures\n", gFailures.count());
632 return 1;
mtklein114c3cd2015-01-15 10:15:02 -0800633 }
mtklein748ca3b2015-01-15 10:56:12 -0800634 if (gPending > 0) {
635 SkDebugf("Hrm, we didn't seem to run everything we intended to! Please file a bug.\n");
636 return 1;
mtklein114c3cd2015-01-15 10:15:02 -0800637 }
mtklein748ca3b2015-01-15 10:56:12 -0800638 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +0000639}
jcgregorio3b27ade2014-11-13 08:06:40 -0800640
641#if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL)
642int main(int argc, char** argv) {
643 SkCommandLineFlags::Parse(argc, argv);
644 return dm_main();
645}
646#endif