bsalomon | 506c802 | 2015-09-14 13:16:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/GrProcessorUnitTest.h" |
bsalomon | 506c802 | 2015-09-14 13:16:26 -0700 | [diff] [blame] | 9 | |
John Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame] | 10 | #include <memory> |
| 11 | |
Robert Phillips | 4e105e2 | 2020-07-16 09:18:50 -0400 | [diff] [blame] | 12 | #include "include/gpu/GrRecordingContext.h" |
Brian Salomon | 766098d | 2019-12-18 10:41:58 -0500 | [diff] [blame] | 13 | #include "src/gpu/GrFragmentProcessor.h" |
Robert Phillips | 4e105e2 | 2020-07-16 09:18:50 -0400 | [diff] [blame] | 14 | #include "src/gpu/GrRecordingContextPriv.h" |
Brian Salomon | 766098d | 2019-12-18 10:41:58 -0500 | [diff] [blame] | 15 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 16 | #if GR_TEST_UTILS |
| 17 | |
John Stiles | eac4ad7 | 2020-07-17 15:46:20 -0400 | [diff] [blame] | 18 | class GrGeometryProcessor; |
| 19 | |
John Stiles | 6609cb6 | 2020-07-17 14:52:12 -0400 | [diff] [blame] | 20 | GrProcessorTestData::GrProcessorTestData(SkRandom* random, GrRecordingContext* context, |
John Stiles | 87d0a2f | 2020-08-10 13:12:41 -0400 | [diff] [blame] | 21 | int maxTreeDepth, int numViews, const ViewInfo views[]) |
| 22 | : GrProcessorTestData(random, context, maxTreeDepth, numViews, views, |
| 23 | /*inputFP=*/nullptr) {} |
John Stiles | 278b4a6 | 2020-07-17 16:44:49 -0400 | [diff] [blame] | 24 | |
| 25 | GrProcessorTestData::GrProcessorTestData(SkRandom* random, GrRecordingContext* context, |
John Stiles | 87d0a2f | 2020-08-10 13:12:41 -0400 | [diff] [blame] | 26 | int maxTreeDepth, int numViews, const ViewInfo views[], |
John Stiles | 6609cb6 | 2020-07-17 14:52:12 -0400 | [diff] [blame] | 27 | std::unique_ptr<GrFragmentProcessor> inputFP) |
John Stiles | 87d0a2f | 2020-08-10 13:12:41 -0400 | [diff] [blame] | 28 | : fRandom(random) |
| 29 | , fMaxTreeDepth(maxTreeDepth) |
| 30 | , fContext(context) |
| 31 | , fInputFP(std::move(inputFP)) { |
Greg Daniel | 026a60c | 2020-02-12 10:53:51 -0500 | [diff] [blame] | 32 | fViews.reset(views, numViews); |
John Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame] | 33 | fArena = std::make_unique<SkArenaAlloc>(1000); |
Brian Salomon | 766098d | 2019-12-18 10:41:58 -0500 | [diff] [blame] | 34 | } |
| 35 | |
John Stiles | e911ce5 | 2020-07-17 13:32:27 -0400 | [diff] [blame] | 36 | GrProcessorTestData::~GrProcessorTestData() {} |
Brian Salomon | 766098d | 2019-12-18 10:41:58 -0500 | [diff] [blame] | 37 | |
| 38 | GrProxyProvider* GrProcessorTestData::proxyProvider() { return fContext->priv().proxyProvider(); } |
| 39 | |
| 40 | const GrCaps* GrProcessorTestData::caps() { return fContext->priv().caps(); } |
| 41 | |
John Stiles | 87d0a2f | 2020-08-10 13:12:41 -0400 | [diff] [blame] | 42 | std::unique_ptr<GrFragmentProcessor> GrProcessorTestData::inputFP() { |
| 43 | if (fCurrentTreeDepth == 0) { |
| 44 | // At the top level of the tree, provide the input FP from the test data. |
| 45 | return fInputFP ? fInputFP->clone() : nullptr; |
| 46 | } else { |
| 47 | // At deeper levels of recursion, synthesize a random input. |
| 48 | return GrProcessorUnitTest::MakeChildFP(this); |
| 49 | } |
| 50 | } |
John Stiles | 6609cb6 | 2020-07-17 14:52:12 -0400 | [diff] [blame] | 51 | |
Greg Daniel | 026a60c | 2020-02-12 10:53:51 -0500 | [diff] [blame] | 52 | GrProcessorTestData::ViewInfo GrProcessorTestData::randomView() { |
| 53 | SkASSERT(!fViews.empty()); |
| 54 | return fViews[fRandom->nextULessThan(fViews.count())]; |
Brian Salomon | 766098d | 2019-12-18 10:41:58 -0500 | [diff] [blame] | 55 | } |
| 56 | |
Greg Daniel | 026a60c | 2020-02-12 10:53:51 -0500 | [diff] [blame] | 57 | GrProcessorTestData::ViewInfo GrProcessorTestData::randomAlphaOnlyView() { |
Brian Salomon | 766098d | 2019-12-18 10:41:58 -0500 | [diff] [blame] | 58 | int numAlphaOnly = 0; |
Greg Daniel | 026a60c | 2020-02-12 10:53:51 -0500 | [diff] [blame] | 59 | for (const auto& [v, ct, at] : fViews) { |
Brian Salomon | 766098d | 2019-12-18 10:41:58 -0500 | [diff] [blame] | 60 | if (GrColorTypeIsAlphaOnly(ct)) { |
| 61 | ++numAlphaOnly; |
| 62 | } |
| 63 | } |
| 64 | SkASSERT(numAlphaOnly); |
| 65 | int idx = fRandom->nextULessThan(numAlphaOnly); |
Greg Daniel | 026a60c | 2020-02-12 10:53:51 -0500 | [diff] [blame] | 66 | for (const auto& [v, ct, at] : fViews) { |
Brian Salomon | 766098d | 2019-12-18 10:41:58 -0500 | [diff] [blame] | 67 | if (GrColorTypeIsAlphaOnly(ct) && !idx--) { |
Greg Daniel | 026a60c | 2020-02-12 10:53:51 -0500 | [diff] [blame] | 68 | return {v, ct, at}; |
Brian Salomon | 766098d | 2019-12-18 10:41:58 -0500 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | SkUNREACHABLE; |
| 72 | } |
| 73 | |
John Stiles | eac4ad7 | 2020-07-17 15:46:20 -0400 | [diff] [blame] | 74 | template <class ProcessorSmartPtr> |
John Stiles | 7c7a7e5 | 2020-07-24 17:03:04 -0400 | [diff] [blame] | 75 | GrProcessorTestFactory<ProcessorSmartPtr>::GrProcessorTestFactory(MakeProc makeProc, |
| 76 | const char* name) |
| 77 | : fMakeProc(makeProc), fName(name) { |
John Stiles | eac4ad7 | 2020-07-17 15:46:20 -0400 | [diff] [blame] | 78 | GetFactories()->push_back(this); |
| 79 | } |
| 80 | |
| 81 | template <class ProcessorSmartPtr> |
| 82 | ProcessorSmartPtr GrProcessorTestFactory<ProcessorSmartPtr>::Make(GrProcessorTestData* data) { |
| 83 | VerifyFactoryCount(); |
| 84 | if (GetFactories()->count() == 0) { |
| 85 | return nullptr; |
| 86 | } |
| 87 | uint32_t idx = data->fRandom->nextULessThan(GetFactories()->count()); |
| 88 | return MakeIdx(idx, data); |
| 89 | } |
| 90 | |
| 91 | template <class ProcessorSmartPtr> |
| 92 | ProcessorSmartPtr GrProcessorTestFactory<ProcessorSmartPtr>::MakeIdx(int idx, |
| 93 | GrProcessorTestData* data) { |
| 94 | SkASSERT(idx < GetFactories()->count()); |
| 95 | GrProcessorTestFactory<ProcessorSmartPtr>* factory = (*GetFactories())[idx]; |
| 96 | ProcessorSmartPtr processor = factory->fMakeProc(data); |
John Stiles | 7c7a7e5 | 2020-07-24 17:03:04 -0400 | [diff] [blame] | 97 | if (processor == nullptr) { |
| 98 | SK_ABORT("%s: TestCreate returned null", factory->fName.c_str()); |
| 99 | } |
John Stiles | eac4ad7 | 2020-07-17 15:46:20 -0400 | [diff] [blame] | 100 | return processor; |
| 101 | } |
| 102 | |
| 103 | template <class ProcessorSmartPtr> |
| 104 | int GrProcessorTestFactory<ProcessorSmartPtr>::Count() { |
| 105 | return GetFactories()->count(); |
| 106 | } |
| 107 | |
| 108 | GrXPFactoryTestFactory::GrXPFactoryTestFactory(GetFn* getProc) : fGetProc(getProc) { |
| 109 | GetFactories()->push_back(this); |
| 110 | } |
| 111 | |
| 112 | const GrXPFactory* GrXPFactoryTestFactory::Get(GrProcessorTestData* data) { |
| 113 | VerifyFactoryCount(); |
| 114 | if (GetFactories()->count() == 0) { |
| 115 | return nullptr; |
| 116 | } |
| 117 | uint32_t idx = data->fRandom->nextRangeU(0, GetFactories()->count() - 1); |
| 118 | const GrXPFactory* xpf = (*GetFactories())[idx]->fGetProc(data); |
| 119 | SkASSERT(xpf); |
| 120 | return xpf; |
| 121 | } |
Brian Salomon | 766098d | 2019-12-18 10:41:58 -0500 | [diff] [blame] | 122 | |
| 123 | /* |
| 124 | * Originally these were both in the processor unit test header, but then it seemed to cause linker |
| 125 | * problems on android. |
| 126 | */ |
| 127 | template <> |
| 128 | SkTArray<GrFragmentProcessorTestFactory*, true>* GrFragmentProcessorTestFactory::GetFactories() { |
| 129 | static SkTArray<GrFragmentProcessorTestFactory*, true> gFactories; |
| 130 | return &gFactories; |
| 131 | } |
| 132 | |
| 133 | template <> |
| 134 | SkTArray<GrGeometryProcessorTestFactory*, true>* GrGeometryProcessorTestFactory::GetFactories() { |
| 135 | static SkTArray<GrGeometryProcessorTestFactory*, true> gFactories; |
| 136 | return &gFactories; |
| 137 | } |
| 138 | |
| 139 | SkTArray<GrXPFactoryTestFactory*, true>* GrXPFactoryTestFactory::GetFactories() { |
| 140 | static SkTArray<GrXPFactoryTestFactory*, true> gFactories; |
| 141 | return &gFactories; |
| 142 | } |
| 143 | |
| 144 | /* |
| 145 | * To ensure we always have successful static initialization, before creating from the factories |
| 146 | * we verify the count is as expected. If a new factory is added, then these numbers must be |
| 147 | * manually adjusted. |
| 148 | */ |
Brian Osman | 1c46777 | 2021-07-02 13:11:15 +0000 | [diff] [blame^] | 149 | static constexpr int kFPFactoryCount = 18; |
John Stiles | eac4ad7 | 2020-07-17 15:46:20 -0400 | [diff] [blame] | 150 | static constexpr int kGPFactoryCount = 14; |
| 151 | static constexpr int kXPFactoryCount = 4; |
Brian Salomon | 766098d | 2019-12-18 10:41:58 -0500 | [diff] [blame] | 152 | |
| 153 | template <> void GrFragmentProcessorTestFactory::VerifyFactoryCount() { |
| 154 | if (kFPFactoryCount != GetFactories()->count()) { |
| 155 | SkDebugf("\nExpected %d fragment processor factories, found %d.\n", kFPFactoryCount, |
| 156 | GetFactories()->count()); |
| 157 | SK_ABORT("Wrong number of fragment processor factories!"); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | template <> void GrGeometryProcessorTestFactory::VerifyFactoryCount() { |
| 162 | if (kGPFactoryCount != GetFactories()->count()) { |
| 163 | SkDebugf("\nExpected %d geometry processor factories, found %d.\n", kGPFactoryCount, |
| 164 | GetFactories()->count()); |
| 165 | SK_ABORT("Wrong number of geometry processor factories!"); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | void GrXPFactoryTestFactory::VerifyFactoryCount() { |
| 170 | if (kXPFactoryCount != GetFactories()->count()) { |
| 171 | SkDebugf("\nExpected %d xp factory factories, found %d.\n", kXPFactoryCount, |
| 172 | GetFactories()->count()); |
| 173 | SK_ABORT("Wrong number of xp factory factories!"); |
| 174 | } |
| 175 | } |
| 176 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 177 | std::unique_ptr<GrFragmentProcessor> GrProcessorUnitTest::MakeChildFP(GrProcessorTestData* data) { |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 178 | std::unique_ptr<GrFragmentProcessor> fp; |
John Stiles | 87d0a2f | 2020-08-10 13:12:41 -0400 | [diff] [blame] | 179 | |
| 180 | ++data->fCurrentTreeDepth; |
| 181 | if (data->fCurrentTreeDepth > data->fMaxTreeDepth) { |
| 182 | // We've gone too deep, but we can't necessarily return null without risking an assertion. |
| 183 | // Instead, return a known-simple zero-child FP. This limits the recursion, and the |
| 184 | // generated FP will be rejected by the numNonNullChildProcessors check below. |
Brian Salomon | 354147a | 2021-04-14 11:15:05 -0400 | [diff] [blame] | 185 | fp = GrFragmentProcessor::MakeColor(SK_PMColor4fTRANSPARENT); |
John Stiles | 87d0a2f | 2020-08-10 13:12:41 -0400 | [diff] [blame] | 186 | } else { |
| 187 | for (;;) { |
| 188 | fp = GrFragmentProcessorTestFactory::Make(data); |
| 189 | SkASSERT(fp); |
| 190 | // If our tree has already reached its max depth, we must reject FPs that have children. |
| 191 | if (data->fCurrentTreeDepth < data->fMaxTreeDepth || |
| 192 | fp->numNonNullChildProcessors() == 0) { |
| 193 | break; |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | --data->fCurrentTreeDepth; |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 199 | return fp; |
bsalomon | 506c802 | 2015-09-14 13:16:26 -0700 | [diff] [blame] | 200 | } |
John Stiles | eac4ad7 | 2020-07-17 15:46:20 -0400 | [diff] [blame] | 201 | |
John Stiles | 87d0a2f | 2020-08-10 13:12:41 -0400 | [diff] [blame] | 202 | std::unique_ptr<GrFragmentProcessor> GrProcessorUnitTest::MakeOptionalChildFP( |
| 203 | GrProcessorTestData* data) { |
| 204 | return data->fRandom->nextBool() ? MakeChildFP(data) : nullptr; |
| 205 | } |
| 206 | |
John Stiles | eac4ad7 | 2020-07-17 15:46:20 -0400 | [diff] [blame] | 207 | template class GrProcessorTestFactory<GrGeometryProcessor*>; |
| 208 | template class GrProcessorTestFactory<std::unique_ptr<GrFragmentProcessor>>; |
| 209 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 210 | #endif |