epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2006 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 4 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | #ifndef SkShader_DEFINED |
| 11 | #define SkShader_DEFINED |
| 12 | |
| 13 | #include "SkBitmap.h" |
| 14 | #include "SkFlattenable.h" |
| 15 | #include "SkMask.h" |
| 16 | #include "SkMatrix.h" |
| 17 | #include "SkPaint.h" |
| 18 | |
| 19 | class SkPath; |
| 20 | |
| 21 | /** \class SkShader |
reed@google.com | ad91799 | 2011-04-11 19:01:12 +0000 | [diff] [blame] | 22 | * |
| 23 | * SkShader is the based class for objects that return horizontal spans of |
| 24 | * colors during drawing. A subclass of SkShader is installed in a SkPaint |
| 25 | * calling paint.setShader(shader). After that any object (other than a bitmap) |
| 26 | * that is drawn with that paint will get its color(s) from the shader. |
| 27 | */ |
ctguil@chromium.org | 7ffb1b2 | 2011-03-15 21:27:08 +0000 | [diff] [blame] | 28 | class SK_API SkShader : public SkFlattenable { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 29 | public: |
| 30 | SkShader(); |
| 31 | virtual ~SkShader(); |
| 32 | |
reed@google.com | ad91799 | 2011-04-11 19:01:12 +0000 | [diff] [blame] | 33 | /** |
| 34 | * Return true if the shader has a non-identity local matrix. |
| 35 | * @param localM Optional: If not null, return the shader's local matrix |
| 36 | * @return true if the shader has a non-identity local matrix. |
| 37 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 38 | bool getLocalMatrix(SkMatrix* localM) const; |
reed@google.com | ad91799 | 2011-04-11 19:01:12 +0000 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * Set the shader's local matrix. |
| 42 | * @param localM The shader's new local matrix. |
| 43 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 44 | void setLocalMatrix(const SkMatrix& localM); |
reed@google.com | ad91799 | 2011-04-11 19:01:12 +0000 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * Reset the shader's local matrix to identity. |
| 48 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 49 | void resetLocalMatrix(); |
| 50 | |
| 51 | enum TileMode { |
| 52 | kClamp_TileMode, //!< replicate the edge color if the shader draws outside of its original bounds |
| 53 | kRepeat_TileMode, //!< repeat the shader's image horizontally and vertically |
| 54 | kMirror_TileMode, //!< repeat the shader's image horizontally and vertically, alternating mirror images so that adjacent images always seam |
| 55 | |
| 56 | kTileModeCount |
| 57 | }; |
| 58 | |
| 59 | // override these in your subclass |
| 60 | |
| 61 | enum Flags { |
| 62 | //!< set if all of the colors will be opaque |
reed@android.com | 3c9b2a4 | 2009-08-27 19:28:37 +0000 | [diff] [blame] | 63 | kOpaqueAlpha_Flag = 0x01, |
reed@android.com | 5119bdb | 2009-06-12 21:27:03 +0000 | [diff] [blame] | 64 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 65 | //! set if this shader's shadeSpan16() method can be called |
reed@android.com | 3c9b2a4 | 2009-08-27 19:28:37 +0000 | [diff] [blame] | 66 | kHasSpan16_Flag = 0x02, |
reed@android.com | 5119bdb | 2009-06-12 21:27:03 +0000 | [diff] [blame] | 67 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 68 | /** Set this bit if the shader's native data type is instrinsically 16 |
| 69 | bit, meaning that calling the 32bit shadeSpan() entry point will |
| 70 | mean the the impl has to up-sample 16bit data into 32bit. Used as a |
| 71 | a means of clearing a dither request if the it will have no effect |
| 72 | */ |
reed@android.com | 5119bdb | 2009-06-12 21:27:03 +0000 | [diff] [blame] | 73 | kIntrinsicly16_Flag = 0x04, |
| 74 | |
| 75 | /** set (after setContext) if the spans only vary in X (const in Y). |
| 76 | 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] | 77 | that varies from left-to-right. This flag specifies this for |
| 78 | shadeSpan(). |
reed@android.com | 5119bdb | 2009-06-12 21:27:03 +0000 | [diff] [blame] | 79 | */ |
reed@android.com | 3c9b2a4 | 2009-08-27 19:28:37 +0000 | [diff] [blame] | 80 | kConstInY32_Flag = 0x08, |
reed@google.com | 7c2f27d | 2011-03-07 19:29:00 +0000 | [diff] [blame] | 81 | |
reed@android.com | 3c9b2a4 | 2009-08-27 19:28:37 +0000 | [diff] [blame] | 82 | /** same as kConstInY32_Flag, but is set if this is true for shadeSpan16 |
| 83 | which may not always be the case, since shadeSpan16 may be |
| 84 | predithered, which would mean it was not const in Y, even though |
| 85 | the 32bit shadeSpan() would be const. |
| 86 | */ |
| 87 | kConstInY16_Flag = 0x10 |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 88 | }; |
| 89 | |
reed@google.com | ad91799 | 2011-04-11 19:01:12 +0000 | [diff] [blame] | 90 | /** |
| 91 | * Called sometimes before drawing with this shader. Return the type of |
| 92 | * alpha your shader will return. The default implementation returns 0. |
| 93 | * Your subclass should override if it can (even sometimes) report a |
| 94 | * non-zero value, since that will enable various blitters to perform |
| 95 | * faster. |
| 96 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 97 | virtual uint32_t getFlags() { return 0; } |
| 98 | |
reed@google.com | ad91799 | 2011-04-11 19:01:12 +0000 | [diff] [blame] | 99 | /** |
junov@chromium.org | b6e1619 | 2011-12-09 15:48:03 +0000 | [diff] [blame^] | 100 | * Returns true if the shader is guaranteed to produce only opaque |
| 101 | * colors, subject to the SkPaint using the shader to apply an opaque |
| 102 | * alpha value. Subclasses should override this to allow some |
| 103 | * optimizations. isOpaque() can be called at any time, unlike getFlags, |
| 104 | * which only works properly when the context is set. |
| 105 | */ |
| 106 | virtual bool isOpaque() const { return false; } |
| 107 | |
| 108 | /** |
reed@google.com | ad91799 | 2011-04-11 19:01:12 +0000 | [diff] [blame] | 109 | * Return the alpha associated with the data returned by shadeSpan16(). If |
| 110 | * kHasSpan16_Flag is not set, this value is meaningless. |
| 111 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 112 | virtual uint8_t getSpan16Alpha() const { return fPaintAlpha; } |
reed@google.com | 7c2f27d | 2011-03-07 19:29:00 +0000 | [diff] [blame] | 113 | |
reed@google.com | ad91799 | 2011-04-11 19:01:12 +0000 | [diff] [blame] | 114 | /** |
| 115 | * Called once before drawing, with the current paint and device matrix. |
| 116 | * Return true if your shader supports these parameters, or false if not. |
| 117 | * If false is returned, nothing will be drawn. |
| 118 | */ |
reed@android.com | 3c9b2a4 | 2009-08-27 19:28:37 +0000 | [diff] [blame] | 119 | virtual bool setContext(const SkBitmap& device, const SkPaint& paint, |
| 120 | const SkMatrix& matrix); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 121 | |
reed@google.com | ad91799 | 2011-04-11 19:01:12 +0000 | [diff] [blame] | 122 | /** |
| 123 | * Called for each span of the object being drawn. Your subclass should |
| 124 | * set the appropriate colors (with premultiplied alpha) that correspond |
| 125 | * to the specified device coordinates. |
| 126 | */ |
reed@android.com | 3c9b2a4 | 2009-08-27 19:28:37 +0000 | [diff] [blame] | 127 | virtual void shadeSpan(int x, int y, SkPMColor[], int count) = 0; |
reed@google.com | ad91799 | 2011-04-11 19:01:12 +0000 | [diff] [blame] | 128 | |
| 129 | /** |
| 130 | * Called only for 16bit devices when getFlags() returns |
| 131 | * kOpaqueAlphaFlag | kHasSpan16_Flag |
| 132 | */ |
reed@android.com | 3c9b2a4 | 2009-08-27 19:28:37 +0000 | [diff] [blame] | 133 | virtual void shadeSpan16(int x, int y, uint16_t[], int count); |
reed@google.com | ad91799 | 2011-04-11 19:01:12 +0000 | [diff] [blame] | 134 | |
| 135 | /** |
| 136 | * Similar to shadeSpan, but only returns the alpha-channel for a span. |
| 137 | * The default implementation calls shadeSpan() and then extracts the alpha |
| 138 | * values from the returned colors. |
| 139 | */ |
reed@android.com | 3c9b2a4 | 2009-08-27 19:28:37 +0000 | [diff] [blame] | 140 | virtual void shadeSpanAlpha(int x, int y, uint8_t alpha[], int count); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 141 | |
reed@google.com | ad91799 | 2011-04-11 19:01:12 +0000 | [diff] [blame] | 142 | /** |
| 143 | * Helper function that returns true if this shader's shadeSpan16() method |
| 144 | * can be called. |
| 145 | */ |
reed@android.com | 3c9b2a4 | 2009-08-27 19:28:37 +0000 | [diff] [blame] | 146 | bool canCallShadeSpan16() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 147 | return SkShader::CanCallShadeSpan16(this->getFlags()); |
| 148 | } |
| 149 | |
reed@google.com | ad91799 | 2011-04-11 19:01:12 +0000 | [diff] [blame] | 150 | /** |
| 151 | * Helper to check the flags to know if it is legal to call shadeSpan16() |
| 152 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 153 | static bool CanCallShadeSpan16(uint32_t flags) { |
| 154 | return (flags & kHasSpan16_Flag) != 0; |
| 155 | } |
| 156 | |
reed@google.com | ad91799 | 2011-04-11 19:01:12 +0000 | [diff] [blame] | 157 | /** |
| 158 | * Called before a session using the shader begins. Some shaders override |
| 159 | * this to defer some of their work (like calling bitmap.lockPixels()). |
| 160 | * Must be balanced by a call to endSession. |
| 161 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 162 | virtual void beginSession(); |
| 163 | virtual void endSession(); |
reed@google.com | 7c2f27d | 2011-03-07 19:29:00 +0000 | [diff] [blame] | 164 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 165 | /** |
| 166 | Gives method bitmap should be read to implement a shader. |
| 167 | Also determines number and interpretation of "extra" parameters returned |
| 168 | by asABitmap |
| 169 | */ |
| 170 | enum BitmapType { |
| 171 | kNone_BitmapType, //<! Shader is not represented as a bitmap |
reed@google.com | 7c2f27d | 2011-03-07 19:29:00 +0000 | [diff] [blame] | 172 | kDefault_BitmapType,//<! Access bitmap using local coords transformed |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 173 | // by matrix. No extras |
reed@google.com | 7c2f27d | 2011-03-07 19:29:00 +0000 | [diff] [blame] | 174 | kRadial_BitmapType, //<! Access bitmap by transforming local coordinates |
| 175 | // by the matrix and taking the distance of result |
| 176 | // from (0,0) as bitmap column. Bitmap is 1 pixel |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 177 | // tall. No extras |
reed@google.com | 7c2f27d | 2011-03-07 19:29:00 +0000 | [diff] [blame] | 178 | kSweep_BitmapType, //<! Access bitmap by transforming local coordinates |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 179 | // by the matrix and taking the angle of result |
| 180 | // to (0,0) as bitmap x coord, where angle = 0 is |
reed@google.com | 7c2f27d | 2011-03-07 19:29:00 +0000 | [diff] [blame] | 181 | // bitmap left edge of bitmap = 2pi is the |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 182 | // right edge. Bitmap is 1 pixel tall. No extras |
vandebo@chromium.org | d3ae779 | 2011-02-24 00:21:06 +0000 | [diff] [blame] | 183 | kTwoPointRadial_BitmapType, |
reed@google.com | 7c2f27d | 2011-03-07 19:29:00 +0000 | [diff] [blame] | 184 | //<! Matrix transforms to space where (0,0) is |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 185 | // the center of the starting circle. The second |
reed@google.com | 7c2f27d | 2011-03-07 19:29:00 +0000 | [diff] [blame] | 186 | // circle will be centered (x, 0) where x may be |
| 187 | // 0. The post-matrix space is normalized such |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 188 | // that 1 is the second radius - first radius. |
| 189 | // Three extra parameters are returned: |
reed@google.com | 7c2f27d | 2011-03-07 19:29:00 +0000 | [diff] [blame] | 190 | // 0: x-offset of second circle center |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 191 | // to first. |
reed@google.com | 7c2f27d | 2011-03-07 19:29:00 +0000 | [diff] [blame] | 192 | // 1: radius of first circle in post-matrix |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 193 | // space |
| 194 | // 2: the second radius minus the first radius |
reed@google.com | 7c2f27d | 2011-03-07 19:29:00 +0000 | [diff] [blame] | 195 | // in pre-transformed space. |
vandebo@chromium.org | d3ae779 | 2011-02-24 00:21:06 +0000 | [diff] [blame] | 196 | |
| 197 | kLast_BitmapType = kTwoPointRadial_BitmapType |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 198 | }; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 199 | /** Optional methods for shaders that can pretend to be a bitmap/texture |
reed@google.com | 7c2f27d | 2011-03-07 19:29:00 +0000 | [diff] [blame] | 200 | to play along with opengl. Default just returns kNone_BitmapType and |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 201 | ignores the out parameters. |
| 202 | |
| 203 | @param outTexture if non-NULL will be the bitmap representing the shader |
| 204 | after return. |
| 205 | @param outMatrix if non-NULL will be the matrix to apply to vertices |
| 206 | to access the bitmap after return. |
| 207 | @param xy if non-NULL will be the tile modes that should be |
| 208 | used to access the bitmap after return. |
| 209 | @param twoPointRadialParams Two extra return values needed for two point |
| 210 | radial bitmaps. The first is the x-offset of |
| 211 | the second point and the second is the radius |
| 212 | about the first point. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 213 | */ |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 214 | virtual BitmapType asABitmap(SkBitmap* outTexture, SkMatrix* outMatrix, |
reed@google.com | ad91799 | 2011-04-11 19:01:12 +0000 | [diff] [blame] | 215 | TileMode xy[2], SkScalar* twoPointRadialParams) const; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 216 | |
vandebo@chromium.org | d3ae779 | 2011-02-24 00:21:06 +0000 | [diff] [blame] | 217 | /** |
| 218 | * If the shader subclass can be represented as a gradient, asAGradient |
| 219 | * returns the matching GradientType enum (or kNone_GradientType if it |
| 220 | * cannot). Also, if info is not null, asAGradient populates info with |
| 221 | * the relevant (see below) parameters for the gradient. fColorCount |
| 222 | * is both an input and output parameter. On input, it indicates how |
| 223 | * many entries in fColors and fColorOffsets can be used, if they are |
| 224 | * non-NULL. After asAGradient has run, fColorCount indicates how |
| 225 | * many color-offset pairs there are in the gradient. If there is |
| 226 | * insufficient space to store all of the color-offset pairs, fColors |
| 227 | * and fColorOffsets will not be altered. fColorOffsets specifies |
| 228 | * where on the range of 0 to 1 to transition to the given color. |
| 229 | * The meaning of fPoint and fRadius is dependant on the type of gradient. |
| 230 | * |
| 231 | * None: |
| 232 | * info is ignored. |
| 233 | * Color: |
| 234 | * fColorOffsets[0] is meaningless. |
| 235 | * Linear: |
| 236 | * fPoint[0] and fPoint[1] are the end-points of the gradient |
| 237 | * Radial: |
| 238 | * fPoint[0] and fRadius[0] are the center and radius |
| 239 | * Radial2: |
| 240 | * fPoint[0] and fRadius[0] are the center and radius of the 1st circle |
| 241 | * fPoint[1] and fRadius[1] are the center and radius of the 2nd circle |
| 242 | * Sweep: |
| 243 | * fPoint[0] is the center of the sweep. |
| 244 | */ |
| 245 | |
| 246 | enum GradientType { |
| 247 | kNone_GradientType, |
| 248 | kColor_GradientType, |
| 249 | kLinear_GradientType, |
| 250 | kRadial_GradientType, |
| 251 | kRadial2_GradientType, |
| 252 | kSweep_GradientType, |
| 253 | kLast_GradientType = kSweep_GradientType |
| 254 | }; |
| 255 | |
| 256 | struct GradientInfo { |
| 257 | int fColorCount; //!< In-out parameter, specifies passed size |
| 258 | // of fColors/fColorOffsets on input, and |
| 259 | // actual number of colors/offsets on |
| 260 | // output. |
| 261 | SkColor* fColors; //!< The colors in the gradient. |
| 262 | SkScalar* fColorOffsets; //!< The unit offset for color transitions. |
| 263 | SkPoint fPoint[2]; //!< Type specific, see above. |
| 264 | SkScalar fRadius[2]; //!< Type specific, see above. |
| 265 | TileMode fTileMode; //!< The tile mode used. |
| 266 | }; |
| 267 | |
| 268 | virtual GradientType asAGradient(GradientInfo* info) const; |
| 269 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 270 | ////////////////////////////////////////////////////////////////////////// |
| 271 | // Factory methods for stock shaders |
| 272 | |
| 273 | /** Call this to create a new shader that will draw with the specified bitmap. |
| 274 | @param src The bitmap to use inside the shader |
| 275 | @param tmx The tiling mode to use when sampling the bitmap in the x-direction. |
| 276 | @param tmy The tiling mode to use when sampling the bitmap in the y-direction. |
| 277 | @return Returns a new shader object. Note: this function never returns null. |
| 278 | */ |
| 279 | static SkShader* CreateBitmapShader(const SkBitmap& src, |
| 280 | TileMode tmx, TileMode tmy); |
| 281 | |
tomhudson@google.com | 1341304 | 2011-10-03 16:01:10 +0000 | [diff] [blame] | 282 | virtual void flatten(SkFlattenableWriteBuffer& ) SK_OVERRIDE; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 283 | protected: |
| 284 | enum MatrixClass { |
| 285 | kLinear_MatrixClass, // no perspective |
| 286 | kFixedStepInX_MatrixClass, // fast perspective, need to call fixedStepInX() each scanline |
| 287 | kPerspective_MatrixClass // slow perspective, need to mappoints each pixel |
| 288 | }; |
| 289 | static MatrixClass ComputeMatrixClass(const SkMatrix&); |
| 290 | |
| 291 | // These can be called by your subclass after setContext() has been called |
| 292 | uint8_t getPaintAlpha() const { return fPaintAlpha; } |
| 293 | SkBitmap::Config getDeviceConfig() const { return (SkBitmap::Config)fDeviceConfig; } |
| 294 | const SkMatrix& getTotalInverse() const { return fTotalInverse; } |
| 295 | MatrixClass getInverseClass() const { return (MatrixClass)fTotalInverseClass; } |
| 296 | |
| 297 | SkShader(SkFlattenableReadBuffer& ); |
| 298 | private: |
| 299 | SkMatrix* fLocalMatrix; |
| 300 | SkMatrix fTotalInverse; |
| 301 | uint8_t fPaintAlpha; |
| 302 | uint8_t fDeviceConfig; |
| 303 | uint8_t fTotalInverseClass; |
| 304 | SkDEBUGCODE(SkBool8 fInSession;) |
| 305 | |
| 306 | static SkShader* CreateBitmapShader(const SkBitmap& src, |
| 307 | TileMode, TileMode, |
| 308 | void* storage, size_t storageSize); |
| 309 | friend class SkAutoBitmapShaderInstall; |
| 310 | typedef SkFlattenable INHERITED; |
| 311 | }; |
| 312 | |
| 313 | #endif |
| 314 | |