reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 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" |
reed@android.com | 845fdac | 2009-06-23 03:01:32 +0000 | [diff] [blame] | 22 | #include "SkXfermode.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 23 | |
| 24 | class SkColorFilter : public SkFlattenable { |
| 25 | public: |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame^] | 26 | /** |
| 27 | * If the filter can be represented by a source color plus Mode, this |
| 28 | * returns true, and sets (if not NULL) the color and mode appropriately. |
| 29 | * If not, this returns false and ignores the parameters. |
| 30 | */ |
| 31 | virtual bool asColorMode(SkColor* color, SkXfermode::Mode* mode); |
| 32 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 33 | /** Called with a scanline of colors, as if there was a shader installed. |
| 34 | The implementation writes out its filtered version into result[]. |
| 35 | Note: shader and result may be the same buffer. |
| 36 | @param src array of colors, possibly generated by a shader |
| 37 | @param count the number of entries in the src[] and result[] arrays |
| 38 | @param result written by the filter |
| 39 | */ |
| 40 | virtual void filterSpan(const SkPMColor src[], int count, |
| 41 | SkPMColor result[]) = 0; |
| 42 | /** Called with a scanline of colors, as if there was a shader installed. |
| 43 | The implementation writes out its filtered version into result[]. |
| 44 | Note: shader and result may be the same buffer. |
| 45 | @param src array of colors, possibly generated by a shader |
| 46 | @param count the number of entries in the src[] and result[] arrays |
| 47 | @param result written by the filter |
| 48 | */ |
| 49 | virtual void filterSpan16(const uint16_t shader[], int count, |
| 50 | uint16_t result[]); |
| 51 | |
| 52 | enum Flags { |
| 53 | /** If set the filter methods will not change the alpha channel of the |
| 54 | colors. |
| 55 | */ |
| 56 | kAlphaUnchanged_Flag = 0x01, |
| 57 | /** If set, this subclass implements filterSpan16(). If this flag is |
| 58 | set, then kAlphaUnchanged_Flag must also be set. |
| 59 | */ |
| 60 | kHasFilter16_Flag = 0x02 |
| 61 | }; |
| 62 | |
| 63 | /** Returns the flags for this filter. Override in subclasses to return |
| 64 | custom flags. |
| 65 | */ |
| 66 | virtual uint32_t getFlags() { return 0; } |
| 67 | |
reed@android.com | 845fdac | 2009-06-23 03:01:32 +0000 | [diff] [blame] | 68 | /** Create a colorfilter that uses the specified color and mode. |
| 69 | If the Mode is DST, this function will return NULL (since that |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 70 | mode will have no effect on the result). |
reed@android.com | 845fdac | 2009-06-23 03:01:32 +0000 | [diff] [blame] | 71 | @param c The source color used with the specified mode |
| 72 | @param mode The xfermode mode that is applied to each color in |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 73 | the colorfilter's filterSpan[16,32] methods |
reed@android.com | 845fdac | 2009-06-23 03:01:32 +0000 | [diff] [blame] | 74 | @return colorfilter object that applies the src color and mode, |
| 75 | or NULL if the mode will have no effect. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 76 | */ |
reed@android.com | 845fdac | 2009-06-23 03:01:32 +0000 | [diff] [blame] | 77 | static SkColorFilter* CreateModeFilter(SkColor c, SkXfermode::Mode mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 78 | |
| 79 | /** Create a colorfilter that calls through to the specified procs to |
| 80 | filter the colors. The SkXfermodeProc parameter must be non-null, but |
| 81 | the SkXfermodeProc16 is optional, and may be null. |
| 82 | */ |
reed@android.com | 845fdac | 2009-06-23 03:01:32 +0000 | [diff] [blame] | 83 | static SkColorFilter* CreateProcFilter(SkColor srcColor, |
| 84 | SkXfermodeProc proc, |
| 85 | SkXfermodeProc16 proc16 = NULL); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 86 | |
| 87 | /** Create a colorfilter that multiplies the RGB channels by one color, and |
| 88 | then adds a second color, pinning the result for each component to |
| 89 | [0..255]. The alpha components of the mul and add arguments |
| 90 | are ignored. |
| 91 | */ |
| 92 | static SkColorFilter* CreateLightingFilter(SkColor mul, SkColor add); |
| 93 | |
| 94 | protected: |
| 95 | SkColorFilter() {} |
| 96 | SkColorFilter(SkFlattenableReadBuffer& rb) : INHERITED(rb) {} |
| 97 | |
| 98 | private: |
| 99 | typedef SkFlattenable INHERITED; |
| 100 | }; |
| 101 | |
| 102 | #include "SkShader.h" |
| 103 | |
| 104 | class SkFilterShader : public SkShader { |
| 105 | public: |
| 106 | SkFilterShader(SkShader* shader, SkColorFilter* filter); |
| 107 | virtual ~SkFilterShader(); |
| 108 | |
| 109 | // override |
| 110 | virtual uint32_t getFlags(); |
| 111 | virtual bool setContext(const SkBitmap& device, const SkPaint& paint, |
| 112 | const SkMatrix& matrix); |
| 113 | virtual void shadeSpan(int x, int y, SkPMColor result[], int count); |
| 114 | virtual void shadeSpan16(int x, int y, uint16_t result[], int count); |
| 115 | virtual void beginSession(); |
| 116 | virtual void endSession(); |
| 117 | |
| 118 | protected: |
| 119 | SkFilterShader(SkFlattenableReadBuffer& ); |
| 120 | virtual void flatten(SkFlattenableWriteBuffer& ); |
| 121 | virtual Factory getFactory() { return CreateProc; } |
| 122 | private: |
| 123 | static SkFlattenable* CreateProc(SkFlattenableReadBuffer& buffer) { |
| 124 | return SkNEW_ARGS(SkFilterShader, (buffer)); } |
| 125 | SkShader* fShader; |
| 126 | SkColorFilter* fFilter; |
| 127 | |
| 128 | typedef SkShader INHERITED; |
| 129 | }; |
| 130 | |
| 131 | #endif |