2018-12-12 10:41:51 +00:00
|
|
|
/*
|
|
|
|
Looking Glass - KVM FrameRelay (KVMFR) Client
|
2021-01-15 01:40:59 +00:00
|
|
|
Copyright (C) 2017-2021 Geoffrey McRae <geoff@hostfission.com>
|
2018-12-12 10:41:51 +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 "desktop.h"
|
2019-04-11 01:12:59 +00:00
|
|
|
#include "common/debug.h"
|
2019-05-23 06:32:18 +00:00
|
|
|
#include "common/option.h"
|
2020-01-17 03:35:08 +00:00
|
|
|
#include "common/locking.h"
|
2018-12-12 10:41:51 +00:00
|
|
|
|
|
|
|
#include "texture.h"
|
|
|
|
#include "shader.h"
|
|
|
|
#include "model.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2019-03-28 13:15:14 +00:00
|
|
|
#include "interface/app.h"
|
|
|
|
|
2019-03-28 03:59:54 +00:00
|
|
|
// these headers are auto generated by cmake
|
|
|
|
#include "desktop.vert.h"
|
|
|
|
#include "desktop_rgb.frag.h"
|
|
|
|
|
2019-05-22 01:36:18 +00:00
|
|
|
struct DesktopShader
|
2018-12-12 10:41:51 +00:00
|
|
|
{
|
2019-05-22 01:36:18 +00:00
|
|
|
EGL_Shader * shader;
|
2018-12-12 10:41:51 +00:00
|
|
|
GLint uDesktopPos;
|
2019-04-19 01:23:51 +00:00
|
|
|
GLint uDesktopSize;
|
2021-01-15 01:40:59 +00:00
|
|
|
GLint uRotate;
|
2019-04-19 01:23:51 +00:00
|
|
|
GLint uNearest;
|
2019-03-28 13:15:14 +00:00
|
|
|
GLint uNV, uNVGain;
|
2020-11-08 19:59:54 +00:00
|
|
|
GLint uCBMode;
|
2019-05-22 01:36:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct EGL_Desktop
|
|
|
|
{
|
2020-10-29 15:32:25 +00:00
|
|
|
EGLDisplay * display;
|
2020-05-21 01:44:56 +00:00
|
|
|
|
2019-05-22 01:36:18 +00:00
|
|
|
EGL_Texture * texture;
|
|
|
|
struct DesktopShader * shader; // the active shader
|
|
|
|
EGL_Model * model;
|
|
|
|
|
2020-05-21 01:44:56 +00:00
|
|
|
// internals
|
2021-01-15 01:40:59 +00:00
|
|
|
int width, height;
|
|
|
|
LG_RendererRotate rotate;
|
2020-05-21 01:44:56 +00:00
|
|
|
|
2019-05-22 01:36:18 +00:00
|
|
|
// shader instances
|
|
|
|
struct DesktopShader shader_generic;
|
2018-12-12 10:41:51 +00:00
|
|
|
|
2019-03-28 13:15:14 +00:00
|
|
|
// night vision
|
|
|
|
KeybindHandle kbNV;
|
2019-05-23 06:32:18 +00:00
|
|
|
int nvMax;
|
2019-03-28 13:15:14 +00:00
|
|
|
int nvGain;
|
2020-11-08 19:59:54 +00:00
|
|
|
|
|
|
|
// colorblind mode
|
|
|
|
int cbMode;
|
2018-12-12 10:41:51 +00:00
|
|
|
};
|
|
|
|
|
2019-03-28 13:15:14 +00:00
|
|
|
// forwards
|
|
|
|
void egl_desktop_toggle_nv(SDL_Scancode key, void * opaque);
|
|
|
|
|
2019-05-22 01:36:18 +00:00
|
|
|
static bool egl_init_desktop_shader(
|
|
|
|
struct DesktopShader * shader,
|
|
|
|
const char * vertex_code , size_t vertex_size,
|
|
|
|
const char * fragment_code, size_t fragment_size
|
|
|
|
)
|
|
|
|
{
|
|
|
|
if (!egl_shader_init(&shader->shader))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!egl_shader_compile(shader->shader,
|
|
|
|
vertex_code , vertex_size,
|
|
|
|
fragment_code, fragment_size))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
shader->uDesktopPos = egl_shader_get_uniform_location(shader->shader, "position");
|
|
|
|
shader->uDesktopSize = egl_shader_get_uniform_location(shader->shader, "size" );
|
2021-01-15 01:40:59 +00:00
|
|
|
shader->uRotate = egl_shader_get_uniform_location(shader->shader, "rotate" );
|
2019-05-22 01:36:18 +00:00
|
|
|
shader->uNearest = egl_shader_get_uniform_location(shader->shader, "nearest" );
|
|
|
|
shader->uNV = egl_shader_get_uniform_location(shader->shader, "nv" );
|
|
|
|
shader->uNVGain = egl_shader_get_uniform_location(shader->shader, "nvGain" );
|
2020-11-08 19:59:54 +00:00
|
|
|
shader->uCBMode = egl_shader_get_uniform_location(shader->shader, "cbMode" );
|
2019-05-22 01:36:18 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-10-29 15:32:25 +00:00
|
|
|
bool egl_desktop_init(EGL_Desktop ** desktop, EGLDisplay * display)
|
2018-12-12 10:41:51 +00:00
|
|
|
{
|
|
|
|
*desktop = (EGL_Desktop *)malloc(sizeof(EGL_Desktop));
|
|
|
|
if (!*desktop)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to malloc EGL_Desktop");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(*desktop, 0, sizeof(EGL_Desktop));
|
2020-10-29 15:32:25 +00:00
|
|
|
(*desktop)->display = display;
|
2018-12-12 10:41:51 +00:00
|
|
|
|
2020-10-29 15:32:25 +00:00
|
|
|
if (!egl_texture_init(&(*desktop)->texture, display))
|
2018-12-12 10:41:51 +00:00
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to initialize the desktop texture");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-22 01:36:18 +00:00
|
|
|
if (!egl_init_desktop_shader(
|
|
|
|
&(*desktop)->shader_generic,
|
|
|
|
b_shader_desktop_vert , b_shader_desktop_vert_size,
|
|
|
|
b_shader_desktop_rgb_frag, b_shader_desktop_rgb_frag_size))
|
2018-12-12 10:41:51 +00:00
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to initialize the generic desktop shader");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!egl_model_init(&(*desktop)->model))
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to initialize the desktop model");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
egl_model_set_default((*desktop)->model);
|
|
|
|
egl_model_set_texture((*desktop)->model, (*desktop)->texture);
|
|
|
|
|
2019-03-28 13:15:14 +00:00
|
|
|
(*desktop)->kbNV = app_register_keybind(SDL_SCANCODE_N, egl_desktop_toggle_nv, *desktop);
|
|
|
|
|
2019-05-23 06:32:18 +00:00
|
|
|
(*desktop)->nvMax = option_get_int("egl", "nvGainMax");
|
|
|
|
(*desktop)->nvGain = option_get_int("egl", "nvGain" );
|
2020-11-08 19:59:54 +00:00
|
|
|
(*desktop)->cbMode = option_get_int("egl", "cbMode" );
|
2019-05-23 06:32:18 +00:00
|
|
|
|
2018-12-12 10:41:51 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-03-28 13:15:14 +00:00
|
|
|
|
|
|
|
void egl_desktop_toggle_nv(SDL_Scancode key, void * opaque)
|
|
|
|
{
|
|
|
|
EGL_Desktop * desktop = (EGL_Desktop *)opaque;
|
2019-05-23 06:32:18 +00:00
|
|
|
if (desktop->nvGain++ == desktop->nvMax)
|
2019-03-28 13:15:14 +00:00
|
|
|
desktop->nvGain = 0;
|
2019-03-30 01:26:06 +00:00
|
|
|
|
2019-03-30 13:08:52 +00:00
|
|
|
if (desktop->nvGain == 0) app_alert(LG_ALERT_INFO, "NV Disabled");
|
|
|
|
else if (desktop->nvGain == 1) app_alert(LG_ALERT_INFO, "NV Enabled");
|
|
|
|
else app_alert(LG_ALERT_INFO, "NV Gain + %d", desktop->nvGain - 1);
|
2019-03-28 13:15:14 +00:00
|
|
|
}
|
|
|
|
|
2018-12-12 10:41:51 +00:00
|
|
|
void egl_desktop_free(EGL_Desktop ** desktop)
|
|
|
|
{
|
|
|
|
if (!*desktop)
|
|
|
|
return;
|
|
|
|
|
2019-05-22 01:36:18 +00:00
|
|
|
egl_texture_free(&(*desktop)->texture );
|
|
|
|
egl_shader_free (&(*desktop)->shader_generic.shader);
|
|
|
|
egl_model_free (&(*desktop)->model );
|
2018-12-12 10:41:51 +00:00
|
|
|
|
2019-03-28 13:15:14 +00:00
|
|
|
app_release_keybind(&(*desktop)->kbNV);
|
|
|
|
|
2018-12-12 10:41:51 +00:00
|
|
|
free(*desktop);
|
|
|
|
*desktop = NULL;
|
|
|
|
}
|
|
|
|
|
2020-10-29 15:32:25 +00:00
|
|
|
bool egl_desktop_setup(EGL_Desktop * desktop, const LG_RendererFormat format, bool useDMA)
|
2018-12-12 10:41:51 +00:00
|
|
|
{
|
2020-10-12 08:43:29 +00:00
|
|
|
enum EGL_PixelFormat pixFmt;
|
|
|
|
switch(format.type)
|
2018-12-12 10:41:51 +00:00
|
|
|
{
|
2020-10-12 08:43:29 +00:00
|
|
|
case FRAME_TYPE_BGRA:
|
|
|
|
pixFmt = EGL_PF_BGRA;
|
|
|
|
desktop->shader = &desktop->shader_generic;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FRAME_TYPE_RGBA:
|
|
|
|
pixFmt = EGL_PF_RGBA;
|
|
|
|
desktop->shader = &desktop->shader_generic;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FRAME_TYPE_RGBA10:
|
|
|
|
pixFmt = EGL_PF_RGBA10;
|
|
|
|
desktop->shader = &desktop->shader_generic;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FRAME_TYPE_RGBA16F:
|
|
|
|
pixFmt = EGL_PF_RGBA16F;
|
|
|
|
desktop->shader = &desktop->shader_generic;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
DEBUG_ERROR("Unsupported frame format");
|
2020-05-21 01:44:56 +00:00
|
|
|
return false;
|
2018-12-12 10:41:51 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 08:43:29 +00:00
|
|
|
desktop->width = format.width;
|
|
|
|
desktop->height = format.height;
|
|
|
|
|
|
|
|
if (!egl_texture_setup(
|
|
|
|
desktop->texture,
|
|
|
|
pixFmt,
|
|
|
|
format.width,
|
|
|
|
format.height,
|
|
|
|
format.pitch,
|
2020-10-29 15:32:25 +00:00
|
|
|
true, // streaming texture
|
|
|
|
useDMA
|
2020-10-12 08:43:29 +00:00
|
|
|
))
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to setup the desktop texture");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-10-29 15:32:25 +00:00
|
|
|
bool egl_desktop_update(EGL_Desktop * desktop, const FrameBuffer * frame, int dmaFd)
|
2020-10-12 08:43:29 +00:00
|
|
|
{
|
2020-10-29 15:32:25 +00:00
|
|
|
if (dmaFd >= 0)
|
|
|
|
{
|
|
|
|
if (!egl_texture_update_from_dma(desktop->texture, frame, dmaFd))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!egl_texture_update_from_frame(desktop->texture, frame))
|
|
|
|
return false;
|
|
|
|
}
|
2020-05-21 01:44:56 +00:00
|
|
|
|
|
|
|
enum EGL_TexStatus status;
|
|
|
|
if ((status = egl_texture_process(desktop->texture)) != EGL_TEX_STATUS_OK)
|
2018-12-12 10:41:51 +00:00
|
|
|
{
|
2020-05-21 01:44:56 +00:00
|
|
|
if (status != EGL_TEX_STATUS_NOTREADY)
|
|
|
|
DEBUG_ERROR("Failed to process the desktop texture");
|
2018-12-12 10:41:51 +00:00
|
|
|
}
|
2020-05-21 01:44:56 +00:00
|
|
|
|
|
|
|
return true;
|
2018-12-12 10:41:51 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 15:44:56 +00:00
|
|
|
bool egl_desktop_render(EGL_Desktop * desktop, const float x, const float y,
|
|
|
|
const float scaleX, const float scaleY, const bool nearest,
|
|
|
|
LG_RendererRotate rotate)
|
2018-12-12 10:41:51 +00:00
|
|
|
{
|
|
|
|
if (!desktop->shader)
|
2019-06-12 12:36:00 +00:00
|
|
|
return false;
|
|
|
|
|
2021-01-18 17:26:59 +00:00
|
|
|
bool useNearest = nearest;
|
|
|
|
if (!nearest)
|
|
|
|
{
|
|
|
|
switch(rotate)
|
|
|
|
{
|
|
|
|
case LG_ROTATE_90:
|
|
|
|
case LG_ROTATE_270:
|
|
|
|
if (scaleX < 1.0f || scaleY < 1.0f)
|
|
|
|
useNearest = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-22 01:36:18 +00:00
|
|
|
const struct DesktopShader * shader = desktop->shader;
|
|
|
|
egl_shader_use(shader->shader);
|
|
|
|
glUniform4f(shader->uDesktopPos , x, y, scaleX, scaleY);
|
2021-01-18 15:44:56 +00:00
|
|
|
glUniform1i(shader->uRotate , rotate);
|
2021-01-18 17:26:59 +00:00
|
|
|
glUniform1i(shader->uNearest , useNearest ? 1 : 0);
|
2019-05-22 01:36:18 +00:00
|
|
|
glUniform2f(shader->uDesktopSize, desktop->width, desktop->height);
|
2019-04-19 01:23:51 +00:00
|
|
|
|
2019-03-28 13:15:14 +00:00
|
|
|
if (desktop->nvGain)
|
|
|
|
{
|
2019-05-22 01:36:18 +00:00
|
|
|
glUniform1i(shader->uNV, 1);
|
|
|
|
glUniform1f(shader->uNVGain, (float)desktop->nvGain);
|
2019-03-28 13:15:14 +00:00
|
|
|
}
|
|
|
|
else
|
2019-05-22 01:36:18 +00:00
|
|
|
glUniform1i(shader->uNV, 0);
|
2019-03-28 13:15:14 +00:00
|
|
|
|
2020-11-08 19:59:54 +00:00
|
|
|
glUniform1i(shader->uCBMode, desktop->cbMode);
|
2018-12-12 10:41:51 +00:00
|
|
|
egl_model_render(desktop->model);
|
2019-06-12 12:36:00 +00:00
|
|
|
return true;
|
2020-01-17 03:35:08 +00:00
|
|
|
}
|