blob: b4c673ff11981bd18693cd13abd5c4d10f349a4b [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 Stiles87d0a2f2020-08-10 13:12:41 -040021 int maxTreeDepth, int numViews, const ViewInfo views[])
22 : GrProcessorTestData(random, context, maxTreeDepth, numViews, views,
23 /*inputFP=*/nullptr) {}
John Stiles278b4a62020-07-17 16:44:49 -040024
25GrProcessorTestData::GrProcessorTestData(SkRandom* random, GrRecordingContext* context,
John Stiles87d0a2f2020-08-10 13:12:41 -040026 int maxTreeDepth, int numViews, const ViewInfo views[],
John Stiles6609cb62020-07-17 14:52:12 -040027 std::unique_ptr<GrFragmentProcessor> inputFP)
John Stiles87d0a2f2020-08-10 13:12:41 -040028 : fRandom(random)
29 , fMaxTreeDepth(maxTreeDepth)
30 , fContext(context)
31 , fInputFP(std::move(inputFP)) {
Greg Daniel026a60c2020-02-12 10:53:51 -050032 fViews.reset(views, numViews);
John Stilesfbd050b2020-08-03 13:21:46 -040033 fArena = std::make_unique<SkArenaAlloc>(1000);
Brian Salomon766098d2019-12-18 10:41:58 -050034}
35
John Stilese911ce52020-07-17 13:32:27 -040036GrProcessorTestData::~GrProcessorTestData() {}
Brian Salomon766098d2019-12-18 10:41:58 -050037
38GrProxyProvider* GrProcessorTestData::proxyProvider() { return fContext->priv().proxyProvider(); }
39
40const GrCaps* GrProcessorTestData::caps() { return fContext->priv().caps(); }
41
John Stiles87d0a2f2020-08-10 13:12:41 -040042std::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 Stiles6609cb62020-07-17 14:52:12 -040051
Greg Daniel026a60c2020-02-12 10:53:51 -050052GrProcessorTestData::ViewInfo GrProcessorTestData::randomView() {
53 SkASSERT(!fViews.empty());
54 return fViews[fRandom->nextULessThan(fViews.count())];
Brian Salomon766098d2019-12-18 10:41:58 -050055}
56
Greg Daniel026a60c2020-02-12 10:53:51 -050057GrProcessorTestData::ViewInfo GrProcessorTestData::randomAlphaOnlyView() {
Brian Salomon766098d2019-12-18 10:41:58 -050058 int numAlphaOnly = 0;
Greg Daniel026a60c2020-02-12 10:53:51 -050059 for (const auto& [v, ct, at] : fViews) {
Brian Salomon766098d2019-12-18 10:41:58 -050060 if (GrColorTypeIsAlphaOnly(ct)) {
61 ++numAlphaOnly;
62 }
63 }
64 SkASSERT(numAlphaOnly);
65 int idx = fRandom->nextULessThan(numAlphaOnly);
Greg Daniel026a60c2020-02-12 10:53:51 -050066 for (const auto& [v, ct, at] : fViews) {
Brian Salomon766098d2019-12-18 10:41:58 -050067 if (GrColorTypeIsAlphaOnly(ct) && !idx--) {
Greg Daniel026a60c2020-02-12 10:53:51 -050068 return {v, ct, at};
Brian Salomon766098d2019-12-18 10:41:58 -050069 }
70 }
71 SkUNREACHABLE;
72}
73
John Stileseac4ad72020-07-17 15:46:20 -040074template <class ProcessorSmartPtr>
John Stiles7c7a7e52020-07-24 17:03:04 -040075GrProcessorTestFactory<ProcessorSmartPtr>::GrProcessorTestFactory(MakeProc makeProc,
76 const char* name)
77 : fMakeProc(makeProc), fName(name) {
John Stileseac4ad72020-07-17 15:46:20 -040078 GetFactories()->push_back(this);
79}
80
81template <class ProcessorSmartPtr>
82ProcessorSmartPtr 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
91template <class ProcessorSmartPtr>
92ProcessorSmartPtr 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 Stiles7c7a7e52020-07-24 17:03:04 -040097 if (processor == nullptr) {
98 SK_ABORT("%s: TestCreate returned null", factory->fName.c_str());
99 }
John Stileseac4ad72020-07-17 15:46:20 -0400100 return processor;
101}
102
103template <class ProcessorSmartPtr>
104int GrProcessorTestFactory<ProcessorSmartPtr>::Count() {
105 return GetFactories()->count();
106}
107
108GrXPFactoryTestFactory::GrXPFactoryTestFactory(GetFn* getProc) : fGetProc(getProc) {
109 GetFactories()->push_back(this);
110}
111
112const 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 Salomon766098d2019-12-18 10:41:58 -0500122
123/*
124 * Originally these were both in the processor unit test header, but then it seemed to cause linker
125 * problems on android.
126 */
127template <>
128SkTArray<GrFragmentProcessorTestFactory*, true>* GrFragmentProcessorTestFactory::GetFactories() {
129 static SkTArray<GrFragmentProcessorTestFactory*, true> gFactories;
130 return &gFactories;
131}
132
133template <>
134SkTArray<GrGeometryProcessorTestFactory*, true>* GrGeometryProcessorTestFactory::GetFactories() {
135 static SkTArray<GrGeometryProcessorTestFactory*, true> gFactories;
136 return &gFactories;
137}
138
139SkTArray<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 Osmane9ab3912021-07-02 10:17:45 -0400149static constexpr int kFPFactoryCount = 17;
John Stileseac4ad72020-07-17 15:46:20 -0400150static constexpr int kGPFactoryCount = 14;
151static constexpr int kXPFactoryCount = 4;
Brian Salomon766098d2019-12-18 10:41:58 -0500152
153template <> 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
161template <> 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
169void 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 Salomonaff329b2017-08-11 09:40:37 -0400177std::unique_ptr<GrFragmentProcessor> GrProcessorUnitTest::MakeChildFP(GrProcessorTestData* data) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400178 std::unique_ptr<GrFragmentProcessor> fp;
John Stiles87d0a2f2020-08-10 13:12:41 -0400179
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 Salomon354147a2021-04-14 11:15:05 -0400185 fp = GrFragmentProcessor::MakeColor(SK_PMColor4fTRANSPARENT);
John Stiles87d0a2f2020-08-10 13:12:41 -0400186 } 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;
bungeman06ca8ec2016-06-09 08:01:03 -0700199 return fp;
bsalomon506c8022015-09-14 13:16:26 -0700200}
John Stileseac4ad72020-07-17 15:46:20 -0400201
John Stiles87d0a2f2020-08-10 13:12:41 -0400202std::unique_ptr<GrFragmentProcessor> GrProcessorUnitTest::MakeOptionalChildFP(
203 GrProcessorTestData* data) {
204 return data->fRandom->nextBool() ? MakeChildFP(data) : nullptr;
205}
206
John Stileseac4ad72020-07-17 15:46:20 -0400207template class GrProcessorTestFactory<GrGeometryProcessor*>;
208template class GrProcessorTestFactory<std::unique_ptr<GrFragmentProcessor>>;
209
Hal Canary6f6961e2017-01-31 13:50:44 -0500210#endif