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