blob: c595a44a121cf46aa935e6d730a8004027ac3cea [file] [log] [blame]
Igor Murashkine7ee7632013-06-11 18:10:18 -07001/*
Shuzhen Wangc28189a2017-11-27 23:05:10 -08002 * Copyright (C) 2013-2018 The Android Open Source Project
Igor Murashkine7ee7632013-06-11 18:10:18 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "CameraDeviceClient"
18#define ATRACE_TAG ATRACE_TAG_CAMERA
Jianing Weicb0652e2014-03-12 18:29:36 -070019//#define LOG_NDEBUG 0
Igor Murashkine7ee7632013-06-11 18:10:18 -070020
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070021#include <cutils/properties.h>
Jayant Chowdhary12361932018-08-27 14:46:13 -070022#include <utils/CameraThreadState.h>
Igor Murashkine7ee7632013-06-11 18:10:18 -070023#include <utils/Log.h>
Colin Crossb8a9dbb2020-08-27 04:12:26 +000024#include <utils/SessionConfigurationUtils.h>
Igor Murashkine7ee7632013-06-11 18:10:18 -070025#include <utils/Trace.h>
Igor Murashkine7ee7632013-06-11 18:10:18 -070026#include <gui/Surface.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070027#include <camera/camera2/CaptureRequest.h>
Ruben Brunk5698d442014-06-18 10:39:40 -070028#include <camera/CameraUtils.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070029
30#include "common/CameraDeviceBase.h"
Emilian Peev35ae8262018-11-08 13:11:32 +000031#include "device3/Camera3Device.h"
32#include "device3/Camera3OutputStream.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070033#include "api2/CameraDeviceClient.h"
Shuzhen Wang316781a2020-08-18 18:11:01 -070034#include "utils/CameraServiceProxyWrapper.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070035
Emilian Peev00420d22018-02-05 21:33:13 +000036#include <camera_metadata_hidden.h>
37
Emilian Peev538c90e2018-12-17 18:03:19 +000038#include "DepthCompositeStream.h"
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -080039#include "HeicCompositeStream.h"
Emilian Peev538c90e2018-12-17 18:03:19 +000040
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080041// Convenience methods for constructing binder::Status objects for error returns
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070042
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080043#define STATUS_ERROR(errorCode, errorString) \
44 binder::Status::fromServiceSpecificError(errorCode, \
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -080045 String8::format("%s:%d: %s", __FUNCTION__, __LINE__, errorString))
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080046
47#define STATUS_ERROR_FMT(errorCode, errorString, ...) \
48 binder::Status::fromServiceSpecificError(errorCode, \
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -080049 String8::format("%s:%d: " errorString, __FUNCTION__, __LINE__, \
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080050 __VA_ARGS__))
Igor Murashkine7ee7632013-06-11 18:10:18 -070051
52namespace android {
53using namespace camera2;
Austin Borgerea931242021-12-13 23:10:41 +000054using namespace camera3;
Emilian Peevf4816702020-04-03 15:44:51 -070055using camera3::camera_stream_rotation_t::CAMERA_STREAM_ROTATION_0;
Igor Murashkine7ee7632013-06-11 18:10:18 -070056
57CameraDeviceClientBase::CameraDeviceClientBase(
58 const sp<CameraService>& cameraService,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080059 const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback,
Igor Murashkine7ee7632013-06-11 18:10:18 -070060 const String16& clientPackageName,
Jayant Chowdharyeb0169f2022-01-31 00:00:02 -080061 bool systemNativeClient,
Jooyung Han3f9a3b42020-01-23 12:27:18 +090062 const std::optional<String16>& clientFeatureId,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080063 const String8& cameraId,
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -080064 int api1CameraId,
Igor Murashkine7ee7632013-06-11 18:10:18 -070065 int cameraFacing,
Emilian Peev8b64f282021-03-25 16:49:57 -070066 int sensorOrientation,
Igor Murashkine7ee7632013-06-11 18:10:18 -070067 int clientPid,
68 uid_t clientUid,
69 int servicePid) :
Eino-Ville Talvalae992e752014-11-07 16:17:48 -080070 BasicClient(cameraService,
Marco Nelissenf8880202014-11-14 07:58:25 -080071 IInterface::asBinder(remoteCallback),
Eino-Ville Talvalae992e752014-11-07 16:17:48 -080072 clientPackageName,
Jayant Chowdharyeb0169f2022-01-31 00:00:02 -080073 systemNativeClient,
Philip P. Moltmann9e648f62019-11-04 12:52:45 -080074 clientFeatureId,
Eino-Ville Talvalae992e752014-11-07 16:17:48 -080075 cameraId,
76 cameraFacing,
Emilian Peev8b64f282021-03-25 16:49:57 -070077 sensorOrientation,
Eino-Ville Talvalae992e752014-11-07 16:17:48 -080078 clientPid,
79 clientUid,
80 servicePid),
Igor Murashkine7ee7632013-06-11 18:10:18 -070081 mRemoteCallback(remoteCallback) {
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -080082 // We don't need it for API2 clients, but Camera2ClientBase requires it.
83 (void) api1CameraId;
Igor Murashkine7ee7632013-06-11 18:10:18 -070084}
Igor Murashkine7ee7632013-06-11 18:10:18 -070085
86// Interface used by CameraService
87
88CameraDeviceClient::CameraDeviceClient(const sp<CameraService>& cameraService,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080089 const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback,
90 const String16& clientPackageName,
Jayant Chowdharyeb0169f2022-01-31 00:00:02 -080091 bool systemNativeClient,
Jooyung Han3f9a3b42020-01-23 12:27:18 +090092 const std::optional<String16>& clientFeatureId,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080093 const String8& cameraId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080094 int cameraFacing,
Emilian Peev8b64f282021-03-25 16:49:57 -070095 int sensorOrientation,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080096 int clientPid,
97 uid_t clientUid,
Shuzhen Wangd4abdf72021-05-28 11:22:50 -070098 int servicePid,
99 bool overrideForPerfClass) :
Jayant Chowdharyeb0169f2022-01-31 00:00:02 -0800100 Camera2ClientBase(cameraService, remoteCallback, clientPackageName, systemNativeClient,
101 clientFeatureId, cameraId, /*API1 camera ID*/ -1, cameraFacing, sensorOrientation,
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700102 clientPid, clientUid, servicePid, overrideForPerfClass),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700103 mInputStream(),
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700104 mStreamingRequestId(REQUEST_ID_NONE),
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700105 mRequestIdCounter(0),
106 mOverrideForPerfClass(overrideForPerfClass) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700107
108 ATRACE_CALL();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800109 ALOGI("CameraDeviceClient %s: Opened", cameraId.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700110}
111
Emilian Peevbd8c5032018-02-14 23:05:40 +0000112status_t CameraDeviceClient::initialize(sp<CameraProviderManager> manager,
113 const String8& monitorTags) {
114 return initializeImpl(manager, monitorTags);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800115}
116
117template<typename TProviderPtr>
Emilian Peevbd8c5032018-02-14 23:05:40 +0000118status_t CameraDeviceClient::initializeImpl(TProviderPtr providerPtr, const String8& monitorTags) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700119 ATRACE_CALL();
120 status_t res;
121
Emilian Peevbd8c5032018-02-14 23:05:40 +0000122 res = Camera2ClientBase::initialize(providerPtr, monitorTags);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700123 if (res != OK) {
124 return res;
125 }
126
127 String8 threadName;
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -0700128 mFrameProcessor = new FrameProcessorBase(mDevice);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800129 threadName = String8::format("CDU-%s-FrameProc", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700130 mFrameProcessor->run(threadName.string());
131
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800132 mFrameProcessor->registerListener(camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MIN_ID,
133 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MAX_ID,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -0800134 /*listener*/this,
Zhijun He25a0aef2014-06-25 11:40:02 -0700135 /*sendPartials*/true);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700136
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800137 const CameraMetadata &deviceInfo = mDevice->info();
138 camera_metadata_ro_entry_t physicalKeysEntry = deviceInfo.find(
Emilian Peev00420d22018-02-05 21:33:13 +0000139 ANDROID_REQUEST_AVAILABLE_PHYSICAL_CAMERA_REQUEST_KEYS);
140 if (physicalKeysEntry.count > 0) {
141 mSupportedPhysicalRequestKeys.insert(mSupportedPhysicalRequestKeys.begin(),
142 physicalKeysEntry.data.i32,
143 physicalKeysEntry.data.i32 + physicalKeysEntry.count);
144 }
145
Emilian Peev2295df72021-11-12 18:14:10 -0800146 auto entry = deviceInfo.find(ANDROID_REQUEST_AVAILABLE_CAPABILITIES);
147 mDynamicProfileMap.emplace(
148 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD,
149 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD);
150 if (entry.count > 0) {
Shuzhen Wangc8ab4522021-12-14 20:12:42 -0800151 const auto it = std::find(entry.data.u8, entry.data.u8 + entry.count,
Emilian Peev2295df72021-11-12 18:14:10 -0800152 ANDROID_REQUEST_AVAILABLE_CAPABILITIES_DYNAMIC_RANGE_TEN_BIT);
Shuzhen Wangc8ab4522021-12-14 20:12:42 -0800153 if (it != entry.data.u8 + entry.count) {
Emilian Peev2295df72021-11-12 18:14:10 -0800154 entry = deviceInfo.find(ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP);
Emilian Peevc81a7592022-02-14 17:38:18 -0800155 if (entry.count > 0 || ((entry.count % 3) != 0)) {
156 int64_t standardBitmap =
157 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD;
158 for (size_t i = 0; i < entry.count; i += 3) {
159 if (entry.data.i64[i] !=
Emilian Peev2295df72021-11-12 18:14:10 -0800160 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD) {
Emilian Peevc81a7592022-02-14 17:38:18 -0800161 mDynamicProfileMap.emplace(entry.data.i64[i], entry.data.i64[i+1]);
162 if ((entry.data.i64[i+1] == 0) || (entry.data.i64[i+1] &
Emilian Peev2295df72021-11-12 18:14:10 -0800163 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD)) {
Emilian Peevc81a7592022-02-14 17:38:18 -0800164 standardBitmap |= entry.data.i64[i];
Emilian Peev2295df72021-11-12 18:14:10 -0800165 }
166 } else {
Emilian Peevc81a7592022-02-14 17:38:18 -0800167 ALOGE("%s: Device %s includes unexpected profile entry: 0x%" PRIx64 "!",
168 __FUNCTION__, mCameraIdStr.c_str(), entry.data.i64[i]);
Emilian Peev2295df72021-11-12 18:14:10 -0800169 }
170 }
Emilian Peevef715e22022-05-19 17:44:33 -0700171 mDynamicProfileMap[ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD] =
172 standardBitmap;
Emilian Peev2295df72021-11-12 18:14:10 -0800173 } else {
174 ALOGE("%s: Device %s supports 10-bit output but doesn't include a dynamic range"
175 " profile map!", __FUNCTION__, mCameraIdStr.c_str());
176 }
177 }
178 }
179
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700180 mProviderManager = providerPtr;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800181 // Cache physical camera ids corresponding to this device and also the high
182 // resolution sensors in this device + physical camera ids
183 mProviderManager->isLogicalCamera(mCameraIdStr.string(), &mPhysicalCameraIds);
184 if (isUltraHighResolutionSensor(mCameraIdStr)) {
185 mHighResolutionSensors.insert(mCameraIdStr.string());
186 }
187 for (auto &physicalId : mPhysicalCameraIds) {
188 if (isUltraHighResolutionSensor(String8(physicalId.c_str()))) {
189 mHighResolutionSensors.insert(physicalId.c_str());
190 }
191 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700192 return OK;
193}
194
195CameraDeviceClient::~CameraDeviceClient() {
196}
197
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800198binder::Status CameraDeviceClient::submitRequest(
199 const hardware::camera2::CaptureRequest& request,
200 bool streaming,
201 /*out*/
202 hardware::camera2::utils::SubmitInfo *submitInfo) {
203 std::vector<hardware::camera2::CaptureRequest> requestList = { request };
204 return submitRequestList(requestList, streaming, submitInfo);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700205}
206
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800207binder::Status CameraDeviceClient::insertGbpLocked(const sp<IGraphicBufferProducer>& gbp,
Emilian Peevf873aa52018-01-26 14:58:28 +0000208 SurfaceMap* outSurfaceMap, Vector<int32_t>* outputStreamIds, int32_t *currentStreamId) {
Emilian Peev538c90e2018-12-17 18:03:19 +0000209 int compositeIdx;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800210 int idx = mStreamMap.indexOfKey(IInterface::asBinder(gbp));
211
Emilian Peev2f5d6012022-01-19 16:16:50 -0800212 Mutex::Autolock l(mCompositeLock);
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800213 // Trying to submit request with surface that wasn't created
214 if (idx == NAME_NOT_FOUND) {
215 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
216 " we have not called createStream on",
217 __FUNCTION__, mCameraIdStr.string());
218 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
219 "Request targets Surface that is not part of current capture session");
Emilian Peev538c90e2018-12-17 18:03:19 +0000220 } else if ((compositeIdx = mCompositeStreamMap.indexOfKey(IInterface::asBinder(gbp)))
221 != NAME_NOT_FOUND) {
222 mCompositeStreamMap.valueAt(compositeIdx)->insertGbp(outSurfaceMap, outputStreamIds,
223 currentStreamId);
224 return binder::Status::ok();
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800225 }
226
227 const StreamSurfaceId& streamSurfaceId = mStreamMap.valueAt(idx);
228 if (outSurfaceMap->find(streamSurfaceId.streamId()) == outSurfaceMap->end()) {
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800229 outputStreamIds->push_back(streamSurfaceId.streamId());
230 }
231 (*outSurfaceMap)[streamSurfaceId.streamId()].push_back(streamSurfaceId.surfaceId());
232
233 ALOGV("%s: Camera %s: Appending output stream %d surface %d to request",
234 __FUNCTION__, mCameraIdStr.string(), streamSurfaceId.streamId(),
235 streamSurfaceId.surfaceId());
236
Emilian Peevf873aa52018-01-26 14:58:28 +0000237 if (currentStreamId != nullptr) {
238 *currentStreamId = streamSurfaceId.streamId();
239 }
240
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800241 return binder::Status::ok();
242}
243
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800244static std::list<int> getIntersection(const std::unordered_set<int> &streamIdsForThisCamera,
245 const Vector<int> &streamIdsForThisRequest) {
246 std::list<int> intersection;
247 for (auto &streamId : streamIdsForThisRequest) {
248 if (streamIdsForThisCamera.find(streamId) != streamIdsForThisCamera.end()) {
249 intersection.emplace_back(streamId);
250 }
251 }
252 return intersection;
253}
254
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800255binder::Status CameraDeviceClient::submitRequestList(
256 const std::vector<hardware::camera2::CaptureRequest>& requests,
257 bool streaming,
258 /*out*/
259 hardware::camera2::utils::SubmitInfo *submitInfo) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700260 ATRACE_CALL();
Mark Salyzyn50468412014-06-18 16:33:43 -0700261 ALOGV("%s-start of function. Request list size %zu", __FUNCTION__, requests.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700262
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800263 binder::Status res = binder::Status::ok();
264 status_t err;
265 if ( !(res = checkPidStatus(__FUNCTION__) ).isOk()) {
266 return res;
267 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700268
269 Mutex::Autolock icl(mBinderSerializationLock);
270
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800271 if (!mDevice.get()) {
272 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
273 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700274
275 if (requests.empty()) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800276 ALOGE("%s: Camera %s: Sent null request. Rejecting request.",
277 __FUNCTION__, mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800278 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Empty request list");
Jianing Wei90e59c92014-03-12 18:29:36 -0700279 }
280
Emilian Peevaebbe412018-01-15 13:53:24 +0000281 List<const CameraDeviceBase::PhysicalCameraSettingsList> metadataRequestList;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700282 std::list<const SurfaceMap> surfaceMapList;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800283 submitInfo->mRequestId = mRequestIdCounter;
Jianing Wei90e59c92014-03-12 18:29:36 -0700284 uint32_t loopCounter = 0;
285
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800286 for (auto&& request: requests) {
287 if (request.mIsReprocess) {
Chien-Yu Chened0412e2015-04-27 15:04:22 -0700288 if (!mInputStream.configured) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800289 ALOGE("%s: Camera %s: no input stream is configured.", __FUNCTION__,
290 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800291 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800292 "No input configured for camera %s but request is for reprocessing",
293 mCameraIdStr.string());
Chien-Yu Chened0412e2015-04-27 15:04:22 -0700294 } else if (streaming) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800295 ALOGE("%s: Camera %s: streaming reprocess requests not supported.", __FUNCTION__,
296 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800297 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
298 "Repeating reprocess requests not supported");
Emilian Peevaebbe412018-01-15 13:53:24 +0000299 } else if (request.mPhysicalCameraSettings.size() > 1) {
300 ALOGE("%s: Camera %s: reprocess requests not supported for "
301 "multiple physical cameras.", __FUNCTION__,
302 mCameraIdStr.string());
303 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
304 "Reprocess requests not supported for multiple cameras");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700305 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700306 }
307
Emilian Peevaebbe412018-01-15 13:53:24 +0000308 if (request.mPhysicalCameraSettings.empty()) {
309 ALOGE("%s: Camera %s: request doesn't contain any settings.", __FUNCTION__,
310 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800311 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
Emilian Peevaebbe412018-01-15 13:53:24 +0000312 "Request doesn't contain any settings");
313 }
314
315 //The first capture settings should always match the logical camera id
316 String8 logicalId(request.mPhysicalCameraSettings.begin()->id.c_str());
317 if (mDevice->getId() != logicalId) {
318 ALOGE("%s: Camera %s: Invalid camera request settings.", __FUNCTION__,
319 mCameraIdStr.string());
320 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
321 "Invalid camera request settings");
322 }
323
Emilian Peevaebbe412018-01-15 13:53:24 +0000324 if (request.mSurfaceList.isEmpty() && request.mStreamIdxList.size() == 0) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800325 ALOGE("%s: Camera %s: Requests must have at least one surface target. "
326 "Rejecting request.", __FUNCTION__, mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800327 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
328 "Request has no output targets");
Jianing Wei90e59c92014-03-12 18:29:36 -0700329 }
330
Jianing Wei90e59c92014-03-12 18:29:36 -0700331 /**
Shuzhen Wang0129d522016-10-30 22:43:41 -0700332 * Write in the output stream IDs and map from stream ID to surface ID
333 * which we calculate from the capture request's list of surface target
Jianing Wei90e59c92014-03-12 18:29:36 -0700334 */
Shuzhen Wang0129d522016-10-30 22:43:41 -0700335 SurfaceMap surfaceMap;
Jianing Wei90e59c92014-03-12 18:29:36 -0700336 Vector<int32_t> outputStreamIds;
Emilian Peevf873aa52018-01-26 14:58:28 +0000337 std::vector<std::string> requestedPhysicalIds;
Emilian Peevc81a7592022-02-14 17:38:18 -0800338 int64_t dynamicProfileBitmap = 0;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800339 if (request.mSurfaceList.size() > 0) {
Chih-Hung Hsieh3ef324d2018-12-11 11:48:12 -0800340 for (const sp<Surface>& surface : request.mSurfaceList) {
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800341 if (surface == 0) continue;
Jianing Wei90e59c92014-03-12 18:29:36 -0700342
Emilian Peevf873aa52018-01-26 14:58:28 +0000343 int32_t streamId;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800344 sp<IGraphicBufferProducer> gbp = surface->getIGraphicBufferProducer();
Emilian Peevf873aa52018-01-26 14:58:28 +0000345 res = insertGbpLocked(gbp, &surfaceMap, &outputStreamIds, &streamId);
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800346 if (!res.isOk()) {
347 return res;
348 }
Emilian Peevf873aa52018-01-26 14:58:28 +0000349
350 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
351 if (index >= 0) {
352 String8 requestedPhysicalId(
353 mConfiguredOutputs.valueAt(index).getPhysicalCameraId());
354 requestedPhysicalIds.push_back(requestedPhysicalId.string());
Emilian Peev2295df72021-11-12 18:14:10 -0800355 dynamicProfileBitmap |=
356 mConfiguredOutputs.valueAt(index).getDynamicRangeProfile();
Emilian Peevf873aa52018-01-26 14:58:28 +0000357 } else {
358 ALOGW("%s: Output stream Id not found among configured outputs!", __FUNCTION__);
359 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700360 }
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800361 } else {
362 for (size_t i = 0; i < request.mStreamIdxList.size(); i++) {
363 int streamId = request.mStreamIdxList.itemAt(i);
364 int surfaceIdx = request.mSurfaceIdxList.itemAt(i);
Jianing Wei90e59c92014-03-12 18:29:36 -0700365
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800366 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
367 if (index < 0) {
368 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
369 " we have not called createStream on: stream %d",
370 __FUNCTION__, mCameraIdStr.string(), streamId);
371 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
372 "Request targets Surface that is not part of current capture session");
373 }
374
375 const auto& gbps = mConfiguredOutputs.valueAt(index).getGraphicBufferProducers();
376 if ((size_t)surfaceIdx >= gbps.size()) {
377 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
378 " we have not called createStream on: stream %d, surfaceIdx %d",
379 __FUNCTION__, mCameraIdStr.string(), streamId, surfaceIdx);
380 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
381 "Request targets Surface has invalid surface index");
382 }
383
Emilian Peevf873aa52018-01-26 14:58:28 +0000384 res = insertGbpLocked(gbps[surfaceIdx], &surfaceMap, &outputStreamIds, nullptr);
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800385 if (!res.isOk()) {
386 return res;
387 }
Emilian Peevf873aa52018-01-26 14:58:28 +0000388
389 String8 requestedPhysicalId(
390 mConfiguredOutputs.valueAt(index).getPhysicalCameraId());
391 requestedPhysicalIds.push_back(requestedPhysicalId.string());
Emilian Peev2295df72021-11-12 18:14:10 -0800392 dynamicProfileBitmap |=
393 mConfiguredOutputs.valueAt(index).getDynamicRangeProfile();
394 }
395 }
396
397 if (dynamicProfileBitmap !=
398 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD) {
399 for (int i = ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD;
400 i < ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_MAX; i <<= 1) {
401 if ((dynamicProfileBitmap & i) == 0) {
402 continue;
403 }
404
405 const auto& it = mDynamicProfileMap.find(i);
406 if (it != mDynamicProfileMap.end()) {
407 if ((it->second == 0) ||
408 ((it->second & dynamicProfileBitmap) == dynamicProfileBitmap)) {
409 continue;
410 } else {
411 ALOGE("%s: Camera %s: Tried to submit a request with a surfaces that"
412 " reference an unsupported dynamic range profile combination"
Emilian Peevc81a7592022-02-14 17:38:18 -0800413 " 0x%" PRIx64 "!", __FUNCTION__, mCameraIdStr.string(),
Emilian Peev2295df72021-11-12 18:14:10 -0800414 dynamicProfileBitmap);
415 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
416 "Request targets an unsupported dynamic range profile"
417 " combination");
418 }
419 } else {
420 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
421 " references unsupported dynamic range profile 0x%x!",
422 __FUNCTION__, mCameraIdStr.string(), i);
423 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
424 "Request targets 10-bit Surface with unsupported dynamic range"
425 " profile");
426 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700427 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700428 }
429
Emilian Peevf873aa52018-01-26 14:58:28 +0000430 CameraDeviceBase::PhysicalCameraSettingsList physicalSettingsList;
431 for (const auto& it : request.mPhysicalCameraSettings) {
Emilian Peev00420d22018-02-05 21:33:13 +0000432 if (it.settings.isEmpty()) {
433 ALOGE("%s: Camera %s: Sent empty metadata packet. Rejecting request.",
434 __FUNCTION__, mCameraIdStr.string());
435 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
436 "Request settings are empty");
437 }
438
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800439 // Check whether the physical / logical stream has settings
440 // consistent with the sensor pixel mode(s) it was configured with.
441 // mCameraIdToStreamSet will only have ids that are high resolution
442 const auto streamIdSetIt = mHighResolutionCameraIdToStreamIdSet.find(it.id);
443 if (streamIdSetIt != mHighResolutionCameraIdToStreamIdSet.end()) {
444 std::list<int> streamIdsUsedInRequest = getIntersection(streamIdSetIt->second,
445 outputStreamIds);
446 if (!request.mIsReprocess &&
447 !isSensorPixelModeConsistent(streamIdsUsedInRequest, it.settings)) {
448 ALOGE("%s: Camera %s: Request settings CONTROL_SENSOR_PIXEL_MODE not "
449 "consistent with configured streams. Rejecting request.",
450 __FUNCTION__, it.id.c_str());
451 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
452 "Request settings CONTROL_SENSOR_PIXEL_MODE are not consistent with "
453 "streams configured");
454 }
455 }
456
Emilian Peevf873aa52018-01-26 14:58:28 +0000457 String8 physicalId(it.id.c_str());
Shuzhen Wang911c6a32021-10-27 13:36:03 -0700458 bool hasTestPatternModePhysicalKey = std::find(mSupportedPhysicalRequestKeys.begin(),
459 mSupportedPhysicalRequestKeys.end(), ANDROID_SENSOR_TEST_PATTERN_MODE) !=
460 mSupportedPhysicalRequestKeys.end();
461 bool hasTestPatternDataPhysicalKey = std::find(mSupportedPhysicalRequestKeys.begin(),
462 mSupportedPhysicalRequestKeys.end(), ANDROID_SENSOR_TEST_PATTERN_DATA) !=
463 mSupportedPhysicalRequestKeys.end();
Emilian Peevf873aa52018-01-26 14:58:28 +0000464 if (physicalId != mDevice->getId()) {
465 auto found = std::find(requestedPhysicalIds.begin(), requestedPhysicalIds.end(),
466 it.id);
467 if (found == requestedPhysicalIds.end()) {
468 ALOGE("%s: Camera %s: Physical camera id: %s not part of attached outputs.",
469 __FUNCTION__, mCameraIdStr.string(), physicalId.string());
470 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
471 "Invalid physical camera id");
472 }
Emilian Peev00420d22018-02-05 21:33:13 +0000473
474 if (!mSupportedPhysicalRequestKeys.empty()) {
475 // Filter out any unsupported physical request keys.
476 CameraMetadata filteredParams(mSupportedPhysicalRequestKeys.size());
477 camera_metadata_t *meta = const_cast<camera_metadata_t *>(
478 filteredParams.getAndLock());
479 set_camera_metadata_vendor_id(meta, mDevice->getVendorTagId());
480 filteredParams.unlock(meta);
481
482 for (const auto& keyIt : mSupportedPhysicalRequestKeys) {
483 camera_metadata_ro_entry entry = it.settings.find(keyIt);
484 if (entry.count > 0) {
485 filteredParams.update(entry);
486 }
487 }
488
Shuzhen Wang911c6a32021-10-27 13:36:03 -0700489 physicalSettingsList.push_back({it.id, filteredParams,
490 hasTestPatternModePhysicalKey, hasTestPatternDataPhysicalKey});
Emilian Peev00420d22018-02-05 21:33:13 +0000491 }
492 } else {
493 physicalSettingsList.push_back({it.id, it.settings});
Emilian Peevf873aa52018-01-26 14:58:28 +0000494 }
Emilian Peevf873aa52018-01-26 14:58:28 +0000495 }
496
497 if (!enforceRequestPermissions(physicalSettingsList.begin()->metadata)) {
498 // Callee logs
499 return STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
500 "Caller does not have permission to change restricted controls");
501 }
502
Emilian Peevaebbe412018-01-15 13:53:24 +0000503 physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_OUTPUT_STREAMS,
504 &outputStreamIds[0], outputStreamIds.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700505
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800506 if (request.mIsReprocess) {
Emilian Peevaebbe412018-01-15 13:53:24 +0000507 physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_INPUT_STREAMS,
508 &mInputStream.id, 1);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700509 }
510
Emilian Peevaebbe412018-01-15 13:53:24 +0000511 physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_ID,
512 &(submitInfo->mRequestId), /*size*/1);
Jianing Wei90e59c92014-03-12 18:29:36 -0700513 loopCounter++; // loopCounter starts from 1
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800514 ALOGV("%s: Camera %s: Creating request with ID %d (%d of %zu)",
515 __FUNCTION__, mCameraIdStr.string(), submitInfo->mRequestId,
516 loopCounter, requests.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700517
Emilian Peevaebbe412018-01-15 13:53:24 +0000518 metadataRequestList.push_back(physicalSettingsList);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700519 surfaceMapList.push_back(surfaceMap);
Shuzhen Wangd26b1862022-03-07 12:05:05 -0800520
521 if (!request.mUserTag.empty()) {
522 mUserTag = request.mUserTag;
523 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700524 }
525 mRequestIdCounter++;
526
527 if (streaming) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700528 err = mDevice->setStreamingRequestList(metadataRequestList, surfaceMapList,
529 &(submitInfo->mLastFrameNumber));
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800530 if (err != OK) {
531 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800532 "Camera %s: Got error %s (%d) after trying to set streaming request",
533 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800534 ALOGE("%s: %s", __FUNCTION__, msg.string());
535 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
536 msg.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700537 } else {
Shuzhen Wangc9ca6782016-04-26 13:40:31 -0700538 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700539 mStreamingRequestId = submitInfo->mRequestId;
Jianing Wei90e59c92014-03-12 18:29:36 -0700540 }
541 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700542 err = mDevice->captureList(metadataRequestList, surfaceMapList,
543 &(submitInfo->mLastFrameNumber));
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800544 if (err != OK) {
545 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800546 "Camera %s: Got error %s (%d) after trying to submit capture request",
547 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800548 ALOGE("%s: %s", __FUNCTION__, msg.string());
549 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
550 msg.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700551 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800552 ALOGV("%s: requestId = %d ", __FUNCTION__, submitInfo->mRequestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700553 }
554
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800555 ALOGV("%s: Camera %s: End of function", __FUNCTION__, mCameraIdStr.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700556 return res;
557}
558
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800559binder::Status CameraDeviceClient::cancelRequest(
560 int requestId,
561 /*out*/
562 int64_t* lastFrameNumber) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700563 ATRACE_CALL();
564 ALOGV("%s, requestId = %d", __FUNCTION__, requestId);
565
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800566 status_t err;
567 binder::Status res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700568
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800569 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700570
571 Mutex::Autolock icl(mBinderSerializationLock);
572
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800573 if (!mDevice.get()) {
574 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
575 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700576
Shuzhen Wangc9ca6782016-04-26 13:40:31 -0700577 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700578 if (mStreamingRequestId != requestId) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800579 String8 msg = String8::format("Camera %s: Canceling request ID %d doesn't match "
580 "current request ID %d", mCameraIdStr.string(), requestId, mStreamingRequestId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800581 ALOGE("%s: %s", __FUNCTION__, msg.string());
582 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700583 }
584
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800585 err = mDevice->clearStreamingRequest(lastFrameNumber);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700586
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800587 if (err == OK) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800588 ALOGV("%s: Camera %s: Successfully cleared streaming request",
589 __FUNCTION__, mCameraIdStr.string());
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700590 mStreamingRequestId = REQUEST_ID_NONE;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800591 } else {
592 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800593 "Camera %s: Error clearing streaming request: %s (%d)",
594 mCameraIdStr.string(), strerror(-err), err);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700595 }
596
597 return res;
598}
599
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800600binder::Status CameraDeviceClient::beginConfigure() {
Ruben Brunkb2119af2014-05-09 19:57:56 -0700601 // TODO: Implement this.
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -0700602 ATRACE_CALL();
Zhijun He1fa89992015-06-01 15:44:31 -0700603 ALOGV("%s: Not implemented yet.", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800604 return binder::Status::ok();
Ruben Brunkb2119af2014-05-09 19:57:56 -0700605}
606
Emilian Peev5fbe0ba2017-10-20 15:45:45 +0100607binder::Status CameraDeviceClient::endConfigure(int operatingMode,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700608 const hardware::camera2::impl::CameraMetadataNative& sessionParams, int64_t startTimeMs,
Emilian Peevcc0b7952020-01-07 13:54:47 -0800609 std::vector<int>* offlineStreamIds /*out*/) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -0700610 ATRACE_CALL();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700611 ALOGV("%s: ending configure (%d input stream, %zu output surfaces)",
612 __FUNCTION__, mInputStream.configured ? 1 : 0,
613 mStreamMap.size());
Igor Murashkine2d167e2014-08-19 16:19:59 -0700614
Zhijun He0effd522016-03-04 10:22:27 -0800615 binder::Status res;
616 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
617
Emilian Peevcc0b7952020-01-07 13:54:47 -0800618 if (offlineStreamIds == nullptr) {
619 String8 msg = String8::format("Invalid offline stream ids");
620 ALOGE("%s: %s", __FUNCTION__, msg.string());
621 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
622 }
623
Zhijun He0effd522016-03-04 10:22:27 -0800624 Mutex::Autolock icl(mBinderSerializationLock);
625
626 if (!mDevice.get()) {
627 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
628 }
629
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700630 res = SessionConfigurationUtils::checkOperatingMode(operatingMode, mDevice->info(),
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000631 mCameraIdStr);
Emilian Peev35ae8262018-11-08 13:11:32 +0000632 if (!res.isOk()) {
633 return res;
634 }
635
636 status_t err = mDevice->configureStreams(sessionParams, operatingMode);
637 if (err == BAD_VALUE) {
638 String8 msg = String8::format("Camera %s: Unsupported set of inputs/outputs provided",
639 mCameraIdStr.string());
640 ALOGE("%s: %s", __FUNCTION__, msg.string());
641 res = STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
642 } else if (err != OK) {
643 String8 msg = String8::format("Camera %s: Error configuring streams: %s (%d)",
644 mCameraIdStr.string(), strerror(-err), err);
645 ALOGE("%s: %s", __FUNCTION__, msg.string());
646 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Emilian Peev538c90e2018-12-17 18:03:19 +0000647 } else {
Emilian Peevcc0b7952020-01-07 13:54:47 -0800648 offlineStreamIds->clear();
649 mDevice->getOfflineStreamIds(offlineStreamIds);
650
Emilian Peev2f5d6012022-01-19 16:16:50 -0800651 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +0000652 for (size_t i = 0; i < mCompositeStreamMap.size(); ++i) {
653 err = mCompositeStreamMap.valueAt(i)->configureStream();
Emilian Peevcc0b7952020-01-07 13:54:47 -0800654 if (err != OK) {
Emilian Peev538c90e2018-12-17 18:03:19 +0000655 String8 msg = String8::format("Camera %s: Error configuring composite "
656 "streams: %s (%d)", mCameraIdStr.string(), strerror(-err), err);
657 ALOGE("%s: %s", __FUNCTION__, msg.string());
658 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
659 break;
660 }
Emilian Peevcc0b7952020-01-07 13:54:47 -0800661
662 // Composite streams can only support offline mode in case all individual internal
663 // streams are also supported.
664 std::vector<int> internalStreams;
665 mCompositeStreamMap.valueAt(i)->insertCompositeStreamIds(&internalStreams);
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800666 offlineStreamIds->erase(
667 std::remove_if(offlineStreamIds->begin(), offlineStreamIds->end(),
Emilian Peevcc0b7952020-01-07 13:54:47 -0800668 [&internalStreams] (int streamId) {
669 auto it = std::find(internalStreams.begin(), internalStreams.end(),
670 streamId);
671 if (it != internalStreams.end()) {
672 internalStreams.erase(it);
673 return true;
674 }
675
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800676 return false;}), offlineStreamIds->end());
Emilian Peevcc0b7952020-01-07 13:54:47 -0800677 if (internalStreams.empty()) {
678 offlineStreamIds->push_back(mCompositeStreamMap.valueAt(i)->getStreamId());
679 }
680 }
681
682 for (const auto& offlineStreamId : *offlineStreamIds) {
683 mStreamInfoMap[offlineStreamId].supportsOffline = true;
Emilian Peev538c90e2018-12-17 18:03:19 +0000684 }
Shuzhen Wang316781a2020-08-18 18:11:01 -0700685
686 nsecs_t configureEnd = systemTime();
687 int32_t configureDurationMs = ns2ms(configureEnd) - startTimeMs;
688 CameraServiceProxyWrapper::logStreamConfigured(mCameraIdStr, operatingMode,
689 false /*internalReconfig*/, configureDurationMs);
Emilian Peev35ae8262018-11-08 13:11:32 +0000690 }
691
692 return res;
693}
694
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800695binder::Status CameraDeviceClient::isSessionConfigurationSupported(
696 const SessionConfiguration& sessionConfiguration, bool *status /*out*/) {
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800697
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800698 ATRACE_CALL();
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800699 binder::Status res;
700 status_t ret = OK;
701 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
702
703 Mutex::Autolock icl(mBinderSerializationLock);
704
705 if (!mDevice.get()) {
706 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
707 }
708
709 auto operatingMode = sessionConfiguration.getOperatingMode();
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700710 res = SessionConfigurationUtils::checkOperatingMode(operatingMode, mDevice->info(),
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000711 mCameraIdStr);
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800712 if (!res.isOk()) {
713 return res;
714 }
715
716 if (status == nullptr) {
717 String8 msg = String8::format( "Camera %s: Invalid status!", mCameraIdStr.string());
718 ALOGE("%s: %s", __FUNCTION__, msg.string());
719 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
720 }
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700721
Emilian Peev35ae8262018-11-08 13:11:32 +0000722 *status = false;
Shuzhen Wanga79a64d2022-04-24 19:56:30 -0700723 camera3::metadataGetter getMetadata = [this](const String8 &id, bool /*overrideForPerfClass*/) {
724 return mDevice->infoPhysical(id);};
Emilian Peev35ae8262018-11-08 13:11:32 +0000725 ret = mProviderManager->isSessionConfigurationSupported(mCameraIdStr.string(),
Shuzhen Wanga79a64d2022-04-24 19:56:30 -0700726 sessionConfiguration, mOverrideForPerfClass, getMetadata, status);
Emilian Peev35ae8262018-11-08 13:11:32 +0000727 switch (ret) {
728 case OK:
729 // Expected, do nothing.
730 break;
731 case INVALID_OPERATION: {
732 String8 msg = String8::format(
733 "Camera %s: Session configuration query not supported!",
734 mCameraIdStr.string());
735 ALOGD("%s: %s", __FUNCTION__, msg.string());
736 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
737 }
738
739 break;
740 default: {
741 String8 msg = String8::format( "Camera %s: Error: %s (%d)", mCameraIdStr.string(),
742 strerror(-ret), ret);
743 ALOGE("%s: %s", __FUNCTION__, msg.string());
744 res = STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
745 msg.string());
746 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800747 }
748
749 return res;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700750}
751
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800752binder::Status CameraDeviceClient::deleteStream(int streamId) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700753 ATRACE_CALL();
754 ALOGV("%s (streamId = 0x%x)", __FUNCTION__, streamId);
755
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800756 binder::Status res;
757 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700758
759 Mutex::Autolock icl(mBinderSerializationLock);
760
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800761 if (!mDevice.get()) {
762 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
763 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700764
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700765 bool isInput = false;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700766 std::vector<sp<IBinder>> surfaces;
Zhijun He5d677d12016-05-29 16:52:39 -0700767 ssize_t dIndex = NAME_NOT_FOUND;
Emilian Peev538c90e2018-12-17 18:03:19 +0000768 ssize_t compositeIndex = NAME_NOT_FOUND;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700769
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700770 if (mInputStream.configured && mInputStream.id == streamId) {
771 isInput = true;
772 } else {
773 // Guard against trying to delete non-created streams
774 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700775 if (streamId == mStreamMap.valueAt(i).streamId()) {
776 surfaces.push_back(mStreamMap.keyAt(i));
777 }
778 }
779
780 // See if this stream is one of the deferred streams.
781 for (size_t i = 0; i < mDeferredStreams.size(); ++i) {
782 if (streamId == mDeferredStreams[i]) {
783 dIndex = i;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700784 break;
785 }
786 }
787
Emilian Peev2f5d6012022-01-19 16:16:50 -0800788 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +0000789 for (size_t i = 0; i < mCompositeStreamMap.size(); ++i) {
790 if (streamId == mCompositeStreamMap.valueAt(i)->getStreamId()) {
791 compositeIndex = i;
792 break;
793 }
794 }
795
Shuzhen Wang0129d522016-10-30 22:43:41 -0700796 if (surfaces.empty() && dIndex == NAME_NOT_FOUND) {
797 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no such"
798 " stream created yet", mCameraIdStr.string(), streamId);
799 ALOGW("%s: %s", __FUNCTION__, msg.string());
800 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700801 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700802 }
803
804 // Also returns BAD_VALUE if stream ID was not valid
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800805 status_t err = mDevice->deleteStream(streamId);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700806
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800807 if (err != OK) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800808 String8 msg = String8::format("Camera %s: Unexpected error %s (%d) when deleting stream %d",
809 mCameraIdStr.string(), strerror(-err), err, streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800810 ALOGE("%s: %s", __FUNCTION__, msg.string());
811 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
812 } else {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700813 if (isInput) {
814 mInputStream.configured = false;
Zhijun He5d677d12016-05-29 16:52:39 -0700815 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700816 for (auto& surface : surfaces) {
817 mStreamMap.removeItem(surface);
818 }
819
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800820 mConfiguredOutputs.removeItem(streamId);
821
Shuzhen Wang0129d522016-10-30 22:43:41 -0700822 if (dIndex != NAME_NOT_FOUND) {
823 mDeferredStreams.removeItemsAt(dIndex);
824 }
Emilian Peev538c90e2018-12-17 18:03:19 +0000825
826 if (compositeIndex != NAME_NOT_FOUND) {
Emilian Peev2f5d6012022-01-19 16:16:50 -0800827 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +0000828 status_t ret;
829 if ((ret = mCompositeStreamMap.valueAt(compositeIndex)->deleteStream())
830 != OK) {
831 String8 msg = String8::format("Camera %s: Unexpected error %s (%d) when "
832 "deleting composite stream %d", mCameraIdStr.string(), strerror(-err), err,
833 streamId);
834 ALOGE("%s: %s", __FUNCTION__, msg.string());
835 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
836 }
837 mCompositeStreamMap.removeItemsAt(compositeIndex);
838 }
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800839 for (auto &mapIt: mHighResolutionCameraIdToStreamIdSet) {
840 auto &streamSet = mapIt.second;
841 if (streamSet.find(streamId) != streamSet.end()) {
842 streamSet.erase(streamId);
843 break;
844 }
845 }
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700846 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700847 }
848
849 return res;
850}
851
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800852binder::Status CameraDeviceClient::createStream(
853 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
854 /*out*/
855 int32_t* newStreamId) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700856 ATRACE_CALL();
Igor Murashkine7ee7632013-06-11 18:10:18 -0700857
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800858 binder::Status res;
859 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700860
861 Mutex::Autolock icl(mBinderSerializationLock);
862
Shuzhen Wang0129d522016-10-30 22:43:41 -0700863 const std::vector<sp<IGraphicBufferProducer>>& bufferProducers =
864 outputConfiguration.getGraphicBufferProducers();
865 size_t numBufferProducers = bufferProducers.size();
Shuzhen Wang758c2152017-01-10 18:26:18 -0800866 bool deferredConsumer = outputConfiguration.isDeferred();
867 bool isShared = outputConfiguration.isShared();
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800868 String8 physicalCameraId = String8(outputConfiguration.getPhysicalCameraId());
Shuzhen Wang758c2152017-01-10 18:26:18 -0800869 bool deferredConsumerOnly = deferredConsumer && numBufferProducers == 0;
Shuzhen Wang83bff122020-11-20 15:51:39 -0800870 bool isMultiResolution = outputConfiguration.isMultiResolution();
Emilian Peevc81a7592022-02-14 17:38:18 -0800871 int64_t dynamicRangeProfile = outputConfiguration.getDynamicRangeProfile();
Shuzhen Wang8ed1e872022-03-08 16:34:33 -0800872 int64_t streamUseCase = outputConfiguration.getStreamUseCase();
Shuzhen Wange4208922022-02-01 16:52:48 -0800873 int timestampBase = outputConfiguration.getTimestampBase();
Shuzhen Wang610d7b82022-02-08 14:37:22 -0800874 int mirrorMode = outputConfiguration.getMirrorMode();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700875
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700876 res = SessionConfigurationUtils::checkSurfaceType(numBufferProducers, deferredConsumer,
Emilian Peev35ae8262018-11-08 13:11:32 +0000877 outputConfiguration.getSurfaceType());
878 if (!res.isOk()) {
879 return res;
Yin-Chia Yeh89f14da2014-06-10 16:05:44 -0700880 }
Zhijun He5d677d12016-05-29 16:52:39 -0700881
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800882 if (!mDevice.get()) {
883 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
884 }
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700885 res = SessionConfigurationUtils::checkPhysicalCameraId(mPhysicalCameraIds,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800886 physicalCameraId, mCameraIdStr);
Emilian Peev35ae8262018-11-08 13:11:32 +0000887 if (!res.isOk()) {
888 return res;
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800889 }
Emilian Peev35ae8262018-11-08 13:11:32 +0000890
Shuzhen Wang0129d522016-10-30 22:43:41 -0700891 std::vector<sp<Surface>> surfaces;
892 std::vector<sp<IBinder>> binders;
Zhijun He5d677d12016-05-29 16:52:39 -0700893 status_t err;
894
895 // Create stream for deferred surface case.
Shuzhen Wang0129d522016-10-30 22:43:41 -0700896 if (deferredConsumerOnly) {
Shuzhen Wang758c2152017-01-10 18:26:18 -0800897 return createDeferredSurfaceStreamLocked(outputConfiguration, isShared, newStreamId);
Zhijun He5d677d12016-05-29 16:52:39 -0700898 }
899
Shuzhen Wang758c2152017-01-10 18:26:18 -0800900 OutputStreamInfo streamInfo;
901 bool isStreamInfoValid = false;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800902 const std::vector<int32_t> &sensorPixelModesUsed =
903 outputConfiguration.getSensorPixelModesUsed();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700904 for (auto& bufferProducer : bufferProducers) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700905 // Don't create multiple streams for the same target surface
Shuzhen Wang0129d522016-10-30 22:43:41 -0700906 sp<IBinder> binder = IInterface::asBinder(bufferProducer);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800907 ssize_t index = mStreamMap.indexOfKey(binder);
908 if (index != NAME_NOT_FOUND) {
909 String8 msg = String8::format("Camera %s: Surface already has a stream created for it "
910 "(ID %zd)", mCameraIdStr.string(), index);
911 ALOGW("%s: %s", __FUNCTION__, msg.string());
912 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
Shuzhen Wang0129d522016-10-30 22:43:41 -0700913 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700914
Shuzhen Wang758c2152017-01-10 18:26:18 -0800915 sp<Surface> surface;
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700916 res = SessionConfigurationUtils::createSurfaceFromGbp(streamInfo,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800917 isStreamInfoValid, surface, bufferProducer, mCameraIdStr,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -0800918 mDevice->infoPhysical(physicalCameraId), sensorPixelModesUsed, dynamicRangeProfile,
Shuzhen Wang610d7b82022-02-08 14:37:22 -0800919 streamUseCase, timestampBase, mirrorMode);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800920
921 if (!res.isOk())
922 return res;
923
924 if (!isStreamInfoValid) {
925 isStreamInfoValid = true;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700926 }
927
Shuzhen Wang758c2152017-01-10 18:26:18 -0800928 binders.push_back(IInterface::asBinder(bufferProducer));
Shuzhen Wang0129d522016-10-30 22:43:41 -0700929 surfaces.push_back(surface);
Ruben Brunkbba75572014-11-20 17:29:50 -0800930 }
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700931
932 // If mOverrideForPerfClass is true, do not fail createStream() for small
933 // JPEG sizes because existing createSurfaceFromGbp() logic will find the
934 // closest possible supported size.
935
Zhijun He125684a2015-12-26 15:07:30 -0800936 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
Emilian Peev40ead602017-09-26 15:46:36 +0100937 std::vector<int> surfaceIds;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800938 bool isDepthCompositeStream =
939 camera3::DepthCompositeStream::isDepthCompositeStream(surfaces[0]);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800940 bool isHeicCompisiteStream = camera3::HeicCompositeStream::isHeicCompositeStream(surfaces[0]);
941 if (isDepthCompositeStream || isHeicCompisiteStream) {
942 sp<CompositeStream> compositeStream;
943 if (isDepthCompositeStream) {
944 compositeStream = new camera3::DepthCompositeStream(mDevice, getRemoteCallback());
945 } else {
946 compositeStream = new camera3::HeicCompositeStream(mDevice, getRemoteCallback());
947 }
948
Emilian Peev538c90e2018-12-17 18:03:19 +0000949 err = compositeStream->createStream(surfaces, deferredConsumer, streamInfo.width,
950 streamInfo.height, streamInfo.format,
Emilian Peevf4816702020-04-03 15:44:51 -0700951 static_cast<camera_stream_rotation_t>(outputConfiguration.getRotation()),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800952 &streamId, physicalCameraId, streamInfo.sensorPixelModesUsed, &surfaceIds,
953 outputConfiguration.getSurfaceSetID(), isShared, isMultiResolution);
Emilian Peev538c90e2018-12-17 18:03:19 +0000954 if (err == OK) {
Emilian Peev2f5d6012022-01-19 16:16:50 -0800955 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +0000956 mCompositeStreamMap.add(IInterface::asBinder(surfaces[0]->getIGraphicBufferProducer()),
957 compositeStream);
958 }
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800959 } else {
960 err = mDevice->createStream(surfaces, deferredConsumer, streamInfo.width,
961 streamInfo.height, streamInfo.format, streamInfo.dataSpace,
Emilian Peevf4816702020-04-03 15:44:51 -0700962 static_cast<camera_stream_rotation_t>(outputConfiguration.getRotation()),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800963 &streamId, physicalCameraId, streamInfo.sensorPixelModesUsed, &surfaceIds,
Emilian Peev2295df72021-11-12 18:14:10 -0800964 outputConfiguration.getSurfaceSetID(), isShared, isMultiResolution,
Shuzhen Wange4208922022-02-01 16:52:48 -0800965 /*consumerUsage*/0, streamInfo.dynamicRangeProfile, streamInfo.streamUseCase,
Shuzhen Wang610d7b82022-02-08 14:37:22 -0800966 streamInfo.timestampBase, streamInfo.mirrorMode);
Emilian Peev538c90e2018-12-17 18:03:19 +0000967 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700968
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800969 if (err != OK) {
970 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800971 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
Shuzhen Wang758c2152017-01-10 18:26:18 -0800972 mCameraIdStr.string(), streamInfo.width, streamInfo.height, streamInfo.format,
973 streamInfo.dataSpace, strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800974 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700975 int i = 0;
976 for (auto& binder : binders) {
977 ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %d",
978 __FUNCTION__, binder.get(), streamId, i);
Emilian Peev40ead602017-09-26 15:46:36 +0100979 mStreamMap.add(binder, StreamSurfaceId(streamId, surfaceIds[i]));
980 i++;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700981 }
Shuzhen Wang758c2152017-01-10 18:26:18 -0800982
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800983 mConfiguredOutputs.add(streamId, outputConfiguration);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800984 mStreamInfoMap[streamId] = streamInfo;
985
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800986 ALOGV("%s: Camera %s: Successfully created a new stream ID %d for output surface"
Shuzhen Wang0129d522016-10-30 22:43:41 -0700987 " (%d x %d) with format 0x%x.",
Shuzhen Wang758c2152017-01-10 18:26:18 -0800988 __FUNCTION__, mCameraIdStr.string(), streamId, streamInfo.width,
989 streamInfo.height, streamInfo.format);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700990
Zhijun He5d677d12016-05-29 16:52:39 -0700991 // Set transform flags to ensure preview to be rotated correctly.
Shuzhen Wang610d7b82022-02-08 14:37:22 -0800992 res = setStreamTransformLocked(streamId, streamInfo.mirrorMode);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700993
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800994 // Fill in mHighResolutionCameraIdToStreamIdSet map
995 const String8 &cameraIdUsed =
996 physicalCameraId.size() != 0 ? physicalCameraId : mCameraIdStr;
997 const char *cameraIdUsedCStr = cameraIdUsed.string();
998 // Only needed for high resolution sensors
999 if (mHighResolutionSensors.find(cameraIdUsedCStr) !=
1000 mHighResolutionSensors.end()) {
1001 mHighResolutionCameraIdToStreamIdSet[cameraIdUsedCStr].insert(streamId);
1002 }
1003
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001004 *newStreamId = streamId;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001005 }
1006
1007 return res;
1008}
1009
Zhijun He5d677d12016-05-29 16:52:39 -07001010binder::Status CameraDeviceClient::createDeferredSurfaceStreamLocked(
1011 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
Shuzhen Wang758c2152017-01-10 18:26:18 -08001012 bool isShared,
Zhijun He5d677d12016-05-29 16:52:39 -07001013 /*out*/
1014 int* newStreamId) {
1015 int width, height, format, surfaceType;
Emilian Peev050f5dc2017-05-18 14:43:56 +01001016 uint64_t consumerUsage;
Zhijun He5d677d12016-05-29 16:52:39 -07001017 android_dataspace dataSpace;
1018 status_t err;
1019 binder::Status res;
1020
1021 if (!mDevice.get()) {
1022 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1023 }
1024
1025 // Infer the surface info for deferred surface stream creation.
1026 width = outputConfiguration.getWidth();
1027 height = outputConfiguration.getHeight();
1028 surfaceType = outputConfiguration.getSurfaceType();
1029 format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
1030 dataSpace = android_dataspace_t::HAL_DATASPACE_UNKNOWN;
1031 // Hardcode consumer usage flags: SurfaceView--0x900, SurfaceTexture--0x100.
1032 consumerUsage = GraphicBuffer::USAGE_HW_TEXTURE;
1033 if (surfaceType == OutputConfiguration::SURFACE_TYPE_SURFACE_VIEW) {
1034 consumerUsage |= GraphicBuffer::USAGE_HW_COMPOSER;
1035 }
1036 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001037 std::vector<sp<Surface>> noSurface;
Emilian Peev40ead602017-09-26 15:46:36 +01001038 std::vector<int> surfaceIds;
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001039 String8 physicalCameraId(outputConfiguration.getPhysicalCameraId());
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001040 const String8 &cameraIdUsed =
1041 physicalCameraId.size() != 0 ? physicalCameraId : mCameraIdStr;
1042 // Here, we override sensor pixel modes
1043 std::unordered_set<int32_t> overriddenSensorPixelModesUsed;
1044 const std::vector<int32_t> &sensorPixelModesUsed =
1045 outputConfiguration.getSensorPixelModesUsed();
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07001046 if (SessionConfigurationUtils::checkAndOverrideSensorPixelModesUsed(
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001047 sensorPixelModesUsed, format, width, height, getStaticInfo(cameraIdUsed),
1048 /*allowRounding*/ false, &overriddenSensorPixelModesUsed) != OK) {
1049 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1050 "sensor pixel modes used not valid for deferred stream");
1051 }
1052
Shuzhen Wang0129d522016-10-30 22:43:41 -07001053 err = mDevice->createStream(noSurface, /*hasDeferredConsumer*/true, width,
1054 height, format, dataSpace,
Emilian Peevf4816702020-04-03 15:44:51 -07001055 static_cast<camera_stream_rotation_t>(outputConfiguration.getRotation()),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001056 &streamId, physicalCameraId,
1057 overriddenSensorPixelModesUsed,
1058 &surfaceIds,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001059 outputConfiguration.getSurfaceSetID(), isShared,
Emilian Peev2295df72021-11-12 18:14:10 -08001060 outputConfiguration.isMultiResolution(), consumerUsage,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001061 outputConfiguration.getDynamicRangeProfile(),
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001062 outputConfiguration.getStreamUseCase(),
1063 outputConfiguration.getMirrorMode());
Zhijun He5d677d12016-05-29 16:52:39 -07001064
1065 if (err != OK) {
1066 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001067 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
1068 mCameraIdStr.string(), width, height, format, dataSpace, strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001069 } else {
1070 // Can not add streamId to mStreamMap here, as the surface is deferred. Add it to
1071 // a separate list to track. Once the deferred surface is set, this id will be
1072 // relocated to mStreamMap.
1073 mDeferredStreams.push_back(streamId);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001074 mStreamInfoMap.emplace(std::piecewise_construct, std::forward_as_tuple(streamId),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001075 std::forward_as_tuple(width, height, format, dataSpace, consumerUsage,
Emilian Peev2295df72021-11-12 18:14:10 -08001076 overriddenSensorPixelModesUsed,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001077 outputConfiguration.getDynamicRangeProfile(),
Shuzhen Wange4208922022-02-01 16:52:48 -08001078 outputConfiguration.getStreamUseCase(),
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001079 outputConfiguration.getTimestampBase(),
1080 outputConfiguration.getMirrorMode()));
Shuzhen Wang758c2152017-01-10 18:26:18 -08001081
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001082 ALOGV("%s: Camera %s: Successfully created a new stream ID %d for a deferred surface"
Zhijun He5d677d12016-05-29 16:52:39 -07001083 " (%d x %d) stream with format 0x%x.",
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001084 __FUNCTION__, mCameraIdStr.string(), streamId, width, height, format);
Zhijun He5d677d12016-05-29 16:52:39 -07001085
1086 // Set transform flags to ensure preview to be rotated correctly.
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001087 res = setStreamTransformLocked(streamId, outputConfiguration.getMirrorMode());
Zhijun He5d677d12016-05-29 16:52:39 -07001088
1089 *newStreamId = streamId;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001090 // Fill in mHighResolutionCameraIdToStreamIdSet
1091 const char *cameraIdUsedCStr = cameraIdUsed.string();
1092 // Only needed for high resolution sensors
1093 if (mHighResolutionSensors.find(cameraIdUsedCStr) !=
1094 mHighResolutionSensors.end()) {
1095 mHighResolutionCameraIdToStreamIdSet[cameraIdUsed.string()].insert(streamId);
1096 }
Zhijun He5d677d12016-05-29 16:52:39 -07001097 }
1098 return res;
1099}
1100
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001101binder::Status CameraDeviceClient::setStreamTransformLocked(int streamId, int mirrorMode) {
Zhijun He5d677d12016-05-29 16:52:39 -07001102 int32_t transform = 0;
1103 status_t err;
1104 binder::Status res;
1105
1106 if (!mDevice.get()) {
1107 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1108 }
1109
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001110 err = getRotationTransformLocked(mirrorMode, &transform);
Zhijun He5d677d12016-05-29 16:52:39 -07001111 if (err != OK) {
1112 // Error logged by getRotationTransformLocked.
1113 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
1114 "Unable to calculate rotation transform for new stream");
1115 }
1116
1117 err = mDevice->setStreamTransform(streamId, transform);
1118 if (err != OK) {
1119 String8 msg = String8::format("Failed to set stream transform (stream id %d)",
1120 streamId);
1121 ALOGE("%s: %s", __FUNCTION__, msg.string());
1122 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
1123 }
1124
1125 return res;
1126}
Ruben Brunkbba75572014-11-20 17:29:50 -08001127
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001128binder::Status CameraDeviceClient::createInputStream(
Shuzhen Wang83bff122020-11-20 15:51:39 -08001129 int width, int height, int format, bool isMultiResolution,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001130 /*out*/
1131 int32_t* newStreamId) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001132
1133 ATRACE_CALL();
Shuzhen Wang83bff122020-11-20 15:51:39 -08001134 ALOGV("%s (w = %d, h = %d, f = 0x%x, isMultiResolution %d)", __FUNCTION__,
1135 width, height, format, isMultiResolution);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001136
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001137 binder::Status res;
1138 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001139
1140 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001141
1142 if (!mDevice.get()) {
1143 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1144 }
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001145
1146 if (mInputStream.configured) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001147 String8 msg = String8::format("Camera %s: Already has an input stream "
Yi Kong0f414de2017-12-15 13:48:50 -08001148 "configured (ID %d)", mCameraIdStr.string(), mInputStream.id);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001149 ALOGE("%s: %s", __FUNCTION__, msg.string() );
1150 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001151 }
1152
1153 int streamId = -1;
Shuzhen Wang83bff122020-11-20 15:51:39 -08001154 status_t err = mDevice->createInputStream(width, height, format, isMultiResolution, &streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001155 if (err == OK) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001156 mInputStream.configured = true;
1157 mInputStream.width = width;
1158 mInputStream.height = height;
1159 mInputStream.format = format;
1160 mInputStream.id = streamId;
1161
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001162 ALOGV("%s: Camera %s: Successfully created a new input stream ID %d",
1163 __FUNCTION__, mCameraIdStr.string(), streamId);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001164
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001165 *newStreamId = streamId;
1166 } else {
1167 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001168 "Camera %s: Error creating new input stream: %s (%d)", mCameraIdStr.string(),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001169 strerror(-err), err);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001170 }
1171
1172 return res;
1173}
1174
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001175binder::Status CameraDeviceClient::getInputSurface(/*out*/ view::Surface *inputSurface) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001176
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001177 binder::Status res;
1178 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1179
1180 if (inputSurface == NULL) {
1181 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Null input surface");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001182 }
1183
1184 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001185 if (!mDevice.get()) {
1186 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1187 }
1188 sp<IGraphicBufferProducer> producer;
1189 status_t err = mDevice->getInputBufferProducer(&producer);
1190 if (err != OK) {
1191 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001192 "Camera %s: Error getting input Surface: %s (%d)",
1193 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001194 } else {
1195 inputSurface->name = String16("CameraInput");
1196 inputSurface->graphicBufferProducer = producer;
1197 }
1198 return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001199}
1200
Emilian Peev40ead602017-09-26 15:46:36 +01001201binder::Status CameraDeviceClient::updateOutputConfiguration(int streamId,
1202 const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
1203 ATRACE_CALL();
1204
1205 binder::Status res;
1206 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1207
1208 Mutex::Autolock icl(mBinderSerializationLock);
1209
1210 if (!mDevice.get()) {
1211 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1212 }
1213
1214 const std::vector<sp<IGraphicBufferProducer> >& bufferProducers =
1215 outputConfiguration.getGraphicBufferProducers();
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -07001216 String8 physicalCameraId(outputConfiguration.getPhysicalCameraId());
1217
Emilian Peev40ead602017-09-26 15:46:36 +01001218 auto producerCount = bufferProducers.size();
1219 if (producerCount == 0) {
1220 ALOGE("%s: bufferProducers must not be empty", __FUNCTION__);
1221 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1222 "bufferProducers must not be empty");
1223 }
1224
1225 // The first output is the one associated with the output configuration.
1226 // It should always be present, valid and the corresponding stream id should match.
1227 sp<IBinder> binder = IInterface::asBinder(bufferProducers[0]);
1228 ssize_t index = mStreamMap.indexOfKey(binder);
1229 if (index == NAME_NOT_FOUND) {
1230 ALOGE("%s: Outputconfiguration is invalid", __FUNCTION__);
1231 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1232 "OutputConfiguration is invalid");
1233 }
1234 if (mStreamMap.valueFor(binder).streamId() != streamId) {
1235 ALOGE("%s: Stream Id: %d provided doesn't match the id: %d in the stream map",
1236 __FUNCTION__, streamId, mStreamMap.valueFor(binder).streamId());
1237 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1238 "Stream id is invalid");
1239 }
1240
1241 std::vector<size_t> removedSurfaceIds;
1242 std::vector<sp<IBinder>> removedOutputs;
1243 std::vector<sp<Surface>> newOutputs;
1244 std::vector<OutputStreamInfo> streamInfos;
1245 KeyedVector<sp<IBinder>, sp<IGraphicBufferProducer>> newOutputsMap;
1246 for (auto &it : bufferProducers) {
1247 newOutputsMap.add(IInterface::asBinder(it), it);
1248 }
1249
1250 for (size_t i = 0; i < mStreamMap.size(); i++) {
1251 ssize_t idx = newOutputsMap.indexOfKey(mStreamMap.keyAt(i));
1252 if (idx == NAME_NOT_FOUND) {
1253 if (mStreamMap[i].streamId() == streamId) {
1254 removedSurfaceIds.push_back(mStreamMap[i].surfaceId());
1255 removedOutputs.push_back(mStreamMap.keyAt(i));
1256 }
1257 } else {
1258 if (mStreamMap[i].streamId() != streamId) {
1259 ALOGE("%s: Output surface already part of a different stream", __FUNCTION__);
1260 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1261 "Target Surface is invalid");
1262 }
1263 newOutputsMap.removeItemsAt(idx);
1264 }
1265 }
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001266 const std::vector<int32_t> &sensorPixelModesUsed =
1267 outputConfiguration.getSensorPixelModesUsed();
Shuzhen Wang8ed1e872022-03-08 16:34:33 -08001268 int64_t streamUseCase = outputConfiguration.getStreamUseCase();
Shuzhen Wange4208922022-02-01 16:52:48 -08001269 int timestampBase = outputConfiguration.getTimestampBase();
Emilian Peevc81a7592022-02-14 17:38:18 -08001270 int64_t dynamicRangeProfile = outputConfiguration.getDynamicRangeProfile();
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001271 int mirrorMode = outputConfiguration.getMirrorMode();
Emilian Peev2295df72021-11-12 18:14:10 -08001272
Emilian Peev40ead602017-09-26 15:46:36 +01001273 for (size_t i = 0; i < newOutputsMap.size(); i++) {
1274 OutputStreamInfo outInfo;
1275 sp<Surface> surface;
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07001276 res = SessionConfigurationUtils::createSurfaceFromGbp(outInfo,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001277 /*isStreamInfoValid*/ false, surface, newOutputsMap.valueAt(i), mCameraIdStr,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001278 mDevice->infoPhysical(physicalCameraId), sensorPixelModesUsed, dynamicRangeProfile,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001279 streamUseCase, timestampBase, mirrorMode);
Emilian Peev40ead602017-09-26 15:46:36 +01001280 if (!res.isOk())
1281 return res;
1282
Emilian Peev40ead602017-09-26 15:46:36 +01001283 streamInfos.push_back(outInfo);
1284 newOutputs.push_back(surface);
1285 }
1286
1287 //Trivial case no changes required
1288 if (removedSurfaceIds.empty() && newOutputs.empty()) {
1289 return binder::Status::ok();
1290 }
1291
1292 KeyedVector<sp<Surface>, size_t> outputMap;
1293 auto ret = mDevice->updateStream(streamId, newOutputs, streamInfos, removedSurfaceIds,
1294 &outputMap);
1295 if (ret != OK) {
1296 switch (ret) {
1297 case NAME_NOT_FOUND:
1298 case BAD_VALUE:
1299 case -EBUSY:
1300 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1301 "Camera %s: Error updating stream: %s (%d)",
1302 mCameraIdStr.string(), strerror(ret), ret);
1303 break;
1304 default:
1305 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1306 "Camera %s: Error updating stream: %s (%d)",
1307 mCameraIdStr.string(), strerror(ret), ret);
1308 break;
1309 }
1310 } else {
1311 for (const auto &it : removedOutputs) {
1312 mStreamMap.removeItem(it);
1313 }
1314
1315 for (size_t i = 0; i < outputMap.size(); i++) {
1316 mStreamMap.add(IInterface::asBinder(outputMap.keyAt(i)->getIGraphicBufferProducer()),
1317 StreamSurfaceId(streamId, outputMap.valueAt(i)));
1318 }
1319
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -08001320 mConfiguredOutputs.replaceValueFor(streamId, outputConfiguration);
1321
Emilian Peev40ead602017-09-26 15:46:36 +01001322 ALOGV("%s: Camera %s: Successful stream ID %d update",
1323 __FUNCTION__, mCameraIdStr.string(), streamId);
1324 }
1325
1326 return res;
1327}
1328
Igor Murashkine7ee7632013-06-11 18:10:18 -07001329// Create a request object from a template.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001330binder::Status CameraDeviceClient::createDefaultRequest(int templateId,
1331 /*out*/
1332 hardware::camera2::impl::CameraMetadataNative* request)
Igor Murashkine7ee7632013-06-11 18:10:18 -07001333{
1334 ATRACE_CALL();
1335 ALOGV("%s (templateId = 0x%x)", __FUNCTION__, templateId);
1336
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001337 binder::Status res;
1338 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001339
1340 Mutex::Autolock icl(mBinderSerializationLock);
1341
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001342 if (!mDevice.get()) {
1343 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1344 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001345
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001346 status_t err;
Emilian Peevf4816702020-04-03 15:44:51 -07001347 camera_request_template_t tempId = camera_request_template_t::CAMERA_TEMPLATE_COUNT;
1348 if (!(res = mapRequestTemplate(templateId, &tempId)).isOk()) return res;
1349
1350 CameraMetadata metadata;
1351 if ( (err = mDevice->createDefaultRequest(tempId, &metadata) ) == OK &&
Igor Murashkine7ee7632013-06-11 18:10:18 -07001352 request != NULL) {
1353
1354 request->swap(metadata);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001355 } else if (err == BAD_VALUE) {
1356 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001357 "Camera %s: Template ID %d is invalid or not supported: %s (%d)",
1358 mCameraIdStr.string(), templateId, strerror(-err), err);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001359
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001360 } else {
1361 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001362 "Camera %s: Error creating default request for template %d: %s (%d)",
1363 mCameraIdStr.string(), templateId, strerror(-err), err);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001364 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001365 return res;
1366}
1367
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001368binder::Status CameraDeviceClient::getCameraInfo(
1369 /*out*/
1370 hardware::camera2::impl::CameraMetadataNative* info)
Igor Murashkine7ee7632013-06-11 18:10:18 -07001371{
1372 ATRACE_CALL();
1373 ALOGV("%s", __FUNCTION__);
1374
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001375 binder::Status res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001376
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001377 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001378
1379 Mutex::Autolock icl(mBinderSerializationLock);
1380
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001381 if (!mDevice.get()) {
1382 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1383 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001384
Igor Murashkin099b4572013-07-12 17:52:16 -07001385 if (info != NULL) {
1386 *info = mDevice->info(); // static camera metadata
1387 // TODO: merge with device-specific camera metadata
1388 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001389
1390 return res;
1391}
1392
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001393binder::Status CameraDeviceClient::waitUntilIdle()
Zhijun He2ab500c2013-07-23 08:02:53 -07001394{
1395 ATRACE_CALL();
1396 ALOGV("%s", __FUNCTION__);
1397
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001398 binder::Status res;
1399 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Zhijun He2ab500c2013-07-23 08:02:53 -07001400
1401 Mutex::Autolock icl(mBinderSerializationLock);
1402
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001403 if (!mDevice.get()) {
1404 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1405 }
Zhijun He2ab500c2013-07-23 08:02:53 -07001406
1407 // FIXME: Also need check repeating burst.
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001408 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001409 if (mStreamingRequestId != REQUEST_ID_NONE) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001410 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001411 "Camera %s: Try to waitUntilIdle when there are active streaming requests",
1412 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001413 ALOGE("%s: %s", __FUNCTION__, msg.string());
1414 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Zhijun He2ab500c2013-07-23 08:02:53 -07001415 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001416 status_t err = mDevice->waitUntilDrained();
1417 if (err != OK) {
1418 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001419 "Camera %s: Error waiting to drain: %s (%d)",
1420 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001421 }
Zhijun He2ab500c2013-07-23 08:02:53 -07001422 ALOGV("%s Done", __FUNCTION__);
Zhijun He2ab500c2013-07-23 08:02:53 -07001423 return res;
1424}
1425
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001426binder::Status CameraDeviceClient::flush(
1427 /*out*/
1428 int64_t* lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001429 ATRACE_CALL();
1430 ALOGV("%s", __FUNCTION__);
1431
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001432 binder::Status res;
1433 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001434
1435 Mutex::Autolock icl(mBinderSerializationLock);
1436
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001437 if (!mDevice.get()) {
1438 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1439 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001440
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001441 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001442 mStreamingRequestId = REQUEST_ID_NONE;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001443 status_t err = mDevice->flush(lastFrameNumber);
1444 if (err != OK) {
1445 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001446 "Camera %s: Error flushing device: %s (%d)", mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001447 }
1448 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001449}
1450
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001451binder::Status CameraDeviceClient::prepare(int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001452 ATRACE_CALL();
1453 ALOGV("%s", __FUNCTION__);
1454
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001455 binder::Status res;
1456 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001457
1458 Mutex::Autolock icl(mBinderSerializationLock);
1459
1460 // Guard against trying to prepare non-created streams
1461 ssize_t index = NAME_NOT_FOUND;
1462 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001463 if (streamId == mStreamMap.valueAt(i).streamId()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001464 index = i;
1465 break;
1466 }
1467 }
1468
1469 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001470 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1471 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001472 ALOGW("%s: %s", __FUNCTION__, msg.string());
1473 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001474 }
1475
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001476 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1477 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001478 status_t err = mDevice->prepare(streamId);
1479 if (err == BAD_VALUE) {
1480 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001481 "Camera %s: Stream %d has already been used, and cannot be prepared",
1482 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001483 } else if (err != OK) {
1484 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001485 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001486 strerror(-err), err);
1487 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001488 return res;
1489}
1490
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001491binder::Status CameraDeviceClient::prepare2(int maxCount, int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001492 ATRACE_CALL();
1493 ALOGV("%s", __FUNCTION__);
1494
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001495 binder::Status res;
1496 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Ruben Brunkc78ac262015-08-13 17:58:46 -07001497
1498 Mutex::Autolock icl(mBinderSerializationLock);
1499
1500 // Guard against trying to prepare non-created streams
1501 ssize_t index = NAME_NOT_FOUND;
1502 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001503 if (streamId == mStreamMap.valueAt(i).streamId()) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001504 index = i;
1505 break;
1506 }
1507 }
1508
1509 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001510 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1511 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001512 ALOGW("%s: %s", __FUNCTION__, msg.string());
1513 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -07001514 }
1515
1516 if (maxCount <= 0) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001517 String8 msg = String8::format("Camera %s: maxCount (%d) must be greater than 0",
1518 mCameraIdStr.string(), maxCount);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001519 ALOGE("%s: %s", __FUNCTION__, msg.string());
1520 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -07001521 }
1522
1523 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1524 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001525 status_t err = mDevice->prepare(maxCount, streamId);
1526 if (err == BAD_VALUE) {
1527 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001528 "Camera %s: Stream %d has already been used, and cannot be prepared",
1529 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001530 } else if (err != OK) {
1531 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001532 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001533 strerror(-err), err);
1534 }
Ruben Brunkc78ac262015-08-13 17:58:46 -07001535
1536 return res;
1537}
1538
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001539binder::Status CameraDeviceClient::tearDown(int streamId) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001540 ATRACE_CALL();
1541 ALOGV("%s", __FUNCTION__);
1542
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001543 binder::Status res;
1544 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001545
1546 Mutex::Autolock icl(mBinderSerializationLock);
1547
1548 // Guard against trying to prepare non-created streams
1549 ssize_t index = NAME_NOT_FOUND;
1550 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001551 if (streamId == mStreamMap.valueAt(i).streamId()) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001552 index = i;
1553 break;
1554 }
1555 }
1556
1557 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001558 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1559 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001560 ALOGW("%s: %s", __FUNCTION__, msg.string());
1561 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001562 }
1563
1564 // Also returns BAD_VALUE if stream ID was not valid or if the stream is in
1565 // use
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001566 status_t err = mDevice->tearDown(streamId);
1567 if (err == BAD_VALUE) {
1568 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001569 "Camera %s: Stream %d is still in use, cannot be torn down",
1570 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001571 } else if (err != OK) {
1572 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001573 "Camera %s: Error tearing down stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001574 strerror(-err), err);
1575 }
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001576
1577 return res;
1578}
1579
Shuzhen Wang758c2152017-01-10 18:26:18 -08001580binder::Status CameraDeviceClient::finalizeOutputConfigurations(int32_t streamId,
Zhijun He5d677d12016-05-29 16:52:39 -07001581 const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
1582 ATRACE_CALL();
1583
1584 binder::Status res;
1585 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1586
1587 Mutex::Autolock icl(mBinderSerializationLock);
1588
Shuzhen Wang0129d522016-10-30 22:43:41 -07001589 const std::vector<sp<IGraphicBufferProducer> >& bufferProducers =
1590 outputConfiguration.getGraphicBufferProducers();
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -07001591 String8 physicalId(outputConfiguration.getPhysicalCameraId());
Zhijun He5d677d12016-05-29 16:52:39 -07001592
Shuzhen Wang0129d522016-10-30 22:43:41 -07001593 if (bufferProducers.size() == 0) {
1594 ALOGE("%s: bufferProducers must not be empty", __FUNCTION__);
Zhijun He5d677d12016-05-29 16:52:39 -07001595 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Target Surface is invalid");
1596 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001597
Shuzhen Wang758c2152017-01-10 18:26:18 -08001598 // streamId should be in mStreamMap if this stream already has a surface attached
1599 // to it. Otherwise, it should be in mDeferredStreams.
1600 bool streamIdConfigured = false;
1601 ssize_t deferredStreamIndex = NAME_NOT_FOUND;
1602 for (size_t i = 0; i < mStreamMap.size(); i++) {
1603 if (mStreamMap.valueAt(i).streamId() == streamId) {
1604 streamIdConfigured = true;
1605 break;
1606 }
Zhijun He5d677d12016-05-29 16:52:39 -07001607 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001608 for (size_t i = 0; i < mDeferredStreams.size(); i++) {
1609 if (streamId == mDeferredStreams[i]) {
1610 deferredStreamIndex = i;
1611 break;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001612 }
1613
Shuzhen Wang758c2152017-01-10 18:26:18 -08001614 }
1615 if (deferredStreamIndex == NAME_NOT_FOUND && !streamIdConfigured) {
1616 String8 msg = String8::format("Camera %s: deferred surface is set to a unknown stream"
1617 "(ID %d)", mCameraIdStr.string(), streamId);
1618 ALOGW("%s: %s", __FUNCTION__, msg.string());
1619 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Zhijun He5d677d12016-05-29 16:52:39 -07001620 }
1621
Shuzhen Wang88fd0052017-02-09 16:40:07 -08001622 if (mStreamInfoMap[streamId].finalized) {
1623 String8 msg = String8::format("Camera %s: finalizeOutputConfigurations has been called"
1624 " on stream ID %d", mCameraIdStr.string(), streamId);
1625 ALOGW("%s: %s", __FUNCTION__, msg.string());
1626 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1627 }
1628
Zhijun He5d677d12016-05-29 16:52:39 -07001629 if (!mDevice.get()) {
1630 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1631 }
1632
Shuzhen Wang758c2152017-01-10 18:26:18 -08001633 std::vector<sp<Surface>> consumerSurfaces;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001634 const std::vector<int32_t> &sensorPixelModesUsed =
1635 outputConfiguration.getSensorPixelModesUsed();
Emilian Peevc81a7592022-02-14 17:38:18 -08001636 int64_t dynamicRangeProfile = outputConfiguration.getDynamicRangeProfile();
Shuzhen Wang8ed1e872022-03-08 16:34:33 -08001637 int64_t streamUseCase= outputConfiguration.getStreamUseCase();
Shuzhen Wange4208922022-02-01 16:52:48 -08001638 int timestampBase = outputConfiguration.getTimestampBase();
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001639 int mirrorMode = outputConfiguration.getMirrorMode();
Shuzhen Wang758c2152017-01-10 18:26:18 -08001640 for (auto& bufferProducer : bufferProducers) {
1641 // Don't create multiple streams for the same target surface
Zhijun He5d677d12016-05-29 16:52:39 -07001642 ssize_t index = mStreamMap.indexOfKey(IInterface::asBinder(bufferProducer));
1643 if (index != NAME_NOT_FOUND) {
Shuzhen Wang758c2152017-01-10 18:26:18 -08001644 ALOGV("Camera %s: Surface already has a stream created "
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001645 " for it (ID %zd)", mCameraIdStr.string(), index);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001646 continue;
Zhijun He5d677d12016-05-29 16:52:39 -07001647 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001648
1649 sp<Surface> surface;
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07001650 res = SessionConfigurationUtils::createSurfaceFromGbp(mStreamInfoMap[streamId],
Colin Crossb8a9dbb2020-08-27 04:12:26 +00001651 true /*isStreamInfoValid*/, surface, bufferProducer, mCameraIdStr,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001652 mDevice->infoPhysical(physicalId), sensorPixelModesUsed, dynamicRangeProfile,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001653 streamUseCase, timestampBase, mirrorMode);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001654
1655 if (!res.isOk())
1656 return res;
1657
1658 consumerSurfaces.push_back(surface);
Zhijun He5d677d12016-05-29 16:52:39 -07001659 }
1660
Shuzhen Wanga81ce342017-04-13 15:18:36 -07001661 // Gracefully handle case where finalizeOutputConfigurations is called
1662 // without any new surface.
1663 if (consumerSurfaces.size() == 0) {
1664 mStreamInfoMap[streamId].finalized = true;
1665 return res;
1666 }
1667
Zhijun He5d677d12016-05-29 16:52:39 -07001668 // Finish the deferred stream configuration with the surface.
Shuzhen Wang758c2152017-01-10 18:26:18 -08001669 status_t err;
Emilian Peev40ead602017-09-26 15:46:36 +01001670 std::vector<int> consumerSurfaceIds;
1671 err = mDevice->setConsumerSurfaces(streamId, consumerSurfaces, &consumerSurfaceIds);
Zhijun He5d677d12016-05-29 16:52:39 -07001672 if (err == OK) {
Shuzhen Wang758c2152017-01-10 18:26:18 -08001673 for (size_t i = 0; i < consumerSurfaces.size(); i++) {
1674 sp<IBinder> binder = IInterface::asBinder(
1675 consumerSurfaces[i]->getIGraphicBufferProducer());
Emilian Peev40ead602017-09-26 15:46:36 +01001676 ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %d", __FUNCTION__,
Shuzhen Wang758c2152017-01-10 18:26:18 -08001677 binder.get(), streamId, consumerSurfaceIds[i]);
1678 mStreamMap.add(binder, StreamSurfaceId(streamId, consumerSurfaceIds[i]));
1679 }
1680 if (deferredStreamIndex != NAME_NOT_FOUND) {
1681 mDeferredStreams.removeItemsAt(deferredStreamIndex);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001682 }
Shuzhen Wang88fd0052017-02-09 16:40:07 -08001683 mStreamInfoMap[streamId].finalized = true;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -08001684 mConfiguredOutputs.replaceValueFor(streamId, outputConfiguration);
Zhijun He5d677d12016-05-29 16:52:39 -07001685 } else if (err == NO_INIT) {
1686 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001687 "Camera %s: Deferred surface is invalid: %s (%d)",
1688 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001689 } else {
1690 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001691 "Camera %s: Error setting output stream deferred surface: %s (%d)",
1692 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001693 }
1694
1695 return res;
1696}
1697
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001698binder::Status CameraDeviceClient::setCameraAudioRestriction(int32_t mode) {
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001699 ATRACE_CALL();
1700 binder::Status res;
1701 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1702
1703 if (!isValidAudioRestriction(mode)) {
1704 String8 msg = String8::format("Camera %s: invalid audio restriction mode %d",
1705 mCameraIdStr.string(), mode);
1706 ALOGW("%s: %s", __FUNCTION__, msg.string());
1707 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1708 }
1709
1710 Mutex::Autolock icl(mBinderSerializationLock);
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001711 BasicClient::setAudioRestriction(mode);
1712 return binder::Status::ok();
1713}
1714
1715binder::Status CameraDeviceClient::getGlobalAudioRestriction(/*out*/ int32_t* outMode) {
1716 ATRACE_CALL();
1717 binder::Status res;
1718 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1719 Mutex::Autolock icl(mBinderSerializationLock);
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001720 if (outMode != nullptr) {
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001721 *outMode = BasicClient::getServiceAudioRestriction();
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001722 }
1723 return binder::Status::ok();
1724}
1725
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08001726status_t CameraDeviceClient::setRotateAndCropOverride(uint8_t rotateAndCrop) {
1727 if (rotateAndCrop > ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return BAD_VALUE;
1728
1729 return mDevice->setRotateAndCropAutoBehavior(
1730 static_cast<camera_metadata_enum_android_scaler_rotate_and_crop_t>(rotateAndCrop));
1731}
1732
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08001733bool CameraDeviceClient::supportsCameraMute() {
1734 return mDevice->supportsCameraMute();
1735}
1736
1737status_t CameraDeviceClient::setCameraMute(bool enabled) {
1738 return mDevice->setCameraMute(enabled);
1739}
1740
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001741binder::Status CameraDeviceClient::switchToOffline(
1742 const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb,
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001743 const std::vector<int>& offlineOutputIds,
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001744 /*out*/
1745 sp<hardware::camera2::ICameraOfflineSession>* session) {
1746 ATRACE_CALL();
1747
1748 binder::Status res;
1749 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1750
1751 Mutex::Autolock icl(mBinderSerializationLock);
1752
1753 if (!mDevice.get()) {
1754 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1755 }
1756
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001757 if (offlineOutputIds.empty()) {
Emilian Peevcc0b7952020-01-07 13:54:47 -08001758 String8 msg = String8::format("Offline surfaces must not be empty");
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001759 ALOGE("%s: %s", __FUNCTION__, msg.string());
1760 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1761 }
1762
1763 if (session == nullptr) {
1764 String8 msg = String8::format("Invalid offline session");
1765 ALOGE("%s: %s", __FUNCTION__, msg.string());
1766 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1767 }
1768
Yin-Chia Yeh87fccca2020-01-28 09:37:18 -08001769 std::vector<int32_t> offlineStreamIds;
1770 offlineStreamIds.reserve(offlineOutputIds.size());
Emilian Peev4697b642019-11-19 17:11:14 -08001771 KeyedVector<sp<IBinder>, sp<CompositeStream>> offlineCompositeStreamMap;
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001772 for (const auto& streamId : offlineOutputIds) {
1773 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001774 if (index == NAME_NOT_FOUND) {
Emilian Peevcc0b7952020-01-07 13:54:47 -08001775 String8 msg = String8::format("Offline surface with id: %d is not registered",
1776 streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001777 ALOGE("%s: %s", __FUNCTION__, msg.string());
1778 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1779 }
Emilian Peevcc0b7952020-01-07 13:54:47 -08001780
1781 if (!mStreamInfoMap[streamId].supportsOffline) {
1782 String8 msg = String8::format("Offline surface with id: %d doesn't support "
1783 "offline mode", streamId);
1784 ALOGE("%s: %s", __FUNCTION__, msg.string());
1785 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1786 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001787
Emilian Peev2f5d6012022-01-19 16:16:50 -08001788 Mutex::Autolock l(mCompositeLock);
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001789 bool isCompositeStream = false;
Emilian Peev2f5d6012022-01-19 16:16:50 -08001790 for (const auto& gbp : mConfiguredOutputs.valueAt(index).getGraphicBufferProducers()) {
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001791 sp<Surface> s = new Surface(gbp, false /*controlledByApp*/);
Pirama Arumuga Nainar8db21062022-01-28 10:15:12 -08001792 isCompositeStream = camera3::DepthCompositeStream::isDepthCompositeStream(s) ||
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001793 camera3::HeicCompositeStream::isHeicCompositeStream(s);
Emilian Peev4697b642019-11-19 17:11:14 -08001794 if (isCompositeStream) {
1795 auto compositeIdx = mCompositeStreamMap.indexOfKey(IInterface::asBinder(gbp));
1796 if (compositeIdx == NAME_NOT_FOUND) {
1797 ALOGE("%s: Unknown composite stream", __FUNCTION__);
1798 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1799 "Unknown composite stream");
1800 }
1801
1802 mCompositeStreamMap.valueAt(compositeIdx)->insertCompositeStreamIds(
1803 &offlineStreamIds);
1804 offlineCompositeStreamMap.add(mCompositeStreamMap.keyAt(compositeIdx),
1805 mCompositeStreamMap.valueAt(compositeIdx));
1806 break;
1807 }
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001808 }
1809
Emilian Peev4697b642019-11-19 17:11:14 -08001810 if (!isCompositeStream) {
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001811 offlineStreamIds.push_back(streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001812 }
1813 }
1814
1815 sp<CameraOfflineSessionBase> offlineSession;
1816 auto ret = mDevice->switchToOffline(offlineStreamIds, &offlineSession);
1817 if (ret != OK) {
1818 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1819 "Camera %s: Error switching to offline mode: %s (%d)",
1820 mCameraIdStr.string(), strerror(ret), ret);
1821 }
1822
Emilian Peevcc0b7952020-01-07 13:54:47 -08001823 sp<CameraOfflineSessionClient> offlineClient;
1824 if (offlineSession.get() != nullptr) {
1825 offlineClient = new CameraOfflineSessionClient(sCameraService,
1826 offlineSession, offlineCompositeStreamMap, cameraCb, mClientPackageName,
Emilian Peev8b64f282021-03-25 16:49:57 -07001827 mClientFeatureId, mCameraIdStr, mCameraFacing, mOrientation, mClientPid, mClientUid,
1828 mServicePid);
Emilian Peevcc0b7952020-01-07 13:54:47 -08001829 ret = sCameraService->addOfflineClient(mCameraIdStr, offlineClient);
1830 }
1831
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001832 if (ret == OK) {
Emilian Peevd99c8ae2019-11-26 13:19:13 -08001833 // A successful offline session switch must reset the current camera client
1834 // and release any resources occupied by previously configured streams.
1835 mStreamMap.clear();
1836 mConfiguredOutputs.clear();
1837 mDeferredStreams.clear();
1838 mStreamInfoMap.clear();
Emilian Peev2f5d6012022-01-19 16:16:50 -08001839 Mutex::Autolock l(mCompositeLock);
Emilian Peevd99c8ae2019-11-26 13:19:13 -08001840 mCompositeStreamMap.clear();
1841 mInputStream = {false, 0, 0, 0, 0};
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001842 } else {
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001843 switch(ret) {
1844 case BAD_VALUE:
1845 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1846 "Illegal argument to HAL module for camera \"%s\"", mCameraIdStr.c_str());
1847 case TIMED_OUT:
1848 return STATUS_ERROR_FMT(CameraService::ERROR_CAMERA_IN_USE,
1849 "Camera \"%s\" is already open", mCameraIdStr.c_str());
1850 default:
1851 return STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1852 "Failed to initialize camera \"%s\": %s (%d)", mCameraIdStr.c_str(),
1853 strerror(-ret), ret);
1854 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001855 }
1856
1857 *session = offlineClient;
1858
1859 return binder::Status::ok();
1860}
1861
Igor Murashkine7ee7632013-06-11 18:10:18 -07001862status_t CameraDeviceClient::dump(int fd, const Vector<String16>& args) {
Eino-Ville Talvalac4003962016-01-13 10:07:04 -08001863 return BasicClient::dump(fd, args);
1864}
1865
1866status_t CameraDeviceClient::dumpClient(int fd, const Vector<String16>& args) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001867 dprintf(fd, " CameraDeviceClient[%s] (%p) dump:\n",
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001868 mCameraIdStr.string(),
Eino-Ville Talvalae992e752014-11-07 16:17:48 -08001869 (getRemoteCallback() != NULL ?
Marco Nelissenf8880202014-11-14 07:58:25 -08001870 IInterface::asBinder(getRemoteCallback()).get() : NULL) );
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001871 dprintf(fd, " Current client UID %u\n", mClientUid);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001872
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001873 dprintf(fd, " State:\n");
1874 dprintf(fd, " Request ID counter: %d\n", mRequestIdCounter);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001875 if (mInputStream.configured) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001876 dprintf(fd, " Current input stream ID: %d\n", mInputStream.id);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001877 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001878 dprintf(fd, " No input stream configured.\n");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001879 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001880 if (!mStreamMap.isEmpty()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001881 dprintf(fd, " Current output stream/surface IDs:\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001882 for (size_t i = 0; i < mStreamMap.size(); i++) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001883 dprintf(fd, " Stream %d Surface %d\n",
Shuzhen Wang0129d522016-10-30 22:43:41 -07001884 mStreamMap.valueAt(i).streamId(),
1885 mStreamMap.valueAt(i).surfaceId());
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001886 }
Zhijun He5d677d12016-05-29 16:52:39 -07001887 } else if (!mDeferredStreams.isEmpty()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001888 dprintf(fd, " Current deferred surface output stream IDs:\n");
Zhijun He5d677d12016-05-29 16:52:39 -07001889 for (auto& streamId : mDeferredStreams) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001890 dprintf(fd, " Stream %d\n", streamId);
Zhijun He5d677d12016-05-29 16:52:39 -07001891 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001892 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001893 dprintf(fd, " No output streams configured.\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001894 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001895 // TODO: print dynamic/request section from most recent requests
1896 mFrameProcessor->dump(fd, args);
1897
1898 return dumpDevice(fd, args);
1899}
1900
Avichal Rakesh7e53cad2021-10-05 13:46:30 -07001901status_t CameraDeviceClient::startWatchingTags(const String8 &tags, int out) {
1902 sp<CameraDeviceBase> device = mDevice;
1903 if (!device) {
1904 dprintf(out, " Device is detached.");
1905 return OK;
1906 }
1907 device->startWatchingTags(tags);
1908 return OK;
1909}
1910
1911status_t CameraDeviceClient::stopWatchingTags(int out) {
1912 sp<CameraDeviceBase> device = mDevice;
1913 if (!device) {
1914 dprintf(out, " Device is detached.");
1915 return OK;
1916 }
1917 device->stopWatchingTags();
1918 return OK;
1919}
1920
1921status_t CameraDeviceClient::dumpWatchedEventsToVector(std::vector<std::string> &out) {
1922 sp<CameraDeviceBase> device = mDevice;
1923 if (!device) {
1924 return OK;
1925 }
1926 device->dumpWatchedEventsToVector(out);
1927 return OK;
1928}
1929
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001930void CameraDeviceClient::notifyError(int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -07001931 const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001932 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001933 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001934
Emilian Peev538c90e2018-12-17 18:03:19 +00001935 bool skipClientNotification = false;
Emilian Peev2f5d6012022-01-19 16:16:50 -08001936 {
1937 // Access to the composite stream map must be synchronized
1938 Mutex::Autolock l(mCompositeLock);
1939 // Composites can have multiple internal streams. Error notifications coming from such
1940 // internal streams may need to remain within camera service.
1941 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
1942 skipClientNotification |= mCompositeStreamMap.valueAt(i)->onError(errorCode,
1943 resultExtras);
1944 }
Emilian Peev538c90e2018-12-17 18:03:19 +00001945 }
1946
1947 if ((remoteCb != 0) && (!skipClientNotification)) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001948 remoteCb->onDeviceError(errorCode, resultExtras);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001949 }
1950}
1951
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001952void CameraDeviceClient::notifyRepeatingRequestError(long lastFrameNumber) {
1953 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
1954
1955 if (remoteCb != 0) {
Yin-Chia Yeh8ca23dc2017-09-05 18:15:56 -07001956 remoteCb->onRepeatingRequestError(lastFrameNumber, mStreamingRequestId);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001957 }
1958
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001959 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001960 mStreamingRequestId = REQUEST_ID_NONE;
1961}
1962
Shuzhen Wang316781a2020-08-18 18:11:01 -07001963void CameraDeviceClient::notifyIdle(
1964 int64_t requestCount, int64_t resultErrorCount, bool deviceError,
1965 const std::vector<hardware::CameraStreamStats>& streamStats) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001966 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001967 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001968
1969 if (remoteCb != 0) {
1970 remoteCb->onDeviceIdle();
1971 }
Shuzhen Wangd26b1862022-03-07 12:05:05 -08001972 Camera2ClientBase::notifyIdleWithUserTag(requestCount, resultErrorCount, deviceError,
1973 streamStats, mUserTag);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001974}
1975
Jianing Weicb0652e2014-03-12 18:29:36 -07001976void CameraDeviceClient::notifyShutter(const CaptureResultExtras& resultExtras,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001977 nsecs_t timestamp) {
1978 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001979 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001980 if (remoteCb != 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001981 remoteCb->onCaptureStarted(resultExtras, timestamp);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001982 }
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07001983 Camera2ClientBase::notifyShutter(resultExtras, timestamp);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001984
Emilian Peev2f5d6012022-01-19 16:16:50 -08001985 // Access to the composite stream map must be synchronized
1986 Mutex::Autolock l(mCompositeLock);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001987 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
1988 mCompositeStreamMap.valueAt(i)->onShutter(resultExtras, timestamp);
1989 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001990}
1991
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001992void CameraDeviceClient::notifyPrepared(int streamId) {
1993 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001994 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001995 if (remoteCb != 0) {
1996 remoteCb->onPrepared(streamId);
1997 }
1998}
1999
Shuzhen Wang9d066012016-09-30 11:30:20 -07002000void CameraDeviceClient::notifyRequestQueueEmpty() {
2001 // Thread safe. Don't bother locking.
2002 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
2003 if (remoteCb != 0) {
2004 remoteCb->onRequestQueueEmpty();
2005 }
2006}
2007
Igor Murashkine7ee7632013-06-11 18:10:18 -07002008void CameraDeviceClient::detachDevice() {
2009 if (mDevice == 0) return;
2010
Shuzhen Wang316781a2020-08-18 18:11:01 -07002011 nsecs_t startTime = systemTime();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002012 ALOGV("Camera %s: Stopping processors", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -07002013
Emilian Peev7ee731f2022-02-08 14:55:52 -08002014 if (mFrameProcessor.get() != nullptr) {
2015 mFrameProcessor->removeListener(
2016 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MIN_ID,
2017 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MAX_ID, /*listener*/this);
2018 mFrameProcessor->requestExit();
2019 ALOGV("Camera %s: Waiting for threads", mCameraIdStr.string());
2020 mFrameProcessor->join();
2021 ALOGV("Camera %s: Disconnecting device", mCameraIdStr.string());
2022 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07002023
2024 // WORKAROUND: HAL refuses to disconnect while there's streams in flight
2025 {
Emilian Peev6b51d7d2018-07-23 11:41:44 +01002026 int64_t lastFrameNumber;
Igor Murashkine7ee7632013-06-11 18:10:18 -07002027 status_t code;
Emilian Peev6b51d7d2018-07-23 11:41:44 +01002028 if ((code = mDevice->flush(&lastFrameNumber)) != OK) {
2029 ALOGE("%s: flush failed with code 0x%x", __FUNCTION__, code);
2030 }
2031
Igor Murashkine7ee7632013-06-11 18:10:18 -07002032 if ((code = mDevice->waitUntilDrained()) != OK) {
2033 ALOGE("%s: waitUntilDrained failed with code 0x%x", __FUNCTION__,
2034 code);
2035 }
2036 }
2037
Emilian Peev2f5d6012022-01-19 16:16:50 -08002038 {
2039 Mutex::Autolock l(mCompositeLock);
2040 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
2041 auto ret = mCompositeStreamMap.valueAt(i)->deleteInternalStreams();
2042 if (ret != OK) {
2043 ALOGE("%s: Failed removing composite stream %s (%d)", __FUNCTION__,
2044 strerror(-ret), ret);
2045 }
Emilian Peev5e4c7322019-10-22 14:20:52 -07002046 }
Emilian Peev2f5d6012022-01-19 16:16:50 -08002047 mCompositeStreamMap.clear();
Emilian Peev5e4c7322019-10-22 14:20:52 -07002048 }
Emilian Peev5e4c7322019-10-22 14:20:52 -07002049
Igor Murashkine7ee7632013-06-11 18:10:18 -07002050 Camera2ClientBase::detachDevice();
Shuzhen Wang316781a2020-08-18 18:11:01 -07002051
2052 int32_t closeLatencyMs = ns2ms(systemTime() - startTime);
2053 CameraServiceProxyWrapper::logClose(mCameraIdStr, closeLatencyMs);
Igor Murashkine7ee7632013-06-11 18:10:18 -07002054}
2055
2056/** Device-related methods */
Jianing Weicb0652e2014-03-12 18:29:36 -07002057void CameraDeviceClient::onResultAvailable(const CaptureResult& result) {
Igor Murashkine7ee7632013-06-11 18:10:18 -07002058 ATRACE_CALL();
2059 ALOGV("%s", __FUNCTION__);
2060
Igor Murashkin4fb55c12013-08-29 17:43:01 -07002061 // Thread-safe. No lock necessary.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002062 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = mRemoteCallback;
Igor Murashkin4fb55c12013-08-29 17:43:01 -07002063 if (remoteCb != NULL) {
Shuzhen Wang5c22c152017-12-31 17:12:25 -08002064 remoteCb->onResultReceived(result.mMetadata, result.mResultExtras,
2065 result.mPhysicalMetadatas);
Igor Murashkine7ee7632013-06-11 18:10:18 -07002066 }
Emilian Peev538c90e2018-12-17 18:03:19 +00002067
Emilian Peev2f5d6012022-01-19 16:16:50 -08002068 // Access to the composite stream map must be synchronized
2069 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +00002070 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
2071 mCompositeStreamMap.valueAt(i)->onResultAvailable(result);
2072 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07002073}
2074
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002075binder::Status CameraDeviceClient::checkPidStatus(const char* checkLocation) {
Eino-Ville Talvala6192b892016-04-04 12:31:18 -07002076 if (mDisconnected) {
2077 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED,
2078 "The camera device has been disconnected");
2079 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002080 status_t res = checkPid(checkLocation);
2081 return (res == OK) ? binder::Status::ok() :
2082 STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
2083 "Attempt to use camera from a different process than original client");
2084}
2085
Igor Murashkine7ee7632013-06-11 18:10:18 -07002086// TODO: move to Camera2ClientBase
2087bool CameraDeviceClient::enforceRequestPermissions(CameraMetadata& metadata) {
2088
Jayant Chowdhary12361932018-08-27 14:46:13 -07002089 const int pid = CameraThreadState::getCallingPid();
Igor Murashkine7ee7632013-06-11 18:10:18 -07002090 const int selfPid = getpid();
2091 camera_metadata_entry_t entry;
2092
2093 /**
2094 * Mixin default important security values
2095 * - android.led.transmit = defaulted ON
2096 */
2097 CameraMetadata staticInfo = mDevice->info();
2098 entry = staticInfo.find(ANDROID_LED_AVAILABLE_LEDS);
2099 for(size_t i = 0; i < entry.count; ++i) {
2100 uint8_t led = entry.data.u8[i];
2101
2102 switch(led) {
2103 case ANDROID_LED_AVAILABLE_LEDS_TRANSMIT: {
2104 uint8_t transmitDefault = ANDROID_LED_TRANSMIT_ON;
2105 if (!metadata.exists(ANDROID_LED_TRANSMIT)) {
2106 metadata.update(ANDROID_LED_TRANSMIT,
2107 &transmitDefault, 1);
2108 }
2109 break;
2110 }
2111 }
2112 }
2113
2114 // We can do anything!
2115 if (pid == selfPid) {
2116 return true;
2117 }
2118
2119 /**
2120 * Permission check special fields in the request
2121 * - android.led.transmit = android.permission.CAMERA_DISABLE_TRANSMIT
2122 */
2123 entry = metadata.find(ANDROID_LED_TRANSMIT);
2124 if (entry.count > 0 && entry.data.u8[0] != ANDROID_LED_TRANSMIT_ON) {
2125 String16 permissionString =
2126 String16("android.permission.CAMERA_DISABLE_TRANSMIT_LED");
2127 if (!checkCallingPermission(permissionString)) {
Jayant Chowdhary12361932018-08-27 14:46:13 -07002128 const int uid = CameraThreadState::getCallingUid();
Igor Murashkine7ee7632013-06-11 18:10:18 -07002129 ALOGE("Permission Denial: "
2130 "can't disable transmit LED pid=%d, uid=%d", pid, uid);
2131 return false;
2132 }
2133 }
2134
2135 return true;
2136}
2137
Shuzhen Wang610d7b82022-02-08 14:37:22 -08002138status_t CameraDeviceClient::getRotationTransformLocked(int mirrorMode,
2139 int32_t* transform) {
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07002140 ALOGV("%s: begin", __FUNCTION__);
2141
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07002142 const CameraMetadata& staticInfo = mDevice->info();
Shuzhen Wang610d7b82022-02-08 14:37:22 -08002143 return CameraUtils::getRotationTransform(staticInfo, mirrorMode, transform);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07002144}
2145
Emilian Peevf4816702020-04-03 15:44:51 -07002146binder::Status CameraDeviceClient::mapRequestTemplate(int templateId,
2147 camera_request_template_t* tempId /*out*/) {
2148 binder::Status ret = binder::Status::ok();
2149
2150 if (tempId == nullptr) {
2151 ret = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
2152 "Camera %s: Invalid template argument", mCameraIdStr.string());
2153 return ret;
2154 }
2155 switch(templateId) {
2156 case ICameraDeviceUser::TEMPLATE_PREVIEW:
2157 *tempId = camera_request_template_t::CAMERA_TEMPLATE_PREVIEW;
2158 break;
2159 case ICameraDeviceUser::TEMPLATE_RECORD:
2160 *tempId = camera_request_template_t::CAMERA_TEMPLATE_VIDEO_RECORD;
2161 break;
2162 case ICameraDeviceUser::TEMPLATE_STILL_CAPTURE:
2163 *tempId = camera_request_template_t::CAMERA_TEMPLATE_STILL_CAPTURE;
2164 break;
2165 case ICameraDeviceUser::TEMPLATE_VIDEO_SNAPSHOT:
2166 *tempId = camera_request_template_t::CAMERA_TEMPLATE_VIDEO_SNAPSHOT;
2167 break;
2168 case ICameraDeviceUser::TEMPLATE_ZERO_SHUTTER_LAG:
2169 *tempId = camera_request_template_t::CAMERA_TEMPLATE_ZERO_SHUTTER_LAG;
2170 break;
2171 case ICameraDeviceUser::TEMPLATE_MANUAL:
2172 *tempId = camera_request_template_t::CAMERA_TEMPLATE_MANUAL;
2173 break;
2174 default:
2175 ret = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
2176 "Camera %s: Template ID %d is invalid or not supported",
2177 mCameraIdStr.string(), templateId);
2178 return ret;
2179 }
2180
2181 return ret;
2182}
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08002183
2184const CameraMetadata &CameraDeviceClient::getStaticInfo(const String8 &cameraId) {
2185 if (mDevice->getId() == cameraId) {
2186 return mDevice->info();
2187 }
2188 return mDevice->infoPhysical(cameraId);
2189}
2190
2191bool CameraDeviceClient::isUltraHighResolutionSensor(const String8 &cameraId) {
2192 const CameraMetadata &deviceInfo = getStaticInfo(cameraId);
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07002193 return SessionConfigurationUtils::isUltraHighResolutionSensor(deviceInfo);
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08002194}
2195
2196bool CameraDeviceClient::isSensorPixelModeConsistent(
2197 const std::list<int> &streamIdList, const CameraMetadata &settings) {
2198 // First we get the sensorPixelMode from the settings metadata.
2199 int32_t sensorPixelMode = ANDROID_SENSOR_PIXEL_MODE_DEFAULT;
2200 camera_metadata_ro_entry sensorPixelModeEntry = settings.find(ANDROID_SENSOR_PIXEL_MODE);
2201 if (sensorPixelModeEntry.count != 0) {
2202 sensorPixelMode = sensorPixelModeEntry.data.u8[0];
2203 if (sensorPixelMode != ANDROID_SENSOR_PIXEL_MODE_DEFAULT &&
2204 sensorPixelMode != ANDROID_SENSOR_PIXEL_MODE_MAXIMUM_RESOLUTION) {
2205 ALOGE("%s: Request sensor pixel mode not is not one of the valid values %d",
2206 __FUNCTION__, sensorPixelMode);
2207 return false;
2208 }
2209 }
2210 // Check whether each stream has max resolution allowed.
2211 bool consistent = true;
2212 for (auto it : streamIdList) {
2213 auto const streamInfoIt = mStreamInfoMap.find(it);
2214 if (streamInfoIt == mStreamInfoMap.end()) {
2215 ALOGE("%s: stream id %d not created, skipping", __FUNCTION__, it);
2216 return false;
2217 }
2218 consistent =
2219 streamInfoIt->second.sensorPixelModesUsed.find(sensorPixelMode) !=
2220 streamInfoIt->second.sensorPixelModesUsed.end();
2221 if (!consistent) {
2222 ALOGE("sensorPixelMode used %i not consistent with configured modes", sensorPixelMode);
2223 for (auto m : streamInfoIt->second.sensorPixelModesUsed) {
2224 ALOGE("sensor pixel mode used list: %i", m);
2225 }
2226 break;
2227 }
2228 }
2229
2230 return consistent;
2231}
2232
Igor Murashkine7ee7632013-06-11 18:10:18 -07002233} // namespace android