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