blob: 1cde80e18128fd3822d9b4111df37202b53153b7 [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 Osman11052242016-10-27 14:47:55 -040025class GrRenderTargetContext;
Brian Salomon766098d2019-12-18 10:41:58 -050026class GrProcessorTestData;
Mike Reedd4706732016-11-15 16:44:34 -050027class GrTexture;
Brian Salomona1633922017-01-09 11:46:10 -050028class GrXPFactory;
Brian Salomon1c053642017-07-24 10:16:19 -040029class GrGeometryProcessor;
joshualittb0a8a372014-09-23 09:50:21 -070030
31namespace GrProcessorUnitTest {
bsalomon506c8022015-09-14 13:16:26 -070032
joshualittb0a8a372014-09-23 09:50:21 -070033// Used to access the dummy textures in TestCreate procs.
34enum {
35 kSkiaPMTextureIdx = 0,
36 kAlphaTextureIdx = 1,
37};
38
bsalomon506c8022015-09-14 13:16:26 -070039/** This allows parent FPs to implement a test create with known leaf children in order to avoid
40creating an unbounded FP tree which may overflow various shader limits. */
Brian Salomonaff329b2017-08-11 09:40:37 -040041std::unique_ptr<GrFragmentProcessor> MakeChildFP(GrProcessorTestData*);
bsalomon506c8022015-09-14 13:16:26 -070042
joshualitt2e3b3e32014-12-09 13:31:14 -080043}
44
joshualitt0067ff52015-07-08 14:26:19 -070045/*
46 * GrProcessorTestData is an argument struct to TestCreate functions
47 * fTextures are valid textures that can optionally be used to construct
Greg Danielb58a3c72020-01-23 10:05:14 -050048 * 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
joshualitt0067ff52015-07-08 14:26:19 -070050 * the GrContext.
51 */
Brian Salomon766098d2019-12-18 10:41:58 -050052class GrProcessorTestData {
53public:
Greg Daniel026a60c2020-02-12 10:53:51 -050054 using ViewInfo = std::tuple<GrSurfaceProxyView, GrColorType, SkAlphaType>;
Robert Phillips4e105e22020-07-16 09:18:50 -040055 GrProcessorTestData(SkRandom*, GrRecordingContext*, int numProxies, const ViewInfo[]);
Robert Phillips757914d2017-01-25 15:48:30 -050056
Robert Phillips4e105e22020-07-16 09:18:50 -040057 GrRecordingContext* context() { return fContext; }
Robert Phillips1afd4cd2018-01-08 13:40:32 -050058 GrProxyProvider* proxyProvider();
Robert Phillips296b1cc2017-03-15 10:42:12 -040059 const GrCaps* caps();
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050060 SkArenaAlloc* allocator() { return fArena.get(); }
Robert Phillips757914d2017-01-25 15:48:30 -050061
Greg Daniel026a60c2020-02-12 10:53:51 -050062 ViewInfo randomView();
63 ViewInfo randomAlphaOnlyView();
Brian Salomon766098d2019-12-18 10:41:58 -050064
65 SkRandom* fRandom;
66
Robert Phillips757914d2017-01-25 15:48:30 -050067private:
Robert Phillips4e105e22020-07-16 09:18:50 -040068 GrRecordingContext* fContext;
Greg Daniel026a60c2020-02-12 10:53:51 -050069 SkTArray<ViewInfo> fViews;
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050070 std::unique_ptr<SkArenaAlloc> fArena;
joshualitt0067ff52015-07-08 14:26:19 -070071};
72
joshualittb0a8a372014-09-23 09:50:21 -070073class GrProcessor;
74class GrTexture;
75
Brian Salomon1c053642017-07-24 10:16:19 -040076template <class ProcessorSmartPtr>
77class GrProcessorTestFactory : private SkNoncopyable {
joshualittb0a8a372014-09-23 09:50:21 -070078public:
Brian Salomon1c053642017-07-24 10:16:19 -040079 using MakeProc = ProcessorSmartPtr (*)(GrProcessorTestData*);
joshualittb0a8a372014-09-23 09:50:21 -070080
bungeman06ca8ec2016-06-09 08:01:03 -070081 GrProcessorTestFactory(MakeProc makeProc) {
82 fMakeProc = makeProc;
joshualittb0a8a372014-09-23 09:50:21 -070083 GetFactories()->push_back(this);
84 }
85
bsalomonb5b60322015-09-14 12:26:33 -070086 /** Pick a random factory function and create a processor. */
Brian Salomon1c053642017-07-24 10:16:19 -040087 static ProcessorSmartPtr Make(GrProcessorTestData* data) {
joshualitt9e87fa72014-10-09 13:12:35 -070088 VerifyFactoryCount();
Ben Wagnerf1344ac2019-05-10 15:01:53 -040089 if (GetFactories()->count() == 0) {
90 return nullptr;
91 }
joshualitt0067ff52015-07-08 14:26:19 -070092 uint32_t idx = data->fRandom->nextRangeU(0, GetFactories()->count() - 1);
bungeman06ca8ec2016-06-09 08:01:03 -070093 return MakeIdx(idx, data);
bsalomonb5b60322015-09-14 12:26:33 -070094 }
95
96 /** Number of registered factory functions */
97 static int Count() { return GetFactories()->count(); }
98
99 /** Use factory function at Index idx to create a processor. */
Brian Salomon94e0b112017-07-28 16:02:46 -0400100 static ProcessorSmartPtr MakeIdx(int idx, GrProcessorTestData* data) {
Ben Wagnerf1344ac2019-05-10 15:01:53 -0400101 SkASSERT(idx < GetFactories()->count());
Brian Salomon1c053642017-07-24 10:16:19 -0400102 GrProcessorTestFactory<ProcessorSmartPtr>* factory = (*GetFactories())[idx];
103 ProcessorSmartPtr processor = factory->fMakeProc(data);
Brian Salomona1633922017-01-09 11:46:10 -0500104 SkASSERT(processor);
105 return processor;
joshualittb0a8a372014-09-23 09:50:21 -0700106 }
107
Brian Salomona1633922017-01-09 11:46:10 -0500108private:
109 /**
joshualitt9e87fa72014-10-09 13:12:35 -0700110 * A test function which verifies the count of factories.
111 */
112 static void VerifyFactoryCount();
113
bungeman06ca8ec2016-06-09 08:01:03 -0700114 MakeProc fMakeProc;
joshualittb0a8a372014-09-23 09:50:21 -0700115
Brian Salomon1c053642017-07-24 10:16:19 -0400116 static SkTArray<GrProcessorTestFactory<ProcessorSmartPtr>*, true>* GetFactories();
joshualittb0a8a372014-09-23 09:50:21 -0700117};
118
Brian Salomonaff329b2017-08-11 09:40:37 -0400119using GrFragmentProcessorTestFactory = GrProcessorTestFactory<std::unique_ptr<GrFragmentProcessor>>;
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500120using GrGeometryProcessorTestFactory = GrProcessorTestFactory<GrGeometryProcessor*>;
Brian Salomon1c053642017-07-24 10:16:19 -0400121
Brian Salomona1633922017-01-09 11:46:10 -0500122class GrXPFactoryTestFactory : private SkNoncopyable {
123public:
124 using GetFn = const GrXPFactory*(GrProcessorTestData*);
125
126 GrXPFactoryTestFactory(GetFn* getProc) : fGetProc(getProc) { GetFactories()->push_back(this); }
127
128 static const GrXPFactory* Get(GrProcessorTestData* data) {
129 VerifyFactoryCount();
Ben Wagnerf1344ac2019-05-10 15:01:53 -0400130 if (GetFactories()->count() == 0) {
131 return nullptr;
132 }
Brian Salomona1633922017-01-09 11:46:10 -0500133 uint32_t idx = data->fRandom->nextRangeU(0, GetFactories()->count() - 1);
134 const GrXPFactory* xpf = (*GetFactories())[idx]->fGetProc(data);
135 SkASSERT(xpf);
136 return xpf;
137 }
138
139private:
140 static void VerifyFactoryCount();
141
142 GetFn* fGetProc;
143 static SkTArray<GrXPFactoryTestFactory*, true>* GetFactories();
144};
145
Ben Wagnerf1344ac2019-05-10 15:01:53 -0400146#if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
147
joshualittb0a8a372014-09-23 09:50:21 -0700148/** GrProcessor subclasses should insert this macro in their declaration to be included in the
149 * program generation unit test.
150 */
Brian Salomon1c053642017-07-24 10:16:19 -0400151#define GR_DECLARE_GEOMETRY_PROCESSOR_TEST \
152 static GrGeometryProcessorTestFactory gTestFactory SK_UNUSED; \
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500153 static GrGeometryProcessor* TestCreate(GrProcessorTestData*);
joshualittb0a8a372014-09-23 09:50:21 -0700154
Brian Salomon1c053642017-07-24 10:16:19 -0400155#define GR_DECLARE_FRAGMENT_PROCESSOR_TEST \
156 static GrFragmentProcessorTestFactory gTestFactory SK_UNUSED; \
Brian Salomonaff329b2017-08-11 09:40:37 -0400157 static std::unique_ptr<GrFragmentProcessor> TestCreate(GrProcessorTestData*);
joshualittb0a8a372014-09-23 09:50:21 -0700158
egdanielc2304142014-12-11 13:15:13 -0800159#define GR_DECLARE_XP_FACTORY_TEST \
Brian Salomona1633922017-01-09 11:46:10 -0500160 static GrXPFactoryTestFactory gTestFactory SK_UNUSED; \
Brian Salomon0c26a9d2017-07-06 10:09:38 -0400161 static const GrXPFactory* TestGet(GrProcessorTestData*);
egdanielc2304142014-12-11 13:15:13 -0800162
joshualittb0a8a372014-09-23 09:50:21 -0700163/** GrProcessor subclasses should insert this macro in their implementation file. They must then
164 * also implement this static function:
joshualitt0067ff52015-07-08 14:26:19 -0700165 * GrProcessor* TestCreate(GrProcessorTestData*);
joshualittb0a8a372014-09-23 09:50:21 -0700166 */
Brian Salomon1c053642017-07-24 10:16:19 -0400167#define GR_DEFINE_FRAGMENT_PROCESSOR_TEST(Effect) \
168 GrFragmentProcessorTestFactory Effect::gTestFactory(Effect::TestCreate)
Brian Salomon003312a2017-01-09 16:00:33 +0000169
Brian Salomon1c053642017-07-24 10:16:19 -0400170#define GR_DEFINE_GEOMETRY_PROCESSOR_TEST(Effect) \
171 GrGeometryProcessorTestFactory Effect::gTestFactory(Effect::TestCreate)
Brian Salomona1633922017-01-09 11:46:10 -0500172
173#define GR_DEFINE_XP_FACTORY_TEST(Factory) \
174 GrXPFactoryTestFactory Factory::gTestFactory(Factory::TestGet)
joshualittb0a8a372014-09-23 09:50:21 -0700175
176#else // !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
177
178// The unit test relies on static initializers. Just declare the TestCreate function so that
179// its definitions will compile.
180#define GR_DECLARE_FRAGMENT_PROCESSOR_TEST \
Brian Salomonaff329b2017-08-11 09:40:37 -0400181 static std::unique_ptr<GrFragmentProcessor> TestCreate(GrProcessorTestData*);
joshualittb0a8a372014-09-23 09:50:21 -0700182#define GR_DEFINE_FRAGMENT_PROCESSOR_TEST(X)
183
184// The unit test relies on static initializers. Just declare the TestCreate function so that
185// its definitions will compile.
186#define GR_DECLARE_GEOMETRY_PROCESSOR_TEST \
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500187 static GrGeometryProcessor* TestCreate(GrProcessorTestData*);
joshualittb0a8a372014-09-23 09:50:21 -0700188#define GR_DEFINE_GEOMETRY_PROCESSOR_TEST(X)
189
Brian Salomona1633922017-01-09 11:46:10 -0500190// The unit test relies on static initializers. Just declare the TestGet function so that
191// its definitions will compile.
192#define GR_DECLARE_XP_FACTORY_TEST \
Brian Salomon0c26a9d2017-07-06 10:09:38 -0400193 const GrXPFactory* TestGet(GrProcessorTestData*);
Brian Salomona1633922017-01-09 11:46:10 -0500194#define GR_DEFINE_XP_FACTORY_TEST(X)
195
Hal Canary6f6961e2017-01-31 13:50:44 -0500196#endif // !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
197#else // GR_TEST_UTILS
198 #define GR_DECLARE_GEOMETRY_PROCESSOR_TEST
199 #define GR_DECLARE_FRAGMENT_PROCESSOR_TEST
200 #define GR_DECLARE_XP_FACTORY_TEST
201 #define GR_DEFINE_FRAGMENT_PROCESSOR_TEST(...)
202 #define GR_DEFINE_GEOMETRY_PROCESSOR_TEST(...)
203 #define GR_DEFINE_XP_FACTORY_TEST(...)
204 #define GR_DECLARE_FRAGMENT_PROCESSOR_TEST
205 #define GR_DEFINE_FRAGMENT_PROCESSOR_TEST(...)
206 #define GR_DECLARE_GEOMETRY_PROCESSOR_TEST
207 #define GR_DEFINE_GEOMETRY_PROCESSOR_TEST(...)
208 #define GR_DECLARE_XP_FACTORY_TEST
209 #define GR_DEFINE_XP_FACTORY_TEST(...)
210#endif // GR_TEST_UTILS
211#endif // GrProcessorUnitTest_DEFINED