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