1 /*
2  * Copyright (C) 2023 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 #pragma once
18 
19 #include <android-base/unique_fd.h>
20 #include <cutils/native_handle.h>
21 
22 #include <aidl/android/hardware/camera/device/StreamBuffer.h>
23 #include <aidl/android/hardware/common/NativeHandle.h>
24 
25 namespace android {
26 namespace hardware {
27 namespace camera {
28 namespace provider {
29 namespace implementation {
30 
31 using aidl::android::hardware::common::NativeHandle;
32 using aidl::android::hardware::camera::device::StreamBuffer;
33 
34 struct CachedStreamBuffer {
35     CachedStreamBuffer(const StreamBuffer& sb);
36     CachedStreamBuffer(CachedStreamBuffer&&) noexcept;
37     ~CachedStreamBuffer();
38 
getStreamIdCachedStreamBuffer39     int32_t getStreamId() const { return mStreamId; }
getBufferIdCachedStreamBuffer40     int64_t getBufferId() const { return mBufferId; }
getBufferCachedStreamBuffer41     const native_handle_t* getBuffer() const { return mBuffer; }
42 
43     void importAcquireFence(const NativeHandle& fence);
44     bool waitAcquireFence(unsigned timeoutMs);
45 
46     // this methods are used by cameras to save on lookups by `getStreamId()`
setStreamInfoCachedStreamBuffer47     void setStreamInfo(const void* ptr) { mStreamInfoPtr = ptr; }
getStreamInfoCachedStreamBuffer48     template <class T> const T* getStreamInfo() const {
49         return static_cast<const T*>(mStreamInfoPtr);
50     }
51 
52     StreamBuffer finish(bool success);
53 
54 private:
55     const native_handle_t* mBuffer;  // owned by this class
56     const void* mStreamInfoPtr = nullptr;
57     int64_t mBufferId;
58     base::unique_fd mAcquireFence;
59     int32_t mStreamId;
60     bool mProcessed = false;
61 
62     CachedStreamBuffer(const CachedStreamBuffer&) = delete;
63     CachedStreamBuffer& operator=(const CachedStreamBuffer&) = delete;
64     CachedStreamBuffer& operator=(CachedStreamBuffer&&) = delete;
65 };
66 
67 }  // namespace implementation
68 }  // namespace provider
69 }  // namespace camera
70 }  // namespace hardware
71 }  // namespace android
72