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 GrEffectUnitTest_DEFINED 9 #define GrEffectUnitTest_DEFINED 10 11 #include "SkRandom.h" 12 #include "GrNoncopyable.h" 13 #include "SkTArray.h" 14 15 class SkMatrix; 16 17 namespace GrEffectUnitTest { 18 // Used to access the dummy textures in TestCreate procs. 19 enum { 20 kSkiaPMTextureIdx = 0, 21 kAlphaTextureIdx = 1, 22 }; 23 24 /** 25 * A helper for use in GrEffect::TestCreate functions. 26 */ 27 const SkMatrix& TestMatrix(SkRandom*); 28 29 } 30 31 #if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS 32 33 class GrContext; 34 class GrEffectRef; 35 class GrTexture; 36 37 class GrEffectTestFactory : GrNoncopyable { 38 public: 39 40 typedef GrEffectRef* (*CreateProc)(SkRandom*, GrContext*, GrTexture* dummyTextures[]); 41 GrEffectTestFactory(CreateProc createProc)42 GrEffectTestFactory(CreateProc createProc) { 43 fCreateProc = createProc; 44 GetFactories()->push_back(this); 45 } 46 CreateStage(SkRandom * random,GrContext * context,GrTexture * dummyTextures[])47 static GrEffectRef* CreateStage(SkRandom* random, 48 GrContext* context, 49 GrTexture* dummyTextures[]) { 50 uint32_t idx = random->nextRangeU(0, GetFactories()->count() - 1); 51 GrEffectTestFactory* factory = (*GetFactories())[idx]; 52 return factory->fCreateProc(random, context, dummyTextures); 53 } 54 55 private: 56 CreateProc fCreateProc; 57 static SkTArray<GrEffectTestFactory*, true>* GetFactories(); 58 }; 59 60 /** GrEffect subclasses should insert this macro in their declaration to be included in the 61 * program generation unit test. 62 */ 63 #define GR_DECLARE_EFFECT_TEST \ 64 static GrEffectTestFactory gTestFactory; \ 65 static GrEffectRef* TestCreate(SkRandom*, GrContext*, GrTexture* dummyTextures[2]) 66 67 /** GrEffect subclasses should insert this macro in their implementation file. They must then 68 * also implement this static function: 69 * GrEffect* TestCreate(SkRandom*, GrContext*, GrTexture* dummyTextures[2]); 70 * dummyTextures[] are valid textures that can optionally be used to construct GrTextureAccesses. 71 * The first texture has config kSkia8888_PM_GrPixelConfig and the second has 72 * kAlpha_8_GrPixelConfig. TestCreate functions are also free to create additional textures using 73 * the GrContext. 74 */ 75 #define GR_DEFINE_EFFECT_TEST(Effect) \ 76 GrEffectTestFactory Effect :: gTestFactory(Effect :: TestCreate) 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. 82 #define GR_DECLARE_EFFECT_TEST \ 83 static GrEffectRef* TestCreate(SkRandom*, GrContext*, GrTexture* dummyTextures[2]) 84 #define GR_DEFINE_EFFECT_TEST(X) 85 86 #endif // !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS 87 #endif 88