(video_thread_wrapper) Use flags instead of bools

This commit is contained in:
LibretroAdmin 2022-10-28 19:20:40 +02:00
parent 2e73b87328
commit 2ec2f4719f
2 changed files with 141 additions and 95 deletions

View File

@ -96,41 +96,45 @@ static void video_thread_send_and_wait_user_to_thread(thread_video_t *thr, threa
static void thread_update_driver_state(thread_video_t *thr) static void thread_update_driver_state(thread_video_t *thr)
{ {
#ifdef HAVE_MENU #ifdef HAVE_MENU
if (thr->texture.frame_updated) if (thr->flags & THR_FLAG_TEXTURE_FRAME_UPDATED)
{ {
if (thr->driver_data && thr->poke && thr->poke->set_texture_frame) if (thr->driver_data && thr->poke && thr->poke->set_texture_frame)
thr->poke->set_texture_frame(thr->driver_data, thr->poke->set_texture_frame(thr->driver_data,
thr->texture.frame, thr->texture.rgb32, thr->texture.frame,
thr->texture.width, thr->texture.height, thr->flags & THR_FLAG_TEXTURE_RGB32,
thr->texture.width,
thr->texture.height,
thr->texture.alpha); thr->texture.alpha);
thr->texture.frame_updated = false; thr->flags &= ~THR_FLAG_TEXTURE_FRAME_UPDATED;
} }
if (thr->driver_data && thr->poke && thr->poke->set_texture_enable) if (thr->driver_data && thr->poke && thr->poke->set_texture_enable)
thr->poke->set_texture_enable(thr->driver_data, thr->poke->set_texture_enable(
thr->texture.enable, thr->texture.full_screen); thr->driver_data,
thr->flags & THR_FLAG_TEXTURE_ENABLE,
thr->flags & THR_FLAG_TEXTURE_FULLSCREEN);
#endif #endif
#ifdef HAVE_OVERLAY #ifdef HAVE_OVERLAY
slock_lock(thr->alpha_lock); slock_lock(thr->alpha_lock);
if (thr->alpha_update) if (thr->flags & THR_FLAG_ALPHA_UPDATE)
{ {
if (thr->driver_data && thr->overlay && thr->overlay->set_alpha) if (thr->driver_data && thr->overlay && thr->overlay->set_alpha)
{ {
unsigned i; int i;
for (i = 0; i < thr->alpha_mods; i++) for (i = 0; i < thr->alpha_mods; i++)
thr->overlay->set_alpha(thr->driver_data, i, thr->alpha_mod[i]); thr->overlay->set_alpha(thr->driver_data, i, thr->alpha_mod[i]);
} }
thr->alpha_update = false; thr->flags &= ~THR_FLAG_ALPHA_UPDATE;
} }
slock_unlock(thr->alpha_lock); slock_unlock(thr->alpha_lock);
#endif #endif
if (thr->apply_state_changes) if (thr->flags & THR_FLAG_APPLY_STATE_CHANGES)
{ {
if (thr->driver_data && thr->poke && thr->poke->apply_state_changes) if (thr->driver_data && thr->poke && thr->poke->apply_state_changes)
thr->poke->apply_state_changes(thr->driver_data); thr->poke->apply_state_changes(thr->driver_data);
thr->apply_state_changes = false; thr->flags &= ~THR_FLAG_APPLY_STATE_CHANGES;
} }
} }
@ -152,9 +156,7 @@ static bool video_thread_handle_packet(
thr->driver->viewport_info(thr->driver_data, &thr->vp); thr->driver->viewport_info(thr->driver_data, &thr->vp);
} }
else else
{
thr->driver_data = NULL; thr->driver_data = NULL;
}
pkt.data.b = (thr->driver_data != NULL); pkt.data.b = (thr->driver_data != NULL);
video_thread_reply(thr, &pkt); video_thread_reply(thr, &pkt);
break; break;
@ -176,7 +178,14 @@ static bool video_thread_handle_packet(
if (thr->driver_data && thr->driver && if (thr->driver_data && thr->driver &&
thr->driver->viewport_info && thr->driver->read_viewport) thr->driver->viewport_info && thr->driver->read_viewport)
{ {
struct video_viewport vp = {0}; struct video_viewport vp;
vp.x = 0;
vp.y = 0;
vp.width = 0;
vp.height = 0;
vp.full_width = 0;
vp.full_height = 0;
thr->driver->viewport_info(thr->driver_data, &vp); thr->driver->viewport_info(thr->driver_data, &vp);
if (!memcmp(&vp, &thr->read_vp, sizeof(vp))) if (!memcmp(&vp, &thr->read_vp, sizeof(vp)))
@ -193,10 +202,12 @@ static bool video_thread_handle_packet(
* *
* To avoid this, set a flag so wrapper can see if * To avoid this, set a flag so wrapper can see if
* it's called in this "special" way. */ * it's called in this "special" way. */
thr->frame.within_thread = true; thr->flags |= THR_FLAG_FRAME_WITHIN_THREAD;
pkt.data.b = thr->driver->read_viewport(thr->driver_data, pkt.data.b = thr->driver->read_viewport(
(uint8_t*)pkt.data.v, thr->is_idle); thr->driver_data,
thr->frame.within_thread = false; (uint8_t*)pkt.data.v,
thr->flags & THR_FLAG_IS_IDLE);
thr->flags &= ~THR_FLAG_FRAME_WITHIN_THREAD;
} }
else else
{ {
@ -206,9 +217,7 @@ static bool video_thread_handle_packet(
} }
} }
else else
{
pkt.data.b = false; pkt.data.b = false;
}
video_thread_reply(thr, &pkt); video_thread_reply(thr, &pkt);
break; break;
@ -253,7 +262,7 @@ static bool video_thread_handle_packet(
if (tmp_alpha_mod) if (tmp_alpha_mod)
{ {
/* Avoid temporary garbage data. */ /* Avoid temporary garbage data. */
unsigned i; int i;
for (i = 0; i < tmp_alpha_mods; i++) for (i = 0; i < tmp_alpha_mods; i++)
tmp_alpha_mod[i] = 1.0f; tmp_alpha_mod[i] = 1.0f;
thr->alpha_mods = tmp_alpha_mods; thr->alpha_mods = tmp_alpha_mods;
@ -415,10 +424,11 @@ static void video_thread_loop(void *data)
for (;;) for (;;)
{ {
slock_lock(thr->lock); slock_lock(thr->lock);
while (thr->send_cmd == CMD_VIDEO_NONE && !thr->frame.updated) while ( thr->send_cmd == CMD_VIDEO_NONE
&& (!(thr->flags & THR_FLAG_FRAME_UPDATED)))
scond_wait(thr->cond_thread, thr->lock); scond_wait(thr->cond_thread, thr->lock);
updated = thr->frame.updated; updated = ((thr->flags & THR_FLAG_FRAME_UPDATED) > 0);
/* To avoid race condition where send_cmd is updated /* To avoid race condition where send_cmd is updated
* right after the switch is checked. */ * right after the switch is checked. */
@ -431,10 +441,17 @@ static void video_thread_loop(void *data)
if (updated) if (updated)
{ {
struct video_viewport vp;
bool alive = false; bool alive = false;
bool focus = false; bool focus = false;
bool has_windowed = false; bool has_windowed = false;
struct video_viewport vp = {0};
vp.x = 0;
vp.y = 0;
vp.width = 0;
vp.height = 0;
vp.full_width = 0;
vp.full_height = 0;
slock_lock(thr->frame.lock); slock_lock(thr->frame.lock);
@ -479,11 +496,20 @@ static void video_thread_loop(void *data)
slock_unlock(thr->frame.lock); slock_unlock(thr->frame.lock);
slock_lock(thr->lock); slock_lock(thr->lock);
thr->alive = alive;
thr->focus = focus;
thr->has_windowed = has_windowed;
thr->vp = vp; thr->vp = vp;
thr->frame.updated = false; if (alive)
thr->flags |= THR_FLAG_ALIVE;
else
thr->flags &= ~THR_FLAG_ALIVE;
if (focus)
thr->flags |= THR_FLAG_FOCUS;
else
thr->flags &= ~THR_FLAG_FOCUS;
if (has_windowed)
thr->flags |= THR_FLAG_HAS_WINDOWED;
else
thr->flags &= ~THR_FLAG_HAS_WINDOWED;
thr->flags &= ~THR_FLAG_FRAME_UPDATED;
scond_signal(thr->cond_cmd); scond_signal(thr->cond_cmd);
slock_unlock(thr->lock); slock_unlock(thr->lock);
} }
@ -492,8 +518,8 @@ static void video_thread_loop(void *data)
static bool video_thread_alive(void *data) static bool video_thread_alive(void *data)
{ {
bool ret;
uint32_t runloop_flags; uint32_t runloop_flags;
bool ret = false;
thread_video_t *thr = (thread_video_t*)data; thread_video_t *thr = (thread_video_t*)data;
if (!thr) if (!thr)
@ -512,7 +538,8 @@ static bool video_thread_alive(void *data)
} }
slock_lock(thr->lock); slock_lock(thr->lock);
ret = thr->alive; if (thr->flags & THR_FLAG_ALIVE)
ret = true;
slock_unlock(thr->lock); slock_unlock(thr->lock);
return ret; return ret;
@ -520,14 +547,15 @@ static bool video_thread_alive(void *data)
static bool video_thread_focus(void *data) static bool video_thread_focus(void *data)
{ {
bool ret; bool ret = false;
thread_video_t *thr = (thread_video_t*)data; thread_video_t *thr = (thread_video_t*)data;
if (!thr) if (!thr)
return false; return false;
slock_lock(thr->lock); slock_lock(thr->lock);
ret = thr->focus; if (thr->flags & THR_FLAG_FOCUS)
ret = true;
slock_unlock(thr->lock); slock_unlock(thr->lock);
return ret; return ret;
@ -535,14 +563,15 @@ static bool video_thread_focus(void *data)
static bool video_thread_suppress_screensaver(void *data, bool enable) static bool video_thread_suppress_screensaver(void *data, bool enable)
{ {
bool ret; bool ret = false;
thread_video_t *thr = (thread_video_t*)data; thread_video_t *thr = (thread_video_t*)data;
if (!thr) if (!thr)
return false; return false;
slock_lock(thr->lock); slock_lock(thr->lock);
ret = thr->suppress_screensaver; if (thr->flags & THR_FLAG_SUPPRESS_SCREENSAVER)
ret = true;
slock_unlock(thr->lock); slock_unlock(thr->lock);
return ret; return ret;
@ -550,14 +579,15 @@ static bool video_thread_suppress_screensaver(void *data, bool enable)
static bool video_thread_has_windowed(void *data) static bool video_thread_has_windowed(void *data)
{ {
bool ret; bool ret = false;
thread_video_t *thr = (thread_video_t*)data; thread_video_t *thr = (thread_video_t*)data;
if (!thr) if (!thr)
return false; return false;
slock_lock(thr->lock); slock_lock(thr->lock);
ret = thr->has_windowed; if (thr->flags & THR_FLAG_HAS_WINDOWED)
ret = true;
slock_unlock(thr->lock); slock_unlock(thr->lock);
return ret; return ret;
@ -574,7 +604,7 @@ static bool video_thread_frame(void *data, const void *frame_,
/* If called from within read_viewport, we're actually in the /* If called from within read_viewport, we're actually in the
* driver thread, so just render directly. */ * driver thread, so just render directly. */
if (thr->frame.within_thread) if (thr->flags & THR_FLAG_FRAME_WITHIN_THREAD)
{ {
thread_update_driver_state(thr); thread_update_driver_state(thr);
@ -587,14 +617,14 @@ static bool video_thread_frame(void *data, const void *frame_,
slock_lock(thr->lock); slock_lock(thr->lock);
if (!thr->nonblock) if ((!(thr->flags & THR_FLAG_NONBLOCK)))
{ {
retro_time_t target_frame_time = retro_time_t target_frame_time =
(retro_time_t)roundf(1000000 / video_info->refresh_rate); (retro_time_t)roundf(1000000 / video_info->refresh_rate);
retro_time_t target = thr->last_time + target_frame_time; retro_time_t target = thr->last_time + target_frame_time;
/* Ideally, use absolute time, but that is only a good idea on POSIX. */ /* Ideally, use absolute time, but that is only a good idea on POSIX. */
while (thr->frame.updated) while (thr->flags & THR_FLAG_FRAME_UPDATED)
{ {
retro_time_t current = cpu_features_get_time_usec(); retro_time_t current = cpu_features_get_time_usec();
retro_time_t delta = target - current; retro_time_t delta = target - current;
@ -609,7 +639,7 @@ static bool video_thread_frame(void *data, const void *frame_,
/* Drop frame if updated flag is still set, as thread is /* Drop frame if updated flag is still set, as thread is
* still working on last frame. */ * still working on last frame. */
if (!thr->frame.updated) if (!(thr->flags & THR_FLAG_FRAME_UPDATED))
{ {
const uint8_t *src = (const uint8_t*)frame_; const uint8_t *src = (const uint8_t*)frame_;
uint8_t *dst = thr->frame.buffer; uint8_t *dst = thr->frame.buffer;
@ -618,16 +648,16 @@ static bool video_thread_frame(void *data, const void *frame_,
if (src) if (src)
{ {
unsigned h; int i; /* TODO/FIXME - increment counter never meaningfully used */
for (h = 0; h < height; h++, src += pitch, dst += copy_stride) for (i = 0; i < height; i++, src += pitch, dst += copy_stride)
memcpy(dst, src, copy_stride); memcpy(dst, src, copy_stride);
} }
thr->frame.updated = true;
thr->frame.width = width; thr->frame.width = width;
thr->frame.height = height; thr->frame.height = height;
thr->frame.count = frame_count; thr->frame.count = frame_count;
thr->frame.pitch = copy_stride; thr->frame.pitch = copy_stride;
thr->flags |= THR_FLAG_FRAME_UPDATED;
if (msg) if (msg)
strlcpy(thr->frame.msg, msg, sizeof(thr->frame.msg)); strlcpy(thr->frame.msg, msg, sizeof(thr->frame.msg));
@ -637,12 +667,12 @@ static bool video_thread_frame(void *data, const void *frame_,
scond_signal(thr->cond_thread); scond_signal(thr->cond_thread);
#ifdef HAVE_MENU #ifdef HAVE_MENU
if (thr->texture.enable) if (thr->flags & THR_FLAG_TEXTURE_ENABLE)
{ {
do do
{ {
scond_wait(thr->cond_cmd, thr->lock); scond_wait(thr->cond_cmd, thr->lock);
} while (thr->frame.updated); } while (thr->flags & THR_FLAG_FRAME_UPDATED);
} }
#endif #endif
thr->hit_count++; thr->hit_count++;
@ -664,7 +694,12 @@ static void video_thread_set_nonblock_state(void *data, bool state,
thread_video_t *thr = (thread_video_t*)data; thread_video_t *thr = (thread_video_t*)data;
if (thr) if (thr)
thr->nonblock = state; {
if (state)
thr->flags |= THR_FLAG_NONBLOCK;
else
thr->flags &= ~THR_FLAG_NONBLOCK;
}
} }
static bool video_thread_init(thread_video_t *thr, static bool video_thread_init(thread_video_t *thr,
@ -673,20 +708,15 @@ static bool video_thread_init(thread_video_t *thr,
{ {
thread_packet_t pkt; thread_packet_t pkt;
thr->lock = slock_new(); if (!(thr->lock = slock_new()))
if (!thr->lock)
return false; return false;
thr->alpha_lock = slock_new(); if (!(thr->alpha_lock = slock_new()))
if (!thr->alpha_lock)
return false; return false;
thr->frame.lock = slock_new(); if (!(thr->frame.lock = slock_new()))
if (!thr->frame.lock)
return false; return false;
thr->cond_cmd = scond_new(); if (!(thr->cond_cmd = scond_new()))
if (!thr->cond_cmd)
return false; return false;
thr->cond_thread = scond_new(); if (!(thr->cond_thread = scond_new()))
if (!thr->cond_thread)
return false; return false;
{ {
@ -709,14 +739,13 @@ static bool video_thread_init(thread_video_t *thr,
thr->input = input; thr->input = input;
thr->input_data = input_data; thr->input_data = input_data;
thr->info = info; thr->info = info;
thr->alive = true;
thr->focus = true;
thr->has_windowed = true;
thr->suppress_screensaver = true;
thr->last_time = cpu_features_get_time_usec(); thr->last_time = cpu_features_get_time_usec();
thr->flags |= THR_FLAG_ALIVE
| THR_FLAG_FOCUS
| THR_FLAG_HAS_WINDOWED
| THR_FLAG_SUPPRESS_SCREENSAVER;
thr->thread = sthread_create(video_thread_loop, thr); if (!(thr->thread = sthread_create(video_thread_loop, thr)))
if (!thr->thread)
return false; return false;
pkt.type = CMD_INIT; pkt.type = CMD_INIT;
@ -805,7 +834,10 @@ static bool video_thread_read_viewport(void *data,
pkt.type = CMD_READ_VIEWPORT; pkt.type = CMD_READ_VIEWPORT;
pkt.data.v = buffer; pkt.data.v = buffer;
thr->is_idle = is_idle; if (is_idle)
thr->flags |= THR_FLAG_IS_IDLE;
else
thr->flags &= ~THR_FLAG_IS_IDLE;
video_thread_send_and_wait_user_to_thread(thr, &pkt); video_thread_send_and_wait_user_to_thread(thr, &pkt);
@ -951,7 +983,7 @@ static void thread_overlay_set_alpha(void *data, unsigned idx, float mod)
{ {
slock_lock(thr->alpha_lock); slock_lock(thr->alpha_lock);
thr->alpha_mod[idx] = mod; thr->alpha_mod[idx] = mod;
thr->alpha_update = true; thr->flags |= THR_FLAG_ALPHA_UPDATE;
slock_unlock(thr->alpha_lock); slock_unlock(thr->alpha_lock);
} }
} }
@ -1138,25 +1170,34 @@ static void thread_set_texture_frame(void *data, const void *frame,
memcpy(thr->texture.frame, frame, required); memcpy(thr->texture.frame, frame, required);
thr->texture.rgb32 = rgb32;
thr->texture.width = width; thr->texture.width = width;
thr->texture.height = height; thr->texture.height = height;
thr->texture.alpha = alpha; thr->texture.alpha = alpha;
thr->texture.frame_updated = true; if (rgb32)
thr->flags |= THR_FLAG_TEXTURE_RGB32;
else
thr->flags &= ~THR_FLAG_TEXTURE_RGB32;
thr->flags |= THR_FLAG_TEXTURE_FRAME_UPDATED;
end: end:
slock_unlock(thr->frame.lock); slock_unlock(thr->frame.lock);
} }
static void thread_set_texture_enable(void *data, bool state, bool full_screen) static void thread_set_texture_enable(void *data, bool state, bool fullscreen)
{ {
thread_video_t *thr = (thread_video_t*)data; thread_video_t *thr = (thread_video_t*)data;
if (thr) if (thr)
{ {
slock_lock(thr->frame.lock); slock_lock(thr->frame.lock);
thr->texture.enable = state; if (state)
thr->texture.full_screen = full_screen; thr->flags |= THR_FLAG_TEXTURE_ENABLE;
else
thr->flags &= ~THR_FLAG_TEXTURE_ENABLE;
if (fullscreen)
thr->flags |= THR_FLAG_TEXTURE_FULLSCREEN;
else
thr->flags &= ~THR_FLAG_TEXTURE_FULLSCREEN;
slock_unlock(thr->frame.lock); slock_unlock(thr->frame.lock);
} }
} }
@ -1227,7 +1268,7 @@ static void thread_apply_state_changes(void *data)
if (thr) if (thr)
{ {
slock_lock(thr->frame.lock); slock_lock(thr->frame.lock);
thr->apply_state_changes = true; thr->flags |= THR_FLAG_APPLY_STATE_CHANGES;
slock_unlock(thr->frame.lock); slock_unlock(thr->frame.lock);
} }
} }
@ -1302,8 +1343,10 @@ static bool video_thread_wrapper_gfx_widgets_enabled(void *data)
{ {
thread_video_t *thr = (thread_video_t*)data; thread_video_t *thr = (thread_video_t*)data;
if (thr && thr->driver_data && if ( thr
thr->driver && thr->driver->gfx_widgets_enabled) && thr->driver_data
&& thr->driver
&& thr->driver->gfx_widgets_enabled)
return thr->driver->gfx_widgets_enabled(thr->driver_data); return thr->driver->gfx_widgets_enabled(thr->driver_data);
return false; return false;

View File

@ -166,6 +166,24 @@ typedef struct thread_packet
enum thread_cmd type; enum thread_cmd type;
} thread_packet_t; } thread_packet_t;
enum thread_video_flags
{
THR_FLAG_APPLY_STATE_CHANGES = (1 << 0),
THR_FLAG_ALIVE = (1 << 1),
THR_FLAG_FOCUS = (1 << 2),
THR_FLAG_SUPPRESS_SCREENSAVER = (1 << 3),
THR_FLAG_HAS_WINDOWED = (1 << 4),
THR_FLAG_NONBLOCK = (1 << 5),
THR_FLAG_IS_IDLE = (1 << 6),
THR_FLAG_ALPHA_UPDATE = (1 << 7),
THR_FLAG_FRAME_WITHIN_THREAD = (1 << 8),
THR_FLAG_FRAME_UPDATED = (1 << 9),
THR_FLAG_TEXTURE_FRAME_UPDATED = (1 << 10),
THR_FLAG_TEXTURE_RGB32 = (1 << 11),
THR_FLAG_TEXTURE_ENABLE = (1 << 12),
THR_FLAG_TEXTURE_FULLSCREEN = (1 << 13)
};
typedef struct thread_video typedef struct thread_video
{ {
retro_time_t last_time; retro_time_t last_time;
@ -197,10 +215,6 @@ typedef struct thread_video
unsigned width; unsigned width;
unsigned height; unsigned height;
float alpha; float alpha;
bool frame_updated;
bool rgb32;
bool enable;
bool full_screen;
} texture; } texture;
unsigned hit_count; unsigned hit_count;
@ -216,7 +230,7 @@ typedef struct thread_video
enum thread_cmd send_cmd; enum thread_cmd send_cmd;
enum thread_cmd reply_cmd; enum thread_cmd reply_cmd;
bool alpha_update; uint16_t flags;
struct struct
{ {
@ -227,18 +241,7 @@ typedef struct thread_video
unsigned height; unsigned height;
unsigned pitch; unsigned pitch;
char msg[NAME_MAX_LENGTH]; char msg[NAME_MAX_LENGTH];
bool updated;
bool within_thread;
} frame; } frame;
bool apply_state_changes;
bool alive;
bool focus;
bool suppress_screensaver;
bool has_windowed;
bool nonblock;
bool is_idle;
} thread_video_t; } thread_video_t;
/** /**