Merge pull request #745 from libretro/font-rewrite

Font rendering rewrite
This commit is contained in:
Twinaphex 2014-06-09 13:25:17 +02:00
commit cf4f756fe8
39 changed files with 632 additions and 997 deletions

View File

@ -19,6 +19,7 @@ OBJ = frontend/frontend.o \
message_queue.o \
rewind.o \
gfx/gfx_common.o \
gfx/fonts/bitmapfont.o \
input/input_common.o \
input/keyboard_line.o \
input/overlay.o \
@ -38,13 +39,13 @@ OBJ = frontend/frontend.o \
gfx/scaler/scaler_filter.o \
gfx/image/image_rpng.o \
gfx/fonts/fonts.o \
gfx/fonts/bitmapfont.o \
audio/resampler.o \
audio/dsp_filter.o \
audio/sinc.o \
audio/cc_resampler.o \
performance.o
JOYCONFIG_OBJ = tools/retroarch-joyconfig.o \
conf/config_file.o \
file_path.o \
@ -393,7 +394,7 @@ else
ifneq ($(findstring icc,$(CC)),)
CFLAGS += -std=c99 -D_GNU_SOURCE
else
CFLAGS += -std=gnu99
CFLAGS += -std=gnu99 -D_GNU_SOURCE
endif
endif
endif

View File

@ -19,7 +19,7 @@
#include <string.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#include "../config.h"
#endif
#ifndef HAVE_STRCASESTR

View File

@ -404,11 +404,8 @@ static const bool crop_overscan = true;
#if defined(HAVE_RMENU)
static const float font_size = 1.0f;
#else
static const float font_size = 48;
static const float font_size = 32;
#endif
// Attempt to scale the font size.
// The scale factor will be window_size / desktop_size.
static const bool font_scale = true;
// Offset for where messages will be placed on-screen. Values are in range [0.0, 1.0].
static const float message_pos_offset_x = 0.05;

View File

@ -342,6 +342,22 @@ typedef struct video_overlay_interface
} video_overlay_interface_t;
#endif
struct font_params
{
float x;
float y;
float scale;
float drop_mod; // Drop shadow color multiplier.
int drop_x, drop_y; // Drop shadow offset. If both are 0, no drop shadow will be rendered.
uint32_t color; // ABGR. Use the macros.
bool full_screen;
};
#define FONT_COLOR_RGBA(r, g, b, a) (((r) << 0) | ((g) << 8) | ((b) << 16) | ((a) << 24))
#define FONT_COLOR_GET_RED(col) (((col) >> 0) & 0xff)
#define FONT_COLOR_GET_GREEN(col) (((col) >> 8) & 0xff)
#define FONT_COLOR_GET_BLUE(col) (((col) >> 16) & 0xff)
#define FONT_COLOR_GET_ALPHA(col) (((col) >> 24) & 0xff)
// Optionally implemented interface to poke more deeply into video driver.
typedef struct video_poke_interface
{
@ -357,7 +373,7 @@ typedef struct video_poke_interface
void (*set_texture_frame)(void *data, const void *frame, bool rgb32, unsigned width, unsigned height, float alpha); // Update texture.
void (*set_texture_enable)(void *data, bool enable, bool full_screen); // Enable/disable rendering.
#endif
void (*set_osd_msg)(void *data, const char *msg, void *userdata);
void (*set_osd_msg)(void *data, const char *msg, const struct font_params *params);
void (*show_mouse)(void *data, bool state);
void (*grab_mouse_toggle)(void *data);

View File

@ -205,7 +205,6 @@ static void menu_common_entries_init(void *data, unsigned menu_type)
case RGUI_SETTINGS_FONT_OPTIONS:
file_list_clear(rgui->selection_buf);
file_list_push(rgui->selection_buf, "OSD Font Enable", RGUI_SETTINGS_FONT_ENABLE, 0);
file_list_push(rgui->selection_buf, "OSD Font Scale to Window", RGUI_SETTINGS_FONT_SCALE, 0);
file_list_push(rgui->selection_buf, "OSD Font Size", RGUI_SETTINGS_FONT_SIZE, 0);
break;
case RGUI_SETTINGS_CORE_OPTIONS:
@ -4946,12 +4945,6 @@ static int menu_common_setting_set(unsigned setting, unsigned action)
else if (action == RGUI_ACTION_START)
g_settings.video.font_enable = true;
break;
case RGUI_SETTINGS_FONT_SCALE:
if (action == RGUI_ACTION_OK || action == RGUI_ACTION_LEFT || action == RGUI_ACTION_RIGHT)
g_settings.video.font_scale = !g_settings.video.font_scale;
else if (action == RGUI_ACTION_START)
g_settings.video.font_scale = true;
break;
case RGUI_SETTINGS_FONT_SIZE:
if (action == RGUI_ACTION_LEFT)
g_settings.video.font_size -= 1.0f;
@ -5470,9 +5463,6 @@ static void menu_common_setting_set_label(char *type_str, size_t type_str_size,
case RGUI_SETTINGS_FONT_ENABLE:
snprintf(type_str, type_str_size, g_settings.video.font_enable ? "ON" : "OFF");
break;
case RGUI_SETTINGS_FONT_SCALE:
snprintf(type_str, type_str_size, g_settings.video.font_scale ? "ON" : "OFF");
break;
case RGUI_SETTINGS_FONT_SIZE:
snprintf(type_str, type_str_size, "%.1f", g_settings.video.font_size);
break;

View File

@ -61,7 +61,6 @@ typedef enum
RGUI_SETTINGS_VIDEO_OPTIONS_LAST,
RGUI_SETTINGS_FONT_OPTIONS,
RGUI_SETTINGS_FONT_ENABLE,
RGUI_SETTINGS_FONT_SCALE,
RGUI_SETTINGS_FONT_SIZE,
RGUI_SETTINGS_SLOWMOTION_RATIO,
RGUI_SETTINGS_FASTFORWARD_RATIO,

View File

@ -54,20 +54,7 @@ float global_alpha = 0;
// Font variables
void *font;
const gl_font_renderer_t *font_ctx;
const font_renderer_driver_t *font_driver;
GLuint font_tex;
GLint max_font_size;
int font_tex_w, font_tex_h;
uint32_t *font_tex_buf;
char font_last_msg[256];
int font_last_width, font_last_height;
struct font_output_list run_label;
struct font_output_list resume_label;
//GL-specific variables
GLfloat font_color[16];
GLfloat font_color_dark[16];
const gl_font_renderer_t *font_driver;
enum
{
@ -209,305 +196,26 @@ static void update_tweens(float dt)
numtweens = 0;
}
struct font_rect
static void lakka_draw_text(const char *str, float x, float y, float scale, float alpha)
{
int x, y;
int width, height;
int pot_width, pot_height;
};
/* font rendering */
#if 0
static void deinit_font(void *data)
{
(void)data;
if (font)
{
if (font_driver)
font_driver->free(font);
font_driver = NULL;
glDeleteTextures(1, &font_tex);
if (font_tex_buf)
free(font_tex_buf);
}
}
#endif
static bool init_font(void *data, const char *font_path, float font_size, unsigned win_width, unsigned win_height)
{
size_t i, j;
gl_t *gl = (gl_t*)data;
(void)win_width;
(void)win_height;
(void)font_size;
if (!g_settings.video.font_enable)
return false;
if (font_renderer_create_default(&font_driver, &font))
{
glGenTextures(1, &font_tex);
glBindTexture(GL_TEXTURE_2D, font_tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glBindTexture(GL_TEXTURE_2D, textures[TEXTURE_MAIN].id);
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max_font_size);
}
else
{
RARCH_WARN("Couldn't init font renderer.\n");
return false;
}
for (i = 0; i < 4; i++)
{
font_color[4 * i + 0] = g_settings.video.msg_color_r;
font_color[4 * i + 1] = g_settings.video.msg_color_g;
font_color[4 * i + 2] = g_settings.video.msg_color_b;
font_color[4 * i + 3] = 1.0;
}
for (i = 0; i < 4; i++)
{
for (j = 0; j < 3; j++)
font_color_dark[4 * i + j] = 0.3 * font_color[4 * i + j];
font_color_dark[4 * i + 3] = 1.0;
}
return true;
}
static void calculate_msg_geometry(const struct font_output *head, struct font_rect *rect)
{
int x_min = head->off_x;
int x_max = head->off_x + head->width+10;
int y_min = head->off_y;
int y_max = head->off_y + head->height+1;
while ((head = head->next))
{
int left = head->off_x;
int right = head->off_x + head->width;
int bottom = head->off_y;
int top = head->off_y + head->height;
if (left < x_min)
x_min = left;
if (right > x_max)
x_max = right;
if (bottom < y_min)
y_min = bottom;
if (top > y_max)
y_max = top;
}
rect->x = x_min;
rect->y = y_min;
rect->width = x_max - x_min;
rect->height = y_max - y_min;
}
static void adjust_power_of_two(gl_t *gl, struct font_rect *geom)
{
// Some systems really hate NPOT textures.
geom->pot_width = next_pow2(geom->width);
geom->pot_height = next_pow2(geom->height);
if (geom->pot_width > max_font_size)
geom->pot_width = max_font_size;
if (geom->pot_height > max_font_size)
geom->pot_height = max_font_size;
if ((geom->pot_width > font_tex_w) || (geom->pot_height > font_tex_h))
{
font_tex_buf = (uint32_t*)realloc(font_tex_buf,
geom->pot_width * geom->pot_height * sizeof(uint32_t));
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, geom->pot_width, geom->pot_height,
0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
font_tex_w = geom->pot_width;
font_tex_h = geom->pot_height;
}
}
static void copy_glyph(const struct font_output *head, const struct font_rect *geom, uint32_t *buffer, unsigned width, unsigned height)
{
int h, w, x, y, font_width, font_height;
uint8_t *src;
uint32_t *dst;
// head has top-left oriented coords.
x = head->off_x - geom->x;
y = head->off_y - geom->y;
y = height - head->height - y - 1;
src = (uint8_t*)head->output;
font_width = head->width + ((x < 0) ? x : 0);
font_height = head->height + ((y < 0) ? y : 0);
if (x < 0)
{
src += -x;
x = 0;
}
if (y < 0)
{
src += -y * head->pitch;
y = 0;
}
if (x + font_width > (int)width)
font_width = width - x;
if (y + font_height > (int)height)
font_height = height - y;
dst = (uint32_t*)(buffer + y * width + x);
for (h = 0; h < font_height; h++, dst += width, src += head->pitch)
{
uint8_t *d = (uint8_t*)dst;
for (w = 0; w < font_width; w++)
{
*d++ = 0xff;
*d++ = 0xff;
*d++ = 0xff;
*d++ = src[w];
}
}
}
static void blit_fonts(gl_t *gl, const struct font_output *head, const struct font_rect *geom)
{
memset(font_tex_buf, 0, font_tex_w * font_tex_h * sizeof(uint32_t));
while (head)
{
copy_glyph(head, geom, font_tex_buf, font_tex_w, font_tex_h);
head = head->next;
}
glPixelStorei(GL_UNPACK_ALIGNMENT, 8);
glTexSubImage2D(GL_TEXTURE_2D,
0, 0, 0, font_tex_w, font_tex_h,
GL_RGBA, GL_UNSIGNED_BYTE, font_tex_buf);
}
static void calculate_font_coords(gl_t *gl,
GLfloat font_vertex[8], GLfloat font_vertex_dark[8], GLfloat font_tex_coords[8], GLfloat scale, GLfloat pos_x, GLfloat pos_y)
{
unsigned i;
GLfloat scale_factor, lx, hx, ly, hy, shift_x, shift_y;
scale_factor = scale;
lx = pos_x;
hx = (GLfloat)font_last_width * scale_factor / gl->vp.width + lx;
ly = pos_y;
hy = (GLfloat)font_last_height * scale_factor / gl->vp.height + ly;
font_vertex[0] = lx;
font_vertex[2] = hx;
font_vertex[4] = lx;
font_vertex[6] = hx;
font_vertex[1] = hy;
font_vertex[3] = hy;
font_vertex[5] = ly;
font_vertex[7] = ly;
shift_x = 2.0f / gl->vp.width;
shift_y = 2.0f / gl->vp.height;
for (i = 0; i < 4; i++)
{
font_vertex_dark[2 * i + 0] = font_vertex[2 * i + 0] - shift_x;
font_vertex_dark[2 * i + 1] = font_vertex[2 * i + 1] - shift_y;
}
lx = 0.0f;
hx = (GLfloat)font_last_width / font_tex_w;
ly = 1.0f - (GLfloat)font_last_height / font_tex_h;
hy = 1.0f;
font_tex_coords[0] = lx;
font_tex_coords[2] = hx;
font_tex_coords[4] = lx;
font_tex_coords[6] = hx;
font_tex_coords[1] = ly;
font_tex_coords[3] = ly;
font_tex_coords[5] = hy;
font_tex_coords[7] = hy;
}
static void lakka_draw_text(struct font_output_list *out, float x, float y, float scale, float alpha)
{
int i;
struct font_output *head;
struct font_rect geom;
struct gl_ortho ortho = {0, 1, 0, 1, -1, 1};
gl_t *gl = (gl_t*)driver.video_data;
if (!gl)
return;
gl_set_viewport(gl, gl->win_width, gl->win_height, false, false);
struct font_params params = {0};
params.x = x / gl->vp.width;
params.y = 1.0f - y / gl->vp.height;
if (alpha > global_alpha)
alpha = global_alpha;
if (!font || !gl || !out)
return;
params.scale = scale;
params.color = FONT_COLOR_RGBA(255, 255, 255, (uint8_t)(255 * alpha));
for (i = 0; i < 4; i++)
{
font_color[4 * i + 0] = 1.0;
font_color[4 * i + 1] = 1.0;
font_color[4 * i + 2] = 1.0;
font_color[4 * i + 3] = alpha;
}
if (gl->shader && gl->shader->use)
gl->shader->use(gl, GL_SHADER_STOCK_BLEND);
gl_set_viewport(gl, gl->win_width, gl->win_height, true, false);
glEnable(GL_BLEND);
GLfloat font_vertex[8];
GLfloat font_vertex_dark[8];
GLfloat font_tex_coords[8];
glBindTexture(GL_TEXTURE_2D, font_tex);
gl->coords.tex_coord = font_tex_coords;
head = (struct font_output*)out->head;
calculate_msg_geometry(head, &geom);
adjust_power_of_two(gl, &geom);
blit_fonts(gl, head, &geom);
font_last_width = geom.width;
font_last_height = geom.height;
calculate_font_coords(gl, font_vertex, font_vertex_dark, font_tex_coords,
scale, x / gl->win_width, (gl->win_height - y) / gl->win_height);
gl->coords.vertex = font_vertex;
gl->coords.color = font_color;
gl_shader_set_coords(gl, &gl->coords, &gl->mvp);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
// Post - Go back to old rendering path.
gl->coords.vertex = gl->vertex_ptr;
gl->coords.tex_coord = gl->tex_coords;
gl->coords.color = gl->white_color_ptr;
glBindTexture(GL_TEXTURE_2D, textures[TEXTURE_MAIN].id);
glDisable(GL_BLEND);
gl_set_projection(gl, &ortho, true);
if (font_driver)
font_driver->render_msg(font, str, &params);
}
void lakka_draw_background(void)
@ -518,8 +226,8 @@ void lakka_draw_background(void)
0.1, 0.74, 0.61, global_alpha,
0.1, 0.74, 0.61, global_alpha,
};
gl_t *gl = (gl_t*)driver.video_data;
gl_t *gl = (gl_t*)driver.video_data;
if (!gl)
return;
@ -531,6 +239,8 @@ void lakka_draw_background(void)
if (gl->shader && gl->shader->use)
gl->shader->use(gl, GL_SHADER_STOCK_BLEND);
gl->coords.vertices = 4;
gl_shader_set_coords(gl, &gl->coords, &gl->mvp_no_rot);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
@ -584,6 +294,7 @@ void lakka_draw_icon(GLuint texture, float x, float y, float alpha, float rotati
matrix_scale(&mscal, scale, scale, 1);
matrix_multiply(&mymat, &mscal, &mymat);
gl->coords.vertices = 4;
gl_shader_set_coords(gl, &gl->coords, &mymat);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
@ -619,7 +330,7 @@ static void lakka_draw_subitems(int i, int j)
subitem->alpha,
0,
subitem->zoom);
lakka_draw_text(&resume_label,
lakka_draw_text("Resume",
156 + HSPACING*(i+2) + all_categories_x + dim/2.0,
300 + subitem->y + 15,
1,
@ -637,7 +348,7 @@ static void lakka_draw_subitems(int i, int j)
subitem->alpha,
0,
subitem->zoom);
lakka_draw_text(&subitem->out,
lakka_draw_text(subitem->name,
156 + HSPACING * (i+2) + all_categories_x + dim/2.0,
300 + subitem->y + 15,
1,
@ -665,7 +376,7 @@ static void lakka_draw_items(int i)
j > active_category->active_item - 4 &&
j < active_category->active_item + 10) // performance improvement
{
lakka_draw_icon(category->item_icon,
lakka_draw_icon(category->item_icon,
156 + HSPACING*(i+1) + all_categories_x - dim/2.0,
300 + item->y + dim/2.0,
item->alpha,
@ -673,7 +384,7 @@ static void lakka_draw_items(int i)
item->zoom);
if (depth == 0)
lakka_draw_text(&item->out,
lakka_draw_text(item->name,
156 + HSPACING * (i+1) + all_categories_x + dim/2.0,
300 + item->y + 15,
1,
@ -732,12 +443,12 @@ static void lakka_frame(void)
if (depth == 0)
{
if (active_category)
lakka_draw_text(&active_category->out, 15.0, 40.0, 1, 1.0);
lakka_draw_text(active_category->name, 15.0, 40.0, 1, 1.0);
}
else
{
if (active_item)
lakka_draw_text(&active_item->out, 15.0, 40.0, 1, 1.0);
lakka_draw_text(active_item->name, 15.0, 40.0, 1, 1.0);
lakka_draw_icon(textures[TEXTURE_ARROW].id,
156 + HSPACING*(menu_active_category+1) + all_categories_x + 150 +-dim/2.0,
@ -937,6 +648,12 @@ static void lakka_context_destroy(void *data)
}
}
if (font_driver)
{
font_driver->free(font);
font_driver = NULL;
}
//if (numtweens)
// free(tweens);
}
@ -1034,29 +751,21 @@ void lakka_settings_context_reset(void)
category->icon = textures[TEXTURE_SETTINGS].id;
category->item_icon = textures[TEXTURE_SETTING].id;
if (font_driver)
font_driver->render_msg(font, category->name, &category->out);
// General options item
item = (menu_item_t*)&category->items[0];
if (font_driver)
font_driver->render_msg(font, item->name, &item->out);
// General options subitems
for (k = 0; k < 2; k++)
{
menu_subitem_t *subitem = (menu_subitem_t*)&item->subitems[k];
subitem->icon = textures[TEXTURE_SUBSETTING].id;
if (font_driver)
font_driver->render_msg(font, subitem->name, &subitem->out);
}
// Quit item
item = (menu_item_t*)&category->items[1];
if (font_driver)
font_driver->render_msg(font, item->name, &item->out);
}
@ -1070,6 +779,9 @@ static void lakka_context_reset(void *data)
if (!rgui)
return;
gl_font_init_first(&font_driver, &font, gl,
*g_settings.video.font_path ? g_settings.video.font_path : NULL, g_settings.video.font_size);
fill_pathname_join(dirpath, g_settings.assets_directory, "lakka", sizeof(dirpath));
fill_pathname_slash(dirpath, sizeof(dirpath));
@ -1087,12 +799,6 @@ static void lakka_context_reset(void *data)
for (k = 0; k < TEXTURE_LAST; k++)
textures[k].id = png_texture_load(textures[k].path, &dim, &dim);
if (font_driver)
{
font_driver->render_msg(font, "Run", &run_label);
font_driver->render_msg(font, "Resume", &resume_label);
}
lakka_settings_context_reset();
for (i = 1; i < num_categories; i++)
{
@ -1131,16 +837,10 @@ static void lakka_context_reset(void *data)
category->icon = png_texture_load(texturepath, &dim, &dim);
category->item_icon = png_texture_load(content_texturepath, &dim, &dim);
if (font_driver)
font_driver->render_msg(font, category->name, &category->out);
for (j = 0; j < category->num_items; j++)
{
menu_item_t *item = (menu_item_t*)&category->items[j];
if (font_driver)
font_driver->render_msg(font, item->name, &item->out);
for (k = 0; k < item->num_subitems; k++)
{
menu_subitem_t *subitem = (menu_subitem_t*)&item->subitems[k];
@ -1163,9 +863,6 @@ static void lakka_context_reset(void *data)
subitem->icon = textures[TEXTURE_RELOAD].id;
break;
}
if (font_driver)
font_driver->render_msg(font, subitem->name, &subitem->out);
}
}
}
@ -1231,9 +928,6 @@ static void lakka_init_items(int i, menu_category_t *category, core_info_t *info
subitem->alpha = 0;
subitem->zoom = k ? I_PASSIVE_ZOOM : I_ACTIVE_ZOOM;
subitem->y = k ? VSPACING * (3+k) : VSPACING * 2.4;
if (font_driver)
font_driver->render_msg(font, subitem->name, &subitem->out);
}
}
}
@ -1289,8 +983,6 @@ static void *lakka_init(void)
if (!rgui || !gl)
return NULL;
init_font(gl, g_settings.video.font_path, g_settings.video.font_size, gl->win_width, gl->win_height);
lakka_init_core_info(rgui);
categories = (menu_category_t*)calloc(num_categories, sizeof(menu_category_t));

View File

@ -42,7 +42,6 @@ typedef struct
float alpha;
float zoom;
float y;
struct font_output_list out;
} menu_subitem_t;
typedef struct
@ -55,7 +54,6 @@ typedef struct
int active_subitem;
int num_subitems;
menu_subitem_t *subitems;
struct font_output_list out;
} menu_item_t;
typedef struct
@ -69,7 +67,6 @@ typedef struct
int active_item;
int num_items;
menu_item_t *items;
struct font_output_list out;
} menu_category_t;
typedef float (*easingFunc)(float, float, float, float);

View File

@ -72,7 +72,7 @@ static void rmenu_render_background(void)
static void rmenu_render_messagebox(const char *message)
{
font_params_t font_parms;
struct font_params font_parms;
size_t i, j;
@ -117,7 +117,7 @@ static void rmenu_render_messagebox(const char *message)
static void rmenu_render(void)
{
size_t begin, end;
font_params_t font_parms;
struct font_params font_parms;
rgui_handle_t *rgui = (rgui_handle_t*)driver.menu;
if (!rgui)

View File

@ -245,7 +245,7 @@ static void rmenu_xui_free(void *data)
static void xui_render_message(const char *msg)
{
font_params_t font_parms;
struct font_params font_parms;
size_t i, j;
struct string_list *list = string_split(msg, "\n");

View File

@ -185,7 +185,6 @@ struct settings
char font_path[PATH_MAX];
float font_size;
bool font_enable;
bool font_scale;
float msg_pos_x;
float msg_pos_y;
float msg_color_r;

View File

@ -280,7 +280,7 @@ bool d3d_init_multipass(void *data)
}
#endif
void d3d_set_font_rect(void *data, font_params_t *params)
void d3d_set_font_rect(void *data, const struct font_params *params)
{
#ifndef _XBOX
d3d_video_t *d3d = (d3d_video_t*)data;
@ -745,7 +745,7 @@ static bool d3d_frame(void *data, const void *frame,
if (d3d->font_ctx && d3d->font_ctx->render_msg && msg)
{
font_params_t font_parms = {0};
struct font_params font_parms = {0};
#ifdef _XBOX
#if defined(_XBOX1)
float msg_width = 60;
@ -1133,9 +1133,8 @@ static void d3d_apply_state_changes(void *data)
d3d->should_resize = true;
}
static void d3d_set_osd_msg(void *data, const char *msg, void *userdata)
static void d3d_set_osd_msg(void *data, const char *msg, const struct font_params *params)
{
font_params_t *params = (font_params_t*)userdata;
d3d_video_t *d3d = (d3d_video_t*)data;
if (params)

View File

@ -110,7 +110,7 @@ typedef struct
LPDIRECT3DVERTEXBUFFER vert_buf;
} overlay_t;
void d3d_set_font_rect(void *data, font_params_t *params);
void d3d_set_font_rect(void *data, const struct font_params *params);
bool d3d_process_shader(void *data);
void d3d_update_title(void *data);
void d3d_recompute_pass_sizes(void *data);

View File

@ -18,6 +18,7 @@
#define FONT_WIDTH 5
#define FONT_HEIGHT 10
#define FONT_HEIGHT_BASELINE 8
#define FONT_WIDTH_STRIDE (FONT_WIDTH + 1)
#define FONT_HEIGHT_STRIDE (FONT_HEIGHT + 1)

View File

@ -21,37 +21,60 @@
#include "../../msvc/msvc_compat.h"
#include "../../boolean.h"
struct font_renderer
#define ATLAS_COLS 16
#define ATLAS_ROWS 16
#define ATLAS_SIZE (ATLAS_COLS * ATLAS_ROWS)
typedef struct bm_renderer
{
unsigned scale_factor;
uint8_t *bitmap_chars[256];
uint8_t *bitmap_alloc;
};
struct font_glyph glyphs[ATLAS_SIZE];
struct font_atlas atlas;
} bm_renderer_t;
static void char_to_texture(font_renderer_t *handle, uint8_t letter)
static const struct font_atlas *font_renderer_get_atlas(void *data)
{
bm_renderer_t *handle = (bm_renderer_t*)data;
return &handle->atlas;
}
static const struct font_glyph *font_renderer_get_glyph(void *data, uint32_t code)
{
bm_renderer_t *handle = (bm_renderer_t*)data;
return code < ATLAS_SIZE ? &handle->glyphs[code] : NULL;
}
static void char_to_texture(bm_renderer_t *handle, uint8_t letter, unsigned atlas_x, unsigned atlas_y)
{
unsigned y, x, xo, yo;
handle->bitmap_chars[letter] = &handle->bitmap_alloc[letter * FONT_WIDTH * FONT_HEIGHT * handle->scale_factor * handle->scale_factor];
uint8_t *target = handle->atlas.buffer + atlas_x + atlas_y * handle->atlas.width;
for (y = 0; y < FONT_HEIGHT; y++)
{
for (x = 0; x < FONT_WIDTH; x++)
{
uint8_t rem = 1 << ((x + y * FONT_WIDTH) & 7);
unsigned offset = (x + y * FONT_WIDTH) >> 3;
uint8_t col = (bitmap_bin[FONT_OFFSET(letter) + offset] & rem) ? 0xFF : 0;
unsigned font_pixel = x + y * FONT_WIDTH;
uint8_t rem = 1 << (font_pixel & 7);
unsigned offset = font_pixel >> 3;
uint8_t col = (bitmap_bin[FONT_OFFSET(letter) + offset] & rem) ? 0xff : 0;
for (xo = 0; xo < handle->scale_factor; xo++)
for (yo = 0; yo < handle->scale_factor; yo++)
handle->bitmap_chars[letter][x * handle->scale_factor + xo + (y * handle->scale_factor + yo) * FONT_WIDTH * handle->scale_factor] = col;
uint8_t *dst = target;
dst += x * handle->scale_factor;
dst += y * handle->scale_factor * handle->atlas.width;
for (yo = 0; yo < handle->scale_factor; yo++)
for (xo = 0; xo < handle->scale_factor; xo++)
dst[xo + yo * handle->atlas.width] = col;
}
}
}
static void *font_renderer_init(const char *font_path, float font_size)
{
(void)font_path;
unsigned i;
font_renderer_t *handle = (font_renderer_t*)calloc(1, sizeof(*handle));
bm_renderer_t *handle = (bm_renderer_t*)calloc(1, sizeof(*handle));
if (!handle)
return NULL;
@ -59,77 +82,35 @@ static void *font_renderer_init(const char *font_path, float font_size)
if (!handle->scale_factor)
handle->scale_factor = 1;
handle->bitmap_alloc = (uint8_t*)malloc(FONT_WIDTH * FONT_HEIGHT * handle->scale_factor * handle->scale_factor * 256);
handle->atlas.width = FONT_WIDTH * handle->scale_factor * ATLAS_COLS;
handle->atlas.height = FONT_HEIGHT * handle->scale_factor * ATLAS_ROWS;
handle->atlas.buffer = (uint8_t*)calloc(handle->atlas.width * handle->atlas.height, 1);
if (!handle->bitmap_alloc)
for (i = 0; i < ATLAS_SIZE; i++)
{
free(handle);
return NULL;
}
unsigned x = (i % ATLAS_COLS) * handle->scale_factor * FONT_WIDTH;
unsigned y = (i / ATLAS_COLS) * handle->scale_factor * FONT_HEIGHT;
char_to_texture(handle, i, x, y);
for (i = 0; i < 256; i++)
char_to_texture(handle, i);
handle->glyphs[i].width = FONT_WIDTH * handle->scale_factor;
handle->glyphs[i].height = FONT_HEIGHT * handle->scale_factor;
handle->glyphs[i].atlas_offset_x = x;
handle->glyphs[i].atlas_offset_y = y;
handle->glyphs[i].draw_offset_x = 0;
handle->glyphs[i].draw_offset_y = -FONT_HEIGHT_BASELINE * (int)handle->scale_factor;
handle->glyphs[i].advance_x = (FONT_WIDTH + 1) * handle->scale_factor;
handle->glyphs[i].advance_y = 0;
}
return handle;
}
static void font_renderer_msg(void *data, const char *msg, struct font_output_list *output)
{
size_t i;
font_renderer_t *handle = (font_renderer_t*)data;
output->head = NULL;
struct font_output *cur = NULL;
size_t len = strlen(msg);
int off_x = 0;
for (i = 0; i < len; i++)
{
struct font_output *tmp = (struct font_output*)calloc(1, sizeof(*tmp));
if (!tmp)
break;
tmp->output = handle->bitmap_chars[(uint8_t)msg[i]];
tmp->width = FONT_WIDTH * handle->scale_factor;
tmp->height = FONT_HEIGHT * handle->scale_factor;
tmp->pitch = tmp->width;
tmp->advance_x = tmp->width;
tmp->advance_y = tmp->height;
tmp->char_off_x = 0;
tmp->char_off_y = tmp->height;
tmp->off_x = off_x;
tmp->off_y = 0;
tmp->next = NULL;
if (i == 0)
output->head = tmp;
else
cur->next = tmp;
cur = tmp;
off_x += FONT_WIDTH_STRIDE * handle->scale_factor;
}
}
static void font_renderer_free_output(void *data, struct font_output_list *output)
{
(void)data;
struct font_output *itr = output->head;
struct font_output *tmp = NULL;
while (itr != NULL)
{
tmp = itr;
itr = itr->next;
free(tmp);
}
output->head = NULL;
}
static void font_renderer_free(void *data)
{
font_renderer_t *handle = (font_renderer_t*)data;
free(handle->bitmap_alloc);
bm_renderer_t *handle = (bm_renderer_t*)data;
if (!handle)
return;
free(handle->atlas.buffer);
free(handle);
}
@ -140,9 +121,10 @@ static const char *font_renderer_get_default_font(void)
const font_renderer_driver_t bitmap_font_renderer = {
font_renderer_init,
font_renderer_msg,
font_renderer_free_output,
font_renderer_get_atlas,
font_renderer_get_glyph,
font_renderer_free,
font_renderer_get_default_font,
"bitmap",
};

View File

@ -18,6 +18,7 @@
#include <stdint.h>
#include "../../boolean.h"
#include "../../driver.h"
#ifdef __cplusplus
extern "C" {
@ -27,7 +28,7 @@ typedef struct d3d_font_renderer
{
bool (*init)(void *data, const char *font_path, unsigned font_size);
void (*deinit)(void *data);
void (*render_msg)(void *data, const char *msg, void *parms);
void (*render_msg)(void *data, const char *msg, const struct font_params *params);
const char *ident;
} d3d_font_renderer_t;

View File

@ -49,10 +49,9 @@ static void d3dfonts_w32_deinit_font(void *data)
d3d->font = NULL;
}
static void d3dfonts_w32_render_msg(void *data, const char *msg, void *userdata)
static void d3dfonts_w32_render_msg(void *data, const char *msg, const struct font_params *params)
{
d3d_video_t *d3d = (d3d_video_t*)data;
font_params_t *params = (font_params_t*)userdata;
if (msg && SUCCEEDED(d3d->dev->BeginScene()))
{

View File

@ -30,19 +30,19 @@ static const font_renderer_driver_t *font_backends[] = {
NULL
};
bool font_renderer_create_default(const font_renderer_driver_t **driver, void **handle)
bool font_renderer_create_default(const font_renderer_driver_t **driver, void **handle,
const char *font_path, unsigned font_size)
{
unsigned i;
for (i = 0; font_backends[i]; i++)
{
const char *font_path = *g_settings.video.font_path ? g_settings.video.font_path : NULL;
if (!font_path)
font_path = font_backends[i]->get_default_font();
if (!font_path)
const char *path = font_path;
if (!path)
path = font_backends[i]->get_default_font();
if (!path)
continue;
*handle = font_backends[i]->init(font_path, g_settings.video.font_size);
*handle = font_backends[i]->init(path, font_size);
if (*handle)
{
RARCH_LOG("Using font rendering backend: %s.\n", font_backends[i]->ident);

View File

@ -20,30 +20,43 @@
#include <stdint.h>
#include "../../boolean.h"
typedef struct font_renderer font_renderer_t;
// All coordinates and offsets are top-left oriented.
//
// This is a texture-atlas approach which allows text to be drawn in a single draw call.
// It is up to the code using this interface to actually generate proper vertex buffers and upload the atlas texture to GPU.
struct font_output
struct font_glyph
{
uint8_t *output; // 8-bit alpha.
unsigned width, height, pitch;
unsigned color;
unsigned scaling_factor;
int off_x, off_y;
int advance_x, advance_y, char_off_x, char_off_y; // for advanced font rendering
struct font_output *next; // linked list.
unsigned width;
unsigned height;
// Texel coordiate offset for top-left pixel of this glyph.
unsigned atlas_offset_x;
unsigned atlas_offset_y;
// When drawing this glyph, apply an offset to current X/Y draw coordinate.
int draw_offset_x;
int draw_offset_y;
// Advance X/Y draw coordinates after drawing this glyph.
int advance_x;
int advance_y;
};
struct font_output_list
struct font_atlas
{
struct font_output *head;
uint8_t *buffer; // Alpha channel.
unsigned width;
unsigned height;
};
typedef struct font_renderer_driver
{
void *(*init)(const char *font_path, float font_size);
void (*render_msg)(void *data, const char *msg, struct font_output_list *output);
void (*free_output)(void *data, struct font_output_list *list);
const struct font_atlas *(*get_atlas)(void *data);
const struct font_glyph *(*get_glyph)(void *data, uint32_t code); // Returns NULL if no glyph for this code is found.
void (*free)(void *data);
const char *(*get_default_font)(void);
const char *ident;
} font_renderer_driver_t;
@ -51,7 +64,8 @@ typedef struct font_renderer_driver
extern const font_renderer_driver_t ft_font_renderer;
extern const font_renderer_driver_t bitmap_font_renderer;
bool font_renderer_create_default(const font_renderer_driver_t **driver, void **handle);
// font_path can be NULL for default font.
bool font_renderer_create_default(const font_renderer_driver_t **driver, void **handle, const char *font_path, unsigned font_size);
#endif

View File

@ -22,18 +22,39 @@
#include <ft2build.h>
#include FT_FREETYPE_H
struct font_renderer
#define ATLAS_ROWS 8
#define ATLAS_COLS 16
#define ATLAS_SIZE (ATLAS_ROWS * ATLAS_COLS)
typedef struct ft_renderer
{
FT_Library lib;
FT_Face face;
};
struct font_atlas atlas;
struct font_glyph glyphs[ATLAS_SIZE];
} ft_renderer_t;
static const struct font_atlas *ft_renderer_get_atlas(void *data)
{
ft_renderer_t *handle = (ft_renderer_t*)data;
return &handle->atlas;
}
static const struct font_glyph *ft_renderer_get_glyph(void *data, uint32_t code)
{
ft_renderer_t *handle = (ft_renderer_t*)data;
return code < ATLAS_SIZE ? &handle->glyphs[code] : NULL;
}
static void ft_renderer_free(void *data)
{
font_renderer_t *handle = (font_renderer_t*)data;
ft_renderer_t *handle = (ft_renderer_t*)data;
if (!handle)
return;
free(handle->atlas.buffer);
if (handle->face)
FT_Done_Face(handle->face);
if (handle->lib)
@ -41,11 +62,92 @@ static void ft_renderer_free(void *data)
free(handle);
}
static bool ft_renderer_create_atlas(ft_renderer_t *handle)
{
unsigned i;
bool ret = true;
uint8_t *buffer[ATLAS_SIZE] = {NULL};
unsigned pitches[ATLAS_SIZE] = {0};
unsigned max_width = 0;
unsigned max_height = 0;
for (i = 0; i < ATLAS_SIZE; i++)
{
struct font_glyph *glyph = &handle->glyphs[i];
if (FT_Load_Char(handle->face, i, FT_LOAD_RENDER))
{
ret = false;
goto end;
}
FT_Render_Glyph(handle->face->glyph, FT_RENDER_MODE_NORMAL);
FT_GlyphSlot slot = handle->face->glyph;
// Some glyphs can be blank.
buffer[i] = (uint8_t*)calloc(slot->bitmap.rows * slot->bitmap.pitch, 1);
glyph->width = slot->bitmap.width;
glyph->height = slot->bitmap.rows;
pitches[i] = slot->bitmap.pitch;
glyph->advance_x = slot->advance.x >> 6;
glyph->advance_y = slot->advance.y >> 6;
glyph->draw_offset_x = slot->bitmap_left;
glyph->draw_offset_y = -slot->bitmap_top;
if (buffer[i])
memcpy(buffer[i], slot->bitmap.buffer, slot->bitmap.rows * pitches[i]);
max_width = max(max_width, (unsigned)slot->bitmap.width);
max_height = max(max_height, (unsigned)slot->bitmap.rows);
}
handle->atlas.width = max_width * ATLAS_COLS;
handle->atlas.height = max_height * ATLAS_ROWS;
handle->atlas.buffer = (uint8_t*)calloc(handle->atlas.width * handle->atlas.height, 1);
if (!handle->atlas.buffer)
{
ret = false;
goto end;
}
// Blit our texture atlas.
for (i = 0; i < ATLAS_SIZE; i++)
{
unsigned r, c;
unsigned offset_x = (i % ATLAS_COLS) * max_width;
unsigned offset_y = (i / ATLAS_COLS) * max_height;
handle->glyphs[i].atlas_offset_x = offset_x;
handle->glyphs[i].atlas_offset_y = offset_y;
uint8_t *dst = handle->atlas.buffer;
dst += offset_x + offset_y * handle->atlas.width;
if (buffer[i])
{
const uint8_t *src = buffer[i];
for (r = 0; r < handle->glyphs[i].height; r++, dst += handle->atlas.width, src += pitches[i])
for (c = 0; c < handle->glyphs[i].width; c++)
dst[c] = src[c];
}
}
end:
for (i = 0; i < ATLAS_SIZE; i++)
free(buffer[i]);
return ret;
}
static void *ft_renderer_init(const char *font_path, float font_size)
{
(void)font_size;
FT_Error err;
font_renderer_t *handle = (font_renderer_t*)calloc(1, sizeof(*handle));
ft_renderer_t *handle = (ft_renderer_t*)calloc(1, sizeof(*handle));
if (!handle)
goto error;
@ -61,6 +163,9 @@ static void *ft_renderer_init(const char *font_path, float font_size)
if (err)
goto error;
if (!ft_renderer_create_atlas(handle))
goto error;
return handle;
error:
@ -68,76 +173,6 @@ error:
return NULL;
}
static void ft_renderer_msg(void *data, const char *msg, struct font_output_list *output)
{
size_t i;
font_renderer_t *handle = (font_renderer_t*)data;
output->head = NULL;
FT_GlyphSlot slot = handle->face->glyph;
struct font_output *cur = NULL;
size_t len = strlen(msg);
int off_x = 0, off_y = 0;
for (i = 0; i < len; i++)
{
FT_Error err = FT_Load_Char(handle->face, msg[i], FT_LOAD_RENDER);
if (!err)
{
struct font_output *tmp = (struct font_output*)calloc(1, sizeof(*tmp));
if (!tmp)
break;
tmp->output = (uint8_t*)malloc(slot->bitmap.pitch * slot->bitmap.rows);
if (!tmp->output)
{
free(tmp);
break;
}
memcpy(tmp->output, slot->bitmap.buffer, slot->bitmap.pitch * slot->bitmap.rows);
tmp->width = slot->bitmap.width;
tmp->height = slot->bitmap.rows;
tmp->pitch = slot->bitmap.pitch;
tmp->advance_x = slot->advance.x >> 6;
tmp->advance_y = slot->advance.y >> 6;
tmp->char_off_x = slot->bitmap_left;
tmp->char_off_y = slot->bitmap_top - slot->bitmap.rows;
tmp->off_x = off_x + tmp->char_off_x;
tmp->off_y = off_y + tmp->char_off_y;
tmp->next = NULL;
if (i == 0)
output->head = tmp;
else
cur->next = tmp;
cur = tmp;
}
off_x += slot->advance.x >> 6;
off_y += slot->advance.y >> 6;
}
}
static void ft_renderer_free_output(void *data, struct font_output_list *output)
{
(void)data;
struct font_output *itr = output->head;
struct font_output *tmp = NULL;
while (itr != NULL)
{
free(itr->output);
tmp = itr;
itr = itr->next;
free(tmp);
}
output->head = NULL;
}
// Not the cleanest way to do things for sure, but should hopefully work ... :)
static const char *font_paths[] = {
@ -172,8 +207,8 @@ static const char *ft_renderer_get_default_font(void)
const font_renderer_driver_t ft_font_renderer = {
ft_renderer_init,
ft_renderer_msg,
ft_renderer_free_output,
ft_renderer_get_atlas,
ft_renderer_get_glyph,
ft_renderer_free,
ft_renderer_get_default_font,
"freetype",

View File

@ -23,18 +23,24 @@ static const gl_font_renderer_t *gl_font_backends[] = {
#else
&gl_raster_font,
#endif
NULL,
};
const gl_font_renderer_t *gl_font_init_first(void *data, const char *font_path, float font_size,
unsigned win_width, unsigned win_height)
bool gl_font_init_first(const gl_font_renderer_t **font_driver, void **font_handle,
void *gl_data, const char *font_path, float font_size)
{
size_t i;
for (i = 0; i < ARRAY_SIZE(gl_font_backends); i++)
unsigned i;
for (i = 0; gl_font_backends[i]; i++)
{
if (gl_font_backends[i]->init(data, font_path, font_size, win_width, win_height))
return gl_font_backends[i];
void *data = gl_font_backends[i]->init(gl_data, font_path, font_size);
if (data)
{
*font_driver = gl_font_backends[i];
*font_handle = data;
return true;
}
}
return NULL;
return false;
}

View File

@ -16,23 +16,22 @@
#ifndef GL_FONT_H__
#define GL_FONT_H__
#include <stdint.h>
#include "../../driver.h"
#include "../../boolean.h"
typedef struct gl_font_renderer
{
bool (*init)(void *data, const char *font_path, float font_size,
unsigned win_width, unsigned win_height);
void (*deinit)(void *data);
void (*render_msg)(void *data, const char *msg, void *parms);
void *(*init)(void *data, const char *font_path, float font_size);
void (*free)(void *data);
void (*render_msg)(void *data, const char *msg, const struct font_params *parms);
const char *ident;
} gl_font_renderer_t;
extern const gl_font_renderer_t gl_raster_font;
extern const gl_font_renderer_t libdbg_font;
const gl_font_renderer_t *gl_font_init_first(void *data,
const char *font_path, float font_size, unsigned win_width, unsigned win_height);
bool gl_font_init_first(const gl_font_renderer_t **font_driver, void **font_handle,
void *gl_data, const char *font_path, float font_size);
#endif

View File

@ -18,352 +18,246 @@
#include "../gl_common.h"
#include "../shader_common.h"
static bool gl_init_font(void *data, const char *font_path, float font_size, unsigned win_width, unsigned win_height)
typedef struct
{
size_t i, j;
(void)win_width;
(void)win_height;
gl_t *gl;
GLuint tex;
unsigned tex_width, tex_height;
if (!g_settings.video.font_enable)
return false;
const font_renderer_driver_t *font_driver;
void *font_data;
} gl_raster_t;
(void)font_size;
gl_t *gl = (gl_t*)data;
static void *gl_init_font(void *gl_data, const char *font_path, float font_size)
{
gl_raster_t *font = (gl_raster_t*)calloc(1, sizeof(*font));
if (!font)
return NULL;
if (font_renderer_create_default(&gl->font_driver, &gl->font))
{
glGenTextures(1, &gl->font_tex);
glBindTexture(GL_TEXTURE_2D, gl->font_tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glBindTexture(GL_TEXTURE_2D, gl->texture[gl->tex_index]);
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &gl->max_font_size);
}
else
font->gl = (gl_t*)gl_data;
if (!font_renderer_create_default(&font->font_driver, &font->font_data, font_path, font_size))
{
RARCH_WARN("Couldn't init font renderer.\n");
return false;
free(font);
return NULL;
}
for (i = 0; i < 4; i++)
glGenTextures(1, &font->tex);
glBindTexture(GL_TEXTURE_2D, font->tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
const struct font_atlas *atlas = font->font_driver->get_atlas(font->font_data);
unsigned width = next_pow2(atlas->width);
unsigned height = next_pow2(atlas->height);
// Ideally, we'd use single component textures, but the difference in ways to do that between core GL and GLES/legacy GL
// is too great to bother going down that route.
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
uint8_t *tmp_buffer = (uint8_t*)malloc(atlas->width * atlas->height * 4);
if (tmp_buffer)
{
gl->font_color[4 * i + 0] = g_settings.video.msg_color_r;
gl->font_color[4 * i + 1] = g_settings.video.msg_color_g;
gl->font_color[4 * i + 2] = g_settings.video.msg_color_b;
gl->font_color[4 * i + 3] = 1.0;
}
for (i = 0; i < 4; i++)
{
for (j = 0; j < 3; j++)
gl->font_color_dark[4 * i + j] = 0.3 * gl->font_color[4 * i + j];
gl->font_color_dark[4 * i + 3] = 1.0;
}
return true;
}
void gl_deinit_font(void *data)
{
gl_t *gl = (gl_t*)data;
if (gl->font)
{
gl->font_driver->free(gl->font);
glDeleteTextures(1, &gl->font_tex);
if (gl->font_tex_buf)
free(gl->font_tex_buf);
}
}
// Somewhat overwhelming code just to render some damn fonts.
// We aim to use NPOT textures for compatibility with old and shitty cards.
// Also, we want to avoid reallocating a texture for each glyph (performance dips), so we
// contruct the whole texture using one call, and copy straight to it with
// glTexSubImage.
struct font_rect
{
int x, y;
int width, height;
int pot_width, pot_height;
};
static void calculate_msg_geometry(const struct font_output *head, struct font_rect *rect)
{
int x_min = head->off_x;
int x_max = head->off_x + head->width;
int y_min = head->off_y;
int y_max = head->off_y + head->height;
while ((head = head->next))
{
int left = head->off_x;
int right = head->off_x + head->width;
int bottom = head->off_y;
int top = head->off_y + head->height;
if (left < x_min)
x_min = left;
if (right > x_max)
x_max = right;
if (bottom < y_min)
y_min = bottom;
if (top > y_max)
y_max = top;
}
rect->x = x_min;
rect->y = y_min;
rect->width = x_max - x_min;
rect->height = y_max - y_min;
}
static void adjust_power_of_two(gl_t *gl, struct font_rect *geom)
{
// Some systems really hate NPOT textures.
geom->pot_width = next_pow2(geom->width);
geom->pot_height = next_pow2(geom->height);
if (geom->pot_width > gl->max_font_size)
geom->pot_width = gl->max_font_size;
if (geom->pot_height > gl->max_font_size)
geom->pot_height = gl->max_font_size;
if ((geom->pot_width > gl->font_tex_w) || (geom->pot_height > gl->font_tex_h))
{
gl->font_tex_buf = (uint32_t*)realloc(gl->font_tex_buf,
geom->pot_width * geom->pot_height * sizeof(uint32_t));
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, geom->pot_width, geom->pot_height,
0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
gl->font_tex_w = geom->pot_width;
gl->font_tex_h = geom->pot_height;
}
}
static void copy_glyph(const struct font_output *head, const struct font_rect *geom, uint32_t *buffer, unsigned width, unsigned height)
{
int h, w;
// head has top-left oriented coords.
int x = head->off_x - geom->x;
int y = head->off_y - geom->y;
y = height - head->height - y - 1;
const uint8_t *src = head->output;
int font_width = head->width + ((x < 0) ? x : 0);
int font_height = head->height + ((y < 0) ? y : 0);
if (x < 0)
{
src += -x;
x = 0;
}
if (y < 0)
{
src += -y * head->pitch;
y = 0;
}
if (x + font_width > (int)width)
font_width = width - x;
if (y + font_height > (int)height)
font_height = height - y;
uint32_t *dst = buffer + y * width + x;
for (h = 0; h < font_height; h++, dst += width, src += head->pitch)
{
uint8_t *d = (uint8_t*)dst;
for (w = 0; w < font_width; w++)
unsigned i;
uint8_t *dst = tmp_buffer;
const uint8_t *src = atlas->buffer;
for (i = 0; i < atlas->width * atlas->height; i++)
{
*d++ = 0xff;
*d++ = 0xff;
*d++ = 0xff;
*d++ = src[w];
*dst++ = 0xff;
*dst++ = 0xff;
*dst++ = 0xff;
*dst++ = *src++;
}
}
}
// Old style "blitting", so we can render all the fonts in one go.
// TODO: Is it possible that fonts could overlap if we blit without alpha blending?
static void blit_fonts(gl_t *gl, const struct font_output *head, const struct font_rect *geom)
{
memset(gl->font_tex_buf, 0, gl->font_tex_w * gl->font_tex_h * sizeof(uint32_t));
while (head)
{
copy_glyph(head, geom, gl->font_tex_buf, gl->font_tex_w, gl->font_tex_h);
head = head->next;
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, atlas->width, atlas->height, GL_RGBA, GL_UNSIGNED_BYTE, tmp_buffer);
free(tmp_buffer);
}
glPixelStorei(GL_UNPACK_ALIGNMENT, 8);
glTexSubImage2D(GL_TEXTURE_2D,
0, 0, 0, gl->font_tex_w, gl->font_tex_h,
GL_RGBA, GL_UNSIGNED_BYTE, gl->font_tex_buf);
font->tex_width = width;
font->tex_height = height;
glBindTexture(GL_TEXTURE_2D, font->gl->texture[font->gl->tex_index]);
return font;
}
static void calculate_font_coords(gl_t *gl,
GLfloat font_vertex[8], GLfloat font_vertex_dark[8], GLfloat font_tex_coords[8], GLfloat scale, GLfloat pos_x, GLfloat pos_y)
void gl_free_font(void *data)
{
unsigned i;
GLfloat scale_factor = scale;
GLfloat lx = pos_x;
GLfloat hx = (GLfloat)gl->font_last_width * scale_factor / gl->vp.width + lx;
GLfloat ly = pos_y;
GLfloat hy = (GLfloat)gl->font_last_height * scale_factor / gl->vp.height + ly;
font_vertex[0] = lx;
font_vertex[2] = hx;
font_vertex[4] = lx;
font_vertex[6] = hx;
font_vertex[1] = hy;
font_vertex[3] = hy;
font_vertex[5] = ly;
font_vertex[7] = ly;
GLfloat shift_x = 2.0f / gl->vp.width;
GLfloat shift_y = 2.0f / gl->vp.height;
for (i = 0; i < 4; i++)
{
font_vertex_dark[2 * i + 0] = font_vertex[2 * i + 0] - shift_x;
font_vertex_dark[2 * i + 1] = font_vertex[2 * i + 1] - shift_y;
}
lx = 0.0f;
hx = (GLfloat)gl->font_last_width / gl->font_tex_w;
ly = 1.0f - (GLfloat)gl->font_last_height / gl->font_tex_h;
hy = 1.0f;
font_tex_coords[0] = lx;
font_tex_coords[2] = hx;
font_tex_coords[4] = lx;
font_tex_coords[6] = hx;
font_tex_coords[1] = ly;
font_tex_coords[3] = ly;
font_tex_coords[5] = hy;
font_tex_coords[7] = hy;
}
static void setup_font(void *data, const char *msg, GLfloat scale, GLfloat pos_x, GLfloat pos_y)
{
gl_t *gl = (gl_t*)data;
if (!gl->font)
gl_raster_t *font = (gl_raster_t*)data;
if (!font)
return;
if (gl->shader && gl->shader->use)
gl->shader->use(gl, GL_SHADER_STOCK_BLEND);
if (font->font_driver && font->font_data)
font->font_driver->free(font->font_data);
gl_set_viewport(gl, gl->win_width, gl->win_height, false, false);
glDeleteTextures(1, &font->tex);
free(font);
}
glEnable(GL_BLEND);
#define emit(c, vx, vy) do { \
font_vertex[ 2 * (6 * i + c) + 0] = (x + (delta_x + off_x + vx * width) * scale) * inv_win_width; \
font_vertex[ 2 * (6 * i + c) + 1] = (y + (delta_y - off_y - vy * height) * scale) * inv_win_height; \
font_tex_coords[ 2 * (6 * i + c) + 0] = (tex_x + vx * width) * inv_tex_size_x; \
font_tex_coords[ 2 * (6 * i + c) + 1] = (tex_y + vy * height) * inv_tex_size_y; \
font_color[ 4 * (6 * i + c) + 0] = color[0]; \
font_color[ 4 * (6 * i + c) + 1] = color[1]; \
font_color[ 4 * (6 * i + c) + 2] = color[2]; \
font_color[ 4 * (6 * i + c) + 3] = color[3]; \
} while(0)
GLfloat font_vertex[8];
GLfloat font_vertex_dark[8];
GLfloat font_tex_coords[8];
static void render_message(gl_raster_t *font, const char *msg, GLfloat scale, const GLfloat color[4], GLfloat pos_x, GLfloat pos_y)
{
unsigned i;
gl_t *gl = font->gl;
glBindTexture(GL_TEXTURE_2D, gl->font_tex);
glBindTexture(GL_TEXTURE_2D, font->tex);
gl->coords.tex_coord = font_tex_coords;
#define MAX_MSG_LEN_CHUNK 64
GLfloat font_tex_coords[2 * 6 * MAX_MSG_LEN_CHUNK];
GLfloat font_vertex[2 * 6 * MAX_MSG_LEN_CHUNK];
GLfloat font_color[4 * 6 * MAX_MSG_LEN_CHUNK];
struct font_output_list out;
unsigned msg_len_full = strlen(msg);
unsigned msg_len = min(msg_len_full, MAX_MSG_LEN_CHUNK);
// If we get the same message, there's obviously no need to render fonts again ...
if (strcmp(gl->font_last_msg, msg) != 0)
int x = roundf(pos_x * gl->vp.width);
int y = roundf(pos_y * gl->vp.height);
int delta_x = 0;
int delta_y = 0;
float inv_tex_size_x = 1.0f / font->tex_width;
float inv_tex_size_y = 1.0f / font->tex_height;
float inv_win_width = 1.0f / font->gl->vp.width;
float inv_win_height = 1.0f / font->gl->vp.height;
while (msg_len_full)
{
gl->font_driver->render_msg(gl->font, msg, &out);
struct font_output *head = out.head;
// Rebind shaders so attrib cache gets reset.
if (gl->shader && gl->shader->use)
gl->shader->use(gl, GL_SHADER_STOCK_BLEND);
struct font_rect geom;
calculate_msg_geometry(head, &geom);
adjust_power_of_two(gl, &geom);
blit_fonts(gl, head, &geom);
for (i = 0; i < msg_len; i++)
{
const struct font_glyph *gly = font->font_driver->get_glyph(font->font_data, (uint8_t)msg[i]);
if (!gly)
gly = font->font_driver->get_glyph(font->font_data, '?'); // Do something smarter here ...
if (!gly)
continue;
gl->font_driver->free_output(gl->font, &out);
strlcpy(gl->font_last_msg, msg, sizeof(gl->font_last_msg));
int off_x = gly->draw_offset_x;
int off_y = gly->draw_offset_y;
int tex_x = gly->atlas_offset_x;
int tex_y = gly->atlas_offset_y;
int width = gly->width;
int height = gly->height;
gl->font_last_width = geom.width;
gl->font_last_height = geom.height;
emit(0, 0, 1); // Bottom-left
emit(1, 1, 1); // Bottom-right
emit(2, 0, 0); // Top-left
emit(3, 1, 0); // Top-right
emit(4, 0, 0); // Top-left
emit(5, 1, 1); // Bottom-right
#undef emit
delta_x += gly->advance_x;
delta_y -= gly->advance_y;
}
gl->coords.tex_coord = font_tex_coords;
gl->coords.vertex = font_vertex;
gl->coords.color = font_color;
gl->coords.vertices = 6 * msg_len;
gl_shader_set_coords(gl, &gl->coords, &gl->mvp_no_rot);
glDrawArrays(GL_TRIANGLES, 0, 6 * msg_len);
msg_len_full -= msg_len;
msg += msg_len;
msg_len = min(msg_len_full, MAX_MSG_LEN_CHUNK);
}
calculate_font_coords(gl, font_vertex, font_vertex_dark, font_tex_coords,
scale, pos_x, pos_y);
gl->coords.vertex = font_vertex_dark;
gl->coords.color = gl->font_color_dark;
gl_shader_set_coords(gl, &gl->coords, &gl->mvp);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
gl->coords.vertex = font_vertex;
gl->coords.color = gl->font_color;
gl_shader_set_coords(gl, &gl->coords, &gl->mvp);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
// Post - Go back to old rendering path.
gl->coords.vertex = gl->vertex_ptr;
gl->coords.tex_coord = gl->tex_coords;
gl->coords.color = gl->white_color_ptr;
gl->coords.vertices = 4;
glBindTexture(GL_TEXTURE_2D, gl->texture[gl->tex_index]);
glDisable(GL_BLEND);
struct gl_ortho ortho = {0, 1, 0, 1, -1, 1};
gl_set_projection(gl, &ortho, true);
}
static void gl_render_msg(void *data, const char *msg, void *parms)
static void gl_render_msg(void *data, const char *msg, const struct font_params *params)
{
GLfloat x, y, scale, alpha;
gl_t *gl;
font_params_t *params;
int i;
GLfloat x, y, scale, drop_mod;
GLfloat color[4], color_dark[4];
int drop_x, drop_y;
bool full_screen;
(void)data;
(void)msg;
gl = (gl_t*)data;
params = (font_params_t*)parms;
if (!gl)
gl_raster_t *font = (gl_raster_t*)data;
if (!font)
return;
gl_t *gl = font->gl;
if (params)
{
x = params->x;
y = params->y;
scale = params->scale;
alpha = params->alpha;
full_screen = params->full_screen;
drop_x = params->drop_x;
drop_y = params->drop_y;
drop_mod = params->drop_mod;
color[0] = FONT_COLOR_GET_RED(params->color) / 255.0f;
color[1] = FONT_COLOR_GET_GREEN(params->color) / 255.0f;
color[2] = FONT_COLOR_GET_BLUE(params->color) / 255.0f;
color[3] = FONT_COLOR_GET_ALPHA(params->color) / 255.0f;
// If alpha is 0.0f, turn it into default 1.0f
if (alpha <= 0.0f)
alpha = 1.0f;
if (color[3] <= 0.0f)
color[3] = 1.0f;
}
else
{
x = g_settings.video.msg_pos_x;
y = g_settings.video.msg_pos_y;
scale = g_settings.video.font_scale ? (GLfloat)gl->vp.width / (GLfloat)gl->full_x : 1.0f;
alpha = 1.0f;
scale = 1.0f;
full_screen = false;
color[0] = g_settings.video.msg_color_r;
color[1] = g_settings.video.msg_color_g;
color[2] = g_settings.video.msg_color_b;
color[3] = 1.0f;
drop_x = -2;
drop_y = -2;
drop_mod = 0.3f;
}
for (i = 0; i < 4; i++)
gl_set_viewport(gl, gl->win_width, gl->win_height, full_screen, false);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBlendEquation(GL_FUNC_ADD);
if (drop_x || drop_y)
{
gl->font_color[4 * i + 3] = alpha;
gl->font_color_dark[4 * i + 3] = alpha;
}
color_dark[0] = color[0] * drop_mod;
color_dark[1] = color[1] * drop_mod;
color_dark[2] = color[2] * drop_mod;
color_dark[3] = color[3];
setup_font(data, msg, scale, x, y);
render_message(font, msg, scale, color_dark,
x + scale * drop_x / gl->vp.width, y + scale * drop_y / gl->vp.height);
}
render_message(font, msg, scale, color, x, y);
glDisable(GL_BLEND);
gl_set_viewport(gl, gl->win_width, gl->win_height, false, true);
}
const gl_font_renderer_t gl_raster_font = {
gl_init_font,
gl_deinit_font,
gl_free_font,
gl_render_msg,
"GL raster",
};

View File

@ -16,6 +16,7 @@
#include "fonts.h"
#include "../gfx_common.h"
#include "../gl_common.h"
#if defined(SN_TARGET_PSP2)
#include <libdbgfont.h>
@ -32,44 +33,38 @@
#define DbgFontExit cellDbgFontExit
#endif
static bool gl_init_font(void *data, const char *font_path, float font_size,
unsigned win_width, unsigned win_height)
static void *gl_init_font(void *gl_data, const char *font_path, float font_size)
{
(void)font_path;
(void)font_size;
font_renderer_t *handle = (font_renderer_t*)calloc(1, sizeof(*handle));
if (!handle)
return NULL;
gl_t *gl = (gl_t*)gl_data;
DbgFontConfig cfg;
#if defined(SN_TARGET_PSP2)
cfg.fontSize = SCE_DBGFONT_FONTSIZE_LARGE;
#elif defined(__CELLOS_LV2__)
cfg.bufSize = SCE_DBGFONT_BUFSIZE_LARGE;
cfg.screenWidth = win_width;
cfg.screenHeight = win_height;
cfg.screenWidth = gl->win_width;
cfg.screenHeight = gl->win_height;
#endif
DbgFontInit(&cfg);
free(handle);
return true;
// Doesn't need any state.
return (void*)-1;
}
static void gl_deinit_font(void *data)
{
(void)data;
DbgFontExit();
}
static void gl_render_msg(void *data, const char *msg, void *parms)
static void gl_render_msg(void *data, const char *msg, const struct font_params *params)
{
(void)data;
float x, y, scale;
unsigned color;
font_params_t *params = (font_params_t*)parms;
if (params)
{
@ -103,3 +98,4 @@ const gl_font_renderer_t libdbg_font = {
gl_render_msg,
"GL raster",
};

View File

@ -42,10 +42,9 @@ static void xfonts_deinit_font(void *data)
(void)data;
}
static void xfonts_render_msg(void *data, const char *msg, void *parms)
static void xfonts_render_msg(void *data, const char *msg, const struct font_params *params)
{
d3d_video_t *d3d = (d3d_video_t*)data;
font_params_t *params = (font_params_t*)parms;
wchar_t str[PATH_MAX];
float x, y;

View File

@ -435,11 +435,10 @@ static void xdk_video_font_draw_text(xdk360_video_font_t *font, void *video_data
d3dr->EndVertices();
}
static void xdk_render_msg(void *data, const char *str_msg, void *parms)
static void xdk_render_msg(void *data, const char *str_msg, const struct font_params *params)
{
d3d_video_t *d3d = (d3d_video_t*)data;
xdk360_video_font_t *font = &m_Font;
font_params_t *params = (font_params_t*)parms;
wchar_t msg[PATH_MAX];
float x, y;

View File

@ -21,6 +21,7 @@ extern "C" {
#endif
#include <stddef.h>
#include <stdint.h>
#include "../general.h"
#include "../boolean.h"
#include "../performance.h"
@ -40,15 +41,6 @@ void gfx_set_dwm(void);
void gfx_scale_integer(struct rarch_viewport *vp, unsigned win_width, unsigned win_height,
float aspect_ratio, bool keep_aspect);
typedef struct
{
float x;
float y;
float scale;
float alpha;
unsigned color;
} font_params_t;
#define MIN_SCALING_FACTOR (1.0f)
#if defined(__CELLOS_LV2__)

View File

@ -1037,6 +1037,7 @@ static void gl_frame_fbo(void *data, const struct gl_tex_info *tex_info)
gl->vp.width, gl->vp.height, g_extern.frame_count,
tex_info, gl->prev_info, fbo_tex_info, fbo_tex_info_cnt);
gl->coords.vertices = 4;
gl_shader_set_coords(gl, &gl->coords, &gl->mvp);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
@ -1086,6 +1087,7 @@ static void gl_frame_fbo(void *data, const struct gl_tex_info *tex_info)
gl->coords.vertex = gl->vertex_ptr;
gl->coords.vertices = 4;
gl_shader_set_coords(gl, &gl->coords, &gl->mvp);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
@ -1474,6 +1476,7 @@ static inline void gl_draw_texture(void *data)
if (gl->shader)
gl->shader->use(gl, GL_SHADER_STOCK_BLEND);
gl->coords.vertices = 4;
gl_shader_set_coords(gl, &gl->coords, &gl->mvp_no_rot);
glEnable(GL_BLEND);
@ -1612,6 +1615,7 @@ static bool gl_frame(void *data, const void *frame, unsigned width, unsigned hei
g_extern.frame_count,
&tex_info, gl->prev_info, NULL, 0);
gl->coords.vertices = 4;
gl_shader_set_coords(gl, &gl->coords, &gl->mvp);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
@ -1630,8 +1634,8 @@ static bool gl_frame(void *data, const void *frame, unsigned width, unsigned hei
gl_draw_texture(gl);
#endif
if (msg && gl->font_ctx)
gl->font_ctx->render_msg(gl, msg, NULL);
if (msg && gl->font_driver && gl->font_handle)
gl->font_driver->render_msg(gl->font_handle, msg, NULL);
#ifdef HAVE_OVERLAY
if (gl->overlay_enable)
@ -1750,8 +1754,8 @@ static void gl_free(void *data)
}
#endif
if (gl->font_ctx)
gl->font_ctx->deinit(gl);
if (gl->font_driver && gl->font_handle)
gl->font_driver->free(gl->font_handle);
gl_shader_deinit(gl);
#ifndef NO_GL_FF_VERTEX
@ -2314,6 +2318,7 @@ static void *gl_init(const video_info_t *video, const input_driver_t **input, vo
gl->coords.tex_coord = gl->tex_coords;
gl->coords.color = gl->white_color_ptr;
gl->coords.lut_tex_coord = tex_coords;
gl->coords.vertices = 4;
// Empty buffer that we use to clear out the texture with on res change.
gl->empty_buf = calloc(sizeof(uint32_t), gl->tex_w * gl->tex_h);
@ -2352,8 +2357,9 @@ static void *gl_init(const video_info_t *video, const input_driver_t **input, vo
if (g_settings.video.font_enable)
#endif
{
gl->font_ctx = gl_font_init_first(gl, g_settings.video.font_path, g_settings.video.font_size,
gl->win_width, gl->win_height);
if (!gl_font_init_first(&gl->font_driver, &gl->font_handle,
gl, *g_settings.video.font_path ? g_settings.video.font_path : NULL, g_settings.video.font_size))
RARCH_ERR("[GL]: Failed to init font renderer.\n");
}
#ifdef HAVE_GL_ASYNC_READBACK
@ -2794,6 +2800,7 @@ static void gl_render_overlay(void *data)
gl->coords.vertex = gl->overlay[i].vertex_coord;
gl->coords.tex_coord = gl->overlay[i].tex_coord;
gl->coords.color = white_color_mod;
gl->coords.vertices = 4;
gl_shader_set_coords(gl, &gl->coords, &gl->mvp_no_rot);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
@ -2934,19 +2941,18 @@ static void gl_apply_state_changes(void *data)
gl->should_resize = true;
}
static void gl_set_osd_msg(void *data, const char *msg, void *userdata)
static void gl_set_osd_msg(void *data, const char *msg, const struct font_params *params)
{
gl_t *gl = (gl_t*)data;
if (!gl)
return;
context_bind_hw_render(gl, false);
font_params_t *params = (font_params_t*)userdata;
if (gl->font_ctx)
gl->font_ctx->render_msg(gl, msg, params);
context_bind_hw_render(gl, true);
if (gl->font_driver && gl->font_handle)
{
context_bind_hw_render(gl, false);
gl->font_driver->render_msg(gl->font_handle, msg, params);
context_bind_hw_render(gl, true);
}
}
static void gl_show_mouse(void *data, bool state)

View File

@ -189,6 +189,7 @@ struct gl_coords
const GLfloat *color;
const GLfloat *tex_coord;
const GLfloat *lut_tex_coord;
unsigned vertices;
};
typedef struct gl_shader_backend gl_shader_backend_t;
@ -282,17 +283,8 @@ typedef struct gl
#endif
// Fonts
void *font;
const gl_font_renderer_t *font_ctx;
const font_renderer_driver_t *font_driver;
GLuint font_tex;
GLint max_font_size;
int font_tex_w, font_tex_h;
uint32_t *font_tex_buf;
char font_last_msg[256];
int font_last_width, font_last_height;
GLfloat font_color[16];
GLfloat font_color_dark[16];
const gl_font_renderer_t *font_driver;
void *font_handle;
bool egl_images;
video_info_t video_info;

View File

@ -478,7 +478,8 @@ static void lima_gfx_free(void *data) {
static void lima_init_font(lima_video_t *vid, const char *font_path, unsigned font_size) {
if (!g_settings.video.font_enable) return;
if (font_renderer_create_default(&vid->font_driver, &vid->font)) {
if (font_renderer_create_default(&vid->font_driver, &vid->font,
*g_settings.video.font_path ? g_settings.video.font_path : NULL, g_settings.video.font_size)) {
int r = g_settings.video.msg_color_r * 255;
int g = g_settings.video.msg_color_g * 255;
int b = g_settings.video.msg_color_b * 255;
@ -498,8 +499,8 @@ static void lima_render_msg(lima_video_t *vid, const char *msg) {
unsigned req_size;
limare_data_t *lima = vid->lima;
const int msg_base_x = g_settings.video.msg_pos_x * lima->font_width;
const int msg_base_y = (1.0 - g_settings.video.msg_pos_y) * lima->font_height;
int msg_base_x = g_settings.video.msg_pos_x * lima->font_width;
int msg_base_y = (1.0 - g_settings.video.msg_pos_y) * lima->font_height;
if (vid->font == NULL) return;
@ -523,11 +524,16 @@ static void lima_render_msg(lima_video_t *vid, const char *msg) {
memset(lima->buffer, 0, req_size);
vid->font_driver->render_msg(vid->font, msg, &out);
/* FIXME: Untested new font rendering code. */
const struct font_atlas *atlas = vid->font_driver->get_atlas(vid->font);
for (head = out.head; head; head = head->next) {
int base_x = msg_base_x + head->off_x;
int base_y = msg_base_y - head->off_y - head->height;
for (; msg; msg++) {
const struct font_glyph *glyph = vid->font_driver->get_glyph(vid->font, (uint8_t)*msg);
if (!glyph)
continue;
int base_x = msg_base_x + glyph->draw_offset_x;
int base_y = msg_base_y + glyph->draw_offset_y;
const int max_width = lima->font_width - base_x;
const int max_height = lima->font_height - base_y;
@ -535,7 +541,7 @@ static void lima_render_msg(lima_video_t *vid, const char *msg) {
int glyph_width = head->width;
int glyph_height = head->height;
const uint8_t *src = head->output;
const uint8_t *src = atlas->buffer + glyph->atlas_offset_x + glyph->atlas_offset_y * atlas->width;
if (base_x < 0) {
src -= base_x;
@ -544,7 +550,7 @@ static void lima_render_msg(lima_video_t *vid, const char *msg) {
}
if (base_y < 0) {
src -= base_y * (int)head->pitch;
src -= base_y * (int)atlas->width;
glyph_height += base_y;
base_y = 0;
}
@ -556,10 +562,11 @@ static void lima_render_msg(lima_video_t *vid, const char *msg) {
put_glyph_rgba4444(lima, src, vid->font_rgb,
glyph_width, glyph_height,
head->pitch, base_x, base_y);
}
atlas->width, base_x, base_y);
vid->font_driver->free_output(vid->font, &out);
msg_base_x += glyph->advance_x;
msg_base_y += glyph->advance_y;
}
}
static void *lima_gfx_init(const video_info_t *video, const input_driver_t **input, void **input_data) {

View File

@ -708,7 +708,8 @@ static void omap_gfx_free(void *data) {
static void omap_init_font(omap_video_t *vid, const char *font_path, unsigned font_size) {
if (!g_settings.video.font_enable) return;
if (font_renderer_create_default(&vid->font_driver, &vid->font)) {
if (font_renderer_create_default(&vid->font_driver, &vid->font,
*g_settings.video.font_path ? g_settings.video.font_path : NULL, g_settings.video.font_size)) {
int r = g_settings.video.msg_color_r * 255;
int g = g_settings.video.msg_color_g * 255;
int b = g_settings.video.msg_color_b * 255;
@ -726,18 +727,19 @@ static void omap_init_font(omap_video_t *vid, const char *font_path, unsigned fo
}
static void omap_render_msg(omap_video_t *vid, const char *msg) {
struct font_output_list out;
struct font_output *head;
const int msg_base_x = g_settings.video.msg_pos_x * vid->width;
const int msg_base_y = (1.0 - g_settings.video.msg_pos_y) * vid->height;
int msg_base_x = g_settings.video.msg_pos_x * vid->width;
int msg_base_y = (1.0 - g_settings.video.msg_pos_y) * vid->height;
if (vid->font == NULL) return;
vid->font_driver->render_msg(vid->font, msg, &out);
for (head = out.head; head; head = head->next) {
int base_x = msg_base_x + head->off_x;
int base_y = msg_base_y - head->off_y - head->height;
const struct font_atlas *atlas = vid->font_driver->get_atlas(vid->font);
for (; *msg; msg++) {
const struct font_glyph *glyph = vid->font_driver->get_glyph(vid->font, (uint8_t)*msg);
if (!glyph) continue;
int base_x = msg_base_x + glyph->draw_offset_x;
int base_y = msg_base_y + glyph->draw_offset_y;
const int max_width = vid->width - base_x;
const int max_height = vid->height - base_y;
@ -745,7 +747,7 @@ static void omap_render_msg(omap_video_t *vid, const char *msg) {
int glyph_width = head->width;
int glyph_height = head->height;
const uint8_t *src = head->output;
const uint8_t *src = atlas->buffer + glyph->atlas_offset_x + glyph->atlas_offset_y * atlas->width;
if (base_x < 0) {
src -= base_x;
@ -754,7 +756,7 @@ static void omap_render_msg(omap_video_t *vid, const char *msg) {
}
if (base_y < 0) {
src -= base_y * (int)head->pitch;
src -= base_y * (int)atlas->width;
glyph_height += base_y;
base_y = 0;
}
@ -767,15 +769,16 @@ static void omap_render_msg(omap_video_t *vid, const char *msg) {
if (vid->bytes_per_pixel == 2) {
omapfb_blend_glyph_rgb565(vid->omap, src, vid->font_rgb,
glyph_width, glyph_height,
head->pitch, base_x, base_y);
atlas->width, base_x, base_y);
} else {
omapfb_blend_glyph_argb8888(vid->omap, src, vid->font_rgb,
glyph_width, glyph_height,
head->pitch, base_x, base_y);
atlas->width, base_x, base_y);
}
}
vid->font_driver->free_output(vid->font, &out);
msg_base_x += glyph->advance_x;
msg_base_y += glyph->advance_y;
}
}
static void *omap_gfx_init(const video_info_t *video, const input_driver_t **input, void **input_data) {

View File

@ -70,7 +70,9 @@ static void sdl_init_font(sdl_video_t *vid, const char *font_path, unsigned font
if (!g_settings.video.font_enable)
return;
if (font_renderer_create_default(&vid->font_driver, &vid->font))
if (font_renderer_create_default(&vid->font_driver, &vid->font,
*g_settings.video.font_path ? g_settings.video.font_path : NULL,
g_settings.video.font_size))
{
int r = g_settings.video.msg_color_r * 255;
int g = g_settings.video.msg_color_g * 255;
@ -91,37 +93,34 @@ static void sdl_init_font(sdl_video_t *vid, const char *font_path, unsigned font
static void sdl_render_msg(sdl_video_t *vid, SDL_Surface *buffer,
const char *msg, unsigned width, unsigned height, const SDL_PixelFormat *fmt)
{
int x, y, msg_base_x, msg_base_y;
int x, y, msg_base_x, msg_base_y, delta_x, delta_y;
unsigned rshift, gshift, bshift;
struct font_output_list out;
struct font_output *head;
if (!vid->font)
return;
vid->font_driver->render_msg(vid->font, msg, &out);
head = (struct font_output*)out.head;
const struct font_atlas *atlas = vid->font_driver->get_atlas(vid->font);
msg_base_x = g_settings.video.msg_pos_x * width;
msg_base_y = (1.0 - g_settings.video.msg_pos_y) * height;
msg_base_y = (1.0f - g_settings.video.msg_pos_y) * height;
rshift = fmt->Rshift;
gshift = fmt->Gshift;
bshift = fmt->Bshift;
for (; head; head = head->next)
for (; *msg; msg++)
{
int base_x, base_y, glyph_width, glyph_height, max_width, max_height;
const uint8_t *src;
uint32_t *out;
const struct font_glyph *glyph = vid->font_driver->get_glyph(vid->font, (uint8_t)*msg);
if (!glyph)
continue;
base_x = msg_base_x + head->off_x;
base_y = msg_base_y - head->off_y - head->height;
int glyph_width = glyph->width;
int glyph_height = glyph->height;
glyph_width = head->width;
glyph_height = head->height;
int base_x = msg_base_x + glyph->draw_offset_x;
int base_y = msg_base_y + glyph->draw_offset_y;
src = (const uint8_t*)head->output;
const uint8_t *src = atlas->buffer + glyph->atlas_offset_x + glyph->atlas_offset_y * atlas->width;
if (base_x < 0)
{
@ -132,13 +131,13 @@ static void sdl_render_msg(sdl_video_t *vid, SDL_Surface *buffer,
if (base_y < 0)
{
src -= base_y * (int)head->pitch;
src -= base_y * (int)atlas->width;
glyph_height += base_y;
base_y = 0;
}
max_width = width - base_x;
max_height = height - base_y;
int max_width = width - base_x;
int max_height = height - base_y;
if (max_width <= 0 || max_height <= 0)
continue;
@ -148,9 +147,9 @@ static void sdl_render_msg(sdl_video_t *vid, SDL_Surface *buffer,
if (glyph_height > max_height)
glyph_height = max_height;
out = (uint32_t*)buffer->pixels + base_y * (buffer->pitch >> 2) + base_x;
uint32_t *out = (uint32_t*)buffer->pixels + base_y * (buffer->pitch >> 2) + base_x;
for (y = 0; y < glyph_height; y++, src += head->pitch, out += buffer->pitch >> 2)
for (y = 0; y < glyph_height; y++, src += atlas->width, out += buffer->pitch >> 2)
{
for (x = 0; x < glyph_width; x++)
{
@ -166,9 +165,10 @@ static void sdl_render_msg(sdl_video_t *vid, SDL_Surface *buffer,
out[x] = (out_r << rshift) | (out_g << gshift) | (out_b << bshift);
}
}
}
vid->font_driver->free_output(vid->font, &out);
msg_base_x += glyph->advance_x;
msg_base_y += glyph->advance_y;
}
}
static void sdl_gfx_set_handles(void)

View File

@ -64,11 +64,11 @@ static char glsl_alias_define[1024];
struct cache_vbo
{
GLuint vbo_primary;
GLfloat buffer_primary[128];
GLfloat *buffer_primary;
size_t size_primary;
GLuint vbo_secondary;
GLfloat buffer_secondary[128];
GLfloat *buffer_secondary;
size_t size_secondary;
};
static struct cache_vbo glsl_vbo[GFX_MAX_SHADERS];
@ -467,18 +467,24 @@ static void gl_glsl_reset_attrib(void)
gl_attrib_index = 0;
}
static void gl_glsl_set_vbo(GLfloat *buffer, size_t *buffer_elems, const GLfloat *data, size_t elems)
static void gl_glsl_set_vbo(GLfloat **buffer, size_t *buffer_elems, const GLfloat *data, size_t elems)
{
if (elems != *buffer_elems || memcmp(data, buffer, elems * sizeof(GLfloat)))
if (elems != *buffer_elems || memcmp(data, *buffer, elems * sizeof(GLfloat)))
{
//RARCH_LOG("[GL]: VBO updated with %u elems.\n", (unsigned)elems);
memcpy(buffer, data, elems * sizeof(GLfloat));
if (elems > *buffer_elems)
{
GLfloat *new_buffer = (GLfloat*)realloc(*buffer, elems * sizeof(GLfloat));
rarch_assert(new_buffer);
*buffer = new_buffer;
}
memcpy(*buffer, data, elems * sizeof(GLfloat));
glBufferData(GL_ARRAY_BUFFER, elems * sizeof(GLfloat), data, GL_STATIC_DRAW);
*buffer_elems = elems;
}
}
static void gl_glsl_set_attribs(GLuint vbo, GLfloat *buffer, size_t *buffer_elems,
static void gl_glsl_set_attribs(GLuint vbo, GLfloat **buffer, size_t *buffer_elems,
const GLfloat *data, size_t elems, const struct glsl_attrib *attrs, size_t num_attrs)
{
size_t i;
@ -639,6 +645,9 @@ static void gl_glsl_deinit(void)
glDeleteBuffers(1, &glsl_vbo[i].vbo_primary);
if (glsl_vbo[i].vbo_secondary)
glDeleteBuffers(1, &glsl_vbo[i].vbo_secondary);
free(glsl_vbo[i].buffer_primary);
free(glsl_vbo[i].buffer_secondary);
}
memset(&glsl_vbo, 0, sizeof(glsl_vbo));
}
@ -843,7 +852,7 @@ static void gl_glsl_set_params(void *data, unsigned width, unsigned height,
if (!glsl_enable || (gl_program[active_index] == 0))
return;
GLfloat buffer[128];
GLfloat buffer[512];
unsigned i;
size_t size = 0;
struct glsl_attrib attribs[32];
@ -986,7 +995,7 @@ static void gl_glsl_set_params(void *data, unsigned width, unsigned height,
if (size)
{
gl_glsl_set_attribs(glsl_vbo[active_index].vbo_secondary,
glsl_vbo[active_index].buffer_secondary,
&glsl_vbo[active_index].buffer_secondary,
&glsl_vbo[active_index].size_secondary,
buffer, size, attribs, attribs_size);
}
@ -1035,7 +1044,15 @@ static bool gl_glsl_set_coords(const struct gl_coords *coords)
if (!glsl_enable || !glsl_shader->modern)
return false;
GLfloat buffer[128];
// Avoid hitting malloc on every single regular quad draw.
GLfloat short_buffer[4 * (2 + 2 + 4 + 2)];
GLfloat *buffer = short_buffer;
if (coords->vertices > 4)
buffer = (GLfloat*)calloc(coords->vertices * (2 + 2 + 4 + 2), sizeof(*buffer));
if (!buffer)
return false;
size_t size = 0;
struct glsl_attrib attribs[4];
@ -1051,8 +1068,8 @@ static bool gl_glsl_set_coords(const struct gl_coords *coords)
attribs_size++;
attr++;
memcpy(buffer + size, coords->tex_coord, 8 * sizeof(GLfloat));
size += 8;
memcpy(buffer + size, coords->tex_coord, 2 * coords->vertices * sizeof(GLfloat));
size += 2 * coords->vertices;
}
if (uni->vertex_coord >= 0)
@ -1063,8 +1080,8 @@ static bool gl_glsl_set_coords(const struct gl_coords *coords)
attribs_size++;
attr++;
memcpy(buffer + size, coords->vertex, 8 * sizeof(GLfloat));
size += 8;
memcpy(buffer + size, coords->vertex, 2 * coords->vertices * sizeof(GLfloat));
size += 2 * coords->vertices;
}
if (uni->color >= 0)
@ -1075,8 +1092,8 @@ static bool gl_glsl_set_coords(const struct gl_coords *coords)
attribs_size++;
attr++;
memcpy(buffer + size, coords->color, 16 * sizeof(GLfloat));
size += 16;
memcpy(buffer + size, coords->color, 4 * coords->vertices * sizeof(GLfloat));
size += 4 * coords->vertices;
}
if (uni->lut_tex_coord >= 0)
@ -1087,19 +1104,21 @@ static bool gl_glsl_set_coords(const struct gl_coords *coords)
attribs_size++;
attr++;
memcpy(buffer + size, coords->lut_tex_coord, 8 * sizeof(GLfloat));
size += 8;
memcpy(buffer + size, coords->lut_tex_coord, 2 * coords->vertices * sizeof(GLfloat));
size += 2 * coords->vertices;
}
if (size)
{
gl_glsl_set_attribs(glsl_vbo[active_index].vbo_primary,
glsl_vbo[active_index].buffer_primary,
&glsl_vbo[active_index].buffer_primary,
&glsl_vbo[active_index].size_primary,
buffer, size,
attribs, attribs_size);
}
if (buffer != short_buffer)
free(buffer);
return true;
}

View File

@ -137,7 +137,8 @@ static void *vg_init(const video_info_t *video, const input_driver_t **input, vo
vg->driver->input_driver(vg, input, input_data);
if (g_settings.video.font_enable && font_renderer_create_default(&vg->font_driver, &vg->mFontRenderer))
if (g_settings.video.font_enable && font_renderer_create_default(&vg->font_driver, &vg->mFontRenderer,
*g_settings.video.font_path ? g_settings.video.font_path : NULL, g_settings.video.font_size))
{
vg->mFont = vgCreateFont(0);
@ -199,6 +200,7 @@ static void vg_free(void *data)
free(vg);
}
#if 0
static void vg_render_message(vg_t *vg, const char *msg)
{
free(vg->mLastMsg);
@ -273,6 +275,7 @@ static void vg_draw_message(vg_t *vg, const char *msg)
vgSeti(VG_SCISSORING, VG_TRUE);
vgSeti(VG_IMAGE_MODE, VG_DRAW_IMAGE_NORMAL);
}
#endif
static void vg_calculate_quad(vg_t *vg)
{
@ -383,8 +386,10 @@ static bool vg_frame(void *data, const void *frame, unsigned width, unsigned hei
vgDrawImage(vg->mImage);
#if 0
if (msg && vg->mFontsOn)
vg_draw_message(vg, msg);
#endif
vg->driver->update_window_title(vg);

View File

@ -129,7 +129,7 @@ static void xv_init_font(xv_t *xv, const char *font_path, unsigned font_size)
if (!g_settings.video.font_enable)
return;
if (font_renderer_create_default(&xv->font_driver, &xv->font))
if (font_renderer_create_default(&xv->font_driver, &xv->font, *g_settings.video.font_path ? g_settings.video.font_path : NULL, g_settings.video.font_size))
{
int r = g_settings.video.msg_color_r * 255;
r = (r < 0 ? 0 : (r > 255 ? 255 : r));
@ -608,12 +608,11 @@ static void xv_render_msg(xv_t *xv, const char *msg, unsigned width, unsigned he
int x, y;
unsigned i;
struct font_output_list out;
xv->font_driver->render_msg(xv->font, msg, &out);
struct font_output *head = out.head;
const struct font_atlas *atlas = xv->font_driver->get_atlas(xv->font);
int msg_base_x = g_settings.video.msg_pos_x * width;
int msg_base_y = height * (1.0 - g_settings.video.msg_pos_y);
int msg_base_y = height * (1.0f - g_settings.video.msg_pos_y);
unsigned luma_index[2] = { xv->luma_index[0], xv->luma_index[1] };
unsigned chroma_u_index = xv->chroma_u_index;
@ -621,15 +620,19 @@ static void xv_render_msg(xv_t *xv, const char *msg, unsigned width, unsigned he
unsigned pitch = width << 1; // YUV formats used are 16 bpp.
for (; head; head = head->next)
for (; *msg; msg++)
{
int base_x = (msg_base_x + head->off_x) & ~1; // Make sure we always start on the correct boundary so the indices are correct.
int base_y = msg_base_y - head->off_y - head->height;
const struct font_glyph *glyph = xv->font_driver->get_glyph(xv->font, (uint8_t)*msg);
if (!glyph)
continue;
int glyph_width = head->width;
int glyph_height = head->height;
int base_x = (msg_base_x + glyph->draw_offset_x + 1) & ~1; // Make sure we always start on the correct boundary so the indices are correct.
int base_y = msg_base_y + glyph->draw_offset_y;
const uint8_t *src = head->output;
int glyph_width = glyph->width;
int glyph_height = glyph->height;
const uint8_t *src = atlas->buffer + glyph->atlas_offset_x + glyph->atlas_offset_y * atlas->width;
if (base_x < 0)
{
@ -640,7 +643,7 @@ static void xv_render_msg(xv_t *xv, const char *msg, unsigned width, unsigned he
if (base_y < 0)
{
src -= base_y * (int)head->pitch;
src -= base_y * (int)atlas->width;
glyph_height += base_y;
base_y = 0;
}
@ -658,7 +661,7 @@ static void xv_render_msg(xv_t *xv, const char *msg, unsigned width, unsigned he
uint8_t *out = (uint8_t*)xv->image->data + base_y * pitch + (base_x << 1);
for (y = 0; y < glyph_height; y++, src += head->pitch, out += pitch)
for (y = 0; y < glyph_height; y++, src += atlas->width, out += pitch)
{
// 2 input pixels => 4 bytes (2Y, 1U, 1V).
for (x = 0; x < glyph_width; x += 2)
@ -689,9 +692,10 @@ static void xv_render_msg(xv_t *xv, const char *msg, unsigned width, unsigned he
out[out_x + chroma_v_index] = blended;
}
}
}
xv->font_driver->free_output(xv->font, &out);
msg_base_x += glyph->advance_x;
msg_base_y += glyph->advance_y;
}
}
static bool xv_frame(void *data, const void *frame, unsigned width, unsigned height, unsigned pitch, const char *msg)

View File

@ -180,10 +180,7 @@
# video_font_path =
# Size of the font rendered.
# video_font_size = 48
# Attempt to scale the font to fit better for multiple window sizes.
# video_font_scale = true
# video_font_size = 32
# Enable usage of OSD messages.
# video_font_enable = true

View File

@ -292,7 +292,6 @@ void config_set_defaults(void)
g_settings.video.font_enable = font_enable;
g_settings.video.font_size = font_size;
g_settings.video.font_scale = font_scale;
g_settings.video.msg_pos_x = message_pos_offset_x;
g_settings.video.msg_pos_y = message_pos_offset_y;
@ -839,7 +838,6 @@ bool config_load_file(const char *path, bool set_defaults)
CONFIG_GET_PATH(video.font_path, "video_font_path");
CONFIG_GET_FLOAT(video.font_size, "video_font_size");
CONFIG_GET_BOOL(video.font_enable, "video_font_enable");
CONFIG_GET_BOOL(video.font_scale, "video_font_scale");
CONFIG_GET_FLOAT(video.msg_pos_x, "video_message_pos_x");
CONFIG_GET_FLOAT(video.msg_pos_y, "video_message_pos_y");
CONFIG_GET_INT(video.rotation, "video_rotation");
@ -1397,7 +1395,6 @@ bool config_save_file(const char *path)
config_set_bool(conf, "location_allow", g_settings.location.allow);
#endif
config_set_bool(conf, "video_font_scale", g_settings.video.font_scale);
config_set_float(conf, "video_font_size", g_settings.video.font_size);
config_set_bool(conf, "video_font_enable", g_settings.video.font_enable);

View File

@ -611,7 +611,6 @@ const rarch_setting_t* setting_data_get_list(void)
CONFIG_PATH(g_settings.video.font_path, "video_font_path", "Font Path", DEFAULT_ME_YO) WITH_FLAGS(SD_FLAG_ALLOW_EMPTY)
CONFIG_FLOAT(g_settings.video.font_size, "video_font_size", "OSD Font Size", font_size)
CONFIG_BOOL(g_settings.video.font_enable, "video_font_enable", "OSD Font Enable", font_enable)
CONFIG_BOOL(g_settings.video.font_scale, "video_font_scale", "OSD Font Scale To Window", font_scale)
CONFIG_FLOAT(g_settings.video.msg_pos_x, "video_message_pos_x", "Message X Position", message_pos_offset_x)
CONFIG_FLOAT(g_settings.video.msg_pos_y, "video_message_pos_y", "Message Y Position", message_pos_offset_y)
/* message color */

View File

@ -908,7 +908,7 @@ static bool d3d_frame(void *data, const void *frame,
float msg_width = (g_extern.lifecycle_state & (1ULL << MODE_MENU_HD)) ? 160 : 100;
float msg_height = 90;
#endif
font_params_t font_parms = {0};
struct font_params font_parms = {0};
font_parms.x = msg_width;
font_parms.y = msg_height;
font_parms.scale = 21;
@ -1024,10 +1024,9 @@ static void d3d_set_texture_enable(void *data, bool state, bool full_screen)
}
#endif
static void d3d_set_osd_msg(void *data, const char *msg, void *userdata)
static void d3d_set_osd_msg(void *data, const char *msg, const struct font_params *params)
{
d3d_video_t *d3d = (d3d_video_t*)data;
font_params_t *params = (font_params_t*)userdata;
if (d3d->font_ctx && d3d->font_ctx->render_msg)
d3d->font_ctx->render_msg(d3d, msg, params);