blob: 65fbc3284297a9070737fbacd3994e08ac2502f5 [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"
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000015#include "SkTArray.h"
mtklein406654b2014-09-03 15:34:37 -070016#include "SkTaskGroup.h"
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000017#include "SkTemplates.h"
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +000018#include "SkTime.h"
reed@android.comed673312009-02-27 16:24:51 +000019#include "Test.h"
20
robertphillips@google.combdb1be52012-09-07 18:24:43 +000021#if SK_SUPPORT_GPU
22#include "GrContext.h"
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +000023#include "GrContextFactory.h"
robertphillips@google.combdb1be52012-09-07 18:24:43 +000024#endif
25
reed@android.comed673312009-02-27 16:24:51 +000026using namespace skiatest;
27
commit-bot@chromium.orgba59d642013-04-11 16:54:09 +000028DEFINE_bool2(extendedTest, x, false, "run extended tests for pathOps.");
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000029
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000030// need to explicitly declare this, or we get some weird infinite loop llist
31template TestRegistry* TestRegistry::gHead;
caryclark26ad22a2015-10-16 09:03:38 -070032void (*gVerboseFinalize)() = nullptr;
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000033
halcanary87f3ba42015-01-20 09:30:20 -080034// The threads report back to this object when they are done.
35class Status {
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000036public:
halcanary87f3ba42015-01-20 09:30:20 -080037 explicit Status(int total)
38 : fDone(0), fTestCount(0), fFailCount(0), fTotal(total) {}
39 // Threadsafe.
40 void endTest(const char* testName,
41 bool success,
42 SkMSec elapsed,
43 int testCount) {
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000044 const int done = 1 + sk_atomic_inc(&fDone);
halcanary87f3ba42015-01-20 09:30:20 -080045 for (int i = 0; i < testCount; ++i) {
46 sk_atomic_inc(&fTestCount);
47 }
48 if (!success) {
49 SkDebugf("\n---- %s FAILED", testName);
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000050 }
51
52 SkString prefix(kSkOverwriteLine);
53 SkString time;
54 if (FLAGS_verbose) {
55 prefix.printf("\n");
halcanary87f3ba42015-01-20 09:30:20 -080056 time.printf("%5dms ", elapsed);
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000057 }
halcanary87f3ba42015-01-20 09:30:20 -080058 SkDebugf("%s[%3d/%3d] %s%s", prefix.c_str(), done, fTotal, time.c_str(),
59 testName);
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000060 }
61
halcanary87f3ba42015-01-20 09:30:20 -080062 void reportFailure() { sk_atomic_inc(&fFailCount); }
63
64 int32_t testCount() { return fTestCount; }
65 int32_t failCount() { return fFailCount; }
66
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000067private:
68 int32_t fDone; // atomic
halcanary87f3ba42015-01-20 09:30:20 -080069 int32_t fTestCount; // atomic
70 int32_t fFailCount; // atomic
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000071 const int fTotal;
72};
73
mtklein048494c2016-02-16 19:06:15 -080074class SkTestRunnable {
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000075public:
halcanary87f3ba42015-01-20 09:30:20 -080076 SkTestRunnable(const Test& test,
77 Status* status,
halcanary96fcdcc2015-08-27 07:41:13 -070078 GrContextFactory* grContextFactory = nullptr)
halcanary87f3ba42015-01-20 09:30:20 -080079 : fTest(test), fStatus(status), fGrContextFactory(grContextFactory) {}
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000080
mtklein048494c2016-02-16 19:06:15 -080081 void operator()() {
halcanary87f3ba42015-01-20 09:30:20 -080082 struct TestReporter : public skiatest::Reporter {
83 public:
84 TestReporter() : fError(false), fTestCount(0) {}
mtklein36352bf2015-03-25 18:17:31 -070085 void bumpTestCount() override { ++fTestCount; }
86 bool allowExtendedTest() const override {
halcanary87f3ba42015-01-20 09:30:20 -080087 return FLAGS_extendedTest;
88 }
mtklein36352bf2015-03-25 18:17:31 -070089 bool verbose() const override { return FLAGS_veryVerbose; }
90 void reportFailed(const skiatest::Failure& failure) override {
halcanary87f3ba42015-01-20 09:30:20 -080091 SkDebugf("\nFAILED: %s", failure.toString().c_str());
92 fError = true;
93 }
94 bool fError;
95 int fTestCount;
96 } reporter;
97
98 const SkMSec start = SkTime::GetMSecs();
99 fTest.proc(&reporter, fGrContextFactory);
100 SkMSec elapsed = SkTime::GetMSecs() - start;
101 if (reporter.fError) {
102 fStatus->reportFailure();
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000103 }
halcanary87f3ba42015-01-20 09:30:20 -0800104 fStatus->endTest(fTest.name, !reporter.fError, elapsed,
105 reporter.fTestCount);
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000106 }
107
108private:
halcanary87f3ba42015-01-20 09:30:20 -0800109 Test fTest;
110 Status* fStatus;
111 GrContextFactory* fGrContextFactory;
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000112};
commit-bot@chromium.orgba59d642013-04-11 16:54:09 +0000113
commit-bot@chromium.org5a47b092014-01-30 15:30:50 +0000114static bool should_run(const char* testName, bool isGPUTest) {
115 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, testName)) {
116 return false;
117 }
118 if (!FLAGS_cpu && !isGPUTest) {
119 return false;
120 }
121 if (!FLAGS_gpu && isGPUTest) {
122 return false;
123 }
124 return true;
125}
126
caryclark17f0b6d2014-07-22 10:15:34 -0700127int test_main();
128int test_main() {
mtklein30e6e2a2014-06-18 11:44:15 -0700129 SetupCrashHandler();
commit-bot@chromium.orgba59d642013-04-11 16:54:09 +0000130
tfarinaa71d3af2014-11-07 06:12:30 -0800131 SkAutoGraphics ag;
bungeman@google.com5af16f82011-09-02 15:06:44 +0000132
reed@google.com91d449e2011-10-26 15:25:18 +0000133 {
134 SkString header("Skia UnitTests:");
reed@google.com9aff1482013-04-11 18:27:52 +0000135 if (!FLAGS_match.isEmpty()) {
caryclark@google.comb631eec2013-05-02 13:14:40 +0000136 header.appendf(" --match");
137 for (int index = 0; index < FLAGS_match.count(); ++index) {
138 header.appendf(" %s", FLAGS_match[index]);
139 }
reed@google.com91d449e2011-10-26 15:25:18 +0000140 }
halcanary87f3ba42015-01-20 09:30:20 -0800141 SkString tmpDir = skiatest::GetTmpDir();
scroggo@google.comc76218d2013-06-06 14:59:56 +0000142 if (!tmpDir.isEmpty()) {
143 header.appendf(" --tmpDir %s", tmpDir.c_str());
djsollen@google.comcb626502013-03-20 13:48:20 +0000144 }
tfarinabcbc1782014-06-18 14:32:48 -0700145 SkString resourcePath = GetResourcePath();
scroggo@google.comc76218d2013-06-06 14:59:56 +0000146 if (!resourcePath.isEmpty()) {
147 header.appendf(" --resourcePath %s", resourcePath.c_str());
reed@google.com789c6f22013-02-25 20:24:24 +0000148 }
reed@google.com91d449e2011-10-26 15:25:18 +0000149#ifdef SK_DEBUG
150 header.append(" SK_DEBUG");
151#else
152 header.append(" SK_RELEASE");
153#endif
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000154 if (FLAGS_veryVerbose) {
155 header.appendf("\n");
156 }
kkinnunen297aaf92015-02-19 06:32:12 -0800157 SkDebugf("%s", header.c_str());
reed@google.com91d449e2011-10-26 15:25:18 +0000158 }
159
reed@android.com80e39a72009-04-02 16:59:40 +0000160
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000161 // Count tests first.
162 int total = 0;
163 int toRun = 0;
sglez@google.com586db932013-07-24 17:24:23 +0000164
halcanary87f3ba42015-01-20 09:30:20 -0800165 for (const TestRegistry* iter = TestRegistry::Head(); iter;
166 iter = iter->next()) {
167 const Test& test = iter->factory();
168 if (should_run(test.name, test.needsGpu)) {
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000169 toRun++;
170 }
171 total++;
172 }
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000173
174 // Now run them.
bungeman@google.com5af16f82011-09-02 15:06:44 +0000175 int skipCount = 0;
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000176
mtklein406654b2014-09-03 15:34:37 -0700177 SkTaskGroup::Enabler enabled(FLAGS_threads);
178 SkTaskGroup cpuTests;
halcanary87f3ba42015-01-20 09:30:20 -0800179 SkTArray<const Test*> gpuTests;
commit-bot@chromium.org261c6662014-01-02 16:19:53 +0000180
halcanary87f3ba42015-01-20 09:30:20 -0800181 Status status(toRun);
182 for (const TestRegistry* iter = TestRegistry::Head(); iter;
183 iter = iter->next()) {
184 const Test& test = iter->factory();
185 if (!should_run(test.name, test.needsGpu)) {
bungeman@google.com5af16f82011-09-02 15:06:44 +0000186 ++skipCount;
halcanary87f3ba42015-01-20 09:30:20 -0800187 } else if (test.needsGpu) {
188 gpuTests.push_back(&test);
bungeman@google.com5af16f82011-09-02 15:06:44 +0000189 } else {
mtklein048494c2016-02-16 19:06:15 -0800190 cpuTests.add(SkTestRunnable(test, &status));
bungeman@google.com5af16f82011-09-02 15:06:44 +0000191 }
reed@android.comed673312009-02-27 16:24:51 +0000192 }
reed@android.com57b799e2009-04-01 20:26:42 +0000193
halcanary96fcdcc2015-08-27 07:41:13 -0700194 GrContextFactory* grContextFactoryPtr = nullptr;
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +0000195#if SK_SUPPORT_GPU
196 // Give GPU tests a context factory if that makes sense on this machine.
197 GrContextFactory grContextFactory;
halcanary87f3ba42015-01-20 09:30:20 -0800198 grContextFactoryPtr = &grContextFactory;
199
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +0000200#endif
201
202 // Run GPU tests on this thread.
203 for (int i = 0; i < gpuTests.count(); i++) {
mtklein048494c2016-02-16 19:06:15 -0800204 SkTestRunnable(*gpuTests[i], &status, grContextFactoryPtr)();
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000205 }
206
commit-bot@chromium.orga7538ba2013-10-10 18:49:04 +0000207 // Block until threaded tests finish.
mtklein406654b2014-09-03 15:34:37 -0700208 cpuTests.wait();
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000209
commit-bot@chromium.org261c6662014-01-02 16:19:53 +0000210 if (FLAGS_verbose) {
halcanary87f3ba42015-01-20 09:30:20 -0800211 SkDebugf(
212 "\nFinished %d tests, %d failures, %d skipped. "
213 "(%d internal tests)",
214 toRun, status.failCount(), skipCount, status.testCount());
caryclark26ad22a2015-10-16 09:03:38 -0700215 if (gVerboseFinalize) {
216 (*gVerboseFinalize)();
217 }
caryclark@google.comd54e1e92013-04-10 15:57:31 +0000218 }
reed@google.coma2769752012-07-22 22:33:05 +0000219
commit-bot@chromium.org261c6662014-01-02 16:19:53 +0000220 SkDebugf("\n");
halcanary87f3ba42015-01-20 09:30:20 -0800221 return (status.failCount() == 0) ? 0 : 1;
reed@android.comed673312009-02-27 16:24:51 +0000222}
caryclark@google.com5987f582012-10-02 18:33:14 +0000223
borenet48087572015-04-02 12:16:36 -0700224#if !defined(SK_BUILD_FOR_IOS)
caryclark17f0b6d2014-07-22 10:15:34 -0700225int main(int argc, char** argv) {
226 SkCommandLineFlags::Parse(argc, argv);
227 return test_main();
caryclark@google.com5987f582012-10-02 18:33:14 +0000228}
229#endif