blob: d6269c85bd599d8e96855a6250610f0da09dc46d [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;
Brian Osman11052242016-10-27 14:47:55 -040022class GrRenderTargetContext;
bsalomon506c8022015-09-14 13:16:26 -070023struct GrProcessorTestData;
Mike Reedd4706732016-11-15 16:44:34 -050024class GrTexture;
Brian Salomona1633922017-01-09 11:46:10 -050025class GrXPFactory;
joshualittb0a8a372014-09-23 09:50:21 -070026
27namespace GrProcessorUnitTest {
bsalomon506c8022015-09-14 13:16:26 -070028
joshualittb0a8a372014-09-23 09:50:21 -070029// Used to access the dummy textures in TestCreate procs.
30enum {
31 kSkiaPMTextureIdx = 0,
32 kAlphaTextureIdx = 1,
33};
34
bsalomon506c8022015-09-14 13:16:26 -070035/** This allows parent FPs to implement a test create with known leaf children in order to avoid
36creating an unbounded FP tree which may overflow various shader limits. */
bungeman06ca8ec2016-06-09 08:01:03 -070037sk_sp<GrFragmentProcessor> MakeChildFP(GrProcessorTestData*);
bsalomon506c8022015-09-14 13:16:26 -070038
joshualitt2e3b3e32014-12-09 13:31:14 -080039}
40
joshualitt0067ff52015-07-08 14:26:19 -070041/*
42 * GrProcessorTestData is an argument struct to TestCreate functions
43 * fTextures are valid textures that can optionally be used to construct
Brian Salomon0bbecb22016-11-17 11:38:22 -050044 * TextureSampler. The first texture has config kSkia8888_GrPixelConfig and the second has
joshualitt0067ff52015-07-08 14:26:19 -070045 * kAlpha_8_GrPixelConfig. TestCreate functions are also free to create additional textures using
46 * the GrContext.
47 */
48struct GrProcessorTestData {
49 GrProcessorTestData(SkRandom* random,
50 GrContext* context,
Brian Osman11052242016-10-27 14:47:55 -040051 const GrRenderTargetContext* renderTargetContext,
Robert Phillipse78b7252017-04-06 07:59:41 -040052 sk_sp<GrTextureProxy> proxies[2])
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -050053 : fRandom(random)
54 , fRenderTargetContext(renderTargetContext)
55 , fContext(context) {
Robert Phillipse78b7252017-04-06 07:59:41 -040056 fProxies[0] = proxies[0];
57 fProxies[1] = proxies[1];
joshualitt0067ff52015-07-08 14:26:19 -070058 }
59 SkRandom* fRandom;
Brian Osman11052242016-10-27 14:47:55 -040060 const GrRenderTargetContext* fRenderTargetContext;
Robert Phillips757914d2017-01-25 15:48:30 -050061
62 GrContext* context() { return fContext; }
Robert Phillips296b1cc2017-03-15 10:42:12 -040063 GrResourceProvider* resourceProvider();
64 const GrCaps* caps();
Robert Phillips63c67462017-02-15 14:19:01 -050065 sk_sp<GrTextureProxy> textureProxy(int index) { return fProxies[index]; }
Robert Phillips757914d2017-01-25 15:48:30 -050066
67private:
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -050068 GrContext* fContext;
Robert Phillips63c67462017-02-15 14:19:01 -050069 sk_sp<GrTextureProxy> fProxies[2];
joshualitt0067ff52015-07-08 14:26:19 -070070};
71
joshualittb0a8a372014-09-23 09:50:21 -070072#if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
73
joshualittb0a8a372014-09-23 09:50:21 -070074class GrProcessor;
75class GrTexture;
76
Brian Salomona1633922017-01-09 11:46:10 -050077template <class Processor> class GrProcessorTestFactory : private SkNoncopyable {
joshualittb0a8a372014-09-23 09:50:21 -070078public:
bungeman06ca8ec2016-06-09 08:01:03 -070079 typedef sk_sp<Processor> (*MakeProc)(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. */
bungeman06ca8ec2016-06-09 08:01:03 -070087 static sk_sp<Processor> Make(GrProcessorTestData* data) {
joshualitt9e87fa72014-10-09 13:12:35 -070088 VerifyFactoryCount();
89 SkASSERT(GetFactories()->count());
joshualitt0067ff52015-07-08 14:26:19 -070090 uint32_t idx = data->fRandom->nextRangeU(0, GetFactories()->count() - 1);
bungeman06ca8ec2016-06-09 08:01:03 -070091 return MakeIdx(idx, data);
bsalomonb5b60322015-09-14 12:26:33 -070092 }
93
94 /** Number of registered factory functions */
95 static int Count() { return GetFactories()->count(); }
96
97 /** Use factory function at Index idx to create a processor. */
bungeman06ca8ec2016-06-09 08:01:03 -070098 static sk_sp<Processor> MakeIdx(int idx, GrProcessorTestData* data) {
joshualittb0a8a372014-09-23 09:50:21 -070099 GrProcessorTestFactory<Processor>* factory = (*GetFactories())[idx];
Brian Salomona1633922017-01-09 11:46:10 -0500100 sk_sp<Processor> processor = factory->fMakeProc(data);
101 SkASSERT(processor);
102 return processor;
joshualittb0a8a372014-09-23 09:50:21 -0700103 }
104
Brian Salomona1633922017-01-09 11:46:10 -0500105private:
106 /**
joshualitt9e87fa72014-10-09 13:12:35 -0700107 * A test function which verifies the count of factories.
108 */
109 static void VerifyFactoryCount();
110
bungeman06ca8ec2016-06-09 08:01:03 -0700111 MakeProc fMakeProc;
joshualittb0a8a372014-09-23 09:50:21 -0700112
joshualitt9e87fa72014-10-09 13:12:35 -0700113 static SkTArray<GrProcessorTestFactory<Processor>*, true>* GetFactories();
joshualittb0a8a372014-09-23 09:50:21 -0700114};
115
Brian Salomona1633922017-01-09 11:46:10 -0500116class GrXPFactoryTestFactory : private SkNoncopyable {
117public:
118 using GetFn = const GrXPFactory*(GrProcessorTestData*);
119
120 GrXPFactoryTestFactory(GetFn* getProc) : fGetProc(getProc) { GetFactories()->push_back(this); }
121
122 static const GrXPFactory* Get(GrProcessorTestData* data) {
123 VerifyFactoryCount();
124 SkASSERT(GetFactories()->count());
125 uint32_t idx = data->fRandom->nextRangeU(0, GetFactories()->count() - 1);
126 const GrXPFactory* xpf = (*GetFactories())[idx]->fGetProc(data);
127 SkASSERT(xpf);
128 return xpf;
129 }
130
131private:
132 static void VerifyFactoryCount();
133
134 GetFn* fGetProc;
135 static SkTArray<GrXPFactoryTestFactory*, true>* GetFactories();
136};
137
joshualittb0a8a372014-09-23 09:50:21 -0700138/** GrProcessor subclasses should insert this macro in their declaration to be included in the
139 * program generation unit test.
140 */
joshualittb0a8a372014-09-23 09:50:21 -0700141#define GR_DECLARE_GEOMETRY_PROCESSOR_TEST \
joshualitt9e87fa72014-10-09 13:12:35 -0700142 static GrProcessorTestFactory<GrGeometryProcessor> gTestFactory SK_UNUSED; \
bungeman06ca8ec2016-06-09 08:01:03 -0700143 static sk_sp<GrGeometryProcessor> TestCreate(GrProcessorTestData*)
joshualittb0a8a372014-09-23 09:50:21 -0700144
145#define GR_DECLARE_FRAGMENT_PROCESSOR_TEST \
joshualitt9e87fa72014-10-09 13:12:35 -0700146 static GrProcessorTestFactory<GrFragmentProcessor> gTestFactory SK_UNUSED; \
bungeman06ca8ec2016-06-09 08:01:03 -0700147 static sk_sp<GrFragmentProcessor> TestCreate(GrProcessorTestData*)
joshualittb0a8a372014-09-23 09:50:21 -0700148
egdanielc2304142014-12-11 13:15:13 -0800149#define GR_DECLARE_XP_FACTORY_TEST \
Brian Salomona1633922017-01-09 11:46:10 -0500150 static GrXPFactoryTestFactory gTestFactory SK_UNUSED; \
151 static const GrXPFactory* TestGet(GrProcessorTestData*)
egdanielc2304142014-12-11 13:15:13 -0800152
joshualittb0a8a372014-09-23 09:50:21 -0700153/** GrProcessor subclasses should insert this macro in their implementation file. They must then
154 * also implement this static function:
joshualitt0067ff52015-07-08 14:26:19 -0700155 * GrProcessor* TestCreate(GrProcessorTestData*);
joshualittb0a8a372014-09-23 09:50:21 -0700156 */
157#define GR_DEFINE_FRAGMENT_PROCESSOR_TEST(Effect) \
Brian Salomona1633922017-01-09 11:46:10 -0500158 GrProcessorTestFactory<GrFragmentProcessor> Effect::gTestFactory(Effect::TestCreate)
Brian Salomon003312a2017-01-09 16:00:33 +0000159
160#define GR_DEFINE_GEOMETRY_PROCESSOR_TEST(Effect) \
Brian Salomona1633922017-01-09 11:46:10 -0500161 GrProcessorTestFactory<GrGeometryProcessor> Effect::gTestFactory(Effect::TestCreate)
162
163#define GR_DEFINE_XP_FACTORY_TEST(Factory) \
164 GrXPFactoryTestFactory Factory::gTestFactory(Factory::TestGet)
joshualittb0a8a372014-09-23 09:50:21 -0700165
166#else // !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
167
168// The unit test relies on static initializers. Just declare the TestCreate function so that
169// its definitions will compile.
170#define GR_DECLARE_FRAGMENT_PROCESSOR_TEST \
bungeman06ca8ec2016-06-09 08:01:03 -0700171 static sk_sp<GrFragmentProcessor> TestCreate(GrProcessorTestData*)
joshualittb0a8a372014-09-23 09:50:21 -0700172#define GR_DEFINE_FRAGMENT_PROCESSOR_TEST(X)
173
174// The unit test relies on static initializers. Just declare the TestCreate function so that
175// its definitions will compile.
176#define GR_DECLARE_GEOMETRY_PROCESSOR_TEST \
bungeman06ca8ec2016-06-09 08:01:03 -0700177 static sk_sp<GrGeometryProcessor> TestCreate(GrProcessorTestData*)
joshualittb0a8a372014-09-23 09:50:21 -0700178#define GR_DEFINE_GEOMETRY_PROCESSOR_TEST(X)
179
Brian Salomona1633922017-01-09 11:46:10 -0500180// The unit test relies on static initializers. Just declare the TestGet function so that
181// its definitions will compile.
182#define GR_DECLARE_XP_FACTORY_TEST \
183 const GrXPFactory* TestGet(GrProcessorTestData*)
184#define GR_DEFINE_XP_FACTORY_TEST(X)
185
Hal Canary6f6961e2017-01-31 13:50:44 -0500186#endif // !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
187#else // GR_TEST_UTILS
188 #define GR_DECLARE_GEOMETRY_PROCESSOR_TEST
189 #define GR_DECLARE_FRAGMENT_PROCESSOR_TEST
190 #define GR_DECLARE_XP_FACTORY_TEST
191 #define GR_DEFINE_FRAGMENT_PROCESSOR_TEST(...)
192 #define GR_DEFINE_GEOMETRY_PROCESSOR_TEST(...)
193 #define GR_DEFINE_XP_FACTORY_TEST(...)
194 #define GR_DECLARE_FRAGMENT_PROCESSOR_TEST
195 #define GR_DEFINE_FRAGMENT_PROCESSOR_TEST(...)
196 #define GR_DECLARE_GEOMETRY_PROCESSOR_TEST
197 #define GR_DEFINE_GEOMETRY_PROCESSOR_TEST(...)
198 #define GR_DECLARE_XP_FACTORY_TEST
199 #define GR_DEFINE_XP_FACTORY_TEST(...)
200#endif // GR_TEST_UTILS
201#endif // GrProcessorUnitTest_DEFINED