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