blob: b650a03d7b044743d7ace5150ecd11c27d5e24fc [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkColor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkFlattenable.h"
Kevin Lubick8a0152a2023-06-05 11:41:39 -040013#include "include/core/SkRefCnt.h"
14#include "include/private/base/SkAPI.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015
Mike Reeda2a85e42021-07-26 15:29:43 -040016class SkBlender;
reed3061af42016-01-07 15:47:29 -080017class SkColorFilter;
reed0ccc62d2016-05-04 13:09:39 -070018class SkColorSpace;
reedf1ac1822016-08-01 11:24:14 -070019class SkImage;
Kevin Lubick8a0152a2023-06-05 11:41:39 -040020class SkMatrix;
21enum class SkBlendMode;
22enum class SkTileMode;
23struct SkRect;
Robert Phillips3ffa3952024-03-20 12:29:55 -040024struct SkSamplingOptions;
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:
Florin Malita4aed1382017-05-25 10:38:07 -040038 /**
junov@chromium.orgb6e16192011-12-09 15:48:03 +000039 * Returns true if the shader is guaranteed to produce only opaque
40 * colors, subject to the SkPaint using the shader to apply an opaque
41 * alpha value. Subclasses should override this to allow some
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +000042 * optimizations.
junov@chromium.orgb6e16192011-12-09 15:48:03 +000043 */
44 virtual bool isOpaque() const { return false; }
45
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +000046 /**
reedf1ac1822016-08-01 11:24:14 -070047 * Iff this shader is backed by a single SkImage, return its ptr (the caller must ref this
48 * if they want to keep it longer than the lifetime of the shader). If not, return nullptr.
49 */
Mike Reedfae8fce2019-04-03 10:27:45 -040050 SkImage* isAImage(SkMatrix* localMatrix, SkTileMode xy[2]) const;
reedf1ac1822016-08-01 11:24:14 -070051
52 bool isAImage() const {
Mike Reedfae8fce2019-04-03 10:27:45 -040053 return this->isAImage(nullptr, (SkTileMode*)nullptr) != nullptr;
reedf1ac1822016-08-01 11:24:14 -070054 }
55
reed@android.com8a1c16f2008-12-17 15:59:43 +000056 //////////////////////////////////////////////////////////////////////////
reedf880e452015-12-30 13:39:41 -080057 // Methods to create combinations or variants of shaders
reed@android.com8a1c16f2008-12-17 15:59:43 +000058
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +000059 /**
reedf880e452015-12-30 13:39:41 -080060 * Return a shader that will apply the specified localMatrix to this shader.
61 * The specified matrix will be applied before any matrix associated with this shader.
62 */
reed150835e2016-03-10 06:36:49 -080063 sk_sp<SkShader> makeWithLocalMatrix(const SkMatrix&) const;
reed3061af42016-01-07 15:47:29 -080064
65 /**
66 * Create a new shader that produces the same colors as invoking this shader and then applying
67 * the colorfilter.
68 */
reedd053ce92016-03-22 10:17:23 -070069 sk_sp<SkShader> makeWithColorFilter(sk_sp<SkColorFilter>) const;
reeda2b340f2016-02-10 08:53:15 -080070
Brian Osman611f0892023-09-13 19:03:21 +000071 /**
72 * Return a shader that will compute this shader in a specific color space.
73 * By default, all shaders operate in the destination (surface) color space.
74 * The results of a shader are still always converted to the destination - this
75 * API has no impact on simple shaders or images. Primarily, it impacts shaders
76 * that perform mathematical operations, like Blend shaders, or runtime shaders.
77 */
78 sk_sp<SkShader> makeWithWorkingColorSpace(sk_sp<SkColorSpace>) const;
79
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +000080private:
Florin Malitaf7beee72017-05-26 12:54:32 -040081 SkShader() = default;
82 friend class SkShaderBase;
83
John Stiles7571f9e2020-09-02 22:42:33 -040084 using INHERITED = SkFlattenable;
reed@android.com8a1c16f2008-12-17 15:59:43 +000085};
86
Kevin Lubick8a0152a2023-06-05 11:41:39 -040087namespace SkShaders {
88SK_API sk_sp<SkShader> Empty();
89SK_API sk_sp<SkShader> Color(SkColor);
90SK_API sk_sp<SkShader> Color(const SkColor4f&, sk_sp<SkColorSpace>);
91SK_API sk_sp<SkShader> Blend(SkBlendMode mode, sk_sp<SkShader> dst, sk_sp<SkShader> src);
92SK_API sk_sp<SkShader> Blend(sk_sp<SkBlender>, sk_sp<SkShader> dst, sk_sp<SkShader> src);
93SK_API sk_sp<SkShader> CoordClamp(sk_sp<SkShader>, const SkRect& subset);
Robert Phillips3ffa3952024-03-20 12:29:55 -040094
95/*
96 * Create an SkShader that will sample the 'image'. This is equivalent to SkImage::makeShader.
97 */
98SK_API sk_sp<SkShader> Image(sk_sp<SkImage> image,
99 SkTileMode tmx, SkTileMode tmy,
100 const SkSamplingOptions& options,
101 const SkMatrix* localMatrix = nullptr);
102/*
103 * Create an SkShader that will sample 'image' with minimal processing. This is equivalent to
104 * SkImage::makeRawShader.
105 */
106SK_API sk_sp<SkShader> RawImage(sk_sp<SkImage> image,
107 SkTileMode tmx, SkTileMode tmy,
108 const SkSamplingOptions& options,
109 const SkMatrix* localMatrix = nullptr);
Kevin Lubick8a0152a2023-06-05 11:41:39 -0400110}
Mike Reedc8bea7d2019-04-09 13:55:36 -0400111
reed@android.com8a1c16f2008-12-17 15:59:43 +0000112#endif