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