blob: 8871f1b02131daac82f4aaf43d52721c532dc846 [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"
dandov9de5b512014-06-10 14:38:28 -070017#include "../gpu/GrColor.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000018
19class SkPath;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000020class SkPicture;
commit-bot@chromium.org79590552014-05-13 18:14:45 +000021class SkXfermode;
rileya@google.com03c1c352012-07-20 20:02:43 +000022class GrContext;
bsalomon97b9ab72014-07-08 06:52:35 -070023class GrEffect;
reed@android.com8a1c16f2008-12-17 15:59:43 +000024
25/** \class SkShader
reed@google.comad917992011-04-11 19:01:12 +000026 *
reed@google.com880dc472012-05-11 14:47:03 +000027 * 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.comad917992011-04-11 19:01:12 +000034 */
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +000035class SK_API SkShader : public SkFlattenable {
reed@android.com8a1c16f2008-12-17 15:59:43 +000036public:
robertphillips@google.com0456e0b2012-06-27 14:03:26 +000037 SK_DECLARE_INST_COUNT(SkShader)
38
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +000039 SkShader(const SkMatrix* localMatrix = NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +000040 virtual ~SkShader();
41
reed@google.comad917992011-04-11 19:01:12 +000042 /**
commit-bot@chromium.orgd12de022014-05-09 15:42:07 +000043 * Returns the local matrix.
44 */
45 const SkMatrix& getLocalMatrix() const { return fLocalMatrix; }
46
commit-bot@chromium.org5970f622014-05-12 20:42:21 +000047 /**
48 * Returns true if the local matrix is not an identity matrix.
49 */
50 bool hasLocalMatrix() const { return !fLocalMatrix.isIdentity(); }
51
commit-bot@chromium.orgde5553a2014-05-22 18:43:05 +000052#ifdef SK_SUPPORT_LEGACY_SHADER_LOCALMATRIX
commit-bot@chromium.orgd12de022014-05-09 15:42:07 +000053 /**
reed@google.comad917992011-04-11 19:01:12 +000054 * Set the shader's local matrix.
55 * @param localM The shader's new local matrix.
56 */
bsalomon@google.comf94b3a42012-10-31 18:09:01 +000057 void setLocalMatrix(const SkMatrix& localM) { fLocalMatrix = localM; }
reed@google.comad917992011-04-11 19:01:12 +000058
59 /**
60 * Reset the shader's local matrix to identity.
61 */
bsalomon@google.comf94b3a42012-10-31 18:09:01 +000062 void resetLocalMatrix() { fLocalMatrix.reset(); }
commit-bot@chromium.org5970f622014-05-12 20:42:21 +000063#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +000064
65 enum TileMode {
reed@google.com0beaba52012-03-16 14:38:06 +000066 /** replicate the edge color if the shader draws outside of its
67 * original bounds
68 */
69 kClamp_TileMode,
70
71 /** repeat the shader's image horizontally and vertically */
72 kRepeat_TileMode,
73
74 /** repeat the shader's image horizontally and vertically, alternating
75 * mirror images so that adjacent images always seam
76 */
77 kMirror_TileMode,
78
79#if 0
80 /** only draw within the original domain, return 0 everywhere else */
81 kDecal_TileMode,
82#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +000083
84 kTileModeCount
85 };
86
87 // override these in your subclass
88
89 enum Flags {
90 //!< set if all of the colors will be opaque
reed@android.com3c9b2a42009-08-27 19:28:37 +000091 kOpaqueAlpha_Flag = 0x01,
reed@android.com5119bdb2009-06-12 21:27:03 +000092
reed@android.com8a1c16f2008-12-17 15:59:43 +000093 //! set if this shader's shadeSpan16() method can be called
reed@android.com3c9b2a42009-08-27 19:28:37 +000094 kHasSpan16_Flag = 0x02,
reed@android.com5119bdb2009-06-12 21:27:03 +000095
reed@android.com8a1c16f2008-12-17 15:59:43 +000096 /** Set this bit if the shader's native data type is instrinsically 16
97 bit, meaning that calling the 32bit shadeSpan() entry point will
98 mean the the impl has to up-sample 16bit data into 32bit. Used as a
99 a means of clearing a dither request if the it will have no effect
100 */
reed@android.com5119bdb2009-06-12 21:27:03 +0000101 kIntrinsicly16_Flag = 0x04,
102
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000103 /** set if the spans only vary in X (const in Y).
reed@android.com5119bdb2009-06-12 21:27:03 +0000104 e.g. an Nx1 bitmap that is being tiled in Y, or a linear-gradient
reed@android.com3c9b2a42009-08-27 19:28:37 +0000105 that varies from left-to-right. This flag specifies this for
106 shadeSpan().
reed@android.com5119bdb2009-06-12 21:27:03 +0000107 */
reed@android.com3c9b2a42009-08-27 19:28:37 +0000108 kConstInY32_Flag = 0x08,
reed@google.com7c2f27d2011-03-07 19:29:00 +0000109
reed@android.com3c9b2a42009-08-27 19:28:37 +0000110 /** same as kConstInY32_Flag, but is set if this is true for shadeSpan16
111 which may not always be the case, since shadeSpan16 may be
112 predithered, which would mean it was not const in Y, even though
113 the 32bit shadeSpan() would be const.
114 */
115 kConstInY16_Flag = 0x10
reed@android.com8a1c16f2008-12-17 15:59:43 +0000116 };
117
reed@google.comad917992011-04-11 19:01:12 +0000118 /**
junov@chromium.orgb6e16192011-12-09 15:48:03 +0000119 * Returns true if the shader is guaranteed to produce only opaque
120 * colors, subject to the SkPaint using the shader to apply an opaque
121 * alpha value. Subclasses should override this to allow some
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000122 * optimizations.
junov@chromium.orgb6e16192011-12-09 15:48:03 +0000123 */
124 virtual bool isOpaque() const { return false; }
125
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000126 /**
127 * ContextRec acts as a parameter bundle for creating Contexts.
128 */
129 struct ContextRec {
commit-bot@chromium.org80116dc2014-05-06 17:16:03 +0000130 ContextRec() : fDevice(NULL), fPaint(NULL), fMatrix(NULL), fLocalMatrix(NULL) {}
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000131 ContextRec(const SkBitmap& device, const SkPaint& paint, const SkMatrix& matrix)
132 : fDevice(&device)
133 , fPaint(&paint)
commit-bot@chromium.org80116dc2014-05-06 17:16:03 +0000134 , fMatrix(&matrix)
135 , fLocalMatrix(NULL) {}
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000136
commit-bot@chromium.org80116dc2014-05-06 17:16:03 +0000137 const SkBitmap* fDevice; // the bitmap we are drawing into
138 const SkPaint* fPaint; // the current paint associated with the draw
139 const SkMatrix* fMatrix; // the current matrix in the canvas
140 const SkMatrix* fLocalMatrix; // optional local matrix
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000141 };
142
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000143 class Context : public ::SkNoncopyable {
144 public:
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000145 Context(const SkShader& shader, const ContextRec&);
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000146
147 virtual ~Context();
148
149 /**
150 * Called sometimes before drawing with this shader. Return the type of
151 * alpha your shader will return. The default implementation returns 0.
152 * Your subclass should override if it can (even sometimes) report a
153 * non-zero value, since that will enable various blitters to perform
154 * faster.
155 */
156 virtual uint32_t getFlags() const { return 0; }
157
158 /**
159 * Return the alpha associated with the data returned by shadeSpan16(). If
160 * kHasSpan16_Flag is not set, this value is meaningless.
161 */
162 virtual uint8_t getSpan16Alpha() const { return fPaintAlpha; }
163
164 /**
165 * Called for each span of the object being drawn. Your subclass should
166 * set the appropriate colors (with premultiplied alpha) that correspond
167 * to the specified device coordinates.
168 */
169 virtual void shadeSpan(int x, int y, SkPMColor[], int count) = 0;
170
171 typedef void (*ShadeProc)(void* ctx, int x, int y, SkPMColor[], int count);
172 virtual ShadeProc asAShadeProc(void** ctx);
173
174 /**
175 * Called only for 16bit devices when getFlags() returns
176 * kOpaqueAlphaFlag | kHasSpan16_Flag
177 */
178 virtual void shadeSpan16(int x, int y, uint16_t[], int count);
179
180 /**
181 * Similar to shadeSpan, but only returns the alpha-channel for a span.
182 * The default implementation calls shadeSpan() and then extracts the alpha
183 * values from the returned colors.
184 */
185 virtual void shadeSpanAlpha(int x, int y, uint8_t alpha[], int count);
186
187 /**
188 * Helper function that returns true if this shader's shadeSpan16() method
189 * can be called.
190 */
191 bool canCallShadeSpan16() {
192 return SkShader::CanCallShadeSpan16(this->getFlags());
193 }
194
195 protected:
196 // Reference to shader, so we don't have to dupe information.
197 const SkShader& fShader;
198
199 enum MatrixClass {
200 kLinear_MatrixClass, // no perspective
201 kFixedStepInX_MatrixClass, // fast perspective, need to call fixedStepInX() each
202 // scanline
203 kPerspective_MatrixClass // slow perspective, need to mappoints each pixel
204 };
205 static MatrixClass ComputeMatrixClass(const SkMatrix&);
206
commit-bot@chromium.org80116dc2014-05-06 17:16:03 +0000207 uint8_t getPaintAlpha() const { return fPaintAlpha; }
208 const SkMatrix& getTotalInverse() const { return fTotalInverse; }
209 MatrixClass getInverseClass() const { return (MatrixClass)fTotalInverseClass; }
210 const SkMatrix& getCTM() const { return fCTM; }
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000211 private:
commit-bot@chromium.org80116dc2014-05-06 17:16:03 +0000212 SkMatrix fCTM;
213 SkMatrix fTotalInverse;
214 uint8_t fPaintAlpha;
215 uint8_t fTotalInverseClass;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000216
217 typedef SkNoncopyable INHERITED;
218 };
reed@google.com7c2f27d2011-03-07 19:29:00 +0000219
reed@google.comad917992011-04-11 19:01:12 +0000220 /**
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000221 * Create the actual object that does the shading.
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000222 * Size of storage must be >= contextSize.
reed@google.coma641f3f2012-12-13 22:16:30 +0000223 */
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000224 Context* createContext(const ContextRec&, void* storage) const;
reed@google.coma641f3f2012-12-13 22:16:30 +0000225
226 /**
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000227 * Return the size of a Context returned by createContext.
commit-bot@chromium.orgf3e50592014-04-30 23:29:02 +0000228 *
229 * Override this if your subclass overrides createContext, to return the correct size of
230 * your subclass' context.
reed@google.comad917992011-04-11 19:01:12 +0000231 */
commit-bot@chromium.orgf3e50592014-04-30 23:29:02 +0000232 virtual size_t contextSize() const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000233
reed@google.comad917992011-04-11 19:01:12 +0000234 /**
235 * Helper to check the flags to know if it is legal to call shadeSpan16()
236 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000237 static bool CanCallShadeSpan16(uint32_t flags) {
238 return (flags & kHasSpan16_Flag) != 0;
239 }
240
reed@google.comad917992011-04-11 19:01:12 +0000241 /**
reed@android.comf2b98d62010-12-20 18:26:13 +0000242 Gives method bitmap should be read to implement a shader.
243 Also determines number and interpretation of "extra" parameters returned
244 by asABitmap
245 */
246 enum BitmapType {
247 kNone_BitmapType, //<! Shader is not represented as a bitmap
reed@google.com7c2f27d2011-03-07 19:29:00 +0000248 kDefault_BitmapType,//<! Access bitmap using local coords transformed
reed@android.comf2b98d62010-12-20 18:26:13 +0000249 // by matrix. No extras
reed@google.com7c2f27d2011-03-07 19:29:00 +0000250 kRadial_BitmapType, //<! Access bitmap by transforming local coordinates
251 // by the matrix and taking the distance of result
252 // from (0,0) as bitmap column. Bitmap is 1 pixel
reed@android.comf2b98d62010-12-20 18:26:13 +0000253 // tall. No extras
reed@google.com7c2f27d2011-03-07 19:29:00 +0000254 kSweep_BitmapType, //<! Access bitmap by transforming local coordinates
reed@android.comf2b98d62010-12-20 18:26:13 +0000255 // by the matrix and taking the angle of result
256 // to (0,0) as bitmap x coord, where angle = 0 is
reed@google.com7c2f27d2011-03-07 19:29:00 +0000257 // bitmap left edge of bitmap = 2pi is the
reed@android.comf2b98d62010-12-20 18:26:13 +0000258 // right edge. Bitmap is 1 pixel tall. No extras
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000259 kTwoPointRadial_BitmapType,
reed@google.com7c2f27d2011-03-07 19:29:00 +0000260 //<! Matrix transforms to space where (0,0) is
reed@android.comf2b98d62010-12-20 18:26:13 +0000261 // the center of the starting circle. The second
reed@google.com7c2f27d2011-03-07 19:29:00 +0000262 // circle will be centered (x, 0) where x may be
263 // 0. The post-matrix space is normalized such
reed@android.comf2b98d62010-12-20 18:26:13 +0000264 // that 1 is the second radius - first radius.
265 // Three extra parameters are returned:
reed@google.com7c2f27d2011-03-07 19:29:00 +0000266 // 0: x-offset of second circle center
reed@android.comf2b98d62010-12-20 18:26:13 +0000267 // to first.
reed@google.com7c2f27d2011-03-07 19:29:00 +0000268 // 1: radius of first circle in post-matrix
reed@android.comf2b98d62010-12-20 18:26:13 +0000269 // space
270 // 2: the second radius minus the first radius
reed@google.com7c2f27d2011-03-07 19:29:00 +0000271 // in pre-transformed space.
rileya@google.com3e332582012-07-03 13:43:35 +0000272 kTwoPointConical_BitmapType,
273 //<! Matrix transforms to space where (0,0) is
274 // the center of the starting circle. The second
275 // circle will be centered (x, 0) where x may be
276 // 0.
277 // Three extra parameters are returned:
278 // 0: x-offset of second circle center
279 // to first.
280 // 1: radius of first circle
281 // 2: the second radius minus the first radius
rileya@google.com22e57f92012-07-19 15:16:19 +0000282 kLinear_BitmapType, //<! Access bitmap using local coords transformed
283 // by matrix. No extras
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000284
rileya@google.com22e57f92012-07-19 15:16:19 +0000285 kLast_BitmapType = kLinear_BitmapType
reed@android.comf2b98d62010-12-20 18:26:13 +0000286 };
reed@android.com8a1c16f2008-12-17 15:59:43 +0000287 /** Optional methods for shaders that can pretend to be a bitmap/texture
reed@google.com7c2f27d2011-03-07 19:29:00 +0000288 to play along with opengl. Default just returns kNone_BitmapType and
reed@android.comf2b98d62010-12-20 18:26:13 +0000289 ignores the out parameters.
290
291 @param outTexture if non-NULL will be the bitmap representing the shader
292 after return.
293 @param outMatrix if non-NULL will be the matrix to apply to vertices
294 to access the bitmap after return.
295 @param xy if non-NULL will be the tile modes that should be
296 used to access the bitmap after return.
297 @param twoPointRadialParams Two extra return values needed for two point
298 radial bitmaps. The first is the x-offset of
299 the second point and the second is the radius
300 about the first point.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000301 */
reed@android.comf2b98d62010-12-20 18:26:13 +0000302 virtual BitmapType asABitmap(SkBitmap* outTexture, SkMatrix* outMatrix,
rileya@google.com91f319c2012-07-25 17:18:31 +0000303 TileMode xy[2]) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000304
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000305 /**
306 * If the shader subclass can be represented as a gradient, asAGradient
307 * returns the matching GradientType enum (or kNone_GradientType if it
308 * cannot). Also, if info is not null, asAGradient populates info with
309 * the relevant (see below) parameters for the gradient. fColorCount
310 * is both an input and output parameter. On input, it indicates how
311 * many entries in fColors and fColorOffsets can be used, if they are
312 * non-NULL. After asAGradient has run, fColorCount indicates how
313 * many color-offset pairs there are in the gradient. If there is
314 * insufficient space to store all of the color-offset pairs, fColors
315 * and fColorOffsets will not be altered. fColorOffsets specifies
316 * where on the range of 0 to 1 to transition to the given color.
317 * The meaning of fPoint and fRadius is dependant on the type of gradient.
318 *
319 * None:
320 * info is ignored.
321 * Color:
322 * fColorOffsets[0] is meaningless.
323 * Linear:
324 * fPoint[0] and fPoint[1] are the end-points of the gradient
325 * Radial:
326 * fPoint[0] and fRadius[0] are the center and radius
327 * Radial2:
328 * fPoint[0] and fRadius[0] are the center and radius of the 1st circle
329 * fPoint[1] and fRadius[1] are the center and radius of the 2nd circle
330 * Sweep:
331 * fPoint[0] is the center of the sweep.
332 */
333
334 enum GradientType {
335 kNone_GradientType,
336 kColor_GradientType,
337 kLinear_GradientType,
338 kRadial_GradientType,
339 kRadial2_GradientType,
340 kSweep_GradientType,
reed@google.com83226972012-06-07 20:26:47 +0000341 kConical_GradientType,
342 kLast_GradientType = kConical_GradientType
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000343 };
344
345 struct GradientInfo {
346 int fColorCount; //!< In-out parameter, specifies passed size
347 // of fColors/fColorOffsets on input, and
348 // actual number of colors/offsets on
349 // output.
350 SkColor* fColors; //!< The colors in the gradient.
351 SkScalar* fColorOffsets; //!< The unit offset for color transitions.
352 SkPoint fPoint[2]; //!< Type specific, see above.
353 SkScalar fRadius[2]; //!< Type specific, see above.
354 TileMode fTileMode; //!< The tile mode used.
reed@google.com3d3a8602013-05-24 14:58:44 +0000355 uint32_t fGradientFlags; //!< see SkGradientShader::Flags
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000356 };
357
358 virtual GradientType asAGradient(GradientInfo* info) const;
359
rileya@google.com03c1c352012-07-20 20:02:43 +0000360 /**
commit-bot@chromium.org79590552014-05-13 18:14:45 +0000361 * If the shader subclass is composed of two shaders, return true, and if rec is not NULL,
362 * fill it out with info about the shader.
commit-bot@chromium.org30558792014-05-14 14:28:34 +0000363 *
364 * These are bare pointers; the ownership and reference count are unchanged.
commit-bot@chromium.org79590552014-05-13 18:14:45 +0000365 */
366
367 struct ComposeRec {
368 const SkShader* fShaderA;
369 const SkShader* fShaderB;
370 const SkXfermode* fMode;
371 };
372
373 virtual bool asACompose(ComposeRec* rec) const { return false; }
374
375
376 /**
bsalomon83d081a2014-07-08 09:56:10 -0700377 * Returns true if the shader subclass succeeds in creating an effect or if none is required.
378 * False is returned if it fails or if there is not an implementation of this method in the
379 * shader subclass.
380 *
381 * On success an implementation of this method must inspect the SkPaint and set paintColor to
382 * the color the effect expects as its input color. If the SkShader wishes to emit a solid
383 * color then it should set paintColor to that color and not create an effect. Note that
384 * GrColor is always premul. The common patterns are to convert paint's SkColor to GrColor or
385 * to extract paint's alpha and replicate it to all channels in paintColor. Upon failure
386 * paintColor should not be modified. It is not recommended to specialize the effect to
387 * the paint's color as then many GPU shaders may be generated.
388 *
389 * 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 +0000390 * call createContext. Instead we pass the SkPaint here in case the shader needs paint info.
rileya@google.com03c1c352012-07-20 20:02:43 +0000391 */
dandov9de5b512014-06-10 14:38:28 -0700392 virtual bool asNewEffect(GrContext* context, const SkPaint& paint,
bsalomon83d081a2014-07-08 09:56:10 -0700393 const SkMatrix* localMatrixOrNull, GrColor* paintColor,
394 GrEffect** effect) const;
rileya@google.com03c1c352012-07-20 20:02:43 +0000395
commit-bot@chromium.org79590552014-05-13 18:14:45 +0000396#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
397 /**
398 * If the shader is a custom shader which has data the caller might want, call this function
399 * to get that data.
400 */
401 virtual bool asACustomShader(void** customData) const { return false; }
402#endif
403
reed@android.com8a1c16f2008-12-17 15:59:43 +0000404 //////////////////////////////////////////////////////////////////////////
405 // Factory methods for stock shaders
406
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000407 /**
408 * Call this to create a new "empty" shader, that will not draw anything.
409 */
410 static SkShader* CreateEmptyShader();
411
reed@android.com8a1c16f2008-12-17 15:59:43 +0000412 /** Call this to create a new shader that will draw with the specified bitmap.
reed@google.com99c114e2012-05-03 20:14:26 +0000413 *
414 * If the bitmap cannot be used (e.g. has no pixels, or its dimensions
415 * exceed implementation limits (currently at 64K - 1)) then SkEmptyShader
416 * may be returned.
417 *
commit-bot@chromium.org91246b92013-12-05 15:43:19 +0000418 * If the src is kA8_Config then that mask will be colorized using the color on
419 * the paint.
420 *
reed@google.com99c114e2012-05-03 20:14:26 +0000421 * @param src The bitmap to use inside the shader
422 * @param tmx The tiling mode to use when sampling the bitmap in the x-direction.
423 * @param tmy The tiling mode to use when sampling the bitmap in the y-direction.
424 * @return Returns a new shader object. Note: this function never returns null.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000425 */
426 static SkShader* CreateBitmapShader(const SkBitmap& src,
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +0000427 TileMode tmx, TileMode tmy,
428 const SkMatrix* localMatrix = NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000429
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000430 /** Call this to create a new shader that will draw with the specified picture.
431 *
432 * @param src The picture to use inside the shader (if not NULL, its ref count
commit-bot@chromium.org855e88e2014-04-21 19:33:12 +0000433 * is incremented). The SkPicture must not be changed after
434 * successfully creating a picture shader.
435 * FIXME: src cannot be const due to SkCanvas::drawPicture
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000436 * @param tmx The tiling mode to use when sampling the bitmap in the x-direction.
437 * @param tmy The tiling mode to use when sampling the bitmap in the y-direction.
438 * @return Returns a new shader object. Note: this function never returns null.
439 */
commit-bot@chromium.org5aacfe92014-05-02 21:23:52 +0000440 static SkShader* CreatePictureShader(SkPicture* src, TileMode tmx, TileMode tmy,
441 const SkMatrix* localMatrix = NULL);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000442
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +0000443 /**
444 * Return a shader that will apply the specified localMatrix to the proxy shader.
445 * The specified matrix will be applied before any matrix associated with the proxy.
446 *
447 * Note: ownership of the proxy is not transferred (though a ref is taken).
448 */
449 static SkShader* CreateLocalMatrixShader(SkShader* proxy, const SkMatrix& localMatrix);
450
451 /**
452 * If this shader can be represented by another shader + a localMatrix, return that shader
453 * and, if not NULL, the localMatrix. If not, return NULL and ignore the localMatrix parameter.
454 *
455 * Note: the returned shader (if not NULL) will have been ref'd, and it is the responsibility
456 * of the caller to balance that with unref() when they are done.
457 */
458 virtual SkShader* refAsALocalMatrixShader(SkMatrix* localMatrix) const;
459
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000460 SK_TO_STRING_VIRT()
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +0000461 SK_DEFINE_FLATTENABLE_TYPE(SkShader)
462
reed@android.com8a1c16f2008-12-17 15:59:43 +0000463protected:
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000464 SkShader(SkReadBuffer& );
465 virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000466
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000467 bool computeTotalInverse(const ContextRec&, SkMatrix* totalInverse) const;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000468
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000469 /**
470 * Your subclass must also override contextSize() if it overrides onCreateContext().
471 * Base class impl returns NULL.
472 */
473 virtual Context* onCreateContext(const ContextRec&, void* storage) const;
474
475private:
476 SkMatrix fLocalMatrix;
dandov9de5b512014-06-10 14:38:28 -0700477
reed@android.com8a1c16f2008-12-17 15:59:43 +0000478 typedef SkFlattenable INHERITED;
479};
480
481#endif