blob: 6b7df71c2847b749d9b9e7fe38ed07698a65e7e9 [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"
robertphillips@google.combdb1be52012-09-07 18:24:43 +000025#endif
26
reed@android.comed673312009-02-27 16:24:51 +000027using namespace skiatest;
bsalomon3724e572016-03-30 18:56:19 -070028using namespace sk_gpu_test;
reed@android.comed673312009-02-27 16:24:51 +000029
caryclark13260682016-10-24 05:10:14 -070030DEFINE_bool2(dumpOp, d, false, "dump the pathOps to a file to recover mid-crash.");
commit-bot@chromium.orgba59d642013-04-11 16:54:09 +000031DEFINE_bool2(extendedTest, x, false, "run extended tests for pathOps.");
Cary Clark59d5a0e2017-01-23 14:38:52 +000032DEFINE_bool2(runFail, f, false, "check for success on tests known to fail.");
caryclark13260682016-10-24 05:10:14 -070033DEFINE_bool2(verifyOp, y, false, "compare the pathOps result against a region.");
34
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:
halcanary87f3ba42015-01-20 09:30:20 -080085 SkTestRunnable(const Test& test,
86 Status* status,
halcanary96fcdcc2015-08-27 07:41:13 -070087 GrContextFactory* grContextFactory = nullptr)
halcanary87f3ba42015-01-20 09:30:20 -080088 : fTest(test), fStatus(status), fGrContextFactory(grContextFactory) {}
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000089
mtklein048494c2016-02-16 19:06:15 -080090 void operator()() {
halcanary87f3ba42015-01-20 09:30:20 -080091 struct TestReporter : public skiatest::Reporter {
92 public:
Cary Clarka35c68d2016-10-04 10:59:29 -040093 TestReporter() : fStats(nullptr), fError(false), fTestCount(0) {}
mtklein36352bf2015-03-25 18:17:31 -070094 void bumpTestCount() override { ++fTestCount; }
95 bool allowExtendedTest() const override {
halcanary87f3ba42015-01-20 09:30:20 -080096 return FLAGS_extendedTest;
97 }
mtklein36352bf2015-03-25 18:17:31 -070098 bool verbose() const override { return FLAGS_veryVerbose; }
99 void reportFailed(const skiatest::Failure& failure) override {
halcanary87f3ba42015-01-20 09:30:20 -0800100 SkDebugf("\nFAILED: %s", failure.toString().c_str());
101 fError = true;
102 }
caryclarkf114a9b2016-10-06 07:06:00 -0700103 void* stats() const override { return fStats; }
Cary Clarkab87d7a2016-10-04 10:01:04 -0400104 void* fStats;
halcanary87f3ba42015-01-20 09:30:20 -0800105 bool fError;
106 int fTestCount;
107 } reporter;
108
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700109 const Timer timer;
halcanary87f3ba42015-01-20 09:30:20 -0800110 fTest.proc(&reporter, fGrContextFactory);
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700111 SkMSec elapsed = timer.elapsedMsInt();
halcanary87f3ba42015-01-20 09:30:20 -0800112 if (reporter.fError) {
113 fStatus->reportFailure();
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000114 }
halcanary87f3ba42015-01-20 09:30:20 -0800115 fStatus->endTest(fTest.name, !reporter.fError, elapsed,
116 reporter.fTestCount);
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000117 }
118
119private:
halcanary87f3ba42015-01-20 09:30:20 -0800120 Test fTest;
121 Status* fStatus;
122 GrContextFactory* fGrContextFactory;
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000123};
commit-bot@chromium.orgba59d642013-04-11 16:54:09 +0000124
commit-bot@chromium.org5a47b092014-01-30 15:30:50 +0000125static bool should_run(const char* testName, bool isGPUTest) {
126 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, testName)) {
127 return false;
128 }
129 if (!FLAGS_cpu && !isGPUTest) {
130 return false;
131 }
132 if (!FLAGS_gpu && isGPUTest) {
133 return false;
134 }
135 return true;
136}
137
Mike Kleinbe28ee22017-02-06 12:46:20 -0500138int main(int argc, char** argv) {
139 SkCommandLineFlags::Parse(argc, argv);
Cary Clark918fb1f2016-11-15 13:22:25 -0500140#if DEBUG_DUMP_VERIFY
caryclark13260682016-10-24 05:10:14 -0700141 SkPathOpsDebug::gDumpOp = FLAGS_dumpOp;
142 SkPathOpsDebug::gVerifyOp = FLAGS_verifyOp;
143#endif
Cary Clark59d5a0e2017-01-23 14:38:52 +0000144 SkPathOpsDebug::gRunFail = FLAGS_runFail;
caryclark13260682016-10-24 05:10:14 -0700145 SkPathOpsDebug::gVeryVerbose = FLAGS_veryVerbose;
mtklein30e6e2a2014-06-18 11:44:15 -0700146 SetupCrashHandler();
commit-bot@chromium.orgba59d642013-04-11 16:54:09 +0000147
tfarinaa71d3af2014-11-07 06:12:30 -0800148 SkAutoGraphics ag;
bungeman@google.com5af16f82011-09-02 15:06:44 +0000149
reed@google.com91d449e2011-10-26 15:25:18 +0000150 {
151 SkString header("Skia UnitTests:");
reed@google.com9aff1482013-04-11 18:27:52 +0000152 if (!FLAGS_match.isEmpty()) {
caryclark@google.comb631eec2013-05-02 13:14:40 +0000153 header.appendf(" --match");
154 for (int index = 0; index < FLAGS_match.count(); ++index) {
155 header.appendf(" %s", FLAGS_match[index]);
156 }
reed@google.com91d449e2011-10-26 15:25:18 +0000157 }
halcanary87f3ba42015-01-20 09:30:20 -0800158 SkString tmpDir = skiatest::GetTmpDir();
scroggo@google.comc76218d2013-06-06 14:59:56 +0000159 if (!tmpDir.isEmpty()) {
160 header.appendf(" --tmpDir %s", tmpDir.c_str());
djsollen@google.comcb626502013-03-20 13:48:20 +0000161 }
tfarinabcbc1782014-06-18 14:32:48 -0700162 SkString resourcePath = GetResourcePath();
scroggo@google.comc76218d2013-06-06 14:59:56 +0000163 if (!resourcePath.isEmpty()) {
164 header.appendf(" --resourcePath %s", resourcePath.c_str());
reed@google.com789c6f22013-02-25 20:24:24 +0000165 }
caryclark13260682016-10-24 05:10:14 -0700166#if DEBUG_COIN
167 if (FLAGS_coinTest) {
168 header.appendf(" -c");
169 }
170#endif
171 if (FLAGS_dumpOp) {
172 header.appendf(" -d");
173 }
Cary Clark59d5a0e2017-01-23 14:38:52 +0000174#ifdef SK_DEBUG
175 if (FLAGS_runFail) {
176 header.appendf(" -f");
177 }
178#endif
caryclark13260682016-10-24 05:10:14 -0700179 if (FLAGS_verbose) {
180 header.appendf(" -v");
181 }
182 if (FLAGS_veryVerbose) {
183 header.appendf(" -V");
184 }
185 if (FLAGS_extendedTest) {
186 header.appendf(" -x");
187 }
188 if (FLAGS_verifyOp) {
189 header.appendf(" -y");
190 }
reed@google.com91d449e2011-10-26 15:25:18 +0000191#ifdef SK_DEBUG
192 header.append(" SK_DEBUG");
193#else
194 header.append(" SK_RELEASE");
195#endif
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000196 if (FLAGS_veryVerbose) {
197 header.appendf("\n");
198 }
kkinnunen297aaf92015-02-19 06:32:12 -0800199 SkDebugf("%s", header.c_str());
reed@google.com91d449e2011-10-26 15:25:18 +0000200 }
201
reed@android.com80e39a72009-04-02 16:59:40 +0000202
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000203 // Count tests first.
204 int total = 0;
205 int toRun = 0;
sglez@google.com586db932013-07-24 17:24:23 +0000206
halcanary87f3ba42015-01-20 09:30:20 -0800207 for (const TestRegistry* iter = TestRegistry::Head(); iter;
208 iter = iter->next()) {
209 const Test& test = iter->factory();
210 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);
224 for (const TestRegistry* iter = TestRegistry::Head(); iter;
225 iter = iter->next()) {
226 const Test& test = iter->factory();
227 if (!should_run(test.name, test.needsGpu)) {
bungeman@google.com5af16f82011-09-02 15:06:44 +0000228 ++skipCount;
halcanary87f3ba42015-01-20 09:30:20 -0800229 } else if (test.needsGpu) {
230 gpuTests.push_back(&test);
bungeman@google.com5af16f82011-09-02 15:06:44 +0000231 } else {
mtklein048494c2016-02-16 19:06:15 -0800232 cpuTests.add(SkTestRunnable(test, &status));
bungeman@google.com5af16f82011-09-02 15:06:44 +0000233 }
reed@android.comed673312009-02-27 16:24:51 +0000234 }
reed@android.com57b799e2009-04-01 20:26:42 +0000235
halcanary96fcdcc2015-08-27 07:41:13 -0700236 GrContextFactory* grContextFactoryPtr = nullptr;
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +0000237#if SK_SUPPORT_GPU
238 // Give GPU tests a context factory if that makes sense on this machine.
239 GrContextFactory grContextFactory;
halcanary87f3ba42015-01-20 09:30:20 -0800240 grContextFactoryPtr = &grContextFactory;
241
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +0000242#endif
243
244 // Run GPU tests on this thread.
245 for (int i = 0; i < gpuTests.count(); i++) {
mtklein048494c2016-02-16 19:06:15 -0800246 SkTestRunnable(*gpuTests[i], &status, grContextFactoryPtr)();
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000247 }
248
commit-bot@chromium.orga7538ba2013-10-10 18:49:04 +0000249 // Block until threaded tests finish.
mtklein406654b2014-09-03 15:34:37 -0700250 cpuTests.wait();
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000251
commit-bot@chromium.org261c6662014-01-02 16:19:53 +0000252 if (FLAGS_verbose) {
halcanary87f3ba42015-01-20 09:30:20 -0800253 SkDebugf(
254 "\nFinished %d tests, %d failures, %d skipped. "
255 "(%d internal tests)",
256 toRun, status.failCount(), skipCount, status.testCount());
caryclark26ad22a2015-10-16 09:03:38 -0700257 if (gVerboseFinalize) {
258 (*gVerboseFinalize)();
259 }
caryclark@google.comd54e1e92013-04-10 15:57:31 +0000260 }
reed@google.coma2769752012-07-22 22:33:05 +0000261
commit-bot@chromium.org261c6662014-01-02 16:19:53 +0000262 SkDebugf("\n");
Cary Clarkab87d7a2016-10-04 10:01:04 -0400263#if DEBUG_COIN
264 if (FLAGS_coinTest) {
265 SkPathOpsDebug::DumpCoinDict();
266 }
267#endif
268
halcanary87f3ba42015-01-20 09:30:20 -0800269 return (status.failCount() == 0) ? 0 : 1;
reed@android.comed673312009-02-27 16:24:51 +0000270}