blob: 4f699a783600f3779a10fd1879f1a1d7651f6a84 [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,
101 bool overrideToPortrait) :
Austin Borger74fca042022-05-23 12:41:21 -0700102 Camera2ClientBase(cameraService, remoteCallback, cameraServiceProxyWrapper, clientPackageName,
103 systemNativeClient, clientFeatureId, cameraId, /*API1 camera ID*/ -1, cameraFacing,
Austin Borger18b30a72022-10-27 12:20:29 -0700104 sensorOrientation, clientPid, clientUid, servicePid, overrideForPerfClass,
105 overrideToPortrait),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700106 mInputStream(),
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700107 mStreamingRequestId(REQUEST_ID_NONE),
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700108 mRequestIdCounter(0),
109 mOverrideForPerfClass(overrideForPerfClass) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700110
111 ATRACE_CALL();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800112 ALOGI("CameraDeviceClient %s: Opened", cameraId.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700113}
114
Emilian Peevbd8c5032018-02-14 23:05:40 +0000115status_t CameraDeviceClient::initialize(sp<CameraProviderManager> manager,
116 const String8& monitorTags) {
117 return initializeImpl(manager, monitorTags);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800118}
119
120template<typename TProviderPtr>
Emilian Peevbd8c5032018-02-14 23:05:40 +0000121status_t CameraDeviceClient::initializeImpl(TProviderPtr providerPtr, const String8& monitorTags) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700122 ATRACE_CALL();
123 status_t res;
124
Emilian Peevbd8c5032018-02-14 23:05:40 +0000125 res = Camera2ClientBase::initialize(providerPtr, monitorTags);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700126 if (res != OK) {
127 return res;
128 }
129
130 String8 threadName;
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -0700131 mFrameProcessor = new FrameProcessorBase(mDevice);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800132 threadName = String8::format("CDU-%s-FrameProc", mCameraIdStr.string());
Austin Borger7b129542022-06-09 13:23:06 -0700133 res = mFrameProcessor->run(threadName.string());
134 if (res != OK) {
135 ALOGE("%s: Unable to start frame processor thread: %s (%d)",
136 __FUNCTION__, strerror(-res), res);
137 return res;
138 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700139
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800140 mFrameProcessor->registerListener(camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MIN_ID,
141 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MAX_ID,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -0800142 /*listener*/this,
Zhijun He25a0aef2014-06-25 11:40:02 -0700143 /*sendPartials*/true);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700144
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800145 const CameraMetadata &deviceInfo = mDevice->info();
146 camera_metadata_ro_entry_t physicalKeysEntry = deviceInfo.find(
Emilian Peev00420d22018-02-05 21:33:13 +0000147 ANDROID_REQUEST_AVAILABLE_PHYSICAL_CAMERA_REQUEST_KEYS);
148 if (physicalKeysEntry.count > 0) {
149 mSupportedPhysicalRequestKeys.insert(mSupportedPhysicalRequestKeys.begin(),
150 physicalKeysEntry.data.i32,
151 physicalKeysEntry.data.i32 + physicalKeysEntry.count);
152 }
153
Emilian Peev2295df72021-11-12 18:14:10 -0800154 auto entry = deviceInfo.find(ANDROID_REQUEST_AVAILABLE_CAPABILITIES);
155 mDynamicProfileMap.emplace(
156 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD,
157 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD);
158 if (entry.count > 0) {
Shuzhen Wangc8ab4522021-12-14 20:12:42 -0800159 const auto it = std::find(entry.data.u8, entry.data.u8 + entry.count,
Emilian Peev2295df72021-11-12 18:14:10 -0800160 ANDROID_REQUEST_AVAILABLE_CAPABILITIES_DYNAMIC_RANGE_TEN_BIT);
Shuzhen Wangc8ab4522021-12-14 20:12:42 -0800161 if (it != entry.data.u8 + entry.count) {
Emilian Peev2295df72021-11-12 18:14:10 -0800162 entry = deviceInfo.find(ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP);
Emilian Peevc81a7592022-02-14 17:38:18 -0800163 if (entry.count > 0 || ((entry.count % 3) != 0)) {
164 int64_t standardBitmap =
165 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD;
166 for (size_t i = 0; i < entry.count; i += 3) {
167 if (entry.data.i64[i] !=
Emilian Peev2295df72021-11-12 18:14:10 -0800168 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD) {
Emilian Peevc81a7592022-02-14 17:38:18 -0800169 mDynamicProfileMap.emplace(entry.data.i64[i], entry.data.i64[i+1]);
170 if ((entry.data.i64[i+1] == 0) || (entry.data.i64[i+1] &
Emilian Peev2295df72021-11-12 18:14:10 -0800171 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD)) {
Emilian Peevc81a7592022-02-14 17:38:18 -0800172 standardBitmap |= entry.data.i64[i];
Emilian Peev2295df72021-11-12 18:14:10 -0800173 }
174 } else {
Emilian Peevc81a7592022-02-14 17:38:18 -0800175 ALOGE("%s: Device %s includes unexpected profile entry: 0x%" PRIx64 "!",
176 __FUNCTION__, mCameraIdStr.c_str(), entry.data.i64[i]);
Emilian Peev2295df72021-11-12 18:14:10 -0800177 }
178 }
Emilian Peevef715e22022-05-19 17:44:33 -0700179 mDynamicProfileMap[ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD] =
180 standardBitmap;
Emilian Peev2295df72021-11-12 18:14:10 -0800181 } else {
182 ALOGE("%s: Device %s supports 10-bit output but doesn't include a dynamic range"
183 " profile map!", __FUNCTION__, mCameraIdStr.c_str());
184 }
185 }
186 }
187
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700188 mProviderManager = providerPtr;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800189 // Cache physical camera ids corresponding to this device and also the high
190 // resolution sensors in this device + physical camera ids
191 mProviderManager->isLogicalCamera(mCameraIdStr.string(), &mPhysicalCameraIds);
Jayant Chowdharydbd1efb2023-02-07 16:14:48 -0800192 if (supportsUltraHighResolutionCapture(mCameraIdStr)) {
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800193 mHighResolutionSensors.insert(mCameraIdStr.string());
194 }
195 for (auto &physicalId : mPhysicalCameraIds) {
Jayant Chowdharydbd1efb2023-02-07 16:14:48 -0800196 if (supportsUltraHighResolutionCapture(String8(physicalId.c_str()))) {
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800197 mHighResolutionSensors.insert(physicalId.c_str());
198 }
199 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700200 return OK;
201}
202
203CameraDeviceClient::~CameraDeviceClient() {
204}
205
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800206binder::Status CameraDeviceClient::submitRequest(
207 const hardware::camera2::CaptureRequest& request,
208 bool streaming,
209 /*out*/
210 hardware::camera2::utils::SubmitInfo *submitInfo) {
211 std::vector<hardware::camera2::CaptureRequest> requestList = { request };
212 return submitRequestList(requestList, streaming, submitInfo);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700213}
214
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800215binder::Status CameraDeviceClient::insertGbpLocked(const sp<IGraphicBufferProducer>& gbp,
Emilian Peevf873aa52018-01-26 14:58:28 +0000216 SurfaceMap* outSurfaceMap, Vector<int32_t>* outputStreamIds, int32_t *currentStreamId) {
Emilian Peev538c90e2018-12-17 18:03:19 +0000217 int compositeIdx;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800218 int idx = mStreamMap.indexOfKey(IInterface::asBinder(gbp));
219
Emilian Peev2f5d6012022-01-19 16:16:50 -0800220 Mutex::Autolock l(mCompositeLock);
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800221 // Trying to submit request with surface that wasn't created
222 if (idx == NAME_NOT_FOUND) {
223 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
224 " we have not called createStream on",
225 __FUNCTION__, mCameraIdStr.string());
226 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
227 "Request targets Surface that is not part of current capture session");
Emilian Peev538c90e2018-12-17 18:03:19 +0000228 } else if ((compositeIdx = mCompositeStreamMap.indexOfKey(IInterface::asBinder(gbp)))
229 != NAME_NOT_FOUND) {
230 mCompositeStreamMap.valueAt(compositeIdx)->insertGbp(outSurfaceMap, outputStreamIds,
231 currentStreamId);
232 return binder::Status::ok();
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800233 }
234
235 const StreamSurfaceId& streamSurfaceId = mStreamMap.valueAt(idx);
236 if (outSurfaceMap->find(streamSurfaceId.streamId()) == outSurfaceMap->end()) {
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800237 outputStreamIds->push_back(streamSurfaceId.streamId());
238 }
239 (*outSurfaceMap)[streamSurfaceId.streamId()].push_back(streamSurfaceId.surfaceId());
240
241 ALOGV("%s: Camera %s: Appending output stream %d surface %d to request",
242 __FUNCTION__, mCameraIdStr.string(), streamSurfaceId.streamId(),
243 streamSurfaceId.surfaceId());
244
Emilian Peevf873aa52018-01-26 14:58:28 +0000245 if (currentStreamId != nullptr) {
246 *currentStreamId = streamSurfaceId.streamId();
247 }
248
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800249 return binder::Status::ok();
250}
251
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800252static std::list<int> getIntersection(const std::unordered_set<int> &streamIdsForThisCamera,
253 const Vector<int> &streamIdsForThisRequest) {
254 std::list<int> intersection;
255 for (auto &streamId : streamIdsForThisRequest) {
256 if (streamIdsForThisCamera.find(streamId) != streamIdsForThisCamera.end()) {
257 intersection.emplace_back(streamId);
258 }
259 }
260 return intersection;
261}
262
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800263binder::Status CameraDeviceClient::submitRequestList(
264 const std::vector<hardware::camera2::CaptureRequest>& requests,
265 bool streaming,
266 /*out*/
267 hardware::camera2::utils::SubmitInfo *submitInfo) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700268 ATRACE_CALL();
Mark Salyzyn50468412014-06-18 16:33:43 -0700269 ALOGV("%s-start of function. Request list size %zu", __FUNCTION__, requests.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700270
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800271 binder::Status res = binder::Status::ok();
272 status_t err;
273 if ( !(res = checkPidStatus(__FUNCTION__) ).isOk()) {
274 return res;
275 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700276
277 Mutex::Autolock icl(mBinderSerializationLock);
278
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800279 if (!mDevice.get()) {
280 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
281 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700282
283 if (requests.empty()) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800284 ALOGE("%s: Camera %s: Sent null request. Rejecting request.",
285 __FUNCTION__, mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800286 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Empty request list");
Jianing Wei90e59c92014-03-12 18:29:36 -0700287 }
288
Emilian Peevaebbe412018-01-15 13:53:24 +0000289 List<const CameraDeviceBase::PhysicalCameraSettingsList> metadataRequestList;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700290 std::list<const SurfaceMap> surfaceMapList;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800291 submitInfo->mRequestId = mRequestIdCounter;
Jianing Wei90e59c92014-03-12 18:29:36 -0700292 uint32_t loopCounter = 0;
293
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800294 for (auto&& request: requests) {
295 if (request.mIsReprocess) {
Chien-Yu Chened0412e2015-04-27 15:04:22 -0700296 if (!mInputStream.configured) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800297 ALOGE("%s: Camera %s: no input stream is configured.", __FUNCTION__,
298 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800299 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800300 "No input configured for camera %s but request is for reprocessing",
301 mCameraIdStr.string());
Chien-Yu Chened0412e2015-04-27 15:04:22 -0700302 } else if (streaming) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800303 ALOGE("%s: Camera %s: streaming reprocess requests not supported.", __FUNCTION__,
304 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800305 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
306 "Repeating reprocess requests not supported");
Emilian Peevaebbe412018-01-15 13:53:24 +0000307 } else if (request.mPhysicalCameraSettings.size() > 1) {
308 ALOGE("%s: Camera %s: reprocess requests not supported for "
309 "multiple physical cameras.", __FUNCTION__,
310 mCameraIdStr.string());
311 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
312 "Reprocess requests not supported for multiple cameras");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700313 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700314 }
315
Emilian Peevaebbe412018-01-15 13:53:24 +0000316 if (request.mPhysicalCameraSettings.empty()) {
317 ALOGE("%s: Camera %s: request doesn't contain any settings.", __FUNCTION__,
318 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800319 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
Emilian Peevaebbe412018-01-15 13:53:24 +0000320 "Request doesn't contain any settings");
321 }
322
323 //The first capture settings should always match the logical camera id
324 String8 logicalId(request.mPhysicalCameraSettings.begin()->id.c_str());
325 if (mDevice->getId() != logicalId) {
326 ALOGE("%s: Camera %s: Invalid camera request settings.", __FUNCTION__,
327 mCameraIdStr.string());
328 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
329 "Invalid camera request settings");
330 }
331
Emilian Peevaebbe412018-01-15 13:53:24 +0000332 if (request.mSurfaceList.isEmpty() && request.mStreamIdxList.size() == 0) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800333 ALOGE("%s: Camera %s: Requests must have at least one surface target. "
334 "Rejecting request.", __FUNCTION__, mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800335 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
336 "Request has no output targets");
Jianing Wei90e59c92014-03-12 18:29:36 -0700337 }
338
Jianing Wei90e59c92014-03-12 18:29:36 -0700339 /**
Shuzhen Wang0129d522016-10-30 22:43:41 -0700340 * Write in the output stream IDs and map from stream ID to surface ID
341 * which we calculate from the capture request's list of surface target
Jianing Wei90e59c92014-03-12 18:29:36 -0700342 */
Shuzhen Wang0129d522016-10-30 22:43:41 -0700343 SurfaceMap surfaceMap;
Jianing Wei90e59c92014-03-12 18:29:36 -0700344 Vector<int32_t> outputStreamIds;
Emilian Peevf873aa52018-01-26 14:58:28 +0000345 std::vector<std::string> requestedPhysicalIds;
Emilian Peevc81a7592022-02-14 17:38:18 -0800346 int64_t dynamicProfileBitmap = 0;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800347 if (request.mSurfaceList.size() > 0) {
Chih-Hung Hsieh3ef324d2018-12-11 11:48:12 -0800348 for (const sp<Surface>& surface : request.mSurfaceList) {
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800349 if (surface == 0) continue;
Jianing Wei90e59c92014-03-12 18:29:36 -0700350
Emilian Peevf873aa52018-01-26 14:58:28 +0000351 int32_t streamId;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800352 sp<IGraphicBufferProducer> gbp = surface->getIGraphicBufferProducer();
Emilian Peevf873aa52018-01-26 14:58:28 +0000353 res = insertGbpLocked(gbp, &surfaceMap, &outputStreamIds, &streamId);
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800354 if (!res.isOk()) {
355 return res;
356 }
Emilian Peevf873aa52018-01-26 14:58:28 +0000357
358 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
359 if (index >= 0) {
360 String8 requestedPhysicalId(
361 mConfiguredOutputs.valueAt(index).getPhysicalCameraId());
362 requestedPhysicalIds.push_back(requestedPhysicalId.string());
Emilian Peev2295df72021-11-12 18:14:10 -0800363 dynamicProfileBitmap |=
364 mConfiguredOutputs.valueAt(index).getDynamicRangeProfile();
Emilian Peevf873aa52018-01-26 14:58:28 +0000365 } else {
366 ALOGW("%s: Output stream Id not found among configured outputs!", __FUNCTION__);
367 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700368 }
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800369 } else {
370 for (size_t i = 0; i < request.mStreamIdxList.size(); i++) {
371 int streamId = request.mStreamIdxList.itemAt(i);
372 int surfaceIdx = request.mSurfaceIdxList.itemAt(i);
Jianing Wei90e59c92014-03-12 18:29:36 -0700373
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800374 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
375 if (index < 0) {
376 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
377 " we have not called createStream on: stream %d",
378 __FUNCTION__, mCameraIdStr.string(), streamId);
379 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
380 "Request targets Surface that is not part of current capture session");
381 }
382
383 const auto& gbps = mConfiguredOutputs.valueAt(index).getGraphicBufferProducers();
384 if ((size_t)surfaceIdx >= gbps.size()) {
385 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
386 " we have not called createStream on: stream %d, surfaceIdx %d",
387 __FUNCTION__, mCameraIdStr.string(), streamId, surfaceIdx);
388 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
389 "Request targets Surface has invalid surface index");
390 }
391
Emilian Peevf873aa52018-01-26 14:58:28 +0000392 res = insertGbpLocked(gbps[surfaceIdx], &surfaceMap, &outputStreamIds, nullptr);
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800393 if (!res.isOk()) {
394 return res;
395 }
Emilian Peevf873aa52018-01-26 14:58:28 +0000396
397 String8 requestedPhysicalId(
398 mConfiguredOutputs.valueAt(index).getPhysicalCameraId());
399 requestedPhysicalIds.push_back(requestedPhysicalId.string());
Emilian Peev2295df72021-11-12 18:14:10 -0800400 dynamicProfileBitmap |=
401 mConfiguredOutputs.valueAt(index).getDynamicRangeProfile();
402 }
403 }
404
405 if (dynamicProfileBitmap !=
406 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD) {
407 for (int i = ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD;
408 i < ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_MAX; i <<= 1) {
409 if ((dynamicProfileBitmap & i) == 0) {
410 continue;
411 }
412
413 const auto& it = mDynamicProfileMap.find(i);
414 if (it != mDynamicProfileMap.end()) {
415 if ((it->second == 0) ||
416 ((it->second & dynamicProfileBitmap) == dynamicProfileBitmap)) {
417 continue;
418 } else {
419 ALOGE("%s: Camera %s: Tried to submit a request with a surfaces that"
420 " reference an unsupported dynamic range profile combination"
Emilian Peevc81a7592022-02-14 17:38:18 -0800421 " 0x%" PRIx64 "!", __FUNCTION__, mCameraIdStr.string(),
Emilian Peev2295df72021-11-12 18:14:10 -0800422 dynamicProfileBitmap);
423 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
424 "Request targets an unsupported dynamic range profile"
425 " combination");
426 }
427 } else {
428 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
429 " references unsupported dynamic range profile 0x%x!",
430 __FUNCTION__, mCameraIdStr.string(), i);
431 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
432 "Request targets 10-bit Surface with unsupported dynamic range"
433 " profile");
434 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700435 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700436 }
437
Emilian Peevf873aa52018-01-26 14:58:28 +0000438 CameraDeviceBase::PhysicalCameraSettingsList physicalSettingsList;
439 for (const auto& it : request.mPhysicalCameraSettings) {
Emilian Peev00420d22018-02-05 21:33:13 +0000440 if (it.settings.isEmpty()) {
441 ALOGE("%s: Camera %s: Sent empty metadata packet. Rejecting request.",
442 __FUNCTION__, mCameraIdStr.string());
443 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
444 "Request settings are empty");
445 }
446
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800447 // Check whether the physical / logical stream has settings
448 // consistent with the sensor pixel mode(s) it was configured with.
449 // mCameraIdToStreamSet will only have ids that are high resolution
450 const auto streamIdSetIt = mHighResolutionCameraIdToStreamIdSet.find(it.id);
451 if (streamIdSetIt != mHighResolutionCameraIdToStreamIdSet.end()) {
452 std::list<int> streamIdsUsedInRequest = getIntersection(streamIdSetIt->second,
453 outputStreamIds);
454 if (!request.mIsReprocess &&
455 !isSensorPixelModeConsistent(streamIdsUsedInRequest, it.settings)) {
456 ALOGE("%s: Camera %s: Request settings CONTROL_SENSOR_PIXEL_MODE not "
457 "consistent with configured streams. Rejecting request.",
458 __FUNCTION__, it.id.c_str());
459 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
460 "Request settings CONTROL_SENSOR_PIXEL_MODE are not consistent with "
461 "streams configured");
462 }
463 }
464
Emilian Peevf873aa52018-01-26 14:58:28 +0000465 String8 physicalId(it.id.c_str());
Shuzhen Wang911c6a32021-10-27 13:36:03 -0700466 bool hasTestPatternModePhysicalKey = std::find(mSupportedPhysicalRequestKeys.begin(),
467 mSupportedPhysicalRequestKeys.end(), ANDROID_SENSOR_TEST_PATTERN_MODE) !=
468 mSupportedPhysicalRequestKeys.end();
469 bool hasTestPatternDataPhysicalKey = std::find(mSupportedPhysicalRequestKeys.begin(),
470 mSupportedPhysicalRequestKeys.end(), ANDROID_SENSOR_TEST_PATTERN_DATA) !=
471 mSupportedPhysicalRequestKeys.end();
Emilian Peevf873aa52018-01-26 14:58:28 +0000472 if (physicalId != mDevice->getId()) {
473 auto found = std::find(requestedPhysicalIds.begin(), requestedPhysicalIds.end(),
474 it.id);
475 if (found == requestedPhysicalIds.end()) {
476 ALOGE("%s: Camera %s: Physical camera id: %s not part of attached outputs.",
477 __FUNCTION__, mCameraIdStr.string(), physicalId.string());
478 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
479 "Invalid physical camera id");
480 }
Emilian Peev00420d22018-02-05 21:33:13 +0000481
482 if (!mSupportedPhysicalRequestKeys.empty()) {
483 // Filter out any unsupported physical request keys.
484 CameraMetadata filteredParams(mSupportedPhysicalRequestKeys.size());
485 camera_metadata_t *meta = const_cast<camera_metadata_t *>(
486 filteredParams.getAndLock());
487 set_camera_metadata_vendor_id(meta, mDevice->getVendorTagId());
488 filteredParams.unlock(meta);
489
490 for (const auto& keyIt : mSupportedPhysicalRequestKeys) {
491 camera_metadata_ro_entry entry = it.settings.find(keyIt);
492 if (entry.count > 0) {
493 filteredParams.update(entry);
494 }
495 }
496
Shuzhen Wang911c6a32021-10-27 13:36:03 -0700497 physicalSettingsList.push_back({it.id, filteredParams,
498 hasTestPatternModePhysicalKey, hasTestPatternDataPhysicalKey});
Emilian Peev00420d22018-02-05 21:33:13 +0000499 }
500 } else {
501 physicalSettingsList.push_back({it.id, it.settings});
Emilian Peevf873aa52018-01-26 14:58:28 +0000502 }
Emilian Peevf873aa52018-01-26 14:58:28 +0000503 }
504
505 if (!enforceRequestPermissions(physicalSettingsList.begin()->metadata)) {
506 // Callee logs
507 return STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
508 "Caller does not have permission to change restricted controls");
509 }
510
Emilian Peevaebbe412018-01-15 13:53:24 +0000511 physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_OUTPUT_STREAMS,
512 &outputStreamIds[0], outputStreamIds.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700513
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800514 if (request.mIsReprocess) {
Emilian Peevaebbe412018-01-15 13:53:24 +0000515 physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_INPUT_STREAMS,
516 &mInputStream.id, 1);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700517 }
518
Emilian Peevaebbe412018-01-15 13:53:24 +0000519 physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_ID,
520 &(submitInfo->mRequestId), /*size*/1);
Jianing Wei90e59c92014-03-12 18:29:36 -0700521 loopCounter++; // loopCounter starts from 1
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800522 ALOGV("%s: Camera %s: Creating request with ID %d (%d of %zu)",
523 __FUNCTION__, mCameraIdStr.string(), submitInfo->mRequestId,
524 loopCounter, requests.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700525
Emilian Peevaebbe412018-01-15 13:53:24 +0000526 metadataRequestList.push_back(physicalSettingsList);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700527 surfaceMapList.push_back(surfaceMap);
Shuzhen Wangd26b1862022-03-07 12:05:05 -0800528
Shuzhen Wang9372b0b2022-05-11 18:55:19 -0700529 // Save certain CaptureRequest settings
Shuzhen Wangd26b1862022-03-07 12:05:05 -0800530 if (!request.mUserTag.empty()) {
531 mUserTag = request.mUserTag;
532 }
Shuzhen Wang9372b0b2022-05-11 18:55:19 -0700533 camera_metadata_entry entry =
534 physicalSettingsList.begin()->metadata.find(
535 ANDROID_CONTROL_VIDEO_STABILIZATION_MODE);
536 if (entry.count == 1) {
537 mVideoStabilizationMode = entry.data.u8[0];
538 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700539 }
540 mRequestIdCounter++;
541
542 if (streaming) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700543 err = mDevice->setStreamingRequestList(metadataRequestList, surfaceMapList,
544 &(submitInfo->mLastFrameNumber));
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800545 if (err != OK) {
546 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800547 "Camera %s: Got error %s (%d) after trying to set streaming request",
548 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800549 ALOGE("%s: %s", __FUNCTION__, msg.string());
550 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
551 msg.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700552 } else {
Shuzhen Wangc9ca6782016-04-26 13:40:31 -0700553 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700554 mStreamingRequestId = submitInfo->mRequestId;
Jianing Wei90e59c92014-03-12 18:29:36 -0700555 }
556 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700557 err = mDevice->captureList(metadataRequestList, surfaceMapList,
558 &(submitInfo->mLastFrameNumber));
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800559 if (err != OK) {
560 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800561 "Camera %s: Got error %s (%d) after trying to submit capture request",
562 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800563 ALOGE("%s: %s", __FUNCTION__, msg.string());
564 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
565 msg.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700566 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800567 ALOGV("%s: requestId = %d ", __FUNCTION__, submitInfo->mRequestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700568 }
569
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800570 ALOGV("%s: Camera %s: End of function", __FUNCTION__, mCameraIdStr.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700571 return res;
572}
573
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800574binder::Status CameraDeviceClient::cancelRequest(
575 int requestId,
576 /*out*/
577 int64_t* lastFrameNumber) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700578 ATRACE_CALL();
579 ALOGV("%s, requestId = %d", __FUNCTION__, requestId);
580
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800581 status_t err;
582 binder::Status res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700583
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800584 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700585
586 Mutex::Autolock icl(mBinderSerializationLock);
587
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800588 if (!mDevice.get()) {
589 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
590 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700591
Shuzhen Wangc9ca6782016-04-26 13:40:31 -0700592 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700593 if (mStreamingRequestId != requestId) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800594 String8 msg = String8::format("Camera %s: Canceling request ID %d doesn't match "
595 "current request ID %d", mCameraIdStr.string(), requestId, mStreamingRequestId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800596 ALOGE("%s: %s", __FUNCTION__, msg.string());
597 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700598 }
599
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800600 err = mDevice->clearStreamingRequest(lastFrameNumber);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700601
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800602 if (err == OK) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800603 ALOGV("%s: Camera %s: Successfully cleared streaming request",
604 __FUNCTION__, mCameraIdStr.string());
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700605 mStreamingRequestId = REQUEST_ID_NONE;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800606 } else {
607 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800608 "Camera %s: Error clearing streaming request: %s (%d)",
609 mCameraIdStr.string(), strerror(-err), err);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700610 }
611
612 return res;
613}
614
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800615binder::Status CameraDeviceClient::beginConfigure() {
Ruben Brunkb2119af2014-05-09 19:57:56 -0700616 // TODO: Implement this.
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -0700617 ATRACE_CALL();
Zhijun He1fa89992015-06-01 15:44:31 -0700618 ALOGV("%s: Not implemented yet.", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800619 return binder::Status::ok();
Ruben Brunkb2119af2014-05-09 19:57:56 -0700620}
621
Emilian Peev5fbe0ba2017-10-20 15:45:45 +0100622binder::Status CameraDeviceClient::endConfigure(int operatingMode,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700623 const hardware::camera2::impl::CameraMetadataNative& sessionParams, int64_t startTimeMs,
Emilian Peevcc0b7952020-01-07 13:54:47 -0800624 std::vector<int>* offlineStreamIds /*out*/) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -0700625 ATRACE_CALL();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700626 ALOGV("%s: ending configure (%d input stream, %zu output surfaces)",
627 __FUNCTION__, mInputStream.configured ? 1 : 0,
628 mStreamMap.size());
Igor Murashkine2d167e2014-08-19 16:19:59 -0700629
Zhijun He0effd522016-03-04 10:22:27 -0800630 binder::Status res;
631 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
632
Emilian Peevcc0b7952020-01-07 13:54:47 -0800633 if (offlineStreamIds == nullptr) {
634 String8 msg = String8::format("Invalid offline stream ids");
635 ALOGE("%s: %s", __FUNCTION__, msg.string());
636 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
637 }
638
Zhijun He0effd522016-03-04 10:22:27 -0800639 Mutex::Autolock icl(mBinderSerializationLock);
640
641 if (!mDevice.get()) {
642 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
643 }
644
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700645 res = SessionConfigurationUtils::checkOperatingMode(operatingMode, mDevice->info(),
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000646 mCameraIdStr);
Emilian Peev35ae8262018-11-08 13:11:32 +0000647 if (!res.isOk()) {
648 return res;
649 }
650
651 status_t err = mDevice->configureStreams(sessionParams, operatingMode);
652 if (err == BAD_VALUE) {
653 String8 msg = String8::format("Camera %s: Unsupported set of inputs/outputs provided",
654 mCameraIdStr.string());
655 ALOGE("%s: %s", __FUNCTION__, msg.string());
656 res = STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
657 } else if (err != OK) {
658 String8 msg = String8::format("Camera %s: Error configuring streams: %s (%d)",
659 mCameraIdStr.string(), strerror(-err), err);
660 ALOGE("%s: %s", __FUNCTION__, msg.string());
661 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Emilian Peev538c90e2018-12-17 18:03:19 +0000662 } else {
Emilian Peevcc0b7952020-01-07 13:54:47 -0800663 offlineStreamIds->clear();
664 mDevice->getOfflineStreamIds(offlineStreamIds);
665
Emilian Peev2f5d6012022-01-19 16:16:50 -0800666 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +0000667 for (size_t i = 0; i < mCompositeStreamMap.size(); ++i) {
668 err = mCompositeStreamMap.valueAt(i)->configureStream();
Emilian Peevcc0b7952020-01-07 13:54:47 -0800669 if (err != OK) {
Emilian Peev538c90e2018-12-17 18:03:19 +0000670 String8 msg = String8::format("Camera %s: Error configuring composite "
671 "streams: %s (%d)", mCameraIdStr.string(), strerror(-err), err);
672 ALOGE("%s: %s", __FUNCTION__, msg.string());
673 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
674 break;
675 }
Emilian Peevcc0b7952020-01-07 13:54:47 -0800676
677 // Composite streams can only support offline mode in case all individual internal
678 // streams are also supported.
679 std::vector<int> internalStreams;
680 mCompositeStreamMap.valueAt(i)->insertCompositeStreamIds(&internalStreams);
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800681 offlineStreamIds->erase(
682 std::remove_if(offlineStreamIds->begin(), offlineStreamIds->end(),
Emilian Peevcc0b7952020-01-07 13:54:47 -0800683 [&internalStreams] (int streamId) {
684 auto it = std::find(internalStreams.begin(), internalStreams.end(),
685 streamId);
686 if (it != internalStreams.end()) {
687 internalStreams.erase(it);
688 return true;
689 }
690
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800691 return false;}), offlineStreamIds->end());
Emilian Peevcc0b7952020-01-07 13:54:47 -0800692 if (internalStreams.empty()) {
693 offlineStreamIds->push_back(mCompositeStreamMap.valueAt(i)->getStreamId());
694 }
695 }
696
697 for (const auto& offlineStreamId : *offlineStreamIds) {
698 mStreamInfoMap[offlineStreamId].supportsOffline = true;
Emilian Peev538c90e2018-12-17 18:03:19 +0000699 }
Shuzhen Wang316781a2020-08-18 18:11:01 -0700700
701 nsecs_t configureEnd = systemTime();
702 int32_t configureDurationMs = ns2ms(configureEnd) - startTimeMs;
Austin Borger74fca042022-05-23 12:41:21 -0700703 mCameraServiceProxyWrapper->logStreamConfigured(mCameraIdStr, operatingMode,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700704 false /*internalReconfig*/, configureDurationMs);
Emilian Peev35ae8262018-11-08 13:11:32 +0000705 }
706
707 return res;
708}
709
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800710binder::Status CameraDeviceClient::isSessionConfigurationSupported(
711 const SessionConfiguration& sessionConfiguration, bool *status /*out*/) {
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800712
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800713 ATRACE_CALL();
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800714 binder::Status res;
715 status_t ret = OK;
716 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
717
718 Mutex::Autolock icl(mBinderSerializationLock);
719
720 if (!mDevice.get()) {
721 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
722 }
723
724 auto operatingMode = sessionConfiguration.getOperatingMode();
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700725 res = SessionConfigurationUtils::checkOperatingMode(operatingMode, mDevice->info(),
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000726 mCameraIdStr);
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800727 if (!res.isOk()) {
728 return res;
729 }
730
731 if (status == nullptr) {
732 String8 msg = String8::format( "Camera %s: Invalid status!", mCameraIdStr.string());
733 ALOGE("%s: %s", __FUNCTION__, msg.string());
734 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
735 }
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700736
Emilian Peev35ae8262018-11-08 13:11:32 +0000737 *status = false;
Shuzhen Wanga79a64d2022-04-24 19:56:30 -0700738 camera3::metadataGetter getMetadata = [this](const String8 &id, bool /*overrideForPerfClass*/) {
739 return mDevice->infoPhysical(id);};
Emilian Peev35ae8262018-11-08 13:11:32 +0000740 ret = mProviderManager->isSessionConfigurationSupported(mCameraIdStr.string(),
Shuzhen Wanga79a64d2022-04-24 19:56:30 -0700741 sessionConfiguration, mOverrideForPerfClass, getMetadata, status);
Emilian Peev35ae8262018-11-08 13:11:32 +0000742 switch (ret) {
743 case OK:
744 // Expected, do nothing.
745 break;
746 case INVALID_OPERATION: {
747 String8 msg = String8::format(
748 "Camera %s: Session configuration query not supported!",
749 mCameraIdStr.string());
750 ALOGD("%s: %s", __FUNCTION__, msg.string());
751 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
752 }
753
754 break;
755 default: {
756 String8 msg = String8::format( "Camera %s: Error: %s (%d)", mCameraIdStr.string(),
757 strerror(-ret), ret);
758 ALOGE("%s: %s", __FUNCTION__, msg.string());
759 res = STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
760 msg.string());
761 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800762 }
763
764 return res;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700765}
766
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800767binder::Status CameraDeviceClient::deleteStream(int streamId) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700768 ATRACE_CALL();
769 ALOGV("%s (streamId = 0x%x)", __FUNCTION__, streamId);
770
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800771 binder::Status res;
772 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700773
774 Mutex::Autolock icl(mBinderSerializationLock);
775
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800776 if (!mDevice.get()) {
777 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
778 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700779
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700780 bool isInput = false;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700781 std::vector<sp<IBinder>> surfaces;
Zhijun He5d677d12016-05-29 16:52:39 -0700782 ssize_t dIndex = NAME_NOT_FOUND;
Emilian Peev538c90e2018-12-17 18:03:19 +0000783 ssize_t compositeIndex = NAME_NOT_FOUND;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700784
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700785 if (mInputStream.configured && mInputStream.id == streamId) {
786 isInput = true;
787 } else {
788 // Guard against trying to delete non-created streams
789 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700790 if (streamId == mStreamMap.valueAt(i).streamId()) {
791 surfaces.push_back(mStreamMap.keyAt(i));
792 }
793 }
794
795 // See if this stream is one of the deferred streams.
796 for (size_t i = 0; i < mDeferredStreams.size(); ++i) {
797 if (streamId == mDeferredStreams[i]) {
798 dIndex = i;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700799 break;
800 }
801 }
802
Emilian Peev2f5d6012022-01-19 16:16:50 -0800803 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +0000804 for (size_t i = 0; i < mCompositeStreamMap.size(); ++i) {
805 if (streamId == mCompositeStreamMap.valueAt(i)->getStreamId()) {
806 compositeIndex = i;
807 break;
808 }
809 }
810
Shuzhen Wang0129d522016-10-30 22:43:41 -0700811 if (surfaces.empty() && dIndex == NAME_NOT_FOUND) {
812 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no such"
813 " stream created yet", mCameraIdStr.string(), streamId);
814 ALOGW("%s: %s", __FUNCTION__, msg.string());
815 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700816 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700817 }
818
819 // Also returns BAD_VALUE if stream ID was not valid
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800820 status_t err = mDevice->deleteStream(streamId);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700821
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800822 if (err != OK) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800823 String8 msg = String8::format("Camera %s: Unexpected error %s (%d) when deleting stream %d",
824 mCameraIdStr.string(), strerror(-err), err, streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800825 ALOGE("%s: %s", __FUNCTION__, msg.string());
826 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
827 } else {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700828 if (isInput) {
829 mInputStream.configured = false;
Zhijun He5d677d12016-05-29 16:52:39 -0700830 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700831 for (auto& surface : surfaces) {
832 mStreamMap.removeItem(surface);
833 }
834
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800835 mConfiguredOutputs.removeItem(streamId);
836
Shuzhen Wang0129d522016-10-30 22:43:41 -0700837 if (dIndex != NAME_NOT_FOUND) {
838 mDeferredStreams.removeItemsAt(dIndex);
839 }
Emilian Peev538c90e2018-12-17 18:03:19 +0000840
841 if (compositeIndex != NAME_NOT_FOUND) {
Emilian Peev2f5d6012022-01-19 16:16:50 -0800842 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +0000843 status_t ret;
844 if ((ret = mCompositeStreamMap.valueAt(compositeIndex)->deleteStream())
845 != OK) {
846 String8 msg = String8::format("Camera %s: Unexpected error %s (%d) when "
847 "deleting composite stream %d", mCameraIdStr.string(), strerror(-err), err,
848 streamId);
849 ALOGE("%s: %s", __FUNCTION__, msg.string());
850 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
851 }
852 mCompositeStreamMap.removeItemsAt(compositeIndex);
853 }
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800854 for (auto &mapIt: mHighResolutionCameraIdToStreamIdSet) {
855 auto &streamSet = mapIt.second;
856 if (streamSet.find(streamId) != streamSet.end()) {
857 streamSet.erase(streamId);
858 break;
859 }
860 }
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700861 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700862 }
863
864 return res;
865}
866
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800867binder::Status CameraDeviceClient::createStream(
868 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
869 /*out*/
870 int32_t* newStreamId) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700871 ATRACE_CALL();
Igor Murashkine7ee7632013-06-11 18:10:18 -0700872
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800873 binder::Status res;
874 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700875
876 Mutex::Autolock icl(mBinderSerializationLock);
877
Shuzhen Wang0129d522016-10-30 22:43:41 -0700878 const std::vector<sp<IGraphicBufferProducer>>& bufferProducers =
879 outputConfiguration.getGraphicBufferProducers();
880 size_t numBufferProducers = bufferProducers.size();
Shuzhen Wang758c2152017-01-10 18:26:18 -0800881 bool deferredConsumer = outputConfiguration.isDeferred();
882 bool isShared = outputConfiguration.isShared();
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800883 String8 physicalCameraId = String8(outputConfiguration.getPhysicalCameraId());
Shuzhen Wang758c2152017-01-10 18:26:18 -0800884 bool deferredConsumerOnly = deferredConsumer && numBufferProducers == 0;
Shuzhen Wang83bff122020-11-20 15:51:39 -0800885 bool isMultiResolution = outputConfiguration.isMultiResolution();
Emilian Peevc81a7592022-02-14 17:38:18 -0800886 int64_t dynamicRangeProfile = outputConfiguration.getDynamicRangeProfile();
Shuzhen Wang8ed1e872022-03-08 16:34:33 -0800887 int64_t streamUseCase = outputConfiguration.getStreamUseCase();
Shuzhen Wange4208922022-02-01 16:52:48 -0800888 int timestampBase = outputConfiguration.getTimestampBase();
Shuzhen Wang610d7b82022-02-08 14:37:22 -0800889 int mirrorMode = outputConfiguration.getMirrorMode();
Austin Borger9e2b27c2022-07-15 11:27:24 -0700890 int32_t colorSpace = outputConfiguration.getColorSpace();
Shuzhen Wangbce53db2022-12-03 00:38:20 +0000891 bool useReadoutTimestamp = outputConfiguration.useReadoutTimestamp();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700892
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700893 res = SessionConfigurationUtils::checkSurfaceType(numBufferProducers, deferredConsumer,
Emilian Peev35ae8262018-11-08 13:11:32 +0000894 outputConfiguration.getSurfaceType());
895 if (!res.isOk()) {
896 return res;
Yin-Chia Yeh89f14da2014-06-10 16:05:44 -0700897 }
Zhijun He5d677d12016-05-29 16:52:39 -0700898
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800899 if (!mDevice.get()) {
900 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
901 }
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700902 res = SessionConfigurationUtils::checkPhysicalCameraId(mPhysicalCameraIds,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800903 physicalCameraId, mCameraIdStr);
Emilian Peev35ae8262018-11-08 13:11:32 +0000904 if (!res.isOk()) {
905 return res;
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800906 }
Emilian Peev35ae8262018-11-08 13:11:32 +0000907
Shuzhen Wang0129d522016-10-30 22:43:41 -0700908 std::vector<sp<Surface>> surfaces;
909 std::vector<sp<IBinder>> binders;
Zhijun He5d677d12016-05-29 16:52:39 -0700910 status_t err;
911
912 // Create stream for deferred surface case.
Shuzhen Wang0129d522016-10-30 22:43:41 -0700913 if (deferredConsumerOnly) {
Shuzhen Wang758c2152017-01-10 18:26:18 -0800914 return createDeferredSurfaceStreamLocked(outputConfiguration, isShared, newStreamId);
Zhijun He5d677d12016-05-29 16:52:39 -0700915 }
916
Shuzhen Wang758c2152017-01-10 18:26:18 -0800917 OutputStreamInfo streamInfo;
918 bool isStreamInfoValid = false;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800919 const std::vector<int32_t> &sensorPixelModesUsed =
920 outputConfiguration.getSensorPixelModesUsed();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700921 for (auto& bufferProducer : bufferProducers) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700922 // Don't create multiple streams for the same target surface
Shuzhen Wang0129d522016-10-30 22:43:41 -0700923 sp<IBinder> binder = IInterface::asBinder(bufferProducer);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800924 ssize_t index = mStreamMap.indexOfKey(binder);
925 if (index != NAME_NOT_FOUND) {
926 String8 msg = String8::format("Camera %s: Surface already has a stream created for it "
927 "(ID %zd)", mCameraIdStr.string(), index);
928 ALOGW("%s: %s", __FUNCTION__, msg.string());
929 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
Shuzhen Wang0129d522016-10-30 22:43:41 -0700930 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700931
Shuzhen Wang758c2152017-01-10 18:26:18 -0800932 sp<Surface> surface;
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700933 res = SessionConfigurationUtils::createSurfaceFromGbp(streamInfo,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800934 isStreamInfoValid, surface, bufferProducer, mCameraIdStr,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -0800935 mDevice->infoPhysical(physicalCameraId), sensorPixelModesUsed, dynamicRangeProfile,
Austin Borger9e2b27c2022-07-15 11:27:24 -0700936 streamUseCase, timestampBase, mirrorMode, colorSpace);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800937
938 if (!res.isOk())
939 return res;
940
941 if (!isStreamInfoValid) {
942 isStreamInfoValid = true;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700943 }
944
Shuzhen Wang758c2152017-01-10 18:26:18 -0800945 binders.push_back(IInterface::asBinder(bufferProducer));
Shuzhen Wang0129d522016-10-30 22:43:41 -0700946 surfaces.push_back(surface);
Ruben Brunkbba75572014-11-20 17:29:50 -0800947 }
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700948
949 // If mOverrideForPerfClass is true, do not fail createStream() for small
950 // JPEG sizes because existing createSurfaceFromGbp() logic will find the
951 // closest possible supported size.
952
Zhijun He125684a2015-12-26 15:07:30 -0800953 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
Emilian Peev40ead602017-09-26 15:46:36 +0100954 std::vector<int> surfaceIds;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800955 bool isDepthCompositeStream =
956 camera3::DepthCompositeStream::isDepthCompositeStream(surfaces[0]);
Chema Gonzalez67de9c82022-12-21 08:59:30 -0800957 bool isHeicCompositeStream = camera3::HeicCompositeStream::isHeicCompositeStream(surfaces[0]);
Emilian Peev434248e2022-10-06 14:58:54 -0700958 bool isJpegRCompositeStream =
Emilian Peeve579d8b2023-02-28 14:16:08 -0800959 camera3::JpegRCompositeStream::isJpegRCompositeStream(surfaces[0]) &&
Emilian Peev15230142023-04-27 20:22:54 +0000960 !mDevice->isCompositeJpegRDisabled();
Chema Gonzalez67de9c82022-12-21 08:59:30 -0800961 if (isDepthCompositeStream || isHeicCompositeStream || isJpegRCompositeStream) {
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800962 sp<CompositeStream> compositeStream;
963 if (isDepthCompositeStream) {
964 compositeStream = new camera3::DepthCompositeStream(mDevice, getRemoteCallback());
Chema Gonzalez67de9c82022-12-21 08:59:30 -0800965 } else if (isHeicCompositeStream) {
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800966 compositeStream = new camera3::HeicCompositeStream(mDevice, getRemoteCallback());
Emilian Peev434248e2022-10-06 14:58:54 -0700967 } else {
968 compositeStream = new camera3::JpegRCompositeStream(mDevice, getRemoteCallback());
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800969 }
970
Emilian Peev538c90e2018-12-17 18:03:19 +0000971 err = compositeStream->createStream(surfaces, deferredConsumer, streamInfo.width,
972 streamInfo.height, streamInfo.format,
Emilian Peevf4816702020-04-03 15:44:51 -0700973 static_cast<camera_stream_rotation_t>(outputConfiguration.getRotation()),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800974 &streamId, physicalCameraId, streamInfo.sensorPixelModesUsed, &surfaceIds,
Emilian Peev434248e2022-10-06 14:58:54 -0700975 outputConfiguration.getSurfaceSetID(), isShared, isMultiResolution,
Shuzhen Wangbce53db2022-12-03 00:38:20 +0000976 streamInfo.colorSpace, streamInfo.dynamicRangeProfile, streamInfo.streamUseCase,
977 useReadoutTimestamp);
Emilian Peev538c90e2018-12-17 18:03:19 +0000978 if (err == OK) {
Emilian Peev2f5d6012022-01-19 16:16:50 -0800979 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +0000980 mCompositeStreamMap.add(IInterface::asBinder(surfaces[0]->getIGraphicBufferProducer()),
981 compositeStream);
982 }
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800983 } else {
984 err = mDevice->createStream(surfaces, deferredConsumer, streamInfo.width,
985 streamInfo.height, streamInfo.format, streamInfo.dataSpace,
Emilian Peevf4816702020-04-03 15:44:51 -0700986 static_cast<camera_stream_rotation_t>(outputConfiguration.getRotation()),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800987 &streamId, physicalCameraId, streamInfo.sensorPixelModesUsed, &surfaceIds,
Emilian Peev2295df72021-11-12 18:14:10 -0800988 outputConfiguration.getSurfaceSetID(), isShared, isMultiResolution,
Shuzhen Wange4208922022-02-01 16:52:48 -0800989 /*consumerUsage*/0, streamInfo.dynamicRangeProfile, streamInfo.streamUseCase,
Shuzhen Wangbce53db2022-12-03 00:38:20 +0000990 streamInfo.timestampBase, streamInfo.mirrorMode, streamInfo.colorSpace,
991 useReadoutTimestamp);
Emilian Peev538c90e2018-12-17 18:03:19 +0000992 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700993
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800994 if (err != OK) {
995 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800996 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
Shuzhen Wang758c2152017-01-10 18:26:18 -0800997 mCameraIdStr.string(), streamInfo.width, streamInfo.height, streamInfo.format,
998 streamInfo.dataSpace, strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800999 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001000 int i = 0;
1001 for (auto& binder : binders) {
1002 ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %d",
1003 __FUNCTION__, binder.get(), streamId, i);
Emilian Peev40ead602017-09-26 15:46:36 +01001004 mStreamMap.add(binder, StreamSurfaceId(streamId, surfaceIds[i]));
1005 i++;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001006 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001007
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -08001008 mConfiguredOutputs.add(streamId, outputConfiguration);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001009 mStreamInfoMap[streamId] = streamInfo;
1010
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001011 ALOGV("%s: Camera %s: Successfully created a new stream ID %d for output surface"
Shuzhen Wang0129d522016-10-30 22:43:41 -07001012 " (%d x %d) with format 0x%x.",
Shuzhen Wang758c2152017-01-10 18:26:18 -08001013 __FUNCTION__, mCameraIdStr.string(), streamId, streamInfo.width,
1014 streamInfo.height, streamInfo.format);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001015
Zhijun He5d677d12016-05-29 16:52:39 -07001016 // Set transform flags to ensure preview to be rotated correctly.
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001017 res = setStreamTransformLocked(streamId, streamInfo.mirrorMode);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001018
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001019 // Fill in mHighResolutionCameraIdToStreamIdSet map
1020 const String8 &cameraIdUsed =
1021 physicalCameraId.size() != 0 ? physicalCameraId : mCameraIdStr;
1022 const char *cameraIdUsedCStr = cameraIdUsed.string();
1023 // Only needed for high resolution sensors
1024 if (mHighResolutionSensors.find(cameraIdUsedCStr) !=
1025 mHighResolutionSensors.end()) {
1026 mHighResolutionCameraIdToStreamIdSet[cameraIdUsedCStr].insert(streamId);
1027 }
1028
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001029 *newStreamId = streamId;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001030 }
1031
1032 return res;
1033}
1034
Zhijun He5d677d12016-05-29 16:52:39 -07001035binder::Status CameraDeviceClient::createDeferredSurfaceStreamLocked(
1036 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
Shuzhen Wang758c2152017-01-10 18:26:18 -08001037 bool isShared,
Zhijun He5d677d12016-05-29 16:52:39 -07001038 /*out*/
1039 int* newStreamId) {
1040 int width, height, format, surfaceType;
Emilian Peev050f5dc2017-05-18 14:43:56 +01001041 uint64_t consumerUsage;
Zhijun He5d677d12016-05-29 16:52:39 -07001042 android_dataspace dataSpace;
Austin Borger9e2b27c2022-07-15 11:27:24 -07001043 int32_t colorSpace;
Zhijun He5d677d12016-05-29 16:52:39 -07001044 status_t err;
1045 binder::Status res;
1046
1047 if (!mDevice.get()) {
1048 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1049 }
1050
1051 // Infer the surface info for deferred surface stream creation.
1052 width = outputConfiguration.getWidth();
1053 height = outputConfiguration.getHeight();
1054 surfaceType = outputConfiguration.getSurfaceType();
1055 format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
1056 dataSpace = android_dataspace_t::HAL_DATASPACE_UNKNOWN;
Austin Borger9e2b27c2022-07-15 11:27:24 -07001057 colorSpace = ANDROID_REQUEST_AVAILABLE_COLOR_SPACE_PROFILES_MAP_UNSPECIFIED;
Zhijun He5d677d12016-05-29 16:52:39 -07001058 // Hardcode consumer usage flags: SurfaceView--0x900, SurfaceTexture--0x100.
1059 consumerUsage = GraphicBuffer::USAGE_HW_TEXTURE;
1060 if (surfaceType == OutputConfiguration::SURFACE_TYPE_SURFACE_VIEW) {
1061 consumerUsage |= GraphicBuffer::USAGE_HW_COMPOSER;
1062 }
1063 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001064 std::vector<sp<Surface>> noSurface;
Emilian Peev40ead602017-09-26 15:46:36 +01001065 std::vector<int> surfaceIds;
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001066 String8 physicalCameraId(outputConfiguration.getPhysicalCameraId());
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001067 const String8 &cameraIdUsed =
1068 physicalCameraId.size() != 0 ? physicalCameraId : mCameraIdStr;
1069 // Here, we override sensor pixel modes
1070 std::unordered_set<int32_t> overriddenSensorPixelModesUsed;
1071 const std::vector<int32_t> &sensorPixelModesUsed =
1072 outputConfiguration.getSensorPixelModesUsed();
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07001073 if (SessionConfigurationUtils::checkAndOverrideSensorPixelModesUsed(
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001074 sensorPixelModesUsed, format, width, height, getStaticInfo(cameraIdUsed),
Jayant Chowdharya80ad242023-03-17 00:49:47 +00001075 &overriddenSensorPixelModesUsed) != OK) {
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001076 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1077 "sensor pixel modes used not valid for deferred stream");
1078 }
1079
Shuzhen Wang0129d522016-10-30 22:43:41 -07001080 err = mDevice->createStream(noSurface, /*hasDeferredConsumer*/true, width,
1081 height, format, dataSpace,
Emilian Peevf4816702020-04-03 15:44:51 -07001082 static_cast<camera_stream_rotation_t>(outputConfiguration.getRotation()),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001083 &streamId, physicalCameraId,
1084 overriddenSensorPixelModesUsed,
1085 &surfaceIds,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001086 outputConfiguration.getSurfaceSetID(), isShared,
Emilian Peev2295df72021-11-12 18:14:10 -08001087 outputConfiguration.isMultiResolution(), consumerUsage,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001088 outputConfiguration.getDynamicRangeProfile(),
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001089 outputConfiguration.getStreamUseCase(),
Shuzhen Wangbce53db2022-12-03 00:38:20 +00001090 outputConfiguration.getMirrorMode(),
1091 outputConfiguration.useReadoutTimestamp());
Zhijun He5d677d12016-05-29 16:52:39 -07001092
1093 if (err != OK) {
1094 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001095 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
1096 mCameraIdStr.string(), width, height, format, dataSpace, strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001097 } else {
1098 // Can not add streamId to mStreamMap here, as the surface is deferred. Add it to
1099 // a separate list to track. Once the deferred surface is set, this id will be
1100 // relocated to mStreamMap.
1101 mDeferredStreams.push_back(streamId);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001102 mStreamInfoMap.emplace(std::piecewise_construct, std::forward_as_tuple(streamId),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001103 std::forward_as_tuple(width, height, format, dataSpace, consumerUsage,
Emilian Peev2295df72021-11-12 18:14:10 -08001104 overriddenSensorPixelModesUsed,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001105 outputConfiguration.getDynamicRangeProfile(),
Shuzhen Wange4208922022-02-01 16:52:48 -08001106 outputConfiguration.getStreamUseCase(),
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001107 outputConfiguration.getTimestampBase(),
Austin Borger9e2b27c2022-07-15 11:27:24 -07001108 outputConfiguration.getMirrorMode(),
1109 colorSpace));
Shuzhen Wang758c2152017-01-10 18:26:18 -08001110
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001111 ALOGV("%s: Camera %s: Successfully created a new stream ID %d for a deferred surface"
Zhijun He5d677d12016-05-29 16:52:39 -07001112 " (%d x %d) stream with format 0x%x.",
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001113 __FUNCTION__, mCameraIdStr.string(), streamId, width, height, format);
Zhijun He5d677d12016-05-29 16:52:39 -07001114
1115 // Set transform flags to ensure preview to be rotated correctly.
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001116 res = setStreamTransformLocked(streamId, outputConfiguration.getMirrorMode());
Zhijun He5d677d12016-05-29 16:52:39 -07001117
1118 *newStreamId = streamId;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001119 // Fill in mHighResolutionCameraIdToStreamIdSet
1120 const char *cameraIdUsedCStr = cameraIdUsed.string();
1121 // Only needed for high resolution sensors
1122 if (mHighResolutionSensors.find(cameraIdUsedCStr) !=
1123 mHighResolutionSensors.end()) {
1124 mHighResolutionCameraIdToStreamIdSet[cameraIdUsed.string()].insert(streamId);
1125 }
Zhijun He5d677d12016-05-29 16:52:39 -07001126 }
1127 return res;
1128}
1129
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001130binder::Status CameraDeviceClient::setStreamTransformLocked(int streamId, int mirrorMode) {
Zhijun He5d677d12016-05-29 16:52:39 -07001131 int32_t transform = 0;
1132 status_t err;
1133 binder::Status res;
1134
1135 if (!mDevice.get()) {
1136 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1137 }
1138
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001139 err = getRotationTransformLocked(mirrorMode, &transform);
Zhijun He5d677d12016-05-29 16:52:39 -07001140 if (err != OK) {
1141 // Error logged by getRotationTransformLocked.
1142 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
1143 "Unable to calculate rotation transform for new stream");
1144 }
1145
1146 err = mDevice->setStreamTransform(streamId, transform);
1147 if (err != OK) {
1148 String8 msg = String8::format("Failed to set stream transform (stream id %d)",
1149 streamId);
1150 ALOGE("%s: %s", __FUNCTION__, msg.string());
1151 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
1152 }
1153
1154 return res;
1155}
Ruben Brunkbba75572014-11-20 17:29:50 -08001156
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001157binder::Status CameraDeviceClient::createInputStream(
Shuzhen Wang83bff122020-11-20 15:51:39 -08001158 int width, int height, int format, bool isMultiResolution,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001159 /*out*/
1160 int32_t* newStreamId) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001161
1162 ATRACE_CALL();
Shuzhen Wang83bff122020-11-20 15:51:39 -08001163 ALOGV("%s (w = %d, h = %d, f = 0x%x, isMultiResolution %d)", __FUNCTION__,
1164 width, height, format, isMultiResolution);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001165
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001166 binder::Status res;
1167 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001168
1169 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001170
1171 if (!mDevice.get()) {
1172 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1173 }
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001174
1175 if (mInputStream.configured) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001176 String8 msg = String8::format("Camera %s: Already has an input stream "
Yi Kong0f414de2017-12-15 13:48:50 -08001177 "configured (ID %d)", mCameraIdStr.string(), mInputStream.id);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001178 ALOGE("%s: %s", __FUNCTION__, msg.string() );
1179 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001180 }
1181
1182 int streamId = -1;
Shuzhen Wang83bff122020-11-20 15:51:39 -08001183 status_t err = mDevice->createInputStream(width, height, format, isMultiResolution, &streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001184 if (err == OK) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001185 mInputStream.configured = true;
1186 mInputStream.width = width;
1187 mInputStream.height = height;
1188 mInputStream.format = format;
1189 mInputStream.id = streamId;
1190
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001191 ALOGV("%s: Camera %s: Successfully created a new input stream ID %d",
1192 __FUNCTION__, mCameraIdStr.string(), streamId);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001193
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001194 *newStreamId = streamId;
1195 } else {
1196 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001197 "Camera %s: Error creating new input stream: %s (%d)", mCameraIdStr.string(),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001198 strerror(-err), err);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001199 }
1200
1201 return res;
1202}
1203
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001204binder::Status CameraDeviceClient::getInputSurface(/*out*/ view::Surface *inputSurface) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001205
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001206 binder::Status res;
1207 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1208
1209 if (inputSurface == NULL) {
1210 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Null input surface");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001211 }
1212
1213 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001214 if (!mDevice.get()) {
1215 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1216 }
1217 sp<IGraphicBufferProducer> producer;
1218 status_t err = mDevice->getInputBufferProducer(&producer);
1219 if (err != OK) {
1220 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001221 "Camera %s: Error getting input Surface: %s (%d)",
1222 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001223 } else {
1224 inputSurface->name = String16("CameraInput");
1225 inputSurface->graphicBufferProducer = producer;
1226 }
1227 return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001228}
1229
Emilian Peev40ead602017-09-26 15:46:36 +01001230binder::Status CameraDeviceClient::updateOutputConfiguration(int streamId,
1231 const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
1232 ATRACE_CALL();
1233
1234 binder::Status res;
1235 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1236
1237 Mutex::Autolock icl(mBinderSerializationLock);
1238
1239 if (!mDevice.get()) {
1240 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1241 }
1242
1243 const std::vector<sp<IGraphicBufferProducer> >& bufferProducers =
1244 outputConfiguration.getGraphicBufferProducers();
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -07001245 String8 physicalCameraId(outputConfiguration.getPhysicalCameraId());
1246
Emilian Peev40ead602017-09-26 15:46:36 +01001247 auto producerCount = bufferProducers.size();
1248 if (producerCount == 0) {
1249 ALOGE("%s: bufferProducers must not be empty", __FUNCTION__);
1250 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1251 "bufferProducers must not be empty");
1252 }
1253
1254 // The first output is the one associated with the output configuration.
1255 // It should always be present, valid and the corresponding stream id should match.
1256 sp<IBinder> binder = IInterface::asBinder(bufferProducers[0]);
1257 ssize_t index = mStreamMap.indexOfKey(binder);
1258 if (index == NAME_NOT_FOUND) {
1259 ALOGE("%s: Outputconfiguration is invalid", __FUNCTION__);
1260 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1261 "OutputConfiguration is invalid");
1262 }
1263 if (mStreamMap.valueFor(binder).streamId() != streamId) {
1264 ALOGE("%s: Stream Id: %d provided doesn't match the id: %d in the stream map",
1265 __FUNCTION__, streamId, mStreamMap.valueFor(binder).streamId());
1266 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1267 "Stream id is invalid");
1268 }
1269
1270 std::vector<size_t> removedSurfaceIds;
1271 std::vector<sp<IBinder>> removedOutputs;
1272 std::vector<sp<Surface>> newOutputs;
1273 std::vector<OutputStreamInfo> streamInfos;
1274 KeyedVector<sp<IBinder>, sp<IGraphicBufferProducer>> newOutputsMap;
1275 for (auto &it : bufferProducers) {
1276 newOutputsMap.add(IInterface::asBinder(it), it);
1277 }
1278
1279 for (size_t i = 0; i < mStreamMap.size(); i++) {
1280 ssize_t idx = newOutputsMap.indexOfKey(mStreamMap.keyAt(i));
1281 if (idx == NAME_NOT_FOUND) {
1282 if (mStreamMap[i].streamId() == streamId) {
1283 removedSurfaceIds.push_back(mStreamMap[i].surfaceId());
1284 removedOutputs.push_back(mStreamMap.keyAt(i));
1285 }
1286 } else {
1287 if (mStreamMap[i].streamId() != streamId) {
1288 ALOGE("%s: Output surface already part of a different stream", __FUNCTION__);
1289 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1290 "Target Surface is invalid");
1291 }
1292 newOutputsMap.removeItemsAt(idx);
1293 }
1294 }
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001295 const std::vector<int32_t> &sensorPixelModesUsed =
1296 outputConfiguration.getSensorPixelModesUsed();
Shuzhen Wang8ed1e872022-03-08 16:34:33 -08001297 int64_t streamUseCase = outputConfiguration.getStreamUseCase();
Shuzhen Wange4208922022-02-01 16:52:48 -08001298 int timestampBase = outputConfiguration.getTimestampBase();
Emilian Peevc81a7592022-02-14 17:38:18 -08001299 int64_t dynamicRangeProfile = outputConfiguration.getDynamicRangeProfile();
Austin Borger9e2b27c2022-07-15 11:27:24 -07001300 int32_t colorSpace = outputConfiguration.getColorSpace();
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001301 int mirrorMode = outputConfiguration.getMirrorMode();
Emilian Peev2295df72021-11-12 18:14:10 -08001302
Emilian Peev40ead602017-09-26 15:46:36 +01001303 for (size_t i = 0; i < newOutputsMap.size(); i++) {
1304 OutputStreamInfo outInfo;
1305 sp<Surface> surface;
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07001306 res = SessionConfigurationUtils::createSurfaceFromGbp(outInfo,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001307 /*isStreamInfoValid*/ false, surface, newOutputsMap.valueAt(i), mCameraIdStr,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001308 mDevice->infoPhysical(physicalCameraId), sensorPixelModesUsed, dynamicRangeProfile,
Austin Borger9e2b27c2022-07-15 11:27:24 -07001309 streamUseCase, timestampBase, mirrorMode, colorSpace);
Emilian Peev40ead602017-09-26 15:46:36 +01001310 if (!res.isOk())
1311 return res;
1312
Emilian Peev40ead602017-09-26 15:46:36 +01001313 streamInfos.push_back(outInfo);
1314 newOutputs.push_back(surface);
1315 }
1316
1317 //Trivial case no changes required
1318 if (removedSurfaceIds.empty() && newOutputs.empty()) {
1319 return binder::Status::ok();
1320 }
1321
1322 KeyedVector<sp<Surface>, size_t> outputMap;
1323 auto ret = mDevice->updateStream(streamId, newOutputs, streamInfos, removedSurfaceIds,
1324 &outputMap);
1325 if (ret != OK) {
1326 switch (ret) {
1327 case NAME_NOT_FOUND:
1328 case BAD_VALUE:
1329 case -EBUSY:
1330 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1331 "Camera %s: Error updating stream: %s (%d)",
1332 mCameraIdStr.string(), strerror(ret), ret);
1333 break;
1334 default:
1335 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1336 "Camera %s: Error updating stream: %s (%d)",
1337 mCameraIdStr.string(), strerror(ret), ret);
1338 break;
1339 }
1340 } else {
1341 for (const auto &it : removedOutputs) {
1342 mStreamMap.removeItem(it);
1343 }
1344
1345 for (size_t i = 0; i < outputMap.size(); i++) {
1346 mStreamMap.add(IInterface::asBinder(outputMap.keyAt(i)->getIGraphicBufferProducer()),
1347 StreamSurfaceId(streamId, outputMap.valueAt(i)));
1348 }
1349
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -08001350 mConfiguredOutputs.replaceValueFor(streamId, outputConfiguration);
1351
Emilian Peev40ead602017-09-26 15:46:36 +01001352 ALOGV("%s: Camera %s: Successful stream ID %d update",
1353 __FUNCTION__, mCameraIdStr.string(), streamId);
1354 }
1355
1356 return res;
1357}
1358
Igor Murashkine7ee7632013-06-11 18:10:18 -07001359// Create a request object from a template.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001360binder::Status CameraDeviceClient::createDefaultRequest(int templateId,
1361 /*out*/
1362 hardware::camera2::impl::CameraMetadataNative* request)
Igor Murashkine7ee7632013-06-11 18:10:18 -07001363{
1364 ATRACE_CALL();
1365 ALOGV("%s (templateId = 0x%x)", __FUNCTION__, templateId);
1366
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001367 binder::Status res;
1368 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001369
1370 Mutex::Autolock icl(mBinderSerializationLock);
1371
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001372 if (!mDevice.get()) {
1373 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1374 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001375
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001376 status_t err;
Emilian Peevf4816702020-04-03 15:44:51 -07001377 camera_request_template_t tempId = camera_request_template_t::CAMERA_TEMPLATE_COUNT;
1378 if (!(res = mapRequestTemplate(templateId, &tempId)).isOk()) return res;
1379
1380 CameraMetadata metadata;
1381 if ( (err = mDevice->createDefaultRequest(tempId, &metadata) ) == OK &&
Igor Murashkine7ee7632013-06-11 18:10:18 -07001382 request != NULL) {
1383
1384 request->swap(metadata);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001385 } else if (err == BAD_VALUE) {
1386 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001387 "Camera %s: Template ID %d is invalid or not supported: %s (%d)",
1388 mCameraIdStr.string(), templateId, strerror(-err), err);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001389
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001390 } else {
1391 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001392 "Camera %s: Error creating default request for template %d: %s (%d)",
1393 mCameraIdStr.string(), templateId, strerror(-err), err);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001394 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001395 return res;
1396}
1397
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001398binder::Status CameraDeviceClient::getCameraInfo(
1399 /*out*/
1400 hardware::camera2::impl::CameraMetadataNative* info)
Igor Murashkine7ee7632013-06-11 18:10:18 -07001401{
1402 ATRACE_CALL();
1403 ALOGV("%s", __FUNCTION__);
1404
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001405 binder::Status res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001406
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001407 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001408
1409 Mutex::Autolock icl(mBinderSerializationLock);
1410
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001411 if (!mDevice.get()) {
1412 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1413 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001414
Igor Murashkin099b4572013-07-12 17:52:16 -07001415 if (info != NULL) {
1416 *info = mDevice->info(); // static camera metadata
1417 // TODO: merge with device-specific camera metadata
1418 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001419
1420 return res;
1421}
1422
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001423binder::Status CameraDeviceClient::waitUntilIdle()
Zhijun He2ab500c2013-07-23 08:02:53 -07001424{
1425 ATRACE_CALL();
1426 ALOGV("%s", __FUNCTION__);
1427
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001428 binder::Status res;
1429 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Zhijun He2ab500c2013-07-23 08:02:53 -07001430
1431 Mutex::Autolock icl(mBinderSerializationLock);
1432
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001433 if (!mDevice.get()) {
1434 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1435 }
Zhijun He2ab500c2013-07-23 08:02:53 -07001436
1437 // FIXME: Also need check repeating burst.
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001438 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001439 if (mStreamingRequestId != REQUEST_ID_NONE) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001440 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001441 "Camera %s: Try to waitUntilIdle when there are active streaming requests",
1442 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001443 ALOGE("%s: %s", __FUNCTION__, msg.string());
1444 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Zhijun He2ab500c2013-07-23 08:02:53 -07001445 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001446 status_t err = mDevice->waitUntilDrained();
1447 if (err != OK) {
1448 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001449 "Camera %s: Error waiting to drain: %s (%d)",
1450 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001451 }
Zhijun He2ab500c2013-07-23 08:02:53 -07001452 ALOGV("%s Done", __FUNCTION__);
Zhijun He2ab500c2013-07-23 08:02:53 -07001453 return res;
1454}
1455
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001456binder::Status CameraDeviceClient::flush(
1457 /*out*/
1458 int64_t* lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001459 ATRACE_CALL();
1460 ALOGV("%s", __FUNCTION__);
1461
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001462 binder::Status res;
1463 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001464
1465 Mutex::Autolock icl(mBinderSerializationLock);
1466
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001467 if (!mDevice.get()) {
1468 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1469 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001470
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001471 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001472 mStreamingRequestId = REQUEST_ID_NONE;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001473 status_t err = mDevice->flush(lastFrameNumber);
1474 if (err != OK) {
1475 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001476 "Camera %s: Error flushing device: %s (%d)", mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001477 }
1478 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001479}
1480
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001481binder::Status CameraDeviceClient::prepare(int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001482 ATRACE_CALL();
Jayant Chowdhary57184d52023-02-14 20:54:39 +00001483 ALOGV("%s stream id %d", __FUNCTION__, streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001484
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001485 binder::Status res;
1486 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001487
1488 Mutex::Autolock icl(mBinderSerializationLock);
1489
1490 // Guard against trying to prepare non-created streams
1491 ssize_t index = NAME_NOT_FOUND;
1492 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001493 if (streamId == mStreamMap.valueAt(i).streamId()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001494 index = i;
1495 break;
1496 }
1497 }
1498
1499 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001500 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1501 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001502 ALOGW("%s: %s", __FUNCTION__, msg.string());
1503 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001504 }
1505
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001506 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1507 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001508 status_t err = mDevice->prepare(streamId);
1509 if (err == BAD_VALUE) {
1510 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001511 "Camera %s: Stream %d has already been used, and cannot be prepared",
1512 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001513 } else if (err != OK) {
1514 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001515 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001516 strerror(-err), err);
1517 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001518 return res;
1519}
1520
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001521binder::Status CameraDeviceClient::prepare2(int maxCount, int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001522 ATRACE_CALL();
Jayant Chowdhary57184d52023-02-14 20:54:39 +00001523 ALOGV("%s stream id %d", __FUNCTION__, streamId);
Ruben Brunkc78ac262015-08-13 17:58:46 -07001524
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001525 binder::Status res;
1526 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Ruben Brunkc78ac262015-08-13 17:58:46 -07001527
1528 Mutex::Autolock icl(mBinderSerializationLock);
1529
1530 // Guard against trying to prepare non-created streams
1531 ssize_t index = NAME_NOT_FOUND;
1532 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001533 if (streamId == mStreamMap.valueAt(i).streamId()) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001534 index = i;
1535 break;
1536 }
1537 }
1538
1539 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001540 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1541 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001542 ALOGW("%s: %s", __FUNCTION__, msg.string());
1543 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -07001544 }
1545
1546 if (maxCount <= 0) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001547 String8 msg = String8::format("Camera %s: maxCount (%d) must be greater than 0",
1548 mCameraIdStr.string(), maxCount);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001549 ALOGE("%s: %s", __FUNCTION__, msg.string());
1550 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -07001551 }
1552
1553 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1554 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001555 status_t err = mDevice->prepare(maxCount, streamId);
1556 if (err == BAD_VALUE) {
1557 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001558 "Camera %s: Stream %d has already been used, and cannot be prepared",
1559 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001560 } else if (err != OK) {
1561 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001562 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001563 strerror(-err), err);
1564 }
Ruben Brunkc78ac262015-08-13 17:58:46 -07001565
1566 return res;
1567}
1568
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001569binder::Status CameraDeviceClient::tearDown(int streamId) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001570 ATRACE_CALL();
1571 ALOGV("%s", __FUNCTION__);
1572
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001573 binder::Status res;
1574 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001575
1576 Mutex::Autolock icl(mBinderSerializationLock);
1577
1578 // Guard against trying to prepare non-created streams
1579 ssize_t index = NAME_NOT_FOUND;
1580 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001581 if (streamId == mStreamMap.valueAt(i).streamId()) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001582 index = i;
1583 break;
1584 }
1585 }
1586
1587 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001588 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1589 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001590 ALOGW("%s: %s", __FUNCTION__, msg.string());
1591 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001592 }
1593
1594 // Also returns BAD_VALUE if stream ID was not valid or if the stream is in
1595 // use
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001596 status_t err = mDevice->tearDown(streamId);
1597 if (err == BAD_VALUE) {
1598 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001599 "Camera %s: Stream %d is still in use, cannot be torn down",
1600 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001601 } else if (err != OK) {
1602 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001603 "Camera %s: Error tearing down stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001604 strerror(-err), err);
1605 }
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001606
1607 return res;
1608}
1609
Shuzhen Wang758c2152017-01-10 18:26:18 -08001610binder::Status CameraDeviceClient::finalizeOutputConfigurations(int32_t streamId,
Zhijun He5d677d12016-05-29 16:52:39 -07001611 const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
1612 ATRACE_CALL();
1613
1614 binder::Status res;
1615 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1616
1617 Mutex::Autolock icl(mBinderSerializationLock);
1618
Shuzhen Wang0129d522016-10-30 22:43:41 -07001619 const std::vector<sp<IGraphicBufferProducer> >& bufferProducers =
1620 outputConfiguration.getGraphicBufferProducers();
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -07001621 String8 physicalId(outputConfiguration.getPhysicalCameraId());
Zhijun He5d677d12016-05-29 16:52:39 -07001622
Shuzhen Wang0129d522016-10-30 22:43:41 -07001623 if (bufferProducers.size() == 0) {
1624 ALOGE("%s: bufferProducers must not be empty", __FUNCTION__);
Zhijun He5d677d12016-05-29 16:52:39 -07001625 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Target Surface is invalid");
1626 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001627
Shuzhen Wang758c2152017-01-10 18:26:18 -08001628 // streamId should be in mStreamMap if this stream already has a surface attached
1629 // to it. Otherwise, it should be in mDeferredStreams.
1630 bool streamIdConfigured = false;
1631 ssize_t deferredStreamIndex = NAME_NOT_FOUND;
1632 for (size_t i = 0; i < mStreamMap.size(); i++) {
1633 if (mStreamMap.valueAt(i).streamId() == streamId) {
1634 streamIdConfigured = true;
1635 break;
1636 }
Zhijun He5d677d12016-05-29 16:52:39 -07001637 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001638 for (size_t i = 0; i < mDeferredStreams.size(); i++) {
1639 if (streamId == mDeferredStreams[i]) {
1640 deferredStreamIndex = i;
1641 break;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001642 }
1643
Shuzhen Wang758c2152017-01-10 18:26:18 -08001644 }
1645 if (deferredStreamIndex == NAME_NOT_FOUND && !streamIdConfigured) {
1646 String8 msg = String8::format("Camera %s: deferred surface is set to a unknown stream"
1647 "(ID %d)", mCameraIdStr.string(), streamId);
1648 ALOGW("%s: %s", __FUNCTION__, msg.string());
1649 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Zhijun He5d677d12016-05-29 16:52:39 -07001650 }
1651
Shuzhen Wang88fd0052017-02-09 16:40:07 -08001652 if (mStreamInfoMap[streamId].finalized) {
1653 String8 msg = String8::format("Camera %s: finalizeOutputConfigurations has been called"
1654 " on stream ID %d", mCameraIdStr.string(), streamId);
1655 ALOGW("%s: %s", __FUNCTION__, msg.string());
1656 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1657 }
1658
Zhijun He5d677d12016-05-29 16:52:39 -07001659 if (!mDevice.get()) {
1660 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1661 }
1662
Shuzhen Wang758c2152017-01-10 18:26:18 -08001663 std::vector<sp<Surface>> consumerSurfaces;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001664 const std::vector<int32_t> &sensorPixelModesUsed =
1665 outputConfiguration.getSensorPixelModesUsed();
Emilian Peevc81a7592022-02-14 17:38:18 -08001666 int64_t dynamicRangeProfile = outputConfiguration.getDynamicRangeProfile();
Austin Borger9e2b27c2022-07-15 11:27:24 -07001667 int32_t colorSpace = outputConfiguration.getColorSpace();
1668 int64_t streamUseCase = outputConfiguration.getStreamUseCase();
Shuzhen Wange4208922022-02-01 16:52:48 -08001669 int timestampBase = outputConfiguration.getTimestampBase();
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001670 int mirrorMode = outputConfiguration.getMirrorMode();
Shuzhen Wang758c2152017-01-10 18:26:18 -08001671 for (auto& bufferProducer : bufferProducers) {
1672 // Don't create multiple streams for the same target surface
Zhijun He5d677d12016-05-29 16:52:39 -07001673 ssize_t index = mStreamMap.indexOfKey(IInterface::asBinder(bufferProducer));
1674 if (index != NAME_NOT_FOUND) {
Shuzhen Wang758c2152017-01-10 18:26:18 -08001675 ALOGV("Camera %s: Surface already has a stream created "
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001676 " for it (ID %zd)", mCameraIdStr.string(), index);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001677 continue;
Zhijun He5d677d12016-05-29 16:52:39 -07001678 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001679
1680 sp<Surface> surface;
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07001681 res = SessionConfigurationUtils::createSurfaceFromGbp(mStreamInfoMap[streamId],
Colin Crossb8a9dbb2020-08-27 04:12:26 +00001682 true /*isStreamInfoValid*/, surface, bufferProducer, mCameraIdStr,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001683 mDevice->infoPhysical(physicalId), sensorPixelModesUsed, dynamicRangeProfile,
Austin Borger9e2b27c2022-07-15 11:27:24 -07001684 streamUseCase, timestampBase, mirrorMode, colorSpace);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001685
1686 if (!res.isOk())
1687 return res;
1688
1689 consumerSurfaces.push_back(surface);
Zhijun He5d677d12016-05-29 16:52:39 -07001690 }
1691
Shuzhen Wanga81ce342017-04-13 15:18:36 -07001692 // Gracefully handle case where finalizeOutputConfigurations is called
1693 // without any new surface.
1694 if (consumerSurfaces.size() == 0) {
1695 mStreamInfoMap[streamId].finalized = true;
1696 return res;
1697 }
1698
Zhijun He5d677d12016-05-29 16:52:39 -07001699 // Finish the deferred stream configuration with the surface.
Shuzhen Wang758c2152017-01-10 18:26:18 -08001700 status_t err;
Emilian Peev40ead602017-09-26 15:46:36 +01001701 std::vector<int> consumerSurfaceIds;
1702 err = mDevice->setConsumerSurfaces(streamId, consumerSurfaces, &consumerSurfaceIds);
Zhijun He5d677d12016-05-29 16:52:39 -07001703 if (err == OK) {
Shuzhen Wang758c2152017-01-10 18:26:18 -08001704 for (size_t i = 0; i < consumerSurfaces.size(); i++) {
1705 sp<IBinder> binder = IInterface::asBinder(
1706 consumerSurfaces[i]->getIGraphicBufferProducer());
Emilian Peev40ead602017-09-26 15:46:36 +01001707 ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %d", __FUNCTION__,
Shuzhen Wang758c2152017-01-10 18:26:18 -08001708 binder.get(), streamId, consumerSurfaceIds[i]);
1709 mStreamMap.add(binder, StreamSurfaceId(streamId, consumerSurfaceIds[i]));
1710 }
1711 if (deferredStreamIndex != NAME_NOT_FOUND) {
1712 mDeferredStreams.removeItemsAt(deferredStreamIndex);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001713 }
Shuzhen Wang88fd0052017-02-09 16:40:07 -08001714 mStreamInfoMap[streamId].finalized = true;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -08001715 mConfiguredOutputs.replaceValueFor(streamId, outputConfiguration);
Zhijun He5d677d12016-05-29 16:52:39 -07001716 } else if (err == NO_INIT) {
1717 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001718 "Camera %s: Deferred surface is invalid: %s (%d)",
1719 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001720 } else {
1721 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001722 "Camera %s: Error setting output stream deferred surface: %s (%d)",
1723 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001724 }
1725
1726 return res;
1727}
1728
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001729binder::Status CameraDeviceClient::setCameraAudioRestriction(int32_t mode) {
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001730 ATRACE_CALL();
1731 binder::Status res;
1732 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1733
1734 if (!isValidAudioRestriction(mode)) {
1735 String8 msg = String8::format("Camera %s: invalid audio restriction mode %d",
1736 mCameraIdStr.string(), mode);
1737 ALOGW("%s: %s", __FUNCTION__, msg.string());
1738 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1739 }
1740
1741 Mutex::Autolock icl(mBinderSerializationLock);
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001742 BasicClient::setAudioRestriction(mode);
1743 return binder::Status::ok();
1744}
1745
1746binder::Status CameraDeviceClient::getGlobalAudioRestriction(/*out*/ int32_t* outMode) {
1747 ATRACE_CALL();
1748 binder::Status res;
1749 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1750 Mutex::Autolock icl(mBinderSerializationLock);
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001751 if (outMode != nullptr) {
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001752 *outMode = BasicClient::getServiceAudioRestriction();
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001753 }
1754 return binder::Status::ok();
1755}
1756
Ravneetaeb20dc2022-03-30 05:33:03 +00001757status_t CameraDeviceClient::setCameraServiceWatchdog(bool enabled) {
1758 return mDevice->setCameraServiceWatchdog(enabled);
1759}
1760
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08001761status_t CameraDeviceClient::setRotateAndCropOverride(uint8_t rotateAndCrop) {
1762 if (rotateAndCrop > ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return BAD_VALUE;
1763
1764 return mDevice->setRotateAndCropAutoBehavior(
1765 static_cast<camera_metadata_enum_android_scaler_rotate_and_crop_t>(rotateAndCrop));
1766}
1767
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00001768status_t CameraDeviceClient::setAutoframingOverride(uint8_t autoframingValue) {
1769 if (autoframingValue > ANDROID_CONTROL_AUTOFRAMING_AUTO) return BAD_VALUE;
1770
1771 return mDevice->setAutoframingAutoBehavior(
1772 static_cast<camera_metadata_enum_android_control_autoframing_t>(autoframingValue));
1773}
1774
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08001775bool CameraDeviceClient::supportsCameraMute() {
1776 return mDevice->supportsCameraMute();
1777}
1778
1779status_t CameraDeviceClient::setCameraMute(bool enabled) {
1780 return mDevice->setCameraMute(enabled);
1781}
1782
Shuzhen Wang16610a62022-12-15 22:38:07 -08001783void CameraDeviceClient::setStreamUseCaseOverrides(
1784 const std::vector<int64_t>& useCaseOverrides) {
1785 mDevice->setStreamUseCaseOverrides(useCaseOverrides);
1786}
1787
1788void CameraDeviceClient::clearStreamUseCaseOverrides() {
1789 mDevice->clearStreamUseCaseOverrides();
1790}
1791
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001792binder::Status CameraDeviceClient::switchToOffline(
1793 const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb,
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001794 const std::vector<int>& offlineOutputIds,
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001795 /*out*/
1796 sp<hardware::camera2::ICameraOfflineSession>* session) {
1797 ATRACE_CALL();
1798
1799 binder::Status res;
1800 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1801
1802 Mutex::Autolock icl(mBinderSerializationLock);
1803
1804 if (!mDevice.get()) {
1805 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1806 }
1807
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001808 if (offlineOutputIds.empty()) {
Emilian Peevcc0b7952020-01-07 13:54:47 -08001809 String8 msg = String8::format("Offline surfaces must not be empty");
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001810 ALOGE("%s: %s", __FUNCTION__, msg.string());
1811 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1812 }
1813
1814 if (session == nullptr) {
1815 String8 msg = String8::format("Invalid offline session");
1816 ALOGE("%s: %s", __FUNCTION__, msg.string());
1817 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1818 }
1819
Yin-Chia Yeh87fccca2020-01-28 09:37:18 -08001820 std::vector<int32_t> offlineStreamIds;
1821 offlineStreamIds.reserve(offlineOutputIds.size());
Emilian Peev4697b642019-11-19 17:11:14 -08001822 KeyedVector<sp<IBinder>, sp<CompositeStream>> offlineCompositeStreamMap;
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001823 for (const auto& streamId : offlineOutputIds) {
1824 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001825 if (index == NAME_NOT_FOUND) {
Emilian Peevcc0b7952020-01-07 13:54:47 -08001826 String8 msg = String8::format("Offline surface with id: %d is not registered",
1827 streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001828 ALOGE("%s: %s", __FUNCTION__, msg.string());
1829 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1830 }
Emilian Peevcc0b7952020-01-07 13:54:47 -08001831
1832 if (!mStreamInfoMap[streamId].supportsOffline) {
1833 String8 msg = String8::format("Offline surface with id: %d doesn't support "
1834 "offline mode", streamId);
1835 ALOGE("%s: %s", __FUNCTION__, msg.string());
1836 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1837 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001838
Emilian Peev2f5d6012022-01-19 16:16:50 -08001839 Mutex::Autolock l(mCompositeLock);
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001840 bool isCompositeStream = false;
Emilian Peev2f5d6012022-01-19 16:16:50 -08001841 for (const auto& gbp : mConfiguredOutputs.valueAt(index).getGraphicBufferProducers()) {
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001842 sp<Surface> s = new Surface(gbp, false /*controlledByApp*/);
Pirama Arumuga Nainar8db21062022-01-28 10:15:12 -08001843 isCompositeStream = camera3::DepthCompositeStream::isDepthCompositeStream(s) ||
Emilian Peev434248e2022-10-06 14:58:54 -07001844 camera3::HeicCompositeStream::isHeicCompositeStream(s) ||
Emilian Peeve579d8b2023-02-28 14:16:08 -08001845 (camera3::JpegRCompositeStream::isJpegRCompositeStream(s) &&
Emilian Peev15230142023-04-27 20:22:54 +00001846 !mDevice->isCompositeJpegRDisabled());
Emilian Peev4697b642019-11-19 17:11:14 -08001847 if (isCompositeStream) {
1848 auto compositeIdx = mCompositeStreamMap.indexOfKey(IInterface::asBinder(gbp));
1849 if (compositeIdx == NAME_NOT_FOUND) {
1850 ALOGE("%s: Unknown composite stream", __FUNCTION__);
1851 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1852 "Unknown composite stream");
1853 }
1854
1855 mCompositeStreamMap.valueAt(compositeIdx)->insertCompositeStreamIds(
1856 &offlineStreamIds);
1857 offlineCompositeStreamMap.add(mCompositeStreamMap.keyAt(compositeIdx),
1858 mCompositeStreamMap.valueAt(compositeIdx));
1859 break;
1860 }
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001861 }
1862
Emilian Peev4697b642019-11-19 17:11:14 -08001863 if (!isCompositeStream) {
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001864 offlineStreamIds.push_back(streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001865 }
1866 }
1867
1868 sp<CameraOfflineSessionBase> offlineSession;
1869 auto ret = mDevice->switchToOffline(offlineStreamIds, &offlineSession);
1870 if (ret != OK) {
1871 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1872 "Camera %s: Error switching to offline mode: %s (%d)",
1873 mCameraIdStr.string(), strerror(ret), ret);
1874 }
1875
Emilian Peevcc0b7952020-01-07 13:54:47 -08001876 sp<CameraOfflineSessionClient> offlineClient;
1877 if (offlineSession.get() != nullptr) {
1878 offlineClient = new CameraOfflineSessionClient(sCameraService,
1879 offlineSession, offlineCompositeStreamMap, cameraCb, mClientPackageName,
Emilian Peev8b64f282021-03-25 16:49:57 -07001880 mClientFeatureId, mCameraIdStr, mCameraFacing, mOrientation, mClientPid, mClientUid,
1881 mServicePid);
Emilian Peevcc0b7952020-01-07 13:54:47 -08001882 ret = sCameraService->addOfflineClient(mCameraIdStr, offlineClient);
1883 }
1884
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001885 if (ret == OK) {
Emilian Peevd99c8ae2019-11-26 13:19:13 -08001886 // A successful offline session switch must reset the current camera client
1887 // and release any resources occupied by previously configured streams.
1888 mStreamMap.clear();
1889 mConfiguredOutputs.clear();
1890 mDeferredStreams.clear();
1891 mStreamInfoMap.clear();
Emilian Peev2f5d6012022-01-19 16:16:50 -08001892 Mutex::Autolock l(mCompositeLock);
Emilian Peevd99c8ae2019-11-26 13:19:13 -08001893 mCompositeStreamMap.clear();
1894 mInputStream = {false, 0, 0, 0, 0};
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001895 } else {
Emilian Peev886ac212023-02-07 14:10:24 -08001896 // In case we failed to register the offline client, ensure that it still initialized
1897 // so that all failing requests can return back correctly once the object is released.
1898 offlineClient->initialize(nullptr /*cameraProviderManager*/, String8()/*monitorTags*/);
1899
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001900 switch(ret) {
1901 case BAD_VALUE:
1902 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1903 "Illegal argument to HAL module for camera \"%s\"", mCameraIdStr.c_str());
1904 case TIMED_OUT:
1905 return STATUS_ERROR_FMT(CameraService::ERROR_CAMERA_IN_USE,
1906 "Camera \"%s\" is already open", mCameraIdStr.c_str());
1907 default:
1908 return STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1909 "Failed to initialize camera \"%s\": %s (%d)", mCameraIdStr.c_str(),
1910 strerror(-ret), ret);
1911 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001912 }
1913
1914 *session = offlineClient;
1915
1916 return binder::Status::ok();
1917}
1918
Igor Murashkine7ee7632013-06-11 18:10:18 -07001919status_t CameraDeviceClient::dump(int fd, const Vector<String16>& args) {
Eino-Ville Talvalac4003962016-01-13 10:07:04 -08001920 return BasicClient::dump(fd, args);
1921}
1922
1923status_t CameraDeviceClient::dumpClient(int fd, const Vector<String16>& args) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001924 dprintf(fd, " CameraDeviceClient[%s] (%p) dump:\n",
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001925 mCameraIdStr.string(),
Eino-Ville Talvalae992e752014-11-07 16:17:48 -08001926 (getRemoteCallback() != NULL ?
Marco Nelissenf8880202014-11-14 07:58:25 -08001927 IInterface::asBinder(getRemoteCallback()).get() : NULL) );
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001928 dprintf(fd, " Current client UID %u\n", mClientUid);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001929
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001930 dprintf(fd, " State:\n");
1931 dprintf(fd, " Request ID counter: %d\n", mRequestIdCounter);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001932 if (mInputStream.configured) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001933 dprintf(fd, " Current input stream ID: %d\n", mInputStream.id);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001934 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001935 dprintf(fd, " No input stream configured.\n");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001936 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001937 if (!mStreamMap.isEmpty()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001938 dprintf(fd, " Current output stream/surface IDs:\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001939 for (size_t i = 0; i < mStreamMap.size(); i++) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001940 dprintf(fd, " Stream %d Surface %d\n",
Shuzhen Wang0129d522016-10-30 22:43:41 -07001941 mStreamMap.valueAt(i).streamId(),
1942 mStreamMap.valueAt(i).surfaceId());
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001943 }
Zhijun He5d677d12016-05-29 16:52:39 -07001944 } else if (!mDeferredStreams.isEmpty()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001945 dprintf(fd, " Current deferred surface output stream IDs:\n");
Zhijun He5d677d12016-05-29 16:52:39 -07001946 for (auto& streamId : mDeferredStreams) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001947 dprintf(fd, " Stream %d\n", streamId);
Zhijun He5d677d12016-05-29 16:52:39 -07001948 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001949 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001950 dprintf(fd, " No output streams configured.\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001951 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001952 // TODO: print dynamic/request section from most recent requests
1953 mFrameProcessor->dump(fd, args);
1954
1955 return dumpDevice(fd, args);
1956}
1957
Avichal Rakesh7e53cad2021-10-05 13:46:30 -07001958status_t CameraDeviceClient::startWatchingTags(const String8 &tags, int out) {
1959 sp<CameraDeviceBase> device = mDevice;
1960 if (!device) {
1961 dprintf(out, " Device is detached.");
1962 return OK;
1963 }
1964 device->startWatchingTags(tags);
1965 return OK;
1966}
1967
1968status_t CameraDeviceClient::stopWatchingTags(int out) {
1969 sp<CameraDeviceBase> device = mDevice;
1970 if (!device) {
1971 dprintf(out, " Device is detached.");
1972 return OK;
1973 }
1974 device->stopWatchingTags();
1975 return OK;
1976}
1977
1978status_t CameraDeviceClient::dumpWatchedEventsToVector(std::vector<std::string> &out) {
1979 sp<CameraDeviceBase> device = mDevice;
1980 if (!device) {
1981 return OK;
1982 }
1983 device->dumpWatchedEventsToVector(out);
1984 return OK;
1985}
1986
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001987void CameraDeviceClient::notifyError(int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -07001988 const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001989 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001990 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001991
Emilian Peev538c90e2018-12-17 18:03:19 +00001992 bool skipClientNotification = false;
Emilian Peev2f5d6012022-01-19 16:16:50 -08001993 {
1994 // Access to the composite stream map must be synchronized
1995 Mutex::Autolock l(mCompositeLock);
1996 // Composites can have multiple internal streams. Error notifications coming from such
1997 // internal streams may need to remain within camera service.
1998 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
1999 skipClientNotification |= mCompositeStreamMap.valueAt(i)->onError(errorCode,
2000 resultExtras);
2001 }
Emilian Peev538c90e2018-12-17 18:03:19 +00002002 }
2003
2004 if ((remoteCb != 0) && (!skipClientNotification)) {
Jianing Weicb0652e2014-03-12 18:29:36 -07002005 remoteCb->onDeviceError(errorCode, resultExtras);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002006 }
2007}
2008
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07002009void CameraDeviceClient::notifyRepeatingRequestError(long lastFrameNumber) {
2010 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
2011
2012 if (remoteCb != 0) {
Yin-Chia Yeh8ca23dc2017-09-05 18:15:56 -07002013 remoteCb->onRepeatingRequestError(lastFrameNumber, mStreamingRequestId);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07002014 }
2015
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07002016 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07002017 mStreamingRequestId = REQUEST_ID_NONE;
2018}
2019
Shuzhen Wang316781a2020-08-18 18:11:01 -07002020void CameraDeviceClient::notifyIdle(
2021 int64_t requestCount, int64_t resultErrorCount, bool deviceError,
2022 const std::vector<hardware::CameraStreamStats>& streamStats) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002023 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002024 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002025
2026 if (remoteCb != 0) {
2027 remoteCb->onDeviceIdle();
2028 }
Emilian Peev567c31c2023-03-06 15:02:37 -08002029
2030 std::vector<hardware::CameraStreamStats> fullStreamStats = streamStats;
2031 {
2032 Mutex::Autolock l(mCompositeLock);
2033 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
2034 hardware::CameraStreamStats compositeStats;
2035 mCompositeStreamMap.valueAt(i)->getStreamStats(&compositeStats);
2036 if (compositeStats.mWidth > 0) {
2037 fullStreamStats.push_back(compositeStats);
2038 }
2039 }
2040 }
Shuzhen Wangd26b1862022-03-07 12:05:05 -08002041 Camera2ClientBase::notifyIdleWithUserTag(requestCount, resultErrorCount, deviceError,
Emilian Peev567c31c2023-03-06 15:02:37 -08002042 fullStreamStats, mUserTag, mVideoStabilizationMode);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002043}
2044
Jianing Weicb0652e2014-03-12 18:29:36 -07002045void CameraDeviceClient::notifyShutter(const CaptureResultExtras& resultExtras,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002046 nsecs_t timestamp) {
2047 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002048 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002049 if (remoteCb != 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -07002050 remoteCb->onCaptureStarted(resultExtras, timestamp);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002051 }
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07002052 Camera2ClientBase::notifyShutter(resultExtras, timestamp);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08002053
Emilian Peev2f5d6012022-01-19 16:16:50 -08002054 // Access to the composite stream map must be synchronized
2055 Mutex::Autolock l(mCompositeLock);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08002056 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
2057 mCompositeStreamMap.valueAt(i)->onShutter(resultExtras, timestamp);
2058 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002059}
2060
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002061void CameraDeviceClient::notifyPrepared(int streamId) {
2062 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002063 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002064 if (remoteCb != 0) {
Jayant Chowdhary57184d52023-02-14 20:54:39 +00002065 ALOGV("%s: stream id %d", __FUNCTION__, streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002066 remoteCb->onPrepared(streamId);
2067 }
2068}
2069
Shuzhen Wang9d066012016-09-30 11:30:20 -07002070void CameraDeviceClient::notifyRequestQueueEmpty() {
2071 // Thread safe. Don't bother locking.
2072 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
2073 if (remoteCb != 0) {
2074 remoteCb->onRequestQueueEmpty();
2075 }
2076}
2077
Igor Murashkine7ee7632013-06-11 18:10:18 -07002078void CameraDeviceClient::detachDevice() {
2079 if (mDevice == 0) return;
2080
Shuzhen Wang316781a2020-08-18 18:11:01 -07002081 nsecs_t startTime = systemTime();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002082 ALOGV("Camera %s: Stopping processors", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -07002083
Emilian Peev7ee731f2022-02-08 14:55:52 -08002084 if (mFrameProcessor.get() != nullptr) {
2085 mFrameProcessor->removeListener(
2086 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MIN_ID,
2087 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MAX_ID, /*listener*/this);
2088 mFrameProcessor->requestExit();
2089 ALOGV("Camera %s: Waiting for threads", mCameraIdStr.string());
2090 mFrameProcessor->join();
2091 ALOGV("Camera %s: Disconnecting device", mCameraIdStr.string());
2092 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07002093
2094 // WORKAROUND: HAL refuses to disconnect while there's streams in flight
2095 {
Emilian Peev6b51d7d2018-07-23 11:41:44 +01002096 int64_t lastFrameNumber;
Igor Murashkine7ee7632013-06-11 18:10:18 -07002097 status_t code;
Emilian Peev6b51d7d2018-07-23 11:41:44 +01002098 if ((code = mDevice->flush(&lastFrameNumber)) != OK) {
2099 ALOGE("%s: flush failed with code 0x%x", __FUNCTION__, code);
2100 }
2101
Igor Murashkine7ee7632013-06-11 18:10:18 -07002102 if ((code = mDevice->waitUntilDrained()) != OK) {
2103 ALOGE("%s: waitUntilDrained failed with code 0x%x", __FUNCTION__,
2104 code);
2105 }
2106 }
2107
Emilian Peev2f5d6012022-01-19 16:16:50 -08002108 {
2109 Mutex::Autolock l(mCompositeLock);
2110 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
2111 auto ret = mCompositeStreamMap.valueAt(i)->deleteInternalStreams();
2112 if (ret != OK) {
2113 ALOGE("%s: Failed removing composite stream %s (%d)", __FUNCTION__,
2114 strerror(-ret), ret);
2115 }
Emilian Peev5e4c7322019-10-22 14:20:52 -07002116 }
Emilian Peev2f5d6012022-01-19 16:16:50 -08002117 mCompositeStreamMap.clear();
Emilian Peev5e4c7322019-10-22 14:20:52 -07002118 }
Emilian Peev5e4c7322019-10-22 14:20:52 -07002119
Shuzhen Wang03fe6232023-02-05 12:41:15 -08002120 bool hasDeviceError = mDevice->hasDeviceError();
Igor Murashkine7ee7632013-06-11 18:10:18 -07002121 Camera2ClientBase::detachDevice();
Shuzhen Wang316781a2020-08-18 18:11:01 -07002122
2123 int32_t closeLatencyMs = ns2ms(systemTime() - startTime);
Shuzhen Wang03fe6232023-02-05 12:41:15 -08002124 mCameraServiceProxyWrapper->logClose(mCameraIdStr, closeLatencyMs, hasDeviceError);
Igor Murashkine7ee7632013-06-11 18:10:18 -07002125}
2126
2127/** Device-related methods */
Jianing Weicb0652e2014-03-12 18:29:36 -07002128void CameraDeviceClient::onResultAvailable(const CaptureResult& result) {
Igor Murashkine7ee7632013-06-11 18:10:18 -07002129 ATRACE_CALL();
2130 ALOGV("%s", __FUNCTION__);
2131
Igor Murashkin4fb55c12013-08-29 17:43:01 -07002132 // Thread-safe. No lock necessary.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002133 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = mRemoteCallback;
Igor Murashkin4fb55c12013-08-29 17:43:01 -07002134 if (remoteCb != NULL) {
Shuzhen Wang5c22c152017-12-31 17:12:25 -08002135 remoteCb->onResultReceived(result.mMetadata, result.mResultExtras,
2136 result.mPhysicalMetadatas);
Igor Murashkine7ee7632013-06-11 18:10:18 -07002137 }
Emilian Peev538c90e2018-12-17 18:03:19 +00002138
Emilian Peev2f5d6012022-01-19 16:16:50 -08002139 // Access to the composite stream map must be synchronized
2140 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +00002141 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
2142 mCompositeStreamMap.valueAt(i)->onResultAvailable(result);
2143 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07002144}
2145
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002146binder::Status CameraDeviceClient::checkPidStatus(const char* checkLocation) {
Eino-Ville Talvala6192b892016-04-04 12:31:18 -07002147 if (mDisconnected) {
2148 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED,
2149 "The camera device has been disconnected");
2150 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002151 status_t res = checkPid(checkLocation);
2152 return (res == OK) ? binder::Status::ok() :
2153 STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
2154 "Attempt to use camera from a different process than original client");
2155}
2156
Igor Murashkine7ee7632013-06-11 18:10:18 -07002157// TODO: move to Camera2ClientBase
2158bool CameraDeviceClient::enforceRequestPermissions(CameraMetadata& metadata) {
2159
Jayant Chowdhary12361932018-08-27 14:46:13 -07002160 const int pid = CameraThreadState::getCallingPid();
Igor Murashkine7ee7632013-06-11 18:10:18 -07002161 const int selfPid = getpid();
2162 camera_metadata_entry_t entry;
2163
2164 /**
2165 * Mixin default important security values
2166 * - android.led.transmit = defaulted ON
2167 */
2168 CameraMetadata staticInfo = mDevice->info();
2169 entry = staticInfo.find(ANDROID_LED_AVAILABLE_LEDS);
2170 for(size_t i = 0; i < entry.count; ++i) {
2171 uint8_t led = entry.data.u8[i];
2172
2173 switch(led) {
2174 case ANDROID_LED_AVAILABLE_LEDS_TRANSMIT: {
2175 uint8_t transmitDefault = ANDROID_LED_TRANSMIT_ON;
2176 if (!metadata.exists(ANDROID_LED_TRANSMIT)) {
2177 metadata.update(ANDROID_LED_TRANSMIT,
2178 &transmitDefault, 1);
2179 }
2180 break;
2181 }
2182 }
2183 }
2184
2185 // We can do anything!
2186 if (pid == selfPid) {
2187 return true;
2188 }
2189
2190 /**
2191 * Permission check special fields in the request
2192 * - android.led.transmit = android.permission.CAMERA_DISABLE_TRANSMIT
2193 */
2194 entry = metadata.find(ANDROID_LED_TRANSMIT);
2195 if (entry.count > 0 && entry.data.u8[0] != ANDROID_LED_TRANSMIT_ON) {
2196 String16 permissionString =
2197 String16("android.permission.CAMERA_DISABLE_TRANSMIT_LED");
2198 if (!checkCallingPermission(permissionString)) {
Jayant Chowdhary12361932018-08-27 14:46:13 -07002199 const int uid = CameraThreadState::getCallingUid();
Igor Murashkine7ee7632013-06-11 18:10:18 -07002200 ALOGE("Permission Denial: "
2201 "can't disable transmit LED pid=%d, uid=%d", pid, uid);
2202 return false;
2203 }
2204 }
2205
2206 return true;
2207}
2208
Shuzhen Wang610d7b82022-02-08 14:37:22 -08002209status_t CameraDeviceClient::getRotationTransformLocked(int mirrorMode,
2210 int32_t* transform) {
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07002211 ALOGV("%s: begin", __FUNCTION__);
2212
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07002213 const CameraMetadata& staticInfo = mDevice->info();
Shuzhen Wang610d7b82022-02-08 14:37:22 -08002214 return CameraUtils::getRotationTransform(staticInfo, mirrorMode, transform);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07002215}
2216
Emilian Peevf4816702020-04-03 15:44:51 -07002217binder::Status CameraDeviceClient::mapRequestTemplate(int templateId,
2218 camera_request_template_t* tempId /*out*/) {
2219 binder::Status ret = binder::Status::ok();
2220
2221 if (tempId == nullptr) {
2222 ret = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
2223 "Camera %s: Invalid template argument", mCameraIdStr.string());
2224 return ret;
2225 }
2226 switch(templateId) {
2227 case ICameraDeviceUser::TEMPLATE_PREVIEW:
2228 *tempId = camera_request_template_t::CAMERA_TEMPLATE_PREVIEW;
2229 break;
2230 case ICameraDeviceUser::TEMPLATE_RECORD:
2231 *tempId = camera_request_template_t::CAMERA_TEMPLATE_VIDEO_RECORD;
2232 break;
2233 case ICameraDeviceUser::TEMPLATE_STILL_CAPTURE:
2234 *tempId = camera_request_template_t::CAMERA_TEMPLATE_STILL_CAPTURE;
2235 break;
2236 case ICameraDeviceUser::TEMPLATE_VIDEO_SNAPSHOT:
2237 *tempId = camera_request_template_t::CAMERA_TEMPLATE_VIDEO_SNAPSHOT;
2238 break;
2239 case ICameraDeviceUser::TEMPLATE_ZERO_SHUTTER_LAG:
2240 *tempId = camera_request_template_t::CAMERA_TEMPLATE_ZERO_SHUTTER_LAG;
2241 break;
2242 case ICameraDeviceUser::TEMPLATE_MANUAL:
2243 *tempId = camera_request_template_t::CAMERA_TEMPLATE_MANUAL;
2244 break;
2245 default:
2246 ret = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
2247 "Camera %s: Template ID %d is invalid or not supported",
2248 mCameraIdStr.string(), templateId);
2249 return ret;
2250 }
2251
2252 return ret;
2253}
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08002254
2255const CameraMetadata &CameraDeviceClient::getStaticInfo(const String8 &cameraId) {
2256 if (mDevice->getId() == cameraId) {
2257 return mDevice->info();
2258 }
2259 return mDevice->infoPhysical(cameraId);
2260}
2261
Jayant Chowdharydbd1efb2023-02-07 16:14:48 -08002262bool CameraDeviceClient::supportsUltraHighResolutionCapture(const String8 &cameraId) {
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08002263 const CameraMetadata &deviceInfo = getStaticInfo(cameraId);
Jayant Chowdharydbd1efb2023-02-07 16:14:48 -08002264 return SessionConfigurationUtils::supportsUltraHighResolutionCapture(deviceInfo);
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08002265}
2266
2267bool CameraDeviceClient::isSensorPixelModeConsistent(
2268 const std::list<int> &streamIdList, const CameraMetadata &settings) {
2269 // First we get the sensorPixelMode from the settings metadata.
2270 int32_t sensorPixelMode = ANDROID_SENSOR_PIXEL_MODE_DEFAULT;
2271 camera_metadata_ro_entry sensorPixelModeEntry = settings.find(ANDROID_SENSOR_PIXEL_MODE);
2272 if (sensorPixelModeEntry.count != 0) {
2273 sensorPixelMode = sensorPixelModeEntry.data.u8[0];
2274 if (sensorPixelMode != ANDROID_SENSOR_PIXEL_MODE_DEFAULT &&
2275 sensorPixelMode != ANDROID_SENSOR_PIXEL_MODE_MAXIMUM_RESOLUTION) {
2276 ALOGE("%s: Request sensor pixel mode not is not one of the valid values %d",
2277 __FUNCTION__, sensorPixelMode);
2278 return false;
2279 }
2280 }
2281 // Check whether each stream has max resolution allowed.
2282 bool consistent = true;
2283 for (auto it : streamIdList) {
2284 auto const streamInfoIt = mStreamInfoMap.find(it);
2285 if (streamInfoIt == mStreamInfoMap.end()) {
2286 ALOGE("%s: stream id %d not created, skipping", __FUNCTION__, it);
2287 return false;
2288 }
2289 consistent =
2290 streamInfoIt->second.sensorPixelModesUsed.find(sensorPixelMode) !=
2291 streamInfoIt->second.sensorPixelModesUsed.end();
2292 if (!consistent) {
2293 ALOGE("sensorPixelMode used %i not consistent with configured modes", sensorPixelMode);
2294 for (auto m : streamInfoIt->second.sensorPixelModesUsed) {
2295 ALOGE("sensor pixel mode used list: %i", m);
2296 }
2297 break;
2298 }
2299 }
2300
2301 return consistent;
2302}
2303
Igor Murashkine7ee7632013-06-11 18:10:18 -07002304} // namespace android