blob: 6c6ddbd7872e7d00a25256e67c152baf1a9d4a15 [file] [log] [blame]
Igor Murashkine7ee7632013-06-11 18:10:18 -07001/*
Shuzhen Wangc28189a2017-11-27 23:05:10 -08002 * Copyright (C) 2013-2018 The Android Open Source Project
Igor Murashkine7ee7632013-06-11 18:10:18 -07003 *
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 "CameraDeviceClient"
18#define ATRACE_TAG ATRACE_TAG_CAMERA
Jianing Weicb0652e2014-03-12 18:29:36 -070019//#define LOG_NDEBUG 0
Igor Murashkine7ee7632013-06-11 18:10:18 -070020
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070021#include <cutils/properties.h>
Jayant Chowdhary12361932018-08-27 14:46:13 -070022#include <utils/CameraThreadState.h>
Igor Murashkine7ee7632013-06-11 18:10:18 -070023#include <utils/Log.h>
Colin Crossb8a9dbb2020-08-27 04:12:26 +000024#include <utils/SessionConfigurationUtils.h>
Igor Murashkine7ee7632013-06-11 18:10:18 -070025#include <utils/Trace.h>
Igor Murashkine7ee7632013-06-11 18:10:18 -070026#include <gui/Surface.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070027#include <camera/camera2/CaptureRequest.h>
Ruben Brunk5698d442014-06-18 10:39:40 -070028#include <camera/CameraUtils.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070029
30#include "common/CameraDeviceBase.h"
Emilian Peev35ae8262018-11-08 13:11:32 +000031#include "device3/Camera3Device.h"
32#include "device3/Camera3OutputStream.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070033#include "api2/CameraDeviceClient.h"
Shuzhen Wang316781a2020-08-18 18:11:01 -070034#include "utils/CameraServiceProxyWrapper.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070035
Emilian Peev00420d22018-02-05 21:33:13 +000036#include <camera_metadata_hidden.h>
37
Emilian Peev538c90e2018-12-17 18:03:19 +000038#include "DepthCompositeStream.h"
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -080039#include "HeicCompositeStream.h"
Emilian Peev538c90e2018-12-17 18:03:19 +000040
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080041// Convenience methods for constructing binder::Status objects for error returns
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070042
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080043#define STATUS_ERROR(errorCode, errorString) \
44 binder::Status::fromServiceSpecificError(errorCode, \
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -080045 String8::format("%s:%d: %s", __FUNCTION__, __LINE__, errorString))
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080046
47#define STATUS_ERROR_FMT(errorCode, errorString, ...) \
48 binder::Status::fromServiceSpecificError(errorCode, \
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -080049 String8::format("%s:%d: " errorString, __FUNCTION__, __LINE__, \
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080050 __VA_ARGS__))
Igor Murashkine7ee7632013-06-11 18:10:18 -070051
52namespace android {
53using namespace camera2;
Austin Borgerea931242021-12-13 23:10:41 +000054using namespace camera3;
Emilian Peevf4816702020-04-03 15:44:51 -070055using camera3::camera_stream_rotation_t::CAMERA_STREAM_ROTATION_0;
Igor Murashkine7ee7632013-06-11 18:10:18 -070056
57CameraDeviceClientBase::CameraDeviceClientBase(
58 const sp<CameraService>& cameraService,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080059 const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback,
Igor Murashkine7ee7632013-06-11 18:10:18 -070060 const String16& clientPackageName,
Jayant Chowdharyeb0169f2022-01-31 00:00:02 -080061 bool systemNativeClient,
Jooyung Han3f9a3b42020-01-23 12:27:18 +090062 const std::optional<String16>& clientFeatureId,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080063 const String8& cameraId,
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -080064 int api1CameraId,
Igor Murashkine7ee7632013-06-11 18:10:18 -070065 int cameraFacing,
Emilian Peev8b64f282021-03-25 16:49:57 -070066 int sensorOrientation,
Igor Murashkine7ee7632013-06-11 18:10:18 -070067 int clientPid,
68 uid_t clientUid,
69 int servicePid) :
Eino-Ville Talvalae992e752014-11-07 16:17:48 -080070 BasicClient(cameraService,
Marco Nelissenf8880202014-11-14 07:58:25 -080071 IInterface::asBinder(remoteCallback),
Eino-Ville Talvalae992e752014-11-07 16:17:48 -080072 clientPackageName,
Jayant Chowdharyeb0169f2022-01-31 00:00:02 -080073 systemNativeClient,
Philip P. Moltmann9e648f62019-11-04 12:52:45 -080074 clientFeatureId,
Eino-Ville Talvalae992e752014-11-07 16:17:48 -080075 cameraId,
76 cameraFacing,
Emilian Peev8b64f282021-03-25 16:49:57 -070077 sensorOrientation,
Eino-Ville Talvalae992e752014-11-07 16:17:48 -080078 clientPid,
79 clientUid,
80 servicePid),
Igor Murashkine7ee7632013-06-11 18:10:18 -070081 mRemoteCallback(remoteCallback) {
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -080082 // We don't need it for API2 clients, but Camera2ClientBase requires it.
83 (void) api1CameraId;
Igor Murashkine7ee7632013-06-11 18:10:18 -070084}
Igor Murashkine7ee7632013-06-11 18:10:18 -070085
86// Interface used by CameraService
87
88CameraDeviceClient::CameraDeviceClient(const sp<CameraService>& cameraService,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080089 const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback,
90 const String16& clientPackageName,
Jayant Chowdharyeb0169f2022-01-31 00:00:02 -080091 bool systemNativeClient,
Jooyung Han3f9a3b42020-01-23 12:27:18 +090092 const std::optional<String16>& clientFeatureId,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080093 const String8& cameraId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080094 int cameraFacing,
Emilian Peev8b64f282021-03-25 16:49:57 -070095 int sensorOrientation,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080096 int clientPid,
97 uid_t clientUid,
Shuzhen Wangd4abdf72021-05-28 11:22:50 -070098 int servicePid,
99 bool overrideForPerfClass) :
Jayant Chowdharyeb0169f2022-01-31 00:00:02 -0800100 Camera2ClientBase(cameraService, remoteCallback, clientPackageName, systemNativeClient,
101 clientFeatureId, cameraId, /*API1 camera ID*/ -1, cameraFacing, sensorOrientation,
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700102 clientPid, clientUid, servicePid, overrideForPerfClass),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700103 mInputStream(),
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700104 mStreamingRequestId(REQUEST_ID_NONE),
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700105 mRequestIdCounter(0),
106 mOverrideForPerfClass(overrideForPerfClass) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700107
108 ATRACE_CALL();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800109 ALOGI("CameraDeviceClient %s: Opened", cameraId.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700110}
111
Emilian Peevbd8c5032018-02-14 23:05:40 +0000112status_t CameraDeviceClient::initialize(sp<CameraProviderManager> manager,
113 const String8& monitorTags) {
114 return initializeImpl(manager, monitorTags);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800115}
116
117template<typename TProviderPtr>
Emilian Peevbd8c5032018-02-14 23:05:40 +0000118status_t CameraDeviceClient::initializeImpl(TProviderPtr providerPtr, const String8& monitorTags) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700119 ATRACE_CALL();
120 status_t res;
121
Emilian Peevbd8c5032018-02-14 23:05:40 +0000122 res = Camera2ClientBase::initialize(providerPtr, monitorTags);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700123 if (res != OK) {
124 return res;
125 }
126
127 String8 threadName;
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -0700128 mFrameProcessor = new FrameProcessorBase(mDevice);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800129 threadName = String8::format("CDU-%s-FrameProc", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700130 mFrameProcessor->run(threadName.string());
131
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800132 mFrameProcessor->registerListener(camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MIN_ID,
133 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MAX_ID,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -0800134 /*listener*/this,
Zhijun He25a0aef2014-06-25 11:40:02 -0700135 /*sendPartials*/true);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700136
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800137 const CameraMetadata &deviceInfo = mDevice->info();
138 camera_metadata_ro_entry_t physicalKeysEntry = deviceInfo.find(
Emilian Peev00420d22018-02-05 21:33:13 +0000139 ANDROID_REQUEST_AVAILABLE_PHYSICAL_CAMERA_REQUEST_KEYS);
140 if (physicalKeysEntry.count > 0) {
141 mSupportedPhysicalRequestKeys.insert(mSupportedPhysicalRequestKeys.begin(),
142 physicalKeysEntry.data.i32,
143 physicalKeysEntry.data.i32 + physicalKeysEntry.count);
144 }
145
Emilian Peev2295df72021-11-12 18:14:10 -0800146 auto entry = deviceInfo.find(ANDROID_REQUEST_AVAILABLE_CAPABILITIES);
147 mDynamicProfileMap.emplace(
148 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD,
149 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD);
150 if (entry.count > 0) {
Shuzhen Wangc8ab4522021-12-14 20:12:42 -0800151 const auto it = std::find(entry.data.u8, entry.data.u8 + entry.count,
Emilian Peev2295df72021-11-12 18:14:10 -0800152 ANDROID_REQUEST_AVAILABLE_CAPABILITIES_DYNAMIC_RANGE_TEN_BIT);
Shuzhen Wangc8ab4522021-12-14 20:12:42 -0800153 if (it != entry.data.u8 + entry.count) {
Emilian Peev2295df72021-11-12 18:14:10 -0800154 entry = deviceInfo.find(ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP);
Emilian Peevc81a7592022-02-14 17:38:18 -0800155 if (entry.count > 0 || ((entry.count % 3) != 0)) {
156 int64_t standardBitmap =
157 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD;
158 for (size_t i = 0; i < entry.count; i += 3) {
159 if (entry.data.i64[i] !=
Emilian Peev2295df72021-11-12 18:14:10 -0800160 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD) {
Emilian Peevc81a7592022-02-14 17:38:18 -0800161 mDynamicProfileMap.emplace(entry.data.i64[i], entry.data.i64[i+1]);
162 if ((entry.data.i64[i+1] == 0) || (entry.data.i64[i+1] &
Emilian Peev2295df72021-11-12 18:14:10 -0800163 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD)) {
Emilian Peevc81a7592022-02-14 17:38:18 -0800164 standardBitmap |= entry.data.i64[i];
Emilian Peev2295df72021-11-12 18:14:10 -0800165 }
166 } else {
Emilian Peevc81a7592022-02-14 17:38:18 -0800167 ALOGE("%s: Device %s includes unexpected profile entry: 0x%" PRIx64 "!",
168 __FUNCTION__, mCameraIdStr.c_str(), entry.data.i64[i]);
Emilian Peev2295df72021-11-12 18:14:10 -0800169 }
170 }
171 mDynamicProfileMap.emplace(
172 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD,
173 standardBitmap);
174 } else {
175 ALOGE("%s: Device %s supports 10-bit output but doesn't include a dynamic range"
176 " profile map!", __FUNCTION__, mCameraIdStr.c_str());
177 }
178 }
179 }
180
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700181 mProviderManager = providerPtr;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800182 // Cache physical camera ids corresponding to this device and also the high
183 // resolution sensors in this device + physical camera ids
184 mProviderManager->isLogicalCamera(mCameraIdStr.string(), &mPhysicalCameraIds);
185 if (isUltraHighResolutionSensor(mCameraIdStr)) {
186 mHighResolutionSensors.insert(mCameraIdStr.string());
187 }
188 for (auto &physicalId : mPhysicalCameraIds) {
189 if (isUltraHighResolutionSensor(String8(physicalId.c_str()))) {
190 mHighResolutionSensors.insert(physicalId.c_str());
191 }
192 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700193 return OK;
194}
195
196CameraDeviceClient::~CameraDeviceClient() {
197}
198
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800199binder::Status CameraDeviceClient::submitRequest(
200 const hardware::camera2::CaptureRequest& request,
201 bool streaming,
202 /*out*/
203 hardware::camera2::utils::SubmitInfo *submitInfo) {
204 std::vector<hardware::camera2::CaptureRequest> requestList = { request };
205 return submitRequestList(requestList, streaming, submitInfo);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700206}
207
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800208binder::Status CameraDeviceClient::insertGbpLocked(const sp<IGraphicBufferProducer>& gbp,
Emilian Peevf873aa52018-01-26 14:58:28 +0000209 SurfaceMap* outSurfaceMap, Vector<int32_t>* outputStreamIds, int32_t *currentStreamId) {
Emilian Peev538c90e2018-12-17 18:03:19 +0000210 int compositeIdx;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800211 int idx = mStreamMap.indexOfKey(IInterface::asBinder(gbp));
212
Emilian Peev2f5d6012022-01-19 16:16:50 -0800213 Mutex::Autolock l(mCompositeLock);
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800214 // Trying to submit request with surface that wasn't created
215 if (idx == NAME_NOT_FOUND) {
216 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
217 " we have not called createStream on",
218 __FUNCTION__, mCameraIdStr.string());
219 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
220 "Request targets Surface that is not part of current capture session");
Emilian Peev538c90e2018-12-17 18:03:19 +0000221 } else if ((compositeIdx = mCompositeStreamMap.indexOfKey(IInterface::asBinder(gbp)))
222 != NAME_NOT_FOUND) {
223 mCompositeStreamMap.valueAt(compositeIdx)->insertGbp(outSurfaceMap, outputStreamIds,
224 currentStreamId);
225 return binder::Status::ok();
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800226 }
227
228 const StreamSurfaceId& streamSurfaceId = mStreamMap.valueAt(idx);
229 if (outSurfaceMap->find(streamSurfaceId.streamId()) == outSurfaceMap->end()) {
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800230 outputStreamIds->push_back(streamSurfaceId.streamId());
231 }
232 (*outSurfaceMap)[streamSurfaceId.streamId()].push_back(streamSurfaceId.surfaceId());
233
234 ALOGV("%s: Camera %s: Appending output stream %d surface %d to request",
235 __FUNCTION__, mCameraIdStr.string(), streamSurfaceId.streamId(),
236 streamSurfaceId.surfaceId());
237
Emilian Peevf873aa52018-01-26 14:58:28 +0000238 if (currentStreamId != nullptr) {
239 *currentStreamId = streamSurfaceId.streamId();
240 }
241
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800242 return binder::Status::ok();
243}
244
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800245static std::list<int> getIntersection(const std::unordered_set<int> &streamIdsForThisCamera,
246 const Vector<int> &streamIdsForThisRequest) {
247 std::list<int> intersection;
248 for (auto &streamId : streamIdsForThisRequest) {
249 if (streamIdsForThisCamera.find(streamId) != streamIdsForThisCamera.end()) {
250 intersection.emplace_back(streamId);
251 }
252 }
253 return intersection;
254}
255
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800256binder::Status CameraDeviceClient::submitRequestList(
257 const std::vector<hardware::camera2::CaptureRequest>& requests,
258 bool streaming,
259 /*out*/
260 hardware::camera2::utils::SubmitInfo *submitInfo) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700261 ATRACE_CALL();
Mark Salyzyn50468412014-06-18 16:33:43 -0700262 ALOGV("%s-start of function. Request list size %zu", __FUNCTION__, requests.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700263
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800264 binder::Status res = binder::Status::ok();
265 status_t err;
266 if ( !(res = checkPidStatus(__FUNCTION__) ).isOk()) {
267 return res;
268 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700269
270 Mutex::Autolock icl(mBinderSerializationLock);
271
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800272 if (!mDevice.get()) {
273 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
274 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700275
276 if (requests.empty()) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800277 ALOGE("%s: Camera %s: Sent null request. Rejecting request.",
278 __FUNCTION__, mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800279 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Empty request list");
Jianing Wei90e59c92014-03-12 18:29:36 -0700280 }
281
Emilian Peevaebbe412018-01-15 13:53:24 +0000282 List<const CameraDeviceBase::PhysicalCameraSettingsList> metadataRequestList;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700283 std::list<const SurfaceMap> surfaceMapList;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800284 submitInfo->mRequestId = mRequestIdCounter;
Jianing Wei90e59c92014-03-12 18:29:36 -0700285 uint32_t loopCounter = 0;
286
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800287 for (auto&& request: requests) {
288 if (request.mIsReprocess) {
Chien-Yu Chened0412e2015-04-27 15:04:22 -0700289 if (!mInputStream.configured) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800290 ALOGE("%s: Camera %s: no input stream is configured.", __FUNCTION__,
291 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800292 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800293 "No input configured for camera %s but request is for reprocessing",
294 mCameraIdStr.string());
Chien-Yu Chened0412e2015-04-27 15:04:22 -0700295 } else if (streaming) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800296 ALOGE("%s: Camera %s: streaming reprocess requests not supported.", __FUNCTION__,
297 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800298 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
299 "Repeating reprocess requests not supported");
Emilian Peevaebbe412018-01-15 13:53:24 +0000300 } else if (request.mPhysicalCameraSettings.size() > 1) {
301 ALOGE("%s: Camera %s: reprocess requests not supported for "
302 "multiple physical cameras.", __FUNCTION__,
303 mCameraIdStr.string());
304 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
305 "Reprocess requests not supported for multiple cameras");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700306 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700307 }
308
Emilian Peevaebbe412018-01-15 13:53:24 +0000309 if (request.mPhysicalCameraSettings.empty()) {
310 ALOGE("%s: Camera %s: request doesn't contain any settings.", __FUNCTION__,
311 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800312 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
Emilian Peevaebbe412018-01-15 13:53:24 +0000313 "Request doesn't contain any settings");
314 }
315
316 //The first capture settings should always match the logical camera id
317 String8 logicalId(request.mPhysicalCameraSettings.begin()->id.c_str());
318 if (mDevice->getId() != logicalId) {
319 ALOGE("%s: Camera %s: Invalid camera request settings.", __FUNCTION__,
320 mCameraIdStr.string());
321 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
322 "Invalid camera request settings");
323 }
324
Emilian Peevaebbe412018-01-15 13:53:24 +0000325 if (request.mSurfaceList.isEmpty() && request.mStreamIdxList.size() == 0) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800326 ALOGE("%s: Camera %s: Requests must have at least one surface target. "
327 "Rejecting request.", __FUNCTION__, mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800328 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
329 "Request has no output targets");
Jianing Wei90e59c92014-03-12 18:29:36 -0700330 }
331
Jianing Wei90e59c92014-03-12 18:29:36 -0700332 /**
Shuzhen Wang0129d522016-10-30 22:43:41 -0700333 * Write in the output stream IDs and map from stream ID to surface ID
334 * which we calculate from the capture request's list of surface target
Jianing Wei90e59c92014-03-12 18:29:36 -0700335 */
Shuzhen Wang0129d522016-10-30 22:43:41 -0700336 SurfaceMap surfaceMap;
Jianing Wei90e59c92014-03-12 18:29:36 -0700337 Vector<int32_t> outputStreamIds;
Emilian Peevf873aa52018-01-26 14:58:28 +0000338 std::vector<std::string> requestedPhysicalIds;
Emilian Peevc81a7592022-02-14 17:38:18 -0800339 int64_t dynamicProfileBitmap = 0;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800340 if (request.mSurfaceList.size() > 0) {
Chih-Hung Hsieh3ef324d2018-12-11 11:48:12 -0800341 for (const sp<Surface>& surface : request.mSurfaceList) {
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800342 if (surface == 0) continue;
Jianing Wei90e59c92014-03-12 18:29:36 -0700343
Emilian Peevf873aa52018-01-26 14:58:28 +0000344 int32_t streamId;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800345 sp<IGraphicBufferProducer> gbp = surface->getIGraphicBufferProducer();
Emilian Peevf873aa52018-01-26 14:58:28 +0000346 res = insertGbpLocked(gbp, &surfaceMap, &outputStreamIds, &streamId);
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800347 if (!res.isOk()) {
348 return res;
349 }
Emilian Peevf873aa52018-01-26 14:58:28 +0000350
351 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
352 if (index >= 0) {
353 String8 requestedPhysicalId(
354 mConfiguredOutputs.valueAt(index).getPhysicalCameraId());
355 requestedPhysicalIds.push_back(requestedPhysicalId.string());
Emilian Peev2295df72021-11-12 18:14:10 -0800356 dynamicProfileBitmap |=
357 mConfiguredOutputs.valueAt(index).getDynamicRangeProfile();
Emilian Peevf873aa52018-01-26 14:58:28 +0000358 } else {
359 ALOGW("%s: Output stream Id not found among configured outputs!", __FUNCTION__);
360 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700361 }
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800362 } else {
363 for (size_t i = 0; i < request.mStreamIdxList.size(); i++) {
364 int streamId = request.mStreamIdxList.itemAt(i);
365 int surfaceIdx = request.mSurfaceIdxList.itemAt(i);
Jianing Wei90e59c92014-03-12 18:29:36 -0700366
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800367 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
368 if (index < 0) {
369 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
370 " we have not called createStream on: stream %d",
371 __FUNCTION__, mCameraIdStr.string(), streamId);
372 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
373 "Request targets Surface that is not part of current capture session");
374 }
375
376 const auto& gbps = mConfiguredOutputs.valueAt(index).getGraphicBufferProducers();
377 if ((size_t)surfaceIdx >= gbps.size()) {
378 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
379 " we have not called createStream on: stream %d, surfaceIdx %d",
380 __FUNCTION__, mCameraIdStr.string(), streamId, surfaceIdx);
381 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
382 "Request targets Surface has invalid surface index");
383 }
384
Emilian Peevf873aa52018-01-26 14:58:28 +0000385 res = insertGbpLocked(gbps[surfaceIdx], &surfaceMap, &outputStreamIds, nullptr);
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800386 if (!res.isOk()) {
387 return res;
388 }
Emilian Peevf873aa52018-01-26 14:58:28 +0000389
390 String8 requestedPhysicalId(
391 mConfiguredOutputs.valueAt(index).getPhysicalCameraId());
392 requestedPhysicalIds.push_back(requestedPhysicalId.string());
Emilian Peev2295df72021-11-12 18:14:10 -0800393 dynamicProfileBitmap |=
394 mConfiguredOutputs.valueAt(index).getDynamicRangeProfile();
395 }
396 }
397
398 if (dynamicProfileBitmap !=
399 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD) {
400 for (int i = ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD;
401 i < ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_MAX; i <<= 1) {
402 if ((dynamicProfileBitmap & i) == 0) {
403 continue;
404 }
405
406 const auto& it = mDynamicProfileMap.find(i);
407 if (it != mDynamicProfileMap.end()) {
408 if ((it->second == 0) ||
409 ((it->second & dynamicProfileBitmap) == dynamicProfileBitmap)) {
410 continue;
411 } else {
412 ALOGE("%s: Camera %s: Tried to submit a request with a surfaces that"
413 " reference an unsupported dynamic range profile combination"
Emilian Peevc81a7592022-02-14 17:38:18 -0800414 " 0x%" PRIx64 "!", __FUNCTION__, mCameraIdStr.string(),
Emilian Peev2295df72021-11-12 18:14:10 -0800415 dynamicProfileBitmap);
416 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
417 "Request targets an unsupported dynamic range profile"
418 " combination");
419 }
420 } else {
421 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
422 " references unsupported dynamic range profile 0x%x!",
423 __FUNCTION__, mCameraIdStr.string(), i);
424 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
425 "Request targets 10-bit Surface with unsupported dynamic range"
426 " profile");
427 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700428 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700429 }
430
Emilian Peevf873aa52018-01-26 14:58:28 +0000431 CameraDeviceBase::PhysicalCameraSettingsList physicalSettingsList;
432 for (const auto& it : request.mPhysicalCameraSettings) {
Emilian Peev00420d22018-02-05 21:33:13 +0000433 if (it.settings.isEmpty()) {
434 ALOGE("%s: Camera %s: Sent empty metadata packet. Rejecting request.",
435 __FUNCTION__, mCameraIdStr.string());
436 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
437 "Request settings are empty");
438 }
439
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800440 // Check whether the physical / logical stream has settings
441 // consistent with the sensor pixel mode(s) it was configured with.
442 // mCameraIdToStreamSet will only have ids that are high resolution
443 const auto streamIdSetIt = mHighResolutionCameraIdToStreamIdSet.find(it.id);
444 if (streamIdSetIt != mHighResolutionCameraIdToStreamIdSet.end()) {
445 std::list<int> streamIdsUsedInRequest = getIntersection(streamIdSetIt->second,
446 outputStreamIds);
447 if (!request.mIsReprocess &&
448 !isSensorPixelModeConsistent(streamIdsUsedInRequest, it.settings)) {
449 ALOGE("%s: Camera %s: Request settings CONTROL_SENSOR_PIXEL_MODE not "
450 "consistent with configured streams. Rejecting request.",
451 __FUNCTION__, it.id.c_str());
452 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
453 "Request settings CONTROL_SENSOR_PIXEL_MODE are not consistent with "
454 "streams configured");
455 }
456 }
457
Emilian Peevf873aa52018-01-26 14:58:28 +0000458 String8 physicalId(it.id.c_str());
Shuzhen Wang911c6a32021-10-27 13:36:03 -0700459 bool hasTestPatternModePhysicalKey = std::find(mSupportedPhysicalRequestKeys.begin(),
460 mSupportedPhysicalRequestKeys.end(), ANDROID_SENSOR_TEST_PATTERN_MODE) !=
461 mSupportedPhysicalRequestKeys.end();
462 bool hasTestPatternDataPhysicalKey = std::find(mSupportedPhysicalRequestKeys.begin(),
463 mSupportedPhysicalRequestKeys.end(), ANDROID_SENSOR_TEST_PATTERN_DATA) !=
464 mSupportedPhysicalRequestKeys.end();
Emilian Peevf873aa52018-01-26 14:58:28 +0000465 if (physicalId != mDevice->getId()) {
466 auto found = std::find(requestedPhysicalIds.begin(), requestedPhysicalIds.end(),
467 it.id);
468 if (found == requestedPhysicalIds.end()) {
469 ALOGE("%s: Camera %s: Physical camera id: %s not part of attached outputs.",
470 __FUNCTION__, mCameraIdStr.string(), physicalId.string());
471 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
472 "Invalid physical camera id");
473 }
Emilian Peev00420d22018-02-05 21:33:13 +0000474
475 if (!mSupportedPhysicalRequestKeys.empty()) {
476 // Filter out any unsupported physical request keys.
477 CameraMetadata filteredParams(mSupportedPhysicalRequestKeys.size());
478 camera_metadata_t *meta = const_cast<camera_metadata_t *>(
479 filteredParams.getAndLock());
480 set_camera_metadata_vendor_id(meta, mDevice->getVendorTagId());
481 filteredParams.unlock(meta);
482
483 for (const auto& keyIt : mSupportedPhysicalRequestKeys) {
484 camera_metadata_ro_entry entry = it.settings.find(keyIt);
485 if (entry.count > 0) {
486 filteredParams.update(entry);
487 }
488 }
489
Shuzhen Wang911c6a32021-10-27 13:36:03 -0700490 physicalSettingsList.push_back({it.id, filteredParams,
491 hasTestPatternModePhysicalKey, hasTestPatternDataPhysicalKey});
Emilian Peev00420d22018-02-05 21:33:13 +0000492 }
493 } else {
494 physicalSettingsList.push_back({it.id, it.settings});
Emilian Peevf873aa52018-01-26 14:58:28 +0000495 }
Emilian Peevf873aa52018-01-26 14:58:28 +0000496 }
497
498 if (!enforceRequestPermissions(physicalSettingsList.begin()->metadata)) {
499 // Callee logs
500 return STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
501 "Caller does not have permission to change restricted controls");
502 }
503
Emilian Peevaebbe412018-01-15 13:53:24 +0000504 physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_OUTPUT_STREAMS,
505 &outputStreamIds[0], outputStreamIds.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700506
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800507 if (request.mIsReprocess) {
Emilian Peevaebbe412018-01-15 13:53:24 +0000508 physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_INPUT_STREAMS,
509 &mInputStream.id, 1);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700510 }
511
Emilian Peevaebbe412018-01-15 13:53:24 +0000512 physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_ID,
513 &(submitInfo->mRequestId), /*size*/1);
Jianing Wei90e59c92014-03-12 18:29:36 -0700514 loopCounter++; // loopCounter starts from 1
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800515 ALOGV("%s: Camera %s: Creating request with ID %d (%d of %zu)",
516 __FUNCTION__, mCameraIdStr.string(), submitInfo->mRequestId,
517 loopCounter, requests.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700518
Emilian Peevaebbe412018-01-15 13:53:24 +0000519 metadataRequestList.push_back(physicalSettingsList);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700520 surfaceMapList.push_back(surfaceMap);
Shuzhen Wangd26b1862022-03-07 12:05:05 -0800521
Shuzhen Wang9372b0b2022-05-11 18:55:19 -0700522 // Save certain CaptureRequest settings
Shuzhen Wangd26b1862022-03-07 12:05:05 -0800523 if (!request.mUserTag.empty()) {
524 mUserTag = request.mUserTag;
525 }
Shuzhen Wang9372b0b2022-05-11 18:55:19 -0700526 camera_metadata_entry entry =
527 physicalSettingsList.begin()->metadata.find(
528 ANDROID_CONTROL_VIDEO_STABILIZATION_MODE);
529 if (entry.count == 1) {
530 mVideoStabilizationMode = entry.data.u8[0];
531 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700532 }
533 mRequestIdCounter++;
534
535 if (streaming) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700536 err = mDevice->setStreamingRequestList(metadataRequestList, surfaceMapList,
537 &(submitInfo->mLastFrameNumber));
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800538 if (err != OK) {
539 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800540 "Camera %s: Got error %s (%d) after trying to set streaming request",
541 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800542 ALOGE("%s: %s", __FUNCTION__, msg.string());
543 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
544 msg.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700545 } else {
Shuzhen Wangc9ca6782016-04-26 13:40:31 -0700546 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700547 mStreamingRequestId = submitInfo->mRequestId;
Jianing Wei90e59c92014-03-12 18:29:36 -0700548 }
549 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700550 err = mDevice->captureList(metadataRequestList, surfaceMapList,
551 &(submitInfo->mLastFrameNumber));
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800552 if (err != OK) {
553 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800554 "Camera %s: Got error %s (%d) after trying to submit capture request",
555 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800556 ALOGE("%s: %s", __FUNCTION__, msg.string());
557 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
558 msg.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700559 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800560 ALOGV("%s: requestId = %d ", __FUNCTION__, submitInfo->mRequestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700561 }
562
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800563 ALOGV("%s: Camera %s: End of function", __FUNCTION__, mCameraIdStr.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700564 return res;
565}
566
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800567binder::Status CameraDeviceClient::cancelRequest(
568 int requestId,
569 /*out*/
570 int64_t* lastFrameNumber) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700571 ATRACE_CALL();
572 ALOGV("%s, requestId = %d", __FUNCTION__, requestId);
573
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800574 status_t err;
575 binder::Status res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700576
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800577 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700578
579 Mutex::Autolock icl(mBinderSerializationLock);
580
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800581 if (!mDevice.get()) {
582 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
583 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700584
Shuzhen Wangc9ca6782016-04-26 13:40:31 -0700585 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700586 if (mStreamingRequestId != requestId) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800587 String8 msg = String8::format("Camera %s: Canceling request ID %d doesn't match "
588 "current request ID %d", mCameraIdStr.string(), requestId, mStreamingRequestId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800589 ALOGE("%s: %s", __FUNCTION__, msg.string());
590 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700591 }
592
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800593 err = mDevice->clearStreamingRequest(lastFrameNumber);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700594
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800595 if (err == OK) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800596 ALOGV("%s: Camera %s: Successfully cleared streaming request",
597 __FUNCTION__, mCameraIdStr.string());
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700598 mStreamingRequestId = REQUEST_ID_NONE;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800599 } else {
600 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800601 "Camera %s: Error clearing streaming request: %s (%d)",
602 mCameraIdStr.string(), strerror(-err), err);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700603 }
604
605 return res;
606}
607
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800608binder::Status CameraDeviceClient::beginConfigure() {
Ruben Brunkb2119af2014-05-09 19:57:56 -0700609 // TODO: Implement this.
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -0700610 ATRACE_CALL();
Zhijun He1fa89992015-06-01 15:44:31 -0700611 ALOGV("%s: Not implemented yet.", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800612 return binder::Status::ok();
Ruben Brunkb2119af2014-05-09 19:57:56 -0700613}
614
Emilian Peev5fbe0ba2017-10-20 15:45:45 +0100615binder::Status CameraDeviceClient::endConfigure(int operatingMode,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700616 const hardware::camera2::impl::CameraMetadataNative& sessionParams, int64_t startTimeMs,
Emilian Peevcc0b7952020-01-07 13:54:47 -0800617 std::vector<int>* offlineStreamIds /*out*/) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -0700618 ATRACE_CALL();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700619 ALOGV("%s: ending configure (%d input stream, %zu output surfaces)",
620 __FUNCTION__, mInputStream.configured ? 1 : 0,
621 mStreamMap.size());
Igor Murashkine2d167e2014-08-19 16:19:59 -0700622
Zhijun He0effd522016-03-04 10:22:27 -0800623 binder::Status res;
624 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
625
Emilian Peevcc0b7952020-01-07 13:54:47 -0800626 if (offlineStreamIds == nullptr) {
627 String8 msg = String8::format("Invalid offline stream ids");
628 ALOGE("%s: %s", __FUNCTION__, msg.string());
629 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
630 }
631
Zhijun He0effd522016-03-04 10:22:27 -0800632 Mutex::Autolock icl(mBinderSerializationLock);
633
634 if (!mDevice.get()) {
635 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
636 }
637
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700638 res = SessionConfigurationUtils::checkOperatingMode(operatingMode, mDevice->info(),
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000639 mCameraIdStr);
Emilian Peev35ae8262018-11-08 13:11:32 +0000640 if (!res.isOk()) {
641 return res;
642 }
643
644 status_t err = mDevice->configureStreams(sessionParams, operatingMode);
645 if (err == BAD_VALUE) {
646 String8 msg = String8::format("Camera %s: Unsupported set of inputs/outputs provided",
647 mCameraIdStr.string());
648 ALOGE("%s: %s", __FUNCTION__, msg.string());
649 res = STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
650 } else if (err != OK) {
651 String8 msg = String8::format("Camera %s: Error configuring streams: %s (%d)",
652 mCameraIdStr.string(), strerror(-err), err);
653 ALOGE("%s: %s", __FUNCTION__, msg.string());
654 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Emilian Peev538c90e2018-12-17 18:03:19 +0000655 } else {
Emilian Peevcc0b7952020-01-07 13:54:47 -0800656 offlineStreamIds->clear();
657 mDevice->getOfflineStreamIds(offlineStreamIds);
658
Emilian Peev2f5d6012022-01-19 16:16:50 -0800659 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +0000660 for (size_t i = 0; i < mCompositeStreamMap.size(); ++i) {
661 err = mCompositeStreamMap.valueAt(i)->configureStream();
Emilian Peevcc0b7952020-01-07 13:54:47 -0800662 if (err != OK) {
Emilian Peev538c90e2018-12-17 18:03:19 +0000663 String8 msg = String8::format("Camera %s: Error configuring composite "
664 "streams: %s (%d)", mCameraIdStr.string(), strerror(-err), err);
665 ALOGE("%s: %s", __FUNCTION__, msg.string());
666 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
667 break;
668 }
Emilian Peevcc0b7952020-01-07 13:54:47 -0800669
670 // Composite streams can only support offline mode in case all individual internal
671 // streams are also supported.
672 std::vector<int> internalStreams;
673 mCompositeStreamMap.valueAt(i)->insertCompositeStreamIds(&internalStreams);
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800674 offlineStreamIds->erase(
675 std::remove_if(offlineStreamIds->begin(), offlineStreamIds->end(),
Emilian Peevcc0b7952020-01-07 13:54:47 -0800676 [&internalStreams] (int streamId) {
677 auto it = std::find(internalStreams.begin(), internalStreams.end(),
678 streamId);
679 if (it != internalStreams.end()) {
680 internalStreams.erase(it);
681 return true;
682 }
683
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800684 return false;}), offlineStreamIds->end());
Emilian Peevcc0b7952020-01-07 13:54:47 -0800685 if (internalStreams.empty()) {
686 offlineStreamIds->push_back(mCompositeStreamMap.valueAt(i)->getStreamId());
687 }
688 }
689
690 for (const auto& offlineStreamId : *offlineStreamIds) {
691 mStreamInfoMap[offlineStreamId].supportsOffline = true;
Emilian Peev538c90e2018-12-17 18:03:19 +0000692 }
Shuzhen Wang316781a2020-08-18 18:11:01 -0700693
694 nsecs_t configureEnd = systemTime();
695 int32_t configureDurationMs = ns2ms(configureEnd) - startTimeMs;
696 CameraServiceProxyWrapper::logStreamConfigured(mCameraIdStr, operatingMode,
697 false /*internalReconfig*/, configureDurationMs);
Emilian Peev35ae8262018-11-08 13:11:32 +0000698 }
699
700 return res;
701}
702
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800703binder::Status CameraDeviceClient::isSessionConfigurationSupported(
704 const SessionConfiguration& sessionConfiguration, bool *status /*out*/) {
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800705
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800706 ATRACE_CALL();
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800707 binder::Status res;
708 status_t ret = OK;
709 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
710
711 Mutex::Autolock icl(mBinderSerializationLock);
712
713 if (!mDevice.get()) {
714 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
715 }
716
717 auto operatingMode = sessionConfiguration.getOperatingMode();
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700718 res = SessionConfigurationUtils::checkOperatingMode(operatingMode, mDevice->info(),
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000719 mCameraIdStr);
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800720 if (!res.isOk()) {
721 return res;
722 }
723
724 if (status == nullptr) {
725 String8 msg = String8::format( "Camera %s: Invalid status!", mCameraIdStr.string());
726 ALOGE("%s: %s", __FUNCTION__, msg.string());
727 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
728 }
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700729
Emilian Peev35ae8262018-11-08 13:11:32 +0000730 *status = false;
Shuzhen Wanga79a64d2022-04-24 19:56:30 -0700731 camera3::metadataGetter getMetadata = [this](const String8 &id, bool /*overrideForPerfClass*/) {
732 return mDevice->infoPhysical(id);};
Emilian Peev35ae8262018-11-08 13:11:32 +0000733 ret = mProviderManager->isSessionConfigurationSupported(mCameraIdStr.string(),
Shuzhen Wanga79a64d2022-04-24 19:56:30 -0700734 sessionConfiguration, mOverrideForPerfClass, getMetadata, status);
Emilian Peev35ae8262018-11-08 13:11:32 +0000735 switch (ret) {
736 case OK:
737 // Expected, do nothing.
738 break;
739 case INVALID_OPERATION: {
740 String8 msg = String8::format(
741 "Camera %s: Session configuration query not supported!",
742 mCameraIdStr.string());
743 ALOGD("%s: %s", __FUNCTION__, msg.string());
744 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
745 }
746
747 break;
748 default: {
749 String8 msg = String8::format( "Camera %s: Error: %s (%d)", mCameraIdStr.string(),
750 strerror(-ret), ret);
751 ALOGE("%s: %s", __FUNCTION__, msg.string());
752 res = STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
753 msg.string());
754 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800755 }
756
757 return res;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700758}
759
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800760binder::Status CameraDeviceClient::deleteStream(int streamId) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700761 ATRACE_CALL();
762 ALOGV("%s (streamId = 0x%x)", __FUNCTION__, streamId);
763
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800764 binder::Status res;
765 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700766
767 Mutex::Autolock icl(mBinderSerializationLock);
768
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800769 if (!mDevice.get()) {
770 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
771 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700772
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700773 bool isInput = false;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700774 std::vector<sp<IBinder>> surfaces;
Zhijun He5d677d12016-05-29 16:52:39 -0700775 ssize_t dIndex = NAME_NOT_FOUND;
Emilian Peev538c90e2018-12-17 18:03:19 +0000776 ssize_t compositeIndex = NAME_NOT_FOUND;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700777
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700778 if (mInputStream.configured && mInputStream.id == streamId) {
779 isInput = true;
780 } else {
781 // Guard against trying to delete non-created streams
782 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700783 if (streamId == mStreamMap.valueAt(i).streamId()) {
784 surfaces.push_back(mStreamMap.keyAt(i));
785 }
786 }
787
788 // See if this stream is one of the deferred streams.
789 for (size_t i = 0; i < mDeferredStreams.size(); ++i) {
790 if (streamId == mDeferredStreams[i]) {
791 dIndex = i;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700792 break;
793 }
794 }
795
Emilian Peev2f5d6012022-01-19 16:16:50 -0800796 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +0000797 for (size_t i = 0; i < mCompositeStreamMap.size(); ++i) {
798 if (streamId == mCompositeStreamMap.valueAt(i)->getStreamId()) {
799 compositeIndex = i;
800 break;
801 }
802 }
803
Shuzhen Wang0129d522016-10-30 22:43:41 -0700804 if (surfaces.empty() && dIndex == NAME_NOT_FOUND) {
805 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no such"
806 " stream created yet", mCameraIdStr.string(), streamId);
807 ALOGW("%s: %s", __FUNCTION__, msg.string());
808 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700809 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700810 }
811
812 // Also returns BAD_VALUE if stream ID was not valid
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800813 status_t err = mDevice->deleteStream(streamId);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700814
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800815 if (err != OK) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800816 String8 msg = String8::format("Camera %s: Unexpected error %s (%d) when deleting stream %d",
817 mCameraIdStr.string(), strerror(-err), err, streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800818 ALOGE("%s: %s", __FUNCTION__, msg.string());
819 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
820 } else {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700821 if (isInput) {
822 mInputStream.configured = false;
Zhijun He5d677d12016-05-29 16:52:39 -0700823 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700824 for (auto& surface : surfaces) {
825 mStreamMap.removeItem(surface);
826 }
827
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800828 mConfiguredOutputs.removeItem(streamId);
829
Shuzhen Wang0129d522016-10-30 22:43:41 -0700830 if (dIndex != NAME_NOT_FOUND) {
831 mDeferredStreams.removeItemsAt(dIndex);
832 }
Emilian Peev538c90e2018-12-17 18:03:19 +0000833
834 if (compositeIndex != NAME_NOT_FOUND) {
Emilian Peev2f5d6012022-01-19 16:16:50 -0800835 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +0000836 status_t ret;
837 if ((ret = mCompositeStreamMap.valueAt(compositeIndex)->deleteStream())
838 != OK) {
839 String8 msg = String8::format("Camera %s: Unexpected error %s (%d) when "
840 "deleting composite stream %d", mCameraIdStr.string(), strerror(-err), err,
841 streamId);
842 ALOGE("%s: %s", __FUNCTION__, msg.string());
843 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
844 }
845 mCompositeStreamMap.removeItemsAt(compositeIndex);
846 }
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800847 for (auto &mapIt: mHighResolutionCameraIdToStreamIdSet) {
848 auto &streamSet = mapIt.second;
849 if (streamSet.find(streamId) != streamSet.end()) {
850 streamSet.erase(streamId);
851 break;
852 }
853 }
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700854 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700855 }
856
857 return res;
858}
859
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800860binder::Status CameraDeviceClient::createStream(
861 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
862 /*out*/
863 int32_t* newStreamId) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700864 ATRACE_CALL();
Igor Murashkine7ee7632013-06-11 18:10:18 -0700865
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800866 binder::Status res;
867 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700868
869 Mutex::Autolock icl(mBinderSerializationLock);
870
Shuzhen Wang0129d522016-10-30 22:43:41 -0700871 const std::vector<sp<IGraphicBufferProducer>>& bufferProducers =
872 outputConfiguration.getGraphicBufferProducers();
873 size_t numBufferProducers = bufferProducers.size();
Shuzhen Wang758c2152017-01-10 18:26:18 -0800874 bool deferredConsumer = outputConfiguration.isDeferred();
875 bool isShared = outputConfiguration.isShared();
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800876 String8 physicalCameraId = String8(outputConfiguration.getPhysicalCameraId());
Shuzhen Wang758c2152017-01-10 18:26:18 -0800877 bool deferredConsumerOnly = deferredConsumer && numBufferProducers == 0;
Shuzhen Wang83bff122020-11-20 15:51:39 -0800878 bool isMultiResolution = outputConfiguration.isMultiResolution();
Emilian Peevc81a7592022-02-14 17:38:18 -0800879 int64_t dynamicRangeProfile = outputConfiguration.getDynamicRangeProfile();
Shuzhen Wang8ed1e872022-03-08 16:34:33 -0800880 int64_t streamUseCase = outputConfiguration.getStreamUseCase();
Shuzhen Wange4208922022-02-01 16:52:48 -0800881 int timestampBase = outputConfiguration.getTimestampBase();
Shuzhen Wang610d7b82022-02-08 14:37:22 -0800882 int mirrorMode = outputConfiguration.getMirrorMode();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700883
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700884 res = SessionConfigurationUtils::checkSurfaceType(numBufferProducers, deferredConsumer,
Emilian Peev35ae8262018-11-08 13:11:32 +0000885 outputConfiguration.getSurfaceType());
886 if (!res.isOk()) {
887 return res;
Yin-Chia Yeh89f14da2014-06-10 16:05:44 -0700888 }
Zhijun He5d677d12016-05-29 16:52:39 -0700889
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800890 if (!mDevice.get()) {
891 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
892 }
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700893 res = SessionConfigurationUtils::checkPhysicalCameraId(mPhysicalCameraIds,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800894 physicalCameraId, mCameraIdStr);
Emilian Peev35ae8262018-11-08 13:11:32 +0000895 if (!res.isOk()) {
896 return res;
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800897 }
Emilian Peev35ae8262018-11-08 13:11:32 +0000898
Shuzhen Wang0129d522016-10-30 22:43:41 -0700899 std::vector<sp<Surface>> surfaces;
900 std::vector<sp<IBinder>> binders;
Zhijun He5d677d12016-05-29 16:52:39 -0700901 status_t err;
902
903 // Create stream for deferred surface case.
Shuzhen Wang0129d522016-10-30 22:43:41 -0700904 if (deferredConsumerOnly) {
Shuzhen Wang758c2152017-01-10 18:26:18 -0800905 return createDeferredSurfaceStreamLocked(outputConfiguration, isShared, newStreamId);
Zhijun He5d677d12016-05-29 16:52:39 -0700906 }
907
Shuzhen Wang758c2152017-01-10 18:26:18 -0800908 OutputStreamInfo streamInfo;
909 bool isStreamInfoValid = false;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800910 const std::vector<int32_t> &sensorPixelModesUsed =
911 outputConfiguration.getSensorPixelModesUsed();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700912 for (auto& bufferProducer : bufferProducers) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700913 // Don't create multiple streams for the same target surface
Shuzhen Wang0129d522016-10-30 22:43:41 -0700914 sp<IBinder> binder = IInterface::asBinder(bufferProducer);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800915 ssize_t index = mStreamMap.indexOfKey(binder);
916 if (index != NAME_NOT_FOUND) {
917 String8 msg = String8::format("Camera %s: Surface already has a stream created for it "
918 "(ID %zd)", mCameraIdStr.string(), index);
919 ALOGW("%s: %s", __FUNCTION__, msg.string());
920 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
Shuzhen Wang0129d522016-10-30 22:43:41 -0700921 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700922
Shuzhen Wang758c2152017-01-10 18:26:18 -0800923 sp<Surface> surface;
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700924 res = SessionConfigurationUtils::createSurfaceFromGbp(streamInfo,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800925 isStreamInfoValid, surface, bufferProducer, mCameraIdStr,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -0800926 mDevice->infoPhysical(physicalCameraId), sensorPixelModesUsed, dynamicRangeProfile,
Shuzhen Wang610d7b82022-02-08 14:37:22 -0800927 streamUseCase, timestampBase, mirrorMode);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800928
929 if (!res.isOk())
930 return res;
931
932 if (!isStreamInfoValid) {
933 isStreamInfoValid = true;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700934 }
935
Shuzhen Wang758c2152017-01-10 18:26:18 -0800936 binders.push_back(IInterface::asBinder(bufferProducer));
Shuzhen Wang0129d522016-10-30 22:43:41 -0700937 surfaces.push_back(surface);
Ruben Brunkbba75572014-11-20 17:29:50 -0800938 }
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700939
940 // If mOverrideForPerfClass is true, do not fail createStream() for small
941 // JPEG sizes because existing createSurfaceFromGbp() logic will find the
942 // closest possible supported size.
943
Zhijun He125684a2015-12-26 15:07:30 -0800944 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
Emilian Peev40ead602017-09-26 15:46:36 +0100945 std::vector<int> surfaceIds;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800946 bool isDepthCompositeStream =
947 camera3::DepthCompositeStream::isDepthCompositeStream(surfaces[0]);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800948 bool isHeicCompisiteStream = camera3::HeicCompositeStream::isHeicCompositeStream(surfaces[0]);
949 if (isDepthCompositeStream || isHeicCompisiteStream) {
950 sp<CompositeStream> compositeStream;
951 if (isDepthCompositeStream) {
952 compositeStream = new camera3::DepthCompositeStream(mDevice, getRemoteCallback());
953 } else {
954 compositeStream = new camera3::HeicCompositeStream(mDevice, getRemoteCallback());
955 }
956
Emilian Peev538c90e2018-12-17 18:03:19 +0000957 err = compositeStream->createStream(surfaces, deferredConsumer, streamInfo.width,
958 streamInfo.height, streamInfo.format,
Emilian Peevf4816702020-04-03 15:44:51 -0700959 static_cast<camera_stream_rotation_t>(outputConfiguration.getRotation()),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800960 &streamId, physicalCameraId, streamInfo.sensorPixelModesUsed, &surfaceIds,
961 outputConfiguration.getSurfaceSetID(), isShared, isMultiResolution);
Emilian Peev538c90e2018-12-17 18:03:19 +0000962 if (err == OK) {
Emilian Peev2f5d6012022-01-19 16:16:50 -0800963 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +0000964 mCompositeStreamMap.add(IInterface::asBinder(surfaces[0]->getIGraphicBufferProducer()),
965 compositeStream);
966 }
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800967 } else {
968 err = mDevice->createStream(surfaces, deferredConsumer, streamInfo.width,
969 streamInfo.height, streamInfo.format, streamInfo.dataSpace,
Emilian Peevf4816702020-04-03 15:44:51 -0700970 static_cast<camera_stream_rotation_t>(outputConfiguration.getRotation()),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800971 &streamId, physicalCameraId, streamInfo.sensorPixelModesUsed, &surfaceIds,
Emilian Peev2295df72021-11-12 18:14:10 -0800972 outputConfiguration.getSurfaceSetID(), isShared, isMultiResolution,
Shuzhen Wange4208922022-02-01 16:52:48 -0800973 /*consumerUsage*/0, streamInfo.dynamicRangeProfile, streamInfo.streamUseCase,
Shuzhen Wang610d7b82022-02-08 14:37:22 -0800974 streamInfo.timestampBase, streamInfo.mirrorMode);
Emilian Peev538c90e2018-12-17 18:03:19 +0000975 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700976
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800977 if (err != OK) {
978 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800979 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
Shuzhen Wang758c2152017-01-10 18:26:18 -0800980 mCameraIdStr.string(), streamInfo.width, streamInfo.height, streamInfo.format,
981 streamInfo.dataSpace, strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800982 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700983 int i = 0;
984 for (auto& binder : binders) {
985 ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %d",
986 __FUNCTION__, binder.get(), streamId, i);
Emilian Peev40ead602017-09-26 15:46:36 +0100987 mStreamMap.add(binder, StreamSurfaceId(streamId, surfaceIds[i]));
988 i++;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700989 }
Shuzhen Wang758c2152017-01-10 18:26:18 -0800990
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800991 mConfiguredOutputs.add(streamId, outputConfiguration);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800992 mStreamInfoMap[streamId] = streamInfo;
993
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800994 ALOGV("%s: Camera %s: Successfully created a new stream ID %d for output surface"
Shuzhen Wang0129d522016-10-30 22:43:41 -0700995 " (%d x %d) with format 0x%x.",
Shuzhen Wang758c2152017-01-10 18:26:18 -0800996 __FUNCTION__, mCameraIdStr.string(), streamId, streamInfo.width,
997 streamInfo.height, streamInfo.format);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700998
Zhijun He5d677d12016-05-29 16:52:39 -0700999 // Set transform flags to ensure preview to be rotated correctly.
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001000 res = setStreamTransformLocked(streamId, streamInfo.mirrorMode);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001001
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001002 // Fill in mHighResolutionCameraIdToStreamIdSet map
1003 const String8 &cameraIdUsed =
1004 physicalCameraId.size() != 0 ? physicalCameraId : mCameraIdStr;
1005 const char *cameraIdUsedCStr = cameraIdUsed.string();
1006 // Only needed for high resolution sensors
1007 if (mHighResolutionSensors.find(cameraIdUsedCStr) !=
1008 mHighResolutionSensors.end()) {
1009 mHighResolutionCameraIdToStreamIdSet[cameraIdUsedCStr].insert(streamId);
1010 }
1011
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001012 *newStreamId = streamId;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001013 }
1014
1015 return res;
1016}
1017
Zhijun He5d677d12016-05-29 16:52:39 -07001018binder::Status CameraDeviceClient::createDeferredSurfaceStreamLocked(
1019 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
Shuzhen Wang758c2152017-01-10 18:26:18 -08001020 bool isShared,
Zhijun He5d677d12016-05-29 16:52:39 -07001021 /*out*/
1022 int* newStreamId) {
1023 int width, height, format, surfaceType;
Emilian Peev050f5dc2017-05-18 14:43:56 +01001024 uint64_t consumerUsage;
Zhijun He5d677d12016-05-29 16:52:39 -07001025 android_dataspace dataSpace;
1026 status_t err;
1027 binder::Status res;
1028
1029 if (!mDevice.get()) {
1030 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1031 }
1032
1033 // Infer the surface info for deferred surface stream creation.
1034 width = outputConfiguration.getWidth();
1035 height = outputConfiguration.getHeight();
1036 surfaceType = outputConfiguration.getSurfaceType();
1037 format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
1038 dataSpace = android_dataspace_t::HAL_DATASPACE_UNKNOWN;
1039 // Hardcode consumer usage flags: SurfaceView--0x900, SurfaceTexture--0x100.
1040 consumerUsage = GraphicBuffer::USAGE_HW_TEXTURE;
1041 if (surfaceType == OutputConfiguration::SURFACE_TYPE_SURFACE_VIEW) {
1042 consumerUsage |= GraphicBuffer::USAGE_HW_COMPOSER;
1043 }
1044 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001045 std::vector<sp<Surface>> noSurface;
Emilian Peev40ead602017-09-26 15:46:36 +01001046 std::vector<int> surfaceIds;
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001047 String8 physicalCameraId(outputConfiguration.getPhysicalCameraId());
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001048 const String8 &cameraIdUsed =
1049 physicalCameraId.size() != 0 ? physicalCameraId : mCameraIdStr;
1050 // Here, we override sensor pixel modes
1051 std::unordered_set<int32_t> overriddenSensorPixelModesUsed;
1052 const std::vector<int32_t> &sensorPixelModesUsed =
1053 outputConfiguration.getSensorPixelModesUsed();
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07001054 if (SessionConfigurationUtils::checkAndOverrideSensorPixelModesUsed(
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001055 sensorPixelModesUsed, format, width, height, getStaticInfo(cameraIdUsed),
1056 /*allowRounding*/ false, &overriddenSensorPixelModesUsed) != OK) {
1057 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1058 "sensor pixel modes used not valid for deferred stream");
1059 }
1060
Shuzhen Wang0129d522016-10-30 22:43:41 -07001061 err = mDevice->createStream(noSurface, /*hasDeferredConsumer*/true, width,
1062 height, format, dataSpace,
Emilian Peevf4816702020-04-03 15:44:51 -07001063 static_cast<camera_stream_rotation_t>(outputConfiguration.getRotation()),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001064 &streamId, physicalCameraId,
1065 overriddenSensorPixelModesUsed,
1066 &surfaceIds,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001067 outputConfiguration.getSurfaceSetID(), isShared,
Emilian Peev2295df72021-11-12 18:14:10 -08001068 outputConfiguration.isMultiResolution(), consumerUsage,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001069 outputConfiguration.getDynamicRangeProfile(),
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001070 outputConfiguration.getStreamUseCase(),
1071 outputConfiguration.getMirrorMode());
Zhijun He5d677d12016-05-29 16:52:39 -07001072
1073 if (err != OK) {
1074 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001075 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
1076 mCameraIdStr.string(), width, height, format, dataSpace, strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001077 } else {
1078 // Can not add streamId to mStreamMap here, as the surface is deferred. Add it to
1079 // a separate list to track. Once the deferred surface is set, this id will be
1080 // relocated to mStreamMap.
1081 mDeferredStreams.push_back(streamId);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001082 mStreamInfoMap.emplace(std::piecewise_construct, std::forward_as_tuple(streamId),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001083 std::forward_as_tuple(width, height, format, dataSpace, consumerUsage,
Emilian Peev2295df72021-11-12 18:14:10 -08001084 overriddenSensorPixelModesUsed,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001085 outputConfiguration.getDynamicRangeProfile(),
Shuzhen Wange4208922022-02-01 16:52:48 -08001086 outputConfiguration.getStreamUseCase(),
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001087 outputConfiguration.getTimestampBase(),
1088 outputConfiguration.getMirrorMode()));
Shuzhen Wang758c2152017-01-10 18:26:18 -08001089
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001090 ALOGV("%s: Camera %s: Successfully created a new stream ID %d for a deferred surface"
Zhijun He5d677d12016-05-29 16:52:39 -07001091 " (%d x %d) stream with format 0x%x.",
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001092 __FUNCTION__, mCameraIdStr.string(), streamId, width, height, format);
Zhijun He5d677d12016-05-29 16:52:39 -07001093
1094 // Set transform flags to ensure preview to be rotated correctly.
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001095 res = setStreamTransformLocked(streamId, outputConfiguration.getMirrorMode());
Zhijun He5d677d12016-05-29 16:52:39 -07001096
1097 *newStreamId = streamId;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001098 // Fill in mHighResolutionCameraIdToStreamIdSet
1099 const char *cameraIdUsedCStr = cameraIdUsed.string();
1100 // Only needed for high resolution sensors
1101 if (mHighResolutionSensors.find(cameraIdUsedCStr) !=
1102 mHighResolutionSensors.end()) {
1103 mHighResolutionCameraIdToStreamIdSet[cameraIdUsed.string()].insert(streamId);
1104 }
Zhijun He5d677d12016-05-29 16:52:39 -07001105 }
1106 return res;
1107}
1108
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001109binder::Status CameraDeviceClient::setStreamTransformLocked(int streamId, int mirrorMode) {
Zhijun He5d677d12016-05-29 16:52:39 -07001110 int32_t transform = 0;
1111 status_t err;
1112 binder::Status res;
1113
1114 if (!mDevice.get()) {
1115 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1116 }
1117
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001118 err = getRotationTransformLocked(mirrorMode, &transform);
Zhijun He5d677d12016-05-29 16:52:39 -07001119 if (err != OK) {
1120 // Error logged by getRotationTransformLocked.
1121 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
1122 "Unable to calculate rotation transform for new stream");
1123 }
1124
1125 err = mDevice->setStreamTransform(streamId, transform);
1126 if (err != OK) {
1127 String8 msg = String8::format("Failed to set stream transform (stream id %d)",
1128 streamId);
1129 ALOGE("%s: %s", __FUNCTION__, msg.string());
1130 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
1131 }
1132
1133 return res;
1134}
Ruben Brunkbba75572014-11-20 17:29:50 -08001135
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001136binder::Status CameraDeviceClient::createInputStream(
Shuzhen Wang83bff122020-11-20 15:51:39 -08001137 int width, int height, int format, bool isMultiResolution,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001138 /*out*/
1139 int32_t* newStreamId) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001140
1141 ATRACE_CALL();
Shuzhen Wang83bff122020-11-20 15:51:39 -08001142 ALOGV("%s (w = %d, h = %d, f = 0x%x, isMultiResolution %d)", __FUNCTION__,
1143 width, height, format, isMultiResolution);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001144
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001145 binder::Status res;
1146 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001147
1148 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001149
1150 if (!mDevice.get()) {
1151 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1152 }
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001153
1154 if (mInputStream.configured) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001155 String8 msg = String8::format("Camera %s: Already has an input stream "
Yi Kong0f414de2017-12-15 13:48:50 -08001156 "configured (ID %d)", mCameraIdStr.string(), mInputStream.id);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001157 ALOGE("%s: %s", __FUNCTION__, msg.string() );
1158 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001159 }
1160
1161 int streamId = -1;
Shuzhen Wang83bff122020-11-20 15:51:39 -08001162 status_t err = mDevice->createInputStream(width, height, format, isMultiResolution, &streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001163 if (err == OK) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001164 mInputStream.configured = true;
1165 mInputStream.width = width;
1166 mInputStream.height = height;
1167 mInputStream.format = format;
1168 mInputStream.id = streamId;
1169
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001170 ALOGV("%s: Camera %s: Successfully created a new input stream ID %d",
1171 __FUNCTION__, mCameraIdStr.string(), streamId);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001172
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001173 *newStreamId = streamId;
1174 } else {
1175 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001176 "Camera %s: Error creating new input stream: %s (%d)", mCameraIdStr.string(),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001177 strerror(-err), err);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001178 }
1179
1180 return res;
1181}
1182
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001183binder::Status CameraDeviceClient::getInputSurface(/*out*/ view::Surface *inputSurface) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001184
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001185 binder::Status res;
1186 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1187
1188 if (inputSurface == NULL) {
1189 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Null input surface");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001190 }
1191
1192 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001193 if (!mDevice.get()) {
1194 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1195 }
1196 sp<IGraphicBufferProducer> producer;
1197 status_t err = mDevice->getInputBufferProducer(&producer);
1198 if (err != OK) {
1199 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001200 "Camera %s: Error getting input Surface: %s (%d)",
1201 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001202 } else {
1203 inputSurface->name = String16("CameraInput");
1204 inputSurface->graphicBufferProducer = producer;
1205 }
1206 return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001207}
1208
Emilian Peev40ead602017-09-26 15:46:36 +01001209binder::Status CameraDeviceClient::updateOutputConfiguration(int streamId,
1210 const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
1211 ATRACE_CALL();
1212
1213 binder::Status res;
1214 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1215
1216 Mutex::Autolock icl(mBinderSerializationLock);
1217
1218 if (!mDevice.get()) {
1219 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1220 }
1221
1222 const std::vector<sp<IGraphicBufferProducer> >& bufferProducers =
1223 outputConfiguration.getGraphicBufferProducers();
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -07001224 String8 physicalCameraId(outputConfiguration.getPhysicalCameraId());
1225
Emilian Peev40ead602017-09-26 15:46:36 +01001226 auto producerCount = bufferProducers.size();
1227 if (producerCount == 0) {
1228 ALOGE("%s: bufferProducers must not be empty", __FUNCTION__);
1229 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1230 "bufferProducers must not be empty");
1231 }
1232
1233 // The first output is the one associated with the output configuration.
1234 // It should always be present, valid and the corresponding stream id should match.
1235 sp<IBinder> binder = IInterface::asBinder(bufferProducers[0]);
1236 ssize_t index = mStreamMap.indexOfKey(binder);
1237 if (index == NAME_NOT_FOUND) {
1238 ALOGE("%s: Outputconfiguration is invalid", __FUNCTION__);
1239 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1240 "OutputConfiguration is invalid");
1241 }
1242 if (mStreamMap.valueFor(binder).streamId() != streamId) {
1243 ALOGE("%s: Stream Id: %d provided doesn't match the id: %d in the stream map",
1244 __FUNCTION__, streamId, mStreamMap.valueFor(binder).streamId());
1245 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1246 "Stream id is invalid");
1247 }
1248
1249 std::vector<size_t> removedSurfaceIds;
1250 std::vector<sp<IBinder>> removedOutputs;
1251 std::vector<sp<Surface>> newOutputs;
1252 std::vector<OutputStreamInfo> streamInfos;
1253 KeyedVector<sp<IBinder>, sp<IGraphicBufferProducer>> newOutputsMap;
1254 for (auto &it : bufferProducers) {
1255 newOutputsMap.add(IInterface::asBinder(it), it);
1256 }
1257
1258 for (size_t i = 0; i < mStreamMap.size(); i++) {
1259 ssize_t idx = newOutputsMap.indexOfKey(mStreamMap.keyAt(i));
1260 if (idx == NAME_NOT_FOUND) {
1261 if (mStreamMap[i].streamId() == streamId) {
1262 removedSurfaceIds.push_back(mStreamMap[i].surfaceId());
1263 removedOutputs.push_back(mStreamMap.keyAt(i));
1264 }
1265 } else {
1266 if (mStreamMap[i].streamId() != streamId) {
1267 ALOGE("%s: Output surface already part of a different stream", __FUNCTION__);
1268 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1269 "Target Surface is invalid");
1270 }
1271 newOutputsMap.removeItemsAt(idx);
1272 }
1273 }
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001274 const std::vector<int32_t> &sensorPixelModesUsed =
1275 outputConfiguration.getSensorPixelModesUsed();
Shuzhen Wang8ed1e872022-03-08 16:34:33 -08001276 int64_t streamUseCase = outputConfiguration.getStreamUseCase();
Shuzhen Wange4208922022-02-01 16:52:48 -08001277 int timestampBase = outputConfiguration.getTimestampBase();
Emilian Peevc81a7592022-02-14 17:38:18 -08001278 int64_t dynamicRangeProfile = outputConfiguration.getDynamicRangeProfile();
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001279 int mirrorMode = outputConfiguration.getMirrorMode();
Emilian Peev2295df72021-11-12 18:14:10 -08001280
Emilian Peev40ead602017-09-26 15:46:36 +01001281 for (size_t i = 0; i < newOutputsMap.size(); i++) {
1282 OutputStreamInfo outInfo;
1283 sp<Surface> surface;
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07001284 res = SessionConfigurationUtils::createSurfaceFromGbp(outInfo,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001285 /*isStreamInfoValid*/ false, surface, newOutputsMap.valueAt(i), mCameraIdStr,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001286 mDevice->infoPhysical(physicalCameraId), sensorPixelModesUsed, dynamicRangeProfile,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001287 streamUseCase, timestampBase, mirrorMode);
Emilian Peev40ead602017-09-26 15:46:36 +01001288 if (!res.isOk())
1289 return res;
1290
Emilian Peev40ead602017-09-26 15:46:36 +01001291 streamInfos.push_back(outInfo);
1292 newOutputs.push_back(surface);
1293 }
1294
1295 //Trivial case no changes required
1296 if (removedSurfaceIds.empty() && newOutputs.empty()) {
1297 return binder::Status::ok();
1298 }
1299
1300 KeyedVector<sp<Surface>, size_t> outputMap;
1301 auto ret = mDevice->updateStream(streamId, newOutputs, streamInfos, removedSurfaceIds,
1302 &outputMap);
1303 if (ret != OK) {
1304 switch (ret) {
1305 case NAME_NOT_FOUND:
1306 case BAD_VALUE:
1307 case -EBUSY:
1308 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1309 "Camera %s: Error updating stream: %s (%d)",
1310 mCameraIdStr.string(), strerror(ret), ret);
1311 break;
1312 default:
1313 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1314 "Camera %s: Error updating stream: %s (%d)",
1315 mCameraIdStr.string(), strerror(ret), ret);
1316 break;
1317 }
1318 } else {
1319 for (const auto &it : removedOutputs) {
1320 mStreamMap.removeItem(it);
1321 }
1322
1323 for (size_t i = 0; i < outputMap.size(); i++) {
1324 mStreamMap.add(IInterface::asBinder(outputMap.keyAt(i)->getIGraphicBufferProducer()),
1325 StreamSurfaceId(streamId, outputMap.valueAt(i)));
1326 }
1327
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -08001328 mConfiguredOutputs.replaceValueFor(streamId, outputConfiguration);
1329
Emilian Peev40ead602017-09-26 15:46:36 +01001330 ALOGV("%s: Camera %s: Successful stream ID %d update",
1331 __FUNCTION__, mCameraIdStr.string(), streamId);
1332 }
1333
1334 return res;
1335}
1336
Igor Murashkine7ee7632013-06-11 18:10:18 -07001337// Create a request object from a template.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001338binder::Status CameraDeviceClient::createDefaultRequest(int templateId,
1339 /*out*/
1340 hardware::camera2::impl::CameraMetadataNative* request)
Igor Murashkine7ee7632013-06-11 18:10:18 -07001341{
1342 ATRACE_CALL();
1343 ALOGV("%s (templateId = 0x%x)", __FUNCTION__, templateId);
1344
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001345 binder::Status res;
1346 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001347
1348 Mutex::Autolock icl(mBinderSerializationLock);
1349
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001350 if (!mDevice.get()) {
1351 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1352 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001353
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001354 status_t err;
Emilian Peevf4816702020-04-03 15:44:51 -07001355 camera_request_template_t tempId = camera_request_template_t::CAMERA_TEMPLATE_COUNT;
1356 if (!(res = mapRequestTemplate(templateId, &tempId)).isOk()) return res;
1357
1358 CameraMetadata metadata;
1359 if ( (err = mDevice->createDefaultRequest(tempId, &metadata) ) == OK &&
Igor Murashkine7ee7632013-06-11 18:10:18 -07001360 request != NULL) {
1361
1362 request->swap(metadata);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001363 } else if (err == BAD_VALUE) {
1364 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001365 "Camera %s: Template ID %d is invalid or not supported: %s (%d)",
1366 mCameraIdStr.string(), templateId, strerror(-err), err);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001367
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001368 } else {
1369 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001370 "Camera %s: Error creating default request for template %d: %s (%d)",
1371 mCameraIdStr.string(), templateId, strerror(-err), err);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001372 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001373 return res;
1374}
1375
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001376binder::Status CameraDeviceClient::getCameraInfo(
1377 /*out*/
1378 hardware::camera2::impl::CameraMetadataNative* info)
Igor Murashkine7ee7632013-06-11 18:10:18 -07001379{
1380 ATRACE_CALL();
1381 ALOGV("%s", __FUNCTION__);
1382
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001383 binder::Status res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001384
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001385 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001386
1387 Mutex::Autolock icl(mBinderSerializationLock);
1388
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001389 if (!mDevice.get()) {
1390 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1391 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001392
Igor Murashkin099b4572013-07-12 17:52:16 -07001393 if (info != NULL) {
1394 *info = mDevice->info(); // static camera metadata
1395 // TODO: merge with device-specific camera metadata
1396 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001397
1398 return res;
1399}
1400
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001401binder::Status CameraDeviceClient::waitUntilIdle()
Zhijun He2ab500c2013-07-23 08:02:53 -07001402{
1403 ATRACE_CALL();
1404 ALOGV("%s", __FUNCTION__);
1405
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001406 binder::Status res;
1407 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Zhijun He2ab500c2013-07-23 08:02:53 -07001408
1409 Mutex::Autolock icl(mBinderSerializationLock);
1410
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001411 if (!mDevice.get()) {
1412 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1413 }
Zhijun He2ab500c2013-07-23 08:02:53 -07001414
1415 // FIXME: Also need check repeating burst.
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001416 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001417 if (mStreamingRequestId != REQUEST_ID_NONE) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001418 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001419 "Camera %s: Try to waitUntilIdle when there are active streaming requests",
1420 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001421 ALOGE("%s: %s", __FUNCTION__, msg.string());
1422 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Zhijun He2ab500c2013-07-23 08:02:53 -07001423 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001424 status_t err = mDevice->waitUntilDrained();
1425 if (err != OK) {
1426 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001427 "Camera %s: Error waiting to drain: %s (%d)",
1428 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001429 }
Zhijun He2ab500c2013-07-23 08:02:53 -07001430 ALOGV("%s Done", __FUNCTION__);
Zhijun He2ab500c2013-07-23 08:02:53 -07001431 return res;
1432}
1433
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001434binder::Status CameraDeviceClient::flush(
1435 /*out*/
1436 int64_t* lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001437 ATRACE_CALL();
1438 ALOGV("%s", __FUNCTION__);
1439
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001440 binder::Status res;
1441 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001442
1443 Mutex::Autolock icl(mBinderSerializationLock);
1444
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001445 if (!mDevice.get()) {
1446 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1447 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001448
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001449 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001450 mStreamingRequestId = REQUEST_ID_NONE;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001451 status_t err = mDevice->flush(lastFrameNumber);
1452 if (err != OK) {
1453 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001454 "Camera %s: Error flushing device: %s (%d)", mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001455 }
1456 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001457}
1458
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001459binder::Status CameraDeviceClient::prepare(int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001460 ATRACE_CALL();
1461 ALOGV("%s", __FUNCTION__);
1462
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001463 binder::Status res;
1464 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001465
1466 Mutex::Autolock icl(mBinderSerializationLock);
1467
1468 // Guard against trying to prepare non-created streams
1469 ssize_t index = NAME_NOT_FOUND;
1470 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001471 if (streamId == mStreamMap.valueAt(i).streamId()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001472 index = i;
1473 break;
1474 }
1475 }
1476
1477 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001478 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1479 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001480 ALOGW("%s: %s", __FUNCTION__, msg.string());
1481 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001482 }
1483
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001484 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1485 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001486 status_t err = mDevice->prepare(streamId);
1487 if (err == BAD_VALUE) {
1488 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001489 "Camera %s: Stream %d has already been used, and cannot be prepared",
1490 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001491 } else if (err != OK) {
1492 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001493 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001494 strerror(-err), err);
1495 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001496 return res;
1497}
1498
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001499binder::Status CameraDeviceClient::prepare2(int maxCount, int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001500 ATRACE_CALL();
1501 ALOGV("%s", __FUNCTION__);
1502
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001503 binder::Status res;
1504 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Ruben Brunkc78ac262015-08-13 17:58:46 -07001505
1506 Mutex::Autolock icl(mBinderSerializationLock);
1507
1508 // Guard against trying to prepare non-created streams
1509 ssize_t index = NAME_NOT_FOUND;
1510 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001511 if (streamId == mStreamMap.valueAt(i).streamId()) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001512 index = i;
1513 break;
1514 }
1515 }
1516
1517 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001518 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1519 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001520 ALOGW("%s: %s", __FUNCTION__, msg.string());
1521 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -07001522 }
1523
1524 if (maxCount <= 0) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001525 String8 msg = String8::format("Camera %s: maxCount (%d) must be greater than 0",
1526 mCameraIdStr.string(), maxCount);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001527 ALOGE("%s: %s", __FUNCTION__, msg.string());
1528 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -07001529 }
1530
1531 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1532 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001533 status_t err = mDevice->prepare(maxCount, streamId);
1534 if (err == BAD_VALUE) {
1535 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001536 "Camera %s: Stream %d has already been used, and cannot be prepared",
1537 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001538 } else if (err != OK) {
1539 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001540 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001541 strerror(-err), err);
1542 }
Ruben Brunkc78ac262015-08-13 17:58:46 -07001543
1544 return res;
1545}
1546
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001547binder::Status CameraDeviceClient::tearDown(int streamId) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001548 ATRACE_CALL();
1549 ALOGV("%s", __FUNCTION__);
1550
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001551 binder::Status res;
1552 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001553
1554 Mutex::Autolock icl(mBinderSerializationLock);
1555
1556 // Guard against trying to prepare non-created streams
1557 ssize_t index = NAME_NOT_FOUND;
1558 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001559 if (streamId == mStreamMap.valueAt(i).streamId()) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001560 index = i;
1561 break;
1562 }
1563 }
1564
1565 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001566 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1567 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001568 ALOGW("%s: %s", __FUNCTION__, msg.string());
1569 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001570 }
1571
1572 // Also returns BAD_VALUE if stream ID was not valid or if the stream is in
1573 // use
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001574 status_t err = mDevice->tearDown(streamId);
1575 if (err == BAD_VALUE) {
1576 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001577 "Camera %s: Stream %d is still in use, cannot be torn down",
1578 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001579 } else if (err != OK) {
1580 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001581 "Camera %s: Error tearing down stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001582 strerror(-err), err);
1583 }
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001584
1585 return res;
1586}
1587
Shuzhen Wang758c2152017-01-10 18:26:18 -08001588binder::Status CameraDeviceClient::finalizeOutputConfigurations(int32_t streamId,
Zhijun He5d677d12016-05-29 16:52:39 -07001589 const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
1590 ATRACE_CALL();
1591
1592 binder::Status res;
1593 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1594
1595 Mutex::Autolock icl(mBinderSerializationLock);
1596
Shuzhen Wang0129d522016-10-30 22:43:41 -07001597 const std::vector<sp<IGraphicBufferProducer> >& bufferProducers =
1598 outputConfiguration.getGraphicBufferProducers();
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -07001599 String8 physicalId(outputConfiguration.getPhysicalCameraId());
Zhijun He5d677d12016-05-29 16:52:39 -07001600
Shuzhen Wang0129d522016-10-30 22:43:41 -07001601 if (bufferProducers.size() == 0) {
1602 ALOGE("%s: bufferProducers must not be empty", __FUNCTION__);
Zhijun He5d677d12016-05-29 16:52:39 -07001603 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Target Surface is invalid");
1604 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001605
Shuzhen Wang758c2152017-01-10 18:26:18 -08001606 // streamId should be in mStreamMap if this stream already has a surface attached
1607 // to it. Otherwise, it should be in mDeferredStreams.
1608 bool streamIdConfigured = false;
1609 ssize_t deferredStreamIndex = NAME_NOT_FOUND;
1610 for (size_t i = 0; i < mStreamMap.size(); i++) {
1611 if (mStreamMap.valueAt(i).streamId() == streamId) {
1612 streamIdConfigured = true;
1613 break;
1614 }
Zhijun He5d677d12016-05-29 16:52:39 -07001615 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001616 for (size_t i = 0; i < mDeferredStreams.size(); i++) {
1617 if (streamId == mDeferredStreams[i]) {
1618 deferredStreamIndex = i;
1619 break;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001620 }
1621
Shuzhen Wang758c2152017-01-10 18:26:18 -08001622 }
1623 if (deferredStreamIndex == NAME_NOT_FOUND && !streamIdConfigured) {
1624 String8 msg = String8::format("Camera %s: deferred surface is set to a unknown stream"
1625 "(ID %d)", mCameraIdStr.string(), streamId);
1626 ALOGW("%s: %s", __FUNCTION__, msg.string());
1627 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Zhijun He5d677d12016-05-29 16:52:39 -07001628 }
1629
Shuzhen Wang88fd0052017-02-09 16:40:07 -08001630 if (mStreamInfoMap[streamId].finalized) {
1631 String8 msg = String8::format("Camera %s: finalizeOutputConfigurations has been called"
1632 " on stream ID %d", mCameraIdStr.string(), streamId);
1633 ALOGW("%s: %s", __FUNCTION__, msg.string());
1634 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1635 }
1636
Zhijun He5d677d12016-05-29 16:52:39 -07001637 if (!mDevice.get()) {
1638 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1639 }
1640
Shuzhen Wang758c2152017-01-10 18:26:18 -08001641 std::vector<sp<Surface>> consumerSurfaces;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001642 const std::vector<int32_t> &sensorPixelModesUsed =
1643 outputConfiguration.getSensorPixelModesUsed();
Emilian Peevc81a7592022-02-14 17:38:18 -08001644 int64_t dynamicRangeProfile = outputConfiguration.getDynamicRangeProfile();
Shuzhen Wang8ed1e872022-03-08 16:34:33 -08001645 int64_t streamUseCase= outputConfiguration.getStreamUseCase();
Shuzhen Wange4208922022-02-01 16:52:48 -08001646 int timestampBase = outputConfiguration.getTimestampBase();
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001647 int mirrorMode = outputConfiguration.getMirrorMode();
Shuzhen Wang758c2152017-01-10 18:26:18 -08001648 for (auto& bufferProducer : bufferProducers) {
1649 // Don't create multiple streams for the same target surface
Zhijun He5d677d12016-05-29 16:52:39 -07001650 ssize_t index = mStreamMap.indexOfKey(IInterface::asBinder(bufferProducer));
1651 if (index != NAME_NOT_FOUND) {
Shuzhen Wang758c2152017-01-10 18:26:18 -08001652 ALOGV("Camera %s: Surface already has a stream created "
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001653 " for it (ID %zd)", mCameraIdStr.string(), index);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001654 continue;
Zhijun He5d677d12016-05-29 16:52:39 -07001655 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001656
1657 sp<Surface> surface;
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07001658 res = SessionConfigurationUtils::createSurfaceFromGbp(mStreamInfoMap[streamId],
Colin Crossb8a9dbb2020-08-27 04:12:26 +00001659 true /*isStreamInfoValid*/, surface, bufferProducer, mCameraIdStr,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001660 mDevice->infoPhysical(physicalId), sensorPixelModesUsed, dynamicRangeProfile,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001661 streamUseCase, timestampBase, mirrorMode);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001662
1663 if (!res.isOk())
1664 return res;
1665
1666 consumerSurfaces.push_back(surface);
Zhijun He5d677d12016-05-29 16:52:39 -07001667 }
1668
Shuzhen Wanga81ce342017-04-13 15:18:36 -07001669 // Gracefully handle case where finalizeOutputConfigurations is called
1670 // without any new surface.
1671 if (consumerSurfaces.size() == 0) {
1672 mStreamInfoMap[streamId].finalized = true;
1673 return res;
1674 }
1675
Zhijun He5d677d12016-05-29 16:52:39 -07001676 // Finish the deferred stream configuration with the surface.
Shuzhen Wang758c2152017-01-10 18:26:18 -08001677 status_t err;
Emilian Peev40ead602017-09-26 15:46:36 +01001678 std::vector<int> consumerSurfaceIds;
1679 err = mDevice->setConsumerSurfaces(streamId, consumerSurfaces, &consumerSurfaceIds);
Zhijun He5d677d12016-05-29 16:52:39 -07001680 if (err == OK) {
Shuzhen Wang758c2152017-01-10 18:26:18 -08001681 for (size_t i = 0; i < consumerSurfaces.size(); i++) {
1682 sp<IBinder> binder = IInterface::asBinder(
1683 consumerSurfaces[i]->getIGraphicBufferProducer());
Emilian Peev40ead602017-09-26 15:46:36 +01001684 ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %d", __FUNCTION__,
Shuzhen Wang758c2152017-01-10 18:26:18 -08001685 binder.get(), streamId, consumerSurfaceIds[i]);
1686 mStreamMap.add(binder, StreamSurfaceId(streamId, consumerSurfaceIds[i]));
1687 }
1688 if (deferredStreamIndex != NAME_NOT_FOUND) {
1689 mDeferredStreams.removeItemsAt(deferredStreamIndex);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001690 }
Shuzhen Wang88fd0052017-02-09 16:40:07 -08001691 mStreamInfoMap[streamId].finalized = true;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -08001692 mConfiguredOutputs.replaceValueFor(streamId, outputConfiguration);
Zhijun He5d677d12016-05-29 16:52:39 -07001693 } else if (err == NO_INIT) {
1694 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001695 "Camera %s: Deferred surface is invalid: %s (%d)",
1696 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001697 } else {
1698 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001699 "Camera %s: Error setting output stream deferred surface: %s (%d)",
1700 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001701 }
1702
1703 return res;
1704}
1705
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001706binder::Status CameraDeviceClient::setCameraAudioRestriction(int32_t mode) {
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001707 ATRACE_CALL();
1708 binder::Status res;
1709 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1710
1711 if (!isValidAudioRestriction(mode)) {
1712 String8 msg = String8::format("Camera %s: invalid audio restriction mode %d",
1713 mCameraIdStr.string(), mode);
1714 ALOGW("%s: %s", __FUNCTION__, msg.string());
1715 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1716 }
1717
1718 Mutex::Autolock icl(mBinderSerializationLock);
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001719 BasicClient::setAudioRestriction(mode);
1720 return binder::Status::ok();
1721}
1722
1723binder::Status CameraDeviceClient::getGlobalAudioRestriction(/*out*/ int32_t* outMode) {
1724 ATRACE_CALL();
1725 binder::Status res;
1726 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1727 Mutex::Autolock icl(mBinderSerializationLock);
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001728 if (outMode != nullptr) {
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001729 *outMode = BasicClient::getServiceAudioRestriction();
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001730 }
1731 return binder::Status::ok();
1732}
1733
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08001734status_t CameraDeviceClient::setRotateAndCropOverride(uint8_t rotateAndCrop) {
1735 if (rotateAndCrop > ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return BAD_VALUE;
1736
1737 return mDevice->setRotateAndCropAutoBehavior(
1738 static_cast<camera_metadata_enum_android_scaler_rotate_and_crop_t>(rotateAndCrop));
1739}
1740
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08001741bool CameraDeviceClient::supportsCameraMute() {
1742 return mDevice->supportsCameraMute();
1743}
1744
1745status_t CameraDeviceClient::setCameraMute(bool enabled) {
1746 return mDevice->setCameraMute(enabled);
1747}
1748
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001749binder::Status CameraDeviceClient::switchToOffline(
1750 const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb,
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001751 const std::vector<int>& offlineOutputIds,
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001752 /*out*/
1753 sp<hardware::camera2::ICameraOfflineSession>* session) {
1754 ATRACE_CALL();
1755
1756 binder::Status res;
1757 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1758
1759 Mutex::Autolock icl(mBinderSerializationLock);
1760
1761 if (!mDevice.get()) {
1762 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1763 }
1764
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001765 if (offlineOutputIds.empty()) {
Emilian Peevcc0b7952020-01-07 13:54:47 -08001766 String8 msg = String8::format("Offline surfaces must not be empty");
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001767 ALOGE("%s: %s", __FUNCTION__, msg.string());
1768 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1769 }
1770
1771 if (session == nullptr) {
1772 String8 msg = String8::format("Invalid offline session");
1773 ALOGE("%s: %s", __FUNCTION__, msg.string());
1774 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1775 }
1776
Yin-Chia Yeh87fccca2020-01-28 09:37:18 -08001777 std::vector<int32_t> offlineStreamIds;
1778 offlineStreamIds.reserve(offlineOutputIds.size());
Emilian Peev4697b642019-11-19 17:11:14 -08001779 KeyedVector<sp<IBinder>, sp<CompositeStream>> offlineCompositeStreamMap;
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001780 for (const auto& streamId : offlineOutputIds) {
1781 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001782 if (index == NAME_NOT_FOUND) {
Emilian Peevcc0b7952020-01-07 13:54:47 -08001783 String8 msg = String8::format("Offline surface with id: %d is not registered",
1784 streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001785 ALOGE("%s: %s", __FUNCTION__, msg.string());
1786 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1787 }
Emilian Peevcc0b7952020-01-07 13:54:47 -08001788
1789 if (!mStreamInfoMap[streamId].supportsOffline) {
1790 String8 msg = String8::format("Offline surface with id: %d doesn't support "
1791 "offline mode", streamId);
1792 ALOGE("%s: %s", __FUNCTION__, msg.string());
1793 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1794 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001795
Emilian Peev2f5d6012022-01-19 16:16:50 -08001796 Mutex::Autolock l(mCompositeLock);
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001797 bool isCompositeStream = false;
Emilian Peev2f5d6012022-01-19 16:16:50 -08001798 for (const auto& gbp : mConfiguredOutputs.valueAt(index).getGraphicBufferProducers()) {
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001799 sp<Surface> s = new Surface(gbp, false /*controlledByApp*/);
Pirama Arumuga Nainar8db21062022-01-28 10:15:12 -08001800 isCompositeStream = camera3::DepthCompositeStream::isDepthCompositeStream(s) ||
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001801 camera3::HeicCompositeStream::isHeicCompositeStream(s);
Emilian Peev4697b642019-11-19 17:11:14 -08001802 if (isCompositeStream) {
1803 auto compositeIdx = mCompositeStreamMap.indexOfKey(IInterface::asBinder(gbp));
1804 if (compositeIdx == NAME_NOT_FOUND) {
1805 ALOGE("%s: Unknown composite stream", __FUNCTION__);
1806 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1807 "Unknown composite stream");
1808 }
1809
1810 mCompositeStreamMap.valueAt(compositeIdx)->insertCompositeStreamIds(
1811 &offlineStreamIds);
1812 offlineCompositeStreamMap.add(mCompositeStreamMap.keyAt(compositeIdx),
1813 mCompositeStreamMap.valueAt(compositeIdx));
1814 break;
1815 }
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001816 }
1817
Emilian Peev4697b642019-11-19 17:11:14 -08001818 if (!isCompositeStream) {
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001819 offlineStreamIds.push_back(streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001820 }
1821 }
1822
1823 sp<CameraOfflineSessionBase> offlineSession;
1824 auto ret = mDevice->switchToOffline(offlineStreamIds, &offlineSession);
1825 if (ret != OK) {
1826 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1827 "Camera %s: Error switching to offline mode: %s (%d)",
1828 mCameraIdStr.string(), strerror(ret), ret);
1829 }
1830
Emilian Peevcc0b7952020-01-07 13:54:47 -08001831 sp<CameraOfflineSessionClient> offlineClient;
1832 if (offlineSession.get() != nullptr) {
1833 offlineClient = new CameraOfflineSessionClient(sCameraService,
1834 offlineSession, offlineCompositeStreamMap, cameraCb, mClientPackageName,
Emilian Peev8b64f282021-03-25 16:49:57 -07001835 mClientFeatureId, mCameraIdStr, mCameraFacing, mOrientation, mClientPid, mClientUid,
1836 mServicePid);
Emilian Peevcc0b7952020-01-07 13:54:47 -08001837 ret = sCameraService->addOfflineClient(mCameraIdStr, offlineClient);
1838 }
1839
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001840 if (ret == OK) {
Emilian Peevd99c8ae2019-11-26 13:19:13 -08001841 // A successful offline session switch must reset the current camera client
1842 // and release any resources occupied by previously configured streams.
1843 mStreamMap.clear();
1844 mConfiguredOutputs.clear();
1845 mDeferredStreams.clear();
1846 mStreamInfoMap.clear();
Emilian Peev2f5d6012022-01-19 16:16:50 -08001847 Mutex::Autolock l(mCompositeLock);
Emilian Peevd99c8ae2019-11-26 13:19:13 -08001848 mCompositeStreamMap.clear();
1849 mInputStream = {false, 0, 0, 0, 0};
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001850 } else {
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001851 switch(ret) {
1852 case BAD_VALUE:
1853 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1854 "Illegal argument to HAL module for camera \"%s\"", mCameraIdStr.c_str());
1855 case TIMED_OUT:
1856 return STATUS_ERROR_FMT(CameraService::ERROR_CAMERA_IN_USE,
1857 "Camera \"%s\" is already open", mCameraIdStr.c_str());
1858 default:
1859 return STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1860 "Failed to initialize camera \"%s\": %s (%d)", mCameraIdStr.c_str(),
1861 strerror(-ret), ret);
1862 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001863 }
1864
1865 *session = offlineClient;
1866
1867 return binder::Status::ok();
1868}
1869
Igor Murashkine7ee7632013-06-11 18:10:18 -07001870status_t CameraDeviceClient::dump(int fd, const Vector<String16>& args) {
Eino-Ville Talvalac4003962016-01-13 10:07:04 -08001871 return BasicClient::dump(fd, args);
1872}
1873
1874status_t CameraDeviceClient::dumpClient(int fd, const Vector<String16>& args) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001875 dprintf(fd, " CameraDeviceClient[%s] (%p) dump:\n",
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001876 mCameraIdStr.string(),
Eino-Ville Talvalae992e752014-11-07 16:17:48 -08001877 (getRemoteCallback() != NULL ?
Marco Nelissenf8880202014-11-14 07:58:25 -08001878 IInterface::asBinder(getRemoteCallback()).get() : NULL) );
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001879 dprintf(fd, " Current client UID %u\n", mClientUid);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001880
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001881 dprintf(fd, " State:\n");
1882 dprintf(fd, " Request ID counter: %d\n", mRequestIdCounter);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001883 if (mInputStream.configured) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001884 dprintf(fd, " Current input stream ID: %d\n", mInputStream.id);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001885 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001886 dprintf(fd, " No input stream configured.\n");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001887 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001888 if (!mStreamMap.isEmpty()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001889 dprintf(fd, " Current output stream/surface IDs:\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001890 for (size_t i = 0; i < mStreamMap.size(); i++) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001891 dprintf(fd, " Stream %d Surface %d\n",
Shuzhen Wang0129d522016-10-30 22:43:41 -07001892 mStreamMap.valueAt(i).streamId(),
1893 mStreamMap.valueAt(i).surfaceId());
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001894 }
Zhijun He5d677d12016-05-29 16:52:39 -07001895 } else if (!mDeferredStreams.isEmpty()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001896 dprintf(fd, " Current deferred surface output stream IDs:\n");
Zhijun He5d677d12016-05-29 16:52:39 -07001897 for (auto& streamId : mDeferredStreams) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001898 dprintf(fd, " Stream %d\n", streamId);
Zhijun He5d677d12016-05-29 16:52:39 -07001899 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001900 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001901 dprintf(fd, " No output streams configured.\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001902 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001903 // TODO: print dynamic/request section from most recent requests
1904 mFrameProcessor->dump(fd, args);
1905
1906 return dumpDevice(fd, args);
1907}
1908
Avichal Rakesh7e53cad2021-10-05 13:46:30 -07001909status_t CameraDeviceClient::startWatchingTags(const String8 &tags, int out) {
1910 sp<CameraDeviceBase> device = mDevice;
1911 if (!device) {
1912 dprintf(out, " Device is detached.");
1913 return OK;
1914 }
1915 device->startWatchingTags(tags);
1916 return OK;
1917}
1918
1919status_t CameraDeviceClient::stopWatchingTags(int out) {
1920 sp<CameraDeviceBase> device = mDevice;
1921 if (!device) {
1922 dprintf(out, " Device is detached.");
1923 return OK;
1924 }
1925 device->stopWatchingTags();
1926 return OK;
1927}
1928
1929status_t CameraDeviceClient::dumpWatchedEventsToVector(std::vector<std::string> &out) {
1930 sp<CameraDeviceBase> device = mDevice;
1931 if (!device) {
1932 return OK;
1933 }
1934 device->dumpWatchedEventsToVector(out);
1935 return OK;
1936}
1937
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001938void CameraDeviceClient::notifyError(int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -07001939 const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001940 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001941 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001942
Emilian Peev538c90e2018-12-17 18:03:19 +00001943 bool skipClientNotification = false;
Emilian Peev2f5d6012022-01-19 16:16:50 -08001944 {
1945 // Access to the composite stream map must be synchronized
1946 Mutex::Autolock l(mCompositeLock);
1947 // Composites can have multiple internal streams. Error notifications coming from such
1948 // internal streams may need to remain within camera service.
1949 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
1950 skipClientNotification |= mCompositeStreamMap.valueAt(i)->onError(errorCode,
1951 resultExtras);
1952 }
Emilian Peev538c90e2018-12-17 18:03:19 +00001953 }
1954
1955 if ((remoteCb != 0) && (!skipClientNotification)) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001956 remoteCb->onDeviceError(errorCode, resultExtras);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001957 }
1958}
1959
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001960void CameraDeviceClient::notifyRepeatingRequestError(long lastFrameNumber) {
1961 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
1962
1963 if (remoteCb != 0) {
Yin-Chia Yeh8ca23dc2017-09-05 18:15:56 -07001964 remoteCb->onRepeatingRequestError(lastFrameNumber, mStreamingRequestId);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001965 }
1966
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001967 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001968 mStreamingRequestId = REQUEST_ID_NONE;
1969}
1970
Shuzhen Wang316781a2020-08-18 18:11:01 -07001971void CameraDeviceClient::notifyIdle(
1972 int64_t requestCount, int64_t resultErrorCount, bool deviceError,
1973 const std::vector<hardware::CameraStreamStats>& streamStats) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001974 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001975 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001976
1977 if (remoteCb != 0) {
1978 remoteCb->onDeviceIdle();
1979 }
Shuzhen Wangd26b1862022-03-07 12:05:05 -08001980 Camera2ClientBase::notifyIdleWithUserTag(requestCount, resultErrorCount, deviceError,
Shuzhen Wang9372b0b2022-05-11 18:55:19 -07001981 streamStats, mUserTag, mVideoStabilizationMode);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001982}
1983
Jianing Weicb0652e2014-03-12 18:29:36 -07001984void CameraDeviceClient::notifyShutter(const CaptureResultExtras& resultExtras,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001985 nsecs_t timestamp) {
1986 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001987 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001988 if (remoteCb != 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001989 remoteCb->onCaptureStarted(resultExtras, timestamp);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001990 }
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07001991 Camera2ClientBase::notifyShutter(resultExtras, timestamp);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001992
Emilian Peev2f5d6012022-01-19 16:16:50 -08001993 // Access to the composite stream map must be synchronized
1994 Mutex::Autolock l(mCompositeLock);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001995 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
1996 mCompositeStreamMap.valueAt(i)->onShutter(resultExtras, timestamp);
1997 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001998}
1999
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002000void CameraDeviceClient::notifyPrepared(int streamId) {
2001 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002002 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002003 if (remoteCb != 0) {
2004 remoteCb->onPrepared(streamId);
2005 }
2006}
2007
Shuzhen Wang9d066012016-09-30 11:30:20 -07002008void CameraDeviceClient::notifyRequestQueueEmpty() {
2009 // Thread safe. Don't bother locking.
2010 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
2011 if (remoteCb != 0) {
2012 remoteCb->onRequestQueueEmpty();
2013 }
2014}
2015
Igor Murashkine7ee7632013-06-11 18:10:18 -07002016void CameraDeviceClient::detachDevice() {
2017 if (mDevice == 0) return;
2018
Shuzhen Wang316781a2020-08-18 18:11:01 -07002019 nsecs_t startTime = systemTime();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002020 ALOGV("Camera %s: Stopping processors", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -07002021
Emilian Peev7ee731f2022-02-08 14:55:52 -08002022 if (mFrameProcessor.get() != nullptr) {
2023 mFrameProcessor->removeListener(
2024 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MIN_ID,
2025 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MAX_ID, /*listener*/this);
2026 mFrameProcessor->requestExit();
2027 ALOGV("Camera %s: Waiting for threads", mCameraIdStr.string());
2028 mFrameProcessor->join();
2029 ALOGV("Camera %s: Disconnecting device", mCameraIdStr.string());
2030 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07002031
2032 // WORKAROUND: HAL refuses to disconnect while there's streams in flight
2033 {
Emilian Peev6b51d7d2018-07-23 11:41:44 +01002034 int64_t lastFrameNumber;
Igor Murashkine7ee7632013-06-11 18:10:18 -07002035 status_t code;
Emilian Peev6b51d7d2018-07-23 11:41:44 +01002036 if ((code = mDevice->flush(&lastFrameNumber)) != OK) {
2037 ALOGE("%s: flush failed with code 0x%x", __FUNCTION__, code);
2038 }
2039
Igor Murashkine7ee7632013-06-11 18:10:18 -07002040 if ((code = mDevice->waitUntilDrained()) != OK) {
2041 ALOGE("%s: waitUntilDrained failed with code 0x%x", __FUNCTION__,
2042 code);
2043 }
2044 }
2045
Emilian Peev2f5d6012022-01-19 16:16:50 -08002046 {
2047 Mutex::Autolock l(mCompositeLock);
2048 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
2049 auto ret = mCompositeStreamMap.valueAt(i)->deleteInternalStreams();
2050 if (ret != OK) {
2051 ALOGE("%s: Failed removing composite stream %s (%d)", __FUNCTION__,
2052 strerror(-ret), ret);
2053 }
Emilian Peev5e4c7322019-10-22 14:20:52 -07002054 }
Emilian Peev2f5d6012022-01-19 16:16:50 -08002055 mCompositeStreamMap.clear();
Emilian Peev5e4c7322019-10-22 14:20:52 -07002056 }
Emilian Peev5e4c7322019-10-22 14:20:52 -07002057
Igor Murashkine7ee7632013-06-11 18:10:18 -07002058 Camera2ClientBase::detachDevice();
Shuzhen Wang316781a2020-08-18 18:11:01 -07002059
2060 int32_t closeLatencyMs = ns2ms(systemTime() - startTime);
2061 CameraServiceProxyWrapper::logClose(mCameraIdStr, closeLatencyMs);
Igor Murashkine7ee7632013-06-11 18:10:18 -07002062}
2063
2064/** Device-related methods */
Jianing Weicb0652e2014-03-12 18:29:36 -07002065void CameraDeviceClient::onResultAvailable(const CaptureResult& result) {
Igor Murashkine7ee7632013-06-11 18:10:18 -07002066 ATRACE_CALL();
2067 ALOGV("%s", __FUNCTION__);
2068
Igor Murashkin4fb55c12013-08-29 17:43:01 -07002069 // Thread-safe. No lock necessary.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002070 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = mRemoteCallback;
Igor Murashkin4fb55c12013-08-29 17:43:01 -07002071 if (remoteCb != NULL) {
Shuzhen Wang5c22c152017-12-31 17:12:25 -08002072 remoteCb->onResultReceived(result.mMetadata, result.mResultExtras,
2073 result.mPhysicalMetadatas);
Igor Murashkine7ee7632013-06-11 18:10:18 -07002074 }
Emilian Peev538c90e2018-12-17 18:03:19 +00002075
Emilian Peev2f5d6012022-01-19 16:16:50 -08002076 // Access to the composite stream map must be synchronized
2077 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +00002078 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
2079 mCompositeStreamMap.valueAt(i)->onResultAvailable(result);
2080 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07002081}
2082
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002083binder::Status CameraDeviceClient::checkPidStatus(const char* checkLocation) {
Eino-Ville Talvala6192b892016-04-04 12:31:18 -07002084 if (mDisconnected) {
2085 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED,
2086 "The camera device has been disconnected");
2087 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002088 status_t res = checkPid(checkLocation);
2089 return (res == OK) ? binder::Status::ok() :
2090 STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
2091 "Attempt to use camera from a different process than original client");
2092}
2093
Igor Murashkine7ee7632013-06-11 18:10:18 -07002094// TODO: move to Camera2ClientBase
2095bool CameraDeviceClient::enforceRequestPermissions(CameraMetadata& metadata) {
2096
Jayant Chowdhary12361932018-08-27 14:46:13 -07002097 const int pid = CameraThreadState::getCallingPid();
Igor Murashkine7ee7632013-06-11 18:10:18 -07002098 const int selfPid = getpid();
2099 camera_metadata_entry_t entry;
2100
2101 /**
2102 * Mixin default important security values
2103 * - android.led.transmit = defaulted ON
2104 */
2105 CameraMetadata staticInfo = mDevice->info();
2106 entry = staticInfo.find(ANDROID_LED_AVAILABLE_LEDS);
2107 for(size_t i = 0; i < entry.count; ++i) {
2108 uint8_t led = entry.data.u8[i];
2109
2110 switch(led) {
2111 case ANDROID_LED_AVAILABLE_LEDS_TRANSMIT: {
2112 uint8_t transmitDefault = ANDROID_LED_TRANSMIT_ON;
2113 if (!metadata.exists(ANDROID_LED_TRANSMIT)) {
2114 metadata.update(ANDROID_LED_TRANSMIT,
2115 &transmitDefault, 1);
2116 }
2117 break;
2118 }
2119 }
2120 }
2121
2122 // We can do anything!
2123 if (pid == selfPid) {
2124 return true;
2125 }
2126
2127 /**
2128 * Permission check special fields in the request
2129 * - android.led.transmit = android.permission.CAMERA_DISABLE_TRANSMIT
2130 */
2131 entry = metadata.find(ANDROID_LED_TRANSMIT);
2132 if (entry.count > 0 && entry.data.u8[0] != ANDROID_LED_TRANSMIT_ON) {
2133 String16 permissionString =
2134 String16("android.permission.CAMERA_DISABLE_TRANSMIT_LED");
2135 if (!checkCallingPermission(permissionString)) {
Jayant Chowdhary12361932018-08-27 14:46:13 -07002136 const int uid = CameraThreadState::getCallingUid();
Igor Murashkine7ee7632013-06-11 18:10:18 -07002137 ALOGE("Permission Denial: "
2138 "can't disable transmit LED pid=%d, uid=%d", pid, uid);
2139 return false;
2140 }
2141 }
2142
2143 return true;
2144}
2145
Shuzhen Wang610d7b82022-02-08 14:37:22 -08002146status_t CameraDeviceClient::getRotationTransformLocked(int mirrorMode,
2147 int32_t* transform) {
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07002148 ALOGV("%s: begin", __FUNCTION__);
2149
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07002150 const CameraMetadata& staticInfo = mDevice->info();
Shuzhen Wang610d7b82022-02-08 14:37:22 -08002151 return CameraUtils::getRotationTransform(staticInfo, mirrorMode, transform);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07002152}
2153
Emilian Peevf4816702020-04-03 15:44:51 -07002154binder::Status CameraDeviceClient::mapRequestTemplate(int templateId,
2155 camera_request_template_t* tempId /*out*/) {
2156 binder::Status ret = binder::Status::ok();
2157
2158 if (tempId == nullptr) {
2159 ret = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
2160 "Camera %s: Invalid template argument", mCameraIdStr.string());
2161 return ret;
2162 }
2163 switch(templateId) {
2164 case ICameraDeviceUser::TEMPLATE_PREVIEW:
2165 *tempId = camera_request_template_t::CAMERA_TEMPLATE_PREVIEW;
2166 break;
2167 case ICameraDeviceUser::TEMPLATE_RECORD:
2168 *tempId = camera_request_template_t::CAMERA_TEMPLATE_VIDEO_RECORD;
2169 break;
2170 case ICameraDeviceUser::TEMPLATE_STILL_CAPTURE:
2171 *tempId = camera_request_template_t::CAMERA_TEMPLATE_STILL_CAPTURE;
2172 break;
2173 case ICameraDeviceUser::TEMPLATE_VIDEO_SNAPSHOT:
2174 *tempId = camera_request_template_t::CAMERA_TEMPLATE_VIDEO_SNAPSHOT;
2175 break;
2176 case ICameraDeviceUser::TEMPLATE_ZERO_SHUTTER_LAG:
2177 *tempId = camera_request_template_t::CAMERA_TEMPLATE_ZERO_SHUTTER_LAG;
2178 break;
2179 case ICameraDeviceUser::TEMPLATE_MANUAL:
2180 *tempId = camera_request_template_t::CAMERA_TEMPLATE_MANUAL;
2181 break;
2182 default:
2183 ret = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
2184 "Camera %s: Template ID %d is invalid or not supported",
2185 mCameraIdStr.string(), templateId);
2186 return ret;
2187 }
2188
2189 return ret;
2190}
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08002191
2192const CameraMetadata &CameraDeviceClient::getStaticInfo(const String8 &cameraId) {
2193 if (mDevice->getId() == cameraId) {
2194 return mDevice->info();
2195 }
2196 return mDevice->infoPhysical(cameraId);
2197}
2198
2199bool CameraDeviceClient::isUltraHighResolutionSensor(const String8 &cameraId) {
2200 const CameraMetadata &deviceInfo = getStaticInfo(cameraId);
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07002201 return SessionConfigurationUtils::isUltraHighResolutionSensor(deviceInfo);
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08002202}
2203
2204bool CameraDeviceClient::isSensorPixelModeConsistent(
2205 const std::list<int> &streamIdList, const CameraMetadata &settings) {
2206 // First we get the sensorPixelMode from the settings metadata.
2207 int32_t sensorPixelMode = ANDROID_SENSOR_PIXEL_MODE_DEFAULT;
2208 camera_metadata_ro_entry sensorPixelModeEntry = settings.find(ANDROID_SENSOR_PIXEL_MODE);
2209 if (sensorPixelModeEntry.count != 0) {
2210 sensorPixelMode = sensorPixelModeEntry.data.u8[0];
2211 if (sensorPixelMode != ANDROID_SENSOR_PIXEL_MODE_DEFAULT &&
2212 sensorPixelMode != ANDROID_SENSOR_PIXEL_MODE_MAXIMUM_RESOLUTION) {
2213 ALOGE("%s: Request sensor pixel mode not is not one of the valid values %d",
2214 __FUNCTION__, sensorPixelMode);
2215 return false;
2216 }
2217 }
2218 // Check whether each stream has max resolution allowed.
2219 bool consistent = true;
2220 for (auto it : streamIdList) {
2221 auto const streamInfoIt = mStreamInfoMap.find(it);
2222 if (streamInfoIt == mStreamInfoMap.end()) {
2223 ALOGE("%s: stream id %d not created, skipping", __FUNCTION__, it);
2224 return false;
2225 }
2226 consistent =
2227 streamInfoIt->second.sensorPixelModesUsed.find(sensorPixelMode) !=
2228 streamInfoIt->second.sensorPixelModesUsed.end();
2229 if (!consistent) {
2230 ALOGE("sensorPixelMode used %i not consistent with configured modes", sensorPixelMode);
2231 for (auto m : streamInfoIt->second.sensorPixelModesUsed) {
2232 ALOGE("sensor pixel mode used list: %i", m);
2233 }
2234 break;
2235 }
2236 }
2237
2238 return consistent;
2239}
2240
Igor Murashkine7ee7632013-06-11 18:10:18 -07002241} // namespace android