blob: a1c3c237810fc8f42e1f45189201ceaadb0f8dc2 [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
commit-bot@chromium.orgba59d642013-04-11 16:54:09 +000030DEFINE_bool2(extendedTest, x, false, "run extended tests for pathOps.");
Cary Clarkab87d7a2016-10-04 10:01:04 -040031#if DEBUG_COIN
32DEFINE_bool2(coinTest, c, false, "detect unused coincidence algorithms.");
33#endif
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000034
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000035// need to explicitly declare this, or we get some weird infinite loop llist
36template TestRegistry* TestRegistry::gHead;
caryclark26ad22a2015-10-16 09:03:38 -070037void (*gVerboseFinalize)() = nullptr;
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000038
halcanary87f3ba42015-01-20 09:30:20 -080039// The threads report back to this object when they are done.
40class Status {
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000041public:
halcanary87f3ba42015-01-20 09:30:20 -080042 explicit Status(int total)
43 : fDone(0), fTestCount(0), fFailCount(0), fTotal(total) {}
44 // Threadsafe.
45 void endTest(const char* testName,
46 bool success,
47 SkMSec elapsed,
48 int testCount) {
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000049 const int done = 1 + sk_atomic_inc(&fDone);
halcanary87f3ba42015-01-20 09:30:20 -080050 for (int i = 0; i < testCount; ++i) {
51 sk_atomic_inc(&fTestCount);
52 }
53 if (!success) {
54 SkDebugf("\n---- %s FAILED", testName);
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000055 }
56
57 SkString prefix(kSkOverwriteLine);
58 SkString time;
59 if (FLAGS_verbose) {
60 prefix.printf("\n");
halcanary87f3ba42015-01-20 09:30:20 -080061 time.printf("%5dms ", elapsed);
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000062 }
halcanary87f3ba42015-01-20 09:30:20 -080063 SkDebugf("%s[%3d/%3d] %s%s", prefix.c_str(), done, fTotal, time.c_str(),
64 testName);
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000065 }
66
halcanary87f3ba42015-01-20 09:30:20 -080067 void reportFailure() { sk_atomic_inc(&fFailCount); }
68
69 int32_t testCount() { return fTestCount; }
70 int32_t failCount() { return fFailCount; }
71
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000072private:
73 int32_t fDone; // atomic
halcanary87f3ba42015-01-20 09:30:20 -080074 int32_t fTestCount; // atomic
75 int32_t fFailCount; // atomic
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000076 const int fTotal;
77};
78
mtklein048494c2016-02-16 19:06:15 -080079class SkTestRunnable {
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000080public:
halcanary87f3ba42015-01-20 09:30:20 -080081 SkTestRunnable(const Test& test,
82 Status* status,
halcanary96fcdcc2015-08-27 07:41:13 -070083 GrContextFactory* grContextFactory = nullptr)
halcanary87f3ba42015-01-20 09:30:20 -080084 : fTest(test), fStatus(status), fGrContextFactory(grContextFactory) {}
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000085
mtklein048494c2016-02-16 19:06:15 -080086 void operator()() {
halcanary87f3ba42015-01-20 09:30:20 -080087 struct TestReporter : public skiatest::Reporter {
88 public:
Cary Clarka35c68d2016-10-04 10:59:29 -040089 TestReporter() : fStats(nullptr), fError(false), fTestCount(0) {}
mtklein36352bf2015-03-25 18:17:31 -070090 void bumpTestCount() override { ++fTestCount; }
91 bool allowExtendedTest() const override {
halcanary87f3ba42015-01-20 09:30:20 -080092 return FLAGS_extendedTest;
93 }
mtklein36352bf2015-03-25 18:17:31 -070094 bool verbose() const override { return FLAGS_veryVerbose; }
95 void reportFailed(const skiatest::Failure& failure) override {
halcanary87f3ba42015-01-20 09:30:20 -080096 SkDebugf("\nFAILED: %s", failure.toString().c_str());
97 fError = true;
98 }
Cary Clarkab87d7a2016-10-04 10:01:04 -040099 void* stats() { return fStats; }
100 void* fStats;
halcanary87f3ba42015-01-20 09:30:20 -0800101 bool fError;
102 int fTestCount;
103 } reporter;
104
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700105 const Timer timer;
halcanary87f3ba42015-01-20 09:30:20 -0800106 fTest.proc(&reporter, fGrContextFactory);
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700107 SkMSec elapsed = timer.elapsedMsInt();
halcanary87f3ba42015-01-20 09:30:20 -0800108 if (reporter.fError) {
109 fStatus->reportFailure();
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000110 }
halcanary87f3ba42015-01-20 09:30:20 -0800111 fStatus->endTest(fTest.name, !reporter.fError, elapsed,
112 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;
118 GrContextFactory* fGrContextFactory;
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000119};
commit-bot@chromium.orgba59d642013-04-11 16:54:09 +0000120
commit-bot@chromium.org5a47b092014-01-30 15:30:50 +0000121static bool should_run(const char* testName, bool isGPUTest) {
122 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, testName)) {
123 return false;
124 }
125 if (!FLAGS_cpu && !isGPUTest) {
126 return false;
127 }
128 if (!FLAGS_gpu && isGPUTest) {
129 return false;
130 }
131 return true;
132}
133
caryclark17f0b6d2014-07-22 10:15:34 -0700134int test_main();
135int test_main() {
mtklein30e6e2a2014-06-18 11:44:15 -0700136 SetupCrashHandler();
commit-bot@chromium.orgba59d642013-04-11 16:54:09 +0000137
tfarinaa71d3af2014-11-07 06:12:30 -0800138 SkAutoGraphics ag;
bungeman@google.com5af16f82011-09-02 15:06:44 +0000139
reed@google.com91d449e2011-10-26 15:25:18 +0000140 {
141 SkString header("Skia UnitTests:");
reed@google.com9aff1482013-04-11 18:27:52 +0000142 if (!FLAGS_match.isEmpty()) {
caryclark@google.comb631eec2013-05-02 13:14:40 +0000143 header.appendf(" --match");
144 for (int index = 0; index < FLAGS_match.count(); ++index) {
145 header.appendf(" %s", FLAGS_match[index]);
146 }
reed@google.com91d449e2011-10-26 15:25:18 +0000147 }
halcanary87f3ba42015-01-20 09:30:20 -0800148 SkString tmpDir = skiatest::GetTmpDir();
scroggo@google.comc76218d2013-06-06 14:59:56 +0000149 if (!tmpDir.isEmpty()) {
150 header.appendf(" --tmpDir %s", tmpDir.c_str());
djsollen@google.comcb626502013-03-20 13:48:20 +0000151 }
tfarinabcbc1782014-06-18 14:32:48 -0700152 SkString resourcePath = GetResourcePath();
scroggo@google.comc76218d2013-06-06 14:59:56 +0000153 if (!resourcePath.isEmpty()) {
154 header.appendf(" --resourcePath %s", resourcePath.c_str());
reed@google.com789c6f22013-02-25 20:24:24 +0000155 }
reed@google.com91d449e2011-10-26 15:25:18 +0000156#ifdef SK_DEBUG
157 header.append(" SK_DEBUG");
158#else
159 header.append(" SK_RELEASE");
160#endif
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000161 if (FLAGS_veryVerbose) {
162 header.appendf("\n");
163 }
kkinnunen297aaf92015-02-19 06:32:12 -0800164 SkDebugf("%s", header.c_str());
reed@google.com91d449e2011-10-26 15:25:18 +0000165 }
166
reed@android.com80e39a72009-04-02 16:59:40 +0000167
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000168 // Count tests first.
169 int total = 0;
170 int toRun = 0;
sglez@google.com586db932013-07-24 17:24:23 +0000171
halcanary87f3ba42015-01-20 09:30:20 -0800172 for (const TestRegistry* iter = TestRegistry::Head(); iter;
173 iter = iter->next()) {
174 const Test& test = iter->factory();
175 if (should_run(test.name, test.needsGpu)) {
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000176 toRun++;
177 }
178 total++;
179 }
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000180
181 // Now run them.
bungeman@google.com5af16f82011-09-02 15:06:44 +0000182 int skipCount = 0;
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000183
mtklein406654b2014-09-03 15:34:37 -0700184 SkTaskGroup::Enabler enabled(FLAGS_threads);
185 SkTaskGroup cpuTests;
halcanary87f3ba42015-01-20 09:30:20 -0800186 SkTArray<const Test*> gpuTests;
commit-bot@chromium.org261c6662014-01-02 16:19:53 +0000187
halcanary87f3ba42015-01-20 09:30:20 -0800188 Status status(toRun);
189 for (const TestRegistry* iter = TestRegistry::Head(); iter;
190 iter = iter->next()) {
191 const Test& test = iter->factory();
192 if (!should_run(test.name, test.needsGpu)) {
bungeman@google.com5af16f82011-09-02 15:06:44 +0000193 ++skipCount;
halcanary87f3ba42015-01-20 09:30:20 -0800194 } else if (test.needsGpu) {
195 gpuTests.push_back(&test);
bungeman@google.com5af16f82011-09-02 15:06:44 +0000196 } else {
mtklein048494c2016-02-16 19:06:15 -0800197 cpuTests.add(SkTestRunnable(test, &status));
bungeman@google.com5af16f82011-09-02 15:06:44 +0000198 }
reed@android.comed673312009-02-27 16:24:51 +0000199 }
reed@android.com57b799e2009-04-01 20:26:42 +0000200
halcanary96fcdcc2015-08-27 07:41:13 -0700201 GrContextFactory* grContextFactoryPtr = nullptr;
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +0000202#if SK_SUPPORT_GPU
203 // Give GPU tests a context factory if that makes sense on this machine.
204 GrContextFactory grContextFactory;
halcanary87f3ba42015-01-20 09:30:20 -0800205 grContextFactoryPtr = &grContextFactory;
206
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +0000207#endif
208
209 // Run GPU tests on this thread.
210 for (int i = 0; i < gpuTests.count(); i++) {
mtklein048494c2016-02-16 19:06:15 -0800211 SkTestRunnable(*gpuTests[i], &status, grContextFactoryPtr)();
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000212 }
213
commit-bot@chromium.orga7538ba2013-10-10 18:49:04 +0000214 // Block until threaded tests finish.
mtklein406654b2014-09-03 15:34:37 -0700215 cpuTests.wait();
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000216
commit-bot@chromium.org261c6662014-01-02 16:19:53 +0000217 if (FLAGS_verbose) {
halcanary87f3ba42015-01-20 09:30:20 -0800218 SkDebugf(
219 "\nFinished %d tests, %d failures, %d skipped. "
220 "(%d internal tests)",
221 toRun, status.failCount(), skipCount, status.testCount());
caryclark26ad22a2015-10-16 09:03:38 -0700222 if (gVerboseFinalize) {
223 (*gVerboseFinalize)();
224 }
caryclark@google.comd54e1e92013-04-10 15:57:31 +0000225 }
reed@google.coma2769752012-07-22 22:33:05 +0000226
commit-bot@chromium.org261c6662014-01-02 16:19:53 +0000227 SkDebugf("\n");
Cary Clarkab87d7a2016-10-04 10:01:04 -0400228#if DEBUG_COIN
229 if (FLAGS_coinTest) {
230 SkPathOpsDebug::DumpCoinDict();
231 }
232#endif
233
halcanary87f3ba42015-01-20 09:30:20 -0800234 return (status.failCount() == 0) ? 0 : 1;
reed@android.comed673312009-02-27 16:24:51 +0000235}
caryclark@google.com5987f582012-10-02 18:33:14 +0000236
borenet48087572015-04-02 12:16:36 -0700237#if !defined(SK_BUILD_FOR_IOS)
caryclark17f0b6d2014-07-22 10:15:34 -0700238int main(int argc, char** argv) {
239 SkCommandLineFlags::Parse(argc, argv);
240 return test_main();
caryclark@google.com5987f582012-10-02 18:33:14 +0000241}
242#endif