blob: a3e5a62ee505d062a0c4504cdebe317c45bf251c [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
11#include "SkFlattenable.h"
senorblanco@chromium.org194d7752013-07-24 22:19:24 +000012#include "SkRect.h"
reed@google.com894aa9a2011-09-23 14:49:49 +000013
reed@google.com15356a62011-11-03 19:29:08 +000014class SkBitmap;
senorblanco@chromium.org8d21f6c2012-10-12 19:14:06 +000015class SkColorFilter;
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000016class SkBaseDevice;
reed@google.com15356a62011-11-03 19:29:08 +000017class SkMatrix;
djsollen@google.comc73dd5c2012-08-07 15:54:32 +000018struct SkIPoint;
sugoi@google.coma1c511b2013-02-21 15:02:28 +000019class SkShader;
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000020class GrEffectRef;
tomhudson@google.comd0c1a062012-07-12 17:23:52 +000021class GrTexture;
reed@google.com15356a62011-11-03 19:29:08 +000022
23/**
reed@google.com15356a62011-11-03 19:29:08 +000024 * Base class for image filters. If one is installed in the paint, then
25 * all drawing occurs as usual, but it is as if the drawing happened into an
26 * offscreen (before the xfermode is applied). This offscreen bitmap will
27 * then be handed to the imagefilter, who in turn creates a new bitmap which
28 * is what will finally be drawn to the device (using the original xfermode).
reed@google.com15356a62011-11-03 19:29:08 +000029 */
senorblanco@chromium.org54e01b22011-11-16 18:20:47 +000030class SK_API SkImageFilter : public SkFlattenable {
reed@google.com894aa9a2011-09-23 14:49:49 +000031public:
robertphillips@google.com0456e0b2012-06-27 14:03:26 +000032 SK_DECLARE_INST_COUNT(SkImageFilter)
33
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
reed@google.com76dd2772012-01-05 21:15:07 +000052 class Proxy {
53 public:
reed@google.com8926b162012-03-23 15:36:36 +000054 virtual ~Proxy() {};
55
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000056 virtual SkBaseDevice* createDevice(int width, int height) = 0;
reed@google.com8926b162012-03-23 15:36:36 +000057 // returns true if the proxy can handle this filter natively
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +000058 virtual bool canHandleImageFilter(const SkImageFilter*) = 0;
reed@google.com76dd2772012-01-05 21:15:07 +000059 // returns true if the proxy handled the filter itself. if this returns
60 // false then the filter's code will be called.
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +000061 virtual bool filterImage(const SkImageFilter*, const SkBitmap& src,
reed@google.com76dd2772012-01-05 21:15:07 +000062 const SkMatrix& ctm,
63 SkBitmap* result, SkIPoint* offset) = 0;
64 };
reed@google.com894aa9a2011-09-23 14:49:49 +000065
66 /**
67 * Request a new (result) image to be created from the src image.
68 * If the src has no pixels (isNull()) then the request just wants to
69 * receive the config and width/height of the result.
70 *
71 * The matrix is the current matrix on the canvas.
72 *
73 * Offset is the amount to translate the resulting image relative to the
senorblanco@chromium.org6776b822014-01-03 21:48:22 +000074 * src when it is drawn. This is an out-param.
reed@google.com894aa9a2011-09-23 14:49:49 +000075 *
76 * If the result image cannot be created, return false, in which case both
77 * the result and offset parameters will be ignored by the caller.
78 */
reed@google.com76dd2772012-01-05 21:15:07 +000079 bool filterImage(Proxy*, const SkBitmap& src, const SkMatrix& ctm,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +000080 SkBitmap* result, SkIPoint* offset) const;
reed@google.com15356a62011-11-03 19:29:08 +000081
82 /**
reed@google.com32d25b62011-12-20 16:19:00 +000083 * Given the src bounds of an image, this returns the bounds of the result
84 * image after the filter has been applied.
85 */
senorblanco@chromium.orgc4b12f12014-02-05 17:51:22 +000086 bool filterBounds(const SkIRect& src, const SkMatrix& ctm, SkIRect* dst) const;
reed@google.com32d25b62011-12-20 16:19:00 +000087
88 /**
senorblanco@chromium.org302cffb2012-08-01 20:16:34 +000089 * Returns true if the filter can be processed on the GPU. This is most
90 * often used for multi-pass effects, where intermediate results must be
bsalomon@google.com8ea78d82012-10-24 20:11:30 +000091 * rendered to textures. For single-pass effects, use asNewEffect().
senorblanco@chromium.org7938bae2013-10-18 20:08:14 +000092 * The default implementation returns asNewEffect(NULL, NULL, SkMatrix::I(),
93 * SkIRect()).
reed@google.com15356a62011-11-03 19:29:08 +000094 */
senorblanco@chromium.org302cffb2012-08-01 20:16:34 +000095 virtual bool canFilterImageGPU() const;
reed@google.com894aa9a2011-09-23 14:49:49 +000096
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000097 /**
skia.committer@gmail.com32840172013-04-09 07:01:27 +000098 * Process this image filter on the GPU. This is most often used for
senorblanco@chromium.orgd043cce2013-04-08 19:43:22 +000099 * multi-pass effects, where intermediate results must be rendered to
100 * textures. For single-pass effects, use asNewEffect(). src is the
101 * source image for processing, as a texture-backed bitmap. result is
102 * the destination bitmap, which should contain a texture-backed pixelref
skia.committer@gmail.comde2e4e82013-07-11 07:01:01 +0000103 * on success. offset is the amount to translate the resulting image
commit-bot@chromium.org7b320702013-07-10 21:22:18 +0000104 * relative to the src when it is drawn. The default implementation does
105 * single-pass processing using asNewEffect().
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000106 */
commit-bot@chromium.org1aa54bf2013-08-05 16:53:50 +0000107 virtual bool filterImageGPU(Proxy*, const SkBitmap& src, const SkMatrix& ctm,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +0000108 SkBitmap* result, SkIPoint* offset) const;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000109
senorblanco@chromium.org8d21f6c2012-10-12 19:14:06 +0000110 /**
sugoi@google.coma1c511b2013-02-21 15:02:28 +0000111 * Returns whether this image filter is a color filter and puts the color filter into the
sugoi@google.com4b6d4322013-02-21 20:26:50 +0000112 * "filterPtr" parameter if it can. Does nothing otherwise.
113 * If this returns false, then the filterPtr is unchanged.
114 * If this returns true, then if filterPtr is not null, it must be set to a ref'd colorfitler
115 * (i.e. it may not be set to NULL).
senorblanco@chromium.org8d21f6c2012-10-12 19:14:06 +0000116 */
sugoi@google.com4b6d4322013-02-21 20:26:50 +0000117 virtual bool asColorFilter(SkColorFilter** filterPtr) const;
senorblanco@chromium.org9f25de72012-10-10 20:36:13 +0000118
senorblanco@chromium.org8d21f6c2012-10-12 19:14:06 +0000119 /**
120 * Returns the number of inputs this filter will accept (some inputs can
121 * be NULL).
122 */
123 int countInputs() const { return fInputCount; }
124
125 /**
126 * Returns the input filter at a given index, or NULL if no input is
127 * connected. The indices used are filter-specific.
128 */
129 SkImageFilter* getInput(int i) const {
130 SkASSERT(i < fInputCount);
131 return fInputs[i];
132 }
133
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000134 /**
senorblanco@chromium.org3f1f2a32013-10-16 18:07:48 +0000135 * Returns whether any edges of the crop rect have been set. The crop
136 * rect is set at construction time, and determines which pixels from the
137 * input image will be processed. The size of the crop rect should be
138 * used as the size of the destination image. The origin of this rect
139 * should be used to offset access to the input images, and should also
140 * be added to the "offset" parameter in onFilterImage and
141 * filterImageGPU(). (The latter ensures that the resulting buffer is
142 * drawn in the correct location.)
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000143 */
senorblanco@chromium.org3f1f2a32013-10-16 18:07:48 +0000144 bool cropRectIsSet() const { return fCropRect.flags() != 0x0; }
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000145
senorblanco@chromium.org336d1d72014-01-27 21:03:17 +0000146 // Default impl returns union of all input bounds.
147 virtual void computeFastBounds(const SkRect&, SkRect*) const;
148
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +0000149 SK_DEFINE_FLATTENABLE_TYPE(SkImageFilter)
150
senorblanco@chromium.org8d21f6c2012-10-12 19:14:06 +0000151protected:
senorblanco@chromium.orgb295fb62013-10-10 13:51:19 +0000152 SkImageFilter(int inputCount, SkImageFilter** inputs, const CropRect* cropRect = NULL);
senorblanco@chromium.org8d21f6c2012-10-12 19:14:06 +0000153
senorblanco@chromium.orgc2e8cef2012-10-22 15:07:14 +0000154 // Convenience constructor for 1-input filters.
senorblanco@chromium.orgb295fb62013-10-10 13:51:19 +0000155 explicit SkImageFilter(SkImageFilter* input, const CropRect* cropRect = NULL);
senorblanco@chromium.orgc2e8cef2012-10-22 15:07:14 +0000156
157 // Convenience constructor for 2-input filters.
senorblanco@chromium.orgb295fb62013-10-10 13:51:19 +0000158 SkImageFilter(SkImageFilter* input1, SkImageFilter* input2, const CropRect* cropRect = NULL);
senorblanco@chromium.org9f25de72012-10-10 20:36:13 +0000159
160 virtual ~SkImageFilter();
161
commit-bot@chromium.orgc84728d2013-12-04 20:07:47 +0000162 /**
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000163 * Constructs a new SkImageFilter read from an SkReadBuffer object.
commit-bot@chromium.orgc84728d2013-12-04 20:07:47 +0000164 *
165 * @param inputCount The exact number of inputs expected for this SkImageFilter object.
166 * -1 can be used if the filter accepts any number of inputs.
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000167 * @param rb SkReadBuffer object from which the SkImageFilter is read.
commit-bot@chromium.orgc84728d2013-12-04 20:07:47 +0000168 */
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000169 explicit SkImageFilter(int inputCount, SkReadBuffer& rb);
senorblanco@chromium.org9f25de72012-10-10 20:36:13 +0000170
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000171 virtual void flatten(SkWriteBuffer& wb) const SK_OVERRIDE;
reed@google.com32d25b62011-12-20 16:19:00 +0000172
senorblanco@chromium.org6776b822014-01-03 21:48:22 +0000173 /**
174 * This is the virtual which should be overridden by the derived class
175 * to perform image filtering.
176 *
177 * src is the original primitive bitmap. If the filter has a connected
178 * input, it should recurse on that input and use that in place of src.
179 *
180 * The matrix is the current matrix on the canvas.
181 *
182 * Offset is the amount to translate the resulting image relative to the
183 * src when it is drawn. This is an out-param.
184 *
185 * If the result image cannot be created, this should false, in which
186 * case both the result and offset parameters will be ignored by the
187 * caller.
188 */
reed@google.com76dd2772012-01-05 21:15:07 +0000189 virtual bool onFilterImage(Proxy*, const SkBitmap& src, const SkMatrix&,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +0000190 SkBitmap* result, SkIPoint* offset) const;
senorblanco@chromium.orgc4b12f12014-02-05 17:51:22 +0000191 // Given the bounds of the destination rect to be filled in device
192 // coordinates (first parameter), and the CTM, compute (conservatively)
193 // which rect of the source image would be required (third parameter).
194 // Used for clipping and temp-buffer allocations, so the result need not
195 // be exact, but should never be smaller than the real answer. The default
196 // implementation recursively unions all input bounds, or returns false if
197 // no inputs.
198 virtual bool onFilterBounds(const SkIRect&, const SkMatrix&, SkIRect*) const;
reed@google.com894aa9a2011-09-23 14:49:49 +0000199
senorblanco@chromium.orgfbaea532013-08-27 21:37:01 +0000200 // Applies "matrix" to the crop rect, and sets "rect" to the intersection of
201 // "rect" and the transformed crop rect. If there is no overlap, returns
202 // false and leaves "rect" unchanged.
203 bool applyCropRect(SkIRect* rect, const SkMatrix& matrix) const;
senorblanco@chromium.org194d7752013-07-24 22:19:24 +0000204
senorblanco@chromium.org1aa68722013-10-17 19:35:09 +0000205 /**
206 * Returns true if the filter can be expressed a single-pass
207 * GrEffect, used to process this filter on the GPU, or false if
208 * not.
209 *
210 * If effect is non-NULL, a new GrEffect instance is stored
211 * in it. The caller assumes ownership of the stage, and it is up to the
212 * caller to unref it.
213 *
214 * The effect can assume its vertexCoords space maps 1-to-1 with texels
215 * in the texture. "matrix" is a transformation to apply to filter
216 * parameters before they are used in the effect. Note that this function
217 * will be called with (NULL, NULL, SkMatrix::I()) to query for support,
218 * so returning "true" indicates support for all possible matrices.
219 */
senorblanco@chromium.org7938bae2013-10-18 20:08:14 +0000220 virtual bool asNewEffect(GrEffectRef** effect,
221 GrTexture*,
222 const SkMatrix& matrix,
223 const SkIRect& bounds) const;
senorblanco@chromium.org1aa68722013-10-17 19:35:09 +0000224
reed@google.com894aa9a2011-09-23 14:49:49 +0000225private:
senorblanco@chromium.org54e01b22011-11-16 18:20:47 +0000226 typedef SkFlattenable INHERITED;
senorblanco@chromium.org8d21f6c2012-10-12 19:14:06 +0000227 int fInputCount;
senorblanco@chromium.org9f25de72012-10-10 20:36:13 +0000228 SkImageFilter** fInputs;
senorblanco@chromium.orgb295fb62013-10-10 13:51:19 +0000229 CropRect fCropRect;
reed@google.com894aa9a2011-09-23 14:49:49 +0000230};
231
232#endif