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 SkColorFilter_DEFINED |
9 | #define SkColorFilter_DEFINED |
10 | |
11 | #include "include/core/SkBlendMode.h" |
12 | #include "include/core/SkColor.h" |
13 | #include "include/core/SkFlattenable.h" |
14 | #include "include/core/SkRefCnt.h" |
15 | |
16 | class GrColorInfo; |
17 | class GrFragmentProcessor; |
18 | class GrRecordingContext; |
19 | class SkArenaAlloc; |
20 | class SkBitmap; |
21 | class SkColorMatrix; |
22 | class SkColorSpace; |
23 | struct SkStageRec; |
24 | |
25 | namespace skvm { |
26 | class Builder; |
27 | struct F32; |
28 | struct Uniforms; |
29 | struct Color; |
30 | } |
31 | |
32 | /** |
33 | * ColorFilters are optional objects in the drawing pipeline. When present in |
34 | * a paint, they are called with the "src" colors, and return new colors, which |
35 | * are then passed onto the next stage (either ImageFilter or Xfermode). |
36 | * |
37 | * All subclasses are required to be reentrant-safe : it must be legal to share |
38 | * the same instance between several threads. |
39 | */ |
40 | class SK_API SkColorFilter : public SkFlattenable { |
41 | public: |
42 | // DEPRECATED. skbug.com/8941 |
43 | |
44 | bool asColorMode(SkColor* color, SkBlendMode* mode) const { |
45 | return this->onAsAColorMode(color, mode); |
46 | } |
47 | |
48 | /** If the filter can be represented by a source color plus Mode, this |
49 | * returns true, and sets (if not NULL) the color and mode appropriately. |
50 | * If not, this returns false and ignores the parameters. |
51 | */ |
52 | bool asAColorMode(SkColor* color, SkBlendMode* mode) const { |
53 | return this->onAsAColorMode(color, mode); |
54 | } |
55 | |
56 | /** If the filter can be represented by a 5x4 matrix, this |
57 | * returns true, and sets the matrix appropriately. |
58 | * If not, this returns false and ignores the parameter. |
59 | */ |
60 | bool asAColorMatrix(float matrix[20]) const { |
61 | return this->onAsAColorMatrix(matrix); |
62 | } |
63 | |
64 | bool appendStages(const SkStageRec& rec, bool shaderIsOpaque) const; |
65 | |
66 | skvm::Color program(skvm::Builder*, skvm::Color, |
67 | SkColorSpace* dstCS, skvm::Uniforms*, SkArenaAlloc*) const; |
68 | |
69 | enum Flags { |
70 | /** If set the filter methods will not change the alpha channel of the colors. |
71 | */ |
72 | kAlphaUnchanged_Flag = 1 << 0, |
73 | }; |
74 | |
75 | /** Returns the flags for this filter. Override in subclasses to return custom flags. |
76 | */ |
77 | virtual uint32_t getFlags() const { return 0; } |
78 | |
79 | SkColor filterColor(SkColor) const; |
80 | |
81 | /** |
82 | * Converts the src color (in src colorspace), into the dst colorspace, |
83 | * then applies this filter to it, returning the filtered color in the dst colorspace. |
84 | */ |
85 | SkColor4f filterColor4f(const SkColor4f& srcColor, SkColorSpace* srcCS, |
86 | SkColorSpace* dstCS) const; |
87 | |
88 | /** Construct a colorfilter whose effect is to first apply the inner filter and then apply |
89 | * this filter, applied to the output of the inner filter. |
90 | * |
91 | * result = this(inner(...)) |
92 | */ |
93 | sk_sp<SkColorFilter> makeComposed(sk_sp<SkColorFilter> inner) const; |
94 | |
95 | #if SK_SUPPORT_GPU |
96 | /** |
97 | * A subclass may implement this factory function to work with the GPU backend. It returns |
98 | * a GrFragmentProcessor that implemets the color filter in GPU shader code. |
99 | * |
100 | * The fragment processor receives a premultiplied input color and produces a premultiplied |
101 | * output color. |
102 | * |
103 | * A null return indicates that the color filter isn't implemented for the GPU backend. |
104 | */ |
105 | virtual std::unique_ptr<GrFragmentProcessor> asFragmentProcessor( |
106 | GrRecordingContext*, const GrColorInfo& dstColorInfo) const; |
107 | #endif |
108 | |
109 | bool affectsTransparentBlack() const { |
110 | return this->filterColor(SK_ColorTRANSPARENT) != SK_ColorTRANSPARENT; |
111 | } |
112 | |
113 | static void RegisterFlattenables(); |
114 | |
115 | static SkFlattenable::Type GetFlattenableType() { |
116 | return kSkColorFilter_Type; |
117 | } |
118 | |
119 | SkFlattenable::Type getFlattenableType() const override { |
120 | return kSkColorFilter_Type; |
121 | } |
122 | |
123 | static sk_sp<SkColorFilter> Deserialize(const void* data, size_t size, |
124 | const SkDeserialProcs* procs = nullptr) { |
125 | return sk_sp<SkColorFilter>(static_cast<SkColorFilter*>( |
126 | SkFlattenable::Deserialize( |
127 | kSkColorFilter_Type, data, size, procs).release())); |
128 | } |
129 | |
130 | protected: |
131 | SkColorFilter() {} |
132 | |
133 | virtual bool onAsAColorMatrix(float[20]) const; |
134 | virtual bool onAsAColorMode(SkColor* color, SkBlendMode* bmode) const; |
135 | |
136 | private: |
137 | virtual bool onAppendStages(const SkStageRec& rec, bool shaderIsOpaque) const = 0; |
138 | |
139 | virtual skvm::Color onProgram(skvm::Builder*, skvm::Color, |
140 | SkColorSpace* dstCS, skvm::Uniforms*, SkArenaAlloc*) const = 0; |
141 | |
142 | typedef SkFlattenable INHERITED; |
143 | }; |
144 | |
145 | class SK_API SkColorFilters { |
146 | public: |
147 | static sk_sp<SkColorFilter> Compose(sk_sp<SkColorFilter> outer, sk_sp<SkColorFilter> inner) { |
148 | return outer ? outer->makeComposed(inner) : inner; |
149 | } |
150 | static sk_sp<SkColorFilter> Blend(SkColor c, SkBlendMode mode); |
151 | static sk_sp<SkColorFilter> Matrix(const SkColorMatrix&); |
152 | static sk_sp<SkColorFilter> Matrix(const float rowMajor[20]); |
153 | |
154 | // A version of Matrix which operates in HSLA space instead of RGBA. |
155 | // I.e. HSLA-to-RGBA(Matrix(RGBA-to-HSLA(input))). |
156 | static sk_sp<SkColorFilter> HSLAMatrix(const float rowMajor[20]); |
157 | |
158 | static sk_sp<SkColorFilter> LinearToSRGBGamma(); |
159 | static sk_sp<SkColorFilter> SRGBToLinearGamma(); |
160 | static sk_sp<SkColorFilter> Lerp(float t, sk_sp<SkColorFilter> dst, sk_sp<SkColorFilter> src); |
161 | |
162 | private: |
163 | SkColorFilters() = delete; |
164 | }; |
165 | |
166 | #endif |
167 | |