blob: 731080e55ef52a55894c7e93c353059363e896ca [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
Hal Canary6f6961e2017-01-31 13:50:44 -050011#include "SkTypes.h"
12
13#if GR_TEST_UTILS
14
Robert Phillips757914d2017-01-25 15:48:30 -050015#include "../private/GrTextureProxy.h"
bungemanbf521ff2016-02-17 13:13:44 -080016#include "../private/SkTArray.h"
joshualitt4eaf9ce2015-04-28 13:31:18 -070017#include "GrTestUtils.h"
joshualittb0a8a372014-09-23 09:50:21 -070018
19class SkMatrix;
bsalomon4b91f762015-05-19 09:29:46 -070020class GrCaps;
joshualitt0067ff52015-07-08 14:26:19 -070021class GrContext;
Robert Phillips1afd4cd2018-01-08 13:40:32 -050022class GrProxyProvider;
Brian Osman11052242016-10-27 14:47:55 -040023class GrRenderTargetContext;
bsalomon506c8022015-09-14 13:16:26 -070024struct GrProcessorTestData;
Mike Reedd4706732016-11-15 16:44:34 -050025class GrTexture;
Brian Salomona1633922017-01-09 11:46:10 -050026class GrXPFactory;
Brian Salomon1c053642017-07-24 10:16:19 -040027class GrGeometryProcessor;
joshualittb0a8a372014-09-23 09:50:21 -070028
29namespace GrProcessorUnitTest {
bsalomon506c8022015-09-14 13:16:26 -070030
joshualittb0a8a372014-09-23 09:50:21 -070031// Used to access the dummy textures in TestCreate procs.
32enum {
33 kSkiaPMTextureIdx = 0,
34 kAlphaTextureIdx = 1,
35};
36
bsalomon506c8022015-09-14 13:16:26 -070037/** This allows parent FPs to implement a test create with known leaf children in order to avoid
38creating an unbounded FP tree which may overflow various shader limits. */
Brian Salomonaff329b2017-08-11 09:40:37 -040039std::unique_ptr<GrFragmentProcessor> MakeChildFP(GrProcessorTestData*);
bsalomon506c8022015-09-14 13:16:26 -070040
joshualitt2e3b3e32014-12-09 13:31:14 -080041}
42
joshualitt0067ff52015-07-08 14:26:19 -070043/*
44 * GrProcessorTestData is an argument struct to TestCreate functions
45 * fTextures are valid textures that can optionally be used to construct
Brian Salomon0bbecb22016-11-17 11:38:22 -050046 * TextureSampler. The first texture has config kSkia8888_GrPixelConfig and the second has
joshualitt0067ff52015-07-08 14:26:19 -070047 * kAlpha_8_GrPixelConfig. TestCreate functions are also free to create additional textures using
48 * the GrContext.
49 */
50struct GrProcessorTestData {
51 GrProcessorTestData(SkRandom* random,
52 GrContext* context,
Brian Osman11052242016-10-27 14:47:55 -040053 const GrRenderTargetContext* renderTargetContext,
Robert Phillipse78b7252017-04-06 07:59:41 -040054 sk_sp<GrTextureProxy> proxies[2])
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -050055 : fRandom(random)
56 , fRenderTargetContext(renderTargetContext)
57 , fContext(context) {
Robert Phillipsbb581ce2017-05-29 15:05:15 -040058 SkASSERT(proxies[0] && proxies[1]);
Robert Phillipse78b7252017-04-06 07:59:41 -040059 fProxies[0] = proxies[0];
60 fProxies[1] = proxies[1];
joshualitt0067ff52015-07-08 14:26:19 -070061 }
62 SkRandom* fRandom;
Brian Osman11052242016-10-27 14:47:55 -040063 const GrRenderTargetContext* fRenderTargetContext;
Robert Phillips757914d2017-01-25 15:48:30 -050064
65 GrContext* context() { return fContext; }
Robert Phillips296b1cc2017-03-15 10:42:12 -040066 GrResourceProvider* resourceProvider();
Robert Phillips1afd4cd2018-01-08 13:40:32 -050067 GrProxyProvider* proxyProvider();
Robert Phillips296b1cc2017-03-15 10:42:12 -040068 const GrCaps* caps();
Robert Phillips63c67462017-02-15 14:19:01 -050069 sk_sp<GrTextureProxy> textureProxy(int index) { return fProxies[index]; }
Robert Phillips757914d2017-01-25 15:48:30 -050070
71private:
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -050072 GrContext* fContext;
Robert Phillips63c67462017-02-15 14:19:01 -050073 sk_sp<GrTextureProxy> fProxies[2];
joshualitt0067ff52015-07-08 14:26:19 -070074};
75
joshualittb0a8a372014-09-23 09:50:21 -070076#if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
77
joshualittb0a8a372014-09-23 09:50:21 -070078class GrProcessor;
79class GrTexture;
80
Brian Salomon1c053642017-07-24 10:16:19 -040081template <class ProcessorSmartPtr>
82class GrProcessorTestFactory : private SkNoncopyable {
joshualittb0a8a372014-09-23 09:50:21 -070083public:
Brian Salomon1c053642017-07-24 10:16:19 -040084 using Processor = typename ProcessorSmartPtr::element_type;
85 using MakeProc = ProcessorSmartPtr (*)(GrProcessorTestData*);
joshualittb0a8a372014-09-23 09:50:21 -070086
bungeman06ca8ec2016-06-09 08:01:03 -070087 GrProcessorTestFactory(MakeProc makeProc) {
88 fMakeProc = makeProc;
joshualittb0a8a372014-09-23 09:50:21 -070089 GetFactories()->push_back(this);
90 }
91
bsalomonb5b60322015-09-14 12:26:33 -070092 /** Pick a random factory function and create a processor. */
Brian Salomon1c053642017-07-24 10:16:19 -040093 static ProcessorSmartPtr Make(GrProcessorTestData* data) {
joshualitt9e87fa72014-10-09 13:12:35 -070094 VerifyFactoryCount();
95 SkASSERT(GetFactories()->count());
joshualitt0067ff52015-07-08 14:26:19 -070096 uint32_t idx = data->fRandom->nextRangeU(0, GetFactories()->count() - 1);
bungeman06ca8ec2016-06-09 08:01:03 -070097 return MakeIdx(idx, data);
bsalomonb5b60322015-09-14 12:26:33 -070098 }
99
100 /** Number of registered factory functions */
101 static int Count() { return GetFactories()->count(); }
102
103 /** Use factory function at Index idx to create a processor. */
Brian Salomon94e0b112017-07-28 16:02:46 -0400104 static ProcessorSmartPtr MakeIdx(int idx, GrProcessorTestData* data) {
Brian Salomon1c053642017-07-24 10:16:19 -0400105 GrProcessorTestFactory<ProcessorSmartPtr>* factory = (*GetFactories())[idx];
106 ProcessorSmartPtr processor = factory->fMakeProc(data);
Brian Salomona1633922017-01-09 11:46:10 -0500107 SkASSERT(processor);
108 return processor;
joshualittb0a8a372014-09-23 09:50:21 -0700109 }
110
Brian Salomona1633922017-01-09 11:46:10 -0500111private:
112 /**
joshualitt9e87fa72014-10-09 13:12:35 -0700113 * A test function which verifies the count of factories.
114 */
115 static void VerifyFactoryCount();
116
bungeman06ca8ec2016-06-09 08:01:03 -0700117 MakeProc fMakeProc;
joshualittb0a8a372014-09-23 09:50:21 -0700118
Brian Salomon1c053642017-07-24 10:16:19 -0400119 static SkTArray<GrProcessorTestFactory<ProcessorSmartPtr>*, true>* GetFactories();
joshualittb0a8a372014-09-23 09:50:21 -0700120};
121
Brian Salomonaff329b2017-08-11 09:40:37 -0400122using GrFragmentProcessorTestFactory = GrProcessorTestFactory<std::unique_ptr<GrFragmentProcessor>>;
Brian Salomon1c053642017-07-24 10:16:19 -0400123using GrGeometryProcessorTestFactory = GrProcessorTestFactory<sk_sp<GrGeometryProcessor>>;
124
Brian Salomona1633922017-01-09 11:46:10 -0500125class GrXPFactoryTestFactory : private SkNoncopyable {
126public:
127 using GetFn = const GrXPFactory*(GrProcessorTestData*);
128
129 GrXPFactoryTestFactory(GetFn* getProc) : fGetProc(getProc) { GetFactories()->push_back(this); }
130
131 static const GrXPFactory* Get(GrProcessorTestData* data) {
132 VerifyFactoryCount();
133 SkASSERT(GetFactories()->count());
134 uint32_t idx = data->fRandom->nextRangeU(0, GetFactories()->count() - 1);
135 const GrXPFactory* xpf = (*GetFactories())[idx]->fGetProc(data);
136 SkASSERT(xpf);
137 return xpf;
138 }
139
140private:
141 static void VerifyFactoryCount();
142
143 GetFn* fGetProc;
144 static SkTArray<GrXPFactoryTestFactory*, true>* GetFactories();
145};
146
joshualittb0a8a372014-09-23 09:50:21 -0700147/** GrProcessor subclasses should insert this macro in their declaration to be included in the
148 * program generation unit test.
149 */
Brian Salomon1c053642017-07-24 10:16:19 -0400150#define GR_DECLARE_GEOMETRY_PROCESSOR_TEST \
151 static GrGeometryProcessorTestFactory gTestFactory SK_UNUSED; \
Brian Salomon0c26a9d2017-07-06 10:09:38 -0400152 static sk_sp<GrGeometryProcessor> TestCreate(GrProcessorTestData*);
joshualittb0a8a372014-09-23 09:50:21 -0700153
Brian Salomon1c053642017-07-24 10:16:19 -0400154#define GR_DECLARE_FRAGMENT_PROCESSOR_TEST \
155 static GrFragmentProcessorTestFactory gTestFactory SK_UNUSED; \
Brian Salomonaff329b2017-08-11 09:40:37 -0400156 static std::unique_ptr<GrFragmentProcessor> TestCreate(GrProcessorTestData*);
joshualittb0a8a372014-09-23 09:50:21 -0700157
egdanielc2304142014-12-11 13:15:13 -0800158#define GR_DECLARE_XP_FACTORY_TEST \
Brian Salomona1633922017-01-09 11:46:10 -0500159 static GrXPFactoryTestFactory gTestFactory SK_UNUSED; \
Brian Salomon0c26a9d2017-07-06 10:09:38 -0400160 static const GrXPFactory* TestGet(GrProcessorTestData*);
egdanielc2304142014-12-11 13:15:13 -0800161
joshualittb0a8a372014-09-23 09:50:21 -0700162/** GrProcessor subclasses should insert this macro in their implementation file. They must then
163 * also implement this static function:
joshualitt0067ff52015-07-08 14:26:19 -0700164 * GrProcessor* TestCreate(GrProcessorTestData*);
joshualittb0a8a372014-09-23 09:50:21 -0700165 */
Brian Salomon1c053642017-07-24 10:16:19 -0400166#define GR_DEFINE_FRAGMENT_PROCESSOR_TEST(Effect) \
167 GrFragmentProcessorTestFactory Effect::gTestFactory(Effect::TestCreate)
Brian Salomon003312a2017-01-09 16:00:33 +0000168
Brian Salomon1c053642017-07-24 10:16:19 -0400169#define GR_DEFINE_GEOMETRY_PROCESSOR_TEST(Effect) \
170 GrGeometryProcessorTestFactory Effect::gTestFactory(Effect::TestCreate)
Brian Salomona1633922017-01-09 11:46:10 -0500171
172#define GR_DEFINE_XP_FACTORY_TEST(Factory) \
173 GrXPFactoryTestFactory Factory::gTestFactory(Factory::TestGet)
joshualittb0a8a372014-09-23 09:50:21 -0700174
175#else // !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
176
177// The unit test relies on static initializers. Just declare the TestCreate function so that
178// its definitions will compile.
179#define GR_DECLARE_FRAGMENT_PROCESSOR_TEST \
Brian Salomonaff329b2017-08-11 09:40:37 -0400180 static std::unique_ptr<GrFragmentProcessor> TestCreate(GrProcessorTestData*);
joshualittb0a8a372014-09-23 09:50:21 -0700181#define GR_DEFINE_FRAGMENT_PROCESSOR_TEST(X)
182
183// The unit test relies on static initializers. Just declare the TestCreate function so that
184// its definitions will compile.
185#define GR_DECLARE_GEOMETRY_PROCESSOR_TEST \
Brian Salomon0c26a9d2017-07-06 10:09:38 -0400186 static sk_sp<GrGeometryProcessor> TestCreate(GrProcessorTestData*);
joshualittb0a8a372014-09-23 09:50:21 -0700187#define GR_DEFINE_GEOMETRY_PROCESSOR_TEST(X)
188
Brian Salomona1633922017-01-09 11:46:10 -0500189// The unit test relies on static initializers. Just declare the TestGet function so that
190// its definitions will compile.
191#define GR_DECLARE_XP_FACTORY_TEST \
Brian Salomon0c26a9d2017-07-06 10:09:38 -0400192 const GrXPFactory* TestGet(GrProcessorTestData*);
Brian Salomona1633922017-01-09 11:46:10 -0500193#define GR_DEFINE_XP_FACTORY_TEST(X)
194
Hal Canary6f6961e2017-01-31 13:50:44 -0500195#endif // !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
196#else // GR_TEST_UTILS
197 #define GR_DECLARE_GEOMETRY_PROCESSOR_TEST
198 #define GR_DECLARE_FRAGMENT_PROCESSOR_TEST
199 #define GR_DECLARE_XP_FACTORY_TEST
200 #define GR_DEFINE_FRAGMENT_PROCESSOR_TEST(...)
201 #define GR_DEFINE_GEOMETRY_PROCESSOR_TEST(...)
202 #define GR_DEFINE_XP_FACTORY_TEST(...)
203 #define GR_DECLARE_FRAGMENT_PROCESSOR_TEST
204 #define GR_DEFINE_FRAGMENT_PROCESSOR_TEST(...)
205 #define GR_DECLARE_GEOMETRY_PROCESSOR_TEST
206 #define GR_DEFINE_GEOMETRY_PROCESSOR_TEST(...)
207 #define GR_DECLARE_XP_FACTORY_TEST
208 #define GR_DEFINE_XP_FACTORY_TEST(...)
209#endif // GR_TEST_UTILS
210#endif // GrProcessorUnitTest_DEFINED