1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #ifndef REGION_H
17 #define REGION_H
18
19 #include "impl_interface/region_impl.h"
20 #include "utils/drawing_macros.h"
21
22 namespace OHOS {
23 namespace Rosen {
24 namespace Drawing {
25 enum class RegionOp {
26 DIFFERENCE,
27 INTERSECT,
28 UNION,
29 XOR,
30 REVERSE_DIFFERENCE,
31 REPLACE,
32 };
33 class DRAWING_API Region {
34 public:
35 Region();
36 Region(const Region& other);
37 Region& operator=(const Region& other);
38 bool operator==(const Region& other) const;
39 virtual ~Region() = default;
40
Clone(const Region & other)41 void Clone(const Region& other)
42 {
43 impl_->Clone(other);
44 }
45
GetDrawingType()46 virtual DrawingType GetDrawingType() const
47 {
48 return DrawingType::COMMON;
49 }
50
51 virtual bool Contains(int32_t x, int32_t y) const;
52
53 /**
54 * @brief Sets to empty bounds at (0, 0) with zero width and height.
55 */
56 virtual void SetEmpty();
57
58 /**
59 * @brief Constructs a rectangular Region matching the bounds of rect.
60 * @param rectI Bounds of constructed Region.
61 * @return If rectI is empty, constructs empty and returns false.
62 */
63 virtual bool SetRect(const RectI& rectI);
64
65 /**
66 * @brief Constructs a copy of an existing region.
67 * @param region Region to copy by value.
68 * @return Return true if constructed region is not empty.
69 */
70 virtual bool SetRegion(const Region& region);
71
72 /**
73 * @brief Constructs Region to match outline of path within clip.
74 * @param path Providing outline.
75 * @param clip Containing path.
76 * @return Return true if constructed Region is not empty.
77 */
78 virtual bool SetPath(const Path& path, const Region& clip);
79
80 /**
81 * @brief Appends outline of Region to path.
82 * @param path Path to append to.
83 * @return Return true if path changed.
84 */
85 bool GetBoundaryPath(Path* path) const;
86
87 /**
88 * @brief Get the external rect with the smallest region.
89 * @return Return the external rect with the smallest region.
90 */
91 RectI GetBounds() const;
92
93 /**
94 * @brief Determine if the area contains multiple rectangles.
95 * @return If true indicates that the Region contains multiple rectangles.
96 */
97 bool IsComplex() const;
98
99 /**
100 * @brief Determines whether it intersects other.
101 * @param other Other Region object.
102 * @return If true indicates that other and Region have area in common.
103 */
104 bool IsIntersects(const Region& other) const;
105
106 /**
107 * @brief Determines whether Region is empty.
108 * @return If true indicates that bounds has no width or height.
109 */
110 bool IsEmpty() const;
111
112 /**
113 * @brief Determines whether Region is one Rect with positive dimensions.
114 * @return If true indicates that Region contains one Rect.
115 */
116 bool IsRect() const;
117
118 /**
119 * @brief Determines whether other region is in the region.
120 * @param other Other region object.
121 * @return If true indicates that other and region have area in common.
122 */
123 virtual bool IsRegionContained(const Region& other) const;
124
125 /**
126 * @brief Replaces Region with the result of Region op region.
127 * @param region Operand.
128 * @param op Operation type.
129 * @return Returns true if replaced Region is not empty.
130 */
131 virtual bool Op(const Region& region, RegionOp op);
132
133 /**
134 * @brief Determines whether rect and region does not intersect.
135 * @param rectI RectI to intersect.
136 * @return Returns true if rect and region is not intersect.
137 */
138 virtual bool QuickReject(const RectI& rectI) const;
139
140 /**
141 * @brief Determines whether two regions does not intersect.
142 * @param region Region to intersect.
143 * @return Returns true if two regions are not intersect.
144 */
145 virtual bool QuickReject(const Region& region) const;
146
147 /**
148 * @brief Translate the region by (x, y).
149 * @param x horizontal translation.
150 * @param y vertical translation.
151 */
152 void Translate(int32_t x, int32_t y);
153
154 inline void Dump(std::string& out) const;
155
156 std::shared_ptr<Data> Serialize() const;
157 bool Deserialize(std::shared_ptr<Data> data);
158
159 /**
160 * @brief Get the adaptation layer instance, called in the adaptation layer.
161 * @return Adaptation Layer instance.
162 */
163 template<typename T>
GetImpl()164 T* GetImpl() const
165 {
166 return impl_->DowncastingTo<T>();
167 }
168
169 private:
170 std::shared_ptr<RegionImpl> impl_;
171 };
172
Dump(std::string & out)173 inline void Region::Dump(std::string& out) const
174 {
175 out += '[';
176 if (impl_ != nullptr) {
177 out += "empty:" + std::string(impl_->IsEmpty() ? "true" : "false");
178 out += " isRect:" + std::string(impl_->IsRect() ? "true" : "false");
179 out += " BoundaryPath";
180 Path path;
181 impl_->GetBoundaryPath(&path);
182 path.Dump(out);
183 }
184 out += ']';
185 }
186 } // namespace Drawing
187 } // namespace Rosen
188 } // namespace OHOS
189 #endif
190