blob: f1c4a4ca7483d71ea6f940cf273a28f89efb3cf9 [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
Mike Klein31868212017-11-06 12:02:47 -05008#include "DMFontMgr.h"
mtklein748ca3b2015-01-15 10:56:12 -08009#include "DMJsonWriter.h"
10#include "DMSrcSink.h"
mtklein748ca3b2015-01-15 10:56:12 -080011#include "ProcStats.h"
halcanary7d571242016-02-24 17:59:16 -080012#include "Resources.h"
mtklein748ca3b2015-01-15 10:56:12 -080013#include "SkBBHFactory.h"
mtklein62bd1a62015-01-27 14:46:26 -080014#include "SkChecksum.h"
Brian Osman53136aa2017-07-20 15:43:35 -040015#include "SkChromeTracingTracer.h"
scroggocc2feb12015-08-14 08:32:46 -070016#include "SkCodec.h"
mtklein27c3fdd2016-02-26 14:43:21 -080017#include "SkColorPriv.h"
msarett888dc162016-05-23 10:21:17 -070018#include "SkColorSpace.h"
Mike Klein919cc452017-03-18 15:36:52 +000019#include "SkColorSpacePriv.h"
caryclark17f0b6d2014-07-22 10:15:34 -070020#include "SkCommonFlags.h"
kkinnunen3e980c32015-12-23 01:33:00 -080021#include "SkCommonFlagsConfig.h"
Chris Dalton040238b2017-12-18 14:22:34 -070022#include "SkCommonFlagsGpu.h"
brianosman3c579dc2016-04-19 09:18:11 -070023#include "SkData.h"
Brian Salomondcbb9d92017-07-19 10:53:20 -040024#include "SkDebugfTracer.h"
Hal Canaryd7b38452017-12-11 17:46:26 -050025#include "SkDocument.h"
Brian Osman53136aa2017-07-20 15:43:35 -040026#include "SkEventTracingPriv.h"
caryclark83ca6282015-06-10 09:31:09 -070027#include "SkFontMgr.h"
Hal Canaryd7b38452017-12-11 17:46:26 -050028#include "SkFontMgrPriv.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +000029#include "SkGraphics.h"
mtklein27c3fdd2016-02-26 14:43:21 -080030#include "SkHalf.h"
halcanary4dbbd042016-06-07 17:21:10 -070031#include "SkLeanWindows.h"
mtklein748ca3b2015-01-15 10:56:12 -080032#include "SkMD5.h"
mtklein1b249332015-07-07 12:21:21 -070033#include "SkMutex.h"
mtklein1d0f1642014-09-08 08:05:18 -070034#include "SkOSFile.h"
Ben Wagnerbf111d72016-11-07 18:05:29 -050035#include "SkOSPath.h"
mtklein27c3fdd2016-02-26 14:43:21 -080036#include "SkPM4fPriv.h"
Mike Klein13b6afd2017-06-12 12:45:41 -040037#include "SkPngEncoder.h"
Brian Salomondcbb9d92017-07-19 10:53:20 -040038#include "SkScan.h"
mtklein246ba3a2016-02-23 10:39:36 -080039#include "SkSpinlock.h"
mtkleina82f5622015-02-20 12:30:19 -080040#include "SkTHash.h"
mtklein406654b2014-09-03 15:34:37 -070041#include "SkTaskGroup.h"
Mike Klein0c5fdcb2017-11-13 12:40:46 -050042#include "SkTypeface_win.h"
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +000043#include "Test.h"
mtklein748ca3b2015-01-15 10:56:12 -080044#include "Timer.h"
Mike Kleinadacaef2017-02-06 09:26:14 -050045#include "ios_utils.h"
brianosman3c579dc2016-04-19 09:18:11 -070046#include "picture_utils.h"
caryclark83ca6282015-06-10 09:31:09 -070047#include "sk_tool_utils.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +000048
scroggo19b91532016-10-24 09:03:26 -070049#include <vector>
50
halcanary7a14b312015-10-01 07:28:13 -070051#ifdef SK_PDF_IMAGE_STATS
52extern void SkPDFImageDumpStats();
53#endif
54
mtkleina5114d72015-08-24 13:27:01 -070055#include "png.h"
56
bungeman60e0fee2015-08-26 05:15:46 -070057#include <stdlib.h>
58
Mike Klein8f11d4d2018-01-24 12:42:55 -050059#ifndef SK_BUILD_FOR_WIN
scroggo27a58342015-10-28 08:56:41 -070060 #include <unistd.h>
61#endif
62
Dongwon Kang0c7861f2018-02-16 10:55:21 -080063#if defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) && defined(SK_HAS_HEIF_LIBRARY)
64#include <binder/IPCThreadState.h>
65#endif
66
Mike Reed7c26ca72017-07-05 15:45:52 -040067extern bool gSkForceRasterPipelineBlitter;
68
Ben Wagner32fa5102017-08-10 21:25:55 -040069DECLARE_bool(undefok);
djsollen54416de2015-04-03 07:24:48 -070070DEFINE_string(src, "tests gm skp image", "Source types to test.");
mtklein748ca3b2015-01-15 10:56:12 -080071DEFINE_bool(nameByHash, false,
72 "If true, write to FLAGS_writePath[0]/<hash>.png instead of "
djsollen54416de2015-04-03 07:24:48 -070073 "to FLAGS_writePath[0]/<config>/<sourceType>/<sourceOptions>/<name>.png");
mtklein748ca3b2015-01-15 10:56:12 -080074DEFINE_bool2(pathOpsExtended, x, false, "Run extended pathOps tests.");
mtkleind603b222015-02-17 11:13:33 -080075DEFINE_string(matrix, "1 0 0 1",
76 "2x2 scale+skew matrix to apply or upright when using "
77 "'matrix' or 'upright' in config.");
mtklein82d28432015-01-15 12:46:02 -080078DEFINE_bool(gpu_threading, false, "Allow GPU work to run on multiple threads?");
mtklein@google.comd36522d2013-10-16 13:02:15 +000079
mtkleina2ef6422015-01-15 13:44:22 -080080DEFINE_string(blacklist, "",
Chris Daltonecf78ac2017-08-15 15:43:08 -060081 "Space-separated config/src/srcOptions/name quadruples to blacklist. "
82 "'_' matches anything. '~' negates the match. E.g. \n"
djsollen54416de2015-04-03 07:24:48 -070083 "'--blacklist gpu skp _ _' will blacklist all SKPs drawn into the gpu config.\n"
Chris Daltonecf78ac2017-08-15 15:43:08 -060084 "'--blacklist gpu skp _ _ 8888 gm _ aarects' will also blacklist the aarects GM on 8888.\n"
85 "'--blacklist ~8888 svg _ svgparse_' blocks non-8888 SVGs that contain \"svgparse_\" in "
86 "the name.");
mtkleina2ef6422015-01-15 13:44:22 -080087
mtklein62bd1a62015-01-27 14:46:26 -080088DEFINE_string2(readPath, r, "", "If set check for equality with golden results in this directory.");
89
borenet09ed4802015-04-03 14:15:33 -070090DEFINE_string(uninterestingHashesFile, "",
91 "File containing a list of uninteresting hashes. If a result hashes to something in "
92 "this list, no image is written for that result.");
93
mtklein6393c062015-04-27 08:45:01 -070094DEFINE_int32(shards, 1, "We're splitting source data into this many shards.");
95DEFINE_int32(shard, 0, "Which shard do I run?");
96
halcanary45420a92016-06-02 12:41:14 -070097DEFINE_string(mskps, "", "Directory to read mskps from, or a single mskp file.");
Mike Reed7c26ca72017-07-05 15:45:52 -040098DEFINE_bool(forceRasterPipeline, false, "sets gSkForceRasterPipelineBlitter");
halcanary45420a92016-06-02 12:41:14 -070099
Robert Phillipsad8a43f2017-08-30 12:06:35 -0400100DEFINE_bool(ddl, false, "If true, use DeferredDisplayLists for GPU SKP rendering.");
101
Derek Sollenbergeredfe3df2017-07-19 15:25:24 -0400102DEFINE_bool(ignoreSigInt, false, "ignore SIGINT signals during test execution");
103
Mike Klein97d6a7a2017-07-24 10:37:19 -0400104DEFINE_string(dont_write, "", "File extensions to skip writing to --writePath."); // See skia:6821
105
Mike Klein31868212017-11-06 12:02:47 -0500106DEFINE_bool(nativeFonts, true, "If true, use native font manager and rendering. "
107 "If false, fonts will draw as portably as possible.");
Mike Klein0c5fdcb2017-11-13 12:40:46 -0500108DEFINE_bool(gdi, false, "On Windows, use GDI instead of DirectWrite for font rendering.");
Mike Klein31868212017-11-06 12:02:47 -0500109
mtklein748ca3b2015-01-15 10:56:12 -0800110using namespace DM;
bsalomon3724e572016-03-30 18:56:19 -0700111using sk_gpu_test::GrContextFactory;
bsalomon273c0f52016-03-31 10:59:06 -0700112using sk_gpu_test::GLTestContext;
bsalomonf2f1c172016-04-05 12:59:06 -0700113using sk_gpu_test::ContextInfo;
mtklein@google.comd36522d2013-10-16 13:02:15 +0000114
mtklein748ca3b2015-01-15 10:56:12 -0800115/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
116
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700117static const double kStartMs = SkTime::GetMSecs();
mtkleinc41fd922016-03-08 09:01:39 -0800118
119static FILE* gVLog;
120
121template <typename... Args>
122static void vlog(const char* fmt, Args&&... args) {
123 if (gVLog) {
Jim Van Verth32bbf372017-09-20 12:53:00 -0400124 char s[64];
125 HumanizeMs(s, 64, SkTime::GetMSecs() - kStartMs);
126 fprintf(gVLog, "%s\t", s);
mtkleinc41fd922016-03-08 09:01:39 -0800127 fprintf(gVLog, fmt, args...);
128 fflush(gVLog);
129 }
130}
131
132template <typename... Args>
133static void info(const char* fmt, Args&&... args) {
134 vlog(fmt, args...);
135 if (!FLAGS_quiet) {
136 printf(fmt, args...);
137 }
138}
139static void info(const char* fmt) {
140 if (!FLAGS_quiet) {
141 printf("%s", fmt); // Clang warns printf(fmt) is insecure.
142 }
143}
144
reed086eea92016-05-04 17:12:46 -0700145SK_DECLARE_STATIC_MUTEX(gFailuresMutex);
mtklein748ca3b2015-01-15 10:56:12 -0800146static SkTArray<SkString> gFailures;
147
mtklein3eed7dd2016-02-24 19:07:07 -0800148static void fail(const SkString& err) {
mtklein748ca3b2015-01-15 10:56:12 -0800149 SkAutoMutexAcquire lock(gFailuresMutex);
150 SkDebugf("\n\nFAILURE: %s\n\n", err.c_str());
151 gFailures.push_back(err);
halcanary9e398f72015-01-10 11:18:04 -0800152}
153
mtklein0f7748a2016-07-25 15:27:29 -0700154struct Running {
155 SkString id;
156 SkThreadID thread;
157
158 void dump() const {
159 info("\t%s\n", id.c_str());
160 }
161};
mtkleinb37cb412015-03-18 05:27:14 -0700162
mtklein246ba3a2016-02-23 10:39:36 -0800163// We use a spinlock to make locking this in a signal handler _somewhat_ safe.
Mike Klein03a95ad2017-06-23 12:36:32 -0400164static SkSpinlock gMutex;
165static int gPending;
mtklein0f7748a2016-07-25 15:27:29 -0700166static SkTArray<Running> gRunning;
mtklein748ca3b2015-01-15 10:56:12 -0800167
mtklein3eed7dd2016-02-24 19:07:07 -0800168static void done(const char* config, const char* src, const char* srcOptions, const char* name) {
169 SkString id = SkStringPrintf("%s %s %s %s", config, src, srcOptions, name);
mtkleinc41fd922016-03-08 09:01:39 -0800170 vlog("done %s\n", id.c_str());
mtkleinafae30a2016-02-24 12:28:32 -0800171 int pending;
mtkleinb37cb412015-03-18 05:27:14 -0700172 {
mtkleinf10637f2016-06-10 13:56:35 -0700173 SkAutoMutexAcquire lock(gMutex);
mtkleinb37cb412015-03-18 05:27:14 -0700174 for (int i = 0; i < gRunning.count(); i++) {
mtklein0f7748a2016-07-25 15:27:29 -0700175 if (gRunning[i].id == id) {
mtkleinb37cb412015-03-18 05:27:14 -0700176 gRunning.removeShuffle(i);
177 break;
178 }
179 }
mtkleinafae30a2016-02-24 12:28:32 -0800180 pending = --gPending;
reed50bc0512015-05-19 14:13:31 -0700181 }
Mike Klein03a95ad2017-06-23 12:36:32 -0400182
183 // We write out dm.json file and print out a progress update every once in a while.
184 // Notice this also handles the final dm.json and progress update when pending == 0.
mtkleina17241b2015-01-23 05:48:00 -0800185 if (pending % 500 == 0) {
186 JsonWriter::DumpJson();
Mike Klein03a95ad2017-06-23 12:36:32 -0400187
188 int curr = sk_tools::getCurrResidentSetSizeMB(),
189 peak = sk_tools::getMaxResidentSetSizeMB();
190 SkString elapsed = HumanizeMs(SkTime::GetMSecs() - kStartMs);
191
192 SkAutoMutexAcquire lock(gMutex);
193 info("\n%dMB RAM, %dMB peak, %s elapsed, %d queued, %d active:\n",
194 curr, peak, elapsed.c_str(), gPending - gRunning.count(), gRunning.count());
195 for (auto& task : gRunning) {
196 task.dump();
197 }
mtkleina17241b2015-01-23 05:48:00 -0800198 }
mtklein@google.comd36522d2013-10-16 13:02:15 +0000199}
200
mtklein3eed7dd2016-02-24 19:07:07 -0800201static void start(const char* config, const char* src, const char* srcOptions, const char* name) {
202 SkString id = SkStringPrintf("%s %s %s %s", config, src, srcOptions, name);
mtkleinc41fd922016-03-08 09:01:39 -0800203 vlog("start %s\n", id.c_str());
mtkleinf10637f2016-06-10 13:56:35 -0700204 SkAutoMutexAcquire lock(gMutex);
mtklein0f7748a2016-07-25 15:27:29 -0700205 gRunning.push_back({id,SkGetThreadID()});
mtkleinb37cb412015-03-18 05:27:14 -0700206}
207
Mike Klein3cc2d202016-10-24 11:39:43 -0400208static void find_culprit() {
209 // Assumes gMutex is locked.
210 SkThreadID thisThread = SkGetThreadID();
211 for (auto& task : gRunning) {
212 if (task.thread == thisThread) {
213 info("Likely culprit:\n");
214 task.dump();
mtklein0f7748a2016-07-25 15:27:29 -0700215 }
216 }
Mike Klein3cc2d202016-10-24 11:39:43 -0400217}
mtklein0f7748a2016-07-25 15:27:29 -0700218
Mike Klein8f11d4d2018-01-24 12:42:55 -0500219#if defined(SK_BUILD_FOR_WIN)
mtklein5b64dab2016-06-09 08:59:48 -0700220 static LONG WINAPI crash_handler(EXCEPTION_POINTERS* e) {
mtkleinf8557292016-02-29 06:35:28 -0800221 static const struct {
222 const char* name;
robertphillips75467862016-03-01 14:10:23 -0800223 DWORD code;
mtkleinf8557292016-02-29 06:35:28 -0800224 } kExceptions[] = {
225 #define _(E) {#E, E}
226 _(EXCEPTION_ACCESS_VIOLATION),
227 _(EXCEPTION_BREAKPOINT),
228 _(EXCEPTION_INT_DIVIDE_BY_ZERO),
229 _(EXCEPTION_STACK_OVERFLOW),
230 // TODO: more?
231 #undef _
232 };
233
mtkleinf10637f2016-06-10 13:56:35 -0700234 SkAutoMutexAcquire lock(gMutex);
mtklein5b64dab2016-06-09 08:59:48 -0700235
236 const DWORD code = e->ExceptionRecord->ExceptionCode;
237 info("\nCaught exception %u", code);
238 for (const auto& exception : kExceptions) {
239 if (exception.code == code) {
240 info(" %s", exception.name);
mtkleinf8557292016-02-29 06:35:28 -0800241 }
mtkleinf8557292016-02-29 06:35:28 -0800242 }
mtklein5b64dab2016-06-09 08:59:48 -0700243 info(", was running:\n");
244 for (auto& task : gRunning) {
mtklein0f7748a2016-07-25 15:27:29 -0700245 task.dump();
mtklein5b64dab2016-06-09 08:59:48 -0700246 }
mtklein0f7748a2016-07-25 15:27:29 -0700247 find_culprit();
mtklein5b64dab2016-06-09 08:59:48 -0700248 fflush(stdout);
249
mtkleinf8557292016-02-29 06:35:28 -0800250 // Execute default exception handler... hopefully, exit.
251 return EXCEPTION_EXECUTE_HANDLER;
mtklein246ba3a2016-02-23 10:39:36 -0800252 }
253
Mike Klein3cc2d202016-10-24 11:39:43 -0400254 static void setup_crash_handler() {
255 SetUnhandledExceptionFilter(crash_handler);
256 }
257#else
mtklein246ba3a2016-02-23 10:39:36 -0800258 #include <signal.h>
Mike Klein3cc2d202016-10-24 11:39:43 -0400259 #if !defined(SK_BUILD_FOR_ANDROID)
260 #include <execinfo.h>
Brian Salomondcbb9d92017-07-19 10:53:20 -0400261
262#endif
Mike Klein3cc2d202016-10-24 11:39:43 -0400263
264 static constexpr int max_of() { return 0; }
265 template <typename... Rest>
266 static constexpr int max_of(int x, Rest... rest) {
267 return x > max_of(rest...) ? x : max_of(rest...);
268 }
269
Mike Kleinc7278ad2018-02-15 09:26:54 -0500270 static void (*previous_handler[max_of(SIGABRT,SIGBUS,SIGFPE,SIGILL,SIGSEGV,SIGTERM)+1])(int);
mtkleinf8557292016-02-29 06:35:28 -0800271
mtklein5b64dab2016-06-09 08:59:48 -0700272 static void crash_handler(int sig) {
mtkleinf10637f2016-06-10 13:56:35 -0700273 SkAutoMutexAcquire lock(gMutex);
mtklein5b64dab2016-06-09 08:59:48 -0700274
275 info("\nCaught signal %d [%s], was running:\n", sig, strsignal(sig));
276 for (auto& task : gRunning) {
mtklein0f7748a2016-07-25 15:27:29 -0700277 task.dump();
mtklein5b64dab2016-06-09 08:59:48 -0700278 }
mtklein0f7748a2016-07-25 15:27:29 -0700279 find_culprit();
mtklein5b64dab2016-06-09 08:59:48 -0700280
Mike Klein3cc2d202016-10-24 11:39:43 -0400281 #if !defined(SK_BUILD_FOR_ANDROID)
mtklein5b64dab2016-06-09 08:59:48 -0700282 void* stack[64];
283 int count = backtrace(stack, SK_ARRAY_COUNT(stack));
284 char** symbols = backtrace_symbols(stack, count);
285 info("\nStack trace:\n");
286 for (int i = 0; i < count; i++) {
287 info(" %s\n", symbols[i]);
288 }
Mike Klein3cc2d202016-10-24 11:39:43 -0400289 #endif
mtklein5b64dab2016-06-09 08:59:48 -0700290 fflush(stdout);
291
Mike Klein3cc2d202016-10-24 11:39:43 -0400292 signal(sig, previous_handler[sig]);
293 raise(sig);
mtklein5b64dab2016-06-09 08:59:48 -0700294 }
295
Mike Kleinc7278ad2018-02-15 09:26:54 -0500296 static void term_handler(int sig) {
297 info("\nWe have been politely asked to die by %s (%d)."
298 "\nCurrently using %dMB RAM, peak %dMB.\n",
299 strsignal(sig), sig,
300 sk_tools::getCurrResidentSetSizeMB(), sk_tools::getMaxResidentSetSizeMB());
301 signal(sig, previous_handler[sig]);
302 raise(sig);
303 }
304
mtklein246ba3a2016-02-23 10:39:36 -0800305 static void setup_crash_handler() {
306 const int kSignals[] = { SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGSEGV };
307 for (int sig : kSignals) {
Mike Klein3cc2d202016-10-24 11:39:43 -0400308 previous_handler[sig] = signal(sig, crash_handler);
mtklein246ba3a2016-02-23 10:39:36 -0800309 }
Mike Kleinc7278ad2018-02-15 09:26:54 -0500310 previous_handler[SIGTERM] = signal(SIGTERM, term_handler);
Derek Sollenbergeredfe3df2017-07-19 15:25:24 -0400311
312 if (FLAGS_ignoreSigInt) {
313 signal(SIGINT, SIG_IGN);
314 }
mtklein246ba3a2016-02-23 10:39:36 -0800315 }
316#endif
317
mtklein748ca3b2015-01-15 10:56:12 -0800318/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
mtklein114c3cd2015-01-15 10:15:02 -0800319
mtklein62bd1a62015-01-27 14:46:26 -0800320struct Gold : public SkString {
mtkleina82f5622015-02-20 12:30:19 -0800321 Gold() : SkString("") {}
mtklein3eed7dd2016-02-24 19:07:07 -0800322 Gold(const SkString& sink, const SkString& src,
323 const SkString& srcOptions, const SkString& name,
324 const SkString& md5)
mtklein62bd1a62015-01-27 14:46:26 -0800325 : SkString("") {
326 this->append(sink);
327 this->append(src);
djsollen54416de2015-04-03 07:24:48 -0700328 this->append(srcOptions);
mtklein62bd1a62015-01-27 14:46:26 -0800329 this->append(name);
330 this->append(md5);
mtklein62bd1a62015-01-27 14:46:26 -0800331 }
mtkleinc8d1dd42015-10-15 12:23:01 -0700332 struct Hash {
333 uint32_t operator()(const Gold& g) const {
334 return SkGoodHash()((const SkString&)g);
335 }
336 };
mtklein62bd1a62015-01-27 14:46:26 -0800337};
mtkleina82f5622015-02-20 12:30:19 -0800338static SkTHashSet<Gold, Gold::Hash> gGold;
mtklein62bd1a62015-01-27 14:46:26 -0800339
340static void add_gold(JsonWriter::BitmapResult r) {
djsollen54416de2015-04-03 07:24:48 -0700341 gGold.add(Gold(r.config, r.sourceType, r.sourceOptions, r.name, r.md5));
mtklein62bd1a62015-01-27 14:46:26 -0800342}
343
344static void gather_gold() {
345 if (!FLAGS_readPath.isEmpty()) {
346 SkString path(FLAGS_readPath[0]);
347 path.append("/dm.json");
348 if (!JsonWriter::ReadJson(path.c_str(), add_gold)) {
349 fail(SkStringPrintf("Couldn't read %s for golden results.", path.c_str()));
350 }
351 }
352}
353
354/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
355
Mike Klein8f11d4d2018-01-24 12:42:55 -0500356#if defined(SK_BUILD_FOR_WIN)
mtklein935f1b12016-03-16 08:37:19 -0700357 static const char* kNewline = "\r\n";
358#else
359 static const char* kNewline = "\n";
360#endif
361
borenet09ed4802015-04-03 14:15:33 -0700362static SkTHashSet<SkString> gUninterestingHashes;
363
364static void gather_uninteresting_hashes() {
365 if (!FLAGS_uninterestingHashesFile.isEmpty()) {
bungeman38d909e2016-08-02 14:40:46 -0700366 sk_sp<SkData> data(SkData::MakeFromFileName(FLAGS_uninterestingHashesFile[0]));
mtkleincc334b32015-09-22 11:43:53 -0700367 if (!data) {
mtkleinc41fd922016-03-08 09:01:39 -0800368 info("WARNING: unable to read uninteresting hashes from %s\n",
369 FLAGS_uninterestingHashesFile[0]);
mtkleincc334b32015-09-22 11:43:53 -0700370 return;
371 }
Mike Klein45dcc0c2017-04-05 18:04:31 -0400372
373 // Copy to a string to make sure SkStrSplit has a terminating \0 to find.
374 SkString contents((const char*)data->data(), data->size());
375
borenet09ed4802015-04-03 14:15:33 -0700376 SkTArray<SkString> hashes;
Mike Klein45dcc0c2017-04-05 18:04:31 -0400377 SkStrSplit(contents.c_str(), kNewline, &hashes);
borenet09ed4802015-04-03 14:15:33 -0700378 for (const SkString& hash : hashes) {
379 gUninterestingHashes.add(hash);
380 }
mtkleinc41fd922016-03-08 09:01:39 -0800381 info("FYI: loaded %d distinct uninteresting hashes from %d lines\n",
382 gUninterestingHashes.count(), hashes.count());
borenet09ed4802015-04-03 14:15:33 -0700383 }
384}
385
386/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
387
Ben Wagner145dbcd2016-11-03 14:40:50 -0400388struct TaggedSrc : public std::unique_ptr<Src> {
mtklein3eed7dd2016-02-24 19:07:07 -0800389 SkString tag;
390 SkString options;
mtkleine0effd62015-07-29 06:37:28 -0700391};
392
Ben Wagner145dbcd2016-11-03 14:40:50 -0400393struct TaggedSink : public std::unique_ptr<Sink> {
kkinnunen3e980c32015-12-23 01:33:00 -0800394 SkString tag;
djsollen54416de2015-04-03 07:24:48 -0700395};
mtklein748ca3b2015-01-15 10:56:12 -0800396
397static const bool kMemcpyOK = true;
398
mtkleine0effd62015-07-29 06:37:28 -0700399static SkTArray<TaggedSrc, kMemcpyOK> gSrcs;
400static SkTArray<TaggedSink, kMemcpyOK> gSinks;
mtklein748ca3b2015-01-15 10:56:12 -0800401
mtklein6393c062015-04-27 08:45:01 -0700402static bool in_shard() {
403 static int N = 0;
404 return N++ % FLAGS_shards == FLAGS_shard;
405}
406
mtklein3eed7dd2016-02-24 19:07:07 -0800407static void push_src(const char* tag, ImplicitString options, Src* s) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400408 std::unique_ptr<Src> src(s);
mtklein6393c062015-04-27 08:45:01 -0700409 if (in_shard() &&
mtklein3eed7dd2016-02-24 19:07:07 -0800410 FLAGS_src.contains(tag) &&
mtklein748ca3b2015-01-15 10:56:12 -0800411 !SkCommandLineFlags::ShouldSkip(FLAGS_match, src->name().c_str())) {
mtkleine0effd62015-07-29 06:37:28 -0700412 TaggedSrc& s = gSrcs.push_back();
mtklein18300a32016-03-16 13:53:35 -0700413 s.reset(src.release());
mtklein748ca3b2015-01-15 10:56:12 -0800414 s.tag = tag;
djsollen54416de2015-04-03 07:24:48 -0700415 s.options = options;
mtklein114c3cd2015-01-15 10:15:02 -0800416 }
mtklein748ca3b2015-01-15 10:56:12 -0800417}
mtklein114c3cd2015-01-15 10:15:02 -0800418
msarett9e707a02015-09-01 14:57:57 -0700419static void push_codec_src(Path path, CodecSrc::Mode mode, CodecSrc::DstColorType dstColorType,
scroggoc5560be2016-02-03 09:42:42 -0800420 SkAlphaType dstAlphaType, float scale) {
scroggoe4499842016-02-25 11:03:47 -0800421 if (FLAGS_simpleCodec) {
scroggo19b91532016-10-24 09:03:26 -0700422 const bool simple = CodecSrc::kCodec_Mode == mode || CodecSrc::kAnimated_Mode == mode;
423 if (!simple || dstColorType != CodecSrc::kGetFromCanvas_DstColorType || scale != 1.0f) {
scroggoe4499842016-02-25 11:03:47 -0800424 // Only decode in the simple case.
425 return;
scroggo19b91532016-10-24 09:03:26 -0700426 }
scroggoe4499842016-02-25 11:03:47 -0800427 }
msarett9e707a02015-09-01 14:57:57 -0700428 SkString folder;
429 switch (mode) {
430 case CodecSrc::kCodec_Mode:
431 folder.append("codec");
432 break;
msarettbb25b532016-01-13 09:31:39 -0800433 case CodecSrc::kCodecZeroInit_Mode:
434 folder.append("codec_zero_init");
435 break;
msarett9e707a02015-09-01 14:57:57 -0700436 case CodecSrc::kScanline_Mode:
437 folder.append("scanline");
438 break;
msarett9e707a02015-09-01 14:57:57 -0700439 case CodecSrc::kStripe_Mode:
440 folder.append("stripe");
441 break;
msarett91c22b22016-02-22 12:27:46 -0800442 case CodecSrc::kCroppedScanline_Mode:
443 folder.append("crop");
444 break;
msarett9e707a02015-09-01 14:57:57 -0700445 case CodecSrc::kSubset_Mode:
msarett36c37962015-09-02 13:20:52 -0700446 folder.append("codec_subset");
msarett9e707a02015-09-01 14:57:57 -0700447 break;
scroggo19b91532016-10-24 09:03:26 -0700448 case CodecSrc::kAnimated_Mode:
449 folder.append("codec_animated");
450 break;
msarett9e707a02015-09-01 14:57:57 -0700451 }
452
453 switch (dstColorType) {
454 case CodecSrc::kGrayscale_Always_DstColorType:
455 folder.append("_kGray8");
456 break;
msarett34e0ec42016-04-22 16:27:24 -0700457 case CodecSrc::kNonNative8888_Always_DstColorType:
458 folder.append("_kNonNative");
459 break;
msarett9e707a02015-09-01 14:57:57 -0700460 default:
461 break;
462 }
463
scroggoc5560be2016-02-03 09:42:42 -0800464 switch (dstAlphaType) {
scroggoc5560be2016-02-03 09:42:42 -0800465 case kPremul_SkAlphaType:
466 folder.append("_premul");
467 break;
468 case kUnpremul_SkAlphaType:
469 folder.append("_unpremul");
470 break;
471 default:
472 break;
473 }
474
msarett9e707a02015-09-01 14:57:57 -0700475 if (1.0f != scale) {
476 folder.appendf("_%.3f", scale);
477 }
478
scroggoc5560be2016-02-03 09:42:42 -0800479 CodecSrc* src = new CodecSrc(path, mode, dstColorType, dstAlphaType, scale);
msarett9e707a02015-09-01 14:57:57 -0700480 push_src("image", folder, src);
481}
482
scroggof8dc9df2016-05-16 09:04:13 -0700483static void push_android_codec_src(Path path, CodecSrc::DstColorType dstColorType,
484 SkAlphaType dstAlphaType, int sampleSize) {
msarett3d9d7a72015-10-21 10:27:10 -0700485 SkString folder;
scroggof8dc9df2016-05-16 09:04:13 -0700486 folder.append("scaled_codec");
msarett3d9d7a72015-10-21 10:27:10 -0700487
488 switch (dstColorType) {
489 case CodecSrc::kGrayscale_Always_DstColorType:
490 folder.append("_kGray8");
491 break;
msarett34e0ec42016-04-22 16:27:24 -0700492 case CodecSrc::kNonNative8888_Always_DstColorType:
493 folder.append("_kNonNative");
494 break;
msarett3d9d7a72015-10-21 10:27:10 -0700495 default:
496 break;
497 }
498
scroggoc5560be2016-02-03 09:42:42 -0800499 switch (dstAlphaType) {
scroggoc5560be2016-02-03 09:42:42 -0800500 case kPremul_SkAlphaType:
501 folder.append("_premul");
502 break;
msarett9e9444c2016-02-03 12:39:10 -0800503 case kUnpremul_SkAlphaType:
504 folder.append("_unpremul");
505 break;
scroggoc5560be2016-02-03 09:42:42 -0800506 default:
507 break;
508 }
509
msarett3d9d7a72015-10-21 10:27:10 -0700510 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800511 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700512 }
513
scroggof8dc9df2016-05-16 09:04:13 -0700514 AndroidCodecSrc* src = new AndroidCodecSrc(path, dstColorType, dstAlphaType, sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700515 push_src("image", folder, src);
516}
517
msarett18976312016-03-09 14:20:58 -0800518static void push_image_gen_src(Path path, ImageGenSrc::Mode mode, SkAlphaType alphaType, bool isGpu)
519{
520 SkString folder;
521 switch (mode) {
522 case ImageGenSrc::kCodec_Mode:
523 folder.append("gen_codec");
524 break;
525 case ImageGenSrc::kPlatform_Mode:
526 folder.append("gen_platform");
527 break;
528 }
529
530 if (isGpu) {
531 folder.append("_gpu");
532 } else {
533 switch (alphaType) {
534 case kOpaque_SkAlphaType:
535 folder.append("_opaque");
536 break;
537 case kPremul_SkAlphaType:
538 folder.append("_premul");
539 break;
540 case kUnpremul_SkAlphaType:
541 folder.append("_unpremul");
542 break;
543 default:
544 break;
545 }
546 }
547
548 ImageGenSrc* src = new ImageGenSrc(path, mode, alphaType, isGpu);
549 push_src("image", folder, src);
550}
551
Leon Scroggins III07418182017-08-15 12:24:02 -0400552static void push_brd_src(Path path, CodecSrc::DstColorType dstColorType, BRDSrc::Mode mode,
553 uint32_t sampleSize) {
554 SkString folder("brd_android_codec");
555 switch (mode) {
556 case BRDSrc::kFullImage_Mode:
557 break;
558 case BRDSrc::kDivisor_Mode:
559 folder.append("_divisor");
560 break;
561 default:
562 SkASSERT(false);
563 return;
564 }
565
566 switch (dstColorType) {
567 case CodecSrc::kGetFromCanvas_DstColorType:
568 break;
569 case CodecSrc::kGrayscale_Always_DstColorType:
570 folder.append("_kGray");
571 break;
572 default:
573 SkASSERT(false);
574 return;
575 }
576
577 if (1 != sampleSize) {
578 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
579 }
580
581 BRDSrc* src = new BRDSrc(path, mode, dstColorType, sampleSize);
582 push_src("image", folder, src);
583}
584
585static void push_brd_srcs(Path path, bool gray) {
586 if (gray) {
587 // Only run grayscale to one sampleSize and Mode. Though interesting
588 // to test grayscale, it should not reveal anything across various
589 // sampleSizes and Modes
590 // Arbitrarily choose Mode and sampleSize.
591 push_brd_src(path, CodecSrc::kGrayscale_Always_DstColorType,
592 BRDSrc::kFullImage_Mode, 2);
593 }
594
595 // Test on a variety of sampleSizes, making sure to include:
596 // - 2, 4, and 8, which are natively supported by jpeg
597 // - multiples of 2 which are not divisible by 4 (analogous for 4)
598 // - larger powers of two, since BRD clients generally use powers of 2
599 // We will only produce output for the larger sizes on large images.
600 const uint32_t sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 24, 32, 64 };
601
602 const BRDSrc::Mode modes[] = { BRDSrc::kFullImage_Mode, BRDSrc::kDivisor_Mode, };
603
604 for (uint32_t sampleSize : sampleSizes) {
605 for (BRDSrc::Mode mode : modes) {
606 push_brd_src(path, CodecSrc::kGetFromCanvas_DstColorType, mode, sampleSize);
607 }
608 }
609}
610
msarett438b2ad2015-04-09 12:43:10 -0700611static void push_codec_srcs(Path path) {
bungeman38d909e2016-08-02 14:40:46 -0700612 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
msarett438b2ad2015-04-09 12:43:10 -0700613 if (!encoded) {
mtkleinc41fd922016-03-08 09:01:39 -0800614 info("Couldn't read %s.", path.c_str());
msarett438b2ad2015-04-09 12:43:10 -0700615 return;
616 }
Mike Reedede7bac2017-07-23 15:30:02 -0400617 std::unique_ptr<SkCodec> codec = SkCodec::MakeFromData(encoded);
halcanary96fcdcc2015-08-27 07:41:13 -0700618 if (nullptr == codec.get()) {
mtkleinc41fd922016-03-08 09:01:39 -0800619 info("Couldn't create codec for %s.", path.c_str());
msarett438b2ad2015-04-09 12:43:10 -0700620 return;
621 }
622
scroggo9366aff2016-05-20 08:38:54 -0700623 // native scaling is only supported by WEBP and JPEG
624 bool supportsNativeScaling = false;
msarett438b2ad2015-04-09 12:43:10 -0700625
msarett91c22b22016-02-22 12:27:46 -0800626 SkTArray<CodecSrc::Mode> nativeModes;
627 nativeModes.push_back(CodecSrc::kCodec_Mode);
628 nativeModes.push_back(CodecSrc::kCodecZeroInit_Mode);
msarett91c22b22016-02-22 12:27:46 -0800629 switch (codec->getEncodedFormat()) {
Hal Canarydb683012016-11-23 08:55:18 -0700630 case SkEncodedImageFormat::kJPEG:
msarett91c22b22016-02-22 12:27:46 -0800631 nativeModes.push_back(CodecSrc::kScanline_Mode);
632 nativeModes.push_back(CodecSrc::kStripe_Mode);
633 nativeModes.push_back(CodecSrc::kCroppedScanline_Mode);
scroggo9366aff2016-05-20 08:38:54 -0700634 supportsNativeScaling = true;
msarett91c22b22016-02-22 12:27:46 -0800635 break;
Hal Canarydb683012016-11-23 08:55:18 -0700636 case SkEncodedImageFormat::kWEBP:
msarett91c22b22016-02-22 12:27:46 -0800637 nativeModes.push_back(CodecSrc::kSubset_Mode);
scroggo9366aff2016-05-20 08:38:54 -0700638 supportsNativeScaling = true;
msarett91c22b22016-02-22 12:27:46 -0800639 break;
Hal Canarydb683012016-11-23 08:55:18 -0700640 case SkEncodedImageFormat::kDNG:
msarettb65e6042016-02-23 05:37:25 -0800641 break;
msarett91c22b22016-02-22 12:27:46 -0800642 default:
643 nativeModes.push_back(CodecSrc::kScanline_Mode);
msarett91c22b22016-02-22 12:27:46 -0800644 break;
645 }
msarett9e707a02015-09-01 14:57:57 -0700646
msarett91c22b22016-02-22 12:27:46 -0800647 SkTArray<CodecSrc::DstColorType> colorTypes;
648 colorTypes.push_back(CodecSrc::kGetFromCanvas_DstColorType);
msarett34e0ec42016-04-22 16:27:24 -0700649 colorTypes.push_back(CodecSrc::kNonNative8888_Always_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700650 switch (codec->getInfo().colorType()) {
651 case kGray_8_SkColorType:
msarett91c22b22016-02-22 12:27:46 -0800652 colorTypes.push_back(CodecSrc::kGrayscale_Always_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700653 break;
654 default:
msarett36c37962015-09-02 13:20:52 -0700655 break;
656 }
msarett9e707a02015-09-01 14:57:57 -0700657
scroggoc5560be2016-02-03 09:42:42 -0800658 SkTArray<SkAlphaType> alphaModes;
659 alphaModes.push_back(kPremul_SkAlphaType);
scroggof2c96a22016-05-20 11:08:27 -0700660 if (codec->getInfo().alphaType() != kOpaque_SkAlphaType) {
661 alphaModes.push_back(kUnpremul_SkAlphaType);
scroggoc5560be2016-02-03 09:42:42 -0800662 }
msarettb714fb02016-01-22 14:46:42 -0800663
664 for (CodecSrc::Mode mode : nativeModes) {
scroggo9366aff2016-05-20 08:38:54 -0700665 for (CodecSrc::DstColorType colorType : colorTypes) {
666 for (SkAlphaType alphaType : alphaModes) {
scroggof2c96a22016-05-20 11:08:27 -0700667 // Only test kCroppedScanline_Mode when the alpha type is premul. The test is
scroggo9366aff2016-05-20 08:38:54 -0700668 // slow and won't be interestingly different with different alpha types.
669 if (CodecSrc::kCroppedScanline_Mode == mode &&
scroggof2c96a22016-05-20 11:08:27 -0700670 kPremul_SkAlphaType != alphaType) {
scroggo9366aff2016-05-20 08:38:54 -0700671 continue;
672 }
msarett91c22b22016-02-22 12:27:46 -0800673
scroggo9366aff2016-05-20 08:38:54 -0700674 push_codec_src(path, mode, colorType, alphaType, 1.0f);
msarettf3dc1882016-04-26 13:06:38 -0700675
scroggo9366aff2016-05-20 08:38:54 -0700676 // Skip kNonNative on different native scales. It won't be interestingly
677 // different.
678 if (supportsNativeScaling &&
679 CodecSrc::kNonNative8888_Always_DstColorType == colorType) {
680 // Native Scales
681 // SkJpegCodec natively supports scaling to the following:
682 for (auto scale : { 0.125f, 0.25f, 0.375f, 0.5f, 0.625f, 0.750f, 0.875f }) {
683 push_codec_src(path, mode, colorType, alphaType, scale);
684 }
scroggoc5560be2016-02-03 09:42:42 -0800685 }
msarett9e707a02015-09-01 14:57:57 -0700686 }
687 }
msarett0a242972015-06-11 14:27:27 -0700688 }
msarett36c37962015-09-02 13:20:52 -0700689
scroggo19b91532016-10-24 09:03:26 -0700690 {
691 std::vector<SkCodec::FrameInfo> frameInfos = codec->getFrameInfo();
692 if (frameInfos.size() > 1) {
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400693 for (auto dstCT : { CodecSrc::kNonNative8888_Always_DstColorType,
694 CodecSrc::kGetFromCanvas_DstColorType }) {
695 for (auto at : { kUnpremul_SkAlphaType, kPremul_SkAlphaType }) {
696 push_codec_src(path, CodecSrc::kAnimated_Mode, dstCT, at, 1.0f);
697 }
698 }
scroggo19b91532016-10-24 09:03:26 -0700699 }
700
701 }
702
scroggoe4499842016-02-25 11:03:47 -0800703 if (FLAGS_simpleCodec) {
704 return;
705 }
706
msarett3d9d7a72015-10-21 10:27:10 -0700707 const int sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
msarett36c37962015-09-02 13:20:52 -0700708
msarett3d9d7a72015-10-21 10:27:10 -0700709 for (int sampleSize : sampleSizes) {
msarett91c22b22016-02-22 12:27:46 -0800710 for (CodecSrc::DstColorType colorType : colorTypes) {
scroggoc5560be2016-02-03 09:42:42 -0800711 for (SkAlphaType alphaType : alphaModes) {
msarettf3dc1882016-04-26 13:06:38 -0700712 // We can exercise all of the kNonNative support code in the swizzler with just a
713 // few sample sizes. Skip the rest.
714 if (CodecSrc::kNonNative8888_Always_DstColorType == colorType && sampleSize > 3) {
715 continue;
716 }
717
scroggof8dc9df2016-05-16 09:04:13 -0700718 push_android_codec_src(path, colorType, alphaType, sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700719 }
msarett36c37962015-09-02 13:20:52 -0700720 }
721 }
msarett18976312016-03-09 14:20:58 -0800722
Leon Scroggins III07418182017-08-15 12:24:02 -0400723 const char* ext = strrchr(path.c_str(), '.');
724 if (ext) {
725 ext++;
msarett18976312016-03-09 14:20:58 -0800726
Leon Scroggins III07418182017-08-15 12:24:02 -0400727 static const char* const rawExts[] = {
728 "arw", "cr2", "dng", "nef", "nrw", "orf", "raf", "rw2", "pef", "srw",
729 "ARW", "CR2", "DNG", "NEF", "NRW", "ORF", "RAF", "RW2", "PEF", "SRW",
730 };
731 for (const char* rawExt : rawExts) {
732 if (0 == strcmp(rawExt, ext)) {
733 // RAW is not supported by image generator (skbug.com/5079) or BRD.
734 return;
735 }
736 }
737
738 static const char* const brdExts[] = {
739 "jpg", "jpeg", "png", "webp",
740 "JPG", "JPEG", "PNG", "WEBP",
741 };
742 for (const char* brdExt : brdExts) {
743 if (0 == strcmp(brdExt, ext)) {
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -0500744 bool gray = codec->getInfo().colorType() == kGray_8_SkColorType;
Leon Scroggins III07418182017-08-15 12:24:02 -0400745 push_brd_srcs(path, gray);
746 break;
747 }
msarett18976312016-03-09 14:20:58 -0800748 }
749 }
750
751 // Push image generator GPU test.
brianosmanc9ced392016-03-24 07:27:43 -0700752 push_image_gen_src(path, ImageGenSrc::kCodec_Mode, codec->getInfo().alphaType(), true);
msarett18976312016-03-09 14:20:58 -0800753
754 // Push image generator CPU tests.
755 for (SkAlphaType alphaType : alphaModes) {
756 push_image_gen_src(path, ImageGenSrc::kCodec_Mode, alphaType, false);
757
758#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
Hal Canarydb683012016-11-23 08:55:18 -0700759 if (SkEncodedImageFormat::kWEBP != codec->getEncodedFormat() &&
760 SkEncodedImageFormat::kWBMP != codec->getEncodedFormat() &&
msarett18976312016-03-09 14:20:58 -0800761 kUnpremul_SkAlphaType != alphaType)
762 {
763 push_image_gen_src(path, ImageGenSrc::kPlatform_Mode, alphaType, false);
764 }
msarettfc0b6d12016-03-17 13:50:17 -0700765#elif defined(SK_BUILD_FOR_WIN)
Hal Canarydb683012016-11-23 08:55:18 -0700766 if (SkEncodedImageFormat::kWEBP != codec->getEncodedFormat() &&
767 SkEncodedImageFormat::kWBMP != codec->getEncodedFormat())
msarettfc0b6d12016-03-17 13:50:17 -0700768 {
769 push_image_gen_src(path, ImageGenSrc::kPlatform_Mode, alphaType, false);
770 }
msarett18976312016-03-09 14:20:58 -0800771#endif
772 }
msarett438b2ad2015-04-09 12:43:10 -0700773}
774
fmalitaa2b9fdf2016-08-03 19:53:36 -0700775template <typename T>
776void gather_file_srcs(const SkCommandLineFlags::StringArray& flags, const char* ext) {
777 for (int i = 0; i < flags.count(); i++) {
778 const char* path = flags[i];
779 if (sk_isdir(path)) {
780 SkOSFile::Iter it(path, ext);
781 for (SkString file; it.next(&file); ) {
782 push_src(ext, "", new T(SkOSPath::Join(path, file.c_str())));
783 }
784 } else {
785 push_src(ext, "", new T(path));
786 }
787 }
788}
789
scroggo86737142016-02-03 12:19:11 -0800790static bool gather_srcs() {
mtklein748ca3b2015-01-15 10:56:12 -0800791 for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) {
djsollen54416de2015-04-03 07:24:48 -0700792 push_src("gm", "", new GMSrc(r->factory()));
mtklein748ca3b2015-01-15 10:56:12 -0800793 }
scroggo86737142016-02-03 12:19:11 -0800794
Robert Phillipsad8a43f2017-08-30 12:06:35 -0400795 if (FLAGS_ddl) {
796 gather_file_srcs<DDLSKPSrc>(FLAGS_skps, "skp");
797 } else {
798 gather_file_srcs<SKPSrc>(FLAGS_skps, "skp");
799 }
fmalitaa2b9fdf2016-08-03 19:53:36 -0700800 gather_file_srcs<MSKPSrc>(FLAGS_mskps, "mskp");
Florin Malita124d5af2017-12-31 17:02:26 -0500801#if !defined(SK_BUILD_FOR_GOOGLE3)
Florin Malita54f65c42018-01-16 17:04:30 -0500802 gather_file_srcs<SkottieSrc>(FLAGS_jsons, "json");
Florin Malita124d5af2017-12-31 17:02:26 -0500803#endif
fmalitaa2b9fdf2016-08-03 19:53:36 -0700804#if defined(SK_XML)
805 gather_file_srcs<SVGSrc>(FLAGS_svgs, "svg");
806#endif
halcanary45420a92016-06-02 12:41:14 -0700807
scroggo86737142016-02-03 12:19:11 -0800808 SkTArray<SkString> images;
msarett69deca82016-04-29 09:38:40 -0700809 if (!CollectImages(FLAGS_images, &images)) {
scroggo86737142016-02-03 12:19:11 -0800810 return false;
811 }
812
813 for (auto image : images) {
814 push_codec_srcs(image);
mtklein709d2c32015-01-15 08:30:25 -0800815 }
scroggo86737142016-02-03 12:19:11 -0800816
msarett69deca82016-04-29 09:38:40 -0700817 SkTArray<SkString> colorImages;
818 if (!CollectImages(FLAGS_colorImages, &colorImages)) {
819 return false;
820 }
821
822 for (auto colorImage : colorImages) {
msarett9ce3a542016-07-15 13:54:38 -0700823 ColorCodecSrc* src = new ColorCodecSrc(colorImage, ColorCodecSrc::kBaseline_Mode,
824 kN32_SkColorType);
msarettb8d1aac2016-08-04 12:22:06 -0700825 push_src("colorImage", "color_codec_baseline", src);
msarett888dc162016-05-23 10:21:17 -0700826
msarett9ce3a542016-07-15 13:54:38 -0700827 src = new ColorCodecSrc(colorImage, ColorCodecSrc::kDst_HPZR30w_Mode, kN32_SkColorType);
msarettb8d1aac2016-08-04 12:22:06 -0700828 push_src("colorImage", "color_codec_HPZR30w", src);
msarett9ce3a542016-07-15 13:54:38 -0700829 // TODO (msarett):
830 // 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 -0700831
msarett9ce3a542016-07-15 13:54:38 -0700832 src = new ColorCodecSrc(colorImage, ColorCodecSrc::kDst_sRGB_Mode, kN32_SkColorType);
msarettb8d1aac2016-08-04 12:22:06 -0700833 push_src("colorImage", "color_codec_sRGB_kN32", src);
msarett9ce3a542016-07-15 13:54:38 -0700834 src = new ColorCodecSrc(colorImage, ColorCodecSrc::kDst_sRGB_Mode, kRGBA_F16_SkColorType);
msarettb8d1aac2016-08-04 12:22:06 -0700835 push_src("colorImage", "color_codec_sRGB_kF16", src);
msarett69deca82016-04-29 09:38:40 -0700836 }
837
scroggo86737142016-02-03 12:19:11 -0800838 return true;
mtklein709d2c32015-01-15 08:30:25 -0800839}
840
kkinnunen3e980c32015-12-23 01:33:00 -0800841static void push_sink(const SkCommandLineConfig& config, Sink* s) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400842 std::unique_ptr<Sink> sink(s);
kkinnunen3e980c32015-12-23 01:33:00 -0800843
mtkleine0effd62015-07-29 06:37:28 -0700844 // Try a simple Src as a canary. If it fails, skip this sink.
mtklein748ca3b2015-01-15 10:56:12 -0800845 struct : public Src {
mtkleine0effd62015-07-29 06:37:28 -0700846 Error draw(SkCanvas* c) const override {
847 c->drawRect(SkRect::MakeWH(1,1), SkPaint());
848 return "";
849 }
mtklein36352bf2015-03-25 18:17:31 -0700850 SkISize size() const override { return SkISize::Make(16, 16); }
mtkleine0effd62015-07-29 06:37:28 -0700851 Name name() const override { return "justOneRect"; }
852 } justOneRect;
mtklein114c3cd2015-01-15 10:15:02 -0800853
mtklein748ca3b2015-01-15 10:56:12 -0800854 SkBitmap bitmap;
855 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800856 SkString log;
mtkleine0effd62015-07-29 06:37:28 -0700857 Error err = sink->draw(justOneRect, &bitmap, &stream, &log);
mtklein4089ef72015-03-05 08:40:28 -0800858 if (err.isFatal()) {
mtkleinc41fd922016-03-08 09:01:39 -0800859 info("Could not run %s: %s\n", config.getTag().c_str(), err.c_str());
mtklein05641a52015-04-21 10:49:13 -0700860 exit(1);
mtklein114c3cd2015-01-15 10:15:02 -0800861 }
862
mtkleine0effd62015-07-29 06:37:28 -0700863 TaggedSink& ts = gSinks.push_back();
mtklein18300a32016-03-16 13:53:35 -0700864 ts.reset(sink.release());
kkinnunen3e980c32015-12-23 01:33:00 -0800865 ts.tag = config.getTag();
mtklein748ca3b2015-01-15 10:56:12 -0800866}
867
868static bool gpu_supported() {
869#if SK_SUPPORT_GPU
870 return FLAGS_gpu;
871#else
872 return false;
873#endif
874}
kkinnunen9ebc3f02015-12-21 23:48:13 -0800875
csmartdalton008b9d82017-02-22 12:00:42 -0700876static Sink* create_sink(const GrContextOptions& grCtxOptions, const SkCommandLineConfig* config) {
kkinnunen3e980c32015-12-23 01:33:00 -0800877#if SK_SUPPORT_GPU
878 if (gpu_supported()) {
879 if (const SkCommandLineConfigGpu* gpuConfig = config->asConfigGpu()) {
bsalomon85b4b532016-04-05 11:06:27 -0700880 GrContextFactory::ContextType contextType = gpuConfig->getContextType();
csmartdaltone812d492017-02-21 12:36:05 -0700881 GrContextFactory::ContextOverrides contextOverrides = gpuConfig->getContextOverrides();
csmartdalton008b9d82017-02-22 12:00:42 -0700882 GrContextFactory testFactory(grCtxOptions);
csmartdaltone812d492017-02-21 12:36:05 -0700883 if (!testFactory.get(contextType, contextOverrides)) {
mtkleinc41fd922016-03-08 09:01:39 -0800884 info("WARNING: can not create GPU context for config '%s'. "
885 "GM tests will be skipped.\n", gpuConfig->getTag().c_str());
kkinnunen3e980c32015-12-23 01:33:00 -0800886 return nullptr;
887 }
Brian Osmanf9810662017-08-30 10:02:10 -0400888 if (gpuConfig->getTestThreading()) {
889 return new GPUThreadTestingSink(contextType, contextOverrides,
890 gpuConfig->getSamples(), gpuConfig->getUseDIText(),
891 gpuConfig->getColorType(),
892 gpuConfig->getAlphaType(),
893 sk_ref_sp(gpuConfig->getColorSpace()),
894 FLAGS_gpu_threading, grCtxOptions);
895 } else {
896 return new GPUSink(contextType, contextOverrides, gpuConfig->getSamples(),
897 gpuConfig->getUseDIText(), gpuConfig->getColorType(),
898 gpuConfig->getAlphaType(), sk_ref_sp(gpuConfig->getColorSpace()),
899 FLAGS_gpu_threading, grCtxOptions);
900 }
kkinnunen3e980c32015-12-23 01:33:00 -0800901 }
902 }
903#endif
904
905#define SINK(t, sink, ...) if (config->getBackend().equals(t)) { return new sink(__VA_ARGS__); }
906
mtklein748ca3b2015-01-15 10:56:12 -0800907 if (FLAGS_cpu) {
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500908 auto srgbColorSpace = SkColorSpace::MakeSRGB();
909 auto srgbLinearColorSpace = SkColorSpace::MakeSRGBLinear();
brianosmanb109b8c2016-06-16 13:03:24 -0700910
Mike Klein515bda62018-01-09 11:21:58 -0500911 SINK("g8", RasterSink, kGray_8_SkColorType);
Matt Sarettd2228302017-03-02 08:53:46 -0500912 SINK("565", RasterSink, kRGB_565_SkColorType);
Mike Klein5340b3f2018-01-24 13:42:05 -0500913 SINK("4444", RasterSink, kARGB_4444_SkColorType);
Matt Sarettd2228302017-03-02 08:53:46 -0500914 SINK("8888", RasterSink, kN32_SkColorType);
915 SINK("srgb", RasterSink, kN32_SkColorType, srgbColorSpace);
Mike Kleinac568a92018-01-25 09:09:32 -0500916 SINK("rgba", RasterSink, kRGBA_8888_SkColorType);
917 SINK("bgra", RasterSink, kBGRA_8888_SkColorType);
918 SINK("rgbx", RasterSink, kRGB_888x_SkColorType);
919 SINK("1010102", RasterSink, kRGBA_1010102_SkColorType);
920 SINK("101010x", RasterSink, kRGB_101010x_SkColorType);
Matt Sarettd2228302017-03-02 08:53:46 -0500921 SINK("f16", RasterSink, kRGBA_F16_SkColorType, srgbLinearColorSpace);
Hal Canaryc7d295e2017-07-20 15:36:00 -0400922 SINK("pdf", PDFSink, false, SK_ScalarDefaultRasterDPI);
Matt Sarettd2228302017-03-02 08:53:46 -0500923 SINK("skp", SKPSink);
924 SINK("pipe", PipeSink);
925 SINK("svg", SVGSink);
926 SINK("null", NullSink);
927 SINK("xps", XPSSink);
Hal Canaryc7d295e2017-07-20 15:36:00 -0400928 SINK("pdfa", PDFSink, true, SK_ScalarDefaultRasterDPI);
929 SINK("pdf300", PDFSink, false, 300);
Hal Canary85c7fe82016-10-25 10:33:27 -0400930 SINK("jsdebug", DebugSink);
mtklein748ca3b2015-01-15 10:56:12 -0800931 }
932#undef SINK
halcanary96fcdcc2015-08-27 07:41:13 -0700933 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800934}
935
Mike Klein841101d2017-03-10 09:55:51 -0500936static sk_sp<SkColorSpace> adobe_rgb() {
937 return SkColorSpace::MakeRGB(SkColorSpace::kSRGB_RenderTargetGamma,
938 SkColorSpace::kAdobeRGB_Gamut);
939}
940
Mike Kleinb7ea3da2017-10-31 17:42:08 +0000941static sk_sp<SkColorSpace> rgb_to_gbr() {
Brian Osman36703d92017-12-12 14:09:31 -0500942 return SkColorSpace::MakeSRGB()->makeColorSpin();
Mike Klein919cc452017-03-18 15:36:52 +0000943}
944
kkinnunen3e980c32015-12-23 01:33:00 -0800945static Sink* create_via(const SkString& tag, Sink* wrapped) {
946#define VIA(t, via, ...) if (tag.equals(t)) { return new via(__VA_ARGS__); }
Mike Klein919cc452017-03-18 15:36:52 +0000947 VIA("adobe", ViaCSXform, wrapped, adobe_rgb(), false);
Mike Kleinb7ea3da2017-10-31 17:42:08 +0000948 VIA("gbr", ViaCSXform, wrapped, rgb_to_gbr(), true);
mtklein9c5052f2016-08-06 12:51:51 -0700949 VIA("lite", ViaLite, wrapped);
reed54dc4872016-09-13 08:09:45 -0700950 VIA("pipe", ViaPipe, wrapped);
Mike Reedbae888e2017-02-18 16:50:45 -0500951#ifdef TEST_VIA_SVG
Mike Reedf67c4592017-02-17 17:06:11 -0500952 VIA("svg", ViaSVG, wrapped);
Mike Reedbae888e2017-02-18 16:50:45 -0500953#endif
halcanary96fcdcc2015-08-27 07:41:13 -0700954 VIA("serialize", ViaSerialization, wrapped);
mtklein4a34ecb2016-01-08 10:19:35 -0800955 VIA("pic", ViaPicture, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700956 VIA("tiles", ViaTiles, 256, 256, nullptr, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800957 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800958
mtkleind603b222015-02-17 11:13:33 -0800959 if (FLAGS_matrix.count() == 4) {
mtklein748ca3b2015-01-15 10:56:12 -0800960 SkMatrix m;
mtkleind603b222015-02-17 11:13:33 -0800961 m.reset();
962 m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
963 m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
964 m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
965 m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
966 VIA("matrix", ViaMatrix, m, wrapped);
967 VIA("upright", ViaUpright, m, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800968 }
tomhudson64de1e12015-03-05 08:01:07 -0800969
mtklein748ca3b2015-01-15 10:56:12 -0800970#undef VIA
halcanary96fcdcc2015-08-27 07:41:13 -0700971 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800972}
973
Ben Wagnerf28d4722017-08-11 17:33:22 -0400974static bool gather_sinks(const GrContextOptions& grCtxOptions, bool defaultConfigs) {
kkinnunen3e980c32015-12-23 01:33:00 -0800975 SkCommandLineConfigArray configs;
976 ParseConfigs(FLAGS_config, &configs);
977 for (int i = 0; i < configs.count(); i++) {
978 const SkCommandLineConfig& config = *configs[i];
csmartdalton008b9d82017-02-22 12:00:42 -0700979 Sink* sink = create_sink(grCtxOptions, &config);
kkinnunen3e980c32015-12-23 01:33:00 -0800980 if (sink == nullptr) {
mtkleinc41fd922016-03-08 09:01:39 -0800981 info("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
982 config.getTag().c_str());
kkinnunen3e980c32015-12-23 01:33:00 -0800983 continue;
984 }
mtklein748ca3b2015-01-15 10:56:12 -0800985
kkinnunen3e980c32015-12-23 01:33:00 -0800986 const SkTArray<SkString>& parts = config.getViaParts();
987 for (int j = parts.count(); j-- > 0;) {
988 const SkString& part = parts[j];
989 Sink* next = create_via(part, sink);
halcanary96fcdcc2015-08-27 07:41:13 -0700990 if (next == nullptr) {
mtkleinc41fd922016-03-08 09:01:39 -0800991 info("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
992 part.c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800993 delete sink;
halcanary96fcdcc2015-08-27 07:41:13 -0700994 sink = nullptr;
mtklein748ca3b2015-01-15 10:56:12 -0800995 break;
996 }
997 sink = next;
998 }
999 if (sink) {
1000 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -08001001 }
1002 }
brianosman05c987c2016-05-09 13:42:16 -07001003
1004 // If no configs were requested (just running tests, perhaps?), then we're okay.
Ben Wagnerf28d4722017-08-11 17:33:22 -04001005 if (configs.count() == 0 ||
1006 // If we're using the default configs, we're okay.
1007 defaultConfigs ||
Ben Wagner32fa5102017-08-10 21:25:55 -04001008 // If we've been told to ignore undefined flags, we're okay.
1009 FLAGS_undefok ||
1010 // Otherwise, make sure that all specified configs have become sinks.
1011 configs.count() == gSinks.count()) {
1012 return true;
1013 }
1014 info("Invalid --config. Use --undefok to bypass this warning.\n");
1015 return false;
mtklein114c3cd2015-01-15 10:15:02 -08001016}
mtklein709d2c32015-01-15 08:30:25 -08001017
mtkleina5114d72015-08-24 13:27:01 -07001018static bool dump_png(SkBitmap bitmap, const char* path, const char* md5) {
Mike Klein13b6afd2017-06-12 12:45:41 -04001019 SkPixmap pm;
1020 if (!bitmap.peekPixels(&pm)) {
1021 return false; // Ought to never happen... we're already read-back at this point.
mtkleina5114d72015-08-24 13:27:01 -07001022 }
Mike Klein13b6afd2017-06-12 12:45:41 -04001023 SkFILEWStream dst{path};
mtkleina5114d72015-08-24 13:27:01 -07001024
mtkleind69ece62015-09-10 10:37:44 -07001025 SkString description;
1026 description.append("Key: ");
1027 for (int i = 0; i < FLAGS_key.count(); i++) {
1028 description.appendf("%s ", FLAGS_key[i]);
1029 }
1030 description.append("Properties: ");
1031 for (int i = 0; i < FLAGS_properties.count(); i++) {
1032 description.appendf("%s ", FLAGS_properties[i]);
1033 }
1034 description.appendf("MD5: %s", md5);
1035
Mike Klein13b6afd2017-06-12 12:45:41 -04001036 const char* comments[] = {
1037 "Author", "DM dump_png()",
1038 "Description", description.c_str(),
1039 };
1040 size_t lengths[] = {
1041 strlen(comments[0])+1, strlen(comments[1])+1,
1042 strlen(comments[2])+1, strlen(comments[3])+1,
1043 };
mtkleina5114d72015-08-24 13:27:01 -07001044
Mike Klein13b6afd2017-06-12 12:45:41 -04001045 SkPngEncoder::Options options;
1046 options.fComments = SkDataTable::MakeCopyArrays((const void**)comments, lengths, 4);
1047 options.fFilterFlags = SkPngEncoder::FilterFlag::kNone;
1048 options.fZLibLevel = 1;
1049 options.fUnpremulBehavior = pm.colorSpace() ? SkTransferFunctionBehavior::kRespect
1050 : SkTransferFunctionBehavior::kIgnore;
1051 return SkPngEncoder::Encode(&dst, pm, options);
mtkleina5114d72015-08-24 13:27:01 -07001052}
1053
mtkleina2ef6422015-01-15 13:44:22 -08001054static bool match(const char* needle, const char* haystack) {
Chris Daltonecf78ac2017-08-15 15:43:08 -06001055 if ('~' == needle[0]) {
1056 return !match(needle + 1, haystack);
1057 }
1058 if (0 == strcmp("_", needle)) {
1059 return true;
1060 }
1061 return nullptr != strstr(haystack, needle);
mtkleina2ef6422015-01-15 13:44:22 -08001062}
1063
mtklein3eed7dd2016-02-24 19:07:07 -08001064static bool is_blacklisted(const char* sink, const char* src,
1065 const char* srcOptions, const char* name) {
mtklein0d243ff2015-04-03 07:51:00 -07001066 for (int i = 0; i < FLAGS_blacklist.count() - 3; i += 4) {
mtkleina2ef6422015-01-15 13:44:22 -08001067 if (match(FLAGS_blacklist[i+0], sink) &&
djsollen54416de2015-04-03 07:24:48 -07001068 match(FLAGS_blacklist[i+1], src) &&
1069 match(FLAGS_blacklist[i+2], srcOptions) &&
1070 match(FLAGS_blacklist[i+3], name)) {
mtklein3eed7dd2016-02-24 19:07:07 -08001071 return true;
mtkleina2ef6422015-01-15 13:44:22 -08001072 }
1073 }
mtklein3eed7dd2016-02-24 19:07:07 -08001074 return false;
mtkleina2ef6422015-01-15 13:44:22 -08001075}
1076
mtkleincd50bca2016-01-05 06:20:20 -08001077// Even when a Task Sink reports to be non-threadsafe (e.g. GPU), we know things like
1078// .png encoding are definitely thread safe. This lets us offload that work to CPU threads.
1079static SkTaskGroup gDefinitelyThreadSafeWork;
1080
mtklein748ca3b2015-01-15 10:56:12 -08001081// The finest-grained unit of work we can run: draw a single Src into a single Sink,
1082// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
1083struct Task {
mtkleine0effd62015-07-29 06:37:28 -07001084 Task(const TaggedSrc& src, const TaggedSink& sink) : src(src), sink(sink) {}
1085 const TaggedSrc& src;
1086 const TaggedSink& sink;
mtklein748ca3b2015-01-15 10:56:12 -08001087
mtklein21eaf3b2016-02-08 12:39:59 -08001088 static void Run(const Task& task) {
1089 SkString name = task.src->name();
mtkleine0effd62015-07-29 06:37:28 -07001090
mtkleinb9eb4ac2015-02-02 18:26:03 -08001091 SkString log;
mtklein3eed7dd2016-02-24 19:07:07 -08001092 if (!FLAGS_dryRun) {
mtklein748ca3b2015-01-15 10:56:12 -08001093 SkBitmap bitmap;
1094 SkDynamicMemoryWStream stream;
mtklein3eed7dd2016-02-24 19:07:07 -08001095 start(task.sink.tag.c_str(), task.src.tag.c_str(),
1096 task.src.options.c_str(), name.c_str());
mtklein21eaf3b2016-02-08 12:39:59 -08001097 Error err = task.sink->draw(*task.src, &bitmap, &stream, &log);
mtkleinb3b13b72016-03-07 13:20:52 -08001098 if (!log.isEmpty()) {
mtkleinc41fd922016-03-08 09:01:39 -08001099 info("%s %s %s %s:\n%s\n", task.sink.tag.c_str()
1100 , task.src.tag.c_str()
1101 , task.src.options.c_str()
1102 , name.c_str()
1103 , log.c_str());
mtkleinb3b13b72016-03-07 13:20:52 -08001104 }
mtklein748ca3b2015-01-15 10:56:12 -08001105 if (!err.isEmpty()) {
mtklein4089ef72015-03-05 08:40:28 -08001106 if (err.isFatal()) {
djsollen54416de2015-04-03 07:24:48 -07001107 fail(SkStringPrintf("%s %s %s %s: %s",
mtklein21eaf3b2016-02-08 12:39:59 -08001108 task.sink.tag.c_str(),
1109 task.src.tag.c_str(),
1110 task.src.options.c_str(),
mtklein4089ef72015-03-05 08:40:28 -08001111 name.c_str(),
1112 err.c_str()));
mtkleinb37cb412015-03-18 05:27:14 -07001113 } else {
mtklein3eed7dd2016-02-24 19:07:07 -08001114 done(task.sink.tag.c_str(), task.src.tag.c_str(),
1115 task.src.options.c_str(), name.c_str());
mtkleinba6ada72016-01-21 09:39:35 -08001116 return;
mtklein4089ef72015-03-05 08:40:28 -08001117 }
mtklein748ca3b2015-01-15 10:56:12 -08001118 }
mtklein62bd1a62015-01-27 14:46:26 -08001119
mtkleincd50bca2016-01-05 06:20:20 -08001120 // We're likely switching threads here, so we must capture by value, [=] or [foo,bar].
Hal Canary0b833192017-03-14 14:35:02 -04001121 SkStreamAsset* data = stream.detachAsStream().release();
mtkleincd50bca2016-01-05 06:20:20 -08001122 gDefinitelyThreadSafeWork.add([task,name,bitmap,data]{
Ben Wagner145dbcd2016-11-03 14:40:50 -04001123 std::unique_ptr<SkStreamAsset> ownedData(data);
mtkleincd50bca2016-01-05 06:20:20 -08001124
mtkleincd50bca2016-01-05 06:20:20 -08001125 SkString md5;
1126 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
1127 SkMD5 hash;
1128 if (data->getLength()) {
1129 hash.writeStream(data, data->getLength());
1130 data->rewind();
mtklein38408462015-07-08 07:25:27 -07001131 } else {
mtkleincd50bca2016-01-05 06:20:20 -08001132 // If we're BGRA (Linux, Windows), swizzle over to RGBA (Mac, Android).
1133 // This helps eliminate multiple 0-pixel-diff hashes on gold.skia.org.
1134 // (Android's general slow speed breaks the tie arbitrarily in RGBA's favor.)
1135 // We might consider promoting 565 to RGBA too.
1136 if (bitmap.colorType() == kBGRA_8888_SkColorType) {
1137 SkBitmap swizzle;
Matt Sarett68b8e3d2017-04-28 11:15:22 -04001138 SkAssertResult(sk_tool_utils::copy_to(&swizzle, kRGBA_8888_SkColorType,
1139 bitmap));
Mike Reedf0ffb892017-10-03 14:47:21 -04001140 hash.write(swizzle.getPixels(), swizzle.computeByteSize());
mtkleincd50bca2016-01-05 06:20:20 -08001141 } else {
Mike Reedf0ffb892017-10-03 14:47:21 -04001142 hash.write(bitmap.getPixels(), bitmap.computeByteSize());
mtkleincd50bca2016-01-05 06:20:20 -08001143 }
1144 }
1145 SkMD5::Digest digest;
1146 hash.finish(digest);
1147 for (int i = 0; i < 16; i++) {
1148 md5.appendf("%02x", digest.data[i]);
mtklein38408462015-07-08 07:25:27 -07001149 }
mtklein62bd1a62015-01-27 14:46:26 -08001150 }
mtklein62bd1a62015-01-27 14:46:26 -08001151
mtkleincd50bca2016-01-05 06:20:20 -08001152 if (!FLAGS_readPath.isEmpty() &&
mtklein3eed7dd2016-02-24 19:07:07 -08001153 !gGold.contains(Gold(task.sink.tag, task.src.tag,
1154 task.src.options, name, md5))) {
mtkleincd50bca2016-01-05 06:20:20 -08001155 fail(SkStringPrintf("%s not found for %s %s %s %s in %s",
1156 md5.c_str(),
mtklein21eaf3b2016-02-08 12:39:59 -08001157 task.sink.tag.c_str(),
1158 task.src.tag.c_str(),
1159 task.src.options.c_str(),
mtkleincd50bca2016-01-05 06:20:20 -08001160 name.c_str(),
1161 FLAGS_readPath[0]));
mtklein748ca3b2015-01-15 10:56:12 -08001162 }
mtkleincd50bca2016-01-05 06:20:20 -08001163
1164 if (!FLAGS_writePath.isEmpty()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001165 const char* ext = task.sink->fileExtension();
Brian Osmanf1942de2017-08-30 14:50:22 -04001166 if (ext && !FLAGS_dont_write.contains(ext)) {
Mike Klein97d6a7a2017-07-24 10:37:19 -04001167 if (data->getLength()) {
1168 WriteToDisk(task, md5, ext, data, data->getLength(), nullptr);
1169 SkASSERT(bitmap.drawsNothing());
1170 } else if (!bitmap.drawsNothing()) {
1171 WriteToDisk(task, md5, ext, nullptr, 0, &bitmap);
1172 }
mtkleincd50bca2016-01-05 06:20:20 -08001173 }
1174 }
1175 });
mtklein748ca3b2015-01-15 10:56:12 -08001176 }
mtklein3eed7dd2016-02-24 19:07:07 -08001177 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 -08001178 }
1179
1180 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -08001181 SkString md5,
1182 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -08001183 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -08001184 const SkBitmap* bitmap) {
mtklein409d4702016-02-29 07:38:01 -08001185 bool gammaCorrect = false;
1186 if (bitmap) {
Brian Osman7039f742016-11-01 15:56:16 -04001187 gammaCorrect = SkToBool(bitmap->info().colorSpace());
mtklein409d4702016-02-29 07:38:01 -08001188 }
1189
mtklein748ca3b2015-01-15 10:56:12 -08001190 JsonWriter::BitmapResult result;
djsollen54416de2015-04-03 07:24:48 -07001191 result.name = task.src->name();
mtklein3eed7dd2016-02-24 19:07:07 -08001192 result.config = task.sink.tag;
djsollen54416de2015-04-03 07:24:48 -07001193 result.sourceType = task.src.tag;
1194 result.sourceOptions = task.src.options;
1195 result.ext = ext;
mtklein409d4702016-02-29 07:38:01 -08001196 result.gammaCorrect = gammaCorrect;
djsollen54416de2015-04-03 07:24:48 -07001197 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -08001198 JsonWriter::AddBitmapResult(result);
1199
mtkleinb0531a72015-04-07 13:38:48 -07001200 // If an MD5 is uninteresting, we want it noted in the JSON file,
1201 // but don't want to dump it out as a .png (or whatever ext is).
1202 if (gUninterestingHashes.contains(md5)) {
1203 return;
1204 }
1205
mtklein748ca3b2015-01-15 10:56:12 -08001206 const char* dir = FLAGS_writePath[0];
1207 if (0 == strcmp(dir, "@")) { // Needed for iOS.
1208 dir = FLAGS_resourcePath[0];
1209 }
1210 sk_mkdir(dir);
1211
1212 SkString path;
1213 if (FLAGS_nameByHash) {
1214 path = SkOSPath::Join(dir, result.md5.c_str());
1215 path.append(".");
1216 path.append(ext);
1217 if (sk_exists(path.c_str())) {
1218 return; // Content-addressed. If it exists already, we're done.
1219 }
1220 } else {
kkinnunen3e980c32015-12-23 01:33:00 -08001221 path = SkOSPath::Join(dir, task.sink.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001222 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001223 path = SkOSPath::Join(path.c_str(), task.src.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001224 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001225 if (strcmp(task.src.options.c_str(), "") != 0) {
1226 path = SkOSPath::Join(path.c_str(), task.src.options.c_str());
djsollen54416de2015-04-03 07:24:48 -07001227 sk_mkdir(path.c_str());
1228 }
mtklein748ca3b2015-01-15 10:56:12 -08001229 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
1230 path.append(".");
1231 path.append(ext);
1232 }
1233
mtklein748ca3b2015-01-15 10:56:12 -08001234 if (bitmap) {
mtkleina5114d72015-08-24 13:27:01 -07001235 if (!dump_png(*bitmap, path.c_str(), result.md5.c_str())) {
mtklein748ca3b2015-01-15 10:56:12 -08001236 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
1237 return;
1238 }
1239 } else {
mtkleina5114d72015-08-24 13:27:01 -07001240 SkFILEWStream file(path.c_str());
1241 if (!file.isValid()) {
1242 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
1243 return;
1244 }
mtklein748ca3b2015-01-15 10:56:12 -08001245 if (!file.writeStream(data, len)) {
1246 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
1247 return;
1248 }
1249 }
1250 }
1251};
1252
mtklein748ca3b2015-01-15 10:56:12 -08001253/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1254
1255// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
1256
mtklein21eaf3b2016-02-08 12:39:59 -08001257static SkTDArray<skiatest::Test> gParallelTests, gSerialTests;
mtklein748ca3b2015-01-15 10:56:12 -08001258
1259static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -08001260 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -08001261 return;
1262 }
mtklein6393c062015-04-27 08:45:01 -07001263 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r; r = r->next()) {
1264 if (!in_shard()) {
1265 continue;
1266 }
halcanary87f3ba42015-01-20 09:30:20 -08001267 // Despite its name, factory() is returning a reference to
1268 // link-time static const POD data.
1269 const skiatest::Test& test = r->factory();
1270 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -08001271 continue;
1272 }
halcanary87f3ba42015-01-20 09:30:20 -08001273 if (test.needsGpu && gpu_supported()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001274 (FLAGS_gpu_threading ? gParallelTests : gSerialTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -08001275 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein21eaf3b2016-02-08 12:39:59 -08001276 gParallelTests.push(test);
mtklein82d28432015-01-15 12:46:02 -08001277 }
mtklein748ca3b2015-01-15 10:56:12 -08001278 }
1279}
1280
csmartdalton008b9d82017-02-22 12:00:42 -07001281static void run_test(skiatest::Test test, const GrContextOptions& grCtxOptions) {
halcanary87f3ba42015-01-20 09:30:20 -08001282 struct : public skiatest::Reporter {
mtklein36352bf2015-03-25 18:17:31 -07001283 void reportFailed(const skiatest::Failure& failure) override {
halcanary87f3ba42015-01-20 09:30:20 -08001284 fail(failure.toString());
1285 JsonWriter::AddTestFailure(failure);
1286 }
mtklein36352bf2015-03-25 18:17:31 -07001287 bool allowExtendedTest() const override {
halcanary87f3ba42015-01-20 09:30:20 -08001288 return FLAGS_pathOpsExtended;
1289 }
mtklein36352bf2015-03-25 18:17:31 -07001290 bool verbose() const override { return FLAGS_veryVerbose; }
halcanary87f3ba42015-01-20 09:30:20 -08001291 } reporter;
djsollen824996a2015-06-12 12:06:22 -07001292
mtklein3eed7dd2016-02-24 19:07:07 -08001293 if (!FLAGS_dryRun && !is_blacklisted("_", "tests", "_", test.name)) {
Robert Phillipsec325342017-10-30 18:02:48 +00001294 GrContextOptions options = grCtxOptions;
1295 test.modifyGrContextOptions(&options);
1296
mtklein21eaf3b2016-02-08 12:39:59 -08001297 start("unit", "test", "", test.name);
Brian Salomondcfca432017-11-15 15:48:03 -05001298 test.run(&reporter, options);
mtklein748ca3b2015-01-15 10:56:12 -08001299 }
mtklein3eed7dd2016-02-24 19:07:07 -08001300 done("unit", "test", "", test.name);
mtklein748ca3b2015-01-15 10:56:12 -08001301}
1302
1303/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1304
caryclark83ca6282015-06-10 09:31:09 -07001305#define PORTABLE_FONT_PREFIX "Toy Liberation "
1306
mbocee6a9912016-05-31 11:42:36 -07001307static sk_sp<SkTypeface> create_from_name(const char familyName[], SkFontStyle style) {
caryclark83ca6282015-06-10 09:31:09 -07001308 if (familyName && strlen(familyName) > sizeof(PORTABLE_FONT_PREFIX)
1309 && !strncmp(familyName, PORTABLE_FONT_PREFIX, sizeof(PORTABLE_FONT_PREFIX) - 1)) {
caryclark1818acb2015-07-24 12:09:25 -07001310 return sk_tool_utils::create_portable_typeface(familyName, style);
caryclark83ca6282015-06-10 09:31:09 -07001311 }
halcanary96fcdcc2015-08-27 07:41:13 -07001312 return nullptr;
caryclark83ca6282015-06-10 09:31:09 -07001313}
1314
1315#undef PORTABLE_FONT_PREFIX
1316
mbocee6a9912016-05-31 11:42:36 -07001317extern sk_sp<SkTypeface> (*gCreateTypefaceDelegate)(const char [], SkFontStyle );
caryclark83ca6282015-06-10 09:31:09 -07001318
Mike Kleinbe28ee22017-02-06 12:46:20 -05001319int main(int argc, char** argv) {
Dongwon Kang0c7861f2018-02-16 10:55:21 -08001320#if defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) && defined(SK_HAS_HEIF_LIBRARY)
1321 android::ProcessState::self()->startThreadPool();
1322#endif
Mike Kleinbe28ee22017-02-06 12:46:20 -05001323 SkCommandLineFlags::Parse(argc, argv);
Brian Osman53136aa2017-07-20 15:43:35 -04001324
Mike Klein31868212017-11-06 12:02:47 -05001325 if (!FLAGS_nativeFonts) {
Hal Canaryccafca02017-11-14 10:34:48 -05001326 gSkFontMgr_DefaultFactory = &DM::MakeFontMgr;
Mike Klein31868212017-11-06 12:02:47 -05001327 }
1328
Mike Klein0c5fdcb2017-11-13 12:40:46 -05001329#if defined(SK_BUILD_FOR_WIN)
1330 if (FLAGS_gdi) {
Hal Canaryccafca02017-11-14 10:34:48 -05001331 gSkFontMgr_DefaultFactory = &SkFontMgr_New_GDI;
Mike Klein0c5fdcb2017-11-13 12:40:46 -05001332 }
1333#endif
1334
Brian Osmanbc8150f2017-07-24 11:38:01 -04001335 initializeEventTracingForTools();
Brian Osman53136aa2017-07-20 15:43:35 -04001336
Mike Klein6613cc52017-12-19 09:09:33 -05001337#if !defined(SK_BUILD_FOR_GOOGLE3) && defined(SK_BUILD_FOR_IOS)
Mike Kleinadacaef2017-02-06 09:26:14 -05001338 cd_Documents();
1339#endif
bsalomon71de3532016-04-26 14:27:21 -07001340 setbuf(stdout, nullptr);
mtklein246ba3a2016-02-23 10:39:36 -08001341 setup_crash_handler();
mtkleinafae30a2016-02-24 12:28:32 -08001342
Yuqian Liba62b4a2016-12-14 13:46:53 -05001343 gSkUseAnalyticAA = FLAGS_analyticAA;
Yuqian Lidf60e362017-07-25 11:26:31 -04001344 gSkUseDeltaAA = FLAGS_deltaAA;
liyuqian38911a72016-10-04 11:23:22 -07001345
Yuqian Li550148b2017-01-13 10:13:13 -05001346 if (FLAGS_forceAnalyticAA) {
1347 gSkForceAnalyticAA = true;
1348 }
Yuqian Lia81b6262017-09-06 17:10:05 -04001349 if (FLAGS_forceDeltaAA) {
1350 gSkForceDeltaAA = true;
1351 }
Mike Reed7c26ca72017-07-05 15:45:52 -04001352 if (FLAGS_forceRasterPipeline) {
1353 gSkForceRasterPipelineBlitter = true;
1354 }
Yuqian Li550148b2017-01-13 10:13:13 -05001355
Mike Kleine9f187b2017-06-26 13:23:12 -04001356 // The bots like having a verbose.log to upload, so always touch the file even if --verbose.
1357 if (!FLAGS_writePath.isEmpty()) {
mtklein51c8cfc2016-03-11 12:59:09 -08001358 sk_mkdir(FLAGS_writePath[0]);
mtkleina1ce2162016-07-22 12:23:46 -07001359 gVLog = fopen(SkOSPath::Join(FLAGS_writePath[0], "verbose.log").c_str(), "w");
mtkleinc41fd922016-03-08 09:01:39 -08001360 }
Mike Kleine9f187b2017-06-26 13:23:12 -04001361 if (FLAGS_verbose) {
1362 gVLog = stderr;
1363 }
mtkleinc41fd922016-03-08 09:01:39 -08001364
csmartdalton008b9d82017-02-22 12:00:42 -07001365 GrContextOptions grCtxOptions;
1366#if SK_SUPPORT_GPU
Chris Dalton040238b2017-12-18 14:22:34 -07001367 SetCtxOptionsFromCommonFlags(&grCtxOptions);
csmartdalton008b9d82017-02-22 12:00:42 -07001368#endif
1369
mtklein5286f022016-01-22 08:18:14 -08001370 JsonWriter::DumpJson(); // It's handy for the bots to assume this is ~never missing.
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +00001371 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -07001372 SkTaskGroup::Enabler enabled(FLAGS_threads);
caryclark83ca6282015-06-10 09:31:09 -07001373 gCreateTypefaceDelegate = &create_from_name;
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +00001374
Mike Reed0933bc92017-12-09 01:27:41 +00001375 if (nullptr == GetResourceAsData("images/color_wheel.png")) {
1376 info("Some resources are missing. Do you need to set --resourcePath?\n");
halcanary7d571242016-02-24 17:59:16 -08001377 }
mtklein62bd1a62015-01-27 14:46:26 -08001378 gather_gold();
borenet09ed4802015-04-03 14:15:33 -07001379 gather_uninteresting_hashes();
mtklein62bd1a62015-01-27 14:46:26 -08001380
scroggo86737142016-02-03 12:19:11 -08001381 if (!gather_srcs()) {
1382 return 1;
1383 }
Ben Wagnerf28d4722017-08-11 17:33:22 -04001384 // TODO(dogben): This is a bit ugly. Find a cleaner way to do this.
1385 bool defaultConfigs = true;
1386 for (int i = 0; i < argc; i++) {
1387 static const char* kConfigArg = "--config";
1388 if (strcmp(argv[i], kConfigArg) == 0) {
1389 defaultConfigs = false;
1390 break;
1391 }
1392 }
1393 if (!gather_sinks(grCtxOptions, defaultConfigs)) {
brianosman05c987c2016-05-09 13:42:16 -07001394 return 1;
1395 }
mtklein748ca3b2015-01-15 10:56:12 -08001396 gather_tests();
mtklein21eaf3b2016-02-08 12:39:59 -08001397 gPending = gSrcs.count() * gSinks.count() + gParallelTests.count() + gSerialTests.count();
Brian Osmand0677bc2017-08-03 12:13:47 -04001398 info("%d srcs * %d sinks + %d tests == %d tasks\n",
mtkleinc41fd922016-03-08 09:01:39 -08001399 gSrcs.count(), gSinks.count(), gParallelTests.count() + gSerialTests.count(), gPending);
mtklein748ca3b2015-01-15 10:56:12 -08001400
mtklein21eaf3b2016-02-08 12:39:59 -08001401 // Kick off as much parallel work as we can, making note of any serial work we'll need to do.
1402 SkTaskGroup parallel;
1403 SkTArray<Task> serial;
1404
1405 for (auto& sink : gSinks)
1406 for (auto& src : gSrcs) {
mtklein3eed7dd2016-02-24 19:07:07 -08001407 if (src->veto(sink->flags()) ||
1408 is_blacklisted(sink.tag.c_str(), src.tag.c_str(),
1409 src.options.c_str(), src->name().c_str())) {
mtkleinf10637f2016-06-10 13:56:35 -07001410 SkAutoMutexAcquire lock(gMutex);
mtklein3eed7dd2016-02-24 19:07:07 -08001411 gPending--;
1412 continue;
1413 }
1414
mtklein21eaf3b2016-02-08 12:39:59 -08001415 Task task(src, sink);
1416 if (src->serial() || sink->serial()) {
1417 serial.push_back(task);
1418 } else {
1419 parallel.add([task] { Task::Run(task); });
mtklein748ca3b2015-01-15 10:56:12 -08001420 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001421 }
mtklein21eaf3b2016-02-08 12:39:59 -08001422 for (auto test : gParallelTests) {
csmartdalton008b9d82017-02-22 12:00:42 -07001423 parallel.add([test, grCtxOptions] { run_test(test, grCtxOptions); });
mtklein55e88b22015-01-21 15:50:13 -08001424 }
mtklein21eaf3b2016-02-08 12:39:59 -08001425
1426 // With the parallel work running, run serial tasks and tests here on main thread.
1427 for (auto task : serial) { Task::Run(task); }
csmartdalton008b9d82017-02-22 12:00:42 -07001428 for (auto test : gSerialTests) { run_test(test, grCtxOptions); }
mtklein21eaf3b2016-02-08 12:39:59 -08001429
1430 // Wait for any remaining parallel work to complete (including any spun off of serial tasks).
1431 parallel.wait();
mtkleincd50bca2016-01-05 06:20:20 -08001432 gDefinitelyThreadSafeWork.wait();
1433
mtkleinda884c42016-02-24 18:00:23 -08001434 // We'd better have run everything.
1435 SkASSERT(gPending == 0);
mtkleine027f172016-02-26 15:53:06 -08001436 // Make sure we've flushed all our results to disk.
1437 JsonWriter::DumpJson();
mtkleinda884c42016-02-24 18:00:23 -08001438
mtklein748ca3b2015-01-15 10:56:12 -08001439 // At this point we're back in single-threaded land.
caryclarkf53ce802015-06-15 06:48:30 -07001440 sk_tool_utils::release_portable_typefaces();
mtklein@google.comd36522d2013-10-16 13:02:15 +00001441
mtklein748ca3b2015-01-15 10:56:12 -08001442 if (gFailures.count() > 0) {
mtkleinc41fd922016-03-08 09:01:39 -08001443 info("Failures:\n");
mtklein748ca3b2015-01-15 10:56:12 -08001444 for (int i = 0; i < gFailures.count(); i++) {
mtkleinc41fd922016-03-08 09:01:39 -08001445 info("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001446 }
mtkleinc41fd922016-03-08 09:01:39 -08001447 info("%d failures\n", gFailures.count());
mtklein748ca3b2015-01-15 10:56:12 -08001448 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001449 }
mtkleinafae30a2016-02-24 12:28:32 -08001450
1451#ifdef SK_PDF_IMAGE_STATS
halcanary7a14b312015-10-01 07:28:13 -07001452 SkPDFImageDumpStats();
mtkleinafae30a2016-02-24 12:28:32 -08001453#endif // SK_PDF_IMAGE_STATS
1454
Herb Derby5a523fe2017-01-26 16:48:28 -05001455 SkGraphics::PurgeAllCaches();
mtkleinc41fd922016-03-08 09:01:39 -08001456 info("Finished!\n");
Brian Osman53136aa2017-07-20 15:43:35 -04001457
mtklein748ca3b2015-01-15 10:56:12 -08001458 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +00001459}