blob: 44090ca1ce46ced5406554f5b5bdf8e6d3458440 [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).
msarett15bfd072015-03-24 12:24:27 -0700149 return strcmp(ext, "png") == 0 || strcmp(ext, "PNG") == 0 ||
150 strcmp(ext, "bmp") == 0 || strcmp(ext, "BMP") == 0 ||
151 strcmp(ext, "ico") == 0 || strcmp(ext, "ICO") == 0;
scroggo9b77ddd2015-03-19 06:03:39 -0700152}
153
mtklein748ca3b2015-01-15 10:56:12 -0800154static void gather_srcs() {
155 for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) {
156 push_src("gm", new GMSrc(r->factory()));
157 }
halcanaryfc37ad12015-01-30 07:31:19 -0800158 for (int i = 0; i < FLAGS_skps.count(); i++) {
159 const char* path = FLAGS_skps[i];
160 if (sk_isdir(path)) {
161 SkOSFile::Iter it(path, "skp");
162 for (SkString file; it.next(&file); ) {
163 push_src("skp", new SKPSrc(SkOSPath::Join(path, file.c_str())));
164 }
165 } else {
mtklein8d17a132015-01-30 11:42:31 -0800166 push_src("skp", new SKPSrc(path));
mtklein114c3cd2015-01-15 10:15:02 -0800167 }
168 }
halcanary23b03c32015-01-30 09:58:58 -0800169 static const char* const exts[] = {
170 "bmp", "gif", "jpg", "jpeg", "png", "webp", "ktx", "astc", "wbmp", "ico",
171 "BMP", "GIF", "JPG", "JPEG", "PNG", "WEBP", "KTX", "ASTC", "WBMP", "ICO",
172 };
173 for (int i = 0; i < FLAGS_images.count(); i++) {
174 const char* flag = FLAGS_images[i];
175 if (sk_isdir(flag)) {
176 for (size_t j = 0; j < SK_ARRAY_COUNT(exts); j++) {
177 SkOSFile::Iter it(flag, exts[j]);
178 for (SkString file; it.next(&file); ) {
179 SkString path = SkOSPath::Join(flag, file.c_str());
180 push_src("image", new ImageSrc(path)); // Decode entire image.
mtkleinedc93bc2015-01-30 13:22:23 -0800181 push_src("subset", new ImageSrc(path, 2)); // Decode into 2 x 2 subsets
scroggo9b77ddd2015-03-19 06:03:39 -0700182 if (codec_supported(exts[j])) {
183 push_src("codec", new CodecSrc(path)); // Decode with SkCodec.
184 }
halcanary23b03c32015-01-30 09:58:58 -0800185 }
mtklein114c3cd2015-01-15 10:15:02 -0800186 }
halcanary23b03c32015-01-30 09:58:58 -0800187 } else if (sk_exists(flag)) {
188 // assume that FLAGS_images[i] is a valid image if it is a file.
mtklein8d17a132015-01-30 11:42:31 -0800189 push_src("image", new ImageSrc(flag)); // Decode entire image.
mtkleinedc93bc2015-01-30 13:22:23 -0800190 push_src("subset", new ImageSrc(flag, 2)); // Decode into 2 x 2 subsets
scroggo9b77ddd2015-03-19 06:03:39 -0700191 push_src("codec", new CodecSrc(flag)); // Decode with SkCodec.
mtklein709d2c32015-01-15 08:30:25 -0800192 }
mtklein709d2c32015-01-15 08:30:25 -0800193 }
194}
195
mtklein748ca3b2015-01-15 10:56:12 -0800196static GrGLStandard get_gpu_api() {
197 if (FLAGS_gpuAPI.contains("gl")) { return kGL_GrGLStandard; }
198 if (FLAGS_gpuAPI.contains("gles")) { return kGLES_GrGLStandard; }
199 return kNone_GrGLStandard;
mtklein709d2c32015-01-15 08:30:25 -0800200}
201
mtklein748ca3b2015-01-15 10:56:12 -0800202static void push_sink(const char* tag, Sink* s) {
203 SkAutoTDelete<Sink> sink(s);
204 if (!FLAGS_config.contains(tag)) {
205 return;
mtklein114c3cd2015-01-15 10:15:02 -0800206 }
mtklein748ca3b2015-01-15 10:56:12 -0800207 // Try a noop Src as a canary. If it fails, skip this sink.
208 struct : public Src {
209 Error draw(SkCanvas*) const SK_OVERRIDE { return ""; }
210 SkISize size() const SK_OVERRIDE { return SkISize::Make(16, 16); }
211 Name name() const SK_OVERRIDE { return "noop"; }
212 } noop;
mtklein114c3cd2015-01-15 10:15:02 -0800213
mtklein748ca3b2015-01-15 10:56:12 -0800214 SkBitmap bitmap;
215 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800216 SkString log;
217 Error err = sink->draw(noop, &bitmap, &stream, &log);
mtklein4089ef72015-03-05 08:40:28 -0800218 if (err.isFatal()) {
mtklein748ca3b2015-01-15 10:56:12 -0800219 SkDebugf("Skipping %s: %s\n", tag, err.c_str());
mtklein114c3cd2015-01-15 10:15:02 -0800220 return;
221 }
222
mtklein748ca3b2015-01-15 10:56:12 -0800223 Tagged<Sink>& ts = gSinks.push_back();
224 ts.reset(sink.detach());
225 ts.tag = tag;
226}
227
228static bool gpu_supported() {
229#if SK_SUPPORT_GPU
230 return FLAGS_gpu;
231#else
232 return false;
233#endif
234}
235
236static Sink* create_sink(const char* tag) {
237#define SINK(t, sink, ...) if (0 == strcmp(t, tag)) { return new sink(__VA_ARGS__); }
238 if (gpu_supported()) {
mtklein82d28432015-01-15 12:46:02 -0800239 typedef GrContextFactory Gr;
mtklein748ca3b2015-01-15 10:56:12 -0800240 const GrGLStandard api = get_gpu_api();
mtklein82d28432015-01-15 12:46:02 -0800241 SINK("gpunull", GPUSink, Gr::kNull_GLContextType, api, 0, false, FLAGS_gpu_threading);
242 SINK("gpudebug", GPUSink, Gr::kDebug_GLContextType, api, 0, false, FLAGS_gpu_threading);
243 SINK("gpu", GPUSink, Gr::kNative_GLContextType, api, 0, false, FLAGS_gpu_threading);
244 SINK("gpudft", GPUSink, Gr::kNative_GLContextType, api, 0, true, FLAGS_gpu_threading);
245 SINK("msaa4", GPUSink, Gr::kNative_GLContextType, api, 4, false, FLAGS_gpu_threading);
246 SINK("msaa16", GPUSink, Gr::kNative_GLContextType, api, 16, false, FLAGS_gpu_threading);
247 SINK("nvprmsaa4", GPUSink, Gr::kNVPR_GLContextType, api, 4, false, FLAGS_gpu_threading);
248 SINK("nvprmsaa16", GPUSink, Gr::kNVPR_GLContextType, api, 16, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800249 #if SK_ANGLE
mtklein82d28432015-01-15 12:46:02 -0800250 SINK("angle", GPUSink, Gr::kANGLE_GLContextType, api, 0, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800251 #endif
252 #if SK_MESA
mtklein82d28432015-01-15 12:46:02 -0800253 SINK("mesa", GPUSink, Gr::kMESA_GLContextType, api, 0, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800254 #endif
mtklein114c3cd2015-01-15 10:15:02 -0800255 }
mtklein748ca3b2015-01-15 10:56:12 -0800256
tomhudsoneebc39a2015-02-23 12:18:05 -0800257#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
258 SINK("hwui", HWUISink);
259#endif
260
mtklein748ca3b2015-01-15 10:56:12 -0800261 if (FLAGS_cpu) {
262 SINK("565", RasterSink, kRGB_565_SkColorType);
263 SINK("8888", RasterSink, kN32_SkColorType);
mtklein19f30602015-01-20 13:34:39 -0800264 SINK("pdf", PDFSink);
mtklein9c3f17d2015-01-28 11:35:18 -0800265 SINK("skp", SKPSink);
mtklein8a4527e2015-01-31 20:00:58 -0800266 SINK("svg", SVGSink);
267 SINK("null", NullSink);
halcanary47ef4d52015-03-03 09:13:09 -0800268 SINK("xps", XPSSink);
mtklein748ca3b2015-01-15 10:56:12 -0800269 }
270#undef SINK
271 return NULL;
mtklein114c3cd2015-01-15 10:15:02 -0800272}
273
mtklein748ca3b2015-01-15 10:56:12 -0800274static Sink* create_via(const char* tag, Sink* wrapped) {
275#define VIA(t, via, ...) if (0 == strcmp(t, tag)) { return new via(__VA_ARGS__); }
mtklein7edca212015-01-21 13:18:51 -0800276 VIA("pipe", ViaPipe, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800277 VIA("serialize", ViaSerialization, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800278 VIA("tiles", ViaTiles, 256, 256, NULL, wrapped);
279 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800280
mtkleind603b222015-02-17 11:13:33 -0800281 if (FLAGS_matrix.count() == 4) {
mtklein748ca3b2015-01-15 10:56:12 -0800282 SkMatrix m;
mtkleind603b222015-02-17 11:13:33 -0800283 m.reset();
284 m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
285 m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
286 m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
287 m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
288 VIA("matrix", ViaMatrix, m, wrapped);
289 VIA("upright", ViaUpright, m, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800290 }
tomhudson64de1e12015-03-05 08:01:07 -0800291
292#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
293 VIA("androidsdk", ViaAndroidSDK, wrapped);
294#endif
295
mtklein748ca3b2015-01-15 10:56:12 -0800296#undef VIA
297 return NULL;
mtklein114c3cd2015-01-15 10:15:02 -0800298}
299
mtklein748ca3b2015-01-15 10:56:12 -0800300static void gather_sinks() {
301 for (int i = 0; i < FLAGS_config.count(); i++) {
302 const char* config = FLAGS_config[i];
303 SkTArray<SkString> parts;
304 SkStrSplit(config, "-", &parts);
305
306 Sink* sink = NULL;
307 for (int i = parts.count(); i-- > 0;) {
308 const char* part = parts[i].c_str();
309 Sink* next = (sink == NULL) ? create_sink(part) : create_via(part, sink);
310 if (next == NULL) {
311 SkDebugf("Skipping %s: Don't understand '%s'.\n", config, part);
312 delete sink;
313 sink = NULL;
314 break;
315 }
316 sink = next;
317 }
318 if (sink) {
319 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800320 }
321 }
322}
mtklein709d2c32015-01-15 08:30:25 -0800323
mtkleina2ef6422015-01-15 13:44:22 -0800324static bool match(const char* needle, const char* haystack) {
325 return 0 == strcmp("_", needle) || NULL != strstr(haystack, needle);
326}
327
328static ImplicitString is_blacklisted(const char* sink, const char* src, const char* name) {
329 for (int i = 0; i < FLAGS_blacklist.count() - 2; i += 3) {
330 if (match(FLAGS_blacklist[i+0], sink) &&
331 match(FLAGS_blacklist[i+1], src) &&
332 match(FLAGS_blacklist[i+2], name)) {
333 return SkStringPrintf("%s %s %s",
334 FLAGS_blacklist[i+0], FLAGS_blacklist[i+1], FLAGS_blacklist[i+2]);
335 }
336 }
337 return "";
338}
339
mtklein748ca3b2015-01-15 10:56:12 -0800340// The finest-grained unit of work we can run: draw a single Src into a single Sink,
341// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
342struct Task {
343 Task(const Tagged<Src>& src, const Tagged<Sink>& sink) : src(src), sink(sink) {}
344 const Tagged<Src>& src;
345 const Tagged<Sink>& sink;
346
347 static void Run(Task* task) {
mtkleina2ef6422015-01-15 13:44:22 -0800348 SkString name = task->src->name();
mtkleinb37cb412015-03-18 05:27:14 -0700349 SkString note;
mtkleina2ef6422015-01-15 13:44:22 -0800350 SkString whyBlacklisted = is_blacklisted(task->sink.tag, task->src.tag, name.c_str());
mtkleinb37cb412015-03-18 05:27:14 -0700351 if (!whyBlacklisted.isEmpty()) {
352 note.appendf(" (--blacklist %s)", whyBlacklisted.c_str());
353 }
mtkleinb9eb4ac2015-02-02 18:26:03 -0800354 SkString log;
mtklein748ca3b2015-01-15 10:56:12 -0800355 WallTimer timer;
356 timer.start();
mtkleina2ef6422015-01-15 13:44:22 -0800357 if (!FLAGS_dryRun && whyBlacklisted.isEmpty()) {
mtklein748ca3b2015-01-15 10:56:12 -0800358 SkBitmap bitmap;
359 SkDynamicMemoryWStream stream;
mtkleinb37cb412015-03-18 05:27:14 -0700360 start(task->sink.tag, task->src.tag, name.c_str());
mtkleinb9eb4ac2015-02-02 18:26:03 -0800361 Error err = task->sink->draw(*task->src, &bitmap, &stream, &log);
mtklein748ca3b2015-01-15 10:56:12 -0800362 if (!err.isEmpty()) {
mtklein4089ef72015-03-05 08:40:28 -0800363 timer.end();
364 if (err.isFatal()) {
365 fail(SkStringPrintf("%s %s %s: %s",
366 task->sink.tag,
367 task->src.tag,
368 name.c_str(),
369 err.c_str()));
mtkleinb37cb412015-03-18 05:27:14 -0700370 } else {
371 note.appendf(" (skipped: %s)", err.c_str());
mtklein4089ef72015-03-05 08:40:28 -0800372 }
mtkleinb37cb412015-03-18 05:27:14 -0700373 done(timer.fWall, task->sink.tag, task->src.tag, name, note, log);
mtklein4089ef72015-03-05 08:40:28 -0800374 return;
mtklein748ca3b2015-01-15 10:56:12 -0800375 }
mtklein62bd1a62015-01-27 14:46:26 -0800376 SkAutoTDelete<SkStreamAsset> data(stream.detachAsStream());
377
378 SkString md5;
379 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
380 SkMD5 hash;
381 if (data->getLength()) {
382 hash.writeStream(data, data->getLength());
383 data->rewind();
384 } else {
385 hash.write(bitmap.getPixels(), bitmap.getSize());
386 }
387 SkMD5::Digest digest;
388 hash.finish(digest);
389 for (int i = 0; i < 16; i++) {
390 md5.appendf("%02x", digest.data[i]);
391 }
392 }
393
394 if (!FLAGS_readPath.isEmpty() &&
mtkleina82f5622015-02-20 12:30:19 -0800395 !gGold.contains(Gold(task->sink.tag, task->src.tag, name, md5))) {
mtklein62bd1a62015-01-27 14:46:26 -0800396 fail(SkStringPrintf("%s not found for %s %s %s in %s",
397 md5.c_str(),
398 task->sink.tag,
399 task->src.tag,
400 name.c_str(),
401 FLAGS_readPath[0]));
402 }
403
mtklein748ca3b2015-01-15 10:56:12 -0800404 if (!FLAGS_writePath.isEmpty()) {
405 const char* ext = task->sink->fileExtension();
mtklein62bd1a62015-01-27 14:46:26 -0800406 if (data->getLength()) {
407 WriteToDisk(*task, md5, ext, data, data->getLength(), NULL);
halcanary022afb82015-01-30 11:00:12 -0800408 SkASSERT(bitmap.drawsNothing());
409 } else if (!bitmap.drawsNothing()) {
mtklein62bd1a62015-01-27 14:46:26 -0800410 WriteToDisk(*task, md5, ext, NULL, 0, &bitmap);
mtklein748ca3b2015-01-15 10:56:12 -0800411 }
412 }
413 }
414 timer.end();
mtkleinb37cb412015-03-18 05:27:14 -0700415 done(timer.fWall, task->sink.tag, task->src.tag, name, note, log);
mtklein748ca3b2015-01-15 10:56:12 -0800416 }
417
418 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -0800419 SkString md5,
420 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -0800421 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -0800422 const SkBitmap* bitmap) {
mtklein748ca3b2015-01-15 10:56:12 -0800423 JsonWriter::BitmapResult result;
424 result.name = task.src->name();
425 result.config = task.sink.tag;
426 result.sourceType = task.src.tag;
427 result.ext = ext;
mtklein62bd1a62015-01-27 14:46:26 -0800428 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -0800429 JsonWriter::AddBitmapResult(result);
430
431 const char* dir = FLAGS_writePath[0];
432 if (0 == strcmp(dir, "@")) { // Needed for iOS.
433 dir = FLAGS_resourcePath[0];
434 }
435 sk_mkdir(dir);
436
437 SkString path;
438 if (FLAGS_nameByHash) {
439 path = SkOSPath::Join(dir, result.md5.c_str());
440 path.append(".");
441 path.append(ext);
442 if (sk_exists(path.c_str())) {
443 return; // Content-addressed. If it exists already, we're done.
444 }
445 } else {
446 path = SkOSPath::Join(dir, task.sink.tag);
447 sk_mkdir(path.c_str());
448 path = SkOSPath::Join(path.c_str(), task.src.tag);
449 sk_mkdir(path.c_str());
450 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
451 path.append(".");
452 path.append(ext);
453 }
454
455 SkFILEWStream file(path.c_str());
456 if (!file.isValid()) {
457 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
458 return;
459 }
460
mtklein748ca3b2015-01-15 10:56:12 -0800461 if (bitmap) {
462 // We can't encode A8 bitmaps as PNGs. Convert them to 8888 first.
463 SkBitmap converted;
464 if (bitmap->info().colorType() == kAlpha_8_SkColorType) {
465 if (!bitmap->copyTo(&converted, kN32_SkColorType)) {
466 fail("Can't convert A8 to 8888.\n");
467 return;
468 }
469 bitmap = &converted;
470 }
471 if (!SkImageEncoder::EncodeStream(&file, *bitmap, SkImageEncoder::kPNG_Type, 100)) {
472 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
473 return;
474 }
475 } else {
476 if (!file.writeStream(data, len)) {
477 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
478 return;
479 }
480 }
481 }
482};
483
484// Run all tasks in the same enclave serially on the same thread.
485// They can't possibly run concurrently with each other.
486static void run_enclave(SkTArray<Task>* tasks) {
487 for (int i = 0; i < tasks->count(); i++) {
488 Task::Run(tasks->begin() + i);
489 }
490}
491
492/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
493
494// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
495
mtklein55e88b22015-01-21 15:50:13 -0800496static SkTDArray<skiatest::Test> gThreadedTests, gGPUTests;
mtklein748ca3b2015-01-15 10:56:12 -0800497
498static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -0800499 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -0800500 return;
501 }
halcanary87f3ba42015-01-20 09:30:20 -0800502 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r;
503 r = r->next()) {
504 // Despite its name, factory() is returning a reference to
505 // link-time static const POD data.
506 const skiatest::Test& test = r->factory();
507 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -0800508 continue;
509 }
halcanary87f3ba42015-01-20 09:30:20 -0800510 if (test.needsGpu && gpu_supported()) {
mtklein55e88b22015-01-21 15:50:13 -0800511 (FLAGS_gpu_threading ? gThreadedTests : gGPUTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -0800512 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein55e88b22015-01-21 15:50:13 -0800513 gThreadedTests.push(test);
mtklein82d28432015-01-15 12:46:02 -0800514 }
mtklein748ca3b2015-01-15 10:56:12 -0800515 }
516}
517
halcanary87f3ba42015-01-20 09:30:20 -0800518static void run_test(skiatest::Test* test) {
519 struct : public skiatest::Reporter {
520 void reportFailed(const skiatest::Failure& failure) SK_OVERRIDE {
521 fail(failure.toString());
522 JsonWriter::AddTestFailure(failure);
523 }
524 bool allowExtendedTest() const SK_OVERRIDE {
525 return FLAGS_pathOpsExtended;
526 }
527 bool verbose() const SK_OVERRIDE { return FLAGS_veryVerbose; }
528 } reporter;
mtklein748ca3b2015-01-15 10:56:12 -0800529 WallTimer timer;
530 timer.start();
mtklein748ca3b2015-01-15 10:56:12 -0800531 if (!FLAGS_dryRun) {
mtkleinb37cb412015-03-18 05:27:14 -0700532 start("unit", "test", test->name);
mtklein55e88b22015-01-21 15:50:13 -0800533 GrContextFactory factory;
534 test->proc(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -0800535 }
536 timer.end();
mtkleinb37cb412015-03-18 05:27:14 -0700537 done(timer.fWall, "unit", "test", test->name, "", "");
mtklein748ca3b2015-01-15 10:56:12 -0800538}
539
540/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
541
mtklein55e88b22015-01-21 15:50:13 -0800542// If we're isolating all GPU-bound work to one thread (the default), this function runs all that.
543static void run_enclave_and_gpu_tests(SkTArray<Task>* tasks) {
544 run_enclave(tasks);
545 for (int i = 0; i < gGPUTests.count(); i++) {
546 run_test(&gGPUTests[i]);
547 }
548}
549
mtkleinde6fc2b2015-03-12 06:28:54 -0700550// Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung.
551// This prints something every once in a while so that it knows we're still working.
mtklein2e1c47e2015-03-12 07:16:56 -0700552static void start_keepalive() {
553 struct Loop {
554 static void forever(void*) {
555 for (;;) {
556 static const int kSec = 300;
557 #if defined(SK_BUILD_FOR_WIN)
558 Sleep(kSec * 1000);
559 #else
560 sleep(kSec);
561 #endif
mtkleinb37cb412015-03-18 05:27:14 -0700562 SkString running;
563 {
564 SkAutoMutexAcquire lock(gRunningMutex);
565 for (int i = 0; i < gRunning.count(); i++) {
566 running.appendf("\n\t%s", gRunning[i].c_str());
567 }
568 }
569 SkDebugf("\nCurrently running:%s\n", running.c_str());
mtklein2e1c47e2015-03-12 07:16:56 -0700570 }
571 }
572 };
573 static SkThread* intentionallyLeaked = new SkThread(Loop::forever);
574 intentionallyLeaked->start();
mtkleinde6fc2b2015-03-12 06:28:54 -0700575}
576
jcgregorio3b27ade2014-11-13 08:06:40 -0800577int dm_main();
caryclark17f0b6d2014-07-22 10:15:34 -0700578int dm_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -0800579 SetupCrashHandler();
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +0000580 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -0700581 SkTaskGroup::Enabler enabled(FLAGS_threads);
mtkleine67164d2015-02-02 13:24:37 -0800582 if (FLAGS_leaks) {
583 SkInstCountPrintLeaksOnExit();
584 }
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +0000585
mtklein2e1c47e2015-03-12 07:16:56 -0700586 start_keepalive();
mtkleinde6fc2b2015-03-12 06:28:54 -0700587
mtklein62bd1a62015-01-27 14:46:26 -0800588 gather_gold();
589
mtklein748ca3b2015-01-15 10:56:12 -0800590 gather_srcs();
591 gather_sinks();
592 gather_tests();
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +0000593
mtklein55e88b22015-01-21 15:50:13 -0800594 gPending = gSrcs.count() * gSinks.count() + gThreadedTests.count() + gGPUTests.count();
mtklein748ca3b2015-01-15 10:56:12 -0800595 SkDebugf("%d srcs * %d sinks + %d tests == %d tasks\n",
mtklein55e88b22015-01-21 15:50:13 -0800596 gSrcs.count(), gSinks.count(), gThreadedTests.count() + gGPUTests.count(), gPending);
mtklein748ca3b2015-01-15 10:56:12 -0800597
598 // We try to exploit as much parallelism as is safe. Most Src/Sink pairs run on any thread,
599 // but Sinks that identify as part of a particular enclave run serially on a single thread.
mtklein82d28432015-01-15 12:46:02 -0800600 // CPU tests run on any thread. GPU tests depend on --gpu_threading.
mtklein748ca3b2015-01-15 10:56:12 -0800601 SkTArray<Task> enclaves[kNumEnclaves];
602 for (int j = 0; j < gSinks.count(); j++) {
603 SkTArray<Task>& tasks = enclaves[gSinks[j]->enclave()];
604 for (int i = 0; i < gSrcs.count(); i++) {
605 tasks.push_back(Task(gSrcs[i], gSinks[j]));
606 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +0000607 }
608
mtklein748ca3b2015-01-15 10:56:12 -0800609 SkTaskGroup tg;
mtklein55e88b22015-01-21 15:50:13 -0800610 tg.batch(run_test, gThreadedTests.begin(), gThreadedTests.count());
611 for (int i = 0; i < kNumEnclaves; i++) {
612 switch(i) {
613 case kAnyThread_Enclave:
614 tg.batch(Task::Run, enclaves[i].begin(), enclaves[i].count());
615 break;
616 case kGPU_Enclave:
617 tg.add(run_enclave_and_gpu_tests, &enclaves[i]);
618 break;
619 default:
620 tg.add(run_enclave, &enclaves[i]);
621 break;
mtklein82d28432015-01-15 12:46:02 -0800622 }
mtklein55e88b22015-01-21 15:50:13 -0800623 }
mtklein748ca3b2015-01-15 10:56:12 -0800624 tg.wait();
mtklein748ca3b2015-01-15 10:56:12 -0800625 // At this point we're back in single-threaded land.
mtklein@google.comd36522d2013-10-16 13:02:15 +0000626
mtklein2f64eec2015-01-15 14:20:41 -0800627 SkDebugf("\n");
mtklein748ca3b2015-01-15 10:56:12 -0800628 if (gFailures.count() > 0) {
629 SkDebugf("Failures:\n");
630 for (int i = 0; i < gFailures.count(); i++) {
mtklein9dc09102015-01-15 15:47:33 -0800631 SkDebugf("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800632 }
633 SkDebugf("%d failures\n", gFailures.count());
634 return 1;
mtklein114c3cd2015-01-15 10:15:02 -0800635 }
mtklein748ca3b2015-01-15 10:56:12 -0800636 if (gPending > 0) {
637 SkDebugf("Hrm, we didn't seem to run everything we intended to! Please file a bug.\n");
638 return 1;
mtklein114c3cd2015-01-15 10:15:02 -0800639 }
mtklein748ca3b2015-01-15 10:56:12 -0800640 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +0000641}
jcgregorio3b27ade2014-11-13 08:06:40 -0800642
643#if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL)
644int main(int argc, char** argv) {
645 SkCommandLineFlags::Parse(argc, argv);
646 return dm_main();
647}
648#endif