blob: 498617ee0fde46b58434fe8cb7891a9d870454ed [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 Yeh9c6dbd52016-12-22 14:55:02 -080020#include <unordered_map>
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -070021#include "hardware/camera_common.h"
22#include "hardware/camera3.h"
23#include "utils/Mutex.h"
24#include <android/hardware/camera/device/3.2/ICameraDevice.h>
25#include <android/hardware/camera/device/3.2/ICameraDeviceSession.h>
26#include <hidl/Status.h>
27#include <hidl/MQDescriptor.h>
28#include <include/convert.h>
29
30namespace android {
31namespace hardware {
32namespace camera {
33namespace device {
34namespace V3_2 {
35namespace implementation {
36
37using ::android::hardware::camera::device::V3_2::CaptureRequest;
38using ::android::hardware::camera::device::V3_2::HalStreamConfiguration;
39using ::android::hardware::camera::device::V3_2::StreamConfiguration;
40using ::android::hardware::camera::device::V3_2::ICameraDeviceSession;
41using ::android::hardware::camera::common::V1_0::Status;
42using ::android::hardware::Return;
43using ::android::hardware::Void;
44using ::android::hardware::hidl_vec;
45using ::android::hardware::hidl_string;
46using ::android::sp;
47using ::android::Mutex;
48
49/**
50 * Function pointer types with C calling convention to
51 * use for HAL callback functions.
52 */
53extern "C" {
54 typedef void (callbacks_process_capture_result_t)(
55 const struct camera3_callback_ops *,
56 const camera3_capture_result_t *);
57
58 typedef void (callbacks_notify_t)(
59 const struct camera3_callback_ops *,
60 const camera3_notify_msg_t *);
61}
62
63struct CameraDeviceSession : public ICameraDeviceSession, private camera3_callback_ops {
64
65 CameraDeviceSession(camera3_device_t*, const sp<ICameraDeviceCallback>&);
66 ~CameraDeviceSession();
67 // Call by CameraDevice to dump active device states
68 void dumpState(const native_handle_t* fd);
69 // Caller must use this method to check if CameraDeviceSession ctor failed
70 bool isInitFailed() { return mInitFail; }
71 // Used by CameraDevice to signal external camera disconnected
72 void disconnect();
73 bool isClosed();
74
75 // Methods from ::android::hardware::camera::device::V3_2::ICameraDeviceSession follow.
76 Return<void> constructDefaultRequestSettings(RequestTemplate type, constructDefaultRequestSettings_cb _hidl_cb) override;
77 Return<void> configureStreams(const StreamConfiguration& requestedConfiguration, configureStreams_cb _hidl_cb) override;
78 Return<Status> processCaptureRequest(const CaptureRequest& request) override;
79 Return<Status> flush() override;
80 Return<void> close() override;
81
82private:
83 // protecting mClosed/mDisconnected/mInitFail
84 mutable Mutex mStateLock;
85 // device is closed either
86 // - closed by user
87 // - init failed
88 // - camera disconnected
89 bool mClosed = false;
90
91 // Set by CameraDevice (when external camera is disconnected)
92 bool mDisconnected = false;
93
94 camera3_device_t* mDevice;
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -080095 const sp<ICameraDeviceCallback> mCallback;
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -070096 // Stream ID -> Camera3Stream cache
97 std::map<int, Camera3Stream> mStreamMap;
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -080098
99 mutable Mutex mInflightLock; // protecting mInflightBuffers and mCirculatingBuffers
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700100 // (streamID, frameNumber) -> inflight buffer cache
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800101 std::map<std::pair<int, uint32_t>, camera3_stream_buffer_t> mInflightBuffers;
102
103 struct BufferHasher {
104 size_t operator()(const buffer_handle_t& buf) const {
105 if (buf == nullptr)
106 return 0;
107
108 size_t result = 1;
109 result = 31 * result + buf->numFds;
110 result = 31 * result + buf->numInts;
111 int length = buf->numFds + buf->numInts;
112 for (int i = 0; i < length; i++) {
113 result = 31 * result + buf->data[i];
114 }
115 return result;
116 }
117 };
118
119 struct BufferComparator {
120 bool operator()(const buffer_handle_t& buf1, const buffer_handle_t& buf2) const {
121 if (buf1->numFds == buf2->numFds && buf1->numInts == buf2->numInts) {
122 int length = buf1->numFds + buf1->numInts;
123 for (int i = 0; i < length; i++) {
124 if (buf1->data[i] != buf2->data[i]) {
125 return false;
126 }
127 }
128 return true;
129 }
130 return false;
131 }
132 };
133
134 // buffers currently ciculating between HAL and camera service
135 // key: buffer_handle_t sent via HIDL interface
136 // value: imported buffer_handle_t
137 // Buffer will be imported during process_capture_request and will be freed
138 // when the its stream is deleted or camera device session is closed
139 typedef std::unordered_map<buffer_handle_t, buffer_handle_t,
140 BufferHasher, BufferComparator> CirculatingBuffers;
141 // Stream ID -> circulating buffers map
142 std::map<int, CirculatingBuffers> mCirculatingBuffers;
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700143
144 bool mInitFail;
145 bool initialize();
146
147 Status initStatus() const;
148
149 // Validate and import request's input buffer and acquire fence
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800150 Status importRequest(
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700151 const CaptureRequest& request,
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800152 hidl_vec<buffer_handle_t*>& allBufPtrs,
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700153 hidl_vec<int>& allFences);
154
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800155 static void cleanupInflightFences(
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700156 hidl_vec<int>& allFences, size_t numFences);
157
158 /**
159 * Static callback forwarding methods from HAL to instance
160 */
161 static callbacks_process_capture_result_t sProcessCaptureResult;
162 static callbacks_notify_t sNotify;
163};
164
165} // namespace implementation
166} // namespace V3_2
167} // namespace device
168} // namespace camera
169} // namespace hardware
170} // namespace android
171
172#endif // ANDROID_HARDWARE_CAMERA_DEVICE_V3_2_CAMERADEVICE3SESSION_H