1 /*
2  * Copyright (C) 2018 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_HARDWARE_CAMERA_DEVICE_V3_5_CAMERADEVICE3SESSION_H
18 #define ANDROID_HARDWARE_CAMERA_DEVICE_V3_5_CAMERADEVICE3SESSION_H
19 
20 #include <android/hardware/camera/device/3.5/ICameraDevice.h>
21 #include <android/hardware/camera/device/3.5/ICameraDeviceSession.h>
22 #include <android/hardware/camera/device/3.5/ICameraDeviceCallback.h>
23 #include <../../3.4/default/include/device_v3_4_impl/CameraDeviceSession.h>
24 #include <unordered_map>
25 
26 namespace android {
27 namespace hardware {
28 namespace camera {
29 namespace device {
30 namespace V3_5 {
31 namespace implementation {
32 
33 using namespace ::android::hardware::camera::device;
34 using ::android::hardware::camera::device::V3_2::BufferStatus;
35 using ::android::hardware::camera::device::V3_2::CaptureRequest;
36 using ::android::hardware::camera::device::V3_2::StreamBuffer;
37 using ::android::hardware::camera::device::V3_5::StreamConfiguration;
38 using ::android::hardware::camera::device::V3_4::HalStreamConfiguration;
39 using ::android::hardware::camera::device::V3_5::ICameraDeviceSession;
40 using ::android::hardware::camera::device::V3_5::ICameraDeviceCallback;
41 using ::android::hardware::camera::device::V3_2::implementation::Camera3Stream;
42 using ::android::hardware::camera::common::V1_0::Status;
43 using ::android::hardware::camera::common::V1_0::helper::HandleImporter;
44 using ::android::hardware::Return;
45 using ::android::hardware::Void;
46 using ::android::hardware::hidl_vec;
47 using ::android::hardware::hidl_string;
48 using ::android::sp;
49 using ::android::Mutex;
50 
51 
52 /**
53  * Function pointer types with C calling convention to
54  * use for HAL callback functions.
55  */
56 extern "C" {
57     typedef camera3_buffer_request_status_t (callbacks_request_stream_buffer_t)(
58             const struct camera3_callback_ops *,
59             uint32_t num_buffer_reqs,
60             const camera3_buffer_request_t *buffer_reqs,
61             /*out*/uint32_t *num_returned_buf_reqs,
62             /*out*/camera3_stream_buffer_ret_t *returned_buf_reqs);
63 
64     typedef void (callbacks_return_stream_buffer_t)(
65             const struct camera3_callback_ops *,
66             uint32_t num_buffers,
67             const camera3_stream_buffer_t* const* buffers);
68 }
69 
70 struct CameraDeviceSession : public V3_4::implementation::CameraDeviceSession {
71 
72     CameraDeviceSession(camera3_device_t*,
73             const camera_metadata_t* deviceInfo,
74             const sp<V3_2::ICameraDeviceCallback>&);
75     virtual ~CameraDeviceSession();
76 
getInterfaceCameraDeviceSession77     virtual sp<V3_2::ICameraDeviceSession> getInterface() override {
78         return new TrampolineSessionInterface_3_5(this);
79     }
80 
81 protected:
82     // Methods from v3.4 and earlier will trampoline to inherited implementation
83     Return<void> configureStreams_3_5(
84             const StreamConfiguration& requestedConfiguration,
85             ICameraDeviceSession::configureStreams_3_5_cb _hidl_cb);
86 
87     Return<void> signalStreamFlush(
88             const hidl_vec<int32_t>& streamIds,
89             uint32_t streamConfigCounter);
90 
91     virtual Status importRequest(
92             const CaptureRequest& request,
93             hidl_vec<buffer_handle_t*>& allBufPtrs,
94             hidl_vec<int>& allFences) override;
95 
96     Return<void> isReconfigurationRequired(const V3_2::CameraMetadata& oldSessionParams,
97             const V3_2::CameraMetadata& newSessionParams,
98             ICameraDeviceSession::isReconfigurationRequired_cb _hidl_cb);
99     /**
100      * Static callback forwarding methods from HAL to instance
101      */
102     static callbacks_request_stream_buffer_t sRequestStreamBuffers;
103     static callbacks_return_stream_buffer_t sReturnStreamBuffers;
104 
105     camera3_buffer_request_status_t requestStreamBuffers(
106             uint32_t num_buffer_reqs,
107             const camera3_buffer_request_t *buffer_reqs,
108             /*out*/uint32_t *num_returned_buf_reqs,
109             /*out*/camera3_stream_buffer_ret_t *returned_buf_reqs);
110 
111     void returnStreamBuffers(
112             uint32_t num_buffers,
113             const camera3_stream_buffer_t* const* buffers);
114 
115     struct BufferHasher {
operatorCameraDeviceSession::BufferHasher116         size_t operator()(const buffer_handle_t& buf) const {
117             if (buf == nullptr)
118                 return 0;
119 
120             size_t result = 1;
121             result = 31 * result + buf->numFds;
122             for (int i = 0; i < buf->numFds; i++) {
123                 result = 31 * result + buf->data[i];
124             }
125             return result;
126         }
127     };
128 
129     struct BufferComparator {
operatorCameraDeviceSession::BufferComparator130         bool operator()(const buffer_handle_t& buf1, const buffer_handle_t& buf2) const {
131             if (buf1->numFds == buf2->numFds) {
132                 for (int i = 0; i < buf1->numFds; i++) {
133                     if (buf1->data[i] != buf2->data[i]) {
134                         return false;
135                     }
136                 }
137                 return true;
138             }
139             return false;
140         }
141     };
142 
143     Camera3Stream* getStreamPointer(int32_t streamId);
144 
145     // Register buffer to mBufferIdMaps so we can find corresponding bufferId
146     // when the buffer is returned to camera service
147     void pushBufferId(const buffer_handle_t& buf, uint64_t bufferId, int streamId);
148 
149     // Method to pop buffer's bufferId from mBufferIdMaps
150     // BUFFER_ID_NO_BUFFER is returned if no matching buffer is found
151     uint64_t popBufferId(const buffer_handle_t& buf, int streamId);
152 
153     // Method to cleanup imported buffer/fences if requestStreamBuffers fails half way
154     void cleanupInflightBufferFences(
155             std::vector<int>& fences, std::vector<std::pair<buffer_handle_t, int>>& bufs);
156 
157     // Overrides the default constructCaptureResult behavior for buffer management APIs
158     virtual uint64_t getCapResultBufferId(const buffer_handle_t& buf, int streamId) override;
159 
160     std::mutex mBufferIdMapLock; // protecting mBufferIdMaps and mNextBufferId
161     typedef std::unordered_map<const buffer_handle_t, uint64_t,
162             BufferHasher, BufferComparator> BufferIdMap;
163     // stream ID -> per stream buffer ID map for buffers coming from requestStreamBuffers API
164     // Entries are created during requestStreamBuffers when a stream first request a buffer, and
165     // deleted in returnStreamBuffers/processCaptureResult* when all buffers are returned
166     std::unordered_map<int, BufferIdMap> mBufferIdMaps;
167 
168     sp<ICameraDeviceCallback> mCallback_3_5;
169     bool mSupportBufMgr;
170 
171 private:
172 
173     struct TrampolineSessionInterface_3_5 : public ICameraDeviceSession {
TrampolineSessionInterface_3_5CameraDeviceSession::TrampolineSessionInterface_3_5174         TrampolineSessionInterface_3_5(sp<CameraDeviceSession> parent) :
175                 mParent(parent) {}
176 
constructDefaultRequestSettingsCameraDeviceSession::TrampolineSessionInterface_3_5177         virtual Return<void> constructDefaultRequestSettings(
178                 V3_2::RequestTemplate type,
179                 V3_3::ICameraDeviceSession::constructDefaultRequestSettings_cb _hidl_cb) override {
180             return mParent->constructDefaultRequestSettings(type, _hidl_cb);
181         }
182 
configureStreamsCameraDeviceSession::TrampolineSessionInterface_3_5183         virtual Return<void> configureStreams(
184                 const V3_2::StreamConfiguration& requestedConfiguration,
185                 V3_3::ICameraDeviceSession::configureStreams_cb _hidl_cb) override {
186             return mParent->configureStreams(requestedConfiguration, _hidl_cb);
187         }
188 
processCaptureRequest_3_4CameraDeviceSession::TrampolineSessionInterface_3_5189         virtual Return<void> processCaptureRequest_3_4(
190                 const hidl_vec<V3_4::CaptureRequest>& requests,
191                 const hidl_vec<V3_2::BufferCache>& cachesToRemove,
192                 ICameraDeviceSession::processCaptureRequest_3_4_cb _hidl_cb) override {
193             return mParent->processCaptureRequest_3_4(requests, cachesToRemove, _hidl_cb);
194         }
195 
processCaptureRequestCameraDeviceSession::TrampolineSessionInterface_3_5196         virtual Return<void> processCaptureRequest(const hidl_vec<V3_2::CaptureRequest>& requests,
197                 const hidl_vec<V3_2::BufferCache>& cachesToRemove,
198                 V3_3::ICameraDeviceSession::processCaptureRequest_cb _hidl_cb) override {
199             return mParent->processCaptureRequest(requests, cachesToRemove, _hidl_cb);
200         }
201 
getCaptureRequestMetadataQueueCameraDeviceSession::TrampolineSessionInterface_3_5202         virtual Return<void> getCaptureRequestMetadataQueue(
203                 V3_3::ICameraDeviceSession::getCaptureRequestMetadataQueue_cb _hidl_cb) override  {
204             return mParent->getCaptureRequestMetadataQueue(_hidl_cb);
205         }
206 
getCaptureResultMetadataQueueCameraDeviceSession::TrampolineSessionInterface_3_5207         virtual Return<void> getCaptureResultMetadataQueue(
208                 V3_3::ICameraDeviceSession::getCaptureResultMetadataQueue_cb _hidl_cb) override  {
209             return mParent->getCaptureResultMetadataQueue(_hidl_cb);
210         }
211 
flushCameraDeviceSession::TrampolineSessionInterface_3_5212         virtual Return<Status> flush() override {
213             return mParent->flush();
214         }
215 
closeCameraDeviceSession::TrampolineSessionInterface_3_5216         virtual Return<void> close() override {
217             return mParent->close();
218         }
219 
configureStreams_3_3CameraDeviceSession::TrampolineSessionInterface_3_5220         virtual Return<void> configureStreams_3_3(
221                 const V3_2::StreamConfiguration& requestedConfiguration,
222                 configureStreams_3_3_cb _hidl_cb) override {
223             return mParent->configureStreams_3_3(requestedConfiguration, _hidl_cb);
224         }
225 
configureStreams_3_4CameraDeviceSession::TrampolineSessionInterface_3_5226         virtual Return<void> configureStreams_3_4(
227                 const V3_4::StreamConfiguration& requestedConfiguration,
228                 configureStreams_3_4_cb _hidl_cb) override {
229             return mParent->configureStreams_3_4(requestedConfiguration, _hidl_cb);
230         }
231 
configureStreams_3_5CameraDeviceSession::TrampolineSessionInterface_3_5232         virtual Return<void> configureStreams_3_5(
233                 const StreamConfiguration& requestedConfiguration,
234                 configureStreams_3_5_cb _hidl_cb) override {
235             return mParent->configureStreams_3_5(requestedConfiguration, _hidl_cb);
236         }
237 
signalStreamFlushCameraDeviceSession::TrampolineSessionInterface_3_5238         virtual Return<void> signalStreamFlush(
239                 const hidl_vec<int32_t>& requests,
240                 uint32_t streamConfigCounter) override {
241             return mParent->signalStreamFlush(requests, streamConfigCounter);
242         }
243 
isReconfigurationRequiredCameraDeviceSession::TrampolineSessionInterface_3_5244         virtual Return<void> isReconfigurationRequired(const V3_2::CameraMetadata& oldSessionParams,
245                 const V3_2::CameraMetadata& newSessionParams,
246                 ICameraDeviceSession::isReconfigurationRequired_cb _hidl_cb) override {
247             return mParent->isReconfigurationRequired(oldSessionParams, newSessionParams, _hidl_cb);
248         }
249     private:
250         sp<CameraDeviceSession> mParent;
251     };
252 };
253 
254 }  // namespace implementation
255 }  // namespace V3_5
256 }  // namespace device
257 }  // namespace camera
258 }  // namespace hardware
259 }  // namespace android
260 
261 #endif  // ANDROID_HARDWARE_CAMERA_DEVICE_V3_5_CAMERADEVICE3SESSION_H
262