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 #ifndef ANDROID_SERVERS_CAMERA_PHOTOGRAPHY_CAMERADEVICECLIENT_H 18 #define ANDROID_SERVERS_CAMERA_PHOTOGRAPHY_CAMERADEVICECLIENT_H 19 20 #include <android/hardware/camera2/BnCameraDeviceUser.h> 21 #include <android/hardware/camera2/ICameraDeviceCallbacks.h> 22 #include <camera/camera2/OutputConfiguration.h> 23 #include <camera/camera2/SessionConfiguration.h> 24 #include <camera/camera2/SubmitInfo.h> 25 #include <unordered_map> 26 27 #include "CameraOfflineSessionClient.h" 28 #include "CameraService.h" 29 #include "common/FrameProcessorBase.h" 30 #include "common/Camera2ClientBase.h" 31 #include "CompositeStream.h" 32 #include "utils/CameraServiceProxyWrapper.h" 33 #include "utils/SessionConfigurationUtils.h" 34 35 using android::camera3::OutputStreamInfo; 36 using android::camera3::CompositeStream; 37 38 namespace android { 39 40 struct CameraDeviceClientBase : 41 public CameraService::BasicClient, 42 public hardware::camera2::BnCameraDeviceUser 43 { 44 typedef hardware::camera2::ICameraDeviceCallbacks TCamCallbacks; 45 getRemoteCallbackCameraDeviceClientBase46 const sp<hardware::camera2::ICameraDeviceCallbacks>& getRemoteCallback() { 47 return mRemoteCallback; 48 } 49 50 protected: 51 CameraDeviceClientBase(const sp<CameraService>& cameraService, 52 const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback, 53 std::shared_ptr<AttributionAndPermissionUtils> attributionAndPermissionUtils, 54 const std::string& clientPackageName, 55 bool systemNativeClient, 56 const std::optional<std::string>& clientFeatureId, 57 const std::string& cameraId, 58 int api1CameraId, 59 int cameraFacing, 60 int sensorOrientation, 61 int clientPid, 62 uid_t clientUid, 63 int servicePid, 64 int rotationOverride); 65 66 sp<hardware::camera2::ICameraDeviceCallbacks> mRemoteCallback; 67 }; 68 69 /** 70 * Implements the binder ICameraDeviceUser API, 71 * meant for HAL3-public implementation of 72 * android.hardware.photography.CameraDevice 73 */ 74 class CameraDeviceClient : 75 public Camera2ClientBase<CameraDeviceClientBase>, 76 public camera2::FrameProcessorBase::FilteredListener 77 { 78 public: 79 /** 80 * ICameraDeviceUser interface (see ICameraDeviceUser for details) 81 */ 82 83 // Note that the callee gets a copy of the metadata. 84 virtual binder::Status submitRequest( 85 const hardware::camera2::CaptureRequest& request, 86 bool streaming = false, 87 /*out*/ 88 hardware::camera2::utils::SubmitInfo *submitInfo = nullptr) override; 89 // List of requests are copied. 90 virtual binder::Status submitRequestList( 91 const std::vector<hardware::camera2::CaptureRequest>& requests, 92 bool streaming = false, 93 /*out*/ 94 hardware::camera2::utils::SubmitInfo *submitInfo = nullptr) override; 95 virtual binder::Status cancelRequest(int requestId, 96 /*out*/ 97 int64_t* lastFrameNumber = NULL) override; 98 99 virtual binder::Status beginConfigure() override; 100 101 virtual binder::Status endConfigure(int operatingMode, 102 const hardware::camera2::impl::CameraMetadataNative& sessionParams, 103 int64_t startTimeMs, 104 /*out*/ 105 std::vector<int>* offlineStreamIds) override; 106 107 // Verify specific session configuration. 108 virtual binder::Status isSessionConfigurationSupported( 109 const SessionConfiguration& sessionConfiguration, 110 /*out*/ 111 bool* streamStatus) override; 112 113 // Returns -EBUSY if device is not idle or in error state 114 virtual binder::Status deleteStream(int streamId) override; 115 116 virtual binder::Status createStream( 117 const hardware::camera2::params::OutputConfiguration &outputConfiguration, 118 /*out*/ 119 int32_t* newStreamId = NULL) override; 120 121 // Create an input stream of width, height, and format. 122 virtual binder::Status createInputStream(int width, int height, int format, 123 bool isMultiResolution, 124 /*out*/ 125 int32_t* newStreamId = NULL) override; 126 127 // Get the buffer producer of the input stream 128 virtual binder::Status getInputSurface( 129 /*out*/ 130 view::Surface *inputSurface) override; 131 132 // Create a request object from a template. 133 virtual binder::Status createDefaultRequest(int templateId, 134 /*out*/ 135 hardware::camera2::impl::CameraMetadataNative* request) override; 136 137 // Get the static metadata for the camera 138 // -- Caller owns the newly allocated metadata 139 virtual binder::Status getCameraInfo( 140 /*out*/ 141 hardware::camera2::impl::CameraMetadataNative* cameraCharacteristics) override; 142 143 // Wait until all the submitted requests have finished processing 144 virtual binder::Status waitUntilIdle() override; 145 146 // Flush all active and pending requests as fast as possible 147 virtual binder::Status flush( 148 /*out*/ 149 int64_t* lastFrameNumber = NULL) override; 150 151 // Prepare stream by preallocating its buffers 152 virtual binder::Status prepare(int32_t streamId) override; 153 154 // Tear down stream resources by freeing its unused buffers 155 virtual binder::Status tearDown(int32_t streamId) override; 156 157 // Prepare stream by preallocating up to maxCount of its buffers 158 virtual binder::Status prepare2(int32_t maxCount, int32_t streamId) override; 159 160 // Update an output configuration 161 virtual binder::Status updateOutputConfiguration(int streamId, 162 const hardware::camera2::params::OutputConfiguration &outputConfiguration) override; 163 164 // Finalize the output configurations with surfaces not added before. 165 virtual binder::Status finalizeOutputConfigurations(int32_t streamId, 166 const hardware::camera2::params::OutputConfiguration &outputConfiguration) override; 167 168 virtual binder::Status setCameraAudioRestriction(int32_t mode) override; 169 170 virtual binder::Status getGlobalAudioRestriction(/*out*/int32_t* outMode) override; 171 172 virtual binder::Status switchToOffline( 173 const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb, 174 const std::vector<int>& offlineOutputIds, 175 /*out*/ 176 sp<hardware::camera2::ICameraOfflineSession>* session) override; 177 178 /** 179 * Interface used by CameraService 180 */ 181 182 CameraDeviceClient(const sp<CameraService>& cameraService, 183 const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback, 184 std::shared_ptr<CameraServiceProxyWrapper> cameraServiceProxyWrapper, 185 std::shared_ptr<AttributionAndPermissionUtils> attributionAndPermissionUtils, 186 const std::string& clientPackageName, 187 bool clientPackageOverride, 188 const std::optional<std::string>& clientFeatureId, 189 const std::string& cameraId, 190 int cameraFacing, 191 int sensorOrientation, 192 int clientPid, 193 uid_t clientUid, 194 int servicePid, 195 bool overrideForPerfClass, 196 int rotationOverride, 197 const std::string& originalCameraId); 198 virtual ~CameraDeviceClient(); 199 200 virtual status_t initialize(sp<CameraProviderManager> manager, 201 const std::string& monitorTags) override; 202 203 virtual status_t setRotateAndCropOverride(uint8_t rotateAndCrop, 204 bool fromHal = false) override; 205 206 virtual status_t setAutoframingOverride(uint8_t autoframingValue) override; 207 208 virtual bool supportsCameraMute(); 209 virtual status_t setCameraMute(bool enabled); 210 211 virtual bool supportsZoomOverride() override; 212 virtual status_t setZoomOverride(int32_t zoomOverride) override; 213 214 virtual status_t dump(int fd, const Vector<String16>& args); 215 216 virtual status_t dumpClient(int fd, const Vector<String16>& args); 217 218 virtual status_t startWatchingTags(const std::string &tags, int out); 219 virtual status_t stopWatchingTags(int out); 220 virtual status_t dumpWatchedEventsToVector(std::vector<std::string> &out); 221 222 virtual status_t setCameraServiceWatchdog(bool enabled); 223 224 virtual void setStreamUseCaseOverrides(const std::vector<int64_t>& useCaseOverrides); 225 virtual void clearStreamUseCaseOverrides() override; 226 227 /** 228 * Device listener interface 229 */ 230 231 virtual void notifyIdle(int64_t requestCount, int64_t resultErrorCount, bool deviceError, 232 std::pair<int32_t, int32_t> mostRequestedFpsRange, 233 const std::vector<hardware::CameraStreamStats>& streamStats); 234 virtual void notifyError(int32_t errorCode, 235 const CaptureResultExtras& resultExtras); 236 virtual void notifyShutter(const CaptureResultExtras& resultExtras, nsecs_t timestamp); 237 virtual void notifyPrepared(int streamId); 238 virtual void notifyRequestQueueEmpty(); 239 virtual void notifyRepeatingRequestError(long lastFrameNumber); 240 setImageDumpMask(int mask)241 void setImageDumpMask(int mask) { if (mDevice != nullptr) mDevice->setImageDumpMask(mask); } 242 /** 243 * Interface used by independent components of CameraDeviceClient. 244 */ 245 protected: 246 /** FilteredListener implementation **/ 247 virtual void onResultAvailable(const CaptureResult& result); 248 virtual void detachDevice(); 249 250 // Calculate the ANativeWindow transform from android.sensor.orientation 251 status_t getRotationTransformLocked(int mirrorMode, /*out*/int32_t* transform); 252 253 bool supportsUltraHighResolutionCapture(const std::string &cameraId); 254 255 bool isSensorPixelModeConsistent(const std::list<int> &streamIdList, 256 const CameraMetadata &settings); 257 258 const CameraMetadata &getStaticInfo(const std::string &cameraId); 259 260 private: 261 // StreamSurfaceId encapsulates streamId + surfaceId for a particular surface. 262 // streamId specifies the index of the stream the surface belongs to, and the 263 // surfaceId specifies the index of the surface within the stream. (one stream 264 // could contain multiple surfaces.) 265 class StreamSurfaceId final { 266 public: StreamSurfaceId()267 StreamSurfaceId() { 268 mStreamId = -1; 269 mSurfaceId = -1; 270 } StreamSurfaceId(int32_t streamId,int32_t surfaceId)271 StreamSurfaceId(int32_t streamId, int32_t surfaceId) { 272 mStreamId = streamId; 273 mSurfaceId = surfaceId; 274 } streamId()275 int32_t streamId() const { 276 return mStreamId; 277 } surfaceId()278 int32_t surfaceId() const { 279 return mSurfaceId; 280 } 281 282 private: 283 int32_t mStreamId; 284 int32_t mSurfaceId; 285 286 }; // class StreamSurfaceId 287 288 private: 289 /** ICameraDeviceUser interface-related private members */ 290 291 /** Preview callback related members */ 292 sp<camera2::FrameProcessorBase> mFrameProcessor; 293 294 std::vector<int32_t> mSupportedPhysicalRequestKeys; 295 296 template<typename TProviderPtr> 297 status_t initializeImpl(TProviderPtr providerPtr, const std::string& monitorTags); 298 299 /** Utility members */ 300 binder::Status checkPidStatus(const char* checkLocation); 301 bool enforceRequestPermissions(CameraMetadata& metadata); 302 303 // Create an output stream with surface deferred for future. 304 binder::Status createDeferredSurfaceStreamLocked( 305 const hardware::camera2::params::OutputConfiguration &outputConfiguration, 306 bool isShared, 307 int* newStreamId = NULL); 308 309 // Set the stream transform flags to automatically rotate the camera stream for preview use 310 // cases. 311 binder::Status setStreamTransformLocked(int streamId, int mirrorMode); 312 313 // Utility method to insert the surface into SurfaceMap 314 binder::Status insertGbpLocked(const sp<IGraphicBufferProducer>& gbp, 315 /*out*/SurfaceMap* surfaceMap, /*out*/Vector<int32_t>* streamIds, 316 /*out*/int32_t* currentStreamId); 317 318 // IGraphicsBufferProducer binder -> Stream ID + Surface ID for output streams 319 KeyedVector<sp<IBinder>, StreamSurfaceId> mStreamMap; 320 321 // Stream ID -> OutputConfiguration. Used for looking up Surface by stream/surface index 322 KeyedVector<int32_t, hardware::camera2::params::OutputConfiguration> mConfiguredOutputs; 323 324 // Dynamic range profile id -> Supported dynamic profiles bitmap within an single capture 325 // request 326 std::unordered_map<int64_t, int64_t> mDynamicProfileMap; 327 328 struct InputStreamConfiguration { 329 bool configured; 330 int32_t width; 331 int32_t height; 332 int32_t format; 333 int32_t id; 334 } mInputStream; 335 336 // Streaming request ID 337 int32_t mStreamingRequestId; 338 Mutex mStreamingRequestIdLock; 339 static const int32_t REQUEST_ID_NONE = -1; 340 341 int32_t mRequestIdCounter; 342 343 std::vector<std::string> mPhysicalCameraIds; 344 345 // The list of output streams whose surfaces are deferred. We have to track them separately 346 // as there are no surfaces available and can not be put into mStreamMap. Once the deferred 347 // Surface is configured, the stream id will be moved to mStreamMap. 348 Vector<int32_t> mDeferredStreams; 349 350 // stream ID -> outputStreamInfo mapping 351 std::unordered_map<int32_t, OutputStreamInfo> mStreamInfoMap; 352 353 // map high resolution camera id (logical / physical) -> list of stream ids configured 354 std::unordered_map<std::string, std::unordered_set<int>> mHighResolutionCameraIdToStreamIdSet; 355 356 // set of high resolution camera id (logical / physical) 357 std::unordered_set<std::string> mHighResolutionSensors; 358 359 // Synchronize access to 'mCompositeStreamMap' 360 Mutex mCompositeLock; 361 KeyedVector<sp<IBinder>, sp<CompositeStream>> mCompositeStreamMap; 362 363 sp<CameraProviderManager> mProviderManager; 364 365 // Override the camera characteristics for performance class primary cameras. 366 bool mOverrideForPerfClass; 367 368 // Various fields used to collect session statistics 369 struct RunningSessionStats { 370 // The string representation of object passed into CaptureRequest.setTag. 371 std::string mUserTag; 372 // The last set video stabilization mode 373 int mVideoStabilizationMode = -1; 374 // Whether a zoom_ratio < 1.0 has been used during this session 375 bool mUsedUltraWide = false; 376 // Whether a zoom settings override has been used during this session 377 bool mUsedSettingsOverrideZoom = false; 378 } mRunningSessionStats; 379 380 // This only exists in case of camera ID Remapping. 381 const std::string mOriginalCameraId; 382 }; 383 384 }; // namespace android 385 386 #endif 387