1 /* 2 * Copyright (C) 2006 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef SkColorFilter_DEFINED 18 #define SkColorFilter_DEFINED 19 20 #include "SkColor.h" 21 #include "SkFlattenable.h" 22 #include "SkXfermode.h" 23 24 class SkColorFilter : public SkFlattenable { 25 public: 26 /** Called with a scanline of colors, as if there was a shader installed. 27 The implementation writes out its filtered version into result[]. 28 Note: shader and result may be the same buffer. 29 @param src array of colors, possibly generated by a shader 30 @param count the number of entries in the src[] and result[] arrays 31 @param result written by the filter 32 */ 33 virtual void filterSpan(const SkPMColor src[], int count, 34 SkPMColor result[]) = 0; 35 /** Called with a scanline of colors, as if there was a shader installed. 36 The implementation writes out its filtered version into result[]. 37 Note: shader and result may be the same buffer. 38 @param src array of colors, possibly generated by a shader 39 @param count the number of entries in the src[] and result[] arrays 40 @param result written by the filter 41 */ 42 virtual void filterSpan16(const uint16_t shader[], int count, 43 uint16_t result[]); 44 45 enum Flags { 46 /** If set the filter methods will not change the alpha channel of the 47 colors. 48 */ 49 kAlphaUnchanged_Flag = 0x01, 50 /** If set, this subclass implements filterSpan16(). If this flag is 51 set, then kAlphaUnchanged_Flag must also be set. 52 */ 53 kHasFilter16_Flag = 0x02 54 }; 55 56 /** Returns the flags for this filter. Override in subclasses to return 57 custom flags. 58 */ 59 virtual uint32_t getFlags() { return 0; } 60 61 /** Create a colorfilter that uses the specified color and mode. 62 If the Mode is DST, this function will return NULL (since that 63 mode will have no effect on the result). 64 @param c The source color used with the specified mode 65 @param mode The xfermode mode that is applied to each color in 66 the colorfilter's filterSpan[16,32] methods 67 @return colorfilter object that applies the src color and mode, 68 or NULL if the mode will have no effect. 69 */ 70 static SkColorFilter* CreateModeFilter(SkColor c, SkXfermode::Mode mode); 71 72 /** Create a colorfilter that calls through to the specified procs to 73 filter the colors. The SkXfermodeProc parameter must be non-null, but 74 the SkXfermodeProc16 is optional, and may be null. 75 */ 76 static SkColorFilter* CreateProcFilter(SkColor srcColor, 77 SkXfermodeProc proc, 78 SkXfermodeProc16 proc16 = NULL); 79 80 /** Create a colorfilter that multiplies the RGB channels by one color, and 81 then adds a second color, pinning the result for each component to 82 [0..255]. The alpha components of the mul and add arguments 83 are ignored. 84 */ 85 static SkColorFilter* CreateLightingFilter(SkColor mul, SkColor add); 86 87 protected: 88 SkColorFilter() {} 89 SkColorFilter(SkFlattenableReadBuffer& rb) : INHERITED(rb) {} 90 91 private: 92 typedef SkFlattenable INHERITED; 93 }; 94 95 #include "SkShader.h" 96 97 class SkFilterShader : public SkShader { 98 public: 99 SkFilterShader(SkShader* shader, SkColorFilter* filter); 100 virtual ~SkFilterShader(); 101 102 // override 103 virtual uint32_t getFlags(); 104 virtual bool setContext(const SkBitmap& device, const SkPaint& paint, 105 const SkMatrix& matrix); 106 virtual void shadeSpan(int x, int y, SkPMColor result[], int count); 107 virtual void shadeSpan16(int x, int y, uint16_t result[], int count); 108 virtual void beginSession(); 109 virtual void endSession(); 110 111 protected: 112 SkFilterShader(SkFlattenableReadBuffer& ); 113 virtual void flatten(SkFlattenableWriteBuffer& ); 114 virtual Factory getFactory() { return CreateProc; } 115 private: 116 static SkFlattenable* CreateProc(SkFlattenableReadBuffer& buffer) { 117 return SkNEW_ARGS(SkFilterShader, (buffer)); } 118 SkShader* fShader; 119 SkColorFilter* fFilter; 120 121 typedef SkShader INHERITED; 122 }; 123 124 #endif 125