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