blob: 749a73a65167634cb0839876dd5579cd5381e953 [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 SkMaskFilter_DEFINED
18#define SkMaskFilter_DEFINED
19
20#include "SkFlattenable.h"
21#include "SkMask.h"
22
23class SkBlitter;
24class SkBounder;
25class SkMatrix;
26class SkPath;
27class SkRegion;
28
29/** \class SkMaskFilter
30
31 SkMaskFilter is the base class for object that perform transformations on
32 an alpha-channel mask before drawing it. A subclass of SkMaskFilter may be
33 installed into a SkPaint. Once there, each time a primitive is drawn, it
34 is first scan converted into a SkMask::kA8_Format mask, and handed to the
35 filter, calling its filterMask() method. If this returns true, then the
36 new mask is used to render into the device.
37
38 Blur and emboss are implemented as subclasses of SkMaskFilter.
39*/
40class SkMaskFilter : public SkFlattenable {
41public:
42 SkMaskFilter() {}
43
44 /** Returns the format of the resulting mask that this subclass will return
45 when its filterMask() method is called.
46 */
47 virtual SkMask::Format getFormat() = 0;
48
49 /** Create a new mask by filter the src mask.
50 If src.fImage == null, then do not allocate or create the dst image
51 but do fill out the other fields in dstMask.
52 If you do allocate a dst image, use SkMask::AllocImage()
53 If this returns false, dst mask is ignored.
54 @param dst the result of the filter. If src.fImage == null, dst should not allocate its image
55 @param src the original image to be filtered.
56 @param matrix the CTM
57 @param margin if not null, return the buffer dx/dy need when calculating the effect. Used when
58 drawing a clipped object to know how much larger to allocate the src before
59 applying the filter. If returning false, ignore this parameter.
60 @return true if the dst mask was correctly created.
61 */
62 virtual bool filterMask(SkMask* dst, const SkMask& src, const SkMatrix&, SkIPoint* margin);
63
64 /** Helper method that, given a path in device space, will rasterize it into a kA8_Format mask
65 and then call filterMask(). If this returns true, the specified blitter will be called
66 to render that mask. Returns false if filterMask() returned false.
67 This method is not exported to java.
68 */
69 bool filterPath(const SkPath& devPath, const SkMatrix& devMatrix,
70 const SkRegion& devClip, SkBounder*, SkBlitter* blitter);
71
72 virtual void flatten(SkFlattenableWriteBuffer& ) {}
73protected:
74 // empty for now, but lets get our subclass to remember to init us for the future
75 SkMaskFilter(SkFlattenableReadBuffer&) {}
76};
77
78/** \class SkAutoMaskImage
79
80 Stack class used to manage the fImage buffer in a SkMask.
81 When this object loses scope, the buffer is freed with SkMask::FreeImage().
82*/
83class SkAutoMaskImage {
84public:
85 SkAutoMaskImage(SkMask* mask, bool alloc)
86 {
87 if (alloc)
88 mask->fImage = SkMask::AllocImage(mask->computeImageSize());
89 fImage = mask->fImage;
90 }
91 ~SkAutoMaskImage()
92 {
93 SkMask::FreeImage(fImage);
94 }
95private:
96 uint8_t* fImage;
97};
98
99#endif
100