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 <utils/Mutex.h>
29 #include <utils/Thread.h>
30 #include "EmulatedBaseCamera.h"
31 #include "hardware/camera2.h"
32 #include "system/camera_metadata.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, struct hw_module_t *module);
53 
54   /* Destructs EmulatedCamera2 instance. */
55   virtual ~EmulatedCamera2();
56 
57   /****************************************************************************
58    * Abstract API
59    ***************************************************************************/
60 
61  public:
62   /****************************************************************************
63    * Public API
64    ***************************************************************************/
65 
66  public:
67   virtual status_t Initialize(const cuttlefish::CameraDefinition &props);
68 
69   /****************************************************************************
70    * Camera module API and generic hardware device API implementation
71    ***************************************************************************/
72 
73  public:
74   virtual status_t connectCamera(hw_device_t **device);
75 
76   virtual status_t closeCamera();
77 
78   virtual status_t getCameraInfo(struct camera_info *info) = 0;
79 
80   /****************************************************************************
81    * Camera API implementation.
82    * These methods are called from the camera API callback routines.
83    ***************************************************************************/
84 
85  protected:
86   /** Request input queue notification */
87   virtual int requestQueueNotify();
88 
89   /** Count of requests in flight */
90   virtual int getInProgressCount();
91 
92   /** Cancel all captures in flight */
93   virtual int flushCapturesInProgress();
94 
95   virtual int constructDefaultRequest(int request_template,
96                                       camera_metadata_t **request);
97 
98   /** Output stream creation and management */
99   virtual int allocateStream(uint32_t width, uint32_t height, int format,
100                              const camera2_stream_ops_t *stream_ops,
101                              uint32_t *stream_id, uint32_t *format_actual,
102                              uint32_t *usage, uint32_t *max_buffers);
103 
104   virtual int registerStreamBuffers(uint32_t stream_id, int num_buffers,
105                                     buffer_handle_t *buffers);
106 
107   virtual int releaseStream(uint32_t stream_id);
108 
109   /** Input stream creation and management */
110   virtual int allocateReprocessStream(
111       uint32_t width, uint32_t height, uint32_t format,
112       const camera2_stream_in_ops_t *reprocess_stream_ops, uint32_t *stream_id,
113       uint32_t *consumer_usage, uint32_t *max_buffers);
114 
115   virtual int allocateReprocessStreamFromStream(
116       uint32_t output_stream_id,
117       const camera2_stream_in_ops_t *reprocess_stream_ops, uint32_t *stream_id);
118 
119   virtual int releaseReprocessStream(uint32_t stream_id);
120 
121   /** 3A action triggering */
122   virtual int triggerAction(uint32_t trigger_id, int32_t ext1, int32_t ext2);
123 
124   /** Custom tag definitions */
125   virtual const char *getVendorSectionName(uint32_t tag);
126   virtual const char *getVendorTagName(uint32_t tag);
127   virtual int getVendorTagType(uint32_t tag);
128 
129   /** Debug methods */
130 
131   virtual int dump(int fd);
132 
133   /****************************************************************************
134    * Camera API callbacks as defined by camera2_device_ops structure.  See
135    * hardware/libhardware/include/hardware/camera2.h for information on each
136    * of these callbacks. Implemented in this class, these callbacks simply
137    * dispatch the call into an instance of EmulatedCamera2 class defined in
138    * the 'camera_device2' parameter.
139    ***************************************************************************/
140 
141  private:
142   /** Input request queue */
143   static int set_request_queue_src_ops(
144       const camera2_device_t *,
145       const camera2_request_queue_src_ops *queue_src_ops);
146   static int notify_request_queue_not_empty(const camera2_device_t *);
147 
148   /** Output frame queue */
149   static int set_frame_queue_dst_ops(
150       const camera2_device_t *,
151       const camera2_frame_queue_dst_ops *queue_dst_ops);
152 
153   /** In-progress request management */
154   static int get_in_progress_count(const camera2_device_t *);
155 
156   static int get_instance_metadata(const struct camera2_device *,
157                                    camera_metadata **);
158 
159   static int flush_captures_in_progress(const camera2_device_t *);
160 
161   /** Request template creation */
162   static int construct_default_request(const camera2_device_t *,
163                                        int request_template,
164                                        camera_metadata_t **request);
165 
166   /** Stream management */
167   static int allocate_stream(const camera2_device_t *, uint32_t width,
168                              uint32_t height, int format,
169                              const camera2_stream_ops_t *stream_ops,
170                              uint32_t *stream_id, uint32_t *format_actual,
171                              uint32_t *usage, uint32_t *max_buffers);
172 
173   static int register_stream_buffers(const camera2_device_t *,
174                                      uint32_t stream_id, int num_buffers,
175                                      buffer_handle_t *buffers);
176 
177   static int release_stream(const camera2_device_t *, uint32_t stream_id);
178 
179   static int allocate_reprocess_stream(
180       const camera2_device_t *, uint32_t width, uint32_t height,
181       uint32_t format, const camera2_stream_in_ops_t *reprocess_stream_ops,
182       uint32_t *stream_id, uint32_t *consumer_usage, uint32_t *max_buffers);
183 
184   static int allocate_reprocess_stream_from_stream(
185       const camera2_device_t *, uint32_t output_stream_id,
186       const camera2_stream_in_ops_t *reprocess_stream_ops, uint32_t *stream_id);
187 
188   static int release_reprocess_stream(const camera2_device_t *,
189                                       uint32_t stream_id);
190 
191   /** 3A triggers*/
192   static int trigger_action(const camera2_device_t *, uint32_t trigger_id,
193                             int ext1, int ext2);
194 
195   /** Notifications to application */
196   static int set_notify_callback(const camera2_device_t *,
197                                  camera2_notify_callback notify_cb, void *user);
198 
199   /** Vendor metadata registration */
200   static int get_metadata_vendor_tag_ops(const camera2_device_t *,
201                                          vendor_tag_query_ops_t **ops);
202   // for get_metadata_vendor_tag_ops
203   static const char *get_camera_vendor_section_name(
204       const vendor_tag_query_ops_t *, uint32_t tag);
205   static const char *get_camera_vendor_tag_name(const vendor_tag_query_ops_t *,
206                                                 uint32_t tag);
207   static int get_camera_vendor_tag_type(const vendor_tag_query_ops_t *,
208                                         uint32_t tag);
209 
210   static int dump(const camera2_device_t *, int fd);
211 
212   /** For hw_device_t ops */
213   static int close(struct hw_device_t *device);
214 
215   /****************************************************************************
216    * Data members shared with implementations
217    ***************************************************************************/
218  protected:
219   /** Mutex for calls through camera2 device interface */
220   Mutex mMutex;
221 
222   bool mStatusPresent;
223 
224   const camera2_request_queue_src_ops *mRequestQueueSrc;
225   const camera2_frame_queue_dst_ops *mFrameQueueDst;
226 
227   struct TagOps : public vendor_tag_query_ops {
228     EmulatedCamera2 *parent;
229   };
230   TagOps mVendorTagOps;
231 
232   void sendNotification(int32_t msgType, int32_t ext1, int32_t ext2,
233                         int32_t ext3);
234 
235   /****************************************************************************
236    * Data members
237    ***************************************************************************/
238  private:
239   static camera2_device_ops_t sDeviceOps;
240   camera2_notify_callback mNotifyCb;
241   void *mNotifyUserPtr;
242 };
243 
244 }; /* namespace android */
245 
246 #endif /* HW_EMULATOR_CAMERA_EMULATED_CAMERA2_H */
247