blob: 792d4d2fcb5f059fe3e722ac02ff326559ca8344 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 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 */
commit-bot@chromium.orgba59d642013-04-11 16:54:09 +00007
Mike Klein88544fb2019-03-20 10:50:33 -05008#include <atomic>
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "include/core/SkGraphics.h"
10#include "include/core/SkTime.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/private/SkTArray.h"
12#include "include/private/SkTemplates.h"
13#include "src/core/SkOSFile.h"
14#include "src/core/SkTaskGroup.h"
15#include "src/pathops/SkPathOpsDebug.h"
16#include "tests/PathOpsDebug.h"
17#include "tests/Test.h"
18#include "tools/CrashHandler.h"
19#include "tools/OverwriteLine.h"
20#include "tools/Resources.h"
21#include "tools/flags/CommandLineFlags.h"
22#include "tools/gpu/GrContextFactory.h"
robertphillips@google.combdb1be52012-09-07 18:24:43 +000023
reed@android.comed673312009-02-27 16:24:51 +000024using namespace skiatest;
bsalomon3724e572016-03-30 18:56:19 -070025using namespace sk_gpu_test;
reed@android.comed673312009-02-27 16:24:51 +000026
Mike Klein84836b72019-03-21 11:31:36 -050027static DEFINE_bool2(dumpOp, d, false, "dump the pathOps to a file to recover mid-crash.");
28static DEFINE_bool2(extendedTest, x, false, "run extended tests for pathOps.");
29static DEFINE_bool2(runFail, f, false, "check for success on tests known to fail.");
30static DEFINE_bool2(verifyOp, y, false, "compare the pathOps result against a region.");
31static DEFINE_string2(json, J, "", "write json version of tests.");
Mike Kleinc6142d82019-03-25 10:54:59 -050032static DEFINE_bool2(verbose, v, false, "enable verbose output from the test driver.");
Mike Kleind0f321b2019-03-22 13:15:11 -050033static DEFINE_bool2(veryVerbose, V, false, "tell individual tests to be verbose.");
Mike Klein629f5fc2019-03-22 14:55:19 -050034static DEFINE_bool(cpu, true, "master switch for running CPU-bound work.");
35static DEFINE_bool(gpu, true, "master switch for running GPU-bound work.");
caryclark13260682016-10-24 05:10:14 -070036
Mike Kleinc6142d82019-03-25 10:54:59 -050037static DEFINE_string2(match, m, nullptr,
38 "[~][^]substring[$] [...] of name to run.\n"
39 "Multiple matches may be separated by spaces.\n"
40 "~ causes a matching name to always be skipped\n"
41 "^ requires the start of the name to match\n"
42 "$ requires the end of the name to match\n"
43 "^ and $ requires an exact match\n"
44 "If a name does not match any list entry,\n"
45 "it is skipped unless some list entry starts with ~");
46
47static DEFINE_int_2(threads, j, -1,
48 "Run threadsafe tests on a threadpool with this many extra threads, "
49 "defaulting to one extra thread per core.");
50
Cary Clarkab87d7a2016-10-04 10:01:04 -040051#if DEBUG_COIN
Mike Klein84836b72019-03-21 11:31:36 -050052static DEFINE_bool2(coinTest, c, false, "detect unused coincidence algorithms.");
Cary Clarkab87d7a2016-10-04 10:01:04 -040053#endif
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000054
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000055// need to explicitly declare this, or we get some weird infinite loop llist
56template TestRegistry* TestRegistry::gHead;
caryclark26ad22a2015-10-16 09:03:38 -070057void (*gVerboseFinalize)() = nullptr;
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000058
halcanary87f3ba42015-01-20 09:30:20 -080059// The threads report back to this object when they are done.
60class Status {
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000061public:
halcanary87f3ba42015-01-20 09:30:20 -080062 explicit Status(int total)
63 : fDone(0), fTestCount(0), fFailCount(0), fTotal(total) {}
64 // Threadsafe.
65 void endTest(const char* testName,
66 bool success,
67 SkMSec elapsed,
68 int testCount) {
Mike Klein0ec1c572018-12-04 11:52:51 -050069 const int done = ++fDone;
70 fTestCount += testCount;
halcanary87f3ba42015-01-20 09:30:20 -080071 if (!success) {
72 SkDebugf("\n---- %s FAILED", testName);
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000073 }
74
75 SkString prefix(kSkOverwriteLine);
76 SkString time;
77 if (FLAGS_verbose) {
78 prefix.printf("\n");
halcanary87f3ba42015-01-20 09:30:20 -080079 time.printf("%5dms ", elapsed);
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000080 }
halcanary87f3ba42015-01-20 09:30:20 -080081 SkDebugf("%s[%3d/%3d] %s%s", prefix.c_str(), done, fTotal, time.c_str(),
82 testName);
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000083 }
84
Mike Klein0ec1c572018-12-04 11:52:51 -050085 void reportFailure() { fFailCount++; }
halcanary87f3ba42015-01-20 09:30:20 -080086
87 int32_t testCount() { return fTestCount; }
88 int32_t failCount() { return fFailCount; }
89
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000090private:
Mike Klein0ec1c572018-12-04 11:52:51 -050091 std::atomic<int32_t> fDone;
92 std::atomic<int32_t> fTestCount;
93 std::atomic<int32_t> fFailCount;
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000094 const int fTotal;
95};
96
mtklein048494c2016-02-16 19:06:15 -080097class SkTestRunnable {
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000098public:
Brian Salomondcfca432017-11-15 15:48:03 -050099 SkTestRunnable(const Test& test, Status* status) : fTest(test), fStatus(status) {}
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000100
Brian Salomondcfca432017-11-15 15:48:03 -0500101 void operator()() {
102 struct TestReporter : public skiatest::Reporter {
103 public:
104 TestReporter() : fStats(nullptr), fError(false), fTestCount(0) {}
105 void bumpTestCount() override { ++fTestCount; }
106 bool allowExtendedTest() const override { return FLAGS_extendedTest; }
107 bool verbose() const override { return FLAGS_veryVerbose; }
108 void reportFailed(const skiatest::Failure& failure) override {
109 SkDebugf("\nFAILED: %s", failure.toString().c_str());
110 fError = true;
111 }
112 void* stats() const override { return fStats; }
113 void* fStats;
114 bool fError;
115 int fTestCount;
116 } reporter;
halcanary87f3ba42015-01-20 09:30:20 -0800117
Brian Salomondcfca432017-11-15 15:48:03 -0500118 const Timer timer;
119 fTest.proc(&reporter, GrContextOptions());
120 SkMSec elapsed = timer.elapsedMsInt();
121 if (reporter.fError) {
122 fStatus->reportFailure();
123 }
124 fStatus->endTest(fTest.name, !reporter.fError, elapsed, reporter.fTestCount);
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000125 }
126
127private:
halcanary87f3ba42015-01-20 09:30:20 -0800128 Test fTest;
129 Status* fStatus;
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000130};
commit-bot@chromium.orgba59d642013-04-11 16:54:09 +0000131
commit-bot@chromium.org5a47b092014-01-30 15:30:50 +0000132static bool should_run(const char* testName, bool isGPUTest) {
Mike Klein88544fb2019-03-20 10:50:33 -0500133 if (CommandLineFlags::ShouldSkip(FLAGS_match, testName)) {
commit-bot@chromium.org5a47b092014-01-30 15:30:50 +0000134 return false;
135 }
136 if (!FLAGS_cpu && !isGPUTest) {
137 return false;
138 }
139 if (!FLAGS_gpu && isGPUTest) {
140 return false;
141 }
142 return true;
143}
144
Mike Kleinbe28ee22017-02-06 12:46:20 -0500145int main(int argc, char** argv) {
Mike Klein88544fb2019-03-20 10:50:33 -0500146 CommandLineFlags::Parse(argc, argv);
Cary Clark918fb1f2016-11-15 13:22:25 -0500147#if DEBUG_DUMP_VERIFY
caryclark13260682016-10-24 05:10:14 -0700148 SkPathOpsDebug::gDumpOp = FLAGS_dumpOp;
149 SkPathOpsDebug::gVerifyOp = FLAGS_verifyOp;
150#endif
Cary Clark59d5a0e2017-01-23 14:38:52 +0000151 SkPathOpsDebug::gRunFail = FLAGS_runFail;
caryclark13260682016-10-24 05:10:14 -0700152 SkPathOpsDebug::gVeryVerbose = FLAGS_veryVerbose;
Cary Clarkf3949122018-08-07 16:38:21 -0400153 PathOpsDebug::gOutFirst = true;
Cary Clark4533f3d2018-08-08 09:48:09 -0400154 PathOpsDebug::gCheckForDuplicateNames = false;
Cary Clark9c9611f2018-08-08 13:17:25 -0400155 PathOpsDebug::gOutputSVG = false;
Cary Clarkf3949122018-08-07 16:38:21 -0400156 if ((PathOpsDebug::gJson = !FLAGS_json.isEmpty())) {
157 PathOpsDebug::gOut = fopen(FLAGS_json[0], "wb");
158 fprintf(PathOpsDebug::gOut, "{\n");
159 FLAGS_threads = 0;
Cary Clarkda6289c2018-08-27 16:10:28 -0400160 PathOpsDebug::gMarkJsonFlaky = false;
Cary Clarkf3949122018-08-07 16:38:21 -0400161 }
mtklein30e6e2a2014-06-18 11:44:15 -0700162 SetupCrashHandler();
commit-bot@chromium.orgba59d642013-04-11 16:54:09 +0000163
tfarinaa71d3af2014-11-07 06:12:30 -0800164 SkAutoGraphics ag;
bungeman@google.com5af16f82011-09-02 15:06:44 +0000165
reed@google.com91d449e2011-10-26 15:25:18 +0000166 {
167 SkString header("Skia UnitTests:");
reed@google.com9aff1482013-04-11 18:27:52 +0000168 if (!FLAGS_match.isEmpty()) {
caryclark@google.comb631eec2013-05-02 13:14:40 +0000169 header.appendf(" --match");
170 for (int index = 0; index < FLAGS_match.count(); ++index) {
171 header.appendf(" %s", FLAGS_match[index]);
172 }
reed@google.com91d449e2011-10-26 15:25:18 +0000173 }
halcanary87f3ba42015-01-20 09:30:20 -0800174 SkString tmpDir = skiatest::GetTmpDir();
scroggo@google.comc76218d2013-06-06 14:59:56 +0000175 if (!tmpDir.isEmpty()) {
176 header.appendf(" --tmpDir %s", tmpDir.c_str());
djsollen@google.comcb626502013-03-20 13:48:20 +0000177 }
tfarinabcbc1782014-06-18 14:32:48 -0700178 SkString resourcePath = GetResourcePath();
scroggo@google.comc76218d2013-06-06 14:59:56 +0000179 if (!resourcePath.isEmpty()) {
180 header.appendf(" --resourcePath %s", resourcePath.c_str());
reed@google.com789c6f22013-02-25 20:24:24 +0000181 }
caryclark13260682016-10-24 05:10:14 -0700182#if DEBUG_COIN
183 if (FLAGS_coinTest) {
184 header.appendf(" -c");
185 }
186#endif
187 if (FLAGS_dumpOp) {
188 header.appendf(" -d");
189 }
Cary Clark59d5a0e2017-01-23 14:38:52 +0000190#ifdef SK_DEBUG
191 if (FLAGS_runFail) {
192 header.appendf(" -f");
193 }
194#endif
caryclark13260682016-10-24 05:10:14 -0700195 if (FLAGS_verbose) {
196 header.appendf(" -v");
197 }
198 if (FLAGS_veryVerbose) {
199 header.appendf(" -V");
200 }
201 if (FLAGS_extendedTest) {
202 header.appendf(" -x");
203 }
204 if (FLAGS_verifyOp) {
205 header.appendf(" -y");
206 }
reed@google.com91d449e2011-10-26 15:25:18 +0000207#ifdef SK_DEBUG
208 header.append(" SK_DEBUG");
209#else
210 header.append(" SK_RELEASE");
211#endif
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000212 if (FLAGS_veryVerbose) {
213 header.appendf("\n");
214 }
kkinnunen297aaf92015-02-19 06:32:12 -0800215 SkDebugf("%s", header.c_str());
reed@google.com91d449e2011-10-26 15:25:18 +0000216 }
217
reed@android.com80e39a72009-04-02 16:59:40 +0000218
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000219 // Count tests first.
220 int total = 0;
221 int toRun = 0;
sglez@google.com586db932013-07-24 17:24:23 +0000222
Hal Canary972eba32018-07-30 17:07:07 -0400223 for (const Test& test : TestRegistry::Range()) {
halcanary87f3ba42015-01-20 09:30:20 -0800224 if (should_run(test.name, test.needsGpu)) {
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000225 toRun++;
226 }
227 total++;
228 }
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000229
230 // Now run them.
bungeman@google.com5af16f82011-09-02 15:06:44 +0000231 int skipCount = 0;
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000232
mtklein406654b2014-09-03 15:34:37 -0700233 SkTaskGroup::Enabler enabled(FLAGS_threads);
234 SkTaskGroup cpuTests;
halcanary87f3ba42015-01-20 09:30:20 -0800235 SkTArray<const Test*> gpuTests;
commit-bot@chromium.org261c6662014-01-02 16:19:53 +0000236
halcanary87f3ba42015-01-20 09:30:20 -0800237 Status status(toRun);
Hal Canary972eba32018-07-30 17:07:07 -0400238
239 for (const Test& test : TestRegistry::Range()) {
halcanary87f3ba42015-01-20 09:30:20 -0800240 if (!should_run(test.name, test.needsGpu)) {
bungeman@google.com5af16f82011-09-02 15:06:44 +0000241 ++skipCount;
halcanary87f3ba42015-01-20 09:30:20 -0800242 } else if (test.needsGpu) {
243 gpuTests.push_back(&test);
bungeman@google.com5af16f82011-09-02 15:06:44 +0000244 } else {
mtklein048494c2016-02-16 19:06:15 -0800245 cpuTests.add(SkTestRunnable(test, &status));
bungeman@google.com5af16f82011-09-02 15:06:44 +0000246 }
reed@android.comed673312009-02-27 16:24:51 +0000247 }
reed@android.com57b799e2009-04-01 20:26:42 +0000248
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +0000249 // Run GPU tests on this thread.
250 for (int i = 0; i < gpuTests.count(); i++) {
Brian Salomondcfca432017-11-15 15:48:03 -0500251 SkTestRunnable(*gpuTests[i], &status)();
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000252 }
253
commit-bot@chromium.orga7538ba2013-10-10 18:49:04 +0000254 // Block until threaded tests finish.
mtklein406654b2014-09-03 15:34:37 -0700255 cpuTests.wait();
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000256
commit-bot@chromium.org261c6662014-01-02 16:19:53 +0000257 if (FLAGS_verbose) {
halcanary87f3ba42015-01-20 09:30:20 -0800258 SkDebugf(
259 "\nFinished %d tests, %d failures, %d skipped. "
260 "(%d internal tests)",
261 toRun, status.failCount(), skipCount, status.testCount());
caryclark26ad22a2015-10-16 09:03:38 -0700262 if (gVerboseFinalize) {
263 (*gVerboseFinalize)();
264 }
caryclark@google.comd54e1e92013-04-10 15:57:31 +0000265 }
reed@google.coma2769752012-07-22 22:33:05 +0000266
commit-bot@chromium.org261c6662014-01-02 16:19:53 +0000267 SkDebugf("\n");
Cary Clarkab87d7a2016-10-04 10:01:04 -0400268#if DEBUG_COIN
269 if (FLAGS_coinTest) {
270 SkPathOpsDebug::DumpCoinDict();
271 }
272#endif
Cary Clarkf3949122018-08-07 16:38:21 -0400273 if (PathOpsDebug::gJson) {
274 fprintf(PathOpsDebug::gOut, "\n}\n");
275 fclose(PathOpsDebug::gOut);
276 }
halcanary87f3ba42015-01-20 09:30:20 -0800277 return (status.failCount() == 0) ? 0 : 1;
reed@android.comed673312009-02-27 16:24:51 +0000278}