joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 11 | #include "GrColor.h" |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 12 | #include "SkRandom.h" |
| 13 | #include "SkTArray.h" |
| 14 | #include "SkTypes.h" |
| 15 | |
| 16 | class SkMatrix; |
| 17 | class GrDrawTargetCaps; |
| 18 | |
| 19 | namespace GrProcessorUnitTest { |
| 20 | // Used to access the dummy textures in TestCreate procs. |
| 21 | enum { |
| 22 | kSkiaPMTextureIdx = 0, |
| 23 | kAlphaTextureIdx = 1, |
| 24 | }; |
| 25 | |
| 26 | /** |
| 27 | * A helper for use in GrProcessor::TestCreate functions. |
| 28 | */ |
| 29 | const SkMatrix& TestMatrix(SkRandom*); |
| 30 | |
| 31 | } |
| 32 | |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 33 | static 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 | |
| 70 | static 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 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 93 | #if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS |
| 94 | |
| 95 | class GrContext; |
| 96 | class GrProcessor; |
| 97 | class GrTexture; |
| 98 | |
| 99 | template <class Processor> |
| 100 | class GrProcessorTestFactory : SkNoncopyable { |
| 101 | public: |
| 102 | |
| 103 | typedef Processor* (*CreateProc)(SkRandom*, |
joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 104 | GrContext*, |
| 105 | const GrDrawTargetCaps& caps, |
| 106 | GrTexture* dummyTextures[]); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 107 | |
| 108 | GrProcessorTestFactory(CreateProc createProc) { |
| 109 | fCreateProc = createProc; |
| 110 | GetFactories()->push_back(this); |
| 111 | } |
| 112 | |
| 113 | static Processor* CreateStage(SkRandom* random, |
joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 114 | GrContext* context, |
| 115 | const GrDrawTargetCaps& caps, |
| 116 | GrTexture* dummyTextures[]) { |
| 117 | VerifyFactoryCount(); |
| 118 | SkASSERT(GetFactories()->count()); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 119 | 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 | |
joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 124 | /* |
| 125 | * A test function which verifies the count of factories. |
| 126 | */ |
| 127 | static void VerifyFactoryCount(); |
| 128 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 129 | private: |
| 130 | CreateProc fCreateProc; |
| 131 | |
joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 132 | static SkTArray<GrProcessorTestFactory<Processor>*, true>* GetFactories(); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | /** GrProcessor subclasses should insert this macro in their declaration to be included in the |
| 136 | * program generation unit test. |
| 137 | */ |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 138 | #define GR_DECLARE_GEOMETRY_PROCESSOR_TEST \ |
joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 139 | static GrProcessorTestFactory<GrGeometryProcessor> gTestFactory SK_UNUSED; \ |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 140 | static GrGeometryProcessor* TestCreate(SkRandom*, \ |
joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 141 | GrContext*, \ |
| 142 | const GrDrawTargetCaps&, \ |
| 143 | GrTexture* dummyTextures[2]) |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 144 | |
| 145 | #define GR_DECLARE_FRAGMENT_PROCESSOR_TEST \ |
joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 146 | static GrProcessorTestFactory<GrFragmentProcessor> gTestFactory SK_UNUSED; \ |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 147 | static GrFragmentProcessor* TestCreate(SkRandom*, \ |
joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 148 | GrContext*, \ |
| 149 | const GrDrawTargetCaps&, \ |
| 150 | GrTexture* dummyTextures[2]) |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 151 | |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 152 | #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 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 160 | /** 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 | |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 174 | #define GR_DEFINE_XP_FACTORY_TEST(Factory) \ |
| 175 | GrProcessorTestFactory<GrXPFactory> Factory :: gTestFactory(Factory :: TestCreate) |
| 176 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 177 | #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*, \ |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 186 | GrContext*, \ |
| 187 | const GrDrawTargetCaps&, \ |
| 188 | GrTexture* dummyTextures[2]) |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 189 | #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. |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 193 | #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. |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 202 | #define GR_DECLARE_GEOMETRY_PROCESSOR_TEST \ |
| 203 | static GrGeometryProcessor* TestCreate(SkRandom*, \ |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 204 | GrContext*, \ |
| 205 | const GrDrawTargetCaps&, \ |
| 206 | GrTexture* dummyTextures[2]) |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 207 | #define GR_DEFINE_GEOMETRY_PROCESSOR_TEST(X) |
| 208 | |
| 209 | #endif // !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS |
| 210 | #endif |