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