Files
RetroArch/gfx/video_thread_wrapper.h
LibretroAdmin d4330754f1 menu: hand the message box's text over with the frame
XMB, MaterialUI and Ozone copied the box's text into their handles on
the main thread every iteration, and their frame() read it there - on
the video thread under threaded video, with nothing ordering the write
against the read. Ozone also read the menu's own buffer from frame().

The frame info now carries the text: video_info->menu.msgbox points at
the menu's buffer, and the threaded wrapper gives a frame that has a
box its own copy, as it does the statistics text. Without the wrapper
the drivers draw the menu's text in place, with no copy at all. XMB
and MaterialUI no longer need a render_messagebox callback; Ozone's
only sets its fade flags, and its fade animations are tagged with the
alpha they drive.
2026-11-16 11:01:34 +01:00

1312 lines
54 KiB
C

/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
* Copyright (C) 2011-2017 - Daniel De Matteis
*
* 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/>.
*/
#ifndef RARCH_VIDEO_THREAD_H__
#define RARCH_VIDEO_THREAD_H__
#include <limits.h>
#include <boolean.h>
#include <retro_common_api.h>
#include <rthreads/rthreads.h>
#include <retro_atomic.h>
#include <retro_miscellaneous.h>
#include <rthreads/retro_eventcount.h>
#include <queues/mpsc_stack.h>
#include <queues/retro_triple_buffer.h>
#include "font_driver.h"
/* Everything here belongs to the wrapper, which is only built with
* HAVE_THREADS. Without it, the callers that must still compile -
* gl2/gl3 for the swap count, video_driver.c for the wrapper-active
* test - get macro stand-ins from video_driver.h, and a prototype here
* would only collide with those. */
#ifdef HAVE_THREADS
#include "video_thread_movie.h"
RETRO_BEGIN_DECLS
enum video_thread_win_flags
{
VIDEO_THREAD_WIN_ALIVE = (1 << 0),
VIDEO_THREAD_WIN_FOCUS = (1 << 1),
VIDEO_THREAD_WIN_PRESENTABLE = (1 << 2),
VIDEO_THREAD_WIN_HAS_WINDOWED = (1 << 3)
};
enum thread_cmd
{
CMD_VIDEO_NONE = 0,
CMD_INIT,
CMD_SET_SHADER,
CMD_SHADER_LOAD_BEGIN,
CMD_SHADER_LOAD_STEP,
CMD_FREE,
CMD_ALIVE, /* Blocking alive check. Used when paused. */
CMD_SET_VIEWPORT,
CMD_SET_ROTATION,
CMD_READ_VIEWPORT,
CMD_READ_VIEWPORT_HDR,
CMD_OVERLAY_ENABLE,
CMD_OVERLAY_LOAD,
CMD_OVERLAY_LOAD_TEXTURES,
CMD_OVERLAY_TEX_GEOM,
CMD_OVERLAY_VERTEX_GEOM,
CMD_OVERLAY_FULL_SCREEN,
CMD_POKE_SET_VIDEO_MODE,
CMD_POKE_SET_FILTERING,
CMD_POKE_SET_FBO_STATE,
CMD_POKE_GET_FBO_STATE,
CMD_FONT_INIT,
CMD_CUSTOM_COMMAND,
CMD_POKE_SHOW_MOUSE,
CMD_POKE_GRAB_MOUSE_TOGGLE,
CMD_POKE_SET_HDR_MENU_NITS,
CMD_POKE_SET_HDR_PAPER_WHITE_NITS,
CMD_POKE_SET_HDR_EXPAND_GAMUT,
CMD_POKE_SET_HDR_SCANLINES,
CMD_POKE_SET_HDR_SUBPIXEL_LAYOUT,
CMD_SET_NONBLOCK,
CMD_SUPPRESS_SCREENSAVER,
CMD_POKE_SET_VIEW_COUNT,
CMD_POKE_HW_CONTEXT_DESTROYING,
CMD_POKE_SET_FRAME_FORMAT,
CMD_POLL_PRESENTABLE, /* Re-asks the context, deferred: no frame needed. */
CMD_DUMMY = INT_MAX
};
typedef uintptr_t (*custom_command_method_t)(void*);
typedef bool (*custom_font_command_method_t)(const void **font_driver,
void **font_handle, void *video_data, const char *font_path,
float font_size, const font_renderer_t *backend,
bool is_threaded);
typedef struct thread_packet
{
union
{
struct
{
uint16_t *buffer;
struct rpng_hdr_metadata *metadata;
} hdr_viewport;
const char *str;
void *v;
int i;
float f;
bool b;
struct
{
enum rarch_shader_type type;
const char *path;
} set_shader;
struct
{
unsigned dims;
bool force_full;
bool allow_rotate;
} set_viewport;
struct
{
unsigned swap_interval;
bool nonblock;
bool adaptive_vsync;
} nonblock;
struct
{
unsigned index;
float x, y, w, h;
} rect;
struct
{
const struct texture_image *data;
const uintptr_t *textures;
unsigned num;
} image;
struct
{
unsigned dims;
bool fullscreen;
} new_mode;
struct
{
unsigned index;
bool smooth;
bool ctx_scaling;
} filtering;
struct
{
char msg[128];
struct font_params params;
} osd_message;
struct
{
custom_command_method_t method;
void* data;
uintptr_t return_value;
} custom_command;
struct
{
custom_font_command_method_t method;
const void **font_driver;
void **font_handle;
void *video_data;
const char *font_path;
float font_size;
bool return_value;
bool is_threaded;
const font_renderer_t *backend;
} font_init;
struct
{
float menu_nits;
float paper_white_nits;
unsigned expand_gamut;
bool scanlines;
unsigned subpixel_layout;
} hdr;
struct
{
unsigned input_scale;
bool rgb32;
bool ret;
} frame_format;
} data;
enum thread_cmd type;
} thread_packet_t;
/* A texture upload the main thread does not wait for. Queued nodes
* are owned by the video thread from post to completion; completed
* nodes wait in the out list until the main thread delivers them from
* video_thread_async_poll(). */
typedef void (*video_thread_async_done_t)(void *user, uintptr_t handle);
typedef void (*video_thread_async_release_t)(void *img);
/* What the video thread does with a node. LOAD creates a texture from
* img and reports the handle; UPDATE writes img into the texture whose
* handle the node carries, in place, and reports that handle back (0
* when the driver could not take the update), with dropped set when
* the driver took it but uploaded nothing. IMPORT does as UPDATE with
* img a video_frame_import_t, making the texture when the handle is
* 0 (made set). */
enum video_thread_async_kind
{
VIDEO_THREAD_ASYNC_LOAD = 0,
VIDEO_THREAD_ASYNC_UPDATE,
VIDEO_THREAD_ASYNC_IMPORT,
VIDEO_THREAD_ASYNC_JOB
};
/* Cold render-owner job. Payload and node survive through done/release.
* Results are opaque, never texture handles. Missing drivers are valid input. */
typedef struct video_thread_async_job
{
uintptr_t (*run)(void *driver_data, const video_poke_interface_t *poke,
void *user);
void *user;
} video_thread_async_job_t;
typedef struct video_thread_async_load
{
/* The wrapper's list link, first so a node and its link share an
* address; the wrapper's from post until done(). */
mpsc_stack_node_t link;
void *img; /* struct texture_image*, ours until released */
void *user;
video_thread_async_done_t done;
video_thread_async_release_t release;
uintptr_t handle;
enum texture_filter_type filter;
uint8_t kind; /* enum video_thread_async_kind */
uint8_t dropped; /* UPDATE: taken, nothing uploaded */
uint8_t made; /* IMPORT: the texture is this node's */
/* UPDATE: driver slots of the texture's upload memory to lend once
* the update has gone, a bit each. What was lent comes back with the
* node: lent_mem the memory, lent_idx the wrapper's record of it for
* video_thread_lend_ready() (-1: not lent). */
uint8_t lend;
int8_t lent_idx[2];
void *lent_mem[2];
size_t lent_pitch[2];
uint8_t lend_padded;
uint8_t lend_refresh;
} video_thread_async_load_t;
/* Deep enough for the burst an overlay issues between two frames - one
* geometry call per descriptor, and a pad's worth of those is dozens -
* as well as the settings a menu page changes. Past it the sender
* waits, as it always did. At 192 bytes a packet this is 24 KiB of the
* wrapper's own state; the video thread runs the packets where they
* lie, so none of it reaches a stack frame. A power of two: the index
* wraps with a mask. */
#define VIDEO_THREAD_DEFERRED_MAX 128
/* Texture slots lent at once under the wrapper: two a streamed
* surface, for the previews and overlays on screen together. */
#define VIDEO_THREAD_LEND_MAX 16
/* The main thread's cost of handing a frame to the video thread,
* counted in CPU cycles in video_thread_frame() while the statistics
* overlay is shown and shown as microseconds, over windows of 120
* pushed frames. Time deliberately spent waiting for a free slot is
* kept apart from the handoff itself. */
typedef struct video_thread_handoff_stats
{
uint64_t handoff_avg_x100; /* entry to signal, wait excluded, us */
uint64_t handoff_worst;
uint64_t copy_avg_x100; /* the frame memcpy, us */
uint64_t copy_worst;
uint64_t wait_avg_x100; /* slot wait, us */
uint64_t wait_worst;
uint64_t bytes_per_frame; /* copied */
unsigned frames_copied; /* of the window */
unsigned frames_zero_copy;
unsigned frames_hw;
unsigned waits; /* pushes that waited for a slot */
unsigned dropped; /* pushes that replaced a queued frame */
unsigned drains; /* holds run a period long to drain */
/* The core's software-framebuffer asks in the window: granted a
* slot, granted but pushed from elsewhere, declined because both
* slots were taken, declined because the frame would not fit */
unsigned asked;
unsigned lent;
unsigned lapsed;
unsigned declined_ring;
unsigned declined_size;
unsigned pushed; /* pushes that took a free slot, since init */
} video_thread_handoff_stats_t;
/* Slots of the statistics snapshot thread_video_t::stats publishes.
* Each 64-bit value takes two, low half first, because the atomics are
* int-wide on every backend. */
enum video_thread_stat_slot
{
VIDEO_THREAD_STAT_FLAGS = 0,
VIDEO_THREAD_STAT_REPEATS_LO,
VIDEO_THREAD_STAT_REPEATS_HI,
VIDEO_THREAD_STAT_LAT_AVG_LO,
VIDEO_THREAD_STAT_LAT_AVG_HI,
VIDEO_THREAD_STAT_LAT_MAX_LO,
VIDEO_THREAD_STAT_LAT_MAX_HI,
VIDEO_THREAD_STAT_CORE_LO,
VIDEO_THREAD_STAT_CORE_HI,
VIDEO_THREAD_STAT_RENDER_LO,
VIDEO_THREAD_STAT_RENDER_HI,
VIDEO_THREAD_STAT_SWAPS_LO,
VIDEO_THREAD_STAT_SWAPS_HI,
/* The presenter's inputs to display pacing, so the hold on the main
* thread reads them from the same snapshot as the numbers above */
VIDEO_THREAD_STAT_PERIOD_LO,
VIDEO_THREAD_STAT_PERIOD_HI,
VIDEO_THREAD_STAT_NEXT_LO,
VIDEO_THREAD_STAT_NEXT_HI,
VIDEO_THREAD_STAT_INPUT_AVG_LO,
VIDEO_THREAD_STAT_INPUT_AVG_HI,
VIDEO_THREAD_STAT_INPUT_MAX_LO,
VIDEO_THREAD_STAT_INPUT_MAX_HI,
/* how long the driver's present waits for the display, where it
* says (get_last_present_wait) */
VIDEO_THREAD_STAT_WAIT_LO,
VIDEO_THREAD_STAT_WAIT_HI,
/* what else the present waited for, where the driver says
* (get_last_present_sync): the frame's fence and the queue lock */
VIDEO_THREAD_STAT_FENCE_LO,
VIDEO_THREAD_STAT_FENCE_HI,
VIDEO_THREAD_STAT_LOCK_LO,
VIDEO_THREAD_STAT_LOCK_HI,
VIDEO_THREAD_STAT_SLOTS
};
/* Slots of the viewport thread_video_t::vp_pub publishes. Each size
* pair travels as one word in VIDEO_SCALE_PACK's layout, so a pair
* cannot be read half updated even before the sequence is checked. */
enum video_thread_vp_slot
{
VIDEO_THREAD_VP_POS = 0,
VIDEO_THREAD_VP_WH,
VIDEO_THREAD_VP_FULL_WH,
VIDEO_THREAD_VP_SLOTS
};
/* The bools of that snapshot, in VIDEO_THREAD_STAT_FLAGS. */
#define VIDEO_THREAD_STAT_F_PRESENT_REPEAT (1 << 0)
#define VIDEO_THREAD_STAT_F_PHASE_DISPLAY (1 << 1)
#define VIDEO_THREAD_STAT_F_LAT_DISPLAY (1 << 2)
#define VIDEO_THREAD_STAT_F_DISPLAY_PACING (1 << 3)
#define VIDEO_THREAD_STAT_F_PRESENT_IMMEDIATE (1 << 4)
/* Cache-line padding, where two threads write fields that would
* otherwise share a line. A line carrying a write from each is pulled
* back and forth between them on every write to either, which costs
* more than the writes do; the article's phrase for it is write
* sharing, and it is the reason the values below are published rather
* than locked in the first place. A full line each way, so the
* separation holds wherever the fields land rather than depending on
* an offset. */
#define VIDEO_THREAD_LINE 64
/* frame.lent when the loan is the spare buffer rather than a ring slot */
#define VIDEO_THREAD_LEND_SPARE 2
/* Frame buffers the slots and the spare are taken from: the two host
* buffers init makes, the driver's frame memory, and host buffers made
* when every other one is in use */
#define VIDEO_THREAD_FRAME_MEM 8
/* Driver frame memory taken at once: the slots, the spare, and enough
* besides for the frames the GPU may still be reading */
#define VIDEO_THREAD_FRAME_MEM_DRIVER 5
/* The frame ring's bookkeeping, one word: the slot the video thread
* claims next, how many filled slots wait for it, and whether it is
* rendering one. Both threads move it with a compare-and-swap, so the
* main thread taking back the newest unclaimed frame and the video
* thread claiming it can never both succeed on the same slot. */
#define VIDEO_THREAD_RING_TAIL (1 << 0)
#define VIDEO_THREAD_RING_PENDING_SHIFT 1
#define VIDEO_THREAD_RING_PENDING_MASK (3 << VIDEO_THREAD_RING_PENDING_SHIFT)
#define VIDEO_THREAD_RING_BUSY (1 << 3)
#define VIDEO_THREAD_RING_TAIL_OF(s) ((unsigned)((s) & VIDEO_THREAD_RING_TAIL))
#define VIDEO_THREAD_RING_PENDING_OF(s) ((unsigned)(((s) & VIDEO_THREAD_RING_PENDING_MASK) \
>> VIDEO_THREAD_RING_PENDING_SHIFT))
#define VIDEO_THREAD_RING_BUSY_OF(s) (((s) & VIDEO_THREAD_RING_BUSY) != 0)
#define VIDEO_THREAD_RING_MAKE(tail, pending, busy) \
((int)(((tail) & 1) | ((pending) << VIDEO_THREAD_RING_PENDING_SHIFT) \
| ((busy) ? VIDEO_THREAD_RING_BUSY : 0)))
#define VIDEO_THREAD_TEXTURE_ENABLE 1
#define VIDEO_THREAD_TEXTURE_FULL_SCREEN 2
/* One menu texture frame, owned by whichever side of the triple
* buffer holds it. */
#define VIDEO_THREAD_MENU_MEM 6
#define VIDEO_THREAD_MENU_EMPTY 0
#define VIDEO_THREAD_MENU_READY 1
#define VIDEO_THREAD_MENU_CPU 2
#define VIDEO_THREAD_MENU_GPU 3
#define VIDEO_THREAD_MENU_BUILD 4
struct video_thread_menu_mem
{
void *ptr;
size_t size;
uintptr_t id;
unsigned pitch_align;
retro_atomic_int_t state;
};
struct video_thread_menu_texture
{
void *frame;
size_t frame_cap;
unsigned mem;
unsigned dims;
float alpha;
bool rgb32;
};
typedef struct thread_video
{
/* Asynchronous texture uploads, see video_thread_async_post(). */
struct
{
/* Any thread posts to in and this thread takes it whole; this
* thread puts each finished node on out and the main thread
* takes that whole. Neither takes 'lock'. */
mpsc_stack_t in; /* to upload */
mpsc_stack_t out; /* to deliver */
} async;
/* Upload memory the driver lent under the wrapper, one record per
* texture slot. The video thread owns every field but busy, which
* it sets when an update leaves the GPU reading the slot and clears
* once the driver says the read is done. For per-write mappings it
* publishes fresh memory/pitch before clearing busy; the main thread
* reads those only after readiness and before posting that slot. */
struct
{
void *mem;
uintptr_t id;
size_t pitch;
size_t row_pitch;
retro_atomic_int_t busy;
unsigned slot;
bool used;
bool refresh;
bool padded;
} lend[VIDEO_THREAD_LEND_MAX];
unsigned lend_busy; /* records busy, the video thread's count */
bool lend_used; /* anything was ever lent: the video thread's */
/* Presenter state, all owned by the video thread. present_period
* is one display period in usec, taken from the refresh rate of the
* last frame rendered; next_present is when a repeat of it falls
* due. present_repeat is set once a frame
* has been rendered with retain_output and the driver can present
* it again. */
retro_time_t present_period;
/* When the next repeat is due: one period after the last present,
* measured from the display's own timestamp when the driver has one
* and from the clock otherwise, and always after that present's
* completion so a late display report cannot pile repeats up. */
retro_time_t next_present;
uint64_t frames_repeated;
/* Whether the last repeat deadline came from a display timestamp
* the driver reported (true) or from the clock (false). Stats. */
bool phase_from_display;
/* The driver's present_immediate() after the last frame. Stats. */
bool present_immediate;
/* The wrapped driver's answer to get_refresh_rate, polled on the
* video thread after each frame; 0 when it has none. The presenter
* paces on it in preference to the setting. Video thread only -
* every other thread takes it from refresh_rate_bits. */
float driver_refresh_rate;
/* Swaps one repeat makes: the group the retained frame made. */
unsigned present_group;
/* Whether to ask the driver when its last present reached the
* display; the setting, carried across with the frame. */
bool present_timing_ask;
/* Latency, push to the vblank the frame goes out on, for the
* statistics overlay: a moving average and the session's worst, in
* microseconds, and whether that vblank is on the display's own
* grid or one estimated from the clock. Video thread only; the
* overlay takes them from the stats snapshot. */
retro_time_t last_present_end;
bool present_shown; /* the frame just presented is out as its call returned */
retro_time_t latency_avg;
retro_time_t latency_max;
retro_time_t latency_max_at;
/* The same two for the input's age: poll to present. */
retro_time_t input_age_avg;
retro_time_t input_age_max;
retro_time_t input_age_max_at;
bool latency_from_display;
/* Display pacing. render_time is the video thread's moving average
* of driver->frame(), video thread only and carried to the hold in
* the stats snapshot. Its main-thread counterparts, core_time and
* run_start (when the last handoff returned), are with the main
* thread's fields below pad_main. */
retro_time_t render_time;
retro_time_t present_wait; /* the wait in the driver's present, averaged as render_time is */
retro_time_t fence_wait; /* the frame fence waited out before it, averaged the same */
retro_time_t lock_wait; /* the queue lock waited for, averaged the same */
retro_time_t slack_min; /* the least a frame had to spare in the window */
retro_time_t slack_window_at; /* when the window began */
unsigned miss_run; /* frames in a row that missed their vblank */
/* The last frame presented had queued behind another: the next
* hold runs a period longer to drain it. Video thread only. */
unsigned drain_cooldown;
/* The view count the driver has, and a smaller one it takes once
* views_shrink_left more frames have drawn. Video thread only. */
unsigned views_count;
unsigned views_shrink_to;
unsigned views_shrink_left;
/* Video thread sets it, the hold takes it with a fetch_and */
retro_atomic_int_t drain_pending;
retro_atomic_int_t movie_capable;
video_thread_movie_state_t movie_state;
video_thread_movie_t movie_next;
/* Fast-forward, from the frame info at the push: the hold stands
* down for it. Distinct from nonblock, which vsync-off also sets. */
bool fast_forward;
/* Whether the core ran this iteration, from the frame info at the
* push: the hold's period is the content's only while it does. */
bool core_running;
/* The menu is up at Menu Frame Rate 'Content Rate', from the frame
* info at the push: the hold keeps the content's period over the
* stopped core */
bool menu_content_rate;
/* Display Pacing On VBlank Events may hold this frame, from the
* frame info at the push */
bool vblank_pacing;
/* What the main thread writes every frame, kept off the lines the
* video thread writes (see VIDEO_THREAD_LINE): the push's clock and
* display pacing's moving averages and schedule, then the values it
* publishes for the video thread, on a line of their own. */
unsigned char pad_main[VIDEO_THREAD_LINE];
retro_time_t last_time;
/* core_time: the main thread's moving average of the time between
* one frame handoff's return and the next handoff's arrival; see
* render_time, its video thread counterpart. */
retro_time_t core_time;
/* Display pacing's schedule: when the next content frame is due,
* accumulated in the content's own period. Main thread. The
* period itself is published for the video thread in
* content_period_us, which judges a frame queued by it: on a
* display faster than the content, a frame is due every few
* vblanks, and the display's period would call every longer render
* a queue. */
retro_time_t content_due;
retro_time_t content_period;
retro_time_t run_start;
/* The hold on the display's vblank events
* (video_thread_hold_vblank): its own due time, the display
* server's vblank clock, the frames it stands aside for (~0u: for
* good), the frames it has held since it last stood down, and the
* vblanks that came late for their frame. Main thread. */
retro_time_t vblank_due;
unsigned (*vblank_wait)(unsigned seen, unsigned timeout_us,
retro_time_t *at);
unsigned vblank_off;
unsigned vblank_held;
unsigned vblank_late;
/* Frames Driver Pacing released, the driver's sleep in the hold's
* place (video_thread_pace_hold). Main thread. */
uint64_t driver_released;
/* Handoff cost, this window and the last full one. Main thread. */
struct
{
uint64_t handoff_sum, handoff_max; /* cycles */
uint64_t copy_sum, copy_max;
uint64_t wait_sum, wait_max;
uint64_t span_ticks, span_us; /* the window's tick rate */
uint64_t bytes;
unsigned copied, zero_copy, hw, waits, dropped, drains;
unsigned asked, lent, lapsed, declined_ring, declined_size;
unsigned frames;
bool counting; /* overlay was up last push */
video_thread_handoff_stats_t last;
} handoff;
unsigned char pad_main_pub[VIDEO_THREAD_LINE];
/* The main thread's inputs the video thread carries into the stats
* snapshot (core_time, display_pacing) and consults when it judges
* a queued frame (the content period), in microseconds. */
retro_atomic_int_t core_time_us;
retro_atomic_int_t display_pacing_pub;
retro_atomic_int_t content_period_us;
unsigned char pad_main_end[VIDEO_THREAD_LINE];
bool display_pacing;
bool present_repeat;
/* Log the geometry clamp once per session, not per frame. */
bool clamp_logged;
/* A main-thread present_last() asks for one repeat at the next
* opportunity rather than waiting for the deadline. */
retro_atomic_int_t repeat_request;
/* present_group while present_repeat, else 0: what present_last()
* on the main thread answers with. Video thread publishes it. */
retro_atomic_int_t repeat_group;
/* What the hold adds to its margin for a driver whose present waits
* for the vblank, in microseconds: written by the video thread, which
* sees each frame's wait, and read by the hold. See
* video_thread_margin_learn(). */
retro_atomic_int_t margin_extra_us;
/* The synchronous command channel takes no lock. A poster holds
* the poster slot from send to reply; the mailbox below is its
* alone while it does, and the video thread's once send_cmd says a
* command is in it.
*
* reply_ec: what the poster sleeps on for its reply (reply_cmd
* turning to its command) or a waiter call, and what the video
* thread sleeps on for that call's done. user_ec: what a thread
* waiting for the poster slot sleeps on. On the main thread on
* Apple, either wait comes up every millisecond to pump the cocoa
* trampoline, so work the video thread marshals back runs.
*
* The poster slot: user-side commands come from more than one
* thread -- the main thread uploads an achievement badge while a
* task thread takes the screenshot the same unlock asked for -- and
* two of them in the single mailbox at once means one reply
* satisfies both waiters, and the video thread then runs a packet
* whose payload points into a stack frame that has already
* returned. user_busy is taken with a compare-exchange; user_owner
* (a thread id) and user_depth only keep an owner from deadlocking
* on its own slot, which the cocoa trampoline can re-enter on the
* waiting thread; a nested post is not serviceable (see
* video_thread_user_acquire()). The video thread never takes the
* slot: a wrapper entry reached from driver->frame() runs inline
* via inline_reply before the acquire. */
retro_eventcount_t reply_ec;
retro_eventcount_t user_ec;
retro_atomic_size_t user_owner;
retro_atomic_int_t user_busy;
/* The owner's alone. */
unsigned user_depth;
#ifndef RETRO_ATOMIC_HAS_PTR
/* Without pointer atomics the asynchronous upload lists are pushed
* and taken under this lock. */
slock_t *lock;
#endif
#ifdef HAVE_GFX_WIDGETS
/* Main thread: the panels' text for the next frame pushed,
* staged by video_thread_status_text() */
char status_text[NAME_MAX_LENGTH];
size_t status_text_len;
#endif
#ifdef HAVE_VIDEO_FILTER
/* Main thread: the next frame pushed is raw, for this thread to
* filter; its bytes per pixel. Staged by video_thread_defer_filter() */
unsigned filter_next;
#endif
/* Main thread: the next frame pushed is in a source pixel format the
* driver does not take, for this thread to convert. One of
* enum video_thread_convert. Staged by video_thread_defer_convert() */
unsigned convert_next;
/* work: the video thread parks here between passes and is notified
* by whoever gives it something to do - a frame published, a
* command sent, an upload posted, a deferred packet queued, a repeat
* asked for. Each notifier writes its work first, so the wait's
* re-check between prepare and commit sees it. */
retro_eventcount_t work;
sthread_t *thread;
/* The video singleton's (stable) address, captured on the main
* thread at init: the loop and its helpers reach ra-video state
* through this, never through the getter, so no thread entry in
* this file calls into a singleton getter at all. */
video_driver_state_t *video_st;
video_info_t info;
const video_driver_t *driver;
#ifdef HAVE_OVERLAY
const video_overlay_interface_t *overlay;
#endif
const video_poke_interface_t *poke;
void *driver_data;
/* Overlay alpha modulation, lock-free. The values are float bits
* in atomic ints: the main thread's set_alpha (fire-and-forget by
* design) stores a value relaxed and then store-releases
* alpha_update; the video thread's per-frame apply clears the
* flag with an acquire exchange BEFORE reading the values, so a
* set that lands mid-apply re-raises the flag and is applied
* whole next frame - the exchange-first order is what makes an
* update impossible to lose. The array pointer and alpha_mods
* are written only inside the CMD_OVERLAY_LOAD handler, with the
* main thread blocked in that command's reply wait and the video
* thread out of its frame call, so plain reads of both are safe
* everywhere. */
retro_atomic_int_t *alpha_mod;
/* Video thread only: the float bits last handed to the driver for
* each image, alpha_mods of them, so an apply passes on only what
* changed. NULL when it could not be had, and every image is set
* at every apply. */
int *alpha_applied;
/* The menu texture: the main thread fills the next frame in the
* triple buffer's back slot and publishes it, the video thread takes
* the newest at its next apply and hands it to the driver, which
* copies CPU slots or retains upload storage until GPU retirement. 'enable' carries the enable and full-screen flags, written
* by the main thread, read at every apply. */
struct
{
struct video_thread_menu_texture slot[3];
struct video_thread_menu_mem mem[VIDEO_THREAD_MENU_MEM];
size_t refused_size;
retro_atomic_int_t request_dims;
retro_triple_buffer_t frames;
retro_atomic_int_t enable; /* VIDEO_THREAD_TEXTURE_* */
} texture;
unsigned hit_count;
unsigned miss_count;
unsigned alpha_mods;
/* The last viewport reported to a caller, VIDEO_THREAD_VP_* words:
* what CMD_READ_VIEWPORT compares the driver's own against. */
retro_atomic_int_t read_vp[VIDEO_THREAD_VP_SLOTS];
/* Content scale, published at the end of each frame. The viewport
* maths that produces it runs on the video thread, so
* video_driver_build_info() reads it from here rather than from
* video_driver_st. Width in the high 16 bits, height in the low, one
* value so the pair is read whole and without 'lock'. Statistics
* only. */
retro_atomic_int_t scale_packed;
/* The statistics overlay's numbers, published as a seqlock so that
* reading them takes no lock: 'lock' stays the frame handoff's and
* the ring's, and a reader only ever loads. The video thread writes
* the slots inside a region it already holds 'lock' for, which is
* what serialises publishers; a reader retries while a publish is
* in flight (odd) or lands across its copy. Statistics only, so the
* two the main thread feeds - core_time and display_pacing - are
* carried at one frame's lag. */
retro_atomic_int_t stats_seq;
retro_atomic_int_t stats[VIDEO_THREAD_STAT_SLOTS];
/* The driver's viewport and the rate that came back with it,
* published by the video thread - the only writer of either - so
* that an input driver asking for the viewport every poll, and the
* runloop asking for the rate every iteration, take no lock. The
* viewport is a seqlock over six slots; the rate is one word, whole
* at int width, carried as float bits. */
retro_atomic_int_t vp_seq;
retro_atomic_int_t vp_pub[VIDEO_THREAD_VP_SLOTS];
retro_atomic_int_t refresh_rate_bits;
/* The snapshots above are written by the video thread every frame;
* cmd_data below is rewritten by a sender for every synchronous
* command, and a burst of them rewrites it in place. */
unsigned char pad_published[VIDEO_THREAD_LINE];
thread_packet_t cmd_data;
/* Set by the video thread while it runs a command inline on itself:
* the reply goes here instead of into cmd_data, so a command the
* main thread sent meanwhile is neither answered nor overwritten.
* Video thread only. */
thread_packet_t *inline_reply;
/* Commands that want nothing back: queued here and run by the video
* thread on its next pass, so the caller does not wait for a round
* trip. A full queue falls back to the synchronous send, so nothing
* is ever dropped. */
video_driver_t video_thread;
/* The poster writes cmd_data, then publishes the command here with
* a release store; the video thread answers into cmd_data, stores
* the command in reply_cmd and clears this. */
retro_atomic_int_t send_cmd;
retro_atomic_int_t reply_cmd;
retro_atomic_int_t alpha_update;
/* Core frames cross to the video thread through a two-slot ring so
* the main thread's copy of frame N+1 overlaps the worker's upload
* and render of frame N. The ring's bookkeeping is the one word
* 'state' (VIDEO_THREAD_RING_*), moved by compare-and-swap on both
* sides; slot contents are owned by whichever side holds the slot -
* the main thread between pick and publish, the video thread
* between claim and completion - and the publish and the claim
* order them, so they need no lock. */
struct
{
/* Ring progress: notified by the video thread when it claims or
* completes a slot. Any number of waiters, each re-testing its
* own predicate against 'state'. */
retro_eventcount_t ring;
/* A third buffer of buffer_size bytes, outside the ring, lent to
* the core when both slots are taken (one queued, one being
* rendered - a driver whose present blocks until vblank keeps
* the ring that way). The push that returns it swaps it into the
* slot it picks, and the buffer it displaces becomes the spare.
* Taken from mem[] on the first ask that needs it, and again
* when a frame in flight still reads it; main thread only. */
uint8_t *spare;
/* The buffers slot[].buffer and spare are. id names driver frame
* memory (frame_mem_alloc), 0 host memory. busy is set by the
* video thread when a frame it submitted reads the buffer where
* it lies, and cleared once the driver says that read is done:
* the main thread writes a buffer only while it is clear, and
* takes another otherwise, never waiting. ptr, size, id and
* cached change only with the main thread waiting on the video
* thread or the video thread stopped. */
struct
{
uint8_t *ptr;
size_t size;
uintptr_t id;
retro_atomic_int_t busy;
bool cached;
} mem[VIDEO_THREAD_FRAME_MEM];
int spare_mem; /* the spare's mem[] index, -1 for none */
/* The driver's poke table, for its frame memory: video thread */
const struct video_poke_interface *mem_poke;
unsigned mem_busy; /* video thread: the entries it set busy */
/* The row pitch multiple the driver reads its frame memory at in
* place, which a copy into it is padded to; set with the main
* thread waiting on the video thread */
unsigned pitch_align;
/* Bytes allocated for each slot buffer at thread_init, from the
* core's declared maximum geometry. A core that hands over a
* larger frame than it declared is clamped to this. */
size_t buffer_size;
/* Main thread: buffer_size / current bytes per pixel. */
size_t pixel_capacity;
struct video_thread_frame_slot
{
video_thread_movie_t movie;
bool movie_control; /* immutable while queued or claimed */
uint64_t count;
/* When the core handed this frame over, on the main thread's
* clock; the latency readout measures from here. */
retro_time_t pushed_at;
/* When the input this frame was made from was read, on the
* same clock; 0 if the poll was not stamped. The input-age
* readout measures from here. */
retro_time_t input_at;
/* Hardware-rendered frame: the HW ring slot it lives in, -1
* for a software frame. See hw_ring below. */
int hw_slot;
/* The push carried no pixels (the core duped): the driver is
* given NULL and repeats what it has, never this buffer -
* which holds whatever frame was last put in it, an old one. */
bool dupe;
uint8_t *buffer;
int mem; /* buffer's mem[] index */
/* Where the frame starts inside buffer: 0 for a copied frame,
* and for a lent frame whatever the core pushed, which may sit
* past the start (a core that crops by pointer offset). */
size_t offset;
unsigned dims;
unsigned pitch;
char msg[NAME_MAX_LENGTH];
#ifdef HAVE_OZONE
char menu_ozone_color_theme[32];
#endif
#ifdef HAVE_MENU
/* video_info.menu.msgbox points here when the frame has one */
char menu_msgbox[1024];
#endif
#ifdef HAVE_GFX_WIDGETS
/* The on-screen panels' text for the widgets, which this
* thread draws; zero length leaves what they show */
char status_text[NAME_MAX_LENGTH];
/* The widget paths this frame was handed, copied for the same
* reason the status text is: they are the main thread's, and
* it may write them again while this frame is drawn. */
char widget_dir_assets[PATH_MAX_LENGTH];
char widget_path_font[PATH_MAX_LENGTH];
/* And the two the menu's frame() watches for a change */
char menu_rgui_theme_preset[PATH_MAX_LENGTH];
char menu_dynamic_wallpapers_dir[PATH_MAX_LENGTH];
size_t status_text_len;
#endif
#ifdef HAVE_VIDEO_FILTER
/* A raw core frame for this thread to run the software filter
* on: its bytes per pixel, 0 for a frame ready to draw; and
* the filter, output buffer and output bytes per pixel it was
* staged with, which stay until a later frame retires them */
unsigned filter_bpp;
unsigned filter_out_bpp;
void *filter;
void *filter_buf;
#endif
/* A frame still in the core's source pixel format, for this
* thread to convert before the filter and the driver */
unsigned convert;
/* Built by the main thread in video_thread_frame() and handed
* to the driver's frame call by pointer on the video thread.
* video_driver_build_info() reads video_driver_st and
* runloop_state, both of which the main thread mutates, so it
* must not be called from the worker. */
video_frame_info_t video_info;
/* Textures the frontend released before this frame was handed
* over: every frame that could still name one is drawn by the
* time this one is, so the video thread frees them once it
* has drawn it (video_thread_tex_retire_t). */
void *tex_retire;
/* The statistics overlay's text for this frame, which the main
* thread's buffer will not hold by the time this thread draws:
* video_info.stat_text points here. Only copied when there is
* text, so a frame without the overlay carries none. */
char stat_text[VIDEO_STAT_TEXT_SIZE];
#ifdef HAVE_VIDEO_WINDOWS
/* The screens' windows this frame was handed, for the same
* reason: video_info.windows points here when it has them. */
video_windows_frame_t windows;
#endif
} slot[2];
/* Zero-copy: the slot handed to the core through
* get_current_software_framebuffer, VIDEO_THREAD_LEND_SPARE for
* the spare, -1 for none. A lent slot is held free until the core
* pushes a frame: a push whose data is inside the lent buffer
* publishes it without a copy; any other push clears the
* reservation first. Main thread only: the loan and the push
* that settles it are both the core's thread's. */
int lent;
/* Slot the video thread claims next (claiming flips it), filled
* slots not yet claimed (0..2), and whether the video thread is
* rendering one - while set, the slot being rendered is
* tail ^ 1. One word, VIDEO_THREAD_RING_*. */
retro_atomic_int_t state;
/* Hardware-rendered cores. The core's sync index space is this
* ring, not the swapchain: VIDEO_THREAD_HW_RING slots each hold a
* copy of what the core handed over for one frame and a fence
* the video thread signals after driving the driver with it.
* Opaque here so this header needs no API types; see
* video_thread_hw.c. */
void *hw_ring;
/* 1 while hw_ring hands one GL context between the threads: the
* video thread reads this, not hw_ring, at every pass - the ring
* is set up on the main thread, and this is stored after it with
* release, so a thread that sees 1 sees the ring. */
retro_atomic_int_t hw_handoff;
uint64_t zero_copy_count;
} frame;
/* Set by the main thread, taken by the video thread's apply. */
retro_atomic_int_t apply_state_changes;
/* Video thread only: the driver was handed a page since the last
* apply, so it holds none of alpha_applied - the next apply sets
* every image. */
bool alpha_reset;
/* Textures the frontend has released since the last frame was handed
* over, waiting for one to carry them to the video thread. Pushed
* from whichever thread unloads, drained by the push. */
mpsc_stack_t tex_retire;
/* Which thread is waiting for a reply, and how deep: the poster
* slot's owner's alone. Maintained unconditionally so the struct
* layout does not depend on the build type; only asserted on in
* debug builds. */
uintptr_t reply_waiter;
unsigned reply_waiters;
/* A call the video thread needs run on the thread that is waiting
* for its reply - the core's thread, which holds the core's GL
* context. Posted from the video thread while a synchronous command
* is in flight, run by the waiter inside its wait loop, and the
* video thread waits for done. See video_thread_call_on_waiter(). */
struct
{
/* Written by the video thread before it raises pending. */
void (*fn)(void *data);
void *data;
/* The number of commands sent and not yet answered, each with a
* thread that will wait for the reply and can service a call
* there; without one, the caller runs it itself. */
retro_atomic_int_t waiters;
retro_atomic_int_t pending;
retro_atomic_int_t done;
} waiter_call;
/* Published by the video thread after each frame and read by the
* main thread, every frame, without 'lock': the window's four
* answers in one word of VIDEO_THREAD_WIN_* bits, one store for all
* of them. PRESENTABLE is the context's answer to "have you anything
* to present to"; the context data belongs to the video thread, and
* asking it directly from the runloop would read a swapchain handle
* while this thread rebuilds it. */
retro_atomic_int_t win_flags;
/* The worker still takes commands: set before it starts, cleared as
* it handles CMD_FREE. Not 'alive', which is the driver's answer for
* the window and goes false - on a close or a quit signal - while the
* worker still runs and holds the context */
retro_atomic_int_t worker_running;
/* The flags above are published by the video thread every frame;
* the two below, and the ring's head after them, are the main
* thread's. */
unsigned char pad_flags[VIDEO_THREAD_LINE];
bool nonblock;
bool is_idle;
/* Commands that want nothing back, for the video thread to run on
* its next pass. One producer (whichever thread calls the setters)
* and one consumer (the video thread), so the ring needs no lock:
* the producer moves head after writing a slot, the consumer moves
* tail after running one, and each reads the other's index with an
* acquire. The indices only grow; the slot is the index modulo the
* size. Allocated once, so the ring's size is not this struct's.
*
* Last in the struct, so a new field goes after these rather than
* between anything above. The padding around them is deliberate and
* is described where VIDEO_THREAD_LINE is defined; a field added
* inside one of those pads puts back the sharing they exist to
* remove. */
thread_packet_t *deferred;
retro_atomic_int_t deferred_head;
/* head is the producer's, tail is the video thread's, and each
* reads the other every pass: on one line the pass costs a
* transfer in each direction. */
unsigned char pad_ring[VIDEO_THREAD_LINE];
retro_atomic_int_t deferred_tail;
unsigned char pad_aspect[VIDEO_THREAD_LINE];
/* Latest aspect index plus one; zero means no resize pending. */
retro_atomic_int_t aspect_ratio_pending;
} thread_video_t;
/**
* video_init_thread:
* @out_driver : Output video driver
* @out_data : Output video data
* @input : Input input driver
* @input_data : Input input data
* @driver : Input Video driver
* @info : Video info handle.
*
* Creates, initializes and starts a video driver in a new thread.
* Access to video driver will be mediated through this driver.
*
* Returns: true (1) if successful, otherwise false (0).
**/
/**
* video_thread_set_prefer_fast_cores:
*
* Asks that the video thread, once spawned by video_init_thread(), be
* placed on the fast cores of a mixed-core processor (see
* sthread_prefer_fast_cores). Off by default.
*/
void video_thread_set_prefer_fast_cores(bool prefer);
/**
* video_thread_set_raise_priority:
*
* Asks that the video thread, once spawned by video_init_thread(), be
* scheduled ahead of ordinary threads (thread_elevation, Games task).
*/
void video_thread_set_raise_priority(bool raise);
/**
* video_thread_host_hold:
* @on_exit : run on the thread before it ends, if it ends without
* another driver having run on it.
*
* Called on the video thread while its driver is being freed: keeps
* the thread for the threaded driver that comes next, instead of
* ending it with this one. For what cannot outlive its thread - on
* Windows, a window. False, and nothing held, if the caller is not
* the video thread.
*/
bool video_thread_host_hold(void (*on_exit)(void));
#ifdef _WIN32
/* Set on the host thread; released there after the kept window. */
bool video_thread_host_set_cleanup(void (*cleanup)(void));
#endif
/* True while the thread is held and waiting for its next driver. */
bool video_thread_host_is_held(void);
/* Ends a held thread: no threaded driver is coming for it. */
void video_thread_host_stop(void);
bool video_init_thread(
const video_driver_t **out_driver, void **out_data,
const video_driver_t *driver, const video_info_t info);
bool video_thread_font_init(
const void **font_driver,
void **font_handle,
void *data,
const char *font_path,
float font_size,
const font_renderer_t *backend,
custom_font_command_method_t func,
bool is_threaded);
/* Blocking run-on-video-thread: func(data) executes on the video
* thread while the caller waits, or directly when the wrapper is
* not active. The mechanism behind video_thread_texture_handle,
* exported for any main-thread code that must touch video-thread-
* owned state. */
uintptr_t video_thread_run_blocking(custom_command_method_t func,
void *data);
/* Main thread: func(data) queued to run on the video thread before its
* next frame, its answer dropped; nothing waits. False when it could
* not be queued (no wrapper, the queue full, or called on the video
* thread), for the caller to run it another way. */
bool video_thread_run_queued(custom_command_method_t func, void *data);
uintptr_t video_thread_texture_handle(void *data,
custom_command_method_t func);
/* Post a caller-owned node (see video_thread_async_load_t) for the
* video thread: kind, img, handle (UPDATE), filter (LOAD), done, user
* and release filled in by the caller, next left alone. The node is
* the wrapper's from the return until done() has run, which happens
* from video_thread_async_poll() on the main thread, or from the
* wrapper's teardown with handle 0. No allocation: this is the
* streaming path, one embedded node per surface. Returns false, having
* taken nothing, when the wrapper is not active or the caller is the
* video thread; the caller then runs the operation synchronously.
* Main thread only. */
bool video_thread_async_post(video_thread_async_load_t *n);
/* Whether lent upload memory a node came back with (lent_idx) may be
* written: false while the GPU may still read the last update from
* it. Never waits. Main thread. */
bool video_thread_lend_ready(int idx);
/* After readiness, while the surface owns the slot until its next post.
* NULL memory revokes a loan whose mapping no longer fits. */
bool video_thread_lend_get(int idx, void **mem, size_t *pitch);
/* Whether the wrapped driver updates textures in place; false while
* no wrapper is up. video_driver_texture_can_update() asks this so a
* wrapper forwarder is never mistaken for a capability. */
bool video_thread_texture_can_update(void);
/* Whether the caller is the video thread; false while no wrapper is
* up. */
bool video_thread_is_video_thread(void);
/* Deliver completed asynchronous uploads to their done() callbacks.
* Main thread; video_thread_frame() calls it, callers that upload
* while no frames are being pushed can call it themselves. */
void video_thread_async_poll(void);
bool video_thread_movie_supported(void);
/* Barrier: wait until the video thread is idle (no pending frame).
* Must be called from the main thread before freeing GPU resources
* that an in-flight frame might reference. No-op on non-threaded
* video or when called from the video thread. */
/* The context's last answer to "have you anything to present to",
* polled on the video thread after each frame and published in
* win_flags. False only when the wrapper is active and the context
* said so; true in every other case, including when there is no
* wrapper, so callers need no threading test of their own. */
bool video_thread_presentable(void);
bool video_thread_presenter_stats(uint64_t *repeats, bool *display_phase);
/* Display pacing statistics: whether it is on, and the core and render
* times it is reserving, in microseconds. From the published snapshot,
* so the two the main thread feeds are a frame behind. Returns whether
* the wrapper is up at all. */
/* How long the driver's present waits for the display, a moving
* average in microseconds; false where the driver does not say or
* there is no wrapper. For the statistics: beside the render time,
* which no longer counts it. */
bool video_thread_present_wait_stats(retro_time_t *wait);
/* What the driver's present waited for besides the display: the
* frame's own fence and the queue lock. False where the driver does
* not say. */
bool video_thread_present_sync_stats(retro_time_t *fence_wait,
retro_time_t *lock_wait);
bool video_thread_pacing_stats(bool *display_pacing,
retro_time_t *core_time, retro_time_t *render_time);
/* Latency from the core's handover to the present, for the overlay:
* the moving average and the session's worst, and whether the present
* end came from the display. False with no wrapper. */
/* How old the input was that a presented frame was made from: from
* the poll that read the devices to the frame's present. False while
* polls are not being stamped (the statistics are not shown) or
* nothing has been presented. */
bool video_thread_input_age_stats(retro_time_t *avg, retro_time_t *worst);
bool video_thread_latency_stats(retro_time_t *avg, retro_time_t *worst,
bool *from_display);
/* video_st->swap_count is written by the video thread while the wrapper
* is installed; this reads it from the published snapshot, which every
* advance of it is published with. Without the wrapper (or from the
* video thread) it is the plain value. */
uint64_t video_thread_swap_count(void);
/* False when the wrapper is not active. Main thread. */
bool video_thread_get_handoff_stats(video_thread_handoff_stats_t *out);
/* On the main thread, on Cocoa: run the trampoline mode briefly so a
* job the video thread marshalled to the main thread can run. Anywhere
* else, nothing. For a main-thread wait on something the video thread
* will signal - a ring fence - where the video thread may need the
* main thread first. */
void video_thread_main_pump(void);
void video_thread_core_context_take(void);
void video_thread_core_context_release(void);
/* From the video thread, while it is answering a synchronous command:
* runs fn on the thread waiting for the reply and returns when it has
* run. That thread is the core's, which is where a driver must go to
* touch the core's GL context under the hardware ring. From any other
* thread, or with no waiter, fn simply runs on the caller. */
void video_thread_call_on_waiter(void (*fn)(void *data), void *data);
void video_thread_wait_idle(void);
/* Main thread: tightly packed menu pixels for set_texture_frame with
* the same dimensions/format. Valid until another loan request, menu
* publication or reinit. NULL when unavailable. */
void *video_thread_get_menu_framebuffer(unsigned dims, bool rgb32,
size_t *pitch);
/* The wrapper's invalidate_hw_render_cache, and what a hardware core's
* set_image(NULL) means under the ring. Main thread. On return no
* frame that reads the core's image is queued, being drawn or still on
* the GPU, and neither the driver nor the ring will hand it to a later
* one: a dupe draws the driver's blank texture until the core presents
* again. The core's image is then the core's to destroy. */
void video_thread_invalidate_hw_render_cache(void *data);
/* GPU recording under the wrapper, main thread. The video thread reads
* back each frame it draws through the driver's poke record_read; this
* takes the newest since the last call without waiting for one.
* Returns 1 with *frame set (width * height rows, B,G,R,X with @bgrx,
* else BGR24) and *bottom_up to their order, 0 when nothing new has
* arrived since a frame was last taken, -1 when there is nothing to
* record yet, and -2 when the driver has no recording reader, for the
* caller to use read_viewport(). Starts the readbacks on first use and
* again at a new output size or format. Viewport changes are scaled and
* letterboxed by the worker. */
int video_thread_record_take(void *data, unsigned dims, bool bgrx,
const uint8_t **frame, bool *bottom_up);
/* Whether video_thread_record_take() reads back on the video thread,
* in either format, rather than returning -2. Main thread. */
bool video_thread_record_has_reader(void *data);
/* Stops the readbacks; the buffers go once no frame names them. */
void video_thread_record_stop(void *data);
/* Main controller, after detaching from future frames. One ring snapshot;
* true while a queued or drawing frame still names this GPU recorder. */
bool video_thread_record_gpu_in_flight(void *data, const void *recorder);
#ifdef HAVE_GFX_WIDGETS
/* Main thread: stages the on-screen panels' text to travel with the
* next frame pushed, for the widgets the worker draws. */
void video_thread_status_text(const char *s);
#endif
#ifdef HAVE_VIDEO_FILTER
/* Main thread: the next frame pushed is the core's, unfiltered, in a
* format of in_bpp bytes per pixel; the worker runs the software
* filter on it before drawing. */
void video_thread_defer_filter(unsigned in_bpp);
/* Main thread: a filter and its output buffer replaced while frames
* staged with them may still be drawn, for the video thread to free
* once they have been. False with no wrapper running. */
bool video_thread_retire_filter(void *filter, void *buffer);
#endif
/* Source pixel formats the video thread converts on the frame's way to
* the driver, in place of the main thread doing so before handover */
enum video_thread_convert
{
VIDEO_THREAD_CONVERT_NONE = 0,
VIDEO_THREAD_CONVERT_0RGB1555, /* to RGB565 through the scaler */
VIDEO_THREAD_CONVERT_XRGB2101010 /* to XRGB8888 */
};
/* Stages the next frame pushed for conversion on the video thread */
void video_thread_defer_convert(enum video_thread_convert kind);
RETRO_END_DECLS
#endif /* HAVE_THREADS */
#endif