joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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 | */ |
| 7 | |
| 8 | #ifndef GrProcessorUnitTest_DEFINED |
| 9 | #define GrProcessorUnitTest_DEFINED |
| 10 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkTypes.h" |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 12 | |
| 13 | #if GR_TEST_UTILS |
| 14 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "include/private/SkTArray.h" |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 16 | #include "src/core/SkArenaAlloc.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "src/gpu/GrTestUtils.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 18 | #include "src/gpu/GrTextureProxy.h" |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 19 | |
Brian Salomon | 766098d | 2019-12-18 10:41:58 -0500 | [diff] [blame] | 20 | #include <tuple> |
| 21 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 22 | class SkMatrix; |
bsalomon | 4b91f76 | 2015-05-19 09:29:46 -0700 | [diff] [blame] | 23 | class GrCaps; |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 24 | class GrContext; |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 25 | class GrProxyProvider; |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 26 | class GrRenderTargetContext; |
Brian Salomon | 766098d | 2019-12-18 10:41:58 -0500 | [diff] [blame] | 27 | class GrProcessorTestData; |
Mike Reed | d470673 | 2016-11-15 16:44:34 -0500 | [diff] [blame] | 28 | class GrTexture; |
Brian Salomon | a163392 | 2017-01-09 11:46:10 -0500 | [diff] [blame] | 29 | class GrXPFactory; |
Brian Salomon | 1c05364 | 2017-07-24 10:16:19 -0400 | [diff] [blame] | 30 | class GrGeometryProcessor; |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 31 | |
| 32 | namespace GrProcessorUnitTest { |
bsalomon | 506c802 | 2015-09-14 13:16:26 -0700 | [diff] [blame] | 33 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 34 | // Used to access the dummy textures in TestCreate procs. |
| 35 | enum { |
| 36 | kSkiaPMTextureIdx = 0, |
| 37 | kAlphaTextureIdx = 1, |
| 38 | }; |
| 39 | |
bsalomon | 506c802 | 2015-09-14 13:16:26 -0700 | [diff] [blame] | 40 | /** This allows parent FPs to implement a test create with known leaf children in order to avoid |
| 41 | creating an unbounded FP tree which may overflow various shader limits. */ |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 42 | std::unique_ptr<GrFragmentProcessor> MakeChildFP(GrProcessorTestData*); |
bsalomon | 506c802 | 2015-09-14 13:16:26 -0700 | [diff] [blame] | 43 | |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 44 | } |
| 45 | |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 46 | /* |
| 47 | * GrProcessorTestData is an argument struct to TestCreate functions |
| 48 | * fTextures are valid textures that can optionally be used to construct |
Greg Daniel | b58a3c7 | 2020-01-23 10:05:14 -0500 | [diff] [blame] | 49 | * TextureSampler. The first texture has a RGBA8 format and the second has Alpha8 format for the |
| 50 | * specific backend API. TestCreate functions are also free to create additional textures using |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 51 | * the GrContext. |
| 52 | */ |
Brian Salomon | 766098d | 2019-12-18 10:41:58 -0500 | [diff] [blame] | 53 | class GrProcessorTestData { |
| 54 | public: |
Greg Daniel | 026a60c | 2020-02-12 10:53:51 -0500 | [diff] [blame] | 55 | using ViewInfo = std::tuple<GrSurfaceProxyView, GrColorType, SkAlphaType>; |
| 56 | GrProcessorTestData(SkRandom* random, GrContext* context, int numProxies, const ViewInfo[]); |
Robert Phillips | 757914d | 2017-01-25 15:48:30 -0500 | [diff] [blame] | 57 | |
| 58 | GrContext* context() { return fContext; } |
Robert Phillips | 296b1cc | 2017-03-15 10:42:12 -0400 | [diff] [blame] | 59 | GrResourceProvider* resourceProvider(); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 60 | GrProxyProvider* proxyProvider(); |
Robert Phillips | 296b1cc | 2017-03-15 10:42:12 -0400 | [diff] [blame] | 61 | const GrCaps* caps(); |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 62 | SkArenaAlloc* allocator() { return fArena.get(); } |
Robert Phillips | 757914d | 2017-01-25 15:48:30 -0500 | [diff] [blame] | 63 | |
Greg Daniel | 026a60c | 2020-02-12 10:53:51 -0500 | [diff] [blame] | 64 | ViewInfo randomView(); |
| 65 | ViewInfo randomAlphaOnlyView(); |
Brian Salomon | 766098d | 2019-12-18 10:41:58 -0500 | [diff] [blame] | 66 | |
| 67 | SkRandom* fRandom; |
| 68 | |
Robert Phillips | 757914d | 2017-01-25 15:48:30 -0500 | [diff] [blame] | 69 | private: |
Robert Phillips | dbc8eeb | 2017-02-21 10:04:31 -0500 | [diff] [blame] | 70 | GrContext* fContext; |
Greg Daniel | 026a60c | 2020-02-12 10:53:51 -0500 | [diff] [blame] | 71 | SkTArray<ViewInfo> fViews; |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 72 | std::unique_ptr<SkArenaAlloc> fArena; |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 73 | }; |
| 74 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 75 | class GrProcessor; |
| 76 | class GrTexture; |
| 77 | |
Brian Salomon | 1c05364 | 2017-07-24 10:16:19 -0400 | [diff] [blame] | 78 | template <class ProcessorSmartPtr> |
| 79 | class GrProcessorTestFactory : private SkNoncopyable { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 80 | public: |
Brian Salomon | 1c05364 | 2017-07-24 10:16:19 -0400 | [diff] [blame] | 81 | using MakeProc = ProcessorSmartPtr (*)(GrProcessorTestData*); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 82 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 83 | GrProcessorTestFactory(MakeProc makeProc) { |
| 84 | fMakeProc = makeProc; |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 85 | GetFactories()->push_back(this); |
| 86 | } |
| 87 | |
bsalomon | b5b6032 | 2015-09-14 12:26:33 -0700 | [diff] [blame] | 88 | /** Pick a random factory function and create a processor. */ |
Brian Salomon | 1c05364 | 2017-07-24 10:16:19 -0400 | [diff] [blame] | 89 | static ProcessorSmartPtr Make(GrProcessorTestData* data) { |
joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 90 | VerifyFactoryCount(); |
Ben Wagner | f1344ac | 2019-05-10 15:01:53 -0400 | [diff] [blame] | 91 | if (GetFactories()->count() == 0) { |
| 92 | return nullptr; |
| 93 | } |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 94 | uint32_t idx = data->fRandom->nextRangeU(0, GetFactories()->count() - 1); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 95 | return MakeIdx(idx, data); |
bsalomon | b5b6032 | 2015-09-14 12:26:33 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | /** Number of registered factory functions */ |
| 99 | static int Count() { return GetFactories()->count(); } |
| 100 | |
| 101 | /** Use factory function at Index idx to create a processor. */ |
Brian Salomon | 94e0b11 | 2017-07-28 16:02:46 -0400 | [diff] [blame] | 102 | static ProcessorSmartPtr MakeIdx(int idx, GrProcessorTestData* data) { |
Ben Wagner | f1344ac | 2019-05-10 15:01:53 -0400 | [diff] [blame] | 103 | SkASSERT(idx < GetFactories()->count()); |
Brian Salomon | 1c05364 | 2017-07-24 10:16:19 -0400 | [diff] [blame] | 104 | GrProcessorTestFactory<ProcessorSmartPtr>* factory = (*GetFactories())[idx]; |
| 105 | ProcessorSmartPtr processor = factory->fMakeProc(data); |
Brian Salomon | a163392 | 2017-01-09 11:46:10 -0500 | [diff] [blame] | 106 | SkASSERT(processor); |
| 107 | return processor; |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Brian Salomon | a163392 | 2017-01-09 11:46:10 -0500 | [diff] [blame] | 110 | private: |
| 111 | /** |
joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 112 | * A test function which verifies the count of factories. |
| 113 | */ |
| 114 | static void VerifyFactoryCount(); |
| 115 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 116 | MakeProc fMakeProc; |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 117 | |
Brian Salomon | 1c05364 | 2017-07-24 10:16:19 -0400 | [diff] [blame] | 118 | static SkTArray<GrProcessorTestFactory<ProcessorSmartPtr>*, true>* GetFactories(); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 119 | }; |
| 120 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 121 | using GrFragmentProcessorTestFactory = GrProcessorTestFactory<std::unique_ptr<GrFragmentProcessor>>; |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 122 | using GrGeometryProcessorTestFactory = GrProcessorTestFactory<GrGeometryProcessor*>; |
Brian Salomon | 1c05364 | 2017-07-24 10:16:19 -0400 | [diff] [blame] | 123 | |
Brian Salomon | a163392 | 2017-01-09 11:46:10 -0500 | [diff] [blame] | 124 | class GrXPFactoryTestFactory : private SkNoncopyable { |
| 125 | public: |
| 126 | using GetFn = const GrXPFactory*(GrProcessorTestData*); |
| 127 | |
| 128 | GrXPFactoryTestFactory(GetFn* getProc) : fGetProc(getProc) { GetFactories()->push_back(this); } |
| 129 | |
| 130 | static const GrXPFactory* Get(GrProcessorTestData* data) { |
| 131 | VerifyFactoryCount(); |
Ben Wagner | f1344ac | 2019-05-10 15:01:53 -0400 | [diff] [blame] | 132 | if (GetFactories()->count() == 0) { |
| 133 | return nullptr; |
| 134 | } |
Brian Salomon | a163392 | 2017-01-09 11:46:10 -0500 | [diff] [blame] | 135 | uint32_t idx = data->fRandom->nextRangeU(0, GetFactories()->count() - 1); |
| 136 | const GrXPFactory* xpf = (*GetFactories())[idx]->fGetProc(data); |
| 137 | SkASSERT(xpf); |
| 138 | return xpf; |
| 139 | } |
| 140 | |
| 141 | private: |
| 142 | static void VerifyFactoryCount(); |
| 143 | |
| 144 | GetFn* fGetProc; |
| 145 | static SkTArray<GrXPFactoryTestFactory*, true>* GetFactories(); |
| 146 | }; |
| 147 | |
Ben Wagner | f1344ac | 2019-05-10 15:01:53 -0400 | [diff] [blame] | 148 | #if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS |
| 149 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 150 | /** GrProcessor subclasses should insert this macro in their declaration to be included in the |
| 151 | * program generation unit test. |
| 152 | */ |
Brian Salomon | 1c05364 | 2017-07-24 10:16:19 -0400 | [diff] [blame] | 153 | #define GR_DECLARE_GEOMETRY_PROCESSOR_TEST \ |
| 154 | static GrGeometryProcessorTestFactory gTestFactory SK_UNUSED; \ |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 155 | static GrGeometryProcessor* TestCreate(GrProcessorTestData*); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 156 | |
Brian Salomon | 1c05364 | 2017-07-24 10:16:19 -0400 | [diff] [blame] | 157 | #define GR_DECLARE_FRAGMENT_PROCESSOR_TEST \ |
| 158 | static GrFragmentProcessorTestFactory gTestFactory SK_UNUSED; \ |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 159 | static std::unique_ptr<GrFragmentProcessor> TestCreate(GrProcessorTestData*); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 160 | |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 161 | #define GR_DECLARE_XP_FACTORY_TEST \ |
Brian Salomon | a163392 | 2017-01-09 11:46:10 -0500 | [diff] [blame] | 162 | static GrXPFactoryTestFactory gTestFactory SK_UNUSED; \ |
Brian Salomon | 0c26a9d | 2017-07-06 10:09:38 -0400 | [diff] [blame] | 163 | static const GrXPFactory* TestGet(GrProcessorTestData*); |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 164 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 165 | /** GrProcessor subclasses should insert this macro in their implementation file. They must then |
| 166 | * also implement this static function: |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 167 | * GrProcessor* TestCreate(GrProcessorTestData*); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 168 | */ |
Brian Salomon | 1c05364 | 2017-07-24 10:16:19 -0400 | [diff] [blame] | 169 | #define GR_DEFINE_FRAGMENT_PROCESSOR_TEST(Effect) \ |
| 170 | GrFragmentProcessorTestFactory Effect::gTestFactory(Effect::TestCreate) |
Brian Salomon | 003312a | 2017-01-09 16:00:33 +0000 | [diff] [blame] | 171 | |
Brian Salomon | 1c05364 | 2017-07-24 10:16:19 -0400 | [diff] [blame] | 172 | #define GR_DEFINE_GEOMETRY_PROCESSOR_TEST(Effect) \ |
| 173 | GrGeometryProcessorTestFactory Effect::gTestFactory(Effect::TestCreate) |
Brian Salomon | a163392 | 2017-01-09 11:46:10 -0500 | [diff] [blame] | 174 | |
| 175 | #define GR_DEFINE_XP_FACTORY_TEST(Factory) \ |
| 176 | GrXPFactoryTestFactory Factory::gTestFactory(Factory::TestGet) |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 177 | |
| 178 | #else // !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS |
| 179 | |
| 180 | // The unit test relies on static initializers. Just declare the TestCreate function so that |
| 181 | // its definitions will compile. |
| 182 | #define GR_DECLARE_FRAGMENT_PROCESSOR_TEST \ |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 183 | static std::unique_ptr<GrFragmentProcessor> TestCreate(GrProcessorTestData*); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 184 | #define GR_DEFINE_FRAGMENT_PROCESSOR_TEST(X) |
| 185 | |
| 186 | // The unit test relies on static initializers. Just declare the TestCreate function so that |
| 187 | // its definitions will compile. |
| 188 | #define GR_DECLARE_GEOMETRY_PROCESSOR_TEST \ |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 189 | static GrGeometryProcessor* TestCreate(GrProcessorTestData*); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 190 | #define GR_DEFINE_GEOMETRY_PROCESSOR_TEST(X) |
| 191 | |
Brian Salomon | a163392 | 2017-01-09 11:46:10 -0500 | [diff] [blame] | 192 | // The unit test relies on static initializers. Just declare the TestGet function so that |
| 193 | // its definitions will compile. |
| 194 | #define GR_DECLARE_XP_FACTORY_TEST \ |
Brian Salomon | 0c26a9d | 2017-07-06 10:09:38 -0400 | [diff] [blame] | 195 | const GrXPFactory* TestGet(GrProcessorTestData*); |
Brian Salomon | a163392 | 2017-01-09 11:46:10 -0500 | [diff] [blame] | 196 | #define GR_DEFINE_XP_FACTORY_TEST(X) |
| 197 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 198 | #endif // !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS |
| 199 | #else // GR_TEST_UTILS |
| 200 | #define GR_DECLARE_GEOMETRY_PROCESSOR_TEST |
| 201 | #define GR_DECLARE_FRAGMENT_PROCESSOR_TEST |
| 202 | #define GR_DECLARE_XP_FACTORY_TEST |
| 203 | #define GR_DEFINE_FRAGMENT_PROCESSOR_TEST(...) |
| 204 | #define GR_DEFINE_GEOMETRY_PROCESSOR_TEST(...) |
| 205 | #define GR_DEFINE_XP_FACTORY_TEST(...) |
| 206 | #define GR_DECLARE_FRAGMENT_PROCESSOR_TEST |
| 207 | #define GR_DEFINE_FRAGMENT_PROCESSOR_TEST(...) |
| 208 | #define GR_DECLARE_GEOMETRY_PROCESSOR_TEST |
| 209 | #define GR_DEFINE_GEOMETRY_PROCESSOR_TEST(...) |
| 210 | #define GR_DECLARE_XP_FACTORY_TEST |
| 211 | #define GR_DEFINE_XP_FACTORY_TEST(...) |
| 212 | #endif // GR_TEST_UTILS |
| 213 | #endif // GrProcessorUnitTest_DEFINED |