blob: d80b6ec842cd2ea2ea9d9ef3369a5783018bdec7 [file] [log] [blame]
Igor Murashkine7ee7632013-06-11 18:10:18 -07001/*
Shuzhen Wangc28189a2017-11-27 23:05:10 -08002 * Copyright (C) 2013-2018 The Android Open Source Project
Igor Murashkine7ee7632013-06-11 18:10:18 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "CameraDeviceClient"
18#define ATRACE_TAG ATRACE_TAG_CAMERA
Jianing Weicb0652e2014-03-12 18:29:36 -070019//#define LOG_NDEBUG 0
Igor Murashkine7ee7632013-06-11 18:10:18 -070020
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070021#include <cutils/properties.h>
Jayant Chowdhary12361932018-08-27 14:46:13 -070022#include <utils/CameraThreadState.h>
Igor Murashkine7ee7632013-06-11 18:10:18 -070023#include <utils/Log.h>
Colin Crossb8a9dbb2020-08-27 04:12:26 +000024#include <utils/SessionConfigurationUtils.h>
Igor Murashkine7ee7632013-06-11 18:10:18 -070025#include <utils/Trace.h>
Igor Murashkine7ee7632013-06-11 18:10:18 -070026#include <gui/Surface.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070027#include <camera/camera2/CaptureRequest.h>
Ruben Brunk5698d442014-06-18 10:39:40 -070028#include <camera/CameraUtils.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070029
30#include "common/CameraDeviceBase.h"
Emilian Peev35ae8262018-11-08 13:11:32 +000031#include "device3/Camera3Device.h"
32#include "device3/Camera3OutputStream.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070033#include "api2/CameraDeviceClient.h"
34
Emilian Peev00420d22018-02-05 21:33:13 +000035#include <camera_metadata_hidden.h>
36
Emilian Peev538c90e2018-12-17 18:03:19 +000037#include "DepthCompositeStream.h"
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -080038#include "HeicCompositeStream.h"
Emilian Peev538c90e2018-12-17 18:03:19 +000039
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080040// Convenience methods for constructing binder::Status objects for error returns
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070041
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080042#define STATUS_ERROR(errorCode, errorString) \
43 binder::Status::fromServiceSpecificError(errorCode, \
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -080044 String8::format("%s:%d: %s", __FUNCTION__, __LINE__, errorString))
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080045
46#define STATUS_ERROR_FMT(errorCode, errorString, ...) \
47 binder::Status::fromServiceSpecificError(errorCode, \
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -080048 String8::format("%s:%d: " errorString, __FUNCTION__, __LINE__, \
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080049 __VA_ARGS__))
Igor Murashkine7ee7632013-06-11 18:10:18 -070050
51namespace android {
52using namespace camera2;
Austin Borgerea931242021-12-13 23:10:41 +000053using namespace camera3;
Emilian Peevf4816702020-04-03 15:44:51 -070054using camera3::camera_stream_rotation_t::CAMERA_STREAM_ROTATION_0;
Igor Murashkine7ee7632013-06-11 18:10:18 -070055
56CameraDeviceClientBase::CameraDeviceClientBase(
57 const sp<CameraService>& cameraService,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080058 const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback,
Igor Murashkine7ee7632013-06-11 18:10:18 -070059 const String16& clientPackageName,
Jayant Chowdharyeb0169f2022-01-31 00:00:02 -080060 bool systemNativeClient,
Jooyung Han3f9a3b42020-01-23 12:27:18 +090061 const std::optional<String16>& clientFeatureId,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080062 const String8& cameraId,
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -080063 int api1CameraId,
Igor Murashkine7ee7632013-06-11 18:10:18 -070064 int cameraFacing,
Emilian Peev8b64f282021-03-25 16:49:57 -070065 int sensorOrientation,
Igor Murashkine7ee7632013-06-11 18:10:18 -070066 int clientPid,
67 uid_t clientUid,
Austin Borger18b30a72022-10-27 12:20:29 -070068 int servicePid,
69 bool overrideToPortrait) :
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,
Austin Borger18b30a72022-10-27 12:20:29 -070080 servicePid,
81 overrideToPortrait),
Igor Murashkine7ee7632013-06-11 18:10:18 -070082 mRemoteCallback(remoteCallback) {
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -080083 // We don't need it for API2 clients, but Camera2ClientBase requires it.
84 (void) api1CameraId;
Igor Murashkine7ee7632013-06-11 18:10:18 -070085}
Igor Murashkine7ee7632013-06-11 18:10:18 -070086
87// Interface used by CameraService
88
89CameraDeviceClient::CameraDeviceClient(const sp<CameraService>& cameraService,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080090 const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback,
Austin Borger74fca042022-05-23 12:41:21 -070091 std::shared_ptr<CameraServiceProxyWrapper> cameraServiceProxyWrapper,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080092 const String16& clientPackageName,
Jayant Chowdharyeb0169f2022-01-31 00:00:02 -080093 bool systemNativeClient,
Jooyung Han3f9a3b42020-01-23 12:27:18 +090094 const std::optional<String16>& clientFeatureId,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080095 const String8& cameraId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080096 int cameraFacing,
Emilian Peev8b64f282021-03-25 16:49:57 -070097 int sensorOrientation,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080098 int clientPid,
99 uid_t clientUid,
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700100 int servicePid,
Austin Borger18b30a72022-10-27 12:20:29 -0700101 bool overrideForPerfClass,
102 bool overrideToPortrait) :
Austin Borger74fca042022-05-23 12:41:21 -0700103 Camera2ClientBase(cameraService, remoteCallback, cameraServiceProxyWrapper, clientPackageName,
104 systemNativeClient, clientFeatureId, cameraId, /*API1 camera ID*/ -1, cameraFacing,
Austin Borger18b30a72022-10-27 12:20:29 -0700105 sensorOrientation, clientPid, clientUid, servicePid, overrideForPerfClass,
106 overrideToPortrait),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700107 mInputStream(),
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700108 mStreamingRequestId(REQUEST_ID_NONE),
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700109 mRequestIdCounter(0),
110 mOverrideForPerfClass(overrideForPerfClass) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700111
112 ATRACE_CALL();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800113 ALOGI("CameraDeviceClient %s: Opened", cameraId.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700114}
115
Emilian Peevbd8c5032018-02-14 23:05:40 +0000116status_t CameraDeviceClient::initialize(sp<CameraProviderManager> manager,
117 const String8& monitorTags) {
118 return initializeImpl(manager, monitorTags);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800119}
120
121template<typename TProviderPtr>
Emilian Peevbd8c5032018-02-14 23:05:40 +0000122status_t CameraDeviceClient::initializeImpl(TProviderPtr providerPtr, const String8& monitorTags) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700123 ATRACE_CALL();
124 status_t res;
125
Emilian Peevbd8c5032018-02-14 23:05:40 +0000126 res = Camera2ClientBase::initialize(providerPtr, monitorTags);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700127 if (res != OK) {
128 return res;
129 }
130
131 String8 threadName;
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -0700132 mFrameProcessor = new FrameProcessorBase(mDevice);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800133 threadName = String8::format("CDU-%s-FrameProc", mCameraIdStr.string());
Austin Borger7b129542022-06-09 13:23:06 -0700134 res = mFrameProcessor->run(threadName.string());
135 if (res != OK) {
136 ALOGE("%s: Unable to start frame processor thread: %s (%d)",
137 __FUNCTION__, strerror(-res), res);
138 return res;
139 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700140
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800141 mFrameProcessor->registerListener(camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MIN_ID,
142 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MAX_ID,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -0800143 /*listener*/this,
Zhijun He25a0aef2014-06-25 11:40:02 -0700144 /*sendPartials*/true);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700145
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800146 const CameraMetadata &deviceInfo = mDevice->info();
147 camera_metadata_ro_entry_t physicalKeysEntry = deviceInfo.find(
Emilian Peev00420d22018-02-05 21:33:13 +0000148 ANDROID_REQUEST_AVAILABLE_PHYSICAL_CAMERA_REQUEST_KEYS);
149 if (physicalKeysEntry.count > 0) {
150 mSupportedPhysicalRequestKeys.insert(mSupportedPhysicalRequestKeys.begin(),
151 physicalKeysEntry.data.i32,
152 physicalKeysEntry.data.i32 + physicalKeysEntry.count);
153 }
154
Emilian Peev2295df72021-11-12 18:14:10 -0800155 auto entry = deviceInfo.find(ANDROID_REQUEST_AVAILABLE_CAPABILITIES);
156 mDynamicProfileMap.emplace(
157 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD,
158 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD);
159 if (entry.count > 0) {
Shuzhen Wangc8ab4522021-12-14 20:12:42 -0800160 const auto it = std::find(entry.data.u8, entry.data.u8 + entry.count,
Emilian Peev2295df72021-11-12 18:14:10 -0800161 ANDROID_REQUEST_AVAILABLE_CAPABILITIES_DYNAMIC_RANGE_TEN_BIT);
Shuzhen Wangc8ab4522021-12-14 20:12:42 -0800162 if (it != entry.data.u8 + entry.count) {
Emilian Peev2295df72021-11-12 18:14:10 -0800163 entry = deviceInfo.find(ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP);
Emilian Peevc81a7592022-02-14 17:38:18 -0800164 if (entry.count > 0 || ((entry.count % 3) != 0)) {
165 int64_t standardBitmap =
166 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD;
167 for (size_t i = 0; i < entry.count; i += 3) {
168 if (entry.data.i64[i] !=
Emilian Peev2295df72021-11-12 18:14:10 -0800169 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD) {
Emilian Peevc81a7592022-02-14 17:38:18 -0800170 mDynamicProfileMap.emplace(entry.data.i64[i], entry.data.i64[i+1]);
171 if ((entry.data.i64[i+1] == 0) || (entry.data.i64[i+1] &
Emilian Peev2295df72021-11-12 18:14:10 -0800172 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD)) {
Emilian Peevc81a7592022-02-14 17:38:18 -0800173 standardBitmap |= entry.data.i64[i];
Emilian Peev2295df72021-11-12 18:14:10 -0800174 }
175 } else {
Emilian Peevc81a7592022-02-14 17:38:18 -0800176 ALOGE("%s: Device %s includes unexpected profile entry: 0x%" PRIx64 "!",
177 __FUNCTION__, mCameraIdStr.c_str(), entry.data.i64[i]);
Emilian Peev2295df72021-11-12 18:14:10 -0800178 }
179 }
Emilian Peevef715e22022-05-19 17:44:33 -0700180 mDynamicProfileMap[ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD] =
181 standardBitmap;
Emilian Peev2295df72021-11-12 18:14:10 -0800182 } else {
183 ALOGE("%s: Device %s supports 10-bit output but doesn't include a dynamic range"
184 " profile map!", __FUNCTION__, mCameraIdStr.c_str());
185 }
186 }
187 }
188
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700189 mProviderManager = providerPtr;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800190 // Cache physical camera ids corresponding to this device and also the high
191 // resolution sensors in this device + physical camera ids
192 mProviderManager->isLogicalCamera(mCameraIdStr.string(), &mPhysicalCameraIds);
193 if (isUltraHighResolutionSensor(mCameraIdStr)) {
194 mHighResolutionSensors.insert(mCameraIdStr.string());
195 }
196 for (auto &physicalId : mPhysicalCameraIds) {
197 if (isUltraHighResolutionSensor(String8(physicalId.c_str()))) {
198 mHighResolutionSensors.insert(physicalId.c_str());
199 }
200 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700201 return OK;
202}
203
204CameraDeviceClient::~CameraDeviceClient() {
205}
206
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800207binder::Status CameraDeviceClient::submitRequest(
208 const hardware::camera2::CaptureRequest& request,
209 bool streaming,
210 /*out*/
211 hardware::camera2::utils::SubmitInfo *submitInfo) {
212 std::vector<hardware::camera2::CaptureRequest> requestList = { request };
213 return submitRequestList(requestList, streaming, submitInfo);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700214}
215
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800216binder::Status CameraDeviceClient::insertGbpLocked(const sp<IGraphicBufferProducer>& gbp,
Emilian Peevf873aa52018-01-26 14:58:28 +0000217 SurfaceMap* outSurfaceMap, Vector<int32_t>* outputStreamIds, int32_t *currentStreamId) {
Emilian Peev538c90e2018-12-17 18:03:19 +0000218 int compositeIdx;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800219 int idx = mStreamMap.indexOfKey(IInterface::asBinder(gbp));
220
Emilian Peev2f5d6012022-01-19 16:16:50 -0800221 Mutex::Autolock l(mCompositeLock);
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800222 // Trying to submit request with surface that wasn't created
223 if (idx == NAME_NOT_FOUND) {
224 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
225 " we have not called createStream on",
226 __FUNCTION__, mCameraIdStr.string());
227 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
228 "Request targets Surface that is not part of current capture session");
Emilian Peev538c90e2018-12-17 18:03:19 +0000229 } else if ((compositeIdx = mCompositeStreamMap.indexOfKey(IInterface::asBinder(gbp)))
230 != NAME_NOT_FOUND) {
231 mCompositeStreamMap.valueAt(compositeIdx)->insertGbp(outSurfaceMap, outputStreamIds,
232 currentStreamId);
233 return binder::Status::ok();
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800234 }
235
236 const StreamSurfaceId& streamSurfaceId = mStreamMap.valueAt(idx);
237 if (outSurfaceMap->find(streamSurfaceId.streamId()) == outSurfaceMap->end()) {
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800238 outputStreamIds->push_back(streamSurfaceId.streamId());
239 }
240 (*outSurfaceMap)[streamSurfaceId.streamId()].push_back(streamSurfaceId.surfaceId());
241
242 ALOGV("%s: Camera %s: Appending output stream %d surface %d to request",
243 __FUNCTION__, mCameraIdStr.string(), streamSurfaceId.streamId(),
244 streamSurfaceId.surfaceId());
245
Emilian Peevf873aa52018-01-26 14:58:28 +0000246 if (currentStreamId != nullptr) {
247 *currentStreamId = streamSurfaceId.streamId();
248 }
249
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800250 return binder::Status::ok();
251}
252
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800253static std::list<int> getIntersection(const std::unordered_set<int> &streamIdsForThisCamera,
254 const Vector<int> &streamIdsForThisRequest) {
255 std::list<int> intersection;
256 for (auto &streamId : streamIdsForThisRequest) {
257 if (streamIdsForThisCamera.find(streamId) != streamIdsForThisCamera.end()) {
258 intersection.emplace_back(streamId);
259 }
260 }
261 return intersection;
262}
263
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800264binder::Status CameraDeviceClient::submitRequestList(
265 const std::vector<hardware::camera2::CaptureRequest>& requests,
266 bool streaming,
267 /*out*/
268 hardware::camera2::utils::SubmitInfo *submitInfo) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700269 ATRACE_CALL();
Mark Salyzyn50468412014-06-18 16:33:43 -0700270 ALOGV("%s-start of function. Request list size %zu", __FUNCTION__, requests.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700271
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800272 binder::Status res = binder::Status::ok();
273 status_t err;
274 if ( !(res = checkPidStatus(__FUNCTION__) ).isOk()) {
275 return res;
276 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700277
278 Mutex::Autolock icl(mBinderSerializationLock);
279
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800280 if (!mDevice.get()) {
281 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
282 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700283
284 if (requests.empty()) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800285 ALOGE("%s: Camera %s: Sent null request. Rejecting request.",
286 __FUNCTION__, mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800287 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Empty request list");
Jianing Wei90e59c92014-03-12 18:29:36 -0700288 }
289
Emilian Peevaebbe412018-01-15 13:53:24 +0000290 List<const CameraDeviceBase::PhysicalCameraSettingsList> metadataRequestList;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700291 std::list<const SurfaceMap> surfaceMapList;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800292 submitInfo->mRequestId = mRequestIdCounter;
Jianing Wei90e59c92014-03-12 18:29:36 -0700293 uint32_t loopCounter = 0;
294
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800295 for (auto&& request: requests) {
296 if (request.mIsReprocess) {
Chien-Yu Chened0412e2015-04-27 15:04:22 -0700297 if (!mInputStream.configured) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800298 ALOGE("%s: Camera %s: no input stream is configured.", __FUNCTION__,
299 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800300 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800301 "No input configured for camera %s but request is for reprocessing",
302 mCameraIdStr.string());
Chien-Yu Chened0412e2015-04-27 15:04:22 -0700303 } else if (streaming) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800304 ALOGE("%s: Camera %s: streaming reprocess requests not supported.", __FUNCTION__,
305 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800306 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
307 "Repeating reprocess requests not supported");
Emilian Peevaebbe412018-01-15 13:53:24 +0000308 } else if (request.mPhysicalCameraSettings.size() > 1) {
309 ALOGE("%s: Camera %s: reprocess requests not supported for "
310 "multiple physical cameras.", __FUNCTION__,
311 mCameraIdStr.string());
312 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
313 "Reprocess requests not supported for multiple cameras");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700314 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700315 }
316
Emilian Peevaebbe412018-01-15 13:53:24 +0000317 if (request.mPhysicalCameraSettings.empty()) {
318 ALOGE("%s: Camera %s: request doesn't contain any settings.", __FUNCTION__,
319 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800320 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
Emilian Peevaebbe412018-01-15 13:53:24 +0000321 "Request doesn't contain any settings");
322 }
323
324 //The first capture settings should always match the logical camera id
325 String8 logicalId(request.mPhysicalCameraSettings.begin()->id.c_str());
326 if (mDevice->getId() != logicalId) {
327 ALOGE("%s: Camera %s: Invalid camera request settings.", __FUNCTION__,
328 mCameraIdStr.string());
329 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
330 "Invalid camera request settings");
331 }
332
Emilian Peevaebbe412018-01-15 13:53:24 +0000333 if (request.mSurfaceList.isEmpty() && request.mStreamIdxList.size() == 0) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800334 ALOGE("%s: Camera %s: Requests must have at least one surface target. "
335 "Rejecting request.", __FUNCTION__, mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800336 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
337 "Request has no output targets");
Jianing Wei90e59c92014-03-12 18:29:36 -0700338 }
339
Jianing Wei90e59c92014-03-12 18:29:36 -0700340 /**
Shuzhen Wang0129d522016-10-30 22:43:41 -0700341 * Write in the output stream IDs and map from stream ID to surface ID
342 * which we calculate from the capture request's list of surface target
Jianing Wei90e59c92014-03-12 18:29:36 -0700343 */
Shuzhen Wang0129d522016-10-30 22:43:41 -0700344 SurfaceMap surfaceMap;
Jianing Wei90e59c92014-03-12 18:29:36 -0700345 Vector<int32_t> outputStreamIds;
Emilian Peevf873aa52018-01-26 14:58:28 +0000346 std::vector<std::string> requestedPhysicalIds;
Emilian Peevc81a7592022-02-14 17:38:18 -0800347 int64_t dynamicProfileBitmap = 0;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800348 if (request.mSurfaceList.size() > 0) {
Chih-Hung Hsieh3ef324d2018-12-11 11:48:12 -0800349 for (const sp<Surface>& surface : request.mSurfaceList) {
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800350 if (surface == 0) continue;
Jianing Wei90e59c92014-03-12 18:29:36 -0700351
Emilian Peevf873aa52018-01-26 14:58:28 +0000352 int32_t streamId;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800353 sp<IGraphicBufferProducer> gbp = surface->getIGraphicBufferProducer();
Emilian Peevf873aa52018-01-26 14:58:28 +0000354 res = insertGbpLocked(gbp, &surfaceMap, &outputStreamIds, &streamId);
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800355 if (!res.isOk()) {
356 return res;
357 }
Emilian Peevf873aa52018-01-26 14:58:28 +0000358
359 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
360 if (index >= 0) {
361 String8 requestedPhysicalId(
362 mConfiguredOutputs.valueAt(index).getPhysicalCameraId());
363 requestedPhysicalIds.push_back(requestedPhysicalId.string());
Emilian Peev2295df72021-11-12 18:14:10 -0800364 dynamicProfileBitmap |=
365 mConfiguredOutputs.valueAt(index).getDynamicRangeProfile();
Emilian Peevf873aa52018-01-26 14:58:28 +0000366 } else {
367 ALOGW("%s: Output stream Id not found among configured outputs!", __FUNCTION__);
368 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700369 }
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800370 } else {
371 for (size_t i = 0; i < request.mStreamIdxList.size(); i++) {
372 int streamId = request.mStreamIdxList.itemAt(i);
373 int surfaceIdx = request.mSurfaceIdxList.itemAt(i);
Jianing Wei90e59c92014-03-12 18:29:36 -0700374
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800375 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
376 if (index < 0) {
377 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
378 " we have not called createStream on: stream %d",
379 __FUNCTION__, mCameraIdStr.string(), streamId);
380 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
381 "Request targets Surface that is not part of current capture session");
382 }
383
384 const auto& gbps = mConfiguredOutputs.valueAt(index).getGraphicBufferProducers();
385 if ((size_t)surfaceIdx >= gbps.size()) {
386 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
387 " we have not called createStream on: stream %d, surfaceIdx %d",
388 __FUNCTION__, mCameraIdStr.string(), streamId, surfaceIdx);
389 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
390 "Request targets Surface has invalid surface index");
391 }
392
Emilian Peevf873aa52018-01-26 14:58:28 +0000393 res = insertGbpLocked(gbps[surfaceIdx], &surfaceMap, &outputStreamIds, nullptr);
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800394 if (!res.isOk()) {
395 return res;
396 }
Emilian Peevf873aa52018-01-26 14:58:28 +0000397
398 String8 requestedPhysicalId(
399 mConfiguredOutputs.valueAt(index).getPhysicalCameraId());
400 requestedPhysicalIds.push_back(requestedPhysicalId.string());
Emilian Peev2295df72021-11-12 18:14:10 -0800401 dynamicProfileBitmap |=
402 mConfiguredOutputs.valueAt(index).getDynamicRangeProfile();
403 }
404 }
405
406 if (dynamicProfileBitmap !=
407 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD) {
408 for (int i = ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD;
409 i < ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_MAX; i <<= 1) {
410 if ((dynamicProfileBitmap & i) == 0) {
411 continue;
412 }
413
414 const auto& it = mDynamicProfileMap.find(i);
415 if (it != mDynamicProfileMap.end()) {
416 if ((it->second == 0) ||
417 ((it->second & dynamicProfileBitmap) == dynamicProfileBitmap)) {
418 continue;
419 } else {
420 ALOGE("%s: Camera %s: Tried to submit a request with a surfaces that"
421 " reference an unsupported dynamic range profile combination"
Emilian Peevc81a7592022-02-14 17:38:18 -0800422 " 0x%" PRIx64 "!", __FUNCTION__, mCameraIdStr.string(),
Emilian Peev2295df72021-11-12 18:14:10 -0800423 dynamicProfileBitmap);
424 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
425 "Request targets an unsupported dynamic range profile"
426 " combination");
427 }
428 } else {
429 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
430 " references unsupported dynamic range profile 0x%x!",
431 __FUNCTION__, mCameraIdStr.string(), i);
432 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
433 "Request targets 10-bit Surface with unsupported dynamic range"
434 " profile");
435 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700436 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700437 }
438
Emilian Peevf873aa52018-01-26 14:58:28 +0000439 CameraDeviceBase::PhysicalCameraSettingsList physicalSettingsList;
440 for (const auto& it : request.mPhysicalCameraSettings) {
Emilian Peev00420d22018-02-05 21:33:13 +0000441 if (it.settings.isEmpty()) {
442 ALOGE("%s: Camera %s: Sent empty metadata packet. Rejecting request.",
443 __FUNCTION__, mCameraIdStr.string());
444 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
445 "Request settings are empty");
446 }
447
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800448 // Check whether the physical / logical stream has settings
449 // consistent with the sensor pixel mode(s) it was configured with.
450 // mCameraIdToStreamSet will only have ids that are high resolution
451 const auto streamIdSetIt = mHighResolutionCameraIdToStreamIdSet.find(it.id);
452 if (streamIdSetIt != mHighResolutionCameraIdToStreamIdSet.end()) {
453 std::list<int> streamIdsUsedInRequest = getIntersection(streamIdSetIt->second,
454 outputStreamIds);
455 if (!request.mIsReprocess &&
456 !isSensorPixelModeConsistent(streamIdsUsedInRequest, it.settings)) {
457 ALOGE("%s: Camera %s: Request settings CONTROL_SENSOR_PIXEL_MODE not "
458 "consistent with configured streams. Rejecting request.",
459 __FUNCTION__, it.id.c_str());
460 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
461 "Request settings CONTROL_SENSOR_PIXEL_MODE are not consistent with "
462 "streams configured");
463 }
464 }
465
Emilian Peevf873aa52018-01-26 14:58:28 +0000466 String8 physicalId(it.id.c_str());
Shuzhen Wang911c6a32021-10-27 13:36:03 -0700467 bool hasTestPatternModePhysicalKey = std::find(mSupportedPhysicalRequestKeys.begin(),
468 mSupportedPhysicalRequestKeys.end(), ANDROID_SENSOR_TEST_PATTERN_MODE) !=
469 mSupportedPhysicalRequestKeys.end();
470 bool hasTestPatternDataPhysicalKey = std::find(mSupportedPhysicalRequestKeys.begin(),
471 mSupportedPhysicalRequestKeys.end(), ANDROID_SENSOR_TEST_PATTERN_DATA) !=
472 mSupportedPhysicalRequestKeys.end();
Emilian Peevf873aa52018-01-26 14:58:28 +0000473 if (physicalId != mDevice->getId()) {
474 auto found = std::find(requestedPhysicalIds.begin(), requestedPhysicalIds.end(),
475 it.id);
476 if (found == requestedPhysicalIds.end()) {
477 ALOGE("%s: Camera %s: Physical camera id: %s not part of attached outputs.",
478 __FUNCTION__, mCameraIdStr.string(), physicalId.string());
479 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
480 "Invalid physical camera id");
481 }
Emilian Peev00420d22018-02-05 21:33:13 +0000482
483 if (!mSupportedPhysicalRequestKeys.empty()) {
484 // Filter out any unsupported physical request keys.
485 CameraMetadata filteredParams(mSupportedPhysicalRequestKeys.size());
486 camera_metadata_t *meta = const_cast<camera_metadata_t *>(
487 filteredParams.getAndLock());
488 set_camera_metadata_vendor_id(meta, mDevice->getVendorTagId());
489 filteredParams.unlock(meta);
490
491 for (const auto& keyIt : mSupportedPhysicalRequestKeys) {
492 camera_metadata_ro_entry entry = it.settings.find(keyIt);
493 if (entry.count > 0) {
494 filteredParams.update(entry);
495 }
496 }
497
Shuzhen Wang911c6a32021-10-27 13:36:03 -0700498 physicalSettingsList.push_back({it.id, filteredParams,
499 hasTestPatternModePhysicalKey, hasTestPatternDataPhysicalKey});
Emilian Peev00420d22018-02-05 21:33:13 +0000500 }
501 } else {
502 physicalSettingsList.push_back({it.id, it.settings});
Emilian Peevf873aa52018-01-26 14:58:28 +0000503 }
Emilian Peevf873aa52018-01-26 14:58:28 +0000504 }
505
506 if (!enforceRequestPermissions(physicalSettingsList.begin()->metadata)) {
507 // Callee logs
508 return STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
509 "Caller does not have permission to change restricted controls");
510 }
511
Emilian Peevaebbe412018-01-15 13:53:24 +0000512 physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_OUTPUT_STREAMS,
513 &outputStreamIds[0], outputStreamIds.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700514
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800515 if (request.mIsReprocess) {
Emilian Peevaebbe412018-01-15 13:53:24 +0000516 physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_INPUT_STREAMS,
517 &mInputStream.id, 1);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700518 }
519
Emilian Peevaebbe412018-01-15 13:53:24 +0000520 physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_ID,
521 &(submitInfo->mRequestId), /*size*/1);
Jianing Wei90e59c92014-03-12 18:29:36 -0700522 loopCounter++; // loopCounter starts from 1
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800523 ALOGV("%s: Camera %s: Creating request with ID %d (%d of %zu)",
524 __FUNCTION__, mCameraIdStr.string(), submitInfo->mRequestId,
525 loopCounter, requests.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700526
Emilian Peevaebbe412018-01-15 13:53:24 +0000527 metadataRequestList.push_back(physicalSettingsList);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700528 surfaceMapList.push_back(surfaceMap);
Shuzhen Wangd26b1862022-03-07 12:05:05 -0800529
Shuzhen Wang9372b0b2022-05-11 18:55:19 -0700530 // Save certain CaptureRequest settings
Shuzhen Wangd26b1862022-03-07 12:05:05 -0800531 if (!request.mUserTag.empty()) {
532 mUserTag = request.mUserTag;
533 }
Shuzhen Wang9372b0b2022-05-11 18:55:19 -0700534 camera_metadata_entry entry =
535 physicalSettingsList.begin()->metadata.find(
536 ANDROID_CONTROL_VIDEO_STABILIZATION_MODE);
537 if (entry.count == 1) {
538 mVideoStabilizationMode = entry.data.u8[0];
539 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700540 }
541 mRequestIdCounter++;
542
543 if (streaming) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700544 err = mDevice->setStreamingRequestList(metadataRequestList, surfaceMapList,
545 &(submitInfo->mLastFrameNumber));
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800546 if (err != OK) {
547 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800548 "Camera %s: Got error %s (%d) after trying to set streaming request",
549 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800550 ALOGE("%s: %s", __FUNCTION__, msg.string());
551 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
552 msg.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700553 } else {
Shuzhen Wangc9ca6782016-04-26 13:40:31 -0700554 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700555 mStreamingRequestId = submitInfo->mRequestId;
Jianing Wei90e59c92014-03-12 18:29:36 -0700556 }
557 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700558 err = mDevice->captureList(metadataRequestList, surfaceMapList,
559 &(submitInfo->mLastFrameNumber));
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800560 if (err != OK) {
561 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800562 "Camera %s: Got error %s (%d) after trying to submit capture request",
563 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800564 ALOGE("%s: %s", __FUNCTION__, msg.string());
565 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
566 msg.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700567 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800568 ALOGV("%s: requestId = %d ", __FUNCTION__, submitInfo->mRequestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700569 }
570
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800571 ALOGV("%s: Camera %s: End of function", __FUNCTION__, mCameraIdStr.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700572 return res;
573}
574
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800575binder::Status CameraDeviceClient::cancelRequest(
576 int requestId,
577 /*out*/
578 int64_t* lastFrameNumber) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700579 ATRACE_CALL();
580 ALOGV("%s, requestId = %d", __FUNCTION__, requestId);
581
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800582 status_t err;
583 binder::Status res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700584
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800585 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700586
587 Mutex::Autolock icl(mBinderSerializationLock);
588
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800589 if (!mDevice.get()) {
590 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
591 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700592
Shuzhen Wangc9ca6782016-04-26 13:40:31 -0700593 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700594 if (mStreamingRequestId != requestId) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800595 String8 msg = String8::format("Camera %s: Canceling request ID %d doesn't match "
596 "current request ID %d", mCameraIdStr.string(), requestId, mStreamingRequestId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800597 ALOGE("%s: %s", __FUNCTION__, msg.string());
598 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700599 }
600
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800601 err = mDevice->clearStreamingRequest(lastFrameNumber);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700602
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800603 if (err == OK) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800604 ALOGV("%s: Camera %s: Successfully cleared streaming request",
605 __FUNCTION__, mCameraIdStr.string());
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700606 mStreamingRequestId = REQUEST_ID_NONE;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800607 } else {
608 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800609 "Camera %s: Error clearing streaming request: %s (%d)",
610 mCameraIdStr.string(), strerror(-err), err);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700611 }
612
613 return res;
614}
615
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800616binder::Status CameraDeviceClient::beginConfigure() {
Ruben Brunkb2119af2014-05-09 19:57:56 -0700617 // TODO: Implement this.
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -0700618 ATRACE_CALL();
Zhijun He1fa89992015-06-01 15:44:31 -0700619 ALOGV("%s: Not implemented yet.", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800620 return binder::Status::ok();
Ruben Brunkb2119af2014-05-09 19:57:56 -0700621}
622
Emilian Peev5fbe0ba2017-10-20 15:45:45 +0100623binder::Status CameraDeviceClient::endConfigure(int operatingMode,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700624 const hardware::camera2::impl::CameraMetadataNative& sessionParams, int64_t startTimeMs,
Emilian Peevcc0b7952020-01-07 13:54:47 -0800625 std::vector<int>* offlineStreamIds /*out*/) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -0700626 ATRACE_CALL();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700627 ALOGV("%s: ending configure (%d input stream, %zu output surfaces)",
628 __FUNCTION__, mInputStream.configured ? 1 : 0,
629 mStreamMap.size());
Igor Murashkine2d167e2014-08-19 16:19:59 -0700630
Zhijun He0effd522016-03-04 10:22:27 -0800631 binder::Status res;
632 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
633
Emilian Peevcc0b7952020-01-07 13:54:47 -0800634 if (offlineStreamIds == nullptr) {
635 String8 msg = String8::format("Invalid offline stream ids");
636 ALOGE("%s: %s", __FUNCTION__, msg.string());
637 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
638 }
639
Zhijun He0effd522016-03-04 10:22:27 -0800640 Mutex::Autolock icl(mBinderSerializationLock);
641
642 if (!mDevice.get()) {
643 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
644 }
645
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700646 res = SessionConfigurationUtils::checkOperatingMode(operatingMode, mDevice->info(),
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000647 mCameraIdStr);
Emilian Peev35ae8262018-11-08 13:11:32 +0000648 if (!res.isOk()) {
649 return res;
650 }
651
652 status_t err = mDevice->configureStreams(sessionParams, operatingMode);
653 if (err == BAD_VALUE) {
654 String8 msg = String8::format("Camera %s: Unsupported set of inputs/outputs provided",
655 mCameraIdStr.string());
656 ALOGE("%s: %s", __FUNCTION__, msg.string());
657 res = STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
658 } else if (err != OK) {
659 String8 msg = String8::format("Camera %s: Error configuring streams: %s (%d)",
660 mCameraIdStr.string(), strerror(-err), err);
661 ALOGE("%s: %s", __FUNCTION__, msg.string());
662 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Emilian Peev538c90e2018-12-17 18:03:19 +0000663 } else {
Emilian Peevcc0b7952020-01-07 13:54:47 -0800664 offlineStreamIds->clear();
665 mDevice->getOfflineStreamIds(offlineStreamIds);
666
Emilian Peev2f5d6012022-01-19 16:16:50 -0800667 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +0000668 for (size_t i = 0; i < mCompositeStreamMap.size(); ++i) {
669 err = mCompositeStreamMap.valueAt(i)->configureStream();
Emilian Peevcc0b7952020-01-07 13:54:47 -0800670 if (err != OK) {
Emilian Peev538c90e2018-12-17 18:03:19 +0000671 String8 msg = String8::format("Camera %s: Error configuring composite "
672 "streams: %s (%d)", mCameraIdStr.string(), strerror(-err), err);
673 ALOGE("%s: %s", __FUNCTION__, msg.string());
674 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
675 break;
676 }
Emilian Peevcc0b7952020-01-07 13:54:47 -0800677
678 // Composite streams can only support offline mode in case all individual internal
679 // streams are also supported.
680 std::vector<int> internalStreams;
681 mCompositeStreamMap.valueAt(i)->insertCompositeStreamIds(&internalStreams);
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800682 offlineStreamIds->erase(
683 std::remove_if(offlineStreamIds->begin(), offlineStreamIds->end(),
Emilian Peevcc0b7952020-01-07 13:54:47 -0800684 [&internalStreams] (int streamId) {
685 auto it = std::find(internalStreams.begin(), internalStreams.end(),
686 streamId);
687 if (it != internalStreams.end()) {
688 internalStreams.erase(it);
689 return true;
690 }
691
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800692 return false;}), offlineStreamIds->end());
Emilian Peevcc0b7952020-01-07 13:54:47 -0800693 if (internalStreams.empty()) {
694 offlineStreamIds->push_back(mCompositeStreamMap.valueAt(i)->getStreamId());
695 }
696 }
697
698 for (const auto& offlineStreamId : *offlineStreamIds) {
699 mStreamInfoMap[offlineStreamId].supportsOffline = true;
Emilian Peev538c90e2018-12-17 18:03:19 +0000700 }
Shuzhen Wang316781a2020-08-18 18:11:01 -0700701
702 nsecs_t configureEnd = systemTime();
703 int32_t configureDurationMs = ns2ms(configureEnd) - startTimeMs;
Austin Borger74fca042022-05-23 12:41:21 -0700704 mCameraServiceProxyWrapper->logStreamConfigured(mCameraIdStr, operatingMode,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700705 false /*internalReconfig*/, configureDurationMs);
Emilian Peev35ae8262018-11-08 13:11:32 +0000706 }
707
708 return res;
709}
710
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800711binder::Status CameraDeviceClient::isSessionConfigurationSupported(
712 const SessionConfiguration& sessionConfiguration, bool *status /*out*/) {
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800713
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800714 ATRACE_CALL();
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800715 binder::Status res;
716 status_t ret = OK;
717 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
718
719 Mutex::Autolock icl(mBinderSerializationLock);
720
721 if (!mDevice.get()) {
722 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
723 }
724
725 auto operatingMode = sessionConfiguration.getOperatingMode();
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700726 res = SessionConfigurationUtils::checkOperatingMode(operatingMode, mDevice->info(),
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000727 mCameraIdStr);
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800728 if (!res.isOk()) {
729 return res;
730 }
731
732 if (status == nullptr) {
733 String8 msg = String8::format( "Camera %s: Invalid status!", mCameraIdStr.string());
734 ALOGE("%s: %s", __FUNCTION__, msg.string());
735 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
736 }
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700737
Emilian Peev35ae8262018-11-08 13:11:32 +0000738 *status = false;
Shuzhen Wanga79a64d2022-04-24 19:56:30 -0700739 camera3::metadataGetter getMetadata = [this](const String8 &id, bool /*overrideForPerfClass*/) {
740 return mDevice->infoPhysical(id);};
Emilian Peev35ae8262018-11-08 13:11:32 +0000741 ret = mProviderManager->isSessionConfigurationSupported(mCameraIdStr.string(),
Shuzhen Wanga79a64d2022-04-24 19:56:30 -0700742 sessionConfiguration, mOverrideForPerfClass, getMetadata, status);
Emilian Peev35ae8262018-11-08 13:11:32 +0000743 switch (ret) {
744 case OK:
745 // Expected, do nothing.
746 break;
747 case INVALID_OPERATION: {
748 String8 msg = String8::format(
749 "Camera %s: Session configuration query not supported!",
750 mCameraIdStr.string());
751 ALOGD("%s: %s", __FUNCTION__, msg.string());
752 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
753 }
754
755 break;
756 default: {
757 String8 msg = String8::format( "Camera %s: Error: %s (%d)", mCameraIdStr.string(),
758 strerror(-ret), ret);
759 ALOGE("%s: %s", __FUNCTION__, msg.string());
760 res = STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
761 msg.string());
762 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800763 }
764
765 return res;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700766}
767
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800768binder::Status CameraDeviceClient::deleteStream(int streamId) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700769 ATRACE_CALL();
770 ALOGV("%s (streamId = 0x%x)", __FUNCTION__, streamId);
771
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800772 binder::Status res;
773 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700774
775 Mutex::Autolock icl(mBinderSerializationLock);
776
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800777 if (!mDevice.get()) {
778 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
779 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700780
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700781 bool isInput = false;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700782 std::vector<sp<IBinder>> surfaces;
Zhijun He5d677d12016-05-29 16:52:39 -0700783 ssize_t dIndex = NAME_NOT_FOUND;
Emilian Peev538c90e2018-12-17 18:03:19 +0000784 ssize_t compositeIndex = NAME_NOT_FOUND;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700785
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700786 if (mInputStream.configured && mInputStream.id == streamId) {
787 isInput = true;
788 } else {
789 // Guard against trying to delete non-created streams
790 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700791 if (streamId == mStreamMap.valueAt(i).streamId()) {
792 surfaces.push_back(mStreamMap.keyAt(i));
793 }
794 }
795
796 // See if this stream is one of the deferred streams.
797 for (size_t i = 0; i < mDeferredStreams.size(); ++i) {
798 if (streamId == mDeferredStreams[i]) {
799 dIndex = i;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700800 break;
801 }
802 }
803
Emilian Peev2f5d6012022-01-19 16:16:50 -0800804 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +0000805 for (size_t i = 0; i < mCompositeStreamMap.size(); ++i) {
806 if (streamId == mCompositeStreamMap.valueAt(i)->getStreamId()) {
807 compositeIndex = i;
808 break;
809 }
810 }
811
Shuzhen Wang0129d522016-10-30 22:43:41 -0700812 if (surfaces.empty() && dIndex == NAME_NOT_FOUND) {
813 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no such"
814 " stream created yet", mCameraIdStr.string(), streamId);
815 ALOGW("%s: %s", __FUNCTION__, msg.string());
816 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700817 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700818 }
819
820 // Also returns BAD_VALUE if stream ID was not valid
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800821 status_t err = mDevice->deleteStream(streamId);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700822
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800823 if (err != OK) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800824 String8 msg = String8::format("Camera %s: Unexpected error %s (%d) when deleting stream %d",
825 mCameraIdStr.string(), strerror(-err), err, streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800826 ALOGE("%s: %s", __FUNCTION__, msg.string());
827 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
828 } else {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700829 if (isInput) {
830 mInputStream.configured = false;
Zhijun He5d677d12016-05-29 16:52:39 -0700831 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700832 for (auto& surface : surfaces) {
833 mStreamMap.removeItem(surface);
834 }
835
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800836 mConfiguredOutputs.removeItem(streamId);
837
Shuzhen Wang0129d522016-10-30 22:43:41 -0700838 if (dIndex != NAME_NOT_FOUND) {
839 mDeferredStreams.removeItemsAt(dIndex);
840 }
Emilian Peev538c90e2018-12-17 18:03:19 +0000841
842 if (compositeIndex != NAME_NOT_FOUND) {
Emilian Peev2f5d6012022-01-19 16:16:50 -0800843 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +0000844 status_t ret;
845 if ((ret = mCompositeStreamMap.valueAt(compositeIndex)->deleteStream())
846 != OK) {
847 String8 msg = String8::format("Camera %s: Unexpected error %s (%d) when "
848 "deleting composite stream %d", mCameraIdStr.string(), strerror(-err), err,
849 streamId);
850 ALOGE("%s: %s", __FUNCTION__, msg.string());
851 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
852 }
853 mCompositeStreamMap.removeItemsAt(compositeIndex);
854 }
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800855 for (auto &mapIt: mHighResolutionCameraIdToStreamIdSet) {
856 auto &streamSet = mapIt.second;
857 if (streamSet.find(streamId) != streamSet.end()) {
858 streamSet.erase(streamId);
859 break;
860 }
861 }
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700862 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700863 }
864
865 return res;
866}
867
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800868binder::Status CameraDeviceClient::createStream(
869 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
870 /*out*/
871 int32_t* newStreamId) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700872 ATRACE_CALL();
Igor Murashkine7ee7632013-06-11 18:10:18 -0700873
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800874 binder::Status res;
875 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700876
877 Mutex::Autolock icl(mBinderSerializationLock);
878
Shuzhen Wang0129d522016-10-30 22:43:41 -0700879 const std::vector<sp<IGraphicBufferProducer>>& bufferProducers =
880 outputConfiguration.getGraphicBufferProducers();
881 size_t numBufferProducers = bufferProducers.size();
Shuzhen Wang758c2152017-01-10 18:26:18 -0800882 bool deferredConsumer = outputConfiguration.isDeferred();
883 bool isShared = outputConfiguration.isShared();
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800884 String8 physicalCameraId = String8(outputConfiguration.getPhysicalCameraId());
Shuzhen Wang758c2152017-01-10 18:26:18 -0800885 bool deferredConsumerOnly = deferredConsumer && numBufferProducers == 0;
Shuzhen Wang83bff122020-11-20 15:51:39 -0800886 bool isMultiResolution = outputConfiguration.isMultiResolution();
Emilian Peevc81a7592022-02-14 17:38:18 -0800887 int64_t dynamicRangeProfile = outputConfiguration.getDynamicRangeProfile();
Shuzhen Wang8ed1e872022-03-08 16:34:33 -0800888 int64_t streamUseCase = outputConfiguration.getStreamUseCase();
Shuzhen Wange4208922022-02-01 16:52:48 -0800889 int timestampBase = outputConfiguration.getTimestampBase();
Shuzhen Wang610d7b82022-02-08 14:37:22 -0800890 int mirrorMode = outputConfiguration.getMirrorMode();
Austin Borger9e2b27c2022-07-15 11:27:24 -0700891 int32_t colorSpace = outputConfiguration.getColorSpace();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700892
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700893 res = SessionConfigurationUtils::checkSurfaceType(numBufferProducers, deferredConsumer,
Emilian Peev35ae8262018-11-08 13:11:32 +0000894 outputConfiguration.getSurfaceType());
895 if (!res.isOk()) {
896 return res;
Yin-Chia Yeh89f14da2014-06-10 16:05:44 -0700897 }
Zhijun He5d677d12016-05-29 16:52:39 -0700898
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800899 if (!mDevice.get()) {
900 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
901 }
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700902 res = SessionConfigurationUtils::checkPhysicalCameraId(mPhysicalCameraIds,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800903 physicalCameraId, mCameraIdStr);
Emilian Peev35ae8262018-11-08 13:11:32 +0000904 if (!res.isOk()) {
905 return res;
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800906 }
Emilian Peev35ae8262018-11-08 13:11:32 +0000907
Shuzhen Wang0129d522016-10-30 22:43:41 -0700908 std::vector<sp<Surface>> surfaces;
909 std::vector<sp<IBinder>> binders;
Zhijun He5d677d12016-05-29 16:52:39 -0700910 status_t err;
911
912 // Create stream for deferred surface case.
Shuzhen Wang0129d522016-10-30 22:43:41 -0700913 if (deferredConsumerOnly) {
Shuzhen Wang758c2152017-01-10 18:26:18 -0800914 return createDeferredSurfaceStreamLocked(outputConfiguration, isShared, newStreamId);
Zhijun He5d677d12016-05-29 16:52:39 -0700915 }
916
Shuzhen Wang758c2152017-01-10 18:26:18 -0800917 OutputStreamInfo streamInfo;
918 bool isStreamInfoValid = false;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800919 const std::vector<int32_t> &sensorPixelModesUsed =
920 outputConfiguration.getSensorPixelModesUsed();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700921 for (auto& bufferProducer : bufferProducers) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700922 // Don't create multiple streams for the same target surface
Shuzhen Wang0129d522016-10-30 22:43:41 -0700923 sp<IBinder> binder = IInterface::asBinder(bufferProducer);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800924 ssize_t index = mStreamMap.indexOfKey(binder);
925 if (index != NAME_NOT_FOUND) {
926 String8 msg = String8::format("Camera %s: Surface already has a stream created for it "
927 "(ID %zd)", mCameraIdStr.string(), index);
928 ALOGW("%s: %s", __FUNCTION__, msg.string());
929 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
Shuzhen Wang0129d522016-10-30 22:43:41 -0700930 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700931
Shuzhen Wang758c2152017-01-10 18:26:18 -0800932 sp<Surface> surface;
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700933 res = SessionConfigurationUtils::createSurfaceFromGbp(streamInfo,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800934 isStreamInfoValid, surface, bufferProducer, mCameraIdStr,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -0800935 mDevice->infoPhysical(physicalCameraId), sensorPixelModesUsed, dynamicRangeProfile,
Austin Borger9e2b27c2022-07-15 11:27:24 -0700936 streamUseCase, timestampBase, mirrorMode, colorSpace);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800937
938 if (!res.isOk())
939 return res;
940
941 if (!isStreamInfoValid) {
942 isStreamInfoValid = true;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700943 }
944
Shuzhen Wang758c2152017-01-10 18:26:18 -0800945 binders.push_back(IInterface::asBinder(bufferProducer));
Shuzhen Wang0129d522016-10-30 22:43:41 -0700946 surfaces.push_back(surface);
Ruben Brunkbba75572014-11-20 17:29:50 -0800947 }
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700948
949 // If mOverrideForPerfClass is true, do not fail createStream() for small
950 // JPEG sizes because existing createSurfaceFromGbp() logic will find the
951 // closest possible supported size.
952
Zhijun He125684a2015-12-26 15:07:30 -0800953 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
Emilian Peev40ead602017-09-26 15:46:36 +0100954 std::vector<int> surfaceIds;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800955 bool isDepthCompositeStream =
956 camera3::DepthCompositeStream::isDepthCompositeStream(surfaces[0]);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800957 bool isHeicCompisiteStream = camera3::HeicCompositeStream::isHeicCompositeStream(surfaces[0]);
958 if (isDepthCompositeStream || isHeicCompisiteStream) {
959 sp<CompositeStream> compositeStream;
960 if (isDepthCompositeStream) {
961 compositeStream = new camera3::DepthCompositeStream(mDevice, getRemoteCallback());
962 } else {
963 compositeStream = new camera3::HeicCompositeStream(mDevice, getRemoteCallback());
964 }
965
Emilian Peev538c90e2018-12-17 18:03:19 +0000966 err = compositeStream->createStream(surfaces, deferredConsumer, streamInfo.width,
967 streamInfo.height, streamInfo.format,
Emilian Peevf4816702020-04-03 15:44:51 -0700968 static_cast<camera_stream_rotation_t>(outputConfiguration.getRotation()),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800969 &streamId, physicalCameraId, streamInfo.sensorPixelModesUsed, &surfaceIds,
970 outputConfiguration.getSurfaceSetID(), isShared, isMultiResolution);
Emilian Peev538c90e2018-12-17 18:03:19 +0000971 if (err == OK) {
Emilian Peev2f5d6012022-01-19 16:16:50 -0800972 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +0000973 mCompositeStreamMap.add(IInterface::asBinder(surfaces[0]->getIGraphicBufferProducer()),
974 compositeStream);
975 }
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800976 } else {
977 err = mDevice->createStream(surfaces, deferredConsumer, streamInfo.width,
978 streamInfo.height, streamInfo.format, streamInfo.dataSpace,
Emilian Peevf4816702020-04-03 15:44:51 -0700979 static_cast<camera_stream_rotation_t>(outputConfiguration.getRotation()),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800980 &streamId, physicalCameraId, streamInfo.sensorPixelModesUsed, &surfaceIds,
Emilian Peev2295df72021-11-12 18:14:10 -0800981 outputConfiguration.getSurfaceSetID(), isShared, isMultiResolution,
Shuzhen Wange4208922022-02-01 16:52:48 -0800982 /*consumerUsage*/0, streamInfo.dynamicRangeProfile, streamInfo.streamUseCase,
Austin Borger9e2b27c2022-07-15 11:27:24 -0700983 streamInfo.timestampBase, streamInfo.mirrorMode, streamInfo.colorSpace);
Emilian Peev538c90e2018-12-17 18:03:19 +0000984 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700985
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800986 if (err != OK) {
987 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800988 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
Shuzhen Wang758c2152017-01-10 18:26:18 -0800989 mCameraIdStr.string(), streamInfo.width, streamInfo.height, streamInfo.format,
990 streamInfo.dataSpace, strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800991 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700992 int i = 0;
993 for (auto& binder : binders) {
994 ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %d",
995 __FUNCTION__, binder.get(), streamId, i);
Emilian Peev40ead602017-09-26 15:46:36 +0100996 mStreamMap.add(binder, StreamSurfaceId(streamId, surfaceIds[i]));
997 i++;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700998 }
Shuzhen Wang758c2152017-01-10 18:26:18 -0800999
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -08001000 mConfiguredOutputs.add(streamId, outputConfiguration);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001001 mStreamInfoMap[streamId] = streamInfo;
1002
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001003 ALOGV("%s: Camera %s: Successfully created a new stream ID %d for output surface"
Shuzhen Wang0129d522016-10-30 22:43:41 -07001004 " (%d x %d) with format 0x%x.",
Shuzhen Wang758c2152017-01-10 18:26:18 -08001005 __FUNCTION__, mCameraIdStr.string(), streamId, streamInfo.width,
1006 streamInfo.height, streamInfo.format);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001007
Zhijun He5d677d12016-05-29 16:52:39 -07001008 // Set transform flags to ensure preview to be rotated correctly.
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001009 res = setStreamTransformLocked(streamId, streamInfo.mirrorMode);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001010
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001011 // Fill in mHighResolutionCameraIdToStreamIdSet map
1012 const String8 &cameraIdUsed =
1013 physicalCameraId.size() != 0 ? physicalCameraId : mCameraIdStr;
1014 const char *cameraIdUsedCStr = cameraIdUsed.string();
1015 // Only needed for high resolution sensors
1016 if (mHighResolutionSensors.find(cameraIdUsedCStr) !=
1017 mHighResolutionSensors.end()) {
1018 mHighResolutionCameraIdToStreamIdSet[cameraIdUsedCStr].insert(streamId);
1019 }
1020
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001021 *newStreamId = streamId;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001022 }
1023
1024 return res;
1025}
1026
Zhijun He5d677d12016-05-29 16:52:39 -07001027binder::Status CameraDeviceClient::createDeferredSurfaceStreamLocked(
1028 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
Shuzhen Wang758c2152017-01-10 18:26:18 -08001029 bool isShared,
Zhijun He5d677d12016-05-29 16:52:39 -07001030 /*out*/
1031 int* newStreamId) {
1032 int width, height, format, surfaceType;
Emilian Peev050f5dc2017-05-18 14:43:56 +01001033 uint64_t consumerUsage;
Zhijun He5d677d12016-05-29 16:52:39 -07001034 android_dataspace dataSpace;
Austin Borger9e2b27c2022-07-15 11:27:24 -07001035 int32_t colorSpace;
Zhijun He5d677d12016-05-29 16:52:39 -07001036 status_t err;
1037 binder::Status res;
1038
1039 if (!mDevice.get()) {
1040 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1041 }
1042
1043 // Infer the surface info for deferred surface stream creation.
1044 width = outputConfiguration.getWidth();
1045 height = outputConfiguration.getHeight();
1046 surfaceType = outputConfiguration.getSurfaceType();
1047 format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
1048 dataSpace = android_dataspace_t::HAL_DATASPACE_UNKNOWN;
Austin Borger9e2b27c2022-07-15 11:27:24 -07001049 colorSpace = ANDROID_REQUEST_AVAILABLE_COLOR_SPACE_PROFILES_MAP_UNSPECIFIED;
Zhijun He5d677d12016-05-29 16:52:39 -07001050 // Hardcode consumer usage flags: SurfaceView--0x900, SurfaceTexture--0x100.
1051 consumerUsage = GraphicBuffer::USAGE_HW_TEXTURE;
1052 if (surfaceType == OutputConfiguration::SURFACE_TYPE_SURFACE_VIEW) {
1053 consumerUsage |= GraphicBuffer::USAGE_HW_COMPOSER;
1054 }
1055 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001056 std::vector<sp<Surface>> noSurface;
Emilian Peev40ead602017-09-26 15:46:36 +01001057 std::vector<int> surfaceIds;
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001058 String8 physicalCameraId(outputConfiguration.getPhysicalCameraId());
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001059 const String8 &cameraIdUsed =
1060 physicalCameraId.size() != 0 ? physicalCameraId : mCameraIdStr;
1061 // Here, we override sensor pixel modes
1062 std::unordered_set<int32_t> overriddenSensorPixelModesUsed;
1063 const std::vector<int32_t> &sensorPixelModesUsed =
1064 outputConfiguration.getSensorPixelModesUsed();
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07001065 if (SessionConfigurationUtils::checkAndOverrideSensorPixelModesUsed(
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001066 sensorPixelModesUsed, format, width, height, getStaticInfo(cameraIdUsed),
1067 /*allowRounding*/ false, &overriddenSensorPixelModesUsed) != OK) {
1068 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1069 "sensor pixel modes used not valid for deferred stream");
1070 }
1071
Shuzhen Wang0129d522016-10-30 22:43:41 -07001072 err = mDevice->createStream(noSurface, /*hasDeferredConsumer*/true, width,
1073 height, format, dataSpace,
Emilian Peevf4816702020-04-03 15:44:51 -07001074 static_cast<camera_stream_rotation_t>(outputConfiguration.getRotation()),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001075 &streamId, physicalCameraId,
1076 overriddenSensorPixelModesUsed,
1077 &surfaceIds,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001078 outputConfiguration.getSurfaceSetID(), isShared,
Emilian Peev2295df72021-11-12 18:14:10 -08001079 outputConfiguration.isMultiResolution(), consumerUsage,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001080 outputConfiguration.getDynamicRangeProfile(),
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001081 outputConfiguration.getStreamUseCase(),
1082 outputConfiguration.getMirrorMode());
Zhijun He5d677d12016-05-29 16:52:39 -07001083
1084 if (err != OK) {
1085 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001086 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
1087 mCameraIdStr.string(), width, height, format, dataSpace, strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001088 } else {
1089 // Can not add streamId to mStreamMap here, as the surface is deferred. Add it to
1090 // a separate list to track. Once the deferred surface is set, this id will be
1091 // relocated to mStreamMap.
1092 mDeferredStreams.push_back(streamId);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001093 mStreamInfoMap.emplace(std::piecewise_construct, std::forward_as_tuple(streamId),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001094 std::forward_as_tuple(width, height, format, dataSpace, consumerUsage,
Emilian Peev2295df72021-11-12 18:14:10 -08001095 overriddenSensorPixelModesUsed,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001096 outputConfiguration.getDynamicRangeProfile(),
Shuzhen Wange4208922022-02-01 16:52:48 -08001097 outputConfiguration.getStreamUseCase(),
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001098 outputConfiguration.getTimestampBase(),
Austin Borger9e2b27c2022-07-15 11:27:24 -07001099 outputConfiguration.getMirrorMode(),
1100 colorSpace));
Shuzhen Wang758c2152017-01-10 18:26:18 -08001101
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001102 ALOGV("%s: Camera %s: Successfully created a new stream ID %d for a deferred surface"
Zhijun He5d677d12016-05-29 16:52:39 -07001103 " (%d x %d) stream with format 0x%x.",
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001104 __FUNCTION__, mCameraIdStr.string(), streamId, width, height, format);
Zhijun He5d677d12016-05-29 16:52:39 -07001105
1106 // Set transform flags to ensure preview to be rotated correctly.
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001107 res = setStreamTransformLocked(streamId, outputConfiguration.getMirrorMode());
Zhijun He5d677d12016-05-29 16:52:39 -07001108
1109 *newStreamId = streamId;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001110 // Fill in mHighResolutionCameraIdToStreamIdSet
1111 const char *cameraIdUsedCStr = cameraIdUsed.string();
1112 // Only needed for high resolution sensors
1113 if (mHighResolutionSensors.find(cameraIdUsedCStr) !=
1114 mHighResolutionSensors.end()) {
1115 mHighResolutionCameraIdToStreamIdSet[cameraIdUsed.string()].insert(streamId);
1116 }
Zhijun He5d677d12016-05-29 16:52:39 -07001117 }
1118 return res;
1119}
1120
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001121binder::Status CameraDeviceClient::setStreamTransformLocked(int streamId, int mirrorMode) {
Zhijun He5d677d12016-05-29 16:52:39 -07001122 int32_t transform = 0;
1123 status_t err;
1124 binder::Status res;
1125
1126 if (!mDevice.get()) {
1127 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1128 }
1129
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001130 err = getRotationTransformLocked(mirrorMode, &transform);
Zhijun He5d677d12016-05-29 16:52:39 -07001131 if (err != OK) {
1132 // Error logged by getRotationTransformLocked.
1133 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
1134 "Unable to calculate rotation transform for new stream");
1135 }
1136
1137 err = mDevice->setStreamTransform(streamId, transform);
1138 if (err != OK) {
1139 String8 msg = String8::format("Failed to set stream transform (stream id %d)",
1140 streamId);
1141 ALOGE("%s: %s", __FUNCTION__, msg.string());
1142 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
1143 }
1144
1145 return res;
1146}
Ruben Brunkbba75572014-11-20 17:29:50 -08001147
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001148binder::Status CameraDeviceClient::createInputStream(
Shuzhen Wang83bff122020-11-20 15:51:39 -08001149 int width, int height, int format, bool isMultiResolution,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001150 /*out*/
1151 int32_t* newStreamId) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001152
1153 ATRACE_CALL();
Shuzhen Wang83bff122020-11-20 15:51:39 -08001154 ALOGV("%s (w = %d, h = %d, f = 0x%x, isMultiResolution %d)", __FUNCTION__,
1155 width, height, format, isMultiResolution);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001156
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001157 binder::Status res;
1158 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001159
1160 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001161
1162 if (!mDevice.get()) {
1163 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1164 }
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001165
1166 if (mInputStream.configured) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001167 String8 msg = String8::format("Camera %s: Already has an input stream "
Yi Kong0f414de2017-12-15 13:48:50 -08001168 "configured (ID %d)", mCameraIdStr.string(), mInputStream.id);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001169 ALOGE("%s: %s", __FUNCTION__, msg.string() );
1170 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001171 }
1172
1173 int streamId = -1;
Shuzhen Wang83bff122020-11-20 15:51:39 -08001174 status_t err = mDevice->createInputStream(width, height, format, isMultiResolution, &streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001175 if (err == OK) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001176 mInputStream.configured = true;
1177 mInputStream.width = width;
1178 mInputStream.height = height;
1179 mInputStream.format = format;
1180 mInputStream.id = streamId;
1181
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001182 ALOGV("%s: Camera %s: Successfully created a new input stream ID %d",
1183 __FUNCTION__, mCameraIdStr.string(), streamId);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001184
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001185 *newStreamId = streamId;
1186 } else {
1187 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001188 "Camera %s: Error creating new input stream: %s (%d)", mCameraIdStr.string(),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001189 strerror(-err), err);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001190 }
1191
1192 return res;
1193}
1194
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001195binder::Status CameraDeviceClient::getInputSurface(/*out*/ view::Surface *inputSurface) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001196
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001197 binder::Status res;
1198 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1199
1200 if (inputSurface == NULL) {
1201 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Null input surface");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001202 }
1203
1204 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001205 if (!mDevice.get()) {
1206 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1207 }
1208 sp<IGraphicBufferProducer> producer;
1209 status_t err = mDevice->getInputBufferProducer(&producer);
1210 if (err != OK) {
1211 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001212 "Camera %s: Error getting input Surface: %s (%d)",
1213 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001214 } else {
1215 inputSurface->name = String16("CameraInput");
1216 inputSurface->graphicBufferProducer = producer;
1217 }
1218 return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001219}
1220
Emilian Peev40ead602017-09-26 15:46:36 +01001221binder::Status CameraDeviceClient::updateOutputConfiguration(int streamId,
1222 const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
1223 ATRACE_CALL();
1224
1225 binder::Status res;
1226 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1227
1228 Mutex::Autolock icl(mBinderSerializationLock);
1229
1230 if (!mDevice.get()) {
1231 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1232 }
1233
1234 const std::vector<sp<IGraphicBufferProducer> >& bufferProducers =
1235 outputConfiguration.getGraphicBufferProducers();
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -07001236 String8 physicalCameraId(outputConfiguration.getPhysicalCameraId());
1237
Emilian Peev40ead602017-09-26 15:46:36 +01001238 auto producerCount = bufferProducers.size();
1239 if (producerCount == 0) {
1240 ALOGE("%s: bufferProducers must not be empty", __FUNCTION__);
1241 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1242 "bufferProducers must not be empty");
1243 }
1244
1245 // The first output is the one associated with the output configuration.
1246 // It should always be present, valid and the corresponding stream id should match.
1247 sp<IBinder> binder = IInterface::asBinder(bufferProducers[0]);
1248 ssize_t index = mStreamMap.indexOfKey(binder);
1249 if (index == NAME_NOT_FOUND) {
1250 ALOGE("%s: Outputconfiguration is invalid", __FUNCTION__);
1251 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1252 "OutputConfiguration is invalid");
1253 }
1254 if (mStreamMap.valueFor(binder).streamId() != streamId) {
1255 ALOGE("%s: Stream Id: %d provided doesn't match the id: %d in the stream map",
1256 __FUNCTION__, streamId, mStreamMap.valueFor(binder).streamId());
1257 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1258 "Stream id is invalid");
1259 }
1260
1261 std::vector<size_t> removedSurfaceIds;
1262 std::vector<sp<IBinder>> removedOutputs;
1263 std::vector<sp<Surface>> newOutputs;
1264 std::vector<OutputStreamInfo> streamInfos;
1265 KeyedVector<sp<IBinder>, sp<IGraphicBufferProducer>> newOutputsMap;
1266 for (auto &it : bufferProducers) {
1267 newOutputsMap.add(IInterface::asBinder(it), it);
1268 }
1269
1270 for (size_t i = 0; i < mStreamMap.size(); i++) {
1271 ssize_t idx = newOutputsMap.indexOfKey(mStreamMap.keyAt(i));
1272 if (idx == NAME_NOT_FOUND) {
1273 if (mStreamMap[i].streamId() == streamId) {
1274 removedSurfaceIds.push_back(mStreamMap[i].surfaceId());
1275 removedOutputs.push_back(mStreamMap.keyAt(i));
1276 }
1277 } else {
1278 if (mStreamMap[i].streamId() != streamId) {
1279 ALOGE("%s: Output surface already part of a different stream", __FUNCTION__);
1280 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1281 "Target Surface is invalid");
1282 }
1283 newOutputsMap.removeItemsAt(idx);
1284 }
1285 }
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001286 const std::vector<int32_t> &sensorPixelModesUsed =
1287 outputConfiguration.getSensorPixelModesUsed();
Shuzhen Wang8ed1e872022-03-08 16:34:33 -08001288 int64_t streamUseCase = outputConfiguration.getStreamUseCase();
Shuzhen Wange4208922022-02-01 16:52:48 -08001289 int timestampBase = outputConfiguration.getTimestampBase();
Emilian Peevc81a7592022-02-14 17:38:18 -08001290 int64_t dynamicRangeProfile = outputConfiguration.getDynamicRangeProfile();
Austin Borger9e2b27c2022-07-15 11:27:24 -07001291 int32_t colorSpace = outputConfiguration.getColorSpace();
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001292 int mirrorMode = outputConfiguration.getMirrorMode();
Emilian Peev2295df72021-11-12 18:14:10 -08001293
Emilian Peev40ead602017-09-26 15:46:36 +01001294 for (size_t i = 0; i < newOutputsMap.size(); i++) {
1295 OutputStreamInfo outInfo;
1296 sp<Surface> surface;
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07001297 res = SessionConfigurationUtils::createSurfaceFromGbp(outInfo,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001298 /*isStreamInfoValid*/ false, surface, newOutputsMap.valueAt(i), mCameraIdStr,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001299 mDevice->infoPhysical(physicalCameraId), sensorPixelModesUsed, dynamicRangeProfile,
Austin Borger9e2b27c2022-07-15 11:27:24 -07001300 streamUseCase, timestampBase, mirrorMode, colorSpace);
Emilian Peev40ead602017-09-26 15:46:36 +01001301 if (!res.isOk())
1302 return res;
1303
Emilian Peev40ead602017-09-26 15:46:36 +01001304 streamInfos.push_back(outInfo);
1305 newOutputs.push_back(surface);
1306 }
1307
1308 //Trivial case no changes required
1309 if (removedSurfaceIds.empty() && newOutputs.empty()) {
1310 return binder::Status::ok();
1311 }
1312
1313 KeyedVector<sp<Surface>, size_t> outputMap;
1314 auto ret = mDevice->updateStream(streamId, newOutputs, streamInfos, removedSurfaceIds,
1315 &outputMap);
1316 if (ret != OK) {
1317 switch (ret) {
1318 case NAME_NOT_FOUND:
1319 case BAD_VALUE:
1320 case -EBUSY:
1321 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1322 "Camera %s: Error updating stream: %s (%d)",
1323 mCameraIdStr.string(), strerror(ret), ret);
1324 break;
1325 default:
1326 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1327 "Camera %s: Error updating stream: %s (%d)",
1328 mCameraIdStr.string(), strerror(ret), ret);
1329 break;
1330 }
1331 } else {
1332 for (const auto &it : removedOutputs) {
1333 mStreamMap.removeItem(it);
1334 }
1335
1336 for (size_t i = 0; i < outputMap.size(); i++) {
1337 mStreamMap.add(IInterface::asBinder(outputMap.keyAt(i)->getIGraphicBufferProducer()),
1338 StreamSurfaceId(streamId, outputMap.valueAt(i)));
1339 }
1340
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -08001341 mConfiguredOutputs.replaceValueFor(streamId, outputConfiguration);
1342
Emilian Peev40ead602017-09-26 15:46:36 +01001343 ALOGV("%s: Camera %s: Successful stream ID %d update",
1344 __FUNCTION__, mCameraIdStr.string(), streamId);
1345 }
1346
1347 return res;
1348}
1349
Igor Murashkine7ee7632013-06-11 18:10:18 -07001350// Create a request object from a template.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001351binder::Status CameraDeviceClient::createDefaultRequest(int templateId,
1352 /*out*/
1353 hardware::camera2::impl::CameraMetadataNative* request)
Igor Murashkine7ee7632013-06-11 18:10:18 -07001354{
1355 ATRACE_CALL();
1356 ALOGV("%s (templateId = 0x%x)", __FUNCTION__, templateId);
1357
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001358 binder::Status res;
1359 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001360
1361 Mutex::Autolock icl(mBinderSerializationLock);
1362
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001363 if (!mDevice.get()) {
1364 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1365 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001366
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001367 status_t err;
Emilian Peevf4816702020-04-03 15:44:51 -07001368 camera_request_template_t tempId = camera_request_template_t::CAMERA_TEMPLATE_COUNT;
1369 if (!(res = mapRequestTemplate(templateId, &tempId)).isOk()) return res;
1370
1371 CameraMetadata metadata;
1372 if ( (err = mDevice->createDefaultRequest(tempId, &metadata) ) == OK &&
Igor Murashkine7ee7632013-06-11 18:10:18 -07001373 request != NULL) {
1374
1375 request->swap(metadata);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001376 } else if (err == BAD_VALUE) {
1377 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001378 "Camera %s: Template ID %d is invalid or not supported: %s (%d)",
1379 mCameraIdStr.string(), templateId, strerror(-err), err);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001380
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001381 } else {
1382 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001383 "Camera %s: Error creating default request for template %d: %s (%d)",
1384 mCameraIdStr.string(), templateId, strerror(-err), err);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001385 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001386 return res;
1387}
1388
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001389binder::Status CameraDeviceClient::getCameraInfo(
1390 /*out*/
1391 hardware::camera2::impl::CameraMetadataNative* info)
Igor Murashkine7ee7632013-06-11 18:10:18 -07001392{
1393 ATRACE_CALL();
1394 ALOGV("%s", __FUNCTION__);
1395
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001396 binder::Status res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001397
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001398 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001399
1400 Mutex::Autolock icl(mBinderSerializationLock);
1401
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001402 if (!mDevice.get()) {
1403 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1404 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001405
Igor Murashkin099b4572013-07-12 17:52:16 -07001406 if (info != NULL) {
1407 *info = mDevice->info(); // static camera metadata
1408 // TODO: merge with device-specific camera metadata
1409 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001410
1411 return res;
1412}
1413
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001414binder::Status CameraDeviceClient::waitUntilIdle()
Zhijun He2ab500c2013-07-23 08:02:53 -07001415{
1416 ATRACE_CALL();
1417 ALOGV("%s", __FUNCTION__);
1418
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001419 binder::Status res;
1420 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Zhijun He2ab500c2013-07-23 08:02:53 -07001421
1422 Mutex::Autolock icl(mBinderSerializationLock);
1423
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001424 if (!mDevice.get()) {
1425 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1426 }
Zhijun He2ab500c2013-07-23 08:02:53 -07001427
1428 // FIXME: Also need check repeating burst.
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001429 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001430 if (mStreamingRequestId != REQUEST_ID_NONE) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001431 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001432 "Camera %s: Try to waitUntilIdle when there are active streaming requests",
1433 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001434 ALOGE("%s: %s", __FUNCTION__, msg.string());
1435 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Zhijun He2ab500c2013-07-23 08:02:53 -07001436 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001437 status_t err = mDevice->waitUntilDrained();
1438 if (err != OK) {
1439 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001440 "Camera %s: Error waiting to drain: %s (%d)",
1441 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001442 }
Zhijun He2ab500c2013-07-23 08:02:53 -07001443 ALOGV("%s Done", __FUNCTION__);
Zhijun He2ab500c2013-07-23 08:02:53 -07001444 return res;
1445}
1446
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001447binder::Status CameraDeviceClient::flush(
1448 /*out*/
1449 int64_t* lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001450 ATRACE_CALL();
1451 ALOGV("%s", __FUNCTION__);
1452
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001453 binder::Status res;
1454 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001455
1456 Mutex::Autolock icl(mBinderSerializationLock);
1457
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001458 if (!mDevice.get()) {
1459 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1460 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001461
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001462 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001463 mStreamingRequestId = REQUEST_ID_NONE;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001464 status_t err = mDevice->flush(lastFrameNumber);
1465 if (err != OK) {
1466 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001467 "Camera %s: Error flushing device: %s (%d)", mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001468 }
1469 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001470}
1471
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001472binder::Status CameraDeviceClient::prepare(int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001473 ATRACE_CALL();
1474 ALOGV("%s", __FUNCTION__);
1475
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001476 binder::Status res;
1477 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001478
1479 Mutex::Autolock icl(mBinderSerializationLock);
1480
1481 // Guard against trying to prepare non-created streams
1482 ssize_t index = NAME_NOT_FOUND;
1483 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001484 if (streamId == mStreamMap.valueAt(i).streamId()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001485 index = i;
1486 break;
1487 }
1488 }
1489
1490 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001491 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1492 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001493 ALOGW("%s: %s", __FUNCTION__, msg.string());
1494 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001495 }
1496
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001497 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1498 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001499 status_t err = mDevice->prepare(streamId);
1500 if (err == BAD_VALUE) {
1501 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001502 "Camera %s: Stream %d has already been used, and cannot be prepared",
1503 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001504 } else if (err != OK) {
1505 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001506 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001507 strerror(-err), err);
1508 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001509 return res;
1510}
1511
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001512binder::Status CameraDeviceClient::prepare2(int maxCount, int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001513 ATRACE_CALL();
1514 ALOGV("%s", __FUNCTION__);
1515
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001516 binder::Status res;
1517 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Ruben Brunkc78ac262015-08-13 17:58:46 -07001518
1519 Mutex::Autolock icl(mBinderSerializationLock);
1520
1521 // Guard against trying to prepare non-created streams
1522 ssize_t index = NAME_NOT_FOUND;
1523 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001524 if (streamId == mStreamMap.valueAt(i).streamId()) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001525 index = i;
1526 break;
1527 }
1528 }
1529
1530 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001531 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1532 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001533 ALOGW("%s: %s", __FUNCTION__, msg.string());
1534 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -07001535 }
1536
1537 if (maxCount <= 0) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001538 String8 msg = String8::format("Camera %s: maxCount (%d) must be greater than 0",
1539 mCameraIdStr.string(), maxCount);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001540 ALOGE("%s: %s", __FUNCTION__, msg.string());
1541 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -07001542 }
1543
1544 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1545 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001546 status_t err = mDevice->prepare(maxCount, streamId);
1547 if (err == BAD_VALUE) {
1548 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001549 "Camera %s: Stream %d has already been used, and cannot be prepared",
1550 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001551 } else if (err != OK) {
1552 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001553 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001554 strerror(-err), err);
1555 }
Ruben Brunkc78ac262015-08-13 17:58:46 -07001556
1557 return res;
1558}
1559
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001560binder::Status CameraDeviceClient::tearDown(int streamId) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001561 ATRACE_CALL();
1562 ALOGV("%s", __FUNCTION__);
1563
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001564 binder::Status res;
1565 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001566
1567 Mutex::Autolock icl(mBinderSerializationLock);
1568
1569 // Guard against trying to prepare non-created streams
1570 ssize_t index = NAME_NOT_FOUND;
1571 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001572 if (streamId == mStreamMap.valueAt(i).streamId()) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001573 index = i;
1574 break;
1575 }
1576 }
1577
1578 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001579 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1580 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001581 ALOGW("%s: %s", __FUNCTION__, msg.string());
1582 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001583 }
1584
1585 // Also returns BAD_VALUE if stream ID was not valid or if the stream is in
1586 // use
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001587 status_t err = mDevice->tearDown(streamId);
1588 if (err == BAD_VALUE) {
1589 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001590 "Camera %s: Stream %d is still in use, cannot be torn down",
1591 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001592 } else if (err != OK) {
1593 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001594 "Camera %s: Error tearing down stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001595 strerror(-err), err);
1596 }
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001597
1598 return res;
1599}
1600
Shuzhen Wang758c2152017-01-10 18:26:18 -08001601binder::Status CameraDeviceClient::finalizeOutputConfigurations(int32_t streamId,
Zhijun He5d677d12016-05-29 16:52:39 -07001602 const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
1603 ATRACE_CALL();
1604
1605 binder::Status res;
1606 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1607
1608 Mutex::Autolock icl(mBinderSerializationLock);
1609
Shuzhen Wang0129d522016-10-30 22:43:41 -07001610 const std::vector<sp<IGraphicBufferProducer> >& bufferProducers =
1611 outputConfiguration.getGraphicBufferProducers();
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -07001612 String8 physicalId(outputConfiguration.getPhysicalCameraId());
Zhijun He5d677d12016-05-29 16:52:39 -07001613
Shuzhen Wang0129d522016-10-30 22:43:41 -07001614 if (bufferProducers.size() == 0) {
1615 ALOGE("%s: bufferProducers must not be empty", __FUNCTION__);
Zhijun He5d677d12016-05-29 16:52:39 -07001616 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Target Surface is invalid");
1617 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001618
Shuzhen Wang758c2152017-01-10 18:26:18 -08001619 // streamId should be in mStreamMap if this stream already has a surface attached
1620 // to it. Otherwise, it should be in mDeferredStreams.
1621 bool streamIdConfigured = false;
1622 ssize_t deferredStreamIndex = NAME_NOT_FOUND;
1623 for (size_t i = 0; i < mStreamMap.size(); i++) {
1624 if (mStreamMap.valueAt(i).streamId() == streamId) {
1625 streamIdConfigured = true;
1626 break;
1627 }
Zhijun He5d677d12016-05-29 16:52:39 -07001628 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001629 for (size_t i = 0; i < mDeferredStreams.size(); i++) {
1630 if (streamId == mDeferredStreams[i]) {
1631 deferredStreamIndex = i;
1632 break;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001633 }
1634
Shuzhen Wang758c2152017-01-10 18:26:18 -08001635 }
1636 if (deferredStreamIndex == NAME_NOT_FOUND && !streamIdConfigured) {
1637 String8 msg = String8::format("Camera %s: deferred surface is set to a unknown stream"
1638 "(ID %d)", mCameraIdStr.string(), streamId);
1639 ALOGW("%s: %s", __FUNCTION__, msg.string());
1640 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Zhijun He5d677d12016-05-29 16:52:39 -07001641 }
1642
Shuzhen Wang88fd0052017-02-09 16:40:07 -08001643 if (mStreamInfoMap[streamId].finalized) {
1644 String8 msg = String8::format("Camera %s: finalizeOutputConfigurations has been called"
1645 " on stream ID %d", mCameraIdStr.string(), streamId);
1646 ALOGW("%s: %s", __FUNCTION__, msg.string());
1647 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1648 }
1649
Zhijun He5d677d12016-05-29 16:52:39 -07001650 if (!mDevice.get()) {
1651 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1652 }
1653
Shuzhen Wang758c2152017-01-10 18:26:18 -08001654 std::vector<sp<Surface>> consumerSurfaces;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001655 const std::vector<int32_t> &sensorPixelModesUsed =
1656 outputConfiguration.getSensorPixelModesUsed();
Emilian Peevc81a7592022-02-14 17:38:18 -08001657 int64_t dynamicRangeProfile = outputConfiguration.getDynamicRangeProfile();
Austin Borger9e2b27c2022-07-15 11:27:24 -07001658 int32_t colorSpace = outputConfiguration.getColorSpace();
1659 int64_t streamUseCase = outputConfiguration.getStreamUseCase();
Shuzhen Wange4208922022-02-01 16:52:48 -08001660 int timestampBase = outputConfiguration.getTimestampBase();
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001661 int mirrorMode = outputConfiguration.getMirrorMode();
Shuzhen Wang758c2152017-01-10 18:26:18 -08001662 for (auto& bufferProducer : bufferProducers) {
1663 // Don't create multiple streams for the same target surface
Zhijun He5d677d12016-05-29 16:52:39 -07001664 ssize_t index = mStreamMap.indexOfKey(IInterface::asBinder(bufferProducer));
1665 if (index != NAME_NOT_FOUND) {
Shuzhen Wang758c2152017-01-10 18:26:18 -08001666 ALOGV("Camera %s: Surface already has a stream created "
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001667 " for it (ID %zd)", mCameraIdStr.string(), index);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001668 continue;
Zhijun He5d677d12016-05-29 16:52:39 -07001669 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001670
1671 sp<Surface> surface;
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07001672 res = SessionConfigurationUtils::createSurfaceFromGbp(mStreamInfoMap[streamId],
Colin Crossb8a9dbb2020-08-27 04:12:26 +00001673 true /*isStreamInfoValid*/, surface, bufferProducer, mCameraIdStr,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001674 mDevice->infoPhysical(physicalId), sensorPixelModesUsed, dynamicRangeProfile,
Austin Borger9e2b27c2022-07-15 11:27:24 -07001675 streamUseCase, timestampBase, mirrorMode, colorSpace);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001676
1677 if (!res.isOk())
1678 return res;
1679
1680 consumerSurfaces.push_back(surface);
Zhijun He5d677d12016-05-29 16:52:39 -07001681 }
1682
Shuzhen Wanga81ce342017-04-13 15:18:36 -07001683 // Gracefully handle case where finalizeOutputConfigurations is called
1684 // without any new surface.
1685 if (consumerSurfaces.size() == 0) {
1686 mStreamInfoMap[streamId].finalized = true;
1687 return res;
1688 }
1689
Zhijun He5d677d12016-05-29 16:52:39 -07001690 // Finish the deferred stream configuration with the surface.
Shuzhen Wang758c2152017-01-10 18:26:18 -08001691 status_t err;
Emilian Peev40ead602017-09-26 15:46:36 +01001692 std::vector<int> consumerSurfaceIds;
1693 err = mDevice->setConsumerSurfaces(streamId, consumerSurfaces, &consumerSurfaceIds);
Zhijun He5d677d12016-05-29 16:52:39 -07001694 if (err == OK) {
Shuzhen Wang758c2152017-01-10 18:26:18 -08001695 for (size_t i = 0; i < consumerSurfaces.size(); i++) {
1696 sp<IBinder> binder = IInterface::asBinder(
1697 consumerSurfaces[i]->getIGraphicBufferProducer());
Emilian Peev40ead602017-09-26 15:46:36 +01001698 ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %d", __FUNCTION__,
Shuzhen Wang758c2152017-01-10 18:26:18 -08001699 binder.get(), streamId, consumerSurfaceIds[i]);
1700 mStreamMap.add(binder, StreamSurfaceId(streamId, consumerSurfaceIds[i]));
1701 }
1702 if (deferredStreamIndex != NAME_NOT_FOUND) {
1703 mDeferredStreams.removeItemsAt(deferredStreamIndex);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001704 }
Shuzhen Wang88fd0052017-02-09 16:40:07 -08001705 mStreamInfoMap[streamId].finalized = true;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -08001706 mConfiguredOutputs.replaceValueFor(streamId, outputConfiguration);
Zhijun He5d677d12016-05-29 16:52:39 -07001707 } else if (err == NO_INIT) {
1708 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001709 "Camera %s: Deferred surface is invalid: %s (%d)",
1710 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001711 } else {
1712 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001713 "Camera %s: Error setting output stream deferred surface: %s (%d)",
1714 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001715 }
1716
1717 return res;
1718}
1719
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001720binder::Status CameraDeviceClient::setCameraAudioRestriction(int32_t mode) {
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001721 ATRACE_CALL();
1722 binder::Status res;
1723 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1724
1725 if (!isValidAudioRestriction(mode)) {
1726 String8 msg = String8::format("Camera %s: invalid audio restriction mode %d",
1727 mCameraIdStr.string(), mode);
1728 ALOGW("%s: %s", __FUNCTION__, msg.string());
1729 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1730 }
1731
1732 Mutex::Autolock icl(mBinderSerializationLock);
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001733 BasicClient::setAudioRestriction(mode);
1734 return binder::Status::ok();
1735}
1736
1737binder::Status CameraDeviceClient::getGlobalAudioRestriction(/*out*/ int32_t* outMode) {
1738 ATRACE_CALL();
1739 binder::Status res;
1740 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1741 Mutex::Autolock icl(mBinderSerializationLock);
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001742 if (outMode != nullptr) {
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001743 *outMode = BasicClient::getServiceAudioRestriction();
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001744 }
1745 return binder::Status::ok();
1746}
1747
Ravneetaeb20dc2022-03-30 05:33:03 +00001748status_t CameraDeviceClient::setCameraServiceWatchdog(bool enabled) {
1749 return mDevice->setCameraServiceWatchdog(enabled);
1750}
1751
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08001752status_t CameraDeviceClient::setRotateAndCropOverride(uint8_t rotateAndCrop) {
1753 if (rotateAndCrop > ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return BAD_VALUE;
1754
1755 return mDevice->setRotateAndCropAutoBehavior(
1756 static_cast<camera_metadata_enum_android_scaler_rotate_and_crop_t>(rotateAndCrop));
1757}
1758
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00001759status_t CameraDeviceClient::setAutoframingOverride(uint8_t autoframingValue) {
1760 if (autoframingValue > ANDROID_CONTROL_AUTOFRAMING_AUTO) return BAD_VALUE;
1761
1762 return mDevice->setAutoframingAutoBehavior(
1763 static_cast<camera_metadata_enum_android_control_autoframing_t>(autoframingValue));
1764}
1765
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08001766bool CameraDeviceClient::supportsCameraMute() {
1767 return mDevice->supportsCameraMute();
1768}
1769
1770status_t CameraDeviceClient::setCameraMute(bool enabled) {
1771 return mDevice->setCameraMute(enabled);
1772}
1773
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001774binder::Status CameraDeviceClient::switchToOffline(
1775 const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb,
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001776 const std::vector<int>& offlineOutputIds,
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001777 /*out*/
1778 sp<hardware::camera2::ICameraOfflineSession>* session) {
1779 ATRACE_CALL();
1780
1781 binder::Status res;
1782 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1783
1784 Mutex::Autolock icl(mBinderSerializationLock);
1785
1786 if (!mDevice.get()) {
1787 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1788 }
1789
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001790 if (offlineOutputIds.empty()) {
Emilian Peevcc0b7952020-01-07 13:54:47 -08001791 String8 msg = String8::format("Offline surfaces must not be empty");
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001792 ALOGE("%s: %s", __FUNCTION__, msg.string());
1793 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1794 }
1795
1796 if (session == nullptr) {
1797 String8 msg = String8::format("Invalid offline session");
1798 ALOGE("%s: %s", __FUNCTION__, msg.string());
1799 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1800 }
1801
Yin-Chia Yeh87fccca2020-01-28 09:37:18 -08001802 std::vector<int32_t> offlineStreamIds;
1803 offlineStreamIds.reserve(offlineOutputIds.size());
Emilian Peev4697b642019-11-19 17:11:14 -08001804 KeyedVector<sp<IBinder>, sp<CompositeStream>> offlineCompositeStreamMap;
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001805 for (const auto& streamId : offlineOutputIds) {
1806 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001807 if (index == NAME_NOT_FOUND) {
Emilian Peevcc0b7952020-01-07 13:54:47 -08001808 String8 msg = String8::format("Offline surface with id: %d is not registered",
1809 streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001810 ALOGE("%s: %s", __FUNCTION__, msg.string());
1811 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1812 }
Emilian Peevcc0b7952020-01-07 13:54:47 -08001813
1814 if (!mStreamInfoMap[streamId].supportsOffline) {
1815 String8 msg = String8::format("Offline surface with id: %d doesn't support "
1816 "offline mode", streamId);
1817 ALOGE("%s: %s", __FUNCTION__, msg.string());
1818 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1819 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001820
Emilian Peev2f5d6012022-01-19 16:16:50 -08001821 Mutex::Autolock l(mCompositeLock);
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001822 bool isCompositeStream = false;
Emilian Peev2f5d6012022-01-19 16:16:50 -08001823 for (const auto& gbp : mConfiguredOutputs.valueAt(index).getGraphicBufferProducers()) {
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001824 sp<Surface> s = new Surface(gbp, false /*controlledByApp*/);
Pirama Arumuga Nainar8db21062022-01-28 10:15:12 -08001825 isCompositeStream = camera3::DepthCompositeStream::isDepthCompositeStream(s) ||
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001826 camera3::HeicCompositeStream::isHeicCompositeStream(s);
Emilian Peev4697b642019-11-19 17:11:14 -08001827 if (isCompositeStream) {
1828 auto compositeIdx = mCompositeStreamMap.indexOfKey(IInterface::asBinder(gbp));
1829 if (compositeIdx == NAME_NOT_FOUND) {
1830 ALOGE("%s: Unknown composite stream", __FUNCTION__);
1831 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1832 "Unknown composite stream");
1833 }
1834
1835 mCompositeStreamMap.valueAt(compositeIdx)->insertCompositeStreamIds(
1836 &offlineStreamIds);
1837 offlineCompositeStreamMap.add(mCompositeStreamMap.keyAt(compositeIdx),
1838 mCompositeStreamMap.valueAt(compositeIdx));
1839 break;
1840 }
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001841 }
1842
Emilian Peev4697b642019-11-19 17:11:14 -08001843 if (!isCompositeStream) {
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001844 offlineStreamIds.push_back(streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001845 }
1846 }
1847
1848 sp<CameraOfflineSessionBase> offlineSession;
1849 auto ret = mDevice->switchToOffline(offlineStreamIds, &offlineSession);
1850 if (ret != OK) {
1851 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1852 "Camera %s: Error switching to offline mode: %s (%d)",
1853 mCameraIdStr.string(), strerror(ret), ret);
1854 }
1855
Emilian Peevcc0b7952020-01-07 13:54:47 -08001856 sp<CameraOfflineSessionClient> offlineClient;
1857 if (offlineSession.get() != nullptr) {
1858 offlineClient = new CameraOfflineSessionClient(sCameraService,
1859 offlineSession, offlineCompositeStreamMap, cameraCb, mClientPackageName,
Emilian Peev8b64f282021-03-25 16:49:57 -07001860 mClientFeatureId, mCameraIdStr, mCameraFacing, mOrientation, mClientPid, mClientUid,
1861 mServicePid);
Emilian Peevcc0b7952020-01-07 13:54:47 -08001862 ret = sCameraService->addOfflineClient(mCameraIdStr, offlineClient);
1863 }
1864
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001865 if (ret == OK) {
Emilian Peevd99c8ae2019-11-26 13:19:13 -08001866 // A successful offline session switch must reset the current camera client
1867 // and release any resources occupied by previously configured streams.
1868 mStreamMap.clear();
1869 mConfiguredOutputs.clear();
1870 mDeferredStreams.clear();
1871 mStreamInfoMap.clear();
Emilian Peev2f5d6012022-01-19 16:16:50 -08001872 Mutex::Autolock l(mCompositeLock);
Emilian Peevd99c8ae2019-11-26 13:19:13 -08001873 mCompositeStreamMap.clear();
1874 mInputStream = {false, 0, 0, 0, 0};
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001875 } else {
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001876 switch(ret) {
1877 case BAD_VALUE:
1878 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1879 "Illegal argument to HAL module for camera \"%s\"", mCameraIdStr.c_str());
1880 case TIMED_OUT:
1881 return STATUS_ERROR_FMT(CameraService::ERROR_CAMERA_IN_USE,
1882 "Camera \"%s\" is already open", mCameraIdStr.c_str());
1883 default:
1884 return STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1885 "Failed to initialize camera \"%s\": %s (%d)", mCameraIdStr.c_str(),
1886 strerror(-ret), ret);
1887 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001888 }
1889
1890 *session = offlineClient;
1891
1892 return binder::Status::ok();
1893}
1894
Igor Murashkine7ee7632013-06-11 18:10:18 -07001895status_t CameraDeviceClient::dump(int fd, const Vector<String16>& args) {
Eino-Ville Talvalac4003962016-01-13 10:07:04 -08001896 return BasicClient::dump(fd, args);
1897}
1898
1899status_t CameraDeviceClient::dumpClient(int fd, const Vector<String16>& args) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001900 dprintf(fd, " CameraDeviceClient[%s] (%p) dump:\n",
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001901 mCameraIdStr.string(),
Eino-Ville Talvalae992e752014-11-07 16:17:48 -08001902 (getRemoteCallback() != NULL ?
Marco Nelissenf8880202014-11-14 07:58:25 -08001903 IInterface::asBinder(getRemoteCallback()).get() : NULL) );
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001904 dprintf(fd, " Current client UID %u\n", mClientUid);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001905
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001906 dprintf(fd, " State:\n");
1907 dprintf(fd, " Request ID counter: %d\n", mRequestIdCounter);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001908 if (mInputStream.configured) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001909 dprintf(fd, " Current input stream ID: %d\n", mInputStream.id);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001910 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001911 dprintf(fd, " No input stream configured.\n");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001912 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001913 if (!mStreamMap.isEmpty()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001914 dprintf(fd, " Current output stream/surface IDs:\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001915 for (size_t i = 0; i < mStreamMap.size(); i++) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001916 dprintf(fd, " Stream %d Surface %d\n",
Shuzhen Wang0129d522016-10-30 22:43:41 -07001917 mStreamMap.valueAt(i).streamId(),
1918 mStreamMap.valueAt(i).surfaceId());
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001919 }
Zhijun He5d677d12016-05-29 16:52:39 -07001920 } else if (!mDeferredStreams.isEmpty()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001921 dprintf(fd, " Current deferred surface output stream IDs:\n");
Zhijun He5d677d12016-05-29 16:52:39 -07001922 for (auto& streamId : mDeferredStreams) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001923 dprintf(fd, " Stream %d\n", streamId);
Zhijun He5d677d12016-05-29 16:52:39 -07001924 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001925 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001926 dprintf(fd, " No output streams configured.\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001927 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001928 // TODO: print dynamic/request section from most recent requests
1929 mFrameProcessor->dump(fd, args);
1930
1931 return dumpDevice(fd, args);
1932}
1933
Avichal Rakesh7e53cad2021-10-05 13:46:30 -07001934status_t CameraDeviceClient::startWatchingTags(const String8 &tags, int out) {
1935 sp<CameraDeviceBase> device = mDevice;
1936 if (!device) {
1937 dprintf(out, " Device is detached.");
1938 return OK;
1939 }
1940 device->startWatchingTags(tags);
1941 return OK;
1942}
1943
1944status_t CameraDeviceClient::stopWatchingTags(int out) {
1945 sp<CameraDeviceBase> device = mDevice;
1946 if (!device) {
1947 dprintf(out, " Device is detached.");
1948 return OK;
1949 }
1950 device->stopWatchingTags();
1951 return OK;
1952}
1953
1954status_t CameraDeviceClient::dumpWatchedEventsToVector(std::vector<std::string> &out) {
1955 sp<CameraDeviceBase> device = mDevice;
1956 if (!device) {
1957 return OK;
1958 }
1959 device->dumpWatchedEventsToVector(out);
1960 return OK;
1961}
1962
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001963void CameraDeviceClient::notifyError(int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -07001964 const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001965 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001966 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001967
Emilian Peev538c90e2018-12-17 18:03:19 +00001968 bool skipClientNotification = false;
Emilian Peev2f5d6012022-01-19 16:16:50 -08001969 {
1970 // Access to the composite stream map must be synchronized
1971 Mutex::Autolock l(mCompositeLock);
1972 // Composites can have multiple internal streams. Error notifications coming from such
1973 // internal streams may need to remain within camera service.
1974 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
1975 skipClientNotification |= mCompositeStreamMap.valueAt(i)->onError(errorCode,
1976 resultExtras);
1977 }
Emilian Peev538c90e2018-12-17 18:03:19 +00001978 }
1979
1980 if ((remoteCb != 0) && (!skipClientNotification)) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001981 remoteCb->onDeviceError(errorCode, resultExtras);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001982 }
1983}
1984
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001985void CameraDeviceClient::notifyRepeatingRequestError(long lastFrameNumber) {
1986 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
1987
1988 if (remoteCb != 0) {
Yin-Chia Yeh8ca23dc2017-09-05 18:15:56 -07001989 remoteCb->onRepeatingRequestError(lastFrameNumber, mStreamingRequestId);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001990 }
1991
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001992 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001993 mStreamingRequestId = REQUEST_ID_NONE;
1994}
1995
Shuzhen Wang316781a2020-08-18 18:11:01 -07001996void CameraDeviceClient::notifyIdle(
1997 int64_t requestCount, int64_t resultErrorCount, bool deviceError,
1998 const std::vector<hardware::CameraStreamStats>& streamStats) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001999 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002000 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002001
2002 if (remoteCb != 0) {
2003 remoteCb->onDeviceIdle();
2004 }
Shuzhen Wangd26b1862022-03-07 12:05:05 -08002005 Camera2ClientBase::notifyIdleWithUserTag(requestCount, resultErrorCount, deviceError,
Shuzhen Wang9372b0b2022-05-11 18:55:19 -07002006 streamStats, mUserTag, mVideoStabilizationMode);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002007}
2008
Jianing Weicb0652e2014-03-12 18:29:36 -07002009void CameraDeviceClient::notifyShutter(const CaptureResultExtras& resultExtras,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002010 nsecs_t timestamp) {
2011 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002012 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002013 if (remoteCb != 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -07002014 remoteCb->onCaptureStarted(resultExtras, timestamp);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002015 }
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07002016 Camera2ClientBase::notifyShutter(resultExtras, timestamp);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08002017
Emilian Peev2f5d6012022-01-19 16:16:50 -08002018 // Access to the composite stream map must be synchronized
2019 Mutex::Autolock l(mCompositeLock);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08002020 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
2021 mCompositeStreamMap.valueAt(i)->onShutter(resultExtras, timestamp);
2022 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002023}
2024
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002025void CameraDeviceClient::notifyPrepared(int streamId) {
2026 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002027 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002028 if (remoteCb != 0) {
2029 remoteCb->onPrepared(streamId);
2030 }
2031}
2032
Shuzhen Wang9d066012016-09-30 11:30:20 -07002033void CameraDeviceClient::notifyRequestQueueEmpty() {
2034 // Thread safe. Don't bother locking.
2035 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
2036 if (remoteCb != 0) {
2037 remoteCb->onRequestQueueEmpty();
2038 }
2039}
2040
Igor Murashkine7ee7632013-06-11 18:10:18 -07002041void CameraDeviceClient::detachDevice() {
2042 if (mDevice == 0) return;
2043
Shuzhen Wang316781a2020-08-18 18:11:01 -07002044 nsecs_t startTime = systemTime();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002045 ALOGV("Camera %s: Stopping processors", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -07002046
Emilian Peev7ee731f2022-02-08 14:55:52 -08002047 if (mFrameProcessor.get() != nullptr) {
2048 mFrameProcessor->removeListener(
2049 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MIN_ID,
2050 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MAX_ID, /*listener*/this);
2051 mFrameProcessor->requestExit();
2052 ALOGV("Camera %s: Waiting for threads", mCameraIdStr.string());
2053 mFrameProcessor->join();
2054 ALOGV("Camera %s: Disconnecting device", mCameraIdStr.string());
2055 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07002056
2057 // WORKAROUND: HAL refuses to disconnect while there's streams in flight
2058 {
Emilian Peev6b51d7d2018-07-23 11:41:44 +01002059 int64_t lastFrameNumber;
Igor Murashkine7ee7632013-06-11 18:10:18 -07002060 status_t code;
Emilian Peev6b51d7d2018-07-23 11:41:44 +01002061 if ((code = mDevice->flush(&lastFrameNumber)) != OK) {
2062 ALOGE("%s: flush failed with code 0x%x", __FUNCTION__, code);
2063 }
2064
Igor Murashkine7ee7632013-06-11 18:10:18 -07002065 if ((code = mDevice->waitUntilDrained()) != OK) {
2066 ALOGE("%s: waitUntilDrained failed with code 0x%x", __FUNCTION__,
2067 code);
2068 }
2069 }
2070
Emilian Peev2f5d6012022-01-19 16:16:50 -08002071 {
2072 Mutex::Autolock l(mCompositeLock);
2073 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
2074 auto ret = mCompositeStreamMap.valueAt(i)->deleteInternalStreams();
2075 if (ret != OK) {
2076 ALOGE("%s: Failed removing composite stream %s (%d)", __FUNCTION__,
2077 strerror(-ret), ret);
2078 }
Emilian Peev5e4c7322019-10-22 14:20:52 -07002079 }
Emilian Peev2f5d6012022-01-19 16:16:50 -08002080 mCompositeStreamMap.clear();
Emilian Peev5e4c7322019-10-22 14:20:52 -07002081 }
Emilian Peev5e4c7322019-10-22 14:20:52 -07002082
Igor Murashkine7ee7632013-06-11 18:10:18 -07002083 Camera2ClientBase::detachDevice();
Shuzhen Wang316781a2020-08-18 18:11:01 -07002084
2085 int32_t closeLatencyMs = ns2ms(systemTime() - startTime);
Austin Borger74fca042022-05-23 12:41:21 -07002086 mCameraServiceProxyWrapper->logClose(mCameraIdStr, closeLatencyMs);
Igor Murashkine7ee7632013-06-11 18:10:18 -07002087}
2088
2089/** Device-related methods */
Jianing Weicb0652e2014-03-12 18:29:36 -07002090void CameraDeviceClient::onResultAvailable(const CaptureResult& result) {
Igor Murashkine7ee7632013-06-11 18:10:18 -07002091 ATRACE_CALL();
2092 ALOGV("%s", __FUNCTION__);
2093
Igor Murashkin4fb55c12013-08-29 17:43:01 -07002094 // Thread-safe. No lock necessary.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002095 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = mRemoteCallback;
Igor Murashkin4fb55c12013-08-29 17:43:01 -07002096 if (remoteCb != NULL) {
Shuzhen Wang5c22c152017-12-31 17:12:25 -08002097 remoteCb->onResultReceived(result.mMetadata, result.mResultExtras,
2098 result.mPhysicalMetadatas);
Igor Murashkine7ee7632013-06-11 18:10:18 -07002099 }
Emilian Peev538c90e2018-12-17 18:03:19 +00002100
Emilian Peev2f5d6012022-01-19 16:16:50 -08002101 // Access to the composite stream map must be synchronized
2102 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +00002103 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
2104 mCompositeStreamMap.valueAt(i)->onResultAvailable(result);
2105 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07002106}
2107
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002108binder::Status CameraDeviceClient::checkPidStatus(const char* checkLocation) {
Eino-Ville Talvala6192b892016-04-04 12:31:18 -07002109 if (mDisconnected) {
2110 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED,
2111 "The camera device has been disconnected");
2112 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002113 status_t res = checkPid(checkLocation);
2114 return (res == OK) ? binder::Status::ok() :
2115 STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
2116 "Attempt to use camera from a different process than original client");
2117}
2118
Igor Murashkine7ee7632013-06-11 18:10:18 -07002119// TODO: move to Camera2ClientBase
2120bool CameraDeviceClient::enforceRequestPermissions(CameraMetadata& metadata) {
2121
Jayant Chowdhary12361932018-08-27 14:46:13 -07002122 const int pid = CameraThreadState::getCallingPid();
Igor Murashkine7ee7632013-06-11 18:10:18 -07002123 const int selfPid = getpid();
2124 camera_metadata_entry_t entry;
2125
2126 /**
2127 * Mixin default important security values
2128 * - android.led.transmit = defaulted ON
2129 */
2130 CameraMetadata staticInfo = mDevice->info();
2131 entry = staticInfo.find(ANDROID_LED_AVAILABLE_LEDS);
2132 for(size_t i = 0; i < entry.count; ++i) {
2133 uint8_t led = entry.data.u8[i];
2134
2135 switch(led) {
2136 case ANDROID_LED_AVAILABLE_LEDS_TRANSMIT: {
2137 uint8_t transmitDefault = ANDROID_LED_TRANSMIT_ON;
2138 if (!metadata.exists(ANDROID_LED_TRANSMIT)) {
2139 metadata.update(ANDROID_LED_TRANSMIT,
2140 &transmitDefault, 1);
2141 }
2142 break;
2143 }
2144 }
2145 }
2146
2147 // We can do anything!
2148 if (pid == selfPid) {
2149 return true;
2150 }
2151
2152 /**
2153 * Permission check special fields in the request
2154 * - android.led.transmit = android.permission.CAMERA_DISABLE_TRANSMIT
2155 */
2156 entry = metadata.find(ANDROID_LED_TRANSMIT);
2157 if (entry.count > 0 && entry.data.u8[0] != ANDROID_LED_TRANSMIT_ON) {
2158 String16 permissionString =
2159 String16("android.permission.CAMERA_DISABLE_TRANSMIT_LED");
2160 if (!checkCallingPermission(permissionString)) {
Jayant Chowdhary12361932018-08-27 14:46:13 -07002161 const int uid = CameraThreadState::getCallingUid();
Igor Murashkine7ee7632013-06-11 18:10:18 -07002162 ALOGE("Permission Denial: "
2163 "can't disable transmit LED pid=%d, uid=%d", pid, uid);
2164 return false;
2165 }
2166 }
2167
2168 return true;
2169}
2170
Shuzhen Wang610d7b82022-02-08 14:37:22 -08002171status_t CameraDeviceClient::getRotationTransformLocked(int mirrorMode,
2172 int32_t* transform) {
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07002173 ALOGV("%s: begin", __FUNCTION__);
2174
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07002175 const CameraMetadata& staticInfo = mDevice->info();
Shuzhen Wang610d7b82022-02-08 14:37:22 -08002176 return CameraUtils::getRotationTransform(staticInfo, mirrorMode, transform);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07002177}
2178
Emilian Peevf4816702020-04-03 15:44:51 -07002179binder::Status CameraDeviceClient::mapRequestTemplate(int templateId,
2180 camera_request_template_t* tempId /*out*/) {
2181 binder::Status ret = binder::Status::ok();
2182
2183 if (tempId == nullptr) {
2184 ret = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
2185 "Camera %s: Invalid template argument", mCameraIdStr.string());
2186 return ret;
2187 }
2188 switch(templateId) {
2189 case ICameraDeviceUser::TEMPLATE_PREVIEW:
2190 *tempId = camera_request_template_t::CAMERA_TEMPLATE_PREVIEW;
2191 break;
2192 case ICameraDeviceUser::TEMPLATE_RECORD:
2193 *tempId = camera_request_template_t::CAMERA_TEMPLATE_VIDEO_RECORD;
2194 break;
2195 case ICameraDeviceUser::TEMPLATE_STILL_CAPTURE:
2196 *tempId = camera_request_template_t::CAMERA_TEMPLATE_STILL_CAPTURE;
2197 break;
2198 case ICameraDeviceUser::TEMPLATE_VIDEO_SNAPSHOT:
2199 *tempId = camera_request_template_t::CAMERA_TEMPLATE_VIDEO_SNAPSHOT;
2200 break;
2201 case ICameraDeviceUser::TEMPLATE_ZERO_SHUTTER_LAG:
2202 *tempId = camera_request_template_t::CAMERA_TEMPLATE_ZERO_SHUTTER_LAG;
2203 break;
2204 case ICameraDeviceUser::TEMPLATE_MANUAL:
2205 *tempId = camera_request_template_t::CAMERA_TEMPLATE_MANUAL;
2206 break;
2207 default:
2208 ret = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
2209 "Camera %s: Template ID %d is invalid or not supported",
2210 mCameraIdStr.string(), templateId);
2211 return ret;
2212 }
2213
2214 return ret;
2215}
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08002216
2217const CameraMetadata &CameraDeviceClient::getStaticInfo(const String8 &cameraId) {
2218 if (mDevice->getId() == cameraId) {
2219 return mDevice->info();
2220 }
2221 return mDevice->infoPhysical(cameraId);
2222}
2223
2224bool CameraDeviceClient::isUltraHighResolutionSensor(const String8 &cameraId) {
2225 const CameraMetadata &deviceInfo = getStaticInfo(cameraId);
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07002226 return SessionConfigurationUtils::isUltraHighResolutionSensor(deviceInfo);
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08002227}
2228
2229bool CameraDeviceClient::isSensorPixelModeConsistent(
2230 const std::list<int> &streamIdList, const CameraMetadata &settings) {
2231 // First we get the sensorPixelMode from the settings metadata.
2232 int32_t sensorPixelMode = ANDROID_SENSOR_PIXEL_MODE_DEFAULT;
2233 camera_metadata_ro_entry sensorPixelModeEntry = settings.find(ANDROID_SENSOR_PIXEL_MODE);
2234 if (sensorPixelModeEntry.count != 0) {
2235 sensorPixelMode = sensorPixelModeEntry.data.u8[0];
2236 if (sensorPixelMode != ANDROID_SENSOR_PIXEL_MODE_DEFAULT &&
2237 sensorPixelMode != ANDROID_SENSOR_PIXEL_MODE_MAXIMUM_RESOLUTION) {
2238 ALOGE("%s: Request sensor pixel mode not is not one of the valid values %d",
2239 __FUNCTION__, sensorPixelMode);
2240 return false;
2241 }
2242 }
2243 // Check whether each stream has max resolution allowed.
2244 bool consistent = true;
2245 for (auto it : streamIdList) {
2246 auto const streamInfoIt = mStreamInfoMap.find(it);
2247 if (streamInfoIt == mStreamInfoMap.end()) {
2248 ALOGE("%s: stream id %d not created, skipping", __FUNCTION__, it);
2249 return false;
2250 }
2251 consistent =
2252 streamInfoIt->second.sensorPixelModesUsed.find(sensorPixelMode) !=
2253 streamInfoIt->second.sensorPixelModesUsed.end();
2254 if (!consistent) {
2255 ALOGE("sensorPixelMode used %i not consistent with configured modes", sensorPixelMode);
2256 for (auto m : streamInfoIt->second.sensorPixelModesUsed) {
2257 ALOGE("sensor pixel mode used list: %i", m);
2258 }
2259 break;
2260 }
2261 }
2262
2263 return consistent;
2264}
2265
Igor Murashkine7ee7632013-06-11 18:10:18 -07002266} // namespace android