blob: 7dfe0035d867e9264237fcf0c2d5eeb5083e5d29 [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;
146 }
mtklein30e6e2a2014-06-18 11:44:15 -0700147 SetupCrashHandler();
commit-bot@chromium.orgba59d642013-04-11 16:54:09 +0000148
tfarinaa71d3af2014-11-07 06:12:30 -0800149 SkAutoGraphics ag;
bungeman@google.com5af16f82011-09-02 15:06:44 +0000150
reed@google.com91d449e2011-10-26 15:25:18 +0000151 {
152 SkString header("Skia UnitTests:");
reed@google.com9aff1482013-04-11 18:27:52 +0000153 if (!FLAGS_match.isEmpty()) {
caryclark@google.comb631eec2013-05-02 13:14:40 +0000154 header.appendf(" --match");
155 for (int index = 0; index < FLAGS_match.count(); ++index) {
156 header.appendf(" %s", FLAGS_match[index]);
157 }
reed@google.com91d449e2011-10-26 15:25:18 +0000158 }
halcanary87f3ba42015-01-20 09:30:20 -0800159 SkString tmpDir = skiatest::GetTmpDir();
scroggo@google.comc76218d2013-06-06 14:59:56 +0000160 if (!tmpDir.isEmpty()) {
161 header.appendf(" --tmpDir %s", tmpDir.c_str());
djsollen@google.comcb626502013-03-20 13:48:20 +0000162 }
tfarinabcbc1782014-06-18 14:32:48 -0700163 SkString resourcePath = GetResourcePath();
scroggo@google.comc76218d2013-06-06 14:59:56 +0000164 if (!resourcePath.isEmpty()) {
165 header.appendf(" --resourcePath %s", resourcePath.c_str());
reed@google.com789c6f22013-02-25 20:24:24 +0000166 }
caryclark13260682016-10-24 05:10:14 -0700167#if DEBUG_COIN
168 if (FLAGS_coinTest) {
169 header.appendf(" -c");
170 }
171#endif
172 if (FLAGS_dumpOp) {
173 header.appendf(" -d");
174 }
Cary Clark59d5a0e2017-01-23 14:38:52 +0000175#ifdef SK_DEBUG
176 if (FLAGS_runFail) {
177 header.appendf(" -f");
178 }
179#endif
caryclark13260682016-10-24 05:10:14 -0700180 if (FLAGS_verbose) {
181 header.appendf(" -v");
182 }
183 if (FLAGS_veryVerbose) {
184 header.appendf(" -V");
185 }
186 if (FLAGS_extendedTest) {
187 header.appendf(" -x");
188 }
189 if (FLAGS_verifyOp) {
190 header.appendf(" -y");
191 }
reed@google.com91d449e2011-10-26 15:25:18 +0000192#ifdef SK_DEBUG
193 header.append(" SK_DEBUG");
194#else
195 header.append(" SK_RELEASE");
196#endif
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000197 if (FLAGS_veryVerbose) {
198 header.appendf("\n");
199 }
kkinnunen297aaf92015-02-19 06:32:12 -0800200 SkDebugf("%s", header.c_str());
reed@google.com91d449e2011-10-26 15:25:18 +0000201 }
202
reed@android.com80e39a72009-04-02 16:59:40 +0000203
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000204 // Count tests first.
205 int total = 0;
206 int toRun = 0;
sglez@google.com586db932013-07-24 17:24:23 +0000207
Hal Canary972eba32018-07-30 17:07:07 -0400208 for (const Test& test : TestRegistry::Range()) {
halcanary87f3ba42015-01-20 09:30:20 -0800209 if (should_run(test.name, test.needsGpu)) {
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000210 toRun++;
211 }
212 total++;
213 }
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000214
215 // Now run them.
bungeman@google.com5af16f82011-09-02 15:06:44 +0000216 int skipCount = 0;
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000217
mtklein406654b2014-09-03 15:34:37 -0700218 SkTaskGroup::Enabler enabled(FLAGS_threads);
219 SkTaskGroup cpuTests;
halcanary87f3ba42015-01-20 09:30:20 -0800220 SkTArray<const Test*> gpuTests;
commit-bot@chromium.org261c6662014-01-02 16:19:53 +0000221
halcanary87f3ba42015-01-20 09:30:20 -0800222 Status status(toRun);
Hal Canary972eba32018-07-30 17:07:07 -0400223
224 for (const Test& test : TestRegistry::Range()) {
halcanary87f3ba42015-01-20 09:30:20 -0800225 if (!should_run(test.name, test.needsGpu)) {
bungeman@google.com5af16f82011-09-02 15:06:44 +0000226 ++skipCount;
halcanary87f3ba42015-01-20 09:30:20 -0800227 } else if (test.needsGpu) {
228 gpuTests.push_back(&test);
bungeman@google.com5af16f82011-09-02 15:06:44 +0000229 } else {
mtklein048494c2016-02-16 19:06:15 -0800230 cpuTests.add(SkTestRunnable(test, &status));
bungeman@google.com5af16f82011-09-02 15:06:44 +0000231 }
reed@android.comed673312009-02-27 16:24:51 +0000232 }
reed@android.com57b799e2009-04-01 20:26:42 +0000233
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +0000234 // Run GPU tests on this thread.
235 for (int i = 0; i < gpuTests.count(); i++) {
Brian Salomondcfca432017-11-15 15:48:03 -0500236 SkTestRunnable(*gpuTests[i], &status)();
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000237 }
238
commit-bot@chromium.orga7538ba2013-10-10 18:49:04 +0000239 // Block until threaded tests finish.
mtklein406654b2014-09-03 15:34:37 -0700240 cpuTests.wait();
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000241
commit-bot@chromium.org261c6662014-01-02 16:19:53 +0000242 if (FLAGS_verbose) {
halcanary87f3ba42015-01-20 09:30:20 -0800243 SkDebugf(
244 "\nFinished %d tests, %d failures, %d skipped. "
245 "(%d internal tests)",
246 toRun, status.failCount(), skipCount, status.testCount());
caryclark26ad22a2015-10-16 09:03:38 -0700247 if (gVerboseFinalize) {
248 (*gVerboseFinalize)();
249 }
caryclark@google.comd54e1e92013-04-10 15:57:31 +0000250 }
reed@google.coma2769752012-07-22 22:33:05 +0000251
commit-bot@chromium.org261c6662014-01-02 16:19:53 +0000252 SkDebugf("\n");
Cary Clarkab87d7a2016-10-04 10:01:04 -0400253#if DEBUG_COIN
254 if (FLAGS_coinTest) {
255 SkPathOpsDebug::DumpCoinDict();
256 }
257#endif
Cary Clarkf3949122018-08-07 16:38:21 -0400258 if (PathOpsDebug::gJson) {
259 fprintf(PathOpsDebug::gOut, "\n}\n");
260 fclose(PathOpsDebug::gOut);
261 }
halcanary87f3ba42015-01-20 09:30:20 -0800262 return (status.failCount() == 0) ? 0 : 1;
reed@android.comed673312009-02-27 16:24:51 +0000263}