Improvement: Enhance --glx-swap-method

- Enhance --glx-swap-method to support longer buffers ages (3-6), and
  automatic buffer age detection via GLX_EXT_buffer_age.
This commit is contained in:
Richard Grenville 2013-04-26 14:01:20 +08:00
parent 2e6fb0203d
commit 5d654e6877
4 changed files with 122 additions and 54 deletions

View File

@ -64,6 +64,7 @@
#include <sys/poll.h> #include <sys/poll.h>
#include <assert.h> #include <assert.h>
#include <time.h> #include <time.h>
#include <ctype.h>
#include <sys/time.h> #include <sys/time.h>
#include <X11/Xlib.h> #include <X11/Xlib.h>
@ -107,6 +108,10 @@
#define GL_TEXTURE_RECTANGLE 0x84F5 #define GL_TEXTURE_RECTANGLE 0x84F5
#endif #endif
#ifndef GLX_BACK_BUFFER_AGE_EXT
#define GLX_BACK_BUFFER_AGE_EXT 0x20F4
#endif
#endif #endif
// === Macros === // === Macros ===
@ -175,6 +180,9 @@
/// @brief Maximum OpenGL FBConfig depth. /// @brief Maximum OpenGL FBConfig depth.
#define OPENGL_MAX_DEPTH 32 #define OPENGL_MAX_DEPTH 32
/// @brief Maximum OpenGL buffer age.
#define CGLX_MAX_BUFFER_AGE 5
// Window flags // Window flags
// Window size is changed // Window size is changed
@ -285,11 +293,11 @@ enum backend {
}; };
/// @brief Possible swap methods. /// @brief Possible swap methods.
enum glx_swap_method { enum {
SWAPM_UNDEFINED, SWAPM_BUFFER_AGE = -1,
SWAPM_EXCHANGE, SWAPM_UNDEFINED = 0,
SWAPM_COPY, SWAPM_COPY = 1,
NUM_SWAPM, SWAPM_EXCHANGE = 2,
}; };
typedef struct _glx_texture glx_texture_t; typedef struct _glx_texture glx_texture_t;
@ -383,7 +391,7 @@ typedef struct {
/// Whether to avoid rebinding pixmap on window damage. /// Whether to avoid rebinding pixmap on window damage.
bool glx_no_rebind_pixmap; bool glx_no_rebind_pixmap;
/// GLX swap method we assume OpenGL uses. /// GLX swap method we assume OpenGL uses.
enum glx_swap_method glx_swap_method; int glx_swap_method;
/// Whether to try to detect WM windows and mark them as focused. /// Whether to try to detect WM windows and mark them as focused.
bool mark_wmwin_focused; bool mark_wmwin_focused;
/// Whether to mark override-redirect windows as focused. /// Whether to mark override-redirect windows as focused.
@ -576,7 +584,7 @@ typedef struct {
/// The region needs to painted on next paint. /// The region needs to painted on next paint.
XserverRegion all_damage; XserverRegion all_damage;
/// The region damaged on the last paint. /// The region damaged on the last paint.
XserverRegion all_damage_last; XserverRegion all_damage_last[CGLX_MAX_BUFFER_AGE];
/// Whether all windows are currently redirected. /// Whether all windows are currently redirected.
bool redirected; bool redirected;
/// Whether there's a highest full-screen window, and all windows could /// Whether there's a highest full-screen window, and all windows could
@ -958,7 +966,6 @@ typedef enum {
extern const char * const WINTYPES[NUM_WINTYPES]; extern const char * const WINTYPES[NUM_WINTYPES];
extern const char * const VSYNC_STRS[NUM_VSYNC + 1]; extern const char * const VSYNC_STRS[NUM_VSYNC + 1];
extern const char * const BACKEND_STRS[NUM_BKEND + 1]; extern const char * const BACKEND_STRS[NUM_BKEND + 1];
extern const char * const GLX_SWAP_METHODS_STRS[NUM_SWAPM + 1];
extern session_t *ps_g; extern session_t *ps_g;
// == Debugging code == // == Debugging code ==
@ -1357,13 +1364,51 @@ parse_backend(session_t *ps, const char *str) {
*/ */
static inline bool static inline bool
parse_glx_swap_method(session_t *ps, const char *str) { parse_glx_swap_method(session_t *ps, const char *str) {
for (enum glx_swap_method i = 0; GLX_SWAP_METHODS_STRS[i]; ++i) // Parse alias
if (!strcasecmp(str, GLX_SWAP_METHODS_STRS[i])) { if (!strcmp("undefined", str)) {
ps->o.glx_swap_method = i; ps->o.glx_swap_method = 0;
return true; return true;
}
if (!strcmp("copy", str)) {
ps->o.glx_swap_method = 1;
return true;
}
if (!strcmp("exchange", str)) {
ps->o.glx_swap_method = 2;
return true;
}
if (!strcmp("buffer-age", str)) {
ps->o.glx_swap_method = -1;
return true;
}
// Parse number
{
char *pc = NULL;
int age = strtol(str, &pc, 0);
if (!pc || str == pc) {
printf_errf("(\"%s\"): Invalid number.", str);
return false;
} }
printf_errf("(\"%s\"): Invalid GLX swap method argument.", str);
return false; for (; *pc; ++pc)
if (!isspace(*pc)) {
printf_errf("(\"%s\"): Trailing characters.", str);
return false;
}
if (age > CGLX_MAX_BUFFER_AGE + 1 || age < -1) {
printf_errf("(\"%s\"): Number too large / too small.", str);
return false;
}
ps->o.glx_swap_method = age;
}
return true;
} }
timeout_t * timeout_t *

View File

@ -50,14 +50,6 @@ const char * const BACKEND_STRS[NUM_BKEND + 1] = {
NULL NULL
}; };
/// Names of GLX swap methods.
const char * const GLX_SWAP_METHODS_STRS[NUM_SWAPM + 1] = {
"undefined", // SWAPM_UNDEFINED
"exchange", // SWAPM_EXCHANGE
"copy", // SWAPM_COPY
NULL
};
/// Function pointers to init VSync modes. /// Function pointers to init VSync modes.
static bool (* const (VSYNC_FUNCS_INIT[NUM_VSYNC]))(session_t *ps) = { static bool (* const (VSYNC_FUNCS_INIT[NUM_VSYNC]))(session_t *ps) = {
[VSYNC_DRM ] = vsync_drm_init, [VSYNC_DRM ] = vsync_drm_init,
@ -4266,12 +4258,14 @@ usage(void) {
" GLX backend: Avoid rebinding pixmap on window damage. Probably\n" " GLX backend: Avoid rebinding pixmap on window damage. Probably\n"
" could improve performance on rapid window content changes, but is\n" " could improve performance on rapid window content changes, but is\n"
" known to break things on some drivers.\n" " known to break things on some drivers.\n"
"--glx-swap-method undefined/exchange/copy\n" "--glx-swap-method undefined/copy/exchange/3/4/5/6/buffer-age\n"
" GLX backend: GLX buffer swap method we assume. Could be\n" " GLX backend: GLX buffer swap method we assume. Could be\n"
" \"undefined\", \"exchange\", or \"copy\". \"undefined\" is the slowest\n" " undefined (0), copy (1), exchange (2), 3-6, or buffer-age (-1).\n"
" and the safest; \"exchange\" and \"copy\" are faster but may fail on\n" " \"undefined\" is the slowest and the safest, and the default value.\n"
" some drivers. Useless with --glx-use-copysubbuffermesa. Defaults to\n" " 1 is fastest, but may fail on some drivers, 2-6 are gradually slower\n"
" \"undefined\".\n" " but safer (6 is still faster than 0). -1 means auto-detect using\n"
" GLX_EXT_buffer_age, supported by some drivers. Useless with\n"
" --glx-use-copysubbuffermesa.\n"
#undef WARNING #undef WARNING
#ifndef CONFIG_DBUS #ifndef CONFIG_DBUS
#define WARNING WARNING_DISABLED #define WARNING WARNING_DISABLED
@ -6027,7 +6021,7 @@ session_init(session_t *ps_old, int argc, char **argv) {
.tmout_lst = NULL, .tmout_lst = NULL,
.all_damage = None, .all_damage = None,
.all_damage_last = None, .all_damage_last = { None },
.time_start = { 0, 0 }, .time_start = { 0, 0 },
.redirected = false, .redirected = false,
.unredir_possible = false, .unredir_possible = false,
@ -6478,7 +6472,8 @@ session_destroy(session_t *ps) {
free_root_tile(ps); free_root_tile(ps);
free_region(ps, &ps->screen_reg); free_region(ps, &ps->screen_reg);
free_region(ps, &ps->all_damage); free_region(ps, &ps->all_damage);
free_region(ps, &ps->all_damage_last); for (int i = 0; i < CGLX_MAX_BUFFER_AGE; ++i)
free_region(ps, &ps->all_damage_last[i]);
free(ps->expose_rects); free(ps->expose_rects);
free(ps->shadow_corner); free(ps->shadow_corner);
free(ps->shadow_top); free(ps->shadow_top);

View File

@ -914,11 +914,7 @@ cdbus_process_opts_get(session_t *ps, DBusMessage *msg) {
cdbus_m_opts_get_do(glx_copy_from_front, cdbus_reply_bool); cdbus_m_opts_get_do(glx_copy_from_front, cdbus_reply_bool);
cdbus_m_opts_get_do(glx_use_copysubbuffermesa, cdbus_reply_bool); cdbus_m_opts_get_do(glx_use_copysubbuffermesa, cdbus_reply_bool);
cdbus_m_opts_get_do(glx_no_rebind_pixmap, cdbus_reply_bool); cdbus_m_opts_get_do(glx_no_rebind_pixmap, cdbus_reply_bool);
if (!strcmp("glx_swap_method", target)) { cdbus_m_opts_get_do(glx_swap_method, cdbus_reply_int32);
assert(ps->o.glx_swap_method < sizeof(GLX_SWAP_METHODS_STRS) / sizeof(GLX_SWAP_METHODS_STRS[0]));
cdbus_reply_string(ps, msg, GLX_SWAP_METHODS_STRS[ps->o.glx_swap_method]);
return true;
}
#endif #endif
cdbus_m_opts_get_do(track_focus, cdbus_reply_bool); cdbus_m_opts_get_do(track_focus, cdbus_reply_bool);

View File

@ -54,18 +54,20 @@ glx_init(session_t *ps, bool need_render) {
if (need_render && !glx_hasglxext(ps, "GLX_EXT_texture_from_pixmap")) if (need_render && !glx_hasglxext(ps, "GLX_EXT_texture_from_pixmap"))
goto glx_init_end; goto glx_init_end;
// Get GLX context
ps->glx_context = glXCreateContext(ps->dpy, pvis, None, GL_TRUE);
if (!ps->glx_context) { if (!ps->glx_context) {
printf_errf("(): Failed to get GLX context."); // Get GLX context
goto glx_init_end; ps->glx_context = glXCreateContext(ps->dpy, pvis, None, GL_TRUE);
}
// Attach GLX context if (!ps->glx_context) {
if (!glXMakeCurrent(ps->dpy, get_tgt_window(ps), ps->glx_context)) { printf_errf("(): Failed to get GLX context.");
printf_errf("(): Failed to attach GLX context."); goto glx_init_end;
goto glx_init_end; }
// Attach GLX context
if (!glXMakeCurrent(ps->dpy, get_tgt_window(ps), ps->glx_context)) {
printf_errf("(): Failed to attach GLX context.");
goto glx_init_end;
}
} }
// Ensure we have a stencil buffer. X Fixes does not guarantee rectangles // Ensure we have a stencil buffer. X Fixes does not guarantee rectangles
@ -111,6 +113,7 @@ glx_init(session_t *ps, bool need_render) {
if (need_render && !glx_update_fbconfig(ps)) if (need_render && !glx_update_fbconfig(ps))
goto glx_init_end; goto glx_init_end;
// Render preparations
if (need_render) { if (need_render) {
glx_on_root_change(ps); glx_on_root_change(ps);
@ -588,20 +591,44 @@ glx_paint_pre(session_t *ps, XserverRegion *preg) {
ps->glx_z = 0.0; ps->glx_z = 0.0;
// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Exchange swap is interested in the raw damaged region only // Get buffer age
XserverRegion all_damage_last = ps->all_damage_last; int buffer_age = ps->o.glx_swap_method;
ps->all_damage_last = None; bool trace_damage = (ps->o.glx_swap_method < 0 || ps->o.glx_swap_method > 1);
if (SWAPM_EXCHANGE == ps->o.glx_swap_method && *preg)
ps->all_damage_last = copy_region(ps, *preg); // Query GLX_EXT_buffer_age for buffer age
if (SWAPM_BUFFER_AGE == buffer_age) {
unsigned val = 0;
glXQueryDrawable(ps->dpy, get_tgt_window(ps),
GLX_BACK_BUFFER_AGE_EXT, &val);
buffer_age = val;
}
// Buffer age too high
if (buffer_age > CGLX_MAX_BUFFER_AGE + 1)
buffer_age = 0;
// Make sure buffer age >= 0
buffer_age = max_i(buffer_age, 0);
// Trace raw damage regions
XserverRegion newdamage = None;
if (trace_damage && *preg)
newdamage = copy_region(ps, *preg);
// OpenGL doesn't support partial repaint without GLX_MESA_copy_sub_buffer, // OpenGL doesn't support partial repaint without GLX_MESA_copy_sub_buffer,
// we could redraw the whole screen or copy unmodified pixels from // we could redraw the whole screen or copy unmodified pixels from
// front buffer with --glx-copy-from-front. // front buffer with --glx-copy-from-front.
if (ps->o.glx_use_copysubbuffermesa || SWAPM_COPY == ps->o.glx_swap_method if (ps->o.glx_use_copysubbuffermesa || 1 == buffer_age || !*preg) {
|| !*preg) {
} }
else if (SWAPM_EXCHANGE == ps->o.glx_swap_method && all_damage_last) { else if (buffer_age > 1) {
XFixesUnionRegion(ps->dpy, *preg, *preg, all_damage_last); for (int i = 0; i < buffer_age - 1; ++i) {
XserverRegion dmg = ps->all_damage_last[i];
if (!dmg) {
free_region(ps, preg);
break;
}
XFixesUnionRegion(ps->dpy, *preg, *preg, dmg);
}
} }
else if (!ps->o.glx_copy_from_front) { else if (!ps->o.glx_copy_from_front) {
free_region(ps, preg); free_region(ps, preg);
@ -625,7 +652,12 @@ glx_paint_pre(session_t *ps, XserverRegion *preg) {
} }
} }
free_region(ps, &all_damage_last); if (trace_damage) {
free_region(ps, &ps->all_damage_last[CGLX_MAX_BUFFER_AGE - 1]);
memmove(ps->all_damage_last + 1, ps->all_damage_last,
(CGLX_MAX_BUFFER_AGE - 1) * sizeof(XserverRegion));
ps->all_damage_last[0] = newdamage;
}
glx_set_clip(ps, *preg, NULL); glx_set_clip(ps, *preg, NULL);