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 HW_EMULATOR_CAMERA_EMULATED_CAMERA3_H
18 #define HW_EMULATOR_CAMERA_EMULATED_CAMERA3_H
19 
20 /**
21  * Contains declaration of a class EmulatedCamera that encapsulates
22  * functionality common to all version 3.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 camera3_device_ops_t API.
26  */
27 
28 #include "EmulatedBaseCamera.h"
29 #include "hardware/camera3.h"
30 #include "system/camera_metadata.h"
31 
32 namespace android {
33 
34 /**
35  * Encapsulates functionality common to all version 3.0 emulated camera devices
36  *
37  * Note that EmulatedCameraFactory instantiates an object of this class just
38  * once, when EmulatedCameraFactory instance gets constructed. Connection to /
39  * disconnection from the actual camera device is handled by calls to
40  * connectDevice(), and closeCamera() methods of this class that are invoked in
41  * response to hw_module_methods_t::open, and camera_device::close callbacks.
42  */
43 class EmulatedCamera3 : public camera3_device, public EmulatedBaseCamera {
44  public:
45   /* Constructs EmulatedCamera3 instance.
46    * Param:
47    *  cameraId - Zero based camera identifier, which is an index of the camera
48    *      instance in camera factory's array.
49    *  module - Emulated camera HAL module descriptor.
50    */
51   EmulatedCamera3(int cameraId, struct hw_module_t *module);
52 
53   /* Destructs EmulatedCamera2 instance. */
54   virtual ~EmulatedCamera3();
55 
56   /* List of all defined capabilities plus useful HW levels */
57   enum AvailableCapabilities {
58     BACKWARD_COMPATIBLE,
59     MANUAL_SENSOR,
60     MANUAL_POST_PROCESSING,
61     RAW,
62     PRIVATE_REPROCESSING,
63     READ_SENSOR_SETTINGS,
64     BURST_CAPTURE,
65     YUV_REPROCESSING,
66     DEPTH_OUTPUT,
67     CONSTRAINED_HIGH_SPEED_VIDEO,
68     // Levels
69     FULL_LEVEL,
70 
71     NUM_CAPABILITIES
72   };
73 
74   // Char strings for above enum, with size NUM_CAPABILITIES
75   static const char *sAvailableCapabilitiesStrings[];
76 
77   /****************************************************************************
78    * Abstract API
79    ***************************************************************************/
80 
81  public:
82   /****************************************************************************
83    * Public API
84    ***************************************************************************/
85 
86  public:
87   virtual status_t Initialize(const cuttlefish::CameraDefinition &params);
88 
89   /****************************************************************************
90    * Camera module API and generic hardware device API implementation
91    ***************************************************************************/
92 
93  public:
94   virtual status_t connectCamera(hw_device_t **device);
95 
96   virtual status_t closeCamera();
97 
98   virtual status_t getCameraInfo(struct camera_info *info);
99 
100   /****************************************************************************
101    * Camera API implementation.
102    * These methods are called from the camera API callback routines.
103    ***************************************************************************/
104 
105  protected:
106   virtual status_t initializeDevice(const camera3_callback_ops *callbackOps);
107 
108   virtual status_t configureStreams(camera3_stream_configuration *streamList);
109 
110   virtual status_t registerStreamBuffers(
111       const camera3_stream_buffer_set *bufferSet);
112 
113   virtual const camera_metadata_t *constructDefaultRequestSettings(int type);
114 
115   virtual status_t processCaptureRequest(camera3_capture_request *request);
116 
117   virtual status_t flush();
118 
119   /** Debug methods */
120 
121   virtual void dump(int fd);
122 
123   /****************************************************************************
124    * Camera API callbacks as defined by camera3_device_ops structure.  See
125    * hardware/libhardware/include/hardware/camera3.h for information on each
126    * of these callbacks. Implemented in this class, these callbacks simply
127    * dispatch the call into an instance of EmulatedCamera3 class defined in
128    * the 'camera_device3' parameter.
129    ***************************************************************************/
130 
131  private:
132   /** Startup */
133   static int initialize(const struct camera3_device *,
134                         const camera3_callback_ops_t *callback_ops);
135 
136   /** Stream configuration and buffer registration */
137 
138   static int configure_streams(const struct camera3_device *,
139                                camera3_stream_configuration_t *stream_list);
140 
141   static int register_stream_buffers(
142       const struct camera3_device *,
143       const camera3_stream_buffer_set_t *buffer_set);
144 
145   /** Template request settings provision */
146 
147   static const camera_metadata_t *construct_default_request_settings(
148       const struct camera3_device *, int type);
149 
150   /** Submission of capture requests to HAL */
151 
152   static int process_capture_request(const struct camera3_device *,
153                                      camera3_capture_request_t *request);
154 
155   static void dump(const camera3_device_t *, int fd);
156 
157   static int flush(const camera3_device_t *);
158 
159   /** For hw_device_t ops */
160   static int close(struct hw_device_t *device);
161 
162   /****************************************************************************
163    * Data members shared with implementations
164    ***************************************************************************/
165  protected:
166   enum {
167     // State at construction time, and after a device operation error
168     STATUS_ERROR = 0,
169     // State after startup-time init and after device instance close
170     STATUS_CLOSED,
171     // State after being opened, before device instance init
172     STATUS_OPEN,
173     // State after device instance initialization
174     STATUS_READY,
175     // State while actively capturing data
176     STATUS_ACTIVE
177   } mStatus;
178 
179   /**
180    * Callbacks back to the framework
181    */
182 
183   void sendCaptureResult(camera3_capture_result_t *result);
184   void sendNotify(camera3_notify_msg_t *msg);
185 
186   /****************************************************************************
187    * Data members
188    ***************************************************************************/
189  private:
190   static camera3_device_ops_t sDeviceOps;
191   const camera3_callback_ops_t *mCallbackOps;
192 };
193 
194 }; /* namespace android */
195 
196 #endif /* HW_EMULATOR_CAMERA_EMULATED_CAMERA3_H */
197