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 "hardware/camera3.h"
29 #include "system/camera_metadata.h"
30 #include "EmulatedBaseCamera.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,
52             struct hw_module_t* module);
53 
54     /* Destructs EmulatedCamera2 instance. */
55     virtual ~EmulatedCamera3();
56 
57     /****************************************************************************
58      * Abstract API
59      ***************************************************************************/
60 
61 public:
62 
63     /****************************************************************************
64      * Public API
65      ***************************************************************************/
66 
67 public:
68     virtual status_t Initialize();
69 
70     /****************************************************************************
71      * Camera module API and generic hardware device API implementation
72      ***************************************************************************/
73 
74 public:
75     virtual status_t connectCamera(hw_device_t** device);
76 
77     virtual status_t closeCamera();
78 
79     virtual status_t getCameraInfo(struct camera_info* info);
80 
81     /****************************************************************************
82      * Camera API implementation.
83      * These methods are called from the camera API callback routines.
84      ***************************************************************************/
85 
86 protected:
87 
88     virtual status_t initializeDevice(
89         const camera3_callback_ops *callbackOps);
90 
91     virtual status_t configureStreams(
92         camera3_stream_configuration *streamList);
93 
94     virtual status_t registerStreamBuffers(
95         const camera3_stream_buffer_set *bufferSet) ;
96 
97     virtual const camera_metadata_t* constructDefaultRequestSettings(
98         int type);
99 
100     virtual status_t processCaptureRequest(camera3_capture_request *request);
101 
102     /** Debug methods */
103 
104     virtual void dump(int fd);
105 
106     /** Tag query methods */
107     virtual const char *getVendorSectionName(uint32_t tag);
108 
109     virtual const char *getVendorTagName(uint32_t tag);
110 
111     virtual int getVendorTagType(uint32_t tag);
112 
113     /****************************************************************************
114      * Camera API callbacks as defined by camera3_device_ops structure.  See
115      * hardware/libhardware/include/hardware/camera3.h for information on each
116      * of these callbacks. Implemented in this class, these callbacks simply
117      * dispatch the call into an instance of EmulatedCamera3 class defined in
118      * the 'camera_device3' parameter.
119      ***************************************************************************/
120 
121 private:
122 
123     /** Startup */
124     static int initialize(const struct camera3_device *,
125             const camera3_callback_ops_t *callback_ops);
126 
127     /** Stream configuration and buffer registration */
128 
129     static int configure_streams(const struct camera3_device *,
130             camera3_stream_configuration_t *stream_list);
131 
132     static int register_stream_buffers(const struct camera3_device *,
133             const camera3_stream_buffer_set_t *buffer_set);
134 
135     /** Template request settings provision */
136 
137     static const camera_metadata_t* construct_default_request_settings(
138             const struct camera3_device *, int type);
139 
140     /** Submission of capture requests to HAL */
141 
142     static int process_capture_request(const struct camera3_device *,
143             camera3_capture_request_t *request);
144 
145     /** Vendor metadata registration */
146     static void get_metadata_vendor_tag_ops(const camera3_device_t *,
147             vendor_tag_query_ops_t *ops);
148     // for get_metadata_vendor_tag_ops
149     static const char* get_camera_vendor_section_name(
150             const vendor_tag_query_ops_t *,
151             uint32_t tag);
152     static const char* get_camera_vendor_tag_name(
153             const vendor_tag_query_ops_t *,
154             uint32_t tag);
155     static int get_camera_vendor_tag_type(
156             const vendor_tag_query_ops_t *,
157             uint32_t tag);
158 
159     static void dump(const camera3_device_t *, int fd);
160 
161     /** For hw_device_t ops */
162     static int close(struct hw_device_t* device);
163 
164     /****************************************************************************
165      * Data members shared with implementations
166      ***************************************************************************/
167   protected:
168 
169     struct TagOps : public vendor_tag_query_ops {
170         EmulatedCamera3 *parent;
171     };
172     TagOps      mVendorTagOps;
173 
174     enum {
175         // State at construction time, and after a device operation error
176         STATUS_ERROR = 0,
177         // State after startup-time init and after device instance close
178         STATUS_CLOSED,
179         // State after being opened, before device instance init
180         STATUS_OPEN,
181         // State after device instance initialization
182         STATUS_READY,
183         // State while actively capturing data
184         STATUS_ACTIVE
185     } mStatus;
186 
187     /**
188      * Callbacks back to the framework
189      */
190 
191     void sendCaptureResult(camera3_capture_result_t *result);
192     void sendNotify(camera3_notify_msg_t *msg);
193 
194     /****************************************************************************
195      * Data members
196      ***************************************************************************/
197   private:
198     static camera3_device_ops_t   sDeviceOps;
199     const camera3_callback_ops_t *mCallbackOps;
200 };
201 
202 }; /* namespace android */
203 
204 #endif  /* HW_EMULATOR_CAMERA_EMULATED_CAMERA3_H */
205