1 /*
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 */
7
8 #include "CrashHandler.h"
9 #include "OverwriteLine.h"
10 #include "Resources.h"
11 #include "SkCommonFlags.h"
12 #include "SkGraphics.h"
13 #include "SkInstCnt.h"
14 #include "SkOSFile.h"
15 #include "SkRunnable.h"
16 #include "SkTArray.h"
17 #include "SkTaskGroup.h"
18 #include "SkTemplates.h"
19 #include "SkThread.h"
20 #include "SkTime.h"
21 #include "Test.h"
22
23 #if SK_SUPPORT_GPU
24 #include "GrContext.h"
25 #include "GrContextFactory.h"
26 #endif
27
28 using namespace skiatest;
29
30 DEFINE_bool2(extendedTest, x, false, "run extended tests for pathOps.");
31
32 // need to explicitly declare this, or we get some weird infinite loop llist
33 template TestRegistry* TestRegistry::gHead;
34
35 // The threads report back to this object when they are done.
36 class Status {
37 public:
Status(int total)38 explicit Status(int total)
39 : fDone(0), fTestCount(0), fFailCount(0), fTotal(total) {}
40 // Threadsafe.
endTest(const char * testName,bool success,SkMSec elapsed,int testCount)41 void endTest(const char* testName,
42 bool success,
43 SkMSec elapsed,
44 int testCount) {
45 const int done = 1 + sk_atomic_inc(&fDone);
46 for (int i = 0; i < testCount; ++i) {
47 sk_atomic_inc(&fTestCount);
48 }
49 if (!success) {
50 SkDebugf("\n---- %s FAILED", testName);
51 }
52
53 SkString prefix(kSkOverwriteLine);
54 SkString time;
55 if (FLAGS_verbose) {
56 prefix.printf("\n");
57 time.printf("%5dms ", elapsed);
58 }
59 SkDebugf("%s[%3d/%3d] %s%s", prefix.c_str(), done, fTotal, time.c_str(),
60 testName);
61 }
62
reportFailure()63 void reportFailure() { sk_atomic_inc(&fFailCount); }
64
testCount()65 int32_t testCount() { return fTestCount; }
failCount()66 int32_t failCount() { return fFailCount; }
67
68 private:
69 int32_t fDone; // atomic
70 int32_t fTestCount; // atomic
71 int32_t fFailCount; // atomic
72 const int fTotal;
73 };
74
75 // Deletes self when run.
76 class SkTestRunnable : public SkRunnable {
77 public:
SkTestRunnable(const Test & test,Status * status,GrContextFactory * grContextFactory=NULL)78 SkTestRunnable(const Test& test,
79 Status* status,
80 GrContextFactory* grContextFactory = NULL)
81 : fTest(test), fStatus(status), fGrContextFactory(grContextFactory) {}
82
run()83 virtual void run() {
84 struct TestReporter : public skiatest::Reporter {
85 public:
86 TestReporter() : fError(false), fTestCount(0) {}
87 void bumpTestCount() override { ++fTestCount; }
88 bool allowExtendedTest() const override {
89 return FLAGS_extendedTest;
90 }
91 bool verbose() const override { return FLAGS_veryVerbose; }
92 void reportFailed(const skiatest::Failure& failure) override {
93 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();
105 }
106 fStatus->endTest(fTest.name, !reporter.fError, elapsed,
107 reporter.fTestCount);
108 SkDELETE(this);
109 }
110
111 private:
112 Test fTest;
113 Status* fStatus;
114 GrContextFactory* fGrContextFactory;
115 };
116
should_run(const char * testName,bool isGPUTest)117 static 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
130 int test_main();
test_main()131 int test_main() {
132 SetupCrashHandler();
133
134 #if SK_ENABLE_INST_COUNT
135 if (FLAGS_leaks) {
136 gPrintInstCount = true;
137 }
138 #endif
139
140 SkAutoGraphics ag;
141
142 {
143 SkString header("Skia UnitTests:");
144 if (!FLAGS_match.isEmpty()) {
145 header.appendf(" --match");
146 for (int index = 0; index < FLAGS_match.count(); ++index) {
147 header.appendf(" %s", FLAGS_match[index]);
148 }
149 }
150 SkString tmpDir = skiatest::GetTmpDir();
151 if (!tmpDir.isEmpty()) {
152 header.appendf(" --tmpDir %s", tmpDir.c_str());
153 }
154 SkString resourcePath = GetResourcePath();
155 if (!resourcePath.isEmpty()) {
156 header.appendf(" --resourcePath %s", resourcePath.c_str());
157 }
158 #ifdef SK_DEBUG
159 header.append(" SK_DEBUG");
160 #else
161 header.append(" SK_RELEASE");
162 #endif
163 header.appendf(" skia_arch_width=%d", (int)sizeof(void*) * 8);
164 if (FLAGS_veryVerbose) {
165 header.appendf("\n");
166 }
167 SkDebugf("%s", header.c_str());
168 }
169
170
171 // Count tests first.
172 int total = 0;
173 int toRun = 0;
174
175 for (const TestRegistry* iter = TestRegistry::Head(); iter;
176 iter = iter->next()) {
177 const Test& test = iter->factory();
178 if (should_run(test.name, test.needsGpu)) {
179 toRun++;
180 }
181 total++;
182 }
183
184 // Now run them.
185 int skipCount = 0;
186
187 SkTaskGroup::Enabler enabled(FLAGS_threads);
188 SkTaskGroup cpuTests;
189 SkTArray<const Test*> gpuTests;
190
191 Status status(toRun);
192 for (const TestRegistry* iter = TestRegistry::Head(); iter;
193 iter = iter->next()) {
194 const Test& test = iter->factory();
195 if (!should_run(test.name, test.needsGpu)) {
196 ++skipCount;
197 } else if (test.needsGpu) {
198 gpuTests.push_back(&test);
199 } else {
200 cpuTests.add(SkNEW_ARGS(SkTestRunnable, (test, &status)));
201 }
202 }
203
204 GrContextFactory* grContextFactoryPtr = NULL;
205 #if SK_SUPPORT_GPU
206 // Give GPU tests a context factory if that makes sense on this machine.
207 GrContextFactory grContextFactory;
208 grContextFactoryPtr = &grContextFactory;
209
210 #endif
211
212 // Run GPU tests on this thread.
213 for (int i = 0; i < gpuTests.count(); i++) {
214 SkNEW_ARGS(SkTestRunnable, (*gpuTests[i], &status, grContextFactoryPtr))
215 ->run();
216 }
217
218 // Block until threaded tests finish.
219 cpuTests.wait();
220
221 if (FLAGS_verbose) {
222 SkDebugf(
223 "\nFinished %d tests, %d failures, %d skipped. "
224 "(%d internal tests)",
225 toRun, status.failCount(), skipCount, status.testCount());
226 }
227
228 SkDebugf("\n");
229 return (status.failCount() == 0) ? 0 : 1;
230 }
231
232 #if !defined(SK_BUILD_FOR_IOS)
main(int argc,char ** argv)233 int main(int argc, char** argv) {
234 SkCommandLineFlags::Parse(argc, argv);
235 return test_main();
236 }
237 #endif
238