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" |
| 12 | |
reed@google.com | 15356a6 | 2011-11-03 19:29:08 +0000 | [diff] [blame] | 13 | class SkBitmap; |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 14 | class SkDevice; |
reed@google.com | 15356a6 | 2011-11-03 19:29:08 +0000 | [diff] [blame] | 15 | class SkMatrix; |
| 16 | struct SkPoint; |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 17 | class GrCustomStage; |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame^] | 18 | class GrTexture; |
reed@google.com | 15356a6 | 2011-11-03 19:29:08 +0000 | [diff] [blame] | 19 | |
| 20 | /** |
| 21 | * Experimental. |
| 22 | * |
| 23 | * Base class for image filters. If one is installed in the paint, then |
| 24 | * all drawing occurs as usual, but it is as if the drawing happened into an |
| 25 | * offscreen (before the xfermode is applied). This offscreen bitmap will |
| 26 | * then be handed to the imagefilter, who in turn creates a new bitmap which |
| 27 | * is what will finally be drawn to the device (using the original xfermode). |
| 28 | * |
reed@google.com | 32d25b6 | 2011-12-20 16:19:00 +0000 | [diff] [blame] | 29 | * THIS SIGNATURE IS TEMPORARY |
| 30 | * |
| 31 | * There are several weaknesses in this function signature: |
| 32 | * 1. Does not expose the destination/target device, so filters that can draw |
| 33 | * directly to it are unable to take advantage of that optimization. |
| 34 | * 2. Does not expose a way to create a "compabitible" image (i.e. gpu -> gpu) |
| 35 | * 3. As with #1, the filter is unable to "read" the dest (which would be slow) |
| 36 | * |
| 37 | * Therefore, we should not create any real dependencies on this API yet -- it |
| 38 | * is being checked in as a check-point so we can explore these and other |
| 39 | * considerations. |
reed@google.com | 15356a6 | 2011-11-03 19:29:08 +0000 | [diff] [blame] | 40 | */ |
senorblanco@chromium.org | 54e01b2 | 2011-11-16 18:20:47 +0000 | [diff] [blame] | 41 | class SK_API SkImageFilter : public SkFlattenable { |
reed@google.com | 894aa9a | 2011-09-23 14:49:49 +0000 | [diff] [blame] | 42 | public: |
robertphillips@google.com | 0456e0b | 2012-06-27 14:03:26 +0000 | [diff] [blame] | 43 | SK_DECLARE_INST_COUNT(SkImageFilter) |
| 44 | |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 45 | class Proxy { |
| 46 | public: |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 47 | virtual ~Proxy() {}; |
| 48 | |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 49 | virtual SkDevice* createDevice(int width, int height) = 0; |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 50 | // returns true if the proxy can handle this filter natively |
| 51 | virtual bool canHandleImageFilter(SkImageFilter*) = 0; |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 52 | // returns true if the proxy handled the filter itself. if this returns |
| 53 | // false then the filter's code will be called. |
| 54 | virtual bool filterImage(SkImageFilter*, const SkBitmap& src, |
| 55 | const SkMatrix& ctm, |
| 56 | SkBitmap* result, SkIPoint* offset) = 0; |
| 57 | }; |
reed@google.com | 894aa9a | 2011-09-23 14:49:49 +0000 | [diff] [blame] | 58 | |
| 59 | /** |
| 60 | * Request a new (result) image to be created from the src image. |
| 61 | * If the src has no pixels (isNull()) then the request just wants to |
| 62 | * receive the config and width/height of the result. |
| 63 | * |
| 64 | * The matrix is the current matrix on the canvas. |
| 65 | * |
| 66 | * Offset is the amount to translate the resulting image relative to the |
| 67 | * src when it is drawn. |
| 68 | * |
| 69 | * If the result image cannot be created, return false, in which case both |
| 70 | * the result and offset parameters will be ignored by the caller. |
| 71 | */ |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 72 | bool filterImage(Proxy*, const SkBitmap& src, const SkMatrix& ctm, |
reed@google.com | 15356a6 | 2011-11-03 19:29:08 +0000 | [diff] [blame] | 73 | SkBitmap* result, SkIPoint* offset); |
| 74 | |
| 75 | /** |
reed@google.com | 32d25b6 | 2011-12-20 16:19:00 +0000 | [diff] [blame] | 76 | * Given the src bounds of an image, this returns the bounds of the result |
| 77 | * image after the filter has been applied. |
| 78 | */ |
| 79 | bool filterBounds(const SkIRect& src, const SkMatrix& ctm, SkIRect* dst); |
| 80 | |
| 81 | /** |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 82 | * Returns true if the filter can be expressed a single-pass |
| 83 | * GrCustomStage, used to process this filter on the GPU, or false if |
| 84 | * not. |
| 85 | * |
| 86 | * If stage is non-NULL, a new GrCustomStage instance is stored |
| 87 | * in it. The caller assumes ownership of the stage, and it is up to the |
| 88 | * caller to unref it. |
| 89 | */ |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame^] | 90 | virtual bool asNewCustomStage(GrCustomStage** stage, GrTexture*) const; |
senorblanco@chromium.org | 894790d | 2012-07-11 16:01:22 +0000 | [diff] [blame] | 91 | |
| 92 | /** |
reed@google.com | 15356a6 | 2011-11-03 19:29:08 +0000 | [diff] [blame] | 93 | * Experimental. |
| 94 | * |
| 95 | * If the filter can be expressed as a gaussian-blur, return true and |
| 96 | * set the sigma to the values for horizontal and vertical. |
| 97 | */ |
| 98 | virtual bool asABlur(SkSize* sigma) const; |
reed@google.com | 894aa9a | 2011-09-23 14:49:49 +0000 | [diff] [blame] | 99 | |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 100 | /** |
| 101 | * Experimental. |
| 102 | * |
| 103 | * If the filter can be expressed as an erode, return true and |
| 104 | * set the radius in X and Y. |
| 105 | */ |
| 106 | virtual bool asAnErode(SkISize* radius) const; |
| 107 | |
| 108 | /** |
| 109 | * Experimental. |
| 110 | * |
| 111 | * If the filter can be expressed as a dilation, return true and |
| 112 | * set the radius in X and Y. |
| 113 | */ |
| 114 | virtual bool asADilate(SkISize* radius) const; |
| 115 | |
reed@google.com | 894aa9a | 2011-09-23 14:49:49 +0000 | [diff] [blame] | 116 | protected: |
senorblanco@chromium.org | 54e01b2 | 2011-11-16 18:20:47 +0000 | [diff] [blame] | 117 | SkImageFilter() {} |
| 118 | explicit SkImageFilter(SkFlattenableReadBuffer& rb) : INHERITED(rb) {} |
reed@google.com | 32d25b6 | 2011-12-20 16:19:00 +0000 | [diff] [blame] | 119 | |
| 120 | // Default impl returns false |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 121 | virtual bool onFilterImage(Proxy*, const SkBitmap& src, const SkMatrix&, |
reed@google.com | 15356a6 | 2011-11-03 19:29:08 +0000 | [diff] [blame] | 122 | SkBitmap* result, SkIPoint* offset); |
reed@google.com | 32d25b6 | 2011-12-20 16:19:00 +0000 | [diff] [blame] | 123 | // Default impl copies src into dst and returns true |
| 124 | virtual bool onFilterBounds(const SkIRect&, const SkMatrix&, SkIRect*); |
reed@google.com | 894aa9a | 2011-09-23 14:49:49 +0000 | [diff] [blame] | 125 | |
| 126 | private: |
senorblanco@chromium.org | 54e01b2 | 2011-11-16 18:20:47 +0000 | [diff] [blame] | 127 | typedef SkFlattenable INHERITED; |
reed@google.com | 894aa9a | 2011-09-23 14:49:49 +0000 | [diff] [blame] | 128 | }; |
| 129 | |
| 130 | #endif |