blob: 4f26665cbeac67f8f5cc6f1ef3d14d2cc4f3fc6d [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
bungemanbf521ff2016-02-17 13:13:44 -080011#include "../private/SkTArray.h"
joshualitt4eaf9ce2015-04-28 13:31:18 -070012#include "GrTestUtils.h"
joshualittb0a8a372014-09-23 09:50:21 -070013#include "SkTypes.h"
14
15class SkMatrix;
bsalomon4b91f762015-05-19 09:29:46 -070016class GrCaps;
joshualitt0067ff52015-07-08 14:26:19 -070017class GrContext;
robertphillips82ec6e52016-05-19 14:01:05 -070018class GrDrawContext;
bsalomon506c8022015-09-14 13:16:26 -070019struct GrProcessorTestData;
joshualittb0a8a372014-09-23 09:50:21 -070020
21namespace GrProcessorUnitTest {
bsalomon506c8022015-09-14 13:16:26 -070022
joshualittb0a8a372014-09-23 09:50:21 -070023// Used to access the dummy textures in TestCreate procs.
24enum {
25 kSkiaPMTextureIdx = 0,
26 kAlphaTextureIdx = 1,
27};
28
bsalomon506c8022015-09-14 13:16:26 -070029/** This allows parent FPs to implement a test create with known leaf children in order to avoid
30creating an unbounded FP tree which may overflow various shader limits. */
bungeman06ca8ec2016-06-09 08:01:03 -070031sk_sp<GrFragmentProcessor> MakeChildFP(GrProcessorTestData*);
bsalomon506c8022015-09-14 13:16:26 -070032
joshualitt2e3b3e32014-12-09 13:31:14 -080033}
34
joshualitt0067ff52015-07-08 14:26:19 -070035/*
36 * GrProcessorTestData is an argument struct to TestCreate functions
37 * fTextures are valid textures that can optionally be used to construct
38 * GrTextureAccesses. The first texture has config kSkia8888_GrPixelConfig and the second has
39 * kAlpha_8_GrPixelConfig. TestCreate functions are also free to create additional textures using
40 * the GrContext.
41 */
42struct GrProcessorTestData {
43 GrProcessorTestData(SkRandom* random,
44 GrContext* context,
joshualitt0067ff52015-07-08 14:26:19 -070045 const GrCaps* caps,
robertphillips82ec6e52016-05-19 14:01:05 -070046 const GrDrawContext* drawContext,
joshualitt0067ff52015-07-08 14:26:19 -070047 GrTexture* textures[2])
48 : fRandom(random)
49 , fContext(context)
cdaltonc94cd7c2015-11-12 12:11:04 -080050 , fCaps(caps)
robertphillips82ec6e52016-05-19 14:01:05 -070051 , fDrawContext(drawContext) {
joshualitt0067ff52015-07-08 14:26:19 -070052 fTextures[0] = textures[0];
53 fTextures[1] = textures[1];
54 }
55 SkRandom* fRandom;
56 GrContext* fContext;
joshualitt0067ff52015-07-08 14:26:19 -070057 const GrCaps* fCaps;
robertphillips82ec6e52016-05-19 14:01:05 -070058 const GrDrawContext* fDrawContext;
joshualitt0067ff52015-07-08 14:26:19 -070059 GrTexture* fTextures[2];
60};
61
joshualittb0a8a372014-09-23 09:50:21 -070062#if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
63
joshualittb0a8a372014-09-23 09:50:21 -070064class GrProcessor;
65class GrTexture;
66
bsalomon506c8022015-09-14 13:16:26 -070067template <class Processor> class GrProcessorTestFactory : SkNoncopyable {
joshualittb0a8a372014-09-23 09:50:21 -070068public:
bungeman06ca8ec2016-06-09 08:01:03 -070069 typedef sk_sp<Processor> (*MakeProc)(GrProcessorTestData*);
joshualittb0a8a372014-09-23 09:50:21 -070070
bungeman06ca8ec2016-06-09 08:01:03 -070071 GrProcessorTestFactory(MakeProc makeProc) {
72 fMakeProc = makeProc;
joshualittb0a8a372014-09-23 09:50:21 -070073 GetFactories()->push_back(this);
74 }
75
bsalomonb5b60322015-09-14 12:26:33 -070076 /** Pick a random factory function and create a processor. */
bungeman06ca8ec2016-06-09 08:01:03 -070077 static sk_sp<Processor> Make(GrProcessorTestData* data) {
joshualitt9e87fa72014-10-09 13:12:35 -070078 VerifyFactoryCount();
79 SkASSERT(GetFactories()->count());
joshualitt0067ff52015-07-08 14:26:19 -070080 uint32_t idx = data->fRandom->nextRangeU(0, GetFactories()->count() - 1);
bungeman06ca8ec2016-06-09 08:01:03 -070081 return MakeIdx(idx, data);
bsalomonb5b60322015-09-14 12:26:33 -070082 }
83
84 /** Number of registered factory functions */
85 static int Count() { return GetFactories()->count(); }
86
87 /** Use factory function at Index idx to create a processor. */
bungeman06ca8ec2016-06-09 08:01:03 -070088 static sk_sp<Processor> MakeIdx(int idx, GrProcessorTestData* data) {
joshualittb0a8a372014-09-23 09:50:21 -070089 GrProcessorTestFactory<Processor>* factory = (*GetFactories())[idx];
bungeman06ca8ec2016-06-09 08:01:03 -070090 return factory->fMakeProc(data);
joshualittb0a8a372014-09-23 09:50:21 -070091 }
92
joshualitt9e87fa72014-10-09 13:12:35 -070093 /*
94 * A test function which verifies the count of factories.
95 */
96 static void VerifyFactoryCount();
97
joshualittb0a8a372014-09-23 09:50:21 -070098private:
bungeman06ca8ec2016-06-09 08:01:03 -070099 MakeProc fMakeProc;
joshualittb0a8a372014-09-23 09:50:21 -0700100
joshualitt9e87fa72014-10-09 13:12:35 -0700101 static SkTArray<GrProcessorTestFactory<Processor>*, true>* GetFactories();
joshualittb0a8a372014-09-23 09:50:21 -0700102};
103
104/** GrProcessor subclasses should insert this macro in their declaration to be included in the
105 * program generation unit test.
106 */
joshualittb0a8a372014-09-23 09:50:21 -0700107#define GR_DECLARE_GEOMETRY_PROCESSOR_TEST \
joshualitt9e87fa72014-10-09 13:12:35 -0700108 static GrProcessorTestFactory<GrGeometryProcessor> gTestFactory SK_UNUSED; \
bungeman06ca8ec2016-06-09 08:01:03 -0700109 static sk_sp<GrGeometryProcessor> TestCreate(GrProcessorTestData*)
joshualittb0a8a372014-09-23 09:50:21 -0700110
111#define GR_DECLARE_FRAGMENT_PROCESSOR_TEST \
joshualitt9e87fa72014-10-09 13:12:35 -0700112 static GrProcessorTestFactory<GrFragmentProcessor> gTestFactory SK_UNUSED; \
bungeman06ca8ec2016-06-09 08:01:03 -0700113 static sk_sp<GrFragmentProcessor> TestCreate(GrProcessorTestData*)
joshualittb0a8a372014-09-23 09:50:21 -0700114
egdanielc2304142014-12-11 13:15:13 -0800115#define GR_DECLARE_XP_FACTORY_TEST \
116 static GrProcessorTestFactory<GrXPFactory> gTestFactory SK_UNUSED; \
bungeman06ca8ec2016-06-09 08:01:03 -0700117 static sk_sp<GrXPFactory> TestCreate(GrProcessorTestData*)
egdanielc2304142014-12-11 13:15:13 -0800118
joshualittb0a8a372014-09-23 09:50:21 -0700119/** GrProcessor subclasses should insert this macro in their implementation file. They must then
120 * also implement this static function:
joshualitt0067ff52015-07-08 14:26:19 -0700121 * GrProcessor* TestCreate(GrProcessorTestData*);
joshualittb0a8a372014-09-23 09:50:21 -0700122 */
123#define GR_DEFINE_FRAGMENT_PROCESSOR_TEST(Effect) \
124 GrProcessorTestFactory<GrFragmentProcessor> Effect :: gTestFactory(Effect :: TestCreate)
125
egdanielc2304142014-12-11 13:15:13 -0800126#define GR_DEFINE_XP_FACTORY_TEST(Factory) \
127 GrProcessorTestFactory<GrXPFactory> Factory :: gTestFactory(Factory :: TestCreate)
128
joshualittb0a8a372014-09-23 09:50:21 -0700129#define GR_DEFINE_GEOMETRY_PROCESSOR_TEST(Effect) \
130 GrProcessorTestFactory<GrGeometryProcessor> Effect :: gTestFactory(Effect :: TestCreate)
131
132#else // !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
133
134// The unit test relies on static initializers. Just declare the TestCreate function so that
135// its definitions will compile.
136#define GR_DECLARE_FRAGMENT_PROCESSOR_TEST \
bungeman06ca8ec2016-06-09 08:01:03 -0700137 static sk_sp<GrFragmentProcessor> TestCreate(GrProcessorTestData*)
joshualittb0a8a372014-09-23 09:50:21 -0700138#define GR_DEFINE_FRAGMENT_PROCESSOR_TEST(X)
139
140// The unit test relies on static initializers. Just declare the TestCreate function so that
141// its definitions will compile.
egdanielc2304142014-12-11 13:15:13 -0800142#define GR_DECLARE_XP_FACTORY_TEST \
bungeman06ca8ec2016-06-09 08:01:03 -0700143 static sk_sp<GrXPFactory> TestCreate(GrProcessorTestData*)
egdanielc2304142014-12-11 13:15:13 -0800144#define GR_DEFINE_XP_FACTORY_TEST(X)
145
146// The unit test relies on static initializers. Just declare the TestCreate function so that
147// its definitions will compile.
joshualittb0a8a372014-09-23 09:50:21 -0700148#define GR_DECLARE_GEOMETRY_PROCESSOR_TEST \
bungeman06ca8ec2016-06-09 08:01:03 -0700149 static sk_sp<GrGeometryProcessor> TestCreate(GrProcessorTestData*)
joshualittb0a8a372014-09-23 09:50:21 -0700150#define GR_DEFINE_GEOMETRY_PROCESSOR_TEST(X)
151
152#endif // !SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
153#endif