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 | |
| 11 | #include "SkFlattenable.h" |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 12 | #include "SkRect.h" |
reed@google.com | 894aa9a | 2011-09-23 14:49:49 +0000 | [diff] [blame] | 13 | |
reed@google.com | 15356a6 | 2011-11-03 19:29:08 +0000 | [diff] [blame] | 14 | class SkBitmap; |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 15 | class SkColorFilter; |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 16 | class SkBaseDevice; |
reed@google.com | 15356a6 | 2011-11-03 19:29:08 +0000 | [diff] [blame] | 17 | class SkMatrix; |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 18 | struct SkIPoint; |
sugoi@google.com | a1c511b | 2013-02-21 15:02:28 +0000 | [diff] [blame] | 19 | class SkShader; |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 20 | class GrEffectRef; |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 21 | class GrTexture; |
reed@google.com | 15356a6 | 2011-11-03 19:29:08 +0000 | [diff] [blame] | 22 | |
| 23 | /** |
reed@google.com | 15356a6 | 2011-11-03 19:29:08 +0000 | [diff] [blame] | 24 | * 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.com | 15356a6 | 2011-11-03 19:29:08 +0000 | [diff] [blame] | 29 | */ |
senorblanco@chromium.org | 54e01b2 | 2011-11-16 18:20:47 +0000 | [diff] [blame] | 30 | class SK_API SkImageFilter : public SkFlattenable { |
reed@google.com | 894aa9a | 2011-09-23 14:49:49 +0000 | [diff] [blame] | 31 | public: |
robertphillips@google.com | 0456e0b | 2012-06-27 14:03:26 +0000 | [diff] [blame] | 32 | SK_DECLARE_INST_COUNT(SkImageFilter) |
| 33 | |
senorblanco@chromium.org | b295fb6 | 2013-10-10 13:51:19 +0000 | [diff] [blame^] | 34 | #ifdef SK_CROP_RECT_IS_INT |
| 35 | typedef SkIRect CropRect; |
| 36 | #else |
| 37 | struct CropRect { |
| 38 | SkRect fRect; |
| 39 | uint32_t fFlags; |
| 40 | enum CropEdge { |
| 41 | kHasLeft_CropEdge = 0x01, |
| 42 | kHasTop_CropEdge = 0x02, |
| 43 | kHasRight_CropEdge = 0x04, |
| 44 | kHasBottom_CropEdge = 0x08, |
| 45 | kHasAll_CropEdge = 0x0F, |
| 46 | }; |
| 47 | CropRect() {} |
| 48 | explicit CropRect(const SkRect& rect, uint32_t flags = kHasAll_CropEdge) : fRect(rect), fFlags(flags) {} |
| 49 | bool isSet() const |
| 50 | { |
| 51 | return fFlags != 0x0; |
| 52 | } |
| 53 | }; |
| 54 | #endif |
| 55 | |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 56 | class Proxy { |
| 57 | public: |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 58 | virtual ~Proxy() {}; |
| 59 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 60 | virtual SkBaseDevice* createDevice(int width, int height) = 0; |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 61 | // returns true if the proxy can handle this filter natively |
| 62 | virtual bool canHandleImageFilter(SkImageFilter*) = 0; |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 63 | // returns true if the proxy handled the filter itself. if this returns |
| 64 | // false then the filter's code will be called. |
| 65 | virtual bool filterImage(SkImageFilter*, const SkBitmap& src, |
| 66 | const SkMatrix& ctm, |
| 67 | SkBitmap* result, SkIPoint* offset) = 0; |
| 68 | }; |
reed@google.com | 894aa9a | 2011-09-23 14:49:49 +0000 | [diff] [blame] | 69 | |
| 70 | /** |
| 71 | * Request a new (result) image to be created from the src image. |
| 72 | * If the src has no pixels (isNull()) then the request just wants to |
| 73 | * receive the config and width/height of the result. |
| 74 | * |
| 75 | * The matrix is the current matrix on the canvas. |
| 76 | * |
| 77 | * Offset is the amount to translate the resulting image relative to the |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 78 | * src when it is drawn. |
reed@google.com | 894aa9a | 2011-09-23 14:49:49 +0000 | [diff] [blame] | 79 | * |
| 80 | * If the result image cannot be created, return false, in which case both |
| 81 | * the result and offset parameters will be ignored by the caller. |
| 82 | */ |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 83 | bool filterImage(Proxy*, const SkBitmap& src, const SkMatrix& ctm, |
reed@google.com | 15356a6 | 2011-11-03 19:29:08 +0000 | [diff] [blame] | 84 | SkBitmap* result, SkIPoint* offset); |
| 85 | |
| 86 | /** |
reed@google.com | 32d25b6 | 2011-12-20 16:19:00 +0000 | [diff] [blame] | 87 | * Given the src bounds of an image, this returns the bounds of the result |
| 88 | * image after the filter has been applied. |
| 89 | */ |
| 90 | bool filterBounds(const SkIRect& src, const SkMatrix& ctm, SkIRect* dst); |
| 91 | |
| 92 | /** |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 93 | * Returns true if the filter can be expressed a single-pass |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 94 | * GrEffect, used to process this filter on the GPU, or false if |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 95 | * not. |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 96 | * |
bsalomon@google.com | 021fc73 | 2012-10-25 12:47:42 +0000 | [diff] [blame] | 97 | * If effect is non-NULL, a new GrEffect instance is stored |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 98 | * in it. The caller assumes ownership of the stage, and it is up to the |
| 99 | * caller to unref it. |
bsalomon@google.com | 17fc651 | 2012-11-02 21:45:01 +0000 | [diff] [blame] | 100 | * |
| 101 | * The effect can assume its vertexCoords space maps 1-to-1 with texels |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 102 | * in the texture. "matrix" is a transformation to apply to filter |
| 103 | * parameters before they are used in the effect. Note that this function |
| 104 | * will be called with (NULL, NULL, SkMatrix::I()) to query for support, |
| 105 | * so returning "true" indicates support for all possible matrices. |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 106 | */ |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 107 | virtual bool asNewEffect(GrEffectRef** effect, GrTexture*, const SkMatrix& matrix) const; |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 108 | |
| 109 | /** |
senorblanco@chromium.org | 302cffb | 2012-08-01 20:16:34 +0000 | [diff] [blame] | 110 | * Returns true if the filter can be processed on the GPU. This is most |
| 111 | * often used for multi-pass effects, where intermediate results must be |
bsalomon@google.com | 8ea78d8 | 2012-10-24 20:11:30 +0000 | [diff] [blame] | 112 | * rendered to textures. For single-pass effects, use asNewEffect(). |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 113 | * The default implementation returns asNewEffect(NULL, NULL, SkMatrix::I()). |
reed@google.com | 15356a6 | 2011-11-03 19:29:08 +0000 | [diff] [blame] | 114 | */ |
senorblanco@chromium.org | 302cffb | 2012-08-01 20:16:34 +0000 | [diff] [blame] | 115 | virtual bool canFilterImageGPU() const; |
reed@google.com | 894aa9a | 2011-09-23 14:49:49 +0000 | [diff] [blame] | 116 | |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 117 | /** |
skia.committer@gmail.com | 3284017 | 2013-04-09 07:01:27 +0000 | [diff] [blame] | 118 | * 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] | 119 | * multi-pass effects, where intermediate results must be rendered to |
| 120 | * textures. For single-pass effects, use asNewEffect(). src is the |
| 121 | * source image for processing, as a texture-backed bitmap. result is |
| 122 | * the destination bitmap, which should contain a texture-backed pixelref |
skia.committer@gmail.com | de2e4e8 | 2013-07-11 07:01:01 +0000 | [diff] [blame] | 123 | * 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] | 124 | * relative to the src when it is drawn. The default implementation does |
| 125 | * single-pass processing using asNewEffect(). |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 126 | */ |
commit-bot@chromium.org | 1aa54bf | 2013-08-05 16:53:50 +0000 | [diff] [blame] | 127 | virtual bool filterImageGPU(Proxy*, const SkBitmap& src, const SkMatrix& ctm, |
| 128 | SkBitmap* result, SkIPoint* offset); |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 129 | |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 130 | /** |
sugoi@google.com | a1c511b | 2013-02-21 15:02:28 +0000 | [diff] [blame] | 131 | * 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] | 132 | * "filterPtr" parameter if it can. Does nothing otherwise. |
| 133 | * If this returns false, then the filterPtr is unchanged. |
| 134 | * If this returns true, then if filterPtr is not null, it must be set to a ref'd colorfitler |
| 135 | * (i.e. it may not be set to NULL). |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 136 | */ |
sugoi@google.com | 4b6d432 | 2013-02-21 20:26:50 +0000 | [diff] [blame] | 137 | virtual bool asColorFilter(SkColorFilter** filterPtr) const; |
senorblanco@chromium.org | 9f25de7 | 2012-10-10 20:36:13 +0000 | [diff] [blame] | 138 | |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 139 | /** |
| 140 | * Returns the number of inputs this filter will accept (some inputs can |
| 141 | * be NULL). |
| 142 | */ |
| 143 | int countInputs() const { return fInputCount; } |
| 144 | |
| 145 | /** |
| 146 | * Returns the input filter at a given index, or NULL if no input is |
| 147 | * connected. The indices used are filter-specific. |
| 148 | */ |
| 149 | SkImageFilter* getInput(int i) const { |
| 150 | SkASSERT(i < fInputCount); |
| 151 | return fInputs[i]; |
| 152 | } |
| 153 | |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 154 | /** |
| 155 | * Returns the crop rectangle of this filter. This is set at construction |
| 156 | * time, and determines which pixels from the input image will |
| 157 | * be processed. The size of this rectangle should be used as the size |
| 158 | * of the destination image. The origin of this rect should be used to |
| 159 | * offset access to the input images, and should also be added to the |
| 160 | * "offset" parameter in onFilterImage and filterImageGPU(). (The latter |
| 161 | * ensures that the resulting buffer is drawn in the correct location.) |
| 162 | */ |
senorblanco@chromium.org | b295fb6 | 2013-10-10 13:51:19 +0000 | [diff] [blame^] | 163 | #ifdef SK_CROP_RECT_IS_INT |
| 164 | bool cropRectIsSet() const { return !fCropRect.isLargest(); } |
| 165 | #else |
| 166 | bool cropRectIsSet() const { return fCropRect.isSet(); } |
| 167 | #endif |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 168 | |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 169 | protected: |
senorblanco@chromium.org | b295fb6 | 2013-10-10 13:51:19 +0000 | [diff] [blame^] | 170 | SkImageFilter(int inputCount, SkImageFilter** inputs, const CropRect* cropRect = NULL); |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 171 | |
senorblanco@chromium.org | c2e8cef | 2012-10-22 15:07:14 +0000 | [diff] [blame] | 172 | // Convenience constructor for 1-input filters. |
senorblanco@chromium.org | b295fb6 | 2013-10-10 13:51:19 +0000 | [diff] [blame^] | 173 | explicit SkImageFilter(SkImageFilter* input, const CropRect* cropRect = NULL); |
senorblanco@chromium.org | c2e8cef | 2012-10-22 15:07:14 +0000 | [diff] [blame] | 174 | |
| 175 | // Convenience constructor for 2-input filters. |
senorblanco@chromium.org | b295fb6 | 2013-10-10 13:51:19 +0000 | [diff] [blame^] | 176 | SkImageFilter(SkImageFilter* input1, SkImageFilter* input2, const CropRect* cropRect = NULL); |
senorblanco@chromium.org | 9f25de7 | 2012-10-10 20:36:13 +0000 | [diff] [blame] | 177 | |
| 178 | virtual ~SkImageFilter(); |
| 179 | |
| 180 | explicit SkImageFilter(SkFlattenableReadBuffer& rb); |
| 181 | |
| 182 | virtual void flatten(SkFlattenableWriteBuffer& wb) const SK_OVERRIDE; |
reed@google.com | 32d25b6 | 2011-12-20 16:19:00 +0000 | [diff] [blame] | 183 | |
| 184 | // Default impl returns false |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 185 | virtual bool onFilterImage(Proxy*, const SkBitmap& src, const SkMatrix&, |
reed@google.com | 15356a6 | 2011-11-03 19:29:08 +0000 | [diff] [blame] | 186 | SkBitmap* result, SkIPoint* offset); |
reed@google.com | 32d25b6 | 2011-12-20 16:19:00 +0000 | [diff] [blame] | 187 | // Default impl copies src into dst and returns true |
| 188 | virtual bool onFilterBounds(const SkIRect&, const SkMatrix&, SkIRect*); |
reed@google.com | 894aa9a | 2011-09-23 14:49:49 +0000 | [diff] [blame] | 189 | |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 190 | // Applies "matrix" to the crop rect, and sets "rect" to the intersection of |
| 191 | // "rect" and the transformed crop rect. If there is no overlap, returns |
| 192 | // false and leaves "rect" unchanged. |
| 193 | bool applyCropRect(SkIRect* rect, const SkMatrix& matrix) const; |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 194 | |
reed@google.com | 894aa9a | 2011-09-23 14:49:49 +0000 | [diff] [blame] | 195 | private: |
senorblanco@chromium.org | 54e01b2 | 2011-11-16 18:20:47 +0000 | [diff] [blame] | 196 | typedef SkFlattenable INHERITED; |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 197 | int fInputCount; |
senorblanco@chromium.org | 9f25de7 | 2012-10-10 20:36:13 +0000 | [diff] [blame] | 198 | SkImageFilter** fInputs; |
senorblanco@chromium.org | b295fb6 | 2013-10-10 13:51:19 +0000 | [diff] [blame^] | 199 | CropRect fCropRect; |
reed@google.com | 894aa9a | 2011-09-23 14:49:49 +0000 | [diff] [blame] | 200 | }; |
| 201 | |
| 202 | #endif |