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 | |
senorblanco | 8c874ee | 2015-03-20 06:38:17 -0700 | [diff] [blame] | 11 | #include "SkFilterQuality.h" |
reed@google.com | 894aa9a | 2011-09-23 14:49:49 +0000 | [diff] [blame] | 12 | #include "SkFlattenable.h" |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 13 | #include "SkMatrix.h" |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 14 | #include "SkRect.h" |
reed | 2c55d7b | 2015-06-09 08:18:39 -0700 | [diff] [blame] | 15 | #include "SkSurfaceProps.h" |
reed | b959ec7 | 2014-07-17 07:03:09 -0700 | [diff] [blame] | 16 | #include "SkTemplates.h" |
reed@google.com | 894aa9a | 2011-09-23 14:49:49 +0000 | [diff] [blame] | 17 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 18 | class GrFragmentProcessor; |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 19 | class GrTexture; |
reed | 2c55d7b | 2015-06-09 08:18:39 -0700 | [diff] [blame] | 20 | class SkBaseDevice; |
| 21 | class SkBitmap; |
| 22 | class SkColorFilter; |
| 23 | struct SkIPoint; |
reed@google.com | 15356a6 | 2011-11-03 19:29:08 +0000 | [diff] [blame] | 24 | |
| 25 | /** |
reed@google.com | 15356a6 | 2011-11-03 19:29:08 +0000 | [diff] [blame] | 26 | * Base class for image filters. If one is installed in the paint, then |
| 27 | * all drawing occurs as usual, but it is as if the drawing happened into an |
| 28 | * offscreen (before the xfermode is applied). This offscreen bitmap will |
| 29 | * then be handed to the imagefilter, who in turn creates a new bitmap which |
| 30 | * 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] | 31 | */ |
senorblanco@chromium.org | 54e01b2 | 2011-11-16 18:20:47 +0000 | [diff] [blame] | 32 | class SK_API SkImageFilter : public SkFlattenable { |
reed@google.com | 894aa9a | 2011-09-23 14:49:49 +0000 | [diff] [blame] | 33 | public: |
senorblanco@chromium.org | 3f1f2a3 | 2013-10-16 18:07:48 +0000 | [diff] [blame] | 34 | class CropRect { |
| 35 | public: |
senorblanco@chromium.org | b295fb6 | 2013-10-10 13:51:19 +0000 | [diff] [blame] | 36 | enum CropEdge { |
| 37 | kHasLeft_CropEdge = 0x01, |
| 38 | kHasTop_CropEdge = 0x02, |
| 39 | kHasRight_CropEdge = 0x04, |
| 40 | kHasBottom_CropEdge = 0x08, |
| 41 | kHasAll_CropEdge = 0x0F, |
| 42 | }; |
| 43 | CropRect() {} |
| 44 | explicit CropRect(const SkRect& rect, uint32_t flags = kHasAll_CropEdge) : fRect(rect), fFlags(flags) {} |
senorblanco@chromium.org | 3f1f2a3 | 2013-10-16 18:07:48 +0000 | [diff] [blame] | 45 | uint32_t flags() const { return fFlags; } |
| 46 | const SkRect& rect() const { return fRect; } |
robertphillips | 157bcd0 | 2015-06-26 08:07:39 -0700 | [diff] [blame] | 47 | #ifndef SK_IGNORE_TO_STRING |
| 48 | void toString(SkString* str) const; |
| 49 | #endif |
senorblanco@chromium.org | 3f1f2a3 | 2013-10-16 18:07:48 +0000 | [diff] [blame] | 50 | private: |
| 51 | SkRect fRect; |
| 52 | uint32_t fFlags; |
senorblanco@chromium.org | b295fb6 | 2013-10-10 13:51:19 +0000 | [diff] [blame] | 53 | }; |
senorblanco@chromium.org | b295fb6 | 2013-10-10 13:51:19 +0000 | [diff] [blame] | 54 | |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 55 | // This cache maps from (filter's unique ID + CTM + clipBounds + src bitmap generation ID) to |
| 56 | // (result, offset). |
senorblanco | be129b2 | 2014-08-08 07:14:35 -0700 | [diff] [blame] | 57 | class Cache : public SkRefCnt { |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 58 | public: |
| 59 | struct Key; |
senorblanco | be129b2 | 2014-08-08 07:14:35 -0700 | [diff] [blame] | 60 | virtual ~Cache() {} |
| 61 | static Cache* Create(size_t maxBytes); |
| 62 | static Cache* Get(); |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 63 | virtual bool get(const Key& key, SkBitmap* result, SkIPoint* offset) const = 0; |
| 64 | virtual void set(const Key& key, const SkBitmap& result, const SkIPoint& offset) = 0; |
reed | 67ca2a9 | 2015-05-20 13:22:58 -0700 | [diff] [blame] | 65 | virtual void purge() {} |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 66 | }; |
| 67 | |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 68 | class Context { |
| 69 | public: |
senorblanco | be129b2 | 2014-08-08 07:14:35 -0700 | [diff] [blame] | 70 | Context(const SkMatrix& ctm, const SkIRect& clipBounds, Cache* cache) : |
commit-bot@chromium.org | f7efa50 | 2014-04-11 18:57:00 +0000 | [diff] [blame] | 71 | fCTM(ctm), fClipBounds(clipBounds), fCache(cache) { |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 72 | } |
| 73 | const SkMatrix& ctm() const { return fCTM; } |
| 74 | const SkIRect& clipBounds() const { return fClipBounds; } |
senorblanco | be129b2 | 2014-08-08 07:14:35 -0700 | [diff] [blame] | 75 | Cache* cache() const { return fCache; } |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 76 | private: |
| 77 | SkMatrix fCTM; |
| 78 | SkIRect fClipBounds; |
reed | 2c55d7b | 2015-06-09 08:18:39 -0700 | [diff] [blame] | 79 | Cache* fCache; |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 80 | }; |
| 81 | |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 82 | class Proxy { |
| 83 | public: |
robertphillips | efbffed | 2015-06-22 12:06:08 -0700 | [diff] [blame] | 84 | Proxy(SkBaseDevice* device) : fDevice(device) { } |
mtklein | 2766c00 | 2015-06-26 11:45:03 -0700 | [diff] [blame^] | 85 | |
reed | 2c55d7b | 2015-06-09 08:18:39 -0700 | [diff] [blame] | 86 | SkBaseDevice* createDevice(int width, int height); |
robertphillips | efbffed | 2015-06-22 12:06:08 -0700 | [diff] [blame] | 87 | |
| 88 | // Returns true if the proxy handled the filter itself. If this returns |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 89 | // false then the filter's code will be called. |
reed | 2c55d7b | 2015-06-09 08:18:39 -0700 | [diff] [blame] | 90 | bool filterImage(const SkImageFilter*, const SkBitmap& src, const SkImageFilter::Context&, |
| 91 | SkBitmap* result, SkIPoint* offset); |
robertphillips | efbffed | 2015-06-22 12:06:08 -0700 | [diff] [blame] | 92 | |
reed | 2c55d7b | 2015-06-09 08:18:39 -0700 | [diff] [blame] | 93 | private: |
robertphillips | efbffed | 2015-06-22 12:06:08 -0700 | [diff] [blame] | 94 | SkBaseDevice* fDevice; |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 95 | }; |
mtklein | 2766c00 | 2015-06-26 11:45:03 -0700 | [diff] [blame^] | 96 | |
reed@google.com | 894aa9a | 2011-09-23 14:49:49 +0000 | [diff] [blame] | 97 | |
| 98 | /** |
| 99 | * Request a new (result) image to be created from the src image. |
| 100 | * If the src has no pixels (isNull()) then the request just wants to |
| 101 | * receive the config and width/height of the result. |
| 102 | * |
| 103 | * The matrix is the current matrix on the canvas. |
| 104 | * |
| 105 | * 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] | 106 | * src when it is drawn. This is an out-param. |
reed@google.com | 894aa9a | 2011-09-23 14:49:49 +0000 | [diff] [blame] | 107 | * |
| 108 | * If the result image cannot be created, return false, in which case both |
| 109 | * the result and offset parameters will be ignored by the caller. |
| 110 | */ |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 111 | bool filterImage(Proxy*, const SkBitmap& src, const Context&, |
commit-bot@chromium.org | ae761f7 | 2014-02-05 22:32:02 +0000 | [diff] [blame] | 112 | SkBitmap* result, SkIPoint* offset) const; |
reed@google.com | 15356a6 | 2011-11-03 19:29:08 +0000 | [diff] [blame] | 113 | |
| 114 | /** |
reed@google.com | 32d25b6 | 2011-12-20 16:19:00 +0000 | [diff] [blame] | 115 | * Given the src bounds of an image, this returns the bounds of the result |
| 116 | * image after the filter has been applied. |
| 117 | */ |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 118 | bool filterBounds(const SkIRect& src, const SkMatrix& ctm, SkIRect* dst) const; |
reed@google.com | 32d25b6 | 2011-12-20 16:19:00 +0000 | [diff] [blame] | 119 | |
| 120 | /** |
senorblanco@chromium.org | 302cffb | 2012-08-01 20:16:34 +0000 | [diff] [blame] | 121 | * Returns true if the filter can be processed on the GPU. This is most |
| 122 | * often used for multi-pass effects, where intermediate results must be |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 123 | * rendered to textures. For single-pass effects, use asFragmentProcessor(). |
| 124 | * The default implementation returns asFragmentProcessor(NULL, NULL, SkMatrix::I(), |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame] | 125 | * SkIRect()). |
reed@google.com | 15356a6 | 2011-11-03 19:29:08 +0000 | [diff] [blame] | 126 | */ |
senorblanco@chromium.org | 302cffb | 2012-08-01 20:16:34 +0000 | [diff] [blame] | 127 | virtual bool canFilterImageGPU() const; |
reed@google.com | 894aa9a | 2011-09-23 14:49:49 +0000 | [diff] [blame] | 128 | |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 129 | /** |
skia.committer@gmail.com | 3284017 | 2013-04-09 07:01:27 +0000 | [diff] [blame] | 130 | * Process this image filter on the GPU. This is most often used for |
senorblanco@chromium.org | d043cce | 2013-04-08 19:43:22 +0000 | [diff] [blame] | 131 | * multi-pass effects, where intermediate results must be rendered to |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 132 | * textures. For single-pass effects, use asFragmentProcessor(). src is the |
senorblanco@chromium.org | d043cce | 2013-04-08 19:43:22 +0000 | [diff] [blame] | 133 | * source image for processing, as a texture-backed bitmap. result is |
| 134 | * the destination bitmap, which should contain a texture-backed pixelref |
skia.committer@gmail.com | de2e4e8 | 2013-07-11 07:01:01 +0000 | [diff] [blame] | 135 | * on success. offset is the amount to translate the resulting image |
commit-bot@chromium.org | 7b32070 | 2013-07-10 21:22:18 +0000 | [diff] [blame] | 136 | * relative to the src when it is drawn. The default implementation does |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 137 | * single-pass processing using asFragmentProcessor(). |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 138 | */ |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 139 | virtual bool filterImageGPU(Proxy*, const SkBitmap& src, const Context&, |
commit-bot@chromium.org | ae761f7 | 2014-02-05 22:32:02 +0000 | [diff] [blame] | 140 | SkBitmap* result, SkIPoint* offset) const; |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 141 | |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 142 | /** |
sugoi@google.com | a1c511b | 2013-02-21 15:02:28 +0000 | [diff] [blame] | 143 | * 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] | 144 | * "filterPtr" parameter if it can. Does nothing otherwise. |
| 145 | * If this returns false, then the filterPtr is unchanged. |
| 146 | * If this returns true, then if filterPtr is not null, it must be set to a ref'd colorfitler |
| 147 | * (i.e. it may not be set to NULL). |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 148 | */ |
reed | cedc36f | 2015-03-08 04:42:52 -0700 | [diff] [blame] | 149 | bool isColorFilterNode(SkColorFilter** filterPtr) const { |
| 150 | return this->onIsColorFilterNode(filterPtr); |
| 151 | } |
| 152 | |
| 153 | // DEPRECATED : use isColorFilterNode() instead |
| 154 | bool asColorFilter(SkColorFilter** filterPtr) const { |
| 155 | return this->isColorFilterNode(filterPtr); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Returns true (and optionally returns a ref'd filter) if this imagefilter can be completely |
| 160 | * replaced by the returned colorfilter. i.e. the two effects will affect drawing in the |
| 161 | * same way. |
| 162 | */ |
| 163 | bool asAColorFilter(SkColorFilter** filterPtr) const { |
| 164 | return this->countInputs() > 0 && |
| 165 | NULL == this->getInput(0) && |
| 166 | this->isColorFilterNode(filterPtr); |
| 167 | } |
senorblanco@chromium.org | 9f25de7 | 2012-10-10 20:36:13 +0000 | [diff] [blame] | 168 | |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 169 | /** |
| 170 | * Returns the number of inputs this filter will accept (some inputs can |
| 171 | * be NULL). |
| 172 | */ |
| 173 | int countInputs() const { return fInputCount; } |
| 174 | |
| 175 | /** |
| 176 | * Returns the input filter at a given index, or NULL if no input is |
| 177 | * connected. The indices used are filter-specific. |
| 178 | */ |
| 179 | SkImageFilter* getInput(int i) const { |
| 180 | SkASSERT(i < fInputCount); |
| 181 | return fInputs[i]; |
| 182 | } |
| 183 | |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 184 | /** |
senorblanco@chromium.org | 3f1f2a3 | 2013-10-16 18:07:48 +0000 | [diff] [blame] | 185 | * Returns whether any edges of the crop rect have been set. The crop |
| 186 | * rect is set at construction time, and determines which pixels from the |
| 187 | * input image will be processed. The size of the crop rect should be |
| 188 | * used as the size of the destination image. The origin of this rect |
| 189 | * should be used to offset access to the input images, and should also |
| 190 | * be added to the "offset" parameter in onFilterImage and |
| 191 | * filterImageGPU(). (The latter ensures that the resulting buffer is |
| 192 | * drawn in the correct location.) |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 193 | */ |
senorblanco@chromium.org | 3f1f2a3 | 2013-10-16 18:07:48 +0000 | [diff] [blame] | 194 | bool cropRectIsSet() const { return fCropRect.flags() != 0x0; } |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 195 | |
reed | b3fe1b8 | 2015-06-23 08:29:20 -0700 | [diff] [blame] | 196 | CropRect getCropRect() const { return fCropRect; } |
| 197 | |
senorblanco@chromium.org | 336d1d7 | 2014-01-27 21:03:17 +0000 | [diff] [blame] | 198 | // Default impl returns union of all input bounds. |
| 199 | virtual void computeFastBounds(const SkRect&, SkRect*) const; |
| 200 | |
senorblanco | 8c874ee | 2015-03-20 06:38:17 -0700 | [diff] [blame] | 201 | /** |
| 202 | * Create an SkMatrixImageFilter, which transforms its input by the given matrix. |
| 203 | */ |
| 204 | static SkImageFilter* CreateMatrixFilter(const SkMatrix& matrix, |
| 205 | SkFilterQuality, |
| 206 | SkImageFilter* input = NULL); |
| 207 | |
kkinnunen | 4509517 | 2014-07-29 06:12:49 -0700 | [diff] [blame] | 208 | #if SK_SUPPORT_GPU |
senorblanco@chromium.org | 6aa6fec | 2014-03-03 22:13:56 +0000 | [diff] [blame] | 209 | /** |
| 210 | * Wrap the given texture in a texture-backed SkBitmap. |
| 211 | */ |
| 212 | static void WrapTexture(GrTexture* texture, int width, int height, SkBitmap* result); |
| 213 | |
| 214 | /** |
| 215 | * Recursively evaluate this filter on the GPU. If the filter has no GPU |
| 216 | * implementation, it will be processed in software and uploaded to the GPU. |
| 217 | */ |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 218 | bool getInputResultGPU(SkImageFilter::Proxy* proxy, const SkBitmap& src, const Context&, |
senorblanco@chromium.org | 6aa6fec | 2014-03-03 22:13:56 +0000 | [diff] [blame] | 219 | SkBitmap* result, SkIPoint* offset) const; |
| 220 | #endif |
| 221 | |
robertphillips | f3f5bad | 2014-12-19 13:49:15 -0800 | [diff] [blame] | 222 | SK_TO_STRING_PUREVIRT() |
commit-bot@chromium.org | c0b7e10 | 2013-10-23 17:06:21 +0000 | [diff] [blame] | 223 | SK_DEFINE_FLATTENABLE_TYPE(SkImageFilter) |
| 224 | |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 225 | protected: |
reed | b959ec7 | 2014-07-17 07:03:09 -0700 | [diff] [blame] | 226 | class Common { |
| 227 | public: |
| 228 | Common() {} |
| 229 | ~Common(); |
| 230 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 231 | /** |
| 232 | * Attempt to unflatten the cropRect and the expected number of input filters. |
| 233 | * If any number of input filters is valid, pass -1. |
| 234 | * If this fails (i.e. corrupt buffer or contents) then return false and common will |
| 235 | * be left uninitialized. |
| 236 | * If this returns true, then inputCount() is the number of found input filters, each |
| 237 | * of which may be NULL or a valid imagefilter. |
| 238 | */ |
| 239 | bool unflatten(SkReadBuffer&, int expectedInputs); |
reed | b959ec7 | 2014-07-17 07:03:09 -0700 | [diff] [blame] | 240 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 241 | const CropRect& cropRect() const { return fCropRect; } |
reed | b959ec7 | 2014-07-17 07:03:09 -0700 | [diff] [blame] | 242 | int inputCount() const { return fInputs.count(); } |
| 243 | SkImageFilter** inputs() const { return fInputs.get(); } |
| 244 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 245 | SkImageFilter* getInput(int index) const { return fInputs[index]; } |
| 246 | |
reed | b959ec7 | 2014-07-17 07:03:09 -0700 | [diff] [blame] | 247 | // If the caller wants a copy of the inputs, call this and it will transfer ownership |
| 248 | // of the unflattened input filters to the caller. This is just a short-cut for copying |
| 249 | // the inputs, calling ref() on each, and then waiting for Common's destructor to call |
| 250 | // unref() on each. |
| 251 | void detachInputs(SkImageFilter** inputs); |
| 252 | |
| 253 | private: |
| 254 | CropRect fCropRect; |
| 255 | // most filters accept at most 2 input-filters |
| 256 | SkAutoSTArray<2, SkImageFilter*> fInputs; |
| 257 | |
| 258 | void allocInputs(int count); |
| 259 | }; |
| 260 | |
senorblanco | 24e06d5 | 2015-03-18 12:11:33 -0700 | [diff] [blame] | 261 | SkImageFilter(int inputCount, SkImageFilter** inputs, const CropRect* cropRect = NULL); |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 262 | |
senorblanco@chromium.org | 9f25de7 | 2012-10-10 20:36:13 +0000 | [diff] [blame] | 263 | virtual ~SkImageFilter(); |
| 264 | |
commit-bot@chromium.org | c84728d | 2013-12-04 20:07:47 +0000 | [diff] [blame] | 265 | /** |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 266 | * Constructs a new SkImageFilter read from an SkReadBuffer object. |
commit-bot@chromium.org | c84728d | 2013-12-04 20:07:47 +0000 | [diff] [blame] | 267 | * |
| 268 | * @param inputCount The exact number of inputs expected for this SkImageFilter object. |
| 269 | * -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] | 270 | * @param rb SkReadBuffer object from which the SkImageFilter is read. |
commit-bot@chromium.org | c84728d | 2013-12-04 20:07:47 +0000 | [diff] [blame] | 271 | */ |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 272 | explicit SkImageFilter(int inputCount, SkReadBuffer& rb); |
senorblanco@chromium.org | 9f25de7 | 2012-10-10 20:36:13 +0000 | [diff] [blame] | 273 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 274 | void flatten(SkWriteBuffer&) const override; |
reed@google.com | 32d25b6 | 2011-12-20 16:19:00 +0000 | [diff] [blame] | 275 | |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 276 | /** |
| 277 | * This is the virtual which should be overridden by the derived class |
| 278 | * to perform image filtering. |
| 279 | * |
| 280 | * src is the original primitive bitmap. If the filter has a connected |
| 281 | * input, it should recurse on that input and use that in place of src. |
| 282 | * |
| 283 | * The matrix is the current matrix on the canvas. |
| 284 | * |
| 285 | * Offset is the amount to translate the resulting image relative to the |
| 286 | * src when it is drawn. This is an out-param. |
| 287 | * |
| 288 | * If the result image cannot be created, this should false, in which |
| 289 | * case both the result and offset parameters will be ignored by the |
| 290 | * caller. |
| 291 | */ |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 292 | virtual bool onFilterImage(Proxy*, const SkBitmap& src, const Context&, |
commit-bot@chromium.org | ae761f7 | 2014-02-05 22:32:02 +0000 | [diff] [blame] | 293 | SkBitmap* result, SkIPoint* offset) const; |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 294 | // Given the bounds of the destination rect to be filled in device |
| 295 | // coordinates (first parameter), and the CTM, compute (conservatively) |
| 296 | // which rect of the source image would be required (third parameter). |
| 297 | // Used for clipping and temp-buffer allocations, so the result need not |
| 298 | // be exact, but should never be smaller than the real answer. The default |
| 299 | // implementation recursively unions all input bounds, or returns false if |
| 300 | // no inputs. |
| 301 | virtual bool onFilterBounds(const SkIRect&, const SkMatrix&, SkIRect*) const; |
reed@google.com | 894aa9a | 2011-09-23 14:49:49 +0000 | [diff] [blame] | 302 | |
reed | cedc36f | 2015-03-08 04:42:52 -0700 | [diff] [blame] | 303 | /** |
| 304 | * Return true (and return a ref'd colorfilter) if this node in the DAG is just a |
| 305 | * colorfilter w/o CropRect constraints. |
| 306 | */ |
| 307 | virtual bool onIsColorFilterNode(SkColorFilter** /*filterPtr*/) const { |
| 308 | return false; |
| 309 | } |
| 310 | |
senorblanco@chromium.org | 1182529 | 2014-03-14 17:44:41 +0000 | [diff] [blame] | 311 | /** Computes source bounds as the src bitmap bounds offset by srcOffset. |
| 312 | * Apply the transformed crop rect to the bounds if any of the |
| 313 | * corresponding edge flags are set. Intersects the result against the |
| 314 | * context's clipBounds, and returns the result in "bounds". If there is |
| 315 | * no intersection, returns false and leaves "bounds" unchanged. |
| 316 | */ |
| 317 | bool applyCropRect(const Context&, const SkBitmap& src, const SkIPoint& srcOffset, |
| 318 | SkIRect* bounds) const; |
| 319 | |
| 320 | /** Same as the above call, except that if the resulting crop rect is not |
| 321 | * entirely contained by the source bitmap's bounds, it creates a new |
| 322 | * bitmap in "result" and pads the edges with transparent black. In that |
| 323 | * case, the srcOffset is modified to be the same as the bounds, since no |
| 324 | * further adjustment is needed by the caller. This version should only |
| 325 | * be used by filters which are not capable of processing a smaller |
| 326 | * source bitmap into a larger destination. |
| 327 | */ |
| 328 | bool applyCropRect(const Context&, Proxy* proxy, const SkBitmap& src, SkIPoint* srcOffset, |
| 329 | SkIRect* bounds, SkBitmap* result) const; |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 330 | |
senorblanco@chromium.org | 1aa6872 | 2013-10-17 19:35:09 +0000 | [diff] [blame] | 331 | /** |
| 332 | * Returns true if the filter can be expressed a single-pass |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 333 | * GrProcessor, used to process this filter on the GPU, or false if |
senorblanco@chromium.org | 1aa6872 | 2013-10-17 19:35:09 +0000 | [diff] [blame] | 334 | * not. |
| 335 | * |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 336 | * If effect is non-NULL, a new GrProcessor instance is stored |
senorblanco@chromium.org | 1aa6872 | 2013-10-17 19:35:09 +0000 | [diff] [blame] | 337 | * in it. The caller assumes ownership of the stage, and it is up to the |
| 338 | * caller to unref it. |
| 339 | * |
| 340 | * The effect can assume its vertexCoords space maps 1-to-1 with texels |
| 341 | * in the texture. "matrix" is a transformation to apply to filter |
| 342 | * parameters before they are used in the effect. Note that this function |
| 343 | * will be called with (NULL, NULL, SkMatrix::I()) to query for support, |
| 344 | * so returning "true" indicates support for all possible matrices. |
| 345 | */ |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 346 | virtual bool asFragmentProcessor(GrFragmentProcessor**, GrTexture*, const SkMatrix&, |
| 347 | const SkIRect& bounds) const; |
senorblanco@chromium.org | 1aa6872 | 2013-10-17 19:35:09 +0000 | [diff] [blame] | 348 | |
reed@google.com | 894aa9a | 2011-09-23 14:49:49 +0000 | [diff] [blame] | 349 | private: |
reed | 67ca2a9 | 2015-05-20 13:22:58 -0700 | [diff] [blame] | 350 | friend class SkGraphics; |
| 351 | static void PurgeCache(); |
| 352 | |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 353 | bool usesSrcInput() const { return fUsesSrcInput; } |
| 354 | |
senorblanco@chromium.org | 54e01b2 | 2011-11-16 18:20:47 +0000 | [diff] [blame] | 355 | typedef SkFlattenable INHERITED; |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 356 | int fInputCount; |
senorblanco@chromium.org | 9f25de7 | 2012-10-10 20:36:13 +0000 | [diff] [blame] | 357 | SkImageFilter** fInputs; |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 358 | bool fUsesSrcInput; |
senorblanco@chromium.org | b295fb6 | 2013-10-10 13:51:19 +0000 | [diff] [blame] | 359 | CropRect fCropRect; |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 360 | uint32_t fUniqueID; // Globally unique |
reed@google.com | 894aa9a | 2011-09-23 14:49:49 +0000 | [diff] [blame] | 361 | }; |
| 362 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 363 | /** |
| 364 | * Helper to unflatten the common data, and return NULL if we fail. |
| 365 | */ |
| 366 | #define SK_IMAGEFILTER_UNFLATTEN_COMMON(localVar, expectedCount) \ |
| 367 | Common localVar; \ |
| 368 | do { \ |
| 369 | if (!localVar.unflatten(buffer, expectedCount)) { \ |
| 370 | return NULL; \ |
| 371 | } \ |
| 372 | } while (0) |
| 373 | |
reed@google.com | 894aa9a | 2011-09-23 14:49:49 +0000 | [diff] [blame] | 374 | #endif |