2018-12-12 07:57:31 +00:00
|
|
|
/*
|
|
|
|
Looking Glass - KVM FrameRelay (KVMFR) Client
|
2019-02-22 11:16:14 +00:00
|
|
|
Copyright (C) 2017-2019 Geoffrey McRae <geoff@hostfission.com>
|
2018-12-12 07:57:31 +00:00
|
|
|
https://looking-glass.hostfission.com
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it under
|
|
|
|
cahe terms of the GNU General Public License as published by the Free Software
|
|
|
|
Foundation; either version 2 of the License, or (at your option) any later
|
|
|
|
version.
|
|
|
|
|
|
|
|
This program 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
|
|
|
|
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
|
|
|
|
Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "cursor.h"
|
2019-04-11 01:12:59 +00:00
|
|
|
#include "common/debug.h"
|
2020-01-17 03:35:08 +00:00
|
|
|
#include "common/locking.h"
|
2020-11-08 19:59:54 +00:00
|
|
|
#include "common/option.h"
|
2018-12-12 07:57:31 +00:00
|
|
|
|
|
|
|
#include "texture.h"
|
|
|
|
#include "shader.h"
|
|
|
|
#include "model.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2019-03-28 04:53:15 +00:00
|
|
|
// these headers are auto generated by cmake
|
|
|
|
#include "cursor.vert.h"
|
|
|
|
#include "cursor_rgb.frag.h"
|
|
|
|
#include "cursor_mono.frag.h"
|
|
|
|
|
2020-11-10 09:42:14 +00:00
|
|
|
struct CursorTex
|
|
|
|
{
|
|
|
|
struct EGL_Texture * texture;
|
|
|
|
struct EGL_Shader * shader;
|
|
|
|
GLuint uMousePos;
|
2021-01-15 01:40:59 +00:00
|
|
|
GLuint uRotate;
|
2020-11-10 09:42:14 +00:00
|
|
|
GLuint uCBMode;
|
|
|
|
};
|
|
|
|
|
2018-12-12 07:57:31 +00:00
|
|
|
struct EGL_Cursor
|
|
|
|
{
|
|
|
|
LG_Lock lock;
|
|
|
|
LG_RendererCursor type;
|
|
|
|
int width;
|
|
|
|
int height;
|
2019-01-01 23:30:19 +00:00
|
|
|
int stride;
|
2018-12-12 07:57:31 +00:00
|
|
|
uint8_t * data;
|
|
|
|
size_t dataSize;
|
|
|
|
bool update;
|
|
|
|
|
|
|
|
// cursor state
|
|
|
|
bool visible;
|
|
|
|
float x, y, w, h;
|
2021-01-15 01:40:59 +00:00
|
|
|
int rotate;
|
2020-11-08 19:59:54 +00:00
|
|
|
int cbMode;
|
2018-12-12 07:57:31 +00:00
|
|
|
|
2020-11-10 09:42:14 +00:00
|
|
|
struct CursorTex norm;
|
|
|
|
struct CursorTex mono;
|
|
|
|
struct EGL_Model * model;
|
2018-12-12 07:57:31 +00:00
|
|
|
};
|
|
|
|
|
2020-11-10 09:42:14 +00:00
|
|
|
static bool egl_cursor_tex_init(struct CursorTex * t,
|
|
|
|
const char * vertex_code , size_t vertex_size,
|
|
|
|
const char * fragment_code, size_t fragment_size)
|
2018-12-12 07:57:31 +00:00
|
|
|
{
|
2020-11-10 09:42:14 +00:00
|
|
|
if (!egl_texture_init(&t->texture, NULL))
|
2018-12-12 07:57:31 +00:00
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to initialize the cursor texture");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-11-10 09:42:14 +00:00
|
|
|
if (!egl_shader_init(&t->shader))
|
2018-12-12 07:57:31 +00:00
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to initialize the cursor shader");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-11-10 09:42:14 +00:00
|
|
|
if (!egl_shader_compile(t->shader,
|
|
|
|
vertex_code, vertex_size, fragment_code, fragment_size))
|
2018-12-12 07:57:31 +00:00
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to compile the cursor shader");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-11-10 09:42:14 +00:00
|
|
|
t->uMousePos = egl_shader_get_uniform_location(t->shader, "mouse" );
|
2021-01-15 01:40:59 +00:00
|
|
|
t->uRotate = egl_shader_get_uniform_location(t->shader, "rotate");
|
2020-11-10 09:42:14 +00:00
|
|
|
t->uCBMode = egl_shader_get_uniform_location(t->shader, "cbMode");
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-01-15 01:40:59 +00:00
|
|
|
static inline void egl_cursor_tex_uniforms(EGL_Cursor * cursor, struct CursorTex * t, bool mono)
|
|
|
|
{
|
|
|
|
float x, y, w, h;
|
|
|
|
|
|
|
|
switch(cursor->rotate)
|
|
|
|
{
|
|
|
|
case LG_ROTATE_UP:
|
|
|
|
x = cursor->x;
|
|
|
|
y = cursor->y;
|
|
|
|
w = cursor->w;
|
|
|
|
h = cursor->h;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LG_ROTATE_DOWN:
|
|
|
|
x = -cursor->x;
|
|
|
|
y = -cursor->y;
|
|
|
|
w = cursor->w;
|
|
|
|
h = cursor->h;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LG_ROTATE_LEFT:
|
|
|
|
x = cursor->y;
|
|
|
|
y = -cursor->x;
|
|
|
|
w = cursor->h;
|
|
|
|
h = cursor->w;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LG_ROTATE_RIGHT:
|
|
|
|
x = -cursor->y;
|
|
|
|
y = cursor->x;
|
|
|
|
w = cursor->h;
|
|
|
|
h = cursor->w;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mono)
|
|
|
|
{
|
|
|
|
glUniform4f(t->uMousePos, x, y, w, h / 2);
|
|
|
|
glUniform1i(t->uRotate , cursor->rotate);
|
|
|
|
glUniform1i(t->uCBMode , cursor->cbMode);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
glUniform4f(t->uMousePos, x, y, w, h);
|
|
|
|
glUniform1i(t->uRotate , cursor->rotate);
|
|
|
|
glUniform1i(t->uCBMode , cursor->cbMode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 09:42:14 +00:00
|
|
|
static void egl_cursor_tex_free(struct CursorTex * t)
|
|
|
|
{
|
|
|
|
egl_texture_free(&t->texture);
|
|
|
|
egl_shader_free (&t->shader );
|
|
|
|
};
|
|
|
|
|
|
|
|
bool egl_cursor_init(EGL_Cursor ** cursor)
|
|
|
|
{
|
|
|
|
*cursor = (EGL_Cursor *)malloc(sizeof(EGL_Cursor));
|
|
|
|
if (!*cursor)
|
2018-12-12 07:57:31 +00:00
|
|
|
{
|
2020-11-10 09:42:14 +00:00
|
|
|
DEBUG_ERROR("Failed to malloc EGL_Cursor");
|
2018-12-12 07:57:31 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-11-10 09:42:14 +00:00
|
|
|
memset(*cursor, 0, sizeof(EGL_Cursor));
|
|
|
|
LG_LOCK_INIT((*cursor)->lock);
|
|
|
|
|
|
|
|
if (!egl_cursor_tex_init(&(*cursor)->norm,
|
|
|
|
b_shader_cursor_vert , b_shader_cursor_vert_size,
|
|
|
|
b_shader_cursor_rgb_frag, b_shader_cursor_rgb_frag_size))
|
|
|
|
return false;
|
2020-11-08 19:59:54 +00:00
|
|
|
|
2020-11-10 09:42:14 +00:00
|
|
|
if (!egl_cursor_tex_init(&(*cursor)->mono,
|
|
|
|
b_shader_cursor_vert , b_shader_cursor_vert_size,
|
|
|
|
b_shader_cursor_mono_frag, b_shader_cursor_mono_frag_size))
|
|
|
|
return false;
|
2018-12-12 07:57:31 +00:00
|
|
|
|
|
|
|
if (!egl_model_init(&(*cursor)->model))
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to initialize the cursor model");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-12-12 09:08:52 +00:00
|
|
|
egl_model_set_default((*cursor)->model);
|
2020-11-08 19:59:54 +00:00
|
|
|
|
|
|
|
(*cursor)->cbMode = option_get_int("egl", "cbMode");
|
|
|
|
|
2018-12-12 07:57:31 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void egl_cursor_free(EGL_Cursor ** cursor)
|
|
|
|
{
|
|
|
|
if (!*cursor)
|
|
|
|
return;
|
|
|
|
|
|
|
|
LG_LOCK_FREE((*cursor)->lock);
|
|
|
|
if ((*cursor)->data)
|
|
|
|
free((*cursor)->data);
|
|
|
|
|
2020-11-10 09:42:14 +00:00
|
|
|
egl_cursor_tex_free(&(*cursor)->norm);
|
|
|
|
egl_cursor_tex_free(&(*cursor)->mono);
|
|
|
|
egl_model_free(&(*cursor)->model);
|
2018-12-12 07:57:31 +00:00
|
|
|
|
|
|
|
free(*cursor);
|
|
|
|
*cursor = NULL;
|
|
|
|
}
|
|
|
|
|
2021-01-15 01:40:59 +00:00
|
|
|
bool egl_cursor_set_shape(EGL_Cursor * cursor, const LG_RendererCursor type,
|
|
|
|
const int width, const int height, const LG_RendererRotate rotate,
|
|
|
|
const int stride, const uint8_t * data)
|
2018-12-12 07:57:31 +00:00
|
|
|
{
|
|
|
|
LG_LOCK(cursor->lock);
|
|
|
|
|
|
|
|
cursor->type = type;
|
|
|
|
cursor->width = width;
|
|
|
|
cursor->height = (type == LG_CURSOR_MONOCHROME ? height / 2 : height);
|
2021-01-15 01:40:59 +00:00
|
|
|
cursor->stride = stride;
|
|
|
|
cursor->rotate = rotate;
|
2018-12-12 07:57:31 +00:00
|
|
|
|
2019-01-01 23:30:19 +00:00
|
|
|
const size_t size = height * stride;
|
2018-12-12 07:57:31 +00:00
|
|
|
if (size > cursor->dataSize)
|
|
|
|
{
|
|
|
|
if (cursor->data)
|
|
|
|
free(cursor->data);
|
|
|
|
|
|
|
|
cursor->data = (uint8_t *)malloc(size);
|
|
|
|
if (!cursor->data)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to malloc buffer for cursor shape");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
cursor->dataSize = size;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(cursor->data, data, size);
|
|
|
|
cursor->update = true;
|
|
|
|
|
|
|
|
LG_UNLOCK(cursor->lock);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void egl_cursor_set_size(EGL_Cursor * cursor, const float w, const float h)
|
|
|
|
{
|
|
|
|
cursor->w = w;
|
|
|
|
cursor->h = h;
|
|
|
|
}
|
|
|
|
|
|
|
|
void egl_cursor_set_state(EGL_Cursor * cursor, const bool visible, const float x, const float y)
|
|
|
|
{
|
|
|
|
cursor->visible = visible;
|
|
|
|
cursor->x = x;
|
|
|
|
cursor->y = y;
|
|
|
|
}
|
|
|
|
|
|
|
|
void egl_cursor_render(EGL_Cursor * cursor)
|
|
|
|
{
|
|
|
|
if (!cursor->visible)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (cursor->update)
|
|
|
|
{
|
|
|
|
LG_LOCK(cursor->lock);
|
|
|
|
cursor->update = false;
|
|
|
|
|
|
|
|
uint8_t * data = cursor->data;
|
|
|
|
|
|
|
|
switch(cursor->type)
|
|
|
|
{
|
|
|
|
case LG_CURSOR_MASKED_COLOR:
|
2019-10-09 08:48:42 +00:00
|
|
|
// fall through
|
2018-12-12 07:57:31 +00:00
|
|
|
|
|
|
|
case LG_CURSOR_COLOR:
|
|
|
|
{
|
2020-11-10 09:42:14 +00:00
|
|
|
egl_texture_setup(cursor->norm.texture, EGL_PF_BGRA, cursor->width, cursor->height, cursor->stride, false, false);
|
|
|
|
egl_texture_update(cursor->norm.texture, data);
|
|
|
|
egl_model_set_texture(cursor->model, cursor->norm.texture);
|
2018-12-12 07:57:31 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case LG_CURSOR_MONOCHROME:
|
|
|
|
{
|
|
|
|
uint32_t and[cursor->width * cursor->height];
|
|
|
|
uint32_t xor[cursor->width * cursor->height];
|
|
|
|
|
|
|
|
for(int y = 0; y < cursor->height; ++y)
|
|
|
|
for(int x = 0; x < cursor->width; ++x)
|
|
|
|
{
|
2019-01-01 23:30:19 +00:00
|
|
|
const uint8_t * srcAnd = data + (cursor->stride * y) + (x / 8);
|
|
|
|
const uint8_t * srcXor = srcAnd + cursor->stride * cursor->height;
|
2018-12-12 07:57:31 +00:00
|
|
|
const uint8_t mask = 0x80 >> (x % 8);
|
|
|
|
const uint32_t andMask = (*srcAnd & mask) ? 0xFFFFFFFF : 0xFF000000;
|
|
|
|
const uint32_t xorMask = (*srcXor & mask) ? 0x00FFFFFF : 0x00000000;
|
|
|
|
|
|
|
|
and[y * cursor->width + x] = andMask;
|
|
|
|
xor[y * cursor->width + x] = xorMask;
|
|
|
|
}
|
|
|
|
|
2021-01-15 01:40:59 +00:00
|
|
|
egl_texture_setup (cursor->norm.texture, EGL_PF_BGRA, cursor->width, cursor->height, cursor->width * 4, false, false);
|
2020-11-10 09:42:14 +00:00
|
|
|
egl_texture_setup (cursor->mono.texture, EGL_PF_BGRA, cursor->width, cursor->height, cursor->width * 4, false, false);
|
2021-01-15 01:40:59 +00:00
|
|
|
egl_texture_update(cursor->norm.texture, (uint8_t *)and);
|
2020-11-10 09:42:14 +00:00
|
|
|
egl_texture_update(cursor->mono.texture, (uint8_t *)xor);
|
2018-12-12 07:57:31 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LG_UNLOCK(cursor->lock);
|
|
|
|
}
|
|
|
|
|
2019-10-09 08:48:42 +00:00
|
|
|
glEnable(GL_BLEND);
|
|
|
|
switch(cursor->type)
|
2018-12-12 07:57:31 +00:00
|
|
|
{
|
2019-10-09 08:48:42 +00:00
|
|
|
case LG_CURSOR_MONOCHROME:
|
|
|
|
{
|
2020-11-10 09:42:14 +00:00
|
|
|
egl_shader_use(cursor->norm.shader);
|
2021-01-15 01:40:59 +00:00
|
|
|
egl_cursor_tex_uniforms(cursor, &cursor->norm, true);;
|
2019-10-09 08:48:42 +00:00
|
|
|
glBlendFunc(GL_ZERO, GL_SRC_COLOR);
|
2020-11-10 09:42:14 +00:00
|
|
|
egl_model_set_texture(cursor->model, cursor->norm.texture);
|
2019-10-09 08:48:42 +00:00
|
|
|
egl_model_render(cursor->model);
|
|
|
|
|
2020-11-10 09:42:14 +00:00
|
|
|
egl_shader_use(cursor->mono.shader);
|
2021-01-15 01:40:59 +00:00
|
|
|
egl_cursor_tex_uniforms(cursor, &cursor->mono, true);;
|
2019-10-09 08:48:42 +00:00
|
|
|
glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO);
|
2020-11-10 09:42:14 +00:00
|
|
|
egl_model_set_texture(cursor->model, cursor->mono.texture);
|
2019-10-09 08:48:42 +00:00
|
|
|
egl_model_render(cursor->model);
|
|
|
|
break;
|
|
|
|
}
|
2018-12-12 14:39:52 +00:00
|
|
|
|
2019-10-09 08:48:42 +00:00
|
|
|
case LG_CURSOR_COLOR:
|
|
|
|
{
|
2020-11-10 09:42:14 +00:00
|
|
|
egl_shader_use(cursor->norm.shader);
|
2021-01-15 01:40:59 +00:00
|
|
|
egl_cursor_tex_uniforms(cursor, &cursor->norm, false);
|
2019-10-09 08:48:42 +00:00
|
|
|
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
|
|
|
|
egl_model_render(cursor->model);
|
|
|
|
break;
|
|
|
|
}
|
2018-12-12 14:39:52 +00:00
|
|
|
|
2019-10-09 08:48:42 +00:00
|
|
|
case LG_CURSOR_MASKED_COLOR:
|
|
|
|
{
|
2020-11-10 09:42:14 +00:00
|
|
|
egl_shader_use(cursor->mono.shader);
|
2021-01-15 01:40:59 +00:00
|
|
|
egl_cursor_tex_uniforms(cursor, &cursor->mono, false);
|
2019-10-09 08:48:42 +00:00
|
|
|
glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO);
|
|
|
|
egl_model_render(cursor->model);
|
|
|
|
break;
|
|
|
|
}
|
2018-12-12 07:57:31 +00:00
|
|
|
}
|
2019-10-09 08:48:42 +00:00
|
|
|
glDisable(GL_BLEND);
|
2020-01-17 03:35:08 +00:00
|
|
|
}
|