reed@google.com | 894aa9a | 2011-09-23 14:49:49 +0000 | [diff] [blame] | 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 | |
bungeman | bf521ff | 2016-02-17 13:13:44 -0800 | [diff] [blame] | 11 | #include "../private/SkTArray.h" |
bungeman | f3c15b7 | 2015-08-19 11:56:48 -0700 | [diff] [blame] | 12 | #include "../private/SkTemplates.h" |
xidachen | 2352696 | 2016-02-01 05:27:16 -0800 | [diff] [blame] | 13 | #include "../private/SkMutex.h" |
brianosman | afbf71d | 2016-07-21 07:15:37 -0700 | [diff] [blame] | 14 | #include "SkColorSpace.h" |
senorblanco | 8c874ee | 2015-03-20 06:38:17 -0700 | [diff] [blame] | 15 | #include "SkFilterQuality.h" |
reed@google.com | 894aa9a | 2011-09-23 14:49:49 +0000 | [diff] [blame] | 16 | #include "SkFlattenable.h" |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 17 | #include "SkMatrix.h" |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 18 | #include "SkRect.h" |
reed@google.com | 894aa9a | 2011-09-23 14:49:49 +0000 | [diff] [blame] | 19 | |
robertphillips | af9b8c8 | 2016-04-12 11:02:25 -0700 | [diff] [blame] | 20 | class GrContext; |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 21 | class GrFragmentProcessor; |
reed | 2c55d7b | 2015-06-09 08:18:39 -0700 | [diff] [blame] | 22 | class SkColorFilter; |
Matt Sarett | 31abf1f | 2017-04-07 16:54:04 -0400 | [diff] [blame] | 23 | class SkColorSpaceXformer; |
reed | 2c55d7b | 2015-06-09 08:18:39 -0700 | [diff] [blame] | 24 | struct SkIPoint; |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 25 | class SkSpecialImage; |
senorblanco | 900c367 | 2016-04-27 11:31:23 -0700 | [diff] [blame] | 26 | class SkImageFilterCache; |
| 27 | struct SkImageFilterCacheKey; |
reed@google.com | 15356a6 | 2011-11-03 19:29:08 +0000 | [diff] [blame] | 28 | |
| 29 | /** |
reed@google.com | 15356a6 | 2011-11-03 19:29:08 +0000 | [diff] [blame] | 30 | * Base class for image filters. If one is installed in the paint, then |
| 31 | * all drawing occurs as usual, but it is as if the drawing happened into an |
| 32 | * offscreen (before the xfermode is applied). This offscreen bitmap will |
| 33 | * then be handed to the imagefilter, who in turn creates a new bitmap which |
| 34 | * is what will finally be drawn to the device (using the original xfermode). |
reed@google.com | 15356a6 | 2011-11-03 19:29:08 +0000 | [diff] [blame] | 35 | */ |
senorblanco@chromium.org | 54e01b2 | 2011-11-16 18:20:47 +0000 | [diff] [blame] | 36 | class SK_API SkImageFilter : public SkFlattenable { |
reed@google.com | 894aa9a | 2011-09-23 14:49:49 +0000 | [diff] [blame] | 37 | public: |
brianosman | 2a75e5d | 2016-09-22 07:15:37 -0700 | [diff] [blame] | 38 | // Extra information about the output of a filter DAG. For now, this is just the color space |
| 39 | // (of the original requesting device). This is used when constructing intermediate rendering |
| 40 | // surfaces, so that we ensure we land in a surface that's similar/compatible to the final |
| 41 | // consumer of the DAG's output. |
| 42 | class OutputProperties { |
| 43 | public: |
| 44 | explicit OutputProperties(SkColorSpace* colorSpace) : fColorSpace(colorSpace) {} |
| 45 | |
| 46 | SkColorSpace* colorSpace() const { return fColorSpace; } |
| 47 | |
| 48 | private: |
| 49 | // This will be a pointer to the device's color space, and our lifetime is bounded by |
| 50 | // the device, so we can store a bare pointer. |
| 51 | SkColorSpace* fColorSpace; |
| 52 | }; |
| 53 | |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 54 | class Context { |
| 55 | public: |
brianosman | 2a75e5d | 2016-09-22 07:15:37 -0700 | [diff] [blame] | 56 | Context(const SkMatrix& ctm, const SkIRect& clipBounds, SkImageFilterCache* cache, |
| 57 | const OutputProperties& outputProperties) |
reed | c9b5f8b | 2015-10-22 13:20:20 -0700 | [diff] [blame] | 58 | : fCTM(ctm) |
| 59 | , fClipBounds(clipBounds) |
| 60 | , fCache(cache) |
brianosman | 2a75e5d | 2016-09-22 07:15:37 -0700 | [diff] [blame] | 61 | , fOutputProperties(outputProperties) |
reed | c9b5f8b | 2015-10-22 13:20:20 -0700 | [diff] [blame] | 62 | {} |
| 63 | |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 64 | const SkMatrix& ctm() const { return fCTM; } |
| 65 | const SkIRect& clipBounds() const { return fClipBounds; } |
senorblanco | 900c367 | 2016-04-27 11:31:23 -0700 | [diff] [blame] | 66 | SkImageFilterCache* cache() const { return fCache; } |
brianosman | 2a75e5d | 2016-09-22 07:15:37 -0700 | [diff] [blame] | 67 | const OutputProperties& outputProperties() const { return fOutputProperties; } |
reed | c9b5f8b | 2015-10-22 13:20:20 -0700 | [diff] [blame] | 68 | |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 69 | private: |
senorblanco | 900c367 | 2016-04-27 11:31:23 -0700 | [diff] [blame] | 70 | SkMatrix fCTM; |
| 71 | SkIRect fClipBounds; |
| 72 | SkImageFilterCache* fCache; |
brianosman | 2a75e5d | 2016-09-22 07:15:37 -0700 | [diff] [blame] | 73 | OutputProperties fOutputProperties; |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 74 | }; |
| 75 | |
reed | 1891863 | 2015-07-16 13:17:13 -0700 | [diff] [blame] | 76 | class CropRect { |
| 77 | public: |
| 78 | enum CropEdge { |
| 79 | kHasLeft_CropEdge = 0x01, |
| 80 | kHasTop_CropEdge = 0x02, |
senorblanco | ed7cf27 | 2015-07-16 15:19:11 -0700 | [diff] [blame] | 81 | kHasWidth_CropEdge = 0x04, |
| 82 | kHasHeight_CropEdge = 0x08, |
reed | 1891863 | 2015-07-16 13:17:13 -0700 | [diff] [blame] | 83 | kHasAll_CropEdge = 0x0F, |
| 84 | }; |
| 85 | CropRect() {} |
| 86 | explicit CropRect(const SkRect& rect, uint32_t flags = kHasAll_CropEdge) |
| 87 | : fRect(rect), fFlags(flags) {} |
| 88 | uint32_t flags() const { return fFlags; } |
| 89 | const SkRect& rect() const { return fRect; } |
| 90 | #ifndef SK_IGNORE_TO_STRING |
| 91 | void toString(SkString* str) const; |
| 92 | #endif |
| 93 | |
| 94 | /** |
| 95 | * Apply this cropRect to the imageBounds. If a given edge of the cropRect is not |
senorblanco | 6db0a7b | 2016-04-01 16:41:10 -0700 | [diff] [blame] | 96 | * set, then the corresponding edge from imageBounds will be used. If "embiggen" |
| 97 | * is true, the crop rect is allowed to enlarge the size of the rect, otherwise |
| 98 | * it may only reduce the rect. Filters that can affect transparent black should |
| 99 | * pass "true", while all other filters should pass "false". |
reed | 1891863 | 2015-07-16 13:17:13 -0700 | [diff] [blame] | 100 | * |
| 101 | * Note: imageBounds is in "device" space, as the output cropped rectangle will be, |
senorblanco | d8ff5b3 | 2016-01-28 08:23:02 -0800 | [diff] [blame] | 102 | * so the matrix is ignored for those. It is only applied the croprect's bounds. |
reed | 1891863 | 2015-07-16 13:17:13 -0700 | [diff] [blame] | 103 | */ |
senorblanco | 6db0a7b | 2016-04-01 16:41:10 -0700 | [diff] [blame] | 104 | void applyTo(const SkIRect& imageBounds, const SkMatrix&, bool embiggen, |
| 105 | SkIRect* cropped) const; |
reed | 1891863 | 2015-07-16 13:17:13 -0700 | [diff] [blame] | 106 | |
| 107 | private: |
| 108 | SkRect fRect; |
| 109 | uint32_t fFlags; |
| 110 | }; |
| 111 | |
senorblanco | a9fbd16 | 2016-01-11 14:09:09 -0800 | [diff] [blame] | 112 | enum TileUsage { |
| 113 | kPossible_TileUsage, //!< the created device may be drawn tiled |
| 114 | kNever_TileUsage, //!< the created device will never be drawn tiled |
| 115 | }; |
| 116 | |
reed@google.com | 894aa9a | 2011-09-23 14:49:49 +0000 | [diff] [blame] | 117 | /** |
senorblanco | 5878dbd | 2016-05-19 14:50:29 -0700 | [diff] [blame] | 118 | * Request a new filtered image to be created from the src image. |
reed@google.com | 894aa9a | 2011-09-23 14:49:49 +0000 | [diff] [blame] | 119 | * |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 120 | * The context contains the environment in which the filter is occurring. |
| 121 | * It includes the clip bounds, CTM and cache. |
reed@google.com | 894aa9a | 2011-09-23 14:49:49 +0000 | [diff] [blame] | 122 | * |
| 123 | * Offset is the amount to translate the resulting image relative to the |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 124 | * src when it is drawn. This is an out-param. |
reed@google.com | 894aa9a | 2011-09-23 14:49:49 +0000 | [diff] [blame] | 125 | * |
senorblanco | 5878dbd | 2016-05-19 14:50:29 -0700 | [diff] [blame] | 126 | * If the result image cannot be created, or the result would be |
| 127 | * transparent black, return null, in which case the offset parameter |
| 128 | * should be ignored by the caller. |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 129 | * |
| 130 | * TODO: Right now the imagefilters sometimes return empty result bitmaps/ |
| 131 | * specialimages. That doesn't seem quite right. |
reed@google.com | 894aa9a | 2011-09-23 14:49:49 +0000 | [diff] [blame] | 132 | */ |
robertphillips | 2302de9 | 2016-03-24 07:26:32 -0700 | [diff] [blame] | 133 | sk_sp<SkSpecialImage> filterImage(SkSpecialImage* src, const Context&, SkIPoint* offset) const; |
robertphillips | eaf086e | 2016-03-07 04:51:10 -0800 | [diff] [blame] | 134 | |
senorblanco | d8ff5b3 | 2016-01-28 08:23:02 -0800 | [diff] [blame] | 135 | enum MapDirection { |
| 136 | kForward_MapDirection, |
| 137 | kReverse_MapDirection |
| 138 | }; |
reed@google.com | 15356a6 | 2011-11-03 19:29:08 +0000 | [diff] [blame] | 139 | /** |
senorblanco | d8ff5b3 | 2016-01-28 08:23:02 -0800 | [diff] [blame] | 140 | * Map a device-space rect recursively forward or backward through the |
| 141 | * filter DAG. kForward_MapDirection is used to determine which pixels of |
| 142 | * the destination canvas a source image rect would touch after filtering. |
jbroman | 127fe3e | 2016-03-21 08:28:48 -0700 | [diff] [blame] | 143 | * kReverse_MapDirection is used to determine which rect of the source |
senorblanco | d8ff5b3 | 2016-01-28 08:23:02 -0800 | [diff] [blame] | 144 | * image would be required to fill the given rect (typically, clip bounds). |
| 145 | * Used for clipping and temp-buffer allocations, so the result need not |
| 146 | * be exact, but should never be smaller than the real answer. The default |
senorblanco | e5e7984 | 2016-03-21 14:51:59 -0700 | [diff] [blame] | 147 | * implementation recursively unions all input bounds, or returns the |
| 148 | * source rect if no inputs. |
reed@google.com | 32d25b6 | 2011-12-20 16:19:00 +0000 | [diff] [blame] | 149 | */ |
senorblanco | e5e7984 | 2016-03-21 14:51:59 -0700 | [diff] [blame] | 150 | SkIRect filterBounds(const SkIRect& src, const SkMatrix& ctm, |
| 151 | MapDirection = kReverse_MapDirection) const; |
| 152 | |
robertphillips | af9b8c8 | 2016-04-12 11:02:25 -0700 | [diff] [blame] | 153 | #if SK_SUPPORT_GPU |
brianosman | 2a75e5d | 2016-09-22 07:15:37 -0700 | [diff] [blame] | 154 | static sk_sp<SkSpecialImage> DrawWithFP(GrContext* context, |
robertphillips | af9b8c8 | 2016-04-12 11:02:25 -0700 | [diff] [blame] | 155 | sk_sp<GrFragmentProcessor> fp, |
brianosman | afbf71d | 2016-07-21 07:15:37 -0700 | [diff] [blame] | 156 | const SkIRect& bounds, |
brianosman | 2a75e5d | 2016-09-22 07:15:37 -0700 | [diff] [blame] | 157 | const OutputProperties& outputProperties); |
robertphillips | af9b8c8 | 2016-04-12 11:02:25 -0700 | [diff] [blame] | 158 | #endif |
| 159 | |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 160 | /** |
sugoi@google.com | a1c511b | 2013-02-21 15:02:28 +0000 | [diff] [blame] | 161 | * Returns whether this image filter is a color filter and puts the color filter into the |
sugoi@google.com | 4b6d432 | 2013-02-21 20:26:50 +0000 | [diff] [blame] | 162 | * "filterPtr" parameter if it can. Does nothing otherwise. |
| 163 | * If this returns false, then the filterPtr is unchanged. |
| 164 | * If this returns true, then if filterPtr is not null, it must be set to a ref'd colorfitler |
| 165 | * (i.e. it may not be set to NULL). |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 166 | */ |
reed | cedc36f | 2015-03-08 04:42:52 -0700 | [diff] [blame] | 167 | bool isColorFilterNode(SkColorFilter** filterPtr) const { |
| 168 | return this->onIsColorFilterNode(filterPtr); |
| 169 | } |
| 170 | |
| 171 | // DEPRECATED : use isColorFilterNode() instead |
| 172 | bool asColorFilter(SkColorFilter** filterPtr) const { |
| 173 | return this->isColorFilterNode(filterPtr); |
| 174 | } |
| 175 | |
vjiaoblack | e1e5c74 | 2016-08-23 11:13:14 -0700 | [diff] [blame] | 176 | static sk_sp<SkImageFilter> MakeBlur(SkScalar sigmaX, SkScalar sigmaY, |
| 177 | sk_sp<SkImageFilter> input, |
| 178 | const CropRect* cropRect = nullptr); |
| 179 | |
reed | cedc36f | 2015-03-08 04:42:52 -0700 | [diff] [blame] | 180 | /** |
| 181 | * Returns true (and optionally returns a ref'd filter) if this imagefilter can be completely |
| 182 | * replaced by the returned colorfilter. i.e. the two effects will affect drawing in the |
| 183 | * same way. |
| 184 | */ |
senorblanco | a544eda | 2015-12-07 07:48:34 -0800 | [diff] [blame] | 185 | bool asAColorFilter(SkColorFilter** filterPtr) const; |
senorblanco@chromium.org | 9f25de7 | 2012-10-10 20:36:13 +0000 | [diff] [blame] | 186 | |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 187 | /** |
| 188 | * Returns the number of inputs this filter will accept (some inputs can |
| 189 | * be NULL). |
| 190 | */ |
robertphillips | 6b13473 | 2016-04-15 09:58:37 -0700 | [diff] [blame] | 191 | int countInputs() const { return fInputs.count(); } |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 192 | |
| 193 | /** |
| 194 | * Returns the input filter at a given index, or NULL if no input is |
| 195 | * connected. The indices used are filter-specific. |
| 196 | */ |
| 197 | SkImageFilter* getInput(int i) const { |
robertphillips | 6b13473 | 2016-04-15 09:58:37 -0700 | [diff] [blame] | 198 | SkASSERT(i < fInputs.count()); |
| 199 | return fInputs[i].get(); |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 200 | } |
| 201 | |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 202 | /** |
senorblanco@chromium.org | 3f1f2a3 | 2013-10-16 18:07:48 +0000 | [diff] [blame] | 203 | * Returns whether any edges of the crop rect have been set. The crop |
| 204 | * rect is set at construction time, and determines which pixels from the |
reed | 1891863 | 2015-07-16 13:17:13 -0700 | [diff] [blame] | 205 | * input image will be processed, and which pixels in the output image will be allowed. |
| 206 | * The size of the crop rect should be |
senorblanco@chromium.org | 3f1f2a3 | 2013-10-16 18:07:48 +0000 | [diff] [blame] | 207 | * used as the size of the destination image. The origin of this rect |
| 208 | * should be used to offset access to the input images, and should also |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 209 | * be added to the "offset" parameter in onFilterImage. |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 210 | */ |
senorblanco@chromium.org | 3f1f2a3 | 2013-10-16 18:07:48 +0000 | [diff] [blame] | 211 | bool cropRectIsSet() const { return fCropRect.flags() != 0x0; } |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 212 | |
reed | b3fe1b8 | 2015-06-23 08:29:20 -0700 | [diff] [blame] | 213 | CropRect getCropRect() const { return fCropRect; } |
| 214 | |
senorblanco@chromium.org | 336d1d7 | 2014-01-27 21:03:17 +0000 | [diff] [blame] | 215 | // Default impl returns union of all input bounds. |
senorblanco | e5e7984 | 2016-03-21 14:51:59 -0700 | [diff] [blame] | 216 | virtual SkRect computeFastBounds(const SkRect&) const; |
senorblanco@chromium.org | 336d1d7 | 2014-01-27 21:03:17 +0000 | [diff] [blame] | 217 | |
senorblanco | 0abdf76 | 2015-08-20 11:10:41 -0700 | [diff] [blame] | 218 | // Can this filter DAG compute the resulting bounds of an object-space rectangle? |
senorblanco | 6db0a7b | 2016-04-01 16:41:10 -0700 | [diff] [blame] | 219 | bool canComputeFastBounds() const; |
senorblanco | 0abdf76 | 2015-08-20 11:10:41 -0700 | [diff] [blame] | 220 | |
senorblanco | 8c874ee | 2015-03-20 06:38:17 -0700 | [diff] [blame] | 221 | /** |
reed | 94dd7a5 | 2015-10-14 07:49:35 -0700 | [diff] [blame] | 222 | * If this filter can be represented by another filter + a localMatrix, return that filter, |
| 223 | * else return null. |
| 224 | */ |
robertphillips | 372177e | 2016-03-30 07:32:28 -0700 | [diff] [blame] | 225 | sk_sp<SkImageFilter> makeWithLocalMatrix(const SkMatrix&) const; |
| 226 | |
reed | 94dd7a5 | 2015-10-14 07:49:35 -0700 | [diff] [blame] | 227 | /** |
reed | bb34a8a | 2016-04-23 15:19:07 -0700 | [diff] [blame] | 228 | * ImageFilters can natively handle scaling and translate components in the CTM. Only some of |
| 229 | * them can handle affine (or more complex) matrices. This call returns true iff the filter |
| 230 | * and all of its (non-null) inputs can handle these more complex matrices. |
| 231 | */ |
reed | 96a04f3 | 2016-04-25 09:25:15 -0700 | [diff] [blame] | 232 | bool canHandleComplexCTM() const; |
reed | bb34a8a | 2016-04-23 15:19:07 -0700 | [diff] [blame] | 233 | |
| 234 | /** |
reed | 8c30a81 | 2016-04-20 16:36:51 -0700 | [diff] [blame] | 235 | * Return an imagefilter which transforms its input by the given matrix. |
senorblanco | 8c874ee | 2015-03-20 06:38:17 -0700 | [diff] [blame] | 236 | */ |
robertphillips | ae8c933 | 2016-04-05 15:09:00 -0700 | [diff] [blame] | 237 | static sk_sp<SkImageFilter> MakeMatrixFilter(const SkMatrix& matrix, |
reed | 8c30a81 | 2016-04-20 16:36:51 -0700 | [diff] [blame] | 238 | SkFilterQuality quality, |
robertphillips | ae8c933 | 2016-04-05 15:09:00 -0700 | [diff] [blame] | 239 | sk_sp<SkImageFilter> input); |
reed | 8c30a81 | 2016-04-20 16:36:51 -0700 | [diff] [blame] | 240 | |
robertphillips | f3f5bad | 2014-12-19 13:49:15 -0800 | [diff] [blame] | 241 | SK_TO_STRING_PUREVIRT() |
mtklein | 3b37545 | 2016-04-04 14:57:19 -0700 | [diff] [blame] | 242 | SK_DEFINE_FLATTENABLE_TYPE(SkImageFilter) |
vjiaoblack | e1e5c74 | 2016-08-23 11:13:14 -0700 | [diff] [blame] | 243 | SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP() |
commit-bot@chromium.org | c0b7e10 | 2013-10-23 17:06:21 +0000 | [diff] [blame] | 244 | |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 245 | protected: |
reed | b959ec7 | 2014-07-17 07:03:09 -0700 | [diff] [blame] | 246 | class Common { |
| 247 | public: |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 248 | /** |
| 249 | * Attempt to unflatten the cropRect and the expected number of input filters. |
| 250 | * If any number of input filters is valid, pass -1. |
| 251 | * If this fails (i.e. corrupt buffer or contents) then return false and common will |
| 252 | * be left uninitialized. |
| 253 | * If this returns true, then inputCount() is the number of found input filters, each |
| 254 | * of which may be NULL or a valid imagefilter. |
| 255 | */ |
| 256 | bool unflatten(SkReadBuffer&, int expectedInputs); |
reed | b959ec7 | 2014-07-17 07:03:09 -0700 | [diff] [blame] | 257 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 258 | const CropRect& cropRect() const { return fCropRect; } |
reed | b959ec7 | 2014-07-17 07:03:09 -0700 | [diff] [blame] | 259 | int inputCount() const { return fInputs.count(); } |
robertphillips | 2238c9d | 2016-03-30 13:34:16 -0700 | [diff] [blame] | 260 | sk_sp<SkImageFilter>* inputs() const { return fInputs.get(); } |
reed | b959ec7 | 2014-07-17 07:03:09 -0700 | [diff] [blame] | 261 | |
robertphillips | 2238c9d | 2016-03-30 13:34:16 -0700 | [diff] [blame] | 262 | sk_sp<SkImageFilter> getInput(int index) const { return fInputs[index]; } |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 263 | |
reed | b959ec7 | 2014-07-17 07:03:09 -0700 | [diff] [blame] | 264 | private: |
| 265 | CropRect fCropRect; |
| 266 | // most filters accept at most 2 input-filters |
robertphillips | 2238c9d | 2016-03-30 13:34:16 -0700 | [diff] [blame] | 267 | SkAutoSTArray<2, sk_sp<SkImageFilter>> fInputs; |
reed | b959ec7 | 2014-07-17 07:03:09 -0700 | [diff] [blame] | 268 | |
| 269 | void allocInputs(int count); |
| 270 | }; |
| 271 | |
robertphillips | 372177e | 2016-03-30 07:32:28 -0700 | [diff] [blame] | 272 | SkImageFilter(sk_sp<SkImageFilter>* inputs, int inputCount, const CropRect* cropRect); |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 273 | |
Brian Salomon | d3b6597 | 2017-03-22 12:05:03 -0400 | [diff] [blame] | 274 | ~SkImageFilter() override; |
senorblanco@chromium.org | 9f25de7 | 2012-10-10 20:36:13 +0000 | [diff] [blame] | 275 | |
commit-bot@chromium.org | c84728d | 2013-12-04 20:07:47 +0000 | [diff] [blame] | 276 | /** |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 277 | * Constructs a new SkImageFilter read from an SkReadBuffer object. |
commit-bot@chromium.org | c84728d | 2013-12-04 20:07:47 +0000 | [diff] [blame] | 278 | * |
| 279 | * @param inputCount The exact number of inputs expected for this SkImageFilter object. |
| 280 | * -1 can be used if the filter accepts any number of inputs. |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 281 | * @param rb SkReadBuffer object from which the SkImageFilter is read. |
commit-bot@chromium.org | c84728d | 2013-12-04 20:07:47 +0000 | [diff] [blame] | 282 | */ |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 283 | explicit SkImageFilter(int inputCount, SkReadBuffer& rb); |
senorblanco@chromium.org | 9f25de7 | 2012-10-10 20:36:13 +0000 | [diff] [blame] | 284 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 285 | void flatten(SkWriteBuffer&) const override; |
reed@google.com | 32d25b6 | 2011-12-20 16:19:00 +0000 | [diff] [blame] | 286 | |
Matt Sarett | 31abf1f | 2017-04-07 16:54:04 -0400 | [diff] [blame] | 287 | const CropRect* getCropRectIfSet() const { |
| 288 | return this->cropRectIsSet() ? &fCropRect : nullptr; |
| 289 | } |
| 290 | |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 291 | /** |
| 292 | * This is the virtual which should be overridden by the derived class |
| 293 | * to perform image filtering. |
| 294 | * |
| 295 | * src is the original primitive bitmap. If the filter has a connected |
| 296 | * input, it should recurse on that input and use that in place of src. |
| 297 | * |
| 298 | * The matrix is the current matrix on the canvas. |
| 299 | * |
| 300 | * Offset is the amount to translate the resulting image relative to the |
| 301 | * src when it is drawn. This is an out-param. |
| 302 | * |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 303 | * If the result image cannot be created (either because of error or if, say, the result |
| 304 | * is entirely clipped out), this should return nullptr. |
| 305 | * Callers that affect transparent black should explicitly handle nullptr |
| 306 | * results and press on. In the error case this behavior will produce a better result |
| 307 | * than nothing and is necessary for the clipped out case. |
| 308 | * If the return value is nullptr then offset should be ignored. |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 309 | */ |
robertphillips | 2302de9 | 2016-03-24 07:26:32 -0700 | [diff] [blame] | 310 | virtual sk_sp<SkSpecialImage> onFilterImage(SkSpecialImage* src, const Context&, |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 311 | SkIPoint* offset) const = 0; |
robertphillips | eaf086e | 2016-03-07 04:51:10 -0800 | [diff] [blame] | 312 | |
senorblanco | d8ff5b3 | 2016-01-28 08:23:02 -0800 | [diff] [blame] | 313 | /** |
senorblanco | e5e7984 | 2016-03-21 14:51:59 -0700 | [diff] [blame] | 314 | * This function recurses into its inputs with the given rect (first |
senorblanco | d8ff5b3 | 2016-01-28 08:23:02 -0800 | [diff] [blame] | 315 | * argument), calls filterBounds() with the given map direction on each, |
senorblanco | e5e7984 | 2016-03-21 14:51:59 -0700 | [diff] [blame] | 316 | * and returns the union of those results. If a derived class has special |
| 317 | * recursion requirements (e.g., it has an input which does not participate |
| 318 | * in bounds computation), it can be overridden here. |
senorblanco | d8ff5b3 | 2016-01-28 08:23:02 -0800 | [diff] [blame] | 319 | * |
| 320 | * Note that this function is *not* responsible for mapping the rect for |
| 321 | * this node's filter bounds requirements (i.e., calling |
| 322 | * onFilterNodeBounds()); that is handled by filterBounds(). |
| 323 | */ |
senorblanco | e5e7984 | 2016-03-21 14:51:59 -0700 | [diff] [blame] | 324 | virtual SkIRect onFilterBounds(const SkIRect&, const SkMatrix&, MapDirection) const; |
senorblanco | db64af3 | 2015-12-09 10:11:43 -0800 | [diff] [blame] | 325 | |
| 326 | /** |
| 327 | * Performs a forwards or reverse mapping of the given rect to accommodate |
| 328 | * this filter's margin requirements. kForward_MapDirection is used to |
| 329 | * determine the destination pixels which would be touched by filtering |
| 330 | * the given given source rect (e.g., given source bitmap bounds, |
| 331 | * determine the optimal bounds of the filtered offscreen bitmap). |
| 332 | * kReverse_MapDirection is used to determine which pixels of the |
| 333 | * input(s) would be required to fill the given destination rect |
| 334 | * (e.g., clip bounds). NOTE: these operations may not be the |
| 335 | * inverse of the other. For example, blurring expands the given rect |
| 336 | * in both forward and reverse directions. Unlike |
| 337 | * onFilterBounds(), this function is non-recursive. |
| 338 | */ |
senorblanco | e5e7984 | 2016-03-21 14:51:59 -0700 | [diff] [blame] | 339 | virtual SkIRect onFilterNodeBounds(const SkIRect&, const SkMatrix&, MapDirection) const; |
reed@google.com | 894aa9a | 2011-09-23 14:49:49 +0000 | [diff] [blame] | 340 | |
senorblanco | b9519f8 | 2015-10-15 12:15:13 -0700 | [diff] [blame] | 341 | // Helper function which invokes filter processing on the input at the |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 342 | // specified "index". If the input is null, it returns "src" and leaves |
| 343 | // "offset" untouched. If the input is non-null, it |
| 344 | // calls filterImage() on that input, and returns the result. |
| 345 | sk_sp<SkSpecialImage> filterInput(int index, |
| 346 | SkSpecialImage* src, |
| 347 | const Context&, |
| 348 | SkIPoint* offset) const; |
senorblanco | b9519f8 | 2015-10-15 12:15:13 -0700 | [diff] [blame] | 349 | |
reed | cedc36f | 2015-03-08 04:42:52 -0700 | [diff] [blame] | 350 | /** |
| 351 | * Return true (and return a ref'd colorfilter) if this node in the DAG is just a |
| 352 | * colorfilter w/o CropRect constraints. |
| 353 | */ |
| 354 | virtual bool onIsColorFilterNode(SkColorFilter** /*filterPtr*/) const { |
| 355 | return false; |
| 356 | } |
| 357 | |
reed | 96a04f3 | 2016-04-25 09:25:15 -0700 | [diff] [blame] | 358 | /** |
| 359 | * Override this to describe the behavior of your subclass - as a leaf node. The caller will |
| 360 | * take care of calling your inputs (and return false if any of them could not handle it). |
| 361 | */ |
| 362 | virtual bool onCanHandleComplexCTM() const { return false; } |
reed | bb34a8a | 2016-04-23 15:19:07 -0700 | [diff] [blame] | 363 | |
robertphillips | af9b8c8 | 2016-04-12 11:02:25 -0700 | [diff] [blame] | 364 | /** Given a "srcBounds" rect, computes destination bounds for this filter. |
| 365 | * "dstBounds" are computed by transforming the crop rect by the context's |
| 366 | * CTM, applying it to the initial bounds, and intersecting the result with |
| 367 | * the context's clip bounds. "srcBounds" (if non-null) are computed by |
| 368 | * intersecting the initial bounds with "dstBounds", to ensure that we never |
| 369 | * sample outside of the crop rect (this restriction may be relaxed in the |
senorblanco | afec27f | 2016-02-16 09:11:18 -0800 | [diff] [blame] | 370 | * future). |
senorblanco@chromium.org | 1182529 | 2014-03-14 17:44:41 +0000 | [diff] [blame] | 371 | */ |
senorblanco | afec27f | 2016-02-16 09:11:18 -0800 | [diff] [blame] | 372 | bool applyCropRect(const Context&, const SkIRect& srcBounds, SkIRect* dstBounds) const; |
senorblanco@chromium.org | 1182529 | 2014-03-14 17:44:41 +0000 | [diff] [blame] | 373 | |
senorblanco | afec27f | 2016-02-16 09:11:18 -0800 | [diff] [blame] | 374 | /** A variant of the above call which takes the original source bitmap and |
| 375 | * source offset. If the resulting crop rect is not entirely contained by |
| 376 | * the source bitmap's bounds, it creates a new bitmap in "result" and |
| 377 | * pads the edges with transparent black. In that case, the srcOffset is |
| 378 | * modified to be the same as the bounds, since no further adjustment is |
| 379 | * needed by the caller. This version should only be used by filters |
| 380 | * which are not capable of processing a smaller source bitmap into a |
| 381 | * larger destination. |
senorblanco@chromium.org | 1182529 | 2014-03-14 17:44:41 +0000 | [diff] [blame] | 382 | */ |
robertphillips | 2302de9 | 2016-03-24 07:26:32 -0700 | [diff] [blame] | 383 | sk_sp<SkSpecialImage> applyCropRect(const Context&, SkSpecialImage* src, SkIPoint* srcOffset, |
| 384 | SkIRect* bounds) const; |
robertphillips | eaf086e | 2016-03-07 04:51:10 -0800 | [diff] [blame] | 385 | |
senorblanco@chromium.org | 1aa6872 | 2013-10-17 19:35:09 +0000 | [diff] [blame] | 386 | /** |
senorblanco | db64af3 | 2015-12-09 10:11:43 -0800 | [diff] [blame] | 387 | * Creates a modified Context for use when recursing up the image filter DAG. |
| 388 | * The clip bounds are adjusted to accommodate any margins that this |
| 389 | * filter requires by calling this node's |
| 390 | * onFilterNodeBounds(..., kReverse_MapDirection). |
| 391 | */ |
| 392 | Context mapContext(const Context& ctx) const; |
| 393 | |
Brian Osman | 615d66d | 2016-12-29 09:18:20 -0500 | [diff] [blame] | 394 | #if SK_SUPPORT_GPU |
| 395 | /** |
| 396 | * Returns a version of the passed-in image (possibly the original), that is in a colorspace |
| 397 | * with the same gamut as the one from the OutputProperties. This allows filters that do many |
| 398 | * texture samples to guarantee that any color space conversion has happened before running. |
| 399 | */ |
| 400 | static sk_sp<SkSpecialImage> ImageToColorSpace(SkSpecialImage* src, const OutputProperties&); |
| 401 | #endif |
| 402 | |
Matt Sarett | 31abf1f | 2017-04-07 16:54:04 -0400 | [diff] [blame] | 403 | /** |
| 404 | * Returns an image filter transformed into a new color space via the |xformer|. |
| 405 | */ |
| 406 | sk_sp<SkImageFilter> makeColorSpace(SkColorSpaceXformer* xformer) const { |
| 407 | return this->onMakeColorSpace(xformer); |
| 408 | } |
| 409 | virtual sk_sp<SkImageFilter> onMakeColorSpace(SkColorSpaceXformer*) const { |
| 410 | return sk_ref_sp(const_cast<SkImageFilter*>(this)); |
| 411 | } |
| 412 | |
reed@google.com | 894aa9a | 2011-09-23 14:49:49 +0000 | [diff] [blame] | 413 | private: |
Matt Sarett | 6d72ed9 | 2017-04-10 16:35:33 -0400 | [diff] [blame^] | 414 | // For makeColorSpace(). |
Matt Sarett | 31abf1f | 2017-04-07 16:54:04 -0400 | [diff] [blame] | 415 | friend class ArithmeticImageFilterImpl; |
Matt Sarett | 6d72ed9 | 2017-04-10 16:35:33 -0400 | [diff] [blame^] | 416 | friend class SkAlphaThresholdFilterImpl; |
| 417 | friend class SkBlurImageFilterImpl; |
| 418 | friend class SkColorFilterImageFilter; |
Matt Sarett | 31abf1f | 2017-04-07 16:54:04 -0400 | [diff] [blame] | 419 | friend class SkColorSpaceXformer; |
Matt Sarett | 6d72ed9 | 2017-04-10 16:35:33 -0400 | [diff] [blame^] | 420 | friend class SkComposeImageFilter; |
| 421 | friend class SkDisplacementMapEffect; |
| 422 | friend class SkDropShadowImageFilter; |
| 423 | friend class SkImageSource; |
| 424 | friend class SkMagnifierImageFilter; |
| 425 | friend class SkMatrixConvolutionImageFilter; |
| 426 | friend class SkMergeImageFilter; |
| 427 | friend class SkMorphologyImageFilter; |
| 428 | friend class SkOffsetImageFilter; |
| 429 | friend class SkTileImageFilter; |
| 430 | friend class SkXfermodeImageFilter_Base; |
| 431 | |
mtklein | 242397a | 2015-09-29 12:17:08 -0700 | [diff] [blame] | 432 | friend class SkGraphics; |
Matt Sarett | 31abf1f | 2017-04-07 16:54:04 -0400 | [diff] [blame] | 433 | |
mtklein | 242397a | 2015-09-29 12:17:08 -0700 | [diff] [blame] | 434 | static void PurgeCache(); |
| 435 | |
robertphillips | 6b13473 | 2016-04-15 09:58:37 -0700 | [diff] [blame] | 436 | void init(sk_sp<SkImageFilter>* inputs, int inputCount, const CropRect* cropRect); |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 437 | |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 438 | bool usesSrcInput() const { return fUsesSrcInput; } |
senorblanco | 6db0a7b | 2016-04-01 16:41:10 -0700 | [diff] [blame] | 439 | virtual bool affectsTransparentBlack() const { return false; } |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 440 | |
robertphillips | 6b13473 | 2016-04-15 09:58:37 -0700 | [diff] [blame] | 441 | SkAutoSTArray<2, sk_sp<SkImageFilter>> fInputs; |
| 442 | |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 443 | bool fUsesSrcInput; |
senorblanco@chromium.org | b295fb6 | 2013-10-10 13:51:19 +0000 | [diff] [blame] | 444 | CropRect fCropRect; |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 445 | uint32_t fUniqueID; // Globally unique |
senorblanco | 900c367 | 2016-04-27 11:31:23 -0700 | [diff] [blame] | 446 | mutable SkTArray<SkImageFilterCacheKey> fCacheKeys; |
xidachen | 2352696 | 2016-02-01 05:27:16 -0800 | [diff] [blame] | 447 | mutable SkMutex fMutex; |
robertphillips | 6b13473 | 2016-04-15 09:58:37 -0700 | [diff] [blame] | 448 | typedef SkFlattenable INHERITED; |
reed@google.com | 894aa9a | 2011-09-23 14:49:49 +0000 | [diff] [blame] | 449 | }; |
| 450 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 451 | /** |
| 452 | * Helper to unflatten the common data, and return NULL if we fail. |
| 453 | */ |
| 454 | #define SK_IMAGEFILTER_UNFLATTEN_COMMON(localVar, expectedCount) \ |
| 455 | Common localVar; \ |
| 456 | do { \ |
| 457 | if (!localVar.unflatten(buffer, expectedCount)) { \ |
| 458 | return NULL; \ |
| 459 | } \ |
| 460 | } while (0) |
| 461 | |
reed@google.com | 894aa9a | 2011-09-23 14:49:49 +0000 | [diff] [blame] | 462 | #endif |