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" |
| 12 | #include "SkMatrix.h" |
| 13 | #include "SkShader.h" |
| 14 | |
| 15 | class GrContext; |
| 16 | class GrFragmentProcessor; |
| 17 | class SkArenaAlloc; |
| 18 | class SkColorSpace; |
| 19 | class SkColorSpaceXformer; |
| 20 | class SkImage; |
| 21 | struct SkImageInfo; |
| 22 | class SkPaint; |
| 23 | class SkRasterPipeline; |
| 24 | |
Florin Malita | 95c993c | 2017-05-26 09:44:10 -0400 | [diff] [blame] | 25 | class SkShaderBase : public SkShader { |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 26 | public: |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 27 | ~SkShaderBase() override; |
| 28 | |
| 29 | /** |
| 30 | * Returns true if the shader is guaranteed to produce only a single color. |
| 31 | * Subclasses can override this to allow loop-hoisting optimization. |
| 32 | */ |
| 33 | virtual bool isConstant() const { return false; } |
| 34 | |
| 35 | const SkMatrix& getLocalMatrix() const { return fLocalMatrix; } |
| 36 | |
| 37 | enum Flags { |
| 38 | //!< set if all of the colors will be opaque |
| 39 | kOpaqueAlpha_Flag = 1 << 0, |
| 40 | |
| 41 | /** set if the spans only vary in X (const in Y). |
| 42 | e.g. an Nx1 bitmap that is being tiled in Y, or a linear-gradient |
| 43 | that varies from left-to-right. This flag specifies this for |
| 44 | shadeSpan(). |
| 45 | */ |
| 46 | kConstInY32_Flag = 1 << 1, |
| 47 | |
| 48 | /** hint for the blitter that 4f is the preferred shading mode. |
| 49 | */ |
| 50 | kPrefers4f_Flag = 1 << 2, |
| 51 | }; |
| 52 | |
| 53 | /** |
| 54 | * ContextRec acts as a parameter bundle for creating Contexts. |
| 55 | */ |
| 56 | struct ContextRec { |
| 57 | enum DstType { |
| 58 | kPMColor_DstType, // clients prefer shading into PMColor dest |
| 59 | kPM4f_DstType, // clients prefer shading into PM4f dest |
| 60 | }; |
| 61 | |
| 62 | ContextRec(const SkPaint& paint, const SkMatrix& matrix, const SkMatrix* localM, |
| 63 | DstType dstType, SkColorSpace* dstColorSpace) |
| 64 | : fPaint(&paint) |
| 65 | , fMatrix(&matrix) |
| 66 | , fLocalMatrix(localM) |
| 67 | , fPreferredDstType(dstType) |
| 68 | , fDstColorSpace(dstColorSpace) {} |
| 69 | |
| 70 | const SkPaint* fPaint; // the current paint associated with the draw |
| 71 | const SkMatrix* fMatrix; // the current matrix in the canvas |
| 72 | const SkMatrix* fLocalMatrix; // optional local matrix |
| 73 | const DstType fPreferredDstType; // the "natural" client dest type |
| 74 | SkColorSpace* fDstColorSpace; // the color space of the dest surface (if any) |
| 75 | }; |
| 76 | |
| 77 | class Context : public ::SkNoncopyable { |
| 78 | public: |
| 79 | Context(const SkShaderBase& shader, const ContextRec&); |
| 80 | |
| 81 | virtual ~Context(); |
| 82 | |
| 83 | /** |
| 84 | * Called sometimes before drawing with this shader. Return the type of |
| 85 | * alpha your shader will return. The default implementation returns 0. |
| 86 | * Your subclass should override if it can (even sometimes) report a |
| 87 | * non-zero value, since that will enable various blitters to perform |
| 88 | * faster. |
| 89 | */ |
| 90 | virtual uint32_t getFlags() const { return 0; } |
| 91 | |
| 92 | /** |
| 93 | * Called for each span of the object being drawn. Your subclass should |
| 94 | * set the appropriate colors (with premultiplied alpha) that correspond |
| 95 | * to the specified device coordinates. |
| 96 | */ |
| 97 | virtual void shadeSpan(int x, int y, SkPMColor[], int count) = 0; |
| 98 | |
| 99 | virtual void shadeSpan4f(int x, int y, SkPM4f[], int count); |
| 100 | |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 101 | // Notification from blitter::blitMask in case we need to see the non-alpha channels |
| 102 | virtual void set3DMask(const SkMask*) {} |
| 103 | |
| 104 | protected: |
| 105 | // Reference to shader, so we don't have to dupe information. |
| 106 | const SkShaderBase& fShader; |
| 107 | |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 108 | uint8_t getPaintAlpha() const { return fPaintAlpha; } |
| 109 | const SkMatrix& getTotalInverse() const { return fTotalInverse; } |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 110 | const SkMatrix& getCTM() const { return fCTM; } |
| 111 | |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 112 | private: |
| 113 | SkMatrix fCTM; |
| 114 | SkMatrix fTotalInverse; |
| 115 | uint8_t fPaintAlpha; |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 116 | |
| 117 | typedef SkNoncopyable INHERITED; |
| 118 | }; |
| 119 | |
| 120 | /** |
| 121 | * Make a context using the memory provided by the arena. |
| 122 | * |
| 123 | * @return pointer to context or nullptr if can't be created |
| 124 | */ |
| 125 | Context* makeContext(const ContextRec&, SkArenaAlloc*) const; |
| 126 | |
Florin Malita | 47e55a5 | 2017-06-06 12:26:54 -0400 | [diff] [blame] | 127 | /** |
| 128 | * Shaders may opt-in for burst mode, if they can operate |
| 129 | * significantly more efficiently in that mode. |
| 130 | * |
| 131 | * Burst mode is prioritized in SkRasterPipelineBlitter over |
| 132 | * regular (appendStages) pipeline operation. |
| 133 | */ |
| 134 | Context* makeBurstPipelineContext(const ContextRec&, SkArenaAlloc*) const; |
| 135 | |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 136 | #if SK_SUPPORT_GPU |
| 137 | struct AsFPArgs { |
| 138 | AsFPArgs() {} |
| 139 | AsFPArgs(GrContext* context, |
| 140 | const SkMatrix* viewMatrix, |
| 141 | const SkMatrix* localMatrix, |
| 142 | SkFilterQuality filterQuality, |
| 143 | SkColorSpace* dstColorSpace) |
| 144 | : fContext(context) |
| 145 | , fViewMatrix(viewMatrix) |
| 146 | , fLocalMatrix(localMatrix) |
| 147 | , fFilterQuality(filterQuality) |
| 148 | , fDstColorSpace(dstColorSpace) {} |
| 149 | |
| 150 | GrContext* fContext; |
| 151 | const SkMatrix* fViewMatrix; |
| 152 | const SkMatrix* fLocalMatrix; |
| 153 | SkFilterQuality fFilterQuality; |
| 154 | SkColorSpace* fDstColorSpace; |
| 155 | }; |
| 156 | |
| 157 | /** |
| 158 | * Returns a GrFragmentProcessor that implements the shader for the GPU backend. NULL is |
| 159 | * returned if there is no GPU implementation. |
| 160 | * |
| 161 | * The GPU device does not call SkShader::createContext(), instead we pass the view matrix, |
| 162 | * local matrix, and filter quality directly. |
| 163 | * |
| 164 | * The GrContext may be used by the to create textures that are required by the returned |
| 165 | * processor. |
| 166 | * |
| 167 | * The returned GrFragmentProcessor should expect an unpremultiplied input color and |
| 168 | * produce a premultiplied output. |
| 169 | */ |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 170 | virtual std::unique_ptr<GrFragmentProcessor> asFragmentProcessor(const AsFPArgs&) const; |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 171 | #endif |
| 172 | |
| 173 | /** |
| 174 | * If the shader can represent its "average" luminance in a single color, return true and |
| 175 | * if color is not NULL, return that color. If it cannot, return false and ignore the color |
| 176 | * parameter. |
| 177 | * |
| 178 | * Note: if this returns true, the returned color will always be opaque, as only the RGB |
| 179 | * components are used to compute luminance. |
| 180 | */ |
| 181 | bool asLuminanceColor(SkColor*) const; |
| 182 | |
| 183 | /** |
| 184 | * Returns a shader transformed into a new color space via the |xformer|. |
| 185 | */ |
| 186 | sk_sp<SkShader> makeColorSpace(SkColorSpaceXformer* xformer) const { |
| 187 | return this->onMakeColorSpace(xformer); |
| 188 | } |
| 189 | |
Mike Reed | 3404207 | 2017-08-08 16:29:22 -0400 | [diff] [blame] | 190 | bool isRasterPipelineOnly(const SkMatrix& ctm) const { |
Florin Malita | 5769dd2 | 2017-07-12 13:31:25 -0400 | [diff] [blame] | 191 | // We always use RP when perspective is present. |
Mike Reed | 3404207 | 2017-08-08 16:29:22 -0400 | [diff] [blame] | 192 | return ctm.hasPerspective() || fLocalMatrix.hasPerspective() |
| 193 | || this->onIsRasterPipelineOnly(ctm); |
Florin Malita | 5769dd2 | 2017-07-12 13:31:25 -0400 | [diff] [blame] | 194 | } |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 195 | |
Mike Reed | 1d8c42e | 2017-08-29 14:58:19 -0400 | [diff] [blame^] | 196 | struct StageRec { |
| 197 | SkRasterPipeline* fPipeline; |
| 198 | SkArenaAlloc* fAlloc; |
| 199 | SkColorSpace* fDstCS; // may be nullptr |
| 200 | const SkPaint& fPaint; |
| 201 | const SkMatrix* fLocalM; // may be nullptr |
| 202 | SkMatrix fCTM; |
| 203 | }; |
| 204 | |
Mike Reed | 6867eee | 2017-06-02 13:25:15 -0400 | [diff] [blame] | 205 | // 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^] | 206 | bool appendStages(const StageRec&) const; |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 207 | |
| 208 | bool computeTotalInverse(const SkMatrix& ctm, |
| 209 | const SkMatrix* outerLocalMatrix, |
| 210 | SkMatrix* totalInverse) const; |
| 211 | |
| 212 | #ifdef SK_SUPPORT_LEGACY_SHADER_ISABITMAP |
| 213 | virtual bool onIsABitmap(SkBitmap*, SkMatrix*, TileMode[2]) const { |
| 214 | return false; |
| 215 | } |
| 216 | #endif |
| 217 | |
| 218 | virtual SkImage* onIsAImage(SkMatrix*, TileMode[2]) const { |
| 219 | return nullptr; |
| 220 | } |
| 221 | |
| 222 | SK_TO_STRING_VIRT() |
| 223 | |
| 224 | SK_DEFINE_FLATTENABLE_TYPE(SkShaderBase) |
| 225 | SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP() |
| 226 | |
| 227 | protected: |
Florin Malita | f7beee7 | 2017-05-26 12:54:32 -0400 | [diff] [blame] | 228 | SkShaderBase(const SkMatrix* localMatrix = nullptr); |
| 229 | |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 230 | void flatten(SkWriteBuffer&) const override; |
| 231 | |
| 232 | /** |
| 233 | * Specialize creating a SkShader context using the supplied allocator. |
| 234 | * @return pointer to context owned by the arena allocator. |
| 235 | */ |
| 236 | virtual Context* onMakeContext(const ContextRec&, SkArenaAlloc*) const { |
| 237 | return nullptr; |
| 238 | } |
| 239 | |
Florin Malita | 47e55a5 | 2017-06-06 12:26:54 -0400 | [diff] [blame] | 240 | /** |
| 241 | * Overriden by shaders which prefer burst mode. |
| 242 | */ |
| 243 | virtual Context* onMakeBurstPipelineContext(const ContextRec&, SkArenaAlloc*) const { |
| 244 | return nullptr; |
| 245 | } |
| 246 | |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 247 | virtual bool onAsLuminanceColor(SkColor*) const { |
| 248 | return false; |
| 249 | } |
| 250 | |
| 251 | virtual sk_sp<SkShader> onMakeColorSpace(SkColorSpaceXformer*) const { |
| 252 | return sk_ref_sp(const_cast<SkShaderBase*>(this)); |
| 253 | } |
| 254 | |
Mike Reed | 6867eee | 2017-06-02 13:25:15 -0400 | [diff] [blame] | 255 | // Default impl creates shadercontext and calls that (not very efficient) |
Mike Reed | 1d8c42e | 2017-08-29 14:58:19 -0400 | [diff] [blame^] | 256 | virtual bool onAppendStages(const StageRec&) const; |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 257 | |
Mike Reed | 3404207 | 2017-08-08 16:29:22 -0400 | [diff] [blame] | 258 | virtual bool onIsRasterPipelineOnly(const SkMatrix& ctm) const { return false; } |
Florin Malita | 5769dd2 | 2017-07-12 13:31:25 -0400 | [diff] [blame] | 259 | |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 260 | private: |
| 261 | // This is essentially const, but not officially so it can be modified in constructors. |
| 262 | SkMatrix fLocalMatrix; |
| 263 | |
| 264 | typedef SkShader INHERITED; |
| 265 | }; |
| 266 | |
| 267 | inline SkShaderBase* as_SB(SkShader* shader) { |
| 268 | return static_cast<SkShaderBase*>(shader); |
| 269 | } |
| 270 | |
| 271 | inline const SkShaderBase* as_SB(const SkShader* shader) { |
| 272 | return static_cast<const SkShaderBase*>(shader); |
| 273 | } |
| 274 | |
| 275 | inline const SkShaderBase* as_SB(const sk_sp<SkShader>& shader) { |
| 276 | return static_cast<SkShaderBase*>(shader.get()); |
| 277 | } |
| 278 | |
| 279 | #endif // SkShaderBase_DEFINED |