Skia
2DGraphicsLibrary
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GrXferProcessor.h
1 /*
2  * Copyright 2014 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 GrXferProcessor_DEFINED
9 #define GrXferProcessor_DEFINED
10 
11 #include "GrBlend.h"
12 #include "GrColor.h"
13 #include "GrProcessor.h"
14 #include "GrTexture.h"
15 #include "GrTypes.h"
16 #include "SkXfermode.h"
17 
18 class GrShaderCaps;
19 class GrGLSLCaps;
20 class GrGLSLXferProcessor;
21 class GrProcOptInfo;
22 struct GrPipelineOptimizations;
23 
28 enum GrXferBarrierType {
29  kNone_GrXferBarrierType = 0, //<! No barrier is required
30  kTexture_GrXferBarrierType, //<! Required when a shader reads and renders to the same texture.
31  kBlend_GrXferBarrierType, //<! Required by certain blend extensions.
32 };
34 GR_STATIC_ASSERT(SkToBool(kNone_GrXferBarrierType) == false);
35 
52 class GrXferProcessor : public GrProcessor {
53 public:
59  class DstTexture {
60  public:
61  DstTexture() { fOffset.set(0, 0); }
62 
63  DstTexture(const DstTexture& other) {
64  *this = other;
65  }
66 
67  DstTexture(GrTexture* texture, const SkIPoint& offset)
68  : fTexture(SkSafeRef(texture))
69  , fOffset(offset) {
70  }
71 
72  DstTexture& operator=(const DstTexture& other) {
73  fTexture.reset(SkSafeRef(other.fTexture.get()));
74  fOffset = other.fOffset;
75  return *this;
76  }
77 
78  const SkIPoint& offset() const { return fOffset; }
79 
80  void setOffset(const SkIPoint& offset) { fOffset = offset; }
81  void setOffset(int ox, int oy) { fOffset.set(ox, oy); }
82 
83  GrTexture* texture() const { return fTexture.get(); }
84 
85  GrTexture* setTexture(GrTexture* texture) {
86  fTexture.reset(SkSafeRef(texture));
87  return texture;
88  }
89 
90  private:
91  SkAutoTUnref<GrTexture> fTexture;
92  SkIPoint fOffset;
93  };
94 
99  void getGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const;
100 
104  virtual GrGLSLXferProcessor* createGLSLInstance() const = 0;
105 
109  enum OptFlags {
130  };
131 
132  static const OptFlags kNone_OptFlags = (OptFlags)0;
133 
134  GR_DECL_BITFIELD_OPS_FRIENDS(OptFlags);
135 
145  OptFlags getOptimizations(const GrPipelineOptimizations& optimizations,
146  bool doesStencilWrite,
147  GrColor* overrideColor,
148  const GrCaps& caps) const;
149 
154  GrXferBarrierType xferBarrierType(const GrRenderTarget* rt, const GrCaps& caps) const;
155 
156  struct BlendInfo {
157  void reset() {
158  fEquation = kAdd_GrBlendEquation;
159  fSrcBlend = kOne_GrBlendCoeff;
160  fDstBlend = kZero_GrBlendCoeff;
161  fBlendConstant = 0;
162  fWriteColor = true;
163  }
164 
165  SkDEBUGCODE(SkString dump() const;)
166 
167  GrBlendEquation fEquation;
168  GrBlendCoeff fSrcBlend;
169  GrBlendCoeff fDstBlend;
170  GrColor fBlendConstant;
171  bool fWriteColor;
172  };
173 
174  void getBlendInfo(BlendInfo* blendInfo) const;
175 
176  bool willReadDstColor() const { return fWillReadDstColor; }
177 
183  const GrTexture* getDstTexture() const { return fDstTexture.getTexture(); }
184 
189  const SkIPoint& dstTextureOffset() const {
190  SkASSERT(this->getDstTexture());
191  return fDstTextureOffset;
192  }
193 
199  bool dstReadUsesMixedSamples() const { return fDstReadUsesMixedSamples; }
200 
205  bool hasSecondaryOutput() const;
206 
215  bool isEqual(const GrXferProcessor& that) const {
216  if (this->classID() != that.classID()) {
217  return false;
218  }
219  if (this->fWillReadDstColor != that.fWillReadDstColor) {
220  return false;
221  }
222  if (this->fDstTexture.getTexture() != that.fDstTexture.getTexture()) {
223  return false;
224  }
225  if (this->fDstTextureOffset != that.fDstTextureOffset) {
226  return false;
227  }
228  if (this->fDstReadUsesMixedSamples != that.fDstReadUsesMixedSamples) {
229  return false;
230  }
231  return this->onIsEqual(that);
232  }
233 
234 protected:
235  GrXferProcessor();
236  GrXferProcessor(const DstTexture*, bool willReadDstColor, bool hasMixedSamples);
237 
238 private:
239  void notifyRefCntIsZero() const final {}
240 
241  virtual OptFlags onGetOptimizations(const GrPipelineOptimizations& optimizations,
242  bool doesStencilWrite,
243  GrColor* overrideColor,
244  const GrCaps& caps) const = 0;
245 
250  virtual void onGetGLSLProcessorKey(const GrGLSLCaps& caps,
251  GrProcessorKeyBuilder* b) const = 0;
252 
258  virtual GrXferBarrierType onXferBarrier(const GrRenderTarget*, const GrCaps&) const {
259  return kNone_GrXferBarrierType;
260  }
261 
267  virtual bool onHasSecondaryOutput() const { return false; }
268 
274  virtual void onGetBlendInfo(BlendInfo*) const {}
275 
276  virtual bool onIsEqual(const GrXferProcessor&) const = 0;
277 
278  bool fWillReadDstColor;
279  bool fDstReadUsesMixedSamples;
280  SkIPoint fDstTextureOffset;
281  GrTextureAccess fDstTexture;
282 
283  typedef GrFragmentProcessor INHERITED;
284 };
285 
286 GR_MAKE_BITFIELD_OPS(GrXferProcessor::OptFlags);
287 
289 
301 class GrXPFactory : public SkRefCnt {
302 public:
304  GrXferProcessor* createXferProcessor(const GrPipelineOptimizations& optimizations,
305  bool hasMixedSamples,
306  const DstTexture*,
307  const GrCaps& caps) const;
312  bool fWillBlendWithDst;
313  GrColor fKnownColor;
314  GrColorComponentFlags fKnownColorFlags;
315  };
316 
323  virtual void getInvariantBlendedColor(const GrProcOptInfo& colorPOI,
324  InvariantBlendedColor*) const = 0;
325 
326  bool willNeedDstTexture(const GrCaps& caps, const GrPipelineOptimizations& optimizations) const;
327 
328  bool isEqual(const GrXPFactory& that) const {
329  if (this->classID() != that.classID()) {
330  return false;
331  }
332  return this->onIsEqual(that);
333  }
334 
338  template <typename T> const T& cast() const { return *static_cast<const T*>(this); }
339 
340  uint32_t classID() const { SkASSERT(kIllegalXPFClassID != fClassID); return fClassID; }
341 
342 protected:
343  GrXPFactory() : fClassID(kIllegalXPFClassID) {}
344 
345  template <typename XPF_SUBCLASS> void initClassID() {
346  static uint32_t kClassID = GenClassID();
347  fClassID = kClassID;
348  }
349 
350  uint32_t fClassID;
351 
352 private:
353  virtual GrXferProcessor* onCreateXferProcessor(const GrCaps& caps,
354  const GrPipelineOptimizations& optimizations,
355  bool hasMixedSamples,
356  const DstTexture*) const = 0;
357 
358  virtual bool onIsEqual(const GrXPFactory&) const = 0;
359 
360  bool willReadDstColor(const GrCaps&, const GrPipelineOptimizations&) const;
365  virtual bool onWillReadDstColor(const GrCaps&, const GrPipelineOptimizations&) const = 0;
366 
367  static uint32_t GenClassID() {
368  // fCurrXPFactoryID has been initialized to kIllegalXPFactoryID. The
369  // atomic inc returns the old value not the incremented value. So we add
370  // 1 to the returned value.
371  uint32_t id = static_cast<uint32_t>(sk_atomic_inc(&gCurrXPFClassID)) + 1;
372  if (!id) {
373  SkFAIL("This should never wrap as it should only be called once for each GrXPFactory "
374  "subclass.");
375  }
376  return id;
377  }
378 
379  enum {
380  kIllegalXPFClassID = 0,
381  };
382  static int32_t gCurrXPFClassID;
383 
384  typedef GrProgramElement INHERITED;
385 };
386 
387 #endif
388 
Used to represent a texture that is required by a GrProcessor.
Definition: GrTextureAccess.h:21
void notifyRefCntIsZero() const final
This will be called when the ref cnt is zero.
Definition: GrXferProcessor.h:239
const SkIPoint & dstTextureOffset() const
Returns the offset in device coords to use when accessing the dst texture to get the dst pixel color ...
Definition: GrXferProcessor.h:189
const GrTexture * getDstTexture() const
Returns the texture to be used as the destination when reading the dst in the fragment shader...
Definition: GrXferProcessor.h:183
Provides custom fragment shader code.
Definition: GrFragmentProcessor.h:24
Represents the capabilities of a GrContext.
Definition: GrCaps.h:130
Can tweak alpha for coverage.
Definition: GrXferProcessor.h:129
Clear color stages and override input color to that returned by getOptimizations. ...
Definition: GrXferProcessor.h:125
const T & cast() const
Helper for down-casting to a GrXPFactory subclass.
Definition: GrXferProcessor.h:338
GrXferProcessor is responsible for implementing the xfer mode that blends the src color and dst color...
Definition: GrXferProcessor.h:52
Used by processors to build their keys.
Definition: GrProcessor.h:28
virtual void onGetGLSLProcessorKey(const GrGLSLCaps &caps, GrProcessorKeyBuilder *b) const =0
Sets a unique key on the GrProcessorKeyBuilder that is directly associated with this xfer processor's...
OptFlags
Optimizations for blending / coverage that an OptDrawState should apply to itself.
Definition: GrXferProcessor.h:109
Definition: SkRefCnt.h:135
bool isEqual(const GrXferProcessor &that) const
Returns true if this and other processor conservatively draw identically.
Definition: GrXferProcessor.h:215
A texture that contains the dst pixel values and an integer coord offset from device space to the spa...
Definition: GrXferProcessor.h:59
The draw can be skipped completely.
Definition: GrXferProcessor.h:113
void getGLSLProcessorKey(const GrGLSLCaps &caps, GrProcessorKeyBuilder *b) const
Sets a unique key on the GrProcessorKeyBuilder calls onGetGLSLProcessorKey(...) to get the specific s...
OptFlags getOptimizations(const GrPipelineOptimizations &optimizations, bool doesStencilWrite, GrColor *overrideColor, const GrCaps &caps) const
Determines which optimizations (as described by the ptFlags above) can be performed by the draw with ...
#define SkToBool(cond)
Returns 0 or 1 based on the condition.
Definition: SkTypes.h:287
Known color information after blending, but before accounting for any coverage.
Definition: GrXferProcessor.h:311
virtual void getInvariantBlendedColor(const GrProcOptInfo &colorPOI, InvariantBlendedColor *) const =0
Returns information about the output color, produced by XPs from this factory, that will be known aft...
Base class for GrProcessor.
Definition: GrProgramElement.h:44
virtual GrXferBarrierType onXferBarrier(const GrRenderTarget *, const GrCaps &) const
Determines the type of barrier (if any) required by the subclass.
Definition: GrXferProcessor.h:258
bool dstReadUsesMixedSamples() const
If we are performing a dst read, returns whether the base class will use mixed samples to antialias t...
Definition: GrXferProcessor.h:199
virtual bool onHasSecondaryOutput() const
If we are not performing a dst read, returns whether the subclass will set a secondary output...
Definition: GrXferProcessor.h:267
Definition: GrCaps.h:20
virtual void onGetBlendInfo(BlendInfo *) const
If we are not performing a dst read, retrieves the fixed-function blend state required by the subclas...
Definition: GrXferProcessor.h:274
GrXferProcessor will ignore coverage, thus no need to provide.
Definition: GrXferProcessor.h:121
virtual GrGLSLXferProcessor * createGLSLInstance() const =0
Returns a new instance of the appropriate GL implementation class for the given GrXferProcessor; call...
virtual bool onWillReadDstColor(const GrCaps &, const GrPipelineOptimizations &) const =0
Returns true if the XP generated by this factory will explicitly read dst in the fragment shader...
GrXferBarrierType xferBarrierType(const GrRenderTarget *rt, const GrCaps &caps) const
Returns whether this XP will require an Xfer barrier on the given rt.
Provides custom shader code to the Ganesh shading pipeline.
Definition: GrProcessor.h:60
Definition: GrXferProcessor.h:156
GrRenderTarget represents a 2D buffer of pixels that can be rendered to.
Definition: GrRenderTarget.h:26
void set(int32_t x, int32_t y)
Set the x and y values of the point.
Definition: SkPoint.h:65
Definition: GrTexture.h:19
SkIPoint holds two 32 bit integer coordinates.
Definition: SkPoint.h:40
We install a GrXPFactory (XPF) early on in the pipeline before all the final draw information is know...
Definition: GrXferProcessor.h:301
Light weight class for managing strings.
Definition: SkString.h:121
bool hasSecondaryOutput() const
Returns whether or not this xferProcossor will set a secondary output to be used with dual source ble...
GrXferProcessor will ignore color, thus no need to provide.
Definition: GrXferProcessor.h:117