1 /*
2 * Copyright (C) 2013-2018 The Android Open Source Project
3 *
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
19 //#define LOG_NDEBUG 0
20
21 #include <com_android_internal_camera_flags.h>
22 #include <cutils/properties.h>
23 #include <utils/Log.h>
24 #include <utils/SessionConfigurationUtils.h>
25 #include <utils/Trace.h>
26 #include <gui/Surface.h>
27 #include <camera/camera2/CaptureRequest.h>
28 #include <camera/CameraUtils.h>
29 #include <camera/StringUtils.h>
30
31 #include "common/CameraDeviceBase.h"
32 #include "device3/Camera3Device.h"
33 #include "device3/Camera3OutputStream.h"
34 #include "api2/CameraDeviceClient.h"
35
36 #include <camera_metadata_hidden.h>
37
38 #include "DepthCompositeStream.h"
39 #include "HeicCompositeStream.h"
40 #include "JpegRCompositeStream.h"
41
42 // Convenience methods for constructing binder::Status objects for error returns
43
44 #define STATUS_ERROR(errorCode, errorString) \
45 binder::Status::fromServiceSpecificError(errorCode, \
46 fmt::sprintf("%s:%d: %s", __FUNCTION__, __LINE__, errorString).c_str())
47
48 #define STATUS_ERROR_FMT(errorCode, errorString, ...) \
49 binder::Status::fromServiceSpecificError(errorCode, \
50 fmt::sprintf("%s:%d: " errorString, __FUNCTION__, __LINE__, \
51 __VA_ARGS__).c_str())
52
53 namespace android {
54 using namespace camera2;
55 using namespace camera3;
56 using camera3::camera_stream_rotation_t::CAMERA_STREAM_ROTATION_0;
57
58 namespace flags = com::android::internal::camera::flags;
59
CameraDeviceClientBase(const sp<CameraService> & cameraService,const sp<hardware::camera2::ICameraDeviceCallbacks> & remoteCallback,std::shared_ptr<AttributionAndPermissionUtils> attributionAndPermissionUtils,const std::string & clientPackageName,bool systemNativeClient,const std::optional<std::string> & clientFeatureId,const std::string & cameraId,int api1CameraId,int cameraFacing,int sensorOrientation,int clientPid,uid_t clientUid,int servicePid,int rotationOverride)60 CameraDeviceClientBase::CameraDeviceClientBase(
61 const sp<CameraService>& cameraService,
62 const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback,
63 std::shared_ptr<AttributionAndPermissionUtils> attributionAndPermissionUtils,
64 const std::string& clientPackageName,
65 bool systemNativeClient,
66 const std::optional<std::string>& clientFeatureId,
67 const std::string& cameraId,
68 [[maybe_unused]] int api1CameraId,
69 int cameraFacing,
70 int sensorOrientation,
71 int clientPid,
72 uid_t clientUid,
73 int servicePid,
74 int rotationOverride) :
75 BasicClient(cameraService,
76 IInterface::asBinder(remoteCallback),
77 attributionAndPermissionUtils,
78 clientPackageName,
79 systemNativeClient,
80 clientFeatureId,
81 cameraId,
82 cameraFacing,
83 sensorOrientation,
84 clientPid,
85 clientUid,
86 servicePid,
87 rotationOverride),
88 mRemoteCallback(remoteCallback) {
89 }
90
91 // Interface used by CameraService
92
CameraDeviceClient(const sp<CameraService> & cameraService,const sp<hardware::camera2::ICameraDeviceCallbacks> & remoteCallback,std::shared_ptr<CameraServiceProxyWrapper> cameraServiceProxyWrapper,std::shared_ptr<AttributionAndPermissionUtils> attributionAndPermissionUtils,const std::string & clientPackageName,bool systemNativeClient,const std::optional<std::string> & clientFeatureId,const std::string & cameraId,int cameraFacing,int sensorOrientation,int clientPid,uid_t clientUid,int servicePid,bool overrideForPerfClass,int rotationOverride,const std::string & originalCameraId)93 CameraDeviceClient::CameraDeviceClient(const sp<CameraService>& cameraService,
94 const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback,
95 std::shared_ptr<CameraServiceProxyWrapper> cameraServiceProxyWrapper,
96 std::shared_ptr<AttributionAndPermissionUtils> attributionAndPermissionUtils,
97 const std::string& clientPackageName,
98 bool systemNativeClient,
99 const std::optional<std::string>& clientFeatureId,
100 const std::string& cameraId,
101 int cameraFacing,
102 int sensorOrientation,
103 int clientPid,
104 uid_t clientUid,
105 int servicePid,
106 bool overrideForPerfClass,
107 int rotationOverride,
108 const std::string& originalCameraId) :
109 Camera2ClientBase(cameraService, remoteCallback, cameraServiceProxyWrapper,
110 attributionAndPermissionUtils, clientPackageName,
111 systemNativeClient, clientFeatureId, cameraId, /*API1 camera ID*/ -1, cameraFacing,
112 sensorOrientation, clientPid, clientUid, servicePid, overrideForPerfClass,
113 rotationOverride),
114 mInputStream(),
115 mStreamingRequestId(REQUEST_ID_NONE),
116 mRequestIdCounter(0),
117 mOverrideForPerfClass(overrideForPerfClass),
118 mOriginalCameraId(originalCameraId) {
119 ATRACE_CALL();
120 ALOGI("CameraDeviceClient %s: Opened", cameraId.c_str());
121 }
122
initialize(sp<CameraProviderManager> manager,const std::string & monitorTags)123 status_t CameraDeviceClient::initialize(sp<CameraProviderManager> manager,
124 const std::string& monitorTags) {
125 return initializeImpl(manager, monitorTags);
126 }
127
128 template<typename TProviderPtr>
initializeImpl(TProviderPtr providerPtr,const std::string & monitorTags)129 status_t CameraDeviceClient::initializeImpl(TProviderPtr providerPtr,
130 const std::string& monitorTags) {
131 ATRACE_CALL();
132 status_t res;
133
134 res = Camera2ClientBase::initialize(providerPtr, monitorTags);
135 if (res != OK) {
136 return res;
137 }
138
139 mFrameProcessor = new FrameProcessorBase(mDevice);
140 std::string threadName = std::string("CDU-") + mCameraIdStr + "-FrameProc";
141 res = mFrameProcessor->run(threadName.c_str());
142 if (res != OK) {
143 ALOGE("%s: Unable to start frame processor thread: %s (%d)",
144 __FUNCTION__, strerror(-res), res);
145 return res;
146 }
147
148 mFrameProcessor->registerListener(camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MIN_ID,
149 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MAX_ID,
150 /*listener*/this,
151 /*sendPartials*/true);
152
153 const CameraMetadata &deviceInfo = mDevice->info();
154 camera_metadata_ro_entry_t physicalKeysEntry = deviceInfo.find(
155 ANDROID_REQUEST_AVAILABLE_PHYSICAL_CAMERA_REQUEST_KEYS);
156 if (physicalKeysEntry.count > 0) {
157 mSupportedPhysicalRequestKeys.insert(mSupportedPhysicalRequestKeys.begin(),
158 physicalKeysEntry.data.i32,
159 physicalKeysEntry.data.i32 + physicalKeysEntry.count);
160 }
161
162 auto entry = deviceInfo.find(ANDROID_REQUEST_AVAILABLE_CAPABILITIES);
163 mDynamicProfileMap.emplace(
164 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD,
165 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD);
166 if (entry.count > 0) {
167 const auto it = std::find(entry.data.u8, entry.data.u8 + entry.count,
168 ANDROID_REQUEST_AVAILABLE_CAPABILITIES_DYNAMIC_RANGE_TEN_BIT);
169 if (it != entry.data.u8 + entry.count) {
170 entry = deviceInfo.find(ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP);
171 if (entry.count > 0 || ((entry.count % 3) != 0)) {
172 int64_t standardBitmap =
173 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD;
174 for (size_t i = 0; i < entry.count; i += 3) {
175 if (entry.data.i64[i] !=
176 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD) {
177 mDynamicProfileMap.emplace(entry.data.i64[i], entry.data.i64[i+1]);
178 if ((entry.data.i64[i+1] == 0) || (entry.data.i64[i+1] &
179 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD)) {
180 standardBitmap |= entry.data.i64[i];
181 }
182 } else {
183 ALOGE("%s: Device %s includes unexpected profile entry: 0x%" PRIx64 "!",
184 __FUNCTION__, mCameraIdStr.c_str(), entry.data.i64[i]);
185 }
186 }
187 mDynamicProfileMap[ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD] =
188 standardBitmap;
189 } else {
190 ALOGE("%s: Device %s supports 10-bit output but doesn't include a dynamic range"
191 " profile map!", __FUNCTION__, mCameraIdStr.c_str());
192 }
193 }
194 }
195
196 mProviderManager = providerPtr;
197 // Cache physical camera ids corresponding to this device and also the high
198 // resolution sensors in this device + physical camera ids
199 mProviderManager->isLogicalCamera(mCameraIdStr, &mPhysicalCameraIds);
200 if (supportsUltraHighResolutionCapture(mCameraIdStr)) {
201 mHighResolutionSensors.insert(mCameraIdStr);
202 }
203 for (auto &physicalId : mPhysicalCameraIds) {
204 if (supportsUltraHighResolutionCapture(physicalId)) {
205 mHighResolutionSensors.insert(physicalId);
206 }
207 }
208 return OK;
209 }
210
~CameraDeviceClient()211 CameraDeviceClient::~CameraDeviceClient() {
212 }
213
submitRequest(const hardware::camera2::CaptureRequest & request,bool streaming,hardware::camera2::utils::SubmitInfo * submitInfo)214 binder::Status CameraDeviceClient::submitRequest(
215 const hardware::camera2::CaptureRequest& request,
216 bool streaming,
217 /*out*/
218 hardware::camera2::utils::SubmitInfo *submitInfo) {
219 std::vector<hardware::camera2::CaptureRequest> requestList = { request };
220 return submitRequestList(requestList, streaming, submitInfo);
221 }
222
insertGbpLocked(const sp<IGraphicBufferProducer> & gbp,SurfaceMap * outSurfaceMap,Vector<int32_t> * outputStreamIds,int32_t * currentStreamId)223 binder::Status CameraDeviceClient::insertGbpLocked(const sp<IGraphicBufferProducer>& gbp,
224 SurfaceMap* outSurfaceMap, Vector<int32_t>* outputStreamIds, int32_t *currentStreamId) {
225 int compositeIdx;
226 int idx = mStreamMap.indexOfKey(IInterface::asBinder(gbp));
227
228 Mutex::Autolock l(mCompositeLock);
229 // Trying to submit request with surface that wasn't created
230 if (idx == NAME_NOT_FOUND) {
231 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
232 " we have not called createStream on",
233 __FUNCTION__, mCameraIdStr.c_str());
234 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
235 "Request targets Surface that is not part of current capture session");
236 } else if ((compositeIdx = mCompositeStreamMap.indexOfKey(IInterface::asBinder(gbp)))
237 != NAME_NOT_FOUND) {
238 mCompositeStreamMap.valueAt(compositeIdx)->insertGbp(outSurfaceMap, outputStreamIds,
239 currentStreamId);
240 return binder::Status::ok();
241 }
242
243 const StreamSurfaceId& streamSurfaceId = mStreamMap.valueAt(idx);
244 if (outSurfaceMap->find(streamSurfaceId.streamId()) == outSurfaceMap->end()) {
245 outputStreamIds->push_back(streamSurfaceId.streamId());
246 }
247 (*outSurfaceMap)[streamSurfaceId.streamId()].push_back(streamSurfaceId.surfaceId());
248
249 ALOGV("%s: Camera %s: Appending output stream %d surface %d to request",
250 __FUNCTION__, mCameraIdStr.c_str(), streamSurfaceId.streamId(),
251 streamSurfaceId.surfaceId());
252
253 if (currentStreamId != nullptr) {
254 *currentStreamId = streamSurfaceId.streamId();
255 }
256
257 return binder::Status::ok();
258 }
259
getIntersection(const std::unordered_set<int> & streamIdsForThisCamera,const Vector<int> & streamIdsForThisRequest)260 static std::list<int> getIntersection(const std::unordered_set<int> &streamIdsForThisCamera,
261 const Vector<int> &streamIdsForThisRequest) {
262 std::list<int> intersection;
263 for (auto &streamId : streamIdsForThisRequest) {
264 if (streamIdsForThisCamera.find(streamId) != streamIdsForThisCamera.end()) {
265 intersection.emplace_back(streamId);
266 }
267 }
268 return intersection;
269 }
270
submitRequestList(const std::vector<hardware::camera2::CaptureRequest> & requests,bool streaming,hardware::camera2::utils::SubmitInfo * submitInfo)271 binder::Status CameraDeviceClient::submitRequestList(
272 const std::vector<hardware::camera2::CaptureRequest>& requests,
273 bool streaming,
274 /*out*/
275 hardware::camera2::utils::SubmitInfo *submitInfo) {
276 ATRACE_CALL();
277 ALOGV("%s-start of function. Request list size %zu", __FUNCTION__, requests.size());
278
279 binder::Status res = binder::Status::ok();
280 status_t err;
281 if ( !(res = checkPidStatus(__FUNCTION__) ).isOk()) {
282 return res;
283 }
284
285 Mutex::Autolock icl(mBinderSerializationLock);
286
287 if (!mDevice.get()) {
288 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
289 }
290
291 if (requests.empty()) {
292 ALOGE("%s: Camera %s: Sent null request. Rejecting request.",
293 __FUNCTION__, mCameraIdStr.c_str());
294 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Empty request list");
295 }
296
297 List<const CameraDeviceBase::PhysicalCameraSettingsList> metadataRequestList;
298 std::list<const SurfaceMap> surfaceMapList;
299 submitInfo->mRequestId = mRequestIdCounter;
300 uint32_t loopCounter = 0;
301
302 for (auto&& request: requests) {
303 if (request.mIsReprocess) {
304 if (!mInputStream.configured) {
305 ALOGE("%s: Camera %s: no input stream is configured.", __FUNCTION__,
306 mCameraIdStr.c_str());
307 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
308 "No input configured for camera %s but request is for reprocessing",
309 mCameraIdStr.c_str());
310 } else if (streaming) {
311 ALOGE("%s: Camera %s: streaming reprocess requests not supported.", __FUNCTION__,
312 mCameraIdStr.c_str());
313 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
314 "Repeating reprocess requests not supported");
315 } else if (request.mPhysicalCameraSettings.size() > 1) {
316 ALOGE("%s: Camera %s: reprocess requests not supported for "
317 "multiple physical cameras.", __FUNCTION__,
318 mCameraIdStr.c_str());
319 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
320 "Reprocess requests not supported for multiple cameras");
321 }
322 }
323
324 if (request.mPhysicalCameraSettings.empty()) {
325 ALOGE("%s: Camera %s: request doesn't contain any settings.", __FUNCTION__,
326 mCameraIdStr.c_str());
327 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
328 "Request doesn't contain any settings");
329 }
330
331 //The first capture settings should always match the logical camera id
332 const std::string &logicalId = request.mPhysicalCameraSettings.begin()->id;
333 if (mDevice->getId() != logicalId && mOriginalCameraId != logicalId) {
334 ALOGE("%s: Camera %s: Invalid camera request settings.", __FUNCTION__,
335 mCameraIdStr.c_str());
336 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
337 "Invalid camera request settings");
338 }
339
340 if (request.mSurfaceList.isEmpty() && request.mStreamIdxList.size() == 0) {
341 ALOGE("%s: Camera %s: Requests must have at least one surface target. "
342 "Rejecting request.", __FUNCTION__, mCameraIdStr.c_str());
343 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
344 "Request has no output targets");
345 }
346
347 /**
348 * Write in the output stream IDs and map from stream ID to surface ID
349 * which we calculate from the capture request's list of surface target
350 */
351 SurfaceMap surfaceMap;
352 Vector<int32_t> outputStreamIds;
353 std::vector<std::string> requestedPhysicalIds;
354 int64_t dynamicProfileBitmap = 0;
355 if (request.mSurfaceList.size() > 0) {
356 for (const sp<Surface>& surface : request.mSurfaceList) {
357 if (surface == 0) continue;
358
359 int32_t streamId;
360 sp<IGraphicBufferProducer> gbp = surface->getIGraphicBufferProducer();
361 res = insertGbpLocked(gbp, &surfaceMap, &outputStreamIds, &streamId);
362 if (!res.isOk()) {
363 return res;
364 }
365
366 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
367 if (index >= 0) {
368 const std::string &requestedPhysicalId =
369 mConfiguredOutputs.valueAt(index).getPhysicalCameraId();
370 requestedPhysicalIds.push_back(requestedPhysicalId);
371 dynamicProfileBitmap |=
372 mConfiguredOutputs.valueAt(index).getDynamicRangeProfile();
373 } else {
374 ALOGW("%s: Output stream Id not found among configured outputs!", __FUNCTION__);
375 }
376 }
377 } else {
378 for (size_t i = 0; i < request.mStreamIdxList.size(); i++) {
379 int streamId = request.mStreamIdxList.itemAt(i);
380 int surfaceIdx = request.mSurfaceIdxList.itemAt(i);
381
382 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
383 if (index < 0) {
384 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
385 " we have not called createStream on: stream %d",
386 __FUNCTION__, mCameraIdStr.c_str(), streamId);
387 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
388 "Request targets Surface that is not part of current capture session");
389 }
390
391 const auto& gbps = mConfiguredOutputs.valueAt(index).getGraphicBufferProducers();
392 if ((size_t)surfaceIdx >= gbps.size()) {
393 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
394 " we have not called createStream on: stream %d, surfaceIdx %d",
395 __FUNCTION__, mCameraIdStr.c_str(), streamId, surfaceIdx);
396 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
397 "Request targets Surface has invalid surface index");
398 }
399
400 res = insertGbpLocked(gbps[surfaceIdx], &surfaceMap, &outputStreamIds, nullptr);
401 if (!res.isOk()) {
402 return res;
403 }
404
405 const std::string &requestedPhysicalId =
406 mConfiguredOutputs.valueAt(index).getPhysicalCameraId();
407 requestedPhysicalIds.push_back(requestedPhysicalId);
408 dynamicProfileBitmap |=
409 mConfiguredOutputs.valueAt(index).getDynamicRangeProfile();
410 }
411 }
412
413 if (dynamicProfileBitmap !=
414 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD) {
415 for (int i = ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD;
416 i < ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_MAX; i <<= 1) {
417 if ((dynamicProfileBitmap & i) == 0) {
418 continue;
419 }
420
421 const auto& it = mDynamicProfileMap.find(i);
422 if (it != mDynamicProfileMap.end()) {
423 if ((it->second == 0) ||
424 ((it->second & dynamicProfileBitmap) == dynamicProfileBitmap)) {
425 continue;
426 } else {
427 ALOGE("%s: Camera %s: Tried to submit a request with a surfaces that"
428 " reference an unsupported dynamic range profile combination"
429 " 0x%" PRIx64 "!", __FUNCTION__, mCameraIdStr.c_str(),
430 dynamicProfileBitmap);
431 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
432 "Request targets an unsupported dynamic range profile"
433 " combination");
434 }
435 } else {
436 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
437 " references unsupported dynamic range profile 0x%x!",
438 __FUNCTION__, mCameraIdStr.c_str(), i);
439 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
440 "Request targets 10-bit Surface with unsupported dynamic range"
441 " profile");
442 }
443 }
444 }
445
446 CameraDeviceBase::PhysicalCameraSettingsList physicalSettingsList;
447 for (const auto& it : request.mPhysicalCameraSettings) {
448 const std::string resolvedId = (mOriginalCameraId == it.id) ? mDevice->getId() : it.id;
449 if (it.settings.isEmpty()) {
450 ALOGE("%s: Camera %s: Sent empty metadata packet. Rejecting request.",
451 __FUNCTION__, mCameraIdStr.c_str());
452 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
453 "Request settings are empty");
454 }
455
456 // Check whether the physical / logical stream has settings
457 // consistent with the sensor pixel mode(s) it was configured with.
458 // mCameraIdToStreamSet will only have ids that are high resolution
459 const auto streamIdSetIt = mHighResolutionCameraIdToStreamIdSet.find(resolvedId);
460 if (streamIdSetIt != mHighResolutionCameraIdToStreamIdSet.end()) {
461 std::list<int> streamIdsUsedInRequest = getIntersection(streamIdSetIt->second,
462 outputStreamIds);
463 if (!request.mIsReprocess &&
464 !isSensorPixelModeConsistent(streamIdsUsedInRequest, it.settings)) {
465 ALOGE("%s: Camera %s: Request settings CONTROL_SENSOR_PIXEL_MODE not "
466 "consistent with configured streams. Rejecting request.",
467 __FUNCTION__, resolvedId.c_str());
468 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
469 "Request settings CONTROL_SENSOR_PIXEL_MODE are not consistent with "
470 "streams configured");
471 }
472 }
473
474 const std::string &physicalId = resolvedId;
475 bool hasTestPatternModePhysicalKey = std::find(mSupportedPhysicalRequestKeys.begin(),
476 mSupportedPhysicalRequestKeys.end(), ANDROID_SENSOR_TEST_PATTERN_MODE) !=
477 mSupportedPhysicalRequestKeys.end();
478 bool hasTestPatternDataPhysicalKey = std::find(mSupportedPhysicalRequestKeys.begin(),
479 mSupportedPhysicalRequestKeys.end(), ANDROID_SENSOR_TEST_PATTERN_DATA) !=
480 mSupportedPhysicalRequestKeys.end();
481 if (physicalId != mDevice->getId()) {
482 auto found = std::find(requestedPhysicalIds.begin(), requestedPhysicalIds.end(),
483 resolvedId);
484 if (found == requestedPhysicalIds.end()) {
485 ALOGE("%s: Camera %s: Physical camera id: %s not part of attached outputs.",
486 __FUNCTION__, mCameraIdStr.c_str(), physicalId.c_str());
487 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
488 "Invalid physical camera id");
489 }
490
491 if (!mSupportedPhysicalRequestKeys.empty()) {
492 // Filter out any unsupported physical request keys.
493 CameraMetadata filteredParams(mSupportedPhysicalRequestKeys.size());
494 camera_metadata_t *meta = const_cast<camera_metadata_t *>(
495 filteredParams.getAndLock());
496 set_camera_metadata_vendor_id(meta, mDevice->getVendorTagId());
497 filteredParams.unlock(meta);
498
499 for (const auto& keyIt : mSupportedPhysicalRequestKeys) {
500 camera_metadata_ro_entry entry = it.settings.find(keyIt);
501 if (entry.count > 0) {
502 filteredParams.update(entry);
503 }
504 }
505
506 physicalSettingsList.push_back({resolvedId, filteredParams,
507 hasTestPatternModePhysicalKey, hasTestPatternDataPhysicalKey});
508 }
509 } else {
510 physicalSettingsList.push_back({resolvedId, it.settings});
511 }
512 }
513
514 if (!enforceRequestPermissions(physicalSettingsList.begin()->metadata)) {
515 // Callee logs
516 return STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
517 "Caller does not have permission to change restricted controls");
518 }
519
520 physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_OUTPUT_STREAMS,
521 &outputStreamIds[0], outputStreamIds.size());
522
523 if (request.mIsReprocess) {
524 physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_INPUT_STREAMS,
525 &mInputStream.id, 1);
526 }
527
528 physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_ID,
529 &(submitInfo->mRequestId), /*size*/1);
530 loopCounter++; // loopCounter starts from 1
531 ALOGV("%s: Camera %s: Creating request with ID %d (%d of %zu)",
532 __FUNCTION__, mCameraIdStr.c_str(), submitInfo->mRequestId,
533 loopCounter, requests.size());
534
535 metadataRequestList.push_back(physicalSettingsList);
536 surfaceMapList.push_back(surfaceMap);
537
538 // Save certain CaptureRequest settings
539 if (!request.mUserTag.empty()) {
540 mRunningSessionStats.mUserTag = request.mUserTag;
541 }
542 camera_metadata_entry entry =
543 physicalSettingsList.begin()->metadata.find(
544 ANDROID_CONTROL_VIDEO_STABILIZATION_MODE);
545 if (entry.count == 1) {
546 mRunningSessionStats.mVideoStabilizationMode = entry.data.u8[0];
547 }
548
549 if (!mRunningSessionStats.mUsedUltraWide && flags::log_ultrawide_usage()) {
550 entry = physicalSettingsList.begin()->metadata.find(
551 ANDROID_CONTROL_ZOOM_RATIO);
552 if (entry.count == 1 && entry.data.f[0] < 1.0f ) {
553 mRunningSessionStats.mUsedUltraWide = true;
554 }
555 }
556 if (!mRunningSessionStats.mUsedSettingsOverrideZoom && flags::log_zoom_override_usage()) {
557 entry = physicalSettingsList.begin()->metadata.find(
558 ANDROID_CONTROL_SETTINGS_OVERRIDE);
559 if (entry.count == 1 && entry.data.i32[0] ==
560 ANDROID_CONTROL_SETTINGS_OVERRIDE_ZOOM) {
561 mRunningSessionStats.mUsedSettingsOverrideZoom = true;
562 }
563 }
564 }
565 mRequestIdCounter++;
566
567 if (streaming) {
568 err = mDevice->setStreamingRequestList(metadataRequestList, surfaceMapList,
569 &(submitInfo->mLastFrameNumber));
570 if (err != OK) {
571 std::string msg = fmt::sprintf(
572 "Camera %s: Got error %s (%d) after trying to set streaming request",
573 mCameraIdStr.c_str(), strerror(-err), err);
574 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
575 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
576 msg.c_str());
577 } else {
578 Mutex::Autolock idLock(mStreamingRequestIdLock);
579 mStreamingRequestId = submitInfo->mRequestId;
580 }
581 } else {
582 err = mDevice->captureList(metadataRequestList, surfaceMapList,
583 &(submitInfo->mLastFrameNumber));
584 if (err != OK) {
585 std::string msg = fmt::sprintf(
586 "Camera %s: Got error %s (%d) after trying to submit capture request",
587 mCameraIdStr.c_str(), strerror(-err), err);
588 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
589 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
590 msg.c_str());
591 }
592 ALOGV("%s: requestId = %d ", __FUNCTION__, submitInfo->mRequestId);
593 }
594
595 ALOGV("%s: Camera %s: End of function", __FUNCTION__, mCameraIdStr.c_str());
596 return res;
597 }
598
cancelRequest(int requestId,int64_t * lastFrameNumber)599 binder::Status CameraDeviceClient::cancelRequest(
600 int requestId,
601 /*out*/
602 int64_t* lastFrameNumber) {
603 ATRACE_CALL();
604 ALOGV("%s, requestId = %d", __FUNCTION__, requestId);
605
606 status_t err;
607 binder::Status res;
608
609 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
610
611 Mutex::Autolock icl(mBinderSerializationLock);
612
613 if (!mDevice.get()) {
614 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
615 }
616
617 Mutex::Autolock idLock(mStreamingRequestIdLock);
618 if (mStreamingRequestId != requestId) {
619 std::string msg = fmt::sprintf("Camera %s: Canceling request ID %d doesn't match "
620 "current request ID %d", mCameraIdStr.c_str(), requestId, mStreamingRequestId);
621 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
622 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
623 }
624
625 err = mDevice->clearStreamingRequest(lastFrameNumber);
626
627 if (err == OK) {
628 ALOGV("%s: Camera %s: Successfully cleared streaming request",
629 __FUNCTION__, mCameraIdStr.c_str());
630 mStreamingRequestId = REQUEST_ID_NONE;
631 } else {
632 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
633 "Camera %s: Error clearing streaming request: %s (%d)",
634 mCameraIdStr.c_str(), strerror(-err), err);
635 }
636
637 return res;
638 }
639
beginConfigure()640 binder::Status CameraDeviceClient::beginConfigure() {
641 // TODO: Implement this.
642 ATRACE_CALL();
643 ALOGV("%s: Not implemented yet.", __FUNCTION__);
644 return binder::Status::ok();
645 }
646
endConfigure(int operatingMode,const hardware::camera2::impl::CameraMetadataNative & sessionParams,int64_t startTimeMs,std::vector<int> * offlineStreamIds)647 binder::Status CameraDeviceClient::endConfigure(int operatingMode,
648 const hardware::camera2::impl::CameraMetadataNative& sessionParams, int64_t startTimeMs,
649 std::vector<int>* offlineStreamIds /*out*/) {
650 ATRACE_CALL();
651 ALOGV("%s: ending configure (%d input stream, %zu output surfaces)",
652 __FUNCTION__, mInputStream.configured ? 1 : 0,
653 mStreamMap.size());
654
655 binder::Status res;
656 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
657
658 if (offlineStreamIds == nullptr) {
659 std::string msg = "Invalid offline stream ids";
660 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
661 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
662 }
663
664 Mutex::Autolock icl(mBinderSerializationLock);
665
666 if (!mDevice.get()) {
667 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
668 }
669
670 res = SessionConfigurationUtils::checkOperatingMode(operatingMode, mDevice->info(),
671 mCameraIdStr);
672 if (!res.isOk()) {
673 return res;
674 }
675
676 status_t err = mDevice->configureStreams(sessionParams, operatingMode);
677 if (err == BAD_VALUE) {
678 std::string msg = fmt::sprintf("Camera %s: Unsupported set of inputs/outputs provided",
679 mCameraIdStr.c_str());
680 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
681 res = STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
682 } else if (err != OK) {
683 std::string msg = fmt::sprintf("Camera %s: Error configuring streams: %s (%d)",
684 mCameraIdStr.c_str(), strerror(-err), err);
685 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
686 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.c_str());
687 } else {
688 offlineStreamIds->clear();
689 mDevice->getOfflineStreamIds(offlineStreamIds);
690
691 Mutex::Autolock l(mCompositeLock);
692 for (size_t i = 0; i < mCompositeStreamMap.size(); ++i) {
693 err = mCompositeStreamMap.valueAt(i)->configureStream();
694 if (err != OK) {
695 std::string msg = fmt::sprintf("Camera %s: Error configuring composite "
696 "streams: %s (%d)", mCameraIdStr.c_str(), strerror(-err), err);
697 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
698 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.c_str());
699 break;
700 }
701
702 // Composite streams can only support offline mode in case all individual internal
703 // streams are also supported.
704 std::vector<int> internalStreams;
705 mCompositeStreamMap.valueAt(i)->insertCompositeStreamIds(&internalStreams);
706 offlineStreamIds->erase(
707 std::remove_if(offlineStreamIds->begin(), offlineStreamIds->end(),
708 [&internalStreams] (int streamId) {
709 auto it = std::find(internalStreams.begin(), internalStreams.end(),
710 streamId);
711 if (it != internalStreams.end()) {
712 internalStreams.erase(it);
713 return true;
714 }
715
716 return false;}), offlineStreamIds->end());
717 if (internalStreams.empty()) {
718 offlineStreamIds->push_back(mCompositeStreamMap.valueAt(i)->getStreamId());
719 }
720 }
721
722 for (const auto& offlineStreamId : *offlineStreamIds) {
723 mStreamInfoMap[offlineStreamId].supportsOffline = true;
724 }
725
726 nsecs_t configureEnd = systemTime();
727 int32_t configureDurationMs = ns2ms(configureEnd) - startTimeMs;
728 mCameraServiceProxyWrapper->logStreamConfigured(mCameraIdStr, operatingMode,
729 false /*internalReconfig*/, configureDurationMs);
730 }
731
732 return res;
733 }
734
isSessionConfigurationSupported(const SessionConfiguration & sessionConfiguration,bool * status)735 binder::Status CameraDeviceClient::isSessionConfigurationSupported(
736 const SessionConfiguration& sessionConfiguration, bool *status /*out*/) {
737
738 ATRACE_CALL();
739 binder::Status res;
740 status_t ret = OK;
741 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
742
743 Mutex::Autolock icl(mBinderSerializationLock);
744
745 if (!mDevice.get()) {
746 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
747 }
748
749 if (status == nullptr) {
750 std::string msg = fmt::sprintf( "Camera %s: Invalid status!", mCameraIdStr.c_str());
751 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
752 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
753 }
754
755 *status = false;
756 ret = mProviderManager->isSessionConfigurationSupported(mCameraIdStr.c_str(),
757 sessionConfiguration, mOverrideForPerfClass, /*checkSessionParams*/false,
758 status);
759 switch (ret) {
760 case OK:
761 // Expected, do nothing.
762 break;
763 case INVALID_OPERATION: {
764 std::string msg = fmt::sprintf(
765 "Camera %s: Session configuration query not supported!",
766 mCameraIdStr.c_str());
767 ALOGD("%s: %s", __FUNCTION__, msg.c_str());
768 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.c_str());
769 }
770
771 break;
772 default: {
773 std::string msg = fmt::sprintf( "Camera %s: Error: %s (%d)", mCameraIdStr.c_str(),
774 strerror(-ret), ret);
775 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
776 res = STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
777 msg.c_str());
778 }
779 }
780
781 return res;
782 }
783
deleteStream(int streamId)784 binder::Status CameraDeviceClient::deleteStream(int streamId) {
785 ATRACE_CALL();
786 ALOGV("%s (streamId = 0x%x)", __FUNCTION__, streamId);
787
788 binder::Status res;
789 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
790
791 Mutex::Autolock icl(mBinderSerializationLock);
792
793 if (!mDevice.get()) {
794 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
795 }
796
797 bool isInput = false;
798 std::vector<sp<IBinder>> surfaces;
799 ssize_t dIndex = NAME_NOT_FOUND;
800 ssize_t compositeIndex = NAME_NOT_FOUND;
801
802 if (mInputStream.configured && mInputStream.id == streamId) {
803 isInput = true;
804 } else {
805 // Guard against trying to delete non-created streams
806 for (size_t i = 0; i < mStreamMap.size(); ++i) {
807 if (streamId == mStreamMap.valueAt(i).streamId()) {
808 surfaces.push_back(mStreamMap.keyAt(i));
809 }
810 }
811
812 // See if this stream is one of the deferred streams.
813 for (size_t i = 0; i < mDeferredStreams.size(); ++i) {
814 if (streamId == mDeferredStreams[i]) {
815 dIndex = i;
816 break;
817 }
818 }
819
820 Mutex::Autolock l(mCompositeLock);
821 for (size_t i = 0; i < mCompositeStreamMap.size(); ++i) {
822 if (streamId == mCompositeStreamMap.valueAt(i)->getStreamId()) {
823 compositeIndex = i;
824 break;
825 }
826 }
827
828 if (surfaces.empty() && dIndex == NAME_NOT_FOUND) {
829 std::string msg = fmt::sprintf("Camera %s: Invalid stream ID (%d) specified, no such"
830 " stream created yet", mCameraIdStr.c_str(), streamId);
831 ALOGW("%s: %s", __FUNCTION__, msg.c_str());
832 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
833 }
834 }
835
836 // Also returns BAD_VALUE if stream ID was not valid
837 status_t err = mDevice->deleteStream(streamId);
838
839 if (err != OK) {
840 std::string msg = fmt::sprintf("Camera %s: Unexpected error %s (%d) when deleting stream "
841 "%d", mCameraIdStr.c_str(), strerror(-err), err, streamId);
842 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
843 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.c_str());
844 } else {
845 if (isInput) {
846 mInputStream.configured = false;
847 } else {
848 for (auto& surface : surfaces) {
849 mStreamMap.removeItem(surface);
850 }
851
852 mConfiguredOutputs.removeItem(streamId);
853
854 if (dIndex != NAME_NOT_FOUND) {
855 mDeferredStreams.removeItemsAt(dIndex);
856 }
857
858 if (compositeIndex != NAME_NOT_FOUND) {
859 Mutex::Autolock l(mCompositeLock);
860 status_t ret;
861 if ((ret = mCompositeStreamMap.valueAt(compositeIndex)->deleteStream())
862 != OK) {
863 std::string msg = fmt::sprintf("Camera %s: Unexpected error %s (%d) when "
864 "deleting composite stream %d", mCameraIdStr.c_str(), strerror(-err),
865 err, streamId);
866 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
867 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.c_str());
868 }
869 mCompositeStreamMap.removeItemsAt(compositeIndex);
870 }
871 for (auto &mapIt: mHighResolutionCameraIdToStreamIdSet) {
872 auto &streamSet = mapIt.second;
873 if (streamSet.find(streamId) != streamSet.end()) {
874 streamSet.erase(streamId);
875 break;
876 }
877 }
878 }
879 }
880
881 return res;
882 }
883
createStream(const hardware::camera2::params::OutputConfiguration & outputConfiguration,int32_t * newStreamId)884 binder::Status CameraDeviceClient::createStream(
885 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
886 /*out*/
887 int32_t* newStreamId) {
888 ATRACE_CALL();
889
890 binder::Status res;
891 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
892
893 Mutex::Autolock icl(mBinderSerializationLock);
894
895 if (!outputConfiguration.isComplete()) {
896 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
897 "OutputConfiguration isn't valid!");
898 }
899
900 const std::vector<sp<IGraphicBufferProducer>>& bufferProducers =
901 outputConfiguration.getGraphicBufferProducers();
902 size_t numBufferProducers = bufferProducers.size();
903 bool deferredConsumer = outputConfiguration.isDeferred();
904 bool isShared = outputConfiguration.isShared();
905 const std::string &physicalCameraId = outputConfiguration.getPhysicalCameraId();
906 bool deferredConsumerOnly = deferredConsumer && numBufferProducers == 0;
907 bool isMultiResolution = outputConfiguration.isMultiResolution();
908 int64_t dynamicRangeProfile = outputConfiguration.getDynamicRangeProfile();
909 int64_t streamUseCase = outputConfiguration.getStreamUseCase();
910 int timestampBase = outputConfiguration.getTimestampBase();
911 int mirrorMode = outputConfiguration.getMirrorMode();
912 int32_t colorSpace = outputConfiguration.getColorSpace();
913 bool useReadoutTimestamp = outputConfiguration.useReadoutTimestamp();
914
915 res = SessionConfigurationUtils::checkSurfaceType(numBufferProducers, deferredConsumer,
916 outputConfiguration.getSurfaceType(), /*isConfigurationComplete*/true);
917 if (!res.isOk()) {
918 return res;
919 }
920
921 if (!mDevice.get()) {
922 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
923 }
924 res = SessionConfigurationUtils::checkPhysicalCameraId(mPhysicalCameraIds,
925 physicalCameraId, mCameraIdStr);
926 if (!res.isOk()) {
927 return res;
928 }
929
930 std::vector<sp<Surface>> surfaces;
931 std::vector<sp<IBinder>> binders;
932 status_t err;
933
934 // Create stream for deferred surface case.
935 if (deferredConsumerOnly) {
936 return createDeferredSurfaceStreamLocked(outputConfiguration, isShared, newStreamId);
937 }
938
939 OutputStreamInfo streamInfo;
940 bool isStreamInfoValid = false;
941 const std::vector<int32_t> &sensorPixelModesUsed =
942 outputConfiguration.getSensorPixelModesUsed();
943 for (auto& bufferProducer : bufferProducers) {
944 // Don't create multiple streams for the same target surface
945 sp<IBinder> binder = IInterface::asBinder(bufferProducer);
946 ssize_t index = mStreamMap.indexOfKey(binder);
947 if (index != NAME_NOT_FOUND) {
948 std::string msg = std::string("Camera ") + mCameraIdStr
949 + ": Surface already has a stream created for it (ID "
950 + std::to_string(index) + ")";
951 ALOGW("%s: %s", __FUNCTION__, msg.c_str());
952 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.c_str());
953 }
954
955 sp<Surface> surface;
956 res = SessionConfigurationUtils::createSurfaceFromGbp(streamInfo,
957 isStreamInfoValid, surface, bufferProducer, mCameraIdStr,
958 mDevice->infoPhysical(physicalCameraId), sensorPixelModesUsed, dynamicRangeProfile,
959 streamUseCase, timestampBase, mirrorMode, colorSpace, /*respectSurfaceSize*/false);
960
961 if (!res.isOk())
962 return res;
963
964 if (!isStreamInfoValid) {
965 isStreamInfoValid = true;
966 }
967
968 binders.push_back(IInterface::asBinder(bufferProducer));
969 surfaces.push_back(surface);
970 }
971
972 // If mOverrideForPerfClass is true, do not fail createStream() for small
973 // JPEG sizes because existing createSurfaceFromGbp() logic will find the
974 // closest possible supported size.
975
976 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
977 std::vector<int> surfaceIds;
978 bool isDepthCompositeStream =
979 camera3::DepthCompositeStream::isDepthCompositeStream(surfaces[0]);
980 bool isHeicCompositeStream = camera3::HeicCompositeStream::isHeicCompositeStream(surfaces[0]);
981 bool isJpegRCompositeStream =
982 camera3::JpegRCompositeStream::isJpegRCompositeStream(surfaces[0]) &&
983 !mDevice->isCompositeJpegRDisabled();
984 if (isDepthCompositeStream || isHeicCompositeStream || isJpegRCompositeStream) {
985 sp<CompositeStream> compositeStream;
986 if (isDepthCompositeStream) {
987 compositeStream = new camera3::DepthCompositeStream(mDevice, getRemoteCallback());
988 } else if (isHeicCompositeStream) {
989 compositeStream = new camera3::HeicCompositeStream(mDevice, getRemoteCallback());
990 } else {
991 compositeStream = new camera3::JpegRCompositeStream(mDevice, getRemoteCallback());
992 }
993
994 err = compositeStream->createStream(surfaces, deferredConsumer, streamInfo.width,
995 streamInfo.height, streamInfo.format,
996 static_cast<camera_stream_rotation_t>(outputConfiguration.getRotation()),
997 &streamId, physicalCameraId, streamInfo.sensorPixelModesUsed, &surfaceIds,
998 outputConfiguration.getSurfaceSetID(), isShared, isMultiResolution,
999 streamInfo.colorSpace, streamInfo.dynamicRangeProfile, streamInfo.streamUseCase,
1000 useReadoutTimestamp);
1001 if (err == OK) {
1002 Mutex::Autolock l(mCompositeLock);
1003 mCompositeStreamMap.add(IInterface::asBinder(surfaces[0]->getIGraphicBufferProducer()),
1004 compositeStream);
1005 }
1006 } else {
1007 err = mDevice->createStream(surfaces, deferredConsumer, streamInfo.width,
1008 streamInfo.height, streamInfo.format, streamInfo.dataSpace,
1009 static_cast<camera_stream_rotation_t>(outputConfiguration.getRotation()),
1010 &streamId, physicalCameraId, streamInfo.sensorPixelModesUsed, &surfaceIds,
1011 outputConfiguration.getSurfaceSetID(), isShared, isMultiResolution,
1012 /*consumerUsage*/0, streamInfo.dynamicRangeProfile, streamInfo.streamUseCase,
1013 streamInfo.timestampBase, streamInfo.mirrorMode, streamInfo.colorSpace,
1014 useReadoutTimestamp);
1015 }
1016
1017 if (err != OK) {
1018 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1019 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
1020 mCameraIdStr.c_str(), streamInfo.width, streamInfo.height, streamInfo.format,
1021 static_cast<int>(streamInfo.dataSpace), strerror(-err), err);
1022 } else {
1023 int i = 0;
1024 for (auto& binder : binders) {
1025 ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %d",
1026 __FUNCTION__, binder.get(), streamId, i);
1027 mStreamMap.add(binder, StreamSurfaceId(streamId, surfaceIds[i]));
1028 i++;
1029 }
1030
1031 mConfiguredOutputs.add(streamId, outputConfiguration);
1032 mStreamInfoMap[streamId] = streamInfo;
1033
1034 ALOGV("%s: Camera %s: Successfully created a new stream ID %d for output surface"
1035 " (%d x %d) with format 0x%x.",
1036 __FUNCTION__, mCameraIdStr.c_str(), streamId, streamInfo.width,
1037 streamInfo.height, streamInfo.format);
1038
1039 // Set transform flags to ensure preview to be rotated correctly.
1040 res = setStreamTransformLocked(streamId, streamInfo.mirrorMode);
1041
1042 // Fill in mHighResolutionCameraIdToStreamIdSet map
1043 const std::string &cameraIdUsed =
1044 physicalCameraId.size() != 0 ? physicalCameraId : mCameraIdStr;
1045 // Only needed for high resolution sensors
1046 if (mHighResolutionSensors.find(cameraIdUsed) !=
1047 mHighResolutionSensors.end()) {
1048 mHighResolutionCameraIdToStreamIdSet[cameraIdUsed].insert(streamId);
1049 }
1050
1051 *newStreamId = streamId;
1052 }
1053
1054 return res;
1055 }
1056
createDeferredSurfaceStreamLocked(const hardware::camera2::params::OutputConfiguration & outputConfiguration,bool isShared,int * newStreamId)1057 binder::Status CameraDeviceClient::createDeferredSurfaceStreamLocked(
1058 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
1059 bool isShared,
1060 /*out*/
1061 int* newStreamId) {
1062 int width, height, format, surfaceType;
1063 uint64_t consumerUsage;
1064 android_dataspace dataSpace;
1065 int32_t colorSpace;
1066 status_t err;
1067 binder::Status res;
1068
1069 if (!mDevice.get()) {
1070 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1071 }
1072 if (!outputConfiguration.isComplete()) {
1073 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1074 "OutputConfiguration isn't valid!");
1075 }
1076
1077 // Infer the surface info for deferred surface stream creation.
1078 width = outputConfiguration.getWidth();
1079 height = outputConfiguration.getHeight();
1080 surfaceType = outputConfiguration.getSurfaceType();
1081 format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
1082 dataSpace = android_dataspace_t::HAL_DATASPACE_UNKNOWN;
1083 colorSpace = ANDROID_REQUEST_AVAILABLE_COLOR_SPACE_PROFILES_MAP_UNSPECIFIED;
1084 // Hardcode consumer usage flags: SurfaceView--0x900, SurfaceTexture--0x100.
1085 consumerUsage = GraphicBuffer::USAGE_HW_TEXTURE;
1086 if (surfaceType == OutputConfiguration::SURFACE_TYPE_SURFACE_VIEW) {
1087 consumerUsage |= GraphicBuffer::USAGE_HW_COMPOSER;
1088 }
1089 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
1090 std::vector<sp<Surface>> noSurface;
1091 std::vector<int> surfaceIds;
1092 const std::string &physicalCameraId = outputConfiguration.getPhysicalCameraId();
1093 const std::string &cameraIdUsed =
1094 physicalCameraId.size() != 0 ? physicalCameraId : mCameraIdStr;
1095 // Here, we override sensor pixel modes
1096 std::unordered_set<int32_t> overriddenSensorPixelModesUsed;
1097 const std::vector<int32_t> &sensorPixelModesUsed =
1098 outputConfiguration.getSensorPixelModesUsed();
1099 if (SessionConfigurationUtils::checkAndOverrideSensorPixelModesUsed(
1100 sensorPixelModesUsed, format, width, height, getStaticInfo(cameraIdUsed),
1101 &overriddenSensorPixelModesUsed) != OK) {
1102 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1103 "sensor pixel modes used not valid for deferred stream");
1104 }
1105
1106 err = mDevice->createStream(noSurface, /*hasDeferredConsumer*/true, width,
1107 height, format, dataSpace,
1108 static_cast<camera_stream_rotation_t>(outputConfiguration.getRotation()),
1109 &streamId, physicalCameraId,
1110 overriddenSensorPixelModesUsed,
1111 &surfaceIds,
1112 outputConfiguration.getSurfaceSetID(), isShared,
1113 outputConfiguration.isMultiResolution(), consumerUsage,
1114 outputConfiguration.getDynamicRangeProfile(),
1115 outputConfiguration.getStreamUseCase(),
1116 outputConfiguration.getMirrorMode(),
1117 outputConfiguration.useReadoutTimestamp());
1118
1119 if (err != OK) {
1120 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1121 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
1122 mCameraIdStr.c_str(), width, height, format, static_cast<int>(dataSpace),
1123 strerror(-err), err);
1124 } else {
1125 // Can not add streamId to mStreamMap here, as the surface is deferred. Add it to
1126 // a separate list to track. Once the deferred surface is set, this id will be
1127 // relocated to mStreamMap.
1128 mDeferredStreams.push_back(streamId);
1129 mStreamInfoMap.emplace(std::piecewise_construct, std::forward_as_tuple(streamId),
1130 std::forward_as_tuple(width, height, format, dataSpace, consumerUsage,
1131 overriddenSensorPixelModesUsed,
1132 outputConfiguration.getDynamicRangeProfile(),
1133 outputConfiguration.getStreamUseCase(),
1134 outputConfiguration.getTimestampBase(),
1135 outputConfiguration.getMirrorMode(),
1136 colorSpace));
1137
1138 ALOGV("%s: Camera %s: Successfully created a new stream ID %d for a deferred surface"
1139 " (%d x %d) stream with format 0x%x.",
1140 __FUNCTION__, mCameraIdStr.c_str(), streamId, width, height, format);
1141
1142 // Set transform flags to ensure preview to be rotated correctly.
1143 res = setStreamTransformLocked(streamId, outputConfiguration.getMirrorMode());
1144
1145 *newStreamId = streamId;
1146 // Fill in mHighResolutionCameraIdToStreamIdSet
1147 // Only needed for high resolution sensors
1148 if (mHighResolutionSensors.find(cameraIdUsed) !=
1149 mHighResolutionSensors.end()) {
1150 mHighResolutionCameraIdToStreamIdSet[cameraIdUsed].insert(streamId);
1151 }
1152 }
1153 return res;
1154 }
1155
setStreamTransformLocked(int streamId,int mirrorMode)1156 binder::Status CameraDeviceClient::setStreamTransformLocked(int streamId, int mirrorMode) {
1157 int32_t transform = 0;
1158 status_t err;
1159 binder::Status res;
1160
1161 if (!mDevice.get()) {
1162 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1163 }
1164
1165 err = getRotationTransformLocked(mirrorMode, &transform);
1166 if (err != OK) {
1167 // Error logged by getRotationTransformLocked.
1168 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
1169 "Unable to calculate rotation transform for new stream");
1170 }
1171
1172 err = mDevice->setStreamTransform(streamId, transform);
1173 if (err != OK) {
1174 std::string msg = fmt::sprintf("Failed to set stream transform (stream id %d)",
1175 streamId);
1176 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
1177 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.c_str());
1178 }
1179
1180 return res;
1181 }
1182
createInputStream(int width,int height,int format,bool isMultiResolution,int32_t * newStreamId)1183 binder::Status CameraDeviceClient::createInputStream(
1184 int width, int height, int format, bool isMultiResolution,
1185 /*out*/
1186 int32_t* newStreamId) {
1187
1188 ATRACE_CALL();
1189 ALOGV("%s (w = %d, h = %d, f = 0x%x, isMultiResolution %d)", __FUNCTION__,
1190 width, height, format, isMultiResolution);
1191
1192 binder::Status res;
1193 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1194
1195 Mutex::Autolock icl(mBinderSerializationLock);
1196
1197 if (!mDevice.get()) {
1198 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1199 }
1200
1201 if (mInputStream.configured) {
1202 std::string msg = fmt::sprintf("Camera %s: Already has an input stream "
1203 "configured (ID %d)", mCameraIdStr.c_str(), mInputStream.id);
1204 ALOGE("%s: %s", __FUNCTION__, msg.c_str() );
1205 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.c_str());
1206 }
1207
1208 int streamId = -1;
1209 status_t err = mDevice->createInputStream(width, height, format, isMultiResolution, &streamId);
1210 if (err == OK) {
1211 mInputStream.configured = true;
1212 mInputStream.width = width;
1213 mInputStream.height = height;
1214 mInputStream.format = format;
1215 mInputStream.id = streamId;
1216
1217 ALOGV("%s: Camera %s: Successfully created a new input stream ID %d",
1218 __FUNCTION__, mCameraIdStr.c_str(), streamId);
1219
1220 *newStreamId = streamId;
1221 } else {
1222 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1223 "Camera %s: Error creating new input stream: %s (%d)", mCameraIdStr.c_str(),
1224 strerror(-err), err);
1225 }
1226
1227 return res;
1228 }
1229
getInputSurface(view::Surface * inputSurface)1230 binder::Status CameraDeviceClient::getInputSurface(/*out*/ view::Surface *inputSurface) {
1231
1232 binder::Status res;
1233 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1234
1235 if (inputSurface == NULL) {
1236 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Null input surface");
1237 }
1238
1239 Mutex::Autolock icl(mBinderSerializationLock);
1240 if (!mDevice.get()) {
1241 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1242 }
1243 sp<IGraphicBufferProducer> producer;
1244 status_t err = mDevice->getInputBufferProducer(&producer);
1245 if (err != OK) {
1246 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1247 "Camera %s: Error getting input Surface: %s (%d)",
1248 mCameraIdStr.c_str(), strerror(-err), err);
1249 } else {
1250 inputSurface->name = toString16("CameraInput");
1251 inputSurface->graphicBufferProducer = producer;
1252 }
1253 return res;
1254 }
1255
updateOutputConfiguration(int streamId,const hardware::camera2::params::OutputConfiguration & outputConfiguration)1256 binder::Status CameraDeviceClient::updateOutputConfiguration(int streamId,
1257 const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
1258 ATRACE_CALL();
1259
1260 binder::Status res;
1261 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1262
1263 Mutex::Autolock icl(mBinderSerializationLock);
1264
1265 if (!mDevice.get()) {
1266 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1267 }
1268 if (!outputConfiguration.isComplete()) {
1269 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1270 "OutputConfiguration isn't valid!");
1271 }
1272
1273 const std::vector<sp<IGraphicBufferProducer> >& bufferProducers =
1274 outputConfiguration.getGraphicBufferProducers();
1275 const std::string &physicalCameraId = outputConfiguration.getPhysicalCameraId();
1276
1277 auto producerCount = bufferProducers.size();
1278 if (producerCount == 0) {
1279 ALOGE("%s: bufferProducers must not be empty", __FUNCTION__);
1280 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1281 "bufferProducers must not be empty");
1282 }
1283
1284 // The first output is the one associated with the output configuration.
1285 // It should always be present, valid and the corresponding stream id should match.
1286 sp<IBinder> binder = IInterface::asBinder(bufferProducers[0]);
1287 ssize_t index = mStreamMap.indexOfKey(binder);
1288 if (index == NAME_NOT_FOUND) {
1289 ALOGE("%s: Outputconfiguration is invalid", __FUNCTION__);
1290 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1291 "OutputConfiguration is invalid");
1292 }
1293 if (mStreamMap.valueFor(binder).streamId() != streamId) {
1294 ALOGE("%s: Stream Id: %d provided doesn't match the id: %d in the stream map",
1295 __FUNCTION__, streamId, mStreamMap.valueFor(binder).streamId());
1296 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1297 "Stream id is invalid");
1298 }
1299
1300 std::vector<size_t> removedSurfaceIds;
1301 std::vector<sp<IBinder>> removedOutputs;
1302 std::vector<sp<Surface>> newOutputs;
1303 std::vector<OutputStreamInfo> streamInfos;
1304 KeyedVector<sp<IBinder>, sp<IGraphicBufferProducer>> newOutputsMap;
1305 for (auto &it : bufferProducers) {
1306 newOutputsMap.add(IInterface::asBinder(it), it);
1307 }
1308
1309 for (size_t i = 0; i < mStreamMap.size(); i++) {
1310 ssize_t idx = newOutputsMap.indexOfKey(mStreamMap.keyAt(i));
1311 if (idx == NAME_NOT_FOUND) {
1312 if (mStreamMap[i].streamId() == streamId) {
1313 removedSurfaceIds.push_back(mStreamMap[i].surfaceId());
1314 removedOutputs.push_back(mStreamMap.keyAt(i));
1315 }
1316 } else {
1317 if (mStreamMap[i].streamId() != streamId) {
1318 ALOGE("%s: Output surface already part of a different stream", __FUNCTION__);
1319 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1320 "Target Surface is invalid");
1321 }
1322 newOutputsMap.removeItemsAt(idx);
1323 }
1324 }
1325 const std::vector<int32_t> &sensorPixelModesUsed =
1326 outputConfiguration.getSensorPixelModesUsed();
1327 int64_t streamUseCase = outputConfiguration.getStreamUseCase();
1328 int timestampBase = outputConfiguration.getTimestampBase();
1329 int64_t dynamicRangeProfile = outputConfiguration.getDynamicRangeProfile();
1330 int32_t colorSpace = outputConfiguration.getColorSpace();
1331 int mirrorMode = outputConfiguration.getMirrorMode();
1332
1333 for (size_t i = 0; i < newOutputsMap.size(); i++) {
1334 OutputStreamInfo outInfo;
1335 sp<Surface> surface;
1336 res = SessionConfigurationUtils::createSurfaceFromGbp(outInfo,
1337 /*isStreamInfoValid*/ false, surface, newOutputsMap.valueAt(i), mCameraIdStr,
1338 mDevice->infoPhysical(physicalCameraId), sensorPixelModesUsed, dynamicRangeProfile,
1339 streamUseCase, timestampBase, mirrorMode, colorSpace, /*respectSurfaceSize*/false);
1340 if (!res.isOk())
1341 return res;
1342
1343 streamInfos.push_back(outInfo);
1344 newOutputs.push_back(surface);
1345 }
1346
1347 //Trivial case no changes required
1348 if (removedSurfaceIds.empty() && newOutputs.empty()) {
1349 return binder::Status::ok();
1350 }
1351
1352 KeyedVector<sp<Surface>, size_t> outputMap;
1353 auto ret = mDevice->updateStream(streamId, newOutputs, streamInfos, removedSurfaceIds,
1354 &outputMap);
1355 if (ret != OK) {
1356 switch (ret) {
1357 case NAME_NOT_FOUND:
1358 case BAD_VALUE:
1359 case -EBUSY:
1360 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1361 "Camera %s: Error updating stream: %s (%d)",
1362 mCameraIdStr.c_str(), strerror(ret), ret);
1363 break;
1364 default:
1365 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1366 "Camera %s: Error updating stream: %s (%d)",
1367 mCameraIdStr.c_str(), strerror(ret), ret);
1368 break;
1369 }
1370 } else {
1371 for (const auto &it : removedOutputs) {
1372 mStreamMap.removeItem(it);
1373 }
1374
1375 for (size_t i = 0; i < outputMap.size(); i++) {
1376 mStreamMap.add(IInterface::asBinder(outputMap.keyAt(i)->getIGraphicBufferProducer()),
1377 StreamSurfaceId(streamId, outputMap.valueAt(i)));
1378 }
1379
1380 mConfiguredOutputs.replaceValueFor(streamId, outputConfiguration);
1381
1382 ALOGV("%s: Camera %s: Successful stream ID %d update",
1383 __FUNCTION__, mCameraIdStr.c_str(), streamId);
1384 }
1385
1386 return res;
1387 }
1388
1389 // Create a request object from a template.
createDefaultRequest(int templateId,hardware::camera2::impl::CameraMetadataNative * request)1390 binder::Status CameraDeviceClient::createDefaultRequest(int templateId,
1391 /*out*/
1392 hardware::camera2::impl::CameraMetadataNative* request)
1393 {
1394 ATRACE_CALL();
1395 ALOGV("%s (templateId = 0x%x)", __FUNCTION__, templateId);
1396
1397 binder::Status res;
1398 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1399
1400 Mutex::Autolock icl(mBinderSerializationLock);
1401
1402 if (!mDevice.get()) {
1403 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1404 }
1405
1406 status_t err;
1407 camera_request_template_t tempId = camera_request_template_t::CAMERA_TEMPLATE_COUNT;
1408 res = SessionConfigurationUtils::mapRequestTemplateFromClient(
1409 mCameraIdStr, templateId, &tempId);
1410 if (!res.isOk()) return res;
1411
1412 CameraMetadata metadata;
1413 if ( (err = mDevice->createDefaultRequest(tempId, &metadata) ) == OK &&
1414 request != NULL) {
1415
1416 request->swap(metadata);
1417 } else if (err == BAD_VALUE) {
1418 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1419 "Camera %s: Template ID %d is invalid or not supported: %s (%d)",
1420 mCameraIdStr.c_str(), templateId, strerror(-err), err);
1421
1422 } else {
1423 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1424 "Camera %s: Error creating default request for template %d: %s (%d)",
1425 mCameraIdStr.c_str(), templateId, strerror(-err), err);
1426 }
1427 return res;
1428 }
1429
getCameraInfo(hardware::camera2::impl::CameraMetadataNative * info)1430 binder::Status CameraDeviceClient::getCameraInfo(
1431 /*out*/
1432 hardware::camera2::impl::CameraMetadataNative* info)
1433 {
1434 ATRACE_CALL();
1435 ALOGV("%s", __FUNCTION__);
1436
1437 binder::Status res;
1438
1439 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1440
1441 Mutex::Autolock icl(mBinderSerializationLock);
1442
1443 if (!mDevice.get()) {
1444 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1445 }
1446
1447 if (info != NULL) {
1448 *info = mDevice->info(); // static camera metadata
1449 // TODO: merge with device-specific camera metadata
1450 }
1451
1452 return res;
1453 }
1454
waitUntilIdle()1455 binder::Status CameraDeviceClient::waitUntilIdle()
1456 {
1457 ATRACE_CALL();
1458 ALOGV("%s", __FUNCTION__);
1459
1460 binder::Status res;
1461 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1462
1463 Mutex::Autolock icl(mBinderSerializationLock);
1464
1465 if (!mDevice.get()) {
1466 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1467 }
1468
1469 // FIXME: Also need check repeating burst.
1470 Mutex::Autolock idLock(mStreamingRequestIdLock);
1471 if (mStreamingRequestId != REQUEST_ID_NONE) {
1472 std::string msg = fmt::sprintf(
1473 "Camera %s: Try to waitUntilIdle when there are active streaming requests",
1474 mCameraIdStr.c_str());
1475 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
1476 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.c_str());
1477 }
1478 status_t err = mDevice->waitUntilDrained();
1479 if (err != OK) {
1480 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1481 "Camera %s: Error waiting to drain: %s (%d)",
1482 mCameraIdStr.c_str(), strerror(-err), err);
1483 }
1484 ALOGV("%s Done", __FUNCTION__);
1485 return res;
1486 }
1487
flush(int64_t * lastFrameNumber)1488 binder::Status CameraDeviceClient::flush(
1489 /*out*/
1490 int64_t* lastFrameNumber) {
1491 ATRACE_CALL();
1492 ALOGV("%s", __FUNCTION__);
1493
1494 binder::Status res;
1495 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1496
1497 Mutex::Autolock icl(mBinderSerializationLock);
1498
1499 if (!mDevice.get()) {
1500 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1501 }
1502
1503 Mutex::Autolock idLock(mStreamingRequestIdLock);
1504 mStreamingRequestId = REQUEST_ID_NONE;
1505 status_t err = mDevice->flush(lastFrameNumber);
1506 if (err != OK) {
1507 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1508 "Camera %s: Error flushing device: %s (%d)", mCameraIdStr.c_str(), strerror(-err),
1509 err);
1510 }
1511 return res;
1512 }
1513
prepare(int streamId)1514 binder::Status CameraDeviceClient::prepare(int streamId) {
1515 ATRACE_CALL();
1516 ALOGV("%s stream id %d", __FUNCTION__, streamId);
1517
1518 binder::Status res;
1519 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1520
1521 Mutex::Autolock icl(mBinderSerializationLock);
1522
1523 // Guard against trying to prepare non-created streams
1524 ssize_t index = NAME_NOT_FOUND;
1525 for (size_t i = 0; i < mStreamMap.size(); ++i) {
1526 if (streamId == mStreamMap.valueAt(i).streamId()) {
1527 index = i;
1528 break;
1529 }
1530 }
1531
1532 if (index == NAME_NOT_FOUND) {
1533 std::string msg = fmt::sprintf("Camera %s: Invalid stream ID (%d) specified, no stream "
1534 "with that ID exists", mCameraIdStr.c_str(), streamId);
1535 ALOGW("%s: %s", __FUNCTION__, msg.c_str());
1536 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
1537 }
1538
1539 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1540 // has been used
1541 status_t err = mDevice->prepare(streamId);
1542 if (err == BAD_VALUE) {
1543 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1544 "Camera %s: Stream %d has already been used, and cannot be prepared",
1545 mCameraIdStr.c_str(), streamId);
1546 } else if (err != OK) {
1547 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1548 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.c_str(), streamId,
1549 strerror(-err), err);
1550 }
1551 return res;
1552 }
1553
prepare2(int maxCount,int streamId)1554 binder::Status CameraDeviceClient::prepare2(int maxCount, int streamId) {
1555 ATRACE_CALL();
1556 ALOGV("%s stream id %d", __FUNCTION__, streamId);
1557
1558 binder::Status res;
1559 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1560
1561 Mutex::Autolock icl(mBinderSerializationLock);
1562
1563 // Guard against trying to prepare non-created streams
1564 ssize_t index = NAME_NOT_FOUND;
1565 for (size_t i = 0; i < mStreamMap.size(); ++i) {
1566 if (streamId == mStreamMap.valueAt(i).streamId()) {
1567 index = i;
1568 break;
1569 }
1570 }
1571
1572 if (index == NAME_NOT_FOUND) {
1573 std::string msg = fmt::sprintf("Camera %s: Invalid stream ID (%d) specified, no stream "
1574 "with that ID exists", mCameraIdStr.c_str(), streamId);
1575 ALOGW("%s: %s", __FUNCTION__, msg.c_str());
1576 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
1577 }
1578
1579 if (maxCount <= 0) {
1580 std::string msg = fmt::sprintf("Camera %s: maxCount (%d) must be greater than 0",
1581 mCameraIdStr.c_str(), maxCount);
1582 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
1583 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
1584 }
1585
1586 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1587 // has been used
1588 status_t err = mDevice->prepare(maxCount, streamId);
1589 if (err == BAD_VALUE) {
1590 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1591 "Camera %s: Stream %d has already been used, and cannot be prepared",
1592 mCameraIdStr.c_str(), streamId);
1593 } else if (err != OK) {
1594 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1595 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.c_str(), streamId,
1596 strerror(-err), err);
1597 }
1598
1599 return res;
1600 }
1601
tearDown(int streamId)1602 binder::Status CameraDeviceClient::tearDown(int streamId) {
1603 ATRACE_CALL();
1604 ALOGV("%s", __FUNCTION__);
1605
1606 binder::Status res;
1607 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1608
1609 Mutex::Autolock icl(mBinderSerializationLock);
1610
1611 // Guard against trying to prepare non-created streams
1612 ssize_t index = NAME_NOT_FOUND;
1613 for (size_t i = 0; i < mStreamMap.size(); ++i) {
1614 if (streamId == mStreamMap.valueAt(i).streamId()) {
1615 index = i;
1616 break;
1617 }
1618 }
1619
1620 if (index == NAME_NOT_FOUND) {
1621 std::string msg = fmt::sprintf("Camera %s: Invalid stream ID (%d) specified, no stream "
1622 "with that ID exists", mCameraIdStr.c_str(), streamId);
1623 ALOGW("%s: %s", __FUNCTION__, msg.c_str());
1624 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
1625 }
1626
1627 // Also returns BAD_VALUE if stream ID was not valid or if the stream is in
1628 // use
1629 status_t err = mDevice->tearDown(streamId);
1630 if (err == BAD_VALUE) {
1631 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1632 "Camera %s: Stream %d is still in use, cannot be torn down",
1633 mCameraIdStr.c_str(), streamId);
1634 } else if (err != OK) {
1635 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1636 "Camera %s: Error tearing down stream %d: %s (%d)", mCameraIdStr.c_str(), streamId,
1637 strerror(-err), err);
1638 }
1639
1640 return res;
1641 }
1642
finalizeOutputConfigurations(int32_t streamId,const hardware::camera2::params::OutputConfiguration & outputConfiguration)1643 binder::Status CameraDeviceClient::finalizeOutputConfigurations(int32_t streamId,
1644 const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
1645 ATRACE_CALL();
1646
1647 binder::Status res;
1648 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1649
1650 Mutex::Autolock icl(mBinderSerializationLock);
1651
1652 if (!outputConfiguration.isComplete()) {
1653 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1654 "OutputConfiguration isn't valid!");
1655 }
1656
1657 const std::vector<sp<IGraphicBufferProducer> >& bufferProducers =
1658 outputConfiguration.getGraphicBufferProducers();
1659 const std::string &physicalId = outputConfiguration.getPhysicalCameraId();
1660
1661 if (bufferProducers.size() == 0) {
1662 ALOGE("%s: bufferProducers must not be empty", __FUNCTION__);
1663 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Target Surface is invalid");
1664 }
1665
1666 // streamId should be in mStreamMap if this stream already has a surface attached
1667 // to it. Otherwise, it should be in mDeferredStreams.
1668 bool streamIdConfigured = false;
1669 ssize_t deferredStreamIndex = NAME_NOT_FOUND;
1670 for (size_t i = 0; i < mStreamMap.size(); i++) {
1671 if (mStreamMap.valueAt(i).streamId() == streamId) {
1672 streamIdConfigured = true;
1673 break;
1674 }
1675 }
1676 for (size_t i = 0; i < mDeferredStreams.size(); i++) {
1677 if (streamId == mDeferredStreams[i]) {
1678 deferredStreamIndex = i;
1679 break;
1680 }
1681
1682 }
1683 if (deferredStreamIndex == NAME_NOT_FOUND && !streamIdConfigured) {
1684 std::string msg = fmt::sprintf("Camera %s: deferred surface is set to a unknown stream"
1685 "(ID %d)", mCameraIdStr.c_str(), streamId);
1686 ALOGW("%s: %s", __FUNCTION__, msg.c_str());
1687 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
1688 }
1689
1690 if (mStreamInfoMap[streamId].finalized) {
1691 std::string msg = fmt::sprintf("Camera %s: finalizeOutputConfigurations has been called"
1692 " on stream ID %d", mCameraIdStr.c_str(), streamId);
1693 ALOGW("%s: %s", __FUNCTION__, msg.c_str());
1694 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
1695 }
1696
1697 if (!mDevice.get()) {
1698 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1699 }
1700
1701 std::vector<sp<Surface>> consumerSurfaces;
1702 const std::vector<int32_t> &sensorPixelModesUsed =
1703 outputConfiguration.getSensorPixelModesUsed();
1704 int64_t dynamicRangeProfile = outputConfiguration.getDynamicRangeProfile();
1705 int32_t colorSpace = outputConfiguration.getColorSpace();
1706 int64_t streamUseCase = outputConfiguration.getStreamUseCase();
1707 int timestampBase = outputConfiguration.getTimestampBase();
1708 int mirrorMode = outputConfiguration.getMirrorMode();
1709 for (auto& bufferProducer : bufferProducers) {
1710 // Don't create multiple streams for the same target surface
1711 ssize_t index = mStreamMap.indexOfKey(IInterface::asBinder(bufferProducer));
1712 if (index != NAME_NOT_FOUND) {
1713 ALOGV("Camera %s: Surface already has a stream created "
1714 " for it (ID %zd)", mCameraIdStr.c_str(), index);
1715 continue;
1716 }
1717
1718 sp<Surface> surface;
1719 res = SessionConfigurationUtils::createSurfaceFromGbp(mStreamInfoMap[streamId],
1720 true /*isStreamInfoValid*/, surface, bufferProducer, mCameraIdStr,
1721 mDevice->infoPhysical(physicalId), sensorPixelModesUsed, dynamicRangeProfile,
1722 streamUseCase, timestampBase, mirrorMode, colorSpace, /*respectSurfaceSize*/false);
1723
1724 if (!res.isOk())
1725 return res;
1726
1727 consumerSurfaces.push_back(surface);
1728 }
1729
1730 // Gracefully handle case where finalizeOutputConfigurations is called
1731 // without any new surface.
1732 if (consumerSurfaces.size() == 0) {
1733 mStreamInfoMap[streamId].finalized = true;
1734 return res;
1735 }
1736
1737 // Finish the deferred stream configuration with the surface.
1738 status_t err;
1739 std::vector<int> consumerSurfaceIds;
1740 err = mDevice->setConsumerSurfaces(streamId, consumerSurfaces, &consumerSurfaceIds);
1741 if (err == OK) {
1742 for (size_t i = 0; i < consumerSurfaces.size(); i++) {
1743 sp<IBinder> binder = IInterface::asBinder(
1744 consumerSurfaces[i]->getIGraphicBufferProducer());
1745 ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %d", __FUNCTION__,
1746 binder.get(), streamId, consumerSurfaceIds[i]);
1747 mStreamMap.add(binder, StreamSurfaceId(streamId, consumerSurfaceIds[i]));
1748 }
1749 if (deferredStreamIndex != NAME_NOT_FOUND) {
1750 mDeferredStreams.removeItemsAt(deferredStreamIndex);
1751 }
1752 mStreamInfoMap[streamId].finalized = true;
1753 mConfiguredOutputs.replaceValueFor(streamId, outputConfiguration);
1754 } else if (err == NO_INIT) {
1755 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1756 "Camera %s: Deferred surface is invalid: %s (%d)",
1757 mCameraIdStr.c_str(), strerror(-err), err);
1758 } else {
1759 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1760 "Camera %s: Error setting output stream deferred surface: %s (%d)",
1761 mCameraIdStr.c_str(), strerror(-err), err);
1762 }
1763
1764 return res;
1765 }
1766
setCameraAudioRestriction(int32_t mode)1767 binder::Status CameraDeviceClient::setCameraAudioRestriction(int32_t mode) {
1768 ATRACE_CALL();
1769 binder::Status res;
1770 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1771
1772 if (!isValidAudioRestriction(mode)) {
1773 std::string msg = fmt::sprintf("Camera %s: invalid audio restriction mode %d",
1774 mCameraIdStr.c_str(), mode);
1775 ALOGW("%s: %s", __FUNCTION__, msg.c_str());
1776 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
1777 }
1778
1779 Mutex::Autolock icl(mBinderSerializationLock);
1780 BasicClient::setAudioRestriction(mode);
1781 return binder::Status::ok();
1782 }
1783
getGlobalAudioRestriction(int32_t * outMode)1784 binder::Status CameraDeviceClient::getGlobalAudioRestriction(/*out*/ int32_t* outMode) {
1785 ATRACE_CALL();
1786 binder::Status res;
1787 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1788 Mutex::Autolock icl(mBinderSerializationLock);
1789 if (outMode != nullptr) {
1790 *outMode = BasicClient::getServiceAudioRestriction();
1791 }
1792 return binder::Status::ok();
1793 }
1794
setCameraServiceWatchdog(bool enabled)1795 status_t CameraDeviceClient::setCameraServiceWatchdog(bool enabled) {
1796 return mDevice->setCameraServiceWatchdog(enabled);
1797 }
1798
setRotateAndCropOverride(uint8_t rotateAndCrop,bool fromHal)1799 status_t CameraDeviceClient::setRotateAndCropOverride(uint8_t rotateAndCrop, bool fromHal) {
1800 if (rotateAndCrop > ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return BAD_VALUE;
1801
1802 return mDevice->setRotateAndCropAutoBehavior(
1803 static_cast<camera_metadata_enum_android_scaler_rotate_and_crop_t>(rotateAndCrop), fromHal);
1804 }
1805
setAutoframingOverride(uint8_t autoframingValue)1806 status_t CameraDeviceClient::setAutoframingOverride(uint8_t autoframingValue) {
1807 if (autoframingValue > ANDROID_CONTROL_AUTOFRAMING_AUTO) return BAD_VALUE;
1808
1809 return mDevice->setAutoframingAutoBehavior(
1810 static_cast<camera_metadata_enum_android_control_autoframing_t>(autoframingValue));
1811 }
1812
supportsCameraMute()1813 bool CameraDeviceClient::supportsCameraMute() {
1814 return mDevice->supportsCameraMute();
1815 }
1816
setCameraMute(bool enabled)1817 status_t CameraDeviceClient::setCameraMute(bool enabled) {
1818 return mDevice->setCameraMute(enabled);
1819 }
1820
setStreamUseCaseOverrides(const std::vector<int64_t> & useCaseOverrides)1821 void CameraDeviceClient::setStreamUseCaseOverrides(
1822 const std::vector<int64_t>& useCaseOverrides) {
1823 mDevice->setStreamUseCaseOverrides(useCaseOverrides);
1824 }
1825
clearStreamUseCaseOverrides()1826 void CameraDeviceClient::clearStreamUseCaseOverrides() {
1827 mDevice->clearStreamUseCaseOverrides();
1828 }
1829
supportsZoomOverride()1830 bool CameraDeviceClient::supportsZoomOverride() {
1831 return mDevice->supportsZoomOverride();
1832 }
1833
setZoomOverride(int32_t zoomOverride)1834 status_t CameraDeviceClient::setZoomOverride(int32_t zoomOverride) {
1835 return mDevice->setZoomOverride(zoomOverride);
1836 }
1837
switchToOffline(const sp<hardware::camera2::ICameraDeviceCallbacks> & cameraCb,const std::vector<int> & offlineOutputIds,sp<hardware::camera2::ICameraOfflineSession> * session)1838 binder::Status CameraDeviceClient::switchToOffline(
1839 const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb,
1840 const std::vector<int>& offlineOutputIds,
1841 /*out*/
1842 sp<hardware::camera2::ICameraOfflineSession>* session) {
1843 ATRACE_CALL();
1844
1845 binder::Status res;
1846 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1847
1848 Mutex::Autolock icl(mBinderSerializationLock);
1849
1850 if (!mDevice.get()) {
1851 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1852 }
1853
1854 if (offlineOutputIds.empty()) {
1855 std::string msg = "Offline surfaces must not be empty";
1856 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
1857 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
1858 }
1859
1860 if (session == nullptr) {
1861 std::string msg = "Invalid offline session";
1862 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
1863 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
1864 }
1865
1866 std::vector<int32_t> offlineStreamIds;
1867 offlineStreamIds.reserve(offlineOutputIds.size());
1868 KeyedVector<sp<IBinder>, sp<CompositeStream>> offlineCompositeStreamMap;
1869 for (const auto& streamId : offlineOutputIds) {
1870 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
1871 if (index == NAME_NOT_FOUND) {
1872 std::string msg = fmt::sprintf("Offline surface with id: %d is not registered",
1873 streamId);
1874 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
1875 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
1876 }
1877
1878 if (!mStreamInfoMap[streamId].supportsOffline) {
1879 std::string msg = fmt::sprintf("Offline surface with id: %d doesn't support "
1880 "offline mode", streamId);
1881 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
1882 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
1883 }
1884
1885 Mutex::Autolock l(mCompositeLock);
1886 bool isCompositeStream = false;
1887 for (const auto& gbp : mConfiguredOutputs.valueAt(index).getGraphicBufferProducers()) {
1888 sp<Surface> s = new Surface(gbp, false /*controlledByApp*/);
1889 isCompositeStream = camera3::DepthCompositeStream::isDepthCompositeStream(s) ||
1890 camera3::HeicCompositeStream::isHeicCompositeStream(s) ||
1891 (camera3::JpegRCompositeStream::isJpegRCompositeStream(s) &&
1892 !mDevice->isCompositeJpegRDisabled());
1893 if (isCompositeStream) {
1894 auto compositeIdx = mCompositeStreamMap.indexOfKey(IInterface::asBinder(gbp));
1895 if (compositeIdx == NAME_NOT_FOUND) {
1896 ALOGE("%s: Unknown composite stream", __FUNCTION__);
1897 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1898 "Unknown composite stream");
1899 }
1900
1901 mCompositeStreamMap.valueAt(compositeIdx)->insertCompositeStreamIds(
1902 &offlineStreamIds);
1903 offlineCompositeStreamMap.add(mCompositeStreamMap.keyAt(compositeIdx),
1904 mCompositeStreamMap.valueAt(compositeIdx));
1905 break;
1906 }
1907 }
1908
1909 if (!isCompositeStream) {
1910 offlineStreamIds.push_back(streamId);
1911 }
1912 }
1913
1914 sp<CameraOfflineSessionBase> offlineSession;
1915 auto ret = mDevice->switchToOffline(offlineStreamIds, &offlineSession);
1916 if (ret != OK) {
1917 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1918 "Camera %s: Error switching to offline mode: %s (%d)",
1919 mCameraIdStr.c_str(), strerror(ret), ret);
1920 }
1921
1922 sp<CameraOfflineSessionClient> offlineClient;
1923 if (offlineSession.get() != nullptr) {
1924 offlineClient = new CameraOfflineSessionClient(sCameraService,
1925 offlineSession, offlineCompositeStreamMap, cameraCb, mAttributionAndPermissionUtils,
1926 mClientPackageName, mClientFeatureId, mCameraIdStr, mCameraFacing, mOrientation,
1927 mClientPid, mClientUid, mServicePid);
1928 ret = sCameraService->addOfflineClient(mCameraIdStr, offlineClient);
1929 }
1930
1931 if (ret == OK) {
1932 // A successful offline session switch must reset the current camera client
1933 // and release any resources occupied by previously configured streams.
1934 mStreamMap.clear();
1935 mConfiguredOutputs.clear();
1936 mDeferredStreams.clear();
1937 mStreamInfoMap.clear();
1938 Mutex::Autolock l(mCompositeLock);
1939 mCompositeStreamMap.clear();
1940 mInputStream = {false, 0, 0, 0, 0};
1941 } else {
1942 // In case we failed to register the offline client, ensure that it still initialized
1943 // so that all failing requests can return back correctly once the object is released.
1944 offlineClient->initialize(nullptr /*cameraProviderManager*/, std::string()/*monitorTags*/);
1945
1946 switch(ret) {
1947 case BAD_VALUE:
1948 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1949 "Illegal argument to HAL module for camera \"%s\"", mCameraIdStr.c_str());
1950 case TIMED_OUT:
1951 return STATUS_ERROR_FMT(CameraService::ERROR_CAMERA_IN_USE,
1952 "Camera \"%s\" is already open", mCameraIdStr.c_str());
1953 default:
1954 return STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1955 "Failed to initialize camera \"%s\": %s (%d)", mCameraIdStr.c_str(),
1956 strerror(-ret), ret);
1957 }
1958 }
1959
1960 *session = offlineClient;
1961
1962 return binder::Status::ok();
1963 }
1964
dump(int fd,const Vector<String16> & args)1965 status_t CameraDeviceClient::dump(int fd, const Vector<String16>& args) {
1966 return BasicClient::dump(fd, args);
1967 }
1968
dumpClient(int fd,const Vector<String16> & args)1969 status_t CameraDeviceClient::dumpClient(int fd, const Vector<String16>& args) {
1970 dprintf(fd, " CameraDeviceClient[%s] (%p) dump:\n",
1971 mCameraIdStr.c_str(),
1972 (getRemoteCallback() != NULL ?
1973 IInterface::asBinder(getRemoteCallback()).get() : NULL) );
1974 dprintf(fd, " Current client UID %u\n", mClientUid);
1975
1976 dprintf(fd, " State:\n");
1977 dprintf(fd, " Request ID counter: %d\n", mRequestIdCounter);
1978 if (mInputStream.configured) {
1979 dprintf(fd, " Current input stream ID: %d\n", mInputStream.id);
1980 } else {
1981 dprintf(fd, " No input stream configured.\n");
1982 }
1983 if (!mStreamMap.isEmpty()) {
1984 dprintf(fd, " Current output stream/surface IDs:\n");
1985 for (size_t i = 0; i < mStreamMap.size(); i++) {
1986 dprintf(fd, " Stream %d Surface %d\n",
1987 mStreamMap.valueAt(i).streamId(),
1988 mStreamMap.valueAt(i).surfaceId());
1989 }
1990 } else if (!mDeferredStreams.isEmpty()) {
1991 dprintf(fd, " Current deferred surface output stream IDs:\n");
1992 for (auto& streamId : mDeferredStreams) {
1993 dprintf(fd, " Stream %d\n", streamId);
1994 }
1995 } else {
1996 dprintf(fd, " No output streams configured.\n");
1997 }
1998 // TODO: print dynamic/request section from most recent requests
1999 mFrameProcessor->dump(fd, args);
2000
2001 return dumpDevice(fd, args);
2002 }
2003
startWatchingTags(const std::string & tags,int out)2004 status_t CameraDeviceClient::startWatchingTags(const std::string &tags, int out) {
2005 sp<CameraDeviceBase> device = mDevice;
2006 if (!device) {
2007 dprintf(out, " Device is detached.");
2008 return OK;
2009 }
2010 device->startWatchingTags(tags);
2011 return OK;
2012 }
2013
stopWatchingTags(int out)2014 status_t CameraDeviceClient::stopWatchingTags(int out) {
2015 sp<CameraDeviceBase> device = mDevice;
2016 if (!device) {
2017 dprintf(out, " Device is detached.");
2018 return OK;
2019 }
2020 device->stopWatchingTags();
2021 return OK;
2022 }
2023
dumpWatchedEventsToVector(std::vector<std::string> & out)2024 status_t CameraDeviceClient::dumpWatchedEventsToVector(std::vector<std::string> &out) {
2025 sp<CameraDeviceBase> device = mDevice;
2026 if (!device) {
2027 return OK;
2028 }
2029 device->dumpWatchedEventsToVector(out);
2030 return OK;
2031 }
2032
notifyError(int32_t errorCode,const CaptureResultExtras & resultExtras)2033 void CameraDeviceClient::notifyError(int32_t errorCode,
2034 const CaptureResultExtras& resultExtras) {
2035 // Thread safe. Don't bother locking.
2036 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
2037
2038 bool skipClientNotification = false;
2039 {
2040 // Access to the composite stream map must be synchronized
2041 Mutex::Autolock l(mCompositeLock);
2042 // Composites can have multiple internal streams. Error notifications coming from such
2043 // internal streams may need to remain within camera service.
2044 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
2045 skipClientNotification |= mCompositeStreamMap.valueAt(i)->onError(errorCode,
2046 resultExtras);
2047 }
2048 }
2049
2050 if ((remoteCb != 0) && (!skipClientNotification)) {
2051 remoteCb->onDeviceError(errorCode, resultExtras);
2052 }
2053 }
2054
notifyRepeatingRequestError(long lastFrameNumber)2055 void CameraDeviceClient::notifyRepeatingRequestError(long lastFrameNumber) {
2056 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
2057
2058 if (remoteCb != 0) {
2059 remoteCb->onRepeatingRequestError(lastFrameNumber, mStreamingRequestId);
2060 }
2061
2062 Mutex::Autolock idLock(mStreamingRequestIdLock);
2063 mStreamingRequestId = REQUEST_ID_NONE;
2064 }
2065
notifyIdle(int64_t requestCount,int64_t resultErrorCount,bool deviceError,std::pair<int32_t,int32_t> mostRequestedFpsRange,const std::vector<hardware::CameraStreamStats> & streamStats)2066 void CameraDeviceClient::notifyIdle(
2067 int64_t requestCount, int64_t resultErrorCount, bool deviceError,
2068 std::pair<int32_t, int32_t> mostRequestedFpsRange,
2069 const std::vector<hardware::CameraStreamStats>& streamStats) {
2070 // Thread safe. Don't bother locking.
2071 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
2072
2073 if (remoteCb != 0) {
2074 remoteCb->onDeviceIdle();
2075 }
2076
2077 std::vector<hardware::CameraStreamStats> fullStreamStats = streamStats;
2078 {
2079 Mutex::Autolock l(mCompositeLock);
2080 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
2081 hardware::CameraStreamStats compositeStats;
2082 mCompositeStreamMap.valueAt(i)->getStreamStats(&compositeStats);
2083 if (compositeStats.mWidth > 0) {
2084 fullStreamStats.push_back(compositeStats);
2085 }
2086 }
2087 }
2088 Camera2ClientBase::notifyIdleWithUserTag(requestCount, resultErrorCount, deviceError,
2089 mostRequestedFpsRange,
2090 fullStreamStats,
2091 mRunningSessionStats.mUserTag,
2092 mRunningSessionStats.mVideoStabilizationMode,
2093 mRunningSessionStats.mUsedUltraWide,
2094 mRunningSessionStats.mUsedSettingsOverrideZoom);
2095 }
2096
notifyShutter(const CaptureResultExtras & resultExtras,nsecs_t timestamp)2097 void CameraDeviceClient::notifyShutter(const CaptureResultExtras& resultExtras,
2098 nsecs_t timestamp) {
2099 // Thread safe. Don't bother locking.
2100 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
2101 if (remoteCb != 0) {
2102 remoteCb->onCaptureStarted(resultExtras, timestamp);
2103 }
2104 Camera2ClientBase::notifyShutter(resultExtras, timestamp);
2105
2106 // Access to the composite stream map must be synchronized
2107 Mutex::Autolock l(mCompositeLock);
2108 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
2109 mCompositeStreamMap.valueAt(i)->onShutter(resultExtras, timestamp);
2110 }
2111 }
2112
notifyPrepared(int streamId)2113 void CameraDeviceClient::notifyPrepared(int streamId) {
2114 // Thread safe. Don't bother locking.
2115 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
2116 if (remoteCb != 0) {
2117 ALOGV("%s: stream id %d", __FUNCTION__, streamId);
2118 remoteCb->onPrepared(streamId);
2119 }
2120 }
2121
notifyRequestQueueEmpty()2122 void CameraDeviceClient::notifyRequestQueueEmpty() {
2123 // Thread safe. Don't bother locking.
2124 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
2125 if (remoteCb != 0) {
2126 remoteCb->onRequestQueueEmpty();
2127 }
2128 }
2129
detachDevice()2130 void CameraDeviceClient::detachDevice() {
2131 if (mDevice == 0) return;
2132
2133 nsecs_t startTime = systemTime();
2134 ALOGV("Camera %s: Stopping processors", mCameraIdStr.c_str());
2135
2136 if (mFrameProcessor.get() != nullptr) {
2137 mFrameProcessor->removeListener(
2138 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MIN_ID,
2139 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MAX_ID, /*listener*/this);
2140 mFrameProcessor->requestExit();
2141 ALOGV("Camera %s: Waiting for threads", mCameraIdStr.c_str());
2142 mFrameProcessor->join();
2143 ALOGV("Camera %s: Disconnecting device", mCameraIdStr.c_str());
2144 }
2145
2146 // WORKAROUND: HAL refuses to disconnect while there's streams in flight
2147 {
2148 int64_t lastFrameNumber;
2149 status_t code;
2150 if ((code = mDevice->flush(&lastFrameNumber)) != OK) {
2151 ALOGE("%s: flush failed with code 0x%x", __FUNCTION__, code);
2152 }
2153
2154 if ((code = mDevice->waitUntilDrained()) != OK) {
2155 ALOGE("%s: waitUntilDrained failed with code 0x%x", __FUNCTION__,
2156 code);
2157 }
2158 }
2159
2160 {
2161 Mutex::Autolock l(mCompositeLock);
2162 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
2163 auto ret = mCompositeStreamMap.valueAt(i)->deleteInternalStreams();
2164 if (ret != OK) {
2165 ALOGE("%s: Failed removing composite stream %s (%d)", __FUNCTION__,
2166 strerror(-ret), ret);
2167 }
2168 }
2169 mCompositeStreamMap.clear();
2170 }
2171
2172 bool hasDeviceError = mDevice->hasDeviceError();
2173 Camera2ClientBase::detachDevice();
2174
2175 int32_t closeLatencyMs = ns2ms(systemTime() - startTime);
2176 mCameraServiceProxyWrapper->logClose(mCameraIdStr, closeLatencyMs, hasDeviceError);
2177 }
2178
2179 /** Device-related methods */
onResultAvailable(const CaptureResult & result)2180 void CameraDeviceClient::onResultAvailable(const CaptureResult& result) {
2181 ATRACE_CALL();
2182 ALOGV("%s", __FUNCTION__);
2183
2184 // Thread-safe. No lock necessary.
2185 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = mRemoteCallback;
2186 if (remoteCb != NULL) {
2187 remoteCb->onResultReceived(result.mMetadata, result.mResultExtras,
2188 result.mPhysicalMetadatas);
2189 }
2190
2191 // Access to the composite stream map must be synchronized
2192 Mutex::Autolock l(mCompositeLock);
2193 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
2194 mCompositeStreamMap.valueAt(i)->onResultAvailable(result);
2195 }
2196 }
2197
checkPidStatus(const char * checkLocation)2198 binder::Status CameraDeviceClient::checkPidStatus(const char* checkLocation) {
2199 if (mDisconnected) {
2200 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED,
2201 "The camera device has been disconnected");
2202 }
2203 status_t res = checkPid(checkLocation);
2204 return (res == OK) ? binder::Status::ok() :
2205 STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
2206 "Attempt to use camera from a different process than original client");
2207 }
2208
2209 // TODO: move to Camera2ClientBase
enforceRequestPermissions(CameraMetadata & metadata)2210 bool CameraDeviceClient::enforceRequestPermissions(CameraMetadata& metadata) {
2211
2212 const int pid = getCallingPid();
2213 const int selfPid = getpid();
2214 camera_metadata_entry_t entry;
2215
2216 /**
2217 * Mixin default important security values
2218 * - android.led.transmit = defaulted ON
2219 */
2220 CameraMetadata staticInfo = mDevice->info();
2221 entry = staticInfo.find(ANDROID_LED_AVAILABLE_LEDS);
2222 for(size_t i = 0; i < entry.count; ++i) {
2223 uint8_t led = entry.data.u8[i];
2224
2225 switch(led) {
2226 case ANDROID_LED_AVAILABLE_LEDS_TRANSMIT: {
2227 uint8_t transmitDefault = ANDROID_LED_TRANSMIT_ON;
2228 if (!metadata.exists(ANDROID_LED_TRANSMIT)) {
2229 metadata.update(ANDROID_LED_TRANSMIT,
2230 &transmitDefault, 1);
2231 }
2232 break;
2233 }
2234 }
2235 }
2236
2237 // We can do anything!
2238 if (pid == selfPid) {
2239 return true;
2240 }
2241
2242 /**
2243 * Permission check special fields in the request
2244 * - android.led.transmit = android.permission.CAMERA_DISABLE_TRANSMIT
2245 */
2246 entry = metadata.find(ANDROID_LED_TRANSMIT);
2247 if (entry.count > 0 && entry.data.u8[0] != ANDROID_LED_TRANSMIT_ON) {
2248 String16 permissionString =
2249 toString16("android.permission.CAMERA_DISABLE_TRANSMIT_LED");
2250 if (!checkCallingPermission(permissionString)) {
2251 const int uid = getCallingUid();
2252 ALOGE("Permission Denial: "
2253 "can't disable transmit LED pid=%d, uid=%d", pid, uid);
2254 return false;
2255 }
2256 }
2257
2258 return true;
2259 }
2260
getRotationTransformLocked(int mirrorMode,int32_t * transform)2261 status_t CameraDeviceClient::getRotationTransformLocked(int mirrorMode,
2262 int32_t* transform) {
2263 ALOGV("%s: begin", __FUNCTION__);
2264
2265 const CameraMetadata& staticInfo = mDevice->info();
2266 return CameraUtils::getRotationTransform(staticInfo, mirrorMode, transform);
2267 }
2268
getStaticInfo(const std::string & cameraId)2269 const CameraMetadata &CameraDeviceClient::getStaticInfo(const std::string &cameraId) {
2270 if (mDevice->getId() == cameraId) {
2271 return mDevice->info();
2272 }
2273 return mDevice->infoPhysical(cameraId);
2274 }
2275
supportsUltraHighResolutionCapture(const std::string & cameraId)2276 bool CameraDeviceClient::supportsUltraHighResolutionCapture(const std::string &cameraId) {
2277 const CameraMetadata &deviceInfo = getStaticInfo(cameraId);
2278 return SessionConfigurationUtils::supportsUltraHighResolutionCapture(deviceInfo);
2279 }
2280
isSensorPixelModeConsistent(const std::list<int> & streamIdList,const CameraMetadata & settings)2281 bool CameraDeviceClient::isSensorPixelModeConsistent(
2282 const std::list<int> &streamIdList, const CameraMetadata &settings) {
2283 // First we get the sensorPixelMode from the settings metadata.
2284 int32_t sensorPixelMode = ANDROID_SENSOR_PIXEL_MODE_DEFAULT;
2285 camera_metadata_ro_entry sensorPixelModeEntry = settings.find(ANDROID_SENSOR_PIXEL_MODE);
2286 if (sensorPixelModeEntry.count != 0) {
2287 sensorPixelMode = sensorPixelModeEntry.data.u8[0];
2288 if (sensorPixelMode != ANDROID_SENSOR_PIXEL_MODE_DEFAULT &&
2289 sensorPixelMode != ANDROID_SENSOR_PIXEL_MODE_MAXIMUM_RESOLUTION) {
2290 ALOGE("%s: Request sensor pixel mode not is not one of the valid values %d",
2291 __FUNCTION__, sensorPixelMode);
2292 return false;
2293 }
2294 }
2295 // Check whether each stream has max resolution allowed.
2296 bool consistent = true;
2297 for (auto it : streamIdList) {
2298 auto const streamInfoIt = mStreamInfoMap.find(it);
2299 if (streamInfoIt == mStreamInfoMap.end()) {
2300 ALOGE("%s: stream id %d not created, skipping", __FUNCTION__, it);
2301 return false;
2302 }
2303 consistent =
2304 streamInfoIt->second.sensorPixelModesUsed.find(sensorPixelMode) !=
2305 streamInfoIt->second.sensorPixelModesUsed.end();
2306 if (!consistent) {
2307 ALOGE("sensorPixelMode used %i not consistent with configured modes", sensorPixelMode);
2308 for (auto m : streamInfoIt->second.sensorPixelModesUsed) {
2309 ALOGE("sensor pixel mode used list: %i", m);
2310 }
2311 break;
2312 }
2313 }
2314
2315 return consistent;
2316 }
2317
2318 } // namespace android
2319