blob: 720ffd754639809aa4bcdae3d492e932eac3f551 [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,
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -080064 int api1CameraId,
Igor Murashkine7ee7632013-06-11 18:10:18 -070065 int cameraFacing,
Emilian Peev8b64f282021-03-25 16:49:57 -070066 int sensorOrientation,
Igor Murashkine7ee7632013-06-11 18:10:18 -070067 int clientPid,
68 uid_t clientUid,
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) {
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -080084 // We don't need it for API2 clients, but Camera2ClientBase requires it.
85 (void) api1CameraId;
Igor Murashkine7ee7632013-06-11 18:10:18 -070086}
Igor Murashkine7ee7632013-06-11 18:10:18 -070087
88// Interface used by CameraService
89
90CameraDeviceClient::CameraDeviceClient(const sp<CameraService>& cameraService,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080091 const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback,
Austin Borger74fca042022-05-23 12:41:21 -070092 std::shared_ptr<CameraServiceProxyWrapper> cameraServiceProxyWrapper,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080093 const String16& clientPackageName,
Jayant Chowdharyeb0169f2022-01-31 00:00:02 -080094 bool systemNativeClient,
Jooyung Han3f9a3b42020-01-23 12:27:18 +090095 const std::optional<String16>& clientFeatureId,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080096 const String8& cameraId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080097 int cameraFacing,
Emilian Peev8b64f282021-03-25 16:49:57 -070098 int sensorOrientation,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080099 int clientPid,
100 uid_t clientUid,
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700101 int servicePid,
Austin Borger18b30a72022-10-27 12:20:29 -0700102 bool overrideForPerfClass,
103 bool overrideToPortrait) :
Austin Borger74fca042022-05-23 12:41:21 -0700104 Camera2ClientBase(cameraService, remoteCallback, cameraServiceProxyWrapper, clientPackageName,
105 systemNativeClient, clientFeatureId, cameraId, /*API1 camera ID*/ -1, cameraFacing,
Austin Borger18b30a72022-10-27 12:20:29 -0700106 sensorOrientation, clientPid, clientUid, servicePid, overrideForPerfClass,
107 overrideToPortrait),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700108 mInputStream(),
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700109 mStreamingRequestId(REQUEST_ID_NONE),
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700110 mRequestIdCounter(0),
111 mOverrideForPerfClass(overrideForPerfClass) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700112
113 ATRACE_CALL();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800114 ALOGI("CameraDeviceClient %s: Opened", cameraId.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700115}
116
Emilian Peevbd8c5032018-02-14 23:05:40 +0000117status_t CameraDeviceClient::initialize(sp<CameraProviderManager> manager,
118 const String8& monitorTags) {
119 return initializeImpl(manager, monitorTags);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800120}
121
122template<typename TProviderPtr>
Emilian Peevbd8c5032018-02-14 23:05:40 +0000123status_t CameraDeviceClient::initializeImpl(TProviderPtr providerPtr, const String8& monitorTags) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700124 ATRACE_CALL();
125 status_t res;
126
Emilian Peevbd8c5032018-02-14 23:05:40 +0000127 res = Camera2ClientBase::initialize(providerPtr, monitorTags);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700128 if (res != OK) {
129 return res;
130 }
131
132 String8 threadName;
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -0700133 mFrameProcessor = new FrameProcessorBase(mDevice);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800134 threadName = String8::format("CDU-%s-FrameProc", mCameraIdStr.string());
Austin Borger7b129542022-06-09 13:23:06 -0700135 res = mFrameProcessor->run(threadName.string());
136 if (res != OK) {
137 ALOGE("%s: Unable to start frame processor thread: %s (%d)",
138 __FUNCTION__, strerror(-res), res);
139 return res;
140 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700141
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800142 mFrameProcessor->registerListener(camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MIN_ID,
143 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MAX_ID,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -0800144 /*listener*/this,
Zhijun He25a0aef2014-06-25 11:40:02 -0700145 /*sendPartials*/true);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700146
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800147 const CameraMetadata &deviceInfo = mDevice->info();
148 camera_metadata_ro_entry_t physicalKeysEntry = deviceInfo.find(
Emilian Peev00420d22018-02-05 21:33:13 +0000149 ANDROID_REQUEST_AVAILABLE_PHYSICAL_CAMERA_REQUEST_KEYS);
150 if (physicalKeysEntry.count > 0) {
151 mSupportedPhysicalRequestKeys.insert(mSupportedPhysicalRequestKeys.begin(),
152 physicalKeysEntry.data.i32,
153 physicalKeysEntry.data.i32 + physicalKeysEntry.count);
154 }
155
Emilian Peev2295df72021-11-12 18:14:10 -0800156 auto entry = deviceInfo.find(ANDROID_REQUEST_AVAILABLE_CAPABILITIES);
157 mDynamicProfileMap.emplace(
158 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD,
159 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD);
160 if (entry.count > 0) {
Shuzhen Wangc8ab4522021-12-14 20:12:42 -0800161 const auto it = std::find(entry.data.u8, entry.data.u8 + entry.count,
Emilian Peev2295df72021-11-12 18:14:10 -0800162 ANDROID_REQUEST_AVAILABLE_CAPABILITIES_DYNAMIC_RANGE_TEN_BIT);
Shuzhen Wangc8ab4522021-12-14 20:12:42 -0800163 if (it != entry.data.u8 + entry.count) {
Emilian Peev2295df72021-11-12 18:14:10 -0800164 entry = deviceInfo.find(ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP);
Emilian Peevc81a7592022-02-14 17:38:18 -0800165 if (entry.count > 0 || ((entry.count % 3) != 0)) {
166 int64_t standardBitmap =
167 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD;
168 for (size_t i = 0; i < entry.count; i += 3) {
169 if (entry.data.i64[i] !=
Emilian Peev2295df72021-11-12 18:14:10 -0800170 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD) {
Emilian Peevc81a7592022-02-14 17:38:18 -0800171 mDynamicProfileMap.emplace(entry.data.i64[i], entry.data.i64[i+1]);
172 if ((entry.data.i64[i+1] == 0) || (entry.data.i64[i+1] &
Emilian Peev2295df72021-11-12 18:14:10 -0800173 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD)) {
Emilian Peevc81a7592022-02-14 17:38:18 -0800174 standardBitmap |= entry.data.i64[i];
Emilian Peev2295df72021-11-12 18:14:10 -0800175 }
176 } else {
Emilian Peevc81a7592022-02-14 17:38:18 -0800177 ALOGE("%s: Device %s includes unexpected profile entry: 0x%" PRIx64 "!",
178 __FUNCTION__, mCameraIdStr.c_str(), entry.data.i64[i]);
Emilian Peev2295df72021-11-12 18:14:10 -0800179 }
180 }
Emilian Peevef715e22022-05-19 17:44:33 -0700181 mDynamicProfileMap[ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD] =
182 standardBitmap;
Emilian Peev2295df72021-11-12 18:14:10 -0800183 } else {
184 ALOGE("%s: Device %s supports 10-bit output but doesn't include a dynamic range"
185 " profile map!", __FUNCTION__, mCameraIdStr.c_str());
186 }
187 }
188 }
189
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700190 mProviderManager = providerPtr;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800191 // Cache physical camera ids corresponding to this device and also the high
192 // resolution sensors in this device + physical camera ids
193 mProviderManager->isLogicalCamera(mCameraIdStr.string(), &mPhysicalCameraIds);
194 if (isUltraHighResolutionSensor(mCameraIdStr)) {
195 mHighResolutionSensors.insert(mCameraIdStr.string());
196 }
197 for (auto &physicalId : mPhysicalCameraIds) {
198 if (isUltraHighResolutionSensor(String8(physicalId.c_str()))) {
199 mHighResolutionSensors.insert(physicalId.c_str());
200 }
201 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700202 return OK;
203}
204
205CameraDeviceClient::~CameraDeviceClient() {
206}
207
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800208binder::Status CameraDeviceClient::submitRequest(
209 const hardware::camera2::CaptureRequest& request,
210 bool streaming,
211 /*out*/
212 hardware::camera2::utils::SubmitInfo *submitInfo) {
213 std::vector<hardware::camera2::CaptureRequest> requestList = { request };
214 return submitRequestList(requestList, streaming, submitInfo);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700215}
216
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800217binder::Status CameraDeviceClient::insertGbpLocked(const sp<IGraphicBufferProducer>& gbp,
Emilian Peevf873aa52018-01-26 14:58:28 +0000218 SurfaceMap* outSurfaceMap, Vector<int32_t>* outputStreamIds, int32_t *currentStreamId) {
Emilian Peev538c90e2018-12-17 18:03:19 +0000219 int compositeIdx;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800220 int idx = mStreamMap.indexOfKey(IInterface::asBinder(gbp));
221
Emilian Peev2f5d6012022-01-19 16:16:50 -0800222 Mutex::Autolock l(mCompositeLock);
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800223 // Trying to submit request with surface that wasn't created
224 if (idx == NAME_NOT_FOUND) {
225 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
226 " we have not called createStream on",
227 __FUNCTION__, mCameraIdStr.string());
228 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
229 "Request targets Surface that is not part of current capture session");
Emilian Peev538c90e2018-12-17 18:03:19 +0000230 } else if ((compositeIdx = mCompositeStreamMap.indexOfKey(IInterface::asBinder(gbp)))
231 != NAME_NOT_FOUND) {
232 mCompositeStreamMap.valueAt(compositeIdx)->insertGbp(outSurfaceMap, outputStreamIds,
233 currentStreamId);
234 return binder::Status::ok();
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800235 }
236
237 const StreamSurfaceId& streamSurfaceId = mStreamMap.valueAt(idx);
238 if (outSurfaceMap->find(streamSurfaceId.streamId()) == outSurfaceMap->end()) {
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800239 outputStreamIds->push_back(streamSurfaceId.streamId());
240 }
241 (*outSurfaceMap)[streamSurfaceId.streamId()].push_back(streamSurfaceId.surfaceId());
242
243 ALOGV("%s: Camera %s: Appending output stream %d surface %d to request",
244 __FUNCTION__, mCameraIdStr.string(), streamSurfaceId.streamId(),
245 streamSurfaceId.surfaceId());
246
Emilian Peevf873aa52018-01-26 14:58:28 +0000247 if (currentStreamId != nullptr) {
248 *currentStreamId = streamSurfaceId.streamId();
249 }
250
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800251 return binder::Status::ok();
252}
253
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800254static std::list<int> getIntersection(const std::unordered_set<int> &streamIdsForThisCamera,
255 const Vector<int> &streamIdsForThisRequest) {
256 std::list<int> intersection;
257 for (auto &streamId : streamIdsForThisRequest) {
258 if (streamIdsForThisCamera.find(streamId) != streamIdsForThisCamera.end()) {
259 intersection.emplace_back(streamId);
260 }
261 }
262 return intersection;
263}
264
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800265binder::Status CameraDeviceClient::submitRequestList(
266 const std::vector<hardware::camera2::CaptureRequest>& requests,
267 bool streaming,
268 /*out*/
269 hardware::camera2::utils::SubmitInfo *submitInfo) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700270 ATRACE_CALL();
Mark Salyzyn50468412014-06-18 16:33:43 -0700271 ALOGV("%s-start of function. Request list size %zu", __FUNCTION__, requests.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700272
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800273 binder::Status res = binder::Status::ok();
274 status_t err;
275 if ( !(res = checkPidStatus(__FUNCTION__) ).isOk()) {
276 return res;
277 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700278
279 Mutex::Autolock icl(mBinderSerializationLock);
280
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800281 if (!mDevice.get()) {
282 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
283 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700284
285 if (requests.empty()) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800286 ALOGE("%s: Camera %s: Sent null request. Rejecting request.",
287 __FUNCTION__, mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800288 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Empty request list");
Jianing Wei90e59c92014-03-12 18:29:36 -0700289 }
290
Emilian Peevaebbe412018-01-15 13:53:24 +0000291 List<const CameraDeviceBase::PhysicalCameraSettingsList> metadataRequestList;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700292 std::list<const SurfaceMap> surfaceMapList;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800293 submitInfo->mRequestId = mRequestIdCounter;
Jianing Wei90e59c92014-03-12 18:29:36 -0700294 uint32_t loopCounter = 0;
295
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800296 for (auto&& request: requests) {
297 if (request.mIsReprocess) {
Chien-Yu Chened0412e2015-04-27 15:04:22 -0700298 if (!mInputStream.configured) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800299 ALOGE("%s: Camera %s: no input stream is configured.", __FUNCTION__,
300 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800301 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800302 "No input configured for camera %s but request is for reprocessing",
303 mCameraIdStr.string());
Chien-Yu Chened0412e2015-04-27 15:04:22 -0700304 } else if (streaming) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800305 ALOGE("%s: Camera %s: streaming reprocess requests not supported.", __FUNCTION__,
306 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800307 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
308 "Repeating reprocess requests not supported");
Emilian Peevaebbe412018-01-15 13:53:24 +0000309 } else if (request.mPhysicalCameraSettings.size() > 1) {
310 ALOGE("%s: Camera %s: reprocess requests not supported for "
311 "multiple physical cameras.", __FUNCTION__,
312 mCameraIdStr.string());
313 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
314 "Reprocess requests not supported for multiple cameras");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700315 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700316 }
317
Emilian Peevaebbe412018-01-15 13:53:24 +0000318 if (request.mPhysicalCameraSettings.empty()) {
319 ALOGE("%s: Camera %s: request doesn't contain any settings.", __FUNCTION__,
320 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800321 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
Emilian Peevaebbe412018-01-15 13:53:24 +0000322 "Request doesn't contain any settings");
323 }
324
325 //The first capture settings should always match the logical camera id
326 String8 logicalId(request.mPhysicalCameraSettings.begin()->id.c_str());
327 if (mDevice->getId() != logicalId) {
328 ALOGE("%s: Camera %s: Invalid camera request settings.", __FUNCTION__,
329 mCameraIdStr.string());
330 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
331 "Invalid camera request settings");
332 }
333
Emilian Peevaebbe412018-01-15 13:53:24 +0000334 if (request.mSurfaceList.isEmpty() && request.mStreamIdxList.size() == 0) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800335 ALOGE("%s: Camera %s: Requests must have at least one surface target. "
336 "Rejecting request.", __FUNCTION__, mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800337 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
338 "Request has no output targets");
Jianing Wei90e59c92014-03-12 18:29:36 -0700339 }
340
Jianing Wei90e59c92014-03-12 18:29:36 -0700341 /**
Shuzhen Wang0129d522016-10-30 22:43:41 -0700342 * Write in the output stream IDs and map from stream ID to surface ID
343 * which we calculate from the capture request's list of surface target
Jianing Wei90e59c92014-03-12 18:29:36 -0700344 */
Shuzhen Wang0129d522016-10-30 22:43:41 -0700345 SurfaceMap surfaceMap;
Jianing Wei90e59c92014-03-12 18:29:36 -0700346 Vector<int32_t> outputStreamIds;
Emilian Peevf873aa52018-01-26 14:58:28 +0000347 std::vector<std::string> requestedPhysicalIds;
Emilian Peevc81a7592022-02-14 17:38:18 -0800348 int64_t dynamicProfileBitmap = 0;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800349 if (request.mSurfaceList.size() > 0) {
Chih-Hung Hsieh3ef324d2018-12-11 11:48:12 -0800350 for (const sp<Surface>& surface : request.mSurfaceList) {
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800351 if (surface == 0) continue;
Jianing Wei90e59c92014-03-12 18:29:36 -0700352
Emilian Peevf873aa52018-01-26 14:58:28 +0000353 int32_t streamId;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800354 sp<IGraphicBufferProducer> gbp = surface->getIGraphicBufferProducer();
Emilian Peevf873aa52018-01-26 14:58:28 +0000355 res = insertGbpLocked(gbp, &surfaceMap, &outputStreamIds, &streamId);
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800356 if (!res.isOk()) {
357 return res;
358 }
Emilian Peevf873aa52018-01-26 14:58:28 +0000359
360 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
361 if (index >= 0) {
362 String8 requestedPhysicalId(
363 mConfiguredOutputs.valueAt(index).getPhysicalCameraId());
364 requestedPhysicalIds.push_back(requestedPhysicalId.string());
Emilian Peev2295df72021-11-12 18:14:10 -0800365 dynamicProfileBitmap |=
366 mConfiguredOutputs.valueAt(index).getDynamicRangeProfile();
Emilian Peevf873aa52018-01-26 14:58:28 +0000367 } else {
368 ALOGW("%s: Output stream Id not found among configured outputs!", __FUNCTION__);
369 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700370 }
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800371 } else {
372 for (size_t i = 0; i < request.mStreamIdxList.size(); i++) {
373 int streamId = request.mStreamIdxList.itemAt(i);
374 int surfaceIdx = request.mSurfaceIdxList.itemAt(i);
Jianing Wei90e59c92014-03-12 18:29:36 -0700375
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800376 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
377 if (index < 0) {
378 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
379 " we have not called createStream on: stream %d",
380 __FUNCTION__, mCameraIdStr.string(), streamId);
381 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
382 "Request targets Surface that is not part of current capture session");
383 }
384
385 const auto& gbps = mConfiguredOutputs.valueAt(index).getGraphicBufferProducers();
386 if ((size_t)surfaceIdx >= gbps.size()) {
387 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
388 " we have not called createStream on: stream %d, surfaceIdx %d",
389 __FUNCTION__, mCameraIdStr.string(), streamId, surfaceIdx);
390 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
391 "Request targets Surface has invalid surface index");
392 }
393
Emilian Peevf873aa52018-01-26 14:58:28 +0000394 res = insertGbpLocked(gbps[surfaceIdx], &surfaceMap, &outputStreamIds, nullptr);
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800395 if (!res.isOk()) {
396 return res;
397 }
Emilian Peevf873aa52018-01-26 14:58:28 +0000398
399 String8 requestedPhysicalId(
400 mConfiguredOutputs.valueAt(index).getPhysicalCameraId());
401 requestedPhysicalIds.push_back(requestedPhysicalId.string());
Emilian Peev2295df72021-11-12 18:14:10 -0800402 dynamicProfileBitmap |=
403 mConfiguredOutputs.valueAt(index).getDynamicRangeProfile();
404 }
405 }
406
407 if (dynamicProfileBitmap !=
408 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD) {
409 for (int i = ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD;
410 i < ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_MAX; i <<= 1) {
411 if ((dynamicProfileBitmap & i) == 0) {
412 continue;
413 }
414
415 const auto& it = mDynamicProfileMap.find(i);
416 if (it != mDynamicProfileMap.end()) {
417 if ((it->second == 0) ||
418 ((it->second & dynamicProfileBitmap) == dynamicProfileBitmap)) {
419 continue;
420 } else {
421 ALOGE("%s: Camera %s: Tried to submit a request with a surfaces that"
422 " reference an unsupported dynamic range profile combination"
Emilian Peevc81a7592022-02-14 17:38:18 -0800423 " 0x%" PRIx64 "!", __FUNCTION__, mCameraIdStr.string(),
Emilian Peev2295df72021-11-12 18:14:10 -0800424 dynamicProfileBitmap);
425 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
426 "Request targets an unsupported dynamic range profile"
427 " combination");
428 }
429 } else {
430 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
431 " references unsupported dynamic range profile 0x%x!",
432 __FUNCTION__, mCameraIdStr.string(), i);
433 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
434 "Request targets 10-bit Surface with unsupported dynamic range"
435 " profile");
436 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700437 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700438 }
439
Emilian Peevf873aa52018-01-26 14:58:28 +0000440 CameraDeviceBase::PhysicalCameraSettingsList physicalSettingsList;
441 for (const auto& it : request.mPhysicalCameraSettings) {
Emilian Peev00420d22018-02-05 21:33:13 +0000442 if (it.settings.isEmpty()) {
443 ALOGE("%s: Camera %s: Sent empty metadata packet. Rejecting request.",
444 __FUNCTION__, mCameraIdStr.string());
445 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
446 "Request settings are empty");
447 }
448
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800449 // Check whether the physical / logical stream has settings
450 // consistent with the sensor pixel mode(s) it was configured with.
451 // mCameraIdToStreamSet will only have ids that are high resolution
452 const auto streamIdSetIt = mHighResolutionCameraIdToStreamIdSet.find(it.id);
453 if (streamIdSetIt != mHighResolutionCameraIdToStreamIdSet.end()) {
454 std::list<int> streamIdsUsedInRequest = getIntersection(streamIdSetIt->second,
455 outputStreamIds);
456 if (!request.mIsReprocess &&
457 !isSensorPixelModeConsistent(streamIdsUsedInRequest, it.settings)) {
458 ALOGE("%s: Camera %s: Request settings CONTROL_SENSOR_PIXEL_MODE not "
459 "consistent with configured streams. Rejecting request.",
460 __FUNCTION__, it.id.c_str());
461 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
462 "Request settings CONTROL_SENSOR_PIXEL_MODE are not consistent with "
463 "streams configured");
464 }
465 }
466
Emilian Peevf873aa52018-01-26 14:58:28 +0000467 String8 physicalId(it.id.c_str());
Shuzhen Wang911c6a32021-10-27 13:36:03 -0700468 bool hasTestPatternModePhysicalKey = std::find(mSupportedPhysicalRequestKeys.begin(),
469 mSupportedPhysicalRequestKeys.end(), ANDROID_SENSOR_TEST_PATTERN_MODE) !=
470 mSupportedPhysicalRequestKeys.end();
471 bool hasTestPatternDataPhysicalKey = std::find(mSupportedPhysicalRequestKeys.begin(),
472 mSupportedPhysicalRequestKeys.end(), ANDROID_SENSOR_TEST_PATTERN_DATA) !=
473 mSupportedPhysicalRequestKeys.end();
Emilian Peevf873aa52018-01-26 14:58:28 +0000474 if (physicalId != mDevice->getId()) {
475 auto found = std::find(requestedPhysicalIds.begin(), requestedPhysicalIds.end(),
476 it.id);
477 if (found == requestedPhysicalIds.end()) {
478 ALOGE("%s: Camera %s: Physical camera id: %s not part of attached outputs.",
479 __FUNCTION__, mCameraIdStr.string(), physicalId.string());
480 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
481 "Invalid physical camera id");
482 }
Emilian Peev00420d22018-02-05 21:33:13 +0000483
484 if (!mSupportedPhysicalRequestKeys.empty()) {
485 // Filter out any unsupported physical request keys.
486 CameraMetadata filteredParams(mSupportedPhysicalRequestKeys.size());
487 camera_metadata_t *meta = const_cast<camera_metadata_t *>(
488 filteredParams.getAndLock());
489 set_camera_metadata_vendor_id(meta, mDevice->getVendorTagId());
490 filteredParams.unlock(meta);
491
492 for (const auto& keyIt : mSupportedPhysicalRequestKeys) {
493 camera_metadata_ro_entry entry = it.settings.find(keyIt);
494 if (entry.count > 0) {
495 filteredParams.update(entry);
496 }
497 }
498
Shuzhen Wang911c6a32021-10-27 13:36:03 -0700499 physicalSettingsList.push_back({it.id, filteredParams,
500 hasTestPatternModePhysicalKey, hasTestPatternDataPhysicalKey});
Emilian Peev00420d22018-02-05 21:33:13 +0000501 }
502 } else {
503 physicalSettingsList.push_back({it.id, it.settings});
Emilian Peevf873aa52018-01-26 14:58:28 +0000504 }
Emilian Peevf873aa52018-01-26 14:58:28 +0000505 }
506
507 if (!enforceRequestPermissions(physicalSettingsList.begin()->metadata)) {
508 // Callee logs
509 return STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
510 "Caller does not have permission to change restricted controls");
511 }
512
Emilian Peevaebbe412018-01-15 13:53:24 +0000513 physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_OUTPUT_STREAMS,
514 &outputStreamIds[0], outputStreamIds.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700515
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800516 if (request.mIsReprocess) {
Emilian Peevaebbe412018-01-15 13:53:24 +0000517 physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_INPUT_STREAMS,
518 &mInputStream.id, 1);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700519 }
520
Emilian Peevaebbe412018-01-15 13:53:24 +0000521 physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_ID,
522 &(submitInfo->mRequestId), /*size*/1);
Jianing Wei90e59c92014-03-12 18:29:36 -0700523 loopCounter++; // loopCounter starts from 1
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800524 ALOGV("%s: Camera %s: Creating request with ID %d (%d of %zu)",
525 __FUNCTION__, mCameraIdStr.string(), submitInfo->mRequestId,
526 loopCounter, requests.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700527
Emilian Peevaebbe412018-01-15 13:53:24 +0000528 metadataRequestList.push_back(physicalSettingsList);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700529 surfaceMapList.push_back(surfaceMap);
Shuzhen Wangd26b1862022-03-07 12:05:05 -0800530
Shuzhen Wang9372b0b2022-05-11 18:55:19 -0700531 // Save certain CaptureRequest settings
Shuzhen Wangd26b1862022-03-07 12:05:05 -0800532 if (!request.mUserTag.empty()) {
533 mUserTag = request.mUserTag;
534 }
Shuzhen Wang9372b0b2022-05-11 18:55:19 -0700535 camera_metadata_entry entry =
536 physicalSettingsList.begin()->metadata.find(
537 ANDROID_CONTROL_VIDEO_STABILIZATION_MODE);
538 if (entry.count == 1) {
539 mVideoStabilizationMode = entry.data.u8[0];
540 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700541 }
542 mRequestIdCounter++;
543
544 if (streaming) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700545 err = mDevice->setStreamingRequestList(metadataRequestList, surfaceMapList,
546 &(submitInfo->mLastFrameNumber));
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800547 if (err != OK) {
548 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800549 "Camera %s: Got error %s (%d) after trying to set streaming request",
550 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800551 ALOGE("%s: %s", __FUNCTION__, msg.string());
552 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
553 msg.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700554 } else {
Shuzhen Wangc9ca6782016-04-26 13:40:31 -0700555 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700556 mStreamingRequestId = submitInfo->mRequestId;
Jianing Wei90e59c92014-03-12 18:29:36 -0700557 }
558 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700559 err = mDevice->captureList(metadataRequestList, surfaceMapList,
560 &(submitInfo->mLastFrameNumber));
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800561 if (err != OK) {
562 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800563 "Camera %s: Got error %s (%d) after trying to submit capture request",
564 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800565 ALOGE("%s: %s", __FUNCTION__, msg.string());
566 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
567 msg.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700568 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800569 ALOGV("%s: requestId = %d ", __FUNCTION__, submitInfo->mRequestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700570 }
571
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800572 ALOGV("%s: Camera %s: End of function", __FUNCTION__, mCameraIdStr.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700573 return res;
574}
575
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800576binder::Status CameraDeviceClient::cancelRequest(
577 int requestId,
578 /*out*/
579 int64_t* lastFrameNumber) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700580 ATRACE_CALL();
581 ALOGV("%s, requestId = %d", __FUNCTION__, requestId);
582
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800583 status_t err;
584 binder::Status res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700585
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800586 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700587
588 Mutex::Autolock icl(mBinderSerializationLock);
589
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800590 if (!mDevice.get()) {
591 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
592 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700593
Shuzhen Wangc9ca6782016-04-26 13:40:31 -0700594 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700595 if (mStreamingRequestId != requestId) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800596 String8 msg = String8::format("Camera %s: Canceling request ID %d doesn't match "
597 "current request ID %d", mCameraIdStr.string(), requestId, mStreamingRequestId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800598 ALOGE("%s: %s", __FUNCTION__, msg.string());
599 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700600 }
601
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800602 err = mDevice->clearStreamingRequest(lastFrameNumber);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700603
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800604 if (err == OK) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800605 ALOGV("%s: Camera %s: Successfully cleared streaming request",
606 __FUNCTION__, mCameraIdStr.string());
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700607 mStreamingRequestId = REQUEST_ID_NONE;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800608 } else {
609 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800610 "Camera %s: Error clearing streaming request: %s (%d)",
611 mCameraIdStr.string(), strerror(-err), err);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700612 }
613
614 return res;
615}
616
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800617binder::Status CameraDeviceClient::beginConfigure() {
Ruben Brunkb2119af2014-05-09 19:57:56 -0700618 // TODO: Implement this.
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -0700619 ATRACE_CALL();
Zhijun He1fa89992015-06-01 15:44:31 -0700620 ALOGV("%s: Not implemented yet.", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800621 return binder::Status::ok();
Ruben Brunkb2119af2014-05-09 19:57:56 -0700622}
623
Emilian Peev5fbe0ba2017-10-20 15:45:45 +0100624binder::Status CameraDeviceClient::endConfigure(int operatingMode,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700625 const hardware::camera2::impl::CameraMetadataNative& sessionParams, int64_t startTimeMs,
Emilian Peevcc0b7952020-01-07 13:54:47 -0800626 std::vector<int>* offlineStreamIds /*out*/) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -0700627 ATRACE_CALL();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700628 ALOGV("%s: ending configure (%d input stream, %zu output surfaces)",
629 __FUNCTION__, mInputStream.configured ? 1 : 0,
630 mStreamMap.size());
Igor Murashkine2d167e2014-08-19 16:19:59 -0700631
Zhijun He0effd522016-03-04 10:22:27 -0800632 binder::Status res;
633 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
634
Emilian Peevcc0b7952020-01-07 13:54:47 -0800635 if (offlineStreamIds == nullptr) {
636 String8 msg = String8::format("Invalid offline stream ids");
637 ALOGE("%s: %s", __FUNCTION__, msg.string());
638 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
639 }
640
Zhijun He0effd522016-03-04 10:22:27 -0800641 Mutex::Autolock icl(mBinderSerializationLock);
642
643 if (!mDevice.get()) {
644 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
645 }
646
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700647 res = SessionConfigurationUtils::checkOperatingMode(operatingMode, mDevice->info(),
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000648 mCameraIdStr);
Emilian Peev35ae8262018-11-08 13:11:32 +0000649 if (!res.isOk()) {
650 return res;
651 }
652
653 status_t err = mDevice->configureStreams(sessionParams, operatingMode);
654 if (err == BAD_VALUE) {
655 String8 msg = String8::format("Camera %s: Unsupported set of inputs/outputs provided",
656 mCameraIdStr.string());
657 ALOGE("%s: %s", __FUNCTION__, msg.string());
658 res = STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
659 } else if (err != OK) {
660 String8 msg = String8::format("Camera %s: Error configuring streams: %s (%d)",
661 mCameraIdStr.string(), strerror(-err), err);
662 ALOGE("%s: %s", __FUNCTION__, msg.string());
663 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Emilian Peev538c90e2018-12-17 18:03:19 +0000664 } else {
Emilian Peevcc0b7952020-01-07 13:54:47 -0800665 offlineStreamIds->clear();
666 mDevice->getOfflineStreamIds(offlineStreamIds);
667
Emilian Peev2f5d6012022-01-19 16:16:50 -0800668 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +0000669 for (size_t i = 0; i < mCompositeStreamMap.size(); ++i) {
670 err = mCompositeStreamMap.valueAt(i)->configureStream();
Emilian Peevcc0b7952020-01-07 13:54:47 -0800671 if (err != OK) {
Emilian Peev538c90e2018-12-17 18:03:19 +0000672 String8 msg = String8::format("Camera %s: Error configuring composite "
673 "streams: %s (%d)", mCameraIdStr.string(), strerror(-err), err);
674 ALOGE("%s: %s", __FUNCTION__, msg.string());
675 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
676 break;
677 }
Emilian Peevcc0b7952020-01-07 13:54:47 -0800678
679 // Composite streams can only support offline mode in case all individual internal
680 // streams are also supported.
681 std::vector<int> internalStreams;
682 mCompositeStreamMap.valueAt(i)->insertCompositeStreamIds(&internalStreams);
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800683 offlineStreamIds->erase(
684 std::remove_if(offlineStreamIds->begin(), offlineStreamIds->end(),
Emilian Peevcc0b7952020-01-07 13:54:47 -0800685 [&internalStreams] (int streamId) {
686 auto it = std::find(internalStreams.begin(), internalStreams.end(),
687 streamId);
688 if (it != internalStreams.end()) {
689 internalStreams.erase(it);
690 return true;
691 }
692
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800693 return false;}), offlineStreamIds->end());
Emilian Peevcc0b7952020-01-07 13:54:47 -0800694 if (internalStreams.empty()) {
695 offlineStreamIds->push_back(mCompositeStreamMap.valueAt(i)->getStreamId());
696 }
697 }
698
699 for (const auto& offlineStreamId : *offlineStreamIds) {
700 mStreamInfoMap[offlineStreamId].supportsOffline = true;
Emilian Peev538c90e2018-12-17 18:03:19 +0000701 }
Shuzhen Wang316781a2020-08-18 18:11:01 -0700702
703 nsecs_t configureEnd = systemTime();
704 int32_t configureDurationMs = ns2ms(configureEnd) - startTimeMs;
Austin Borger74fca042022-05-23 12:41:21 -0700705 mCameraServiceProxyWrapper->logStreamConfigured(mCameraIdStr, operatingMode,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700706 false /*internalReconfig*/, configureDurationMs);
Emilian Peev35ae8262018-11-08 13:11:32 +0000707 }
708
709 return res;
710}
711
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800712binder::Status CameraDeviceClient::isSessionConfigurationSupported(
713 const SessionConfiguration& sessionConfiguration, bool *status /*out*/) {
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800714
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800715 ATRACE_CALL();
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800716 binder::Status res;
717 status_t ret = OK;
718 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
719
720 Mutex::Autolock icl(mBinderSerializationLock);
721
722 if (!mDevice.get()) {
723 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
724 }
725
726 auto operatingMode = sessionConfiguration.getOperatingMode();
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700727 res = SessionConfigurationUtils::checkOperatingMode(operatingMode, mDevice->info(),
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000728 mCameraIdStr);
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800729 if (!res.isOk()) {
730 return res;
731 }
732
733 if (status == nullptr) {
734 String8 msg = String8::format( "Camera %s: Invalid status!", mCameraIdStr.string());
735 ALOGE("%s: %s", __FUNCTION__, msg.string());
736 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
737 }
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700738
Emilian Peev35ae8262018-11-08 13:11:32 +0000739 *status = false;
Shuzhen Wanga79a64d2022-04-24 19:56:30 -0700740 camera3::metadataGetter getMetadata = [this](const String8 &id, bool /*overrideForPerfClass*/) {
741 return mDevice->infoPhysical(id);};
Emilian Peev35ae8262018-11-08 13:11:32 +0000742 ret = mProviderManager->isSessionConfigurationSupported(mCameraIdStr.string(),
Shuzhen Wanga79a64d2022-04-24 19:56:30 -0700743 sessionConfiguration, mOverrideForPerfClass, getMetadata, status);
Emilian Peev35ae8262018-11-08 13:11:32 +0000744 switch (ret) {
745 case OK:
746 // Expected, do nothing.
747 break;
748 case INVALID_OPERATION: {
749 String8 msg = String8::format(
750 "Camera %s: Session configuration query not supported!",
751 mCameraIdStr.string());
752 ALOGD("%s: %s", __FUNCTION__, msg.string());
753 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
754 }
755
756 break;
757 default: {
758 String8 msg = String8::format( "Camera %s: Error: %s (%d)", mCameraIdStr.string(),
759 strerror(-ret), ret);
760 ALOGE("%s: %s", __FUNCTION__, msg.string());
761 res = STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
762 msg.string());
763 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800764 }
765
766 return res;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700767}
768
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800769binder::Status CameraDeviceClient::deleteStream(int streamId) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700770 ATRACE_CALL();
771 ALOGV("%s (streamId = 0x%x)", __FUNCTION__, streamId);
772
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800773 binder::Status res;
774 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700775
776 Mutex::Autolock icl(mBinderSerializationLock);
777
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800778 if (!mDevice.get()) {
779 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
780 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700781
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700782 bool isInput = false;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700783 std::vector<sp<IBinder>> surfaces;
Zhijun He5d677d12016-05-29 16:52:39 -0700784 ssize_t dIndex = NAME_NOT_FOUND;
Emilian Peev538c90e2018-12-17 18:03:19 +0000785 ssize_t compositeIndex = NAME_NOT_FOUND;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700786
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700787 if (mInputStream.configured && mInputStream.id == streamId) {
788 isInput = true;
789 } else {
790 // Guard against trying to delete non-created streams
791 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700792 if (streamId == mStreamMap.valueAt(i).streamId()) {
793 surfaces.push_back(mStreamMap.keyAt(i));
794 }
795 }
796
797 // See if this stream is one of the deferred streams.
798 for (size_t i = 0; i < mDeferredStreams.size(); ++i) {
799 if (streamId == mDeferredStreams[i]) {
800 dIndex = i;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700801 break;
802 }
803 }
804
Emilian Peev2f5d6012022-01-19 16:16:50 -0800805 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +0000806 for (size_t i = 0; i < mCompositeStreamMap.size(); ++i) {
807 if (streamId == mCompositeStreamMap.valueAt(i)->getStreamId()) {
808 compositeIndex = i;
809 break;
810 }
811 }
812
Shuzhen Wang0129d522016-10-30 22:43:41 -0700813 if (surfaces.empty() && dIndex == NAME_NOT_FOUND) {
814 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no such"
815 " stream created yet", mCameraIdStr.string(), streamId);
816 ALOGW("%s: %s", __FUNCTION__, msg.string());
817 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700818 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700819 }
820
821 // Also returns BAD_VALUE if stream ID was not valid
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800822 status_t err = mDevice->deleteStream(streamId);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700823
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800824 if (err != OK) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800825 String8 msg = String8::format("Camera %s: Unexpected error %s (%d) when deleting stream %d",
826 mCameraIdStr.string(), strerror(-err), err, streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800827 ALOGE("%s: %s", __FUNCTION__, msg.string());
828 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
829 } else {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700830 if (isInput) {
831 mInputStream.configured = false;
Zhijun He5d677d12016-05-29 16:52:39 -0700832 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700833 for (auto& surface : surfaces) {
834 mStreamMap.removeItem(surface);
835 }
836
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800837 mConfiguredOutputs.removeItem(streamId);
838
Shuzhen Wang0129d522016-10-30 22:43:41 -0700839 if (dIndex != NAME_NOT_FOUND) {
840 mDeferredStreams.removeItemsAt(dIndex);
841 }
Emilian Peev538c90e2018-12-17 18:03:19 +0000842
843 if (compositeIndex != NAME_NOT_FOUND) {
Emilian Peev2f5d6012022-01-19 16:16:50 -0800844 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +0000845 status_t ret;
846 if ((ret = mCompositeStreamMap.valueAt(compositeIndex)->deleteStream())
847 != OK) {
848 String8 msg = String8::format("Camera %s: Unexpected error %s (%d) when "
849 "deleting composite stream %d", mCameraIdStr.string(), strerror(-err), err,
850 streamId);
851 ALOGE("%s: %s", __FUNCTION__, msg.string());
852 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
853 }
854 mCompositeStreamMap.removeItemsAt(compositeIndex);
855 }
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800856 for (auto &mapIt: mHighResolutionCameraIdToStreamIdSet) {
857 auto &streamSet = mapIt.second;
858 if (streamSet.find(streamId) != streamSet.end()) {
859 streamSet.erase(streamId);
860 break;
861 }
862 }
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700863 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700864 }
865
866 return res;
867}
868
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800869binder::Status CameraDeviceClient::createStream(
870 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
871 /*out*/
872 int32_t* newStreamId) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700873 ATRACE_CALL();
Igor Murashkine7ee7632013-06-11 18:10:18 -0700874
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800875 binder::Status res;
876 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700877
878 Mutex::Autolock icl(mBinderSerializationLock);
879
Shuzhen Wang0129d522016-10-30 22:43:41 -0700880 const std::vector<sp<IGraphicBufferProducer>>& bufferProducers =
881 outputConfiguration.getGraphicBufferProducers();
882 size_t numBufferProducers = bufferProducers.size();
Shuzhen Wang758c2152017-01-10 18:26:18 -0800883 bool deferredConsumer = outputConfiguration.isDeferred();
884 bool isShared = outputConfiguration.isShared();
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800885 String8 physicalCameraId = String8(outputConfiguration.getPhysicalCameraId());
Shuzhen Wang758c2152017-01-10 18:26:18 -0800886 bool deferredConsumerOnly = deferredConsumer && numBufferProducers == 0;
Shuzhen Wang83bff122020-11-20 15:51:39 -0800887 bool isMultiResolution = outputConfiguration.isMultiResolution();
Emilian Peevc81a7592022-02-14 17:38:18 -0800888 int64_t dynamicRangeProfile = outputConfiguration.getDynamicRangeProfile();
Shuzhen Wang8ed1e872022-03-08 16:34:33 -0800889 int64_t streamUseCase = outputConfiguration.getStreamUseCase();
Shuzhen Wange4208922022-02-01 16:52:48 -0800890 int timestampBase = outputConfiguration.getTimestampBase();
Shuzhen Wang610d7b82022-02-08 14:37:22 -0800891 int mirrorMode = outputConfiguration.getMirrorMode();
Austin Borger9e2b27c2022-07-15 11:27:24 -0700892 int32_t colorSpace = outputConfiguration.getColorSpace();
Shuzhen Wangbce53db2022-12-03 00:38:20 +0000893 bool useReadoutTimestamp = outputConfiguration.useReadoutTimestamp();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700894
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700895 res = SessionConfigurationUtils::checkSurfaceType(numBufferProducers, deferredConsumer,
Emilian Peev35ae8262018-11-08 13:11:32 +0000896 outputConfiguration.getSurfaceType());
897 if (!res.isOk()) {
898 return res;
Yin-Chia Yeh89f14da2014-06-10 16:05:44 -0700899 }
Zhijun He5d677d12016-05-29 16:52:39 -0700900
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800901 if (!mDevice.get()) {
902 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
903 }
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700904 res = SessionConfigurationUtils::checkPhysicalCameraId(mPhysicalCameraIds,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800905 physicalCameraId, mCameraIdStr);
Emilian Peev35ae8262018-11-08 13:11:32 +0000906 if (!res.isOk()) {
907 return res;
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800908 }
Emilian Peev35ae8262018-11-08 13:11:32 +0000909
Shuzhen Wang0129d522016-10-30 22:43:41 -0700910 std::vector<sp<Surface>> surfaces;
911 std::vector<sp<IBinder>> binders;
Zhijun He5d677d12016-05-29 16:52:39 -0700912 status_t err;
913
914 // Create stream for deferred surface case.
Shuzhen Wang0129d522016-10-30 22:43:41 -0700915 if (deferredConsumerOnly) {
Shuzhen Wang758c2152017-01-10 18:26:18 -0800916 return createDeferredSurfaceStreamLocked(outputConfiguration, isShared, newStreamId);
Zhijun He5d677d12016-05-29 16:52:39 -0700917 }
918
Shuzhen Wang758c2152017-01-10 18:26:18 -0800919 OutputStreamInfo streamInfo;
920 bool isStreamInfoValid = false;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800921 const std::vector<int32_t> &sensorPixelModesUsed =
922 outputConfiguration.getSensorPixelModesUsed();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700923 for (auto& bufferProducer : bufferProducers) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700924 // Don't create multiple streams for the same target surface
Shuzhen Wang0129d522016-10-30 22:43:41 -0700925 sp<IBinder> binder = IInterface::asBinder(bufferProducer);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800926 ssize_t index = mStreamMap.indexOfKey(binder);
927 if (index != NAME_NOT_FOUND) {
928 String8 msg = String8::format("Camera %s: Surface already has a stream created for it "
929 "(ID %zd)", mCameraIdStr.string(), index);
930 ALOGW("%s: %s", __FUNCTION__, msg.string());
931 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
Shuzhen Wang0129d522016-10-30 22:43:41 -0700932 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700933
Shuzhen Wang758c2152017-01-10 18:26:18 -0800934 sp<Surface> surface;
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700935 res = SessionConfigurationUtils::createSurfaceFromGbp(streamInfo,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800936 isStreamInfoValid, surface, bufferProducer, mCameraIdStr,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -0800937 mDevice->infoPhysical(physicalCameraId), sensorPixelModesUsed, dynamicRangeProfile,
Austin Borger9e2b27c2022-07-15 11:27:24 -0700938 streamUseCase, timestampBase, mirrorMode, colorSpace);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800939
940 if (!res.isOk())
941 return res;
942
943 if (!isStreamInfoValid) {
944 isStreamInfoValid = true;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700945 }
946
Shuzhen Wang758c2152017-01-10 18:26:18 -0800947 binders.push_back(IInterface::asBinder(bufferProducer));
Shuzhen Wang0129d522016-10-30 22:43:41 -0700948 surfaces.push_back(surface);
Ruben Brunkbba75572014-11-20 17:29:50 -0800949 }
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700950
951 // If mOverrideForPerfClass is true, do not fail createStream() for small
952 // JPEG sizes because existing createSurfaceFromGbp() logic will find the
953 // closest possible supported size.
954
Zhijun He125684a2015-12-26 15:07:30 -0800955 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
Emilian Peev40ead602017-09-26 15:46:36 +0100956 std::vector<int> surfaceIds;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800957 bool isDepthCompositeStream =
958 camera3::DepthCompositeStream::isDepthCompositeStream(surfaces[0]);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800959 bool isHeicCompisiteStream = camera3::HeicCompositeStream::isHeicCompositeStream(surfaces[0]);
Emilian Peev434248e2022-10-06 14:58:54 -0700960 bool isJpegRCompositeStream =
961 camera3::JpegRCompositeStream::isJpegRCompositeStream(surfaces[0]);
962 if (isDepthCompositeStream || isHeicCompisiteStream || isJpegRCompositeStream) {
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800963 sp<CompositeStream> compositeStream;
964 if (isDepthCompositeStream) {
965 compositeStream = new camera3::DepthCompositeStream(mDevice, getRemoteCallback());
Emilian Peev434248e2022-10-06 14:58:54 -0700966 } else if (isHeicCompisiteStream) {
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800967 compositeStream = new camera3::HeicCompositeStream(mDevice, getRemoteCallback());
Emilian Peev434248e2022-10-06 14:58:54 -0700968 } else {
969 compositeStream = new camera3::JpegRCompositeStream(mDevice, getRemoteCallback());
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800970 }
971
Emilian Peev538c90e2018-12-17 18:03:19 +0000972 err = compositeStream->createStream(surfaces, deferredConsumer, streamInfo.width,
973 streamInfo.height, streamInfo.format,
Emilian Peevf4816702020-04-03 15:44:51 -0700974 static_cast<camera_stream_rotation_t>(outputConfiguration.getRotation()),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800975 &streamId, physicalCameraId, streamInfo.sensorPixelModesUsed, &surfaceIds,
Emilian Peev434248e2022-10-06 14:58:54 -0700976 outputConfiguration.getSurfaceSetID(), isShared, isMultiResolution,
Shuzhen Wangbce53db2022-12-03 00:38:20 +0000977 streamInfo.colorSpace, streamInfo.dynamicRangeProfile, streamInfo.streamUseCase,
978 useReadoutTimestamp);
Emilian Peev538c90e2018-12-17 18:03:19 +0000979 if (err == OK) {
Emilian Peev2f5d6012022-01-19 16:16:50 -0800980 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +0000981 mCompositeStreamMap.add(IInterface::asBinder(surfaces[0]->getIGraphicBufferProducer()),
982 compositeStream);
983 }
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800984 } else {
985 err = mDevice->createStream(surfaces, deferredConsumer, streamInfo.width,
986 streamInfo.height, streamInfo.format, streamInfo.dataSpace,
Emilian Peevf4816702020-04-03 15:44:51 -0700987 static_cast<camera_stream_rotation_t>(outputConfiguration.getRotation()),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800988 &streamId, physicalCameraId, streamInfo.sensorPixelModesUsed, &surfaceIds,
Emilian Peev2295df72021-11-12 18:14:10 -0800989 outputConfiguration.getSurfaceSetID(), isShared, isMultiResolution,
Shuzhen Wange4208922022-02-01 16:52:48 -0800990 /*consumerUsage*/0, streamInfo.dynamicRangeProfile, streamInfo.streamUseCase,
Shuzhen Wangbce53db2022-12-03 00:38:20 +0000991 streamInfo.timestampBase, streamInfo.mirrorMode, streamInfo.colorSpace,
992 useReadoutTimestamp);
Emilian Peev538c90e2018-12-17 18:03:19 +0000993 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700994
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800995 if (err != OK) {
996 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800997 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
Shuzhen Wang758c2152017-01-10 18:26:18 -0800998 mCameraIdStr.string(), streamInfo.width, streamInfo.height, streamInfo.format,
999 streamInfo.dataSpace, strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001000 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001001 int i = 0;
1002 for (auto& binder : binders) {
1003 ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %d",
1004 __FUNCTION__, binder.get(), streamId, i);
Emilian Peev40ead602017-09-26 15:46:36 +01001005 mStreamMap.add(binder, StreamSurfaceId(streamId, surfaceIds[i]));
1006 i++;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001007 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001008
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -08001009 mConfiguredOutputs.add(streamId, outputConfiguration);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001010 mStreamInfoMap[streamId] = streamInfo;
1011
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001012 ALOGV("%s: Camera %s: Successfully created a new stream ID %d for output surface"
Shuzhen Wang0129d522016-10-30 22:43:41 -07001013 " (%d x %d) with format 0x%x.",
Shuzhen Wang758c2152017-01-10 18:26:18 -08001014 __FUNCTION__, mCameraIdStr.string(), streamId, streamInfo.width,
1015 streamInfo.height, streamInfo.format);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001016
Zhijun He5d677d12016-05-29 16:52:39 -07001017 // Set transform flags to ensure preview to be rotated correctly.
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001018 res = setStreamTransformLocked(streamId, streamInfo.mirrorMode);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001019
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001020 // Fill in mHighResolutionCameraIdToStreamIdSet map
1021 const String8 &cameraIdUsed =
1022 physicalCameraId.size() != 0 ? physicalCameraId : mCameraIdStr;
1023 const char *cameraIdUsedCStr = cameraIdUsed.string();
1024 // Only needed for high resolution sensors
1025 if (mHighResolutionSensors.find(cameraIdUsedCStr) !=
1026 mHighResolutionSensors.end()) {
1027 mHighResolutionCameraIdToStreamIdSet[cameraIdUsedCStr].insert(streamId);
1028 }
1029
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001030 *newStreamId = streamId;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001031 }
1032
1033 return res;
1034}
1035
Zhijun He5d677d12016-05-29 16:52:39 -07001036binder::Status CameraDeviceClient::createDeferredSurfaceStreamLocked(
1037 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
Shuzhen Wang758c2152017-01-10 18:26:18 -08001038 bool isShared,
Zhijun He5d677d12016-05-29 16:52:39 -07001039 /*out*/
1040 int* newStreamId) {
1041 int width, height, format, surfaceType;
Emilian Peev050f5dc2017-05-18 14:43:56 +01001042 uint64_t consumerUsage;
Zhijun He5d677d12016-05-29 16:52:39 -07001043 android_dataspace dataSpace;
Austin Borger9e2b27c2022-07-15 11:27:24 -07001044 int32_t colorSpace;
Zhijun He5d677d12016-05-29 16:52:39 -07001045 status_t err;
1046 binder::Status res;
1047
1048 if (!mDevice.get()) {
1049 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1050 }
1051
1052 // Infer the surface info for deferred surface stream creation.
1053 width = outputConfiguration.getWidth();
1054 height = outputConfiguration.getHeight();
1055 surfaceType = outputConfiguration.getSurfaceType();
1056 format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
1057 dataSpace = android_dataspace_t::HAL_DATASPACE_UNKNOWN;
Austin Borger9e2b27c2022-07-15 11:27:24 -07001058 colorSpace = ANDROID_REQUEST_AVAILABLE_COLOR_SPACE_PROFILES_MAP_UNSPECIFIED;
Zhijun He5d677d12016-05-29 16:52:39 -07001059 // Hardcode consumer usage flags: SurfaceView--0x900, SurfaceTexture--0x100.
1060 consumerUsage = GraphicBuffer::USAGE_HW_TEXTURE;
1061 if (surfaceType == OutputConfiguration::SURFACE_TYPE_SURFACE_VIEW) {
1062 consumerUsage |= GraphicBuffer::USAGE_HW_COMPOSER;
1063 }
1064 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001065 std::vector<sp<Surface>> noSurface;
Emilian Peev40ead602017-09-26 15:46:36 +01001066 std::vector<int> surfaceIds;
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001067 String8 physicalCameraId(outputConfiguration.getPhysicalCameraId());
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001068 const String8 &cameraIdUsed =
1069 physicalCameraId.size() != 0 ? physicalCameraId : mCameraIdStr;
1070 // Here, we override sensor pixel modes
1071 std::unordered_set<int32_t> overriddenSensorPixelModesUsed;
1072 const std::vector<int32_t> &sensorPixelModesUsed =
1073 outputConfiguration.getSensorPixelModesUsed();
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07001074 if (SessionConfigurationUtils::checkAndOverrideSensorPixelModesUsed(
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001075 sensorPixelModesUsed, format, width, height, getStaticInfo(cameraIdUsed),
1076 /*allowRounding*/ false, &overriddenSensorPixelModesUsed) != OK) {
1077 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1078 "sensor pixel modes used not valid for deferred stream");
1079 }
1080
Shuzhen Wang0129d522016-10-30 22:43:41 -07001081 err = mDevice->createStream(noSurface, /*hasDeferredConsumer*/true, width,
1082 height, format, dataSpace,
Emilian Peevf4816702020-04-03 15:44:51 -07001083 static_cast<camera_stream_rotation_t>(outputConfiguration.getRotation()),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001084 &streamId, physicalCameraId,
1085 overriddenSensorPixelModesUsed,
1086 &surfaceIds,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001087 outputConfiguration.getSurfaceSetID(), isShared,
Emilian Peev2295df72021-11-12 18:14:10 -08001088 outputConfiguration.isMultiResolution(), consumerUsage,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001089 outputConfiguration.getDynamicRangeProfile(),
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001090 outputConfiguration.getStreamUseCase(),
Shuzhen Wangbce53db2022-12-03 00:38:20 +00001091 outputConfiguration.getMirrorMode(),
1092 outputConfiguration.useReadoutTimestamp());
Zhijun He5d677d12016-05-29 16:52:39 -07001093
1094 if (err != OK) {
1095 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001096 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
1097 mCameraIdStr.string(), width, height, format, dataSpace, strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001098 } else {
1099 // Can not add streamId to mStreamMap here, as the surface is deferred. Add it to
1100 // a separate list to track. Once the deferred surface is set, this id will be
1101 // relocated to mStreamMap.
1102 mDeferredStreams.push_back(streamId);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001103 mStreamInfoMap.emplace(std::piecewise_construct, std::forward_as_tuple(streamId),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001104 std::forward_as_tuple(width, height, format, dataSpace, consumerUsage,
Emilian Peev2295df72021-11-12 18:14:10 -08001105 overriddenSensorPixelModesUsed,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001106 outputConfiguration.getDynamicRangeProfile(),
Shuzhen Wange4208922022-02-01 16:52:48 -08001107 outputConfiguration.getStreamUseCase(),
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001108 outputConfiguration.getTimestampBase(),
Austin Borger9e2b27c2022-07-15 11:27:24 -07001109 outputConfiguration.getMirrorMode(),
1110 colorSpace));
Shuzhen Wang758c2152017-01-10 18:26:18 -08001111
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001112 ALOGV("%s: Camera %s: Successfully created a new stream ID %d for a deferred surface"
Zhijun He5d677d12016-05-29 16:52:39 -07001113 " (%d x %d) stream with format 0x%x.",
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001114 __FUNCTION__, mCameraIdStr.string(), streamId, width, height, format);
Zhijun He5d677d12016-05-29 16:52:39 -07001115
1116 // Set transform flags to ensure preview to be rotated correctly.
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001117 res = setStreamTransformLocked(streamId, outputConfiguration.getMirrorMode());
Zhijun He5d677d12016-05-29 16:52:39 -07001118
1119 *newStreamId = streamId;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001120 // Fill in mHighResolutionCameraIdToStreamIdSet
1121 const char *cameraIdUsedCStr = cameraIdUsed.string();
1122 // Only needed for high resolution sensors
1123 if (mHighResolutionSensors.find(cameraIdUsedCStr) !=
1124 mHighResolutionSensors.end()) {
1125 mHighResolutionCameraIdToStreamIdSet[cameraIdUsed.string()].insert(streamId);
1126 }
Zhijun He5d677d12016-05-29 16:52:39 -07001127 }
1128 return res;
1129}
1130
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001131binder::Status CameraDeviceClient::setStreamTransformLocked(int streamId, int mirrorMode) {
Zhijun He5d677d12016-05-29 16:52:39 -07001132 int32_t transform = 0;
1133 status_t err;
1134 binder::Status res;
1135
1136 if (!mDevice.get()) {
1137 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1138 }
1139
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001140 err = getRotationTransformLocked(mirrorMode, &transform);
Zhijun He5d677d12016-05-29 16:52:39 -07001141 if (err != OK) {
1142 // Error logged by getRotationTransformLocked.
1143 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
1144 "Unable to calculate rotation transform for new stream");
1145 }
1146
1147 err = mDevice->setStreamTransform(streamId, transform);
1148 if (err != OK) {
1149 String8 msg = String8::format("Failed to set stream transform (stream id %d)",
1150 streamId);
1151 ALOGE("%s: %s", __FUNCTION__, msg.string());
1152 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
1153 }
1154
1155 return res;
1156}
Ruben Brunkbba75572014-11-20 17:29:50 -08001157
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001158binder::Status CameraDeviceClient::createInputStream(
Shuzhen Wang83bff122020-11-20 15:51:39 -08001159 int width, int height, int format, bool isMultiResolution,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001160 /*out*/
1161 int32_t* newStreamId) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001162
1163 ATRACE_CALL();
Shuzhen Wang83bff122020-11-20 15:51:39 -08001164 ALOGV("%s (w = %d, h = %d, f = 0x%x, isMultiResolution %d)", __FUNCTION__,
1165 width, height, format, isMultiResolution);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001166
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001167 binder::Status res;
1168 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001169
1170 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001171
1172 if (!mDevice.get()) {
1173 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1174 }
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001175
1176 if (mInputStream.configured) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001177 String8 msg = String8::format("Camera %s: Already has an input stream "
Yi Kong0f414de2017-12-15 13:48:50 -08001178 "configured (ID %d)", mCameraIdStr.string(), mInputStream.id);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001179 ALOGE("%s: %s", __FUNCTION__, msg.string() );
1180 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001181 }
1182
1183 int streamId = -1;
Shuzhen Wang83bff122020-11-20 15:51:39 -08001184 status_t err = mDevice->createInputStream(width, height, format, isMultiResolution, &streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001185 if (err == OK) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001186 mInputStream.configured = true;
1187 mInputStream.width = width;
1188 mInputStream.height = height;
1189 mInputStream.format = format;
1190 mInputStream.id = streamId;
1191
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001192 ALOGV("%s: Camera %s: Successfully created a new input stream ID %d",
1193 __FUNCTION__, mCameraIdStr.string(), streamId);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001194
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001195 *newStreamId = streamId;
1196 } else {
1197 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001198 "Camera %s: Error creating new input stream: %s (%d)", mCameraIdStr.string(),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001199 strerror(-err), err);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001200 }
1201
1202 return res;
1203}
1204
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001205binder::Status CameraDeviceClient::getInputSurface(/*out*/ view::Surface *inputSurface) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001206
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001207 binder::Status res;
1208 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1209
1210 if (inputSurface == NULL) {
1211 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Null input surface");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001212 }
1213
1214 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001215 if (!mDevice.get()) {
1216 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1217 }
1218 sp<IGraphicBufferProducer> producer;
1219 status_t err = mDevice->getInputBufferProducer(&producer);
1220 if (err != OK) {
1221 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001222 "Camera %s: Error getting input Surface: %s (%d)",
1223 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001224 } else {
1225 inputSurface->name = String16("CameraInput");
1226 inputSurface->graphicBufferProducer = producer;
1227 }
1228 return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001229}
1230
Emilian Peev40ead602017-09-26 15:46:36 +01001231binder::Status CameraDeviceClient::updateOutputConfiguration(int streamId,
1232 const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
1233 ATRACE_CALL();
1234
1235 binder::Status res;
1236 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1237
1238 Mutex::Autolock icl(mBinderSerializationLock);
1239
1240 if (!mDevice.get()) {
1241 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1242 }
1243
1244 const std::vector<sp<IGraphicBufferProducer> >& bufferProducers =
1245 outputConfiguration.getGraphicBufferProducers();
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -07001246 String8 physicalCameraId(outputConfiguration.getPhysicalCameraId());
1247
Emilian Peev40ead602017-09-26 15:46:36 +01001248 auto producerCount = bufferProducers.size();
1249 if (producerCount == 0) {
1250 ALOGE("%s: bufferProducers must not be empty", __FUNCTION__);
1251 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1252 "bufferProducers must not be empty");
1253 }
1254
1255 // The first output is the one associated with the output configuration.
1256 // It should always be present, valid and the corresponding stream id should match.
1257 sp<IBinder> binder = IInterface::asBinder(bufferProducers[0]);
1258 ssize_t index = mStreamMap.indexOfKey(binder);
1259 if (index == NAME_NOT_FOUND) {
1260 ALOGE("%s: Outputconfiguration is invalid", __FUNCTION__);
1261 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1262 "OutputConfiguration is invalid");
1263 }
1264 if (mStreamMap.valueFor(binder).streamId() != streamId) {
1265 ALOGE("%s: Stream Id: %d provided doesn't match the id: %d in the stream map",
1266 __FUNCTION__, streamId, mStreamMap.valueFor(binder).streamId());
1267 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1268 "Stream id is invalid");
1269 }
1270
1271 std::vector<size_t> removedSurfaceIds;
1272 std::vector<sp<IBinder>> removedOutputs;
1273 std::vector<sp<Surface>> newOutputs;
1274 std::vector<OutputStreamInfo> streamInfos;
1275 KeyedVector<sp<IBinder>, sp<IGraphicBufferProducer>> newOutputsMap;
1276 for (auto &it : bufferProducers) {
1277 newOutputsMap.add(IInterface::asBinder(it), it);
1278 }
1279
1280 for (size_t i = 0; i < mStreamMap.size(); i++) {
1281 ssize_t idx = newOutputsMap.indexOfKey(mStreamMap.keyAt(i));
1282 if (idx == NAME_NOT_FOUND) {
1283 if (mStreamMap[i].streamId() == streamId) {
1284 removedSurfaceIds.push_back(mStreamMap[i].surfaceId());
1285 removedOutputs.push_back(mStreamMap.keyAt(i));
1286 }
1287 } else {
1288 if (mStreamMap[i].streamId() != streamId) {
1289 ALOGE("%s: Output surface already part of a different stream", __FUNCTION__);
1290 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1291 "Target Surface is invalid");
1292 }
1293 newOutputsMap.removeItemsAt(idx);
1294 }
1295 }
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001296 const std::vector<int32_t> &sensorPixelModesUsed =
1297 outputConfiguration.getSensorPixelModesUsed();
Shuzhen Wang8ed1e872022-03-08 16:34:33 -08001298 int64_t streamUseCase = outputConfiguration.getStreamUseCase();
Shuzhen Wange4208922022-02-01 16:52:48 -08001299 int timestampBase = outputConfiguration.getTimestampBase();
Emilian Peevc81a7592022-02-14 17:38:18 -08001300 int64_t dynamicRangeProfile = outputConfiguration.getDynamicRangeProfile();
Austin Borger9e2b27c2022-07-15 11:27:24 -07001301 int32_t colorSpace = outputConfiguration.getColorSpace();
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001302 int mirrorMode = outputConfiguration.getMirrorMode();
Emilian Peev2295df72021-11-12 18:14:10 -08001303
Emilian Peev40ead602017-09-26 15:46:36 +01001304 for (size_t i = 0; i < newOutputsMap.size(); i++) {
1305 OutputStreamInfo outInfo;
1306 sp<Surface> surface;
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07001307 res = SessionConfigurationUtils::createSurfaceFromGbp(outInfo,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001308 /*isStreamInfoValid*/ false, surface, newOutputsMap.valueAt(i), mCameraIdStr,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001309 mDevice->infoPhysical(physicalCameraId), sensorPixelModesUsed, dynamicRangeProfile,
Austin Borger9e2b27c2022-07-15 11:27:24 -07001310 streamUseCase, timestampBase, mirrorMode, colorSpace);
Emilian Peev40ead602017-09-26 15:46:36 +01001311 if (!res.isOk())
1312 return res;
1313
Emilian Peev40ead602017-09-26 15:46:36 +01001314 streamInfos.push_back(outInfo);
1315 newOutputs.push_back(surface);
1316 }
1317
1318 //Trivial case no changes required
1319 if (removedSurfaceIds.empty() && newOutputs.empty()) {
1320 return binder::Status::ok();
1321 }
1322
1323 KeyedVector<sp<Surface>, size_t> outputMap;
1324 auto ret = mDevice->updateStream(streamId, newOutputs, streamInfos, removedSurfaceIds,
1325 &outputMap);
1326 if (ret != OK) {
1327 switch (ret) {
1328 case NAME_NOT_FOUND:
1329 case BAD_VALUE:
1330 case -EBUSY:
1331 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1332 "Camera %s: Error updating stream: %s (%d)",
1333 mCameraIdStr.string(), strerror(ret), ret);
1334 break;
1335 default:
1336 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1337 "Camera %s: Error updating stream: %s (%d)",
1338 mCameraIdStr.string(), strerror(ret), ret);
1339 break;
1340 }
1341 } else {
1342 for (const auto &it : removedOutputs) {
1343 mStreamMap.removeItem(it);
1344 }
1345
1346 for (size_t i = 0; i < outputMap.size(); i++) {
1347 mStreamMap.add(IInterface::asBinder(outputMap.keyAt(i)->getIGraphicBufferProducer()),
1348 StreamSurfaceId(streamId, outputMap.valueAt(i)));
1349 }
1350
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -08001351 mConfiguredOutputs.replaceValueFor(streamId, outputConfiguration);
1352
Emilian Peev40ead602017-09-26 15:46:36 +01001353 ALOGV("%s: Camera %s: Successful stream ID %d update",
1354 __FUNCTION__, mCameraIdStr.string(), streamId);
1355 }
1356
1357 return res;
1358}
1359
Igor Murashkine7ee7632013-06-11 18:10:18 -07001360// Create a request object from a template.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001361binder::Status CameraDeviceClient::createDefaultRequest(int templateId,
1362 /*out*/
1363 hardware::camera2::impl::CameraMetadataNative* request)
Igor Murashkine7ee7632013-06-11 18:10:18 -07001364{
1365 ATRACE_CALL();
1366 ALOGV("%s (templateId = 0x%x)", __FUNCTION__, templateId);
1367
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001368 binder::Status res;
1369 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001370
1371 Mutex::Autolock icl(mBinderSerializationLock);
1372
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001373 if (!mDevice.get()) {
1374 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1375 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001376
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001377 status_t err;
Emilian Peevf4816702020-04-03 15:44:51 -07001378 camera_request_template_t tempId = camera_request_template_t::CAMERA_TEMPLATE_COUNT;
1379 if (!(res = mapRequestTemplate(templateId, &tempId)).isOk()) return res;
1380
1381 CameraMetadata metadata;
1382 if ( (err = mDevice->createDefaultRequest(tempId, &metadata) ) == OK &&
Igor Murashkine7ee7632013-06-11 18:10:18 -07001383 request != NULL) {
1384
1385 request->swap(metadata);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001386 } else if (err == BAD_VALUE) {
1387 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001388 "Camera %s: Template ID %d is invalid or not supported: %s (%d)",
1389 mCameraIdStr.string(), templateId, strerror(-err), err);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001390
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001391 } else {
1392 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001393 "Camera %s: Error creating default request for template %d: %s (%d)",
1394 mCameraIdStr.string(), templateId, strerror(-err), err);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001395 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001396 return res;
1397}
1398
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001399binder::Status CameraDeviceClient::getCameraInfo(
1400 /*out*/
1401 hardware::camera2::impl::CameraMetadataNative* info)
Igor Murashkine7ee7632013-06-11 18:10:18 -07001402{
1403 ATRACE_CALL();
1404 ALOGV("%s", __FUNCTION__);
1405
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001406 binder::Status res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001407
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001408 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001409
1410 Mutex::Autolock icl(mBinderSerializationLock);
1411
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001412 if (!mDevice.get()) {
1413 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1414 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001415
Igor Murashkin099b4572013-07-12 17:52:16 -07001416 if (info != NULL) {
1417 *info = mDevice->info(); // static camera metadata
1418 // TODO: merge with device-specific camera metadata
1419 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001420
1421 return res;
1422}
1423
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001424binder::Status CameraDeviceClient::waitUntilIdle()
Zhijun He2ab500c2013-07-23 08:02:53 -07001425{
1426 ATRACE_CALL();
1427 ALOGV("%s", __FUNCTION__);
1428
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001429 binder::Status res;
1430 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Zhijun He2ab500c2013-07-23 08:02:53 -07001431
1432 Mutex::Autolock icl(mBinderSerializationLock);
1433
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001434 if (!mDevice.get()) {
1435 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1436 }
Zhijun He2ab500c2013-07-23 08:02:53 -07001437
1438 // FIXME: Also need check repeating burst.
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001439 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001440 if (mStreamingRequestId != REQUEST_ID_NONE) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001441 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001442 "Camera %s: Try to waitUntilIdle when there are active streaming requests",
1443 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001444 ALOGE("%s: %s", __FUNCTION__, msg.string());
1445 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Zhijun He2ab500c2013-07-23 08:02:53 -07001446 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001447 status_t err = mDevice->waitUntilDrained();
1448 if (err != OK) {
1449 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001450 "Camera %s: Error waiting to drain: %s (%d)",
1451 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001452 }
Zhijun He2ab500c2013-07-23 08:02:53 -07001453 ALOGV("%s Done", __FUNCTION__);
Zhijun He2ab500c2013-07-23 08:02:53 -07001454 return res;
1455}
1456
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001457binder::Status CameraDeviceClient::flush(
1458 /*out*/
1459 int64_t* lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001460 ATRACE_CALL();
1461 ALOGV("%s", __FUNCTION__);
1462
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001463 binder::Status res;
1464 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001465
1466 Mutex::Autolock icl(mBinderSerializationLock);
1467
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001468 if (!mDevice.get()) {
1469 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1470 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001471
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001472 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001473 mStreamingRequestId = REQUEST_ID_NONE;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001474 status_t err = mDevice->flush(lastFrameNumber);
1475 if (err != OK) {
1476 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001477 "Camera %s: Error flushing device: %s (%d)", mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001478 }
1479 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001480}
1481
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001482binder::Status CameraDeviceClient::prepare(int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001483 ATRACE_CALL();
1484 ALOGV("%s", __FUNCTION__);
1485
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001486 binder::Status res;
1487 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001488
1489 Mutex::Autolock icl(mBinderSerializationLock);
1490
1491 // Guard against trying to prepare non-created streams
1492 ssize_t index = NAME_NOT_FOUND;
1493 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001494 if (streamId == mStreamMap.valueAt(i).streamId()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001495 index = i;
1496 break;
1497 }
1498 }
1499
1500 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001501 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1502 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001503 ALOGW("%s: %s", __FUNCTION__, msg.string());
1504 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001505 }
1506
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001507 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1508 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001509 status_t err = mDevice->prepare(streamId);
1510 if (err == BAD_VALUE) {
1511 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001512 "Camera %s: Stream %d has already been used, and cannot be prepared",
1513 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001514 } else if (err != OK) {
1515 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001516 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001517 strerror(-err), err);
1518 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001519 return res;
1520}
1521
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001522binder::Status CameraDeviceClient::prepare2(int maxCount, int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001523 ATRACE_CALL();
1524 ALOGV("%s", __FUNCTION__);
1525
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001526 binder::Status res;
1527 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Ruben Brunkc78ac262015-08-13 17:58:46 -07001528
1529 Mutex::Autolock icl(mBinderSerializationLock);
1530
1531 // Guard against trying to prepare non-created streams
1532 ssize_t index = NAME_NOT_FOUND;
1533 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001534 if (streamId == mStreamMap.valueAt(i).streamId()) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001535 index = i;
1536 break;
1537 }
1538 }
1539
1540 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001541 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1542 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001543 ALOGW("%s: %s", __FUNCTION__, msg.string());
1544 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -07001545 }
1546
1547 if (maxCount <= 0) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001548 String8 msg = String8::format("Camera %s: maxCount (%d) must be greater than 0",
1549 mCameraIdStr.string(), maxCount);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001550 ALOGE("%s: %s", __FUNCTION__, msg.string());
1551 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -07001552 }
1553
1554 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1555 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001556 status_t err = mDevice->prepare(maxCount, streamId);
1557 if (err == BAD_VALUE) {
1558 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001559 "Camera %s: Stream %d has already been used, and cannot be prepared",
1560 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001561 } else if (err != OK) {
1562 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001563 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001564 strerror(-err), err);
1565 }
Ruben Brunkc78ac262015-08-13 17:58:46 -07001566
1567 return res;
1568}
1569
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001570binder::Status CameraDeviceClient::tearDown(int streamId) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001571 ATRACE_CALL();
1572 ALOGV("%s", __FUNCTION__);
1573
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001574 binder::Status res;
1575 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001576
1577 Mutex::Autolock icl(mBinderSerializationLock);
1578
1579 // Guard against trying to prepare non-created streams
1580 ssize_t index = NAME_NOT_FOUND;
1581 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001582 if (streamId == mStreamMap.valueAt(i).streamId()) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001583 index = i;
1584 break;
1585 }
1586 }
1587
1588 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001589 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1590 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001591 ALOGW("%s: %s", __FUNCTION__, msg.string());
1592 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001593 }
1594
1595 // Also returns BAD_VALUE if stream ID was not valid or if the stream is in
1596 // use
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001597 status_t err = mDevice->tearDown(streamId);
1598 if (err == BAD_VALUE) {
1599 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001600 "Camera %s: Stream %d is still in use, cannot be torn down",
1601 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001602 } else if (err != OK) {
1603 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001604 "Camera %s: Error tearing down stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001605 strerror(-err), err);
1606 }
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001607
1608 return res;
1609}
1610
Shuzhen Wang758c2152017-01-10 18:26:18 -08001611binder::Status CameraDeviceClient::finalizeOutputConfigurations(int32_t streamId,
Zhijun He5d677d12016-05-29 16:52:39 -07001612 const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
1613 ATRACE_CALL();
1614
1615 binder::Status res;
1616 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1617
1618 Mutex::Autolock icl(mBinderSerializationLock);
1619
Shuzhen Wang0129d522016-10-30 22:43:41 -07001620 const std::vector<sp<IGraphicBufferProducer> >& bufferProducers =
1621 outputConfiguration.getGraphicBufferProducers();
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -07001622 String8 physicalId(outputConfiguration.getPhysicalCameraId());
Zhijun He5d677d12016-05-29 16:52:39 -07001623
Shuzhen Wang0129d522016-10-30 22:43:41 -07001624 if (bufferProducers.size() == 0) {
1625 ALOGE("%s: bufferProducers must not be empty", __FUNCTION__);
Zhijun He5d677d12016-05-29 16:52:39 -07001626 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Target Surface is invalid");
1627 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001628
Shuzhen Wang758c2152017-01-10 18:26:18 -08001629 // streamId should be in mStreamMap if this stream already has a surface attached
1630 // to it. Otherwise, it should be in mDeferredStreams.
1631 bool streamIdConfigured = false;
1632 ssize_t deferredStreamIndex = NAME_NOT_FOUND;
1633 for (size_t i = 0; i < mStreamMap.size(); i++) {
1634 if (mStreamMap.valueAt(i).streamId() == streamId) {
1635 streamIdConfigured = true;
1636 break;
1637 }
Zhijun He5d677d12016-05-29 16:52:39 -07001638 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001639 for (size_t i = 0; i < mDeferredStreams.size(); i++) {
1640 if (streamId == mDeferredStreams[i]) {
1641 deferredStreamIndex = i;
1642 break;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001643 }
1644
Shuzhen Wang758c2152017-01-10 18:26:18 -08001645 }
1646 if (deferredStreamIndex == NAME_NOT_FOUND && !streamIdConfigured) {
1647 String8 msg = String8::format("Camera %s: deferred surface is set to a unknown stream"
1648 "(ID %d)", mCameraIdStr.string(), streamId);
1649 ALOGW("%s: %s", __FUNCTION__, msg.string());
1650 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Zhijun He5d677d12016-05-29 16:52:39 -07001651 }
1652
Shuzhen Wang88fd0052017-02-09 16:40:07 -08001653 if (mStreamInfoMap[streamId].finalized) {
1654 String8 msg = String8::format("Camera %s: finalizeOutputConfigurations has been called"
1655 " on stream ID %d", mCameraIdStr.string(), streamId);
1656 ALOGW("%s: %s", __FUNCTION__, msg.string());
1657 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1658 }
1659
Zhijun He5d677d12016-05-29 16:52:39 -07001660 if (!mDevice.get()) {
1661 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1662 }
1663
Shuzhen Wang758c2152017-01-10 18:26:18 -08001664 std::vector<sp<Surface>> consumerSurfaces;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001665 const std::vector<int32_t> &sensorPixelModesUsed =
1666 outputConfiguration.getSensorPixelModesUsed();
Emilian Peevc81a7592022-02-14 17:38:18 -08001667 int64_t dynamicRangeProfile = outputConfiguration.getDynamicRangeProfile();
Austin Borger9e2b27c2022-07-15 11:27:24 -07001668 int32_t colorSpace = outputConfiguration.getColorSpace();
1669 int64_t streamUseCase = outputConfiguration.getStreamUseCase();
Shuzhen Wange4208922022-02-01 16:52:48 -08001670 int timestampBase = outputConfiguration.getTimestampBase();
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001671 int mirrorMode = outputConfiguration.getMirrorMode();
Shuzhen Wang758c2152017-01-10 18:26:18 -08001672 for (auto& bufferProducer : bufferProducers) {
1673 // Don't create multiple streams for the same target surface
Zhijun He5d677d12016-05-29 16:52:39 -07001674 ssize_t index = mStreamMap.indexOfKey(IInterface::asBinder(bufferProducer));
1675 if (index != NAME_NOT_FOUND) {
Shuzhen Wang758c2152017-01-10 18:26:18 -08001676 ALOGV("Camera %s: Surface already has a stream created "
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001677 " for it (ID %zd)", mCameraIdStr.string(), index);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001678 continue;
Zhijun He5d677d12016-05-29 16:52:39 -07001679 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001680
1681 sp<Surface> surface;
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07001682 res = SessionConfigurationUtils::createSurfaceFromGbp(mStreamInfoMap[streamId],
Colin Crossb8a9dbb2020-08-27 04:12:26 +00001683 true /*isStreamInfoValid*/, surface, bufferProducer, mCameraIdStr,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001684 mDevice->infoPhysical(physicalId), sensorPixelModesUsed, dynamicRangeProfile,
Austin Borger9e2b27c2022-07-15 11:27:24 -07001685 streamUseCase, timestampBase, mirrorMode, colorSpace);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001686
1687 if (!res.isOk())
1688 return res;
1689
1690 consumerSurfaces.push_back(surface);
Zhijun He5d677d12016-05-29 16:52:39 -07001691 }
1692
Shuzhen Wanga81ce342017-04-13 15:18:36 -07001693 // Gracefully handle case where finalizeOutputConfigurations is called
1694 // without any new surface.
1695 if (consumerSurfaces.size() == 0) {
1696 mStreamInfoMap[streamId].finalized = true;
1697 return res;
1698 }
1699
Zhijun He5d677d12016-05-29 16:52:39 -07001700 // Finish the deferred stream configuration with the surface.
Shuzhen Wang758c2152017-01-10 18:26:18 -08001701 status_t err;
Emilian Peev40ead602017-09-26 15:46:36 +01001702 std::vector<int> consumerSurfaceIds;
1703 err = mDevice->setConsumerSurfaces(streamId, consumerSurfaces, &consumerSurfaceIds);
Zhijun He5d677d12016-05-29 16:52:39 -07001704 if (err == OK) {
Shuzhen Wang758c2152017-01-10 18:26:18 -08001705 for (size_t i = 0; i < consumerSurfaces.size(); i++) {
1706 sp<IBinder> binder = IInterface::asBinder(
1707 consumerSurfaces[i]->getIGraphicBufferProducer());
Emilian Peev40ead602017-09-26 15:46:36 +01001708 ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %d", __FUNCTION__,
Shuzhen Wang758c2152017-01-10 18:26:18 -08001709 binder.get(), streamId, consumerSurfaceIds[i]);
1710 mStreamMap.add(binder, StreamSurfaceId(streamId, consumerSurfaceIds[i]));
1711 }
1712 if (deferredStreamIndex != NAME_NOT_FOUND) {
1713 mDeferredStreams.removeItemsAt(deferredStreamIndex);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001714 }
Shuzhen Wang88fd0052017-02-09 16:40:07 -08001715 mStreamInfoMap[streamId].finalized = true;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -08001716 mConfiguredOutputs.replaceValueFor(streamId, outputConfiguration);
Zhijun He5d677d12016-05-29 16:52:39 -07001717 } else if (err == NO_INIT) {
1718 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001719 "Camera %s: Deferred surface is invalid: %s (%d)",
1720 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001721 } else {
1722 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001723 "Camera %s: Error setting output stream deferred surface: %s (%d)",
1724 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001725 }
1726
1727 return res;
1728}
1729
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001730binder::Status CameraDeviceClient::setCameraAudioRestriction(int32_t mode) {
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001731 ATRACE_CALL();
1732 binder::Status res;
1733 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1734
1735 if (!isValidAudioRestriction(mode)) {
1736 String8 msg = String8::format("Camera %s: invalid audio restriction mode %d",
1737 mCameraIdStr.string(), mode);
1738 ALOGW("%s: %s", __FUNCTION__, msg.string());
1739 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1740 }
1741
1742 Mutex::Autolock icl(mBinderSerializationLock);
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001743 BasicClient::setAudioRestriction(mode);
1744 return binder::Status::ok();
1745}
1746
1747binder::Status CameraDeviceClient::getGlobalAudioRestriction(/*out*/ int32_t* outMode) {
1748 ATRACE_CALL();
1749 binder::Status res;
1750 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1751 Mutex::Autolock icl(mBinderSerializationLock);
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001752 if (outMode != nullptr) {
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001753 *outMode = BasicClient::getServiceAudioRestriction();
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001754 }
1755 return binder::Status::ok();
1756}
1757
Ravneetaeb20dc2022-03-30 05:33:03 +00001758status_t CameraDeviceClient::setCameraServiceWatchdog(bool enabled) {
1759 return mDevice->setCameraServiceWatchdog(enabled);
1760}
1761
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08001762status_t CameraDeviceClient::setRotateAndCropOverride(uint8_t rotateAndCrop) {
1763 if (rotateAndCrop > ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return BAD_VALUE;
1764
1765 return mDevice->setRotateAndCropAutoBehavior(
1766 static_cast<camera_metadata_enum_android_scaler_rotate_and_crop_t>(rotateAndCrop));
1767}
1768
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00001769status_t CameraDeviceClient::setAutoframingOverride(uint8_t autoframingValue) {
1770 if (autoframingValue > ANDROID_CONTROL_AUTOFRAMING_AUTO) return BAD_VALUE;
1771
1772 return mDevice->setAutoframingAutoBehavior(
1773 static_cast<camera_metadata_enum_android_control_autoframing_t>(autoframingValue));
1774}
1775
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08001776bool CameraDeviceClient::supportsCameraMute() {
1777 return mDevice->supportsCameraMute();
1778}
1779
1780status_t CameraDeviceClient::setCameraMute(bool enabled) {
1781 return mDevice->setCameraMute(enabled);
1782}
1783
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001784binder::Status CameraDeviceClient::switchToOffline(
1785 const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb,
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001786 const std::vector<int>& offlineOutputIds,
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001787 /*out*/
1788 sp<hardware::camera2::ICameraOfflineSession>* session) {
1789 ATRACE_CALL();
1790
1791 binder::Status res;
1792 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1793
1794 Mutex::Autolock icl(mBinderSerializationLock);
1795
1796 if (!mDevice.get()) {
1797 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1798 }
1799
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001800 if (offlineOutputIds.empty()) {
Emilian Peevcc0b7952020-01-07 13:54:47 -08001801 String8 msg = String8::format("Offline surfaces must not be empty");
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001802 ALOGE("%s: %s", __FUNCTION__, msg.string());
1803 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1804 }
1805
1806 if (session == nullptr) {
1807 String8 msg = String8::format("Invalid offline session");
1808 ALOGE("%s: %s", __FUNCTION__, msg.string());
1809 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1810 }
1811
Yin-Chia Yeh87fccca2020-01-28 09:37:18 -08001812 std::vector<int32_t> offlineStreamIds;
1813 offlineStreamIds.reserve(offlineOutputIds.size());
Emilian Peev4697b642019-11-19 17:11:14 -08001814 KeyedVector<sp<IBinder>, sp<CompositeStream>> offlineCompositeStreamMap;
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001815 for (const auto& streamId : offlineOutputIds) {
1816 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001817 if (index == NAME_NOT_FOUND) {
Emilian Peevcc0b7952020-01-07 13:54:47 -08001818 String8 msg = String8::format("Offline surface with id: %d is not registered",
1819 streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001820 ALOGE("%s: %s", __FUNCTION__, msg.string());
1821 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1822 }
Emilian Peevcc0b7952020-01-07 13:54:47 -08001823
1824 if (!mStreamInfoMap[streamId].supportsOffline) {
1825 String8 msg = String8::format("Offline surface with id: %d doesn't support "
1826 "offline mode", streamId);
1827 ALOGE("%s: %s", __FUNCTION__, msg.string());
1828 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1829 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001830
Emilian Peev2f5d6012022-01-19 16:16:50 -08001831 Mutex::Autolock l(mCompositeLock);
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001832 bool isCompositeStream = false;
Emilian Peev2f5d6012022-01-19 16:16:50 -08001833 for (const auto& gbp : mConfiguredOutputs.valueAt(index).getGraphicBufferProducers()) {
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001834 sp<Surface> s = new Surface(gbp, false /*controlledByApp*/);
Pirama Arumuga Nainar8db21062022-01-28 10:15:12 -08001835 isCompositeStream = camera3::DepthCompositeStream::isDepthCompositeStream(s) ||
Emilian Peev434248e2022-10-06 14:58:54 -07001836 camera3::HeicCompositeStream::isHeicCompositeStream(s) ||
1837 camera3::JpegRCompositeStream::isJpegRCompositeStream(s);
Emilian Peev4697b642019-11-19 17:11:14 -08001838 if (isCompositeStream) {
1839 auto compositeIdx = mCompositeStreamMap.indexOfKey(IInterface::asBinder(gbp));
1840 if (compositeIdx == NAME_NOT_FOUND) {
1841 ALOGE("%s: Unknown composite stream", __FUNCTION__);
1842 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1843 "Unknown composite stream");
1844 }
1845
1846 mCompositeStreamMap.valueAt(compositeIdx)->insertCompositeStreamIds(
1847 &offlineStreamIds);
1848 offlineCompositeStreamMap.add(mCompositeStreamMap.keyAt(compositeIdx),
1849 mCompositeStreamMap.valueAt(compositeIdx));
1850 break;
1851 }
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001852 }
1853
Emilian Peev4697b642019-11-19 17:11:14 -08001854 if (!isCompositeStream) {
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001855 offlineStreamIds.push_back(streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001856 }
1857 }
1858
1859 sp<CameraOfflineSessionBase> offlineSession;
1860 auto ret = mDevice->switchToOffline(offlineStreamIds, &offlineSession);
1861 if (ret != OK) {
1862 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1863 "Camera %s: Error switching to offline mode: %s (%d)",
1864 mCameraIdStr.string(), strerror(ret), ret);
1865 }
1866
Emilian Peevcc0b7952020-01-07 13:54:47 -08001867 sp<CameraOfflineSessionClient> offlineClient;
1868 if (offlineSession.get() != nullptr) {
1869 offlineClient = new CameraOfflineSessionClient(sCameraService,
1870 offlineSession, offlineCompositeStreamMap, cameraCb, mClientPackageName,
Emilian Peev8b64f282021-03-25 16:49:57 -07001871 mClientFeatureId, mCameraIdStr, mCameraFacing, mOrientation, mClientPid, mClientUid,
1872 mServicePid);
Emilian Peevcc0b7952020-01-07 13:54:47 -08001873 ret = sCameraService->addOfflineClient(mCameraIdStr, offlineClient);
1874 }
1875
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001876 if (ret == OK) {
Emilian Peevd99c8ae2019-11-26 13:19:13 -08001877 // A successful offline session switch must reset the current camera client
1878 // and release any resources occupied by previously configured streams.
1879 mStreamMap.clear();
1880 mConfiguredOutputs.clear();
1881 mDeferredStreams.clear();
1882 mStreamInfoMap.clear();
Emilian Peev2f5d6012022-01-19 16:16:50 -08001883 Mutex::Autolock l(mCompositeLock);
Emilian Peevd99c8ae2019-11-26 13:19:13 -08001884 mCompositeStreamMap.clear();
1885 mInputStream = {false, 0, 0, 0, 0};
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001886 } else {
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001887 switch(ret) {
1888 case BAD_VALUE:
1889 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1890 "Illegal argument to HAL module for camera \"%s\"", mCameraIdStr.c_str());
1891 case TIMED_OUT:
1892 return STATUS_ERROR_FMT(CameraService::ERROR_CAMERA_IN_USE,
1893 "Camera \"%s\" is already open", mCameraIdStr.c_str());
1894 default:
1895 return STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1896 "Failed to initialize camera \"%s\": %s (%d)", mCameraIdStr.c_str(),
1897 strerror(-ret), ret);
1898 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001899 }
1900
1901 *session = offlineClient;
1902
1903 return binder::Status::ok();
1904}
1905
Igor Murashkine7ee7632013-06-11 18:10:18 -07001906status_t CameraDeviceClient::dump(int fd, const Vector<String16>& args) {
Eino-Ville Talvalac4003962016-01-13 10:07:04 -08001907 return BasicClient::dump(fd, args);
1908}
1909
1910status_t CameraDeviceClient::dumpClient(int fd, const Vector<String16>& args) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001911 dprintf(fd, " CameraDeviceClient[%s] (%p) dump:\n",
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001912 mCameraIdStr.string(),
Eino-Ville Talvalae992e752014-11-07 16:17:48 -08001913 (getRemoteCallback() != NULL ?
Marco Nelissenf8880202014-11-14 07:58:25 -08001914 IInterface::asBinder(getRemoteCallback()).get() : NULL) );
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001915 dprintf(fd, " Current client UID %u\n", mClientUid);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001916
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001917 dprintf(fd, " State:\n");
1918 dprintf(fd, " Request ID counter: %d\n", mRequestIdCounter);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001919 if (mInputStream.configured) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001920 dprintf(fd, " Current input stream ID: %d\n", mInputStream.id);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001921 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001922 dprintf(fd, " No input stream configured.\n");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001923 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001924 if (!mStreamMap.isEmpty()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001925 dprintf(fd, " Current output stream/surface IDs:\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001926 for (size_t i = 0; i < mStreamMap.size(); i++) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001927 dprintf(fd, " Stream %d Surface %d\n",
Shuzhen Wang0129d522016-10-30 22:43:41 -07001928 mStreamMap.valueAt(i).streamId(),
1929 mStreamMap.valueAt(i).surfaceId());
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001930 }
Zhijun He5d677d12016-05-29 16:52:39 -07001931 } else if (!mDeferredStreams.isEmpty()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001932 dprintf(fd, " Current deferred surface output stream IDs:\n");
Zhijun He5d677d12016-05-29 16:52:39 -07001933 for (auto& streamId : mDeferredStreams) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001934 dprintf(fd, " Stream %d\n", streamId);
Zhijun He5d677d12016-05-29 16:52:39 -07001935 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001936 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001937 dprintf(fd, " No output streams configured.\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001938 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001939 // TODO: print dynamic/request section from most recent requests
1940 mFrameProcessor->dump(fd, args);
1941
1942 return dumpDevice(fd, args);
1943}
1944
Avichal Rakesh7e53cad2021-10-05 13:46:30 -07001945status_t CameraDeviceClient::startWatchingTags(const String8 &tags, int out) {
1946 sp<CameraDeviceBase> device = mDevice;
1947 if (!device) {
1948 dprintf(out, " Device is detached.");
1949 return OK;
1950 }
1951 device->startWatchingTags(tags);
1952 return OK;
1953}
1954
1955status_t CameraDeviceClient::stopWatchingTags(int out) {
1956 sp<CameraDeviceBase> device = mDevice;
1957 if (!device) {
1958 dprintf(out, " Device is detached.");
1959 return OK;
1960 }
1961 device->stopWatchingTags();
1962 return OK;
1963}
1964
1965status_t CameraDeviceClient::dumpWatchedEventsToVector(std::vector<std::string> &out) {
1966 sp<CameraDeviceBase> device = mDevice;
1967 if (!device) {
1968 return OK;
1969 }
1970 device->dumpWatchedEventsToVector(out);
1971 return OK;
1972}
1973
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001974void CameraDeviceClient::notifyError(int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -07001975 const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001976 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001977 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001978
Emilian Peev538c90e2018-12-17 18:03:19 +00001979 bool skipClientNotification = false;
Emilian Peev2f5d6012022-01-19 16:16:50 -08001980 {
1981 // Access to the composite stream map must be synchronized
1982 Mutex::Autolock l(mCompositeLock);
1983 // Composites can have multiple internal streams. Error notifications coming from such
1984 // internal streams may need to remain within camera service.
1985 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
1986 skipClientNotification |= mCompositeStreamMap.valueAt(i)->onError(errorCode,
1987 resultExtras);
1988 }
Emilian Peev538c90e2018-12-17 18:03:19 +00001989 }
1990
1991 if ((remoteCb != 0) && (!skipClientNotification)) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001992 remoteCb->onDeviceError(errorCode, resultExtras);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001993 }
1994}
1995
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001996void CameraDeviceClient::notifyRepeatingRequestError(long lastFrameNumber) {
1997 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
1998
1999 if (remoteCb != 0) {
Yin-Chia Yeh8ca23dc2017-09-05 18:15:56 -07002000 remoteCb->onRepeatingRequestError(lastFrameNumber, mStreamingRequestId);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07002001 }
2002
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07002003 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07002004 mStreamingRequestId = REQUEST_ID_NONE;
2005}
2006
Shuzhen Wang316781a2020-08-18 18:11:01 -07002007void CameraDeviceClient::notifyIdle(
2008 int64_t requestCount, int64_t resultErrorCount, bool deviceError,
2009 const std::vector<hardware::CameraStreamStats>& streamStats) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002010 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002011 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002012
2013 if (remoteCb != 0) {
2014 remoteCb->onDeviceIdle();
2015 }
Shuzhen Wangd26b1862022-03-07 12:05:05 -08002016 Camera2ClientBase::notifyIdleWithUserTag(requestCount, resultErrorCount, deviceError,
Shuzhen Wang9372b0b2022-05-11 18:55:19 -07002017 streamStats, mUserTag, mVideoStabilizationMode);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002018}
2019
Jianing Weicb0652e2014-03-12 18:29:36 -07002020void CameraDeviceClient::notifyShutter(const CaptureResultExtras& resultExtras,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002021 nsecs_t timestamp) {
2022 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002023 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002024 if (remoteCb != 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -07002025 remoteCb->onCaptureStarted(resultExtras, timestamp);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002026 }
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07002027 Camera2ClientBase::notifyShutter(resultExtras, timestamp);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08002028
Emilian Peev2f5d6012022-01-19 16:16:50 -08002029 // Access to the composite stream map must be synchronized
2030 Mutex::Autolock l(mCompositeLock);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08002031 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
2032 mCompositeStreamMap.valueAt(i)->onShutter(resultExtras, timestamp);
2033 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002034}
2035
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002036void CameraDeviceClient::notifyPrepared(int streamId) {
2037 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002038 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002039 if (remoteCb != 0) {
2040 remoteCb->onPrepared(streamId);
2041 }
2042}
2043
Shuzhen Wang9d066012016-09-30 11:30:20 -07002044void CameraDeviceClient::notifyRequestQueueEmpty() {
2045 // Thread safe. Don't bother locking.
2046 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
2047 if (remoteCb != 0) {
2048 remoteCb->onRequestQueueEmpty();
2049 }
2050}
2051
Igor Murashkine7ee7632013-06-11 18:10:18 -07002052void CameraDeviceClient::detachDevice() {
2053 if (mDevice == 0) return;
2054
Shuzhen Wang316781a2020-08-18 18:11:01 -07002055 nsecs_t startTime = systemTime();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002056 ALOGV("Camera %s: Stopping processors", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -07002057
Emilian Peev7ee731f2022-02-08 14:55:52 -08002058 if (mFrameProcessor.get() != nullptr) {
2059 mFrameProcessor->removeListener(
2060 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MIN_ID,
2061 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MAX_ID, /*listener*/this);
2062 mFrameProcessor->requestExit();
2063 ALOGV("Camera %s: Waiting for threads", mCameraIdStr.string());
2064 mFrameProcessor->join();
2065 ALOGV("Camera %s: Disconnecting device", mCameraIdStr.string());
2066 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07002067
2068 // WORKAROUND: HAL refuses to disconnect while there's streams in flight
2069 {
Emilian Peev6b51d7d2018-07-23 11:41:44 +01002070 int64_t lastFrameNumber;
Igor Murashkine7ee7632013-06-11 18:10:18 -07002071 status_t code;
Emilian Peev6b51d7d2018-07-23 11:41:44 +01002072 if ((code = mDevice->flush(&lastFrameNumber)) != OK) {
2073 ALOGE("%s: flush failed with code 0x%x", __FUNCTION__, code);
2074 }
2075
Igor Murashkine7ee7632013-06-11 18:10:18 -07002076 if ((code = mDevice->waitUntilDrained()) != OK) {
2077 ALOGE("%s: waitUntilDrained failed with code 0x%x", __FUNCTION__,
2078 code);
2079 }
2080 }
2081
Emilian Peev2f5d6012022-01-19 16:16:50 -08002082 {
2083 Mutex::Autolock l(mCompositeLock);
2084 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
2085 auto ret = mCompositeStreamMap.valueAt(i)->deleteInternalStreams();
2086 if (ret != OK) {
2087 ALOGE("%s: Failed removing composite stream %s (%d)", __FUNCTION__,
2088 strerror(-ret), ret);
2089 }
Emilian Peev5e4c7322019-10-22 14:20:52 -07002090 }
Emilian Peev2f5d6012022-01-19 16:16:50 -08002091 mCompositeStreamMap.clear();
Emilian Peev5e4c7322019-10-22 14:20:52 -07002092 }
Emilian Peev5e4c7322019-10-22 14:20:52 -07002093
Igor Murashkine7ee7632013-06-11 18:10:18 -07002094 Camera2ClientBase::detachDevice();
Shuzhen Wang316781a2020-08-18 18:11:01 -07002095
2096 int32_t closeLatencyMs = ns2ms(systemTime() - startTime);
Austin Borger74fca042022-05-23 12:41:21 -07002097 mCameraServiceProxyWrapper->logClose(mCameraIdStr, closeLatencyMs);
Igor Murashkine7ee7632013-06-11 18:10:18 -07002098}
2099
2100/** Device-related methods */
Jianing Weicb0652e2014-03-12 18:29:36 -07002101void CameraDeviceClient::onResultAvailable(const CaptureResult& result) {
Igor Murashkine7ee7632013-06-11 18:10:18 -07002102 ATRACE_CALL();
2103 ALOGV("%s", __FUNCTION__);
2104
Igor Murashkin4fb55c12013-08-29 17:43:01 -07002105 // Thread-safe. No lock necessary.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002106 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = mRemoteCallback;
Igor Murashkin4fb55c12013-08-29 17:43:01 -07002107 if (remoteCb != NULL) {
Shuzhen Wang5c22c152017-12-31 17:12:25 -08002108 remoteCb->onResultReceived(result.mMetadata, result.mResultExtras,
2109 result.mPhysicalMetadatas);
Igor Murashkine7ee7632013-06-11 18:10:18 -07002110 }
Emilian Peev538c90e2018-12-17 18:03:19 +00002111
Emilian Peev2f5d6012022-01-19 16:16:50 -08002112 // Access to the composite stream map must be synchronized
2113 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +00002114 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
2115 mCompositeStreamMap.valueAt(i)->onResultAvailable(result);
2116 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07002117}
2118
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002119binder::Status CameraDeviceClient::checkPidStatus(const char* checkLocation) {
Eino-Ville Talvala6192b892016-04-04 12:31:18 -07002120 if (mDisconnected) {
2121 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED,
2122 "The camera device has been disconnected");
2123 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002124 status_t res = checkPid(checkLocation);
2125 return (res == OK) ? binder::Status::ok() :
2126 STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
2127 "Attempt to use camera from a different process than original client");
2128}
2129
Igor Murashkine7ee7632013-06-11 18:10:18 -07002130// TODO: move to Camera2ClientBase
2131bool CameraDeviceClient::enforceRequestPermissions(CameraMetadata& metadata) {
2132
Jayant Chowdhary12361932018-08-27 14:46:13 -07002133 const int pid = CameraThreadState::getCallingPid();
Igor Murashkine7ee7632013-06-11 18:10:18 -07002134 const int selfPid = getpid();
2135 camera_metadata_entry_t entry;
2136
2137 /**
2138 * Mixin default important security values
2139 * - android.led.transmit = defaulted ON
2140 */
2141 CameraMetadata staticInfo = mDevice->info();
2142 entry = staticInfo.find(ANDROID_LED_AVAILABLE_LEDS);
2143 for(size_t i = 0; i < entry.count; ++i) {
2144 uint8_t led = entry.data.u8[i];
2145
2146 switch(led) {
2147 case ANDROID_LED_AVAILABLE_LEDS_TRANSMIT: {
2148 uint8_t transmitDefault = ANDROID_LED_TRANSMIT_ON;
2149 if (!metadata.exists(ANDROID_LED_TRANSMIT)) {
2150 metadata.update(ANDROID_LED_TRANSMIT,
2151 &transmitDefault, 1);
2152 }
2153 break;
2154 }
2155 }
2156 }
2157
2158 // We can do anything!
2159 if (pid == selfPid) {
2160 return true;
2161 }
2162
2163 /**
2164 * Permission check special fields in the request
2165 * - android.led.transmit = android.permission.CAMERA_DISABLE_TRANSMIT
2166 */
2167 entry = metadata.find(ANDROID_LED_TRANSMIT);
2168 if (entry.count > 0 && entry.data.u8[0] != ANDROID_LED_TRANSMIT_ON) {
2169 String16 permissionString =
2170 String16("android.permission.CAMERA_DISABLE_TRANSMIT_LED");
2171 if (!checkCallingPermission(permissionString)) {
Jayant Chowdhary12361932018-08-27 14:46:13 -07002172 const int uid = CameraThreadState::getCallingUid();
Igor Murashkine7ee7632013-06-11 18:10:18 -07002173 ALOGE("Permission Denial: "
2174 "can't disable transmit LED pid=%d, uid=%d", pid, uid);
2175 return false;
2176 }
2177 }
2178
2179 return true;
2180}
2181
Shuzhen Wang610d7b82022-02-08 14:37:22 -08002182status_t CameraDeviceClient::getRotationTransformLocked(int mirrorMode,
2183 int32_t* transform) {
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07002184 ALOGV("%s: begin", __FUNCTION__);
2185
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07002186 const CameraMetadata& staticInfo = mDevice->info();
Shuzhen Wang610d7b82022-02-08 14:37:22 -08002187 return CameraUtils::getRotationTransform(staticInfo, mirrorMode, transform);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07002188}
2189
Emilian Peevf4816702020-04-03 15:44:51 -07002190binder::Status CameraDeviceClient::mapRequestTemplate(int templateId,
2191 camera_request_template_t* tempId /*out*/) {
2192 binder::Status ret = binder::Status::ok();
2193
2194 if (tempId == nullptr) {
2195 ret = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
2196 "Camera %s: Invalid template argument", mCameraIdStr.string());
2197 return ret;
2198 }
2199 switch(templateId) {
2200 case ICameraDeviceUser::TEMPLATE_PREVIEW:
2201 *tempId = camera_request_template_t::CAMERA_TEMPLATE_PREVIEW;
2202 break;
2203 case ICameraDeviceUser::TEMPLATE_RECORD:
2204 *tempId = camera_request_template_t::CAMERA_TEMPLATE_VIDEO_RECORD;
2205 break;
2206 case ICameraDeviceUser::TEMPLATE_STILL_CAPTURE:
2207 *tempId = camera_request_template_t::CAMERA_TEMPLATE_STILL_CAPTURE;
2208 break;
2209 case ICameraDeviceUser::TEMPLATE_VIDEO_SNAPSHOT:
2210 *tempId = camera_request_template_t::CAMERA_TEMPLATE_VIDEO_SNAPSHOT;
2211 break;
2212 case ICameraDeviceUser::TEMPLATE_ZERO_SHUTTER_LAG:
2213 *tempId = camera_request_template_t::CAMERA_TEMPLATE_ZERO_SHUTTER_LAG;
2214 break;
2215 case ICameraDeviceUser::TEMPLATE_MANUAL:
2216 *tempId = camera_request_template_t::CAMERA_TEMPLATE_MANUAL;
2217 break;
2218 default:
2219 ret = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
2220 "Camera %s: Template ID %d is invalid or not supported",
2221 mCameraIdStr.string(), templateId);
2222 return ret;
2223 }
2224
2225 return ret;
2226}
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08002227
2228const CameraMetadata &CameraDeviceClient::getStaticInfo(const String8 &cameraId) {
2229 if (mDevice->getId() == cameraId) {
2230 return mDevice->info();
2231 }
2232 return mDevice->infoPhysical(cameraId);
2233}
2234
2235bool CameraDeviceClient::isUltraHighResolutionSensor(const String8 &cameraId) {
2236 const CameraMetadata &deviceInfo = getStaticInfo(cameraId);
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07002237 return SessionConfigurationUtils::isUltraHighResolutionSensor(deviceInfo);
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08002238}
2239
2240bool CameraDeviceClient::isSensorPixelModeConsistent(
2241 const std::list<int> &streamIdList, const CameraMetadata &settings) {
2242 // First we get the sensorPixelMode from the settings metadata.
2243 int32_t sensorPixelMode = ANDROID_SENSOR_PIXEL_MODE_DEFAULT;
2244 camera_metadata_ro_entry sensorPixelModeEntry = settings.find(ANDROID_SENSOR_PIXEL_MODE);
2245 if (sensorPixelModeEntry.count != 0) {
2246 sensorPixelMode = sensorPixelModeEntry.data.u8[0];
2247 if (sensorPixelMode != ANDROID_SENSOR_PIXEL_MODE_DEFAULT &&
2248 sensorPixelMode != ANDROID_SENSOR_PIXEL_MODE_MAXIMUM_RESOLUTION) {
2249 ALOGE("%s: Request sensor pixel mode not is not one of the valid values %d",
2250 __FUNCTION__, sensorPixelMode);
2251 return false;
2252 }
2253 }
2254 // Check whether each stream has max resolution allowed.
2255 bool consistent = true;
2256 for (auto it : streamIdList) {
2257 auto const streamInfoIt = mStreamInfoMap.find(it);
2258 if (streamInfoIt == mStreamInfoMap.end()) {
2259 ALOGE("%s: stream id %d not created, skipping", __FUNCTION__, it);
2260 return false;
2261 }
2262 consistent =
2263 streamInfoIt->second.sensorPixelModesUsed.find(sensorPixelMode) !=
2264 streamInfoIt->second.sensorPixelModesUsed.end();
2265 if (!consistent) {
2266 ALOGE("sensorPixelMode used %i not consistent with configured modes", sensorPixelMode);
2267 for (auto m : streamInfoIt->second.sensorPixelModesUsed) {
2268 ALOGE("sensor pixel mode used list: %i", m);
2269 }
2270 break;
2271 }
2272 }
2273
2274 return consistent;
2275}
2276
Igor Murashkine7ee7632013-06-11 18:10:18 -07002277} // namespace android