blob: 06f96f390b2d345ddf0fae75c77245e27a8ee123 [file] [log] [blame]
Igor Murashkine7ee7632013-06-11 18:10:18 -07001/*
Shuzhen Wangc28189a2017-11-27 23:05:10 -08002 * Copyright (C) 2013-2018 The Android Open Source Project
Igor Murashkine7ee7632013-06-11 18:10:18 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "CameraDeviceClient"
18#define ATRACE_TAG ATRACE_TAG_CAMERA
Jianing Weicb0652e2014-03-12 18:29:36 -070019//#define LOG_NDEBUG 0
Igor Murashkine7ee7632013-06-11 18:10:18 -070020
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070021#include <cutils/properties.h>
Jayant Chowdhary12361932018-08-27 14:46:13 -070022#include <utils/CameraThreadState.h>
Igor Murashkine7ee7632013-06-11 18:10:18 -070023#include <utils/Log.h>
Colin Crossb8a9dbb2020-08-27 04:12:26 +000024#include <utils/SessionConfigurationUtils.h>
Igor Murashkine7ee7632013-06-11 18:10:18 -070025#include <utils/Trace.h>
Igor Murashkine7ee7632013-06-11 18:10:18 -070026#include <gui/Surface.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070027#include <camera/camera2/CaptureRequest.h>
Ruben Brunk5698d442014-06-18 10:39:40 -070028#include <camera/CameraUtils.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070029
30#include "common/CameraDeviceBase.h"
Emilian Peev35ae8262018-11-08 13:11:32 +000031#include "device3/Camera3Device.h"
32#include "device3/Camera3OutputStream.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070033#include "api2/CameraDeviceClient.h"
34
Emilian Peev00420d22018-02-05 21:33:13 +000035#include <camera_metadata_hidden.h>
36
Emilian Peev538c90e2018-12-17 18:03:19 +000037#include "DepthCompositeStream.h"
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -080038#include "HeicCompositeStream.h"
Emilian Peev434248e2022-10-06 14:58:54 -070039#include "JpegRCompositeStream.h"
Emilian Peev538c90e2018-12-17 18:03:19 +000040
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080041// Convenience methods for constructing binder::Status objects for error returns
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070042
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080043#define STATUS_ERROR(errorCode, errorString) \
44 binder::Status::fromServiceSpecificError(errorCode, \
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -080045 String8::format("%s:%d: %s", __FUNCTION__, __LINE__, errorString))
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080046
47#define STATUS_ERROR_FMT(errorCode, errorString, ...) \
48 binder::Status::fromServiceSpecificError(errorCode, \
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -080049 String8::format("%s:%d: " errorString, __FUNCTION__, __LINE__, \
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080050 __VA_ARGS__))
Igor Murashkine7ee7632013-06-11 18:10:18 -070051
52namespace android {
53using namespace camera2;
Austin Borgerea931242021-12-13 23:10:41 +000054using namespace camera3;
Emilian Peevf4816702020-04-03 15:44:51 -070055using camera3::camera_stream_rotation_t::CAMERA_STREAM_ROTATION_0;
Igor Murashkine7ee7632013-06-11 18:10:18 -070056
57CameraDeviceClientBase::CameraDeviceClientBase(
58 const sp<CameraService>& cameraService,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080059 const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback,
Igor Murashkine7ee7632013-06-11 18:10:18 -070060 const String16& clientPackageName,
Jayant Chowdharyeb0169f2022-01-31 00:00:02 -080061 bool systemNativeClient,
Jooyung Han3f9a3b42020-01-23 12:27:18 +090062 const std::optional<String16>& clientFeatureId,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080063 const String8& cameraId,
Jing Mikec7f9b132023-03-12 11:12:04 +080064 [[maybe_unused]] int api1CameraId,
Igor Murashkine7ee7632013-06-11 18:10:18 -070065 int cameraFacing,
Emilian Peev8b64f282021-03-25 16:49:57 -070066 int sensorOrientation,
Igor Murashkine7ee7632013-06-11 18:10:18 -070067 int clientPid,
68 uid_t clientUid,
Austin Borger18b30a72022-10-27 12:20:29 -070069 int servicePid,
70 bool overrideToPortrait) :
Eino-Ville Talvalae992e752014-11-07 16:17:48 -080071 BasicClient(cameraService,
Marco Nelissenf8880202014-11-14 07:58:25 -080072 IInterface::asBinder(remoteCallback),
Eino-Ville Talvalae992e752014-11-07 16:17:48 -080073 clientPackageName,
Jayant Chowdharyeb0169f2022-01-31 00:00:02 -080074 systemNativeClient,
Philip P. Moltmann9e648f62019-11-04 12:52:45 -080075 clientFeatureId,
Eino-Ville Talvalae992e752014-11-07 16:17:48 -080076 cameraId,
77 cameraFacing,
Emilian Peev8b64f282021-03-25 16:49:57 -070078 sensorOrientation,
Eino-Ville Talvalae992e752014-11-07 16:17:48 -080079 clientPid,
80 clientUid,
Austin Borger18b30a72022-10-27 12:20:29 -070081 servicePid,
82 overrideToPortrait),
Igor Murashkine7ee7632013-06-11 18:10:18 -070083 mRemoteCallback(remoteCallback) {
84}
Igor Murashkine7ee7632013-06-11 18:10:18 -070085
86// Interface used by CameraService
87
88CameraDeviceClient::CameraDeviceClient(const sp<CameraService>& cameraService,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080089 const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback,
Austin Borger74fca042022-05-23 12:41:21 -070090 std::shared_ptr<CameraServiceProxyWrapper> cameraServiceProxyWrapper,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080091 const String16& clientPackageName,
Jayant Chowdharyeb0169f2022-01-31 00:00:02 -080092 bool systemNativeClient,
Jooyung Han3f9a3b42020-01-23 12:27:18 +090093 const std::optional<String16>& clientFeatureId,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080094 const String8& 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 Borger18b30a72022-10-27 12:20:29 -0700100 bool overrideForPerfClass,
malikakash82ed4352023-07-21 22:44:34 +0000101 bool overrideToPortrait,
102 const String8& originalCameraId) :
Austin Borger74fca042022-05-23 12:41:21 -0700103 Camera2ClientBase(cameraService, remoteCallback, cameraServiceProxyWrapper, clientPackageName,
104 systemNativeClient, clientFeatureId, cameraId, /*API1 camera ID*/ -1, cameraFacing,
Austin Borger18b30a72022-10-27 12:20:29 -0700105 sensorOrientation, clientPid, clientUid, servicePid, overrideForPerfClass,
106 overrideToPortrait),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700107 mInputStream(),
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700108 mStreamingRequestId(REQUEST_ID_NONE),
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700109 mRequestIdCounter(0),
malikakash82ed4352023-07-21 22:44:34 +0000110 mOverrideForPerfClass(overrideForPerfClass),
111 mOriginalCameraId(originalCameraId) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700112 ATRACE_CALL();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800113 ALOGI("CameraDeviceClient %s: Opened", cameraId.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700114}
115
Emilian Peevbd8c5032018-02-14 23:05:40 +0000116status_t CameraDeviceClient::initialize(sp<CameraProviderManager> manager,
117 const String8& monitorTags) {
118 return initializeImpl(manager, monitorTags);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800119}
120
121template<typename TProviderPtr>
Emilian Peevbd8c5032018-02-14 23:05:40 +0000122status_t CameraDeviceClient::initializeImpl(TProviderPtr providerPtr, const String8& monitorTags) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700123 ATRACE_CALL();
124 status_t res;
125
Emilian Peevbd8c5032018-02-14 23:05:40 +0000126 res = Camera2ClientBase::initialize(providerPtr, monitorTags);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700127 if (res != OK) {
128 return res;
129 }
130
131 String8 threadName;
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -0700132 mFrameProcessor = new FrameProcessorBase(mDevice);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800133 threadName = String8::format("CDU-%s-FrameProc", mCameraIdStr.string());
Austin Borger7b129542022-06-09 13:23:06 -0700134 res = mFrameProcessor->run(threadName.string());
135 if (res != OK) {
136 ALOGE("%s: Unable to start frame processor thread: %s (%d)",
137 __FUNCTION__, strerror(-res), res);
138 return res;
139 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700140
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800141 mFrameProcessor->registerListener(camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MIN_ID,
142 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MAX_ID,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -0800143 /*listener*/this,
Zhijun He25a0aef2014-06-25 11:40:02 -0700144 /*sendPartials*/true);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700145
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800146 const CameraMetadata &deviceInfo = mDevice->info();
147 camera_metadata_ro_entry_t physicalKeysEntry = deviceInfo.find(
Emilian Peev00420d22018-02-05 21:33:13 +0000148 ANDROID_REQUEST_AVAILABLE_PHYSICAL_CAMERA_REQUEST_KEYS);
149 if (physicalKeysEntry.count > 0) {
150 mSupportedPhysicalRequestKeys.insert(mSupportedPhysicalRequestKeys.begin(),
151 physicalKeysEntry.data.i32,
152 physicalKeysEntry.data.i32 + physicalKeysEntry.count);
153 }
154
Emilian Peev2295df72021-11-12 18:14:10 -0800155 auto entry = deviceInfo.find(ANDROID_REQUEST_AVAILABLE_CAPABILITIES);
156 mDynamicProfileMap.emplace(
157 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD,
158 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD);
159 if (entry.count > 0) {
Shuzhen Wangc8ab4522021-12-14 20:12:42 -0800160 const auto it = std::find(entry.data.u8, entry.data.u8 + entry.count,
Emilian Peev2295df72021-11-12 18:14:10 -0800161 ANDROID_REQUEST_AVAILABLE_CAPABILITIES_DYNAMIC_RANGE_TEN_BIT);
Shuzhen Wangc8ab4522021-12-14 20:12:42 -0800162 if (it != entry.data.u8 + entry.count) {
Emilian Peev2295df72021-11-12 18:14:10 -0800163 entry = deviceInfo.find(ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP);
Emilian Peevc81a7592022-02-14 17:38:18 -0800164 if (entry.count > 0 || ((entry.count % 3) != 0)) {
165 int64_t standardBitmap =
166 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD;
167 for (size_t i = 0; i < entry.count; i += 3) {
168 if (entry.data.i64[i] !=
Emilian Peev2295df72021-11-12 18:14:10 -0800169 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD) {
Emilian Peevc81a7592022-02-14 17:38:18 -0800170 mDynamicProfileMap.emplace(entry.data.i64[i], entry.data.i64[i+1]);
171 if ((entry.data.i64[i+1] == 0) || (entry.data.i64[i+1] &
Emilian Peev2295df72021-11-12 18:14:10 -0800172 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD)) {
Emilian Peevc81a7592022-02-14 17:38:18 -0800173 standardBitmap |= entry.data.i64[i];
Emilian Peev2295df72021-11-12 18:14:10 -0800174 }
175 } else {
Emilian Peevc81a7592022-02-14 17:38:18 -0800176 ALOGE("%s: Device %s includes unexpected profile entry: 0x%" PRIx64 "!",
177 __FUNCTION__, mCameraIdStr.c_str(), entry.data.i64[i]);
Emilian Peev2295df72021-11-12 18:14:10 -0800178 }
179 }
Emilian Peevef715e22022-05-19 17:44:33 -0700180 mDynamicProfileMap[ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD] =
181 standardBitmap;
Emilian Peev2295df72021-11-12 18:14:10 -0800182 } else {
183 ALOGE("%s: Device %s supports 10-bit output but doesn't include a dynamic range"
184 " profile map!", __FUNCTION__, mCameraIdStr.c_str());
185 }
186 }
187 }
188
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700189 mProviderManager = providerPtr;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800190 // Cache physical camera ids corresponding to this device and also the high
191 // resolution sensors in this device + physical camera ids
192 mProviderManager->isLogicalCamera(mCameraIdStr.string(), &mPhysicalCameraIds);
Jayant Chowdharydbd1efb2023-02-07 16:14:48 -0800193 if (supportsUltraHighResolutionCapture(mCameraIdStr)) {
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800194 mHighResolutionSensors.insert(mCameraIdStr.string());
195 }
196 for (auto &physicalId : mPhysicalCameraIds) {
Jayant Chowdharydbd1efb2023-02-07 16:14:48 -0800197 if (supportsUltraHighResolutionCapture(String8(physicalId.c_str()))) {
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800198 mHighResolutionSensors.insert(physicalId.c_str());
199 }
200 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700201 return OK;
202}
203
204CameraDeviceClient::~CameraDeviceClient() {
205}
206
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800207binder::Status CameraDeviceClient::submitRequest(
208 const hardware::camera2::CaptureRequest& request,
209 bool streaming,
210 /*out*/
211 hardware::camera2::utils::SubmitInfo *submitInfo) {
212 std::vector<hardware::camera2::CaptureRequest> requestList = { request };
213 return submitRequestList(requestList, streaming, submitInfo);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700214}
215
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800216binder::Status CameraDeviceClient::insertGbpLocked(const sp<IGraphicBufferProducer>& gbp,
Emilian Peevf873aa52018-01-26 14:58:28 +0000217 SurfaceMap* outSurfaceMap, Vector<int32_t>* outputStreamIds, int32_t *currentStreamId) {
Emilian Peev538c90e2018-12-17 18:03:19 +0000218 int compositeIdx;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800219 int idx = mStreamMap.indexOfKey(IInterface::asBinder(gbp));
220
Emilian Peev2f5d6012022-01-19 16:16:50 -0800221 Mutex::Autolock l(mCompositeLock);
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800222 // Trying to submit request with surface that wasn't created
223 if (idx == NAME_NOT_FOUND) {
224 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
225 " we have not called createStream on",
226 __FUNCTION__, mCameraIdStr.string());
227 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
228 "Request targets Surface that is not part of current capture session");
Emilian Peev538c90e2018-12-17 18:03:19 +0000229 } else if ((compositeIdx = mCompositeStreamMap.indexOfKey(IInterface::asBinder(gbp)))
230 != NAME_NOT_FOUND) {
231 mCompositeStreamMap.valueAt(compositeIdx)->insertGbp(outSurfaceMap, outputStreamIds,
232 currentStreamId);
233 return binder::Status::ok();
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800234 }
235
236 const StreamSurfaceId& streamSurfaceId = mStreamMap.valueAt(idx);
237 if (outSurfaceMap->find(streamSurfaceId.streamId()) == outSurfaceMap->end()) {
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800238 outputStreamIds->push_back(streamSurfaceId.streamId());
239 }
240 (*outSurfaceMap)[streamSurfaceId.streamId()].push_back(streamSurfaceId.surfaceId());
241
242 ALOGV("%s: Camera %s: Appending output stream %d surface %d to request",
243 __FUNCTION__, mCameraIdStr.string(), streamSurfaceId.streamId(),
244 streamSurfaceId.surfaceId());
245
Emilian Peevf873aa52018-01-26 14:58:28 +0000246 if (currentStreamId != nullptr) {
247 *currentStreamId = streamSurfaceId.streamId();
248 }
249
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800250 return binder::Status::ok();
251}
252
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800253static std::list<int> getIntersection(const std::unordered_set<int> &streamIdsForThisCamera,
254 const Vector<int> &streamIdsForThisRequest) {
255 std::list<int> intersection;
256 for (auto &streamId : streamIdsForThisRequest) {
257 if (streamIdsForThisCamera.find(streamId) != streamIdsForThisCamera.end()) {
258 intersection.emplace_back(streamId);
259 }
260 }
261 return intersection;
262}
263
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800264binder::Status CameraDeviceClient::submitRequestList(
265 const std::vector<hardware::camera2::CaptureRequest>& requests,
266 bool streaming,
267 /*out*/
268 hardware::camera2::utils::SubmitInfo *submitInfo) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700269 ATRACE_CALL();
Mark Salyzyn50468412014-06-18 16:33:43 -0700270 ALOGV("%s-start of function. Request list size %zu", __FUNCTION__, requests.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700271
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800272 binder::Status res = binder::Status::ok();
273 status_t err;
274 if ( !(res = checkPidStatus(__FUNCTION__) ).isOk()) {
275 return res;
276 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700277
278 Mutex::Autolock icl(mBinderSerializationLock);
279
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800280 if (!mDevice.get()) {
281 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
282 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700283
284 if (requests.empty()) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800285 ALOGE("%s: Camera %s: Sent null request. Rejecting request.",
286 __FUNCTION__, mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800287 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Empty request list");
Jianing Wei90e59c92014-03-12 18:29:36 -0700288 }
289
Emilian Peevaebbe412018-01-15 13:53:24 +0000290 List<const CameraDeviceBase::PhysicalCameraSettingsList> metadataRequestList;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700291 std::list<const SurfaceMap> surfaceMapList;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800292 submitInfo->mRequestId = mRequestIdCounter;
Jianing Wei90e59c92014-03-12 18:29:36 -0700293 uint32_t loopCounter = 0;
294
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800295 for (auto&& request: requests) {
296 if (request.mIsReprocess) {
Chien-Yu Chened0412e2015-04-27 15:04:22 -0700297 if (!mInputStream.configured) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800298 ALOGE("%s: Camera %s: no input stream is configured.", __FUNCTION__,
299 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800300 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800301 "No input configured for camera %s but request is for reprocessing",
302 mCameraIdStr.string());
Chien-Yu Chened0412e2015-04-27 15:04:22 -0700303 } else if (streaming) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800304 ALOGE("%s: Camera %s: streaming reprocess requests not supported.", __FUNCTION__,
305 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800306 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
307 "Repeating reprocess requests not supported");
Emilian Peevaebbe412018-01-15 13:53:24 +0000308 } else if (request.mPhysicalCameraSettings.size() > 1) {
309 ALOGE("%s: Camera %s: reprocess requests not supported for "
310 "multiple physical cameras.", __FUNCTION__,
311 mCameraIdStr.string());
312 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
313 "Reprocess requests not supported for multiple cameras");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700314 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700315 }
316
Emilian Peevaebbe412018-01-15 13:53:24 +0000317 if (request.mPhysicalCameraSettings.empty()) {
318 ALOGE("%s: Camera %s: request doesn't contain any settings.", __FUNCTION__,
319 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800320 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
Emilian Peevaebbe412018-01-15 13:53:24 +0000321 "Request doesn't contain any settings");
322 }
323
324 //The first capture settings should always match the logical camera id
325 String8 logicalId(request.mPhysicalCameraSettings.begin()->id.c_str());
malikakash82ed4352023-07-21 22:44:34 +0000326 if (mDevice->getId() != logicalId && mOriginalCameraId != logicalId) {
Emilian Peevaebbe412018-01-15 13:53:24 +0000327 ALOGE("%s: Camera %s: Invalid camera request settings.", __FUNCTION__,
328 mCameraIdStr.string());
329 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
330 "Invalid camera request settings");
331 }
332
Emilian Peevaebbe412018-01-15 13:53:24 +0000333 if (request.mSurfaceList.isEmpty() && request.mStreamIdxList.size() == 0) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800334 ALOGE("%s: Camera %s: Requests must have at least one surface target. "
335 "Rejecting request.", __FUNCTION__, mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800336 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
337 "Request has no output targets");
Jianing Wei90e59c92014-03-12 18:29:36 -0700338 }
339
Jianing Wei90e59c92014-03-12 18:29:36 -0700340 /**
Shuzhen Wang0129d522016-10-30 22:43:41 -0700341 * Write in the output stream IDs and map from stream ID to surface ID
342 * which we calculate from the capture request's list of surface target
Jianing Wei90e59c92014-03-12 18:29:36 -0700343 */
Shuzhen Wang0129d522016-10-30 22:43:41 -0700344 SurfaceMap surfaceMap;
Jianing Wei90e59c92014-03-12 18:29:36 -0700345 Vector<int32_t> outputStreamIds;
Emilian Peevf873aa52018-01-26 14:58:28 +0000346 std::vector<std::string> requestedPhysicalIds;
Emilian Peevc81a7592022-02-14 17:38:18 -0800347 int64_t dynamicProfileBitmap = 0;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800348 if (request.mSurfaceList.size() > 0) {
Chih-Hung Hsieh3ef324d2018-12-11 11:48:12 -0800349 for (const sp<Surface>& surface : request.mSurfaceList) {
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800350 if (surface == 0) continue;
Jianing Wei90e59c92014-03-12 18:29:36 -0700351
Emilian Peevf873aa52018-01-26 14:58:28 +0000352 int32_t streamId;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800353 sp<IGraphicBufferProducer> gbp = surface->getIGraphicBufferProducer();
Emilian Peevf873aa52018-01-26 14:58:28 +0000354 res = insertGbpLocked(gbp, &surfaceMap, &outputStreamIds, &streamId);
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800355 if (!res.isOk()) {
356 return res;
357 }
Emilian Peevf873aa52018-01-26 14:58:28 +0000358
359 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
360 if (index >= 0) {
361 String8 requestedPhysicalId(
362 mConfiguredOutputs.valueAt(index).getPhysicalCameraId());
363 requestedPhysicalIds.push_back(requestedPhysicalId.string());
Emilian Peev2295df72021-11-12 18:14:10 -0800364 dynamicProfileBitmap |=
365 mConfiguredOutputs.valueAt(index).getDynamicRangeProfile();
Emilian Peevf873aa52018-01-26 14:58:28 +0000366 } else {
367 ALOGW("%s: Output stream Id not found among configured outputs!", __FUNCTION__);
368 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700369 }
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800370 } else {
371 for (size_t i = 0; i < request.mStreamIdxList.size(); i++) {
372 int streamId = request.mStreamIdxList.itemAt(i);
373 int surfaceIdx = request.mSurfaceIdxList.itemAt(i);
Jianing Wei90e59c92014-03-12 18:29:36 -0700374
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800375 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
376 if (index < 0) {
377 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
378 " we have not called createStream on: stream %d",
379 __FUNCTION__, mCameraIdStr.string(), streamId);
380 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
381 "Request targets Surface that is not part of current capture session");
382 }
383
384 const auto& gbps = mConfiguredOutputs.valueAt(index).getGraphicBufferProducers();
385 if ((size_t)surfaceIdx >= gbps.size()) {
386 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
387 " we have not called createStream on: stream %d, surfaceIdx %d",
388 __FUNCTION__, mCameraIdStr.string(), streamId, surfaceIdx);
389 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
390 "Request targets Surface has invalid surface index");
391 }
392
Emilian Peevf873aa52018-01-26 14:58:28 +0000393 res = insertGbpLocked(gbps[surfaceIdx], &surfaceMap, &outputStreamIds, nullptr);
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800394 if (!res.isOk()) {
395 return res;
396 }
Emilian Peevf873aa52018-01-26 14:58:28 +0000397
398 String8 requestedPhysicalId(
399 mConfiguredOutputs.valueAt(index).getPhysicalCameraId());
400 requestedPhysicalIds.push_back(requestedPhysicalId.string());
Emilian Peev2295df72021-11-12 18:14:10 -0800401 dynamicProfileBitmap |=
402 mConfiguredOutputs.valueAt(index).getDynamicRangeProfile();
403 }
404 }
405
406 if (dynamicProfileBitmap !=
407 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD) {
408 for (int i = ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD;
409 i < ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_MAX; i <<= 1) {
410 if ((dynamicProfileBitmap & i) == 0) {
411 continue;
412 }
413
414 const auto& it = mDynamicProfileMap.find(i);
415 if (it != mDynamicProfileMap.end()) {
416 if ((it->second == 0) ||
417 ((it->second & dynamicProfileBitmap) == dynamicProfileBitmap)) {
418 continue;
419 } else {
420 ALOGE("%s: Camera %s: Tried to submit a request with a surfaces that"
421 " reference an unsupported dynamic range profile combination"
Emilian Peevc81a7592022-02-14 17:38:18 -0800422 " 0x%" PRIx64 "!", __FUNCTION__, mCameraIdStr.string(),
Emilian Peev2295df72021-11-12 18:14:10 -0800423 dynamicProfileBitmap);
424 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
425 "Request targets an unsupported dynamic range profile"
426 " combination");
427 }
428 } else {
429 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
430 " references unsupported dynamic range profile 0x%x!",
431 __FUNCTION__, mCameraIdStr.string(), i);
432 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
433 "Request targets 10-bit Surface with unsupported dynamic range"
434 " profile");
435 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700436 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700437 }
438
Emilian Peevf873aa52018-01-26 14:58:28 +0000439 CameraDeviceBase::PhysicalCameraSettingsList physicalSettingsList;
440 for (const auto& it : request.mPhysicalCameraSettings) {
malikakash82ed4352023-07-21 22:44:34 +0000441 std::string resolvedId = (
442 mOriginalCameraId.string() == it.id) ? mDevice->getId().string() : it.id;
Emilian Peev00420d22018-02-05 21:33:13 +0000443 if (it.settings.isEmpty()) {
444 ALOGE("%s: Camera %s: Sent empty metadata packet. Rejecting request.",
445 __FUNCTION__, mCameraIdStr.string());
446 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
447 "Request settings are empty");
448 }
449
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800450 // Check whether the physical / logical stream has settings
451 // consistent with the sensor pixel mode(s) it was configured with.
452 // mCameraIdToStreamSet will only have ids that are high resolution
malikakash82ed4352023-07-21 22:44:34 +0000453 const auto streamIdSetIt = mHighResolutionCameraIdToStreamIdSet.find(resolvedId);
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800454 if (streamIdSetIt != mHighResolutionCameraIdToStreamIdSet.end()) {
455 std::list<int> streamIdsUsedInRequest = getIntersection(streamIdSetIt->second,
456 outputStreamIds);
457 if (!request.mIsReprocess &&
458 !isSensorPixelModeConsistent(streamIdsUsedInRequest, it.settings)) {
459 ALOGE("%s: Camera %s: Request settings CONTROL_SENSOR_PIXEL_MODE not "
460 "consistent with configured streams. Rejecting request.",
malikakash82ed4352023-07-21 22:44:34 +0000461 __FUNCTION__, resolvedId.c_str());
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800462 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
463 "Request settings CONTROL_SENSOR_PIXEL_MODE are not consistent with "
464 "streams configured");
465 }
466 }
467
malikakash82ed4352023-07-21 22:44:34 +0000468 String8 physicalId(resolvedId.c_str());
Shuzhen Wang911c6a32021-10-27 13:36:03 -0700469 bool hasTestPatternModePhysicalKey = std::find(mSupportedPhysicalRequestKeys.begin(),
470 mSupportedPhysicalRequestKeys.end(), ANDROID_SENSOR_TEST_PATTERN_MODE) !=
471 mSupportedPhysicalRequestKeys.end();
472 bool hasTestPatternDataPhysicalKey = std::find(mSupportedPhysicalRequestKeys.begin(),
473 mSupportedPhysicalRequestKeys.end(), ANDROID_SENSOR_TEST_PATTERN_DATA) !=
474 mSupportedPhysicalRequestKeys.end();
Emilian Peevf873aa52018-01-26 14:58:28 +0000475 if (physicalId != mDevice->getId()) {
476 auto found = std::find(requestedPhysicalIds.begin(), requestedPhysicalIds.end(),
malikakash82ed4352023-07-21 22:44:34 +0000477 resolvedId);
Emilian Peevf873aa52018-01-26 14:58:28 +0000478 if (found == requestedPhysicalIds.end()) {
479 ALOGE("%s: Camera %s: Physical camera id: %s not part of attached outputs.",
480 __FUNCTION__, mCameraIdStr.string(), physicalId.string());
481 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
482 "Invalid physical camera id");
483 }
Emilian Peev00420d22018-02-05 21:33:13 +0000484
485 if (!mSupportedPhysicalRequestKeys.empty()) {
486 // Filter out any unsupported physical request keys.
487 CameraMetadata filteredParams(mSupportedPhysicalRequestKeys.size());
488 camera_metadata_t *meta = const_cast<camera_metadata_t *>(
489 filteredParams.getAndLock());
490 set_camera_metadata_vendor_id(meta, mDevice->getVendorTagId());
491 filteredParams.unlock(meta);
492
493 for (const auto& keyIt : mSupportedPhysicalRequestKeys) {
494 camera_metadata_ro_entry entry = it.settings.find(keyIt);
495 if (entry.count > 0) {
496 filteredParams.update(entry);
497 }
498 }
499
malikakash82ed4352023-07-21 22:44:34 +0000500 physicalSettingsList.push_back({resolvedId, filteredParams,
Shuzhen Wang911c6a32021-10-27 13:36:03 -0700501 hasTestPatternModePhysicalKey, hasTestPatternDataPhysicalKey});
Emilian Peev00420d22018-02-05 21:33:13 +0000502 }
503 } else {
malikakash82ed4352023-07-21 22:44:34 +0000504 physicalSettingsList.push_back({resolvedId, it.settings});
Emilian Peevf873aa52018-01-26 14:58:28 +0000505 }
Emilian Peevf873aa52018-01-26 14:58:28 +0000506 }
507
508 if (!enforceRequestPermissions(physicalSettingsList.begin()->metadata)) {
509 // Callee logs
510 return STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
511 "Caller does not have permission to change restricted controls");
512 }
513
Emilian Peevaebbe412018-01-15 13:53:24 +0000514 physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_OUTPUT_STREAMS,
515 &outputStreamIds[0], outputStreamIds.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700516
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800517 if (request.mIsReprocess) {
Emilian Peevaebbe412018-01-15 13:53:24 +0000518 physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_INPUT_STREAMS,
519 &mInputStream.id, 1);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700520 }
521
Emilian Peevaebbe412018-01-15 13:53:24 +0000522 physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_ID,
523 &(submitInfo->mRequestId), /*size*/1);
Jianing Wei90e59c92014-03-12 18:29:36 -0700524 loopCounter++; // loopCounter starts from 1
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800525 ALOGV("%s: Camera %s: Creating request with ID %d (%d of %zu)",
526 __FUNCTION__, mCameraIdStr.string(), submitInfo->mRequestId,
527 loopCounter, requests.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700528
Emilian Peevaebbe412018-01-15 13:53:24 +0000529 metadataRequestList.push_back(physicalSettingsList);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700530 surfaceMapList.push_back(surfaceMap);
Shuzhen Wangd26b1862022-03-07 12:05:05 -0800531
Shuzhen Wang9372b0b2022-05-11 18:55:19 -0700532 // Save certain CaptureRequest settings
Shuzhen Wangd26b1862022-03-07 12:05:05 -0800533 if (!request.mUserTag.empty()) {
534 mUserTag = request.mUserTag;
535 }
Shuzhen Wang9372b0b2022-05-11 18:55:19 -0700536 camera_metadata_entry entry =
537 physicalSettingsList.begin()->metadata.find(
538 ANDROID_CONTROL_VIDEO_STABILIZATION_MODE);
539 if (entry.count == 1) {
540 mVideoStabilizationMode = entry.data.u8[0];
541 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700542 }
543 mRequestIdCounter++;
544
545 if (streaming) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700546 err = mDevice->setStreamingRequestList(metadataRequestList, surfaceMapList,
547 &(submitInfo->mLastFrameNumber));
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800548 if (err != OK) {
549 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800550 "Camera %s: Got error %s (%d) after trying to set streaming request",
551 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800552 ALOGE("%s: %s", __FUNCTION__, msg.string());
553 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
554 msg.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700555 } else {
Shuzhen Wangc9ca6782016-04-26 13:40:31 -0700556 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700557 mStreamingRequestId = submitInfo->mRequestId;
Jianing Wei90e59c92014-03-12 18:29:36 -0700558 }
559 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700560 err = mDevice->captureList(metadataRequestList, surfaceMapList,
561 &(submitInfo->mLastFrameNumber));
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800562 if (err != OK) {
563 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800564 "Camera %s: Got error %s (%d) after trying to submit capture request",
565 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800566 ALOGE("%s: %s", __FUNCTION__, msg.string());
567 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
568 msg.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700569 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800570 ALOGV("%s: requestId = %d ", __FUNCTION__, submitInfo->mRequestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700571 }
572
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800573 ALOGV("%s: Camera %s: End of function", __FUNCTION__, mCameraIdStr.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700574 return res;
575}
576
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800577binder::Status CameraDeviceClient::cancelRequest(
578 int requestId,
579 /*out*/
580 int64_t* lastFrameNumber) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700581 ATRACE_CALL();
582 ALOGV("%s, requestId = %d", __FUNCTION__, requestId);
583
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800584 status_t err;
585 binder::Status res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700586
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800587 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700588
589 Mutex::Autolock icl(mBinderSerializationLock);
590
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800591 if (!mDevice.get()) {
592 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
593 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700594
Shuzhen Wangc9ca6782016-04-26 13:40:31 -0700595 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700596 if (mStreamingRequestId != requestId) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800597 String8 msg = String8::format("Camera %s: Canceling request ID %d doesn't match "
598 "current request ID %d", mCameraIdStr.string(), requestId, mStreamingRequestId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800599 ALOGE("%s: %s", __FUNCTION__, msg.string());
600 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700601 }
602
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800603 err = mDevice->clearStreamingRequest(lastFrameNumber);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700604
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800605 if (err == OK) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800606 ALOGV("%s: Camera %s: Successfully cleared streaming request",
607 __FUNCTION__, mCameraIdStr.string());
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700608 mStreamingRequestId = REQUEST_ID_NONE;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800609 } else {
610 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800611 "Camera %s: Error clearing streaming request: %s (%d)",
612 mCameraIdStr.string(), strerror(-err), err);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700613 }
614
615 return res;
616}
617
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800618binder::Status CameraDeviceClient::beginConfigure() {
Ruben Brunkb2119af2014-05-09 19:57:56 -0700619 // TODO: Implement this.
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -0700620 ATRACE_CALL();
Zhijun He1fa89992015-06-01 15:44:31 -0700621 ALOGV("%s: Not implemented yet.", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800622 return binder::Status::ok();
Ruben Brunkb2119af2014-05-09 19:57:56 -0700623}
624
Emilian Peev5fbe0ba2017-10-20 15:45:45 +0100625binder::Status CameraDeviceClient::endConfigure(int operatingMode,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700626 const hardware::camera2::impl::CameraMetadataNative& sessionParams, int64_t startTimeMs,
Emilian Peevcc0b7952020-01-07 13:54:47 -0800627 std::vector<int>* offlineStreamIds /*out*/) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -0700628 ATRACE_CALL();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700629 ALOGV("%s: ending configure (%d input stream, %zu output surfaces)",
630 __FUNCTION__, mInputStream.configured ? 1 : 0,
631 mStreamMap.size());
Igor Murashkine2d167e2014-08-19 16:19:59 -0700632
Zhijun He0effd522016-03-04 10:22:27 -0800633 binder::Status res;
634 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
635
Emilian Peevcc0b7952020-01-07 13:54:47 -0800636 if (offlineStreamIds == nullptr) {
637 String8 msg = String8::format("Invalid offline stream ids");
638 ALOGE("%s: %s", __FUNCTION__, msg.string());
639 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
640 }
641
Zhijun He0effd522016-03-04 10:22:27 -0800642 Mutex::Autolock icl(mBinderSerializationLock);
643
644 if (!mDevice.get()) {
645 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
646 }
647
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700648 res = SessionConfigurationUtils::checkOperatingMode(operatingMode, mDevice->info(),
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000649 mCameraIdStr);
Emilian Peev35ae8262018-11-08 13:11:32 +0000650 if (!res.isOk()) {
651 return res;
652 }
653
654 status_t err = mDevice->configureStreams(sessionParams, operatingMode);
655 if (err == BAD_VALUE) {
656 String8 msg = String8::format("Camera %s: Unsupported set of inputs/outputs provided",
657 mCameraIdStr.string());
658 ALOGE("%s: %s", __FUNCTION__, msg.string());
659 res = STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
660 } else if (err != OK) {
661 String8 msg = String8::format("Camera %s: Error configuring streams: %s (%d)",
662 mCameraIdStr.string(), strerror(-err), err);
663 ALOGE("%s: %s", __FUNCTION__, msg.string());
664 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Emilian Peev538c90e2018-12-17 18:03:19 +0000665 } else {
Emilian Peevcc0b7952020-01-07 13:54:47 -0800666 offlineStreamIds->clear();
667 mDevice->getOfflineStreamIds(offlineStreamIds);
668
Emilian Peev2f5d6012022-01-19 16:16:50 -0800669 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +0000670 for (size_t i = 0; i < mCompositeStreamMap.size(); ++i) {
671 err = mCompositeStreamMap.valueAt(i)->configureStream();
Emilian Peevcc0b7952020-01-07 13:54:47 -0800672 if (err != OK) {
Emilian Peev538c90e2018-12-17 18:03:19 +0000673 String8 msg = String8::format("Camera %s: Error configuring composite "
674 "streams: %s (%d)", mCameraIdStr.string(), strerror(-err), err);
675 ALOGE("%s: %s", __FUNCTION__, msg.string());
676 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
677 break;
678 }
Emilian Peevcc0b7952020-01-07 13:54:47 -0800679
680 // Composite streams can only support offline mode in case all individual internal
681 // streams are also supported.
682 std::vector<int> internalStreams;
683 mCompositeStreamMap.valueAt(i)->insertCompositeStreamIds(&internalStreams);
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800684 offlineStreamIds->erase(
685 std::remove_if(offlineStreamIds->begin(), offlineStreamIds->end(),
Emilian Peevcc0b7952020-01-07 13:54:47 -0800686 [&internalStreams] (int streamId) {
687 auto it = std::find(internalStreams.begin(), internalStreams.end(),
688 streamId);
689 if (it != internalStreams.end()) {
690 internalStreams.erase(it);
691 return true;
692 }
693
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800694 return false;}), offlineStreamIds->end());
Emilian Peevcc0b7952020-01-07 13:54:47 -0800695 if (internalStreams.empty()) {
696 offlineStreamIds->push_back(mCompositeStreamMap.valueAt(i)->getStreamId());
697 }
698 }
699
700 for (const auto& offlineStreamId : *offlineStreamIds) {
701 mStreamInfoMap[offlineStreamId].supportsOffline = true;
Emilian Peev538c90e2018-12-17 18:03:19 +0000702 }
Shuzhen Wang316781a2020-08-18 18:11:01 -0700703
704 nsecs_t configureEnd = systemTime();
705 int32_t configureDurationMs = ns2ms(configureEnd) - startTimeMs;
Austin Borger74fca042022-05-23 12:41:21 -0700706 mCameraServiceProxyWrapper->logStreamConfigured(mCameraIdStr, operatingMode,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700707 false /*internalReconfig*/, configureDurationMs);
Emilian Peev35ae8262018-11-08 13:11:32 +0000708 }
709
710 return res;
711}
712
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800713binder::Status CameraDeviceClient::isSessionConfigurationSupported(
714 const SessionConfiguration& sessionConfiguration, bool *status /*out*/) {
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800715
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800716 ATRACE_CALL();
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800717 binder::Status res;
718 status_t ret = OK;
719 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
720
721 Mutex::Autolock icl(mBinderSerializationLock);
722
723 if (!mDevice.get()) {
724 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
725 }
726
727 auto operatingMode = sessionConfiguration.getOperatingMode();
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700728 res = SessionConfigurationUtils::checkOperatingMode(operatingMode, mDevice->info(),
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000729 mCameraIdStr);
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800730 if (!res.isOk()) {
731 return res;
732 }
733
734 if (status == nullptr) {
735 String8 msg = String8::format( "Camera %s: Invalid status!", mCameraIdStr.string());
736 ALOGE("%s: %s", __FUNCTION__, msg.string());
737 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
738 }
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700739
Emilian Peev35ae8262018-11-08 13:11:32 +0000740 *status = false;
Shuzhen Wanga79a64d2022-04-24 19:56:30 -0700741 camera3::metadataGetter getMetadata = [this](const String8 &id, bool /*overrideForPerfClass*/) {
742 return mDevice->infoPhysical(id);};
Emilian Peev35ae8262018-11-08 13:11:32 +0000743 ret = mProviderManager->isSessionConfigurationSupported(mCameraIdStr.string(),
Shuzhen Wanga79a64d2022-04-24 19:56:30 -0700744 sessionConfiguration, mOverrideForPerfClass, getMetadata, status);
Emilian Peev35ae8262018-11-08 13:11:32 +0000745 switch (ret) {
746 case OK:
747 // Expected, do nothing.
748 break;
749 case INVALID_OPERATION: {
750 String8 msg = String8::format(
751 "Camera %s: Session configuration query not supported!",
752 mCameraIdStr.string());
753 ALOGD("%s: %s", __FUNCTION__, msg.string());
754 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
755 }
756
757 break;
758 default: {
759 String8 msg = String8::format( "Camera %s: Error: %s (%d)", mCameraIdStr.string(),
760 strerror(-ret), ret);
761 ALOGE("%s: %s", __FUNCTION__, msg.string());
762 res = STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
763 msg.string());
764 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800765 }
766
767 return res;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700768}
769
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800770binder::Status CameraDeviceClient::deleteStream(int streamId) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700771 ATRACE_CALL();
772 ALOGV("%s (streamId = 0x%x)", __FUNCTION__, streamId);
773
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800774 binder::Status res;
775 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700776
777 Mutex::Autolock icl(mBinderSerializationLock);
778
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800779 if (!mDevice.get()) {
780 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
781 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700782
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700783 bool isInput = false;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700784 std::vector<sp<IBinder>> surfaces;
Zhijun He5d677d12016-05-29 16:52:39 -0700785 ssize_t dIndex = NAME_NOT_FOUND;
Emilian Peev538c90e2018-12-17 18:03:19 +0000786 ssize_t compositeIndex = NAME_NOT_FOUND;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700787
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700788 if (mInputStream.configured && mInputStream.id == streamId) {
789 isInput = true;
790 } else {
791 // Guard against trying to delete non-created streams
792 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700793 if (streamId == mStreamMap.valueAt(i).streamId()) {
794 surfaces.push_back(mStreamMap.keyAt(i));
795 }
796 }
797
798 // See if this stream is one of the deferred streams.
799 for (size_t i = 0; i < mDeferredStreams.size(); ++i) {
800 if (streamId == mDeferredStreams[i]) {
801 dIndex = i;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700802 break;
803 }
804 }
805
Emilian Peev2f5d6012022-01-19 16:16:50 -0800806 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +0000807 for (size_t i = 0; i < mCompositeStreamMap.size(); ++i) {
808 if (streamId == mCompositeStreamMap.valueAt(i)->getStreamId()) {
809 compositeIndex = i;
810 break;
811 }
812 }
813
Shuzhen Wang0129d522016-10-30 22:43:41 -0700814 if (surfaces.empty() && dIndex == NAME_NOT_FOUND) {
815 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no such"
816 " stream created yet", mCameraIdStr.string(), streamId);
817 ALOGW("%s: %s", __FUNCTION__, msg.string());
818 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700819 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700820 }
821
822 // Also returns BAD_VALUE if stream ID was not valid
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800823 status_t err = mDevice->deleteStream(streamId);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700824
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800825 if (err != OK) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800826 String8 msg = String8::format("Camera %s: Unexpected error %s (%d) when deleting stream %d",
827 mCameraIdStr.string(), strerror(-err), err, streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800828 ALOGE("%s: %s", __FUNCTION__, msg.string());
829 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
830 } else {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700831 if (isInput) {
832 mInputStream.configured = false;
Zhijun He5d677d12016-05-29 16:52:39 -0700833 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700834 for (auto& surface : surfaces) {
835 mStreamMap.removeItem(surface);
836 }
837
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800838 mConfiguredOutputs.removeItem(streamId);
839
Shuzhen Wang0129d522016-10-30 22:43:41 -0700840 if (dIndex != NAME_NOT_FOUND) {
841 mDeferredStreams.removeItemsAt(dIndex);
842 }
Emilian Peev538c90e2018-12-17 18:03:19 +0000843
844 if (compositeIndex != NAME_NOT_FOUND) {
Emilian Peev2f5d6012022-01-19 16:16:50 -0800845 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +0000846 status_t ret;
847 if ((ret = mCompositeStreamMap.valueAt(compositeIndex)->deleteStream())
848 != OK) {
849 String8 msg = String8::format("Camera %s: Unexpected error %s (%d) when "
850 "deleting composite stream %d", mCameraIdStr.string(), strerror(-err), err,
851 streamId);
852 ALOGE("%s: %s", __FUNCTION__, msg.string());
853 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
854 }
855 mCompositeStreamMap.removeItemsAt(compositeIndex);
856 }
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800857 for (auto &mapIt: mHighResolutionCameraIdToStreamIdSet) {
858 auto &streamSet = mapIt.second;
859 if (streamSet.find(streamId) != streamSet.end()) {
860 streamSet.erase(streamId);
861 break;
862 }
863 }
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700864 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700865 }
866
867 return res;
868}
869
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800870binder::Status CameraDeviceClient::createStream(
871 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
872 /*out*/
873 int32_t* newStreamId) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700874 ATRACE_CALL();
Igor Murashkine7ee7632013-06-11 18:10:18 -0700875
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800876 binder::Status res;
877 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700878
879 Mutex::Autolock icl(mBinderSerializationLock);
880
Shuzhen Wang0129d522016-10-30 22:43:41 -0700881 const std::vector<sp<IGraphicBufferProducer>>& bufferProducers =
882 outputConfiguration.getGraphicBufferProducers();
883 size_t numBufferProducers = bufferProducers.size();
Shuzhen Wang758c2152017-01-10 18:26:18 -0800884 bool deferredConsumer = outputConfiguration.isDeferred();
885 bool isShared = outputConfiguration.isShared();
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800886 String8 physicalCameraId = String8(outputConfiguration.getPhysicalCameraId());
Shuzhen Wang758c2152017-01-10 18:26:18 -0800887 bool deferredConsumerOnly = deferredConsumer && numBufferProducers == 0;
Shuzhen Wang83bff122020-11-20 15:51:39 -0800888 bool isMultiResolution = outputConfiguration.isMultiResolution();
Emilian Peevc81a7592022-02-14 17:38:18 -0800889 int64_t dynamicRangeProfile = outputConfiguration.getDynamicRangeProfile();
Shuzhen Wang8ed1e872022-03-08 16:34:33 -0800890 int64_t streamUseCase = outputConfiguration.getStreamUseCase();
Shuzhen Wange4208922022-02-01 16:52:48 -0800891 int timestampBase = outputConfiguration.getTimestampBase();
Shuzhen Wang610d7b82022-02-08 14:37:22 -0800892 int mirrorMode = outputConfiguration.getMirrorMode();
Austin Borger9e2b27c2022-07-15 11:27:24 -0700893 int32_t colorSpace = outputConfiguration.getColorSpace();
Shuzhen Wangbce53db2022-12-03 00:38:20 +0000894 bool useReadoutTimestamp = outputConfiguration.useReadoutTimestamp();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700895
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700896 res = SessionConfigurationUtils::checkSurfaceType(numBufferProducers, deferredConsumer,
Emilian Peev35ae8262018-11-08 13:11:32 +0000897 outputConfiguration.getSurfaceType());
898 if (!res.isOk()) {
899 return res;
Yin-Chia Yeh89f14da2014-06-10 16:05:44 -0700900 }
Zhijun He5d677d12016-05-29 16:52:39 -0700901
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800902 if (!mDevice.get()) {
903 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
904 }
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700905 res = SessionConfigurationUtils::checkPhysicalCameraId(mPhysicalCameraIds,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800906 physicalCameraId, mCameraIdStr);
Emilian Peev35ae8262018-11-08 13:11:32 +0000907 if (!res.isOk()) {
908 return res;
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800909 }
Emilian Peev35ae8262018-11-08 13:11:32 +0000910
Shuzhen Wang0129d522016-10-30 22:43:41 -0700911 std::vector<sp<Surface>> surfaces;
912 std::vector<sp<IBinder>> binders;
Zhijun He5d677d12016-05-29 16:52:39 -0700913 status_t err;
914
915 // Create stream for deferred surface case.
Shuzhen Wang0129d522016-10-30 22:43:41 -0700916 if (deferredConsumerOnly) {
Shuzhen Wang758c2152017-01-10 18:26:18 -0800917 return createDeferredSurfaceStreamLocked(outputConfiguration, isShared, newStreamId);
Zhijun He5d677d12016-05-29 16:52:39 -0700918 }
919
Shuzhen Wang758c2152017-01-10 18:26:18 -0800920 OutputStreamInfo streamInfo;
921 bool isStreamInfoValid = false;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800922 const std::vector<int32_t> &sensorPixelModesUsed =
923 outputConfiguration.getSensorPixelModesUsed();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700924 for (auto& bufferProducer : bufferProducers) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700925 // Don't create multiple streams for the same target surface
Shuzhen Wang0129d522016-10-30 22:43:41 -0700926 sp<IBinder> binder = IInterface::asBinder(bufferProducer);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800927 ssize_t index = mStreamMap.indexOfKey(binder);
928 if (index != NAME_NOT_FOUND) {
929 String8 msg = String8::format("Camera %s: Surface already has a stream created for it "
930 "(ID %zd)", mCameraIdStr.string(), index);
931 ALOGW("%s: %s", __FUNCTION__, msg.string());
932 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
Shuzhen Wang0129d522016-10-30 22:43:41 -0700933 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700934
Shuzhen Wang758c2152017-01-10 18:26:18 -0800935 sp<Surface> surface;
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700936 res = SessionConfigurationUtils::createSurfaceFromGbp(streamInfo,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800937 isStreamInfoValid, surface, bufferProducer, mCameraIdStr,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -0800938 mDevice->infoPhysical(physicalCameraId), sensorPixelModesUsed, dynamicRangeProfile,
Austin Borger9e2b27c2022-07-15 11:27:24 -0700939 streamUseCase, timestampBase, mirrorMode, colorSpace);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800940
941 if (!res.isOk())
942 return res;
943
944 if (!isStreamInfoValid) {
945 isStreamInfoValid = true;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700946 }
947
Shuzhen Wang758c2152017-01-10 18:26:18 -0800948 binders.push_back(IInterface::asBinder(bufferProducer));
Shuzhen Wang0129d522016-10-30 22:43:41 -0700949 surfaces.push_back(surface);
Ruben Brunkbba75572014-11-20 17:29:50 -0800950 }
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700951
952 // If mOverrideForPerfClass is true, do not fail createStream() for small
953 // JPEG sizes because existing createSurfaceFromGbp() logic will find the
954 // closest possible supported size.
955
Zhijun He125684a2015-12-26 15:07:30 -0800956 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
Emilian Peev40ead602017-09-26 15:46:36 +0100957 std::vector<int> surfaceIds;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800958 bool isDepthCompositeStream =
959 camera3::DepthCompositeStream::isDepthCompositeStream(surfaces[0]);
Chema Gonzalez67de9c82022-12-21 08:59:30 -0800960 bool isHeicCompositeStream = camera3::HeicCompositeStream::isHeicCompositeStream(surfaces[0]);
Emilian Peev434248e2022-10-06 14:58:54 -0700961 bool isJpegRCompositeStream =
Emilian Peeve579d8b2023-02-28 14:16:08 -0800962 camera3::JpegRCompositeStream::isJpegRCompositeStream(surfaces[0]) &&
Emilian Peev15230142023-04-27 20:22:54 +0000963 !mDevice->isCompositeJpegRDisabled();
Chema Gonzalez67de9c82022-12-21 08:59:30 -0800964 if (isDepthCompositeStream || isHeicCompositeStream || isJpegRCompositeStream) {
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800965 sp<CompositeStream> compositeStream;
966 if (isDepthCompositeStream) {
967 compositeStream = new camera3::DepthCompositeStream(mDevice, getRemoteCallback());
Chema Gonzalez67de9c82022-12-21 08:59:30 -0800968 } else if (isHeicCompositeStream) {
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800969 compositeStream = new camera3::HeicCompositeStream(mDevice, getRemoteCallback());
Emilian Peev434248e2022-10-06 14:58:54 -0700970 } else {
971 compositeStream = new camera3::JpegRCompositeStream(mDevice, getRemoteCallback());
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800972 }
973
Emilian Peev538c90e2018-12-17 18:03:19 +0000974 err = compositeStream->createStream(surfaces, deferredConsumer, streamInfo.width,
975 streamInfo.height, streamInfo.format,
Emilian Peevf4816702020-04-03 15:44:51 -0700976 static_cast<camera_stream_rotation_t>(outputConfiguration.getRotation()),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800977 &streamId, physicalCameraId, streamInfo.sensorPixelModesUsed, &surfaceIds,
Emilian Peev434248e2022-10-06 14:58:54 -0700978 outputConfiguration.getSurfaceSetID(), isShared, isMultiResolution,
Shuzhen Wangbce53db2022-12-03 00:38:20 +0000979 streamInfo.colorSpace, streamInfo.dynamicRangeProfile, streamInfo.streamUseCase,
980 useReadoutTimestamp);
Emilian Peev538c90e2018-12-17 18:03:19 +0000981 if (err == OK) {
Emilian Peev2f5d6012022-01-19 16:16:50 -0800982 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +0000983 mCompositeStreamMap.add(IInterface::asBinder(surfaces[0]->getIGraphicBufferProducer()),
984 compositeStream);
985 }
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800986 } else {
987 err = mDevice->createStream(surfaces, deferredConsumer, streamInfo.width,
988 streamInfo.height, streamInfo.format, streamInfo.dataSpace,
Emilian Peevf4816702020-04-03 15:44:51 -0700989 static_cast<camera_stream_rotation_t>(outputConfiguration.getRotation()),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800990 &streamId, physicalCameraId, streamInfo.sensorPixelModesUsed, &surfaceIds,
Emilian Peev2295df72021-11-12 18:14:10 -0800991 outputConfiguration.getSurfaceSetID(), isShared, isMultiResolution,
Shuzhen Wange4208922022-02-01 16:52:48 -0800992 /*consumerUsage*/0, streamInfo.dynamicRangeProfile, streamInfo.streamUseCase,
Shuzhen Wangbce53db2022-12-03 00:38:20 +0000993 streamInfo.timestampBase, streamInfo.mirrorMode, streamInfo.colorSpace,
994 useReadoutTimestamp);
Emilian Peev538c90e2018-12-17 18:03:19 +0000995 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700996
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800997 if (err != OK) {
998 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800999 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
Shuzhen Wang758c2152017-01-10 18:26:18 -08001000 mCameraIdStr.string(), streamInfo.width, streamInfo.height, streamInfo.format,
1001 streamInfo.dataSpace, strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001002 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001003 int i = 0;
1004 for (auto& binder : binders) {
1005 ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %d",
1006 __FUNCTION__, binder.get(), streamId, i);
Emilian Peev40ead602017-09-26 15:46:36 +01001007 mStreamMap.add(binder, StreamSurfaceId(streamId, surfaceIds[i]));
1008 i++;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001009 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001010
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -08001011 mConfiguredOutputs.add(streamId, outputConfiguration);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001012 mStreamInfoMap[streamId] = streamInfo;
1013
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001014 ALOGV("%s: Camera %s: Successfully created a new stream ID %d for output surface"
Shuzhen Wang0129d522016-10-30 22:43:41 -07001015 " (%d x %d) with format 0x%x.",
Shuzhen Wang758c2152017-01-10 18:26:18 -08001016 __FUNCTION__, mCameraIdStr.string(), streamId, streamInfo.width,
1017 streamInfo.height, streamInfo.format);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001018
Zhijun He5d677d12016-05-29 16:52:39 -07001019 // Set transform flags to ensure preview to be rotated correctly.
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001020 res = setStreamTransformLocked(streamId, streamInfo.mirrorMode);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001021
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001022 // Fill in mHighResolutionCameraIdToStreamIdSet map
1023 const String8 &cameraIdUsed =
1024 physicalCameraId.size() != 0 ? physicalCameraId : mCameraIdStr;
1025 const char *cameraIdUsedCStr = cameraIdUsed.string();
1026 // Only needed for high resolution sensors
1027 if (mHighResolutionSensors.find(cameraIdUsedCStr) !=
1028 mHighResolutionSensors.end()) {
1029 mHighResolutionCameraIdToStreamIdSet[cameraIdUsedCStr].insert(streamId);
1030 }
1031
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001032 *newStreamId = streamId;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001033 }
1034
1035 return res;
1036}
1037
Zhijun He5d677d12016-05-29 16:52:39 -07001038binder::Status CameraDeviceClient::createDeferredSurfaceStreamLocked(
1039 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
Shuzhen Wang758c2152017-01-10 18:26:18 -08001040 bool isShared,
Zhijun He5d677d12016-05-29 16:52:39 -07001041 /*out*/
1042 int* newStreamId) {
1043 int width, height, format, surfaceType;
Emilian Peev050f5dc2017-05-18 14:43:56 +01001044 uint64_t consumerUsage;
Zhijun He5d677d12016-05-29 16:52:39 -07001045 android_dataspace dataSpace;
Austin Borger9e2b27c2022-07-15 11:27:24 -07001046 int32_t colorSpace;
Zhijun He5d677d12016-05-29 16:52:39 -07001047 status_t err;
1048 binder::Status res;
1049
1050 if (!mDevice.get()) {
1051 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1052 }
1053
1054 // Infer the surface info for deferred surface stream creation.
1055 width = outputConfiguration.getWidth();
1056 height = outputConfiguration.getHeight();
1057 surfaceType = outputConfiguration.getSurfaceType();
1058 format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
1059 dataSpace = android_dataspace_t::HAL_DATASPACE_UNKNOWN;
Austin Borger9e2b27c2022-07-15 11:27:24 -07001060 colorSpace = ANDROID_REQUEST_AVAILABLE_COLOR_SPACE_PROFILES_MAP_UNSPECIFIED;
Zhijun He5d677d12016-05-29 16:52:39 -07001061 // Hardcode consumer usage flags: SurfaceView--0x900, SurfaceTexture--0x100.
1062 consumerUsage = GraphicBuffer::USAGE_HW_TEXTURE;
1063 if (surfaceType == OutputConfiguration::SURFACE_TYPE_SURFACE_VIEW) {
1064 consumerUsage |= GraphicBuffer::USAGE_HW_COMPOSER;
1065 }
1066 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001067 std::vector<sp<Surface>> noSurface;
Emilian Peev40ead602017-09-26 15:46:36 +01001068 std::vector<int> surfaceIds;
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001069 String8 physicalCameraId(outputConfiguration.getPhysicalCameraId());
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001070 const String8 &cameraIdUsed =
1071 physicalCameraId.size() != 0 ? physicalCameraId : mCameraIdStr;
1072 // Here, we override sensor pixel modes
1073 std::unordered_set<int32_t> overriddenSensorPixelModesUsed;
1074 const std::vector<int32_t> &sensorPixelModesUsed =
1075 outputConfiguration.getSensorPixelModesUsed();
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07001076 if (SessionConfigurationUtils::checkAndOverrideSensorPixelModesUsed(
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001077 sensorPixelModesUsed, format, width, height, getStaticInfo(cameraIdUsed),
Jayant Chowdharya80ad242023-03-17 00:49:47 +00001078 &overriddenSensorPixelModesUsed) != OK) {
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001079 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1080 "sensor pixel modes used not valid for deferred stream");
1081 }
1082
Shuzhen Wang0129d522016-10-30 22:43:41 -07001083 err = mDevice->createStream(noSurface, /*hasDeferredConsumer*/true, width,
1084 height, format, dataSpace,
Emilian Peevf4816702020-04-03 15:44:51 -07001085 static_cast<camera_stream_rotation_t>(outputConfiguration.getRotation()),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001086 &streamId, physicalCameraId,
1087 overriddenSensorPixelModesUsed,
1088 &surfaceIds,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001089 outputConfiguration.getSurfaceSetID(), isShared,
Emilian Peev2295df72021-11-12 18:14:10 -08001090 outputConfiguration.isMultiResolution(), consumerUsage,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001091 outputConfiguration.getDynamicRangeProfile(),
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001092 outputConfiguration.getStreamUseCase(),
Shuzhen Wangbce53db2022-12-03 00:38:20 +00001093 outputConfiguration.getMirrorMode(),
1094 outputConfiguration.useReadoutTimestamp());
Zhijun He5d677d12016-05-29 16:52:39 -07001095
1096 if (err != OK) {
1097 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001098 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
1099 mCameraIdStr.string(), width, height, format, dataSpace, strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001100 } else {
1101 // Can not add streamId to mStreamMap here, as the surface is deferred. Add it to
1102 // a separate list to track. Once the deferred surface is set, this id will be
1103 // relocated to mStreamMap.
1104 mDeferredStreams.push_back(streamId);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001105 mStreamInfoMap.emplace(std::piecewise_construct, std::forward_as_tuple(streamId),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001106 std::forward_as_tuple(width, height, format, dataSpace, consumerUsage,
Emilian Peev2295df72021-11-12 18:14:10 -08001107 overriddenSensorPixelModesUsed,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001108 outputConfiguration.getDynamicRangeProfile(),
Shuzhen Wange4208922022-02-01 16:52:48 -08001109 outputConfiguration.getStreamUseCase(),
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001110 outputConfiguration.getTimestampBase(),
Austin Borger9e2b27c2022-07-15 11:27:24 -07001111 outputConfiguration.getMirrorMode(),
1112 colorSpace));
Shuzhen Wang758c2152017-01-10 18:26:18 -08001113
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001114 ALOGV("%s: Camera %s: Successfully created a new stream ID %d for a deferred surface"
Zhijun He5d677d12016-05-29 16:52:39 -07001115 " (%d x %d) stream with format 0x%x.",
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001116 __FUNCTION__, mCameraIdStr.string(), streamId, width, height, format);
Zhijun He5d677d12016-05-29 16:52:39 -07001117
1118 // Set transform flags to ensure preview to be rotated correctly.
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001119 res = setStreamTransformLocked(streamId, outputConfiguration.getMirrorMode());
Zhijun He5d677d12016-05-29 16:52:39 -07001120
1121 *newStreamId = streamId;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001122 // Fill in mHighResolutionCameraIdToStreamIdSet
1123 const char *cameraIdUsedCStr = cameraIdUsed.string();
1124 // Only needed for high resolution sensors
1125 if (mHighResolutionSensors.find(cameraIdUsedCStr) !=
1126 mHighResolutionSensors.end()) {
1127 mHighResolutionCameraIdToStreamIdSet[cameraIdUsed.string()].insert(streamId);
1128 }
Zhijun He5d677d12016-05-29 16:52:39 -07001129 }
1130 return res;
1131}
1132
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001133binder::Status CameraDeviceClient::setStreamTransformLocked(int streamId, int mirrorMode) {
Zhijun He5d677d12016-05-29 16:52:39 -07001134 int32_t transform = 0;
1135 status_t err;
1136 binder::Status res;
1137
1138 if (!mDevice.get()) {
1139 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1140 }
1141
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001142 err = getRotationTransformLocked(mirrorMode, &transform);
Zhijun He5d677d12016-05-29 16:52:39 -07001143 if (err != OK) {
1144 // Error logged by getRotationTransformLocked.
1145 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
1146 "Unable to calculate rotation transform for new stream");
1147 }
1148
1149 err = mDevice->setStreamTransform(streamId, transform);
1150 if (err != OK) {
1151 String8 msg = String8::format("Failed to set stream transform (stream id %d)",
1152 streamId);
1153 ALOGE("%s: %s", __FUNCTION__, msg.string());
1154 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
1155 }
1156
1157 return res;
1158}
Ruben Brunkbba75572014-11-20 17:29:50 -08001159
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001160binder::Status CameraDeviceClient::createInputStream(
Shuzhen Wang83bff122020-11-20 15:51:39 -08001161 int width, int height, int format, bool isMultiResolution,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001162 /*out*/
1163 int32_t* newStreamId) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001164
1165 ATRACE_CALL();
Shuzhen Wang83bff122020-11-20 15:51:39 -08001166 ALOGV("%s (w = %d, h = %d, f = 0x%x, isMultiResolution %d)", __FUNCTION__,
1167 width, height, format, isMultiResolution);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001168
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001169 binder::Status res;
1170 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001171
1172 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001173
1174 if (!mDevice.get()) {
1175 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1176 }
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001177
1178 if (mInputStream.configured) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001179 String8 msg = String8::format("Camera %s: Already has an input stream "
Yi Kong0f414de2017-12-15 13:48:50 -08001180 "configured (ID %d)", mCameraIdStr.string(), mInputStream.id);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001181 ALOGE("%s: %s", __FUNCTION__, msg.string() );
1182 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001183 }
1184
1185 int streamId = -1;
Shuzhen Wang83bff122020-11-20 15:51:39 -08001186 status_t err = mDevice->createInputStream(width, height, format, isMultiResolution, &streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001187 if (err == OK) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001188 mInputStream.configured = true;
1189 mInputStream.width = width;
1190 mInputStream.height = height;
1191 mInputStream.format = format;
1192 mInputStream.id = streamId;
1193
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001194 ALOGV("%s: Camera %s: Successfully created a new input stream ID %d",
1195 __FUNCTION__, mCameraIdStr.string(), streamId);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001196
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001197 *newStreamId = streamId;
1198 } else {
1199 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001200 "Camera %s: Error creating new input stream: %s (%d)", mCameraIdStr.string(),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001201 strerror(-err), err);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001202 }
1203
1204 return res;
1205}
1206
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001207binder::Status CameraDeviceClient::getInputSurface(/*out*/ view::Surface *inputSurface) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001208
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001209 binder::Status res;
1210 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1211
1212 if (inputSurface == NULL) {
1213 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Null input surface");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001214 }
1215
1216 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001217 if (!mDevice.get()) {
1218 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1219 }
1220 sp<IGraphicBufferProducer> producer;
1221 status_t err = mDevice->getInputBufferProducer(&producer);
1222 if (err != OK) {
1223 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001224 "Camera %s: Error getting input Surface: %s (%d)",
1225 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001226 } else {
1227 inputSurface->name = String16("CameraInput");
1228 inputSurface->graphicBufferProducer = producer;
1229 }
1230 return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001231}
1232
Emilian Peev40ead602017-09-26 15:46:36 +01001233binder::Status CameraDeviceClient::updateOutputConfiguration(int streamId,
1234 const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
1235 ATRACE_CALL();
1236
1237 binder::Status res;
1238 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1239
1240 Mutex::Autolock icl(mBinderSerializationLock);
1241
1242 if (!mDevice.get()) {
1243 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1244 }
1245
1246 const std::vector<sp<IGraphicBufferProducer> >& bufferProducers =
1247 outputConfiguration.getGraphicBufferProducers();
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -07001248 String8 physicalCameraId(outputConfiguration.getPhysicalCameraId());
1249
Emilian Peev40ead602017-09-26 15:46:36 +01001250 auto producerCount = bufferProducers.size();
1251 if (producerCount == 0) {
1252 ALOGE("%s: bufferProducers must not be empty", __FUNCTION__);
1253 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1254 "bufferProducers must not be empty");
1255 }
1256
1257 // The first output is the one associated with the output configuration.
1258 // It should always be present, valid and the corresponding stream id should match.
1259 sp<IBinder> binder = IInterface::asBinder(bufferProducers[0]);
1260 ssize_t index = mStreamMap.indexOfKey(binder);
1261 if (index == NAME_NOT_FOUND) {
1262 ALOGE("%s: Outputconfiguration is invalid", __FUNCTION__);
1263 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1264 "OutputConfiguration is invalid");
1265 }
1266 if (mStreamMap.valueFor(binder).streamId() != streamId) {
1267 ALOGE("%s: Stream Id: %d provided doesn't match the id: %d in the stream map",
1268 __FUNCTION__, streamId, mStreamMap.valueFor(binder).streamId());
1269 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1270 "Stream id is invalid");
1271 }
1272
1273 std::vector<size_t> removedSurfaceIds;
1274 std::vector<sp<IBinder>> removedOutputs;
1275 std::vector<sp<Surface>> newOutputs;
1276 std::vector<OutputStreamInfo> streamInfos;
1277 KeyedVector<sp<IBinder>, sp<IGraphicBufferProducer>> newOutputsMap;
1278 for (auto &it : bufferProducers) {
1279 newOutputsMap.add(IInterface::asBinder(it), it);
1280 }
1281
1282 for (size_t i = 0; i < mStreamMap.size(); i++) {
1283 ssize_t idx = newOutputsMap.indexOfKey(mStreamMap.keyAt(i));
1284 if (idx == NAME_NOT_FOUND) {
1285 if (mStreamMap[i].streamId() == streamId) {
1286 removedSurfaceIds.push_back(mStreamMap[i].surfaceId());
1287 removedOutputs.push_back(mStreamMap.keyAt(i));
1288 }
1289 } else {
1290 if (mStreamMap[i].streamId() != streamId) {
1291 ALOGE("%s: Output surface already part of a different stream", __FUNCTION__);
1292 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1293 "Target Surface is invalid");
1294 }
1295 newOutputsMap.removeItemsAt(idx);
1296 }
1297 }
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001298 const std::vector<int32_t> &sensorPixelModesUsed =
1299 outputConfiguration.getSensorPixelModesUsed();
Shuzhen Wang8ed1e872022-03-08 16:34:33 -08001300 int64_t streamUseCase = outputConfiguration.getStreamUseCase();
Shuzhen Wange4208922022-02-01 16:52:48 -08001301 int timestampBase = outputConfiguration.getTimestampBase();
Emilian Peevc81a7592022-02-14 17:38:18 -08001302 int64_t dynamicRangeProfile = outputConfiguration.getDynamicRangeProfile();
Austin Borger9e2b27c2022-07-15 11:27:24 -07001303 int32_t colorSpace = outputConfiguration.getColorSpace();
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001304 int mirrorMode = outputConfiguration.getMirrorMode();
Emilian Peev2295df72021-11-12 18:14:10 -08001305
Emilian Peev40ead602017-09-26 15:46:36 +01001306 for (size_t i = 0; i < newOutputsMap.size(); i++) {
1307 OutputStreamInfo outInfo;
1308 sp<Surface> surface;
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07001309 res = SessionConfigurationUtils::createSurfaceFromGbp(outInfo,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001310 /*isStreamInfoValid*/ false, surface, newOutputsMap.valueAt(i), mCameraIdStr,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001311 mDevice->infoPhysical(physicalCameraId), sensorPixelModesUsed, dynamicRangeProfile,
Austin Borger9e2b27c2022-07-15 11:27:24 -07001312 streamUseCase, timestampBase, mirrorMode, colorSpace);
Emilian Peev40ead602017-09-26 15:46:36 +01001313 if (!res.isOk())
1314 return res;
1315
Emilian Peev40ead602017-09-26 15:46:36 +01001316 streamInfos.push_back(outInfo);
1317 newOutputs.push_back(surface);
1318 }
1319
1320 //Trivial case no changes required
1321 if (removedSurfaceIds.empty() && newOutputs.empty()) {
1322 return binder::Status::ok();
1323 }
1324
1325 KeyedVector<sp<Surface>, size_t> outputMap;
1326 auto ret = mDevice->updateStream(streamId, newOutputs, streamInfos, removedSurfaceIds,
1327 &outputMap);
1328 if (ret != OK) {
1329 switch (ret) {
1330 case NAME_NOT_FOUND:
1331 case BAD_VALUE:
1332 case -EBUSY:
1333 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1334 "Camera %s: Error updating stream: %s (%d)",
1335 mCameraIdStr.string(), strerror(ret), ret);
1336 break;
1337 default:
1338 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1339 "Camera %s: Error updating stream: %s (%d)",
1340 mCameraIdStr.string(), strerror(ret), ret);
1341 break;
1342 }
1343 } else {
1344 for (const auto &it : removedOutputs) {
1345 mStreamMap.removeItem(it);
1346 }
1347
1348 for (size_t i = 0; i < outputMap.size(); i++) {
1349 mStreamMap.add(IInterface::asBinder(outputMap.keyAt(i)->getIGraphicBufferProducer()),
1350 StreamSurfaceId(streamId, outputMap.valueAt(i)));
1351 }
1352
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -08001353 mConfiguredOutputs.replaceValueFor(streamId, outputConfiguration);
1354
Emilian Peev40ead602017-09-26 15:46:36 +01001355 ALOGV("%s: Camera %s: Successful stream ID %d update",
1356 __FUNCTION__, mCameraIdStr.string(), streamId);
1357 }
1358
1359 return res;
1360}
1361
Igor Murashkine7ee7632013-06-11 18:10:18 -07001362// Create a request object from a template.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001363binder::Status CameraDeviceClient::createDefaultRequest(int templateId,
1364 /*out*/
1365 hardware::camera2::impl::CameraMetadataNative* request)
Igor Murashkine7ee7632013-06-11 18:10:18 -07001366{
1367 ATRACE_CALL();
1368 ALOGV("%s (templateId = 0x%x)", __FUNCTION__, templateId);
1369
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001370 binder::Status res;
1371 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001372
1373 Mutex::Autolock icl(mBinderSerializationLock);
1374
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001375 if (!mDevice.get()) {
1376 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1377 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001378
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001379 status_t err;
Emilian Peevf4816702020-04-03 15:44:51 -07001380 camera_request_template_t tempId = camera_request_template_t::CAMERA_TEMPLATE_COUNT;
1381 if (!(res = mapRequestTemplate(templateId, &tempId)).isOk()) return res;
1382
1383 CameraMetadata metadata;
1384 if ( (err = mDevice->createDefaultRequest(tempId, &metadata) ) == OK &&
Igor Murashkine7ee7632013-06-11 18:10:18 -07001385 request != NULL) {
1386
1387 request->swap(metadata);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001388 } else if (err == BAD_VALUE) {
1389 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001390 "Camera %s: Template ID %d is invalid or not supported: %s (%d)",
1391 mCameraIdStr.string(), templateId, strerror(-err), err);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001392
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001393 } else {
1394 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001395 "Camera %s: Error creating default request for template %d: %s (%d)",
1396 mCameraIdStr.string(), templateId, strerror(-err), err);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001397 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001398 return res;
1399}
1400
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001401binder::Status CameraDeviceClient::getCameraInfo(
1402 /*out*/
1403 hardware::camera2::impl::CameraMetadataNative* info)
Igor Murashkine7ee7632013-06-11 18:10:18 -07001404{
1405 ATRACE_CALL();
1406 ALOGV("%s", __FUNCTION__);
1407
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001408 binder::Status res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001409
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001410 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001411
1412 Mutex::Autolock icl(mBinderSerializationLock);
1413
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001414 if (!mDevice.get()) {
1415 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1416 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001417
Igor Murashkin099b4572013-07-12 17:52:16 -07001418 if (info != NULL) {
1419 *info = mDevice->info(); // static camera metadata
1420 // TODO: merge with device-specific camera metadata
1421 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001422
1423 return res;
1424}
1425
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001426binder::Status CameraDeviceClient::waitUntilIdle()
Zhijun He2ab500c2013-07-23 08:02:53 -07001427{
1428 ATRACE_CALL();
1429 ALOGV("%s", __FUNCTION__);
1430
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001431 binder::Status res;
1432 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Zhijun He2ab500c2013-07-23 08:02:53 -07001433
1434 Mutex::Autolock icl(mBinderSerializationLock);
1435
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001436 if (!mDevice.get()) {
1437 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1438 }
Zhijun He2ab500c2013-07-23 08:02:53 -07001439
1440 // FIXME: Also need check repeating burst.
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001441 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001442 if (mStreamingRequestId != REQUEST_ID_NONE) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001443 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001444 "Camera %s: Try to waitUntilIdle when there are active streaming requests",
1445 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001446 ALOGE("%s: %s", __FUNCTION__, msg.string());
1447 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Zhijun He2ab500c2013-07-23 08:02:53 -07001448 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001449 status_t err = mDevice->waitUntilDrained();
1450 if (err != OK) {
1451 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001452 "Camera %s: Error waiting to drain: %s (%d)",
1453 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001454 }
Zhijun He2ab500c2013-07-23 08:02:53 -07001455 ALOGV("%s Done", __FUNCTION__);
Zhijun He2ab500c2013-07-23 08:02:53 -07001456 return res;
1457}
1458
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001459binder::Status CameraDeviceClient::flush(
1460 /*out*/
1461 int64_t* lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -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 Talvalaabaa51d2013-08-14 11:37:00 -07001467
1468 Mutex::Autolock icl(mBinderSerializationLock);
1469
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001470 if (!mDevice.get()) {
1471 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1472 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001473
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001474 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001475 mStreamingRequestId = REQUEST_ID_NONE;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001476 status_t err = mDevice->flush(lastFrameNumber);
1477 if (err != OK) {
1478 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001479 "Camera %s: Error flushing device: %s (%d)", mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001480 }
1481 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001482}
1483
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001484binder::Status CameraDeviceClient::prepare(int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001485 ATRACE_CALL();
Jayant Chowdhary57184d52023-02-14 20:54:39 +00001486 ALOGV("%s stream id %d", __FUNCTION__, streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001487
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001488 binder::Status res;
1489 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001490
1491 Mutex::Autolock icl(mBinderSerializationLock);
1492
1493 // Guard against trying to prepare non-created streams
1494 ssize_t index = NAME_NOT_FOUND;
1495 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001496 if (streamId == mStreamMap.valueAt(i).streamId()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001497 index = i;
1498 break;
1499 }
1500 }
1501
1502 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001503 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1504 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001505 ALOGW("%s: %s", __FUNCTION__, msg.string());
1506 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001507 }
1508
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001509 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1510 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001511 status_t err = mDevice->prepare(streamId);
1512 if (err == BAD_VALUE) {
1513 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001514 "Camera %s: Stream %d has already been used, and cannot be prepared",
1515 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001516 } else if (err != OK) {
1517 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001518 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001519 strerror(-err), err);
1520 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001521 return res;
1522}
1523
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001524binder::Status CameraDeviceClient::prepare2(int maxCount, int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001525 ATRACE_CALL();
Jayant Chowdhary57184d52023-02-14 20:54:39 +00001526 ALOGV("%s stream id %d", __FUNCTION__, streamId);
Ruben Brunkc78ac262015-08-13 17:58:46 -07001527
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001528 binder::Status res;
1529 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Ruben Brunkc78ac262015-08-13 17:58:46 -07001530
1531 Mutex::Autolock icl(mBinderSerializationLock);
1532
1533 // Guard against trying to prepare non-created streams
1534 ssize_t index = NAME_NOT_FOUND;
1535 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001536 if (streamId == mStreamMap.valueAt(i).streamId()) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001537 index = i;
1538 break;
1539 }
1540 }
1541
1542 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001543 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1544 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001545 ALOGW("%s: %s", __FUNCTION__, msg.string());
1546 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -07001547 }
1548
1549 if (maxCount <= 0) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001550 String8 msg = String8::format("Camera %s: maxCount (%d) must be greater than 0",
1551 mCameraIdStr.string(), maxCount);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001552 ALOGE("%s: %s", __FUNCTION__, msg.string());
1553 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -07001554 }
1555
1556 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1557 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001558 status_t err = mDevice->prepare(maxCount, streamId);
1559 if (err == BAD_VALUE) {
1560 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001561 "Camera %s: Stream %d has already been used, and cannot be prepared",
1562 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001563 } else if (err != OK) {
1564 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001565 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001566 strerror(-err), err);
1567 }
Ruben Brunkc78ac262015-08-13 17:58:46 -07001568
1569 return res;
1570}
1571
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001572binder::Status CameraDeviceClient::tearDown(int streamId) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001573 ATRACE_CALL();
1574 ALOGV("%s", __FUNCTION__);
1575
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001576 binder::Status res;
1577 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001578
1579 Mutex::Autolock icl(mBinderSerializationLock);
1580
1581 // Guard against trying to prepare non-created streams
1582 ssize_t index = NAME_NOT_FOUND;
1583 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001584 if (streamId == mStreamMap.valueAt(i).streamId()) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001585 index = i;
1586 break;
1587 }
1588 }
1589
1590 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001591 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1592 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001593 ALOGW("%s: %s", __FUNCTION__, msg.string());
1594 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001595 }
1596
1597 // Also returns BAD_VALUE if stream ID was not valid or if the stream is in
1598 // use
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001599 status_t err = mDevice->tearDown(streamId);
1600 if (err == BAD_VALUE) {
1601 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001602 "Camera %s: Stream %d is still in use, cannot be torn down",
1603 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001604 } else if (err != OK) {
1605 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001606 "Camera %s: Error tearing down stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001607 strerror(-err), err);
1608 }
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001609
1610 return res;
1611}
1612
Shuzhen Wang758c2152017-01-10 18:26:18 -08001613binder::Status CameraDeviceClient::finalizeOutputConfigurations(int32_t streamId,
Zhijun He5d677d12016-05-29 16:52:39 -07001614 const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
1615 ATRACE_CALL();
1616
1617 binder::Status res;
1618 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1619
1620 Mutex::Autolock icl(mBinderSerializationLock);
1621
Shuzhen Wang0129d522016-10-30 22:43:41 -07001622 const std::vector<sp<IGraphicBufferProducer> >& bufferProducers =
1623 outputConfiguration.getGraphicBufferProducers();
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -07001624 String8 physicalId(outputConfiguration.getPhysicalCameraId());
Zhijun He5d677d12016-05-29 16:52:39 -07001625
Shuzhen Wang0129d522016-10-30 22:43:41 -07001626 if (bufferProducers.size() == 0) {
1627 ALOGE("%s: bufferProducers must not be empty", __FUNCTION__);
Zhijun He5d677d12016-05-29 16:52:39 -07001628 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Target Surface is invalid");
1629 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001630
Shuzhen Wang758c2152017-01-10 18:26:18 -08001631 // streamId should be in mStreamMap if this stream already has a surface attached
1632 // to it. Otherwise, it should be in mDeferredStreams.
1633 bool streamIdConfigured = false;
1634 ssize_t deferredStreamIndex = NAME_NOT_FOUND;
1635 for (size_t i = 0; i < mStreamMap.size(); i++) {
1636 if (mStreamMap.valueAt(i).streamId() == streamId) {
1637 streamIdConfigured = true;
1638 break;
1639 }
Zhijun He5d677d12016-05-29 16:52:39 -07001640 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001641 for (size_t i = 0; i < mDeferredStreams.size(); i++) {
1642 if (streamId == mDeferredStreams[i]) {
1643 deferredStreamIndex = i;
1644 break;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001645 }
1646
Shuzhen Wang758c2152017-01-10 18:26:18 -08001647 }
1648 if (deferredStreamIndex == NAME_NOT_FOUND && !streamIdConfigured) {
1649 String8 msg = String8::format("Camera %s: deferred surface is set to a unknown stream"
1650 "(ID %d)", mCameraIdStr.string(), streamId);
1651 ALOGW("%s: %s", __FUNCTION__, msg.string());
1652 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Zhijun He5d677d12016-05-29 16:52:39 -07001653 }
1654
Shuzhen Wang88fd0052017-02-09 16:40:07 -08001655 if (mStreamInfoMap[streamId].finalized) {
1656 String8 msg = String8::format("Camera %s: finalizeOutputConfigurations has been called"
1657 " on stream ID %d", mCameraIdStr.string(), streamId);
1658 ALOGW("%s: %s", __FUNCTION__, msg.string());
1659 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1660 }
1661
Zhijun He5d677d12016-05-29 16:52:39 -07001662 if (!mDevice.get()) {
1663 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1664 }
1665
Shuzhen Wang758c2152017-01-10 18:26:18 -08001666 std::vector<sp<Surface>> consumerSurfaces;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001667 const std::vector<int32_t> &sensorPixelModesUsed =
1668 outputConfiguration.getSensorPixelModesUsed();
Emilian Peevc81a7592022-02-14 17:38:18 -08001669 int64_t dynamicRangeProfile = outputConfiguration.getDynamicRangeProfile();
Austin Borger9e2b27c2022-07-15 11:27:24 -07001670 int32_t colorSpace = outputConfiguration.getColorSpace();
1671 int64_t streamUseCase = outputConfiguration.getStreamUseCase();
Shuzhen Wange4208922022-02-01 16:52:48 -08001672 int timestampBase = outputConfiguration.getTimestampBase();
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001673 int mirrorMode = outputConfiguration.getMirrorMode();
Shuzhen Wang758c2152017-01-10 18:26:18 -08001674 for (auto& bufferProducer : bufferProducers) {
1675 // Don't create multiple streams for the same target surface
Zhijun He5d677d12016-05-29 16:52:39 -07001676 ssize_t index = mStreamMap.indexOfKey(IInterface::asBinder(bufferProducer));
1677 if (index != NAME_NOT_FOUND) {
Shuzhen Wang758c2152017-01-10 18:26:18 -08001678 ALOGV("Camera %s: Surface already has a stream created "
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001679 " for it (ID %zd)", mCameraIdStr.string(), index);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001680 continue;
Zhijun He5d677d12016-05-29 16:52:39 -07001681 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001682
1683 sp<Surface> surface;
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07001684 res = SessionConfigurationUtils::createSurfaceFromGbp(mStreamInfoMap[streamId],
Colin Crossb8a9dbb2020-08-27 04:12:26 +00001685 true /*isStreamInfoValid*/, surface, bufferProducer, mCameraIdStr,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001686 mDevice->infoPhysical(physicalId), sensorPixelModesUsed, dynamicRangeProfile,
Austin Borger9e2b27c2022-07-15 11:27:24 -07001687 streamUseCase, timestampBase, mirrorMode, colorSpace);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001688
1689 if (!res.isOk())
1690 return res;
1691
1692 consumerSurfaces.push_back(surface);
Zhijun He5d677d12016-05-29 16:52:39 -07001693 }
1694
Shuzhen Wanga81ce342017-04-13 15:18:36 -07001695 // Gracefully handle case where finalizeOutputConfigurations is called
1696 // without any new surface.
1697 if (consumerSurfaces.size() == 0) {
1698 mStreamInfoMap[streamId].finalized = true;
1699 return res;
1700 }
1701
Zhijun He5d677d12016-05-29 16:52:39 -07001702 // Finish the deferred stream configuration with the surface.
Shuzhen Wang758c2152017-01-10 18:26:18 -08001703 status_t err;
Emilian Peev40ead602017-09-26 15:46:36 +01001704 std::vector<int> consumerSurfaceIds;
1705 err = mDevice->setConsumerSurfaces(streamId, consumerSurfaces, &consumerSurfaceIds);
Zhijun He5d677d12016-05-29 16:52:39 -07001706 if (err == OK) {
Shuzhen Wang758c2152017-01-10 18:26:18 -08001707 for (size_t i = 0; i < consumerSurfaces.size(); i++) {
1708 sp<IBinder> binder = IInterface::asBinder(
1709 consumerSurfaces[i]->getIGraphicBufferProducer());
Emilian Peev40ead602017-09-26 15:46:36 +01001710 ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %d", __FUNCTION__,
Shuzhen Wang758c2152017-01-10 18:26:18 -08001711 binder.get(), streamId, consumerSurfaceIds[i]);
1712 mStreamMap.add(binder, StreamSurfaceId(streamId, consumerSurfaceIds[i]));
1713 }
1714 if (deferredStreamIndex != NAME_NOT_FOUND) {
1715 mDeferredStreams.removeItemsAt(deferredStreamIndex);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001716 }
Shuzhen Wang88fd0052017-02-09 16:40:07 -08001717 mStreamInfoMap[streamId].finalized = true;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -08001718 mConfiguredOutputs.replaceValueFor(streamId, outputConfiguration);
Zhijun He5d677d12016-05-29 16:52:39 -07001719 } else if (err == NO_INIT) {
1720 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001721 "Camera %s: Deferred surface is invalid: %s (%d)",
1722 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001723 } else {
1724 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001725 "Camera %s: Error setting output stream deferred surface: %s (%d)",
1726 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001727 }
1728
1729 return res;
1730}
1731
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001732binder::Status CameraDeviceClient::setCameraAudioRestriction(int32_t mode) {
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001733 ATRACE_CALL();
1734 binder::Status res;
1735 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1736
1737 if (!isValidAudioRestriction(mode)) {
1738 String8 msg = String8::format("Camera %s: invalid audio restriction mode %d",
1739 mCameraIdStr.string(), mode);
1740 ALOGW("%s: %s", __FUNCTION__, msg.string());
1741 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1742 }
1743
1744 Mutex::Autolock icl(mBinderSerializationLock);
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001745 BasicClient::setAudioRestriction(mode);
1746 return binder::Status::ok();
1747}
1748
1749binder::Status CameraDeviceClient::getGlobalAudioRestriction(/*out*/ int32_t* outMode) {
1750 ATRACE_CALL();
1751 binder::Status res;
1752 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1753 Mutex::Autolock icl(mBinderSerializationLock);
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001754 if (outMode != nullptr) {
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001755 *outMode = BasicClient::getServiceAudioRestriction();
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001756 }
1757 return binder::Status::ok();
1758}
1759
Ravneetaeb20dc2022-03-30 05:33:03 +00001760status_t CameraDeviceClient::setCameraServiceWatchdog(bool enabled) {
1761 return mDevice->setCameraServiceWatchdog(enabled);
1762}
1763
Jayant Chowdhary44d5f622023-09-20 03:11:41 +00001764status_t CameraDeviceClient::setRotateAndCropOverride(uint8_t rotateAndCrop, bool fromHal) {
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08001765 if (rotateAndCrop > ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return BAD_VALUE;
1766
1767 return mDevice->setRotateAndCropAutoBehavior(
Jayant Chowdhary44d5f622023-09-20 03:11:41 +00001768 static_cast<camera_metadata_enum_android_scaler_rotate_and_crop_t>(rotateAndCrop), fromHal);
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08001769}
1770
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00001771status_t CameraDeviceClient::setAutoframingOverride(uint8_t autoframingValue) {
1772 if (autoframingValue > ANDROID_CONTROL_AUTOFRAMING_AUTO) return BAD_VALUE;
1773
1774 return mDevice->setAutoframingAutoBehavior(
1775 static_cast<camera_metadata_enum_android_control_autoframing_t>(autoframingValue));
1776}
1777
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08001778bool CameraDeviceClient::supportsCameraMute() {
1779 return mDevice->supportsCameraMute();
1780}
1781
1782status_t CameraDeviceClient::setCameraMute(bool enabled) {
1783 return mDevice->setCameraMute(enabled);
1784}
1785
Shuzhen Wang16610a62022-12-15 22:38:07 -08001786void CameraDeviceClient::setStreamUseCaseOverrides(
1787 const std::vector<int64_t>& useCaseOverrides) {
1788 mDevice->setStreamUseCaseOverrides(useCaseOverrides);
1789}
1790
1791void CameraDeviceClient::clearStreamUseCaseOverrides() {
1792 mDevice->clearStreamUseCaseOverrides();
1793}
1794
Shuzhen Wangaf22e912023-04-11 16:03:17 -07001795bool CameraDeviceClient::supportsZoomOverride() {
1796 return mDevice->supportsZoomOverride();
1797}
1798
1799status_t CameraDeviceClient::setZoomOverride(int32_t zoomOverride) {
1800 return mDevice->setZoomOverride(zoomOverride);
1801}
1802
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001803binder::Status CameraDeviceClient::switchToOffline(
1804 const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb,
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001805 const std::vector<int>& offlineOutputIds,
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001806 /*out*/
1807 sp<hardware::camera2::ICameraOfflineSession>* session) {
1808 ATRACE_CALL();
1809
1810 binder::Status res;
1811 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1812
1813 Mutex::Autolock icl(mBinderSerializationLock);
1814
1815 if (!mDevice.get()) {
1816 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1817 }
1818
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001819 if (offlineOutputIds.empty()) {
Emilian Peevcc0b7952020-01-07 13:54:47 -08001820 String8 msg = String8::format("Offline surfaces must not be empty");
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001821 ALOGE("%s: %s", __FUNCTION__, msg.string());
1822 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1823 }
1824
1825 if (session == nullptr) {
1826 String8 msg = String8::format("Invalid offline session");
1827 ALOGE("%s: %s", __FUNCTION__, msg.string());
1828 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1829 }
1830
Yin-Chia Yeh87fccca2020-01-28 09:37:18 -08001831 std::vector<int32_t> offlineStreamIds;
1832 offlineStreamIds.reserve(offlineOutputIds.size());
Emilian Peev4697b642019-11-19 17:11:14 -08001833 KeyedVector<sp<IBinder>, sp<CompositeStream>> offlineCompositeStreamMap;
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001834 for (const auto& streamId : offlineOutputIds) {
1835 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001836 if (index == NAME_NOT_FOUND) {
Emilian Peevcc0b7952020-01-07 13:54:47 -08001837 String8 msg = String8::format("Offline surface with id: %d is not registered",
1838 streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001839 ALOGE("%s: %s", __FUNCTION__, msg.string());
1840 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1841 }
Emilian Peevcc0b7952020-01-07 13:54:47 -08001842
1843 if (!mStreamInfoMap[streamId].supportsOffline) {
1844 String8 msg = String8::format("Offline surface with id: %d doesn't support "
1845 "offline mode", streamId);
1846 ALOGE("%s: %s", __FUNCTION__, msg.string());
1847 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1848 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001849
Emilian Peev2f5d6012022-01-19 16:16:50 -08001850 Mutex::Autolock l(mCompositeLock);
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001851 bool isCompositeStream = false;
Emilian Peev2f5d6012022-01-19 16:16:50 -08001852 for (const auto& gbp : mConfiguredOutputs.valueAt(index).getGraphicBufferProducers()) {
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001853 sp<Surface> s = new Surface(gbp, false /*controlledByApp*/);
Pirama Arumuga Nainar8db21062022-01-28 10:15:12 -08001854 isCompositeStream = camera3::DepthCompositeStream::isDepthCompositeStream(s) ||
Emilian Peev434248e2022-10-06 14:58:54 -07001855 camera3::HeicCompositeStream::isHeicCompositeStream(s) ||
Emilian Peeve579d8b2023-02-28 14:16:08 -08001856 (camera3::JpegRCompositeStream::isJpegRCompositeStream(s) &&
Emilian Peev15230142023-04-27 20:22:54 +00001857 !mDevice->isCompositeJpegRDisabled());
Emilian Peev4697b642019-11-19 17:11:14 -08001858 if (isCompositeStream) {
1859 auto compositeIdx = mCompositeStreamMap.indexOfKey(IInterface::asBinder(gbp));
1860 if (compositeIdx == NAME_NOT_FOUND) {
1861 ALOGE("%s: Unknown composite stream", __FUNCTION__);
1862 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1863 "Unknown composite stream");
1864 }
1865
1866 mCompositeStreamMap.valueAt(compositeIdx)->insertCompositeStreamIds(
1867 &offlineStreamIds);
1868 offlineCompositeStreamMap.add(mCompositeStreamMap.keyAt(compositeIdx),
1869 mCompositeStreamMap.valueAt(compositeIdx));
1870 break;
1871 }
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001872 }
1873
Emilian Peev4697b642019-11-19 17:11:14 -08001874 if (!isCompositeStream) {
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001875 offlineStreamIds.push_back(streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001876 }
1877 }
1878
1879 sp<CameraOfflineSessionBase> offlineSession;
1880 auto ret = mDevice->switchToOffline(offlineStreamIds, &offlineSession);
1881 if (ret != OK) {
1882 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1883 "Camera %s: Error switching to offline mode: %s (%d)",
1884 mCameraIdStr.string(), strerror(ret), ret);
1885 }
1886
Emilian Peevcc0b7952020-01-07 13:54:47 -08001887 sp<CameraOfflineSessionClient> offlineClient;
1888 if (offlineSession.get() != nullptr) {
1889 offlineClient = new CameraOfflineSessionClient(sCameraService,
1890 offlineSession, offlineCompositeStreamMap, cameraCb, mClientPackageName,
Emilian Peev8b64f282021-03-25 16:49:57 -07001891 mClientFeatureId, mCameraIdStr, mCameraFacing, mOrientation, mClientPid, mClientUid,
1892 mServicePid);
Emilian Peevcc0b7952020-01-07 13:54:47 -08001893 ret = sCameraService->addOfflineClient(mCameraIdStr, offlineClient);
1894 }
1895
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001896 if (ret == OK) {
Emilian Peevd99c8ae2019-11-26 13:19:13 -08001897 // A successful offline session switch must reset the current camera client
1898 // and release any resources occupied by previously configured streams.
1899 mStreamMap.clear();
1900 mConfiguredOutputs.clear();
1901 mDeferredStreams.clear();
1902 mStreamInfoMap.clear();
Emilian Peev2f5d6012022-01-19 16:16:50 -08001903 Mutex::Autolock l(mCompositeLock);
Emilian Peevd99c8ae2019-11-26 13:19:13 -08001904 mCompositeStreamMap.clear();
1905 mInputStream = {false, 0, 0, 0, 0};
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001906 } else {
Emilian Peev886ac212023-02-07 14:10:24 -08001907 // In case we failed to register the offline client, ensure that it still initialized
1908 // so that all failing requests can return back correctly once the object is released.
1909 offlineClient->initialize(nullptr /*cameraProviderManager*/, String8()/*monitorTags*/);
1910
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001911 switch(ret) {
1912 case BAD_VALUE:
1913 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1914 "Illegal argument to HAL module for camera \"%s\"", mCameraIdStr.c_str());
1915 case TIMED_OUT:
1916 return STATUS_ERROR_FMT(CameraService::ERROR_CAMERA_IN_USE,
1917 "Camera \"%s\" is already open", mCameraIdStr.c_str());
1918 default:
1919 return STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1920 "Failed to initialize camera \"%s\": %s (%d)", mCameraIdStr.c_str(),
1921 strerror(-ret), ret);
1922 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001923 }
1924
1925 *session = offlineClient;
1926
1927 return binder::Status::ok();
1928}
1929
Igor Murashkine7ee7632013-06-11 18:10:18 -07001930status_t CameraDeviceClient::dump(int fd, const Vector<String16>& args) {
Eino-Ville Talvalac4003962016-01-13 10:07:04 -08001931 return BasicClient::dump(fd, args);
1932}
1933
1934status_t CameraDeviceClient::dumpClient(int fd, const Vector<String16>& args) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001935 dprintf(fd, " CameraDeviceClient[%s] (%p) dump:\n",
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001936 mCameraIdStr.string(),
Eino-Ville Talvalae992e752014-11-07 16:17:48 -08001937 (getRemoteCallback() != NULL ?
Marco Nelissenf8880202014-11-14 07:58:25 -08001938 IInterface::asBinder(getRemoteCallback()).get() : NULL) );
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001939 dprintf(fd, " Current client UID %u\n", mClientUid);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001940
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001941 dprintf(fd, " State:\n");
1942 dprintf(fd, " Request ID counter: %d\n", mRequestIdCounter);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001943 if (mInputStream.configured) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001944 dprintf(fd, " Current input stream ID: %d\n", mInputStream.id);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001945 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001946 dprintf(fd, " No input stream configured.\n");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001947 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001948 if (!mStreamMap.isEmpty()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001949 dprintf(fd, " Current output stream/surface IDs:\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001950 for (size_t i = 0; i < mStreamMap.size(); i++) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001951 dprintf(fd, " Stream %d Surface %d\n",
Shuzhen Wang0129d522016-10-30 22:43:41 -07001952 mStreamMap.valueAt(i).streamId(),
1953 mStreamMap.valueAt(i).surfaceId());
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001954 }
Zhijun He5d677d12016-05-29 16:52:39 -07001955 } else if (!mDeferredStreams.isEmpty()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001956 dprintf(fd, " Current deferred surface output stream IDs:\n");
Zhijun He5d677d12016-05-29 16:52:39 -07001957 for (auto& streamId : mDeferredStreams) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001958 dprintf(fd, " Stream %d\n", streamId);
Zhijun He5d677d12016-05-29 16:52:39 -07001959 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001960 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001961 dprintf(fd, " No output streams configured.\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001962 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001963 // TODO: print dynamic/request section from most recent requests
1964 mFrameProcessor->dump(fd, args);
1965
1966 return dumpDevice(fd, args);
1967}
1968
Avichal Rakesh7e53cad2021-10-05 13:46:30 -07001969status_t CameraDeviceClient::startWatchingTags(const String8 &tags, int out) {
1970 sp<CameraDeviceBase> device = mDevice;
1971 if (!device) {
1972 dprintf(out, " Device is detached.");
1973 return OK;
1974 }
1975 device->startWatchingTags(tags);
1976 return OK;
1977}
1978
1979status_t CameraDeviceClient::stopWatchingTags(int out) {
1980 sp<CameraDeviceBase> device = mDevice;
1981 if (!device) {
1982 dprintf(out, " Device is detached.");
1983 return OK;
1984 }
1985 device->stopWatchingTags();
1986 return OK;
1987}
1988
1989status_t CameraDeviceClient::dumpWatchedEventsToVector(std::vector<std::string> &out) {
1990 sp<CameraDeviceBase> device = mDevice;
1991 if (!device) {
1992 return OK;
1993 }
1994 device->dumpWatchedEventsToVector(out);
1995 return OK;
1996}
1997
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001998void CameraDeviceClient::notifyError(int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -07001999 const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002000 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002001 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002002
Emilian Peev538c90e2018-12-17 18:03:19 +00002003 bool skipClientNotification = false;
Emilian Peev2f5d6012022-01-19 16:16:50 -08002004 {
2005 // Access to the composite stream map must be synchronized
2006 Mutex::Autolock l(mCompositeLock);
2007 // Composites can have multiple internal streams. Error notifications coming from such
2008 // internal streams may need to remain within camera service.
2009 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
2010 skipClientNotification |= mCompositeStreamMap.valueAt(i)->onError(errorCode,
2011 resultExtras);
2012 }
Emilian Peev538c90e2018-12-17 18:03:19 +00002013 }
2014
2015 if ((remoteCb != 0) && (!skipClientNotification)) {
Jianing Weicb0652e2014-03-12 18:29:36 -07002016 remoteCb->onDeviceError(errorCode, resultExtras);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002017 }
2018}
2019
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07002020void CameraDeviceClient::notifyRepeatingRequestError(long lastFrameNumber) {
2021 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
2022
2023 if (remoteCb != 0) {
Yin-Chia Yeh8ca23dc2017-09-05 18:15:56 -07002024 remoteCb->onRepeatingRequestError(lastFrameNumber, mStreamingRequestId);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07002025 }
2026
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07002027 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07002028 mStreamingRequestId = REQUEST_ID_NONE;
2029}
2030
Shuzhen Wang316781a2020-08-18 18:11:01 -07002031void CameraDeviceClient::notifyIdle(
2032 int64_t requestCount, int64_t resultErrorCount, bool deviceError,
2033 const std::vector<hardware::CameraStreamStats>& streamStats) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002034 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002035 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002036
2037 if (remoteCb != 0) {
2038 remoteCb->onDeviceIdle();
2039 }
Emilian Peev567c31c2023-03-06 15:02:37 -08002040
2041 std::vector<hardware::CameraStreamStats> fullStreamStats = streamStats;
2042 {
2043 Mutex::Autolock l(mCompositeLock);
2044 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
2045 hardware::CameraStreamStats compositeStats;
2046 mCompositeStreamMap.valueAt(i)->getStreamStats(&compositeStats);
2047 if (compositeStats.mWidth > 0) {
2048 fullStreamStats.push_back(compositeStats);
2049 }
2050 }
2051 }
Shuzhen Wangd26b1862022-03-07 12:05:05 -08002052 Camera2ClientBase::notifyIdleWithUserTag(requestCount, resultErrorCount, deviceError,
Emilian Peev567c31c2023-03-06 15:02:37 -08002053 fullStreamStats, mUserTag, mVideoStabilizationMode);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002054}
2055
Jianing Weicb0652e2014-03-12 18:29:36 -07002056void CameraDeviceClient::notifyShutter(const CaptureResultExtras& resultExtras,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002057 nsecs_t timestamp) {
2058 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002059 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002060 if (remoteCb != 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -07002061 remoteCb->onCaptureStarted(resultExtras, timestamp);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002062 }
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07002063 Camera2ClientBase::notifyShutter(resultExtras, timestamp);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08002064
Emilian Peev2f5d6012022-01-19 16:16:50 -08002065 // Access to the composite stream map must be synchronized
2066 Mutex::Autolock l(mCompositeLock);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08002067 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
2068 mCompositeStreamMap.valueAt(i)->onShutter(resultExtras, timestamp);
2069 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002070}
2071
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002072void CameraDeviceClient::notifyPrepared(int streamId) {
2073 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002074 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002075 if (remoteCb != 0) {
Jayant Chowdhary57184d52023-02-14 20:54:39 +00002076 ALOGV("%s: stream id %d", __FUNCTION__, streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002077 remoteCb->onPrepared(streamId);
2078 }
2079}
2080
Shuzhen Wang9d066012016-09-30 11:30:20 -07002081void CameraDeviceClient::notifyRequestQueueEmpty() {
2082 // Thread safe. Don't bother locking.
2083 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
2084 if (remoteCb != 0) {
2085 remoteCb->onRequestQueueEmpty();
2086 }
2087}
2088
Igor Murashkine7ee7632013-06-11 18:10:18 -07002089void CameraDeviceClient::detachDevice() {
2090 if (mDevice == 0) return;
2091
Shuzhen Wang316781a2020-08-18 18:11:01 -07002092 nsecs_t startTime = systemTime();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002093 ALOGV("Camera %s: Stopping processors", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -07002094
Emilian Peev7ee731f2022-02-08 14:55:52 -08002095 if (mFrameProcessor.get() != nullptr) {
2096 mFrameProcessor->removeListener(
2097 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MIN_ID,
2098 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MAX_ID, /*listener*/this);
2099 mFrameProcessor->requestExit();
2100 ALOGV("Camera %s: Waiting for threads", mCameraIdStr.string());
2101 mFrameProcessor->join();
2102 ALOGV("Camera %s: Disconnecting device", mCameraIdStr.string());
2103 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07002104
2105 // WORKAROUND: HAL refuses to disconnect while there's streams in flight
2106 {
Emilian Peev6b51d7d2018-07-23 11:41:44 +01002107 int64_t lastFrameNumber;
Igor Murashkine7ee7632013-06-11 18:10:18 -07002108 status_t code;
Emilian Peev6b51d7d2018-07-23 11:41:44 +01002109 if ((code = mDevice->flush(&lastFrameNumber)) != OK) {
2110 ALOGE("%s: flush failed with code 0x%x", __FUNCTION__, code);
2111 }
2112
Igor Murashkine7ee7632013-06-11 18:10:18 -07002113 if ((code = mDevice->waitUntilDrained()) != OK) {
2114 ALOGE("%s: waitUntilDrained failed with code 0x%x", __FUNCTION__,
2115 code);
2116 }
2117 }
2118
Emilian Peev2f5d6012022-01-19 16:16:50 -08002119 {
2120 Mutex::Autolock l(mCompositeLock);
2121 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
2122 auto ret = mCompositeStreamMap.valueAt(i)->deleteInternalStreams();
2123 if (ret != OK) {
2124 ALOGE("%s: Failed removing composite stream %s (%d)", __FUNCTION__,
2125 strerror(-ret), ret);
2126 }
Emilian Peev5e4c7322019-10-22 14:20:52 -07002127 }
Emilian Peev2f5d6012022-01-19 16:16:50 -08002128 mCompositeStreamMap.clear();
Emilian Peev5e4c7322019-10-22 14:20:52 -07002129 }
Emilian Peev5e4c7322019-10-22 14:20:52 -07002130
Shuzhen Wang03fe6232023-02-05 12:41:15 -08002131 bool hasDeviceError = mDevice->hasDeviceError();
Igor Murashkine7ee7632013-06-11 18:10:18 -07002132 Camera2ClientBase::detachDevice();
Shuzhen Wang316781a2020-08-18 18:11:01 -07002133
2134 int32_t closeLatencyMs = ns2ms(systemTime() - startTime);
Shuzhen Wang03fe6232023-02-05 12:41:15 -08002135 mCameraServiceProxyWrapper->logClose(mCameraIdStr, closeLatencyMs, hasDeviceError);
Igor Murashkine7ee7632013-06-11 18:10:18 -07002136}
2137
2138/** Device-related methods */
Jianing Weicb0652e2014-03-12 18:29:36 -07002139void CameraDeviceClient::onResultAvailable(const CaptureResult& result) {
Igor Murashkine7ee7632013-06-11 18:10:18 -07002140 ATRACE_CALL();
2141 ALOGV("%s", __FUNCTION__);
2142
Igor Murashkin4fb55c12013-08-29 17:43:01 -07002143 // Thread-safe. No lock necessary.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002144 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = mRemoteCallback;
Igor Murashkin4fb55c12013-08-29 17:43:01 -07002145 if (remoteCb != NULL) {
Shuzhen Wang5c22c152017-12-31 17:12:25 -08002146 remoteCb->onResultReceived(result.mMetadata, result.mResultExtras,
2147 result.mPhysicalMetadatas);
Igor Murashkine7ee7632013-06-11 18:10:18 -07002148 }
Emilian Peev538c90e2018-12-17 18:03:19 +00002149
Emilian Peev2f5d6012022-01-19 16:16:50 -08002150 // Access to the composite stream map must be synchronized
2151 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +00002152 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
2153 mCompositeStreamMap.valueAt(i)->onResultAvailable(result);
2154 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07002155}
2156
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002157binder::Status CameraDeviceClient::checkPidStatus(const char* checkLocation) {
Eino-Ville Talvala6192b892016-04-04 12:31:18 -07002158 if (mDisconnected) {
2159 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED,
2160 "The camera device has been disconnected");
2161 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002162 status_t res = checkPid(checkLocation);
2163 return (res == OK) ? binder::Status::ok() :
2164 STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
2165 "Attempt to use camera from a different process than original client");
2166}
2167
Igor Murashkine7ee7632013-06-11 18:10:18 -07002168// TODO: move to Camera2ClientBase
2169bool CameraDeviceClient::enforceRequestPermissions(CameraMetadata& metadata) {
2170
Jayant Chowdhary12361932018-08-27 14:46:13 -07002171 const int pid = CameraThreadState::getCallingPid();
Igor Murashkine7ee7632013-06-11 18:10:18 -07002172 const int selfPid = getpid();
2173 camera_metadata_entry_t entry;
2174
2175 /**
2176 * Mixin default important security values
2177 * - android.led.transmit = defaulted ON
2178 */
2179 CameraMetadata staticInfo = mDevice->info();
2180 entry = staticInfo.find(ANDROID_LED_AVAILABLE_LEDS);
2181 for(size_t i = 0; i < entry.count; ++i) {
2182 uint8_t led = entry.data.u8[i];
2183
2184 switch(led) {
2185 case ANDROID_LED_AVAILABLE_LEDS_TRANSMIT: {
2186 uint8_t transmitDefault = ANDROID_LED_TRANSMIT_ON;
2187 if (!metadata.exists(ANDROID_LED_TRANSMIT)) {
2188 metadata.update(ANDROID_LED_TRANSMIT,
2189 &transmitDefault, 1);
2190 }
2191 break;
2192 }
2193 }
2194 }
2195
2196 // We can do anything!
2197 if (pid == selfPid) {
2198 return true;
2199 }
2200
2201 /**
2202 * Permission check special fields in the request
2203 * - android.led.transmit = android.permission.CAMERA_DISABLE_TRANSMIT
2204 */
2205 entry = metadata.find(ANDROID_LED_TRANSMIT);
2206 if (entry.count > 0 && entry.data.u8[0] != ANDROID_LED_TRANSMIT_ON) {
2207 String16 permissionString =
2208 String16("android.permission.CAMERA_DISABLE_TRANSMIT_LED");
2209 if (!checkCallingPermission(permissionString)) {
Jayant Chowdhary12361932018-08-27 14:46:13 -07002210 const int uid = CameraThreadState::getCallingUid();
Igor Murashkine7ee7632013-06-11 18:10:18 -07002211 ALOGE("Permission Denial: "
2212 "can't disable transmit LED pid=%d, uid=%d", pid, uid);
2213 return false;
2214 }
2215 }
2216
2217 return true;
2218}
2219
Shuzhen Wang610d7b82022-02-08 14:37:22 -08002220status_t CameraDeviceClient::getRotationTransformLocked(int mirrorMode,
2221 int32_t* transform) {
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07002222 ALOGV("%s: begin", __FUNCTION__);
2223
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07002224 const CameraMetadata& staticInfo = mDevice->info();
Shuzhen Wang610d7b82022-02-08 14:37:22 -08002225 return CameraUtils::getRotationTransform(staticInfo, mirrorMode, transform);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07002226}
2227
Emilian Peevf4816702020-04-03 15:44:51 -07002228binder::Status CameraDeviceClient::mapRequestTemplate(int templateId,
2229 camera_request_template_t* tempId /*out*/) {
2230 binder::Status ret = binder::Status::ok();
2231
2232 if (tempId == nullptr) {
2233 ret = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
2234 "Camera %s: Invalid template argument", mCameraIdStr.string());
2235 return ret;
2236 }
2237 switch(templateId) {
2238 case ICameraDeviceUser::TEMPLATE_PREVIEW:
2239 *tempId = camera_request_template_t::CAMERA_TEMPLATE_PREVIEW;
2240 break;
2241 case ICameraDeviceUser::TEMPLATE_RECORD:
2242 *tempId = camera_request_template_t::CAMERA_TEMPLATE_VIDEO_RECORD;
2243 break;
2244 case ICameraDeviceUser::TEMPLATE_STILL_CAPTURE:
2245 *tempId = camera_request_template_t::CAMERA_TEMPLATE_STILL_CAPTURE;
2246 break;
2247 case ICameraDeviceUser::TEMPLATE_VIDEO_SNAPSHOT:
2248 *tempId = camera_request_template_t::CAMERA_TEMPLATE_VIDEO_SNAPSHOT;
2249 break;
2250 case ICameraDeviceUser::TEMPLATE_ZERO_SHUTTER_LAG:
2251 *tempId = camera_request_template_t::CAMERA_TEMPLATE_ZERO_SHUTTER_LAG;
2252 break;
2253 case ICameraDeviceUser::TEMPLATE_MANUAL:
2254 *tempId = camera_request_template_t::CAMERA_TEMPLATE_MANUAL;
2255 break;
2256 default:
2257 ret = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
2258 "Camera %s: Template ID %d is invalid or not supported",
2259 mCameraIdStr.string(), templateId);
2260 return ret;
2261 }
2262
2263 return ret;
2264}
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08002265
2266const CameraMetadata &CameraDeviceClient::getStaticInfo(const String8 &cameraId) {
2267 if (mDevice->getId() == cameraId) {
2268 return mDevice->info();
2269 }
2270 return mDevice->infoPhysical(cameraId);
2271}
2272
Jayant Chowdharydbd1efb2023-02-07 16:14:48 -08002273bool CameraDeviceClient::supportsUltraHighResolutionCapture(const String8 &cameraId) {
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08002274 const CameraMetadata &deviceInfo = getStaticInfo(cameraId);
Jayant Chowdharydbd1efb2023-02-07 16:14:48 -08002275 return SessionConfigurationUtils::supportsUltraHighResolutionCapture(deviceInfo);
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08002276}
2277
2278bool CameraDeviceClient::isSensorPixelModeConsistent(
2279 const std::list<int> &streamIdList, const CameraMetadata &settings) {
2280 // First we get the sensorPixelMode from the settings metadata.
2281 int32_t sensorPixelMode = ANDROID_SENSOR_PIXEL_MODE_DEFAULT;
2282 camera_metadata_ro_entry sensorPixelModeEntry = settings.find(ANDROID_SENSOR_PIXEL_MODE);
2283 if (sensorPixelModeEntry.count != 0) {
2284 sensorPixelMode = sensorPixelModeEntry.data.u8[0];
2285 if (sensorPixelMode != ANDROID_SENSOR_PIXEL_MODE_DEFAULT &&
2286 sensorPixelMode != ANDROID_SENSOR_PIXEL_MODE_MAXIMUM_RESOLUTION) {
2287 ALOGE("%s: Request sensor pixel mode not is not one of the valid values %d",
2288 __FUNCTION__, sensorPixelMode);
2289 return false;
2290 }
2291 }
2292 // Check whether each stream has max resolution allowed.
2293 bool consistent = true;
2294 for (auto it : streamIdList) {
2295 auto const streamInfoIt = mStreamInfoMap.find(it);
2296 if (streamInfoIt == mStreamInfoMap.end()) {
2297 ALOGE("%s: stream id %d not created, skipping", __FUNCTION__, it);
2298 return false;
2299 }
2300 consistent =
2301 streamInfoIt->second.sensorPixelModesUsed.find(sensorPixelMode) !=
2302 streamInfoIt->second.sensorPixelModesUsed.end();
2303 if (!consistent) {
2304 ALOGE("sensorPixelMode used %i not consistent with configured modes", sensorPixelMode);
2305 for (auto m : streamInfoIt->second.sensorPixelModesUsed) {
2306 ALOGE("sensor pixel mode used list: %i", m);
2307 }
2308 break;
2309 }
2310 }
2311
2312 return consistent;
2313}
2314
Igor Murashkine7ee7632013-06-11 18:10:18 -07002315} // namespace android