blob: 870b371cc44d089cc53696d81a2c51e2fabdb1ef [file] [log] [blame]
scroggo478652e2015-03-25 07:11:02 -07001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
jcgregorio3b27ade2014-11-13 08:06:40 -08008#include "CrashHandler.h"
mtklein748ca3b2015-01-15 10:56:12 -08009#include "DMJsonWriter.h"
10#include "DMSrcSink.h"
tomhudsoneebc39a2015-02-23 12:18:05 -080011#include "DMSrcSinkAndroid.h"
mtklein748ca3b2015-01-15 10:56:12 -080012#include "OverwriteLine.h"
13#include "ProcStats.h"
14#include "SkBBHFactory.h"
mtklein62bd1a62015-01-27 14:46:26 -080015#include "SkChecksum.h"
caryclark17f0b6d2014-07-22 10:15:34 -070016#include "SkCommonFlags.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +000017#include "SkForceLinking.h"
18#include "SkGraphics.h"
mtkleine67164d2015-02-02 13:24:37 -080019#include "SkInstCnt.h"
mtklein748ca3b2015-01-15 10:56:12 -080020#include "SkMD5.h"
mtklein1d0f1642014-09-08 08:05:18 -070021#include "SkOSFile.h"
mtkleina82f5622015-02-20 12:30:19 -080022#include "SkTHash.h"
mtklein406654b2014-09-03 15:34:37 -070023#include "SkTaskGroup.h"
mtkleinde6fc2b2015-03-12 06:28:54 -070024#include "SkThreadUtils.h"
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +000025#include "Test.h"
mtklein748ca3b2015-01-15 10:56:12 -080026#include "Timer.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +000027
scroggo9c59ebc2015-03-25 13:48:49 -070028DEFINE_string(src, "tests gm skp image subset codec scanline", "Source types to test.");
mtklein748ca3b2015-01-15 10:56:12 -080029DEFINE_bool(nameByHash, false,
30 "If true, write to FLAGS_writePath[0]/<hash>.png instead of "
31 "to FLAGS_writePath[0]/<config>/<sourceType>/<name>.png");
32DEFINE_bool2(pathOpsExtended, x, false, "Run extended pathOps tests.");
mtkleind603b222015-02-17 11:13:33 -080033DEFINE_string(matrix, "1 0 0 1",
34 "2x2 scale+skew matrix to apply or upright when using "
35 "'matrix' or 'upright' in config.");
mtklein82d28432015-01-15 12:46:02 -080036DEFINE_bool(gpu_threading, false, "Allow GPU work to run on multiple threads?");
mtklein@google.comd36522d2013-10-16 13:02:15 +000037
mtkleina2ef6422015-01-15 13:44:22 -080038DEFINE_string(blacklist, "",
39 "Space-separated config/src/name triples to blacklist. '_' matches anything. E.g. \n"
40 "'--blacklist gpu skp _' will blacklist all SKPs drawn into the gpu config.\n"
41 "'--blacklist gpu skp _ 8888 gm aarects' will also blacklist the aarects GM on 8888.");
42
mtklein62bd1a62015-01-27 14:46:26 -080043DEFINE_string2(readPath, r, "", "If set check for equality with golden results in this directory.");
44
mtklein@google.comd36522d2013-10-16 13:02:15 +000045__SK_FORCE_IMAGE_DECODER_LINKING;
mtklein748ca3b2015-01-15 10:56:12 -080046using namespace DM;
mtklein@google.comd36522d2013-10-16 13:02:15 +000047
mtklein748ca3b2015-01-15 10:56:12 -080048/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
49
50SK_DECLARE_STATIC_MUTEX(gFailuresMutex);
51static SkTArray<SkString> gFailures;
52
53static void fail(ImplicitString err) {
54 SkAutoMutexAcquire lock(gFailuresMutex);
55 SkDebugf("\n\nFAILURE: %s\n\n", err.c_str());
56 gFailures.push_back(err);
halcanary9e398f72015-01-10 11:18:04 -080057}
58
mtkleinb37cb412015-03-18 05:27:14 -070059static int32_t gPending = 0; // Atomic. Total number of running and queued tasks.
60
61SK_DECLARE_STATIC_MUTEX(gRunningMutex);
62static SkTArray<SkString> gRunning;
mtklein748ca3b2015-01-15 10:56:12 -080063
mtkleinb9eb4ac2015-02-02 18:26:03 -080064static void done(double ms,
65 ImplicitString config, ImplicitString src, ImplicitString name,
mtkleinb37cb412015-03-18 05:27:14 -070066 ImplicitString note, ImplicitString log) {
67 SkString id = SkStringPrintf("%s %s %s", config.c_str(), src.c_str(), name.c_str());
68 {
69 SkAutoMutexAcquire lock(gRunningMutex);
70 for (int i = 0; i < gRunning.count(); i++) {
71 if (gRunning[i] == id) {
72 gRunning.removeShuffle(i);
73 break;
74 }
75 }
76 }
77 if (!FLAGS_verbose) {
78 note = "";
79 }
mtkleinb9eb4ac2015-02-02 18:26:03 -080080 if (!log.isEmpty()) {
81 log.prepend("\n");
82 }
mtklein6dee2ad2015-02-05 09:53:44 -080083 auto pending = sk_atomic_dec(&gPending)-1;
mtkleinb37cb412015-03-18 05:27:14 -070084 SkDebugf("%s(%4dMB %5d) %s\t%s%s%s", FLAGS_verbose ? "\n" : kSkOverwriteLine
85 , sk_tools::getBestResidentSetSizeMB()
86 , pending
87 , HumanizeMs(ms).c_str()
88 , id.c_str()
89 , note.c_str()
90 , log.c_str());
mtkleina17241b2015-01-23 05:48:00 -080091 // We write our dm.json file every once in a while in case we crash.
92 // Notice this also handles the final dm.json when pending == 0.
93 if (pending % 500 == 0) {
94 JsonWriter::DumpJson();
95 }
mtklein@google.comd36522d2013-10-16 13:02:15 +000096}
97
mtkleinb37cb412015-03-18 05:27:14 -070098static void start(ImplicitString config, ImplicitString src, ImplicitString name) {
99 SkString id = SkStringPrintf("%s %s %s", config.c_str(), src.c_str(), name.c_str());
100 SkAutoMutexAcquire lock(gRunningMutex);
101 gRunning.push_back(id);
102}
103
mtklein748ca3b2015-01-15 10:56:12 -0800104/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
mtklein114c3cd2015-01-15 10:15:02 -0800105
mtklein62bd1a62015-01-27 14:46:26 -0800106struct Gold : public SkString {
mtkleina82f5622015-02-20 12:30:19 -0800107 Gold() : SkString("") {}
mtklein62bd1a62015-01-27 14:46:26 -0800108 Gold(ImplicitString sink, ImplicitString src, ImplicitString name, ImplicitString md5)
109 : SkString("") {
110 this->append(sink);
111 this->append(src);
112 this->append(name);
113 this->append(md5);
mtklein62bd1a62015-01-27 14:46:26 -0800114 }
mtklein02f46cf2015-03-20 13:48:42 -0700115 static uint32_t Hash(const Gold& g) { return SkGoodHash((const SkString&)g); }
mtklein62bd1a62015-01-27 14:46:26 -0800116};
mtkleina82f5622015-02-20 12:30:19 -0800117static SkTHashSet<Gold, Gold::Hash> gGold;
mtklein62bd1a62015-01-27 14:46:26 -0800118
119static void add_gold(JsonWriter::BitmapResult r) {
mtkleina82f5622015-02-20 12:30:19 -0800120 gGold.add(Gold(r.config, r.sourceType, r.name, r.md5));
mtklein62bd1a62015-01-27 14:46:26 -0800121}
122
123static void gather_gold() {
124 if (!FLAGS_readPath.isEmpty()) {
125 SkString path(FLAGS_readPath[0]);
126 path.append("/dm.json");
127 if (!JsonWriter::ReadJson(path.c_str(), add_gold)) {
128 fail(SkStringPrintf("Couldn't read %s for golden results.", path.c_str()));
129 }
130 }
131}
132
133/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
134
mtklein748ca3b2015-01-15 10:56:12 -0800135template <typename T>
136struct Tagged : public SkAutoTDelete<T> { const char* tag; };
137
138static const bool kMemcpyOK = true;
139
140static SkTArray<Tagged<Src>, kMemcpyOK> gSrcs;
141static SkTArray<Tagged<Sink>, kMemcpyOK> gSinks;
142
143static void push_src(const char* tag, Src* s) {
144 SkAutoTDelete<Src> src(s);
145 if (FLAGS_src.contains(tag) &&
146 !SkCommandLineFlags::ShouldSkip(FLAGS_match, src->name().c_str())) {
147 Tagged<Src>& s = gSrcs.push_back();
148 s.reset(src.detach());
149 s.tag = tag;
mtklein114c3cd2015-01-15 10:15:02 -0800150 }
mtklein748ca3b2015-01-15 10:56:12 -0800151}
mtklein114c3cd2015-01-15 10:15:02 -0800152
scroggo9b77ddd2015-03-19 06:03:39 -0700153static bool codec_supported(const char* ext) {
154 // FIXME: Once other versions of SkCodec are available, we can add them to this
155 // list (and eventually we can remove this check once they are all supported).
msarett9bde9182015-03-25 05:27:48 -0700156 return strcmp(ext, "png") == 0 || strcmp(ext, "PNG") == 0 ||
157 strcmp(ext, "bmp") == 0 || strcmp(ext, "BMP") == 0 ||
158 strcmp(ext, "ico") == 0 || strcmp(ext, "ICO") == 0;
scroggo9b77ddd2015-03-19 06:03:39 -0700159}
160
mtklein748ca3b2015-01-15 10:56:12 -0800161static void gather_srcs() {
162 for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) {
163 push_src("gm", new GMSrc(r->factory()));
164 }
halcanaryfc37ad12015-01-30 07:31:19 -0800165 for (int i = 0; i < FLAGS_skps.count(); i++) {
166 const char* path = FLAGS_skps[i];
167 if (sk_isdir(path)) {
168 SkOSFile::Iter it(path, "skp");
169 for (SkString file; it.next(&file); ) {
170 push_src("skp", new SKPSrc(SkOSPath::Join(path, file.c_str())));
171 }
172 } else {
mtklein8d17a132015-01-30 11:42:31 -0800173 push_src("skp", new SKPSrc(path));
mtklein114c3cd2015-01-15 10:15:02 -0800174 }
175 }
halcanary23b03c32015-01-30 09:58:58 -0800176 static const char* const exts[] = {
177 "bmp", "gif", "jpg", "jpeg", "png", "webp", "ktx", "astc", "wbmp", "ico",
178 "BMP", "GIF", "JPG", "JPEG", "PNG", "WEBP", "KTX", "ASTC", "WBMP", "ICO",
179 };
180 for (int i = 0; i < FLAGS_images.count(); i++) {
181 const char* flag = FLAGS_images[i];
182 if (sk_isdir(flag)) {
183 for (size_t j = 0; j < SK_ARRAY_COUNT(exts); j++) {
184 SkOSFile::Iter it(flag, exts[j]);
185 for (SkString file; it.next(&file); ) {
186 SkString path = SkOSPath::Join(flag, file.c_str());
187 push_src("image", new ImageSrc(path)); // Decode entire image.
mtkleinedc93bc2015-01-30 13:22:23 -0800188 push_src("subset", new ImageSrc(path, 2)); // Decode into 2 x 2 subsets
scroggo9b77ddd2015-03-19 06:03:39 -0700189 if (codec_supported(exts[j])) {
scroggo9c59ebc2015-03-25 13:48:49 -0700190 push_src("codec", new CodecSrc(path, CodecSrc::kNormal_Mode));
191 push_src("scanline", new CodecSrc(path, CodecSrc::kScanline_Mode));
scroggo9b77ddd2015-03-19 06:03:39 -0700192 }
halcanary23b03c32015-01-30 09:58:58 -0800193 }
mtklein114c3cd2015-01-15 10:15:02 -0800194 }
halcanary23b03c32015-01-30 09:58:58 -0800195 } else if (sk_exists(flag)) {
196 // assume that FLAGS_images[i] is a valid image if it is a file.
mtklein8d17a132015-01-30 11:42:31 -0800197 push_src("image", new ImageSrc(flag)); // Decode entire image.
mtkleinedc93bc2015-01-30 13:22:23 -0800198 push_src("subset", new ImageSrc(flag, 2)); // Decode into 2 x 2 subsets
scroggo9c59ebc2015-03-25 13:48:49 -0700199 push_src("codec", new CodecSrc(flag, CodecSrc::kNormal_Mode));
200 push_src("scanline", new CodecSrc(flag, CodecSrc::kScanline_Mode));
mtklein709d2c32015-01-15 08:30:25 -0800201 }
mtklein709d2c32015-01-15 08:30:25 -0800202 }
203}
204
mtklein748ca3b2015-01-15 10:56:12 -0800205static GrGLStandard get_gpu_api() {
206 if (FLAGS_gpuAPI.contains("gl")) { return kGL_GrGLStandard; }
207 if (FLAGS_gpuAPI.contains("gles")) { return kGLES_GrGLStandard; }
208 return kNone_GrGLStandard;
mtklein709d2c32015-01-15 08:30:25 -0800209}
210
mtklein748ca3b2015-01-15 10:56:12 -0800211static void push_sink(const char* tag, Sink* s) {
212 SkAutoTDelete<Sink> sink(s);
213 if (!FLAGS_config.contains(tag)) {
214 return;
mtklein114c3cd2015-01-15 10:15:02 -0800215 }
mtklein748ca3b2015-01-15 10:56:12 -0800216 // Try a noop Src as a canary. If it fails, skip this sink.
217 struct : public Src {
mtklein36352bf2015-03-25 18:17:31 -0700218 Error draw(SkCanvas*) const override { return ""; }
219 SkISize size() const override { return SkISize::Make(16, 16); }
220 Name name() const override { return "noop"; }
mtklein748ca3b2015-01-15 10:56:12 -0800221 } noop;
mtklein114c3cd2015-01-15 10:15:02 -0800222
mtklein748ca3b2015-01-15 10:56:12 -0800223 SkBitmap bitmap;
224 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800225 SkString log;
226 Error err = sink->draw(noop, &bitmap, &stream, &log);
mtklein4089ef72015-03-05 08:40:28 -0800227 if (err.isFatal()) {
mtklein748ca3b2015-01-15 10:56:12 -0800228 SkDebugf("Skipping %s: %s\n", tag, err.c_str());
mtklein114c3cd2015-01-15 10:15:02 -0800229 return;
230 }
231
mtklein748ca3b2015-01-15 10:56:12 -0800232 Tagged<Sink>& ts = gSinks.push_back();
233 ts.reset(sink.detach());
234 ts.tag = tag;
235}
236
237static bool gpu_supported() {
238#if SK_SUPPORT_GPU
239 return FLAGS_gpu;
240#else
241 return false;
242#endif
243}
244
245static Sink* create_sink(const char* tag) {
246#define SINK(t, sink, ...) if (0 == strcmp(t, tag)) { return new sink(__VA_ARGS__); }
247 if (gpu_supported()) {
mtklein82d28432015-01-15 12:46:02 -0800248 typedef GrContextFactory Gr;
mtklein748ca3b2015-01-15 10:56:12 -0800249 const GrGLStandard api = get_gpu_api();
mtklein82d28432015-01-15 12:46:02 -0800250 SINK("gpunull", GPUSink, Gr::kNull_GLContextType, api, 0, false, FLAGS_gpu_threading);
251 SINK("gpudebug", GPUSink, Gr::kDebug_GLContextType, api, 0, false, FLAGS_gpu_threading);
252 SINK("gpu", GPUSink, Gr::kNative_GLContextType, api, 0, false, FLAGS_gpu_threading);
253 SINK("gpudft", GPUSink, Gr::kNative_GLContextType, api, 0, true, FLAGS_gpu_threading);
254 SINK("msaa4", GPUSink, Gr::kNative_GLContextType, api, 4, false, FLAGS_gpu_threading);
255 SINK("msaa16", GPUSink, Gr::kNative_GLContextType, api, 16, false, FLAGS_gpu_threading);
256 SINK("nvprmsaa4", GPUSink, Gr::kNVPR_GLContextType, api, 4, false, FLAGS_gpu_threading);
257 SINK("nvprmsaa16", GPUSink, Gr::kNVPR_GLContextType, api, 16, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800258 #if SK_ANGLE
mtklein82d28432015-01-15 12:46:02 -0800259 SINK("angle", GPUSink, Gr::kANGLE_GLContextType, api, 0, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800260 #endif
261 #if SK_MESA
mtklein82d28432015-01-15 12:46:02 -0800262 SINK("mesa", GPUSink, Gr::kMESA_GLContextType, api, 0, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800263 #endif
mtklein114c3cd2015-01-15 10:15:02 -0800264 }
mtklein748ca3b2015-01-15 10:56:12 -0800265
tomhudsoneebc39a2015-02-23 12:18:05 -0800266#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
267 SINK("hwui", HWUISink);
268#endif
269
mtklein748ca3b2015-01-15 10:56:12 -0800270 if (FLAGS_cpu) {
271 SINK("565", RasterSink, kRGB_565_SkColorType);
272 SINK("8888", RasterSink, kN32_SkColorType);
mtklein19f30602015-01-20 13:34:39 -0800273 SINK("pdf", PDFSink);
mtklein9c3f17d2015-01-28 11:35:18 -0800274 SINK("skp", SKPSink);
mtklein8a4527e2015-01-31 20:00:58 -0800275 SINK("svg", SVGSink);
276 SINK("null", NullSink);
halcanary47ef4d52015-03-03 09:13:09 -0800277 SINK("xps", XPSSink);
mtklein748ca3b2015-01-15 10:56:12 -0800278 }
279#undef SINK
280 return NULL;
mtklein114c3cd2015-01-15 10:15:02 -0800281}
282
mtklein748ca3b2015-01-15 10:56:12 -0800283static Sink* create_via(const char* tag, Sink* wrapped) {
284#define VIA(t, via, ...) if (0 == strcmp(t, tag)) { return new via(__VA_ARGS__); }
mtklein7edca212015-01-21 13:18:51 -0800285 VIA("pipe", ViaPipe, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800286 VIA("serialize", ViaSerialization, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800287 VIA("tiles", ViaTiles, 256, 256, NULL, wrapped);
288 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800289
mtkleind603b222015-02-17 11:13:33 -0800290 if (FLAGS_matrix.count() == 4) {
mtklein748ca3b2015-01-15 10:56:12 -0800291 SkMatrix m;
mtkleind603b222015-02-17 11:13:33 -0800292 m.reset();
293 m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
294 m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
295 m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
296 m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
297 VIA("matrix", ViaMatrix, m, wrapped);
298 VIA("upright", ViaUpright, m, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800299 }
tomhudson64de1e12015-03-05 08:01:07 -0800300
301#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
302 VIA("androidsdk", ViaAndroidSDK, wrapped);
303#endif
304
mtklein748ca3b2015-01-15 10:56:12 -0800305#undef VIA
306 return NULL;
mtklein114c3cd2015-01-15 10:15:02 -0800307}
308
mtklein748ca3b2015-01-15 10:56:12 -0800309static void gather_sinks() {
310 for (int i = 0; i < FLAGS_config.count(); i++) {
311 const char* config = FLAGS_config[i];
312 SkTArray<SkString> parts;
313 SkStrSplit(config, "-", &parts);
314
315 Sink* sink = NULL;
316 for (int i = parts.count(); i-- > 0;) {
317 const char* part = parts[i].c_str();
318 Sink* next = (sink == NULL) ? create_sink(part) : create_via(part, sink);
319 if (next == NULL) {
320 SkDebugf("Skipping %s: Don't understand '%s'.\n", config, part);
321 delete sink;
322 sink = NULL;
323 break;
324 }
325 sink = next;
326 }
327 if (sink) {
328 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800329 }
330 }
331}
mtklein709d2c32015-01-15 08:30:25 -0800332
mtkleina2ef6422015-01-15 13:44:22 -0800333static bool match(const char* needle, const char* haystack) {
334 return 0 == strcmp("_", needle) || NULL != strstr(haystack, needle);
335}
336
337static ImplicitString is_blacklisted(const char* sink, const char* src, const char* name) {
338 for (int i = 0; i < FLAGS_blacklist.count() - 2; i += 3) {
339 if (match(FLAGS_blacklist[i+0], sink) &&
340 match(FLAGS_blacklist[i+1], src) &&
341 match(FLAGS_blacklist[i+2], name)) {
342 return SkStringPrintf("%s %s %s",
343 FLAGS_blacklist[i+0], FLAGS_blacklist[i+1], FLAGS_blacklist[i+2]);
344 }
345 }
346 return "";
347}
348
mtklein748ca3b2015-01-15 10:56:12 -0800349// The finest-grained unit of work we can run: draw a single Src into a single Sink,
350// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
351struct Task {
352 Task(const Tagged<Src>& src, const Tagged<Sink>& sink) : src(src), sink(sink) {}
353 const Tagged<Src>& src;
354 const Tagged<Sink>& sink;
355
356 static void Run(Task* task) {
mtkleina2ef6422015-01-15 13:44:22 -0800357 SkString name = task->src->name();
mtkleinb37cb412015-03-18 05:27:14 -0700358 SkString note;
mtkleina2ef6422015-01-15 13:44:22 -0800359 SkString whyBlacklisted = is_blacklisted(task->sink.tag, task->src.tag, name.c_str());
mtkleinb37cb412015-03-18 05:27:14 -0700360 if (!whyBlacklisted.isEmpty()) {
361 note.appendf(" (--blacklist %s)", whyBlacklisted.c_str());
362 }
mtkleinb9eb4ac2015-02-02 18:26:03 -0800363 SkString log;
mtklein748ca3b2015-01-15 10:56:12 -0800364 WallTimer timer;
365 timer.start();
mtkleina2ef6422015-01-15 13:44:22 -0800366 if (!FLAGS_dryRun && whyBlacklisted.isEmpty()) {
mtklein748ca3b2015-01-15 10:56:12 -0800367 SkBitmap bitmap;
368 SkDynamicMemoryWStream stream;
mtkleinb37cb412015-03-18 05:27:14 -0700369 start(task->sink.tag, task->src.tag, name.c_str());
mtkleinb9eb4ac2015-02-02 18:26:03 -0800370 Error err = task->sink->draw(*task->src, &bitmap, &stream, &log);
mtklein748ca3b2015-01-15 10:56:12 -0800371 if (!err.isEmpty()) {
mtklein4089ef72015-03-05 08:40:28 -0800372 timer.end();
373 if (err.isFatal()) {
374 fail(SkStringPrintf("%s %s %s: %s",
375 task->sink.tag,
376 task->src.tag,
377 name.c_str(),
378 err.c_str()));
mtkleinb37cb412015-03-18 05:27:14 -0700379 } else {
380 note.appendf(" (skipped: %s)", err.c_str());
mtklein4089ef72015-03-05 08:40:28 -0800381 }
mtkleinb37cb412015-03-18 05:27:14 -0700382 done(timer.fWall, task->sink.tag, task->src.tag, name, note, log);
mtklein4089ef72015-03-05 08:40:28 -0800383 return;
mtklein748ca3b2015-01-15 10:56:12 -0800384 }
mtklein62bd1a62015-01-27 14:46:26 -0800385 SkAutoTDelete<SkStreamAsset> data(stream.detachAsStream());
386
387 SkString md5;
388 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
389 SkMD5 hash;
390 if (data->getLength()) {
391 hash.writeStream(data, data->getLength());
392 data->rewind();
393 } else {
394 hash.write(bitmap.getPixels(), bitmap.getSize());
395 }
396 SkMD5::Digest digest;
397 hash.finish(digest);
398 for (int i = 0; i < 16; i++) {
399 md5.appendf("%02x", digest.data[i]);
400 }
401 }
402
403 if (!FLAGS_readPath.isEmpty() &&
mtkleina82f5622015-02-20 12:30:19 -0800404 !gGold.contains(Gold(task->sink.tag, task->src.tag, name, md5))) {
mtklein62bd1a62015-01-27 14:46:26 -0800405 fail(SkStringPrintf("%s not found for %s %s %s in %s",
406 md5.c_str(),
407 task->sink.tag,
408 task->src.tag,
409 name.c_str(),
410 FLAGS_readPath[0]));
411 }
412
mtklein748ca3b2015-01-15 10:56:12 -0800413 if (!FLAGS_writePath.isEmpty()) {
414 const char* ext = task->sink->fileExtension();
mtklein62bd1a62015-01-27 14:46:26 -0800415 if (data->getLength()) {
416 WriteToDisk(*task, md5, ext, data, data->getLength(), NULL);
halcanary022afb82015-01-30 11:00:12 -0800417 SkASSERT(bitmap.drawsNothing());
418 } else if (!bitmap.drawsNothing()) {
mtklein62bd1a62015-01-27 14:46:26 -0800419 WriteToDisk(*task, md5, ext, NULL, 0, &bitmap);
mtklein748ca3b2015-01-15 10:56:12 -0800420 }
421 }
422 }
423 timer.end();
mtkleinb37cb412015-03-18 05:27:14 -0700424 done(timer.fWall, task->sink.tag, task->src.tag, name, note, log);
mtklein748ca3b2015-01-15 10:56:12 -0800425 }
426
427 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -0800428 SkString md5,
429 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -0800430 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -0800431 const SkBitmap* bitmap) {
mtklein748ca3b2015-01-15 10:56:12 -0800432 JsonWriter::BitmapResult result;
433 result.name = task.src->name();
434 result.config = task.sink.tag;
435 result.sourceType = task.src.tag;
436 result.ext = ext;
mtklein62bd1a62015-01-27 14:46:26 -0800437 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -0800438 JsonWriter::AddBitmapResult(result);
439
440 const char* dir = FLAGS_writePath[0];
441 if (0 == strcmp(dir, "@")) { // Needed for iOS.
442 dir = FLAGS_resourcePath[0];
443 }
444 sk_mkdir(dir);
445
446 SkString path;
447 if (FLAGS_nameByHash) {
448 path = SkOSPath::Join(dir, result.md5.c_str());
449 path.append(".");
450 path.append(ext);
451 if (sk_exists(path.c_str())) {
452 return; // Content-addressed. If it exists already, we're done.
453 }
454 } else {
455 path = SkOSPath::Join(dir, task.sink.tag);
456 sk_mkdir(path.c_str());
457 path = SkOSPath::Join(path.c_str(), task.src.tag);
458 sk_mkdir(path.c_str());
459 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
460 path.append(".");
461 path.append(ext);
462 }
463
464 SkFILEWStream file(path.c_str());
465 if (!file.isValid()) {
466 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
467 return;
468 }
469
mtklein748ca3b2015-01-15 10:56:12 -0800470 if (bitmap) {
471 // We can't encode A8 bitmaps as PNGs. Convert them to 8888 first.
472 SkBitmap converted;
473 if (bitmap->info().colorType() == kAlpha_8_SkColorType) {
474 if (!bitmap->copyTo(&converted, kN32_SkColorType)) {
475 fail("Can't convert A8 to 8888.\n");
476 return;
477 }
478 bitmap = &converted;
479 }
480 if (!SkImageEncoder::EncodeStream(&file, *bitmap, SkImageEncoder::kPNG_Type, 100)) {
481 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
482 return;
483 }
484 } else {
485 if (!file.writeStream(data, len)) {
486 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
487 return;
488 }
489 }
490 }
491};
492
493// Run all tasks in the same enclave serially on the same thread.
494// They can't possibly run concurrently with each other.
495static void run_enclave(SkTArray<Task>* tasks) {
496 for (int i = 0; i < tasks->count(); i++) {
497 Task::Run(tasks->begin() + i);
498 }
499}
500
501/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
502
503// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
504
mtklein55e88b22015-01-21 15:50:13 -0800505static SkTDArray<skiatest::Test> gThreadedTests, gGPUTests;
mtklein748ca3b2015-01-15 10:56:12 -0800506
507static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -0800508 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -0800509 return;
510 }
halcanary87f3ba42015-01-20 09:30:20 -0800511 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r;
512 r = r->next()) {
513 // Despite its name, factory() is returning a reference to
514 // link-time static const POD data.
515 const skiatest::Test& test = r->factory();
516 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -0800517 continue;
518 }
halcanary87f3ba42015-01-20 09:30:20 -0800519 if (test.needsGpu && gpu_supported()) {
mtklein55e88b22015-01-21 15:50:13 -0800520 (FLAGS_gpu_threading ? gThreadedTests : gGPUTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -0800521 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein55e88b22015-01-21 15:50:13 -0800522 gThreadedTests.push(test);
mtklein82d28432015-01-15 12:46:02 -0800523 }
mtklein748ca3b2015-01-15 10:56:12 -0800524 }
525}
526
halcanary87f3ba42015-01-20 09:30:20 -0800527static void run_test(skiatest::Test* test) {
528 struct : public skiatest::Reporter {
mtklein36352bf2015-03-25 18:17:31 -0700529 void reportFailed(const skiatest::Failure& failure) override {
halcanary87f3ba42015-01-20 09:30:20 -0800530 fail(failure.toString());
531 JsonWriter::AddTestFailure(failure);
532 }
mtklein36352bf2015-03-25 18:17:31 -0700533 bool allowExtendedTest() const override {
halcanary87f3ba42015-01-20 09:30:20 -0800534 return FLAGS_pathOpsExtended;
535 }
mtklein36352bf2015-03-25 18:17:31 -0700536 bool verbose() const override { return FLAGS_veryVerbose; }
halcanary87f3ba42015-01-20 09:30:20 -0800537 } reporter;
mtklein748ca3b2015-01-15 10:56:12 -0800538 WallTimer timer;
539 timer.start();
mtklein748ca3b2015-01-15 10:56:12 -0800540 if (!FLAGS_dryRun) {
mtkleinb37cb412015-03-18 05:27:14 -0700541 start("unit", "test", test->name);
mtklein55e88b22015-01-21 15:50:13 -0800542 GrContextFactory factory;
543 test->proc(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -0800544 }
545 timer.end();
mtkleinb37cb412015-03-18 05:27:14 -0700546 done(timer.fWall, "unit", "test", test->name, "", "");
mtklein748ca3b2015-01-15 10:56:12 -0800547}
548
549/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
550
mtklein55e88b22015-01-21 15:50:13 -0800551// If we're isolating all GPU-bound work to one thread (the default), this function runs all that.
552static void run_enclave_and_gpu_tests(SkTArray<Task>* tasks) {
553 run_enclave(tasks);
554 for (int i = 0; i < gGPUTests.count(); i++) {
555 run_test(&gGPUTests[i]);
556 }
557}
558
mtkleinde6fc2b2015-03-12 06:28:54 -0700559// Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung.
560// This prints something every once in a while so that it knows we're still working.
mtklein2e1c47e2015-03-12 07:16:56 -0700561static void start_keepalive() {
562 struct Loop {
563 static void forever(void*) {
564 for (;;) {
565 static const int kSec = 300;
566 #if defined(SK_BUILD_FOR_WIN)
567 Sleep(kSec * 1000);
568 #else
569 sleep(kSec);
570 #endif
mtkleinb37cb412015-03-18 05:27:14 -0700571 SkString running;
572 {
573 SkAutoMutexAcquire lock(gRunningMutex);
574 for (int i = 0; i < gRunning.count(); i++) {
575 running.appendf("\n\t%s", gRunning[i].c_str());
576 }
577 }
578 SkDebugf("\nCurrently running:%s\n", running.c_str());
mtklein2e1c47e2015-03-12 07:16:56 -0700579 }
580 }
581 };
582 static SkThread* intentionallyLeaked = new SkThread(Loop::forever);
583 intentionallyLeaked->start();
mtkleinde6fc2b2015-03-12 06:28:54 -0700584}
585
jcgregorio3b27ade2014-11-13 08:06:40 -0800586int dm_main();
caryclark17f0b6d2014-07-22 10:15:34 -0700587int dm_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -0800588 SetupCrashHandler();
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +0000589 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -0700590 SkTaskGroup::Enabler enabled(FLAGS_threads);
mtkleine67164d2015-02-02 13:24:37 -0800591 if (FLAGS_leaks) {
592 SkInstCountPrintLeaksOnExit();
593 }
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +0000594
mtklein2e1c47e2015-03-12 07:16:56 -0700595 start_keepalive();
mtkleinde6fc2b2015-03-12 06:28:54 -0700596
mtklein62bd1a62015-01-27 14:46:26 -0800597 gather_gold();
598
mtklein748ca3b2015-01-15 10:56:12 -0800599 gather_srcs();
600 gather_sinks();
601 gather_tests();
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +0000602
mtklein55e88b22015-01-21 15:50:13 -0800603 gPending = gSrcs.count() * gSinks.count() + gThreadedTests.count() + gGPUTests.count();
mtklein748ca3b2015-01-15 10:56:12 -0800604 SkDebugf("%d srcs * %d sinks + %d tests == %d tasks\n",
mtklein55e88b22015-01-21 15:50:13 -0800605 gSrcs.count(), gSinks.count(), gThreadedTests.count() + gGPUTests.count(), gPending);
mtklein748ca3b2015-01-15 10:56:12 -0800606
607 // We try to exploit as much parallelism as is safe. Most Src/Sink pairs run on any thread,
608 // but Sinks that identify as part of a particular enclave run serially on a single thread.
mtklein82d28432015-01-15 12:46:02 -0800609 // CPU tests run on any thread. GPU tests depend on --gpu_threading.
mtklein748ca3b2015-01-15 10:56:12 -0800610 SkTArray<Task> enclaves[kNumEnclaves];
611 for (int j = 0; j < gSinks.count(); j++) {
612 SkTArray<Task>& tasks = enclaves[gSinks[j]->enclave()];
613 for (int i = 0; i < gSrcs.count(); i++) {
614 tasks.push_back(Task(gSrcs[i], gSinks[j]));
615 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +0000616 }
617
mtklein748ca3b2015-01-15 10:56:12 -0800618 SkTaskGroup tg;
mtklein55e88b22015-01-21 15:50:13 -0800619 tg.batch(run_test, gThreadedTests.begin(), gThreadedTests.count());
620 for (int i = 0; i < kNumEnclaves; i++) {
621 switch(i) {
622 case kAnyThread_Enclave:
623 tg.batch(Task::Run, enclaves[i].begin(), enclaves[i].count());
624 break;
625 case kGPU_Enclave:
626 tg.add(run_enclave_and_gpu_tests, &enclaves[i]);
627 break;
628 default:
629 tg.add(run_enclave, &enclaves[i]);
630 break;
mtklein82d28432015-01-15 12:46:02 -0800631 }
mtklein55e88b22015-01-21 15:50:13 -0800632 }
mtklein748ca3b2015-01-15 10:56:12 -0800633 tg.wait();
mtklein748ca3b2015-01-15 10:56:12 -0800634 // At this point we're back in single-threaded land.
mtklein@google.comd36522d2013-10-16 13:02:15 +0000635
mtklein2f64eec2015-01-15 14:20:41 -0800636 SkDebugf("\n");
mtklein748ca3b2015-01-15 10:56:12 -0800637 if (gFailures.count() > 0) {
638 SkDebugf("Failures:\n");
639 for (int i = 0; i < gFailures.count(); i++) {
mtklein9dc09102015-01-15 15:47:33 -0800640 SkDebugf("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800641 }
642 SkDebugf("%d failures\n", gFailures.count());
643 return 1;
mtklein114c3cd2015-01-15 10:15:02 -0800644 }
mtklein748ca3b2015-01-15 10:56:12 -0800645 if (gPending > 0) {
646 SkDebugf("Hrm, we didn't seem to run everything we intended to! Please file a bug.\n");
647 return 1;
mtklein114c3cd2015-01-15 10:15:02 -0800648 }
mtklein748ca3b2015-01-15 10:56:12 -0800649 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +0000650}
jcgregorio3b27ade2014-11-13 08:06:40 -0800651
652#if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL)
653int main(int argc, char** argv) {
654 SkCommandLineFlags::Parse(argc, argv);
655 return dm_main();
656}
657#endif