blob: 4b97483c0fa438f017c2c6728ed04c2b5084bd44 [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
joshualitt2e3b3e32014-12-09 13:31:14 -080011#include "GrColor.h"
joshualittb0a8a372014-09-23 09:50:21 -070012#include "SkRandom.h"
13#include "SkTArray.h"
14#include "SkTypes.h"
15
16class SkMatrix;
17class GrDrawTargetCaps;
18
19namespace GrProcessorUnitTest {
20// Used to access the dummy textures in TestCreate procs.
21enum {
22 kSkiaPMTextureIdx = 0,
23 kAlphaTextureIdx = 1,
24};
25
26/**
27 * A helper for use in GrProcessor::TestCreate functions.
28 */
29const SkMatrix& TestMatrix(SkRandom*);
30
31}
32
joshualitt2e3b3e32014-12-09 13:31:14 -080033static inline GrColor GrRandomColor(SkRandom* random) {
34 // There are only a few cases of random colors which interest us
35 enum ColorMode {
36 kAllOnes_ColorMode,
37 kAllZeros_ColorMode,
38 kAlphaOne_ColorMode,
39 kRandom_ColorMode,
40 kLast_ColorMode = kRandom_ColorMode
41 };
42
43 ColorMode colorMode = ColorMode(random->nextULessThan(kLast_ColorMode + 1));
44 GrColor color;
45 switch (colorMode) {
46 case kAllOnes_ColorMode:
47 color = GrColorPackRGBA(0xFF, 0xFF, 0xFF, 0xFF);
48 break;
49 case kAllZeros_ColorMode:
50 color = GrColorPackRGBA(0, 0, 0, 0);
51 break;
52 case kAlphaOne_ColorMode:
53 color = GrColorPackRGBA(random->nextULessThan(256),
54 random->nextULessThan(256),
55 random->nextULessThan(256),
56 0xFF);
57 break;
58 case kRandom_ColorMode:
59 uint8_t alpha = random->nextULessThan(256);
60 color = GrColorPackRGBA(random->nextRangeU(0, alpha),
61 random->nextRangeU(0, alpha),
62 random->nextRangeU(0, alpha),
63 alpha);
64 break;
65 }
66 GrColorIsPMAssert(color);
67 return color;
68}
69
70static inline uint8_t GrRandomCoverage(SkRandom* random) {
71 enum CoverageMode {
72 kZero_CoverageMode,
73 kAllOnes_CoverageMode,
74 kRandom_CoverageMode,
75 kLast_CoverageMode = kRandom_CoverageMode
76 };
77
78 CoverageMode colorMode = CoverageMode(random->nextULessThan(kLast_CoverageMode + 1));
79 uint8_t coverage;
80 switch (colorMode) {
81 case kZero_CoverageMode:
82 coverage = 0;
83 case kAllOnes_CoverageMode:
84 coverage = 0xff;
85 break;
86 case kRandom_CoverageMode:
87 coverage = random->nextULessThan(256);
88 break;
89 }
90 return coverage;
91}
92
joshualittb0a8a372014-09-23 09:50:21 -070093#if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
94
95class GrContext;
96class GrProcessor;
97class GrTexture;
98
99template <class Processor>
100class GrProcessorTestFactory : SkNoncopyable {
101public:
102
103 typedef Processor* (*CreateProc)(SkRandom*,
joshualitt9e87fa72014-10-09 13:12:35 -0700104 GrContext*,
105 const GrDrawTargetCaps& caps,
106 GrTexture* dummyTextures[]);
joshualittb0a8a372014-09-23 09:50:21 -0700107
108 GrProcessorTestFactory(CreateProc createProc) {
109 fCreateProc = createProc;
110 GetFactories()->push_back(this);
111 }
112
113 static Processor* CreateStage(SkRandom* random,
joshualitt9e87fa72014-10-09 13:12:35 -0700114 GrContext* context,
115 const GrDrawTargetCaps& caps,
116 GrTexture* dummyTextures[]) {
117 VerifyFactoryCount();
118 SkASSERT(GetFactories()->count());
joshualittb0a8a372014-09-23 09:50:21 -0700119 uint32_t idx = random->nextRangeU(0, GetFactories()->count() - 1);
120 GrProcessorTestFactory<Processor>* factory = (*GetFactories())[idx];
121 return factory->fCreateProc(random, context, caps, dummyTextures);
122 }
123
joshualitt9e87fa72014-10-09 13:12:35 -0700124 /*
125 * A test function which verifies the count of factories.
126 */
127 static void VerifyFactoryCount();
128
joshualittb0a8a372014-09-23 09:50:21 -0700129private:
130 CreateProc fCreateProc;
131
joshualitt9e87fa72014-10-09 13:12:35 -0700132 static SkTArray<GrProcessorTestFactory<Processor>*, true>* GetFactories();
joshualittb0a8a372014-09-23 09:50:21 -0700133};
134
135/** GrProcessor subclasses should insert this macro in their declaration to be included in the
136 * program generation unit test.
137 */
joshualittb0a8a372014-09-23 09:50:21 -0700138#define GR_DECLARE_GEOMETRY_PROCESSOR_TEST \
joshualitt9e87fa72014-10-09 13:12:35 -0700139 static GrProcessorTestFactory<GrGeometryProcessor> gTestFactory SK_UNUSED; \
joshualittb0a8a372014-09-23 09:50:21 -0700140 static GrGeometryProcessor* TestCreate(SkRandom*, \
joshualitt9e87fa72014-10-09 13:12:35 -0700141 GrContext*, \
142 const GrDrawTargetCaps&, \
143 GrTexture* dummyTextures[2])
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; \
joshualittb0a8a372014-09-23 09:50:21 -0700147 static GrFragmentProcessor* TestCreate(SkRandom*, \
joshualitt9e87fa72014-10-09 13:12:35 -0700148 GrContext*, \
149 const GrDrawTargetCaps&, \
150 GrTexture* dummyTextures[2])
joshualittb0a8a372014-09-23 09:50:21 -0700151
egdanielc2304142014-12-11 13:15:13 -0800152#define GR_DECLARE_XP_FACTORY_TEST \
153 static GrProcessorTestFactory<GrXPFactory> gTestFactory SK_UNUSED; \
154 static GrXPFactory* TestCreate(SkRandom*, \
155 GrContext*, \
156 const GrDrawTargetCaps&, \
157 GrTexture* dummyTextures[2])
158
159
joshualittb0a8a372014-09-23 09:50:21 -0700160/** GrProcessor subclasses should insert this macro in their implementation file. They must then
161 * also implement this static function:
162 * GrProcessor* TestCreate(SkRandom*,
163 * GrContext*,
164 * const GrDrawTargetCaps&,
165 * GrTexture* dummyTextures[2]);
166 * dummyTextures[] are valid textures that can optionally be used to construct GrTextureAccesses.
167 * The first texture has config kSkia8888_GrPixelConfig and the second has
168 * kAlpha_8_GrPixelConfig. TestCreate functions are also free to create additional textures using
169 * the GrContext.
170 */
171#define GR_DEFINE_FRAGMENT_PROCESSOR_TEST(Effect) \
172 GrProcessorTestFactory<GrFragmentProcessor> Effect :: gTestFactory(Effect :: TestCreate)
173
egdanielc2304142014-12-11 13:15:13 -0800174#define GR_DEFINE_XP_FACTORY_TEST(Factory) \
175 GrProcessorTestFactory<GrXPFactory> Factory :: gTestFactory(Factory :: TestCreate)
176
joshualittb0a8a372014-09-23 09:50:21 -0700177#define GR_DEFINE_GEOMETRY_PROCESSOR_TEST(Effect) \
178 GrProcessorTestFactory<GrGeometryProcessor> Effect :: gTestFactory(Effect :: TestCreate)
179
180#else // !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
181
182// The unit test relies on static initializers. Just declare the TestCreate function so that
183// its definitions will compile.
184#define GR_DECLARE_FRAGMENT_PROCESSOR_TEST \
185 static GrFragmentProcessor* TestCreate(SkRandom*, \
egdanielc2304142014-12-11 13:15:13 -0800186 GrContext*, \
187 const GrDrawTargetCaps&, \
188 GrTexture* dummyTextures[2])
joshualittb0a8a372014-09-23 09:50:21 -0700189#define GR_DEFINE_FRAGMENT_PROCESSOR_TEST(X)
190
191// The unit test relies on static initializers. Just declare the TestCreate function so that
192// its definitions will compile.
egdanielc2304142014-12-11 13:15:13 -0800193#define GR_DECLARE_XP_FACTORY_TEST \
194 static GrXPFactory* TestCreate(SkRandom*, \
195 GrContext*, \
196 const GrDrawTargetCaps&, \
197 GrTexture* dummyTextures[2])
198#define GR_DEFINE_XP_FACTORY_TEST(X)
199
200// The unit test relies on static initializers. Just declare the TestCreate function so that
201// its definitions will compile.
joshualittb0a8a372014-09-23 09:50:21 -0700202#define GR_DECLARE_GEOMETRY_PROCESSOR_TEST \
203 static GrGeometryProcessor* TestCreate(SkRandom*, \
egdanielc2304142014-12-11 13:15:13 -0800204 GrContext*, \
205 const GrDrawTargetCaps&, \
206 GrTexture* dummyTextures[2])
joshualittb0a8a372014-09-23 09:50:21 -0700207#define GR_DEFINE_GEOMETRY_PROCESSOR_TEST(X)
208
209#endif // !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
210#endif