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