mirror of
https://github.com/dragonpilot/dragonpilot.git
synced 2026-07-08 22:42:05 +08:00
Refactor image texture stuff into class (#19719)
* Refactor image texture stuff into class * cleanup * Update selfdrive/common/visionimg.cc Co-authored-by: Willem Melching <willem.melching@gmail.com>
This commit is contained in:
@@ -1,99 +1,62 @@
|
||||
#include <cassert>
|
||||
#include "common/visionimg.h"
|
||||
|
||||
#ifdef QCOM
|
||||
|
||||
#include <system/graphics.h>
|
||||
#include <ui/GraphicBuffer.h>
|
||||
#include <ui/PixelFormat.h>
|
||||
#include <gralloc_priv.h>
|
||||
|
||||
#include <GLES3/gl3.h>
|
||||
#define GL_GLEXT_PROTOTYPES
|
||||
#include <GLES2/gl2ext.h>
|
||||
|
||||
#include <EGL/egl.h>
|
||||
#define EGL_EGLEXT_PROTOTYPES
|
||||
#include <EGL/eglext.h>
|
||||
|
||||
#else // ifdef QCOM
|
||||
|
||||
#ifdef __APPLE__
|
||||
#include <OpenGL/gl3.h>
|
||||
#else
|
||||
#include <GLES3/gl3.h>
|
||||
#endif
|
||||
|
||||
#endif // ifdef QCOM
|
||||
|
||||
#include "common/visionimg.h"
|
||||
|
||||
#ifdef QCOM
|
||||
using namespace android;
|
||||
|
||||
static EGLClientBuffer visionimg_to_egl(const VisionImg *img, void **pph) {
|
||||
assert((img->size % img->stride) == 0);
|
||||
assert((img->stride % img->bpp) == 0);
|
||||
EGLImageTexture::EGLImageTexture(const VisionBuf *buf) {
|
||||
const int bpp = 3;
|
||||
assert((buf->len % buf->stride) == 0);
|
||||
assert((buf->stride % bpp) == 0);
|
||||
|
||||
int format = 0;
|
||||
if (img->format == VISIONIMG_FORMAT_RGB24) {
|
||||
format = HAL_PIXEL_FORMAT_RGB_888;
|
||||
} else {
|
||||
assert(false);
|
||||
}
|
||||
|
||||
private_handle_t* hnd = new private_handle_t(img->fd, img->size,
|
||||
const int format = HAL_PIXEL_FORMAT_RGB_888;
|
||||
private_handle = new private_handle_t(buf->fd, buf->len,
|
||||
private_handle_t::PRIV_FLAGS_USES_ION|private_handle_t::PRIV_FLAGS_FRAMEBUFFER,
|
||||
0, format,
|
||||
img->stride/img->bpp, img->size/img->stride,
|
||||
img->width, img->height);
|
||||
buf->stride/bpp, buf->len/buf->stride,
|
||||
buf->width, buf->height);
|
||||
|
||||
GraphicBuffer* gb = new GraphicBuffer(img->width, img->height, (PixelFormat)format,
|
||||
GraphicBuffer::USAGE_HW_TEXTURE, img->stride/img->bpp, hnd, false);
|
||||
// GraphicBuffer is ref counted by EGLClientBuffer(ANativeWindowBuffer), no need and not possible to release.
|
||||
*pph = hnd;
|
||||
return (EGLClientBuffer) gb->getNativeBuffer();
|
||||
}
|
||||
|
||||
GLuint visionimg_to_gl(const VisionImg *img, EGLImageKHR *pkhr, void **pph) {
|
||||
|
||||
EGLClientBuffer buf = visionimg_to_egl(img, pph);
|
||||
// GraphicBuffer is ref counted by EGLClientBuffer(ANativeWindowBuffer), no need and not possible to release.
|
||||
GraphicBuffer* gb = new GraphicBuffer(buf->width, buf->height, (PixelFormat)format,
|
||||
GraphicBuffer::USAGE_HW_TEXTURE, buf->stride/bpp, (private_handle_t*)private_handle, false);
|
||||
|
||||
EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
|
||||
assert(display != EGL_NO_DISPLAY);
|
||||
|
||||
EGLint img_attrs[] = { EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, EGL_NONE };
|
||||
EGLImageKHR image = eglCreateImageKHR(display, EGL_NO_CONTEXT,
|
||||
EGL_NATIVE_BUFFER_ANDROID, buf, img_attrs);
|
||||
assert(image != EGL_NO_IMAGE_KHR);
|
||||
EGLint img_attrs[] = {EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, EGL_NONE};
|
||||
img_khr = eglCreateImageKHR(display, EGL_NO_CONTEXT,
|
||||
EGL_NATIVE_BUFFER_ANDROID, gb->getNativeBuffer(), img_attrs);
|
||||
assert(img_khr != EGL_NO_IMAGE_KHR);
|
||||
|
||||
GLuint tex = 0;
|
||||
glGenTextures(1, &tex);
|
||||
glBindTexture(GL_TEXTURE_2D, tex);
|
||||
glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, image);
|
||||
*pkhr = image;
|
||||
return tex;
|
||||
glGenTextures(1, &frame_tex);
|
||||
glBindTexture(GL_TEXTURE_2D, frame_tex);
|
||||
glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, img_khr);
|
||||
}
|
||||
|
||||
void visionimg_destroy_gl(EGLImageKHR khr, void *ph) {
|
||||
EGLImageTexture::~EGLImageTexture() {
|
||||
glDeleteTextures(1, &frame_tex);
|
||||
EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
|
||||
assert(display != EGL_NO_DISPLAY);
|
||||
eglDestroyImageKHR(display, khr);
|
||||
delete (private_handle_t*)ph;
|
||||
eglDestroyImageKHR(display, img_khr);
|
||||
delete (private_handle_t*)private_handle;
|
||||
}
|
||||
|
||||
#else // ifdef QCOM
|
||||
|
||||
GLuint visionimg_to_gl(const VisionImg *img, EGLImageKHR *pkhr, void **pph) {
|
||||
unsigned int texture;
|
||||
glGenTextures(1, &texture);
|
||||
glBindTexture(GL_TEXTURE_2D, texture);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img->width, img->height, 0, GL_RGB, GL_UNSIGNED_BYTE, *pph);
|
||||
EGLImageTexture::EGLImageTexture(const VisionBuf *buf) {
|
||||
glGenTextures(1, &frame_tex);
|
||||
glBindTexture(GL_TEXTURE_2D, frame_tex);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, buf->width, buf->height, 0, GL_RGB, GL_UNSIGNED_BYTE, buf->addr);
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
*pkhr = (EGLImageKHR)1; // not NULL
|
||||
return texture;
|
||||
}
|
||||
|
||||
void visionimg_destroy_gl(EGLImageKHR khr, void *ph) {
|
||||
// empty
|
||||
EGLImageTexture::~EGLImageTexture() {
|
||||
glDeleteTextures(1, &frame_tex);
|
||||
}
|
||||
#endif // ifdef QCOM
|
||||
|
||||
@@ -1,25 +1,27 @@
|
||||
#pragma once
|
||||
|
||||
#include "visionbuf.h"
|
||||
#include "common/glutil.h"
|
||||
|
||||
#ifdef __APPLE__
|
||||
#include <OpenGL/gl3.h>
|
||||
#else
|
||||
#include <GLES3/gl3.h>
|
||||
#endif
|
||||
|
||||
#ifdef QCOM
|
||||
#include <EGL/egl.h>
|
||||
#define EGL_EGLEXT_PROTOTYPES
|
||||
#include <EGL/eglext.h>
|
||||
#undef Status
|
||||
#else
|
||||
typedef int EGLImageKHR;
|
||||
typedef void *EGLClientBuffer;
|
||||
#endif
|
||||
|
||||
#define VISIONIMG_FORMAT_RGB24 1
|
||||
|
||||
typedef struct VisionImg {
|
||||
int fd;
|
||||
int format;
|
||||
int width, height, stride;
|
||||
int bpp;
|
||||
size_t size;
|
||||
} VisionImg;
|
||||
|
||||
GLuint visionimg_to_gl(const VisionImg *img, EGLImageKHR *pkhr, void **pph);
|
||||
void visionimg_destroy_gl(EGLImageKHR khr, void *ph);
|
||||
class EGLImageTexture {
|
||||
public:
|
||||
EGLImageTexture(const VisionBuf *buf);
|
||||
~EGLImageTexture();
|
||||
GLuint frame_tex = 0;
|
||||
#ifdef QCOM
|
||||
void *private_handle = nullptr;
|
||||
EGLImageKHR img_khr = 0;
|
||||
#endif
|
||||
};
|
||||
|
||||
@@ -158,11 +158,11 @@ static void draw_frame(UIState *s) {
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
|
||||
if (s->last_frame) {
|
||||
glBindTexture(GL_TEXTURE_2D, s->frame_texs[s->last_frame->idx]);
|
||||
glBindTexture(GL_TEXTURE_2D, s->texture[s->last_frame->idx]->frame_tex);
|
||||
#ifndef QCOM
|
||||
// this is handled in ion on QCOM
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, s->last_frame->width, s->last_frame->height,
|
||||
0, GL_RGB, GL_UNSIGNED_BYTE, s->priv_hnds[s->last_frame->idx]);
|
||||
0, GL_RGB, GL_UNSIGNED_BYTE, s->last_frame->addr);
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -643,9 +643,4 @@ void ui_nvg_init(UIState *s) {
|
||||
|
||||
s->front_frame_mat = matmul(device_transform, full_to_wide_frame_transform);
|
||||
s->rear_frame_mat = matmul(device_transform, frame_transform);
|
||||
|
||||
for(int i = 0; i < UI_BUF_COUNT; i++) {
|
||||
s->khr[i] = 0;
|
||||
s->priv_hnds[i] = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
+2
-20
@@ -24,27 +24,9 @@ static void ui_init_vision(UIState *s) {
|
||||
s->scene.world_objects_visible = false;
|
||||
|
||||
for (int i = 0; i < s->vipc_client->num_buffers; i++) {
|
||||
if (s->khr[i] != 0) {
|
||||
visionimg_destroy_gl(s->khr[i], s->priv_hnds[i]);
|
||||
glDeleteTextures(1, &s->frame_texs[i]);
|
||||
}
|
||||
VisionBuf * buf = &s->vipc_client->buffers[i];
|
||||
s->texture[i].reset(new EGLImageTexture(&s->vipc_client->buffers[i]));
|
||||
|
||||
VisionImg img = {
|
||||
.fd = buf->fd,
|
||||
.format = VISIONIMG_FORMAT_RGB24,
|
||||
.width = (int)buf->width,
|
||||
.height = (int)buf->height,
|
||||
.stride = (int)buf->stride,
|
||||
.bpp = 3,
|
||||
.size = buf->len,
|
||||
};
|
||||
#ifndef QCOM
|
||||
s->priv_hnds[i] = buf->addr;
|
||||
#endif
|
||||
s->frame_texs[i] = visionimg_to_gl(&img, &s->khr[i], &s->priv_hnds[i]);
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, s->frame_texs[i]);
|
||||
glBindTexture(GL_TEXTURE_2D, s->texture[i]->frame_tex);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
|
||||
|
||||
+2
-3
@@ -13,6 +13,7 @@
|
||||
|
||||
#include <atomic>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
|
||||
@@ -171,9 +172,7 @@ typedef struct UIState {
|
||||
|
||||
// graphics
|
||||
GLuint frame_program;
|
||||
GLuint frame_texs[UI_BUF_COUNT];
|
||||
EGLImageKHR khr[UI_BUF_COUNT];
|
||||
void *priv_hnds[UI_BUF_COUNT];
|
||||
std::unique_ptr<EGLImageTexture> texture[UI_BUF_COUNT];
|
||||
|
||||
GLint frame_pos_loc, frame_texcoord_loc;
|
||||
GLint frame_texture_loc, frame_transform_loc;
|
||||
|
||||
Reference in New Issue
Block a user