blob: 9cfca4c6bae2e44e3b7f63083d87a5edd2427391 [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"
Cary Clark4dc5a452018-05-21 11:56:57 -040012#include "SkFlattenablePriv.h"
Mike Reed75ae4212018-01-23 11:24:08 -050013#include "SkMask.h"
Florin Malita4aed1382017-05-25 10:38:07 -040014#include "SkMatrix.h"
15#include "SkShader.h"
Florin Malitac6c5ead2018-04-11 15:33:40 -040016#include "SkTLazy.h"
Florin Malita4aed1382017-05-25 10:38:07 -040017
Mike Reede3429e62018-01-19 11:43:34 -050018#if SK_SUPPORT_GPU
19#include "GrFPArgs.h"
20#endif
21
Florin Malita4aed1382017-05-25 10:38:07 -040022class GrContext;
Brian Salomon4cbb6e62017-10-25 15:12:19 -040023class GrColorSpaceInfo;
Florin Malita4aed1382017-05-25 10:38:07 -040024class GrFragmentProcessor;
25class SkArenaAlloc;
26class SkColorSpace;
27class SkColorSpaceXformer;
28class SkImage;
29struct SkImageInfo;
30class SkPaint;
31class SkRasterPipeline;
32
Florin Malita95c993c2017-05-26 09:44:10 -040033class SkShaderBase : public SkShader {
Florin Malita4aed1382017-05-25 10:38:07 -040034public:
Florin Malita4aed1382017-05-25 10:38:07 -040035 ~SkShaderBase() override;
36
37 /**
38 * Returns true if the shader is guaranteed to produce only a single color.
39 * Subclasses can override this to allow loop-hoisting optimization.
40 */
41 virtual bool isConstant() const { return false; }
42
43 const SkMatrix& getLocalMatrix() const { return fLocalMatrix; }
44
45 enum Flags {
46 //!< set if all of the colors will be opaque
47 kOpaqueAlpha_Flag = 1 << 0,
48
49 /** set if the spans only vary in X (const in Y).
50 e.g. an Nx1 bitmap that is being tiled in Y, or a linear-gradient
51 that varies from left-to-right. This flag specifies this for
52 shadeSpan().
53 */
54 kConstInY32_Flag = 1 << 1,
55
56 /** hint for the blitter that 4f is the preferred shading mode.
57 */
58 kPrefers4f_Flag = 1 << 2,
59 };
60
61 /**
62 * ContextRec acts as a parameter bundle for creating Contexts.
63 */
64 struct ContextRec {
65 enum DstType {
66 kPMColor_DstType, // clients prefer shading into PMColor dest
67 kPM4f_DstType, // clients prefer shading into PM4f dest
68 };
69
70 ContextRec(const SkPaint& paint, const SkMatrix& matrix, const SkMatrix* localM,
71 DstType dstType, SkColorSpace* dstColorSpace)
72 : fPaint(&paint)
73 , fMatrix(&matrix)
74 , fLocalMatrix(localM)
75 , fPreferredDstType(dstType)
76 , fDstColorSpace(dstColorSpace) {}
77
78 const SkPaint* fPaint; // the current paint associated with the draw
79 const SkMatrix* fMatrix; // the current matrix in the canvas
80 const SkMatrix* fLocalMatrix; // optional local matrix
81 const DstType fPreferredDstType; // the "natural" client dest type
82 SkColorSpace* fDstColorSpace; // the color space of the dest surface (if any)
83 };
84
85 class Context : public ::SkNoncopyable {
86 public:
87 Context(const SkShaderBase& shader, const ContextRec&);
88
89 virtual ~Context();
90
91 /**
92 * Called sometimes before drawing with this shader. Return the type of
93 * alpha your shader will return. The default implementation returns 0.
94 * Your subclass should override if it can (even sometimes) report a
95 * non-zero value, since that will enable various blitters to perform
96 * faster.
97 */
98 virtual uint32_t getFlags() const { return 0; }
99
100 /**
101 * Called for each span of the object being drawn. Your subclass should
102 * set the appropriate colors (with premultiplied alpha) that correspond
103 * to the specified device coordinates.
104 */
105 virtual void shadeSpan(int x, int y, SkPMColor[], int count) = 0;
106
107 virtual void shadeSpan4f(int x, int y, SkPM4f[], int count);
108
Florin Malita4aed1382017-05-25 10:38:07 -0400109 // Notification from blitter::blitMask in case we need to see the non-alpha channels
110 virtual void set3DMask(const SkMask*) {}
111
112 protected:
113 // Reference to shader, so we don't have to dupe information.
114 const SkShaderBase& fShader;
115
Florin Malita4aed1382017-05-25 10:38:07 -0400116 uint8_t getPaintAlpha() const { return fPaintAlpha; }
117 const SkMatrix& getTotalInverse() const { return fTotalInverse; }
Florin Malita4aed1382017-05-25 10:38:07 -0400118 const SkMatrix& getCTM() const { return fCTM; }
119
Florin Malita4aed1382017-05-25 10:38:07 -0400120 private:
121 SkMatrix fCTM;
122 SkMatrix fTotalInverse;
123 uint8_t fPaintAlpha;
Florin Malita4aed1382017-05-25 10:38:07 -0400124
125 typedef SkNoncopyable INHERITED;
126 };
127
128 /**
129 * Make a context using the memory provided by the arena.
130 *
131 * @return pointer to context or nullptr if can't be created
132 */
133 Context* makeContext(const ContextRec&, SkArenaAlloc*) const;
134
Florin Malita47e55a52017-06-06 12:26:54 -0400135 /**
136 * Shaders may opt-in for burst mode, if they can operate
137 * significantly more efficiently in that mode.
138 *
139 * Burst mode is prioritized in SkRasterPipelineBlitter over
140 * regular (appendStages) pipeline operation.
141 */
142 Context* makeBurstPipelineContext(const ContextRec&, SkArenaAlloc*) const;
143
Florin Malita4aed1382017-05-25 10:38:07 -0400144#if SK_SUPPORT_GPU
Florin Malita4aed1382017-05-25 10:38:07 -0400145 /**
146 * Returns a GrFragmentProcessor that implements the shader for the GPU backend. NULL is
147 * returned if there is no GPU implementation.
148 *
149 * The GPU device does not call SkShader::createContext(), instead we pass the view matrix,
150 * local matrix, and filter quality directly.
151 *
152 * The GrContext may be used by the to create textures that are required by the returned
153 * processor.
154 *
155 * The returned GrFragmentProcessor should expect an unpremultiplied input color and
156 * produce a premultiplied output.
157 */
Mike Reede3429e62018-01-19 11:43:34 -0500158 virtual std::unique_ptr<GrFragmentProcessor> asFragmentProcessor(const GrFPArgs&) const;
Florin Malita4aed1382017-05-25 10:38:07 -0400159#endif
160
161 /**
162 * If the shader can represent its "average" luminance in a single color, return true and
163 * if color is not NULL, return that color. If it cannot, return false and ignore the color
164 * parameter.
165 *
166 * Note: if this returns true, the returned color will always be opaque, as only the RGB
167 * components are used to compute luminance.
168 */
169 bool asLuminanceColor(SkColor*) const;
170
171 /**
172 * Returns a shader transformed into a new color space via the |xformer|.
173 */
174 sk_sp<SkShader> makeColorSpace(SkColorSpaceXformer* xformer) const {
175 return this->onMakeColorSpace(xformer);
176 }
177
Mike Reed1d8c42e2017-08-29 14:58:19 -0400178 struct StageRec {
179 SkRasterPipeline* fPipeline;
180 SkArenaAlloc* fAlloc;
181 SkColorSpace* fDstCS; // may be nullptr
182 const SkPaint& fPaint;
183 const SkMatrix* fLocalM; // may be nullptr
184 SkMatrix fCTM;
185 };
186
Mike Reed6867eee2017-06-02 13:25:15 -0400187 // If this returns false, then we draw nothing (do not fall back to shader context)
Mike Reed1d8c42e2017-08-29 14:58:19 -0400188 bool appendStages(const StageRec&) const;
Florin Malita4aed1382017-05-25 10:38:07 -0400189
Florin Malitac6c5ead2018-04-11 15:33:40 -0400190 bool SK_WARN_UNUSED_RESULT computeTotalInverse(const SkMatrix& ctm,
191 const SkMatrix* outerLocalMatrix,
192 SkMatrix* totalInverse) const;
193
194 // Returns the total local matrix for this shader:
195 //
196 // M = postLocalMatrix x shaderLocalMatrix x preLocalMatrix
197 //
198 SkTCopyOnFirstWrite<SkMatrix> totalLocalMatrix(const SkMatrix* preLocalMatrix,
199 const SkMatrix* postLocalMatrix = nullptr) const;
Florin Malita4aed1382017-05-25 10:38:07 -0400200
201#ifdef SK_SUPPORT_LEGACY_SHADER_ISABITMAP
202 virtual bool onIsABitmap(SkBitmap*, SkMatrix*, TileMode[2]) const {
203 return false;
204 }
205#endif
206
207 virtual SkImage* onIsAImage(SkMatrix*, TileMode[2]) const {
208 return nullptr;
209 }
210
Florin Malita4aed1382017-05-25 10:38:07 -0400211 SK_DEFINE_FLATTENABLE_TYPE(SkShaderBase)
212 SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP()
213
214protected:
Florin Malitaf7beee72017-05-26 12:54:32 -0400215 SkShaderBase(const SkMatrix* localMatrix = nullptr);
216
Florin Malita4aed1382017-05-25 10:38:07 -0400217 void flatten(SkWriteBuffer&) const override;
218
219 /**
220 * Specialize creating a SkShader context using the supplied allocator.
221 * @return pointer to context owned by the arena allocator.
222 */
223 virtual Context* onMakeContext(const ContextRec&, SkArenaAlloc*) const {
224 return nullptr;
225 }
226
Florin Malita47e55a52017-06-06 12:26:54 -0400227 /**
228 * Overriden by shaders which prefer burst mode.
229 */
230 virtual Context* onMakeBurstPipelineContext(const ContextRec&, SkArenaAlloc*) const {
231 return nullptr;
232 }
233
Florin Malita4aed1382017-05-25 10:38:07 -0400234 virtual bool onAsLuminanceColor(SkColor*) const {
235 return false;
236 }
237
238 virtual sk_sp<SkShader> onMakeColorSpace(SkColorSpaceXformer*) const {
239 return sk_ref_sp(const_cast<SkShaderBase*>(this));
240 }
241
Mike Reed6867eee2017-06-02 13:25:15 -0400242 // Default impl creates shadercontext and calls that (not very efficient)
Mike Reed1d8c42e2017-08-29 14:58:19 -0400243 virtual bool onAppendStages(const StageRec&) const;
Florin Malita4aed1382017-05-25 10:38:07 -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