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