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