reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2006 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 3 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #ifndef SkColorFilter_DEFINED |
| 9 | #define SkColorFilter_DEFINED |
| 10 | |
Mike Reed | 71fecc3 | 2016-11-18 17:19:54 -0500 | [diff] [blame] | 11 | #include "SkBlendMode.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 12 | #include "SkColor.h" |
| 13 | #include "SkFlattenable.h" |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 14 | #include "SkRefCnt.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 15 | |
reed | fb8c1fc | 2015-08-04 18:44:56 -0700 | [diff] [blame] | 16 | class GrContext; |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 17 | class GrFragmentProcessor; |
Herb Derby | ac04fef | 2017-01-13 17:34:33 -0500 | [diff] [blame] | 18 | class SkArenaAlloc; |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 19 | class SkBitmap; |
Brian Osman | 618d304 | 2016-10-25 10:51:28 -0400 | [diff] [blame] | 20 | class SkColorSpace; |
Mike Klein | 4e92b82 | 2017-04-26 13:45:36 -0400 | [diff] [blame] | 21 | class SkColorSpaceXformer; |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 22 | class SkRasterPipeline; |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 23 | |
reed@google.com | fb6deed | 2013-10-10 17:35:58 +0000 | [diff] [blame] | 24 | /** |
| 25 | * ColorFilters are optional objects in the drawing pipeline. When present in |
| 26 | * a paint, they are called with the "src" colors, and return new colors, which |
| 27 | * are then passed onto the next stage (either ImageFilter or Xfermode). |
| 28 | * |
| 29 | * All subclasses are required to be reentrant-safe : it must be legal to share |
| 30 | * the same instance between several threads. |
| 31 | */ |
bsalomon@google.com | 8c3ff17 | 2011-04-15 15:42:24 +0000 | [diff] [blame] | 32 | class SK_API SkColorFilter : public SkFlattenable { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 33 | public: |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 34 | /** |
| 35 | * If the filter can be represented by a source color plus Mode, this |
| 36 | * returns true, and sets (if not NULL) the color and mode appropriately. |
| 37 | * If not, this returns false and ignores the parameters. |
| 38 | */ |
Mike Reed | faba371 | 2016-11-03 14:45:31 -0400 | [diff] [blame] | 39 | virtual bool asColorMode(SkColor* color, SkBlendMode* bmode) const; |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 40 | |
senorblanco@chromium.org | e5ff3ce | 2011-12-20 20:58:18 +0000 | [diff] [blame] | 41 | /** |
| 42 | * If the filter can be represented by a 5x4 matrix, this |
| 43 | * returns true, and sets the matrix appropriately. |
| 44 | * If not, this returns false and ignores the parameter. |
| 45 | */ |
reed@google.com | bada644 | 2012-12-17 20:21:44 +0000 | [diff] [blame] | 46 | virtual bool asColorMatrix(SkScalar matrix[20]) const; |
senorblanco@chromium.org | e5ff3ce | 2011-12-20 20:58:18 +0000 | [diff] [blame] | 47 | |
reed@google.com | 7191840 | 2012-01-05 17:24:35 +0000 | [diff] [blame] | 48 | /** |
| 49 | * If the filter can be represented by per-component table, return true, |
| 50 | * and if table is not null, copy the bitmap containing the table into it. |
| 51 | * |
| 52 | * The table bitmap will be in SkBitmap::kA8_Config. Each row corresponding |
| 53 | * to each component in ARGB order. e.g. row[0] == alpha, row[1] == red, |
| 54 | * etc. To transform a color, you (logically) perform the following: |
| 55 | * |
| 56 | * a' = *table.getAddr8(a, 0); |
| 57 | * r' = *table.getAddr8(r, 1); |
| 58 | * g' = *table.getAddr8(g, 2); |
| 59 | * b' = *table.getAddr8(b, 3); |
| 60 | * |
| 61 | * The original component value is the horizontal index for a given row, |
| 62 | * and the stored value at that index is the new value for that component. |
| 63 | */ |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 64 | virtual bool asComponentTable(SkBitmap* table) const; |
reed@google.com | 7191840 | 2012-01-05 17:24:35 +0000 | [diff] [blame] | 65 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 66 | /** Called with a scanline of colors, as if there was a shader installed. |
| 67 | The implementation writes out its filtered version into result[]. |
| 68 | Note: shader and result may be the same buffer. |
| 69 | @param src array of colors, possibly generated by a shader |
| 70 | @param count the number of entries in the src[] and result[] arrays |
| 71 | @param result written by the filter |
| 72 | */ |
reed | 62a320c | 2015-03-24 06:35:23 -0700 | [diff] [blame] | 73 | virtual void filterSpan(const SkPMColor src[], int count, SkPMColor result[]) const = 0; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 74 | |
Mike Reed | b9641bd | 2017-05-04 10:57:40 -0400 | [diff] [blame] | 75 | virtual void filterSpan4f(const SkPM4f src[], int count, SkPM4f result[]) const = 0; |
reed | 6d3cef9 | 2016-01-22 01:04:29 -0800 | [diff] [blame] | 76 | |
Mike Klein | 6dfceca | 2017-05-09 11:52:35 -0400 | [diff] [blame^] | 77 | void appendStages(SkRasterPipeline*, SkColorSpace*, SkArenaAlloc*, bool shaderIsOpaque) const; |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 78 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 79 | enum Flags { |
reed | 62a320c | 2015-03-24 06:35:23 -0700 | [diff] [blame] | 80 | /** If set the filter methods will not change the alpha channel of the colors. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 81 | */ |
reed | 6d3cef9 | 2016-01-22 01:04:29 -0800 | [diff] [blame] | 82 | kAlphaUnchanged_Flag = 1 << 0, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 83 | }; |
| 84 | |
reed | 62a320c | 2015-03-24 06:35:23 -0700 | [diff] [blame] | 85 | /** Returns the flags for this filter. Override in subclasses to return custom flags. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 86 | */ |
reed@google.com | bada644 | 2012-12-17 20:21:44 +0000 | [diff] [blame] | 87 | virtual uint32_t getFlags() const { return 0; } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 88 | |
reed@google.com | 6b7aee3 | 2011-04-19 18:36:09 +0000 | [diff] [blame] | 89 | /** |
reed | 8a8d841 | 2015-03-02 13:46:03 -0800 | [diff] [blame] | 90 | * If this subclass can optimally createa composition with the inner filter, return it as |
| 91 | * a new filter (which the caller must unref() when it is done). If no such optimization |
| 92 | * is known, return NULL. |
| 93 | * |
| 94 | * e.g. result(color) == this_filter(inner(color)) |
| 95 | */ |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 96 | virtual sk_sp<SkColorFilter> makeComposed(sk_sp<SkColorFilter>) const { return nullptr; } |
reed | 8a8d841 | 2015-03-02 13:46:03 -0800 | [diff] [blame] | 97 | |
| 98 | /** |
reed@google.com | 6b7aee3 | 2011-04-19 18:36:09 +0000 | [diff] [blame] | 99 | * Apply this colorfilter to the specified SkColor. This routine handles |
| 100 | * converting to SkPMColor, calling the filter, and then converting back |
| 101 | * to SkColor. This method is not virtual, but will call filterSpan() |
| 102 | * which is virtual. |
| 103 | */ |
reed@google.com | bada644 | 2012-12-17 20:21:44 +0000 | [diff] [blame] | 104 | SkColor filterColor(SkColor) const; |
tomhudson@google.com | 1447c6f | 2011-04-27 14:09:52 +0000 | [diff] [blame] | 105 | |
reed | f7cdb06 | 2016-02-04 11:35:27 -0800 | [diff] [blame] | 106 | /** |
| 107 | * Filters a single color. |
| 108 | */ |
| 109 | SkColor4f filterColor4f(const SkColor4f&) const; |
| 110 | |
reed@android.com | 845fdac | 2009-06-23 03:01:32 +0000 | [diff] [blame] | 111 | /** Create a colorfilter that uses the specified color and mode. |
| 112 | If the Mode is DST, this function will return NULL (since that |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 113 | mode will have no effect on the result). |
reed@android.com | 845fdac | 2009-06-23 03:01:32 +0000 | [diff] [blame] | 114 | @param c The source color used with the specified mode |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 115 | @param mode The blend that is applied to each color in |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 116 | the colorfilter's filterSpan[16,32] methods |
reed@android.com | 845fdac | 2009-06-23 03:01:32 +0000 | [diff] [blame] | 117 | @return colorfilter object that applies the src color and mode, |
| 118 | or NULL if the mode will have no effect. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 119 | */ |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 120 | static sk_sp<SkColorFilter> MakeModeFilter(SkColor c, SkBlendMode mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 121 | |
reed | db873d8 | 2015-03-01 19:53:47 -0800 | [diff] [blame] | 122 | /** Construct a colorfilter whose effect is to first apply the inner filter and then apply |
| 123 | * the outer filter to the result of the inner's. |
| 124 | * The reference counts for outer and inner are incremented. |
reed | dc81222 | 2015-03-05 07:21:02 -0800 | [diff] [blame] | 125 | * |
| 126 | * Due to internal limits, it is possible that this will return NULL, so the caller must |
| 127 | * always check. |
reed | db873d8 | 2015-03-01 19:53:47 -0800 | [diff] [blame] | 128 | */ |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 129 | static sk_sp<SkColorFilter> MakeComposeFilter(sk_sp<SkColorFilter> outer, |
| 130 | sk_sp<SkColorFilter> inner); |
reed | db873d8 | 2015-03-01 19:53:47 -0800 | [diff] [blame] | 131 | |
bsalomon | 8610002 | 2016-02-01 12:09:07 -0800 | [diff] [blame] | 132 | /** Construct a color filter that transforms a color by a 4x5 matrix. The matrix is in row- |
| 133 | * major order and the translation column is specified in unnormalized, 0...255, space. |
| 134 | */ |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 135 | static sk_sp<SkColorFilter> MakeMatrixFilterRowMajor255(const SkScalar array[20]); |
| 136 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 137 | #if SK_SUPPORT_GPU |
reed | cff10b2 | 2015-03-03 06:41:45 -0800 | [diff] [blame] | 138 | /** |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 139 | * A subclass may implement this factory function to work with the GPU backend. It returns |
| 140 | * a GrFragmentProcessor that implemets the color filter in GPU shader code. |
reed | cff10b2 | 2015-03-03 06:41:45 -0800 | [diff] [blame] | 141 | * |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 142 | * The fragment processor receives a premultiplied input color and produces a premultiplied |
| 143 | * output color. |
reed | b7affb5 | 2015-03-04 13:30:50 -0800 | [diff] [blame] | 144 | * |
bsalomon | e25eea4 | 2015-09-29 06:38:55 -0700 | [diff] [blame] | 145 | * A null return indicates that the color filter isn't implemented for the GPU backend. |
bsalomon@google.com | 67e78c9 | 2012-10-17 13:36:14 +0000 | [diff] [blame] | 146 | */ |
Brian Osman | 618d304 | 2016-10-25 10:51:28 -0400 | [diff] [blame] | 147 | virtual sk_sp<GrFragmentProcessor> asFragmentProcessor(GrContext*, |
| 148 | SkColorSpace* dstColorSpace) const; |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 149 | #endif |
bsalomon@google.com | 67e78c9 | 2012-10-17 13:36:14 +0000 | [diff] [blame] | 150 | |
senorblanco | 0abdf76 | 2015-08-20 11:10:41 -0700 | [diff] [blame] | 151 | bool affectsTransparentBlack() const { |
| 152 | return this->filterColor(0) != 0; |
| 153 | } |
| 154 | |
commit-bot@chromium.org | 0f10f7b | 2014-03-13 18:02:17 +0000 | [diff] [blame] | 155 | SK_TO_STRING_PUREVIRT() |
robertphillips@google.com | 1202c2a | 2013-05-23 14:00:17 +0000 | [diff] [blame] | 156 | |
djsollen@google.com | a2ca41e | 2012-03-23 19:00:34 +0000 | [diff] [blame] | 157 | SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP() |
mtklein | 3b37545 | 2016-04-04 14:57:19 -0700 | [diff] [blame] | 158 | SK_DEFINE_FLATTENABLE_TYPE(SkColorFilter) |
commit-bot@chromium.org | c0b7e10 | 2013-10-23 17:06:21 +0000 | [diff] [blame] | 159 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 160 | protected: |
| 161 | SkColorFilter() {} |
tomhudson@google.com | 1447c6f | 2011-04-27 14:09:52 +0000 | [diff] [blame] | 162 | |
Mike Klein | 4e92b82 | 2017-04-26 13:45:36 -0400 | [diff] [blame] | 163 | sk_sp<SkColorFilter> makeColorSpace(SkColorSpaceXformer* xformer) const { |
| 164 | return this->onMakeColorSpace(xformer); |
| 165 | } |
| 166 | virtual sk_sp<SkColorFilter> onMakeColorSpace(SkColorSpaceXformer*) const { |
| 167 | return sk_ref_sp(const_cast<SkColorFilter*>(this)); |
| 168 | } |
| 169 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 170 | private: |
reed | dc81222 | 2015-03-05 07:21:02 -0800 | [diff] [blame] | 171 | /* |
| 172 | * Returns 1 if this is a single filter (not a composition of other filters), otherwise it |
| 173 | * reutrns the number of leaf-node filters in a composition. This should be the same value |
| 174 | * as the number of GrFragmentProcessors returned by asFragmentProcessors's array parameter. |
| 175 | * |
| 176 | * e.g. compose(filter, compose(compose(filter, filter), filter)) --> 4 |
| 177 | */ |
| 178 | virtual int privateComposedFilterCount() const { return 1; } |
Matt Sarett | c1b879f | 2017-04-17 17:18:57 -0400 | [diff] [blame] | 179 | |
| 180 | /* |
| 181 | * Returns true and sets |outer| and |inner| if this is a compose color filter. |
| 182 | * Returns false otherwise. |
| 183 | */ |
| 184 | virtual bool asACompose(SkColorFilter** /*outer*/, SkColorFilter** /*inner*/) const { |
| 185 | return false; |
| 186 | } |
| 187 | |
Mike Klein | 6dfceca | 2017-05-09 11:52:35 -0400 | [diff] [blame^] | 188 | virtual bool onAppendStages(SkRasterPipeline*, SkColorSpace*, SkArenaAlloc*, |
| 189 | bool shaderIsOpaque) const; |
| 190 | |
| 191 | |
Matt Sarett | c1b879f | 2017-04-17 17:18:57 -0400 | [diff] [blame] | 192 | friend class SkColorSpaceXformer; |
reed | dc81222 | 2015-03-05 07:21:02 -0800 | [diff] [blame] | 193 | friend class SkComposeColorFilter; |
| 194 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 195 | typedef SkFlattenable INHERITED; |
| 196 | }; |
| 197 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 198 | #endif |