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