blob: 290f4eb35590dc60ce12e5b7cd9350cbbfde0416 [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"
Brian Osman53136aa2017-07-20 15:43:35 -040014#include "SkChromeTracingTracer.h"
scroggocc2feb12015-08-14 08:32:46 -070015#include "SkCodec.h"
mtklein27c3fdd2016-02-26 14:43:21 -080016#include "SkColorPriv.h"
msarett888dc162016-05-23 10:21:17 -070017#include "SkColorSpace.h"
Mike Klein919cc452017-03-18 15:36:52 +000018#include "SkColorSpacePriv.h"
caryclark17f0b6d2014-07-22 10:15:34 -070019#include "SkCommonFlags.h"
kkinnunen3e980c32015-12-23 01:33:00 -080020#include "SkCommonFlagsConfig.h"
csmartdalton008b9d82017-02-22 12:00:42 -070021#include "SkCommonFlagsPathRenderer.h"
brianosman3c579dc2016-04-19 09:18:11 -070022#include "SkData.h"
Hal Canaryc7d295e2017-07-20 15:36:00 -040023#include "SkDocument.h"
Brian Salomondcbb9d92017-07-19 10:53:20 -040024#include "SkDebugfTracer.h"
Brian Osman53136aa2017-07-20 15:43:35 -040025#include "SkEventTracingPriv.h"
caryclark83ca6282015-06-10 09:31:09 -070026#include "SkFontMgr.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +000027#include "SkGraphics.h"
mtklein27c3fdd2016-02-26 14:43:21 -080028#include "SkHalf.h"
halcanary4dbbd042016-06-07 17:21:10 -070029#include "SkLeanWindows.h"
mtklein748ca3b2015-01-15 10:56:12 -080030#include "SkMD5.h"
mtklein1b249332015-07-07 12:21:21 -070031#include "SkMutex.h"
mtklein1d0f1642014-09-08 08:05:18 -070032#include "SkOSFile.h"
Ben Wagnerbf111d72016-11-07 18:05:29 -050033#include "SkOSPath.h"
mtklein27c3fdd2016-02-26 14:43:21 -080034#include "SkPM4fPriv.h"
Mike Klein13b6afd2017-06-12 12:45:41 -040035#include "SkPngEncoder.h"
Brian Salomondcbb9d92017-07-19 10:53:20 -040036#include "SkScan.h"
mtklein246ba3a2016-02-23 10:39:36 -080037#include "SkSpinlock.h"
mtkleina82f5622015-02-20 12:30:19 -080038#include "SkTHash.h"
mtklein406654b2014-09-03 15:34:37 -070039#include "SkTaskGroup.h"
mtkleinde6fc2b2015-03-12 06:28:54 -070040#include "SkThreadUtils.h"
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +000041#include "Test.h"
mtklein748ca3b2015-01-15 10:56:12 -080042#include "Timer.h"
Mike Kleinadacaef2017-02-06 09:26:14 -050043#include "ios_utils.h"
brianosman3c579dc2016-04-19 09:18:11 -070044#include "picture_utils.h"
caryclark83ca6282015-06-10 09:31:09 -070045#include "sk_tool_utils.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +000046
scroggo19b91532016-10-24 09:03:26 -070047#include <vector>
48
halcanary7a14b312015-10-01 07:28:13 -070049#ifdef SK_PDF_IMAGE_STATS
50extern void SkPDFImageDumpStats();
51#endif
52
mtkleina5114d72015-08-24 13:27:01 -070053#include "png.h"
54
bungeman60e0fee2015-08-26 05:15:46 -070055#include <stdlib.h>
56
scroggo27a58342015-10-28 08:56:41 -070057#ifndef SK_BUILD_FOR_WIN32
58 #include <unistd.h>
59#endif
60
Mike Reed7c26ca72017-07-05 15:45:52 -040061extern bool gSkForceRasterPipelineBlitter;
62
djsollen54416de2015-04-03 07:24:48 -070063DEFINE_string(src, "tests gm skp image", "Source types to test.");
mtklein748ca3b2015-01-15 10:56:12 -080064DEFINE_bool(nameByHash, false,
65 "If true, write to FLAGS_writePath[0]/<hash>.png instead of "
djsollen54416de2015-04-03 07:24:48 -070066 "to FLAGS_writePath[0]/<config>/<sourceType>/<sourceOptions>/<name>.png");
mtklein748ca3b2015-01-15 10:56:12 -080067DEFINE_bool2(pathOpsExtended, x, false, "Run extended pathOps tests.");
mtkleind603b222015-02-17 11:13:33 -080068DEFINE_string(matrix, "1 0 0 1",
69 "2x2 scale+skew matrix to apply or upright when using "
70 "'matrix' or 'upright' in config.");
mtklein82d28432015-01-15 12:46:02 -080071DEFINE_bool(gpu_threading, false, "Allow GPU work to run on multiple threads?");
mtklein@google.comd36522d2013-10-16 13:02:15 +000072
mtkleina2ef6422015-01-15 13:44:22 -080073DEFINE_string(blacklist, "",
djsollen54416de2015-04-03 07:24:48 -070074 "Space-separated config/src/srcOptions/name quadruples to blacklist. '_' matches anything. E.g. \n"
75 "'--blacklist gpu skp _ _' will blacklist all SKPs drawn into the gpu config.\n"
76 "'--blacklist gpu skp _ _ 8888 gm _ aarects' will also blacklist the aarects GM on 8888.");
mtkleina2ef6422015-01-15 13:44:22 -080077
mtklein62bd1a62015-01-27 14:46:26 -080078DEFINE_string2(readPath, r, "", "If set check for equality with golden results in this directory.");
79
borenet09ed4802015-04-03 14:15:33 -070080DEFINE_string(uninterestingHashesFile, "",
81 "File containing a list of uninteresting hashes. If a result hashes to something in "
82 "this list, no image is written for that result.");
83
mtklein6393c062015-04-27 08:45:01 -070084DEFINE_int32(shards, 1, "We're splitting source data into this many shards.");
85DEFINE_int32(shard, 0, "Which shard do I run?");
86
halcanary45420a92016-06-02 12:41:14 -070087DEFINE_string(mskps, "", "Directory to read mskps from, or a single mskp file.");
Mike Reed7c26ca72017-07-05 15:45:52 -040088DEFINE_bool(forceRasterPipeline, false, "sets gSkForceRasterPipelineBlitter");
halcanary45420a92016-06-02 12:41:14 -070089
csmartdalton008b9d82017-02-22 12:00:42 -070090#if SK_SUPPORT_GPU
91DEFINE_pathrenderer_flag;
92#endif
93
Derek Sollenbergeredfe3df2017-07-19 15:25:24 -040094DEFINE_bool(ignoreSigInt, false, "ignore SIGINT signals during test execution");
95
mtklein748ca3b2015-01-15 10:56:12 -080096using namespace DM;
bsalomon3724e572016-03-30 18:56:19 -070097using sk_gpu_test::GrContextFactory;
bsalomon273c0f52016-03-31 10:59:06 -070098using sk_gpu_test::GLTestContext;
bsalomonf2f1c172016-04-05 12:59:06 -070099using sk_gpu_test::ContextInfo;
mtklein@google.comd36522d2013-10-16 13:02:15 +0000100
mtklein748ca3b2015-01-15 10:56:12 -0800101/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
102
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700103static const double kStartMs = SkTime::GetMSecs();
mtkleinc41fd922016-03-08 09:01:39 -0800104
105static FILE* gVLog;
106
107template <typename... Args>
108static void vlog(const char* fmt, Args&&... args) {
109 if (gVLog) {
110 fprintf(gVLog, "%s\t", HumanizeMs(SkTime::GetMSecs() - kStartMs).c_str());
111 fprintf(gVLog, fmt, args...);
112 fflush(gVLog);
113 }
114}
115
116template <typename... Args>
117static void info(const char* fmt, Args&&... args) {
118 vlog(fmt, args...);
119 if (!FLAGS_quiet) {
120 printf(fmt, args...);
121 }
122}
123static void info(const char* fmt) {
124 if (!FLAGS_quiet) {
125 printf("%s", fmt); // Clang warns printf(fmt) is insecure.
126 }
127}
128
reed086eea92016-05-04 17:12:46 -0700129SK_DECLARE_STATIC_MUTEX(gFailuresMutex);
mtklein748ca3b2015-01-15 10:56:12 -0800130static SkTArray<SkString> gFailures;
131
mtklein3eed7dd2016-02-24 19:07:07 -0800132static void fail(const SkString& err) {
mtklein748ca3b2015-01-15 10:56:12 -0800133 SkAutoMutexAcquire lock(gFailuresMutex);
134 SkDebugf("\n\nFAILURE: %s\n\n", err.c_str());
135 gFailures.push_back(err);
halcanary9e398f72015-01-10 11:18:04 -0800136}
137
mtklein0f7748a2016-07-25 15:27:29 -0700138struct Running {
139 SkString id;
140 SkThreadID thread;
141
142 void dump() const {
143 info("\t%s\n", id.c_str());
144 }
145};
mtkleinb37cb412015-03-18 05:27:14 -0700146
mtklein246ba3a2016-02-23 10:39:36 -0800147// We use a spinlock to make locking this in a signal handler _somewhat_ safe.
Mike Klein03a95ad2017-06-23 12:36:32 -0400148static SkSpinlock gMutex;
149static int gPending;
mtklein0f7748a2016-07-25 15:27:29 -0700150static SkTArray<Running> gRunning;
mtklein748ca3b2015-01-15 10:56:12 -0800151
mtklein3eed7dd2016-02-24 19:07:07 -0800152static void done(const char* config, const char* src, const char* srcOptions, const char* name) {
153 SkString id = SkStringPrintf("%s %s %s %s", config, src, srcOptions, name);
mtkleinc41fd922016-03-08 09:01:39 -0800154 vlog("done %s\n", id.c_str());
mtkleinafae30a2016-02-24 12:28:32 -0800155 int pending;
mtkleinb37cb412015-03-18 05:27:14 -0700156 {
mtkleinf10637f2016-06-10 13:56:35 -0700157 SkAutoMutexAcquire lock(gMutex);
mtkleinb37cb412015-03-18 05:27:14 -0700158 for (int i = 0; i < gRunning.count(); i++) {
mtklein0f7748a2016-07-25 15:27:29 -0700159 if (gRunning[i].id == id) {
mtkleinb37cb412015-03-18 05:27:14 -0700160 gRunning.removeShuffle(i);
161 break;
162 }
163 }
mtkleinafae30a2016-02-24 12:28:32 -0800164 pending = --gPending;
reed50bc0512015-05-19 14:13:31 -0700165 }
Mike Klein03a95ad2017-06-23 12:36:32 -0400166
167 // We write out dm.json file and print out a progress update every once in a while.
168 // Notice this also handles the final dm.json and progress update when pending == 0.
mtkleina17241b2015-01-23 05:48:00 -0800169 if (pending % 500 == 0) {
170 JsonWriter::DumpJson();
Mike Klein03a95ad2017-06-23 12:36:32 -0400171
172 int curr = sk_tools::getCurrResidentSetSizeMB(),
173 peak = sk_tools::getMaxResidentSetSizeMB();
174 SkString elapsed = HumanizeMs(SkTime::GetMSecs() - kStartMs);
175
176 SkAutoMutexAcquire lock(gMutex);
177 info("\n%dMB RAM, %dMB peak, %s elapsed, %d queued, %d active:\n",
178 curr, peak, elapsed.c_str(), gPending - gRunning.count(), gRunning.count());
179 for (auto& task : gRunning) {
180 task.dump();
181 }
mtkleina17241b2015-01-23 05:48:00 -0800182 }
mtklein@google.comd36522d2013-10-16 13:02:15 +0000183}
184
mtklein3eed7dd2016-02-24 19:07:07 -0800185static void start(const char* config, const char* src, const char* srcOptions, const char* name) {
186 SkString id = SkStringPrintf("%s %s %s %s", config, src, srcOptions, name);
mtkleinc41fd922016-03-08 09:01:39 -0800187 vlog("start %s\n", id.c_str());
mtkleinf10637f2016-06-10 13:56:35 -0700188 SkAutoMutexAcquire lock(gMutex);
mtklein0f7748a2016-07-25 15:27:29 -0700189 gRunning.push_back({id,SkGetThreadID()});
mtkleinb37cb412015-03-18 05:27:14 -0700190}
191
Mike Klein3cc2d202016-10-24 11:39:43 -0400192static void find_culprit() {
193 // Assumes gMutex is locked.
194 SkThreadID thisThread = SkGetThreadID();
195 for (auto& task : gRunning) {
196 if (task.thread == thisThread) {
197 info("Likely culprit:\n");
198 task.dump();
mtklein0f7748a2016-07-25 15:27:29 -0700199 }
200 }
Mike Klein3cc2d202016-10-24 11:39:43 -0400201}
mtklein0f7748a2016-07-25 15:27:29 -0700202
mtklein246ba3a2016-02-23 10:39:36 -0800203#if defined(SK_BUILD_FOR_WIN32)
mtklein5b64dab2016-06-09 08:59:48 -0700204 static LONG WINAPI crash_handler(EXCEPTION_POINTERS* e) {
mtkleinf8557292016-02-29 06:35:28 -0800205 static const struct {
206 const char* name;
robertphillips75467862016-03-01 14:10:23 -0800207 DWORD code;
mtkleinf8557292016-02-29 06:35:28 -0800208 } kExceptions[] = {
209 #define _(E) {#E, E}
210 _(EXCEPTION_ACCESS_VIOLATION),
211 _(EXCEPTION_BREAKPOINT),
212 _(EXCEPTION_INT_DIVIDE_BY_ZERO),
213 _(EXCEPTION_STACK_OVERFLOW),
214 // TODO: more?
215 #undef _
216 };
217
mtkleinf10637f2016-06-10 13:56:35 -0700218 SkAutoMutexAcquire lock(gMutex);
mtklein5b64dab2016-06-09 08:59:48 -0700219
220 const DWORD code = e->ExceptionRecord->ExceptionCode;
221 info("\nCaught exception %u", code);
222 for (const auto& exception : kExceptions) {
223 if (exception.code == code) {
224 info(" %s", exception.name);
mtkleinf8557292016-02-29 06:35:28 -0800225 }
mtkleinf8557292016-02-29 06:35:28 -0800226 }
mtklein5b64dab2016-06-09 08:59:48 -0700227 info(", was running:\n");
228 for (auto& task : gRunning) {
mtklein0f7748a2016-07-25 15:27:29 -0700229 task.dump();
mtklein5b64dab2016-06-09 08:59:48 -0700230 }
mtklein0f7748a2016-07-25 15:27:29 -0700231 find_culprit();
mtklein5b64dab2016-06-09 08:59:48 -0700232 fflush(stdout);
233
mtkleinf8557292016-02-29 06:35:28 -0800234 // Execute default exception handler... hopefully, exit.
235 return EXCEPTION_EXECUTE_HANDLER;
mtklein246ba3a2016-02-23 10:39:36 -0800236 }
237
Mike Klein3cc2d202016-10-24 11:39:43 -0400238 static void setup_crash_handler() {
239 SetUnhandledExceptionFilter(crash_handler);
240 }
241#else
mtklein246ba3a2016-02-23 10:39:36 -0800242 #include <signal.h>
Mike Klein3cc2d202016-10-24 11:39:43 -0400243 #if !defined(SK_BUILD_FOR_ANDROID)
244 #include <execinfo.h>
Brian Salomondcbb9d92017-07-19 10:53:20 -0400245
246#endif
Mike Klein3cc2d202016-10-24 11:39:43 -0400247
248 static constexpr int max_of() { return 0; }
249 template <typename... Rest>
250 static constexpr int max_of(int x, Rest... rest) {
251 return x > max_of(rest...) ? x : max_of(rest...);
252 }
253
254 static void (*previous_handler[max_of(SIGABRT,SIGBUS,SIGFPE,SIGILL,SIGSEGV)+1])(int);
mtkleinf8557292016-02-29 06:35:28 -0800255
mtklein5b64dab2016-06-09 08:59:48 -0700256 static void crash_handler(int sig) {
mtkleinf10637f2016-06-10 13:56:35 -0700257 SkAutoMutexAcquire lock(gMutex);
mtklein5b64dab2016-06-09 08:59:48 -0700258
259 info("\nCaught signal %d [%s], was running:\n", sig, strsignal(sig));
260 for (auto& task : gRunning) {
mtklein0f7748a2016-07-25 15:27:29 -0700261 task.dump();
mtklein5b64dab2016-06-09 08:59:48 -0700262 }
mtklein0f7748a2016-07-25 15:27:29 -0700263 find_culprit();
mtklein5b64dab2016-06-09 08:59:48 -0700264
Mike Klein3cc2d202016-10-24 11:39:43 -0400265 #if !defined(SK_BUILD_FOR_ANDROID)
mtklein5b64dab2016-06-09 08:59:48 -0700266 void* stack[64];
267 int count = backtrace(stack, SK_ARRAY_COUNT(stack));
268 char** symbols = backtrace_symbols(stack, count);
269 info("\nStack trace:\n");
270 for (int i = 0; i < count; i++) {
271 info(" %s\n", symbols[i]);
272 }
Mike Klein3cc2d202016-10-24 11:39:43 -0400273 #endif
mtklein5b64dab2016-06-09 08:59:48 -0700274 fflush(stdout);
275
Mike Klein3cc2d202016-10-24 11:39:43 -0400276 signal(sig, previous_handler[sig]);
277 raise(sig);
mtklein5b64dab2016-06-09 08:59:48 -0700278 }
279
mtklein246ba3a2016-02-23 10:39:36 -0800280 static void setup_crash_handler() {
281 const int kSignals[] = { SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGSEGV };
282 for (int sig : kSignals) {
Mike Klein3cc2d202016-10-24 11:39:43 -0400283 previous_handler[sig] = signal(sig, crash_handler);
mtklein246ba3a2016-02-23 10:39:36 -0800284 }
Derek Sollenbergeredfe3df2017-07-19 15:25:24 -0400285
286 if (FLAGS_ignoreSigInt) {
287 signal(SIGINT, SIG_IGN);
288 }
mtklein246ba3a2016-02-23 10:39:36 -0800289 }
290#endif
291
mtklein748ca3b2015-01-15 10:56:12 -0800292/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
mtklein114c3cd2015-01-15 10:15:02 -0800293
mtklein62bd1a62015-01-27 14:46:26 -0800294struct Gold : public SkString {
mtkleina82f5622015-02-20 12:30:19 -0800295 Gold() : SkString("") {}
mtklein3eed7dd2016-02-24 19:07:07 -0800296 Gold(const SkString& sink, const SkString& src,
297 const SkString& srcOptions, const SkString& name,
298 const SkString& md5)
mtklein62bd1a62015-01-27 14:46:26 -0800299 : SkString("") {
300 this->append(sink);
301 this->append(src);
djsollen54416de2015-04-03 07:24:48 -0700302 this->append(srcOptions);
mtklein62bd1a62015-01-27 14:46:26 -0800303 this->append(name);
304 this->append(md5);
mtklein62bd1a62015-01-27 14:46:26 -0800305 }
mtkleinc8d1dd42015-10-15 12:23:01 -0700306 struct Hash {
307 uint32_t operator()(const Gold& g) const {
308 return SkGoodHash()((const SkString&)g);
309 }
310 };
mtklein62bd1a62015-01-27 14:46:26 -0800311};
mtkleina82f5622015-02-20 12:30:19 -0800312static SkTHashSet<Gold, Gold::Hash> gGold;
mtklein62bd1a62015-01-27 14:46:26 -0800313
314static void add_gold(JsonWriter::BitmapResult r) {
djsollen54416de2015-04-03 07:24:48 -0700315 gGold.add(Gold(r.config, r.sourceType, r.sourceOptions, r.name, r.md5));
mtklein62bd1a62015-01-27 14:46:26 -0800316}
317
318static void gather_gold() {
319 if (!FLAGS_readPath.isEmpty()) {
320 SkString path(FLAGS_readPath[0]);
321 path.append("/dm.json");
322 if (!JsonWriter::ReadJson(path.c_str(), add_gold)) {
323 fail(SkStringPrintf("Couldn't read %s for golden results.", path.c_str()));
324 }
325 }
326}
327
328/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
329
mtklein935f1b12016-03-16 08:37:19 -0700330#if defined(SK_BUILD_FOR_WIN32)
331 static const char* kNewline = "\r\n";
332#else
333 static const char* kNewline = "\n";
334#endif
335
borenet09ed4802015-04-03 14:15:33 -0700336static SkTHashSet<SkString> gUninterestingHashes;
337
338static void gather_uninteresting_hashes() {
339 if (!FLAGS_uninterestingHashesFile.isEmpty()) {
bungeman38d909e2016-08-02 14:40:46 -0700340 sk_sp<SkData> data(SkData::MakeFromFileName(FLAGS_uninterestingHashesFile[0]));
mtkleincc334b32015-09-22 11:43:53 -0700341 if (!data) {
mtkleinc41fd922016-03-08 09:01:39 -0800342 info("WARNING: unable to read uninteresting hashes from %s\n",
343 FLAGS_uninterestingHashesFile[0]);
mtkleincc334b32015-09-22 11:43:53 -0700344 return;
345 }
Mike Klein45dcc0c2017-04-05 18:04:31 -0400346
347 // Copy to a string to make sure SkStrSplit has a terminating \0 to find.
348 SkString contents((const char*)data->data(), data->size());
349
borenet09ed4802015-04-03 14:15:33 -0700350 SkTArray<SkString> hashes;
Mike Klein45dcc0c2017-04-05 18:04:31 -0400351 SkStrSplit(contents.c_str(), kNewline, &hashes);
borenet09ed4802015-04-03 14:15:33 -0700352 for (const SkString& hash : hashes) {
353 gUninterestingHashes.add(hash);
354 }
mtkleinc41fd922016-03-08 09:01:39 -0800355 info("FYI: loaded %d distinct uninteresting hashes from %d lines\n",
356 gUninterestingHashes.count(), hashes.count());
borenet09ed4802015-04-03 14:15:33 -0700357 }
358}
359
360/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
361
Ben Wagner145dbcd2016-11-03 14:40:50 -0400362struct TaggedSrc : public std::unique_ptr<Src> {
mtklein3eed7dd2016-02-24 19:07:07 -0800363 SkString tag;
364 SkString options;
mtkleine0effd62015-07-29 06:37:28 -0700365};
366
Ben Wagner145dbcd2016-11-03 14:40:50 -0400367struct TaggedSink : public std::unique_ptr<Sink> {
kkinnunen3e980c32015-12-23 01:33:00 -0800368 SkString tag;
djsollen54416de2015-04-03 07:24:48 -0700369};
mtklein748ca3b2015-01-15 10:56:12 -0800370
371static const bool kMemcpyOK = true;
372
mtkleine0effd62015-07-29 06:37:28 -0700373static SkTArray<TaggedSrc, kMemcpyOK> gSrcs;
374static SkTArray<TaggedSink, kMemcpyOK> gSinks;
mtklein748ca3b2015-01-15 10:56:12 -0800375
mtklein6393c062015-04-27 08:45:01 -0700376static bool in_shard() {
377 static int N = 0;
378 return N++ % FLAGS_shards == FLAGS_shard;
379}
380
mtklein3eed7dd2016-02-24 19:07:07 -0800381static void push_src(const char* tag, ImplicitString options, Src* s) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400382 std::unique_ptr<Src> src(s);
mtklein6393c062015-04-27 08:45:01 -0700383 if (in_shard() &&
mtklein3eed7dd2016-02-24 19:07:07 -0800384 FLAGS_src.contains(tag) &&
mtklein748ca3b2015-01-15 10:56:12 -0800385 !SkCommandLineFlags::ShouldSkip(FLAGS_match, src->name().c_str())) {
mtkleine0effd62015-07-29 06:37:28 -0700386 TaggedSrc& s = gSrcs.push_back();
mtklein18300a32016-03-16 13:53:35 -0700387 s.reset(src.release());
mtklein748ca3b2015-01-15 10:56:12 -0800388 s.tag = tag;
djsollen54416de2015-04-03 07:24:48 -0700389 s.options = options;
mtklein114c3cd2015-01-15 10:15:02 -0800390 }
mtklein748ca3b2015-01-15 10:56:12 -0800391}
mtklein114c3cd2015-01-15 10:15:02 -0800392
msarett9e707a02015-09-01 14:57:57 -0700393static void push_codec_src(Path path, CodecSrc::Mode mode, CodecSrc::DstColorType dstColorType,
scroggoc5560be2016-02-03 09:42:42 -0800394 SkAlphaType dstAlphaType, float scale) {
scroggoe4499842016-02-25 11:03:47 -0800395 if (FLAGS_simpleCodec) {
scroggo19b91532016-10-24 09:03:26 -0700396 const bool simple = CodecSrc::kCodec_Mode == mode || CodecSrc::kAnimated_Mode == mode;
397 if (!simple || dstColorType != CodecSrc::kGetFromCanvas_DstColorType || scale != 1.0f) {
scroggoe4499842016-02-25 11:03:47 -0800398 // Only decode in the simple case.
399 return;
scroggo19b91532016-10-24 09:03:26 -0700400 }
scroggoe4499842016-02-25 11:03:47 -0800401 }
msarett9e707a02015-09-01 14:57:57 -0700402 SkString folder;
403 switch (mode) {
404 case CodecSrc::kCodec_Mode:
405 folder.append("codec");
406 break;
msarettbb25b532016-01-13 09:31:39 -0800407 case CodecSrc::kCodecZeroInit_Mode:
408 folder.append("codec_zero_init");
409 break;
msarett9e707a02015-09-01 14:57:57 -0700410 case CodecSrc::kScanline_Mode:
411 folder.append("scanline");
412 break;
msarett9e707a02015-09-01 14:57:57 -0700413 case CodecSrc::kStripe_Mode:
414 folder.append("stripe");
415 break;
msarett91c22b22016-02-22 12:27:46 -0800416 case CodecSrc::kCroppedScanline_Mode:
417 folder.append("crop");
418 break;
msarett9e707a02015-09-01 14:57:57 -0700419 case CodecSrc::kSubset_Mode:
msarett36c37962015-09-02 13:20:52 -0700420 folder.append("codec_subset");
msarett9e707a02015-09-01 14:57:57 -0700421 break;
scroggo19b91532016-10-24 09:03:26 -0700422 case CodecSrc::kAnimated_Mode:
423 folder.append("codec_animated");
424 break;
msarett9e707a02015-09-01 14:57:57 -0700425 }
426
427 switch (dstColorType) {
428 case CodecSrc::kGrayscale_Always_DstColorType:
429 folder.append("_kGray8");
430 break;
msarett34e0ec42016-04-22 16:27:24 -0700431 case CodecSrc::kNonNative8888_Always_DstColorType:
432 folder.append("_kNonNative");
433 break;
msarett9e707a02015-09-01 14:57:57 -0700434 default:
435 break;
436 }
437
scroggoc5560be2016-02-03 09:42:42 -0800438 switch (dstAlphaType) {
scroggoc5560be2016-02-03 09:42:42 -0800439 case kPremul_SkAlphaType:
440 folder.append("_premul");
441 break;
442 case kUnpremul_SkAlphaType:
443 folder.append("_unpremul");
444 break;
445 default:
446 break;
447 }
448
msarett9e707a02015-09-01 14:57:57 -0700449 if (1.0f != scale) {
450 folder.appendf("_%.3f", scale);
451 }
452
scroggoc5560be2016-02-03 09:42:42 -0800453 CodecSrc* src = new CodecSrc(path, mode, dstColorType, dstAlphaType, scale);
msarett9e707a02015-09-01 14:57:57 -0700454 push_src("image", folder, src);
455}
456
scroggof8dc9df2016-05-16 09:04:13 -0700457static void push_android_codec_src(Path path, CodecSrc::DstColorType dstColorType,
458 SkAlphaType dstAlphaType, int sampleSize) {
msarett3d9d7a72015-10-21 10:27:10 -0700459 SkString folder;
scroggof8dc9df2016-05-16 09:04:13 -0700460 folder.append("scaled_codec");
msarett3d9d7a72015-10-21 10:27:10 -0700461
462 switch (dstColorType) {
463 case CodecSrc::kGrayscale_Always_DstColorType:
464 folder.append("_kGray8");
465 break;
msarett34e0ec42016-04-22 16:27:24 -0700466 case CodecSrc::kNonNative8888_Always_DstColorType:
467 folder.append("_kNonNative");
468 break;
msarett3d9d7a72015-10-21 10:27:10 -0700469 default:
470 break;
471 }
472
scroggoc5560be2016-02-03 09:42:42 -0800473 switch (dstAlphaType) {
scroggoc5560be2016-02-03 09:42:42 -0800474 case kPremul_SkAlphaType:
475 folder.append("_premul");
476 break;
msarett9e9444c2016-02-03 12:39:10 -0800477 case kUnpremul_SkAlphaType:
478 folder.append("_unpremul");
479 break;
scroggoc5560be2016-02-03 09:42:42 -0800480 default:
481 break;
482 }
483
msarett3d9d7a72015-10-21 10:27:10 -0700484 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800485 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700486 }
487
scroggof8dc9df2016-05-16 09:04:13 -0700488 AndroidCodecSrc* src = new AndroidCodecSrc(path, dstColorType, dstAlphaType, sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700489 push_src("image", folder, src);
490}
491
msarett18976312016-03-09 14:20:58 -0800492static void push_image_gen_src(Path path, ImageGenSrc::Mode mode, SkAlphaType alphaType, bool isGpu)
493{
494 SkString folder;
495 switch (mode) {
496 case ImageGenSrc::kCodec_Mode:
497 folder.append("gen_codec");
498 break;
499 case ImageGenSrc::kPlatform_Mode:
500 folder.append("gen_platform");
501 break;
502 }
503
504 if (isGpu) {
505 folder.append("_gpu");
506 } else {
507 switch (alphaType) {
508 case kOpaque_SkAlphaType:
509 folder.append("_opaque");
510 break;
511 case kPremul_SkAlphaType:
512 folder.append("_premul");
513 break;
514 case kUnpremul_SkAlphaType:
515 folder.append("_unpremul");
516 break;
517 default:
518 break;
519 }
520 }
521
522 ImageGenSrc* src = new ImageGenSrc(path, mode, alphaType, isGpu);
523 push_src("image", folder, src);
524}
525
msarett438b2ad2015-04-09 12:43:10 -0700526static void push_codec_srcs(Path path) {
bungeman38d909e2016-08-02 14:40:46 -0700527 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
msarett438b2ad2015-04-09 12:43:10 -0700528 if (!encoded) {
mtkleinc41fd922016-03-08 09:01:39 -0800529 info("Couldn't read %s.", path.c_str());
msarett438b2ad2015-04-09 12:43:10 -0700530 return;
531 }
Ben Wagner145dbcd2016-11-03 14:40:50 -0400532 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(encoded));
halcanary96fcdcc2015-08-27 07:41:13 -0700533 if (nullptr == codec.get()) {
mtkleinc41fd922016-03-08 09:01:39 -0800534 info("Couldn't create codec for %s.", path.c_str());
msarett438b2ad2015-04-09 12:43:10 -0700535 return;
536 }
537
scroggo9366aff2016-05-20 08:38:54 -0700538 // native scaling is only supported by WEBP and JPEG
539 bool supportsNativeScaling = false;
msarett438b2ad2015-04-09 12:43:10 -0700540
msarett91c22b22016-02-22 12:27:46 -0800541 SkTArray<CodecSrc::Mode> nativeModes;
542 nativeModes.push_back(CodecSrc::kCodec_Mode);
543 nativeModes.push_back(CodecSrc::kCodecZeroInit_Mode);
msarett91c22b22016-02-22 12:27:46 -0800544 switch (codec->getEncodedFormat()) {
Hal Canarydb683012016-11-23 08:55:18 -0700545 case SkEncodedImageFormat::kJPEG:
msarett91c22b22016-02-22 12:27:46 -0800546 nativeModes.push_back(CodecSrc::kScanline_Mode);
547 nativeModes.push_back(CodecSrc::kStripe_Mode);
548 nativeModes.push_back(CodecSrc::kCroppedScanline_Mode);
scroggo9366aff2016-05-20 08:38:54 -0700549 supportsNativeScaling = true;
msarett91c22b22016-02-22 12:27:46 -0800550 break;
Hal Canarydb683012016-11-23 08:55:18 -0700551 case SkEncodedImageFormat::kWEBP:
msarett91c22b22016-02-22 12:27:46 -0800552 nativeModes.push_back(CodecSrc::kSubset_Mode);
scroggo9366aff2016-05-20 08:38:54 -0700553 supportsNativeScaling = true;
msarett91c22b22016-02-22 12:27:46 -0800554 break;
Hal Canarydb683012016-11-23 08:55:18 -0700555 case SkEncodedImageFormat::kDNG:
msarettb65e6042016-02-23 05:37:25 -0800556 break;
msarett91c22b22016-02-22 12:27:46 -0800557 default:
558 nativeModes.push_back(CodecSrc::kScanline_Mode);
msarett91c22b22016-02-22 12:27:46 -0800559 break;
560 }
msarett9e707a02015-09-01 14:57:57 -0700561
msarett91c22b22016-02-22 12:27:46 -0800562 SkTArray<CodecSrc::DstColorType> colorTypes;
563 colorTypes.push_back(CodecSrc::kGetFromCanvas_DstColorType);
msarett34e0ec42016-04-22 16:27:24 -0700564 colorTypes.push_back(CodecSrc::kNonNative8888_Always_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700565 switch (codec->getInfo().colorType()) {
566 case kGray_8_SkColorType:
msarett91c22b22016-02-22 12:27:46 -0800567 colorTypes.push_back(CodecSrc::kGrayscale_Always_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700568 break;
569 default:
msarett36c37962015-09-02 13:20:52 -0700570 break;
571 }
msarett9e707a02015-09-01 14:57:57 -0700572
scroggoc5560be2016-02-03 09:42:42 -0800573 SkTArray<SkAlphaType> alphaModes;
574 alphaModes.push_back(kPremul_SkAlphaType);
scroggof2c96a22016-05-20 11:08:27 -0700575 if (codec->getInfo().alphaType() != kOpaque_SkAlphaType) {
576 alphaModes.push_back(kUnpremul_SkAlphaType);
scroggoc5560be2016-02-03 09:42:42 -0800577 }
msarettb714fb02016-01-22 14:46:42 -0800578
579 for (CodecSrc::Mode mode : nativeModes) {
scroggo9366aff2016-05-20 08:38:54 -0700580 for (CodecSrc::DstColorType colorType : colorTypes) {
581 for (SkAlphaType alphaType : alphaModes) {
scroggof2c96a22016-05-20 11:08:27 -0700582 // Only test kCroppedScanline_Mode when the alpha type is premul. The test is
scroggo9366aff2016-05-20 08:38:54 -0700583 // slow and won't be interestingly different with different alpha types.
584 if (CodecSrc::kCroppedScanline_Mode == mode &&
scroggof2c96a22016-05-20 11:08:27 -0700585 kPremul_SkAlphaType != alphaType) {
scroggo9366aff2016-05-20 08:38:54 -0700586 continue;
587 }
msarett91c22b22016-02-22 12:27:46 -0800588
scroggo9366aff2016-05-20 08:38:54 -0700589 push_codec_src(path, mode, colorType, alphaType, 1.0f);
msarettf3dc1882016-04-26 13:06:38 -0700590
scroggo9366aff2016-05-20 08:38:54 -0700591 // Skip kNonNative on different native scales. It won't be interestingly
592 // different.
593 if (supportsNativeScaling &&
594 CodecSrc::kNonNative8888_Always_DstColorType == colorType) {
595 // Native Scales
596 // SkJpegCodec natively supports scaling to the following:
597 for (auto scale : { 0.125f, 0.25f, 0.375f, 0.5f, 0.625f, 0.750f, 0.875f }) {
598 push_codec_src(path, mode, colorType, alphaType, scale);
599 }
scroggoc5560be2016-02-03 09:42:42 -0800600 }
msarett9e707a02015-09-01 14:57:57 -0700601 }
602 }
msarett0a242972015-06-11 14:27:27 -0700603 }
msarett36c37962015-09-02 13:20:52 -0700604
scroggo19b91532016-10-24 09:03:26 -0700605 {
606 std::vector<SkCodec::FrameInfo> frameInfos = codec->getFrameInfo();
607 if (frameInfos.size() > 1) {
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400608 for (auto dstCT : { CodecSrc::kNonNative8888_Always_DstColorType,
609 CodecSrc::kGetFromCanvas_DstColorType }) {
610 for (auto at : { kUnpremul_SkAlphaType, kPremul_SkAlphaType }) {
611 push_codec_src(path, CodecSrc::kAnimated_Mode, dstCT, at, 1.0f);
612 }
613 }
scroggo19b91532016-10-24 09:03:26 -0700614 }
615
616 }
617
scroggoe4499842016-02-25 11:03:47 -0800618 if (FLAGS_simpleCodec) {
619 return;
620 }
621
msarett3d9d7a72015-10-21 10:27:10 -0700622 const int sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
msarett36c37962015-09-02 13:20:52 -0700623
msarett3d9d7a72015-10-21 10:27:10 -0700624 for (int sampleSize : sampleSizes) {
msarett91c22b22016-02-22 12:27:46 -0800625 for (CodecSrc::DstColorType colorType : colorTypes) {
scroggoc5560be2016-02-03 09:42:42 -0800626 for (SkAlphaType alphaType : alphaModes) {
msarettf3dc1882016-04-26 13:06:38 -0700627 // We can exercise all of the kNonNative support code in the swizzler with just a
628 // few sample sizes. Skip the rest.
629 if (CodecSrc::kNonNative8888_Always_DstColorType == colorType && sampleSize > 3) {
630 continue;
631 }
632
scroggof8dc9df2016-05-16 09:04:13 -0700633 push_android_codec_src(path, colorType, alphaType, sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700634 }
msarett36c37962015-09-02 13:20:52 -0700635 }
636 }
msarett18976312016-03-09 14:20:58 -0800637
638 static const char* const rawExts[] = {
639 "arw", "cr2", "dng", "nef", "nrw", "orf", "raf", "rw2", "pef", "srw",
640 "ARW", "CR2", "DNG", "NEF", "NRW", "ORF", "RAF", "RW2", "PEF", "SRW",
641 };
642
643 // There is not currently a reason to test RAW images on image generator.
644 // If we want to enable these tests, we will need to fix skbug.com/5079.
645 for (const char* ext : rawExts) {
646 if (path.endsWith(ext)) {
647 return;
648 }
649 }
650
651 // Push image generator GPU test.
brianosmanc9ced392016-03-24 07:27:43 -0700652 push_image_gen_src(path, ImageGenSrc::kCodec_Mode, codec->getInfo().alphaType(), true);
msarett18976312016-03-09 14:20:58 -0800653
654 // Push image generator CPU tests.
655 for (SkAlphaType alphaType : alphaModes) {
656 push_image_gen_src(path, ImageGenSrc::kCodec_Mode, alphaType, false);
657
658#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
Hal Canarydb683012016-11-23 08:55:18 -0700659 if (SkEncodedImageFormat::kWEBP != codec->getEncodedFormat() &&
660 SkEncodedImageFormat::kWBMP != codec->getEncodedFormat() &&
msarett18976312016-03-09 14:20:58 -0800661 kUnpremul_SkAlphaType != alphaType)
662 {
663 push_image_gen_src(path, ImageGenSrc::kPlatform_Mode, alphaType, false);
664 }
msarettfc0b6d12016-03-17 13:50:17 -0700665#elif defined(SK_BUILD_FOR_WIN)
Hal Canarydb683012016-11-23 08:55:18 -0700666 if (SkEncodedImageFormat::kWEBP != codec->getEncodedFormat() &&
667 SkEncodedImageFormat::kWBMP != codec->getEncodedFormat())
msarettfc0b6d12016-03-17 13:50:17 -0700668 {
669 push_image_gen_src(path, ImageGenSrc::kPlatform_Mode, alphaType, false);
670 }
msarett18976312016-03-09 14:20:58 -0800671#endif
672 }
msarett438b2ad2015-04-09 12:43:10 -0700673}
674
msarettd1227a72016-05-18 06:23:57 -0700675static void push_brd_src(Path path, CodecSrc::DstColorType dstColorType, BRDSrc::Mode mode,
676 uint32_t sampleSize) {
677 SkString folder("brd_android_codec");
msaretta5783ae2015-09-08 15:35:32 -0700678 switch (mode) {
679 case BRDSrc::kFullImage_Mode:
680 break;
681 case BRDSrc::kDivisor_Mode:
682 folder.append("_divisor");
683 break;
684 default:
685 SkASSERT(false);
686 return;
687 }
688
689 switch (dstColorType) {
690 case CodecSrc::kGetFromCanvas_DstColorType:
691 break;
msaretta5783ae2015-09-08 15:35:32 -0700692 case CodecSrc::kGrayscale_Always_DstColorType:
693 folder.append("_kGray");
694 break;
695 default:
696 SkASSERT(false);
697 return;
698 }
699
700 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800701 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700702 }
703
msarettd1227a72016-05-18 06:23:57 -0700704 BRDSrc* src = new BRDSrc(path, mode, dstColorType, sampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700705 push_src("image", folder, src);
706}
707
708static void push_brd_srcs(Path path) {
Leon Scroggins IIIbace7a92017-07-05 11:39:27 -0400709 // Only run grayscale to one sampleSize and Mode. Though interesting
710 // to test grayscale, it should not reveal anything across various
scroggo24a9bd72016-05-20 06:02:42 -0700711 // sampleSizes and Modes
Leon Scroggins IIIbace7a92017-07-05 11:39:27 -0400712 // Arbitrarily choose Mode and sampleSize.
713 push_brd_src(path, CodecSrc::kGrayscale_Always_DstColorType, BRDSrc::kFullImage_Mode, 2);
scroggo24a9bd72016-05-20 06:02:42 -0700714
715
scroggo501b7342015-11-03 07:55:11 -0800716 // Test on a variety of sampleSizes, making sure to include:
717 // - 2, 4, and 8, which are natively supported by jpeg
718 // - multiples of 2 which are not divisible by 4 (analogous for 4)
719 // - larger powers of two, since BRD clients generally use powers of 2
720 // We will only produce output for the larger sizes on large images.
721 const uint32_t sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 24, 32, 64 };
msarett6efbe052015-09-11 09:01:16 -0700722
msaretta5783ae2015-09-08 15:35:32 -0700723 const BRDSrc::Mode modes[] = {
msarett26ad17b2015-10-22 07:29:19 -0700724 BRDSrc::kFullImage_Mode,
725 BRDSrc::kDivisor_Mode,
msaretta5783ae2015-09-08 15:35:32 -0700726 };
727
msarettd1227a72016-05-18 06:23:57 -0700728 for (uint32_t sampleSize : sampleSizes) {
scroggo24a9bd72016-05-20 06:02:42 -0700729 for (BRDSrc::Mode mode : modes) {
730 push_brd_src(path, CodecSrc::kGetFromCanvas_DstColorType, mode, sampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700731 }
732 }
733}
734
735static bool brd_supported(const char* ext) {
msarett8c8f22a2015-04-01 06:58:48 -0700736 static const char* const exts[] = {
msaretta5783ae2015-09-08 15:35:32 -0700737 "jpg", "jpeg", "png", "webp",
738 "JPG", "JPEG", "PNG", "WEBP",
msarett8c8f22a2015-04-01 06:58:48 -0700739 };
740
741 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
742 if (0 == strcmp(exts[i], ext)) {
743 return true;
744 }
745 }
746 return false;
scroggo9b77ddd2015-03-19 06:03:39 -0700747}
748
fmalitaa2b9fdf2016-08-03 19:53:36 -0700749template <typename T>
750void gather_file_srcs(const SkCommandLineFlags::StringArray& flags, const char* ext) {
751 for (int i = 0; i < flags.count(); i++) {
752 const char* path = flags[i];
753 if (sk_isdir(path)) {
754 SkOSFile::Iter it(path, ext);
755 for (SkString file; it.next(&file); ) {
756 push_src(ext, "", new T(SkOSPath::Join(path, file.c_str())));
757 }
758 } else {
759 push_src(ext, "", new T(path));
760 }
761 }
762}
763
scroggo86737142016-02-03 12:19:11 -0800764static bool gather_srcs() {
mtklein748ca3b2015-01-15 10:56:12 -0800765 for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) {
djsollen54416de2015-04-03 07:24:48 -0700766 push_src("gm", "", new GMSrc(r->factory()));
mtklein748ca3b2015-01-15 10:56:12 -0800767 }
scroggo86737142016-02-03 12:19:11 -0800768
fmalitaa2b9fdf2016-08-03 19:53:36 -0700769 gather_file_srcs<SKPSrc>(FLAGS_skps, "skp");
770 gather_file_srcs<MSKPSrc>(FLAGS_mskps, "mskp");
771#if defined(SK_XML)
772 gather_file_srcs<SVGSrc>(FLAGS_svgs, "svg");
773#endif
halcanary45420a92016-06-02 12:41:14 -0700774
scroggo86737142016-02-03 12:19:11 -0800775 SkTArray<SkString> images;
msarett69deca82016-04-29 09:38:40 -0700776 if (!CollectImages(FLAGS_images, &images)) {
scroggo86737142016-02-03 12:19:11 -0800777 return false;
778 }
779
780 for (auto image : images) {
781 push_codec_srcs(image);
scroggoe4499842016-02-25 11:03:47 -0800782 if (FLAGS_simpleCodec) {
783 continue;
784 }
785
mtklein21eaf3b2016-02-08 12:39:59 -0800786 const char* ext = strrchr(image.c_str(), '.');
787 if (ext && brd_supported(ext+1)) {
scroggo86737142016-02-03 12:19:11 -0800788 push_brd_srcs(image);
mtklein709d2c32015-01-15 08:30:25 -0800789 }
mtklein709d2c32015-01-15 08:30:25 -0800790 }
scroggo86737142016-02-03 12:19:11 -0800791
msarett69deca82016-04-29 09:38:40 -0700792 SkTArray<SkString> colorImages;
793 if (!CollectImages(FLAGS_colorImages, &colorImages)) {
794 return false;
795 }
796
797 for (auto colorImage : colorImages) {
msarett9ce3a542016-07-15 13:54:38 -0700798 ColorCodecSrc* src = new ColorCodecSrc(colorImage, ColorCodecSrc::kBaseline_Mode,
799 kN32_SkColorType);
msarettb8d1aac2016-08-04 12:22:06 -0700800 push_src("colorImage", "color_codec_baseline", src);
msarett888dc162016-05-23 10:21:17 -0700801
msarett9ce3a542016-07-15 13:54:38 -0700802 src = new ColorCodecSrc(colorImage, ColorCodecSrc::kDst_HPZR30w_Mode, kN32_SkColorType);
msarettb8d1aac2016-08-04 12:22:06 -0700803 push_src("colorImage", "color_codec_HPZR30w", src);
msarett9ce3a542016-07-15 13:54:38 -0700804 // TODO (msarett):
805 // 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 -0700806
msarett9ce3a542016-07-15 13:54:38 -0700807 src = new ColorCodecSrc(colorImage, ColorCodecSrc::kDst_sRGB_Mode, kN32_SkColorType);
msarettb8d1aac2016-08-04 12:22:06 -0700808 push_src("colorImage", "color_codec_sRGB_kN32", src);
msarett9ce3a542016-07-15 13:54:38 -0700809 src = new ColorCodecSrc(colorImage, ColorCodecSrc::kDst_sRGB_Mode, kRGBA_F16_SkColorType);
msarettb8d1aac2016-08-04 12:22:06 -0700810 push_src("colorImage", "color_codec_sRGB_kF16", src);
msarett69deca82016-04-29 09:38:40 -0700811 }
812
scroggo86737142016-02-03 12:19:11 -0800813 return true;
mtklein709d2c32015-01-15 08:30:25 -0800814}
815
kkinnunen3e980c32015-12-23 01:33:00 -0800816static void push_sink(const SkCommandLineConfig& config, Sink* s) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400817 std::unique_ptr<Sink> sink(s);
kkinnunen3e980c32015-12-23 01:33:00 -0800818
mtkleine0effd62015-07-29 06:37:28 -0700819 // Try a simple Src as a canary. If it fails, skip this sink.
mtklein748ca3b2015-01-15 10:56:12 -0800820 struct : public Src {
mtkleine0effd62015-07-29 06:37:28 -0700821 Error draw(SkCanvas* c) const override {
822 c->drawRect(SkRect::MakeWH(1,1), SkPaint());
823 return "";
824 }
mtklein36352bf2015-03-25 18:17:31 -0700825 SkISize size() const override { return SkISize::Make(16, 16); }
mtkleine0effd62015-07-29 06:37:28 -0700826 Name name() const override { return "justOneRect"; }
827 } justOneRect;
mtklein114c3cd2015-01-15 10:15:02 -0800828
mtklein748ca3b2015-01-15 10:56:12 -0800829 SkBitmap bitmap;
830 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800831 SkString log;
mtkleine0effd62015-07-29 06:37:28 -0700832 Error err = sink->draw(justOneRect, &bitmap, &stream, &log);
mtklein4089ef72015-03-05 08:40:28 -0800833 if (err.isFatal()) {
mtkleinc41fd922016-03-08 09:01:39 -0800834 info("Could not run %s: %s\n", config.getTag().c_str(), err.c_str());
mtklein05641a52015-04-21 10:49:13 -0700835 exit(1);
mtklein114c3cd2015-01-15 10:15:02 -0800836 }
837
mtkleine0effd62015-07-29 06:37:28 -0700838 TaggedSink& ts = gSinks.push_back();
mtklein18300a32016-03-16 13:53:35 -0700839 ts.reset(sink.release());
kkinnunen3e980c32015-12-23 01:33:00 -0800840 ts.tag = config.getTag();
mtklein748ca3b2015-01-15 10:56:12 -0800841}
842
843static bool gpu_supported() {
844#if SK_SUPPORT_GPU
845 return FLAGS_gpu;
846#else
847 return false;
848#endif
849}
kkinnunen9ebc3f02015-12-21 23:48:13 -0800850
csmartdalton008b9d82017-02-22 12:00:42 -0700851static Sink* create_sink(const GrContextOptions& grCtxOptions, const SkCommandLineConfig* config) {
kkinnunen3e980c32015-12-23 01:33:00 -0800852#if SK_SUPPORT_GPU
853 if (gpu_supported()) {
854 if (const SkCommandLineConfigGpu* gpuConfig = config->asConfigGpu()) {
bsalomon85b4b532016-04-05 11:06:27 -0700855 GrContextFactory::ContextType contextType = gpuConfig->getContextType();
csmartdaltone812d492017-02-21 12:36:05 -0700856 GrContextFactory::ContextOverrides contextOverrides = gpuConfig->getContextOverrides();
csmartdalton008b9d82017-02-22 12:00:42 -0700857 GrContextFactory testFactory(grCtxOptions);
csmartdaltone812d492017-02-21 12:36:05 -0700858 if (!testFactory.get(contextType, contextOverrides)) {
mtkleinc41fd922016-03-08 09:01:39 -0800859 info("WARNING: can not create GPU context for config '%s'. "
860 "GM tests will be skipped.\n", gpuConfig->getTag().c_str());
kkinnunen3e980c32015-12-23 01:33:00 -0800861 return nullptr;
862 }
csmartdaltone812d492017-02-21 12:36:05 -0700863 return new GPUSink(contextType, contextOverrides, gpuConfig->getSamples(),
brianosmand93c1202016-03-10 07:49:08 -0800864 gpuConfig->getUseDIText(), gpuConfig->getColorType(),
Brian Salomonce5ee602017-07-17 11:31:31 -0400865 gpuConfig->getAlphaType(), sk_ref_sp(gpuConfig->getColorSpace()),
866 FLAGS_gpu_threading);
kkinnunen3e980c32015-12-23 01:33:00 -0800867 }
868 }
869#endif
870
871#define SINK(t, sink, ...) if (config->getBackend().equals(t)) { return new sink(__VA_ARGS__); }
872
mtklein748ca3b2015-01-15 10:56:12 -0800873 if (FLAGS_cpu) {
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500874 auto srgbColorSpace = SkColorSpace::MakeSRGB();
875 auto srgbLinearColorSpace = SkColorSpace::MakeSRGBLinear();
brianosmanb109b8c2016-06-16 13:03:24 -0700876
Matt Sarettd2228302017-03-02 08:53:46 -0500877 SINK("565", RasterSink, kRGB_565_SkColorType);
878 SINK("8888", RasterSink, kN32_SkColorType);
879 SINK("srgb", RasterSink, kN32_SkColorType, srgbColorSpace);
Matt Sarettd2228302017-03-02 08:53:46 -0500880 SINK("f16", RasterSink, kRGBA_F16_SkColorType, srgbLinearColorSpace);
Hal Canaryc7d295e2017-07-20 15:36:00 -0400881 SINK("pdf", PDFSink, false, SK_ScalarDefaultRasterDPI);
Matt Sarettd2228302017-03-02 08:53:46 -0500882 SINK("skp", SKPSink);
883 SINK("pipe", PipeSink);
884 SINK("svg", SVGSink);
885 SINK("null", NullSink);
886 SINK("xps", XPSSink);
Hal Canaryc7d295e2017-07-20 15:36:00 -0400887 SINK("pdfa", PDFSink, true, SK_ScalarDefaultRasterDPI);
888 SINK("pdf300", PDFSink, false, 300);
Hal Canary85c7fe82016-10-25 10:33:27 -0400889 SINK("jsdebug", DebugSink);
mtklein748ca3b2015-01-15 10:56:12 -0800890 }
891#undef SINK
halcanary96fcdcc2015-08-27 07:41:13 -0700892 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800893}
894
Mike Klein841101d2017-03-10 09:55:51 -0500895static sk_sp<SkColorSpace> adobe_rgb() {
896 return SkColorSpace::MakeRGB(SkColorSpace::kSRGB_RenderTargetGamma,
897 SkColorSpace::kAdobeRGB_Gamut);
898}
899
Mike Klein919cc452017-03-18 15:36:52 +0000900static sk_sp<SkColorSpace> rgb_to_gbr() {
901 float gbr[9];
902 gbr[0] = gSRGB_toXYZD50[1];
903 gbr[1] = gSRGB_toXYZD50[2];
904 gbr[2] = gSRGB_toXYZD50[0];
905 gbr[3] = gSRGB_toXYZD50[4];
906 gbr[4] = gSRGB_toXYZD50[5];
907 gbr[5] = gSRGB_toXYZD50[3];
908 gbr[6] = gSRGB_toXYZD50[7];
909 gbr[7] = gSRGB_toXYZD50[8];
910 gbr[8] = gSRGB_toXYZD50[6];
911 SkMatrix44 toXYZD50(SkMatrix44::kUninitialized_Constructor);
912 toXYZD50.set3x3RowMajorf(gbr);
913 return SkColorSpace::MakeRGB(SkColorSpace::kSRGB_RenderTargetGamma, toXYZD50);
914}
915
kkinnunen3e980c32015-12-23 01:33:00 -0800916static Sink* create_via(const SkString& tag, Sink* wrapped) {
917#define VIA(t, via, ...) if (tag.equals(t)) { return new via(__VA_ARGS__); }
Mike Klein919cc452017-03-18 15:36:52 +0000918 VIA("adobe", ViaCSXform, wrapped, adobe_rgb(), false);
919 VIA("gbr", ViaCSXform, wrapped, rgb_to_gbr(), true);
mtklein9c5052f2016-08-06 12:51:51 -0700920 VIA("lite", ViaLite, wrapped);
reed54dc4872016-09-13 08:09:45 -0700921 VIA("pipe", ViaPipe, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700922 VIA("twice", ViaTwice, wrapped);
Mike Reedbae888e2017-02-18 16:50:45 -0500923#ifdef TEST_VIA_SVG
Mike Reedf67c4592017-02-17 17:06:11 -0500924 VIA("svg", ViaSVG, wrapped);
Mike Reedbae888e2017-02-18 16:50:45 -0500925#endif
halcanary96fcdcc2015-08-27 07:41:13 -0700926 VIA("serialize", ViaSerialization, wrapped);
mtklein4a34ecb2016-01-08 10:19:35 -0800927 VIA("pic", ViaPicture, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700928 VIA("2ndpic", ViaSecondPicture, wrapped);
mtkleind31c13d2015-05-05 12:59:56 -0700929 VIA("sp", ViaSingletonPictures, wrapped);
reedbabc3de2016-07-08 08:43:27 -0700930 VIA("defer", ViaDefer, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700931 VIA("tiles", ViaTiles, 256, 256, nullptr, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800932 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800933
mtkleind603b222015-02-17 11:13:33 -0800934 if (FLAGS_matrix.count() == 4) {
mtklein748ca3b2015-01-15 10:56:12 -0800935 SkMatrix m;
mtkleind603b222015-02-17 11:13:33 -0800936 m.reset();
937 m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
938 m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
939 m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
940 m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
941 VIA("matrix", ViaMatrix, m, wrapped);
942 VIA("upright", ViaUpright, m, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800943 }
tomhudson64de1e12015-03-05 08:01:07 -0800944
mtklein748ca3b2015-01-15 10:56:12 -0800945#undef VIA
halcanary96fcdcc2015-08-27 07:41:13 -0700946 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800947}
948
csmartdalton008b9d82017-02-22 12:00:42 -0700949static bool gather_sinks(const GrContextOptions& grCtxOptions) {
kkinnunen3e980c32015-12-23 01:33:00 -0800950 SkCommandLineConfigArray configs;
951 ParseConfigs(FLAGS_config, &configs);
952 for (int i = 0; i < configs.count(); i++) {
953 const SkCommandLineConfig& config = *configs[i];
csmartdalton008b9d82017-02-22 12:00:42 -0700954 Sink* sink = create_sink(grCtxOptions, &config);
kkinnunen3e980c32015-12-23 01:33:00 -0800955 if (sink == nullptr) {
mtkleinc41fd922016-03-08 09:01:39 -0800956 info("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
957 config.getTag().c_str());
kkinnunen3e980c32015-12-23 01:33:00 -0800958 continue;
959 }
mtklein748ca3b2015-01-15 10:56:12 -0800960
kkinnunen3e980c32015-12-23 01:33:00 -0800961 const SkTArray<SkString>& parts = config.getViaParts();
962 for (int j = parts.count(); j-- > 0;) {
963 const SkString& part = parts[j];
964 Sink* next = create_via(part, sink);
halcanary96fcdcc2015-08-27 07:41:13 -0700965 if (next == nullptr) {
mtkleinc41fd922016-03-08 09:01:39 -0800966 info("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
967 part.c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800968 delete sink;
halcanary96fcdcc2015-08-27 07:41:13 -0700969 sink = nullptr;
mtklein748ca3b2015-01-15 10:56:12 -0800970 break;
971 }
972 sink = next;
973 }
974 if (sink) {
975 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800976 }
977 }
brianosman05c987c2016-05-09 13:42:16 -0700978
979 // If no configs were requested (just running tests, perhaps?), then we're okay.
980 // Otherwise, make sure that at least one sink was constructed correctly. This catches
981 // the case of bots without a GPU being assigned GPU configs.
982 return (configs.count() == 0) || (gSinks.count() > 0);
mtklein114c3cd2015-01-15 10:15:02 -0800983}
mtklein709d2c32015-01-15 08:30:25 -0800984
mtkleina5114d72015-08-24 13:27:01 -0700985static bool dump_png(SkBitmap bitmap, const char* path, const char* md5) {
Mike Klein13b6afd2017-06-12 12:45:41 -0400986 SkPixmap pm;
987 if (!bitmap.peekPixels(&pm)) {
988 return false; // Ought to never happen... we're already read-back at this point.
mtkleina5114d72015-08-24 13:27:01 -0700989 }
Mike Klein13b6afd2017-06-12 12:45:41 -0400990 SkFILEWStream dst{path};
mtkleina5114d72015-08-24 13:27:01 -0700991
mtkleind69ece62015-09-10 10:37:44 -0700992 SkString description;
993 description.append("Key: ");
994 for (int i = 0; i < FLAGS_key.count(); i++) {
995 description.appendf("%s ", FLAGS_key[i]);
996 }
997 description.append("Properties: ");
998 for (int i = 0; i < FLAGS_properties.count(); i++) {
999 description.appendf("%s ", FLAGS_properties[i]);
1000 }
1001 description.appendf("MD5: %s", md5);
1002
Mike Klein13b6afd2017-06-12 12:45:41 -04001003 const char* comments[] = {
1004 "Author", "DM dump_png()",
1005 "Description", description.c_str(),
1006 };
1007 size_t lengths[] = {
1008 strlen(comments[0])+1, strlen(comments[1])+1,
1009 strlen(comments[2])+1, strlen(comments[3])+1,
1010 };
mtkleina5114d72015-08-24 13:27:01 -07001011
Mike Klein13b6afd2017-06-12 12:45:41 -04001012 SkPngEncoder::Options options;
1013 options.fComments = SkDataTable::MakeCopyArrays((const void**)comments, lengths, 4);
1014 options.fFilterFlags = SkPngEncoder::FilterFlag::kNone;
1015 options.fZLibLevel = 1;
1016 options.fUnpremulBehavior = pm.colorSpace() ? SkTransferFunctionBehavior::kRespect
1017 : SkTransferFunctionBehavior::kIgnore;
1018 return SkPngEncoder::Encode(&dst, pm, options);
mtkleina5114d72015-08-24 13:27:01 -07001019}
1020
mtkleina2ef6422015-01-15 13:44:22 -08001021static bool match(const char* needle, const char* haystack) {
halcanary96fcdcc2015-08-27 07:41:13 -07001022 return 0 == strcmp("_", needle) || nullptr != strstr(haystack, needle);
mtkleina2ef6422015-01-15 13:44:22 -08001023}
1024
mtklein3eed7dd2016-02-24 19:07:07 -08001025static bool is_blacklisted(const char* sink, const char* src,
1026 const char* srcOptions, const char* name) {
mtklein0d243ff2015-04-03 07:51:00 -07001027 for (int i = 0; i < FLAGS_blacklist.count() - 3; i += 4) {
mtkleina2ef6422015-01-15 13:44:22 -08001028 if (match(FLAGS_blacklist[i+0], sink) &&
djsollen54416de2015-04-03 07:24:48 -07001029 match(FLAGS_blacklist[i+1], src) &&
1030 match(FLAGS_blacklist[i+2], srcOptions) &&
1031 match(FLAGS_blacklist[i+3], name)) {
mtklein3eed7dd2016-02-24 19:07:07 -08001032 return true;
mtkleina2ef6422015-01-15 13:44:22 -08001033 }
1034 }
mtklein3eed7dd2016-02-24 19:07:07 -08001035 return false;
mtkleina2ef6422015-01-15 13:44:22 -08001036}
1037
mtkleincd50bca2016-01-05 06:20:20 -08001038// Even when a Task Sink reports to be non-threadsafe (e.g. GPU), we know things like
1039// .png encoding are definitely thread safe. This lets us offload that work to CPU threads.
1040static SkTaskGroup gDefinitelyThreadSafeWork;
1041
mtklein748ca3b2015-01-15 10:56:12 -08001042// The finest-grained unit of work we can run: draw a single Src into a single Sink,
1043// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
1044struct Task {
mtkleine0effd62015-07-29 06:37:28 -07001045 Task(const TaggedSrc& src, const TaggedSink& sink) : src(src), sink(sink) {}
1046 const TaggedSrc& src;
1047 const TaggedSink& sink;
mtklein748ca3b2015-01-15 10:56:12 -08001048
mtklein21eaf3b2016-02-08 12:39:59 -08001049 static void Run(const Task& task) {
1050 SkString name = task.src->name();
mtkleine0effd62015-07-29 06:37:28 -07001051
mtkleinb9eb4ac2015-02-02 18:26:03 -08001052 SkString log;
mtklein3eed7dd2016-02-24 19:07:07 -08001053 if (!FLAGS_dryRun) {
mtklein748ca3b2015-01-15 10:56:12 -08001054 SkBitmap bitmap;
1055 SkDynamicMemoryWStream stream;
mtklein3eed7dd2016-02-24 19:07:07 -08001056 start(task.sink.tag.c_str(), task.src.tag.c_str(),
1057 task.src.options.c_str(), name.c_str());
mtklein21eaf3b2016-02-08 12:39:59 -08001058 Error err = task.sink->draw(*task.src, &bitmap, &stream, &log);
mtkleinb3b13b72016-03-07 13:20:52 -08001059 if (!log.isEmpty()) {
mtkleinc41fd922016-03-08 09:01:39 -08001060 info("%s %s %s %s:\n%s\n", task.sink.tag.c_str()
1061 , task.src.tag.c_str()
1062 , task.src.options.c_str()
1063 , name.c_str()
1064 , log.c_str());
mtkleinb3b13b72016-03-07 13:20:52 -08001065 }
mtklein748ca3b2015-01-15 10:56:12 -08001066 if (!err.isEmpty()) {
mtklein4089ef72015-03-05 08:40:28 -08001067 if (err.isFatal()) {
djsollen54416de2015-04-03 07:24:48 -07001068 fail(SkStringPrintf("%s %s %s %s: %s",
mtklein21eaf3b2016-02-08 12:39:59 -08001069 task.sink.tag.c_str(),
1070 task.src.tag.c_str(),
1071 task.src.options.c_str(),
mtklein4089ef72015-03-05 08:40:28 -08001072 name.c_str(),
1073 err.c_str()));
mtkleinb37cb412015-03-18 05:27:14 -07001074 } else {
mtklein3eed7dd2016-02-24 19:07:07 -08001075 done(task.sink.tag.c_str(), task.src.tag.c_str(),
1076 task.src.options.c_str(), name.c_str());
mtkleinba6ada72016-01-21 09:39:35 -08001077 return;
mtklein4089ef72015-03-05 08:40:28 -08001078 }
mtklein748ca3b2015-01-15 10:56:12 -08001079 }
mtklein62bd1a62015-01-27 14:46:26 -08001080
mtkleincd50bca2016-01-05 06:20:20 -08001081 // We're likely switching threads here, so we must capture by value, [=] or [foo,bar].
Hal Canary0b833192017-03-14 14:35:02 -04001082 SkStreamAsset* data = stream.detachAsStream().release();
mtkleincd50bca2016-01-05 06:20:20 -08001083 gDefinitelyThreadSafeWork.add([task,name,bitmap,data]{
Ben Wagner145dbcd2016-11-03 14:40:50 -04001084 std::unique_ptr<SkStreamAsset> ownedData(data);
mtkleincd50bca2016-01-05 06:20:20 -08001085
mtkleincd50bca2016-01-05 06:20:20 -08001086 SkString md5;
1087 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
1088 SkMD5 hash;
1089 if (data->getLength()) {
1090 hash.writeStream(data, data->getLength());
1091 data->rewind();
mtklein38408462015-07-08 07:25:27 -07001092 } else {
mtkleincd50bca2016-01-05 06:20:20 -08001093 // If we're BGRA (Linux, Windows), swizzle over to RGBA (Mac, Android).
1094 // This helps eliminate multiple 0-pixel-diff hashes on gold.skia.org.
1095 // (Android's general slow speed breaks the tie arbitrarily in RGBA's favor.)
1096 // We might consider promoting 565 to RGBA too.
1097 if (bitmap.colorType() == kBGRA_8888_SkColorType) {
1098 SkBitmap swizzle;
Matt Sarett68b8e3d2017-04-28 11:15:22 -04001099 SkAssertResult(sk_tool_utils::copy_to(&swizzle, kRGBA_8888_SkColorType,
1100 bitmap));
mtkleincd50bca2016-01-05 06:20:20 -08001101 hash.write(swizzle.getPixels(), swizzle.getSize());
1102 } else {
1103 hash.write(bitmap.getPixels(), bitmap.getSize());
1104 }
1105 }
1106 SkMD5::Digest digest;
1107 hash.finish(digest);
1108 for (int i = 0; i < 16; i++) {
1109 md5.appendf("%02x", digest.data[i]);
mtklein38408462015-07-08 07:25:27 -07001110 }
mtklein62bd1a62015-01-27 14:46:26 -08001111 }
mtklein62bd1a62015-01-27 14:46:26 -08001112
mtkleincd50bca2016-01-05 06:20:20 -08001113 if (!FLAGS_readPath.isEmpty() &&
mtklein3eed7dd2016-02-24 19:07:07 -08001114 !gGold.contains(Gold(task.sink.tag, task.src.tag,
1115 task.src.options, name, md5))) {
mtkleincd50bca2016-01-05 06:20:20 -08001116 fail(SkStringPrintf("%s not found for %s %s %s %s in %s",
1117 md5.c_str(),
mtklein21eaf3b2016-02-08 12:39:59 -08001118 task.sink.tag.c_str(),
1119 task.src.tag.c_str(),
1120 task.src.options.c_str(),
mtkleincd50bca2016-01-05 06:20:20 -08001121 name.c_str(),
1122 FLAGS_readPath[0]));
mtklein748ca3b2015-01-15 10:56:12 -08001123 }
mtkleincd50bca2016-01-05 06:20:20 -08001124
1125 if (!FLAGS_writePath.isEmpty()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001126 const char* ext = task.sink->fileExtension();
mtkleincd50bca2016-01-05 06:20:20 -08001127 if (data->getLength()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001128 WriteToDisk(task, md5, ext, data, data->getLength(), nullptr);
mtkleincd50bca2016-01-05 06:20:20 -08001129 SkASSERT(bitmap.drawsNothing());
1130 } else if (!bitmap.drawsNothing()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001131 WriteToDisk(task, md5, ext, nullptr, 0, &bitmap);
mtkleincd50bca2016-01-05 06:20:20 -08001132 }
1133 }
1134 });
mtklein748ca3b2015-01-15 10:56:12 -08001135 }
mtklein3eed7dd2016-02-24 19:07:07 -08001136 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 -08001137 }
1138
1139 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -08001140 SkString md5,
1141 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -08001142 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -08001143 const SkBitmap* bitmap) {
mtklein409d4702016-02-29 07:38:01 -08001144 bool gammaCorrect = false;
1145 if (bitmap) {
Brian Osman7039f742016-11-01 15:56:16 -04001146 gammaCorrect = SkToBool(bitmap->info().colorSpace());
mtklein409d4702016-02-29 07:38:01 -08001147 }
1148
mtklein748ca3b2015-01-15 10:56:12 -08001149 JsonWriter::BitmapResult result;
djsollen54416de2015-04-03 07:24:48 -07001150 result.name = task.src->name();
mtklein3eed7dd2016-02-24 19:07:07 -08001151 result.config = task.sink.tag;
djsollen54416de2015-04-03 07:24:48 -07001152 result.sourceType = task.src.tag;
1153 result.sourceOptions = task.src.options;
1154 result.ext = ext;
mtklein409d4702016-02-29 07:38:01 -08001155 result.gammaCorrect = gammaCorrect;
djsollen54416de2015-04-03 07:24:48 -07001156 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -08001157 JsonWriter::AddBitmapResult(result);
1158
mtkleinb0531a72015-04-07 13:38:48 -07001159 // If an MD5 is uninteresting, we want it noted in the JSON file,
1160 // but don't want to dump it out as a .png (or whatever ext is).
1161 if (gUninterestingHashes.contains(md5)) {
1162 return;
1163 }
1164
mtklein748ca3b2015-01-15 10:56:12 -08001165 const char* dir = FLAGS_writePath[0];
1166 if (0 == strcmp(dir, "@")) { // Needed for iOS.
1167 dir = FLAGS_resourcePath[0];
1168 }
1169 sk_mkdir(dir);
1170
1171 SkString path;
1172 if (FLAGS_nameByHash) {
1173 path = SkOSPath::Join(dir, result.md5.c_str());
1174 path.append(".");
1175 path.append(ext);
1176 if (sk_exists(path.c_str())) {
1177 return; // Content-addressed. If it exists already, we're done.
1178 }
1179 } else {
kkinnunen3e980c32015-12-23 01:33:00 -08001180 path = SkOSPath::Join(dir, task.sink.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001181 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001182 path = SkOSPath::Join(path.c_str(), task.src.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001183 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001184 if (strcmp(task.src.options.c_str(), "") != 0) {
1185 path = SkOSPath::Join(path.c_str(), task.src.options.c_str());
djsollen54416de2015-04-03 07:24:48 -07001186 sk_mkdir(path.c_str());
1187 }
mtklein748ca3b2015-01-15 10:56:12 -08001188 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
1189 path.append(".");
1190 path.append(ext);
1191 }
1192
mtklein748ca3b2015-01-15 10:56:12 -08001193 if (bitmap) {
mtkleina5114d72015-08-24 13:27:01 -07001194 if (!dump_png(*bitmap, path.c_str(), result.md5.c_str())) {
mtklein748ca3b2015-01-15 10:56:12 -08001195 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
1196 return;
1197 }
1198 } else {
mtkleina5114d72015-08-24 13:27:01 -07001199 SkFILEWStream file(path.c_str());
1200 if (!file.isValid()) {
1201 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
1202 return;
1203 }
mtklein748ca3b2015-01-15 10:56:12 -08001204 if (!file.writeStream(data, len)) {
1205 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
1206 return;
1207 }
1208 }
1209 }
1210};
1211
mtklein748ca3b2015-01-15 10:56:12 -08001212/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1213
1214// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
1215
mtklein21eaf3b2016-02-08 12:39:59 -08001216static SkTDArray<skiatest::Test> gParallelTests, gSerialTests;
mtklein748ca3b2015-01-15 10:56:12 -08001217
1218static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -08001219 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -08001220 return;
1221 }
mtklein6393c062015-04-27 08:45:01 -07001222 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r; r = r->next()) {
1223 if (!in_shard()) {
1224 continue;
1225 }
halcanary87f3ba42015-01-20 09:30:20 -08001226 // Despite its name, factory() is returning a reference to
1227 // link-time static const POD data.
1228 const skiatest::Test& test = r->factory();
1229 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -08001230 continue;
1231 }
halcanary87f3ba42015-01-20 09:30:20 -08001232 if (test.needsGpu && gpu_supported()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001233 (FLAGS_gpu_threading ? gParallelTests : gSerialTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -08001234 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein21eaf3b2016-02-08 12:39:59 -08001235 gParallelTests.push(test);
mtklein82d28432015-01-15 12:46:02 -08001236 }
mtklein748ca3b2015-01-15 10:56:12 -08001237 }
1238}
1239
csmartdalton008b9d82017-02-22 12:00:42 -07001240static void run_test(skiatest::Test test, const GrContextOptions& grCtxOptions) {
halcanary87f3ba42015-01-20 09:30:20 -08001241 struct : public skiatest::Reporter {
mtklein36352bf2015-03-25 18:17:31 -07001242 void reportFailed(const skiatest::Failure& failure) override {
halcanary87f3ba42015-01-20 09:30:20 -08001243 fail(failure.toString());
1244 JsonWriter::AddTestFailure(failure);
1245 }
mtklein36352bf2015-03-25 18:17:31 -07001246 bool allowExtendedTest() const override {
halcanary87f3ba42015-01-20 09:30:20 -08001247 return FLAGS_pathOpsExtended;
1248 }
mtklein36352bf2015-03-25 18:17:31 -07001249 bool verbose() const override { return FLAGS_veryVerbose; }
halcanary87f3ba42015-01-20 09:30:20 -08001250 } reporter;
djsollen824996a2015-06-12 12:06:22 -07001251
mtklein3eed7dd2016-02-24 19:07:07 -08001252 if (!FLAGS_dryRun && !is_blacklisted("_", "tests", "_", test.name)) {
mtklein21eaf3b2016-02-08 12:39:59 -08001253 start("unit", "test", "", test.name);
csmartdalton008b9d82017-02-22 12:00:42 -07001254 GrContextFactory factory(grCtxOptions);
mtklein21eaf3b2016-02-08 12:39:59 -08001255 test.proc(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -08001256 }
mtklein3eed7dd2016-02-24 19:07:07 -08001257 done("unit", "test", "", test.name);
mtklein748ca3b2015-01-15 10:56:12 -08001258}
1259
1260/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1261
caryclark83ca6282015-06-10 09:31:09 -07001262#define PORTABLE_FONT_PREFIX "Toy Liberation "
1263
mbocee6a9912016-05-31 11:42:36 -07001264static sk_sp<SkTypeface> create_from_name(const char familyName[], SkFontStyle style) {
caryclark83ca6282015-06-10 09:31:09 -07001265 if (familyName && strlen(familyName) > sizeof(PORTABLE_FONT_PREFIX)
1266 && !strncmp(familyName, PORTABLE_FONT_PREFIX, sizeof(PORTABLE_FONT_PREFIX) - 1)) {
caryclark1818acb2015-07-24 12:09:25 -07001267 return sk_tool_utils::create_portable_typeface(familyName, style);
caryclark83ca6282015-06-10 09:31:09 -07001268 }
halcanary96fcdcc2015-08-27 07:41:13 -07001269 return nullptr;
caryclark83ca6282015-06-10 09:31:09 -07001270}
1271
1272#undef PORTABLE_FONT_PREFIX
1273
mbocee6a9912016-05-31 11:42:36 -07001274extern sk_sp<SkTypeface> (*gCreateTypefaceDelegate)(const char [], SkFontStyle );
caryclark83ca6282015-06-10 09:31:09 -07001275
Mike Kleinbe28ee22017-02-06 12:46:20 -05001276int main(int argc, char** argv) {
1277 SkCommandLineFlags::Parse(argc, argv);
Brian Osman53136aa2017-07-20 15:43:35 -04001278
1279 initializeEventTracingForTools(&FLAGS_threads);
1280
1281#if defined(SK_BUILD_FOR_IOS)
Mike Kleinadacaef2017-02-06 09:26:14 -05001282 cd_Documents();
1283#endif
bsalomon71de3532016-04-26 14:27:21 -07001284 setbuf(stdout, nullptr);
mtklein246ba3a2016-02-23 10:39:36 -08001285 setup_crash_handler();
mtkleinafae30a2016-02-24 12:28:32 -08001286
Yuqian Liba62b4a2016-12-14 13:46:53 -05001287 gSkUseAnalyticAA = FLAGS_analyticAA;
liyuqian38911a72016-10-04 11:23:22 -07001288
Yuqian Li550148b2017-01-13 10:13:13 -05001289 if (FLAGS_forceAnalyticAA) {
1290 gSkForceAnalyticAA = true;
1291 }
Mike Reed7c26ca72017-07-05 15:45:52 -04001292 if (FLAGS_forceRasterPipeline) {
1293 gSkForceRasterPipelineBlitter = true;
1294 }
Yuqian Li550148b2017-01-13 10:13:13 -05001295
Mike Kleine9f187b2017-06-26 13:23:12 -04001296 // The bots like having a verbose.log to upload, so always touch the file even if --verbose.
1297 if (!FLAGS_writePath.isEmpty()) {
mtklein51c8cfc2016-03-11 12:59:09 -08001298 sk_mkdir(FLAGS_writePath[0]);
mtkleina1ce2162016-07-22 12:23:46 -07001299 gVLog = fopen(SkOSPath::Join(FLAGS_writePath[0], "verbose.log").c_str(), "w");
mtkleinc41fd922016-03-08 09:01:39 -08001300 }
Mike Kleine9f187b2017-06-26 13:23:12 -04001301 if (FLAGS_verbose) {
1302 gVLog = stderr;
1303 }
mtkleinc41fd922016-03-08 09:01:39 -08001304
csmartdalton008b9d82017-02-22 12:00:42 -07001305 GrContextOptions grCtxOptions;
1306#if SK_SUPPORT_GPU
1307 grCtxOptions.fGpuPathRenderers = CollectGpuPathRenderersFromFlags();
1308#endif
1309
mtklein5286f022016-01-22 08:18:14 -08001310 JsonWriter::DumpJson(); // It's handy for the bots to assume this is ~never missing.
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +00001311 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -07001312 SkTaskGroup::Enabler enabled(FLAGS_threads);
caryclark83ca6282015-06-10 09:31:09 -07001313 gCreateTypefaceDelegate = &create_from_name;
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +00001314
halcanary7d571242016-02-24 17:59:16 -08001315 {
1316 SkString testResourcePath = GetResourcePath("color_wheel.png");
1317 SkFILEStream testResource(testResourcePath.c_str());
1318 if (!testResource.isValid()) {
mtkleinc41fd922016-03-08 09:01:39 -08001319 info("Some resources are missing. Do you need to set --resourcePath?\n");
halcanary7d571242016-02-24 17:59:16 -08001320 }
1321 }
mtklein62bd1a62015-01-27 14:46:26 -08001322 gather_gold();
borenet09ed4802015-04-03 14:15:33 -07001323 gather_uninteresting_hashes();
mtklein62bd1a62015-01-27 14:46:26 -08001324
scroggo86737142016-02-03 12:19:11 -08001325 if (!gather_srcs()) {
1326 return 1;
1327 }
csmartdalton008b9d82017-02-22 12:00:42 -07001328 if (!gather_sinks(grCtxOptions)) {
brianosman05c987c2016-05-09 13:42:16 -07001329 return 1;
1330 }
mtklein748ca3b2015-01-15 10:56:12 -08001331 gather_tests();
mtklein21eaf3b2016-02-08 12:39:59 -08001332 gPending = gSrcs.count() * gSinks.count() + gParallelTests.count() + gSerialTests.count();
mtkleinc41fd922016-03-08 09:01:39 -08001333 info("%d srcs * %d sinks + %d tests == %d tasks",
1334 gSrcs.count(), gSinks.count(), gParallelTests.count() + gSerialTests.count(), gPending);
mtklein748ca3b2015-01-15 10:56:12 -08001335
mtklein21eaf3b2016-02-08 12:39:59 -08001336 // Kick off as much parallel work as we can, making note of any serial work we'll need to do.
1337 SkTaskGroup parallel;
1338 SkTArray<Task> serial;
1339
1340 for (auto& sink : gSinks)
1341 for (auto& src : gSrcs) {
mtklein3eed7dd2016-02-24 19:07:07 -08001342 if (src->veto(sink->flags()) ||
1343 is_blacklisted(sink.tag.c_str(), src.tag.c_str(),
1344 src.options.c_str(), src->name().c_str())) {
mtkleinf10637f2016-06-10 13:56:35 -07001345 SkAutoMutexAcquire lock(gMutex);
mtklein3eed7dd2016-02-24 19:07:07 -08001346 gPending--;
1347 continue;
1348 }
1349
mtklein21eaf3b2016-02-08 12:39:59 -08001350 Task task(src, sink);
1351 if (src->serial() || sink->serial()) {
1352 serial.push_back(task);
1353 } else {
1354 parallel.add([task] { Task::Run(task); });
mtklein748ca3b2015-01-15 10:56:12 -08001355 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001356 }
mtklein21eaf3b2016-02-08 12:39:59 -08001357 for (auto test : gParallelTests) {
csmartdalton008b9d82017-02-22 12:00:42 -07001358 parallel.add([test, grCtxOptions] { run_test(test, grCtxOptions); });
mtklein55e88b22015-01-21 15:50:13 -08001359 }
mtklein21eaf3b2016-02-08 12:39:59 -08001360
1361 // With the parallel work running, run serial tasks and tests here on main thread.
1362 for (auto task : serial) { Task::Run(task); }
csmartdalton008b9d82017-02-22 12:00:42 -07001363 for (auto test : gSerialTests) { run_test(test, grCtxOptions); }
mtklein21eaf3b2016-02-08 12:39:59 -08001364
1365 // Wait for any remaining parallel work to complete (including any spun off of serial tasks).
1366 parallel.wait();
mtkleincd50bca2016-01-05 06:20:20 -08001367 gDefinitelyThreadSafeWork.wait();
1368
mtkleinda884c42016-02-24 18:00:23 -08001369 // We'd better have run everything.
1370 SkASSERT(gPending == 0);
mtkleine027f172016-02-26 15:53:06 -08001371 // Make sure we've flushed all our results to disk.
1372 JsonWriter::DumpJson();
mtkleinda884c42016-02-24 18:00:23 -08001373
mtklein748ca3b2015-01-15 10:56:12 -08001374 // At this point we're back in single-threaded land.
caryclarkf53ce802015-06-15 06:48:30 -07001375 sk_tool_utils::release_portable_typefaces();
mtklein@google.comd36522d2013-10-16 13:02:15 +00001376
mtklein748ca3b2015-01-15 10:56:12 -08001377 if (gFailures.count() > 0) {
mtkleinc41fd922016-03-08 09:01:39 -08001378 info("Failures:\n");
mtklein748ca3b2015-01-15 10:56:12 -08001379 for (int i = 0; i < gFailures.count(); i++) {
mtkleinc41fd922016-03-08 09:01:39 -08001380 info("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001381 }
mtkleinc41fd922016-03-08 09:01:39 -08001382 info("%d failures\n", gFailures.count());
mtklein748ca3b2015-01-15 10:56:12 -08001383 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001384 }
mtkleinafae30a2016-02-24 12:28:32 -08001385
1386#ifdef SK_PDF_IMAGE_STATS
halcanary7a14b312015-10-01 07:28:13 -07001387 SkPDFImageDumpStats();
mtkleinafae30a2016-02-24 12:28:32 -08001388#endif // SK_PDF_IMAGE_STATS
1389
Herb Derby5a523fe2017-01-26 16:48:28 -05001390 SkGraphics::PurgeAllCaches();
mtkleinc41fd922016-03-08 09:01:39 -08001391 info("Finished!\n");
Brian Osman53136aa2017-07-20 15:43:35 -04001392
mtklein748ca3b2015-01-15 10:56:12 -08001393 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +00001394}
jcgregorio3b27ade2014-11-13 08:06:40 -08001395
kkinnunen179a8f52015-11-20 13:32:24 -08001396// TODO: currently many GPU tests are declared outside SK_SUPPORT_GPU guards.
1397// Thus we export the empty RunWithGPUTestContexts when SK_SUPPORT_GPU=0.
1398namespace skiatest {
bsalomon758586c2016-04-06 14:02:39 -07001399
1400#if SK_SUPPORT_GPU
1401bool IsGLContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1402 return kOpenGL_GrBackend == GrContextFactory::ContextTypeBackend(type);
1403}
bsalomondc0fcd42016-04-11 14:21:33 -07001404bool IsVulkanContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1405 return kVulkan_GrBackend == GrContextFactory::ContextTypeBackend(type);
1406}
bsalomon758586c2016-04-06 14:02:39 -07001407bool IsRenderingGLContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1408 return IsGLContextType(type) && GrContextFactory::IsRenderingContext(type);
1409}
1410bool IsNullGLContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1411 return type == GrContextFactory::kNullGL_ContextType;
1412}
1413#else
1414bool IsGLContextType(int) { return false; }
bsalomondc0fcd42016-04-11 14:21:33 -07001415bool IsVulkanContextType(int) { return false; }
bsalomon758586c2016-04-06 14:02:39 -07001416bool IsRenderingGLContextType(int) { return false; }
1417bool IsNullGLContextType(int) { return false; }
1418#endif
1419
1420void RunWithGPUTestContexts(GrContextTestFn* test, GrContextTypeFilterFn* contextTypeFilter,
bsalomonf2f1c172016-04-05 12:59:06 -07001421 Reporter* reporter, GrContextFactory* factory) {
kkinnunen179a8f52015-11-20 13:32:24 -08001422#if SK_SUPPORT_GPU
kkinnunen3e980c32015-12-23 01:33:00 -08001423
Brian Salomon6405e712017-03-20 08:54:16 -04001424#if defined(SK_BUILD_FOR_UNIX) || defined(SK_BUILD_FOR_WIN) || defined(SK_BUILD_FOR_MAC)
1425 static constexpr auto kNativeGLType = GrContextFactory::kGL_ContextType;
1426#else
1427 static constexpr auto kNativeGLType = GrContextFactory::kGLES_ContextType;
1428#endif
1429
bsalomon758586c2016-04-06 14:02:39 -07001430 for (int typeInt = 0; typeInt < GrContextFactory::kContextTypeCnt; ++typeInt) {
1431 GrContextFactory::ContextType contextType = (GrContextFactory::ContextType) typeInt;
Brian Osmanaa2219c2016-12-05 10:12:17 -05001432 // Use "native" instead of explicitly trying OpenGL and OpenGL ES. Do not use GLES on
bsalomon758586c2016-04-06 14:02:39 -07001433 // desktop since tests do not account for not fixing http://skbug.com/2809
1434 if (contextType == GrContextFactory::kGL_ContextType ||
1435 contextType == GrContextFactory::kGLES_ContextType) {
Brian Salomon6405e712017-03-20 08:54:16 -04001436 if (contextType != kNativeGLType) {
bsalomon758586c2016-04-06 14:02:39 -07001437 continue;
1438 }
1439 }
csmartdaltone812d492017-02-21 12:36:05 -07001440 ContextInfo ctxInfo = factory->getContextInfo(contextType,
1441 GrContextFactory::ContextOverrides::kDisableNVPR);
Brian Osmanaa2219c2016-12-05 10:12:17 -05001442 if (contextTypeFilter && !(*contextTypeFilter)(contextType)) {
1443 continue;
1444 }
Greg Daniela5cb7812017-06-16 09:45:32 -04001445 ReporterContext ctx(reporter, SkString(GrContextFactory::ContextTypeName(contextType)));
bsalomon8b7451a2016-05-11 06:33:06 -07001446 if (ctxInfo.grContext()) {
bsalomonf2f1c172016-04-05 12:59:06 -07001447 (*test)(reporter, ctxInfo);
kkinnunen5219fd92015-12-10 06:28:13 -08001448 }
bsalomonf2f1c172016-04-05 12:59:06 -07001449 ctxInfo = factory->getContextInfo(contextType,
csmartdaltone812d492017-02-21 12:36:05 -07001450 GrContextFactory::ContextOverrides::kRequireNVPRSupport);
bsalomon8b7451a2016-05-11 06:33:06 -07001451 if (ctxInfo.grContext()) {
bsalomonf2f1c172016-04-05 12:59:06 -07001452 (*test)(reporter, ctxInfo);
kkinnunen179a8f52015-11-20 13:32:24 -08001453 }
1454 }
1455#endif
1456}
kkinnunen179a8f52015-11-20 13:32:24 -08001457} // namespace skiatest