Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 1 | /* |
| 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #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 Klein | e4522dc | 2019-10-29 12:10:34 -0500 | [diff] [blame] | 18 | #include "src/core/SkVM.h" |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 19 | |
Mike Reed | e3429e6 | 2018-01-19 11:43:34 -0500 | [diff] [blame] | 20 | #if SK_SUPPORT_GPU |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 21 | #include "src/gpu/GrFPArgs.h" |
Mike Reed | e3429e6 | 2018-01-19 11:43:34 -0500 | [diff] [blame] | 22 | #endif |
| 23 | |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 24 | class GrContext; |
| 25 | class GrFragmentProcessor; |
| 26 | class SkArenaAlloc; |
| 27 | class SkColorSpace; |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 28 | class SkImage; |
| 29 | struct SkImageInfo; |
| 30 | class SkPaint; |
| 31 | class SkRasterPipeline; |
| 32 | |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 33 | /** |
| 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 | */ |
| 43 | class SkStageUpdater { |
| 44 | public: |
| 45 | virtual ~SkStageUpdater() {} |
| 46 | |
| 47 | virtual bool update(const SkMatrix& ctm, const SkMatrix* localM) = 0; |
| 48 | }; |
| 49 | |
Florin Malita | 95c993c | 2017-05-26 09:44:10 -0400 | [diff] [blame] | 50 | class SkShaderBase : public SkShader { |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 51 | public: |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 52 | ~SkShaderBase() override; |
| 53 | |
| 54 | /** |
| 55 | * Returns true if the shader is guaranteed to produce only a single color. |
| 56 | * Subclasses can override this to allow loop-hoisting optimization. |
| 57 | */ |
| 58 | virtual bool isConstant() const { return false; } |
| 59 | |
| 60 | const SkMatrix& getLocalMatrix() const { return fLocalMatrix; } |
| 61 | |
| 62 | enum Flags { |
| 63 | //!< set if all of the colors will be opaque |
| 64 | kOpaqueAlpha_Flag = 1 << 0, |
| 65 | |
| 66 | /** set if the spans only vary in X (const in Y). |
| 67 | e.g. an Nx1 bitmap that is being tiled in Y, or a linear-gradient |
| 68 | that varies from left-to-right. This flag specifies this for |
| 69 | shadeSpan(). |
| 70 | */ |
| 71 | kConstInY32_Flag = 1 << 1, |
| 72 | |
| 73 | /** hint for the blitter that 4f is the preferred shading mode. |
| 74 | */ |
| 75 | kPrefers4f_Flag = 1 << 2, |
| 76 | }; |
| 77 | |
| 78 | /** |
| 79 | * ContextRec acts as a parameter bundle for creating Contexts. |
| 80 | */ |
| 81 | struct ContextRec { |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 82 | ContextRec(const SkPaint& paint, const SkMatrix& matrix, const SkMatrix* localM, |
Brian Osman | 2e8f48e | 2018-10-19 13:50:48 -0400 | [diff] [blame] | 83 | SkColorType dstColorType, SkColorSpace* dstColorSpace) |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 84 | : fPaint(&paint) |
| 85 | , fMatrix(&matrix) |
| 86 | , fLocalMatrix(localM) |
Brian Osman | 2e8f48e | 2018-10-19 13:50:48 -0400 | [diff] [blame] | 87 | , fDstColorType(dstColorType) |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 88 | , fDstColorSpace(dstColorSpace) {} |
| 89 | |
| 90 | const SkPaint* fPaint; // the current paint associated with the draw |
| 91 | const SkMatrix* fMatrix; // the current matrix in the canvas |
| 92 | const SkMatrix* fLocalMatrix; // optional local matrix |
Brian Osman | 2e8f48e | 2018-10-19 13:50:48 -0400 | [diff] [blame] | 93 | SkColorType fDstColorType; // the color type of the dest surface |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 94 | SkColorSpace* fDstColorSpace; // the color space of the dest surface (if any) |
Mike Reed | 011d166 | 2019-02-28 17:19:25 -0500 | [diff] [blame] | 95 | |
| 96 | bool isLegacyCompatible(SkColorSpace* shadersColorSpace) const; |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | class Context : public ::SkNoncopyable { |
| 100 | public: |
| 101 | Context(const SkShaderBase& shader, const ContextRec&); |
| 102 | |
| 103 | virtual ~Context(); |
| 104 | |
| 105 | /** |
| 106 | * Called sometimes before drawing with this shader. Return the type of |
| 107 | * alpha your shader will return. The default implementation returns 0. |
| 108 | * Your subclass should override if it can (even sometimes) report a |
| 109 | * non-zero value, since that will enable various blitters to perform |
| 110 | * faster. |
| 111 | */ |
| 112 | virtual uint32_t getFlags() const { return 0; } |
| 113 | |
| 114 | /** |
| 115 | * Called for each span of the object being drawn. Your subclass should |
| 116 | * set the appropriate colors (with premultiplied alpha) that correspond |
| 117 | * to the specified device coordinates. |
| 118 | */ |
| 119 | virtual void shadeSpan(int x, int y, SkPMColor[], int count) = 0; |
| 120 | |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 121 | protected: |
| 122 | // Reference to shader, so we don't have to dupe information. |
| 123 | const SkShaderBase& fShader; |
| 124 | |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 125 | uint8_t getPaintAlpha() const { return fPaintAlpha; } |
| 126 | const SkMatrix& getTotalInverse() const { return fTotalInverse; } |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 127 | const SkMatrix& getCTM() const { return fCTM; } |
| 128 | |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 129 | private: |
| 130 | SkMatrix fCTM; |
| 131 | SkMatrix fTotalInverse; |
| 132 | uint8_t fPaintAlpha; |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 133 | |
| 134 | typedef SkNoncopyable INHERITED; |
| 135 | }; |
| 136 | |
| 137 | /** |
| 138 | * Make a context using the memory provided by the arena. |
| 139 | * |
| 140 | * @return pointer to context or nullptr if can't be created |
| 141 | */ |
| 142 | Context* makeContext(const ContextRec&, SkArenaAlloc*) const; |
| 143 | |
| 144 | #if SK_SUPPORT_GPU |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 145 | /** |
| 146 | * Returns a GrFragmentProcessor that implements the shader for the GPU backend. NULL is |
| 147 | * returned if there is no GPU implementation. |
| 148 | * |
| 149 | * The GPU device does not call SkShader::createContext(), instead we pass the view matrix, |
| 150 | * local matrix, and filter quality directly. |
| 151 | * |
| 152 | * The GrContext may be used by the to create textures that are required by the returned |
| 153 | * processor. |
| 154 | * |
| 155 | * The returned GrFragmentProcessor should expect an unpremultiplied input color and |
| 156 | * produce a premultiplied output. |
| 157 | */ |
Mike Reed | e3429e6 | 2018-01-19 11:43:34 -0500 | [diff] [blame] | 158 | virtual std::unique_ptr<GrFragmentProcessor> asFragmentProcessor(const GrFPArgs&) const; |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 159 | #endif |
| 160 | |
| 161 | /** |
| 162 | * If the shader can represent its "average" luminance in a single color, return true and |
| 163 | * if color is not NULL, return that color. If it cannot, return false and ignore the color |
| 164 | * parameter. |
| 165 | * |
| 166 | * Note: if this returns true, the returned color will always be opaque, as only the RGB |
| 167 | * components are used to compute luminance. |
| 168 | */ |
| 169 | bool asLuminanceColor(SkColor*) const; |
| 170 | |
Mike Reed | 6867eee | 2017-06-02 13:25:15 -0400 | [diff] [blame] | 171 | // If this returns false, then we draw nothing (do not fall back to shader context) |
Mike Reed | 1386b2d | 2019-03-13 21:15:05 -0400 | [diff] [blame] | 172 | bool appendStages(const SkStageRec&) const; |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 173 | |
Florin Malita | c6c5ead | 2018-04-11 15:33:40 -0400 | [diff] [blame] | 174 | bool SK_WARN_UNUSED_RESULT computeTotalInverse(const SkMatrix& ctm, |
| 175 | const SkMatrix* outerLocalMatrix, |
| 176 | SkMatrix* totalInverse) const; |
| 177 | |
| 178 | // Returns the total local matrix for this shader: |
| 179 | // |
| 180 | // M = postLocalMatrix x shaderLocalMatrix x preLocalMatrix |
| 181 | // |
| 182 | SkTCopyOnFirstWrite<SkMatrix> totalLocalMatrix(const SkMatrix* preLocalMatrix, |
| 183 | const SkMatrix* postLocalMatrix = nullptr) const; |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 184 | |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 185 | virtual SkImage* onIsAImage(SkMatrix*, SkTileMode[2]) const { |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 186 | return nullptr; |
| 187 | } |
Hal Canary | 8a64fa6 | 2019-05-30 10:29:40 -0400 | [diff] [blame] | 188 | virtual SkPicture* isAPicture(SkMatrix*, SkTileMode[2], SkRect* tile) const { return nullptr; } |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 189 | |
Mike Klein | 1295672 | 2018-10-19 10:00:21 -0400 | [diff] [blame] | 190 | static Type GetFlattenableType() { return kSkShaderBase_Type; } |
| 191 | Type getFlattenableType() const override { return GetFlattenableType(); } |
| 192 | |
| 193 | static sk_sp<SkShaderBase> Deserialize(const void* data, size_t size, |
| 194 | const SkDeserialProcs* procs = nullptr) { |
| 195 | return sk_sp<SkShaderBase>(static_cast<SkShaderBase*>( |
| 196 | SkFlattenable::Deserialize(GetFlattenableType(), data, size, procs).release())); |
| 197 | } |
Mike Klein | fa5f6ce | 2018-10-20 08:21:31 -0400 | [diff] [blame] | 198 | static void RegisterFlattenables(); |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 199 | |
Mike Reed | 7656b2c | 2019-04-08 11:48:20 -0400 | [diff] [blame] | 200 | /** DEPRECATED. skbug.com/8941 |
| 201 | * If this shader can be represented by another shader + a localMatrix, return that shader and |
| 202 | * the localMatrix. If not, return nullptr and ignore the localMatrix parameter. |
| 203 | */ |
| 204 | virtual sk_sp<SkShader> makeAsALocalMatrixShader(SkMatrix* localMatrix) const; |
| 205 | |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 206 | SkStageUpdater* appendUpdatableStages(const SkStageRec& rec) const { |
| 207 | return this->onAppendUpdatableStages(rec); |
| 208 | } |
| 209 | |
Mike Klein | 0393505 | 2019-11-01 11:36:55 -0500 | [diff] [blame] | 210 | bool program(skvm::Builder*, |
| 211 | SkColorSpace* dstCS, |
Mike Klein | 1cc6067 | 2019-11-05 14:19:58 -0600 | [diff] [blame] | 212 | skvm::Uniforms* uniforms, |
Mike Klein | f3d4109 | 2019-11-04 13:22:02 -0600 | [diff] [blame] | 213 | skvm::F32 x, skvm::F32 y, |
Mike Klein | b9f2088 | 2019-11-08 12:14:15 -0600 | [diff] [blame] | 214 | skvm::F32* r, skvm::F32* g, skvm::F32* b, skvm::F32* a) const; |
Mike Klein | e4522dc | 2019-10-29 12:10:34 -0500 | [diff] [blame] | 215 | |
Mike Klein | 0393505 | 2019-11-01 11:36:55 -0500 | [diff] [blame] | 216 | virtual bool onProgram(skvm::Builder*, |
| 217 | SkColorSpace* dstCS, |
Mike Klein | 1cc6067 | 2019-11-05 14:19:58 -0600 | [diff] [blame] | 218 | skvm::Uniforms* uniforms, |
Mike Klein | f3d4109 | 2019-11-04 13:22:02 -0600 | [diff] [blame] | 219 | skvm::F32 x, skvm::F32 y, |
Mike Klein | b9f2088 | 2019-11-08 12:14:15 -0600 | [diff] [blame] | 220 | skvm::F32* r, skvm::F32* g, skvm::F32* b, skvm::F32* a) const { |
Mike Klein | e4522dc | 2019-10-29 12:10:34 -0500 | [diff] [blame] | 221 | return false; |
| 222 | } |
| 223 | |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 224 | protected: |
Florin Malita | f7beee7 | 2017-05-26 12:54:32 -0400 | [diff] [blame] | 225 | SkShaderBase(const SkMatrix* localMatrix = nullptr); |
| 226 | |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 227 | void flatten(SkWriteBuffer&) const override; |
| 228 | |
Mike Reed | e92aae6 | 2018-10-17 10:21:51 -0400 | [diff] [blame] | 229 | #ifdef SK_ENABLE_LEGACY_SHADERCONTEXT |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 230 | /** |
| 231 | * Specialize creating a SkShader context using the supplied allocator. |
| 232 | * @return pointer to context owned by the arena allocator. |
| 233 | */ |
| 234 | virtual Context* onMakeContext(const ContextRec&, SkArenaAlloc*) const { |
| 235 | return nullptr; |
| 236 | } |
Mike Reed | e92aae6 | 2018-10-17 10:21:51 -0400 | [diff] [blame] | 237 | #endif |
Florin Malita | 47e55a5 | 2017-06-06 12:26:54 -0400 | [diff] [blame] | 238 | |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 239 | virtual bool onAsLuminanceColor(SkColor*) const { |
| 240 | return false; |
| 241 | } |
| 242 | |
Mike Reed | 6867eee | 2017-06-02 13:25:15 -0400 | [diff] [blame] | 243 | // Default impl creates shadercontext and calls that (not very efficient) |
Mike Reed | 1386b2d | 2019-03-13 21:15:05 -0400 | [diff] [blame] | 244 | virtual bool onAppendStages(const SkStageRec&) const; |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 245 | |
Mike Reed | 9318a6c | 2019-08-16 16:16:25 -0400 | [diff] [blame] | 246 | virtual SkStageUpdater* onAppendUpdatableStages(const SkStageRec&) const { return nullptr; } |
| 247 | |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 248 | private: |
| 249 | // This is essentially const, but not officially so it can be modified in constructors. |
| 250 | SkMatrix fLocalMatrix; |
| 251 | |
| 252 | typedef SkShader INHERITED; |
| 253 | }; |
| 254 | |
| 255 | inline SkShaderBase* as_SB(SkShader* shader) { |
| 256 | return static_cast<SkShaderBase*>(shader); |
| 257 | } |
| 258 | |
| 259 | inline const SkShaderBase* as_SB(const SkShader* shader) { |
| 260 | return static_cast<const SkShaderBase*>(shader); |
| 261 | } |
| 262 | |
| 263 | inline const SkShaderBase* as_SB(const sk_sp<SkShader>& shader) { |
| 264 | return static_cast<SkShaderBase*>(shader.get()); |
| 265 | } |
| 266 | |
| 267 | #endif // SkShaderBase_DEFINED |