blob: bc13b52edefcb85faab265336da0580e06060af1 [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 *
Xavier Ducrohet94252962011-01-06 15:50:42 -080072 * @param shape1 the firt shape to combine which can be null if there's no original clip.
Xavier Ducrohetb9761242010-12-23 10:22:14 -080073 * @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) {
Xavier Ducrohet94252962011-01-06 15:50:42 -080079 // if shape1 is null (empty), then the result is null.
80 if (shape1 == null) {
81 return null;
82 }
83
Xavier Ducrohetb9761242010-12-23 10:22:14 -080084 // result is always a new area.
85 Area result = new Area(shape1);
86 result.subtract(shape2 instanceof Area ? (Area) shape2 : new Area(shape2));
87 return result;
88
89 } else if (regionOp == Region.Op.INTERSECT.nativeInt) {
Xavier Ducrohet94252962011-01-06 15:50:42 -080090 // if shape1 is null, then the result is simply shape2.
91 if (shape1 == null) {
92 return new Area(shape2);
93 }
94
Xavier Ducrohetb9761242010-12-23 10:22:14 -080095 // result is always a new area.
96 Area result = new Area(shape1);
97 result.intersect(shape2 instanceof Area ? (Area) shape2 : new Area(shape2));
98 return result;
99
100 } else if (regionOp == Region.Op.UNION.nativeInt) {
Xavier Ducrohet94252962011-01-06 15:50:42 -0800101 // if shape1 is null, then the result is simply shape2.
102 if (shape1 == null) {
103 return new Area(shape2);
104 }
105
Xavier Ducrohetb9761242010-12-23 10:22:14 -0800106 // result is always a new area.
107 Area result = new Area(shape1);
108 result.add(shape2 instanceof Area ? (Area) shape2 : new Area(shape2));
109 return result;
110
111 } else if (regionOp == Region.Op.XOR.nativeInt) {
Xavier Ducrohet94252962011-01-06 15:50:42 -0800112 // if shape1 is null, then the result is simply shape2
113 if (shape1 == null) {
114 return new Area(shape2);
115 }
116
Xavier Ducrohetb9761242010-12-23 10:22:14 -0800117 // result is always a new area.
118 Area result = new Area(shape1);
119 result.exclusiveOr(shape2 instanceof Area ? (Area) shape2 : new Area(shape2));
Xavier Ducrohet94252962011-01-06 15:50:42 -0800120 return result;
Xavier Ducrohetb9761242010-12-23 10:22:14 -0800121
122 } else if (regionOp == Region.Op.REVERSE_DIFFERENCE.nativeInt) {
123 // result is always a new area.
124 Area result = new Area(shape2);
Xavier Ducrohet94252962011-01-06 15:50:42 -0800125
126 if (shape1 != null) {
127 result.subtract(shape1 instanceof Area ? (Area) shape1 : new Area(shape1));
128 }
129
Xavier Ducrohetb9761242010-12-23 10:22:14 -0800130 return result;
131 }
132
133 return null;
134 }
135
136 // ---- native methods ----
137
138 /*package*/ static int nativeConstructor() {
139 Region_Delegate newDelegate = new Region_Delegate();
140 return sManager.addDelegate(newDelegate);
141 }
142
143 /*package*/ static void nativeDestructor(int native_region) {
144 sManager.removeDelegate(native_region);
145 }
146
147 /*package*/ static boolean nativeSetRegion(int native_dst, int native_src) {
148 Region_Delegate dstRegion = sManager.getDelegate(native_dst);
149 if (dstRegion == null) {
150 return true;
151 }
152
153 Region_Delegate srcRegion = sManager.getDelegate(native_src);
154 if (srcRegion == null) {
155 return true;
156 }
157
158 dstRegion.mArea.reset();
159 dstRegion.mArea.add(srcRegion.mArea);
160
161 return true;
162 }
163
164 /*package*/ static boolean nativeSetRect(int native_dst,
165 int left, int top, int right, int bottom) {
166 Region_Delegate dstRegion = sManager.getDelegate(native_dst);
167 if (dstRegion == null) {
168 return true;
169 }
170
171 dstRegion.mArea = new Area(new Rectangle2D.Float(left, top, right - left, bottom - top));
172 return dstRegion.mArea.getBounds().isEmpty() == false;
173 }
174
175 /*package*/ static boolean nativeSetPath(int native_dst, int native_path, int native_clip) {
176 Region_Delegate dstRegion = sManager.getDelegate(native_dst);
177 if (dstRegion == null) {
178 return true;
179 }
180
181 Path_Delegate path = Path_Delegate.getDelegate(native_path);
182 if (path == null) {
183 return true;
184 }
185
186 dstRegion.mArea = new Area(path.getJavaShape());
187
188 Region_Delegate clip = sManager.getDelegate(native_clip);
189 if (clip != null) {
190 dstRegion.mArea.subtract(clip.getJavaArea());
191 }
192
193 return dstRegion.mArea.getBounds().isEmpty() == false;
194 }
195
196 /*package*/ static boolean nativeGetBounds(int native_region, Rect rect) {
197 Region_Delegate region = sManager.getDelegate(native_region);
198 if (region == null) {
199 return true;
200 }
201
202 Rectangle bounds = region.mArea.getBounds();
203 if (bounds.isEmpty()) {
204 rect.left = rect.top = rect.right = rect.bottom = 0;
205 return false;
206 }
207
208 rect.left = bounds.x;
209 rect.top = bounds.y;
210 rect.right = bounds.x + bounds.width;
211 rect.bottom = bounds.y + bounds.height;
212 return true;
213 }
214
215 /*package*/ static boolean nativeGetBoundaryPath(int native_region, int native_path) {
216 Region_Delegate region = sManager.getDelegate(native_region);
217 if (region == null) {
218 return false;
219 }
220
221 Path_Delegate path = Path_Delegate.getDelegate(native_path);
222 if (path == null) {
223 return false;
224 }
225
226 if (region.mArea.isEmpty()) {
227 path.reset();
228 return false;
229 }
230
231 path.setPathIterator(region.mArea.getPathIterator(new AffineTransform()));
232 return true;
233 }
234
235 /*package*/ static boolean nativeOp(int native_dst,
236 int left, int top, int right, int bottom, int op) {
237 Region_Delegate region = sManager.getDelegate(native_dst);
238 if (region == null) {
239 return false;
240 }
241
242 region.mArea = combineShapes(region.mArea,
243 new Rectangle2D.Float(left, top, right - left, bottom - top), op);
244
245 assert region.mArea != null;
246 if (region.mArea != null) {
247 region.mArea = new Area();
248 }
249
250 return region.mArea.getBounds().isEmpty() == false;
251 }
252
253 /*package*/ static boolean nativeOp(int native_dst, Rect rect, int native_region, int op) {
254 Region_Delegate region = sManager.getDelegate(native_dst);
255 if (region == null) {
256 return false;
257 }
258
259 region.mArea = combineShapes(region.mArea,
260 new Rectangle2D.Float(rect.left, rect.top, rect.width(), rect.height()), op);
261
262 assert region.mArea != null;
263 if (region.mArea != null) {
264 region.mArea = new Area();
265 }
266
267 return region.mArea.getBounds().isEmpty() == false;
268 }
269
270 /*package*/ static boolean nativeOp(int native_dst,
271 int native_region1, int native_region2, int op) {
272 Region_Delegate dstRegion = sManager.getDelegate(native_dst);
273 if (dstRegion == null) {
274 return true;
275 }
276
277 Region_Delegate region1 = sManager.getDelegate(native_region1);
278 if (region1 == null) {
279 return false;
280 }
281
282 Region_Delegate region2 = sManager.getDelegate(native_region2);
283 if (region2 == null) {
284 return false;
285 }
286
287 dstRegion.mArea = combineShapes(region1.mArea, region2.mArea, op);
288
289 assert dstRegion.mArea != null;
290 if (dstRegion.mArea != null) {
291 dstRegion.mArea = new Area();
292 }
293
294 return dstRegion.mArea.getBounds().isEmpty() == false;
295
296 }
297
298 /*package*/ static int nativeCreateFromParcel(Parcel p) {
299 // This is only called by Region.CREATOR (Parcelable.Creator<Region>), which is only
300 // used during aidl call so really this should not be called.
301 Bridge.getLog().error(null,
302 "AIDL is not suppored, and therefore Regions cannot be created from parcels.");
303 return 0;
304 }
305
306 /*package*/ static boolean nativeWriteToParcel(int native_region,
307 Parcel p) {
308 // This is only called when sending a region through aidl, so really this should not
309 // be called.
310 Bridge.getLog().error(null,
311 "AIDL is not suppored, and therefore Regions cannot be written to parcels.");
312 return false;
313 }
314
315 /*package*/ static boolean nativeEquals(int native_r1, int native_r2) {
316 Region_Delegate region1 = sManager.getDelegate(native_r1);
317 if (region1 == null) {
318 return false;
319 }
320
321 Region_Delegate region2 = sManager.getDelegate(native_r2);
322 if (region2 == null) {
323 return false;
324 }
325
326 return region1.mArea.equals(region2.mArea);
327 }
328
329 // ---- Private delegate/helper methods ----
330
331}