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 "SkFilterQuality.h"
12 #include "SkMask.h"
13 #include "SkMatrix.h"
14 #include "SkShader.h"
15
16 #if SK_SUPPORT_GPU
17 #include "GrFPArgs.h"
18 #endif
19
20 class GrContext;
21 class GrColorSpaceInfo;
22 class GrFragmentProcessor;
23 class SkArenaAlloc;
24 class SkColorSpace;
25 class SkColorSpaceXformer;
26 class SkImage;
27 struct SkImageInfo;
28 class SkPaint;
29 class SkRasterPipeline;
30
31 class SkShaderBase : public SkShader {
32 public:
33 ~SkShaderBase() override;
34
35 /**
36 * Returns true if the shader is guaranteed to produce only a single color.
37 * Subclasses can override this to allow loop-hoisting optimization.
38 */
isConstant()39 virtual bool isConstant() const { return false; }
40
getLocalMatrix()41 const SkMatrix& getLocalMatrix() const { return fLocalMatrix; }
42
43 enum Flags {
44 //!< set if all of the colors will be opaque
45 kOpaqueAlpha_Flag = 1 << 0,
46
47 /** set if the spans only vary in X (const in Y).
48 e.g. an Nx1 bitmap that is being tiled in Y, or a linear-gradient
49 that varies from left-to-right. This flag specifies this for
50 shadeSpan().
51 */
52 kConstInY32_Flag = 1 << 1,
53
54 /** hint for the blitter that 4f is the preferred shading mode.
55 */
56 kPrefers4f_Flag = 1 << 2,
57 };
58
59 /**
60 * ContextRec acts as a parameter bundle for creating Contexts.
61 */
62 struct ContextRec {
63 enum DstType {
64 kPMColor_DstType, // clients prefer shading into PMColor dest
65 kPM4f_DstType, // clients prefer shading into PM4f dest
66 };
67
ContextRecContextRec68 ContextRec(const SkPaint& paint, const SkMatrix& matrix, const SkMatrix* localM,
69 DstType dstType, SkColorSpace* dstColorSpace)
70 : fPaint(&paint)
71 , fMatrix(&matrix)
72 , fLocalMatrix(localM)
73 , fPreferredDstType(dstType)
74 , fDstColorSpace(dstColorSpace) {}
75
76 const SkPaint* fPaint; // the current paint associated with the draw
77 const SkMatrix* fMatrix; // the current matrix in the canvas
78 const SkMatrix* fLocalMatrix; // optional local matrix
79 const DstType fPreferredDstType; // the "natural" client dest type
80 SkColorSpace* fDstColorSpace; // the color space of the dest surface (if any)
81 };
82
83 class Context : public ::SkNoncopyable {
84 public:
85 Context(const SkShaderBase& shader, const ContextRec&);
86
87 virtual ~Context();
88
89 /**
90 * Called sometimes before drawing with this shader. Return the type of
91 * alpha your shader will return. The default implementation returns 0.
92 * Your subclass should override if it can (even sometimes) report a
93 * non-zero value, since that will enable various blitters to perform
94 * faster.
95 */
getFlags()96 virtual uint32_t getFlags() const { return 0; }
97
98 /**
99 * Called for each span of the object being drawn. Your subclass should
100 * set the appropriate colors (with premultiplied alpha) that correspond
101 * to the specified device coordinates.
102 */
103 virtual void shadeSpan(int x, int y, SkPMColor[], int count) = 0;
104
105 virtual void shadeSpan4f(int x, int y, SkPM4f[], int count);
106
107 // Notification from blitter::blitMask in case we need to see the non-alpha channels
set3DMask(const SkMask *)108 virtual void set3DMask(const SkMask*) {}
109
110 protected:
111 // Reference to shader, so we don't have to dupe information.
112 const SkShaderBase& fShader;
113
getPaintAlpha()114 uint8_t getPaintAlpha() const { return fPaintAlpha; }
getTotalInverse()115 const SkMatrix& getTotalInverse() const { return fTotalInverse; }
getCTM()116 const SkMatrix& getCTM() const { return fCTM; }
117
118 private:
119 SkMatrix fCTM;
120 SkMatrix fTotalInverse;
121 uint8_t fPaintAlpha;
122
123 typedef SkNoncopyable INHERITED;
124 };
125
126 /**
127 * Make a context using the memory provided by the arena.
128 *
129 * @return pointer to context or nullptr if can't be created
130 */
131 Context* makeContext(const ContextRec&, SkArenaAlloc*) const;
132
133 /**
134 * Shaders may opt-in for burst mode, if they can operate
135 * significantly more efficiently in that mode.
136 *
137 * Burst mode is prioritized in SkRasterPipelineBlitter over
138 * regular (appendStages) pipeline operation.
139 */
140 Context* makeBurstPipelineContext(const ContextRec&, SkArenaAlloc*) const;
141
142 #if SK_SUPPORT_GPU
143 /**
144 * Returns a GrFragmentProcessor that implements the shader for the GPU backend. NULL is
145 * returned if there is no GPU implementation.
146 *
147 * The GPU device does not call SkShader::createContext(), instead we pass the view matrix,
148 * local matrix, and filter quality directly.
149 *
150 * The GrContext may be used by the to create textures that are required by the returned
151 * processor.
152 *
153 * The returned GrFragmentProcessor should expect an unpremultiplied input color and
154 * produce a premultiplied output.
155 */
156 virtual std::unique_ptr<GrFragmentProcessor> asFragmentProcessor(const GrFPArgs&) const;
157 #endif
158
159 /**
160 * If the shader can represent its "average" luminance in a single color, return true and
161 * if color is not NULL, return that color. If it cannot, return false and ignore the color
162 * parameter.
163 *
164 * Note: if this returns true, the returned color will always be opaque, as only the RGB
165 * components are used to compute luminance.
166 */
167 bool asLuminanceColor(SkColor*) const;
168
169 /**
170 * Returns a shader transformed into a new color space via the |xformer|.
171 */
makeColorSpace(SkColorSpaceXformer * xformer)172 sk_sp<SkShader> makeColorSpace(SkColorSpaceXformer* xformer) const {
173 return this->onMakeColorSpace(xformer);
174 }
175
isRasterPipelineOnly(const SkMatrix & ctm)176 bool isRasterPipelineOnly(const SkMatrix& ctm) const {
177 // We always use RP when perspective is present.
178 return ctm.hasPerspective() || fLocalMatrix.hasPerspective()
179 || this->onIsRasterPipelineOnly(ctm);
180 }
181
182 struct StageRec {
183 SkRasterPipeline* fPipeline;
184 SkArenaAlloc* fAlloc;
185 SkColorSpace* fDstCS; // may be nullptr
186 const SkPaint& fPaint;
187 const SkMatrix* fLocalM; // may be nullptr
188 SkMatrix fCTM;
189 };
190
191 // If this returns false, then we draw nothing (do not fall back to shader context)
192 bool appendStages(const StageRec&) const;
193
194 bool computeTotalInverse(const SkMatrix& ctm,
195 const SkMatrix* outerLocalMatrix,
196 SkMatrix* totalInverse) const;
197
198 #ifdef SK_SUPPORT_LEGACY_SHADER_ISABITMAP
onIsABitmap(SkBitmap *,SkMatrix *,TileMode[2])199 virtual bool onIsABitmap(SkBitmap*, SkMatrix*, TileMode[2]) const {
200 return false;
201 }
202 #endif
203
onIsAImage(SkMatrix *,TileMode[2])204 virtual SkImage* onIsAImage(SkMatrix*, TileMode[2]) const {
205 return nullptr;
206 }
207
208 SK_TO_STRING_VIRT()
209
210 SK_DEFINE_FLATTENABLE_TYPE(SkShaderBase)
211 SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP()
212
213 protected:
214 SkShaderBase(const SkMatrix* localMatrix = nullptr);
215
216 void flatten(SkWriteBuffer&) const override;
217
218 /**
219 * Specialize creating a SkShader context using the supplied allocator.
220 * @return pointer to context owned by the arena allocator.
221 */
onMakeContext(const ContextRec &,SkArenaAlloc *)222 virtual Context* onMakeContext(const ContextRec&, SkArenaAlloc*) const {
223 return nullptr;
224 }
225
226 /**
227 * Overriden by shaders which prefer burst mode.
228 */
onMakeBurstPipelineContext(const ContextRec &,SkArenaAlloc *)229 virtual Context* onMakeBurstPipelineContext(const ContextRec&, SkArenaAlloc*) const {
230 return nullptr;
231 }
232
onAsLuminanceColor(SkColor *)233 virtual bool onAsLuminanceColor(SkColor*) const {
234 return false;
235 }
236
onMakeColorSpace(SkColorSpaceXformer *)237 virtual sk_sp<SkShader> onMakeColorSpace(SkColorSpaceXformer*) const {
238 return sk_ref_sp(const_cast<SkShaderBase*>(this));
239 }
240
241 // Default impl creates shadercontext and calls that (not very efficient)
242 virtual bool onAppendStages(const StageRec&) const;
243
onIsRasterPipelineOnly(const SkMatrix & ctm)244 virtual bool onIsRasterPipelineOnly(const SkMatrix& ctm) const { return false; }
245
246 private:
247 // This is essentially const, but not officially so it can be modified in constructors.
248 SkMatrix fLocalMatrix;
249
250 typedef SkShader INHERITED;
251 };
252
as_SB(SkShader * shader)253 inline SkShaderBase* as_SB(SkShader* shader) {
254 return static_cast<SkShaderBase*>(shader);
255 }
256
as_SB(const SkShader * shader)257 inline const SkShaderBase* as_SB(const SkShader* shader) {
258 return static_cast<const SkShaderBase*>(shader);
259 }
260
as_SB(const sk_sp<SkShader> & shader)261 inline const SkShaderBase* as_SB(const sk_sp<SkShader>& shader) {
262 return static_cast<SkShaderBase*>(shader.get());
263 }
264
265 #endif // SkShaderBase_DEFINED
266