blob: 3ddc8272ce68a4b1514375395abe5ff1126097ab [file] [log] [blame]
scroggo478652e2015-03-25 07:11:02 -07001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
mtklein748ca3b2015-01-15 10:56:12 -08008#include "DMJsonWriter.h"
9#include "DMSrcSink.h"
mtklein748ca3b2015-01-15 10:56:12 -080010#include "ProcStats.h"
halcanary7d571242016-02-24 17:59:16 -080011#include "Resources.h"
mtklein748ca3b2015-01-15 10:56:12 -080012#include "SkBBHFactory.h"
mtklein62bd1a62015-01-27 14:46:26 -080013#include "SkChecksum.h"
scroggocc2feb12015-08-14 08:32:46 -070014#include "SkCodec.h"
mtklein27c3fdd2016-02-26 14:43:21 -080015#include "SkColorPriv.h"
msarett888dc162016-05-23 10:21:17 -070016#include "SkColorSpace.h"
Mike Klein919cc452017-03-18 15:36:52 +000017#include "SkColorSpacePriv.h"
caryclark17f0b6d2014-07-22 10:15:34 -070018#include "SkCommonFlags.h"
kkinnunen3e980c32015-12-23 01:33:00 -080019#include "SkCommonFlagsConfig.h"
csmartdalton008b9d82017-02-22 12:00:42 -070020#include "SkCommonFlagsPathRenderer.h"
brianosman3c579dc2016-04-19 09:18:11 -070021#include "SkData.h"
caryclark83ca6282015-06-10 09:31:09 -070022#include "SkFontMgr.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +000023#include "SkGraphics.h"
mtklein27c3fdd2016-02-26 14:43:21 -080024#include "SkHalf.h"
halcanary4dbbd042016-06-07 17:21:10 -070025#include "SkLeanWindows.h"
mtklein748ca3b2015-01-15 10:56:12 -080026#include "SkMD5.h"
mtklein1b249332015-07-07 12:21:21 -070027#include "SkMutex.h"
mtklein1d0f1642014-09-08 08:05:18 -070028#include "SkOSFile.h"
Ben Wagnerbf111d72016-11-07 18:05:29 -050029#include "SkOSPath.h"
mtklein27c3fdd2016-02-26 14:43:21 -080030#include "SkPM4fPriv.h"
Mike Klein13b6afd2017-06-12 12:45:41 -040031#include "SkPngEncoder.h"
mtklein246ba3a2016-02-23 10:39:36 -080032#include "SkSpinlock.h"
mtkleina82f5622015-02-20 12:30:19 -080033#include "SkTHash.h"
mtklein406654b2014-09-03 15:34:37 -070034#include "SkTaskGroup.h"
mtkleinde6fc2b2015-03-12 06:28:54 -070035#include "SkThreadUtils.h"
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +000036#include "Test.h"
mtklein748ca3b2015-01-15 10:56:12 -080037#include "Timer.h"
Mike Kleinadacaef2017-02-06 09:26:14 -050038#include "ios_utils.h"
brianosman3c579dc2016-04-19 09:18:11 -070039#include "picture_utils.h"
caryclark83ca6282015-06-10 09:31:09 -070040#include "sk_tool_utils.h"
liyuqian38911a72016-10-04 11:23:22 -070041#include "SkScan.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +000042
scroggo19b91532016-10-24 09:03:26 -070043#include <vector>
44
halcanary7a14b312015-10-01 07:28:13 -070045#ifdef SK_PDF_IMAGE_STATS
46extern void SkPDFImageDumpStats();
47#endif
48
mtkleina5114d72015-08-24 13:27:01 -070049#include "png.h"
50
bungeman60e0fee2015-08-26 05:15:46 -070051#include <stdlib.h>
52
scroggo27a58342015-10-28 08:56:41 -070053#ifndef SK_BUILD_FOR_WIN32
54 #include <unistd.h>
55#endif
56
Mike Reed7c26ca72017-07-05 15:45:52 -040057extern bool gSkForceRasterPipelineBlitter;
58
djsollen54416de2015-04-03 07:24:48 -070059DEFINE_string(src, "tests gm skp image", "Source types to test.");
mtklein748ca3b2015-01-15 10:56:12 -080060DEFINE_bool(nameByHash, false,
61 "If true, write to FLAGS_writePath[0]/<hash>.png instead of "
djsollen54416de2015-04-03 07:24:48 -070062 "to FLAGS_writePath[0]/<config>/<sourceType>/<sourceOptions>/<name>.png");
mtklein748ca3b2015-01-15 10:56:12 -080063DEFINE_bool2(pathOpsExtended, x, false, "Run extended pathOps tests.");
mtkleind603b222015-02-17 11:13:33 -080064DEFINE_string(matrix, "1 0 0 1",
65 "2x2 scale+skew matrix to apply or upright when using "
66 "'matrix' or 'upright' in config.");
mtklein82d28432015-01-15 12:46:02 -080067DEFINE_bool(gpu_threading, false, "Allow GPU work to run on multiple threads?");
mtklein@google.comd36522d2013-10-16 13:02:15 +000068
mtkleina2ef6422015-01-15 13:44:22 -080069DEFINE_string(blacklist, "",
djsollen54416de2015-04-03 07:24:48 -070070 "Space-separated config/src/srcOptions/name quadruples to blacklist. '_' matches anything. E.g. \n"
71 "'--blacklist gpu skp _ _' will blacklist all SKPs drawn into the gpu config.\n"
72 "'--blacklist gpu skp _ _ 8888 gm _ aarects' will also blacklist the aarects GM on 8888.");
mtkleina2ef6422015-01-15 13:44:22 -080073
mtklein62bd1a62015-01-27 14:46:26 -080074DEFINE_string2(readPath, r, "", "If set check for equality with golden results in this directory.");
75
borenet09ed4802015-04-03 14:15:33 -070076DEFINE_string(uninterestingHashesFile, "",
77 "File containing a list of uninteresting hashes. If a result hashes to something in "
78 "this list, no image is written for that result.");
79
mtklein6393c062015-04-27 08:45:01 -070080DEFINE_int32(shards, 1, "We're splitting source data into this many shards.");
81DEFINE_int32(shard, 0, "Which shard do I run?");
82
halcanary45420a92016-06-02 12:41:14 -070083DEFINE_string(mskps, "", "Directory to read mskps from, or a single mskp file.");
Mike Reed7c26ca72017-07-05 15:45:52 -040084DEFINE_bool(forceRasterPipeline, false, "sets gSkForceRasterPipelineBlitter");
halcanary45420a92016-06-02 12:41:14 -070085
csmartdalton008b9d82017-02-22 12:00:42 -070086#if SK_SUPPORT_GPU
87DEFINE_pathrenderer_flag;
88#endif
89
mtklein748ca3b2015-01-15 10:56:12 -080090using namespace DM;
bsalomon3724e572016-03-30 18:56:19 -070091using sk_gpu_test::GrContextFactory;
bsalomon273c0f52016-03-31 10:59:06 -070092using sk_gpu_test::GLTestContext;
bsalomonf2f1c172016-04-05 12:59:06 -070093using sk_gpu_test::ContextInfo;
mtklein@google.comd36522d2013-10-16 13:02:15 +000094
mtklein748ca3b2015-01-15 10:56:12 -080095/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
96
benjaminwagnerec4d4d72016-03-25 12:59:53 -070097static const double kStartMs = SkTime::GetMSecs();
mtkleinc41fd922016-03-08 09:01:39 -080098
99static FILE* gVLog;
100
101template <typename... Args>
102static void vlog(const char* fmt, Args&&... args) {
103 if (gVLog) {
104 fprintf(gVLog, "%s\t", HumanizeMs(SkTime::GetMSecs() - kStartMs).c_str());
105 fprintf(gVLog, fmt, args...);
106 fflush(gVLog);
107 }
108}
109
110template <typename... Args>
111static void info(const char* fmt, Args&&... args) {
112 vlog(fmt, args...);
113 if (!FLAGS_quiet) {
114 printf(fmt, args...);
115 }
116}
117static void info(const char* fmt) {
118 if (!FLAGS_quiet) {
119 printf("%s", fmt); // Clang warns printf(fmt) is insecure.
120 }
121}
122
reed086eea92016-05-04 17:12:46 -0700123SK_DECLARE_STATIC_MUTEX(gFailuresMutex);
mtklein748ca3b2015-01-15 10:56:12 -0800124static SkTArray<SkString> gFailures;
125
mtklein3eed7dd2016-02-24 19:07:07 -0800126static void fail(const SkString& err) {
mtklein748ca3b2015-01-15 10:56:12 -0800127 SkAutoMutexAcquire lock(gFailuresMutex);
128 SkDebugf("\n\nFAILURE: %s\n\n", err.c_str());
129 gFailures.push_back(err);
halcanary9e398f72015-01-10 11:18:04 -0800130}
131
mtklein0f7748a2016-07-25 15:27:29 -0700132struct Running {
133 SkString id;
134 SkThreadID thread;
135
136 void dump() const {
137 info("\t%s\n", id.c_str());
138 }
139};
mtkleinb37cb412015-03-18 05:27:14 -0700140
mtklein246ba3a2016-02-23 10:39:36 -0800141// We use a spinlock to make locking this in a signal handler _somewhat_ safe.
Mike Klein03a95ad2017-06-23 12:36:32 -0400142static SkSpinlock gMutex;
143static int gPending;
mtklein0f7748a2016-07-25 15:27:29 -0700144static SkTArray<Running> gRunning;
mtklein748ca3b2015-01-15 10:56:12 -0800145
mtklein3eed7dd2016-02-24 19:07:07 -0800146static void done(const char* config, const char* src, const char* srcOptions, const char* name) {
147 SkString id = SkStringPrintf("%s %s %s %s", config, src, srcOptions, name);
mtkleinc41fd922016-03-08 09:01:39 -0800148 vlog("done %s\n", id.c_str());
mtkleinafae30a2016-02-24 12:28:32 -0800149 int pending;
mtkleinb37cb412015-03-18 05:27:14 -0700150 {
mtkleinf10637f2016-06-10 13:56:35 -0700151 SkAutoMutexAcquire lock(gMutex);
mtkleinb37cb412015-03-18 05:27:14 -0700152 for (int i = 0; i < gRunning.count(); i++) {
mtklein0f7748a2016-07-25 15:27:29 -0700153 if (gRunning[i].id == id) {
mtkleinb37cb412015-03-18 05:27:14 -0700154 gRunning.removeShuffle(i);
155 break;
156 }
157 }
mtkleinafae30a2016-02-24 12:28:32 -0800158 pending = --gPending;
reed50bc0512015-05-19 14:13:31 -0700159 }
Mike Klein03a95ad2017-06-23 12:36:32 -0400160
161 // We write out dm.json file and print out a progress update every once in a while.
162 // Notice this also handles the final dm.json and progress update when pending == 0.
mtkleina17241b2015-01-23 05:48:00 -0800163 if (pending % 500 == 0) {
164 JsonWriter::DumpJson();
Mike Klein03a95ad2017-06-23 12:36:32 -0400165
166 int curr = sk_tools::getCurrResidentSetSizeMB(),
167 peak = sk_tools::getMaxResidentSetSizeMB();
168 SkString elapsed = HumanizeMs(SkTime::GetMSecs() - kStartMs);
169
170 SkAutoMutexAcquire lock(gMutex);
171 info("\n%dMB RAM, %dMB peak, %s elapsed, %d queued, %d active:\n",
172 curr, peak, elapsed.c_str(), gPending - gRunning.count(), gRunning.count());
173 for (auto& task : gRunning) {
174 task.dump();
175 }
mtkleina17241b2015-01-23 05:48:00 -0800176 }
mtklein@google.comd36522d2013-10-16 13:02:15 +0000177}
178
mtklein3eed7dd2016-02-24 19:07:07 -0800179static void start(const char* config, const char* src, const char* srcOptions, const char* name) {
180 SkString id = SkStringPrintf("%s %s %s %s", config, src, srcOptions, name);
mtkleinc41fd922016-03-08 09:01:39 -0800181 vlog("start %s\n", id.c_str());
mtkleinf10637f2016-06-10 13:56:35 -0700182 SkAutoMutexAcquire lock(gMutex);
mtklein0f7748a2016-07-25 15:27:29 -0700183 gRunning.push_back({id,SkGetThreadID()});
mtkleinb37cb412015-03-18 05:27:14 -0700184}
185
Mike Klein3cc2d202016-10-24 11:39:43 -0400186static void find_culprit() {
187 // Assumes gMutex is locked.
188 SkThreadID thisThread = SkGetThreadID();
189 for (auto& task : gRunning) {
190 if (task.thread == thisThread) {
191 info("Likely culprit:\n");
192 task.dump();
mtklein0f7748a2016-07-25 15:27:29 -0700193 }
194 }
Mike Klein3cc2d202016-10-24 11:39:43 -0400195}
mtklein0f7748a2016-07-25 15:27:29 -0700196
mtklein246ba3a2016-02-23 10:39:36 -0800197#if defined(SK_BUILD_FOR_WIN32)
mtklein5b64dab2016-06-09 08:59:48 -0700198 static LONG WINAPI crash_handler(EXCEPTION_POINTERS* e) {
mtkleinf8557292016-02-29 06:35:28 -0800199 static const struct {
200 const char* name;
robertphillips75467862016-03-01 14:10:23 -0800201 DWORD code;
mtkleinf8557292016-02-29 06:35:28 -0800202 } kExceptions[] = {
203 #define _(E) {#E, E}
204 _(EXCEPTION_ACCESS_VIOLATION),
205 _(EXCEPTION_BREAKPOINT),
206 _(EXCEPTION_INT_DIVIDE_BY_ZERO),
207 _(EXCEPTION_STACK_OVERFLOW),
208 // TODO: more?
209 #undef _
210 };
211
mtkleinf10637f2016-06-10 13:56:35 -0700212 SkAutoMutexAcquire lock(gMutex);
mtklein5b64dab2016-06-09 08:59:48 -0700213
214 const DWORD code = e->ExceptionRecord->ExceptionCode;
215 info("\nCaught exception %u", code);
216 for (const auto& exception : kExceptions) {
217 if (exception.code == code) {
218 info(" %s", exception.name);
mtkleinf8557292016-02-29 06:35:28 -0800219 }
mtkleinf8557292016-02-29 06:35:28 -0800220 }
mtklein5b64dab2016-06-09 08:59:48 -0700221 info(", was running:\n");
222 for (auto& task : gRunning) {
mtklein0f7748a2016-07-25 15:27:29 -0700223 task.dump();
mtklein5b64dab2016-06-09 08:59:48 -0700224 }
mtklein0f7748a2016-07-25 15:27:29 -0700225 find_culprit();
mtklein5b64dab2016-06-09 08:59:48 -0700226 fflush(stdout);
227
mtkleinf8557292016-02-29 06:35:28 -0800228 // Execute default exception handler... hopefully, exit.
229 return EXCEPTION_EXECUTE_HANDLER;
mtklein246ba3a2016-02-23 10:39:36 -0800230 }
231
Mike Klein3cc2d202016-10-24 11:39:43 -0400232 static void setup_crash_handler() {
233 SetUnhandledExceptionFilter(crash_handler);
234 }
235#else
mtklein246ba3a2016-02-23 10:39:36 -0800236 #include <signal.h>
Mike Klein3cc2d202016-10-24 11:39:43 -0400237 #if !defined(SK_BUILD_FOR_ANDROID)
238 #include <execinfo.h>
239 #endif
240
241 static constexpr int max_of() { return 0; }
242 template <typename... Rest>
243 static constexpr int max_of(int x, Rest... rest) {
244 return x > max_of(rest...) ? x : max_of(rest...);
245 }
246
247 static void (*previous_handler[max_of(SIGABRT,SIGBUS,SIGFPE,SIGILL,SIGSEGV)+1])(int);
mtkleinf8557292016-02-29 06:35:28 -0800248
mtklein5b64dab2016-06-09 08:59:48 -0700249 static void crash_handler(int sig) {
mtkleinf10637f2016-06-10 13:56:35 -0700250 SkAutoMutexAcquire lock(gMutex);
mtklein5b64dab2016-06-09 08:59:48 -0700251
252 info("\nCaught signal %d [%s], was running:\n", sig, strsignal(sig));
253 for (auto& task : gRunning) {
mtklein0f7748a2016-07-25 15:27:29 -0700254 task.dump();
mtklein5b64dab2016-06-09 08:59:48 -0700255 }
mtklein0f7748a2016-07-25 15:27:29 -0700256 find_culprit();
mtklein5b64dab2016-06-09 08:59:48 -0700257
Mike Klein3cc2d202016-10-24 11:39:43 -0400258 #if !defined(SK_BUILD_FOR_ANDROID)
mtklein5b64dab2016-06-09 08:59:48 -0700259 void* stack[64];
260 int count = backtrace(stack, SK_ARRAY_COUNT(stack));
261 char** symbols = backtrace_symbols(stack, count);
262 info("\nStack trace:\n");
263 for (int i = 0; i < count; i++) {
264 info(" %s\n", symbols[i]);
265 }
Mike Klein3cc2d202016-10-24 11:39:43 -0400266 #endif
mtklein5b64dab2016-06-09 08:59:48 -0700267 fflush(stdout);
268
Mike Klein3cc2d202016-10-24 11:39:43 -0400269 signal(sig, previous_handler[sig]);
270 raise(sig);
mtklein5b64dab2016-06-09 08:59:48 -0700271 }
272
mtklein246ba3a2016-02-23 10:39:36 -0800273 static void setup_crash_handler() {
274 const int kSignals[] = { SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGSEGV };
275 for (int sig : kSignals) {
Mike Klein3cc2d202016-10-24 11:39:43 -0400276 previous_handler[sig] = signal(sig, crash_handler);
mtklein246ba3a2016-02-23 10:39:36 -0800277 }
278 }
279#endif
280
mtklein748ca3b2015-01-15 10:56:12 -0800281/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
mtklein114c3cd2015-01-15 10:15:02 -0800282
mtklein62bd1a62015-01-27 14:46:26 -0800283struct Gold : public SkString {
mtkleina82f5622015-02-20 12:30:19 -0800284 Gold() : SkString("") {}
mtklein3eed7dd2016-02-24 19:07:07 -0800285 Gold(const SkString& sink, const SkString& src,
286 const SkString& srcOptions, const SkString& name,
287 const SkString& md5)
mtklein62bd1a62015-01-27 14:46:26 -0800288 : SkString("") {
289 this->append(sink);
290 this->append(src);
djsollen54416de2015-04-03 07:24:48 -0700291 this->append(srcOptions);
mtklein62bd1a62015-01-27 14:46:26 -0800292 this->append(name);
293 this->append(md5);
mtklein62bd1a62015-01-27 14:46:26 -0800294 }
mtkleinc8d1dd42015-10-15 12:23:01 -0700295 struct Hash {
296 uint32_t operator()(const Gold& g) const {
297 return SkGoodHash()((const SkString&)g);
298 }
299 };
mtklein62bd1a62015-01-27 14:46:26 -0800300};
mtkleina82f5622015-02-20 12:30:19 -0800301static SkTHashSet<Gold, Gold::Hash> gGold;
mtklein62bd1a62015-01-27 14:46:26 -0800302
303static void add_gold(JsonWriter::BitmapResult r) {
djsollen54416de2015-04-03 07:24:48 -0700304 gGold.add(Gold(r.config, r.sourceType, r.sourceOptions, r.name, r.md5));
mtklein62bd1a62015-01-27 14:46:26 -0800305}
306
307static void gather_gold() {
308 if (!FLAGS_readPath.isEmpty()) {
309 SkString path(FLAGS_readPath[0]);
310 path.append("/dm.json");
311 if (!JsonWriter::ReadJson(path.c_str(), add_gold)) {
312 fail(SkStringPrintf("Couldn't read %s for golden results.", path.c_str()));
313 }
314 }
315}
316
317/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
318
mtklein935f1b12016-03-16 08:37:19 -0700319#if defined(SK_BUILD_FOR_WIN32)
320 static const char* kNewline = "\r\n";
321#else
322 static const char* kNewline = "\n";
323#endif
324
borenet09ed4802015-04-03 14:15:33 -0700325static SkTHashSet<SkString> gUninterestingHashes;
326
327static void gather_uninteresting_hashes() {
328 if (!FLAGS_uninterestingHashesFile.isEmpty()) {
bungeman38d909e2016-08-02 14:40:46 -0700329 sk_sp<SkData> data(SkData::MakeFromFileName(FLAGS_uninterestingHashesFile[0]));
mtkleincc334b32015-09-22 11:43:53 -0700330 if (!data) {
mtkleinc41fd922016-03-08 09:01:39 -0800331 info("WARNING: unable to read uninteresting hashes from %s\n",
332 FLAGS_uninterestingHashesFile[0]);
mtkleincc334b32015-09-22 11:43:53 -0700333 return;
334 }
Mike Klein45dcc0c2017-04-05 18:04:31 -0400335
336 // Copy to a string to make sure SkStrSplit has a terminating \0 to find.
337 SkString contents((const char*)data->data(), data->size());
338
borenet09ed4802015-04-03 14:15:33 -0700339 SkTArray<SkString> hashes;
Mike Klein45dcc0c2017-04-05 18:04:31 -0400340 SkStrSplit(contents.c_str(), kNewline, &hashes);
borenet09ed4802015-04-03 14:15:33 -0700341 for (const SkString& hash : hashes) {
342 gUninterestingHashes.add(hash);
343 }
mtkleinc41fd922016-03-08 09:01:39 -0800344 info("FYI: loaded %d distinct uninteresting hashes from %d lines\n",
345 gUninterestingHashes.count(), hashes.count());
borenet09ed4802015-04-03 14:15:33 -0700346 }
347}
348
349/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
350
Ben Wagner145dbcd2016-11-03 14:40:50 -0400351struct TaggedSrc : public std::unique_ptr<Src> {
mtklein3eed7dd2016-02-24 19:07:07 -0800352 SkString tag;
353 SkString options;
mtkleine0effd62015-07-29 06:37:28 -0700354};
355
Ben Wagner145dbcd2016-11-03 14:40:50 -0400356struct TaggedSink : public std::unique_ptr<Sink> {
kkinnunen3e980c32015-12-23 01:33:00 -0800357 SkString tag;
djsollen54416de2015-04-03 07:24:48 -0700358};
mtklein748ca3b2015-01-15 10:56:12 -0800359
360static const bool kMemcpyOK = true;
361
mtkleine0effd62015-07-29 06:37:28 -0700362static SkTArray<TaggedSrc, kMemcpyOK> gSrcs;
363static SkTArray<TaggedSink, kMemcpyOK> gSinks;
mtklein748ca3b2015-01-15 10:56:12 -0800364
mtklein6393c062015-04-27 08:45:01 -0700365static bool in_shard() {
366 static int N = 0;
367 return N++ % FLAGS_shards == FLAGS_shard;
368}
369
mtklein3eed7dd2016-02-24 19:07:07 -0800370static void push_src(const char* tag, ImplicitString options, Src* s) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400371 std::unique_ptr<Src> src(s);
mtklein6393c062015-04-27 08:45:01 -0700372 if (in_shard() &&
mtklein3eed7dd2016-02-24 19:07:07 -0800373 FLAGS_src.contains(tag) &&
mtklein748ca3b2015-01-15 10:56:12 -0800374 !SkCommandLineFlags::ShouldSkip(FLAGS_match, src->name().c_str())) {
mtkleine0effd62015-07-29 06:37:28 -0700375 TaggedSrc& s = gSrcs.push_back();
mtklein18300a32016-03-16 13:53:35 -0700376 s.reset(src.release());
mtklein748ca3b2015-01-15 10:56:12 -0800377 s.tag = tag;
djsollen54416de2015-04-03 07:24:48 -0700378 s.options = options;
mtklein114c3cd2015-01-15 10:15:02 -0800379 }
mtklein748ca3b2015-01-15 10:56:12 -0800380}
mtklein114c3cd2015-01-15 10:15:02 -0800381
msarett9e707a02015-09-01 14:57:57 -0700382static void push_codec_src(Path path, CodecSrc::Mode mode, CodecSrc::DstColorType dstColorType,
scroggoc5560be2016-02-03 09:42:42 -0800383 SkAlphaType dstAlphaType, float scale) {
scroggoe4499842016-02-25 11:03:47 -0800384 if (FLAGS_simpleCodec) {
scroggo19b91532016-10-24 09:03:26 -0700385 const bool simple = CodecSrc::kCodec_Mode == mode || CodecSrc::kAnimated_Mode == mode;
386 if (!simple || dstColorType != CodecSrc::kGetFromCanvas_DstColorType || scale != 1.0f) {
scroggoe4499842016-02-25 11:03:47 -0800387 // Only decode in the simple case.
388 return;
scroggo19b91532016-10-24 09:03:26 -0700389 }
scroggoe4499842016-02-25 11:03:47 -0800390 }
msarett9e707a02015-09-01 14:57:57 -0700391 SkString folder;
392 switch (mode) {
393 case CodecSrc::kCodec_Mode:
394 folder.append("codec");
395 break;
msarettbb25b532016-01-13 09:31:39 -0800396 case CodecSrc::kCodecZeroInit_Mode:
397 folder.append("codec_zero_init");
398 break;
msarett9e707a02015-09-01 14:57:57 -0700399 case CodecSrc::kScanline_Mode:
400 folder.append("scanline");
401 break;
msarett9e707a02015-09-01 14:57:57 -0700402 case CodecSrc::kStripe_Mode:
403 folder.append("stripe");
404 break;
msarett91c22b22016-02-22 12:27:46 -0800405 case CodecSrc::kCroppedScanline_Mode:
406 folder.append("crop");
407 break;
msarett9e707a02015-09-01 14:57:57 -0700408 case CodecSrc::kSubset_Mode:
msarett36c37962015-09-02 13:20:52 -0700409 folder.append("codec_subset");
msarett9e707a02015-09-01 14:57:57 -0700410 break;
scroggo19b91532016-10-24 09:03:26 -0700411 case CodecSrc::kAnimated_Mode:
412 folder.append("codec_animated");
413 break;
msarett9e707a02015-09-01 14:57:57 -0700414 }
415
416 switch (dstColorType) {
417 case CodecSrc::kGrayscale_Always_DstColorType:
418 folder.append("_kGray8");
419 break;
msarett34e0ec42016-04-22 16:27:24 -0700420 case CodecSrc::kNonNative8888_Always_DstColorType:
421 folder.append("_kNonNative");
422 break;
msarett9e707a02015-09-01 14:57:57 -0700423 default:
424 break;
425 }
426
scroggoc5560be2016-02-03 09:42:42 -0800427 switch (dstAlphaType) {
scroggoc5560be2016-02-03 09:42:42 -0800428 case kPremul_SkAlphaType:
429 folder.append("_premul");
430 break;
431 case kUnpremul_SkAlphaType:
432 folder.append("_unpremul");
433 break;
434 default:
435 break;
436 }
437
msarett9e707a02015-09-01 14:57:57 -0700438 if (1.0f != scale) {
439 folder.appendf("_%.3f", scale);
440 }
441
scroggoc5560be2016-02-03 09:42:42 -0800442 CodecSrc* src = new CodecSrc(path, mode, dstColorType, dstAlphaType, scale);
msarett9e707a02015-09-01 14:57:57 -0700443 push_src("image", folder, src);
444}
445
scroggof8dc9df2016-05-16 09:04:13 -0700446static void push_android_codec_src(Path path, CodecSrc::DstColorType dstColorType,
447 SkAlphaType dstAlphaType, int sampleSize) {
msarett3d9d7a72015-10-21 10:27:10 -0700448 SkString folder;
scroggof8dc9df2016-05-16 09:04:13 -0700449 folder.append("scaled_codec");
msarett3d9d7a72015-10-21 10:27:10 -0700450
451 switch (dstColorType) {
452 case CodecSrc::kGrayscale_Always_DstColorType:
453 folder.append("_kGray8");
454 break;
msarett34e0ec42016-04-22 16:27:24 -0700455 case CodecSrc::kNonNative8888_Always_DstColorType:
456 folder.append("_kNonNative");
457 break;
msarett3d9d7a72015-10-21 10:27:10 -0700458 default:
459 break;
460 }
461
scroggoc5560be2016-02-03 09:42:42 -0800462 switch (dstAlphaType) {
scroggoc5560be2016-02-03 09:42:42 -0800463 case kPremul_SkAlphaType:
464 folder.append("_premul");
465 break;
msarett9e9444c2016-02-03 12:39:10 -0800466 case kUnpremul_SkAlphaType:
467 folder.append("_unpremul");
468 break;
scroggoc5560be2016-02-03 09:42:42 -0800469 default:
470 break;
471 }
472
msarett3d9d7a72015-10-21 10:27:10 -0700473 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800474 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700475 }
476
scroggof8dc9df2016-05-16 09:04:13 -0700477 AndroidCodecSrc* src = new AndroidCodecSrc(path, dstColorType, dstAlphaType, sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700478 push_src("image", folder, src);
479}
480
msarett18976312016-03-09 14:20:58 -0800481static void push_image_gen_src(Path path, ImageGenSrc::Mode mode, SkAlphaType alphaType, bool isGpu)
482{
483 SkString folder;
484 switch (mode) {
485 case ImageGenSrc::kCodec_Mode:
486 folder.append("gen_codec");
487 break;
488 case ImageGenSrc::kPlatform_Mode:
489 folder.append("gen_platform");
490 break;
491 }
492
493 if (isGpu) {
494 folder.append("_gpu");
495 } else {
496 switch (alphaType) {
497 case kOpaque_SkAlphaType:
498 folder.append("_opaque");
499 break;
500 case kPremul_SkAlphaType:
501 folder.append("_premul");
502 break;
503 case kUnpremul_SkAlphaType:
504 folder.append("_unpremul");
505 break;
506 default:
507 break;
508 }
509 }
510
511 ImageGenSrc* src = new ImageGenSrc(path, mode, alphaType, isGpu);
512 push_src("image", folder, src);
513}
514
msarett438b2ad2015-04-09 12:43:10 -0700515static void push_codec_srcs(Path path) {
bungeman38d909e2016-08-02 14:40:46 -0700516 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
msarett438b2ad2015-04-09 12:43:10 -0700517 if (!encoded) {
mtkleinc41fd922016-03-08 09:01:39 -0800518 info("Couldn't read %s.", path.c_str());
msarett438b2ad2015-04-09 12:43:10 -0700519 return;
520 }
Ben Wagner145dbcd2016-11-03 14:40:50 -0400521 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(encoded));
halcanary96fcdcc2015-08-27 07:41:13 -0700522 if (nullptr == codec.get()) {
mtkleinc41fd922016-03-08 09:01:39 -0800523 info("Couldn't create codec for %s.", path.c_str());
msarett438b2ad2015-04-09 12:43:10 -0700524 return;
525 }
526
scroggo9366aff2016-05-20 08:38:54 -0700527 // native scaling is only supported by WEBP and JPEG
528 bool supportsNativeScaling = false;
msarett438b2ad2015-04-09 12:43:10 -0700529
msarett91c22b22016-02-22 12:27:46 -0800530 SkTArray<CodecSrc::Mode> nativeModes;
531 nativeModes.push_back(CodecSrc::kCodec_Mode);
532 nativeModes.push_back(CodecSrc::kCodecZeroInit_Mode);
msarett91c22b22016-02-22 12:27:46 -0800533 switch (codec->getEncodedFormat()) {
Hal Canarydb683012016-11-23 08:55:18 -0700534 case SkEncodedImageFormat::kJPEG:
msarett91c22b22016-02-22 12:27:46 -0800535 nativeModes.push_back(CodecSrc::kScanline_Mode);
536 nativeModes.push_back(CodecSrc::kStripe_Mode);
537 nativeModes.push_back(CodecSrc::kCroppedScanline_Mode);
scroggo9366aff2016-05-20 08:38:54 -0700538 supportsNativeScaling = true;
msarett91c22b22016-02-22 12:27:46 -0800539 break;
Hal Canarydb683012016-11-23 08:55:18 -0700540 case SkEncodedImageFormat::kWEBP:
msarett91c22b22016-02-22 12:27:46 -0800541 nativeModes.push_back(CodecSrc::kSubset_Mode);
scroggo9366aff2016-05-20 08:38:54 -0700542 supportsNativeScaling = true;
msarett91c22b22016-02-22 12:27:46 -0800543 break;
Hal Canarydb683012016-11-23 08:55:18 -0700544 case SkEncodedImageFormat::kDNG:
msarettb65e6042016-02-23 05:37:25 -0800545 break;
msarett91c22b22016-02-22 12:27:46 -0800546 default:
547 nativeModes.push_back(CodecSrc::kScanline_Mode);
msarett91c22b22016-02-22 12:27:46 -0800548 break;
549 }
msarett9e707a02015-09-01 14:57:57 -0700550
msarett91c22b22016-02-22 12:27:46 -0800551 SkTArray<CodecSrc::DstColorType> colorTypes;
552 colorTypes.push_back(CodecSrc::kGetFromCanvas_DstColorType);
msarett34e0ec42016-04-22 16:27:24 -0700553 colorTypes.push_back(CodecSrc::kNonNative8888_Always_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700554 switch (codec->getInfo().colorType()) {
555 case kGray_8_SkColorType:
msarett91c22b22016-02-22 12:27:46 -0800556 colorTypes.push_back(CodecSrc::kGrayscale_Always_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700557 break;
558 default:
msarett36c37962015-09-02 13:20:52 -0700559 break;
560 }
msarett9e707a02015-09-01 14:57:57 -0700561
scroggoc5560be2016-02-03 09:42:42 -0800562 SkTArray<SkAlphaType> alphaModes;
563 alphaModes.push_back(kPremul_SkAlphaType);
scroggof2c96a22016-05-20 11:08:27 -0700564 if (codec->getInfo().alphaType() != kOpaque_SkAlphaType) {
565 alphaModes.push_back(kUnpremul_SkAlphaType);
scroggoc5560be2016-02-03 09:42:42 -0800566 }
msarettb714fb02016-01-22 14:46:42 -0800567
568 for (CodecSrc::Mode mode : nativeModes) {
scroggo9366aff2016-05-20 08:38:54 -0700569 for (CodecSrc::DstColorType colorType : colorTypes) {
570 for (SkAlphaType alphaType : alphaModes) {
scroggof2c96a22016-05-20 11:08:27 -0700571 // Only test kCroppedScanline_Mode when the alpha type is premul. The test is
scroggo9366aff2016-05-20 08:38:54 -0700572 // slow and won't be interestingly different with different alpha types.
573 if (CodecSrc::kCroppedScanline_Mode == mode &&
scroggof2c96a22016-05-20 11:08:27 -0700574 kPremul_SkAlphaType != alphaType) {
scroggo9366aff2016-05-20 08:38:54 -0700575 continue;
576 }
msarett91c22b22016-02-22 12:27:46 -0800577
scroggo9366aff2016-05-20 08:38:54 -0700578 push_codec_src(path, mode, colorType, alphaType, 1.0f);
msarettf3dc1882016-04-26 13:06:38 -0700579
scroggo9366aff2016-05-20 08:38:54 -0700580 // Skip kNonNative on different native scales. It won't be interestingly
581 // different.
582 if (supportsNativeScaling &&
583 CodecSrc::kNonNative8888_Always_DstColorType == colorType) {
584 // Native Scales
585 // SkJpegCodec natively supports scaling to the following:
586 for (auto scale : { 0.125f, 0.25f, 0.375f, 0.5f, 0.625f, 0.750f, 0.875f }) {
587 push_codec_src(path, mode, colorType, alphaType, scale);
588 }
scroggoc5560be2016-02-03 09:42:42 -0800589 }
msarett9e707a02015-09-01 14:57:57 -0700590 }
591 }
msarett0a242972015-06-11 14:27:27 -0700592 }
msarett36c37962015-09-02 13:20:52 -0700593
scroggo19b91532016-10-24 09:03:26 -0700594 {
595 std::vector<SkCodec::FrameInfo> frameInfos = codec->getFrameInfo();
596 if (frameInfos.size() > 1) {
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400597 for (auto dstCT : { CodecSrc::kNonNative8888_Always_DstColorType,
598 CodecSrc::kGetFromCanvas_DstColorType }) {
599 for (auto at : { kUnpremul_SkAlphaType, kPremul_SkAlphaType }) {
600 push_codec_src(path, CodecSrc::kAnimated_Mode, dstCT, at, 1.0f);
601 }
602 }
scroggo19b91532016-10-24 09:03:26 -0700603 }
604
605 }
606
scroggoe4499842016-02-25 11:03:47 -0800607 if (FLAGS_simpleCodec) {
608 return;
609 }
610
msarett3d9d7a72015-10-21 10:27:10 -0700611 const int sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
msarett36c37962015-09-02 13:20:52 -0700612
msarett3d9d7a72015-10-21 10:27:10 -0700613 for (int sampleSize : sampleSizes) {
msarett91c22b22016-02-22 12:27:46 -0800614 for (CodecSrc::DstColorType colorType : colorTypes) {
scroggoc5560be2016-02-03 09:42:42 -0800615 for (SkAlphaType alphaType : alphaModes) {
msarettf3dc1882016-04-26 13:06:38 -0700616 // We can exercise all of the kNonNative support code in the swizzler with just a
617 // few sample sizes. Skip the rest.
618 if (CodecSrc::kNonNative8888_Always_DstColorType == colorType && sampleSize > 3) {
619 continue;
620 }
621
scroggof8dc9df2016-05-16 09:04:13 -0700622 push_android_codec_src(path, colorType, alphaType, sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700623 }
msarett36c37962015-09-02 13:20:52 -0700624 }
625 }
msarett18976312016-03-09 14:20:58 -0800626
627 static const char* const rawExts[] = {
628 "arw", "cr2", "dng", "nef", "nrw", "orf", "raf", "rw2", "pef", "srw",
629 "ARW", "CR2", "DNG", "NEF", "NRW", "ORF", "RAF", "RW2", "PEF", "SRW",
630 };
631
632 // There is not currently a reason to test RAW images on image generator.
633 // If we want to enable these tests, we will need to fix skbug.com/5079.
634 for (const char* ext : rawExts) {
635 if (path.endsWith(ext)) {
636 return;
637 }
638 }
639
640 // Push image generator GPU test.
brianosmanc9ced392016-03-24 07:27:43 -0700641 push_image_gen_src(path, ImageGenSrc::kCodec_Mode, codec->getInfo().alphaType(), true);
msarett18976312016-03-09 14:20:58 -0800642
643 // Push image generator CPU tests.
644 for (SkAlphaType alphaType : alphaModes) {
645 push_image_gen_src(path, ImageGenSrc::kCodec_Mode, alphaType, false);
646
647#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
Hal Canarydb683012016-11-23 08:55:18 -0700648 if (SkEncodedImageFormat::kWEBP != codec->getEncodedFormat() &&
649 SkEncodedImageFormat::kWBMP != codec->getEncodedFormat() &&
msarett18976312016-03-09 14:20:58 -0800650 kUnpremul_SkAlphaType != alphaType)
651 {
652 push_image_gen_src(path, ImageGenSrc::kPlatform_Mode, alphaType, false);
653 }
msarettfc0b6d12016-03-17 13:50:17 -0700654#elif defined(SK_BUILD_FOR_WIN)
Hal Canarydb683012016-11-23 08:55:18 -0700655 if (SkEncodedImageFormat::kWEBP != codec->getEncodedFormat() &&
656 SkEncodedImageFormat::kWBMP != codec->getEncodedFormat())
msarettfc0b6d12016-03-17 13:50:17 -0700657 {
658 push_image_gen_src(path, ImageGenSrc::kPlatform_Mode, alphaType, false);
659 }
msarett18976312016-03-09 14:20:58 -0800660#endif
661 }
msarett438b2ad2015-04-09 12:43:10 -0700662}
663
msarettd1227a72016-05-18 06:23:57 -0700664static void push_brd_src(Path path, CodecSrc::DstColorType dstColorType, BRDSrc::Mode mode,
665 uint32_t sampleSize) {
666 SkString folder("brd_android_codec");
msaretta5783ae2015-09-08 15:35:32 -0700667 switch (mode) {
668 case BRDSrc::kFullImage_Mode:
669 break;
670 case BRDSrc::kDivisor_Mode:
671 folder.append("_divisor");
672 break;
673 default:
674 SkASSERT(false);
675 return;
676 }
677
678 switch (dstColorType) {
679 case CodecSrc::kGetFromCanvas_DstColorType:
680 break;
msaretta5783ae2015-09-08 15:35:32 -0700681 case CodecSrc::kGrayscale_Always_DstColorType:
682 folder.append("_kGray");
683 break;
684 default:
685 SkASSERT(false);
686 return;
687 }
688
689 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800690 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700691 }
692
msarettd1227a72016-05-18 06:23:57 -0700693 BRDSrc* src = new BRDSrc(path, mode, dstColorType, sampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700694 push_src("image", folder, src);
695}
696
697static void push_brd_srcs(Path path) {
Leon Scroggins IIIbace7a92017-07-05 11:39:27 -0400698 // Only run grayscale to one sampleSize and Mode. Though interesting
699 // to test grayscale, it should not reveal anything across various
scroggo24a9bd72016-05-20 06:02:42 -0700700 // sampleSizes and Modes
Leon Scroggins IIIbace7a92017-07-05 11:39:27 -0400701 // Arbitrarily choose Mode and sampleSize.
702 push_brd_src(path, CodecSrc::kGrayscale_Always_DstColorType, BRDSrc::kFullImage_Mode, 2);
scroggo24a9bd72016-05-20 06:02:42 -0700703
704
scroggo501b7342015-11-03 07:55:11 -0800705 // Test on a variety of sampleSizes, making sure to include:
706 // - 2, 4, and 8, which are natively supported by jpeg
707 // - multiples of 2 which are not divisible by 4 (analogous for 4)
708 // - larger powers of two, since BRD clients generally use powers of 2
709 // We will only produce output for the larger sizes on large images.
710 const uint32_t sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 24, 32, 64 };
msarett6efbe052015-09-11 09:01:16 -0700711
msaretta5783ae2015-09-08 15:35:32 -0700712 const BRDSrc::Mode modes[] = {
msarett26ad17b2015-10-22 07:29:19 -0700713 BRDSrc::kFullImage_Mode,
714 BRDSrc::kDivisor_Mode,
msaretta5783ae2015-09-08 15:35:32 -0700715 };
716
msarettd1227a72016-05-18 06:23:57 -0700717 for (uint32_t sampleSize : sampleSizes) {
scroggo24a9bd72016-05-20 06:02:42 -0700718 for (BRDSrc::Mode mode : modes) {
719 push_brd_src(path, CodecSrc::kGetFromCanvas_DstColorType, mode, sampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700720 }
721 }
722}
723
724static bool brd_supported(const char* ext) {
msarett8c8f22a2015-04-01 06:58:48 -0700725 static const char* const exts[] = {
msaretta5783ae2015-09-08 15:35:32 -0700726 "jpg", "jpeg", "png", "webp",
727 "JPG", "JPEG", "PNG", "WEBP",
msarett8c8f22a2015-04-01 06:58:48 -0700728 };
729
730 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
731 if (0 == strcmp(exts[i], ext)) {
732 return true;
733 }
734 }
735 return false;
scroggo9b77ddd2015-03-19 06:03:39 -0700736}
737
fmalitaa2b9fdf2016-08-03 19:53:36 -0700738template <typename T>
739void gather_file_srcs(const SkCommandLineFlags::StringArray& flags, const char* ext) {
740 for (int i = 0; i < flags.count(); i++) {
741 const char* path = flags[i];
742 if (sk_isdir(path)) {
743 SkOSFile::Iter it(path, ext);
744 for (SkString file; it.next(&file); ) {
745 push_src(ext, "", new T(SkOSPath::Join(path, file.c_str())));
746 }
747 } else {
748 push_src(ext, "", new T(path));
749 }
750 }
751}
752
scroggo86737142016-02-03 12:19:11 -0800753static bool gather_srcs() {
mtklein748ca3b2015-01-15 10:56:12 -0800754 for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) {
djsollen54416de2015-04-03 07:24:48 -0700755 push_src("gm", "", new GMSrc(r->factory()));
mtklein748ca3b2015-01-15 10:56:12 -0800756 }
scroggo86737142016-02-03 12:19:11 -0800757
fmalitaa2b9fdf2016-08-03 19:53:36 -0700758 gather_file_srcs<SKPSrc>(FLAGS_skps, "skp");
759 gather_file_srcs<MSKPSrc>(FLAGS_mskps, "mskp");
760#if defined(SK_XML)
761 gather_file_srcs<SVGSrc>(FLAGS_svgs, "svg");
762#endif
halcanary45420a92016-06-02 12:41:14 -0700763
scroggo86737142016-02-03 12:19:11 -0800764 SkTArray<SkString> images;
msarett69deca82016-04-29 09:38:40 -0700765 if (!CollectImages(FLAGS_images, &images)) {
scroggo86737142016-02-03 12:19:11 -0800766 return false;
767 }
768
769 for (auto image : images) {
770 push_codec_srcs(image);
scroggoe4499842016-02-25 11:03:47 -0800771 if (FLAGS_simpleCodec) {
772 continue;
773 }
774
mtklein21eaf3b2016-02-08 12:39:59 -0800775 const char* ext = strrchr(image.c_str(), '.');
776 if (ext && brd_supported(ext+1)) {
scroggo86737142016-02-03 12:19:11 -0800777 push_brd_srcs(image);
mtklein709d2c32015-01-15 08:30:25 -0800778 }
mtklein709d2c32015-01-15 08:30:25 -0800779 }
scroggo86737142016-02-03 12:19:11 -0800780
msarett69deca82016-04-29 09:38:40 -0700781 SkTArray<SkString> colorImages;
782 if (!CollectImages(FLAGS_colorImages, &colorImages)) {
783 return false;
784 }
785
786 for (auto colorImage : colorImages) {
msarett9ce3a542016-07-15 13:54:38 -0700787 ColorCodecSrc* src = new ColorCodecSrc(colorImage, ColorCodecSrc::kBaseline_Mode,
788 kN32_SkColorType);
msarettb8d1aac2016-08-04 12:22:06 -0700789 push_src("colorImage", "color_codec_baseline", src);
msarett888dc162016-05-23 10:21:17 -0700790
msarett9ce3a542016-07-15 13:54:38 -0700791 src = new ColorCodecSrc(colorImage, ColorCodecSrc::kDst_HPZR30w_Mode, kN32_SkColorType);
msarettb8d1aac2016-08-04 12:22:06 -0700792 push_src("colorImage", "color_codec_HPZR30w", src);
msarett9ce3a542016-07-15 13:54:38 -0700793 // TODO (msarett):
794 // 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 -0700795
msarett9ce3a542016-07-15 13:54:38 -0700796 src = new ColorCodecSrc(colorImage, ColorCodecSrc::kDst_sRGB_Mode, kN32_SkColorType);
msarettb8d1aac2016-08-04 12:22:06 -0700797 push_src("colorImage", "color_codec_sRGB_kN32", src);
msarett9ce3a542016-07-15 13:54:38 -0700798 src = new ColorCodecSrc(colorImage, ColorCodecSrc::kDst_sRGB_Mode, kRGBA_F16_SkColorType);
msarettb8d1aac2016-08-04 12:22:06 -0700799 push_src("colorImage", "color_codec_sRGB_kF16", src);
msarett69deca82016-04-29 09:38:40 -0700800 }
801
scroggo86737142016-02-03 12:19:11 -0800802 return true;
mtklein709d2c32015-01-15 08:30:25 -0800803}
804
kkinnunen3e980c32015-12-23 01:33:00 -0800805static void push_sink(const SkCommandLineConfig& config, Sink* s) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400806 std::unique_ptr<Sink> sink(s);
kkinnunen3e980c32015-12-23 01:33:00 -0800807
mtkleine0effd62015-07-29 06:37:28 -0700808 // Try a simple Src as a canary. If it fails, skip this sink.
mtklein748ca3b2015-01-15 10:56:12 -0800809 struct : public Src {
mtkleine0effd62015-07-29 06:37:28 -0700810 Error draw(SkCanvas* c) const override {
811 c->drawRect(SkRect::MakeWH(1,1), SkPaint());
812 return "";
813 }
mtklein36352bf2015-03-25 18:17:31 -0700814 SkISize size() const override { return SkISize::Make(16, 16); }
mtkleine0effd62015-07-29 06:37:28 -0700815 Name name() const override { return "justOneRect"; }
816 } justOneRect;
mtklein114c3cd2015-01-15 10:15:02 -0800817
mtklein748ca3b2015-01-15 10:56:12 -0800818 SkBitmap bitmap;
819 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800820 SkString log;
mtkleine0effd62015-07-29 06:37:28 -0700821 Error err = sink->draw(justOneRect, &bitmap, &stream, &log);
mtklein4089ef72015-03-05 08:40:28 -0800822 if (err.isFatal()) {
mtkleinc41fd922016-03-08 09:01:39 -0800823 info("Could not run %s: %s\n", config.getTag().c_str(), err.c_str());
mtklein05641a52015-04-21 10:49:13 -0700824 exit(1);
mtklein114c3cd2015-01-15 10:15:02 -0800825 }
826
mtkleine0effd62015-07-29 06:37:28 -0700827 TaggedSink& ts = gSinks.push_back();
mtklein18300a32016-03-16 13:53:35 -0700828 ts.reset(sink.release());
kkinnunen3e980c32015-12-23 01:33:00 -0800829 ts.tag = config.getTag();
mtklein748ca3b2015-01-15 10:56:12 -0800830}
831
832static bool gpu_supported() {
833#if SK_SUPPORT_GPU
834 return FLAGS_gpu;
835#else
836 return false;
837#endif
838}
kkinnunen9ebc3f02015-12-21 23:48:13 -0800839
csmartdalton008b9d82017-02-22 12:00:42 -0700840static Sink* create_sink(const GrContextOptions& grCtxOptions, const SkCommandLineConfig* config) {
kkinnunen3e980c32015-12-23 01:33:00 -0800841#if SK_SUPPORT_GPU
842 if (gpu_supported()) {
843 if (const SkCommandLineConfigGpu* gpuConfig = config->asConfigGpu()) {
bsalomon85b4b532016-04-05 11:06:27 -0700844 GrContextFactory::ContextType contextType = gpuConfig->getContextType();
csmartdaltone812d492017-02-21 12:36:05 -0700845 GrContextFactory::ContextOverrides contextOverrides = gpuConfig->getContextOverrides();
csmartdalton008b9d82017-02-22 12:00:42 -0700846 GrContextFactory testFactory(grCtxOptions);
csmartdaltone812d492017-02-21 12:36:05 -0700847 if (!testFactory.get(contextType, contextOverrides)) {
mtkleinc41fd922016-03-08 09:01:39 -0800848 info("WARNING: can not create GPU context for config '%s'. "
849 "GM tests will be skipped.\n", gpuConfig->getTag().c_str());
kkinnunen3e980c32015-12-23 01:33:00 -0800850 return nullptr;
851 }
csmartdaltone812d492017-02-21 12:36:05 -0700852 return new GPUSink(contextType, contextOverrides, gpuConfig->getSamples(),
brianosmand93c1202016-03-10 07:49:08 -0800853 gpuConfig->getUseDIText(), gpuConfig->getColorType(),
brianosmanb109b8c2016-06-16 13:03:24 -0700854 sk_ref_sp(gpuConfig->getColorSpace()), FLAGS_gpu_threading);
kkinnunen3e980c32015-12-23 01:33:00 -0800855 }
856 }
857#endif
858
859#define SINK(t, sink, ...) if (config->getBackend().equals(t)) { return new sink(__VA_ARGS__); }
860
mtklein748ca3b2015-01-15 10:56:12 -0800861 if (FLAGS_cpu) {
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500862 auto srgbColorSpace = SkColorSpace::MakeSRGB();
863 auto srgbLinearColorSpace = SkColorSpace::MakeSRGBLinear();
brianosmanb109b8c2016-06-16 13:03:24 -0700864
Matt Sarettd2228302017-03-02 08:53:46 -0500865 SINK("565", RasterSink, kRGB_565_SkColorType);
866 SINK("8888", RasterSink, kN32_SkColorType);
867 SINK("srgb", RasterSink, kN32_SkColorType, srgbColorSpace);
Matt Sarettd2228302017-03-02 08:53:46 -0500868 SINK("f16", RasterSink, kRGBA_F16_SkColorType, srgbLinearColorSpace);
869 SINK("pdf", PDFSink);
870 SINK("skp", SKPSink);
871 SINK("pipe", PipeSink);
872 SINK("svg", SVGSink);
873 SINK("null", NullSink);
874 SINK("xps", XPSSink);
875 SINK("pdfa", PDFSink, true);
Hal Canary85c7fe82016-10-25 10:33:27 -0400876 SINK("jsdebug", DebugSink);
mtklein748ca3b2015-01-15 10:56:12 -0800877 }
878#undef SINK
halcanary96fcdcc2015-08-27 07:41:13 -0700879 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800880}
881
Mike Klein841101d2017-03-10 09:55:51 -0500882static sk_sp<SkColorSpace> adobe_rgb() {
883 return SkColorSpace::MakeRGB(SkColorSpace::kSRGB_RenderTargetGamma,
884 SkColorSpace::kAdobeRGB_Gamut);
885}
886
Mike Klein919cc452017-03-18 15:36:52 +0000887static sk_sp<SkColorSpace> rgb_to_gbr() {
888 float gbr[9];
889 gbr[0] = gSRGB_toXYZD50[1];
890 gbr[1] = gSRGB_toXYZD50[2];
891 gbr[2] = gSRGB_toXYZD50[0];
892 gbr[3] = gSRGB_toXYZD50[4];
893 gbr[4] = gSRGB_toXYZD50[5];
894 gbr[5] = gSRGB_toXYZD50[3];
895 gbr[6] = gSRGB_toXYZD50[7];
896 gbr[7] = gSRGB_toXYZD50[8];
897 gbr[8] = gSRGB_toXYZD50[6];
898 SkMatrix44 toXYZD50(SkMatrix44::kUninitialized_Constructor);
899 toXYZD50.set3x3RowMajorf(gbr);
900 return SkColorSpace::MakeRGB(SkColorSpace::kSRGB_RenderTargetGamma, toXYZD50);
901}
902
kkinnunen3e980c32015-12-23 01:33:00 -0800903static Sink* create_via(const SkString& tag, Sink* wrapped) {
904#define VIA(t, via, ...) if (tag.equals(t)) { return new via(__VA_ARGS__); }
Mike Klein919cc452017-03-18 15:36:52 +0000905 VIA("adobe", ViaCSXform, wrapped, adobe_rgb(), false);
906 VIA("gbr", ViaCSXform, wrapped, rgb_to_gbr(), true);
mtklein9c5052f2016-08-06 12:51:51 -0700907 VIA("lite", ViaLite, wrapped);
reed54dc4872016-09-13 08:09:45 -0700908 VIA("pipe", ViaPipe, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700909 VIA("twice", ViaTwice, wrapped);
Mike Reedbae888e2017-02-18 16:50:45 -0500910#ifdef TEST_VIA_SVG
Mike Reedf67c4592017-02-17 17:06:11 -0500911 VIA("svg", ViaSVG, wrapped);
Mike Reedbae888e2017-02-18 16:50:45 -0500912#endif
halcanary96fcdcc2015-08-27 07:41:13 -0700913 VIA("serialize", ViaSerialization, wrapped);
mtklein4a34ecb2016-01-08 10:19:35 -0800914 VIA("pic", ViaPicture, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700915 VIA("2ndpic", ViaSecondPicture, wrapped);
mtkleind31c13d2015-05-05 12:59:56 -0700916 VIA("sp", ViaSingletonPictures, wrapped);
reedbabc3de2016-07-08 08:43:27 -0700917 VIA("defer", ViaDefer, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700918 VIA("tiles", ViaTiles, 256, 256, nullptr, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800919 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800920
mtkleind603b222015-02-17 11:13:33 -0800921 if (FLAGS_matrix.count() == 4) {
mtklein748ca3b2015-01-15 10:56:12 -0800922 SkMatrix m;
mtkleind603b222015-02-17 11:13:33 -0800923 m.reset();
924 m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
925 m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
926 m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
927 m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
928 VIA("matrix", ViaMatrix, m, wrapped);
929 VIA("upright", ViaUpright, m, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800930 }
tomhudson64de1e12015-03-05 08:01:07 -0800931
mtklein748ca3b2015-01-15 10:56:12 -0800932#undef VIA
halcanary96fcdcc2015-08-27 07:41:13 -0700933 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800934}
935
csmartdalton008b9d82017-02-22 12:00:42 -0700936static bool gather_sinks(const GrContextOptions& grCtxOptions) {
kkinnunen3e980c32015-12-23 01:33:00 -0800937 SkCommandLineConfigArray configs;
938 ParseConfigs(FLAGS_config, &configs);
939 for (int i = 0; i < configs.count(); i++) {
940 const SkCommandLineConfig& config = *configs[i];
csmartdalton008b9d82017-02-22 12:00:42 -0700941 Sink* sink = create_sink(grCtxOptions, &config);
kkinnunen3e980c32015-12-23 01:33:00 -0800942 if (sink == nullptr) {
mtkleinc41fd922016-03-08 09:01:39 -0800943 info("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
944 config.getTag().c_str());
kkinnunen3e980c32015-12-23 01:33:00 -0800945 continue;
946 }
mtklein748ca3b2015-01-15 10:56:12 -0800947
kkinnunen3e980c32015-12-23 01:33:00 -0800948 const SkTArray<SkString>& parts = config.getViaParts();
949 for (int j = parts.count(); j-- > 0;) {
950 const SkString& part = parts[j];
951 Sink* next = create_via(part, sink);
halcanary96fcdcc2015-08-27 07:41:13 -0700952 if (next == nullptr) {
mtkleinc41fd922016-03-08 09:01:39 -0800953 info("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
954 part.c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800955 delete sink;
halcanary96fcdcc2015-08-27 07:41:13 -0700956 sink = nullptr;
mtklein748ca3b2015-01-15 10:56:12 -0800957 break;
958 }
959 sink = next;
960 }
961 if (sink) {
962 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800963 }
964 }
brianosman05c987c2016-05-09 13:42:16 -0700965
966 // If no configs were requested (just running tests, perhaps?), then we're okay.
967 // Otherwise, make sure that at least one sink was constructed correctly. This catches
968 // the case of bots without a GPU being assigned GPU configs.
969 return (configs.count() == 0) || (gSinks.count() > 0);
mtklein114c3cd2015-01-15 10:15:02 -0800970}
mtklein709d2c32015-01-15 08:30:25 -0800971
mtkleina5114d72015-08-24 13:27:01 -0700972static bool dump_png(SkBitmap bitmap, const char* path, const char* md5) {
Mike Klein13b6afd2017-06-12 12:45:41 -0400973 SkPixmap pm;
974 if (!bitmap.peekPixels(&pm)) {
975 return false; // Ought to never happen... we're already read-back at this point.
mtkleina5114d72015-08-24 13:27:01 -0700976 }
Mike Klein13b6afd2017-06-12 12:45:41 -0400977 SkFILEWStream dst{path};
mtkleina5114d72015-08-24 13:27:01 -0700978
mtkleind69ece62015-09-10 10:37:44 -0700979 SkString description;
980 description.append("Key: ");
981 for (int i = 0; i < FLAGS_key.count(); i++) {
982 description.appendf("%s ", FLAGS_key[i]);
983 }
984 description.append("Properties: ");
985 for (int i = 0; i < FLAGS_properties.count(); i++) {
986 description.appendf("%s ", FLAGS_properties[i]);
987 }
988 description.appendf("MD5: %s", md5);
989
Mike Klein13b6afd2017-06-12 12:45:41 -0400990 const char* comments[] = {
991 "Author", "DM dump_png()",
992 "Description", description.c_str(),
993 };
994 size_t lengths[] = {
995 strlen(comments[0])+1, strlen(comments[1])+1,
996 strlen(comments[2])+1, strlen(comments[3])+1,
997 };
mtkleina5114d72015-08-24 13:27:01 -0700998
Mike Klein13b6afd2017-06-12 12:45:41 -0400999 SkPngEncoder::Options options;
1000 options.fComments = SkDataTable::MakeCopyArrays((const void**)comments, lengths, 4);
1001 options.fFilterFlags = SkPngEncoder::FilterFlag::kNone;
1002 options.fZLibLevel = 1;
1003 options.fUnpremulBehavior = pm.colorSpace() ? SkTransferFunctionBehavior::kRespect
1004 : SkTransferFunctionBehavior::kIgnore;
1005 return SkPngEncoder::Encode(&dst, pm, options);
mtkleina5114d72015-08-24 13:27:01 -07001006}
1007
mtkleina2ef6422015-01-15 13:44:22 -08001008static bool match(const char* needle, const char* haystack) {
halcanary96fcdcc2015-08-27 07:41:13 -07001009 return 0 == strcmp("_", needle) || nullptr != strstr(haystack, needle);
mtkleina2ef6422015-01-15 13:44:22 -08001010}
1011
mtklein3eed7dd2016-02-24 19:07:07 -08001012static bool is_blacklisted(const char* sink, const char* src,
1013 const char* srcOptions, const char* name) {
mtklein0d243ff2015-04-03 07:51:00 -07001014 for (int i = 0; i < FLAGS_blacklist.count() - 3; i += 4) {
mtkleina2ef6422015-01-15 13:44:22 -08001015 if (match(FLAGS_blacklist[i+0], sink) &&
djsollen54416de2015-04-03 07:24:48 -07001016 match(FLAGS_blacklist[i+1], src) &&
1017 match(FLAGS_blacklist[i+2], srcOptions) &&
1018 match(FLAGS_blacklist[i+3], name)) {
mtklein3eed7dd2016-02-24 19:07:07 -08001019 return true;
mtkleina2ef6422015-01-15 13:44:22 -08001020 }
1021 }
mtklein3eed7dd2016-02-24 19:07:07 -08001022 return false;
mtkleina2ef6422015-01-15 13:44:22 -08001023}
1024
mtkleincd50bca2016-01-05 06:20:20 -08001025// Even when a Task Sink reports to be non-threadsafe (e.g. GPU), we know things like
1026// .png encoding are definitely thread safe. This lets us offload that work to CPU threads.
1027static SkTaskGroup gDefinitelyThreadSafeWork;
1028
mtklein748ca3b2015-01-15 10:56:12 -08001029// The finest-grained unit of work we can run: draw a single Src into a single Sink,
1030// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
1031struct Task {
mtkleine0effd62015-07-29 06:37:28 -07001032 Task(const TaggedSrc& src, const TaggedSink& sink) : src(src), sink(sink) {}
1033 const TaggedSrc& src;
1034 const TaggedSink& sink;
mtklein748ca3b2015-01-15 10:56:12 -08001035
mtklein21eaf3b2016-02-08 12:39:59 -08001036 static void Run(const Task& task) {
1037 SkString name = task.src->name();
mtkleine0effd62015-07-29 06:37:28 -07001038
mtkleinb9eb4ac2015-02-02 18:26:03 -08001039 SkString log;
mtklein3eed7dd2016-02-24 19:07:07 -08001040 if (!FLAGS_dryRun) {
mtklein748ca3b2015-01-15 10:56:12 -08001041 SkBitmap bitmap;
1042 SkDynamicMemoryWStream stream;
mtklein3eed7dd2016-02-24 19:07:07 -08001043 start(task.sink.tag.c_str(), task.src.tag.c_str(),
1044 task.src.options.c_str(), name.c_str());
mtklein21eaf3b2016-02-08 12:39:59 -08001045 Error err = task.sink->draw(*task.src, &bitmap, &stream, &log);
mtkleinb3b13b72016-03-07 13:20:52 -08001046 if (!log.isEmpty()) {
mtkleinc41fd922016-03-08 09:01:39 -08001047 info("%s %s %s %s:\n%s\n", task.sink.tag.c_str()
1048 , task.src.tag.c_str()
1049 , task.src.options.c_str()
1050 , name.c_str()
1051 , log.c_str());
mtkleinb3b13b72016-03-07 13:20:52 -08001052 }
mtklein748ca3b2015-01-15 10:56:12 -08001053 if (!err.isEmpty()) {
mtklein4089ef72015-03-05 08:40:28 -08001054 if (err.isFatal()) {
djsollen54416de2015-04-03 07:24:48 -07001055 fail(SkStringPrintf("%s %s %s %s: %s",
mtklein21eaf3b2016-02-08 12:39:59 -08001056 task.sink.tag.c_str(),
1057 task.src.tag.c_str(),
1058 task.src.options.c_str(),
mtklein4089ef72015-03-05 08:40:28 -08001059 name.c_str(),
1060 err.c_str()));
mtkleinb37cb412015-03-18 05:27:14 -07001061 } else {
mtklein3eed7dd2016-02-24 19:07:07 -08001062 done(task.sink.tag.c_str(), task.src.tag.c_str(),
1063 task.src.options.c_str(), name.c_str());
mtkleinba6ada72016-01-21 09:39:35 -08001064 return;
mtklein4089ef72015-03-05 08:40:28 -08001065 }
mtklein748ca3b2015-01-15 10:56:12 -08001066 }
mtklein62bd1a62015-01-27 14:46:26 -08001067
mtkleincd50bca2016-01-05 06:20:20 -08001068 // We're likely switching threads here, so we must capture by value, [=] or [foo,bar].
Hal Canary0b833192017-03-14 14:35:02 -04001069 SkStreamAsset* data = stream.detachAsStream().release();
mtkleincd50bca2016-01-05 06:20:20 -08001070 gDefinitelyThreadSafeWork.add([task,name,bitmap,data]{
Ben Wagner145dbcd2016-11-03 14:40:50 -04001071 std::unique_ptr<SkStreamAsset> ownedData(data);
mtkleincd50bca2016-01-05 06:20:20 -08001072
mtkleincd50bca2016-01-05 06:20:20 -08001073 SkString md5;
1074 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
1075 SkMD5 hash;
1076 if (data->getLength()) {
1077 hash.writeStream(data, data->getLength());
1078 data->rewind();
mtklein38408462015-07-08 07:25:27 -07001079 } else {
mtkleincd50bca2016-01-05 06:20:20 -08001080 // If we're BGRA (Linux, Windows), swizzle over to RGBA (Mac, Android).
1081 // This helps eliminate multiple 0-pixel-diff hashes on gold.skia.org.
1082 // (Android's general slow speed breaks the tie arbitrarily in RGBA's favor.)
1083 // We might consider promoting 565 to RGBA too.
1084 if (bitmap.colorType() == kBGRA_8888_SkColorType) {
1085 SkBitmap swizzle;
Matt Sarett68b8e3d2017-04-28 11:15:22 -04001086 SkAssertResult(sk_tool_utils::copy_to(&swizzle, kRGBA_8888_SkColorType,
1087 bitmap));
mtkleincd50bca2016-01-05 06:20:20 -08001088 hash.write(swizzle.getPixels(), swizzle.getSize());
1089 } else {
1090 hash.write(bitmap.getPixels(), bitmap.getSize());
1091 }
1092 }
1093 SkMD5::Digest digest;
1094 hash.finish(digest);
1095 for (int i = 0; i < 16; i++) {
1096 md5.appendf("%02x", digest.data[i]);
mtklein38408462015-07-08 07:25:27 -07001097 }
mtklein62bd1a62015-01-27 14:46:26 -08001098 }
mtklein62bd1a62015-01-27 14:46:26 -08001099
mtkleincd50bca2016-01-05 06:20:20 -08001100 if (!FLAGS_readPath.isEmpty() &&
mtklein3eed7dd2016-02-24 19:07:07 -08001101 !gGold.contains(Gold(task.sink.tag, task.src.tag,
1102 task.src.options, name, md5))) {
mtkleincd50bca2016-01-05 06:20:20 -08001103 fail(SkStringPrintf("%s not found for %s %s %s %s in %s",
1104 md5.c_str(),
mtklein21eaf3b2016-02-08 12:39:59 -08001105 task.sink.tag.c_str(),
1106 task.src.tag.c_str(),
1107 task.src.options.c_str(),
mtkleincd50bca2016-01-05 06:20:20 -08001108 name.c_str(),
1109 FLAGS_readPath[0]));
mtklein748ca3b2015-01-15 10:56:12 -08001110 }
mtkleincd50bca2016-01-05 06:20:20 -08001111
1112 if (!FLAGS_writePath.isEmpty()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001113 const char* ext = task.sink->fileExtension();
mtkleincd50bca2016-01-05 06:20:20 -08001114 if (data->getLength()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001115 WriteToDisk(task, md5, ext, data, data->getLength(), nullptr);
mtkleincd50bca2016-01-05 06:20:20 -08001116 SkASSERT(bitmap.drawsNothing());
1117 } else if (!bitmap.drawsNothing()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001118 WriteToDisk(task, md5, ext, nullptr, 0, &bitmap);
mtkleincd50bca2016-01-05 06:20:20 -08001119 }
1120 }
1121 });
mtklein748ca3b2015-01-15 10:56:12 -08001122 }
mtklein3eed7dd2016-02-24 19:07:07 -08001123 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 -08001124 }
1125
1126 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -08001127 SkString md5,
1128 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -08001129 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -08001130 const SkBitmap* bitmap) {
mtklein409d4702016-02-29 07:38:01 -08001131 bool gammaCorrect = false;
1132 if (bitmap) {
Brian Osman7039f742016-11-01 15:56:16 -04001133 gammaCorrect = SkToBool(bitmap->info().colorSpace());
mtklein409d4702016-02-29 07:38:01 -08001134 }
1135
mtklein748ca3b2015-01-15 10:56:12 -08001136 JsonWriter::BitmapResult result;
djsollen54416de2015-04-03 07:24:48 -07001137 result.name = task.src->name();
mtklein3eed7dd2016-02-24 19:07:07 -08001138 result.config = task.sink.tag;
djsollen54416de2015-04-03 07:24:48 -07001139 result.sourceType = task.src.tag;
1140 result.sourceOptions = task.src.options;
1141 result.ext = ext;
mtklein409d4702016-02-29 07:38:01 -08001142 result.gammaCorrect = gammaCorrect;
djsollen54416de2015-04-03 07:24:48 -07001143 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -08001144 JsonWriter::AddBitmapResult(result);
1145
mtkleinb0531a72015-04-07 13:38:48 -07001146 // If an MD5 is uninteresting, we want it noted in the JSON file,
1147 // but don't want to dump it out as a .png (or whatever ext is).
1148 if (gUninterestingHashes.contains(md5)) {
1149 return;
1150 }
1151
mtklein748ca3b2015-01-15 10:56:12 -08001152 const char* dir = FLAGS_writePath[0];
1153 if (0 == strcmp(dir, "@")) { // Needed for iOS.
1154 dir = FLAGS_resourcePath[0];
1155 }
1156 sk_mkdir(dir);
1157
1158 SkString path;
1159 if (FLAGS_nameByHash) {
1160 path = SkOSPath::Join(dir, result.md5.c_str());
1161 path.append(".");
1162 path.append(ext);
1163 if (sk_exists(path.c_str())) {
1164 return; // Content-addressed. If it exists already, we're done.
1165 }
1166 } else {
kkinnunen3e980c32015-12-23 01:33:00 -08001167 path = SkOSPath::Join(dir, task.sink.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001168 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001169 path = SkOSPath::Join(path.c_str(), task.src.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001170 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001171 if (strcmp(task.src.options.c_str(), "") != 0) {
1172 path = SkOSPath::Join(path.c_str(), task.src.options.c_str());
djsollen54416de2015-04-03 07:24:48 -07001173 sk_mkdir(path.c_str());
1174 }
mtklein748ca3b2015-01-15 10:56:12 -08001175 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
1176 path.append(".");
1177 path.append(ext);
1178 }
1179
mtklein748ca3b2015-01-15 10:56:12 -08001180 if (bitmap) {
mtkleina5114d72015-08-24 13:27:01 -07001181 if (!dump_png(*bitmap, path.c_str(), result.md5.c_str())) {
mtklein748ca3b2015-01-15 10:56:12 -08001182 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
1183 return;
1184 }
1185 } else {
mtkleina5114d72015-08-24 13:27:01 -07001186 SkFILEWStream file(path.c_str());
1187 if (!file.isValid()) {
1188 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
1189 return;
1190 }
mtklein748ca3b2015-01-15 10:56:12 -08001191 if (!file.writeStream(data, len)) {
1192 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
1193 return;
1194 }
1195 }
1196 }
1197};
1198
mtklein748ca3b2015-01-15 10:56:12 -08001199/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1200
1201// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
1202
mtklein21eaf3b2016-02-08 12:39:59 -08001203static SkTDArray<skiatest::Test> gParallelTests, gSerialTests;
mtklein748ca3b2015-01-15 10:56:12 -08001204
1205static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -08001206 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -08001207 return;
1208 }
mtklein6393c062015-04-27 08:45:01 -07001209 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r; r = r->next()) {
1210 if (!in_shard()) {
1211 continue;
1212 }
halcanary87f3ba42015-01-20 09:30:20 -08001213 // Despite its name, factory() is returning a reference to
1214 // link-time static const POD data.
1215 const skiatest::Test& test = r->factory();
1216 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -08001217 continue;
1218 }
halcanary87f3ba42015-01-20 09:30:20 -08001219 if (test.needsGpu && gpu_supported()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001220 (FLAGS_gpu_threading ? gParallelTests : gSerialTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -08001221 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein21eaf3b2016-02-08 12:39:59 -08001222 gParallelTests.push(test);
mtklein82d28432015-01-15 12:46:02 -08001223 }
mtklein748ca3b2015-01-15 10:56:12 -08001224 }
1225}
1226
csmartdalton008b9d82017-02-22 12:00:42 -07001227static void run_test(skiatest::Test test, const GrContextOptions& grCtxOptions) {
halcanary87f3ba42015-01-20 09:30:20 -08001228 struct : public skiatest::Reporter {
mtklein36352bf2015-03-25 18:17:31 -07001229 void reportFailed(const skiatest::Failure& failure) override {
halcanary87f3ba42015-01-20 09:30:20 -08001230 fail(failure.toString());
1231 JsonWriter::AddTestFailure(failure);
1232 }
mtklein36352bf2015-03-25 18:17:31 -07001233 bool allowExtendedTest() const override {
halcanary87f3ba42015-01-20 09:30:20 -08001234 return FLAGS_pathOpsExtended;
1235 }
mtklein36352bf2015-03-25 18:17:31 -07001236 bool verbose() const override { return FLAGS_veryVerbose; }
halcanary87f3ba42015-01-20 09:30:20 -08001237 } reporter;
djsollen824996a2015-06-12 12:06:22 -07001238
mtklein3eed7dd2016-02-24 19:07:07 -08001239 if (!FLAGS_dryRun && !is_blacklisted("_", "tests", "_", test.name)) {
mtklein21eaf3b2016-02-08 12:39:59 -08001240 start("unit", "test", "", test.name);
csmartdalton008b9d82017-02-22 12:00:42 -07001241 GrContextFactory factory(grCtxOptions);
mtklein21eaf3b2016-02-08 12:39:59 -08001242 test.proc(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -08001243 }
mtklein3eed7dd2016-02-24 19:07:07 -08001244 done("unit", "test", "", test.name);
mtklein748ca3b2015-01-15 10:56:12 -08001245}
1246
1247/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1248
caryclark83ca6282015-06-10 09:31:09 -07001249#define PORTABLE_FONT_PREFIX "Toy Liberation "
1250
mbocee6a9912016-05-31 11:42:36 -07001251static sk_sp<SkTypeface> create_from_name(const char familyName[], SkFontStyle style) {
caryclark83ca6282015-06-10 09:31:09 -07001252 if (familyName && strlen(familyName) > sizeof(PORTABLE_FONT_PREFIX)
1253 && !strncmp(familyName, PORTABLE_FONT_PREFIX, sizeof(PORTABLE_FONT_PREFIX) - 1)) {
caryclark1818acb2015-07-24 12:09:25 -07001254 return sk_tool_utils::create_portable_typeface(familyName, style);
caryclark83ca6282015-06-10 09:31:09 -07001255 }
halcanary96fcdcc2015-08-27 07:41:13 -07001256 return nullptr;
caryclark83ca6282015-06-10 09:31:09 -07001257}
1258
1259#undef PORTABLE_FONT_PREFIX
1260
mbocee6a9912016-05-31 11:42:36 -07001261extern sk_sp<SkTypeface> (*gCreateTypefaceDelegate)(const char [], SkFontStyle );
caryclark83ca6282015-06-10 09:31:09 -07001262
Mike Kleinbe28ee22017-02-06 12:46:20 -05001263int main(int argc, char** argv) {
1264 SkCommandLineFlags::Parse(argc, argv);
Mike Kleinadacaef2017-02-06 09:26:14 -05001265#if defined(SK_BUILD_FOR_IOS)
1266 cd_Documents();
1267#endif
bsalomon71de3532016-04-26 14:27:21 -07001268 setbuf(stdout, nullptr);
mtklein246ba3a2016-02-23 10:39:36 -08001269 setup_crash_handler();
mtkleinafae30a2016-02-24 12:28:32 -08001270
Yuqian Liba62b4a2016-12-14 13:46:53 -05001271 gSkUseAnalyticAA = FLAGS_analyticAA;
liyuqian38911a72016-10-04 11:23:22 -07001272
Yuqian Li550148b2017-01-13 10:13:13 -05001273 if (FLAGS_forceAnalyticAA) {
1274 gSkForceAnalyticAA = true;
1275 }
Mike Reed7c26ca72017-07-05 15:45:52 -04001276 if (FLAGS_forceRasterPipeline) {
1277 gSkForceRasterPipelineBlitter = true;
1278 }
Yuqian Li550148b2017-01-13 10:13:13 -05001279
Mike Kleine9f187b2017-06-26 13:23:12 -04001280 // The bots like having a verbose.log to upload, so always touch the file even if --verbose.
1281 if (!FLAGS_writePath.isEmpty()) {
mtklein51c8cfc2016-03-11 12:59:09 -08001282 sk_mkdir(FLAGS_writePath[0]);
mtkleina1ce2162016-07-22 12:23:46 -07001283 gVLog = fopen(SkOSPath::Join(FLAGS_writePath[0], "verbose.log").c_str(), "w");
mtkleinc41fd922016-03-08 09:01:39 -08001284 }
Mike Kleine9f187b2017-06-26 13:23:12 -04001285 if (FLAGS_verbose) {
1286 gVLog = stderr;
1287 }
mtkleinc41fd922016-03-08 09:01:39 -08001288
csmartdalton008b9d82017-02-22 12:00:42 -07001289 GrContextOptions grCtxOptions;
1290#if SK_SUPPORT_GPU
1291 grCtxOptions.fGpuPathRenderers = CollectGpuPathRenderersFromFlags();
1292#endif
1293
mtklein5286f022016-01-22 08:18:14 -08001294 JsonWriter::DumpJson(); // It's handy for the bots to assume this is ~never missing.
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +00001295 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -07001296 SkTaskGroup::Enabler enabled(FLAGS_threads);
caryclark83ca6282015-06-10 09:31:09 -07001297 gCreateTypefaceDelegate = &create_from_name;
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +00001298
halcanary7d571242016-02-24 17:59:16 -08001299 {
1300 SkString testResourcePath = GetResourcePath("color_wheel.png");
1301 SkFILEStream testResource(testResourcePath.c_str());
1302 if (!testResource.isValid()) {
mtkleinc41fd922016-03-08 09:01:39 -08001303 info("Some resources are missing. Do you need to set --resourcePath?\n");
halcanary7d571242016-02-24 17:59:16 -08001304 }
1305 }
mtklein62bd1a62015-01-27 14:46:26 -08001306 gather_gold();
borenet09ed4802015-04-03 14:15:33 -07001307 gather_uninteresting_hashes();
mtklein62bd1a62015-01-27 14:46:26 -08001308
scroggo86737142016-02-03 12:19:11 -08001309 if (!gather_srcs()) {
1310 return 1;
1311 }
csmartdalton008b9d82017-02-22 12:00:42 -07001312 if (!gather_sinks(grCtxOptions)) {
brianosman05c987c2016-05-09 13:42:16 -07001313 return 1;
1314 }
mtklein748ca3b2015-01-15 10:56:12 -08001315 gather_tests();
mtklein21eaf3b2016-02-08 12:39:59 -08001316 gPending = gSrcs.count() * gSinks.count() + gParallelTests.count() + gSerialTests.count();
mtkleinc41fd922016-03-08 09:01:39 -08001317 info("%d srcs * %d sinks + %d tests == %d tasks",
1318 gSrcs.count(), gSinks.count(), gParallelTests.count() + gSerialTests.count(), gPending);
mtklein748ca3b2015-01-15 10:56:12 -08001319
mtklein21eaf3b2016-02-08 12:39:59 -08001320 // Kick off as much parallel work as we can, making note of any serial work we'll need to do.
1321 SkTaskGroup parallel;
1322 SkTArray<Task> serial;
1323
1324 for (auto& sink : gSinks)
1325 for (auto& src : gSrcs) {
mtklein3eed7dd2016-02-24 19:07:07 -08001326 if (src->veto(sink->flags()) ||
1327 is_blacklisted(sink.tag.c_str(), src.tag.c_str(),
1328 src.options.c_str(), src->name().c_str())) {
mtkleinf10637f2016-06-10 13:56:35 -07001329 SkAutoMutexAcquire lock(gMutex);
mtklein3eed7dd2016-02-24 19:07:07 -08001330 gPending--;
1331 continue;
1332 }
1333
mtklein21eaf3b2016-02-08 12:39:59 -08001334 Task task(src, sink);
1335 if (src->serial() || sink->serial()) {
1336 serial.push_back(task);
1337 } else {
1338 parallel.add([task] { Task::Run(task); });
mtklein748ca3b2015-01-15 10:56:12 -08001339 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001340 }
mtklein21eaf3b2016-02-08 12:39:59 -08001341 for (auto test : gParallelTests) {
csmartdalton008b9d82017-02-22 12:00:42 -07001342 parallel.add([test, grCtxOptions] { run_test(test, grCtxOptions); });
mtklein55e88b22015-01-21 15:50:13 -08001343 }
mtklein21eaf3b2016-02-08 12:39:59 -08001344
1345 // With the parallel work running, run serial tasks and tests here on main thread.
1346 for (auto task : serial) { Task::Run(task); }
csmartdalton008b9d82017-02-22 12:00:42 -07001347 for (auto test : gSerialTests) { run_test(test, grCtxOptions); }
mtklein21eaf3b2016-02-08 12:39:59 -08001348
1349 // Wait for any remaining parallel work to complete (including any spun off of serial tasks).
1350 parallel.wait();
mtkleincd50bca2016-01-05 06:20:20 -08001351 gDefinitelyThreadSafeWork.wait();
1352
mtkleinda884c42016-02-24 18:00:23 -08001353 // We'd better have run everything.
1354 SkASSERT(gPending == 0);
mtkleine027f172016-02-26 15:53:06 -08001355 // Make sure we've flushed all our results to disk.
1356 JsonWriter::DumpJson();
mtkleinda884c42016-02-24 18:00:23 -08001357
mtklein748ca3b2015-01-15 10:56:12 -08001358 // At this point we're back in single-threaded land.
caryclarkf53ce802015-06-15 06:48:30 -07001359 sk_tool_utils::release_portable_typefaces();
mtklein@google.comd36522d2013-10-16 13:02:15 +00001360
mtklein748ca3b2015-01-15 10:56:12 -08001361 if (gFailures.count() > 0) {
mtkleinc41fd922016-03-08 09:01:39 -08001362 info("Failures:\n");
mtklein748ca3b2015-01-15 10:56:12 -08001363 for (int i = 0; i < gFailures.count(); i++) {
mtkleinc41fd922016-03-08 09:01:39 -08001364 info("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001365 }
mtkleinc41fd922016-03-08 09:01:39 -08001366 info("%d failures\n", gFailures.count());
mtklein748ca3b2015-01-15 10:56:12 -08001367 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001368 }
mtkleinafae30a2016-02-24 12:28:32 -08001369
1370#ifdef SK_PDF_IMAGE_STATS
halcanary7a14b312015-10-01 07:28:13 -07001371 SkPDFImageDumpStats();
mtkleinafae30a2016-02-24 12:28:32 -08001372#endif // SK_PDF_IMAGE_STATS
1373
Herb Derby5a523fe2017-01-26 16:48:28 -05001374 SkGraphics::PurgeAllCaches();
mtkleinc41fd922016-03-08 09:01:39 -08001375 info("Finished!\n");
mtklein748ca3b2015-01-15 10:56:12 -08001376 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +00001377}
jcgregorio3b27ade2014-11-13 08:06:40 -08001378
kkinnunen179a8f52015-11-20 13:32:24 -08001379// TODO: currently many GPU tests are declared outside SK_SUPPORT_GPU guards.
1380// Thus we export the empty RunWithGPUTestContexts when SK_SUPPORT_GPU=0.
1381namespace skiatest {
bsalomon758586c2016-04-06 14:02:39 -07001382
1383#if SK_SUPPORT_GPU
1384bool IsGLContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1385 return kOpenGL_GrBackend == GrContextFactory::ContextTypeBackend(type);
1386}
bsalomondc0fcd42016-04-11 14:21:33 -07001387bool IsVulkanContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1388 return kVulkan_GrBackend == GrContextFactory::ContextTypeBackend(type);
1389}
bsalomon758586c2016-04-06 14:02:39 -07001390bool IsRenderingGLContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1391 return IsGLContextType(type) && GrContextFactory::IsRenderingContext(type);
1392}
1393bool IsNullGLContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1394 return type == GrContextFactory::kNullGL_ContextType;
1395}
1396#else
1397bool IsGLContextType(int) { return false; }
bsalomondc0fcd42016-04-11 14:21:33 -07001398bool IsVulkanContextType(int) { return false; }
bsalomon758586c2016-04-06 14:02:39 -07001399bool IsRenderingGLContextType(int) { return false; }
1400bool IsNullGLContextType(int) { return false; }
1401#endif
1402
1403void RunWithGPUTestContexts(GrContextTestFn* test, GrContextTypeFilterFn* contextTypeFilter,
bsalomonf2f1c172016-04-05 12:59:06 -07001404 Reporter* reporter, GrContextFactory* factory) {
kkinnunen179a8f52015-11-20 13:32:24 -08001405#if SK_SUPPORT_GPU
kkinnunen3e980c32015-12-23 01:33:00 -08001406
Brian Salomon6405e712017-03-20 08:54:16 -04001407#if defined(SK_BUILD_FOR_UNIX) || defined(SK_BUILD_FOR_WIN) || defined(SK_BUILD_FOR_MAC)
1408 static constexpr auto kNativeGLType = GrContextFactory::kGL_ContextType;
1409#else
1410 static constexpr auto kNativeGLType = GrContextFactory::kGLES_ContextType;
1411#endif
1412
bsalomon758586c2016-04-06 14:02:39 -07001413 for (int typeInt = 0; typeInt < GrContextFactory::kContextTypeCnt; ++typeInt) {
1414 GrContextFactory::ContextType contextType = (GrContextFactory::ContextType) typeInt;
Brian Osmanaa2219c2016-12-05 10:12:17 -05001415 // Use "native" instead of explicitly trying OpenGL and OpenGL ES. Do not use GLES on
bsalomon758586c2016-04-06 14:02:39 -07001416 // desktop since tests do not account for not fixing http://skbug.com/2809
1417 if (contextType == GrContextFactory::kGL_ContextType ||
1418 contextType == GrContextFactory::kGLES_ContextType) {
Brian Salomon6405e712017-03-20 08:54:16 -04001419 if (contextType != kNativeGLType) {
bsalomon758586c2016-04-06 14:02:39 -07001420 continue;
1421 }
1422 }
csmartdaltone812d492017-02-21 12:36:05 -07001423 ContextInfo ctxInfo = factory->getContextInfo(contextType,
1424 GrContextFactory::ContextOverrides::kDisableNVPR);
Brian Osmanaa2219c2016-12-05 10:12:17 -05001425 if (contextTypeFilter && !(*contextTypeFilter)(contextType)) {
1426 continue;
1427 }
Greg Daniela5cb7812017-06-16 09:45:32 -04001428 ReporterContext ctx(reporter, SkString(GrContextFactory::ContextTypeName(contextType)));
bsalomon8b7451a2016-05-11 06:33:06 -07001429 if (ctxInfo.grContext()) {
bsalomonf2f1c172016-04-05 12:59:06 -07001430 (*test)(reporter, ctxInfo);
kkinnunen5219fd92015-12-10 06:28:13 -08001431 }
bsalomonf2f1c172016-04-05 12:59:06 -07001432 ctxInfo = factory->getContextInfo(contextType,
csmartdaltone812d492017-02-21 12:36:05 -07001433 GrContextFactory::ContextOverrides::kRequireNVPRSupport);
bsalomon8b7451a2016-05-11 06:33:06 -07001434 if (ctxInfo.grContext()) {
bsalomonf2f1c172016-04-05 12:59:06 -07001435 (*test)(reporter, ctxInfo);
kkinnunen179a8f52015-11-20 13:32:24 -08001436 }
1437 }
1438#endif
1439}
kkinnunen179a8f52015-11-20 13:32:24 -08001440} // namespace skiatest