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