1 /* 2 * Copyright (C) 2021 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.evs; 18 19 import static com.android.car.internal.ExcludeFromCodeCoverageGeneratedReport.BOILERPLATE_CODE; 20 import static com.android.car.internal.ExcludeFromCodeCoverageGeneratedReport.DUMP_INFO; 21 22 import android.annotation.NonNull; 23 import android.annotation.SuppressLint; 24 import android.annotation.SystemApi; 25 import android.car.Car; 26 import android.car.annotation.AddedInOrBefore; 27 import android.car.annotation.ApiRequirements; 28 import android.car.annotation.RequiredFeature; 29 import android.hardware.HardwareBuffer; 30 import android.os.Parcel; 31 import android.os.Parcelable; 32 33 import com.android.car.internal.ExcludeFromCodeCoverageGeneratedReport; 34 35 import java.util.Objects; 36 37 /** 38 * Wraps around {@link android.hardware.HardwareBuffer} to embed additional metadata of the buffer 39 * from the Extended View System service. 40 * 41 * @hide 42 */ 43 @SystemApi 44 @RequiredFeature(Car.CAR_EVS_SERVICE) 45 public final class CarEvsBufferDescriptor implements Parcelable, AutoCloseable { 46 @AddedInOrBefore(majorVersion = 33) 47 public static final @NonNull Parcelable.Creator<CarEvsBufferDescriptor> CREATOR = 48 new Parcelable.Creator<CarEvsBufferDescriptor>() { 49 @NonNull 50 @Override 51 public CarEvsBufferDescriptor createFromParcel(final Parcel in) { 52 return new CarEvsBufferDescriptor(in); 53 } 54 55 @NonNull 56 @Override 57 public CarEvsBufferDescriptor[] newArray(final int size) { 58 return new CarEvsBufferDescriptor[size]; 59 } 60 }; 61 62 private final int mId; 63 64 @NonNull 65 private final HardwareBuffer mHardwareBuffer; 66 67 /** 68 * Creates a {@link CarEvsBufferDescriptor} given an unique identifier and 69 * {@link android.hardware.HardwareBuffer}. 70 * 71 * @param id A 32-bit integer to uniquely identify associated hardware buffer. 72 * @param buffer Hardware buffer that contains the imagery data from EVS service. 73 */ CarEvsBufferDescriptor(int id, @NonNull final HardwareBuffer buffer)74 public CarEvsBufferDescriptor(int id, @NonNull final HardwareBuffer buffer) { 75 Objects.requireNonNull(buffer, "HardwardBuffer cannot be null."); 76 77 mId = id; 78 mHardwareBuffer = buffer; 79 } 80 81 /** Parcelable methods */ CarEvsBufferDescriptor(final Parcel in)82 private CarEvsBufferDescriptor(final Parcel in) { 83 mId = in.readInt(); 84 mHardwareBuffer = Objects.requireNonNull(HardwareBuffer.CREATOR.createFromParcel(in)); 85 } 86 87 @Override 88 @ExcludeFromCodeCoverageGeneratedReport(reason = BOILERPLATE_CODE) 89 @AddedInOrBefore(majorVersion = 33) describeContents()90 public int describeContents() { 91 return 0; 92 } 93 94 @Override 95 @AddedInOrBefore(majorVersion = 33) writeToParcel(@onNull final Parcel dest, final int flags)96 public void writeToParcel(@NonNull final Parcel dest, final int flags) { 97 dest.writeInt(mId); 98 mHardwareBuffer.writeToParcel(dest, flags); 99 } 100 101 @Override 102 @ExcludeFromCodeCoverageGeneratedReport(reason = DUMP_INFO) toString()103 public String toString() { 104 return "CarEvsBufferDescriptor: id = " + mId + ", buffer = " + mHardwareBuffer; 105 } 106 107 @Override 108 @SuppressLint("GenericException") finalize()109 protected void finalize() throws Throwable { 110 try { 111 close(); 112 } finally { 113 super.finalize(); 114 } 115 } 116 117 @Override 118 @ApiRequirements(minCarVersion = ApiRequirements.CarVersion.UPSIDE_DOWN_CAKE_0, 119 minPlatformVersion = ApiRequirements.PlatformVersion.TIRAMISU_0) close()120 public void close() { 121 if (!mHardwareBuffer.isClosed()) { 122 mHardwareBuffer.close(); 123 } 124 } 125 126 /** 127 * Returns an unique identifier of the hardware buffer described by this object. 128 * 129 * @return A 32-bit signed integer unique buffer identifier. 130 */ 131 @AddedInOrBefore(majorVersion = 33) getId()132 public int getId() { 133 return mId; 134 } 135 136 /** 137 * Returns a reference to {@link android.hardware.HardwareBuffer} registered 138 * to this descriptor. 139 * 140 * @return the registered {@link android.hardware.HardwareBuffer}. 141 */ 142 @NonNull 143 @AddedInOrBefore(majorVersion = 33) getHardwareBuffer()144 public HardwareBuffer getHardwareBuffer() { 145 return mHardwareBuffer; 146 } 147 } 148