reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2006 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 3 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #ifndef SkShader_DEFINED |
| 9 | #define SkShader_DEFINED |
| 10 | |
| 11 | #include "SkBitmap.h" |
| 12 | #include "SkFlattenable.h" |
fmalita | d0c4e09 | 2016-02-22 17:19:04 -0800 | [diff] [blame] | 13 | #include "SkImageInfo.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 14 | #include "SkMask.h" |
| 15 | #include "SkMatrix.h" |
| 16 | #include "SkPaint.h" |
bsalomon | 4beef91 | 2014-07-28 13:43:02 -0700 | [diff] [blame] | 17 | #include "../gpu/GrColor.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 18 | |
reed | 3061af4 | 2016-01-07 15:47:29 -0800 | [diff] [blame] | 19 | class SkColorFilter; |
reed | 0ccc62d | 2016-05-04 13:09:39 -0700 | [diff] [blame] | 20 | class SkColorSpace; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 21 | class SkPath; |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 22 | class SkPicture; |
commit-bot@chromium.org | 7959055 | 2014-05-13 18:14:45 +0000 | [diff] [blame] | 23 | class SkXfermode; |
rileya@google.com | 03c1c35 | 2012-07-20 20:02:43 +0000 | [diff] [blame] | 24 | class GrContext; |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 25 | class GrFragmentProcessor; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 26 | |
| 27 | /** \class SkShader |
reed@google.com | ad91799 | 2011-04-11 19:01:12 +0000 | [diff] [blame] | 28 | * |
reed@google.com | 880dc47 | 2012-05-11 14:47:03 +0000 | [diff] [blame] | 29 | * Shaders specify the source color(s) for what is being drawn. If a paint |
| 30 | * has no shader, then the paint's color is used. If the paint has a |
| 31 | * shader, then the shader's color(s) are use instead, but they are |
| 32 | * modulated by the paint's alpha. This makes it easy to create a shader |
| 33 | * once (e.g. bitmap tiling or gradient) and then change its transparency |
| 34 | * w/o having to modify the original shader... only the paint's alpha needs |
| 35 | * to be modified. |
reed@google.com | ad91799 | 2011-04-11 19:01:12 +0000 | [diff] [blame] | 36 | */ |
ctguil@chromium.org | 7ffb1b2 | 2011-03-15 21:27:08 +0000 | [diff] [blame] | 37 | class SK_API SkShader : public SkFlattenable { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 38 | public: |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 39 | SkShader(const SkMatrix* localMatrix = NULL); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 40 | virtual ~SkShader(); |
| 41 | |
reed@google.com | ad91799 | 2011-04-11 19:01:12 +0000 | [diff] [blame] | 42 | /** |
commit-bot@chromium.org | d12de02 | 2014-05-09 15:42:07 +0000 | [diff] [blame] | 43 | * Returns the local matrix. |
scroggo | c870d49 | 2014-07-11 10:42:12 -0700 | [diff] [blame] | 44 | * |
| 45 | * FIXME: This can be incorrect for a Shader with its own local matrix |
| 46 | * that is also wrapped via CreateLocalMatrixShader. |
commit-bot@chromium.org | d12de02 | 2014-05-09 15:42:07 +0000 | [diff] [blame] | 47 | */ |
| 48 | const SkMatrix& getLocalMatrix() const { return fLocalMatrix; } |
| 49 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 50 | enum TileMode { |
reed@google.com | 0beaba5 | 2012-03-16 14:38:06 +0000 | [diff] [blame] | 51 | /** replicate the edge color if the shader draws outside of its |
| 52 | * original bounds |
| 53 | */ |
| 54 | kClamp_TileMode, |
| 55 | |
| 56 | /** repeat the shader's image horizontally and vertically */ |
| 57 | kRepeat_TileMode, |
| 58 | |
| 59 | /** repeat the shader's image horizontally and vertically, alternating |
| 60 | * mirror images so that adjacent images always seam |
| 61 | */ |
| 62 | kMirror_TileMode, |
| 63 | |
| 64 | #if 0 |
| 65 | /** only draw within the original domain, return 0 everywhere else */ |
| 66 | kDecal_TileMode, |
| 67 | #endif |
reed | 19c25f1 | 2015-03-15 14:01:21 -0700 | [diff] [blame] | 68 | }; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 69 | |
reed | 19c25f1 | 2015-03-15 14:01:21 -0700 | [diff] [blame] | 70 | enum { |
| 71 | kTileModeCount = kMirror_TileMode + 1 |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | // override these in your subclass |
| 75 | |
| 76 | enum Flags { |
| 77 | //!< set if all of the colors will be opaque |
reed | 4e5a758 | 2016-01-05 05:10:33 -0800 | [diff] [blame] | 78 | kOpaqueAlpha_Flag = 1 << 0, |
reed@android.com | 5119bdb | 2009-06-12 21:27:03 +0000 | [diff] [blame] | 79 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 80 | /** set if the spans only vary in X (const in Y). |
reed@android.com | 5119bdb | 2009-06-12 21:27:03 +0000 | [diff] [blame] | 81 | e.g. an Nx1 bitmap that is being tiled in Y, or a linear-gradient |
reed@android.com | 3c9b2a4 | 2009-08-27 19:28:37 +0000 | [diff] [blame] | 82 | that varies from left-to-right. This flag specifies this for |
| 83 | shadeSpan(). |
reed@android.com | 5119bdb | 2009-06-12 21:27:03 +0000 | [diff] [blame] | 84 | */ |
reed | 4e5a758 | 2016-01-05 05:10:33 -0800 | [diff] [blame] | 85 | kConstInY32_Flag = 1 << 1, |
fmalita | ca058f5 | 2016-02-23 19:02:20 -0800 | [diff] [blame] | 86 | |
| 87 | /** hint for the blitter that 4f is the preferred shading mode. |
| 88 | */ |
| 89 | kPrefers4f_Flag = 1 << 2, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 90 | }; |
| 91 | |
reed@google.com | ad91799 | 2011-04-11 19:01:12 +0000 | [diff] [blame] | 92 | /** |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 93 | * Returns true if the shader is guaranteed to produce only opaque |
| 94 | * colors, subject to the SkPaint using the shader to apply an opaque |
| 95 | * alpha value. Subclasses should override this to allow some |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 96 | * optimizations. |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame] | 97 | */ |
| 98 | virtual bool isOpaque() const { return false; } |
| 99 | |
commit-bot@chromium.org | e901b6d | 2014-05-01 19:31:31 +0000 | [diff] [blame] | 100 | /** |
| 101 | * ContextRec acts as a parameter bundle for creating Contexts. |
| 102 | */ |
| 103 | struct ContextRec { |
fmalita | d0c4e09 | 2016-02-22 17:19:04 -0800 | [diff] [blame] | 104 | enum DstType { |
| 105 | kPMColor_DstType, // clients prefer shading into PMColor dest |
| 106 | kPM4f_DstType, // clients prefer shading into PM4f dest |
| 107 | }; |
| 108 | |
| 109 | ContextRec(const SkPaint& paint, const SkMatrix& matrix, const SkMatrix* localM, |
| 110 | DstType dstType) |
reed | 56263c7 | 2015-06-05 11:31:26 -0700 | [diff] [blame] | 111 | : fPaint(&paint) |
commit-bot@chromium.org | 80116dc | 2014-05-06 17:16:03 +0000 | [diff] [blame] | 112 | , fMatrix(&matrix) |
fmalita | d0c4e09 | 2016-02-22 17:19:04 -0800 | [diff] [blame] | 113 | , fLocalMatrix(localM) |
| 114 | , fPreferredDstType(dstType) {} |
commit-bot@chromium.org | e901b6d | 2014-05-01 19:31:31 +0000 | [diff] [blame] | 115 | |
fmalita | d0c4e09 | 2016-02-22 17:19:04 -0800 | [diff] [blame] | 116 | const SkPaint* fPaint; // the current paint associated with the draw |
| 117 | const SkMatrix* fMatrix; // the current matrix in the canvas |
| 118 | const SkMatrix* fLocalMatrix; // optional local matrix |
| 119 | const DstType fPreferredDstType; // the "natural" client dest type |
commit-bot@chromium.org | e901b6d | 2014-05-01 19:31:31 +0000 | [diff] [blame] | 120 | }; |
| 121 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 122 | class Context : public ::SkNoncopyable { |
| 123 | public: |
commit-bot@chromium.org | e901b6d | 2014-05-01 19:31:31 +0000 | [diff] [blame] | 124 | Context(const SkShader& shader, const ContextRec&); |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 125 | |
| 126 | virtual ~Context(); |
| 127 | |
| 128 | /** |
| 129 | * Called sometimes before drawing with this shader. Return the type of |
| 130 | * alpha your shader will return. The default implementation returns 0. |
| 131 | * Your subclass should override if it can (even sometimes) report a |
| 132 | * non-zero value, since that will enable various blitters to perform |
| 133 | * faster. |
| 134 | */ |
| 135 | virtual uint32_t getFlags() const { return 0; } |
| 136 | |
| 137 | /** |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 138 | * Called for each span of the object being drawn. Your subclass should |
| 139 | * set the appropriate colors (with premultiplied alpha) that correspond |
| 140 | * to the specified device coordinates. |
| 141 | */ |
| 142 | virtual void shadeSpan(int x, int y, SkPMColor[], int count) = 0; |
| 143 | |
reed | 6d3cef9 | 2016-01-22 01:04:29 -0800 | [diff] [blame] | 144 | virtual void shadeSpan4f(int x, int y, SkPM4f[], int count); |
| 145 | |
reed | 58fc94e | 2016-03-18 12:42:26 -0700 | [diff] [blame] | 146 | struct BlitState; |
| 147 | typedef void (*BlitBW)(BlitState*, |
| 148 | int x, int y, const SkPixmap&, int count); |
| 149 | typedef void (*BlitAA)(BlitState*, |
| 150 | int x, int y, const SkPixmap&, int count, const SkAlpha[]); |
| 151 | |
reed | 830dfd8 | 2016-03-16 12:29:01 -0700 | [diff] [blame] | 152 | struct BlitState { |
reed | 58fc94e | 2016-03-18 12:42:26 -0700 | [diff] [blame] | 153 | // inputs |
reed | 830dfd8 | 2016-03-16 12:29:01 -0700 | [diff] [blame] | 154 | Context* fCtx; |
| 155 | SkXfermode* fXfer; |
reed | 58fc94e | 2016-03-18 12:42:26 -0700 | [diff] [blame] | 156 | |
| 157 | // outputs |
reed | 830dfd8 | 2016-03-16 12:29:01 -0700 | [diff] [blame] | 158 | enum { N = 2 }; |
| 159 | void* fStorage[N]; |
reed | 58fc94e | 2016-03-18 12:42:26 -0700 | [diff] [blame] | 160 | BlitBW fBlitBW; |
| 161 | BlitAA fBlitAA; |
reed | 830dfd8 | 2016-03-16 12:29:01 -0700 | [diff] [blame] | 162 | }; |
reed | 58fc94e | 2016-03-18 12:42:26 -0700 | [diff] [blame] | 163 | |
| 164 | // Returns true if one or more of the blitprocs are set in the BlitState |
| 165 | bool chooseBlitProcs(const SkImageInfo& info, BlitState* state) { |
| 166 | state->fBlitBW = nullptr; |
| 167 | state->fBlitAA = nullptr; |
| 168 | if (this->onChooseBlitProcs(info, state)) { |
| 169 | SkASSERT(state->fBlitBW || state->fBlitAA); |
| 170 | return true; |
| 171 | } |
| 172 | return false; |
reed | 830dfd8 | 2016-03-16 12:29:01 -0700 | [diff] [blame] | 173 | } |
reed | 58fc94e | 2016-03-18 12:42:26 -0700 | [diff] [blame] | 174 | |
herb | c7a784c | 2015-12-18 09:52:15 -0800 | [diff] [blame] | 175 | /** |
| 176 | * The const void* ctx is only const because all the implementations are const. |
| 177 | * This can be changed to non-const if a new shade proc needs to change the ctx. |
| 178 | */ |
| 179 | typedef void (*ShadeProc)(const void* ctx, int x, int y, SkPMColor[], int count); |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 180 | virtual ShadeProc asAShadeProc(void** ctx); |
| 181 | |
| 182 | /** |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 183 | * Similar to shadeSpan, but only returns the alpha-channel for a span. |
| 184 | * The default implementation calls shadeSpan() and then extracts the alpha |
| 185 | * values from the returned colors. |
| 186 | */ |
| 187 | virtual void shadeSpanAlpha(int x, int y, uint8_t alpha[], int count); |
| 188 | |
reed | cc0e311 | 2014-09-10 10:20:24 -0700 | [diff] [blame] | 189 | // Notification from blitter::blitMask in case we need to see the non-alpha channels |
| 190 | virtual void set3DMask(const SkMask*) {} |
| 191 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 192 | protected: |
| 193 | // Reference to shader, so we don't have to dupe information. |
| 194 | const SkShader& fShader; |
| 195 | |
| 196 | enum MatrixClass { |
| 197 | kLinear_MatrixClass, // no perspective |
| 198 | kFixedStepInX_MatrixClass, // fast perspective, need to call fixedStepInX() each |
| 199 | // scanline |
| 200 | kPerspective_MatrixClass // slow perspective, need to mappoints each pixel |
| 201 | }; |
| 202 | static MatrixClass ComputeMatrixClass(const SkMatrix&); |
| 203 | |
commit-bot@chromium.org | 80116dc | 2014-05-06 17:16:03 +0000 | [diff] [blame] | 204 | uint8_t getPaintAlpha() const { return fPaintAlpha; } |
| 205 | const SkMatrix& getTotalInverse() const { return fTotalInverse; } |
| 206 | MatrixClass getInverseClass() const { return (MatrixClass)fTotalInverseClass; } |
| 207 | const SkMatrix& getCTM() const { return fCTM; } |
reed | 830dfd8 | 2016-03-16 12:29:01 -0700 | [diff] [blame] | 208 | |
reed | 58fc94e | 2016-03-18 12:42:26 -0700 | [diff] [blame] | 209 | virtual bool onChooseBlitProcs(const SkImageInfo&, BlitState*) { return false; } |
reed | 830dfd8 | 2016-03-16 12:29:01 -0700 | [diff] [blame] | 210 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 211 | private: |
commit-bot@chromium.org | 80116dc | 2014-05-06 17:16:03 +0000 | [diff] [blame] | 212 | SkMatrix fCTM; |
| 213 | SkMatrix fTotalInverse; |
| 214 | uint8_t fPaintAlpha; |
| 215 | uint8_t fTotalInverseClass; |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 216 | |
| 217 | typedef SkNoncopyable INHERITED; |
| 218 | }; |
reed@google.com | 7c2f27d | 2011-03-07 19:29:00 +0000 | [diff] [blame] | 219 | |
reed@google.com | ad91799 | 2011-04-11 19:01:12 +0000 | [diff] [blame] | 220 | /** |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 221 | * Create the actual object that does the shading. |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 222 | * Size of storage must be >= contextSize. |
reed@google.com | a641f3f | 2012-12-13 22:16:30 +0000 | [diff] [blame] | 223 | */ |
commit-bot@chromium.org | ce56d96 | 2014-05-05 18:39:18 +0000 | [diff] [blame] | 224 | Context* createContext(const ContextRec&, void* storage) const; |
reed@google.com | a641f3f | 2012-12-13 22:16:30 +0000 | [diff] [blame] | 225 | |
| 226 | /** |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 227 | * Return the size of a Context returned by createContext. |
reed@google.com | ad91799 | 2011-04-11 19:01:12 +0000 | [diff] [blame] | 228 | */ |
reed | 773ceda | 2016-03-03 18:18:25 -0800 | [diff] [blame] | 229 | size_t contextSize(const ContextRec&) const; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 230 | |
reed@google.com | ad91799 | 2011-04-11 19:01:12 +0000 | [diff] [blame] | 231 | /** |
reed | 0f0af23 | 2015-09-08 11:02:04 -0700 | [diff] [blame] | 232 | * Returns true if this shader is just a bitmap, and if not null, returns the bitmap, |
| 233 | * localMatrix, and tilemodes. If this is not a bitmap, returns false and ignores the |
| 234 | * out-parameters. |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 235 | */ |
reed | 0f0af23 | 2015-09-08 11:02:04 -0700 | [diff] [blame] | 236 | bool isABitmap(SkBitmap* outTexture, SkMatrix* outMatrix, TileMode xy[2]) const { |
| 237 | return this->onIsABitmap(outTexture, outMatrix, xy); |
scroggo | ff390c9 | 2015-09-08 06:24:08 -0700 | [diff] [blame] | 238 | } |
reed | 0f0af23 | 2015-09-08 11:02:04 -0700 | [diff] [blame] | 239 | |
reed | f582282 | 2015-08-19 11:46:38 -0700 | [diff] [blame] | 240 | bool isABitmap() const { |
| 241 | return this->isABitmap(nullptr, nullptr, nullptr); |
| 242 | } |
| 243 | |
vandebo@chromium.org | d3ae779 | 2011-02-24 00:21:06 +0000 | [diff] [blame] | 244 | /** |
| 245 | * If the shader subclass can be represented as a gradient, asAGradient |
| 246 | * returns the matching GradientType enum (or kNone_GradientType if it |
| 247 | * cannot). Also, if info is not null, asAGradient populates info with |
| 248 | * the relevant (see below) parameters for the gradient. fColorCount |
| 249 | * is both an input and output parameter. On input, it indicates how |
| 250 | * many entries in fColors and fColorOffsets can be used, if they are |
| 251 | * non-NULL. After asAGradient has run, fColorCount indicates how |
| 252 | * many color-offset pairs there are in the gradient. If there is |
| 253 | * insufficient space to store all of the color-offset pairs, fColors |
| 254 | * and fColorOffsets will not be altered. fColorOffsets specifies |
| 255 | * where on the range of 0 to 1 to transition to the given color. |
| 256 | * The meaning of fPoint and fRadius is dependant on the type of gradient. |
| 257 | * |
| 258 | * None: |
| 259 | * info is ignored. |
| 260 | * Color: |
| 261 | * fColorOffsets[0] is meaningless. |
| 262 | * Linear: |
| 263 | * fPoint[0] and fPoint[1] are the end-points of the gradient |
| 264 | * Radial: |
| 265 | * fPoint[0] and fRadius[0] are the center and radius |
reed | 71a6cbf | 2015-05-04 08:32:51 -0700 | [diff] [blame] | 266 | * Conical: |
vandebo@chromium.org | d3ae779 | 2011-02-24 00:21:06 +0000 | [diff] [blame] | 267 | * fPoint[0] and fRadius[0] are the center and radius of the 1st circle |
| 268 | * fPoint[1] and fRadius[1] are the center and radius of the 2nd circle |
| 269 | * Sweep: |
| 270 | * fPoint[0] is the center of the sweep. |
| 271 | */ |
| 272 | |
| 273 | enum GradientType { |
| 274 | kNone_GradientType, |
| 275 | kColor_GradientType, |
| 276 | kLinear_GradientType, |
| 277 | kRadial_GradientType, |
vandebo@chromium.org | d3ae779 | 2011-02-24 00:21:06 +0000 | [diff] [blame] | 278 | kSweep_GradientType, |
reed@google.com | 8322697 | 2012-06-07 20:26:47 +0000 | [diff] [blame] | 279 | kConical_GradientType, |
| 280 | kLast_GradientType = kConical_GradientType |
vandebo@chromium.org | d3ae779 | 2011-02-24 00:21:06 +0000 | [diff] [blame] | 281 | }; |
| 282 | |
| 283 | struct GradientInfo { |
| 284 | int fColorCount; //!< In-out parameter, specifies passed size |
| 285 | // of fColors/fColorOffsets on input, and |
| 286 | // actual number of colors/offsets on |
| 287 | // output. |
| 288 | SkColor* fColors; //!< The colors in the gradient. |
| 289 | SkScalar* fColorOffsets; //!< The unit offset for color transitions. |
| 290 | SkPoint fPoint[2]; //!< Type specific, see above. |
| 291 | SkScalar fRadius[2]; //!< Type specific, see above. |
| 292 | TileMode fTileMode; //!< The tile mode used. |
reed@google.com | 3d3a860 | 2013-05-24 14:58:44 +0000 | [diff] [blame] | 293 | uint32_t fGradientFlags; //!< see SkGradientShader::Flags |
vandebo@chromium.org | d3ae779 | 2011-02-24 00:21:06 +0000 | [diff] [blame] | 294 | }; |
| 295 | |
| 296 | virtual GradientType asAGradient(GradientInfo* info) const; |
| 297 | |
rileya@google.com | 03c1c35 | 2012-07-20 20:02:43 +0000 | [diff] [blame] | 298 | /** |
commit-bot@chromium.org | 7959055 | 2014-05-13 18:14:45 +0000 | [diff] [blame] | 299 | * If the shader subclass is composed of two shaders, return true, and if rec is not NULL, |
| 300 | * fill it out with info about the shader. |
commit-bot@chromium.org | 3055879 | 2014-05-14 14:28:34 +0000 | [diff] [blame] | 301 | * |
| 302 | * These are bare pointers; the ownership and reference count are unchanged. |
commit-bot@chromium.org | 7959055 | 2014-05-13 18:14:45 +0000 | [diff] [blame] | 303 | */ |
| 304 | |
| 305 | struct ComposeRec { |
| 306 | const SkShader* fShaderA; |
| 307 | const SkShader* fShaderB; |
| 308 | const SkXfermode* fMode; |
| 309 | }; |
| 310 | |
djsollen | c87dd2c | 2014-11-14 11:11:46 -0800 | [diff] [blame] | 311 | virtual bool asACompose(ComposeRec*) const { return false; } |
commit-bot@chromium.org | 7959055 | 2014-05-13 18:14:45 +0000 | [diff] [blame] | 312 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame^] | 313 | #if SK_SUPPORT_GPU |
commit-bot@chromium.org | 7959055 | 2014-05-13 18:14:45 +0000 | [diff] [blame] | 314 | /** |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 315 | * Returns a GrFragmentProcessor that implements the shader for the GPU backend. NULL is |
| 316 | * returned if there is no GPU implementation. |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 317 | * |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 318 | * The GPU device does not call SkShader::createContext(), instead we pass the view matrix, |
| 319 | * local matrix, and filter quality directly. |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 320 | * |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 321 | * The GrContext may be used by the to create textures that are required by the returned |
| 322 | * processor. |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 323 | * |
| 324 | * The returned GrFragmentProcessor should expect an unpremultiplied input color and |
| 325 | * produce a premultiplied output. |
rileya@google.com | 03c1c35 | 2012-07-20 20:02:43 +0000 | [diff] [blame] | 326 | */ |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame^] | 327 | virtual sk_sp<GrFragmentProcessor> asFragmentProcessor(GrContext*, |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 328 | const SkMatrix& viewMatrix, |
| 329 | const SkMatrix* localMatrix, |
brianosman | 982eb7f | 2016-06-06 13:10:58 -0700 | [diff] [blame] | 330 | SkFilterQuality, |
| 331 | SkSourceGammaTreatment) const; |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame^] | 332 | #endif |
rileya@google.com | 03c1c35 | 2012-07-20 20:02:43 +0000 | [diff] [blame] | 333 | |
reed | 8367b8c | 2014-08-22 08:30:20 -0700 | [diff] [blame] | 334 | /** |
| 335 | * If the shader can represent its "average" luminance in a single color, return true and |
| 336 | * if color is not NULL, return that color. If it cannot, return false and ignore the color |
| 337 | * parameter. |
| 338 | * |
| 339 | * Note: if this returns true, the returned color will always be opaque, as only the RGB |
| 340 | * components are used to compute luminance. |
| 341 | */ |
| 342 | bool asLuminanceColor(SkColor*) const; |
| 343 | |
commit-bot@chromium.org | 7959055 | 2014-05-13 18:14:45 +0000 | [diff] [blame] | 344 | #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 345 | /** |
| 346 | * If the shader is a custom shader which has data the caller might want, call this function |
| 347 | * to get that data. |
| 348 | */ |
scroggo | 01c412e | 2014-11-24 09:05:35 -0800 | [diff] [blame] | 349 | virtual bool asACustomShader(void** /* customData */) const { return false; } |
commit-bot@chromium.org | 7959055 | 2014-05-13 18:14:45 +0000 | [diff] [blame] | 350 | #endif |
| 351 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 352 | ////////////////////////////////////////////////////////////////////////// |
reed | f880e45 | 2015-12-30 13:39:41 -0800 | [diff] [blame] | 353 | // Methods to create combinations or variants of shaders |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 354 | |
commit-bot@chromium.org | ce56d96 | 2014-05-05 18:39:18 +0000 | [diff] [blame] | 355 | /** |
reed | f880e45 | 2015-12-30 13:39:41 -0800 | [diff] [blame] | 356 | * Return a shader that will apply the specified localMatrix to this shader. |
| 357 | * The specified matrix will be applied before any matrix associated with this shader. |
| 358 | */ |
reed | 150835e | 2016-03-10 06:36:49 -0800 | [diff] [blame] | 359 | sk_sp<SkShader> makeWithLocalMatrix(const SkMatrix&) const; |
reed | 3061af4 | 2016-01-07 15:47:29 -0800 | [diff] [blame] | 360 | |
| 361 | /** |
| 362 | * Create a new shader that produces the same colors as invoking this shader and then applying |
| 363 | * the colorfilter. |
| 364 | */ |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 365 | sk_sp<SkShader> makeWithColorFilter(sk_sp<SkColorFilter>) const; |
reed | a2b340f | 2016-02-10 08:53:15 -0800 | [diff] [blame] | 366 | |
reed | f880e45 | 2015-12-30 13:39:41 -0800 | [diff] [blame] | 367 | ////////////////////////////////////////////////////////////////////////// |
| 368 | // Factory methods for stock shaders |
| 369 | |
| 370 | /** |
commit-bot@chromium.org | ce56d96 | 2014-05-05 18:39:18 +0000 | [diff] [blame] | 371 | * Call this to create a new "empty" shader, that will not draw anything. |
| 372 | */ |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 373 | static sk_sp<SkShader> MakeEmptyShader(); |
commit-bot@chromium.org | ce56d96 | 2014-05-05 18:39:18 +0000 | [diff] [blame] | 374 | |
reed | 8367b8c | 2014-08-22 08:30:20 -0700 | [diff] [blame] | 375 | /** |
| 376 | * Call this to create a new shader that just draws the specified color. This should always |
| 377 | * draw the same as a paint with this color (and no shader). |
| 378 | */ |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 379 | static sk_sp<SkShader> MakeColorShader(SkColor); |
reed | 8367b8c | 2014-08-22 08:30:20 -0700 | [diff] [blame] | 380 | |
reed | 0ccc62d | 2016-05-04 13:09:39 -0700 | [diff] [blame] | 381 | /** |
| 382 | * Create a shader that draws the specified color (in the specified colorspace). |
| 383 | * |
| 384 | * This works around the limitation that SkPaint::setColor() only takes byte values, and does |
| 385 | * not support specific colorspaces. |
| 386 | */ |
| 387 | static sk_sp<SkShader> MakeColorShader(const SkColor4f&, sk_sp<SkColorSpace>); |
| 388 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 389 | static sk_sp<SkShader> MakeComposeShader(sk_sp<SkShader> dst, sk_sp<SkShader> src, |
| 390 | SkXfermode::Mode); |
| 391 | |
| 392 | #ifdef SK_SUPPORT_LEGACY_CREATESHADER_PTR |
| 393 | static SkShader* CreateEmptyShader() { return MakeEmptyShader().release(); } |
| 394 | static SkShader* CreateColorShader(SkColor c) { return MakeColorShader(c).release(); } |
| 395 | static SkShader* CreateBitmapShader(const SkBitmap& src, TileMode tmx, TileMode tmy, |
| 396 | const SkMatrix* localMatrix = nullptr) { |
| 397 | return MakeBitmapShader(src, tmx, tmy, localMatrix).release(); |
| 398 | } |
| 399 | static SkShader* CreateComposeShader(SkShader* dst, SkShader* src, SkXfermode::Mode mode); |
| 400 | static SkShader* CreateComposeShader(SkShader* dst, SkShader* src, SkXfermode* xfer); |
| 401 | static SkShader* CreatePictureShader(const SkPicture* src, TileMode tmx, TileMode tmy, |
| 402 | const SkMatrix* localMatrix, const SkRect* tile); |
reed | 150835e | 2016-03-10 06:36:49 -0800 | [diff] [blame] | 403 | |
| 404 | SkShader* newWithLocalMatrix(const SkMatrix& matrix) const { |
| 405 | return this->makeWithLocalMatrix(matrix).release(); |
| 406 | } |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 407 | SkShader* newWithColorFilter(SkColorFilter* filter) const; |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 408 | #endif |
reed | a2b340f | 2016-02-10 08:53:15 -0800 | [diff] [blame] | 409 | |
| 410 | /** |
| 411 | * Create a new compose shader, given shaders dst, src, and a combining xfermode mode. |
| 412 | * The xfermode is called with the output of the two shaders, and its output is returned. |
| 413 | * If xfer is null, SkXfermode::kSrcOver_Mode is assumed. |
| 414 | * |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 415 | * The caller is responsible for managing its reference-count for the xfer (if not null). |
reed | a2b340f | 2016-02-10 08:53:15 -0800 | [diff] [blame] | 416 | */ |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 417 | static sk_sp<SkShader> MakeComposeShader(sk_sp<SkShader> dst, sk_sp<SkShader> src, |
reed | cfb6bdf | 2016-03-29 11:32:50 -0700 | [diff] [blame] | 418 | sk_sp<SkXfermode> xfer); |
| 419 | #ifdef SK_SUPPORT_LEGACY_XFERMODE_PTR |
| 420 | static sk_sp<SkShader> MakeComposeShader(sk_sp<SkShader> dst, sk_sp<SkShader> src, |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 421 | SkXfermode* xfer); |
reed | cfb6bdf | 2016-03-29 11:32:50 -0700 | [diff] [blame] | 422 | #endif |
reed | a2b340f | 2016-02-10 08:53:15 -0800 | [diff] [blame] | 423 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 424 | /** Call this to create a new shader that will draw with the specified bitmap. |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 425 | * |
| 426 | * If the bitmap cannot be used (e.g. has no pixels, or its dimensions |
| 427 | * exceed implementation limits (currently at 64K - 1)) then SkEmptyShader |
| 428 | * may be returned. |
| 429 | * |
commit-bot@chromium.org | 91246b9 | 2013-12-05 15:43:19 +0000 | [diff] [blame] | 430 | * If the src is kA8_Config then that mask will be colorized using the color on |
| 431 | * the paint. |
| 432 | * |
reed@google.com | 99c114e | 2012-05-03 20:14:26 +0000 | [diff] [blame] | 433 | * @param src The bitmap to use inside the shader |
| 434 | * @param tmx The tiling mode to use when sampling the bitmap in the x-direction. |
| 435 | * @param tmy The tiling mode to use when sampling the bitmap in the y-direction. |
| 436 | * @return Returns a new shader object. Note: this function never returns null. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 437 | */ |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 438 | static sk_sp<SkShader> MakeBitmapShader(const SkBitmap& src, TileMode tmx, TileMode tmy, |
| 439 | const SkMatrix* localMatrix = nullptr); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 440 | |
halcanary | a5f46e1 | 2015-09-08 07:12:25 -0700 | [diff] [blame] | 441 | // NOTE: You can create an SkImage Shader with SkImage::newShader(). |
| 442 | |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 443 | /** Call this to create a new shader that will draw with the specified picture. |
| 444 | * |
| 445 | * @param src The picture to use inside the shader (if not NULL, its ref count |
commit-bot@chromium.org | 855e88e | 2014-04-21 19:33:12 +0000 | [diff] [blame] | 446 | * is incremented). The SkPicture must not be changed after |
| 447 | * successfully creating a picture shader. |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 448 | * @param tmx The tiling mode to use when sampling the bitmap in the x-direction. |
| 449 | * @param tmy The tiling mode to use when sampling the bitmap in the y-direction. |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 450 | * @param tile The tile rectangle in picture coordinates: this represents the subset |
| 451 | * (or superset) of the picture used when building a tile. It is not |
| 452 | * affected by localMatrix and does not imply scaling (only translation |
| 453 | * and cropping). If null, the tile rect is considered equal to the picture |
| 454 | * bounds. |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 455 | * @return Returns a new shader object. Note: this function never returns null. |
| 456 | */ |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 457 | static sk_sp<SkShader> MakePictureShader(sk_sp<SkPicture> src, TileMode tmx, TileMode tmy, |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 458 | const SkMatrix* localMatrix, const SkRect* tile); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 459 | |
commit-bot@chromium.org | 8fae213 | 2014-05-07 22:26:37 +0000 | [diff] [blame] | 460 | /** |
commit-bot@chromium.org | 8fae213 | 2014-05-07 22:26:37 +0000 | [diff] [blame] | 461 | * If this shader can be represented by another shader + a localMatrix, return that shader |
| 462 | * and, if not NULL, the localMatrix. If not, return NULL and ignore the localMatrix parameter. |
| 463 | * |
| 464 | * Note: the returned shader (if not NULL) will have been ref'd, and it is the responsibility |
| 465 | * of the caller to balance that with unref() when they are done. |
| 466 | */ |
| 467 | virtual SkShader* refAsALocalMatrixShader(SkMatrix* localMatrix) const; |
| 468 | |
robertphillips | 0a482f4 | 2015-01-26 07:00:04 -0800 | [diff] [blame] | 469 | SK_TO_STRING_VIRT() |
mtklein | 3b37545 | 2016-04-04 14:57:19 -0700 | [diff] [blame] | 470 | SK_DEFINE_FLATTENABLE_TYPE(SkShader) |
commit-bot@chromium.org | c0b7e10 | 2013-10-23 17:06:21 +0000 | [diff] [blame] | 471 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 472 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 473 | void flatten(SkWriteBuffer&) const override; |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 474 | |
commit-bot@chromium.org | ce56d96 | 2014-05-05 18:39:18 +0000 | [diff] [blame] | 475 | bool computeTotalInverse(const ContextRec&, SkMatrix* totalInverse) const; |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 476 | |
commit-bot@chromium.org | ce56d96 | 2014-05-05 18:39:18 +0000 | [diff] [blame] | 477 | /** |
| 478 | * Your subclass must also override contextSize() if it overrides onCreateContext(). |
| 479 | * Base class impl returns NULL. |
| 480 | */ |
| 481 | virtual Context* onCreateContext(const ContextRec&, void* storage) const; |
| 482 | |
reed | 773ceda | 2016-03-03 18:18:25 -0800 | [diff] [blame] | 483 | /** |
| 484 | * Override this if your subclass overrides createContext, to return the correct size of |
| 485 | * your subclass' context. |
| 486 | */ |
| 487 | virtual size_t onContextSize(const ContextRec&) const; |
| 488 | |
reed | 8367b8c | 2014-08-22 08:30:20 -0700 | [diff] [blame] | 489 | virtual bool onAsLuminanceColor(SkColor*) const { |
| 490 | return false; |
| 491 | } |
reed | 0f0af23 | 2015-09-08 11:02:04 -0700 | [diff] [blame] | 492 | |
| 493 | virtual bool onIsABitmap(SkBitmap*, SkMatrix*, TileMode[2]) const { |
| 494 | return false; |
| 495 | } |
| 496 | |
commit-bot@chromium.org | ce56d96 | 2014-05-05 18:39:18 +0000 | [diff] [blame] | 497 | private: |
scroggo | ef0fd61 | 2014-07-11 11:33:52 -0700 | [diff] [blame] | 498 | // This is essentially const, but not officially so it can be modified in |
| 499 | // constructors. |
commit-bot@chromium.org | ce56d96 | 2014-05-05 18:39:18 +0000 | [diff] [blame] | 500 | SkMatrix fLocalMatrix; |
scroggo | c870d49 | 2014-07-11 10:42:12 -0700 | [diff] [blame] | 501 | |
| 502 | // So the SkLocalMatrixShader can whack fLocalMatrix in its SkReadBuffer constructor. |
| 503 | friend class SkLocalMatrixShader; |
reed | 7a4d847 | 2015-09-15 13:33:58 -0700 | [diff] [blame] | 504 | friend class SkBitmapProcShader; // for computeTotalInverse() |
scroggo | c870d49 | 2014-07-11 10:42:12 -0700 | [diff] [blame] | 505 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 506 | typedef SkFlattenable INHERITED; |
| 507 | }; |
| 508 | |
| 509 | #endif |