1 /*
2  * Copyright 2012 The Android Open Source Project
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 
9 #ifndef SkMorphologyImageFilter_DEFINED
10 #define SkMorphologyImageFilter_DEFINED
11 
12 #include "SkColor.h"
13 #include "SkImageFilter.h"
14 #include "SkSize.h"
15 
16 class SK_API SkMorphologyImageFilter : public SkImageFilter {
17 public:
18     void computeFastBounds(const SkRect& src, SkRect* dst) const override;
19     bool onFilterBounds(const SkIRect& src, const SkMatrix& ctm, SkIRect* dst) const override;
20 
21     /**
22      * All morphology procs have the same signature: src is the source buffer, dst the
23      * destination buffer, radius is the morphology radius, width and height are the bounds
24      * of the destination buffer (in pixels), and srcStride and dstStride are the
25      * number of pixels per row in each buffer. All buffers are 8888.
26      */
27 
28     typedef void (*Proc)(const SkPMColor* src, SkPMColor* dst, int radius,
29                          int width, int height, int srcStride, int dstStride);
30 
31 protected:
32     SkMorphologyImageFilter(int radiusX, int radiusY, SkImageFilter* input,
33                             const CropRect* cropRect);
34     bool filterImageGeneric(Proc procX, Proc procY,
35                             Proxy*, const SkBitmap& src, const Context&,
36                             SkBitmap* result, SkIPoint* offset) const;
37     void flatten(SkWriteBuffer&) const override;
38 #if SK_SUPPORT_GPU
canFilterImageGPU()39     bool canFilterImageGPU() const override { return true; }
40     bool filterImageGPUGeneric(bool dilate, Proxy* proxy, const SkBitmap& src,
41                                const Context& ctm, SkBitmap* result,
42                                SkIPoint* offset) const;
43 #endif
44 
radius()45     SkISize    radius() const { return fRadius; }
46 
47 private:
48     SkISize    fRadius;
49     typedef SkImageFilter INHERITED;
50 };
51 
52 class SK_API SkDilateImageFilter : public SkMorphologyImageFilter {
53 public:
54     static SkDilateImageFilter* Create(int radiusX, int radiusY,
55                                        SkImageFilter* input = NULL,
56                                        const CropRect* cropRect = NULL) {
57         if (radiusX < 0 || radiusY < 0) {
58             return NULL;
59         }
60         return SkNEW_ARGS(SkDilateImageFilter, (radiusX, radiusY, input, cropRect));
61     }
62 
63     virtual bool onFilterImage(Proxy*, const SkBitmap& src, const Context&,
64                                SkBitmap* result, SkIPoint* offset) const override;
65 #if SK_SUPPORT_GPU
66     virtual bool filterImageGPU(Proxy* proxy, const SkBitmap& src, const Context&,
67                                 SkBitmap* result, SkIPoint* offset) const override;
68 #endif
69 
70     SK_TO_STRING_OVERRIDE()
SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkDilateImageFilter)71     SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkDilateImageFilter)
72 
73 protected:
74     SkDilateImageFilter(int radiusX, int radiusY, SkImageFilter* input, const CropRect* cropRect)
75         : INHERITED(radiusX, radiusY, input, cropRect) {}
76 private:
77     typedef SkMorphologyImageFilter INHERITED;
78 };
79 
80 class SK_API SkErodeImageFilter : public SkMorphologyImageFilter {
81 public:
82     static SkErodeImageFilter* Create(int radiusX, int radiusY,
83                                       SkImageFilter* input = NULL,
84                                       const CropRect* cropRect = NULL) {
85         if (radiusX < 0 || radiusY < 0) {
86             return NULL;
87         }
88         return SkNEW_ARGS(SkErodeImageFilter, (radiusX, radiusY, input, cropRect));
89     }
90 
91     virtual bool onFilterImage(Proxy*, const SkBitmap& src, const Context&,
92                                SkBitmap* result, SkIPoint* offset) const override;
93 #if SK_SUPPORT_GPU
94     virtual bool filterImageGPU(Proxy* proxy, const SkBitmap& src, const Context&,
95                                 SkBitmap* result, SkIPoint* offset) const override;
96 #endif
97 
98     SK_TO_STRING_OVERRIDE()
SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkErodeImageFilter)99     SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkErodeImageFilter)
100 
101 protected:
102     SkErodeImageFilter(int radiusX, int radiusY, SkImageFilter* input, const CropRect* cropRect)
103         : INHERITED(radiusX, radiusY, input, cropRect) {}
104 
105 private:
106     typedef SkMorphologyImageFilter INHERITED;
107 };
108 
109 #endif
110