blob: ef048a48d74a82405e2831d33b2734a888c0ff31 [file] [log] [blame]
scroggo478652e2015-03-25 07:11:02 -07001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
mtklein748ca3b2015-01-15 10:56:12 -08008#include "DMJsonWriter.h"
9#include "DMSrcSink.h"
mtklein748ca3b2015-01-15 10:56:12 -080010#include "ProcStats.h"
halcanary7d571242016-02-24 17:59:16 -080011#include "Resources.h"
mtklein748ca3b2015-01-15 10:56:12 -080012#include "SkBBHFactory.h"
mtklein62bd1a62015-01-27 14:46:26 -080013#include "SkChecksum.h"
scroggocc2feb12015-08-14 08:32:46 -070014#include "SkCodec.h"
mtklein27c3fdd2016-02-26 14:43:21 -080015#include "SkColorPriv.h"
msarett888dc162016-05-23 10:21:17 -070016#include "SkColorSpace.h"
Mike Klein919cc452017-03-18 15:36:52 +000017#include "SkColorSpacePriv.h"
caryclark17f0b6d2014-07-22 10:15:34 -070018#include "SkCommonFlags.h"
kkinnunen3e980c32015-12-23 01:33:00 -080019#include "SkCommonFlagsConfig.h"
csmartdalton008b9d82017-02-22 12:00:42 -070020#include "SkCommonFlagsPathRenderer.h"
brianosman3c579dc2016-04-19 09:18:11 -070021#include "SkData.h"
Brian Salomondcbb9d92017-07-19 10:53:20 -040022#include "SkDebugfTracer.h"
23#include "SkEventTracer.h"
caryclark83ca6282015-06-10 09:31:09 -070024#include "SkFontMgr.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +000025#include "SkGraphics.h"
mtklein27c3fdd2016-02-26 14:43:21 -080026#include "SkHalf.h"
halcanary4dbbd042016-06-07 17:21:10 -070027#include "SkLeanWindows.h"
mtklein748ca3b2015-01-15 10:56:12 -080028#include "SkMD5.h"
mtklein1b249332015-07-07 12:21:21 -070029#include "SkMutex.h"
mtklein1d0f1642014-09-08 08:05:18 -070030#include "SkOSFile.h"
Ben Wagnerbf111d72016-11-07 18:05:29 -050031#include "SkOSPath.h"
mtklein27c3fdd2016-02-26 14:43:21 -080032#include "SkPM4fPriv.h"
Mike Klein13b6afd2017-06-12 12:45:41 -040033#include "SkPngEncoder.h"
Brian Salomondcbb9d92017-07-19 10:53:20 -040034#include "SkScan.h"
mtklein246ba3a2016-02-23 10:39:36 -080035#include "SkSpinlock.h"
mtkleina82f5622015-02-20 12:30:19 -080036#include "SkTHash.h"
mtklein406654b2014-09-03 15:34:37 -070037#include "SkTaskGroup.h"
mtkleinde6fc2b2015-03-12 06:28:54 -070038#include "SkThreadUtils.h"
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +000039#include "Test.h"
mtklein748ca3b2015-01-15 10:56:12 -080040#include "Timer.h"
Mike Kleinadacaef2017-02-06 09:26:14 -050041#include "ios_utils.h"
brianosman3c579dc2016-04-19 09:18:11 -070042#include "picture_utils.h"
caryclark83ca6282015-06-10 09:31:09 -070043#include "sk_tool_utils.h"
mtklein@google.comd36522d2013-10-16 13:02:15 +000044
scroggo19b91532016-10-24 09:03:26 -070045#include <vector>
46
halcanary7a14b312015-10-01 07:28:13 -070047#ifdef SK_PDF_IMAGE_STATS
48extern void SkPDFImageDumpStats();
49#endif
50
mtkleina5114d72015-08-24 13:27:01 -070051#include "png.h"
52
bungeman60e0fee2015-08-26 05:15:46 -070053#include <stdlib.h>
54
scroggo27a58342015-10-28 08:56:41 -070055#ifndef SK_BUILD_FOR_WIN32
56 #include <unistd.h>
57#endif
58
Mike Reed7c26ca72017-07-05 15:45:52 -040059extern bool gSkForceRasterPipelineBlitter;
60
djsollen54416de2015-04-03 07:24:48 -070061DEFINE_string(src, "tests gm skp image", "Source types to test.");
mtklein748ca3b2015-01-15 10:56:12 -080062DEFINE_bool(nameByHash, false,
63 "If true, write to FLAGS_writePath[0]/<hash>.png instead of "
djsollen54416de2015-04-03 07:24:48 -070064 "to FLAGS_writePath[0]/<config>/<sourceType>/<sourceOptions>/<name>.png");
mtklein748ca3b2015-01-15 10:56:12 -080065DEFINE_bool2(pathOpsExtended, x, false, "Run extended pathOps tests.");
mtkleind603b222015-02-17 11:13:33 -080066DEFINE_string(matrix, "1 0 0 1",
67 "2x2 scale+skew matrix to apply or upright when using "
68 "'matrix' or 'upright' in config.");
mtklein82d28432015-01-15 12:46:02 -080069DEFINE_bool(gpu_threading, false, "Allow GPU work to run on multiple threads?");
mtklein@google.comd36522d2013-10-16 13:02:15 +000070
mtkleina2ef6422015-01-15 13:44:22 -080071DEFINE_string(blacklist, "",
djsollen54416de2015-04-03 07:24:48 -070072 "Space-separated config/src/srcOptions/name quadruples to blacklist. '_' matches anything. E.g. \n"
73 "'--blacklist gpu skp _ _' will blacklist all SKPs drawn into the gpu config.\n"
74 "'--blacklist gpu skp _ _ 8888 gm _ aarects' will also blacklist the aarects GM on 8888.");
mtkleina2ef6422015-01-15 13:44:22 -080075
mtklein62bd1a62015-01-27 14:46:26 -080076DEFINE_string2(readPath, r, "", "If set check for equality with golden results in this directory.");
77
borenet09ed4802015-04-03 14:15:33 -070078DEFINE_string(uninterestingHashesFile, "",
79 "File containing a list of uninteresting hashes. If a result hashes to something in "
80 "this list, no image is written for that result.");
81
mtklein6393c062015-04-27 08:45:01 -070082DEFINE_int32(shards, 1, "We're splitting source data into this many shards.");
83DEFINE_int32(shard, 0, "Which shard do I run?");
84
halcanary45420a92016-06-02 12:41:14 -070085DEFINE_string(mskps, "", "Directory to read mskps from, or a single mskp file.");
Mike Reed7c26ca72017-07-05 15:45:52 -040086DEFINE_bool(forceRasterPipeline, false, "sets gSkForceRasterPipelineBlitter");
halcanary45420a92016-06-02 12:41:14 -070087
csmartdalton008b9d82017-02-22 12:00:42 -070088#if SK_SUPPORT_GPU
89DEFINE_pathrenderer_flag;
90#endif
91
mtklein748ca3b2015-01-15 10:56:12 -080092using namespace DM;
bsalomon3724e572016-03-30 18:56:19 -070093using sk_gpu_test::GrContextFactory;
bsalomon273c0f52016-03-31 10:59:06 -070094using sk_gpu_test::GLTestContext;
bsalomonf2f1c172016-04-05 12:59:06 -070095using sk_gpu_test::ContextInfo;
mtklein@google.comd36522d2013-10-16 13:02:15 +000096
mtklein748ca3b2015-01-15 10:56:12 -080097/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
98
benjaminwagnerec4d4d72016-03-25 12:59:53 -070099static const double kStartMs = SkTime::GetMSecs();
mtkleinc41fd922016-03-08 09:01:39 -0800100
101static FILE* gVLog;
102
103template <typename... Args>
104static void vlog(const char* fmt, Args&&... args) {
105 if (gVLog) {
106 fprintf(gVLog, "%s\t", HumanizeMs(SkTime::GetMSecs() - kStartMs).c_str());
107 fprintf(gVLog, fmt, args...);
108 fflush(gVLog);
109 }
110}
111
112template <typename... Args>
113static void info(const char* fmt, Args&&... args) {
114 vlog(fmt, args...);
115 if (!FLAGS_quiet) {
116 printf(fmt, args...);
117 }
118}
119static void info(const char* fmt) {
120 if (!FLAGS_quiet) {
121 printf("%s", fmt); // Clang warns printf(fmt) is insecure.
122 }
123}
124
reed086eea92016-05-04 17:12:46 -0700125SK_DECLARE_STATIC_MUTEX(gFailuresMutex);
mtklein748ca3b2015-01-15 10:56:12 -0800126static SkTArray<SkString> gFailures;
127
mtklein3eed7dd2016-02-24 19:07:07 -0800128static void fail(const SkString& err) {
mtklein748ca3b2015-01-15 10:56:12 -0800129 SkAutoMutexAcquire lock(gFailuresMutex);
130 SkDebugf("\n\nFAILURE: %s\n\n", err.c_str());
131 gFailures.push_back(err);
halcanary9e398f72015-01-10 11:18:04 -0800132}
133
mtklein0f7748a2016-07-25 15:27:29 -0700134struct Running {
135 SkString id;
136 SkThreadID thread;
137
138 void dump() const {
139 info("\t%s\n", id.c_str());
140 }
141};
mtkleinb37cb412015-03-18 05:27:14 -0700142
mtklein246ba3a2016-02-23 10:39:36 -0800143// We use a spinlock to make locking this in a signal handler _somewhat_ safe.
Mike Klein03a95ad2017-06-23 12:36:32 -0400144static SkSpinlock gMutex;
145static int gPending;
mtklein0f7748a2016-07-25 15:27:29 -0700146static SkTArray<Running> gRunning;
mtklein748ca3b2015-01-15 10:56:12 -0800147
mtklein3eed7dd2016-02-24 19:07:07 -0800148static void done(const char* config, const char* src, const char* srcOptions, const char* name) {
149 SkString id = SkStringPrintf("%s %s %s %s", config, src, srcOptions, name);
mtkleinc41fd922016-03-08 09:01:39 -0800150 vlog("done %s\n", id.c_str());
mtkleinafae30a2016-02-24 12:28:32 -0800151 int pending;
mtkleinb37cb412015-03-18 05:27:14 -0700152 {
mtkleinf10637f2016-06-10 13:56:35 -0700153 SkAutoMutexAcquire lock(gMutex);
mtkleinb37cb412015-03-18 05:27:14 -0700154 for (int i = 0; i < gRunning.count(); i++) {
mtklein0f7748a2016-07-25 15:27:29 -0700155 if (gRunning[i].id == id) {
mtkleinb37cb412015-03-18 05:27:14 -0700156 gRunning.removeShuffle(i);
157 break;
158 }
159 }
mtkleinafae30a2016-02-24 12:28:32 -0800160 pending = --gPending;
reed50bc0512015-05-19 14:13:31 -0700161 }
Mike Klein03a95ad2017-06-23 12:36:32 -0400162
163 // We write out dm.json file and print out a progress update every once in a while.
164 // Notice this also handles the final dm.json and progress update when pending == 0.
mtkleina17241b2015-01-23 05:48:00 -0800165 if (pending % 500 == 0) {
166 JsonWriter::DumpJson();
Mike Klein03a95ad2017-06-23 12:36:32 -0400167
168 int curr = sk_tools::getCurrResidentSetSizeMB(),
169 peak = sk_tools::getMaxResidentSetSizeMB();
170 SkString elapsed = HumanizeMs(SkTime::GetMSecs() - kStartMs);
171
172 SkAutoMutexAcquire lock(gMutex);
173 info("\n%dMB RAM, %dMB peak, %s elapsed, %d queued, %d active:\n",
174 curr, peak, elapsed.c_str(), gPending - gRunning.count(), gRunning.count());
175 for (auto& task : gRunning) {
176 task.dump();
177 }
mtkleina17241b2015-01-23 05:48:00 -0800178 }
mtklein@google.comd36522d2013-10-16 13:02:15 +0000179}
180
mtklein3eed7dd2016-02-24 19:07:07 -0800181static void start(const char* config, const char* src, const char* srcOptions, const char* name) {
182 SkString id = SkStringPrintf("%s %s %s %s", config, src, srcOptions, name);
mtkleinc41fd922016-03-08 09:01:39 -0800183 vlog("start %s\n", id.c_str());
mtkleinf10637f2016-06-10 13:56:35 -0700184 SkAutoMutexAcquire lock(gMutex);
mtklein0f7748a2016-07-25 15:27:29 -0700185 gRunning.push_back({id,SkGetThreadID()});
mtkleinb37cb412015-03-18 05:27:14 -0700186}
187
Mike Klein3cc2d202016-10-24 11:39:43 -0400188static void find_culprit() {
189 // Assumes gMutex is locked.
190 SkThreadID thisThread = SkGetThreadID();
191 for (auto& task : gRunning) {
192 if (task.thread == thisThread) {
193 info("Likely culprit:\n");
194 task.dump();
mtklein0f7748a2016-07-25 15:27:29 -0700195 }
196 }
Mike Klein3cc2d202016-10-24 11:39:43 -0400197}
mtklein0f7748a2016-07-25 15:27:29 -0700198
mtklein246ba3a2016-02-23 10:39:36 -0800199#if defined(SK_BUILD_FOR_WIN32)
mtklein5b64dab2016-06-09 08:59:48 -0700200 static LONG WINAPI crash_handler(EXCEPTION_POINTERS* e) {
mtkleinf8557292016-02-29 06:35:28 -0800201 static const struct {
202 const char* name;
robertphillips75467862016-03-01 14:10:23 -0800203 DWORD code;
mtkleinf8557292016-02-29 06:35:28 -0800204 } kExceptions[] = {
205 #define _(E) {#E, E}
206 _(EXCEPTION_ACCESS_VIOLATION),
207 _(EXCEPTION_BREAKPOINT),
208 _(EXCEPTION_INT_DIVIDE_BY_ZERO),
209 _(EXCEPTION_STACK_OVERFLOW),
210 // TODO: more?
211 #undef _
212 };
213
mtkleinf10637f2016-06-10 13:56:35 -0700214 SkAutoMutexAcquire lock(gMutex);
mtklein5b64dab2016-06-09 08:59:48 -0700215
216 const DWORD code = e->ExceptionRecord->ExceptionCode;
217 info("\nCaught exception %u", code);
218 for (const auto& exception : kExceptions) {
219 if (exception.code == code) {
220 info(" %s", exception.name);
mtkleinf8557292016-02-29 06:35:28 -0800221 }
mtkleinf8557292016-02-29 06:35:28 -0800222 }
mtklein5b64dab2016-06-09 08:59:48 -0700223 info(", was running:\n");
224 for (auto& task : gRunning) {
mtklein0f7748a2016-07-25 15:27:29 -0700225 task.dump();
mtklein5b64dab2016-06-09 08:59:48 -0700226 }
mtklein0f7748a2016-07-25 15:27:29 -0700227 find_culprit();
mtklein5b64dab2016-06-09 08:59:48 -0700228 fflush(stdout);
229
mtkleinf8557292016-02-29 06:35:28 -0800230 // Execute default exception handler... hopefully, exit.
231 return EXCEPTION_EXECUTE_HANDLER;
mtklein246ba3a2016-02-23 10:39:36 -0800232 }
233
Mike Klein3cc2d202016-10-24 11:39:43 -0400234 static void setup_crash_handler() {
235 SetUnhandledExceptionFilter(crash_handler);
236 }
237#else
mtklein246ba3a2016-02-23 10:39:36 -0800238 #include <signal.h>
Mike Klein3cc2d202016-10-24 11:39:43 -0400239 #if !defined(SK_BUILD_FOR_ANDROID)
240 #include <execinfo.h>
Brian Salomondcbb9d92017-07-19 10:53:20 -0400241
242#endif
Mike Klein3cc2d202016-10-24 11:39:43 -0400243
244 static constexpr int max_of() { return 0; }
245 template <typename... Rest>
246 static constexpr int max_of(int x, Rest... rest) {
247 return x > max_of(rest...) ? x : max_of(rest...);
248 }
249
250 static void (*previous_handler[max_of(SIGABRT,SIGBUS,SIGFPE,SIGILL,SIGSEGV)+1])(int);
mtkleinf8557292016-02-29 06:35:28 -0800251
mtklein5b64dab2016-06-09 08:59:48 -0700252 static void crash_handler(int sig) {
mtkleinf10637f2016-06-10 13:56:35 -0700253 SkAutoMutexAcquire lock(gMutex);
mtklein5b64dab2016-06-09 08:59:48 -0700254
255 info("\nCaught signal %d [%s], was running:\n", sig, strsignal(sig));
256 for (auto& task : gRunning) {
mtklein0f7748a2016-07-25 15:27:29 -0700257 task.dump();
mtklein5b64dab2016-06-09 08:59:48 -0700258 }
mtklein0f7748a2016-07-25 15:27:29 -0700259 find_culprit();
mtklein5b64dab2016-06-09 08:59:48 -0700260
Mike Klein3cc2d202016-10-24 11:39:43 -0400261 #if !defined(SK_BUILD_FOR_ANDROID)
mtklein5b64dab2016-06-09 08:59:48 -0700262 void* stack[64];
263 int count = backtrace(stack, SK_ARRAY_COUNT(stack));
264 char** symbols = backtrace_symbols(stack, count);
265 info("\nStack trace:\n");
266 for (int i = 0; i < count; i++) {
267 info(" %s\n", symbols[i]);
268 }
Mike Klein3cc2d202016-10-24 11:39:43 -0400269 #endif
mtklein5b64dab2016-06-09 08:59:48 -0700270 fflush(stdout);
271
Mike Klein3cc2d202016-10-24 11:39:43 -0400272 signal(sig, previous_handler[sig]);
273 raise(sig);
mtklein5b64dab2016-06-09 08:59:48 -0700274 }
275
mtklein246ba3a2016-02-23 10:39:36 -0800276 static void setup_crash_handler() {
277 const int kSignals[] = { SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGSEGV };
278 for (int sig : kSignals) {
Mike Klein3cc2d202016-10-24 11:39:43 -0400279 previous_handler[sig] = signal(sig, crash_handler);
mtklein246ba3a2016-02-23 10:39:36 -0800280 }
281 }
282#endif
283
mtklein748ca3b2015-01-15 10:56:12 -0800284/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
mtklein114c3cd2015-01-15 10:15:02 -0800285
mtklein62bd1a62015-01-27 14:46:26 -0800286struct Gold : public SkString {
mtkleina82f5622015-02-20 12:30:19 -0800287 Gold() : SkString("") {}
mtklein3eed7dd2016-02-24 19:07:07 -0800288 Gold(const SkString& sink, const SkString& src,
289 const SkString& srcOptions, const SkString& name,
290 const SkString& md5)
mtklein62bd1a62015-01-27 14:46:26 -0800291 : SkString("") {
292 this->append(sink);
293 this->append(src);
djsollen54416de2015-04-03 07:24:48 -0700294 this->append(srcOptions);
mtklein62bd1a62015-01-27 14:46:26 -0800295 this->append(name);
296 this->append(md5);
mtklein62bd1a62015-01-27 14:46:26 -0800297 }
mtkleinc8d1dd42015-10-15 12:23:01 -0700298 struct Hash {
299 uint32_t operator()(const Gold& g) const {
300 return SkGoodHash()((const SkString&)g);
301 }
302 };
mtklein62bd1a62015-01-27 14:46:26 -0800303};
mtkleina82f5622015-02-20 12:30:19 -0800304static SkTHashSet<Gold, Gold::Hash> gGold;
mtklein62bd1a62015-01-27 14:46:26 -0800305
306static void add_gold(JsonWriter::BitmapResult r) {
djsollen54416de2015-04-03 07:24:48 -0700307 gGold.add(Gold(r.config, r.sourceType, r.sourceOptions, r.name, r.md5));
mtklein62bd1a62015-01-27 14:46:26 -0800308}
309
310static void gather_gold() {
311 if (!FLAGS_readPath.isEmpty()) {
312 SkString path(FLAGS_readPath[0]);
313 path.append("/dm.json");
314 if (!JsonWriter::ReadJson(path.c_str(), add_gold)) {
315 fail(SkStringPrintf("Couldn't read %s for golden results.", path.c_str()));
316 }
317 }
318}
319
320/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
321
mtklein935f1b12016-03-16 08:37:19 -0700322#if defined(SK_BUILD_FOR_WIN32)
323 static const char* kNewline = "\r\n";
324#else
325 static const char* kNewline = "\n";
326#endif
327
borenet09ed4802015-04-03 14:15:33 -0700328static SkTHashSet<SkString> gUninterestingHashes;
329
330static void gather_uninteresting_hashes() {
331 if (!FLAGS_uninterestingHashesFile.isEmpty()) {
bungeman38d909e2016-08-02 14:40:46 -0700332 sk_sp<SkData> data(SkData::MakeFromFileName(FLAGS_uninterestingHashesFile[0]));
mtkleincc334b32015-09-22 11:43:53 -0700333 if (!data) {
mtkleinc41fd922016-03-08 09:01:39 -0800334 info("WARNING: unable to read uninteresting hashes from %s\n",
335 FLAGS_uninterestingHashesFile[0]);
mtkleincc334b32015-09-22 11:43:53 -0700336 return;
337 }
Mike Klein45dcc0c2017-04-05 18:04:31 -0400338
339 // Copy to a string to make sure SkStrSplit has a terminating \0 to find.
340 SkString contents((const char*)data->data(), data->size());
341
borenet09ed4802015-04-03 14:15:33 -0700342 SkTArray<SkString> hashes;
Mike Klein45dcc0c2017-04-05 18:04:31 -0400343 SkStrSplit(contents.c_str(), kNewline, &hashes);
borenet09ed4802015-04-03 14:15:33 -0700344 for (const SkString& hash : hashes) {
345 gUninterestingHashes.add(hash);
346 }
mtkleinc41fd922016-03-08 09:01:39 -0800347 info("FYI: loaded %d distinct uninteresting hashes from %d lines\n",
348 gUninterestingHashes.count(), hashes.count());
borenet09ed4802015-04-03 14:15:33 -0700349 }
350}
351
352/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
353
Ben Wagner145dbcd2016-11-03 14:40:50 -0400354struct TaggedSrc : public std::unique_ptr<Src> {
mtklein3eed7dd2016-02-24 19:07:07 -0800355 SkString tag;
356 SkString options;
mtkleine0effd62015-07-29 06:37:28 -0700357};
358
Ben Wagner145dbcd2016-11-03 14:40:50 -0400359struct TaggedSink : public std::unique_ptr<Sink> {
kkinnunen3e980c32015-12-23 01:33:00 -0800360 SkString tag;
djsollen54416de2015-04-03 07:24:48 -0700361};
mtklein748ca3b2015-01-15 10:56:12 -0800362
363static const bool kMemcpyOK = true;
364
mtkleine0effd62015-07-29 06:37:28 -0700365static SkTArray<TaggedSrc, kMemcpyOK> gSrcs;
366static SkTArray<TaggedSink, kMemcpyOK> gSinks;
mtklein748ca3b2015-01-15 10:56:12 -0800367
mtklein6393c062015-04-27 08:45:01 -0700368static bool in_shard() {
369 static int N = 0;
370 return N++ % FLAGS_shards == FLAGS_shard;
371}
372
mtklein3eed7dd2016-02-24 19:07:07 -0800373static void push_src(const char* tag, ImplicitString options, Src* s) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400374 std::unique_ptr<Src> src(s);
mtklein6393c062015-04-27 08:45:01 -0700375 if (in_shard() &&
mtklein3eed7dd2016-02-24 19:07:07 -0800376 FLAGS_src.contains(tag) &&
mtklein748ca3b2015-01-15 10:56:12 -0800377 !SkCommandLineFlags::ShouldSkip(FLAGS_match, src->name().c_str())) {
mtkleine0effd62015-07-29 06:37:28 -0700378 TaggedSrc& s = gSrcs.push_back();
mtklein18300a32016-03-16 13:53:35 -0700379 s.reset(src.release());
mtklein748ca3b2015-01-15 10:56:12 -0800380 s.tag = tag;
djsollen54416de2015-04-03 07:24:48 -0700381 s.options = options;
mtklein114c3cd2015-01-15 10:15:02 -0800382 }
mtklein748ca3b2015-01-15 10:56:12 -0800383}
mtklein114c3cd2015-01-15 10:15:02 -0800384
msarett9e707a02015-09-01 14:57:57 -0700385static void push_codec_src(Path path, CodecSrc::Mode mode, CodecSrc::DstColorType dstColorType,
scroggoc5560be2016-02-03 09:42:42 -0800386 SkAlphaType dstAlphaType, float scale) {
scroggoe4499842016-02-25 11:03:47 -0800387 if (FLAGS_simpleCodec) {
scroggo19b91532016-10-24 09:03:26 -0700388 const bool simple = CodecSrc::kCodec_Mode == mode || CodecSrc::kAnimated_Mode == mode;
389 if (!simple || dstColorType != CodecSrc::kGetFromCanvas_DstColorType || scale != 1.0f) {
scroggoe4499842016-02-25 11:03:47 -0800390 // Only decode in the simple case.
391 return;
scroggo19b91532016-10-24 09:03:26 -0700392 }
scroggoe4499842016-02-25 11:03:47 -0800393 }
msarett9e707a02015-09-01 14:57:57 -0700394 SkString folder;
395 switch (mode) {
396 case CodecSrc::kCodec_Mode:
397 folder.append("codec");
398 break;
msarettbb25b532016-01-13 09:31:39 -0800399 case CodecSrc::kCodecZeroInit_Mode:
400 folder.append("codec_zero_init");
401 break;
msarett9e707a02015-09-01 14:57:57 -0700402 case CodecSrc::kScanline_Mode:
403 folder.append("scanline");
404 break;
msarett9e707a02015-09-01 14:57:57 -0700405 case CodecSrc::kStripe_Mode:
406 folder.append("stripe");
407 break;
msarett91c22b22016-02-22 12:27:46 -0800408 case CodecSrc::kCroppedScanline_Mode:
409 folder.append("crop");
410 break;
msarett9e707a02015-09-01 14:57:57 -0700411 case CodecSrc::kSubset_Mode:
msarett36c37962015-09-02 13:20:52 -0700412 folder.append("codec_subset");
msarett9e707a02015-09-01 14:57:57 -0700413 break;
scroggo19b91532016-10-24 09:03:26 -0700414 case CodecSrc::kAnimated_Mode:
415 folder.append("codec_animated");
416 break;
msarett9e707a02015-09-01 14:57:57 -0700417 }
418
419 switch (dstColorType) {
420 case CodecSrc::kGrayscale_Always_DstColorType:
421 folder.append("_kGray8");
422 break;
msarett34e0ec42016-04-22 16:27:24 -0700423 case CodecSrc::kNonNative8888_Always_DstColorType:
424 folder.append("_kNonNative");
425 break;
msarett9e707a02015-09-01 14:57:57 -0700426 default:
427 break;
428 }
429
scroggoc5560be2016-02-03 09:42:42 -0800430 switch (dstAlphaType) {
scroggoc5560be2016-02-03 09:42:42 -0800431 case kPremul_SkAlphaType:
432 folder.append("_premul");
433 break;
434 case kUnpremul_SkAlphaType:
435 folder.append("_unpremul");
436 break;
437 default:
438 break;
439 }
440
msarett9e707a02015-09-01 14:57:57 -0700441 if (1.0f != scale) {
442 folder.appendf("_%.3f", scale);
443 }
444
scroggoc5560be2016-02-03 09:42:42 -0800445 CodecSrc* src = new CodecSrc(path, mode, dstColorType, dstAlphaType, scale);
msarett9e707a02015-09-01 14:57:57 -0700446 push_src("image", folder, src);
447}
448
scroggof8dc9df2016-05-16 09:04:13 -0700449static void push_android_codec_src(Path path, CodecSrc::DstColorType dstColorType,
450 SkAlphaType dstAlphaType, int sampleSize) {
msarett3d9d7a72015-10-21 10:27:10 -0700451 SkString folder;
scroggof8dc9df2016-05-16 09:04:13 -0700452 folder.append("scaled_codec");
msarett3d9d7a72015-10-21 10:27:10 -0700453
454 switch (dstColorType) {
455 case CodecSrc::kGrayscale_Always_DstColorType:
456 folder.append("_kGray8");
457 break;
msarett34e0ec42016-04-22 16:27:24 -0700458 case CodecSrc::kNonNative8888_Always_DstColorType:
459 folder.append("_kNonNative");
460 break;
msarett3d9d7a72015-10-21 10:27:10 -0700461 default:
462 break;
463 }
464
scroggoc5560be2016-02-03 09:42:42 -0800465 switch (dstAlphaType) {
scroggoc5560be2016-02-03 09:42:42 -0800466 case kPremul_SkAlphaType:
467 folder.append("_premul");
468 break;
msarett9e9444c2016-02-03 12:39:10 -0800469 case kUnpremul_SkAlphaType:
470 folder.append("_unpremul");
471 break;
scroggoc5560be2016-02-03 09:42:42 -0800472 default:
473 break;
474 }
475
msarett3d9d7a72015-10-21 10:27:10 -0700476 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800477 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700478 }
479
scroggof8dc9df2016-05-16 09:04:13 -0700480 AndroidCodecSrc* src = new AndroidCodecSrc(path, dstColorType, dstAlphaType, sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700481 push_src("image", folder, src);
482}
483
msarett18976312016-03-09 14:20:58 -0800484static void push_image_gen_src(Path path, ImageGenSrc::Mode mode, SkAlphaType alphaType, bool isGpu)
485{
486 SkString folder;
487 switch (mode) {
488 case ImageGenSrc::kCodec_Mode:
489 folder.append("gen_codec");
490 break;
491 case ImageGenSrc::kPlatform_Mode:
492 folder.append("gen_platform");
493 break;
494 }
495
496 if (isGpu) {
497 folder.append("_gpu");
498 } else {
499 switch (alphaType) {
500 case kOpaque_SkAlphaType:
501 folder.append("_opaque");
502 break;
503 case kPremul_SkAlphaType:
504 folder.append("_premul");
505 break;
506 case kUnpremul_SkAlphaType:
507 folder.append("_unpremul");
508 break;
509 default:
510 break;
511 }
512 }
513
514 ImageGenSrc* src = new ImageGenSrc(path, mode, alphaType, isGpu);
515 push_src("image", folder, src);
516}
517
msarett438b2ad2015-04-09 12:43:10 -0700518static void push_codec_srcs(Path path) {
bungeman38d909e2016-08-02 14:40:46 -0700519 sk_sp<SkData> encoded(SkData::MakeFromFileName(path.c_str()));
msarett438b2ad2015-04-09 12:43:10 -0700520 if (!encoded) {
mtkleinc41fd922016-03-08 09:01:39 -0800521 info("Couldn't read %s.", path.c_str());
msarett438b2ad2015-04-09 12:43:10 -0700522 return;
523 }
Ben Wagner145dbcd2016-11-03 14:40:50 -0400524 std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(encoded));
halcanary96fcdcc2015-08-27 07:41:13 -0700525 if (nullptr == codec.get()) {
mtkleinc41fd922016-03-08 09:01:39 -0800526 info("Couldn't create codec for %s.", path.c_str());
msarett438b2ad2015-04-09 12:43:10 -0700527 return;
528 }
529
scroggo9366aff2016-05-20 08:38:54 -0700530 // native scaling is only supported by WEBP and JPEG
531 bool supportsNativeScaling = false;
msarett438b2ad2015-04-09 12:43:10 -0700532
msarett91c22b22016-02-22 12:27:46 -0800533 SkTArray<CodecSrc::Mode> nativeModes;
534 nativeModes.push_back(CodecSrc::kCodec_Mode);
535 nativeModes.push_back(CodecSrc::kCodecZeroInit_Mode);
msarett91c22b22016-02-22 12:27:46 -0800536 switch (codec->getEncodedFormat()) {
Hal Canarydb683012016-11-23 08:55:18 -0700537 case SkEncodedImageFormat::kJPEG:
msarett91c22b22016-02-22 12:27:46 -0800538 nativeModes.push_back(CodecSrc::kScanline_Mode);
539 nativeModes.push_back(CodecSrc::kStripe_Mode);
540 nativeModes.push_back(CodecSrc::kCroppedScanline_Mode);
scroggo9366aff2016-05-20 08:38:54 -0700541 supportsNativeScaling = true;
msarett91c22b22016-02-22 12:27:46 -0800542 break;
Hal Canarydb683012016-11-23 08:55:18 -0700543 case SkEncodedImageFormat::kWEBP:
msarett91c22b22016-02-22 12:27:46 -0800544 nativeModes.push_back(CodecSrc::kSubset_Mode);
scroggo9366aff2016-05-20 08:38:54 -0700545 supportsNativeScaling = true;
msarett91c22b22016-02-22 12:27:46 -0800546 break;
Hal Canarydb683012016-11-23 08:55:18 -0700547 case SkEncodedImageFormat::kDNG:
msarettb65e6042016-02-23 05:37:25 -0800548 break;
msarett91c22b22016-02-22 12:27:46 -0800549 default:
550 nativeModes.push_back(CodecSrc::kScanline_Mode);
msarett91c22b22016-02-22 12:27:46 -0800551 break;
552 }
msarett9e707a02015-09-01 14:57:57 -0700553
msarett91c22b22016-02-22 12:27:46 -0800554 SkTArray<CodecSrc::DstColorType> colorTypes;
555 colorTypes.push_back(CodecSrc::kGetFromCanvas_DstColorType);
msarett34e0ec42016-04-22 16:27:24 -0700556 colorTypes.push_back(CodecSrc::kNonNative8888_Always_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700557 switch (codec->getInfo().colorType()) {
558 case kGray_8_SkColorType:
msarett91c22b22016-02-22 12:27:46 -0800559 colorTypes.push_back(CodecSrc::kGrayscale_Always_DstColorType);
msarett36c37962015-09-02 13:20:52 -0700560 break;
561 default:
msarett36c37962015-09-02 13:20:52 -0700562 break;
563 }
msarett9e707a02015-09-01 14:57:57 -0700564
scroggoc5560be2016-02-03 09:42:42 -0800565 SkTArray<SkAlphaType> alphaModes;
566 alphaModes.push_back(kPremul_SkAlphaType);
scroggof2c96a22016-05-20 11:08:27 -0700567 if (codec->getInfo().alphaType() != kOpaque_SkAlphaType) {
568 alphaModes.push_back(kUnpremul_SkAlphaType);
scroggoc5560be2016-02-03 09:42:42 -0800569 }
msarettb714fb02016-01-22 14:46:42 -0800570
571 for (CodecSrc::Mode mode : nativeModes) {
scroggo9366aff2016-05-20 08:38:54 -0700572 for (CodecSrc::DstColorType colorType : colorTypes) {
573 for (SkAlphaType alphaType : alphaModes) {
scroggof2c96a22016-05-20 11:08:27 -0700574 // Only test kCroppedScanline_Mode when the alpha type is premul. The test is
scroggo9366aff2016-05-20 08:38:54 -0700575 // slow and won't be interestingly different with different alpha types.
576 if (CodecSrc::kCroppedScanline_Mode == mode &&
scroggof2c96a22016-05-20 11:08:27 -0700577 kPremul_SkAlphaType != alphaType) {
scroggo9366aff2016-05-20 08:38:54 -0700578 continue;
579 }
msarett91c22b22016-02-22 12:27:46 -0800580
scroggo9366aff2016-05-20 08:38:54 -0700581 push_codec_src(path, mode, colorType, alphaType, 1.0f);
msarettf3dc1882016-04-26 13:06:38 -0700582
scroggo9366aff2016-05-20 08:38:54 -0700583 // Skip kNonNative on different native scales. It won't be interestingly
584 // different.
585 if (supportsNativeScaling &&
586 CodecSrc::kNonNative8888_Always_DstColorType == colorType) {
587 // Native Scales
588 // SkJpegCodec natively supports scaling to the following:
589 for (auto scale : { 0.125f, 0.25f, 0.375f, 0.5f, 0.625f, 0.750f, 0.875f }) {
590 push_codec_src(path, mode, colorType, alphaType, scale);
591 }
scroggoc5560be2016-02-03 09:42:42 -0800592 }
msarett9e707a02015-09-01 14:57:57 -0700593 }
594 }
msarett0a242972015-06-11 14:27:27 -0700595 }
msarett36c37962015-09-02 13:20:52 -0700596
scroggo19b91532016-10-24 09:03:26 -0700597 {
598 std::vector<SkCodec::FrameInfo> frameInfos = codec->getFrameInfo();
599 if (frameInfos.size() > 1) {
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400600 for (auto dstCT : { CodecSrc::kNonNative8888_Always_DstColorType,
601 CodecSrc::kGetFromCanvas_DstColorType }) {
602 for (auto at : { kUnpremul_SkAlphaType, kPremul_SkAlphaType }) {
603 push_codec_src(path, CodecSrc::kAnimated_Mode, dstCT, at, 1.0f);
604 }
605 }
scroggo19b91532016-10-24 09:03:26 -0700606 }
607
608 }
609
scroggoe4499842016-02-25 11:03:47 -0800610 if (FLAGS_simpleCodec) {
611 return;
612 }
613
msarett3d9d7a72015-10-21 10:27:10 -0700614 const int sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
msarett36c37962015-09-02 13:20:52 -0700615
msarett3d9d7a72015-10-21 10:27:10 -0700616 for (int sampleSize : sampleSizes) {
msarett91c22b22016-02-22 12:27:46 -0800617 for (CodecSrc::DstColorType colorType : colorTypes) {
scroggoc5560be2016-02-03 09:42:42 -0800618 for (SkAlphaType alphaType : alphaModes) {
msarettf3dc1882016-04-26 13:06:38 -0700619 // We can exercise all of the kNonNative support code in the swizzler with just a
620 // few sample sizes. Skip the rest.
621 if (CodecSrc::kNonNative8888_Always_DstColorType == colorType && sampleSize > 3) {
622 continue;
623 }
624
scroggof8dc9df2016-05-16 09:04:13 -0700625 push_android_codec_src(path, colorType, alphaType, sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700626 }
msarett36c37962015-09-02 13:20:52 -0700627 }
628 }
msarett18976312016-03-09 14:20:58 -0800629
630 static const char* const rawExts[] = {
631 "arw", "cr2", "dng", "nef", "nrw", "orf", "raf", "rw2", "pef", "srw",
632 "ARW", "CR2", "DNG", "NEF", "NRW", "ORF", "RAF", "RW2", "PEF", "SRW",
633 };
634
635 // There is not currently a reason to test RAW images on image generator.
636 // If we want to enable these tests, we will need to fix skbug.com/5079.
637 for (const char* ext : rawExts) {
638 if (path.endsWith(ext)) {
639 return;
640 }
641 }
642
643 // Push image generator GPU test.
brianosmanc9ced392016-03-24 07:27:43 -0700644 push_image_gen_src(path, ImageGenSrc::kCodec_Mode, codec->getInfo().alphaType(), true);
msarett18976312016-03-09 14:20:58 -0800645
646 // Push image generator CPU tests.
647 for (SkAlphaType alphaType : alphaModes) {
648 push_image_gen_src(path, ImageGenSrc::kCodec_Mode, alphaType, false);
649
650#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
Hal Canarydb683012016-11-23 08:55:18 -0700651 if (SkEncodedImageFormat::kWEBP != codec->getEncodedFormat() &&
652 SkEncodedImageFormat::kWBMP != codec->getEncodedFormat() &&
msarett18976312016-03-09 14:20:58 -0800653 kUnpremul_SkAlphaType != alphaType)
654 {
655 push_image_gen_src(path, ImageGenSrc::kPlatform_Mode, alphaType, false);
656 }
msarettfc0b6d12016-03-17 13:50:17 -0700657#elif defined(SK_BUILD_FOR_WIN)
Hal Canarydb683012016-11-23 08:55:18 -0700658 if (SkEncodedImageFormat::kWEBP != codec->getEncodedFormat() &&
659 SkEncodedImageFormat::kWBMP != codec->getEncodedFormat())
msarettfc0b6d12016-03-17 13:50:17 -0700660 {
661 push_image_gen_src(path, ImageGenSrc::kPlatform_Mode, alphaType, false);
662 }
msarett18976312016-03-09 14:20:58 -0800663#endif
664 }
msarett438b2ad2015-04-09 12:43:10 -0700665}
666
msarettd1227a72016-05-18 06:23:57 -0700667static void push_brd_src(Path path, CodecSrc::DstColorType dstColorType, BRDSrc::Mode mode,
668 uint32_t sampleSize) {
669 SkString folder("brd_android_codec");
msaretta5783ae2015-09-08 15:35:32 -0700670 switch (mode) {
671 case BRDSrc::kFullImage_Mode:
672 break;
673 case BRDSrc::kDivisor_Mode:
674 folder.append("_divisor");
675 break;
676 default:
677 SkASSERT(false);
678 return;
679 }
680
681 switch (dstColorType) {
682 case CodecSrc::kGetFromCanvas_DstColorType:
683 break;
msaretta5783ae2015-09-08 15:35:32 -0700684 case CodecSrc::kGrayscale_Always_DstColorType:
685 folder.append("_kGray");
686 break;
687 default:
688 SkASSERT(false);
689 return;
690 }
691
692 if (1 != sampleSize) {
msarett4b0778e2015-11-13 09:59:11 -0800693 folder.appendf("_%.3f", 1.0f / (float) sampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700694 }
695
msarettd1227a72016-05-18 06:23:57 -0700696 BRDSrc* src = new BRDSrc(path, mode, dstColorType, sampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700697 push_src("image", folder, src);
698}
699
700static void push_brd_srcs(Path path) {
Leon Scroggins IIIbace7a92017-07-05 11:39:27 -0400701 // Only run grayscale to one sampleSize and Mode. Though interesting
702 // to test grayscale, it should not reveal anything across various
scroggo24a9bd72016-05-20 06:02:42 -0700703 // sampleSizes and Modes
Leon Scroggins IIIbace7a92017-07-05 11:39:27 -0400704 // Arbitrarily choose Mode and sampleSize.
705 push_brd_src(path, CodecSrc::kGrayscale_Always_DstColorType, BRDSrc::kFullImage_Mode, 2);
scroggo24a9bd72016-05-20 06:02:42 -0700706
707
scroggo501b7342015-11-03 07:55:11 -0800708 // Test on a variety of sampleSizes, making sure to include:
709 // - 2, 4, and 8, which are natively supported by jpeg
710 // - multiples of 2 which are not divisible by 4 (analogous for 4)
711 // - larger powers of two, since BRD clients generally use powers of 2
712 // We will only produce output for the larger sizes on large images.
713 const uint32_t sampleSizes[] = { 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 24, 32, 64 };
msarett6efbe052015-09-11 09:01:16 -0700714
msaretta5783ae2015-09-08 15:35:32 -0700715 const BRDSrc::Mode modes[] = {
msarett26ad17b2015-10-22 07:29:19 -0700716 BRDSrc::kFullImage_Mode,
717 BRDSrc::kDivisor_Mode,
msaretta5783ae2015-09-08 15:35:32 -0700718 };
719
msarettd1227a72016-05-18 06:23:57 -0700720 for (uint32_t sampleSize : sampleSizes) {
scroggo24a9bd72016-05-20 06:02:42 -0700721 for (BRDSrc::Mode mode : modes) {
722 push_brd_src(path, CodecSrc::kGetFromCanvas_DstColorType, mode, sampleSize);
msaretta5783ae2015-09-08 15:35:32 -0700723 }
724 }
725}
726
727static bool brd_supported(const char* ext) {
msarett8c8f22a2015-04-01 06:58:48 -0700728 static const char* const exts[] = {
msaretta5783ae2015-09-08 15:35:32 -0700729 "jpg", "jpeg", "png", "webp",
730 "JPG", "JPEG", "PNG", "WEBP",
msarett8c8f22a2015-04-01 06:58:48 -0700731 };
732
733 for (uint32_t i = 0; i < SK_ARRAY_COUNT(exts); i++) {
734 if (0 == strcmp(exts[i], ext)) {
735 return true;
736 }
737 }
738 return false;
scroggo9b77ddd2015-03-19 06:03:39 -0700739}
740
fmalitaa2b9fdf2016-08-03 19:53:36 -0700741template <typename T>
742void gather_file_srcs(const SkCommandLineFlags::StringArray& flags, const char* ext) {
743 for (int i = 0; i < flags.count(); i++) {
744 const char* path = flags[i];
745 if (sk_isdir(path)) {
746 SkOSFile::Iter it(path, ext);
747 for (SkString file; it.next(&file); ) {
748 push_src(ext, "", new T(SkOSPath::Join(path, file.c_str())));
749 }
750 } else {
751 push_src(ext, "", new T(path));
752 }
753 }
754}
755
scroggo86737142016-02-03 12:19:11 -0800756static bool gather_srcs() {
mtklein748ca3b2015-01-15 10:56:12 -0800757 for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) {
djsollen54416de2015-04-03 07:24:48 -0700758 push_src("gm", "", new GMSrc(r->factory()));
mtklein748ca3b2015-01-15 10:56:12 -0800759 }
scroggo86737142016-02-03 12:19:11 -0800760
fmalitaa2b9fdf2016-08-03 19:53:36 -0700761 gather_file_srcs<SKPSrc>(FLAGS_skps, "skp");
762 gather_file_srcs<MSKPSrc>(FLAGS_mskps, "mskp");
763#if defined(SK_XML)
764 gather_file_srcs<SVGSrc>(FLAGS_svgs, "svg");
765#endif
halcanary45420a92016-06-02 12:41:14 -0700766
scroggo86737142016-02-03 12:19:11 -0800767 SkTArray<SkString> images;
msarett69deca82016-04-29 09:38:40 -0700768 if (!CollectImages(FLAGS_images, &images)) {
scroggo86737142016-02-03 12:19:11 -0800769 return false;
770 }
771
772 for (auto image : images) {
773 push_codec_srcs(image);
scroggoe4499842016-02-25 11:03:47 -0800774 if (FLAGS_simpleCodec) {
775 continue;
776 }
777
mtklein21eaf3b2016-02-08 12:39:59 -0800778 const char* ext = strrchr(image.c_str(), '.');
779 if (ext && brd_supported(ext+1)) {
scroggo86737142016-02-03 12:19:11 -0800780 push_brd_srcs(image);
mtklein709d2c32015-01-15 08:30:25 -0800781 }
mtklein709d2c32015-01-15 08:30:25 -0800782 }
scroggo86737142016-02-03 12:19:11 -0800783
msarett69deca82016-04-29 09:38:40 -0700784 SkTArray<SkString> colorImages;
785 if (!CollectImages(FLAGS_colorImages, &colorImages)) {
786 return false;
787 }
788
789 for (auto colorImage : colorImages) {
msarett9ce3a542016-07-15 13:54:38 -0700790 ColorCodecSrc* src = new ColorCodecSrc(colorImage, ColorCodecSrc::kBaseline_Mode,
791 kN32_SkColorType);
msarettb8d1aac2016-08-04 12:22:06 -0700792 push_src("colorImage", "color_codec_baseline", src);
msarett888dc162016-05-23 10:21:17 -0700793
msarett9ce3a542016-07-15 13:54:38 -0700794 src = new ColorCodecSrc(colorImage, ColorCodecSrc::kDst_HPZR30w_Mode, kN32_SkColorType);
msarettb8d1aac2016-08-04 12:22:06 -0700795 push_src("colorImage", "color_codec_HPZR30w", src);
msarett9ce3a542016-07-15 13:54:38 -0700796 // TODO (msarett):
797 // 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 -0700798
msarett9ce3a542016-07-15 13:54:38 -0700799 src = new ColorCodecSrc(colorImage, ColorCodecSrc::kDst_sRGB_Mode, kN32_SkColorType);
msarettb8d1aac2016-08-04 12:22:06 -0700800 push_src("colorImage", "color_codec_sRGB_kN32", src);
msarett9ce3a542016-07-15 13:54:38 -0700801 src = new ColorCodecSrc(colorImage, ColorCodecSrc::kDst_sRGB_Mode, kRGBA_F16_SkColorType);
msarettb8d1aac2016-08-04 12:22:06 -0700802 push_src("colorImage", "color_codec_sRGB_kF16", src);
msarett69deca82016-04-29 09:38:40 -0700803 }
804
scroggo86737142016-02-03 12:19:11 -0800805 return true;
mtklein709d2c32015-01-15 08:30:25 -0800806}
807
kkinnunen3e980c32015-12-23 01:33:00 -0800808static void push_sink(const SkCommandLineConfig& config, Sink* s) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400809 std::unique_ptr<Sink> sink(s);
kkinnunen3e980c32015-12-23 01:33:00 -0800810
mtkleine0effd62015-07-29 06:37:28 -0700811 // Try a simple Src as a canary. If it fails, skip this sink.
mtklein748ca3b2015-01-15 10:56:12 -0800812 struct : public Src {
mtkleine0effd62015-07-29 06:37:28 -0700813 Error draw(SkCanvas* c) const override {
814 c->drawRect(SkRect::MakeWH(1,1), SkPaint());
815 return "";
816 }
mtklein36352bf2015-03-25 18:17:31 -0700817 SkISize size() const override { return SkISize::Make(16, 16); }
mtkleine0effd62015-07-29 06:37:28 -0700818 Name name() const override { return "justOneRect"; }
819 } justOneRect;
mtklein114c3cd2015-01-15 10:15:02 -0800820
mtklein748ca3b2015-01-15 10:56:12 -0800821 SkBitmap bitmap;
822 SkDynamicMemoryWStream stream;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800823 SkString log;
mtkleine0effd62015-07-29 06:37:28 -0700824 Error err = sink->draw(justOneRect, &bitmap, &stream, &log);
mtklein4089ef72015-03-05 08:40:28 -0800825 if (err.isFatal()) {
mtkleinc41fd922016-03-08 09:01:39 -0800826 info("Could not run %s: %s\n", config.getTag().c_str(), err.c_str());
mtklein05641a52015-04-21 10:49:13 -0700827 exit(1);
mtklein114c3cd2015-01-15 10:15:02 -0800828 }
829
mtkleine0effd62015-07-29 06:37:28 -0700830 TaggedSink& ts = gSinks.push_back();
mtklein18300a32016-03-16 13:53:35 -0700831 ts.reset(sink.release());
kkinnunen3e980c32015-12-23 01:33:00 -0800832 ts.tag = config.getTag();
mtklein748ca3b2015-01-15 10:56:12 -0800833}
834
835static bool gpu_supported() {
836#if SK_SUPPORT_GPU
837 return FLAGS_gpu;
838#else
839 return false;
840#endif
841}
kkinnunen9ebc3f02015-12-21 23:48:13 -0800842
csmartdalton008b9d82017-02-22 12:00:42 -0700843static Sink* create_sink(const GrContextOptions& grCtxOptions, const SkCommandLineConfig* config) {
kkinnunen3e980c32015-12-23 01:33:00 -0800844#if SK_SUPPORT_GPU
845 if (gpu_supported()) {
846 if (const SkCommandLineConfigGpu* gpuConfig = config->asConfigGpu()) {
bsalomon85b4b532016-04-05 11:06:27 -0700847 GrContextFactory::ContextType contextType = gpuConfig->getContextType();
csmartdaltone812d492017-02-21 12:36:05 -0700848 GrContextFactory::ContextOverrides contextOverrides = gpuConfig->getContextOverrides();
csmartdalton008b9d82017-02-22 12:00:42 -0700849 GrContextFactory testFactory(grCtxOptions);
csmartdaltone812d492017-02-21 12:36:05 -0700850 if (!testFactory.get(contextType, contextOverrides)) {
mtkleinc41fd922016-03-08 09:01:39 -0800851 info("WARNING: can not create GPU context for config '%s'. "
852 "GM tests will be skipped.\n", gpuConfig->getTag().c_str());
kkinnunen3e980c32015-12-23 01:33:00 -0800853 return nullptr;
854 }
csmartdaltone812d492017-02-21 12:36:05 -0700855 return new GPUSink(contextType, contextOverrides, gpuConfig->getSamples(),
brianosmand93c1202016-03-10 07:49:08 -0800856 gpuConfig->getUseDIText(), gpuConfig->getColorType(),
Brian Salomonce5ee602017-07-17 11:31:31 -0400857 gpuConfig->getAlphaType(), sk_ref_sp(gpuConfig->getColorSpace()),
858 FLAGS_gpu_threading);
kkinnunen3e980c32015-12-23 01:33:00 -0800859 }
860 }
861#endif
862
863#define SINK(t, sink, ...) if (config->getBackend().equals(t)) { return new sink(__VA_ARGS__); }
864
mtklein748ca3b2015-01-15 10:56:12 -0800865 if (FLAGS_cpu) {
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500866 auto srgbColorSpace = SkColorSpace::MakeSRGB();
867 auto srgbLinearColorSpace = SkColorSpace::MakeSRGBLinear();
brianosmanb109b8c2016-06-16 13:03:24 -0700868
Matt Sarettd2228302017-03-02 08:53:46 -0500869 SINK("565", RasterSink, kRGB_565_SkColorType);
870 SINK("8888", RasterSink, kN32_SkColorType);
871 SINK("srgb", RasterSink, kN32_SkColorType, srgbColorSpace);
Matt Sarettd2228302017-03-02 08:53:46 -0500872 SINK("f16", RasterSink, kRGBA_F16_SkColorType, srgbLinearColorSpace);
873 SINK("pdf", PDFSink);
874 SINK("skp", SKPSink);
875 SINK("pipe", PipeSink);
876 SINK("svg", SVGSink);
877 SINK("null", NullSink);
878 SINK("xps", XPSSink);
879 SINK("pdfa", PDFSink, true);
Hal Canary85c7fe82016-10-25 10:33:27 -0400880 SINK("jsdebug", DebugSink);
mtklein748ca3b2015-01-15 10:56:12 -0800881 }
882#undef SINK
halcanary96fcdcc2015-08-27 07:41:13 -0700883 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800884}
885
Mike Klein841101d2017-03-10 09:55:51 -0500886static sk_sp<SkColorSpace> adobe_rgb() {
887 return SkColorSpace::MakeRGB(SkColorSpace::kSRGB_RenderTargetGamma,
888 SkColorSpace::kAdobeRGB_Gamut);
889}
890
Mike Klein919cc452017-03-18 15:36:52 +0000891static sk_sp<SkColorSpace> rgb_to_gbr() {
892 float gbr[9];
893 gbr[0] = gSRGB_toXYZD50[1];
894 gbr[1] = gSRGB_toXYZD50[2];
895 gbr[2] = gSRGB_toXYZD50[0];
896 gbr[3] = gSRGB_toXYZD50[4];
897 gbr[4] = gSRGB_toXYZD50[5];
898 gbr[5] = gSRGB_toXYZD50[3];
899 gbr[6] = gSRGB_toXYZD50[7];
900 gbr[7] = gSRGB_toXYZD50[8];
901 gbr[8] = gSRGB_toXYZD50[6];
902 SkMatrix44 toXYZD50(SkMatrix44::kUninitialized_Constructor);
903 toXYZD50.set3x3RowMajorf(gbr);
904 return SkColorSpace::MakeRGB(SkColorSpace::kSRGB_RenderTargetGamma, toXYZD50);
905}
906
kkinnunen3e980c32015-12-23 01:33:00 -0800907static Sink* create_via(const SkString& tag, Sink* wrapped) {
908#define VIA(t, via, ...) if (tag.equals(t)) { return new via(__VA_ARGS__); }
Mike Klein919cc452017-03-18 15:36:52 +0000909 VIA("adobe", ViaCSXform, wrapped, adobe_rgb(), false);
910 VIA("gbr", ViaCSXform, wrapped, rgb_to_gbr(), true);
mtklein9c5052f2016-08-06 12:51:51 -0700911 VIA("lite", ViaLite, wrapped);
reed54dc4872016-09-13 08:09:45 -0700912 VIA("pipe", ViaPipe, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700913 VIA("twice", ViaTwice, wrapped);
Mike Reedbae888e2017-02-18 16:50:45 -0500914#ifdef TEST_VIA_SVG
Mike Reedf67c4592017-02-17 17:06:11 -0500915 VIA("svg", ViaSVG, wrapped);
Mike Reedbae888e2017-02-18 16:50:45 -0500916#endif
halcanary96fcdcc2015-08-27 07:41:13 -0700917 VIA("serialize", ViaSerialization, wrapped);
mtklein4a34ecb2016-01-08 10:19:35 -0800918 VIA("pic", ViaPicture, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700919 VIA("2ndpic", ViaSecondPicture, wrapped);
mtkleind31c13d2015-05-05 12:59:56 -0700920 VIA("sp", ViaSingletonPictures, wrapped);
reedbabc3de2016-07-08 08:43:27 -0700921 VIA("defer", ViaDefer, wrapped);
halcanary96fcdcc2015-08-27 07:41:13 -0700922 VIA("tiles", ViaTiles, 256, 256, nullptr, wrapped);
mtklein7edca212015-01-21 13:18:51 -0800923 VIA("tiles_rt", ViaTiles, 256, 256, new SkRTreeFactory, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800924
mtkleind603b222015-02-17 11:13:33 -0800925 if (FLAGS_matrix.count() == 4) {
mtklein748ca3b2015-01-15 10:56:12 -0800926 SkMatrix m;
mtkleind603b222015-02-17 11:13:33 -0800927 m.reset();
928 m.setScaleX((SkScalar)atof(FLAGS_matrix[0]));
929 m.setSkewX ((SkScalar)atof(FLAGS_matrix[1]));
930 m.setSkewY ((SkScalar)atof(FLAGS_matrix[2]));
931 m.setScaleY((SkScalar)atof(FLAGS_matrix[3]));
932 VIA("matrix", ViaMatrix, m, wrapped);
933 VIA("upright", ViaUpright, m, wrapped);
mtklein748ca3b2015-01-15 10:56:12 -0800934 }
tomhudson64de1e12015-03-05 08:01:07 -0800935
mtklein748ca3b2015-01-15 10:56:12 -0800936#undef VIA
halcanary96fcdcc2015-08-27 07:41:13 -0700937 return nullptr;
mtklein114c3cd2015-01-15 10:15:02 -0800938}
939
csmartdalton008b9d82017-02-22 12:00:42 -0700940static bool gather_sinks(const GrContextOptions& grCtxOptions) {
kkinnunen3e980c32015-12-23 01:33:00 -0800941 SkCommandLineConfigArray configs;
942 ParseConfigs(FLAGS_config, &configs);
943 for (int i = 0; i < configs.count(); i++) {
944 const SkCommandLineConfig& config = *configs[i];
csmartdalton008b9d82017-02-22 12:00:42 -0700945 Sink* sink = create_sink(grCtxOptions, &config);
kkinnunen3e980c32015-12-23 01:33:00 -0800946 if (sink == nullptr) {
mtkleinc41fd922016-03-08 09:01:39 -0800947 info("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
948 config.getTag().c_str());
kkinnunen3e980c32015-12-23 01:33:00 -0800949 continue;
950 }
mtklein748ca3b2015-01-15 10:56:12 -0800951
kkinnunen3e980c32015-12-23 01:33:00 -0800952 const SkTArray<SkString>& parts = config.getViaParts();
953 for (int j = parts.count(); j-- > 0;) {
954 const SkString& part = parts[j];
955 Sink* next = create_via(part, sink);
halcanary96fcdcc2015-08-27 07:41:13 -0700956 if (next == nullptr) {
mtkleinc41fd922016-03-08 09:01:39 -0800957 info("Skipping config %s: Don't understand '%s'.\n", config.getTag().c_str(),
958 part.c_str());
mtklein748ca3b2015-01-15 10:56:12 -0800959 delete sink;
halcanary96fcdcc2015-08-27 07:41:13 -0700960 sink = nullptr;
mtklein748ca3b2015-01-15 10:56:12 -0800961 break;
962 }
963 sink = next;
964 }
965 if (sink) {
966 push_sink(config, sink);
mtklein114c3cd2015-01-15 10:15:02 -0800967 }
968 }
brianosman05c987c2016-05-09 13:42:16 -0700969
970 // If no configs were requested (just running tests, perhaps?), then we're okay.
971 // Otherwise, make sure that at least one sink was constructed correctly. This catches
972 // the case of bots without a GPU being assigned GPU configs.
973 return (configs.count() == 0) || (gSinks.count() > 0);
mtklein114c3cd2015-01-15 10:15:02 -0800974}
mtklein709d2c32015-01-15 08:30:25 -0800975
mtkleina5114d72015-08-24 13:27:01 -0700976static bool dump_png(SkBitmap bitmap, const char* path, const char* md5) {
Mike Klein13b6afd2017-06-12 12:45:41 -0400977 SkPixmap pm;
978 if (!bitmap.peekPixels(&pm)) {
979 return false; // Ought to never happen... we're already read-back at this point.
mtkleina5114d72015-08-24 13:27:01 -0700980 }
Mike Klein13b6afd2017-06-12 12:45:41 -0400981 SkFILEWStream dst{path};
mtkleina5114d72015-08-24 13:27:01 -0700982
mtkleind69ece62015-09-10 10:37:44 -0700983 SkString description;
984 description.append("Key: ");
985 for (int i = 0; i < FLAGS_key.count(); i++) {
986 description.appendf("%s ", FLAGS_key[i]);
987 }
988 description.append("Properties: ");
989 for (int i = 0; i < FLAGS_properties.count(); i++) {
990 description.appendf("%s ", FLAGS_properties[i]);
991 }
992 description.appendf("MD5: %s", md5);
993
Mike Klein13b6afd2017-06-12 12:45:41 -0400994 const char* comments[] = {
995 "Author", "DM dump_png()",
996 "Description", description.c_str(),
997 };
998 size_t lengths[] = {
999 strlen(comments[0])+1, strlen(comments[1])+1,
1000 strlen(comments[2])+1, strlen(comments[3])+1,
1001 };
mtkleina5114d72015-08-24 13:27:01 -07001002
Mike Klein13b6afd2017-06-12 12:45:41 -04001003 SkPngEncoder::Options options;
1004 options.fComments = SkDataTable::MakeCopyArrays((const void**)comments, lengths, 4);
1005 options.fFilterFlags = SkPngEncoder::FilterFlag::kNone;
1006 options.fZLibLevel = 1;
1007 options.fUnpremulBehavior = pm.colorSpace() ? SkTransferFunctionBehavior::kRespect
1008 : SkTransferFunctionBehavior::kIgnore;
1009 return SkPngEncoder::Encode(&dst, pm, options);
mtkleina5114d72015-08-24 13:27:01 -07001010}
1011
mtkleina2ef6422015-01-15 13:44:22 -08001012static bool match(const char* needle, const char* haystack) {
halcanary96fcdcc2015-08-27 07:41:13 -07001013 return 0 == strcmp("_", needle) || nullptr != strstr(haystack, needle);
mtkleina2ef6422015-01-15 13:44:22 -08001014}
1015
mtklein3eed7dd2016-02-24 19:07:07 -08001016static bool is_blacklisted(const char* sink, const char* src,
1017 const char* srcOptions, const char* name) {
mtklein0d243ff2015-04-03 07:51:00 -07001018 for (int i = 0; i < FLAGS_blacklist.count() - 3; i += 4) {
mtkleina2ef6422015-01-15 13:44:22 -08001019 if (match(FLAGS_blacklist[i+0], sink) &&
djsollen54416de2015-04-03 07:24:48 -07001020 match(FLAGS_blacklist[i+1], src) &&
1021 match(FLAGS_blacklist[i+2], srcOptions) &&
1022 match(FLAGS_blacklist[i+3], name)) {
mtklein3eed7dd2016-02-24 19:07:07 -08001023 return true;
mtkleina2ef6422015-01-15 13:44:22 -08001024 }
1025 }
mtklein3eed7dd2016-02-24 19:07:07 -08001026 return false;
mtkleina2ef6422015-01-15 13:44:22 -08001027}
1028
mtkleincd50bca2016-01-05 06:20:20 -08001029// Even when a Task Sink reports to be non-threadsafe (e.g. GPU), we know things like
1030// .png encoding are definitely thread safe. This lets us offload that work to CPU threads.
1031static SkTaskGroup gDefinitelyThreadSafeWork;
1032
mtklein748ca3b2015-01-15 10:56:12 -08001033// The finest-grained unit of work we can run: draw a single Src into a single Sink,
1034// report any errors, and perhaps write out the output: a .png of the bitmap, or a raw stream.
1035struct Task {
mtkleine0effd62015-07-29 06:37:28 -07001036 Task(const TaggedSrc& src, const TaggedSink& sink) : src(src), sink(sink) {}
1037 const TaggedSrc& src;
1038 const TaggedSink& sink;
mtklein748ca3b2015-01-15 10:56:12 -08001039
mtklein21eaf3b2016-02-08 12:39:59 -08001040 static void Run(const Task& task) {
1041 SkString name = task.src->name();
mtkleine0effd62015-07-29 06:37:28 -07001042
mtkleinb9eb4ac2015-02-02 18:26:03 -08001043 SkString log;
mtklein3eed7dd2016-02-24 19:07:07 -08001044 if (!FLAGS_dryRun) {
mtklein748ca3b2015-01-15 10:56:12 -08001045 SkBitmap bitmap;
1046 SkDynamicMemoryWStream stream;
mtklein3eed7dd2016-02-24 19:07:07 -08001047 start(task.sink.tag.c_str(), task.src.tag.c_str(),
1048 task.src.options.c_str(), name.c_str());
mtklein21eaf3b2016-02-08 12:39:59 -08001049 Error err = task.sink->draw(*task.src, &bitmap, &stream, &log);
mtkleinb3b13b72016-03-07 13:20:52 -08001050 if (!log.isEmpty()) {
mtkleinc41fd922016-03-08 09:01:39 -08001051 info("%s %s %s %s:\n%s\n", task.sink.tag.c_str()
1052 , task.src.tag.c_str()
1053 , task.src.options.c_str()
1054 , name.c_str()
1055 , log.c_str());
mtkleinb3b13b72016-03-07 13:20:52 -08001056 }
mtklein748ca3b2015-01-15 10:56:12 -08001057 if (!err.isEmpty()) {
mtklein4089ef72015-03-05 08:40:28 -08001058 if (err.isFatal()) {
djsollen54416de2015-04-03 07:24:48 -07001059 fail(SkStringPrintf("%s %s %s %s: %s",
mtklein21eaf3b2016-02-08 12:39:59 -08001060 task.sink.tag.c_str(),
1061 task.src.tag.c_str(),
1062 task.src.options.c_str(),
mtklein4089ef72015-03-05 08:40:28 -08001063 name.c_str(),
1064 err.c_str()));
mtkleinb37cb412015-03-18 05:27:14 -07001065 } else {
mtklein3eed7dd2016-02-24 19:07:07 -08001066 done(task.sink.tag.c_str(), task.src.tag.c_str(),
1067 task.src.options.c_str(), name.c_str());
mtkleinba6ada72016-01-21 09:39:35 -08001068 return;
mtklein4089ef72015-03-05 08:40:28 -08001069 }
mtklein748ca3b2015-01-15 10:56:12 -08001070 }
mtklein62bd1a62015-01-27 14:46:26 -08001071
mtkleincd50bca2016-01-05 06:20:20 -08001072 // We're likely switching threads here, so we must capture by value, [=] or [foo,bar].
Hal Canary0b833192017-03-14 14:35:02 -04001073 SkStreamAsset* data = stream.detachAsStream().release();
mtkleincd50bca2016-01-05 06:20:20 -08001074 gDefinitelyThreadSafeWork.add([task,name,bitmap,data]{
Ben Wagner145dbcd2016-11-03 14:40:50 -04001075 std::unique_ptr<SkStreamAsset> ownedData(data);
mtkleincd50bca2016-01-05 06:20:20 -08001076
mtkleincd50bca2016-01-05 06:20:20 -08001077 SkString md5;
1078 if (!FLAGS_writePath.isEmpty() || !FLAGS_readPath.isEmpty()) {
1079 SkMD5 hash;
1080 if (data->getLength()) {
1081 hash.writeStream(data, data->getLength());
1082 data->rewind();
mtklein38408462015-07-08 07:25:27 -07001083 } else {
mtkleincd50bca2016-01-05 06:20:20 -08001084 // If we're BGRA (Linux, Windows), swizzle over to RGBA (Mac, Android).
1085 // This helps eliminate multiple 0-pixel-diff hashes on gold.skia.org.
1086 // (Android's general slow speed breaks the tie arbitrarily in RGBA's favor.)
1087 // We might consider promoting 565 to RGBA too.
1088 if (bitmap.colorType() == kBGRA_8888_SkColorType) {
1089 SkBitmap swizzle;
Matt Sarett68b8e3d2017-04-28 11:15:22 -04001090 SkAssertResult(sk_tool_utils::copy_to(&swizzle, kRGBA_8888_SkColorType,
1091 bitmap));
mtkleincd50bca2016-01-05 06:20:20 -08001092 hash.write(swizzle.getPixels(), swizzle.getSize());
1093 } else {
1094 hash.write(bitmap.getPixels(), bitmap.getSize());
1095 }
1096 }
1097 SkMD5::Digest digest;
1098 hash.finish(digest);
1099 for (int i = 0; i < 16; i++) {
1100 md5.appendf("%02x", digest.data[i]);
mtklein38408462015-07-08 07:25:27 -07001101 }
mtklein62bd1a62015-01-27 14:46:26 -08001102 }
mtklein62bd1a62015-01-27 14:46:26 -08001103
mtkleincd50bca2016-01-05 06:20:20 -08001104 if (!FLAGS_readPath.isEmpty() &&
mtklein3eed7dd2016-02-24 19:07:07 -08001105 !gGold.contains(Gold(task.sink.tag, task.src.tag,
1106 task.src.options, name, md5))) {
mtkleincd50bca2016-01-05 06:20:20 -08001107 fail(SkStringPrintf("%s not found for %s %s %s %s in %s",
1108 md5.c_str(),
mtklein21eaf3b2016-02-08 12:39:59 -08001109 task.sink.tag.c_str(),
1110 task.src.tag.c_str(),
1111 task.src.options.c_str(),
mtkleincd50bca2016-01-05 06:20:20 -08001112 name.c_str(),
1113 FLAGS_readPath[0]));
mtklein748ca3b2015-01-15 10:56:12 -08001114 }
mtkleincd50bca2016-01-05 06:20:20 -08001115
1116 if (!FLAGS_writePath.isEmpty()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001117 const char* ext = task.sink->fileExtension();
mtkleincd50bca2016-01-05 06:20:20 -08001118 if (data->getLength()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001119 WriteToDisk(task, md5, ext, data, data->getLength(), nullptr);
mtkleincd50bca2016-01-05 06:20:20 -08001120 SkASSERT(bitmap.drawsNothing());
1121 } else if (!bitmap.drawsNothing()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001122 WriteToDisk(task, md5, ext, nullptr, 0, &bitmap);
mtkleincd50bca2016-01-05 06:20:20 -08001123 }
1124 }
1125 });
mtklein748ca3b2015-01-15 10:56:12 -08001126 }
mtklein3eed7dd2016-02-24 19:07:07 -08001127 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 -08001128 }
1129
1130 static void WriteToDisk(const Task& task,
mtklein62bd1a62015-01-27 14:46:26 -08001131 SkString md5,
1132 const char* ext,
mtklein748ca3b2015-01-15 10:56:12 -08001133 SkStream* data, size_t len,
mtklein62bd1a62015-01-27 14:46:26 -08001134 const SkBitmap* bitmap) {
mtklein409d4702016-02-29 07:38:01 -08001135 bool gammaCorrect = false;
1136 if (bitmap) {
Brian Osman7039f742016-11-01 15:56:16 -04001137 gammaCorrect = SkToBool(bitmap->info().colorSpace());
mtklein409d4702016-02-29 07:38:01 -08001138 }
1139
mtklein748ca3b2015-01-15 10:56:12 -08001140 JsonWriter::BitmapResult result;
djsollen54416de2015-04-03 07:24:48 -07001141 result.name = task.src->name();
mtklein3eed7dd2016-02-24 19:07:07 -08001142 result.config = task.sink.tag;
djsollen54416de2015-04-03 07:24:48 -07001143 result.sourceType = task.src.tag;
1144 result.sourceOptions = task.src.options;
1145 result.ext = ext;
mtklein409d4702016-02-29 07:38:01 -08001146 result.gammaCorrect = gammaCorrect;
djsollen54416de2015-04-03 07:24:48 -07001147 result.md5 = md5;
mtklein748ca3b2015-01-15 10:56:12 -08001148 JsonWriter::AddBitmapResult(result);
1149
mtkleinb0531a72015-04-07 13:38:48 -07001150 // If an MD5 is uninteresting, we want it noted in the JSON file,
1151 // but don't want to dump it out as a .png (or whatever ext is).
1152 if (gUninterestingHashes.contains(md5)) {
1153 return;
1154 }
1155
mtklein748ca3b2015-01-15 10:56:12 -08001156 const char* dir = FLAGS_writePath[0];
1157 if (0 == strcmp(dir, "@")) { // Needed for iOS.
1158 dir = FLAGS_resourcePath[0];
1159 }
1160 sk_mkdir(dir);
1161
1162 SkString path;
1163 if (FLAGS_nameByHash) {
1164 path = SkOSPath::Join(dir, result.md5.c_str());
1165 path.append(".");
1166 path.append(ext);
1167 if (sk_exists(path.c_str())) {
1168 return; // Content-addressed. If it exists already, we're done.
1169 }
1170 } else {
kkinnunen3e980c32015-12-23 01:33:00 -08001171 path = SkOSPath::Join(dir, task.sink.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001172 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001173 path = SkOSPath::Join(path.c_str(), task.src.tag.c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001174 sk_mkdir(path.c_str());
msarett9e707a02015-09-01 14:57:57 -07001175 if (strcmp(task.src.options.c_str(), "") != 0) {
1176 path = SkOSPath::Join(path.c_str(), task.src.options.c_str());
djsollen54416de2015-04-03 07:24:48 -07001177 sk_mkdir(path.c_str());
1178 }
mtklein748ca3b2015-01-15 10:56:12 -08001179 path = SkOSPath::Join(path.c_str(), task.src->name().c_str());
1180 path.append(".");
1181 path.append(ext);
1182 }
1183
mtklein748ca3b2015-01-15 10:56:12 -08001184 if (bitmap) {
mtkleina5114d72015-08-24 13:27:01 -07001185 if (!dump_png(*bitmap, path.c_str(), result.md5.c_str())) {
mtklein748ca3b2015-01-15 10:56:12 -08001186 fail(SkStringPrintf("Can't encode PNG to %s.\n", path.c_str()));
1187 return;
1188 }
1189 } else {
mtkleina5114d72015-08-24 13:27:01 -07001190 SkFILEWStream file(path.c_str());
1191 if (!file.isValid()) {
1192 fail(SkStringPrintf("Can't open %s for writing.\n", path.c_str()));
1193 return;
1194 }
mtklein748ca3b2015-01-15 10:56:12 -08001195 if (!file.writeStream(data, len)) {
1196 fail(SkStringPrintf("Can't write to %s.\n", path.c_str()));
1197 return;
1198 }
1199 }
1200 }
1201};
1202
mtklein748ca3b2015-01-15 10:56:12 -08001203/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1204
1205// Unit tests don't fit so well into the Src/Sink model, so we give them special treatment.
1206
mtklein21eaf3b2016-02-08 12:39:59 -08001207static SkTDArray<skiatest::Test> gParallelTests, gSerialTests;
mtklein748ca3b2015-01-15 10:56:12 -08001208
1209static void gather_tests() {
mtklein6c5fed22015-01-20 13:47:23 -08001210 if (!FLAGS_src.contains("tests")) {
mtklein748ca3b2015-01-15 10:56:12 -08001211 return;
1212 }
mtklein6393c062015-04-27 08:45:01 -07001213 for (const skiatest::TestRegistry* r = skiatest::TestRegistry::Head(); r; r = r->next()) {
1214 if (!in_shard()) {
1215 continue;
1216 }
halcanary87f3ba42015-01-20 09:30:20 -08001217 // Despite its name, factory() is returning a reference to
1218 // link-time static const POD data.
1219 const skiatest::Test& test = r->factory();
1220 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test.name)) {
mtklein748ca3b2015-01-15 10:56:12 -08001221 continue;
1222 }
halcanary87f3ba42015-01-20 09:30:20 -08001223 if (test.needsGpu && gpu_supported()) {
mtklein21eaf3b2016-02-08 12:39:59 -08001224 (FLAGS_gpu_threading ? gParallelTests : gSerialTests).push(test);
halcanary87f3ba42015-01-20 09:30:20 -08001225 } else if (!test.needsGpu && FLAGS_cpu) {
mtklein21eaf3b2016-02-08 12:39:59 -08001226 gParallelTests.push(test);
mtklein82d28432015-01-15 12:46:02 -08001227 }
mtklein748ca3b2015-01-15 10:56:12 -08001228 }
1229}
1230
csmartdalton008b9d82017-02-22 12:00:42 -07001231static void run_test(skiatest::Test test, const GrContextOptions& grCtxOptions) {
halcanary87f3ba42015-01-20 09:30:20 -08001232 struct : public skiatest::Reporter {
mtklein36352bf2015-03-25 18:17:31 -07001233 void reportFailed(const skiatest::Failure& failure) override {
halcanary87f3ba42015-01-20 09:30:20 -08001234 fail(failure.toString());
1235 JsonWriter::AddTestFailure(failure);
1236 }
mtklein36352bf2015-03-25 18:17:31 -07001237 bool allowExtendedTest() const override {
halcanary87f3ba42015-01-20 09:30:20 -08001238 return FLAGS_pathOpsExtended;
1239 }
mtklein36352bf2015-03-25 18:17:31 -07001240 bool verbose() const override { return FLAGS_veryVerbose; }
halcanary87f3ba42015-01-20 09:30:20 -08001241 } reporter;
djsollen824996a2015-06-12 12:06:22 -07001242
mtklein3eed7dd2016-02-24 19:07:07 -08001243 if (!FLAGS_dryRun && !is_blacklisted("_", "tests", "_", test.name)) {
mtklein21eaf3b2016-02-08 12:39:59 -08001244 start("unit", "test", "", test.name);
csmartdalton008b9d82017-02-22 12:00:42 -07001245 GrContextFactory factory(grCtxOptions);
mtklein21eaf3b2016-02-08 12:39:59 -08001246 test.proc(&reporter, &factory);
mtklein748ca3b2015-01-15 10:56:12 -08001247 }
mtklein3eed7dd2016-02-24 19:07:07 -08001248 done("unit", "test", "", test.name);
mtklein748ca3b2015-01-15 10:56:12 -08001249}
1250
1251/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
1252
caryclark83ca6282015-06-10 09:31:09 -07001253#define PORTABLE_FONT_PREFIX "Toy Liberation "
1254
mbocee6a9912016-05-31 11:42:36 -07001255static sk_sp<SkTypeface> create_from_name(const char familyName[], SkFontStyle style) {
caryclark83ca6282015-06-10 09:31:09 -07001256 if (familyName && strlen(familyName) > sizeof(PORTABLE_FONT_PREFIX)
1257 && !strncmp(familyName, PORTABLE_FONT_PREFIX, sizeof(PORTABLE_FONT_PREFIX) - 1)) {
caryclark1818acb2015-07-24 12:09:25 -07001258 return sk_tool_utils::create_portable_typeface(familyName, style);
caryclark83ca6282015-06-10 09:31:09 -07001259 }
halcanary96fcdcc2015-08-27 07:41:13 -07001260 return nullptr;
caryclark83ca6282015-06-10 09:31:09 -07001261}
1262
1263#undef PORTABLE_FONT_PREFIX
1264
mbocee6a9912016-05-31 11:42:36 -07001265extern sk_sp<SkTypeface> (*gCreateTypefaceDelegate)(const char [], SkFontStyle );
caryclark83ca6282015-06-10 09:31:09 -07001266
Mike Kleinbe28ee22017-02-06 12:46:20 -05001267int main(int argc, char** argv) {
1268 SkCommandLineFlags::Parse(argc, argv);
Brian Salomondcbb9d92017-07-19 10:53:20 -04001269 if (FLAGS_trace) {
1270 SkAssertResult(SkEventTracer::SetInstance(new SkDebugfTracer()));
1271 }
1272 #if defined(SK_BUILD_FOR_IOS)
Mike Kleinadacaef2017-02-06 09:26:14 -05001273 cd_Documents();
1274#endif
bsalomon71de3532016-04-26 14:27:21 -07001275 setbuf(stdout, nullptr);
mtklein246ba3a2016-02-23 10:39:36 -08001276 setup_crash_handler();
mtkleinafae30a2016-02-24 12:28:32 -08001277
Yuqian Liba62b4a2016-12-14 13:46:53 -05001278 gSkUseAnalyticAA = FLAGS_analyticAA;
liyuqian38911a72016-10-04 11:23:22 -07001279
Yuqian Li550148b2017-01-13 10:13:13 -05001280 if (FLAGS_forceAnalyticAA) {
1281 gSkForceAnalyticAA = true;
1282 }
Mike Reed7c26ca72017-07-05 15:45:52 -04001283 if (FLAGS_forceRasterPipeline) {
1284 gSkForceRasterPipelineBlitter = true;
1285 }
Yuqian Li550148b2017-01-13 10:13:13 -05001286
Mike Kleine9f187b2017-06-26 13:23:12 -04001287 // The bots like having a verbose.log to upload, so always touch the file even if --verbose.
1288 if (!FLAGS_writePath.isEmpty()) {
mtklein51c8cfc2016-03-11 12:59:09 -08001289 sk_mkdir(FLAGS_writePath[0]);
mtkleina1ce2162016-07-22 12:23:46 -07001290 gVLog = fopen(SkOSPath::Join(FLAGS_writePath[0], "verbose.log").c_str(), "w");
mtkleinc41fd922016-03-08 09:01:39 -08001291 }
Mike Kleine9f187b2017-06-26 13:23:12 -04001292 if (FLAGS_verbose) {
1293 gVLog = stderr;
1294 }
mtkleinc41fd922016-03-08 09:01:39 -08001295
csmartdalton008b9d82017-02-22 12:00:42 -07001296 GrContextOptions grCtxOptions;
1297#if SK_SUPPORT_GPU
1298 grCtxOptions.fGpuPathRenderers = CollectGpuPathRenderersFromFlags();
1299#endif
1300
mtklein5286f022016-01-22 08:18:14 -08001301 JsonWriter::DumpJson(); // It's handy for the bots to assume this is ~never missing.
commit-bot@chromium.org39e8d932014-05-29 20:14:48 +00001302 SkAutoGraphics ag;
mtklein406654b2014-09-03 15:34:37 -07001303 SkTaskGroup::Enabler enabled(FLAGS_threads);
caryclark83ca6282015-06-10 09:31:09 -07001304 gCreateTypefaceDelegate = &create_from_name;
commit-bot@chromium.orga65e2fd2014-05-30 17:23:31 +00001305
halcanary7d571242016-02-24 17:59:16 -08001306 {
1307 SkString testResourcePath = GetResourcePath("color_wheel.png");
1308 SkFILEStream testResource(testResourcePath.c_str());
1309 if (!testResource.isValid()) {
mtkleinc41fd922016-03-08 09:01:39 -08001310 info("Some resources are missing. Do you need to set --resourcePath?\n");
halcanary7d571242016-02-24 17:59:16 -08001311 }
1312 }
mtklein62bd1a62015-01-27 14:46:26 -08001313 gather_gold();
borenet09ed4802015-04-03 14:15:33 -07001314 gather_uninteresting_hashes();
mtklein62bd1a62015-01-27 14:46:26 -08001315
scroggo86737142016-02-03 12:19:11 -08001316 if (!gather_srcs()) {
1317 return 1;
1318 }
csmartdalton008b9d82017-02-22 12:00:42 -07001319 if (!gather_sinks(grCtxOptions)) {
brianosman05c987c2016-05-09 13:42:16 -07001320 return 1;
1321 }
mtklein748ca3b2015-01-15 10:56:12 -08001322 gather_tests();
mtklein21eaf3b2016-02-08 12:39:59 -08001323 gPending = gSrcs.count() * gSinks.count() + gParallelTests.count() + gSerialTests.count();
mtkleinc41fd922016-03-08 09:01:39 -08001324 info("%d srcs * %d sinks + %d tests == %d tasks",
1325 gSrcs.count(), gSinks.count(), gParallelTests.count() + gSerialTests.count(), gPending);
mtklein748ca3b2015-01-15 10:56:12 -08001326
mtklein21eaf3b2016-02-08 12:39:59 -08001327 // Kick off as much parallel work as we can, making note of any serial work we'll need to do.
1328 SkTaskGroup parallel;
1329 SkTArray<Task> serial;
1330
1331 for (auto& sink : gSinks)
1332 for (auto& src : gSrcs) {
mtklein3eed7dd2016-02-24 19:07:07 -08001333 if (src->veto(sink->flags()) ||
1334 is_blacklisted(sink.tag.c_str(), src.tag.c_str(),
1335 src.options.c_str(), src->name().c_str())) {
mtkleinf10637f2016-06-10 13:56:35 -07001336 SkAutoMutexAcquire lock(gMutex);
mtklein3eed7dd2016-02-24 19:07:07 -08001337 gPending--;
1338 continue;
1339 }
1340
mtklein21eaf3b2016-02-08 12:39:59 -08001341 Task task(src, sink);
1342 if (src->serial() || sink->serial()) {
1343 serial.push_back(task);
1344 } else {
1345 parallel.add([task] { Task::Run(task); });
mtklein748ca3b2015-01-15 10:56:12 -08001346 }
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +00001347 }
mtklein21eaf3b2016-02-08 12:39:59 -08001348 for (auto test : gParallelTests) {
csmartdalton008b9d82017-02-22 12:00:42 -07001349 parallel.add([test, grCtxOptions] { run_test(test, grCtxOptions); });
mtklein55e88b22015-01-21 15:50:13 -08001350 }
mtklein21eaf3b2016-02-08 12:39:59 -08001351
1352 // With the parallel work running, run serial tasks and tests here on main thread.
1353 for (auto task : serial) { Task::Run(task); }
csmartdalton008b9d82017-02-22 12:00:42 -07001354 for (auto test : gSerialTests) { run_test(test, grCtxOptions); }
mtklein21eaf3b2016-02-08 12:39:59 -08001355
1356 // Wait for any remaining parallel work to complete (including any spun off of serial tasks).
1357 parallel.wait();
mtkleincd50bca2016-01-05 06:20:20 -08001358 gDefinitelyThreadSafeWork.wait();
1359
mtkleinda884c42016-02-24 18:00:23 -08001360 // We'd better have run everything.
1361 SkASSERT(gPending == 0);
mtkleine027f172016-02-26 15:53:06 -08001362 // Make sure we've flushed all our results to disk.
1363 JsonWriter::DumpJson();
mtkleinda884c42016-02-24 18:00:23 -08001364
mtklein748ca3b2015-01-15 10:56:12 -08001365 // At this point we're back in single-threaded land.
caryclarkf53ce802015-06-15 06:48:30 -07001366 sk_tool_utils::release_portable_typefaces();
mtklein@google.comd36522d2013-10-16 13:02:15 +00001367
mtklein748ca3b2015-01-15 10:56:12 -08001368 if (gFailures.count() > 0) {
mtkleinc41fd922016-03-08 09:01:39 -08001369 info("Failures:\n");
mtklein748ca3b2015-01-15 10:56:12 -08001370 for (int i = 0; i < gFailures.count(); i++) {
mtkleinc41fd922016-03-08 09:01:39 -08001371 info("\t%s\n", gFailures[i].c_str());
mtklein748ca3b2015-01-15 10:56:12 -08001372 }
mtkleinc41fd922016-03-08 09:01:39 -08001373 info("%d failures\n", gFailures.count());
mtklein748ca3b2015-01-15 10:56:12 -08001374 return 1;
mtklein114c3cd2015-01-15 10:15:02 -08001375 }
mtkleinafae30a2016-02-24 12:28:32 -08001376
1377#ifdef SK_PDF_IMAGE_STATS
halcanary7a14b312015-10-01 07:28:13 -07001378 SkPDFImageDumpStats();
mtkleinafae30a2016-02-24 12:28:32 -08001379#endif // SK_PDF_IMAGE_STATS
1380
Herb Derby5a523fe2017-01-26 16:48:28 -05001381 SkGraphics::PurgeAllCaches();
mtkleinc41fd922016-03-08 09:01:39 -08001382 info("Finished!\n");
mtklein748ca3b2015-01-15 10:56:12 -08001383 return 0;
mtklein@google.comd36522d2013-10-16 13:02:15 +00001384}
jcgregorio3b27ade2014-11-13 08:06:40 -08001385
kkinnunen179a8f52015-11-20 13:32:24 -08001386// TODO: currently many GPU tests are declared outside SK_SUPPORT_GPU guards.
1387// Thus we export the empty RunWithGPUTestContexts when SK_SUPPORT_GPU=0.
1388namespace skiatest {
bsalomon758586c2016-04-06 14:02:39 -07001389
1390#if SK_SUPPORT_GPU
1391bool IsGLContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1392 return kOpenGL_GrBackend == GrContextFactory::ContextTypeBackend(type);
1393}
bsalomondc0fcd42016-04-11 14:21:33 -07001394bool IsVulkanContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1395 return kVulkan_GrBackend == GrContextFactory::ContextTypeBackend(type);
1396}
bsalomon758586c2016-04-06 14:02:39 -07001397bool IsRenderingGLContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1398 return IsGLContextType(type) && GrContextFactory::IsRenderingContext(type);
1399}
1400bool IsNullGLContextType(sk_gpu_test::GrContextFactory::ContextType type) {
1401 return type == GrContextFactory::kNullGL_ContextType;
1402}
1403#else
1404bool IsGLContextType(int) { return false; }
bsalomondc0fcd42016-04-11 14:21:33 -07001405bool IsVulkanContextType(int) { return false; }
bsalomon758586c2016-04-06 14:02:39 -07001406bool IsRenderingGLContextType(int) { return false; }
1407bool IsNullGLContextType(int) { return false; }
1408#endif
1409
1410void RunWithGPUTestContexts(GrContextTestFn* test, GrContextTypeFilterFn* contextTypeFilter,
bsalomonf2f1c172016-04-05 12:59:06 -07001411 Reporter* reporter, GrContextFactory* factory) {
kkinnunen179a8f52015-11-20 13:32:24 -08001412#if SK_SUPPORT_GPU
kkinnunen3e980c32015-12-23 01:33:00 -08001413
Brian Salomon6405e712017-03-20 08:54:16 -04001414#if defined(SK_BUILD_FOR_UNIX) || defined(SK_BUILD_FOR_WIN) || defined(SK_BUILD_FOR_MAC)
1415 static constexpr auto kNativeGLType = GrContextFactory::kGL_ContextType;
1416#else
1417 static constexpr auto kNativeGLType = GrContextFactory::kGLES_ContextType;
1418#endif
1419
bsalomon758586c2016-04-06 14:02:39 -07001420 for (int typeInt = 0; typeInt < GrContextFactory::kContextTypeCnt; ++typeInt) {
1421 GrContextFactory::ContextType contextType = (GrContextFactory::ContextType) typeInt;
Brian Osmanaa2219c2016-12-05 10:12:17 -05001422 // Use "native" instead of explicitly trying OpenGL and OpenGL ES. Do not use GLES on
bsalomon758586c2016-04-06 14:02:39 -07001423 // desktop since tests do not account for not fixing http://skbug.com/2809
1424 if (contextType == GrContextFactory::kGL_ContextType ||
1425 contextType == GrContextFactory::kGLES_ContextType) {
Brian Salomon6405e712017-03-20 08:54:16 -04001426 if (contextType != kNativeGLType) {
bsalomon758586c2016-04-06 14:02:39 -07001427 continue;
1428 }
1429 }
csmartdaltone812d492017-02-21 12:36:05 -07001430 ContextInfo ctxInfo = factory->getContextInfo(contextType,
1431 GrContextFactory::ContextOverrides::kDisableNVPR);
Brian Osmanaa2219c2016-12-05 10:12:17 -05001432 if (contextTypeFilter && !(*contextTypeFilter)(contextType)) {
1433 continue;
1434 }
Greg Daniela5cb7812017-06-16 09:45:32 -04001435 ReporterContext ctx(reporter, SkString(GrContextFactory::ContextTypeName(contextType)));
bsalomon8b7451a2016-05-11 06:33:06 -07001436 if (ctxInfo.grContext()) {
bsalomonf2f1c172016-04-05 12:59:06 -07001437 (*test)(reporter, ctxInfo);
kkinnunen5219fd92015-12-10 06:28:13 -08001438 }
bsalomonf2f1c172016-04-05 12:59:06 -07001439 ctxInfo = factory->getContextInfo(contextType,
csmartdaltone812d492017-02-21 12:36:05 -07001440 GrContextFactory::ContextOverrides::kRequireNVPRSupport);
bsalomon8b7451a2016-05-11 06:33:06 -07001441 if (ctxInfo.grContext()) {
bsalomonf2f1c172016-04-05 12:59:06 -07001442 (*test)(reporter, ctxInfo);
kkinnunen179a8f52015-11-20 13:32:24 -08001443 }
1444 }
1445#endif
1446}
kkinnunen179a8f52015-11-20 13:32:24 -08001447} // namespace skiatest