blob: 1d90cdc0660623b4f6fc85dae307d255618853c5 [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"
tfarinabcbc1782014-06-18 14:32:48 -070010#include "Resources.h"
herb62a69c22015-09-29 11:47:45 -070011#include "SkAtomics.h"
caryclark17f0b6d2014-07-22 10:15:34 -070012#include "SkCommonFlags.h"
reed@android.com5e5adfd2009-03-07 03:39:23 +000013#include "SkGraphics.h"
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +000014#include "SkOSFile.h"
Cary Clarkab87d7a2016-10-04 10:01:04 -040015#include "SkPathOpsDebug.h"
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000016#include "SkTArray.h"
mtklein406654b2014-09-03 15:34:37 -070017#include "SkTaskGroup.h"
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000018#include "SkTemplates.h"
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +000019#include "SkTime.h"
reed@android.comed673312009-02-27 16:24:51 +000020#include "Test.h"
21
robertphillips@google.combdb1be52012-09-07 18:24:43 +000022#if SK_SUPPORT_GPU
23#include "GrContext.h"
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +000024#include "GrContextFactory.h"
Brian Salomondcfca432017-11-15 15:48:03 -050025#else
26struct GrContextOptions {};
robertphillips@google.combdb1be52012-09-07 18:24:43 +000027#endif
28
reed@android.comed673312009-02-27 16:24:51 +000029using namespace skiatest;
bsalomon3724e572016-03-30 18:56:19 -070030using namespace sk_gpu_test;
reed@android.comed673312009-02-27 16:24:51 +000031
caryclark13260682016-10-24 05:10:14 -070032DEFINE_bool2(dumpOp, d, false, "dump the pathOps to a file to recover mid-crash.");
commit-bot@chromium.orgba59d642013-04-11 16:54:09 +000033DEFINE_bool2(extendedTest, x, false, "run extended tests for pathOps.");
Cary Clark59d5a0e2017-01-23 14:38:52 +000034DEFINE_bool2(runFail, f, false, "check for success on tests known to fail.");
caryclark13260682016-10-24 05:10:14 -070035DEFINE_bool2(verifyOp, y, false, "compare the pathOps result against a region.");
36
Cary Clarkab87d7a2016-10-04 10:01:04 -040037#if DEBUG_COIN
38DEFINE_bool2(coinTest, c, false, "detect unused coincidence algorithms.");
39#endif
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000040
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000041// need to explicitly declare this, or we get some weird infinite loop llist
42template TestRegistry* TestRegistry::gHead;
caryclark26ad22a2015-10-16 09:03:38 -070043void (*gVerboseFinalize)() = nullptr;
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000044
halcanary87f3ba42015-01-20 09:30:20 -080045// The threads report back to this object when they are done.
46class Status {
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000047public:
halcanary87f3ba42015-01-20 09:30:20 -080048 explicit Status(int total)
49 : fDone(0), fTestCount(0), fFailCount(0), fTotal(total) {}
50 // Threadsafe.
51 void endTest(const char* testName,
52 bool success,
53 SkMSec elapsed,
54 int testCount) {
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000055 const int done = 1 + sk_atomic_inc(&fDone);
halcanary87f3ba42015-01-20 09:30:20 -080056 for (int i = 0; i < testCount; ++i) {
57 sk_atomic_inc(&fTestCount);
58 }
59 if (!success) {
60 SkDebugf("\n---- %s FAILED", testName);
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000061 }
62
63 SkString prefix(kSkOverwriteLine);
64 SkString time;
65 if (FLAGS_verbose) {
66 prefix.printf("\n");
halcanary87f3ba42015-01-20 09:30:20 -080067 time.printf("%5dms ", elapsed);
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000068 }
halcanary87f3ba42015-01-20 09:30:20 -080069 SkDebugf("%s[%3d/%3d] %s%s", prefix.c_str(), done, fTotal, time.c_str(),
70 testName);
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000071 }
72
halcanary87f3ba42015-01-20 09:30:20 -080073 void reportFailure() { sk_atomic_inc(&fFailCount); }
74
75 int32_t testCount() { return fTestCount; }
76 int32_t failCount() { return fFailCount; }
77
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000078private:
79 int32_t fDone; // atomic
halcanary87f3ba42015-01-20 09:30:20 -080080 int32_t fTestCount; // atomic
81 int32_t fFailCount; // atomic
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000082 const int fTotal;
83};
84
mtklein048494c2016-02-16 19:06:15 -080085class SkTestRunnable {
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000086public:
Brian Salomondcfca432017-11-15 15:48:03 -050087 SkTestRunnable(const Test& test, Status* status) : fTest(test), fStatus(status) {}
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000088
Brian Salomondcfca432017-11-15 15:48:03 -050089 void operator()() {
90 struct TestReporter : public skiatest::Reporter {
91 public:
92 TestReporter() : fStats(nullptr), fError(false), fTestCount(0) {}
93 void bumpTestCount() override { ++fTestCount; }
94 bool allowExtendedTest() const override { return FLAGS_extendedTest; }
95 bool verbose() const override { return FLAGS_veryVerbose; }
96 void reportFailed(const skiatest::Failure& failure) override {
97 SkDebugf("\nFAILED: %s", failure.toString().c_str());
98 fError = true;
99 }
100 void* stats() const override { return fStats; }
101 void* fStats;
102 bool fError;
103 int fTestCount;
104 } reporter;
halcanary87f3ba42015-01-20 09:30:20 -0800105
Brian Salomondcfca432017-11-15 15:48:03 -0500106 const Timer timer;
107 fTest.proc(&reporter, GrContextOptions());
108 SkMSec elapsed = timer.elapsedMsInt();
109 if (reporter.fError) {
110 fStatus->reportFailure();
111 }
112 fStatus->endTest(fTest.name, !reporter.fError, elapsed, reporter.fTestCount);
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000113 }
114
115private:
halcanary87f3ba42015-01-20 09:30:20 -0800116 Test fTest;
117 Status* fStatus;
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000118};
commit-bot@chromium.orgba59d642013-04-11 16:54:09 +0000119
commit-bot@chromium.org5a47b092014-01-30 15:30:50 +0000120static bool should_run(const char* testName, bool isGPUTest) {
121 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, testName)) {
122 return false;
123 }
124 if (!FLAGS_cpu && !isGPUTest) {
125 return false;
126 }
127 if (!FLAGS_gpu && isGPUTest) {
128 return false;
129 }
130 return true;
131}
132
Mike Kleinbe28ee22017-02-06 12:46:20 -0500133int main(int argc, char** argv) {
134 SkCommandLineFlags::Parse(argc, argv);
Cary Clark918fb1f2016-11-15 13:22:25 -0500135#if DEBUG_DUMP_VERIFY
caryclark13260682016-10-24 05:10:14 -0700136 SkPathOpsDebug::gDumpOp = FLAGS_dumpOp;
137 SkPathOpsDebug::gVerifyOp = FLAGS_verifyOp;
138#endif
Cary Clark59d5a0e2017-01-23 14:38:52 +0000139 SkPathOpsDebug::gRunFail = FLAGS_runFail;
caryclark13260682016-10-24 05:10:14 -0700140 SkPathOpsDebug::gVeryVerbose = FLAGS_veryVerbose;
mtklein30e6e2a2014-06-18 11:44:15 -0700141 SetupCrashHandler();
commit-bot@chromium.orgba59d642013-04-11 16:54:09 +0000142
tfarinaa71d3af2014-11-07 06:12:30 -0800143 SkAutoGraphics ag;
bungeman@google.com5af16f82011-09-02 15:06:44 +0000144
reed@google.com91d449e2011-10-26 15:25:18 +0000145 {
146 SkString header("Skia UnitTests:");
reed@google.com9aff1482013-04-11 18:27:52 +0000147 if (!FLAGS_match.isEmpty()) {
caryclark@google.comb631eec2013-05-02 13:14:40 +0000148 header.appendf(" --match");
149 for (int index = 0; index < FLAGS_match.count(); ++index) {
150 header.appendf(" %s", FLAGS_match[index]);
151 }
reed@google.com91d449e2011-10-26 15:25:18 +0000152 }
halcanary87f3ba42015-01-20 09:30:20 -0800153 SkString tmpDir = skiatest::GetTmpDir();
scroggo@google.comc76218d2013-06-06 14:59:56 +0000154 if (!tmpDir.isEmpty()) {
155 header.appendf(" --tmpDir %s", tmpDir.c_str());
djsollen@google.comcb626502013-03-20 13:48:20 +0000156 }
tfarinabcbc1782014-06-18 14:32:48 -0700157 SkString resourcePath = GetResourcePath();
scroggo@google.comc76218d2013-06-06 14:59:56 +0000158 if (!resourcePath.isEmpty()) {
159 header.appendf(" --resourcePath %s", resourcePath.c_str());
reed@google.com789c6f22013-02-25 20:24:24 +0000160 }
caryclark13260682016-10-24 05:10:14 -0700161#if DEBUG_COIN
162 if (FLAGS_coinTest) {
163 header.appendf(" -c");
164 }
165#endif
166 if (FLAGS_dumpOp) {
167 header.appendf(" -d");
168 }
Cary Clark59d5a0e2017-01-23 14:38:52 +0000169#ifdef SK_DEBUG
170 if (FLAGS_runFail) {
171 header.appendf(" -f");
172 }
173#endif
caryclark13260682016-10-24 05:10:14 -0700174 if (FLAGS_verbose) {
175 header.appendf(" -v");
176 }
177 if (FLAGS_veryVerbose) {
178 header.appendf(" -V");
179 }
180 if (FLAGS_extendedTest) {
181 header.appendf(" -x");
182 }
183 if (FLAGS_verifyOp) {
184 header.appendf(" -y");
185 }
reed@google.com91d449e2011-10-26 15:25:18 +0000186#ifdef SK_DEBUG
187 header.append(" SK_DEBUG");
188#else
189 header.append(" SK_RELEASE");
190#endif
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000191 if (FLAGS_veryVerbose) {
192 header.appendf("\n");
193 }
kkinnunen297aaf92015-02-19 06:32:12 -0800194 SkDebugf("%s", header.c_str());
reed@google.com91d449e2011-10-26 15:25:18 +0000195 }
196
reed@android.com80e39a72009-04-02 16:59:40 +0000197
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000198 // Count tests first.
199 int total = 0;
200 int toRun = 0;
sglez@google.com586db932013-07-24 17:24:23 +0000201
halcanary87f3ba42015-01-20 09:30:20 -0800202 for (const TestRegistry* iter = TestRegistry::Head(); iter;
203 iter = iter->next()) {
204 const Test& test = iter->factory();
205 if (should_run(test.name, test.needsGpu)) {
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000206 toRun++;
207 }
208 total++;
209 }
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000210
211 // Now run them.
bungeman@google.com5af16f82011-09-02 15:06:44 +0000212 int skipCount = 0;
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000213
mtklein406654b2014-09-03 15:34:37 -0700214 SkTaskGroup::Enabler enabled(FLAGS_threads);
215 SkTaskGroup cpuTests;
halcanary87f3ba42015-01-20 09:30:20 -0800216 SkTArray<const Test*> gpuTests;
commit-bot@chromium.org261c6662014-01-02 16:19:53 +0000217
halcanary87f3ba42015-01-20 09:30:20 -0800218 Status status(toRun);
219 for (const TestRegistry* iter = TestRegistry::Head(); iter;
220 iter = iter->next()) {
221 const Test& test = iter->factory();
222 if (!should_run(test.name, test.needsGpu)) {
bungeman@google.com5af16f82011-09-02 15:06:44 +0000223 ++skipCount;
halcanary87f3ba42015-01-20 09:30:20 -0800224 } else if (test.needsGpu) {
225 gpuTests.push_back(&test);
bungeman@google.com5af16f82011-09-02 15:06:44 +0000226 } else {
mtklein048494c2016-02-16 19:06:15 -0800227 cpuTests.add(SkTestRunnable(test, &status));
bungeman@google.com5af16f82011-09-02 15:06:44 +0000228 }
reed@android.comed673312009-02-27 16:24:51 +0000229 }
reed@android.com57b799e2009-04-01 20:26:42 +0000230
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +0000231 // Run GPU tests on this thread.
232 for (int i = 0; i < gpuTests.count(); i++) {
Brian Salomondcfca432017-11-15 15:48:03 -0500233 SkTestRunnable(*gpuTests[i], &status)();
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000234 }
235
commit-bot@chromium.orga7538ba2013-10-10 18:49:04 +0000236 // Block until threaded tests finish.
mtklein406654b2014-09-03 15:34:37 -0700237 cpuTests.wait();
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000238
commit-bot@chromium.org261c6662014-01-02 16:19:53 +0000239 if (FLAGS_verbose) {
halcanary87f3ba42015-01-20 09:30:20 -0800240 SkDebugf(
241 "\nFinished %d tests, %d failures, %d skipped. "
242 "(%d internal tests)",
243 toRun, status.failCount(), skipCount, status.testCount());
caryclark26ad22a2015-10-16 09:03:38 -0700244 if (gVerboseFinalize) {
245 (*gVerboseFinalize)();
246 }
caryclark@google.comd54e1e92013-04-10 15:57:31 +0000247 }
reed@google.coma2769752012-07-22 22:33:05 +0000248
commit-bot@chromium.org261c6662014-01-02 16:19:53 +0000249 SkDebugf("\n");
Cary Clarkab87d7a2016-10-04 10:01:04 -0400250#if DEBUG_COIN
251 if (FLAGS_coinTest) {
252 SkPathOpsDebug::DumpCoinDict();
253 }
254#endif
255
halcanary87f3ba42015-01-20 09:30:20 -0800256 return (status.failCount() == 0) ? 0 : 1;
reed@android.comed673312009-02-27 16:24:51 +0000257}