bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [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 | |
bsalomon@google.com | 6f261be | 2012-10-24 19:07:10 +0000 | [diff] [blame] | 8 | #ifndef GrEffectUnitTest_DEFINED |
| 9 | #define GrEffectUnitTest_DEFINED |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 10 | |
bsalomon@google.com | c26d94f | 2013-03-25 18:19:00 +0000 | [diff] [blame] | 11 | #include "SkRandom.h" |
bsalomon@google.com | 2a48c3a | 2012-08-03 14:54:45 +0000 | [diff] [blame] | 12 | #include "SkTArray.h" |
commit-bot@chromium.org | a0b4028 | 2013-09-18 13:00:55 +0000 | [diff] [blame] | 13 | #include "SkTypes.h" |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 14 | |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 15 | class SkMatrix; |
bsalomon@google.com | c26d94f | 2013-03-25 18:19:00 +0000 | [diff] [blame] | 16 | class GrDrawTargetCaps; |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 17 | |
bsalomon@google.com | 6f261be | 2012-10-24 19:07:10 +0000 | [diff] [blame] | 18 | namespace GrEffectUnitTest { |
bsalomon@google.com | 8d3d210 | 2012-08-03 18:49:51 +0000 | [diff] [blame] | 19 | // Used to access the dummy textures in TestCreate procs. |
| 20 | enum { |
| 21 | kSkiaPMTextureIdx = 0, |
| 22 | kAlphaTextureIdx = 1, |
| 23 | }; |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 24 | |
| 25 | /** |
| 26 | * A helper for use in GrEffect::TestCreate functions. |
| 27 | */ |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 28 | const SkMatrix& TestMatrix(SkRandom*); |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 29 | |
bsalomon@google.com | 8d3d210 | 2012-08-03 18:49:51 +0000 | [diff] [blame] | 30 | } |
| 31 | |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 32 | #if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS |
| 33 | |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 34 | class GrContext; |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 35 | class GrEffectRef; |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 36 | class GrTexture; |
| 37 | |
commit-bot@chromium.org | e3beb6b | 2014-04-07 19:34:38 +0000 | [diff] [blame^] | 38 | class GrEffectTestFactory : SkNoncopyable { |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 39 | public: |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 40 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 41 | typedef GrEffectRef* (*CreateProc)(SkRandom*, |
bsalomon@google.com | c26d94f | 2013-03-25 18:19:00 +0000 | [diff] [blame] | 42 | GrContext*, |
| 43 | const GrDrawTargetCaps& caps, |
| 44 | GrTexture* dummyTextures[]); |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 45 | |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 46 | GrEffectTestFactory(CreateProc createProc) { |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 47 | fCreateProc = createProc; |
| 48 | GetFactories()->push_back(this); |
| 49 | } |
| 50 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 51 | static GrEffectRef* CreateStage(SkRandom* random, |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 52 | GrContext* context, |
bsalomon@google.com | c26d94f | 2013-03-25 18:19:00 +0000 | [diff] [blame] | 53 | const GrDrawTargetCaps& caps, |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 54 | GrTexture* dummyTextures[]) { |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 55 | uint32_t idx = random->nextRangeU(0, GetFactories()->count() - 1); |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 56 | GrEffectTestFactory* factory = (*GetFactories())[idx]; |
bsalomon@google.com | c26d94f | 2013-03-25 18:19:00 +0000 | [diff] [blame] | 57 | return factory->fCreateProc(random, context, caps, dummyTextures); |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | private: |
| 61 | CreateProc fCreateProc; |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 62 | static SkTArray<GrEffectTestFactory*, true>* GetFactories(); |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 63 | }; |
| 64 | |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 65 | /** GrEffect subclasses should insert this macro in their declaration to be included in the |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 66 | * program generation unit test. |
| 67 | */ |
bsalomon@google.com | f271cc7 | 2012-10-24 19:35:13 +0000 | [diff] [blame] | 68 | #define GR_DECLARE_EFFECT_TEST \ |
| 69 | static GrEffectTestFactory gTestFactory; \ |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 70 | static GrEffectRef* TestCreate(SkRandom*, \ |
bsalomon@google.com | c26d94f | 2013-03-25 18:19:00 +0000 | [diff] [blame] | 71 | GrContext*, \ |
| 72 | const GrDrawTargetCaps&, \ |
| 73 | GrTexture* dummyTextures[2]) |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 74 | |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 75 | /** GrEffect subclasses should insert this macro in their implementation file. They must then |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 76 | * also implement this static function: |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 77 | * GrEffect* TestCreate(SkRandom*, |
bsalomon@google.com | c26d94f | 2013-03-25 18:19:00 +0000 | [diff] [blame] | 78 | * GrContext*, |
| 79 | * const GrDrawTargetCaps&, |
| 80 | * GrTexture* dummyTextures[2]); |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 81 | * dummyTextures[] are valid textures that can optionally be used to construct GrTextureAccesses. |
bsalomon@google.com | fec0bc3 | 2013-02-07 14:43:04 +0000 | [diff] [blame] | 82 | * The first texture has config kSkia8888_GrPixelConfig and the second has |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 83 | * kAlpha_8_GrPixelConfig. TestCreate functions are also free to create additional textures using |
| 84 | * the GrContext. |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 85 | */ |
bsalomon@google.com | f271cc7 | 2012-10-24 19:35:13 +0000 | [diff] [blame] | 86 | #define GR_DEFINE_EFFECT_TEST(Effect) \ |
| 87 | GrEffectTestFactory Effect :: gTestFactory(Effect :: TestCreate) |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 88 | |
| 89 | #else // !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS |
| 90 | |
| 91 | // The unit test relies on static initializers. Just declare the TestCreate function so that |
| 92 | // its definitions will compile. |
bsalomon@google.com | c26d94f | 2013-03-25 18:19:00 +0000 | [diff] [blame] | 93 | #define GR_DECLARE_EFFECT_TEST \ |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 94 | static GrEffectRef* TestCreate(SkRandom*, \ |
bsalomon@google.com | c26d94f | 2013-03-25 18:19:00 +0000 | [diff] [blame] | 95 | GrContext*, \ |
| 96 | const GrDrawTargetCaps&, \ |
| 97 | GrTexture* dummyTextures[2]) |
bsalomon@google.com | f271cc7 | 2012-10-24 19:35:13 +0000 | [diff] [blame] | 98 | #define GR_DEFINE_EFFECT_TEST(X) |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 99 | |
| 100 | #endif // !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS |
reed@google.com | b932407 | 2012-08-06 14:37:22 +0000 | [diff] [blame] | 101 | #endif |