| 1 |
/* |
| 2 |
* Copyright 2012 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 |
#ifndef GrProcessorUnitTest_DEFINED |
| 9 |
#define GrProcessorUnitTest_DEFINED |
| 10 |
|
| 11 |
#include "include/core/SkTypes.h" |
| 12 |
|
| 13 |
#if defined(GPU_TEST_UTILS) |
| 14 |
|
| 15 |
#include "include/core/SkString.h" |
| 16 |
#include "include/private/base/SkNoncopyable.h" |
| 17 |
#include "include/private/base/SkTArray.h" |
| 18 |
#include "src/gpu/ganesh/GrFragmentProcessor.h" |
| 19 |
#include "src/gpu/ganesh/GrSurfaceProxyView.h" // IWYU pragma: keep |
| 20 |
|
| 21 |
#include <memory> |
| 22 |
#include <tuple> |
| 23 |
|
| 24 |
class GrCaps; |
| 25 |
class GrGeometryProcessor; |
| 26 |
class GrProcessorTestData; |
| 27 |
class GrProxyProvider; |
| 28 |
class GrRecordingContext; |
| 29 |
class GrXPFactory; |
| 30 |
class SkArenaAlloc; |
| 31 |
class SkRandom; |
| 32 |
enum SkAlphaType : int; |
| 33 |
enum class GrColorType; |
| 34 |
namespace skgpu::ganesh { class SurfaceDrawContext; } |
| 35 |
|
| 36 |
namespace GrProcessorUnitTest { |
| 37 |
|
| 38 |
/** This allows parent FPs to implement a test create with known leaf children in order to avoid |
| 39 |
* creating an unbounded FP tree which may overflow various shader limits. |
| 40 |
* MakeOptionalChildFP is the same as MakeChildFP, but can return null. |
| 41 |
*/ |
| 42 |
std::unique_ptr<GrFragmentProcessor> MakeChildFP(GrProcessorTestData*); |
| 43 |
std::unique_ptr<GrFragmentProcessor> MakeOptionalChildFP(GrProcessorTestData*); |
| 44 |
|
| 45 |
} // namespace GrProcessorUnitTest |
| 46 |
|
| 47 |
/** GrProcessorTestData is an argument struct to TestCreate functions |
| 48 |
* fTextures are valid textures that can optionally be used to construct |
| 49 |
* TextureSampler. The first texture has a RGBA8 format and the second has Alpha8 format for the |
| 50 |
* specific backend API. TestCreate functions are also free to create additional textures using |
| 51 |
* the GrContext. |
| 52 |
*/ |
| 53 |
class GrProcessorTestData { |
| 54 |
public: |
| 55 |
using ViewInfo = std::tuple<GrSurfaceProxyView, GrColorType, SkAlphaType>; |
| 56 |
|
| 57 |
GrProcessorTestData(SkRandom* random, skgpu::ganesh::SurfaceDrawContext* sdc, |
| 58 |
int maxTreeDepth, SkSpan<const ViewInfo> views); |
| 59 |
GrProcessorTestData(SkRandom* random, skgpu::ganesh::SurfaceDrawContext* sdc, |
| 60 |
int maxTreeDepth, SkSpan<const ViewInfo> views, |
| 61 |
std::unique_ptr<GrFragmentProcessor> inputFP); |
| 62 |
GrProcessorTestData(const GrProcessorTestData&) = delete; |
| 63 |
~GrProcessorTestData(); |
| 64 |
|
| 65 |
GrRecordingContext* context() { return fContext; } |
| 66 |
skgpu::ganesh::SurfaceDrawContext* surfaceDrawContext() { return fDrawContext; } |
| 67 |
GrProxyProvider* proxyProvider(); |
| 68 |
const GrCaps* caps(); |
| 69 |
SkArenaAlloc* allocator() { return fArena.get(); } |
| 70 |
std::unique_ptr<GrFragmentProcessor> inputFP(); |
| 71 |
|
| 72 |
ViewInfo randomView(); |
| 73 |
ViewInfo randomAlphaOnlyView(); |
| 74 |
|
| 75 |
SkRandom* fRandom; |
| 76 |
int fCurrentTreeDepth = 0; |
| 77 |
int fMaxTreeDepth = 1; |
| 78 |
|
| 79 |
private: |
| 80 |
GrRecordingContext* fContext; |
| 81 |
skgpu::ganesh::SurfaceDrawContext* fDrawContext; |
| 82 |
skia_private::TArray<ViewInfo> fViews; |
| 83 |
std::unique_ptr<SkArenaAlloc> fArena; |
| 84 |
std::unique_ptr<GrFragmentProcessor> fInputFP; |
| 85 |
}; |
| 86 |
|
| 87 |
template <class ProcessorSmartPtr> |
| 88 |
class GrProcessorTestFactory : private SkNoncopyable { |
| 89 |
public: |
| 90 |
using MakeProc = ProcessorSmartPtr (*)(GrProcessorTestData*); |
| 91 |
|
| 92 |
GrProcessorTestFactory(MakeProc makeProc, const char* name); |
| 93 |
|
| 94 |
/** Pick a random factory function and create a processor. */ |
| 95 |
static ProcessorSmartPtr Make(GrProcessorTestData* data); |
| 96 |
|
| 97 |
/** Use factory function at Index idx to create a processor. */ |
| 98 |
static ProcessorSmartPtr MakeIdx(int idx, GrProcessorTestData* data); |
| 99 |
|
| 100 |
/** Number of registered factory functions */ |
| 101 |
static int Count(); |
| 102 |
|
| 103 |
private: |
| 104 |
/** A test function which verifies the count of factories. */ |
| 105 |
static void VerifyFactoryCount(); |
| 106 |
static skia_private::TArray<GrProcessorTestFactory<ProcessorSmartPtr>*, true>* GetFactories(); |
| 107 |
|
| 108 |
MakeProc fMakeProc; |
| 109 |
SkString fName; |
| 110 |
}; |
| 111 |
|
| 112 |
using GrFragmentProcessorTestFactory = GrProcessorTestFactory<std::unique_ptr<GrFragmentProcessor>>; |
| 113 |
using GrGeometryProcessorTestFactory = GrProcessorTestFactory<GrGeometryProcessor*>; |
| 114 |
|
| 115 |
class GrXPFactoryTestFactory : private SkNoncopyable { |
| 116 |
public: |
| 117 |
using GetFn = const GrXPFactory*(GrProcessorTestData*); |
| 118 |
|
| 119 |
GrXPFactoryTestFactory(GetFn* getProc); |
| 120 |
|
| 121 |
static const GrXPFactory* Get(GrProcessorTestData* data); |
| 122 |
|
| 123 |
private: |
| 124 |
/** A test function which verifies the count of factories. */ |
| 125 |
static void VerifyFactoryCount(); |
| 126 |
static skia_private::TArray<GrXPFactoryTestFactory*, true>* GetFactories(); |
| 127 |
|
| 128 |
GetFn* fGetProc; |
| 129 |
}; |
| 130 |
|
| 131 |
#if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS |
| 132 |
|
| 133 |
/** GrProcessor subclasses should insert this macro in their declaration to be included in the |
| 134 |
* program generation unit test. |
| 135 |
*/ |
| 136 |
#define GR_DECLARE_GEOMETRY_PROCESSOR_TEST \ |
| 137 |
[[maybe_unused]] static GrGeometryProcessorTestFactory* gTestFactory; \ |
| 138 |
static GrGeometryProcessor* TestCreate(GrProcessorTestData*); |
| 139 |
|
| 140 |
#define GR_DECLARE_FRAGMENT_PROCESSOR_TEST \ |
| 141 |
[[maybe_unused]] static GrFragmentProcessorTestFactory* gTestFactory; \ |
| 142 |
static std::unique_ptr<GrFragmentProcessor> TestCreate(GrProcessorTestData*); |
| 143 |
|
| 144 |
#define GR_DECLARE_XP_FACTORY_TEST \ |
| 145 |
[[maybe_unused]] static GrXPFactoryTestFactory* gTestFactory; \ |
| 146 |
static const GrXPFactory* TestGet(GrProcessorTestData*); |
| 147 |
|
| 148 |
/** GrProcessor subclasses should insert this macro in their implementation file. They must then |
| 149 |
* also implement this static function: |
| 150 |
* GrProcessor* TestCreate(GrProcessorTestData*); |
| 151 |
*/ |
| 152 |
#define GR_DEFINE_FRAGMENT_PROCESSOR_TEST(Effect) \ |
| 153 |
GrFragmentProcessorTestFactory* Effect::gTestFactory = \ |
| 154 |
new GrFragmentProcessorTestFactory(Effect::TestCreate, #Effect); |
| 155 |
|
| 156 |
#define GR_DEFINE_GEOMETRY_PROCESSOR_TEST(Effect) \ |
| 157 |
GrGeometryProcessorTestFactory* Effect::gTestFactory = \ |
| 158 |
new GrGeometryProcessorTestFactory(Effect::TestCreate, #Effect); |
| 159 |
|
| 160 |
#define GR_DEFINE_XP_FACTORY_TEST(Factory) \ |
| 161 |
GrXPFactoryTestFactory* Factory::gTestFactory = new GrXPFactoryTestFactory(Factory::TestGet); |
| 162 |
|
| 163 |
#else // !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS |
| 164 |
|
| 165 |
// The unit test relies on static initializers. Just declare the TestCreate function so that |
| 166 |
// its definitions will compile. |
| 167 |
#define GR_DECLARE_FRAGMENT_PROCESSOR_TEST \ |
| 168 |
static std::unique_ptr<GrFragmentProcessor> TestCreate(GrProcessorTestData*); |
| 169 |
#define GR_DEFINE_FRAGMENT_PROCESSOR_TEST(X) |
| 170 |
|
| 171 |
// The unit test relies on static initializers. Just declare the TestCreate function so that |
| 172 |
// its definitions will compile. |
| 173 |
#define GR_DECLARE_GEOMETRY_PROCESSOR_TEST \ |
| 174 |
static GrGeometryProcessor* TestCreate(GrProcessorTestData*); |
| 175 |
#define GR_DEFINE_GEOMETRY_PROCESSOR_TEST(X) |
| 176 |
|
| 177 |
// The unit test relies on static initializers. Just declare the TestGet function so that |
| 178 |
// its definitions will compile. |
| 179 |
#define GR_DECLARE_XP_FACTORY_TEST \ |
| 180 |
const GrXPFactory* TestGet(GrProcessorTestData*); |
| 181 |
#define GR_DEFINE_XP_FACTORY_TEST(X) |
| 182 |
|
| 183 |
#endif // !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS |
| 184 |
#else // defined(GPU_TEST_UTILS) |
| 185 |
#define GR_DECLARE_GEOMETRY_PROCESSOR_TEST |
| 186 |
#define GR_DECLARE_FRAGMENT_PROCESSOR_TEST |
| 187 |
#define GR_DECLARE_XP_FACTORY_TEST |
| 188 |
#define GR_DEFINE_FRAGMENT_PROCESSOR_TEST(...) |
| 189 |
#define GR_DEFINE_GEOMETRY_PROCESSOR_TEST(...) |
| 190 |
#define GR_DEFINE_XP_FACTORY_TEST(...) |
| 191 |
#define GR_DECLARE_FRAGMENT_PROCESSOR_TEST |
| 192 |
#define GR_DEFINE_FRAGMENT_PROCESSOR_TEST(...) |
| 193 |
#define GR_DECLARE_GEOMETRY_PROCESSOR_TEST |
| 194 |
#define GR_DEFINE_GEOMETRY_PROCESSOR_TEST(...) |
| 195 |
#define GR_DECLARE_XP_FACTORY_TEST |
| 196 |
#define GR_DEFINE_XP_FACTORY_TEST(...) |
| 197 |
#endif // defined(GPU_TEST_UTILS) |
| 198 |
#endif // GrProcessorUnitTest_DEFINED |