blob: ca9d24d2407372d7f73a4cbe14a8bcbc7b0cba27 [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
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800103 // buffers currently ciculating between HAL and camera service
Yin-Chia Yehd926f932017-01-09 15:21:11 -0800104 // key: bufferId sent via HIDL interface
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800105 // value: imported buffer_handle_t
106 // Buffer will be imported during process_capture_request and will be freed
107 // when the its stream is deleted or camera device session is closed
Yin-Chia Yehd926f932017-01-09 15:21:11 -0800108 typedef std::unordered_map<uint64_t, buffer_handle_t> CirculatingBuffers;
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800109 // Stream ID -> circulating buffers map
110 std::map<int, CirculatingBuffers> mCirculatingBuffers;
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700111
112 bool mInitFail;
113 bool initialize();
114
115 Status initStatus() const;
116
117 // Validate and import request's input buffer and acquire fence
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800118 Status importRequest(
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700119 const CaptureRequest& request,
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800120 hidl_vec<buffer_handle_t*>& allBufPtrs,
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700121 hidl_vec<int>& allFences);
122
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800123 static void cleanupInflightFences(
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700124 hidl_vec<int>& allFences, size_t numFences);
125
126 /**
127 * Static callback forwarding methods from HAL to instance
128 */
129 static callbacks_process_capture_result_t sProcessCaptureResult;
130 static callbacks_notify_t sNotify;
131};
132
133} // namespace implementation
134} // namespace V3_2
135} // namespace device
136} // namespace camera
137} // namespace hardware
138} // namespace android
139
140#endif // ANDROID_HARDWARE_CAMERA_DEVICE_V3_2_CAMERADEVICE3SESSION_H