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 com.android.server.display;
18 
19 import android.Manifest;
20 import android.annotation.NonNull;
21 import android.annotation.RequiresPermission;
22 import android.os.IBinder;
23 import android.view.Display;
24 
25 import java.util.Objects;
26 
27 /**
28  * Calls into SurfaceFlinger for Display creation and deletion.
29  */
30 public class DisplayControl {
nativeCreateVirtualDisplay(String name, boolean secure, String uniqueId, float requestedRefreshRate)31     private static native IBinder nativeCreateVirtualDisplay(String name, boolean secure,
32             String uniqueId, float requestedRefreshRate);
nativeDestroyVirtualDisplay(IBinder displayToken)33     private static native void nativeDestroyVirtualDisplay(IBinder displayToken);
nativeOverrideHdrTypes(IBinder displayToken, int[] modes)34     private static native void nativeOverrideHdrTypes(IBinder displayToken, int[] modes);
nativeGetPhysicalDisplayIds()35     private static native long[] nativeGetPhysicalDisplayIds();
nativeGetPhysicalDisplayToken(long physicalDisplayId)36     private static native IBinder nativeGetPhysicalDisplayToken(long physicalDisplayId);
nativeSetHdrConversionMode(int conversionMode, int preferredHdrOutputType, int[] autoHdrTypes, int autoHdrTypesLength)37     private static native int nativeSetHdrConversionMode(int conversionMode,
38             int preferredHdrOutputType, int[] autoHdrTypes, int autoHdrTypesLength);
nativeGetSupportedHdrOutputTypes()39     private static native int[] nativeGetSupportedHdrOutputTypes();
nativeGetHdrOutputTypesWithLatency()40     private static native int[] nativeGetHdrOutputTypesWithLatency();
nativeGetHdrOutputConversionSupport()41     private static native boolean nativeGetHdrOutputConversionSupport();
42 
43     /**
44      * Create a virtual display in SurfaceFlinger.
45      *
46      * @param name The name of the virtual display.
47      * @param secure Whether this display is secure.
48      * @return The token reference for the display in SurfaceFlinger.
49      */
createVirtualDisplay(String name, boolean secure)50     public static IBinder createVirtualDisplay(String name, boolean secure) {
51         Objects.requireNonNull(name, "name must not be null");
52         return nativeCreateVirtualDisplay(name, secure, "", 0.0f);
53     }
54 
55     /**
56      * Create a virtual display in SurfaceFlinger.
57      *
58      * @param name The name of the virtual display.
59      * @param secure Whether this display is secure.
60      * @param uniqueId The unique ID for the display.
61      * @param requestedRefreshRate The requested refresh rate in frames per second.
62      * For best results, specify a divisor of the physical refresh rate, e.g., 30 or 60 on
63      * 120hz display. If an arbitrary refresh rate is specified, the rate will be rounded
64      * up or down to a divisor of the physical display. If 0 is specified, the virtual
65      * display is refreshed at the physical display refresh rate.
66      * @return The token reference for the display in SurfaceFlinger.
67      */
createVirtualDisplay(String name, boolean secure, String uniqueId, float requestedRefreshRate)68     public static IBinder createVirtualDisplay(String name, boolean secure,
69             String uniqueId, float requestedRefreshRate) {
70         Objects.requireNonNull(name, "name must not be null");
71         Objects.requireNonNull(uniqueId, "uniqueId must not be null");
72         return nativeCreateVirtualDisplay(name, secure, uniqueId, requestedRefreshRate);
73     }
74 
75     /**
76      * Destroy a virtual display in SurfaceFlinger.
77      *
78      * @param displayToken The display token for the virtual display to be destroyed.
79      */
destroyVirtualDisplay(IBinder displayToken)80     public static void destroyVirtualDisplay(IBinder displayToken) {
81         if (displayToken == null) {
82             throw new IllegalArgumentException("displayToken must not be null");
83         }
84         nativeDestroyVirtualDisplay(displayToken);
85     }
86 
87     /**
88      * Overrides HDR modes for a display device.
89      */
90     @RequiresPermission(Manifest.permission.ACCESS_SURFACE_FLINGER)
overrideHdrTypes(@onNull IBinder displayToken, @NonNull int[] modes)91     public static void overrideHdrTypes(@NonNull IBinder displayToken, @NonNull int[] modes) {
92         nativeOverrideHdrTypes(displayToken, modes);
93     }
94 
95     /**
96      * Gets all the physical display ids.
97      */
getPhysicalDisplayIds()98     public static long[] getPhysicalDisplayIds() {
99         return nativeGetPhysicalDisplayIds();
100     }
101 
102     /**
103      * Gets the display's token from the physical display id
104      */
getPhysicalDisplayToken(long physicalDisplayId)105     public static IBinder getPhysicalDisplayToken(long physicalDisplayId) {
106         return nativeGetPhysicalDisplayToken(physicalDisplayId);
107     }
108 
109     /**
110      * Sets the HDR conversion mode for the device.
111      *
112      * Returns the system preferred Hdr output type nn case when HDR conversion mode is
113      * {@link android.hardware.display.HdrConversionMode#HDR_CONVERSION_SYSTEM}.
114      * Returns Hdr::INVALID in other cases.
115      * @hide
116      */
setHdrConversionMode(int conversionMode, int preferredHdrOutputType, int[] autoHdrTypes)117     public static int setHdrConversionMode(int conversionMode, int preferredHdrOutputType,
118             int[] autoHdrTypes) {
119         int length = autoHdrTypes != null ? autoHdrTypes.length : 0;
120         return nativeSetHdrConversionMode(
121                 conversionMode, preferredHdrOutputType, autoHdrTypes, length);
122     }
123 
124     /**
125      * Returns the HDR output types supported by the device.
126      * @hide
127      */
getSupportedHdrOutputTypes()128     public static @Display.HdrCapabilities.HdrType int[] getSupportedHdrOutputTypes() {
129         return nativeGetSupportedHdrOutputTypes();
130     }
131 
132     /**
133      * Returns the HDR output types which introduces latency on conversion to them.
134      * @hide
135      */
getHdrOutputTypesWithLatency()136     public static @Display.HdrCapabilities.HdrType int[] getHdrOutputTypesWithLatency() {
137         return nativeGetHdrOutputTypesWithLatency();
138     }
139 
140     /**
141      * Returns whether the HDR output conversion is supported by the device.
142      * @hide
143      */
getHdrOutputConversionSupport()144     public static boolean getHdrOutputConversionSupport() {
145         return nativeGetHdrOutputConversionSupport();
146     }
147 }
148