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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkString.h" |
| 11 | #include "include/core/SkTypes.h" |
| 12 | #include "src/core/SkClipOpPriv.h" |
| 13 | #include "src/core/SkTraceEvent.h" |
| 14 | #include "tools/Registry.h" |
| 15 | #include "tools/gpu/GrContextFactory.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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 97 | #include "tests/Test.h" |
commit-bot@chromium.org | e2eac8b | 2014-01-14 21:04:37 +0000 | [diff] [blame] | 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); |
Greg Daniel | a58db7f | 2020-07-15 09:17:59 -0400 | [diff] [blame] | 121 | extern bool IsDawnContextType(GrContextFactoryContextType); |
Greg Daniel | d4928d0 | 2020-06-19 11:13:26 -0400 | [diff] [blame] | 122 | extern bool IsDirect3DContextType(GrContextFactoryContextType); |
bsalomon | 758586c | 2016-04-06 14:02:39 -0700 | [diff] [blame] | 123 | extern bool IsRenderingGLContextType(GrContextFactoryContextType); |
Jim Van Verth | 5a8f3e4 | 2019-10-24 10:23:26 -0400 | [diff] [blame] | 124 | extern bool IsRenderingGLOrMetalContextType(GrContextFactoryContextType); |
Brian Osman | 7c59774 | 2019-03-26 11:10:11 -0400 | [diff] [blame] | 125 | extern bool IsMockContextType(GrContextFactoryContextType); |
Brian Salomon | dcfca43 | 2017-11-15 15:48:03 -0500 | [diff] [blame] | 126 | void RunWithGPUTestContexts(GrContextTestFn*, GrContextTypeFilterFn*, Reporter*, |
| 127 | const GrContextOptions&); |
benjaminwagner | ec4d4d7 | 2016-03-25 12:59:53 -0700 | [diff] [blame] | 128 | |
| 129 | /** Timer provides wall-clock duration since its creation. */ |
| 130 | class Timer { |
| 131 | public: |
| 132 | /** Starts the timer. */ |
| 133 | Timer(); |
| 134 | |
| 135 | /** Nanoseconds since creation. */ |
| 136 | double elapsedNs() const; |
| 137 | |
| 138 | /** Milliseconds since creation. */ |
| 139 | double elapsedMs() const; |
| 140 | |
| 141 | /** Milliseconds since creation as an integer. |
| 142 | Behavior is undefined for durations longer than SK_MSecMax. |
| 143 | */ |
| 144 | SkMSec elapsedMsInt() const; |
| 145 | private: |
| 146 | double fStartNanos; |
| 147 | }; |
| 148 | |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 149 | } // namespace skiatest |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 150 | |
Adlai Holler | 684838f | 2020-05-12 10:41:04 -0400 | [diff] [blame] | 151 | static inline SkString reporter_string() { return {}; } |
| 152 | /// Prevent security warnings when using a non-literal string i.e. not a format string. |
| 153 | static inline SkString reporter_string(const char* s) { return SkString(s); } |
| 154 | template<typename... Args> |
| 155 | static inline SkString reporter_string(const char* fmt, Args... args) { |
| 156 | return SkStringPrintf(fmt, std::forward<Args>(args)...); |
| 157 | } |
| 158 | |
| 159 | #define REPORTER_ASSERT(r, cond, ...) \ |
| 160 | do { \ |
| 161 | if (!(cond)) { \ |
| 162 | REPORT_FAILURE(r, #cond, reporter_string(__VA_ARGS__)); \ |
| 163 | } \ |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 164 | } while (0) |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 165 | |
Adlai Holler | 684838f | 2020-05-12 10:41:04 -0400 | [diff] [blame] | 166 | #define ERRORF(r, ...) \ |
| 167 | do { \ |
| 168 | REPORT_FAILURE(r, "", reporter_string(__VA_ARGS__)); \ |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 169 | } while (0) |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 170 | |
halcanary | 7d57124 | 2016-02-24 17:59:16 -0800 | [diff] [blame] | 171 | #define INFOF(REPORTER, ...) \ |
| 172 | do { \ |
| 173 | if ((REPORTER)->verbose()) { \ |
| 174 | SkDebugf(__VA_ARGS__); \ |
| 175 | } \ |
| 176 | } while (0) |
| 177 | |
Brian Salomon | dcfca43 | 2017-11-15 15:48:03 -0500 | [diff] [blame] | 178 | #define DEF_TEST(name, reporter) \ |
| 179 | static void test_##name(skiatest::Reporter*, const GrContextOptions&); \ |
| 180 | skiatest::TestRegistry name##TestRegistry(skiatest::Test(#name, false, test_##name)); \ |
| 181 | void test_##name(skiatest::Reporter* reporter, const GrContextOptions&) |
commit-bot@chromium.org | e2eac8b | 2014-01-14 21:04:37 +0000 | [diff] [blame] | 182 | |
Brian Salomon | dcfca43 | 2017-11-15 15:48:03 -0500 | [diff] [blame] | 183 | #define DEF_GPUTEST(name, reporter, options) \ |
| 184 | static void test_##name(skiatest::Reporter*, const GrContextOptions&); \ |
| 185 | skiatest::TestRegistry name##TestRegistry(skiatest::Test(#name, true, test_##name)); \ |
| 186 | void test_##name(skiatest::Reporter* reporter, const GrContextOptions& options) |
commit-bot@chromium.org | e2eac8b | 2014-01-14 21:04:37 +0000 | [diff] [blame] | 187 | |
Robert Phillips | ec32534 | 2017-10-30 18:02:48 +0000 | [diff] [blame] | 188 | #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] | 189 | 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] | 190 | static void test_gpu_contexts_##name(skiatest::Reporter* reporter, \ |
Brian Salomon | dcfca43 | 2017-11-15 15:48:03 -0500 | [diff] [blame] | 191 | const GrContextOptions& options) { \ |
| 192 | skiatest::RunWithGPUTestContexts(test_##name, context_filter, reporter, options); \ |
Robert Phillips | ec32534 | 2017-10-30 18:02:48 +0000 | [diff] [blame] | 193 | } \ |
| 194 | skiatest::TestRegistry name##TestRegistry( \ |
| 195 | skiatest::Test(#name, true, test_gpu_contexts_##name, options_filter)); \ |
Brian Salomon | dcfca43 | 2017-11-15 15:48:03 -0500 | [diff] [blame] | 196 | void test_##name(skiatest::Reporter* reporter, const sk_gpu_test::ContextInfo& context_info) |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 197 | |
bsalomon | fda8807 | 2016-04-11 14:40:50 -0700 | [diff] [blame] | 198 | #define DEF_GPUTEST_FOR_ALL_CONTEXTS(name, reporter, context_info) \ |
Robert Phillips | ec32534 | 2017-10-30 18:02:48 +0000 | [diff] [blame] | 199 | DEF_GPUTEST_FOR_CONTEXTS(name, nullptr, reporter, context_info, nullptr) |
| 200 | |
bsalomon | 68d9134 | 2016-04-12 09:59:58 -0700 | [diff] [blame] | 201 | #define DEF_GPUTEST_FOR_RENDERING_CONTEXTS(name, reporter, context_info) \ |
| 202 | DEF_GPUTEST_FOR_CONTEXTS(name, sk_gpu_test::GrContextFactory::IsRenderingContext, \ |
Robert Phillips | ec32534 | 2017-10-30 18:02:48 +0000 | [diff] [blame] | 203 | reporter, context_info, nullptr) |
bsalomon | 758586c | 2016-04-06 14:02:39 -0700 | [diff] [blame] | 204 | #define DEF_GPUTEST_FOR_ALL_GL_CONTEXTS(name, reporter, context_info) \ |
Robert Phillips | ec32534 | 2017-10-30 18:02:48 +0000 | [diff] [blame] | 205 | DEF_GPUTEST_FOR_CONTEXTS(name, &skiatest::IsGLContextType, \ |
| 206 | reporter, context_info, nullptr) |
bsalomon | 758586c | 2016-04-06 14:02:39 -0700 | [diff] [blame] | 207 | #define DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(name, reporter, context_info) \ |
Robert Phillips | ec32534 | 2017-10-30 18:02:48 +0000 | [diff] [blame] | 208 | DEF_GPUTEST_FOR_CONTEXTS(name, &skiatest::IsRenderingGLContextType, \ |
| 209 | reporter, context_info, nullptr) |
Brian Osman | 7c59774 | 2019-03-26 11:10:11 -0400 | [diff] [blame] | 210 | #define DEF_GPUTEST_FOR_MOCK_CONTEXT(name, reporter, context_info) \ |
| 211 | DEF_GPUTEST_FOR_CONTEXTS(name, &skiatest::IsMockContextType, \ |
| 212 | reporter, context_info, nullptr) |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 213 | #define DEF_GPUTEST_FOR_VULKAN_CONTEXT(name, reporter, context_info) \ |
Robert Phillips | ec32534 | 2017-10-30 18:02:48 +0000 | [diff] [blame] | 214 | DEF_GPUTEST_FOR_CONTEXTS(name, &skiatest::IsVulkanContextType, \ |
| 215 | reporter, context_info, nullptr) |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 216 | #define DEF_GPUTEST_FOR_METAL_CONTEXT(name, reporter, context_info) \ |
| 217 | DEF_GPUTEST_FOR_CONTEXTS(name, &skiatest::IsMetalContextType, \ |
| 218 | reporter, context_info, nullptr) |
Greg Daniel | d4928d0 | 2020-06-19 11:13:26 -0400 | [diff] [blame] | 219 | #define DEF_GPUTEST_FOR_D3D_CONTEXT(name, reporter, context_info) \ |
| 220 | DEF_GPUTEST_FOR_CONTEXTS(name, &skiatest::IsDirect3DContextType, \ |
| 221 | reporter, context_info, nullptr) |
Greg Daniel | a58db7f | 2020-07-15 09:17:59 -0400 | [diff] [blame] | 222 | #define DEF_GPUTEST_FOR_DAWN_CONTEXT(name, reporter, context_info) \ |
| 223 | DEF_GPUTEST_FOR_CONTEXTS(name, &skiatest::IsDawnContextType, \ |
| 224 | reporter, context_info, nullptr) |
kkinnunen | 179a8f5 | 2015-11-20 13:32:24 -0800 | [diff] [blame] | 225 | |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 226 | #define REQUIRE_PDF_DOCUMENT(TEST_NAME, REPORTER) \ |
| 227 | do { \ |
Hal Canary | 3026d4b | 2019-01-07 10:00:48 -0500 | [diff] [blame] | 228 | SkNullWStream testStream; \ |
| 229 | auto testDoc = SkPDF::MakeDocument(&testStream); \ |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 230 | if (!testDoc) { \ |
| 231 | INFOF(REPORTER, "PDF disabled; %s test skipped.", #TEST_NAME); \ |
| 232 | return; \ |
| 233 | } \ |
halcanary | 2ccdb63 | 2015-08-11 13:35:12 -0700 | [diff] [blame] | 234 | } while (false) |
| 235 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 236 | #endif |