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 "SkBlendMode.h"
12 #include "SkColor.h"
13 #include "SkFlattenable.h"
14 #include "SkRefCnt.h"
15 
16 class GrContext;
17 class GrColorSpaceInfo;
18 class GrFragmentProcessor;
19 class SkArenaAlloc;
20 class SkBitmap;
21 class SkColorSpace;
22 class SkColorSpaceXformer;
23 class SkRasterPipeline;
24 
25 /**
26  *  ColorFilters are optional objects in the drawing pipeline. When present in
27  *  a paint, they are called with the "src" colors, and return new colors, which
28  *  are then passed onto the next stage (either ImageFilter or Xfermode).
29  *
30  *  All subclasses are required to be reentrant-safe : it must be legal to share
31  *  the same instance between several threads.
32  */
33 class SK_API SkColorFilter : public SkFlattenable {
34 public:
35     /**
36      *  If the filter can be represented by a source color plus Mode, this
37      *  returns true, and sets (if not NULL) the color and mode appropriately.
38      *  If not, this returns false and ignores the parameters.
39      */
40     virtual bool asColorMode(SkColor* color, SkBlendMode* bmode) const;
41 
42     /**
43      *  If the filter can be represented by a 5x4 matrix, this
44      *  returns true, and sets the matrix appropriately.
45      *  If not, this returns false and ignores the parameter.
46      */
47     virtual bool asColorMatrix(SkScalar matrix[20]) const;
48 
49     /**
50      *  If the filter can be represented by per-component table, return true,
51      *  and if table is not null, copy the bitmap containing the table into it.
52      *
53      *  The table bitmap will be in SkBitmap::kA8_Config. Each row corresponding
54      *  to each component in ARGB order. e.g. row[0] == alpha, row[1] == red,
55      *  etc. To transform a color, you (logically) perform the following:
56      *
57      *      a' = *table.getAddr8(a, 0);
58      *      r' = *table.getAddr8(r, 1);
59      *      g' = *table.getAddr8(g, 2);
60      *      b' = *table.getAddr8(b, 3);
61      *
62      *  The original component value is the horizontal index for a given row,
63      *  and the stored value at that index is the new value for that component.
64      */
65     virtual bool asComponentTable(SkBitmap* table) const;
66 
67     void appendStages(SkRasterPipeline*, SkColorSpace*, SkArenaAlloc*, bool shaderIsOpaque) 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     */
getFlags()77     virtual uint32_t getFlags() const { return 0; }
78 
79     SkColor filterColor(SkColor) const;
80     SkColor4f filterColor4f(const SkColor4f&) const;
81 
82     /** Create a colorfilter that uses the specified color and mode.
83         If the Mode is DST, this function will return NULL (since that
84         mode will have no effect on the result).
85         @param c    The source color used with the specified mode
86         @param mode The blend that is applied to each color in
87                         the colorfilter's filterSpan[16,32] methods
88         @return colorfilter object that applies the src color and mode,
89                     or NULL if the mode will have no effect.
90     */
91     static sk_sp<SkColorFilter> MakeModeFilter(SkColor c, SkBlendMode mode);
92 
93     /** Construct a colorfilter whose effect is to first apply the inner filter and then apply
94      *  this filter, applied to the output of the inner filter.
95      *
96      *  result = this(inner(...))
97      *
98      *  Due to internal limits, it is possible that this will return NULL, so the caller must
99      *  always check.
100      */
101     sk_sp<SkColorFilter> makeComposed(sk_sp<SkColorFilter> inner) const;
102 
103     // DEPRECATED, call makeComposed instead
MakeComposeFilter(sk_sp<SkColorFilter> outer,sk_sp<SkColorFilter> inner)104     static sk_sp<SkColorFilter> MakeComposeFilter(sk_sp<SkColorFilter> outer,
105                                                   sk_sp<SkColorFilter> inner) {
106         return outer ? outer->makeComposed(inner) : inner;
107     }
108 
109     /** Construct a color filter that transforms a color by a 4x5 matrix. The matrix is in row-
110      *  major order and the translation column is specified in unnormalized, 0...255, space.
111      */
112     static sk_sp<SkColorFilter> MakeMatrixFilterRowMajor255(const SkScalar array[20]);
113 
114     /** Construct a colorfilter that applies the srgb gamma curve to the RGB channels */
115     static sk_sp<SkColorFilter> MakeLinearToSRGBGamma();
116 
117     /** Construct a colorfilter that applies the inverse of the srgb gamma curve to the
118      *  RGB channels
119      */
120     static sk_sp<SkColorFilter> MakeSRGBToLinearGamma();
121 
122 #if SK_SUPPORT_GPU
123     /**
124      *  A subclass may implement this factory function to work with the GPU backend. It returns
125      *  a GrFragmentProcessor that implemets the color filter in GPU shader code.
126      *
127      *  The fragment processor receives a premultiplied input color and produces a premultiplied
128      *  output color.
129      *
130      *  A null return indicates that the color filter isn't implemented for the GPU backend.
131      */
132     virtual std::unique_ptr<GrFragmentProcessor> asFragmentProcessor(
133             GrContext*, const GrColorSpaceInfo& dstColorSpaceInfo) const;
134 #endif
135 
affectsTransparentBlack()136     bool affectsTransparentBlack() const {
137         return this->filterColor(0) != 0;
138     }
139 
140     SK_TO_STRING_PUREVIRT()
141 
SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP()142     SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP()
143     SK_DEFINE_FLATTENABLE_TYPE(SkColorFilter)
144 
145 protected:
146     SkColorFilter() {}
147 
makeColorSpace(SkColorSpaceXformer * xformer)148     sk_sp<SkColorFilter> makeColorSpace(SkColorSpaceXformer* xformer) const {
149         return this->onMakeColorSpace(xformer);
150     }
onMakeColorSpace(SkColorSpaceXformer *)151     virtual sk_sp<SkColorFilter> onMakeColorSpace(SkColorSpaceXformer*) const {
152         return sk_ref_sp(const_cast<SkColorFilter*>(this));
153     }
154 
155     /**
156      *  If this subclass can optimally createa composition with the inner filter, return it as
157      *  a new filter (which the caller must unref() when it is done). If no such optimization
158      *  is known, return NULL.
159      *
160      *  e.g. result(color) == this_filter(inner(color))
161      */
onMakeComposed(sk_sp<SkColorFilter>)162     virtual sk_sp<SkColorFilter> onMakeComposed(sk_sp<SkColorFilter>) const { return nullptr; }
163 
164 private:
165     /*
166      *  Returns 1 if this is a single filter (not a composition of other filters), otherwise it
167      *  reutrns the number of leaf-node filters in a composition. This should be the same value
168      *  as the number of GrFragmentProcessors returned by asFragmentProcessors's array parameter.
169      *
170      *  e.g. compose(filter, compose(compose(filter, filter), filter)) --> 4
171      */
privateComposedFilterCount()172     virtual int privateComposedFilterCount() const { return 1; }
173 
174     virtual void onAppendStages(SkRasterPipeline*, SkColorSpace*, SkArenaAlloc*,
175                                 bool shaderIsOpaque) const = 0;
176 
177     friend class SkColorSpaceXformer;
178     friend class SkComposeColorFilter;
179 
180     typedef SkFlattenable INHERITED;
181 };
182 
183 #endif
184