• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 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 namespace android {
30 
31 struct CameraDeviceClientBase :
32          public CameraService::BasicClient,
33          public hardware::camera2::BnCameraDeviceUser
34 {
35     typedef hardware::camera2::ICameraDeviceCallbacks TCamCallbacks;
36 
getRemoteCallbackCameraDeviceClientBase37     const sp<hardware::camera2::ICameraDeviceCallbacks>& getRemoteCallback() {
38         return mRemoteCallback;
39     }
40 
41 protected:
42     CameraDeviceClientBase(const sp<CameraService>& cameraService,
43             const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback,
44             const String16& clientPackageName,
45             int cameraId,
46             int cameraFacing,
47             int clientPid,
48             uid_t clientUid,
49             int servicePid);
50 
51     sp<hardware::camera2::ICameraDeviceCallbacks> mRemoteCallback;
52 };
53 
54 /**
55  * Implements the binder ICameraDeviceUser API,
56  * meant for HAL3-public implementation of
57  * android.hardware.photography.CameraDevice
58  */
59 class CameraDeviceClient :
60         public Camera2ClientBase<CameraDeviceClientBase>,
61         public camera2::FrameProcessorBase::FilteredListener
62 {
63 public:
64     /**
65      * ICameraDeviceUser interface (see ICameraDeviceUser for details)
66      */
67 
68     // Note that the callee gets a copy of the metadata.
69     virtual binder::Status submitRequest(
70             const hardware::camera2::CaptureRequest& request,
71             bool streaming = false,
72             /*out*/
73             hardware::camera2::utils::SubmitInfo *submitInfo = nullptr);
74     // List of requests are copied.
75     virtual binder::Status submitRequestList(
76             const std::vector<hardware::camera2::CaptureRequest>& requests,
77             bool streaming = false,
78             /*out*/
79             hardware::camera2::utils::SubmitInfo *submitInfo = nullptr);
80     virtual binder::Status cancelRequest(int requestId,
81             /*out*/
82             int64_t* lastFrameNumber = NULL);
83 
84     virtual binder::Status beginConfigure();
85 
86     virtual binder::Status endConfigure(bool isConstrainedHighSpeed = false);
87 
88     // Returns -EBUSY if device is not idle
89     virtual binder::Status deleteStream(int streamId);
90 
91     virtual binder::Status createStream(
92             const hardware::camera2::params::OutputConfiguration &outputConfiguration,
93             /*out*/
94             int32_t* newStreamId = NULL);
95 
96     // Create an input stream of width, height, and format.
97     virtual binder::Status createInputStream(int width, int height, int format,
98             /*out*/
99             int32_t* newStreamId = NULL);
100 
101     // Get the buffer producer of the input stream
102     virtual binder::Status getInputSurface(
103             /*out*/
104             view::Surface *inputSurface);
105 
106     // Create a request object from a template.
107     virtual binder::Status createDefaultRequest(int templateId,
108             /*out*/
109             hardware::camera2::impl::CameraMetadataNative* request);
110 
111     // Get the static metadata for the camera
112     // -- Caller owns the newly allocated metadata
113     virtual binder::Status getCameraInfo(
114             /*out*/
115             hardware::camera2::impl::CameraMetadataNative* cameraCharacteristics);
116 
117     // Wait until all the submitted requests have finished processing
118     virtual binder::Status waitUntilIdle();
119 
120     // Flush all active and pending requests as fast as possible
121     virtual binder::Status flush(
122             /*out*/
123             int64_t* lastFrameNumber = NULL);
124 
125     // Prepare stream by preallocating its buffers
126     virtual binder::Status prepare(int32_t streamId);
127 
128     // Tear down stream resources by freeing its unused buffers
129     virtual binder::Status tearDown(int32_t streamId);
130 
131     // Prepare stream by preallocating up to maxCount of its buffers
132     virtual binder::Status prepare2(int32_t maxCount, int32_t streamId);
133 
134     // Set the deferred surface for a stream.
135     virtual binder::Status setDeferredConfiguration(int32_t streamId,
136             const hardware::camera2::params::OutputConfiguration &outputConfiguration);
137 
138     /**
139      * Interface used by CameraService
140      */
141 
142     CameraDeviceClient(const sp<CameraService>& cameraService,
143             const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback,
144             const String16& clientPackageName,
145             int cameraId,
146             int cameraFacing,
147             int clientPid,
148             uid_t clientUid,
149             int servicePid);
150     virtual ~CameraDeviceClient();
151 
152     virtual status_t      initialize(CameraModule *module);
153 
154     virtual status_t      dump(int fd, const Vector<String16>& args);
155 
156     virtual status_t      dumpClient(int fd, const Vector<String16>& args);
157 
158     /**
159      * Device listener interface
160      */
161 
162     virtual void notifyIdle();
163     virtual void notifyError(int32_t errorCode,
164                              const CaptureResultExtras& resultExtras);
165     virtual void notifyShutter(const CaptureResultExtras& resultExtras, nsecs_t timestamp);
166     virtual void notifyPrepared(int streamId);
167     virtual void notifyRepeatingRequestError(long lastFrameNumber);
168 
169     /**
170      * Interface used by independent components of CameraDeviceClient.
171      */
172 protected:
173     /** FilteredListener implementation **/
174     virtual void          onResultAvailable(const CaptureResult& result);
175     virtual void          detachDevice();
176 
177     // Calculate the ANativeWindow transform from android.sensor.orientation
178     status_t              getRotationTransformLocked(/*out*/int32_t* transform);
179 
180 private:
181     /** ICameraDeviceUser interface-related private members */
182 
183     /** Preview callback related members */
184     sp<camera2::FrameProcessorBase> mFrameProcessor;
185     static const int32_t FRAME_PROCESSOR_LISTENER_MIN_ID = 0;
186     static const int32_t FRAME_PROCESSOR_LISTENER_MAX_ID = 0x7fffffffL;
187 
188     /** Utility members */
189     binder::Status checkPidStatus(const char* checkLocation);
190     bool enforceRequestPermissions(CameraMetadata& metadata);
191 
192     // Find the square of the euclidean distance between two points
193     static int64_t euclidDistSquare(int32_t x0, int32_t y0, int32_t x1, int32_t y1);
194 
195     // Create an output stream with surface deferred for future.
196     binder::Status createDeferredSurfaceStreamLocked(
197             const hardware::camera2::params::OutputConfiguration &outputConfiguration,
198             int* newStreamId = NULL);
199 
200     // Set the stream transform flags to automatically rotate the camera stream for preview use
201     // cases.
202     binder::Status setStreamTransformLocked(int streamId);
203 
204     // Find the closest dimensions for a given format in available stream configurations with
205     // a width <= ROUNDING_WIDTH_CAP
206     static const int32_t ROUNDING_WIDTH_CAP = 1920;
207     static bool roundBufferDimensionNearest(int32_t width, int32_t height, int32_t format,
208             android_dataspace dataSpace, const CameraMetadata& info,
209             /*out*/int32_t* outWidth, /*out*/int32_t* outHeight);
210 
211     // IGraphicsBufferProducer binder -> Stream ID for output streams
212     KeyedVector<sp<IBinder>, int> mStreamMap;
213 
214     struct InputStreamConfiguration {
215         bool configured;
216         int32_t width;
217         int32_t height;
218         int32_t format;
219         int32_t id;
220     } mInputStream;
221 
222     // Streaming request ID
223     int32_t mStreamingRequestId;
224     Mutex mStreamingRequestIdLock;
225     static const int32_t REQUEST_ID_NONE = -1;
226 
227     int32_t mRequestIdCounter;
228 
229     // The list of output streams whose surfaces are deferred. We have to track them separately
230     // as there are no surfaces available and can not be put into mStreamMap. Once the deferred
231     // Surface is configured, the stream id will be moved to mStreamMap.
232     Vector<int32_t> mDeferredStreams;
233 };
234 
235 }; // namespace android
236 
237 #endif
238