Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 1 | /* |
| 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 Yeh | 9c6dbd5 | 2016-12-22 14:55:02 -0800 | [diff] [blame] | 20 | #include <unordered_map> |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 21 | #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 | |
| 30 | namespace android { |
| 31 | namespace hardware { |
| 32 | namespace camera { |
| 33 | namespace device { |
| 34 | namespace V3_2 { |
| 35 | namespace implementation { |
| 36 | |
| 37 | using ::android::hardware::camera::device::V3_2::CaptureRequest; |
| 38 | using ::android::hardware::camera::device::V3_2::HalStreamConfiguration; |
| 39 | using ::android::hardware::camera::device::V3_2::StreamConfiguration; |
| 40 | using ::android::hardware::camera::device::V3_2::ICameraDeviceSession; |
| 41 | using ::android::hardware::camera::common::V1_0::Status; |
| 42 | using ::android::hardware::Return; |
| 43 | using ::android::hardware::Void; |
| 44 | using ::android::hardware::hidl_vec; |
| 45 | using ::android::hardware::hidl_string; |
| 46 | using ::android::sp; |
| 47 | using ::android::Mutex; |
| 48 | |
| 49 | /** |
| 50 | * Function pointer types with C calling convention to |
| 51 | * use for HAL callback functions. |
| 52 | */ |
| 53 | extern "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 | |
| 63 | struct 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 | |
| 82 | private: |
| 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 Yeh | 9c6dbd5 | 2016-12-22 14:55:02 -0800 | [diff] [blame] | 95 | const sp<ICameraDeviceCallback> mCallback; |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 96 | // Stream ID -> Camera3Stream cache |
| 97 | std::map<int, Camera3Stream> mStreamMap; |
Yin-Chia Yeh | 9c6dbd5 | 2016-12-22 14:55:02 -0800 | [diff] [blame] | 98 | |
| 99 | mutable Mutex mInflightLock; // protecting mInflightBuffers and mCirculatingBuffers |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 100 | // (streamID, frameNumber) -> inflight buffer cache |
Yin-Chia Yeh | 9c6dbd5 | 2016-12-22 14:55:02 -0800 | [diff] [blame] | 101 | std::map<std::pair<int, uint32_t>, camera3_stream_buffer_t> mInflightBuffers; |
| 102 | |
Yin-Chia Yeh | 9c6dbd5 | 2016-12-22 14:55:02 -0800 | [diff] [blame] | 103 | // buffers currently ciculating between HAL and camera service |
Yin-Chia Yeh | d926f93 | 2017-01-09 15:21:11 -0800 | [diff] [blame] | 104 | // key: bufferId sent via HIDL interface |
Yin-Chia Yeh | 9c6dbd5 | 2016-12-22 14:55:02 -0800 | [diff] [blame] | 105 | // 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 Yeh | d926f93 | 2017-01-09 15:21:11 -0800 | [diff] [blame] | 108 | typedef std::unordered_map<uint64_t, buffer_handle_t> CirculatingBuffers; |
Yin-Chia Yeh | 9c6dbd5 | 2016-12-22 14:55:02 -0800 | [diff] [blame] | 109 | // Stream ID -> circulating buffers map |
| 110 | std::map<int, CirculatingBuffers> mCirculatingBuffers; |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 111 | |
| 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 Yeh | 9c6dbd5 | 2016-12-22 14:55:02 -0800 | [diff] [blame] | 118 | Status importRequest( |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 119 | const CaptureRequest& request, |
Yin-Chia Yeh | 9c6dbd5 | 2016-12-22 14:55:02 -0800 | [diff] [blame] | 120 | hidl_vec<buffer_handle_t*>& allBufPtrs, |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 121 | hidl_vec<int>& allFences); |
| 122 | |
Yin-Chia Yeh | 9c6dbd5 | 2016-12-22 14:55:02 -0800 | [diff] [blame] | 123 | static void cleanupInflightFences( |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 124 | hidl_vec<int>& allFences, size_t numFences); |
| 125 | |
Emilian Peev | 98014ff | 2017-02-02 16:20:12 +0000 | [diff] [blame] | 126 | void cleanupBuffersLocked(int id); |
| 127 | |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 128 | /** |
| 129 | * Static callback forwarding methods from HAL to instance |
| 130 | */ |
| 131 | static callbacks_process_capture_result_t sProcessCaptureResult; |
| 132 | static callbacks_notify_t sNotify; |
| 133 | }; |
| 134 | |
| 135 | } // namespace implementation |
| 136 | } // namespace V3_2 |
| 137 | } // namespace device |
| 138 | } // namespace camera |
| 139 | } // namespace hardware |
| 140 | } // namespace android |
| 141 | |
| 142 | #endif // ANDROID_HARDWARE_CAMERA_DEVICE_V3_2_CAMERADEVICE3SESSION_H |