blob: 57650fbd98597a3f16fdf76f05c605dde8c18a6a [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00003 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00004 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00006 */
7
8#ifndef SkColorFilter_DEFINED
9#define SkColorFilter_DEFINED
10
11#include "SkColor.h"
12#include "SkFlattenable.h"
reedcff10b22015-03-03 06:41:45 -080013#include "SkTDArray.h"
reed@android.com845fdac2009-06-23 03:01:32 +000014#include "SkXfermode.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015
reedfb8c1fc2015-08-04 18:44:56 -070016class GrContext;
bungemand3ebb482015-08-05 13:57:49 -070017class GrFragmentProcessor;
bungemand3ebb482015-08-05 13:57:49 -070018class SkBitmap;
djsollen@google.comc73dd5c2012-08-07 15:54:32 +000019
reed@google.comfb6deed2013-10-10 17:35:58 +000020/**
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.com8c3ff172011-04-15 15:42:24 +000028class SK_API SkColorFilter : public SkFlattenable {
reed@android.com8a1c16f2008-12-17 15:59:43 +000029public:
reed@google.com43c50c82011-04-14 15:50:52 +000030 /**
31 * If the filter can be represented by a source color plus Mode, this
32 * returns true, and sets (if not NULL) the color and mode appropriately.
33 * If not, this returns false and ignores the parameters.
34 */
reed@google.combada6442012-12-17 20:21:44 +000035 virtual bool asColorMode(SkColor* color, SkXfermode::Mode* mode) const;
reed@google.com43c50c82011-04-14 15:50:52 +000036
senorblanco@chromium.orge5ff3ce2011-12-20 20:58:18 +000037 /**
38 * If the filter can be represented by a 5x4 matrix, this
39 * returns true, and sets the matrix appropriately.
40 * If not, this returns false and ignores the parameter.
41 */
reed@google.combada6442012-12-17 20:21:44 +000042 virtual bool asColorMatrix(SkScalar matrix[20]) const;
senorblanco@chromium.orge5ff3ce2011-12-20 20:58:18 +000043
reed@google.com71918402012-01-05 17:24:35 +000044 /**
45 * If the filter can be represented by per-component table, return true,
46 * and if table is not null, copy the bitmap containing the table into it.
47 *
48 * The table bitmap will be in SkBitmap::kA8_Config. Each row corresponding
49 * to each component in ARGB order. e.g. row[0] == alpha, row[1] == red,
50 * etc. To transform a color, you (logically) perform the following:
51 *
52 * a' = *table.getAddr8(a, 0);
53 * r' = *table.getAddr8(r, 1);
54 * g' = *table.getAddr8(g, 2);
55 * b' = *table.getAddr8(b, 3);
56 *
57 * The original component value is the horizontal index for a given row,
58 * and the stored value at that index is the new value for that component.
59 */
bsalomon@google.comb2ad1012012-10-17 15:00:32 +000060 virtual bool asComponentTable(SkBitmap* table) const;
reed@google.com71918402012-01-05 17:24:35 +000061
reed@android.com8a1c16f2008-12-17 15:59:43 +000062 /** Called with a scanline of colors, as if there was a shader installed.
63 The implementation writes out its filtered version into result[].
64 Note: shader and result may be the same buffer.
65 @param src array of colors, possibly generated by a shader
66 @param count the number of entries in the src[] and result[] arrays
67 @param result written by the filter
68 */
reed62a320c2015-03-24 06:35:23 -070069 virtual void filterSpan(const SkPMColor src[], int count, SkPMColor result[]) const = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +000070
71 enum Flags {
reed62a320c2015-03-24 06:35:23 -070072 /** If set the filter methods will not change the alpha channel of the colors.
reed@android.com8a1c16f2008-12-17 15:59:43 +000073 */
74 kAlphaUnchanged_Flag = 0x01,
reed@android.com8a1c16f2008-12-17 15:59:43 +000075 };
76
reed62a320c2015-03-24 06:35:23 -070077 /** Returns the flags for this filter. Override in subclasses to return custom flags.
reed@android.com8a1c16f2008-12-17 15:59:43 +000078 */
reed@google.combada6442012-12-17 20:21:44 +000079 virtual uint32_t getFlags() const { return 0; }
reed@android.com8a1c16f2008-12-17 15:59:43 +000080
reed@google.com6b7aee32011-04-19 18:36:09 +000081 /**
reed8a8d8412015-03-02 13:46:03 -080082 * If this subclass can optimally createa composition with the inner filter, return it as
83 * a new filter (which the caller must unref() when it is done). If no such optimization
84 * is known, return NULL.
85 *
86 * e.g. result(color) == this_filter(inner(color))
87 */
88 virtual SkColorFilter* newComposed(const SkColorFilter* /*inner*/) const { return NULL; }
89
90 /**
reed@google.com6b7aee32011-04-19 18:36:09 +000091 * Apply this colorfilter to the specified SkColor. This routine handles
92 * converting to SkPMColor, calling the filter, and then converting back
93 * to SkColor. This method is not virtual, but will call filterSpan()
94 * which is virtual.
95 */
reed@google.combada6442012-12-17 20:21:44 +000096 SkColor filterColor(SkColor) const;
tomhudson@google.com1447c6f2011-04-27 14:09:52 +000097
reed@android.com845fdac2009-06-23 03:01:32 +000098 /** Create a colorfilter that uses the specified color and mode.
99 If the Mode is DST, this function will return NULL (since that
reed@android.com8a1c16f2008-12-17 15:59:43 +0000100 mode will have no effect on the result).
reed@android.com845fdac2009-06-23 03:01:32 +0000101 @param c The source color used with the specified mode
102 @param mode The xfermode mode that is applied to each color in
reed@android.com8a1c16f2008-12-17 15:59:43 +0000103 the colorfilter's filterSpan[16,32] methods
reed@android.com845fdac2009-06-23 03:01:32 +0000104 @return colorfilter object that applies the src color and mode,
105 or NULL if the mode will have no effect.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000106 */
reed@android.com845fdac2009-06-23 03:01:32 +0000107 static SkColorFilter* CreateModeFilter(SkColor c, SkXfermode::Mode mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000108
reed@android.com8a1c16f2008-12-17 15:59:43 +0000109 /** Create a colorfilter that multiplies the RGB channels by one color, and
110 then adds a second color, pinning the result for each component to
111 [0..255]. The alpha components of the mul and add arguments
112 are ignored.
113 */
114 static SkColorFilter* CreateLightingFilter(SkColor mul, SkColor add);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000115
reeddb873d82015-03-01 19:53:47 -0800116 /** Construct a colorfilter whose effect is to first apply the inner filter and then apply
117 * the outer filter to the result of the inner's.
118 * The reference counts for outer and inner are incremented.
reeddc812222015-03-05 07:21:02 -0800119 *
120 * Due to internal limits, it is possible that this will return NULL, so the caller must
121 * always check.
reeddb873d82015-03-01 19:53:47 -0800122 */
123 static SkColorFilter* CreateComposeFilter(SkColorFilter* outer, SkColorFilter* inner);
124
reedcff10b22015-03-03 06:41:45 -0800125 /**
bsalomone25eea42015-09-29 06:38:55 -0700126 * A subclass may implement this factory function to work with the GPU backend. It returns
127 * a GrFragmentProcessor that implemets the color filter in GPU shader code.
reedcff10b22015-03-03 06:41:45 -0800128 *
bsalomone25eea42015-09-29 06:38:55 -0700129 * The fragment processor receives a premultiplied input color and produces a premultiplied
130 * output color.
reedb7affb52015-03-04 13:30:50 -0800131 *
bsalomone25eea42015-09-29 06:38:55 -0700132 * A null return indicates that the color filter isn't implemented for the GPU backend.
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000133 */
bsalomon4a339522015-10-06 08:40:50 -0700134 virtual const GrFragmentProcessor* asFragmentProcessor(GrContext*) const {
bsalomone25eea42015-09-29 06:38:55 -0700135 return nullptr;
reedcff10b22015-03-03 06:41:45 -0800136 }
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000137
senorblanco0abdf762015-08-20 11:10:41 -0700138 bool affectsTransparentBlack() const {
139 return this->filterColor(0) != 0;
140 }
141
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000142 SK_TO_STRING_PUREVIRT()
robertphillips@google.com1202c2a2013-05-23 14:00:17 +0000143
djsollen@google.coma2ca41e2012-03-23 19:00:34 +0000144 SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP()
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +0000145 SK_DEFINE_FLATTENABLE_TYPE(SkColorFilter)
146
reed@android.com8a1c16f2008-12-17 15:59:43 +0000147protected:
148 SkColorFilter() {}
tomhudson@google.com1447c6f2011-04-27 14:09:52 +0000149
reed@android.com8a1c16f2008-12-17 15:59:43 +0000150private:
reeddc812222015-03-05 07:21:02 -0800151 /*
152 * Returns 1 if this is a single filter (not a composition of other filters), otherwise it
153 * reutrns the number of leaf-node filters in a composition. This should be the same value
154 * as the number of GrFragmentProcessors returned by asFragmentProcessors's array parameter.
155 *
156 * e.g. compose(filter, compose(compose(filter, filter), filter)) --> 4
157 */
158 virtual int privateComposedFilterCount() const { return 1; }
159 friend class SkComposeColorFilter;
160
reed@android.com8a1c16f2008-12-17 15:59:43 +0000161 typedef SkFlattenable INHERITED;
162};
163
reed@android.com8a1c16f2008-12-17 15:59:43 +0000164#endif