Skip to content

Commit

Permalink
Get rid of some code duplication in MaterialUI/Ozone
Browse files Browse the repository at this point in the history
  • Loading branch information
LibretroAdmin committed Jun 15, 2023
1 parent 8ea67c0 commit bc16727
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 127 deletions.
13 changes: 13 additions & 0 deletions gfx/font_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,19 @@ void font_driver_bind_block(void *font_data, void *block)
font->renderer->bind_block(font->renderer_data, block);
}

/* Flushing is slow - only do it if font has actually been used */
void font_flush(
unsigned video_width,
unsigned video_height,
font_data_impl_t *font_data)
{
if (font_data->raster_block.carr.coords.vertices == 0)
return;
if (font_data->font && font_data->font->renderer && font_data->font->renderer->flush)
font_data->font->renderer->flush(video_width, video_height, font_data->font->renderer_data);
font_data->raster_block.carr.coords.vertices = 0;
}

int font_driver_get_message_width(void *font_data,
const char *msg, size_t len, float scale)
{
Expand Down
34 changes: 32 additions & 2 deletions gfx/font_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include <boolean.h>
#include <retro_common_api.h>
#include <retro_inline.h>

#include "../retroarch.h"

Expand Down Expand Up @@ -73,6 +74,32 @@ typedef struct
float size;
} font_data_t;

/* This structure holds all objects + metadata
* corresponding to a particular font */
typedef struct
{
font_data_t *font;
video_font_raster_block_t raster_block; /* ptr alignment */
unsigned glyph_width;
unsigned wideglyph_width;
int line_height;
int line_ascender;
int line_centre_offset;
} font_data_impl_t;

void font_driver_bind_block(void *font_data, void *block);

static void INLINE font_bind(font_data_impl_t *font_data)
{
font_driver_bind_block(font_data->font, &font_data->raster_block);
font_data->raster_block.carr.coords.vertices = 0;
}

static void INLINE font_unbind(font_data_impl_t *font_data)
{
font_driver_bind_block(font_data->font, NULL);
}

/* font_path can be NULL for default font. */
int font_renderer_create_default(
const font_renderer_driver_t **drv,
Expand All @@ -82,12 +109,15 @@ int font_renderer_create_default(
void font_driver_render_msg(void *data,
const char *msg, const struct font_params *params, void *font_data);

void font_driver_bind_block(void *font_data, void *block);

int font_driver_get_message_width(void *font_data, const char *msg, size_t len, float scale);

void font_driver_free(font_data_t *font);

void font_flush(
unsigned video_width,
unsigned video_height,
font_data_impl_t *font_data);

font_data_t *font_driver_init_first(
void *video_data,
const char *font_path,
Expand Down
75 changes: 18 additions & 57 deletions menu/drivers/materialui.c
Original file line number Diff line number Diff line change
Expand Up @@ -495,19 +495,6 @@ typedef struct
bool dragged;
} materialui_scrollbar_t;

/* This structure holds all objects + metadata
* corresponding to a particular font */
typedef struct
{
font_data_t *font;
video_font_raster_block_t raster_block; /* ptr alignment */
unsigned glyph_width;
unsigned wideglyph_width;
int line_height;
int line_ascender;
int line_centre_offset;
} materialui_font_data_t;

/* This structure is used to cache system bar
* string data (+ metadata) to improve rendering
* performance */
Expand Down Expand Up @@ -597,9 +584,9 @@ typedef struct materialui_handle
/* Font data */
struct
{
materialui_font_data_t title; /* ptr alignment */
materialui_font_data_t list; /* ptr alignment */
materialui_font_data_t hint; /* ptr alignment */
font_data_impl_t title; /* ptr alignment */
font_data_impl_t list; /* ptr alignment */
font_data_impl_t hint; /* ptr alignment */
} font_data;

void (*word_wrap)(
Expand Down Expand Up @@ -2193,30 +2180,6 @@ static const char *materialui_texture_path(unsigned id)
return NULL;
}

static void INLINE materialui_font_bind(materialui_font_data_t *font_data)
{
font_driver_bind_block(font_data->font, &font_data->raster_block);
font_data->raster_block.carr.coords.vertices = 0;
}

static void INLINE materialui_font_unbind(materialui_font_data_t *font_data)
{
font_driver_bind_block(font_data->font, NULL);
}

/* Flushing is slow - only do it if font
* has actually been used */
static void materialui_font_flush(
unsigned video_width, unsigned video_height,
materialui_font_data_t *font_data)
{
if (font_data->raster_block.carr.coords.vertices == 0)
return;
if (font_data->font && font_data->font->renderer && font_data->font->renderer->flush)
font_data->font->renderer->flush(video_width, video_height, font_data->font->renderer_data);
font_data->raster_block.carr.coords.vertices = 0;
}

/* ==============================
* Playlist icons START
* ============================== */
Expand Down Expand Up @@ -3560,7 +3523,7 @@ static bool (*materialui_render_process_entry)(

static void materialui_init_font(
gfx_display_t *p_disp,
materialui_font_data_t *font_data,
font_data_impl_t *font_data,
int font_size,
bool video_is_threaded,
const char *str_latin);
Expand Down Expand Up @@ -5119,7 +5082,7 @@ static void materialui_render_selected_entry_aux_playlist_desktop(
/* Status bar overlaps list entries
* > Must flush list font before attempting
* to draw it */
materialui_font_flush(video_width, video_height, &mui->font_data.list);
font_flush(video_width, video_height, &mui->font_data.list);

/* Background
* > Surface */
Expand Down Expand Up @@ -7070,9 +7033,9 @@ static void materialui_frame(void *data, video_frame_info_t *video_info)
video_st->data, video_width, video_height, true, false);

/* Clear text */
materialui_font_bind(&mui->font_data.title);
materialui_font_bind(&mui->font_data.list);
materialui_font_bind(&mui->font_data.hint);
font_bind(&mui->font_data.title);
font_bind(&mui->font_data.list);
font_bind(&mui->font_data.hint);

/* Update theme colours, if required */
if (mui->color_theme != materialui_color_theme)
Expand Down Expand Up @@ -7163,8 +7126,8 @@ static void materialui_frame(void *data, video_frame_info_t *video_info)

/* Flush first layer of text
* > Menu list only uses list and hint fonts */
materialui_font_flush(video_width, video_height, &mui->font_data.list);
materialui_font_flush(video_width, video_height, &mui->font_data.hint);
font_flush(video_width, video_height, &mui->font_data.list);
font_flush(video_width, video_height, &mui->font_data.hint);

/* Draw fullscreen thumbnails, if currently active
* > Must be done *after* we flush the first layer
Expand All @@ -7183,10 +7146,8 @@ static void materialui_frame(void *data, video_frame_info_t *video_info)

/* Flush second layer of text
* > Title + system bar only use title and hint fonts */
materialui_font_flush(video_width,
video_height, &mui->font_data.title);
materialui_font_flush(video_width,
video_height, &mui->font_data.hint);
font_flush(video_width, video_height, &mui->font_data.title);
font_flush(video_width, video_height, &mui->font_data.hint);

/* Handle onscreen keyboard */
if (menu_input_dialog_get_display_kb())
Expand Down Expand Up @@ -7238,7 +7199,7 @@ static void materialui_frame(void *data, video_frame_info_t *video_info)

/* Flush message box & osk text
* > Message box & osk only use list font */
materialui_font_flush(video_width, video_height, &mui->font_data.list);
font_flush(video_width, video_height, &mui->font_data.list);
}

/* Draw message box */
Expand Down Expand Up @@ -7267,7 +7228,7 @@ static void materialui_frame(void *data, video_frame_info_t *video_info)

/* Flush message box text
* > Message box only uses list font */
materialui_font_flush(video_width, video_height, &mui->font_data.list);
font_flush(video_width, video_height, &mui->font_data.list);
}

/* Draw mouse cursor */
Expand Down Expand Up @@ -7304,9 +7265,9 @@ static void materialui_frame(void *data, video_frame_info_t *video_info)
materialui_colors_reset_transition_alpha(mui);

/* Unbind fonts */
materialui_font_unbind(&mui->font_data.title);
materialui_font_unbind(&mui->font_data.list);
materialui_font_unbind(&mui->font_data.hint);
font_unbind(&mui->font_data.title);
font_unbind(&mui->font_data.list);
font_unbind(&mui->font_data.hint);

if (video_st->current_video && video_st->current_video->set_viewport)
video_st->current_video->set_viewport(
Expand Down Expand Up @@ -7886,7 +7847,7 @@ static void materialui_update_list_view(materialui_handle_t *mui, settings_t *se

static void materialui_init_font(
gfx_display_t *p_disp,
materialui_font_data_t *font_data,
font_data_impl_t *font_data,
int font_size,
bool video_is_threaded,
const char *str_latin
Expand Down
Loading

0 comments on commit bc16727

Please sign in to comment.