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.hardware; 18 19 import android.annotation.FlaggedApi; 20 import android.annotation.NonNull; 21 import android.annotation.SuppressLint; 22 import android.hardware.flags.Flags; 23 import android.os.Parcel; 24 import android.os.Parcelable; 25 26 import libcore.util.NativeAllocationRegistry; 27 28 /** 29 * Provides supported overlay properties of the device. 30 * 31 * <p> 32 * Hardware overlay is a technique to composite different buffers directly 33 * to the screen using display hardware rather than the GPU. 34 * The system compositor is able to assign any content managed by a 35 * {@link android.view.SurfaceControl} onto a hardware overlay if possible. 36 * Applications may be interested in the display hardware capabilities exposed 37 * by this class as a hint to determine if their {@link android.view.SurfaceControl} 38 * tree is power-efficient and performant. 39 * </p> 40 */ 41 @FlaggedApi(Flags.FLAG_OVERLAYPROPERTIES_CLASS_API) 42 public final class OverlayProperties implements Parcelable { 43 44 private static final NativeAllocationRegistry sRegistry = 45 NativeAllocationRegistry.createMalloced(OverlayProperties.class.getClassLoader(), 46 nGetDestructor()); 47 48 private long mNativeObject; 49 // only for virtual displays 50 private static OverlayProperties sDefaultOverlayProperties; 51 // Invoked on destruction 52 private Runnable mCloser; 53 54 private LutProperties[] mLutProperties; 55 OverlayProperties(long nativeObject)56 private OverlayProperties(long nativeObject) { 57 if (nativeObject != 0) { 58 mCloser = sRegistry.registerNativeAllocation(this, nativeObject); 59 } 60 mNativeObject = nativeObject; 61 } 62 63 /** 64 * For virtual displays, we provide an overlay properties object 65 * with RGBA 8888 only, sRGB only, true for mixed color spaces. 66 * @hide 67 */ getDefault()68 public static OverlayProperties getDefault() { 69 if (sDefaultOverlayProperties == null) { 70 sDefaultOverlayProperties = new OverlayProperties(nCreateDefault()); 71 } 72 return sDefaultOverlayProperties; 73 } 74 75 /** 76 * Returns the lut properties of the device. 77 */ 78 @FlaggedApi(Flags.FLAG_LUTS_API) 79 @SuppressLint("ArrayReturn") 80 @NonNull getLutProperties()81 public LutProperties[] getLutProperties() { 82 if (mNativeObject == 0) { 83 return null; 84 } 85 if (mLutProperties == null) { 86 mLutProperties = nGetLutProperties(mNativeObject); 87 } 88 return mLutProperties; 89 } 90 91 /** 92 * Indicates that hardware composition of a buffer encoded with the provided {@link DataSpace} 93 * and {@link HardwareBuffer.Format} is supported on the device. 94 * 95 * @return True if the device can support efficiently compositing the content described by the 96 * dataspace and format. False if GPU composition fallback is otherwise required. 97 */ 98 @FlaggedApi(Flags.FLAG_OVERLAYPROPERTIES_CLASS_API) isCombinationSupported(@ataSpace.ColorDataSpace int dataspace, @HardwareBuffer.Format int format)99 public boolean isCombinationSupported(@DataSpace.ColorDataSpace int dataspace, 100 @HardwareBuffer.Format int format) { 101 if (mNativeObject == 0) { 102 return false; 103 } 104 105 return nIsCombinationSupported(mNativeObject, dataspace, format); 106 } 107 108 /** 109 * Indicates that hardware composition of two or more overlays 110 * with different colorspaces is supported on the device. 111 * 112 * @return True if the device can support mixed colorspaces efficiently, 113 * false if GPU composition fallback is otherwise required. 114 */ 115 @FlaggedApi(Flags.FLAG_OVERLAYPROPERTIES_CLASS_API) isMixedColorSpacesSupported()116 public boolean isMixedColorSpacesSupported() { 117 if (mNativeObject == 0) { 118 return false; 119 } 120 return nSupportMixedColorSpaces(mNativeObject); 121 } 122 123 @FlaggedApi(Flags.FLAG_OVERLAYPROPERTIES_CLASS_API) 124 @Override describeContents()125 public int describeContents() { 126 return 0; 127 } 128 129 @FlaggedApi(Flags.FLAG_OVERLAYPROPERTIES_CLASS_API) 130 @Override writeToParcel(@onNull Parcel dest, int flags)131 public void writeToParcel(@NonNull Parcel dest, int flags) { 132 if (mNativeObject == 0) { 133 dest.writeInt(0); 134 return; 135 } 136 dest.writeInt(1); 137 nWriteOverlayPropertiesToParcel(mNativeObject, dest); 138 } 139 140 @FlaggedApi(Flags.FLAG_OVERLAYPROPERTIES_CLASS_API) 141 public static final @NonNull Parcelable.Creator<OverlayProperties> CREATOR = 142 new Parcelable.Creator<OverlayProperties>() { 143 public OverlayProperties createFromParcel(Parcel in) { 144 if (in.readInt() != 0) { 145 return new OverlayProperties(nReadOverlayPropertiesFromParcel(in)); 146 } 147 return null; 148 } 149 150 public OverlayProperties[] newArray(int size) { 151 return new OverlayProperties[size]; 152 } 153 }; 154 nGetDestructor()155 private static native long nGetDestructor(); nCreateDefault()156 private static native long nCreateDefault(); nSupportMixedColorSpaces(long nativeObject)157 private static native boolean nSupportMixedColorSpaces(long nativeObject); nIsCombinationSupported( long nativeObject, int dataspace, int format)158 private static native boolean nIsCombinationSupported( 159 long nativeObject, int dataspace, int format); nWriteOverlayPropertiesToParcel(long nativeObject, Parcel dest)160 private static native void nWriteOverlayPropertiesToParcel(long nativeObject, Parcel dest); nReadOverlayPropertiesFromParcel(Parcel in)161 private static native long nReadOverlayPropertiesFromParcel(Parcel in); nGetLutProperties(long nativeObject)162 private static native LutProperties[] nGetLutProperties(long nativeObject); 163 }