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 android.annotation.NonNull; 20 import android.annotation.SystemApi; 21 import android.car.Car; 22 import android.car.annotation.RequiredFeature; 23 import android.hardware.HardwareBuffer; 24 import android.os.Parcel; 25 import android.os.Parcelable; 26 27 import java.util.Objects; 28 29 /** 30 * Wraps around {@link android.hardware.HardwareBuffer} to embed additional metadata of the buffer 31 * from the Extended View System service. 32 * 33 * @hide 34 */ 35 @SystemApi 36 @RequiredFeature(Car.CAR_EVS_SERVICE) 37 public final class CarEvsBufferDescriptor implements Parcelable { 38 public static final @NonNull Parcelable.Creator<CarEvsBufferDescriptor> CREATOR = 39 new Parcelable.Creator<CarEvsBufferDescriptor>() { 40 @NonNull 41 @Override 42 public CarEvsBufferDescriptor createFromParcel(final Parcel in) { 43 return new CarEvsBufferDescriptor(in); 44 } 45 46 @NonNull 47 @Override 48 public CarEvsBufferDescriptor[] newArray(final int size) { 49 return new CarEvsBufferDescriptor[size]; 50 } 51 }; 52 53 private final int mId; 54 55 @NonNull 56 private final HardwareBuffer mHardwareBuffer; 57 58 /** 59 * Creates a {@link CarEvsBufferDescriptor} given an unique identifier and 60 * {@link android.hardware.HardwareBuffer}. 61 * 62 * @param id A 32-bit integer to uniquely identify associated hardware buffer. 63 * @param buffer Hardware buffer that contains the imagery data from EVS service. 64 */ CarEvsBufferDescriptor(int id, @NonNull final HardwareBuffer buffer)65 public CarEvsBufferDescriptor(int id, @NonNull final HardwareBuffer buffer) { 66 Objects.requireNonNull(buffer, "HardwardBuffer cannot be null."); 67 68 mId = id; 69 mHardwareBuffer = buffer; 70 } 71 72 /** Parcelable methods */ CarEvsBufferDescriptor(final Parcel in)73 private CarEvsBufferDescriptor(final Parcel in) { 74 mId = in.readInt(); 75 mHardwareBuffer = Objects.requireNonNull(HardwareBuffer.CREATOR.createFromParcel(in)); 76 } 77 78 @Override describeContents()79 public int describeContents() { 80 return 0; 81 } 82 83 @Override writeToParcel(@onNull final Parcel dest, final int flags)84 public void writeToParcel(@NonNull final Parcel dest, final int flags) { 85 dest.writeInt(mId); 86 mHardwareBuffer.writeToParcel(dest, flags); 87 } 88 89 @Override toString()90 public String toString() { 91 return "CarEvsBufferDescriptor: id = " + mId + ", buffer = " + mHardwareBuffer; 92 } 93 94 /** 95 * Returns an unique identifier of the hardware buffer described by this object. 96 * 97 * @return A 32-bit signed integer unique buffer identifier. 98 */ getId()99 public int getId() { 100 return mId; 101 } 102 103 /** 104 * Returns a reference to {@link android.hardware.HardwareBuffer} registered 105 * to this descriptor. 106 * 107 * @return the registered {@link android.hardware.HardwareBuffer}. 108 */ 109 @NonNull getHardwareBuffer()110 public HardwareBuffer getHardwareBuffer() { 111 return mHardwareBuffer; 112 } 113 } 114