blob: 99f9609327d30a8c640a1432afb8e65aed06e31e [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
mtklein30e6e2a2014-06-18 11:44:15 -07008#include "CrashHandler.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +00009#include "OverwriteLine.h"
Cary Clarkf3949122018-08-07 16:38:21 -040010#include "PathOpsDebug.h"
tfarinabcbc1782014-06-18 14:32:48 -070011#include "Resources.h"
herb62a69c22015-09-29 11:47:45 -070012#include "SkAtomics.h"
caryclark17f0b6d2014-07-22 10:15:34 -070013#include "SkCommonFlags.h"
reed@android.com5e5adfd2009-03-07 03:39:23 +000014#include "SkGraphics.h"
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +000015#include "SkOSFile.h"
Cary Clarkab87d7a2016-10-04 10:01:04 -040016#include "SkPathOpsDebug.h"
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000017#include "SkTArray.h"
mtklein406654b2014-09-03 15:34:37 -070018#include "SkTaskGroup.h"
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000019#include "SkTemplates.h"
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +000020#include "SkTime.h"
reed@android.comed673312009-02-27 16:24:51 +000021#include "Test.h"
22
robertphillips@google.combdb1be52012-09-07 18:24:43 +000023#include "GrContext.h"
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +000024#include "GrContextFactory.h"
robertphillips@google.combdb1be52012-09-07 18:24:43 +000025
reed@android.comed673312009-02-27 16:24:51 +000026using namespace skiatest;
bsalomon3724e572016-03-30 18:56:19 -070027using namespace sk_gpu_test;
reed@android.comed673312009-02-27 16:24:51 +000028
caryclark13260682016-10-24 05:10:14 -070029DEFINE_bool2(dumpOp, d, false, "dump the pathOps to a file to recover mid-crash.");
commit-bot@chromium.orgba59d642013-04-11 16:54:09 +000030DEFINE_bool2(extendedTest, x, false, "run extended tests for pathOps.");
Cary Clark59d5a0e2017-01-23 14:38:52 +000031DEFINE_bool2(runFail, f, false, "check for success on tests known to fail.");
caryclark13260682016-10-24 05:10:14 -070032DEFINE_bool2(verifyOp, y, false, "compare the pathOps result against a region.");
Cary Clarkf3949122018-08-07 16:38:21 -040033DEFINE_string2(json, J, "", "write json version of tests.");
caryclark13260682016-10-24 05:10:14 -070034
Cary Clarkab87d7a2016-10-04 10:01:04 -040035#if DEBUG_COIN
36DEFINE_bool2(coinTest, c, false, "detect unused coincidence algorithms.");
37#endif
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000038
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000039// need to explicitly declare this, or we get some weird infinite loop llist
40template TestRegistry* TestRegistry::gHead;
caryclark26ad22a2015-10-16 09:03:38 -070041void (*gVerboseFinalize)() = nullptr;
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000042
halcanary87f3ba42015-01-20 09:30:20 -080043// The threads report back to this object when they are done.
44class Status {
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000045public:
halcanary87f3ba42015-01-20 09:30:20 -080046 explicit Status(int total)
47 : fDone(0), fTestCount(0), fFailCount(0), fTotal(total) {}
48 // Threadsafe.
49 void endTest(const char* testName,
50 bool success,
51 SkMSec elapsed,
52 int testCount) {
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000053 const int done = 1 + sk_atomic_inc(&fDone);
halcanary87f3ba42015-01-20 09:30:20 -080054 for (int i = 0; i < testCount; ++i) {
55 sk_atomic_inc(&fTestCount);
56 }
57 if (!success) {
58 SkDebugf("\n---- %s FAILED", testName);
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000059 }
60
61 SkString prefix(kSkOverwriteLine);
62 SkString time;
63 if (FLAGS_verbose) {
64 prefix.printf("\n");
halcanary87f3ba42015-01-20 09:30:20 -080065 time.printf("%5dms ", elapsed);
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000066 }
halcanary87f3ba42015-01-20 09:30:20 -080067 SkDebugf("%s[%3d/%3d] %s%s", prefix.c_str(), done, fTotal, time.c_str(),
68 testName);
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000069 }
70
halcanary87f3ba42015-01-20 09:30:20 -080071 void reportFailure() { sk_atomic_inc(&fFailCount); }
72
73 int32_t testCount() { return fTestCount; }
74 int32_t failCount() { return fFailCount; }
75
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000076private:
77 int32_t fDone; // atomic
halcanary87f3ba42015-01-20 09:30:20 -080078 int32_t fTestCount; // atomic
79 int32_t fFailCount; // atomic
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000080 const int fTotal;
81};
82
mtklein048494c2016-02-16 19:06:15 -080083class SkTestRunnable {
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000084public:
Brian Salomondcfca432017-11-15 15:48:03 -050085 SkTestRunnable(const Test& test, Status* status) : fTest(test), fStatus(status) {}
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000086
Brian Salomondcfca432017-11-15 15:48:03 -050087 void operator()() {
88 struct TestReporter : public skiatest::Reporter {
89 public:
90 TestReporter() : fStats(nullptr), fError(false), fTestCount(0) {}
91 void bumpTestCount() override { ++fTestCount; }
92 bool allowExtendedTest() const override { return FLAGS_extendedTest; }
93 bool verbose() const override { return FLAGS_veryVerbose; }
94 void reportFailed(const skiatest::Failure& failure) override {
95 SkDebugf("\nFAILED: %s", failure.toString().c_str());
96 fError = true;
97 }
98 void* stats() const override { return fStats; }
99 void* fStats;
100 bool fError;
101 int fTestCount;
102 } reporter;
halcanary87f3ba42015-01-20 09:30:20 -0800103
Brian Salomondcfca432017-11-15 15:48:03 -0500104 const Timer timer;
105 fTest.proc(&reporter, GrContextOptions());
106 SkMSec elapsed = timer.elapsedMsInt();
107 if (reporter.fError) {
108 fStatus->reportFailure();
109 }
110 fStatus->endTest(fTest.name, !reporter.fError, elapsed, reporter.fTestCount);
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000111 }
112
113private:
halcanary87f3ba42015-01-20 09:30:20 -0800114 Test fTest;
115 Status* fStatus;
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000116};
commit-bot@chromium.orgba59d642013-04-11 16:54:09 +0000117
commit-bot@chromium.org5a47b092014-01-30 15:30:50 +0000118static bool should_run(const char* testName, bool isGPUTest) {
119 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, testName)) {
120 return false;
121 }
122 if (!FLAGS_cpu && !isGPUTest) {
123 return false;
124 }
125 if (!FLAGS_gpu && isGPUTest) {
126 return false;
127 }
128 return true;
129}
130
Mike Kleinbe28ee22017-02-06 12:46:20 -0500131int main(int argc, char** argv) {
132 SkCommandLineFlags::Parse(argc, argv);
Cary Clark918fb1f2016-11-15 13:22:25 -0500133#if DEBUG_DUMP_VERIFY
caryclark13260682016-10-24 05:10:14 -0700134 SkPathOpsDebug::gDumpOp = FLAGS_dumpOp;
135 SkPathOpsDebug::gVerifyOp = FLAGS_verifyOp;
136#endif
Cary Clark59d5a0e2017-01-23 14:38:52 +0000137 SkPathOpsDebug::gRunFail = FLAGS_runFail;
caryclark13260682016-10-24 05:10:14 -0700138 SkPathOpsDebug::gVeryVerbose = FLAGS_veryVerbose;
Cary Clarkf3949122018-08-07 16:38:21 -0400139 PathOpsDebug::gOutFirst = true;
Cary Clark4533f3d2018-08-08 09:48:09 -0400140 PathOpsDebug::gCheckForDuplicateNames = false;
Cary Clark9c9611f2018-08-08 13:17:25 -0400141 PathOpsDebug::gOutputSVG = false;
Cary Clarkf3949122018-08-07 16:38:21 -0400142 if ((PathOpsDebug::gJson = !FLAGS_json.isEmpty())) {
143 PathOpsDebug::gOut = fopen(FLAGS_json[0], "wb");
144 fprintf(PathOpsDebug::gOut, "{\n");
145 FLAGS_threads = 0;
Cary Clarkda6289c2018-08-27 16:10:28 -0400146 PathOpsDebug::gMarkJsonFlaky = false;
Cary Clarkf3949122018-08-07 16:38:21 -0400147 }
mtklein30e6e2a2014-06-18 11:44:15 -0700148 SetupCrashHandler();
commit-bot@chromium.orgba59d642013-04-11 16:54:09 +0000149
tfarinaa71d3af2014-11-07 06:12:30 -0800150 SkAutoGraphics ag;
bungeman@google.com5af16f82011-09-02 15:06:44 +0000151
reed@google.com91d449e2011-10-26 15:25:18 +0000152 {
153 SkString header("Skia UnitTests:");
reed@google.com9aff1482013-04-11 18:27:52 +0000154 if (!FLAGS_match.isEmpty()) {
caryclark@google.comb631eec2013-05-02 13:14:40 +0000155 header.appendf(" --match");
156 for (int index = 0; index < FLAGS_match.count(); ++index) {
157 header.appendf(" %s", FLAGS_match[index]);
158 }
reed@google.com91d449e2011-10-26 15:25:18 +0000159 }
halcanary87f3ba42015-01-20 09:30:20 -0800160 SkString tmpDir = skiatest::GetTmpDir();
scroggo@google.comc76218d2013-06-06 14:59:56 +0000161 if (!tmpDir.isEmpty()) {
162 header.appendf(" --tmpDir %s", tmpDir.c_str());
djsollen@google.comcb626502013-03-20 13:48:20 +0000163 }
tfarinabcbc1782014-06-18 14:32:48 -0700164 SkString resourcePath = GetResourcePath();
scroggo@google.comc76218d2013-06-06 14:59:56 +0000165 if (!resourcePath.isEmpty()) {
166 header.appendf(" --resourcePath %s", resourcePath.c_str());
reed@google.com789c6f22013-02-25 20:24:24 +0000167 }
caryclark13260682016-10-24 05:10:14 -0700168#if DEBUG_COIN
169 if (FLAGS_coinTest) {
170 header.appendf(" -c");
171 }
172#endif
173 if (FLAGS_dumpOp) {
174 header.appendf(" -d");
175 }
Cary Clark59d5a0e2017-01-23 14:38:52 +0000176#ifdef SK_DEBUG
177 if (FLAGS_runFail) {
178 header.appendf(" -f");
179 }
180#endif
caryclark13260682016-10-24 05:10:14 -0700181 if (FLAGS_verbose) {
182 header.appendf(" -v");
183 }
184 if (FLAGS_veryVerbose) {
185 header.appendf(" -V");
186 }
187 if (FLAGS_extendedTest) {
188 header.appendf(" -x");
189 }
190 if (FLAGS_verifyOp) {
191 header.appendf(" -y");
192 }
reed@google.com91d449e2011-10-26 15:25:18 +0000193#ifdef SK_DEBUG
194 header.append(" SK_DEBUG");
195#else
196 header.append(" SK_RELEASE");
197#endif
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000198 if (FLAGS_veryVerbose) {
199 header.appendf("\n");
200 }
kkinnunen297aaf92015-02-19 06:32:12 -0800201 SkDebugf("%s", header.c_str());
reed@google.com91d449e2011-10-26 15:25:18 +0000202 }
203
reed@android.com80e39a72009-04-02 16:59:40 +0000204
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000205 // Count tests first.
206 int total = 0;
207 int toRun = 0;
sglez@google.com586db932013-07-24 17:24:23 +0000208
Hal Canary972eba32018-07-30 17:07:07 -0400209 for (const Test& test : TestRegistry::Range()) {
halcanary87f3ba42015-01-20 09:30:20 -0800210 if (should_run(test.name, test.needsGpu)) {
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000211 toRun++;
212 }
213 total++;
214 }
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000215
216 // Now run them.
bungeman@google.com5af16f82011-09-02 15:06:44 +0000217 int skipCount = 0;
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000218
mtklein406654b2014-09-03 15:34:37 -0700219 SkTaskGroup::Enabler enabled(FLAGS_threads);
220 SkTaskGroup cpuTests;
halcanary87f3ba42015-01-20 09:30:20 -0800221 SkTArray<const Test*> gpuTests;
commit-bot@chromium.org261c6662014-01-02 16:19:53 +0000222
halcanary87f3ba42015-01-20 09:30:20 -0800223 Status status(toRun);
Hal Canary972eba32018-07-30 17:07:07 -0400224
225 for (const Test& test : TestRegistry::Range()) {
halcanary87f3ba42015-01-20 09:30:20 -0800226 if (!should_run(test.name, test.needsGpu)) {
bungeman@google.com5af16f82011-09-02 15:06:44 +0000227 ++skipCount;
halcanary87f3ba42015-01-20 09:30:20 -0800228 } else if (test.needsGpu) {
229 gpuTests.push_back(&test);
bungeman@google.com5af16f82011-09-02 15:06:44 +0000230 } else {
mtklein048494c2016-02-16 19:06:15 -0800231 cpuTests.add(SkTestRunnable(test, &status));
bungeman@google.com5af16f82011-09-02 15:06:44 +0000232 }
reed@android.comed673312009-02-27 16:24:51 +0000233 }
reed@android.com57b799e2009-04-01 20:26:42 +0000234
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +0000235 // Run GPU tests on this thread.
236 for (int i = 0; i < gpuTests.count(); i++) {
Brian Salomondcfca432017-11-15 15:48:03 -0500237 SkTestRunnable(*gpuTests[i], &status)();
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000238 }
239
commit-bot@chromium.orga7538ba2013-10-10 18:49:04 +0000240 // Block until threaded tests finish.
mtklein406654b2014-09-03 15:34:37 -0700241 cpuTests.wait();
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000242
commit-bot@chromium.org261c6662014-01-02 16:19:53 +0000243 if (FLAGS_verbose) {
halcanary87f3ba42015-01-20 09:30:20 -0800244 SkDebugf(
245 "\nFinished %d tests, %d failures, %d skipped. "
246 "(%d internal tests)",
247 toRun, status.failCount(), skipCount, status.testCount());
caryclark26ad22a2015-10-16 09:03:38 -0700248 if (gVerboseFinalize) {
249 (*gVerboseFinalize)();
250 }
caryclark@google.comd54e1e92013-04-10 15:57:31 +0000251 }
reed@google.coma2769752012-07-22 22:33:05 +0000252
commit-bot@chromium.org261c6662014-01-02 16:19:53 +0000253 SkDebugf("\n");
Cary Clarkab87d7a2016-10-04 10:01:04 -0400254#if DEBUG_COIN
255 if (FLAGS_coinTest) {
256 SkPathOpsDebug::DumpCoinDict();
257 }
258#endif
Cary Clarkf3949122018-08-07 16:38:21 -0400259 if (PathOpsDebug::gJson) {
260 fprintf(PathOpsDebug::gOut, "\n}\n");
261 fclose(PathOpsDebug::gOut);
262 }
halcanary87f3ba42015-01-20 09:30:20 -0800263 return (status.failCount() == 0) ? 0 : 1;
reed@android.comed673312009-02-27 16:24:51 +0000264}