blob: 684bb90e13e22606d778a29aff30544731d57f90 [file] [log] [blame]
Xavier Ducrohetb9761242010-12-23 10:22:14 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.graphics;
18
19import com.android.layoutlib.bridge.Bridge;
20import com.android.layoutlib.bridge.impl.DelegateManager;
21
22import android.os.Parcel;
23
24import java.awt.Rectangle;
25import java.awt.Shape;
26import java.awt.geom.AffineTransform;
27import java.awt.geom.Area;
28import java.awt.geom.Rectangle2D;
29
30/**
31 * Delegate implementing the native methods of android.graphics.Region
32 *
33 * Through the layoutlib_create tool, the original native methods of Region have been replaced
34 * by calls to methods of the same name in this delegate class.
35 *
36 * This class behaves like the original native implementation, but in Java, keeping previously
37 * native data into its own objects and mapping them to int that are sent back and forth between
38 * it and the original Region class.
39 *
40 * This also serve as a base class for all Region delegate classes.
41 *
42 * @see DelegateManager
43 *
44 */
45public class Region_Delegate {
46
47 // ---- delegate manager ----
48 protected static final DelegateManager<Region_Delegate> sManager =
49 new DelegateManager<Region_Delegate>();
50
51 // ---- delegate helper data ----
52
53 // ---- delegate data ----
54 private Area mArea = new Area();
55
56 // ---- Public Helper methods ----
57
58 public static Region_Delegate getDelegate(int nativeShader) {
59 return sManager.getDelegate(nativeShader);
60 }
61
62 public Area getJavaArea() {
63 return mArea;
64 }
65
66 /**
67 * Combines two {@link Shape} into another one (actually an {@link Area}), according
68 * to the given {@link Region.Op}.
69 *
70 * If the Op is not one that combines two shapes, then this return null
71 *
72 * @param shape1 the firt shape to combine
73 * @param shape2 the 2nd shape to combine
74 * @param regionOp the operande for the combine
75 * @return a new area or null.
76 */
77 public static Area combineShapes(Shape shape1, Shape shape2, int regionOp) {
78 if (regionOp == Region.Op.DIFFERENCE.nativeInt) {
79 // result is always a new area.
80 Area result = new Area(shape1);
81 result.subtract(shape2 instanceof Area ? (Area) shape2 : new Area(shape2));
82 return result;
83
84 } else if (regionOp == Region.Op.INTERSECT.nativeInt) {
85 // result is always a new area.
86 Area result = new Area(shape1);
87 result.intersect(shape2 instanceof Area ? (Area) shape2 : new Area(shape2));
88 return result;
89
90 } else if (regionOp == Region.Op.UNION.nativeInt) {
91 // result is always a new area.
92 Area result = new Area(shape1);
93 result.add(shape2 instanceof Area ? (Area) shape2 : new Area(shape2));
94 return result;
95
96 } else if (regionOp == Region.Op.XOR.nativeInt) {
97 // result is always a new area.
98 Area result = new Area(shape1);
99 result.exclusiveOr(shape2 instanceof Area ? (Area) shape2 : new Area(shape2));
100
101 } else if (regionOp == Region.Op.REVERSE_DIFFERENCE.nativeInt) {
102 // result is always a new area.
103 Area result = new Area(shape2);
104 result.subtract(shape1 instanceof Area ? (Area) shape1 : new Area(shape1));
105 return result;
106 }
107
108 return null;
109 }
110
111 // ---- native methods ----
112
113 /*package*/ static int nativeConstructor() {
114 Region_Delegate newDelegate = new Region_Delegate();
115 return sManager.addDelegate(newDelegate);
116 }
117
118 /*package*/ static void nativeDestructor(int native_region) {
119 sManager.removeDelegate(native_region);
120 }
121
122 /*package*/ static boolean nativeSetRegion(int native_dst, int native_src) {
123 Region_Delegate dstRegion = sManager.getDelegate(native_dst);
124 if (dstRegion == null) {
125 return true;
126 }
127
128 Region_Delegate srcRegion = sManager.getDelegate(native_src);
129 if (srcRegion == null) {
130 return true;
131 }
132
133 dstRegion.mArea.reset();
134 dstRegion.mArea.add(srcRegion.mArea);
135
136 return true;
137 }
138
139 /*package*/ static boolean nativeSetRect(int native_dst,
140 int left, int top, int right, int bottom) {
141 Region_Delegate dstRegion = sManager.getDelegate(native_dst);
142 if (dstRegion == null) {
143 return true;
144 }
145
146 dstRegion.mArea = new Area(new Rectangle2D.Float(left, top, right - left, bottom - top));
147 return dstRegion.mArea.getBounds().isEmpty() == false;
148 }
149
150 /*package*/ static boolean nativeSetPath(int native_dst, int native_path, int native_clip) {
151 Region_Delegate dstRegion = sManager.getDelegate(native_dst);
152 if (dstRegion == null) {
153 return true;
154 }
155
156 Path_Delegate path = Path_Delegate.getDelegate(native_path);
157 if (path == null) {
158 return true;
159 }
160
161 dstRegion.mArea = new Area(path.getJavaShape());
162
163 Region_Delegate clip = sManager.getDelegate(native_clip);
164 if (clip != null) {
165 dstRegion.mArea.subtract(clip.getJavaArea());
166 }
167
168 return dstRegion.mArea.getBounds().isEmpty() == false;
169 }
170
171 /*package*/ static boolean nativeGetBounds(int native_region, Rect rect) {
172 Region_Delegate region = sManager.getDelegate(native_region);
173 if (region == null) {
174 return true;
175 }
176
177 Rectangle bounds = region.mArea.getBounds();
178 if (bounds.isEmpty()) {
179 rect.left = rect.top = rect.right = rect.bottom = 0;
180 return false;
181 }
182
183 rect.left = bounds.x;
184 rect.top = bounds.y;
185 rect.right = bounds.x + bounds.width;
186 rect.bottom = bounds.y + bounds.height;
187 return true;
188 }
189
190 /*package*/ static boolean nativeGetBoundaryPath(int native_region, int native_path) {
191 Region_Delegate region = sManager.getDelegate(native_region);
192 if (region == null) {
193 return false;
194 }
195
196 Path_Delegate path = Path_Delegate.getDelegate(native_path);
197 if (path == null) {
198 return false;
199 }
200
201 if (region.mArea.isEmpty()) {
202 path.reset();
203 return false;
204 }
205
206 path.setPathIterator(region.mArea.getPathIterator(new AffineTransform()));
207 return true;
208 }
209
210 /*package*/ static boolean nativeOp(int native_dst,
211 int left, int top, int right, int bottom, int op) {
212 Region_Delegate region = sManager.getDelegate(native_dst);
213 if (region == null) {
214 return false;
215 }
216
217 region.mArea = combineShapes(region.mArea,
218 new Rectangle2D.Float(left, top, right - left, bottom - top), op);
219
220 assert region.mArea != null;
221 if (region.mArea != null) {
222 region.mArea = new Area();
223 }
224
225 return region.mArea.getBounds().isEmpty() == false;
226 }
227
228 /*package*/ static boolean nativeOp(int native_dst, Rect rect, int native_region, int op) {
229 Region_Delegate region = sManager.getDelegate(native_dst);
230 if (region == null) {
231 return false;
232 }
233
234 region.mArea = combineShapes(region.mArea,
235 new Rectangle2D.Float(rect.left, rect.top, rect.width(), rect.height()), op);
236
237 assert region.mArea != null;
238 if (region.mArea != null) {
239 region.mArea = new Area();
240 }
241
242 return region.mArea.getBounds().isEmpty() == false;
243 }
244
245 /*package*/ static boolean nativeOp(int native_dst,
246 int native_region1, int native_region2, int op) {
247 Region_Delegate dstRegion = sManager.getDelegate(native_dst);
248 if (dstRegion == null) {
249 return true;
250 }
251
252 Region_Delegate region1 = sManager.getDelegate(native_region1);
253 if (region1 == null) {
254 return false;
255 }
256
257 Region_Delegate region2 = sManager.getDelegate(native_region2);
258 if (region2 == null) {
259 return false;
260 }
261
262 dstRegion.mArea = combineShapes(region1.mArea, region2.mArea, op);
263
264 assert dstRegion.mArea != null;
265 if (dstRegion.mArea != null) {
266 dstRegion.mArea = new Area();
267 }
268
269 return dstRegion.mArea.getBounds().isEmpty() == false;
270
271 }
272
273 /*package*/ static int nativeCreateFromParcel(Parcel p) {
274 // This is only called by Region.CREATOR (Parcelable.Creator<Region>), which is only
275 // used during aidl call so really this should not be called.
276 Bridge.getLog().error(null,
277 "AIDL is not suppored, and therefore Regions cannot be created from parcels.");
278 return 0;
279 }
280
281 /*package*/ static boolean nativeWriteToParcel(int native_region,
282 Parcel p) {
283 // This is only called when sending a region through aidl, so really this should not
284 // be called.
285 Bridge.getLog().error(null,
286 "AIDL is not suppored, and therefore Regions cannot be written to parcels.");
287 return false;
288 }
289
290 /*package*/ static boolean nativeEquals(int native_r1, int native_r2) {
291 Region_Delegate region1 = sManager.getDelegate(native_r1);
292 if (region1 == null) {
293 return false;
294 }
295
296 Region_Delegate region2 = sManager.getDelegate(native_r2);
297 if (region2 == null) {
298 return false;
299 }
300
301 return region1.mArea.equals(region2.mArea);
302 }
303
304 // ---- Private delegate/helper methods ----
305
306}