mirror of
https://github.com/sunnypilot/sunnypilot.git
synced 2026-07-11 22:32:07 +08:00
3f8e95952a
version: sunnypilot v2026.002.000 (dev)
date: 2026-06-29T03:29:47
master commit: 31dc4d8e52
80 lines
1.9 KiB
C++
80 lines
1.9 KiB
C++
#include "msgq/visionipc/visionbuf.h"
|
|
|
|
#include <atomic>
|
|
#include <stdio.h>
|
|
#include <fcntl.h>
|
|
#include <assert.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
#include <sys/mman.h>
|
|
#include <sys/types.h>
|
|
|
|
std::atomic<int> offset = 0;
|
|
|
|
static void *malloc_with_fd(size_t len, int *fd) {
|
|
char full_path[0x100];
|
|
|
|
#ifdef __APPLE__
|
|
snprintf(full_path, sizeof(full_path)-1, "/tmp/visionbuf_%d_%d", getpid(), offset++);
|
|
#else
|
|
snprintf(full_path, sizeof(full_path)-1, "/dev/shm/msgq_visionbuf_%d_%d", getpid(), offset++);
|
|
#endif
|
|
|
|
*fd = open(full_path, O_RDWR | O_CREAT, 0664);
|
|
assert(*fd >= 0);
|
|
|
|
unlink(full_path);
|
|
|
|
int ret = ftruncate(*fd, len);
|
|
assert(ret == 0);
|
|
void *addr = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, *fd, 0);
|
|
assert(addr != MAP_FAILED);
|
|
|
|
return addr;
|
|
}
|
|
|
|
void VisionBuf::allocate(size_t length) {
|
|
this->len = length;
|
|
this->mmap_len = this->len + sizeof(uint64_t);
|
|
this->addr = malloc_with_fd(this->mmap_len, &this->fd);
|
|
this->frame_id = (uint64_t*)((uint8_t*)this->addr + this->len);
|
|
}
|
|
|
|
void VisionBuf::import(){
|
|
assert(this->fd >= 0);
|
|
this->addr = mmap(NULL, this->mmap_len, PROT_READ | PROT_WRITE, MAP_SHARED, this->fd, 0);
|
|
assert(this->addr != MAP_FAILED);
|
|
|
|
this->frame_id = (uint64_t*)((uint8_t*)this->addr + this->len);
|
|
}
|
|
|
|
void VisionBuf::init_yuv(size_t init_width, size_t init_height, size_t init_stride, size_t init_uv_offset){
|
|
this->width = init_width;
|
|
this->height = init_height;
|
|
this->stride = init_stride;
|
|
this->uv_offset = init_uv_offset;
|
|
|
|
this->y = (uint8_t *)this->addr;
|
|
this->uv = this->y + this->uv_offset;
|
|
}
|
|
|
|
int VisionBuf::sync(int dir) {
|
|
return 0;
|
|
}
|
|
|
|
int VisionBuf::free() {
|
|
int err = munmap(this->addr, this->mmap_len);
|
|
if (err != 0) return err;
|
|
|
|
err = close(this->fd);
|
|
return err;
|
|
}
|
|
|
|
uint64_t VisionBuf::get_frame_id() {
|
|
return *frame_id;
|
|
}
|
|
|
|
void VisionBuf::set_frame_id(uint64_t id) {
|
|
*frame_id = id;
|
|
}
|