blob: 781056eb3bd2ec6a54f54bed8c2ac1e54c6fc993 [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
Yin-Chia Yehbed3a942017-03-06 14:14:17 -080020#include <deque>
21#include <map>
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -080022#include <unordered_map>
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -070023#include "hardware/camera_common.h"
24#include "hardware/camera3.h"
25#include "utils/Mutex.h"
26#include <android/hardware/camera/device/3.2/ICameraDevice.h>
27#include <android/hardware/camera/device/3.2/ICameraDeviceSession.h>
28#include <hidl/Status.h>
29#include <hidl/MQDescriptor.h>
30#include <include/convert.h>
Yin-Chia Yeh248ed702017-01-23 17:27:26 -080031#include "HandleImporter.h"
Yin-Chia Yehbed3a942017-03-06 14:14:17 -080032#include "CameraMetadata.h"
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -070033
34namespace android {
35namespace hardware {
36namespace camera {
37namespace device {
38namespace V3_2 {
39namespace implementation {
40
41using ::android::hardware::camera::device::V3_2::CaptureRequest;
42using ::android::hardware::camera::device::V3_2::HalStreamConfiguration;
43using ::android::hardware::camera::device::V3_2::StreamConfiguration;
44using ::android::hardware::camera::device::V3_2::ICameraDeviceSession;
45using ::android::hardware::camera::common::V1_0::Status;
Yin-Chia Yeh248ed702017-01-23 17:27:26 -080046using ::android::hardware::camera::common::V1_0::helper::HandleImporter;
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -070047using ::android::hardware::Return;
48using ::android::hardware::Void;
49using ::android::hardware::hidl_vec;
50using ::android::hardware::hidl_string;
51using ::android::sp;
52using ::android::Mutex;
53
54/**
55 * Function pointer types with C calling convention to
56 * use for HAL callback functions.
57 */
58extern "C" {
59 typedef void (callbacks_process_capture_result_t)(
60 const struct camera3_callback_ops *,
61 const camera3_capture_result_t *);
62
63 typedef void (callbacks_notify_t)(
64 const struct camera3_callback_ops *,
65 const camera3_notify_msg_t *);
66}
67
68struct CameraDeviceSession : public ICameraDeviceSession, private camera3_callback_ops {
69
Yin-Chia Yehbed3a942017-03-06 14:14:17 -080070 CameraDeviceSession(camera3_device_t*,
71 const camera_metadata_t* deviceInfo,
72 const sp<ICameraDeviceCallback>&);
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -070073 ~CameraDeviceSession();
74 // Call by CameraDevice to dump active device states
75 void dumpState(const native_handle_t* fd);
76 // Caller must use this method to check if CameraDeviceSession ctor failed
77 bool isInitFailed() { return mInitFail; }
78 // Used by CameraDevice to signal external camera disconnected
79 void disconnect();
80 bool isClosed();
81
82 // Methods from ::android::hardware::camera::device::V3_2::ICameraDeviceSession follow.
Yin-Chia Yehbed3a942017-03-06 14:14:17 -080083 Return<void> constructDefaultRequestSettings(
84 RequestTemplate type, constructDefaultRequestSettings_cb _hidl_cb) override;
85 Return<void> configureStreams(
86 const StreamConfiguration& requestedConfiguration, configureStreams_cb _hidl_cb) override;
87 Return<void> processCaptureRequest(
Yin-Chia Yeh28eebbf2017-03-30 15:06:20 -070088 const hidl_vec<CaptureRequest>& requests,
89 const hidl_vec<BufferCache>& cachesToRemove,
90 processCaptureRequest_cb _hidl_cb) override;
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -070091 Return<Status> flush() override;
92 Return<void> close() override;
93
94private:
95 // protecting mClosed/mDisconnected/mInitFail
96 mutable Mutex mStateLock;
97 // device is closed either
98 // - closed by user
99 // - init failed
100 // - camera disconnected
101 bool mClosed = false;
102
103 // Set by CameraDevice (when external camera is disconnected)
104 bool mDisconnected = false;
105
106 camera3_device_t* mDevice;
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700107 // Stream ID -> Camera3Stream cache
108 std::map<int, Camera3Stream> mStreamMap;
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800109
110 mutable Mutex mInflightLock; // protecting mInflightBuffers and mCirculatingBuffers
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700111 // (streamID, frameNumber) -> inflight buffer cache
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800112 std::map<std::pair<int, uint32_t>, camera3_stream_buffer_t> mInflightBuffers;
113
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800114 // buffers currently ciculating between HAL and camera service
Yin-Chia Yehd926f932017-01-09 15:21:11 -0800115 // key: bufferId sent via HIDL interface
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800116 // value: imported buffer_handle_t
117 // Buffer will be imported during process_capture_request and will be freed
118 // when the its stream is deleted or camera device session is closed
Yin-Chia Yehd926f932017-01-09 15:21:11 -0800119 typedef std::unordered_map<uint64_t, buffer_handle_t> CirculatingBuffers;
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800120 // Stream ID -> circulating buffers map
121 std::map<int, CirculatingBuffers> mCirculatingBuffers;
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700122
Yin-Chia Yeh248ed702017-01-23 17:27:26 -0800123 static HandleImporter& sHandleImporter;
124
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700125 bool mInitFail;
Yin-Chia Yehbed3a942017-03-06 14:14:17 -0800126
127 common::V1_0::helper::CameraMetadata mDeviceInfo;
128
129 class ResultBatcher {
130 public:
131 ResultBatcher(const sp<ICameraDeviceCallback>& callback);
132 void setNumPartialResults(uint32_t n);
133 void setBatchedStreams(const std::vector<int>& streamsToBatch);
134
135 void registerBatch(const hidl_vec<CaptureRequest>& requests);
136 void notify(NotifyMsg& msg);
137 void processCaptureResult(CaptureResult& result);
138
139 private:
140 struct InflightBatch {
141 // Protect access to entire struct. Acquire this lock before read/write any data or
142 // calling any methods. processCaptureResult and notify will compete for this lock
143 // HIDL IPCs might be issued while the lock is held
144 Mutex mLock;
145
146 bool allDelivered() const;
147
148 uint32_t mFirstFrame;
149 uint32_t mLastFrame;
150 uint32_t mBatchSize;
151
152 bool mShutterDelivered = false;
153 std::vector<NotifyMsg> mShutterMsgs;
154
155 struct BufferBatch {
156 bool mDelivered = false;
157 // This currently assumes every batched request will output to the batched stream
158 // and since HAL must always send buffers in order, no frameNumber tracking is
159 // needed
160 std::vector<StreamBuffer> mBuffers;
161 };
162 // Stream ID -> VideoBatch
163 std::unordered_map<int, BufferBatch> mBatchBufs;
164
165 struct MetadataBatch {
166 // (frameNumber, metadata)
167 std::vector<std::pair<uint32_t, CameraMetadata>> mMds;
168 };
169 // Partial result IDs that has been delivered to framework
170 uint32_t mNumPartialResults;
171 uint32_t mPartialResultProgress = 0;
172 // partialResult -> MetadataBatch
173 std::map<uint32_t, MetadataBatch> mResultMds;
174
175 // Set to true when batch is removed from mInflightBatches
176 // processCaptureResult and notify must check this flag after acquiring mLock to make
177 // sure this batch isn't removed while waiting for mLock
178 bool mRemoved = false;
179 };
180
181 static const int NOT_BATCHED = -1;
182
183 // Get the batch index and pointer to InflightBatch (nullptrt if the frame is not batched)
184 // Caller must acquire the InflightBatch::mLock before accessing the InflightBatch
185 // It's possible that the InflightBatch is removed from mInflightBatches before the
186 // InflightBatch::mLock is acquired (most likely caused by an error notification), so
187 // caller must check InflightBatch::mRemoved flag after the lock is acquried.
188 // This method will hold ResultBatcher::mLock briefly
189 std::pair<int, std::shared_ptr<InflightBatch>> getBatch(uint32_t frameNumber);
190
191 // Check if the first batch in mInflightBatches is ready to be removed, and remove it if so
192 // This method will hold ResultBatcher::mLock briefly
193 void checkAndRemoveFirstBatch();
194
195 // The following sendXXXX methods must be called while the InflightBatch::mLock is locked
196 // HIDL IPC methods will be called during these methods.
197 void sendBatchShutterCbsLocked(std::shared_ptr<InflightBatch> batch);
198 // send buffers for all batched streams
199 void sendBatchBuffersLocked(std::shared_ptr<InflightBatch> batch);
200 // send buffers for specified streams
201 void sendBatchBuffersLocked(
202 std::shared_ptr<InflightBatch> batch, const std::vector<int>& streams);
203 void sendBatchMetadataLocked(
204 std::shared_ptr<InflightBatch> batch, uint32_t lastPartialResultIdx);
205 // End of sendXXXX methods
206
207 // helper methods
208 void freeReleaseFences(hidl_vec<CaptureResult>&);
209 void notifySingleMsg(NotifyMsg& msg);
210 void processOneCaptureResult(CaptureResult& result);
211
212 // Protect access to mInflightBatches, mNumPartialResults and mStreamsToBatch
213 // processCaptureRequest, processCaptureResult, notify will compete for this lock
214 // Do NOT issue HIDL IPCs while holding this lock (except when HAL reports error)
215 mutable Mutex mLock;
216 std::deque<std::shared_ptr<InflightBatch>> mInflightBatches;
217 uint32_t mNumPartialResults;
218 std::vector<int> mStreamsToBatch;
219 const sp<ICameraDeviceCallback> mCallback;
220 } mResultBatcher;
221
222 std::vector<int> mVideoStreamIds;
223
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700224 bool initialize();
225
226 Status initStatus() const;
227
228 // Validate and import request's input buffer and acquire fence
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800229 Status importRequest(
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700230 const CaptureRequest& request,
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800231 hidl_vec<buffer_handle_t*>& allBufPtrs,
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700232 hidl_vec<int>& allFences);
233
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800234 static void cleanupInflightFences(
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700235 hidl_vec<int>& allFences, size_t numFences);
236
Emilian Peev98014ff2017-02-02 16:20:12 +0000237 void cleanupBuffersLocked(int id);
238
Yin-Chia Yeh28eebbf2017-03-30 15:06:20 -0700239 void updateBufferCaches(const hidl_vec<BufferCache>& cachesToRemove);
240
Yin-Chia Yehbed3a942017-03-06 14:14:17 -0800241 Status processOneCaptureRequest(const CaptureRequest& request);
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700242 /**
243 * Static callback forwarding methods from HAL to instance
244 */
245 static callbacks_process_capture_result_t sProcessCaptureResult;
246 static callbacks_notify_t sNotify;
247};
248
249} // namespace implementation
250} // namespace V3_2
251} // namespace device
252} // namespace camera
253} // namespace hardware
254} // namespace android
255
256#endif // ANDROID_HARDWARE_CAMERA_DEVICE_V3_2_CAMERADEVICE3SESSION_H