blob: 8f4e84e161c155d359ecb2ce2d0da10f69b23d0d [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
joshualitt4eaf9ce2015-04-28 13:31:18 -070011#include "GrTestUtils.h"
joshualittb0a8a372014-09-23 09:50:21 -070012#include "SkTArray.h"
13#include "SkTypes.h"
14
15class SkMatrix;
bsalomon4b91f762015-05-19 09:29:46 -070016class GrCaps;
joshualitt0067ff52015-07-08 14:26:19 -070017class GrContext;
bsalomon506c8022015-09-14 13:16:26 -070018struct GrProcessorTestData;
joshualittb0a8a372014-09-23 09:50:21 -070019
20namespace GrProcessorUnitTest {
bsalomon506c8022015-09-14 13:16:26 -070021
joshualittb0a8a372014-09-23 09:50:21 -070022// Used to access the dummy textures in TestCreate procs.
23enum {
24 kSkiaPMTextureIdx = 0,
25 kAlphaTextureIdx = 1,
26};
27
bsalomon506c8022015-09-14 13:16:26 -070028/** This allows parent FPs to implement a test create with known leaf children in order to avoid
29creating an unbounded FP tree which may overflow various shader limits. */
30const GrFragmentProcessor* CreateChildFP(GrProcessorTestData*);
31
joshualitt2e3b3e32014-12-09 13:31:14 -080032}
33
joshualitt0067ff52015-07-08 14:26:19 -070034/*
35 * GrProcessorTestData is an argument struct to TestCreate functions
36 * fTextures are valid textures that can optionally be used to construct
37 * GrTextureAccesses. The first texture has config kSkia8888_GrPixelConfig and the second has
38 * kAlpha_8_GrPixelConfig. TestCreate functions are also free to create additional textures using
39 * the GrContext.
40 */
41struct GrProcessorTestData {
42 GrProcessorTestData(SkRandom* random,
43 GrContext* context,
joshualitt0067ff52015-07-08 14:26:19 -070044 const GrCaps* caps,
45 GrTexture* textures[2])
46 : fRandom(random)
47 , fContext(context)
joshualitt0067ff52015-07-08 14:26:19 -070048 , fCaps(caps) {
49 fTextures[0] = textures[0];
50 fTextures[1] = textures[1];
51 }
52 SkRandom* fRandom;
53 GrContext* fContext;
joshualitt0067ff52015-07-08 14:26:19 -070054 const GrCaps* fCaps;
55 GrTexture* fTextures[2];
56};
57
joshualittb0a8a372014-09-23 09:50:21 -070058#if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
59
joshualittb0a8a372014-09-23 09:50:21 -070060class GrProcessor;
61class GrTexture;
62
bsalomon506c8022015-09-14 13:16:26 -070063template <class Processor> class GrProcessorTestFactory : SkNoncopyable {
joshualittb0a8a372014-09-23 09:50:21 -070064public:
bsalomonc21b09e2015-08-28 18:46:56 -070065 typedef const Processor* (*CreateProc)(GrProcessorTestData*);
joshualittb0a8a372014-09-23 09:50:21 -070066
67 GrProcessorTestFactory(CreateProc createProc) {
68 fCreateProc = createProc;
69 GetFactories()->push_back(this);
70 }
71
bsalomonb5b60322015-09-14 12:26:33 -070072 /** Pick a random factory function and create a processor. */
73 static const Processor* Create(GrProcessorTestData* data) {
joshualitt9e87fa72014-10-09 13:12:35 -070074 VerifyFactoryCount();
75 SkASSERT(GetFactories()->count());
joshualitt0067ff52015-07-08 14:26:19 -070076 uint32_t idx = data->fRandom->nextRangeU(0, GetFactories()->count() - 1);
bsalomonb5b60322015-09-14 12:26:33 -070077 return CreateIdx(idx, data);
78 }
79
80 /** Number of registered factory functions */
81 static int Count() { return GetFactories()->count(); }
82
83 /** Use factory function at Index idx to create a processor. */
84 static const Processor* CreateIdx(int idx, GrProcessorTestData* data) {
joshualittb0a8a372014-09-23 09:50:21 -070085 GrProcessorTestFactory<Processor>* factory = (*GetFactories())[idx];
joshualitt0067ff52015-07-08 14:26:19 -070086 return factory->fCreateProc(data);
joshualittb0a8a372014-09-23 09:50:21 -070087 }
88
joshualitt9e87fa72014-10-09 13:12:35 -070089 /*
90 * A test function which verifies the count of factories.
91 */
92 static void VerifyFactoryCount();
93
joshualittb0a8a372014-09-23 09:50:21 -070094private:
95 CreateProc fCreateProc;
96
joshualitt9e87fa72014-10-09 13:12:35 -070097 static SkTArray<GrProcessorTestFactory<Processor>*, true>* GetFactories();
joshualittb0a8a372014-09-23 09:50:21 -070098};
99
100/** GrProcessor subclasses should insert this macro in their declaration to be included in the
101 * program generation unit test.
102 */
joshualittb0a8a372014-09-23 09:50:21 -0700103#define GR_DECLARE_GEOMETRY_PROCESSOR_TEST \
joshualitt9e87fa72014-10-09 13:12:35 -0700104 static GrProcessorTestFactory<GrGeometryProcessor> gTestFactory SK_UNUSED; \
bsalomonc21b09e2015-08-28 18:46:56 -0700105 static const GrGeometryProcessor* TestCreate(GrProcessorTestData*)
joshualittb0a8a372014-09-23 09:50:21 -0700106
107#define GR_DECLARE_FRAGMENT_PROCESSOR_TEST \
joshualitt9e87fa72014-10-09 13:12:35 -0700108 static GrProcessorTestFactory<GrFragmentProcessor> gTestFactory SK_UNUSED; \
bsalomonc21b09e2015-08-28 18:46:56 -0700109 static const GrFragmentProcessor* TestCreate(GrProcessorTestData*)
joshualittb0a8a372014-09-23 09:50:21 -0700110
egdanielc2304142014-12-11 13:15:13 -0800111#define GR_DECLARE_XP_FACTORY_TEST \
112 static GrProcessorTestFactory<GrXPFactory> gTestFactory SK_UNUSED; \
bsalomonc21b09e2015-08-28 18:46:56 -0700113 static const GrXPFactory* TestCreate(GrProcessorTestData*)
egdanielc2304142014-12-11 13:15:13 -0800114
joshualittb0a8a372014-09-23 09:50:21 -0700115/** GrProcessor subclasses should insert this macro in their implementation file. They must then
116 * also implement this static function:
joshualitt0067ff52015-07-08 14:26:19 -0700117 * GrProcessor* TestCreate(GrProcessorTestData*);
joshualittb0a8a372014-09-23 09:50:21 -0700118 */
119#define GR_DEFINE_FRAGMENT_PROCESSOR_TEST(Effect) \
120 GrProcessorTestFactory<GrFragmentProcessor> Effect :: gTestFactory(Effect :: TestCreate)
121
egdanielc2304142014-12-11 13:15:13 -0800122#define GR_DEFINE_XP_FACTORY_TEST(Factory) \
123 GrProcessorTestFactory<GrXPFactory> Factory :: gTestFactory(Factory :: TestCreate)
124
joshualittb0a8a372014-09-23 09:50:21 -0700125#define GR_DEFINE_GEOMETRY_PROCESSOR_TEST(Effect) \
126 GrProcessorTestFactory<GrGeometryProcessor> Effect :: gTestFactory(Effect :: TestCreate)
127
128#else // !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
129
130// The unit test relies on static initializers. Just declare the TestCreate function so that
131// its definitions will compile.
132#define GR_DECLARE_FRAGMENT_PROCESSOR_TEST \
bsalomonc21b09e2015-08-28 18:46:56 -0700133 static const GrFragmentProcessor* TestCreate(GrProcessorTestData*)
joshualittb0a8a372014-09-23 09:50:21 -0700134#define GR_DEFINE_FRAGMENT_PROCESSOR_TEST(X)
135
136// The unit test relies on static initializers. Just declare the TestCreate function so that
137// its definitions will compile.
egdanielc2304142014-12-11 13:15:13 -0800138#define GR_DECLARE_XP_FACTORY_TEST \
bsalomonc21b09e2015-08-28 18:46:56 -0700139 static const GrXPFactory* TestCreate(GrProcessorTestData*)
egdanielc2304142014-12-11 13:15:13 -0800140#define GR_DEFINE_XP_FACTORY_TEST(X)
141
142// The unit test relies on static initializers. Just declare the TestCreate function so that
143// its definitions will compile.
joshualittb0a8a372014-09-23 09:50:21 -0700144#define GR_DECLARE_GEOMETRY_PROCESSOR_TEST \
bsalomonc21b09e2015-08-28 18:46:56 -0700145 static const GrGeometryProcessor* TestCreate(GrProcessorTestData*)
joshualittb0a8a372014-09-23 09:50:21 -0700146#define GR_DEFINE_GEOMETRY_PROCESSOR_TEST(X)
147
148#endif // !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
149#endif