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