blob: 1d0c057a37fe0f1402e2c5dbf1b0e307e6ecd8ac [file] [log] [blame]
Avichal Rakeshe1857f82022-06-08 17:47:23 -07001/*
2 * Copyright (C) 2022 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 HARDWARE_INTERFACES_CAMERA_DEVICE_DEFAULT_EXTERNALCAMERAOFFLINESESSION_H_
18#define HARDWARE_INTERFACES_CAMERA_DEVICE_DEFAULT_EXTERNALCAMERAOFFLINESESSION_H_
19
20#include <ExternalCameraDeviceSession.h>
21#include <ExternalCameraUtils.h>
22#include <aidl/android/hardware/camera/common/Status.h>
23#include <aidl/android/hardware/camera/device/BnCameraOfflineSession.h>
24#include <aidl/android/hardware/camera/device/Stream.h>
25#include <fmq/AidlMessageQueue.h>
26#include <utils/RefBase.h>
27#include <deque>
28
29namespace android {
30namespace hardware {
31namespace camera {
32namespace device {
33namespace implementation {
34
35using ::aidl::android::hardware::camera::common::Status;
36using ::aidl::android::hardware::camera::device::BnCameraOfflineSession;
37using ::aidl::android::hardware::camera::device::ICameraDeviceCallback;
38using ::aidl::android::hardware::camera::device::Stream;
39using ::aidl::android::hardware::common::fmq::MQDescriptor;
40using ::aidl::android::hardware::common::fmq::SynchronizedReadWrite;
41
Avichal Rakesh740c2562022-12-13 13:25:24 -080042class ExternalCameraOfflineSession final : public BnCameraOfflineSession,
43 public virtual RefBase,
44 public virtual OutputThreadInterface {
Avichal Rakeshe1857f82022-06-08 17:47:23 -070045 public:
46 ExternalCameraOfflineSession(const CroppingType& croppingType,
47 const common::V1_0::helper::CameraMetadata& chars,
48 const std::string& cameraId, const std::string& exifMake,
49 const std::string& exifModel, uint32_t blobBufferSize,
50 bool afTrigger, const std::vector<Stream>& offlineStreams,
51 std::deque<std::shared_ptr<HalRequest>>& offlineReqs,
52 const std::map<int, CirculatingBuffers>& circulatingBuffers);
53
54 ~ExternalCameraOfflineSession() override;
55
56 bool initialize();
57
58 // Methods from OutputThreadInterface
59 Status importBuffer(int32_t streamId, uint64_t bufId, buffer_handle_t buf,
60 /*out*/ buffer_handle_t** outBufPtr) override;
61
62 Status processCaptureResult(std::shared_ptr<HalRequest>&) override;
63
64 Status processCaptureRequestError(const std::shared_ptr<HalRequest>&,
65 /*out*/ std::vector<NotifyMsg>* msgs,
66 /*out*/ std::vector<CaptureResult>* results) override;
67
68 ssize_t getJpegBufferSize(int32_t width, int32_t height) const override;
69
70 void notifyError(int32_t frameNumber, int32_t streamId, ErrorCode ec) override;
71 // End of OutputThreadInterface methods
72
73 ScopedAStatus setCallback(const std::shared_ptr<ICameraDeviceCallback>& in_cb) override;
74 ScopedAStatus getCaptureResultMetadataQueue(
75 MQDescriptor<int8_t, SynchronizedReadWrite>* _aidl_return) override;
76 ScopedAStatus close() override;
77
78 private:
79 class OutputThread : public ExternalCameraDeviceSession::OutputThread {
80 public:
81 OutputThread(std::weak_ptr<OutputThreadInterface> parent, CroppingType ct,
82 const common::V1_0::helper::CameraMetadata& chars,
83 std::shared_ptr<ExternalCameraDeviceSession::BufferRequestThread> bufReqThread,
84 std::deque<std::shared_ptr<HalRequest>>& offlineReqs)
85 : ExternalCameraDeviceSession::OutputThread(std::move(parent), ct, chars,
86 std::move(bufReqThread)),
87 mOfflineReqs(offlineReqs) {}
88
89 bool threadLoop() override;
90
91 protected:
92 std::deque<std::shared_ptr<HalRequest>> mOfflineReqs;
93 }; // OutputThread
94
95 status_t fillCaptureResult(common::V1_0::helper::CameraMetadata md, nsecs_t timestamp);
96 void invokeProcessCaptureResultCallback(std::vector<CaptureResult>& results, bool tryWriteFmq);
97 void initOutputThread();
98 void cleanupBuffersLocked(int32_t id);
99
100 // Protect (most of) HIDL interface methods from synchronized-entering
101 mutable Mutex mInterfaceLock;
102
103 mutable Mutex mLock; // Protect all data members except otherwise noted
104
105 bool mClosed = false;
106 const CroppingType mCroppingType;
107 const common::V1_0::helper::CameraMetadata mChars;
108 const std::string mCameraId;
109 const std::string mExifMake;
110 const std::string mExifModel;
111 const uint32_t mBlobBufferSize;
112
113 std::mutex mAfTriggerLock; // protect mAfTrigger
114 bool mAfTrigger;
115
116 const std::vector<Stream> mOfflineStreams;
117 std::deque<std::shared_ptr<HalRequest>> mOfflineReqs;
118
119 // Protect mCirculatingBuffers, must not lock mLock after acquiring this lock
120 mutable Mutex mCbsLock;
121 std::map<int, CirculatingBuffers> mCirculatingBuffers;
122
123 static HandleImporter sHandleImporter;
124
125 using ResultMetadataQueue = AidlMessageQueue<int8_t, SynchronizedReadWrite>;
126 std::shared_ptr<ResultMetadataQueue> mResultMetadataQueue;
127
128 // Protect against invokeProcessCaptureResultCallback()
129 Mutex mProcessCaptureResultLock;
130
131 std::shared_ptr<ICameraDeviceCallback> mCallback;
132
133 std::shared_ptr<ExternalCameraDeviceSession::BufferRequestThread> mBufferRequestThread;
134 std::shared_ptr<OutputThread> mOutputThread;
135};
136
137} // namespace implementation
138} // namespace device
139} // namespace camera
140} // namespace hardware
141} // namespace android
142
143#endif // HARDWARE_INTERFACES_CAMERA_DEVICE_DEFAULT_EXTERNALCAMERAOFFLINESESSION_H_