blob: d54ba4657d6df29bf6b05ed62078ceda356f0276 [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>
Austin Borger0fb3ad92023-06-01 16:51:35 -070029#include <camera/StringUtils.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070030
31#include "common/CameraDeviceBase.h"
Emilian Peev35ae8262018-11-08 13:11:32 +000032#include "device3/Camera3Device.h"
33#include "device3/Camera3OutputStream.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070034#include "api2/CameraDeviceClient.h"
Shuzhen Wang316781a2020-08-18 18:11:01 -070035#include "utils/CameraServiceProxyWrapper.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070036
Emilian Peev00420d22018-02-05 21:33:13 +000037#include <camera_metadata_hidden.h>
38
Emilian Peev538c90e2018-12-17 18:03:19 +000039#include "DepthCompositeStream.h"
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -080040#include "HeicCompositeStream.h"
Emilian Peev538c90e2018-12-17 18:03:19 +000041
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080042// Convenience methods for constructing binder::Status objects for error returns
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070043
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080044#define STATUS_ERROR(errorCode, errorString) \
45 binder::Status::fromServiceSpecificError(errorCode, \
Austin Borger0fb3ad92023-06-01 16:51:35 -070046 fmt::sprintf("%s:%d: %s", __FUNCTION__, __LINE__, errorString).c_str())
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080047
48#define STATUS_ERROR_FMT(errorCode, errorString, ...) \
49 binder::Status::fromServiceSpecificError(errorCode, \
Austin Borger0fb3ad92023-06-01 16:51:35 -070050 fmt::sprintf("%s:%d: " errorString, __FUNCTION__, __LINE__, \
51 __VA_ARGS__).c_str())
Igor Murashkine7ee7632013-06-11 18:10:18 -070052
53namespace android {
54using namespace camera2;
Austin Borgerea931242021-12-13 23:10:41 +000055using namespace camera3;
Emilian Peevf4816702020-04-03 15:44:51 -070056using camera3::camera_stream_rotation_t::CAMERA_STREAM_ROTATION_0;
Igor Murashkine7ee7632013-06-11 18:10:18 -070057
58CameraDeviceClientBase::CameraDeviceClientBase(
59 const sp<CameraService>& cameraService,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080060 const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback,
Austin Borger0fb3ad92023-06-01 16:51:35 -070061 const std::string& clientPackageName,
Jayant Chowdharyeb0169f2022-01-31 00:00:02 -080062 bool systemNativeClient,
Austin Borger0fb3ad92023-06-01 16:51:35 -070063 const std::optional<std::string>& clientFeatureId,
64 const std::string& cameraId,
Jing Mikec7f9b132023-03-12 11:12:04 +080065 [[maybe_unused]] int api1CameraId,
Igor Murashkine7ee7632013-06-11 18:10:18 -070066 int cameraFacing,
Emilian Peev8b64f282021-03-25 16:49:57 -070067 int sensorOrientation,
Igor Murashkine7ee7632013-06-11 18:10:18 -070068 int clientPid,
69 uid_t clientUid,
Austin Borger3560b7e2022-10-27 12:20:29 -070070 int servicePid,
71 bool overrideToPortrait) :
Eino-Ville Talvalae992e752014-11-07 16:17:48 -080072 BasicClient(cameraService,
Marco Nelissenf8880202014-11-14 07:58:25 -080073 IInterface::asBinder(remoteCallback),
Eino-Ville Talvalae992e752014-11-07 16:17:48 -080074 clientPackageName,
Jayant Chowdharyeb0169f2022-01-31 00:00:02 -080075 systemNativeClient,
Philip P. Moltmann9e648f62019-11-04 12:52:45 -080076 clientFeatureId,
Eino-Ville Talvalae992e752014-11-07 16:17:48 -080077 cameraId,
78 cameraFacing,
Emilian Peev8b64f282021-03-25 16:49:57 -070079 sensorOrientation,
Eino-Ville Talvalae992e752014-11-07 16:17:48 -080080 clientPid,
81 clientUid,
Austin Borger3560b7e2022-10-27 12:20:29 -070082 servicePid,
83 overrideToPortrait),
Igor Murashkine7ee7632013-06-11 18:10:18 -070084 mRemoteCallback(remoteCallback) {
85}
Igor Murashkine7ee7632013-06-11 18:10:18 -070086
87// Interface used by CameraService
88
89CameraDeviceClient::CameraDeviceClient(const sp<CameraService>& cameraService,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080090 const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback,
Austin Borger0fb3ad92023-06-01 16:51:35 -070091 const std::string& clientPackageName,
Jayant Chowdharyeb0169f2022-01-31 00:00:02 -080092 bool systemNativeClient,
Austin Borger0fb3ad92023-06-01 16:51:35 -070093 const std::optional<std::string>& clientFeatureId,
94 const std::string& cameraId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080095 int cameraFacing,
Emilian Peev8b64f282021-03-25 16:49:57 -070096 int sensorOrientation,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080097 int clientPid,
98 uid_t clientUid,
Shuzhen Wangd4abdf72021-05-28 11:22:50 -070099 int servicePid,
Austin Borger3560b7e2022-10-27 12:20:29 -0700100 bool overrideForPerfClass,
101 bool overrideToPortrait) :
Jayant Chowdharyeb0169f2022-01-31 00:00:02 -0800102 Camera2ClientBase(cameraService, remoteCallback, clientPackageName, systemNativeClient,
103 clientFeatureId, cameraId, /*API1 camera ID*/ -1, cameraFacing, sensorOrientation,
Austin Borger3560b7e2022-10-27 12:20:29 -0700104 clientPid, clientUid, servicePid, overrideForPerfClass, overrideToPortrait),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700105 mInputStream(),
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700106 mStreamingRequestId(REQUEST_ID_NONE),
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700107 mRequestIdCounter(0),
108 mOverrideForPerfClass(overrideForPerfClass) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700109
110 ATRACE_CALL();
Austin Borger0fb3ad92023-06-01 16:51:35 -0700111 ALOGI("CameraDeviceClient %s: Opened", cameraId.c_str());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700112}
113
Emilian Peevbd8c5032018-02-14 23:05:40 +0000114status_t CameraDeviceClient::initialize(sp<CameraProviderManager> manager,
Austin Borger0fb3ad92023-06-01 16:51:35 -0700115 const std::string& monitorTags) {
Emilian Peevbd8c5032018-02-14 23:05:40 +0000116 return initializeImpl(manager, monitorTags);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800117}
118
119template<typename TProviderPtr>
Austin Borger0fb3ad92023-06-01 16:51:35 -0700120status_t CameraDeviceClient::initializeImpl(TProviderPtr providerPtr,
121 const std::string& monitorTags) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700122 ATRACE_CALL();
123 status_t res;
124
Emilian Peevbd8c5032018-02-14 23:05:40 +0000125 res = Camera2ClientBase::initialize(providerPtr, monitorTags);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700126 if (res != OK) {
127 return res;
128 }
129
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -0700130 mFrameProcessor = new FrameProcessorBase(mDevice);
Austin Borger0fb3ad92023-06-01 16:51:35 -0700131 std::string threadName = std::string("CDU-") + mCameraIdStr + "-FrameProc";
132 mFrameProcessor->run(threadName.c_str());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700133
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800134 mFrameProcessor->registerListener(camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MIN_ID,
135 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MAX_ID,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -0800136 /*listener*/this,
Zhijun He25a0aef2014-06-25 11:40:02 -0700137 /*sendPartials*/true);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700138
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800139 const CameraMetadata &deviceInfo = mDevice->info();
140 camera_metadata_ro_entry_t physicalKeysEntry = deviceInfo.find(
Emilian Peev00420d22018-02-05 21:33:13 +0000141 ANDROID_REQUEST_AVAILABLE_PHYSICAL_CAMERA_REQUEST_KEYS);
142 if (physicalKeysEntry.count > 0) {
143 mSupportedPhysicalRequestKeys.insert(mSupportedPhysicalRequestKeys.begin(),
144 physicalKeysEntry.data.i32,
145 physicalKeysEntry.data.i32 + physicalKeysEntry.count);
146 }
147
Emilian Peev2295df72021-11-12 18:14:10 -0800148 auto entry = deviceInfo.find(ANDROID_REQUEST_AVAILABLE_CAPABILITIES);
149 mDynamicProfileMap.emplace(
150 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD,
151 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD);
152 if (entry.count > 0) {
Shuzhen Wangc8ab4522021-12-14 20:12:42 -0800153 const auto it = std::find(entry.data.u8, entry.data.u8 + entry.count,
Emilian Peev2295df72021-11-12 18:14:10 -0800154 ANDROID_REQUEST_AVAILABLE_CAPABILITIES_DYNAMIC_RANGE_TEN_BIT);
Shuzhen Wangc8ab4522021-12-14 20:12:42 -0800155 if (it != entry.data.u8 + entry.count) {
Emilian Peev2295df72021-11-12 18:14:10 -0800156 entry = deviceInfo.find(ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP);
Emilian Peevc81a7592022-02-14 17:38:18 -0800157 if (entry.count > 0 || ((entry.count % 3) != 0)) {
158 int64_t standardBitmap =
159 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD;
160 for (size_t i = 0; i < entry.count; i += 3) {
161 if (entry.data.i64[i] !=
Emilian Peev2295df72021-11-12 18:14:10 -0800162 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD) {
Emilian Peevc81a7592022-02-14 17:38:18 -0800163 mDynamicProfileMap.emplace(entry.data.i64[i], entry.data.i64[i+1]);
164 if ((entry.data.i64[i+1] == 0) || (entry.data.i64[i+1] &
Emilian Peev2295df72021-11-12 18:14:10 -0800165 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD)) {
Emilian Peevc81a7592022-02-14 17:38:18 -0800166 standardBitmap |= entry.data.i64[i];
Emilian Peev2295df72021-11-12 18:14:10 -0800167 }
168 } else {
Emilian Peevc81a7592022-02-14 17:38:18 -0800169 ALOGE("%s: Device %s includes unexpected profile entry: 0x%" PRIx64 "!",
170 __FUNCTION__, mCameraIdStr.c_str(), entry.data.i64[i]);
Emilian Peev2295df72021-11-12 18:14:10 -0800171 }
172 }
Emilian Peevef715e22022-05-19 17:44:33 -0700173 mDynamicProfileMap[ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD] =
174 standardBitmap;
Emilian Peev2295df72021-11-12 18:14:10 -0800175 } else {
176 ALOGE("%s: Device %s supports 10-bit output but doesn't include a dynamic range"
177 " profile map!", __FUNCTION__, mCameraIdStr.c_str());
178 }
179 }
180 }
181
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700182 mProviderManager = providerPtr;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800183 // Cache physical camera ids corresponding to this device and also the high
184 // resolution sensors in this device + physical camera ids
Austin Borger0fb3ad92023-06-01 16:51:35 -0700185 mProviderManager->isLogicalCamera(mCameraIdStr, &mPhysicalCameraIds);
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800186 if (isUltraHighResolutionSensor(mCameraIdStr)) {
Austin Borger0fb3ad92023-06-01 16:51:35 -0700187 mHighResolutionSensors.insert(mCameraIdStr);
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800188 }
189 for (auto &physicalId : mPhysicalCameraIds) {
Austin Borger0fb3ad92023-06-01 16:51:35 -0700190 if (isUltraHighResolutionSensor(physicalId)) {
191 mHighResolutionSensors.insert(physicalId);
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800192 }
193 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700194 return OK;
195}
196
197CameraDeviceClient::~CameraDeviceClient() {
198}
199
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800200binder::Status CameraDeviceClient::submitRequest(
201 const hardware::camera2::CaptureRequest& request,
202 bool streaming,
203 /*out*/
204 hardware::camera2::utils::SubmitInfo *submitInfo) {
205 std::vector<hardware::camera2::CaptureRequest> requestList = { request };
206 return submitRequestList(requestList, streaming, submitInfo);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700207}
208
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800209binder::Status CameraDeviceClient::insertGbpLocked(const sp<IGraphicBufferProducer>& gbp,
Emilian Peevf873aa52018-01-26 14:58:28 +0000210 SurfaceMap* outSurfaceMap, Vector<int32_t>* outputStreamIds, int32_t *currentStreamId) {
Emilian Peev538c90e2018-12-17 18:03:19 +0000211 int compositeIdx;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800212 int idx = mStreamMap.indexOfKey(IInterface::asBinder(gbp));
213
Emilian Peev2f5d6012022-01-19 16:16:50 -0800214 Mutex::Autolock l(mCompositeLock);
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800215 // Trying to submit request with surface that wasn't created
216 if (idx == NAME_NOT_FOUND) {
217 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
218 " we have not called createStream on",
Austin Borger0fb3ad92023-06-01 16:51:35 -0700219 __FUNCTION__, mCameraIdStr.c_str());
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800220 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
221 "Request targets Surface that is not part of current capture session");
Emilian Peev538c90e2018-12-17 18:03:19 +0000222 } else if ((compositeIdx = mCompositeStreamMap.indexOfKey(IInterface::asBinder(gbp)))
223 != NAME_NOT_FOUND) {
224 mCompositeStreamMap.valueAt(compositeIdx)->insertGbp(outSurfaceMap, outputStreamIds,
225 currentStreamId);
226 return binder::Status::ok();
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800227 }
228
229 const StreamSurfaceId& streamSurfaceId = mStreamMap.valueAt(idx);
230 if (outSurfaceMap->find(streamSurfaceId.streamId()) == outSurfaceMap->end()) {
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800231 outputStreamIds->push_back(streamSurfaceId.streamId());
232 }
233 (*outSurfaceMap)[streamSurfaceId.streamId()].push_back(streamSurfaceId.surfaceId());
234
235 ALOGV("%s: Camera %s: Appending output stream %d surface %d to request",
Austin Borger0fb3ad92023-06-01 16:51:35 -0700236 __FUNCTION__, mCameraIdStr.c_str(), streamSurfaceId.streamId(),
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800237 streamSurfaceId.surfaceId());
238
Emilian Peevf873aa52018-01-26 14:58:28 +0000239 if (currentStreamId != nullptr) {
240 *currentStreamId = streamSurfaceId.streamId();
241 }
242
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800243 return binder::Status::ok();
244}
245
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800246static std::list<int> getIntersection(const std::unordered_set<int> &streamIdsForThisCamera,
247 const Vector<int> &streamIdsForThisRequest) {
248 std::list<int> intersection;
249 for (auto &streamId : streamIdsForThisRequest) {
250 if (streamIdsForThisCamera.find(streamId) != streamIdsForThisCamera.end()) {
251 intersection.emplace_back(streamId);
252 }
253 }
254 return intersection;
255}
256
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800257binder::Status CameraDeviceClient::submitRequestList(
258 const std::vector<hardware::camera2::CaptureRequest>& requests,
259 bool streaming,
260 /*out*/
261 hardware::camera2::utils::SubmitInfo *submitInfo) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700262 ATRACE_CALL();
Mark Salyzyn50468412014-06-18 16:33:43 -0700263 ALOGV("%s-start of function. Request list size %zu", __FUNCTION__, requests.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700264
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800265 binder::Status res = binder::Status::ok();
266 status_t err;
267 if ( !(res = checkPidStatus(__FUNCTION__) ).isOk()) {
268 return res;
269 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700270
271 Mutex::Autolock icl(mBinderSerializationLock);
272
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800273 if (!mDevice.get()) {
274 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
275 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700276
277 if (requests.empty()) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800278 ALOGE("%s: Camera %s: Sent null request. Rejecting request.",
Austin Borger0fb3ad92023-06-01 16:51:35 -0700279 __FUNCTION__, mCameraIdStr.c_str());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800280 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Empty request list");
Jianing Wei90e59c92014-03-12 18:29:36 -0700281 }
282
Emilian Peevaebbe412018-01-15 13:53:24 +0000283 List<const CameraDeviceBase::PhysicalCameraSettingsList> metadataRequestList;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700284 std::list<const SurfaceMap> surfaceMapList;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800285 submitInfo->mRequestId = mRequestIdCounter;
Jianing Wei90e59c92014-03-12 18:29:36 -0700286 uint32_t loopCounter = 0;
287
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800288 for (auto&& request: requests) {
289 if (request.mIsReprocess) {
Chien-Yu Chened0412e2015-04-27 15:04:22 -0700290 if (!mInputStream.configured) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800291 ALOGE("%s: Camera %s: no input stream is configured.", __FUNCTION__,
Austin Borger0fb3ad92023-06-01 16:51:35 -0700292 mCameraIdStr.c_str());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800293 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800294 "No input configured for camera %s but request is for reprocessing",
Austin Borger0fb3ad92023-06-01 16:51:35 -0700295 mCameraIdStr.c_str());
Chien-Yu Chened0412e2015-04-27 15:04:22 -0700296 } else if (streaming) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800297 ALOGE("%s: Camera %s: streaming reprocess requests not supported.", __FUNCTION__,
Austin Borger0fb3ad92023-06-01 16:51:35 -0700298 mCameraIdStr.c_str());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800299 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
300 "Repeating reprocess requests not supported");
Emilian Peevaebbe412018-01-15 13:53:24 +0000301 } else if (request.mPhysicalCameraSettings.size() > 1) {
302 ALOGE("%s: Camera %s: reprocess requests not supported for "
303 "multiple physical cameras.", __FUNCTION__,
Austin Borger0fb3ad92023-06-01 16:51:35 -0700304 mCameraIdStr.c_str());
Emilian Peevaebbe412018-01-15 13:53:24 +0000305 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
306 "Reprocess requests not supported for multiple cameras");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700307 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700308 }
309
Emilian Peevaebbe412018-01-15 13:53:24 +0000310 if (request.mPhysicalCameraSettings.empty()) {
311 ALOGE("%s: Camera %s: request doesn't contain any settings.", __FUNCTION__,
Austin Borger0fb3ad92023-06-01 16:51:35 -0700312 mCameraIdStr.c_str());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800313 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
Emilian Peevaebbe412018-01-15 13:53:24 +0000314 "Request doesn't contain any settings");
315 }
316
317 //The first capture settings should always match the logical camera id
Austin Borger0fb3ad92023-06-01 16:51:35 -0700318 const std::string &logicalId = request.mPhysicalCameraSettings.begin()->id;
Emilian Peevaebbe412018-01-15 13:53:24 +0000319 if (mDevice->getId() != logicalId) {
320 ALOGE("%s: Camera %s: Invalid camera request settings.", __FUNCTION__,
Austin Borger0fb3ad92023-06-01 16:51:35 -0700321 mCameraIdStr.c_str());
Emilian Peevaebbe412018-01-15 13:53:24 +0000322 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
323 "Invalid camera request settings");
324 }
325
Emilian Peevaebbe412018-01-15 13:53:24 +0000326 if (request.mSurfaceList.isEmpty() && request.mStreamIdxList.size() == 0) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800327 ALOGE("%s: Camera %s: Requests must have at least one surface target. "
Austin Borger0fb3ad92023-06-01 16:51:35 -0700328 "Rejecting request.", __FUNCTION__, mCameraIdStr.c_str());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800329 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
330 "Request has no output targets");
Jianing Wei90e59c92014-03-12 18:29:36 -0700331 }
332
Jianing Wei90e59c92014-03-12 18:29:36 -0700333 /**
Shuzhen Wang0129d522016-10-30 22:43:41 -0700334 * Write in the output stream IDs and map from stream ID to surface ID
335 * which we calculate from the capture request's list of surface target
Jianing Wei90e59c92014-03-12 18:29:36 -0700336 */
Shuzhen Wang0129d522016-10-30 22:43:41 -0700337 SurfaceMap surfaceMap;
Jianing Wei90e59c92014-03-12 18:29:36 -0700338 Vector<int32_t> outputStreamIds;
Emilian Peevf873aa52018-01-26 14:58:28 +0000339 std::vector<std::string> requestedPhysicalIds;
Emilian Peevc81a7592022-02-14 17:38:18 -0800340 int64_t dynamicProfileBitmap = 0;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800341 if (request.mSurfaceList.size() > 0) {
Chih-Hung Hsieh3ef324d2018-12-11 11:48:12 -0800342 for (const sp<Surface>& surface : request.mSurfaceList) {
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800343 if (surface == 0) continue;
Jianing Wei90e59c92014-03-12 18:29:36 -0700344
Emilian Peevf873aa52018-01-26 14:58:28 +0000345 int32_t streamId;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800346 sp<IGraphicBufferProducer> gbp = surface->getIGraphicBufferProducer();
Emilian Peevf873aa52018-01-26 14:58:28 +0000347 res = insertGbpLocked(gbp, &surfaceMap, &outputStreamIds, &streamId);
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800348 if (!res.isOk()) {
349 return res;
350 }
Emilian Peevf873aa52018-01-26 14:58:28 +0000351
352 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
353 if (index >= 0) {
Austin Borger0fb3ad92023-06-01 16:51:35 -0700354 const std::string &requestedPhysicalId =
355 mConfiguredOutputs.valueAt(index).getPhysicalCameraId();
356 requestedPhysicalIds.push_back(requestedPhysicalId);
Emilian Peev2295df72021-11-12 18:14:10 -0800357 dynamicProfileBitmap |=
358 mConfiguredOutputs.valueAt(index).getDynamicRangeProfile();
Emilian Peevf873aa52018-01-26 14:58:28 +0000359 } else {
360 ALOGW("%s: Output stream Id not found among configured outputs!", __FUNCTION__);
361 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700362 }
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800363 } else {
364 for (size_t i = 0; i < request.mStreamIdxList.size(); i++) {
365 int streamId = request.mStreamIdxList.itemAt(i);
366 int surfaceIdx = request.mSurfaceIdxList.itemAt(i);
Jianing Wei90e59c92014-03-12 18:29:36 -0700367
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800368 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
369 if (index < 0) {
370 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
371 " we have not called createStream on: stream %d",
Austin Borger0fb3ad92023-06-01 16:51:35 -0700372 __FUNCTION__, mCameraIdStr.c_str(), streamId);
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800373 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
374 "Request targets Surface that is not part of current capture session");
375 }
376
377 const auto& gbps = mConfiguredOutputs.valueAt(index).getGraphicBufferProducers();
378 if ((size_t)surfaceIdx >= gbps.size()) {
379 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
380 " we have not called createStream on: stream %d, surfaceIdx %d",
Austin Borger0fb3ad92023-06-01 16:51:35 -0700381 __FUNCTION__, mCameraIdStr.c_str(), streamId, surfaceIdx);
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800382 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
383 "Request targets Surface has invalid surface index");
384 }
385
Emilian Peevf873aa52018-01-26 14:58:28 +0000386 res = insertGbpLocked(gbps[surfaceIdx], &surfaceMap, &outputStreamIds, nullptr);
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800387 if (!res.isOk()) {
388 return res;
389 }
Emilian Peevf873aa52018-01-26 14:58:28 +0000390
Austin Borger0fb3ad92023-06-01 16:51:35 -0700391 const std::string &requestedPhysicalId =
392 mConfiguredOutputs.valueAt(index).getPhysicalCameraId();
393 requestedPhysicalIds.push_back(requestedPhysicalId);
Emilian Peev2295df72021-11-12 18:14:10 -0800394 dynamicProfileBitmap |=
395 mConfiguredOutputs.valueAt(index).getDynamicRangeProfile();
396 }
397 }
398
399 if (dynamicProfileBitmap !=
400 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD) {
401 for (int i = ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD;
402 i < ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_MAX; i <<= 1) {
403 if ((dynamicProfileBitmap & i) == 0) {
404 continue;
405 }
406
407 const auto& it = mDynamicProfileMap.find(i);
408 if (it != mDynamicProfileMap.end()) {
409 if ((it->second == 0) ||
410 ((it->second & dynamicProfileBitmap) == dynamicProfileBitmap)) {
411 continue;
412 } else {
413 ALOGE("%s: Camera %s: Tried to submit a request with a surfaces that"
414 " reference an unsupported dynamic range profile combination"
Austin Borger0fb3ad92023-06-01 16:51:35 -0700415 " 0x%" PRIx64 "!", __FUNCTION__, mCameraIdStr.c_str(),
Emilian Peev2295df72021-11-12 18:14:10 -0800416 dynamicProfileBitmap);
417 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
418 "Request targets an unsupported dynamic range profile"
419 " combination");
420 }
421 } else {
422 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
423 " references unsupported dynamic range profile 0x%x!",
Austin Borger0fb3ad92023-06-01 16:51:35 -0700424 __FUNCTION__, mCameraIdStr.c_str(), i);
Emilian Peev2295df72021-11-12 18:14:10 -0800425 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
426 "Request targets 10-bit Surface with unsupported dynamic range"
427 " profile");
428 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700429 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700430 }
431
Emilian Peevf873aa52018-01-26 14:58:28 +0000432 CameraDeviceBase::PhysicalCameraSettingsList physicalSettingsList;
433 for (const auto& it : request.mPhysicalCameraSettings) {
Emilian Peev00420d22018-02-05 21:33:13 +0000434 if (it.settings.isEmpty()) {
435 ALOGE("%s: Camera %s: Sent empty metadata packet. Rejecting request.",
Austin Borger0fb3ad92023-06-01 16:51:35 -0700436 __FUNCTION__, mCameraIdStr.c_str());
Emilian Peev00420d22018-02-05 21:33:13 +0000437 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
438 "Request settings are empty");
439 }
440
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800441 // Check whether the physical / logical stream has settings
442 // consistent with the sensor pixel mode(s) it was configured with.
443 // mCameraIdToStreamSet will only have ids that are high resolution
444 const auto streamIdSetIt = mHighResolutionCameraIdToStreamIdSet.find(it.id);
445 if (streamIdSetIt != mHighResolutionCameraIdToStreamIdSet.end()) {
446 std::list<int> streamIdsUsedInRequest = getIntersection(streamIdSetIt->second,
447 outputStreamIds);
448 if (!request.mIsReprocess &&
449 !isSensorPixelModeConsistent(streamIdsUsedInRequest, it.settings)) {
450 ALOGE("%s: Camera %s: Request settings CONTROL_SENSOR_PIXEL_MODE not "
451 "consistent with configured streams. Rejecting request.",
452 __FUNCTION__, it.id.c_str());
453 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
454 "Request settings CONTROL_SENSOR_PIXEL_MODE are not consistent with "
455 "streams configured");
456 }
457 }
458
Austin Borger0fb3ad92023-06-01 16:51:35 -0700459 const std::string &physicalId = it.id;
Shuzhen Wang911c6a32021-10-27 13:36:03 -0700460 bool hasTestPatternModePhysicalKey = std::find(mSupportedPhysicalRequestKeys.begin(),
461 mSupportedPhysicalRequestKeys.end(), ANDROID_SENSOR_TEST_PATTERN_MODE) !=
462 mSupportedPhysicalRequestKeys.end();
463 bool hasTestPatternDataPhysicalKey = std::find(mSupportedPhysicalRequestKeys.begin(),
464 mSupportedPhysicalRequestKeys.end(), ANDROID_SENSOR_TEST_PATTERN_DATA) !=
465 mSupportedPhysicalRequestKeys.end();
Emilian Peevf873aa52018-01-26 14:58:28 +0000466 if (physicalId != mDevice->getId()) {
467 auto found = std::find(requestedPhysicalIds.begin(), requestedPhysicalIds.end(),
468 it.id);
469 if (found == requestedPhysicalIds.end()) {
470 ALOGE("%s: Camera %s: Physical camera id: %s not part of attached outputs.",
Austin Borger0fb3ad92023-06-01 16:51:35 -0700471 __FUNCTION__, mCameraIdStr.c_str(), physicalId.c_str());
Emilian Peevf873aa52018-01-26 14:58:28 +0000472 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
473 "Invalid physical camera id");
474 }
Emilian Peev00420d22018-02-05 21:33:13 +0000475
476 if (!mSupportedPhysicalRequestKeys.empty()) {
477 // Filter out any unsupported physical request keys.
478 CameraMetadata filteredParams(mSupportedPhysicalRequestKeys.size());
479 camera_metadata_t *meta = const_cast<camera_metadata_t *>(
480 filteredParams.getAndLock());
481 set_camera_metadata_vendor_id(meta, mDevice->getVendorTagId());
482 filteredParams.unlock(meta);
483
484 for (const auto& keyIt : mSupportedPhysicalRequestKeys) {
485 camera_metadata_ro_entry entry = it.settings.find(keyIt);
486 if (entry.count > 0) {
487 filteredParams.update(entry);
488 }
489 }
490
Shuzhen Wang911c6a32021-10-27 13:36:03 -0700491 physicalSettingsList.push_back({it.id, filteredParams,
492 hasTestPatternModePhysicalKey, hasTestPatternDataPhysicalKey});
Emilian Peev00420d22018-02-05 21:33:13 +0000493 }
494 } else {
495 physicalSettingsList.push_back({it.id, it.settings});
Emilian Peevf873aa52018-01-26 14:58:28 +0000496 }
Emilian Peevf873aa52018-01-26 14:58:28 +0000497 }
498
499 if (!enforceRequestPermissions(physicalSettingsList.begin()->metadata)) {
500 // Callee logs
501 return STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
502 "Caller does not have permission to change restricted controls");
503 }
504
Emilian Peevaebbe412018-01-15 13:53:24 +0000505 physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_OUTPUT_STREAMS,
506 &outputStreamIds[0], outputStreamIds.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700507
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800508 if (request.mIsReprocess) {
Emilian Peevaebbe412018-01-15 13:53:24 +0000509 physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_INPUT_STREAMS,
510 &mInputStream.id, 1);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700511 }
512
Emilian Peevaebbe412018-01-15 13:53:24 +0000513 physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_ID,
514 &(submitInfo->mRequestId), /*size*/1);
Jianing Wei90e59c92014-03-12 18:29:36 -0700515 loopCounter++; // loopCounter starts from 1
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800516 ALOGV("%s: Camera %s: Creating request with ID %d (%d of %zu)",
Austin Borger0fb3ad92023-06-01 16:51:35 -0700517 __FUNCTION__, mCameraIdStr.c_str(), submitInfo->mRequestId,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800518 loopCounter, requests.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700519
Emilian Peevaebbe412018-01-15 13:53:24 +0000520 metadataRequestList.push_back(physicalSettingsList);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700521 surfaceMapList.push_back(surfaceMap);
Shuzhen Wangd26b1862022-03-07 12:05:05 -0800522
Shuzhen Wang9372b0b2022-05-11 18:55:19 -0700523 // Save certain CaptureRequest settings
Shuzhen Wangd26b1862022-03-07 12:05:05 -0800524 if (!request.mUserTag.empty()) {
525 mUserTag = request.mUserTag;
526 }
Shuzhen Wang9372b0b2022-05-11 18:55:19 -0700527 camera_metadata_entry entry =
528 physicalSettingsList.begin()->metadata.find(
529 ANDROID_CONTROL_VIDEO_STABILIZATION_MODE);
530 if (entry.count == 1) {
531 mVideoStabilizationMode = entry.data.u8[0];
532 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700533 }
534 mRequestIdCounter++;
535
536 if (streaming) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700537 err = mDevice->setStreamingRequestList(metadataRequestList, surfaceMapList,
538 &(submitInfo->mLastFrameNumber));
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800539 if (err != OK) {
Austin Borger0fb3ad92023-06-01 16:51:35 -0700540 std::string msg = fmt::sprintf(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800541 "Camera %s: Got error %s (%d) after trying to set streaming request",
Austin Borger0fb3ad92023-06-01 16:51:35 -0700542 mCameraIdStr.c_str(), strerror(-err), err);
543 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800544 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
Austin Borger0fb3ad92023-06-01 16:51:35 -0700545 msg.c_str());
Jianing Wei90e59c92014-03-12 18:29:36 -0700546 } else {
Shuzhen Wangc9ca6782016-04-26 13:40:31 -0700547 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700548 mStreamingRequestId = submitInfo->mRequestId;
Jianing Wei90e59c92014-03-12 18:29:36 -0700549 }
550 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700551 err = mDevice->captureList(metadataRequestList, surfaceMapList,
552 &(submitInfo->mLastFrameNumber));
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800553 if (err != OK) {
Austin Borger0fb3ad92023-06-01 16:51:35 -0700554 std::string msg = fmt::sprintf(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800555 "Camera %s: Got error %s (%d) after trying to submit capture request",
Austin Borger0fb3ad92023-06-01 16:51:35 -0700556 mCameraIdStr.c_str(), strerror(-err), err);
557 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800558 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
Austin Borger0fb3ad92023-06-01 16:51:35 -0700559 msg.c_str());
Jianing Wei90e59c92014-03-12 18:29:36 -0700560 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800561 ALOGV("%s: requestId = %d ", __FUNCTION__, submitInfo->mRequestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700562 }
563
Austin Borger0fb3ad92023-06-01 16:51:35 -0700564 ALOGV("%s: Camera %s: End of function", __FUNCTION__, mCameraIdStr.c_str());
Jianing Wei90e59c92014-03-12 18:29:36 -0700565 return res;
566}
567
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800568binder::Status CameraDeviceClient::cancelRequest(
569 int requestId,
570 /*out*/
571 int64_t* lastFrameNumber) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700572 ATRACE_CALL();
573 ALOGV("%s, requestId = %d", __FUNCTION__, requestId);
574
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800575 status_t err;
576 binder::Status res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700577
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800578 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700579
580 Mutex::Autolock icl(mBinderSerializationLock);
581
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800582 if (!mDevice.get()) {
583 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
584 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700585
Shuzhen Wangc9ca6782016-04-26 13:40:31 -0700586 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700587 if (mStreamingRequestId != requestId) {
Austin Borger0fb3ad92023-06-01 16:51:35 -0700588 std::string msg = fmt::sprintf("Camera %s: Canceling request ID %d doesn't match "
589 "current request ID %d", mCameraIdStr.c_str(), requestId, mStreamingRequestId);
590 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
591 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700592 }
593
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800594 err = mDevice->clearStreamingRequest(lastFrameNumber);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700595
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800596 if (err == OK) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800597 ALOGV("%s: Camera %s: Successfully cleared streaming request",
Austin Borger0fb3ad92023-06-01 16:51:35 -0700598 __FUNCTION__, mCameraIdStr.c_str());
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700599 mStreamingRequestId = REQUEST_ID_NONE;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800600 } else {
601 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800602 "Camera %s: Error clearing streaming request: %s (%d)",
Austin Borger0fb3ad92023-06-01 16:51:35 -0700603 mCameraIdStr.c_str(), strerror(-err), err);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700604 }
605
606 return res;
607}
608
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800609binder::Status CameraDeviceClient::beginConfigure() {
Ruben Brunkb2119af2014-05-09 19:57:56 -0700610 // TODO: Implement this.
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -0700611 ATRACE_CALL();
Zhijun He1fa89992015-06-01 15:44:31 -0700612 ALOGV("%s: Not implemented yet.", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800613 return binder::Status::ok();
Ruben Brunkb2119af2014-05-09 19:57:56 -0700614}
615
Emilian Peev5fbe0ba2017-10-20 15:45:45 +0100616binder::Status CameraDeviceClient::endConfigure(int operatingMode,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700617 const hardware::camera2::impl::CameraMetadataNative& sessionParams, int64_t startTimeMs,
Emilian Peevcc0b7952020-01-07 13:54:47 -0800618 std::vector<int>* offlineStreamIds /*out*/) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -0700619 ATRACE_CALL();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700620 ALOGV("%s: ending configure (%d input stream, %zu output surfaces)",
621 __FUNCTION__, mInputStream.configured ? 1 : 0,
622 mStreamMap.size());
Igor Murashkine2d167e2014-08-19 16:19:59 -0700623
Zhijun He0effd522016-03-04 10:22:27 -0800624 binder::Status res;
625 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
626
Emilian Peevcc0b7952020-01-07 13:54:47 -0800627 if (offlineStreamIds == nullptr) {
Austin Borger0fb3ad92023-06-01 16:51:35 -0700628 std::string msg = "Invalid offline stream ids";
629 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
630 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
Emilian Peevcc0b7952020-01-07 13:54:47 -0800631 }
632
Zhijun He0effd522016-03-04 10:22:27 -0800633 Mutex::Autolock icl(mBinderSerializationLock);
634
635 if (!mDevice.get()) {
636 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
637 }
638
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700639 res = SessionConfigurationUtils::checkOperatingMode(operatingMode, mDevice->info(),
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000640 mCameraIdStr);
Emilian Peev35ae8262018-11-08 13:11:32 +0000641 if (!res.isOk()) {
642 return res;
643 }
644
645 status_t err = mDevice->configureStreams(sessionParams, operatingMode);
646 if (err == BAD_VALUE) {
Austin Borger0fb3ad92023-06-01 16:51:35 -0700647 std::string msg = fmt::sprintf("Camera %s: Unsupported set of inputs/outputs provided",
648 mCameraIdStr.c_str());
649 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
650 res = STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
Emilian Peev35ae8262018-11-08 13:11:32 +0000651 } else if (err != OK) {
Austin Borger0fb3ad92023-06-01 16:51:35 -0700652 std::string msg = fmt::sprintf("Camera %s: Error configuring streams: %s (%d)",
653 mCameraIdStr.c_str(), strerror(-err), err);
654 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
655 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.c_str());
Emilian Peev538c90e2018-12-17 18:03:19 +0000656 } else {
Emilian Peevcc0b7952020-01-07 13:54:47 -0800657 offlineStreamIds->clear();
658 mDevice->getOfflineStreamIds(offlineStreamIds);
659
Emilian Peev2f5d6012022-01-19 16:16:50 -0800660 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +0000661 for (size_t i = 0; i < mCompositeStreamMap.size(); ++i) {
662 err = mCompositeStreamMap.valueAt(i)->configureStream();
Emilian Peevcc0b7952020-01-07 13:54:47 -0800663 if (err != OK) {
Austin Borger0fb3ad92023-06-01 16:51:35 -0700664 std::string msg = fmt::sprintf("Camera %s: Error configuring composite "
665 "streams: %s (%d)", mCameraIdStr.c_str(), strerror(-err), err);
666 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
667 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.c_str());
Emilian Peev538c90e2018-12-17 18:03:19 +0000668 break;
669 }
Emilian Peevcc0b7952020-01-07 13:54:47 -0800670
671 // Composite streams can only support offline mode in case all individual internal
672 // streams are also supported.
673 std::vector<int> internalStreams;
674 mCompositeStreamMap.valueAt(i)->insertCompositeStreamIds(&internalStreams);
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800675 offlineStreamIds->erase(
676 std::remove_if(offlineStreamIds->begin(), offlineStreamIds->end(),
Emilian Peevcc0b7952020-01-07 13:54:47 -0800677 [&internalStreams] (int streamId) {
678 auto it = std::find(internalStreams.begin(), internalStreams.end(),
679 streamId);
680 if (it != internalStreams.end()) {
681 internalStreams.erase(it);
682 return true;
683 }
684
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800685 return false;}), offlineStreamIds->end());
Emilian Peevcc0b7952020-01-07 13:54:47 -0800686 if (internalStreams.empty()) {
687 offlineStreamIds->push_back(mCompositeStreamMap.valueAt(i)->getStreamId());
688 }
689 }
690
691 for (const auto& offlineStreamId : *offlineStreamIds) {
692 mStreamInfoMap[offlineStreamId].supportsOffline = true;
Emilian Peev538c90e2018-12-17 18:03:19 +0000693 }
Shuzhen Wang316781a2020-08-18 18:11:01 -0700694
695 nsecs_t configureEnd = systemTime();
696 int32_t configureDurationMs = ns2ms(configureEnd) - startTimeMs;
697 CameraServiceProxyWrapper::logStreamConfigured(mCameraIdStr, operatingMode,
698 false /*internalReconfig*/, configureDurationMs);
Emilian Peev35ae8262018-11-08 13:11:32 +0000699 }
700
701 return res;
702}
703
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800704binder::Status CameraDeviceClient::isSessionConfigurationSupported(
705 const SessionConfiguration& sessionConfiguration, bool *status /*out*/) {
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800706
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800707 ATRACE_CALL();
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800708 binder::Status res;
709 status_t ret = OK;
710 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
711
712 Mutex::Autolock icl(mBinderSerializationLock);
713
714 if (!mDevice.get()) {
715 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
716 }
717
718 auto operatingMode = sessionConfiguration.getOperatingMode();
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700719 res = SessionConfigurationUtils::checkOperatingMode(operatingMode, mDevice->info(),
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000720 mCameraIdStr);
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800721 if (!res.isOk()) {
722 return res;
723 }
724
725 if (status == nullptr) {
Austin Borger0fb3ad92023-06-01 16:51:35 -0700726 std::string msg = fmt::sprintf( "Camera %s: Invalid status!", mCameraIdStr.c_str());
727 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
728 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800729 }
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700730
Emilian Peev35ae8262018-11-08 13:11:32 +0000731 *status = false;
Austin Borger0fb3ad92023-06-01 16:51:35 -0700732 camera3::metadataGetter getMetadata = [this](const std::string &id,
733 bool /*overrideForPerfClass*/) {
Shuzhen Wanga79a64d2022-04-24 19:56:30 -0700734 return mDevice->infoPhysical(id);};
Austin Borger0fb3ad92023-06-01 16:51:35 -0700735 ret = mProviderManager->isSessionConfigurationSupported(mCameraIdStr.c_str(),
Shuzhen Wanga79a64d2022-04-24 19:56:30 -0700736 sessionConfiguration, mOverrideForPerfClass, getMetadata, status);
Emilian Peev35ae8262018-11-08 13:11:32 +0000737 switch (ret) {
738 case OK:
739 // Expected, do nothing.
740 break;
741 case INVALID_OPERATION: {
Austin Borger0fb3ad92023-06-01 16:51:35 -0700742 std::string msg = fmt::sprintf(
Emilian Peev35ae8262018-11-08 13:11:32 +0000743 "Camera %s: Session configuration query not supported!",
Austin Borger0fb3ad92023-06-01 16:51:35 -0700744 mCameraIdStr.c_str());
745 ALOGD("%s: %s", __FUNCTION__, msg.c_str());
746 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.c_str());
Emilian Peev35ae8262018-11-08 13:11:32 +0000747 }
748
749 break;
750 default: {
Austin Borger0fb3ad92023-06-01 16:51:35 -0700751 std::string msg = fmt::sprintf( "Camera %s: Error: %s (%d)", mCameraIdStr.c_str(),
Emilian Peev35ae8262018-11-08 13:11:32 +0000752 strerror(-ret), ret);
Austin Borger0fb3ad92023-06-01 16:51:35 -0700753 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
Emilian Peev35ae8262018-11-08 13:11:32 +0000754 res = STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
Austin Borger0fb3ad92023-06-01 16:51:35 -0700755 msg.c_str());
Emilian Peev35ae8262018-11-08 13:11:32 +0000756 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800757 }
758
759 return res;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700760}
761
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800762binder::Status CameraDeviceClient::deleteStream(int streamId) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700763 ATRACE_CALL();
764 ALOGV("%s (streamId = 0x%x)", __FUNCTION__, streamId);
765
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800766 binder::Status res;
767 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700768
769 Mutex::Autolock icl(mBinderSerializationLock);
770
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800771 if (!mDevice.get()) {
772 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
773 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700774
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700775 bool isInput = false;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700776 std::vector<sp<IBinder>> surfaces;
Zhijun He5d677d12016-05-29 16:52:39 -0700777 ssize_t dIndex = NAME_NOT_FOUND;
Emilian Peev538c90e2018-12-17 18:03:19 +0000778 ssize_t compositeIndex = NAME_NOT_FOUND;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700779
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700780 if (mInputStream.configured && mInputStream.id == streamId) {
781 isInput = true;
782 } else {
783 // Guard against trying to delete non-created streams
784 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700785 if (streamId == mStreamMap.valueAt(i).streamId()) {
786 surfaces.push_back(mStreamMap.keyAt(i));
787 }
788 }
789
790 // See if this stream is one of the deferred streams.
791 for (size_t i = 0; i < mDeferredStreams.size(); ++i) {
792 if (streamId == mDeferredStreams[i]) {
793 dIndex = i;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700794 break;
795 }
796 }
797
Emilian Peev2f5d6012022-01-19 16:16:50 -0800798 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +0000799 for (size_t i = 0; i < mCompositeStreamMap.size(); ++i) {
800 if (streamId == mCompositeStreamMap.valueAt(i)->getStreamId()) {
801 compositeIndex = i;
802 break;
803 }
804 }
805
Shuzhen Wang0129d522016-10-30 22:43:41 -0700806 if (surfaces.empty() && dIndex == NAME_NOT_FOUND) {
Austin Borger0fb3ad92023-06-01 16:51:35 -0700807 std::string msg = fmt::sprintf("Camera %s: Invalid stream ID (%d) specified, no such"
808 " stream created yet", mCameraIdStr.c_str(), streamId);
809 ALOGW("%s: %s", __FUNCTION__, msg.c_str());
810 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700811 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700812 }
813
814 // Also returns BAD_VALUE if stream ID was not valid
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800815 status_t err = mDevice->deleteStream(streamId);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700816
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800817 if (err != OK) {
Austin Borger0fb3ad92023-06-01 16:51:35 -0700818 std::string msg = fmt::sprintf("Camera %s: Unexpected error %s (%d) when deleting stream "
819 "%d", mCameraIdStr.c_str(), strerror(-err), err, streamId);
820 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
821 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.c_str());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800822 } else {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700823 if (isInput) {
824 mInputStream.configured = false;
Zhijun He5d677d12016-05-29 16:52:39 -0700825 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700826 for (auto& surface : surfaces) {
827 mStreamMap.removeItem(surface);
828 }
829
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800830 mConfiguredOutputs.removeItem(streamId);
831
Shuzhen Wang0129d522016-10-30 22:43:41 -0700832 if (dIndex != NAME_NOT_FOUND) {
833 mDeferredStreams.removeItemsAt(dIndex);
834 }
Emilian Peev538c90e2018-12-17 18:03:19 +0000835
836 if (compositeIndex != NAME_NOT_FOUND) {
Emilian Peev2f5d6012022-01-19 16:16:50 -0800837 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +0000838 status_t ret;
839 if ((ret = mCompositeStreamMap.valueAt(compositeIndex)->deleteStream())
840 != OK) {
Austin Borger0fb3ad92023-06-01 16:51:35 -0700841 std::string msg = fmt::sprintf("Camera %s: Unexpected error %s (%d) when "
842 "deleting composite stream %d", mCameraIdStr.c_str(), strerror(-err),
843 err, streamId);
844 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
845 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.c_str());
Emilian Peev538c90e2018-12-17 18:03:19 +0000846 }
847 mCompositeStreamMap.removeItemsAt(compositeIndex);
848 }
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800849 for (auto &mapIt: mHighResolutionCameraIdToStreamIdSet) {
850 auto &streamSet = mapIt.second;
851 if (streamSet.find(streamId) != streamSet.end()) {
852 streamSet.erase(streamId);
853 break;
854 }
855 }
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700856 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700857 }
858
859 return res;
860}
861
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800862binder::Status CameraDeviceClient::createStream(
863 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
864 /*out*/
865 int32_t* newStreamId) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700866 ATRACE_CALL();
Igor Murashkine7ee7632013-06-11 18:10:18 -0700867
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800868 binder::Status res;
869 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700870
871 Mutex::Autolock icl(mBinderSerializationLock);
872
Shuzhen Wang0129d522016-10-30 22:43:41 -0700873 const std::vector<sp<IGraphicBufferProducer>>& bufferProducers =
874 outputConfiguration.getGraphicBufferProducers();
875 size_t numBufferProducers = bufferProducers.size();
Shuzhen Wang758c2152017-01-10 18:26:18 -0800876 bool deferredConsumer = outputConfiguration.isDeferred();
877 bool isShared = outputConfiguration.isShared();
Austin Borger0fb3ad92023-06-01 16:51:35 -0700878 const std::string &physicalCameraId = outputConfiguration.getPhysicalCameraId();
Shuzhen Wang758c2152017-01-10 18:26:18 -0800879 bool deferredConsumerOnly = deferredConsumer && numBufferProducers == 0;
Shuzhen Wang83bff122020-11-20 15:51:39 -0800880 bool isMultiResolution = outputConfiguration.isMultiResolution();
Emilian Peevc81a7592022-02-14 17:38:18 -0800881 int64_t dynamicRangeProfile = outputConfiguration.getDynamicRangeProfile();
Shuzhen Wang8ed1e872022-03-08 16:34:33 -0800882 int64_t streamUseCase = outputConfiguration.getStreamUseCase();
Shuzhen Wange4208922022-02-01 16:52:48 -0800883 int timestampBase = outputConfiguration.getTimestampBase();
Shuzhen Wang610d7b82022-02-08 14:37:22 -0800884 int mirrorMode = outputConfiguration.getMirrorMode();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700885
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700886 res = SessionConfigurationUtils::checkSurfaceType(numBufferProducers, deferredConsumer,
Emilian Peev35ae8262018-11-08 13:11:32 +0000887 outputConfiguration.getSurfaceType());
888 if (!res.isOk()) {
889 return res;
Yin-Chia Yeh89f14da2014-06-10 16:05:44 -0700890 }
Zhijun He5d677d12016-05-29 16:52:39 -0700891
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800892 if (!mDevice.get()) {
893 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
894 }
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700895 res = SessionConfigurationUtils::checkPhysicalCameraId(mPhysicalCameraIds,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800896 physicalCameraId, mCameraIdStr);
Emilian Peev35ae8262018-11-08 13:11:32 +0000897 if (!res.isOk()) {
898 return res;
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800899 }
Emilian Peev35ae8262018-11-08 13:11:32 +0000900
Shuzhen Wang0129d522016-10-30 22:43:41 -0700901 std::vector<sp<Surface>> surfaces;
902 std::vector<sp<IBinder>> binders;
Zhijun He5d677d12016-05-29 16:52:39 -0700903 status_t err;
904
905 // Create stream for deferred surface case.
Shuzhen Wang0129d522016-10-30 22:43:41 -0700906 if (deferredConsumerOnly) {
Shuzhen Wang758c2152017-01-10 18:26:18 -0800907 return createDeferredSurfaceStreamLocked(outputConfiguration, isShared, newStreamId);
Zhijun He5d677d12016-05-29 16:52:39 -0700908 }
909
Shuzhen Wang758c2152017-01-10 18:26:18 -0800910 OutputStreamInfo streamInfo;
911 bool isStreamInfoValid = false;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800912 const std::vector<int32_t> &sensorPixelModesUsed =
913 outputConfiguration.getSensorPixelModesUsed();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700914 for (auto& bufferProducer : bufferProducers) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700915 // Don't create multiple streams for the same target surface
Shuzhen Wang0129d522016-10-30 22:43:41 -0700916 sp<IBinder> binder = IInterface::asBinder(bufferProducer);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800917 ssize_t index = mStreamMap.indexOfKey(binder);
918 if (index != NAME_NOT_FOUND) {
Austin Borger0fb3ad92023-06-01 16:51:35 -0700919 std::string msg = std::string("Camera ") + mCameraIdStr
920 + ": Surface already has a stream created for it (ID "
921 + std::to_string(index) + ")";
922 ALOGW("%s: %s", __FUNCTION__, msg.c_str());
923 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.c_str());
Shuzhen Wang0129d522016-10-30 22:43:41 -0700924 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700925
Shuzhen Wang758c2152017-01-10 18:26:18 -0800926 sp<Surface> surface;
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700927 res = SessionConfigurationUtils::createSurfaceFromGbp(streamInfo,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800928 isStreamInfoValid, surface, bufferProducer, mCameraIdStr,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -0800929 mDevice->infoPhysical(physicalCameraId), sensorPixelModesUsed, dynamicRangeProfile,
Shuzhen Wang610d7b82022-02-08 14:37:22 -0800930 streamUseCase, timestampBase, mirrorMode);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800931
932 if (!res.isOk())
933 return res;
934
935 if (!isStreamInfoValid) {
936 isStreamInfoValid = true;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700937 }
938
Shuzhen Wang758c2152017-01-10 18:26:18 -0800939 binders.push_back(IInterface::asBinder(bufferProducer));
Shuzhen Wang0129d522016-10-30 22:43:41 -0700940 surfaces.push_back(surface);
Ruben Brunkbba75572014-11-20 17:29:50 -0800941 }
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700942
943 // If mOverrideForPerfClass is true, do not fail createStream() for small
944 // JPEG sizes because existing createSurfaceFromGbp() logic will find the
945 // closest possible supported size.
946
Zhijun He125684a2015-12-26 15:07:30 -0800947 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
Emilian Peev40ead602017-09-26 15:46:36 +0100948 std::vector<int> surfaceIds;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800949 bool isDepthCompositeStream =
950 camera3::DepthCompositeStream::isDepthCompositeStream(surfaces[0]);
Chema Gonzalezdcc8a912022-12-21 08:59:30 -0800951 bool isHeicCompositeStream = camera3::HeicCompositeStream::isHeicCompositeStream(surfaces[0]);
952 if (isDepthCompositeStream || isHeicCompositeStream) {
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800953 sp<CompositeStream> compositeStream;
954 if (isDepthCompositeStream) {
955 compositeStream = new camera3::DepthCompositeStream(mDevice, getRemoteCallback());
956 } else {
957 compositeStream = new camera3::HeicCompositeStream(mDevice, getRemoteCallback());
958 }
959
Emilian Peev538c90e2018-12-17 18:03:19 +0000960 err = compositeStream->createStream(surfaces, deferredConsumer, streamInfo.width,
961 streamInfo.height, streamInfo.format,
Emilian Peevf4816702020-04-03 15:44:51 -0700962 static_cast<camera_stream_rotation_t>(outputConfiguration.getRotation()),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800963 &streamId, physicalCameraId, streamInfo.sensorPixelModesUsed, &surfaceIds,
964 outputConfiguration.getSurfaceSetID(), isShared, isMultiResolution);
Emilian Peev538c90e2018-12-17 18:03:19 +0000965 if (err == OK) {
Emilian Peev2f5d6012022-01-19 16:16:50 -0800966 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +0000967 mCompositeStreamMap.add(IInterface::asBinder(surfaces[0]->getIGraphicBufferProducer()),
968 compositeStream);
969 }
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800970 } else {
971 err = mDevice->createStream(surfaces, deferredConsumer, streamInfo.width,
972 streamInfo.height, streamInfo.format, streamInfo.dataSpace,
Emilian Peevf4816702020-04-03 15:44:51 -0700973 static_cast<camera_stream_rotation_t>(outputConfiguration.getRotation()),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800974 &streamId, physicalCameraId, streamInfo.sensorPixelModesUsed, &surfaceIds,
Emilian Peev2295df72021-11-12 18:14:10 -0800975 outputConfiguration.getSurfaceSetID(), isShared, isMultiResolution,
Shuzhen Wange4208922022-02-01 16:52:48 -0800976 /*consumerUsage*/0, streamInfo.dynamicRangeProfile, streamInfo.streamUseCase,
Shuzhen Wang610d7b82022-02-08 14:37:22 -0800977 streamInfo.timestampBase, streamInfo.mirrorMode);
Emilian Peev538c90e2018-12-17 18:03:19 +0000978 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700979
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800980 if (err != OK) {
981 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800982 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
Austin Borger0fb3ad92023-06-01 16:51:35 -0700983 mCameraIdStr.c_str(), streamInfo.width, streamInfo.height, streamInfo.format,
Shuzhen Wang758c2152017-01-10 18:26:18 -0800984 streamInfo.dataSpace, strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800985 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700986 int i = 0;
987 for (auto& binder : binders) {
988 ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %d",
989 __FUNCTION__, binder.get(), streamId, i);
Emilian Peev40ead602017-09-26 15:46:36 +0100990 mStreamMap.add(binder, StreamSurfaceId(streamId, surfaceIds[i]));
991 i++;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700992 }
Shuzhen Wang758c2152017-01-10 18:26:18 -0800993
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800994 mConfiguredOutputs.add(streamId, outputConfiguration);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800995 mStreamInfoMap[streamId] = streamInfo;
996
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800997 ALOGV("%s: Camera %s: Successfully created a new stream ID %d for output surface"
Shuzhen Wang0129d522016-10-30 22:43:41 -0700998 " (%d x %d) with format 0x%x.",
Austin Borger0fb3ad92023-06-01 16:51:35 -0700999 __FUNCTION__, mCameraIdStr.c_str(), streamId, streamInfo.width,
Shuzhen Wang758c2152017-01-10 18:26:18 -08001000 streamInfo.height, streamInfo.format);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001001
Zhijun He5d677d12016-05-29 16:52:39 -07001002 // Set transform flags to ensure preview to be rotated correctly.
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001003 res = setStreamTransformLocked(streamId, streamInfo.mirrorMode);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001004
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001005 // Fill in mHighResolutionCameraIdToStreamIdSet map
Austin Borger0fb3ad92023-06-01 16:51:35 -07001006 const std::string &cameraIdUsed =
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001007 physicalCameraId.size() != 0 ? physicalCameraId : mCameraIdStr;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001008 // Only needed for high resolution sensors
Austin Borger0fb3ad92023-06-01 16:51:35 -07001009 if (mHighResolutionSensors.find(cameraIdUsed) !=
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001010 mHighResolutionSensors.end()) {
Austin Borger0fb3ad92023-06-01 16:51:35 -07001011 mHighResolutionCameraIdToStreamIdSet[cameraIdUsed].insert(streamId);
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001012 }
1013
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001014 *newStreamId = streamId;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001015 }
1016
1017 return res;
1018}
1019
Zhijun He5d677d12016-05-29 16:52:39 -07001020binder::Status CameraDeviceClient::createDeferredSurfaceStreamLocked(
1021 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
Shuzhen Wang758c2152017-01-10 18:26:18 -08001022 bool isShared,
Zhijun He5d677d12016-05-29 16:52:39 -07001023 /*out*/
1024 int* newStreamId) {
1025 int width, height, format, surfaceType;
Emilian Peev050f5dc2017-05-18 14:43:56 +01001026 uint64_t consumerUsage;
Zhijun He5d677d12016-05-29 16:52:39 -07001027 android_dataspace dataSpace;
1028 status_t err;
1029 binder::Status res;
1030
1031 if (!mDevice.get()) {
1032 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1033 }
1034
1035 // Infer the surface info for deferred surface stream creation.
1036 width = outputConfiguration.getWidth();
1037 height = outputConfiguration.getHeight();
1038 surfaceType = outputConfiguration.getSurfaceType();
1039 format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
1040 dataSpace = android_dataspace_t::HAL_DATASPACE_UNKNOWN;
1041 // Hardcode consumer usage flags: SurfaceView--0x900, SurfaceTexture--0x100.
1042 consumerUsage = GraphicBuffer::USAGE_HW_TEXTURE;
1043 if (surfaceType == OutputConfiguration::SURFACE_TYPE_SURFACE_VIEW) {
1044 consumerUsage |= GraphicBuffer::USAGE_HW_COMPOSER;
1045 }
1046 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001047 std::vector<sp<Surface>> noSurface;
Emilian Peev40ead602017-09-26 15:46:36 +01001048 std::vector<int> surfaceIds;
Austin Borger0fb3ad92023-06-01 16:51:35 -07001049 const std::string &physicalCameraId = outputConfiguration.getPhysicalCameraId();
1050 const std::string &cameraIdUsed =
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001051 physicalCameraId.size() != 0 ? physicalCameraId : mCameraIdStr;
1052 // Here, we override sensor pixel modes
1053 std::unordered_set<int32_t> overriddenSensorPixelModesUsed;
1054 const std::vector<int32_t> &sensorPixelModesUsed =
1055 outputConfiguration.getSensorPixelModesUsed();
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07001056 if (SessionConfigurationUtils::checkAndOverrideSensorPixelModesUsed(
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001057 sensorPixelModesUsed, format, width, height, getStaticInfo(cameraIdUsed),
1058 /*allowRounding*/ false, &overriddenSensorPixelModesUsed) != OK) {
1059 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1060 "sensor pixel modes used not valid for deferred stream");
1061 }
1062
Shuzhen Wang0129d522016-10-30 22:43:41 -07001063 err = mDevice->createStream(noSurface, /*hasDeferredConsumer*/true, width,
1064 height, format, dataSpace,
Emilian Peevf4816702020-04-03 15:44:51 -07001065 static_cast<camera_stream_rotation_t>(outputConfiguration.getRotation()),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001066 &streamId, physicalCameraId,
1067 overriddenSensorPixelModesUsed,
1068 &surfaceIds,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001069 outputConfiguration.getSurfaceSetID(), isShared,
Emilian Peev2295df72021-11-12 18:14:10 -08001070 outputConfiguration.isMultiResolution(), consumerUsage,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001071 outputConfiguration.getDynamicRangeProfile(),
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001072 outputConfiguration.getStreamUseCase(),
1073 outputConfiguration.getMirrorMode());
Zhijun He5d677d12016-05-29 16:52:39 -07001074
1075 if (err != OK) {
1076 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001077 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
Austin Borger0fb3ad92023-06-01 16:51:35 -07001078 mCameraIdStr.c_str(), width, height, format, dataSpace, strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001079 } else {
1080 // Can not add streamId to mStreamMap here, as the surface is deferred. Add it to
1081 // a separate list to track. Once the deferred surface is set, this id will be
1082 // relocated to mStreamMap.
1083 mDeferredStreams.push_back(streamId);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001084 mStreamInfoMap.emplace(std::piecewise_construct, std::forward_as_tuple(streamId),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001085 std::forward_as_tuple(width, height, format, dataSpace, consumerUsage,
Emilian Peev2295df72021-11-12 18:14:10 -08001086 overriddenSensorPixelModesUsed,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001087 outputConfiguration.getDynamicRangeProfile(),
Shuzhen Wange4208922022-02-01 16:52:48 -08001088 outputConfiguration.getStreamUseCase(),
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001089 outputConfiguration.getTimestampBase(),
1090 outputConfiguration.getMirrorMode()));
Shuzhen Wang758c2152017-01-10 18:26:18 -08001091
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001092 ALOGV("%s: Camera %s: Successfully created a new stream ID %d for a deferred surface"
Zhijun He5d677d12016-05-29 16:52:39 -07001093 " (%d x %d) stream with format 0x%x.",
Austin Borger0fb3ad92023-06-01 16:51:35 -07001094 __FUNCTION__, mCameraIdStr.c_str(), streamId, width, height, format);
Zhijun He5d677d12016-05-29 16:52:39 -07001095
1096 // Set transform flags to ensure preview to be rotated correctly.
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001097 res = setStreamTransformLocked(streamId, outputConfiguration.getMirrorMode());
Zhijun He5d677d12016-05-29 16:52:39 -07001098
1099 *newStreamId = streamId;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001100 // Fill in mHighResolutionCameraIdToStreamIdSet
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001101 // Only needed for high resolution sensors
Austin Borger0fb3ad92023-06-01 16:51:35 -07001102 if (mHighResolutionSensors.find(cameraIdUsed) !=
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001103 mHighResolutionSensors.end()) {
Austin Borger0fb3ad92023-06-01 16:51:35 -07001104 mHighResolutionCameraIdToStreamIdSet[cameraIdUsed].insert(streamId);
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001105 }
Zhijun He5d677d12016-05-29 16:52:39 -07001106 }
1107 return res;
1108}
1109
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001110binder::Status CameraDeviceClient::setStreamTransformLocked(int streamId, int mirrorMode) {
Zhijun He5d677d12016-05-29 16:52:39 -07001111 int32_t transform = 0;
1112 status_t err;
1113 binder::Status res;
1114
1115 if (!mDevice.get()) {
1116 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1117 }
1118
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001119 err = getRotationTransformLocked(mirrorMode, &transform);
Zhijun He5d677d12016-05-29 16:52:39 -07001120 if (err != OK) {
1121 // Error logged by getRotationTransformLocked.
1122 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
1123 "Unable to calculate rotation transform for new stream");
1124 }
1125
1126 err = mDevice->setStreamTransform(streamId, transform);
1127 if (err != OK) {
Austin Borger0fb3ad92023-06-01 16:51:35 -07001128 std::string msg = fmt::sprintf("Failed to set stream transform (stream id %d)",
Zhijun He5d677d12016-05-29 16:52:39 -07001129 streamId);
Austin Borger0fb3ad92023-06-01 16:51:35 -07001130 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
1131 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.c_str());
Zhijun He5d677d12016-05-29 16:52:39 -07001132 }
1133
1134 return res;
1135}
Ruben Brunkbba75572014-11-20 17:29:50 -08001136
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001137binder::Status CameraDeviceClient::createInputStream(
Shuzhen Wang83bff122020-11-20 15:51:39 -08001138 int width, int height, int format, bool isMultiResolution,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001139 /*out*/
1140 int32_t* newStreamId) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001141
1142 ATRACE_CALL();
Shuzhen Wang83bff122020-11-20 15:51:39 -08001143 ALOGV("%s (w = %d, h = %d, f = 0x%x, isMultiResolution %d)", __FUNCTION__,
1144 width, height, format, isMultiResolution);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001145
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001146 binder::Status res;
1147 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001148
1149 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001150
1151 if (!mDevice.get()) {
1152 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1153 }
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001154
1155 if (mInputStream.configured) {
Austin Borger0fb3ad92023-06-01 16:51:35 -07001156 std::string msg = fmt::sprintf("Camera %s: Already has an input stream "
1157 "configured (ID %d)", mCameraIdStr.c_str(), mInputStream.id);
1158 ALOGE("%s: %s", __FUNCTION__, msg.c_str() );
1159 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.c_str());
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001160 }
1161
1162 int streamId = -1;
Shuzhen Wang83bff122020-11-20 15:51:39 -08001163 status_t err = mDevice->createInputStream(width, height, format, isMultiResolution, &streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001164 if (err == OK) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001165 mInputStream.configured = true;
1166 mInputStream.width = width;
1167 mInputStream.height = height;
1168 mInputStream.format = format;
1169 mInputStream.id = streamId;
1170
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001171 ALOGV("%s: Camera %s: Successfully created a new input stream ID %d",
Austin Borger0fb3ad92023-06-01 16:51:35 -07001172 __FUNCTION__, mCameraIdStr.c_str(), streamId);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001173
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001174 *newStreamId = streamId;
1175 } else {
1176 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Austin Borger0fb3ad92023-06-01 16:51:35 -07001177 "Camera %s: Error creating new input stream: %s (%d)", mCameraIdStr.c_str(),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001178 strerror(-err), err);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001179 }
1180
1181 return res;
1182}
1183
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001184binder::Status CameraDeviceClient::getInputSurface(/*out*/ view::Surface *inputSurface) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001185
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001186 binder::Status res;
1187 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1188
1189 if (inputSurface == NULL) {
1190 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Null input surface");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001191 }
1192
1193 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001194 if (!mDevice.get()) {
1195 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1196 }
1197 sp<IGraphicBufferProducer> producer;
1198 status_t err = mDevice->getInputBufferProducer(&producer);
1199 if (err != OK) {
1200 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001201 "Camera %s: Error getting input Surface: %s (%d)",
Austin Borger0fb3ad92023-06-01 16:51:35 -07001202 mCameraIdStr.c_str(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001203 } else {
Austin Borger0fb3ad92023-06-01 16:51:35 -07001204 inputSurface->name = toString16("CameraInput");
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001205 inputSurface->graphicBufferProducer = producer;
1206 }
1207 return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001208}
1209
Emilian Peev40ead602017-09-26 15:46:36 +01001210binder::Status CameraDeviceClient::updateOutputConfiguration(int streamId,
1211 const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
1212 ATRACE_CALL();
1213
1214 binder::Status res;
1215 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1216
1217 Mutex::Autolock icl(mBinderSerializationLock);
1218
1219 if (!mDevice.get()) {
1220 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1221 }
1222
1223 const std::vector<sp<IGraphicBufferProducer> >& bufferProducers =
1224 outputConfiguration.getGraphicBufferProducers();
Austin Borger0fb3ad92023-06-01 16:51:35 -07001225 const std::string &physicalCameraId = outputConfiguration.getPhysicalCameraId();
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -07001226
Emilian Peev40ead602017-09-26 15:46:36 +01001227 auto producerCount = bufferProducers.size();
1228 if (producerCount == 0) {
1229 ALOGE("%s: bufferProducers must not be empty", __FUNCTION__);
1230 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1231 "bufferProducers must not be empty");
1232 }
1233
1234 // The first output is the one associated with the output configuration.
1235 // It should always be present, valid and the corresponding stream id should match.
1236 sp<IBinder> binder = IInterface::asBinder(bufferProducers[0]);
1237 ssize_t index = mStreamMap.indexOfKey(binder);
1238 if (index == NAME_NOT_FOUND) {
1239 ALOGE("%s: Outputconfiguration is invalid", __FUNCTION__);
1240 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1241 "OutputConfiguration is invalid");
1242 }
1243 if (mStreamMap.valueFor(binder).streamId() != streamId) {
1244 ALOGE("%s: Stream Id: %d provided doesn't match the id: %d in the stream map",
1245 __FUNCTION__, streamId, mStreamMap.valueFor(binder).streamId());
1246 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1247 "Stream id is invalid");
1248 }
1249
1250 std::vector<size_t> removedSurfaceIds;
1251 std::vector<sp<IBinder>> removedOutputs;
1252 std::vector<sp<Surface>> newOutputs;
1253 std::vector<OutputStreamInfo> streamInfos;
1254 KeyedVector<sp<IBinder>, sp<IGraphicBufferProducer>> newOutputsMap;
1255 for (auto &it : bufferProducers) {
1256 newOutputsMap.add(IInterface::asBinder(it), it);
1257 }
1258
1259 for (size_t i = 0; i < mStreamMap.size(); i++) {
1260 ssize_t idx = newOutputsMap.indexOfKey(mStreamMap.keyAt(i));
1261 if (idx == NAME_NOT_FOUND) {
1262 if (mStreamMap[i].streamId() == streamId) {
1263 removedSurfaceIds.push_back(mStreamMap[i].surfaceId());
1264 removedOutputs.push_back(mStreamMap.keyAt(i));
1265 }
1266 } else {
1267 if (mStreamMap[i].streamId() != streamId) {
1268 ALOGE("%s: Output surface already part of a different stream", __FUNCTION__);
1269 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1270 "Target Surface is invalid");
1271 }
1272 newOutputsMap.removeItemsAt(idx);
1273 }
1274 }
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001275 const std::vector<int32_t> &sensorPixelModesUsed =
1276 outputConfiguration.getSensorPixelModesUsed();
Shuzhen Wang8ed1e872022-03-08 16:34:33 -08001277 int64_t streamUseCase = outputConfiguration.getStreamUseCase();
Shuzhen Wange4208922022-02-01 16:52:48 -08001278 int timestampBase = outputConfiguration.getTimestampBase();
Emilian Peevc81a7592022-02-14 17:38:18 -08001279 int64_t dynamicRangeProfile = outputConfiguration.getDynamicRangeProfile();
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001280 int mirrorMode = outputConfiguration.getMirrorMode();
Emilian Peev2295df72021-11-12 18:14:10 -08001281
Emilian Peev40ead602017-09-26 15:46:36 +01001282 for (size_t i = 0; i < newOutputsMap.size(); i++) {
1283 OutputStreamInfo outInfo;
1284 sp<Surface> surface;
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07001285 res = SessionConfigurationUtils::createSurfaceFromGbp(outInfo,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001286 /*isStreamInfoValid*/ false, surface, newOutputsMap.valueAt(i), mCameraIdStr,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001287 mDevice->infoPhysical(physicalCameraId), sensorPixelModesUsed, dynamicRangeProfile,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001288 streamUseCase, timestampBase, mirrorMode);
Emilian Peev40ead602017-09-26 15:46:36 +01001289 if (!res.isOk())
1290 return res;
1291
Emilian Peev40ead602017-09-26 15:46:36 +01001292 streamInfos.push_back(outInfo);
1293 newOutputs.push_back(surface);
1294 }
1295
1296 //Trivial case no changes required
1297 if (removedSurfaceIds.empty() && newOutputs.empty()) {
1298 return binder::Status::ok();
1299 }
1300
1301 KeyedVector<sp<Surface>, size_t> outputMap;
1302 auto ret = mDevice->updateStream(streamId, newOutputs, streamInfos, removedSurfaceIds,
1303 &outputMap);
1304 if (ret != OK) {
1305 switch (ret) {
1306 case NAME_NOT_FOUND:
1307 case BAD_VALUE:
1308 case -EBUSY:
1309 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1310 "Camera %s: Error updating stream: %s (%d)",
Austin Borger0fb3ad92023-06-01 16:51:35 -07001311 mCameraIdStr.c_str(), strerror(ret), ret);
Emilian Peev40ead602017-09-26 15:46:36 +01001312 break;
1313 default:
1314 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1315 "Camera %s: Error updating stream: %s (%d)",
Austin Borger0fb3ad92023-06-01 16:51:35 -07001316 mCameraIdStr.c_str(), strerror(ret), ret);
Emilian Peev40ead602017-09-26 15:46:36 +01001317 break;
1318 }
1319 } else {
1320 for (const auto &it : removedOutputs) {
1321 mStreamMap.removeItem(it);
1322 }
1323
1324 for (size_t i = 0; i < outputMap.size(); i++) {
1325 mStreamMap.add(IInterface::asBinder(outputMap.keyAt(i)->getIGraphicBufferProducer()),
1326 StreamSurfaceId(streamId, outputMap.valueAt(i)));
1327 }
1328
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -08001329 mConfiguredOutputs.replaceValueFor(streamId, outputConfiguration);
1330
Emilian Peev40ead602017-09-26 15:46:36 +01001331 ALOGV("%s: Camera %s: Successful stream ID %d update",
Austin Borger0fb3ad92023-06-01 16:51:35 -07001332 __FUNCTION__, mCameraIdStr.c_str(), streamId);
Emilian Peev40ead602017-09-26 15:46:36 +01001333 }
1334
1335 return res;
1336}
1337
Igor Murashkine7ee7632013-06-11 18:10:18 -07001338// Create a request object from a template.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001339binder::Status CameraDeviceClient::createDefaultRequest(int templateId,
1340 /*out*/
1341 hardware::camera2::impl::CameraMetadataNative* request)
Igor Murashkine7ee7632013-06-11 18:10:18 -07001342{
1343 ATRACE_CALL();
1344 ALOGV("%s (templateId = 0x%x)", __FUNCTION__, templateId);
1345
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001346 binder::Status res;
1347 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001348
1349 Mutex::Autolock icl(mBinderSerializationLock);
1350
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001351 if (!mDevice.get()) {
1352 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1353 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001354
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001355 status_t err;
Emilian Peevf4816702020-04-03 15:44:51 -07001356 camera_request_template_t tempId = camera_request_template_t::CAMERA_TEMPLATE_COUNT;
1357 if (!(res = mapRequestTemplate(templateId, &tempId)).isOk()) return res;
1358
1359 CameraMetadata metadata;
1360 if ( (err = mDevice->createDefaultRequest(tempId, &metadata) ) == OK &&
Igor Murashkine7ee7632013-06-11 18:10:18 -07001361 request != NULL) {
1362
1363 request->swap(metadata);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001364 } else if (err == BAD_VALUE) {
1365 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001366 "Camera %s: Template ID %d is invalid or not supported: %s (%d)",
Austin Borger0fb3ad92023-06-01 16:51:35 -07001367 mCameraIdStr.c_str(), templateId, strerror(-err), err);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001368
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001369 } else {
1370 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001371 "Camera %s: Error creating default request for template %d: %s (%d)",
Austin Borger0fb3ad92023-06-01 16:51:35 -07001372 mCameraIdStr.c_str(), templateId, strerror(-err), err);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001373 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001374 return res;
1375}
1376
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001377binder::Status CameraDeviceClient::getCameraInfo(
1378 /*out*/
1379 hardware::camera2::impl::CameraMetadataNative* info)
Igor Murashkine7ee7632013-06-11 18:10:18 -07001380{
1381 ATRACE_CALL();
1382 ALOGV("%s", __FUNCTION__);
1383
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001384 binder::Status res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001385
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001386 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001387
1388 Mutex::Autolock icl(mBinderSerializationLock);
1389
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001390 if (!mDevice.get()) {
1391 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1392 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001393
Igor Murashkin099b4572013-07-12 17:52:16 -07001394 if (info != NULL) {
1395 *info = mDevice->info(); // static camera metadata
1396 // TODO: merge with device-specific camera metadata
1397 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001398
1399 return res;
1400}
1401
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001402binder::Status CameraDeviceClient::waitUntilIdle()
Zhijun He2ab500c2013-07-23 08:02:53 -07001403{
1404 ATRACE_CALL();
1405 ALOGV("%s", __FUNCTION__);
1406
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001407 binder::Status res;
1408 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Zhijun He2ab500c2013-07-23 08:02:53 -07001409
1410 Mutex::Autolock icl(mBinderSerializationLock);
1411
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001412 if (!mDevice.get()) {
1413 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1414 }
Zhijun He2ab500c2013-07-23 08:02:53 -07001415
1416 // FIXME: Also need check repeating burst.
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001417 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001418 if (mStreamingRequestId != REQUEST_ID_NONE) {
Austin Borger0fb3ad92023-06-01 16:51:35 -07001419 std::string msg = fmt::sprintf(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001420 "Camera %s: Try to waitUntilIdle when there are active streaming requests",
Austin Borger0fb3ad92023-06-01 16:51:35 -07001421 mCameraIdStr.c_str());
1422 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
1423 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.c_str());
Zhijun He2ab500c2013-07-23 08:02:53 -07001424 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001425 status_t err = mDevice->waitUntilDrained();
1426 if (err != OK) {
1427 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001428 "Camera %s: Error waiting to drain: %s (%d)",
Austin Borger0fb3ad92023-06-01 16:51:35 -07001429 mCameraIdStr.c_str(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001430 }
Zhijun He2ab500c2013-07-23 08:02:53 -07001431 ALOGV("%s Done", __FUNCTION__);
Zhijun He2ab500c2013-07-23 08:02:53 -07001432 return res;
1433}
1434
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001435binder::Status CameraDeviceClient::flush(
1436 /*out*/
1437 int64_t* lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001438 ATRACE_CALL();
1439 ALOGV("%s", __FUNCTION__);
1440
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001441 binder::Status res;
1442 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001443
1444 Mutex::Autolock icl(mBinderSerializationLock);
1445
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001446 if (!mDevice.get()) {
1447 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1448 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001449
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001450 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001451 mStreamingRequestId = REQUEST_ID_NONE;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001452 status_t err = mDevice->flush(lastFrameNumber);
1453 if (err != OK) {
1454 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Austin Borger0fb3ad92023-06-01 16:51:35 -07001455 "Camera %s: Error flushing device: %s (%d)", mCameraIdStr.c_str(), strerror(-err),
1456 err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001457 }
1458 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001459}
1460
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001461binder::Status CameraDeviceClient::prepare(int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001462 ATRACE_CALL();
1463 ALOGV("%s", __FUNCTION__);
1464
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001465 binder::Status res;
1466 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001467
1468 Mutex::Autolock icl(mBinderSerializationLock);
1469
1470 // Guard against trying to prepare non-created streams
1471 ssize_t index = NAME_NOT_FOUND;
1472 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001473 if (streamId == mStreamMap.valueAt(i).streamId()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001474 index = i;
1475 break;
1476 }
1477 }
1478
1479 if (index == NAME_NOT_FOUND) {
Austin Borger0fb3ad92023-06-01 16:51:35 -07001480 std::string msg = fmt::sprintf("Camera %s: Invalid stream ID (%d) specified, no stream "
1481 "with that ID exists", mCameraIdStr.c_str(), streamId);
1482 ALOGW("%s: %s", __FUNCTION__, msg.c_str());
1483 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001484 }
1485
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001486 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1487 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001488 status_t err = mDevice->prepare(streamId);
1489 if (err == BAD_VALUE) {
1490 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001491 "Camera %s: Stream %d has already been used, and cannot be prepared",
Austin Borger0fb3ad92023-06-01 16:51:35 -07001492 mCameraIdStr.c_str(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001493 } else if (err != OK) {
1494 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Austin Borger0fb3ad92023-06-01 16:51:35 -07001495 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.c_str(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001496 strerror(-err), err);
1497 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001498 return res;
1499}
1500
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001501binder::Status CameraDeviceClient::prepare2(int maxCount, int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001502 ATRACE_CALL();
1503 ALOGV("%s", __FUNCTION__);
1504
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001505 binder::Status res;
1506 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Ruben Brunkc78ac262015-08-13 17:58:46 -07001507
1508 Mutex::Autolock icl(mBinderSerializationLock);
1509
1510 // Guard against trying to prepare non-created streams
1511 ssize_t index = NAME_NOT_FOUND;
1512 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001513 if (streamId == mStreamMap.valueAt(i).streamId()) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001514 index = i;
1515 break;
1516 }
1517 }
1518
1519 if (index == NAME_NOT_FOUND) {
Austin Borger0fb3ad92023-06-01 16:51:35 -07001520 std::string msg = fmt::sprintf("Camera %s: Invalid stream ID (%d) specified, no stream "
1521 "with that ID exists", mCameraIdStr.c_str(), streamId);
1522 ALOGW("%s: %s", __FUNCTION__, msg.c_str());
1523 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
Ruben Brunkc78ac262015-08-13 17:58:46 -07001524 }
1525
1526 if (maxCount <= 0) {
Austin Borger0fb3ad92023-06-01 16:51:35 -07001527 std::string msg = fmt::sprintf("Camera %s: maxCount (%d) must be greater than 0",
1528 mCameraIdStr.c_str(), maxCount);
1529 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
1530 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
Ruben Brunkc78ac262015-08-13 17:58:46 -07001531 }
1532
1533 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1534 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001535 status_t err = mDevice->prepare(maxCount, streamId);
1536 if (err == BAD_VALUE) {
1537 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001538 "Camera %s: Stream %d has already been used, and cannot be prepared",
Austin Borger0fb3ad92023-06-01 16:51:35 -07001539 mCameraIdStr.c_str(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001540 } else if (err != OK) {
1541 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Austin Borger0fb3ad92023-06-01 16:51:35 -07001542 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.c_str(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001543 strerror(-err), err);
1544 }
Ruben Brunkc78ac262015-08-13 17:58:46 -07001545
1546 return res;
1547}
1548
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001549binder::Status CameraDeviceClient::tearDown(int streamId) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001550 ATRACE_CALL();
1551 ALOGV("%s", __FUNCTION__);
1552
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001553 binder::Status res;
1554 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001555
1556 Mutex::Autolock icl(mBinderSerializationLock);
1557
1558 // Guard against trying to prepare non-created streams
1559 ssize_t index = NAME_NOT_FOUND;
1560 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001561 if (streamId == mStreamMap.valueAt(i).streamId()) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001562 index = i;
1563 break;
1564 }
1565 }
1566
1567 if (index == NAME_NOT_FOUND) {
Austin Borger0fb3ad92023-06-01 16:51:35 -07001568 std::string msg = fmt::sprintf("Camera %s: Invalid stream ID (%d) specified, no stream "
1569 "with that ID exists", mCameraIdStr.c_str(), streamId);
1570 ALOGW("%s: %s", __FUNCTION__, msg.c_str());
1571 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001572 }
1573
1574 // Also returns BAD_VALUE if stream ID was not valid or if the stream is in
1575 // use
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001576 status_t err = mDevice->tearDown(streamId);
1577 if (err == BAD_VALUE) {
1578 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001579 "Camera %s: Stream %d is still in use, cannot be torn down",
Austin Borger0fb3ad92023-06-01 16:51:35 -07001580 mCameraIdStr.c_str(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001581 } else if (err != OK) {
1582 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Austin Borger0fb3ad92023-06-01 16:51:35 -07001583 "Camera %s: Error tearing down stream %d: %s (%d)", mCameraIdStr.c_str(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001584 strerror(-err), err);
1585 }
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001586
1587 return res;
1588}
1589
Shuzhen Wang758c2152017-01-10 18:26:18 -08001590binder::Status CameraDeviceClient::finalizeOutputConfigurations(int32_t streamId,
Zhijun He5d677d12016-05-29 16:52:39 -07001591 const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
1592 ATRACE_CALL();
1593
1594 binder::Status res;
1595 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1596
1597 Mutex::Autolock icl(mBinderSerializationLock);
1598
Shuzhen Wang0129d522016-10-30 22:43:41 -07001599 const std::vector<sp<IGraphicBufferProducer> >& bufferProducers =
1600 outputConfiguration.getGraphicBufferProducers();
Austin Borger0fb3ad92023-06-01 16:51:35 -07001601 const std::string &physicalId = outputConfiguration.getPhysicalCameraId();
Zhijun He5d677d12016-05-29 16:52:39 -07001602
Shuzhen Wang0129d522016-10-30 22:43:41 -07001603 if (bufferProducers.size() == 0) {
1604 ALOGE("%s: bufferProducers must not be empty", __FUNCTION__);
Zhijun He5d677d12016-05-29 16:52:39 -07001605 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Target Surface is invalid");
1606 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001607
Shuzhen Wang758c2152017-01-10 18:26:18 -08001608 // streamId should be in mStreamMap if this stream already has a surface attached
1609 // to it. Otherwise, it should be in mDeferredStreams.
1610 bool streamIdConfigured = false;
1611 ssize_t deferredStreamIndex = NAME_NOT_FOUND;
1612 for (size_t i = 0; i < mStreamMap.size(); i++) {
1613 if (mStreamMap.valueAt(i).streamId() == streamId) {
1614 streamIdConfigured = true;
1615 break;
1616 }
Zhijun He5d677d12016-05-29 16:52:39 -07001617 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001618 for (size_t i = 0; i < mDeferredStreams.size(); i++) {
1619 if (streamId == mDeferredStreams[i]) {
1620 deferredStreamIndex = i;
1621 break;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001622 }
1623
Shuzhen Wang758c2152017-01-10 18:26:18 -08001624 }
1625 if (deferredStreamIndex == NAME_NOT_FOUND && !streamIdConfigured) {
Austin Borger0fb3ad92023-06-01 16:51:35 -07001626 std::string msg = fmt::sprintf("Camera %s: deferred surface is set to a unknown stream"
1627 "(ID %d)", mCameraIdStr.c_str(), streamId);
1628 ALOGW("%s: %s", __FUNCTION__, msg.c_str());
1629 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
Zhijun He5d677d12016-05-29 16:52:39 -07001630 }
1631
Shuzhen Wang88fd0052017-02-09 16:40:07 -08001632 if (mStreamInfoMap[streamId].finalized) {
Austin Borger0fb3ad92023-06-01 16:51:35 -07001633 std::string msg = fmt::sprintf("Camera %s: finalizeOutputConfigurations has been called"
1634 " on stream ID %d", mCameraIdStr.c_str(), streamId);
1635 ALOGW("%s: %s", __FUNCTION__, msg.c_str());
1636 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
Shuzhen Wang88fd0052017-02-09 16:40:07 -08001637 }
1638
Zhijun He5d677d12016-05-29 16:52:39 -07001639 if (!mDevice.get()) {
1640 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1641 }
1642
Shuzhen Wang758c2152017-01-10 18:26:18 -08001643 std::vector<sp<Surface>> consumerSurfaces;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001644 const std::vector<int32_t> &sensorPixelModesUsed =
1645 outputConfiguration.getSensorPixelModesUsed();
Emilian Peevc81a7592022-02-14 17:38:18 -08001646 int64_t dynamicRangeProfile = outputConfiguration.getDynamicRangeProfile();
Shuzhen Wang8ed1e872022-03-08 16:34:33 -08001647 int64_t streamUseCase= outputConfiguration.getStreamUseCase();
Shuzhen Wange4208922022-02-01 16:52:48 -08001648 int timestampBase = outputConfiguration.getTimestampBase();
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001649 int mirrorMode = outputConfiguration.getMirrorMode();
Shuzhen Wang758c2152017-01-10 18:26:18 -08001650 for (auto& bufferProducer : bufferProducers) {
1651 // Don't create multiple streams for the same target surface
Zhijun He5d677d12016-05-29 16:52:39 -07001652 ssize_t index = mStreamMap.indexOfKey(IInterface::asBinder(bufferProducer));
1653 if (index != NAME_NOT_FOUND) {
Shuzhen Wang758c2152017-01-10 18:26:18 -08001654 ALOGV("Camera %s: Surface already has a stream created "
Austin Borger0fb3ad92023-06-01 16:51:35 -07001655 " for it (ID %zd)", mCameraIdStr.c_str(), index);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001656 continue;
Zhijun He5d677d12016-05-29 16:52:39 -07001657 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001658
1659 sp<Surface> surface;
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07001660 res = SessionConfigurationUtils::createSurfaceFromGbp(mStreamInfoMap[streamId],
Colin Crossb8a9dbb2020-08-27 04:12:26 +00001661 true /*isStreamInfoValid*/, surface, bufferProducer, mCameraIdStr,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001662 mDevice->infoPhysical(physicalId), sensorPixelModesUsed, dynamicRangeProfile,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001663 streamUseCase, timestampBase, mirrorMode);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001664
1665 if (!res.isOk())
1666 return res;
1667
1668 consumerSurfaces.push_back(surface);
Zhijun He5d677d12016-05-29 16:52:39 -07001669 }
1670
Shuzhen Wanga81ce342017-04-13 15:18:36 -07001671 // Gracefully handle case where finalizeOutputConfigurations is called
1672 // without any new surface.
1673 if (consumerSurfaces.size() == 0) {
1674 mStreamInfoMap[streamId].finalized = true;
1675 return res;
1676 }
1677
Zhijun He5d677d12016-05-29 16:52:39 -07001678 // Finish the deferred stream configuration with the surface.
Shuzhen Wang758c2152017-01-10 18:26:18 -08001679 status_t err;
Emilian Peev40ead602017-09-26 15:46:36 +01001680 std::vector<int> consumerSurfaceIds;
1681 err = mDevice->setConsumerSurfaces(streamId, consumerSurfaces, &consumerSurfaceIds);
Zhijun He5d677d12016-05-29 16:52:39 -07001682 if (err == OK) {
Shuzhen Wang758c2152017-01-10 18:26:18 -08001683 for (size_t i = 0; i < consumerSurfaces.size(); i++) {
1684 sp<IBinder> binder = IInterface::asBinder(
1685 consumerSurfaces[i]->getIGraphicBufferProducer());
Emilian Peev40ead602017-09-26 15:46:36 +01001686 ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %d", __FUNCTION__,
Shuzhen Wang758c2152017-01-10 18:26:18 -08001687 binder.get(), streamId, consumerSurfaceIds[i]);
1688 mStreamMap.add(binder, StreamSurfaceId(streamId, consumerSurfaceIds[i]));
1689 }
1690 if (deferredStreamIndex != NAME_NOT_FOUND) {
1691 mDeferredStreams.removeItemsAt(deferredStreamIndex);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001692 }
Shuzhen Wang88fd0052017-02-09 16:40:07 -08001693 mStreamInfoMap[streamId].finalized = true;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -08001694 mConfiguredOutputs.replaceValueFor(streamId, outputConfiguration);
Zhijun He5d677d12016-05-29 16:52:39 -07001695 } else if (err == NO_INIT) {
1696 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001697 "Camera %s: Deferred surface is invalid: %s (%d)",
Austin Borger0fb3ad92023-06-01 16:51:35 -07001698 mCameraIdStr.c_str(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001699 } else {
1700 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001701 "Camera %s: Error setting output stream deferred surface: %s (%d)",
Austin Borger0fb3ad92023-06-01 16:51:35 -07001702 mCameraIdStr.c_str(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001703 }
1704
1705 return res;
1706}
1707
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001708binder::Status CameraDeviceClient::setCameraAudioRestriction(int32_t mode) {
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001709 ATRACE_CALL();
1710 binder::Status res;
1711 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1712
1713 if (!isValidAudioRestriction(mode)) {
Austin Borger0fb3ad92023-06-01 16:51:35 -07001714 std::string msg = fmt::sprintf("Camera %s: invalid audio restriction mode %d",
1715 mCameraIdStr.c_str(), mode);
1716 ALOGW("%s: %s", __FUNCTION__, msg.c_str());
1717 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001718 }
1719
1720 Mutex::Autolock icl(mBinderSerializationLock);
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001721 BasicClient::setAudioRestriction(mode);
1722 return binder::Status::ok();
1723}
1724
1725binder::Status CameraDeviceClient::getGlobalAudioRestriction(/*out*/ int32_t* outMode) {
1726 ATRACE_CALL();
1727 binder::Status res;
1728 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1729 Mutex::Autolock icl(mBinderSerializationLock);
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001730 if (outMode != nullptr) {
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001731 *outMode = BasicClient::getServiceAudioRestriction();
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001732 }
1733 return binder::Status::ok();
1734}
1735
Ravneet74cd3732022-03-30 05:33:03 +00001736status_t CameraDeviceClient::setCameraServiceWatchdog(bool enabled) {
1737 return mDevice->setCameraServiceWatchdog(enabled);
1738}
1739
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08001740status_t CameraDeviceClient::setRotateAndCropOverride(uint8_t rotateAndCrop) {
1741 if (rotateAndCrop > ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return BAD_VALUE;
1742
1743 return mDevice->setRotateAndCropAutoBehavior(
1744 static_cast<camera_metadata_enum_android_scaler_rotate_and_crop_t>(rotateAndCrop));
1745}
1746
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08001747bool CameraDeviceClient::supportsCameraMute() {
1748 return mDevice->supportsCameraMute();
1749}
1750
1751status_t CameraDeviceClient::setCameraMute(bool enabled) {
1752 return mDevice->setCameraMute(enabled);
1753}
1754
Shuzhen Wangabe5ea12022-12-15 22:38:07 -08001755void CameraDeviceClient::setStreamUseCaseOverrides(
1756 const std::vector<int64_t>& useCaseOverrides) {
1757 mDevice->setStreamUseCaseOverrides(useCaseOverrides);
1758}
1759
1760void CameraDeviceClient::clearStreamUseCaseOverrides() {
1761 mDevice->clearStreamUseCaseOverrides();
1762}
1763
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001764binder::Status CameraDeviceClient::switchToOffline(
1765 const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb,
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001766 const std::vector<int>& offlineOutputIds,
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001767 /*out*/
1768 sp<hardware::camera2::ICameraOfflineSession>* session) {
1769 ATRACE_CALL();
1770
1771 binder::Status res;
1772 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1773
1774 Mutex::Autolock icl(mBinderSerializationLock);
1775
1776 if (!mDevice.get()) {
1777 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1778 }
1779
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001780 if (offlineOutputIds.empty()) {
Austin Borger0fb3ad92023-06-01 16:51:35 -07001781 std::string msg = "Offline surfaces must not be empty";
1782 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
1783 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001784 }
1785
1786 if (session == nullptr) {
Austin Borger0fb3ad92023-06-01 16:51:35 -07001787 std::string msg = "Invalid offline session";
1788 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
1789 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001790 }
1791
Yin-Chia Yeh87fccca2020-01-28 09:37:18 -08001792 std::vector<int32_t> offlineStreamIds;
1793 offlineStreamIds.reserve(offlineOutputIds.size());
Emilian Peev4697b642019-11-19 17:11:14 -08001794 KeyedVector<sp<IBinder>, sp<CompositeStream>> offlineCompositeStreamMap;
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001795 for (const auto& streamId : offlineOutputIds) {
1796 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001797 if (index == NAME_NOT_FOUND) {
Austin Borger0fb3ad92023-06-01 16:51:35 -07001798 std::string msg = fmt::sprintf("Offline surface with id: %d is not registered",
Emilian Peevcc0b7952020-01-07 13:54:47 -08001799 streamId);
Austin Borger0fb3ad92023-06-01 16:51:35 -07001800 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
1801 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001802 }
Emilian Peevcc0b7952020-01-07 13:54:47 -08001803
1804 if (!mStreamInfoMap[streamId].supportsOffline) {
Austin Borger0fb3ad92023-06-01 16:51:35 -07001805 std::string msg = fmt::sprintf("Offline surface with id: %d doesn't support "
Emilian Peevcc0b7952020-01-07 13:54:47 -08001806 "offline mode", streamId);
Austin Borger0fb3ad92023-06-01 16:51:35 -07001807 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
1808 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
Emilian Peevcc0b7952020-01-07 13:54:47 -08001809 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001810
Emilian Peev2f5d6012022-01-19 16:16:50 -08001811 Mutex::Autolock l(mCompositeLock);
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001812 bool isCompositeStream = false;
Emilian Peev2f5d6012022-01-19 16:16:50 -08001813 for (const auto& gbp : mConfiguredOutputs.valueAt(index).getGraphicBufferProducers()) {
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001814 sp<Surface> s = new Surface(gbp, false /*controlledByApp*/);
Pirama Arumuga Nainar8db21062022-01-28 10:15:12 -08001815 isCompositeStream = camera3::DepthCompositeStream::isDepthCompositeStream(s) ||
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001816 camera3::HeicCompositeStream::isHeicCompositeStream(s);
Emilian Peev4697b642019-11-19 17:11:14 -08001817 if (isCompositeStream) {
1818 auto compositeIdx = mCompositeStreamMap.indexOfKey(IInterface::asBinder(gbp));
1819 if (compositeIdx == NAME_NOT_FOUND) {
1820 ALOGE("%s: Unknown composite stream", __FUNCTION__);
1821 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1822 "Unknown composite stream");
1823 }
1824
1825 mCompositeStreamMap.valueAt(compositeIdx)->insertCompositeStreamIds(
1826 &offlineStreamIds);
1827 offlineCompositeStreamMap.add(mCompositeStreamMap.keyAt(compositeIdx),
1828 mCompositeStreamMap.valueAt(compositeIdx));
1829 break;
1830 }
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001831 }
1832
Emilian Peev4697b642019-11-19 17:11:14 -08001833 if (!isCompositeStream) {
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001834 offlineStreamIds.push_back(streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001835 }
1836 }
1837
1838 sp<CameraOfflineSessionBase> offlineSession;
1839 auto ret = mDevice->switchToOffline(offlineStreamIds, &offlineSession);
1840 if (ret != OK) {
1841 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1842 "Camera %s: Error switching to offline mode: %s (%d)",
Austin Borger0fb3ad92023-06-01 16:51:35 -07001843 mCameraIdStr.c_str(), strerror(ret), ret);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001844 }
1845
Emilian Peevcc0b7952020-01-07 13:54:47 -08001846 sp<CameraOfflineSessionClient> offlineClient;
1847 if (offlineSession.get() != nullptr) {
1848 offlineClient = new CameraOfflineSessionClient(sCameraService,
1849 offlineSession, offlineCompositeStreamMap, cameraCb, mClientPackageName,
Emilian Peev8b64f282021-03-25 16:49:57 -07001850 mClientFeatureId, mCameraIdStr, mCameraFacing, mOrientation, mClientPid, mClientUid,
1851 mServicePid);
Emilian Peevcc0b7952020-01-07 13:54:47 -08001852 ret = sCameraService->addOfflineClient(mCameraIdStr, offlineClient);
1853 }
1854
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001855 if (ret == OK) {
Emilian Peevd99c8ae2019-11-26 13:19:13 -08001856 // A successful offline session switch must reset the current camera client
1857 // and release any resources occupied by previously configured streams.
1858 mStreamMap.clear();
1859 mConfiguredOutputs.clear();
1860 mDeferredStreams.clear();
1861 mStreamInfoMap.clear();
Emilian Peev2f5d6012022-01-19 16:16:50 -08001862 Mutex::Autolock l(mCompositeLock);
Emilian Peevd99c8ae2019-11-26 13:19:13 -08001863 mCompositeStreamMap.clear();
1864 mInputStream = {false, 0, 0, 0, 0};
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001865 } else {
Emilian Peev886ac212023-02-07 14:10:24 -08001866 // In case we failed to register the offline client, ensure that it still initialized
1867 // so that all failing requests can return back correctly once the object is released.
Austin Borger0fb3ad92023-06-01 16:51:35 -07001868 offlineClient->initialize(nullptr /*cameraProviderManager*/, std::string()/*monitorTags*/);
Emilian Peev886ac212023-02-07 14:10:24 -08001869
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001870 switch(ret) {
1871 case BAD_VALUE:
1872 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1873 "Illegal argument to HAL module for camera \"%s\"", mCameraIdStr.c_str());
1874 case TIMED_OUT:
1875 return STATUS_ERROR_FMT(CameraService::ERROR_CAMERA_IN_USE,
1876 "Camera \"%s\" is already open", mCameraIdStr.c_str());
1877 default:
1878 return STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1879 "Failed to initialize camera \"%s\": %s (%d)", mCameraIdStr.c_str(),
1880 strerror(-ret), ret);
1881 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001882 }
1883
1884 *session = offlineClient;
1885
1886 return binder::Status::ok();
1887}
1888
Igor Murashkine7ee7632013-06-11 18:10:18 -07001889status_t CameraDeviceClient::dump(int fd, const Vector<String16>& args) {
Eino-Ville Talvalac4003962016-01-13 10:07:04 -08001890 return BasicClient::dump(fd, args);
1891}
1892
1893status_t CameraDeviceClient::dumpClient(int fd, const Vector<String16>& args) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001894 dprintf(fd, " CameraDeviceClient[%s] (%p) dump:\n",
Austin Borger0fb3ad92023-06-01 16:51:35 -07001895 mCameraIdStr.c_str(),
Eino-Ville Talvalae992e752014-11-07 16:17:48 -08001896 (getRemoteCallback() != NULL ?
Marco Nelissenf8880202014-11-14 07:58:25 -08001897 IInterface::asBinder(getRemoteCallback()).get() : NULL) );
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001898 dprintf(fd, " Current client UID %u\n", mClientUid);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001899
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001900 dprintf(fd, " State:\n");
1901 dprintf(fd, " Request ID counter: %d\n", mRequestIdCounter);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001902 if (mInputStream.configured) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001903 dprintf(fd, " Current input stream ID: %d\n", mInputStream.id);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001904 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001905 dprintf(fd, " No input stream configured.\n");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001906 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001907 if (!mStreamMap.isEmpty()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001908 dprintf(fd, " Current output stream/surface IDs:\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001909 for (size_t i = 0; i < mStreamMap.size(); i++) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001910 dprintf(fd, " Stream %d Surface %d\n",
Shuzhen Wang0129d522016-10-30 22:43:41 -07001911 mStreamMap.valueAt(i).streamId(),
1912 mStreamMap.valueAt(i).surfaceId());
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001913 }
Zhijun He5d677d12016-05-29 16:52:39 -07001914 } else if (!mDeferredStreams.isEmpty()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001915 dprintf(fd, " Current deferred surface output stream IDs:\n");
Zhijun He5d677d12016-05-29 16:52:39 -07001916 for (auto& streamId : mDeferredStreams) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001917 dprintf(fd, " Stream %d\n", streamId);
Zhijun He5d677d12016-05-29 16:52:39 -07001918 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001919 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001920 dprintf(fd, " No output streams configured.\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001921 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001922 // TODO: print dynamic/request section from most recent requests
1923 mFrameProcessor->dump(fd, args);
1924
1925 return dumpDevice(fd, args);
1926}
1927
Austin Borger0fb3ad92023-06-01 16:51:35 -07001928status_t CameraDeviceClient::startWatchingTags(const std::string &tags, int out) {
Avichal Rakesh7e53cad2021-10-05 13:46:30 -07001929 sp<CameraDeviceBase> device = mDevice;
1930 if (!device) {
1931 dprintf(out, " Device is detached.");
1932 return OK;
1933 }
1934 device->startWatchingTags(tags);
1935 return OK;
1936}
1937
1938status_t CameraDeviceClient::stopWatchingTags(int out) {
1939 sp<CameraDeviceBase> device = mDevice;
1940 if (!device) {
1941 dprintf(out, " Device is detached.");
1942 return OK;
1943 }
1944 device->stopWatchingTags();
1945 return OK;
1946}
1947
1948status_t CameraDeviceClient::dumpWatchedEventsToVector(std::vector<std::string> &out) {
1949 sp<CameraDeviceBase> device = mDevice;
1950 if (!device) {
1951 return OK;
1952 }
1953 device->dumpWatchedEventsToVector(out);
1954 return OK;
1955}
1956
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001957void CameraDeviceClient::notifyError(int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -07001958 const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001959 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001960 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001961
Emilian Peev538c90e2018-12-17 18:03:19 +00001962 bool skipClientNotification = false;
Emilian Peev2f5d6012022-01-19 16:16:50 -08001963 {
1964 // Access to the composite stream map must be synchronized
1965 Mutex::Autolock l(mCompositeLock);
1966 // Composites can have multiple internal streams. Error notifications coming from such
1967 // internal streams may need to remain within camera service.
1968 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
1969 skipClientNotification |= mCompositeStreamMap.valueAt(i)->onError(errorCode,
1970 resultExtras);
1971 }
Emilian Peev538c90e2018-12-17 18:03:19 +00001972 }
1973
1974 if ((remoteCb != 0) && (!skipClientNotification)) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001975 remoteCb->onDeviceError(errorCode, resultExtras);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001976 }
1977}
1978
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001979void CameraDeviceClient::notifyRepeatingRequestError(long lastFrameNumber) {
1980 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
1981
1982 if (remoteCb != 0) {
Yin-Chia Yeh8ca23dc2017-09-05 18:15:56 -07001983 remoteCb->onRepeatingRequestError(lastFrameNumber, mStreamingRequestId);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001984 }
1985
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001986 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001987 mStreamingRequestId = REQUEST_ID_NONE;
1988}
1989
Shuzhen Wang316781a2020-08-18 18:11:01 -07001990void CameraDeviceClient::notifyIdle(
1991 int64_t requestCount, int64_t resultErrorCount, bool deviceError,
1992 const std::vector<hardware::CameraStreamStats>& streamStats) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001993 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001994 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001995
1996 if (remoteCb != 0) {
1997 remoteCb->onDeviceIdle();
1998 }
Shuzhen Wangd26b1862022-03-07 12:05:05 -08001999 Camera2ClientBase::notifyIdleWithUserTag(requestCount, resultErrorCount, deviceError,
Shuzhen Wang9372b0b2022-05-11 18:55:19 -07002000 streamStats, mUserTag, mVideoStabilizationMode);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002001}
2002
Jianing Weicb0652e2014-03-12 18:29:36 -07002003void CameraDeviceClient::notifyShutter(const CaptureResultExtras& resultExtras,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002004 nsecs_t timestamp) {
2005 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002006 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002007 if (remoteCb != 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -07002008 remoteCb->onCaptureStarted(resultExtras, timestamp);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002009 }
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07002010 Camera2ClientBase::notifyShutter(resultExtras, timestamp);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08002011
Emilian Peev2f5d6012022-01-19 16:16:50 -08002012 // Access to the composite stream map must be synchronized
2013 Mutex::Autolock l(mCompositeLock);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08002014 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
2015 mCompositeStreamMap.valueAt(i)->onShutter(resultExtras, timestamp);
2016 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002017}
2018
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002019void CameraDeviceClient::notifyPrepared(int streamId) {
2020 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002021 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002022 if (remoteCb != 0) {
2023 remoteCb->onPrepared(streamId);
2024 }
2025}
2026
Shuzhen Wang9d066012016-09-30 11:30:20 -07002027void CameraDeviceClient::notifyRequestQueueEmpty() {
2028 // Thread safe. Don't bother locking.
2029 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
2030 if (remoteCb != 0) {
2031 remoteCb->onRequestQueueEmpty();
2032 }
2033}
2034
Igor Murashkine7ee7632013-06-11 18:10:18 -07002035void CameraDeviceClient::detachDevice() {
2036 if (mDevice == 0) return;
2037
Shuzhen Wang316781a2020-08-18 18:11:01 -07002038 nsecs_t startTime = systemTime();
Austin Borger0fb3ad92023-06-01 16:51:35 -07002039 ALOGV("Camera %s: Stopping processors", mCameraIdStr.c_str());
Igor Murashkine7ee7632013-06-11 18:10:18 -07002040
Emilian Peev7ee731f2022-02-08 14:55:52 -08002041 if (mFrameProcessor.get() != nullptr) {
2042 mFrameProcessor->removeListener(
2043 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MIN_ID,
2044 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MAX_ID, /*listener*/this);
2045 mFrameProcessor->requestExit();
Austin Borger0fb3ad92023-06-01 16:51:35 -07002046 ALOGV("Camera %s: Waiting for threads", mCameraIdStr.c_str());
Emilian Peev7ee731f2022-02-08 14:55:52 -08002047 mFrameProcessor->join();
Austin Borger0fb3ad92023-06-01 16:51:35 -07002048 ALOGV("Camera %s: Disconnecting device", mCameraIdStr.c_str());
Emilian Peev7ee731f2022-02-08 14:55:52 -08002049 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07002050
2051 // WORKAROUND: HAL refuses to disconnect while there's streams in flight
2052 {
Emilian Peev6b51d7d2018-07-23 11:41:44 +01002053 int64_t lastFrameNumber;
Igor Murashkine7ee7632013-06-11 18:10:18 -07002054 status_t code;
Emilian Peev6b51d7d2018-07-23 11:41:44 +01002055 if ((code = mDevice->flush(&lastFrameNumber)) != OK) {
2056 ALOGE("%s: flush failed with code 0x%x", __FUNCTION__, code);
2057 }
2058
Igor Murashkine7ee7632013-06-11 18:10:18 -07002059 if ((code = mDevice->waitUntilDrained()) != OK) {
2060 ALOGE("%s: waitUntilDrained failed with code 0x%x", __FUNCTION__,
2061 code);
2062 }
2063 }
2064
Emilian Peev2f5d6012022-01-19 16:16:50 -08002065 {
2066 Mutex::Autolock l(mCompositeLock);
2067 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
2068 auto ret = mCompositeStreamMap.valueAt(i)->deleteInternalStreams();
2069 if (ret != OK) {
2070 ALOGE("%s: Failed removing composite stream %s (%d)", __FUNCTION__,
2071 strerror(-ret), ret);
2072 }
Emilian Peev5e4c7322019-10-22 14:20:52 -07002073 }
Emilian Peev2f5d6012022-01-19 16:16:50 -08002074 mCompositeStreamMap.clear();
Emilian Peev5e4c7322019-10-22 14:20:52 -07002075 }
Emilian Peev5e4c7322019-10-22 14:20:52 -07002076
Igor Murashkine7ee7632013-06-11 18:10:18 -07002077 Camera2ClientBase::detachDevice();
Shuzhen Wang316781a2020-08-18 18:11:01 -07002078
2079 int32_t closeLatencyMs = ns2ms(systemTime() - startTime);
2080 CameraServiceProxyWrapper::logClose(mCameraIdStr, closeLatencyMs);
Igor Murashkine7ee7632013-06-11 18:10:18 -07002081}
2082
2083/** Device-related methods */
Jianing Weicb0652e2014-03-12 18:29:36 -07002084void CameraDeviceClient::onResultAvailable(const CaptureResult& result) {
Igor Murashkine7ee7632013-06-11 18:10:18 -07002085 ATRACE_CALL();
2086 ALOGV("%s", __FUNCTION__);
2087
Igor Murashkin4fb55c12013-08-29 17:43:01 -07002088 // Thread-safe. No lock necessary.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002089 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = mRemoteCallback;
Igor Murashkin4fb55c12013-08-29 17:43:01 -07002090 if (remoteCb != NULL) {
Shuzhen Wang5c22c152017-12-31 17:12:25 -08002091 remoteCb->onResultReceived(result.mMetadata, result.mResultExtras,
2092 result.mPhysicalMetadatas);
Igor Murashkine7ee7632013-06-11 18:10:18 -07002093 }
Emilian Peev538c90e2018-12-17 18:03:19 +00002094
Emilian Peev2f5d6012022-01-19 16:16:50 -08002095 // Access to the composite stream map must be synchronized
2096 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +00002097 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
2098 mCompositeStreamMap.valueAt(i)->onResultAvailable(result);
2099 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07002100}
2101
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002102binder::Status CameraDeviceClient::checkPidStatus(const char* checkLocation) {
Eino-Ville Talvala6192b892016-04-04 12:31:18 -07002103 if (mDisconnected) {
2104 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED,
2105 "The camera device has been disconnected");
2106 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002107 status_t res = checkPid(checkLocation);
2108 return (res == OK) ? binder::Status::ok() :
2109 STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
2110 "Attempt to use camera from a different process than original client");
2111}
2112
Igor Murashkine7ee7632013-06-11 18:10:18 -07002113// TODO: move to Camera2ClientBase
2114bool CameraDeviceClient::enforceRequestPermissions(CameraMetadata& metadata) {
2115
Jayant Chowdhary12361932018-08-27 14:46:13 -07002116 const int pid = CameraThreadState::getCallingPid();
Igor Murashkine7ee7632013-06-11 18:10:18 -07002117 const int selfPid = getpid();
2118 camera_metadata_entry_t entry;
2119
2120 /**
2121 * Mixin default important security values
2122 * - android.led.transmit = defaulted ON
2123 */
2124 CameraMetadata staticInfo = mDevice->info();
2125 entry = staticInfo.find(ANDROID_LED_AVAILABLE_LEDS);
2126 for(size_t i = 0; i < entry.count; ++i) {
2127 uint8_t led = entry.data.u8[i];
2128
2129 switch(led) {
2130 case ANDROID_LED_AVAILABLE_LEDS_TRANSMIT: {
2131 uint8_t transmitDefault = ANDROID_LED_TRANSMIT_ON;
2132 if (!metadata.exists(ANDROID_LED_TRANSMIT)) {
2133 metadata.update(ANDROID_LED_TRANSMIT,
2134 &transmitDefault, 1);
2135 }
2136 break;
2137 }
2138 }
2139 }
2140
2141 // We can do anything!
2142 if (pid == selfPid) {
2143 return true;
2144 }
2145
2146 /**
2147 * Permission check special fields in the request
2148 * - android.led.transmit = android.permission.CAMERA_DISABLE_TRANSMIT
2149 */
2150 entry = metadata.find(ANDROID_LED_TRANSMIT);
2151 if (entry.count > 0 && entry.data.u8[0] != ANDROID_LED_TRANSMIT_ON) {
2152 String16 permissionString =
Austin Borger0fb3ad92023-06-01 16:51:35 -07002153 toString16("android.permission.CAMERA_DISABLE_TRANSMIT_LED");
Igor Murashkine7ee7632013-06-11 18:10:18 -07002154 if (!checkCallingPermission(permissionString)) {
Jayant Chowdhary12361932018-08-27 14:46:13 -07002155 const int uid = CameraThreadState::getCallingUid();
Igor Murashkine7ee7632013-06-11 18:10:18 -07002156 ALOGE("Permission Denial: "
2157 "can't disable transmit LED pid=%d, uid=%d", pid, uid);
2158 return false;
2159 }
2160 }
2161
2162 return true;
2163}
2164
Shuzhen Wang610d7b82022-02-08 14:37:22 -08002165status_t CameraDeviceClient::getRotationTransformLocked(int mirrorMode,
2166 int32_t* transform) {
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07002167 ALOGV("%s: begin", __FUNCTION__);
2168
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07002169 const CameraMetadata& staticInfo = mDevice->info();
Shuzhen Wang610d7b82022-02-08 14:37:22 -08002170 return CameraUtils::getRotationTransform(staticInfo, mirrorMode, transform);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07002171}
2172
Emilian Peevf4816702020-04-03 15:44:51 -07002173binder::Status CameraDeviceClient::mapRequestTemplate(int templateId,
2174 camera_request_template_t* tempId /*out*/) {
2175 binder::Status ret = binder::Status::ok();
2176
2177 if (tempId == nullptr) {
2178 ret = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Austin Borger0fb3ad92023-06-01 16:51:35 -07002179 "Camera %s: Invalid template argument", mCameraIdStr.c_str());
Emilian Peevf4816702020-04-03 15:44:51 -07002180 return ret;
2181 }
2182 switch(templateId) {
2183 case ICameraDeviceUser::TEMPLATE_PREVIEW:
2184 *tempId = camera_request_template_t::CAMERA_TEMPLATE_PREVIEW;
2185 break;
2186 case ICameraDeviceUser::TEMPLATE_RECORD:
2187 *tempId = camera_request_template_t::CAMERA_TEMPLATE_VIDEO_RECORD;
2188 break;
2189 case ICameraDeviceUser::TEMPLATE_STILL_CAPTURE:
2190 *tempId = camera_request_template_t::CAMERA_TEMPLATE_STILL_CAPTURE;
2191 break;
2192 case ICameraDeviceUser::TEMPLATE_VIDEO_SNAPSHOT:
2193 *tempId = camera_request_template_t::CAMERA_TEMPLATE_VIDEO_SNAPSHOT;
2194 break;
2195 case ICameraDeviceUser::TEMPLATE_ZERO_SHUTTER_LAG:
2196 *tempId = camera_request_template_t::CAMERA_TEMPLATE_ZERO_SHUTTER_LAG;
2197 break;
2198 case ICameraDeviceUser::TEMPLATE_MANUAL:
2199 *tempId = camera_request_template_t::CAMERA_TEMPLATE_MANUAL;
2200 break;
2201 default:
2202 ret = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
2203 "Camera %s: Template ID %d is invalid or not supported",
Austin Borger0fb3ad92023-06-01 16:51:35 -07002204 mCameraIdStr.c_str(), templateId);
Emilian Peevf4816702020-04-03 15:44:51 -07002205 return ret;
2206 }
2207
2208 return ret;
2209}
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08002210
Austin Borger0fb3ad92023-06-01 16:51:35 -07002211const CameraMetadata &CameraDeviceClient::getStaticInfo(const std::string &cameraId) {
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08002212 if (mDevice->getId() == cameraId) {
2213 return mDevice->info();
2214 }
2215 return mDevice->infoPhysical(cameraId);
2216}
2217
Austin Borger0fb3ad92023-06-01 16:51:35 -07002218bool CameraDeviceClient::isUltraHighResolutionSensor(const std::string &cameraId) {
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08002219 const CameraMetadata &deviceInfo = getStaticInfo(cameraId);
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07002220 return SessionConfigurationUtils::isUltraHighResolutionSensor(deviceInfo);
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08002221}
2222
2223bool CameraDeviceClient::isSensorPixelModeConsistent(
2224 const std::list<int> &streamIdList, const CameraMetadata &settings) {
2225 // First we get the sensorPixelMode from the settings metadata.
2226 int32_t sensorPixelMode = ANDROID_SENSOR_PIXEL_MODE_DEFAULT;
2227 camera_metadata_ro_entry sensorPixelModeEntry = settings.find(ANDROID_SENSOR_PIXEL_MODE);
2228 if (sensorPixelModeEntry.count != 0) {
2229 sensorPixelMode = sensorPixelModeEntry.data.u8[0];
2230 if (sensorPixelMode != ANDROID_SENSOR_PIXEL_MODE_DEFAULT &&
2231 sensorPixelMode != ANDROID_SENSOR_PIXEL_MODE_MAXIMUM_RESOLUTION) {
2232 ALOGE("%s: Request sensor pixel mode not is not one of the valid values %d",
2233 __FUNCTION__, sensorPixelMode);
2234 return false;
2235 }
2236 }
2237 // Check whether each stream has max resolution allowed.
2238 bool consistent = true;
2239 for (auto it : streamIdList) {
2240 auto const streamInfoIt = mStreamInfoMap.find(it);
2241 if (streamInfoIt == mStreamInfoMap.end()) {
2242 ALOGE("%s: stream id %d not created, skipping", __FUNCTION__, it);
2243 return false;
2244 }
2245 consistent =
2246 streamInfoIt->second.sensorPixelModesUsed.find(sensorPixelMode) !=
2247 streamInfoIt->second.sensorPixelModesUsed.end();
2248 if (!consistent) {
2249 ALOGE("sensorPixelMode used %i not consistent with configured modes", sensorPixelMode);
2250 for (auto m : streamInfoIt->second.sensorPixelModesUsed) {
2251 ALOGE("sensor pixel mode used list: %i", m);
2252 }
2253 break;
2254 }
2255 }
2256
2257 return consistent;
2258}
2259
Igor Murashkine7ee7632013-06-11 18:10:18 -07002260} // namespace android