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