| 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 | 4eaf9ce | 2015-04-28 13:31:18 -0700 | [diff] [blame] | 11 | #include "GrTestUtils.h" |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 12 | #include "SkTArray.h" |
| 13 | #include "SkTypes.h" |
| 14 | |
| 15 | class SkMatrix; |
| bsalomon | 4b91f76 | 2015-05-19 09:29:46 -0700 | [diff] [blame] | 16 | class GrCaps; |
| joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 17 | class GrContext; |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 18 | |
| 19 | namespace GrProcessorUnitTest { |
| 20 | // Used to access the dummy textures in TestCreate procs. |
| 21 | enum { |
| 22 | kSkiaPMTextureIdx = 0, |
| 23 | kAlphaTextureIdx = 1, |
| 24 | }; |
| 25 | |
| joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 26 | } |
| 27 | |
| joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 28 | /* |
| 29 | * GrProcessorTestData is an argument struct to TestCreate functions |
| 30 | * fTextures are valid textures that can optionally be used to construct |
| 31 | * GrTextureAccesses. The first texture has config kSkia8888_GrPixelConfig and the second has |
| 32 | * kAlpha_8_GrPixelConfig. TestCreate functions are also free to create additional textures using |
| 33 | * the GrContext. |
| 34 | */ |
| 35 | struct GrProcessorTestData { |
| 36 | GrProcessorTestData(SkRandom* random, |
| 37 | GrContext* context, |
| 38 | GrShaderDataManager* shaderDataManager, |
| 39 | const GrCaps* caps, |
| 40 | GrTexture* textures[2]) |
| 41 | : fRandom(random) |
| 42 | , fContext(context) |
| 43 | , fShaderDataManager(shaderDataManager) |
| 44 | , fCaps(caps) { |
| 45 | fTextures[0] = textures[0]; |
| 46 | fTextures[1] = textures[1]; |
| 47 | } |
| 48 | SkRandom* fRandom; |
| 49 | GrContext* fContext; |
| 50 | GrShaderDataManager* fShaderDataManager; |
| 51 | const GrCaps* fCaps; |
| 52 | GrTexture* fTextures[2]; |
| 53 | }; |
| 54 | |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 55 | #if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS |
| 56 | |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 57 | class GrProcessor; |
| 58 | class GrTexture; |
| 59 | |
| 60 | template <class Processor> |
| 61 | class GrProcessorTestFactory : SkNoncopyable { |
| 62 | public: |
| 63 | |
| joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 64 | typedef Processor* (*CreateProc)(GrProcessorTestData*); |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 65 | |
| 66 | GrProcessorTestFactory(CreateProc createProc) { |
| 67 | fCreateProc = createProc; |
| 68 | GetFactories()->push_back(this); |
| 69 | } |
| 70 | |
| joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 71 | static Processor* CreateStage(GrProcessorTestData* data) { |
| joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 72 | VerifyFactoryCount(); |
| 73 | SkASSERT(GetFactories()->count()); |
| joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 74 | uint32_t idx = data->fRandom->nextRangeU(0, GetFactories()->count() - 1); |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 75 | GrProcessorTestFactory<Processor>* factory = (*GetFactories())[idx]; |
| joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 76 | return factory->fCreateProc(data); |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 79 | /* |
| 80 | * A test function which verifies the count of factories. |
| 81 | */ |
| 82 | static void VerifyFactoryCount(); |
| 83 | |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 84 | private: |
| 85 | CreateProc fCreateProc; |
| 86 | |
| joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 87 | static SkTArray<GrProcessorTestFactory<Processor>*, true>* GetFactories(); |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | /** GrProcessor subclasses should insert this macro in their declaration to be included in the |
| 91 | * program generation unit test. |
| 92 | */ |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 93 | #define GR_DECLARE_GEOMETRY_PROCESSOR_TEST \ |
| joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 94 | static GrProcessorTestFactory<GrGeometryProcessor> gTestFactory SK_UNUSED; \ |
| joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 95 | static GrGeometryProcessor* TestCreate(GrProcessorTestData*) |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 96 | |
| 97 | #define GR_DECLARE_FRAGMENT_PROCESSOR_TEST \ |
| joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 98 | static GrProcessorTestFactory<GrFragmentProcessor> gTestFactory SK_UNUSED; \ |
| joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 99 | static GrFragmentProcessor* TestCreate(GrProcessorTestData*) |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 100 | |
| egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 101 | #define GR_DECLARE_XP_FACTORY_TEST \ |
| 102 | static GrProcessorTestFactory<GrXPFactory> gTestFactory SK_UNUSED; \ |
| joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 103 | static GrXPFactory* TestCreate(GrProcessorTestData*) |
| egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 104 | |
| 105 | |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 106 | /** GrProcessor subclasses should insert this macro in their implementation file. They must then |
| 107 | * also implement this static function: |
| joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 108 | * GrProcessor* TestCreate(GrProcessorTestData*); |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 109 | */ |
| 110 | #define GR_DEFINE_FRAGMENT_PROCESSOR_TEST(Effect) \ |
| 111 | GrProcessorTestFactory<GrFragmentProcessor> Effect :: gTestFactory(Effect :: TestCreate) |
| 112 | |
| egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 113 | #define GR_DEFINE_XP_FACTORY_TEST(Factory) \ |
| 114 | GrProcessorTestFactory<GrXPFactory> Factory :: gTestFactory(Factory :: TestCreate) |
| 115 | |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 116 | #define GR_DEFINE_GEOMETRY_PROCESSOR_TEST(Effect) \ |
| 117 | GrProcessorTestFactory<GrGeometryProcessor> Effect :: gTestFactory(Effect :: TestCreate) |
| 118 | |
| 119 | #else // !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS |
| 120 | |
| 121 | // The unit test relies on static initializers. Just declare the TestCreate function so that |
| 122 | // its definitions will compile. |
| 123 | #define GR_DECLARE_FRAGMENT_PROCESSOR_TEST \ |
| joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 124 | static GrFragmentProcessor* TestCreate(GrProcessorTestData*) |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 125 | #define GR_DEFINE_FRAGMENT_PROCESSOR_TEST(X) |
| 126 | |
| 127 | // The unit test relies on static initializers. Just declare the TestCreate function so that |
| 128 | // its definitions will compile. |
| egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 129 | #define GR_DECLARE_XP_FACTORY_TEST \ |
| joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 130 | static GrXPFactory* TestCreate(GrProcessorTestData*) |
| egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 131 | #define GR_DEFINE_XP_FACTORY_TEST(X) |
| 132 | |
| 133 | // The unit test relies on static initializers. Just declare the TestCreate function so that |
| 134 | // its definitions will compile. |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 135 | #define GR_DECLARE_GEOMETRY_PROCESSOR_TEST \ |
| joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 136 | static GrGeometryProcessor* TestCreate(GrProcessorTestData*) |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 137 | #define GR_DEFINE_GEOMETRY_PROCESSOR_TEST(X) |
| 138 | |
| 139 | #endif // !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS |
| 140 | #endif |