• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/SkFlattenable.h"
12 #include "include/core/SkMatrix.h"
13 #include "include/core/SkRect.h"
14 
15 class SkColorFilter;
16 
17 /**
18  *  Base class for image filters. If one is installed in the paint, then all drawing occurs as
19  *  usual, but it is as if the drawing happened into an offscreen (before the xfermode is applied).
20  *  This offscreen bitmap will then be handed to the imagefilter, who in turn creates a new bitmap
21  *  which is what will finally be drawn to the device (using the original xfermode).
22  *
23  *  The local space of image filters matches the local space of the drawn geometry. For instance if
24  *  there is rotation on the canvas, the blur will be computed along those rotated axes and not in
25  *  the device space. In order to achieve this result, the actual drawing of the geometry may happen
26  *  in an unrotated coordinate system so that the filtered image can be computed more easily, and
27  *  then it will be post transformed to match what would have been produced if the geometry were
28  *  drawn with the total canvas matrix to begin with.
29  */
30 class SK_API SkImageFilter : public SkFlattenable {
31 public:
32     enum MapDirection {
33         kForward_MapDirection,
34         kReverse_MapDirection,
35     };
36     /**
37      * Map a device-space rect recursively forward or backward through the filter DAG.
38      * kForward_MapDirection is used to determine which pixels of the destination canvas a source
39      * image rect would touch after filtering. kReverse_MapDirection is used to determine which rect
40      * of the source image would be required to fill the given rect (typically, clip bounds). Used
41      * for clipping and temp-buffer allocations, so the result need not be exact, but should never
42      * be smaller than the real answer. The default implementation recursively unions all input
43      * bounds, or returns the source rect if no inputs.
44      *
45      * In kReverse mode, 'inputRect' is the device-space bounds of the input pixels. In kForward
46      * mode it should always be null. If 'inputRect' is null in kReverse mode the resulting answer
47      * may be incorrect.
48      */
49     SkIRect filterBounds(const SkIRect& src, const SkMatrix& ctm,
50                          MapDirection, const SkIRect* inputRect = nullptr) const;
51 
52     /**
53      *  Returns whether this image filter is a color filter and puts the color filter into the
54      *  "filterPtr" parameter if it can. Does nothing otherwise.
55      *  If this returns false, then the filterPtr is unchanged.
56      *  If this returns true, then if filterPtr is not null, it must be set to a ref'd colorfitler
57      *  (i.e. it may not be set to NULL).
58      */
59     bool isColorFilterNode(SkColorFilter** filterPtr) const;
60 
61     // DEPRECATED : use isColorFilterNode() instead
asColorFilter(SkColorFilter ** filterPtr)62     bool asColorFilter(SkColorFilter** filterPtr) const {
63         return this->isColorFilterNode(filterPtr);
64     }
65 
66     /**
67      *  Returns true (and optionally returns a ref'd filter) if this imagefilter can be completely
68      *  replaced by the returned colorfilter. i.e. the two effects will affect drawing in the same
69      *  way.
70      */
71     bool asAColorFilter(SkColorFilter** filterPtr) const;
72 
73     /**
74      *  Returns the number of inputs this filter will accept (some inputs can be NULL).
75      */
76     int countInputs() const;
77 
78     /**
79      *  Returns the input filter at a given index, or NULL if no input is connected.  The indices
80      *  used are filter-specific.
81      */
82     const SkImageFilter* getInput(int i) const;
83 
84     // Default impl returns union of all input bounds.
85     virtual SkRect computeFastBounds(const SkRect& bounds) const;
86 
87     // Can this filter DAG compute the resulting bounds of an object-space rectangle?
88     bool canComputeFastBounds() const;
89 
90     /**
91      *  If this filter can be represented by another filter + a localMatrix, return that filter,
92      *  else return null.
93      */
94     sk_sp<SkImageFilter> makeWithLocalMatrix(const SkMatrix& matrix) const;
95 
96     static sk_sp<SkImageFilter> Deserialize(const void* data, size_t size,
97                                           const SkDeserialProcs* procs = nullptr) {
98         return sk_sp<SkImageFilter>(static_cast<SkImageFilter*>(
99                 SkFlattenable::Deserialize(kSkImageFilter_Type, data, size, procs).release()));
100     }
101 
102 protected:
103 
refMe()104     sk_sp<SkImageFilter> refMe() const {
105         return sk_ref_sp(const_cast<SkImageFilter*>(this));
106     }
107 
108 private:
109     friend class SkImageFilter_Base;
110 
111     using INHERITED = SkFlattenable;
112 };
113 
114 #endif
115