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