blob: 38237e4c02d7ef19018084926894597ce33bd318 [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"
reed89889b62014-10-29 12:36:45 -070015#include "SkRunnable.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;
28
commit-bot@chromium.orgba59d642013-04-11 16:54:09 +000029DEFINE_bool2(extendedTest, x, false, "run extended tests for pathOps.");
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000030
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000031// need to explicitly declare this, or we get some weird infinite loop llist
32template TestRegistry* TestRegistry::gHead;
caryclark26ad22a2015-10-16 09:03:38 -070033void (*gVerboseFinalize)() = nullptr;
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000034
halcanary87f3ba42015-01-20 09:30:20 -080035// The threads report back to this object when they are done.
36class Status {
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000037public:
halcanary87f3ba42015-01-20 09:30:20 -080038 explicit Status(int total)
39 : fDone(0), fTestCount(0), fFailCount(0), fTotal(total) {}
40 // Threadsafe.
41 void endTest(const char* testName,
42 bool success,
43 SkMSec elapsed,
44 int testCount) {
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000045 const int done = 1 + sk_atomic_inc(&fDone);
halcanary87f3ba42015-01-20 09:30:20 -080046 for (int i = 0; i < testCount; ++i) {
47 sk_atomic_inc(&fTestCount);
48 }
49 if (!success) {
50 SkDebugf("\n---- %s FAILED", testName);
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000051 }
52
53 SkString prefix(kSkOverwriteLine);
54 SkString time;
55 if (FLAGS_verbose) {
56 prefix.printf("\n");
halcanary87f3ba42015-01-20 09:30:20 -080057 time.printf("%5dms ", elapsed);
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000058 }
halcanary87f3ba42015-01-20 09:30:20 -080059 SkDebugf("%s[%3d/%3d] %s%s", prefix.c_str(), done, fTotal, time.c_str(),
60 testName);
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000061 }
62
halcanary87f3ba42015-01-20 09:30:20 -080063 void reportFailure() { sk_atomic_inc(&fFailCount); }
64
65 int32_t testCount() { return fTestCount; }
66 int32_t failCount() { return fFailCount; }
67
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000068private:
69 int32_t fDone; // atomic
halcanary87f3ba42015-01-20 09:30:20 -080070 int32_t fTestCount; // atomic
71 int32_t fFailCount; // atomic
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000072 const int fTotal;
73};
74
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000075// Deletes self when run.
76class SkTestRunnable : public SkRunnable {
77public:
halcanary87f3ba42015-01-20 09:30:20 -080078 SkTestRunnable(const Test& test,
79 Status* status,
halcanary96fcdcc2015-08-27 07:41:13 -070080 GrContextFactory* grContextFactory = nullptr)
halcanary87f3ba42015-01-20 09:30:20 -080081 : fTest(test), fStatus(status), fGrContextFactory(grContextFactory) {}
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000082
83 virtual void run() {
halcanary87f3ba42015-01-20 09:30:20 -080084 struct TestReporter : public skiatest::Reporter {
85 public:
86 TestReporter() : fError(false), fTestCount(0) {}
mtklein36352bf2015-03-25 18:17:31 -070087 void bumpTestCount() override { ++fTestCount; }
88 bool allowExtendedTest() const override {
halcanary87f3ba42015-01-20 09:30:20 -080089 return FLAGS_extendedTest;
90 }
mtklein36352bf2015-03-25 18:17:31 -070091 bool verbose() const override { return FLAGS_veryVerbose; }
92 void reportFailed(const skiatest::Failure& failure) override {
halcanary87f3ba42015-01-20 09:30:20 -080093 SkDebugf("\nFAILED: %s", failure.toString().c_str());
94 fError = true;
95 }
96 bool fError;
97 int fTestCount;
98 } reporter;
99
100 const SkMSec start = SkTime::GetMSecs();
101 fTest.proc(&reporter, fGrContextFactory);
102 SkMSec elapsed = SkTime::GetMSecs() - start;
103 if (reporter.fError) {
104 fStatus->reportFailure();
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000105 }
halcanary87f3ba42015-01-20 09:30:20 -0800106 fStatus->endTest(fTest.name, !reporter.fError, elapsed,
107 reporter.fTestCount);
halcanary385fe4d2015-08-26 13:07:48 -0700108 delete this;
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000109 }
110
111private:
halcanary87f3ba42015-01-20 09:30:20 -0800112 Test fTest;
113 Status* fStatus;
114 GrContextFactory* fGrContextFactory;
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000115};
commit-bot@chromium.orgba59d642013-04-11 16:54:09 +0000116
commit-bot@chromium.org5a47b092014-01-30 15:30:50 +0000117static bool should_run(const char* testName, bool isGPUTest) {
118 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, testName)) {
119 return false;
120 }
121 if (!FLAGS_cpu && !isGPUTest) {
122 return false;
123 }
124 if (!FLAGS_gpu && isGPUTest) {
125 return false;
126 }
127 return true;
128}
129
caryclark17f0b6d2014-07-22 10:15:34 -0700130int test_main();
131int test_main() {
mtklein30e6e2a2014-06-18 11:44:15 -0700132 SetupCrashHandler();
commit-bot@chromium.orgba59d642013-04-11 16:54:09 +0000133
tfarinaa71d3af2014-11-07 06:12:30 -0800134 SkAutoGraphics ag;
bungeman@google.com5af16f82011-09-02 15:06:44 +0000135
reed@google.com91d449e2011-10-26 15:25:18 +0000136 {
137 SkString header("Skia UnitTests:");
reed@google.com9aff1482013-04-11 18:27:52 +0000138 if (!FLAGS_match.isEmpty()) {
caryclark@google.comb631eec2013-05-02 13:14:40 +0000139 header.appendf(" --match");
140 for (int index = 0; index < FLAGS_match.count(); ++index) {
141 header.appendf(" %s", FLAGS_match[index]);
142 }
reed@google.com91d449e2011-10-26 15:25:18 +0000143 }
halcanary87f3ba42015-01-20 09:30:20 -0800144 SkString tmpDir = skiatest::GetTmpDir();
scroggo@google.comc76218d2013-06-06 14:59:56 +0000145 if (!tmpDir.isEmpty()) {
146 header.appendf(" --tmpDir %s", tmpDir.c_str());
djsollen@google.comcb626502013-03-20 13:48:20 +0000147 }
tfarinabcbc1782014-06-18 14:32:48 -0700148 SkString resourcePath = GetResourcePath();
scroggo@google.comc76218d2013-06-06 14:59:56 +0000149 if (!resourcePath.isEmpty()) {
150 header.appendf(" --resourcePath %s", resourcePath.c_str());
reed@google.com789c6f22013-02-25 20:24:24 +0000151 }
reed@google.com91d449e2011-10-26 15:25:18 +0000152#ifdef SK_DEBUG
153 header.append(" SK_DEBUG");
154#else
155 header.append(" SK_RELEASE");
156#endif
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000157 if (FLAGS_veryVerbose) {
158 header.appendf("\n");
159 }
kkinnunen297aaf92015-02-19 06:32:12 -0800160 SkDebugf("%s", header.c_str());
reed@google.com91d449e2011-10-26 15:25:18 +0000161 }
162
reed@android.com80e39a72009-04-02 16:59:40 +0000163
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000164 // Count tests first.
165 int total = 0;
166 int toRun = 0;
sglez@google.com586db932013-07-24 17:24:23 +0000167
halcanary87f3ba42015-01-20 09:30:20 -0800168 for (const TestRegistry* iter = TestRegistry::Head(); iter;
169 iter = iter->next()) {
170 const Test& test = iter->factory();
171 if (should_run(test.name, test.needsGpu)) {
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000172 toRun++;
173 }
174 total++;
175 }
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000176
177 // Now run them.
bungeman@google.com5af16f82011-09-02 15:06:44 +0000178 int skipCount = 0;
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000179
mtklein406654b2014-09-03 15:34:37 -0700180 SkTaskGroup::Enabler enabled(FLAGS_threads);
181 SkTaskGroup cpuTests;
halcanary87f3ba42015-01-20 09:30:20 -0800182 SkTArray<const Test*> gpuTests;
commit-bot@chromium.org261c6662014-01-02 16:19:53 +0000183
halcanary87f3ba42015-01-20 09:30:20 -0800184 Status status(toRun);
185 for (const TestRegistry* iter = TestRegistry::Head(); iter;
186 iter = iter->next()) {
187 const Test& test = iter->factory();
188 if (!should_run(test.name, test.needsGpu)) {
bungeman@google.com5af16f82011-09-02 15:06:44 +0000189 ++skipCount;
halcanary87f3ba42015-01-20 09:30:20 -0800190 } else if (test.needsGpu) {
191 gpuTests.push_back(&test);
bungeman@google.com5af16f82011-09-02 15:06:44 +0000192 } else {
halcanary385fe4d2015-08-26 13:07:48 -0700193 cpuTests.add(new SkTestRunnable(test, &status));
bungeman@google.com5af16f82011-09-02 15:06:44 +0000194 }
reed@android.comed673312009-02-27 16:24:51 +0000195 }
reed@android.com57b799e2009-04-01 20:26:42 +0000196
halcanary96fcdcc2015-08-27 07:41:13 -0700197 GrContextFactory* grContextFactoryPtr = nullptr;
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +0000198#if SK_SUPPORT_GPU
199 // Give GPU tests a context factory if that makes sense on this machine.
200 GrContextFactory grContextFactory;
halcanary87f3ba42015-01-20 09:30:20 -0800201 grContextFactoryPtr = &grContextFactory;
202
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +0000203#endif
204
205 // Run GPU tests on this thread.
206 for (int i = 0; i < gpuTests.count(); i++) {
halcanary385fe4d2015-08-26 13:07:48 -0700207 (new SkTestRunnable(*gpuTests[i], &status, grContextFactoryPtr))->run();
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000208 }
209
commit-bot@chromium.orga7538ba2013-10-10 18:49:04 +0000210 // Block until threaded tests finish.
mtklein406654b2014-09-03 15:34:37 -0700211 cpuTests.wait();
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000212
commit-bot@chromium.org261c6662014-01-02 16:19:53 +0000213 if (FLAGS_verbose) {
halcanary87f3ba42015-01-20 09:30:20 -0800214 SkDebugf(
215 "\nFinished %d tests, %d failures, %d skipped. "
216 "(%d internal tests)",
217 toRun, status.failCount(), skipCount, status.testCount());
caryclark26ad22a2015-10-16 09:03:38 -0700218 if (gVerboseFinalize) {
219 (*gVerboseFinalize)();
220 }
caryclark@google.comd54e1e92013-04-10 15:57:31 +0000221 }
reed@google.coma2769752012-07-22 22:33:05 +0000222
commit-bot@chromium.org261c6662014-01-02 16:19:53 +0000223 SkDebugf("\n");
halcanary87f3ba42015-01-20 09:30:20 -0800224 return (status.failCount() == 0) ? 0 : 1;
reed@android.comed673312009-02-27 16:24:51 +0000225}
caryclark@google.com5987f582012-10-02 18:33:14 +0000226
borenet48087572015-04-02 12:16:36 -0700227#if !defined(SK_BUILD_FOR_IOS)
caryclark17f0b6d2014-07-22 10:15:34 -0700228int main(int argc, char** argv) {
229 SkCommandLineFlags::Parse(argc, argv);
230 return test_main();
caryclark@google.com5987f582012-10-02 18:33:14 +0000231}
232#endif