1 /*
2  * Copyright (C) 2012 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 HW_EMULATOR_CAMERA_EMULATED_CAMERA2_H
18 #define HW_EMULATOR_CAMERA_EMULATED_CAMERA2_H
19 
20 /*
21  * Contains declaration of a class EmulatedCamera that encapsulates
22  * functionality common to all version 2.0 emulated camera devices.  Instances
23  * of this class (for each emulated camera) are created during the construction
24  * of the EmulatedCameraFactory instance.  This class serves as an entry point
25  * for all camera API calls that defined by camera2_device_ops_t API.
26  */
27 
28 #include "hardware/camera2.h"
29 #include "system/camera_metadata.h"
30 #include "EmulatedBaseCamera.h"
31 #include <utils/Thread.h>
32 #include <utils/Mutex.h>
33 
34 namespace android {
35 
36 /* Encapsulates functionality common to all version 2.0 emulated camera devices
37  *
38  * Note that EmulatedCameraFactory instantiates object of this class just once,
39  * when EmulatedCameraFactory instance gets constructed. Connection to /
40  * disconnection from the actual camera device is handled by calls to
41  * connectDevice(), and closeCamera() methods of this class that are invoked in
42  * response to hw_module_methods_t::open, and camera_device::close callbacks.
43  */
44 class EmulatedCamera2 : public camera2_device, public EmulatedBaseCamera {
45 public:
46     /* Constructs EmulatedCamera2 instance.
47      * Param:
48      *  cameraId - Zero based camera identifier, which is an index of the camera
49      *      instance in camera factory's array.
50      *  module - Emulated camera HAL module descriptor.
51      */
52     EmulatedCamera2(int cameraId,
53             struct hw_module_t* module);
54 
55     /* Destructs EmulatedCamera2 instance. */
56     virtual ~EmulatedCamera2();
57 
58     /****************************************************************************
59      * Abstract API
60      ***************************************************************************/
61 
62 public:
63 
64     /****************************************************************************
65      * Public API
66      ***************************************************************************/
67 
68 public:
69     virtual status_t Initialize();
70 
71     /****************************************************************************
72      * Camera module API and generic hardware device API implementation
73      ***************************************************************************/
74 
75 public:
76     virtual status_t connectCamera(hw_device_t** device);
77 
78     virtual status_t closeCamera();
79 
80     virtual status_t getCameraInfo(struct camera_info* info) = 0;
81 
82     /****************************************************************************
83      * Camera API implementation.
84      * These methods are called from the camera API callback routines.
85      ***************************************************************************/
86 
87 protected:
88     /** Request input queue notification */
89     virtual int requestQueueNotify();
90 
91     /** Count of requests in flight */
92     virtual int getInProgressCount();
93 
94     /** Cancel all captures in flight */
95     virtual int flushCapturesInProgress();
96 
97     virtual int constructDefaultRequest(
98         int request_template,
99         camera_metadata_t **request);
100 
101     /** Output stream creation and management */
102     virtual int allocateStream(
103             uint32_t width,
104             uint32_t height,
105             int format,
106             const camera2_stream_ops_t *stream_ops,
107             uint32_t *stream_id,
108             uint32_t *format_actual,
109             uint32_t *usage,
110             uint32_t *max_buffers);
111 
112     virtual int registerStreamBuffers(
113             uint32_t stream_id,
114             int num_buffers,
115             buffer_handle_t *buffers);
116 
117     virtual int releaseStream(uint32_t stream_id);
118 
119     /** Input stream creation and management */
120     virtual int allocateReprocessStream(
121             uint32_t width,
122             uint32_t height,
123             uint32_t format,
124             const camera2_stream_in_ops_t *reprocess_stream_ops,
125             uint32_t *stream_id,
126             uint32_t *consumer_usage,
127             uint32_t *max_buffers);
128 
129     virtual int allocateReprocessStreamFromStream(
130             uint32_t output_stream_id,
131             const camera2_stream_in_ops_t *reprocess_stream_ops,
132             uint32_t *stream_id);
133 
134     virtual int releaseReprocessStream(uint32_t stream_id);
135 
136     /** 3A action triggering */
137     virtual int triggerAction(uint32_t trigger_id,
138             int32_t ext1, int32_t ext2);
139 
140     /** Custom tag definitions */
141     virtual const char* getVendorSectionName(uint32_t tag);
142     virtual const char* getVendorTagName(uint32_t tag);
143     virtual int         getVendorTagType(uint32_t tag);
144 
145     /** Debug methods */
146 
147     virtual int dump(int fd);
148 
149     /****************************************************************************
150      * Camera API callbacks as defined by camera2_device_ops structure.  See
151      * hardware/libhardware/include/hardware/camera2.h for information on each
152      * of these callbacks. Implemented in this class, these callbacks simply
153      * dispatch the call into an instance of EmulatedCamera2 class defined in
154      * the 'camera_device2' parameter.
155      ***************************************************************************/
156 
157 private:
158     /** Input request queue */
159     static int set_request_queue_src_ops(const camera2_device_t *,
160             const camera2_request_queue_src_ops *queue_src_ops);
161     static int notify_request_queue_not_empty(const camera2_device_t *);
162 
163     /** Output frame queue */
164     static int set_frame_queue_dst_ops(const camera2_device_t *,
165             const camera2_frame_queue_dst_ops *queue_dst_ops);
166 
167     /** In-progress request management */
168     static int get_in_progress_count(const camera2_device_t *);
169 
170     static int flush_captures_in_progress(const camera2_device_t *);
171 
172     /** Request template creation */
173     static int construct_default_request(const camera2_device_t *,
174             int request_template,
175             camera_metadata_t **request);
176 
177     /** Stream management */
178     static int allocate_stream(const camera2_device_t *,
179             uint32_t width,
180             uint32_t height,
181             int format,
182             const camera2_stream_ops_t *stream_ops,
183             uint32_t *stream_id,
184             uint32_t *format_actual,
185             uint32_t *usage,
186             uint32_t *max_buffers);
187 
188     static int register_stream_buffers(const camera2_device_t *,
189             uint32_t stream_id,
190             int num_buffers,
191             buffer_handle_t *buffers);
192 
193     static int release_stream(const camera2_device_t *,
194             uint32_t stream_id);
195 
196     static int allocate_reprocess_stream(const camera2_device_t *,
197             uint32_t width,
198             uint32_t height,
199             uint32_t format,
200             const camera2_stream_in_ops_t *reprocess_stream_ops,
201             uint32_t *stream_id,
202             uint32_t *consumer_usage,
203             uint32_t *max_buffers);
204 
205     static int allocate_reprocess_stream_from_stream(const camera2_device_t *,
206             uint32_t output_stream_id,
207             const camera2_stream_in_ops_t *reprocess_stream_ops,
208             uint32_t *stream_id);
209 
210     static int release_reprocess_stream(const camera2_device_t *,
211             uint32_t stream_id);
212 
213     /** 3A triggers*/
214     static int trigger_action(const camera2_device_t *,
215             uint32_t trigger_id,
216             int ext1,
217             int ext2);
218 
219     /** Notifications to application */
220     static int set_notify_callback(const camera2_device_t *,
221             camera2_notify_callback notify_cb,
222             void *user);
223 
224     /** Vendor metadata registration */
225     static int get_metadata_vendor_tag_ops(const camera2_device_t *,
226             vendor_tag_query_ops_t **ops);
227     // for get_metadata_vendor_tag_ops
228     static const char* get_camera_vendor_section_name(
229             const vendor_tag_query_ops_t *,
230             uint32_t tag);
231     static const char* get_camera_vendor_tag_name(
232             const vendor_tag_query_ops_t *,
233             uint32_t tag);
234     static int get_camera_vendor_tag_type(
235             const vendor_tag_query_ops_t *,
236             uint32_t tag);
237 
238     static int dump(const camera2_device_t *, int fd);
239 
240     /** For hw_device_t ops */
241     static int close(struct hw_device_t* device);
242 
243     /****************************************************************************
244      * Data members shared with implementations
245      ***************************************************************************/
246   protected:
247     /** Mutex for calls through camera2 device interface */
248     Mutex mMutex;
249 
250     bool mStatusPresent;
251 
252     const camera2_request_queue_src_ops *mRequestQueueSrc;
253     const camera2_frame_queue_dst_ops *mFrameQueueDst;
254 
255     struct TagOps : public vendor_tag_query_ops {
256         EmulatedCamera2 *parent;
257     };
258     TagOps      mVendorTagOps;
259 
260     void sendNotification(int32_t msgType,
261             int32_t ext1, int32_t ext2, int32_t ext3);
262 
263     /****************************************************************************
264      * Data members
265      ***************************************************************************/
266   private:
267     static camera2_device_ops_t sDeviceOps;
268     camera2_notify_callback mNotifyCb;
269     void* mNotifyUserPtr;
270 };
271 
272 }; /* namespace android */
273 
274 #endif  /* HW_EMULATOR_CAMERA_EMULATED_CAMERA2_H */
275