SkColorFilter.h revision 1447c6f7f4579942b32af6ffff1eadede40b42bc
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
24class SK_API SkColorFilter : public SkFlattenable {
25public:
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
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
68    /**
69     *  Apply this colorfilter to the specified SkColor. This routine handles
70     *  converting to SkPMColor, calling the filter, and then converting back
71     *  to SkColor. This method is not virtual, but will call filterSpan()
72     *   which is virtual.
73     */
74    SkColor filterColor(SkColor);
75
76
77    /** Create a colorfilter that uses the specified color and mode.
78        If the Mode is DST, this function will return NULL (since that
79        mode will have no effect on the result).
80        @param c    The source color used with the specified mode
81        @param mode The xfermode mode that is applied to each color in
82                        the colorfilter's filterSpan[16,32] methods
83        @return colorfilter object that applies the src color and mode,
84                    or NULL if the mode will have no effect.
85    */
86    static SkColorFilter* CreateModeFilter(SkColor c, SkXfermode::Mode mode);
87
88    /** Create a colorfilter that calls through to the specified procs to
89        filter the colors. The SkXfermodeProc parameter must be non-null, but
90        the SkXfermodeProc16 is optional, and may be null.
91    */
92    static SkColorFilter* CreateProcFilter(SkColor srcColor,
93                                           SkXfermodeProc proc,
94                                           SkXfermodeProc16 proc16 = NULL);
95
96    /** Create a colorfilter that multiplies the RGB channels by one color, and
97        then adds a second color, pinning the result for each component to
98        [0..255]. The alpha components of the mul and add arguments
99        are ignored.
100    */
101    static SkColorFilter* CreateLightingFilter(SkColor mul, SkColor add);
102
103protected:
104    SkColorFilter() {}
105    SkColorFilter(SkFlattenableReadBuffer& rb) : INHERITED(rb) {}
106
107private:
108    typedef SkFlattenable INHERITED;
109};
110
111#include "SkShader.h"
112
113class SkFilterShader : public SkShader {
114public:
115    SkFilterShader(SkShader* shader, SkColorFilter* filter);
116    virtual ~SkFilterShader();
117
118    // override
119    virtual uint32_t getFlags();
120    virtual bool setContext(const SkBitmap& device, const SkPaint& paint,
121                            const SkMatrix& matrix);
122    virtual void shadeSpan(int x, int y, SkPMColor result[], int count);
123    virtual void shadeSpan16(int x, int y, uint16_t result[], int count);
124    virtual void beginSession();
125    virtual void endSession();
126
127protected:
128    SkFilterShader(SkFlattenableReadBuffer& );
129    virtual void flatten(SkFlattenableWriteBuffer& );
130    virtual Factory getFactory() { return CreateProc; }
131private:
132    static SkFlattenable* CreateProc(SkFlattenableReadBuffer& buffer) {
133        return SkNEW_ARGS(SkFilterShader, (buffer)); }
134    SkShader*       fShader;
135    SkColorFilter*  fFilter;
136
137    typedef SkShader INHERITED;
138};
139
140#endif
141