jcgregorio | 3b27ade | 2014-11-13 08:06:40 -0800 | [diff] [blame] | 1 | #include "CrashHandler.h" |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 2 | #include "DMJsonWriter.h" |
| 3 | #include "DMSrcSink.h" |
| 4 | #include "OverwriteLine.h" |
| 5 | #include "ProcStats.h" |
| 6 | #include "SkBBHFactory.h" |
mtklein | 62bd1a6 | 2015-01-27 14:46:26 -0800 | [diff] [blame^] | 7 | #include "SkChecksum.h" |
caryclark | 17f0b6d | 2014-07-22 10:15:34 -0700 | [diff] [blame] | 8 | #include "SkCommonFlags.h" |
mtklein@google.com | d36522d | 2013-10-16 13:02:15 +0000 | [diff] [blame] | 9 | #include "SkForceLinking.h" |
| 10 | #include "SkGraphics.h" |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 11 | #include "SkMD5.h" |
mtklein | 1d0f164 | 2014-09-08 08:05:18 -0700 | [diff] [blame] | 12 | #include "SkOSFile.h" |
mtklein | 62bd1a6 | 2015-01-27 14:46:26 -0800 | [diff] [blame^] | 13 | #include "SkTDynamicHash.h" |
mtklein | 406654b | 2014-09-03 15:34:37 -0700 | [diff] [blame] | 14 | #include "SkTaskGroup.h" |
commit-bot@chromium.org | 0dc5bd1 | 2014-02-26 16:31:22 +0000 | [diff] [blame] | 15 | #include "Test.h" |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 16 | #include "Timer.h" |
mtklein@google.com | d36522d | 2013-10-16 13:02:15 +0000 | [diff] [blame] | 17 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 18 | DEFINE_string(images, "resources", "Images to decode."); |
mtklein | 6c5fed2 | 2015-01-20 13:47:23 -0800 | [diff] [blame] | 19 | DEFINE_string(src, "tests gm skp image", "Source types to test."); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 20 | DEFINE_bool(nameByHash, false, |
| 21 | "If true, write to FLAGS_writePath[0]/<hash>.png instead of " |
| 22 | "to FLAGS_writePath[0]/<config>/<sourceType>/<name>.png"); |
| 23 | DEFINE_bool2(pathOpsExtended, x, false, "Run extended pathOps tests."); |
| 24 | DEFINE_string(matrix, "1 0 0 0 1 0 0 0 1", |
| 25 | "Matrix to apply when using 'matrix' in config."); |
mtklein | 82d2843 | 2015-01-15 12:46:02 -0800 | [diff] [blame] | 26 | DEFINE_bool(gpu_threading, false, "Allow GPU work to run on multiple threads?"); |
mtklein@google.com | d36522d | 2013-10-16 13:02:15 +0000 | [diff] [blame] | 27 | |
mtklein | a2ef642 | 2015-01-15 13:44:22 -0800 | [diff] [blame] | 28 | DEFINE_string(blacklist, "", |
| 29 | "Space-separated config/src/name triples to blacklist. '_' matches anything. E.g. \n" |
| 30 | "'--blacklist gpu skp _' will blacklist all SKPs drawn into the gpu config.\n" |
| 31 | "'--blacklist gpu skp _ 8888 gm aarects' will also blacklist the aarects GM on 8888."); |
| 32 | |
mtklein | 62bd1a6 | 2015-01-27 14:46:26 -0800 | [diff] [blame^] | 33 | DEFINE_string2(readPath, r, "", "If set check for equality with golden results in this directory."); |
| 34 | |
mtklein@google.com | d36522d | 2013-10-16 13:02:15 +0000 | [diff] [blame] | 35 | __SK_FORCE_IMAGE_DECODER_LINKING; |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 36 | using namespace DM; |
mtklein@google.com | d36522d | 2013-10-16 13:02:15 +0000 | [diff] [blame] | 37 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 38 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 39 | |
| 40 | SK_DECLARE_STATIC_MUTEX(gFailuresMutex); |
| 41 | static SkTArray<SkString> gFailures; |
| 42 | |
| 43 | static void fail(ImplicitString err) { |
| 44 | SkAutoMutexAcquire lock(gFailuresMutex); |
| 45 | SkDebugf("\n\nFAILURE: %s\n\n", err.c_str()); |
| 46 | gFailures.push_back(err); |
halcanary | 9e398f7 | 2015-01-10 11:18:04 -0800 | [diff] [blame] | 47 | } |
| 48 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 49 | static int32_t gPending = 0; // Atomic. |
| 50 | |
| 51 | static void done(double ms, ImplicitString config, ImplicitString src, ImplicitString name) { |
mtklein | a17241b | 2015-01-23 05:48:00 -0800 | [diff] [blame] | 52 | int32_t pending = sk_atomic_dec(&gPending)-1; |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 53 | SkDebugf("%s(%4dMB %5d) %s\t%s %s %s ", FLAGS_verbose ? "\n" : kSkOverwriteLine |
| 54 | , sk_tools::getMaxResidentSetSizeMB() |
mtklein | a17241b | 2015-01-23 05:48:00 -0800 | [diff] [blame] | 55 | , pending |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 56 | , HumanizeMs(ms).c_str() |
| 57 | , config.c_str() |
| 58 | , src.c_str() |
| 59 | , name.c_str()); |
mtklein | a17241b | 2015-01-23 05:48:00 -0800 | [diff] [blame] | 60 | // We write our dm.json file every once in a while in case we crash. |
| 61 | // Notice this also handles the final dm.json when pending == 0. |
| 62 | if (pending % 500 == 0) { |
| 63 | JsonWriter::DumpJson(); |
| 64 | } |
mtklein@google.com | d36522d | 2013-10-16 13:02:15 +0000 | [diff] [blame] | 65 | } |
| 66 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 67 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
mtklein | 114c3cd | 2015-01-15 10:15:02 -0800 | [diff] [blame] | 68 | |
mtklein | 62bd1a6 | 2015-01-27 14:46:26 -0800 | [diff] [blame^] | 69 | struct Gold : public SkString { |
| 70 | Gold(ImplicitString sink, ImplicitString src, ImplicitString name, ImplicitString md5) |
| 71 | : SkString("") { |
| 72 | this->append(sink); |
| 73 | this->append(src); |
| 74 | this->append(name); |
| 75 | this->append(md5); |
| 76 | while (this->size() % 4) { |
| 77 | this->append("!"); // Pad out if needed so we can pass this to Murmur3. |
| 78 | } |
| 79 | } |
| 80 | static const Gold& GetKey(const Gold& g) { return g; } |
| 81 | static uint32_t Hash(const Gold& g) { |
| 82 | return SkChecksum::Murmur3((const uint32_t*)g.c_str(), g.size()); |
| 83 | } |
| 84 | }; |
| 85 | static SkTDynamicHash<Gold, Gold> gGold; |
| 86 | |
| 87 | static void add_gold(JsonWriter::BitmapResult r) { |
| 88 | gGold.add(new Gold(r.config, r.sourceType, r.name, r.md5)); // We'll let these leak. Lazybones. |
| 89 | } |
| 90 | |
| 91 | static void gather_gold() { |
| 92 | if (!FLAGS_readPath.isEmpty()) { |
| 93 | SkString path(FLAGS_readPath[0]); |
| 94 | path.append("/dm.json"); |
| 95 | if (!JsonWriter::ReadJson(path.c_str(), add_gold)) { |
| 96 | fail(SkStringPrintf("Couldn't read %s for golden results.", path.c_str())); |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 102 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 103 | template <typename T> |
| 104 | struct Tagged : public SkAutoTDelete<T> { const char* tag; }; |
| 105 | |
| 106 | static const bool kMemcpyOK = true; |
| 107 | |
| 108 | static SkTArray<Tagged<Src>, kMemcpyOK> gSrcs; |
| 109 | static SkTArray<Tagged<Sink>, kMemcpyOK> gSinks; |
| 110 | |
| 111 | static void push_src(const char* tag, Src* s) { |
| 112 | SkAutoTDelete<Src> src(s); |
| 113 | if (FLAGS_src.contains(tag) && |
| 114 | !SkCommandLineFlags::ShouldSkip(FLAGS_match, src->name().c_str())) { |
| 115 | Tagged<Src>& s = gSrcs.push_back(); |
| 116 | s.reset(src.detach()); |
| 117 | s.tag = tag; |
mtklein | 114c3cd | 2015-01-15 10:15:02 -0800 | [diff] [blame] | 118 | } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 119 | } |
mtklein | 114c3cd | 2015-01-15 10:15:02 -0800 | [diff] [blame] | 120 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 121 | static void gather_srcs() { |
| 122 | for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) { |
| 123 | push_src("gm", new GMSrc(r->factory())); |
| 124 | } |
| 125 | if (!FLAGS_skps.isEmpty()) { |
| 126 | SkOSFile::Iter it(FLAGS_skps[0], "skp"); |
| 127 | for (SkString file; it.next(&file); ) { |
| 128 | push_src("skp", new SKPSrc(SkOSPath::Join(FLAGS_skps[0], file.c_str()))); |
mtklein | 114c3cd | 2015-01-15 10:15:02 -0800 | [diff] [blame] | 129 | } |
| 130 | } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 131 | if (!FLAGS_images.isEmpty()) { |
| 132 | const char* exts[] = { |
| 133 | "bmp", "gif", "jpg", "jpeg", "png", "webp", "ktx", "astc", "wbmp", "ico", |
| 134 | "BMP", "GIF", "JPG", "JPEG", "PNG", "WEBP", "KTX", "ASTC", "WBMP", "ICO", |
| 135 | }; |
| 136 | for (size_t i = 0; i < SK_ARRAY_COUNT(exts); i++) { |
| 137 | SkOSFile::Iter it(FLAGS_images[0], exts[i]); |
| 138 | for (SkString file; it.next(&file); ) { |
| 139 | SkString path = SkOSPath::Join(FLAGS_images[0], file.c_str()); |
mtklein | 9264a95 | 2015-01-20 10:11:53 -0800 | [diff] [blame] | 140 | push_src("image", new ImageSrc(path)); // Decode entire image. |
| 141 | push_src("image", new ImageSrc(path, 5)); // Decode 5 random subsets. |
mtklein | 114c3cd | 2015-01-15 10:15:02 -0800 | [diff] [blame] | 142 | } |
mtklein | 709d2c3 | 2015-01-15 08:30:25 -0800 | [diff] [blame] | 143 | } |
mtklein | 709d2c3 | 2015-01-15 08:30:25 -0800 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 147 | static GrGLStandard get_gpu_api() { |
| 148 | if (FLAGS_gpuAPI.contains("gl")) { return kGL_GrGLStandard; } |
| 149 | if (FLAGS_gpuAPI.contains("gles")) { return kGLES_GrGLStandard; } |
| 150 | return kNone_GrGLStandard; |
mtklein | 709d2c3 | 2015-01-15 08:30:25 -0800 | [diff] [blame] | 151 | } |
| 152 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 153 | static void push_sink(const char* tag, Sink* s) { |
| 154 | SkAutoTDelete<Sink> sink(s); |
| 155 | if (!FLAGS_config.contains(tag)) { |
| 156 | return; |
mtklein | 114c3cd | 2015-01-15 10:15:02 -0800 | [diff] [blame] | 157 | } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 158 | // Try a noop Src as a canary. If it fails, skip this sink. |
| 159 | struct : public Src { |
| 160 | Error draw(SkCanvas*) const SK_OVERRIDE { return ""; } |
| 161 | SkISize size() const SK_OVERRIDE { return SkISize::Make(16, 16); } |
| 162 | Name name() const SK_OVERRIDE { return "noop"; } |
| 163 | } noop; |
mtklein | 114c3cd | 2015-01-15 10:15:02 -0800 | [diff] [blame] | 164 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 165 | SkBitmap bitmap; |
| 166 | SkDynamicMemoryWStream stream; |
| 167 | Error err = sink->draw(noop, &bitmap, &stream); |
| 168 | if (!err.isEmpty()) { |
| 169 | SkDebugf("Skipping %s: %s\n", tag, err.c_str()); |
mtklein | 114c3cd | 2015-01-15 10:15:02 -0800 | [diff] [blame] | 170 | return; |
| 171 | } |
| 172 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 173 | Tagged<Sink>& ts = gSinks.push_back(); |
| 174 | ts.reset(sink.detach()); |
| 175 | ts.tag = tag; |
| 176 | } |
| 177 | |
| 178 | static bool gpu_supported() { |
| 179 | #if SK_SUPPORT_GPU |
| 180 | return FLAGS_gpu; |
| 181 | #else |
| 182 | return false; |
| 183 | #endif |
| 184 | } |
| 185 | |
| 186 | static Sink* create_sink(const char* tag) { |
| 187 | #define SINK(t, sink, ...) if (0 == strcmp(t, tag)) { return new sink(__VA_ARGS__); } |
| 188 | if (gpu_supported()) { |
mtklein | 82d2843 | 2015-01-15 12:46:02 -0800 | [diff] [blame] | 189 | typedef GrContextFactory Gr; |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 190 | const GrGLStandard api = get_gpu_api(); |
mtklein | 82d2843 | 2015-01-15 12:46:02 -0800 | [diff] [blame] | 191 | SINK("gpunull", GPUSink, Gr::kNull_GLContextType, api, 0, false, FLAGS_gpu_threading); |
| 192 | SINK("gpudebug", GPUSink, Gr::kDebug_GLContextType, api, 0, false, FLAGS_gpu_threading); |
| 193 | SINK("gpu", GPUSink, Gr::kNative_GLContextType, api, 0, false, FLAGS_gpu_threading); |
| 194 | SINK("gpudft", GPUSink, Gr::kNative_GLContextType, api, 0, true, FLAGS_gpu_threading); |
| 195 | SINK("msaa4", GPUSink, Gr::kNative_GLContextType, api, 4, false, FLAGS_gpu_threading); |
| 196 | SINK("msaa16", GPUSink, Gr::kNative_GLContextType, api, 16, false, FLAGS_gpu_threading); |
| 197 | SINK("nvprmsaa4", GPUSink, Gr::kNVPR_GLContextType, api, 4, false, FLAGS_gpu_threading); |
| 198 | SINK("nvprmsaa16", GPUSink, Gr::kNVPR_GLContextType, api, 16, false, FLAGS_gpu_threading); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 199 | #if SK_ANGLE |
mtklein | 82d2843 | 2015-01-15 12:46:02 -0800 | [diff] [blame] | 200 | SINK("angle", GPUSink, Gr::kANGLE_GLContextType, api, 0, false, FLAGS_gpu_threading); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 201 | #endif |
| 202 | #if SK_MESA |
mtklein | 82d2843 | 2015-01-15 12:46:02 -0800 | [diff] [blame] | 203 | SINK("mesa", GPUSink, Gr::kMESA_GLContextType, api, 0, false, FLAGS_gpu_threading); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 204 | #endif |
mtklein | 114c3cd | 2015-01-15 10:15:02 -0800 | [diff] [blame] | 205 | } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 206 | |
| 207 | if (FLAGS_cpu) { |
| 208 | SINK("565", RasterSink, kRGB_565_SkColorType); |
| 209 | SINK("8888", RasterSink, kN32_SkColorType); |
mtklein | 19f3060 | 2015-01-20 13:34:39 -0800 | [diff] [blame] | 210 | SINK("pdf", PDFSink); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 211 | } |
| 212 | #undef SINK |
| 213 | return NULL; |
mtklein | 114c3cd | 2015-01-15 10:15:02 -0800 | [diff] [blame] | 214 | } |
| 215 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 216 | static Sink* create_via(const char* tag, Sink* wrapped) { |
| 217 | #define VIA(t, via, ...) if (0 == strcmp(t, tag)) { return new via(__VA_ARGS__); } |
mtklein | 7edca21 | 2015-01-21 13:18:51 -0800 | [diff] [blame] | 218 | VIA("pipe", ViaPipe, wrapped); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 219 | VIA("serialize", ViaSerialization, wrapped); |
mtklein | 7edca21 | 2015-01-21 13:18:51 -0800 | [diff] [blame] | 220 | VIA("tiles", ViaTiles, 256, 256, NULL, wrapped); |
| 221 | VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 222 | |
| 223 | if (FLAGS_matrix.count() == 9) { |
| 224 | SkMatrix m; |
| 225 | for (int i = 0; i < 9; i++) { |
| 226 | m[i] = (SkScalar)atof(FLAGS_matrix[i]); |
| 227 | } |
| 228 | VIA("matrix", ViaMatrix, m, wrapped); |
| 229 | } |
| 230 | #undef VIA |
| 231 | return NULL; |
mtklein | 114c3cd | 2015-01-15 10:15:02 -0800 | [diff] [blame] | 232 | } |
| 233 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 234 | static void gather_sinks() { |
| 235 | for (int i = 0; i < FLAGS_config.count(); i++) { |
| 236 | const char* config = FLAGS_config[i]; |
| 237 | SkTArray<SkString> parts; |
| 238 | SkStrSplit(config, "-", &parts); |
| 239 | |
| 240 | Sink* sink = NULL; |
| 241 | for (int i = parts.count(); i-- > 0;) { |
| 242 | const char* part = parts[i].c_str(); |
| 243 | Sink* next = (sink == NULL) ? create_sink(part) : create_via(part, sink); |
| 244 | if (next == NULL) { |
| 245 | SkDebugf("Skipping %s: Don't understand '%s'.\n", config, part); |
| 246 | delete sink; |
| 247 | sink = NULL; |
| 248 | break; |
| 249 | } |
| 250 | sink = next; |
| 251 | } |
| 252 | if (sink) { |
| 253 | push_sink(config, sink); |
mtklein | 114c3cd | 2015-01-15 10:15:02 -0800 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | } |
mtklein | 709d2c3 | 2015-01-15 08:30:25 -0800 | [diff] [blame] | 257 | |
mtklein | a2ef642 | 2015-01-15 13:44:22 -0800 | [diff] [blame] | 258 | static bool match(const char* needle, const char* haystack) { |
| 259 | return 0 == strcmp("_", needle) || NULL != strstr(haystack, needle); |
| 260 | } |
| 261 | |
| 262 | static ImplicitString is_blacklisted(const char* sink, const char* src, const char* name) { |
| 263 | for (int i = 0; i < FLAGS_blacklist.count() - 2; i += 3) { |
| 264 | if (match(FLAGS_blacklist[i+0], sink) && |
| 265 | match(FLAGS_blacklist[i+1], src) && |
| 266 | match(FLAGS_blacklist[i+2], name)) { |
| 267 | return SkStringPrintf("%s %s %s", |
| 268 | FLAGS_blacklist[i+0], FLAGS_blacklist[i+1], FLAGS_blacklist[i+2]); |
| 269 | } |
| 270 | } |
| 271 | return ""; |
| 272 | } |
| 273 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 274 | // The finest-grained unit of work we can run: draw a single Src into a single Sink, |
| 275 | // report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream. |
| 276 | struct Task { |
| 277 | Task(const Tagged<Src>& src, const Tagged<Sink>& sink) : src(src), sink(sink) {} |
| 278 | const Tagged<Src>& src; |
| 279 | const Tagged<Sink>& sink; |
| 280 | |
| 281 | static void Run(Task* task) { |
mtklein | a2ef642 | 2015-01-15 13:44:22 -0800 | [diff] [blame] | 282 | SkString name = task->src->name(); |
| 283 | SkString whyBlacklisted = is_blacklisted(task->sink.tag, task->src.tag, name.c_str()); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 284 | WallTimer timer; |
| 285 | timer.start(); |
mtklein | a2ef642 | 2015-01-15 13:44:22 -0800 | [diff] [blame] | 286 | if (!FLAGS_dryRun && whyBlacklisted.isEmpty()) { |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 287 | SkBitmap bitmap; |
| 288 | SkDynamicMemoryWStream stream; |
| 289 | Error err = task->sink->draw(*task->src, &bitmap, &stream); |
| 290 | if (!err.isEmpty()) { |
| 291 | fail(SkStringPrintf("%s %s %s: %s", |
| 292 | task->sink.tag, |
| 293 | task->src.tag, |
mtklein | a2ef642 | 2015-01-15 13:44:22 -0800 | [diff] [blame] | 294 | name.c_str(), |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 295 | err.c_str())); |
| 296 | } |
mtklein | 62bd1a6 | 2015-01-27 14:46:26 -0800 | [diff] [blame^] | 297 | SkAutoTDelete<SkStreamAsset> data(stream.detachAsStream()); |
| 298 | |
| 299 | SkString md5; |
| 300 | if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) { |
| 301 | SkMD5 hash; |
| 302 | if (data->getLength()) { |
| 303 | hash.writeStream(data, data->getLength()); |
| 304 | data->rewind(); |
| 305 | } else { |
| 306 | hash.write(bitmap.getPixels(), bitmap.getSize()); |
| 307 | } |
| 308 | SkMD5::Digest digest; |
| 309 | hash.finish(digest); |
| 310 | for (int i = 0; i < 16; i++) { |
| 311 | md5.appendf("%02x", digest.data[i]); |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | if (!FLAGS_readPath.isEmpty() && |
| 316 | !gGold.find(Gold(task->sink.tag, task->src.tag, name, md5))) { |
| 317 | fail(SkStringPrintf("%s not found for %s %s %s in %s", |
| 318 | md5.c_str(), |
| 319 | task->sink.tag, |
| 320 | task->src.tag, |
| 321 | name.c_str(), |
| 322 | FLAGS_readPath[0])); |
| 323 | } |
| 324 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 325 | if (!FLAGS_writePath.isEmpty()) { |
| 326 | const char* ext = task->sink->fileExtension(); |
mtklein | 62bd1a6 | 2015-01-27 14:46:26 -0800 | [diff] [blame^] | 327 | if (data->getLength()) { |
| 328 | WriteToDisk(*task, md5, ext, data, data->getLength(), NULL); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 329 | } else { |
mtklein | 62bd1a6 | 2015-01-27 14:46:26 -0800 | [diff] [blame^] | 330 | WriteToDisk(*task, md5, ext, NULL, 0, &bitmap); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 331 | } |
| 332 | } |
| 333 | } |
| 334 | timer.end(); |
mtklein | a2ef642 | 2015-01-15 13:44:22 -0800 | [diff] [blame] | 335 | if (!whyBlacklisted.isEmpty()) { |
| 336 | name.appendf(" (--blacklist, %s)", whyBlacklisted.c_str()); |
| 337 | } |
| 338 | done(timer.fWall, task->sink.tag, task->src.tag, name); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | static void WriteToDisk(const Task& task, |
mtklein | 62bd1a6 | 2015-01-27 14:46:26 -0800 | [diff] [blame^] | 342 | SkString md5, |
| 343 | const char* ext, |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 344 | SkStream* data, size_t len, |
mtklein | 62bd1a6 | 2015-01-27 14:46:26 -0800 | [diff] [blame^] | 345 | const SkBitmap* bitmap) { |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 346 | JsonWriter::BitmapResult result; |
| 347 | result.name = task.src->name(); |
| 348 | result.config = task.sink.tag; |
| 349 | result.sourceType = task.src.tag; |
| 350 | result.ext = ext; |
mtklein | 62bd1a6 | 2015-01-27 14:46:26 -0800 | [diff] [blame^] | 351 | result.md5 = md5; |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 352 | JsonWriter::AddBitmapResult(result); |
| 353 | |
| 354 | const char* dir = FLAGS_writePath[0]; |
| 355 | if (0 == strcmp(dir, "@")) { // Needed for iOS. |
| 356 | dir = FLAGS_resourcePath[0]; |
| 357 | } |
| 358 | sk_mkdir(dir); |
| 359 | |
| 360 | SkString path; |
| 361 | if (FLAGS_nameByHash) { |
| 362 | path = SkOSPath::Join(dir, result.md5.c_str()); |
| 363 | path.append("."); |
| 364 | path.append(ext); |
| 365 | if (sk_exists(path.c_str())) { |
| 366 | return; // Content-addressed. If it exists already, we're done. |
| 367 | } |
| 368 | } else { |
| 369 | path = SkOSPath::Join(dir, task.sink.tag); |
| 370 | sk_mkdir(path.c_str()); |
| 371 | path = SkOSPath::Join(path.c_str(), task.src.tag); |
| 372 | sk_mkdir(path.c_str()); |
| 373 | path = SkOSPath::Join(path.c_str(), task.src->name().c_str()); |
| 374 | path.append("."); |
| 375 | path.append(ext); |
| 376 | } |
| 377 | |
| 378 | SkFILEWStream file(path.c_str()); |
| 379 | if (!file.isValid()) { |
| 380 | fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str())); |
| 381 | return; |
| 382 | } |
| 383 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 384 | if (bitmap) { |
| 385 | // We can't encode A8 bitmaps as PNGs. Convert them to 8888 first. |
| 386 | SkBitmap converted; |
| 387 | if (bitmap->info().colorType() == kAlpha_8_SkColorType) { |
| 388 | if (!bitmap->copyTo(&converted, kN32_SkColorType)) { |
| 389 | fail("Can't convert A8 to 8888.\n"); |
| 390 | return; |
| 391 | } |
| 392 | bitmap = &converted; |
| 393 | } |
| 394 | if (!SkImageEncoder::EncodeStream(&file, *bitmap, SkImageEncoder::kPNG_Type, 100)) { |
| 395 | fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str())); |
| 396 | return; |
| 397 | } |
| 398 | } else { |
| 399 | if (!file.writeStream(data, len)) { |
| 400 | fail(SkStringPrintf("Can't write to %s.\n", path.c_str())); |
| 401 | return; |
| 402 | } |
| 403 | } |
| 404 | } |
| 405 | }; |
| 406 | |
| 407 | // Run all tasks in the same enclave serially on the same thread. |
| 408 | // They can't possibly run concurrently with each other. |
| 409 | static void run_enclave(SkTArray<Task>* tasks) { |
| 410 | for (int i = 0; i < tasks->count(); i++) { |
| 411 | Task::Run(tasks->begin() + i); |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 416 | |
| 417 | // Unit tests don't fit so well into the Src/Sink model, so we give them special treatment. |
| 418 | |
mtklein | 55e88b2 | 2015-01-21 15:50:13 -0800 | [diff] [blame] | 419 | static SkTDArray<skiatest::Test> gThreadedTests, gGPUTests; |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 420 | |
| 421 | static void gather_tests() { |
mtklein | 6c5fed2 | 2015-01-20 13:47:23 -0800 | [diff] [blame] | 422 | if (!FLAGS_src.contains("tests")) { |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 423 | return; |
| 424 | } |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 425 | for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r; |
| 426 | r = r->next()) { |
| 427 | // Despite its name, factory() is returning a reference to |
| 428 | // link-time static const POD data. |
| 429 | const skiatest::Test& test = r->factory(); |
| 430 | if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) { |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 431 | continue; |
| 432 | } |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 433 | if (test.needsGpu && gpu_supported()) { |
mtklein | 55e88b2 | 2015-01-21 15:50:13 -0800 | [diff] [blame] | 434 | (FLAGS_gpu_threading ? gThreadedTests : gGPUTests).push(test); |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 435 | } else if (!test.needsGpu && FLAGS_cpu) { |
mtklein | 55e88b2 | 2015-01-21 15:50:13 -0800 | [diff] [blame] | 436 | gThreadedTests.push(test); |
mtklein | 82d2843 | 2015-01-15 12:46:02 -0800 | [diff] [blame] | 437 | } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 438 | } |
| 439 | } |
| 440 | |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 441 | static void run_test(skiatest::Test* test) { |
| 442 | struct : public skiatest::Reporter { |
| 443 | void reportFailed(const skiatest::Failure& failure) SK_OVERRIDE { |
| 444 | fail(failure.toString()); |
| 445 | JsonWriter::AddTestFailure(failure); |
| 446 | } |
| 447 | bool allowExtendedTest() const SK_OVERRIDE { |
| 448 | return FLAGS_pathOpsExtended; |
| 449 | } |
| 450 | bool verbose() const SK_OVERRIDE { return FLAGS_veryVerbose; } |
| 451 | } reporter; |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 452 | WallTimer timer; |
| 453 | timer.start(); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 454 | if (!FLAGS_dryRun) { |
mtklein | 55e88b2 | 2015-01-21 15:50:13 -0800 | [diff] [blame] | 455 | GrContextFactory factory; |
| 456 | test->proc(&reporter, &factory); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 457 | } |
| 458 | timer.end(); |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 459 | done(timer.fWall, "unit", "test", test->name); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 463 | |
mtklein | 55e88b2 | 2015-01-21 15:50:13 -0800 | [diff] [blame] | 464 | // If we're isolating all GPU-bound work to one thread (the default), this function runs all that. |
| 465 | static void run_enclave_and_gpu_tests(SkTArray<Task>* tasks) { |
| 466 | run_enclave(tasks); |
| 467 | for (int i = 0; i < gGPUTests.count(); i++) { |
| 468 | run_test(&gGPUTests[i]); |
| 469 | } |
| 470 | } |
| 471 | |
jcgregorio | 3b27ade | 2014-11-13 08:06:40 -0800 | [diff] [blame] | 472 | int dm_main(); |
caryclark | 17f0b6d | 2014-07-22 10:15:34 -0700 | [diff] [blame] | 473 | int dm_main() { |
jcgregorio | 3b27ade | 2014-11-13 08:06:40 -0800 | [diff] [blame] | 474 | SetupCrashHandler(); |
commit-bot@chromium.org | 39e8d93 | 2014-05-29 20:14:48 +0000 | [diff] [blame] | 475 | SkAutoGraphics ag; |
mtklein | 406654b | 2014-09-03 15:34:37 -0700 | [diff] [blame] | 476 | SkTaskGroup::Enabler enabled(FLAGS_threads); |
commit-bot@chromium.org | a65e2fd | 2014-05-30 17:23:31 +0000 | [diff] [blame] | 477 | |
mtklein | 62bd1a6 | 2015-01-27 14:46:26 -0800 | [diff] [blame^] | 478 | gather_gold(); |
| 479 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 480 | gather_srcs(); |
| 481 | gather_sinks(); |
| 482 | gather_tests(); |
commit-bot@chromium.org | 0dc5bd1 | 2014-02-26 16:31:22 +0000 | [diff] [blame] | 483 | |
mtklein | 55e88b2 | 2015-01-21 15:50:13 -0800 | [diff] [blame] | 484 | gPending = gSrcs.count() * gSinks.count() + gThreadedTests.count() + gGPUTests.count(); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 485 | SkDebugf("%d srcs * %d sinks + %d tests == %d tasks\n", |
mtklein | 55e88b2 | 2015-01-21 15:50:13 -0800 | [diff] [blame] | 486 | gSrcs.count(), gSinks.count(), gThreadedTests.count() + gGPUTests.count(), gPending); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 487 | |
| 488 | // We try to exploit as much parallelism as is safe. Most Src/Sink pairs run on any thread, |
| 489 | // but Sinks that identify as part of a particular enclave run serially on a single thread. |
mtklein | 82d2843 | 2015-01-15 12:46:02 -0800 | [diff] [blame] | 490 | // CPU tests run on any thread. GPU tests depend on --gpu_threading. |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 491 | SkTArray<Task> enclaves[kNumEnclaves]; |
| 492 | for (int j = 0; j < gSinks.count(); j++) { |
| 493 | SkTArray<Task>& tasks = enclaves[gSinks[j]->enclave()]; |
| 494 | for (int i = 0; i < gSrcs.count(); i++) { |
| 495 | tasks.push_back(Task(gSrcs[i], gSinks[j])); |
| 496 | } |
commit-bot@chromium.org | 38aeb0f | 2014-02-26 23:01:57 +0000 | [diff] [blame] | 497 | } |
| 498 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 499 | SkTaskGroup tg; |
mtklein | 55e88b2 | 2015-01-21 15:50:13 -0800 | [diff] [blame] | 500 | tg.batch(run_test, gThreadedTests.begin(), gThreadedTests.count()); |
| 501 | for (int i = 0; i < kNumEnclaves; i++) { |
| 502 | switch(i) { |
| 503 | case kAnyThread_Enclave: |
| 504 | tg.batch(Task::Run, enclaves[i].begin(), enclaves[i].count()); |
| 505 | break; |
| 506 | case kGPU_Enclave: |
| 507 | tg.add(run_enclave_and_gpu_tests, &enclaves[i]); |
| 508 | break; |
| 509 | default: |
| 510 | tg.add(run_enclave, &enclaves[i]); |
| 511 | break; |
mtklein | 82d2843 | 2015-01-15 12:46:02 -0800 | [diff] [blame] | 512 | } |
mtklein | 55e88b2 | 2015-01-21 15:50:13 -0800 | [diff] [blame] | 513 | } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 514 | tg.wait(); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 515 | // At this point we're back in single-threaded land. |
mtklein@google.com | d36522d | 2013-10-16 13:02:15 +0000 | [diff] [blame] | 516 | |
mtklein | 2f64eec | 2015-01-15 14:20:41 -0800 | [diff] [blame] | 517 | SkDebugf("\n"); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 518 | if (gFailures.count() > 0) { |
| 519 | SkDebugf("Failures:\n"); |
| 520 | for (int i = 0; i < gFailures.count(); i++) { |
mtklein | 9dc0910 | 2015-01-15 15:47:33 -0800 | [diff] [blame] | 521 | SkDebugf("\t%s\n", gFailures[i].c_str()); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 522 | } |
| 523 | SkDebugf("%d failures\n", gFailures.count()); |
| 524 | return 1; |
mtklein | 114c3cd | 2015-01-15 10:15:02 -0800 | [diff] [blame] | 525 | } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 526 | if (gPending > 0) { |
| 527 | SkDebugf("Hrm, we didn't seem to run everything we intended to! Please file a bug.\n"); |
| 528 | return 1; |
mtklein | 114c3cd | 2015-01-15 10:15:02 -0800 | [diff] [blame] | 529 | } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 530 | return 0; |
mtklein@google.com | d36522d | 2013-10-16 13:02:15 +0000 | [diff] [blame] | 531 | } |
jcgregorio | 3b27ade | 2014-11-13 08:06:40 -0800 | [diff] [blame] | 532 | |
| 533 | #if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL) |
| 534 | int main(int argc, char** argv) { |
| 535 | SkCommandLineFlags::Parse(argc, argv); |
| 536 | return dm_main(); |
| 537 | } |
| 538 | #endif |