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 | 278b4a6 | 2020-07-17 16:44:49 -0400 | [diff] [blame] | 21 | int numViews, const ViewInfo views[]) |
| 22 | : GrProcessorTestData(random, context, numViews, views, /*inputFP=*/nullptr) {} |
| 23 | |
| 24 | GrProcessorTestData::GrProcessorTestData(SkRandom* random, GrRecordingContext* context, |
John Stiles | 6609cb6 | 2020-07-17 14:52:12 -0400 | [diff] [blame] | 25 | int numViews, const ViewInfo views[], |
| 26 | std::unique_ptr<GrFragmentProcessor> inputFP) |
| 27 | : fRandom(random), fContext(context), fInputFP(std::move(inputFP)) { |
Greg Daniel | 026a60c | 2020-02-12 10:53:51 -0500 | [diff] [blame] | 28 | fViews.reset(views, numViews); |
John Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame^] | 29 | fArena = std::make_unique<SkArenaAlloc>(1000); |
Brian Salomon | 766098d | 2019-12-18 10:41:58 -0500 | [diff] [blame] | 30 | } |
| 31 | |
John Stiles | e911ce5 | 2020-07-17 13:32:27 -0400 | [diff] [blame] | 32 | GrProcessorTestData::~GrProcessorTestData() {} |
Brian Salomon | 766098d | 2019-12-18 10:41:58 -0500 | [diff] [blame] | 33 | |
| 34 | GrProxyProvider* GrProcessorTestData::proxyProvider() { return fContext->priv().proxyProvider(); } |
| 35 | |
| 36 | const GrCaps* GrProcessorTestData::caps() { return fContext->priv().caps(); } |
| 37 | |
John Stiles | 6609cb6 | 2020-07-17 14:52:12 -0400 | [diff] [blame] | 38 | std::unique_ptr<GrFragmentProcessor> GrProcessorTestData::inputFP() { return std::move(fInputFP); } |
| 39 | |
Greg Daniel | 026a60c | 2020-02-12 10:53:51 -0500 | [diff] [blame] | 40 | GrProcessorTestData::ViewInfo GrProcessorTestData::randomView() { |
| 41 | SkASSERT(!fViews.empty()); |
| 42 | return fViews[fRandom->nextULessThan(fViews.count())]; |
Brian Salomon | 766098d | 2019-12-18 10:41:58 -0500 | [diff] [blame] | 43 | } |
| 44 | |
Greg Daniel | 026a60c | 2020-02-12 10:53:51 -0500 | [diff] [blame] | 45 | GrProcessorTestData::ViewInfo GrProcessorTestData::randomAlphaOnlyView() { |
Brian Salomon | 766098d | 2019-12-18 10:41:58 -0500 | [diff] [blame] | 46 | int numAlphaOnly = 0; |
Greg Daniel | 026a60c | 2020-02-12 10:53:51 -0500 | [diff] [blame] | 47 | for (const auto& [v, ct, at] : fViews) { |
Brian Salomon | 766098d | 2019-12-18 10:41:58 -0500 | [diff] [blame] | 48 | if (GrColorTypeIsAlphaOnly(ct)) { |
| 49 | ++numAlphaOnly; |
| 50 | } |
| 51 | } |
| 52 | SkASSERT(numAlphaOnly); |
| 53 | int idx = fRandom->nextULessThan(numAlphaOnly); |
Greg Daniel | 026a60c | 2020-02-12 10:53:51 -0500 | [diff] [blame] | 54 | for (const auto& [v, ct, at] : fViews) { |
Brian Salomon | 766098d | 2019-12-18 10:41:58 -0500 | [diff] [blame] | 55 | if (GrColorTypeIsAlphaOnly(ct) && !idx--) { |
Greg Daniel | 026a60c | 2020-02-12 10:53:51 -0500 | [diff] [blame] | 56 | return {v, ct, at}; |
Brian Salomon | 766098d | 2019-12-18 10:41:58 -0500 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | SkUNREACHABLE; |
| 60 | } |
| 61 | |
John Stiles | eac4ad7 | 2020-07-17 15:46:20 -0400 | [diff] [blame] | 62 | template <class ProcessorSmartPtr> |
John Stiles | 7c7a7e5 | 2020-07-24 17:03:04 -0400 | [diff] [blame] | 63 | GrProcessorTestFactory<ProcessorSmartPtr>::GrProcessorTestFactory(MakeProc makeProc, |
| 64 | const char* name) |
| 65 | : fMakeProc(makeProc), fName(name) { |
John Stiles | eac4ad7 | 2020-07-17 15:46:20 -0400 | [diff] [blame] | 66 | GetFactories()->push_back(this); |
| 67 | } |
| 68 | |
| 69 | template <class ProcessorSmartPtr> |
| 70 | ProcessorSmartPtr GrProcessorTestFactory<ProcessorSmartPtr>::Make(GrProcessorTestData* data) { |
| 71 | VerifyFactoryCount(); |
| 72 | if (GetFactories()->count() == 0) { |
| 73 | return nullptr; |
| 74 | } |
| 75 | uint32_t idx = data->fRandom->nextULessThan(GetFactories()->count()); |
| 76 | return MakeIdx(idx, data); |
| 77 | } |
| 78 | |
| 79 | template <class ProcessorSmartPtr> |
| 80 | ProcessorSmartPtr GrProcessorTestFactory<ProcessorSmartPtr>::MakeIdx(int idx, |
| 81 | GrProcessorTestData* data) { |
| 82 | SkASSERT(idx < GetFactories()->count()); |
| 83 | GrProcessorTestFactory<ProcessorSmartPtr>* factory = (*GetFactories())[idx]; |
| 84 | ProcessorSmartPtr processor = factory->fMakeProc(data); |
John Stiles | 7c7a7e5 | 2020-07-24 17:03:04 -0400 | [diff] [blame] | 85 | if (processor == nullptr) { |
| 86 | SK_ABORT("%s: TestCreate returned null", factory->fName.c_str()); |
| 87 | } |
John Stiles | eac4ad7 | 2020-07-17 15:46:20 -0400 | [diff] [blame] | 88 | return processor; |
| 89 | } |
| 90 | |
| 91 | template <class ProcessorSmartPtr> |
| 92 | int GrProcessorTestFactory<ProcessorSmartPtr>::Count() { |
| 93 | return GetFactories()->count(); |
| 94 | } |
| 95 | |
| 96 | GrXPFactoryTestFactory::GrXPFactoryTestFactory(GetFn* getProc) : fGetProc(getProc) { |
| 97 | GetFactories()->push_back(this); |
| 98 | } |
| 99 | |
| 100 | const GrXPFactory* GrXPFactoryTestFactory::Get(GrProcessorTestData* data) { |
| 101 | VerifyFactoryCount(); |
| 102 | if (GetFactories()->count() == 0) { |
| 103 | return nullptr; |
| 104 | } |
| 105 | uint32_t idx = data->fRandom->nextRangeU(0, GetFactories()->count() - 1); |
| 106 | const GrXPFactory* xpf = (*GetFactories())[idx]->fGetProc(data); |
| 107 | SkASSERT(xpf); |
| 108 | return xpf; |
| 109 | } |
Brian Salomon | 766098d | 2019-12-18 10:41:58 -0500 | [diff] [blame] | 110 | |
| 111 | /* |
| 112 | * Originally these were both in the processor unit test header, but then it seemed to cause linker |
| 113 | * problems on android. |
| 114 | */ |
| 115 | template <> |
| 116 | SkTArray<GrFragmentProcessorTestFactory*, true>* GrFragmentProcessorTestFactory::GetFactories() { |
| 117 | static SkTArray<GrFragmentProcessorTestFactory*, true> gFactories; |
| 118 | return &gFactories; |
| 119 | } |
| 120 | |
| 121 | template <> |
| 122 | SkTArray<GrGeometryProcessorTestFactory*, true>* GrGeometryProcessorTestFactory::GetFactories() { |
| 123 | static SkTArray<GrGeometryProcessorTestFactory*, true> gFactories; |
| 124 | return &gFactories; |
| 125 | } |
| 126 | |
| 127 | SkTArray<GrXPFactoryTestFactory*, true>* GrXPFactoryTestFactory::GetFactories() { |
| 128 | static SkTArray<GrXPFactoryTestFactory*, true> gFactories; |
| 129 | return &gFactories; |
| 130 | } |
| 131 | |
| 132 | /* |
| 133 | * To ensure we always have successful static initialization, before creating from the factories |
| 134 | * we verify the count is as expected. If a new factory is added, then these numbers must be |
| 135 | * manually adjusted. |
| 136 | */ |
John Stiles | eac4ad7 | 2020-07-17 15:46:20 -0400 | [diff] [blame] | 137 | static constexpr int kFPFactoryCount = 37; |
| 138 | static constexpr int kGPFactoryCount = 14; |
| 139 | static constexpr int kXPFactoryCount = 4; |
Brian Salomon | 766098d | 2019-12-18 10:41:58 -0500 | [diff] [blame] | 140 | |
| 141 | template <> void GrFragmentProcessorTestFactory::VerifyFactoryCount() { |
| 142 | if (kFPFactoryCount != GetFactories()->count()) { |
| 143 | SkDebugf("\nExpected %d fragment processor factories, found %d.\n", kFPFactoryCount, |
| 144 | GetFactories()->count()); |
| 145 | SK_ABORT("Wrong number of fragment processor factories!"); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | template <> void GrGeometryProcessorTestFactory::VerifyFactoryCount() { |
| 150 | if (kGPFactoryCount != GetFactories()->count()) { |
| 151 | SkDebugf("\nExpected %d geometry processor factories, found %d.\n", kGPFactoryCount, |
| 152 | GetFactories()->count()); |
| 153 | SK_ABORT("Wrong number of geometry processor factories!"); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | void GrXPFactoryTestFactory::VerifyFactoryCount() { |
| 158 | if (kXPFactoryCount != GetFactories()->count()) { |
| 159 | SkDebugf("\nExpected %d xp factory factories, found %d.\n", kXPFactoryCount, |
| 160 | GetFactories()->count()); |
| 161 | SK_ABORT("Wrong number of xp factory factories!"); |
| 162 | } |
| 163 | } |
| 164 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 165 | std::unique_ptr<GrFragmentProcessor> GrProcessorUnitTest::MakeChildFP(GrProcessorTestData* data) { |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 166 | std::unique_ptr<GrFragmentProcessor> fp; |
bsalomon | 506c802 | 2015-09-14 13:16:26 -0700 | [diff] [blame] | 167 | do { |
Brian Salomon | 1c05364 | 2017-07-24 10:16:19 -0400 | [diff] [blame] | 168 | fp = GrFragmentProcessorTestFactory::Make(data); |
bsalomon | 506c802 | 2015-09-14 13:16:26 -0700 | [diff] [blame] | 169 | SkASSERT(fp); |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 170 | } while (fp->numNonNullChildProcessors() != 0); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 171 | return fp; |
bsalomon | 506c802 | 2015-09-14 13:16:26 -0700 | [diff] [blame] | 172 | } |
John Stiles | eac4ad7 | 2020-07-17 15:46:20 -0400 | [diff] [blame] | 173 | |
| 174 | template class GrProcessorTestFactory<GrGeometryProcessor*>; |
| 175 | template class GrProcessorTestFactory<std::unique_ptr<GrFragmentProcessor>>; |
| 176 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 177 | #endif |