blob: e67167d924cfa02f2ad87c6ce6ad533febefe553 [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00003 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00004 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00006 */
7
epoger@google.comec3ed6a2011-07-28 14:26:00 +00008
reed@android.com8a1c16f2008-12-17 15:59:43 +00009#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
18class SkPath;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000019class SkPicture;
commit-bot@chromium.org79590552014-05-13 18:14:45 +000020class SkXfermode;
rileya@google.com03c1c352012-07-20 20:02:43 +000021class GrContext;
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000022class GrEffectRef;
reed@android.com8a1c16f2008-12-17 15:59:43 +000023
24/** \class SkShader
reed@google.comad917992011-04-11 19:01:12 +000025 *
reed@google.com880dc472012-05-11 14:47:03 +000026 * 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.comad917992011-04-11 19:01:12 +000033 */
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +000034class SK_API SkShader : public SkFlattenable {
reed@android.com8a1c16f2008-12-17 15:59:43 +000035public:
robertphillips@google.com0456e0b2012-06-27 14:03:26 +000036 SK_DECLARE_INST_COUNT(SkShader)
37
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +000038 SkShader(const SkMatrix* localMatrix = NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +000039 virtual ~SkShader();
40
reed@google.comad917992011-04-11 19:01:12 +000041 /**
commit-bot@chromium.orgd12de022014-05-09 15:42:07 +000042 * Returns the local matrix.
43 */
44 const SkMatrix& getLocalMatrix() const { return fLocalMatrix; }
45
commit-bot@chromium.org5970f622014-05-12 20:42:21 +000046#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.orgd12de022014-05-09 15:42:07 +000052 /**
reed@google.comad917992011-04-11 19:01:12 +000053 * Set the shader's local matrix.
54 * @param localM The shader's new local matrix.
55 */
bsalomon@google.comf94b3a42012-10-31 18:09:01 +000056 void setLocalMatrix(const SkMatrix& localM) { fLocalMatrix = localM; }
reed@google.comad917992011-04-11 19:01:12 +000057
58 /**
59 * Reset the shader's local matrix to identity.
60 */
bsalomon@google.comf94b3a42012-10-31 18:09:01 +000061 void resetLocalMatrix() { fLocalMatrix.reset(); }
commit-bot@chromium.org5970f622014-05-12 20:42:21 +000062#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +000063
64 enum TileMode {
reed@google.com0beaba52012-03-16 14:38:06 +000065 /** 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.com8a1c16f2008-12-17 15:59:43 +000082
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.com3c9b2a42009-08-27 19:28:37 +000090 kOpaqueAlpha_Flag = 0x01,
reed@android.com5119bdb2009-06-12 21:27:03 +000091
reed@android.com8a1c16f2008-12-17 15:59:43 +000092 //! set if this shader's shadeSpan16() method can be called
reed@android.com3c9b2a42009-08-27 19:28:37 +000093 kHasSpan16_Flag = 0x02,
reed@android.com5119bdb2009-06-12 21:27:03 +000094
reed@android.com8a1c16f2008-12-17 15:59:43 +000095 /** 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.com5119bdb2009-06-12 21:27:03 +0000100 kIntrinsicly16_Flag = 0x04,
101
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000102 /** set if the spans only vary in X (const in Y).
reed@android.com5119bdb2009-06-12 21:27:03 +0000103 e.g. an Nx1 bitmap that is being tiled in Y, or a linear-gradient
reed@android.com3c9b2a42009-08-27 19:28:37 +0000104 that varies from left-to-right. This flag specifies this for
105 shadeSpan().
reed@android.com5119bdb2009-06-12 21:27:03 +0000106 */
reed@android.com3c9b2a42009-08-27 19:28:37 +0000107 kConstInY32_Flag = 0x08,
reed@google.com7c2f27d2011-03-07 19:29:00 +0000108
reed@android.com3c9b2a42009-08-27 19:28:37 +0000109 /** 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.com8a1c16f2008-12-17 15:59:43 +0000115 };
116
reed@google.comad917992011-04-11 19:01:12 +0000117 /**
junov@chromium.orgb6e16192011-12-09 15:48:03 +0000118 * 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.org87fcd952014-04-23 19:10:51 +0000121 * optimizations.
junov@chromium.orgb6e16192011-12-09 15:48:03 +0000122 */
123 virtual bool isOpaque() const { return false; }
124
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000125 /**
126 * ContextRec acts as a parameter bundle for creating Contexts.
127 */
128 struct ContextRec {
commit-bot@chromium.org80116dc2014-05-06 17:16:03 +0000129 ContextRec() : fDevice(NULL), fPaint(NULL), fMatrix(NULL), fLocalMatrix(NULL) {}
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000130 ContextRec(const SkBitmap& device, const SkPaint& paint, const SkMatrix& matrix)
131 : fDevice(&device)
132 , fPaint(&paint)
commit-bot@chromium.org80116dc2014-05-06 17:16:03 +0000133 , fMatrix(&matrix)
134 , fLocalMatrix(NULL) {}
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000135
commit-bot@chromium.org80116dc2014-05-06 17:16:03 +0000136 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.orge901b6d2014-05-01 19:31:31 +0000140 };
141
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000142 class Context : public ::SkNoncopyable {
143 public:
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000144 Context(const SkShader& shader, const ContextRec&);
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000145
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.org80116dc2014-05-06 17:16:03 +0000206 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.org87fcd952014-04-23 19:10:51 +0000210 private:
commit-bot@chromium.org80116dc2014-05-06 17:16:03 +0000211 SkMatrix fCTM;
212 SkMatrix fTotalInverse;
213 uint8_t fPaintAlpha;
214 uint8_t fTotalInverseClass;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000215
216 typedef SkNoncopyable INHERITED;
217 };
reed@google.com7c2f27d2011-03-07 19:29:00 +0000218
reed@google.comad917992011-04-11 19:01:12 +0000219 /**
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000220 * Create the actual object that does the shading.
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000221 * Size of storage must be >= contextSize.
reed@google.coma641f3f2012-12-13 22:16:30 +0000222 */
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000223 Context* createContext(const ContextRec&, void* storage) const;
reed@google.coma641f3f2012-12-13 22:16:30 +0000224
225 /**
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000226 * Return the size of a Context returned by createContext.
commit-bot@chromium.orgf3e50592014-04-30 23:29:02 +0000227 *
228 * Override this if your subclass overrides createContext, to return the correct size of
229 * your subclass' context.
reed@google.comad917992011-04-11 19:01:12 +0000230 */
commit-bot@chromium.orgf3e50592014-04-30 23:29:02 +0000231 virtual size_t contextSize() const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000232
reed@google.comad917992011-04-11 19:01:12 +0000233 /**
234 * Helper to check the flags to know if it is legal to call shadeSpan16()
235 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000236 static bool CanCallShadeSpan16(uint32_t flags) {
237 return (flags & kHasSpan16_Flag) != 0;
238 }
239
reed@google.comad917992011-04-11 19:01:12 +0000240 /**
reed@android.comf2b98d62010-12-20 18:26:13 +0000241 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.com7c2f27d2011-03-07 19:29:00 +0000247 kDefault_BitmapType,//<! Access bitmap using local coords transformed
reed@android.comf2b98d62010-12-20 18:26:13 +0000248 // by matrix. No extras
reed@google.com7c2f27d2011-03-07 19:29:00 +0000249 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.comf2b98d62010-12-20 18:26:13 +0000252 // tall. No extras
reed@google.com7c2f27d2011-03-07 19:29:00 +0000253 kSweep_BitmapType, //<! Access bitmap by transforming local coordinates
reed@android.comf2b98d62010-12-20 18:26:13 +0000254 // by the matrix and taking the angle of result
255 // to (0,0) as bitmap x coord, where angle = 0 is
reed@google.com7c2f27d2011-03-07 19:29:00 +0000256 // bitmap left edge of bitmap = 2pi is the
reed@android.comf2b98d62010-12-20 18:26:13 +0000257 // right edge. Bitmap is 1 pixel tall. No extras
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000258 kTwoPointRadial_BitmapType,
reed@google.com7c2f27d2011-03-07 19:29:00 +0000259 //<! Matrix transforms to space where (0,0) is
reed@android.comf2b98d62010-12-20 18:26:13 +0000260 // the center of the starting circle. The second
reed@google.com7c2f27d2011-03-07 19:29:00 +0000261 // circle will be centered (x, 0) where x may be
262 // 0. The post-matrix space is normalized such
reed@android.comf2b98d62010-12-20 18:26:13 +0000263 // that 1 is the second radius - first radius.
264 // Three extra parameters are returned:
reed@google.com7c2f27d2011-03-07 19:29:00 +0000265 // 0: x-offset of second circle center
reed@android.comf2b98d62010-12-20 18:26:13 +0000266 // to first.
reed@google.com7c2f27d2011-03-07 19:29:00 +0000267 // 1: radius of first circle in post-matrix
reed@android.comf2b98d62010-12-20 18:26:13 +0000268 // space
269 // 2: the second radius minus the first radius
reed@google.com7c2f27d2011-03-07 19:29:00 +0000270 // in pre-transformed space.
rileya@google.com3e332582012-07-03 13:43:35 +0000271 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.com22e57f92012-07-19 15:16:19 +0000281 kLinear_BitmapType, //<! Access bitmap using local coords transformed
282 // by matrix. No extras
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000283
rileya@google.com22e57f92012-07-19 15:16:19 +0000284 kLast_BitmapType = kLinear_BitmapType
reed@android.comf2b98d62010-12-20 18:26:13 +0000285 };
reed@android.com8a1c16f2008-12-17 15:59:43 +0000286 /** Optional methods for shaders that can pretend to be a bitmap/texture
reed@google.com7c2f27d2011-03-07 19:29:00 +0000287 to play along with opengl. Default just returns kNone_BitmapType and
reed@android.comf2b98d62010-12-20 18:26:13 +0000288 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.com8a1c16f2008-12-17 15:59:43 +0000300 */
reed@android.comf2b98d62010-12-20 18:26:13 +0000301 virtual BitmapType asABitmap(SkBitmap* outTexture, SkMatrix* outMatrix,
rileya@google.com91f319c2012-07-25 17:18:31 +0000302 TileMode xy[2]) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000303
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000304 /**
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.com83226972012-06-07 20:26:47 +0000340 kConical_GradientType,
341 kLast_GradientType = kConical_GradientType
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000342 };
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.com3d3a8602013-05-24 14:58:44 +0000354 uint32_t fGradientFlags; //!< see SkGradientShader::Flags
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000355 };
356
357 virtual GradientType asAGradient(GradientInfo* info) const;
358
rileya@google.com03c1c352012-07-20 20:02:43 +0000359 /**
commit-bot@chromium.org79590552014-05-13 18:14:45 +0000360 * 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.
commit-bot@chromium.org30558792014-05-14 14:28:34 +0000362 *
363 * These are bare pointers; the ownership and reference count are unchanged.
commit-bot@chromium.org79590552014-05-13 18:14:45 +0000364 */
365
366 struct ComposeRec {
367 const SkShader* fShaderA;
368 const SkShader* fShaderB;
369 const SkXfermode* fMode;
370 };
371
372 virtual bool asACompose(ComposeRec* rec) const { return false; }
373
374
375 /**
commit-bot@chromium.org91a798f2013-09-06 15:31:06 +0000376 * If the shader subclass has a GrEffect implementation, this resturns the effect to install.
377 * The incoming color to the effect has r=g=b=a all extracted from the SkPaint's alpha.
378 * The output color should be the computed SkShader premul color modulated by the incoming
379 * color. The GrContext may be used by the effect to create textures. The GPU device does not
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000380 * call createContext. Instead we pass the SkPaint here in case the shader needs paint info.
rileya@google.com03c1c352012-07-20 20:02:43 +0000381 */
commit-bot@chromium.org96fb7482014-05-09 20:28:11 +0000382 virtual GrEffectRef* asNewEffect(GrContext* context, const SkPaint& paint,
383 const SkMatrix* localMatrixOrNull) const;
rileya@google.com03c1c352012-07-20 20:02:43 +0000384
commit-bot@chromium.org79590552014-05-13 18:14:45 +0000385#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
386 /**
387 * If the shader is a custom shader which has data the caller might want, call this function
388 * to get that data.
389 */
390 virtual bool asACustomShader(void** customData) const { return false; }
391#endif
392
reed@android.com8a1c16f2008-12-17 15:59:43 +0000393 //////////////////////////////////////////////////////////////////////////
394 // Factory methods for stock shaders
395
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000396 /**
397 * Call this to create a new "empty" shader, that will not draw anything.
398 */
399 static SkShader* CreateEmptyShader();
400
reed@android.com8a1c16f2008-12-17 15:59:43 +0000401 /** Call this to create a new shader that will draw with the specified bitmap.
reed@google.com99c114e2012-05-03 20:14:26 +0000402 *
403 * If the bitmap cannot be used (e.g. has no pixels, or its dimensions
404 * exceed implementation limits (currently at 64K - 1)) then SkEmptyShader
405 * may be returned.
406 *
commit-bot@chromium.org91246b92013-12-05 15:43:19 +0000407 * If the src is kA8_Config then that mask will be colorized using the color on
408 * the paint.
409 *
reed@google.com99c114e2012-05-03 20:14:26 +0000410 * @param src The bitmap to use inside the shader
411 * @param tmx The tiling mode to use when sampling the bitmap in the x-direction.
412 * @param tmy The tiling mode to use when sampling the bitmap in the y-direction.
413 * @return Returns a new shader object. Note: this function never returns null.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000414 */
415 static SkShader* CreateBitmapShader(const SkBitmap& src,
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +0000416 TileMode tmx, TileMode tmy,
417 const SkMatrix* localMatrix = NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000418
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000419 /** Call this to create a new shader that will draw with the specified picture.
420 *
421 * @param src The picture to use inside the shader (if not NULL, its ref count
commit-bot@chromium.org855e88e2014-04-21 19:33:12 +0000422 * is incremented). The SkPicture must not be changed after
423 * successfully creating a picture shader.
424 * FIXME: src cannot be const due to SkCanvas::drawPicture
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000425 * @param tmx The tiling mode to use when sampling the bitmap in the x-direction.
426 * @param tmy The tiling mode to use when sampling the bitmap in the y-direction.
427 * @return Returns a new shader object. Note: this function never returns null.
428 */
commit-bot@chromium.org5aacfe92014-05-02 21:23:52 +0000429 static SkShader* CreatePictureShader(SkPicture* src, TileMode tmx, TileMode tmy,
430 const SkMatrix* localMatrix = NULL);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000431
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +0000432 /**
433 * Return a shader that will apply the specified localMatrix to the proxy shader.
434 * The specified matrix will be applied before any matrix associated with the proxy.
435 *
436 * Note: ownership of the proxy is not transferred (though a ref is taken).
437 */
438 static SkShader* CreateLocalMatrixShader(SkShader* proxy, const SkMatrix& localMatrix);
439
440 /**
441 * If this shader can be represented by another shader + a localMatrix, return that shader
442 * and, if not NULL, the localMatrix. If not, return NULL and ignore the localMatrix parameter.
443 *
444 * Note: the returned shader (if not NULL) will have been ref'd, and it is the responsibility
445 * of the caller to balance that with unref() when they are done.
446 */
447 virtual SkShader* refAsALocalMatrixShader(SkMatrix* localMatrix) const;
448
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000449 SK_TO_STRING_VIRT()
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +0000450 SK_DEFINE_FLATTENABLE_TYPE(SkShader)
451
reed@android.com8a1c16f2008-12-17 15:59:43 +0000452protected:
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000453 SkShader(SkReadBuffer& );
454 virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000455
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000456 bool computeTotalInverse(const ContextRec&, SkMatrix* totalInverse) const;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000457
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000458 /**
459 * Your subclass must also override contextSize() if it overrides onCreateContext().
460 * Base class impl returns NULL.
461 */
462 virtual Context* onCreateContext(const ContextRec&, void* storage) const;
463
464private:
465 SkMatrix fLocalMatrix;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000466
reed@android.com8a1c16f2008-12-17 15:59:43 +0000467 typedef SkFlattenable INHERITED;
468};
469
470#endif