blob: f9796d6c1a204804407bfd93c7d0ec720ed5947f [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;
reed0ccc62d2016-05-04 13:09:39 -070020class SkColorSpace;
reedf1ac1822016-08-01 11:24:14 -070021class SkImage;
reed@android.com8a1c16f2008-12-17 15:59:43 +000022class SkPath;
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +000023class SkPicture;
commit-bot@chromium.org79590552014-05-13 18:14:45 +000024class SkXfermode;
rileya@google.com03c1c352012-07-20 20:02:43 +000025class GrContext;
joshualittb0a8a372014-09-23 09:50:21 -070026class GrFragmentProcessor;
reed@android.com8a1c16f2008-12-17 15:59:43 +000027
28/** \class SkShader
reed@google.comad917992011-04-11 19:01:12 +000029 *
reed@google.com880dc472012-05-11 14:47:03 +000030 * Shaders specify the source color(s) for what is being drawn. If a paint
31 * has no shader, then the paint's color is used. If the paint has a
32 * shader, then the shader's color(s) are use instead, but they are
33 * modulated by the paint's alpha. This makes it easy to create a shader
34 * once (e.g. bitmap tiling or gradient) and then change its transparency
35 * w/o having to modify the original shader... only the paint's alpha needs
36 * to be modified.
reed@google.comad917992011-04-11 19:01:12 +000037 */
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +000038class SK_API SkShader : public SkFlattenable {
reed@android.com8a1c16f2008-12-17 15:59:43 +000039public:
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +000040 SkShader(const SkMatrix* localMatrix = NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +000041 virtual ~SkShader();
42
reed@google.comad917992011-04-11 19:01:12 +000043 /**
commit-bot@chromium.orgd12de022014-05-09 15:42:07 +000044 * Returns the local matrix.
scroggoc870d492014-07-11 10:42:12 -070045 *
46 * FIXME: This can be incorrect for a Shader with its own local matrix
47 * that is also wrapped via CreateLocalMatrixShader.
commit-bot@chromium.orgd12de022014-05-09 15:42:07 +000048 */
49 const SkMatrix& getLocalMatrix() const { return fLocalMatrix; }
50
reed@android.com8a1c16f2008-12-17 15:59:43 +000051 enum TileMode {
reed@google.com0beaba52012-03-16 14:38:06 +000052 /** replicate the edge color if the shader draws outside of its
53 * original bounds
54 */
55 kClamp_TileMode,
56
57 /** repeat the shader's image horizontally and vertically */
58 kRepeat_TileMode,
59
60 /** repeat the shader's image horizontally and vertically, alternating
61 * mirror images so that adjacent images always seam
62 */
63 kMirror_TileMode,
64
65#if 0
66 /** only draw within the original domain, return 0 everywhere else */
67 kDecal_TileMode,
68#endif
reed19c25f12015-03-15 14:01:21 -070069 };
reed@android.com8a1c16f2008-12-17 15:59:43 +000070
reed19c25f12015-03-15 14:01:21 -070071 enum {
72 kTileModeCount = kMirror_TileMode + 1
reed@android.com8a1c16f2008-12-17 15:59:43 +000073 };
74
75 // override these in your subclass
76
77 enum Flags {
78 //!< set if all of the colors will be opaque
reed4e5a7582016-01-05 05:10:33 -080079 kOpaqueAlpha_Flag = 1 << 0,
reed@android.com5119bdb2009-06-12 21:27:03 +000080
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +000081 /** set if the spans only vary in X (const in Y).
reed@android.com5119bdb2009-06-12 21:27:03 +000082 e.g. an Nx1 bitmap that is being tiled in Y, or a linear-gradient
reed@android.com3c9b2a42009-08-27 19:28:37 +000083 that varies from left-to-right. This flag specifies this for
84 shadeSpan().
reed@android.com5119bdb2009-06-12 21:27:03 +000085 */
reed4e5a7582016-01-05 05:10:33 -080086 kConstInY32_Flag = 1 << 1,
fmalitaca058f52016-02-23 19:02:20 -080087
88 /** hint for the blitter that 4f is the preferred shading mode.
89 */
90 kPrefers4f_Flag = 1 << 2,
reed@android.com8a1c16f2008-12-17 15:59:43 +000091 };
92
reed@google.comad917992011-04-11 19:01:12 +000093 /**
junov@chromium.orgb6e16192011-12-09 15:48:03 +000094 * Returns true if the shader is guaranteed to produce only opaque
95 * colors, subject to the SkPaint using the shader to apply an opaque
96 * alpha value. Subclasses should override this to allow some
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +000097 * optimizations.
junov@chromium.orgb6e16192011-12-09 15:48:03 +000098 */
99 virtual bool isOpaque() const { return false; }
100
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000101 /**
102 * ContextRec acts as a parameter bundle for creating Contexts.
103 */
104 struct ContextRec {
fmalitad0c4e092016-02-22 17:19:04 -0800105 enum DstType {
106 kPMColor_DstType, // clients prefer shading into PMColor dest
107 kPM4f_DstType, // clients prefer shading into PM4f dest
108 };
109
110 ContextRec(const SkPaint& paint, const SkMatrix& matrix, const SkMatrix* localM,
111 DstType dstType)
reed56263c72015-06-05 11:31:26 -0700112 : fPaint(&paint)
commit-bot@chromium.org80116dc2014-05-06 17:16:03 +0000113 , fMatrix(&matrix)
fmalitad0c4e092016-02-22 17:19:04 -0800114 , fLocalMatrix(localM)
115 , fPreferredDstType(dstType) {}
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000116
fmalitad0c4e092016-02-22 17:19:04 -0800117 const SkPaint* fPaint; // the current paint associated with the draw
118 const SkMatrix* fMatrix; // the current matrix in the canvas
119 const SkMatrix* fLocalMatrix; // optional local matrix
120 const DstType fPreferredDstType; // the "natural" client dest type
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000121 };
122
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000123 class Context : public ::SkNoncopyable {
124 public:
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000125 Context(const SkShader& shader, const ContextRec&);
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000126
127 virtual ~Context();
128
129 /**
130 * Called sometimes before drawing with this shader. Return the type of
131 * alpha your shader will return. The default implementation returns 0.
132 * Your subclass should override if it can (even sometimes) report a
133 * non-zero value, since that will enable various blitters to perform
134 * faster.
135 */
136 virtual uint32_t getFlags() const { return 0; }
137
138 /**
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000139 * Called for each span of the object being drawn. Your subclass should
140 * set the appropriate colors (with premultiplied alpha) that correspond
141 * to the specified device coordinates.
142 */
143 virtual void shadeSpan(int x, int y, SkPMColor[], int count) = 0;
144
reed6d3cef92016-01-22 01:04:29 -0800145 virtual void shadeSpan4f(int x, int y, SkPM4f[], int count);
146
reed58fc94e2016-03-18 12:42:26 -0700147 struct BlitState;
148 typedef void (*BlitBW)(BlitState*,
149 int x, int y, const SkPixmap&, int count);
150 typedef void (*BlitAA)(BlitState*,
151 int x, int y, const SkPixmap&, int count, const SkAlpha[]);
152
reed830dfd82016-03-16 12:29:01 -0700153 struct BlitState {
reed58fc94e2016-03-18 12:42:26 -0700154 // inputs
reed830dfd82016-03-16 12:29:01 -0700155 Context* fCtx;
156 SkXfermode* fXfer;
reed58fc94e2016-03-18 12:42:26 -0700157
158 // outputs
reed830dfd82016-03-16 12:29:01 -0700159 enum { N = 2 };
160 void* fStorage[N];
reed58fc94e2016-03-18 12:42:26 -0700161 BlitBW fBlitBW;
162 BlitAA fBlitAA;
reed830dfd82016-03-16 12:29:01 -0700163 };
reed58fc94e2016-03-18 12:42:26 -0700164
165 // Returns true if one or more of the blitprocs are set in the BlitState
166 bool chooseBlitProcs(const SkImageInfo& info, BlitState* state) {
167 state->fBlitBW = nullptr;
168 state->fBlitAA = nullptr;
169 if (this->onChooseBlitProcs(info, state)) {
170 SkASSERT(state->fBlitBW || state->fBlitAA);
171 return true;
172 }
173 return false;
reed830dfd82016-03-16 12:29:01 -0700174 }
reed58fc94e2016-03-18 12:42:26 -0700175
herbc7a784c2015-12-18 09:52:15 -0800176 /**
177 * The const void* ctx is only const because all the implementations are const.
178 * This can be changed to non-const if a new shade proc needs to change the ctx.
179 */
180 typedef void (*ShadeProc)(const void* ctx, int x, int y, SkPMColor[], int count);
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000181 virtual ShadeProc asAShadeProc(void** ctx);
182
183 /**
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000184 * Similar to shadeSpan, but only returns the alpha-channel for a span.
185 * The default implementation calls shadeSpan() and then extracts the alpha
186 * values from the returned colors.
187 */
188 virtual void shadeSpanAlpha(int x, int y, uint8_t alpha[], int count);
189
reedcc0e3112014-09-10 10:20:24 -0700190 // Notification from blitter::blitMask in case we need to see the non-alpha channels
191 virtual void set3DMask(const SkMask*) {}
192
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000193 protected:
194 // Reference to shader, so we don't have to dupe information.
195 const SkShader& fShader;
196
197 enum MatrixClass {
198 kLinear_MatrixClass, // no perspective
199 kFixedStepInX_MatrixClass, // fast perspective, need to call fixedStepInX() each
200 // scanline
201 kPerspective_MatrixClass // slow perspective, need to mappoints each pixel
202 };
203 static MatrixClass ComputeMatrixClass(const SkMatrix&);
204
commit-bot@chromium.org80116dc2014-05-06 17:16:03 +0000205 uint8_t getPaintAlpha() const { return fPaintAlpha; }
206 const SkMatrix& getTotalInverse() const { return fTotalInverse; }
207 MatrixClass getInverseClass() const { return (MatrixClass)fTotalInverseClass; }
208 const SkMatrix& getCTM() const { return fCTM; }
reed830dfd82016-03-16 12:29:01 -0700209
reed58fc94e2016-03-18 12:42:26 -0700210 virtual bool onChooseBlitProcs(const SkImageInfo&, BlitState*) { return false; }
reed830dfd82016-03-16 12:29:01 -0700211
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000212 private:
commit-bot@chromium.org80116dc2014-05-06 17:16:03 +0000213 SkMatrix fCTM;
214 SkMatrix fTotalInverse;
215 uint8_t fPaintAlpha;
216 uint8_t fTotalInverseClass;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000217
218 typedef SkNoncopyable INHERITED;
219 };
reed@google.com7c2f27d2011-03-07 19:29:00 +0000220
reed@google.comad917992011-04-11 19:01:12 +0000221 /**
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000222 * Create the actual object that does the shading.
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000223 * Size of storage must be >= contextSize.
reed@google.coma641f3f2012-12-13 22:16:30 +0000224 */
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000225 Context* createContext(const ContextRec&, void* storage) const;
reed@google.coma641f3f2012-12-13 22:16:30 +0000226
227 /**
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000228 * Return the size of a Context returned by createContext.
reed@google.comad917992011-04-11 19:01:12 +0000229 */
reed773ceda2016-03-03 18:18:25 -0800230 size_t contextSize(const ContextRec&) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000231
Mike Reed627778d2016-09-28 17:13:38 -0400232#ifdef SK_SUPPORT_LEGACY_SHADER_ISABITMAP
reed@google.comad917992011-04-11 19:01:12 +0000233 /**
reed0f0af232015-09-08 11:02:04 -0700234 * Returns true if this shader is just a bitmap, and if not null, returns the bitmap,
235 * localMatrix, and tilemodes. If this is not a bitmap, returns false and ignores the
236 * out-parameters.
reed@android.comf2b98d62010-12-20 18:26:13 +0000237 */
reed0f0af232015-09-08 11:02:04 -0700238 bool isABitmap(SkBitmap* outTexture, SkMatrix* outMatrix, TileMode xy[2]) const {
239 return this->onIsABitmap(outTexture, outMatrix, xy);
scroggoff390c92015-09-08 06:24:08 -0700240 }
reed0f0af232015-09-08 11:02:04 -0700241
reedf5822822015-08-19 11:46:38 -0700242 bool isABitmap() const {
243 return this->isABitmap(nullptr, nullptr, nullptr);
244 }
Mike Reed627778d2016-09-28 17:13:38 -0400245#endif
reedf5822822015-08-19 11:46:38 -0700246
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000247 /**
reedf1ac1822016-08-01 11:24:14 -0700248 * Iff this shader is backed by a single SkImage, return its ptr (the caller must ref this
249 * if they want to keep it longer than the lifetime of the shader). If not, return nullptr.
250 */
251 SkImage* isAImage(SkMatrix* localMatrix, TileMode xy[2]) const {
252 return this->onIsAImage(localMatrix, xy);
253 }
254
255 bool isAImage() const {
256 return this->isAImage(nullptr, nullptr) != nullptr;
257 }
258
259 /**
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000260 * If the shader subclass can be represented as a gradient, asAGradient
261 * returns the matching GradientType enum (or kNone_GradientType if it
262 * cannot). Also, if info is not null, asAGradient populates info with
263 * the relevant (see below) parameters for the gradient. fColorCount
264 * is both an input and output parameter. On input, it indicates how
265 * many entries in fColors and fColorOffsets can be used, if they are
266 * non-NULL. After asAGradient has run, fColorCount indicates how
267 * many color-offset pairs there are in the gradient. If there is
268 * insufficient space to store all of the color-offset pairs, fColors
269 * and fColorOffsets will not be altered. fColorOffsets specifies
270 * where on the range of 0 to 1 to transition to the given color.
271 * The meaning of fPoint and fRadius is dependant on the type of gradient.
272 *
273 * None:
274 * info is ignored.
275 * Color:
276 * fColorOffsets[0] is meaningless.
277 * Linear:
278 * fPoint[0] and fPoint[1] are the end-points of the gradient
279 * Radial:
280 * fPoint[0] and fRadius[0] are the center and radius
reed71a6cbf2015-05-04 08:32:51 -0700281 * Conical:
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000282 * fPoint[0] and fRadius[0] are the center and radius of the 1st circle
283 * fPoint[1] and fRadius[1] are the center and radius of the 2nd circle
284 * Sweep:
285 * fPoint[0] is the center of the sweep.
286 */
287
288 enum GradientType {
289 kNone_GradientType,
290 kColor_GradientType,
291 kLinear_GradientType,
292 kRadial_GradientType,
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000293 kSweep_GradientType,
reed@google.com83226972012-06-07 20:26:47 +0000294 kConical_GradientType,
295 kLast_GradientType = kConical_GradientType
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000296 };
297
298 struct GradientInfo {
299 int fColorCount; //!< In-out parameter, specifies passed size
300 // of fColors/fColorOffsets on input, and
301 // actual number of colors/offsets on
302 // output.
303 SkColor* fColors; //!< The colors in the gradient.
304 SkScalar* fColorOffsets; //!< The unit offset for color transitions.
305 SkPoint fPoint[2]; //!< Type specific, see above.
306 SkScalar fRadius[2]; //!< Type specific, see above.
307 TileMode fTileMode; //!< The tile mode used.
reed@google.com3d3a8602013-05-24 14:58:44 +0000308 uint32_t fGradientFlags; //!< see SkGradientShader::Flags
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000309 };
310
311 virtual GradientType asAGradient(GradientInfo* info) const;
312
rileya@google.com03c1c352012-07-20 20:02:43 +0000313 /**
commit-bot@chromium.org79590552014-05-13 18:14:45 +0000314 * If the shader subclass is composed of two shaders, return true, and if rec is not NULL,
315 * fill it out with info about the shader.
commit-bot@chromium.org30558792014-05-14 14:28:34 +0000316 *
317 * These are bare pointers; the ownership and reference count are unchanged.
commit-bot@chromium.org79590552014-05-13 18:14:45 +0000318 */
319
320 struct ComposeRec {
321 const SkShader* fShaderA;
322 const SkShader* fShaderB;
323 const SkXfermode* fMode;
324 };
325
djsollenc87dd2c2014-11-14 11:11:46 -0800326 virtual bool asACompose(ComposeRec*) const { return false; }
commit-bot@chromium.org79590552014-05-13 18:14:45 +0000327
bungeman06ca8ec2016-06-09 08:01:03 -0700328#if SK_SUPPORT_GPU
brianosman839345d2016-07-22 11:04:53 -0700329 struct AsFPArgs {
Brian Osman9f532a32016-10-19 11:12:09 -0400330 AsFPArgs() {}
brianosman839345d2016-07-22 11:04:53 -0700331 AsFPArgs(GrContext* context,
332 const SkMatrix* viewMatrix,
333 const SkMatrix* localMatrix,
334 SkFilterQuality filterQuality,
brianosman1638c0d2016-07-25 05:12:53 -0700335 SkColorSpace* dstColorSpace,
brianosman839345d2016-07-22 11:04:53 -0700336 SkSourceGammaTreatment gammaTreatment)
337 : fContext(context)
338 , fViewMatrix(viewMatrix)
339 , fLocalMatrix(localMatrix)
340 , fFilterQuality(filterQuality)
brianosman1638c0d2016-07-25 05:12:53 -0700341 , fDstColorSpace(dstColorSpace)
brianosman839345d2016-07-22 11:04:53 -0700342 , fGammaTreatment(gammaTreatment) {}
343
344 GrContext* fContext;
345 const SkMatrix* fViewMatrix;
346 const SkMatrix* fLocalMatrix;
347 SkFilterQuality fFilterQuality;
brianosman1638c0d2016-07-25 05:12:53 -0700348 SkColorSpace* fDstColorSpace;
brianosman839345d2016-07-22 11:04:53 -0700349 SkSourceGammaTreatment fGammaTreatment;
350 };
351
commit-bot@chromium.org79590552014-05-13 18:14:45 +0000352 /**
bsalomonc21b09e2015-08-28 18:46:56 -0700353 * Returns a GrFragmentProcessor that implements the shader for the GPU backend. NULL is
354 * returned if there is no GPU implementation.
bsalomon83d081a2014-07-08 09:56:10 -0700355 *
bsalomonc21b09e2015-08-28 18:46:56 -0700356 * The GPU device does not call SkShader::createContext(), instead we pass the view matrix,
357 * local matrix, and filter quality directly.
bsalomon83d081a2014-07-08 09:56:10 -0700358 *
bsalomonc21b09e2015-08-28 18:46:56 -0700359 * The GrContext may be used by the to create textures that are required by the returned
360 * processor.
bsalomonf1b7a1d2015-09-28 06:26:28 -0700361 *
362 * The returned GrFragmentProcessor should expect an unpremultiplied input color and
363 * produce a premultiplied output.
rileya@google.com03c1c352012-07-20 20:02:43 +0000364 */
brianosman839345d2016-07-22 11:04:53 -0700365 virtual sk_sp<GrFragmentProcessor> asFragmentProcessor(const AsFPArgs&) const;
bungeman06ca8ec2016-06-09 08:01:03 -0700366#endif
rileya@google.com03c1c352012-07-20 20:02:43 +0000367
reed8367b8c2014-08-22 08:30:20 -0700368 /**
369 * If the shader can represent its "average" luminance in a single color, return true and
370 * if color is not NULL, return that color. If it cannot, return false and ignore the color
371 * parameter.
372 *
373 * Note: if this returns true, the returned color will always be opaque, as only the RGB
374 * components are used to compute luminance.
375 */
376 bool asLuminanceColor(SkColor*) const;
377
commit-bot@chromium.org79590552014-05-13 18:14:45 +0000378#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
379 /**
380 * If the shader is a custom shader which has data the caller might want, call this function
381 * to get that data.
382 */
scroggo01c412e2014-11-24 09:05:35 -0800383 virtual bool asACustomShader(void** /* customData */) const { return false; }
commit-bot@chromium.org79590552014-05-13 18:14:45 +0000384#endif
385
reed@android.com8a1c16f2008-12-17 15:59:43 +0000386 //////////////////////////////////////////////////////////////////////////
reedf880e452015-12-30 13:39:41 -0800387 // Methods to create combinations or variants of shaders
reed@android.com8a1c16f2008-12-17 15:59:43 +0000388
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000389 /**
reedf880e452015-12-30 13:39:41 -0800390 * Return a shader that will apply the specified localMatrix to this shader.
391 * The specified matrix will be applied before any matrix associated with this shader.
392 */
reed150835e2016-03-10 06:36:49 -0800393 sk_sp<SkShader> makeWithLocalMatrix(const SkMatrix&) const;
reed3061af42016-01-07 15:47:29 -0800394
395 /**
396 * Create a new shader that produces the same colors as invoking this shader and then applying
397 * the colorfilter.
398 */
reedd053ce92016-03-22 10:17:23 -0700399 sk_sp<SkShader> makeWithColorFilter(sk_sp<SkColorFilter>) const;
reeda2b340f2016-02-10 08:53:15 -0800400
reedf880e452015-12-30 13:39:41 -0800401 //////////////////////////////////////////////////////////////////////////
402 // Factory methods for stock shaders
403
404 /**
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000405 * Call this to create a new "empty" shader, that will not draw anything.
406 */
reed8a21c9f2016-03-08 18:50:00 -0800407 static sk_sp<SkShader> MakeEmptyShader();
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000408
reed8367b8c2014-08-22 08:30:20 -0700409 /**
410 * Call this to create a new shader that just draws the specified color. This should always
411 * draw the same as a paint with this color (and no shader).
412 */
reed8a21c9f2016-03-08 18:50:00 -0800413 static sk_sp<SkShader> MakeColorShader(SkColor);
reed8367b8c2014-08-22 08:30:20 -0700414
reed0ccc62d2016-05-04 13:09:39 -0700415 /**
416 * Create a shader that draws the specified color (in the specified colorspace).
417 *
418 * This works around the limitation that SkPaint::setColor() only takes byte values, and does
419 * not support specific colorspaces.
420 */
421 static sk_sp<SkShader> MakeColorShader(const SkColor4f&, sk_sp<SkColorSpace>);
422
reed8a21c9f2016-03-08 18:50:00 -0800423 static sk_sp<SkShader> MakeComposeShader(sk_sp<SkShader> dst, sk_sp<SkShader> src,
424 SkXfermode::Mode);
425
426#ifdef SK_SUPPORT_LEGACY_CREATESHADER_PTR
427 static SkShader* CreateEmptyShader() { return MakeEmptyShader().release(); }
428 static SkShader* CreateColorShader(SkColor c) { return MakeColorShader(c).release(); }
429 static SkShader* CreateBitmapShader(const SkBitmap& src, TileMode tmx, TileMode tmy,
430 const SkMatrix* localMatrix = nullptr) {
431 return MakeBitmapShader(src, tmx, tmy, localMatrix).release();
432 }
433 static SkShader* CreateComposeShader(SkShader* dst, SkShader* src, SkXfermode::Mode mode);
434 static SkShader* CreateComposeShader(SkShader* dst, SkShader* src, SkXfermode* xfer);
435 static SkShader* CreatePictureShader(const SkPicture* src, TileMode tmx, TileMode tmy,
436 const SkMatrix* localMatrix, const SkRect* tile);
reed150835e2016-03-10 06:36:49 -0800437
438 SkShader* newWithLocalMatrix(const SkMatrix& matrix) const {
439 return this->makeWithLocalMatrix(matrix).release();
440 }
reedd053ce92016-03-22 10:17:23 -0700441 SkShader* newWithColorFilter(SkColorFilter* filter) const;
reed8a21c9f2016-03-08 18:50:00 -0800442#endif
reeda2b340f2016-02-10 08:53:15 -0800443
444 /**
445 * Create a new compose shader, given shaders dst, src, and a combining xfermode mode.
446 * The xfermode is called with the output of the two shaders, and its output is returned.
447 * If xfer is null, SkXfermode::kSrcOver_Mode is assumed.
448 *
reed8a21c9f2016-03-08 18:50:00 -0800449 * The caller is responsible for managing its reference-count for the xfer (if not null).
reeda2b340f2016-02-10 08:53:15 -0800450 */
reed8a21c9f2016-03-08 18:50:00 -0800451 static sk_sp<SkShader> MakeComposeShader(sk_sp<SkShader> dst, sk_sp<SkShader> src,
reedcfb6bdf2016-03-29 11:32:50 -0700452 sk_sp<SkXfermode> xfer);
453#ifdef SK_SUPPORT_LEGACY_XFERMODE_PTR
454 static sk_sp<SkShader> MakeComposeShader(sk_sp<SkShader> dst, sk_sp<SkShader> src,
reed8a21c9f2016-03-08 18:50:00 -0800455 SkXfermode* xfer);
reedcfb6bdf2016-03-29 11:32:50 -0700456#endif
reeda2b340f2016-02-10 08:53:15 -0800457
reed@android.com8a1c16f2008-12-17 15:59:43 +0000458 /** Call this to create a new shader that will draw with the specified bitmap.
reed@google.com99c114e2012-05-03 20:14:26 +0000459 *
460 * If the bitmap cannot be used (e.g. has no pixels, or its dimensions
461 * exceed implementation limits (currently at 64K - 1)) then SkEmptyShader
462 * may be returned.
463 *
commit-bot@chromium.org91246b92013-12-05 15:43:19 +0000464 * If the src is kA8_Config then that mask will be colorized using the color on
465 * the paint.
466 *
reed@google.com99c114e2012-05-03 20:14:26 +0000467 * @param src The bitmap to use inside the shader
468 * @param tmx The tiling mode to use when sampling the bitmap in the x-direction.
469 * @param tmy The tiling mode to use when sampling the bitmap in the y-direction.
470 * @return Returns a new shader object. Note: this function never returns null.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000471 */
reed8a21c9f2016-03-08 18:50:00 -0800472 static sk_sp<SkShader> MakeBitmapShader(const SkBitmap& src, TileMode tmx, TileMode tmy,
473 const SkMatrix* localMatrix = nullptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000474
halcanarya5f46e12015-09-08 07:12:25 -0700475 // NOTE: You can create an SkImage Shader with SkImage::newShader().
476
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000477 /** Call this to create a new shader that will draw with the specified picture.
478 *
479 * @param src The picture to use inside the shader (if not NULL, its ref count
commit-bot@chromium.org855e88e2014-04-21 19:33:12 +0000480 * is incremented). The SkPicture must not be changed after
481 * successfully creating a picture shader.
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000482 * @param tmx The tiling mode to use when sampling the bitmap in the x-direction.
483 * @param tmy The tiling mode to use when sampling the bitmap in the y-direction.
fmalitab5f78262014-08-06 13:07:15 -0700484 * @param tile The tile rectangle in picture coordinates: this represents the subset
485 * (or superset) of the picture used when building a tile. It is not
486 * affected by localMatrix and does not imply scaling (only translation
487 * and cropping). If null, the tile rect is considered equal to the picture
488 * bounds.
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000489 * @return Returns a new shader object. Note: this function never returns null.
490 */
reed7fb4f8b2016-03-11 04:33:52 -0800491 static sk_sp<SkShader> MakePictureShader(sk_sp<SkPicture> src, TileMode tmx, TileMode tmy,
reed8a21c9f2016-03-08 18:50:00 -0800492 const SkMatrix* localMatrix, const SkRect* tile);
commit-bot@chromium.orgc5d9bb02014-04-08 15:19:34 +0000493
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +0000494 /**
commit-bot@chromium.org8fae2132014-05-07 22:26:37 +0000495 * If this shader can be represented by another shader + a localMatrix, return that shader
496 * and, if not NULL, the localMatrix. If not, return NULL and ignore the localMatrix parameter.
497 *
498 * Note: the returned shader (if not NULL) will have been ref'd, and it is the responsibility
499 * of the caller to balance that with unref() when they are done.
500 */
501 virtual SkShader* refAsALocalMatrixShader(SkMatrix* localMatrix) const;
502
robertphillips0a482f42015-01-26 07:00:04 -0800503 SK_TO_STRING_VIRT()
mtklein3b375452016-04-04 14:57:19 -0700504 SK_DEFINE_FLATTENABLE_TYPE(SkShader)
reed320a40d2016-08-02 06:12:06 -0700505 SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP()
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +0000506
reed@android.com8a1c16f2008-12-17 15:59:43 +0000507protected:
mtklein36352bf2015-03-25 18:17:31 -0700508 void flatten(SkWriteBuffer&) const override;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000509
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000510 bool computeTotalInverse(const ContextRec&, SkMatrix* totalInverse) const;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000511
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000512 /**
513 * Your subclass must also override contextSize() if it overrides onCreateContext().
514 * Base class impl returns NULL.
515 */
516 virtual Context* onCreateContext(const ContextRec&, void* storage) const;
517
reed773ceda2016-03-03 18:18:25 -0800518 /**
519 * Override this if your subclass overrides createContext, to return the correct size of
520 * your subclass' context.
521 */
522 virtual size_t onContextSize(const ContextRec&) const;
523
reed8367b8c2014-08-22 08:30:20 -0700524 virtual bool onAsLuminanceColor(SkColor*) const {
525 return false;
526 }
reed0f0af232015-09-08 11:02:04 -0700527
Mike Reed627778d2016-09-28 17:13:38 -0400528#ifdef SK_SUPPORT_LEGACY_SHADER_ISABITMAP
reed0f0af232015-09-08 11:02:04 -0700529 virtual bool onIsABitmap(SkBitmap*, SkMatrix*, TileMode[2]) const {
530 return false;
531 }
Mike Reed627778d2016-09-28 17:13:38 -0400532#endif
reed0f0af232015-09-08 11:02:04 -0700533
reedf1ac1822016-08-01 11:24:14 -0700534 virtual SkImage* onIsAImage(SkMatrix*, TileMode[2]) const {
535 return nullptr;
536 }
537
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000538private:
scroggoef0fd612014-07-11 11:33:52 -0700539 // This is essentially const, but not officially so it can be modified in
540 // constructors.
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000541 SkMatrix fLocalMatrix;
scroggoc870d492014-07-11 10:42:12 -0700542
543 // So the SkLocalMatrixShader can whack fLocalMatrix in its SkReadBuffer constructor.
544 friend class SkLocalMatrixShader;
reed320a40d2016-08-02 06:12:06 -0700545 friend class SkBitmapProcLegacyShader; // for computeTotalInverse()
scroggoc870d492014-07-11 10:42:12 -0700546
reed@android.com8a1c16f2008-12-17 15:59:43 +0000547 typedef SkFlattenable INHERITED;
548};
549
550#endif