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 "GrColor.h"
12 #include "GrGpuBuffer.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         kGrMixerEffect_ClassID,
124         kGrOverdrawFragmentProcessor_ClassID,
125         kGrOverrideInputFragmentProcessor_ClassID,
126         kGrPathProcessor_ClassID,
127         kGrPerlinNoise2Effect_ClassID,
128         kGrPipelineDynamicStateTestProcessor_ClassID,
129         kGrPremulInputFragmentProcessor_ClassID,
130         kGrQuadEffect_ClassID,
131         kGrRadialGradient_ClassID,
132         kGrRadialGradientLayout_ClassID,
133         kGrRectBlurEffect_ClassID,
134         kGrRRectBlurEffect_ClassID,
135         kGrRRectShadowGeoProc_ClassID,
136         kGrSimpleTextureEffect_ClassID,
137         kGrSingleIntervalGradientColorizer_ClassID,
138         kGrSkSLFP_ClassID,
139         kGrSpecularLightingEffect_ClassID,
140         kGrSRGBEffect_ClassID,
141         kGrSweepGradient_ClassID,
142         kGrSweepGradientLayout_ClassID,
143         kGrTextureDomainEffect_ClassID,
144         kGrTextureGradientColorizer_ClassID,
145         kGrTiledGradientEffect_ClassID,
146         kGrTwoPointConicalGradientLayout_ClassID,
147         kGrUnpremulInputFragmentProcessor_ClassID,
148         kGrUnrolledBinaryGradientColorizer_ClassID,
149         kGrYUVtoRGBEffect_ClassID,
150         kHighContrastFilterEffect_ClassID,
151         kInstanceProcessor_ClassID,
152         kLatticeGP_ClassID,
153         kLumaColorFilterEffect_ClassID,
154         kMSAAQuadProcessor_ClassID,
155         kPDLCDXferProcessor_ClassID,
156         kPorterDuffXferProcessor_ClassID,
157         kPremulFragmentProcessor_ClassID,
158         kQuadEdgeEffect_ClassID,
159         kQuadPerEdgeAAGeometryProcessor_ClassID,
160         kReplaceInputFragmentProcessor_ClassID,
161         kRRectsGaussianEdgeFP_ClassID,
162         kSampleLocationsTestProcessor_ClassID,
163         kSeriesFragmentProcessor_ClassID,
164         kShaderPDXferProcessor_ClassID,
165         kFwidthSquircleTestProcessor_ClassID,
166         kSwizzleFragmentProcessor_ClassID,
167         kTestFP_ClassID,
168         kTextureGeometryProcessor_ClassID,
169         kFlatNormalsFP_ClassID,
170         kMappedNormalsFP_ClassID,
171         kLightingFP_ClassID,
172         kLinearStrokeProcessor_ClassID,
173     };
174 
175     virtual ~GrProcessor() = default;
176 
177     /** Human-meaningful string to identify this prcoessor; may be embedded in generated shader
178         code. */
179     virtual const char* name() const = 0;
180 
181     /** Human-readable dump of all information */
182 #ifdef SK_DEBUG
dumpInfo()183     virtual SkString dumpInfo() const {
184         SkString str;
185         str.appendf("Missing data");
186         return str;
187     }
188 #else
dumpInfo()189     SkString dumpInfo() const { return SkString("<Processor information unavailable>"); }
190 #endif
191 
192     /**
193      * Custom shader features provided by the framework. These require special handling when
194      * preparing shaders, so a processor must call setWillUseCustomFeature() from its constructor if
195      * it intends to use one.
196      */
197     enum class CustomFeatures {
198         kNone = 0,
199         kSampleLocations = 1 << 0,
200     };
201 
202     GR_DECL_BITFIELD_CLASS_OPS_FRIENDS(CustomFeatures);
203 
requestedFeatures()204     CustomFeatures requestedFeatures() const { return fRequestedFeatures; }
205 
206     void* operator new(size_t size);
207     void operator delete(void* target);
208 
new(size_t size,void * placement)209     void* operator new(size_t size, void* placement) {
210         return ::operator new(size, placement);
211     }
delete(void * target,void * placement)212     void operator delete(void* target, void* placement) {
213         ::operator delete(target, placement);
214     }
215 
216     /** Helper for down-casting to a GrProcessor subclass */
cast()217     template <typename T> const T& cast() const { return *static_cast<const T*>(this); }
218 
classID()219     ClassID classID() const { return fClassID; }
220 
221 protected:
GrProcessor(ClassID classID)222     GrProcessor(ClassID classID) : fClassID(classID) {}
223     GrProcessor(const GrProcessor&) = delete;
224     GrProcessor& operator=(const GrProcessor&) = delete;
225 
setWillUseCustomFeature(CustomFeatures feature)226     void setWillUseCustomFeature(CustomFeatures feature) { fRequestedFeatures |= feature; }
227 
228     const ClassID fClassID;
229     CustomFeatures fRequestedFeatures = CustomFeatures::kNone;
230 };
231 
232 GR_MAKE_BITFIELD_CLASS_OPS(GrProcessor::CustomFeatures);
233 
234 #endif
235