blob: f70a7fab62fc22d5141aebfcf6a34a9013b8ff03 [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"
scroggo@google.com5a6324e2013-04-11 20:11:40 +000010#include "SkCommandLineFlags.h"
reed@android.com5e5adfd2009-03-07 03:39:23 +000011#include "SkGraphics.h"
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +000012#include "SkOSFile.h"
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000013#include "SkTArray.h"
14#include "SkTemplates.h"
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +000015#include "SkThreadPool.h"
16#include "SkTime.h"
reed@android.comed673312009-02-27 16:24:51 +000017#include "Test.h"
18
robertphillips@google.combdb1be52012-09-07 18:24:43 +000019#if SK_SUPPORT_GPU
20#include "GrContext.h"
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +000021#include "GrContextFactory.h"
robertphillips@google.combdb1be52012-09-07 18:24:43 +000022#endif
23
reed@android.comed673312009-02-27 16:24:51 +000024using namespace skiatest;
25
caryclark@google.comb631eec2013-05-02 13:14:40 +000026DEFINE_string2(match, m, NULL, "[~][^]substring[$] [...] of test name to run.\n" \
27 "Multiple matches may be separated by spaces.\n" \
28 "~ causes a matching test to always be skipped\n" \
29 "^ requires the start of the test to match\n" \
30 "$ requires the end of the test to match\n" \
31 "^ and $ requires an exact match\n" \
32 "If a test does not match any list entry,\n" \
33 "it is skipped unless some list entry starts with ~");
commit-bot@chromium.orgba59d642013-04-11 16:54:09 +000034DEFINE_bool2(extendedTest, x, false, "run extended tests for pathOps.");
commit-bot@chromium.org6dda8272014-01-23 17:21:19 +000035DEFINE_bool2(leaks, l, false, "show leaked ref cnt'd objects.");
caryclark@google.com8d0a5242013-07-16 16:11:16 +000036DEFINE_bool2(single, z, false, "run tests on a single thread internally.");
commit-bot@chromium.org11ea96c2014-01-28 21:15:42 +000037DEFINE_bool2(verbose, v, false, "enable verbose output from the test driver.");
38DEFINE_bool2(veryVerbose, V, false, "tell individual tests to be verbose.");
commit-bot@chromium.org5a47b092014-01-30 15:30:50 +000039DEFINE_bool(cpu, true, "whether or not to run CPU tests.");
40DEFINE_bool(gpu, true, "whether or not to run GPU tests.");
commit-bot@chromium.org44c661f2013-04-22 15:23:14 +000041DEFINE_int32(threads, SkThreadPool::kThreadPerCore,
42 "Run threadsafe tests on a threadpool with this many threads.");
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +000043DEFINE_string2(resourcePath, i, "resources", "directory for test resources.");
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000044
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000045// need to explicitly declare this, or we get some weird infinite loop llist
46template TestRegistry* TestRegistry::gHead;
47
48class Iter {
49public:
50 Iter() { this->reset(); }
51 void reset() { fReg = TestRegistry::Head(); }
52
53 Test* next(Reporter* r) {
54 if (fReg) {
55 TestRegistry::Factory fact = fReg->factory();
56 fReg = fReg->next();
57 Test* test = fact(NULL);
58 test->setReporter(r);
59 return test;
60 }
61 return NULL;
62 }
63
64private:
65 const TestRegistry* fReg;
66};
67
68class DebugfReporter : public Reporter {
69public:
70 explicit DebugfReporter(int total) : fDone(0), fTotal(total) {}
71
72 virtual bool allowExtendedTest() const SK_OVERRIDE { return FLAGS_extendedTest; }
73 virtual bool allowThreaded() const SK_OVERRIDE { return !FLAGS_single; }
commit-bot@chromium.org11ea96c2014-01-28 21:15:42 +000074 virtual bool verbose() const SK_OVERRIDE { return FLAGS_veryVerbose; }
commit-bot@chromium.org261c6662014-01-02 16:19:53 +000075
76protected:
77 virtual void onReportFailed(const SkString& desc) SK_OVERRIDE {
78 SkDebugf("\nFAILED: %s", desc.c_str());
79 }
80
81 virtual void onEnd(Test* test) SK_OVERRIDE {
82 const int done = 1 + sk_atomic_inc(&fDone);
83
84 if (!test->passed()) {
85 SkDebugf("\n---- %s FAILED", test->getName());
86 }
87
88 SkString prefix(kSkOverwriteLine);
89 SkString time;
90 if (FLAGS_verbose) {
91 prefix.printf("\n");
92 time.printf("%5dms ", test->elapsedMs());
93 }
94 SkDebugf("%s[%3d/%3d] %s%s", prefix.c_str(), done, fTotal, time.c_str(), test->getName());
95 }
96
97private:
98 int32_t fDone; // atomic
99 const int fTotal;
100};
101
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000102// Deletes self when run.
103class SkTestRunnable : public SkRunnable {
104public:
105 // Takes ownership of test.
106 SkTestRunnable(Test* test, int32_t* failCount) : fTest(test), fFailCount(failCount) {}
107
108 virtual void run() {
109 fTest->run();
110 if(!fTest->passed()) {
111 sk_atomic_inc(fFailCount);
112 }
113 SkDELETE(this);
114 }
115
116private:
117 SkAutoTDelete<Test> fTest;
118 int32_t* fFailCount;
119};
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
caryclark@google.com5987f582012-10-02 18:33:14 +0000134int tool_main(int argc, char** argv);
135int tool_main(int argc, char** argv) {
mtklein30e6e2a2014-06-18 11:44:15 -0700136 SetupCrashHandler();
commit-bot@chromium.orgba59d642013-04-11 16:54:09 +0000137 SkCommandLineFlags::SetUsage("");
138 SkCommandLineFlags::Parse(argc, argv);
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +0000139 Test::SetResourcePath(FLAGS_resourcePath[0]);
commit-bot@chromium.orgba59d642013-04-11 16:54:09 +0000140
bsalomon@google.com4e230682013-01-15 20:37:04 +0000141#if SK_ENABLE_INST_COUNT
commit-bot@chromium.org6dda8272014-01-23 17:21:19 +0000142 if (FLAGS_leaks) {
143 gPrintInstCount = true;
144 }
reed@google.coma2769752012-07-22 22:33:05 +0000145#endif
caryclark@google.comd54e1e92013-04-10 15:57:31 +0000146
reed@google.coma2769752012-07-22 22:33:05 +0000147 SkGraphics::Init();
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 }
scroggo@google.comc76218d2013-06-06 14:59:56 +0000157 SkString tmpDir = Test::GetTmpDir();
158 if (!tmpDir.isEmpty()) {
159 header.appendf(" --tmpDir %s", tmpDir.c_str());
djsollen@google.comcb626502013-03-20 13:48:20 +0000160 }
scroggo@google.comc76218d2013-06-06 14:59:56 +0000161 SkString resourcePath = Test::GetResourcePath();
162 if (!resourcePath.isEmpty()) {
163 header.appendf(" --resourcePath %s", resourcePath.c_str());
reed@google.com789c6f22013-02-25 20:24:24 +0000164 }
reed@google.com91d449e2011-10-26 15:25:18 +0000165#ifdef SK_DEBUG
166 header.append(" SK_DEBUG");
167#else
168 header.append(" SK_RELEASE");
169#endif
reed@google.com5696baa2013-08-29 20:20:39 +0000170 header.appendf(" skia_arch_width=%d", (int)sizeof(void*) * 8);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000171 if (FLAGS_veryVerbose) {
172 header.appendf("\n");
173 }
commit-bot@chromium.org261c6662014-01-02 16:19:53 +0000174 SkDebugf(header.c_str());
reed@google.com91d449e2011-10-26 15:25:18 +0000175 }
176
reed@android.com80e39a72009-04-02 16:59:40 +0000177
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000178 // Count tests first.
179 int total = 0;
180 int toRun = 0;
181 Test* test;
sglez@google.com586db932013-07-24 17:24:23 +0000182
commit-bot@chromium.org261c6662014-01-02 16:19:53 +0000183 Iter iter;
184 while ((test = iter.next(NULL/*reporter not needed*/)) != NULL) {
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000185 SkAutoTDelete<Test> owned(test);
commit-bot@chromium.org5a47b092014-01-30 15:30:50 +0000186 if (should_run(test->getName(), test->isGPUTest())) {
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000187 toRun++;
188 }
189 total++;
190 }
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000191
192 // Now run them.
193 iter.reset();
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000194 int32_t failCount = 0;
bungeman@google.com5af16f82011-09-02 15:06:44 +0000195 int skipCount = 0;
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000196
commit-bot@chromium.orga7538ba2013-10-10 18:49:04 +0000197 SkThreadPool threadpool(FLAGS_threads);
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +0000198 SkTArray<Test*> gpuTests; // Always passes ownership to an SkTestRunnable
commit-bot@chromium.org261c6662014-01-02 16:19:53 +0000199
200 DebugfReporter reporter(toRun);
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000201 for (int i = 0; i < total; i++) {
commit-bot@chromium.org261c6662014-01-02 16:19:53 +0000202 SkAutoTDelete<Test> test(iter.next(&reporter));
commit-bot@chromium.org5a47b092014-01-30 15:30:50 +0000203 if (!should_run(test->getName(), test->isGPUTest())) {
bungeman@google.com5af16f82011-09-02 15:06:44 +0000204 ++skipCount;
commit-bot@chromium.org5a47b092014-01-30 15:30:50 +0000205 } else if (test->isGPUTest()) {
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +0000206 gpuTests.push_back() = test.detach();
bungeman@google.com5af16f82011-09-02 15:06:44 +0000207 } else {
commit-bot@chromium.orga7538ba2013-10-10 18:49:04 +0000208 threadpool.add(SkNEW_ARGS(SkTestRunnable, (test.detach(), &failCount)));
bungeman@google.com5af16f82011-09-02 15:06:44 +0000209 }
reed@android.comed673312009-02-27 16:24:51 +0000210 }
reed@android.com57b799e2009-04-01 20:26:42 +0000211
commit-bot@chromium.org0dc5bd12014-02-26 16:31:22 +0000212#if SK_SUPPORT_GPU
213 // Give GPU tests a context factory if that makes sense on this machine.
214 GrContextFactory grContextFactory;
215 for (int i = 0; i < gpuTests.count(); i++) {
216 gpuTests[i]->setGrContextFactory(&grContextFactory);
217 }
218#endif
219
220 // Run GPU tests on this thread.
221 for (int i = 0; i < gpuTests.count(); i++) {
222 SkNEW_ARGS(SkTestRunnable, (gpuTests[i], &failCount))->run();
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000223 }
224
commit-bot@chromium.orga7538ba2013-10-10 18:49:04 +0000225 // Block until threaded tests finish.
226 threadpool.wait();
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000227
commit-bot@chromium.org261c6662014-01-02 16:19:53 +0000228 if (FLAGS_verbose) {
229 SkDebugf("\nFinished %d tests, %d failures, %d skipped. (%d internal tests)",
230 toRun, failCount, skipCount, reporter.countTests());
caryclark@google.comd54e1e92013-04-10 15:57:31 +0000231 }
reed@google.coma2769752012-07-22 22:33:05 +0000232 SkGraphics::Term();
233
commit-bot@chromium.org261c6662014-01-02 16:19:53 +0000234 SkDebugf("\n");
bungeman@google.com5af16f82011-09-02 15:06:44 +0000235 return (failCount == 0) ? 0 : 1;
reed@android.comed673312009-02-27 16:24:51 +0000236}
caryclark@google.com5987f582012-10-02 18:33:14 +0000237
borenet@google.com7158e6a2012-11-01 17:43:44 +0000238#if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL)
caryclark@google.com5987f582012-10-02 18:33:14 +0000239int main(int argc, char * const argv[]) {
240 return tool_main(argc, (char**) argv);
241}
242#endif