blob: 3786e4b2bafa19b55b561a3ee4b2b1d69405699e [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>
Yin-Chia Yeh248ed702017-01-23 17:27:26 -080029#include "HandleImporter.h"
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -070030
31namespace android {
32namespace hardware {
33namespace camera {
34namespace device {
35namespace V3_2 {
36namespace implementation {
37
38using ::android::hardware::camera::device::V3_2::CaptureRequest;
39using ::android::hardware::camera::device::V3_2::HalStreamConfiguration;
40using ::android::hardware::camera::device::V3_2::StreamConfiguration;
41using ::android::hardware::camera::device::V3_2::ICameraDeviceSession;
42using ::android::hardware::camera::common::V1_0::Status;
Yin-Chia Yeh248ed702017-01-23 17:27:26 -080043using ::android::hardware::camera::common::V1_0::helper::HandleImporter;
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -070044using ::android::hardware::Return;
45using ::android::hardware::Void;
46using ::android::hardware::hidl_vec;
47using ::android::hardware::hidl_string;
48using ::android::sp;
49using ::android::Mutex;
50
51/**
52 * Function pointer types with C calling convention to
53 * use for HAL callback functions.
54 */
55extern "C" {
56 typedef void (callbacks_process_capture_result_t)(
57 const struct camera3_callback_ops *,
58 const camera3_capture_result_t *);
59
60 typedef void (callbacks_notify_t)(
61 const struct camera3_callback_ops *,
62 const camera3_notify_msg_t *);
63}
64
65struct CameraDeviceSession : public ICameraDeviceSession, private camera3_callback_ops {
66
67 CameraDeviceSession(camera3_device_t*, const sp<ICameraDeviceCallback>&);
68 ~CameraDeviceSession();
69 // Call by CameraDevice to dump active device states
70 void dumpState(const native_handle_t* fd);
71 // Caller must use this method to check if CameraDeviceSession ctor failed
72 bool isInitFailed() { return mInitFail; }
73 // Used by CameraDevice to signal external camera disconnected
74 void disconnect();
75 bool isClosed();
76
77 // Methods from ::android::hardware::camera::device::V3_2::ICameraDeviceSession follow.
78 Return<void> constructDefaultRequestSettings(RequestTemplate type, constructDefaultRequestSettings_cb _hidl_cb) override;
79 Return<void> configureStreams(const StreamConfiguration& requestedConfiguration, configureStreams_cb _hidl_cb) override;
80 Return<Status> processCaptureRequest(const CaptureRequest& request) override;
81 Return<Status> flush() override;
82 Return<void> close() override;
83
84private:
85 // protecting mClosed/mDisconnected/mInitFail
86 mutable Mutex mStateLock;
87 // device is closed either
88 // - closed by user
89 // - init failed
90 // - camera disconnected
91 bool mClosed = false;
92
93 // Set by CameraDevice (when external camera is disconnected)
94 bool mDisconnected = false;
95
96 camera3_device_t* mDevice;
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -080097 const sp<ICameraDeviceCallback> mCallback;
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -070098 // Stream ID -> Camera3Stream cache
99 std::map<int, Camera3Stream> mStreamMap;
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800100
101 mutable Mutex mInflightLock; // protecting mInflightBuffers and mCirculatingBuffers
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700102 // (streamID, frameNumber) -> inflight buffer cache
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800103 std::map<std::pair<int, uint32_t>, camera3_stream_buffer_t> mInflightBuffers;
104
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800105 // buffers currently ciculating between HAL and camera service
Yin-Chia Yehd926f932017-01-09 15:21:11 -0800106 // key: bufferId sent via HIDL interface
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800107 // value: imported buffer_handle_t
108 // Buffer will be imported during process_capture_request and will be freed
109 // when the its stream is deleted or camera device session is closed
Yin-Chia Yehd926f932017-01-09 15:21:11 -0800110 typedef std::unordered_map<uint64_t, buffer_handle_t> CirculatingBuffers;
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800111 // Stream ID -> circulating buffers map
112 std::map<int, CirculatingBuffers> mCirculatingBuffers;
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700113
Yin-Chia Yeh248ed702017-01-23 17:27:26 -0800114 static HandleImporter& sHandleImporter;
115
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700116 bool mInitFail;
117 bool initialize();
118
119 Status initStatus() const;
120
121 // Validate and import request's input buffer and acquire fence
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800122 Status importRequest(
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700123 const CaptureRequest& request,
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800124 hidl_vec<buffer_handle_t*>& allBufPtrs,
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700125 hidl_vec<int>& allFences);
126
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800127 static void cleanupInflightFences(
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700128 hidl_vec<int>& allFences, size_t numFences);
129
Emilian Peev98014ff2017-02-02 16:20:12 +0000130 void cleanupBuffersLocked(int id);
131
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700132 /**
133 * Static callback forwarding methods from HAL to instance
134 */
135 static callbacks_process_capture_result_t sProcessCaptureResult;
136 static callbacks_notify_t sNotify;
137};
138
139} // namespace implementation
140} // namespace V3_2
141} // namespace device
142} // namespace camera
143} // namespace hardware
144} // namespace android
145
146#endif // ANDROID_HARDWARE_CAMERA_DEVICE_V3_2_CAMERADEVICE3SESSION_H