blob: f59f503b9a254c81922ec28bbfe247530030024b [file] [log] [blame]
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -07001/*
2 * Copyright (C) 2016 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_2_CAMERADEVICE3SESSION_H
18#define ANDROID_HARDWARE_CAMERA_DEVICE_V3_2_CAMERADEVICE3SESSION_H
19
Yifan Hong1192e1d2017-04-11 14:45:00 -070020#include <android/hardware/camera/device/3.2/ICameraDevice.h>
21#include <android/hardware/camera/device/3.2/ICameraDeviceSession.h>
22#include <fmq/MessageQueue.h>
23#include <hidl/MQDescriptor.h>
24#include <hidl/Status.h>
25#include <include/convert.h>
Yin-Chia Yehbed3a942017-03-06 14:14:17 -080026#include <deque>
27#include <map>
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -080028#include <unordered_map>
Yin-Chia Yehbed3a942017-03-06 14:14:17 -080029#include "CameraMetadata.h"
Yifan Hong1192e1d2017-04-11 14:45:00 -070030#include "HandleImporter.h"
31#include "hardware/camera3.h"
32#include "hardware/camera_common.h"
33#include "utils/Mutex.h"
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -070034
35namespace android {
36namespace hardware {
37namespace camera {
38namespace device {
39namespace V3_2 {
40namespace implementation {
41
42using ::android::hardware::camera::device::V3_2::CaptureRequest;
43using ::android::hardware::camera::device::V3_2::HalStreamConfiguration;
44using ::android::hardware::camera::device::V3_2::StreamConfiguration;
45using ::android::hardware::camera::device::V3_2::ICameraDeviceSession;
46using ::android::hardware::camera::common::V1_0::Status;
Yin-Chia Yeh248ed702017-01-23 17:27:26 -080047using ::android::hardware::camera::common::V1_0::helper::HandleImporter;
Yifan Hong1192e1d2017-04-11 14:45:00 -070048using ::android::hardware::kSynchronizedReadWrite;
49using ::android::hardware::MessageQueue;
50using ::android::hardware::MQDescriptorSync;
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -070051using ::android::hardware::Return;
52using ::android::hardware::Void;
53using ::android::hardware::hidl_vec;
54using ::android::hardware::hidl_string;
55using ::android::sp;
56using ::android::Mutex;
57
58/**
59 * Function pointer types with C calling convention to
60 * use for HAL callback functions.
61 */
62extern "C" {
63 typedef void (callbacks_process_capture_result_t)(
64 const struct camera3_callback_ops *,
65 const camera3_capture_result_t *);
66
67 typedef void (callbacks_notify_t)(
68 const struct camera3_callback_ops *,
69 const camera3_notify_msg_t *);
70}
71
72struct CameraDeviceSession : public ICameraDeviceSession, private camera3_callback_ops {
73
Yin-Chia Yehbed3a942017-03-06 14:14:17 -080074 CameraDeviceSession(camera3_device_t*,
75 const camera_metadata_t* deviceInfo,
76 const sp<ICameraDeviceCallback>&);
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -070077 ~CameraDeviceSession();
78 // Call by CameraDevice to dump active device states
79 void dumpState(const native_handle_t* fd);
80 // Caller must use this method to check if CameraDeviceSession ctor failed
81 bool isInitFailed() { return mInitFail; }
82 // Used by CameraDevice to signal external camera disconnected
83 void disconnect();
84 bool isClosed();
85
86 // Methods from ::android::hardware::camera::device::V3_2::ICameraDeviceSession follow.
Yin-Chia Yehbed3a942017-03-06 14:14:17 -080087 Return<void> constructDefaultRequestSettings(
88 RequestTemplate type, constructDefaultRequestSettings_cb _hidl_cb) override;
89 Return<void> configureStreams(
90 const StreamConfiguration& requestedConfiguration, configureStreams_cb _hidl_cb) override;
Yifan Hong1192e1d2017-04-11 14:45:00 -070091 Return<void> getCaptureRequestMetadataQueue(
92 getCaptureRequestMetadataQueue_cb _hidl_cb) override;
Yin-Chia Yehbed3a942017-03-06 14:14:17 -080093 Return<void> processCaptureRequest(
Yin-Chia Yeh28eebbf2017-03-30 15:06:20 -070094 const hidl_vec<CaptureRequest>& requests,
95 const hidl_vec<BufferCache>& cachesToRemove,
96 processCaptureRequest_cb _hidl_cb) override;
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -070097 Return<Status> flush() override;
98 Return<void> close() override;
99
100private:
101 // protecting mClosed/mDisconnected/mInitFail
102 mutable Mutex mStateLock;
103 // device is closed either
104 // - closed by user
105 // - init failed
106 // - camera disconnected
107 bool mClosed = false;
108
109 // Set by CameraDevice (when external camera is disconnected)
110 bool mDisconnected = false;
111
112 camera3_device_t* mDevice;
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700113 // Stream ID -> Camera3Stream cache
114 std::map<int, Camera3Stream> mStreamMap;
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800115
116 mutable Mutex mInflightLock; // protecting mInflightBuffers and mCirculatingBuffers
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700117 // (streamID, frameNumber) -> inflight buffer cache
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800118 std::map<std::pair<int, uint32_t>, camera3_stream_buffer_t> mInflightBuffers;
119
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800120 // buffers currently ciculating between HAL and camera service
Yin-Chia Yehd926f932017-01-09 15:21:11 -0800121 // key: bufferId sent via HIDL interface
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800122 // value: imported buffer_handle_t
123 // Buffer will be imported during process_capture_request and will be freed
124 // when the its stream is deleted or camera device session is closed
Yin-Chia Yehd926f932017-01-09 15:21:11 -0800125 typedef std::unordered_map<uint64_t, buffer_handle_t> CirculatingBuffers;
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800126 // Stream ID -> circulating buffers map
127 std::map<int, CirculatingBuffers> mCirculatingBuffers;
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700128
Yin-Chia Yeh248ed702017-01-23 17:27:26 -0800129 static HandleImporter& sHandleImporter;
130
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700131 bool mInitFail;
Yin-Chia Yehbed3a942017-03-06 14:14:17 -0800132
133 common::V1_0::helper::CameraMetadata mDeviceInfo;
134
Yifan Hong1192e1d2017-04-11 14:45:00 -0700135 using RequestMetadataQueue = MessageQueue<uint8_t, kSynchronizedReadWrite>;
136 std::unique_ptr<RequestMetadataQueue> mRequestMetadataQueue;
137
Yin-Chia Yehbed3a942017-03-06 14:14:17 -0800138 class ResultBatcher {
139 public:
140 ResultBatcher(const sp<ICameraDeviceCallback>& callback);
141 void setNumPartialResults(uint32_t n);
142 void setBatchedStreams(const std::vector<int>& streamsToBatch);
143
144 void registerBatch(const hidl_vec<CaptureRequest>& requests);
145 void notify(NotifyMsg& msg);
146 void processCaptureResult(CaptureResult& result);
147
148 private:
149 struct InflightBatch {
150 // Protect access to entire struct. Acquire this lock before read/write any data or
151 // calling any methods. processCaptureResult and notify will compete for this lock
152 // HIDL IPCs might be issued while the lock is held
153 Mutex mLock;
154
155 bool allDelivered() const;
156
157 uint32_t mFirstFrame;
158 uint32_t mLastFrame;
159 uint32_t mBatchSize;
160
161 bool mShutterDelivered = false;
162 std::vector<NotifyMsg> mShutterMsgs;
163
164 struct BufferBatch {
165 bool mDelivered = false;
166 // This currently assumes every batched request will output to the batched stream
167 // and since HAL must always send buffers in order, no frameNumber tracking is
168 // needed
169 std::vector<StreamBuffer> mBuffers;
170 };
171 // Stream ID -> VideoBatch
172 std::unordered_map<int, BufferBatch> mBatchBufs;
173
174 struct MetadataBatch {
175 // (frameNumber, metadata)
176 std::vector<std::pair<uint32_t, CameraMetadata>> mMds;
177 };
178 // Partial result IDs that has been delivered to framework
179 uint32_t mNumPartialResults;
180 uint32_t mPartialResultProgress = 0;
181 // partialResult -> MetadataBatch
182 std::map<uint32_t, MetadataBatch> mResultMds;
183
184 // Set to true when batch is removed from mInflightBatches
185 // processCaptureResult and notify must check this flag after acquiring mLock to make
186 // sure this batch isn't removed while waiting for mLock
187 bool mRemoved = false;
188 };
189
190 static const int NOT_BATCHED = -1;
191
192 // Get the batch index and pointer to InflightBatch (nullptrt if the frame is not batched)
193 // Caller must acquire the InflightBatch::mLock before accessing the InflightBatch
194 // It's possible that the InflightBatch is removed from mInflightBatches before the
195 // InflightBatch::mLock is acquired (most likely caused by an error notification), so
196 // caller must check InflightBatch::mRemoved flag after the lock is acquried.
197 // This method will hold ResultBatcher::mLock briefly
198 std::pair<int, std::shared_ptr<InflightBatch>> getBatch(uint32_t frameNumber);
199
200 // Check if the first batch in mInflightBatches is ready to be removed, and remove it if so
201 // This method will hold ResultBatcher::mLock briefly
202 void checkAndRemoveFirstBatch();
203
204 // The following sendXXXX methods must be called while the InflightBatch::mLock is locked
205 // HIDL IPC methods will be called during these methods.
206 void sendBatchShutterCbsLocked(std::shared_ptr<InflightBatch> batch);
207 // send buffers for all batched streams
208 void sendBatchBuffersLocked(std::shared_ptr<InflightBatch> batch);
209 // send buffers for specified streams
210 void sendBatchBuffersLocked(
211 std::shared_ptr<InflightBatch> batch, const std::vector<int>& streams);
212 void sendBatchMetadataLocked(
213 std::shared_ptr<InflightBatch> batch, uint32_t lastPartialResultIdx);
214 // End of sendXXXX methods
215
216 // helper methods
217 void freeReleaseFences(hidl_vec<CaptureResult>&);
218 void notifySingleMsg(NotifyMsg& msg);
219 void processOneCaptureResult(CaptureResult& result);
220
221 // Protect access to mInflightBatches, mNumPartialResults and mStreamsToBatch
222 // processCaptureRequest, processCaptureResult, notify will compete for this lock
223 // Do NOT issue HIDL IPCs while holding this lock (except when HAL reports error)
224 mutable Mutex mLock;
225 std::deque<std::shared_ptr<InflightBatch>> mInflightBatches;
226 uint32_t mNumPartialResults;
227 std::vector<int> mStreamsToBatch;
228 const sp<ICameraDeviceCallback> mCallback;
229 } mResultBatcher;
230
231 std::vector<int> mVideoStreamIds;
232
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700233 bool initialize();
234
235 Status initStatus() const;
236
237 // Validate and import request's input buffer and acquire fence
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800238 Status importRequest(
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700239 const CaptureRequest& request,
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800240 hidl_vec<buffer_handle_t*>& allBufPtrs,
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700241 hidl_vec<int>& allFences);
242
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800243 static void cleanupInflightFences(
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700244 hidl_vec<int>& allFences, size_t numFences);
245
Emilian Peev98014ff2017-02-02 16:20:12 +0000246 void cleanupBuffersLocked(int id);
247
Yin-Chia Yeh28eebbf2017-03-30 15:06:20 -0700248 void updateBufferCaches(const hidl_vec<BufferCache>& cachesToRemove);
249
Yin-Chia Yehbed3a942017-03-06 14:14:17 -0800250 Status processOneCaptureRequest(const CaptureRequest& request);
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700251 /**
252 * Static callback forwarding methods from HAL to instance
253 */
254 static callbacks_process_capture_result_t sProcessCaptureResult;
255 static callbacks_notify_t sNotify;
256};
257
258} // namespace implementation
259} // namespace V3_2
260} // namespace device
261} // namespace camera
262} // namespace hardware
263} // namespace android
264
265#endif // ANDROID_HARDWARE_CAMERA_DEVICE_V3_2_CAMERADEVICE3SESSION_H