blob: 06ad765f3d9a240e3ab55245f872f7805017874d [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
mtklein748ca3b2015-01-15 10:56:12 -08008#include "DMJsonWriter.h"
9#include "DMSrcSink.h"
mtklein748ca3b2015-01-15 10:56:12 -080010#include "ProcStats.h"
halcanary7d571242016-02-24 17:59:16 -080011#include "Resources.h"
mtklein748ca3b2015-01-15 10:56:12 -080012#include "SkBBHFactory.h"
mtklein62bd1a62015-01-27 14:46:26 -080013#include "SkChecksum.h"
scroggocc2feb12015-08-14 08:32:46 -070014#include "SkCodec.h"
mtklein27c3fdd2016-02-26 14:43:21 -080015#include "SkColorPriv.h"
msarett888dc162016-05-23 10:21:17 -070016#include "SkColorSpace.h"
Mike Klein919cc452017-03-18 15:36:52 +000017#include "SkColorSpacePriv.h"
caryclark17f0b6d2014-07-22 10:15:34 -070018#include "SkCommonFlags.h"
kkinnunen3e980c32015-12-23 01:33:00 -080019#include "SkCommonFlagsConfig.h"
csmartdalton008b9d82017-02-22 12:00:42 -070020#include "SkCommonFlagsPathRenderer.h"
brianosman3c579dc2016-04-19 09:18:11 -070021#include "SkData.h"
Brian Salomondcbb9d92017-07-19 10:53:20 -040022#include "SkDebugfTracer.h"
23#include "SkEventTracer.h"
caryclark83ca6282015-06-10 09:31:09 -070024#include "SkFontMgr.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +000025#include "SkGraphics.h"
mtklein27c3fdd2016-02-26 14:43:21 -080026#include "SkHalf.h"
halcanary4dbbd042016-06-07 17:21:10 -070027#include "SkLeanWindows.h"
mtklein748ca3b2015-01-15 10:56:12 -080028#include "SkMD5.h"
mtklein1b249332015-07-07 12:21:21 -070029#include "SkMutex.h"
mtklein1d0f1642014-09-08 08:05:18 -070030#include "SkOSFile.h"
Ben Wagnerbf111d72016-11-07 18:05:29 -050031#include "SkOSPath.h"
mtklein27c3fdd2016-02-26 14:43:21 -080032#include "SkPM4fPriv.h"
Mike Klein13b6afd2017-06-12 12:45:41 -040033#include "SkPngEncoder.h"
Brian Salomondcbb9d92017-07-19 10:53:20 -040034#include "SkScan.h"
mtklein246ba3a2016-02-23 10:39:36 -080035#include "SkSpinlock.h"
mtkleina82f5622015-02-20 12:30:19 -080036#include "SkTHash.h"
mtklein406654b2014-09-03 15:34:37 -070037#include "SkTaskGroup.h"
mtkleinde6fc2b2015-03-12 06:28:54 -070038#include "SkThreadUtils.h"
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +000039#include "Test.h"
mtklein748ca3b2015-01-15 10:56:12 -080040#include "Timer.h"
Mike Kleinadacaef2017-02-06 09:26:14 -050041#include "ios_utils.h"
brianosman3c579dc2016-04-19 09:18:11 -070042#include "picture_utils.h"
caryclark83ca6282015-06-10 09:31:09 -070043#include "sk_tool_utils.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +000044
scroggo19b91532016-10-24 09:03:26 -070045#include <vector>
46
halcanary7a14b312015-10-01 07:28:13 -070047#ifdef SK_PDF_IMAGE_STATS
48extern void SkPDFImageDumpStats();
49#endif
50
mtkleina5114d72015-08-24 13:27:01 -070051#include "png.h"
52
bungeman60e0fee2015-08-26 05:15:46 -070053#include <stdlib.h>
54
scroggo27a58342015-10-28 08:56:41 -070055#ifndef SK_BUILD_FOR_WIN32
56 #include <unistd.h>
57#endif
58
Mike Reed7c26ca72017-07-05 15:45:52 -040059extern bool gSkForceRasterPipelineBlitter;
60
djsollen54416de2015-04-03 07:24:48 -070061DEFINE_string(src, "tests gm skp image", "Source types to test.");
mtklein748ca3b2015-01-15 10:56:12 -080062DEFINE_bool(nameByHash, false,
63 "If true, write to FLAGS_writePath[0]/<hash>.png instead of "
djsollen54416de2015-04-03 07:24:48 -070064 "to FLAGS_writePath[0]/<config>/<sourceType>/<sourceOptions>/<name>.png");
mtklein748ca3b2015-01-15 10:56:12 -080065DEFINE_bool2(pathOpsExtended, x, false, "Run extended pathOps tests.");
mtkleind603b222015-02-17 11:13:33 -080066DEFINE_string(matrix, "1 0 0 1",
67 "2x2 scale+skew matrix to apply or upright when using "
68 "'matrix' or 'upright' in config.");
mtklein82d28432015-01-15 12:46:02 -080069DEFINE_bool(gpu_threading, false, "Allow GPU work to run on multiple threads?");
mtklein@google.comd36522d2013-10-16 13:02:15 +000070
mtkleina2ef6422015-01-15 13:44:22 -080071DEFINE_string(blacklist, "",
djsollen54416de2015-04-03 07:24:48 -070072 "Space-separated config/src/srcOptions/name quadruples to blacklist. '_' matches anything. E.g. \n"
73 "'--blacklist gpu skp _ _' will blacklist all SKPs drawn into the gpu config.\n"
74 "'--blacklist gpu skp _ _ 8888 gm _ aarects' will also blacklist the aarects GM on 8888.");
mtkleina2ef6422015-01-15 13:44:22 -080075
mtklein62bd1a62015-01-27 14:46:26 -080076DEFINE_string2(readPath, r, "", "If set check for equality with golden results in this directory.");
77
borenet09ed4802015-04-03 14:15:33 -070078DEFINE_string(uninterestingHashesFile, "",
79 "File containing a list of uninteresting hashes. If a result hashes to something in "
80 "this list, no image is written for that result.");
81
mtklein6393c062015-04-27 08:45:01 -070082DEFINE_int32(shards, 1, "We're splitting source data into this many shards.");
83DEFINE_int32(shard, 0, "Which shard do I run?");
84
halcanary45420a92016-06-02 12:41:14 -070085DEFINE_string(mskps, "", "Directory to read mskps from, or a single mskp file.");
Mike Reed7c26ca72017-07-05 15:45:52 -040086DEFINE_bool(forceRasterPipeline, false, "sets gSkForceRasterPipelineBlitter");
halcanary45420a92016-06-02 12:41:14 -070087
csmartdalton008b9d82017-02-22 12:00:42 -070088#if SK_SUPPORT_GPU
89DEFINE_pathrenderer_flag;
90#endif
91
Derek Sollenbergeredfe3df2017-07-19 15:25:24 -040092DEFINE_bool(ignoreSigInt, false, "ignore SIGINT signals during test execution");
93
mtklein748ca3b2015-01-15 10:56:12 -080094using namespace DM;
bsalomon3724e572016-03-30 18:56:19 -070095using sk_gpu_test::GrContextFactory;
bsalomon273c0f52016-03-31 10:59:06 -070096using sk_gpu_test::GLTestContext;
bsalomonf2f1c172016-04-05 12:59:06 -070097using sk_gpu_test::ContextInfo;
mtklein@google.comd36522d2013-10-16 13:02:15 +000098
mtklein748ca3b2015-01-15 10:56:12 -080099/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
100
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700101static const double kStartMs = SkTime::GetMSecs();
mtkleinc41fd922016-03-08 09:01:39 -0800102
103static FILE* gVLog;
104
105template <typename... Args>
106static void vlog(const char* fmt, Args&&... args) {
107 if (gVLog) {
108 fprintf(gVLog, "%s\t", HumanizeMs(SkTime::GetMSecs() - kStartMs).c_str());
109 fprintf(gVLog, fmt, args...);
110 fflush(gVLog);
111 }
112}
113
114template <typename... Args>
115static void info(const char* fmt, Args&&... args) {
116 vlog(fmt, args...);
117 if (!FLAGS_quiet) {
118 printf(fmt, args...);
119 }
120}
121static void info(const char* fmt) {
122 if (!FLAGS_quiet) {
123 printf("%s", fmt); // Clang warns printf(fmt) is insecure.
124 }
125}
126
reed086eea92016-05-04 17:12:46 -0700127SK_DECLARE_STATIC_MUTEX(gFailuresMutex);
mtklein748ca3b2015-01-15 10:56:12 -0800128static SkTArray<SkString> gFailures;
129
mtklein3eed7dd2016-02-24 19:07:07 -0800130static void fail(const SkString& err) {
mtklein748ca3b2015-01-15 10:56:12 -0800131 SkAutoMutexAcquire lock(gFailuresMutex);
132 SkDebugf("\n\nFAILURE: %s\n\n", err.c_str());
133 gFailures.push_back(err);
halcanary9e398f72015-01-10 11:18:04 -0800134}
135
mtklein0f7748a2016-07-25 15:27:29 -0700136struct Running {
137 SkString id;
138 SkThreadID thread;
139
140 void dump() const {
141 info("\t%s\n", id.c_str());
142 }
143};
mtkleinb37cb412015-03-18 05:27:14 -0700144
mtklein246ba3a2016-02-23 10:39:36 -0800145// We use a spinlock to make locking this in a signal handler _somewhat_ safe.
Mike Klein03a95ad2017-06-23 12:36:32 -0400146static SkSpinlock gMutex;
147static int gPending;
mtklein0f7748a2016-07-25 15:27:29 -0700148static SkTArray<Running> gRunning;
mtklein748ca3b2015-01-15 10:56:12 -0800149
mtklein3eed7dd2016-02-24 19:07:07 -0800150static void done(const char* config, const char* src, const char* srcOptions, const char* name) {
151 SkString id = SkStringPrintf("%s %s %s %s", config, src, srcOptions, name);
mtkleinc41fd922016-03-08 09:01:39 -0800152 vlog("done %s\n", id.c_str());
mtkleinafae30a2016-02-24 12:28:32 -0800153 int pending;
mtkleinb37cb412015-03-18 05:27:14 -0700154 {
mtkleinf10637f2016-06-10 13:56:35 -0700155 SkAutoMutexAcquire lock(gMutex);
mtkleinb37cb412015-03-18 05:27:14 -0700156 for (int i = 0; i < gRunning.count(); i++) {
mtklein0f7748a2016-07-25 15:27:29 -0700157 if (gRunning[i].id == id) {
mtkleinb37cb412015-03-18 05:27:14 -0700158 gRunning.removeShuffle(i);
159 break;
160 }
161 }
mtkleinafae30a2016-02-24 12:28:32 -0800162 pending = --gPending;
reed50bc0512015-05-19 14:13:31 -0700163 }
Mike Klein03a95ad2017-06-23 12:36:32 -0400164
165 // We write out dm.json file and print out a progress update every once in a while.
166 // Notice this also handles the final dm.json and progress update when pending == 0.
mtkleina17241b2015-01-23 05:48:00 -0800167 if (pending % 500 == 0) {
168 JsonWriter::DumpJson();
Mike Klein03a95ad2017-06-23 12:36:32 -0400169
170 int curr = sk_tools::getCurrResidentSetSizeMB(),
171 peak = sk_tools::getMaxResidentSetSizeMB();
172 SkString elapsed = HumanizeMs(SkTime::GetMSecs() - kStartMs);
173
174 SkAutoMutexAcquire lock(gMutex);
175 info("\n%dMB RAM, %dMB peak, %s elapsed, %d queued, %d active:\n",
176 curr, peak, elapsed.c_str(), gPending - gRunning.count(), gRunning.count());
177 for (auto& task : gRunning) {
178 task.dump();
179 }
mtkleina17241b2015-01-23 05:48:00 -0800180 }
mtklein@google.comd36522d2013-10-16 13:02:15 +0000181}
182
mtklein3eed7dd2016-02-24 19:07:07 -0800183static void start(const char* config, const char* src, const char* srcOptions, const char* name) {
184 SkString id = SkStringPrintf("%s %s %s %s", config, src, srcOptions, name);
mtkleinc41fd922016-03-08 09:01:39 -0800185 vlog("start %s\n", id.c_str());
mtkleinf10637f2016-06-10 13:56:35 -0700186 SkAutoMutexAcquire lock(gMutex);
mtklein0f7748a2016-07-25 15:27:29 -0700187 gRunning.push_back({id,SkGetThreadID()});
mtkleinb37cb412015-03-18 05:27:14 -0700188}
189
Mike Klein3cc2d202016-10-24 11:39:43 -0400190static void find_culprit() {
191 // Assumes gMutex is locked.
192 SkThreadID thisThread = SkGetThreadID();
193 for (auto& task : gRunning) {
194 if (task.thread == thisThread) {
195 info("Likely culprit:\n");
196 task.dump();
mtklein0f7748a2016-07-25 15:27:29 -0700197 }
198 }
Mike Klein3cc2d202016-10-24 11:39:43 -0400199}
mtklein0f7748a2016-07-25 15:27:29 -0700200
mtklein246ba3a2016-02-23 10:39:36 -0800201#if defined(SK_BUILD_FOR_WIN32)
mtklein5b64dab2016-06-09 08:59:48 -0700202 static LONG WINAPI crash_handler(EXCEPTION_POINTERS* e) {
mtkleinf8557292016-02-29 06:35:28 -0800203 static const struct {
204 const char* name;
robertphillips75467862016-03-01 14:10:23 -0800205 DWORD code;
mtkleinf8557292016-02-29 06:35:28 -0800206 } kExceptions[] = {
207 #define _(E) {#E, E}
208 _(EXCEPTION_ACCESS_VIOLATION),
209 _(EXCEPTION_BREAKPOINT),
210 _(EXCEPTION_INT_DIVIDE_BY_ZERO),
211 _(EXCEPTION_STACK_OVERFLOW),
212 // TODO: more?
213 #undef _
214 };
215
mtkleinf10637f2016-06-10 13:56:35 -0700216 SkAutoMutexAcquire lock(gMutex);
mtklein5b64dab2016-06-09 08:59:48 -0700217
218 const DWORD code = e->ExceptionRecord->ExceptionCode;
219 info("\nCaught exception %u", code);
220 for (const auto& exception : kExceptions) {
221 if (exception.code == code) {
222 info(" %s", exception.name);
mtkleinf8557292016-02-29 06:35:28 -0800223 }
mtkleinf8557292016-02-29 06:35:28 -0800224 }
mtklein5b64dab2016-06-09 08:59:48 -0700225 info(", was running:\n");
226 for (auto& task : gRunning) {
mtklein0f7748a2016-07-25 15:27:29 -0700227 task.dump();
mtklein5b64dab2016-06-09 08:59:48 -0700228 }
mtklein0f7748a2016-07-25 15:27:29 -0700229 find_culprit();
mtklein5b64dab2016-06-09 08:59:48 -0700230 fflush(stdout);
231
mtkleinf8557292016-02-29 06:35:28 -0800232 // Execute default exception handler... hopefully, exit.
233 return EXCEPTION_EXECUTE_HANDLER;
mtklein246ba3a2016-02-23 10:39:36 -0800234 }
235
Mike Klein3cc2d202016-10-24 11:39:43 -0400236 static void setup_crash_handler() {
237 SetUnhandledExceptionFilter(crash_handler);
238 }
239#else
mtklein246ba3a2016-02-23 10:39:36 -0800240 #include <signal.h>
Mike Klein3cc2d202016-10-24 11:39:43 -0400241 #if !defined(SK_BUILD_FOR_ANDROID)
242 #include <execinfo.h>
Brian Salomondcbb9d92017-07-19 10:53:20 -0400243
244#endif
Mike Klein3cc2d202016-10-24 11:39:43 -0400245
246 static constexpr int max_of() { return 0; }
247 template <typename... Rest>
248 static constexpr int max_of(int x, Rest... rest) {
249 return x > max_of(rest...) ? x : max_of(rest...);
250 }
251
252 static void (*previous_handler[max_of(SIGABRT,SIGBUS,SIGFPE,SIGILL,SIGSEGV)+1])(int);
mtkleinf8557292016-02-29 06:35:28 -0800253
mtklein5b64dab2016-06-09 08:59:48 -0700254 static void crash_handler(int sig) {
mtkleinf10637f2016-06-10 13:56:35 -0700255 SkAutoMutexAcquire lock(gMutex);
mtklein5b64dab2016-06-09 08:59:48 -0700256
257 info("\nCaught signal %d [%s], was running:\n", sig, strsignal(sig));
258 for (auto& task : gRunning) {
mtklein0f7748a2016-07-25 15:27:29 -0700259 task.dump();
mtklein5b64dab2016-06-09 08:59:48 -0700260 }
mtklein0f7748a2016-07-25 15:27:29 -0700261 find_culprit();
mtklein5b64dab2016-06-09 08:59:48 -0700262
Mike Klein3cc2d202016-10-24 11:39:43 -0400263 #if !defined(SK_BUILD_FOR_ANDROID)
mtklein5b64dab2016-06-09 08:59:48 -0700264 void* stack[64];
265 int count = backtrace(stack, SK_ARRAY_COUNT(stack));
266 char** symbols = backtrace_symbols(stack, count);
267 info("\nStack trace:\n");
268 for (int i = 0; i < count; i++) {
269 info(" %s\n", symbols[i]);
270 }
Mike Klein3cc2d202016-10-24 11:39:43 -0400271 #endif
mtklein5b64dab2016-06-09 08:59:48 -0700272 fflush(stdout);
273
Mike Klein3cc2d202016-10-24 11:39:43 -0400274 signal(sig, previous_handler[sig]);
275 raise(sig);
mtklein5b64dab2016-06-09 08:59:48 -0700276 }
277
mtklein246ba3a2016-02-23 10:39:36 -0800278 static void setup_crash_handler() {
279 const int kSignals[] = { SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGSEGV };
280 for (int sig : kSignals) {
Mike Klein3cc2d202016-10-24 11:39:43 -0400281 previous_handler[sig] = signal(sig, crash_handler);
mtklein246ba3a2016-02-23 10:39:36 -0800282 }
Derek Sollenbergeredfe3df2017-07-19 15:25:24 -0400283
284 if (FLAGS_ignoreSigInt) {
285 signal(SIGINT, SIG_IGN);
286 }
mtklein246ba3a2016-02-23 10:39:36 -0800287 }
288#endif
289
mtklein748ca3b2015-01-15 10:56:12 -0800290/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
mtklein114c3cd2015-01-15 10:15:02 -0800291
mtklein62bd1a62015-01-27 14:46:26 -0800292struct Gold : public SkString {
mtkleina82f5622015-02-20 12:30:19 -0800293 Gold() : SkString("") {}
mtklein3eed7dd2016-02-24 19:07:07 -0800294 Gold(const SkString& sink, const SkString& src,
295 const SkString& srcOptions, const SkString& name,
296 const SkString& md5)
mtklein62bd1a62015-01-27 14:46:26 -0800297 : SkString("") {
298 this->append(sink);
299 this->append(src);
djsollen54416de2015-04-03 07:24:48 -0700300 this->append(srcOptions);
mtklein62bd1a62015-01-27 14:46:26 -0800301 this->append(name);
302 this->append(md5);
mtklein62bd1a62015-01-27 14:46:26 -0800303 }
mtkleinc8d1dd42015-10-15 12:23:01 -0700304 struct Hash {
305 uint32_t operator()(const Gold& g) const {
306 return SkGoodHash()((const SkString&)g);
307 }
308 };
mtklein62bd1a62015-01-27 14:46:26 -0800309};
mtkleina82f5622015-02-20 12:30:19 -0800310static SkTHashSet<Gold, Gold::Hash> gGold;
mtklein62bd1a62015-01-27 14:46:26 -0800311
312static void add_gold(JsonWriter::BitmapResult r) {
djsollen54416de2015-04-03 07:24:48 -0700313 gGold.add(Gold(r.config, r.sourceType, r.sourceOptions, r.name, r.md5));
mtklein62bd1a62015-01-27 14:46:26 -0800314}
315
316static void gather_gold() {
317 if (!FLAGS_readPath.isEmpty()) {
318 SkString path(FLAGS_readPath[0]);
319 path.append("/dm.json");
320 if (!JsonWriter::ReadJson(path.c_str(), add_gold)) {
321 fail(SkStringPrintf("Couldn't read %s for golden results.", path.c_str()));
322 }
323 }
324}
325
326/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
327
mtklein935f1b12016-03-16 08:37:19 -0700328#if defined(SK_BUILD_FOR_WIN32)
329 static const char* kNewline = "\r\n";
330#else
331 static const char* kNewline = "\n";
332#endif
333
borenet09ed4802015-04-03 14:15:33 -0700334static SkTHashSet<SkString> gUninterestingHashes;
335
336static void gather_uninteresting_hashes() {
337 if (!FLAGS_uninterestingHashesFile.isEmpty()) {
bungeman38d909e2016-08-02 14:40:46 -0700338 sk_sp<SkData> data(SkData::MakeFromFileName(FLAGS_uninterestingHashesFile[0]));
mtkleincc334b32015-09-22 11:43:53 -0700339 if (!data) {
mtkleinc41fd922016-03-08 09:01:39 -0800340 info("WARNING: unable to read uninteresting hashes from %s\n",
341 FLAGS_uninterestingHashesFile[0]);
mtkleincc334b32015-09-22 11:43:53 -0700342 return;
343 }
Mike Klein45dcc0c2017-04-05 18:04:31 -0400344
345 // Copy to a string to make sure SkStrSplit has a terminating \0 to find.
346 SkString contents((const char*)data->data(), data->size());
347
borenet09ed4802015-04-03 14:15:33 -0700348 SkTArray<SkString> hashes;
Mike Klein45dcc0c2017-04-05 18:04:31 -0400349 SkStrSplit(contents.c_str(), kNewline, &hashes);
borenet09ed4802015-04-03 14:15:33 -0700350 for (const SkString& hash : hashes) {
351 gUninterestingHashes.add(hash);
352 }
mtkleinc41fd922016-03-08 09:01:39 -0800353 info("FYI: loaded %d distinct uninteresting hashes from %d lines\n",
354 gUninterestingHashes.count(), hashes.count());
borenet09ed4802015-04-03 14:15:33 -0700355 }
356}
357
358/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
359
Ben Wagner145dbcd2016-11-03 14:40:50 -0400360struct TaggedSrc : public std::unique_ptr<Src> {
mtklein3eed7dd2016-02-24 19:07:07 -0800361 SkString tag;
362 SkString options;
mtkleine0effd62015-07-29 06:37:28 -0700363};
364
Ben Wagner145dbcd2016-11-03 14:40:50 -0400365struct TaggedSink : public std::unique_ptr<Sink> {
kkinnunen3e980c32015-12-23 01:33:00 -0800366 SkString tag;
djsollen54416de2015-04-03 07:24:48 -0700367};
mtklein748ca3b2015-01-15 10:56:12 -0800368
369static const bool kMemcpyOK = true;
370
mtkleine0effd62015-07-29 06:37:28 -0700371static SkTArray<TaggedSrc, kMemcpyOK> gSrcs;
372static SkTArray<TaggedSink, kMemcpyOK> gSinks;
mtklein748ca3b2015-01-15 10:56:12 -0800373
mtklein6393c062015-04-27 08:45:01 -0700374static bool in_shard() {
375 static int N = 0;
376 return N++ % FLAGS_shards == FLAGS_shard;
377}
378
mtklein3eed7dd2016-02-24 19:07:07 -0800379static void push_src(const char* tag, ImplicitString options, Src* s) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400380 std::unique_ptr<Src> src(s);
mtklein6393c062015-04-27 08:45:01 -0700381 if (in_shard() &&
mtklein3eed7dd2016-02-24 19:07:07 -0800382 FLAGS_src.contains(tag) &&
mtklein748ca3b2015-01-15 10:56:12 -0800383 !SkCommandLineFlags::ShouldSkip(FLAGS_match, src->name().c_str())) {
mtkleine0effd62015-07-29 06:37:28 -0700384 TaggedSrc& s = gSrcs.push_back();
mtklein18300a32016-03-16 13:53:35 -0700385 s.reset(src.release());
mtklein748ca3b2015-01-15 10:56:12 -0800386 s.tag = tag;
djsollen54416de2015-04-03 07:24:48 -0700387 s.options = options;
mtklein114c3cd2015-01-15 10:15:02 -0800388 }
mtklein748ca3b2015-01-15 10:56:12 -0800389}
mtklein114c3cd2015-01-15 10:15:02 -0800390
msarett9e707a02015-09-01 14:57:57 -0700391static void push_codec_src(Path path, CodecSrc::Mode mode, CodecSrc::DstColorType dstColorType,
scroggoc5560be2016-02-03 09:42:42 -0800392 SkAlphaType dstAlphaType, float scale) {
scroggoe4499842016-02-25 11:03:47 -0800393 if (FLAGS_simpleCodec) {
scroggo19b91532016-10-24 09:03:26 -0700394 const bool simple = CodecSrc::kCodec_Mode == mode || CodecSrc::kAnimated_Mode == mode;
395 if (!simple || dstColorType != CodecSrc::kGetFromCanvas_DstColorType || scale != 1.0f) {
scroggoe4499842016-02-25 11:03:47 -0800396 // Only decode in the simple case.
397 return;
scroggo19b91532016-10-24 09:03:26 -0700398 }
scroggoe4499842016-02-25 11:03:47 -0800399 }
msarett9e707a02015-09-01 14:57:57 -0700400 SkString folder;
401 switch (mode) {
402 case CodecSrc::kCodec_Mode:
403 folder.append("codec");
404 break;
msarettbb25b532016-01-13 09:31:39 -0800405 case CodecSrc::kCodecZeroInit_Mode:
406 folder.append("codec_zero_init");
407 break;
msarett9e707a02015-09-01 14:57:57 -0700408 case CodecSrc::kScanline_Mode:
409 folder.append("scanline");
410 break;
msarett9e707a02015-09-01 14:57:57 -0700411 case CodecSrc::kStripe_Mode:
412 folder.append("stripe");
413 break;
msarett91c22b22016-02-22 12:27:46 -0800414 case CodecSrc::kCroppedScanline_Mode:
415 folder.append("crop");
416 break;
msarett9e707a02015-09-01 14:57:57 -0700417 case CodecSrc::kSubset_Mode:
msarett36c37962015-09-02 13:20:52 -0700418 folder.append("codec_subset");
msarett9e707a02015-09-01 14:57:57 -0700419 break;
scroggo19b91532016-10-24 09:03:26 -0700420 case CodecSrc::kAnimated_Mode:
421 folder.append("codec_animated");
422 break;
msarett9e707a02015-09-01 14:57:57 -0700423 }
424
425 switch (dstColorType) {
426 case CodecSrc::kGrayscale_Always_DstColorType:
427 folder.append("_kGray8");
428 break;
msarett34e0ec42016-04-22 16:27:24 -0700429 case CodecSrc::kNonNative8888_Always_DstColorType:
430 folder.append("_kNonNative");
431 break;
msarett9e707a02015-09-01 14:57:57 -0700432 default:
433 break;
434 }
435
scroggoc5560be2016-02-03 09:42:42 -0800436 switch (dstAlphaType) {
scroggoc5560be2016-02-03 09:42:42 -0800437 case kPremul_SkAlphaType:
438 folder.append("_premul");
439 break;
440 case kUnpremul_SkAlphaType:
441 folder.append("_unpremul");
442 break;
443 default:
444 break;
445 }
446
msarett9e707a02015-09-01 14:57:57 -0700447 if (1.0f != scale) {
448 folder.appendf("_%.3f", scale);
449 }
450
scroggoc5560be2016-02-03 09:42:42 -0800451 CodecSrc* src = new CodecSrc(path, mode, dstColorType, dstAlphaType, scale);
msarett9e707a02015-09-01 14:57:57 -0700452 push_src("image", folder, src);
453}
454
scroggof8dc9df2016-05-16 09:04:13 -0700455static void push_android_codec_src(Path path, CodecSrc::DstColorType dstColorType,
456 SkAlphaType dstAlphaType, int sampleSize) {
msarett3d9d7a72015-10-21 10:27:10 -0700457 SkString folder;
scroggof8dc9df2016-05-16 09:04:13 -0700458 folder.append("scaled_codec");
msarett3d9d7a72015-10-21 10:27:10 -0700459
460 switch (dstColorType) {
461 case CodecSrc::kGrayscale_Always_DstColorType:
462 folder.append("_kGray8");
463 break;
msarett34e0ec42016-04-22 16:27:24 -0700464 case CodecSrc::kNonNative8888_Always_DstColorType:
465 folder.append("_kNonNative");
466 break;
msarett3d9d7a72015-10-21 10:27:10 -0700467 default:
468 break;
469 }
470
scroggoc5560be2016-02-03 09:42:42 -0800471 switch (dstAlphaType) {
scroggoc5560be2016-02-03 09:42:42 -0800472 case kPremul_SkAlphaType:
473 folder.append("_premul");
474 break;
msarett9e9444c2016-02-03 12:39:10 -0800475 case kUnpremul_SkAlphaType:
476 folder.append("_unpremul");
477 break;
scroggoc5560be2016-02-03 09:42:42 -0800478 default:
479 break;
480 }
481
msarett3d9d7a72015-10-21 10:27:10 -0700482 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800483 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700484 }
485
scroggof8dc9df2016-05-16 09:04:13 -0700486 AndroidCodecSrc* src = new AndroidCodecSrc(path, dstColorType, dstAlphaType, sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700487 push_src("image", folder, src);
488}
489
msarett18976312016-03-09 14:20:58 -0800490static void push_image_gen_src(Path path, ImageGenSrc::Mode mode, SkAlphaType alphaType, bool isGpu)
491{
492 SkString folder;
493 switch (mode) {
494 case ImageGenSrc::kCodec_Mode:
495 folder.append("gen_codec");
496 break;
497 case ImageGenSrc::kPlatform_Mode:
498 folder.append("gen_platform");
499 break;
500 }
501
502 if (isGpu) {
503 folder.append("_gpu");
504 } else {
505 switch (alphaType) {
506 case kOpaque_SkAlphaType:
507 folder.append("_opaque");
508 break;
509 case kPremul_SkAlphaType:
510 folder.append("_premul");
511 break;
512 case kUnpremul_SkAlphaType:
513 folder.append("_unpremul");
514 break;
515 default:
516 break;
517 }
518 }
519
520 ImageGenSrc* src = new ImageGenSrc(path, mode, alphaType, isGpu);
521 push_src("image", folder, src);
522}
523
msarett438b2ad2015-04-09 12:43:10 -0700524static void push_codec_srcs(Path path) {
bungeman38d909e2016-08-02 14:40:46 -0700525 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
msarett438b2ad2015-04-09 12:43:10 -0700526 if (!encoded) {
mtkleinc41fd922016-03-08 09:01:39 -0800527 info("Couldn't read %s.", path.c_str());
msarett438b2ad2015-04-09 12:43:10 -0700528 return;
529 }
Ben Wagner145dbcd2016-11-03 14:40:50 -0400530 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(encoded));
halcanary96fcdcc2015-08-27 07:41:13 -0700531 if (nullptr == codec.get()) {
mtkleinc41fd922016-03-08 09:01:39 -0800532 info("Couldn't create codec for %s.", path.c_str());
msarett438b2ad2015-04-09 12:43:10 -0700533 return;
534 }
535
scroggo9366aff2016-05-20 08:38:54 -0700536 // native scaling is only supported by WEBP and JPEG
537 bool supportsNativeScaling = false;
msarett438b2ad2015-04-09 12:43:10 -0700538
msarett91c22b22016-02-22 12:27:46 -0800539 SkTArray<CodecSrc::Mode> nativeModes;
540 nativeModes.push_back(CodecSrc::kCodec_Mode);
541 nativeModes.push_back(CodecSrc::kCodecZeroInit_Mode);
msarett91c22b22016-02-22 12:27:46 -0800542 switch (codec->getEncodedFormat()) {
Hal Canarydb683012016-11-23 08:55:18 -0700543 case SkEncodedImageFormat::kJPEG:
msarett91c22b22016-02-22 12:27:46 -0800544 nativeModes.push_back(CodecSrc::kScanline_Mode);
545 nativeModes.push_back(CodecSrc::kStripe_Mode);
546 nativeModes.push_back(CodecSrc::kCroppedScanline_Mode);
scroggo9366aff2016-05-20 08:38:54 -0700547 supportsNativeScaling = true;
msarett91c22b22016-02-22 12:27:46 -0800548 break;
Hal Canarydb683012016-11-23 08:55:18 -0700549 case SkEncodedImageFormat::kWEBP:
msarett91c22b22016-02-22 12:27:46 -0800550 nativeModes.push_back(CodecSrc::kSubset_Mode);
scroggo9366aff2016-05-20 08:38:54 -0700551 supportsNativeScaling = true;
msarett91c22b22016-02-22 12:27:46 -0800552 break;
Hal Canarydb683012016-11-23 08:55:18 -0700553 case SkEncodedImageFormat::kDNG:
msarettb65e6042016-02-23 05:37:25 -0800554 break;
msarett91c22b22016-02-22 12:27:46 -0800555 default:
556 nativeModes.push_back(CodecSrc::kScanline_Mode);
msarett91c22b22016-02-22 12:27:46 -0800557 break;
558 }
msarett9e707a02015-09-01 14:57:57 -0700559
msarett91c22b22016-02-22 12:27:46 -0800560 SkTArray<CodecSrc::DstColorType> colorTypes;
561 colorTypes.push_back(CodecSrc::kGetFromCanvas_DstColorType);
msarett34e0ec42016-04-22 16:27:24 -0700562 colorTypes.push_back(CodecSrc::kNonNative8888_Always_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700563 switch (codec->getInfo().colorType()) {
564 case kGray_8_SkColorType:
msarett91c22b22016-02-22 12:27:46 -0800565 colorTypes.push_back(CodecSrc::kGrayscale_Always_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700566 break;
567 default:
msarett36c37962015-09-02 13:20:52 -0700568 break;
569 }
msarett9e707a02015-09-01 14:57:57 -0700570
scroggoc5560be2016-02-03 09:42:42 -0800571 SkTArray<SkAlphaType> alphaModes;
572 alphaModes.push_back(kPremul_SkAlphaType);
scroggof2c96a22016-05-20 11:08:27 -0700573 if (codec->getInfo().alphaType() != kOpaque_SkAlphaType) {
574 alphaModes.push_back(kUnpremul_SkAlphaType);
scroggoc5560be2016-02-03 09:42:42 -0800575 }
msarettb714fb02016-01-22 14:46:42 -0800576
577 for (CodecSrc::Mode mode : nativeModes) {
scroggo9366aff2016-05-20 08:38:54 -0700578 for (CodecSrc::DstColorType colorType : colorTypes) {
579 for (SkAlphaType alphaType : alphaModes) {
scroggof2c96a22016-05-20 11:08:27 -0700580 // Only test kCroppedScanline_Mode when the alpha type is premul. The test is
scroggo9366aff2016-05-20 08:38:54 -0700581 // slow and won't be interestingly different with different alpha types.
582 if (CodecSrc::kCroppedScanline_Mode == mode &&
scroggof2c96a22016-05-20 11:08:27 -0700583 kPremul_SkAlphaType != alphaType) {
scroggo9366aff2016-05-20 08:38:54 -0700584 continue;
585 }
msarett91c22b22016-02-22 12:27:46 -0800586
scroggo9366aff2016-05-20 08:38:54 -0700587 push_codec_src(path, mode, colorType, alphaType, 1.0f);
msarettf3dc1882016-04-26 13:06:38 -0700588
scroggo9366aff2016-05-20 08:38:54 -0700589 // Skip kNonNative on different native scales. It won't be interestingly
590 // different.
591 if (supportsNativeScaling &&
592 CodecSrc::kNonNative8888_Always_DstColorType == colorType) {
593 // Native Scales
594 // SkJpegCodec natively supports scaling to the following:
595 for (auto scale : { 0.125f, 0.25f, 0.375f, 0.5f, 0.625f, 0.750f, 0.875f }) {
596 push_codec_src(path, mode, colorType, alphaType, scale);
597 }
scroggoc5560be2016-02-03 09:42:42 -0800598 }
msarett9e707a02015-09-01 14:57:57 -0700599 }
600 }
msarett0a242972015-06-11 14:27:27 -0700601 }
msarett36c37962015-09-02 13:20:52 -0700602
scroggo19b91532016-10-24 09:03:26 -0700603 {
604 std::vector<SkCodec::FrameInfo> frameInfos = codec->getFrameInfo();
605 if (frameInfos.size() > 1) {
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400606 for (auto dstCT : { CodecSrc::kNonNative8888_Always_DstColorType,
607 CodecSrc::kGetFromCanvas_DstColorType }) {
608 for (auto at : { kUnpremul_SkAlphaType, kPremul_SkAlphaType }) {
609 push_codec_src(path, CodecSrc::kAnimated_Mode, dstCT, at, 1.0f);
610 }
611 }
scroggo19b91532016-10-24 09:03:26 -0700612 }
613
614 }
615
scroggoe4499842016-02-25 11:03:47 -0800616 if (FLAGS_simpleCodec) {
617 return;
618 }
619
msarett3d9d7a72015-10-21 10:27:10 -0700620 const int sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
msarett36c37962015-09-02 13:20:52 -0700621
msarett3d9d7a72015-10-21 10:27:10 -0700622 for (int sampleSize : sampleSizes) {
msarett91c22b22016-02-22 12:27:46 -0800623 for (CodecSrc::DstColorType colorType : colorTypes) {
scroggoc5560be2016-02-03 09:42:42 -0800624 for (SkAlphaType alphaType : alphaModes) {
msarettf3dc1882016-04-26 13:06:38 -0700625 // We can exercise all of the kNonNative support code in the swizzler with just a
626 // few sample sizes. Skip the rest.
627 if (CodecSrc::kNonNative8888_Always_DstColorType == colorType && sampleSize > 3) {
628 continue;
629 }
630
scroggof8dc9df2016-05-16 09:04:13 -0700631 push_android_codec_src(path, colorType, alphaType, sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700632 }
msarett36c37962015-09-02 13:20:52 -0700633 }
634 }
msarett18976312016-03-09 14:20:58 -0800635
636 static const char* const rawExts[] = {
637 "arw", "cr2", "dng", "nef", "nrw", "orf", "raf", "rw2", "pef", "srw",
638 "ARW", "CR2", "DNG", "NEF", "NRW", "ORF", "RAF", "RW2", "PEF", "SRW",
639 };
640
641 // There is not currently a reason to test RAW images on image generator.
642 // If we want to enable these tests, we will need to fix skbug.com/5079.
643 for (const char* ext : rawExts) {
644 if (path.endsWith(ext)) {
645 return;
646 }
647 }
648
649 // Push image generator GPU test.
brianosmanc9ced392016-03-24 07:27:43 -0700650 push_image_gen_src(path, ImageGenSrc::kCodec_Mode, codec->getInfo().alphaType(), true);
msarett18976312016-03-09 14:20:58 -0800651
652 // Push image generator CPU tests.
653 for (SkAlphaType alphaType : alphaModes) {
654 push_image_gen_src(path, ImageGenSrc::kCodec_Mode, alphaType, false);
655
656#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
Hal Canarydb683012016-11-23 08:55:18 -0700657 if (SkEncodedImageFormat::kWEBP != codec->getEncodedFormat() &&
658 SkEncodedImageFormat::kWBMP != codec->getEncodedFormat() &&
msarett18976312016-03-09 14:20:58 -0800659 kUnpremul_SkAlphaType != alphaType)
660 {
661 push_image_gen_src(path, ImageGenSrc::kPlatform_Mode, alphaType, false);
662 }
msarettfc0b6d12016-03-17 13:50:17 -0700663#elif defined(SK_BUILD_FOR_WIN)
Hal Canarydb683012016-11-23 08:55:18 -0700664 if (SkEncodedImageFormat::kWEBP != codec->getEncodedFormat() &&
665 SkEncodedImageFormat::kWBMP != codec->getEncodedFormat())
msarettfc0b6d12016-03-17 13:50:17 -0700666 {
667 push_image_gen_src(path, ImageGenSrc::kPlatform_Mode, alphaType, false);
668 }
msarett18976312016-03-09 14:20:58 -0800669#endif
670 }
msarett438b2ad2015-04-09 12:43:10 -0700671}
672
msarettd1227a72016-05-18 06:23:57 -0700673static void push_brd_src(Path path, CodecSrc::DstColorType dstColorType, BRDSrc::Mode mode,
674 uint32_t sampleSize) {
675 SkString folder("brd_android_codec");
msaretta5783ae2015-09-08 15:35:32 -0700676 switch (mode) {
677 case BRDSrc::kFullImage_Mode:
678 break;
679 case BRDSrc::kDivisor_Mode:
680 folder.append("_divisor");
681 break;
682 default:
683 SkASSERT(false);
684 return;
685 }
686
687 switch (dstColorType) {
688 case CodecSrc::kGetFromCanvas_DstColorType:
689 break;
msaretta5783ae2015-09-08 15:35:32 -0700690 case CodecSrc::kGrayscale_Always_DstColorType:
691 folder.append("_kGray");
692 break;
693 default:
694 SkASSERT(false);
695 return;
696 }
697
698 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800699 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700700 }
701
msarettd1227a72016-05-18 06:23:57 -0700702 BRDSrc* src = new BRDSrc(path, mode, dstColorType, sampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700703 push_src("image", folder, src);
704}
705
706static void push_brd_srcs(Path path) {
Leon Scroggins IIIbace7a92017-07-05 11:39:27 -0400707 // Only run grayscale to one sampleSize and Mode. Though interesting
708 // to test grayscale, it should not reveal anything across various
scroggo24a9bd72016-05-20 06:02:42 -0700709 // sampleSizes and Modes
Leon Scroggins IIIbace7a92017-07-05 11:39:27 -0400710 // Arbitrarily choose Mode and sampleSize.
711 push_brd_src(path, CodecSrc::kGrayscale_Always_DstColorType, BRDSrc::kFullImage_Mode, 2);
scroggo24a9bd72016-05-20 06:02:42 -0700712
713
scroggo501b7342015-11-03 07:55:11 -0800714 // Test on a variety of sampleSizes, making sure to include:
715 // - 2, 4, and 8, which are natively supported by jpeg
716 // - multiples of 2 which are not divisible by 4 (analogous for 4)
717 // - larger powers of two, since BRD clients generally use powers of 2
718 // We will only produce output for the larger sizes on large images.
719 const uint32_t sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 24, 32, 64 };
msarett6efbe052015-09-11 09:01:16 -0700720
msaretta5783ae2015-09-08 15:35:32 -0700721 const BRDSrc::Mode modes[] = {
msarett26ad17b2015-10-22 07:29:19 -0700722 BRDSrc::kFullImage_Mode,
723 BRDSrc::kDivisor_Mode,
msaretta5783ae2015-09-08 15:35:32 -0700724 };
725
msarettd1227a72016-05-18 06:23:57 -0700726 for (uint32_t sampleSize : sampleSizes) {
scroggo24a9bd72016-05-20 06:02:42 -0700727 for (BRDSrc::Mode mode : modes) {
728 push_brd_src(path, CodecSrc::kGetFromCanvas_DstColorType, mode, sampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700729 }
730 }
731}
732
733static bool brd_supported(const char* ext) {
msarett8c8f22a2015-04-01 06:58:48 -0700734 static const char* const exts[] = {
msaretta5783ae2015-09-08 15:35:32 -0700735 "jpg", "jpeg", "png", "webp",
736 "JPG", "JPEG", "PNG", "WEBP",
msarett8c8f22a2015-04-01 06:58:48 -0700737 };
738
739 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
740 if (0 == strcmp(exts[i], ext)) {
741 return true;
742 }
743 }
744 return false;
scroggo9b77ddd2015-03-19 06:03:39 -0700745}
746
fmalitaa2b9fdf2016-08-03 19:53:36 -0700747template <typename T>
748void gather_file_srcs(const SkCommandLineFlags::StringArray& flags, const char* ext) {
749 for (int i = 0; i < flags.count(); i++) {
750 const char* path = flags[i];
751 if (sk_isdir(path)) {
752 SkOSFile::Iter it(path, ext);
753 for (SkString file; it.next(&file); ) {
754 push_src(ext, "", new T(SkOSPath::Join(path, file.c_str())));
755 }
756 } else {
757 push_src(ext, "", new T(path));
758 }
759 }
760}
761
scroggo86737142016-02-03 12:19:11 -0800762static bool gather_srcs() {
mtklein748ca3b2015-01-15 10:56:12 -0800763 for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) {
djsollen54416de2015-04-03 07:24:48 -0700764 push_src("gm", "", new GMSrc(r->factory()));
mtklein748ca3b2015-01-15 10:56:12 -0800765 }
scroggo86737142016-02-03 12:19:11 -0800766
fmalitaa2b9fdf2016-08-03 19:53:36 -0700767 gather_file_srcs<SKPSrc>(FLAGS_skps, "skp");
768 gather_file_srcs<MSKPSrc>(FLAGS_mskps, "mskp");
769#if defined(SK_XML)
770 gather_file_srcs<SVGSrc>(FLAGS_svgs, "svg");
771#endif
halcanary45420a92016-06-02 12:41:14 -0700772
scroggo86737142016-02-03 12:19:11 -0800773 SkTArray<SkString> images;
msarett69deca82016-04-29 09:38:40 -0700774 if (!CollectImages(FLAGS_images, &images)) {
scroggo86737142016-02-03 12:19:11 -0800775 return false;
776 }
777
778 for (auto image : images) {
779 push_codec_srcs(image);
scroggoe4499842016-02-25 11:03:47 -0800780 if (FLAGS_simpleCodec) {
781 continue;
782 }
783
mtklein21eaf3b2016-02-08 12:39:59 -0800784 const char* ext = strrchr(image.c_str(), '.');
785 if (ext && brd_supported(ext+1)) {
scroggo86737142016-02-03 12:19:11 -0800786 push_brd_srcs(image);
mtklein709d2c32015-01-15 08:30:25 -0800787 }
mtklein709d2c32015-01-15 08:30:25 -0800788 }
scroggo86737142016-02-03 12:19:11 -0800789
msarett69deca82016-04-29 09:38:40 -0700790 SkTArray<SkString> colorImages;
791 if (!CollectImages(FLAGS_colorImages, &colorImages)) {
792 return false;
793 }
794
795 for (auto colorImage : colorImages) {
msarett9ce3a542016-07-15 13:54:38 -0700796 ColorCodecSrc* src = new ColorCodecSrc(colorImage, ColorCodecSrc::kBaseline_Mode,
797 kN32_SkColorType);
msarettb8d1aac2016-08-04 12:22:06 -0700798 push_src("colorImage", "color_codec_baseline", src);
msarett888dc162016-05-23 10:21:17 -0700799
msarett9ce3a542016-07-15 13:54:38 -0700800 src = new ColorCodecSrc(colorImage, ColorCodecSrc::kDst_HPZR30w_Mode, kN32_SkColorType);
msarettb8d1aac2016-08-04 12:22:06 -0700801 push_src("colorImage", "color_codec_HPZR30w", src);
msarett9ce3a542016-07-15 13:54:38 -0700802 // TODO (msarett):
803 // Should we test this Dst in F16 mode (even though the Dst gamma is 2.2 instead of sRGB)?
msarett9876ac52016-06-01 14:47:18 -0700804
msarett9ce3a542016-07-15 13:54:38 -0700805 src = new ColorCodecSrc(colorImage, ColorCodecSrc::kDst_sRGB_Mode, kN32_SkColorType);
msarettb8d1aac2016-08-04 12:22:06 -0700806 push_src("colorImage", "color_codec_sRGB_kN32", src);
msarett9ce3a542016-07-15 13:54:38 -0700807 src = new ColorCodecSrc(colorImage, ColorCodecSrc::kDst_sRGB_Mode, kRGBA_F16_SkColorType);
msarettb8d1aac2016-08-04 12:22:06 -0700808 push_src("colorImage", "color_codec_sRGB_kF16", src);
msarett69deca82016-04-29 09:38:40 -0700809 }
810
scroggo86737142016-02-03 12:19:11 -0800811 return true;
mtklein709d2c32015-01-15 08:30:25 -0800812}
813
kkinnunen3e980c32015-12-23 01:33:00 -0800814static void push_sink(const SkCommandLineConfig& config, Sink* s) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400815 std::unique_ptr<Sink> sink(s);
kkinnunen3e980c32015-12-23 01:33:00 -0800816
mtkleine0effd62015-07-29 06:37:28 -0700817 // Try a simple Src as a canary. If it fails, skip this sink.
mtklein748ca3b2015-01-15 10:56:12 -0800818 struct : public Src {
mtkleine0effd62015-07-29 06:37:28 -0700819 Error draw(SkCanvas* c) const override {
820 c->drawRect(SkRect::MakeWH(1,1), SkPaint());
821 return "";
822 }
mtklein36352bf2015-03-25 18:17:31 -0700823 SkISize size() const override { return SkISize::Make(16, 16); }
mtkleine0effd62015-07-29 06:37:28 -0700824 Name name() const override { return "justOneRect"; }
825 } justOneRect;
mtklein114c3cd2015-01-15 10:15:02 -0800826
mtklein748ca3b2015-01-15 10:56:12 -0800827 SkBitmap bitmap;
828 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800829 SkString log;
mtkleine0effd62015-07-29 06:37:28 -0700830 Error err = sink->draw(justOneRect, &bitmap, &stream, &log);
mtklein4089ef72015-03-05 08:40:28 -0800831 if (err.isFatal()) {
mtkleinc41fd922016-03-08 09:01:39 -0800832 info("Could not run %s: %s\n", config.getTag().c_str(), err.c_str());
mtklein05641a52015-04-21 10:49:13 -0700833 exit(1);
mtklein114c3cd2015-01-15 10:15:02 -0800834 }
835
mtkleine0effd62015-07-29 06:37:28 -0700836 TaggedSink& ts = gSinks.push_back();
mtklein18300a32016-03-16 13:53:35 -0700837 ts.reset(sink.release());
kkinnunen3e980c32015-12-23 01:33:00 -0800838 ts.tag = config.getTag();
mtklein748ca3b2015-01-15 10:56:12 -0800839}
840
841static bool gpu_supported() {
842#if SK_SUPPORT_GPU
843 return FLAGS_gpu;
844#else
845 return false;
846#endif
847}
kkinnunen9ebc3f02015-12-21 23:48:13 -0800848
csmartdalton008b9d82017-02-22 12:00:42 -0700849static Sink* create_sink(const GrContextOptions& grCtxOptions, const SkCommandLineConfig* config) {
kkinnunen3e980c32015-12-23 01:33:00 -0800850#if SK_SUPPORT_GPU
851 if (gpu_supported()) {
852 if (const SkCommandLineConfigGpu* gpuConfig = config->asConfigGpu()) {
bsalomon85b4b532016-04-05 11:06:27 -0700853 GrContextFactory::ContextType contextType = gpuConfig->getContextType();
csmartdaltone812d492017-02-21 12:36:05 -0700854 GrContextFactory::ContextOverrides contextOverrides = gpuConfig->getContextOverrides();
csmartdalton008b9d82017-02-22 12:00:42 -0700855 GrContextFactory testFactory(grCtxOptions);
csmartdaltone812d492017-02-21 12:36:05 -0700856 if (!testFactory.get(contextType, contextOverrides)) {
mtkleinc41fd922016-03-08 09:01:39 -0800857 info("WARNING: can not create GPU context for config '%s'. "
858 "GM tests will be skipped.\n", gpuConfig->getTag().c_str());
kkinnunen3e980c32015-12-23 01:33:00 -0800859 return nullptr;
860 }
csmartdaltone812d492017-02-21 12:36:05 -0700861 return new GPUSink(contextType, contextOverrides, gpuConfig->getSamples(),
brianosmand93c1202016-03-10 07:49:08 -0800862 gpuConfig->getUseDIText(), gpuConfig->getColorType(),
Brian Salomonce5ee602017-07-17 11:31:31 -0400863 gpuConfig->getAlphaType(), sk_ref_sp(gpuConfig->getColorSpace()),
864 FLAGS_gpu_threading);
kkinnunen3e980c32015-12-23 01:33:00 -0800865 }
866 }
867#endif
868
869#define SINK(t, sink, ...) if (config->getBackend().equals(t)) { return new sink(__VA_ARGS__); }
870
mtklein748ca3b2015-01-15 10:56:12 -0800871 if (FLAGS_cpu) {
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500872 auto srgbColorSpace = SkColorSpace::MakeSRGB();
873 auto srgbLinearColorSpace = SkColorSpace::MakeSRGBLinear();
brianosmanb109b8c2016-06-16 13:03:24 -0700874
Matt Sarettd2228302017-03-02 08:53:46 -0500875 SINK("565", RasterSink, kRGB_565_SkColorType);
876 SINK("8888", RasterSink, kN32_SkColorType);
877 SINK("srgb", RasterSink, kN32_SkColorType, srgbColorSpace);
Matt Sarettd2228302017-03-02 08:53:46 -0500878 SINK("f16", RasterSink, kRGBA_F16_SkColorType, srgbLinearColorSpace);
879 SINK("pdf", PDFSink);
880 SINK("skp", SKPSink);
881 SINK("pipe", PipeSink);
882 SINK("svg", SVGSink);
883 SINK("null", NullSink);
884 SINK("xps", XPSSink);
885 SINK("pdfa", PDFSink, true);
Hal Canary85c7fe82016-10-25 10:33:27 -0400886 SINK("jsdebug", DebugSink);
mtklein748ca3b2015-01-15 10:56:12 -0800887 }
888#undef SINK
halcanary96fcdcc2015-08-27 07:41:13 -0700889 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800890}
891
Mike Klein841101d2017-03-10 09:55:51 -0500892static sk_sp<SkColorSpace> adobe_rgb() {
893 return SkColorSpace::MakeRGB(SkColorSpace::kSRGB_RenderTargetGamma,
894 SkColorSpace::kAdobeRGB_Gamut);
895}
896
Mike Klein919cc452017-03-18 15:36:52 +0000897static sk_sp<SkColorSpace> rgb_to_gbr() {
898 float gbr[9];
899 gbr[0] = gSRGB_toXYZD50[1];
900 gbr[1] = gSRGB_toXYZD50[2];
901 gbr[2] = gSRGB_toXYZD50[0];
902 gbr[3] = gSRGB_toXYZD50[4];
903 gbr[4] = gSRGB_toXYZD50[5];
904 gbr[5] = gSRGB_toXYZD50[3];
905 gbr[6] = gSRGB_toXYZD50[7];
906 gbr[7] = gSRGB_toXYZD50[8];
907 gbr[8] = gSRGB_toXYZD50[6];
908 SkMatrix44 toXYZD50(SkMatrix44::kUninitialized_Constructor);
909 toXYZD50.set3x3RowMajorf(gbr);
910 return SkColorSpace::MakeRGB(SkColorSpace::kSRGB_RenderTargetGamma, toXYZD50);
911}
912
kkinnunen3e980c32015-12-23 01:33:00 -0800913static Sink* create_via(const SkString& tag, Sink* wrapped) {
914#define VIA(t, via, ...) if (tag.equals(t)) { return new via(__VA_ARGS__); }
Mike Klein919cc452017-03-18 15:36:52 +0000915 VIA("adobe", ViaCSXform, wrapped, adobe_rgb(), false);
916 VIA("gbr", ViaCSXform, wrapped, rgb_to_gbr(), true);
mtklein9c5052f2016-08-06 12:51:51 -0700917 VIA("lite", ViaLite, wrapped);
reed54dc4872016-09-13 08:09:45 -0700918 VIA("pipe", ViaPipe, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700919 VIA("twice", ViaTwice, wrapped);
Mike Reedbae888e2017-02-18 16:50:45 -0500920#ifdef TEST_VIA_SVG
Mike Reedf67c4592017-02-17 17:06:11 -0500921 VIA("svg", ViaSVG, wrapped);
Mike Reedbae888e2017-02-18 16:50:45 -0500922#endif
halcanary96fcdcc2015-08-27 07:41:13 -0700923 VIA("serialize", ViaSerialization, wrapped);
mtklein4a34ecb2016-01-08 10:19:35 -0800924 VIA("pic", ViaPicture, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700925 VIA("2ndpic", ViaSecondPicture, wrapped);
mtkleind31c13d2015-05-05 12:59:56 -0700926 VIA("sp", ViaSingletonPictures, wrapped);
reedbabc3de2016-07-08 08:43:27 -0700927 VIA("defer", ViaDefer, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700928 VIA("tiles", ViaTiles, 256, 256, nullptr, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800929 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800930
mtkleind603b222015-02-17 11:13:33 -0800931 if (FLAGS_matrix.count() == 4) {
mtklein748ca3b2015-01-15 10:56:12 -0800932 SkMatrix m;
mtkleind603b222015-02-17 11:13:33 -0800933 m.reset();
934 m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
935 m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
936 m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
937 m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
938 VIA("matrix", ViaMatrix, m, wrapped);
939 VIA("upright", ViaUpright, m, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800940 }
tomhudson64de1e12015-03-05 08:01:07 -0800941
mtklein748ca3b2015-01-15 10:56:12 -0800942#undef VIA
halcanary96fcdcc2015-08-27 07:41:13 -0700943 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800944}
945
csmartdalton008b9d82017-02-22 12:00:42 -0700946static bool gather_sinks(const GrContextOptions& grCtxOptions) {
kkinnunen3e980c32015-12-23 01:33:00 -0800947 SkCommandLineConfigArray configs;
948 ParseConfigs(FLAGS_config, &configs);
949 for (int i = 0; i < configs.count(); i++) {
950 const SkCommandLineConfig& config = *configs[i];
csmartdalton008b9d82017-02-22 12:00:42 -0700951 Sink* sink = create_sink(grCtxOptions, &config);
kkinnunen3e980c32015-12-23 01:33:00 -0800952 if (sink == nullptr) {
mtkleinc41fd922016-03-08 09:01:39 -0800953 info("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
954 config.getTag().c_str());
kkinnunen3e980c32015-12-23 01:33:00 -0800955 continue;
956 }
mtklein748ca3b2015-01-15 10:56:12 -0800957
kkinnunen3e980c32015-12-23 01:33:00 -0800958 const SkTArray<SkString>& parts = config.getViaParts();
959 for (int j = parts.count(); j-- > 0;) {
960 const SkString& part = parts[j];
961 Sink* next = create_via(part, sink);
halcanary96fcdcc2015-08-27 07:41:13 -0700962 if (next == nullptr) {
mtkleinc41fd922016-03-08 09:01:39 -0800963 info("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
964 part.c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800965 delete sink;
halcanary96fcdcc2015-08-27 07:41:13 -0700966 sink = nullptr;
mtklein748ca3b2015-01-15 10:56:12 -0800967 break;
968 }
969 sink = next;
970 }
971 if (sink) {
972 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800973 }
974 }
brianosman05c987c2016-05-09 13:42:16 -0700975
976 // If no configs were requested (just running tests, perhaps?), then we're okay.
977 // Otherwise, make sure that at least one sink was constructed correctly. This catches
978 // the case of bots without a GPU being assigned GPU configs.
979 return (configs.count() == 0) || (gSinks.count() > 0);
mtklein114c3cd2015-01-15 10:15:02 -0800980}
mtklein709d2c32015-01-15 08:30:25 -0800981
mtkleina5114d72015-08-24 13:27:01 -0700982static bool dump_png(SkBitmap bitmap, const char* path, const char* md5) {
Mike Klein13b6afd2017-06-12 12:45:41 -0400983 SkPixmap pm;
984 if (!bitmap.peekPixels(&pm)) {
985 return false; // Ought to never happen... we're already read-back at this point.
mtkleina5114d72015-08-24 13:27:01 -0700986 }
Mike Klein13b6afd2017-06-12 12:45:41 -0400987 SkFILEWStream dst{path};
mtkleina5114d72015-08-24 13:27:01 -0700988
mtkleind69ece62015-09-10 10:37:44 -0700989 SkString description;
990 description.append("Key: ");
991 for (int i = 0; i < FLAGS_key.count(); i++) {
992 description.appendf("%s ", FLAGS_key[i]);
993 }
994 description.append("Properties: ");
995 for (int i = 0; i < FLAGS_properties.count(); i++) {
996 description.appendf("%s ", FLAGS_properties[i]);
997 }
998 description.appendf("MD5: %s", md5);
999
Mike Klein13b6afd2017-06-12 12:45:41 -04001000 const char* comments[] = {
1001 "Author", "DM dump_png()",
1002 "Description", description.c_str(),
1003 };
1004 size_t lengths[] = {
1005 strlen(comments[0])+1, strlen(comments[1])+1,
1006 strlen(comments[2])+1, strlen(comments[3])+1,
1007 };
mtkleina5114d72015-08-24 13:27:01 -07001008
Mike Klein13b6afd2017-06-12 12:45:41 -04001009 SkPngEncoder::Options options;
1010 options.fComments = SkDataTable::MakeCopyArrays((const void**)comments, lengths, 4);
1011 options.fFilterFlags = SkPngEncoder::FilterFlag::kNone;
1012 options.fZLibLevel = 1;
1013 options.fUnpremulBehavior = pm.colorSpace() ? SkTransferFunctionBehavior::kRespect
1014 : SkTransferFunctionBehavior::kIgnore;
1015 return SkPngEncoder::Encode(&dst, pm, options);
mtkleina5114d72015-08-24 13:27:01 -07001016}
1017
mtkleina2ef6422015-01-15 13:44:22 -08001018static bool match(const char* needle, const char* haystack) {
halcanary96fcdcc2015-08-27 07:41:13 -07001019 return 0 == strcmp("_", needle) || nullptr != strstr(haystack, needle);
mtkleina2ef6422015-01-15 13:44:22 -08001020}
1021
mtklein3eed7dd2016-02-24 19:07:07 -08001022static bool is_blacklisted(const char* sink, const char* src,
1023 const char* srcOptions, const char* name) {
mtklein0d243ff2015-04-03 07:51:00 -07001024 for (int i = 0; i < FLAGS_blacklist.count() - 3; i += 4) {
mtkleina2ef6422015-01-15 13:44:22 -08001025 if (match(FLAGS_blacklist[i+0], sink) &&
djsollen54416de2015-04-03 07:24:48 -07001026 match(FLAGS_blacklist[i+1], src) &&
1027 match(FLAGS_blacklist[i+2], srcOptions) &&
1028 match(FLAGS_blacklist[i+3], name)) {
mtklein3eed7dd2016-02-24 19:07:07 -08001029 return true;
mtkleina2ef6422015-01-15 13:44:22 -08001030 }
1031 }
mtklein3eed7dd2016-02-24 19:07:07 -08001032 return false;
mtkleina2ef6422015-01-15 13:44:22 -08001033}
1034
mtkleincd50bca2016-01-05 06:20:20 -08001035// Even when a Task Sink reports to be non-threadsafe (e.g. GPU), we know things like
1036// .png encoding are definitely thread safe. This lets us offload that work to CPU threads.
1037static SkTaskGroup gDefinitelyThreadSafeWork;
1038
mtklein748ca3b2015-01-15 10:56:12 -08001039// The finest-grained unit of work we can run: draw a single Src into a single Sink,
1040// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
1041struct Task {
mtkleine0effd62015-07-29 06:37:28 -07001042 Task(const TaggedSrc& src, const TaggedSink& sink) : src(src), sink(sink) {}
1043 const TaggedSrc& src;
1044 const TaggedSink& sink;
mtklein748ca3b2015-01-15 10:56:12 -08001045
mtklein21eaf3b2016-02-08 12:39:59 -08001046 static void Run(const Task& task) {
1047 SkString name = task.src->name();
mtkleine0effd62015-07-29 06:37:28 -07001048
mtkleinb9eb4ac2015-02-02 18:26:03 -08001049 SkString log;
mtklein3eed7dd2016-02-24 19:07:07 -08001050 if (!FLAGS_dryRun) {
mtklein748ca3b2015-01-15 10:56:12 -08001051 SkBitmap bitmap;
1052 SkDynamicMemoryWStream stream;
mtklein3eed7dd2016-02-24 19:07:07 -08001053 start(task.sink.tag.c_str(), task.src.tag.c_str(),
1054 task.src.options.c_str(), name.c_str());
mtklein21eaf3b2016-02-08 12:39:59 -08001055 Error err = task.sink->draw(*task.src, &bitmap, &stream, &log);
mtkleinb3b13b72016-03-07 13:20:52 -08001056 if (!log.isEmpty()) {
mtkleinc41fd922016-03-08 09:01:39 -08001057 info("%s %s %s %s:\n%s\n", task.sink.tag.c_str()
1058 , task.src.tag.c_str()
1059 , task.src.options.c_str()
1060 , name.c_str()
1061 , log.c_str());
mtkleinb3b13b72016-03-07 13:20:52 -08001062 }
mtklein748ca3b2015-01-15 10:56:12 -08001063 if (!err.isEmpty()) {
mtklein4089ef72015-03-05 08:40:28 -08001064 if (err.isFatal()) {
djsollen54416de2015-04-03 07:24:48 -07001065 fail(SkStringPrintf("%s %s %s %s: %s",
mtklein21eaf3b2016-02-08 12:39:59 -08001066 task.sink.tag.c_str(),
1067 task.src.tag.c_str(),
1068 task.src.options.c_str(),
mtklein4089ef72015-03-05 08:40:28 -08001069 name.c_str(),
1070 err.c_str()));
mtkleinb37cb412015-03-18 05:27:14 -07001071 } else {
mtklein3eed7dd2016-02-24 19:07:07 -08001072 done(task.sink.tag.c_str(), task.src.tag.c_str(),
1073 task.src.options.c_str(), name.c_str());
mtkleinba6ada72016-01-21 09:39:35 -08001074 return;
mtklein4089ef72015-03-05 08:40:28 -08001075 }
mtklein748ca3b2015-01-15 10:56:12 -08001076 }
mtklein62bd1a62015-01-27 14:46:26 -08001077
mtkleincd50bca2016-01-05 06:20:20 -08001078 // We're likely switching threads here, so we must capture by value, [=] or [foo,bar].
Hal Canary0b833192017-03-14 14:35:02 -04001079 SkStreamAsset* data = stream.detachAsStream().release();
mtkleincd50bca2016-01-05 06:20:20 -08001080 gDefinitelyThreadSafeWork.add([task,name,bitmap,data]{
Ben Wagner145dbcd2016-11-03 14:40:50 -04001081 std::unique_ptr<SkStreamAsset> ownedData(data);
mtkleincd50bca2016-01-05 06:20:20 -08001082
mtkleincd50bca2016-01-05 06:20:20 -08001083 SkString md5;
1084 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
1085 SkMD5 hash;
1086 if (data->getLength()) {
1087 hash.writeStream(data, data->getLength());
1088 data->rewind();
mtklein38408462015-07-08 07:25:27 -07001089 } else {
mtkleincd50bca2016-01-05 06:20:20 -08001090 // If we're BGRA (Linux, Windows), swizzle over to RGBA (Mac, Android).
1091 // This helps eliminate multiple 0-pixel-diff hashes on gold.skia.org.
1092 // (Android's general slow speed breaks the tie arbitrarily in RGBA's favor.)
1093 // We might consider promoting 565 to RGBA too.
1094 if (bitmap.colorType() == kBGRA_8888_SkColorType) {
1095 SkBitmap swizzle;
Matt Sarett68b8e3d2017-04-28 11:15:22 -04001096 SkAssertResult(sk_tool_utils::copy_to(&swizzle, kRGBA_8888_SkColorType,
1097 bitmap));
mtkleincd50bca2016-01-05 06:20:20 -08001098 hash.write(swizzle.getPixels(), swizzle.getSize());
1099 } else {
1100 hash.write(bitmap.getPixels(), bitmap.getSize());
1101 }
1102 }
1103 SkMD5::Digest digest;
1104 hash.finish(digest);
1105 for (int i = 0; i < 16; i++) {
1106 md5.appendf("%02x", digest.data[i]);
mtklein38408462015-07-08 07:25:27 -07001107 }
mtklein62bd1a62015-01-27 14:46:26 -08001108 }
mtklein62bd1a62015-01-27 14:46:26 -08001109
mtkleincd50bca2016-01-05 06:20:20 -08001110 if (!FLAGS_readPath.isEmpty() &&
mtklein3eed7dd2016-02-24 19:07:07 -08001111 !gGold.contains(Gold(task.sink.tag, task.src.tag,
1112 task.src.options, name, md5))) {
mtkleincd50bca2016-01-05 06:20:20 -08001113 fail(SkStringPrintf("%s not found for %s %s %s %s in %s",
1114 md5.c_str(),
mtklein21eaf3b2016-02-08 12:39:59 -08001115 task.sink.tag.c_str(),
1116 task.src.tag.c_str(),
1117 task.src.options.c_str(),
mtkleincd50bca2016-01-05 06:20:20 -08001118 name.c_str(),
1119 FLAGS_readPath[0]));
mtklein748ca3b2015-01-15 10:56:12 -08001120 }
mtkleincd50bca2016-01-05 06:20:20 -08001121
1122 if (!FLAGS_writePath.isEmpty()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001123 const char* ext = task.sink->fileExtension();
mtkleincd50bca2016-01-05 06:20:20 -08001124 if (data->getLength()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001125 WriteToDisk(task, md5, ext, data, data->getLength(), nullptr);
mtkleincd50bca2016-01-05 06:20:20 -08001126 SkASSERT(bitmap.drawsNothing());
1127 } else if (!bitmap.drawsNothing()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001128 WriteToDisk(task, md5, ext, nullptr, 0, &bitmap);
mtkleincd50bca2016-01-05 06:20:20 -08001129 }
1130 }
1131 });
mtklein748ca3b2015-01-15 10:56:12 -08001132 }
mtklein3eed7dd2016-02-24 19:07:07 -08001133 done(task.sink.tag.c_str(), task.src.tag.c_str(), task.src.options.c_str(), name.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001134 }
1135
1136 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -08001137 SkString md5,
1138 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -08001139 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -08001140 const SkBitmap* bitmap) {
mtklein409d4702016-02-29 07:38:01 -08001141 bool gammaCorrect = false;
1142 if (bitmap) {
Brian Osman7039f742016-11-01 15:56:16 -04001143 gammaCorrect = SkToBool(bitmap->info().colorSpace());
mtklein409d4702016-02-29 07:38:01 -08001144 }
1145
mtklein748ca3b2015-01-15 10:56:12 -08001146 JsonWriter::BitmapResult result;
djsollen54416de2015-04-03 07:24:48 -07001147 result.name = task.src->name();
mtklein3eed7dd2016-02-24 19:07:07 -08001148 result.config = task.sink.tag;
djsollen54416de2015-04-03 07:24:48 -07001149 result.sourceType = task.src.tag;
1150 result.sourceOptions = task.src.options;
1151 result.ext = ext;
mtklein409d4702016-02-29 07:38:01 -08001152 result.gammaCorrect = gammaCorrect;
djsollen54416de2015-04-03 07:24:48 -07001153 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -08001154 JsonWriter::AddBitmapResult(result);
1155
mtkleinb0531a72015-04-07 13:38:48 -07001156 // If an MD5 is uninteresting, we want it noted in the JSON file,
1157 // but don't want to dump it out as a .png (or whatever ext is).
1158 if (gUninterestingHashes.contains(md5)) {
1159 return;
1160 }
1161
mtklein748ca3b2015-01-15 10:56:12 -08001162 const char* dir = FLAGS_writePath[0];
1163 if (0 == strcmp(dir, "@")) { // Needed for iOS.
1164 dir = FLAGS_resourcePath[0];
1165 }
1166 sk_mkdir(dir);
1167
1168 SkString path;
1169 if (FLAGS_nameByHash) {
1170 path = SkOSPath::Join(dir, result.md5.c_str());
1171 path.append(".");
1172 path.append(ext);
1173 if (sk_exists(path.c_str())) {
1174 return; // Content-addressed. If it exists already, we're done.
1175 }
1176 } else {
kkinnunen3e980c32015-12-23 01:33:00 -08001177 path = SkOSPath::Join(dir, task.sink.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001178 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001179 path = SkOSPath::Join(path.c_str(), task.src.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001180 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001181 if (strcmp(task.src.options.c_str(), "") != 0) {
1182 path = SkOSPath::Join(path.c_str(), task.src.options.c_str());
djsollen54416de2015-04-03 07:24:48 -07001183 sk_mkdir(path.c_str());
1184 }
mtklein748ca3b2015-01-15 10:56:12 -08001185 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
1186 path.append(".");
1187 path.append(ext);
1188 }
1189
mtklein748ca3b2015-01-15 10:56:12 -08001190 if (bitmap) {
mtkleina5114d72015-08-24 13:27:01 -07001191 if (!dump_png(*bitmap, path.c_str(), result.md5.c_str())) {
mtklein748ca3b2015-01-15 10:56:12 -08001192 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
1193 return;
1194 }
1195 } else {
mtkleina5114d72015-08-24 13:27:01 -07001196 SkFILEWStream file(path.c_str());
1197 if (!file.isValid()) {
1198 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
1199 return;
1200 }
mtklein748ca3b2015-01-15 10:56:12 -08001201 if (!file.writeStream(data, len)) {
1202 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
1203 return;
1204 }
1205 }
1206 }
1207};
1208
mtklein748ca3b2015-01-15 10:56:12 -08001209/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1210
1211// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
1212
mtklein21eaf3b2016-02-08 12:39:59 -08001213static SkTDArray<skiatest::Test> gParallelTests, gSerialTests;
mtklein748ca3b2015-01-15 10:56:12 -08001214
1215static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -08001216 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -08001217 return;
1218 }
mtklein6393c062015-04-27 08:45:01 -07001219 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r; r = r->next()) {
1220 if (!in_shard()) {
1221 continue;
1222 }
halcanary87f3ba42015-01-20 09:30:20 -08001223 // Despite its name, factory() is returning a reference to
1224 // link-time static const POD data.
1225 const skiatest::Test& test = r->factory();
1226 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -08001227 continue;
1228 }
halcanary87f3ba42015-01-20 09:30:20 -08001229 if (test.needsGpu && gpu_supported()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001230 (FLAGS_gpu_threading ? gParallelTests : gSerialTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -08001231 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein21eaf3b2016-02-08 12:39:59 -08001232 gParallelTests.push(test);
mtklein82d28432015-01-15 12:46:02 -08001233 }
mtklein748ca3b2015-01-15 10:56:12 -08001234 }
1235}
1236
csmartdalton008b9d82017-02-22 12:00:42 -07001237static void run_test(skiatest::Test test, const GrContextOptions& grCtxOptions) {
halcanary87f3ba42015-01-20 09:30:20 -08001238 struct : public skiatest::Reporter {
mtklein36352bf2015-03-25 18:17:31 -07001239 void reportFailed(const skiatest::Failure& failure) override {
halcanary87f3ba42015-01-20 09:30:20 -08001240 fail(failure.toString());
1241 JsonWriter::AddTestFailure(failure);
1242 }
mtklein36352bf2015-03-25 18:17:31 -07001243 bool allowExtendedTest() const override {
halcanary87f3ba42015-01-20 09:30:20 -08001244 return FLAGS_pathOpsExtended;
1245 }
mtklein36352bf2015-03-25 18:17:31 -07001246 bool verbose() const override { return FLAGS_veryVerbose; }
halcanary87f3ba42015-01-20 09:30:20 -08001247 } reporter;
djsollen824996a2015-06-12 12:06:22 -07001248
mtklein3eed7dd2016-02-24 19:07:07 -08001249 if (!FLAGS_dryRun && !is_blacklisted("_", "tests", "_", test.name)) {
mtklein21eaf3b2016-02-08 12:39:59 -08001250 start("unit", "test", "", test.name);
csmartdalton008b9d82017-02-22 12:00:42 -07001251 GrContextFactory factory(grCtxOptions);
mtklein21eaf3b2016-02-08 12:39:59 -08001252 test.proc(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -08001253 }
mtklein3eed7dd2016-02-24 19:07:07 -08001254 done("unit", "test", "", test.name);
mtklein748ca3b2015-01-15 10:56:12 -08001255}
1256
1257/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1258
caryclark83ca6282015-06-10 09:31:09 -07001259#define PORTABLE_FONT_PREFIX "Toy Liberation "
1260
mbocee6a9912016-05-31 11:42:36 -07001261static sk_sp<SkTypeface> create_from_name(const char familyName[], SkFontStyle style) {
caryclark83ca6282015-06-10 09:31:09 -07001262 if (familyName && strlen(familyName) > sizeof(PORTABLE_FONT_PREFIX)
1263 && !strncmp(familyName, PORTABLE_FONT_PREFIX, sizeof(PORTABLE_FONT_PREFIX) - 1)) {
caryclark1818acb2015-07-24 12:09:25 -07001264 return sk_tool_utils::create_portable_typeface(familyName, style);
caryclark83ca6282015-06-10 09:31:09 -07001265 }
halcanary96fcdcc2015-08-27 07:41:13 -07001266 return nullptr;
caryclark83ca6282015-06-10 09:31:09 -07001267}
1268
1269#undef PORTABLE_FONT_PREFIX
1270
mbocee6a9912016-05-31 11:42:36 -07001271extern sk_sp<SkTypeface> (*gCreateTypefaceDelegate)(const char [], SkFontStyle );
caryclark83ca6282015-06-10 09:31:09 -07001272
Mike Kleinbe28ee22017-02-06 12:46:20 -05001273int main(int argc, char** argv) {
1274 SkCommandLineFlags::Parse(argc, argv);
Brian Salomondcbb9d92017-07-19 10:53:20 -04001275 if (FLAGS_trace) {
1276 SkAssertResult(SkEventTracer::SetInstance(new SkDebugfTracer()));
1277 }
1278 #if defined(SK_BUILD_FOR_IOS)
Mike Kleinadacaef2017-02-06 09:26:14 -05001279 cd_Documents();
1280#endif
bsalomon71de3532016-04-26 14:27:21 -07001281 setbuf(stdout, nullptr);
mtklein246ba3a2016-02-23 10:39:36 -08001282 setup_crash_handler();
mtkleinafae30a2016-02-24 12:28:32 -08001283
Yuqian Liba62b4a2016-12-14 13:46:53 -05001284 gSkUseAnalyticAA = FLAGS_analyticAA;
liyuqian38911a72016-10-04 11:23:22 -07001285
Yuqian Li550148b2017-01-13 10:13:13 -05001286 if (FLAGS_forceAnalyticAA) {
1287 gSkForceAnalyticAA = true;
1288 }
Mike Reed7c26ca72017-07-05 15:45:52 -04001289 if (FLAGS_forceRasterPipeline) {
1290 gSkForceRasterPipelineBlitter = true;
1291 }
Yuqian Li550148b2017-01-13 10:13:13 -05001292
Mike Kleine9f187b2017-06-26 13:23:12 -04001293 // The bots like having a verbose.log to upload, so always touch the file even if --verbose.
1294 if (!FLAGS_writePath.isEmpty()) {
mtklein51c8cfc2016-03-11 12:59:09 -08001295 sk_mkdir(FLAGS_writePath[0]);
mtkleina1ce2162016-07-22 12:23:46 -07001296 gVLog = fopen(SkOSPath::Join(FLAGS_writePath[0], "verbose.log").c_str(), "w");
mtkleinc41fd922016-03-08 09:01:39 -08001297 }
Mike Kleine9f187b2017-06-26 13:23:12 -04001298 if (FLAGS_verbose) {
1299 gVLog = stderr;
1300 }
mtkleinc41fd922016-03-08 09:01:39 -08001301
csmartdalton008b9d82017-02-22 12:00:42 -07001302 GrContextOptions grCtxOptions;
1303#if SK_SUPPORT_GPU
1304 grCtxOptions.fGpuPathRenderers = CollectGpuPathRenderersFromFlags();
1305#endif
1306
mtklein5286f022016-01-22 08:18:14 -08001307 JsonWriter::DumpJson(); // It's handy for the bots to assume this is ~never missing.
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +00001308 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -07001309 SkTaskGroup::Enabler enabled(FLAGS_threads);
caryclark83ca6282015-06-10 09:31:09 -07001310 gCreateTypefaceDelegate = &create_from_name;
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +00001311
halcanary7d571242016-02-24 17:59:16 -08001312 {
1313 SkString testResourcePath = GetResourcePath("color_wheel.png");
1314 SkFILEStream testResource(testResourcePath.c_str());
1315 if (!testResource.isValid()) {
mtkleinc41fd922016-03-08 09:01:39 -08001316 info("Some resources are missing. Do you need to set --resourcePath?\n");
halcanary7d571242016-02-24 17:59:16 -08001317 }
1318 }
mtklein62bd1a62015-01-27 14:46:26 -08001319 gather_gold();
borenet09ed4802015-04-03 14:15:33 -07001320 gather_uninteresting_hashes();
mtklein62bd1a62015-01-27 14:46:26 -08001321
scroggo86737142016-02-03 12:19:11 -08001322 if (!gather_srcs()) {
1323 return 1;
1324 }
csmartdalton008b9d82017-02-22 12:00:42 -07001325 if (!gather_sinks(grCtxOptions)) {
brianosman05c987c2016-05-09 13:42:16 -07001326 return 1;
1327 }
mtklein748ca3b2015-01-15 10:56:12 -08001328 gather_tests();
mtklein21eaf3b2016-02-08 12:39:59 -08001329 gPending = gSrcs.count() * gSinks.count() + gParallelTests.count() + gSerialTests.count();
mtkleinc41fd922016-03-08 09:01:39 -08001330 info("%d srcs * %d sinks + %d tests == %d tasks",
1331 gSrcs.count(), gSinks.count(), gParallelTests.count() + gSerialTests.count(), gPending);
mtklein748ca3b2015-01-15 10:56:12 -08001332
mtklein21eaf3b2016-02-08 12:39:59 -08001333 // Kick off as much parallel work as we can, making note of any serial work we'll need to do.
1334 SkTaskGroup parallel;
1335 SkTArray<Task> serial;
1336
1337 for (auto& sink : gSinks)
1338 for (auto& src : gSrcs) {
mtklein3eed7dd2016-02-24 19:07:07 -08001339 if (src->veto(sink->flags()) ||
1340 is_blacklisted(sink.tag.c_str(), src.tag.c_str(),
1341 src.options.c_str(), src->name().c_str())) {
mtkleinf10637f2016-06-10 13:56:35 -07001342 SkAutoMutexAcquire lock(gMutex);
mtklein3eed7dd2016-02-24 19:07:07 -08001343 gPending--;
1344 continue;
1345 }
1346
mtklein21eaf3b2016-02-08 12:39:59 -08001347 Task task(src, sink);
1348 if (src->serial() || sink->serial()) {
1349 serial.push_back(task);
1350 } else {
1351 parallel.add([task] { Task::Run(task); });
mtklein748ca3b2015-01-15 10:56:12 -08001352 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001353 }
mtklein21eaf3b2016-02-08 12:39:59 -08001354 for (auto test : gParallelTests) {
csmartdalton008b9d82017-02-22 12:00:42 -07001355 parallel.add([test, grCtxOptions] { run_test(test, grCtxOptions); });
mtklein55e88b22015-01-21 15:50:13 -08001356 }
mtklein21eaf3b2016-02-08 12:39:59 -08001357
1358 // With the parallel work running, run serial tasks and tests here on main thread.
1359 for (auto task : serial) { Task::Run(task); }
csmartdalton008b9d82017-02-22 12:00:42 -07001360 for (auto test : gSerialTests) { run_test(test, grCtxOptions); }
mtklein21eaf3b2016-02-08 12:39:59 -08001361
1362 // Wait for any remaining parallel work to complete (including any spun off of serial tasks).
1363 parallel.wait();
mtkleincd50bca2016-01-05 06:20:20 -08001364 gDefinitelyThreadSafeWork.wait();
1365
mtkleinda884c42016-02-24 18:00:23 -08001366 // We'd better have run everything.
1367 SkASSERT(gPending == 0);
mtkleine027f172016-02-26 15:53:06 -08001368 // Make sure we've flushed all our results to disk.
1369 JsonWriter::DumpJson();
mtkleinda884c42016-02-24 18:00:23 -08001370
mtklein748ca3b2015-01-15 10:56:12 -08001371 // At this point we're back in single-threaded land.
caryclarkf53ce802015-06-15 06:48:30 -07001372 sk_tool_utils::release_portable_typefaces();
mtklein@google.comd36522d2013-10-16 13:02:15 +00001373
mtklein748ca3b2015-01-15 10:56:12 -08001374 if (gFailures.count() > 0) {
mtkleinc41fd922016-03-08 09:01:39 -08001375 info("Failures:\n");
mtklein748ca3b2015-01-15 10:56:12 -08001376 for (int i = 0; i < gFailures.count(); i++) {
mtkleinc41fd922016-03-08 09:01:39 -08001377 info("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001378 }
mtkleinc41fd922016-03-08 09:01:39 -08001379 info("%d failures\n", gFailures.count());
mtklein748ca3b2015-01-15 10:56:12 -08001380 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001381 }
mtkleinafae30a2016-02-24 12:28:32 -08001382
1383#ifdef SK_PDF_IMAGE_STATS
halcanary7a14b312015-10-01 07:28:13 -07001384 SkPDFImageDumpStats();
mtkleinafae30a2016-02-24 12:28:32 -08001385#endif // SK_PDF_IMAGE_STATS
1386
Herb Derby5a523fe2017-01-26 16:48:28 -05001387 SkGraphics::PurgeAllCaches();
mtkleinc41fd922016-03-08 09:01:39 -08001388 info("Finished!\n");
mtklein748ca3b2015-01-15 10:56:12 -08001389 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +00001390}
jcgregorio3b27ade2014-11-13 08:06:40 -08001391
kkinnunen179a8f52015-11-20 13:32:24 -08001392// TODO: currently many GPU tests are declared outside SK_SUPPORT_GPU guards.
1393// Thus we export the empty RunWithGPUTestContexts when SK_SUPPORT_GPU=0.
1394namespace skiatest {
bsalomon758586c2016-04-06 14:02:39 -07001395
1396#if SK_SUPPORT_GPU
1397bool IsGLContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1398 return kOpenGL_GrBackend == GrContextFactory::ContextTypeBackend(type);
1399}
bsalomondc0fcd42016-04-11 14:21:33 -07001400bool IsVulkanContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1401 return kVulkan_GrBackend == GrContextFactory::ContextTypeBackend(type);
1402}
bsalomon758586c2016-04-06 14:02:39 -07001403bool IsRenderingGLContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1404 return IsGLContextType(type) && GrContextFactory::IsRenderingContext(type);
1405}
1406bool IsNullGLContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1407 return type == GrContextFactory::kNullGL_ContextType;
1408}
1409#else
1410bool IsGLContextType(int) { return false; }
bsalomondc0fcd42016-04-11 14:21:33 -07001411bool IsVulkanContextType(int) { return false; }
bsalomon758586c2016-04-06 14:02:39 -07001412bool IsRenderingGLContextType(int) { return false; }
1413bool IsNullGLContextType(int) { return false; }
1414#endif
1415
1416void RunWithGPUTestContexts(GrContextTestFn* test, GrContextTypeFilterFn* contextTypeFilter,
bsalomonf2f1c172016-04-05 12:59:06 -07001417 Reporter* reporter, GrContextFactory* factory) {
kkinnunen179a8f52015-11-20 13:32:24 -08001418#if SK_SUPPORT_GPU
kkinnunen3e980c32015-12-23 01:33:00 -08001419
Brian Salomon6405e712017-03-20 08:54:16 -04001420#if defined(SK_BUILD_FOR_UNIX) || defined(SK_BUILD_FOR_WIN) || defined(SK_BUILD_FOR_MAC)
1421 static constexpr auto kNativeGLType = GrContextFactory::kGL_ContextType;
1422#else
1423 static constexpr auto kNativeGLType = GrContextFactory::kGLES_ContextType;
1424#endif
1425
bsalomon758586c2016-04-06 14:02:39 -07001426 for (int typeInt = 0; typeInt < GrContextFactory::kContextTypeCnt; ++typeInt) {
1427 GrContextFactory::ContextType contextType = (GrContextFactory::ContextType) typeInt;
Brian Osmanaa2219c2016-12-05 10:12:17 -05001428 // Use "native" instead of explicitly trying OpenGL and OpenGL ES. Do not use GLES on
bsalomon758586c2016-04-06 14:02:39 -07001429 // desktop since tests do not account for not fixing http://skbug.com/2809
1430 if (contextType == GrContextFactory::kGL_ContextType ||
1431 contextType == GrContextFactory::kGLES_ContextType) {
Brian Salomon6405e712017-03-20 08:54:16 -04001432 if (contextType != kNativeGLType) {
bsalomon758586c2016-04-06 14:02:39 -07001433 continue;
1434 }
1435 }
csmartdaltone812d492017-02-21 12:36:05 -07001436 ContextInfo ctxInfo = factory->getContextInfo(contextType,
1437 GrContextFactory::ContextOverrides::kDisableNVPR);
Brian Osmanaa2219c2016-12-05 10:12:17 -05001438 if (contextTypeFilter && !(*contextTypeFilter)(contextType)) {
1439 continue;
1440 }
Greg Daniela5cb7812017-06-16 09:45:32 -04001441 ReporterContext ctx(reporter, SkString(GrContextFactory::ContextTypeName(contextType)));
bsalomon8b7451a2016-05-11 06:33:06 -07001442 if (ctxInfo.grContext()) {
bsalomonf2f1c172016-04-05 12:59:06 -07001443 (*test)(reporter, ctxInfo);
kkinnunen5219fd92015-12-10 06:28:13 -08001444 }
bsalomonf2f1c172016-04-05 12:59:06 -07001445 ctxInfo = factory->getContextInfo(contextType,
csmartdaltone812d492017-02-21 12:36:05 -07001446 GrContextFactory::ContextOverrides::kRequireNVPRSupport);
bsalomon8b7451a2016-05-11 06:33:06 -07001447 if (ctxInfo.grContext()) {
bsalomonf2f1c172016-04-05 12:59:06 -07001448 (*test)(reporter, ctxInfo);
kkinnunen179a8f52015-11-20 13:32:24 -08001449 }
1450 }
1451#endif
1452}
kkinnunen179a8f52015-11-20 13:32:24 -08001453} // namespace skiatest