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