liyujie
2025-08-28 d9927380ed7c8366f762049be9f3fee225860833
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
 * Copyright (c) 2021 by Allwinnertech Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
#ifndef DEFAULT_CAMERA_HAL_CAMERA_H_
#define DEFAULT_CAMERA_HAL_CAMERA_H_
 
#include "common.h"
 
#ifdef LOG_NDEBUG
#undef LOG_NDEBUG
#endif
 
#if DBG_CAMERA
#undef NDEBUG
#define LOG_NDEBUG 0
#else
#define LOG_NDEBUG 1
#endif
 
#include <hardware/hardware.h>
#include <hardware/camera3.h>
#include <utils/Mutex.h>
#include <utils/RefBase.h>
 
#include <memory>
 
#include "v4l2_log.h"
#include "CameraMetadata.h"
#include "capture_request.h"
#include "metadata/metadata.h"
#include "request_tracker.h"
#include "static_properties.h"
 
namespace default_camera_hal {
 
using ::android::hardware::camera::common::V1_0::helper::CameraMetadata;
 
// Camera represents a physical camera on a device.
// This is constructed when the HAL module is loaded, one per physical camera.
// TODO(b/29185945): Support hotplugging.
// It is opened by the framework, and must be closed before it can be opened
// again.
// This is an abstract class, containing all logic and data shared between all
// camera devices (front, back, etc) and common to the ISP.
class Camera : public virtual android::RefBase {
 public:
  // id is used to distinguish cameras. 0 <= id < NUM_CAMERAS.
  // module is a handle to the HAL module, used when the device is opened.
  explicit Camera(int id);
  virtual ~Camera();
 
  virtual int setFlashTorchMode(bool enabled) = 0;
  virtual int closeFlashTorch() = 0;
 
  // Common Camera Device Operations (see <hardware/camera_common.h>)
  int openDevice(const hw_module_t *module, hw_device_t **device);
  int getInfo(struct camera_info *info);
  int close();
 
  // Camera v3 Device Operations (see <hardware/camera3.h>)
  int initialize(const camera3_callback_ops_t *callback_ops);
  int configureStreams(camera3_stream_configuration_t *stream_list);
  const camera_metadata_t *constructDefaultRequestSettings(int type);
  int processCaptureRequest(camera3_capture_request_t *temp_request);
  void dump(int fd);
  int flush();
  bool isBusy();
 
 protected:
  // Connect to the device: open dev nodes, etc.
  virtual int connect() = 0;
  // Disconnect from the device: close dev nodes, etc.
  virtual void disconnect() = 0;
  // Initialize static camera characteristics for individual device
  virtual int initStaticInfo(CameraMetadata* out) = 0;
  // Initialize a template of the given type
  virtual int initTemplate(int type, CameraMetadata* out) = 0;
  // Initialize device info: resource cost and conflicting devices
  // (/conflicting devices length)
  virtual void initDeviceInfo(struct camera_info *info) = 0;
  // Separate initialization method for individual devices when opened
  virtual int initDevice() = 0;
  // Verify stream configuration dataspaces and rotation values
  virtual bool validateDataspacesAndRotations(
      const camera3_stream_configuration_t* stream_config) = 0;
  // Set up the streams, including seting usage & max_buffers
  virtual int setupStreams(
      camera3_stream_configuration_t* stream_config) = 0;
  // Verify settings are valid for a capture or reprocessing
  virtual bool isValidRequestSettings(
      const CameraMetadata& settings) = 0;
  // Enqueue a request to receive data from the camera
  virtual int enqueueRequest(
      std::shared_ptr<CaptureRequest> request) = 0;
  // Flush in flight buffers.
  virtual int flushBuffers() = 0;
  // Callback for when the device has filled in the requested data.
  // Fills in the result struct, validates the data, sends appropriate
  // notifications, and returns the result to the framework.
  void completeRequest(
      std::shared_ptr<CaptureRequest> request, int err);
  // Prettyprint template names
  const char* templateToString(int type);
  // Be compatible with camera api 1.
  bool isVideoByTemplate;
  int mRequestInflightNum;
 
 private:
  // Camera device handle returned to framework for use
  camera3_device_t mDevice;
  // Get static info from the device and store it in mStaticInfo.
  int loadStaticInfo();
  // Confirm that a stream configuration is valid.
  int validateStreamConfiguration(
      const camera3_stream_configuration_t* stream_config);
  // Verify settings are valid for reprocessing an input buffer
  bool isValidReprocessSettings(const camera_metadata_t *settings);
  // Pre-process an output buffer
  int preprocessCaptureBuffer(camera3_stream_buffer_t *buffer);
  // Send a shutter notify message with start of exposure time
  void notifyShutter(uint32_t frame_number, uint64_t timestamp);
  // Send an error message and return the errored out result.
  void completeRequestWithError(std::shared_ptr<CaptureRequest> request);
  // Send a capture result for a request.
  void sendResult(std::shared_ptr<CaptureRequest> request);
  // Is type a valid template type (and valid index into mTemplates)
  bool isValidTemplateType(int type);
  // Identifier used by framework to distinguish cameras
  const int mId;
  // CameraMetadata containing static characteristics
  std::unique_ptr<StaticProperties> mStaticInfo;
  // Flag indicating if settings have been set since
  // the last configure_streams() call.
  bool mSettingsSet;
  // Busy flag indicates camera is in use
  bool mBusy;
  // Camera device operations handle shared by all devices
  static const camera3_device_ops_t sOps;
  // Methods used to call back into the framework
  const camera3_callback_ops_t *mCallbackOps;
  // Lock protecting the Camera object for modifications
  android::Mutex mDeviceLock;
  // Lock protecting only static camera characteristics, which may
  // be accessed without the camera device open
  android::Mutex mStaticInfoLock;
  android::Mutex mFlushLock;
  // Standard camera settings templates
  std::unique_ptr<const CameraMetadata> mTemplates[CAMERA3_TEMPLATE_COUNT];
  // Track in flight requests.
  std::unique_ptr<RequestTracker> mInFlightTracker;
  enum DeviceStatus {
    DEVICE_CLOSE = 0,
    DEVICE_OPEN
  };
  int64_t  btimeRequest;
  DeviceStatus mCameraDeviceState;
};
}  // namespace default_camera_hal
 
#endif  // DEFAULT_CAMERA_HAL_CAMERA_H_