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