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