blob: 253d491ba8e46b9cb3c1a93645f371e72ed22659 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkFilterQuality.h"
12#include "include/core/SkMatrix.h"
13#include "include/core/SkShader.h"
14#include "include/private/SkNoncopyable.h"
15#include "src/core/SkEffectPriv.h"
16#include "src/core/SkMask.h"
17#include "src/core/SkTLazy.h"
Florin Malita4aed1382017-05-25 10:38:07 -040018
Mike Reede3429e62018-01-19 11:43:34 -050019#if SK_SUPPORT_GPU
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/gpu/GrFPArgs.h"
Mike Reede3429e62018-01-19 11:43:34 -050021#endif
22
Florin Malita4aed1382017-05-25 10:38:07 -040023class GrContext;
24class GrFragmentProcessor;
25class SkArenaAlloc;
26class SkColorSpace;
Florin Malita4aed1382017-05-25 10:38:07 -040027class SkImage;
28struct SkImageInfo;
29class SkPaint;
30class SkRasterPipeline;
31
Florin Malita95c993c2017-05-26 09:44:10 -040032class SkShaderBase : public SkShader {
Florin Malita4aed1382017-05-25 10:38:07 -040033public:
Florin Malita4aed1382017-05-25 10:38:07 -040034 ~SkShaderBase() override;
35
36 /**
37 * Returns true if the shader is guaranteed to produce only a single color.
38 * Subclasses can override this to allow loop-hoisting optimization.
39 */
40 virtual bool isConstant() const { return false; }
41
42 const SkMatrix& getLocalMatrix() const { return fLocalMatrix; }
43
44 enum Flags {
45 //!< set if all of the colors will be opaque
46 kOpaqueAlpha_Flag = 1 << 0,
47
48 /** set if the spans only vary in X (const in Y).
49 e.g. an Nx1 bitmap that is being tiled in Y, or a linear-gradient
50 that varies from left-to-right. This flag specifies this for
51 shadeSpan().
52 */
53 kConstInY32_Flag = 1 << 1,
54
55 /** hint for the blitter that 4f is the preferred shading mode.
56 */
57 kPrefers4f_Flag = 1 << 2,
58 };
59
60 /**
61 * ContextRec acts as a parameter bundle for creating Contexts.
62 */
63 struct ContextRec {
Florin Malita4aed1382017-05-25 10:38:07 -040064 ContextRec(const SkPaint& paint, const SkMatrix& matrix, const SkMatrix* localM,
Brian Osman2e8f48e2018-10-19 13:50:48 -040065 SkColorType dstColorType, SkColorSpace* dstColorSpace)
Florin Malita4aed1382017-05-25 10:38:07 -040066 : fPaint(&paint)
67 , fMatrix(&matrix)
68 , fLocalMatrix(localM)
Brian Osman2e8f48e2018-10-19 13:50:48 -040069 , fDstColorType(dstColorType)
Florin Malita4aed1382017-05-25 10:38:07 -040070 , fDstColorSpace(dstColorSpace) {}
71
72 const SkPaint* fPaint; // the current paint associated with the draw
73 const SkMatrix* fMatrix; // the current matrix in the canvas
74 const SkMatrix* fLocalMatrix; // optional local matrix
Brian Osman2e8f48e2018-10-19 13:50:48 -040075 SkColorType fDstColorType; // the color type of the dest surface
Florin Malita4aed1382017-05-25 10:38:07 -040076 SkColorSpace* fDstColorSpace; // the color space of the dest surface (if any)
Mike Reed011d1662019-02-28 17:19:25 -050077
78 bool isLegacyCompatible(SkColorSpace* shadersColorSpace) const;
Florin Malita4aed1382017-05-25 10:38:07 -040079 };
80
81 class Context : public ::SkNoncopyable {
82 public:
83 Context(const SkShaderBase& shader, const ContextRec&);
84
85 virtual ~Context();
86
87 /**
88 * Called sometimes before drawing with this shader. Return the type of
89 * alpha your shader will return. The default implementation returns 0.
90 * Your subclass should override if it can (even sometimes) report a
91 * non-zero value, since that will enable various blitters to perform
92 * faster.
93 */
94 virtual uint32_t getFlags() const { return 0; }
95
96 /**
97 * Called for each span of the object being drawn. Your subclass should
98 * set the appropriate colors (with premultiplied alpha) that correspond
99 * to the specified device coordinates.
100 */
101 virtual void shadeSpan(int x, int y, SkPMColor[], int count) = 0;
102
Florin Malita4aed1382017-05-25 10:38:07 -0400103 protected:
104 // Reference to shader, so we don't have to dupe information.
105 const SkShaderBase& fShader;
106
Florin Malita4aed1382017-05-25 10:38:07 -0400107 uint8_t getPaintAlpha() const { return fPaintAlpha; }
108 const SkMatrix& getTotalInverse() const { return fTotalInverse; }
Florin Malita4aed1382017-05-25 10:38:07 -0400109 const SkMatrix& getCTM() const { return fCTM; }
110
Florin Malita4aed1382017-05-25 10:38:07 -0400111 private:
112 SkMatrix fCTM;
113 SkMatrix fTotalInverse;
114 uint8_t fPaintAlpha;
Florin Malita4aed1382017-05-25 10:38:07 -0400115
116 typedef SkNoncopyable INHERITED;
117 };
118
119 /**
120 * Make a context using the memory provided by the arena.
121 *
122 * @return pointer to context or nullptr if can't be created
123 */
124 Context* makeContext(const ContextRec&, SkArenaAlloc*) const;
125
126#if SK_SUPPORT_GPU
Florin Malita4aed1382017-05-25 10:38:07 -0400127 /**
128 * Returns a GrFragmentProcessor that implements the shader for the GPU backend. NULL is
129 * returned if there is no GPU implementation.
130 *
131 * The GPU device does not call SkShader::createContext(), instead we pass the view matrix,
132 * local matrix, and filter quality directly.
133 *
134 * The GrContext may be used by the to create textures that are required by the returned
135 * processor.
136 *
137 * The returned GrFragmentProcessor should expect an unpremultiplied input color and
138 * produce a premultiplied output.
139 */
Mike Reede3429e62018-01-19 11:43:34 -0500140 virtual std::unique_ptr<GrFragmentProcessor> asFragmentProcessor(const GrFPArgs&) const;
Florin Malita4aed1382017-05-25 10:38:07 -0400141#endif
142
143 /**
144 * If the shader can represent its "average" luminance in a single color, return true and
145 * if color is not NULL, return that color. If it cannot, return false and ignore the color
146 * parameter.
147 *
148 * Note: if this returns true, the returned color will always be opaque, as only the RGB
149 * components are used to compute luminance.
150 */
151 bool asLuminanceColor(SkColor*) const;
152
Mike Reed6867eee2017-06-02 13:25:15 -0400153 // If this returns false, then we draw nothing (do not fall back to shader context)
Mike Reed1386b2d2019-03-13 21:15:05 -0400154 bool appendStages(const SkStageRec&) const;
Florin Malita4aed1382017-05-25 10:38:07 -0400155
Florin Malitac6c5ead2018-04-11 15:33:40 -0400156 bool SK_WARN_UNUSED_RESULT computeTotalInverse(const SkMatrix& ctm,
157 const SkMatrix* outerLocalMatrix,
158 SkMatrix* totalInverse) const;
159
160 // Returns the total local matrix for this shader:
161 //
162 // M = postLocalMatrix x shaderLocalMatrix x preLocalMatrix
163 //
164 SkTCopyOnFirstWrite<SkMatrix> totalLocalMatrix(const SkMatrix* preLocalMatrix,
165 const SkMatrix* postLocalMatrix = nullptr) const;
Florin Malita4aed1382017-05-25 10:38:07 -0400166
Mike Reedfae8fce2019-04-03 10:27:45 -0400167 virtual SkImage* onIsAImage(SkMatrix*, SkTileMode[2]) const {
Florin Malita4aed1382017-05-25 10:38:07 -0400168 return nullptr;
169 }
Hal Canary8a64fa62019-05-30 10:29:40 -0400170 virtual SkPicture* isAPicture(SkMatrix*, SkTileMode[2], SkRect* tile) const { return nullptr; }
Florin Malita4aed1382017-05-25 10:38:07 -0400171
Mike Klein12956722018-10-19 10:00:21 -0400172 static Type GetFlattenableType() { return kSkShaderBase_Type; }
173 Type getFlattenableType() const override { return GetFlattenableType(); }
174
175 static sk_sp<SkShaderBase> Deserialize(const void* data, size_t size,
176 const SkDeserialProcs* procs = nullptr) {
177 return sk_sp<SkShaderBase>(static_cast<SkShaderBase*>(
178 SkFlattenable::Deserialize(GetFlattenableType(), data, size, procs).release()));
179 }
Mike Kleinfa5f6ce2018-10-20 08:21:31 -0400180 static void RegisterFlattenables();
Florin Malita4aed1382017-05-25 10:38:07 -0400181
Mike Reed7656b2c2019-04-08 11:48:20 -0400182 /** DEPRECATED. skbug.com/8941
183 * If this shader can be represented by another shader + a localMatrix, return that shader and
184 * the localMatrix. If not, return nullptr and ignore the localMatrix parameter.
185 */
186 virtual sk_sp<SkShader> makeAsALocalMatrixShader(SkMatrix* localMatrix) const;
187
Florin Malita4aed1382017-05-25 10:38:07 -0400188protected:
Florin Malitaf7beee72017-05-26 12:54:32 -0400189 SkShaderBase(const SkMatrix* localMatrix = nullptr);
190
Florin Malita4aed1382017-05-25 10:38:07 -0400191 void flatten(SkWriteBuffer&) const override;
192
Mike Reede92aae62018-10-17 10:21:51 -0400193#ifdef SK_ENABLE_LEGACY_SHADERCONTEXT
Florin Malita4aed1382017-05-25 10:38:07 -0400194 /**
195 * Specialize creating a SkShader context using the supplied allocator.
196 * @return pointer to context owned by the arena allocator.
197 */
198 virtual Context* onMakeContext(const ContextRec&, SkArenaAlloc*) const {
199 return nullptr;
200 }
201
Florin Malita47e55a52017-06-06 12:26:54 -0400202 /**
203 * Overriden by shaders which prefer burst mode.
204 */
205 virtual Context* onMakeBurstPipelineContext(const ContextRec&, SkArenaAlloc*) const {
206 return nullptr;
207 }
Mike Reede92aae62018-10-17 10:21:51 -0400208#endif
Florin Malita47e55a52017-06-06 12:26:54 -0400209
Florin Malita4aed1382017-05-25 10:38:07 -0400210 virtual bool onAsLuminanceColor(SkColor*) const {
211 return false;
212 }
213
Mike Reed6867eee2017-06-02 13:25:15 -0400214 // Default impl creates shadercontext and calls that (not very efficient)
Mike Reed1386b2d2019-03-13 21:15:05 -0400215 virtual bool onAppendStages(const SkStageRec&) const;
Florin Malita4aed1382017-05-25 10:38:07 -0400216
Florin Malita4aed1382017-05-25 10:38:07 -0400217private:
218 // This is essentially const, but not officially so it can be modified in constructors.
219 SkMatrix fLocalMatrix;
220
221 typedef SkShader INHERITED;
222};
223
224inline SkShaderBase* as_SB(SkShader* shader) {
225 return static_cast<SkShaderBase*>(shader);
226}
227
228inline const SkShaderBase* as_SB(const SkShader* shader) {
229 return static_cast<const SkShaderBase*>(shader);
230}
231
232inline const SkShaderBase* as_SB(const sk_sp<SkShader>& shader) {
233 return static_cast<SkShaderBase*>(shader.get());
234}
235
236#endif // SkShaderBase_DEFINED