blob: bcba80eadc57b17a212fe5436e0068b9e5cb6edb [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 }
171 mDynamicProfileMap.emplace(
172 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD,
173 standardBitmap);
174 } else {
175 ALOGE("%s: Device %s supports 10-bit output but doesn't include a dynamic range"
176 " profile map!", __FUNCTION__, mCameraIdStr.c_str());
177 }
178 }
179 }
180
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700181 mProviderManager = providerPtr;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800182 // Cache physical camera ids corresponding to this device and also the high
183 // resolution sensors in this device + physical camera ids
184 mProviderManager->isLogicalCamera(mCameraIdStr.string(), &mPhysicalCameraIds);
185 if (isUltraHighResolutionSensor(mCameraIdStr)) {
186 mHighResolutionSensors.insert(mCameraIdStr.string());
187 }
188 for (auto &physicalId : mPhysicalCameraIds) {
189 if (isUltraHighResolutionSensor(String8(physicalId.c_str()))) {
190 mHighResolutionSensors.insert(physicalId.c_str());
191 }
192 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700193 return OK;
194}
195
196CameraDeviceClient::~CameraDeviceClient() {
197}
198
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800199binder::Status CameraDeviceClient::submitRequest(
200 const hardware::camera2::CaptureRequest& request,
201 bool streaming,
202 /*out*/
203 hardware::camera2::utils::SubmitInfo *submitInfo) {
204 std::vector<hardware::camera2::CaptureRequest> requestList = { request };
205 return submitRequestList(requestList, streaming, submitInfo);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700206}
207
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800208binder::Status CameraDeviceClient::insertGbpLocked(const sp<IGraphicBufferProducer>& gbp,
Emilian Peevf873aa52018-01-26 14:58:28 +0000209 SurfaceMap* outSurfaceMap, Vector<int32_t>* outputStreamIds, int32_t *currentStreamId) {
Emilian Peev538c90e2018-12-17 18:03:19 +0000210 int compositeIdx;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800211 int idx = mStreamMap.indexOfKey(IInterface::asBinder(gbp));
212
Emilian Peev2f5d6012022-01-19 16:16:50 -0800213 Mutex::Autolock l(mCompositeLock);
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800214 // Trying to submit request with surface that wasn't created
215 if (idx == NAME_NOT_FOUND) {
216 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
217 " we have not called createStream on",
218 __FUNCTION__, mCameraIdStr.string());
219 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
220 "Request targets Surface that is not part of current capture session");
Emilian Peev538c90e2018-12-17 18:03:19 +0000221 } else if ((compositeIdx = mCompositeStreamMap.indexOfKey(IInterface::asBinder(gbp)))
222 != NAME_NOT_FOUND) {
223 mCompositeStreamMap.valueAt(compositeIdx)->insertGbp(outSurfaceMap, outputStreamIds,
224 currentStreamId);
225 return binder::Status::ok();
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800226 }
227
228 const StreamSurfaceId& streamSurfaceId = mStreamMap.valueAt(idx);
229 if (outSurfaceMap->find(streamSurfaceId.streamId()) == outSurfaceMap->end()) {
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800230 outputStreamIds->push_back(streamSurfaceId.streamId());
231 }
232 (*outSurfaceMap)[streamSurfaceId.streamId()].push_back(streamSurfaceId.surfaceId());
233
234 ALOGV("%s: Camera %s: Appending output stream %d surface %d to request",
235 __FUNCTION__, mCameraIdStr.string(), streamSurfaceId.streamId(),
236 streamSurfaceId.surfaceId());
237
Emilian Peevf873aa52018-01-26 14:58:28 +0000238 if (currentStreamId != nullptr) {
239 *currentStreamId = streamSurfaceId.streamId();
240 }
241
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800242 return binder::Status::ok();
243}
244
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800245static std::list<int> getIntersection(const std::unordered_set<int> &streamIdsForThisCamera,
246 const Vector<int> &streamIdsForThisRequest) {
247 std::list<int> intersection;
248 for (auto &streamId : streamIdsForThisRequest) {
249 if (streamIdsForThisCamera.find(streamId) != streamIdsForThisCamera.end()) {
250 intersection.emplace_back(streamId);
251 }
252 }
253 return intersection;
254}
255
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800256binder::Status CameraDeviceClient::submitRequestList(
257 const std::vector<hardware::camera2::CaptureRequest>& requests,
258 bool streaming,
259 /*out*/
260 hardware::camera2::utils::SubmitInfo *submitInfo) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700261 ATRACE_CALL();
Mark Salyzyn50468412014-06-18 16:33:43 -0700262 ALOGV("%s-start of function. Request list size %zu", __FUNCTION__, requests.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700263
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800264 binder::Status res = binder::Status::ok();
265 status_t err;
266 if ( !(res = checkPidStatus(__FUNCTION__) ).isOk()) {
267 return res;
268 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700269
270 Mutex::Autolock icl(mBinderSerializationLock);
271
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800272 if (!mDevice.get()) {
273 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
274 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700275
276 if (requests.empty()) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800277 ALOGE("%s: Camera %s: Sent null request. Rejecting request.",
278 __FUNCTION__, mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800279 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Empty request list");
Jianing Wei90e59c92014-03-12 18:29:36 -0700280 }
281
Emilian Peevaebbe412018-01-15 13:53:24 +0000282 List<const CameraDeviceBase::PhysicalCameraSettingsList> metadataRequestList;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700283 std::list<const SurfaceMap> surfaceMapList;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800284 submitInfo->mRequestId = mRequestIdCounter;
Jianing Wei90e59c92014-03-12 18:29:36 -0700285 uint32_t loopCounter = 0;
286
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800287 for (auto&& request: requests) {
288 if (request.mIsReprocess) {
Chien-Yu Chened0412e2015-04-27 15:04:22 -0700289 if (!mInputStream.configured) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800290 ALOGE("%s: Camera %s: no input stream is configured.", __FUNCTION__,
291 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800292 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800293 "No input configured for camera %s but request is for reprocessing",
294 mCameraIdStr.string());
Chien-Yu Chened0412e2015-04-27 15:04:22 -0700295 } else if (streaming) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800296 ALOGE("%s: Camera %s: streaming reprocess requests not supported.", __FUNCTION__,
297 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800298 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
299 "Repeating reprocess requests not supported");
Emilian Peevaebbe412018-01-15 13:53:24 +0000300 } else if (request.mPhysicalCameraSettings.size() > 1) {
301 ALOGE("%s: Camera %s: reprocess requests not supported for "
302 "multiple physical cameras.", __FUNCTION__,
303 mCameraIdStr.string());
304 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
305 "Reprocess requests not supported for multiple cameras");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700306 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700307 }
308
Emilian Peevaebbe412018-01-15 13:53:24 +0000309 if (request.mPhysicalCameraSettings.empty()) {
310 ALOGE("%s: Camera %s: request doesn't contain any settings.", __FUNCTION__,
311 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800312 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
Emilian Peevaebbe412018-01-15 13:53:24 +0000313 "Request doesn't contain any settings");
314 }
315
316 //The first capture settings should always match the logical camera id
317 String8 logicalId(request.mPhysicalCameraSettings.begin()->id.c_str());
318 if (mDevice->getId() != logicalId) {
319 ALOGE("%s: Camera %s: Invalid camera request settings.", __FUNCTION__,
320 mCameraIdStr.string());
321 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
322 "Invalid camera request settings");
323 }
324
Emilian Peevaebbe412018-01-15 13:53:24 +0000325 if (request.mSurfaceList.isEmpty() && request.mStreamIdxList.size() == 0) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800326 ALOGE("%s: Camera %s: Requests must have at least one surface target. "
327 "Rejecting request.", __FUNCTION__, mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800328 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
329 "Request has no output targets");
Jianing Wei90e59c92014-03-12 18:29:36 -0700330 }
331
Jianing Wei90e59c92014-03-12 18:29:36 -0700332 /**
Shuzhen Wang0129d522016-10-30 22:43:41 -0700333 * Write in the output stream IDs and map from stream ID to surface ID
334 * which we calculate from the capture request's list of surface target
Jianing Wei90e59c92014-03-12 18:29:36 -0700335 */
Shuzhen Wang0129d522016-10-30 22:43:41 -0700336 SurfaceMap surfaceMap;
Jianing Wei90e59c92014-03-12 18:29:36 -0700337 Vector<int32_t> outputStreamIds;
Emilian Peevf873aa52018-01-26 14:58:28 +0000338 std::vector<std::string> requestedPhysicalIds;
Emilian Peevc81a7592022-02-14 17:38:18 -0800339 int64_t dynamicProfileBitmap = 0;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800340 if (request.mSurfaceList.size() > 0) {
Chih-Hung Hsieh3ef324d2018-12-11 11:48:12 -0800341 for (const sp<Surface>& surface : request.mSurfaceList) {
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800342 if (surface == 0) continue;
Jianing Wei90e59c92014-03-12 18:29:36 -0700343
Emilian Peevf873aa52018-01-26 14:58:28 +0000344 int32_t streamId;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800345 sp<IGraphicBufferProducer> gbp = surface->getIGraphicBufferProducer();
Emilian Peevf873aa52018-01-26 14:58:28 +0000346 res = insertGbpLocked(gbp, &surfaceMap, &outputStreamIds, &streamId);
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800347 if (!res.isOk()) {
348 return res;
349 }
Emilian Peevf873aa52018-01-26 14:58:28 +0000350
351 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
352 if (index >= 0) {
353 String8 requestedPhysicalId(
354 mConfiguredOutputs.valueAt(index).getPhysicalCameraId());
355 requestedPhysicalIds.push_back(requestedPhysicalId.string());
Emilian Peev2295df72021-11-12 18:14:10 -0800356 dynamicProfileBitmap |=
357 mConfiguredOutputs.valueAt(index).getDynamicRangeProfile();
Emilian Peevf873aa52018-01-26 14:58:28 +0000358 } else {
359 ALOGW("%s: Output stream Id not found among configured outputs!", __FUNCTION__);
360 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700361 }
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800362 } else {
363 for (size_t i = 0; i < request.mStreamIdxList.size(); i++) {
364 int streamId = request.mStreamIdxList.itemAt(i);
365 int surfaceIdx = request.mSurfaceIdxList.itemAt(i);
Jianing Wei90e59c92014-03-12 18:29:36 -0700366
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800367 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
368 if (index < 0) {
369 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
370 " we have not called createStream on: stream %d",
371 __FUNCTION__, mCameraIdStr.string(), streamId);
372 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
373 "Request targets Surface that is not part of current capture session");
374 }
375
376 const auto& gbps = mConfiguredOutputs.valueAt(index).getGraphicBufferProducers();
377 if ((size_t)surfaceIdx >= gbps.size()) {
378 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
379 " we have not called createStream on: stream %d, surfaceIdx %d",
380 __FUNCTION__, mCameraIdStr.string(), streamId, surfaceIdx);
381 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
382 "Request targets Surface has invalid surface index");
383 }
384
Emilian Peevf873aa52018-01-26 14:58:28 +0000385 res = insertGbpLocked(gbps[surfaceIdx], &surfaceMap, &outputStreamIds, nullptr);
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800386 if (!res.isOk()) {
387 return res;
388 }
Emilian Peevf873aa52018-01-26 14:58:28 +0000389
390 String8 requestedPhysicalId(
391 mConfiguredOutputs.valueAt(index).getPhysicalCameraId());
392 requestedPhysicalIds.push_back(requestedPhysicalId.string());
Emilian Peev2295df72021-11-12 18:14:10 -0800393 dynamicProfileBitmap |=
394 mConfiguredOutputs.valueAt(index).getDynamicRangeProfile();
395 }
396 }
397
398 if (dynamicProfileBitmap !=
399 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD) {
400 for (int i = ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD;
401 i < ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_MAX; i <<= 1) {
402 if ((dynamicProfileBitmap & i) == 0) {
403 continue;
404 }
405
406 const auto& it = mDynamicProfileMap.find(i);
407 if (it != mDynamicProfileMap.end()) {
408 if ((it->second == 0) ||
409 ((it->second & dynamicProfileBitmap) == dynamicProfileBitmap)) {
410 continue;
411 } else {
412 ALOGE("%s: Camera %s: Tried to submit a request with a surfaces that"
413 " reference an unsupported dynamic range profile combination"
Emilian Peevc81a7592022-02-14 17:38:18 -0800414 " 0x%" PRIx64 "!", __FUNCTION__, mCameraIdStr.string(),
Emilian Peev2295df72021-11-12 18:14:10 -0800415 dynamicProfileBitmap);
416 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
417 "Request targets an unsupported dynamic range profile"
418 " combination");
419 }
420 } else {
421 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
422 " references unsupported dynamic range profile 0x%x!",
423 __FUNCTION__, mCameraIdStr.string(), i);
424 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
425 "Request targets 10-bit Surface with unsupported dynamic range"
426 " profile");
427 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700428 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700429 }
430
Emilian Peevf873aa52018-01-26 14:58:28 +0000431 CameraDeviceBase::PhysicalCameraSettingsList physicalSettingsList;
432 for (const auto& it : request.mPhysicalCameraSettings) {
Emilian Peev00420d22018-02-05 21:33:13 +0000433 if (it.settings.isEmpty()) {
434 ALOGE("%s: Camera %s: Sent empty metadata packet. Rejecting request.",
435 __FUNCTION__, mCameraIdStr.string());
436 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
437 "Request settings are empty");
438 }
439
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800440 // Check whether the physical / logical stream has settings
441 // consistent with the sensor pixel mode(s) it was configured with.
442 // mCameraIdToStreamSet will only have ids that are high resolution
443 const auto streamIdSetIt = mHighResolutionCameraIdToStreamIdSet.find(it.id);
444 if (streamIdSetIt != mHighResolutionCameraIdToStreamIdSet.end()) {
445 std::list<int> streamIdsUsedInRequest = getIntersection(streamIdSetIt->second,
446 outputStreamIds);
447 if (!request.mIsReprocess &&
448 !isSensorPixelModeConsistent(streamIdsUsedInRequest, it.settings)) {
449 ALOGE("%s: Camera %s: Request settings CONTROL_SENSOR_PIXEL_MODE not "
450 "consistent with configured streams. Rejecting request.",
451 __FUNCTION__, it.id.c_str());
452 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
453 "Request settings CONTROL_SENSOR_PIXEL_MODE are not consistent with "
454 "streams configured");
455 }
456 }
457
Emilian Peevf873aa52018-01-26 14:58:28 +0000458 String8 physicalId(it.id.c_str());
Shuzhen Wang911c6a32021-10-27 13:36:03 -0700459 bool hasTestPatternModePhysicalKey = std::find(mSupportedPhysicalRequestKeys.begin(),
460 mSupportedPhysicalRequestKeys.end(), ANDROID_SENSOR_TEST_PATTERN_MODE) !=
461 mSupportedPhysicalRequestKeys.end();
462 bool hasTestPatternDataPhysicalKey = std::find(mSupportedPhysicalRequestKeys.begin(),
463 mSupportedPhysicalRequestKeys.end(), ANDROID_SENSOR_TEST_PATTERN_DATA) !=
464 mSupportedPhysicalRequestKeys.end();
Emilian Peevf873aa52018-01-26 14:58:28 +0000465 if (physicalId != mDevice->getId()) {
466 auto found = std::find(requestedPhysicalIds.begin(), requestedPhysicalIds.end(),
467 it.id);
468 if (found == requestedPhysicalIds.end()) {
469 ALOGE("%s: Camera %s: Physical camera id: %s not part of attached outputs.",
470 __FUNCTION__, mCameraIdStr.string(), physicalId.string());
471 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
472 "Invalid physical camera id");
473 }
Emilian Peev00420d22018-02-05 21:33:13 +0000474
475 if (!mSupportedPhysicalRequestKeys.empty()) {
476 // Filter out any unsupported physical request keys.
477 CameraMetadata filteredParams(mSupportedPhysicalRequestKeys.size());
478 camera_metadata_t *meta = const_cast<camera_metadata_t *>(
479 filteredParams.getAndLock());
480 set_camera_metadata_vendor_id(meta, mDevice->getVendorTagId());
481 filteredParams.unlock(meta);
482
483 for (const auto& keyIt : mSupportedPhysicalRequestKeys) {
484 camera_metadata_ro_entry entry = it.settings.find(keyIt);
485 if (entry.count > 0) {
486 filteredParams.update(entry);
487 }
488 }
489
Shuzhen Wang911c6a32021-10-27 13:36:03 -0700490 physicalSettingsList.push_back({it.id, filteredParams,
491 hasTestPatternModePhysicalKey, hasTestPatternDataPhysicalKey});
Emilian Peev00420d22018-02-05 21:33:13 +0000492 }
493 } else {
494 physicalSettingsList.push_back({it.id, it.settings});
Emilian Peevf873aa52018-01-26 14:58:28 +0000495 }
Emilian Peevf873aa52018-01-26 14:58:28 +0000496 }
497
498 if (!enforceRequestPermissions(physicalSettingsList.begin()->metadata)) {
499 // Callee logs
500 return STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
501 "Caller does not have permission to change restricted controls");
502 }
503
Emilian Peevaebbe412018-01-15 13:53:24 +0000504 physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_OUTPUT_STREAMS,
505 &outputStreamIds[0], outputStreamIds.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700506
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800507 if (request.mIsReprocess) {
Emilian Peevaebbe412018-01-15 13:53:24 +0000508 physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_INPUT_STREAMS,
509 &mInputStream.id, 1);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700510 }
511
Emilian Peevaebbe412018-01-15 13:53:24 +0000512 physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_ID,
513 &(submitInfo->mRequestId), /*size*/1);
Jianing Wei90e59c92014-03-12 18:29:36 -0700514 loopCounter++; // loopCounter starts from 1
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800515 ALOGV("%s: Camera %s: Creating request with ID %d (%d of %zu)",
516 __FUNCTION__, mCameraIdStr.string(), submitInfo->mRequestId,
517 loopCounter, requests.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700518
Emilian Peevaebbe412018-01-15 13:53:24 +0000519 metadataRequestList.push_back(physicalSettingsList);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700520 surfaceMapList.push_back(surfaceMap);
Jianing Wei90e59c92014-03-12 18:29:36 -0700521 }
522 mRequestIdCounter++;
523
524 if (streaming) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700525 err = mDevice->setStreamingRequestList(metadataRequestList, surfaceMapList,
526 &(submitInfo->mLastFrameNumber));
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800527 if (err != OK) {
528 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800529 "Camera %s: Got error %s (%d) after trying to set streaming request",
530 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800531 ALOGE("%s: %s", __FUNCTION__, msg.string());
532 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
533 msg.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700534 } else {
Shuzhen Wangc9ca6782016-04-26 13:40:31 -0700535 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700536 mStreamingRequestId = submitInfo->mRequestId;
Jianing Wei90e59c92014-03-12 18:29:36 -0700537 }
538 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700539 err = mDevice->captureList(metadataRequestList, surfaceMapList,
540 &(submitInfo->mLastFrameNumber));
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800541 if (err != OK) {
542 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800543 "Camera %s: Got error %s (%d) after trying to submit capture request",
544 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800545 ALOGE("%s: %s", __FUNCTION__, msg.string());
546 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
547 msg.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700548 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800549 ALOGV("%s: requestId = %d ", __FUNCTION__, submitInfo->mRequestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700550 }
551
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800552 ALOGV("%s: Camera %s: End of function", __FUNCTION__, mCameraIdStr.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700553 return res;
554}
555
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800556binder::Status CameraDeviceClient::cancelRequest(
557 int requestId,
558 /*out*/
559 int64_t* lastFrameNumber) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700560 ATRACE_CALL();
561 ALOGV("%s, requestId = %d", __FUNCTION__, requestId);
562
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800563 status_t err;
564 binder::Status res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700565
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800566 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700567
568 Mutex::Autolock icl(mBinderSerializationLock);
569
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800570 if (!mDevice.get()) {
571 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
572 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700573
Shuzhen Wangc9ca6782016-04-26 13:40:31 -0700574 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700575 if (mStreamingRequestId != requestId) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800576 String8 msg = String8::format("Camera %s: Canceling request ID %d doesn't match "
577 "current request ID %d", mCameraIdStr.string(), requestId, mStreamingRequestId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800578 ALOGE("%s: %s", __FUNCTION__, msg.string());
579 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700580 }
581
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800582 err = mDevice->clearStreamingRequest(lastFrameNumber);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700583
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800584 if (err == OK) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800585 ALOGV("%s: Camera %s: Successfully cleared streaming request",
586 __FUNCTION__, mCameraIdStr.string());
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700587 mStreamingRequestId = REQUEST_ID_NONE;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800588 } else {
589 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800590 "Camera %s: Error clearing streaming request: %s (%d)",
591 mCameraIdStr.string(), strerror(-err), err);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700592 }
593
594 return res;
595}
596
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800597binder::Status CameraDeviceClient::beginConfigure() {
Ruben Brunkb2119af2014-05-09 19:57:56 -0700598 // TODO: Implement this.
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -0700599 ATRACE_CALL();
Zhijun He1fa89992015-06-01 15:44:31 -0700600 ALOGV("%s: Not implemented yet.", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800601 return binder::Status::ok();
Ruben Brunkb2119af2014-05-09 19:57:56 -0700602}
603
Emilian Peev5fbe0ba2017-10-20 15:45:45 +0100604binder::Status CameraDeviceClient::endConfigure(int operatingMode,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700605 const hardware::camera2::impl::CameraMetadataNative& sessionParams, int64_t startTimeMs,
Emilian Peevcc0b7952020-01-07 13:54:47 -0800606 std::vector<int>* offlineStreamIds /*out*/) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -0700607 ATRACE_CALL();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700608 ALOGV("%s: ending configure (%d input stream, %zu output surfaces)",
609 __FUNCTION__, mInputStream.configured ? 1 : 0,
610 mStreamMap.size());
Igor Murashkine2d167e2014-08-19 16:19:59 -0700611
Zhijun He0effd522016-03-04 10:22:27 -0800612 binder::Status res;
613 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
614
Emilian Peevcc0b7952020-01-07 13:54:47 -0800615 if (offlineStreamIds == nullptr) {
616 String8 msg = String8::format("Invalid offline stream ids");
617 ALOGE("%s: %s", __FUNCTION__, msg.string());
618 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
619 }
620
Zhijun He0effd522016-03-04 10:22:27 -0800621 Mutex::Autolock icl(mBinderSerializationLock);
622
623 if (!mDevice.get()) {
624 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
625 }
626
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700627 res = SessionConfigurationUtils::checkOperatingMode(operatingMode, mDevice->info(),
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000628 mCameraIdStr);
Emilian Peev35ae8262018-11-08 13:11:32 +0000629 if (!res.isOk()) {
630 return res;
631 }
632
633 status_t err = mDevice->configureStreams(sessionParams, operatingMode);
634 if (err == BAD_VALUE) {
635 String8 msg = String8::format("Camera %s: Unsupported set of inputs/outputs provided",
636 mCameraIdStr.string());
637 ALOGE("%s: %s", __FUNCTION__, msg.string());
638 res = STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
639 } else if (err != OK) {
640 String8 msg = String8::format("Camera %s: Error configuring streams: %s (%d)",
641 mCameraIdStr.string(), strerror(-err), err);
642 ALOGE("%s: %s", __FUNCTION__, msg.string());
643 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Emilian Peev538c90e2018-12-17 18:03:19 +0000644 } else {
Emilian Peevcc0b7952020-01-07 13:54:47 -0800645 offlineStreamIds->clear();
646 mDevice->getOfflineStreamIds(offlineStreamIds);
647
Emilian Peev2f5d6012022-01-19 16:16:50 -0800648 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +0000649 for (size_t i = 0; i < mCompositeStreamMap.size(); ++i) {
650 err = mCompositeStreamMap.valueAt(i)->configureStream();
Emilian Peevcc0b7952020-01-07 13:54:47 -0800651 if (err != OK) {
Emilian Peev538c90e2018-12-17 18:03:19 +0000652 String8 msg = String8::format("Camera %s: Error configuring composite "
653 "streams: %s (%d)", mCameraIdStr.string(), strerror(-err), err);
654 ALOGE("%s: %s", __FUNCTION__, msg.string());
655 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
656 break;
657 }
Emilian Peevcc0b7952020-01-07 13:54:47 -0800658
659 // Composite streams can only support offline mode in case all individual internal
660 // streams are also supported.
661 std::vector<int> internalStreams;
662 mCompositeStreamMap.valueAt(i)->insertCompositeStreamIds(&internalStreams);
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800663 offlineStreamIds->erase(
664 std::remove_if(offlineStreamIds->begin(), offlineStreamIds->end(),
Emilian Peevcc0b7952020-01-07 13:54:47 -0800665 [&internalStreams] (int streamId) {
666 auto it = std::find(internalStreams.begin(), internalStreams.end(),
667 streamId);
668 if (it != internalStreams.end()) {
669 internalStreams.erase(it);
670 return true;
671 }
672
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800673 return false;}), offlineStreamIds->end());
Emilian Peevcc0b7952020-01-07 13:54:47 -0800674 if (internalStreams.empty()) {
675 offlineStreamIds->push_back(mCompositeStreamMap.valueAt(i)->getStreamId());
676 }
677 }
678
679 for (const auto& offlineStreamId : *offlineStreamIds) {
680 mStreamInfoMap[offlineStreamId].supportsOffline = true;
Emilian Peev538c90e2018-12-17 18:03:19 +0000681 }
Shuzhen Wang316781a2020-08-18 18:11:01 -0700682
683 nsecs_t configureEnd = systemTime();
684 int32_t configureDurationMs = ns2ms(configureEnd) - startTimeMs;
685 CameraServiceProxyWrapper::logStreamConfigured(mCameraIdStr, operatingMode,
686 false /*internalReconfig*/, configureDurationMs);
Emilian Peev35ae8262018-11-08 13:11:32 +0000687 }
688
689 return res;
690}
691
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800692binder::Status CameraDeviceClient::isSessionConfigurationSupported(
693 const SessionConfiguration& sessionConfiguration, bool *status /*out*/) {
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800694
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800695 ATRACE_CALL();
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800696 binder::Status res;
697 status_t ret = OK;
698 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
699
700 Mutex::Autolock icl(mBinderSerializationLock);
701
702 if (!mDevice.get()) {
703 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
704 }
705
706 auto operatingMode = sessionConfiguration.getOperatingMode();
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700707 res = SessionConfigurationUtils::checkOperatingMode(operatingMode, mDevice->info(),
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000708 mCameraIdStr);
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800709 if (!res.isOk()) {
710 return res;
711 }
712
713 if (status == nullptr) {
714 String8 msg = String8::format( "Camera %s: Invalid status!", mCameraIdStr.string());
715 ALOGE("%s: %s", __FUNCTION__, msg.string());
716 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
717 }
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700718
Emilian Peev35ae8262018-11-08 13:11:32 +0000719 *status = false;
720 ret = mProviderManager->isSessionConfigurationSupported(mCameraIdStr.string(),
Jayant Chowdhary0bd38522021-11-05 17:49:27 -0700721 sessionConfiguration, mOverrideForPerfClass, status);
Emilian Peev35ae8262018-11-08 13:11:32 +0000722 switch (ret) {
723 case OK:
724 // Expected, do nothing.
725 break;
726 case INVALID_OPERATION: {
727 String8 msg = String8::format(
728 "Camera %s: Session configuration query not supported!",
729 mCameraIdStr.string());
730 ALOGD("%s: %s", __FUNCTION__, msg.string());
731 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
732 }
733
734 break;
735 default: {
736 String8 msg = String8::format( "Camera %s: Error: %s (%d)", mCameraIdStr.string(),
737 strerror(-ret), ret);
738 ALOGE("%s: %s", __FUNCTION__, msg.string());
739 res = STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
740 msg.string());
741 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800742 }
743
744 return res;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700745}
746
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800747binder::Status CameraDeviceClient::deleteStream(int streamId) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700748 ATRACE_CALL();
749 ALOGV("%s (streamId = 0x%x)", __FUNCTION__, streamId);
750
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800751 binder::Status res;
752 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700753
754 Mutex::Autolock icl(mBinderSerializationLock);
755
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800756 if (!mDevice.get()) {
757 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
758 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700759
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700760 bool isInput = false;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700761 std::vector<sp<IBinder>> surfaces;
Zhijun He5d677d12016-05-29 16:52:39 -0700762 ssize_t dIndex = NAME_NOT_FOUND;
Emilian Peev538c90e2018-12-17 18:03:19 +0000763 ssize_t compositeIndex = NAME_NOT_FOUND;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700764
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700765 if (mInputStream.configured && mInputStream.id == streamId) {
766 isInput = true;
767 } else {
768 // Guard against trying to delete non-created streams
769 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700770 if (streamId == mStreamMap.valueAt(i).streamId()) {
771 surfaces.push_back(mStreamMap.keyAt(i));
772 }
773 }
774
775 // See if this stream is one of the deferred streams.
776 for (size_t i = 0; i < mDeferredStreams.size(); ++i) {
777 if (streamId == mDeferredStreams[i]) {
778 dIndex = i;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700779 break;
780 }
781 }
782
Emilian Peev2f5d6012022-01-19 16:16:50 -0800783 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +0000784 for (size_t i = 0; i < mCompositeStreamMap.size(); ++i) {
785 if (streamId == mCompositeStreamMap.valueAt(i)->getStreamId()) {
786 compositeIndex = i;
787 break;
788 }
789 }
790
Shuzhen Wang0129d522016-10-30 22:43:41 -0700791 if (surfaces.empty() && dIndex == NAME_NOT_FOUND) {
792 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no such"
793 " stream created yet", mCameraIdStr.string(), streamId);
794 ALOGW("%s: %s", __FUNCTION__, msg.string());
795 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700796 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700797 }
798
799 // Also returns BAD_VALUE if stream ID was not valid
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800800 status_t err = mDevice->deleteStream(streamId);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700801
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800802 if (err != OK) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800803 String8 msg = String8::format("Camera %s: Unexpected error %s (%d) when deleting stream %d",
804 mCameraIdStr.string(), strerror(-err), err, streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800805 ALOGE("%s: %s", __FUNCTION__, msg.string());
806 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
807 } else {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700808 if (isInput) {
809 mInputStream.configured = false;
Zhijun He5d677d12016-05-29 16:52:39 -0700810 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700811 for (auto& surface : surfaces) {
812 mStreamMap.removeItem(surface);
813 }
814
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800815 mConfiguredOutputs.removeItem(streamId);
816
Shuzhen Wang0129d522016-10-30 22:43:41 -0700817 if (dIndex != NAME_NOT_FOUND) {
818 mDeferredStreams.removeItemsAt(dIndex);
819 }
Emilian Peev538c90e2018-12-17 18:03:19 +0000820
821 if (compositeIndex != NAME_NOT_FOUND) {
Emilian Peev2f5d6012022-01-19 16:16:50 -0800822 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +0000823 status_t ret;
824 if ((ret = mCompositeStreamMap.valueAt(compositeIndex)->deleteStream())
825 != OK) {
826 String8 msg = String8::format("Camera %s: Unexpected error %s (%d) when "
827 "deleting composite stream %d", mCameraIdStr.string(), strerror(-err), err,
828 streamId);
829 ALOGE("%s: %s", __FUNCTION__, msg.string());
830 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
831 }
832 mCompositeStreamMap.removeItemsAt(compositeIndex);
833 }
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800834 for (auto &mapIt: mHighResolutionCameraIdToStreamIdSet) {
835 auto &streamSet = mapIt.second;
836 if (streamSet.find(streamId) != streamSet.end()) {
837 streamSet.erase(streamId);
838 break;
839 }
840 }
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700841 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700842 }
843
844 return res;
845}
846
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800847binder::Status CameraDeviceClient::createStream(
848 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
849 /*out*/
850 int32_t* newStreamId) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700851 ATRACE_CALL();
Igor Murashkine7ee7632013-06-11 18:10:18 -0700852
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800853 binder::Status res;
854 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700855
856 Mutex::Autolock icl(mBinderSerializationLock);
857
Shuzhen Wang0129d522016-10-30 22:43:41 -0700858 const std::vector<sp<IGraphicBufferProducer>>& bufferProducers =
859 outputConfiguration.getGraphicBufferProducers();
860 size_t numBufferProducers = bufferProducers.size();
Shuzhen Wang758c2152017-01-10 18:26:18 -0800861 bool deferredConsumer = outputConfiguration.isDeferred();
862 bool isShared = outputConfiguration.isShared();
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800863 String8 physicalCameraId = String8(outputConfiguration.getPhysicalCameraId());
Shuzhen Wang758c2152017-01-10 18:26:18 -0800864 bool deferredConsumerOnly = deferredConsumer && numBufferProducers == 0;
Shuzhen Wang83bff122020-11-20 15:51:39 -0800865 bool isMultiResolution = outputConfiguration.isMultiResolution();
Emilian Peevc81a7592022-02-14 17:38:18 -0800866 int64_t dynamicRangeProfile = outputConfiguration.getDynamicRangeProfile();
Shuzhen Wangc8ab4522021-12-14 20:12:42 -0800867 int streamUseCase = outputConfiguration.getStreamUseCase();
Shuzhen Wange4208922022-02-01 16:52:48 -0800868 int timestampBase = outputConfiguration.getTimestampBase();
Shuzhen Wang610d7b82022-02-08 14:37:22 -0800869 int mirrorMode = outputConfiguration.getMirrorMode();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700870
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700871 res = SessionConfigurationUtils::checkSurfaceType(numBufferProducers, deferredConsumer,
Emilian Peev35ae8262018-11-08 13:11:32 +0000872 outputConfiguration.getSurfaceType());
873 if (!res.isOk()) {
874 return res;
Yin-Chia Yeh89f14da2014-06-10 16:05:44 -0700875 }
Zhijun He5d677d12016-05-29 16:52:39 -0700876
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800877 if (!mDevice.get()) {
878 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
879 }
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700880 res = SessionConfigurationUtils::checkPhysicalCameraId(mPhysicalCameraIds,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800881 physicalCameraId, mCameraIdStr);
Emilian Peev35ae8262018-11-08 13:11:32 +0000882 if (!res.isOk()) {
883 return res;
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800884 }
Emilian Peev35ae8262018-11-08 13:11:32 +0000885
Shuzhen Wang0129d522016-10-30 22:43:41 -0700886 std::vector<sp<Surface>> surfaces;
887 std::vector<sp<IBinder>> binders;
Zhijun He5d677d12016-05-29 16:52:39 -0700888 status_t err;
889
890 // Create stream for deferred surface case.
Shuzhen Wang0129d522016-10-30 22:43:41 -0700891 if (deferredConsumerOnly) {
Shuzhen Wang758c2152017-01-10 18:26:18 -0800892 return createDeferredSurfaceStreamLocked(outputConfiguration, isShared, newStreamId);
Zhijun He5d677d12016-05-29 16:52:39 -0700893 }
894
Shuzhen Wang758c2152017-01-10 18:26:18 -0800895 OutputStreamInfo streamInfo;
896 bool isStreamInfoValid = false;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800897 const std::vector<int32_t> &sensorPixelModesUsed =
898 outputConfiguration.getSensorPixelModesUsed();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700899 for (auto& bufferProducer : bufferProducers) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700900 // Don't create multiple streams for the same target surface
Shuzhen Wang0129d522016-10-30 22:43:41 -0700901 sp<IBinder> binder = IInterface::asBinder(bufferProducer);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800902 ssize_t index = mStreamMap.indexOfKey(binder);
903 if (index != NAME_NOT_FOUND) {
904 String8 msg = String8::format("Camera %s: Surface already has a stream created for it "
905 "(ID %zd)", mCameraIdStr.string(), index);
906 ALOGW("%s: %s", __FUNCTION__, msg.string());
907 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
Shuzhen Wang0129d522016-10-30 22:43:41 -0700908 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700909
Shuzhen Wang758c2152017-01-10 18:26:18 -0800910 sp<Surface> surface;
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700911 res = SessionConfigurationUtils::createSurfaceFromGbp(streamInfo,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800912 isStreamInfoValid, surface, bufferProducer, mCameraIdStr,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -0800913 mDevice->infoPhysical(physicalCameraId), sensorPixelModesUsed, dynamicRangeProfile,
Shuzhen Wang610d7b82022-02-08 14:37:22 -0800914 streamUseCase, timestampBase, mirrorMode);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800915
916 if (!res.isOk())
917 return res;
918
919 if (!isStreamInfoValid) {
920 isStreamInfoValid = true;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700921 }
922
Shuzhen Wang758c2152017-01-10 18:26:18 -0800923 binders.push_back(IInterface::asBinder(bufferProducer));
Shuzhen Wang0129d522016-10-30 22:43:41 -0700924 surfaces.push_back(surface);
Ruben Brunkbba75572014-11-20 17:29:50 -0800925 }
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700926
927 // If mOverrideForPerfClass is true, do not fail createStream() for small
928 // JPEG sizes because existing createSurfaceFromGbp() logic will find the
929 // closest possible supported size.
930
Zhijun He125684a2015-12-26 15:07:30 -0800931 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
Emilian Peev40ead602017-09-26 15:46:36 +0100932 std::vector<int> surfaceIds;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800933 bool isDepthCompositeStream =
934 camera3::DepthCompositeStream::isDepthCompositeStream(surfaces[0]);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800935 bool isHeicCompisiteStream = camera3::HeicCompositeStream::isHeicCompositeStream(surfaces[0]);
936 if (isDepthCompositeStream || isHeicCompisiteStream) {
937 sp<CompositeStream> compositeStream;
938 if (isDepthCompositeStream) {
939 compositeStream = new camera3::DepthCompositeStream(mDevice, getRemoteCallback());
940 } else {
941 compositeStream = new camera3::HeicCompositeStream(mDevice, getRemoteCallback());
942 }
943
Emilian Peev538c90e2018-12-17 18:03:19 +0000944 err = compositeStream->createStream(surfaces, deferredConsumer, streamInfo.width,
945 streamInfo.height, streamInfo.format,
Emilian Peevf4816702020-04-03 15:44:51 -0700946 static_cast<camera_stream_rotation_t>(outputConfiguration.getRotation()),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800947 &streamId, physicalCameraId, streamInfo.sensorPixelModesUsed, &surfaceIds,
948 outputConfiguration.getSurfaceSetID(), isShared, isMultiResolution);
Emilian Peev538c90e2018-12-17 18:03:19 +0000949 if (err == OK) {
Emilian Peev2f5d6012022-01-19 16:16:50 -0800950 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +0000951 mCompositeStreamMap.add(IInterface::asBinder(surfaces[0]->getIGraphicBufferProducer()),
952 compositeStream);
953 }
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800954 } else {
955 err = mDevice->createStream(surfaces, deferredConsumer, streamInfo.width,
956 streamInfo.height, streamInfo.format, streamInfo.dataSpace,
Emilian Peevf4816702020-04-03 15:44:51 -0700957 static_cast<camera_stream_rotation_t>(outputConfiguration.getRotation()),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800958 &streamId, physicalCameraId, streamInfo.sensorPixelModesUsed, &surfaceIds,
Emilian Peev2295df72021-11-12 18:14:10 -0800959 outputConfiguration.getSurfaceSetID(), isShared, isMultiResolution,
Shuzhen Wange4208922022-02-01 16:52:48 -0800960 /*consumerUsage*/0, streamInfo.dynamicRangeProfile, streamInfo.streamUseCase,
Shuzhen Wang610d7b82022-02-08 14:37:22 -0800961 streamInfo.timestampBase, streamInfo.mirrorMode);
Emilian Peev538c90e2018-12-17 18:03:19 +0000962 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700963
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800964 if (err != OK) {
965 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800966 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
Shuzhen Wang758c2152017-01-10 18:26:18 -0800967 mCameraIdStr.string(), streamInfo.width, streamInfo.height, streamInfo.format,
968 streamInfo.dataSpace, strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800969 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700970 int i = 0;
971 for (auto& binder : binders) {
972 ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %d",
973 __FUNCTION__, binder.get(), streamId, i);
Emilian Peev40ead602017-09-26 15:46:36 +0100974 mStreamMap.add(binder, StreamSurfaceId(streamId, surfaceIds[i]));
975 i++;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700976 }
Shuzhen Wang758c2152017-01-10 18:26:18 -0800977
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800978 mConfiguredOutputs.add(streamId, outputConfiguration);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800979 mStreamInfoMap[streamId] = streamInfo;
980
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800981 ALOGV("%s: Camera %s: Successfully created a new stream ID %d for output surface"
Shuzhen Wang0129d522016-10-30 22:43:41 -0700982 " (%d x %d) with format 0x%x.",
Shuzhen Wang758c2152017-01-10 18:26:18 -0800983 __FUNCTION__, mCameraIdStr.string(), streamId, streamInfo.width,
984 streamInfo.height, streamInfo.format);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700985
Zhijun He5d677d12016-05-29 16:52:39 -0700986 // Set transform flags to ensure preview to be rotated correctly.
Shuzhen Wang610d7b82022-02-08 14:37:22 -0800987 res = setStreamTransformLocked(streamId, streamInfo.mirrorMode);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700988
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800989 // Fill in mHighResolutionCameraIdToStreamIdSet map
990 const String8 &cameraIdUsed =
991 physicalCameraId.size() != 0 ? physicalCameraId : mCameraIdStr;
992 const char *cameraIdUsedCStr = cameraIdUsed.string();
993 // Only needed for high resolution sensors
994 if (mHighResolutionSensors.find(cameraIdUsedCStr) !=
995 mHighResolutionSensors.end()) {
996 mHighResolutionCameraIdToStreamIdSet[cameraIdUsedCStr].insert(streamId);
997 }
998
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800999 *newStreamId = streamId;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001000 }
1001
1002 return res;
1003}
1004
Zhijun He5d677d12016-05-29 16:52:39 -07001005binder::Status CameraDeviceClient::createDeferredSurfaceStreamLocked(
1006 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
Shuzhen Wang758c2152017-01-10 18:26:18 -08001007 bool isShared,
Zhijun He5d677d12016-05-29 16:52:39 -07001008 /*out*/
1009 int* newStreamId) {
1010 int width, height, format, surfaceType;
Emilian Peev050f5dc2017-05-18 14:43:56 +01001011 uint64_t consumerUsage;
Zhijun He5d677d12016-05-29 16:52:39 -07001012 android_dataspace dataSpace;
1013 status_t err;
1014 binder::Status res;
1015
1016 if (!mDevice.get()) {
1017 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1018 }
1019
1020 // Infer the surface info for deferred surface stream creation.
1021 width = outputConfiguration.getWidth();
1022 height = outputConfiguration.getHeight();
1023 surfaceType = outputConfiguration.getSurfaceType();
1024 format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
1025 dataSpace = android_dataspace_t::HAL_DATASPACE_UNKNOWN;
1026 // Hardcode consumer usage flags: SurfaceView--0x900, SurfaceTexture--0x100.
1027 consumerUsage = GraphicBuffer::USAGE_HW_TEXTURE;
1028 if (surfaceType == OutputConfiguration::SURFACE_TYPE_SURFACE_VIEW) {
1029 consumerUsage |= GraphicBuffer::USAGE_HW_COMPOSER;
1030 }
1031 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001032 std::vector<sp<Surface>> noSurface;
Emilian Peev40ead602017-09-26 15:46:36 +01001033 std::vector<int> surfaceIds;
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001034 String8 physicalCameraId(outputConfiguration.getPhysicalCameraId());
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001035 const String8 &cameraIdUsed =
1036 physicalCameraId.size() != 0 ? physicalCameraId : mCameraIdStr;
1037 // Here, we override sensor pixel modes
1038 std::unordered_set<int32_t> overriddenSensorPixelModesUsed;
1039 const std::vector<int32_t> &sensorPixelModesUsed =
1040 outputConfiguration.getSensorPixelModesUsed();
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07001041 if (SessionConfigurationUtils::checkAndOverrideSensorPixelModesUsed(
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001042 sensorPixelModesUsed, format, width, height, getStaticInfo(cameraIdUsed),
1043 /*allowRounding*/ false, &overriddenSensorPixelModesUsed) != OK) {
1044 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1045 "sensor pixel modes used not valid for deferred stream");
1046 }
1047
Shuzhen Wang0129d522016-10-30 22:43:41 -07001048 err = mDevice->createStream(noSurface, /*hasDeferredConsumer*/true, width,
1049 height, format, dataSpace,
Emilian Peevf4816702020-04-03 15:44:51 -07001050 static_cast<camera_stream_rotation_t>(outputConfiguration.getRotation()),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001051 &streamId, physicalCameraId,
1052 overriddenSensorPixelModesUsed,
1053 &surfaceIds,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001054 outputConfiguration.getSurfaceSetID(), isShared,
Emilian Peev2295df72021-11-12 18:14:10 -08001055 outputConfiguration.isMultiResolution(), consumerUsage,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001056 outputConfiguration.getDynamicRangeProfile(),
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001057 outputConfiguration.getStreamUseCase(),
1058 outputConfiguration.getMirrorMode());
Zhijun He5d677d12016-05-29 16:52:39 -07001059
1060 if (err != OK) {
1061 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001062 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
1063 mCameraIdStr.string(), width, height, format, dataSpace, strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001064 } else {
1065 // Can not add streamId to mStreamMap here, as the surface is deferred. Add it to
1066 // a separate list to track. Once the deferred surface is set, this id will be
1067 // relocated to mStreamMap.
1068 mDeferredStreams.push_back(streamId);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001069 mStreamInfoMap.emplace(std::piecewise_construct, std::forward_as_tuple(streamId),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001070 std::forward_as_tuple(width, height, format, dataSpace, consumerUsage,
Emilian Peev2295df72021-11-12 18:14:10 -08001071 overriddenSensorPixelModesUsed,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001072 outputConfiguration.getDynamicRangeProfile(),
Shuzhen Wange4208922022-02-01 16:52:48 -08001073 outputConfiguration.getStreamUseCase(),
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001074 outputConfiguration.getTimestampBase(),
1075 outputConfiguration.getMirrorMode()));
Shuzhen Wang758c2152017-01-10 18:26:18 -08001076
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001077 ALOGV("%s: Camera %s: Successfully created a new stream ID %d for a deferred surface"
Zhijun He5d677d12016-05-29 16:52:39 -07001078 " (%d x %d) stream with format 0x%x.",
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001079 __FUNCTION__, mCameraIdStr.string(), streamId, width, height, format);
Zhijun He5d677d12016-05-29 16:52:39 -07001080
1081 // Set transform flags to ensure preview to be rotated correctly.
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001082 res = setStreamTransformLocked(streamId, outputConfiguration.getMirrorMode());
Zhijun He5d677d12016-05-29 16:52:39 -07001083
1084 *newStreamId = streamId;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001085 // Fill in mHighResolutionCameraIdToStreamIdSet
1086 const char *cameraIdUsedCStr = cameraIdUsed.string();
1087 // Only needed for high resolution sensors
1088 if (mHighResolutionSensors.find(cameraIdUsedCStr) !=
1089 mHighResolutionSensors.end()) {
1090 mHighResolutionCameraIdToStreamIdSet[cameraIdUsed.string()].insert(streamId);
1091 }
Zhijun He5d677d12016-05-29 16:52:39 -07001092 }
1093 return res;
1094}
1095
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001096binder::Status CameraDeviceClient::setStreamTransformLocked(int streamId, int mirrorMode) {
Zhijun He5d677d12016-05-29 16:52:39 -07001097 int32_t transform = 0;
1098 status_t err;
1099 binder::Status res;
1100
1101 if (!mDevice.get()) {
1102 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1103 }
1104
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001105 err = getRotationTransformLocked(mirrorMode, &transform);
Zhijun He5d677d12016-05-29 16:52:39 -07001106 if (err != OK) {
1107 // Error logged by getRotationTransformLocked.
1108 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
1109 "Unable to calculate rotation transform for new stream");
1110 }
1111
1112 err = mDevice->setStreamTransform(streamId, transform);
1113 if (err != OK) {
1114 String8 msg = String8::format("Failed to set stream transform (stream id %d)",
1115 streamId);
1116 ALOGE("%s: %s", __FUNCTION__, msg.string());
1117 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
1118 }
1119
1120 return res;
1121}
Ruben Brunkbba75572014-11-20 17:29:50 -08001122
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001123binder::Status CameraDeviceClient::createInputStream(
Shuzhen Wang83bff122020-11-20 15:51:39 -08001124 int width, int height, int format, bool isMultiResolution,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001125 /*out*/
1126 int32_t* newStreamId) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001127
1128 ATRACE_CALL();
Shuzhen Wang83bff122020-11-20 15:51:39 -08001129 ALOGV("%s (w = %d, h = %d, f = 0x%x, isMultiResolution %d)", __FUNCTION__,
1130 width, height, format, isMultiResolution);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001131
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001132 binder::Status res;
1133 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001134
1135 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001136
1137 if (!mDevice.get()) {
1138 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1139 }
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001140
1141 if (mInputStream.configured) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001142 String8 msg = String8::format("Camera %s: Already has an input stream "
Yi Kong0f414de2017-12-15 13:48:50 -08001143 "configured (ID %d)", mCameraIdStr.string(), mInputStream.id);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001144 ALOGE("%s: %s", __FUNCTION__, msg.string() );
1145 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001146 }
1147
1148 int streamId = -1;
Shuzhen Wang83bff122020-11-20 15:51:39 -08001149 status_t err = mDevice->createInputStream(width, height, format, isMultiResolution, &streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001150 if (err == OK) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001151 mInputStream.configured = true;
1152 mInputStream.width = width;
1153 mInputStream.height = height;
1154 mInputStream.format = format;
1155 mInputStream.id = streamId;
1156
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001157 ALOGV("%s: Camera %s: Successfully created a new input stream ID %d",
1158 __FUNCTION__, mCameraIdStr.string(), streamId);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001159
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001160 *newStreamId = streamId;
1161 } else {
1162 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001163 "Camera %s: Error creating new input stream: %s (%d)", mCameraIdStr.string(),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001164 strerror(-err), err);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001165 }
1166
1167 return res;
1168}
1169
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001170binder::Status CameraDeviceClient::getInputSurface(/*out*/ view::Surface *inputSurface) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001171
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001172 binder::Status res;
1173 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1174
1175 if (inputSurface == NULL) {
1176 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Null input surface");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001177 }
1178
1179 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001180 if (!mDevice.get()) {
1181 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1182 }
1183 sp<IGraphicBufferProducer> producer;
1184 status_t err = mDevice->getInputBufferProducer(&producer);
1185 if (err != OK) {
1186 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001187 "Camera %s: Error getting input Surface: %s (%d)",
1188 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001189 } else {
1190 inputSurface->name = String16("CameraInput");
1191 inputSurface->graphicBufferProducer = producer;
1192 }
1193 return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001194}
1195
Emilian Peev40ead602017-09-26 15:46:36 +01001196binder::Status CameraDeviceClient::updateOutputConfiguration(int streamId,
1197 const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
1198 ATRACE_CALL();
1199
1200 binder::Status res;
1201 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1202
1203 Mutex::Autolock icl(mBinderSerializationLock);
1204
1205 if (!mDevice.get()) {
1206 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1207 }
1208
1209 const std::vector<sp<IGraphicBufferProducer> >& bufferProducers =
1210 outputConfiguration.getGraphicBufferProducers();
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -07001211 String8 physicalCameraId(outputConfiguration.getPhysicalCameraId());
1212
Emilian Peev40ead602017-09-26 15:46:36 +01001213 auto producerCount = bufferProducers.size();
1214 if (producerCount == 0) {
1215 ALOGE("%s: bufferProducers must not be empty", __FUNCTION__);
1216 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1217 "bufferProducers must not be empty");
1218 }
1219
1220 // The first output is the one associated with the output configuration.
1221 // It should always be present, valid and the corresponding stream id should match.
1222 sp<IBinder> binder = IInterface::asBinder(bufferProducers[0]);
1223 ssize_t index = mStreamMap.indexOfKey(binder);
1224 if (index == NAME_NOT_FOUND) {
1225 ALOGE("%s: Outputconfiguration is invalid", __FUNCTION__);
1226 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1227 "OutputConfiguration is invalid");
1228 }
1229 if (mStreamMap.valueFor(binder).streamId() != streamId) {
1230 ALOGE("%s: Stream Id: %d provided doesn't match the id: %d in the stream map",
1231 __FUNCTION__, streamId, mStreamMap.valueFor(binder).streamId());
1232 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1233 "Stream id is invalid");
1234 }
1235
1236 std::vector<size_t> removedSurfaceIds;
1237 std::vector<sp<IBinder>> removedOutputs;
1238 std::vector<sp<Surface>> newOutputs;
1239 std::vector<OutputStreamInfo> streamInfos;
1240 KeyedVector<sp<IBinder>, sp<IGraphicBufferProducer>> newOutputsMap;
1241 for (auto &it : bufferProducers) {
1242 newOutputsMap.add(IInterface::asBinder(it), it);
1243 }
1244
1245 for (size_t i = 0; i < mStreamMap.size(); i++) {
1246 ssize_t idx = newOutputsMap.indexOfKey(mStreamMap.keyAt(i));
1247 if (idx == NAME_NOT_FOUND) {
1248 if (mStreamMap[i].streamId() == streamId) {
1249 removedSurfaceIds.push_back(mStreamMap[i].surfaceId());
1250 removedOutputs.push_back(mStreamMap.keyAt(i));
1251 }
1252 } else {
1253 if (mStreamMap[i].streamId() != streamId) {
1254 ALOGE("%s: Output surface already part of a different stream", __FUNCTION__);
1255 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1256 "Target Surface is invalid");
1257 }
1258 newOutputsMap.removeItemsAt(idx);
1259 }
1260 }
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001261 const std::vector<int32_t> &sensorPixelModesUsed =
1262 outputConfiguration.getSensorPixelModesUsed();
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001263 int streamUseCase = outputConfiguration.getStreamUseCase();
Shuzhen Wange4208922022-02-01 16:52:48 -08001264 int timestampBase = outputConfiguration.getTimestampBase();
Emilian Peevc81a7592022-02-14 17:38:18 -08001265 int64_t dynamicRangeProfile = outputConfiguration.getDynamicRangeProfile();
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001266 int mirrorMode = outputConfiguration.getMirrorMode();
Emilian Peev2295df72021-11-12 18:14:10 -08001267
Emilian Peev40ead602017-09-26 15:46:36 +01001268 for (size_t i = 0; i < newOutputsMap.size(); i++) {
1269 OutputStreamInfo outInfo;
1270 sp<Surface> surface;
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07001271 res = SessionConfigurationUtils::createSurfaceFromGbp(outInfo,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001272 /*isStreamInfoValid*/ false, surface, newOutputsMap.valueAt(i), mCameraIdStr,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001273 mDevice->infoPhysical(physicalCameraId), sensorPixelModesUsed, dynamicRangeProfile,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001274 streamUseCase, timestampBase, mirrorMode);
Emilian Peev40ead602017-09-26 15:46:36 +01001275 if (!res.isOk())
1276 return res;
1277
Emilian Peev40ead602017-09-26 15:46:36 +01001278 streamInfos.push_back(outInfo);
1279 newOutputs.push_back(surface);
1280 }
1281
1282 //Trivial case no changes required
1283 if (removedSurfaceIds.empty() && newOutputs.empty()) {
1284 return binder::Status::ok();
1285 }
1286
1287 KeyedVector<sp<Surface>, size_t> outputMap;
1288 auto ret = mDevice->updateStream(streamId, newOutputs, streamInfos, removedSurfaceIds,
1289 &outputMap);
1290 if (ret != OK) {
1291 switch (ret) {
1292 case NAME_NOT_FOUND:
1293 case BAD_VALUE:
1294 case -EBUSY:
1295 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1296 "Camera %s: Error updating stream: %s (%d)",
1297 mCameraIdStr.string(), strerror(ret), ret);
1298 break;
1299 default:
1300 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1301 "Camera %s: Error updating stream: %s (%d)",
1302 mCameraIdStr.string(), strerror(ret), ret);
1303 break;
1304 }
1305 } else {
1306 for (const auto &it : removedOutputs) {
1307 mStreamMap.removeItem(it);
1308 }
1309
1310 for (size_t i = 0; i < outputMap.size(); i++) {
1311 mStreamMap.add(IInterface::asBinder(outputMap.keyAt(i)->getIGraphicBufferProducer()),
1312 StreamSurfaceId(streamId, outputMap.valueAt(i)));
1313 }
1314
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -08001315 mConfiguredOutputs.replaceValueFor(streamId, outputConfiguration);
1316
Emilian Peev40ead602017-09-26 15:46:36 +01001317 ALOGV("%s: Camera %s: Successful stream ID %d update",
1318 __FUNCTION__, mCameraIdStr.string(), streamId);
1319 }
1320
1321 return res;
1322}
1323
Igor Murashkine7ee7632013-06-11 18:10:18 -07001324// Create a request object from a template.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001325binder::Status CameraDeviceClient::createDefaultRequest(int templateId,
1326 /*out*/
1327 hardware::camera2::impl::CameraMetadataNative* request)
Igor Murashkine7ee7632013-06-11 18:10:18 -07001328{
1329 ATRACE_CALL();
1330 ALOGV("%s (templateId = 0x%x)", __FUNCTION__, templateId);
1331
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001332 binder::Status res;
1333 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001334
1335 Mutex::Autolock icl(mBinderSerializationLock);
1336
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001337 if (!mDevice.get()) {
1338 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1339 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001340
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001341 status_t err;
Emilian Peevf4816702020-04-03 15:44:51 -07001342 camera_request_template_t tempId = camera_request_template_t::CAMERA_TEMPLATE_COUNT;
1343 if (!(res = mapRequestTemplate(templateId, &tempId)).isOk()) return res;
1344
1345 CameraMetadata metadata;
1346 if ( (err = mDevice->createDefaultRequest(tempId, &metadata) ) == OK &&
Igor Murashkine7ee7632013-06-11 18:10:18 -07001347 request != NULL) {
1348
1349 request->swap(metadata);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001350 } else if (err == BAD_VALUE) {
1351 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001352 "Camera %s: Template ID %d is invalid or not supported: %s (%d)",
1353 mCameraIdStr.string(), templateId, strerror(-err), err);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001354
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001355 } else {
1356 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001357 "Camera %s: Error creating default request for template %d: %s (%d)",
1358 mCameraIdStr.string(), templateId, strerror(-err), err);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001359 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001360 return res;
1361}
1362
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001363binder::Status CameraDeviceClient::getCameraInfo(
1364 /*out*/
1365 hardware::camera2::impl::CameraMetadataNative* info)
Igor Murashkine7ee7632013-06-11 18:10:18 -07001366{
1367 ATRACE_CALL();
1368 ALOGV("%s", __FUNCTION__);
1369
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001370 binder::Status res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001371
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001372 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001373
1374 Mutex::Autolock icl(mBinderSerializationLock);
1375
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001376 if (!mDevice.get()) {
1377 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1378 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001379
Igor Murashkin099b4572013-07-12 17:52:16 -07001380 if (info != NULL) {
1381 *info = mDevice->info(); // static camera metadata
1382 // TODO: merge with device-specific camera metadata
1383 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001384
1385 return res;
1386}
1387
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001388binder::Status CameraDeviceClient::waitUntilIdle()
Zhijun He2ab500c2013-07-23 08:02:53 -07001389{
1390 ATRACE_CALL();
1391 ALOGV("%s", __FUNCTION__);
1392
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001393 binder::Status res;
1394 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Zhijun He2ab500c2013-07-23 08:02:53 -07001395
1396 Mutex::Autolock icl(mBinderSerializationLock);
1397
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001398 if (!mDevice.get()) {
1399 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1400 }
Zhijun He2ab500c2013-07-23 08:02:53 -07001401
1402 // FIXME: Also need check repeating burst.
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001403 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001404 if (mStreamingRequestId != REQUEST_ID_NONE) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001405 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001406 "Camera %s: Try to waitUntilIdle when there are active streaming requests",
1407 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001408 ALOGE("%s: %s", __FUNCTION__, msg.string());
1409 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Zhijun He2ab500c2013-07-23 08:02:53 -07001410 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001411 status_t err = mDevice->waitUntilDrained();
1412 if (err != OK) {
1413 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001414 "Camera %s: Error waiting to drain: %s (%d)",
1415 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001416 }
Zhijun He2ab500c2013-07-23 08:02:53 -07001417 ALOGV("%s Done", __FUNCTION__);
Zhijun He2ab500c2013-07-23 08:02:53 -07001418 return res;
1419}
1420
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001421binder::Status CameraDeviceClient::flush(
1422 /*out*/
1423 int64_t* lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001424 ATRACE_CALL();
1425 ALOGV("%s", __FUNCTION__);
1426
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001427 binder::Status res;
1428 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001429
1430 Mutex::Autolock icl(mBinderSerializationLock);
1431
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001432 if (!mDevice.get()) {
1433 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1434 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001435
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001436 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001437 mStreamingRequestId = REQUEST_ID_NONE;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001438 status_t err = mDevice->flush(lastFrameNumber);
1439 if (err != OK) {
1440 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001441 "Camera %s: Error flushing device: %s (%d)", mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001442 }
1443 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001444}
1445
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001446binder::Status CameraDeviceClient::prepare(int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001447 ATRACE_CALL();
1448 ALOGV("%s", __FUNCTION__);
1449
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001450 binder::Status res;
1451 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001452
1453 Mutex::Autolock icl(mBinderSerializationLock);
1454
1455 // Guard against trying to prepare non-created streams
1456 ssize_t index = NAME_NOT_FOUND;
1457 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001458 if (streamId == mStreamMap.valueAt(i).streamId()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001459 index = i;
1460 break;
1461 }
1462 }
1463
1464 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001465 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1466 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001467 ALOGW("%s: %s", __FUNCTION__, msg.string());
1468 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001469 }
1470
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001471 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1472 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001473 status_t err = mDevice->prepare(streamId);
1474 if (err == BAD_VALUE) {
1475 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001476 "Camera %s: Stream %d has already been used, and cannot be prepared",
1477 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001478 } else if (err != OK) {
1479 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001480 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001481 strerror(-err), err);
1482 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001483 return res;
1484}
1485
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001486binder::Status CameraDeviceClient::prepare2(int maxCount, int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001487 ATRACE_CALL();
1488 ALOGV("%s", __FUNCTION__);
1489
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001490 binder::Status res;
1491 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Ruben Brunkc78ac262015-08-13 17:58:46 -07001492
1493 Mutex::Autolock icl(mBinderSerializationLock);
1494
1495 // Guard against trying to prepare non-created streams
1496 ssize_t index = NAME_NOT_FOUND;
1497 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001498 if (streamId == mStreamMap.valueAt(i).streamId()) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001499 index = i;
1500 break;
1501 }
1502 }
1503
1504 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001505 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1506 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001507 ALOGW("%s: %s", __FUNCTION__, msg.string());
1508 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -07001509 }
1510
1511 if (maxCount <= 0) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001512 String8 msg = String8::format("Camera %s: maxCount (%d) must be greater than 0",
1513 mCameraIdStr.string(), maxCount);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001514 ALOGE("%s: %s", __FUNCTION__, msg.string());
1515 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -07001516 }
1517
1518 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1519 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001520 status_t err = mDevice->prepare(maxCount, streamId);
1521 if (err == BAD_VALUE) {
1522 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001523 "Camera %s: Stream %d has already been used, and cannot be prepared",
1524 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001525 } else if (err != OK) {
1526 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001527 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001528 strerror(-err), err);
1529 }
Ruben Brunkc78ac262015-08-13 17:58:46 -07001530
1531 return res;
1532}
1533
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001534binder::Status CameraDeviceClient::tearDown(int streamId) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001535 ATRACE_CALL();
1536 ALOGV("%s", __FUNCTION__);
1537
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001538 binder::Status res;
1539 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001540
1541 Mutex::Autolock icl(mBinderSerializationLock);
1542
1543 // Guard against trying to prepare non-created streams
1544 ssize_t index = NAME_NOT_FOUND;
1545 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001546 if (streamId == mStreamMap.valueAt(i).streamId()) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001547 index = i;
1548 break;
1549 }
1550 }
1551
1552 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001553 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1554 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001555 ALOGW("%s: %s", __FUNCTION__, msg.string());
1556 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001557 }
1558
1559 // Also returns BAD_VALUE if stream ID was not valid or if the stream is in
1560 // use
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001561 status_t err = mDevice->tearDown(streamId);
1562 if (err == BAD_VALUE) {
1563 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001564 "Camera %s: Stream %d is still in use, cannot be torn down",
1565 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001566 } else if (err != OK) {
1567 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001568 "Camera %s: Error tearing down stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001569 strerror(-err), err);
1570 }
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001571
1572 return res;
1573}
1574
Shuzhen Wang758c2152017-01-10 18:26:18 -08001575binder::Status CameraDeviceClient::finalizeOutputConfigurations(int32_t streamId,
Zhijun He5d677d12016-05-29 16:52:39 -07001576 const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
1577 ATRACE_CALL();
1578
1579 binder::Status res;
1580 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1581
1582 Mutex::Autolock icl(mBinderSerializationLock);
1583
Shuzhen Wang0129d522016-10-30 22:43:41 -07001584 const std::vector<sp<IGraphicBufferProducer> >& bufferProducers =
1585 outputConfiguration.getGraphicBufferProducers();
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -07001586 String8 physicalId(outputConfiguration.getPhysicalCameraId());
Zhijun He5d677d12016-05-29 16:52:39 -07001587
Shuzhen Wang0129d522016-10-30 22:43:41 -07001588 if (bufferProducers.size() == 0) {
1589 ALOGE("%s: bufferProducers must not be empty", __FUNCTION__);
Zhijun He5d677d12016-05-29 16:52:39 -07001590 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Target Surface is invalid");
1591 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001592
Shuzhen Wang758c2152017-01-10 18:26:18 -08001593 // streamId should be in mStreamMap if this stream already has a surface attached
1594 // to it. Otherwise, it should be in mDeferredStreams.
1595 bool streamIdConfigured = false;
1596 ssize_t deferredStreamIndex = NAME_NOT_FOUND;
1597 for (size_t i = 0; i < mStreamMap.size(); i++) {
1598 if (mStreamMap.valueAt(i).streamId() == streamId) {
1599 streamIdConfigured = true;
1600 break;
1601 }
Zhijun He5d677d12016-05-29 16:52:39 -07001602 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001603 for (size_t i = 0; i < mDeferredStreams.size(); i++) {
1604 if (streamId == mDeferredStreams[i]) {
1605 deferredStreamIndex = i;
1606 break;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001607 }
1608
Shuzhen Wang758c2152017-01-10 18:26:18 -08001609 }
1610 if (deferredStreamIndex == NAME_NOT_FOUND && !streamIdConfigured) {
1611 String8 msg = String8::format("Camera %s: deferred surface is set to a unknown stream"
1612 "(ID %d)", mCameraIdStr.string(), streamId);
1613 ALOGW("%s: %s", __FUNCTION__, msg.string());
1614 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Zhijun He5d677d12016-05-29 16:52:39 -07001615 }
1616
Shuzhen Wang88fd0052017-02-09 16:40:07 -08001617 if (mStreamInfoMap[streamId].finalized) {
1618 String8 msg = String8::format("Camera %s: finalizeOutputConfigurations has been called"
1619 " on stream ID %d", mCameraIdStr.string(), streamId);
1620 ALOGW("%s: %s", __FUNCTION__, msg.string());
1621 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1622 }
1623
Zhijun He5d677d12016-05-29 16:52:39 -07001624 if (!mDevice.get()) {
1625 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1626 }
1627
Shuzhen Wang758c2152017-01-10 18:26:18 -08001628 std::vector<sp<Surface>> consumerSurfaces;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001629 const std::vector<int32_t> &sensorPixelModesUsed =
1630 outputConfiguration.getSensorPixelModesUsed();
Emilian Peevc81a7592022-02-14 17:38:18 -08001631 int64_t dynamicRangeProfile = outputConfiguration.getDynamicRangeProfile();
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001632 int streamUseCase= outputConfiguration.getStreamUseCase();
Shuzhen Wange4208922022-02-01 16:52:48 -08001633 int timestampBase = outputConfiguration.getTimestampBase();
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001634 int mirrorMode = outputConfiguration.getMirrorMode();
Shuzhen Wang758c2152017-01-10 18:26:18 -08001635 for (auto& bufferProducer : bufferProducers) {
1636 // Don't create multiple streams for the same target surface
Zhijun He5d677d12016-05-29 16:52:39 -07001637 ssize_t index = mStreamMap.indexOfKey(IInterface::asBinder(bufferProducer));
1638 if (index != NAME_NOT_FOUND) {
Shuzhen Wang758c2152017-01-10 18:26:18 -08001639 ALOGV("Camera %s: Surface already has a stream created "
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001640 " for it (ID %zd)", mCameraIdStr.string(), index);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001641 continue;
Zhijun He5d677d12016-05-29 16:52:39 -07001642 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001643
1644 sp<Surface> surface;
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07001645 res = SessionConfigurationUtils::createSurfaceFromGbp(mStreamInfoMap[streamId],
Colin Crossb8a9dbb2020-08-27 04:12:26 +00001646 true /*isStreamInfoValid*/, surface, bufferProducer, mCameraIdStr,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001647 mDevice->infoPhysical(physicalId), sensorPixelModesUsed, dynamicRangeProfile,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001648 streamUseCase, timestampBase, mirrorMode);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001649
1650 if (!res.isOk())
1651 return res;
1652
1653 consumerSurfaces.push_back(surface);
Zhijun He5d677d12016-05-29 16:52:39 -07001654 }
1655
Shuzhen Wanga81ce342017-04-13 15:18:36 -07001656 // Gracefully handle case where finalizeOutputConfigurations is called
1657 // without any new surface.
1658 if (consumerSurfaces.size() == 0) {
1659 mStreamInfoMap[streamId].finalized = true;
1660 return res;
1661 }
1662
Zhijun He5d677d12016-05-29 16:52:39 -07001663 // Finish the deferred stream configuration with the surface.
Shuzhen Wang758c2152017-01-10 18:26:18 -08001664 status_t err;
Emilian Peev40ead602017-09-26 15:46:36 +01001665 std::vector<int> consumerSurfaceIds;
1666 err = mDevice->setConsumerSurfaces(streamId, consumerSurfaces, &consumerSurfaceIds);
Zhijun He5d677d12016-05-29 16:52:39 -07001667 if (err == OK) {
Shuzhen Wang758c2152017-01-10 18:26:18 -08001668 for (size_t i = 0; i < consumerSurfaces.size(); i++) {
1669 sp<IBinder> binder = IInterface::asBinder(
1670 consumerSurfaces[i]->getIGraphicBufferProducer());
Emilian Peev40ead602017-09-26 15:46:36 +01001671 ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %d", __FUNCTION__,
Shuzhen Wang758c2152017-01-10 18:26:18 -08001672 binder.get(), streamId, consumerSurfaceIds[i]);
1673 mStreamMap.add(binder, StreamSurfaceId(streamId, consumerSurfaceIds[i]));
1674 }
1675 if (deferredStreamIndex != NAME_NOT_FOUND) {
1676 mDeferredStreams.removeItemsAt(deferredStreamIndex);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001677 }
Shuzhen Wang88fd0052017-02-09 16:40:07 -08001678 mStreamInfoMap[streamId].finalized = true;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -08001679 mConfiguredOutputs.replaceValueFor(streamId, outputConfiguration);
Zhijun He5d677d12016-05-29 16:52:39 -07001680 } else if (err == NO_INIT) {
1681 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001682 "Camera %s: Deferred surface is invalid: %s (%d)",
1683 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001684 } else {
1685 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001686 "Camera %s: Error setting output stream deferred surface: %s (%d)",
1687 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001688 }
1689
1690 return res;
1691}
1692
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001693binder::Status CameraDeviceClient::setCameraAudioRestriction(int32_t mode) {
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001694 ATRACE_CALL();
1695 binder::Status res;
1696 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1697
1698 if (!isValidAudioRestriction(mode)) {
1699 String8 msg = String8::format("Camera %s: invalid audio restriction mode %d",
1700 mCameraIdStr.string(), mode);
1701 ALOGW("%s: %s", __FUNCTION__, msg.string());
1702 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1703 }
1704
1705 Mutex::Autolock icl(mBinderSerializationLock);
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001706 BasicClient::setAudioRestriction(mode);
1707 return binder::Status::ok();
1708}
1709
1710binder::Status CameraDeviceClient::getGlobalAudioRestriction(/*out*/ int32_t* outMode) {
1711 ATRACE_CALL();
1712 binder::Status res;
1713 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1714 Mutex::Autolock icl(mBinderSerializationLock);
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001715 if (outMode != nullptr) {
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001716 *outMode = BasicClient::getServiceAudioRestriction();
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001717 }
1718 return binder::Status::ok();
1719}
1720
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08001721status_t CameraDeviceClient::setRotateAndCropOverride(uint8_t rotateAndCrop) {
1722 if (rotateAndCrop > ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return BAD_VALUE;
1723
1724 return mDevice->setRotateAndCropAutoBehavior(
1725 static_cast<camera_metadata_enum_android_scaler_rotate_and_crop_t>(rotateAndCrop));
1726}
1727
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08001728bool CameraDeviceClient::supportsCameraMute() {
1729 return mDevice->supportsCameraMute();
1730}
1731
1732status_t CameraDeviceClient::setCameraMute(bool enabled) {
1733 return mDevice->setCameraMute(enabled);
1734}
1735
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001736binder::Status CameraDeviceClient::switchToOffline(
1737 const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb,
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001738 const std::vector<int>& offlineOutputIds,
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001739 /*out*/
1740 sp<hardware::camera2::ICameraOfflineSession>* session) {
1741 ATRACE_CALL();
1742
1743 binder::Status res;
1744 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1745
1746 Mutex::Autolock icl(mBinderSerializationLock);
1747
1748 if (!mDevice.get()) {
1749 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1750 }
1751
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001752 if (offlineOutputIds.empty()) {
Emilian Peevcc0b7952020-01-07 13:54:47 -08001753 String8 msg = String8::format("Offline surfaces must not be empty");
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001754 ALOGE("%s: %s", __FUNCTION__, msg.string());
1755 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1756 }
1757
1758 if (session == nullptr) {
1759 String8 msg = String8::format("Invalid offline session");
1760 ALOGE("%s: %s", __FUNCTION__, msg.string());
1761 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1762 }
1763
Yin-Chia Yeh87fccca2020-01-28 09:37:18 -08001764 std::vector<int32_t> offlineStreamIds;
1765 offlineStreamIds.reserve(offlineOutputIds.size());
Emilian Peev4697b642019-11-19 17:11:14 -08001766 KeyedVector<sp<IBinder>, sp<CompositeStream>> offlineCompositeStreamMap;
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001767 for (const auto& streamId : offlineOutputIds) {
1768 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001769 if (index == NAME_NOT_FOUND) {
Emilian Peevcc0b7952020-01-07 13:54:47 -08001770 String8 msg = String8::format("Offline surface with id: %d is not registered",
1771 streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001772 ALOGE("%s: %s", __FUNCTION__, msg.string());
1773 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1774 }
Emilian Peevcc0b7952020-01-07 13:54:47 -08001775
1776 if (!mStreamInfoMap[streamId].supportsOffline) {
1777 String8 msg = String8::format("Offline surface with id: %d doesn't support "
1778 "offline mode", streamId);
1779 ALOGE("%s: %s", __FUNCTION__, msg.string());
1780 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1781 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001782
Emilian Peev2f5d6012022-01-19 16:16:50 -08001783 Mutex::Autolock l(mCompositeLock);
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001784 bool isCompositeStream = false;
Emilian Peev2f5d6012022-01-19 16:16:50 -08001785 for (const auto& gbp : mConfiguredOutputs.valueAt(index).getGraphicBufferProducers()) {
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001786 sp<Surface> s = new Surface(gbp, false /*controlledByApp*/);
Pirama Arumuga Nainar8db21062022-01-28 10:15:12 -08001787 isCompositeStream = camera3::DepthCompositeStream::isDepthCompositeStream(s) ||
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001788 camera3::HeicCompositeStream::isHeicCompositeStream(s);
Emilian Peev4697b642019-11-19 17:11:14 -08001789 if (isCompositeStream) {
1790 auto compositeIdx = mCompositeStreamMap.indexOfKey(IInterface::asBinder(gbp));
1791 if (compositeIdx == NAME_NOT_FOUND) {
1792 ALOGE("%s: Unknown composite stream", __FUNCTION__);
1793 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1794 "Unknown composite stream");
1795 }
1796
1797 mCompositeStreamMap.valueAt(compositeIdx)->insertCompositeStreamIds(
1798 &offlineStreamIds);
1799 offlineCompositeStreamMap.add(mCompositeStreamMap.keyAt(compositeIdx),
1800 mCompositeStreamMap.valueAt(compositeIdx));
1801 break;
1802 }
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001803 }
1804
Emilian Peev4697b642019-11-19 17:11:14 -08001805 if (!isCompositeStream) {
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001806 offlineStreamIds.push_back(streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001807 }
1808 }
1809
1810 sp<CameraOfflineSessionBase> offlineSession;
1811 auto ret = mDevice->switchToOffline(offlineStreamIds, &offlineSession);
1812 if (ret != OK) {
1813 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1814 "Camera %s: Error switching to offline mode: %s (%d)",
1815 mCameraIdStr.string(), strerror(ret), ret);
1816 }
1817
Emilian Peevcc0b7952020-01-07 13:54:47 -08001818 sp<CameraOfflineSessionClient> offlineClient;
1819 if (offlineSession.get() != nullptr) {
1820 offlineClient = new CameraOfflineSessionClient(sCameraService,
1821 offlineSession, offlineCompositeStreamMap, cameraCb, mClientPackageName,
Emilian Peev8b64f282021-03-25 16:49:57 -07001822 mClientFeatureId, mCameraIdStr, mCameraFacing, mOrientation, mClientPid, mClientUid,
1823 mServicePid);
Emilian Peevcc0b7952020-01-07 13:54:47 -08001824 ret = sCameraService->addOfflineClient(mCameraIdStr, offlineClient);
1825 }
1826
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001827 if (ret == OK) {
Emilian Peevd99c8ae2019-11-26 13:19:13 -08001828 // A successful offline session switch must reset the current camera client
1829 // and release any resources occupied by previously configured streams.
1830 mStreamMap.clear();
1831 mConfiguredOutputs.clear();
1832 mDeferredStreams.clear();
1833 mStreamInfoMap.clear();
Emilian Peev2f5d6012022-01-19 16:16:50 -08001834 Mutex::Autolock l(mCompositeLock);
Emilian Peevd99c8ae2019-11-26 13:19:13 -08001835 mCompositeStreamMap.clear();
1836 mInputStream = {false, 0, 0, 0, 0};
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001837 } else {
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001838 switch(ret) {
1839 case BAD_VALUE:
1840 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1841 "Illegal argument to HAL module for camera \"%s\"", mCameraIdStr.c_str());
1842 case TIMED_OUT:
1843 return STATUS_ERROR_FMT(CameraService::ERROR_CAMERA_IN_USE,
1844 "Camera \"%s\" is already open", mCameraIdStr.c_str());
1845 default:
1846 return STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1847 "Failed to initialize camera \"%s\": %s (%d)", mCameraIdStr.c_str(),
1848 strerror(-ret), ret);
1849 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001850 }
1851
1852 *session = offlineClient;
1853
1854 return binder::Status::ok();
1855}
1856
Igor Murashkine7ee7632013-06-11 18:10:18 -07001857status_t CameraDeviceClient::dump(int fd, const Vector<String16>& args) {
Eino-Ville Talvalac4003962016-01-13 10:07:04 -08001858 return BasicClient::dump(fd, args);
1859}
1860
1861status_t CameraDeviceClient::dumpClient(int fd, const Vector<String16>& args) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001862 dprintf(fd, " CameraDeviceClient[%s] (%p) dump:\n",
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001863 mCameraIdStr.string(),
Eino-Ville Talvalae992e752014-11-07 16:17:48 -08001864 (getRemoteCallback() != NULL ?
Marco Nelissenf8880202014-11-14 07:58:25 -08001865 IInterface::asBinder(getRemoteCallback()).get() : NULL) );
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001866 dprintf(fd, " Current client UID %u\n", mClientUid);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001867
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001868 dprintf(fd, " State:\n");
1869 dprintf(fd, " Request ID counter: %d\n", mRequestIdCounter);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001870 if (mInputStream.configured) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001871 dprintf(fd, " Current input stream ID: %d\n", mInputStream.id);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001872 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001873 dprintf(fd, " No input stream configured.\n");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001874 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001875 if (!mStreamMap.isEmpty()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001876 dprintf(fd, " Current output stream/surface IDs:\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001877 for (size_t i = 0; i < mStreamMap.size(); i++) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001878 dprintf(fd, " Stream %d Surface %d\n",
Shuzhen Wang0129d522016-10-30 22:43:41 -07001879 mStreamMap.valueAt(i).streamId(),
1880 mStreamMap.valueAt(i).surfaceId());
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001881 }
Zhijun He5d677d12016-05-29 16:52:39 -07001882 } else if (!mDeferredStreams.isEmpty()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001883 dprintf(fd, " Current deferred surface output stream IDs:\n");
Zhijun He5d677d12016-05-29 16:52:39 -07001884 for (auto& streamId : mDeferredStreams) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001885 dprintf(fd, " Stream %d\n", streamId);
Zhijun He5d677d12016-05-29 16:52:39 -07001886 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001887 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001888 dprintf(fd, " No output streams configured.\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001889 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001890 // TODO: print dynamic/request section from most recent requests
1891 mFrameProcessor->dump(fd, args);
1892
1893 return dumpDevice(fd, args);
1894}
1895
Avichal Rakesh7e53cad2021-10-05 13:46:30 -07001896status_t CameraDeviceClient::startWatchingTags(const String8 &tags, int out) {
1897 sp<CameraDeviceBase> device = mDevice;
1898 if (!device) {
1899 dprintf(out, " Device is detached.");
1900 return OK;
1901 }
1902 device->startWatchingTags(tags);
1903 return OK;
1904}
1905
1906status_t CameraDeviceClient::stopWatchingTags(int out) {
1907 sp<CameraDeviceBase> device = mDevice;
1908 if (!device) {
1909 dprintf(out, " Device is detached.");
1910 return OK;
1911 }
1912 device->stopWatchingTags();
1913 return OK;
1914}
1915
1916status_t CameraDeviceClient::dumpWatchedEventsToVector(std::vector<std::string> &out) {
1917 sp<CameraDeviceBase> device = mDevice;
1918 if (!device) {
1919 return OK;
1920 }
1921 device->dumpWatchedEventsToVector(out);
1922 return OK;
1923}
1924
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001925void CameraDeviceClient::notifyError(int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -07001926 const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001927 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001928 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001929
Emilian Peev538c90e2018-12-17 18:03:19 +00001930 bool skipClientNotification = false;
Emilian Peev2f5d6012022-01-19 16:16:50 -08001931 {
1932 // Access to the composite stream map must be synchronized
1933 Mutex::Autolock l(mCompositeLock);
1934 // Composites can have multiple internal streams. Error notifications coming from such
1935 // internal streams may need to remain within camera service.
1936 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
1937 skipClientNotification |= mCompositeStreamMap.valueAt(i)->onError(errorCode,
1938 resultExtras);
1939 }
Emilian Peev538c90e2018-12-17 18:03:19 +00001940 }
1941
1942 if ((remoteCb != 0) && (!skipClientNotification)) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001943 remoteCb->onDeviceError(errorCode, resultExtras);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001944 }
1945}
1946
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001947void CameraDeviceClient::notifyRepeatingRequestError(long lastFrameNumber) {
1948 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
1949
1950 if (remoteCb != 0) {
Yin-Chia Yeh8ca23dc2017-09-05 18:15:56 -07001951 remoteCb->onRepeatingRequestError(lastFrameNumber, mStreamingRequestId);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001952 }
1953
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001954 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001955 mStreamingRequestId = REQUEST_ID_NONE;
1956}
1957
Shuzhen Wang316781a2020-08-18 18:11:01 -07001958void CameraDeviceClient::notifyIdle(
1959 int64_t requestCount, int64_t resultErrorCount, bool deviceError,
1960 const std::vector<hardware::CameraStreamStats>& streamStats) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001961 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001962 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001963
1964 if (remoteCb != 0) {
1965 remoteCb->onDeviceIdle();
1966 }
Shuzhen Wang316781a2020-08-18 18:11:01 -07001967 Camera2ClientBase::notifyIdle(requestCount, resultErrorCount, deviceError, streamStats);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001968}
1969
Jianing Weicb0652e2014-03-12 18:29:36 -07001970void CameraDeviceClient::notifyShutter(const CaptureResultExtras& resultExtras,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001971 nsecs_t timestamp) {
1972 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001973 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001974 if (remoteCb != 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001975 remoteCb->onCaptureStarted(resultExtras, timestamp);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001976 }
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07001977 Camera2ClientBase::notifyShutter(resultExtras, timestamp);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001978
Emilian Peev2f5d6012022-01-19 16:16:50 -08001979 // Access to the composite stream map must be synchronized
1980 Mutex::Autolock l(mCompositeLock);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001981 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
1982 mCompositeStreamMap.valueAt(i)->onShutter(resultExtras, timestamp);
1983 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001984}
1985
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001986void CameraDeviceClient::notifyPrepared(int streamId) {
1987 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001988 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001989 if (remoteCb != 0) {
1990 remoteCb->onPrepared(streamId);
1991 }
1992}
1993
Shuzhen Wang9d066012016-09-30 11:30:20 -07001994void CameraDeviceClient::notifyRequestQueueEmpty() {
1995 // Thread safe. Don't bother locking.
1996 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
1997 if (remoteCb != 0) {
1998 remoteCb->onRequestQueueEmpty();
1999 }
2000}
2001
Igor Murashkine7ee7632013-06-11 18:10:18 -07002002void CameraDeviceClient::detachDevice() {
2003 if (mDevice == 0) return;
2004
Shuzhen Wang316781a2020-08-18 18:11:01 -07002005 nsecs_t startTime = systemTime();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002006 ALOGV("Camera %s: Stopping processors", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -07002007
Emilian Peev7ee731f2022-02-08 14:55:52 -08002008 if (mFrameProcessor.get() != nullptr) {
2009 mFrameProcessor->removeListener(
2010 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MIN_ID,
2011 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MAX_ID, /*listener*/this);
2012 mFrameProcessor->requestExit();
2013 ALOGV("Camera %s: Waiting for threads", mCameraIdStr.string());
2014 mFrameProcessor->join();
2015 ALOGV("Camera %s: Disconnecting device", mCameraIdStr.string());
2016 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07002017
2018 // WORKAROUND: HAL refuses to disconnect while there's streams in flight
2019 {
Emilian Peev6b51d7d2018-07-23 11:41:44 +01002020 int64_t lastFrameNumber;
Igor Murashkine7ee7632013-06-11 18:10:18 -07002021 status_t code;
Emilian Peev6b51d7d2018-07-23 11:41:44 +01002022 if ((code = mDevice->flush(&lastFrameNumber)) != OK) {
2023 ALOGE("%s: flush failed with code 0x%x", __FUNCTION__, code);
2024 }
2025
Igor Murashkine7ee7632013-06-11 18:10:18 -07002026 if ((code = mDevice->waitUntilDrained()) != OK) {
2027 ALOGE("%s: waitUntilDrained failed with code 0x%x", __FUNCTION__,
2028 code);
2029 }
2030 }
2031
Emilian Peev2f5d6012022-01-19 16:16:50 -08002032 {
2033 Mutex::Autolock l(mCompositeLock);
2034 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
2035 auto ret = mCompositeStreamMap.valueAt(i)->deleteInternalStreams();
2036 if (ret != OK) {
2037 ALOGE("%s: Failed removing composite stream %s (%d)", __FUNCTION__,
2038 strerror(-ret), ret);
2039 }
Emilian Peev5e4c7322019-10-22 14:20:52 -07002040 }
Emilian Peev2f5d6012022-01-19 16:16:50 -08002041 mCompositeStreamMap.clear();
Emilian Peev5e4c7322019-10-22 14:20:52 -07002042 }
Emilian Peev5e4c7322019-10-22 14:20:52 -07002043
Igor Murashkine7ee7632013-06-11 18:10:18 -07002044 Camera2ClientBase::detachDevice();
Shuzhen Wang316781a2020-08-18 18:11:01 -07002045
2046 int32_t closeLatencyMs = ns2ms(systemTime() - startTime);
2047 CameraServiceProxyWrapper::logClose(mCameraIdStr, closeLatencyMs);
Igor Murashkine7ee7632013-06-11 18:10:18 -07002048}
2049
2050/** Device-related methods */
Jianing Weicb0652e2014-03-12 18:29:36 -07002051void CameraDeviceClient::onResultAvailable(const CaptureResult& result) {
Igor Murashkine7ee7632013-06-11 18:10:18 -07002052 ATRACE_CALL();
2053 ALOGV("%s", __FUNCTION__);
2054
Igor Murashkin4fb55c12013-08-29 17:43:01 -07002055 // Thread-safe. No lock necessary.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002056 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = mRemoteCallback;
Igor Murashkin4fb55c12013-08-29 17:43:01 -07002057 if (remoteCb != NULL) {
Shuzhen Wang5c22c152017-12-31 17:12:25 -08002058 remoteCb->onResultReceived(result.mMetadata, result.mResultExtras,
2059 result.mPhysicalMetadatas);
Igor Murashkine7ee7632013-06-11 18:10:18 -07002060 }
Emilian Peev538c90e2018-12-17 18:03:19 +00002061
Emilian Peev2f5d6012022-01-19 16:16:50 -08002062 // Access to the composite stream map must be synchronized
2063 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +00002064 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
2065 mCompositeStreamMap.valueAt(i)->onResultAvailable(result);
2066 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07002067}
2068
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002069binder::Status CameraDeviceClient::checkPidStatus(const char* checkLocation) {
Eino-Ville Talvala6192b892016-04-04 12:31:18 -07002070 if (mDisconnected) {
2071 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED,
2072 "The camera device has been disconnected");
2073 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002074 status_t res = checkPid(checkLocation);
2075 return (res == OK) ? binder::Status::ok() :
2076 STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
2077 "Attempt to use camera from a different process than original client");
2078}
2079
Igor Murashkine7ee7632013-06-11 18:10:18 -07002080// TODO: move to Camera2ClientBase
2081bool CameraDeviceClient::enforceRequestPermissions(CameraMetadata& metadata) {
2082
Jayant Chowdhary12361932018-08-27 14:46:13 -07002083 const int pid = CameraThreadState::getCallingPid();
Igor Murashkine7ee7632013-06-11 18:10:18 -07002084 const int selfPid = getpid();
2085 camera_metadata_entry_t entry;
2086
2087 /**
2088 * Mixin default important security values
2089 * - android.led.transmit = defaulted ON
2090 */
2091 CameraMetadata staticInfo = mDevice->info();
2092 entry = staticInfo.find(ANDROID_LED_AVAILABLE_LEDS);
2093 for(size_t i = 0; i < entry.count; ++i) {
2094 uint8_t led = entry.data.u8[i];
2095
2096 switch(led) {
2097 case ANDROID_LED_AVAILABLE_LEDS_TRANSMIT: {
2098 uint8_t transmitDefault = ANDROID_LED_TRANSMIT_ON;
2099 if (!metadata.exists(ANDROID_LED_TRANSMIT)) {
2100 metadata.update(ANDROID_LED_TRANSMIT,
2101 &transmitDefault, 1);
2102 }
2103 break;
2104 }
2105 }
2106 }
2107
2108 // We can do anything!
2109 if (pid == selfPid) {
2110 return true;
2111 }
2112
2113 /**
2114 * Permission check special fields in the request
2115 * - android.led.transmit = android.permission.CAMERA_DISABLE_TRANSMIT
2116 */
2117 entry = metadata.find(ANDROID_LED_TRANSMIT);
2118 if (entry.count > 0 && entry.data.u8[0] != ANDROID_LED_TRANSMIT_ON) {
2119 String16 permissionString =
2120 String16("android.permission.CAMERA_DISABLE_TRANSMIT_LED");
2121 if (!checkCallingPermission(permissionString)) {
Jayant Chowdhary12361932018-08-27 14:46:13 -07002122 const int uid = CameraThreadState::getCallingUid();
Igor Murashkine7ee7632013-06-11 18:10:18 -07002123 ALOGE("Permission Denial: "
2124 "can't disable transmit LED pid=%d, uid=%d", pid, uid);
2125 return false;
2126 }
2127 }
2128
2129 return true;
2130}
2131
Shuzhen Wang610d7b82022-02-08 14:37:22 -08002132status_t CameraDeviceClient::getRotationTransformLocked(int mirrorMode,
2133 int32_t* transform) {
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07002134 ALOGV("%s: begin", __FUNCTION__);
2135
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07002136 const CameraMetadata& staticInfo = mDevice->info();
Shuzhen Wang610d7b82022-02-08 14:37:22 -08002137 return CameraUtils::getRotationTransform(staticInfo, mirrorMode, transform);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07002138}
2139
Emilian Peevf4816702020-04-03 15:44:51 -07002140binder::Status CameraDeviceClient::mapRequestTemplate(int templateId,
2141 camera_request_template_t* tempId /*out*/) {
2142 binder::Status ret = binder::Status::ok();
2143
2144 if (tempId == nullptr) {
2145 ret = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
2146 "Camera %s: Invalid template argument", mCameraIdStr.string());
2147 return ret;
2148 }
2149 switch(templateId) {
2150 case ICameraDeviceUser::TEMPLATE_PREVIEW:
2151 *tempId = camera_request_template_t::CAMERA_TEMPLATE_PREVIEW;
2152 break;
2153 case ICameraDeviceUser::TEMPLATE_RECORD:
2154 *tempId = camera_request_template_t::CAMERA_TEMPLATE_VIDEO_RECORD;
2155 break;
2156 case ICameraDeviceUser::TEMPLATE_STILL_CAPTURE:
2157 *tempId = camera_request_template_t::CAMERA_TEMPLATE_STILL_CAPTURE;
2158 break;
2159 case ICameraDeviceUser::TEMPLATE_VIDEO_SNAPSHOT:
2160 *tempId = camera_request_template_t::CAMERA_TEMPLATE_VIDEO_SNAPSHOT;
2161 break;
2162 case ICameraDeviceUser::TEMPLATE_ZERO_SHUTTER_LAG:
2163 *tempId = camera_request_template_t::CAMERA_TEMPLATE_ZERO_SHUTTER_LAG;
2164 break;
2165 case ICameraDeviceUser::TEMPLATE_MANUAL:
2166 *tempId = camera_request_template_t::CAMERA_TEMPLATE_MANUAL;
2167 break;
2168 default:
2169 ret = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
2170 "Camera %s: Template ID %d is invalid or not supported",
2171 mCameraIdStr.string(), templateId);
2172 return ret;
2173 }
2174
2175 return ret;
2176}
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08002177
2178const CameraMetadata &CameraDeviceClient::getStaticInfo(const String8 &cameraId) {
2179 if (mDevice->getId() == cameraId) {
2180 return mDevice->info();
2181 }
2182 return mDevice->infoPhysical(cameraId);
2183}
2184
2185bool CameraDeviceClient::isUltraHighResolutionSensor(const String8 &cameraId) {
2186 const CameraMetadata &deviceInfo = getStaticInfo(cameraId);
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07002187 return SessionConfigurationUtils::isUltraHighResolutionSensor(deviceInfo);
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08002188}
2189
2190bool CameraDeviceClient::isSensorPixelModeConsistent(
2191 const std::list<int> &streamIdList, const CameraMetadata &settings) {
2192 // First we get the sensorPixelMode from the settings metadata.
2193 int32_t sensorPixelMode = ANDROID_SENSOR_PIXEL_MODE_DEFAULT;
2194 camera_metadata_ro_entry sensorPixelModeEntry = settings.find(ANDROID_SENSOR_PIXEL_MODE);
2195 if (sensorPixelModeEntry.count != 0) {
2196 sensorPixelMode = sensorPixelModeEntry.data.u8[0];
2197 if (sensorPixelMode != ANDROID_SENSOR_PIXEL_MODE_DEFAULT &&
2198 sensorPixelMode != ANDROID_SENSOR_PIXEL_MODE_MAXIMUM_RESOLUTION) {
2199 ALOGE("%s: Request sensor pixel mode not is not one of the valid values %d",
2200 __FUNCTION__, sensorPixelMode);
2201 return false;
2202 }
2203 }
2204 // Check whether each stream has max resolution allowed.
2205 bool consistent = true;
2206 for (auto it : streamIdList) {
2207 auto const streamInfoIt = mStreamInfoMap.find(it);
2208 if (streamInfoIt == mStreamInfoMap.end()) {
2209 ALOGE("%s: stream id %d not created, skipping", __FUNCTION__, it);
2210 return false;
2211 }
2212 consistent =
2213 streamInfoIt->second.sensorPixelModesUsed.find(sensorPixelMode) !=
2214 streamInfoIt->second.sensorPixelModesUsed.end();
2215 if (!consistent) {
2216 ALOGE("sensorPixelMode used %i not consistent with configured modes", sensorPixelMode);
2217 for (auto m : streamInfoIt->second.sensorPixelModesUsed) {
2218 ALOGE("sensor pixel mode used list: %i", m);
2219 }
2220 break;
2221 }
2222 }
2223
2224 return consistent;
2225}
2226
Igor Murashkine7ee7632013-06-11 18:10:18 -07002227} // namespace android