blob: 3e1601dc8e049e20c9c4110f9d917b5db4559e87 [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
11#include "SkRandom.h"
12#include "SkTArray.h"
13#include "SkTypes.h"
14
15class SkMatrix;
16class GrDrawTargetCaps;
17
18namespace GrProcessorUnitTest {
19// Used to access the dummy textures in TestCreate procs.
20enum {
21 kSkiaPMTextureIdx = 0,
22 kAlphaTextureIdx = 1,
23};
24
25/**
26 * A helper for use in GrProcessor::TestCreate functions.
27 */
28const SkMatrix& TestMatrix(SkRandom*);
29
30}
31
32#if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
33
34class GrContext;
35class GrProcessor;
36class GrTexture;
37
38template <class Processor>
39class GrProcessorTestFactory : SkNoncopyable {
40public:
41
42 typedef Processor* (*CreateProc)(SkRandom*,
joshualitt9e87fa72014-10-09 13:12:35 -070043 GrContext*,
44 const GrDrawTargetCaps& caps,
45 GrTexture* dummyTextures[]);
joshualittb0a8a372014-09-23 09:50:21 -070046
47 GrProcessorTestFactory(CreateProc createProc) {
48 fCreateProc = createProc;
49 GetFactories()->push_back(this);
50 }
51
52 static Processor* CreateStage(SkRandom* random,
joshualitt9e87fa72014-10-09 13:12:35 -070053 GrContext* context,
54 const GrDrawTargetCaps& caps,
55 GrTexture* dummyTextures[]) {
56 VerifyFactoryCount();
57 SkASSERT(GetFactories()->count());
joshualittb0a8a372014-09-23 09:50:21 -070058 uint32_t idx = random->nextRangeU(0, GetFactories()->count() - 1);
59 GrProcessorTestFactory<Processor>* factory = (*GetFactories())[idx];
60 return factory->fCreateProc(random, context, caps, dummyTextures);
61 }
62
joshualitt9e87fa72014-10-09 13:12:35 -070063 /*
64 * A test function which verifies the count of factories.
65 */
66 static void VerifyFactoryCount();
67
joshualittb0a8a372014-09-23 09:50:21 -070068private:
69 CreateProc fCreateProc;
70
joshualitt9e87fa72014-10-09 13:12:35 -070071 static SkTArray<GrProcessorTestFactory<Processor>*, true>* GetFactories();
joshualittb0a8a372014-09-23 09:50:21 -070072};
73
74/** GrProcessor subclasses should insert this macro in their declaration to be included in the
75 * program generation unit test.
76 */
joshualittb0a8a372014-09-23 09:50:21 -070077#define GR_DECLARE_GEOMETRY_PROCESSOR_TEST \
joshualitt9e87fa72014-10-09 13:12:35 -070078 static GrProcessorTestFactory<GrGeometryProcessor> gTestFactory SK_UNUSED; \
joshualittb0a8a372014-09-23 09:50:21 -070079 static GrGeometryProcessor* TestCreate(SkRandom*, \
joshualitt9e87fa72014-10-09 13:12:35 -070080 GrContext*, \
81 const GrDrawTargetCaps&, \
82 GrTexture* dummyTextures[2])
joshualittb0a8a372014-09-23 09:50:21 -070083
84#define GR_DECLARE_FRAGMENT_PROCESSOR_TEST \
joshualitt9e87fa72014-10-09 13:12:35 -070085 static GrProcessorTestFactory<GrFragmentProcessor> gTestFactory SK_UNUSED; \
joshualittb0a8a372014-09-23 09:50:21 -070086 static GrFragmentProcessor* TestCreate(SkRandom*, \
joshualitt9e87fa72014-10-09 13:12:35 -070087 GrContext*, \
88 const GrDrawTargetCaps&, \
89 GrTexture* dummyTextures[2])
joshualittb0a8a372014-09-23 09:50:21 -070090
91/** GrProcessor subclasses should insert this macro in their implementation file. They must then
92 * also implement this static function:
93 * GrProcessor* TestCreate(SkRandom*,
94 * GrContext*,
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
100 * the GrContext.
101 */
102#define GR_DEFINE_FRAGMENT_PROCESSOR_TEST(Effect) \
103 GrProcessorTestFactory<GrFragmentProcessor> Effect :: gTestFactory(Effect :: TestCreate)
104
105#define GR_DEFINE_GEOMETRY_PROCESSOR_TEST(Effect) \
106 GrProcessorTestFactory<GrGeometryProcessor> Effect :: gTestFactory(Effect :: TestCreate)
107
108#else // !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
109
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*, \
114 GrContext*, \
115 const GrDrawTargetCaps&, \
116 GrTexture* dummyTextures[2])
117#define GR_DEFINE_FRAGMENT_PROCESSOR_TEST(X)
118
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*, \
123 GrContext*, \
124 const GrDrawTargetCaps&, \
125 GrTexture* dummyTextures[2])
126#define GR_DEFINE_GEOMETRY_PROCESSOR_TEST(X)
127
128#endif // !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
129#endif