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 | */ |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 7 | #ifndef skiatest_Test_DEFINED |
| 8 | #define skiatest_Test_DEFINED |
| 9 | |
Mike Reed | ab273fa | 2017-01-11 13:58:55 -0500 | [diff] [blame] | 10 | #include "../tools/Registry.h" |
Hal Canary | 23564b9 | 2018-09-07 14:33:14 -0400 | [diff] [blame] | 11 | #include "GrContextFactory.h" |
Mike Reed | ebfce6d | 2016-12-12 10:02:12 -0500 | [diff] [blame] | 12 | #include "SkClipOpPriv.h" |
Mike Klein | b323a5e | 2017-07-24 15:21:31 -0400 | [diff] [blame] | 13 | #include "SkString.h" |
| 14 | #include "SkTraceEvent.h" |
| 15 | #include "SkTypes.h" |
bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 16 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 17 | namespace skiatest { |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 18 | |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 19 | SkString GetTmpDir(); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 20 | |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 21 | struct Failure { |
| 22 | Failure(const char* f, int l, const char* c, const SkString& m) |
| 23 | : fileName(f), lineNo(l), condition(c), message(m) {} |
| 24 | const char* fileName; |
| 25 | int lineNo; |
| 26 | const char* condition; |
| 27 | SkString message; |
| 28 | SkString toString() const; |
| 29 | }; |
scroggo | 0ee2627 | 2014-11-07 06:07:32 -0800 | [diff] [blame] | 30 | |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 31 | class Reporter : SkNoncopyable { |
| 32 | public: |
| 33 | virtual ~Reporter() {} |
| 34 | virtual void bumpTestCount(); |
| 35 | virtual void reportFailed(const skiatest::Failure&) = 0; |
| 36 | virtual bool allowExtendedTest() const; |
| 37 | virtual bool verbose() const; |
Cary Clark | ab87d7a | 2016-10-04 10:01:04 -0400 | [diff] [blame] | 38 | virtual void* stats() const { return nullptr; } |
Brian Osman | 287f651 | 2016-12-05 11:35:07 -0500 | [diff] [blame] | 39 | |
Mike Klein | 4d907da | 2019-02-07 09:54:30 -0500 | [diff] [blame] | 40 | void reportFailedWithContext(const skiatest::Failure&); |
| 41 | |
Brian Osman | 287f651 | 2016-12-05 11:35:07 -0500 | [diff] [blame] | 42 | void push(const SkString& message) { |
| 43 | fContextStack.push_back(message); |
| 44 | } |
| 45 | void pop() { |
| 46 | fContextStack.pop_back(); |
| 47 | } |
| 48 | |
| 49 | private: |
| 50 | SkTArray<SkString> fContextStack; |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 51 | }; |
scroggo | 0ee2627 | 2014-11-07 06:07:32 -0800 | [diff] [blame] | 52 | |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 53 | #define REPORT_FAILURE(reporter, cond, message) \ |
Brian Osman | 287f651 | 2016-12-05 11:35:07 -0500 | [diff] [blame] | 54 | reporter->reportFailedWithContext(skiatest::Failure(__FILE__, __LINE__, cond, message)) |
| 55 | |
| 56 | class ReporterContext : SkNoncopyable { |
| 57 | public: |
| 58 | ReporterContext(Reporter* reporter, const SkString& message) : fReporter(reporter) { |
| 59 | fReporter->push(message); |
| 60 | } |
| 61 | ~ReporterContext() { |
| 62 | fReporter->pop(); |
| 63 | } |
| 64 | |
| 65 | private: |
| 66 | Reporter* fReporter; |
| 67 | }; |
scroggo | 0ee2627 | 2014-11-07 06:07:32 -0800 | [diff] [blame] | 68 | |
Brian Salomon | dcfca43 | 2017-11-15 15:48:03 -0500 | [diff] [blame] | 69 | typedef void (*TestProc)(skiatest::Reporter*, const GrContextOptions&); |
Robert Phillips | ec32534 | 2017-10-30 18:02:48 +0000 | [diff] [blame] | 70 | typedef void (*ContextOptionsProc)(GrContextOptions*); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 71 | |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 72 | struct Test { |
Robert Phillips | ec32534 | 2017-10-30 18:02:48 +0000 | [diff] [blame] | 73 | Test(const char* n, bool g, TestProc p, ContextOptionsProc optionsProc = nullptr) |
| 74 | : name(n), needsGpu(g), proc(p), fContextOptionsProc(optionsProc) {} |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 75 | const char* name; |
| 76 | bool needsGpu; |
| 77 | TestProc proc; |
Robert Phillips | ec32534 | 2017-10-30 18:02:48 +0000 | [diff] [blame] | 78 | ContextOptionsProc fContextOptionsProc; |
| 79 | |
| 80 | void modifyGrContextOptions(GrContextOptions* options) { |
| 81 | if (fContextOptionsProc) { |
| 82 | (*fContextOptionsProc)(options); |
| 83 | } |
| 84 | } |
Mike Klein | b323a5e | 2017-07-24 15:21:31 -0400 | [diff] [blame] | 85 | |
Brian Salomon | dcfca43 | 2017-11-15 15:48:03 -0500 | [diff] [blame] | 86 | void run(skiatest::Reporter* r, const GrContextOptions& options) const { |
Mike Klein | b323a5e | 2017-07-24 15:21:31 -0400 | [diff] [blame] | 87 | TRACE_EVENT1("test", TRACE_FUNC, "name", this->name/*these are static*/); |
Brian Salomon | dcfca43 | 2017-11-15 15:48:03 -0500 | [diff] [blame] | 88 | this->proc(r, options); |
Mike Klein | b323a5e | 2017-07-24 15:21:31 -0400 | [diff] [blame] | 89 | } |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 90 | }; |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 91 | |
Mike Reed | ab273fa | 2017-01-11 13:58:55 -0500 | [diff] [blame] | 92 | typedef sk_tools::Registry<Test> TestRegistry; |
commit-bot@chromium.org | e2eac8b | 2014-01-14 21:04:37 +0000 | [diff] [blame] | 93 | |
| 94 | /* |
| 95 | Use the following macros to make use of the skiatest classes, e.g. |
| 96 | |
| 97 | #include "Test.h" |
| 98 | |
| 99 | DEF_TEST(TestName, reporter) { |
| 100 | ... |
| 101 | REPORTER_ASSERT(reporter, x == 15); |
| 102 | ... |
Brian Salomon | 1c80e99 | 2018-01-29 09:50:47 -0500 | [diff] [blame] | 103 | REPORTER_ASSERT(reporter, x == 15, "x should be 15"); |
commit-bot@chromium.org | e2eac8b | 2014-01-14 21:04:37 +0000 | [diff] [blame] | 104 | ... |
| 105 | if (x != 15) { |
| 106 | ERRORF(reporter, "x should be 15, but is %d", x); |
| 107 | return; |
| 108 | } |
| 109 | ... |
| 110 | } |
| 111 | */ |
bsalomon | 758586c | 2016-04-06 14:02:39 -0700 | [diff] [blame] | 112 | |
bsalomon | 758586c | 2016-04-06 14:02:39 -0700 | [diff] [blame] | 113 | using GrContextFactoryContextType = sk_gpu_test::GrContextFactory::ContextType; |
bsalomon | f2f1c17 | 2016-04-05 12:59:06 -0700 | [diff] [blame] | 114 | |
| 115 | typedef void GrContextTestFn(Reporter*, const sk_gpu_test::ContextInfo&); |
bsalomon | 758586c | 2016-04-06 14:02:39 -0700 | [diff] [blame] | 116 | typedef bool GrContextTypeFilterFn(GrContextFactoryContextType); |
bsalomon | f2f1c17 | 2016-04-05 12:59:06 -0700 | [diff] [blame] | 117 | |
bsalomon | 758586c | 2016-04-06 14:02:39 -0700 | [diff] [blame] | 118 | extern bool IsGLContextType(GrContextFactoryContextType); |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 119 | extern bool IsVulkanContextType(GrContextFactoryContextType); |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 120 | extern bool IsMetalContextType(GrContextFactoryContextType); |
bsalomon | 758586c | 2016-04-06 14:02:39 -0700 | [diff] [blame] | 121 | extern bool IsRenderingGLContextType(GrContextFactoryContextType); |
Brian Osman | 7c59774 | 2019-03-26 11:10:11 -0400 | [diff] [blame] | 122 | extern bool IsMockContextType(GrContextFactoryContextType); |
Brian Salomon | dcfca43 | 2017-11-15 15:48:03 -0500 | [diff] [blame] | 123 | void RunWithGPUTestContexts(GrContextTestFn*, GrContextTypeFilterFn*, Reporter*, |
| 124 | const GrContextOptions&); |
benjaminwagner | ec4d4d7 | 2016-03-25 12:59:53 -0700 | [diff] [blame] | 125 | |
| 126 | /** Timer provides wall-clock duration since its creation. */ |
| 127 | class Timer { |
| 128 | public: |
| 129 | /** Starts the timer. */ |
| 130 | Timer(); |
| 131 | |
| 132 | /** Nanoseconds since creation. */ |
| 133 | double elapsedNs() const; |
| 134 | |
| 135 | /** Milliseconds since creation. */ |
| 136 | double elapsedMs() const; |
| 137 | |
| 138 | /** Milliseconds since creation as an integer. |
| 139 | Behavior is undefined for durations longer than SK_MSecMax. |
| 140 | */ |
| 141 | SkMSec elapsedMsInt() const; |
| 142 | private: |
| 143 | double fStartNanos; |
| 144 | }; |
| 145 | |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 146 | } // namespace skiatest |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 147 | |
Brian Salomon | 1c80e99 | 2018-01-29 09:50:47 -0500 | [diff] [blame] | 148 | #define REPORTER_ASSERT(r, cond, ...) \ |
| 149 | do { \ |
| 150 | if (!(cond)) { \ |
| 151 | REPORT_FAILURE(r, #cond, SkStringPrintf(__VA_ARGS__)); \ |
| 152 | } \ |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 153 | } while (0) |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 154 | |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 155 | #define ERRORF(r, ...) \ |
| 156 | do { \ |
| 157 | REPORT_FAILURE(r, "", SkStringPrintf(__VA_ARGS__)); \ |
| 158 | } while (0) |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 159 | |
halcanary | 7d57124 | 2016-02-24 17:59:16 -0800 | [diff] [blame] | 160 | #define INFOF(REPORTER, ...) \ |
| 161 | do { \ |
| 162 | if ((REPORTER)->verbose()) { \ |
| 163 | SkDebugf(__VA_ARGS__); \ |
| 164 | } \ |
| 165 | } while (0) |
| 166 | |
Brian Salomon | dcfca43 | 2017-11-15 15:48:03 -0500 | [diff] [blame] | 167 | #define DEF_TEST(name, reporter) \ |
| 168 | static void test_##name(skiatest::Reporter*, const GrContextOptions&); \ |
| 169 | skiatest::TestRegistry name##TestRegistry(skiatest::Test(#name, false, test_##name)); \ |
| 170 | void test_##name(skiatest::Reporter* reporter, const GrContextOptions&) |
commit-bot@chromium.org | e2eac8b | 2014-01-14 21:04:37 +0000 | [diff] [blame] | 171 | |
Brian Salomon | dcfca43 | 2017-11-15 15:48:03 -0500 | [diff] [blame] | 172 | #define DEF_GPUTEST(name, reporter, options) \ |
| 173 | static void test_##name(skiatest::Reporter*, const GrContextOptions&); \ |
| 174 | skiatest::TestRegistry name##TestRegistry(skiatest::Test(#name, true, test_##name)); \ |
| 175 | void test_##name(skiatest::Reporter* reporter, const GrContextOptions& options) |
commit-bot@chromium.org | e2eac8b | 2014-01-14 21:04:37 +0000 | [diff] [blame] | 176 | |
Robert Phillips | ec32534 | 2017-10-30 18:02:48 +0000 | [diff] [blame] | 177 | #define DEF_GPUTEST_FOR_CONTEXTS(name, context_filter, reporter, context_info, options_filter) \ |
Brian Salomon | dcfca43 | 2017-11-15 15:48:03 -0500 | [diff] [blame] | 178 | static void test_##name(skiatest::Reporter*, const sk_gpu_test::ContextInfo& context_info); \ |
Robert Phillips | ec32534 | 2017-10-30 18:02:48 +0000 | [diff] [blame] | 179 | static void test_gpu_contexts_##name(skiatest::Reporter* reporter, \ |
Brian Salomon | dcfca43 | 2017-11-15 15:48:03 -0500 | [diff] [blame] | 180 | const GrContextOptions& options) { \ |
| 181 | skiatest::RunWithGPUTestContexts(test_##name, context_filter, reporter, options); \ |
Robert Phillips | ec32534 | 2017-10-30 18:02:48 +0000 | [diff] [blame] | 182 | } \ |
| 183 | skiatest::TestRegistry name##TestRegistry( \ |
| 184 | skiatest::Test(#name, true, test_gpu_contexts_##name, options_filter)); \ |
Brian Salomon | dcfca43 | 2017-11-15 15:48:03 -0500 | [diff] [blame] | 185 | void test_##name(skiatest::Reporter* reporter, const sk_gpu_test::ContextInfo& context_info) |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 186 | |
bsalomon | fda8807 | 2016-04-11 14:40:50 -0700 | [diff] [blame] | 187 | #define DEF_GPUTEST_FOR_ALL_CONTEXTS(name, reporter, context_info) \ |
Robert Phillips | ec32534 | 2017-10-30 18:02:48 +0000 | [diff] [blame] | 188 | DEF_GPUTEST_FOR_CONTEXTS(name, nullptr, reporter, context_info, nullptr) |
| 189 | |
bsalomon | 68d9134 | 2016-04-12 09:59:58 -0700 | [diff] [blame] | 190 | #define DEF_GPUTEST_FOR_RENDERING_CONTEXTS(name, reporter, context_info) \ |
| 191 | DEF_GPUTEST_FOR_CONTEXTS(name, sk_gpu_test::GrContextFactory::IsRenderingContext, \ |
Robert Phillips | ec32534 | 2017-10-30 18:02:48 +0000 | [diff] [blame] | 192 | reporter, context_info, nullptr) |
bsalomon | 758586c | 2016-04-06 14:02:39 -0700 | [diff] [blame] | 193 | #define DEF_GPUTEST_FOR_ALL_GL_CONTEXTS(name, reporter, context_info) \ |
Robert Phillips | ec32534 | 2017-10-30 18:02:48 +0000 | [diff] [blame] | 194 | DEF_GPUTEST_FOR_CONTEXTS(name, &skiatest::IsGLContextType, \ |
| 195 | reporter, context_info, nullptr) |
bsalomon | 758586c | 2016-04-06 14:02:39 -0700 | [diff] [blame] | 196 | #define DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(name, reporter, context_info) \ |
Robert Phillips | ec32534 | 2017-10-30 18:02:48 +0000 | [diff] [blame] | 197 | DEF_GPUTEST_FOR_CONTEXTS(name, &skiatest::IsRenderingGLContextType, \ |
| 198 | reporter, context_info, nullptr) |
Brian Osman | 7c59774 | 2019-03-26 11:10:11 -0400 | [diff] [blame] | 199 | #define DEF_GPUTEST_FOR_MOCK_CONTEXT(name, reporter, context_info) \ |
| 200 | DEF_GPUTEST_FOR_CONTEXTS(name, &skiatest::IsMockContextType, \ |
| 201 | reporter, context_info, nullptr) |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 202 | #define DEF_GPUTEST_FOR_VULKAN_CONTEXT(name, reporter, context_info) \ |
Robert Phillips | ec32534 | 2017-10-30 18:02:48 +0000 | [diff] [blame] | 203 | DEF_GPUTEST_FOR_CONTEXTS(name, &skiatest::IsVulkanContextType, \ |
| 204 | reporter, context_info, nullptr) |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 205 | #define DEF_GPUTEST_FOR_METAL_CONTEXT(name, reporter, context_info) \ |
| 206 | DEF_GPUTEST_FOR_CONTEXTS(name, &skiatest::IsMetalContextType, \ |
| 207 | reporter, context_info, nullptr) |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 208 | |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 209 | #define REQUIRE_PDF_DOCUMENT(TEST_NAME, REPORTER) \ |
| 210 | do { \ |
Hal Canary | 3026d4b | 2019-01-07 10:00:48 -0500 | [diff] [blame] | 211 | SkNullWStream testStream; \ |
| 212 | auto testDoc = SkPDF::MakeDocument(&testStream); \ |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 213 | if (!testDoc) { \ |
| 214 | INFOF(REPORTER, "PDF disabled; %s test skipped.", #TEST_NAME); \ |
| 215 | return; \ |
| 216 | } \ |
halcanary | 2ccdb63 | 2015-08-11 13:35:12 -0700 | [diff] [blame] | 217 | } while (false) |
| 218 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 219 | #endif |