xref: /aosp_15_r20/hardware/libhardware/modules/camera/3_4/camera.h (revision e01b6f769022e40d0923dee176e8dc7cd1d52984)
1*e01b6f76SAndroid Build Coastguard Worker /*
2*e01b6f76SAndroid Build Coastguard Worker  * Copyright (C) 2016 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 // Modified from hardware/libhardware/modules/camera/Camera.h
18*e01b6f76SAndroid Build Coastguard Worker 
19*e01b6f76SAndroid Build Coastguard Worker #ifndef DEFAULT_CAMERA_HAL_CAMERA_H_
20*e01b6f76SAndroid Build Coastguard Worker #define DEFAULT_CAMERA_HAL_CAMERA_H_
21*e01b6f76SAndroid Build Coastguard Worker 
22*e01b6f76SAndroid Build Coastguard Worker #include <camera/CameraMetadata.h>
23*e01b6f76SAndroid Build Coastguard Worker #include <hardware/hardware.h>
24*e01b6f76SAndroid Build Coastguard Worker #include <hardware/camera3.h>
25*e01b6f76SAndroid Build Coastguard Worker #include <utils/Mutex.h>
26*e01b6f76SAndroid Build Coastguard Worker 
27*e01b6f76SAndroid Build Coastguard Worker #include "capture_request.h"
28*e01b6f76SAndroid Build Coastguard Worker #include "metadata/metadata.h"
29*e01b6f76SAndroid Build Coastguard Worker #include "request_tracker.h"
30*e01b6f76SAndroid Build Coastguard Worker #include "static_properties.h"
31*e01b6f76SAndroid Build Coastguard Worker 
32*e01b6f76SAndroid Build Coastguard Worker namespace default_camera_hal {
33*e01b6f76SAndroid Build Coastguard Worker // Camera represents a physical camera on a device.
34*e01b6f76SAndroid Build Coastguard Worker // This is constructed when the HAL module is loaded, one per physical camera.
35*e01b6f76SAndroid Build Coastguard Worker // TODO(b/29185945): Support hotplugging.
36*e01b6f76SAndroid Build Coastguard Worker // It is opened by the framework, and must be closed before it can be opened
37*e01b6f76SAndroid Build Coastguard Worker // again.
38*e01b6f76SAndroid Build Coastguard Worker // This is an abstract class, containing all logic and data shared between all
39*e01b6f76SAndroid Build Coastguard Worker // camera devices (front, back, etc) and common to the ISP.
40*e01b6f76SAndroid Build Coastguard Worker class Camera {
41*e01b6f76SAndroid Build Coastguard Worker     public:
42*e01b6f76SAndroid Build Coastguard Worker         // id is used to distinguish cameras. 0 <= id < NUM_CAMERAS.
43*e01b6f76SAndroid Build Coastguard Worker         // module is a handle to the HAL module, used when the device is opened.
44*e01b6f76SAndroid Build Coastguard Worker         Camera(int id);
45*e01b6f76SAndroid Build Coastguard Worker         virtual ~Camera();
46*e01b6f76SAndroid Build Coastguard Worker 
47*e01b6f76SAndroid Build Coastguard Worker         // Common Camera Device Operations (see <hardware/camera_common.h>)
48*e01b6f76SAndroid Build Coastguard Worker         int openDevice(const hw_module_t *module, hw_device_t **device);
49*e01b6f76SAndroid Build Coastguard Worker         int getInfo(struct camera_info *info);
50*e01b6f76SAndroid Build Coastguard Worker         int close();
51*e01b6f76SAndroid Build Coastguard Worker 
52*e01b6f76SAndroid Build Coastguard Worker         // Camera v3 Device Operations (see <hardware/camera3.h>)
53*e01b6f76SAndroid Build Coastguard Worker         int initialize(const camera3_callback_ops_t *callback_ops);
54*e01b6f76SAndroid Build Coastguard Worker         int configureStreams(camera3_stream_configuration_t *stream_list);
55*e01b6f76SAndroid Build Coastguard Worker         const camera_metadata_t *constructDefaultRequestSettings(int type);
56*e01b6f76SAndroid Build Coastguard Worker         int processCaptureRequest(camera3_capture_request_t *temp_request);
57*e01b6f76SAndroid Build Coastguard Worker         void dump(int fd);
58*e01b6f76SAndroid Build Coastguard Worker         int flush();
59*e01b6f76SAndroid Build Coastguard Worker 
60*e01b6f76SAndroid Build Coastguard Worker     protected:
61*e01b6f76SAndroid Build Coastguard Worker         // Connect to the device: open dev nodes, etc.
62*e01b6f76SAndroid Build Coastguard Worker         virtual int connect() = 0;
63*e01b6f76SAndroid Build Coastguard Worker         // Disconnect from the device: close dev nodes, etc.
64*e01b6f76SAndroid Build Coastguard Worker         virtual void disconnect() = 0;
65*e01b6f76SAndroid Build Coastguard Worker         // Initialize static camera characteristics for individual device
66*e01b6f76SAndroid Build Coastguard Worker         virtual int initStaticInfo(android::CameraMetadata* out) = 0;
67*e01b6f76SAndroid Build Coastguard Worker         // Initialize a template of the given type
68*e01b6f76SAndroid Build Coastguard Worker         virtual int initTemplate(int type, android::CameraMetadata* out) = 0;
69*e01b6f76SAndroid Build Coastguard Worker         // Initialize device info: resource cost and conflicting devices
70*e01b6f76SAndroid Build Coastguard Worker         // (/conflicting devices length)
71*e01b6f76SAndroid Build Coastguard Worker         virtual void initDeviceInfo(struct camera_info *info) = 0;
72*e01b6f76SAndroid Build Coastguard Worker         // Separate initialization method for individual devices when opened
73*e01b6f76SAndroid Build Coastguard Worker         virtual int initDevice() = 0;
74*e01b6f76SAndroid Build Coastguard Worker         // Verify stream configuration dataspaces and rotation values
75*e01b6f76SAndroid Build Coastguard Worker         virtual bool validateDataspacesAndRotations(
76*e01b6f76SAndroid Build Coastguard Worker             const camera3_stream_configuration_t* stream_config) = 0;
77*e01b6f76SAndroid Build Coastguard Worker         // Set up the streams, including seting usage & max_buffers
78*e01b6f76SAndroid Build Coastguard Worker         virtual int setupStreams(
79*e01b6f76SAndroid Build Coastguard Worker             camera3_stream_configuration_t* stream_config) = 0;
80*e01b6f76SAndroid Build Coastguard Worker         // Verify settings are valid for a capture or reprocessing
81*e01b6f76SAndroid Build Coastguard Worker         virtual bool isValidRequestSettings(
82*e01b6f76SAndroid Build Coastguard Worker             const android::CameraMetadata& settings) = 0;
83*e01b6f76SAndroid Build Coastguard Worker         // Enqueue a request to receive data from the camera
84*e01b6f76SAndroid Build Coastguard Worker         virtual int enqueueRequest(
85*e01b6f76SAndroid Build Coastguard Worker             std::shared_ptr<CaptureRequest> request) = 0;
86*e01b6f76SAndroid Build Coastguard Worker         // Flush in flight buffers.
87*e01b6f76SAndroid Build Coastguard Worker         virtual int flushBuffers() = 0;
88*e01b6f76SAndroid Build Coastguard Worker 
89*e01b6f76SAndroid Build Coastguard Worker 
90*e01b6f76SAndroid Build Coastguard Worker         // Callback for when the device has filled in the requested data.
91*e01b6f76SAndroid Build Coastguard Worker         // Fills in the result struct, validates the data, sends appropriate
92*e01b6f76SAndroid Build Coastguard Worker         // notifications, and returns the result to the framework.
93*e01b6f76SAndroid Build Coastguard Worker         void completeRequest(
94*e01b6f76SAndroid Build Coastguard Worker             std::shared_ptr<CaptureRequest> request, int err);
95*e01b6f76SAndroid Build Coastguard Worker         // Prettyprint template names
96*e01b6f76SAndroid Build Coastguard Worker         const char* templateToString(int type);
97*e01b6f76SAndroid Build Coastguard Worker 
98*e01b6f76SAndroid Build Coastguard Worker     private:
99*e01b6f76SAndroid Build Coastguard Worker         // Camera device handle returned to framework for use
100*e01b6f76SAndroid Build Coastguard Worker         camera3_device_t mDevice;
101*e01b6f76SAndroid Build Coastguard Worker         // Get static info from the device and store it in mStaticInfo.
102*e01b6f76SAndroid Build Coastguard Worker         int loadStaticInfo();
103*e01b6f76SAndroid Build Coastguard Worker         // Confirm that a stream configuration is valid.
104*e01b6f76SAndroid Build Coastguard Worker         int validateStreamConfiguration(
105*e01b6f76SAndroid Build Coastguard Worker             const camera3_stream_configuration_t* stream_config);
106*e01b6f76SAndroid Build Coastguard Worker         // Verify settings are valid for reprocessing an input buffer
107*e01b6f76SAndroid Build Coastguard Worker         bool isValidReprocessSettings(const camera_metadata_t *settings);
108*e01b6f76SAndroid Build Coastguard Worker         // Pre-process an output buffer
109*e01b6f76SAndroid Build Coastguard Worker         int preprocessCaptureBuffer(camera3_stream_buffer_t *buffer);
110*e01b6f76SAndroid Build Coastguard Worker         // Send a shutter notify message with start of exposure time
111*e01b6f76SAndroid Build Coastguard Worker         void notifyShutter(uint32_t frame_number, uint64_t timestamp);
112*e01b6f76SAndroid Build Coastguard Worker         // Send an error message and return the errored out result.
113*e01b6f76SAndroid Build Coastguard Worker         void completeRequestWithError(std::shared_ptr<CaptureRequest> request);
114*e01b6f76SAndroid Build Coastguard Worker         // Send a capture result for a request.
115*e01b6f76SAndroid Build Coastguard Worker         void sendResult(std::shared_ptr<CaptureRequest> request);
116*e01b6f76SAndroid Build Coastguard Worker         // Is type a valid template type (and valid index into mTemplates)
117*e01b6f76SAndroid Build Coastguard Worker         bool isValidTemplateType(int type);
118*e01b6f76SAndroid Build Coastguard Worker 
119*e01b6f76SAndroid Build Coastguard Worker         // Identifier used by framework to distinguish cameras
120*e01b6f76SAndroid Build Coastguard Worker         const int mId;
121*e01b6f76SAndroid Build Coastguard Worker         // CameraMetadata containing static characteristics
122*e01b6f76SAndroid Build Coastguard Worker         std::unique_ptr<StaticProperties> mStaticInfo;
123*e01b6f76SAndroid Build Coastguard Worker         // Flag indicating if settings have been set since
124*e01b6f76SAndroid Build Coastguard Worker         // the last configure_streams() call.
125*e01b6f76SAndroid Build Coastguard Worker         bool mSettingsSet;
126*e01b6f76SAndroid Build Coastguard Worker         // Busy flag indicates camera is in use
127*e01b6f76SAndroid Build Coastguard Worker         bool mBusy;
128*e01b6f76SAndroid Build Coastguard Worker         // Camera device operations handle shared by all devices
129*e01b6f76SAndroid Build Coastguard Worker         const static camera3_device_ops_t sOps;
130*e01b6f76SAndroid Build Coastguard Worker         // Methods used to call back into the framework
131*e01b6f76SAndroid Build Coastguard Worker         const camera3_callback_ops_t *mCallbackOps;
132*e01b6f76SAndroid Build Coastguard Worker         // Lock protecting the Camera object for modifications
133*e01b6f76SAndroid Build Coastguard Worker         android::Mutex mDeviceLock;
134*e01b6f76SAndroid Build Coastguard Worker         // Lock protecting only static camera characteristics, which may
135*e01b6f76SAndroid Build Coastguard Worker         // be accessed without the camera device open
136*e01b6f76SAndroid Build Coastguard Worker         android::Mutex mStaticInfoLock;
137*e01b6f76SAndroid Build Coastguard Worker         // Standard camera settings templates
138*e01b6f76SAndroid Build Coastguard Worker         std::unique_ptr<const android::CameraMetadata> mTemplates[CAMERA3_TEMPLATE_COUNT];
139*e01b6f76SAndroid Build Coastguard Worker         // Track in flight requests.
140*e01b6f76SAndroid Build Coastguard Worker         std::unique_ptr<RequestTracker> mInFlightTracker;
141*e01b6f76SAndroid Build Coastguard Worker         android::Mutex mInFlightTrackerLock;
142*e01b6f76SAndroid Build Coastguard Worker };
143*e01b6f76SAndroid Build Coastguard Worker }  // namespace default_camera_hal
144*e01b6f76SAndroid Build Coastguard Worker 
145*e01b6f76SAndroid Build Coastguard Worker #endif  // DEFAULT_CAMERA_HAL_CAMERA_H_
146