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 | |
| 11 | #include "SkRandom.h" |
bsalomon@google.com | 2a48c3a | 2012-08-03 14:54:45 +0000 | [diff] [blame] | 12 | #include "GrNoncopyable.h" |
| 13 | #include "SkTArray.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; |
| 16 | |
bsalomon@google.com | 6f261be | 2012-10-24 19:07:10 +0000 | [diff] [blame] | 17 | namespace GrEffectUnitTest { |
bsalomon@google.com | 8d3d210 | 2012-08-03 18:49:51 +0000 | [diff] [blame] | 18 | // Used to access the dummy textures in TestCreate procs. |
| 19 | enum { |
| 20 | kSkiaPMTextureIdx = 0, |
| 21 | kAlphaTextureIdx = 1, |
| 22 | }; |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 23 | |
| 24 | /** |
| 25 | * A helper for use in GrEffect::TestCreate functions. |
| 26 | */ |
| 27 | const SkMatrix& TestMatrix(SkRandom*); |
| 28 | |
bsalomon@google.com | 8d3d210 | 2012-08-03 18:49:51 +0000 | [diff] [blame] | 29 | } |
| 30 | |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 31 | #if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS |
| 32 | |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 33 | class GrContext; |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 34 | class GrEffectRef; |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 35 | class GrTexture; |
| 36 | |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 37 | class GrEffectTestFactory : GrNoncopyable { |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 38 | public: |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 39 | |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 40 | typedef GrEffectRef* (*CreateProc)(SkRandom*, GrContext*, GrTexture* dummyTextures[]); |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 41 | |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 42 | GrEffectTestFactory(CreateProc createProc) { |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 43 | fCreateProc = createProc; |
| 44 | GetFactories()->push_back(this); |
| 45 | } |
| 46 | |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 47 | static GrEffectRef* CreateStage(SkRandom* random, |
| 48 | GrContext* context, |
| 49 | GrTexture* dummyTextures[]) { |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 50 | uint32_t idx = random->nextRangeU(0, GetFactories()->count() - 1); |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 51 | GrEffectTestFactory* factory = (*GetFactories())[idx]; |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 52 | return factory->fCreateProc(random, context, dummyTextures); |
| 53 | } |
| 54 | |
| 55 | private: |
| 56 | CreateProc fCreateProc; |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 57 | static SkTArray<GrEffectTestFactory*, true>* GetFactories(); |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 58 | }; |
| 59 | |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 60 | /** 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] | 61 | * program generation unit test. |
| 62 | */ |
bsalomon@google.com | f271cc7 | 2012-10-24 19:35:13 +0000 | [diff] [blame] | 63 | #define GR_DECLARE_EFFECT_TEST \ |
| 64 | static GrEffectTestFactory gTestFactory; \ |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 65 | static GrEffectRef* TestCreate(SkRandom*, GrContext*, GrTexture* dummyTextures[2]) |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 66 | |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 67 | /** 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] | 68 | * also implement this static function: |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 69 | * GrEffect* TestCreate(SkRandom*, GrContext*, GrTexture* dummyTextures[2]); |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 70 | * 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] | 71 | * The first texture has config kSkia8888_GrPixelConfig and the second has |
bsalomon@google.com | d8b5fac | 2012-11-01 17:02:46 +0000 | [diff] [blame] | 72 | * kAlpha_8_GrPixelConfig. TestCreate functions are also free to create additional textures using |
| 73 | * the GrContext. |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 74 | */ |
bsalomon@google.com | f271cc7 | 2012-10-24 19:35:13 +0000 | [diff] [blame] | 75 | #define GR_DEFINE_EFFECT_TEST(Effect) \ |
| 76 | GrEffectTestFactory Effect :: gTestFactory(Effect :: TestCreate) |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 77 | |
| 78 | #else // !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS |
| 79 | |
| 80 | // The unit test relies on static initializers. Just declare the TestCreate function so that |
| 81 | // its definitions will compile. |
bsalomon@google.com | f271cc7 | 2012-10-24 19:35:13 +0000 | [diff] [blame] | 82 | #define GR_DECLARE_EFFECT_TEST \ |
bsalomon@google.com | 87c4726 | 2013-01-16 15:38:37 +0000 | [diff] [blame] | 83 | static GrEffectRef* TestCreate(SkRandom*, GrContext*, GrTexture* dummyTextures[2]) |
bsalomon@google.com | f271cc7 | 2012-10-24 19:35:13 +0000 | [diff] [blame] | 84 | #define GR_DEFINE_EFFECT_TEST(X) |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 85 | |
| 86 | #endif // !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS |
reed@google.com | b932407 | 2012-08-06 14:37:22 +0000 | [diff] [blame] | 87 | #endif |