1
0
mirror of https://github.com/libretro/RetroArch synced 2025-04-01 22:20:31 +00:00

oga_gfx: strlcpy, msg max len 128 + style nits

This commit is contained in:
valadaa48 2020-09-19 11:16:30 -04:00
parent 2a3000793b
commit 15708f5d44

@ -61,15 +61,15 @@ typedef struct oga_rect
typedef struct oga_surface typedef struct oga_surface
{ {
uint8_t* map; uint8_t* map;
int width; int width;
int height; int height;
int pitch; int pitch;
int prime_fd; int prime_fd;
int rk_format; int rk_format;
int display_fd; int display_fd;
uint32_t handle; uint32_t handle;
} oga_surface_t; } oga_surface_t;
typedef struct oga_framebuf typedef struct oga_framebuf
@ -80,199 +80,199 @@ typedef struct oga_framebuf
typedef struct oga_video typedef struct oga_video
{ {
int fd; int fd;
uint32_t connector_id; uint32_t connector_id;
drmModeModeInfo mode; drmModeModeInfo mode;
#if 0 #if 0
uint32_t width; uint32_t width;
uint32_t height; uint32_t height;
#endif #endif
uint32_t crtc_id; uint32_t crtc_id;
oga_surface_t* frame_surface; oga_surface_t* frame_surface;
oga_surface_t* menu_surface; oga_surface_t* menu_surface;
oga_framebuf_t* pages[NUM_PAGES]; oga_framebuf_t* pages[NUM_PAGES];
int cur_page; int cur_page;
int scale_mode; int scale_mode;
int rotation; int rotation;
bool threaded; bool threaded;
oga_surface_t* msg_surface; oga_surface_t* msg_surface;
const font_renderer_driver_t *font_driver; const font_renderer_driver_t *font_driver;
void *font; void *font;
int msg_width; int msg_width;
int msg_height; int msg_height;
char last_msg[1024]; char last_msg[128];
int bpp; int bpp;
} oga_video_t; } oga_video_t;
bool oga_create_display(oga_video_t* vid) bool oga_create_display(oga_video_t* vid)
{ {
int i, ret; int i, ret;
drmModeConnector *connector; drmModeConnector *connector;
drmModeModeInfo *mode; drmModeModeInfo *mode;
drmModeEncoder *encoder; drmModeEncoder *encoder;
drmModeRes *resources; drmModeRes *resources;
vid->fd = open("/dev/dri/card0", O_RDWR); vid->fd = open("/dev/dri/card0", O_RDWR);
if (vid->fd < 0) if (vid->fd < 0)
{ {
RARCH_ERR("open /dev/dri/card0 failed.\n"); RARCH_ERR("open /dev/dri/card0 failed.\n");
return false; return false;
} }
resources = drmModeGetResources(vid->fd); resources = drmModeGetResources(vid->fd);
if (!resources) if (!resources)
{ {
RARCH_ERR("drmModeGetResources failed: %s\n", strerror(errno)); RARCH_ERR("drmModeGetResources failed: %s\n", strerror(errno));
goto err_01; goto err_01;
} }
for (i = 0; i < resources->count_connectors; i++) for (i = 0; i < resources->count_connectors; i++)
{ {
connector = drmModeGetConnector(vid->fd, resources->connectors[i]); connector = drmModeGetConnector(vid->fd, resources->connectors[i]);
if (connector->connection == DRM_MODE_CONNECTED) if (connector->connection == DRM_MODE_CONNECTED)
break; break;
drmModeFreeConnector(connector); drmModeFreeConnector(connector);
connector = NULL; connector = NULL;
} }
if (!connector) if (!connector)
{ {
RARCH_ERR("DRM_MODE_CONNECTED not found.\n"); RARCH_ERR("DRM_MODE_CONNECTED not found.\n");
goto err_02; goto err_02;
} }
vid->connector_id = connector->connector_id; vid->connector_id = connector->connector_id;
/* Find prefered mode */ /* Find prefered mode */
for (i = 0; i < connector->count_modes; i++) for (i = 0; i < connector->count_modes; i++)
{ {
drmModeModeInfo *current_mode = &connector->modes[i]; drmModeModeInfo *current_mode = &connector->modes[i];
if (current_mode->type & DRM_MODE_TYPE_PREFERRED) if (current_mode->type & DRM_MODE_TYPE_PREFERRED)
{ {
mode = current_mode; mode = current_mode;
break; break;
} }
mode = NULL; mode = NULL;
} }
if (!mode) if (!mode)
{ {
RARCH_ERR("DRM_MODE_TYPE_PREFERRED not found.\n"); RARCH_ERR("DRM_MODE_TYPE_PREFERRED not found.\n");
goto err_03; goto err_03;
} }
vid->mode = *mode; vid->mode = *mode;
#if 0 #if 0
vid->width = mode->hdisplay; vid->width = mode->hdisplay;
vid->height = mode->vdisplay; vid->height = mode->vdisplay;
#endif #endif
/* Find encoder */ /* Find encoder */
for (i = 0; i < resources->count_encoders; i++) for (i = 0; i < resources->count_encoders; i++)
{ {
encoder = drmModeGetEncoder(vid->fd, resources->encoders[i]); encoder = drmModeGetEncoder(vid->fd, resources->encoders[i]);
if (encoder->encoder_id == connector->encoder_id) if (encoder->encoder_id == connector->encoder_id)
break; break;
drmModeFreeEncoder(encoder); drmModeFreeEncoder(encoder);
encoder = NULL; encoder = NULL;
} }
if (!encoder) if (!encoder)
{ {
RARCH_ERR("could not find encoder!\n"); RARCH_ERR("could not find encoder!\n");
goto err_03; goto err_03;
} }
vid->crtc_id = encoder->crtc_id; vid->crtc_id = encoder->crtc_id;
drmModeFreeEncoder(encoder); drmModeFreeEncoder(encoder);
drmModeFreeConnector(connector); drmModeFreeConnector(connector);
drmModeFreeResources(resources); drmModeFreeResources(resources);
return true; return true;
err_03: err_03:
drmModeFreeConnector(connector); drmModeFreeConnector(connector);
err_02: err_02:
drmModeFreeResources(resources); drmModeFreeResources(resources);
err_01: err_01:
close(vid->fd); close(vid->fd);
return false; return false;
} }
oga_surface_t* oga_create_surface(int display_fd, oga_surface_t* oga_create_surface(int display_fd,
int width, int height, int rk_format) int width, int height, int rk_format)
{ {
struct drm_mode_create_dumb args = {0}; struct drm_mode_create_dumb args = {0};
oga_surface_t* surface = (oga_surface_t*) oga_surface_t* surface = (oga_surface_t*)
calloc(1, sizeof(oga_surface_t)); calloc(1, sizeof(oga_surface_t));
if (!surface) if (!surface)
{ {
RARCH_ERR("Error allocating surface\n"); RARCH_ERR("Error allocating surface\n");
return NULL; return NULL;
} }
args.width = width; args.width = width;
args.height = height; args.height = height;
args.bpp = rk_format == RK_FORMAT_BGRA_8888 ? 32 : 16; args.bpp = rk_format == RK_FORMAT_BGRA_8888 ? 32 : 16;
args.flags = 0; args.flags = 0;
if (drmIoctl(display_fd, DRM_IOCTL_MODE_CREATE_DUMB, &args) < 0) if (drmIoctl(display_fd, DRM_IOCTL_MODE_CREATE_DUMB, &args) < 0)
{ {
RARCH_ERR("DRM_IOCTL_MODE_CREATE_DUMB failed.\n"); RARCH_ERR("DRM_IOCTL_MODE_CREATE_DUMB failed.\n");
goto out; goto out;
} }
surface->display_fd = display_fd; surface->display_fd = display_fd;
surface->handle = args.handle; surface->handle = args.handle;
surface->width = width; surface->width = width;
surface->height = height; surface->height = height;
surface->pitch = args.pitch; surface->pitch = args.pitch;
surface->rk_format = rk_format; surface->rk_format = rk_format;
if (drmPrimeHandleToFD(display_fd, surface->handle, DRM_RDWR | DRM_CLOEXEC, &surface->prime_fd) < 0) if (drmPrimeHandleToFD(display_fd, surface->handle, DRM_RDWR | DRM_CLOEXEC, &surface->prime_fd) < 0)
{ {
RARCH_ERR("drmPrimeHandleToFD failed.\n"); RARCH_ERR("drmPrimeHandleToFD failed.\n");
goto out; goto out;
} }
surface->map = mmap(NULL, args.size, PROT_READ | PROT_WRITE, MAP_SHARED, surface->prime_fd, 0); surface->map = mmap(NULL, args.size, PROT_READ | PROT_WRITE, MAP_SHARED, surface->prime_fd, 0);
if (surface->map == MAP_FAILED) if (surface->map == MAP_FAILED)
{ {
RARCH_LOG("mmap failed.\n"); RARCH_LOG("mmap failed.\n");
return NULL; return NULL;
} }
return surface; return surface;
out: out:
free(surface); free(surface);
return NULL; return NULL;
} }
void oga_destroy_surface(oga_surface_t* surface) void oga_destroy_surface(oga_surface_t* surface)
{ {
int io; int io;
struct drm_mode_destroy_dumb args = { 0 }; struct drm_mode_destroy_dumb args = { 0 };
args.handle = surface->handle; args.handle = surface->handle;
io = drmIoctl(surface->display_fd, io = drmIoctl(surface->display_fd,
DRM_IOCTL_MODE_DESTROY_DUMB, &args); DRM_IOCTL_MODE_DESTROY_DUMB, &args);
if (io < 0) if (io < 0)
RARCH_ERR("DRM_IOCTL_MODE_DESTROY_DUMB failed.\n"); RARCH_ERR("DRM_IOCTL_MODE_DESTROY_DUMB failed.\n");
free(surface); free(surface);
} }
oga_framebuf_t* oga_create_framebuf(oga_surface_t* surface) oga_framebuf_t* oga_create_framebuf(oga_surface_t* surface)
@ -282,7 +282,7 @@ oga_framebuf_t* oga_create_framebuf(oga_surface_t* surface)
const uint32_t pitches[4] = {surface->pitch, 0, 0, 0}; const uint32_t pitches[4] = {surface->pitch, 0, 0, 0};
const uint32_t offsets[4] = {0, 0, 0, 0}; const uint32_t offsets[4] = {0, 0, 0, 0};
oga_framebuf_t* framebuf = calloc(1, sizeof(oga_framebuf_t)); oga_framebuf_t* framebuf = calloc(1, sizeof(oga_framebuf_t));
if (!framebuf) if (!framebuf)
{ {
RARCH_ERR("Error allocating framebuf\n"); RARCH_ERR("Error allocating framebuf\n");
@ -293,8 +293,8 @@ oga_framebuf_t* oga_create_framebuf(oga_surface_t* surface)
ret = drmModeAddFB2(surface->display_fd, ret = drmModeAddFB2(surface->display_fd,
surface->width, surface->width,
surface->height, surface->height,
surface->rk_format == RK_FORMAT_BGRA_8888 surface->rk_format == RK_FORMAT_BGRA_8888
? DRM_FORMAT_ARGB8888 ? DRM_FORMAT_ARGB8888
: DRM_FORMAT_RGB565, : DRM_FORMAT_RGB565,
handles, handles,
pitches, pitches,
@ -314,11 +314,11 @@ oga_framebuf_t* oga_create_framebuf(oga_surface_t* surface)
void oga_destroy_framebuf(oga_framebuf_t* framebuf) void oga_destroy_framebuf(oga_framebuf_t* framebuf)
{ {
if (drmModeRmFB(framebuf->surface->display_fd, framebuf->fb_id) != 0) if (drmModeRmFB(framebuf->surface->display_fd, framebuf->fb_id) != 0)
RARCH_ERR("drmModeRmFB failed.\n"); RARCH_ERR("drmModeRmFB failed.\n");
oga_destroy_surface(framebuf->surface); oga_destroy_surface(framebuf->surface);
free(framebuf); free(framebuf);
} }
static void oga_gfx_free(void *data) static void oga_gfx_free(void *data)
@ -349,7 +349,7 @@ static void oga_gfx_free(void *data)
} }
static void *oga_gfx_init(const video_info_t *video, static void *oga_gfx_init(const video_info_t *video,
input_driver_t **input, void **input_data) input_driver_t **input, void **input_data)
{ {
int i; int i;
oga_video_t *vid = NULL; oga_video_t *vid = NULL;
@ -399,7 +399,7 @@ static void *oga_gfx_init(const video_info_t *video,
* 1 MITCHELL * 1 MITCHELL
* 2 HERMITE * 2 HERMITE
* 3 B-SPLINE * 3 B-SPLINE
*/ */
vid->scale_mode = video->ctx_scaling << 1 | video->smooth; vid->scale_mode = video->ctx_scaling << 1 | video->smooth;
vid->rotation = 0; vid->rotation = 0;
@ -444,16 +444,16 @@ static void rga_clear_surface(oga_surface_t* surface, int color)
int get_message_width(oga_video_t* vid, const char* msg) int get_message_width(oga_video_t* vid, const char* msg)
{ {
int width = 0; int width = 0;
for (const char* c = msg; *c; c++) for (const char* c = msg; *c; c++)
{ {
const struct font_glyph* glyph = vid->font_driver->get_glyph(vid->font, *c); const struct font_glyph* glyph = vid->font_driver->get_glyph(vid->font, *c);
if (unlikely(!glyph)) if (unlikely(!glyph))
continue; continue;
width += glyph->advance_x; width += glyph->advance_x;
} }
return width; return width;
} }
static bool render_msg(oga_video_t* vid, const char* msg) static bool render_msg(oga_video_t* vid, const char* msg)
@ -466,9 +466,9 @@ static bool render_msg(oga_video_t* vid, const char* msg)
int dest_stride; int dest_stride;
if (strcmp(msg, vid->last_msg) == 0) if (strcmp(msg, vid->last_msg) == 0)
return true; return true;
strncpy(vid->last_msg, c, sizeof(vid->last_msg)-1); strlcpy(vid->last_msg, c, sizeof(vid->last_msg));
rga_clear_surface(vid->msg_surface, 0); rga_clear_surface(vid->msg_surface, 0);
atlas = vid->font_driver->get_atlas(vid->font); atlas = vid->font_driver->get_atlas(vid->font);
@ -496,8 +496,8 @@ static bool render_msg(oga_video_t* vid, const char* msg)
vid->msg_height += g->height; vid->msg_height += g->height;
} }
source = atlas->buffer + g->atlas_offset_y * source = atlas->buffer + g->atlas_offset_y *
atlas->width + g->atlas_offset_x; atlas->width + g->atlas_offset_x;
dest = fb + dest_y * dest_stride + dest_x; dest = fb + dest_y * dest_stride + dest_x;
for (y = 0; y < g->height; y++) for (y = 0; y < g->height; y++)
@ -526,22 +526,22 @@ void oga_blit(oga_surface_t* src, int sx, int sy, int sw, int sh,
oga_surface_t* dst, int dx, int dy, int dw, int dh, oga_surface_t* dst, int dx, int dy, int dw, int dh,
int rotation, int scale_mode, unsigned int blend) int rotation, int scale_mode, unsigned int blend)
{ {
rga_info_t s = { rga_info_t s = {
.fd = src->prime_fd, .fd = src->prime_fd,
.rect = { sx, sy, sw, sh, src->width, src->height, src->rk_format }, .rect = { sx, sy, sw, sh, src->width, src->height, src->rk_format },
.rotation = rotation, .rotation = rotation,
.mmuFlag = 1, .mmuFlag = 1,
.scale_mode = scale_mode, .scale_mode = scale_mode,
.blend = blend, .blend = blend,
}; };
rga_info_t d = { rga_info_t d = {
.fd = dst->prime_fd, .fd = dst->prime_fd,
.rect = { dx, dy, dw, dh, dst->width, dst->height, dst->rk_format }, .rect = { dx, dy, dw, dh, dst->width, dst->height, dst->rk_format },
.mmuFlag = 1, .mmuFlag = 1,
}; };
c_RkRgaBlit(&s, &d, NULL); c_RkRgaBlit(&s, &d, NULL);
} }
void oga_calc_bounds(oga_rect_t* r, int width, int height, float aspect) void oga_calc_bounds(oga_rect_t* r, int width, int height, float aspect)
@ -563,8 +563,8 @@ void oga_calc_bounds(oga_rect_t* r, int width, int height, float aspect)
} }
static bool oga_gfx_frame(void *data, const void *frame, unsigned width, static bool oga_gfx_frame(void *data, const void *frame, unsigned width,
unsigned height, uint64_t frame_count, unsigned height, uint64_t frame_count,
unsigned pitch, const char *msg, video_frame_info_t *video_info) unsigned pitch, const char *msg, video_frame_info_t *video_info)
{ {
oga_video_t *vid = (oga_video_t*)data; oga_video_t *vid = (oga_video_t*)data;
oga_framebuf_t* page = vid->pages[vid->cur_page]; oga_framebuf_t* page = vid->pages[vid->cur_page];
@ -638,7 +638,7 @@ static bool oga_gfx_frame(void *data, const void *frame, unsigned width,
} }
static void oga_gfx_set_texture_frame(void *data, const void *frame, bool rgb32, static void oga_gfx_set_texture_frame(void *data, const void *frame, bool rgb32,
unsigned width, unsigned height, float alpha) unsigned width, unsigned height, float alpha)
{ {
oga_video_t *vid = (oga_video_t*)data; oga_video_t *vid = (oga_video_t*)data;
unsigned i, j; unsigned i, j;
@ -674,7 +674,7 @@ static void oga_gfx_set_texture_frame(void *data, const void *frame, bool rgb32,
for (j = 0; j < src_pitch / 2; j++) for (j = 0; j < src_pitch / 2; j++)
{ {
uint16_t src_pix = *((uint16_t*)frame + (src_pitch / 2 * i) + j); uint16_t src_pix = *((uint16_t*)frame + (src_pitch / 2 * i) + j);
/* The hex AND is for keeping only the part /* The hex AND is for keeping only the part
* we need for each component. */ * we need for each component. */
uint32_t R = (src_pix << 8) & 0x00FF0000; uint32_t R = (src_pix << 8) & 0x00FF0000;
uint32_t G = (src_pix << 4) & 0x0000FF00; uint32_t G = (src_pix << 4) & 0x0000FF00;
@ -705,13 +705,13 @@ static bool oga_gfx_has_windowed(void *data) { return false; }
static void oga_gfx_viewport_info(void *data, struct video_viewport *vp) static void oga_gfx_viewport_info(void *data, struct video_viewport *vp)
{ {
oga_video_t *vid = (oga_video_t*)data; oga_video_t *vid = (oga_video_t*)data;
if (unlikely(!vid)) if (unlikely(!vid))
return; return;
vp->x = vp->y = 0; vp->x = vp->y = 0;
vp->width = vp->full_width = NATIVE_WIDTH; vp->width = vp->full_width = NATIVE_WIDTH;
vp->height = vp->full_height = NATIVE_HEIGHT; vp->height = vp->full_height = NATIVE_HEIGHT;
} }
static bool oga_gfx_set_shader(void *data, enum rarch_shader_type type, const char *path) static bool oga_gfx_set_shader(void *data, enum rarch_shader_type type, const char *path)
@ -731,74 +731,74 @@ void oga_set_rotation(void *data, unsigned rotation)
switch (rotation) switch (rotation)
{ {
case 0: case 0:
vid->rotation = HAL_TRANSFORM_ROT_270; vid->rotation = HAL_TRANSFORM_ROT_270;
break; break;
case 1: case 1:
vid->rotation = HAL_TRANSFORM_ROT_180; vid->rotation = HAL_TRANSFORM_ROT_180;
break; break;
case 2: case 2:
vid->rotation = HAL_TRANSFORM_ROT_90; vid->rotation = HAL_TRANSFORM_ROT_90;
break; break;
case 3: case 3:
vid->rotation = 0; vid->rotation = 0;
break; break;
default: default:
RARCH_ERR("Unhandled rotation %hu\n", rotation); RARCH_ERR("Unhandled rotation %hu\n", rotation);
break; break;
} }
} }
static const video_poke_interface_t oga_poke_interface = { static const video_poke_interface_t oga_poke_interface = {
NULL, NULL,
NULL, NULL,
NULL, NULL,
NULL, NULL,
NULL, NULL,
NULL, NULL,
NULL, NULL,
NULL, NULL,
NULL, NULL,
NULL, NULL,
NULL, NULL,
NULL, NULL,
NULL, NULL,
oga_gfx_set_texture_frame, oga_gfx_set_texture_frame,
oga_gfx_texture_enable, oga_gfx_texture_enable,
NULL, NULL,
NULL, NULL,
NULL, NULL,
NULL, NULL,
NULL, NULL,
NULL NULL
}; };
static void oga_get_poke_interface(void *data, const video_poke_interface_t **iface) static void oga_get_poke_interface(void *data, const video_poke_interface_t **iface)
{ {
*iface = &oga_poke_interface; *iface = &oga_poke_interface;
} }
video_driver_t video_oga = { video_driver_t video_oga = {
oga_gfx_init, oga_gfx_init,
oga_gfx_frame, oga_gfx_frame,
oga_gfx_set_nonblock_state, oga_gfx_set_nonblock_state,
oga_gfx_alive, oga_gfx_alive,
oga_gfx_focus, oga_gfx_focus,
oga_gfx_suppress_screensaver, oga_gfx_suppress_screensaver,
oga_gfx_has_windowed, oga_gfx_has_windowed,
oga_gfx_set_shader, oga_gfx_set_shader,
oga_gfx_free, oga_gfx_free,
"oga", "oga",
NULL, NULL,
oga_set_rotation, oga_set_rotation,
oga_gfx_viewport_info, oga_gfx_viewport_info,
NULL, NULL,
NULL, NULL,
#ifdef HAVE_OVERLAY #ifdef HAVE_OVERLAY
NULL, NULL,
#endif #endif
#ifdef HAVE_VIDEO_LAYOUT #ifdef HAVE_VIDEO_LAYOUT
NULL, NULL,
#endif #endif
oga_get_poke_interface oga_get_poke_interface
}; };