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" |
herb | 62a69c2 | 2015-09-29 11:47:45 -0700 | [diff] [blame] | 11 | #include "SkAtomics.h" |
caryclark | 17f0b6d | 2014-07-22 10:15:34 -0700 | [diff] [blame] | 12 | #include "SkCommonFlags.h" |
reed@android.com | 5e5adfd | 2009-03-07 03:39:23 +0000 | [diff] [blame] | 13 | #include "SkGraphics.h" |
commit-bot@chromium.org | 0506b9d | 2013-04-22 16:43:07 +0000 | [diff] [blame] | 14 | #include "SkOSFile.h" |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame] | 15 | #include "SkTArray.h" |
mtklein | 406654b | 2014-09-03 15:34:37 -0700 | [diff] [blame] | 16 | #include "SkTaskGroup.h" |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame] | 17 | #include "SkTemplates.h" |
commit-bot@chromium.org | 0506b9d | 2013-04-22 16:43:07 +0000 | [diff] [blame] | 18 | #include "SkTime.h" |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 19 | #include "Test.h" |
| 20 | |
robertphillips@google.com | bdb1be5 | 2012-09-07 18:24:43 +0000 | [diff] [blame] | 21 | #if SK_SUPPORT_GPU |
| 22 | #include "GrContext.h" |
commit-bot@chromium.org | 0dc5bd1 | 2014-02-26 16:31:22 +0000 | [diff] [blame] | 23 | #include "GrContextFactory.h" |
robertphillips@google.com | bdb1be5 | 2012-09-07 18:24:43 +0000 | [diff] [blame] | 24 | #endif |
| 25 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 26 | using namespace skiatest; |
| 27 | |
commit-bot@chromium.org | ba59d64 | 2013-04-11 16:54:09 +0000 | [diff] [blame] | 28 | DEFINE_bool2(extendedTest, x, false, "run extended tests for pathOps."); |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame] | 29 | |
commit-bot@chromium.org | 261c666 | 2014-01-02 16:19:53 +0000 | [diff] [blame] | 30 | // need to explicitly declare this, or we get some weird infinite loop llist |
| 31 | template TestRegistry* TestRegistry::gHead; |
caryclark | 26ad22a | 2015-10-16 09:03:38 -0700 | [diff] [blame] | 32 | void (*gVerboseFinalize)() = nullptr; |
commit-bot@chromium.org | 261c666 | 2014-01-02 16:19:53 +0000 | [diff] [blame] | 33 | |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 34 | // The threads report back to this object when they are done. |
| 35 | class Status { |
commit-bot@chromium.org | 261c666 | 2014-01-02 16:19:53 +0000 | [diff] [blame] | 36 | public: |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 37 | explicit Status(int total) |
| 38 | : fDone(0), fTestCount(0), fFailCount(0), fTotal(total) {} |
| 39 | // Threadsafe. |
| 40 | void endTest(const char* testName, |
| 41 | bool success, |
| 42 | SkMSec elapsed, |
| 43 | int testCount) { |
commit-bot@chromium.org | 261c666 | 2014-01-02 16:19:53 +0000 | [diff] [blame] | 44 | const int done = 1 + sk_atomic_inc(&fDone); |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 45 | for (int i = 0; i < testCount; ++i) { |
| 46 | sk_atomic_inc(&fTestCount); |
| 47 | } |
| 48 | if (!success) { |
| 49 | SkDebugf("\n---- %s FAILED", testName); |
commit-bot@chromium.org | 261c666 | 2014-01-02 16:19:53 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | SkString prefix(kSkOverwriteLine); |
| 53 | SkString time; |
| 54 | if (FLAGS_verbose) { |
| 55 | prefix.printf("\n"); |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 56 | time.printf("%5dms ", elapsed); |
commit-bot@chromium.org | 261c666 | 2014-01-02 16:19:53 +0000 | [diff] [blame] | 57 | } |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 58 | SkDebugf("%s[%3d/%3d] %s%s", prefix.c_str(), done, fTotal, time.c_str(), |
| 59 | testName); |
commit-bot@chromium.org | 261c666 | 2014-01-02 16:19:53 +0000 | [diff] [blame] | 60 | } |
| 61 | |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 62 | void reportFailure() { sk_atomic_inc(&fFailCount); } |
| 63 | |
| 64 | int32_t testCount() { return fTestCount; } |
| 65 | int32_t failCount() { return fFailCount; } |
| 66 | |
commit-bot@chromium.org | 261c666 | 2014-01-02 16:19:53 +0000 | [diff] [blame] | 67 | private: |
| 68 | int32_t fDone; // atomic |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 69 | int32_t fTestCount; // atomic |
| 70 | int32_t fFailCount; // atomic |
commit-bot@chromium.org | 261c666 | 2014-01-02 16:19:53 +0000 | [diff] [blame] | 71 | const int fTotal; |
| 72 | }; |
| 73 | |
mtklein | 048494c | 2016-02-16 19:06:15 -0800 | [diff] [blame] | 74 | class SkTestRunnable { |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame] | 75 | public: |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 76 | SkTestRunnable(const Test& test, |
| 77 | Status* status, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 78 | GrContextFactory* grContextFactory = nullptr) |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 79 | : fTest(test), fStatus(status), fGrContextFactory(grContextFactory) {} |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame] | 80 | |
mtklein | 048494c | 2016-02-16 19:06:15 -0800 | [diff] [blame] | 81 | void operator()() { |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 82 | struct TestReporter : public skiatest::Reporter { |
| 83 | public: |
| 84 | TestReporter() : fError(false), fTestCount(0) {} |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 85 | void bumpTestCount() override { ++fTestCount; } |
| 86 | bool allowExtendedTest() const override { |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 87 | return FLAGS_extendedTest; |
| 88 | } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 89 | bool verbose() const override { return FLAGS_veryVerbose; } |
| 90 | void reportFailed(const skiatest::Failure& failure) override { |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 91 | SkDebugf("\nFAILED: %s", failure.toString().c_str()); |
| 92 | fError = true; |
| 93 | } |
| 94 | bool fError; |
| 95 | int fTestCount; |
| 96 | } reporter; |
| 97 | |
| 98 | const SkMSec start = SkTime::GetMSecs(); |
| 99 | fTest.proc(&reporter, fGrContextFactory); |
| 100 | SkMSec elapsed = SkTime::GetMSecs() - start; |
| 101 | if (reporter.fError) { |
| 102 | fStatus->reportFailure(); |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame] | 103 | } |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 104 | fStatus->endTest(fTest.name, !reporter.fError, elapsed, |
| 105 | reporter.fTestCount); |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | private: |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 109 | Test fTest; |
| 110 | Status* fStatus; |
| 111 | GrContextFactory* fGrContextFactory; |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame] | 112 | }; |
commit-bot@chromium.org | ba59d64 | 2013-04-11 16:54:09 +0000 | [diff] [blame] | 113 | |
commit-bot@chromium.org | 5a47b09 | 2014-01-30 15:30:50 +0000 | [diff] [blame] | 114 | static bool should_run(const char* testName, bool isGPUTest) { |
| 115 | if (SkCommandLineFlags::ShouldSkip(FLAGS_match, testName)) { |
| 116 | return false; |
| 117 | } |
| 118 | if (!FLAGS_cpu && !isGPUTest) { |
| 119 | return false; |
| 120 | } |
| 121 | if (!FLAGS_gpu && isGPUTest) { |
| 122 | return false; |
| 123 | } |
| 124 | return true; |
| 125 | } |
| 126 | |
caryclark | 17f0b6d | 2014-07-22 10:15:34 -0700 | [diff] [blame] | 127 | int test_main(); |
| 128 | int test_main() { |
mtklein | 30e6e2a | 2014-06-18 11:44:15 -0700 | [diff] [blame] | 129 | SetupCrashHandler(); |
commit-bot@chromium.org | ba59d64 | 2013-04-11 16:54:09 +0000 | [diff] [blame] | 130 | |
tfarina | a71d3af | 2014-11-07 06:12:30 -0800 | [diff] [blame] | 131 | SkAutoGraphics ag; |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 132 | |
reed@google.com | 91d449e | 2011-10-26 15:25:18 +0000 | [diff] [blame] | 133 | { |
| 134 | SkString header("Skia UnitTests:"); |
reed@google.com | 9aff148 | 2013-04-11 18:27:52 +0000 | [diff] [blame] | 135 | if (!FLAGS_match.isEmpty()) { |
caryclark@google.com | b631eec | 2013-05-02 13:14:40 +0000 | [diff] [blame] | 136 | header.appendf(" --match"); |
| 137 | for (int index = 0; index < FLAGS_match.count(); ++index) { |
| 138 | header.appendf(" %s", FLAGS_match[index]); |
| 139 | } |
reed@google.com | 91d449e | 2011-10-26 15:25:18 +0000 | [diff] [blame] | 140 | } |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 141 | SkString tmpDir = skiatest::GetTmpDir(); |
scroggo@google.com | c76218d | 2013-06-06 14:59:56 +0000 | [diff] [blame] | 142 | if (!tmpDir.isEmpty()) { |
| 143 | header.appendf(" --tmpDir %s", tmpDir.c_str()); |
djsollen@google.com | cb62650 | 2013-03-20 13:48:20 +0000 | [diff] [blame] | 144 | } |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 145 | SkString resourcePath = GetResourcePath(); |
scroggo@google.com | c76218d | 2013-06-06 14:59:56 +0000 | [diff] [blame] | 146 | if (!resourcePath.isEmpty()) { |
| 147 | header.appendf(" --resourcePath %s", resourcePath.c_str()); |
reed@google.com | 789c6f2 | 2013-02-25 20:24:24 +0000 | [diff] [blame] | 148 | } |
reed@google.com | 91d449e | 2011-10-26 15:25:18 +0000 | [diff] [blame] | 149 | #ifdef SK_DEBUG |
| 150 | header.append(" SK_DEBUG"); |
| 151 | #else |
| 152 | header.append(" SK_RELEASE"); |
| 153 | #endif |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 154 | if (FLAGS_veryVerbose) { |
| 155 | header.appendf("\n"); |
| 156 | } |
kkinnunen | 297aaf9 | 2015-02-19 06:32:12 -0800 | [diff] [blame] | 157 | SkDebugf("%s", header.c_str()); |
reed@google.com | 91d449e | 2011-10-26 15:25:18 +0000 | [diff] [blame] | 158 | } |
| 159 | |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 160 | |
commit-bot@chromium.org | 0506b9d | 2013-04-22 16:43:07 +0000 | [diff] [blame] | 161 | // Count tests first. |
| 162 | int total = 0; |
| 163 | int toRun = 0; |
sglez@google.com | 586db93 | 2013-07-24 17:24:23 +0000 | [diff] [blame] | 164 | |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 165 | for (const TestRegistry* iter = TestRegistry::Head(); iter; |
| 166 | iter = iter->next()) { |
| 167 | const Test& test = iter->factory(); |
| 168 | if (should_run(test.name, test.needsGpu)) { |
commit-bot@chromium.org | 0506b9d | 2013-04-22 16:43:07 +0000 | [diff] [blame] | 169 | toRun++; |
| 170 | } |
| 171 | total++; |
| 172 | } |
commit-bot@chromium.org | 0506b9d | 2013-04-22 16:43:07 +0000 | [diff] [blame] | 173 | |
| 174 | // Now run them. |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 175 | int skipCount = 0; |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame] | 176 | |
mtklein | 406654b | 2014-09-03 15:34:37 -0700 | [diff] [blame] | 177 | SkTaskGroup::Enabler enabled(FLAGS_threads); |
| 178 | SkTaskGroup cpuTests; |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 179 | SkTArray<const Test*> gpuTests; |
commit-bot@chromium.org | 261c666 | 2014-01-02 16:19:53 +0000 | [diff] [blame] | 180 | |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 181 | Status status(toRun); |
| 182 | for (const TestRegistry* iter = TestRegistry::Head(); iter; |
| 183 | iter = iter->next()) { |
| 184 | const Test& test = iter->factory(); |
| 185 | if (!should_run(test.name, test.needsGpu)) { |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 186 | ++skipCount; |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 187 | } else if (test.needsGpu) { |
| 188 | gpuTests.push_back(&test); |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 189 | } else { |
mtklein | 048494c | 2016-02-16 19:06:15 -0800 | [diff] [blame] | 190 | cpuTests.add(SkTestRunnable(test, &status)); |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 191 | } |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 192 | } |
reed@android.com | 57b799e | 2009-04-01 20:26:42 +0000 | [diff] [blame] | 193 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 194 | GrContextFactory* grContextFactoryPtr = nullptr; |
commit-bot@chromium.org | 0dc5bd1 | 2014-02-26 16:31:22 +0000 | [diff] [blame] | 195 | #if SK_SUPPORT_GPU |
| 196 | // Give GPU tests a context factory if that makes sense on this machine. |
| 197 | GrContextFactory grContextFactory; |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 198 | grContextFactoryPtr = &grContextFactory; |
| 199 | |
commit-bot@chromium.org | 0dc5bd1 | 2014-02-26 16:31:22 +0000 | [diff] [blame] | 200 | #endif |
| 201 | |
| 202 | // Run GPU tests on this thread. |
| 203 | for (int i = 0; i < gpuTests.count(); i++) { |
mtklein | 048494c | 2016-02-16 19:06:15 -0800 | [diff] [blame] | 204 | SkTestRunnable(*gpuTests[i], &status, grContextFactoryPtr)(); |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame] | 205 | } |
| 206 | |
commit-bot@chromium.org | a7538ba | 2013-10-10 18:49:04 +0000 | [diff] [blame] | 207 | // Block until threaded tests finish. |
mtklein | 406654b | 2014-09-03 15:34:37 -0700 | [diff] [blame] | 208 | cpuTests.wait(); |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame] | 209 | |
commit-bot@chromium.org | 261c666 | 2014-01-02 16:19:53 +0000 | [diff] [blame] | 210 | if (FLAGS_verbose) { |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 211 | SkDebugf( |
| 212 | "\nFinished %d tests, %d failures, %d skipped. " |
| 213 | "(%d internal tests)", |
| 214 | toRun, status.failCount(), skipCount, status.testCount()); |
caryclark | 26ad22a | 2015-10-16 09:03:38 -0700 | [diff] [blame] | 215 | if (gVerboseFinalize) { |
| 216 | (*gVerboseFinalize)(); |
| 217 | } |
caryclark@google.com | d54e1e9 | 2013-04-10 15:57:31 +0000 | [diff] [blame] | 218 | } |
reed@google.com | a276975 | 2012-07-22 22:33:05 +0000 | [diff] [blame] | 219 | |
commit-bot@chromium.org | 261c666 | 2014-01-02 16:19:53 +0000 | [diff] [blame] | 220 | SkDebugf("\n"); |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 221 | return (status.failCount() == 0) ? 0 : 1; |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 222 | } |
caryclark@google.com | 5987f58 | 2012-10-02 18:33:14 +0000 | [diff] [blame] | 223 | |
borenet | 4808757 | 2015-04-02 12:16:36 -0700 | [diff] [blame] | 224 | #if !defined(SK_BUILD_FOR_IOS) |
caryclark | 17f0b6d | 2014-07-22 10:15:34 -0700 | [diff] [blame] | 225 | int main(int argc, char** argv) { |
| 226 | SkCommandLineFlags::Parse(argc, argv); |
| 227 | return test_main(); |
caryclark@google.com | 5987f58 | 2012-10-02 18:33:14 +0000 | [diff] [blame] | 228 | } |
| 229 | #endif |