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