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