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 #ifndef ANDROID_SERVERS_AIDL_CAMERA3_OUTPUT_UTILS_H
18 #define ANDROID_SERVERS_AIDL_CAMERA3_OUTPUT_UTILS_H
19 
20 #include <memory>
21 #include <mutex>
22 
23 #include <cutils/native_handle.h>
24 
25 #include <aidlcommonsupport/NativeHandle.h>
26 #include <fmq/AidlMessageQueue.h>
27 
28 #include <common/CameraDeviceBase.h>
29 
30 #include <aidl/android/hardware/camera/device/ICameraDevice.h>
31 #include <aidl/android/hardware/camera/device/ICameraDeviceCallback.h>
32 #include "device3/BufferUtils.h"
33 #include "device3/InFlightRequest.h"
34 #include "device3/Camera3Stream.h"
35 #include "device3/Camera3OutputStreamInterface.h"
36 #include "device3/Camera3OutputUtils.h"
37 #include "utils/SessionStatsBuilder.h"
38 #include "utils/TagMonitor.h"
39 
40 namespace android {
41 
42 using ::aidl::android::hardware::common::fmq::SynchronizedReadWrite;
43 using ::android::AidlMessageQueue;
44 
45 using AidlResultMetadataQueue = AidlMessageQueue<int8_t, SynchronizedReadWrite>;
46 namespace camera3 {
dupToAidlIfNotNull(const native_handle_t * nh)47     inline aidl::android::hardware::common::NativeHandle dupToAidlIfNotNull(
48             const native_handle_t *nh) {
49         if (nh == nullptr) {
50             return aidl::android::hardware::common::NativeHandle();
51         }
52         return dupToAidl(nh);
53     }
54 
makeToAidlIfNotNull(const native_handle_t * nh)55     inline aidl::android::hardware::common::NativeHandle makeToAidlIfNotNull(
56             const native_handle_t *nh) {
57         if (nh == nullptr) {
58             return aidl::android::hardware::common::NativeHandle();
59         }
60         return makeToAidl(nh);
61     }
62 
63     /**
64      * Helper methods shared between AidlCamera3Device/AidlCamera3OfflineSession for HAL callbacks
65      */
66 
67     // Camera3Device/Camera3OfflineSession internal states used in notify/processCaptureResult
68     // callbacks
69     struct AidlCaptureOutputStates : public CaptureOutputStates {
70         std::unique_ptr<AidlResultMetadataQueue>& fmq;
71     };
72 
73     // Handle one capture result. Assume callers hold the lock to serialize all
74     // processCaptureResult calls
75     void processOneCaptureResultLocked(
76             AidlCaptureOutputStates& states,
77             const aidl::android::hardware::camera::device::CaptureResult& result,
78             const std::vector<aidl::android::hardware::camera::device::PhysicalCameraMetadata>
79                     &physicalCameraMetadata);
80 
81     void notify(CaptureOutputStates& states,
82             const aidl::android::hardware::camera::device::NotifyMsg& msg,
83             bool hasReadoutTimestamp);
84 
85     void requestStreamBuffers(RequestBufferStates& states,
86         const std::vector<aidl::android::hardware::camera::device::BufferRequest>& bufReqs,
87         std::vector<::aidl::android::hardware::camera::device::StreamBufferRet>* out_buffers,
88         ::aidl::android::hardware::camera::device::BufferRequestStatus* _aidl_return);
89 
90     void returnStreamBuffers(ReturnBufferStates& states,
91         const std::vector<aidl::android::hardware::camera::device::StreamBuffer>& buffers);
92 
93 } // namespace camera3
94 
95 } // namespace android
96 
97 #endif
98