| 1 |
/* |
| 2 |
* Copyright 2006 The Android Open Source Project |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#ifndef SkShader_DEFINED |
| 9 |
#define SkShader_DEFINED |
| 10 |
|
| 11 |
#include "include/core/SkColor.h" |
| 12 |
#include "include/core/SkColorSpace.h" |
| 13 |
#include "include/core/SkFlattenable.h" |
| 14 |
#include "include/core/SkRefCnt.h" |
| 15 |
#include "include/private/base/SkAPI.h" |
| 16 |
|
| 17 |
class SkBlender; |
| 18 |
class SkColorFilter; |
| 19 |
class SkImage; |
| 20 |
class SkMatrix; |
| 21 |
enum class SkBlendMode; |
| 22 |
enum class SkTileMode; |
| 23 |
struct SkRect; |
| 24 |
struct SkSamplingOptions; |
| 25 |
|
| 26 |
/** \class SkShader |
| 27 |
* |
| 28 |
* Shaders specify the premultiplied source color(s) for what is being drawn. |
| 29 |
* If a paint has no shader, then the paint's color is used. If the paint has a |
| 30 |
* shader, then the shader's color(s) are used 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. |
| 35 |
*/ |
| 36 |
class SK_API SkShader : public SkFlattenable { |
| 37 |
public: |
| 38 |
/** |
| 39 |
* 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 |
| 42 |
* optimizations. |
| 43 |
*/ |
| 44 |
virtual bool isOpaque() const { return false; } |
| 45 |
|
| 46 |
/** |
| 47 |
* 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 |
*/ |
| 50 |
SkImage* isAImage(SkMatrix* localMatrix, SkTileMode xy[2]) const; |
| 51 |
|
| 52 |
bool isAImage() const { |
| 53 |
return this->isAImage(nullptr, (SkTileMode*)nullptr) != nullptr; |
| 54 |
} |
| 55 |
|
| 56 |
////////////////////////////////////////////////////////////////////////// |
| 57 |
// Methods to create combinations or variants of shaders |
| 58 |
|
| 59 |
/** |
| 60 |
* 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 |
*/ |
| 63 |
sk_sp<SkShader> makeWithLocalMatrix(const SkMatrix&) const; |
| 64 |
|
| 65 |
/** |
| 66 |
* Create a new shader that produces the same colors as invoking this shader and then applying |
| 67 |
* the colorfilter. |
| 68 |
*/ |
| 69 |
sk_sp<SkShader> makeWithColorFilter(sk_sp<SkColorFilter>) const; |
| 70 |
|
| 71 |
/** |
| 72 |
* Return a shader that will compute this shader in a context such that any child shaders |
| 73 |
* return RGBA values converted to the `inputCS` colorspace. |
| 74 |
* |
| 75 |
* It is then assumed that the RGBA values returned by this shader have been transformed into |
| 76 |
* `outputCS` by the shader being wrapped. By default, shaders are assumed to return values |
| 77 |
* in the destination colorspace and premultiplied. Using a different outputCS than inputCS |
| 78 |
* allows custom shaders to replace the color management Skia normally performs w/o forcing |
| 79 |
* authors to otherwise manipulate surface/image color info to avoid unnecessary or incorrect |
| 80 |
* work. |
| 81 |
* |
| 82 |
* If the shader is not performing colorspace conversion but needs to operate in the `inputCS` |
| 83 |
* then it should have `outputCS` be the same as `inputCS`. Regardless of the `outputCS` here, |
| 84 |
* the RGBA values of the returned SkShader are always converted from `outputCS` to the |
| 85 |
* destination surface color space. |
| 86 |
* |
| 87 |
* A null inputCS is assumed to be the destination CS. |
| 88 |
* A null outputCS is assumed to be the inputCS. |
| 89 |
*/ |
| 90 |
sk_sp<SkShader> makeWithWorkingColorSpace(sk_sp<SkColorSpace> inputCS, |
| 91 |
sk_sp<SkColorSpace> outputCS=nullptr) const; |
| 92 |
|
| 93 |
private: |
| 94 |
SkShader() = default; |
| 95 |
friend class SkShaderBase; |
| 96 |
|
| 97 |
using INHERITED = SkFlattenable; |
| 98 |
}; |
| 99 |
|
| 100 |
namespace SkShaders { |
| 101 |
SK_API sk_sp<SkShader> Empty(); |
| 102 |
SK_API sk_sp<SkShader> Color(SkColor); |
| 103 |
SK_API sk_sp<SkShader> Color(const SkColor4f&, sk_sp<SkColorSpace>); |
| 104 |
SK_API sk_sp<SkShader> Blend(SkBlendMode mode, sk_sp<SkShader> dst, sk_sp<SkShader> src); |
| 105 |
SK_API sk_sp<SkShader> Blend(sk_sp<SkBlender>, sk_sp<SkShader> dst, sk_sp<SkShader> src); |
| 106 |
SK_API sk_sp<SkShader> CoordClamp(sk_sp<SkShader>, const SkRect& subset); |
| 107 |
|
| 108 |
/* |
| 109 |
* Create an SkShader that will sample the 'image'. This is equivalent to SkImage::makeShader. |
| 110 |
*/ |
| 111 |
SK_API sk_sp<SkShader> Image(sk_sp<SkImage> image, |
| 112 |
SkTileMode tmx, SkTileMode tmy, |
| 113 |
const SkSamplingOptions& options, |
| 114 |
const SkMatrix* localMatrix = nullptr); |
| 115 |
/* |
| 116 |
* Create an SkShader that will sample 'image' with minimal processing. This is equivalent to |
| 117 |
* SkImage::makeRawShader. |
| 118 |
*/ |
| 119 |
SK_API sk_sp<SkShader> RawImage(sk_sp<SkImage> image, |
| 120 |
SkTileMode tmx, SkTileMode tmy, |
| 121 |
const SkSamplingOptions& options, |
| 122 |
const SkMatrix* localMatrix = nullptr); |
| 123 |
} |
| 124 |
|
| 125 |
#endif |