1 /*
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 GrProcessor_DEFINED
9 #define GrProcessor_DEFINED
10 
11 #include "GrBuffer.h"
12 #include "GrColor.h"
13 #include "GrProcessorUnitTest.h"
14 #include "GrSamplerState.h"
15 #include "GrShaderVar.h"
16 #include "GrSurfaceProxyPriv.h"
17 #include "GrTextureProxy.h"
18 #include "SkMath.h"
19 #include "SkString.h"
20 
21 class GrContext;
22 class GrResourceProvider;
23 
24 /**
25  * Used by processors to build their keys. It incorporates each per-processor key into a larger
26  * shader key.
27  */
28 class GrProcessorKeyBuilder {
29 public:
GrProcessorKeyBuilder(SkTArray<unsigned char,true> * data)30     GrProcessorKeyBuilder(SkTArray<unsigned char, true>* data) : fData(data), fCount(0) {
31         SkASSERT(0 == fData->count() % sizeof(uint32_t));
32     }
33 
add32(uint32_t v)34     void add32(uint32_t v) {
35         ++fCount;
36         fData->push_back_n(4, reinterpret_cast<uint8_t*>(&v));
37     }
38 
39     /** Inserts count uint32_ts into the key. The returned pointer is only valid until the next
40         add*() call. */
add32n(int count)41     uint32_t* SK_WARN_UNUSED_RESULT add32n(int count) {
42         SkASSERT(count > 0);
43         fCount += count;
44         return reinterpret_cast<uint32_t*>(fData->push_back_n(4 * count));
45     }
46 
size()47     size_t size() const { return sizeof(uint32_t) * fCount; }
48 
49 private:
50     SkTArray<uint8_t, true>* fData; // unowned ptr to the larger key.
51     int fCount;                     // number of uint32_ts added to fData by the processor.
52 };
53 
54 /** Provides custom shader code to the Ganesh shading pipeline. GrProcessor objects *must* be
55     immutable: after being constructed, their fields may not change.
56 
57     Dynamically allocated GrProcessors are managed by a per-thread memory pool. The ref count of an
58     processor must reach 0 before the thread terminates and the pool is destroyed.
59  */
60 class GrProcessor {
61 public:
62     enum ClassID {
63         kBigKeyProcessor_ClassID,
64         kBlockInputFragmentProcessor_ClassID,
65         kButtCapStrokedCircleGeometryProcessor_ClassID,
66         kCircleGeometryProcessor_ClassID,
67         kCircularRRectEffect_ClassID,
68         kClockwiseTestProcessor_ClassID,
69         kColorMatrixEffect_ClassID,
70         kColorTableEffect_ClassID,
71         kComposeOneFragmentProcessor_ClassID,
72         kComposeTwoFragmentProcessor_ClassID,
73         kCoverageSetOpXP_ClassID,
74         kCubicStrokeProcessor_ClassID,
75         kCustomXP_ClassID,
76         kDashingCircleEffect_ClassID,
77         kDashingLineEffect_ClassID,
78         kDefaultGeoProc_ClassID,
79         kDIEllipseGeometryProcessor_ClassID,
80         kDisableColorXP_ClassID,
81         kTwoPointConicalEffect_ClassID,
82         kEllipseGeometryProcessor_ClassID,
83         kEllipticalRRectEffect_ClassID,
84         kGP_ClassID,
85         kVertexColorSpaceBenchGP_ClassID,
86         kGrAAFillRRectOp_Processor_ClassID,
87         kGrAARectEffect_ClassID,
88         kGrAlphaThresholdFragmentProcessor_ClassID,
89         kGrArithmeticFP_ClassID,
90         kGrBicubicEffect_ClassID,
91         kGrBitmapTextGeoProc_ClassID,
92         kGrBlurredEdgeFragmentProcessor_ClassID,
93         kGrCCClipProcessor_ClassID,
94         kGrCCCoverageProcessor_ClassID,
95         kGrCCPathProcessor_ClassID,
96         kGrCircleBlurFragmentProcessor_ClassID,
97         kGrCircleEffect_ClassID,
98         kGrClampedGradientEffect_ClassID,
99         kGrColorSpaceXformEffect_ClassID,
100         kGrConfigConversionEffect_ClassID,
101         kGrConicEffect_ClassID,
102         kGrConstColorProcessor_ClassID,
103         kGrConvexPolyEffect_ClassID,
104         kGrDeviceSpaceTextureDecalFragmentProcessor_ClassID,
105         kGrDiffuseLightingEffect_ClassID,
106         kGrDisplacementMapEffect_ClassID,
107         kGrDistanceFieldA8TextGeoProc_ClassID,
108         kGrDistanceFieldLCDTextGeoProc_ClassID,
109         kGrDistanceFieldPathGeoProc_ClassID,
110         kGrDitherEffect_ClassID,
111         kGrDualIntervalGradientColorizer_ClassID,
112         kGrEllipseEffect_ClassID,
113         kGrGaussianConvolutionFragmentProcessor_ClassID,
114         kGrImprovedPerlinNoiseEffect_ClassID,
115         kGrLightingEffect_ClassID,
116         kGrLinearGradient_ClassID,
117         kGrLinearGradientLayout_ClassID,
118         kGrLumaColorFilterEffect_ClassID,
119         kGrMagnifierEffect_ClassID,
120         kGrMatrixConvolutionEffect_ClassID,
121         kGrMeshTestProcessor_ClassID,
122         kGrMorphologyEffect_ClassID,
123         kGrOverdrawFragmentProcessor_ClassID,
124         kGrPathProcessor_ClassID,
125         kGrPerlinNoise2Effect_ClassID,
126         kGrPipelineDynamicStateTestProcessor_ClassID,
127         kGrPremulInputFragmentProcessor_ClassID,
128         kGrQuadEffect_ClassID,
129         kGrRadialGradient_ClassID,
130         kGrRadialGradientLayout_ClassID,
131         kGrRectBlurEffect_ClassID,
132         kGrRRectBlurEffect_ClassID,
133         kGrRRectShadowGeoProc_ClassID,
134         kGrSimpleTextureEffect_ClassID,
135         kGrSingleIntervalGradientColorizer_ClassID,
136         kGrSkSLFP_ClassID,
137         kGrSpecularLightingEffect_ClassID,
138         kGrSRGBEffect_ClassID,
139         kGrSweepGradient_ClassID,
140         kGrSweepGradientLayout_ClassID,
141         kGrTextureDomainEffect_ClassID,
142         kGrTextureGradientColorizer_ClassID,
143         kGrTiledGradientEffect_ClassID,
144         kGrTwoPointConicalGradientLayout_ClassID,
145         kGrUnpremulInputFragmentProcessor_ClassID,
146         kGrUnrolledBinaryGradientColorizer_ClassID,
147         kGrYUVtoRGBEffect_ClassID,
148         kHighContrastFilterEffect_ClassID,
149         kInstanceProcessor_ClassID,
150         kLatticeGP_ClassID,
151         kLumaColorFilterEffect_ClassID,
152         kMSAAQuadProcessor_ClassID,
153         kPDLCDXferProcessor_ClassID,
154         kPorterDuffXferProcessor_ClassID,
155         kPremulFragmentProcessor_ClassID,
156         kQuadEdgeEffect_ClassID,
157         kQuadPerEdgeAAGeometryProcessor_ClassID,
158         kReplaceInputFragmentProcessor_ClassID,
159         kRRectsGaussianEdgeFP_ClassID,
160         kSeriesFragmentProcessor_ClassID,
161         kShaderPDXferProcessor_ClassID,
162         kFwidthSquircleTestProcessor_ClassID,
163         kSwizzleFragmentProcessor_ClassID,
164         kTestFP_ClassID,
165         kTextureGeometryProcessor_ClassID,
166         kFlatNormalsFP_ClassID,
167         kMappedNormalsFP_ClassID,
168         kLightingFP_ClassID,
169         kLinearStrokeProcessor_ClassID,
170     };
171 
172     virtual ~GrProcessor() = default;
173 
174     /** Human-meaningful string to identify this prcoessor; may be embedded in generated shader
175         code. */
176     virtual const char* name() const = 0;
177 
178     /** Human-readable dump of all information */
179 #ifdef SK_DEBUG
dumpInfo()180     virtual SkString dumpInfo() const {
181         SkString str;
182         str.appendf("Missing data");
183         return str;
184     }
185 #else
dumpInfo()186     SkString dumpInfo() const { return SkString("<Processor information unavailable>"); }
187 #endif
188 
189     void* operator new(size_t size);
190     void operator delete(void* target);
191 
new(size_t size,void * placement)192     void* operator new(size_t size, void* placement) {
193         return ::operator new(size, placement);
194     }
delete(void * target,void * placement)195     void operator delete(void* target, void* placement) {
196         ::operator delete(target, placement);
197     }
198 
199     /** Helper for down-casting to a GrProcessor subclass */
cast()200     template <typename T> const T& cast() const { return *static_cast<const T*>(this); }
201 
classID()202     ClassID classID() const { return fClassID; }
203 
204 protected:
GrProcessor(ClassID classID)205     GrProcessor(ClassID classID) : fClassID(classID) {}
206 
207 private:
208     GrProcessor(const GrProcessor&) = delete;
209     GrProcessor& operator=(const GrProcessor&) = delete;
210 
211     ClassID fClassID;
212 };
213 
214 #endif
215