• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2017 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 SkShaderBase_DEFINED
9 #define SkShaderBase_DEFINED
10 
11 #include "include/core/SkFilterQuality.h"
12 #include "include/core/SkMatrix.h"
13 #include "include/core/SkShader.h"
14 #include "include/private/SkNoncopyable.h"
15 #include "src/core/SkEffectPriv.h"
16 #include "src/core/SkMask.h"
17 #include "src/core/SkTLazy.h"
18 
19 #if SK_SUPPORT_GPU
20 #include "src/gpu/GrFPArgs.h"
21 #endif
22 
23 class GrContext;
24 class GrFragmentProcessor;
25 class SkArenaAlloc;
26 class SkColorSpace;
27 class SkImage;
28 struct SkImageInfo;
29 class SkPaint;
30 class SkRasterPipeline;
31 
32 /**
33  *  Shaders can optionally return a subclass of this when appending their stages.
34  *  Doing so tells the caller that the stages can be reused with different CTMs (but nothing
35  *  else can change), by calling the updater's udpate() method before each use.
36  *
37  *  This can be a perf-win bulk draws like drawAtlas and drawVertices, where most of the setup
38  *  (i.e. uniforms) are constant, and only something small is changing (i.e. matrices). This
39  *  reuse skips the cost of computing the stages (and/or avoids having to allocate a separate
40  *  shader for each small draw.
41  */
42 class SkStageUpdater {
43 public:
~SkStageUpdater()44     virtual ~SkStageUpdater() {}
45 
46     virtual bool update(const SkMatrix& ctm, const SkMatrix* localM) = 0;
47 };
48 
49 class SkShaderBase : public SkShader {
50 public:
51     ~SkShaderBase() override;
52 
53     /**
54      *  Returns true if the shader is guaranteed to produce only a single color.
55      *  Subclasses can override this to allow loop-hoisting optimization.
56      */
isConstant()57     virtual bool isConstant() const { return false; }
58 
getLocalMatrix()59     const SkMatrix& getLocalMatrix() const { return fLocalMatrix; }
60 
61     enum Flags {
62         //!< set if all of the colors will be opaque
63         kOpaqueAlpha_Flag = 1 << 0,
64 
65         /** set if the spans only vary in X (const in Y).
66             e.g. an Nx1 bitmap that is being tiled in Y, or a linear-gradient
67             that varies from left-to-right. This flag specifies this for
68             shadeSpan().
69          */
70         kConstInY32_Flag = 1 << 1,
71 
72         /** hint for the blitter that 4f is the preferred shading mode.
73          */
74         kPrefers4f_Flag  = 1 << 2,
75     };
76 
77     /**
78      *  ContextRec acts as a parameter bundle for creating Contexts.
79      */
80     struct ContextRec {
ContextRecContextRec81         ContextRec(const SkPaint& paint, const SkMatrix& matrix, const SkMatrix* localM,
82                    SkColorType dstColorType, SkColorSpace* dstColorSpace)
83             : fPaint(&paint)
84             , fMatrix(&matrix)
85             , fLocalMatrix(localM)
86             , fDstColorType(dstColorType)
87             , fDstColorSpace(dstColorSpace) {}
88 
89         const SkPaint*  fPaint;            // the current paint associated with the draw
90         const SkMatrix* fMatrix;           // the current matrix in the canvas
91         const SkMatrix* fLocalMatrix;      // optional local matrix
92         SkColorType     fDstColorType;     // the color type of the dest surface
93         SkColorSpace*   fDstColorSpace;    // the color space of the dest surface (if any)
94 
95         bool isLegacyCompatible(SkColorSpace* shadersColorSpace) const;
96     };
97 
98     class Context : public ::SkNoncopyable {
99     public:
100         Context(const SkShaderBase& shader, const ContextRec&);
101 
102         virtual ~Context();
103 
104         /**
105          *  Called sometimes before drawing with this shader. Return the type of
106          *  alpha your shader will return. The default implementation returns 0.
107          *  Your subclass should override if it can (even sometimes) report a
108          *  non-zero value, since that will enable various blitters to perform
109          *  faster.
110          */
getFlags()111         virtual uint32_t getFlags() const { return 0; }
112 
113         /**
114          *  Called for each span of the object being drawn. Your subclass should
115          *  set the appropriate colors (with premultiplied alpha) that correspond
116          *  to the specified device coordinates.
117          */
118         virtual void shadeSpan(int x, int y, SkPMColor[], int count) = 0;
119 
120     protected:
121         // Reference to shader, so we don't have to dupe information.
122         const SkShaderBase& fShader;
123 
getPaintAlpha()124         uint8_t         getPaintAlpha() const { return fPaintAlpha; }
getTotalInverse()125         const SkMatrix& getTotalInverse() const { return fTotalInverse; }
getCTM()126         const SkMatrix& getCTM() const { return fCTM; }
127 
128     private:
129         SkMatrix    fCTM;
130         SkMatrix    fTotalInverse;
131         uint8_t     fPaintAlpha;
132 
133         typedef SkNoncopyable INHERITED;
134     };
135 
136     /**
137      * Make a context using the memory provided by the arena.
138      *
139      * @return pointer to context or nullptr if can't be created
140      */
141     Context* makeContext(const ContextRec&, SkArenaAlloc*) const;
142 
143 #if SK_SUPPORT_GPU
144     /**
145      *  Returns a GrFragmentProcessor that implements the shader for the GPU backend. NULL is
146      *  returned if there is no GPU implementation.
147      *
148      *  The GPU device does not call SkShader::createContext(), instead we pass the view matrix,
149      *  local matrix, and filter quality directly.
150      *
151      *  The GrContext may be used by the to create textures that are required by the returned
152      *  processor.
153      *
154      *  The returned GrFragmentProcessor should expect an unpremultiplied input color and
155      *  produce a premultiplied output.
156      */
157     virtual std::unique_ptr<GrFragmentProcessor> asFragmentProcessor(const GrFPArgs&) const;
158 #endif
159 
160     /**
161      *  If the shader can represent its "average" luminance in a single color, return true and
162      *  if color is not NULL, return that color. If it cannot, return false and ignore the color
163      *  parameter.
164      *
165      *  Note: if this returns true, the returned color will always be opaque, as only the RGB
166      *  components are used to compute luminance.
167      */
168     bool asLuminanceColor(SkColor*) const;
169 
170     // If this returns false, then we draw nothing (do not fall back to shader context)
171     bool appendStages(const SkStageRec&) const;
172 
173     bool SK_WARN_UNUSED_RESULT computeTotalInverse(const SkMatrix& ctm,
174                                                    const SkMatrix* outerLocalMatrix,
175                                                    SkMatrix* totalInverse) const;
176 
177     // Returns the total local matrix for this shader:
178     //
179     //   M = postLocalMatrix x shaderLocalMatrix x preLocalMatrix
180     //
181     SkTCopyOnFirstWrite<SkMatrix> totalLocalMatrix(const SkMatrix* preLocalMatrix,
182                                                    const SkMatrix* postLocalMatrix = nullptr) const;
183 
onIsAImage(SkMatrix *,SkTileMode[2])184     virtual SkImage* onIsAImage(SkMatrix*, SkTileMode[2]) const {
185         return nullptr;
186     }
isAPicture(SkMatrix *,SkTileMode[2],SkRect * tile)187     virtual SkPicture* isAPicture(SkMatrix*, SkTileMode[2], SkRect* tile) const { return nullptr; }
188 
GetFlattenableType()189     static Type GetFlattenableType() { return kSkShaderBase_Type; }
getFlattenableType()190     Type getFlattenableType() const override { return GetFlattenableType(); }
191 
192     static sk_sp<SkShaderBase> Deserialize(const void* data, size_t size,
193                                              const SkDeserialProcs* procs = nullptr) {
194         return sk_sp<SkShaderBase>(static_cast<SkShaderBase*>(
195                 SkFlattenable::Deserialize(GetFlattenableType(), data, size, procs).release()));
196     }
197     static void RegisterFlattenables();
198 
199     /** DEPRECATED. skbug.com/8941
200      *  If this shader can be represented by another shader + a localMatrix, return that shader and
201      *  the localMatrix. If not, return nullptr and ignore the localMatrix parameter.
202      */
203     virtual sk_sp<SkShader> makeAsALocalMatrixShader(SkMatrix* localMatrix) const;
204 
appendUpdatableStages(const SkStageRec & rec)205     SkStageUpdater* appendUpdatableStages(const SkStageRec& rec) const {
206         return this->onAppendUpdatableStages(rec);
207     }
208 
209 protected:
210     SkShaderBase(const SkMatrix* localMatrix = nullptr);
211 
212     void flatten(SkWriteBuffer&) const override;
213 
214 #ifdef SK_ENABLE_LEGACY_SHADERCONTEXT
215     /**
216      * Specialize creating a SkShader context using the supplied allocator.
217      * @return pointer to context owned by the arena allocator.
218      */
onMakeContext(const ContextRec &,SkArenaAlloc *)219     virtual Context* onMakeContext(const ContextRec&, SkArenaAlloc*) const {
220         return nullptr;
221     }
222 #endif
223 
onAsLuminanceColor(SkColor *)224     virtual bool onAsLuminanceColor(SkColor*) const {
225         return false;
226     }
227 
228     // Default impl creates shadercontext and calls that (not very efficient)
229     virtual bool onAppendStages(const SkStageRec&) const;
230 
onAppendUpdatableStages(const SkStageRec &)231     virtual SkStageUpdater* onAppendUpdatableStages(const SkStageRec&) const { return nullptr; }
232 
233 private:
234     // This is essentially const, but not officially so it can be modified in constructors.
235     SkMatrix fLocalMatrix;
236 
237     typedef SkShader INHERITED;
238 };
239 
as_SB(SkShader * shader)240 inline SkShaderBase* as_SB(SkShader* shader) {
241     return static_cast<SkShaderBase*>(shader);
242 }
243 
as_SB(const SkShader * shader)244 inline const SkShaderBase* as_SB(const SkShader* shader) {
245     return static_cast<const SkShaderBase*>(shader);
246 }
247 
as_SB(const sk_sp<SkShader> & shader)248 inline const SkShaderBase* as_SB(const sk_sp<SkShader>& shader) {
249     return static_cast<SkShaderBase*>(shader.get());
250 }
251 
252 #endif // SkShaderBase_DEFINED
253