blob: 3241a2b31191e97d8b852a821bcfdafa9fc093b4 [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
Mike Reed71fecc32016-11-18 17:19:54 -050011#include "SkBlendMode.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000012#include "SkColor.h"
13#include "SkFlattenable.h"
bungeman06ca8ec2016-06-09 08:01:03 -070014#include "SkRefCnt.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;
Herb Derbyac04fef2017-01-13 17:34:33 -050018class SkArenaAlloc;
bungemand3ebb482015-08-05 13:57:49 -070019class SkBitmap;
Brian Osman618d3042016-10-25 10:51:28 -040020class SkColorSpace;
Mike Klein4e92b822017-04-26 13:45:36 -040021class SkColorSpaceXformer;
mtklein9a5c47f2016-07-22 11:05:04 -070022class SkRasterPipeline;
djsollen@google.comc73dd5c2012-08-07 15:54:32 +000023
reed@google.comfb6deed2013-10-10 17:35:58 +000024/**
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.com8c3ff172011-04-15 15:42:24 +000032class SK_API SkColorFilter : public SkFlattenable {
reed@android.com8a1c16f2008-12-17 15:59:43 +000033public:
reed@google.com43c50c82011-04-14 15:50:52 +000034 /**
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 Reedfaba3712016-11-03 14:45:31 -040039 virtual bool asColorMode(SkColor* color, SkBlendMode* bmode) const;
reed@google.com43c50c82011-04-14 15:50:52 +000040
senorblanco@chromium.orge5ff3ce2011-12-20 20:58:18 +000041 /**
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.combada6442012-12-17 20:21:44 +000046 virtual bool asColorMatrix(SkScalar matrix[20]) const;
senorblanco@chromium.orge5ff3ce2011-12-20 20:58:18 +000047
reed@google.com71918402012-01-05 17:24:35 +000048 /**
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.comb2ad1012012-10-17 15:00:32 +000064 virtual bool asComponentTable(SkBitmap* table) const;
reed@google.com71918402012-01-05 17:24:35 +000065
Mike Klein6dfceca2017-05-09 11:52:35 -040066 void appendStages(SkRasterPipeline*, SkColorSpace*, SkArenaAlloc*, bool shaderIsOpaque) const;
mtklein9a5c47f2016-07-22 11:05:04 -070067
reed@android.com8a1c16f2008-12-17 15:59:43 +000068 enum Flags {
reed62a320c2015-03-24 06:35:23 -070069 /** If set the filter methods will not change the alpha channel of the colors.
reed@android.com8a1c16f2008-12-17 15:59:43 +000070 */
reed6d3cef92016-01-22 01:04:29 -080071 kAlphaUnchanged_Flag = 1 << 0,
reed@android.com8a1c16f2008-12-17 15:59:43 +000072 };
73
reed62a320c2015-03-24 06:35:23 -070074 /** Returns the flags for this filter. Override in subclasses to return custom flags.
reed@android.com8a1c16f2008-12-17 15:59:43 +000075 */
reed@google.combada6442012-12-17 20:21:44 +000076 virtual uint32_t getFlags() const { return 0; }
reed@android.com8a1c16f2008-12-17 15:59:43 +000077
reed@google.com6b7aee32011-04-19 18:36:09 +000078 /**
reed8a8d8412015-03-02 13:46:03 -080079 * If this subclass can optimally createa composition with the inner filter, return it as
80 * a new filter (which the caller must unref() when it is done). If no such optimization
81 * is known, return NULL.
82 *
83 * e.g. result(color) == this_filter(inner(color))
84 */
reedd053ce92016-03-22 10:17:23 -070085 virtual sk_sp<SkColorFilter> makeComposed(sk_sp<SkColorFilter>) const { return nullptr; }
reed8a8d8412015-03-02 13:46:03 -080086
reed@google.combada6442012-12-17 20:21:44 +000087 SkColor filterColor(SkColor) const;
reedf7cdb062016-02-04 11:35:27 -080088 SkColor4f filterColor4f(const SkColor4f&) const;
89
reed@android.com845fdac2009-06-23 03:01:32 +000090 /** Create a colorfilter that uses the specified color and mode.
91 If the Mode is DST, this function will return NULL (since that
reed@android.com8a1c16f2008-12-17 15:59:43 +000092 mode will have no effect on the result).
reed@android.com845fdac2009-06-23 03:01:32 +000093 @param c The source color used with the specified mode
Mike Reed7d954ad2016-10-28 15:42:34 -040094 @param mode The blend that is applied to each color in
reed@android.com8a1c16f2008-12-17 15:59:43 +000095 the colorfilter's filterSpan[16,32] methods
reed@android.com845fdac2009-06-23 03:01:32 +000096 @return colorfilter object that applies the src color and mode,
97 or NULL if the mode will have no effect.
reed@android.com8a1c16f2008-12-17 15:59:43 +000098 */
Mike Reed7d954ad2016-10-28 15:42:34 -040099 static sk_sp<SkColorFilter> MakeModeFilter(SkColor c, SkBlendMode mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000100
reeddb873d82015-03-01 19:53:47 -0800101 /** Construct a colorfilter whose effect is to first apply the inner filter and then apply
102 * the outer filter to the result of the inner's.
103 * The reference counts for outer and inner are incremented.
reeddc812222015-03-05 07:21:02 -0800104 *
105 * Due to internal limits, it is possible that this will return NULL, so the caller must
106 * always check.
reeddb873d82015-03-01 19:53:47 -0800107 */
reedd053ce92016-03-22 10:17:23 -0700108 static sk_sp<SkColorFilter> MakeComposeFilter(sk_sp<SkColorFilter> outer,
109 sk_sp<SkColorFilter> inner);
reeddb873d82015-03-01 19:53:47 -0800110
bsalomon86100022016-02-01 12:09:07 -0800111 /** Construct a color filter that transforms a color by a 4x5 matrix. The matrix is in row-
112 * major order and the translation column is specified in unnormalized, 0...255, space.
113 */
reedd053ce92016-03-22 10:17:23 -0700114 static sk_sp<SkColorFilter> MakeMatrixFilterRowMajor255(const SkScalar array[20]);
115
Mike Reed412cda72017-07-05 15:43:15 -0400116 /** Construct a colorfilter that applies the srgb gamma curve to the RGB channels */
117 static sk_sp<SkColorFilter> MakeLinearToSRGBGamma();
118
119 /** Construct a colorfilter that applies the inverse of the srgb gamma curve to the
120 * RGB channels
121 */
122 static sk_sp<SkColorFilter> MakeSRGBToLinearGamma();
123
bungeman06ca8ec2016-06-09 08:01:03 -0700124#if SK_SUPPORT_GPU
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 */
Brian Salomonaff329b2017-08-11 09:40:37 -0400134 virtual std::unique_ptr<GrFragmentProcessor> asFragmentProcessor(
135 GrContext*, SkColorSpace* dstColorSpace) const;
bungeman06ca8ec2016-06-09 08:01:03 -0700136#endif
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()
mtklein3b375452016-04-04 14:57:19 -0700145 SK_DEFINE_FLATTENABLE_TYPE(SkColorFilter)
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +0000146
reed@android.com8a1c16f2008-12-17 15:59:43 +0000147protected:
148 SkColorFilter() {}
tomhudson@google.com1447c6f2011-04-27 14:09:52 +0000149
Mike Klein4e92b822017-04-26 13:45:36 -0400150 sk_sp<SkColorFilter> makeColorSpace(SkColorSpaceXformer* xformer) const {
151 return this->onMakeColorSpace(xformer);
152 }
153 virtual sk_sp<SkColorFilter> onMakeColorSpace(SkColorSpaceXformer*) const {
154 return sk_ref_sp(const_cast<SkColorFilter*>(this));
155 }
156
reed@android.com8a1c16f2008-12-17 15:59:43 +0000157private:
reeddc812222015-03-05 07:21:02 -0800158 /*
159 * Returns 1 if this is a single filter (not a composition of other filters), otherwise it
160 * reutrns the number of leaf-node filters in a composition. This should be the same value
161 * as the number of GrFragmentProcessors returned by asFragmentProcessors's array parameter.
162 *
163 * e.g. compose(filter, compose(compose(filter, filter), filter)) --> 4
164 */
165 virtual int privateComposedFilterCount() const { return 1; }
Matt Sarettc1b879f2017-04-17 17:18:57 -0400166
Mike Kleinfdf31032017-05-09 14:57:58 -0400167 virtual void onAppendStages(SkRasterPipeline*, SkColorSpace*, SkArenaAlloc*,
Mike Kleinc6912f72017-05-31 16:27:14 -0400168 bool shaderIsOpaque) const = 0;
Mike Reed25f38052017-05-31 15:03:02 -0400169
Matt Sarettc1b879f2017-04-17 17:18:57 -0400170 friend class SkColorSpaceXformer;
reeddc812222015-03-05 07:21:02 -0800171 friend class SkComposeColorFilter;
172
reed@android.com8a1c16f2008-12-17 15:59:43 +0000173 typedef SkFlattenable INHERITED;
174};
175
reed@android.com8a1c16f2008-12-17 15:59:43 +0000176#endif