michael@0: /* michael@0: * Copyright 2012 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #ifndef GrEffectUnitTest_DEFINED michael@0: #define GrEffectUnitTest_DEFINED michael@0: michael@0: #include "SkRandom.h" michael@0: #include "SkTArray.h" michael@0: #include "SkTypes.h" michael@0: michael@0: class SkMatrix; michael@0: class GrDrawTargetCaps; michael@0: michael@0: namespace GrEffectUnitTest { michael@0: // Used to access the dummy textures in TestCreate procs. michael@0: enum { michael@0: kSkiaPMTextureIdx = 0, michael@0: kAlphaTextureIdx = 1, michael@0: }; michael@0: michael@0: /** michael@0: * A helper for use in GrEffect::TestCreate functions. michael@0: */ michael@0: const SkMatrix& TestMatrix(SkRandom*); michael@0: michael@0: } michael@0: michael@0: #if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS michael@0: michael@0: class GrContext; michael@0: class GrEffectRef; michael@0: class GrTexture; michael@0: michael@0: class GrEffectTestFactory : public SkNoncopyable { michael@0: public: michael@0: michael@0: typedef GrEffectRef* (*CreateProc)(SkRandom*, michael@0: GrContext*, michael@0: const GrDrawTargetCaps& caps, michael@0: GrTexture* dummyTextures[]); michael@0: michael@0: GrEffectTestFactory(CreateProc createProc) { michael@0: fCreateProc = createProc; michael@0: GetFactories()->push_back(this); michael@0: } michael@0: michael@0: static GrEffectRef* CreateStage(SkRandom* random, michael@0: GrContext* context, michael@0: const GrDrawTargetCaps& caps, michael@0: GrTexture* dummyTextures[]) { michael@0: uint32_t idx = random->nextRangeU(0, GetFactories()->count() - 1); michael@0: GrEffectTestFactory* factory = (*GetFactories())[idx]; michael@0: return factory->fCreateProc(random, context, caps, dummyTextures); michael@0: } michael@0: michael@0: private: michael@0: CreateProc fCreateProc; michael@0: static SkTArray* GetFactories(); michael@0: }; michael@0: michael@0: /** GrEffect subclasses should insert this macro in their declaration to be included in the michael@0: * program generation unit test. michael@0: */ michael@0: #define GR_DECLARE_EFFECT_TEST \ michael@0: static GrEffectTestFactory gTestFactory; \ michael@0: static GrEffectRef* TestCreate(SkRandom*, \ michael@0: GrContext*, \ michael@0: const GrDrawTargetCaps&, \ michael@0: GrTexture* dummyTextures[2]) michael@0: michael@0: /** GrEffect subclasses should insert this macro in their implementation file. They must then michael@0: * also implement this static function: michael@0: * GrEffect* TestCreate(SkRandom*, michael@0: * GrContext*, michael@0: * const GrDrawTargetCaps&, michael@0: * GrTexture* dummyTextures[2]); michael@0: * dummyTextures[] are valid textures that can optionally be used to construct GrTextureAccesses. michael@0: * The first texture has config kSkia8888_GrPixelConfig and the second has michael@0: * kAlpha_8_GrPixelConfig. TestCreate functions are also free to create additional textures using michael@0: * the GrContext. michael@0: */ michael@0: #define GR_DEFINE_EFFECT_TEST(Effect) \ michael@0: GrEffectTestFactory Effect :: gTestFactory(Effect :: TestCreate) michael@0: michael@0: #else // !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS michael@0: michael@0: // The unit test relies on static initializers. Just declare the TestCreate function so that michael@0: // its definitions will compile. michael@0: #define GR_DECLARE_EFFECT_TEST \ michael@0: static GrEffectRef* TestCreate(SkRandom*, \ michael@0: GrContext*, \ michael@0: const GrDrawTargetCaps&, \ michael@0: GrTexture* dummyTextures[2]) michael@0: #define GR_DEFINE_EFFECT_TEST(X) michael@0: michael@0: #endif // !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS michael@0: #endif