blob: 412fd9ed458031bd8fbbc5db88c26d44001d486a [file] [log] [blame]
joshualittb0a8a372014-09-23 09:50:21 -07001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkTypes.h"
Hal Canary6f6961e2017-01-31 13:50:44 -050012
13#if GR_TEST_UTILS
14
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "include/private/SkTArray.h"
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050016#include "src/core/SkArenaAlloc.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/gpu/GrTestUtils.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040018#include "src/gpu/GrTextureProxy.h"
joshualittb0a8a372014-09-23 09:50:21 -070019
Brian Salomon766098d2019-12-18 10:41:58 -050020#include <tuple>
21
joshualittb0a8a372014-09-23 09:50:21 -070022class SkMatrix;
bsalomon4b91f762015-05-19 09:29:46 -070023class GrCaps;
Robert Phillips1afd4cd2018-01-08 13:40:32 -050024class GrProxyProvider;
Brian Salomon766098d2019-12-18 10:41:58 -050025class GrProcessorTestData;
Mike Reedd4706732016-11-15 16:44:34 -050026class GrTexture;
Brian Salomona1633922017-01-09 11:46:10 -050027class GrXPFactory;
Brian Salomon1c053642017-07-24 10:16:19 -040028class GrGeometryProcessor;
joshualittb0a8a372014-09-23 09:50:21 -070029
30namespace GrProcessorUnitTest {
bsalomon506c8022015-09-14 13:16:26 -070031
bsalomon506c8022015-09-14 13:16:26 -070032/** This allows parent FPs to implement a test create with known leaf children in order to avoid
John Stiles87d0a2f2020-08-10 13:12:41 -040033 * creating an unbounded FP tree which may overflow various shader limits.
34 * MakeOptionalChildFP is the same as MakeChildFP, but can return null.
35 */
Brian Salomonaff329b2017-08-11 09:40:37 -040036std::unique_ptr<GrFragmentProcessor> MakeChildFP(GrProcessorTestData*);
John Stiles87d0a2f2020-08-10 13:12:41 -040037std::unique_ptr<GrFragmentProcessor> MakeOptionalChildFP(GrProcessorTestData*);
bsalomon506c8022015-09-14 13:16:26 -070038
John Stilesa6841be2020-08-06 14:11:56 -040039} // namespace GrProcessorUnitTest
joshualitt2e3b3e32014-12-09 13:31:14 -080040
John Stiles87d0a2f2020-08-10 13:12:41 -040041/** GrProcessorTestData is an argument struct to TestCreate functions
42 * fTextures are valid textures that can optionally be used to construct
43 * TextureSampler. The first texture has a RGBA8 format and the second has Alpha8 format for the
44 * specific backend API. TestCreate functions are also free to create additional textures using
45 * the GrContext.
joshualitt0067ff52015-07-08 14:26:19 -070046 */
Brian Salomon766098d2019-12-18 10:41:58 -050047class GrProcessorTestData {
48public:
Greg Daniel026a60c2020-02-12 10:53:51 -050049 using ViewInfo = std::tuple<GrSurfaceProxyView, GrColorType, SkAlphaType>;
John Stilese911ce52020-07-17 13:32:27 -040050
John Stiles87d0a2f2020-08-10 13:12:41 -040051 GrProcessorTestData(SkRandom* random, GrRecordingContext* context, int maxTreeDepth,
John Stiles278b4a62020-07-17 16:44:49 -040052 int numViews, const ViewInfo views[]);
John Stiles87d0a2f2020-08-10 13:12:41 -040053 GrProcessorTestData(SkRandom* random, GrRecordingContext* context, int maxTreeDepth,
John Stiles6609cb62020-07-17 14:52:12 -040054 int numViews, const ViewInfo views[],
John Stiles278b4a62020-07-17 16:44:49 -040055 std::unique_ptr<GrFragmentProcessor> inputFP);
John Stilese911ce52020-07-17 13:32:27 -040056 GrProcessorTestData(const GrProcessorTestData&) = delete;
57 ~GrProcessorTestData();
Robert Phillips757914d2017-01-25 15:48:30 -050058
Robert Phillips4e105e22020-07-16 09:18:50 -040059 GrRecordingContext* context() { return fContext; }
Robert Phillips1afd4cd2018-01-08 13:40:32 -050060 GrProxyProvider* proxyProvider();
Robert Phillips296b1cc2017-03-15 10:42:12 -040061 const GrCaps* caps();
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050062 SkArenaAlloc* allocator() { return fArena.get(); }
John Stiles6609cb62020-07-17 14:52:12 -040063 std::unique_ptr<GrFragmentProcessor> inputFP();
Robert Phillips757914d2017-01-25 15:48:30 -050064
Greg Daniel026a60c2020-02-12 10:53:51 -050065 ViewInfo randomView();
66 ViewInfo randomAlphaOnlyView();
Brian Salomon766098d2019-12-18 10:41:58 -050067
68 SkRandom* fRandom;
John Stiles87d0a2f2020-08-10 13:12:41 -040069 int fCurrentTreeDepth = 0;
70 int fMaxTreeDepth = 1;
Brian Salomon766098d2019-12-18 10:41:58 -050071
Robert Phillips757914d2017-01-25 15:48:30 -050072private:
Robert Phillips4e105e22020-07-16 09:18:50 -040073 GrRecordingContext* fContext;
Greg Daniel026a60c2020-02-12 10:53:51 -050074 SkTArray<ViewInfo> fViews;
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050075 std::unique_ptr<SkArenaAlloc> fArena;
John Stilese911ce52020-07-17 13:32:27 -040076 std::unique_ptr<GrFragmentProcessor> fInputFP;
joshualitt0067ff52015-07-08 14:26:19 -070077};
78
joshualittb0a8a372014-09-23 09:50:21 -070079class GrProcessor;
80class GrTexture;
81
Brian Salomon1c053642017-07-24 10:16:19 -040082template <class ProcessorSmartPtr>
83class GrProcessorTestFactory : private SkNoncopyable {
joshualittb0a8a372014-09-23 09:50:21 -070084public:
Brian Salomon1c053642017-07-24 10:16:19 -040085 using MakeProc = ProcessorSmartPtr (*)(GrProcessorTestData*);
joshualittb0a8a372014-09-23 09:50:21 -070086
John Stiles7c7a7e52020-07-24 17:03:04 -040087 GrProcessorTestFactory(MakeProc makeProc, const char* name);
joshualittb0a8a372014-09-23 09:50:21 -070088
bsalomonb5b60322015-09-14 12:26:33 -070089 /** Pick a random factory function and create a processor. */
John Stileseac4ad72020-07-17 15:46:20 -040090 static ProcessorSmartPtr Make(GrProcessorTestData* data);
bsalomonb5b60322015-09-14 12:26:33 -070091
92 /** Use factory function at Index idx to create a processor. */
John Stileseac4ad72020-07-17 15:46:20 -040093 static ProcessorSmartPtr MakeIdx(int idx, GrProcessorTestData* data);
94
95 /** Number of registered factory functions */
96 static int Count();
joshualittb0a8a372014-09-23 09:50:21 -070097
Brian Salomona1633922017-01-09 11:46:10 -050098private:
John Stileseac4ad72020-07-17 15:46:20 -040099 /** A test function which verifies the count of factories. */
joshualitt9e87fa72014-10-09 13:12:35 -0700100 static void VerifyFactoryCount();
John Stileseac4ad72020-07-17 15:46:20 -0400101 static SkTArray<GrProcessorTestFactory<ProcessorSmartPtr>*, true>* GetFactories();
joshualitt9e87fa72014-10-09 13:12:35 -0700102
bungeman06ca8ec2016-06-09 08:01:03 -0700103 MakeProc fMakeProc;
John Stiles7c7a7e52020-07-24 17:03:04 -0400104 SkString fName;
joshualittb0a8a372014-09-23 09:50:21 -0700105};
106
Brian Salomonaff329b2017-08-11 09:40:37 -0400107using GrFragmentProcessorTestFactory = GrProcessorTestFactory<std::unique_ptr<GrFragmentProcessor>>;
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500108using GrGeometryProcessorTestFactory = GrProcessorTestFactory<GrGeometryProcessor*>;
Brian Salomon1c053642017-07-24 10:16:19 -0400109
Brian Salomona1633922017-01-09 11:46:10 -0500110class GrXPFactoryTestFactory : private SkNoncopyable {
111public:
112 using GetFn = const GrXPFactory*(GrProcessorTestData*);
113
John Stileseac4ad72020-07-17 15:46:20 -0400114 GrXPFactoryTestFactory(GetFn* getProc);
Brian Salomona1633922017-01-09 11:46:10 -0500115
John Stileseac4ad72020-07-17 15:46:20 -0400116 static const GrXPFactory* Get(GrProcessorTestData* data);
Brian Salomona1633922017-01-09 11:46:10 -0500117
118private:
John Stileseac4ad72020-07-17 15:46:20 -0400119 /** A test function which verifies the count of factories. */
Brian Salomona1633922017-01-09 11:46:10 -0500120 static void VerifyFactoryCount();
John Stileseac4ad72020-07-17 15:46:20 -0400121 static SkTArray<GrXPFactoryTestFactory*, true>* GetFactories();
Brian Salomona1633922017-01-09 11:46:10 -0500122
123 GetFn* fGetProc;
Brian Salomona1633922017-01-09 11:46:10 -0500124};
125
Ben Wagnerf1344ac2019-05-10 15:01:53 -0400126#if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
127
joshualittb0a8a372014-09-23 09:50:21 -0700128/** GrProcessor subclasses should insert this macro in their declaration to be included in the
129 * program generation unit test.
130 */
Brian Salomon1c053642017-07-24 10:16:19 -0400131#define GR_DECLARE_GEOMETRY_PROCESSOR_TEST \
132 static GrGeometryProcessorTestFactory gTestFactory SK_UNUSED; \
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500133 static GrGeometryProcessor* TestCreate(GrProcessorTestData*);
joshualittb0a8a372014-09-23 09:50:21 -0700134
Brian Salomon1c053642017-07-24 10:16:19 -0400135#define GR_DECLARE_FRAGMENT_PROCESSOR_TEST \
136 static GrFragmentProcessorTestFactory gTestFactory SK_UNUSED; \
Brian Salomonaff329b2017-08-11 09:40:37 -0400137 static std::unique_ptr<GrFragmentProcessor> TestCreate(GrProcessorTestData*);
joshualittb0a8a372014-09-23 09:50:21 -0700138
egdanielc2304142014-12-11 13:15:13 -0800139#define GR_DECLARE_XP_FACTORY_TEST \
Brian Salomona1633922017-01-09 11:46:10 -0500140 static GrXPFactoryTestFactory gTestFactory SK_UNUSED; \
Brian Salomon0c26a9d2017-07-06 10:09:38 -0400141 static const GrXPFactory* TestGet(GrProcessorTestData*);
egdanielc2304142014-12-11 13:15:13 -0800142
joshualittb0a8a372014-09-23 09:50:21 -0700143/** GrProcessor subclasses should insert this macro in their implementation file. They must then
144 * also implement this static function:
joshualitt0067ff52015-07-08 14:26:19 -0700145 * GrProcessor* TestCreate(GrProcessorTestData*);
joshualittb0a8a372014-09-23 09:50:21 -0700146 */
Brian Salomon1c053642017-07-24 10:16:19 -0400147#define GR_DEFINE_FRAGMENT_PROCESSOR_TEST(Effect) \
John Stiles7c7a7e52020-07-24 17:03:04 -0400148 GrFragmentProcessorTestFactory Effect::gTestFactory(Effect::TestCreate, #Effect)
Brian Salomon003312a2017-01-09 16:00:33 +0000149
Brian Salomon1c053642017-07-24 10:16:19 -0400150#define GR_DEFINE_GEOMETRY_PROCESSOR_TEST(Effect) \
John Stiles7c7a7e52020-07-24 17:03:04 -0400151 GrGeometryProcessorTestFactory Effect::gTestFactory(Effect::TestCreate, #Effect)
Brian Salomona1633922017-01-09 11:46:10 -0500152
153#define GR_DEFINE_XP_FACTORY_TEST(Factory) \
154 GrXPFactoryTestFactory Factory::gTestFactory(Factory::TestGet)
joshualittb0a8a372014-09-23 09:50:21 -0700155
156#else // !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
157
158// The unit test relies on static initializers. Just declare the TestCreate function so that
159// its definitions will compile.
160#define GR_DECLARE_FRAGMENT_PROCESSOR_TEST \
Brian Salomonaff329b2017-08-11 09:40:37 -0400161 static std::unique_ptr<GrFragmentProcessor> TestCreate(GrProcessorTestData*);
joshualittb0a8a372014-09-23 09:50:21 -0700162#define GR_DEFINE_FRAGMENT_PROCESSOR_TEST(X)
163
164// The unit test relies on static initializers. Just declare the TestCreate function so that
165// its definitions will compile.
166#define GR_DECLARE_GEOMETRY_PROCESSOR_TEST \
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500167 static GrGeometryProcessor* TestCreate(GrProcessorTestData*);
joshualittb0a8a372014-09-23 09:50:21 -0700168#define GR_DEFINE_GEOMETRY_PROCESSOR_TEST(X)
169
Brian Salomona1633922017-01-09 11:46:10 -0500170// The unit test relies on static initializers. Just declare the TestGet function so that
171// its definitions will compile.
172#define GR_DECLARE_XP_FACTORY_TEST \
Brian Salomon0c26a9d2017-07-06 10:09:38 -0400173 const GrXPFactory* TestGet(GrProcessorTestData*);
Brian Salomona1633922017-01-09 11:46:10 -0500174#define GR_DEFINE_XP_FACTORY_TEST(X)
175
Hal Canary6f6961e2017-01-31 13:50:44 -0500176#endif // !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
177#else // GR_TEST_UTILS
178 #define GR_DECLARE_GEOMETRY_PROCESSOR_TEST
179 #define GR_DECLARE_FRAGMENT_PROCESSOR_TEST
180 #define GR_DECLARE_XP_FACTORY_TEST
181 #define GR_DEFINE_FRAGMENT_PROCESSOR_TEST(...)
182 #define GR_DEFINE_GEOMETRY_PROCESSOR_TEST(...)
183 #define GR_DEFINE_XP_FACTORY_TEST(...)
184 #define GR_DECLARE_FRAGMENT_PROCESSOR_TEST
185 #define GR_DEFINE_FRAGMENT_PROCESSOR_TEST(...)
186 #define GR_DECLARE_GEOMETRY_PROCESSOR_TEST
187 #define GR_DEFINE_GEOMETRY_PROCESSOR_TEST(...)
188 #define GR_DECLARE_XP_FACTORY_TEST
189 #define GR_DEFINE_XP_FACTORY_TEST(...)
190#endif // GR_TEST_UTILS
191#endif // GrProcessorUnitTest_DEFINED