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