blob: 98f0d947a00bee2079eb7cb8dcb8d2b7b67cfe83 [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
scroggo9b77ddd2015-03-19 06:03:39 -070028DEFINE_string(src, "tests gm skp image subset codec", "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])) {
190 push_src("codec", new CodecSrc(path)); // Decode with SkCodec.
191 }
halcanary23b03c32015-01-30 09:58:58 -0800192 }
mtklein114c3cd2015-01-15 10:15:02 -0800193 }
halcanary23b03c32015-01-30 09:58:58 -0800194 } else if (sk_exists(flag)) {
195 // assume that FLAGS_images[i] is a valid image if it is a file.
mtklein8d17a132015-01-30 11:42:31 -0800196 push_src("image", new ImageSrc(flag)); // Decode entire image.
mtkleinedc93bc2015-01-30 13:22:23 -0800197 push_src("subset", new ImageSrc(flag, 2)); // Decode into 2 x 2 subsets
scroggo9b77ddd2015-03-19 06:03:39 -0700198 push_src("codec", new CodecSrc(flag)); // Decode with SkCodec.
mtklein709d2c32015-01-15 08:30:25 -0800199 }
mtklein709d2c32015-01-15 08:30:25 -0800200 }
201}
202
mtklein748ca3b2015-01-15 10:56:12 -0800203static GrGLStandard get_gpu_api() {
204 if (FLAGS_gpuAPI.contains("gl")) { return kGL_GrGLStandard; }
205 if (FLAGS_gpuAPI.contains("gles")) { return kGLES_GrGLStandard; }
206 return kNone_GrGLStandard;
mtklein709d2c32015-01-15 08:30:25 -0800207}
208
mtklein748ca3b2015-01-15 10:56:12 -0800209static void push_sink(const char* tag, Sink* s) {
210 SkAutoTDelete<Sink> sink(s);
211 if (!FLAGS_config.contains(tag)) {
212 return;
mtklein114c3cd2015-01-15 10:15:02 -0800213 }
mtklein748ca3b2015-01-15 10:56:12 -0800214 // Try a noop Src as a canary. If it fails, skip this sink.
215 struct : public Src {
216 Error draw(SkCanvas*) const SK_OVERRIDE { return ""; }
217 SkISize size() const SK_OVERRIDE { return SkISize::Make(16, 16); }
218 Name name() const SK_OVERRIDE { return "noop"; }
219 } noop;
mtklein114c3cd2015-01-15 10:15:02 -0800220
mtklein748ca3b2015-01-15 10:56:12 -0800221 SkBitmap bitmap;
222 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800223 SkString log;
224 Error err = sink->draw(noop, &bitmap, &stream, &log);
mtklein4089ef72015-03-05 08:40:28 -0800225 if (err.isFatal()) {
mtklein748ca3b2015-01-15 10:56:12 -0800226 SkDebugf("Skipping %s: %s\n", tag, err.c_str());
mtklein114c3cd2015-01-15 10:15:02 -0800227 return;
228 }
229
mtklein748ca3b2015-01-15 10:56:12 -0800230 Tagged<Sink>& ts = gSinks.push_back();
231 ts.reset(sink.detach());
232 ts.tag = tag;
233}
234
235static bool gpu_supported() {
236#if SK_SUPPORT_GPU
237 return FLAGS_gpu;
238#else
239 return false;
240#endif
241}
242
243static Sink* create_sink(const char* tag) {
244#define SINK(t, sink, ...) if (0 == strcmp(t, tag)) { return new sink(__VA_ARGS__); }
245 if (gpu_supported()) {
mtklein82d28432015-01-15 12:46:02 -0800246 typedef GrContextFactory Gr;
mtklein748ca3b2015-01-15 10:56:12 -0800247 const GrGLStandard api = get_gpu_api();
mtklein82d28432015-01-15 12:46:02 -0800248 SINK("gpunull", GPUSink, Gr::kNull_GLContextType, api, 0, false, FLAGS_gpu_threading);
249 SINK("gpudebug", GPUSink, Gr::kDebug_GLContextType, api, 0, false, FLAGS_gpu_threading);
250 SINK("gpu", GPUSink, Gr::kNative_GLContextType, api, 0, false, FLAGS_gpu_threading);
251 SINK("gpudft", GPUSink, Gr::kNative_GLContextType, api, 0, true, FLAGS_gpu_threading);
252 SINK("msaa4", GPUSink, Gr::kNative_GLContextType, api, 4, false, FLAGS_gpu_threading);
253 SINK("msaa16", GPUSink, Gr::kNative_GLContextType, api, 16, false, FLAGS_gpu_threading);
254 SINK("nvprmsaa4", GPUSink, Gr::kNVPR_GLContextType, api, 4, false, FLAGS_gpu_threading);
255 SINK("nvprmsaa16", GPUSink, Gr::kNVPR_GLContextType, api, 16, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800256 #if SK_ANGLE
mtklein82d28432015-01-15 12:46:02 -0800257 SINK("angle", GPUSink, Gr::kANGLE_GLContextType, api, 0, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800258 #endif
259 #if SK_MESA
mtklein82d28432015-01-15 12:46:02 -0800260 SINK("mesa", GPUSink, Gr::kMESA_GLContextType, api, 0, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800261 #endif
mtklein114c3cd2015-01-15 10:15:02 -0800262 }
mtklein748ca3b2015-01-15 10:56:12 -0800263
tomhudsoneebc39a2015-02-23 12:18:05 -0800264#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
265 SINK("hwui", HWUISink);
266#endif
267
mtklein748ca3b2015-01-15 10:56:12 -0800268 if (FLAGS_cpu) {
269 SINK("565", RasterSink, kRGB_565_SkColorType);
270 SINK("8888", RasterSink, kN32_SkColorType);
mtklein19f30602015-01-20 13:34:39 -0800271 SINK("pdf", PDFSink);
mtklein9c3f17d2015-01-28 11:35:18 -0800272 SINK("skp", SKPSink);
mtklein8a4527e2015-01-31 20:00:58 -0800273 SINK("svg", SVGSink);
274 SINK("null", NullSink);
halcanary47ef4d52015-03-03 09:13:09 -0800275 SINK("xps", XPSSink);
mtklein748ca3b2015-01-15 10:56:12 -0800276 }
277#undef SINK
278 return NULL;
mtklein114c3cd2015-01-15 10:15:02 -0800279}
280
mtklein748ca3b2015-01-15 10:56:12 -0800281static Sink* create_via(const char* tag, Sink* wrapped) {
282#define VIA(t, via, ...) if (0 == strcmp(t, tag)) { return new via(__VA_ARGS__); }
mtklein7edca212015-01-21 13:18:51 -0800283 VIA("pipe", ViaPipe, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800284 VIA("serialize", ViaSerialization, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800285 VIA("tiles", ViaTiles, 256, 256, NULL, wrapped);
286 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800287
mtkleind603b222015-02-17 11:13:33 -0800288 if (FLAGS_matrix.count() == 4) {
mtklein748ca3b2015-01-15 10:56:12 -0800289 SkMatrix m;
mtkleind603b222015-02-17 11:13:33 -0800290 m.reset();
291 m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
292 m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
293 m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
294 m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
295 VIA("matrix", ViaMatrix, m, wrapped);
296 VIA("upright", ViaUpright, m, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800297 }
tomhudson64de1e12015-03-05 08:01:07 -0800298
299#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
300 VIA("androidsdk", ViaAndroidSDK, wrapped);
301#endif
302
mtklein748ca3b2015-01-15 10:56:12 -0800303#undef VIA
304 return NULL;
mtklein114c3cd2015-01-15 10:15:02 -0800305}
306
mtklein748ca3b2015-01-15 10:56:12 -0800307static void gather_sinks() {
308 for (int i = 0; i < FLAGS_config.count(); i++) {
309 const char* config = FLAGS_config[i];
310 SkTArray<SkString> parts;
311 SkStrSplit(config, "-", &parts);
312
313 Sink* sink = NULL;
314 for (int i = parts.count(); i-- > 0;) {
315 const char* part = parts[i].c_str();
316 Sink* next = (sink == NULL) ? create_sink(part) : create_via(part, sink);
317 if (next == NULL) {
318 SkDebugf("Skipping %s: Don't understand '%s'.\n", config, part);
319 delete sink;
320 sink = NULL;
321 break;
322 }
323 sink = next;
324 }
325 if (sink) {
326 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800327 }
328 }
329}
mtklein709d2c32015-01-15 08:30:25 -0800330
mtkleina2ef6422015-01-15 13:44:22 -0800331static bool match(const char* needle, const char* haystack) {
332 return 0 == strcmp("_", needle) || NULL != strstr(haystack, needle);
333}
334
335static ImplicitString is_blacklisted(const char* sink, const char* src, const char* name) {
336 for (int i = 0; i < FLAGS_blacklist.count() - 2; i += 3) {
337 if (match(FLAGS_blacklist[i+0], sink) &&
338 match(FLAGS_blacklist[i+1], src) &&
339 match(FLAGS_blacklist[i+2], name)) {
340 return SkStringPrintf("%s %s %s",
341 FLAGS_blacklist[i+0], FLAGS_blacklist[i+1], FLAGS_blacklist[i+2]);
342 }
343 }
344 return "";
345}
346
mtklein748ca3b2015-01-15 10:56:12 -0800347// The finest-grained unit of work we can run: draw a single Src into a single Sink,
348// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
349struct Task {
350 Task(const Tagged<Src>& src, const Tagged<Sink>& sink) : src(src), sink(sink) {}
351 const Tagged<Src>& src;
352 const Tagged<Sink>& sink;
353
354 static void Run(Task* task) {
mtkleina2ef6422015-01-15 13:44:22 -0800355 SkString name = task->src->name();
mtkleinb37cb412015-03-18 05:27:14 -0700356 SkString note;
mtkleina2ef6422015-01-15 13:44:22 -0800357 SkString whyBlacklisted = is_blacklisted(task->sink.tag, task->src.tag, name.c_str());
mtkleinb37cb412015-03-18 05:27:14 -0700358 if (!whyBlacklisted.isEmpty()) {
359 note.appendf(" (--blacklist %s)", whyBlacklisted.c_str());
360 }
mtkleinb9eb4ac2015-02-02 18:26:03 -0800361 SkString log;
mtklein748ca3b2015-01-15 10:56:12 -0800362 WallTimer timer;
363 timer.start();
mtkleina2ef6422015-01-15 13:44:22 -0800364 if (!FLAGS_dryRun && whyBlacklisted.isEmpty()) {
mtklein748ca3b2015-01-15 10:56:12 -0800365 SkBitmap bitmap;
366 SkDynamicMemoryWStream stream;
mtkleinb37cb412015-03-18 05:27:14 -0700367 start(task->sink.tag, task->src.tag, name.c_str());
mtkleinb9eb4ac2015-02-02 18:26:03 -0800368 Error err = task->sink->draw(*task->src, &bitmap, &stream, &log);
mtklein748ca3b2015-01-15 10:56:12 -0800369 if (!err.isEmpty()) {
mtklein4089ef72015-03-05 08:40:28 -0800370 timer.end();
371 if (err.isFatal()) {
372 fail(SkStringPrintf("%s %s %s: %s",
373 task->sink.tag,
374 task->src.tag,
375 name.c_str(),
376 err.c_str()));
mtkleinb37cb412015-03-18 05:27:14 -0700377 } else {
378 note.appendf(" (skipped: %s)", err.c_str());
mtklein4089ef72015-03-05 08:40:28 -0800379 }
mtkleinb37cb412015-03-18 05:27:14 -0700380 done(timer.fWall, task->sink.tag, task->src.tag, name, note, log);
mtklein4089ef72015-03-05 08:40:28 -0800381 return;
mtklein748ca3b2015-01-15 10:56:12 -0800382 }
mtklein62bd1a62015-01-27 14:46:26 -0800383 SkAutoTDelete<SkStreamAsset> data(stream.detachAsStream());
384
385 SkString md5;
386 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
387 SkMD5 hash;
388 if (data->getLength()) {
389 hash.writeStream(data, data->getLength());
390 data->rewind();
391 } else {
392 hash.write(bitmap.getPixels(), bitmap.getSize());
393 }
394 SkMD5::Digest digest;
395 hash.finish(digest);
396 for (int i = 0; i < 16; i++) {
397 md5.appendf("%02x", digest.data[i]);
398 }
399 }
400
401 if (!FLAGS_readPath.isEmpty() &&
mtkleina82f5622015-02-20 12:30:19 -0800402 !gGold.contains(Gold(task->sink.tag, task->src.tag, name, md5))) {
mtklein62bd1a62015-01-27 14:46:26 -0800403 fail(SkStringPrintf("%s not found for %s %s %s in %s",
404 md5.c_str(),
405 task->sink.tag,
406 task->src.tag,
407 name.c_str(),
408 FLAGS_readPath[0]));
409 }
410
mtklein748ca3b2015-01-15 10:56:12 -0800411 if (!FLAGS_writePath.isEmpty()) {
412 const char* ext = task->sink->fileExtension();
mtklein62bd1a62015-01-27 14:46:26 -0800413 if (data->getLength()) {
414 WriteToDisk(*task, md5, ext, data, data->getLength(), NULL);
halcanary022afb82015-01-30 11:00:12 -0800415 SkASSERT(bitmap.drawsNothing());
416 } else if (!bitmap.drawsNothing()) {
mtklein62bd1a62015-01-27 14:46:26 -0800417 WriteToDisk(*task, md5, ext, NULL, 0, &bitmap);
mtklein748ca3b2015-01-15 10:56:12 -0800418 }
419 }
420 }
421 timer.end();
mtkleinb37cb412015-03-18 05:27:14 -0700422 done(timer.fWall, task->sink.tag, task->src.tag, name, note, log);
mtklein748ca3b2015-01-15 10:56:12 -0800423 }
424
425 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -0800426 SkString md5,
427 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -0800428 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -0800429 const SkBitmap* bitmap) {
mtklein748ca3b2015-01-15 10:56:12 -0800430 JsonWriter::BitmapResult result;
431 result.name = task.src->name();
432 result.config = task.sink.tag;
433 result.sourceType = task.src.tag;
434 result.ext = ext;
mtklein62bd1a62015-01-27 14:46:26 -0800435 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -0800436 JsonWriter::AddBitmapResult(result);
437
438 const char* dir = FLAGS_writePath[0];
439 if (0 == strcmp(dir, "@")) { // Needed for iOS.
440 dir = FLAGS_resourcePath[0];
441 }
442 sk_mkdir(dir);
443
444 SkString path;
445 if (FLAGS_nameByHash) {
446 path = SkOSPath::Join(dir, result.md5.c_str());
447 path.append(".");
448 path.append(ext);
449 if (sk_exists(path.c_str())) {
450 return; // Content-addressed. If it exists already, we're done.
451 }
452 } else {
453 path = SkOSPath::Join(dir, task.sink.tag);
454 sk_mkdir(path.c_str());
455 path = SkOSPath::Join(path.c_str(), task.src.tag);
456 sk_mkdir(path.c_str());
457 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
458 path.append(".");
459 path.append(ext);
460 }
461
462 SkFILEWStream file(path.c_str());
463 if (!file.isValid()) {
464 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
465 return;
466 }
467
mtklein748ca3b2015-01-15 10:56:12 -0800468 if (bitmap) {
469 // We can't encode A8 bitmaps as PNGs. Convert them to 8888 first.
470 SkBitmap converted;
471 if (bitmap->info().colorType() == kAlpha_8_SkColorType) {
472 if (!bitmap->copyTo(&converted, kN32_SkColorType)) {
473 fail("Can't convert A8 to 8888.\n");
474 return;
475 }
476 bitmap = &converted;
477 }
478 if (!SkImageEncoder::EncodeStream(&file, *bitmap, SkImageEncoder::kPNG_Type, 100)) {
479 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
480 return;
481 }
482 } else {
483 if (!file.writeStream(data, len)) {
484 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
485 return;
486 }
487 }
488 }
489};
490
491// Run all tasks in the same enclave serially on the same thread.
492// They can't possibly run concurrently with each other.
493static void run_enclave(SkTArray<Task>* tasks) {
494 for (int i = 0; i < tasks->count(); i++) {
495 Task::Run(tasks->begin() + i);
496 }
497}
498
499/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
500
501// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
502
mtklein55e88b22015-01-21 15:50:13 -0800503static SkTDArray<skiatest::Test> gThreadedTests, gGPUTests;
mtklein748ca3b2015-01-15 10:56:12 -0800504
505static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -0800506 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -0800507 return;
508 }
halcanary87f3ba42015-01-20 09:30:20 -0800509 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r;
510 r = r->next()) {
511 // Despite its name, factory() is returning a reference to
512 // link-time static const POD data.
513 const skiatest::Test& test = r->factory();
514 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -0800515 continue;
516 }
halcanary87f3ba42015-01-20 09:30:20 -0800517 if (test.needsGpu && gpu_supported()) {
mtklein55e88b22015-01-21 15:50:13 -0800518 (FLAGS_gpu_threading ? gThreadedTests : gGPUTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -0800519 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein55e88b22015-01-21 15:50:13 -0800520 gThreadedTests.push(test);
mtklein82d28432015-01-15 12:46:02 -0800521 }
mtklein748ca3b2015-01-15 10:56:12 -0800522 }
523}
524
halcanary87f3ba42015-01-20 09:30:20 -0800525static void run_test(skiatest::Test* test) {
526 struct : public skiatest::Reporter {
527 void reportFailed(const skiatest::Failure& failure) SK_OVERRIDE {
528 fail(failure.toString());
529 JsonWriter::AddTestFailure(failure);
530 }
531 bool allowExtendedTest() const SK_OVERRIDE {
532 return FLAGS_pathOpsExtended;
533 }
534 bool verbose() const SK_OVERRIDE { return FLAGS_veryVerbose; }
535 } reporter;
mtklein748ca3b2015-01-15 10:56:12 -0800536 WallTimer timer;
537 timer.start();
mtklein748ca3b2015-01-15 10:56:12 -0800538 if (!FLAGS_dryRun) {
mtkleinb37cb412015-03-18 05:27:14 -0700539 start("unit", "test", test->name);
mtklein55e88b22015-01-21 15:50:13 -0800540 GrContextFactory factory;
541 test->proc(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -0800542 }
543 timer.end();
mtkleinb37cb412015-03-18 05:27:14 -0700544 done(timer.fWall, "unit", "test", test->name, "", "");
mtklein748ca3b2015-01-15 10:56:12 -0800545}
546
547/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
548
mtklein55e88b22015-01-21 15:50:13 -0800549// If we're isolating all GPU-bound work to one thread (the default), this function runs all that.
550static void run_enclave_and_gpu_tests(SkTArray<Task>* tasks) {
551 run_enclave(tasks);
552 for (int i = 0; i < gGPUTests.count(); i++) {
553 run_test(&gGPUTests[i]);
554 }
555}
556
mtkleinde6fc2b2015-03-12 06:28:54 -0700557// Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung.
558// This prints something every once in a while so that it knows we're still working.
mtklein2e1c47e2015-03-12 07:16:56 -0700559static void start_keepalive() {
560 struct Loop {
561 static void forever(void*) {
562 for (;;) {
563 static const int kSec = 300;
564 #if defined(SK_BUILD_FOR_WIN)
565 Sleep(kSec * 1000);
566 #else
567 sleep(kSec);
568 #endif
mtkleinb37cb412015-03-18 05:27:14 -0700569 SkString running;
570 {
571 SkAutoMutexAcquire lock(gRunningMutex);
572 for (int i = 0; i < gRunning.count(); i++) {
573 running.appendf("\n\t%s", gRunning[i].c_str());
574 }
575 }
576 SkDebugf("\nCurrently running:%s\n", running.c_str());
mtklein2e1c47e2015-03-12 07:16:56 -0700577 }
578 }
579 };
580 static SkThread* intentionallyLeaked = new SkThread(Loop::forever);
581 intentionallyLeaked->start();
mtkleinde6fc2b2015-03-12 06:28:54 -0700582}
583
jcgregorio3b27ade2014-11-13 08:06:40 -0800584int dm_main();
caryclark17f0b6d2014-07-22 10:15:34 -0700585int dm_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -0800586 SetupCrashHandler();
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +0000587 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -0700588 SkTaskGroup::Enabler enabled(FLAGS_threads);
mtkleine67164d2015-02-02 13:24:37 -0800589 if (FLAGS_leaks) {
590 SkInstCountPrintLeaksOnExit();
591 }
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +0000592
mtklein2e1c47e2015-03-12 07:16:56 -0700593 start_keepalive();
mtkleinde6fc2b2015-03-12 06:28:54 -0700594
mtklein62bd1a62015-01-27 14:46:26 -0800595 gather_gold();
596
mtklein748ca3b2015-01-15 10:56:12 -0800597 gather_srcs();
598 gather_sinks();
599 gather_tests();
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +0000600
mtklein55e88b22015-01-21 15:50:13 -0800601 gPending = gSrcs.count() * gSinks.count() + gThreadedTests.count() + gGPUTests.count();
mtklein748ca3b2015-01-15 10:56:12 -0800602 SkDebugf("%d srcs * %d sinks + %d tests == %d tasks\n",
mtklein55e88b22015-01-21 15:50:13 -0800603 gSrcs.count(), gSinks.count(), gThreadedTests.count() + gGPUTests.count(), gPending);
mtklein748ca3b2015-01-15 10:56:12 -0800604
605 // We try to exploit as much parallelism as is safe. Most Src/Sink pairs run on any thread,
606 // but Sinks that identify as part of a particular enclave run serially on a single thread.
mtklein82d28432015-01-15 12:46:02 -0800607 // CPU tests run on any thread. GPU tests depend on --gpu_threading.
mtklein748ca3b2015-01-15 10:56:12 -0800608 SkTArray<Task> enclaves[kNumEnclaves];
609 for (int j = 0; j < gSinks.count(); j++) {
610 SkTArray<Task>& tasks = enclaves[gSinks[j]->enclave()];
611 for (int i = 0; i < gSrcs.count(); i++) {
612 tasks.push_back(Task(gSrcs[i], gSinks[j]));
613 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +0000614 }
615
mtklein748ca3b2015-01-15 10:56:12 -0800616 SkTaskGroup tg;
mtklein55e88b22015-01-21 15:50:13 -0800617 tg.batch(run_test, gThreadedTests.begin(), gThreadedTests.count());
618 for (int i = 0; i < kNumEnclaves; i++) {
619 switch(i) {
620 case kAnyThread_Enclave:
621 tg.batch(Task::Run, enclaves[i].begin(), enclaves[i].count());
622 break;
623 case kGPU_Enclave:
624 tg.add(run_enclave_and_gpu_tests, &enclaves[i]);
625 break;
626 default:
627 tg.add(run_enclave, &enclaves[i]);
628 break;
mtklein82d28432015-01-15 12:46:02 -0800629 }
mtklein55e88b22015-01-21 15:50:13 -0800630 }
mtklein748ca3b2015-01-15 10:56:12 -0800631 tg.wait();
mtklein748ca3b2015-01-15 10:56:12 -0800632 // At this point we're back in single-threaded land.
mtklein@google.comd36522d2013-10-16 13:02:15 +0000633
mtklein2f64eec2015-01-15 14:20:41 -0800634 SkDebugf("\n");
mtklein748ca3b2015-01-15 10:56:12 -0800635 if (gFailures.count() > 0) {
636 SkDebugf("Failures:\n");
637 for (int i = 0; i < gFailures.count(); i++) {
mtklein9dc09102015-01-15 15:47:33 -0800638 SkDebugf("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800639 }
640 SkDebugf("%d failures\n", gFailures.count());
641 return 1;
mtklein114c3cd2015-01-15 10:15:02 -0800642 }
mtklein748ca3b2015-01-15 10:56:12 -0800643 if (gPending > 0) {
644 SkDebugf("Hrm, we didn't seem to run everything we intended to! Please file a bug.\n");
645 return 1;
mtklein114c3cd2015-01-15 10:15:02 -0800646 }
mtklein748ca3b2015-01-15 10:56:12 -0800647 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +0000648}
jcgregorio3b27ade2014-11-13 08:06:40 -0800649
650#if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL)
651int main(int argc, char** argv) {
652 SkCommandLineFlags::Parse(argc, argv);
653 return dm_main();
654}
655#endif