blob: 5d3169ba1b50fee0e214273de1297dcfc59468cb [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"
Mike Klein0ec1c572018-12-04 11:52:51 -05009#include "GrContext.h"
10#include "GrContextFactory.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000011#include "OverwriteLine.h"
Cary Clarkf3949122018-08-07 16:38:21 -040012#include "PathOpsDebug.h"
tfarinabcbc1782014-06-18 14:32:48 -070013#include "Resources.h"
caryclark17f0b6d2014-07-22 10:15:34 -070014#include "SkCommonFlags.h"
reed@android.com5e5adfd2009-03-07 03:39:23 +000015#include "SkGraphics.h"
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +000016#include "SkOSFile.h"
Cary Clarkab87d7a2016-10-04 10:01:04 -040017#include "SkPathOpsDebug.h"
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000018#include "SkTArray.h"
mtklein406654b2014-09-03 15:34:37 -070019#include "SkTaskGroup.h"
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000020#include "SkTemplates.h"
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +000021#include "SkTime.h"
reed@android.comed673312009-02-27 16:24:51 +000022#include "Test.h"
Mike Klein0ec1c572018-12-04 11:52:51 -050023#include <atomic>
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
caryclark13260682016-10-24 05:10:14 -070028DEFINE_bool2(dumpOp, d, false, "dump the pathOps to a file to recover mid-crash.");
commit-bot@chromium.orgba59d642013-04-11 16:54:09 +000029DEFINE_bool2(extendedTest, x, false, "run extended tests for pathOps.");
Cary Clark59d5a0e2017-01-23 14:38:52 +000030DEFINE_bool2(runFail, f, false, "check for success on tests known to fail.");
caryclark13260682016-10-24 05:10:14 -070031DEFINE_bool2(verifyOp, y, false, "compare the pathOps result against a region.");
Cary Clarkf3949122018-08-07 16:38:21 -040032DEFINE_string2(json, J, "", "write json version of tests.");
caryclark13260682016-10-24 05:10:14 -070033
Cary Clarkab87d7a2016-10-04 10:01:04 -040034#if DEBUG_COIN
35DEFINE_bool2(coinTest, c, false, "detect unused coincidence algorithms.");
36#endif
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000037
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000038// need to explicitly declare this, or we get some weird infinite loop llist
39template TestRegistry* TestRegistry::gHead;
caryclark26ad22a2015-10-16 09:03:38 -070040void (*gVerboseFinalize)() = nullptr;
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000041
halcanary87f3ba42015-01-20 09:30:20 -080042// The threads report back to this object when they are done.
43class Status {
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000044public:
halcanary87f3ba42015-01-20 09:30:20 -080045 explicit Status(int total)
46 : fDone(0), fTestCount(0), fFailCount(0), fTotal(total) {}
47 // Threadsafe.
48 void endTest(const char* testName,
49 bool success,
50 SkMSec elapsed,
51 int testCount) {
Mike Klein0ec1c572018-12-04 11:52:51 -050052 const int done = ++fDone;
53 fTestCount += testCount;
halcanary87f3ba42015-01-20 09:30:20 -080054 if (!success) {
55 SkDebugf("\n---- %s FAILED", testName);
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000056 }
57
58 SkString prefix(kSkOverwriteLine);
59 SkString time;
60 if (FLAGS_verbose) {
61 prefix.printf("\n");
halcanary87f3ba42015-01-20 09:30:20 -080062 time.printf("%5dms ", elapsed);
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000063 }
halcanary87f3ba42015-01-20 09:30:20 -080064 SkDebugf("%s[%3d/%3d] %s%s", prefix.c_str(), done, fTotal, time.c_str(),
65 testName);
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000066 }
67
Mike Klein0ec1c572018-12-04 11:52:51 -050068 void reportFailure() { fFailCount++; }
halcanary87f3ba42015-01-20 09:30:20 -080069
70 int32_t testCount() { return fTestCount; }
71 int32_t failCount() { return fFailCount; }
72
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000073private:
Mike Klein0ec1c572018-12-04 11:52:51 -050074 std::atomic<int32_t> fDone;
75 std::atomic<int32_t> fTestCount;
76 std::atomic<int32_t> fFailCount;
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000077 const int fTotal;
78};
79
mtklein048494c2016-02-16 19:06:15 -080080class SkTestRunnable {
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000081public:
Brian Salomondcfca432017-11-15 15:48:03 -050082 SkTestRunnable(const Test& test, Status* status) : fTest(test), fStatus(status) {}
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000083
Brian Salomondcfca432017-11-15 15:48:03 -050084 void operator()() {
85 struct TestReporter : public skiatest::Reporter {
86 public:
87 TestReporter() : fStats(nullptr), fError(false), fTestCount(0) {}
88 void bumpTestCount() override { ++fTestCount; }
89 bool allowExtendedTest() const override { return FLAGS_extendedTest; }
90 bool verbose() const override { return FLAGS_veryVerbose; }
91 void reportFailed(const skiatest::Failure& failure) override {
92 SkDebugf("\nFAILED: %s", failure.toString().c_str());
93 fError = true;
94 }
95 void* stats() const override { return fStats; }
96 void* fStats;
97 bool fError;
98 int fTestCount;
99 } reporter;
halcanary87f3ba42015-01-20 09:30:20 -0800100
Brian Salomondcfca432017-11-15 15:48:03 -0500101 const Timer timer;
102 fTest.proc(&reporter, GrContextOptions());
103 SkMSec elapsed = timer.elapsedMsInt();
104 if (reporter.fError) {
105 fStatus->reportFailure();
106 }
107 fStatus->endTest(fTest.name, !reporter.fError, elapsed, reporter.fTestCount);
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000108 }
109
110private:
halcanary87f3ba42015-01-20 09:30:20 -0800111 Test fTest;
112 Status* fStatus;
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000113};
commit-bot@chromium.orgba59d642013-04-11 16:54:09 +0000114
commit-bot@chromium.org5a47b092014-01-30 15:30:50 +0000115static bool should_run(const char* testName, bool isGPUTest) {
116 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, testName)) {
117 return false;
118 }
119 if (!FLAGS_cpu && !isGPUTest) {
120 return false;
121 }
122 if (!FLAGS_gpu && isGPUTest) {
123 return false;
124 }
125 return true;
126}
127
Mike Kleinbe28ee22017-02-06 12:46:20 -0500128int main(int argc, char** argv) {
129 SkCommandLineFlags::Parse(argc, argv);
Cary Clark918fb1f2016-11-15 13:22:25 -0500130#if DEBUG_DUMP_VERIFY
caryclark13260682016-10-24 05:10:14 -0700131 SkPathOpsDebug::gDumpOp = FLAGS_dumpOp;
132 SkPathOpsDebug::gVerifyOp = FLAGS_verifyOp;
133#endif
Cary Clark59d5a0e2017-01-23 14:38:52 +0000134 SkPathOpsDebug::gRunFail = FLAGS_runFail;
caryclark13260682016-10-24 05:10:14 -0700135 SkPathOpsDebug::gVeryVerbose = FLAGS_veryVerbose;
Cary Clarkf3949122018-08-07 16:38:21 -0400136 PathOpsDebug::gOutFirst = true;
Cary Clark4533f3d2018-08-08 09:48:09 -0400137 PathOpsDebug::gCheckForDuplicateNames = false;
Cary Clark9c9611f2018-08-08 13:17:25 -0400138 PathOpsDebug::gOutputSVG = false;
Cary Clarkf3949122018-08-07 16:38:21 -0400139 if ((PathOpsDebug::gJson = !FLAGS_json.isEmpty())) {
140 PathOpsDebug::gOut = fopen(FLAGS_json[0], "wb");
141 fprintf(PathOpsDebug::gOut, "{\n");
142 FLAGS_threads = 0;
Cary Clarkda6289c2018-08-27 16:10:28 -0400143 PathOpsDebug::gMarkJsonFlaky = false;
Cary Clarkf3949122018-08-07 16:38:21 -0400144 }
mtklein30e6e2a2014-06-18 11:44:15 -0700145 SetupCrashHandler();
commit-bot@chromium.orgba59d642013-04-11 16:54:09 +0000146
tfarinaa71d3af2014-11-07 06:12:30 -0800147 SkAutoGraphics ag;
bungeman@google.com5af16f82011-09-02 15:06:44 +0000148
reed@google.com91d449e2011-10-26 15:25:18 +0000149 {
150 SkString header("Skia UnitTests:");
reed@google.com9aff1482013-04-11 18:27:52 +0000151 if (!FLAGS_match.isEmpty()) {
caryclark@google.comb631eec2013-05-02 13:14:40 +0000152 header.appendf(" --match");
153 for (int index = 0; index < FLAGS_match.count(); ++index) {
154 header.appendf(" %s", FLAGS_match[index]);
155 }
reed@google.com91d449e2011-10-26 15:25:18 +0000156 }
halcanary87f3ba42015-01-20 09:30:20 -0800157 SkString tmpDir = skiatest::GetTmpDir();
scroggo@google.comc76218d2013-06-06 14:59:56 +0000158 if (!tmpDir.isEmpty()) {
159 header.appendf(" --tmpDir %s", tmpDir.c_str());
djsollen@google.comcb626502013-03-20 13:48:20 +0000160 }
tfarinabcbc1782014-06-18 14:32:48 -0700161 SkString resourcePath = GetResourcePath();
scroggo@google.comc76218d2013-06-06 14:59:56 +0000162 if (!resourcePath.isEmpty()) {
163 header.appendf(" --resourcePath %s", resourcePath.c_str());
reed@google.com789c6f22013-02-25 20:24:24 +0000164 }
caryclark13260682016-10-24 05:10:14 -0700165#if DEBUG_COIN
166 if (FLAGS_coinTest) {
167 header.appendf(" -c");
168 }
169#endif
170 if (FLAGS_dumpOp) {
171 header.appendf(" -d");
172 }
Cary Clark59d5a0e2017-01-23 14:38:52 +0000173#ifdef SK_DEBUG
174 if (FLAGS_runFail) {
175 header.appendf(" -f");
176 }
177#endif
caryclark13260682016-10-24 05:10:14 -0700178 if (FLAGS_verbose) {
179 header.appendf(" -v");
180 }
181 if (FLAGS_veryVerbose) {
182 header.appendf(" -V");
183 }
184 if (FLAGS_extendedTest) {
185 header.appendf(" -x");
186 }
187 if (FLAGS_verifyOp) {
188 header.appendf(" -y");
189 }
reed@google.com91d449e2011-10-26 15:25:18 +0000190#ifdef SK_DEBUG
191 header.append(" SK_DEBUG");
192#else
193 header.append(" SK_RELEASE");
194#endif
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000195 if (FLAGS_veryVerbose) {
196 header.appendf("\n");
197 }
kkinnunen297aaf92015-02-19 06:32:12 -0800198 SkDebugf("%s", header.c_str());
reed@google.com91d449e2011-10-26 15:25:18 +0000199 }
200
reed@android.com80e39a72009-04-02 16:59:40 +0000201
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000202 // Count tests first.
203 int total = 0;
204 int toRun = 0;
sglez@google.com586db932013-07-24 17:24:23 +0000205
Hal Canary972eba32018-07-30 17:07:07 -0400206 for (const Test& test : TestRegistry::Range()) {
halcanary87f3ba42015-01-20 09:30:20 -0800207 if (should_run(test.name, test.needsGpu)) {
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000208 toRun++;
209 }
210 total++;
211 }
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000212
213 // Now run them.
bungeman@google.com5af16f82011-09-02 15:06:44 +0000214 int skipCount = 0;
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000215
mtklein406654b2014-09-03 15:34:37 -0700216 SkTaskGroup::Enabler enabled(FLAGS_threads);
217 SkTaskGroup cpuTests;
halcanary87f3ba42015-01-20 09:30:20 -0800218 SkTArray<const Test*> gpuTests;
commit-bot@chromium.org261c6662014-01-02 16:19:53 +0000219
halcanary87f3ba42015-01-20 09:30:20 -0800220 Status status(toRun);
Hal Canary972eba32018-07-30 17:07:07 -0400221
222 for (const Test& test : TestRegistry::Range()) {
halcanary87f3ba42015-01-20 09:30:20 -0800223 if (!should_run(test.name, test.needsGpu)) {
bungeman@google.com5af16f82011-09-02 15:06:44 +0000224 ++skipCount;
halcanary87f3ba42015-01-20 09:30:20 -0800225 } else if (test.needsGpu) {
226 gpuTests.push_back(&test);
bungeman@google.com5af16f82011-09-02 15:06:44 +0000227 } else {
mtklein048494c2016-02-16 19:06:15 -0800228 cpuTests.add(SkTestRunnable(test, &status));
bungeman@google.com5af16f82011-09-02 15:06:44 +0000229 }
reed@android.comed673312009-02-27 16:24:51 +0000230 }
reed@android.com57b799e2009-04-01 20:26:42 +0000231
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +0000232 // Run GPU tests on this thread.
233 for (int i = 0; i < gpuTests.count(); i++) {
Brian Salomondcfca432017-11-15 15:48:03 -0500234 SkTestRunnable(*gpuTests[i], &status)();
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000235 }
236
commit-bot@chromium.orga7538ba2013-10-10 18:49:04 +0000237 // Block until threaded tests finish.
mtklein406654b2014-09-03 15:34:37 -0700238 cpuTests.wait();
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000239
commit-bot@chromium.org261c6662014-01-02 16:19:53 +0000240 if (FLAGS_verbose) {
halcanary87f3ba42015-01-20 09:30:20 -0800241 SkDebugf(
242 "\nFinished %d tests, %d failures, %d skipped. "
243 "(%d internal tests)",
244 toRun, status.failCount(), skipCount, status.testCount());
caryclark26ad22a2015-10-16 09:03:38 -0700245 if (gVerboseFinalize) {
246 (*gVerboseFinalize)();
247 }
caryclark@google.comd54e1e92013-04-10 15:57:31 +0000248 }
reed@google.coma2769752012-07-22 22:33:05 +0000249
commit-bot@chromium.org261c6662014-01-02 16:19:53 +0000250 SkDebugf("\n");
Cary Clarkab87d7a2016-10-04 10:01:04 -0400251#if DEBUG_COIN
252 if (FLAGS_coinTest) {
253 SkPathOpsDebug::DumpCoinDict();
254 }
255#endif
Cary Clarkf3949122018-08-07 16:38:21 -0400256 if (PathOpsDebug::gJson) {
257 fprintf(PathOpsDebug::gOut, "\n}\n");
258 fclose(PathOpsDebug::gOut);
259 }
halcanary87f3ba42015-01-20 09:30:20 -0800260 return (status.failCount() == 0) ? 0 : 1;
reed@android.comed673312009-02-27 16:24:51 +0000261}