• 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 
26 #include "CameraOfflineSessionClient.h"
27 #include "CameraService.h"
28 #include "common/FrameProcessorBase.h"
29 #include "common/Camera2ClientBase.h"
30 #include "CompositeStream.h"
31 #include "utils/SessionConfigurationUtils.h"
32 
33 using android::camera3::OutputStreamInfo;
34 using android::camera3::CompositeStream;
35 
36 namespace android {
37 
38 struct CameraDeviceClientBase :
39          public CameraService::BasicClient,
40          public hardware::camera2::BnCameraDeviceUser
41 {
42     typedef hardware::camera2::ICameraDeviceCallbacks TCamCallbacks;
43 
getRemoteCallbackCameraDeviceClientBase44     const sp<hardware::camera2::ICameraDeviceCallbacks>& getRemoteCallback() {
45         return mRemoteCallback;
46     }
47 
48 protected:
49     CameraDeviceClientBase(const sp<CameraService>& cameraService,
50             const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback,
51             const String16& clientPackageName,
52             const std::optional<String16>& clientFeatureId,
53             const String8& cameraId,
54             int api1CameraId,
55             int cameraFacing,
56             int sensorOrientation,
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             int64_t startTimeMs,
99             /*out*/
100             std::vector<int>* offlineStreamIds) override;
101 
102     // Verify specific session configuration.
103     virtual binder::Status isSessionConfigurationSupported(
104             const SessionConfiguration& sessionConfiguration,
105             /*out*/
106             bool* streamStatus) override;
107 
108     // Returns -EBUSY if device is not idle or in error state
109     virtual binder::Status deleteStream(int streamId) override;
110 
111     virtual binder::Status createStream(
112             const hardware::camera2::params::OutputConfiguration &outputConfiguration,
113             /*out*/
114             int32_t* newStreamId = NULL) override;
115 
116     // Create an input stream of width, height, and format.
117     virtual binder::Status createInputStream(int width, int height, int format,
118             bool isMultiResolution,
119             /*out*/
120             int32_t* newStreamId = NULL) override;
121 
122     // Get the buffer producer of the input stream
123     virtual binder::Status getInputSurface(
124             /*out*/
125             view::Surface *inputSurface) override;
126 
127     // Create a request object from a template.
128     virtual binder::Status createDefaultRequest(int templateId,
129             /*out*/
130             hardware::camera2::impl::CameraMetadataNative* request) override;
131 
132     // Get the static metadata for the camera
133     // -- Caller owns the newly allocated metadata
134     virtual binder::Status getCameraInfo(
135             /*out*/
136             hardware::camera2::impl::CameraMetadataNative* cameraCharacteristics) override;
137 
138     // Wait until all the submitted requests have finished processing
139     virtual binder::Status waitUntilIdle() override;
140 
141     // Flush all active and pending requests as fast as possible
142     virtual binder::Status flush(
143             /*out*/
144             int64_t* lastFrameNumber = NULL) override;
145 
146     // Prepare stream by preallocating its buffers
147     virtual binder::Status prepare(int32_t streamId) override;
148 
149     // Tear down stream resources by freeing its unused buffers
150     virtual binder::Status tearDown(int32_t streamId) override;
151 
152     // Prepare stream by preallocating up to maxCount of its buffers
153     virtual binder::Status prepare2(int32_t maxCount, int32_t streamId) override;
154 
155     // Update an output configuration
156     virtual binder::Status updateOutputConfiguration(int streamId,
157             const hardware::camera2::params::OutputConfiguration &outputConfiguration) override;
158 
159     // Finalize the output configurations with surfaces not added before.
160     virtual binder::Status finalizeOutputConfigurations(int32_t streamId,
161             const hardware::camera2::params::OutputConfiguration &outputConfiguration) override;
162 
163     virtual binder::Status setCameraAudioRestriction(int32_t mode) override;
164 
165     virtual binder::Status getGlobalAudioRestriction(/*out*/int32_t* outMode) override;
166 
167     virtual binder::Status switchToOffline(
168             const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb,
169             const std::vector<int>& offlineOutputIds,
170             /*out*/
171             sp<hardware::camera2::ICameraOfflineSession>* session) override;
172 
173     /**
174      * Interface used by CameraService
175      */
176 
177     CameraDeviceClient(const sp<CameraService>& cameraService,
178             const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback,
179             const String16& clientPackageName,
180             const std::optional<String16>& clientFeatureId,
181             const String8& cameraId,
182             int cameraFacing,
183             int sensorOrientation,
184             int clientPid,
185             uid_t clientUid,
186             int servicePid,
187             bool overrideForPerfClass);
188     virtual ~CameraDeviceClient();
189 
190     virtual status_t      initialize(sp<CameraProviderManager> manager,
191             const String8& monitorTags) override;
192 
193     virtual status_t      setRotateAndCropOverride(uint8_t rotateAndCrop) override;
194 
195     virtual bool          supportsCameraMute();
196     virtual status_t      setCameraMute(bool enabled);
197 
198     virtual status_t      dump(int fd, const Vector<String16>& args);
199 
200     virtual status_t      dumpClient(int fd, const Vector<String16>& args);
201 
202     /**
203      * Device listener interface
204      */
205 
206     virtual void notifyIdle(int64_t requestCount, int64_t resultErrorCount, bool deviceError,
207                             const std::vector<hardware::CameraStreamStats>& streamStats);
208     virtual void notifyError(int32_t errorCode,
209                              const CaptureResultExtras& resultExtras);
210     virtual void notifyShutter(const CaptureResultExtras& resultExtras, nsecs_t timestamp);
211     virtual void notifyPrepared(int streamId);
212     virtual void notifyRequestQueueEmpty();
213     virtual void notifyRepeatingRequestError(long lastFrameNumber);
214 
setImageDumpMask(int mask)215     void setImageDumpMask(int mask) { if (mDevice != nullptr) mDevice->setImageDumpMask(mask); }
216     /**
217      * Interface used by independent components of CameraDeviceClient.
218      */
219 protected:
220     /** FilteredListener implementation **/
221     virtual void          onResultAvailable(const CaptureResult& result);
222     virtual void          detachDevice();
223 
224     // Calculate the ANativeWindow transform from android.sensor.orientation
225     status_t              getRotationTransformLocked(/*out*/int32_t* transform);
226 
227     bool isUltraHighResolutionSensor(const String8 &cameraId);
228 
229     bool isSensorPixelModeConsistent(const std::list<int> &streamIdList,
230             const CameraMetadata &settings);
231 
232     const CameraMetadata &getStaticInfo(const String8 &cameraId);
233 
234 private:
235     // StreamSurfaceId encapsulates streamId + surfaceId for a particular surface.
236     // streamId specifies the index of the stream the surface belongs to, and the
237     // surfaceId specifies the index of the surface within the stream. (one stream
238     // could contain multiple surfaces.)
239     class StreamSurfaceId final {
240     public:
StreamSurfaceId()241         StreamSurfaceId() {
242             mStreamId = -1;
243             mSurfaceId = -1;
244         }
StreamSurfaceId(int32_t streamId,int32_t surfaceId)245         StreamSurfaceId(int32_t streamId, int32_t surfaceId) {
246             mStreamId = streamId;
247             mSurfaceId = surfaceId;
248         }
streamId()249         int32_t streamId() const {
250             return mStreamId;
251         }
surfaceId()252         int32_t surfaceId() const {
253             return mSurfaceId;
254         }
255 
256     private:
257         int32_t mStreamId;
258         int32_t mSurfaceId;
259 
260     }; // class StreamSurfaceId
261 
262 private:
263     /** ICameraDeviceUser interface-related private members */
264 
265     /** Preview callback related members */
266     sp<camera2::FrameProcessorBase> mFrameProcessor;
267 
268     std::vector<int32_t> mSupportedPhysicalRequestKeys;
269 
270     template<typename TProviderPtr>
271     status_t      initializeImpl(TProviderPtr providerPtr, const String8& monitorTags);
272 
273     /** Utility members */
274     binder::Status checkPidStatus(const char* checkLocation);
275     bool enforceRequestPermissions(CameraMetadata& metadata);
276 
277     // Create an output stream with surface deferred for future.
278     binder::Status createDeferredSurfaceStreamLocked(
279             const hardware::camera2::params::OutputConfiguration &outputConfiguration,
280             bool isShared,
281             int* newStreamId = NULL);
282 
283     // Set the stream transform flags to automatically rotate the camera stream for preview use
284     // cases.
285     binder::Status setStreamTransformLocked(int streamId);
286 
287     // Utility method to insert the surface into SurfaceMap
288     binder::Status insertGbpLocked(const sp<IGraphicBufferProducer>& gbp,
289             /*out*/SurfaceMap* surfaceMap, /*out*/Vector<int32_t>* streamIds,
290             /*out*/int32_t*  currentStreamId);
291 
292     // Utility method that maps AIDL request templates.
293     binder::Status mapRequestTemplate(int templateId,
294             camera_request_template_t* tempId /*out*/);
295 
296     // IGraphicsBufferProducer binder -> Stream ID + Surface ID for output streams
297     KeyedVector<sp<IBinder>, StreamSurfaceId> mStreamMap;
298 
299     // Stream ID -> OutputConfiguration. Used for looking up Surface by stream/surface index
300     KeyedVector<int32_t, hardware::camera2::params::OutputConfiguration> mConfiguredOutputs;
301 
302     struct InputStreamConfiguration {
303         bool configured;
304         int32_t width;
305         int32_t height;
306         int32_t format;
307         int32_t id;
308     } mInputStream;
309 
310     // Streaming request ID
311     int32_t mStreamingRequestId;
312     Mutex mStreamingRequestIdLock;
313     static const int32_t REQUEST_ID_NONE = -1;
314 
315     int32_t mRequestIdCounter;
316 
317     std::vector<std::string> mPhysicalCameraIds;
318 
319     // The list of output streams whose surfaces are deferred. We have to track them separately
320     // as there are no surfaces available and can not be put into mStreamMap. Once the deferred
321     // Surface is configured, the stream id will be moved to mStreamMap.
322     Vector<int32_t> mDeferredStreams;
323 
324     // stream ID -> outputStreamInfo mapping
325     std::unordered_map<int32_t, OutputStreamInfo> mStreamInfoMap;
326 
327     // map high resolution camera id (logical / physical) -> list of stream ids configured
328     std::unordered_map<std::string, std::unordered_set<int>> mHighResolutionCameraIdToStreamIdSet;
329 
330     // set of high resolution camera id (logical / physical)
331     std::unordered_set<std::string> mHighResolutionSensors;
332 
333     KeyedVector<sp<IBinder>, sp<CompositeStream>> mCompositeStreamMap;
334 
335     sp<CameraProviderManager> mProviderManager;
336 
337     // Override the camera characteristics for performance class primary cameras.
338     bool mOverrideForPerfClass;
339 };
340 
341 }; // namespace android
342 
343 #endif
344