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 GrFragmentProcessor; 18 class SkArenaAlloc; 19 class SkBitmap; 20 class SkColorSpace; 21 class SkColorSpaceXformer; 22 class SkRasterPipeline; 23 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 */ 32 class SK_API SkColorFilter : public SkFlattenable { 33 public: 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 */ 39 virtual bool asColorMode(SkColor* color, SkBlendMode* bmode) const; 40 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 */ 46 virtual bool asColorMatrix(SkScalar matrix[20]) const; 47 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 */ 64 virtual bool asComponentTable(SkBitmap* table) const; 65 66 void appendStages(SkRasterPipeline*, SkColorSpace*, SkArenaAlloc*, bool shaderIsOpaque) const; 67 68 enum Flags { 69 /** If set the filter methods will not change the alpha channel of the colors. 70 */ 71 kAlphaUnchanged_Flag = 1 << 0, 72 }; 73 74 /** Returns the flags for this filter. Override in subclasses to return custom flags. 75 */ 76 virtual uint32_t getFlags() const { return 0; } 77 78 /** 79 * 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 */ 85 virtual sk_sp<SkColorFilter> makeComposed(sk_sp<SkColorFilter>) const { return nullptr; } 86 87 SkColor filterColor(SkColor) const; 88 SkColor4f filterColor4f(const SkColor4f&) const; 89 90 /** Create a colorfilter that uses the specified color and mode. 91 If the Mode is DST, this function will return NULL (since that 92 mode will have no effect on the result). 93 @param c The source color used with the specified mode 94 @param mode The blend that is applied to each color in 95 the colorfilter's filterSpan[16,32] methods 96 @return colorfilter object that applies the src color and mode, 97 or NULL if the mode will have no effect. 98 */ 99 static sk_sp<SkColorFilter> MakeModeFilter(SkColor c, SkBlendMode mode); 100 101 /** 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. 104 * 105 * Due to internal limits, it is possible that this will return NULL, so the caller must 106 * always check. 107 */ 108 static sk_sp<SkColorFilter> MakeComposeFilter(sk_sp<SkColorFilter> outer, 109 sk_sp<SkColorFilter> inner); 110 111 /** 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 */ 114 static sk_sp<SkColorFilter> MakeMatrixFilterRowMajor255(const SkScalar array[20]); 115 116 /** 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 124 #if SK_SUPPORT_GPU 125 /** 126 * 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. 128 * 129 * The fragment processor receives a premultiplied input color and produces a premultiplied 130 * output color. 131 * 132 * A null return indicates that the color filter isn't implemented for the GPU backend. 133 */ 134 virtual sk_sp<GrFragmentProcessor> asFragmentProcessor(GrContext*, 135 SkColorSpace* dstColorSpace) const; 136 #endif 137 138 bool affectsTransparentBlack() const { 139 return this->filterColor(0) != 0; 140 } 141 142 SK_TO_STRING_PUREVIRT() 143 144 SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP() 145 SK_DEFINE_FLATTENABLE_TYPE(SkColorFilter) 146 147 protected: 148 SkColorFilter() {} 149 150 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 157 private: 158 /* 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; } 166 167 virtual void onAppendStages(SkRasterPipeline*, SkColorSpace*, SkArenaAlloc*, 168 bool shaderIsOpaque) const = 0; 169 170 friend class SkColorSpaceXformer; 171 friend class SkComposeColorFilter; 172 173 typedef SkFlattenable INHERITED; 174 }; 175 176 #endif 177