blob: 6b0443e2b1c8de6c135302b7feb247fad7c5d8dc [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"
16#include "src/gpu/GrTestUtils.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040017#include "src/gpu/GrTextureProxy.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,
Greg Danielc594e622019-10-15 14:01:49 -040054 sk_sp<GrTextureProxy> proxies[2],
55 GrColorType proxyColorTypes[2])
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -050056 : fRandom(random)
57 , fRenderTargetContext(renderTargetContext)
58 , fContext(context) {
Robert Phillipsbb581ce2017-05-29 15:05:15 -040059 SkASSERT(proxies[0] && proxies[1]);
Robert Phillipse78b7252017-04-06 07:59:41 -040060 fProxies[0] = proxies[0];
61 fProxies[1] = proxies[1];
Greg Danielc594e622019-10-15 14:01:49 -040062 fProxyColorTypes[0] = proxyColorTypes[0];
63 fProxyColorTypes[1] = proxyColorTypes[1];
joshualitt0067ff52015-07-08 14:26:19 -070064 }
65 SkRandom* fRandom;
Brian Osman11052242016-10-27 14:47:55 -040066 const GrRenderTargetContext* fRenderTargetContext;
Robert Phillips757914d2017-01-25 15:48:30 -050067
68 GrContext* context() { return fContext; }
Robert Phillips296b1cc2017-03-15 10:42:12 -040069 GrResourceProvider* resourceProvider();
Robert Phillips1afd4cd2018-01-08 13:40:32 -050070 GrProxyProvider* proxyProvider();
Robert Phillips296b1cc2017-03-15 10:42:12 -040071 const GrCaps* caps();
Robert Phillips63c67462017-02-15 14:19:01 -050072 sk_sp<GrTextureProxy> textureProxy(int index) { return fProxies[index]; }
Greg Danielc594e622019-10-15 14:01:49 -040073 GrColorType textureProxyColorType(int index) { return fProxyColorTypes[index]; }
Robert Phillips757914d2017-01-25 15:48:30 -050074
75private:
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -050076 GrContext* fContext;
Robert Phillips63c67462017-02-15 14:19:01 -050077 sk_sp<GrTextureProxy> fProxies[2];
Greg Danielc594e622019-10-15 14:01:49 -040078 GrColorType fProxyColorTypes[2];
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 Processor = typename ProcessorSmartPtr::element_type;
88 using MakeProc = ProcessorSmartPtr (*)(GrProcessorTestData*);
joshualittb0a8a372014-09-23 09:50:21 -070089
bungeman06ca8ec2016-06-09 08:01:03 -070090 GrProcessorTestFactory(MakeProc makeProc) {
91 fMakeProc = makeProc;
joshualittb0a8a372014-09-23 09:50:21 -070092 GetFactories()->push_back(this);
93 }
94
bsalomonb5b60322015-09-14 12:26:33 -070095 /** Pick a random factory function and create a processor. */
Brian Salomon1c053642017-07-24 10:16:19 -040096 static ProcessorSmartPtr Make(GrProcessorTestData* data) {
joshualitt9e87fa72014-10-09 13:12:35 -070097 VerifyFactoryCount();
Ben Wagnerf1344ac2019-05-10 15:01:53 -040098 if (GetFactories()->count() == 0) {
99 return nullptr;
100 }
joshualitt0067ff52015-07-08 14:26:19 -0700101 uint32_t idx = data->fRandom->nextRangeU(0, GetFactories()->count() - 1);
bungeman06ca8ec2016-06-09 08:01:03 -0700102 return MakeIdx(idx, data);
bsalomonb5b60322015-09-14 12:26:33 -0700103 }
104
105 /** Number of registered factory functions */
106 static int Count() { return GetFactories()->count(); }
107
108 /** Use factory function at Index idx to create a processor. */
Brian Salomon94e0b112017-07-28 16:02:46 -0400109 static ProcessorSmartPtr MakeIdx(int idx, GrProcessorTestData* data) {
Ben Wagnerf1344ac2019-05-10 15:01:53 -0400110 SkASSERT(idx < GetFactories()->count());
Brian Salomon1c053642017-07-24 10:16:19 -0400111 GrProcessorTestFactory<ProcessorSmartPtr>* factory = (*GetFactories())[idx];
112 ProcessorSmartPtr processor = factory->fMakeProc(data);
Brian Salomona1633922017-01-09 11:46:10 -0500113 SkASSERT(processor);
114 return processor;
joshualittb0a8a372014-09-23 09:50:21 -0700115 }
116
Brian Salomona1633922017-01-09 11:46:10 -0500117private:
118 /**
joshualitt9e87fa72014-10-09 13:12:35 -0700119 * A test function which verifies the count of factories.
120 */
121 static void VerifyFactoryCount();
122
bungeman06ca8ec2016-06-09 08:01:03 -0700123 MakeProc fMakeProc;
joshualittb0a8a372014-09-23 09:50:21 -0700124
Brian Salomon1c053642017-07-24 10:16:19 -0400125 static SkTArray<GrProcessorTestFactory<ProcessorSmartPtr>*, true>* GetFactories();
joshualittb0a8a372014-09-23 09:50:21 -0700126};
127
Brian Salomonaff329b2017-08-11 09:40:37 -0400128using GrFragmentProcessorTestFactory = GrProcessorTestFactory<std::unique_ptr<GrFragmentProcessor>>;
Brian Salomon1c053642017-07-24 10:16:19 -0400129using GrGeometryProcessorTestFactory = GrProcessorTestFactory<sk_sp<GrGeometryProcessor>>;
130
Brian Salomona1633922017-01-09 11:46:10 -0500131class GrXPFactoryTestFactory : private SkNoncopyable {
132public:
133 using GetFn = const GrXPFactory*(GrProcessorTestData*);
134
135 GrXPFactoryTestFactory(GetFn* getProc) : fGetProc(getProc) { GetFactories()->push_back(this); }
136
137 static const GrXPFactory* Get(GrProcessorTestData* data) {
138 VerifyFactoryCount();
Ben Wagnerf1344ac2019-05-10 15:01:53 -0400139 if (GetFactories()->count() == 0) {
140 return nullptr;
141 }
Brian Salomona1633922017-01-09 11:46:10 -0500142 uint32_t idx = data->fRandom->nextRangeU(0, GetFactories()->count() - 1);
143 const GrXPFactory* xpf = (*GetFactories())[idx]->fGetProc(data);
144 SkASSERT(xpf);
145 return xpf;
146 }
147
148private:
149 static void VerifyFactoryCount();
150
151 GetFn* fGetProc;
152 static SkTArray<GrXPFactoryTestFactory*, true>* GetFactories();
153};
154
Ben Wagnerf1344ac2019-05-10 15:01:53 -0400155#if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
156
joshualittb0a8a372014-09-23 09:50:21 -0700157/** GrProcessor subclasses should insert this macro in their declaration to be included in the
158 * program generation unit test.
159 */
Brian Salomon1c053642017-07-24 10:16:19 -0400160#define GR_DECLARE_GEOMETRY_PROCESSOR_TEST \
161 static GrGeometryProcessorTestFactory gTestFactory SK_UNUSED; \
Brian Salomon0c26a9d2017-07-06 10:09:38 -0400162 static sk_sp<GrGeometryProcessor> TestCreate(GrProcessorTestData*);
joshualittb0a8a372014-09-23 09:50:21 -0700163
Brian Salomon1c053642017-07-24 10:16:19 -0400164#define GR_DECLARE_FRAGMENT_PROCESSOR_TEST \
165 static GrFragmentProcessorTestFactory gTestFactory SK_UNUSED; \
Brian Salomonaff329b2017-08-11 09:40:37 -0400166 static std::unique_ptr<GrFragmentProcessor> TestCreate(GrProcessorTestData*);
joshualittb0a8a372014-09-23 09:50:21 -0700167
egdanielc2304142014-12-11 13:15:13 -0800168#define GR_DECLARE_XP_FACTORY_TEST \
Brian Salomona1633922017-01-09 11:46:10 -0500169 static GrXPFactoryTestFactory gTestFactory SK_UNUSED; \
Brian Salomon0c26a9d2017-07-06 10:09:38 -0400170 static const GrXPFactory* TestGet(GrProcessorTestData*);
egdanielc2304142014-12-11 13:15:13 -0800171
joshualittb0a8a372014-09-23 09:50:21 -0700172/** GrProcessor subclasses should insert this macro in their implementation file. They must then
173 * also implement this static function:
joshualitt0067ff52015-07-08 14:26:19 -0700174 * GrProcessor* TestCreate(GrProcessorTestData*);
joshualittb0a8a372014-09-23 09:50:21 -0700175 */
Brian Salomon1c053642017-07-24 10:16:19 -0400176#define GR_DEFINE_FRAGMENT_PROCESSOR_TEST(Effect) \
177 GrFragmentProcessorTestFactory Effect::gTestFactory(Effect::TestCreate)
Brian Salomon003312a2017-01-09 16:00:33 +0000178
Brian Salomon1c053642017-07-24 10:16:19 -0400179#define GR_DEFINE_GEOMETRY_PROCESSOR_TEST(Effect) \
180 GrGeometryProcessorTestFactory Effect::gTestFactory(Effect::TestCreate)
Brian Salomona1633922017-01-09 11:46:10 -0500181
182#define GR_DEFINE_XP_FACTORY_TEST(Factory) \
183 GrXPFactoryTestFactory Factory::gTestFactory(Factory::TestGet)
joshualittb0a8a372014-09-23 09:50:21 -0700184
185#else // !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
186
187// The unit test relies on static initializers. Just declare the TestCreate function so that
188// its definitions will compile.
189#define GR_DECLARE_FRAGMENT_PROCESSOR_TEST \
Brian Salomonaff329b2017-08-11 09:40:37 -0400190 static std::unique_ptr<GrFragmentProcessor> TestCreate(GrProcessorTestData*);
joshualittb0a8a372014-09-23 09:50:21 -0700191#define GR_DEFINE_FRAGMENT_PROCESSOR_TEST(X)
192
193// The unit test relies on static initializers. Just declare the TestCreate function so that
194// its definitions will compile.
195#define GR_DECLARE_GEOMETRY_PROCESSOR_TEST \
Brian Salomon0c26a9d2017-07-06 10:09:38 -0400196 static sk_sp<GrGeometryProcessor> TestCreate(GrProcessorTestData*);
joshualittb0a8a372014-09-23 09:50:21 -0700197#define GR_DEFINE_GEOMETRY_PROCESSOR_TEST(X)
198
Brian Salomona1633922017-01-09 11:46:10 -0500199// The unit test relies on static initializers. Just declare the TestGet function so that
200// its definitions will compile.
201#define GR_DECLARE_XP_FACTORY_TEST \
Brian Salomon0c26a9d2017-07-06 10:09:38 -0400202 const GrXPFactory* TestGet(GrProcessorTestData*);
Brian Salomona1633922017-01-09 11:46:10 -0500203#define GR_DEFINE_XP_FACTORY_TEST(X)
204
Hal Canary6f6961e2017-01-31 13:50:44 -0500205#endif // !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
206#else // GR_TEST_UTILS
207 #define GR_DECLARE_GEOMETRY_PROCESSOR_TEST
208 #define GR_DECLARE_FRAGMENT_PROCESSOR_TEST
209 #define GR_DECLARE_XP_FACTORY_TEST
210 #define GR_DEFINE_FRAGMENT_PROCESSOR_TEST(...)
211 #define GR_DEFINE_GEOMETRY_PROCESSOR_TEST(...)
212 #define GR_DEFINE_XP_FACTORY_TEST(...)
213 #define GR_DECLARE_FRAGMENT_PROCESSOR_TEST
214 #define GR_DEFINE_FRAGMENT_PROCESSOR_TEST(...)
215 #define GR_DECLARE_GEOMETRY_PROCESSOR_TEST
216 #define GR_DEFINE_GEOMETRY_PROCESSOR_TEST(...)
217 #define GR_DECLARE_XP_FACTORY_TEST
218 #define GR_DEFINE_XP_FACTORY_TEST(...)
219#endif // GR_TEST_UTILS
220#endif // GrProcessorUnitTest_DEFINED