blob: 07b90ce3dd5ce0a4057cf92f667bdcaa3b19a4c2 [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
Robert Phillips31722082020-10-14 18:30:06 +000024class GrContext;
Florin Malita4aed1382017-05-25 10:38:07 -040025class GrFragmentProcessor;
26class SkArenaAlloc;
27class SkColorSpace;
Florin Malita4aed1382017-05-25 10:38:07 -040028class SkImage;
29struct SkImageInfo;
30class SkPaint;
31class SkRasterPipeline;
Brian Osman3c358422020-03-23 10:44:12 -040032class SkRuntimeEffect;
Florin Malita4aed1382017-05-25 10:38:07 -040033
Mike Reed9318a6c2019-08-16 16:16:25 -040034/**
35 * Shaders can optionally return a subclass of this when appending their stages.
36 * Doing so tells the caller that the stages can be reused with different CTMs (but nothing
37 * else can change), by calling the updater's udpate() method before each use.
38 *
39 * This can be a perf-win bulk draws like drawAtlas and drawVertices, where most of the setup
40 * (i.e. uniforms) are constant, and only something small is changing (i.e. matrices). This
41 * reuse skips the cost of computing the stages (and/or avoids having to allocate a separate
42 * shader for each small draw.
43 */
44class SkStageUpdater {
45public:
46 virtual ~SkStageUpdater() {}
47
Mike Kleine1508882020-04-28 08:45:01 -050048 virtual bool SK_WARN_UNUSED_RESULT update(const SkMatrix& ctm, const SkMatrix* localM) = 0;
Mike Reed9318a6c2019-08-16 16:16:25 -040049};
50
Florin Malita95c993c2017-05-26 09:44:10 -040051class SkShaderBase : public SkShader {
Florin Malita4aed1382017-05-25 10:38:07 -040052public:
Florin Malita4aed1382017-05-25 10:38:07 -040053 ~SkShaderBase() override;
54
Mike Reed121c2af2020-03-10 14:02:56 -040055 sk_sp<SkShader> makeInvertAlpha() const;
56 sk_sp<SkShader> makeWithCTM(const SkMatrix&) const; // owns its own ctm
57
Florin Malita4aed1382017-05-25 10:38:07 -040058 /**
59 * Returns true if the shader is guaranteed to produce only a single color.
60 * Subclasses can override this to allow loop-hoisting optimization.
61 */
62 virtual bool isConstant() const { return false; }
63
64 const SkMatrix& getLocalMatrix() const { return fLocalMatrix; }
65
66 enum Flags {
67 //!< set if all of the colors will be opaque
68 kOpaqueAlpha_Flag = 1 << 0,
69
70 /** set if the spans only vary in X (const in Y).
71 e.g. an Nx1 bitmap that is being tiled in Y, or a linear-gradient
72 that varies from left-to-right. This flag specifies this for
73 shadeSpan().
74 */
75 kConstInY32_Flag = 1 << 1,
76
77 /** hint for the blitter that 4f is the preferred shading mode.
78 */
79 kPrefers4f_Flag = 1 << 2,
80 };
81
82 /**
83 * ContextRec acts as a parameter bundle for creating Contexts.
84 */
85 struct ContextRec {
Florin Malita4aed1382017-05-25 10:38:07 -040086 ContextRec(const SkPaint& paint, const SkMatrix& matrix, const SkMatrix* localM,
Brian Osman2e8f48e2018-10-19 13:50:48 -040087 SkColorType dstColorType, SkColorSpace* dstColorSpace)
Florin Malita4aed1382017-05-25 10:38:07 -040088 : fPaint(&paint)
89 , fMatrix(&matrix)
90 , fLocalMatrix(localM)
Brian Osman2e8f48e2018-10-19 13:50:48 -040091 , fDstColorType(dstColorType)
Florin Malita4aed1382017-05-25 10:38:07 -040092 , fDstColorSpace(dstColorSpace) {}
93
94 const SkPaint* fPaint; // the current paint associated with the draw
95 const SkMatrix* fMatrix; // the current matrix in the canvas
96 const SkMatrix* fLocalMatrix; // optional local matrix
Brian Osman2e8f48e2018-10-19 13:50:48 -040097 SkColorType fDstColorType; // the color type of the dest surface
Florin Malita4aed1382017-05-25 10:38:07 -040098 SkColorSpace* fDstColorSpace; // the color space of the dest surface (if any)
Mike Reed011d1662019-02-28 17:19:25 -050099
100 bool isLegacyCompatible(SkColorSpace* shadersColorSpace) const;
Florin Malita4aed1382017-05-25 10:38:07 -0400101 };
102
103 class Context : public ::SkNoncopyable {
104 public:
105 Context(const SkShaderBase& shader, const ContextRec&);
106
107 virtual ~Context();
108
109 /**
110 * Called sometimes before drawing with this shader. Return the type of
111 * alpha your shader will return. The default implementation returns 0.
112 * Your subclass should override if it can (even sometimes) report a
113 * non-zero value, since that will enable various blitters to perform
114 * faster.
115 */
116 virtual uint32_t getFlags() const { return 0; }
117
118 /**
119 * Called for each span of the object being drawn. Your subclass should
120 * set the appropriate colors (with premultiplied alpha) that correspond
121 * to the specified device coordinates.
122 */
123 virtual void shadeSpan(int x, int y, SkPMColor[], int count) = 0;
124
Florin Malita4aed1382017-05-25 10:38:07 -0400125 protected:
126 // Reference to shader, so we don't have to dupe information.
127 const SkShaderBase& fShader;
128
Florin Malita4aed1382017-05-25 10:38:07 -0400129 uint8_t getPaintAlpha() const { return fPaintAlpha; }
130 const SkMatrix& getTotalInverse() const { return fTotalInverse; }
Florin Malita4aed1382017-05-25 10:38:07 -0400131 const SkMatrix& getCTM() const { return fCTM; }
132
Florin Malita4aed1382017-05-25 10:38:07 -0400133 private:
134 SkMatrix fCTM;
135 SkMatrix fTotalInverse;
136 uint8_t fPaintAlpha;
Florin Malita4aed1382017-05-25 10:38:07 -0400137
John Stiles7571f9e2020-09-02 22:42:33 -0400138 using INHERITED = SkNoncopyable;
Florin Malita4aed1382017-05-25 10:38:07 -0400139 };
140
141 /**
142 * Make a context using the memory provided by the arena.
143 *
144 * @return pointer to context or nullptr if can't be created
145 */
146 Context* makeContext(const ContextRec&, SkArenaAlloc*) const;
147
148#if SK_SUPPORT_GPU
Florin Malita4aed1382017-05-25 10:38:07 -0400149 /**
Robert Phillips31722082020-10-14 18:30:06 +0000150 * Returns a GrFragmentProcessor that implements the shader for the GPU backend. NULL is
Florin Malita4aed1382017-05-25 10:38:07 -0400151 * returned if there is no GPU implementation.
152 *
153 * The GPU device does not call SkShader::createContext(), instead we pass the view matrix,
154 * local matrix, and filter quality directly.
155 *
Robert Phillips31722082020-10-14 18:30:06 +0000156 * The GrContext may be used by the to create textures that are required by the returned
157 * processor.
Florin Malita4aed1382017-05-25 10:38:07 -0400158 *
159 * The returned GrFragmentProcessor should expect an unpremultiplied input color and
160 * produce a premultiplied output.
161 */
Mike Reede3429e62018-01-19 11:43:34 -0500162 virtual std::unique_ptr<GrFragmentProcessor> asFragmentProcessor(const GrFPArgs&) const;
Florin Malita4aed1382017-05-25 10:38:07 -0400163#endif
164
165 /**
166 * If the shader can represent its "average" luminance in a single color, return true and
167 * if color is not NULL, return that color. If it cannot, return false and ignore the color
168 * parameter.
169 *
170 * Note: if this returns true, the returned color will always be opaque, as only the RGB
171 * components are used to compute luminance.
172 */
173 bool asLuminanceColor(SkColor*) const;
174
Mike Reed6867eee2017-06-02 13:25:15 -0400175 // If this returns false, then we draw nothing (do not fall back to shader context)
Mike Klein6d00de32020-08-05 12:41:30 -0500176 SK_WARN_UNUSED_RESULT
Mike Reed1386b2d2019-03-13 21:15:05 -0400177 bool appendStages(const SkStageRec&) const;
Florin Malita4aed1382017-05-25 10:38:07 -0400178
Florin Malitac6c5ead2018-04-11 15:33:40 -0400179 bool SK_WARN_UNUSED_RESULT computeTotalInverse(const SkMatrix& ctm,
180 const SkMatrix* outerLocalMatrix,
181 SkMatrix* totalInverse) const;
182
183 // Returns the total local matrix for this shader:
184 //
185 // M = postLocalMatrix x shaderLocalMatrix x preLocalMatrix
186 //
Florin Malita52f02912020-03-09 16:33:17 -0400187 SkTCopyOnFirstWrite<SkMatrix> totalLocalMatrix(const SkMatrix* preLocalMatrix) const;
Florin Malita4aed1382017-05-25 10:38:07 -0400188
Mike Reedfae8fce2019-04-03 10:27:45 -0400189 virtual SkImage* onIsAImage(SkMatrix*, SkTileMode[2]) const {
Florin Malita4aed1382017-05-25 10:38:07 -0400190 return nullptr;
191 }
192
Brian Osman3c358422020-03-23 10:44:12 -0400193 virtual SkRuntimeEffect* asRuntimeEffect() const { return nullptr; }
194
Mike Klein12956722018-10-19 10:00:21 -0400195 static Type GetFlattenableType() { return kSkShaderBase_Type; }
196 Type getFlattenableType() const override { return GetFlattenableType(); }
197
198 static sk_sp<SkShaderBase> Deserialize(const void* data, size_t size,
199 const SkDeserialProcs* procs = nullptr) {
200 return sk_sp<SkShaderBase>(static_cast<SkShaderBase*>(
201 SkFlattenable::Deserialize(GetFlattenableType(), data, size, procs).release()));
202 }
Mike Kleinfa5f6ce2018-10-20 08:21:31 -0400203 static void RegisterFlattenables();
Florin Malita4aed1382017-05-25 10:38:07 -0400204
Mike Reed7656b2c2019-04-08 11:48:20 -0400205 /** DEPRECATED. skbug.com/8941
206 * If this shader can be represented by another shader + a localMatrix, return that shader and
207 * the localMatrix. If not, return nullptr and ignore the localMatrix parameter.
208 */
209 virtual sk_sp<SkShader> makeAsALocalMatrixShader(SkMatrix* localMatrix) const;
210
Mike Reed9318a6c2019-08-16 16:16:25 -0400211 SkStageUpdater* appendUpdatableStages(const SkStageRec& rec) const {
212 return this->onAppendUpdatableStages(rec);
213 }
214
Mike Klein6d00de32020-08-05 12:41:30 -0500215 SK_WARN_UNUSED_RESULT
Brian Osman5aaaeea2020-06-22 14:26:03 -0400216 skvm::Color program(skvm::Builder*, skvm::Coord device, skvm::Coord local, skvm::Color paint,
Mike Kleine81b1082020-06-19 11:29:13 -0500217 const SkMatrixProvider&, const SkMatrix* localM,
Mike Kleina434e0f2020-03-23 09:33:48 -0500218 SkFilterQuality quality, const SkColorInfo& dst,
Mike Klein276a7852020-03-15 08:46:09 -0500219 skvm::Uniforms* uniforms, SkArenaAlloc* alloc) const;
Mike Kleine4522dc2019-10-29 12:10:34 -0500220
Florin Malita4aed1382017-05-25 10:38:07 -0400221protected:
Florin Malitaf7beee72017-05-26 12:54:32 -0400222 SkShaderBase(const SkMatrix* localMatrix = nullptr);
223
Florin Malita4aed1382017-05-25 10:38:07 -0400224 void flatten(SkWriteBuffer&) const override;
225
Mike Reede92aae62018-10-17 10:21:51 -0400226#ifdef SK_ENABLE_LEGACY_SHADERCONTEXT
Florin Malita4aed1382017-05-25 10:38:07 -0400227 /**
228 * Specialize creating a SkShader context using the supplied allocator.
229 * @return pointer to context owned by the arena allocator.
230 */
231 virtual Context* onMakeContext(const ContextRec&, SkArenaAlloc*) const {
232 return nullptr;
233 }
Mike Reede92aae62018-10-17 10:21:51 -0400234#endif
Florin Malita47e55a52017-06-06 12:26:54 -0400235
Florin Malita4aed1382017-05-25 10:38:07 -0400236 virtual bool onAsLuminanceColor(SkColor*) const {
237 return false;
238 }
239
Mike Reed6867eee2017-06-02 13:25:15 -0400240 // Default impl creates shadercontext and calls that (not very efficient)
Mike Reed1386b2d2019-03-13 21:15:05 -0400241 virtual bool onAppendStages(const SkStageRec&) const;
Florin Malita4aed1382017-05-25 10:38:07 -0400242
Mike Reed9318a6c2019-08-16 16:16:25 -0400243 virtual SkStageUpdater* onAppendUpdatableStages(const SkStageRec&) const { return nullptr; }
244
Mike Klein85754d52020-01-22 10:04:11 -0600245protected:
Mike Kleinb8219e42020-06-24 07:01:40 -0500246 static skvm::Coord ApplyMatrix(skvm::Builder*, const SkMatrix&, skvm::Coord, skvm::Uniforms*);
Mike Klein85754d52020-01-22 10:04:11 -0600247
Florin Malita4aed1382017-05-25 10:38:07 -0400248private:
249 // This is essentially const, but not officially so it can be modified in constructors.
250 SkMatrix fLocalMatrix;
251
Brian Osman5aaaeea2020-06-22 14:26:03 -0400252 virtual skvm::Color onProgram(skvm::Builder*,
253 skvm::Coord device, skvm::Coord local, skvm::Color paint,
Mike Kleine81b1082020-06-19 11:29:13 -0500254 const SkMatrixProvider&, const SkMatrix* localM,
Mike Kleina434e0f2020-03-23 09:33:48 -0500255 SkFilterQuality quality, const SkColorInfo& dst,
Mike Klein276a7852020-03-15 08:46:09 -0500256 skvm::Uniforms* uniforms, SkArenaAlloc* alloc) const;
Mike Klein8e717442020-01-07 10:22:33 -0600257
John Stiles7571f9e2020-09-02 22:42:33 -0400258 using INHERITED = SkShader;
Florin Malita4aed1382017-05-25 10:38:07 -0400259};
260
261inline SkShaderBase* as_SB(SkShader* shader) {
262 return static_cast<SkShaderBase*>(shader);
263}
264
265inline const SkShaderBase* as_SB(const SkShader* shader) {
266 return static_cast<const SkShaderBase*>(shader);
267}
268
269inline const SkShaderBase* as_SB(const sk_sp<SkShader>& shader) {
270 return static_cast<SkShaderBase*>(shader.get());
271}
272
273#endif // SkShaderBase_DEFINED