blob: 983a8704ae063bf7e020facb3d7b747dc49fec14 [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"
scroggocc2feb12015-08-14 08:32:46 -070016#include "SkCodec.h"
caryclark17f0b6d2014-07-22 10:15:34 -070017#include "SkCommonFlags.h"
caryclark83ca6282015-06-10 09:31:09 -070018#include "SkFontMgr.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +000019#include "SkForceLinking.h"
20#include "SkGraphics.h"
mtklein748ca3b2015-01-15 10:56:12 -080021#include "SkMD5.h"
mtklein1b249332015-07-07 12:21:21 -070022#include "SkMutex.h"
mtklein1d0f1642014-09-08 08:05:18 -070023#include "SkOSFile.h"
mtkleina82f5622015-02-20 12:30:19 -080024#include "SkTHash.h"
mtklein406654b2014-09-03 15:34:37 -070025#include "SkTaskGroup.h"
mtkleinde6fc2b2015-03-12 06:28:54 -070026#include "SkThreadUtils.h"
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +000027#include "Test.h"
mtklein748ca3b2015-01-15 10:56:12 -080028#include "Timer.h"
caryclark83ca6282015-06-10 09:31:09 -070029#include "sk_tool_utils.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +000030
mtkleina5114d72015-08-24 13:27:01 -070031#ifdef SKIA_PNG_PREFIXED
32 // this must proceed png.h
33 #include "pngprefix.h"
34#endif
35#include "png.h"
36
bungeman60e0fee2015-08-26 05:15:46 -070037#include <stdlib.h>
38
djsollen54416de2015-04-03 07:24:48 -070039DEFINE_string(src, "tests gm skp image", "Source types to test.");
mtklein748ca3b2015-01-15 10:56:12 -080040DEFINE_bool(nameByHash, false,
41 "If true, write to FLAGS_writePath[0]/<hash>.png instead of "
djsollen54416de2015-04-03 07:24:48 -070042 "to FLAGS_writePath[0]/<config>/<sourceType>/<sourceOptions>/<name>.png");
mtklein748ca3b2015-01-15 10:56:12 -080043DEFINE_bool2(pathOpsExtended, x, false, "Run extended pathOps tests.");
mtkleind603b222015-02-17 11:13:33 -080044DEFINE_string(matrix, "1 0 0 1",
45 "2x2 scale+skew matrix to apply or upright when using "
46 "'matrix' or 'upright' in config.");
mtklein82d28432015-01-15 12:46:02 -080047DEFINE_bool(gpu_threading, false, "Allow GPU work to run on multiple threads?");
mtklein@google.comd36522d2013-10-16 13:02:15 +000048
mtkleina2ef6422015-01-15 13:44:22 -080049DEFINE_string(blacklist, "",
djsollen54416de2015-04-03 07:24:48 -070050 "Space-separated config/src/srcOptions/name quadruples to blacklist. '_' matches anything. E.g. \n"
51 "'--blacklist gpu skp _ _' will blacklist all SKPs drawn into the gpu config.\n"
52 "'--blacklist gpu skp _ _ 8888 gm _ aarects' will also blacklist the aarects GM on 8888.");
mtkleina2ef6422015-01-15 13:44:22 -080053
mtklein62bd1a62015-01-27 14:46:26 -080054DEFINE_string2(readPath, r, "", "If set check for equality with golden results in this directory.");
55
borenet09ed4802015-04-03 14:15:33 -070056DEFINE_string(uninterestingHashesFile, "",
57 "File containing a list of uninteresting hashes. If a result hashes to something in "
58 "this list, no image is written for that result.");
59
mtklein6393c062015-04-27 08:45:01 -070060DEFINE_int32(shards, 1, "We're splitting source data into this many shards.");
61DEFINE_int32(shard, 0, "Which shard do I run?");
bsalomon821e10e2015-06-04 14:15:33 -070062DEFINE_bool2(pre_log, p, false, "Log before running each test. May be incomprehensible when threading");
mtklein6393c062015-04-27 08:45:01 -070063
mtklein@google.comd36522d2013-10-16 13:02:15 +000064__SK_FORCE_IMAGE_DECODER_LINKING;
mtklein748ca3b2015-01-15 10:56:12 -080065using namespace DM;
mtklein@google.comd36522d2013-10-16 13:02:15 +000066
mtklein748ca3b2015-01-15 10:56:12 -080067/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
68
69SK_DECLARE_STATIC_MUTEX(gFailuresMutex);
70static SkTArray<SkString> gFailures;
71
72static void fail(ImplicitString err) {
73 SkAutoMutexAcquire lock(gFailuresMutex);
74 SkDebugf("\n\nFAILURE: %s\n\n", err.c_str());
75 gFailures.push_back(err);
halcanary9e398f72015-01-10 11:18:04 -080076}
77
mtkleinb37cb412015-03-18 05:27:14 -070078static int32_t gPending = 0; // Atomic. Total number of running and queued tasks.
79
80SK_DECLARE_STATIC_MUTEX(gRunningMutex);
81static SkTArray<SkString> gRunning;
mtklein748ca3b2015-01-15 10:56:12 -080082
mtkleinb9eb4ac2015-02-02 18:26:03 -080083static void done(double ms,
djsollen54416de2015-04-03 07:24:48 -070084 ImplicitString config, ImplicitString src, ImplicitString srcOptions,
85 ImplicitString name, ImplicitString note, ImplicitString log) {
86 SkString id = SkStringPrintf("%s %s %s %s", config.c_str(), src.c_str(),
87 srcOptions.c_str(), name.c_str());
mtkleinb37cb412015-03-18 05:27:14 -070088 {
89 SkAutoMutexAcquire lock(gRunningMutex);
90 for (int i = 0; i < gRunning.count(); i++) {
91 if (gRunning[i] == id) {
92 gRunning.removeShuffle(i);
93 break;
94 }
95 }
96 }
97 if (!FLAGS_verbose) {
98 note = "";
99 }
mtkleinb9eb4ac2015-02-02 18:26:03 -0800100 if (!log.isEmpty()) {
101 log.prepend("\n");
102 }
mtklein6dee2ad2015-02-05 09:53:44 -0800103 auto pending = sk_atomic_dec(&gPending)-1;
reed50bc0512015-05-19 14:13:31 -0700104 if (!FLAGS_quiet) {
mtklein711a2452015-07-08 08:49:20 -0700105 SkDebugf("%s(%4d/%-4dMB %6d) %s\t%s%s%s", FLAGS_verbose ? "\n" : kSkOverwriteLine
reed50bc0512015-05-19 14:13:31 -0700106 , sk_tools::getCurrResidentSetSizeMB()
107 , sk_tools::getMaxResidentSetSizeMB()
108 , pending
109 , HumanizeMs(ms).c_str()
110 , id.c_str()
111 , note.c_str()
112 , log.c_str());
113 }
mtkleina17241b2015-01-23 05:48:00 -0800114 // We write our dm.json file every once in a while in case we crash.
115 // Notice this also handles the final dm.json when pending == 0.
116 if (pending % 500 == 0) {
117 JsonWriter::DumpJson();
118 }
mtklein@google.comd36522d2013-10-16 13:02:15 +0000119}
120
djsollen54416de2015-04-03 07:24:48 -0700121static void start(ImplicitString config, ImplicitString src,
122 ImplicitString srcOptions, ImplicitString name) {
123 SkString id = SkStringPrintf("%s %s %s %s", config.c_str(), src.c_str(),
124 srcOptions.c_str(), name.c_str());
mtkleinb37cb412015-03-18 05:27:14 -0700125 SkAutoMutexAcquire lock(gRunningMutex);
126 gRunning.push_back(id);
127}
128
mtklein748ca3b2015-01-15 10:56:12 -0800129/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
mtklein114c3cd2015-01-15 10:15:02 -0800130
mtklein62bd1a62015-01-27 14:46:26 -0800131struct Gold : public SkString {
mtkleina82f5622015-02-20 12:30:19 -0800132 Gold() : SkString("") {}
djsollen54416de2015-04-03 07:24:48 -0700133 Gold(ImplicitString sink, ImplicitString src, ImplicitString srcOptions,
134 ImplicitString name, ImplicitString md5)
mtklein62bd1a62015-01-27 14:46:26 -0800135 : SkString("") {
136 this->append(sink);
137 this->append(src);
djsollen54416de2015-04-03 07:24:48 -0700138 this->append(srcOptions);
mtklein62bd1a62015-01-27 14:46:26 -0800139 this->append(name);
140 this->append(md5);
mtklein62bd1a62015-01-27 14:46:26 -0800141 }
mtklein02f46cf2015-03-20 13:48:42 -0700142 static uint32_t Hash(const Gold& g) { return SkGoodHash((const SkString&)g); }
mtklein62bd1a62015-01-27 14:46:26 -0800143};
mtkleina82f5622015-02-20 12:30:19 -0800144static SkTHashSet<Gold, Gold::Hash> gGold;
mtklein62bd1a62015-01-27 14:46:26 -0800145
146static void add_gold(JsonWriter::BitmapResult r) {
djsollen54416de2015-04-03 07:24:48 -0700147 gGold.add(Gold(r.config, r.sourceType, r.sourceOptions, r.name, r.md5));
mtklein62bd1a62015-01-27 14:46:26 -0800148}
149
150static void gather_gold() {
151 if (!FLAGS_readPath.isEmpty()) {
152 SkString path(FLAGS_readPath[0]);
153 path.append("/dm.json");
154 if (!JsonWriter::ReadJson(path.c_str(), add_gold)) {
155 fail(SkStringPrintf("Couldn't read %s for golden results.", path.c_str()));
156 }
157 }
158}
159
160/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
161
borenet09ed4802015-04-03 14:15:33 -0700162static SkTHashSet<SkString> gUninterestingHashes;
163
164static void gather_uninteresting_hashes() {
165 if (!FLAGS_uninterestingHashesFile.isEmpty()) {
166 SkAutoTUnref<SkData> data(SkData::NewFromFileName(FLAGS_uninterestingHashesFile[0]));
167 SkTArray<SkString> hashes;
168 SkStrSplit((const char*)data->data(), "\n", &hashes);
169 for (const SkString& hash : hashes) {
170 gUninterestingHashes.add(hash);
171 }
172 }
173}
174
175/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
176
mtkleine0effd62015-07-29 06:37:28 -0700177struct TaggedSrc : public SkAutoTDelete<Src> {
178 const char* tag;
179 const char* options;
180};
181
182struct TaggedSink : public SkAutoTDelete<Sink> {
183 const char* tag;
djsollen54416de2015-04-03 07:24:48 -0700184};
mtklein748ca3b2015-01-15 10:56:12 -0800185
186static const bool kMemcpyOK = true;
187
mtkleine0effd62015-07-29 06:37:28 -0700188static SkTArray<TaggedSrc, kMemcpyOK> gSrcs;
189static SkTArray<TaggedSink, kMemcpyOK> gSinks;
mtklein748ca3b2015-01-15 10:56:12 -0800190
mtklein6393c062015-04-27 08:45:01 -0700191static bool in_shard() {
192 static int N = 0;
193 return N++ % FLAGS_shards == FLAGS_shard;
194}
195
djsollen54416de2015-04-03 07:24:48 -0700196static void push_src(const char* tag, const char* options, Src* s) {
mtklein748ca3b2015-01-15 10:56:12 -0800197 SkAutoTDelete<Src> src(s);
mtklein6393c062015-04-27 08:45:01 -0700198 if (in_shard() &&
199 FLAGS_src.contains(tag) &&
mtklein748ca3b2015-01-15 10:56:12 -0800200 !SkCommandLineFlags::ShouldSkip(FLAGS_match, src->name().c_str())) {
mtkleine0effd62015-07-29 06:37:28 -0700201 TaggedSrc& s = gSrcs.push_back();
mtklein748ca3b2015-01-15 10:56:12 -0800202 s.reset(src.detach());
203 s.tag = tag;
djsollen54416de2015-04-03 07:24:48 -0700204 s.options = options;
mtklein114c3cd2015-01-15 10:15:02 -0800205 }
mtklein748ca3b2015-01-15 10:56:12 -0800206}
mtklein114c3cd2015-01-15 10:15:02 -0800207
msarett438b2ad2015-04-09 12:43:10 -0700208static void push_codec_srcs(Path path) {
209 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
210 if (!encoded) {
211 SkDebugf("Couldn't read %s.", path.c_str());
212 return;
213 }
214 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
215 if (NULL == codec.get()) {
216 SkDebugf("Couldn't create codec for %s.", path.c_str());
217 return;
218 }
219
msarett0a242972015-06-11 14:27:27 -0700220 // Choose scales for scaling tests.
221 // TODO (msarett): Add more scaling tests as we implement more flexible scaling.
222 // TODO (msarett): Implement scaling tests for SkImageDecoder in order to compare with these
223 // tests. SkImageDecoder supports downscales by integer factors.
emmaleer8f4ba762015-08-14 07:44:46 -0700224 // SkJpegCodec natively supports scaling to: 0.125, 0.25, 0.375, 0.5, 0.625, 0.75, 0.875
225 // 0.1, 0.16, 0.2 etc allow us to test SkScaledCodec with sampleSize 10, 6, 5, etc
226 // 0.4, 0.7 etc allow to test what happens when the client requests a scale that
227 // does not exactly match a sampleSize or native scaling capability
228 const float scales[] = { 0.1f, 0.125f, 0.166f, 0.2f, 0.25f, 0.333f, 0.375f, 0.4f, 0.5f, 0.6f,
229 0.625f, 0.750f, 0.8f, 0.875f, 1.0f };
msarett438b2ad2015-04-09 12:43:10 -0700230
msarett0a242972015-06-11 14:27:27 -0700231 for (float scale : scales) {
scroggo0df0e432015-08-07 12:53:25 -0700232 if (scale != 1.0f && (path.endsWith(".webp") || path.endsWith(".WEBP"))) {
233 // FIXME: skbug.com/4038 Scaling webp seems to leave some pixels uninitialized/
234 // compute their colors based on uninitialized values.
235 continue;
236 }
msarett0a242972015-06-11 14:27:27 -0700237 // Build additional test cases for images that decode natively to non-canvas types
238 switch(codec->getInfo().colorType()) {
239 case kGray_8_SkColorType:
240 push_src("image", "codec_kGray8", new CodecSrc(path, CodecSrc::kNormal_Mode,
241 CodecSrc::kGrayscale_Always_DstColorType, scale));
242 push_src("image", "scanline_kGray8", new CodecSrc(path, CodecSrc::kScanline_Mode,
243 CodecSrc::kGrayscale_Always_DstColorType, scale));
244 push_src("image", "scanline_subset_kGray8", new CodecSrc(path,
245 CodecSrc::kScanline_Subset_Mode, CodecSrc::kGrayscale_Always_DstColorType,
246 scale));
247 push_src("image", "stripe_kGray8", new CodecSrc(path, CodecSrc::kStripe_Mode,
248 CodecSrc::kGrayscale_Always_DstColorType, scale));
249 // Intentional fall through
250 // FIXME: Is this a long term solution for testing wbmps decodes to kIndex8?
251 // Further discussion on this topic is at skbug.com/3683
252 case kIndex_8_SkColorType:
253 push_src("image", "codec_kIndex8", new CodecSrc(path, CodecSrc::kNormal_Mode,
254 CodecSrc::kIndex8_Always_DstColorType, scale));
255 push_src("image", "scanline_kIndex8", new CodecSrc(path, CodecSrc::kScanline_Mode,
256 CodecSrc::kIndex8_Always_DstColorType, scale));
257 push_src("image", "scanline_subset_kIndex8", new CodecSrc(path,
258 CodecSrc::kScanline_Subset_Mode, CodecSrc::kIndex8_Always_DstColorType,
259 scale));
260 push_src("image", "stripe_kIndex8", new CodecSrc(path, CodecSrc::kStripe_Mode,
261 CodecSrc::kIndex8_Always_DstColorType, scale));
262 break;
263 default:
264 // Do nothing
265 break;
266 }
267
268 // Decode all images to the canvas color type
269 push_src("image", "codec", new CodecSrc(path, CodecSrc::kNormal_Mode,
270 CodecSrc::kGetFromCanvas_DstColorType, scale));
271 push_src("image", "scanline", new CodecSrc(path, CodecSrc::kScanline_Mode,
272 CodecSrc::kGetFromCanvas_DstColorType, scale));
273 push_src("image", "scanline_subset", new CodecSrc(path, CodecSrc::kScanline_Subset_Mode,
274 CodecSrc::kGetFromCanvas_DstColorType, scale));
275 push_src("image", "stripe", new CodecSrc(path, CodecSrc::kStripe_Mode,
276 CodecSrc::kGetFromCanvas_DstColorType, scale));
scroggob636b452015-07-22 07:16:20 -0700277 // Note: The only codec which supports subsets natively is SkWebpCodec, which will never
278 // report kIndex_8 or kGray_8, so there is no need to test kSubset_mode with those color
279 // types specifically requested.
280 push_src("image", "codec_subset", new CodecSrc(path, CodecSrc::kSubset_Mode,
281 CodecSrc::kGetFromCanvas_DstColorType, scale));
msarett0a242972015-06-11 14:27:27 -0700282 }
msarett438b2ad2015-04-09 12:43:10 -0700283}
284
scroggo9b77ddd2015-03-19 06:03:39 -0700285static bool codec_supported(const char* ext) {
286 // FIXME: Once other versions of SkCodec are available, we can add them to this
287 // list (and eventually we can remove this check once they are all supported).
msarett8c8f22a2015-04-01 06:58:48 -0700288 static const char* const exts[] = {
scroggo6f5e6192015-06-18 12:53:43 -0700289 "bmp", "gif", "jpg", "jpeg", "png", "ico", "wbmp", "webp",
290 "BMP", "GIF", "JPG", "JPEG", "PNG", "ICO", "WBMP", "WEBP",
msarett8c8f22a2015-04-01 06:58:48 -0700291 };
292
293 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
294 if (0 == strcmp(exts[i], ext)) {
295 return true;
296 }
297 }
298 return false;
scroggo9b77ddd2015-03-19 06:03:39 -0700299}
300
mtklein748ca3b2015-01-15 10:56:12 -0800301static void gather_srcs() {
302 for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) {
djsollen54416de2015-04-03 07:24:48 -0700303 push_src("gm", "", new GMSrc(r->factory()));
mtklein748ca3b2015-01-15 10:56:12 -0800304 }
halcanaryfc37ad12015-01-30 07:31:19 -0800305 for (int i = 0; i < FLAGS_skps.count(); i++) {
306 const char* path = FLAGS_skps[i];
307 if (sk_isdir(path)) {
308 SkOSFile::Iter it(path, "skp");
309 for (SkString file; it.next(&file); ) {
djsollen54416de2015-04-03 07:24:48 -0700310 push_src("skp", "", new SKPSrc(SkOSPath::Join(path, file.c_str())));
halcanaryfc37ad12015-01-30 07:31:19 -0800311 }
312 } else {
djsollen54416de2015-04-03 07:24:48 -0700313 push_src("skp", "", new SKPSrc(path));
mtklein114c3cd2015-01-15 10:15:02 -0800314 }
315 }
halcanary23b03c32015-01-30 09:58:58 -0800316 static const char* const exts[] = {
317 "bmp", "gif", "jpg", "jpeg", "png", "webp", "ktx", "astc", "wbmp", "ico",
318 "BMP", "GIF", "JPG", "JPEG", "PNG", "WEBP", "KTX", "ASTC", "WBMP", "ICO",
319 };
320 for (int i = 0; i < FLAGS_images.count(); i++) {
321 const char* flag = FLAGS_images[i];
322 if (sk_isdir(flag)) {
323 for (size_t j = 0; j < SK_ARRAY_COUNT(exts); j++) {
324 SkOSFile::Iter it(flag, exts[j]);
325 for (SkString file; it.next(&file); ) {
326 SkString path = SkOSPath::Join(flag, file.c_str());
djsollen54416de2015-04-03 07:24:48 -0700327 push_src("image", "decode", new ImageSrc(path)); // Decode entire image
328 push_src("image", "subset", new ImageSrc(path, 2)); // Decode into 2x2 subsets
scroggo9b77ddd2015-03-19 06:03:39 -0700329 if (codec_supported(exts[j])) {
msarett438b2ad2015-04-09 12:43:10 -0700330 push_codec_srcs(path);
scroggo9b77ddd2015-03-19 06:03:39 -0700331 }
halcanary23b03c32015-01-30 09:58:58 -0800332 }
mtklein114c3cd2015-01-15 10:15:02 -0800333 }
halcanary23b03c32015-01-30 09:58:58 -0800334 } else if (sk_exists(flag)) {
335 // assume that FLAGS_images[i] is a valid image if it is a file.
djsollen54416de2015-04-03 07:24:48 -0700336 push_src("image", "decode", new ImageSrc(flag)); // Decode entire image.
337 push_src("image", "subset", new ImageSrc(flag, 2)); // Decode into 2 x 2 subsets
msarett438b2ad2015-04-09 12:43:10 -0700338 push_codec_srcs(flag);
mtklein709d2c32015-01-15 08:30:25 -0800339 }
mtklein709d2c32015-01-15 08:30:25 -0800340 }
341}
342
mtklein748ca3b2015-01-15 10:56:12 -0800343static GrGLStandard get_gpu_api() {
344 if (FLAGS_gpuAPI.contains("gl")) { return kGL_GrGLStandard; }
345 if (FLAGS_gpuAPI.contains("gles")) { return kGLES_GrGLStandard; }
346 return kNone_GrGLStandard;
mtklein709d2c32015-01-15 08:30:25 -0800347}
348
mtklein748ca3b2015-01-15 10:56:12 -0800349static void push_sink(const char* tag, Sink* s) {
350 SkAutoTDelete<Sink> sink(s);
351 if (!FLAGS_config.contains(tag)) {
352 return;
mtklein114c3cd2015-01-15 10:15:02 -0800353 }
mtkleine0effd62015-07-29 06:37:28 -0700354 // Try a simple Src as a canary. If it fails, skip this sink.
mtklein748ca3b2015-01-15 10:56:12 -0800355 struct : public Src {
mtkleine0effd62015-07-29 06:37:28 -0700356 Error draw(SkCanvas* c) const override {
357 c->drawRect(SkRect::MakeWH(1,1), SkPaint());
358 return "";
359 }
mtklein36352bf2015-03-25 18:17:31 -0700360 SkISize size() const override { return SkISize::Make(16, 16); }
mtkleine0effd62015-07-29 06:37:28 -0700361 Name name() const override { return "justOneRect"; }
362 } justOneRect;
mtklein114c3cd2015-01-15 10:15:02 -0800363
mtklein748ca3b2015-01-15 10:56:12 -0800364 SkBitmap bitmap;
365 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800366 SkString log;
mtkleine0effd62015-07-29 06:37:28 -0700367 Error err = sink->draw(justOneRect, &bitmap, &stream, &log);
mtklein4089ef72015-03-05 08:40:28 -0800368 if (err.isFatal()) {
mtklein05641a52015-04-21 10:49:13 -0700369 SkDebugf("Could not run %s: %s\n", tag, err.c_str());
370 exit(1);
mtklein114c3cd2015-01-15 10:15:02 -0800371 }
372
mtkleine0effd62015-07-29 06:37:28 -0700373 TaggedSink& ts = gSinks.push_back();
mtklein748ca3b2015-01-15 10:56:12 -0800374 ts.reset(sink.detach());
375 ts.tag = tag;
376}
377
378static bool gpu_supported() {
379#if SK_SUPPORT_GPU
380 return FLAGS_gpu;
381#else
382 return false;
383#endif
384}
385
386static Sink* create_sink(const char* tag) {
387#define SINK(t, sink, ...) if (0 == strcmp(t, tag)) { return new sink(__VA_ARGS__); }
388 if (gpu_supported()) {
mtklein82d28432015-01-15 12:46:02 -0800389 typedef GrContextFactory Gr;
mtklein748ca3b2015-01-15 10:56:12 -0800390 const GrGLStandard api = get_gpu_api();
mtklein82d28432015-01-15 12:46:02 -0800391 SINK("gpunull", GPUSink, Gr::kNull_GLContextType, api, 0, false, FLAGS_gpu_threading);
392 SINK("gpudebug", GPUSink, Gr::kDebug_GLContextType, api, 0, false, FLAGS_gpu_threading);
393 SINK("gpu", GPUSink, Gr::kNative_GLContextType, api, 0, false, FLAGS_gpu_threading);
394 SINK("gpudft", GPUSink, Gr::kNative_GLContextType, api, 0, true, FLAGS_gpu_threading);
395 SINK("msaa4", GPUSink, Gr::kNative_GLContextType, api, 4, false, FLAGS_gpu_threading);
396 SINK("msaa16", GPUSink, Gr::kNative_GLContextType, api, 16, false, FLAGS_gpu_threading);
397 SINK("nvprmsaa4", GPUSink, Gr::kNVPR_GLContextType, api, 4, false, FLAGS_gpu_threading);
398 SINK("nvprmsaa16", GPUSink, Gr::kNVPR_GLContextType, api, 16, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800399 #if SK_ANGLE
mtklein82d28432015-01-15 12:46:02 -0800400 SINK("angle", GPUSink, Gr::kANGLE_GLContextType, api, 0, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800401 #endif
402 #if SK_MESA
mtklein82d28432015-01-15 12:46:02 -0800403 SINK("mesa", GPUSink, Gr::kMESA_GLContextType, api, 0, false, FLAGS_gpu_threading);
mtklein748ca3b2015-01-15 10:56:12 -0800404 #endif
mtklein114c3cd2015-01-15 10:15:02 -0800405 }
mtklein748ca3b2015-01-15 10:56:12 -0800406
tomhudsoneebc39a2015-02-23 12:18:05 -0800407#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
408 SINK("hwui", HWUISink);
409#endif
410
mtklein748ca3b2015-01-15 10:56:12 -0800411 if (FLAGS_cpu) {
412 SINK("565", RasterSink, kRGB_565_SkColorType);
413 SINK("8888", RasterSink, kN32_SkColorType);
mtklein19f30602015-01-20 13:34:39 -0800414 SINK("pdf", PDFSink);
mtklein9c3f17d2015-01-28 11:35:18 -0800415 SINK("skp", SKPSink);
mtklein8a4527e2015-01-31 20:00:58 -0800416 SINK("svg", SVGSink);
417 SINK("null", NullSink);
halcanary47ef4d52015-03-03 09:13:09 -0800418 SINK("xps", XPSSink);
mtklein748ca3b2015-01-15 10:56:12 -0800419 }
420#undef SINK
421 return NULL;
mtklein114c3cd2015-01-15 10:15:02 -0800422}
423
mtklein748ca3b2015-01-15 10:56:12 -0800424static Sink* create_via(const char* tag, Sink* wrapped) {
425#define VIA(t, via, ...) if (0 == strcmp(t, tag)) { return new via(__VA_ARGS__); }
mtklein6fbf4b32015-05-06 11:35:40 -0700426 VIA("twice", ViaTwice, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800427 VIA("pipe", ViaPipe, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800428 VIA("serialize", ViaSerialization, wrapped);
mtkleinb7e8d692015-04-07 08:30:32 -0700429 VIA("2ndpic", ViaSecondPicture, wrapped);
mtkleind31c13d2015-05-05 12:59:56 -0700430 VIA("sp", ViaSingletonPictures, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800431 VIA("tiles", ViaTiles, 256, 256, NULL, wrapped);
432 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800433
mtkleind603b222015-02-17 11:13:33 -0800434 if (FLAGS_matrix.count() == 4) {
mtklein748ca3b2015-01-15 10:56:12 -0800435 SkMatrix m;
mtkleind603b222015-02-17 11:13:33 -0800436 m.reset();
437 m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
438 m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
439 m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
440 m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
441 VIA("matrix", ViaMatrix, m, wrapped);
442 VIA("upright", ViaUpright, m, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800443 }
tomhudson64de1e12015-03-05 08:01:07 -0800444
445#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
446 VIA("androidsdk", ViaAndroidSDK, wrapped);
447#endif
448
mtklein748ca3b2015-01-15 10:56:12 -0800449#undef VIA
450 return NULL;
mtklein114c3cd2015-01-15 10:15:02 -0800451}
452
mtklein748ca3b2015-01-15 10:56:12 -0800453static void gather_sinks() {
454 for (int i = 0; i < FLAGS_config.count(); i++) {
455 const char* config = FLAGS_config[i];
456 SkTArray<SkString> parts;
457 SkStrSplit(config, "-", &parts);
458
459 Sink* sink = NULL;
460 for (int i = parts.count(); i-- > 0;) {
461 const char* part = parts[i].c_str();
462 Sink* next = (sink == NULL) ? create_sink(part) : create_via(part, sink);
463 if (next == NULL) {
464 SkDebugf("Skipping %s: Don't understand '%s'.\n", config, part);
465 delete sink;
466 sink = NULL;
467 break;
468 }
469 sink = next;
470 }
471 if (sink) {
472 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800473 }
474 }
475}
mtklein709d2c32015-01-15 08:30:25 -0800476
mtkleina5114d72015-08-24 13:27:01 -0700477static bool dump_png(SkBitmap bitmap, const char* path, const char* md5) {
478 const int w = bitmap.width(),
479 h = bitmap.height();
480
481 // First get the bitmap into N32 color format. The next step will work only there.
482 if (bitmap.colorType() != kN32_SkColorType) {
483 SkBitmap n32;
484 if (!bitmap.copyTo(&n32, kN32_SkColorType)) {
485 return false;
486 }
487 bitmap = n32;
488 }
489
490 // Convert our N32 bitmap into unpremul RGBA for libpng.
491 SkAutoTMalloc<uint32_t> rgba(w*h);
492 if (!bitmap.readPixels(SkImageInfo::Make(w,h, kRGBA_8888_SkColorType, kUnpremul_SkAlphaType),
493 rgba, 4*w, 0,0)) {
494 return false;
495 }
496
497 // We don't need bitmap anymore. Might as well drop our ref.
mtkleinfccb77d2015-08-24 14:13:29 -0700498 bitmap.reset();
mtkleina5114d72015-08-24 13:27:01 -0700499
mtkleinc64137c2015-08-25 10:56:08 -0700500 FILE* f = fopen(path, "wb");
mtkleina5114d72015-08-24 13:27:01 -0700501 if (!f) { return false; }
502
503 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
504 if (!png) {
505 fclose(f);
506 return false;
507 }
508
509 png_infop info = png_create_info_struct(png);
510 if (!info) {
511 png_destroy_write_struct(&png, &info);
512 fclose(f);
513 return false;
514 }
515
516 png_text text[2];
517 text[0].key = (png_charp)"Author";
518 text[0].text = (png_charp)"DM dump_png()";
519 text[0].compression = PNG_TEXT_COMPRESSION_NONE;
520 text[1].key = (png_charp)"Description";
521 text[1].text = (png_charp)md5;
522 text[1].compression = PNG_TEXT_COMPRESSION_NONE;
523 png_set_text(png, info, text, 2);
524
525 png_init_io(png, f);
526 png_set_IHDR(png, info, (png_uint_32)w, (png_uint_32)h, 8,
527 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
528 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
529 png_write_info(png, info);
530 for (int j = 0; j < h; j++) {
531 png_bytep row = (png_bytep)(rgba.get() + w*j);
532 png_write_rows(png, &row, 1);
533 }
534 png_write_end(png, info);
535
536 png_destroy_write_struct(&png, &info);
537 fclose(f);
538 return true;
539}
540
mtkleina2ef6422015-01-15 13:44:22 -0800541static bool match(const char* needle, const char* haystack) {
542 return 0 == strcmp("_", needle) || NULL != strstr(haystack, needle);
543}
544
djsollen54416de2015-04-03 07:24:48 -0700545static ImplicitString is_blacklisted(const char* sink, const char* src,
546 const char* srcOptions, const char* name) {
mtklein0d243ff2015-04-03 07:51:00 -0700547 for (int i = 0; i < FLAGS_blacklist.count() - 3; i += 4) {
mtkleina2ef6422015-01-15 13:44:22 -0800548 if (match(FLAGS_blacklist[i+0], sink) &&
djsollen54416de2015-04-03 07:24:48 -0700549 match(FLAGS_blacklist[i+1], src) &&
550 match(FLAGS_blacklist[i+2], srcOptions) &&
551 match(FLAGS_blacklist[i+3], name)) {
552 return SkStringPrintf("%s %s %s %s",
553 FLAGS_blacklist[i+0], FLAGS_blacklist[i+1],
554 FLAGS_blacklist[i+2], FLAGS_blacklist[i+3]);
mtkleina2ef6422015-01-15 13:44:22 -0800555 }
556 }
557 return "";
558}
559
mtklein748ca3b2015-01-15 10:56:12 -0800560// The finest-grained unit of work we can run: draw a single Src into a single Sink,
561// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
562struct Task {
mtkleine0effd62015-07-29 06:37:28 -0700563 Task(const TaggedSrc& src, const TaggedSink& sink) : src(src), sink(sink) {}
564 const TaggedSrc& src;
565 const TaggedSink& sink;
mtklein748ca3b2015-01-15 10:56:12 -0800566
567 static void Run(Task* task) {
mtkleina2ef6422015-01-15 13:44:22 -0800568 SkString name = task->src->name();
mtkleine0effd62015-07-29 06:37:28 -0700569
570 // We'll skip drawing this Src/Sink pair if:
571 // - the Src vetoes the Sink;
572 // - this Src / Sink combination is on the blacklist;
573 // - it's a dry run.
mtklein99cab4e2015-07-31 06:43:04 -0700574 SkString note(task->src->veto(task->sink->flags()) ? " (veto)" : "");
djsollen54416de2015-04-03 07:24:48 -0700575 SkString whyBlacklisted = is_blacklisted(task->sink.tag, task->src.tag,
576 task->src.options, name.c_str());
mtkleinb37cb412015-03-18 05:27:14 -0700577 if (!whyBlacklisted.isEmpty()) {
578 note.appendf(" (--blacklist %s)", whyBlacklisted.c_str());
579 }
mtkleine0effd62015-07-29 06:37:28 -0700580
mtkleinb9eb4ac2015-02-02 18:26:03 -0800581 SkString log;
mtklein748ca3b2015-01-15 10:56:12 -0800582 WallTimer timer;
583 timer.start();
mtkleine0effd62015-07-29 06:37:28 -0700584 if (!FLAGS_dryRun && note.isEmpty()) {
mtklein748ca3b2015-01-15 10:56:12 -0800585 SkBitmap bitmap;
586 SkDynamicMemoryWStream stream;
bsalomon821e10e2015-06-04 14:15:33 -0700587 if (FLAGS_pre_log) {
588 SkDebugf("\nRunning %s->%s", name.c_str(), task->sink.tag);
589 }
djsollen54416de2015-04-03 07:24:48 -0700590 start(task->sink.tag, task->src.tag, task->src.options, name.c_str());
mtkleinb9eb4ac2015-02-02 18:26:03 -0800591 Error err = task->sink->draw(*task->src, &bitmap, &stream, &log);
mtklein748ca3b2015-01-15 10:56:12 -0800592 if (!err.isEmpty()) {
mtklein4089ef72015-03-05 08:40:28 -0800593 timer.end();
594 if (err.isFatal()) {
djsollen54416de2015-04-03 07:24:48 -0700595 fail(SkStringPrintf("%s %s %s %s: %s",
mtklein4089ef72015-03-05 08:40:28 -0800596 task->sink.tag,
597 task->src.tag,
djsollen54416de2015-04-03 07:24:48 -0700598 task->src.options,
mtklein4089ef72015-03-05 08:40:28 -0800599 name.c_str(),
600 err.c_str()));
mtkleinb37cb412015-03-18 05:27:14 -0700601 } else {
602 note.appendf(" (skipped: %s)", err.c_str());
mtklein4089ef72015-03-05 08:40:28 -0800603 }
djsollen54416de2015-04-03 07:24:48 -0700604 done(timer.fWall, task->sink.tag, task->src.tag, task->src.options,
605 name, note, log);
mtklein4089ef72015-03-05 08:40:28 -0800606 return;
mtklein748ca3b2015-01-15 10:56:12 -0800607 }
mtklein62bd1a62015-01-27 14:46:26 -0800608 SkAutoTDelete<SkStreamAsset> data(stream.detachAsStream());
609
610 SkString md5;
611 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
612 SkMD5 hash;
613 if (data->getLength()) {
614 hash.writeStream(data, data->getLength());
615 data->rewind();
616 } else {
mtklein38408462015-07-08 07:25:27 -0700617 // If we're BGRA (Linux, Windows), swizzle over to RGBA (Mac, Android).
618 // This helps eliminate multiple 0-pixel-diff hashes on gold.skia.org.
619 // (Android's general slow speed breaks the tie arbitrarily in RGBA's favor.)
620 // We might consider promoting 565 to RGBA too.
621 if (bitmap.colorType() == kBGRA_8888_SkColorType) {
622 SkBitmap swizzle;
623 SkAssertResult(bitmap.copyTo(&swizzle, kRGBA_8888_SkColorType));
624 hash.write(swizzle.getPixels(), swizzle.getSize());
625 } else {
626 hash.write(bitmap.getPixels(), bitmap.getSize());
627 }
mtklein62bd1a62015-01-27 14:46:26 -0800628 }
629 SkMD5::Digest digest;
630 hash.finish(digest);
631 for (int i = 0; i < 16; i++) {
632 md5.appendf("%02x", digest.data[i]);
633 }
634 }
635
636 if (!FLAGS_readPath.isEmpty() &&
djsollen54416de2015-04-03 07:24:48 -0700637 !gGold.contains(Gold(task->sink.tag, task->src.tag,
638 task->src.options, name, md5))) {
639 fail(SkStringPrintf("%s not found for %s %s %s %s in %s",
mtklein62bd1a62015-01-27 14:46:26 -0800640 md5.c_str(),
641 task->sink.tag,
642 task->src.tag,
djsollen54416de2015-04-03 07:24:48 -0700643 task->src.options,
mtklein62bd1a62015-01-27 14:46:26 -0800644 name.c_str(),
645 FLAGS_readPath[0]));
646 }
647
mtkleinb0531a72015-04-07 13:38:48 -0700648 if (!FLAGS_writePath.isEmpty()) {
mtklein748ca3b2015-01-15 10:56:12 -0800649 const char* ext = task->sink->fileExtension();
mtklein62bd1a62015-01-27 14:46:26 -0800650 if (data->getLength()) {
651 WriteToDisk(*task, md5, ext, data, data->getLength(), NULL);
halcanary022afb82015-01-30 11:00:12 -0800652 SkASSERT(bitmap.drawsNothing());
653 } else if (!bitmap.drawsNothing()) {
mtklein62bd1a62015-01-27 14:46:26 -0800654 WriteToDisk(*task, md5, ext, NULL, 0, &bitmap);
mtklein748ca3b2015-01-15 10:56:12 -0800655 }
656 }
657 }
658 timer.end();
djsollen54416de2015-04-03 07:24:48 -0700659 done(timer.fWall, task->sink.tag, task->src.tag, task->src.options, name, note, log);
mtklein748ca3b2015-01-15 10:56:12 -0800660 }
661
662 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -0800663 SkString md5,
664 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -0800665 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -0800666 const SkBitmap* bitmap) {
mtklein748ca3b2015-01-15 10:56:12 -0800667 JsonWriter::BitmapResult result;
djsollen54416de2015-04-03 07:24:48 -0700668 result.name = task.src->name();
669 result.config = task.sink.tag;
670 result.sourceType = task.src.tag;
671 result.sourceOptions = task.src.options;
672 result.ext = ext;
673 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -0800674 JsonWriter::AddBitmapResult(result);
675
mtkleinb0531a72015-04-07 13:38:48 -0700676 // If an MD5 is uninteresting, we want it noted in the JSON file,
677 // but don't want to dump it out as a .png (or whatever ext is).
678 if (gUninterestingHashes.contains(md5)) {
679 return;
680 }
681
mtklein748ca3b2015-01-15 10:56:12 -0800682 const char* dir = FLAGS_writePath[0];
683 if (0 == strcmp(dir, "@")) { // Needed for iOS.
684 dir = FLAGS_resourcePath[0];
685 }
686 sk_mkdir(dir);
687
688 SkString path;
689 if (FLAGS_nameByHash) {
690 path = SkOSPath::Join(dir, result.md5.c_str());
691 path.append(".");
692 path.append(ext);
693 if (sk_exists(path.c_str())) {
694 return; // Content-addressed. If it exists already, we're done.
695 }
696 } else {
697 path = SkOSPath::Join(dir, task.sink.tag);
698 sk_mkdir(path.c_str());
699 path = SkOSPath::Join(path.c_str(), task.src.tag);
700 sk_mkdir(path.c_str());
djsollen54416de2015-04-03 07:24:48 -0700701 if (strcmp(task.src.options, "") != 0) {
702 path = SkOSPath::Join(path.c_str(), task.src.options);
703 sk_mkdir(path.c_str());
704 }
mtklein748ca3b2015-01-15 10:56:12 -0800705 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
706 path.append(".");
707 path.append(ext);
708 }
709
mtklein748ca3b2015-01-15 10:56:12 -0800710 if (bitmap) {
mtkleina5114d72015-08-24 13:27:01 -0700711 if (!dump_png(*bitmap, path.c_str(), result.md5.c_str())) {
mtklein748ca3b2015-01-15 10:56:12 -0800712 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
713 return;
714 }
715 } else {
mtkleina5114d72015-08-24 13:27:01 -0700716 SkFILEWStream file(path.c_str());
717 if (!file.isValid()) {
718 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
719 return;
720 }
mtklein748ca3b2015-01-15 10:56:12 -0800721 if (!file.writeStream(data, len)) {
722 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
723 return;
724 }
725 }
726 }
727};
728
729// Run all tasks in the same enclave serially on the same thread.
730// They can't possibly run concurrently with each other.
731static void run_enclave(SkTArray<Task>* tasks) {
732 for (int i = 0; i < tasks->count(); i++) {
733 Task::Run(tasks->begin() + i);
734 }
735}
736
737/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
738
739// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
740
mtklein55e88b22015-01-21 15:50:13 -0800741static SkTDArray<skiatest::Test> gThreadedTests, gGPUTests;
mtklein748ca3b2015-01-15 10:56:12 -0800742
743static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -0800744 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -0800745 return;
746 }
mtklein6393c062015-04-27 08:45:01 -0700747 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r; r = r->next()) {
748 if (!in_shard()) {
749 continue;
750 }
halcanary87f3ba42015-01-20 09:30:20 -0800751 // Despite its name, factory() is returning a reference to
752 // link-time static const POD data.
753 const skiatest::Test& test = r->factory();
754 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -0800755 continue;
756 }
halcanary87f3ba42015-01-20 09:30:20 -0800757 if (test.needsGpu && gpu_supported()) {
mtklein55e88b22015-01-21 15:50:13 -0800758 (FLAGS_gpu_threading ? gThreadedTests : gGPUTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -0800759 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein55e88b22015-01-21 15:50:13 -0800760 gThreadedTests.push(test);
mtklein82d28432015-01-15 12:46:02 -0800761 }
mtklein748ca3b2015-01-15 10:56:12 -0800762 }
763}
764
halcanary87f3ba42015-01-20 09:30:20 -0800765static void run_test(skiatest::Test* test) {
766 struct : public skiatest::Reporter {
mtklein36352bf2015-03-25 18:17:31 -0700767 void reportFailed(const skiatest::Failure& failure) override {
halcanary87f3ba42015-01-20 09:30:20 -0800768 fail(failure.toString());
769 JsonWriter::AddTestFailure(failure);
770 }
mtklein36352bf2015-03-25 18:17:31 -0700771 bool allowExtendedTest() const override {
halcanary87f3ba42015-01-20 09:30:20 -0800772 return FLAGS_pathOpsExtended;
773 }
mtklein36352bf2015-03-25 18:17:31 -0700774 bool verbose() const override { return FLAGS_veryVerbose; }
halcanary87f3ba42015-01-20 09:30:20 -0800775 } reporter;
djsollen824996a2015-06-12 12:06:22 -0700776
777 SkString note;
778 SkString whyBlacklisted = is_blacklisted("_", "tests", "_", test->name);
779 if (!whyBlacklisted.isEmpty()) {
780 note.appendf(" (--blacklist %s)", whyBlacklisted.c_str());
781 }
782
mtklein748ca3b2015-01-15 10:56:12 -0800783 WallTimer timer;
784 timer.start();
djsollen824996a2015-06-12 12:06:22 -0700785 if (!FLAGS_dryRun && whyBlacklisted.isEmpty()) {
djsollen54416de2015-04-03 07:24:48 -0700786 start("unit", "test", "", test->name);
mtklein55e88b22015-01-21 15:50:13 -0800787 GrContextFactory factory;
bsalomon821e10e2015-06-04 14:15:33 -0700788 if (FLAGS_pre_log) {
789 SkDebugf("\nRunning test %s", test->name);
790 }
mtklein55e88b22015-01-21 15:50:13 -0800791 test->proc(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -0800792 }
793 timer.end();
djsollen824996a2015-06-12 12:06:22 -0700794 done(timer.fWall, "unit", "test", "", test->name, note, "");
mtklein748ca3b2015-01-15 10:56:12 -0800795}
796
797/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
798
mtklein55e88b22015-01-21 15:50:13 -0800799// If we're isolating all GPU-bound work to one thread (the default), this function runs all that.
800static void run_enclave_and_gpu_tests(SkTArray<Task>* tasks) {
801 run_enclave(tasks);
802 for (int i = 0; i < gGPUTests.count(); i++) {
803 run_test(&gGPUTests[i]);
804 }
805}
806
mtkleinde6fc2b2015-03-12 06:28:54 -0700807// Some runs (mostly, Valgrind) are so slow that the bot framework thinks we've hung.
808// This prints something every once in a while so that it knows we're still working.
mtklein2e1c47e2015-03-12 07:16:56 -0700809static void start_keepalive() {
810 struct Loop {
811 static void forever(void*) {
812 for (;;) {
813 static const int kSec = 300;
814 #if defined(SK_BUILD_FOR_WIN)
815 Sleep(kSec * 1000);
816 #else
817 sleep(kSec);
818 #endif
mtkleinb37cb412015-03-18 05:27:14 -0700819 SkString running;
820 {
821 SkAutoMutexAcquire lock(gRunningMutex);
822 for (int i = 0; i < gRunning.count(); i++) {
823 running.appendf("\n\t%s", gRunning[i].c_str());
824 }
825 }
826 SkDebugf("\nCurrently running:%s\n", running.c_str());
mtklein2e1c47e2015-03-12 07:16:56 -0700827 }
828 }
829 };
830 static SkThread* intentionallyLeaked = new SkThread(Loop::forever);
831 intentionallyLeaked->start();
mtkleinde6fc2b2015-03-12 06:28:54 -0700832}
833
caryclark83ca6282015-06-10 09:31:09 -0700834#define PORTABLE_FONT_PREFIX "Toy Liberation "
835
836static SkTypeface* create_from_name(const char familyName[], SkTypeface::Style style) {
837 if (familyName && strlen(familyName) > sizeof(PORTABLE_FONT_PREFIX)
838 && !strncmp(familyName, PORTABLE_FONT_PREFIX, sizeof(PORTABLE_FONT_PREFIX) - 1)) {
caryclark1818acb2015-07-24 12:09:25 -0700839 return sk_tool_utils::create_portable_typeface(familyName, style);
caryclark83ca6282015-06-10 09:31:09 -0700840 }
841 return NULL;
842}
843
844#undef PORTABLE_FONT_PREFIX
845
846extern SkTypeface* (*gCreateTypefaceDelegate)(const char [], SkTypeface::Style );
847
jcgregorio3b27ade2014-11-13 08:06:40 -0800848int dm_main();
caryclark17f0b6d2014-07-22 10:15:34 -0700849int dm_main() {
jcgregorio3b27ade2014-11-13 08:06:40 -0800850 SetupCrashHandler();
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +0000851 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -0700852 SkTaskGroup::Enabler enabled(FLAGS_threads);
caryclark83ca6282015-06-10 09:31:09 -0700853 gCreateTypefaceDelegate = &create_from_name;
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +0000854
mtklein2e1c47e2015-03-12 07:16:56 -0700855 start_keepalive();
mtkleinde6fc2b2015-03-12 06:28:54 -0700856
mtklein62bd1a62015-01-27 14:46:26 -0800857 gather_gold();
borenet09ed4802015-04-03 14:15:33 -0700858 gather_uninteresting_hashes();
mtklein62bd1a62015-01-27 14:46:26 -0800859
mtklein748ca3b2015-01-15 10:56:12 -0800860 gather_srcs();
861 gather_sinks();
862 gather_tests();
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +0000863
mtklein55e88b22015-01-21 15:50:13 -0800864 gPending = gSrcs.count() * gSinks.count() + gThreadedTests.count() + gGPUTests.count();
mtklein748ca3b2015-01-15 10:56:12 -0800865 SkDebugf("%d srcs * %d sinks + %d tests == %d tasks\n",
mtklein55e88b22015-01-21 15:50:13 -0800866 gSrcs.count(), gSinks.count(), gThreadedTests.count() + gGPUTests.count(), gPending);
mtklein748ca3b2015-01-15 10:56:12 -0800867
868 // We try to exploit as much parallelism as is safe. Most Src/Sink pairs run on any thread,
869 // but Sinks that identify as part of a particular enclave run serially on a single thread.
mtklein82d28432015-01-15 12:46:02 -0800870 // CPU tests run on any thread. GPU tests depend on --gpu_threading.
mtklein748ca3b2015-01-15 10:56:12 -0800871 SkTArray<Task> enclaves[kNumEnclaves];
872 for (int j = 0; j < gSinks.count(); j++) {
873 SkTArray<Task>& tasks = enclaves[gSinks[j]->enclave()];
874 for (int i = 0; i < gSrcs.count(); i++) {
875 tasks.push_back(Task(gSrcs[i], gSinks[j]));
876 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +0000877 }
878
mtklein748ca3b2015-01-15 10:56:12 -0800879 SkTaskGroup tg;
mtklein55e88b22015-01-21 15:50:13 -0800880 tg.batch(run_test, gThreadedTests.begin(), gThreadedTests.count());
881 for (int i = 0; i < kNumEnclaves; i++) {
882 switch(i) {
883 case kAnyThread_Enclave:
884 tg.batch(Task::Run, enclaves[i].begin(), enclaves[i].count());
885 break;
886 case kGPU_Enclave:
887 tg.add(run_enclave_and_gpu_tests, &enclaves[i]);
888 break;
889 default:
890 tg.add(run_enclave, &enclaves[i]);
891 break;
mtklein82d28432015-01-15 12:46:02 -0800892 }
mtklein55e88b22015-01-21 15:50:13 -0800893 }
mtklein748ca3b2015-01-15 10:56:12 -0800894 tg.wait();
mtklein748ca3b2015-01-15 10:56:12 -0800895 // At this point we're back in single-threaded land.
caryclarkf53ce802015-06-15 06:48:30 -0700896 sk_tool_utils::release_portable_typefaces();
mtklein@google.comd36522d2013-10-16 13:02:15 +0000897
mtklein2f64eec2015-01-15 14:20:41 -0800898 SkDebugf("\n");
mtklein748ca3b2015-01-15 10:56:12 -0800899 if (gFailures.count() > 0) {
900 SkDebugf("Failures:\n");
901 for (int i = 0; i < gFailures.count(); i++) {
mtklein9dc09102015-01-15 15:47:33 -0800902 SkDebugf("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800903 }
904 SkDebugf("%d failures\n", gFailures.count());
905 return 1;
mtklein114c3cd2015-01-15 10:15:02 -0800906 }
mtklein748ca3b2015-01-15 10:56:12 -0800907 if (gPending > 0) {
908 SkDebugf("Hrm, we didn't seem to run everything we intended to! Please file a bug.\n");
909 return 1;
mtklein114c3cd2015-01-15 10:15:02 -0800910 }
mtklein748ca3b2015-01-15 10:56:12 -0800911 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +0000912}
jcgregorio3b27ade2014-11-13 08:06:40 -0800913
borenet48087572015-04-02 12:16:36 -0700914#if !defined(SK_BUILD_FOR_IOS)
jcgregorio3b27ade2014-11-13 08:06:40 -0800915int main(int argc, char** argv) {
916 SkCommandLineFlags::Parse(argc, argv);
917 return dm_main();
918}
919#endif