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