blob: 8dc9354e533e3874ddac17fade97347675dc44e1 [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 /**
102 * The const void* ctx is only const because all the implementations are const.
103 * This can be changed to non-const if a new shade proc needs to change the ctx.
104 */
105 typedef void (*ShadeProc)(const void* ctx, int x, int y, SkPMColor[], int count);
106 virtual ShadeProc asAShadeProc(void** ctx);
107
108 /**
109 * Similar to shadeSpan, but only returns the alpha-channel for a span.
110 * The default implementation calls shadeSpan() and then extracts the alpha
111 * values from the returned colors.
112 */
113 virtual void shadeSpanAlpha(int x, int y, uint8_t alpha[], int count);
114
115 // Notification from blitter::blitMask in case we need to see the non-alpha channels
116 virtual void set3DMask(const SkMask*) {}
117
118 protected:
119 // Reference to shader, so we don't have to dupe information.
120 const SkShaderBase& fShader;
121
122 enum MatrixClass {
123 kLinear_MatrixClass, // no perspective
124 kFixedStepInX_MatrixClass, // fast perspective, need to call fixedStepInX() each
125 // scanline
126 kPerspective_MatrixClass // slow perspective, need to mappoints each pixel
127 };
128 static MatrixClass ComputeMatrixClass(const SkMatrix&);
129
130 uint8_t getPaintAlpha() const { return fPaintAlpha; }
131 const SkMatrix& getTotalInverse() const { return fTotalInverse; }
132 MatrixClass getInverseClass() const { return (MatrixClass)fTotalInverseClass; }
133 const SkMatrix& getCTM() const { return fCTM; }
134
Florin Malita4aed1382017-05-25 10:38:07 -0400135 private:
136 SkMatrix fCTM;
137 SkMatrix fTotalInverse;
138 uint8_t fPaintAlpha;
139 uint8_t fTotalInverseClass;
140
141 typedef SkNoncopyable INHERITED;
142 };
143
144 /**
145 * Make a context using the memory provided by the arena.
146 *
147 * @return pointer to context or nullptr if can't be created
148 */
149 Context* makeContext(const ContextRec&, SkArenaAlloc*) const;
150
Florin Malita47e55a52017-06-06 12:26:54 -0400151 /**
152 * Shaders may opt-in for burst mode, if they can operate
153 * significantly more efficiently in that mode.
154 *
155 * Burst mode is prioritized in SkRasterPipelineBlitter over
156 * regular (appendStages) pipeline operation.
157 */
158 Context* makeBurstPipelineContext(const ContextRec&, SkArenaAlloc*) const;
159
Florin Malita4aed1382017-05-25 10:38:07 -0400160#if SK_SUPPORT_GPU
161 struct AsFPArgs {
162 AsFPArgs() {}
163 AsFPArgs(GrContext* context,
164 const SkMatrix* viewMatrix,
165 const SkMatrix* localMatrix,
166 SkFilterQuality filterQuality,
167 SkColorSpace* dstColorSpace)
168 : fContext(context)
169 , fViewMatrix(viewMatrix)
170 , fLocalMatrix(localMatrix)
171 , fFilterQuality(filterQuality)
172 , fDstColorSpace(dstColorSpace) {}
173
174 GrContext* fContext;
175 const SkMatrix* fViewMatrix;
176 const SkMatrix* fLocalMatrix;
177 SkFilterQuality fFilterQuality;
178 SkColorSpace* fDstColorSpace;
179 };
180
181 /**
182 * Returns a GrFragmentProcessor that implements the shader for the GPU backend. NULL is
183 * returned if there is no GPU implementation.
184 *
185 * The GPU device does not call SkShader::createContext(), instead we pass the view matrix,
186 * local matrix, and filter quality directly.
187 *
188 * The GrContext may be used by the to create textures that are required by the returned
189 * processor.
190 *
191 * The returned GrFragmentProcessor should expect an unpremultiplied input color and
192 * produce a premultiplied output.
193 */
194 virtual sk_sp<GrFragmentProcessor> asFragmentProcessor(const AsFPArgs&) const;
195#endif
196
197 /**
198 * If the shader can represent its "average" luminance in a single color, return true and
199 * if color is not NULL, return that color. If it cannot, return false and ignore the color
200 * parameter.
201 *
202 * Note: if this returns true, the returned color will always be opaque, as only the RGB
203 * components are used to compute luminance.
204 */
205 bool asLuminanceColor(SkColor*) const;
206
207 /**
208 * Returns a shader transformed into a new color space via the |xformer|.
209 */
210 sk_sp<SkShader> makeColorSpace(SkColorSpaceXformer* xformer) const {
211 return this->onMakeColorSpace(xformer);
212 }
213
214 virtual bool isRasterPipelineOnly() const { return false; }
215
Mike Reed6867eee2017-06-02 13:25:15 -0400216 // If this returns false, then we draw nothing (do not fall back to shader context)
Florin Malita4aed1382017-05-25 10:38:07 -0400217 bool appendStages(SkRasterPipeline*, SkColorSpace* dstCS, SkArenaAlloc*,
218 const SkMatrix& ctm, const SkPaint&, const SkMatrix* localM=nullptr) const;
219
220 bool computeTotalInverse(const SkMatrix& ctm,
221 const SkMatrix* outerLocalMatrix,
222 SkMatrix* totalInverse) const;
223
224#ifdef SK_SUPPORT_LEGACY_SHADER_ISABITMAP
225 virtual bool onIsABitmap(SkBitmap*, SkMatrix*, TileMode[2]) const {
226 return false;
227 }
228#endif
229
230 virtual SkImage* onIsAImage(SkMatrix*, TileMode[2]) const {
231 return nullptr;
232 }
233
234 SK_TO_STRING_VIRT()
235
236 SK_DEFINE_FLATTENABLE_TYPE(SkShaderBase)
237 SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP()
238
239protected:
Florin Malitaf7beee72017-05-26 12:54:32 -0400240 SkShaderBase(const SkMatrix* localMatrix = nullptr);
241
Florin Malita4aed1382017-05-25 10:38:07 -0400242 void flatten(SkWriteBuffer&) const override;
243
244 /**
245 * Specialize creating a SkShader context using the supplied allocator.
246 * @return pointer to context owned by the arena allocator.
247 */
248 virtual Context* onMakeContext(const ContextRec&, SkArenaAlloc*) const {
249 return nullptr;
250 }
251
Florin Malita47e55a52017-06-06 12:26:54 -0400252 /**
253 * Overriden by shaders which prefer burst mode.
254 */
255 virtual Context* onMakeBurstPipelineContext(const ContextRec&, SkArenaAlloc*) const {
256 return nullptr;
257 }
258
Florin Malita4aed1382017-05-25 10:38:07 -0400259 virtual bool onAsLuminanceColor(SkColor*) const {
260 return false;
261 }
262
263 virtual sk_sp<SkShader> onMakeColorSpace(SkColorSpaceXformer*) const {
264 return sk_ref_sp(const_cast<SkShaderBase*>(this));
265 }
266
Mike Reed6867eee2017-06-02 13:25:15 -0400267 // Default impl creates shadercontext and calls that (not very efficient)
Florin Malita4aed1382017-05-25 10:38:07 -0400268 virtual bool onAppendStages(SkRasterPipeline*, SkColorSpace* dstCS, SkArenaAlloc*,
269 const SkMatrix&, const SkPaint&, const SkMatrix* localM) const;
270
271private:
272 // This is essentially const, but not officially so it can be modified in constructors.
273 SkMatrix fLocalMatrix;
274
275 typedef SkShader INHERITED;
276};
277
278inline SkShaderBase* as_SB(SkShader* shader) {
279 return static_cast<SkShaderBase*>(shader);
280}
281
282inline const SkShaderBase* as_SB(const SkShader* shader) {
283 return static_cast<const SkShaderBase*>(shader);
284}
285
286inline const SkShaderBase* as_SB(const sk_sp<SkShader>& shader) {
287 return static_cast<SkShaderBase*>(shader.get());
288}
289
290#endif // SkShaderBase_DEFINED