1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
|
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.graphics;
import com.android.ide.common.rendering.api.LayoutLog;
import com.android.layoutlib.bridge.Bridge;
import com.android.layoutlib.bridge.impl.DelegateManager;
import com.android.tools.layoutlib.annotations.LayoutlibDelegate;
import android.os.Parcel;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.Area;
import java.awt.geom.Rectangle2D;
/**
* Delegate implementing the native methods of android.graphics.Region
*
* Through the layoutlib_create tool, the original native methods of Region have been replaced
* by calls to methods of the same name in this delegate class.
*
* This class behaves like the original native implementation, but in Java, keeping previously
* native data into its own objects and mapping them to int that are sent back and forth between
* it and the original Region class.
*
* This also serve as a base class for all Region delegate classes.
*
* @see DelegateManager
*
*/
public class Region_Delegate {
// ---- delegate manager ----
protected static final DelegateManager<Region_Delegate> sManager =
new DelegateManager<Region_Delegate>(Region_Delegate.class);
// ---- delegate helper data ----
// ---- delegate data ----
private Area mArea = new Area();
// ---- Public Helper methods ----
public static Region_Delegate getDelegate(long nativeShader) {
return sManager.getDelegate(nativeShader);
}
public Area getJavaArea() {
return mArea;
}
/**
* Combines two {@link Shape} into another one (actually an {@link Area}), according
* to the given {@link Region.Op}.
*
* If the Op is not one that combines two shapes, then this return null
*
* @param shape1 the firt shape to combine which can be null if there's no original clip.
* @param shape2 the 2nd shape to combine
* @param regionOp the operande for the combine
* @return a new area or null.
*/
public static Area combineShapes(Shape shape1, Shape shape2, int regionOp) {
if (regionOp == Region.Op.DIFFERENCE.nativeInt) {
// if shape1 is null (empty), then the result is null.
if (shape1 == null) {
return null;
}
// result is always a new area.
Area result = new Area(shape1);
result.subtract(shape2 instanceof Area ? (Area) shape2 : new Area(shape2));
return result;
} else if (regionOp == Region.Op.INTERSECT.nativeInt) {
// if shape1 is null, then the result is simply shape2.
if (shape1 == null) {
return new Area(shape2);
}
// result is always a new area.
Area result = new Area(shape1);
result.intersect(shape2 instanceof Area ? (Area) shape2 : new Area(shape2));
return result;
} else if (regionOp == Region.Op.UNION.nativeInt) {
// if shape1 is null, then the result is simply shape2.
if (shape1 == null) {
return new Area(shape2);
}
// result is always a new area.
Area result = new Area(shape1);
result.add(shape2 instanceof Area ? (Area) shape2 : new Area(shape2));
return result;
} else if (regionOp == Region.Op.XOR.nativeInt) {
// if shape1 is null, then the result is simply shape2
if (shape1 == null) {
return new Area(shape2);
}
// result is always a new area.
Area result = new Area(shape1);
result.exclusiveOr(shape2 instanceof Area ? (Area) shape2 : new Area(shape2));
return result;
} else if (regionOp == Region.Op.REVERSE_DIFFERENCE.nativeInt) {
// result is always a new area.
Area result = new Area(shape2);
if (shape1 != null) {
result.subtract(shape1 instanceof Area ? (Area) shape1 : new Area(shape1));
}
return result;
}
return null;
}
// ---- native methods ----
@LayoutlibDelegate
/*package*/ static boolean isEmpty(Region thisRegion) {
Region_Delegate regionDelegate = sManager.getDelegate(thisRegion.mNativeRegion);
if (regionDelegate == null) {
return true;
}
return regionDelegate.mArea.isEmpty();
}
@LayoutlibDelegate
/*package*/ static boolean isRect(Region thisRegion) {
Region_Delegate regionDelegate = sManager.getDelegate(thisRegion.mNativeRegion);
if (regionDelegate == null) {
return true;
}
return regionDelegate.mArea.isRectangular();
}
@LayoutlibDelegate
/*package*/ static boolean isComplex(Region thisRegion) {
Region_Delegate regionDelegate = sManager.getDelegate(thisRegion.mNativeRegion);
if (regionDelegate == null) {
return true;
}
return regionDelegate.mArea.isSingular() == false;
}
@LayoutlibDelegate
/*package*/ static boolean contains(Region thisRegion, int x, int y) {
Region_Delegate regionDelegate = sManager.getDelegate(thisRegion.mNativeRegion);
if (regionDelegate == null) {
return false;
}
return regionDelegate.mArea.contains(x, y);
}
@LayoutlibDelegate
/*package*/ static boolean quickContains(Region thisRegion,
int left, int top, int right, int bottom) {
Region_Delegate regionDelegate = sManager.getDelegate(thisRegion.mNativeRegion);
if (regionDelegate == null) {
return false;
}
return regionDelegate.mArea.isRectangular() &&
regionDelegate.mArea.contains(left, top, right - left, bottom - top);
}
@LayoutlibDelegate
/*package*/ static boolean quickReject(Region thisRegion,
int left, int top, int right, int bottom) {
Region_Delegate regionDelegate = sManager.getDelegate(thisRegion.mNativeRegion);
if (regionDelegate == null) {
return false;
}
return regionDelegate.mArea.isEmpty() ||
regionDelegate.mArea.intersects(left, top, right - left, bottom - top) == false;
}
@LayoutlibDelegate
/*package*/ static boolean quickReject(Region thisRegion, Region rgn) {
Region_Delegate regionDelegate = sManager.getDelegate(thisRegion.mNativeRegion);
if (regionDelegate == null) {
return false;
}
Region_Delegate targetRegionDelegate = sManager.getDelegate(rgn.mNativeRegion);
if (targetRegionDelegate == null) {
return false;
}
return regionDelegate.mArea.isEmpty() ||
regionDelegate.mArea.getBounds().intersects(
targetRegionDelegate.mArea.getBounds()) == false;
}
@LayoutlibDelegate
/*package*/ static void translate(Region thisRegion, int dx, int dy, Region dst) {
Region_Delegate regionDelegate = sManager.getDelegate(thisRegion.mNativeRegion);
if (regionDelegate == null) {
return;
}
Region_Delegate targetRegionDelegate = sManager.getDelegate(dst.mNativeRegion);
if (targetRegionDelegate == null) {
return;
}
if (regionDelegate.mArea.isEmpty()) {
targetRegionDelegate.mArea = new Area();
} else {
targetRegionDelegate.mArea = new Area(regionDelegate.mArea);
AffineTransform mtx = new AffineTransform();
mtx.translate(dx, dy);
targetRegionDelegate.mArea.transform(mtx);
}
}
@LayoutlibDelegate
/*package*/ static void scale(Region thisRegion, float scale, Region dst) {
Region_Delegate regionDelegate = sManager.getDelegate(thisRegion.mNativeRegion);
if (regionDelegate == null) {
return;
}
Region_Delegate targetRegionDelegate = sManager.getDelegate(dst.mNativeRegion);
if (targetRegionDelegate == null) {
return;
}
if (regionDelegate.mArea.isEmpty()) {
targetRegionDelegate.mArea = new Area();
} else {
targetRegionDelegate.mArea = new Area(regionDelegate.mArea);
AffineTransform mtx = new AffineTransform();
mtx.scale(scale, scale);
targetRegionDelegate.mArea.transform(mtx);
}
}
@LayoutlibDelegate
/*package*/ static long nativeConstructor() {
Region_Delegate newDelegate = new Region_Delegate();
return sManager.addNewDelegate(newDelegate);
}
@LayoutlibDelegate
/*package*/ static void nativeDestructor(long native_region) {
sManager.removeJavaReferenceFor(native_region);
}
@LayoutlibDelegate
/*package*/ static void nativeSetRegion(long native_dst, long native_src) {
Region_Delegate dstRegion = sManager.getDelegate(native_dst);
if (dstRegion == null) {
return;
}
Region_Delegate srcRegion = sManager.getDelegate(native_src);
if (srcRegion == null) {
return;
}
dstRegion.mArea.reset();
dstRegion.mArea.add(srcRegion.mArea);
}
@LayoutlibDelegate
/*package*/ static boolean nativeSetRect(long native_dst,
int left, int top, int right, int bottom) {
Region_Delegate dstRegion = sManager.getDelegate(native_dst);
if (dstRegion == null) {
return true;
}
dstRegion.mArea = new Area(new Rectangle2D.Float(left, top, right - left, bottom - top));
return dstRegion.mArea.getBounds().isEmpty() == false;
}
@LayoutlibDelegate
/*package*/ static boolean nativeSetPath(long native_dst, long native_path, long native_clip) {
Region_Delegate dstRegion = sManager.getDelegate(native_dst);
if (dstRegion == null) {
return true;
}
Path_Delegate path = Path_Delegate.getDelegate(native_path);
if (path == null) {
return true;
}
dstRegion.mArea = new Area(path.getJavaShape());
Region_Delegate clip = sManager.getDelegate(native_clip);
if (clip != null) {
dstRegion.mArea.subtract(clip.getJavaArea());
}
return dstRegion.mArea.getBounds().isEmpty() == false;
}
@LayoutlibDelegate
/*package*/ static boolean nativeGetBounds(long native_region, Rect rect) {
Region_Delegate region = sManager.getDelegate(native_region);
if (region == null) {
return true;
}
Rectangle bounds = region.mArea.getBounds();
if (bounds.isEmpty()) {
rect.left = rect.top = rect.right = rect.bottom = 0;
return false;
}
rect.left = bounds.x;
rect.top = bounds.y;
rect.right = bounds.x + bounds.width;
rect.bottom = bounds.y + bounds.height;
return true;
}
@LayoutlibDelegate
/*package*/ static boolean nativeGetBoundaryPath(long native_region, long native_path) {
Region_Delegate region = sManager.getDelegate(native_region);
if (region == null) {
return false;
}
Path_Delegate path = Path_Delegate.getDelegate(native_path);
if (path == null) {
return false;
}
if (region.mArea.isEmpty()) {
path.reset();
return false;
}
path.setPathIterator(region.mArea.getPathIterator(new AffineTransform()));
return true;
}
@LayoutlibDelegate
/*package*/ static boolean nativeOp(long native_dst,
int left, int top, int right, int bottom, int op) {
Region_Delegate region = sManager.getDelegate(native_dst);
if (region == null) {
return false;
}
region.mArea = combineShapes(region.mArea,
new Rectangle2D.Float(left, top, right - left, bottom - top), op);
assert region.mArea != null;
if (region.mArea != null) {
region.mArea = new Area();
}
return region.mArea.getBounds().isEmpty() == false;
}
@LayoutlibDelegate
/*package*/ static boolean nativeOp(long native_dst, Rect rect, long native_region, int op) {
Region_Delegate region = sManager.getDelegate(native_dst);
if (region == null) {
return false;
}
region.mArea = combineShapes(region.mArea,
new Rectangle2D.Float(rect.left, rect.top, rect.width(), rect.height()), op);
assert region.mArea != null;
if (region.mArea != null) {
region.mArea = new Area();
}
return region.mArea.getBounds().isEmpty() == false;
}
@LayoutlibDelegate
/*package*/ static boolean nativeOp(long native_dst,
long native_region1, long native_region2, int op) {
Region_Delegate dstRegion = sManager.getDelegate(native_dst);
if (dstRegion == null) {
return true;
}
Region_Delegate region1 = sManager.getDelegate(native_region1);
if (region1 == null) {
return false;
}
Region_Delegate region2 = sManager.getDelegate(native_region2);
if (region2 == null) {
return false;
}
dstRegion.mArea = combineShapes(region1.mArea, region2.mArea, op);
assert dstRegion.mArea != null;
if (dstRegion.mArea != null) {
dstRegion.mArea = new Area();
}
return dstRegion.mArea.getBounds().isEmpty() == false;
}
@LayoutlibDelegate
/*package*/ static long nativeCreateFromParcel(Parcel p) {
// This is only called by Region.CREATOR (Parcelable.Creator<Region>), which is only
// used during aidl call so really this should not be called.
Bridge.getLog().error(LayoutLog.TAG_UNSUPPORTED,
"AIDL is not suppored, and therefore Regions cannot be created from parcels.",
null /*data*/);
return 0;
}
@LayoutlibDelegate
/*package*/ static boolean nativeWriteToParcel(long native_region,
Parcel p) {
// This is only called when sending a region through aidl, so really this should not
// be called.
Bridge.getLog().error(LayoutLog.TAG_UNSUPPORTED,
"AIDL is not suppored, and therefore Regions cannot be written to parcels.",
null /*data*/);
return false;
}
@LayoutlibDelegate
/*package*/ static boolean nativeEquals(long native_r1, long native_r2) {
Region_Delegate region1 = sManager.getDelegate(native_r1);
if (region1 == null) {
return false;
}
Region_Delegate region2 = sManager.getDelegate(native_r2);
if (region2 == null) {
return false;
}
return region1.mArea.equals(region2.mArea);
}
@LayoutlibDelegate
/*package*/ static String nativeToString(long native_region) {
Region_Delegate region = sManager.getDelegate(native_region);
if (region == null) {
return "not found";
}
return region.mArea.toString();
}
// ---- Private delegate/helper methods ----
}
|