blob: a1b3171fddb87a8948f54eec6080324506d02272 [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/*
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 "SkPorterDuff.h"
23
24class SkColorFilter : public SkFlattenable {
25public:
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 porter-duff mode.
62 If porterDuffMode is DST, this function will return NULL (since that
63 mode will have no effect on the result).
64 @param srcColor The source color used with the specified mode
65 @param mode The porter-duff 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 porter-duff
68 mode, or NULL if the mode will have no effect.
69 */
70 static SkColorFilter* CreatePorterDuffFilter(SkColor srcColor,
71 SkPorterDuff::Mode mode);
72
73 /** Create a colorfilter that calls through to the specified procs to
74 filter the colors. The SkXfermodeProc parameter must be non-null, but
75 the SkXfermodeProc16 is optional, and may be null.
76 */
77 static SkColorFilter* CreatXfermodeProcFilter(SkColor srcColor,
78 SkXfermodeProc proc,
79 SkXfermodeProc16 proc16 = NULL);
80
81 /** Create a colorfilter that multiplies the RGB channels by one color, and
82 then adds a second color, pinning the result for each component to
83 [0..255]. The alpha components of the mul and add arguments
84 are ignored.
85 */
86 static SkColorFilter* CreateLightingFilter(SkColor mul, SkColor add);
87
88protected:
89 SkColorFilter() {}
90 SkColorFilter(SkFlattenableReadBuffer& rb) : INHERITED(rb) {}
91
92private:
93 typedef SkFlattenable INHERITED;
94};
95
96#include "SkShader.h"
97
98class SkFilterShader : public SkShader {
99public:
100 SkFilterShader(SkShader* shader, SkColorFilter* filter);
101 virtual ~SkFilterShader();
102
103 // override
104 virtual uint32_t getFlags();
105 virtual bool setContext(const SkBitmap& device, const SkPaint& paint,
106 const SkMatrix& matrix);
107 virtual void shadeSpan(int x, int y, SkPMColor result[], int count);
108 virtual void shadeSpan16(int x, int y, uint16_t result[], int count);
109 virtual void beginSession();
110 virtual void endSession();
111
112protected:
113 SkFilterShader(SkFlattenableReadBuffer& );
114 virtual void flatten(SkFlattenableWriteBuffer& );
115 virtual Factory getFactory() { return CreateProc; }
116private:
117 static SkFlattenable* CreateProc(SkFlattenableReadBuffer& buffer) {
118 return SkNEW_ARGS(SkFilterShader, (buffer)); }
119 SkShader* fShader;
120 SkColorFilter* fFilter;
121
122 typedef SkShader INHERITED;
123};
124
125#endif