blob: 256014e5daabc979a0339b36086a230744700216 [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
djsollen54416de2015-04-03 07:24:48 -070028DEFINE_string(src, "tests gm skp image", "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 "
djsollen54416de2015-04-03 07:24:48 -070031 "to FLAGS_writePath[0]/<config>/<sourceType>/<sourceOptions>/<name>.png");
mtklein748ca3b2015-01-15 10:56:12 -080032DEFINE_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, "",
djsollen54416de2015-04-03 07:24:48 -070039 "Space-separated config/src/srcOptions/name quadruples 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.");
mtkleina2ef6422015-01-15 13:44:22 -080042
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,
djsollen54416de2015-04-03 07:24:48 -070065 ImplicitString config, ImplicitString src, ImplicitString srcOptions,
66 ImplicitString name, ImplicitString note, ImplicitString log) {
67 SkString id = SkStringPrintf("%s %s %s %s", config.c_str(), src.c_str(),
68 srcOptions.c_str(), name.c_str());
mtkleinb37cb412015-03-18 05:27:14 -070069 {
70 SkAutoMutexAcquire lock(gRunningMutex);
71 for (int i = 0; i < gRunning.count(); i++) {
72 if (gRunning[i] == id) {
73 gRunning.removeShuffle(i);
74 break;
75 }
76 }
77 }
78 if (!FLAGS_verbose) {
79 note = "";
80 }
mtkleinb9eb4ac2015-02-02 18:26:03 -080081 if (!log.isEmpty()) {
82 log.prepend("\n");
83 }
mtklein6dee2ad2015-02-05 09:53:44 -080084 auto pending = sk_atomic_dec(&gPending)-1;
mtkleinb37cb412015-03-18 05:27:14 -070085 SkDebugf("%s(%4dMB %5d) %s\t%s%s%s", FLAGS_verbose ? "\n" : kSkOverwriteLine
86 , sk_tools::getBestResidentSetSizeMB()
87 , pending
88 , HumanizeMs(ms).c_str()
89 , id.c_str()
90 , note.c_str()
91 , log.c_str());
mtkleina17241b2015-01-23 05:48:00 -080092 // We write our dm.json file every once in a while in case we crash.
93 // Notice this also handles the final dm.json when pending == 0.
94 if (pending % 500 == 0) {
95 JsonWriter::DumpJson();
96 }
mtklein@google.comd36522d2013-10-16 13:02:15 +000097}
98
djsollen54416de2015-04-03 07:24:48 -070099static void start(ImplicitString config, ImplicitString src,
100 ImplicitString srcOptions, ImplicitString name) {
101 SkString id = SkStringPrintf("%s %s %s %s", config.c_str(), src.c_str(),
102 srcOptions.c_str(), name.c_str());
mtkleinb37cb412015-03-18 05:27:14 -0700103 SkAutoMutexAcquire lock(gRunningMutex);
104 gRunning.push_back(id);
105}
106
mtklein748ca3b2015-01-15 10:56:12 -0800107/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
mtklein114c3cd2015-01-15 10:15:02 -0800108
mtklein62bd1a62015-01-27 14:46:26 -0800109struct Gold : public SkString {
mtkleina82f5622015-02-20 12:30:19 -0800110 Gold() : SkString("") {}
djsollen54416de2015-04-03 07:24:48 -0700111 Gold(ImplicitString sink, ImplicitString src, ImplicitString srcOptions,
112 ImplicitString name, ImplicitString md5)
mtklein62bd1a62015-01-27 14:46:26 -0800113 : SkString("") {
114 this->append(sink);
115 this->append(src);
djsollen54416de2015-04-03 07:24:48 -0700116 this->append(srcOptions);
mtklein62bd1a62015-01-27 14:46:26 -0800117 this->append(name);
118 this->append(md5);
mtklein62bd1a62015-01-27 14:46:26 -0800119 }
mtklein02f46cf2015-03-20 13:48:42 -0700120 static uint32_t Hash(const Gold& g) { return SkGoodHash((const SkString&)g); }
mtklein62bd1a62015-01-27 14:46:26 -0800121};
mtkleina82f5622015-02-20 12:30:19 -0800122static SkTHashSet<Gold, Gold::Hash> gGold;
mtklein62bd1a62015-01-27 14:46:26 -0800123
124static void add_gold(JsonWriter::BitmapResult r) {
djsollen54416de2015-04-03 07:24:48 -0700125 gGold.add(Gold(r.config, r.sourceType, r.sourceOptions, r.name, r.md5));
mtklein62bd1a62015-01-27 14:46:26 -0800126}
127
128static void gather_gold() {
129 if (!FLAGS_readPath.isEmpty()) {
130 SkString path(FLAGS_readPath[0]);
131 path.append("/dm.json");
132 if (!JsonWriter::ReadJson(path.c_str(), add_gold)) {
133 fail(SkStringPrintf("Couldn't read %s for golden results.", path.c_str()));
134 }
135 }
136}
137
138/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
139
mtklein748ca3b2015-01-15 10:56:12 -0800140template <typename T>
djsollen54416de2015-04-03 07:24:48 -0700141struct Tagged : public SkAutoTDelete<T> {
142 const char* tag;
143 const char* options;
144};
mtklein748ca3b2015-01-15 10:56:12 -0800145
146static const bool kMemcpyOK = true;
147
148static SkTArray<Tagged<Src>, kMemcpyOK> gSrcs;
149static SkTArray<Tagged<Sink>, kMemcpyOK> gSinks;
150
djsollen54416de2015-04-03 07:24:48 -0700151static void push_src(const char* tag, const char* options, Src* s) {
mtklein748ca3b2015-01-15 10:56:12 -0800152 SkAutoTDelete<Src> src(s);
153 if (FLAGS_src.contains(tag) &&
154 !SkCommandLineFlags::ShouldSkip(FLAGS_match, src->name().c_str())) {
155 Tagged<Src>& s = gSrcs.push_back();
156 s.reset(src.detach());
157 s.tag = tag;
djsollen54416de2015-04-03 07:24:48 -0700158 s.options = options;
mtklein114c3cd2015-01-15 10:15:02 -0800159 }
mtklein748ca3b2015-01-15 10:56:12 -0800160}
mtklein114c3cd2015-01-15 10:15:02 -0800161
scroggo9b77ddd2015-03-19 06:03:39 -0700162static bool codec_supported(const char* ext) {
163 // FIXME: Once other versions of SkCodec are available, we can add them to this
164 // list (and eventually we can remove this check once they are all supported).
msarett8c8f22a2015-04-01 06:58:48 -0700165 static const char* const exts[] = {
166 "bmp", "gif", "png", "ico", "wbmp",
167 "BMP", "GIF", "PNG", "ICO", "WBMP"
168 };
169
170 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
171 if (0 == strcmp(exts[i], ext)) {
172 return true;
173 }
174 }
175 return false;
scroggo9b77ddd2015-03-19 06:03:39 -0700176}
177
mtklein748ca3b2015-01-15 10:56:12 -0800178static void gather_srcs() {
179 for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) {
djsollen54416de2015-04-03 07:24:48 -0700180 push_src("gm", "", new GMSrc(r->factory()));
mtklein748ca3b2015-01-15 10:56:12 -0800181 }
halcanaryfc37ad12015-01-30 07:31:19 -0800182 for (int i = 0; i < FLAGS_skps.count(); i++) {
183 const char* path = FLAGS_skps[i];
184 if (sk_isdir(path)) {
185 SkOSFile::Iter it(path, "skp");
186 for (SkString file; it.next(&file); ) {
djsollen54416de2015-04-03 07:24:48 -0700187 push_src("skp", "", new SKPSrc(SkOSPath::Join(path, file.c_str())));
halcanaryfc37ad12015-01-30 07:31:19 -0800188 }
189 } else {
djsollen54416de2015-04-03 07:24:48 -0700190 push_src("skp", "", new SKPSrc(path));
mtklein114c3cd2015-01-15 10:15:02 -0800191 }
192 }
halcanary23b03c32015-01-30 09:58:58 -0800193 static const char* const exts[] = {
194 "bmp", "gif", "jpg", "jpeg", "png", "webp", "ktx", "astc", "wbmp", "ico",
195 "BMP", "GIF", "JPG", "JPEG", "PNG", "WEBP", "KTX", "ASTC", "WBMP", "ICO",
196 };
197 for (int i = 0; i < FLAGS_images.count(); i++) {
198 const char* flag = FLAGS_images[i];
199 if (sk_isdir(flag)) {
200 for (size_t j = 0; j < SK_ARRAY_COUNT(exts); j++) {
201 SkOSFile::Iter it(flag, exts[j]);
202 for (SkString file; it.next(&file); ) {
203 SkString path = SkOSPath::Join(flag, file.c_str());
djsollen54416de2015-04-03 07:24:48 -0700204 push_src("image", "decode", new ImageSrc(path)); // Decode entire image
205 push_src("image", "subset", new ImageSrc(path, 2)); // Decode into 2x2 subsets
scroggo9b77ddd2015-03-19 06:03:39 -0700206 if (codec_supported(exts[j])) {
djsollen54416de2015-04-03 07:24:48 -0700207 push_src("image", "codec", new CodecSrc(path, CodecSrc::kNormal_Mode));
208 push_src("image", "scanline", new CodecSrc(path, CodecSrc::kScanline_Mode));
scroggo9b77ddd2015-03-19 06:03:39 -0700209 }
halcanary23b03c32015-01-30 09:58:58 -0800210 }
mtklein114c3cd2015-01-15 10:15:02 -0800211 }
halcanary23b03c32015-01-30 09:58:58 -0800212 } else if (sk_exists(flag)) {
213 // assume that FLAGS_images[i] is a valid image if it is a file.
djsollen54416de2015-04-03 07:24:48 -0700214 push_src("image", "decode", new ImageSrc(flag)); // Decode entire image.
215 push_src("image", "subset", new ImageSrc(flag, 2)); // Decode into 2 x 2 subsets
216 push_src("image", "codec", new CodecSrc(flag, CodecSrc::kNormal_Mode));
217 push_src("image", "scanline", new CodecSrc(flag, CodecSrc::kScanline_Mode));
mtklein709d2c32015-01-15 08:30:25 -0800218 }
mtklein709d2c32015-01-15 08:30:25 -0800219 }
220}
221
mtklein748ca3b2015-01-15 10:56:12 -0800222static GrGLStandard get_gpu_api() {
223 if (FLAGS_gpuAPI.contains("gl")) { return kGL_GrGLStandard; }
224 if (FLAGS_gpuAPI.contains("gles")) { return kGLES_GrGLStandard; }
225 return kNone_GrGLStandard;
mtklein709d2c32015-01-15 08:30:25 -0800226}
227
mtklein748ca3b2015-01-15 10:56:12 -0800228static void push_sink(const char* tag, Sink* s) {
229 SkAutoTDelete<Sink> sink(s);
230 if (!FLAGS_config.contains(tag)) {
231 return;
mtklein114c3cd2015-01-15 10:15:02 -0800232 }
mtklein748ca3b2015-01-15 10:56:12 -0800233 // Try a noop Src as a canary. If it fails, skip this sink.
234 struct : public Src {
mtklein36352bf2015-03-25 18:17:31 -0700235 Error draw(SkCanvas*) const override { return ""; }
236 SkISize size() const override { return SkISize::Make(16, 16); }
237 Name name() const override { return "noop"; }
mtklein748ca3b2015-01-15 10:56:12 -0800238 } noop;
mtklein114c3cd2015-01-15 10:15:02 -0800239
mtklein748ca3b2015-01-15 10:56:12 -0800240 SkBitmap bitmap;
241 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800242 SkString log;
243 Error err = sink->draw(noop, &bitmap, &stream, &log);
mtklein4089ef72015-03-05 08:40:28 -0800244 if (err.isFatal()) {
mtklein748ca3b2015-01-15 10:56:12 -0800245 SkDebugf("Skipping %s: %s\n", tag, err.c_str());
mtklein114c3cd2015-01-15 10:15:02 -0800246 return;
247 }
248
mtklein748ca3b2015-01-15 10:56:12 -0800249 Tagged<Sink>& ts = gSinks.push_back();
250 ts.reset(sink.detach());
251 ts.tag = tag;
252}
253
254static bool gpu_supported() {
255#if SK_SUPPORT_GPU
256 return FLAGS_gpu;
257#else
258 return false;
259#endif
260}
261
262static Sink* create_sink(const char* tag) {
263#define SINK(t, sink, ...) if (0 == strcmp(t, tag)) { return new sink(__VA_ARGS__); }
264 if (gpu_supported()) {
mtklein82d28432015-01-15 12:46:02 -0800265 typedef GrContextFactory Gr;
mtklein748ca3b2015-01-15 10:56:12 -0800266 const GrGLStandard api = get_gpu_api();
mtklein82d28432015-01-15 12:46:02 -0800267 SINK("gpunull", GPUSink, Gr::kNull_GLContextType, api, 0, false, FLAGS_gpu_threading);
268 SINK("gpudebug", GPUSink, Gr::kDebug_GLContextType, api, 0, false, FLAGS_gpu_threading);
269 SINK("gpu", GPUSink, Gr::kNative_GLContextType, api, 0, false, FLAGS_gpu_threading);
270 SINK("gpudft", GPUSink, Gr::kNative_GLContextType, api, 0, true, FLAGS_gpu_threading);
271 SINK("msaa4", GPUSink, Gr::kNative_GLContextType, api, 4, false, FLAGS_gpu_threading);
272 SINK("msaa16", GPUSink, Gr::kNative_GLContextType, api, 16, false, FLAGS_gpu_threading);
273 SINK("nvprmsaa4", GPUSink, Gr::kNVPR_GLContextType, api, 4, false, FLAGS_gpu_threading);
274 SINK("nvprmsaa16", GPUSink, Gr::kNVPR_GLContextType, api, 16, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800275 #if SK_ANGLE
mtklein82d28432015-01-15 12:46:02 -0800276 SINK("angle", GPUSink, Gr::kANGLE_GLContextType, api, 0, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800277 #endif
278 #if SK_MESA
mtklein82d28432015-01-15 12:46:02 -0800279 SINK("mesa", GPUSink, Gr::kMESA_GLContextType, api, 0, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800280 #endif
mtklein114c3cd2015-01-15 10:15:02 -0800281 }
mtklein748ca3b2015-01-15 10:56:12 -0800282
tomhudsoneebc39a2015-02-23 12:18:05 -0800283#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
284 SINK("hwui", HWUISink);
285#endif
286
mtklein748ca3b2015-01-15 10:56:12 -0800287 if (FLAGS_cpu) {
288 SINK("565", RasterSink, kRGB_565_SkColorType);
289 SINK("8888", RasterSink, kN32_SkColorType);
mtklein19f30602015-01-20 13:34:39 -0800290 SINK("pdf", PDFSink);
mtklein9c3f17d2015-01-28 11:35:18 -0800291 SINK("skp", SKPSink);
mtklein8a4527e2015-01-31 20:00:58 -0800292 SINK("svg", SVGSink);
293 SINK("null", NullSink);
halcanary47ef4d52015-03-03 09:13:09 -0800294 SINK("xps", XPSSink);
mtklein748ca3b2015-01-15 10:56:12 -0800295 }
296#undef SINK
297 return NULL;
mtklein114c3cd2015-01-15 10:15:02 -0800298}
299
mtklein748ca3b2015-01-15 10:56:12 -0800300static Sink* create_via(const char* tag, Sink* wrapped) {
301#define VIA(t, via, ...) if (0 == strcmp(t, tag)) { return new via(__VA_ARGS__); }
mtklein7edca212015-01-21 13:18:51 -0800302 VIA("pipe", ViaPipe, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800303 VIA("serialize", ViaSerialization, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800304 VIA("tiles", ViaTiles, 256, 256, NULL, wrapped);
305 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800306
mtkleind603b222015-02-17 11:13:33 -0800307 if (FLAGS_matrix.count() == 4) {
mtklein748ca3b2015-01-15 10:56:12 -0800308 SkMatrix m;
mtkleind603b222015-02-17 11:13:33 -0800309 m.reset();
310 m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
311 m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
312 m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
313 m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
314 VIA("matrix", ViaMatrix, m, wrapped);
315 VIA("upright", ViaUpright, m, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800316 }
tomhudson64de1e12015-03-05 08:01:07 -0800317
318#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
319 VIA("androidsdk", ViaAndroidSDK, wrapped);
320#endif
321
mtklein748ca3b2015-01-15 10:56:12 -0800322#undef VIA
323 return NULL;
mtklein114c3cd2015-01-15 10:15:02 -0800324}
325
mtklein748ca3b2015-01-15 10:56:12 -0800326static void gather_sinks() {
327 for (int i = 0; i < FLAGS_config.count(); i++) {
328 const char* config = FLAGS_config[i];
329 SkTArray<SkString> parts;
330 SkStrSplit(config, "-", &parts);
331
332 Sink* sink = NULL;
333 for (int i = parts.count(); i-- > 0;) {
334 const char* part = parts[i].c_str();
335 Sink* next = (sink == NULL) ? create_sink(part) : create_via(part, sink);
336 if (next == NULL) {
337 SkDebugf("Skipping %s: Don't understand '%s'.\n", config, part);
338 delete sink;
339 sink = NULL;
340 break;
341 }
342 sink = next;
343 }
344 if (sink) {
345 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800346 }
347 }
348}
mtklein709d2c32015-01-15 08:30:25 -0800349
mtkleina2ef6422015-01-15 13:44:22 -0800350static bool match(const char* needle, const char* haystack) {
351 return 0 == strcmp("_", needle) || NULL != strstr(haystack, needle);
352}
353
djsollen54416de2015-04-03 07:24:48 -0700354static ImplicitString is_blacklisted(const char* sink, const char* src,
355 const char* srcOptions, const char* name) {
mtklein0d243ff2015-04-03 07:51:00 -0700356 for (int i = 0; i < FLAGS_blacklist.count() - 3; i += 4) {
mtkleina2ef6422015-01-15 13:44:22 -0800357 if (match(FLAGS_blacklist[i+0], sink) &&
djsollen54416de2015-04-03 07:24:48 -0700358 match(FLAGS_blacklist[i+1], src) &&
359 match(FLAGS_blacklist[i+2], srcOptions) &&
360 match(FLAGS_blacklist[i+3], name)) {
361 return SkStringPrintf("%s %s %s %s",
362 FLAGS_blacklist[i+0], FLAGS_blacklist[i+1],
363 FLAGS_blacklist[i+2], FLAGS_blacklist[i+3]);
mtkleina2ef6422015-01-15 13:44:22 -0800364 }
365 }
366 return "";
367}
368
mtklein748ca3b2015-01-15 10:56:12 -0800369// The finest-grained unit of work we can run: draw a single Src into a single Sink,
370// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
371struct Task {
372 Task(const Tagged<Src>& src, const Tagged<Sink>& sink) : src(src), sink(sink) {}
373 const Tagged<Src>& src;
374 const Tagged<Sink>& sink;
375
376 static void Run(Task* task) {
mtkleina2ef6422015-01-15 13:44:22 -0800377 SkString name = task->src->name();
mtkleinb37cb412015-03-18 05:27:14 -0700378 SkString note;
djsollen54416de2015-04-03 07:24:48 -0700379 SkString whyBlacklisted = is_blacklisted(task->sink.tag, task->src.tag,
380 task->src.options, name.c_str());
mtkleinb37cb412015-03-18 05:27:14 -0700381 if (!whyBlacklisted.isEmpty()) {
382 note.appendf(" (--blacklist %s)", whyBlacklisted.c_str());
383 }
mtkleinb9eb4ac2015-02-02 18:26:03 -0800384 SkString log;
mtklein748ca3b2015-01-15 10:56:12 -0800385 WallTimer timer;
386 timer.start();
mtkleina2ef6422015-01-15 13:44:22 -0800387 if (!FLAGS_dryRun && whyBlacklisted.isEmpty()) {
mtklein748ca3b2015-01-15 10:56:12 -0800388 SkBitmap bitmap;
389 SkDynamicMemoryWStream stream;
djsollen54416de2015-04-03 07:24:48 -0700390 start(task->sink.tag, task->src.tag, task->src.options, name.c_str());
mtkleinb9eb4ac2015-02-02 18:26:03 -0800391 Error err = task->sink->draw(*task->src, &bitmap, &stream, &log);
mtklein748ca3b2015-01-15 10:56:12 -0800392 if (!err.isEmpty()) {
mtklein4089ef72015-03-05 08:40:28 -0800393 timer.end();
394 if (err.isFatal()) {
djsollen54416de2015-04-03 07:24:48 -0700395 fail(SkStringPrintf("%s %s %s %s: %s",
mtklein4089ef72015-03-05 08:40:28 -0800396 task->sink.tag,
397 task->src.tag,
djsollen54416de2015-04-03 07:24:48 -0700398 task->src.options,
mtklein4089ef72015-03-05 08:40:28 -0800399 name.c_str(),
400 err.c_str()));
mtkleinb37cb412015-03-18 05:27:14 -0700401 } else {
402 note.appendf(" (skipped: %s)", err.c_str());
mtklein4089ef72015-03-05 08:40:28 -0800403 }
djsollen54416de2015-04-03 07:24:48 -0700404 done(timer.fWall, task->sink.tag, task->src.tag, task->src.options,
405 name, note, log);
mtklein4089ef72015-03-05 08:40:28 -0800406 return;
mtklein748ca3b2015-01-15 10:56:12 -0800407 }
mtklein62bd1a62015-01-27 14:46:26 -0800408 SkAutoTDelete<SkStreamAsset> data(stream.detachAsStream());
409
410 SkString md5;
411 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
412 SkMD5 hash;
413 if (data->getLength()) {
414 hash.writeStream(data, data->getLength());
415 data->rewind();
416 } else {
417 hash.write(bitmap.getPixels(), bitmap.getSize());
418 }
419 SkMD5::Digest digest;
420 hash.finish(digest);
421 for (int i = 0; i < 16; i++) {
422 md5.appendf("%02x", digest.data[i]);
423 }
424 }
425
426 if (!FLAGS_readPath.isEmpty() &&
djsollen54416de2015-04-03 07:24:48 -0700427 !gGold.contains(Gold(task->sink.tag, task->src.tag,
428 task->src.options, name, md5))) {
429 fail(SkStringPrintf("%s not found for %s %s %s %s in %s",
mtklein62bd1a62015-01-27 14:46:26 -0800430 md5.c_str(),
431 task->sink.tag,
432 task->src.tag,
djsollen54416de2015-04-03 07:24:48 -0700433 task->src.options,
mtklein62bd1a62015-01-27 14:46:26 -0800434 name.c_str(),
435 FLAGS_readPath[0]));
436 }
437
mtklein748ca3b2015-01-15 10:56:12 -0800438 if (!FLAGS_writePath.isEmpty()) {
439 const char* ext = task->sink->fileExtension();
mtklein62bd1a62015-01-27 14:46:26 -0800440 if (data->getLength()) {
441 WriteToDisk(*task, md5, ext, data, data->getLength(), NULL);
halcanary022afb82015-01-30 11:00:12 -0800442 SkASSERT(bitmap.drawsNothing());
443 } else if (!bitmap.drawsNothing()) {
mtklein62bd1a62015-01-27 14:46:26 -0800444 WriteToDisk(*task, md5, ext, NULL, 0, &bitmap);
mtklein748ca3b2015-01-15 10:56:12 -0800445 }
446 }
447 }
448 timer.end();
djsollen54416de2015-04-03 07:24:48 -0700449 done(timer.fWall, task->sink.tag, task->src.tag, task->src.options, name, note, log);
mtklein748ca3b2015-01-15 10:56:12 -0800450 }
451
452 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -0800453 SkString md5,
454 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -0800455 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -0800456 const SkBitmap* bitmap) {
mtklein748ca3b2015-01-15 10:56:12 -0800457 JsonWriter::BitmapResult result;
djsollen54416de2015-04-03 07:24:48 -0700458 result.name = task.src->name();
459 result.config = task.sink.tag;
460 result.sourceType = task.src.tag;
461 result.sourceOptions = task.src.options;
462 result.ext = ext;
463 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -0800464 JsonWriter::AddBitmapResult(result);
465
466 const char* dir = FLAGS_writePath[0];
467 if (0 == strcmp(dir, "@")) { // Needed for iOS.
468 dir = FLAGS_resourcePath[0];
469 }
470 sk_mkdir(dir);
471
472 SkString path;
473 if (FLAGS_nameByHash) {
474 path = SkOSPath::Join(dir, result.md5.c_str());
475 path.append(".");
476 path.append(ext);
477 if (sk_exists(path.c_str())) {
478 return; // Content-addressed. If it exists already, we're done.
479 }
480 } else {
481 path = SkOSPath::Join(dir, task.sink.tag);
482 sk_mkdir(path.c_str());
483 path = SkOSPath::Join(path.c_str(), task.src.tag);
484 sk_mkdir(path.c_str());
djsollen54416de2015-04-03 07:24:48 -0700485 if (strcmp(task.src.options, "") != 0) {
486 path = SkOSPath::Join(path.c_str(), task.src.options);
487 sk_mkdir(path.c_str());
488 }
mtklein748ca3b2015-01-15 10:56:12 -0800489 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
490 path.append(".");
491 path.append(ext);
492 }
493
494 SkFILEWStream file(path.c_str());
495 if (!file.isValid()) {
496 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
497 return;
498 }
499
mtklein748ca3b2015-01-15 10:56:12 -0800500 if (bitmap) {
501 // We can't encode A8 bitmaps as PNGs. Convert them to 8888 first.
502 SkBitmap converted;
503 if (bitmap->info().colorType() == kAlpha_8_SkColorType) {
504 if (!bitmap->copyTo(&converted, kN32_SkColorType)) {
505 fail("Can't convert A8 to 8888.\n");
506 return;
507 }
508 bitmap = &converted;
509 }
510 if (!SkImageEncoder::EncodeStream(&file, *bitmap, SkImageEncoder::kPNG_Type, 100)) {
511 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
512 return;
513 }
514 } else {
515 if (!file.writeStream(data, len)) {
516 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
517 return;
518 }
519 }
520 }
521};
522
523// Run all tasks in the same enclave serially on the same thread.
524// They can't possibly run concurrently with each other.
525static void run_enclave(SkTArray<Task>* tasks) {
526 for (int i = 0; i < tasks->count(); i++) {
527 Task::Run(tasks->begin() + i);
528 }
529}
530
531/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
532
533// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
534
mtklein55e88b22015-01-21 15:50:13 -0800535static SkTDArray<skiatest::Test> gThreadedTests, gGPUTests;
mtklein748ca3b2015-01-15 10:56:12 -0800536
537static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -0800538 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -0800539 return;
540 }
halcanary87f3ba42015-01-20 09:30:20 -0800541 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r;
542 r = r->next()) {
543 // Despite its name, factory() is returning a reference to
544 // link-time static const POD data.
545 const skiatest::Test& test = r->factory();
546 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -0800547 continue;
548 }
halcanary87f3ba42015-01-20 09:30:20 -0800549 if (test.needsGpu && gpu_supported()) {
mtklein55e88b22015-01-21 15:50:13 -0800550 (FLAGS_gpu_threading ? gThreadedTests : gGPUTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -0800551 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein55e88b22015-01-21 15:50:13 -0800552 gThreadedTests.push(test);
mtklein82d28432015-01-15 12:46:02 -0800553 }
mtklein748ca3b2015-01-15 10:56:12 -0800554 }
555}
556
halcanary87f3ba42015-01-20 09:30:20 -0800557static void run_test(skiatest::Test* test) {
558 struct : public skiatest::Reporter {
mtklein36352bf2015-03-25 18:17:31 -0700559 void reportFailed(const skiatest::Failure& failure) override {
halcanary87f3ba42015-01-20 09:30:20 -0800560 fail(failure.toString());
561 JsonWriter::AddTestFailure(failure);
562 }
mtklein36352bf2015-03-25 18:17:31 -0700563 bool allowExtendedTest() const override {
halcanary87f3ba42015-01-20 09:30:20 -0800564 return FLAGS_pathOpsExtended;
565 }
mtklein36352bf2015-03-25 18:17:31 -0700566 bool verbose() const override { return FLAGS_veryVerbose; }
halcanary87f3ba42015-01-20 09:30:20 -0800567 } reporter;
mtklein748ca3b2015-01-15 10:56:12 -0800568 WallTimer timer;
569 timer.start();
mtklein748ca3b2015-01-15 10:56:12 -0800570 if (!FLAGS_dryRun) {
djsollen54416de2015-04-03 07:24:48 -0700571 start("unit", "test", "", test->name);
mtklein55e88b22015-01-21 15:50:13 -0800572 GrContextFactory factory;
573 test->proc(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -0800574 }
575 timer.end();
djsollen54416de2015-04-03 07:24:48 -0700576 done(timer.fWall, "unit", "test", "", test->name, "", "");
mtklein748ca3b2015-01-15 10:56:12 -0800577}
578
579/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
580
mtklein55e88b22015-01-21 15:50:13 -0800581// If we're isolating all GPU-bound work to one thread (the default), this function runs all that.
582static void run_enclave_and_gpu_tests(SkTArray<Task>* tasks) {
583 run_enclave(tasks);
584 for (int i = 0; i < gGPUTests.count(); i++) {
585 run_test(&gGPUTests[i]);
586 }
587}
588
mtkleinde6fc2b2015-03-12 06:28:54 -0700589// Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung.
590// This prints something every once in a while so that it knows we're still working.
mtklein2e1c47e2015-03-12 07:16:56 -0700591static void start_keepalive() {
592 struct Loop {
593 static void forever(void*) {
594 for (;;) {
595 static const int kSec = 300;
596 #if defined(SK_BUILD_FOR_WIN)
597 Sleep(kSec * 1000);
598 #else
599 sleep(kSec);
600 #endif
mtkleinb37cb412015-03-18 05:27:14 -0700601 SkString running;
602 {
603 SkAutoMutexAcquire lock(gRunningMutex);
604 for (int i = 0; i < gRunning.count(); i++) {
605 running.appendf("\n\t%s", gRunning[i].c_str());
606 }
607 }
608 SkDebugf("\nCurrently running:%s\n", running.c_str());
mtklein2e1c47e2015-03-12 07:16:56 -0700609 }
610 }
611 };
612 static SkThread* intentionallyLeaked = new SkThread(Loop::forever);
613 intentionallyLeaked->start();
mtkleinde6fc2b2015-03-12 06:28:54 -0700614}
615
jcgregorio3b27ade2014-11-13 08:06:40 -0800616int dm_main();
caryclark17f0b6d2014-07-22 10:15:34 -0700617int dm_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -0800618 SetupCrashHandler();
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +0000619 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -0700620 SkTaskGroup::Enabler enabled(FLAGS_threads);
mtkleine67164d2015-02-02 13:24:37 -0800621 if (FLAGS_leaks) {
622 SkInstCountPrintLeaksOnExit();
623 }
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +0000624
mtklein2e1c47e2015-03-12 07:16:56 -0700625 start_keepalive();
mtkleinde6fc2b2015-03-12 06:28:54 -0700626
mtklein62bd1a62015-01-27 14:46:26 -0800627 gather_gold();
628
mtklein748ca3b2015-01-15 10:56:12 -0800629 gather_srcs();
630 gather_sinks();
631 gather_tests();
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +0000632
mtklein55e88b22015-01-21 15:50:13 -0800633 gPending = gSrcs.count() * gSinks.count() + gThreadedTests.count() + gGPUTests.count();
mtklein748ca3b2015-01-15 10:56:12 -0800634 SkDebugf("%d srcs * %d sinks + %d tests == %d tasks\n",
mtklein55e88b22015-01-21 15:50:13 -0800635 gSrcs.count(), gSinks.count(), gThreadedTests.count() + gGPUTests.count(), gPending);
mtklein748ca3b2015-01-15 10:56:12 -0800636
637 // We try to exploit as much parallelism as is safe. Most Src/Sink pairs run on any thread,
638 // but Sinks that identify as part of a particular enclave run serially on a single thread.
mtklein82d28432015-01-15 12:46:02 -0800639 // CPU tests run on any thread. GPU tests depend on --gpu_threading.
mtklein748ca3b2015-01-15 10:56:12 -0800640 SkTArray<Task> enclaves[kNumEnclaves];
641 for (int j = 0; j < gSinks.count(); j++) {
642 SkTArray<Task>& tasks = enclaves[gSinks[j]->enclave()];
643 for (int i = 0; i < gSrcs.count(); i++) {
644 tasks.push_back(Task(gSrcs[i], gSinks[j]));
645 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +0000646 }
647
mtklein748ca3b2015-01-15 10:56:12 -0800648 SkTaskGroup tg;
mtklein55e88b22015-01-21 15:50:13 -0800649 tg.batch(run_test, gThreadedTests.begin(), gThreadedTests.count());
650 for (int i = 0; i < kNumEnclaves; i++) {
651 switch(i) {
652 case kAnyThread_Enclave:
653 tg.batch(Task::Run, enclaves[i].begin(), enclaves[i].count());
654 break;
655 case kGPU_Enclave:
656 tg.add(run_enclave_and_gpu_tests, &enclaves[i]);
657 break;
658 default:
659 tg.add(run_enclave, &enclaves[i]);
660 break;
mtklein82d28432015-01-15 12:46:02 -0800661 }
mtklein55e88b22015-01-21 15:50:13 -0800662 }
mtklein748ca3b2015-01-15 10:56:12 -0800663 tg.wait();
mtklein748ca3b2015-01-15 10:56:12 -0800664 // At this point we're back in single-threaded land.
mtklein@google.comd36522d2013-10-16 13:02:15 +0000665
mtklein2f64eec2015-01-15 14:20:41 -0800666 SkDebugf("\n");
mtklein748ca3b2015-01-15 10:56:12 -0800667 if (gFailures.count() > 0) {
668 SkDebugf("Failures:\n");
669 for (int i = 0; i < gFailures.count(); i++) {
mtklein9dc09102015-01-15 15:47:33 -0800670 SkDebugf("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800671 }
672 SkDebugf("%d failures\n", gFailures.count());
673 return 1;
mtklein114c3cd2015-01-15 10:15:02 -0800674 }
mtklein748ca3b2015-01-15 10:56:12 -0800675 if (gPending > 0) {
676 SkDebugf("Hrm, we didn't seem to run everything we intended to! Please file a bug.\n");
677 return 1;
mtklein114c3cd2015-01-15 10:15:02 -0800678 }
mtklein748ca3b2015-01-15 10:56:12 -0800679 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +0000680}
jcgregorio3b27ade2014-11-13 08:06:40 -0800681
borenet48087572015-04-02 12:16:36 -0700682#if !defined(SK_BUILD_FOR_IOS)
jcgregorio3b27ade2014-11-13 08:06:40 -0800683int main(int argc, char** argv) {
684 SkCommandLineFlags::Parse(argc, argv);
685 return dm_main();
686}
687#endif