mirror of
https://github.com/gnif/LookingGlass.git
synced 2024-11-14 21:17:54 +00:00
[client] egl: improve cursor damage logic
1. Use atomics and return exact cursor positions from egl_cursor_render to avoid race conditions between cursor render and update. 2. Instead of messing with lastCursorValid in various overlays, simply use the hasOverlay/hadOverlay logic for cursor damage. This simplifies the logic greatly. As a result, I believe all cursor-related artifacts are fixed. Note to reviewer: as atomic_init and atomic_store are implemented as macros, it is currently not possible to pass structs as compound literals due to the comma being interpreted as an argument separator by the preprocessor.
This commit is contained in:
parent
b4dc021381
commit
563ad18f4e
3 changed files with 58 additions and 59 deletions
|
@ -27,6 +27,7 @@
|
||||||
#include "shader.h"
|
#include "shader.h"
|
||||||
#include "model.h"
|
#include "model.h"
|
||||||
|
|
||||||
|
#include <stdatomic.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
@ -44,6 +45,16 @@ struct CursorTex
|
||||||
GLuint uCBMode;
|
GLuint uCBMode;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct CursorPos
|
||||||
|
{
|
||||||
|
float x, y;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct CursorSize
|
||||||
|
{
|
||||||
|
float w, h;
|
||||||
|
};
|
||||||
|
|
||||||
struct EGL_Cursor
|
struct EGL_Cursor
|
||||||
{
|
{
|
||||||
LG_Lock lock;
|
LG_Lock lock;
|
||||||
|
@ -57,10 +68,12 @@ struct EGL_Cursor
|
||||||
|
|
||||||
// cursor state
|
// cursor state
|
||||||
bool visible;
|
bool visible;
|
||||||
float x, y, w, h;
|
|
||||||
LG_RendererRotate rotate;
|
LG_RendererRotate rotate;
|
||||||
int cbMode;
|
int cbMode;
|
||||||
|
|
||||||
|
_Atomic(struct CursorPos) pos;
|
||||||
|
_Atomic(struct CursorSize) size;
|
||||||
|
|
||||||
struct CursorTex norm;
|
struct CursorTex norm;
|
||||||
struct CursorTex mono;
|
struct CursorTex mono;
|
||||||
struct EGL_Model * model;
|
struct EGL_Model * model;
|
||||||
|
@ -96,17 +109,18 @@ static bool egl_cursor_tex_init(struct CursorTex * t,
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void egl_cursor_tex_uniforms(EGL_Cursor * cursor, struct CursorTex * t, bool mono)
|
static inline void egl_cursor_tex_uniforms(EGL_Cursor * cursor, struct CursorTex * t,
|
||||||
|
bool mono, float x, float y, float w, float h)
|
||||||
{
|
{
|
||||||
if (mono)
|
if (mono)
|
||||||
{
|
{
|
||||||
glUniform4f(t->uMousePos, cursor->x, cursor->y, cursor->w, cursor->h / 2);
|
glUniform4f(t->uMousePos, x, y, w, h / 2);
|
||||||
glUniform1i(t->uRotate , cursor->rotate);
|
glUniform1i(t->uRotate , cursor->rotate);
|
||||||
glUniform1i(t->uCBMode , cursor->cbMode);
|
glUniform1i(t->uCBMode , cursor->cbMode);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
glUniform4f(t->uMousePos, cursor->x, cursor->y, cursor->w, cursor->h);
|
glUniform4f(t->uMousePos, x, y, w, h);
|
||||||
glUniform1i(t->uRotate , cursor->rotate);
|
glUniform1i(t->uRotate , cursor->rotate);
|
||||||
glUniform1i(t->uCBMode , cursor->cbMode);
|
glUniform1i(t->uCBMode , cursor->cbMode);
|
||||||
}
|
}
|
||||||
|
@ -150,6 +164,11 @@ bool egl_cursor_init(EGL_Cursor ** cursor)
|
||||||
|
|
||||||
(*cursor)->cbMode = option_get_int("egl", "cbMode");
|
(*cursor)->cbMode = option_get_int("egl", "cbMode");
|
||||||
|
|
||||||
|
struct CursorPos pos = { .x = 0, .y = 0 };
|
||||||
|
struct CursorSize size = { .w = 0, .h = 0 };
|
||||||
|
atomic_init(&(*cursor)->pos, pos);
|
||||||
|
atomic_init(&(*cursor)->size, size);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -205,31 +224,22 @@ bool egl_cursor_set_shape(EGL_Cursor * cursor, const LG_RendererCursor type,
|
||||||
|
|
||||||
void egl_cursor_set_size(EGL_Cursor * cursor, const float w, const float h)
|
void egl_cursor_set_size(EGL_Cursor * cursor, const float w, const float h)
|
||||||
{
|
{
|
||||||
cursor->w = w;
|
struct CursorSize size = { .w = w, .h = h };
|
||||||
cursor->h = h;
|
atomic_store(&cursor->size, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
void egl_cursor_set_state(EGL_Cursor * cursor, const bool visible, const float x, const float y)
|
void egl_cursor_set_state(EGL_Cursor * cursor, const bool visible, const float x, const float y)
|
||||||
{
|
{
|
||||||
cursor->visible = visible;
|
cursor->visible = visible;
|
||||||
cursor->x = x;
|
struct CursorPos pos = { .x = x, .y = y};
|
||||||
cursor->y = y;
|
atomic_store(&cursor->pos, pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct CursorState egl_cursor_get_state(EGL_Cursor * cursor, int width, int height) {
|
struct CursorState egl_cursor_render(EGL_Cursor * cursor, LG_RendererRotate rotate,
|
||||||
return (struct CursorState) {
|
int width, int height)
|
||||||
.visible = cursor->visible,
|
|
||||||
.rect.x = (cursor->x * width + width) / 2,
|
|
||||||
.rect.y = (-cursor->y * height + height) / 2 - cursor->h * height,
|
|
||||||
.rect.w = cursor->w * width + 2,
|
|
||||||
.rect.h = cursor->h * height + 2,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
void egl_cursor_render(EGL_Cursor * cursor, LG_RendererRotate rotate)
|
|
||||||
{
|
{
|
||||||
if (!cursor->visible)
|
if (!cursor->visible)
|
||||||
return;
|
return (struct CursorState) { .visible = false };
|
||||||
|
|
||||||
if (cursor->update)
|
if (cursor->update)
|
||||||
{
|
{
|
||||||
|
@ -281,19 +291,30 @@ void egl_cursor_render(EGL_Cursor * cursor, LG_RendererRotate rotate)
|
||||||
|
|
||||||
cursor->rotate = rotate;
|
cursor->rotate = rotate;
|
||||||
|
|
||||||
|
struct CursorPos pos = atomic_load(&cursor->pos);
|
||||||
|
struct CursorSize size = atomic_load(&cursor->size);
|
||||||
|
|
||||||
|
struct CursorState state = {
|
||||||
|
.visible = true,
|
||||||
|
.rect.x = (pos.x * width + width) / 2,
|
||||||
|
.rect.y = (-pos.y * height + height) / 2 - size.h * height,
|
||||||
|
.rect.w = size.w * width + 2,
|
||||||
|
.rect.h = size.h * height + 2,
|
||||||
|
};
|
||||||
|
|
||||||
glEnable(GL_BLEND);
|
glEnable(GL_BLEND);
|
||||||
switch(cursor->type)
|
switch(cursor->type)
|
||||||
{
|
{
|
||||||
case LG_CURSOR_MONOCHROME:
|
case LG_CURSOR_MONOCHROME:
|
||||||
{
|
{
|
||||||
egl_shader_use(cursor->norm.shader);
|
egl_shader_use(cursor->norm.shader);
|
||||||
egl_cursor_tex_uniforms(cursor, &cursor->norm, true);;
|
egl_cursor_tex_uniforms(cursor, &cursor->norm, true, pos.x, pos.y, size.w, size.h);
|
||||||
glBlendFunc(GL_ZERO, GL_SRC_COLOR);
|
glBlendFunc(GL_ZERO, GL_SRC_COLOR);
|
||||||
egl_model_set_texture(cursor->model, cursor->norm.texture);
|
egl_model_set_texture(cursor->model, cursor->norm.texture);
|
||||||
egl_model_render(cursor->model);
|
egl_model_render(cursor->model);
|
||||||
|
|
||||||
egl_shader_use(cursor->mono.shader);
|
egl_shader_use(cursor->mono.shader);
|
||||||
egl_cursor_tex_uniforms(cursor, &cursor->mono, true);;
|
egl_cursor_tex_uniforms(cursor, &cursor->mono, true, pos.x, pos.y, size.w, size.h);
|
||||||
glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO);
|
glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO);
|
||||||
egl_model_set_texture(cursor->model, cursor->mono.texture);
|
egl_model_set_texture(cursor->model, cursor->mono.texture);
|
||||||
egl_model_render(cursor->model);
|
egl_model_render(cursor->model);
|
||||||
|
@ -303,7 +324,7 @@ void egl_cursor_render(EGL_Cursor * cursor, LG_RendererRotate rotate)
|
||||||
case LG_CURSOR_COLOR:
|
case LG_CURSOR_COLOR:
|
||||||
{
|
{
|
||||||
egl_shader_use(cursor->norm.shader);
|
egl_shader_use(cursor->norm.shader);
|
||||||
egl_cursor_tex_uniforms(cursor, &cursor->norm, false);
|
egl_cursor_tex_uniforms(cursor, &cursor->norm, false, pos.x, pos.y, size.w, size.h);
|
||||||
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
|
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
|
||||||
egl_model_render(cursor->model);
|
egl_model_render(cursor->model);
|
||||||
break;
|
break;
|
||||||
|
@ -312,11 +333,13 @@ void egl_cursor_render(EGL_Cursor * cursor, LG_RendererRotate rotate)
|
||||||
case LG_CURSOR_MASKED_COLOR:
|
case LG_CURSOR_MASKED_COLOR:
|
||||||
{
|
{
|
||||||
egl_shader_use(cursor->mono.shader);
|
egl_shader_use(cursor->mono.shader);
|
||||||
egl_cursor_tex_uniforms(cursor, &cursor->mono, false);
|
egl_cursor_tex_uniforms(cursor, &cursor->mono, false, pos.x, pos.y, size.w, size.h);
|
||||||
glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO);
|
glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO);
|
||||||
egl_model_render(cursor->model);
|
egl_model_render(cursor->model);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
glDisable(GL_BLEND);
|
glDisable(GL_BLEND);
|
||||||
|
|
||||||
|
return state;
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,6 +47,5 @@ void egl_cursor_set_size(EGL_Cursor * cursor, const float x, const float y);
|
||||||
void egl_cursor_set_state(EGL_Cursor * cursor, const bool visible,
|
void egl_cursor_set_state(EGL_Cursor * cursor, const bool visible,
|
||||||
const float x, const float y);
|
const float x, const float y);
|
||||||
|
|
||||||
struct CursorState egl_cursor_get_state(EGL_Cursor * cursor, int width, int height);
|
struct CursorState egl_cursor_render(EGL_Cursor * cursor, LG_RendererRotate rotate,
|
||||||
|
int width, int height);
|
||||||
void egl_cursor_render(EGL_Cursor * cursor, LG_RendererRotate rotate);
|
|
||||||
|
|
|
@ -116,7 +116,6 @@ struct Inst
|
||||||
LG_FontObj helpFontObj;
|
LG_FontObj helpFontObj;
|
||||||
unsigned helpFontSize;
|
unsigned helpFontSize;
|
||||||
|
|
||||||
bool cursorLastValid;
|
|
||||||
struct CursorState cursorLast;
|
struct CursorState cursorLast;
|
||||||
|
|
||||||
bool hadOverlay;
|
bool hadOverlay;
|
||||||
|
@ -496,8 +495,6 @@ void egl_on_resize(void * opaque, const int width, const int height, const doubl
|
||||||
egl_update_font(this);
|
egl_update_font(this);
|
||||||
egl_update_help_font(this);
|
egl_update_help_font(this);
|
||||||
|
|
||||||
this->cursorLastValid = false;
|
|
||||||
|
|
||||||
struct DesktopDamage * damage = malloc(sizeof(struct DesktopDamage));
|
struct DesktopDamage * damage = malloc(sizeof(struct DesktopDamage));
|
||||||
if (!damage)
|
if (!damage)
|
||||||
{
|
{
|
||||||
|
@ -584,7 +581,6 @@ bool egl_on_frame(void * opaque, const FrameBuffer * frame, int dmaFd,
|
||||||
}
|
}
|
||||||
|
|
||||||
this->start = true;
|
this->start = true;
|
||||||
this->cursorLastValid = false;
|
|
||||||
|
|
||||||
struct DesktopDamage * damage = malloc(sizeof(struct DesktopDamage));
|
struct DesktopDamage * damage = malloc(sizeof(struct DesktopDamage));
|
||||||
if (!damage)
|
if (!damage)
|
||||||
|
@ -632,14 +628,12 @@ void egl_on_alert(void * opaque, const LG_MsgAlert alert, const char * message,
|
||||||
}
|
}
|
||||||
|
|
||||||
this->showAlert = true;
|
this->showAlert = true;
|
||||||
this->cursorLastValid = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void egl_on_help(void * opaque, const char * message)
|
void egl_on_help(void * opaque, const char * message)
|
||||||
{
|
{
|
||||||
struct Inst * this = (struct Inst *)opaque;
|
struct Inst * this = (struct Inst *)opaque;
|
||||||
egl_help_set_text(this->help, message);
|
egl_help_set_text(this->help, message);
|
||||||
this->cursorLastValid = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void debugCallback(GLenum source, GLenum type, GLuint id,
|
static void debugCallback(GLenum source, GLenum type, GLuint id,
|
||||||
|
@ -936,10 +930,8 @@ bool egl_render(void * opaque, LG_RendererRotate rotate)
|
||||||
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
||||||
glClear(GL_COLOR_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
|
|
||||||
bool hasLastCursor = this->cursorLastValid;
|
|
||||||
bool cursorRendered = false;
|
|
||||||
bool hasOverlay = false;
|
bool hasOverlay = false;
|
||||||
struct CursorState cursorState;
|
struct CursorState cursorState = { .visible = false };
|
||||||
struct DesktopDamage * desktopDamage = NULL;
|
struct DesktopDamage * desktopDamage = NULL;
|
||||||
|
|
||||||
if (this->start)
|
if (this->start)
|
||||||
|
@ -958,10 +950,9 @@ bool egl_render(void * opaque, LG_RendererRotate rotate)
|
||||||
this->waitDone = true;
|
this->waitDone = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
cursorRendered = true;
|
cursorState = egl_cursor_render(this->cursor,
|
||||||
cursorState = egl_cursor_get_state(this->cursor, this->width, this->height);
|
(this->format.rotate + rotate) % LG_ROTATE_MAX,
|
||||||
egl_cursor_render(this->cursor,
|
this->width, this->height);
|
||||||
(this->format.rotate + rotate) % LG_ROTATE_MAX);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
desktopDamage->count = 0;
|
desktopDamage->count = 0;
|
||||||
|
@ -1005,10 +996,7 @@ bool egl_render(void * opaque, LG_RendererRotate rotate)
|
||||||
close = true;
|
close = true;
|
||||||
|
|
||||||
if (close)
|
if (close)
|
||||||
{
|
|
||||||
this->showAlert = false;
|
this->showAlert = false;
|
||||||
this->cursorLastValid = false;
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
egl_alert_render(this->alert, this->screenScaleX, this->screenScaleY);
|
egl_alert_render(this->alert, this->screenScaleX, this->screenScaleY);
|
||||||
|
@ -1031,24 +1019,13 @@ bool egl_render(void * opaque, LG_RendererRotate rotate)
|
||||||
|
|
||||||
if (!hasOverlay && !this->hadOverlay)
|
if (!hasOverlay && !this->hadOverlay)
|
||||||
{
|
{
|
||||||
if (cursorRendered && hasLastCursor)
|
if (this->cursorLast.visible)
|
||||||
{
|
damage[damageIdx++] = this->cursorLast.rect;
|
||||||
if (this->cursorLast.visible)
|
|
||||||
damage[damageIdx++] = this->cursorLast.rect;
|
|
||||||
|
|
||||||
if (cursorState.visible)
|
if (cursorState.visible)
|
||||||
damage[damageIdx++] = cursorState.rect;
|
damage[damageIdx++] = cursorState.rect;
|
||||||
|
|
||||||
this->cursorLast = cursorState;
|
this->cursorLast = cursorState;
|
||||||
}
|
|
||||||
else if (cursorRendered)
|
|
||||||
{
|
|
||||||
this->cursorLast = cursorState;
|
|
||||||
this->cursorLastValid = true;
|
|
||||||
|
|
||||||
if (cursorState.visible)
|
|
||||||
damage[damageIdx++] = cursorState.rect;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (desktopDamage)
|
if (desktopDamage)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue