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 | 6f261be | 2012-10-24 19:07:10 +0000 | [diff] [blame^] | 15 | namespace GrEffectUnitTest { |
bsalomon@google.com | 8d3d210 | 2012-08-03 18:49:51 +0000 | [diff] [blame] | 16 | // Used to access the dummy textures in TestCreate procs. |
| 17 | enum { |
| 18 | kSkiaPMTextureIdx = 0, |
| 19 | kAlphaTextureIdx = 1, |
| 20 | }; |
| 21 | } |
| 22 | |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 23 | #if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS |
| 24 | |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 25 | class GrContext; |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 26 | class GrEffect; |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 27 | class GrTexture; |
| 28 | |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 29 | class GrEffectTestFactory : GrNoncopyable { |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 30 | public: |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 31 | |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 32 | typedef GrEffect* (*CreateProc)(SkRandom*, GrContext*, GrTexture* dummyTextures[]); |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 33 | |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 34 | GrEffectTestFactory(CreateProc createProc) { |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 35 | fCreateProc = createProc; |
| 36 | GetFactories()->push_back(this); |
| 37 | } |
| 38 | |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 39 | static GrEffect* CreateStage(SkRandom* random, |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 40 | GrContext* context, |
| 41 | GrTexture* dummyTextures[]) { |
| 42 | uint32_t idx = random->nextRangeU(0, GetFactories()->count() - 1); |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 43 | GrEffectTestFactory* factory = (*GetFactories())[idx]; |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 44 | return factory->fCreateProc(random, context, dummyTextures); |
| 45 | } |
| 46 | |
| 47 | private: |
| 48 | CreateProc fCreateProc; |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 49 | static SkTArray<GrEffectTestFactory*, true>* GetFactories(); |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 50 | }; |
| 51 | |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 52 | /** 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] | 53 | * program generation unit test. |
| 54 | */ |
| 55 | #define GR_DECLARE_CUSTOM_STAGE_TEST \ |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 56 | static GrEffectTestFactory gTestFactory; \ |
| 57 | static GrEffect* TestCreate(SkRandom*, GrContext*, GrTexture* dummyTextures[2]) |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 58 | |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 59 | /** GrEffect subclasses should insert this macro in their implemenation file. They must then |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 60 | * also implement this static function: |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 61 | * GrEffect* TestCreate(SkRandom*, GrContext*, GrTexture* dummyTextures[2]); |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 62 | * dummyTextures[] are valied textures that they can optionally use for their texture accesses. The |
| 63 | * first texture has config kSkia8888_PM_GrPixelConfig and the second has kAlpha_8_GrPixelConfig. |
| 64 | * TestCreate functions are also free to create additional textures using the GrContext. |
| 65 | */ |
| 66 | #define GR_DEFINE_CUSTOM_STAGE_TEST(CustomStage) \ |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 67 | GrEffectTestFactory CustomStage :: gTestFactory(CustomStage :: TestCreate) |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 68 | |
| 69 | #else // !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS |
| 70 | |
| 71 | // The unit test relies on static initializers. Just declare the TestCreate function so that |
| 72 | // its definitions will compile. |
| 73 | #define GR_DECLARE_CUSTOM_STAGE_TEST \ |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 74 | static GrEffect* TestCreate(SkRandom*, GrContext*, GrTexture* dummyTextures[2]) |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 75 | #define GR_DEFINE_CUSTOM_STAGE_TEST(X) |
| 76 | |
| 77 | #endif // !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS |
reed@google.com | b932407 | 2012-08-06 14:37:22 +0000 | [diff] [blame] | 78 | #endif |