Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

visionbuf use EGL texture #310

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
make \
ocl-icd-opencl-dev \
opencl-headers \
libegl-dev \
python-openssl \
tk-dev \
wget \
Expand Down
1 change: 1 addition & 0 deletions SConscript
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ if arch == "Darwin":
vipc_frameworks.append('OpenCL')
else:
vipc_libs.append('OpenCL')
vipc_libs.append('EGL')
envCython.Program('visionipc/visionipc_pyx.so', 'visionipc/visionipc_pyx.pyx',
LIBS=vipc_libs, FRAMEWORKS=vipc_frameworks)

Expand Down
10 changes: 10 additions & 0 deletions visionipc/visionbuf.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
#include <CL/cl.h>
#endif

#define EGL_EGLEXT_PROTOTYPES
#define EGL_NO_X11
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <drm/drm_fourcc.h>

#define VISIONBUF_SYNC_FROM_DEVICE 0
#define VISIONBUF_SYNC_TO_DEVICE 1

Expand Down Expand Up @@ -51,12 +57,16 @@ class VisionBuf {
cl_mem buf_cl = nullptr;
cl_command_queue copy_q = nullptr;

// OpenGL
EGLImageKHR egl_image = EGL_NO_IMAGE_KHR;

// ion
int handle = 0;

void allocate(size_t len);
void import();
void init_cl(cl_device_id device_id, cl_context ctx);
void init_gl();
void init_rgb(size_t width, size_t height, size_t stride);
void init_yuv(size_t width, size_t height, size_t stride, size_t uv_offset);
int sync(int dir);
Expand Down
3 changes: 3 additions & 0 deletions visionipc/visionbuf_cl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ void VisionBuf::init_cl(cl_device_id device_id, cl_context ctx){
assert(err == 0);
}

void VisionBuf::init_gl() {
assert(false); // only used for visionbuf_ion
};

void VisionBuf::import(){
assert(this->fd >= 0);
Expand Down
23 changes: 23 additions & 0 deletions visionipc/visionbuf_ion.cc
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,24 @@ void VisionBuf::init_cl(cl_device_id device_id, cl_context ctx) {
assert(err == 0);
}

void VisionBuf::init_gl() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any reason to do this here instead of UI?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the image/texture is linked to the shared buffers, so it requires a texture pointer for every visionbuf object, so I guess it makes more sense to put it there

EGLDisplay display = eglGetCurrentDisplay();

EGLint img_attrs[] = {
EGL_WIDTH, (int)this->width,
EGL_HEIGHT, (int)this->height,
EGL_LINUX_DRM_FOURCC_EXT, DRM_FORMAT_NV12,
EGL_DMA_BUF_PLANE0_FD_EXT, this->fd,
EGL_DMA_BUF_PLANE0_OFFSET_EXT, 0,
EGL_DMA_BUF_PLANE0_PITCH_EXT, (int)this->stride,
EGL_DMA_BUF_PLANE1_FD_EXT, this->fd,
EGL_DMA_BUF_PLANE1_OFFSET_EXT, (int)this->uv_offset,
EGL_DMA_BUF_PLANE1_PITCH_EXT, (int)this->stride,
EGL_NONE
};
this->egl_image = eglCreateImageKHR(display, EGL_NO_CONTEXT, EGL_LINUX_DMA_BUF_EXT, 0, img_attrs);
assert(eglGetError() == EGL_SUCCESS);
}

int VisionBuf::sync(int dir) {
struct ion_flush_data flush_data = {0};
Expand All @@ -142,6 +160,11 @@ int VisionBuf::sync(int dir) {
int VisionBuf::free() {
int err = 0;

if (this->egl_image) {
EGLDisplay display = eglGetCurrentDisplay();
eglDestroyImageKHR(display, this->egl_image);
}

if (this->buf_cl){
err = clReleaseMemObject(this->buf_cl);
if (err != 0) return err;
Expand Down