blob: 4125db37349e20c9f25e3b5b7859d199720e8dbe [file] [log] [blame]
reed@google.com894aa9a2011-09-23 14:49:49 +00001/*
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
senorblanco8c874ee2015-03-20 06:38:17 -070011#include "SkFilterQuality.h"
reed@google.com894aa9a2011-09-23 14:49:49 +000012#include "SkFlattenable.h"
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +000013#include "SkMatrix.h"
senorblanco@chromium.org194d7752013-07-24 22:19:24 +000014#include "SkRect.h"
reed2c55d7b2015-06-09 08:18:39 -070015#include "SkSurfaceProps.h"
reedb959ec72014-07-17 07:03:09 -070016#include "SkTemplates.h"
reed@google.com894aa9a2011-09-23 14:49:49 +000017
joshualittb0a8a372014-09-23 09:50:21 -070018class GrFragmentProcessor;
tomhudson@google.comd0c1a062012-07-12 17:23:52 +000019class GrTexture;
reed2c55d7b2015-06-09 08:18:39 -070020class SkBaseDevice;
21class SkBitmap;
22class SkColorFilter;
23struct SkIPoint;
reed@google.com15356a62011-11-03 19:29:08 +000024
25/**
reed@google.com15356a62011-11-03 19:29:08 +000026 * 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.com15356a62011-11-03 19:29:08 +000031 */
senorblanco@chromium.org54e01b22011-11-16 18:20:47 +000032class SK_API SkImageFilter : public SkFlattenable {
reed@google.com894aa9a2011-09-23 14:49:49 +000033public:
senorblanco@chromium.org3f1f2a32013-10-16 18:07:48 +000034 class CropRect {
35 public:
senorblanco@chromium.orgb295fb62013-10-10 13:51:19 +000036 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.org3f1f2a32013-10-16 18:07:48 +000045 uint32_t flags() const { return fFlags; }
46 const SkRect& rect() const { return fRect; }
47 private:
48 SkRect fRect;
49 uint32_t fFlags;
senorblanco@chromium.orgb295fb62013-10-10 13:51:19 +000050 };
senorblanco@chromium.orgb295fb62013-10-10 13:51:19 +000051
senorblanco55b6d8b2014-07-30 11:26:46 -070052 // This cache maps from (filter's unique ID + CTM + clipBounds + src bitmap generation ID) to
53 // (result, offset).
senorblancobe129b22014-08-08 07:14:35 -070054 class Cache : public SkRefCnt {
senorblanco55b6d8b2014-07-30 11:26:46 -070055 public:
56 struct Key;
senorblancobe129b22014-08-08 07:14:35 -070057 virtual ~Cache() {}
58 static Cache* Create(size_t maxBytes);
59 static Cache* Get();
senorblanco55b6d8b2014-07-30 11:26:46 -070060 virtual bool get(const Key& key, SkBitmap* result, SkIPoint* offset) const = 0;
61 virtual void set(const Key& key, const SkBitmap& result, const SkIPoint& offset) = 0;
reed67ca2a92015-05-20 13:22:58 -070062 virtual void purge() {}
senorblanco55b6d8b2014-07-30 11:26:46 -070063 };
64
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +000065 class Context {
66 public:
senorblancobe129b22014-08-08 07:14:35 -070067 Context(const SkMatrix& ctm, const SkIRect& clipBounds, Cache* cache) :
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +000068 fCTM(ctm), fClipBounds(clipBounds), fCache(cache) {
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +000069 }
70 const SkMatrix& ctm() const { return fCTM; }
71 const SkIRect& clipBounds() const { return fClipBounds; }
senorblancobe129b22014-08-08 07:14:35 -070072 Cache* cache() const { return fCache; }
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +000073 private:
74 SkMatrix fCTM;
75 SkIRect fClipBounds;
reed2c55d7b2015-06-09 08:18:39 -070076 Cache* fCache;
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +000077 };
78
reed@google.com76dd2772012-01-05 21:15:07 +000079 class Proxy {
80 public:
robertphillipsefbffed2015-06-22 12:06:08 -070081 Proxy(SkBaseDevice* device) : fDevice(device) { }
mtklein2766c002015-06-26 11:45:03 -070082
reed2c55d7b2015-06-09 08:18:39 -070083 SkBaseDevice* createDevice(int width, int height);
robertphillipsefbffed2015-06-22 12:06:08 -070084
85 // Returns true if the proxy handled the filter itself. If this returns
reed@google.com76dd2772012-01-05 21:15:07 +000086 // false then the filter's code will be called.
reed2c55d7b2015-06-09 08:18:39 -070087 bool filterImage(const SkImageFilter*, const SkBitmap& src, const SkImageFilter::Context&,
88 SkBitmap* result, SkIPoint* offset);
robertphillipsefbffed2015-06-22 12:06:08 -070089
reed2c55d7b2015-06-09 08:18:39 -070090 private:
robertphillipsefbffed2015-06-22 12:06:08 -070091 SkBaseDevice* fDevice;
reed@google.com76dd2772012-01-05 21:15:07 +000092 };
mtklein2766c002015-06-26 11:45:03 -070093
reed@google.com894aa9a2011-09-23 14:49:49 +000094
95 /**
96 * Request a new (result) image to be created from the src image.
97 * If the src has no pixels (isNull()) then the request just wants to
98 * receive the config and width/height of the result.
99 *
100 * The matrix is the current matrix on the canvas.
101 *
102 * Offset is the amount to translate the resulting image relative to the
senorblanco@chromium.org6776b822014-01-03 21:48:22 +0000103 * src when it is drawn. This is an out-param.
reed@google.com894aa9a2011-09-23 14:49:49 +0000104 *
105 * If the result image cannot be created, return false, in which case both
106 * the result and offset parameters will be ignored by the caller.
107 */
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +0000108 bool filterImage(Proxy*, const SkBitmap& src, const Context&,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +0000109 SkBitmap* result, SkIPoint* offset) const;
reed@google.com15356a62011-11-03 19:29:08 +0000110
111 /**
reed@google.com32d25b62011-12-20 16:19:00 +0000112 * Given the src bounds of an image, this returns the bounds of the result
113 * image after the filter has been applied.
114 */
senorblanco@chromium.orgc4b12f12014-02-05 17:51:22 +0000115 bool filterBounds(const SkIRect& src, const SkMatrix& ctm, SkIRect* dst) const;
reed@google.com32d25b62011-12-20 16:19:00 +0000116
117 /**
senorblanco@chromium.org302cffb2012-08-01 20:16:34 +0000118 * Returns true if the filter can be processed on the GPU. This is most
119 * often used for multi-pass effects, where intermediate results must be
joshualittb0a8a372014-09-23 09:50:21 -0700120 * rendered to textures. For single-pass effects, use asFragmentProcessor().
121 * The default implementation returns asFragmentProcessor(NULL, NULL, SkMatrix::I(),
senorblanco@chromium.org7938bae2013-10-18 20:08:14 +0000122 * SkIRect()).
reed@google.com15356a62011-11-03 19:29:08 +0000123 */
senorblanco@chromium.org302cffb2012-08-01 20:16:34 +0000124 virtual bool canFilterImageGPU() const;
reed@google.com894aa9a2011-09-23 14:49:49 +0000125
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000126 /**
skia.committer@gmail.com32840172013-04-09 07:01:27 +0000127 * Process this image filter on the GPU. This is most often used for
senorblanco@chromium.orgd043cce2013-04-08 19:43:22 +0000128 * multi-pass effects, where intermediate results must be rendered to
joshualittb0a8a372014-09-23 09:50:21 -0700129 * textures. For single-pass effects, use asFragmentProcessor(). src is the
senorblanco@chromium.orgd043cce2013-04-08 19:43:22 +0000130 * source image for processing, as a texture-backed bitmap. result is
131 * the destination bitmap, which should contain a texture-backed pixelref
skia.committer@gmail.comde2e4e82013-07-11 07:01:01 +0000132 * on success. offset is the amount to translate the resulting image
commit-bot@chromium.org7b320702013-07-10 21:22:18 +0000133 * relative to the src when it is drawn. The default implementation does
joshualittb0a8a372014-09-23 09:50:21 -0700134 * single-pass processing using asFragmentProcessor().
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000135 */
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +0000136 virtual bool filterImageGPU(Proxy*, const SkBitmap& src, const Context&,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +0000137 SkBitmap* result, SkIPoint* offset) const;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000138
senorblanco@chromium.org8d21f6c2012-10-12 19:14:06 +0000139 /**
sugoi@google.coma1c511b2013-02-21 15:02:28 +0000140 * Returns whether this image filter is a color filter and puts the color filter into the
sugoi@google.com4b6d4322013-02-21 20:26:50 +0000141 * "filterPtr" parameter if it can. Does nothing otherwise.
142 * If this returns false, then the filterPtr is unchanged.
143 * If this returns true, then if filterPtr is not null, it must be set to a ref'd colorfitler
144 * (i.e. it may not be set to NULL).
senorblanco@chromium.org8d21f6c2012-10-12 19:14:06 +0000145 */
reedcedc36f2015-03-08 04:42:52 -0700146 bool isColorFilterNode(SkColorFilter** filterPtr) const {
147 return this->onIsColorFilterNode(filterPtr);
148 }
149
150 // DEPRECATED : use isColorFilterNode() instead
151 bool asColorFilter(SkColorFilter** filterPtr) const {
152 return this->isColorFilterNode(filterPtr);
153 }
154
155 /**
156 * Returns true (and optionally returns a ref'd filter) if this imagefilter can be completely
157 * replaced by the returned colorfilter. i.e. the two effects will affect drawing in the
158 * same way.
159 */
160 bool asAColorFilter(SkColorFilter** filterPtr) const {
161 return this->countInputs() > 0 &&
162 NULL == this->getInput(0) &&
163 this->isColorFilterNode(filterPtr);
164 }
senorblanco@chromium.org9f25de72012-10-10 20:36:13 +0000165
senorblanco@chromium.org8d21f6c2012-10-12 19:14:06 +0000166 /**
167 * Returns the number of inputs this filter will accept (some inputs can
168 * be NULL).
169 */
170 int countInputs() const { return fInputCount; }
171
172 /**
173 * Returns the input filter at a given index, or NULL if no input is
174 * connected. The indices used are filter-specific.
175 */
176 SkImageFilter* getInput(int i) const {
177 SkASSERT(i < fInputCount);
178 return fInputs[i];
179 }
180
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000181 /**
senorblanco@chromium.org3f1f2a32013-10-16 18:07:48 +0000182 * Returns whether any edges of the crop rect have been set. The crop
183 * rect is set at construction time, and determines which pixels from the
184 * input image will be processed. The size of the crop rect should be
185 * used as the size of the destination image. The origin of this rect
186 * should be used to offset access to the input images, and should also
187 * be added to the "offset" parameter in onFilterImage and
188 * filterImageGPU(). (The latter ensures that the resulting buffer is
189 * drawn in the correct location.)
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000190 */
senorblanco@chromium.org3f1f2a32013-10-16 18:07:48 +0000191 bool cropRectIsSet() const { return fCropRect.flags() != 0x0; }
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000192
reedb3fe1b82015-06-23 08:29:20 -0700193 CropRect getCropRect() const { return fCropRect; }
194
senorblanco@chromium.org336d1d72014-01-27 21:03:17 +0000195 // Default impl returns union of all input bounds.
196 virtual void computeFastBounds(const SkRect&, SkRect*) const;
197
senorblanco8c874ee2015-03-20 06:38:17 -0700198 /**
199 * Create an SkMatrixImageFilter, which transforms its input by the given matrix.
200 */
201 static SkImageFilter* CreateMatrixFilter(const SkMatrix& matrix,
202 SkFilterQuality,
203 SkImageFilter* input = NULL);
204
kkinnunen45095172014-07-29 06:12:49 -0700205#if SK_SUPPORT_GPU
senorblanco@chromium.org6aa6fec2014-03-03 22:13:56 +0000206 /**
207 * Wrap the given texture in a texture-backed SkBitmap.
208 */
209 static void WrapTexture(GrTexture* texture, int width, int height, SkBitmap* result);
210
211 /**
212 * Recursively evaluate this filter on the GPU. If the filter has no GPU
213 * implementation, it will be processed in software and uploaded to the GPU.
214 */
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +0000215 bool getInputResultGPU(SkImageFilter::Proxy* proxy, const SkBitmap& src, const Context&,
senorblanco@chromium.org6aa6fec2014-03-03 22:13:56 +0000216 SkBitmap* result, SkIPoint* offset) const;
217#endif
218
robertphillipsf3f5bad2014-12-19 13:49:15 -0800219 SK_TO_STRING_PUREVIRT()
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +0000220 SK_DEFINE_FLATTENABLE_TYPE(SkImageFilter)
221
senorblanco@chromium.org8d21f6c2012-10-12 19:14:06 +0000222protected:
reedb959ec72014-07-17 07:03:09 -0700223 class Common {
224 public:
225 Common() {}
226 ~Common();
227
reed9fa60da2014-08-21 07:59:51 -0700228 /**
229 * Attempt to unflatten the cropRect and the expected number of input filters.
230 * If any number of input filters is valid, pass -1.
231 * If this fails (i.e. corrupt buffer or contents) then return false and common will
232 * be left uninitialized.
233 * If this returns true, then inputCount() is the number of found input filters, each
234 * of which may be NULL or a valid imagefilter.
235 */
236 bool unflatten(SkReadBuffer&, int expectedInputs);
reedb959ec72014-07-17 07:03:09 -0700237
reed9fa60da2014-08-21 07:59:51 -0700238 const CropRect& cropRect() const { return fCropRect; }
reedb959ec72014-07-17 07:03:09 -0700239 int inputCount() const { return fInputs.count(); }
240 SkImageFilter** inputs() const { return fInputs.get(); }
241
reed9fa60da2014-08-21 07:59:51 -0700242 SkImageFilter* getInput(int index) const { return fInputs[index]; }
243
reedb959ec72014-07-17 07:03:09 -0700244 // If the caller wants a copy of the inputs, call this and it will transfer ownership
245 // of the unflattened input filters to the caller. This is just a short-cut for copying
246 // the inputs, calling ref() on each, and then waiting for Common's destructor to call
247 // unref() on each.
248 void detachInputs(SkImageFilter** inputs);
249
250 private:
251 CropRect fCropRect;
252 // most filters accept at most 2 input-filters
253 SkAutoSTArray<2, SkImageFilter*> fInputs;
254
255 void allocInputs(int count);
256 };
257
senorblanco24e06d52015-03-18 12:11:33 -0700258 SkImageFilter(int inputCount, SkImageFilter** inputs, const CropRect* cropRect = NULL);
senorblanco@chromium.org8d21f6c2012-10-12 19:14:06 +0000259
senorblanco@chromium.org9f25de72012-10-10 20:36:13 +0000260 virtual ~SkImageFilter();
261
commit-bot@chromium.orgc84728d2013-12-04 20:07:47 +0000262 /**
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000263 * Constructs a new SkImageFilter read from an SkReadBuffer object.
commit-bot@chromium.orgc84728d2013-12-04 20:07:47 +0000264 *
265 * @param inputCount The exact number of inputs expected for this SkImageFilter object.
266 * -1 can be used if the filter accepts any number of inputs.
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000267 * @param rb SkReadBuffer object from which the SkImageFilter is read.
commit-bot@chromium.orgc84728d2013-12-04 20:07:47 +0000268 */
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000269 explicit SkImageFilter(int inputCount, SkReadBuffer& rb);
senorblanco@chromium.org9f25de72012-10-10 20:36:13 +0000270
mtklein36352bf2015-03-25 18:17:31 -0700271 void flatten(SkWriteBuffer&) const override;
reed@google.com32d25b62011-12-20 16:19:00 +0000272
senorblanco@chromium.org6776b822014-01-03 21:48:22 +0000273 /**
274 * This is the virtual which should be overridden by the derived class
275 * to perform image filtering.
276 *
277 * src is the original primitive bitmap. If the filter has a connected
278 * input, it should recurse on that input and use that in place of src.
279 *
280 * The matrix is the current matrix on the canvas.
281 *
282 * Offset is the amount to translate the resulting image relative to the
283 * src when it is drawn. This is an out-param.
284 *
285 * If the result image cannot be created, this should false, in which
286 * case both the result and offset parameters will be ignored by the
287 * caller.
288 */
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +0000289 virtual bool onFilterImage(Proxy*, const SkBitmap& src, const Context&,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +0000290 SkBitmap* result, SkIPoint* offset) const;
senorblanco@chromium.orgc4b12f12014-02-05 17:51:22 +0000291 // Given the bounds of the destination rect to be filled in device
292 // coordinates (first parameter), and the CTM, compute (conservatively)
293 // which rect of the source image would be required (third parameter).
294 // Used for clipping and temp-buffer allocations, so the result need not
295 // be exact, but should never be smaller than the real answer. The default
296 // implementation recursively unions all input bounds, or returns false if
297 // no inputs.
298 virtual bool onFilterBounds(const SkIRect&, const SkMatrix&, SkIRect*) const;
reed@google.com894aa9a2011-09-23 14:49:49 +0000299
reedcedc36f2015-03-08 04:42:52 -0700300 /**
301 * Return true (and return a ref'd colorfilter) if this node in the DAG is just a
302 * colorfilter w/o CropRect constraints.
303 */
304 virtual bool onIsColorFilterNode(SkColorFilter** /*filterPtr*/) const {
305 return false;
306 }
307
senorblanco@chromium.org11825292014-03-14 17:44:41 +0000308 /** Computes source bounds as the src bitmap bounds offset by srcOffset.
309 * Apply the transformed crop rect to the bounds if any of the
310 * corresponding edge flags are set. Intersects the result against the
311 * context's clipBounds, and returns the result in "bounds". If there is
312 * no intersection, returns false and leaves "bounds" unchanged.
313 */
314 bool applyCropRect(const Context&, const SkBitmap& src, const SkIPoint& srcOffset,
315 SkIRect* bounds) const;
316
317 /** Same as the above call, except that if the resulting crop rect is not
318 * entirely contained by the source bitmap's bounds, it creates a new
319 * bitmap in "result" and pads the edges with transparent black. In that
320 * case, the srcOffset is modified to be the same as the bounds, since no
321 * further adjustment is needed by the caller. This version should only
322 * be used by filters which are not capable of processing a smaller
323 * source bitmap into a larger destination.
324 */
325 bool applyCropRect(const Context&, Proxy* proxy, const SkBitmap& src, SkIPoint* srcOffset,
326 SkIRect* bounds, SkBitmap* result) const;
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000327
senorblanco@chromium.org1aa68722013-10-17 19:35:09 +0000328 /**
329 * Returns true if the filter can be expressed a single-pass
joshualittb0a8a372014-09-23 09:50:21 -0700330 * GrProcessor, used to process this filter on the GPU, or false if
senorblanco@chromium.org1aa68722013-10-17 19:35:09 +0000331 * not.
332 *
joshualittb0a8a372014-09-23 09:50:21 -0700333 * If effect is non-NULL, a new GrProcessor instance is stored
senorblanco@chromium.org1aa68722013-10-17 19:35:09 +0000334 * in it. The caller assumes ownership of the stage, and it is up to the
335 * caller to unref it.
336 *
337 * The effect can assume its vertexCoords space maps 1-to-1 with texels
338 * in the texture. "matrix" is a transformation to apply to filter
339 * parameters before they are used in the effect. Note that this function
340 * will be called with (NULL, NULL, SkMatrix::I()) to query for support,
341 * so returning "true" indicates support for all possible matrices.
342 */
joshualittb0a8a372014-09-23 09:50:21 -0700343 virtual bool asFragmentProcessor(GrFragmentProcessor**, GrTexture*, const SkMatrix&,
344 const SkIRect& bounds) const;
senorblanco@chromium.org1aa68722013-10-17 19:35:09 +0000345
reed@google.com894aa9a2011-09-23 14:49:49 +0000346private:
reed67ca2a92015-05-20 13:22:58 -0700347 friend class SkGraphics;
348 static void PurgeCache();
349
senorblanco55b6d8b2014-07-30 11:26:46 -0700350 bool usesSrcInput() const { return fUsesSrcInput; }
351
senorblanco@chromium.org54e01b22011-11-16 18:20:47 +0000352 typedef SkFlattenable INHERITED;
senorblanco@chromium.org8d21f6c2012-10-12 19:14:06 +0000353 int fInputCount;
senorblanco@chromium.org9f25de72012-10-10 20:36:13 +0000354 SkImageFilter** fInputs;
senorblanco55b6d8b2014-07-30 11:26:46 -0700355 bool fUsesSrcInput;
senorblanco@chromium.orgb295fb62013-10-10 13:51:19 +0000356 CropRect fCropRect;
senorblanco55b6d8b2014-07-30 11:26:46 -0700357 uint32_t fUniqueID; // Globally unique
reed@google.com894aa9a2011-09-23 14:49:49 +0000358};
359
reed9fa60da2014-08-21 07:59:51 -0700360/**
361 * Helper to unflatten the common data, and return NULL if we fail.
362 */
363#define SK_IMAGEFILTER_UNFLATTEN_COMMON(localVar, expectedCount) \
364 Common localVar; \
365 do { \
366 if (!localVar.unflatten(buffer, expectedCount)) { \
367 return NULL; \
368 } \
369 } while (0)
370
reed@google.com894aa9a2011-09-23 14:49:49 +0000371#endif