blob: f72863e4989412b0bc02569554e4af1424b14204 [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"
Mike Reed75ae4212018-01-23 11:24:08 -050012#include "SkMask.h"
Florin Malita4aed1382017-05-25 10:38:07 -040013#include "SkMatrix.h"
14#include "SkShader.h"
15
Mike Reede3429e62018-01-19 11:43:34 -050016#if SK_SUPPORT_GPU
17#include "GrFPArgs.h"
18#endif
19
Florin Malita4aed1382017-05-25 10:38:07 -040020class GrContext;
Brian Salomon4cbb6e62017-10-25 15:12:19 -040021class GrColorSpaceInfo;
Florin Malita4aed1382017-05-25 10:38:07 -040022class GrFragmentProcessor;
23class SkArenaAlloc;
24class SkColorSpace;
25class SkColorSpaceXformer;
26class SkImage;
27struct SkImageInfo;
28class SkPaint;
29class SkRasterPipeline;
30
Florin Malita95c993c2017-05-26 09:44:10 -040031class SkShaderBase : public SkShader {
Florin Malita4aed1382017-05-25 10:38:07 -040032public:
Florin Malita4aed1382017-05-25 10:38:07 -040033 ~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 */
39 virtual bool isConstant() const { return false; }
40
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
68 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 */
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
Florin Malita4aed1382017-05-25 10:38:07 -0400107 // Notification from blitter::blitMask in case we need to see the non-alpha channels
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
Florin Malita4aed1382017-05-25 10:38:07 -0400114 uint8_t getPaintAlpha() const { return fPaintAlpha; }
115 const SkMatrix& getTotalInverse() const { return fTotalInverse; }
Florin Malita4aed1382017-05-25 10:38:07 -0400116 const SkMatrix& getCTM() const { return fCTM; }
117
Florin Malita4aed1382017-05-25 10:38:07 -0400118 private:
119 SkMatrix fCTM;
120 SkMatrix fTotalInverse;
121 uint8_t fPaintAlpha;
Florin Malita4aed1382017-05-25 10:38:07 -0400122
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
Florin Malita47e55a52017-06-06 12:26:54 -0400133 /**
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
Florin Malita4aed1382017-05-25 10:38:07 -0400142#if SK_SUPPORT_GPU
Florin Malita4aed1382017-05-25 10:38:07 -0400143 /**
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 */
Mike Reede3429e62018-01-19 11:43:34 -0500156 virtual std::unique_ptr<GrFragmentProcessor> asFragmentProcessor(const GrFPArgs&) const;
Florin Malita4aed1382017-05-25 10:38:07 -0400157#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 */
172 sk_sp<SkShader> makeColorSpace(SkColorSpaceXformer* xformer) const {
173 return this->onMakeColorSpace(xformer);
174 }
175
Mike Reed34042072017-08-08 16:29:22 -0400176 bool isRasterPipelineOnly(const SkMatrix& ctm) const {
Florin Malita5769dd22017-07-12 13:31:25 -0400177 // We always use RP when perspective is present.
Mike Reed34042072017-08-08 16:29:22 -0400178 return ctm.hasPerspective() || fLocalMatrix.hasPerspective()
179 || this->onIsRasterPipelineOnly(ctm);
Florin Malita5769dd22017-07-12 13:31:25 -0400180 }
Florin Malita4aed1382017-05-25 10:38:07 -0400181
Mike Reed1d8c42e2017-08-29 14:58:19 -0400182 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
Mike Reed6867eee2017-06-02 13:25:15 -0400191 // If this returns false, then we draw nothing (do not fall back to shader context)
Mike Reed1d8c42e2017-08-29 14:58:19 -0400192 bool appendStages(const StageRec&) const;
Florin Malita4aed1382017-05-25 10:38:07 -0400193
194 bool computeTotalInverse(const SkMatrix& ctm,
195 const SkMatrix* outerLocalMatrix,
196 SkMatrix* totalInverse) const;
197
198#ifdef SK_SUPPORT_LEGACY_SHADER_ISABITMAP
199 virtual bool onIsABitmap(SkBitmap*, SkMatrix*, TileMode[2]) const {
200 return false;
201 }
202#endif
203
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
213protected:
Florin Malitaf7beee72017-05-26 12:54:32 -0400214 SkShaderBase(const SkMatrix* localMatrix = nullptr);
215
Florin Malita4aed1382017-05-25 10:38:07 -0400216 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 */
222 virtual Context* onMakeContext(const ContextRec&, SkArenaAlloc*) const {
223 return nullptr;
224 }
225
Florin Malita47e55a52017-06-06 12:26:54 -0400226 /**
227 * Overriden by shaders which prefer burst mode.
228 */
229 virtual Context* onMakeBurstPipelineContext(const ContextRec&, SkArenaAlloc*) const {
230 return nullptr;
231 }
232
Florin Malita4aed1382017-05-25 10:38:07 -0400233 virtual bool onAsLuminanceColor(SkColor*) const {
234 return false;
235 }
236
237 virtual sk_sp<SkShader> onMakeColorSpace(SkColorSpaceXformer*) const {
238 return sk_ref_sp(const_cast<SkShaderBase*>(this));
239 }
240
Mike Reed6867eee2017-06-02 13:25:15 -0400241 // Default impl creates shadercontext and calls that (not very efficient)
Mike Reed1d8c42e2017-08-29 14:58:19 -0400242 virtual bool onAppendStages(const StageRec&) const;
Florin Malita4aed1382017-05-25 10:38:07 -0400243
Mike Reed34042072017-08-08 16:29:22 -0400244 virtual bool onIsRasterPipelineOnly(const SkMatrix& ctm) const { return false; }
Florin Malita5769dd22017-07-12 13:31:25 -0400245
Florin Malita4aed1382017-05-25 10:38:07 -0400246private:
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
253inline SkShaderBase* as_SB(SkShader* shader) {
254 return static_cast<SkShaderBase*>(shader);
255}
256
257inline const SkShaderBase* as_SB(const SkShader* shader) {
258 return static_cast<const SkShaderBase*>(shader);
259}
260
261inline const SkShaderBase* as_SB(const sk_sp<SkShader>& shader) {
262 return static_cast<SkShaderBase*>(shader.get());
263}
264
265#endif // SkShaderBase_DEFINED