xref: /aosp_15_r20/hardware/libhardware/modules/usbcamera/Camera.h (revision e01b6f769022e40d0923dee176e8dc7cd1d52984)
1*e01b6f76SAndroid Build Coastguard Worker /*
2*e01b6f76SAndroid Build Coastguard Worker  * Copyright (C) 2015 The Android Open Source Project
3*e01b6f76SAndroid Build Coastguard Worker  *
4*e01b6f76SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*e01b6f76SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*e01b6f76SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*e01b6f76SAndroid Build Coastguard Worker  *
8*e01b6f76SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*e01b6f76SAndroid Build Coastguard Worker  *
10*e01b6f76SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*e01b6f76SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*e01b6f76SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*e01b6f76SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*e01b6f76SAndroid Build Coastguard Worker  * limitations under the License.
15*e01b6f76SAndroid Build Coastguard Worker  */
16*e01b6f76SAndroid Build Coastguard Worker 
17*e01b6f76SAndroid Build Coastguard Worker #ifndef CAMERA_H_
18*e01b6f76SAndroid Build Coastguard Worker #define CAMERA_H_
19*e01b6f76SAndroid Build Coastguard Worker 
20*e01b6f76SAndroid Build Coastguard Worker #include <hardware/hardware.h>
21*e01b6f76SAndroid Build Coastguard Worker #include <hardware/camera3.h>
22*e01b6f76SAndroid Build Coastguard Worker #include <utils/Mutex.h>
23*e01b6f76SAndroid Build Coastguard Worker #include <utils/Vector.h>
24*e01b6f76SAndroid Build Coastguard Worker #include "Metadata.h"
25*e01b6f76SAndroid Build Coastguard Worker #include <sync/sync.h>
26*e01b6f76SAndroid Build Coastguard Worker #include "Stream.h"
27*e01b6f76SAndroid Build Coastguard Worker 
28*e01b6f76SAndroid Build Coastguard Worker #define CAMERA_SYNC_TIMEOUT_MS 5000
29*e01b6f76SAndroid Build Coastguard Worker 
30*e01b6f76SAndroid Build Coastguard Worker namespace usb_camera_hal {
31*e01b6f76SAndroid Build Coastguard Worker // Camera represents a physical camera on a device.
32*e01b6f76SAndroid Build Coastguard Worker // This is constructed when the HAL module is loaded, one per physical camera.
33*e01b6f76SAndroid Build Coastguard Worker // It is opened by the framework, and must be closed before it can be opened
34*e01b6f76SAndroid Build Coastguard Worker // again.
35*e01b6f76SAndroid Build Coastguard Worker // This is an abstract class, containing all logic and data shared between all
36*e01b6f76SAndroid Build Coastguard Worker // camera devices.
37*e01b6f76SAndroid Build Coastguard Worker class Camera {
38*e01b6f76SAndroid Build Coastguard Worker     public:
39*e01b6f76SAndroid Build Coastguard Worker         // id is used to distinguish cameras. 0 <= id < NUM_CAMERAS.
40*e01b6f76SAndroid Build Coastguard Worker         // module is a handle to the HAL module, used when the device is opened.
41*e01b6f76SAndroid Build Coastguard Worker         explicit Camera(int id);
42*e01b6f76SAndroid Build Coastguard Worker         virtual ~Camera();
43*e01b6f76SAndroid Build Coastguard Worker 
44*e01b6f76SAndroid Build Coastguard Worker         // Common Camera Device Operations (see <hardware/camera_common.h>)
45*e01b6f76SAndroid Build Coastguard Worker         int open(const hw_module_t *module, hw_device_t **device);
46*e01b6f76SAndroid Build Coastguard Worker         int getInfo(struct camera_info *info);
47*e01b6f76SAndroid Build Coastguard Worker         int close();
48*e01b6f76SAndroid Build Coastguard Worker 
49*e01b6f76SAndroid Build Coastguard Worker         // Camera v3 Device Operations (see <hardware/camera3.h>)
50*e01b6f76SAndroid Build Coastguard Worker         int initialize(const camera3_callback_ops_t *callback_ops);
51*e01b6f76SAndroid Build Coastguard Worker         int configureStreams(camera3_stream_configuration_t *stream_list);
52*e01b6f76SAndroid Build Coastguard Worker         const camera_metadata_t *constructDefaultRequestSettings(int type);
53*e01b6f76SAndroid Build Coastguard Worker         int processCaptureRequest(camera3_capture_request_t *request);
54*e01b6f76SAndroid Build Coastguard Worker         int flush();
55*e01b6f76SAndroid Build Coastguard Worker         void dump(int fd);
56*e01b6f76SAndroid Build Coastguard Worker 
57*e01b6f76SAndroid Build Coastguard Worker         // Update static camera characteristics. This method could be called by
58*e01b6f76SAndroid Build Coastguard Worker         // HAL hotplug thread when camera is plugged.
59*e01b6f76SAndroid Build Coastguard Worker         void updateInfo();
60*e01b6f76SAndroid Build Coastguard Worker 
61*e01b6f76SAndroid Build Coastguard Worker     protected:
62*e01b6f76SAndroid Build Coastguard Worker         // Initialize static camera characteristics.
63*e01b6f76SAndroid Build Coastguard Worker         virtual int initStaticInfo() = 0;
64*e01b6f76SAndroid Build Coastguard Worker         // Verify settings are valid for a capture
65*e01b6f76SAndroid Build Coastguard Worker         virtual bool isValidCaptureSettings(const camera_metadata_t *) = 0;
66*e01b6f76SAndroid Build Coastguard Worker         // Separate open method for individual devices
67*e01b6f76SAndroid Build Coastguard Worker         virtual int openDevice() = 0;
68*e01b6f76SAndroid Build Coastguard Worker         // Separate initialization method for individual devices when opened
69*e01b6f76SAndroid Build Coastguard Worker         virtual int initDevice() = 0;
70*e01b6f76SAndroid Build Coastguard Worker         // Flush camera pipeline for each individual device
71*e01b6f76SAndroid Build Coastguard Worker         virtual int flushDevice() = 0;
72*e01b6f76SAndroid Build Coastguard Worker         // Separate close method for individual devices
73*e01b6f76SAndroid Build Coastguard Worker         virtual int closeDevice() = 0;
74*e01b6f76SAndroid Build Coastguard Worker         // Capture and file an output buffer for an input buffer.
75*e01b6f76SAndroid Build Coastguard Worker         virtual int processCaptureBuffer(const camera3_stream_buffer_t *in,
76*e01b6f76SAndroid Build Coastguard Worker                 camera3_stream_buffer_t *out) = 0;
77*e01b6f76SAndroid Build Coastguard Worker         // Accessor method used by initDevice() to set the templates' metadata
78*e01b6f76SAndroid Build Coastguard Worker         int setTemplate(int type, camera_metadata_t *settings);
79*e01b6f76SAndroid Build Coastguard Worker         // Prettyprint template names
80*e01b6f76SAndroid Build Coastguard Worker         const char* templateToString(int type);
81*e01b6f76SAndroid Build Coastguard Worker         // Process an output buffer
82*e01b6f76SAndroid Build Coastguard Worker 
83*e01b6f76SAndroid Build Coastguard Worker         // Identifier used by framework to distinguish cameras
84*e01b6f76SAndroid Build Coastguard Worker         const int mId;
85*e01b6f76SAndroid Build Coastguard Worker         // Metadata containing persistent camera characteristics
86*e01b6f76SAndroid Build Coastguard Worker         Metadata mMetadata;
87*e01b6f76SAndroid Build Coastguard Worker         // camera_metadata structure containing static characteristics
88*e01b6f76SAndroid Build Coastguard Worker         camera_metadata_t *mStaticInfo;
89*e01b6f76SAndroid Build Coastguard Worker 
90*e01b6f76SAndroid Build Coastguard Worker     private:
91*e01b6f76SAndroid Build Coastguard Worker         // Camera device handle returned to framework for use
92*e01b6f76SAndroid Build Coastguard Worker         camera3_device_t mDevice;
93*e01b6f76SAndroid Build Coastguard Worker         // Reuse a stream already created by this device. Must be called with mDeviceLock held.
94*e01b6f76SAndroid Build Coastguard Worker         Stream *reuseStreamLocked(camera3_stream_t *astream);
95*e01b6f76SAndroid Build Coastguard Worker         // Destroy all streams in a stream array, and the array itself. Must be called with
96*e01b6f76SAndroid Build Coastguard Worker         // mDeviceLock held.
97*e01b6f76SAndroid Build Coastguard Worker         void destroyStreamsLocked(android::Vector<Stream *> &streams);
98*e01b6f76SAndroid Build Coastguard Worker         // Verify a set of streams is valid in aggregate. Must be called with mDeviceLock held.
99*e01b6f76SAndroid Build Coastguard Worker         bool isValidStreamSetLocked(const android::Vector<Stream *> &streams);
100*e01b6f76SAndroid Build Coastguard Worker         // Calculate usage and max_bufs of each stream. Must be called with mDeviceLock held.
101*e01b6f76SAndroid Build Coastguard Worker         void setupStreamsLocked(android::Vector<Stream *> &streams);
102*e01b6f76SAndroid Build Coastguard Worker         // Update new settings for re-use and clean up old settings. Must be called with
103*e01b6f76SAndroid Build Coastguard Worker         // mDeviceLock held.
104*e01b6f76SAndroid Build Coastguard Worker         void updateSettingsLocked(const camera_metadata_t *new_settings);
105*e01b6f76SAndroid Build Coastguard Worker         // Send a shutter notify message with start of exposure time
106*e01b6f76SAndroid Build Coastguard Worker         void notifyShutter(uint32_t frame_number, uint64_t timestamp);
107*e01b6f76SAndroid Build Coastguard Worker         // Is type a valid template type (and valid index into mTemplates)
108*e01b6f76SAndroid Build Coastguard Worker         bool isValidTemplateType(int type);
109*e01b6f76SAndroid Build Coastguard Worker 
110*e01b6f76SAndroid Build Coastguard Worker         // Busy flag indicates camera is in use
111*e01b6f76SAndroid Build Coastguard Worker         bool mBusy;
112*e01b6f76SAndroid Build Coastguard Worker         // Camera device operations handle shared by all devices
113*e01b6f76SAndroid Build Coastguard Worker         const static camera3_device_ops_t sOps;
114*e01b6f76SAndroid Build Coastguard Worker         // Methods used to call back into the framework
115*e01b6f76SAndroid Build Coastguard Worker         const camera3_callback_ops_t *mCallbackOps;
116*e01b6f76SAndroid Build Coastguard Worker         // Lock protecting the Camera object for modifications
117*e01b6f76SAndroid Build Coastguard Worker         android::Mutex mDeviceLock;
118*e01b6f76SAndroid Build Coastguard Worker         // Lock protecting only static camera characteristics, which may
119*e01b6f76SAndroid Build Coastguard Worker         // be accessed without the camera device open
120*e01b6f76SAndroid Build Coastguard Worker         android::Mutex mStaticInfoLock;
121*e01b6f76SAndroid Build Coastguard Worker         // Array of handles to streams currently in use by the device
122*e01b6f76SAndroid Build Coastguard Worker         android::Vector<Stream *> mStreams;
123*e01b6f76SAndroid Build Coastguard Worker         // Static array of standard camera settings templates
124*e01b6f76SAndroid Build Coastguard Worker         camera_metadata_t *mTemplates[CAMERA3_TEMPLATE_COUNT];
125*e01b6f76SAndroid Build Coastguard Worker         // Most recent request settings seen, memoized to be reused
126*e01b6f76SAndroid Build Coastguard Worker         camera_metadata_t *mSettings;
127*e01b6f76SAndroid Build Coastguard Worker         bool mIsInitialized;
128*e01b6f76SAndroid Build Coastguard Worker };
129*e01b6f76SAndroid Build Coastguard Worker } // namespace usb_camera_hal
130*e01b6f76SAndroid Build Coastguard Worker 
131*e01b6f76SAndroid Build Coastguard Worker #endif // CAMERA_H_
132