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 | |
| 11 | #include "SkColor.h" |
| 12 | #include "SkFlattenable.h" |
reed | cff10b2 | 2015-03-03 06:41:45 -0800 | [diff] [blame] | 13 | #include "SkTDArray.h" |
reed@android.com | 845fdac | 2009-06-23 03:01:32 +0000 | [diff] [blame] | 14 | #include "SkXfermode.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 15 | |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 16 | class SkBitmap; |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 17 | class GrProcessor; |
bsalomon@google.com | 67e78c9 | 2012-10-17 13:36:14 +0000 | [diff] [blame] | 18 | class GrContext; |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 19 | |
reed@google.com | fb6deed | 2013-10-10 17:35:58 +0000 | [diff] [blame] | 20 | /** |
| 21 | * ColorFilters are optional objects in the drawing pipeline. When present in |
| 22 | * a paint, they are called with the "src" colors, and return new colors, which |
| 23 | * are then passed onto the next stage (either ImageFilter or Xfermode). |
| 24 | * |
| 25 | * All subclasses are required to be reentrant-safe : it must be legal to share |
| 26 | * the same instance between several threads. |
| 27 | */ |
bsalomon@google.com | 8c3ff17 | 2011-04-15 15:42:24 +0000 | [diff] [blame] | 28 | class SK_API SkColorFilter : public SkFlattenable { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 29 | public: |
robertphillips@google.com | 0456e0b | 2012-06-27 14:03:26 +0000 | [diff] [blame] | 30 | SK_DECLARE_INST_COUNT(SkColorFilter) |
| 31 | |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 32 | /** |
| 33 | * If the filter can be represented by a source color plus Mode, this |
| 34 | * returns true, and sets (if not NULL) the color and mode appropriately. |
| 35 | * If not, this returns false and ignores the parameters. |
| 36 | */ |
reed@google.com | bada644 | 2012-12-17 20:21:44 +0000 | [diff] [blame] | 37 | virtual bool asColorMode(SkColor* color, SkXfermode::Mode* mode) const; |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 38 | |
senorblanco@chromium.org | e5ff3ce | 2011-12-20 20:58:18 +0000 | [diff] [blame] | 39 | /** |
| 40 | * If the filter can be represented by a 5x4 matrix, this |
| 41 | * returns true, and sets the matrix appropriately. |
| 42 | * If not, this returns false and ignores the parameter. |
| 43 | */ |
reed@google.com | bada644 | 2012-12-17 20:21:44 +0000 | [diff] [blame] | 44 | virtual bool asColorMatrix(SkScalar matrix[20]) const; |
senorblanco@chromium.org | e5ff3ce | 2011-12-20 20:58:18 +0000 | [diff] [blame] | 45 | |
reed@google.com | 7191840 | 2012-01-05 17:24:35 +0000 | [diff] [blame] | 46 | /** |
| 47 | * If the filter can be represented by per-component table, return true, |
| 48 | * and if table is not null, copy the bitmap containing the table into it. |
| 49 | * |
| 50 | * The table bitmap will be in SkBitmap::kA8_Config. Each row corresponding |
| 51 | * to each component in ARGB order. e.g. row[0] == alpha, row[1] == red, |
| 52 | * etc. To transform a color, you (logically) perform the following: |
| 53 | * |
| 54 | * a' = *table.getAddr8(a, 0); |
| 55 | * r' = *table.getAddr8(r, 1); |
| 56 | * g' = *table.getAddr8(g, 2); |
| 57 | * b' = *table.getAddr8(b, 3); |
| 58 | * |
| 59 | * The original component value is the horizontal index for a given row, |
| 60 | * and the stored value at that index is the new value for that component. |
| 61 | */ |
bsalomon@google.com | b2ad101 | 2012-10-17 15:00:32 +0000 | [diff] [blame] | 62 | virtual bool asComponentTable(SkBitmap* table) const; |
reed@google.com | 7191840 | 2012-01-05 17:24:35 +0000 | [diff] [blame] | 63 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 64 | /** Called with a scanline of colors, as if there was a shader installed. |
| 65 | The implementation writes out its filtered version into result[]. |
| 66 | Note: shader and result may be the same buffer. |
| 67 | @param src array of colors, possibly generated by a shader |
| 68 | @param count the number of entries in the src[] and result[] arrays |
| 69 | @param result written by the filter |
| 70 | */ |
| 71 | virtual void filterSpan(const SkPMColor src[], int count, |
reed@google.com | bada644 | 2012-12-17 20:21:44 +0000 | [diff] [blame] | 72 | SkPMColor result[]) const = 0; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 73 | /** Called with a scanline of colors, as if there was a shader installed. |
| 74 | The implementation writes out its filtered version into result[]. |
| 75 | Note: shader and result may be the same buffer. |
| 76 | @param src array of colors, possibly generated by a shader |
| 77 | @param count the number of entries in the src[] and result[] arrays |
| 78 | @param result written by the filter |
| 79 | */ |
| 80 | virtual void filterSpan16(const uint16_t shader[], int count, |
reed@google.com | bada644 | 2012-12-17 20:21:44 +0000 | [diff] [blame] | 81 | uint16_t result[]) const; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 82 | |
| 83 | enum Flags { |
| 84 | /** If set the filter methods will not change the alpha channel of the |
| 85 | colors. |
| 86 | */ |
| 87 | kAlphaUnchanged_Flag = 0x01, |
| 88 | /** If set, this subclass implements filterSpan16(). If this flag is |
| 89 | set, then kAlphaUnchanged_Flag must also be set. |
| 90 | */ |
| 91 | kHasFilter16_Flag = 0x02 |
| 92 | }; |
| 93 | |
| 94 | /** Returns the flags for this filter. Override in subclasses to return |
| 95 | custom flags. |
| 96 | */ |
reed@google.com | bada644 | 2012-12-17 20:21:44 +0000 | [diff] [blame] | 97 | virtual uint32_t getFlags() const { return 0; } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 98 | |
reed@google.com | 6b7aee3 | 2011-04-19 18:36:09 +0000 | [diff] [blame] | 99 | /** |
reed | 8a8d841 | 2015-03-02 13:46:03 -0800 | [diff] [blame] | 100 | * If this subclass can optimally createa composition with the inner filter, return it as |
| 101 | * a new filter (which the caller must unref() when it is done). If no such optimization |
| 102 | * is known, return NULL. |
| 103 | * |
| 104 | * e.g. result(color) == this_filter(inner(color)) |
| 105 | */ |
| 106 | virtual SkColorFilter* newComposed(const SkColorFilter* /*inner*/) const { return NULL; } |
| 107 | |
| 108 | /** |
reed@google.com | 6b7aee3 | 2011-04-19 18:36:09 +0000 | [diff] [blame] | 109 | * Apply this colorfilter to the specified SkColor. This routine handles |
| 110 | * converting to SkPMColor, calling the filter, and then converting back |
| 111 | * to SkColor. This method is not virtual, but will call filterSpan() |
| 112 | * which is virtual. |
| 113 | */ |
reed@google.com | bada644 | 2012-12-17 20:21:44 +0000 | [diff] [blame] | 114 | SkColor filterColor(SkColor) const; |
tomhudson@google.com | 1447c6f | 2011-04-27 14:09:52 +0000 | [diff] [blame] | 115 | |
reed@android.com | 845fdac | 2009-06-23 03:01:32 +0000 | [diff] [blame] | 116 | /** Create a colorfilter that uses the specified color and mode. |
| 117 | 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] | 118 | mode will have no effect on the result). |
reed@android.com | 845fdac | 2009-06-23 03:01:32 +0000 | [diff] [blame] | 119 | @param c The source color used with the specified mode |
| 120 | @param mode The xfermode mode that is applied to each color in |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 121 | the colorfilter's filterSpan[16,32] methods |
reed@android.com | 845fdac | 2009-06-23 03:01:32 +0000 | [diff] [blame] | 122 | @return colorfilter object that applies the src color and mode, |
| 123 | or NULL if the mode will have no effect. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 124 | */ |
reed@android.com | 845fdac | 2009-06-23 03:01:32 +0000 | [diff] [blame] | 125 | static SkColorFilter* CreateModeFilter(SkColor c, SkXfermode::Mode mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 126 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 127 | /** Create a colorfilter that multiplies the RGB channels by one color, and |
| 128 | then adds a second color, pinning the result for each component to |
| 129 | [0..255]. The alpha components of the mul and add arguments |
| 130 | are ignored. |
| 131 | */ |
| 132 | static SkColorFilter* CreateLightingFilter(SkColor mul, SkColor add); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 133 | |
reed | db873d8 | 2015-03-01 19:53:47 -0800 | [diff] [blame] | 134 | /** Construct a colorfilter whose effect is to first apply the inner filter and then apply |
| 135 | * the outer filter to the result of the inner's. |
| 136 | * The reference counts for outer and inner are incremented. |
| 137 | */ |
| 138 | static SkColorFilter* CreateComposeFilter(SkColorFilter* outer, SkColorFilter* inner); |
| 139 | |
reed | cff10b2 | 2015-03-03 06:41:45 -0800 | [diff] [blame] | 140 | /** |
| 141 | * A subclass may implement this factory function to work with the GPU backend. |
| 142 | * If it returns true, then 1 or more fragment processors will have been appended to the |
| 143 | * array, each of which has been ref'd, so that the caller is responsible for calling unref() |
| 144 | * on them when they are finished. If more than one processor is appended, they will be |
| 145 | * applied in FIFO order. |
| 146 | * |
reed | b7affb5 | 2015-03-04 13:30:50 -0800 | [diff] [blame^] | 147 | * The fragment processor(s) must each return their color as a premul normalized value |
| 148 | * e.g. each component between [0..1] and each color component <= alpha. |
| 149 | * |
reed | cff10b2 | 2015-03-03 06:41:45 -0800 | [diff] [blame] | 150 | * If the subclass returns false, then it should not modify the array at all. |
bsalomon@google.com | 67e78c9 | 2012-10-17 13:36:14 +0000 | [diff] [blame] | 151 | */ |
reed | cff10b2 | 2015-03-03 06:41:45 -0800 | [diff] [blame] | 152 | virtual bool asFragmentProcessors(GrContext*, SkTDArray<GrFragmentProcessor*>*) const { |
| 153 | return false; |
| 154 | } |
bsalomon@google.com | 67e78c9 | 2012-10-17 13:36:14 +0000 | [diff] [blame] | 155 | |
commit-bot@chromium.org | 0f10f7b | 2014-03-13 18:02:17 +0000 | [diff] [blame] | 156 | SK_TO_STRING_PUREVIRT() |
robertphillips@google.com | 1202c2a | 2013-05-23 14:00:17 +0000 | [diff] [blame] | 157 | |
djsollen@google.com | a2ca41e | 2012-03-23 19:00:34 +0000 | [diff] [blame] | 158 | SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP() |
commit-bot@chromium.org | c0b7e10 | 2013-10-23 17:06:21 +0000 | [diff] [blame] | 159 | SK_DEFINE_FLATTENABLE_TYPE(SkColorFilter) |
| 160 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 161 | protected: |
| 162 | SkColorFilter() {} |
tomhudson@google.com | 1447c6f | 2011-04-27 14:09:52 +0000 | [diff] [blame] | 163 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 164 | private: |
| 165 | typedef SkFlattenable INHERITED; |
| 166 | }; |
| 167 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 168 | #endif |