1 | /* |
2 | * Copyright 2011 Google Inc. |
3 | * |
4 | * Use of this source code is governed by a BSD-style license that can be |
5 | * found in the LICENSE file. |
6 | */ |
7 | |
8 | #ifndef SkImageFilter_DEFINED |
9 | #define SkImageFilter_DEFINED |
10 | |
11 | #include "include/core/SkFilterQuality.h" |
12 | #include "include/core/SkFlattenable.h" |
13 | #include "include/core/SkMatrix.h" |
14 | #include "include/core/SkRect.h" |
15 | |
16 | class SkColorFilter; |
17 | |
18 | /** |
19 | * Base class for image filters. If one is installed in the paint, then all drawing occurs as |
20 | * usual, but it is as if the drawing happened into an offscreen (before the xfermode is applied). |
21 | * This offscreen bitmap will then be handed to the imagefilter, who in turn creates a new bitmap |
22 | * which is what will finally be drawn to the device (using the original xfermode). |
23 | * |
24 | * The local space of image filters matches the local space of the drawn geometry. For instance if |
25 | * there is rotation on the canvas, the blur will be computed along those rotated axes and not in |
26 | * the device space. In order to achieve this result, the actual drawing of the geometry may happen |
27 | * in an unrotated coordinate system so that the filtered image can be computed more easily, and |
28 | * then it will be post transformed to match what would have been produced if the geometry were |
29 | * drawn with the total canvas matrix to begin with. |
30 | */ |
31 | class SK_API SkImageFilter : public SkFlattenable { |
32 | public: |
33 | class CropRect { |
34 | public: |
35 | enum CropEdge { |
36 | kHasLeft_CropEdge = 0x01, |
37 | kHasTop_CropEdge = 0x02, |
38 | kHasWidth_CropEdge = 0x04, |
39 | kHasHeight_CropEdge = 0x08, |
40 | kHasAll_CropEdge = 0x0F, |
41 | }; |
42 | CropRect() {} |
43 | explicit CropRect(const SkRect& rect, uint32_t flags = kHasAll_CropEdge) |
44 | : fRect(rect), fFlags(flags) {} |
45 | uint32_t flags() const { return fFlags; } |
46 | const SkRect& rect() const { return fRect; } |
47 | |
48 | /** |
49 | * Apply this cropRect to the imageBounds. If a given edge of the cropRect is not set, then |
50 | * the corresponding edge from imageBounds will be used. If "embiggen" is true, the crop |
51 | * rect is allowed to enlarge the size of the rect, otherwise it may only reduce the rect. |
52 | * Filters that can affect transparent black should pass "true", while all other filters |
53 | * should pass "false". |
54 | * |
55 | * Note: imageBounds is in "device" space, as the output cropped rectangle will be, so the |
56 | * matrix is ignored for those. It is only applied to the cropRect's bounds. |
57 | */ |
58 | void applyTo(const SkIRect& imageBounds, const SkMatrix& matrix, bool embiggen, |
59 | SkIRect* cropped) const; |
60 | |
61 | private: |
62 | SkRect fRect; |
63 | uint32_t fFlags; |
64 | }; |
65 | |
66 | enum MapDirection { |
67 | kForward_MapDirection, |
68 | kReverse_MapDirection, |
69 | }; |
70 | /** |
71 | * Map a device-space rect recursively forward or backward through the filter DAG. |
72 | * kForward_MapDirection is used to determine which pixels of the destination canvas a source |
73 | * image rect would touch after filtering. kReverse_MapDirection is used to determine which rect |
74 | * of the source image would be required to fill the given rect (typically, clip bounds). Used |
75 | * for clipping and temp-buffer allocations, so the result need not be exact, but should never |
76 | * be smaller than the real answer. The default implementation recursively unions all input |
77 | * bounds, or returns the source rect if no inputs. |
78 | * |
79 | * In kReverse mode, 'inputRect' is the device-space bounds of the input pixels. In kForward |
80 | * mode it should always be null. If 'inputRect' is null in kReverse mode the resulting answer |
81 | * may be incorrect. |
82 | */ |
83 | SkIRect filterBounds(const SkIRect& src, const SkMatrix& ctm, |
84 | MapDirection, const SkIRect* inputRect = nullptr) const; |
85 | |
86 | /** |
87 | * Returns whether this image filter is a color filter and puts the color filter into the |
88 | * "filterPtr" parameter if it can. Does nothing otherwise. |
89 | * If this returns false, then the filterPtr is unchanged. |
90 | * If this returns true, then if filterPtr is not null, it must be set to a ref'd colorfitler |
91 | * (i.e. it may not be set to NULL). |
92 | */ |
93 | bool isColorFilterNode(SkColorFilter** filterPtr) const; |
94 | |
95 | // DEPRECATED : use isColorFilterNode() instead |
96 | bool asColorFilter(SkColorFilter** filterPtr) const { |
97 | return this->isColorFilterNode(filterPtr); |
98 | } |
99 | |
100 | /** |
101 | * Returns true (and optionally returns a ref'd filter) if this imagefilter can be completely |
102 | * replaced by the returned colorfilter. i.e. the two effects will affect drawing in the same |
103 | * way. |
104 | */ |
105 | bool asAColorFilter(SkColorFilter** filterPtr) const; |
106 | |
107 | /** |
108 | * Returns the number of inputs this filter will accept (some inputs can be NULL). |
109 | */ |
110 | int countInputs() const; |
111 | |
112 | /** |
113 | * Returns the input filter at a given index, or NULL if no input is connected. The indices |
114 | * used are filter-specific. |
115 | */ |
116 | const SkImageFilter* getInput(int i) const; |
117 | |
118 | // Default impl returns union of all input bounds. |
119 | virtual SkRect computeFastBounds(const SkRect& bounds) const; |
120 | |
121 | // Can this filter DAG compute the resulting bounds of an object-space rectangle? |
122 | bool canComputeFastBounds() const; |
123 | |
124 | /** |
125 | * If this filter can be represented by another filter + a localMatrix, return that filter, |
126 | * else return null. |
127 | */ |
128 | sk_sp<SkImageFilter> makeWithLocalMatrix(const SkMatrix& matrix) const; |
129 | |
130 | /** |
131 | * Return an imagefilter which transforms its input by the given matrix. |
132 | * DEPRECATED: Use include/effects/SkImageFilters::MatrixTransform |
133 | */ |
134 | static sk_sp<SkImageFilter> MakeMatrixFilter(const SkMatrix& matrix, |
135 | SkFilterQuality quality, |
136 | sk_sp<SkImageFilter> input); |
137 | |
138 | static SkFlattenable::Type GetFlattenableType() { |
139 | return kSkImageFilter_Type; |
140 | } |
141 | |
142 | SkFlattenable::Type getFlattenableType() const override { |
143 | return kSkImageFilter_Type; |
144 | } |
145 | |
146 | static sk_sp<SkImageFilter> Deserialize(const void* data, size_t size, |
147 | const SkDeserialProcs* procs = nullptr) { |
148 | return sk_sp<SkImageFilter>(static_cast<SkImageFilter*>( |
149 | SkFlattenable::Deserialize(kSkImageFilter_Type, data, size, procs).release())); |
150 | } |
151 | |
152 | protected: |
153 | |
154 | sk_sp<SkImageFilter> refMe() const { |
155 | return sk_ref_sp(const_cast<SkImageFilter*>(this)); |
156 | } |
157 | |
158 | private: |
159 | friend class SkImageFilter_Base; |
160 | |
161 | typedef SkFlattenable INHERITED; |
162 | }; |
163 | |
164 | #endif |
165 | |