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