blob: b661763fb94764db740752297f683be2de317988 [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"
11#include "include/gpu/GrContext.h"
12#include "include/private/SkTArray.h"
13#include "include/private/SkTemplates.h"
14#include "src/core/SkOSFile.h"
15#include "src/core/SkTaskGroup.h"
16#include "src/pathops/SkPathOpsDebug.h"
17#include "tests/PathOpsDebug.h"
18#include "tests/Test.h"
19#include "tools/CrashHandler.h"
20#include "tools/OverwriteLine.h"
21#include "tools/Resources.h"
22#include "tools/flags/CommandLineFlags.h"
23#include "tools/gpu/GrContextFactory.h"
robertphillips@google.combdb1be52012-09-07 18:24:43 +000024
reed@android.comed673312009-02-27 16:24:51 +000025using namespace skiatest;
bsalomon3724e572016-03-30 18:56:19 -070026using namespace sk_gpu_test;
reed@android.comed673312009-02-27 16:24:51 +000027
Mike Klein84836b72019-03-21 11:31:36 -050028static DEFINE_bool2(dumpOp, d, false, "dump the pathOps to a file to recover mid-crash.");
29static DEFINE_bool2(extendedTest, x, false, "run extended tests for pathOps.");
30static DEFINE_bool2(runFail, f, false, "check for success on tests known to fail.");
31static DEFINE_bool2(verifyOp, y, false, "compare the pathOps result against a region.");
32static DEFINE_string2(json, J, "", "write json version of tests.");
Mike Kleinc6142d82019-03-25 10:54:59 -050033static DEFINE_bool2(verbose, v, false, "enable verbose output from the test driver.");
Mike Kleind0f321b2019-03-22 13:15:11 -050034static DEFINE_bool2(veryVerbose, V, false, "tell individual tests to be verbose.");
Mike Klein629f5fc2019-03-22 14:55:19 -050035static DEFINE_bool(cpu, true, "master switch for running CPU-bound work.");
36static DEFINE_bool(gpu, true, "master switch for running GPU-bound work.");
caryclark13260682016-10-24 05:10:14 -070037
Mike Kleinc6142d82019-03-25 10:54:59 -050038static DEFINE_string2(match, m, nullptr,
39 "[~][^]substring[$] [...] of name to run.\n"
40 "Multiple matches may be separated by spaces.\n"
41 "~ causes a matching name to always be skipped\n"
42 "^ requires the start of the name to match\n"
43 "$ requires the end of the name to match\n"
44 "^ and $ requires an exact match\n"
45 "If a name does not match any list entry,\n"
46 "it is skipped unless some list entry starts with ~");
47
48static DEFINE_int_2(threads, j, -1,
49 "Run threadsafe tests on a threadpool with this many extra threads, "
50 "defaulting to one extra thread per core.");
51
Cary Clarkab87d7a2016-10-04 10:01:04 -040052#if DEBUG_COIN
Mike Klein84836b72019-03-21 11:31:36 -050053static DEFINE_bool2(coinTest, c, false, "detect unused coincidence algorithms.");
Cary Clarkab87d7a2016-10-04 10:01:04 -040054#endif
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000055
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000056// need to explicitly declare this, or we get some weird infinite loop llist
57template TestRegistry* TestRegistry::gHead;
caryclark26ad22a2015-10-16 09:03:38 -070058void (*gVerboseFinalize)() = nullptr;
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000059
halcanary87f3ba42015-01-20 09:30:20 -080060// The threads report back to this object when they are done.
61class Status {
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000062public:
halcanary87f3ba42015-01-20 09:30:20 -080063 explicit Status(int total)
64 : fDone(0), fTestCount(0), fFailCount(0), fTotal(total) {}
65 // Threadsafe.
66 void endTest(const char* testName,
67 bool success,
68 SkMSec elapsed,
69 int testCount) {
Mike Klein0ec1c572018-12-04 11:52:51 -050070 const int done = ++fDone;
71 fTestCount += testCount;
halcanary87f3ba42015-01-20 09:30:20 -080072 if (!success) {
73 SkDebugf("\n---- %s FAILED", testName);
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000074 }
75
76 SkString prefix(kSkOverwriteLine);
77 SkString time;
78 if (FLAGS_verbose) {
79 prefix.printf("\n");
halcanary87f3ba42015-01-20 09:30:20 -080080 time.printf("%5dms ", elapsed);
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000081 }
halcanary87f3ba42015-01-20 09:30:20 -080082 SkDebugf("%s[%3d/%3d] %s%s", prefix.c_str(), done, fTotal, time.c_str(),
83 testName);
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000084 }
85
Mike Klein0ec1c572018-12-04 11:52:51 -050086 void reportFailure() { fFailCount++; }
halcanary87f3ba42015-01-20 09:30:20 -080087
88 int32_t testCount() { return fTestCount; }
89 int32_t failCount() { return fFailCount; }
90
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000091private:
Mike Klein0ec1c572018-12-04 11:52:51 -050092 std::atomic<int32_t> fDone;
93 std::atomic<int32_t> fTestCount;
94 std::atomic<int32_t> fFailCount;
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000095 const int fTotal;
96};
97
mtklein048494c2016-02-16 19:06:15 -080098class SkTestRunnable {
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000099public:
Brian Salomondcfca432017-11-15 15:48:03 -0500100 SkTestRunnable(const Test& test, Status* status) : fTest(test), fStatus(status) {}
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000101
Brian Salomondcfca432017-11-15 15:48:03 -0500102 void operator()() {
103 struct TestReporter : public skiatest::Reporter {
104 public:
105 TestReporter() : fStats(nullptr), fError(false), fTestCount(0) {}
106 void bumpTestCount() override { ++fTestCount; }
107 bool allowExtendedTest() const override { return FLAGS_extendedTest; }
108 bool verbose() const override { return FLAGS_veryVerbose; }
109 void reportFailed(const skiatest::Failure& failure) override {
110 SkDebugf("\nFAILED: %s", failure.toString().c_str());
111 fError = true;
112 }
113 void* stats() const override { return fStats; }
114 void* fStats;
115 bool fError;
116 int fTestCount;
117 } reporter;
halcanary87f3ba42015-01-20 09:30:20 -0800118
Brian Salomondcfca432017-11-15 15:48:03 -0500119 const Timer timer;
120 fTest.proc(&reporter, GrContextOptions());
121 SkMSec elapsed = timer.elapsedMsInt();
122 if (reporter.fError) {
123 fStatus->reportFailure();
124 }
125 fStatus->endTest(fTest.name, !reporter.fError, elapsed, reporter.fTestCount);
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000126 }
127
128private:
halcanary87f3ba42015-01-20 09:30:20 -0800129 Test fTest;
130 Status* fStatus;
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000131};
commit-bot@chromium.orgba59d642013-04-11 16:54:09 +0000132
commit-bot@chromium.org5a47b092014-01-30 15:30:50 +0000133static bool should_run(const char* testName, bool isGPUTest) {
Mike Klein88544fb2019-03-20 10:50:33 -0500134 if (CommandLineFlags::ShouldSkip(FLAGS_match, testName)) {
commit-bot@chromium.org5a47b092014-01-30 15:30:50 +0000135 return false;
136 }
137 if (!FLAGS_cpu && !isGPUTest) {
138 return false;
139 }
140 if (!FLAGS_gpu && isGPUTest) {
141 return false;
142 }
143 return true;
144}
145
Mike Kleinbe28ee22017-02-06 12:46:20 -0500146int main(int argc, char** argv) {
Mike Klein88544fb2019-03-20 10:50:33 -0500147 CommandLineFlags::Parse(argc, argv);
Cary Clark918fb1f2016-11-15 13:22:25 -0500148#if DEBUG_DUMP_VERIFY
caryclark13260682016-10-24 05:10:14 -0700149 SkPathOpsDebug::gDumpOp = FLAGS_dumpOp;
150 SkPathOpsDebug::gVerifyOp = FLAGS_verifyOp;
151#endif
Cary Clark59d5a0e2017-01-23 14:38:52 +0000152 SkPathOpsDebug::gRunFail = FLAGS_runFail;
caryclark13260682016-10-24 05:10:14 -0700153 SkPathOpsDebug::gVeryVerbose = FLAGS_veryVerbose;
Cary Clarkf3949122018-08-07 16:38:21 -0400154 PathOpsDebug::gOutFirst = true;
Cary Clark4533f3d2018-08-08 09:48:09 -0400155 PathOpsDebug::gCheckForDuplicateNames = false;
Cary Clark9c9611f2018-08-08 13:17:25 -0400156 PathOpsDebug::gOutputSVG = false;
Cary Clarkf3949122018-08-07 16:38:21 -0400157 if ((PathOpsDebug::gJson = !FLAGS_json.isEmpty())) {
158 PathOpsDebug::gOut = fopen(FLAGS_json[0], "wb");
159 fprintf(PathOpsDebug::gOut, "{\n");
160 FLAGS_threads = 0;
Cary Clarkda6289c2018-08-27 16:10:28 -0400161 PathOpsDebug::gMarkJsonFlaky = false;
Cary Clarkf3949122018-08-07 16:38:21 -0400162 }
mtklein30e6e2a2014-06-18 11:44:15 -0700163 SetupCrashHandler();
commit-bot@chromium.orgba59d642013-04-11 16:54:09 +0000164
tfarinaa71d3af2014-11-07 06:12:30 -0800165 SkAutoGraphics ag;
bungeman@google.com5af16f82011-09-02 15:06:44 +0000166
reed@google.com91d449e2011-10-26 15:25:18 +0000167 {
168 SkString header("Skia UnitTests:");
reed@google.com9aff1482013-04-11 18:27:52 +0000169 if (!FLAGS_match.isEmpty()) {
caryclark@google.comb631eec2013-05-02 13:14:40 +0000170 header.appendf(" --match");
171 for (int index = 0; index < FLAGS_match.count(); ++index) {
172 header.appendf(" %s", FLAGS_match[index]);
173 }
reed@google.com91d449e2011-10-26 15:25:18 +0000174 }
halcanary87f3ba42015-01-20 09:30:20 -0800175 SkString tmpDir = skiatest::GetTmpDir();
scroggo@google.comc76218d2013-06-06 14:59:56 +0000176 if (!tmpDir.isEmpty()) {
177 header.appendf(" --tmpDir %s", tmpDir.c_str());
djsollen@google.comcb626502013-03-20 13:48:20 +0000178 }
tfarinabcbc1782014-06-18 14:32:48 -0700179 SkString resourcePath = GetResourcePath();
scroggo@google.comc76218d2013-06-06 14:59:56 +0000180 if (!resourcePath.isEmpty()) {
181 header.appendf(" --resourcePath %s", resourcePath.c_str());
reed@google.com789c6f22013-02-25 20:24:24 +0000182 }
caryclark13260682016-10-24 05:10:14 -0700183#if DEBUG_COIN
184 if (FLAGS_coinTest) {
185 header.appendf(" -c");
186 }
187#endif
188 if (FLAGS_dumpOp) {
189 header.appendf(" -d");
190 }
Cary Clark59d5a0e2017-01-23 14:38:52 +0000191#ifdef SK_DEBUG
192 if (FLAGS_runFail) {
193 header.appendf(" -f");
194 }
195#endif
caryclark13260682016-10-24 05:10:14 -0700196 if (FLAGS_verbose) {
197 header.appendf(" -v");
198 }
199 if (FLAGS_veryVerbose) {
200 header.appendf(" -V");
201 }
202 if (FLAGS_extendedTest) {
203 header.appendf(" -x");
204 }
205 if (FLAGS_verifyOp) {
206 header.appendf(" -y");
207 }
reed@google.com91d449e2011-10-26 15:25:18 +0000208#ifdef SK_DEBUG
209 header.append(" SK_DEBUG");
210#else
211 header.append(" SK_RELEASE");
212#endif
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000213 if (FLAGS_veryVerbose) {
214 header.appendf("\n");
215 }
kkinnunen297aaf92015-02-19 06:32:12 -0800216 SkDebugf("%s", header.c_str());
reed@google.com91d449e2011-10-26 15:25:18 +0000217 }
218
reed@android.com80e39a72009-04-02 16:59:40 +0000219
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000220 // Count tests first.
221 int total = 0;
222 int toRun = 0;
sglez@google.com586db932013-07-24 17:24:23 +0000223
Hal Canary972eba32018-07-30 17:07:07 -0400224 for (const Test& test : TestRegistry::Range()) {
halcanary87f3ba42015-01-20 09:30:20 -0800225 if (should_run(test.name, test.needsGpu)) {
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000226 toRun++;
227 }
228 total++;
229 }
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000230
231 // Now run them.
bungeman@google.com5af16f82011-09-02 15:06:44 +0000232 int skipCount = 0;
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000233
mtklein406654b2014-09-03 15:34:37 -0700234 SkTaskGroup::Enabler enabled(FLAGS_threads);
235 SkTaskGroup cpuTests;
halcanary87f3ba42015-01-20 09:30:20 -0800236 SkTArray<const Test*> gpuTests;
commit-bot@chromium.org261c6662014-01-02 16:19:53 +0000237
halcanary87f3ba42015-01-20 09:30:20 -0800238 Status status(toRun);
Hal Canary972eba32018-07-30 17:07:07 -0400239
240 for (const Test& test : TestRegistry::Range()) {
halcanary87f3ba42015-01-20 09:30:20 -0800241 if (!should_run(test.name, test.needsGpu)) {
bungeman@google.com5af16f82011-09-02 15:06:44 +0000242 ++skipCount;
halcanary87f3ba42015-01-20 09:30:20 -0800243 } else if (test.needsGpu) {
244 gpuTests.push_back(&test);
bungeman@google.com5af16f82011-09-02 15:06:44 +0000245 } else {
mtklein048494c2016-02-16 19:06:15 -0800246 cpuTests.add(SkTestRunnable(test, &status));
bungeman@google.com5af16f82011-09-02 15:06:44 +0000247 }
reed@android.comed673312009-02-27 16:24:51 +0000248 }
reed@android.com57b799e2009-04-01 20:26:42 +0000249
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +0000250 // Run GPU tests on this thread.
251 for (int i = 0; i < gpuTests.count(); i++) {
Brian Salomondcfca432017-11-15 15:48:03 -0500252 SkTestRunnable(*gpuTests[i], &status)();
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000253 }
254
commit-bot@chromium.orga7538ba2013-10-10 18:49:04 +0000255 // Block until threaded tests finish.
mtklein406654b2014-09-03 15:34:37 -0700256 cpuTests.wait();
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000257
commit-bot@chromium.org261c6662014-01-02 16:19:53 +0000258 if (FLAGS_verbose) {
halcanary87f3ba42015-01-20 09:30:20 -0800259 SkDebugf(
260 "\nFinished %d tests, %d failures, %d skipped. "
261 "(%d internal tests)",
262 toRun, status.failCount(), skipCount, status.testCount());
caryclark26ad22a2015-10-16 09:03:38 -0700263 if (gVerboseFinalize) {
264 (*gVerboseFinalize)();
265 }
caryclark@google.comd54e1e92013-04-10 15:57:31 +0000266 }
reed@google.coma2769752012-07-22 22:33:05 +0000267
commit-bot@chromium.org261c6662014-01-02 16:19:53 +0000268 SkDebugf("\n");
Cary Clarkab87d7a2016-10-04 10:01:04 -0400269#if DEBUG_COIN
270 if (FLAGS_coinTest) {
271 SkPathOpsDebug::DumpCoinDict();
272 }
273#endif
Cary Clarkf3949122018-08-07 16:38:21 -0400274 if (PathOpsDebug::gJson) {
275 fprintf(PathOpsDebug::gOut, "\n}\n");
276 fclose(PathOpsDebug::gOut);
277 }
halcanary87f3ba42015-01-20 09:30:20 -0800278 return (status.failCount() == 0) ? 0 : 1;
reed@android.comed673312009-02-27 16:24:51 +0000279}