blob: 117b015fb6f3d6c63a73446a57e998bdca309b9f [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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.comed673312009-02-27 16:24:51 +00007#ifndef skiatest_Test_DEFINED
8#define skiatest_Test_DEFINED
9
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#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.coma8e686e2011-08-16 15:45:58 +000016
reed@android.comed673312009-02-27 16:24:51 +000017namespace skiatest {
reed@android.com80e39a72009-04-02 16:59:40 +000018
halcanary87f3ba42015-01-20 09:30:20 -080019SkString GetTmpDir();
reed@android.comed673312009-02-27 16:24:51 +000020
halcanary87f3ba42015-01-20 09:30:20 -080021struct 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};
scroggo0ee26272014-11-07 06:07:32 -080030
halcanary87f3ba42015-01-20 09:30:20 -080031class Reporter : SkNoncopyable {
32public:
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 Clarkab87d7a2016-10-04 10:01:04 -040038 virtual void* stats() const { return nullptr; }
Brian Osman287f6512016-12-05 11:35:07 -050039
Mike Klein4d907da2019-02-07 09:54:30 -050040 void reportFailedWithContext(const skiatest::Failure&);
41
Brian Osman287f6512016-12-05 11:35:07 -050042 void push(const SkString& message) {
43 fContextStack.push_back(message);
44 }
45 void pop() {
46 fContextStack.pop_back();
47 }
48
49private:
50 SkTArray<SkString> fContextStack;
halcanary87f3ba42015-01-20 09:30:20 -080051};
scroggo0ee26272014-11-07 06:07:32 -080052
halcanary87f3ba42015-01-20 09:30:20 -080053#define REPORT_FAILURE(reporter, cond, message) \
Brian Osman287f6512016-12-05 11:35:07 -050054 reporter->reportFailedWithContext(skiatest::Failure(__FILE__, __LINE__, cond, message))
55
56class ReporterContext : SkNoncopyable {
57public:
58 ReporterContext(Reporter* reporter, const SkString& message) : fReporter(reporter) {
59 fReporter->push(message);
60 }
61 ~ReporterContext() {
62 fReporter->pop();
63 }
64
65private:
66 Reporter* fReporter;
67};
scroggo0ee26272014-11-07 06:07:32 -080068
Brian Salomondcfca432017-11-15 15:48:03 -050069typedef void (*TestProc)(skiatest::Reporter*, const GrContextOptions&);
Robert Phillipsec325342017-10-30 18:02:48 +000070typedef void (*ContextOptionsProc)(GrContextOptions*);
reed@android.comed673312009-02-27 16:24:51 +000071
halcanary87f3ba42015-01-20 09:30:20 -080072struct Test {
Robert Phillipsec325342017-10-30 18:02:48 +000073 Test(const char* n, bool g, TestProc p, ContextOptionsProc optionsProc = nullptr)
74 : name(n), needsGpu(g), proc(p), fContextOptionsProc(optionsProc) {}
halcanary87f3ba42015-01-20 09:30:20 -080075 const char* name;
76 bool needsGpu;
77 TestProc proc;
Robert Phillipsec325342017-10-30 18:02:48 +000078 ContextOptionsProc fContextOptionsProc;
79
80 void modifyGrContextOptions(GrContextOptions* options) {
81 if (fContextOptionsProc) {
82 (*fContextOptionsProc)(options);
83 }
84 }
Mike Kleinb323a5e2017-07-24 15:21:31 -040085
Brian Salomondcfca432017-11-15 15:48:03 -050086 void run(skiatest::Reporter* r, const GrContextOptions& options) const {
Mike Kleinb323a5e2017-07-24 15:21:31 -040087 TRACE_EVENT1("test", TRACE_FUNC, "name", this->name/*these are static*/);
Brian Salomondcfca432017-11-15 15:48:03 -050088 this->proc(r, options);
Mike Kleinb323a5e2017-07-24 15:21:31 -040089 }
halcanary87f3ba42015-01-20 09:30:20 -080090};
reed@android.comed673312009-02-27 16:24:51 +000091
Mike Reedab273fa2017-01-11 13:58:55 -050092typedef sk_tools::Registry<Test> TestRegistry;
commit-bot@chromium.orge2eac8b2014-01-14 21:04:37 +000093
94/*
95 Use the following macros to make use of the skiatest classes, e.g.
96
Mike Kleinc0bd9f92019-04-23 12:05:21 -050097 #include "tests/Test.h"
commit-bot@chromium.orge2eac8b2014-01-14 21:04:37 +000098
99 DEF_TEST(TestName, reporter) {
100 ...
101 REPORTER_ASSERT(reporter, x == 15);
102 ...
Brian Salomon1c80e992018-01-29 09:50:47 -0500103 REPORTER_ASSERT(reporter, x == 15, "x should be 15");
commit-bot@chromium.orge2eac8b2014-01-14 21:04:37 +0000104 ...
105 if (x != 15) {
106 ERRORF(reporter, "x should be 15, but is %d", x);
107 return;
108 }
109 ...
110 }
111*/
bsalomon758586c2016-04-06 14:02:39 -0700112
bsalomon758586c2016-04-06 14:02:39 -0700113using GrContextFactoryContextType = sk_gpu_test::GrContextFactory::ContextType;
bsalomonf2f1c172016-04-05 12:59:06 -0700114
115typedef void GrContextTestFn(Reporter*, const sk_gpu_test::ContextInfo&);
bsalomon758586c2016-04-06 14:02:39 -0700116typedef bool GrContextTypeFilterFn(GrContextFactoryContextType);
bsalomonf2f1c172016-04-05 12:59:06 -0700117
bsalomon758586c2016-04-06 14:02:39 -0700118extern bool IsGLContextType(GrContextFactoryContextType);
bsalomondc0fcd42016-04-11 14:21:33 -0700119extern bool IsVulkanContextType(GrContextFactoryContextType);
Timothy Liang760dbc42018-07-17 13:28:20 -0400120extern bool IsMetalContextType(GrContextFactoryContextType);
Greg Daniela58db7f2020-07-15 09:17:59 -0400121extern bool IsDawnContextType(GrContextFactoryContextType);
Greg Danield4928d02020-06-19 11:13:26 -0400122extern bool IsDirect3DContextType(GrContextFactoryContextType);
bsalomon758586c2016-04-06 14:02:39 -0700123extern bool IsRenderingGLContextType(GrContextFactoryContextType);
Jim Van Verth5a8f3e42019-10-24 10:23:26 -0400124extern bool IsRenderingGLOrMetalContextType(GrContextFactoryContextType);
Brian Osman7c597742019-03-26 11:10:11 -0400125extern bool IsMockContextType(GrContextFactoryContextType);
Brian Salomondcfca432017-11-15 15:48:03 -0500126void RunWithGPUTestContexts(GrContextTestFn*, GrContextTypeFilterFn*, Reporter*,
127 const GrContextOptions&);
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700128
129/** Timer provides wall-clock duration since its creation. */
130class Timer {
131public:
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;
145private:
146 double fStartNanos;
147};
148
halcanary87f3ba42015-01-20 09:30:20 -0800149} // namespace skiatest
reed@android.comed673312009-02-27 16:24:51 +0000150
Adlai Holler684838f2020-05-12 10:41:04 -0400151static inline SkString reporter_string() { return {}; }
152/// Prevent security warnings when using a non-literal string i.e. not a format string.
153static inline SkString reporter_string(const char* s) { return SkString(s); }
154template<typename... Args>
155static 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 } \
halcanary87f3ba42015-01-20 09:30:20 -0800164 } while (0)
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000165
Adlai Holler684838f2020-05-12 10:41:04 -0400166#define ERRORF(r, ...) \
167 do { \
168 REPORT_FAILURE(r, "", reporter_string(__VA_ARGS__)); \
halcanary87f3ba42015-01-20 09:30:20 -0800169 } while (0)
reed@android.comed673312009-02-27 16:24:51 +0000170
halcanary7d571242016-02-24 17:59:16 -0800171#define INFOF(REPORTER, ...) \
172 do { \
173 if ((REPORTER)->verbose()) { \
174 SkDebugf(__VA_ARGS__); \
175 } \
176 } while (0)
177
Brian Salomondcfca432017-11-15 15:48:03 -0500178#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.orge2eac8b2014-01-14 21:04:37 +0000182
Brian Salomondcfca432017-11-15 15:48:03 -0500183#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.orge2eac8b2014-01-14 21:04:37 +0000187
Robert Phillipsec325342017-10-30 18:02:48 +0000188#define DEF_GPUTEST_FOR_CONTEXTS(name, context_filter, reporter, context_info, options_filter) \
Brian Salomondcfca432017-11-15 15:48:03 -0500189 static void test_##name(skiatest::Reporter*, const sk_gpu_test::ContextInfo& context_info); \
Robert Phillipsec325342017-10-30 18:02:48 +0000190 static void test_gpu_contexts_##name(skiatest::Reporter* reporter, \
Brian Salomondcfca432017-11-15 15:48:03 -0500191 const GrContextOptions& options) { \
192 skiatest::RunWithGPUTestContexts(test_##name, context_filter, reporter, options); \
Robert Phillipsec325342017-10-30 18:02:48 +0000193 } \
194 skiatest::TestRegistry name##TestRegistry( \
195 skiatest::Test(#name, true, test_gpu_contexts_##name, options_filter)); \
Brian Salomondcfca432017-11-15 15:48:03 -0500196 void test_##name(skiatest::Reporter* reporter, const sk_gpu_test::ContextInfo& context_info)
kkinnunen179a8f52015-11-20 13:32:24 -0800197
bsalomonfda88072016-04-11 14:40:50 -0700198#define DEF_GPUTEST_FOR_ALL_CONTEXTS(name, reporter, context_info) \
Robert Phillipsec325342017-10-30 18:02:48 +0000199 DEF_GPUTEST_FOR_CONTEXTS(name, nullptr, reporter, context_info, nullptr)
200
bsalomon68d91342016-04-12 09:59:58 -0700201#define DEF_GPUTEST_FOR_RENDERING_CONTEXTS(name, reporter, context_info) \
202 DEF_GPUTEST_FOR_CONTEXTS(name, sk_gpu_test::GrContextFactory::IsRenderingContext, \
Robert Phillipsec325342017-10-30 18:02:48 +0000203 reporter, context_info, nullptr)
bsalomon758586c2016-04-06 14:02:39 -0700204#define DEF_GPUTEST_FOR_ALL_GL_CONTEXTS(name, reporter, context_info) \
Robert Phillipsec325342017-10-30 18:02:48 +0000205 DEF_GPUTEST_FOR_CONTEXTS(name, &skiatest::IsGLContextType, \
206 reporter, context_info, nullptr)
bsalomon758586c2016-04-06 14:02:39 -0700207#define DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(name, reporter, context_info) \
Robert Phillipsec325342017-10-30 18:02:48 +0000208 DEF_GPUTEST_FOR_CONTEXTS(name, &skiatest::IsRenderingGLContextType, \
209 reporter, context_info, nullptr)
Brian Osman7c597742019-03-26 11:10:11 -0400210#define DEF_GPUTEST_FOR_MOCK_CONTEXT(name, reporter, context_info) \
211 DEF_GPUTEST_FOR_CONTEXTS(name, &skiatest::IsMockContextType, \
212 reporter, context_info, nullptr)
bsalomondc0fcd42016-04-11 14:21:33 -0700213#define DEF_GPUTEST_FOR_VULKAN_CONTEXT(name, reporter, context_info) \
Robert Phillipsec325342017-10-30 18:02:48 +0000214 DEF_GPUTEST_FOR_CONTEXTS(name, &skiatest::IsVulkanContextType, \
215 reporter, context_info, nullptr)
Timothy Liang760dbc42018-07-17 13:28:20 -0400216#define DEF_GPUTEST_FOR_METAL_CONTEXT(name, reporter, context_info) \
217 DEF_GPUTEST_FOR_CONTEXTS(name, &skiatest::IsMetalContextType, \
218 reporter, context_info, nullptr)
Greg Danield4928d02020-06-19 11:13:26 -0400219#define DEF_GPUTEST_FOR_D3D_CONTEXT(name, reporter, context_info) \
220 DEF_GPUTEST_FOR_CONTEXTS(name, &skiatest::IsDirect3DContextType, \
221 reporter, context_info, nullptr)
Greg Daniela58db7f2020-07-15 09:17:59 -0400222#define DEF_GPUTEST_FOR_DAWN_CONTEXT(name, reporter, context_info) \
223 DEF_GPUTEST_FOR_CONTEXTS(name, &skiatest::IsDawnContextType, \
224 reporter, context_info, nullptr)
kkinnunen179a8f52015-11-20 13:32:24 -0800225
halcanary4b656662016-04-27 07:45:18 -0700226#define REQUIRE_PDF_DOCUMENT(TEST_NAME, REPORTER) \
227 do { \
Hal Canary3026d4b2019-01-07 10:00:48 -0500228 SkNullWStream testStream; \
229 auto testDoc = SkPDF::MakeDocument(&testStream); \
halcanary4b656662016-04-27 07:45:18 -0700230 if (!testDoc) { \
231 INFOF(REPORTER, "PDF disabled; %s test skipped.", #TEST_NAME); \
232 return; \
233 } \
halcanary2ccdb632015-08-11 13:35:12 -0700234 } while (false)
235
reed@android.comed673312009-02-27 16:24:51 +0000236#endif