blob: 0f47ba536acf65e7d64a192fdec302ed004eac82 [file] [log] [blame]
bsalomon506c8022015-09-14 13:16:26 -07001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/GrProcessorUnitTest.h"
bsalomon506c8022015-09-14 13:16:26 -07009
John Stilesfbd050b2020-08-03 13:21:46 -040010#include <memory>
11
Robert Phillips4e105e22020-07-16 09:18:50 -040012#include "include/gpu/GrRecordingContext.h"
Brian Salomon766098d2019-12-18 10:41:58 -050013#include "src/gpu/GrFragmentProcessor.h"
Robert Phillips4e105e22020-07-16 09:18:50 -040014#include "src/gpu/GrRecordingContextPriv.h"
Brian Salomon766098d2019-12-18 10:41:58 -050015
Hal Canary6f6961e2017-01-31 13:50:44 -050016#if GR_TEST_UTILS
17
John Stileseac4ad72020-07-17 15:46:20 -040018class GrGeometryProcessor;
19
John Stiles6609cb62020-07-17 14:52:12 -040020GrProcessorTestData::GrProcessorTestData(SkRandom* random, GrRecordingContext* context,
John Stiles278b4a62020-07-17 16:44:49 -040021 int numViews, const ViewInfo views[])
22 : GrProcessorTestData(random, context, numViews, views, /*inputFP=*/nullptr) {}
23
24GrProcessorTestData::GrProcessorTestData(SkRandom* random, GrRecordingContext* context,
John Stiles6609cb62020-07-17 14:52:12 -040025 int numViews, const ViewInfo views[],
26 std::unique_ptr<GrFragmentProcessor> inputFP)
27 : fRandom(random), fContext(context), fInputFP(std::move(inputFP)) {
Greg Daniel026a60c2020-02-12 10:53:51 -050028 fViews.reset(views, numViews);
John Stilesfbd050b2020-08-03 13:21:46 -040029 fArena = std::make_unique<SkArenaAlloc>(1000);
Brian Salomon766098d2019-12-18 10:41:58 -050030}
31
John Stilese911ce52020-07-17 13:32:27 -040032GrProcessorTestData::~GrProcessorTestData() {}
Brian Salomon766098d2019-12-18 10:41:58 -050033
34GrProxyProvider* GrProcessorTestData::proxyProvider() { return fContext->priv().proxyProvider(); }
35
36const GrCaps* GrProcessorTestData::caps() { return fContext->priv().caps(); }
37
John Stiles6609cb62020-07-17 14:52:12 -040038std::unique_ptr<GrFragmentProcessor> GrProcessorTestData::inputFP() { return std::move(fInputFP); }
39
Greg Daniel026a60c2020-02-12 10:53:51 -050040GrProcessorTestData::ViewInfo GrProcessorTestData::randomView() {
41 SkASSERT(!fViews.empty());
42 return fViews[fRandom->nextULessThan(fViews.count())];
Brian Salomon766098d2019-12-18 10:41:58 -050043}
44
Greg Daniel026a60c2020-02-12 10:53:51 -050045GrProcessorTestData::ViewInfo GrProcessorTestData::randomAlphaOnlyView() {
Brian Salomon766098d2019-12-18 10:41:58 -050046 int numAlphaOnly = 0;
Greg Daniel026a60c2020-02-12 10:53:51 -050047 for (const auto& [v, ct, at] : fViews) {
Brian Salomon766098d2019-12-18 10:41:58 -050048 if (GrColorTypeIsAlphaOnly(ct)) {
49 ++numAlphaOnly;
50 }
51 }
52 SkASSERT(numAlphaOnly);
53 int idx = fRandom->nextULessThan(numAlphaOnly);
Greg Daniel026a60c2020-02-12 10:53:51 -050054 for (const auto& [v, ct, at] : fViews) {
Brian Salomon766098d2019-12-18 10:41:58 -050055 if (GrColorTypeIsAlphaOnly(ct) && !idx--) {
Greg Daniel026a60c2020-02-12 10:53:51 -050056 return {v, ct, at};
Brian Salomon766098d2019-12-18 10:41:58 -050057 }
58 }
59 SkUNREACHABLE;
60}
61
John Stileseac4ad72020-07-17 15:46:20 -040062template <class ProcessorSmartPtr>
John Stiles7c7a7e52020-07-24 17:03:04 -040063GrProcessorTestFactory<ProcessorSmartPtr>::GrProcessorTestFactory(MakeProc makeProc,
64 const char* name)
65 : fMakeProc(makeProc), fName(name) {
John Stileseac4ad72020-07-17 15:46:20 -040066 GetFactories()->push_back(this);
67}
68
69template <class ProcessorSmartPtr>
70ProcessorSmartPtr 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
79template <class ProcessorSmartPtr>
80ProcessorSmartPtr 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 Stiles7c7a7e52020-07-24 17:03:04 -040085 if (processor == nullptr) {
86 SK_ABORT("%s: TestCreate returned null", factory->fName.c_str());
87 }
John Stileseac4ad72020-07-17 15:46:20 -040088 return processor;
89}
90
91template <class ProcessorSmartPtr>
92int GrProcessorTestFactory<ProcessorSmartPtr>::Count() {
93 return GetFactories()->count();
94}
95
96GrXPFactoryTestFactory::GrXPFactoryTestFactory(GetFn* getProc) : fGetProc(getProc) {
97 GetFactories()->push_back(this);
98}
99
100const 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 Salomon766098d2019-12-18 10:41:58 -0500110
111/*
112 * Originally these were both in the processor unit test header, but then it seemed to cause linker
113 * problems on android.
114 */
115template <>
116SkTArray<GrFragmentProcessorTestFactory*, true>* GrFragmentProcessorTestFactory::GetFactories() {
117 static SkTArray<GrFragmentProcessorTestFactory*, true> gFactories;
118 return &gFactories;
119}
120
121template <>
122SkTArray<GrGeometryProcessorTestFactory*, true>* GrGeometryProcessorTestFactory::GetFactories() {
123 static SkTArray<GrGeometryProcessorTestFactory*, true> gFactories;
124 return &gFactories;
125}
126
127SkTArray<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 Stileseac4ad72020-07-17 15:46:20 -0400137static constexpr int kFPFactoryCount = 37;
138static constexpr int kGPFactoryCount = 14;
139static constexpr int kXPFactoryCount = 4;
Brian Salomon766098d2019-12-18 10:41:58 -0500140
141template <> 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
149template <> 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
157void 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 Salomonaff329b2017-08-11 09:40:37 -0400165std::unique_ptr<GrFragmentProcessor> GrProcessorUnitTest::MakeChildFP(GrProcessorTestData* data) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400166 std::unique_ptr<GrFragmentProcessor> fp;
bsalomon506c8022015-09-14 13:16:26 -0700167 do {
Brian Salomon1c053642017-07-24 10:16:19 -0400168 fp = GrFragmentProcessorTestFactory::Make(data);
bsalomon506c8022015-09-14 13:16:26 -0700169 SkASSERT(fp);
Brian Osman12c5d292020-07-13 16:11:35 -0400170 } while (fp->numNonNullChildProcessors() != 0);
bungeman06ca8ec2016-06-09 08:01:03 -0700171 return fp;
bsalomon506c8022015-09-14 13:16:26 -0700172}
John Stileseac4ad72020-07-17 15:46:20 -0400173
174template class GrProcessorTestFactory<GrGeometryProcessor*>;
175template class GrProcessorTestFactory<std::unique_ptr<GrFragmentProcessor>>;
176
Hal Canary6f6961e2017-01-31 13:50:44 -0500177#endif