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