camerad: auto free the buffer when it goes out of scope (#28807)
old-commit-hash: 8076107b4fa1b3c2b7c14a8464aa92e949e2e139
This commit is contained in:
@@ -143,7 +143,7 @@ void CameraState::sensors_start() {
|
||||
void CameraState::sensors_poke(int request_id) {
|
||||
uint32_t cam_packet_handle = 0;
|
||||
int size = sizeof(struct cam_packet);
|
||||
struct cam_packet *pkt = (struct cam_packet *)mm.alloc(size, &cam_packet_handle);
|
||||
auto pkt = mm.alloc<struct cam_packet>(size, &cam_packet_handle);
|
||||
pkt->num_cmd_buf = 0;
|
||||
pkt->kmd_cmd_buf_index = -1;
|
||||
pkt->header.size = size;
|
||||
@@ -156,15 +156,13 @@ void CameraState::sensors_poke(int request_id) {
|
||||
enabled = false;
|
||||
return;
|
||||
}
|
||||
|
||||
mm.free(pkt);
|
||||
}
|
||||
|
||||
void CameraState::sensors_i2c(struct i2c_random_wr_payload* dat, int len, int op_code, bool data_word) {
|
||||
// LOGD("sensors_i2c: %d", len);
|
||||
uint32_t cam_packet_handle = 0;
|
||||
int size = sizeof(struct cam_packet)+sizeof(struct cam_cmd_buf_desc)*1;
|
||||
struct cam_packet *pkt = (struct cam_packet *)mm.alloc(size, &cam_packet_handle);
|
||||
auto pkt = mm.alloc<struct cam_packet>(size, &cam_packet_handle);
|
||||
pkt->num_cmd_buf = 1;
|
||||
pkt->kmd_cmd_buf_index = -1;
|
||||
pkt->header.size = size;
|
||||
@@ -174,7 +172,7 @@ void CameraState::sensors_i2c(struct i2c_random_wr_payload* dat, int len, int op
|
||||
buf_desc[0].size = buf_desc[0].length = sizeof(struct i2c_rdwr_header) + len*sizeof(struct i2c_random_wr_payload);
|
||||
buf_desc[0].type = CAM_CMD_BUF_I2C;
|
||||
|
||||
struct cam_cmd_i2c_random_wr *i2c_random_wr = (struct cam_cmd_i2c_random_wr *)mm.alloc(buf_desc[0].size, (uint32_t*)&buf_desc[0].mem_handle);
|
||||
auto i2c_random_wr = mm.alloc<struct cam_cmd_i2c_random_wr>(buf_desc[0].size, (uint32_t*)&buf_desc[0].mem_handle);
|
||||
i2c_random_wr->header.count = len;
|
||||
i2c_random_wr->header.op_code = 1;
|
||||
i2c_random_wr->header.cmd_type = CAMERA_SENSOR_CMD_TYPE_I2C_RNDM_WR;
|
||||
@@ -188,9 +186,6 @@ void CameraState::sensors_i2c(struct i2c_random_wr_payload* dat, int len, int op
|
||||
enabled = false;
|
||||
return;
|
||||
}
|
||||
|
||||
mm.free(i2c_random_wr);
|
||||
mm.free(pkt);
|
||||
}
|
||||
|
||||
static cam_cmd_power *power_set_wait(cam_cmd_power *power, int16_t delay_ms) {
|
||||
@@ -204,7 +199,7 @@ static cam_cmd_power *power_set_wait(cam_cmd_power *power, int16_t delay_ms) {
|
||||
int CameraState::sensors_init() {
|
||||
uint32_t cam_packet_handle = 0;
|
||||
int size = sizeof(struct cam_packet)+sizeof(struct cam_cmd_buf_desc)*2;
|
||||
struct cam_packet *pkt = (struct cam_packet *)mm.alloc(size, &cam_packet_handle);
|
||||
auto pkt = mm.alloc<struct cam_packet>(size, &cam_packet_handle);
|
||||
pkt->num_cmd_buf = 2;
|
||||
pkt->kmd_cmd_buf_index = -1;
|
||||
pkt->header.op_code = 0x1000000 | CAM_SENSOR_PACKET_OPCODE_SENSOR_PROBE;
|
||||
@@ -213,8 +208,8 @@ int CameraState::sensors_init() {
|
||||
|
||||
buf_desc[0].size = buf_desc[0].length = sizeof(struct cam_cmd_i2c_info) + sizeof(struct cam_cmd_probe);
|
||||
buf_desc[0].type = CAM_CMD_BUF_LEGACY;
|
||||
struct cam_cmd_i2c_info *i2c_info = (struct cam_cmd_i2c_info *)mm.alloc(buf_desc[0].size, (uint32_t*)&buf_desc[0].mem_handle);
|
||||
auto probe = (struct cam_cmd_probe *)(i2c_info + 1);
|
||||
auto i2c_info = mm.alloc<struct cam_cmd_i2c_info>(buf_desc[0].size, (uint32_t*)&buf_desc[0].mem_handle);
|
||||
auto probe = (struct cam_cmd_probe *)(i2c_info.get() + 1);
|
||||
|
||||
probe->camera_id = camera_num;
|
||||
switch (camera_num) {
|
||||
@@ -255,11 +250,11 @@ int CameraState::sensors_init() {
|
||||
//buf_desc[1].size = buf_desc[1].length = 148;
|
||||
buf_desc[1].size = buf_desc[1].length = 196;
|
||||
buf_desc[1].type = CAM_CMD_BUF_I2C;
|
||||
struct cam_cmd_power *power_settings = (struct cam_cmd_power *)mm.alloc(buf_desc[1].size, (uint32_t*)&buf_desc[1].mem_handle);
|
||||
memset(power_settings, 0, buf_desc[1].size);
|
||||
auto power_settings = mm.alloc<struct cam_cmd_power>(buf_desc[1].size, (uint32_t*)&buf_desc[1].mem_handle);
|
||||
memset(power_settings.get(), 0, buf_desc[1].size);
|
||||
|
||||
// power on
|
||||
struct cam_cmd_power *power = power_settings;
|
||||
struct cam_cmd_power *power = power_settings.get();
|
||||
power->count = 4;
|
||||
power->cmd_type = CAMERA_SENSOR_CMD_TYPE_PWR_UP;
|
||||
power->power_settings[0].power_seq_type = 3; // clock??
|
||||
@@ -315,11 +310,6 @@ int CameraState::sensors_init() {
|
||||
|
||||
int ret = do_cam_control(sensor_fd, CAM_SENSOR_PROBE_CMD, (void *)(uintptr_t)cam_packet_handle, 0);
|
||||
LOGD("probing the sensor: %d", ret);
|
||||
|
||||
mm.free(i2c_info);
|
||||
mm.free(power_settings);
|
||||
mm.free(pkt);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -329,7 +319,7 @@ void CameraState::config_isp(int io_mem_handle, int fence, int request_id, int b
|
||||
if (io_mem_handle != 0) {
|
||||
size += sizeof(struct cam_buf_io_cfg);
|
||||
}
|
||||
struct cam_packet *pkt = (struct cam_packet *)mm.alloc(size, &cam_packet_handle);
|
||||
auto pkt = mm.alloc<struct cam_packet>(size, &cam_packet_handle);
|
||||
pkt->num_cmd_buf = 2;
|
||||
pkt->kmd_cmd_buf_index = 0;
|
||||
// YUV has kmd_cmd_buf_offset = 1780
|
||||
@@ -424,8 +414,8 @@ void CameraState::config_isp(int io_mem_handle, int fence, int request_id, int b
|
||||
buf_desc[1].length = buf_desc[1].size - buf_desc[1].offset;
|
||||
buf_desc[1].type = CAM_CMD_BUF_GENERIC;
|
||||
buf_desc[1].meta_data = CAM_ISP_PACKET_META_GENERIC_BLOB_COMMON;
|
||||
uint32_t *buf2 = (uint32_t *)mm.alloc(buf_desc[1].size, (uint32_t*)&buf_desc[1].mem_handle);
|
||||
memcpy(buf2, &tmp, sizeof(tmp));
|
||||
auto buf2 = mm.alloc<uint32_t>(buf_desc[1].size, (uint32_t*)&buf_desc[1].mem_handle);
|
||||
memcpy(buf2.get(), &tmp, sizeof(tmp));
|
||||
|
||||
if (io_mem_handle != 0) {
|
||||
io_cfg[0].mem_handle[0] = io_mem_handle;
|
||||
@@ -459,9 +449,6 @@ void CameraState::config_isp(int io_mem_handle, int fence, int request_id, int b
|
||||
if (ret != 0) {
|
||||
LOGE("isp config failed");
|
||||
}
|
||||
|
||||
mm.free(buf2);
|
||||
mm.free(pkt);
|
||||
}
|
||||
|
||||
void CameraState::enqueue_buffer(int i, bool dp) {
|
||||
@@ -746,7 +733,7 @@ void CameraState::camera_open(MultiCameraState *multi_cam_state_, int camera_num
|
||||
{
|
||||
uint32_t cam_packet_handle = 0;
|
||||
int size = sizeof(struct cam_packet)+sizeof(struct cam_cmd_buf_desc)*1;
|
||||
struct cam_packet *pkt = (struct cam_packet *)mm.alloc(size, &cam_packet_handle);
|
||||
auto pkt = mm.alloc<struct cam_packet>(size, &cam_packet_handle);
|
||||
pkt->num_cmd_buf = 1;
|
||||
pkt->kmd_cmd_buf_index = -1;
|
||||
pkt->header.size = size;
|
||||
@@ -755,7 +742,7 @@ void CameraState::camera_open(MultiCameraState *multi_cam_state_, int camera_num
|
||||
buf_desc[0].size = buf_desc[0].length = sizeof(struct cam_csiphy_info);
|
||||
buf_desc[0].type = CAM_CMD_BUF_GENERIC;
|
||||
|
||||
struct cam_csiphy_info *csiphy_info = (struct cam_csiphy_info *)mm.alloc(buf_desc[0].size, (uint32_t*)&buf_desc[0].mem_handle);
|
||||
auto csiphy_info = mm.alloc<struct cam_csiphy_info>(buf_desc[0].size, (uint32_t*)&buf_desc[0].mem_handle);
|
||||
csiphy_info->lane_mask = 0x1f;
|
||||
csiphy_info->lane_assign = 0x3210;// skip clk. How is this 16 bit for 5 channels??
|
||||
csiphy_info->csiphy_3phase = 0x0; // no 3 phase, only 2 conductors per lane
|
||||
@@ -767,9 +754,6 @@ void CameraState::camera_open(MultiCameraState *multi_cam_state_, int camera_num
|
||||
|
||||
int ret_ = device_config(csiphy_fd, session_handle, csiphy_dev_handle, cam_packet_handle);
|
||||
assert(ret_ == 0);
|
||||
|
||||
mm.free(csiphy_info);
|
||||
mm.free(pkt);
|
||||
}
|
||||
|
||||
// link devices
|
||||
|
||||
@@ -98,7 +98,7 @@ void release_fd(int video0_fd, uint32_t handle) {
|
||||
release(video0_fd, handle);
|
||||
}
|
||||
|
||||
void *MemoryManager::alloc(int size, uint32_t *handle) {
|
||||
void *MemoryManager::alloc_buf(int size, uint32_t *handle) {
|
||||
lock.lock();
|
||||
void *ptr;
|
||||
if (!cached_allocations[size].empty()) {
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <optional>
|
||||
#include <queue>
|
||||
@@ -18,10 +20,17 @@ void release(int video0_fd, uint32_t handle);
|
||||
class MemoryManager {
|
||||
public:
|
||||
void init(int _video0_fd) { video0_fd = _video0_fd; }
|
||||
void *alloc(int len, uint32_t *handle);
|
||||
void free(void *ptr);
|
||||
~MemoryManager();
|
||||
|
||||
template <class T>
|
||||
auto alloc(int len, uint32_t *handle) {
|
||||
return std::unique_ptr<T, std::function<void(void *)>>((T*)alloc_buf(len, handle), [this](void *ptr) { this->free(ptr); });
|
||||
}
|
||||
|
||||
private:
|
||||
void *alloc_buf(int len, uint32_t *handle);
|
||||
void free(void *ptr);
|
||||
|
||||
std::mutex lock;
|
||||
std::map<void *, uint32_t> handle_lookup;
|
||||
std::map<void *, int> size_lookup;
|
||||
|
||||
Reference in New Issue
Block a user