1 /*
2  * Copyright (C) 2018 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.uirendering.cts.bitmapverifiers;
18 
19 import static org.junit.Assert.assertFalse;
20 
21 import android.graphics.Rect;
22 import android.graphics.Region;
23 import android.graphics.RegionIterator;
24 
25 import java.util.ArrayList;
26 import java.util.List;
27 
28 public class RegionVerifier extends BitmapVerifier {
29     private static class SubRegionVerifiers {
30         public Region region;
31         public BitmapVerifier verifier;
32 
SubRegionVerifiers(Region region, BitmapVerifier verifier)33         SubRegionVerifiers(Region region, BitmapVerifier verifier) {
34             this.region = region;
35             this.verifier = verifier;
36         }
37     }
38 
39     private List<SubRegionVerifiers> mRegionVerifiers = new ArrayList<>();
40 
41     @Override
verify(int[] bitmap, int offset, int stride, int width, int height)42     public boolean verify(int[] bitmap, int offset, int stride, int width, int height) {
43         assertFalse(mRegionVerifiers.isEmpty());
44         boolean isVerified = true;
45         for (SubRegionVerifiers subRegionVerifier : mRegionVerifiers) {
46             if (subRegionVerifier.region.isRect()) {
47                 Rect area = subRegionVerifier.region.getBounds();
48                 isVerified &= verifySubRect(bitmap, offset, stride, width, height,
49                         subRegionVerifier.verifier, area);
50             } else {
51                 RegionIterator iter = new RegionIterator(subRegionVerifier.region);
52                 Rect area = new Rect();
53                 while (iter.next(area)) {
54                     isVerified &= verifySubRect(bitmap, offset, stride, width, height,
55                             subRegionVerifier.verifier, area);
56                 }
57             }
58         }
59         return isVerified;
60     }
61 
verifySubRect(int[] bitmap, int offset, int stride, int width, int height, BitmapVerifier subVerifier, Rect rect)62     private boolean verifySubRect(int[] bitmap, int offset, int stride, int width, int height,
63             BitmapVerifier subVerifier, Rect rect) {
64         final int newOffset = rect.top * stride + rect.left + offset;
65         return subVerifier.verify(bitmap, newOffset, stride, rect.width(), rect.height());
66     }
67 
addVerifier(Rect area, BitmapVerifier verifier)68     public RegionVerifier addVerifier(Rect area, BitmapVerifier verifier) {
69         return addVerifier(new Region(area), verifier);
70     }
71 
addVerifier(Region area, BitmapVerifier verifier)72     public RegionVerifier addVerifier(Region area, BitmapVerifier verifier) {
73         mRegionVerifiers.add(new SubRegionVerifiers(area, verifier));
74         return this;
75     }
76 }
77