blob: 03a302c3b216bdf228a31fe8ae31352a00a46928 [file] [log] [blame]
bsalomon@google.comd4726202012-08-03 14:34:46 +00001/*
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.com6f261be2012-10-24 19:07:10 +00008#ifndef GrEffectUnitTest_DEFINED
9#define GrEffectUnitTest_DEFINED
bsalomon@google.comd4726202012-08-03 14:34:46 +000010
11#include "SkRandom.h"
bsalomon@google.com2a48c3a2012-08-03 14:54:45 +000012#include "GrNoncopyable.h"
13#include "SkTArray.h"
bsalomon@google.comd4726202012-08-03 14:34:46 +000014
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000015class SkMatrix;
16
bsalomon@google.com6f261be2012-10-24 19:07:10 +000017namespace GrEffectUnitTest {
bsalomon@google.com8d3d2102012-08-03 18:49:51 +000018// Used to access the dummy textures in TestCreate procs.
19enum {
20 kSkiaPMTextureIdx = 0,
21 kAlphaTextureIdx = 1,
22};
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000023
24/**
25 * A helper for use in GrEffect::TestCreate functions.
26 */
bsalomon@google.com73a96942013-02-13 16:31:19 +000027const SkMatrix& TestMatrix(SkMWCRandom*);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000028
bsalomon@google.com8d3d2102012-08-03 18:49:51 +000029}
30
bsalomon@google.comd4726202012-08-03 14:34:46 +000031#if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
32
bsalomon@google.comd4726202012-08-03 14:34:46 +000033class GrContext;
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000034class GrEffectRef;
bsalomon@google.comd4726202012-08-03 14:34:46 +000035class GrTexture;
36
bsalomon@google.coma469c282012-10-24 18:28:34 +000037class GrEffectTestFactory : GrNoncopyable {
bsalomon@google.comd4726202012-08-03 14:34:46 +000038public:
bsalomon@google.comd4726202012-08-03 14:34:46 +000039
bsalomon@google.com73a96942013-02-13 16:31:19 +000040 typedef GrEffectRef* (*CreateProc)(SkMWCRandom*, GrContext*, GrTexture* dummyTextures[]);
bsalomon@google.comd4726202012-08-03 14:34:46 +000041
bsalomon@google.coma469c282012-10-24 18:28:34 +000042 GrEffectTestFactory(CreateProc createProc) {
bsalomon@google.comd4726202012-08-03 14:34:46 +000043 fCreateProc = createProc;
44 GetFactories()->push_back(this);
45 }
46
bsalomon@google.com73a96942013-02-13 16:31:19 +000047 static GrEffectRef* CreateStage(SkMWCRandom* random,
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000048 GrContext* context,
49 GrTexture* dummyTextures[]) {
bsalomon@google.comd4726202012-08-03 14:34:46 +000050 uint32_t idx = random->nextRangeU(0, GetFactories()->count() - 1);
bsalomon@google.coma469c282012-10-24 18:28:34 +000051 GrEffectTestFactory* factory = (*GetFactories())[idx];
bsalomon@google.comd4726202012-08-03 14:34:46 +000052 return factory->fCreateProc(random, context, dummyTextures);
53 }
54
55private:
56 CreateProc fCreateProc;
bsalomon@google.coma469c282012-10-24 18:28:34 +000057 static SkTArray<GrEffectTestFactory*, true>* GetFactories();
bsalomon@google.comd4726202012-08-03 14:34:46 +000058};
59
bsalomon@google.coma469c282012-10-24 18:28:34 +000060/** GrEffect subclasses should insert this macro in their declaration to be included in the
bsalomon@google.comd4726202012-08-03 14:34:46 +000061 * program generation unit test.
62 */
bsalomon@google.comf271cc72012-10-24 19:35:13 +000063#define GR_DECLARE_EFFECT_TEST \
64 static GrEffectTestFactory gTestFactory; \
bsalomon@google.com73a96942013-02-13 16:31:19 +000065 static GrEffectRef* TestCreate(SkMWCRandom*, GrContext*, GrTexture* dummyTextures[2])
bsalomon@google.comd4726202012-08-03 14:34:46 +000066
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000067/** GrEffect subclasses should insert this macro in their implementation file. They must then
bsalomon@google.comd4726202012-08-03 14:34:46 +000068 * also implement this static function:
bsalomon@google.com73a96942013-02-13 16:31:19 +000069 * GrEffect* TestCreate(SkMWCRandom*, GrContext*, GrTexture* dummyTextures[2]);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000070 * dummyTextures[] are valid textures that can optionally be used to construct GrTextureAccesses.
bsalomon@google.comfec0bc32013-02-07 14:43:04 +000071 * The first texture has config kSkia8888_GrPixelConfig and the second has
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000072 * kAlpha_8_GrPixelConfig. TestCreate functions are also free to create additional textures using
73 * the GrContext.
bsalomon@google.comd4726202012-08-03 14:34:46 +000074 */
bsalomon@google.comf271cc72012-10-24 19:35:13 +000075#define GR_DEFINE_EFFECT_TEST(Effect) \
76 GrEffectTestFactory Effect :: gTestFactory(Effect :: TestCreate)
bsalomon@google.comd4726202012-08-03 14:34:46 +000077
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.comf271cc72012-10-24 19:35:13 +000082#define GR_DECLARE_EFFECT_TEST \
bsalomon@google.com73a96942013-02-13 16:31:19 +000083 static GrEffectRef* TestCreate(SkMWCRandom*, GrContext*, GrTexture* dummyTextures[2])
bsalomon@google.comf271cc72012-10-24 19:35:13 +000084#define GR_DEFINE_EFFECT_TEST(X)
bsalomon@google.comd4726202012-08-03 14:34:46 +000085
86#endif // !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
reed@google.comb9324072012-08-06 14:37:22 +000087#endif