1 /*
2  * Copyright (C) 2017-2018 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_4_CAMERADEVICE3SESSION_H
18 #define ANDROID_HARDWARE_CAMERA_DEVICE_V3_4_CAMERADEVICE3SESSION_H
19 
20 #include <android/hardware/camera/device/3.2/ICameraDevice.h>
21 #include <android/hardware/camera/device/3.4/ICameraDeviceSession.h>
22 #include <android/hardware/camera/device/3.4/ICameraDeviceCallback.h>
23 #include <../../3.3/default/CameraDeviceSession.h>
24 #include <../../3.3/default/include/convert.h>
25 #include <fmq/MessageQueue.h>
26 #include <hidl/MQDescriptor.h>
27 #include <hidl/Status.h>
28 #include <deque>
29 #include <map>
30 #include <unordered_set>
31 #include <unordered_map>
32 #include "CameraMetadata.h"
33 #include "HandleImporter.h"
34 #include "hardware/camera3.h"
35 #include "hardware/camera_common.h"
36 #include "utils/Mutex.h"
37 
38 namespace android {
39 namespace hardware {
40 namespace camera {
41 namespace device {
42 namespace V3_4 {
43 namespace implementation {
44 
45 using namespace ::android::hardware::camera::device;
46 using ::android::hardware::camera::device::V3_2::CaptureRequest;
47 using ::android::hardware::camera::device::V3_2::StreamType;
48 using ::android::hardware::camera::device::V3_4::StreamConfiguration;
49 using ::android::hardware::camera::device::V3_4::HalStreamConfiguration;
50 using ::android::hardware::camera::device::V3_4::ICameraDeviceSession;
51 using ::android::hardware::camera::device::V3_4::ICameraDeviceCallback;
52 using ::android::hardware::camera::common::V1_0::Status;
53 using ::android::hardware::camera::common::V1_0::helper::HandleImporter;
54 using ::android::hardware::kSynchronizedReadWrite;
55 using ::android::hardware::MessageQueue;
56 using ::android::hardware::MQDescriptorSync;
57 using ::android::hardware::Return;
58 using ::android::hardware::Void;
59 using ::android::hardware::hidl_vec;
60 using ::android::hardware::hidl_string;
61 using ::android::sp;
62 using ::android::Mutex;
63 
64 struct CameraDeviceSession : public V3_3::implementation::CameraDeviceSession {
65 
66     CameraDeviceSession(camera3_device_t*,
67             const camera_metadata_t* deviceInfo,
68             const sp<V3_2::ICameraDeviceCallback>&);
69     virtual ~CameraDeviceSession();
70 
getInterfaceCameraDeviceSession71     virtual sp<V3_2::ICameraDeviceSession> getInterface() override {
72         return new TrampolineSessionInterface_3_4(this);
73     }
74 
75 protected:
76     // Methods from v3.3 and earlier will trampoline to inherited implementation
77 
78     Return<void> configureStreams_3_4(
79             const StreamConfiguration& requestedConfiguration,
80             ICameraDeviceSession::configureStreams_3_4_cb _hidl_cb);
81 
82     bool preProcessConfigurationLocked_3_4(
83             const StreamConfiguration& requestedConfiguration, bool useOverriddenFields,
84             camera3_stream_configuration_t *stream_list /*out*/,
85             hidl_vec<camera3_stream_t*> *streams /*out*/);
86     void postProcessConfigurationLocked_3_4(const StreamConfiguration& requestedConfiguration);
87     void postProcessConfigurationFailureLocked_3_4(
88             const StreamConfiguration& requestedConfiguration);
89 
90     void configureStreams_3_4_Impl(
91             const StreamConfiguration& requestedConfiguration,
92             ICameraDeviceSession::configureStreams_3_4_cb _hidl_cb,
93             // Optional argument for ICameraDeviceSession@3.5 impl
94             uint32_t streamConfigCounter = 0, bool useOverriddenFields = true);
95 
96     Return<void> processCaptureRequest_3_4(
97             const hidl_vec<V3_4::CaptureRequest>& requests,
98             const hidl_vec<V3_2::BufferCache>& cachesToRemove,
99             ICameraDeviceSession::processCaptureRequest_3_4_cb _hidl_cb);
100     Status processOneCaptureRequest_3_4(const V3_4::CaptureRequest& request);
101 
102     std::map<int, std::string> mPhysicalCameraIdMap;
103 
104     static V3_2::implementation::callbacks_process_capture_result_t sProcessCaptureResult_3_4;
105     static V3_2::implementation::callbacks_notify_t sNotify_3_4;
106 
107     class ResultBatcher_3_4 : public V3_3::implementation::CameraDeviceSession::ResultBatcher {
108     public:
109         ResultBatcher_3_4(const sp<V3_2::ICameraDeviceCallback>& callback);
110         void processCaptureResult_3_4(CaptureResult& result);
111     private:
112         void freeReleaseFences_3_4(hidl_vec<CaptureResult>&);
113         void processOneCaptureResult_3_4(CaptureResult& result);
114         void invokeProcessCaptureResultCallback_3_4(hidl_vec<CaptureResult> &results,
115                 bool tryWriteFmq);
116 
117         sp<ICameraDeviceCallback> mCallback_3_4;
118     } mResultBatcher_3_4;
119 
120     // Whether this camera device session is created with version 3.4 callback.
121     bool mHasCallback_3_4;
122 
123     // Physical camera ids for the logical multi-camera. Empty if this
124     // is not a logical multi-camera.
125     std::unordered_set<std::string> mPhysicalCameraIds;
126 
127     Mutex    mStreamConfigCounterLock;
128     uint32_t mStreamConfigCounter = 1;
129 
130 private:
131 
132     struct TrampolineSessionInterface_3_4 : public ICameraDeviceSession {
TrampolineSessionInterface_3_4CameraDeviceSession::TrampolineSessionInterface_3_4133         TrampolineSessionInterface_3_4(sp<CameraDeviceSession> parent) :
134                 mParent(parent) {}
135 
constructDefaultRequestSettingsCameraDeviceSession::TrampolineSessionInterface_3_4136         virtual Return<void> constructDefaultRequestSettings(
137                 V3_2::RequestTemplate type,
138                 V3_3::ICameraDeviceSession::constructDefaultRequestSettings_cb _hidl_cb) override {
139             return mParent->constructDefaultRequestSettings(type, _hidl_cb);
140         }
141 
configureStreamsCameraDeviceSession::TrampolineSessionInterface_3_4142         virtual Return<void> configureStreams(
143                 const V3_2::StreamConfiguration& requestedConfiguration,
144                 V3_3::ICameraDeviceSession::configureStreams_cb _hidl_cb) override {
145             return mParent->configureStreams(requestedConfiguration, _hidl_cb);
146         }
147 
processCaptureRequest_3_4CameraDeviceSession::TrampolineSessionInterface_3_4148         virtual Return<void> processCaptureRequest_3_4(const hidl_vec<V3_4::CaptureRequest>& requests,
149                 const hidl_vec<V3_2::BufferCache>& cachesToRemove,
150                 ICameraDeviceSession::processCaptureRequest_3_4_cb _hidl_cb) override {
151             return mParent->processCaptureRequest_3_4(requests, cachesToRemove, _hidl_cb);
152         }
153 
processCaptureRequestCameraDeviceSession::TrampolineSessionInterface_3_4154         virtual Return<void> processCaptureRequest(const hidl_vec<V3_2::CaptureRequest>& requests,
155                 const hidl_vec<V3_2::BufferCache>& cachesToRemove,
156                 V3_3::ICameraDeviceSession::processCaptureRequest_cb _hidl_cb) override {
157             return mParent->processCaptureRequest(requests, cachesToRemove, _hidl_cb);
158         }
159 
getCaptureRequestMetadataQueueCameraDeviceSession::TrampolineSessionInterface_3_4160         virtual Return<void> getCaptureRequestMetadataQueue(
161                 V3_3::ICameraDeviceSession::getCaptureRequestMetadataQueue_cb _hidl_cb) override  {
162             return mParent->getCaptureRequestMetadataQueue(_hidl_cb);
163         }
164 
getCaptureResultMetadataQueueCameraDeviceSession::TrampolineSessionInterface_3_4165         virtual Return<void> getCaptureResultMetadataQueue(
166                 V3_3::ICameraDeviceSession::getCaptureResultMetadataQueue_cb _hidl_cb) override  {
167             return mParent->getCaptureResultMetadataQueue(_hidl_cb);
168         }
169 
flushCameraDeviceSession::TrampolineSessionInterface_3_4170         virtual Return<Status> flush() override {
171             return mParent->flush();
172         }
173 
closeCameraDeviceSession::TrampolineSessionInterface_3_4174         virtual Return<void> close() override {
175             return mParent->close();
176         }
177 
configureStreams_3_3CameraDeviceSession::TrampolineSessionInterface_3_4178         virtual Return<void> configureStreams_3_3(
179                 const V3_2::StreamConfiguration& requestedConfiguration,
180                 configureStreams_3_3_cb _hidl_cb) override {
181             return mParent->configureStreams_3_3(requestedConfiguration, _hidl_cb);
182         }
183 
configureStreams_3_4CameraDeviceSession::TrampolineSessionInterface_3_4184         virtual Return<void> configureStreams_3_4(
185                 const StreamConfiguration& requestedConfiguration,
186                 configureStreams_3_4_cb _hidl_cb) override {
187             return mParent->configureStreams_3_4(requestedConfiguration, _hidl_cb);
188         }
189 
190     private:
191         sp<CameraDeviceSession> mParent;
192     };
193 };
194 
195 }  // namespace implementation
196 }  // namespace V3_4
197 }  // namespace device
198 }  // namespace camera
199 }  // namespace hardware
200 }  // namespace android
201 
202 #endif  // ANDROID_HARDWARE_CAMERA_DEVICE_V3_4_CAMERADEVICE3SESSION_H
203