• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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.car.test.util;
18 
19 import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY;
20 import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC;
21 import static android.view.Display.DEFAULT_DISPLAY;
22 
23 import android.content.Context;
24 import android.graphics.PixelFormat;
25 import android.hardware.display.DisplayManager;
26 import android.hardware.display.VirtualDisplay;
27 import android.media.ImageReader;
28 import android.os.Looper;
29 import android.util.DisplayMetrics;
30 import android.view.Display;
31 
32 import com.android.compatibility.common.util.TestUtils;
33 
34 /**
35  * Utilities needed when interacting with the display
36  */
37 public class DisplayUtils {
38     private static final int DISPLAY_ADDED_TIMEOUT_MS = 10_000;
39     private static final String DISPLAY_NAME = "VirtualDisplayForCarTestLib";
40 
41     public static class VirtualDisplaySession implements AutoCloseable {
42         private VirtualDisplay mVirtualDisplay;
43         private ImageReader mReader;
44 
45         /**
46          * Creates a virtual display having same size with default display and waits until it's
47          * in display list. The density of the virtual display is based on
48          * {@link DisplayMetrics#xdpi} so that the threshold of gesture detection is same as
49          * the default display's.
50          *
51          * @param context
52          * @param isPrivate if this display is a private display.
53          * @return virtual display.
54          *
55          * @throws IllegalStateException if called from main thread.
56          */
createDisplayWithDefaultDisplayMetricsAndWait(Context context, boolean isPrivate)57         public Display createDisplayWithDefaultDisplayMetricsAndWait(Context context,
58                 boolean isPrivate) {
59             if (Looper.myLooper() == Looper.getMainLooper()) {
60                 throw new IllegalStateException("Should not call from main thread");
61             }
62 
63             if (mReader != null) {
64                 throw new IllegalStateException(
65                         "Only one display can be created during this session.");
66             }
67 
68             DisplayManager displayManager = context.getSystemService(DisplayManager.class);
69             DisplayMetrics metrics = new DisplayMetrics();
70             displayManager.getDisplay(DEFAULT_DISPLAY).getRealMetrics(metrics);
71 
72             mReader = ImageReader.newInstance(metrics.widthPixels, metrics.heightPixels,
73                     PixelFormat.RGBA_8888, 1 /* maxImages */);
74 
75             Object waitObject = new Object();
76             DisplayManager.DisplayListener listener = new DisplayManager.DisplayListener() {
77                 @Override
78                 public void onDisplayAdded(int i) {
79                     synchronized (waitObject) {
80                         waitObject.notifyAll();
81                     }
82                 }
83 
84                 @Override
85                 public void onDisplayRemoved(int i) {
86                 }
87 
88                 @Override
89                 public void onDisplayChanged(int i) {
90                 }
91             };
92 
93             displayManager.registerDisplayListener(listener, null);
94 
95             int flags = isPrivate ? 0
96                     : (VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY | VIRTUAL_DISPLAY_FLAG_PUBLIC);
97 
98             mVirtualDisplay = displayManager.createVirtualDisplay(DISPLAY_NAME,
99                     metrics.widthPixels, metrics.heightPixels, (int) metrics.xdpi,
100                     mReader.getSurface(), flags);
101 
102             try {
103                 int theNewDisplayId = mVirtualDisplay.getDisplay().getDisplayId();
104                 TestUtils.waitOn(waitObject,
105                         () -> displayManager.getDisplay(theNewDisplayId) != null,
106                         DISPLAY_ADDED_TIMEOUT_MS,
107                         String.format("wait for virtual display %d adding", theNewDisplayId));
108             } finally {
109                 displayManager.unregisterDisplayListener(listener);
110             }
111 
112             return mVirtualDisplay.getDisplay();
113         }
114 
115         @Override
close()116         public void close() {
117             if (mVirtualDisplay != null) {
118                 mVirtualDisplay.release();
119             }
120             if (mReader != null) {
121                 mReader.close();
122             }
123         }
124     }
125 }
126