SkImageFilter.h revision 4f1dae40e24d57d647db01443b8bf2410514b8b5
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
13class SkBitmap;
14class SkDevice;
15class SkMatrix;
16struct SkPoint;
17
18/**
19 *  Experimental.
20 *
21 *  Base class for image filters. If one is installed in the paint, then
22 *  all drawing occurs as usual, but it is as if the drawing happened into an
23 *  offscreen (before the xfermode is applied). This offscreen bitmap will
24 *  then be handed to the imagefilter, who in turn creates a new bitmap which
25 *  is what will finally be drawn to the device (using the original xfermode).
26 *
27 *  THIS SIGNATURE IS TEMPORARY
28 *
29 *  There are several weaknesses in this function signature:
30 *  1. Does not expose the destination/target device, so filters that can draw
31 *     directly to it are unable to take advantage of that optimization.
32 *  2. Does not expose a way to create a "compabitible" image (i.e. gpu -> gpu)
33 *  3. As with #1, the filter is unable to "read" the dest (which would be slow)
34 *
35 *  Therefore, we should not create any real dependencies on this API yet -- it
36 *  is being checked in as a check-point so we can explore these and other
37 *  considerations.
38 */
39class SK_API SkImageFilter : public SkFlattenable {
40public:
41    class Proxy {
42    public:
43        virtual SkDevice* createDevice(int width, int height) = 0;
44
45        // returns true if the proxy handled the filter itself. if this returns
46        // false then the filter's code will be called.
47        virtual bool filterImage(SkImageFilter*, const SkBitmap& src,
48                                 const SkMatrix& ctm,
49                                 SkBitmap* result, SkIPoint* offset) = 0;
50        virtual ~Proxy() {};
51    };
52
53    /**
54     *  Request a new (result) image to be created from the src image.
55     *  If the src has no pixels (isNull()) then the request just wants to
56     *  receive the config and width/height of the result.
57     *
58     *  The matrix is the current matrix on the canvas.
59     *
60     *  Offset is the amount to translate the resulting image relative to the
61     *  src when it is drawn.
62     *
63     *  If the result image cannot be created, return false, in which case both
64     *  the result and offset parameters will be ignored by the caller.
65     */
66    bool filterImage(Proxy*, const SkBitmap& src, const SkMatrix& ctm,
67                     SkBitmap* result, SkIPoint* offset);
68
69    /**
70     *  Given the src bounds of an image, this returns the bounds of the result
71     *  image after the filter has been applied.
72     */
73    bool filterBounds(const SkIRect& src, const SkMatrix& ctm, SkIRect* dst);
74
75    /**
76     *  Experimental.
77     *
78     *  If the filter can be expressed as a gaussian-blur, return true and
79     *  set the sigma to the values for horizontal and vertical.
80     */
81    virtual bool asABlur(SkSize* sigma) const;
82
83    /**
84     *  Experimental.
85     *
86     *  If the filter can be expressed as an erode, return true and
87     *  set the radius in X and Y.
88     */
89    virtual bool asAnErode(SkISize* radius) const;
90
91    /**
92     *  Experimental.
93     *
94     *  If the filter can be expressed as a dilation, return true and
95     *  set the radius in X and Y.
96     */
97    virtual bool asADilate(SkISize* radius) const;
98
99protected:
100    SkImageFilter() {}
101    explicit SkImageFilter(SkFlattenableReadBuffer& rb) : INHERITED(rb) {}
102
103    // Default impl returns false
104    virtual bool onFilterImage(Proxy*, const SkBitmap& src, const SkMatrix&,
105                               SkBitmap* result, SkIPoint* offset);
106    // Default impl copies src into dst and returns true
107    virtual bool onFilterBounds(const SkIRect&, const SkMatrix&, SkIRect*);
108
109private:
110    typedef SkFlattenable INHERITED;
111};
112
113#endif
114