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 | |
| 11 | #include "SkRandom.h" |
| 12 | #include "SkTArray.h" |
| 13 | #include "SkTypes.h" |
| 14 | |
| 15 | class SkMatrix; |
| 16 | class GrDrawTargetCaps; |
| 17 | |
| 18 | namespace GrProcessorUnitTest { |
| 19 | // Used to access the dummy textures in TestCreate procs. |
| 20 | enum { |
| 21 | kSkiaPMTextureIdx = 0, |
| 22 | kAlphaTextureIdx = 1, |
| 23 | }; |
| 24 | |
| 25 | /** |
| 26 | * A helper for use in GrProcessor::TestCreate functions. |
| 27 | */ |
| 28 | const SkMatrix& TestMatrix(SkRandom*); |
| 29 | |
| 30 | } |
| 31 | |
| 32 | #if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS |
| 33 | |
| 34 | class GrContext; |
| 35 | class GrProcessor; |
| 36 | class GrTexture; |
| 37 | |
| 38 | template <class Processor> |
| 39 | class GrProcessorTestFactory : SkNoncopyable { |
| 40 | public: |
| 41 | |
| 42 | typedef Processor* (*CreateProc)(SkRandom*, |
joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 43 | GrContext*, |
| 44 | const GrDrawTargetCaps& caps, |
| 45 | GrTexture* dummyTextures[]); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 46 | |
| 47 | GrProcessorTestFactory(CreateProc createProc) { |
| 48 | fCreateProc = createProc; |
| 49 | GetFactories()->push_back(this); |
| 50 | } |
| 51 | |
| 52 | static Processor* CreateStage(SkRandom* random, |
joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 53 | GrContext* context, |
| 54 | const GrDrawTargetCaps& caps, |
| 55 | GrTexture* dummyTextures[]) { |
| 56 | VerifyFactoryCount(); |
| 57 | SkASSERT(GetFactories()->count()); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 58 | 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 | |
joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 63 | /* |
| 64 | * A test function which verifies the count of factories. |
| 65 | */ |
| 66 | static void VerifyFactoryCount(); |
| 67 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 68 | private: |
| 69 | CreateProc fCreateProc; |
| 70 | |
joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 71 | static SkTArray<GrProcessorTestFactory<Processor>*, true>* GetFactories(); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | /** GrProcessor subclasses should insert this macro in their declaration to be included in the |
| 75 | * program generation unit test. |
| 76 | */ |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 77 | #define GR_DECLARE_GEOMETRY_PROCESSOR_TEST \ |
joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 78 | static GrProcessorTestFactory<GrGeometryProcessor> gTestFactory SK_UNUSED; \ |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 79 | static GrGeometryProcessor* TestCreate(SkRandom*, \ |
joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 80 | GrContext*, \ |
| 81 | const GrDrawTargetCaps&, \ |
| 82 | GrTexture* dummyTextures[2]) |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 83 | |
| 84 | #define GR_DECLARE_FRAGMENT_PROCESSOR_TEST \ |
joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 85 | static GrProcessorTestFactory<GrFragmentProcessor> gTestFactory SK_UNUSED; \ |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 86 | static GrFragmentProcessor* TestCreate(SkRandom*, \ |
joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 87 | GrContext*, \ |
| 88 | const GrDrawTargetCaps&, \ |
| 89 | GrTexture* dummyTextures[2]) |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 90 | |
| 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 |