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