1 /* 2 * Copyright (C) 2019 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 com.android.server.wm.utils; 18 19 import android.graphics.Rect; 20 import android.graphics.Region; 21 import android.graphics.RegionIterator; 22 23 import java.util.ArrayList; 24 import java.util.Collections; 25 import java.util.List; 26 import java.util.function.Consumer; 27 28 /** 29 * Utility methods to handle Regions. 30 */ 31 public class RegionUtils { 32 RegionUtils()33 private RegionUtils() {} 34 35 36 /** 37 * Converts a list of rects into a {@code Region}. 38 * 39 * @param rects the list of rects to convert 40 * @param outRegion the Region to set to the list of rects 41 */ rectListToRegion(List<Rect> rects, Region outRegion)42 public static void rectListToRegion(List<Rect> rects, Region outRegion) { 43 outRegion.setEmpty(); 44 final int n = rects.size(); 45 for (int i = 0; i < n; i++) { 46 outRegion.union(rects.get(i)); 47 } 48 } 49 50 /** 51 * Applies actions on each rect contained within a {@code Region}. 52 * 53 * @param region the given region. 54 * @param rectConsumer the action holder. 55 */ forEachRect(Region region, Consumer<Rect> rectConsumer)56 public static void forEachRect(Region region, Consumer<Rect> rectConsumer) { 57 final RegionIterator it = new RegionIterator(region); 58 final Rect rect = new Rect(); 59 while (it.next(rect)) { 60 rectConsumer.accept(rect); 61 } 62 } 63 64 /** 65 * Applies actions on each rect contained within a {@code Region}. 66 * 67 * Order is bottom to top, then right to left. 68 * 69 * @param region the given region. 70 * @param rectConsumer the action holder. 71 */ forEachRectReverse(Region region, Consumer<Rect> rectConsumer)72 public static void forEachRectReverse(Region region, Consumer<Rect> rectConsumer) { 73 final RegionIterator it = new RegionIterator(region); 74 final ArrayList<Rect> rects = new ArrayList<>(); 75 final Rect rect = new Rect(); 76 while (it.next(rect)) { 77 rects.add(new Rect(rect)); 78 } 79 // TODO: instead of creating an array and reversing it, expose the reverse iterator through 80 // JNI. 81 Collections.reverse(rects); 82 rects.forEach(rectConsumer); 83 } 84 } 85