blob: 66ba2396029724925e1214c0f4729b23098e3855 [file] [log] [blame]
joshualittb0a8a372014-09-23 09:50:21 -07001/*
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 GrProcessorUnitTest_DEFINED
9#define GrProcessorUnitTest_DEFINED
10
joshualitt4eaf9ce2015-04-28 13:31:18 -070011#include "GrTestUtils.h"
joshualittb0a8a372014-09-23 09:50:21 -070012#include "SkTArray.h"
13#include "SkTypes.h"
14
15class SkMatrix;
bsalomon4b91f762015-05-19 09:29:46 -070016class GrCaps;
joshualitt0067ff52015-07-08 14:26:19 -070017class GrContext;
joshualittb0a8a372014-09-23 09:50:21 -070018
19namespace GrProcessorUnitTest {
20// Used to access the dummy textures in TestCreate procs.
21enum {
22 kSkiaPMTextureIdx = 0,
23 kAlphaTextureIdx = 1,
24};
25
joshualitt2e3b3e32014-12-09 13:31:14 -080026}
27
joshualitt0067ff52015-07-08 14:26:19 -070028/*
29 * GrProcessorTestData is an argument struct to TestCreate functions
30 * fTextures are valid textures that can optionally be used to construct
31 * GrTextureAccesses. The first texture has config kSkia8888_GrPixelConfig and the second has
32 * kAlpha_8_GrPixelConfig. TestCreate functions are also free to create additional textures using
33 * the GrContext.
34 */
35struct GrProcessorTestData {
36 GrProcessorTestData(SkRandom* random,
37 GrContext* context,
joshualitt9cc17752015-07-09 06:28:14 -070038 GrProcessorDataManager* procDataManager,
joshualitt0067ff52015-07-08 14:26:19 -070039 const GrCaps* caps,
40 GrTexture* textures[2])
41 : fRandom(random)
42 , fContext(context)
joshualitt9cc17752015-07-09 06:28:14 -070043 , fProcDataManager(procDataManager)
joshualitt0067ff52015-07-08 14:26:19 -070044 , fCaps(caps) {
45 fTextures[0] = textures[0];
46 fTextures[1] = textures[1];
47 }
48 SkRandom* fRandom;
49 GrContext* fContext;
joshualitt9cc17752015-07-09 06:28:14 -070050 GrProcessorDataManager* fProcDataManager;
joshualitt0067ff52015-07-08 14:26:19 -070051 const GrCaps* fCaps;
52 GrTexture* fTextures[2];
53};
54
joshualittb0a8a372014-09-23 09:50:21 -070055#if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
56
joshualittb0a8a372014-09-23 09:50:21 -070057class GrProcessor;
58class GrTexture;
59
60template <class Processor>
61class GrProcessorTestFactory : SkNoncopyable {
62public:
63
bsalomonecfdc252015-08-28 14:33:46 -070064 typedef const Processor* (*CreateProc)(GrProcessorTestData*);
joshualittb0a8a372014-09-23 09:50:21 -070065
66 GrProcessorTestFactory(CreateProc createProc) {
67 fCreateProc = createProc;
68 GetFactories()->push_back(this);
69 }
70
bsalomonecfdc252015-08-28 14:33:46 -070071 static const Processor* CreateStage(GrProcessorTestData* data) {
joshualitt9e87fa72014-10-09 13:12:35 -070072 VerifyFactoryCount();
73 SkASSERT(GetFactories()->count());
joshualitt0067ff52015-07-08 14:26:19 -070074 uint32_t idx = data->fRandom->nextRangeU(0, GetFactories()->count() - 1);
joshualittb0a8a372014-09-23 09:50:21 -070075 GrProcessorTestFactory<Processor>* factory = (*GetFactories())[idx];
joshualitt0067ff52015-07-08 14:26:19 -070076 return factory->fCreateProc(data);
joshualittb0a8a372014-09-23 09:50:21 -070077 }
78
joshualitt9e87fa72014-10-09 13:12:35 -070079 /*
80 * A test function which verifies the count of factories.
81 */
82 static void VerifyFactoryCount();
83
joshualittb0a8a372014-09-23 09:50:21 -070084private:
85 CreateProc fCreateProc;
86
joshualitt9e87fa72014-10-09 13:12:35 -070087 static SkTArray<GrProcessorTestFactory<Processor>*, true>* GetFactories();
joshualittb0a8a372014-09-23 09:50:21 -070088};
89
90/** GrProcessor subclasses should insert this macro in their declaration to be included in the
91 * program generation unit test.
92 */
joshualittb0a8a372014-09-23 09:50:21 -070093#define GR_DECLARE_GEOMETRY_PROCESSOR_TEST \
joshualitt9e87fa72014-10-09 13:12:35 -070094 static GrProcessorTestFactory<GrGeometryProcessor> gTestFactory SK_UNUSED; \
bsalomonecfdc252015-08-28 14:33:46 -070095 static const GrGeometryProcessor* TestCreate(GrProcessorTestData*)
joshualittb0a8a372014-09-23 09:50:21 -070096
97#define GR_DECLARE_FRAGMENT_PROCESSOR_TEST \
joshualitt9e87fa72014-10-09 13:12:35 -070098 static GrProcessorTestFactory<GrFragmentProcessor> gTestFactory SK_UNUSED; \
bsalomonecfdc252015-08-28 14:33:46 -070099 static const GrFragmentProcessor* TestCreate(GrProcessorTestData*)
joshualittb0a8a372014-09-23 09:50:21 -0700100
egdanielc2304142014-12-11 13:15:13 -0800101#define GR_DECLARE_XP_FACTORY_TEST \
102 static GrProcessorTestFactory<GrXPFactory> gTestFactory SK_UNUSED; \
bsalomonecfdc252015-08-28 14:33:46 -0700103 static const GrXPFactory* TestCreate(GrProcessorTestData*)
egdanielc2304142014-12-11 13:15:13 -0800104
105
joshualittb0a8a372014-09-23 09:50:21 -0700106/** GrProcessor subclasses should insert this macro in their implementation file. They must then
107 * also implement this static function:
joshualitt0067ff52015-07-08 14:26:19 -0700108 * GrProcessor* TestCreate(GrProcessorTestData*);
joshualittb0a8a372014-09-23 09:50:21 -0700109 */
110#define GR_DEFINE_FRAGMENT_PROCESSOR_TEST(Effect) \
111 GrProcessorTestFactory<GrFragmentProcessor> Effect :: gTestFactory(Effect :: TestCreate)
112
egdanielc2304142014-12-11 13:15:13 -0800113#define GR_DEFINE_XP_FACTORY_TEST(Factory) \
114 GrProcessorTestFactory<GrXPFactory> Factory :: gTestFactory(Factory :: TestCreate)
115
joshualittb0a8a372014-09-23 09:50:21 -0700116#define GR_DEFINE_GEOMETRY_PROCESSOR_TEST(Effect) \
117 GrProcessorTestFactory<GrGeometryProcessor> Effect :: gTestFactory(Effect :: TestCreate)
118
119#else // !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
120
121// The unit test relies on static initializers. Just declare the TestCreate function so that
122// its definitions will compile.
123#define GR_DECLARE_FRAGMENT_PROCESSOR_TEST \
bsalomonecfdc252015-08-28 14:33:46 -0700124 static const GrFragmentProcessor* TestCreate(GrProcessorTestData*)
joshualittb0a8a372014-09-23 09:50:21 -0700125#define GR_DEFINE_FRAGMENT_PROCESSOR_TEST(X)
126
127// The unit test relies on static initializers. Just declare the TestCreate function so that
128// its definitions will compile.
egdanielc2304142014-12-11 13:15:13 -0800129#define GR_DECLARE_XP_FACTORY_TEST \
bsalomonecfdc252015-08-28 14:33:46 -0700130 static const GrXPFactory* TestCreate(GrProcessorTestData*)
egdanielc2304142014-12-11 13:15:13 -0800131#define GR_DEFINE_XP_FACTORY_TEST(X)
132
133// The unit test relies on static initializers. Just declare the TestCreate function so that
134// its definitions will compile.
joshualittb0a8a372014-09-23 09:50:21 -0700135#define GR_DECLARE_GEOMETRY_PROCESSOR_TEST \
bsalomonecfdc252015-08-28 14:33:46 -0700136 static const GrGeometryProcessor* TestCreate(GrProcessorTestData*)
joshualittb0a8a372014-09-23 09:50:21 -0700137#define GR_DEFINE_GEOMETRY_PROCESSOR_TEST(X)
138
139#endif // !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
140#endif