blob: 39ffd86f3a369d9c31c177cb640e1779e9c09104 [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
8#ifndef SkShader_DEFINED
9#define SkShader_DEFINED
10
11#include "SkBitmap.h"
12#include "SkFlattenable.h"
fmalitad0c4e092016-02-22 17:19:04 -080013#include "SkImageInfo.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000014#include "SkMask.h"
15#include "SkMatrix.h"
16#include "SkPaint.h"
bsalomon4beef912014-07-28 13:43:02 -070017#include "../gpu/GrColor.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000018
reed3061af42016-01-07 15:47:29 -080019class SkColorFilter;
reed@android.com8a1c16f2008-12-17 15:59:43 +000020class SkPath;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000021class SkPicture;
commit-bot@chromium.org79590552014-05-13 18:14:45 +000022class SkXfermode;
rileya@google.com03c1c352012-07-20 20:02:43 +000023class GrContext;
joshualittb0a8a372014-09-23 09:50:21 -070024class GrFragmentProcessor;
reed@android.com8a1c16f2008-12-17 15:59:43 +000025
26/** \class SkShader
reed@google.comad917992011-04-11 19:01:12 +000027 *
reed@google.com880dc472012-05-11 14:47:03 +000028 * Shaders specify the source color(s) for what is being drawn. If a paint
29 * has no shader, then the paint's color is used. If the paint has a
30 * shader, then the shader's color(s) are use instead, but they are
31 * modulated by the paint's alpha. This makes it easy to create a shader
32 * once (e.g. bitmap tiling or gradient) and then change its transparency
33 * w/o having to modify the original shader... only the paint's alpha needs
34 * to be modified.
reed@google.comad917992011-04-11 19:01:12 +000035 */
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +000036class SK_API SkShader : public SkFlattenable {
reed@android.com8a1c16f2008-12-17 15:59:43 +000037public:
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.
scroggoc870d492014-07-11 10:42:12 -070043 *
44 * FIXME: This can be incorrect for a Shader with its own local matrix
45 * that is also wrapped via CreateLocalMatrixShader.
commit-bot@chromium.orgd12de022014-05-09 15:42:07 +000046 */
47 const SkMatrix& getLocalMatrix() const { return fLocalMatrix; }
48
reed@android.com8a1c16f2008-12-17 15:59:43 +000049 enum TileMode {
reed@google.com0beaba52012-03-16 14:38:06 +000050 /** replicate the edge color if the shader draws outside of its
51 * original bounds
52 */
53 kClamp_TileMode,
54
55 /** repeat the shader's image horizontally and vertically */
56 kRepeat_TileMode,
57
58 /** repeat the shader's image horizontally and vertically, alternating
59 * mirror images so that adjacent images always seam
60 */
61 kMirror_TileMode,
62
63#if 0
64 /** only draw within the original domain, return 0 everywhere else */
65 kDecal_TileMode,
66#endif
reed19c25f12015-03-15 14:01:21 -070067 };
reed@android.com8a1c16f2008-12-17 15:59:43 +000068
reed19c25f12015-03-15 14:01:21 -070069 enum {
70 kTileModeCount = kMirror_TileMode + 1
reed@android.com8a1c16f2008-12-17 15:59:43 +000071 };
72
73 // override these in your subclass
74
75 enum Flags {
76 //!< set if all of the colors will be opaque
reed4e5a7582016-01-05 05:10:33 -080077 kOpaqueAlpha_Flag = 1 << 0,
reed@android.com5119bdb2009-06-12 21:27:03 +000078
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +000079 /** set if the spans only vary in X (const in Y).
reed@android.com5119bdb2009-06-12 21:27:03 +000080 e.g. an Nx1 bitmap that is being tiled in Y, or a linear-gradient
reed@android.com3c9b2a42009-08-27 19:28:37 +000081 that varies from left-to-right. This flag specifies this for
82 shadeSpan().
reed@android.com5119bdb2009-06-12 21:27:03 +000083 */
reed4e5a7582016-01-05 05:10:33 -080084 kConstInY32_Flag = 1 << 1,
fmalitaca058f52016-02-23 19:02:20 -080085
86 /** hint for the blitter that 4f is the preferred shading mode.
87 */
88 kPrefers4f_Flag = 1 << 2,
reed@android.com8a1c16f2008-12-17 15:59:43 +000089 };
90
reed@google.comad917992011-04-11 19:01:12 +000091 /**
junov@chromium.orgb6e16192011-12-09 15:48:03 +000092 * Returns true if the shader is guaranteed to produce only opaque
93 * colors, subject to the SkPaint using the shader to apply an opaque
94 * alpha value. Subclasses should override this to allow some
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +000095 * optimizations.
junov@chromium.orgb6e16192011-12-09 15:48:03 +000096 */
97 virtual bool isOpaque() const { return false; }
98
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +000099 /**
100 * ContextRec acts as a parameter bundle for creating Contexts.
101 */
102 struct ContextRec {
fmalitad0c4e092016-02-22 17:19:04 -0800103 enum DstType {
104 kPMColor_DstType, // clients prefer shading into PMColor dest
105 kPM4f_DstType, // clients prefer shading into PM4f dest
106 };
107
108 ContextRec(const SkPaint& paint, const SkMatrix& matrix, const SkMatrix* localM,
109 DstType dstType)
reed56263c72015-06-05 11:31:26 -0700110 : fPaint(&paint)
commit-bot@chromium.org80116dc2014-05-06 17:16:03 +0000111 , fMatrix(&matrix)
fmalitad0c4e092016-02-22 17:19:04 -0800112 , fLocalMatrix(localM)
113 , fPreferredDstType(dstType) {}
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000114
fmalitad0c4e092016-02-22 17:19:04 -0800115 const SkPaint* fPaint; // the current paint associated with the draw
116 const SkMatrix* fMatrix; // the current matrix in the canvas
117 const SkMatrix* fLocalMatrix; // optional local matrix
118 const DstType fPreferredDstType; // the "natural" client dest type
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000119 };
120
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000121 class Context : public ::SkNoncopyable {
122 public:
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000123 Context(const SkShader& shader, const ContextRec&);
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000124
125 virtual ~Context();
126
127 /**
128 * Called sometimes before drawing with this shader. Return the type of
129 * alpha your shader will return. The default implementation returns 0.
130 * Your subclass should override if it can (even sometimes) report a
131 * non-zero value, since that will enable various blitters to perform
132 * faster.
133 */
134 virtual uint32_t getFlags() const { return 0; }
135
136 /**
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000137 * Called for each span of the object being drawn. Your subclass should
138 * set the appropriate colors (with premultiplied alpha) that correspond
139 * to the specified device coordinates.
140 */
141 virtual void shadeSpan(int x, int y, SkPMColor[], int count) = 0;
142
reed6d3cef92016-01-22 01:04:29 -0800143 virtual void shadeSpan4f(int x, int y, SkPM4f[], int count);
144
reed830dfd82016-03-16 12:29:01 -0700145 struct BlitState {
146 Context* fCtx;
147 SkXfermode* fXfer;
148 enum { N = 2 };
149 void* fStorage[N];
150 };
151 typedef void (*BlitProc)(BlitState*,
152 int x, int y, const SkPixmap&, int count, const SkAlpha[]);
153 BlitProc chooseBlitProc(const SkImageInfo& info, BlitState* state) {
154 return this->onChooseBlitProc(info, state);
155 }
156
herbc7a784c2015-12-18 09:52:15 -0800157 /**
158 * The const void* ctx is only const because all the implementations are const.
159 * This can be changed to non-const if a new shade proc needs to change the ctx.
160 */
161 typedef void (*ShadeProc)(const void* ctx, int x, int y, SkPMColor[], int count);
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000162 virtual ShadeProc asAShadeProc(void** ctx);
163
164 /**
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000165 * Similar to shadeSpan, but only returns the alpha-channel for a span.
166 * The default implementation calls shadeSpan() and then extracts the alpha
167 * values from the returned colors.
168 */
169 virtual void shadeSpanAlpha(int x, int y, uint8_t alpha[], int count);
170
reedcc0e3112014-09-10 10:20:24 -0700171 // Notification from blitter::blitMask in case we need to see the non-alpha channels
172 virtual void set3DMask(const SkMask*) {}
173
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000174 protected:
175 // Reference to shader, so we don't have to dupe information.
176 const SkShader& fShader;
177
178 enum MatrixClass {
179 kLinear_MatrixClass, // no perspective
180 kFixedStepInX_MatrixClass, // fast perspective, need to call fixedStepInX() each
181 // scanline
182 kPerspective_MatrixClass // slow perspective, need to mappoints each pixel
183 };
184 static MatrixClass ComputeMatrixClass(const SkMatrix&);
185
commit-bot@chromium.org80116dc2014-05-06 17:16:03 +0000186 uint8_t getPaintAlpha() const { return fPaintAlpha; }
187 const SkMatrix& getTotalInverse() const { return fTotalInverse; }
188 MatrixClass getInverseClass() const { return (MatrixClass)fTotalInverseClass; }
189 const SkMatrix& getCTM() const { return fCTM; }
reed830dfd82016-03-16 12:29:01 -0700190
191 virtual BlitProc onChooseBlitProc(const SkImageInfo&, BlitState*) {
192 return nullptr;
193 }
194
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000195 private:
commit-bot@chromium.org80116dc2014-05-06 17:16:03 +0000196 SkMatrix fCTM;
197 SkMatrix fTotalInverse;
198 uint8_t fPaintAlpha;
199 uint8_t fTotalInverseClass;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000200
201 typedef SkNoncopyable INHERITED;
202 };
reed@google.com7c2f27d2011-03-07 19:29:00 +0000203
reed@google.comad917992011-04-11 19:01:12 +0000204 /**
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000205 * Create the actual object that does the shading.
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000206 * Size of storage must be >= contextSize.
reed@google.coma641f3f2012-12-13 22:16:30 +0000207 */
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000208 Context* createContext(const ContextRec&, void* storage) const;
reed@google.coma641f3f2012-12-13 22:16:30 +0000209
210 /**
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000211 * Return the size of a Context returned by createContext.
reed@google.comad917992011-04-11 19:01:12 +0000212 */
reed773ceda2016-03-03 18:18:25 -0800213 size_t contextSize(const ContextRec&) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000214
reed@google.comad917992011-04-11 19:01:12 +0000215 /**
reed0f0af232015-09-08 11:02:04 -0700216 * Returns true if this shader is just a bitmap, and if not null, returns the bitmap,
217 * localMatrix, and tilemodes. If this is not a bitmap, returns false and ignores the
218 * out-parameters.
reed@android.comf2b98d62010-12-20 18:26:13 +0000219 */
reed0f0af232015-09-08 11:02:04 -0700220 bool isABitmap(SkBitmap* outTexture, SkMatrix* outMatrix, TileMode xy[2]) const {
221 return this->onIsABitmap(outTexture, outMatrix, xy);
scroggoff390c92015-09-08 06:24:08 -0700222 }
reed0f0af232015-09-08 11:02:04 -0700223
reedf5822822015-08-19 11:46:38 -0700224 bool isABitmap() const {
225 return this->isABitmap(nullptr, nullptr, nullptr);
226 }
227
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000228 /**
229 * If the shader subclass can be represented as a gradient, asAGradient
230 * returns the matching GradientType enum (or kNone_GradientType if it
231 * cannot). Also, if info is not null, asAGradient populates info with
232 * the relevant (see below) parameters for the gradient. fColorCount
233 * is both an input and output parameter. On input, it indicates how
234 * many entries in fColors and fColorOffsets can be used, if they are
235 * non-NULL. After asAGradient has run, fColorCount indicates how
236 * many color-offset pairs there are in the gradient. If there is
237 * insufficient space to store all of the color-offset pairs, fColors
238 * and fColorOffsets will not be altered. fColorOffsets specifies
239 * where on the range of 0 to 1 to transition to the given color.
240 * The meaning of fPoint and fRadius is dependant on the type of gradient.
241 *
242 * None:
243 * info is ignored.
244 * Color:
245 * fColorOffsets[0] is meaningless.
246 * Linear:
247 * fPoint[0] and fPoint[1] are the end-points of the gradient
248 * Radial:
249 * fPoint[0] and fRadius[0] are the center and radius
reed71a6cbf2015-05-04 08:32:51 -0700250 * Conical:
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000251 * fPoint[0] and fRadius[0] are the center and radius of the 1st circle
252 * fPoint[1] and fRadius[1] are the center and radius of the 2nd circle
253 * Sweep:
254 * fPoint[0] is the center of the sweep.
255 */
256
257 enum GradientType {
258 kNone_GradientType,
259 kColor_GradientType,
260 kLinear_GradientType,
261 kRadial_GradientType,
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000262 kSweep_GradientType,
reed@google.com83226972012-06-07 20:26:47 +0000263 kConical_GradientType,
264 kLast_GradientType = kConical_GradientType
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000265 };
266
267 struct GradientInfo {
268 int fColorCount; //!< In-out parameter, specifies passed size
269 // of fColors/fColorOffsets on input, and
270 // actual number of colors/offsets on
271 // output.
272 SkColor* fColors; //!< The colors in the gradient.
273 SkScalar* fColorOffsets; //!< The unit offset for color transitions.
274 SkPoint fPoint[2]; //!< Type specific, see above.
275 SkScalar fRadius[2]; //!< Type specific, see above.
276 TileMode fTileMode; //!< The tile mode used.
reed@google.com3d3a8602013-05-24 14:58:44 +0000277 uint32_t fGradientFlags; //!< see SkGradientShader::Flags
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000278 };
279
280 virtual GradientType asAGradient(GradientInfo* info) const;
281
rileya@google.com03c1c352012-07-20 20:02:43 +0000282 /**
commit-bot@chromium.org79590552014-05-13 18:14:45 +0000283 * If the shader subclass is composed of two shaders, return true, and if rec is not NULL,
284 * fill it out with info about the shader.
commit-bot@chromium.org30558792014-05-14 14:28:34 +0000285 *
286 * These are bare pointers; the ownership and reference count are unchanged.
commit-bot@chromium.org79590552014-05-13 18:14:45 +0000287 */
288
289 struct ComposeRec {
290 const SkShader* fShaderA;
291 const SkShader* fShaderB;
292 const SkXfermode* fMode;
293 };
294
djsollenc87dd2c2014-11-14 11:11:46 -0800295 virtual bool asACompose(ComposeRec*) const { return false; }
commit-bot@chromium.org79590552014-05-13 18:14:45 +0000296
297
298 /**
bsalomonc21b09e2015-08-28 18:46:56 -0700299 * Returns a GrFragmentProcessor that implements the shader for the GPU backend. NULL is
300 * returned if there is no GPU implementation.
bsalomon83d081a2014-07-08 09:56:10 -0700301 *
bsalomonc21b09e2015-08-28 18:46:56 -0700302 * The GPU device does not call SkShader::createContext(), instead we pass the view matrix,
303 * local matrix, and filter quality directly.
bsalomon83d081a2014-07-08 09:56:10 -0700304 *
bsalomonc21b09e2015-08-28 18:46:56 -0700305 * The GrContext may be used by the to create textures that are required by the returned
306 * processor.
bsalomonf1b7a1d2015-09-28 06:26:28 -0700307 *
308 * The returned GrFragmentProcessor should expect an unpremultiplied input color and
309 * produce a premultiplied output.
rileya@google.com03c1c352012-07-20 20:02:43 +0000310 */
bsalomonc21b09e2015-08-28 18:46:56 -0700311 virtual const GrFragmentProcessor* asFragmentProcessor(GrContext*,
312 const SkMatrix& viewMatrix,
313 const SkMatrix* localMatrix,
bsalomon4a339522015-10-06 08:40:50 -0700314 SkFilterQuality) const;
rileya@google.com03c1c352012-07-20 20:02:43 +0000315
reed8367b8c2014-08-22 08:30:20 -0700316 /**
317 * If the shader can represent its "average" luminance in a single color, return true and
318 * if color is not NULL, return that color. If it cannot, return false and ignore the color
319 * parameter.
320 *
321 * Note: if this returns true, the returned color will always be opaque, as only the RGB
322 * components are used to compute luminance.
323 */
324 bool asLuminanceColor(SkColor*) const;
325
commit-bot@chromium.org79590552014-05-13 18:14:45 +0000326#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
327 /**
328 * If the shader is a custom shader which has data the caller might want, call this function
329 * to get that data.
330 */
scroggo01c412e2014-11-24 09:05:35 -0800331 virtual bool asACustomShader(void** /* customData */) const { return false; }
commit-bot@chromium.org79590552014-05-13 18:14:45 +0000332#endif
333
reed@android.com8a1c16f2008-12-17 15:59:43 +0000334 //////////////////////////////////////////////////////////////////////////
reedf880e452015-12-30 13:39:41 -0800335 // Methods to create combinations or variants of shaders
reed@android.com8a1c16f2008-12-17 15:59:43 +0000336
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000337 /**
reedf880e452015-12-30 13:39:41 -0800338 * Return a shader that will apply the specified localMatrix to this shader.
339 * The specified matrix will be applied before any matrix associated with this shader.
340 */
reed150835e2016-03-10 06:36:49 -0800341 sk_sp<SkShader> makeWithLocalMatrix(const SkMatrix&) const;
reed3061af42016-01-07 15:47:29 -0800342
343 /**
344 * Create a new shader that produces the same colors as invoking this shader and then applying
345 * the colorfilter.
346 */
reed150835e2016-03-10 06:36:49 -0800347 sk_sp<SkShader> makeWithColorFilter(SkColorFilter*) const;
reeda2b340f2016-02-10 08:53:15 -0800348
reedf880e452015-12-30 13:39:41 -0800349 //////////////////////////////////////////////////////////////////////////
350 // Factory methods for stock shaders
351
352 /**
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000353 * Call this to create a new "empty" shader, that will not draw anything.
354 */
reed8a21c9f2016-03-08 18:50:00 -0800355 static sk_sp<SkShader> MakeEmptyShader();
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000356
reed8367b8c2014-08-22 08:30:20 -0700357 /**
358 * Call this to create a new shader that just draws the specified color. This should always
359 * draw the same as a paint with this color (and no shader).
360 */
reed8a21c9f2016-03-08 18:50:00 -0800361 static sk_sp<SkShader> MakeColorShader(SkColor);
reed8367b8c2014-08-22 08:30:20 -0700362
reed8a21c9f2016-03-08 18:50:00 -0800363 static sk_sp<SkShader> MakeComposeShader(sk_sp<SkShader> dst, sk_sp<SkShader> src,
364 SkXfermode::Mode);
365
366#ifdef SK_SUPPORT_LEGACY_CREATESHADER_PTR
367 static SkShader* CreateEmptyShader() { return MakeEmptyShader().release(); }
368 static SkShader* CreateColorShader(SkColor c) { return MakeColorShader(c).release(); }
369 static SkShader* CreateBitmapShader(const SkBitmap& src, TileMode tmx, TileMode tmy,
370 const SkMatrix* localMatrix = nullptr) {
371 return MakeBitmapShader(src, tmx, tmy, localMatrix).release();
372 }
373 static SkShader* CreateComposeShader(SkShader* dst, SkShader* src, SkXfermode::Mode mode);
374 static SkShader* CreateComposeShader(SkShader* dst, SkShader* src, SkXfermode* xfer);
375 static SkShader* CreatePictureShader(const SkPicture* src, TileMode tmx, TileMode tmy,
376 const SkMatrix* localMatrix, const SkRect* tile);
reed150835e2016-03-10 06:36:49 -0800377
378 SkShader* newWithLocalMatrix(const SkMatrix& matrix) const {
379 return this->makeWithLocalMatrix(matrix).release();
380 }
381 SkShader* newWithColorFilter(SkColorFilter* filter) const {
382 return this->makeWithColorFilter(filter).release();
383 }
reed8a21c9f2016-03-08 18:50:00 -0800384#endif
reeda2b340f2016-02-10 08:53:15 -0800385
386 /**
387 * Create a new compose shader, given shaders dst, src, and a combining xfermode mode.
388 * The xfermode is called with the output of the two shaders, and its output is returned.
389 * If xfer is null, SkXfermode::kSrcOver_Mode is assumed.
390 *
reed8a21c9f2016-03-08 18:50:00 -0800391 * The caller is responsible for managing its reference-count for the xfer (if not null).
reeda2b340f2016-02-10 08:53:15 -0800392 */
reed8a21c9f2016-03-08 18:50:00 -0800393 static sk_sp<SkShader> MakeComposeShader(sk_sp<SkShader> dst, sk_sp<SkShader> src,
394 SkXfermode* xfer);
reeda2b340f2016-02-10 08:53:15 -0800395
reed@android.com8a1c16f2008-12-17 15:59:43 +0000396 /** Call this to create a new shader that will draw with the specified bitmap.
reed@google.com99c114e2012-05-03 20:14:26 +0000397 *
398 * If the bitmap cannot be used (e.g. has no pixels, or its dimensions
399 * exceed implementation limits (currently at 64K - 1)) then SkEmptyShader
400 * may be returned.
401 *
commit-bot@chromium.org91246b92013-12-05 15:43:19 +0000402 * If the src is kA8_Config then that mask will be colorized using the color on
403 * the paint.
404 *
reed@google.com99c114e2012-05-03 20:14:26 +0000405 * @param src The bitmap to use inside the shader
406 * @param tmx The tiling mode to use when sampling the bitmap in the x-direction.
407 * @param tmy The tiling mode to use when sampling the bitmap in the y-direction.
408 * @return Returns a new shader object. Note: this function never returns null.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000409 */
reed8a21c9f2016-03-08 18:50:00 -0800410 static sk_sp<SkShader> MakeBitmapShader(const SkBitmap& src, TileMode tmx, TileMode tmy,
411 const SkMatrix* localMatrix = nullptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000412
halcanarya5f46e12015-09-08 07:12:25 -0700413 // NOTE: You can create an SkImage Shader with SkImage::newShader().
414
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000415 /** Call this to create a new shader that will draw with the specified picture.
416 *
417 * @param src The picture to use inside the shader (if not NULL, its ref count
commit-bot@chromium.org855e88e2014-04-21 19:33:12 +0000418 * is incremented). The SkPicture must not be changed after
419 * successfully creating a picture shader.
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000420 * @param tmx The tiling mode to use when sampling the bitmap in the x-direction.
421 * @param tmy The tiling mode to use when sampling the bitmap in the y-direction.
fmalitab5f78262014-08-06 13:07:15 -0700422 * @param tile The tile rectangle in picture coordinates: this represents the subset
423 * (or superset) of the picture used when building a tile. It is not
424 * affected by localMatrix and does not imply scaling (only translation
425 * and cropping). If null, the tile rect is considered equal to the picture
426 * bounds.
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000427 * @return Returns a new shader object. Note: this function never returns null.
428 */
reed7fb4f8b2016-03-11 04:33:52 -0800429 static sk_sp<SkShader> MakePictureShader(sk_sp<SkPicture> src, TileMode tmx, TileMode tmy,
reed8a21c9f2016-03-08 18:50:00 -0800430 const SkMatrix* localMatrix, const SkRect* tile);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000431
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +0000432 /**
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +0000433 * If this shader can be represented by another shader + a localMatrix, return that shader
434 * and, if not NULL, the localMatrix. If not, return NULL and ignore the localMatrix parameter.
435 *
436 * Note: the returned shader (if not NULL) will have been ref'd, and it is the responsibility
437 * of the caller to balance that with unref() when they are done.
438 */
439 virtual SkShader* refAsALocalMatrixShader(SkMatrix* localMatrix) const;
440
robertphillips0a482f42015-01-26 07:00:04 -0800441 SK_TO_STRING_VIRT()
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +0000442 SK_DEFINE_FLATTENABLE_TYPE(SkShader)
443
reed@android.com8a1c16f2008-12-17 15:59:43 +0000444protected:
mtklein36352bf2015-03-25 18:17:31 -0700445 void flatten(SkWriteBuffer&) const override;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000446
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000447 bool computeTotalInverse(const ContextRec&, SkMatrix* totalInverse) const;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000448
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000449 /**
450 * Your subclass must also override contextSize() if it overrides onCreateContext().
451 * Base class impl returns NULL.
452 */
453 virtual Context* onCreateContext(const ContextRec&, void* storage) const;
454
reed773ceda2016-03-03 18:18:25 -0800455 /**
456 * Override this if your subclass overrides createContext, to return the correct size of
457 * your subclass' context.
458 */
459 virtual size_t onContextSize(const ContextRec&) const;
460
reed8367b8c2014-08-22 08:30:20 -0700461 virtual bool onAsLuminanceColor(SkColor*) const {
462 return false;
463 }
reed0f0af232015-09-08 11:02:04 -0700464
465 virtual bool onIsABitmap(SkBitmap*, SkMatrix*, TileMode[2]) const {
466 return false;
467 }
468
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000469private:
scroggoef0fd612014-07-11 11:33:52 -0700470 // This is essentially const, but not officially so it can be modified in
471 // constructors.
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000472 SkMatrix fLocalMatrix;
scroggoc870d492014-07-11 10:42:12 -0700473
474 // So the SkLocalMatrixShader can whack fLocalMatrix in its SkReadBuffer constructor.
475 friend class SkLocalMatrixShader;
reed7a4d8472015-09-15 13:33:58 -0700476 friend class SkBitmapProcShader; // for computeTotalInverse()
scroggoc870d492014-07-11 10:42:12 -0700477
reed@android.com8a1c16f2008-12-17 15:59:43 +0000478 typedef SkFlattenable INHERITED;
479};
480
481#endif