blob: ffc64f2b8268b5c646a9d92529d94346e294c69a [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
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000011#include "SkRandom.h"
bsalomon@google.com2a48c3a2012-08-03 14:54:45 +000012#include "SkTArray.h"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000013#include "SkTypes.h"
bsalomon@google.comd4726202012-08-03 14:34:46 +000014
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000015class SkMatrix;
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000016class GrDrawTargetCaps;
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000017
bsalomon@google.com6f261be2012-10-24 19:07:10 +000018namespace GrEffectUnitTest {
bsalomon@google.com8d3d2102012-08-03 18:49:51 +000019// Used to access the dummy textures in TestCreate procs.
20enum {
21 kSkiaPMTextureIdx = 0,
22 kAlphaTextureIdx = 1,
23};
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000024
25/**
26 * A helper for use in GrEffect::TestCreate functions.
27 */
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000028const SkMatrix& TestMatrix(SkRandom*);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000029
bsalomon@google.com8d3d2102012-08-03 18:49:51 +000030}
31
bsalomon@google.comd4726202012-08-03 14:34:46 +000032#if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
33
bsalomon@google.comd4726202012-08-03 14:34:46 +000034class GrContext;
bsalomon97b9ab72014-07-08 06:52:35 -070035class GrEffect;
bsalomon@google.comd4726202012-08-03 14:34:46 +000036class GrTexture;
37
commit-bot@chromium.orge3beb6b2014-04-07 19:34:38 +000038class GrEffectTestFactory : SkNoncopyable {
bsalomon@google.comd4726202012-08-03 14:34:46 +000039public:
bsalomon@google.comd4726202012-08-03 14:34:46 +000040
bsalomon97b9ab72014-07-08 06:52:35 -070041 typedef GrEffect* (*CreateProc)(SkRandom*,
42 GrContext*,
43 const GrDrawTargetCaps& caps,
44 GrTexture* dummyTextures[]);
bsalomon@google.comd4726202012-08-03 14:34:46 +000045
bsalomon@google.coma469c282012-10-24 18:28:34 +000046 GrEffectTestFactory(CreateProc createProc) {
bsalomon@google.comd4726202012-08-03 14:34:46 +000047 fCreateProc = createProc;
48 GetFactories()->push_back(this);
49 }
50
bsalomon97b9ab72014-07-08 06:52:35 -070051 static GrEffect* CreateStage(SkRandom* random,
52 GrContext* context,
53 const GrDrawTargetCaps& caps,
54 GrTexture* dummyTextures[]) {
bsalomon@google.comd4726202012-08-03 14:34:46 +000055 uint32_t idx = random->nextRangeU(0, GetFactories()->count() - 1);
bsalomon@google.coma469c282012-10-24 18:28:34 +000056 GrEffectTestFactory* factory = (*GetFactories())[idx];
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000057 return factory->fCreateProc(random, context, caps, dummyTextures);
bsalomon@google.comd4726202012-08-03 14:34:46 +000058 }
59
60private:
61 CreateProc fCreateProc;
bsalomon@google.coma469c282012-10-24 18:28:34 +000062 static SkTArray<GrEffectTestFactory*, true>* GetFactories();
bsalomon@google.comd4726202012-08-03 14:34:46 +000063};
64
bsalomon@google.coma469c282012-10-24 18:28:34 +000065/** GrEffect subclasses should insert this macro in their declaration to be included in the
bsalomon@google.comd4726202012-08-03 14:34:46 +000066 * program generation unit test.
67 */
bsalomon@google.comf271cc72012-10-24 19:35:13 +000068#define GR_DECLARE_EFFECT_TEST \
69 static GrEffectTestFactory gTestFactory; \
bsalomon97b9ab72014-07-08 06:52:35 -070070 static GrEffect* TestCreate(SkRandom*, \
71 GrContext*, \
72 const GrDrawTargetCaps&, \
73 GrTexture* dummyTextures[2])
bsalomon@google.comd4726202012-08-03 14:34:46 +000074
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000075/** GrEffect subclasses should insert this macro in their implementation file. They must then
bsalomon@google.comd4726202012-08-03 14:34:46 +000076 * also implement this static function:
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000077 * GrEffect* TestCreate(SkRandom*,
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000078 * GrContext*,
79 * const GrDrawTargetCaps&,
80 * GrTexture* dummyTextures[2]);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000081 * dummyTextures[] are valid textures that can optionally be used to construct GrTextureAccesses.
bsalomon@google.comfec0bc32013-02-07 14:43:04 +000082 * The first texture has config kSkia8888_GrPixelConfig and the second has
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000083 * kAlpha_8_GrPixelConfig. TestCreate functions are also free to create additional textures using
84 * the GrContext.
bsalomon@google.comd4726202012-08-03 14:34:46 +000085 */
bsalomon@google.comf271cc72012-10-24 19:35:13 +000086#define GR_DEFINE_EFFECT_TEST(Effect) \
87 GrEffectTestFactory Effect :: gTestFactory(Effect :: TestCreate)
bsalomon@google.comd4726202012-08-03 14:34:46 +000088
89#else // !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
90
91// The unit test relies on static initializers. Just declare the TestCreate function so that
92// its definitions will compile.
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000093#define GR_DECLARE_EFFECT_TEST \
bsalomon97b9ab72014-07-08 06:52:35 -070094 static GrEffect* TestCreate(SkRandom*, \
95 GrContext*, \
96 const GrDrawTargetCaps&, \
97 GrTexture* dummyTextures[2])
bsalomon@google.comf271cc72012-10-24 19:35:13 +000098#define GR_DEFINE_EFFECT_TEST(X)
bsalomon@google.comd4726202012-08-03 14:34:46 +000099
100#endif // !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
reed@google.comb9324072012-08-06 14:37:22 +0000101#endif