blob: c18bdd0990d15cef7083ebd85fbd2e370b937ec3 [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.com6f261be2012-10-24 19:07:10 +000015namespace GrEffectUnitTest {
bsalomon@google.com8d3d2102012-08-03 18:49:51 +000016// Used to access the dummy textures in TestCreate procs.
17enum {
18 kSkiaPMTextureIdx = 0,
19 kAlphaTextureIdx = 1,
20};
21}
22
bsalomon@google.comd4726202012-08-03 14:34:46 +000023#if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
24
bsalomon@google.comd4726202012-08-03 14:34:46 +000025class GrContext;
bsalomon@google.coma469c282012-10-24 18:28:34 +000026class GrEffect;
bsalomon@google.comd4726202012-08-03 14:34:46 +000027class GrTexture;
28
bsalomon@google.coma469c282012-10-24 18:28:34 +000029class GrEffectTestFactory : GrNoncopyable {
bsalomon@google.comd4726202012-08-03 14:34:46 +000030public:
bsalomon@google.comd4726202012-08-03 14:34:46 +000031
bsalomon@google.coma469c282012-10-24 18:28:34 +000032 typedef GrEffect* (*CreateProc)(SkRandom*, GrContext*, GrTexture* dummyTextures[]);
bsalomon@google.comd4726202012-08-03 14:34:46 +000033
bsalomon@google.coma469c282012-10-24 18:28:34 +000034 GrEffectTestFactory(CreateProc createProc) {
bsalomon@google.comd4726202012-08-03 14:34:46 +000035 fCreateProc = createProc;
36 GetFactories()->push_back(this);
37 }
38
bsalomon@google.coma469c282012-10-24 18:28:34 +000039 static GrEffect* CreateStage(SkRandom* random,
bsalomon@google.comd4726202012-08-03 14:34:46 +000040 GrContext* context,
41 GrTexture* dummyTextures[]) {
42 uint32_t idx = random->nextRangeU(0, GetFactories()->count() - 1);
bsalomon@google.coma469c282012-10-24 18:28:34 +000043 GrEffectTestFactory* factory = (*GetFactories())[idx];
bsalomon@google.comd4726202012-08-03 14:34:46 +000044 return factory->fCreateProc(random, context, dummyTextures);
45 }
46
47private:
48 CreateProc fCreateProc;
bsalomon@google.coma469c282012-10-24 18:28:34 +000049 static SkTArray<GrEffectTestFactory*, true>* GetFactories();
bsalomon@google.comd4726202012-08-03 14:34:46 +000050};
51
bsalomon@google.coma469c282012-10-24 18:28:34 +000052/** GrEffect subclasses should insert this macro in their declaration to be included in the
bsalomon@google.comd4726202012-08-03 14:34:46 +000053 * program generation unit test.
54 */
55#define GR_DECLARE_CUSTOM_STAGE_TEST \
bsalomon@google.coma469c282012-10-24 18:28:34 +000056 static GrEffectTestFactory gTestFactory; \
57 static GrEffect* TestCreate(SkRandom*, GrContext*, GrTexture* dummyTextures[2])
bsalomon@google.comd4726202012-08-03 14:34:46 +000058
bsalomon@google.coma469c282012-10-24 18:28:34 +000059/** GrEffect subclasses should insert this macro in their implemenation file. They must then
bsalomon@google.comd4726202012-08-03 14:34:46 +000060 * also implement this static function:
bsalomon@google.coma469c282012-10-24 18:28:34 +000061 * GrEffect* TestCreate(SkRandom*, GrContext*, GrTexture* dummyTextures[2]);
bsalomon@google.comd4726202012-08-03 14:34:46 +000062 * 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.coma469c282012-10-24 18:28:34 +000067 GrEffectTestFactory CustomStage :: gTestFactory(CustomStage :: TestCreate)
bsalomon@google.comd4726202012-08-03 14:34:46 +000068
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.coma469c282012-10-24 18:28:34 +000074 static GrEffect* TestCreate(SkRandom*, GrContext*, GrTexture* dummyTextures[2])
bsalomon@google.comd4726202012-08-03 14:34:46 +000075#define GR_DEFINE_CUSTOM_STAGE_TEST(X)
76
77#endif // !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
reed@google.comb9324072012-08-06 14:37:22 +000078#endif