Skip to content

Commit fa514e7

Browse files
committed
Reapply "visionipc: remove OpenCL support (#673)" (#675)
This reverts commit 4c4e814.
1 parent 4c4e814 commit fa514e7

13 files changed

Lines changed: 93 additions & 197 deletions

SConscript

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,25 @@ msgq = env.Library('msgq', msgq_objects)
1717
msgq_python = envCython.Program('msgq/ipc_pyx.so', 'msgq/ipc_pyx.pyx', LIBS=envCython["LIBS"]+[msgq, common])
1818

1919
# Build Vision IPC
20-
vipc_files = ['visionipc.cc', 'visionipc_server.cc', 'visionipc_client.cc', 'visionbuf.cc']
21-
vipc_sources = [f'{visionipc_dir.abspath}/{f}' for f in vipc_files]
22-
20+
vipc_files = ['visionipc.cc', 'visionipc_server.cc', 'visionipc_client.cc']
2321
if arch == "larch64":
24-
vipc_sources += [f'{visionipc_dir.abspath}/visionbuf_ion.cc']
22+
vipc_files += ['visionbuf_ion.cc']
2523
else:
26-
vipc_sources += [f'{visionipc_dir.abspath}/visionbuf_cl.cc']
24+
vipc_files += ['visionbuf.cc']
25+
vipc_sources = [f'{visionipc_dir.abspath}/{f}' for f in vipc_files]
2726

2827
vipc_objects = env.SharedObject(vipc_sources)
2928
visionipc = env.Library('visionipc', vipc_objects)
3029

3130

32-
vipc_frameworks = []
3331
vipc_libs = envCython["LIBS"] + [visionipc, msgq, common]
34-
if arch == "Darwin":
35-
vipc_frameworks.append('OpenCL')
36-
else:
37-
vipc_libs.append('OpenCL')
3832
envCython.Program(f'{visionipc_dir.abspath}/visionipc_pyx.so', f'{visionipc_dir.abspath}/visionipc_pyx.pyx',
39-
LIBS=vipc_libs, FRAMEWORKS=vipc_frameworks)
33+
LIBS=vipc_libs)
4034

4135
if GetOption('extras'):
4236
env.Program('msgq/test_runner', ['msgq/test_runner.cc', 'msgq/msgq_tests.cc'], LIBS=[msgq, common])
4337
env.Program(f'{visionipc_dir.abspath}/test_runner',
4438
[f'{visionipc_dir.abspath}/test_runner.cc', f'{visionipc_dir.abspath}/visionipc_tests.cc'],
45-
LIBS=['pthread'] + vipc_libs, FRAMEWORKS=vipc_frameworks)
39+
LIBS=['pthread'] + vipc_libs)
4640

4741
Export('visionipc', 'msgq', 'msgq_python')

msgq/visionipc/visionbuf.cc

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,52 @@
11
#include "msgq/visionipc/visionbuf.h"
22

3+
#include <atomic>
4+
#include <stdio.h>
5+
#include <fcntl.h>
6+
#include <assert.h>
7+
#include <stdlib.h>
8+
#include <unistd.h>
9+
#include <sys/mman.h>
10+
#include <sys/types.h>
11+
12+
std::atomic<int> offset = 0;
13+
14+
static void *malloc_with_fd(size_t len, int *fd) {
15+
char full_path[0x100];
16+
17+
#ifdef __APPLE__
18+
snprintf(full_path, sizeof(full_path)-1, "/tmp/visionbuf_%d_%d", getpid(), offset++);
19+
#else
20+
snprintf(full_path, sizeof(full_path)-1, "/dev/shm/msgq_visionbuf_%d_%d", getpid(), offset++);
21+
#endif
22+
23+
*fd = open(full_path, O_RDWR | O_CREAT, 0664);
24+
assert(*fd >= 0);
25+
26+
unlink(full_path);
27+
28+
ftruncate(*fd, len);
29+
void *addr = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, *fd, 0);
30+
assert(addr != MAP_FAILED);
31+
32+
return addr;
33+
}
34+
35+
void VisionBuf::allocate(size_t length) {
36+
this->len = length;
37+
this->mmap_len = this->len + sizeof(uint64_t);
38+
this->addr = malloc_with_fd(this->mmap_len, &this->fd);
39+
this->frame_id = (uint64_t*)((uint8_t*)this->addr + this->len);
40+
}
41+
42+
void VisionBuf::import(){
43+
assert(this->fd >= 0);
44+
this->addr = mmap(NULL, this->mmap_len, PROT_READ | PROT_WRITE, MAP_SHARED, this->fd, 0);
45+
assert(this->addr != MAP_FAILED);
46+
47+
this->frame_id = (uint64_t*)((uint8_t*)this->addr + this->len);
48+
}
49+
350
void VisionBuf::init_yuv(size_t init_width, size_t init_height, size_t init_stride, size_t init_uv_offset){
451
this->width = init_width;
552
this->height = init_height;
@@ -10,6 +57,17 @@ void VisionBuf::init_yuv(size_t init_width, size_t init_height, size_t init_stri
1057
this->uv = this->y + this->uv_offset;
1158
}
1259

60+
int VisionBuf::sync(int dir) {
61+
return 0;
62+
}
63+
64+
int VisionBuf::free() {
65+
int err = munmap(this->addr, this->mmap_len);
66+
if (err != 0) return err;
67+
68+
err = close(this->fd);
69+
return err;
70+
}
1371

1472
uint64_t VisionBuf::get_frame_id() {
1573
return *frame_id;

msgq/visionipc/visionbuf.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,6 @@
22

33
#include "msgq/visionipc/visionipc.h"
44

5-
#define CL_USE_DEPRECATED_OPENCL_1_2_APIS
6-
#ifdef __APPLE__
7-
#include <OpenCL/cl.h>
8-
#else
9-
#include <CL/cl.h>
10-
#endif
11-
125
#define VISIONBUF_SYNC_FROM_DEVICE 0
136
#define VISIONBUF_SYNC_TO_DEVICE 1
147

@@ -43,16 +36,11 @@ class VisionBuf {
4336
size_t idx = 0;
4437
VisionStreamType type;
4538

46-
// OpenCL
47-
cl_mem buf_cl = nullptr;
48-
cl_command_queue copy_q = nullptr;
49-
5039
// ion
5140
int handle = 0;
5241

5342
void allocate(size_t len);
5443
void import();
55-
void init_cl(cl_device_id device_id, cl_context ctx);
5644
void init_yuv(size_t width, size_t height, size_t stride, size_t uv_offset);
5745
int sync(int dir);
5846
int free();

msgq/visionipc/visionbuf_cl.cc

Lines changed: 0 additions & 94 deletions
This file was deleted.

msgq/visionipc/visionbuf_ion.cc

Lines changed: 19 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include <fcntl.h>
1111
#include <unistd.h>
1212
#include <linux/ion.h>
13-
#include <CL/cl_ext.h>
1413

1514
#include <msm_ion.h>
1615

@@ -27,19 +26,6 @@
2726
ret; \
2827
})
2928

30-
// just hard-code these for convenience
31-
// size_t device_page_size = 0;
32-
// clGetDeviceInfo(device_id, CL_DEVICE_PAGE_SIZE_QCOM,
33-
// sizeof(device_page_size), &device_page_size,
34-
// NULL);
35-
36-
// size_t padding_cl = 0;
37-
// clGetDeviceInfo(device_id, CL_DEVICE_EXT_MEM_PADDING_IN_BYTES_QCOM,
38-
// sizeof(padding_cl), &padding_cl,
39-
// NULL);
40-
#define DEVICE_PAGE_SIZE_CL 4096
41-
#define PADDING_CL 0
42-
4329
struct IonFileHandle {
4430
IonFileHandle() {
4531
fd = open("/dev/ion", O_RDWR | O_NONBLOCK);
@@ -58,7 +44,7 @@ int ion_fd() {
5844

5945
void VisionBuf::allocate(size_t length) {
6046
struct ion_allocation_data ion_alloc = {0};
61-
ion_alloc.len = length + PADDING_CL + sizeof(uint64_t);
47+
ion_alloc.len = length + sizeof(uint64_t);
6248
ion_alloc.align = 4096;
6349
ion_alloc.heap_id_mask = 1 << ION_IOMMU_HEAP_ID;
6450
ion_alloc.flags = ION_FLAG_CACHED;
@@ -83,7 +69,7 @@ void VisionBuf::allocate(size_t length) {
8369
this->addr = mmap_addr;
8470
this->handle = ion_alloc.handle;
8571
this->fd = ion_fd_data.fd;
86-
this->frame_id = (uint64_t*)((uint8_t*)this->addr + this->len + PADDING_CL);
72+
this->frame_id = (uint64_t*)((uint8_t*)this->addr + this->len);
8773
}
8874

8975
void VisionBuf::import(){
@@ -100,27 +86,19 @@ void VisionBuf::import(){
10086
this->addr = mmap(NULL, this->mmap_len, PROT_READ | PROT_WRITE, MAP_SHARED, this->fd, 0);
10187
assert(this->addr != MAP_FAILED);
10288

103-
this->frame_id = (uint64_t*)((uint8_t*)this->addr + this->len + PADDING_CL);
89+
this->frame_id = (uint64_t*)((uint8_t*)this->addr + this->len);
10490
}
10591

106-
void VisionBuf::init_cl(cl_device_id device_id, cl_context ctx) {
107-
int err;
108-
109-
assert(((uintptr_t)this->addr % DEVICE_PAGE_SIZE_CL) == 0);
92+
void VisionBuf::init_yuv(size_t init_width, size_t init_height, size_t init_stride, size_t init_uv_offset){
93+
this->width = init_width;
94+
this->height = init_height;
95+
this->stride = init_stride;
96+
this->uv_offset = init_uv_offset;
11097

111-
cl_mem_ion_host_ptr ion_cl = {0};
112-
ion_cl.ext_host_ptr.allocation_type = CL_MEM_ION_HOST_PTR_QCOM;
113-
ion_cl.ext_host_ptr.host_cache_policy = CL_MEM_HOST_UNCACHED_QCOM;
114-
ion_cl.ion_filedesc = this->fd;
115-
ion_cl.ion_hostptr = this->addr;
116-
117-
this->buf_cl = clCreateBuffer(ctx,
118-
CL_MEM_USE_HOST_PTR | CL_MEM_EXT_HOST_PTR_QCOM,
119-
this->len, &ion_cl, &err);
120-
assert(err == 0);
98+
this->y = (uint8_t *)this->addr;
99+
this->uv = this->y + this->uv_offset;
121100
}
122101

123-
124102
int VisionBuf::sync(int dir) {
125103
struct ion_flush_data flush_data = {0};
126104
flush_data.handle = this->handle;
@@ -143,14 +121,7 @@ int VisionBuf::sync(int dir) {
143121
}
144122

145123
int VisionBuf::free() {
146-
int err = 0;
147-
148-
if (this->buf_cl){
149-
err = clReleaseMemObject(this->buf_cl);
150-
if (err != 0) return err;
151-
}
152-
153-
err = munmap(this->addr, this->mmap_len);
124+
int err = munmap(this->addr, this->mmap_len);
154125
if (err != 0) return err;
155126

156127
err = close(this->fd);
@@ -159,3 +130,11 @@ int VisionBuf::free() {
159130
struct ion_handle_data handle_data = {.handle = this->handle};
160131
return HANDLE_EINTR(ioctl(ion_fd(), ION_IOC_FREE, &handle_data));
161132
}
133+
134+
uint64_t VisionBuf::get_frame_id() {
135+
return *frame_id;
136+
}
137+
138+
void VisionBuf::set_frame_id(uint64_t id) {
139+
*frame_id = id;
140+
}

msgq/visionipc/visionipc.pxd

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,6 @@ from libc.stdint cimport uint32_t, uint64_t
88
from libcpp cimport bool, int
99

1010
cdef extern from "msgq/visionipc/visionbuf.h":
11-
struct _cl_device_id
12-
struct _cl_context
13-
struct _cl_mem
14-
15-
ctypedef _cl_device_id * cl_device_id
16-
ctypedef _cl_context * cl_context
17-
ctypedef _cl_mem * cl_mem
18-
1911
cdef enum VisionStreamType:
2012
pass
2113

@@ -28,7 +20,6 @@ cdef extern from "msgq/visionipc/visionbuf.h":
2820
size_t stride
2921
size_t uv_offset
3022
size_t idx
31-
cl_mem buf_cl
3223
void set_frame_id(uint64_t id)
3324

3425
cdef extern from "msgq/visionipc/visionipc.h":
@@ -42,7 +33,7 @@ cdef extern from "msgq/visionipc/visionipc_server.h":
4233
string get_endpoint_name(string, VisionStreamType)
4334

4435
cdef cppclass VisionIpcServer:
45-
VisionIpcServer(string, void*, void*)
36+
VisionIpcServer(string)
4637
void create_buffers(VisionStreamType, size_t, size_t, size_t)
4738
void create_buffers_with_sizes(VisionStreamType, size_t, size_t, size_t, size_t, size_t, size_t)
4839
VisionBuf * get_buffer(VisionStreamType)
@@ -53,7 +44,7 @@ cdef extern from "msgq/visionipc/visionipc_client.h":
5344
cdef cppclass VisionIpcClient:
5445
int num_buffers
5546
VisionBuf buffers[1]
56-
VisionIpcClient(string, VisionStreamType, bool, void*, void*)
47+
VisionIpcClient(string, VisionStreamType, bool)
5748
VisionBuf * recv(VisionIpcBufExtra *, int)
5849
bool connect(bool)
5950
bool is_connected()

0 commit comments

Comments
 (0)