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