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