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