2 * Copyright 2012 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #ifndef GrProcessorUnitTest_DEFINED
9 #define GrProcessorUnitTest_DEFINED
16 class GrDrawTargetCaps;
18 namespace GrProcessorUnitTest {
19 // Used to access the dummy textures in TestCreate procs.
21 kSkiaPMTextureIdx = 0,
26 * A helper for use in GrProcessor::TestCreate functions.
28 const SkMatrix& TestMatrix(SkRandom*);
32 #if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
38 template <class Processor>
39 class GrProcessorTestFactory : SkNoncopyable {
42 typedef Processor* (*CreateProc)(SkRandom*,
44 const GrDrawTargetCaps& caps,
45 GrTexture* dummyTextures[]);
47 GrProcessorTestFactory(CreateProc createProc) {
48 fCreateProc = createProc;
49 GetFactories()->push_back(this);
52 static Processor* CreateStage(SkRandom* random,
54 const GrDrawTargetCaps& caps,
55 GrTexture* dummyTextures[]) {
57 SkASSERT(GetFactories()->count());
58 uint32_t idx = random->nextRangeU(0, GetFactories()->count() - 1);
59 GrProcessorTestFactory<Processor>* factory = (*GetFactories())[idx];
60 return factory->fCreateProc(random, context, caps, dummyTextures);
64 * A test function which verifies the count of factories.
66 static void VerifyFactoryCount();
69 CreateProc fCreateProc;
71 static SkTArray<GrProcessorTestFactory<Processor>*, true>* GetFactories();
74 /** GrProcessor subclasses should insert this macro in their declaration to be included in the
75 * program generation unit test.
77 #define GR_DECLARE_GEOMETRY_PROCESSOR_TEST \
78 static GrProcessorTestFactory<GrGeometryProcessor> gTestFactory SK_UNUSED; \
79 static GrGeometryProcessor* TestCreate(SkRandom*, \
81 const GrDrawTargetCaps&, \
82 GrTexture* dummyTextures[2])
84 #define GR_DECLARE_FRAGMENT_PROCESSOR_TEST \
85 static GrProcessorTestFactory<GrFragmentProcessor> gTestFactory SK_UNUSED; \
86 static GrFragmentProcessor* TestCreate(SkRandom*, \
88 const GrDrawTargetCaps&, \
89 GrTexture* dummyTextures[2])
91 /** GrProcessor subclasses should insert this macro in their implementation file. They must then
92 * also implement this static function:
93 * GrProcessor* TestCreate(SkRandom*,
95 * const GrDrawTargetCaps&,
96 * GrTexture* dummyTextures[2]);
97 * dummyTextures[] are valid textures that can optionally be used to construct GrTextureAccesses.
98 * The first texture has config kSkia8888_GrPixelConfig and the second has
99 * kAlpha_8_GrPixelConfig. TestCreate functions are also free to create additional textures using
102 #define GR_DEFINE_FRAGMENT_PROCESSOR_TEST(Effect) \
103 GrProcessorTestFactory<GrFragmentProcessor> Effect :: gTestFactory(Effect :: TestCreate)
105 #define GR_DEFINE_GEOMETRY_PROCESSOR_TEST(Effect) \
106 GrProcessorTestFactory<GrGeometryProcessor> Effect :: gTestFactory(Effect :: TestCreate)
108 #else // !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
110 // The unit test relies on static initializers. Just declare the TestCreate function so that
111 // its definitions will compile.
112 #define GR_DECLARE_FRAGMENT_PROCESSOR_TEST \
113 static GrFragmentProcessor* TestCreate(SkRandom*, \
115 const GrDrawTargetCaps&, \
116 GrTexture* dummyTextures[2])
117 #define GR_DEFINE_FRAGMENT_PROCESSOR_TEST(X)
119 // The unit test relies on static initializers. Just declare the TestCreate function so that
120 // its definitions will compile.
121 #define GR_DECLARE_GEOMETRY_PROCESSOR_TEST \
122 static GrGeometryProcessor* TestCreate(SkRandom*, \
124 const GrDrawTargetCaps&, \
125 GrTexture* dummyTextures[2])
126 #define GR_DEFINE_GEOMETRY_PROCESSOR_TEST(X)
128 #endif // !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS