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