blob: 36e152f368d1711a5a0f1de3132e9ba4ec9459f1 [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"
Ben Wagnerd5148e32018-07-16 17:44:06 -040014#include "SkNoncopyable.h"
Florin Malita4aed1382017-05-25 10:38:07 -040015#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 {
Florin Malita4aed1382017-05-25 10:38:07 -040065 ContextRec(const SkPaint& paint, const SkMatrix& matrix, const SkMatrix* localM,
Brian Osman2e8f48e2018-10-19 13:50:48 -040066 SkColorType dstColorType, SkColorSpace* dstColorSpace)
Florin Malita4aed1382017-05-25 10:38:07 -040067 : fPaint(&paint)
68 , fMatrix(&matrix)
69 , fLocalMatrix(localM)
Brian Osman2e8f48e2018-10-19 13:50:48 -040070 , fDstColorType(dstColorType)
Florin Malita4aed1382017-05-25 10:38:07 -040071 , fDstColorSpace(dstColorSpace) {}
72
73 const SkPaint* fPaint; // the current paint associated with the draw
74 const SkMatrix* fMatrix; // the current matrix in the canvas
75 const SkMatrix* fLocalMatrix; // optional local matrix
Brian Osman2e8f48e2018-10-19 13:50:48 -040076 SkColorType fDstColorType; // the color type of the dest surface
Florin Malita4aed1382017-05-25 10:38:07 -040077 SkColorSpace* fDstColorSpace; // the color space of the dest surface (if any)
78 };
79
80 class Context : public ::SkNoncopyable {
81 public:
82 Context(const SkShaderBase& shader, const ContextRec&);
83
84 virtual ~Context();
85
86 /**
87 * Called sometimes before drawing with this shader. Return the type of
88 * alpha your shader will return. The default implementation returns 0.
89 * Your subclass should override if it can (even sometimes) report a
90 * non-zero value, since that will enable various blitters to perform
91 * faster.
92 */
93 virtual uint32_t getFlags() const { return 0; }
94
95 /**
96 * Called for each span of the object being drawn. Your subclass should
97 * set the appropriate colors (with premultiplied alpha) that correspond
98 * to the specified device coordinates.
99 */
100 virtual void shadeSpan(int x, int y, SkPMColor[], int count) = 0;
101
Florin Malita4aed1382017-05-25 10:38:07 -0400102 protected:
103 // Reference to shader, so we don't have to dupe information.
104 const SkShaderBase& fShader;
105
Florin Malita4aed1382017-05-25 10:38:07 -0400106 uint8_t getPaintAlpha() const { return fPaintAlpha; }
107 const SkMatrix& getTotalInverse() const { return fTotalInverse; }
Florin Malita4aed1382017-05-25 10:38:07 -0400108 const SkMatrix& getCTM() const { return fCTM; }
109
Florin Malita4aed1382017-05-25 10:38:07 -0400110 private:
111 SkMatrix fCTM;
112 SkMatrix fTotalInverse;
113 uint8_t fPaintAlpha;
Florin Malita4aed1382017-05-25 10:38:07 -0400114
115 typedef SkNoncopyable INHERITED;
116 };
117
118 /**
119 * Make a context using the memory provided by the arena.
120 *
121 * @return pointer to context or nullptr if can't be created
122 */
123 Context* makeContext(const ContextRec&, SkArenaAlloc*) const;
124
125#if SK_SUPPORT_GPU
Florin Malita4aed1382017-05-25 10:38:07 -0400126 /**
127 * Returns a GrFragmentProcessor that implements the shader for the GPU backend. NULL is
128 * returned if there is no GPU implementation.
129 *
130 * The GPU device does not call SkShader::createContext(), instead we pass the view matrix,
131 * local matrix, and filter quality directly.
132 *
133 * The GrContext may be used by the to create textures that are required by the returned
134 * processor.
135 *
136 * The returned GrFragmentProcessor should expect an unpremultiplied input color and
137 * produce a premultiplied output.
138 */
Mike Reede3429e62018-01-19 11:43:34 -0500139 virtual std::unique_ptr<GrFragmentProcessor> asFragmentProcessor(const GrFPArgs&) const;
Florin Malita4aed1382017-05-25 10:38:07 -0400140#endif
141
142 /**
143 * If the shader can represent its "average" luminance in a single color, return true and
144 * if color is not NULL, return that color. If it cannot, return false and ignore the color
145 * parameter.
146 *
147 * Note: if this returns true, the returned color will always be opaque, as only the RGB
148 * components are used to compute luminance.
149 */
150 bool asLuminanceColor(SkColor*) const;
151
152 /**
153 * Returns a shader transformed into a new color space via the |xformer|.
154 */
155 sk_sp<SkShader> makeColorSpace(SkColorSpaceXformer* xformer) const {
156 return this->onMakeColorSpace(xformer);
157 }
158
Mike Reed1d8c42e2017-08-29 14:58:19 -0400159 struct StageRec {
160 SkRasterPipeline* fPipeline;
161 SkArenaAlloc* fAlloc;
Brian Osman2e8f48e2018-10-19 13:50:48 -0400162 SkColorType fDstColorType;
Mike Reed1d8c42e2017-08-29 14:58:19 -0400163 SkColorSpace* fDstCS; // may be nullptr
164 const SkPaint& fPaint;
165 const SkMatrix* fLocalM; // may be nullptr
166 SkMatrix fCTM;
167 };
168
Mike Reed6867eee2017-06-02 13:25:15 -0400169 // If this returns false, then we draw nothing (do not fall back to shader context)
Mike Reed1d8c42e2017-08-29 14:58:19 -0400170 bool appendStages(const StageRec&) const;
Florin Malita4aed1382017-05-25 10:38:07 -0400171
Florin Malitac6c5ead2018-04-11 15:33:40 -0400172 bool SK_WARN_UNUSED_RESULT computeTotalInverse(const SkMatrix& ctm,
173 const SkMatrix* outerLocalMatrix,
174 SkMatrix* totalInverse) const;
175
176 // Returns the total local matrix for this shader:
177 //
178 // M = postLocalMatrix x shaderLocalMatrix x preLocalMatrix
179 //
180 SkTCopyOnFirstWrite<SkMatrix> totalLocalMatrix(const SkMatrix* preLocalMatrix,
181 const SkMatrix* postLocalMatrix = nullptr) const;
Florin Malita4aed1382017-05-25 10:38:07 -0400182
Florin Malita4aed1382017-05-25 10:38:07 -0400183 virtual SkImage* onIsAImage(SkMatrix*, TileMode[2]) const {
184 return nullptr;
185 }
186
Mike Klein12956722018-10-19 10:00:21 -0400187 static Type GetFlattenableType() { return kSkShaderBase_Type; }
188 Type getFlattenableType() const override { return GetFlattenableType(); }
189
190 static sk_sp<SkShaderBase> Deserialize(const void* data, size_t size,
191 const SkDeserialProcs* procs = nullptr) {
192 return sk_sp<SkShaderBase>(static_cast<SkShaderBase*>(
193 SkFlattenable::Deserialize(GetFlattenableType(), data, size, procs).release()));
194 }
Mike Kleinfa5f6ce2018-10-20 08:21:31 -0400195 static void RegisterFlattenables();
Florin Malita4aed1382017-05-25 10:38:07 -0400196
197protected:
Florin Malitaf7beee72017-05-26 12:54:32 -0400198 SkShaderBase(const SkMatrix* localMatrix = nullptr);
199
Florin Malita4aed1382017-05-25 10:38:07 -0400200 void flatten(SkWriteBuffer&) const override;
201
Mike Reede92aae62018-10-17 10:21:51 -0400202#ifdef SK_ENABLE_LEGACY_SHADERCONTEXT
Florin Malita4aed1382017-05-25 10:38:07 -0400203 /**
204 * Specialize creating a SkShader context using the supplied allocator.
205 * @return pointer to context owned by the arena allocator.
206 */
207 virtual Context* onMakeContext(const ContextRec&, SkArenaAlloc*) const {
208 return nullptr;
209 }
210
Florin Malita47e55a52017-06-06 12:26:54 -0400211 /**
212 * Overriden by shaders which prefer burst mode.
213 */
214 virtual Context* onMakeBurstPipelineContext(const ContextRec&, SkArenaAlloc*) const {
215 return nullptr;
216 }
Mike Reede92aae62018-10-17 10:21:51 -0400217#endif
Florin Malita47e55a52017-06-06 12:26:54 -0400218
Florin Malita4aed1382017-05-25 10:38:07 -0400219 virtual bool onAsLuminanceColor(SkColor*) const {
220 return false;
221 }
222
223 virtual sk_sp<SkShader> onMakeColorSpace(SkColorSpaceXformer*) const {
224 return sk_ref_sp(const_cast<SkShaderBase*>(this));
225 }
226
Mike Reed6867eee2017-06-02 13:25:15 -0400227 // Default impl creates shadercontext and calls that (not very efficient)
Mike Reed1d8c42e2017-08-29 14:58:19 -0400228 virtual bool onAppendStages(const StageRec&) const;
Florin Malita4aed1382017-05-25 10:38:07 -0400229
Florin Malita4aed1382017-05-25 10:38:07 -0400230private:
231 // This is essentially const, but not officially so it can be modified in constructors.
232 SkMatrix fLocalMatrix;
233
234 typedef SkShader INHERITED;
235};
236
237inline SkShaderBase* as_SB(SkShader* shader) {
238 return static_cast<SkShaderBase*>(shader);
239}
240
241inline const SkShaderBase* as_SB(const SkShader* shader) {
242 return static_cast<const SkShaderBase*>(shader);
243}
244
245inline const SkShaderBase* as_SB(const sk_sp<SkShader>& shader) {
246 return static_cast<SkShaderBase*>(shader.get());
247}
248
249#endif // SkShaderBase_DEFINED