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