blob: 1e74f833e984eee97b667f64c753d514733161b2 [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"
Mike Klein8e717442020-01-07 10:22:33 -060018#include "src/core/SkVM_fwd.h"
Florin Malita4aed1382017-05-25 10:38:07 -040019
Mike Reede3429e62018-01-19 11:43:34 -050020#if SK_SUPPORT_GPU
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/gpu/GrFPArgs.h"
Mike Reede3429e62018-01-19 11:43:34 -050022#endif
23
Florin Malita4aed1382017-05-25 10:38:07 -040024class GrFragmentProcessor;
25class SkArenaAlloc;
26class SkColorSpace;
Florin Malita4aed1382017-05-25 10:38:07 -040027class SkImage;
28struct SkImageInfo;
29class SkPaint;
30class SkRasterPipeline;
Brian Osman3c358422020-03-23 10:44:12 -040031class SkRuntimeEffect;
Florin Malita4aed1382017-05-25 10:38:07 -040032
Mike Reed9318a6c2019-08-16 16:16:25 -040033/**
34 * Shaders can optionally return a subclass of this when appending their stages.
35 * Doing so tells the caller that the stages can be reused with different CTMs (but nothing
36 * else can change), by calling the updater's udpate() method before each use.
37 *
38 * This can be a perf-win bulk draws like drawAtlas and drawVertices, where most of the setup
39 * (i.e. uniforms) are constant, and only something small is changing (i.e. matrices). This
40 * reuse skips the cost of computing the stages (and/or avoids having to allocate a separate
41 * shader for each small draw.
42 */
43class SkStageUpdater {
44public:
45 virtual ~SkStageUpdater() {}
46
Mike Kleine1508882020-04-28 08:45:01 -050047 virtual bool SK_WARN_UNUSED_RESULT update(const SkMatrix& ctm, const SkMatrix* localM) = 0;
Mike Reed9318a6c2019-08-16 16:16:25 -040048};
49
Florin Malita95c993c2017-05-26 09:44:10 -040050class SkShaderBase : public SkShader {
Florin Malita4aed1382017-05-25 10:38:07 -040051public:
Florin Malita4aed1382017-05-25 10:38:07 -040052 ~SkShaderBase() override;
53
Mike Reed121c2af2020-03-10 14:02:56 -040054 sk_sp<SkShader> makeInvertAlpha() const;
55 sk_sp<SkShader> makeWithCTM(const SkMatrix&) const; // owns its own ctm
56
Florin Malita4aed1382017-05-25 10:38:07 -040057 /**
58 * Returns true if the shader is guaranteed to produce only a single color.
59 * Subclasses can override this to allow loop-hoisting optimization.
60 */
61 virtual bool isConstant() const { return false; }
62
63 const SkMatrix& getLocalMatrix() const { return fLocalMatrix; }
64
65 enum Flags {
66 //!< set if all of the colors will be opaque
67 kOpaqueAlpha_Flag = 1 << 0,
68
69 /** set if the spans only vary in X (const in Y).
70 e.g. an Nx1 bitmap that is being tiled in Y, or a linear-gradient
71 that varies from left-to-right. This flag specifies this for
72 shadeSpan().
73 */
74 kConstInY32_Flag = 1 << 1,
75
76 /** hint for the blitter that 4f is the preferred shading mode.
77 */
78 kPrefers4f_Flag = 1 << 2,
79 };
80
81 /**
82 * ContextRec acts as a parameter bundle for creating Contexts.
83 */
84 struct ContextRec {
Florin Malita4aed1382017-05-25 10:38:07 -040085 ContextRec(const SkPaint& paint, const SkMatrix& matrix, const SkMatrix* localM,
Brian Osman2e8f48e2018-10-19 13:50:48 -040086 SkColorType dstColorType, SkColorSpace* dstColorSpace)
Florin Malita4aed1382017-05-25 10:38:07 -040087 : fPaint(&paint)
88 , fMatrix(&matrix)
89 , fLocalMatrix(localM)
Brian Osman2e8f48e2018-10-19 13:50:48 -040090 , fDstColorType(dstColorType)
Florin Malita4aed1382017-05-25 10:38:07 -040091 , fDstColorSpace(dstColorSpace) {}
92
93 const SkPaint* fPaint; // the current paint associated with the draw
94 const SkMatrix* fMatrix; // the current matrix in the canvas
95 const SkMatrix* fLocalMatrix; // optional local matrix
Brian Osman2e8f48e2018-10-19 13:50:48 -040096 SkColorType fDstColorType; // the color type of the dest surface
Florin Malita4aed1382017-05-25 10:38:07 -040097 SkColorSpace* fDstColorSpace; // the color space of the dest surface (if any)
Mike Reed011d1662019-02-28 17:19:25 -050098
99 bool isLegacyCompatible(SkColorSpace* shadersColorSpace) const;
Florin Malita4aed1382017-05-25 10:38:07 -0400100 };
101
102 class Context : public ::SkNoncopyable {
103 public:
104 Context(const SkShaderBase& shader, const ContextRec&);
105
106 virtual ~Context();
107
108 /**
109 * Called sometimes before drawing with this shader. Return the type of
110 * alpha your shader will return. The default implementation returns 0.
111 * Your subclass should override if it can (even sometimes) report a
112 * non-zero value, since that will enable various blitters to perform
113 * faster.
114 */
115 virtual uint32_t getFlags() const { return 0; }
116
117 /**
118 * Called for each span of the object being drawn. Your subclass should
119 * set the appropriate colors (with premultiplied alpha) that correspond
120 * to the specified device coordinates.
121 */
122 virtual void shadeSpan(int x, int y, SkPMColor[], int count) = 0;
123
Florin Malita4aed1382017-05-25 10:38:07 -0400124 protected:
125 // Reference to shader, so we don't have to dupe information.
126 const SkShaderBase& fShader;
127
Florin Malita4aed1382017-05-25 10:38:07 -0400128 uint8_t getPaintAlpha() const { return fPaintAlpha; }
129 const SkMatrix& getTotalInverse() const { return fTotalInverse; }
Florin Malita4aed1382017-05-25 10:38:07 -0400130 const SkMatrix& getCTM() const { return fCTM; }
131
Florin Malita4aed1382017-05-25 10:38:07 -0400132 private:
133 SkMatrix fCTM;
134 SkMatrix fTotalInverse;
135 uint8_t fPaintAlpha;
Florin Malita4aed1382017-05-25 10:38:07 -0400136
John Stiles7571f9e2020-09-02 22:42:33 -0400137 using INHERITED = SkNoncopyable;
Florin Malita4aed1382017-05-25 10:38:07 -0400138 };
139
140 /**
141 * Make a context using the memory provided by the arena.
142 *
143 * @return pointer to context or nullptr if can't be created
144 */
145 Context* makeContext(const ContextRec&, SkArenaAlloc*) const;
146
147#if SK_SUPPORT_GPU
Florin Malita4aed1382017-05-25 10:38:07 -0400148 /**
Adlai Holler2edf18d2020-10-14 13:02:09 -0400149 * Returns a GrFragmentProcessor that implements the shader for the GPU backend. nullptr is
Florin Malita4aed1382017-05-25 10:38:07 -0400150 * returned if there is no GPU implementation.
151 *
152 * The GPU device does not call SkShader::createContext(), instead we pass the view matrix,
153 * local matrix, and filter quality directly.
154 *
Adlai Holler2edf18d2020-10-14 13:02:09 -0400155 * The GrRecordingContext may be used by the to create textures that are required by the
156 * returned processor.
Florin Malita4aed1382017-05-25 10:38:07 -0400157 *
158 * The returned GrFragmentProcessor should expect an unpremultiplied input color and
159 * produce a premultiplied output.
160 */
Mike Reede3429e62018-01-19 11:43:34 -0500161 virtual std::unique_ptr<GrFragmentProcessor> asFragmentProcessor(const GrFPArgs&) const;
Florin Malita4aed1382017-05-25 10:38:07 -0400162#endif
163
164 /**
165 * If the shader can represent its "average" luminance in a single color, return true and
166 * if color is not NULL, return that color. If it cannot, return false and ignore the color
167 * parameter.
168 *
169 * Note: if this returns true, the returned color will always be opaque, as only the RGB
170 * components are used to compute luminance.
171 */
172 bool asLuminanceColor(SkColor*) const;
173
Mike Reed6867eee2017-06-02 13:25:15 -0400174 // If this returns false, then we draw nothing (do not fall back to shader context)
Mike Klein6d00de32020-08-05 12:41:30 -0500175 SK_WARN_UNUSED_RESULT
Mike Reed1386b2d2019-03-13 21:15:05 -0400176 bool appendStages(const SkStageRec&) const;
Florin Malita4aed1382017-05-25 10:38:07 -0400177
Florin Malitac6c5ead2018-04-11 15:33:40 -0400178 bool SK_WARN_UNUSED_RESULT computeTotalInverse(const SkMatrix& ctm,
179 const SkMatrix* outerLocalMatrix,
180 SkMatrix* totalInverse) const;
181
182 // Returns the total local matrix for this shader:
183 //
184 // M = postLocalMatrix x shaderLocalMatrix x preLocalMatrix
185 //
Florin Malita52f02912020-03-09 16:33:17 -0400186 SkTCopyOnFirstWrite<SkMatrix> totalLocalMatrix(const SkMatrix* preLocalMatrix) const;
Florin Malita4aed1382017-05-25 10:38:07 -0400187
Mike Reedfae8fce2019-04-03 10:27:45 -0400188 virtual SkImage* onIsAImage(SkMatrix*, SkTileMode[2]) const {
Florin Malita4aed1382017-05-25 10:38:07 -0400189 return nullptr;
190 }
191
Brian Osman3c358422020-03-23 10:44:12 -0400192 virtual SkRuntimeEffect* asRuntimeEffect() const { return nullptr; }
193
Mike Klein12956722018-10-19 10:00:21 -0400194 static Type GetFlattenableType() { return kSkShaderBase_Type; }
195 Type getFlattenableType() const override { return GetFlattenableType(); }
196
197 static sk_sp<SkShaderBase> Deserialize(const void* data, size_t size,
198 const SkDeserialProcs* procs = nullptr) {
199 return sk_sp<SkShaderBase>(static_cast<SkShaderBase*>(
200 SkFlattenable::Deserialize(GetFlattenableType(), data, size, procs).release()));
201 }
Mike Kleinfa5f6ce2018-10-20 08:21:31 -0400202 static void RegisterFlattenables();
Florin Malita4aed1382017-05-25 10:38:07 -0400203
Mike Reed7656b2c2019-04-08 11:48:20 -0400204 /** DEPRECATED. skbug.com/8941
205 * If this shader can be represented by another shader + a localMatrix, return that shader and
206 * the localMatrix. If not, return nullptr and ignore the localMatrix parameter.
207 */
208 virtual sk_sp<SkShader> makeAsALocalMatrixShader(SkMatrix* localMatrix) const;
209
Mike Reed9318a6c2019-08-16 16:16:25 -0400210 SkStageUpdater* appendUpdatableStages(const SkStageRec& rec) const {
211 return this->onAppendUpdatableStages(rec);
212 }
213
Mike Klein6d00de32020-08-05 12:41:30 -0500214 SK_WARN_UNUSED_RESULT
Brian Osman5aaaeea2020-06-22 14:26:03 -0400215 skvm::Color program(skvm::Builder*, skvm::Coord device, skvm::Coord local, skvm::Color paint,
Mike Kleine81b1082020-06-19 11:29:13 -0500216 const SkMatrixProvider&, const SkMatrix* localM,
Mike Kleina434e0f2020-03-23 09:33:48 -0500217 SkFilterQuality quality, const SkColorInfo& dst,
Mike Klein276a7852020-03-15 08:46:09 -0500218 skvm::Uniforms* uniforms, SkArenaAlloc* alloc) const;
Mike Kleine4522dc2019-10-29 12:10:34 -0500219
Florin Malita4aed1382017-05-25 10:38:07 -0400220protected:
Florin Malitaf7beee72017-05-26 12:54:32 -0400221 SkShaderBase(const SkMatrix* localMatrix = nullptr);
222
Florin Malita4aed1382017-05-25 10:38:07 -0400223 void flatten(SkWriteBuffer&) const override;
224
Mike Reede92aae62018-10-17 10:21:51 -0400225#ifdef SK_ENABLE_LEGACY_SHADERCONTEXT
Florin Malita4aed1382017-05-25 10:38:07 -0400226 /**
227 * Specialize creating a SkShader context using the supplied allocator.
228 * @return pointer to context owned by the arena allocator.
229 */
230 virtual Context* onMakeContext(const ContextRec&, SkArenaAlloc*) const {
231 return nullptr;
232 }
Mike Reede92aae62018-10-17 10:21:51 -0400233#endif
Florin Malita47e55a52017-06-06 12:26:54 -0400234
Florin Malita4aed1382017-05-25 10:38:07 -0400235 virtual bool onAsLuminanceColor(SkColor*) const {
236 return false;
237 }
238
Mike Reed6867eee2017-06-02 13:25:15 -0400239 // Default impl creates shadercontext and calls that (not very efficient)
Mike Reed1386b2d2019-03-13 21:15:05 -0400240 virtual bool onAppendStages(const SkStageRec&) const;
Florin Malita4aed1382017-05-25 10:38:07 -0400241
Mike Reed9318a6c2019-08-16 16:16:25 -0400242 virtual SkStageUpdater* onAppendUpdatableStages(const SkStageRec&) const { return nullptr; }
243
Mike Klein85754d52020-01-22 10:04:11 -0600244protected:
Mike Kleinb8219e42020-06-24 07:01:40 -0500245 static skvm::Coord ApplyMatrix(skvm::Builder*, const SkMatrix&, skvm::Coord, skvm::Uniforms*);
Mike Klein85754d52020-01-22 10:04:11 -0600246
Florin Malita4aed1382017-05-25 10:38:07 -0400247private:
248 // This is essentially const, but not officially so it can be modified in constructors.
249 SkMatrix fLocalMatrix;
250
Brian Osman5aaaeea2020-06-22 14:26:03 -0400251 virtual skvm::Color onProgram(skvm::Builder*,
252 skvm::Coord device, skvm::Coord local, skvm::Color paint,
Mike Kleine81b1082020-06-19 11:29:13 -0500253 const SkMatrixProvider&, const SkMatrix* localM,
Mike Kleina434e0f2020-03-23 09:33:48 -0500254 SkFilterQuality quality, const SkColorInfo& dst,
Mike Klein276a7852020-03-15 08:46:09 -0500255 skvm::Uniforms* uniforms, SkArenaAlloc* alloc) const;
Mike Klein8e717442020-01-07 10:22:33 -0600256
John Stiles7571f9e2020-09-02 22:42:33 -0400257 using INHERITED = SkShader;
Florin Malita4aed1382017-05-25 10:38:07 -0400258};
259
260inline SkShaderBase* as_SB(SkShader* shader) {
261 return static_cast<SkShaderBase*>(shader);
262}
263
264inline const SkShaderBase* as_SB(const SkShader* shader) {
265 return static_cast<const SkShaderBase*>(shader);
266}
267
268inline const SkShaderBase* as_SB(const sk_sp<SkShader>& shader) {
269 return static_cast<SkShaderBase*>(shader.get());
270}
271
272#endif // SkShaderBase_DEFINED