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 26 #include "CameraOfflineSessionClient.h" 27 #include "CameraService.h" 28 #include "common/FrameProcessorBase.h" 29 #include "common/Camera2ClientBase.h" 30 #include "CompositeStream.h" 31 32 using android::camera3::OutputStreamInfo; 33 using android::camera3::CompositeStream; 34 35 namespace android { 36 37 typedef std::function<CameraMetadata (const String8 &)> metadataGetter; 38 39 struct CameraDeviceClientBase : 40 public CameraService::BasicClient, 41 public hardware::camera2::BnCameraDeviceUser 42 { 43 typedef hardware::camera2::ICameraDeviceCallbacks TCamCallbacks; 44 getRemoteCallbackCameraDeviceClientBase45 const sp<hardware::camera2::ICameraDeviceCallbacks>& getRemoteCallback() { 46 return mRemoteCallback; 47 } 48 49 protected: 50 CameraDeviceClientBase(const sp<CameraService>& cameraService, 51 const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback, 52 const String16& clientPackageName, 53 const std::unique_ptr<String16>& clientFeatureId, 54 const String8& cameraId, 55 int api1CameraId, 56 int cameraFacing, 57 int clientPid, 58 uid_t clientUid, 59 int servicePid); 60 61 sp<hardware::camera2::ICameraDeviceCallbacks> mRemoteCallback; 62 }; 63 64 /** 65 * Implements the binder ICameraDeviceUser API, 66 * meant for HAL3-public implementation of 67 * android.hardware.photography.CameraDevice 68 */ 69 class CameraDeviceClient : 70 public Camera2ClientBase<CameraDeviceClientBase>, 71 public camera2::FrameProcessorBase::FilteredListener 72 { 73 public: 74 /** 75 * ICameraDeviceUser interface (see ICameraDeviceUser for details) 76 */ 77 78 // Note that the callee gets a copy of the metadata. 79 virtual binder::Status submitRequest( 80 const hardware::camera2::CaptureRequest& request, 81 bool streaming = false, 82 /*out*/ 83 hardware::camera2::utils::SubmitInfo *submitInfo = nullptr) override; 84 // List of requests are copied. 85 virtual binder::Status submitRequestList( 86 const std::vector<hardware::camera2::CaptureRequest>& requests, 87 bool streaming = false, 88 /*out*/ 89 hardware::camera2::utils::SubmitInfo *submitInfo = nullptr) override; 90 virtual binder::Status cancelRequest(int requestId, 91 /*out*/ 92 int64_t* lastFrameNumber = NULL) override; 93 94 virtual binder::Status beginConfigure() override; 95 96 virtual binder::Status endConfigure(int operatingMode, 97 const hardware::camera2::impl::CameraMetadataNative& sessionParams, 98 /*out*/ 99 std::vector<int>* offlineStreamIds) override; 100 101 // Verify specific session configuration. 102 virtual binder::Status isSessionConfigurationSupported( 103 const SessionConfiguration& sessionConfiguration, 104 /*out*/ 105 bool* streamStatus) override; 106 107 // Returns -EBUSY if device is not idle or in error state 108 virtual binder::Status deleteStream(int streamId) override; 109 110 virtual binder::Status createStream( 111 const hardware::camera2::params::OutputConfiguration &outputConfiguration, 112 /*out*/ 113 int32_t* newStreamId = NULL) override; 114 115 // Create an input stream of width, height, and format. 116 virtual binder::Status createInputStream(int width, int height, int format, 117 /*out*/ 118 int32_t* newStreamId = NULL) override; 119 120 // Get the buffer producer of the input stream 121 virtual binder::Status getInputSurface( 122 /*out*/ 123 view::Surface *inputSurface) override; 124 125 // Create a request object from a template. 126 virtual binder::Status createDefaultRequest(int templateId, 127 /*out*/ 128 hardware::camera2::impl::CameraMetadataNative* request) override; 129 130 // Get the static metadata for the camera 131 // -- Caller owns the newly allocated metadata 132 virtual binder::Status getCameraInfo( 133 /*out*/ 134 hardware::camera2::impl::CameraMetadataNative* cameraCharacteristics) override; 135 136 // Wait until all the submitted requests have finished processing 137 virtual binder::Status waitUntilIdle() override; 138 139 // Flush all active and pending requests as fast as possible 140 virtual binder::Status flush( 141 /*out*/ 142 int64_t* lastFrameNumber = NULL) override; 143 144 // Prepare stream by preallocating its buffers 145 virtual binder::Status prepare(int32_t streamId) override; 146 147 // Tear down stream resources by freeing its unused buffers 148 virtual binder::Status tearDown(int32_t streamId) override; 149 150 // Prepare stream by preallocating up to maxCount of its buffers 151 virtual binder::Status prepare2(int32_t maxCount, int32_t streamId) override; 152 153 // Update an output configuration 154 virtual binder::Status updateOutputConfiguration(int streamId, 155 const hardware::camera2::params::OutputConfiguration &outputConfiguration) override; 156 157 // Finalize the output configurations with surfaces not added before. 158 virtual binder::Status finalizeOutputConfigurations(int32_t streamId, 159 const hardware::camera2::params::OutputConfiguration &outputConfiguration) override; 160 161 virtual binder::Status setCameraAudioRestriction(int32_t mode) override; 162 163 virtual binder::Status getGlobalAudioRestriction(/*out*/int32_t* outMode) override; 164 165 virtual binder::Status switchToOffline( 166 const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb, 167 const std::vector<int>& offlineOutputIds, 168 /*out*/ 169 sp<hardware::camera2::ICameraOfflineSession>* session) override; 170 171 /** 172 * Interface used by CameraService 173 */ 174 175 CameraDeviceClient(const sp<CameraService>& cameraService, 176 const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback, 177 const String16& clientPackageName, 178 const std::unique_ptr<String16>& clientFeatureId, 179 const String8& cameraId, 180 int cameraFacing, 181 int clientPid, 182 uid_t clientUid, 183 int servicePid); 184 virtual ~CameraDeviceClient(); 185 186 virtual status_t initialize(sp<CameraProviderManager> manager, 187 const String8& monitorTags) override; 188 189 virtual status_t setRotateAndCropOverride(uint8_t rotateAndCrop) override; 190 191 virtual status_t dump(int fd, const Vector<String16>& args); 192 193 virtual status_t dumpClient(int fd, const Vector<String16>& args); 194 195 /** 196 * Device listener interface 197 */ 198 199 virtual void notifyIdle(); 200 virtual void notifyError(int32_t errorCode, 201 const CaptureResultExtras& resultExtras); 202 virtual void notifyShutter(const CaptureResultExtras& resultExtras, nsecs_t timestamp); 203 virtual void notifyPrepared(int streamId); 204 virtual void notifyRequestQueueEmpty(); 205 virtual void notifyRepeatingRequestError(long lastFrameNumber); 206 207 // utility function to convert AIDL SessionConfiguration to HIDL 208 // streamConfiguration. Also checks for validity of SessionConfiguration and 209 // returns a non-ok binder::Status if the passed in session configuration 210 // isn't valid. 211 static binder::Status 212 convertToHALStreamCombination(const SessionConfiguration& sessionConfiguration, 213 const String8 &cameraId, const CameraMetadata &deviceInfo, 214 metadataGetter getMetadata, const std::vector<std::string> &physicalCameraIds, 215 hardware::camera::device::V3_4::StreamConfiguration &streamConfiguration, 216 bool *earlyExit); 217 /** 218 * Interface used by independent components of CameraDeviceClient. 219 */ 220 protected: 221 /** FilteredListener implementation **/ 222 virtual void onResultAvailable(const CaptureResult& result); 223 virtual void detachDevice(); 224 225 // Calculate the ANativeWindow transform from android.sensor.orientation 226 status_t getRotationTransformLocked(/*out*/int32_t* transform); 227 228 private: 229 // StreamSurfaceId encapsulates streamId + surfaceId for a particular surface. 230 // streamId specifies the index of the stream the surface belongs to, and the 231 // surfaceId specifies the index of the surface within the stream. (one stream 232 // could contain multiple surfaces.) 233 class StreamSurfaceId final { 234 public: StreamSurfaceId()235 StreamSurfaceId() { 236 mStreamId = -1; 237 mSurfaceId = -1; 238 } StreamSurfaceId(int32_t streamId,int32_t surfaceId)239 StreamSurfaceId(int32_t streamId, int32_t surfaceId) { 240 mStreamId = streamId; 241 mSurfaceId = surfaceId; 242 } streamId()243 int32_t streamId() const { 244 return mStreamId; 245 } surfaceId()246 int32_t surfaceId() const { 247 return mSurfaceId; 248 } 249 250 private: 251 int32_t mStreamId; 252 int32_t mSurfaceId; 253 254 }; // class StreamSurfaceId 255 256 private: 257 /** ICameraDeviceUser interface-related private members */ 258 259 /** Preview callback related members */ 260 sp<camera2::FrameProcessorBase> mFrameProcessor; 261 262 std::vector<int32_t> mSupportedPhysicalRequestKeys; 263 264 template<typename TProviderPtr> 265 status_t initializeImpl(TProviderPtr providerPtr, const String8& monitorTags); 266 267 /** Utility members */ 268 binder::Status checkPidStatus(const char* checkLocation); 269 static binder::Status checkOperatingMode(int operatingMode, const CameraMetadata &staticInfo, 270 const String8 &cameraId); 271 static binder::Status checkSurfaceType(size_t numBufferProducers, bool deferredConsumer, 272 int surfaceType); 273 static void mapStreamInfo(const OutputStreamInfo &streamInfo, 274 camera3_stream_rotation_t rotation, String8 physicalId, 275 hardware::camera::device::V3_4::Stream *stream /*out*/); 276 bool enforceRequestPermissions(CameraMetadata& metadata); 277 278 // Find the square of the euclidean distance between two points 279 static int64_t euclidDistSquare(int32_t x0, int32_t y0, int32_t x1, int32_t y1); 280 281 // Create an output stream with surface deferred for future. 282 binder::Status createDeferredSurfaceStreamLocked( 283 const hardware::camera2::params::OutputConfiguration &outputConfiguration, 284 bool isShared, 285 int* newStreamId = NULL); 286 287 // Set the stream transform flags to automatically rotate the camera stream for preview use 288 // cases. 289 binder::Status setStreamTransformLocked(int streamId); 290 291 // Find the closest dimensions for a given format in available stream configurations with 292 // a width <= ROUNDING_WIDTH_CAP 293 static const int32_t ROUNDING_WIDTH_CAP = 1920; 294 static bool roundBufferDimensionNearest(int32_t width, int32_t height, int32_t format, 295 android_dataspace dataSpace, const CameraMetadata& info, 296 /*out*/int32_t* outWidth, /*out*/int32_t* outHeight); 297 298 //check if format is not custom format 299 static bool isPublicFormat(int32_t format); 300 301 // Create a Surface from an IGraphicBufferProducer. Returns error if 302 // IGraphicBufferProducer's property doesn't match with streamInfo 303 static binder::Status createSurfaceFromGbp(OutputStreamInfo& streamInfo, bool isStreamInfoValid, 304 sp<Surface>& surface, const sp<IGraphicBufferProducer>& gbp, const String8 &cameraId, 305 const CameraMetadata &physicalCameraMetadata); 306 307 308 // Utility method to insert the surface into SurfaceMap 309 binder::Status insertGbpLocked(const sp<IGraphicBufferProducer>& gbp, 310 /*out*/SurfaceMap* surfaceMap, /*out*/Vector<int32_t>* streamIds, 311 /*out*/int32_t* currentStreamId); 312 313 // Check that the physicalCameraId passed in is spported by the camera 314 // device. 315 static binder::Status checkPhysicalCameraId(const std::vector<std::string> &physicalCameraIds, 316 const String8 &physicalCameraId, const String8 &logicalCameraId); 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 struct InputStreamConfiguration { 325 bool configured; 326 int32_t width; 327 int32_t height; 328 int32_t format; 329 int32_t id; 330 } mInputStream; 331 332 // Streaming request ID 333 int32_t mStreamingRequestId; 334 Mutex mStreamingRequestIdLock; 335 static const int32_t REQUEST_ID_NONE = -1; 336 337 int32_t mRequestIdCounter; 338 339 // The list of output streams whose surfaces are deferred. We have to track them separately 340 // as there are no surfaces available and can not be put into mStreamMap. Once the deferred 341 // Surface is configured, the stream id will be moved to mStreamMap. 342 Vector<int32_t> mDeferredStreams; 343 344 // stream ID -> outputStreamInfo mapping 345 std::unordered_map<int32_t, OutputStreamInfo> mStreamInfoMap; 346 347 KeyedVector<sp<IBinder>, sp<CompositeStream>> mCompositeStreamMap; 348 349 static const int32_t MAX_SURFACES_PER_STREAM = 4; 350 sp<CameraProviderManager> mProviderManager; 351 }; 352 353 }; // namespace android 354 355 #endif 356