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 "../private/SkAtomics.h"
12 #include "GrBuffer.h"
13 #include "GrColor.h"
14 #include "GrGpuResourceRef.h"
15 #include "GrProcessorUnitTest.h"
16 #include "GrProgramElement.h"
17 #include "GrSamplerState.h"
18 #include "GrShaderVar.h"
19 #include "GrSurfaceProxyPriv.h"
20 #include "GrSurfaceProxyRef.h"
21 #include "GrTextureProxy.h"
22 #include "SkMath.h"
23 #include "SkString.h"
24 
25 class GrContext;
26 class GrCoordTransform;
27 class GrInvariantOutput;
28 class GrResourceProvider;
29 
30 /**
31  * Used by processors to build their keys. It incorporates each per-processor key into a larger
32  * shader key.
33  */
34 class GrProcessorKeyBuilder {
35 public:
GrProcessorKeyBuilder(SkTArray<unsigned char,true> * data)36     GrProcessorKeyBuilder(SkTArray<unsigned char, true>* data) : fData(data), fCount(0) {
37         SkASSERT(0 == fData->count() % sizeof(uint32_t));
38     }
39 
add32(uint32_t v)40     void add32(uint32_t v) {
41         ++fCount;
42         fData->push_back_n(4, reinterpret_cast<uint8_t*>(&v));
43     }
44 
45     /** Inserts count uint32_ts into the key. The returned pointer is only valid until the next
46         add*() call. */
add32n(int count)47     uint32_t* SK_WARN_UNUSED_RESULT add32n(int count) {
48         SkASSERT(count > 0);
49         fCount += count;
50         return reinterpret_cast<uint32_t*>(fData->push_back_n(4 * count));
51     }
52 
size()53     size_t size() const { return sizeof(uint32_t) * fCount; }
54 
55 private:
56     SkTArray<uint8_t, true>* fData; // unowned ptr to the larger key.
57     int fCount;                     // number of uint32_ts added to fData by the processor.
58 };
59 
60 /** Provides custom shader code to the Ganesh shading pipeline. GrProcessor objects *must* be
61     immutable: after being constructed, their fields may not change.
62 
63     Dynamically allocated GrProcessors are managed by a per-thread memory pool. The ref count of an
64     processor must reach 0 before the thread terminates and the pool is destroyed.
65  */
66 class GrProcessor {
67 public:
68     enum ClassID {
69         kBigKeyProcessor_ClassID,
70         kBlockInputFragmentProcessor_ClassID,
71         kCircleGeometryProcessor_ClassID,
72         kCircularRRectEffect_ClassID,
73         kColorMatrixEffect_ClassID,
74         kColorTableEffect_ClassID,
75         kComposeOneFragmentProcessor_ClassID,
76         kComposeTwoFragmentProcessor_ClassID,
77         kCoverageSetOpXP_ClassID,
78         kCustomXP_ClassID,
79         kDashingCircleEffect_ClassID,
80         kDashingLineEffect_ClassID,
81         kDefaultGeoProc_ClassID,
82         kDIEllipseGeometryProcessor_ClassID,
83         kDisableColorXP_ClassID,
84         kTwoPointConicalEffect_ClassID,
85         kEllipseGeometryProcessor_ClassID,
86         kEllipticalRRectEffect_ClassID,
87         kGP_ClassID,
88         kGrAARectEffect_ClassID,
89         kGrAlphaThresholdFragmentProcessor_ClassID,
90         kGrArithmeticFP_ClassID,
91         kGrBicubicEffect_ClassID,
92         kGrBitmapTextGeoProc_ClassID,
93         kGrBlurredEdgeFragmentProcessor_ClassID,
94         kGrCCClipProcessor_ClassID,
95         kGrCCCoverageProcessor_ClassID,
96         kGrCCPathProcessor_ClassID,
97         kGrCircleBlurFragmentProcessor_ClassID,
98         kGrCircleEffect_ClassID,
99         kGrColorSpaceXformEffect_ClassID,
100         kGrConfigConversionEffect_ClassID,
101         kGrConicEffect_ClassID,
102         kGrConstColorProcessor_ClassID,
103         kGrConvexPolyEffect_ClassID,
104         kGrCubicEffect_ClassID,
105         kGrDeviceSpaceTextureDecalFragmentProcessor_ClassID,
106         kGrDiffuseLightingEffect_ClassID,
107         kGrDisplacementMapEffect_ClassID,
108         kGrDistanceFieldA8TextGeoProc_ClassID,
109         kGrDistanceFieldLCDTextGeoProc_ClassID,
110         kGrDistanceFieldPathGeoProc_ClassID,
111         kGrDitherEffect_ClassID,
112         kGrEllipseEffect_ClassID,
113         kGrGaussianConvolutionFragmentProcessor_ClassID,
114         kGrImprovedPerlinNoiseEffect_ClassID,
115         kGrLightingEffect_ClassID,
116         kGrLinearGradient_ClassID,
117         kGrLumaColorFilterEffect_ClassID,
118         kGrMagnifierEffect_ClassID,
119         kGrMatrixConvolutionEffect_ClassID,
120         kGrMeshTestProcessor_ClassID,
121         kGrMorphologyEffect_ClassID,
122         kGrNonlinearColorSpaceXformEffect_ClassID,
123         kGrOverdrawFragmentProcessor_ClassID,
124         kGrPathProcessor_ClassID,
125         kGrPerlinNoise2Effect_ClassID,
126         kGrPipelineDynamicStateTestProcessor_ClassID,
127         kGrPremulInputFragmentProcessor_ClassID,
128         kGrQuadEffect_ClassID,
129         kGrRadialGradient_ClassID,
130         kGrRectBlurEffect_ClassID,
131         kGrRRectBlurEffect_ClassID,
132         kGrRRectShadowGeoProc_ClassID,
133         kGrSimpleTextureEffect_ClassID,
134         kGrSpecularLightingEffect_ClassID,
135         kGrSRGBEffect_ClassID,
136         kGrSweepGradient_ClassID,
137         kGrTextureDomainEffect_ClassID,
138         kGrUnpremulInputFragmentProcessor_ClassID,
139         kGrYUVtoRGBEffect_ClassID,
140         kHighContrastFilterEffect_ClassID,
141         kInstanceProcessor_ClassID,
142         kLumaColorFilterEffect_ClassID,
143         kMSAAQuadProcessor_ClassID,
144         kPDLCDXferProcessor_ClassID,
145         kPorterDuffXferProcessor_ClassID,
146         kPremulFragmentProcessor_ClassID,
147         kQuadEdgeEffect_ClassID,
148         kReplaceInputFragmentProcessor_ClassID,
149         kRRectsGaussianEdgeFP_ClassID,
150         kSeriesFragmentProcessor_ClassID,
151         kShaderPDXferProcessor_ClassID,
152         kSwizzleFragmentProcessor_ClassID,
153         kTestFP_ClassID,
154         kTextureGeometryProcessor_ClassID,
155     };
156 
157     virtual ~GrProcessor() = default;
158 
159     /** Human-meaningful string to identify this prcoessor; may be embedded in generated shader
160         code. */
161     virtual const char* name() const = 0;
162 
163     /** Human-readable dump of all information */
dumpInfo()164     virtual SkString dumpInfo() const {
165         SkString str;
166         str.appendf("Missing data");
167         return str;
168     }
169 
170     void* operator new(size_t size);
171     void operator delete(void* target);
172 
new(size_t size,void * placement)173     void* operator new(size_t size, void* placement) {
174         return ::operator new(size, placement);
175     }
delete(void * target,void * placement)176     void operator delete(void* target, void* placement) {
177         ::operator delete(target, placement);
178     }
179 
180     /** Helper for down-casting to a GrProcessor subclass */
cast()181     template <typename T> const T& cast() const { return *static_cast<const T*>(this); }
182 
classID()183     ClassID classID() const { return fClassID; }
184 
185 protected:
GrProcessor(ClassID classID)186     GrProcessor(ClassID classID) : fClassID(classID) {}
187 
188 private:
189     GrProcessor(const GrProcessor&) = delete;
190     GrProcessor& operator=(const GrProcessor&) = delete;
191 
192     ClassID fClassID;
193 };
194 
195 /** A GrProcessor with the ability to access textures, buffers, and image storages. */
196 class GrResourceIOProcessor : public GrProcessor {
197 public:
198     class TextureSampler;
199     class BufferAccess;
200 
numTextureSamplers()201     int numTextureSamplers() const { return fTextureSamplers.count(); }
202 
203     /** Returns the access pattern for the texture at index. index must be valid according to
204         numTextureSamplers(). */
textureSampler(int index)205     const TextureSampler& textureSampler(int index) const { return *fTextureSamplers[index]; }
206 
numBuffers()207     int numBuffers() const { return fBufferAccesses.count(); }
208 
209     /** Returns the access pattern for the buffer at index. index must be valid according to
210         numBuffers(). */
bufferAccess(int index)211     const BufferAccess& bufferAccess(int index) const { return *fBufferAccesses[index]; }
212 
213     bool instantiate(GrResourceProvider* resourceProvider) const;
214 
215 protected:
GrResourceIOProcessor(ClassID classID)216     GrResourceIOProcessor(ClassID classID)
217     : INHERITED(classID) {}
218 
219     /**
220      * Subclasses call these from their constructor to register sampler sources. The processor
221      * subclass manages the lifetime of the objects (these functions only store pointers). The
222      * TextureSampler and/or BufferAccess instances are typically member fields of the GrProcessor
223      * subclass. These must only be called from the constructor because GrProcessors are immutable.
224      */
225     void addTextureSampler(const TextureSampler*);
226     void addBufferAccess(const BufferAccess*);
227 
228     bool hasSameSamplersAndAccesses(const GrResourceIOProcessor&) const;
229 
230     // These methods can be used by derived classes that also derive from GrProgramElement.
231     void addPendingIOs() const;
232     void removeRefs() const;
233     void pendingIOComplete() const;
234 
235 private:
236     SkSTArray<4, const TextureSampler*, true> fTextureSamplers;
237     SkSTArray<1, const BufferAccess*, true> fBufferAccesses;
238 
239     typedef GrProcessor INHERITED;
240 };
241 
242 /**
243  * Used to represent a texture that is required by a GrResourceIOProcessor. It holds a GrTexture
244  * along with an associated GrSamplerState. TextureSamplers don't perform any coord manipulation to
245  * account for texture origin.
246  */
247 class GrResourceIOProcessor::TextureSampler {
248 public:
249     /**
250      * Must be initialized before adding to a GrProcessor's texture access list.
251      */
252     TextureSampler();
253     /**
254      * This copy constructor is used by GrFragmentProcessor::clone() implementations. The copy
255      * always takes a new ref on the texture proxy as the new fragment processor will not yet be
256      * in pending execution state.
257      */
TextureSampler(const TextureSampler & that)258     explicit TextureSampler(const TextureSampler& that)
259             : fProxyRef(sk_ref_sp(that.fProxyRef.get()), that.fProxyRef.ioType())
260             , fSamplerState(that.fSamplerState)
261             , fVisibility(that.fVisibility) {}
262 
263     TextureSampler(sk_sp<GrTextureProxy>, const GrSamplerState&);
264 
265     explicit TextureSampler(sk_sp<GrTextureProxy>,
266                             GrSamplerState::Filter = GrSamplerState::Filter::kNearest,
267                             GrSamplerState::WrapMode wrapXAndY = GrSamplerState::WrapMode::kClamp,
268                             GrShaderFlags visibility = kFragment_GrShaderFlag);
269 
270     TextureSampler& operator=(const TextureSampler&) = delete;
271 
272     void reset(sk_sp<GrTextureProxy>, const GrSamplerState&,
273                GrShaderFlags visibility = kFragment_GrShaderFlag);
274     void reset(sk_sp<GrTextureProxy>,
275                GrSamplerState::Filter = GrSamplerState::Filter::kNearest,
276                GrSamplerState::WrapMode wrapXAndY = GrSamplerState::WrapMode::kClamp,
277                GrShaderFlags visibility = kFragment_GrShaderFlag);
278 
279     bool operator==(const TextureSampler& that) const {
280         return this->proxy()->underlyingUniqueID() == that.proxy()->underlyingUniqueID() &&
281                fSamplerState == that.fSamplerState && fVisibility == that.fVisibility;
282     }
283 
284     bool operator!=(const TextureSampler& other) const { return !(*this == other); }
285 
286     // 'instantiate' should only ever be called at flush time.
instantiate(GrResourceProvider * resourceProvider)287     bool instantiate(GrResourceProvider* resourceProvider) const {
288         return SkToBool(fProxyRef.get()->instantiate(resourceProvider));
289     }
290 
291     // 'peekTexture' should only ever be called after a successful 'instantiate' call
peekTexture()292     GrTexture* peekTexture() const {
293         SkASSERT(fProxyRef.get()->priv().peekTexture());
294         return fProxyRef.get()->priv().peekTexture();
295     }
296 
proxy()297     GrTextureProxy* proxy() const { return fProxyRef.get()->asTextureProxy(); }
visibility()298     GrShaderFlags visibility() const { return fVisibility; }
samplerState()299     const GrSamplerState& samplerState() const { return fSamplerState; }
300 
isInitialized()301     bool isInitialized() const { return SkToBool(fProxyRef.get()); }
302     /**
303      * For internal use by GrProcessor.
304      */
programProxy()305     const GrSurfaceProxyRef* programProxy() const { return &fProxyRef; }
306 
307 private:
308     GrSurfaceProxyRef fProxyRef;
309     GrSamplerState fSamplerState;
310     GrShaderFlags fVisibility;
311 };
312 
313 /**
314  * Used to represent a texel buffer that will be read in a GrResourceIOProcessor. It holds a
315  * GrBuffer along with an associated offset and texel config.
316  */
317 class GrResourceIOProcessor::BufferAccess {
318 public:
319     BufferAccess() = default;
320     BufferAccess(GrPixelConfig texelConfig, GrBuffer* buffer,
321                  GrShaderFlags visibility = kFragment_GrShaderFlag) {
322         this->reset(texelConfig, buffer, visibility);
323     }
324     /**
325      * This copy constructor is used by GrFragmentProcessor::clone() implementations. The copy
326      * always takes a new ref on the buffer proxy as the new fragment processor will not yet be
327      * in pending execution state.
328      */
BufferAccess(const BufferAccess & that)329     explicit BufferAccess(const BufferAccess& that) {
330         this->reset(that.fTexelConfig, that.fBuffer.get(), that.fVisibility);
331     }
332 
333     BufferAccess& operator=(const BufferAccess&) = delete;
334 
335     /**
336      * Must be initialized before adding to a GrProcessor's buffer access list.
337      */
338     void reset(GrPixelConfig texelConfig, GrBuffer* buffer,
339                GrShaderFlags visibility = kFragment_GrShaderFlag) {
340         fTexelConfig = texelConfig;
341         fBuffer.set(SkRef(buffer), kRead_GrIOType);
342         fVisibility = visibility;
343     }
344 
345     bool operator==(const BufferAccess& that) const {
346         return fTexelConfig == that.fTexelConfig &&
347                this->buffer() == that.buffer() &&
348                fVisibility == that.fVisibility;
349     }
350 
351     bool operator!=(const BufferAccess& that) const { return !(*this == that); }
352 
texelConfig()353     GrPixelConfig texelConfig() const { return fTexelConfig; }
buffer()354     GrBuffer* buffer() const { return fBuffer.get(); }
visibility()355     GrShaderFlags visibility() const { return fVisibility; }
356 
357     /**
358      * For internal use by GrProcessor.
359      */
programBuffer()360     const GrGpuResourceRef* programBuffer() const { return &fBuffer;}
361 
362 private:
363     GrPixelConfig fTexelConfig;
364     GrTGpuResourceRef<GrBuffer> fBuffer;
365     GrShaderFlags fVisibility;
366 
367     typedef SkNoncopyable INHERITED;
368 };
369 
370 #endif
371