blob: 0fb6454408557e6d3f0af9dc00d34ff1f53465fa [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@android.com8a1c16f2008-12-17 15:59:43 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00004 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00005 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#ifndef SkShader_DEFINED
11#define SkShader_DEFINED
12
13#include "SkBitmap.h"
14#include "SkFlattenable.h"
15#include "SkMask.h"
16#include "SkMatrix.h"
17#include "SkPaint.h"
18
19class SkPath;
20
21/** \class SkShader
reed@google.comad917992011-04-11 19:01:12 +000022 *
reed@google.com880dc472012-05-11 14:47:03 +000023 * Shaders specify the source color(s) for what is being drawn. If a paint
24 * has no shader, then the paint's color is used. If the paint has a
25 * shader, then the shader's color(s) are use instead, but they are
26 * modulated by the paint's alpha. This makes it easy to create a shader
27 * once (e.g. bitmap tiling or gradient) and then change its transparency
28 * w/o having to modify the original shader... only the paint's alpha needs
29 * to be modified.
reed@google.comad917992011-04-11 19:01:12 +000030 */
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +000031class SK_API SkShader : public SkFlattenable {
reed@android.com8a1c16f2008-12-17 15:59:43 +000032public:
33 SkShader();
34 virtual ~SkShader();
35
reed@google.comad917992011-04-11 19:01:12 +000036 /**
37 * Return true if the shader has a non-identity local matrix.
38 * @param localM Optional: If not null, return the shader's local matrix
39 * @return true if the shader has a non-identity local matrix.
40 */
reed@android.com8a1c16f2008-12-17 15:59:43 +000041 bool getLocalMatrix(SkMatrix* localM) const;
reed@google.comad917992011-04-11 19:01:12 +000042
43 /**
44 * Set the shader's local matrix.
45 * @param localM The shader's new local matrix.
46 */
reed@android.com8a1c16f2008-12-17 15:59:43 +000047 void setLocalMatrix(const SkMatrix& localM);
reed@google.comad917992011-04-11 19:01:12 +000048
49 /**
50 * Reset the shader's local matrix to identity.
51 */
reed@android.com8a1c16f2008-12-17 15:59:43 +000052 void resetLocalMatrix();
53
54 enum TileMode {
reed@google.com0beaba52012-03-16 14:38:06 +000055 /** replicate the edge color if the shader draws outside of its
56 * original bounds
57 */
58 kClamp_TileMode,
59
60 /** repeat the shader's image horizontally and vertically */
61 kRepeat_TileMode,
62
63 /** repeat the shader's image horizontally and vertically, alternating
64 * mirror images so that adjacent images always seam
65 */
66 kMirror_TileMode,
67
68#if 0
69 /** only draw within the original domain, return 0 everywhere else */
70 kDecal_TileMode,
71#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +000072
73 kTileModeCount
74 };
75
76 // override these in your subclass
77
78 enum Flags {
79 //!< set if all of the colors will be opaque
reed@android.com3c9b2a42009-08-27 19:28:37 +000080 kOpaqueAlpha_Flag = 0x01,
reed@android.com5119bdb2009-06-12 21:27:03 +000081
reed@android.com8a1c16f2008-12-17 15:59:43 +000082 //! set if this shader's shadeSpan16() method can be called
reed@android.com3c9b2a42009-08-27 19:28:37 +000083 kHasSpan16_Flag = 0x02,
reed@android.com5119bdb2009-06-12 21:27:03 +000084
reed@android.com8a1c16f2008-12-17 15:59:43 +000085 /** Set this bit if the shader's native data type is instrinsically 16
86 bit, meaning that calling the 32bit shadeSpan() entry point will
87 mean the the impl has to up-sample 16bit data into 32bit. Used as a
88 a means of clearing a dither request if the it will have no effect
89 */
reed@android.com5119bdb2009-06-12 21:27:03 +000090 kIntrinsicly16_Flag = 0x04,
91
92 /** set (after setContext) if the spans only vary in X (const in Y).
93 e.g. an Nx1 bitmap that is being tiled in Y, or a linear-gradient
reed@android.com3c9b2a42009-08-27 19:28:37 +000094 that varies from left-to-right. This flag specifies this for
95 shadeSpan().
reed@android.com5119bdb2009-06-12 21:27:03 +000096 */
reed@android.com3c9b2a42009-08-27 19:28:37 +000097 kConstInY32_Flag = 0x08,
reed@google.com7c2f27d2011-03-07 19:29:00 +000098
reed@android.com3c9b2a42009-08-27 19:28:37 +000099 /** same as kConstInY32_Flag, but is set if this is true for shadeSpan16
100 which may not always be the case, since shadeSpan16 may be
101 predithered, which would mean it was not const in Y, even though
102 the 32bit shadeSpan() would be const.
103 */
104 kConstInY16_Flag = 0x10
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105 };
106
reed@google.comad917992011-04-11 19:01:12 +0000107 /**
108 * Called sometimes before drawing with this shader. Return the type of
109 * alpha your shader will return. The default implementation returns 0.
110 * Your subclass should override if it can (even sometimes) report a
111 * non-zero value, since that will enable various blitters to perform
112 * faster.
113 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000114 virtual uint32_t getFlags() { return 0; }
115
reed@google.comad917992011-04-11 19:01:12 +0000116 /**
junov@chromium.orgb6e16192011-12-09 15:48:03 +0000117 * Returns true if the shader is guaranteed to produce only opaque
118 * colors, subject to the SkPaint using the shader to apply an opaque
119 * alpha value. Subclasses should override this to allow some
120 * optimizations. isOpaque() can be called at any time, unlike getFlags,
121 * which only works properly when the context is set.
122 */
123 virtual bool isOpaque() const { return false; }
124
125 /**
reed@google.comad917992011-04-11 19:01:12 +0000126 * Return the alpha associated with the data returned by shadeSpan16(). If
127 * kHasSpan16_Flag is not set, this value is meaningless.
128 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000129 virtual uint8_t getSpan16Alpha() const { return fPaintAlpha; }
reed@google.com7c2f27d2011-03-07 19:29:00 +0000130
reed@google.comad917992011-04-11 19:01:12 +0000131 /**
132 * Called once before drawing, with the current paint and device matrix.
133 * Return true if your shader supports these parameters, or false if not.
134 * If false is returned, nothing will be drawn.
135 */
reed@android.com3c9b2a42009-08-27 19:28:37 +0000136 virtual bool setContext(const SkBitmap& device, const SkPaint& paint,
137 const SkMatrix& matrix);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000138
reed@google.comad917992011-04-11 19:01:12 +0000139 /**
140 * Called for each span of the object being drawn. Your subclass should
141 * set the appropriate colors (with premultiplied alpha) that correspond
142 * to the specified device coordinates.
143 */
reed@android.com3c9b2a42009-08-27 19:28:37 +0000144 virtual void shadeSpan(int x, int y, SkPMColor[], int count) = 0;
reed@google.comad917992011-04-11 19:01:12 +0000145
146 /**
147 * Called only for 16bit devices when getFlags() returns
148 * kOpaqueAlphaFlag | kHasSpan16_Flag
149 */
reed@android.com3c9b2a42009-08-27 19:28:37 +0000150 virtual void shadeSpan16(int x, int y, uint16_t[], int count);
reed@google.comad917992011-04-11 19:01:12 +0000151
152 /**
153 * Similar to shadeSpan, but only returns the alpha-channel for a span.
154 * The default implementation calls shadeSpan() and then extracts the alpha
155 * values from the returned colors.
156 */
reed@android.com3c9b2a42009-08-27 19:28:37 +0000157 virtual void shadeSpanAlpha(int x, int y, uint8_t alpha[], int count);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000158
reed@google.comad917992011-04-11 19:01:12 +0000159 /**
160 * Helper function that returns true if this shader's shadeSpan16() method
161 * can be called.
162 */
reed@android.com3c9b2a42009-08-27 19:28:37 +0000163 bool canCallShadeSpan16() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000164 return SkShader::CanCallShadeSpan16(this->getFlags());
165 }
166
reed@google.comad917992011-04-11 19:01:12 +0000167 /**
168 * Helper to check the flags to know if it is legal to call shadeSpan16()
169 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000170 static bool CanCallShadeSpan16(uint32_t flags) {
171 return (flags & kHasSpan16_Flag) != 0;
172 }
173
reed@google.comad917992011-04-11 19:01:12 +0000174 /**
175 * Called before a session using the shader begins. Some shaders override
176 * this to defer some of their work (like calling bitmap.lockPixels()).
177 * Must be balanced by a call to endSession.
178 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000179 virtual void beginSession();
180 virtual void endSession();
reed@google.com7c2f27d2011-03-07 19:29:00 +0000181
reed@android.comf2b98d62010-12-20 18:26:13 +0000182 /**
183 Gives method bitmap should be read to implement a shader.
184 Also determines number and interpretation of "extra" parameters returned
185 by asABitmap
186 */
187 enum BitmapType {
188 kNone_BitmapType, //<! Shader is not represented as a bitmap
reed@google.com7c2f27d2011-03-07 19:29:00 +0000189 kDefault_BitmapType,//<! Access bitmap using local coords transformed
reed@android.comf2b98d62010-12-20 18:26:13 +0000190 // by matrix. No extras
reed@google.com7c2f27d2011-03-07 19:29:00 +0000191 kRadial_BitmapType, //<! Access bitmap by transforming local coordinates
192 // by the matrix and taking the distance of result
193 // from (0,0) as bitmap column. Bitmap is 1 pixel
reed@android.comf2b98d62010-12-20 18:26:13 +0000194 // tall. No extras
reed@google.com7c2f27d2011-03-07 19:29:00 +0000195 kSweep_BitmapType, //<! Access bitmap by transforming local coordinates
reed@android.comf2b98d62010-12-20 18:26:13 +0000196 // by the matrix and taking the angle of result
197 // to (0,0) as bitmap x coord, where angle = 0 is
reed@google.com7c2f27d2011-03-07 19:29:00 +0000198 // bitmap left edge of bitmap = 2pi is the
reed@android.comf2b98d62010-12-20 18:26:13 +0000199 // right edge. Bitmap is 1 pixel tall. No extras
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000200 kTwoPointRadial_BitmapType,
reed@google.com7c2f27d2011-03-07 19:29:00 +0000201 //<! Matrix transforms to space where (0,0) is
reed@android.comf2b98d62010-12-20 18:26:13 +0000202 // the center of the starting circle. The second
reed@google.com7c2f27d2011-03-07 19:29:00 +0000203 // circle will be centered (x, 0) where x may be
204 // 0. The post-matrix space is normalized such
reed@android.comf2b98d62010-12-20 18:26:13 +0000205 // that 1 is the second radius - first radius.
206 // Three extra parameters are returned:
reed@google.com7c2f27d2011-03-07 19:29:00 +0000207 // 0: x-offset of second circle center
reed@android.comf2b98d62010-12-20 18:26:13 +0000208 // to first.
reed@google.com7c2f27d2011-03-07 19:29:00 +0000209 // 1: radius of first circle in post-matrix
reed@android.comf2b98d62010-12-20 18:26:13 +0000210 // space
211 // 2: the second radius minus the first radius
reed@google.com7c2f27d2011-03-07 19:29:00 +0000212 // in pre-transformed space.
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000213
214 kLast_BitmapType = kTwoPointRadial_BitmapType
reed@android.comf2b98d62010-12-20 18:26:13 +0000215 };
reed@android.com8a1c16f2008-12-17 15:59:43 +0000216 /** Optional methods for shaders that can pretend to be a bitmap/texture
reed@google.com7c2f27d2011-03-07 19:29:00 +0000217 to play along with opengl. Default just returns kNone_BitmapType and
reed@android.comf2b98d62010-12-20 18:26:13 +0000218 ignores the out parameters.
219
220 @param outTexture if non-NULL will be the bitmap representing the shader
221 after return.
222 @param outMatrix if non-NULL will be the matrix to apply to vertices
223 to access the bitmap after return.
224 @param xy if non-NULL will be the tile modes that should be
225 used to access the bitmap after return.
226 @param twoPointRadialParams Two extra return values needed for two point
227 radial bitmaps. The first is the x-offset of
228 the second point and the second is the radius
229 about the first point.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000230 */
reed@android.comf2b98d62010-12-20 18:26:13 +0000231 virtual BitmapType asABitmap(SkBitmap* outTexture, SkMatrix* outMatrix,
reed@google.comad917992011-04-11 19:01:12 +0000232 TileMode xy[2], SkScalar* twoPointRadialParams) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000233
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000234 /**
235 * If the shader subclass can be represented as a gradient, asAGradient
236 * returns the matching GradientType enum (or kNone_GradientType if it
237 * cannot). Also, if info is not null, asAGradient populates info with
238 * the relevant (see below) parameters for the gradient. fColorCount
239 * is both an input and output parameter. On input, it indicates how
240 * many entries in fColors and fColorOffsets can be used, if they are
241 * non-NULL. After asAGradient has run, fColorCount indicates how
242 * many color-offset pairs there are in the gradient. If there is
243 * insufficient space to store all of the color-offset pairs, fColors
244 * and fColorOffsets will not be altered. fColorOffsets specifies
245 * where on the range of 0 to 1 to transition to the given color.
246 * The meaning of fPoint and fRadius is dependant on the type of gradient.
247 *
248 * None:
249 * info is ignored.
250 * Color:
251 * fColorOffsets[0] is meaningless.
252 * Linear:
253 * fPoint[0] and fPoint[1] are the end-points of the gradient
254 * Radial:
255 * fPoint[0] and fRadius[0] are the center and radius
256 * Radial2:
257 * fPoint[0] and fRadius[0] are the center and radius of the 1st circle
258 * fPoint[1] and fRadius[1] are the center and radius of the 2nd circle
259 * Sweep:
260 * fPoint[0] is the center of the sweep.
261 */
262
263 enum GradientType {
264 kNone_GradientType,
265 kColor_GradientType,
266 kLinear_GradientType,
267 kRadial_GradientType,
268 kRadial2_GradientType,
269 kSweep_GradientType,
270 kLast_GradientType = kSweep_GradientType
271 };
272
273 struct GradientInfo {
274 int fColorCount; //!< In-out parameter, specifies passed size
275 // of fColors/fColorOffsets on input, and
276 // actual number of colors/offsets on
277 // output.
278 SkColor* fColors; //!< The colors in the gradient.
279 SkScalar* fColorOffsets; //!< The unit offset for color transitions.
280 SkPoint fPoint[2]; //!< Type specific, see above.
281 SkScalar fRadius[2]; //!< Type specific, see above.
282 TileMode fTileMode; //!< The tile mode used.
283 };
284
285 virtual GradientType asAGradient(GradientInfo* info) const;
286
reed@android.com8a1c16f2008-12-17 15:59:43 +0000287 //////////////////////////////////////////////////////////////////////////
288 // Factory methods for stock shaders
289
290 /** Call this to create a new shader that will draw with the specified bitmap.
reed@google.com99c114e2012-05-03 20:14:26 +0000291 *
292 * If the bitmap cannot be used (e.g. has no pixels, or its dimensions
293 * exceed implementation limits (currently at 64K - 1)) then SkEmptyShader
294 * may be returned.
295 *
296 * @param src The bitmap to use inside the shader
297 * @param tmx The tiling mode to use when sampling the bitmap in the x-direction.
298 * @param tmy The tiling mode to use when sampling the bitmap in the y-direction.
299 * @return Returns a new shader object. Note: this function never returns null.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000300 */
301 static SkShader* CreateBitmapShader(const SkBitmap& src,
302 TileMode tmx, TileMode tmy);
303
reed@android.com8a1c16f2008-12-17 15:59:43 +0000304protected:
305 enum MatrixClass {
306 kLinear_MatrixClass, // no perspective
307 kFixedStepInX_MatrixClass, // fast perspective, need to call fixedStepInX() each scanline
308 kPerspective_MatrixClass // slow perspective, need to mappoints each pixel
309 };
310 static MatrixClass ComputeMatrixClass(const SkMatrix&);
311
312 // These can be called by your subclass after setContext() has been called
313 uint8_t getPaintAlpha() const { return fPaintAlpha; }
314 SkBitmap::Config getDeviceConfig() const { return (SkBitmap::Config)fDeviceConfig; }
315 const SkMatrix& getTotalInverse() const { return fTotalInverse; }
316 MatrixClass getInverseClass() const { return (MatrixClass)fTotalInverseClass; }
317
318 SkShader(SkFlattenableReadBuffer& );
djsollen@google.com54924242012-03-29 15:18:04 +0000319 virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000320private:
321 SkMatrix* fLocalMatrix;
322 SkMatrix fTotalInverse;
323 uint8_t fPaintAlpha;
324 uint8_t fDeviceConfig;
325 uint8_t fTotalInverseClass;
326 SkDEBUGCODE(SkBool8 fInSession;)
327
328 static SkShader* CreateBitmapShader(const SkBitmap& src,
329 TileMode, TileMode,
330 void* storage, size_t storageSize);
331 friend class SkAutoBitmapShaderInstall;
332 typedef SkFlattenable INHERITED;
333};
334
335#endif
336