epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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 | */ |
commit-bot@chromium.org | ba59d64 | 2013-04-11 16:54:09 +0000 | [diff] [blame] | 7 | |
mtklein | 30e6e2a | 2014-06-18 11:44:15 -0700 | [diff] [blame] | 8 | #include "CrashHandler.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 9 | #include "OverwriteLine.h" |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 10 | #include "Resources.h" |
scroggo@google.com | 5a6324e | 2013-04-11 20:11:40 +0000 | [diff] [blame] | 11 | #include "SkCommandLineFlags.h" |
reed@android.com | 5e5adfd | 2009-03-07 03:39:23 +0000 | [diff] [blame] | 12 | #include "SkGraphics.h" |
commit-bot@chromium.org | 0506b9d | 2013-04-22 16:43:07 +0000 | [diff] [blame] | 13 | #include "SkOSFile.h" |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame] | 14 | #include "SkTArray.h" |
| 15 | #include "SkTemplates.h" |
commit-bot@chromium.org | 0506b9d | 2013-04-22 16:43:07 +0000 | [diff] [blame] | 16 | #include "SkThreadPool.h" |
| 17 | #include "SkTime.h" |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 18 | #include "Test.h" |
| 19 | |
robertphillips@google.com | bdb1be5 | 2012-09-07 18:24:43 +0000 | [diff] [blame] | 20 | #if SK_SUPPORT_GPU |
| 21 | #include "GrContext.h" |
commit-bot@chromium.org | 0dc5bd1 | 2014-02-26 16:31:22 +0000 | [diff] [blame] | 22 | #include "GrContextFactory.h" |
robertphillips@google.com | bdb1be5 | 2012-09-07 18:24:43 +0000 | [diff] [blame] | 23 | #endif |
| 24 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 25 | using namespace skiatest; |
| 26 | |
caryclark@google.com | b631eec | 2013-05-02 13:14:40 +0000 | [diff] [blame] | 27 | DEFINE_string2(match, m, NULL, "[~][^]substring[$] [...] of test name to run.\n" \ |
| 28 | "Multiple matches may be separated by spaces.\n" \ |
| 29 | "~ causes a matching test to always be skipped\n" \ |
| 30 | "^ requires the start of the test to match\n" \ |
| 31 | "$ requires the end of the test to match\n" \ |
| 32 | "^ and $ requires an exact match\n" \ |
| 33 | "If a test does not match any list entry,\n" \ |
| 34 | "it is skipped unless some list entry starts with ~"); |
commit-bot@chromium.org | ba59d64 | 2013-04-11 16:54:09 +0000 | [diff] [blame] | 35 | DEFINE_bool2(extendedTest, x, false, "run extended tests for pathOps."); |
commit-bot@chromium.org | 6dda827 | 2014-01-23 17:21:19 +0000 | [diff] [blame] | 36 | DEFINE_bool2(leaks, l, false, "show leaked ref cnt'd objects."); |
caryclark@google.com | 8d0a524 | 2013-07-16 16:11:16 +0000 | [diff] [blame] | 37 | DEFINE_bool2(single, z, false, "run tests on a single thread internally."); |
commit-bot@chromium.org | 11ea96c | 2014-01-28 21:15:42 +0000 | [diff] [blame] | 38 | DEFINE_bool2(verbose, v, false, "enable verbose output from the test driver."); |
| 39 | DEFINE_bool2(veryVerbose, V, false, "tell individual tests to be verbose."); |
commit-bot@chromium.org | 5a47b09 | 2014-01-30 15:30:50 +0000 | [diff] [blame] | 40 | DEFINE_bool(cpu, true, "whether or not to run CPU tests."); |
| 41 | DEFINE_bool(gpu, true, "whether or not to run GPU tests."); |
commit-bot@chromium.org | 44c661f | 2013-04-22 15:23:14 +0000 | [diff] [blame] | 42 | DEFINE_int32(threads, SkThreadPool::kThreadPerCore, |
| 43 | "Run threadsafe tests on a threadpool with this many threads."); |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame] | 44 | |
commit-bot@chromium.org | 261c666 | 2014-01-02 16:19:53 +0000 | [diff] [blame] | 45 | // need to explicitly declare this, or we get some weird infinite loop llist |
| 46 | template TestRegistry* TestRegistry::gHead; |
| 47 | |
| 48 | class Iter { |
| 49 | public: |
| 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 | |
| 64 | private: |
| 65 | const TestRegistry* fReg; |
| 66 | }; |
| 67 | |
| 68 | class DebugfReporter : public Reporter { |
| 69 | public: |
| 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.org | 11ea96c | 2014-01-28 21:15:42 +0000 | [diff] [blame] | 74 | virtual bool verbose() const SK_OVERRIDE { return FLAGS_veryVerbose; } |
commit-bot@chromium.org | 261c666 | 2014-01-02 16:19:53 +0000 | [diff] [blame] | 75 | |
| 76 | protected: |
| 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 | |
| 97 | private: |
| 98 | int32_t fDone; // atomic |
| 99 | const int fTotal; |
| 100 | }; |
| 101 | |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame] | 102 | // Deletes self when run. |
| 103 | class SkTestRunnable : public SkRunnable { |
| 104 | public: |
| 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 | |
| 116 | private: |
| 117 | SkAutoTDelete<Test> fTest; |
| 118 | int32_t* fFailCount; |
| 119 | }; |
commit-bot@chromium.org | ba59d64 | 2013-04-11 16:54:09 +0000 | [diff] [blame] | 120 | |
commit-bot@chromium.org | 5a47b09 | 2014-01-30 15:30:50 +0000 | [diff] [blame] | 121 | static 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.com | 5987f58 | 2012-10-02 18:33:14 +0000 | [diff] [blame] | 134 | int tool_main(int argc, char** argv); |
| 135 | int tool_main(int argc, char** argv) { |
mtklein | 30e6e2a | 2014-06-18 11:44:15 -0700 | [diff] [blame] | 136 | SetupCrashHandler(); |
commit-bot@chromium.org | ba59d64 | 2013-04-11 16:54:09 +0000 | [diff] [blame] | 137 | SkCommandLineFlags::SetUsage(""); |
| 138 | SkCommandLineFlags::Parse(argc, argv); |
| 139 | |
bsalomon@google.com | 4e23068 | 2013-01-15 20:37:04 +0000 | [diff] [blame] | 140 | #if SK_ENABLE_INST_COUNT |
commit-bot@chromium.org | 6dda827 | 2014-01-23 17:21:19 +0000 | [diff] [blame] | 141 | if (FLAGS_leaks) { |
| 142 | gPrintInstCount = true; |
| 143 | } |
reed@google.com | a276975 | 2012-07-22 22:33:05 +0000 | [diff] [blame] | 144 | #endif |
caryclark@google.com | d54e1e9 | 2013-04-10 15:57:31 +0000 | [diff] [blame] | 145 | |
reed@google.com | a276975 | 2012-07-22 22:33:05 +0000 | [diff] [blame] | 146 | SkGraphics::Init(); |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 147 | |
reed@google.com | 91d449e | 2011-10-26 15:25:18 +0000 | [diff] [blame] | 148 | { |
| 149 | SkString header("Skia UnitTests:"); |
reed@google.com | 9aff148 | 2013-04-11 18:27:52 +0000 | [diff] [blame] | 150 | if (!FLAGS_match.isEmpty()) { |
caryclark@google.com | b631eec | 2013-05-02 13:14:40 +0000 | [diff] [blame] | 151 | header.appendf(" --match"); |
| 152 | for (int index = 0; index < FLAGS_match.count(); ++index) { |
| 153 | header.appendf(" %s", FLAGS_match[index]); |
| 154 | } |
reed@google.com | 91d449e | 2011-10-26 15:25:18 +0000 | [diff] [blame] | 155 | } |
scroggo@google.com | c76218d | 2013-06-06 14:59:56 +0000 | [diff] [blame] | 156 | SkString tmpDir = Test::GetTmpDir(); |
| 157 | if (!tmpDir.isEmpty()) { |
| 158 | header.appendf(" --tmpDir %s", tmpDir.c_str()); |
djsollen@google.com | cb62650 | 2013-03-20 13:48:20 +0000 | [diff] [blame] | 159 | } |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 160 | SkString resourcePath = GetResourcePath(); |
scroggo@google.com | c76218d | 2013-06-06 14:59:56 +0000 | [diff] [blame] | 161 | if (!resourcePath.isEmpty()) { |
| 162 | header.appendf(" --resourcePath %s", resourcePath.c_str()); |
reed@google.com | 789c6f2 | 2013-02-25 20:24:24 +0000 | [diff] [blame] | 163 | } |
reed@google.com | 91d449e | 2011-10-26 15:25:18 +0000 | [diff] [blame] | 164 | #ifdef SK_DEBUG |
| 165 | header.append(" SK_DEBUG"); |
| 166 | #else |
| 167 | header.append(" SK_RELEASE"); |
| 168 | #endif |
reed@google.com | 5696baa | 2013-08-29 20:20:39 +0000 | [diff] [blame] | 169 | header.appendf(" skia_arch_width=%d", (int)sizeof(void*) * 8); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 170 | if (FLAGS_veryVerbose) { |
| 171 | header.appendf("\n"); |
| 172 | } |
commit-bot@chromium.org | 261c666 | 2014-01-02 16:19:53 +0000 | [diff] [blame] | 173 | SkDebugf(header.c_str()); |
reed@google.com | 91d449e | 2011-10-26 15:25:18 +0000 | [diff] [blame] | 174 | } |
| 175 | |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 176 | |
commit-bot@chromium.org | 0506b9d | 2013-04-22 16:43:07 +0000 | [diff] [blame] | 177 | // Count tests first. |
| 178 | int total = 0; |
| 179 | int toRun = 0; |
| 180 | Test* test; |
sglez@google.com | 586db93 | 2013-07-24 17:24:23 +0000 | [diff] [blame] | 181 | |
commit-bot@chromium.org | 261c666 | 2014-01-02 16:19:53 +0000 | [diff] [blame] | 182 | Iter iter; |
| 183 | while ((test = iter.next(NULL/*reporter not needed*/)) != NULL) { |
commit-bot@chromium.org | 0506b9d | 2013-04-22 16:43:07 +0000 | [diff] [blame] | 184 | SkAutoTDelete<Test> owned(test); |
commit-bot@chromium.org | 5a47b09 | 2014-01-30 15:30:50 +0000 | [diff] [blame] | 185 | if (should_run(test->getName(), test->isGPUTest())) { |
commit-bot@chromium.org | 0506b9d | 2013-04-22 16:43:07 +0000 | [diff] [blame] | 186 | toRun++; |
| 187 | } |
| 188 | total++; |
| 189 | } |
commit-bot@chromium.org | 0506b9d | 2013-04-22 16:43:07 +0000 | [diff] [blame] | 190 | |
| 191 | // Now run them. |
| 192 | iter.reset(); |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame] | 193 | int32_t failCount = 0; |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 194 | int skipCount = 0; |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame] | 195 | |
commit-bot@chromium.org | a7538ba | 2013-10-10 18:49:04 +0000 | [diff] [blame] | 196 | SkThreadPool threadpool(FLAGS_threads); |
commit-bot@chromium.org | 0dc5bd1 | 2014-02-26 16:31:22 +0000 | [diff] [blame] | 197 | SkTArray<Test*> gpuTests; // Always passes ownership to an SkTestRunnable |
commit-bot@chromium.org | 261c666 | 2014-01-02 16:19:53 +0000 | [diff] [blame] | 198 | |
| 199 | DebugfReporter reporter(toRun); |
commit-bot@chromium.org | 0506b9d | 2013-04-22 16:43:07 +0000 | [diff] [blame] | 200 | for (int i = 0; i < total; i++) { |
commit-bot@chromium.org | 261c666 | 2014-01-02 16:19:53 +0000 | [diff] [blame] | 201 | SkAutoTDelete<Test> test(iter.next(&reporter)); |
commit-bot@chromium.org | 5a47b09 | 2014-01-30 15:30:50 +0000 | [diff] [blame] | 202 | if (!should_run(test->getName(), test->isGPUTest())) { |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 203 | ++skipCount; |
commit-bot@chromium.org | 5a47b09 | 2014-01-30 15:30:50 +0000 | [diff] [blame] | 204 | } else if (test->isGPUTest()) { |
commit-bot@chromium.org | 0dc5bd1 | 2014-02-26 16:31:22 +0000 | [diff] [blame] | 205 | gpuTests.push_back() = test.detach(); |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 206 | } else { |
commit-bot@chromium.org | a7538ba | 2013-10-10 18:49:04 +0000 | [diff] [blame] | 207 | threadpool.add(SkNEW_ARGS(SkTestRunnable, (test.detach(), &failCount))); |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 208 | } |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 209 | } |
reed@android.com | 57b799e | 2009-04-01 20:26:42 +0000 | [diff] [blame] | 210 | |
commit-bot@chromium.org | 0dc5bd1 | 2014-02-26 16:31:22 +0000 | [diff] [blame] | 211 | #if SK_SUPPORT_GPU |
| 212 | // Give GPU tests a context factory if that makes sense on this machine. |
| 213 | GrContextFactory grContextFactory; |
| 214 | for (int i = 0; i < gpuTests.count(); i++) { |
| 215 | gpuTests[i]->setGrContextFactory(&grContextFactory); |
| 216 | } |
| 217 | #endif |
| 218 | |
| 219 | // Run GPU tests on this thread. |
| 220 | for (int i = 0; i < gpuTests.count(); i++) { |
| 221 | SkNEW_ARGS(SkTestRunnable, (gpuTests[i], &failCount))->run(); |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame] | 222 | } |
| 223 | |
commit-bot@chromium.org | a7538ba | 2013-10-10 18:49:04 +0000 | [diff] [blame] | 224 | // Block until threaded tests finish. |
| 225 | threadpool.wait(); |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame] | 226 | |
commit-bot@chromium.org | 261c666 | 2014-01-02 16:19:53 +0000 | [diff] [blame] | 227 | if (FLAGS_verbose) { |
| 228 | SkDebugf("\nFinished %d tests, %d failures, %d skipped. (%d internal tests)", |
| 229 | toRun, failCount, skipCount, reporter.countTests()); |
caryclark@google.com | d54e1e9 | 2013-04-10 15:57:31 +0000 | [diff] [blame] | 230 | } |
reed@google.com | a276975 | 2012-07-22 22:33:05 +0000 | [diff] [blame] | 231 | SkGraphics::Term(); |
| 232 | |
commit-bot@chromium.org | 261c666 | 2014-01-02 16:19:53 +0000 | [diff] [blame] | 233 | SkDebugf("\n"); |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 234 | return (failCount == 0) ? 0 : 1; |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 235 | } |
caryclark@google.com | 5987f58 | 2012-10-02 18:33:14 +0000 | [diff] [blame] | 236 | |
borenet@google.com | 7158e6a | 2012-11-01 17:43:44 +0000 | [diff] [blame] | 237 | #if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL) |
caryclark@google.com | 5987f58 | 2012-10-02 18:33:14 +0000 | [diff] [blame] | 238 | int main(int argc, char * const argv[]) { |
| 239 | return tool_main(argc, (char**) argv); |
| 240 | } |
| 241 | #endif |