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 | |
| 11 | #include "SkFilterQuality.h" |
Cary Clark | 4dc5a45 | 2018-05-21 11:56:57 -0400 | [diff] [blame] | 12 | #include "SkFlattenablePriv.h" |
Mike Reed | 75ae421 | 2018-01-23 11:24:08 -0500 | [diff] [blame] | 13 | #include "SkMask.h" |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 14 | #include "SkMatrix.h" |
| 15 | #include "SkShader.h" |
Florin Malita | c6c5ead | 2018-04-11 15:33:40 -0400 | [diff] [blame] | 16 | #include "SkTLazy.h" |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 17 | |
Mike Reed | e3429e6 | 2018-01-19 11:43:34 -0500 | [diff] [blame] | 18 | #if SK_SUPPORT_GPU |
| 19 | #include "GrFPArgs.h" |
| 20 | #endif |
| 21 | |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 22 | class GrContext; |
Brian Salomon | 4cbb6e6 | 2017-10-25 15:12:19 -0400 | [diff] [blame] | 23 | class GrColorSpaceInfo; |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 24 | class GrFragmentProcessor; |
| 25 | class SkArenaAlloc; |
| 26 | class SkColorSpace; |
| 27 | class SkColorSpaceXformer; |
| 28 | class SkImage; |
| 29 | struct SkImageInfo; |
| 30 | class SkPaint; |
| 31 | class SkRasterPipeline; |
| 32 | |
Florin Malita | 95c993c | 2017-05-26 09:44:10 -0400 | [diff] [blame] | 33 | class SkShaderBase : public SkShader { |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 34 | public: |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 35 | ~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 { |
| 65 | enum DstType { |
| 66 | kPMColor_DstType, // clients prefer shading into PMColor dest |
| 67 | kPM4f_DstType, // clients prefer shading into PM4f dest |
| 68 | }; |
| 69 | |
| 70 | ContextRec(const SkPaint& paint, const SkMatrix& matrix, const SkMatrix* localM, |
| 71 | DstType dstType, SkColorSpace* dstColorSpace) |
| 72 | : fPaint(&paint) |
| 73 | , fMatrix(&matrix) |
| 74 | , fLocalMatrix(localM) |
| 75 | , fPreferredDstType(dstType) |
| 76 | , fDstColorSpace(dstColorSpace) {} |
| 77 | |
| 78 | const SkPaint* fPaint; // the current paint associated with the draw |
| 79 | const SkMatrix* fMatrix; // the current matrix in the canvas |
| 80 | const SkMatrix* fLocalMatrix; // optional local matrix |
| 81 | const DstType fPreferredDstType; // the "natural" client dest type |
| 82 | SkColorSpace* fDstColorSpace; // the color space of the dest surface (if any) |
| 83 | }; |
| 84 | |
| 85 | class Context : public ::SkNoncopyable { |
| 86 | public: |
| 87 | Context(const SkShaderBase& shader, const ContextRec&); |
| 88 | |
| 89 | virtual ~Context(); |
| 90 | |
| 91 | /** |
| 92 | * Called sometimes before drawing with this shader. Return the type of |
| 93 | * alpha your shader will return. The default implementation returns 0. |
| 94 | * Your subclass should override if it can (even sometimes) report a |
| 95 | * non-zero value, since that will enable various blitters to perform |
| 96 | * faster. |
| 97 | */ |
| 98 | virtual uint32_t getFlags() const { return 0; } |
| 99 | |
| 100 | /** |
| 101 | * Called for each span of the object being drawn. Your subclass should |
| 102 | * set the appropriate colors (with premultiplied alpha) that correspond |
| 103 | * to the specified device coordinates. |
| 104 | */ |
| 105 | virtual void shadeSpan(int x, int y, SkPMColor[], int count) = 0; |
| 106 | |
| 107 | virtual void shadeSpan4f(int x, int y, SkPM4f[], int count); |
| 108 | |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 109 | // Notification from blitter::blitMask in case we need to see the non-alpha channels |
| 110 | virtual void set3DMask(const SkMask*) {} |
| 111 | |
| 112 | protected: |
| 113 | // Reference to shader, so we don't have to dupe information. |
| 114 | const SkShaderBase& fShader; |
| 115 | |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 116 | uint8_t getPaintAlpha() const { return fPaintAlpha; } |
| 117 | const SkMatrix& getTotalInverse() const { return fTotalInverse; } |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 118 | const SkMatrix& getCTM() const { return fCTM; } |
| 119 | |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 120 | private: |
| 121 | SkMatrix fCTM; |
| 122 | SkMatrix fTotalInverse; |
| 123 | uint8_t fPaintAlpha; |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 124 | |
| 125 | typedef SkNoncopyable INHERITED; |
| 126 | }; |
| 127 | |
| 128 | /** |
| 129 | * Make a context using the memory provided by the arena. |
| 130 | * |
| 131 | * @return pointer to context or nullptr if can't be created |
| 132 | */ |
| 133 | Context* makeContext(const ContextRec&, SkArenaAlloc*) const; |
| 134 | |
Florin Malita | 47e55a5 | 2017-06-06 12:26:54 -0400 | [diff] [blame] | 135 | /** |
| 136 | * Shaders may opt-in for burst mode, if they can operate |
| 137 | * significantly more efficiently in that mode. |
| 138 | * |
| 139 | * Burst mode is prioritized in SkRasterPipelineBlitter over |
| 140 | * regular (appendStages) pipeline operation. |
| 141 | */ |
| 142 | Context* makeBurstPipelineContext(const ContextRec&, SkArenaAlloc*) const; |
| 143 | |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 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 | |
| 171 | /** |
| 172 | * Returns a shader transformed into a new color space via the |xformer|. |
| 173 | */ |
| 174 | sk_sp<SkShader> makeColorSpace(SkColorSpaceXformer* xformer) const { |
| 175 | return this->onMakeColorSpace(xformer); |
| 176 | } |
| 177 | |
Mike Reed | 1d8c42e | 2017-08-29 14:58:19 -0400 | [diff] [blame] | 178 | struct StageRec { |
| 179 | SkRasterPipeline* fPipeline; |
| 180 | SkArenaAlloc* fAlloc; |
| 181 | SkColorSpace* fDstCS; // may be nullptr |
| 182 | const SkPaint& fPaint; |
| 183 | const SkMatrix* fLocalM; // may be nullptr |
| 184 | SkMatrix fCTM; |
| 185 | }; |
| 186 | |
Mike Reed | 6867eee | 2017-06-02 13:25:15 -0400 | [diff] [blame] | 187 | // If this returns false, then we draw nothing (do not fall back to shader context) |
Mike Reed | 1d8c42e | 2017-08-29 14:58:19 -0400 | [diff] [blame] | 188 | bool appendStages(const StageRec&) const; |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 189 | |
Florin Malita | c6c5ead | 2018-04-11 15:33:40 -0400 | [diff] [blame] | 190 | bool SK_WARN_UNUSED_RESULT computeTotalInverse(const SkMatrix& ctm, |
| 191 | const SkMatrix* outerLocalMatrix, |
| 192 | SkMatrix* totalInverse) const; |
| 193 | |
| 194 | // Returns the total local matrix for this shader: |
| 195 | // |
| 196 | // M = postLocalMatrix x shaderLocalMatrix x preLocalMatrix |
| 197 | // |
| 198 | SkTCopyOnFirstWrite<SkMatrix> totalLocalMatrix(const SkMatrix* preLocalMatrix, |
| 199 | const SkMatrix* postLocalMatrix = nullptr) const; |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 200 | |
| 201 | #ifdef SK_SUPPORT_LEGACY_SHADER_ISABITMAP |
| 202 | virtual bool onIsABitmap(SkBitmap*, SkMatrix*, TileMode[2]) const { |
| 203 | return false; |
| 204 | } |
| 205 | #endif |
| 206 | |
| 207 | virtual SkImage* onIsAImage(SkMatrix*, TileMode[2]) const { |
| 208 | return nullptr; |
| 209 | } |
| 210 | |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 211 | SK_DEFINE_FLATTENABLE_TYPE(SkShaderBase) |
| 212 | SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP() |
| 213 | |
| 214 | protected: |
Florin Malita | f7beee7 | 2017-05-26 12:54:32 -0400 | [diff] [blame] | 215 | SkShaderBase(const SkMatrix* localMatrix = nullptr); |
| 216 | |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 217 | void flatten(SkWriteBuffer&) const override; |
| 218 | |
| 219 | /** |
| 220 | * Specialize creating a SkShader context using the supplied allocator. |
| 221 | * @return pointer to context owned by the arena allocator. |
| 222 | */ |
| 223 | virtual Context* onMakeContext(const ContextRec&, SkArenaAlloc*) const { |
| 224 | return nullptr; |
| 225 | } |
| 226 | |
Florin Malita | 47e55a5 | 2017-06-06 12:26:54 -0400 | [diff] [blame] | 227 | /** |
| 228 | * Overriden by shaders which prefer burst mode. |
| 229 | */ |
| 230 | virtual Context* onMakeBurstPipelineContext(const ContextRec&, SkArenaAlloc*) const { |
| 231 | return nullptr; |
| 232 | } |
| 233 | |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 234 | virtual bool onAsLuminanceColor(SkColor*) const { |
| 235 | return false; |
| 236 | } |
| 237 | |
| 238 | virtual sk_sp<SkShader> onMakeColorSpace(SkColorSpaceXformer*) const { |
| 239 | return sk_ref_sp(const_cast<SkShaderBase*>(this)); |
| 240 | } |
| 241 | |
Mike Reed | 6867eee | 2017-06-02 13:25:15 -0400 | [diff] [blame] | 242 | // Default impl creates shadercontext and calls that (not very efficient) |
Mike Reed | 1d8c42e | 2017-08-29 14:58:19 -0400 | [diff] [blame] | 243 | virtual bool onAppendStages(const StageRec&) const; |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 244 | |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 245 | private: |
| 246 | // This is essentially const, but not officially so it can be modified in constructors. |
| 247 | SkMatrix fLocalMatrix; |
| 248 | |
| 249 | typedef SkShader INHERITED; |
| 250 | }; |
| 251 | |
| 252 | inline SkShaderBase* as_SB(SkShader* shader) { |
| 253 | return static_cast<SkShaderBase*>(shader); |
| 254 | } |
| 255 | |
| 256 | inline const SkShaderBase* as_SB(const SkShader* shader) { |
| 257 | return static_cast<const SkShaderBase*>(shader); |
| 258 | } |
| 259 | |
| 260 | inline const SkShaderBase* as_SB(const sk_sp<SkShader>& shader) { |
| 261 | return static_cast<SkShaderBase*>(shader.get()); |
| 262 | } |
| 263 | |
| 264 | #endif // SkShaderBase_DEFINED |