1/*
2 * Copyright 2013 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 SkDisplacementMapEffect_DEFINED
9#define SkDisplacementMapEffect_DEFINED
10
11#include "SkImageFilter.h"
12#include "SkBitmap.h"
13
14class SK_API SkDisplacementMapEffect : public SkImageFilter {
15public:
16    enum ChannelSelectorType {
17        kUnknown_ChannelSelectorType,
18        kR_ChannelSelectorType,
19        kG_ChannelSelectorType,
20        kB_ChannelSelectorType,
21        kA_ChannelSelectorType
22    };
23
24    ~SkDisplacementMapEffect();
25
26    static SkDisplacementMapEffect* Create(ChannelSelectorType xChannelSelector,
27                                           ChannelSelectorType yChannelSelector,
28                                           SkScalar scale, SkImageFilter* displacement,
29                                           SkImageFilter* color = NULL,
30                                           const CropRect* cropRect = NULL) {
31        return SkNEW_ARGS(SkDisplacementMapEffect, (xChannelSelector, yChannelSelector, scale,
32                                                    displacement, color, cropRect));
33    }
34
35    SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkDisplacementMapEffect)
36
37    virtual bool onFilterImage(Proxy* proxy,
38                               const SkBitmap& src,
39                               const Context& ctx,
40                               SkBitmap* dst,
41                               SkIPoint* offset) const SK_OVERRIDE;
42    virtual void computeFastBounds(const SkRect& src, SkRect* dst) const SK_OVERRIDE;
43
44    virtual bool onFilterBounds(const SkIRect& src, const SkMatrix&,
45                                SkIRect* dst) const SK_OVERRIDE;
46
47#if SK_SUPPORT_GPU
48    virtual bool canFilterImageGPU() const SK_OVERRIDE { return true; }
49    virtual bool filterImageGPU(Proxy* proxy, const SkBitmap& src, const Context& ctx,
50                                SkBitmap* result, SkIPoint* offset) const SK_OVERRIDE;
51#endif
52
53protected:
54    SkDisplacementMapEffect(ChannelSelectorType xChannelSelector,
55                            ChannelSelectorType yChannelSelector,
56                            SkScalar scale, SkImageFilter* displacement,
57                            SkImageFilter* color = NULL,
58                            const CropRect* cropRect = NULL);
59    explicit SkDisplacementMapEffect(SkReadBuffer& buffer);
60    virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE;
61
62private:
63    ChannelSelectorType fXChannelSelector;
64    ChannelSelectorType fYChannelSelector;
65    SkScalar fScale;
66    typedef SkImageFilter INHERITED;
67    const SkImageFilter* getDisplacementInput() const { return getInput(0); }
68    const SkImageFilter* getColorInput() const { return getInput(1); }
69};
70
71#endif
72