blob: d10f5c1a2113c96680df7c679cf3999ba2a319d3 [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
20class SkMatrix;
bsalomon4b91f762015-05-19 09:29:46 -070021class GrCaps;
joshualitt0067ff52015-07-08 14:26:19 -070022class GrContext;
Robert Phillips1afd4cd2018-01-08 13:40:32 -050023class GrProxyProvider;
Brian Osman11052242016-10-27 14:47:55 -040024class GrRenderTargetContext;
bsalomon506c8022015-09-14 13:16:26 -070025struct 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
joshualittb0a8a372014-09-23 09:50:21 -070032// Used to access the dummy textures in TestCreate procs.
33enum {
34 kSkiaPMTextureIdx = 0,
35 kAlphaTextureIdx = 1,
36};
37
bsalomon506c8022015-09-14 13:16:26 -070038/** This allows parent FPs to implement a test create with known leaf children in order to avoid
39creating an unbounded FP tree which may overflow various shader limits. */
Brian Salomonaff329b2017-08-11 09:40:37 -040040std::unique_ptr<GrFragmentProcessor> MakeChildFP(GrProcessorTestData*);
bsalomon506c8022015-09-14 13:16:26 -070041
joshualitt2e3b3e32014-12-09 13:31:14 -080042}
43
joshualitt0067ff52015-07-08 14:26:19 -070044/*
45 * GrProcessorTestData is an argument struct to TestCreate functions
46 * fTextures are valid textures that can optionally be used to construct
Brian Salomon0bbecb22016-11-17 11:38:22 -050047 * TextureSampler. The first texture has config kSkia8888_GrPixelConfig and the second has
joshualitt0067ff52015-07-08 14:26:19 -070048 * kAlpha_8_GrPixelConfig. TestCreate functions are also free to create additional textures using
49 * the GrContext.
50 */
51struct GrProcessorTestData {
52 GrProcessorTestData(SkRandom* random,
53 GrContext* context,
Brian Osman11052242016-10-27 14:47:55 -040054 const GrRenderTargetContext* renderTargetContext,
Brian Salomonfc118442019-11-22 19:09:27 -050055 sk_sp<GrTextureProxy> proxies[2])
56 : fRandom(random), fRenderTargetContext(renderTargetContext), fContext(context) {
Robert Phillipsbb581ce2017-05-29 15:05:15 -040057 SkASSERT(proxies[0] && proxies[1]);
Robert Phillipse78b7252017-04-06 07:59:41 -040058 fProxies[0] = proxies[0];
59 fProxies[1] = proxies[1];
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050060
61 fArena = std::unique_ptr<SkArenaAlloc>(new SkArenaAlloc(1000));
joshualitt0067ff52015-07-08 14:26:19 -070062 }
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050063
joshualitt0067ff52015-07-08 14:26:19 -070064 SkRandom* fRandom;
Brian Osman11052242016-10-27 14:47:55 -040065 const GrRenderTargetContext* fRenderTargetContext;
Robert Phillips757914d2017-01-25 15:48:30 -050066
67 GrContext* context() { return fContext; }
Robert Phillips296b1cc2017-03-15 10:42:12 -040068 GrResourceProvider* resourceProvider();
Robert Phillips1afd4cd2018-01-08 13:40:32 -050069 GrProxyProvider* proxyProvider();
Robert Phillips296b1cc2017-03-15 10:42:12 -040070 const GrCaps* caps();
Robert Phillips63c67462017-02-15 14:19:01 -050071 sk_sp<GrTextureProxy> textureProxy(int index) { return fProxies[index]; }
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050072 SkArenaAlloc* allocator() { return fArena.get(); }
Robert Phillips757914d2017-01-25 15:48:30 -050073
74private:
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -050075 GrContext* fContext;
Robert Phillips63c67462017-02-15 14:19:01 -050076 sk_sp<GrTextureProxy> fProxies[2];
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050077
78 std::unique_ptr<SkArenaAlloc> fArena;
joshualitt0067ff52015-07-08 14:26:19 -070079};
80
joshualittb0a8a372014-09-23 09:50:21 -070081class GrProcessor;
82class GrTexture;
83
Brian Salomon1c053642017-07-24 10:16:19 -040084template <class ProcessorSmartPtr>
85class GrProcessorTestFactory : private SkNoncopyable {
joshualittb0a8a372014-09-23 09:50:21 -070086public:
Brian Salomon1c053642017-07-24 10:16:19 -040087 using MakeProc = ProcessorSmartPtr (*)(GrProcessorTestData*);
joshualittb0a8a372014-09-23 09:50:21 -070088
bungeman06ca8ec2016-06-09 08:01:03 -070089 GrProcessorTestFactory(MakeProc makeProc) {
90 fMakeProc = makeProc;
joshualittb0a8a372014-09-23 09:50:21 -070091 GetFactories()->push_back(this);
92 }
93
bsalomonb5b60322015-09-14 12:26:33 -070094 /** Pick a random factory function and create a processor. */
Brian Salomon1c053642017-07-24 10:16:19 -040095 static ProcessorSmartPtr Make(GrProcessorTestData* data) {
joshualitt9e87fa72014-10-09 13:12:35 -070096 VerifyFactoryCount();
Ben Wagnerf1344ac2019-05-10 15:01:53 -040097 if (GetFactories()->count() == 0) {
98 return nullptr;
99 }
joshualitt0067ff52015-07-08 14:26:19 -0700100 uint32_t idx = data->fRandom->nextRangeU(0, GetFactories()->count() - 1);
bungeman06ca8ec2016-06-09 08:01:03 -0700101 return MakeIdx(idx, data);
bsalomonb5b60322015-09-14 12:26:33 -0700102 }
103
104 /** Number of registered factory functions */
105 static int Count() { return GetFactories()->count(); }
106
107 /** Use factory function at Index idx to create a processor. */
Brian Salomon94e0b112017-07-28 16:02:46 -0400108 static ProcessorSmartPtr MakeIdx(int idx, GrProcessorTestData* data) {
Ben Wagnerf1344ac2019-05-10 15:01:53 -0400109 SkASSERT(idx < GetFactories()->count());
Brian Salomon1c053642017-07-24 10:16:19 -0400110 GrProcessorTestFactory<ProcessorSmartPtr>* factory = (*GetFactories())[idx];
111 ProcessorSmartPtr processor = factory->fMakeProc(data);
Brian Salomona1633922017-01-09 11:46:10 -0500112 SkASSERT(processor);
113 return processor;
joshualittb0a8a372014-09-23 09:50:21 -0700114 }
115
Brian Salomona1633922017-01-09 11:46:10 -0500116private:
117 /**
joshualitt9e87fa72014-10-09 13:12:35 -0700118 * A test function which verifies the count of factories.
119 */
120 static void VerifyFactoryCount();
121
bungeman06ca8ec2016-06-09 08:01:03 -0700122 MakeProc fMakeProc;
joshualittb0a8a372014-09-23 09:50:21 -0700123
Brian Salomon1c053642017-07-24 10:16:19 -0400124 static SkTArray<GrProcessorTestFactory<ProcessorSmartPtr>*, true>* GetFactories();
joshualittb0a8a372014-09-23 09:50:21 -0700125};
126
Brian Salomonaff329b2017-08-11 09:40:37 -0400127using GrFragmentProcessorTestFactory = GrProcessorTestFactory<std::unique_ptr<GrFragmentProcessor>>;
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500128using GrGeometryProcessorTestFactory = GrProcessorTestFactory<GrGeometryProcessor*>;
Brian Salomon1c053642017-07-24 10:16:19 -0400129
Brian Salomona1633922017-01-09 11:46:10 -0500130class GrXPFactoryTestFactory : private SkNoncopyable {
131public:
132 using GetFn = const GrXPFactory*(GrProcessorTestData*);
133
134 GrXPFactoryTestFactory(GetFn* getProc) : fGetProc(getProc) { GetFactories()->push_back(this); }
135
136 static const GrXPFactory* Get(GrProcessorTestData* data) {
137 VerifyFactoryCount();
Ben Wagnerf1344ac2019-05-10 15:01:53 -0400138 if (GetFactories()->count() == 0) {
139 return nullptr;
140 }
Brian Salomona1633922017-01-09 11:46:10 -0500141 uint32_t idx = data->fRandom->nextRangeU(0, GetFactories()->count() - 1);
142 const GrXPFactory* xpf = (*GetFactories())[idx]->fGetProc(data);
143 SkASSERT(xpf);
144 return xpf;
145 }
146
147private:
148 static void VerifyFactoryCount();
149
150 GetFn* fGetProc;
151 static SkTArray<GrXPFactoryTestFactory*, true>* GetFactories();
152};
153
Ben Wagnerf1344ac2019-05-10 15:01:53 -0400154#if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
155
joshualittb0a8a372014-09-23 09:50:21 -0700156/** GrProcessor subclasses should insert this macro in their declaration to be included in the
157 * program generation unit test.
158 */
Brian Salomon1c053642017-07-24 10:16:19 -0400159#define GR_DECLARE_GEOMETRY_PROCESSOR_TEST \
160 static GrGeometryProcessorTestFactory gTestFactory SK_UNUSED; \
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500161 static GrGeometryProcessor* TestCreate(GrProcessorTestData*);
joshualittb0a8a372014-09-23 09:50:21 -0700162
Brian Salomon1c053642017-07-24 10:16:19 -0400163#define GR_DECLARE_FRAGMENT_PROCESSOR_TEST \
164 static GrFragmentProcessorTestFactory gTestFactory SK_UNUSED; \
Brian Salomonaff329b2017-08-11 09:40:37 -0400165 static std::unique_ptr<GrFragmentProcessor> TestCreate(GrProcessorTestData*);
joshualittb0a8a372014-09-23 09:50:21 -0700166
egdanielc2304142014-12-11 13:15:13 -0800167#define GR_DECLARE_XP_FACTORY_TEST \
Brian Salomona1633922017-01-09 11:46:10 -0500168 static GrXPFactoryTestFactory gTestFactory SK_UNUSED; \
Brian Salomon0c26a9d2017-07-06 10:09:38 -0400169 static const GrXPFactory* TestGet(GrProcessorTestData*);
egdanielc2304142014-12-11 13:15:13 -0800170
joshualittb0a8a372014-09-23 09:50:21 -0700171/** GrProcessor subclasses should insert this macro in their implementation file. They must then
172 * also implement this static function:
joshualitt0067ff52015-07-08 14:26:19 -0700173 * GrProcessor* TestCreate(GrProcessorTestData*);
joshualittb0a8a372014-09-23 09:50:21 -0700174 */
Brian Salomon1c053642017-07-24 10:16:19 -0400175#define GR_DEFINE_FRAGMENT_PROCESSOR_TEST(Effect) \
176 GrFragmentProcessorTestFactory Effect::gTestFactory(Effect::TestCreate)
Brian Salomon003312a2017-01-09 16:00:33 +0000177
Brian Salomon1c053642017-07-24 10:16:19 -0400178#define GR_DEFINE_GEOMETRY_PROCESSOR_TEST(Effect) \
179 GrGeometryProcessorTestFactory Effect::gTestFactory(Effect::TestCreate)
Brian Salomona1633922017-01-09 11:46:10 -0500180
181#define GR_DEFINE_XP_FACTORY_TEST(Factory) \
182 GrXPFactoryTestFactory Factory::gTestFactory(Factory::TestGet)
joshualittb0a8a372014-09-23 09:50:21 -0700183
184#else // !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
185
186// The unit test relies on static initializers. Just declare the TestCreate function so that
187// its definitions will compile.
188#define GR_DECLARE_FRAGMENT_PROCESSOR_TEST \
Brian Salomonaff329b2017-08-11 09:40:37 -0400189 static std::unique_ptr<GrFragmentProcessor> TestCreate(GrProcessorTestData*);
joshualittb0a8a372014-09-23 09:50:21 -0700190#define GR_DEFINE_FRAGMENT_PROCESSOR_TEST(X)
191
192// The unit test relies on static initializers. Just declare the TestCreate function so that
193// its definitions will compile.
194#define GR_DECLARE_GEOMETRY_PROCESSOR_TEST \
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500195 static GrGeometryProcessor* TestCreate(GrProcessorTestData*);
joshualittb0a8a372014-09-23 09:50:21 -0700196#define GR_DEFINE_GEOMETRY_PROCESSOR_TEST(X)
197
Brian Salomona1633922017-01-09 11:46:10 -0500198// The unit test relies on static initializers. Just declare the TestGet function so that
199// its definitions will compile.
200#define GR_DECLARE_XP_FACTORY_TEST \
Brian Salomon0c26a9d2017-07-06 10:09:38 -0400201 const GrXPFactory* TestGet(GrProcessorTestData*);
Brian Salomona1633922017-01-09 11:46:10 -0500202#define GR_DEFINE_XP_FACTORY_TEST(X)
203
Hal Canary6f6961e2017-01-31 13:50:44 -0500204#endif // !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
205#else // GR_TEST_UTILS
206 #define GR_DECLARE_GEOMETRY_PROCESSOR_TEST
207 #define GR_DECLARE_FRAGMENT_PROCESSOR_TEST
208 #define GR_DECLARE_XP_FACTORY_TEST
209 #define GR_DEFINE_FRAGMENT_PROCESSOR_TEST(...)
210 #define GR_DEFINE_GEOMETRY_PROCESSOR_TEST(...)
211 #define GR_DEFINE_XP_FACTORY_TEST(...)
212 #define GR_DECLARE_FRAGMENT_PROCESSOR_TEST
213 #define GR_DEFINE_FRAGMENT_PROCESSOR_TEST(...)
214 #define GR_DECLARE_GEOMETRY_PROCESSOR_TEST
215 #define GR_DEFINE_GEOMETRY_PROCESSOR_TEST(...)
216 #define GR_DECLARE_XP_FACTORY_TEST
217 #define GR_DEFINE_XP_FACTORY_TEST(...)
218#endif // GR_TEST_UTILS
219#endif // GrProcessorUnitTest_DEFINED