RetroArch/gfx/drivers_font/gl_raster_font.c

563 lines
16 KiB
C
Raw Normal View History

/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
2017-01-22 12:40:32 +00:00
* Copyright (C) 2011-2017 - Daniel De Matteis
* Copyright (C) 2016-2017 - Brad Parker
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
2016-09-01 03:46:10 +00:00
#include <stdlib.h>
#include <encodings/utf.h>
2016-09-30 06:58:57 +00:00
#include <string/stdstring.h>
2017-06-28 02:41:38 +00:00
#include <retro_math.h>
2016-09-01 03:46:10 +00:00
2015-11-17 07:01:33 +00:00
#include "../common/gl_common.h"
#include "../font_driver.h"
#include "../video_driver.h"
2016-09-01 03:46:10 +00:00
2015-03-26 17:07:39 +00:00
/* TODO: Move viewport side effects to the caller: it's a source of bugs. */
2015-04-03 01:27:34 +00:00
#define gl_raster_font_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]; \
2015-05-19 18:11:05 +00:00
font_lut_tex_coord[ 2 * (6 * i + c) + 0] = gl->coords.lut_tex_coord[0]; \
font_lut_tex_coord[ 2 * (6 * i + c) + 1] = gl->coords.lut_tex_coord[1]; \
} while(0)
#define MAX_MSG_LEN_CHUNK 64
typedef struct
{
gl_t *gl;
GLuint tex;
unsigned tex_width, tex_height;
const font_renderer_driver_t *font_driver;
void *font_data;
struct font_atlas *atlas;
2016-05-10 00:42:02 +00:00
video_font_raster_block_t *block;
} gl_raster_t;
2017-04-25 11:25:37 +00:00
static void gl_raster_font_free_font(void *data,
bool is_threaded)
{
gl_raster_t *font = (gl_raster_t*)data;
if (!font)
return;
if (font->font_driver && font->font_data)
font->font_driver->free(font->font_data);
if (is_threaded)
video_context_driver_make_current(true);
glDeleteTextures(1, &font->tex);
free(font);
}
2017-06-08 06:10:23 +00:00
#if 0
static bool gl_raster_font_upload_atlas_components_4(gl_raster_t *font)
{
unsigned i, j;
2017-06-08 06:10:23 +00:00
GLint gl_internal = GL_RGBA;
GLenum gl_format = GL_RGBA;
size_t ncomponents = 4;
uint8_t *tmp = NULL;
2017-06-08 06:10:23 +00:00
tmp = (uint8_t*)calloc(font->tex_height, font->tex_width * ncomponents);
2017-06-08 06:10:23 +00:00
for (i = 0; i < font->atlas->height; ++i)
{
2017-06-08 06:10:23 +00:00
const uint8_t *src = &font->atlas->buffer[i * font->atlas->width];
uint8_t *dst = &tmp[i * font->tex_width * ncomponents];
for (j = 0; j < font->atlas->width; ++j)
{
*dst++ = 0xff;
*dst++ = 0xff;
*dst++ = 0xff;
*dst++ = *src++;
}
break;
}
2017-06-08 06:10:23 +00:00
glTexImage2D(GL_TEXTURE_2D, 0, gl_internal, font->tex_width, font->tex_height,
0, gl_format, GL_UNSIGNED_BYTE, tmp);
free(tmp);
return true;
}
#endif
2017-06-08 06:10:23 +00:00
static bool gl_raster_font_upload_atlas(gl_raster_t *font)
{
unsigned i, j;
GLint gl_internal = GL_LUMINANCE_ALPHA;
GLenum gl_format = GL_LUMINANCE_ALPHA;
size_t ncomponents = 2;
uint8_t *tmp = NULL;
2016-05-02 15:08:40 +00:00
#if defined(GL_VERSION_3_0)
2017-06-08 06:10:23 +00:00
struct retro_hw_render_callback *hwr = video_driver_get_hw_context();
2016-05-16 03:55:42 +00:00
if (gl_query_core_context_in_use() ||
2016-05-02 15:08:40 +00:00
(hwr->context_type == RETRO_HW_CONTEXT_OPENGL &&
hwr->version_major >= 3))
{
GLint swizzle[] = { GL_ONE, GL_ONE, GL_ONE, GL_RED };
glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_RGBA, swizzle);
gl_internal = GL_R8;
gl_format = GL_RED;
ncomponents = 1;
}
#endif
tmp = (uint8_t*)calloc(font->tex_height, font->tex_width * ncomponents);
2017-06-08 06:10:23 +00:00
switch (ncomponents)
{
2017-06-08 06:10:23 +00:00
case 1:
for (i = 0; i < font->atlas->height; ++i)
{
const uint8_t *src = &font->atlas->buffer[i * font->atlas->width];
uint8_t *dst = &tmp[i * font->tex_width * ncomponents];
memcpy(dst, src, font->atlas->width);
2017-06-08 06:10:23 +00:00
}
break;
case 2:
for (i = 0; i < font->atlas->height; ++i)
{
const uint8_t *src = &font->atlas->buffer[i * font->atlas->width];
uint8_t *dst = &tmp[i * font->tex_width * ncomponents];
for (j = 0; j < font->atlas->width; ++j)
{
*dst++ = 0xff;
*dst++ = *src++;
}
2017-06-08 06:10:23 +00:00
}
break;
}
glTexImage2D(GL_TEXTURE_2D, 0, gl_internal, font->tex_width, font->tex_height,
0, gl_format, GL_UNSIGNED_BYTE, tmp);
free(tmp);
return true;
}
2015-04-21 15:13:55 +00:00
static void *gl_raster_font_init_font(void *data,
const char *font_path, float font_size,
bool is_threaded)
{
2016-10-21 17:07:28 +00:00
gl_raster_t *font = (gl_raster_t*)calloc(1, sizeof(*font));
if (!font)
return NULL;
2015-04-21 15:13:55 +00:00
font->gl = (gl_t*)data;
if (!font_renderer_create_default((const void**)&font->font_driver,
&font->font_data, font_path, font_size))
{
RARCH_WARN("Couldn't initialize font renderer.\n");
free(font);
return NULL;
}
if (is_threaded)
video_context_driver_make_current(false);
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);
2017-06-08 04:44:04 +00:00
font->atlas = font->font_driver->get_atlas(font->font_data);
font->tex_width = next_pow2(font->atlas->width);
font->tex_height = next_pow2(font->atlas->height);
if (!gl_raster_font_upload_atlas(font))
2015-09-28 14:20:26 +00:00
goto error;
font->atlas->dirty = false;
glBindTexture(GL_TEXTURE_2D, font->gl->texture[font->gl->tex_index]);
2015-04-21 15:13:55 +00:00
return font;
2015-09-28 14:20:26 +00:00
error:
gl_raster_font_free_font(font, is_threaded);
font = NULL;
2015-09-28 14:20:26 +00:00
return NULL;
}
2016-01-23 04:10:47 +00:00
static int gl_get_message_width(void *data, const char *msg,
unsigned msg_len, float scale)
2015-01-23 00:54:17 +00:00
{
gl_raster_t *font = (gl_raster_t*)data;
const char* msg_end = msg + msg_len;
int delta_x = 0;
2017-01-10 20:43:45 +00:00
if ( !font
|| !font->font_driver
|| !font->font_driver->get_glyph
|| !font->font_data )
2015-06-26 15:46:13 +00:00
return 0;
while (msg < msg_end)
2015-01-23 00:54:17 +00:00
{
unsigned code = utf8_walk(&msg);
2017-01-10 20:43:45 +00:00
const struct font_glyph *glyph = font->font_driver->get_glyph(
font->font_data, code);
2015-01-23 00:54:17 +00:00
if (!glyph) /* Do something smarter here ... */
glyph = font->font_driver->get_glyph(font->font_data, '?');
if (!glyph)
continue;
2015-01-23 00:54:17 +00:00
delta_x += glyph->advance_x;
2015-01-23 00:54:17 +00:00
}
2015-05-14 16:16:34 +00:00
return delta_x * scale;
2015-01-23 00:54:17 +00:00
}
static void gl_raster_font_draw_vertices(gl_raster_t *font, const video_coords_t *coords)
{
2016-02-14 18:43:47 +00:00
video_shader_ctx_mvp_t mvp;
2016-02-14 17:59:42 +00:00
video_shader_ctx_coords_t coords_data;
if (font->atlas->dirty)
{
gl_raster_font_upload_atlas(font);
2017-04-25 11:25:37 +00:00
font->atlas->dirty = false;
}
2016-02-14 17:59:42 +00:00
coords_data.handle_data = NULL;
coords_data.data = coords;
video_shader_driver_set_coords(coords_data);
2016-02-14 18:43:47 +00:00
2017-01-10 20:43:45 +00:00
mvp.data = font->gl;
mvp.matrix = &font->gl->mvp_no_rot;
2016-02-14 18:43:47 +00:00
video_shader_driver_set_mvp(mvp);
glDrawArrays(GL_TRIANGLES, 0, coords->vertices);
}
static void gl_raster_font_render_line(
gl_raster_t *font, const char *msg, unsigned msg_len,
GLfloat scale, const GLfloat color[4], GLfloat pos_x,
GLfloat pos_y, unsigned text_align)
{
unsigned i;
2016-05-10 00:32:49 +00:00
struct video_coords coords;
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];
GLfloat font_lut_tex_coord[2 * 6 * MAX_MSG_LEN_CHUNK];
2017-04-25 11:22:55 +00:00
gl_t *gl = font->gl;
const char* msg_end = msg + msg_len;
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;
switch (text_align)
{
case TEXT_ALIGN_RIGHT:
x -= gl_get_message_width(font, msg, msg_len, scale);
break;
case TEXT_ALIGN_CENTER:
x -= gl_get_message_width(font, msg, msg_len, scale) / 2.0;
break;
}
2015-01-23 00:54:17 +00:00
while (msg < msg_end)
{
i = 0;
while ((i < MAX_MSG_LEN_CHUNK) && (msg < msg_end))
{
int off_x, off_y, tex_x, tex_y, width, height;
2017-01-10 20:43:45 +00:00
unsigned code = utf8_walk(&msg);
const struct font_glyph *glyph = font->font_driver->get_glyph(
font->font_data, code);
2015-04-21 15:13:55 +00:00
if (!glyph) /* Do something smarter here ... */
glyph = font->font_driver->get_glyph(font->font_data, '?');
2017-01-10 20:43:45 +00:00
if (!glyph)
continue;
off_x = glyph->draw_offset_x;
off_y = glyph->draw_offset_y;
tex_x = glyph->atlas_offset_x;
tex_y = glyph->atlas_offset_y;
width = glyph->width;
height = glyph->height;
2015-04-03 01:27:34 +00:00
gl_raster_font_emit(0, 0, 1); /* Bottom-left */
gl_raster_font_emit(1, 1, 1); /* Bottom-right */
gl_raster_font_emit(2, 0, 0); /* Top-left */
2015-04-03 01:27:34 +00:00
gl_raster_font_emit(3, 1, 0); /* Top-right */
gl_raster_font_emit(4, 0, 0); /* Top-left */
gl_raster_font_emit(5, 1, 1); /* Bottom-right */
i++;
delta_x += glyph->advance_x;
delta_y -= glyph->advance_y;
}
2015-04-21 15:13:55 +00:00
coords.tex_coord = font_tex_coords;
coords.vertex = font_vertex;
coords.color = font_color;
coords.vertices = i * 6;
coords.lut_tex_coord = font_lut_tex_coord;
if (font->block)
2016-05-10 00:32:49 +00:00
video_coord_array_append(&font->block->carr, &coords, coords.vertices);
else
gl_raster_font_draw_vertices(font, &coords);
}
}
static void gl_raster_font_render_message(
gl_raster_t *font, const char *msg, GLfloat scale,
const GLfloat color[4], GLfloat pos_x, GLfloat pos_y,
unsigned text_align)
{
2015-06-26 15:46:13 +00:00
float line_height;
2017-01-10 20:37:45 +00:00
int lines = 0;
2015-06-26 15:46:13 +00:00
/* If the font height is not supported just draw as usual */
if (!font->font_driver->get_line_height)
{
2017-04-29 17:21:24 +00:00
gl_raster_font_render_line(font,
msg, (unsigned)strlen(msg), scale, color, pos_x,
pos_y, text_align);
return;
}
line_height = (float) font->font_driver->get_line_height(font->font_data) *
scale / font->gl->vp.height;
for (;;)
{
const char *delim = strchr(msg, '\n');
2017-04-25 11:25:37 +00:00
unsigned msg_len = delim
? (unsigned)(delim - msg) : (unsigned)strlen(msg);
2015-06-26 15:46:13 +00:00
/* Draw the line */
2017-04-29 17:21:24 +00:00
gl_raster_font_render_line(font,
msg, msg_len, scale, color, pos_x,
pos_y - (float)lines*line_height, text_align);
2017-01-10 20:37:45 +00:00
if (!delim)
break;
2017-01-10 20:37:45 +00:00
msg += msg_len + 1;
lines++;
}
}
static void gl_raster_font_setup_viewport(unsigned width, unsigned height,
gl_raster_t *font, bool full_screen)
{
2017-01-10 20:37:45 +00:00
video_shader_ctx_info_t shader_info;
2015-04-21 15:13:55 +00:00
video_driver_set_viewport(width, height, full_screen, false);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBlendEquation(GL_FUNC_ADD);
glBindTexture(GL_TEXTURE_2D, font->tex);
2016-04-15 05:37:06 +00:00
shader_info.data = NULL;
shader_info.idx = VIDEO_SHADER_STOCK_BLEND;
2016-04-15 05:37:06 +00:00
shader_info.set_active = true;
2016-02-14 19:01:39 +00:00
video_shader_driver_use(shader_info);
}
static void gl_raster_font_render_msg(
video_frame_info_t *video_info,
void *data, const char *msg,
const void *userdata)
{
2017-04-25 11:27:40 +00:00
GLfloat color[4];
int drop_x, drop_y;
2017-04-25 11:27:40 +00:00
GLfloat x, y, scale, drop_mod, drop_alpha;
2017-01-10 20:43:45 +00:00
enum text_alignment text_align = TEXT_ALIGN_LEFT;
2017-01-10 20:37:45 +00:00
bool full_screen = false ;
gl_raster_t *font = (gl_raster_t*)data;
unsigned width = video_info->width;
unsigned height = video_info->height;
const struct font_params *params = (const struct font_params*)userdata;
2016-09-30 07:00:04 +00:00
if (!font || string_is_empty(msg))
return;
if (params)
{
2015-03-20 21:08:36 +00:00
x = params->x;
y = params->y;
scale = params->scale;
full_screen = params->full_screen;
text_align = params->text_align;
2015-03-20 21:08:36 +00:00
drop_x = params->drop_x;
drop_y = params->drop_y;
drop_mod = params->drop_mod;
2016-04-09 06:23:59 +00:00
drop_alpha = params->drop_alpha;
2015-03-20 21:08:36 +00:00
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 (color[3] <= 0.0f)
color[3] = 1.0f;
}
else
{
x = video_info->font_msg_pos_x;
y = video_info->font_msg_pos_y;
2017-01-10 20:37:45 +00:00
scale = 1.0f;
full_screen = true;
text_align = TEXT_ALIGN_LEFT;
color[0] = video_info->font_msg_color_r;
color[1] = video_info->font_msg_color_g;
color[2] = video_info->font_msg_color_b;
2017-01-10 20:37:45 +00:00
color[3] = 1.0f;
drop_x = -2;
drop_y = -2;
drop_mod = 0.3f;
drop_alpha = 1.0f;
}
2017-04-25 11:28:42 +00:00
if (font->block)
2015-03-27 14:20:10 +00:00
font->block->fullscreen = full_screen;
else
gl_raster_font_setup_viewport(width, height, font, full_screen);
2017-10-04 04:04:17 +00:00
if (!string_is_empty(msg) && font->gl
2017-04-25 11:28:42 +00:00
&& font->font_data && font->font_driver)
{
2017-04-25 11:27:40 +00:00
if (drop_x || drop_y)
{
GLfloat color_dark[4];
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] * drop_alpha;
2017-04-29 17:21:24 +00:00
if (font->gl)
gl_raster_font_render_message(font, msg, scale, color_dark,
x + scale * drop_x / font->gl->vp.width, y +
scale * drop_y / font->gl->vp.height, text_align);
2017-04-25 11:27:40 +00:00
}
2017-04-29 17:21:24 +00:00
if (font->gl)
gl_raster_font_render_message(font, msg, scale, color, x, y, text_align);
2017-04-25 11:27:40 +00:00
}
2017-04-29 17:22:44 +00:00
if (!font->block && font->gl)
2017-06-08 04:44:04 +00:00
{
/* restore viewport */
glBindTexture(GL_TEXTURE_2D, font->gl->texture[font->gl->tex_index]);
glDisable(GL_BLEND);
video_driver_set_viewport(width, height, false, true);
}
}
static const struct font_glyph *gl_raster_font_get_glyph(
void *data, uint32_t code)
{
gl_raster_t *font = (gl_raster_t*)data;
if (!font || !font->font_driver)
return NULL;
if (!font->font_driver->ident)
return NULL;
return font->font_driver->get_glyph((void*)font->font_driver, code);
}
static void gl_raster_font_flush_block(unsigned width, unsigned height,
void *data)
{
2017-01-10 20:37:45 +00:00
gl_raster_t *font = (gl_raster_t*)data;
2016-05-10 00:42:02 +00:00
video_font_raster_block_t *block = font ? font->block : NULL;
if (!font || !block || !block->carr.coords.vertices)
return;
gl_raster_font_setup_viewport(width, height, font, block->fullscreen);
gl_raster_font_draw_vertices(font, (video_coords_t*)&block->carr.coords);
2017-04-29 17:22:44 +00:00
if (font->gl)
2017-06-08 04:44:04 +00:00
{
/* restore viewport */
glBindTexture(GL_TEXTURE_2D, font->gl->texture[font->gl->tex_index]);
glDisable(GL_BLEND);
video_driver_set_viewport(width, height, block->fullscreen, true);
}
}
static void gl_raster_font_bind_block(void *data, void *userdata)
{
2017-01-10 20:37:45 +00:00
gl_raster_t *font = (gl_raster_t*)data;
2016-05-10 00:42:02 +00:00
video_font_raster_block_t *block = (video_font_raster_block_t*)userdata;
if (font)
font->block = block;
}
font_renderer_t gl_raster_font = {
gl_raster_font_init_font,
gl_raster_font_free_font,
gl_raster_font_render_msg,
"GL raster",
gl_raster_font_get_glyph,
gl_raster_font_bind_block,
gl_raster_font_flush_block,
gl_get_message_width
};