blob: c08ed90011d30c85900e7f7c400511773479c447 [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;
Yifan Hong993e3d02017-04-12 16:31:23 -070093 Return<void> getCaptureResultMetadataQueue(
94 getCaptureResultMetadataQueue_cb _hidl_cb) override;
Yin-Chia Yehbed3a942017-03-06 14:14:17 -080095 Return<void> processCaptureRequest(
Yin-Chia Yeh28eebbf2017-03-30 15:06:20 -070096 const hidl_vec<CaptureRequest>& requests,
97 const hidl_vec<BufferCache>& cachesToRemove,
98 processCaptureRequest_cb _hidl_cb) override;
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -070099 Return<Status> flush() override;
100 Return<void> close() override;
101
102private:
103 // protecting mClosed/mDisconnected/mInitFail
104 mutable Mutex mStateLock;
105 // device is closed either
106 // - closed by user
107 // - init failed
108 // - camera disconnected
109 bool mClosed = false;
110
111 // Set by CameraDevice (when external camera is disconnected)
112 bool mDisconnected = false;
113
Emilian Peevcf581372017-04-07 13:53:10 +0100114 struct AETriggerCancelOverride {
115 bool applyAeLock;
116 uint8_t aeLock;
117 bool applyAePrecaptureTrigger;
118 uint8_t aePrecaptureTrigger;
119 };
120
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700121 camera3_device_t* mDevice;
Emilian Peev7d52a6f2017-04-07 09:53:48 +0100122 uint32_t mDeviceVersion;
Emilian Peevcf581372017-04-07 13:53:10 +0100123 bool mIsAELockAvailable;
124 uint32_t mNumPartialResults;
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700125 // Stream ID -> Camera3Stream cache
126 std::map<int, Camera3Stream> mStreamMap;
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800127
128 mutable Mutex mInflightLock; // protecting mInflightBuffers and mCirculatingBuffers
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700129 // (streamID, frameNumber) -> inflight buffer cache
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800130 std::map<std::pair<int, uint32_t>, camera3_stream_buffer_t> mInflightBuffers;
131
Emilian Peevcf581372017-04-07 13:53:10 +0100132 // (frameNumber, AETriggerOverride) -> inflight request AETriggerOverrides
133 std::map<uint32_t, AETriggerCancelOverride> mInflightAETriggerOverrides;
134 ::android::hardware::camera::common::V1_0::helper::CameraMetadata mOverridenResult;
135
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800136 // buffers currently ciculating between HAL and camera service
Yin-Chia Yehd926f932017-01-09 15:21:11 -0800137 // key: bufferId sent via HIDL interface
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800138 // value: imported buffer_handle_t
139 // Buffer will be imported during process_capture_request and will be freed
140 // when the its stream is deleted or camera device session is closed
Yin-Chia Yehd926f932017-01-09 15:21:11 -0800141 typedef std::unordered_map<uint64_t, buffer_handle_t> CirculatingBuffers;
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800142 // Stream ID -> circulating buffers map
143 std::map<int, CirculatingBuffers> mCirculatingBuffers;
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700144
Yin-Chia Yeh248ed702017-01-23 17:27:26 -0800145 static HandleImporter& sHandleImporter;
146
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700147 bool mInitFail;
Yin-Chia Yehbed3a942017-03-06 14:14:17 -0800148
149 common::V1_0::helper::CameraMetadata mDeviceInfo;
150
Yifan Hong1192e1d2017-04-11 14:45:00 -0700151 using RequestMetadataQueue = MessageQueue<uint8_t, kSynchronizedReadWrite>;
152 std::unique_ptr<RequestMetadataQueue> mRequestMetadataQueue;
Yifan Hong993e3d02017-04-12 16:31:23 -0700153 using ResultMetadataQueue = MessageQueue<uint8_t, kSynchronizedReadWrite>;
154 std::shared_ptr<ResultMetadataQueue> mResultMetadataQueue;
Yifan Hong1192e1d2017-04-11 14:45:00 -0700155
Yin-Chia Yehbed3a942017-03-06 14:14:17 -0800156 class ResultBatcher {
157 public:
158 ResultBatcher(const sp<ICameraDeviceCallback>& callback);
159 void setNumPartialResults(uint32_t n);
160 void setBatchedStreams(const std::vector<int>& streamsToBatch);
Yifan Hong993e3d02017-04-12 16:31:23 -0700161 void setResultMetadataQueue(std::shared_ptr<ResultMetadataQueue> q);
Yin-Chia Yehbed3a942017-03-06 14:14:17 -0800162
163 void registerBatch(const hidl_vec<CaptureRequest>& requests);
164 void notify(NotifyMsg& msg);
165 void processCaptureResult(CaptureResult& result);
166
167 private:
168 struct InflightBatch {
169 // Protect access to entire struct. Acquire this lock before read/write any data or
170 // calling any methods. processCaptureResult and notify will compete for this lock
171 // HIDL IPCs might be issued while the lock is held
172 Mutex mLock;
173
174 bool allDelivered() const;
175
176 uint32_t mFirstFrame;
177 uint32_t mLastFrame;
178 uint32_t mBatchSize;
179
180 bool mShutterDelivered = false;
181 std::vector<NotifyMsg> mShutterMsgs;
182
183 struct BufferBatch {
184 bool mDelivered = false;
185 // This currently assumes every batched request will output to the batched stream
186 // and since HAL must always send buffers in order, no frameNumber tracking is
187 // needed
188 std::vector<StreamBuffer> mBuffers;
189 };
190 // Stream ID -> VideoBatch
191 std::unordered_map<int, BufferBatch> mBatchBufs;
192
193 struct MetadataBatch {
194 // (frameNumber, metadata)
195 std::vector<std::pair<uint32_t, CameraMetadata>> mMds;
196 };
197 // Partial result IDs that has been delivered to framework
198 uint32_t mNumPartialResults;
199 uint32_t mPartialResultProgress = 0;
200 // partialResult -> MetadataBatch
201 std::map<uint32_t, MetadataBatch> mResultMds;
202
203 // Set to true when batch is removed from mInflightBatches
204 // processCaptureResult and notify must check this flag after acquiring mLock to make
205 // sure this batch isn't removed while waiting for mLock
206 bool mRemoved = false;
207 };
208
209 static const int NOT_BATCHED = -1;
210
211 // Get the batch index and pointer to InflightBatch (nullptrt if the frame is not batched)
212 // Caller must acquire the InflightBatch::mLock before accessing the InflightBatch
213 // It's possible that the InflightBatch is removed from mInflightBatches before the
214 // InflightBatch::mLock is acquired (most likely caused by an error notification), so
215 // caller must check InflightBatch::mRemoved flag after the lock is acquried.
216 // This method will hold ResultBatcher::mLock briefly
217 std::pair<int, std::shared_ptr<InflightBatch>> getBatch(uint32_t frameNumber);
218
219 // Check if the first batch in mInflightBatches is ready to be removed, and remove it if so
220 // This method will hold ResultBatcher::mLock briefly
221 void checkAndRemoveFirstBatch();
222
223 // The following sendXXXX methods must be called while the InflightBatch::mLock is locked
224 // HIDL IPC methods will be called during these methods.
225 void sendBatchShutterCbsLocked(std::shared_ptr<InflightBatch> batch);
226 // send buffers for all batched streams
227 void sendBatchBuffersLocked(std::shared_ptr<InflightBatch> batch);
228 // send buffers for specified streams
229 void sendBatchBuffersLocked(
230 std::shared_ptr<InflightBatch> batch, const std::vector<int>& streams);
231 void sendBatchMetadataLocked(
232 std::shared_ptr<InflightBatch> batch, uint32_t lastPartialResultIdx);
233 // End of sendXXXX methods
234
235 // helper methods
236 void freeReleaseFences(hidl_vec<CaptureResult>&);
237 void notifySingleMsg(NotifyMsg& msg);
238 void processOneCaptureResult(CaptureResult& result);
Yifan Hong993e3d02017-04-12 16:31:23 -0700239 void invokeProcessCaptureResultCallback(hidl_vec<CaptureResult> &results, bool tryWriteFmq);
Yin-Chia Yehbed3a942017-03-06 14:14:17 -0800240
241 // Protect access to mInflightBatches, mNumPartialResults and mStreamsToBatch
242 // processCaptureRequest, processCaptureResult, notify will compete for this lock
243 // Do NOT issue HIDL IPCs while holding this lock (except when HAL reports error)
244 mutable Mutex mLock;
245 std::deque<std::shared_ptr<InflightBatch>> mInflightBatches;
246 uint32_t mNumPartialResults;
247 std::vector<int> mStreamsToBatch;
248 const sp<ICameraDeviceCallback> mCallback;
Yifan Hong993e3d02017-04-12 16:31:23 -0700249 std::shared_ptr<ResultMetadataQueue> mResultMetadataQueue;
250
251 // Protect against invokeProcessCaptureResultCallback()
252 Mutex mProcessCaptureResultLock;
253
Yin-Chia Yehbed3a942017-03-06 14:14:17 -0800254 } mResultBatcher;
255
256 std::vector<int> mVideoStreamIds;
257
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700258 bool initialize();
259
260 Status initStatus() const;
261
262 // Validate and import request's input buffer and acquire fence
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800263 Status importRequest(
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700264 const CaptureRequest& request,
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800265 hidl_vec<buffer_handle_t*>& allBufPtrs,
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700266 hidl_vec<int>& allFences);
267
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800268 static void cleanupInflightFences(
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700269 hidl_vec<int>& allFences, size_t numFences);
270
Emilian Peev98014ff2017-02-02 16:20:12 +0000271 void cleanupBuffersLocked(int id);
272
Yin-Chia Yeh28eebbf2017-03-30 15:06:20 -0700273 void updateBufferCaches(const hidl_vec<BufferCache>& cachesToRemove);
274
Emilian Peev7d52a6f2017-04-07 09:53:48 +0100275 android_dataspace mapToLegacyDataspace(
276 android_dataspace dataSpace) const;
277
Emilian Peevcf581372017-04-07 13:53:10 +0100278 bool handleAePrecaptureCancelRequestLocked(
279 const camera3_capture_request_t &halRequest,
280 android::hardware::camera::common::V1_0::helper::CameraMetadata *settings /*out*/,
281 AETriggerCancelOverride *override /*out*/);
282
283 void overrideResultForPrecaptureCancelLocked(
284 const AETriggerCancelOverride &aeTriggerCancelOverride,
285 ::android::hardware::camera::common::V1_0::helper::CameraMetadata *settings /*out*/);
286
Yin-Chia Yehbed3a942017-03-06 14:14:17 -0800287 Status processOneCaptureRequest(const CaptureRequest& request);
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700288 /**
289 * Static callback forwarding methods from HAL to instance
290 */
291 static callbacks_process_capture_result_t sProcessCaptureResult;
292 static callbacks_notify_t sNotify;
293};
294
295} // namespace implementation
296} // namespace V3_2
297} // namespace device
298} // namespace camera
299} // namespace hardware
300} // namespace android
301
302#endif // ANDROID_HARDWARE_CAMERA_DEVICE_V3_2_CAMERADEVICE3SESSION_H