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      * Order is bottom to top, then right to left.
54      *
55      * @param region the given region.
56      * @param rectConsumer the action holder.
57      */
forEachRectReverse(Region region, Consumer<Rect> rectConsumer)58     public static void forEachRectReverse(Region region, Consumer<Rect> rectConsumer) {
59         final RegionIterator it = new RegionIterator(region);
60         final ArrayList<Rect> rects = new ArrayList<>();
61         final Rect rect = new Rect();
62         while (it.next(rect)) {
63             rects.add(new Rect(rect));
64         }
65         // TODO: instead of creating an array and reversing it, expose the reverse iterator through
66         //       JNI.
67         Collections.reverse(rects);
68         rects.forEach(rectConsumer);
69     }
70 }
71