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