blob: 11956ce0d2d187f8401b5224b7e12cd3cc80345e [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;
rileya@google.com03c1c352012-07-20 20:02:43 +000019class GrContext;
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000020class GrEffectRef;
reed@android.com8a1c16f2008-12-17 15:59:43 +000021
22/** \class SkShader
reed@google.comad917992011-04-11 19:01:12 +000023 *
reed@google.com880dc472012-05-11 14:47:03 +000024 * Shaders specify the source color(s) for what is being drawn. If a paint
25 * has no shader, then the paint's color is used. If the paint has a
26 * shader, then the shader's color(s) are use instead, but they are
27 * modulated by the paint's alpha. This makes it easy to create a shader
28 * once (e.g. bitmap tiling or gradient) and then change its transparency
29 * w/o having to modify the original shader... only the paint's alpha needs
30 * to be modified.
reed@google.comad917992011-04-11 19:01:12 +000031 */
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +000032class SK_API SkShader : public SkFlattenable {
reed@android.com8a1c16f2008-12-17 15:59:43 +000033public:
robertphillips@google.com0456e0b2012-06-27 14:03:26 +000034 SK_DECLARE_INST_COUNT(SkShader)
35
36 SkShader();
reed@android.com8a1c16f2008-12-17 15:59:43 +000037 virtual ~SkShader();
38
reed@google.comad917992011-04-11 19:01:12 +000039 /**
bsalomon@google.comf94b3a42012-10-31 18:09:01 +000040 * Returns true if the local matrix is not an identity matrix.
reed@google.comad917992011-04-11 19:01:12 +000041 */
bsalomon@google.comf94b3a42012-10-31 18:09:01 +000042 bool hasLocalMatrix() const { return !fLocalMatrix.isIdentity(); }
43
44 /**
45 * Returns the local matrix.
46 */
47 const SkMatrix& getLocalMatrix() const { return fLocalMatrix; }
reed@google.comad917992011-04-11 19:01:12 +000048
49 /**
50 * Set the shader's local matrix.
51 * @param localM The shader's new local matrix.
52 */
bsalomon@google.comf94b3a42012-10-31 18:09:01 +000053 void setLocalMatrix(const SkMatrix& localM) { fLocalMatrix = localM; }
reed@google.comad917992011-04-11 19:01:12 +000054
55 /**
56 * Reset the shader's local matrix to identity.
57 */
bsalomon@google.comf94b3a42012-10-31 18:09:01 +000058 void resetLocalMatrix() { fLocalMatrix.reset(); }
reed@android.com8a1c16f2008-12-17 15:59:43 +000059
60 enum TileMode {
reed@google.com0beaba52012-03-16 14:38:06 +000061 /** replicate the edge color if the shader draws outside of its
62 * original bounds
63 */
64 kClamp_TileMode,
65
66 /** repeat the shader's image horizontally and vertically */
67 kRepeat_TileMode,
68
69 /** repeat the shader's image horizontally and vertically, alternating
70 * mirror images so that adjacent images always seam
71 */
72 kMirror_TileMode,
73
74#if 0
75 /** only draw within the original domain, return 0 everywhere else */
76 kDecal_TileMode,
77#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +000078
79 kTileModeCount
80 };
81
82 // override these in your subclass
83
84 enum Flags {
85 //!< set if all of the colors will be opaque
reed@android.com3c9b2a42009-08-27 19:28:37 +000086 kOpaqueAlpha_Flag = 0x01,
reed@android.com5119bdb2009-06-12 21:27:03 +000087
reed@android.com8a1c16f2008-12-17 15:59:43 +000088 //! set if this shader's shadeSpan16() method can be called
reed@android.com3c9b2a42009-08-27 19:28:37 +000089 kHasSpan16_Flag = 0x02,
reed@android.com5119bdb2009-06-12 21:27:03 +000090
reed@android.com8a1c16f2008-12-17 15:59:43 +000091 /** Set this bit if the shader's native data type is instrinsically 16
92 bit, meaning that calling the 32bit shadeSpan() entry point will
93 mean the the impl has to up-sample 16bit data into 32bit. Used as a
94 a means of clearing a dither request if the it will have no effect
95 */
reed@android.com5119bdb2009-06-12 21:27:03 +000096 kIntrinsicly16_Flag = 0x04,
97
98 /** set (after setContext) if the spans only vary in X (const in Y).
99 e.g. an Nx1 bitmap that is being tiled in Y, or a linear-gradient
reed@android.com3c9b2a42009-08-27 19:28:37 +0000100 that varies from left-to-right. This flag specifies this for
101 shadeSpan().
reed@android.com5119bdb2009-06-12 21:27:03 +0000102 */
reed@android.com3c9b2a42009-08-27 19:28:37 +0000103 kConstInY32_Flag = 0x08,
reed@google.com7c2f27d2011-03-07 19:29:00 +0000104
reed@android.com3c9b2a42009-08-27 19:28:37 +0000105 /** same as kConstInY32_Flag, but is set if this is true for shadeSpan16
106 which may not always be the case, since shadeSpan16 may be
107 predithered, which would mean it was not const in Y, even though
108 the 32bit shadeSpan() would be const.
109 */
110 kConstInY16_Flag = 0x10
reed@android.com8a1c16f2008-12-17 15:59:43 +0000111 };
112
reed@google.comad917992011-04-11 19:01:12 +0000113 /**
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 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000120 virtual uint32_t getFlags() { return 0; }
121
reed@google.comad917992011-04-11 19:01:12 +0000122 /**
junov@chromium.orgb6e16192011-12-09 15:48:03 +0000123 * Returns true if the shader is guaranteed to produce only opaque
124 * colors, subject to the SkPaint using the shader to apply an opaque
125 * alpha value. Subclasses should override this to allow some
126 * optimizations. isOpaque() can be called at any time, unlike getFlags,
127 * which only works properly when the context is set.
128 */
129 virtual bool isOpaque() const { return false; }
130
131 /**
reed@google.comad917992011-04-11 19:01:12 +0000132 * Return the alpha associated with the data returned by shadeSpan16(). If
133 * kHasSpan16_Flag is not set, this value is meaningless.
134 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000135 virtual uint8_t getSpan16Alpha() const { return fPaintAlpha; }
reed@google.com7c2f27d2011-03-07 19:29:00 +0000136
reed@google.comad917992011-04-11 19:01:12 +0000137 /**
138 * Called once before drawing, with the current paint and device matrix.
139 * Return true if your shader supports these parameters, or false if not.
reed@google.coma641f3f2012-12-13 22:16:30 +0000140 * If false is returned, nothing will be drawn. If true is returned, then
141 * a balancing call to endContext() will be made before the next call to
142 * setContext.
143 *
144 * Subclasses should be sure to call their INHERITED::setContext() if they
145 * override this method.
reed@google.comad917992011-04-11 19:01:12 +0000146 */
reed@android.com3c9b2a42009-08-27 19:28:37 +0000147 virtual bool setContext(const SkBitmap& device, const SkPaint& paint,
148 const SkMatrix& matrix);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000149
reed@google.comad917992011-04-11 19:01:12 +0000150 /**
reed@google.coma641f3f2012-12-13 22:16:30 +0000151 * Assuming setContext returned true, endContext() will be called when
152 * the draw using the shader has completed. It is an error for setContext
153 * to be called twice w/o an intervening call to endContext().
154 *
155 * Subclasses should be sure to call their INHERITED::endContext() if they
156 * override this method.
157 */
158 virtual void endContext();
159
robertphillips@google.com441a0052012-12-14 13:55:06 +0000160 SkDEBUGCODE(bool setContextHasBeenCalled() const { return SkToBool(fInSetContext); })
reed@google.coma641f3f2012-12-13 22:16:30 +0000161
162 /**
reed@google.comad917992011-04-11 19:01:12 +0000163 * Called for each span of the object being drawn. Your subclass should
164 * set the appropriate colors (with premultiplied alpha) that correspond
165 * to the specified device coordinates.
166 */
reed@android.com3c9b2a42009-08-27 19:28:37 +0000167 virtual void shadeSpan(int x, int y, SkPMColor[], int count) = 0;
reed@google.comad917992011-04-11 19:01:12 +0000168
reed@google.com3bafe742012-10-12 18:56:18 +0000169 typedef void (*ShadeProc)(void* ctx, int x, int y, SkPMColor[], int count);
170 virtual ShadeProc asAShadeProc(void** ctx);
171
reed@google.comad917992011-04-11 19:01:12 +0000172 /**
173 * Called only for 16bit devices when getFlags() returns
174 * kOpaqueAlphaFlag | kHasSpan16_Flag
175 */
reed@android.com3c9b2a42009-08-27 19:28:37 +0000176 virtual void shadeSpan16(int x, int y, uint16_t[], int count);
reed@google.comad917992011-04-11 19:01:12 +0000177
178 /**
179 * Similar to shadeSpan, but only returns the alpha-channel for a span.
180 * The default implementation calls shadeSpan() and then extracts the alpha
181 * values from the returned colors.
182 */
reed@android.com3c9b2a42009-08-27 19:28:37 +0000183 virtual void shadeSpanAlpha(int x, int y, uint8_t alpha[], int count);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000184
reed@google.comad917992011-04-11 19:01:12 +0000185 /**
186 * Helper function that returns true if this shader's shadeSpan16() method
187 * can be called.
188 */
reed@android.com3c9b2a42009-08-27 19:28:37 +0000189 bool canCallShadeSpan16() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000190 return SkShader::CanCallShadeSpan16(this->getFlags());
191 }
192
reed@google.comad917992011-04-11 19:01:12 +0000193 /**
194 * Helper to check the flags to know if it is legal to call shadeSpan16()
195 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000196 static bool CanCallShadeSpan16(uint32_t flags) {
197 return (flags & kHasSpan16_Flag) != 0;
198 }
199
reed@google.comad917992011-04-11 19:01:12 +0000200 /**
reed@android.comf2b98d62010-12-20 18:26:13 +0000201 Gives method bitmap should be read to implement a shader.
202 Also determines number and interpretation of "extra" parameters returned
203 by asABitmap
204 */
205 enum BitmapType {
206 kNone_BitmapType, //<! Shader is not represented as a bitmap
reed@google.com7c2f27d2011-03-07 19:29:00 +0000207 kDefault_BitmapType,//<! Access bitmap using local coords transformed
reed@android.comf2b98d62010-12-20 18:26:13 +0000208 // by matrix. No extras
reed@google.com7c2f27d2011-03-07 19:29:00 +0000209 kRadial_BitmapType, //<! Access bitmap by transforming local coordinates
210 // by the matrix and taking the distance of result
211 // from (0,0) as bitmap column. Bitmap is 1 pixel
reed@android.comf2b98d62010-12-20 18:26:13 +0000212 // tall. No extras
reed@google.com7c2f27d2011-03-07 19:29:00 +0000213 kSweep_BitmapType, //<! Access bitmap by transforming local coordinates
reed@android.comf2b98d62010-12-20 18:26:13 +0000214 // by the matrix and taking the angle of result
215 // to (0,0) as bitmap x coord, where angle = 0 is
reed@google.com7c2f27d2011-03-07 19:29:00 +0000216 // bitmap left edge of bitmap = 2pi is the
reed@android.comf2b98d62010-12-20 18:26:13 +0000217 // right edge. Bitmap is 1 pixel tall. No extras
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000218 kTwoPointRadial_BitmapType,
reed@google.com7c2f27d2011-03-07 19:29:00 +0000219 //<! Matrix transforms to space where (0,0) is
reed@android.comf2b98d62010-12-20 18:26:13 +0000220 // the center of the starting circle. The second
reed@google.com7c2f27d2011-03-07 19:29:00 +0000221 // circle will be centered (x, 0) where x may be
222 // 0. The post-matrix space is normalized such
reed@android.comf2b98d62010-12-20 18:26:13 +0000223 // that 1 is the second radius - first radius.
224 // Three extra parameters are returned:
reed@google.com7c2f27d2011-03-07 19:29:00 +0000225 // 0: x-offset of second circle center
reed@android.comf2b98d62010-12-20 18:26:13 +0000226 // to first.
reed@google.com7c2f27d2011-03-07 19:29:00 +0000227 // 1: radius of first circle in post-matrix
reed@android.comf2b98d62010-12-20 18:26:13 +0000228 // space
229 // 2: the second radius minus the first radius
reed@google.com7c2f27d2011-03-07 19:29:00 +0000230 // in pre-transformed space.
rileya@google.com3e332582012-07-03 13:43:35 +0000231 kTwoPointConical_BitmapType,
232 //<! Matrix transforms to space where (0,0) is
233 // the center of the starting circle. The second
234 // circle will be centered (x, 0) where x may be
235 // 0.
236 // Three extra parameters are returned:
237 // 0: x-offset of second circle center
238 // to first.
239 // 1: radius of first circle
240 // 2: the second radius minus the first radius
rileya@google.com22e57f92012-07-19 15:16:19 +0000241 kLinear_BitmapType, //<! Access bitmap using local coords transformed
242 // by matrix. No extras
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000243
rileya@google.com22e57f92012-07-19 15:16:19 +0000244 kLast_BitmapType = kLinear_BitmapType
reed@android.comf2b98d62010-12-20 18:26:13 +0000245 };
reed@android.com8a1c16f2008-12-17 15:59:43 +0000246 /** Optional methods for shaders that can pretend to be a bitmap/texture
reed@google.com7c2f27d2011-03-07 19:29:00 +0000247 to play along with opengl. Default just returns kNone_BitmapType and
reed@android.comf2b98d62010-12-20 18:26:13 +0000248 ignores the out parameters.
249
250 @param outTexture if non-NULL will be the bitmap representing the shader
251 after return.
252 @param outMatrix if non-NULL will be the matrix to apply to vertices
253 to access the bitmap after return.
254 @param xy if non-NULL will be the tile modes that should be
255 used to access the bitmap after return.
256 @param twoPointRadialParams Two extra return values needed for two point
257 radial bitmaps. The first is the x-offset of
258 the second point and the second is the radius
259 about the first point.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000260 */
reed@android.comf2b98d62010-12-20 18:26:13 +0000261 virtual BitmapType asABitmap(SkBitmap* outTexture, SkMatrix* outMatrix,
rileya@google.com91f319c2012-07-25 17:18:31 +0000262 TileMode xy[2]) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000263
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000264 /**
265 * If the shader subclass can be represented as a gradient, asAGradient
266 * returns the matching GradientType enum (or kNone_GradientType if it
267 * cannot). Also, if info is not null, asAGradient populates info with
268 * the relevant (see below) parameters for the gradient. fColorCount
269 * is both an input and output parameter. On input, it indicates how
270 * many entries in fColors and fColorOffsets can be used, if they are
271 * non-NULL. After asAGradient has run, fColorCount indicates how
272 * many color-offset pairs there are in the gradient. If there is
273 * insufficient space to store all of the color-offset pairs, fColors
274 * and fColorOffsets will not be altered. fColorOffsets specifies
275 * where on the range of 0 to 1 to transition to the given color.
276 * The meaning of fPoint and fRadius is dependant on the type of gradient.
277 *
278 * None:
279 * info is ignored.
280 * Color:
281 * fColorOffsets[0] is meaningless.
282 * Linear:
283 * fPoint[0] and fPoint[1] are the end-points of the gradient
284 * Radial:
285 * fPoint[0] and fRadius[0] are the center and radius
286 * Radial2:
287 * fPoint[0] and fRadius[0] are the center and radius of the 1st circle
288 * fPoint[1] and fRadius[1] are the center and radius of the 2nd circle
289 * Sweep:
290 * fPoint[0] is the center of the sweep.
291 */
292
293 enum GradientType {
294 kNone_GradientType,
295 kColor_GradientType,
296 kLinear_GradientType,
297 kRadial_GradientType,
298 kRadial2_GradientType,
299 kSweep_GradientType,
reed@google.com83226972012-06-07 20:26:47 +0000300 kConical_GradientType,
301 kLast_GradientType = kConical_GradientType
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000302 };
303
304 struct GradientInfo {
305 int fColorCount; //!< In-out parameter, specifies passed size
306 // of fColors/fColorOffsets on input, and
307 // actual number of colors/offsets on
308 // output.
309 SkColor* fColors; //!< The colors in the gradient.
310 SkScalar* fColorOffsets; //!< The unit offset for color transitions.
311 SkPoint fPoint[2]; //!< Type specific, see above.
312 SkScalar fRadius[2]; //!< Type specific, see above.
313 TileMode fTileMode; //!< The tile mode used.
reed@google.com3d3a8602013-05-24 14:58:44 +0000314 uint32_t fGradientFlags; //!< see SkGradientShader::Flags
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000315 };
316
317 virtual GradientType asAGradient(GradientInfo* info) const;
318
rileya@google.com03c1c352012-07-20 20:02:43 +0000319 /**
commit-bot@chromium.org91a798f2013-09-06 15:31:06 +0000320 * If the shader subclass has a GrEffect implementation, this resturns the effect to install.
321 * The incoming color to the effect has r=g=b=a all extracted from the SkPaint's alpha.
322 * The output color should be the computed SkShader premul color modulated by the incoming
323 * color. The GrContext may be used by the effect to create textures. The GPU device does not
324 * call setContext. Instead we pass the SkPaint here in case the shader needs paint info.
rileya@google.com03c1c352012-07-20 20:02:43 +0000325 */
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000326 virtual GrEffectRef* asNewEffect(GrContext* context, const SkPaint& paint) const;
rileya@google.com03c1c352012-07-20 20:02:43 +0000327
reed@android.com8a1c16f2008-12-17 15:59:43 +0000328 //////////////////////////////////////////////////////////////////////////
329 // Factory methods for stock shaders
330
331 /** Call this to create a new shader that will draw with the specified bitmap.
reed@google.com99c114e2012-05-03 20:14:26 +0000332 *
333 * If the bitmap cannot be used (e.g. has no pixels, or its dimensions
334 * exceed implementation limits (currently at 64K - 1)) then SkEmptyShader
335 * may be returned.
336 *
337 * @param src The bitmap to use inside the shader
338 * @param tmx The tiling mode to use when sampling the bitmap in the x-direction.
339 * @param tmy The tiling mode to use when sampling the bitmap in the y-direction.
340 * @return Returns a new shader object. Note: this function never returns null.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000341 */
342 static SkShader* CreateBitmapShader(const SkBitmap& src,
343 TileMode tmx, TileMode tmy);
344
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000345 SkDEVCODE(virtual void toString(SkString* str) const;)
346
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +0000347 SK_DEFINE_FLATTENABLE_TYPE(SkShader)
348
reed@android.com8a1c16f2008-12-17 15:59:43 +0000349protected:
350 enum MatrixClass {
351 kLinear_MatrixClass, // no perspective
352 kFixedStepInX_MatrixClass, // fast perspective, need to call fixedStepInX() each scanline
353 kPerspective_MatrixClass // slow perspective, need to mappoints each pixel
354 };
355 static MatrixClass ComputeMatrixClass(const SkMatrix&);
356
357 // These can be called by your subclass after setContext() has been called
358 uint8_t getPaintAlpha() const { return fPaintAlpha; }
359 SkBitmap::Config getDeviceConfig() const { return (SkBitmap::Config)fDeviceConfig; }
360 const SkMatrix& getTotalInverse() const { return fTotalInverse; }
361 MatrixClass getInverseClass() const { return (MatrixClass)fTotalInverseClass; }
362
363 SkShader(SkFlattenableReadBuffer& );
djsollen@google.com54924242012-03-29 15:18:04 +0000364 virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000365private:
bsalomon@google.comf94b3a42012-10-31 18:09:01 +0000366 SkMatrix fLocalMatrix;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000367 SkMatrix fTotalInverse;
368 uint8_t fPaintAlpha;
369 uint8_t fDeviceConfig;
370 uint8_t fTotalInverseClass;
reed@google.coma641f3f2012-12-13 22:16:30 +0000371 SkDEBUGCODE(SkBool8 fInSetContext;)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000372
373 static SkShader* CreateBitmapShader(const SkBitmap& src,
374 TileMode, TileMode,
375 void* storage, size_t storageSize);
376 friend class SkAutoBitmapShaderInstall;
377 typedef SkFlattenable INHERITED;
378};
379
380#endif