blob: c08aff30f2c53efcd6746a47b1d418d845ff5f0d [file] [log] [blame]
Igor Murashkine7ee7632013-06-11 18:10:18 -07001/*
Shuzhen Wangc28189a2017-11-27 23:05:10 -08002 * Copyright (C) 2013-2018 The Android Open Source Project
Igor Murashkine7ee7632013-06-11 18:10:18 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "CameraDeviceClient"
18#define ATRACE_TAG ATRACE_TAG_CAMERA
Jianing Weicb0652e2014-03-12 18:29:36 -070019//#define LOG_NDEBUG 0
Igor Murashkine7ee7632013-06-11 18:10:18 -070020
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070021#include <cutils/properties.h>
Jayant Chowdhary12361932018-08-27 14:46:13 -070022#include <utils/CameraThreadState.h>
Igor Murashkine7ee7632013-06-11 18:10:18 -070023#include <utils/Log.h>
Colin Crossb8a9dbb2020-08-27 04:12:26 +000024#include <utils/SessionConfigurationUtils.h>
Igor Murashkine7ee7632013-06-11 18:10:18 -070025#include <utils/Trace.h>
Igor Murashkine7ee7632013-06-11 18:10:18 -070026#include <gui/Surface.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070027#include <camera/camera2/CaptureRequest.h>
Ruben Brunk5698d442014-06-18 10:39:40 -070028#include <camera/CameraUtils.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070029
30#include "common/CameraDeviceBase.h"
Emilian Peev35ae8262018-11-08 13:11:32 +000031#include "device3/Camera3Device.h"
32#include "device3/Camera3OutputStream.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070033#include "api2/CameraDeviceClient.h"
34
Emilian Peev00420d22018-02-05 21:33:13 +000035#include <camera_metadata_hidden.h>
36
Emilian Peev538c90e2018-12-17 18:03:19 +000037#include "DepthCompositeStream.h"
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -080038#include "HeicCompositeStream.h"
Emilian Peev434248e2022-10-06 14:58:54 -070039#include "JpegRCompositeStream.h"
Emilian Peev538c90e2018-12-17 18:03:19 +000040
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080041// Convenience methods for constructing binder::Status objects for error returns
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070042
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080043#define STATUS_ERROR(errorCode, errorString) \
44 binder::Status::fromServiceSpecificError(errorCode, \
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -080045 String8::format("%s:%d: %s", __FUNCTION__, __LINE__, errorString))
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080046
47#define STATUS_ERROR_FMT(errorCode, errorString, ...) \
48 binder::Status::fromServiceSpecificError(errorCode, \
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -080049 String8::format("%s:%d: " errorString, __FUNCTION__, __LINE__, \
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080050 __VA_ARGS__))
Igor Murashkine7ee7632013-06-11 18:10:18 -070051
52namespace android {
53using namespace camera2;
Austin Borgerea931242021-12-13 23:10:41 +000054using namespace camera3;
Emilian Peevf4816702020-04-03 15:44:51 -070055using camera3::camera_stream_rotation_t::CAMERA_STREAM_ROTATION_0;
Igor Murashkine7ee7632013-06-11 18:10:18 -070056
57CameraDeviceClientBase::CameraDeviceClientBase(
58 const sp<CameraService>& cameraService,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080059 const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback,
Igor Murashkine7ee7632013-06-11 18:10:18 -070060 const String16& clientPackageName,
Jayant Chowdharyeb0169f2022-01-31 00:00:02 -080061 bool systemNativeClient,
Jooyung Han3f9a3b42020-01-23 12:27:18 +090062 const std::optional<String16>& clientFeatureId,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080063 const String8& cameraId,
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -080064 int api1CameraId,
Igor Murashkine7ee7632013-06-11 18:10:18 -070065 int cameraFacing,
Emilian Peev8b64f282021-03-25 16:49:57 -070066 int sensorOrientation,
Igor Murashkine7ee7632013-06-11 18:10:18 -070067 int clientPid,
68 uid_t clientUid,
Austin Borger18b30a72022-10-27 12:20:29 -070069 int servicePid,
70 bool overrideToPortrait) :
Eino-Ville Talvalae992e752014-11-07 16:17:48 -080071 BasicClient(cameraService,
Marco Nelissenf8880202014-11-14 07:58:25 -080072 IInterface::asBinder(remoteCallback),
Eino-Ville Talvalae992e752014-11-07 16:17:48 -080073 clientPackageName,
Jayant Chowdharyeb0169f2022-01-31 00:00:02 -080074 systemNativeClient,
Philip P. Moltmann9e648f62019-11-04 12:52:45 -080075 clientFeatureId,
Eino-Ville Talvalae992e752014-11-07 16:17:48 -080076 cameraId,
77 cameraFacing,
Emilian Peev8b64f282021-03-25 16:49:57 -070078 sensorOrientation,
Eino-Ville Talvalae992e752014-11-07 16:17:48 -080079 clientPid,
80 clientUid,
Austin Borger18b30a72022-10-27 12:20:29 -070081 servicePid,
82 overrideToPortrait),
Igor Murashkine7ee7632013-06-11 18:10:18 -070083 mRemoteCallback(remoteCallback) {
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -080084 // We don't need it for API2 clients, but Camera2ClientBase requires it.
85 (void) api1CameraId;
Igor Murashkine7ee7632013-06-11 18:10:18 -070086}
Igor Murashkine7ee7632013-06-11 18:10:18 -070087
88// Interface used by CameraService
89
90CameraDeviceClient::CameraDeviceClient(const sp<CameraService>& cameraService,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080091 const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback,
Austin Borger74fca042022-05-23 12:41:21 -070092 std::shared_ptr<CameraServiceProxyWrapper> cameraServiceProxyWrapper,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080093 const String16& clientPackageName,
Jayant Chowdharyeb0169f2022-01-31 00:00:02 -080094 bool systemNativeClient,
Jooyung Han3f9a3b42020-01-23 12:27:18 +090095 const std::optional<String16>& clientFeatureId,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080096 const String8& cameraId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080097 int cameraFacing,
Emilian Peev8b64f282021-03-25 16:49:57 -070098 int sensorOrientation,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080099 int clientPid,
100 uid_t clientUid,
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700101 int servicePid,
Austin Borger18b30a72022-10-27 12:20:29 -0700102 bool overrideForPerfClass,
103 bool overrideToPortrait) :
Austin Borger74fca042022-05-23 12:41:21 -0700104 Camera2ClientBase(cameraService, remoteCallback, cameraServiceProxyWrapper, clientPackageName,
105 systemNativeClient, clientFeatureId, cameraId, /*API1 camera ID*/ -1, cameraFacing,
Austin Borger18b30a72022-10-27 12:20:29 -0700106 sensorOrientation, clientPid, clientUid, servicePid, overrideForPerfClass,
107 overrideToPortrait),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700108 mInputStream(),
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700109 mStreamingRequestId(REQUEST_ID_NONE),
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700110 mRequestIdCounter(0),
111 mOverrideForPerfClass(overrideForPerfClass) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700112
113 ATRACE_CALL();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800114 ALOGI("CameraDeviceClient %s: Opened", cameraId.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700115}
116
Emilian Peevbd8c5032018-02-14 23:05:40 +0000117status_t CameraDeviceClient::initialize(sp<CameraProviderManager> manager,
118 const String8& monitorTags) {
119 return initializeImpl(manager, monitorTags);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800120}
121
122template<typename TProviderPtr>
Emilian Peevbd8c5032018-02-14 23:05:40 +0000123status_t CameraDeviceClient::initializeImpl(TProviderPtr providerPtr, const String8& monitorTags) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700124 ATRACE_CALL();
125 status_t res;
126
Emilian Peevbd8c5032018-02-14 23:05:40 +0000127 res = Camera2ClientBase::initialize(providerPtr, monitorTags);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700128 if (res != OK) {
129 return res;
130 }
131
132 String8 threadName;
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -0700133 mFrameProcessor = new FrameProcessorBase(mDevice);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800134 threadName = String8::format("CDU-%s-FrameProc", mCameraIdStr.string());
Austin Borger7b129542022-06-09 13:23:06 -0700135 res = mFrameProcessor->run(threadName.string());
136 if (res != OK) {
137 ALOGE("%s: Unable to start frame processor thread: %s (%d)",
138 __FUNCTION__, strerror(-res), res);
139 return res;
140 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700141
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800142 mFrameProcessor->registerListener(camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MIN_ID,
143 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MAX_ID,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -0800144 /*listener*/this,
Zhijun He25a0aef2014-06-25 11:40:02 -0700145 /*sendPartials*/true);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700146
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800147 const CameraMetadata &deviceInfo = mDevice->info();
148 camera_metadata_ro_entry_t physicalKeysEntry = deviceInfo.find(
Emilian Peev00420d22018-02-05 21:33:13 +0000149 ANDROID_REQUEST_AVAILABLE_PHYSICAL_CAMERA_REQUEST_KEYS);
150 if (physicalKeysEntry.count > 0) {
151 mSupportedPhysicalRequestKeys.insert(mSupportedPhysicalRequestKeys.begin(),
152 physicalKeysEntry.data.i32,
153 physicalKeysEntry.data.i32 + physicalKeysEntry.count);
154 }
155
Emilian Peev2295df72021-11-12 18:14:10 -0800156 auto entry = deviceInfo.find(ANDROID_REQUEST_AVAILABLE_CAPABILITIES);
157 mDynamicProfileMap.emplace(
158 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD,
159 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD);
160 if (entry.count > 0) {
Shuzhen Wangc8ab4522021-12-14 20:12:42 -0800161 const auto it = std::find(entry.data.u8, entry.data.u8 + entry.count,
Emilian Peev2295df72021-11-12 18:14:10 -0800162 ANDROID_REQUEST_AVAILABLE_CAPABILITIES_DYNAMIC_RANGE_TEN_BIT);
Shuzhen Wangc8ab4522021-12-14 20:12:42 -0800163 if (it != entry.data.u8 + entry.count) {
Emilian Peev2295df72021-11-12 18:14:10 -0800164 entry = deviceInfo.find(ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP);
Emilian Peevc81a7592022-02-14 17:38:18 -0800165 if (entry.count > 0 || ((entry.count % 3) != 0)) {
166 int64_t standardBitmap =
167 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD;
168 for (size_t i = 0; i < entry.count; i += 3) {
169 if (entry.data.i64[i] !=
Emilian Peev2295df72021-11-12 18:14:10 -0800170 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD) {
Emilian Peevc81a7592022-02-14 17:38:18 -0800171 mDynamicProfileMap.emplace(entry.data.i64[i], entry.data.i64[i+1]);
172 if ((entry.data.i64[i+1] == 0) || (entry.data.i64[i+1] &
Emilian Peev2295df72021-11-12 18:14:10 -0800173 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD)) {
Emilian Peevc81a7592022-02-14 17:38:18 -0800174 standardBitmap |= entry.data.i64[i];
Emilian Peev2295df72021-11-12 18:14:10 -0800175 }
176 } else {
Emilian Peevc81a7592022-02-14 17:38:18 -0800177 ALOGE("%s: Device %s includes unexpected profile entry: 0x%" PRIx64 "!",
178 __FUNCTION__, mCameraIdStr.c_str(), entry.data.i64[i]);
Emilian Peev2295df72021-11-12 18:14:10 -0800179 }
180 }
Emilian Peevef715e22022-05-19 17:44:33 -0700181 mDynamicProfileMap[ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD] =
182 standardBitmap;
Emilian Peev2295df72021-11-12 18:14:10 -0800183 } else {
184 ALOGE("%s: Device %s supports 10-bit output but doesn't include a dynamic range"
185 " profile map!", __FUNCTION__, mCameraIdStr.c_str());
186 }
187 }
188 }
189
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700190 mProviderManager = providerPtr;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800191 // Cache physical camera ids corresponding to this device and also the high
192 // resolution sensors in this device + physical camera ids
193 mProviderManager->isLogicalCamera(mCameraIdStr.string(), &mPhysicalCameraIds);
194 if (isUltraHighResolutionSensor(mCameraIdStr)) {
195 mHighResolutionSensors.insert(mCameraIdStr.string());
196 }
197 for (auto &physicalId : mPhysicalCameraIds) {
198 if (isUltraHighResolutionSensor(String8(physicalId.c_str()))) {
199 mHighResolutionSensors.insert(physicalId.c_str());
200 }
201 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700202 return OK;
203}
204
205CameraDeviceClient::~CameraDeviceClient() {
206}
207
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800208binder::Status CameraDeviceClient::submitRequest(
209 const hardware::camera2::CaptureRequest& request,
210 bool streaming,
211 /*out*/
212 hardware::camera2::utils::SubmitInfo *submitInfo) {
213 std::vector<hardware::camera2::CaptureRequest> requestList = { request };
214 return submitRequestList(requestList, streaming, submitInfo);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700215}
216
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800217binder::Status CameraDeviceClient::insertGbpLocked(const sp<IGraphicBufferProducer>& gbp,
Emilian Peevf873aa52018-01-26 14:58:28 +0000218 SurfaceMap* outSurfaceMap, Vector<int32_t>* outputStreamIds, int32_t *currentStreamId) {
Emilian Peev538c90e2018-12-17 18:03:19 +0000219 int compositeIdx;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800220 int idx = mStreamMap.indexOfKey(IInterface::asBinder(gbp));
221
Emilian Peev2f5d6012022-01-19 16:16:50 -0800222 Mutex::Autolock l(mCompositeLock);
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800223 // Trying to submit request with surface that wasn't created
224 if (idx == NAME_NOT_FOUND) {
225 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
226 " we have not called createStream on",
227 __FUNCTION__, mCameraIdStr.string());
228 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
229 "Request targets Surface that is not part of current capture session");
Emilian Peev538c90e2018-12-17 18:03:19 +0000230 } else if ((compositeIdx = mCompositeStreamMap.indexOfKey(IInterface::asBinder(gbp)))
231 != NAME_NOT_FOUND) {
232 mCompositeStreamMap.valueAt(compositeIdx)->insertGbp(outSurfaceMap, outputStreamIds,
233 currentStreamId);
234 return binder::Status::ok();
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800235 }
236
237 const StreamSurfaceId& streamSurfaceId = mStreamMap.valueAt(idx);
238 if (outSurfaceMap->find(streamSurfaceId.streamId()) == outSurfaceMap->end()) {
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800239 outputStreamIds->push_back(streamSurfaceId.streamId());
240 }
241 (*outSurfaceMap)[streamSurfaceId.streamId()].push_back(streamSurfaceId.surfaceId());
242
243 ALOGV("%s: Camera %s: Appending output stream %d surface %d to request",
244 __FUNCTION__, mCameraIdStr.string(), streamSurfaceId.streamId(),
245 streamSurfaceId.surfaceId());
246
Emilian Peevf873aa52018-01-26 14:58:28 +0000247 if (currentStreamId != nullptr) {
248 *currentStreamId = streamSurfaceId.streamId();
249 }
250
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800251 return binder::Status::ok();
252}
253
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800254static std::list<int> getIntersection(const std::unordered_set<int> &streamIdsForThisCamera,
255 const Vector<int> &streamIdsForThisRequest) {
256 std::list<int> intersection;
257 for (auto &streamId : streamIdsForThisRequest) {
258 if (streamIdsForThisCamera.find(streamId) != streamIdsForThisCamera.end()) {
259 intersection.emplace_back(streamId);
260 }
261 }
262 return intersection;
263}
264
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800265binder::Status CameraDeviceClient::submitRequestList(
266 const std::vector<hardware::camera2::CaptureRequest>& requests,
267 bool streaming,
268 /*out*/
269 hardware::camera2::utils::SubmitInfo *submitInfo) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700270 ATRACE_CALL();
Mark Salyzyn50468412014-06-18 16:33:43 -0700271 ALOGV("%s-start of function. Request list size %zu", __FUNCTION__, requests.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700272
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800273 binder::Status res = binder::Status::ok();
274 status_t err;
275 if ( !(res = checkPidStatus(__FUNCTION__) ).isOk()) {
276 return res;
277 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700278
279 Mutex::Autolock icl(mBinderSerializationLock);
280
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800281 if (!mDevice.get()) {
282 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
283 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700284
285 if (requests.empty()) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800286 ALOGE("%s: Camera %s: Sent null request. Rejecting request.",
287 __FUNCTION__, mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800288 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Empty request list");
Jianing Wei90e59c92014-03-12 18:29:36 -0700289 }
290
Emilian Peevaebbe412018-01-15 13:53:24 +0000291 List<const CameraDeviceBase::PhysicalCameraSettingsList> metadataRequestList;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700292 std::list<const SurfaceMap> surfaceMapList;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800293 submitInfo->mRequestId = mRequestIdCounter;
Jianing Wei90e59c92014-03-12 18:29:36 -0700294 uint32_t loopCounter = 0;
295
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800296 for (auto&& request: requests) {
297 if (request.mIsReprocess) {
Chien-Yu Chened0412e2015-04-27 15:04:22 -0700298 if (!mInputStream.configured) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800299 ALOGE("%s: Camera %s: no input stream is configured.", __FUNCTION__,
300 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800301 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800302 "No input configured for camera %s but request is for reprocessing",
303 mCameraIdStr.string());
Chien-Yu Chened0412e2015-04-27 15:04:22 -0700304 } else if (streaming) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800305 ALOGE("%s: Camera %s: streaming reprocess requests not supported.", __FUNCTION__,
306 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800307 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
308 "Repeating reprocess requests not supported");
Emilian Peevaebbe412018-01-15 13:53:24 +0000309 } else if (request.mPhysicalCameraSettings.size() > 1) {
310 ALOGE("%s: Camera %s: reprocess requests not supported for "
311 "multiple physical cameras.", __FUNCTION__,
312 mCameraIdStr.string());
313 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
314 "Reprocess requests not supported for multiple cameras");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700315 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700316 }
317
Emilian Peevaebbe412018-01-15 13:53:24 +0000318 if (request.mPhysicalCameraSettings.empty()) {
319 ALOGE("%s: Camera %s: request doesn't contain any settings.", __FUNCTION__,
320 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800321 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
Emilian Peevaebbe412018-01-15 13:53:24 +0000322 "Request doesn't contain any settings");
323 }
324
325 //The first capture settings should always match the logical camera id
326 String8 logicalId(request.mPhysicalCameraSettings.begin()->id.c_str());
327 if (mDevice->getId() != logicalId) {
328 ALOGE("%s: Camera %s: Invalid camera request settings.", __FUNCTION__,
329 mCameraIdStr.string());
330 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
331 "Invalid camera request settings");
332 }
333
Emilian Peevaebbe412018-01-15 13:53:24 +0000334 if (request.mSurfaceList.isEmpty() && request.mStreamIdxList.size() == 0) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800335 ALOGE("%s: Camera %s: Requests must have at least one surface target. "
336 "Rejecting request.", __FUNCTION__, mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800337 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
338 "Request has no output targets");
Jianing Wei90e59c92014-03-12 18:29:36 -0700339 }
340
Jianing Wei90e59c92014-03-12 18:29:36 -0700341 /**
Shuzhen Wang0129d522016-10-30 22:43:41 -0700342 * Write in the output stream IDs and map from stream ID to surface ID
343 * which we calculate from the capture request's list of surface target
Jianing Wei90e59c92014-03-12 18:29:36 -0700344 */
Shuzhen Wang0129d522016-10-30 22:43:41 -0700345 SurfaceMap surfaceMap;
Jianing Wei90e59c92014-03-12 18:29:36 -0700346 Vector<int32_t> outputStreamIds;
Emilian Peevf873aa52018-01-26 14:58:28 +0000347 std::vector<std::string> requestedPhysicalIds;
Emilian Peevc81a7592022-02-14 17:38:18 -0800348 int64_t dynamicProfileBitmap = 0;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800349 if (request.mSurfaceList.size() > 0) {
Chih-Hung Hsieh3ef324d2018-12-11 11:48:12 -0800350 for (const sp<Surface>& surface : request.mSurfaceList) {
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800351 if (surface == 0) continue;
Jianing Wei90e59c92014-03-12 18:29:36 -0700352
Emilian Peevf873aa52018-01-26 14:58:28 +0000353 int32_t streamId;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800354 sp<IGraphicBufferProducer> gbp = surface->getIGraphicBufferProducer();
Emilian Peevf873aa52018-01-26 14:58:28 +0000355 res = insertGbpLocked(gbp, &surfaceMap, &outputStreamIds, &streamId);
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800356 if (!res.isOk()) {
357 return res;
358 }
Emilian Peevf873aa52018-01-26 14:58:28 +0000359
360 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
361 if (index >= 0) {
362 String8 requestedPhysicalId(
363 mConfiguredOutputs.valueAt(index).getPhysicalCameraId());
364 requestedPhysicalIds.push_back(requestedPhysicalId.string());
Emilian Peev2295df72021-11-12 18:14:10 -0800365 dynamicProfileBitmap |=
366 mConfiguredOutputs.valueAt(index).getDynamicRangeProfile();
Emilian Peevf873aa52018-01-26 14:58:28 +0000367 } else {
368 ALOGW("%s: Output stream Id not found among configured outputs!", __FUNCTION__);
369 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700370 }
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800371 } else {
372 for (size_t i = 0; i < request.mStreamIdxList.size(); i++) {
373 int streamId = request.mStreamIdxList.itemAt(i);
374 int surfaceIdx = request.mSurfaceIdxList.itemAt(i);
Jianing Wei90e59c92014-03-12 18:29:36 -0700375
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800376 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
377 if (index < 0) {
378 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
379 " we have not called createStream on: stream %d",
380 __FUNCTION__, mCameraIdStr.string(), streamId);
381 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
382 "Request targets Surface that is not part of current capture session");
383 }
384
385 const auto& gbps = mConfiguredOutputs.valueAt(index).getGraphicBufferProducers();
386 if ((size_t)surfaceIdx >= gbps.size()) {
387 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
388 " we have not called createStream on: stream %d, surfaceIdx %d",
389 __FUNCTION__, mCameraIdStr.string(), streamId, surfaceIdx);
390 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
391 "Request targets Surface has invalid surface index");
392 }
393
Emilian Peevf873aa52018-01-26 14:58:28 +0000394 res = insertGbpLocked(gbps[surfaceIdx], &surfaceMap, &outputStreamIds, nullptr);
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800395 if (!res.isOk()) {
396 return res;
397 }
Emilian Peevf873aa52018-01-26 14:58:28 +0000398
399 String8 requestedPhysicalId(
400 mConfiguredOutputs.valueAt(index).getPhysicalCameraId());
401 requestedPhysicalIds.push_back(requestedPhysicalId.string());
Emilian Peev2295df72021-11-12 18:14:10 -0800402 dynamicProfileBitmap |=
403 mConfiguredOutputs.valueAt(index).getDynamicRangeProfile();
404 }
405 }
406
407 if (dynamicProfileBitmap !=
408 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD) {
409 for (int i = ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD;
410 i < ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_MAX; i <<= 1) {
411 if ((dynamicProfileBitmap & i) == 0) {
412 continue;
413 }
414
415 const auto& it = mDynamicProfileMap.find(i);
416 if (it != mDynamicProfileMap.end()) {
417 if ((it->second == 0) ||
418 ((it->second & dynamicProfileBitmap) == dynamicProfileBitmap)) {
419 continue;
420 } else {
421 ALOGE("%s: Camera %s: Tried to submit a request with a surfaces that"
422 " reference an unsupported dynamic range profile combination"
Emilian Peevc81a7592022-02-14 17:38:18 -0800423 " 0x%" PRIx64 "!", __FUNCTION__, mCameraIdStr.string(),
Emilian Peev2295df72021-11-12 18:14:10 -0800424 dynamicProfileBitmap);
425 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
426 "Request targets an unsupported dynamic range profile"
427 " combination");
428 }
429 } else {
430 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
431 " references unsupported dynamic range profile 0x%x!",
432 __FUNCTION__, mCameraIdStr.string(), i);
433 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
434 "Request targets 10-bit Surface with unsupported dynamic range"
435 " profile");
436 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700437 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700438 }
439
Emilian Peevf873aa52018-01-26 14:58:28 +0000440 CameraDeviceBase::PhysicalCameraSettingsList physicalSettingsList;
441 for (const auto& it : request.mPhysicalCameraSettings) {
Emilian Peev00420d22018-02-05 21:33:13 +0000442 if (it.settings.isEmpty()) {
443 ALOGE("%s: Camera %s: Sent empty metadata packet. Rejecting request.",
444 __FUNCTION__, mCameraIdStr.string());
445 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
446 "Request settings are empty");
447 }
448
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800449 // Check whether the physical / logical stream has settings
450 // consistent with the sensor pixel mode(s) it was configured with.
451 // mCameraIdToStreamSet will only have ids that are high resolution
452 const auto streamIdSetIt = mHighResolutionCameraIdToStreamIdSet.find(it.id);
453 if (streamIdSetIt != mHighResolutionCameraIdToStreamIdSet.end()) {
454 std::list<int> streamIdsUsedInRequest = getIntersection(streamIdSetIt->second,
455 outputStreamIds);
456 if (!request.mIsReprocess &&
457 !isSensorPixelModeConsistent(streamIdsUsedInRequest, it.settings)) {
458 ALOGE("%s: Camera %s: Request settings CONTROL_SENSOR_PIXEL_MODE not "
459 "consistent with configured streams. Rejecting request.",
460 __FUNCTION__, it.id.c_str());
461 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
462 "Request settings CONTROL_SENSOR_PIXEL_MODE are not consistent with "
463 "streams configured");
464 }
465 }
466
Emilian Peevf873aa52018-01-26 14:58:28 +0000467 String8 physicalId(it.id.c_str());
Shuzhen Wang911c6a32021-10-27 13:36:03 -0700468 bool hasTestPatternModePhysicalKey = std::find(mSupportedPhysicalRequestKeys.begin(),
469 mSupportedPhysicalRequestKeys.end(), ANDROID_SENSOR_TEST_PATTERN_MODE) !=
470 mSupportedPhysicalRequestKeys.end();
471 bool hasTestPatternDataPhysicalKey = std::find(mSupportedPhysicalRequestKeys.begin(),
472 mSupportedPhysicalRequestKeys.end(), ANDROID_SENSOR_TEST_PATTERN_DATA) !=
473 mSupportedPhysicalRequestKeys.end();
Emilian Peevf873aa52018-01-26 14:58:28 +0000474 if (physicalId != mDevice->getId()) {
475 auto found = std::find(requestedPhysicalIds.begin(), requestedPhysicalIds.end(),
476 it.id);
477 if (found == requestedPhysicalIds.end()) {
478 ALOGE("%s: Camera %s: Physical camera id: %s not part of attached outputs.",
479 __FUNCTION__, mCameraIdStr.string(), physicalId.string());
480 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
481 "Invalid physical camera id");
482 }
Emilian Peev00420d22018-02-05 21:33:13 +0000483
484 if (!mSupportedPhysicalRequestKeys.empty()) {
485 // Filter out any unsupported physical request keys.
486 CameraMetadata filteredParams(mSupportedPhysicalRequestKeys.size());
487 camera_metadata_t *meta = const_cast<camera_metadata_t *>(
488 filteredParams.getAndLock());
489 set_camera_metadata_vendor_id(meta, mDevice->getVendorTagId());
490 filteredParams.unlock(meta);
491
492 for (const auto& keyIt : mSupportedPhysicalRequestKeys) {
493 camera_metadata_ro_entry entry = it.settings.find(keyIt);
494 if (entry.count > 0) {
495 filteredParams.update(entry);
496 }
497 }
498
Shuzhen Wang911c6a32021-10-27 13:36:03 -0700499 physicalSettingsList.push_back({it.id, filteredParams,
500 hasTestPatternModePhysicalKey, hasTestPatternDataPhysicalKey});
Emilian Peev00420d22018-02-05 21:33:13 +0000501 }
502 } else {
503 physicalSettingsList.push_back({it.id, it.settings});
Emilian Peevf873aa52018-01-26 14:58:28 +0000504 }
Emilian Peevf873aa52018-01-26 14:58:28 +0000505 }
506
507 if (!enforceRequestPermissions(physicalSettingsList.begin()->metadata)) {
508 // Callee logs
509 return STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
510 "Caller does not have permission to change restricted controls");
511 }
512
Emilian Peevaebbe412018-01-15 13:53:24 +0000513 physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_OUTPUT_STREAMS,
514 &outputStreamIds[0], outputStreamIds.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700515
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800516 if (request.mIsReprocess) {
Emilian Peevaebbe412018-01-15 13:53:24 +0000517 physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_INPUT_STREAMS,
518 &mInputStream.id, 1);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700519 }
520
Emilian Peevaebbe412018-01-15 13:53:24 +0000521 physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_ID,
522 &(submitInfo->mRequestId), /*size*/1);
Jianing Wei90e59c92014-03-12 18:29:36 -0700523 loopCounter++; // loopCounter starts from 1
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800524 ALOGV("%s: Camera %s: Creating request with ID %d (%d of %zu)",
525 __FUNCTION__, mCameraIdStr.string(), submitInfo->mRequestId,
526 loopCounter, requests.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700527
Emilian Peevaebbe412018-01-15 13:53:24 +0000528 metadataRequestList.push_back(physicalSettingsList);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700529 surfaceMapList.push_back(surfaceMap);
Shuzhen Wangd26b1862022-03-07 12:05:05 -0800530
Shuzhen Wang9372b0b2022-05-11 18:55:19 -0700531 // Save certain CaptureRequest settings
Shuzhen Wangd26b1862022-03-07 12:05:05 -0800532 if (!request.mUserTag.empty()) {
533 mUserTag = request.mUserTag;
534 }
Shuzhen Wang9372b0b2022-05-11 18:55:19 -0700535 camera_metadata_entry entry =
536 physicalSettingsList.begin()->metadata.find(
537 ANDROID_CONTROL_VIDEO_STABILIZATION_MODE);
538 if (entry.count == 1) {
539 mVideoStabilizationMode = entry.data.u8[0];
540 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700541 }
542 mRequestIdCounter++;
543
544 if (streaming) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700545 err = mDevice->setStreamingRequestList(metadataRequestList, surfaceMapList,
546 &(submitInfo->mLastFrameNumber));
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800547 if (err != OK) {
548 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800549 "Camera %s: Got error %s (%d) after trying to set streaming request",
550 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800551 ALOGE("%s: %s", __FUNCTION__, msg.string());
552 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
553 msg.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700554 } else {
Shuzhen Wangc9ca6782016-04-26 13:40:31 -0700555 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700556 mStreamingRequestId = submitInfo->mRequestId;
Jianing Wei90e59c92014-03-12 18:29:36 -0700557 }
558 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700559 err = mDevice->captureList(metadataRequestList, surfaceMapList,
560 &(submitInfo->mLastFrameNumber));
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800561 if (err != OK) {
562 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800563 "Camera %s: Got error %s (%d) after trying to submit capture request",
564 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800565 ALOGE("%s: %s", __FUNCTION__, msg.string());
566 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
567 msg.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700568 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800569 ALOGV("%s: requestId = %d ", __FUNCTION__, submitInfo->mRequestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700570 }
571
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800572 ALOGV("%s: Camera %s: End of function", __FUNCTION__, mCameraIdStr.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700573 return res;
574}
575
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800576binder::Status CameraDeviceClient::cancelRequest(
577 int requestId,
578 /*out*/
579 int64_t* lastFrameNumber) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700580 ATRACE_CALL();
581 ALOGV("%s, requestId = %d", __FUNCTION__, requestId);
582
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800583 status_t err;
584 binder::Status res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700585
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800586 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700587
588 Mutex::Autolock icl(mBinderSerializationLock);
589
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800590 if (!mDevice.get()) {
591 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
592 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700593
Shuzhen Wangc9ca6782016-04-26 13:40:31 -0700594 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700595 if (mStreamingRequestId != requestId) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800596 String8 msg = String8::format("Camera %s: Canceling request ID %d doesn't match "
597 "current request ID %d", mCameraIdStr.string(), requestId, mStreamingRequestId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800598 ALOGE("%s: %s", __FUNCTION__, msg.string());
599 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700600 }
601
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800602 err = mDevice->clearStreamingRequest(lastFrameNumber);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700603
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800604 if (err == OK) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800605 ALOGV("%s: Camera %s: Successfully cleared streaming request",
606 __FUNCTION__, mCameraIdStr.string());
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700607 mStreamingRequestId = REQUEST_ID_NONE;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800608 } else {
609 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800610 "Camera %s: Error clearing streaming request: %s (%d)",
611 mCameraIdStr.string(), strerror(-err), err);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700612 }
613
614 return res;
615}
616
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800617binder::Status CameraDeviceClient::beginConfigure() {
Ruben Brunkb2119af2014-05-09 19:57:56 -0700618 // TODO: Implement this.
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -0700619 ATRACE_CALL();
Zhijun He1fa89992015-06-01 15:44:31 -0700620 ALOGV("%s: Not implemented yet.", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800621 return binder::Status::ok();
Ruben Brunkb2119af2014-05-09 19:57:56 -0700622}
623
Emilian Peev5fbe0ba2017-10-20 15:45:45 +0100624binder::Status CameraDeviceClient::endConfigure(int operatingMode,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700625 const hardware::camera2::impl::CameraMetadataNative& sessionParams, int64_t startTimeMs,
Emilian Peevcc0b7952020-01-07 13:54:47 -0800626 std::vector<int>* offlineStreamIds /*out*/) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -0700627 ATRACE_CALL();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700628 ALOGV("%s: ending configure (%d input stream, %zu output surfaces)",
629 __FUNCTION__, mInputStream.configured ? 1 : 0,
630 mStreamMap.size());
Igor Murashkine2d167e2014-08-19 16:19:59 -0700631
Zhijun He0effd522016-03-04 10:22:27 -0800632 binder::Status res;
633 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
634
Emilian Peevcc0b7952020-01-07 13:54:47 -0800635 if (offlineStreamIds == nullptr) {
636 String8 msg = String8::format("Invalid offline stream ids");
637 ALOGE("%s: %s", __FUNCTION__, msg.string());
638 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
639 }
640
Zhijun He0effd522016-03-04 10:22:27 -0800641 Mutex::Autolock icl(mBinderSerializationLock);
642
643 if (!mDevice.get()) {
644 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
645 }
646
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700647 res = SessionConfigurationUtils::checkOperatingMode(operatingMode, mDevice->info(),
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000648 mCameraIdStr);
Emilian Peev35ae8262018-11-08 13:11:32 +0000649 if (!res.isOk()) {
650 return res;
651 }
652
653 status_t err = mDevice->configureStreams(sessionParams, operatingMode);
654 if (err == BAD_VALUE) {
655 String8 msg = String8::format("Camera %s: Unsupported set of inputs/outputs provided",
656 mCameraIdStr.string());
657 ALOGE("%s: %s", __FUNCTION__, msg.string());
658 res = STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
659 } else if (err != OK) {
660 String8 msg = String8::format("Camera %s: Error configuring streams: %s (%d)",
661 mCameraIdStr.string(), strerror(-err), err);
662 ALOGE("%s: %s", __FUNCTION__, msg.string());
663 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Emilian Peev538c90e2018-12-17 18:03:19 +0000664 } else {
Emilian Peevcc0b7952020-01-07 13:54:47 -0800665 offlineStreamIds->clear();
666 mDevice->getOfflineStreamIds(offlineStreamIds);
667
Emilian Peev2f5d6012022-01-19 16:16:50 -0800668 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +0000669 for (size_t i = 0; i < mCompositeStreamMap.size(); ++i) {
670 err = mCompositeStreamMap.valueAt(i)->configureStream();
Emilian Peevcc0b7952020-01-07 13:54:47 -0800671 if (err != OK) {
Emilian Peev538c90e2018-12-17 18:03:19 +0000672 String8 msg = String8::format("Camera %s: Error configuring composite "
673 "streams: %s (%d)", mCameraIdStr.string(), strerror(-err), err);
674 ALOGE("%s: %s", __FUNCTION__, msg.string());
675 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
676 break;
677 }
Emilian Peevcc0b7952020-01-07 13:54:47 -0800678
679 // Composite streams can only support offline mode in case all individual internal
680 // streams are also supported.
681 std::vector<int> internalStreams;
682 mCompositeStreamMap.valueAt(i)->insertCompositeStreamIds(&internalStreams);
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800683 offlineStreamIds->erase(
684 std::remove_if(offlineStreamIds->begin(), offlineStreamIds->end(),
Emilian Peevcc0b7952020-01-07 13:54:47 -0800685 [&internalStreams] (int streamId) {
686 auto it = std::find(internalStreams.begin(), internalStreams.end(),
687 streamId);
688 if (it != internalStreams.end()) {
689 internalStreams.erase(it);
690 return true;
691 }
692
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800693 return false;}), offlineStreamIds->end());
Emilian Peevcc0b7952020-01-07 13:54:47 -0800694 if (internalStreams.empty()) {
695 offlineStreamIds->push_back(mCompositeStreamMap.valueAt(i)->getStreamId());
696 }
697 }
698
699 for (const auto& offlineStreamId : *offlineStreamIds) {
700 mStreamInfoMap[offlineStreamId].supportsOffline = true;
Emilian Peev538c90e2018-12-17 18:03:19 +0000701 }
Shuzhen Wang316781a2020-08-18 18:11:01 -0700702
703 nsecs_t configureEnd = systemTime();
704 int32_t configureDurationMs = ns2ms(configureEnd) - startTimeMs;
Austin Borger74fca042022-05-23 12:41:21 -0700705 mCameraServiceProxyWrapper->logStreamConfigured(mCameraIdStr, operatingMode,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700706 false /*internalReconfig*/, configureDurationMs);
Emilian Peev35ae8262018-11-08 13:11:32 +0000707 }
708
709 return res;
710}
711
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800712binder::Status CameraDeviceClient::isSessionConfigurationSupported(
713 const SessionConfiguration& sessionConfiguration, bool *status /*out*/) {
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800714
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800715 ATRACE_CALL();
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800716 binder::Status res;
717 status_t ret = OK;
718 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
719
720 Mutex::Autolock icl(mBinderSerializationLock);
721
722 if (!mDevice.get()) {
723 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
724 }
725
726 auto operatingMode = sessionConfiguration.getOperatingMode();
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700727 res = SessionConfigurationUtils::checkOperatingMode(operatingMode, mDevice->info(),
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000728 mCameraIdStr);
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800729 if (!res.isOk()) {
730 return res;
731 }
732
733 if (status == nullptr) {
734 String8 msg = String8::format( "Camera %s: Invalid status!", mCameraIdStr.string());
735 ALOGE("%s: %s", __FUNCTION__, msg.string());
736 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
737 }
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700738
Emilian Peev35ae8262018-11-08 13:11:32 +0000739 *status = false;
Shuzhen Wanga79a64d2022-04-24 19:56:30 -0700740 camera3::metadataGetter getMetadata = [this](const String8 &id, bool /*overrideForPerfClass*/) {
741 return mDevice->infoPhysical(id);};
Emilian Peev35ae8262018-11-08 13:11:32 +0000742 ret = mProviderManager->isSessionConfigurationSupported(mCameraIdStr.string(),
Shuzhen Wanga79a64d2022-04-24 19:56:30 -0700743 sessionConfiguration, mOverrideForPerfClass, getMetadata, status);
Emilian Peev35ae8262018-11-08 13:11:32 +0000744 switch (ret) {
745 case OK:
746 // Expected, do nothing.
747 break;
748 case INVALID_OPERATION: {
749 String8 msg = String8::format(
750 "Camera %s: Session configuration query not supported!",
751 mCameraIdStr.string());
752 ALOGD("%s: %s", __FUNCTION__, msg.string());
753 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
754 }
755
756 break;
757 default: {
758 String8 msg = String8::format( "Camera %s: Error: %s (%d)", mCameraIdStr.string(),
759 strerror(-ret), ret);
760 ALOGE("%s: %s", __FUNCTION__, msg.string());
761 res = STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
762 msg.string());
763 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800764 }
765
766 return res;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700767}
768
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800769binder::Status CameraDeviceClient::deleteStream(int streamId) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700770 ATRACE_CALL();
771 ALOGV("%s (streamId = 0x%x)", __FUNCTION__, streamId);
772
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800773 binder::Status res;
774 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700775
776 Mutex::Autolock icl(mBinderSerializationLock);
777
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800778 if (!mDevice.get()) {
779 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
780 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700781
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700782 bool isInput = false;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700783 std::vector<sp<IBinder>> surfaces;
Zhijun He5d677d12016-05-29 16:52:39 -0700784 ssize_t dIndex = NAME_NOT_FOUND;
Emilian Peev538c90e2018-12-17 18:03:19 +0000785 ssize_t compositeIndex = NAME_NOT_FOUND;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700786
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700787 if (mInputStream.configured && mInputStream.id == streamId) {
788 isInput = true;
789 } else {
790 // Guard against trying to delete non-created streams
791 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700792 if (streamId == mStreamMap.valueAt(i).streamId()) {
793 surfaces.push_back(mStreamMap.keyAt(i));
794 }
795 }
796
797 // See if this stream is one of the deferred streams.
798 for (size_t i = 0; i < mDeferredStreams.size(); ++i) {
799 if (streamId == mDeferredStreams[i]) {
800 dIndex = i;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700801 break;
802 }
803 }
804
Emilian Peev2f5d6012022-01-19 16:16:50 -0800805 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +0000806 for (size_t i = 0; i < mCompositeStreamMap.size(); ++i) {
807 if (streamId == mCompositeStreamMap.valueAt(i)->getStreamId()) {
808 compositeIndex = i;
809 break;
810 }
811 }
812
Shuzhen Wang0129d522016-10-30 22:43:41 -0700813 if (surfaces.empty() && dIndex == NAME_NOT_FOUND) {
814 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no such"
815 " stream created yet", mCameraIdStr.string(), streamId);
816 ALOGW("%s: %s", __FUNCTION__, msg.string());
817 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700818 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700819 }
820
821 // Also returns BAD_VALUE if stream ID was not valid
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800822 status_t err = mDevice->deleteStream(streamId);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700823
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800824 if (err != OK) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800825 String8 msg = String8::format("Camera %s: Unexpected error %s (%d) when deleting stream %d",
826 mCameraIdStr.string(), strerror(-err), err, streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800827 ALOGE("%s: %s", __FUNCTION__, msg.string());
828 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
829 } else {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700830 if (isInput) {
831 mInputStream.configured = false;
Zhijun He5d677d12016-05-29 16:52:39 -0700832 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700833 for (auto& surface : surfaces) {
834 mStreamMap.removeItem(surface);
835 }
836
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800837 mConfiguredOutputs.removeItem(streamId);
838
Shuzhen Wang0129d522016-10-30 22:43:41 -0700839 if (dIndex != NAME_NOT_FOUND) {
840 mDeferredStreams.removeItemsAt(dIndex);
841 }
Emilian Peev538c90e2018-12-17 18:03:19 +0000842
843 if (compositeIndex != NAME_NOT_FOUND) {
Emilian Peev2f5d6012022-01-19 16:16:50 -0800844 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +0000845 status_t ret;
846 if ((ret = mCompositeStreamMap.valueAt(compositeIndex)->deleteStream())
847 != OK) {
848 String8 msg = String8::format("Camera %s: Unexpected error %s (%d) when "
849 "deleting composite stream %d", mCameraIdStr.string(), strerror(-err), err,
850 streamId);
851 ALOGE("%s: %s", __FUNCTION__, msg.string());
852 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
853 }
854 mCompositeStreamMap.removeItemsAt(compositeIndex);
855 }
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800856 for (auto &mapIt: mHighResolutionCameraIdToStreamIdSet) {
857 auto &streamSet = mapIt.second;
858 if (streamSet.find(streamId) != streamSet.end()) {
859 streamSet.erase(streamId);
860 break;
861 }
862 }
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700863 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700864 }
865
866 return res;
867}
868
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800869binder::Status CameraDeviceClient::createStream(
870 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
871 /*out*/
872 int32_t* newStreamId) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700873 ATRACE_CALL();
Igor Murashkine7ee7632013-06-11 18:10:18 -0700874
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800875 binder::Status res;
876 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700877
878 Mutex::Autolock icl(mBinderSerializationLock);
879
Shuzhen Wang0129d522016-10-30 22:43:41 -0700880 const std::vector<sp<IGraphicBufferProducer>>& bufferProducers =
881 outputConfiguration.getGraphicBufferProducers();
882 size_t numBufferProducers = bufferProducers.size();
Shuzhen Wang758c2152017-01-10 18:26:18 -0800883 bool deferredConsumer = outputConfiguration.isDeferred();
884 bool isShared = outputConfiguration.isShared();
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800885 String8 physicalCameraId = String8(outputConfiguration.getPhysicalCameraId());
Shuzhen Wang758c2152017-01-10 18:26:18 -0800886 bool deferredConsumerOnly = deferredConsumer && numBufferProducers == 0;
Shuzhen Wang83bff122020-11-20 15:51:39 -0800887 bool isMultiResolution = outputConfiguration.isMultiResolution();
Emilian Peevc81a7592022-02-14 17:38:18 -0800888 int64_t dynamicRangeProfile = outputConfiguration.getDynamicRangeProfile();
Shuzhen Wang8ed1e872022-03-08 16:34:33 -0800889 int64_t streamUseCase = outputConfiguration.getStreamUseCase();
Shuzhen Wange4208922022-02-01 16:52:48 -0800890 int timestampBase = outputConfiguration.getTimestampBase();
Shuzhen Wang610d7b82022-02-08 14:37:22 -0800891 int mirrorMode = outputConfiguration.getMirrorMode();
Austin Borger9e2b27c2022-07-15 11:27:24 -0700892 int32_t colorSpace = outputConfiguration.getColorSpace();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700893
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700894 res = SessionConfigurationUtils::checkSurfaceType(numBufferProducers, deferredConsumer,
Emilian Peev35ae8262018-11-08 13:11:32 +0000895 outputConfiguration.getSurfaceType());
896 if (!res.isOk()) {
897 return res;
Yin-Chia Yeh89f14da2014-06-10 16:05:44 -0700898 }
Zhijun He5d677d12016-05-29 16:52:39 -0700899
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800900 if (!mDevice.get()) {
901 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
902 }
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700903 res = SessionConfigurationUtils::checkPhysicalCameraId(mPhysicalCameraIds,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800904 physicalCameraId, mCameraIdStr);
Emilian Peev35ae8262018-11-08 13:11:32 +0000905 if (!res.isOk()) {
906 return res;
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800907 }
Emilian Peev35ae8262018-11-08 13:11:32 +0000908
Shuzhen Wang0129d522016-10-30 22:43:41 -0700909 std::vector<sp<Surface>> surfaces;
910 std::vector<sp<IBinder>> binders;
Zhijun He5d677d12016-05-29 16:52:39 -0700911 status_t err;
912
913 // Create stream for deferred surface case.
Shuzhen Wang0129d522016-10-30 22:43:41 -0700914 if (deferredConsumerOnly) {
Shuzhen Wang758c2152017-01-10 18:26:18 -0800915 return createDeferredSurfaceStreamLocked(outputConfiguration, isShared, newStreamId);
Zhijun He5d677d12016-05-29 16:52:39 -0700916 }
917
Shuzhen Wang758c2152017-01-10 18:26:18 -0800918 OutputStreamInfo streamInfo;
919 bool isStreamInfoValid = false;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800920 const std::vector<int32_t> &sensorPixelModesUsed =
921 outputConfiguration.getSensorPixelModesUsed();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700922 for (auto& bufferProducer : bufferProducers) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700923 // Don't create multiple streams for the same target surface
Shuzhen Wang0129d522016-10-30 22:43:41 -0700924 sp<IBinder> binder = IInterface::asBinder(bufferProducer);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800925 ssize_t index = mStreamMap.indexOfKey(binder);
926 if (index != NAME_NOT_FOUND) {
927 String8 msg = String8::format("Camera %s: Surface already has a stream created for it "
928 "(ID %zd)", mCameraIdStr.string(), index);
929 ALOGW("%s: %s", __FUNCTION__, msg.string());
930 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
Shuzhen Wang0129d522016-10-30 22:43:41 -0700931 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700932
Shuzhen Wang758c2152017-01-10 18:26:18 -0800933 sp<Surface> surface;
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700934 res = SessionConfigurationUtils::createSurfaceFromGbp(streamInfo,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800935 isStreamInfoValid, surface, bufferProducer, mCameraIdStr,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -0800936 mDevice->infoPhysical(physicalCameraId), sensorPixelModesUsed, dynamicRangeProfile,
Austin Borger9e2b27c2022-07-15 11:27:24 -0700937 streamUseCase, timestampBase, mirrorMode, colorSpace);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800938
939 if (!res.isOk())
940 return res;
941
942 if (!isStreamInfoValid) {
943 isStreamInfoValid = true;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700944 }
945
Shuzhen Wang758c2152017-01-10 18:26:18 -0800946 binders.push_back(IInterface::asBinder(bufferProducer));
Shuzhen Wang0129d522016-10-30 22:43:41 -0700947 surfaces.push_back(surface);
Ruben Brunkbba75572014-11-20 17:29:50 -0800948 }
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700949
950 // If mOverrideForPerfClass is true, do not fail createStream() for small
951 // JPEG sizes because existing createSurfaceFromGbp() logic will find the
952 // closest possible supported size.
953
Zhijun He125684a2015-12-26 15:07:30 -0800954 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
Emilian Peev40ead602017-09-26 15:46:36 +0100955 std::vector<int> surfaceIds;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800956 bool isDepthCompositeStream =
957 camera3::DepthCompositeStream::isDepthCompositeStream(surfaces[0]);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800958 bool isHeicCompisiteStream = camera3::HeicCompositeStream::isHeicCompositeStream(surfaces[0]);
Emilian Peev434248e2022-10-06 14:58:54 -0700959 bool isJpegRCompositeStream =
960 camera3::JpegRCompositeStream::isJpegRCompositeStream(surfaces[0]);
961 if (isDepthCompositeStream || isHeicCompisiteStream || isJpegRCompositeStream) {
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800962 sp<CompositeStream> compositeStream;
963 if (isDepthCompositeStream) {
964 compositeStream = new camera3::DepthCompositeStream(mDevice, getRemoteCallback());
Emilian Peev434248e2022-10-06 14:58:54 -0700965 } else if (isHeicCompisiteStream) {
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800966 compositeStream = new camera3::HeicCompositeStream(mDevice, getRemoteCallback());
Emilian Peev434248e2022-10-06 14:58:54 -0700967 } else {
968 compositeStream = new camera3::JpegRCompositeStream(mDevice, getRemoteCallback());
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800969 }
970
Emilian Peev538c90e2018-12-17 18:03:19 +0000971 err = compositeStream->createStream(surfaces, deferredConsumer, streamInfo.width,
972 streamInfo.height, streamInfo.format,
Emilian Peevf4816702020-04-03 15:44:51 -0700973 static_cast<camera_stream_rotation_t>(outputConfiguration.getRotation()),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800974 &streamId, physicalCameraId, streamInfo.sensorPixelModesUsed, &surfaceIds,
Emilian Peev434248e2022-10-06 14:58:54 -0700975 outputConfiguration.getSurfaceSetID(), isShared, isMultiResolution,
976 streamInfo.colorSpace, streamInfo.dynamicRangeProfile, streamInfo.streamUseCase);
Emilian Peev538c90e2018-12-17 18:03:19 +0000977 if (err == OK) {
Emilian Peev2f5d6012022-01-19 16:16:50 -0800978 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +0000979 mCompositeStreamMap.add(IInterface::asBinder(surfaces[0]->getIGraphicBufferProducer()),
980 compositeStream);
981 }
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800982 } else {
983 err = mDevice->createStream(surfaces, deferredConsumer, streamInfo.width,
984 streamInfo.height, streamInfo.format, streamInfo.dataSpace,
Emilian Peevf4816702020-04-03 15:44:51 -0700985 static_cast<camera_stream_rotation_t>(outputConfiguration.getRotation()),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800986 &streamId, physicalCameraId, streamInfo.sensorPixelModesUsed, &surfaceIds,
Emilian Peev2295df72021-11-12 18:14:10 -0800987 outputConfiguration.getSurfaceSetID(), isShared, isMultiResolution,
Shuzhen Wange4208922022-02-01 16:52:48 -0800988 /*consumerUsage*/0, streamInfo.dynamicRangeProfile, streamInfo.streamUseCase,
Austin Borger9e2b27c2022-07-15 11:27:24 -0700989 streamInfo.timestampBase, streamInfo.mirrorMode, streamInfo.colorSpace);
Emilian Peev538c90e2018-12-17 18:03:19 +0000990 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700991
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800992 if (err != OK) {
993 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800994 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
Shuzhen Wang758c2152017-01-10 18:26:18 -0800995 mCameraIdStr.string(), streamInfo.width, streamInfo.height, streamInfo.format,
996 streamInfo.dataSpace, strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800997 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700998 int i = 0;
999 for (auto& binder : binders) {
1000 ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %d",
1001 __FUNCTION__, binder.get(), streamId, i);
Emilian Peev40ead602017-09-26 15:46:36 +01001002 mStreamMap.add(binder, StreamSurfaceId(streamId, surfaceIds[i]));
1003 i++;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001004 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001005
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -08001006 mConfiguredOutputs.add(streamId, outputConfiguration);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001007 mStreamInfoMap[streamId] = streamInfo;
1008
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001009 ALOGV("%s: Camera %s: Successfully created a new stream ID %d for output surface"
Shuzhen Wang0129d522016-10-30 22:43:41 -07001010 " (%d x %d) with format 0x%x.",
Shuzhen Wang758c2152017-01-10 18:26:18 -08001011 __FUNCTION__, mCameraIdStr.string(), streamId, streamInfo.width,
1012 streamInfo.height, streamInfo.format);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001013
Zhijun He5d677d12016-05-29 16:52:39 -07001014 // Set transform flags to ensure preview to be rotated correctly.
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001015 res = setStreamTransformLocked(streamId, streamInfo.mirrorMode);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001016
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001017 // Fill in mHighResolutionCameraIdToStreamIdSet map
1018 const String8 &cameraIdUsed =
1019 physicalCameraId.size() != 0 ? physicalCameraId : mCameraIdStr;
1020 const char *cameraIdUsedCStr = cameraIdUsed.string();
1021 // Only needed for high resolution sensors
1022 if (mHighResolutionSensors.find(cameraIdUsedCStr) !=
1023 mHighResolutionSensors.end()) {
1024 mHighResolutionCameraIdToStreamIdSet[cameraIdUsedCStr].insert(streamId);
1025 }
1026
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001027 *newStreamId = streamId;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001028 }
1029
1030 return res;
1031}
1032
Zhijun He5d677d12016-05-29 16:52:39 -07001033binder::Status CameraDeviceClient::createDeferredSurfaceStreamLocked(
1034 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
Shuzhen Wang758c2152017-01-10 18:26:18 -08001035 bool isShared,
Zhijun He5d677d12016-05-29 16:52:39 -07001036 /*out*/
1037 int* newStreamId) {
1038 int width, height, format, surfaceType;
Emilian Peev050f5dc2017-05-18 14:43:56 +01001039 uint64_t consumerUsage;
Zhijun He5d677d12016-05-29 16:52:39 -07001040 android_dataspace dataSpace;
Austin Borger9e2b27c2022-07-15 11:27:24 -07001041 int32_t colorSpace;
Zhijun He5d677d12016-05-29 16:52:39 -07001042 status_t err;
1043 binder::Status res;
1044
1045 if (!mDevice.get()) {
1046 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1047 }
1048
1049 // Infer the surface info for deferred surface stream creation.
1050 width = outputConfiguration.getWidth();
1051 height = outputConfiguration.getHeight();
1052 surfaceType = outputConfiguration.getSurfaceType();
1053 format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
1054 dataSpace = android_dataspace_t::HAL_DATASPACE_UNKNOWN;
Austin Borger9e2b27c2022-07-15 11:27:24 -07001055 colorSpace = ANDROID_REQUEST_AVAILABLE_COLOR_SPACE_PROFILES_MAP_UNSPECIFIED;
Zhijun He5d677d12016-05-29 16:52:39 -07001056 // Hardcode consumer usage flags: SurfaceView--0x900, SurfaceTexture--0x100.
1057 consumerUsage = GraphicBuffer::USAGE_HW_TEXTURE;
1058 if (surfaceType == OutputConfiguration::SURFACE_TYPE_SURFACE_VIEW) {
1059 consumerUsage |= GraphicBuffer::USAGE_HW_COMPOSER;
1060 }
1061 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001062 std::vector<sp<Surface>> noSurface;
Emilian Peev40ead602017-09-26 15:46:36 +01001063 std::vector<int> surfaceIds;
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001064 String8 physicalCameraId(outputConfiguration.getPhysicalCameraId());
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001065 const String8 &cameraIdUsed =
1066 physicalCameraId.size() != 0 ? physicalCameraId : mCameraIdStr;
1067 // Here, we override sensor pixel modes
1068 std::unordered_set<int32_t> overriddenSensorPixelModesUsed;
1069 const std::vector<int32_t> &sensorPixelModesUsed =
1070 outputConfiguration.getSensorPixelModesUsed();
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07001071 if (SessionConfigurationUtils::checkAndOverrideSensorPixelModesUsed(
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001072 sensorPixelModesUsed, format, width, height, getStaticInfo(cameraIdUsed),
1073 /*allowRounding*/ false, &overriddenSensorPixelModesUsed) != OK) {
1074 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1075 "sensor pixel modes used not valid for deferred stream");
1076 }
1077
Shuzhen Wang0129d522016-10-30 22:43:41 -07001078 err = mDevice->createStream(noSurface, /*hasDeferredConsumer*/true, width,
1079 height, format, dataSpace,
Emilian Peevf4816702020-04-03 15:44:51 -07001080 static_cast<camera_stream_rotation_t>(outputConfiguration.getRotation()),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001081 &streamId, physicalCameraId,
1082 overriddenSensorPixelModesUsed,
1083 &surfaceIds,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001084 outputConfiguration.getSurfaceSetID(), isShared,
Emilian Peev2295df72021-11-12 18:14:10 -08001085 outputConfiguration.isMultiResolution(), consumerUsage,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001086 outputConfiguration.getDynamicRangeProfile(),
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001087 outputConfiguration.getStreamUseCase(),
1088 outputConfiguration.getMirrorMode());
Zhijun He5d677d12016-05-29 16:52:39 -07001089
1090 if (err != OK) {
1091 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001092 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
1093 mCameraIdStr.string(), width, height, format, dataSpace, strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001094 } else {
1095 // Can not add streamId to mStreamMap here, as the surface is deferred. Add it to
1096 // a separate list to track. Once the deferred surface is set, this id will be
1097 // relocated to mStreamMap.
1098 mDeferredStreams.push_back(streamId);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001099 mStreamInfoMap.emplace(std::piecewise_construct, std::forward_as_tuple(streamId),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001100 std::forward_as_tuple(width, height, format, dataSpace, consumerUsage,
Emilian Peev2295df72021-11-12 18:14:10 -08001101 overriddenSensorPixelModesUsed,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001102 outputConfiguration.getDynamicRangeProfile(),
Shuzhen Wange4208922022-02-01 16:52:48 -08001103 outputConfiguration.getStreamUseCase(),
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001104 outputConfiguration.getTimestampBase(),
Austin Borger9e2b27c2022-07-15 11:27:24 -07001105 outputConfiguration.getMirrorMode(),
1106 colorSpace));
Shuzhen Wang758c2152017-01-10 18:26:18 -08001107
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001108 ALOGV("%s: Camera %s: Successfully created a new stream ID %d for a deferred surface"
Zhijun He5d677d12016-05-29 16:52:39 -07001109 " (%d x %d) stream with format 0x%x.",
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001110 __FUNCTION__, mCameraIdStr.string(), streamId, width, height, format);
Zhijun He5d677d12016-05-29 16:52:39 -07001111
1112 // Set transform flags to ensure preview to be rotated correctly.
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001113 res = setStreamTransformLocked(streamId, outputConfiguration.getMirrorMode());
Zhijun He5d677d12016-05-29 16:52:39 -07001114
1115 *newStreamId = streamId;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001116 // Fill in mHighResolutionCameraIdToStreamIdSet
1117 const char *cameraIdUsedCStr = cameraIdUsed.string();
1118 // Only needed for high resolution sensors
1119 if (mHighResolutionSensors.find(cameraIdUsedCStr) !=
1120 mHighResolutionSensors.end()) {
1121 mHighResolutionCameraIdToStreamIdSet[cameraIdUsed.string()].insert(streamId);
1122 }
Zhijun He5d677d12016-05-29 16:52:39 -07001123 }
1124 return res;
1125}
1126
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001127binder::Status CameraDeviceClient::setStreamTransformLocked(int streamId, int mirrorMode) {
Zhijun He5d677d12016-05-29 16:52:39 -07001128 int32_t transform = 0;
1129 status_t err;
1130 binder::Status res;
1131
1132 if (!mDevice.get()) {
1133 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1134 }
1135
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001136 err = getRotationTransformLocked(mirrorMode, &transform);
Zhijun He5d677d12016-05-29 16:52:39 -07001137 if (err != OK) {
1138 // Error logged by getRotationTransformLocked.
1139 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
1140 "Unable to calculate rotation transform for new stream");
1141 }
1142
1143 err = mDevice->setStreamTransform(streamId, transform);
1144 if (err != OK) {
1145 String8 msg = String8::format("Failed to set stream transform (stream id %d)",
1146 streamId);
1147 ALOGE("%s: %s", __FUNCTION__, msg.string());
1148 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
1149 }
1150
1151 return res;
1152}
Ruben Brunkbba75572014-11-20 17:29:50 -08001153
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001154binder::Status CameraDeviceClient::createInputStream(
Shuzhen Wang83bff122020-11-20 15:51:39 -08001155 int width, int height, int format, bool isMultiResolution,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001156 /*out*/
1157 int32_t* newStreamId) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001158
1159 ATRACE_CALL();
Shuzhen Wang83bff122020-11-20 15:51:39 -08001160 ALOGV("%s (w = %d, h = %d, f = 0x%x, isMultiResolution %d)", __FUNCTION__,
1161 width, height, format, isMultiResolution);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001162
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001163 binder::Status res;
1164 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001165
1166 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001167
1168 if (!mDevice.get()) {
1169 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1170 }
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001171
1172 if (mInputStream.configured) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001173 String8 msg = String8::format("Camera %s: Already has an input stream "
Yi Kong0f414de2017-12-15 13:48:50 -08001174 "configured (ID %d)", mCameraIdStr.string(), mInputStream.id);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001175 ALOGE("%s: %s", __FUNCTION__, msg.string() );
1176 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001177 }
1178
1179 int streamId = -1;
Shuzhen Wang83bff122020-11-20 15:51:39 -08001180 status_t err = mDevice->createInputStream(width, height, format, isMultiResolution, &streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001181 if (err == OK) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001182 mInputStream.configured = true;
1183 mInputStream.width = width;
1184 mInputStream.height = height;
1185 mInputStream.format = format;
1186 mInputStream.id = streamId;
1187
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001188 ALOGV("%s: Camera %s: Successfully created a new input stream ID %d",
1189 __FUNCTION__, mCameraIdStr.string(), streamId);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001190
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001191 *newStreamId = streamId;
1192 } else {
1193 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001194 "Camera %s: Error creating new input stream: %s (%d)", mCameraIdStr.string(),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001195 strerror(-err), err);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001196 }
1197
1198 return res;
1199}
1200
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001201binder::Status CameraDeviceClient::getInputSurface(/*out*/ view::Surface *inputSurface) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001202
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001203 binder::Status res;
1204 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1205
1206 if (inputSurface == NULL) {
1207 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Null input surface");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001208 }
1209
1210 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001211 if (!mDevice.get()) {
1212 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1213 }
1214 sp<IGraphicBufferProducer> producer;
1215 status_t err = mDevice->getInputBufferProducer(&producer);
1216 if (err != OK) {
1217 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001218 "Camera %s: Error getting input Surface: %s (%d)",
1219 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001220 } else {
1221 inputSurface->name = String16("CameraInput");
1222 inputSurface->graphicBufferProducer = producer;
1223 }
1224 return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001225}
1226
Emilian Peev40ead602017-09-26 15:46:36 +01001227binder::Status CameraDeviceClient::updateOutputConfiguration(int streamId,
1228 const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
1229 ATRACE_CALL();
1230
1231 binder::Status res;
1232 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1233
1234 Mutex::Autolock icl(mBinderSerializationLock);
1235
1236 if (!mDevice.get()) {
1237 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1238 }
1239
1240 const std::vector<sp<IGraphicBufferProducer> >& bufferProducers =
1241 outputConfiguration.getGraphicBufferProducers();
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -07001242 String8 physicalCameraId(outputConfiguration.getPhysicalCameraId());
1243
Emilian Peev40ead602017-09-26 15:46:36 +01001244 auto producerCount = bufferProducers.size();
1245 if (producerCount == 0) {
1246 ALOGE("%s: bufferProducers must not be empty", __FUNCTION__);
1247 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1248 "bufferProducers must not be empty");
1249 }
1250
1251 // The first output is the one associated with the output configuration.
1252 // It should always be present, valid and the corresponding stream id should match.
1253 sp<IBinder> binder = IInterface::asBinder(bufferProducers[0]);
1254 ssize_t index = mStreamMap.indexOfKey(binder);
1255 if (index == NAME_NOT_FOUND) {
1256 ALOGE("%s: Outputconfiguration is invalid", __FUNCTION__);
1257 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1258 "OutputConfiguration is invalid");
1259 }
1260 if (mStreamMap.valueFor(binder).streamId() != streamId) {
1261 ALOGE("%s: Stream Id: %d provided doesn't match the id: %d in the stream map",
1262 __FUNCTION__, streamId, mStreamMap.valueFor(binder).streamId());
1263 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1264 "Stream id is invalid");
1265 }
1266
1267 std::vector<size_t> removedSurfaceIds;
1268 std::vector<sp<IBinder>> removedOutputs;
1269 std::vector<sp<Surface>> newOutputs;
1270 std::vector<OutputStreamInfo> streamInfos;
1271 KeyedVector<sp<IBinder>, sp<IGraphicBufferProducer>> newOutputsMap;
1272 for (auto &it : bufferProducers) {
1273 newOutputsMap.add(IInterface::asBinder(it), it);
1274 }
1275
1276 for (size_t i = 0; i < mStreamMap.size(); i++) {
1277 ssize_t idx = newOutputsMap.indexOfKey(mStreamMap.keyAt(i));
1278 if (idx == NAME_NOT_FOUND) {
1279 if (mStreamMap[i].streamId() == streamId) {
1280 removedSurfaceIds.push_back(mStreamMap[i].surfaceId());
1281 removedOutputs.push_back(mStreamMap.keyAt(i));
1282 }
1283 } else {
1284 if (mStreamMap[i].streamId() != streamId) {
1285 ALOGE("%s: Output surface already part of a different stream", __FUNCTION__);
1286 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1287 "Target Surface is invalid");
1288 }
1289 newOutputsMap.removeItemsAt(idx);
1290 }
1291 }
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001292 const std::vector<int32_t> &sensorPixelModesUsed =
1293 outputConfiguration.getSensorPixelModesUsed();
Shuzhen Wang8ed1e872022-03-08 16:34:33 -08001294 int64_t streamUseCase = outputConfiguration.getStreamUseCase();
Shuzhen Wange4208922022-02-01 16:52:48 -08001295 int timestampBase = outputConfiguration.getTimestampBase();
Emilian Peevc81a7592022-02-14 17:38:18 -08001296 int64_t dynamicRangeProfile = outputConfiguration.getDynamicRangeProfile();
Austin Borger9e2b27c2022-07-15 11:27:24 -07001297 int32_t colorSpace = outputConfiguration.getColorSpace();
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001298 int mirrorMode = outputConfiguration.getMirrorMode();
Emilian Peev2295df72021-11-12 18:14:10 -08001299
Emilian Peev40ead602017-09-26 15:46:36 +01001300 for (size_t i = 0; i < newOutputsMap.size(); i++) {
1301 OutputStreamInfo outInfo;
1302 sp<Surface> surface;
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07001303 res = SessionConfigurationUtils::createSurfaceFromGbp(outInfo,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001304 /*isStreamInfoValid*/ false, surface, newOutputsMap.valueAt(i), mCameraIdStr,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001305 mDevice->infoPhysical(physicalCameraId), sensorPixelModesUsed, dynamicRangeProfile,
Austin Borger9e2b27c2022-07-15 11:27:24 -07001306 streamUseCase, timestampBase, mirrorMode, colorSpace);
Emilian Peev40ead602017-09-26 15:46:36 +01001307 if (!res.isOk())
1308 return res;
1309
Emilian Peev40ead602017-09-26 15:46:36 +01001310 streamInfos.push_back(outInfo);
1311 newOutputs.push_back(surface);
1312 }
1313
1314 //Trivial case no changes required
1315 if (removedSurfaceIds.empty() && newOutputs.empty()) {
1316 return binder::Status::ok();
1317 }
1318
1319 KeyedVector<sp<Surface>, size_t> outputMap;
1320 auto ret = mDevice->updateStream(streamId, newOutputs, streamInfos, removedSurfaceIds,
1321 &outputMap);
1322 if (ret != OK) {
1323 switch (ret) {
1324 case NAME_NOT_FOUND:
1325 case BAD_VALUE:
1326 case -EBUSY:
1327 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1328 "Camera %s: Error updating stream: %s (%d)",
1329 mCameraIdStr.string(), strerror(ret), ret);
1330 break;
1331 default:
1332 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1333 "Camera %s: Error updating stream: %s (%d)",
1334 mCameraIdStr.string(), strerror(ret), ret);
1335 break;
1336 }
1337 } else {
1338 for (const auto &it : removedOutputs) {
1339 mStreamMap.removeItem(it);
1340 }
1341
1342 for (size_t i = 0; i < outputMap.size(); i++) {
1343 mStreamMap.add(IInterface::asBinder(outputMap.keyAt(i)->getIGraphicBufferProducer()),
1344 StreamSurfaceId(streamId, outputMap.valueAt(i)));
1345 }
1346
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -08001347 mConfiguredOutputs.replaceValueFor(streamId, outputConfiguration);
1348
Emilian Peev40ead602017-09-26 15:46:36 +01001349 ALOGV("%s: Camera %s: Successful stream ID %d update",
1350 __FUNCTION__, mCameraIdStr.string(), streamId);
1351 }
1352
1353 return res;
1354}
1355
Igor Murashkine7ee7632013-06-11 18:10:18 -07001356// Create a request object from a template.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001357binder::Status CameraDeviceClient::createDefaultRequest(int templateId,
1358 /*out*/
1359 hardware::camera2::impl::CameraMetadataNative* request)
Igor Murashkine7ee7632013-06-11 18:10:18 -07001360{
1361 ATRACE_CALL();
1362 ALOGV("%s (templateId = 0x%x)", __FUNCTION__, templateId);
1363
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001364 binder::Status res;
1365 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001366
1367 Mutex::Autolock icl(mBinderSerializationLock);
1368
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001369 if (!mDevice.get()) {
1370 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1371 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001372
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001373 status_t err;
Emilian Peevf4816702020-04-03 15:44:51 -07001374 camera_request_template_t tempId = camera_request_template_t::CAMERA_TEMPLATE_COUNT;
1375 if (!(res = mapRequestTemplate(templateId, &tempId)).isOk()) return res;
1376
1377 CameraMetadata metadata;
1378 if ( (err = mDevice->createDefaultRequest(tempId, &metadata) ) == OK &&
Igor Murashkine7ee7632013-06-11 18:10:18 -07001379 request != NULL) {
1380
1381 request->swap(metadata);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001382 } else if (err == BAD_VALUE) {
1383 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001384 "Camera %s: Template ID %d is invalid or not supported: %s (%d)",
1385 mCameraIdStr.string(), templateId, strerror(-err), err);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001386
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001387 } else {
1388 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001389 "Camera %s: Error creating default request for template %d: %s (%d)",
1390 mCameraIdStr.string(), templateId, strerror(-err), err);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001391 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001392 return res;
1393}
1394
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001395binder::Status CameraDeviceClient::getCameraInfo(
1396 /*out*/
1397 hardware::camera2::impl::CameraMetadataNative* info)
Igor Murashkine7ee7632013-06-11 18:10:18 -07001398{
1399 ATRACE_CALL();
1400 ALOGV("%s", __FUNCTION__);
1401
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001402 binder::Status res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001403
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001404 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001405
1406 Mutex::Autolock icl(mBinderSerializationLock);
1407
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001408 if (!mDevice.get()) {
1409 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1410 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001411
Igor Murashkin099b4572013-07-12 17:52:16 -07001412 if (info != NULL) {
1413 *info = mDevice->info(); // static camera metadata
1414 // TODO: merge with device-specific camera metadata
1415 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001416
1417 return res;
1418}
1419
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001420binder::Status CameraDeviceClient::waitUntilIdle()
Zhijun He2ab500c2013-07-23 08:02:53 -07001421{
1422 ATRACE_CALL();
1423 ALOGV("%s", __FUNCTION__);
1424
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001425 binder::Status res;
1426 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Zhijun He2ab500c2013-07-23 08:02:53 -07001427
1428 Mutex::Autolock icl(mBinderSerializationLock);
1429
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001430 if (!mDevice.get()) {
1431 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1432 }
Zhijun He2ab500c2013-07-23 08:02:53 -07001433
1434 // FIXME: Also need check repeating burst.
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001435 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001436 if (mStreamingRequestId != REQUEST_ID_NONE) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001437 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001438 "Camera %s: Try to waitUntilIdle when there are active streaming requests",
1439 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001440 ALOGE("%s: %s", __FUNCTION__, msg.string());
1441 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Zhijun He2ab500c2013-07-23 08:02:53 -07001442 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001443 status_t err = mDevice->waitUntilDrained();
1444 if (err != OK) {
1445 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001446 "Camera %s: Error waiting to drain: %s (%d)",
1447 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001448 }
Zhijun He2ab500c2013-07-23 08:02:53 -07001449 ALOGV("%s Done", __FUNCTION__);
Zhijun He2ab500c2013-07-23 08:02:53 -07001450 return res;
1451}
1452
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001453binder::Status CameraDeviceClient::flush(
1454 /*out*/
1455 int64_t* lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001456 ATRACE_CALL();
1457 ALOGV("%s", __FUNCTION__);
1458
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001459 binder::Status res;
1460 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001461
1462 Mutex::Autolock icl(mBinderSerializationLock);
1463
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001464 if (!mDevice.get()) {
1465 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1466 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001467
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001468 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001469 mStreamingRequestId = REQUEST_ID_NONE;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001470 status_t err = mDevice->flush(lastFrameNumber);
1471 if (err != OK) {
1472 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001473 "Camera %s: Error flushing device: %s (%d)", mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001474 }
1475 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001476}
1477
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001478binder::Status CameraDeviceClient::prepare(int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001479 ATRACE_CALL();
1480 ALOGV("%s", __FUNCTION__);
1481
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001482 binder::Status res;
1483 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001484
1485 Mutex::Autolock icl(mBinderSerializationLock);
1486
1487 // Guard against trying to prepare non-created streams
1488 ssize_t index = NAME_NOT_FOUND;
1489 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001490 if (streamId == mStreamMap.valueAt(i).streamId()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001491 index = i;
1492 break;
1493 }
1494 }
1495
1496 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001497 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1498 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001499 ALOGW("%s: %s", __FUNCTION__, msg.string());
1500 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001501 }
1502
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001503 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1504 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001505 status_t err = mDevice->prepare(streamId);
1506 if (err == BAD_VALUE) {
1507 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001508 "Camera %s: Stream %d has already been used, and cannot be prepared",
1509 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001510 } else if (err != OK) {
1511 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001512 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001513 strerror(-err), err);
1514 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001515 return res;
1516}
1517
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001518binder::Status CameraDeviceClient::prepare2(int maxCount, int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001519 ATRACE_CALL();
1520 ALOGV("%s", __FUNCTION__);
1521
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001522 binder::Status res;
1523 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Ruben Brunkc78ac262015-08-13 17:58:46 -07001524
1525 Mutex::Autolock icl(mBinderSerializationLock);
1526
1527 // Guard against trying to prepare non-created streams
1528 ssize_t index = NAME_NOT_FOUND;
1529 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001530 if (streamId == mStreamMap.valueAt(i).streamId()) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001531 index = i;
1532 break;
1533 }
1534 }
1535
1536 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001537 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1538 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001539 ALOGW("%s: %s", __FUNCTION__, msg.string());
1540 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -07001541 }
1542
1543 if (maxCount <= 0) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001544 String8 msg = String8::format("Camera %s: maxCount (%d) must be greater than 0",
1545 mCameraIdStr.string(), maxCount);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001546 ALOGE("%s: %s", __FUNCTION__, msg.string());
1547 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -07001548 }
1549
1550 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1551 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001552 status_t err = mDevice->prepare(maxCount, streamId);
1553 if (err == BAD_VALUE) {
1554 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001555 "Camera %s: Stream %d has already been used, and cannot be prepared",
1556 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001557 } else if (err != OK) {
1558 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001559 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001560 strerror(-err), err);
1561 }
Ruben Brunkc78ac262015-08-13 17:58:46 -07001562
1563 return res;
1564}
1565
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001566binder::Status CameraDeviceClient::tearDown(int streamId) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001567 ATRACE_CALL();
1568 ALOGV("%s", __FUNCTION__);
1569
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001570 binder::Status res;
1571 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001572
1573 Mutex::Autolock icl(mBinderSerializationLock);
1574
1575 // Guard against trying to prepare non-created streams
1576 ssize_t index = NAME_NOT_FOUND;
1577 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001578 if (streamId == mStreamMap.valueAt(i).streamId()) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001579 index = i;
1580 break;
1581 }
1582 }
1583
1584 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001585 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1586 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001587 ALOGW("%s: %s", __FUNCTION__, msg.string());
1588 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001589 }
1590
1591 // Also returns BAD_VALUE if stream ID was not valid or if the stream is in
1592 // use
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001593 status_t err = mDevice->tearDown(streamId);
1594 if (err == BAD_VALUE) {
1595 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001596 "Camera %s: Stream %d is still in use, cannot be torn down",
1597 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001598 } else if (err != OK) {
1599 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001600 "Camera %s: Error tearing down stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001601 strerror(-err), err);
1602 }
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001603
1604 return res;
1605}
1606
Shuzhen Wang758c2152017-01-10 18:26:18 -08001607binder::Status CameraDeviceClient::finalizeOutputConfigurations(int32_t streamId,
Zhijun He5d677d12016-05-29 16:52:39 -07001608 const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
1609 ATRACE_CALL();
1610
1611 binder::Status res;
1612 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1613
1614 Mutex::Autolock icl(mBinderSerializationLock);
1615
Shuzhen Wang0129d522016-10-30 22:43:41 -07001616 const std::vector<sp<IGraphicBufferProducer> >& bufferProducers =
1617 outputConfiguration.getGraphicBufferProducers();
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -07001618 String8 physicalId(outputConfiguration.getPhysicalCameraId());
Zhijun He5d677d12016-05-29 16:52:39 -07001619
Shuzhen Wang0129d522016-10-30 22:43:41 -07001620 if (bufferProducers.size() == 0) {
1621 ALOGE("%s: bufferProducers must not be empty", __FUNCTION__);
Zhijun He5d677d12016-05-29 16:52:39 -07001622 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Target Surface is invalid");
1623 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001624
Shuzhen Wang758c2152017-01-10 18:26:18 -08001625 // streamId should be in mStreamMap if this stream already has a surface attached
1626 // to it. Otherwise, it should be in mDeferredStreams.
1627 bool streamIdConfigured = false;
1628 ssize_t deferredStreamIndex = NAME_NOT_FOUND;
1629 for (size_t i = 0; i < mStreamMap.size(); i++) {
1630 if (mStreamMap.valueAt(i).streamId() == streamId) {
1631 streamIdConfigured = true;
1632 break;
1633 }
Zhijun He5d677d12016-05-29 16:52:39 -07001634 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001635 for (size_t i = 0; i < mDeferredStreams.size(); i++) {
1636 if (streamId == mDeferredStreams[i]) {
1637 deferredStreamIndex = i;
1638 break;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001639 }
1640
Shuzhen Wang758c2152017-01-10 18:26:18 -08001641 }
1642 if (deferredStreamIndex == NAME_NOT_FOUND && !streamIdConfigured) {
1643 String8 msg = String8::format("Camera %s: deferred surface is set to a unknown stream"
1644 "(ID %d)", mCameraIdStr.string(), streamId);
1645 ALOGW("%s: %s", __FUNCTION__, msg.string());
1646 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Zhijun He5d677d12016-05-29 16:52:39 -07001647 }
1648
Shuzhen Wang88fd0052017-02-09 16:40:07 -08001649 if (mStreamInfoMap[streamId].finalized) {
1650 String8 msg = String8::format("Camera %s: finalizeOutputConfigurations has been called"
1651 " on stream ID %d", mCameraIdStr.string(), streamId);
1652 ALOGW("%s: %s", __FUNCTION__, msg.string());
1653 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1654 }
1655
Zhijun He5d677d12016-05-29 16:52:39 -07001656 if (!mDevice.get()) {
1657 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1658 }
1659
Shuzhen Wang758c2152017-01-10 18:26:18 -08001660 std::vector<sp<Surface>> consumerSurfaces;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001661 const std::vector<int32_t> &sensorPixelModesUsed =
1662 outputConfiguration.getSensorPixelModesUsed();
Emilian Peevc81a7592022-02-14 17:38:18 -08001663 int64_t dynamicRangeProfile = outputConfiguration.getDynamicRangeProfile();
Austin Borger9e2b27c2022-07-15 11:27:24 -07001664 int32_t colorSpace = outputConfiguration.getColorSpace();
1665 int64_t streamUseCase = outputConfiguration.getStreamUseCase();
Shuzhen Wange4208922022-02-01 16:52:48 -08001666 int timestampBase = outputConfiguration.getTimestampBase();
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001667 int mirrorMode = outputConfiguration.getMirrorMode();
Shuzhen Wang758c2152017-01-10 18:26:18 -08001668 for (auto& bufferProducer : bufferProducers) {
1669 // Don't create multiple streams for the same target surface
Zhijun He5d677d12016-05-29 16:52:39 -07001670 ssize_t index = mStreamMap.indexOfKey(IInterface::asBinder(bufferProducer));
1671 if (index != NAME_NOT_FOUND) {
Shuzhen Wang758c2152017-01-10 18:26:18 -08001672 ALOGV("Camera %s: Surface already has a stream created "
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001673 " for it (ID %zd)", mCameraIdStr.string(), index);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001674 continue;
Zhijun He5d677d12016-05-29 16:52:39 -07001675 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001676
1677 sp<Surface> surface;
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07001678 res = SessionConfigurationUtils::createSurfaceFromGbp(mStreamInfoMap[streamId],
Colin Crossb8a9dbb2020-08-27 04:12:26 +00001679 true /*isStreamInfoValid*/, surface, bufferProducer, mCameraIdStr,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001680 mDevice->infoPhysical(physicalId), sensorPixelModesUsed, dynamicRangeProfile,
Austin Borger9e2b27c2022-07-15 11:27:24 -07001681 streamUseCase, timestampBase, mirrorMode, colorSpace);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001682
1683 if (!res.isOk())
1684 return res;
1685
1686 consumerSurfaces.push_back(surface);
Zhijun He5d677d12016-05-29 16:52:39 -07001687 }
1688
Shuzhen Wanga81ce342017-04-13 15:18:36 -07001689 // Gracefully handle case where finalizeOutputConfigurations is called
1690 // without any new surface.
1691 if (consumerSurfaces.size() == 0) {
1692 mStreamInfoMap[streamId].finalized = true;
1693 return res;
1694 }
1695
Zhijun He5d677d12016-05-29 16:52:39 -07001696 // Finish the deferred stream configuration with the surface.
Shuzhen Wang758c2152017-01-10 18:26:18 -08001697 status_t err;
Emilian Peev40ead602017-09-26 15:46:36 +01001698 std::vector<int> consumerSurfaceIds;
1699 err = mDevice->setConsumerSurfaces(streamId, consumerSurfaces, &consumerSurfaceIds);
Zhijun He5d677d12016-05-29 16:52:39 -07001700 if (err == OK) {
Shuzhen Wang758c2152017-01-10 18:26:18 -08001701 for (size_t i = 0; i < consumerSurfaces.size(); i++) {
1702 sp<IBinder> binder = IInterface::asBinder(
1703 consumerSurfaces[i]->getIGraphicBufferProducer());
Emilian Peev40ead602017-09-26 15:46:36 +01001704 ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %d", __FUNCTION__,
Shuzhen Wang758c2152017-01-10 18:26:18 -08001705 binder.get(), streamId, consumerSurfaceIds[i]);
1706 mStreamMap.add(binder, StreamSurfaceId(streamId, consumerSurfaceIds[i]));
1707 }
1708 if (deferredStreamIndex != NAME_NOT_FOUND) {
1709 mDeferredStreams.removeItemsAt(deferredStreamIndex);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001710 }
Shuzhen Wang88fd0052017-02-09 16:40:07 -08001711 mStreamInfoMap[streamId].finalized = true;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -08001712 mConfiguredOutputs.replaceValueFor(streamId, outputConfiguration);
Zhijun He5d677d12016-05-29 16:52:39 -07001713 } else if (err == NO_INIT) {
1714 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001715 "Camera %s: Deferred surface is invalid: %s (%d)",
1716 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001717 } else {
1718 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001719 "Camera %s: Error setting output stream deferred surface: %s (%d)",
1720 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001721 }
1722
1723 return res;
1724}
1725
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001726binder::Status CameraDeviceClient::setCameraAudioRestriction(int32_t mode) {
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001727 ATRACE_CALL();
1728 binder::Status res;
1729 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1730
1731 if (!isValidAudioRestriction(mode)) {
1732 String8 msg = String8::format("Camera %s: invalid audio restriction mode %d",
1733 mCameraIdStr.string(), mode);
1734 ALOGW("%s: %s", __FUNCTION__, msg.string());
1735 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1736 }
1737
1738 Mutex::Autolock icl(mBinderSerializationLock);
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001739 BasicClient::setAudioRestriction(mode);
1740 return binder::Status::ok();
1741}
1742
1743binder::Status CameraDeviceClient::getGlobalAudioRestriction(/*out*/ int32_t* outMode) {
1744 ATRACE_CALL();
1745 binder::Status res;
1746 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1747 Mutex::Autolock icl(mBinderSerializationLock);
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001748 if (outMode != nullptr) {
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001749 *outMode = BasicClient::getServiceAudioRestriction();
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001750 }
1751 return binder::Status::ok();
1752}
1753
Ravneetaeb20dc2022-03-30 05:33:03 +00001754status_t CameraDeviceClient::setCameraServiceWatchdog(bool enabled) {
1755 return mDevice->setCameraServiceWatchdog(enabled);
1756}
1757
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08001758status_t CameraDeviceClient::setRotateAndCropOverride(uint8_t rotateAndCrop) {
1759 if (rotateAndCrop > ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return BAD_VALUE;
1760
1761 return mDevice->setRotateAndCropAutoBehavior(
1762 static_cast<camera_metadata_enum_android_scaler_rotate_and_crop_t>(rotateAndCrop));
1763}
1764
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00001765status_t CameraDeviceClient::setAutoframingOverride(uint8_t autoframingValue) {
1766 if (autoframingValue > ANDROID_CONTROL_AUTOFRAMING_AUTO) return BAD_VALUE;
1767
1768 return mDevice->setAutoframingAutoBehavior(
1769 static_cast<camera_metadata_enum_android_control_autoframing_t>(autoframingValue));
1770}
1771
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08001772bool CameraDeviceClient::supportsCameraMute() {
1773 return mDevice->supportsCameraMute();
1774}
1775
1776status_t CameraDeviceClient::setCameraMute(bool enabled) {
1777 return mDevice->setCameraMute(enabled);
1778}
1779
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001780binder::Status CameraDeviceClient::switchToOffline(
1781 const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb,
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001782 const std::vector<int>& offlineOutputIds,
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001783 /*out*/
1784 sp<hardware::camera2::ICameraOfflineSession>* session) {
1785 ATRACE_CALL();
1786
1787 binder::Status res;
1788 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1789
1790 Mutex::Autolock icl(mBinderSerializationLock);
1791
1792 if (!mDevice.get()) {
1793 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1794 }
1795
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001796 if (offlineOutputIds.empty()) {
Emilian Peevcc0b7952020-01-07 13:54:47 -08001797 String8 msg = String8::format("Offline surfaces must not be empty");
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001798 ALOGE("%s: %s", __FUNCTION__, msg.string());
1799 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1800 }
1801
1802 if (session == nullptr) {
1803 String8 msg = String8::format("Invalid offline session");
1804 ALOGE("%s: %s", __FUNCTION__, msg.string());
1805 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1806 }
1807
Yin-Chia Yeh87fccca2020-01-28 09:37:18 -08001808 std::vector<int32_t> offlineStreamIds;
1809 offlineStreamIds.reserve(offlineOutputIds.size());
Emilian Peev4697b642019-11-19 17:11:14 -08001810 KeyedVector<sp<IBinder>, sp<CompositeStream>> offlineCompositeStreamMap;
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001811 for (const auto& streamId : offlineOutputIds) {
1812 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001813 if (index == NAME_NOT_FOUND) {
Emilian Peevcc0b7952020-01-07 13:54:47 -08001814 String8 msg = String8::format("Offline surface with id: %d is not registered",
1815 streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001816 ALOGE("%s: %s", __FUNCTION__, msg.string());
1817 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1818 }
Emilian Peevcc0b7952020-01-07 13:54:47 -08001819
1820 if (!mStreamInfoMap[streamId].supportsOffline) {
1821 String8 msg = String8::format("Offline surface with id: %d doesn't support "
1822 "offline mode", streamId);
1823 ALOGE("%s: %s", __FUNCTION__, msg.string());
1824 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1825 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001826
Emilian Peev2f5d6012022-01-19 16:16:50 -08001827 Mutex::Autolock l(mCompositeLock);
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001828 bool isCompositeStream = false;
Emilian Peev2f5d6012022-01-19 16:16:50 -08001829 for (const auto& gbp : mConfiguredOutputs.valueAt(index).getGraphicBufferProducers()) {
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001830 sp<Surface> s = new Surface(gbp, false /*controlledByApp*/);
Pirama Arumuga Nainar8db21062022-01-28 10:15:12 -08001831 isCompositeStream = camera3::DepthCompositeStream::isDepthCompositeStream(s) ||
Emilian Peev434248e2022-10-06 14:58:54 -07001832 camera3::HeicCompositeStream::isHeicCompositeStream(s) ||
1833 camera3::JpegRCompositeStream::isJpegRCompositeStream(s);
Emilian Peev4697b642019-11-19 17:11:14 -08001834 if (isCompositeStream) {
1835 auto compositeIdx = mCompositeStreamMap.indexOfKey(IInterface::asBinder(gbp));
1836 if (compositeIdx == NAME_NOT_FOUND) {
1837 ALOGE("%s: Unknown composite stream", __FUNCTION__);
1838 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1839 "Unknown composite stream");
1840 }
1841
1842 mCompositeStreamMap.valueAt(compositeIdx)->insertCompositeStreamIds(
1843 &offlineStreamIds);
1844 offlineCompositeStreamMap.add(mCompositeStreamMap.keyAt(compositeIdx),
1845 mCompositeStreamMap.valueAt(compositeIdx));
1846 break;
1847 }
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001848 }
1849
Emilian Peev4697b642019-11-19 17:11:14 -08001850 if (!isCompositeStream) {
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001851 offlineStreamIds.push_back(streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001852 }
1853 }
1854
1855 sp<CameraOfflineSessionBase> offlineSession;
1856 auto ret = mDevice->switchToOffline(offlineStreamIds, &offlineSession);
1857 if (ret != OK) {
1858 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1859 "Camera %s: Error switching to offline mode: %s (%d)",
1860 mCameraIdStr.string(), strerror(ret), ret);
1861 }
1862
Emilian Peevcc0b7952020-01-07 13:54:47 -08001863 sp<CameraOfflineSessionClient> offlineClient;
1864 if (offlineSession.get() != nullptr) {
1865 offlineClient = new CameraOfflineSessionClient(sCameraService,
1866 offlineSession, offlineCompositeStreamMap, cameraCb, mClientPackageName,
Emilian Peev8b64f282021-03-25 16:49:57 -07001867 mClientFeatureId, mCameraIdStr, mCameraFacing, mOrientation, mClientPid, mClientUid,
1868 mServicePid);
Emilian Peevcc0b7952020-01-07 13:54:47 -08001869 ret = sCameraService->addOfflineClient(mCameraIdStr, offlineClient);
1870 }
1871
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001872 if (ret == OK) {
Emilian Peevd99c8ae2019-11-26 13:19:13 -08001873 // A successful offline session switch must reset the current camera client
1874 // and release any resources occupied by previously configured streams.
1875 mStreamMap.clear();
1876 mConfiguredOutputs.clear();
1877 mDeferredStreams.clear();
1878 mStreamInfoMap.clear();
Emilian Peev2f5d6012022-01-19 16:16:50 -08001879 Mutex::Autolock l(mCompositeLock);
Emilian Peevd99c8ae2019-11-26 13:19:13 -08001880 mCompositeStreamMap.clear();
1881 mInputStream = {false, 0, 0, 0, 0};
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001882 } else {
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001883 switch(ret) {
1884 case BAD_VALUE:
1885 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1886 "Illegal argument to HAL module for camera \"%s\"", mCameraIdStr.c_str());
1887 case TIMED_OUT:
1888 return STATUS_ERROR_FMT(CameraService::ERROR_CAMERA_IN_USE,
1889 "Camera \"%s\" is already open", mCameraIdStr.c_str());
1890 default:
1891 return STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1892 "Failed to initialize camera \"%s\": %s (%d)", mCameraIdStr.c_str(),
1893 strerror(-ret), ret);
1894 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001895 }
1896
1897 *session = offlineClient;
1898
1899 return binder::Status::ok();
1900}
1901
Igor Murashkine7ee7632013-06-11 18:10:18 -07001902status_t CameraDeviceClient::dump(int fd, const Vector<String16>& args) {
Eino-Ville Talvalac4003962016-01-13 10:07:04 -08001903 return BasicClient::dump(fd, args);
1904}
1905
1906status_t CameraDeviceClient::dumpClient(int fd, const Vector<String16>& args) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001907 dprintf(fd, " CameraDeviceClient[%s] (%p) dump:\n",
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001908 mCameraIdStr.string(),
Eino-Ville Talvalae992e752014-11-07 16:17:48 -08001909 (getRemoteCallback() != NULL ?
Marco Nelissenf8880202014-11-14 07:58:25 -08001910 IInterface::asBinder(getRemoteCallback()).get() : NULL) );
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001911 dprintf(fd, " Current client UID %u\n", mClientUid);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001912
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001913 dprintf(fd, " State:\n");
1914 dprintf(fd, " Request ID counter: %d\n", mRequestIdCounter);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001915 if (mInputStream.configured) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001916 dprintf(fd, " Current input stream ID: %d\n", mInputStream.id);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001917 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001918 dprintf(fd, " No input stream configured.\n");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001919 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001920 if (!mStreamMap.isEmpty()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001921 dprintf(fd, " Current output stream/surface IDs:\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001922 for (size_t i = 0; i < mStreamMap.size(); i++) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001923 dprintf(fd, " Stream %d Surface %d\n",
Shuzhen Wang0129d522016-10-30 22:43:41 -07001924 mStreamMap.valueAt(i).streamId(),
1925 mStreamMap.valueAt(i).surfaceId());
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001926 }
Zhijun He5d677d12016-05-29 16:52:39 -07001927 } else if (!mDeferredStreams.isEmpty()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001928 dprintf(fd, " Current deferred surface output stream IDs:\n");
Zhijun He5d677d12016-05-29 16:52:39 -07001929 for (auto& streamId : mDeferredStreams) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001930 dprintf(fd, " Stream %d\n", streamId);
Zhijun He5d677d12016-05-29 16:52:39 -07001931 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001932 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001933 dprintf(fd, " No output streams configured.\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001934 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001935 // TODO: print dynamic/request section from most recent requests
1936 mFrameProcessor->dump(fd, args);
1937
1938 return dumpDevice(fd, args);
1939}
1940
Avichal Rakesh7e53cad2021-10-05 13:46:30 -07001941status_t CameraDeviceClient::startWatchingTags(const String8 &tags, int out) {
1942 sp<CameraDeviceBase> device = mDevice;
1943 if (!device) {
1944 dprintf(out, " Device is detached.");
1945 return OK;
1946 }
1947 device->startWatchingTags(tags);
1948 return OK;
1949}
1950
1951status_t CameraDeviceClient::stopWatchingTags(int out) {
1952 sp<CameraDeviceBase> device = mDevice;
1953 if (!device) {
1954 dprintf(out, " Device is detached.");
1955 return OK;
1956 }
1957 device->stopWatchingTags();
1958 return OK;
1959}
1960
1961status_t CameraDeviceClient::dumpWatchedEventsToVector(std::vector<std::string> &out) {
1962 sp<CameraDeviceBase> device = mDevice;
1963 if (!device) {
1964 return OK;
1965 }
1966 device->dumpWatchedEventsToVector(out);
1967 return OK;
1968}
1969
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001970void CameraDeviceClient::notifyError(int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -07001971 const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001972 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001973 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001974
Emilian Peev538c90e2018-12-17 18:03:19 +00001975 bool skipClientNotification = false;
Emilian Peev2f5d6012022-01-19 16:16:50 -08001976 {
1977 // Access to the composite stream map must be synchronized
1978 Mutex::Autolock l(mCompositeLock);
1979 // Composites can have multiple internal streams. Error notifications coming from such
1980 // internal streams may need to remain within camera service.
1981 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
1982 skipClientNotification |= mCompositeStreamMap.valueAt(i)->onError(errorCode,
1983 resultExtras);
1984 }
Emilian Peev538c90e2018-12-17 18:03:19 +00001985 }
1986
1987 if ((remoteCb != 0) && (!skipClientNotification)) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001988 remoteCb->onDeviceError(errorCode, resultExtras);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001989 }
1990}
1991
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001992void CameraDeviceClient::notifyRepeatingRequestError(long lastFrameNumber) {
1993 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
1994
1995 if (remoteCb != 0) {
Yin-Chia Yeh8ca23dc2017-09-05 18:15:56 -07001996 remoteCb->onRepeatingRequestError(lastFrameNumber, mStreamingRequestId);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001997 }
1998
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001999 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07002000 mStreamingRequestId = REQUEST_ID_NONE;
2001}
2002
Shuzhen Wang316781a2020-08-18 18:11:01 -07002003void CameraDeviceClient::notifyIdle(
2004 int64_t requestCount, int64_t resultErrorCount, bool deviceError,
2005 const std::vector<hardware::CameraStreamStats>& streamStats) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002006 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002007 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002008
2009 if (remoteCb != 0) {
2010 remoteCb->onDeviceIdle();
2011 }
Shuzhen Wangd26b1862022-03-07 12:05:05 -08002012 Camera2ClientBase::notifyIdleWithUserTag(requestCount, resultErrorCount, deviceError,
Shuzhen Wang9372b0b2022-05-11 18:55:19 -07002013 streamStats, mUserTag, mVideoStabilizationMode);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002014}
2015
Jianing Weicb0652e2014-03-12 18:29:36 -07002016void CameraDeviceClient::notifyShutter(const CaptureResultExtras& resultExtras,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002017 nsecs_t timestamp) {
2018 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002019 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002020 if (remoteCb != 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -07002021 remoteCb->onCaptureStarted(resultExtras, timestamp);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002022 }
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07002023 Camera2ClientBase::notifyShutter(resultExtras, timestamp);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08002024
Emilian Peev2f5d6012022-01-19 16:16:50 -08002025 // Access to the composite stream map must be synchronized
2026 Mutex::Autolock l(mCompositeLock);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08002027 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
2028 mCompositeStreamMap.valueAt(i)->onShutter(resultExtras, timestamp);
2029 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002030}
2031
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002032void CameraDeviceClient::notifyPrepared(int streamId) {
2033 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002034 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002035 if (remoteCb != 0) {
2036 remoteCb->onPrepared(streamId);
2037 }
2038}
2039
Shuzhen Wang9d066012016-09-30 11:30:20 -07002040void CameraDeviceClient::notifyRequestQueueEmpty() {
2041 // Thread safe. Don't bother locking.
2042 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
2043 if (remoteCb != 0) {
2044 remoteCb->onRequestQueueEmpty();
2045 }
2046}
2047
Igor Murashkine7ee7632013-06-11 18:10:18 -07002048void CameraDeviceClient::detachDevice() {
2049 if (mDevice == 0) return;
2050
Shuzhen Wang316781a2020-08-18 18:11:01 -07002051 nsecs_t startTime = systemTime();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002052 ALOGV("Camera %s: Stopping processors", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -07002053
Emilian Peev7ee731f2022-02-08 14:55:52 -08002054 if (mFrameProcessor.get() != nullptr) {
2055 mFrameProcessor->removeListener(
2056 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MIN_ID,
2057 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MAX_ID, /*listener*/this);
2058 mFrameProcessor->requestExit();
2059 ALOGV("Camera %s: Waiting for threads", mCameraIdStr.string());
2060 mFrameProcessor->join();
2061 ALOGV("Camera %s: Disconnecting device", mCameraIdStr.string());
2062 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07002063
2064 // WORKAROUND: HAL refuses to disconnect while there's streams in flight
2065 {
Emilian Peev6b51d7d2018-07-23 11:41:44 +01002066 int64_t lastFrameNumber;
Igor Murashkine7ee7632013-06-11 18:10:18 -07002067 status_t code;
Emilian Peev6b51d7d2018-07-23 11:41:44 +01002068 if ((code = mDevice->flush(&lastFrameNumber)) != OK) {
2069 ALOGE("%s: flush failed with code 0x%x", __FUNCTION__, code);
2070 }
2071
Igor Murashkine7ee7632013-06-11 18:10:18 -07002072 if ((code = mDevice->waitUntilDrained()) != OK) {
2073 ALOGE("%s: waitUntilDrained failed with code 0x%x", __FUNCTION__,
2074 code);
2075 }
2076 }
2077
Emilian Peev2f5d6012022-01-19 16:16:50 -08002078 {
2079 Mutex::Autolock l(mCompositeLock);
2080 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
2081 auto ret = mCompositeStreamMap.valueAt(i)->deleteInternalStreams();
2082 if (ret != OK) {
2083 ALOGE("%s: Failed removing composite stream %s (%d)", __FUNCTION__,
2084 strerror(-ret), ret);
2085 }
Emilian Peev5e4c7322019-10-22 14:20:52 -07002086 }
Emilian Peev2f5d6012022-01-19 16:16:50 -08002087 mCompositeStreamMap.clear();
Emilian Peev5e4c7322019-10-22 14:20:52 -07002088 }
Emilian Peev5e4c7322019-10-22 14:20:52 -07002089
Igor Murashkine7ee7632013-06-11 18:10:18 -07002090 Camera2ClientBase::detachDevice();
Shuzhen Wang316781a2020-08-18 18:11:01 -07002091
2092 int32_t closeLatencyMs = ns2ms(systemTime() - startTime);
Austin Borger74fca042022-05-23 12:41:21 -07002093 mCameraServiceProxyWrapper->logClose(mCameraIdStr, closeLatencyMs);
Igor Murashkine7ee7632013-06-11 18:10:18 -07002094}
2095
2096/** Device-related methods */
Jianing Weicb0652e2014-03-12 18:29:36 -07002097void CameraDeviceClient::onResultAvailable(const CaptureResult& result) {
Igor Murashkine7ee7632013-06-11 18:10:18 -07002098 ATRACE_CALL();
2099 ALOGV("%s", __FUNCTION__);
2100
Igor Murashkin4fb55c12013-08-29 17:43:01 -07002101 // Thread-safe. No lock necessary.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002102 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = mRemoteCallback;
Igor Murashkin4fb55c12013-08-29 17:43:01 -07002103 if (remoteCb != NULL) {
Shuzhen Wang5c22c152017-12-31 17:12:25 -08002104 remoteCb->onResultReceived(result.mMetadata, result.mResultExtras,
2105 result.mPhysicalMetadatas);
Igor Murashkine7ee7632013-06-11 18:10:18 -07002106 }
Emilian Peev538c90e2018-12-17 18:03:19 +00002107
Emilian Peev2f5d6012022-01-19 16:16:50 -08002108 // Access to the composite stream map must be synchronized
2109 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +00002110 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
2111 mCompositeStreamMap.valueAt(i)->onResultAvailable(result);
2112 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07002113}
2114
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002115binder::Status CameraDeviceClient::checkPidStatus(const char* checkLocation) {
Eino-Ville Talvala6192b892016-04-04 12:31:18 -07002116 if (mDisconnected) {
2117 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED,
2118 "The camera device has been disconnected");
2119 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002120 status_t res = checkPid(checkLocation);
2121 return (res == OK) ? binder::Status::ok() :
2122 STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
2123 "Attempt to use camera from a different process than original client");
2124}
2125
Igor Murashkine7ee7632013-06-11 18:10:18 -07002126// TODO: move to Camera2ClientBase
2127bool CameraDeviceClient::enforceRequestPermissions(CameraMetadata& metadata) {
2128
Jayant Chowdhary12361932018-08-27 14:46:13 -07002129 const int pid = CameraThreadState::getCallingPid();
Igor Murashkine7ee7632013-06-11 18:10:18 -07002130 const int selfPid = getpid();
2131 camera_metadata_entry_t entry;
2132
2133 /**
2134 * Mixin default important security values
2135 * - android.led.transmit = defaulted ON
2136 */
2137 CameraMetadata staticInfo = mDevice->info();
2138 entry = staticInfo.find(ANDROID_LED_AVAILABLE_LEDS);
2139 for(size_t i = 0; i < entry.count; ++i) {
2140 uint8_t led = entry.data.u8[i];
2141
2142 switch(led) {
2143 case ANDROID_LED_AVAILABLE_LEDS_TRANSMIT: {
2144 uint8_t transmitDefault = ANDROID_LED_TRANSMIT_ON;
2145 if (!metadata.exists(ANDROID_LED_TRANSMIT)) {
2146 metadata.update(ANDROID_LED_TRANSMIT,
2147 &transmitDefault, 1);
2148 }
2149 break;
2150 }
2151 }
2152 }
2153
2154 // We can do anything!
2155 if (pid == selfPid) {
2156 return true;
2157 }
2158
2159 /**
2160 * Permission check special fields in the request
2161 * - android.led.transmit = android.permission.CAMERA_DISABLE_TRANSMIT
2162 */
2163 entry = metadata.find(ANDROID_LED_TRANSMIT);
2164 if (entry.count > 0 && entry.data.u8[0] != ANDROID_LED_TRANSMIT_ON) {
2165 String16 permissionString =
2166 String16("android.permission.CAMERA_DISABLE_TRANSMIT_LED");
2167 if (!checkCallingPermission(permissionString)) {
Jayant Chowdhary12361932018-08-27 14:46:13 -07002168 const int uid = CameraThreadState::getCallingUid();
Igor Murashkine7ee7632013-06-11 18:10:18 -07002169 ALOGE("Permission Denial: "
2170 "can't disable transmit LED pid=%d, uid=%d", pid, uid);
2171 return false;
2172 }
2173 }
2174
2175 return true;
2176}
2177
Shuzhen Wang610d7b82022-02-08 14:37:22 -08002178status_t CameraDeviceClient::getRotationTransformLocked(int mirrorMode,
2179 int32_t* transform) {
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07002180 ALOGV("%s: begin", __FUNCTION__);
2181
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07002182 const CameraMetadata& staticInfo = mDevice->info();
Shuzhen Wang610d7b82022-02-08 14:37:22 -08002183 return CameraUtils::getRotationTransform(staticInfo, mirrorMode, transform);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07002184}
2185
Emilian Peevf4816702020-04-03 15:44:51 -07002186binder::Status CameraDeviceClient::mapRequestTemplate(int templateId,
2187 camera_request_template_t* tempId /*out*/) {
2188 binder::Status ret = binder::Status::ok();
2189
2190 if (tempId == nullptr) {
2191 ret = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
2192 "Camera %s: Invalid template argument", mCameraIdStr.string());
2193 return ret;
2194 }
2195 switch(templateId) {
2196 case ICameraDeviceUser::TEMPLATE_PREVIEW:
2197 *tempId = camera_request_template_t::CAMERA_TEMPLATE_PREVIEW;
2198 break;
2199 case ICameraDeviceUser::TEMPLATE_RECORD:
2200 *tempId = camera_request_template_t::CAMERA_TEMPLATE_VIDEO_RECORD;
2201 break;
2202 case ICameraDeviceUser::TEMPLATE_STILL_CAPTURE:
2203 *tempId = camera_request_template_t::CAMERA_TEMPLATE_STILL_CAPTURE;
2204 break;
2205 case ICameraDeviceUser::TEMPLATE_VIDEO_SNAPSHOT:
2206 *tempId = camera_request_template_t::CAMERA_TEMPLATE_VIDEO_SNAPSHOT;
2207 break;
2208 case ICameraDeviceUser::TEMPLATE_ZERO_SHUTTER_LAG:
2209 *tempId = camera_request_template_t::CAMERA_TEMPLATE_ZERO_SHUTTER_LAG;
2210 break;
2211 case ICameraDeviceUser::TEMPLATE_MANUAL:
2212 *tempId = camera_request_template_t::CAMERA_TEMPLATE_MANUAL;
2213 break;
2214 default:
2215 ret = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
2216 "Camera %s: Template ID %d is invalid or not supported",
2217 mCameraIdStr.string(), templateId);
2218 return ret;
2219 }
2220
2221 return ret;
2222}
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08002223
2224const CameraMetadata &CameraDeviceClient::getStaticInfo(const String8 &cameraId) {
2225 if (mDevice->getId() == cameraId) {
2226 return mDevice->info();
2227 }
2228 return mDevice->infoPhysical(cameraId);
2229}
2230
2231bool CameraDeviceClient::isUltraHighResolutionSensor(const String8 &cameraId) {
2232 const CameraMetadata &deviceInfo = getStaticInfo(cameraId);
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07002233 return SessionConfigurationUtils::isUltraHighResolutionSensor(deviceInfo);
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08002234}
2235
2236bool CameraDeviceClient::isSensorPixelModeConsistent(
2237 const std::list<int> &streamIdList, const CameraMetadata &settings) {
2238 // First we get the sensorPixelMode from the settings metadata.
2239 int32_t sensorPixelMode = ANDROID_SENSOR_PIXEL_MODE_DEFAULT;
2240 camera_metadata_ro_entry sensorPixelModeEntry = settings.find(ANDROID_SENSOR_PIXEL_MODE);
2241 if (sensorPixelModeEntry.count != 0) {
2242 sensorPixelMode = sensorPixelModeEntry.data.u8[0];
2243 if (sensorPixelMode != ANDROID_SENSOR_PIXEL_MODE_DEFAULT &&
2244 sensorPixelMode != ANDROID_SENSOR_PIXEL_MODE_MAXIMUM_RESOLUTION) {
2245 ALOGE("%s: Request sensor pixel mode not is not one of the valid values %d",
2246 __FUNCTION__, sensorPixelMode);
2247 return false;
2248 }
2249 }
2250 // Check whether each stream has max resolution allowed.
2251 bool consistent = true;
2252 for (auto it : streamIdList) {
2253 auto const streamInfoIt = mStreamInfoMap.find(it);
2254 if (streamInfoIt == mStreamInfoMap.end()) {
2255 ALOGE("%s: stream id %d not created, skipping", __FUNCTION__, it);
2256 return false;
2257 }
2258 consistent =
2259 streamInfoIt->second.sensorPixelModesUsed.find(sensorPixelMode) !=
2260 streamInfoIt->second.sensorPixelModesUsed.end();
2261 if (!consistent) {
2262 ALOGE("sensorPixelMode used %i not consistent with configured modes", sensorPixelMode);
2263 for (auto m : streamInfoIt->second.sensorPixelModesUsed) {
2264 ALOGE("sensor pixel mode used list: %i", m);
2265 }
2266 break;
2267 }
2268 }
2269
2270 return consistent;
2271}
2272
Igor Murashkine7ee7632013-06-11 18:10:18 -07002273} // namespace android