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