• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 #define LOG_TAG "AidlCamera3-OffLnSsn"
18 #define ATRACE_TAG ATRACE_TAG_CAMERA
19 //#define LOG_NDEBUG 0
20 //#define LOG_NNDEBUG 0  // Per-frame verbose logging
21 
22 #ifdef LOG_NNDEBUG
23 #define ALOGVV(...) ALOGV(__VA_ARGS__)
24 #else
25 #define ALOGVV(...) ((void)0)
26 #endif
27 
28 #include <inttypes.h>
29 
30 #include <utils/Trace.h>
31 
32 #include <android/hardware/ICameraService.h>
33 #include <android/hardware/camera2/ICameraDeviceCallbacks.h>
34 #include <android/binder_ibinder_platform.h>
35 #include <camera/StringUtils.h>
36 
37 #include "device3/aidl/AidlCamera3OfflineSession.h"
38 #include "device3/Camera3OutputStream.h"
39 #include "device3/aidl/AidlCamera3OutputUtils.h"
40 #include "device3/Camera3InputStream.h"
41 #include "device3/Camera3SharedOutputStream.h"
42 #include "utils/CameraTraces.h"
43 
44 using namespace android::camera3;
45 using namespace aidl::android::hardware;
46 
47 namespace android {
48 
49 
~AidlCamera3OfflineSession()50 AidlCamera3OfflineSession::~AidlCamera3OfflineSession() {
51     ATRACE_CALL();
52     ALOGV("%s: Tearing down aidl offline session for camera id %s", __FUNCTION__, mId.c_str());
53     Camera3OfflineSession::disconnectImpl();
54 }
55 
initialize(wp<NotificationListener> listener)56 status_t AidlCamera3OfflineSession::initialize(wp<NotificationListener> listener) {
57     ATRACE_CALL();
58 
59     if (mSession == nullptr) {
60         ALOGE("%s: AIDL session is null!", __FUNCTION__);
61         return DEAD_OBJECT;
62     }
63 
64     {
65         std::lock_guard<std::mutex> lock(mLock);
66 
67         mListener = listener;
68 
69         // setup result FMQ
70         std::unique_ptr<AidlResultMetadataQueue>& resQueue = mResultMetadataQueue;
71         ::aidl::android::hardware::common::fmq::MQDescriptor<
72             int8_t, ::aidl::android::hardware::common::fmq::SynchronizedReadWrite> desc;
73         ::ndk::ScopedAStatus resultQueueRet = mSession->getCaptureResultMetadataQueue(&desc);
74         if (!resultQueueRet.isOk()) {
75             ALOGE("Transaction error when getting result metadata queue from camera session: %s",
76                     resultQueueRet.getMessage());
77             return DEAD_OBJECT;
78         }
79         resQueue = std::make_unique<AidlResultMetadataQueue>(desc);
80         if (!resQueue->isValid() || resQueue->availableToWrite() <= 0) {
81             ALOGE("HAL returns empty result metadata fmq, not use it");
82             resQueue = nullptr;
83             // Don't use resQueue onwards.
84         }
85 
86         mStatus = STATUS_ACTIVE;
87     }
88 
89     mSession->setCallback(mCallbacks);
90 
91     return OK;
92 }
93 
processCaptureResult(const std::vector<camera::device::CaptureResult> & results)94 ::ndk::ScopedAStatus AidlCamera3OfflineSession::AidlCameraDeviceCallbacks::processCaptureResult(
95         const std::vector<camera::device::CaptureResult>& results) {
96     sp<AidlCamera3OfflineSession> p = mParent.promote();
97     if (p == nullptr) {
98         ALOGE("%s Parent AidlCameraDevice not alive, can't process callbacks", __FUNCTION__);
99         return ::ndk::ScopedAStatus::ok();
100     }
101     return p->processCaptureResult(results);
102 }
103 
processCaptureResult(const std::vector<camera::device::CaptureResult> & results)104 ::ndk::ScopedAStatus AidlCamera3OfflineSession::processCaptureResult(
105         const std::vector<camera::device::CaptureResult>& results) {
106     sp<NotificationListener> listener;
107     {
108         std::lock_guard<std::mutex> lock(mLock);
109         if (mStatus != STATUS_ACTIVE) {
110             ALOGE("%s called in wrong state %d", __FUNCTION__, mStatus);
111             return ::ndk::ScopedAStatus::ok();
112         }
113         listener = mListener.promote();
114     }
115 
116     std::string activePhysicalId(""); // Unused
117     AidlCaptureOutputStates states {
118       { mId,
119         mOfflineReqsLock, mLastCompletedRegularFrameNumber,
120         mLastCompletedReprocessFrameNumber, mLastCompletedZslFrameNumber,
121         mOfflineReqs, mOutputLock, mResultQueue, mResultSignal,
122         mNextShutterFrameNumber,
123         mNextReprocessShutterFrameNumber, mNextZslStillShutterFrameNumber,
124         mNextResultFrameNumber,
125         mNextReprocessResultFrameNumber, mNextZslStillResultFrameNumber,
126         mUseHalBufManager, mHalBufManagedStreamIds, mUsePartialResult, mNeedFixupMonochromeTags,
127         mNumPartialResults, mVendorTagId, mDeviceInfo, mPhysicalDeviceInfoMap,
128         mDistortionMappers, mZoomRatioMappers, mRotateAndCropMappers,
129         mTagMonitor, mInputStream, mOutputStreams, mSessionStatsBuilder, listener, *this,
130         *this, mBufferRecords, /*legacyClient*/ false, mMinExpectedDuration, mIsFixedFps,
131         hardware::ICameraService::ROTATION_OVERRIDE_NONE, activePhysicalId}, mResultMetadataQueue
132     };
133 
134     std::lock_guard<std::mutex> lock(mProcessCaptureResultLock);
135     for (const auto& result : results) {
136         processOneCaptureResultLocked(states, result, result.physicalCameraMetadata);
137     }
138     return ::ndk::ScopedAStatus::ok();
139 }
140 
notify(const std::vector<camera::device::NotifyMsg> & msgs)141 ::ndk::ScopedAStatus AidlCamera3OfflineSession::AidlCameraDeviceCallbacks::notify(
142         const std::vector<camera::device::NotifyMsg>& msgs) {
143     sp<AidlCamera3OfflineSession> p = mParent.promote();
144     if (p == nullptr) {
145         ALOGE("%s Parent AidlCameraDevice not alive, can't process callbacks", __FUNCTION__);
146         return ::ndk::ScopedAStatus::ok();
147     }
148     return p->notify(msgs);
149 }
150 
notify(const std::vector<camera::device::NotifyMsg> & msgs)151 ::ndk::ScopedAStatus AidlCamera3OfflineSession::notify(
152         const std::vector<camera::device::NotifyMsg>& msgs) {
153     sp<NotificationListener> listener;
154     {
155         std::lock_guard<std::mutex> lock(mLock);
156         if (mStatus != STATUS_ACTIVE) {
157             ALOGE("%s called in wrong state %d", __FUNCTION__, mStatus);
158             return ::ndk::ScopedAStatus::ok();
159         }
160         listener = mListener.promote();
161     }
162 
163     std::string activePhysicalId(""); // Unused
164     AidlCaptureOutputStates states {
165       { mId,
166         mOfflineReqsLock, mLastCompletedRegularFrameNumber,
167         mLastCompletedReprocessFrameNumber, mLastCompletedZslFrameNumber,
168         mOfflineReqs, mOutputLock, mResultQueue, mResultSignal,
169         mNextShutterFrameNumber,
170         mNextReprocessShutterFrameNumber, mNextZslStillShutterFrameNumber,
171         mNextResultFrameNumber,
172         mNextReprocessResultFrameNumber, mNextZslStillResultFrameNumber,
173         mUseHalBufManager, mHalBufManagedStreamIds, mUsePartialResult, mNeedFixupMonochromeTags,
174         mNumPartialResults, mVendorTagId, mDeviceInfo, mPhysicalDeviceInfoMap,
175         mDistortionMappers, mZoomRatioMappers, mRotateAndCropMappers,
176         mTagMonitor, mInputStream, mOutputStreams, mSessionStatsBuilder, listener, *this,
177         *this, mBufferRecords, /*legacyClient*/ false, mMinExpectedDuration, mIsFixedFps,
178         hardware::ICameraService::ROTATION_OVERRIDE_NONE, activePhysicalId}, mResultMetadataQueue
179     };
180     for (const auto& msg : msgs) {
181         camera3::notify(states, msg, mSensorReadoutTimestampSupported);
182     }
183     return ::ndk::ScopedAStatus::ok();
184 }
185 
requestStreamBuffers(const std::vector<::aidl::android::hardware::camera::device::BufferRequest> & bufReqs,std::vector<::aidl::android::hardware::camera::device::StreamBufferRet> * buffers,::aidl::android::hardware::camera::device::BufferRequestStatus * status)186 ::ndk::ScopedAStatus AidlCamera3OfflineSession::AidlCameraDeviceCallbacks::requestStreamBuffers(
187         const std::vector<::aidl::android::hardware::camera::device::BufferRequest>& bufReqs,
188         std::vector<::aidl::android::hardware::camera::device::StreamBufferRet>* buffers,
189         ::aidl::android::hardware::camera::device::BufferRequestStatus* status) {
190     sp<AidlCamera3OfflineSession> p = mParent.promote();
191     if (p == nullptr) {
192         ALOGE("%s Parent AidlCameraDevice not alive, can't process callbacks", __FUNCTION__);
193         return ::ndk::ScopedAStatus::ok();
194     }
195     return p->requestStreamBuffers(bufReqs, buffers, status);
196 }
197 
requestStreamBuffers(const std::vector<::aidl::android::hardware::camera::device::BufferRequest> & bufReqs,std::vector<::aidl::android::hardware::camera::device::StreamBufferRet> * buffers,::aidl::android::hardware::camera::device::BufferRequestStatus * status)198 ::ndk::ScopedAStatus AidlCamera3OfflineSession::requestStreamBuffers(
199         const std::vector<::aidl::android::hardware::camera::device::BufferRequest>& bufReqs,
200         std::vector<::aidl::android::hardware::camera::device::StreamBufferRet>* buffers,
201         ::aidl::android::hardware::camera::device::BufferRequestStatus* status) {
202 
203     {
204         std::lock_guard<std::mutex> lock(mLock);
205         if (mStatus != STATUS_ACTIVE) {
206             ALOGE("%s called in wrong state %d", __FUNCTION__, mStatus);
207             return ::ndk::ScopedAStatus::ok();
208         }
209     }
210 
211     RequestBufferStates states {
212         mId, mRequestBufferInterfaceLock, mUseHalBufManager,
213         mHalBufManagedStreamIds, mOutputStreams, mSessionStatsBuilder,
214         *this, mBufferRecords, *this};
215     camera3::requestStreamBuffers(states, bufReqs, buffers, status);
216     return ::ndk::ScopedAStatus::ok();
217 }
218 
returnStreamBuffers(const std::vector<camera::device::StreamBuffer> & buffers)219 ::ndk::ScopedAStatus AidlCamera3OfflineSession::AidlCameraDeviceCallbacks::returnStreamBuffers(
220         const std::vector<camera::device::StreamBuffer>& buffers) {
221     sp<AidlCamera3OfflineSession> p = mParent.promote();
222     if (p == nullptr) {
223         ALOGE("%s Parent AidlCameraDevice not alive, can't process callbacks", __FUNCTION__);
224         return ::ndk::ScopedAStatus::ok();
225     }
226     return p->returnStreamBuffers(buffers);
227 }
228 
createBinder()229 ::ndk::SpAIBinder AidlCamera3OfflineSession::AidlCameraDeviceCallbacks::createBinder() {
230     auto binder = BnCameraDeviceCallback::createBinder();
231     AIBinder_setInheritRt(binder.get(), /*inheritRt*/ true);
232     return binder;
233 }
234 
returnStreamBuffers(const std::vector<camera::device::StreamBuffer> & buffers)235 ::ndk::ScopedAStatus AidlCamera3OfflineSession::returnStreamBuffers(
236         const std::vector<camera::device::StreamBuffer>& buffers) {
237     {
238         std::lock_guard<std::mutex> lock(mLock);
239         if (mStatus != STATUS_ACTIVE) {
240             ALOGE("%s called in wrong state %d", __FUNCTION__, mStatus);
241             return ::ndk::ScopedAStatus::ok();
242         }
243     }
244 
245     ReturnBufferStates states {
246         mId, mUseHalBufManager, mHalBufManagedStreamIds, mOutputStreams, mSessionStatsBuilder,
247         mBufferRecords};
248 
249     camera3::returnStreamBuffers(states, buffers);
250     return ::ndk::ScopedAStatus::ok();
251 }
252 
closeSessionLocked()253 void AidlCamera3OfflineSession::closeSessionLocked() {
254     if (mSession != nullptr) {
255         auto err = mSession->close();
256         if (!err.isOk()) {
257             ALOGE("%s: Close transaction error: %s", __FUNCTION__, err.getDescription().c_str());
258         }
259     }
260 }
261 
releaseSessionLocked()262 void AidlCamera3OfflineSession::releaseSessionLocked() {
263     mSession.reset();
264 }
265 
266 }; // namespace android
267