2021-06-06 01:26:18 +00:00
|
|
|
/**
|
|
|
|
* Looking Glass
|
2021-08-04 09:48:32 +00:00
|
|
|
* Copyright © 2017-2021 The Looking Glass Authors
|
2021-06-06 01:26:18 +00:00
|
|
|
* https://looking-glass.io
|
|
|
|
*
|
|
|
|
* This program 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 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
|
|
|
|
*/
|
2018-12-12 10:41:51 +00:00
|
|
|
|
|
|
|
#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
|
|
|
|
2021-01-25 15:33:28 +00:00
|
|
|
#include "app.h"
|
2018-12-12 10:41:51 +00:00
|
|
|
#include "texture.h"
|
|
|
|
#include "shader.h"
|
2021-08-03 00:45:47 +00:00
|
|
|
#include "desktop_rects.h"
|
2021-08-06 07:48:07 +00:00
|
|
|
#include "cimgui.h"
|
2018-12-12 10:41:51 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.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"
|
2021-02-22 04:56:51 +00:00
|
|
|
#include "desktop_rgb.def.h"
|
2021-02-21 00:50:59 +00:00
|
|
|
|
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;
|
2021-08-03 00:45:47 +00:00
|
|
|
GLint uTransform;
|
|
|
|
GLint uUVScale;
|
2019-04-19 01:23:51 +00:00
|
|
|
GLint uDesktopSize;
|
2021-02-21 00:50:59 +00:00
|
|
|
GLint uScaleAlgo;
|
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
|
2021-08-03 00:45:47 +00:00
|
|
|
EGL_DesktopRects * mesh;
|
2019-05-22 01:36:18 +00:00
|
|
|
|
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
|
|
|
|
2021-02-22 04:56:51 +00:00
|
|
|
// scale algorithm
|
|
|
|
int scaleAlgo;
|
|
|
|
|
2019-03-28 13:15:14 +00:00
|
|
|
// night vision
|
2021-01-25 15:33:28 +00:00
|
|
|
int nvMax;
|
|
|
|
int nvGain;
|
2020-11-08 19:59:54 +00:00
|
|
|
|
|
|
|
// colorblind mode
|
2021-01-25 15:33:28 +00:00
|
|
|
int cbMode;
|
2021-08-07 23:49:55 +00:00
|
|
|
|
|
|
|
bool useDMA;
|
|
|
|
LG_RendererFormat format;
|
2018-12-12 10:41:51 +00:00
|
|
|
};
|
|
|
|
|
2019-03-28 13:15:14 +00:00
|
|
|
// forwards
|
2021-01-25 15:51:03 +00:00
|
|
|
void egl_desktop_toggle_nv(int key, void * opaque);
|
2019-03-28 13:15:14 +00:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-08-03 00:45:47 +00:00
|
|
|
egl_shader_associate_textures(shader->shader, 1);
|
|
|
|
|
|
|
|
shader->uTransform = egl_shader_get_uniform_location(shader->shader, "transform");
|
|
|
|
shader->uUVScale = egl_shader_get_uniform_location(shader->shader, "uvScale" );
|
2021-02-21 00:50:59 +00:00
|
|
|
shader->uDesktopSize = egl_shader_get_uniform_location(shader->shader, "size" );
|
|
|
|
shader->uScaleAlgo = egl_shader_get_uniform_location(shader->shader, "scaleAlgo");
|
|
|
|
shader->uNV = egl_shader_get_uniform_location(shader->shader, "nv" );
|
|
|
|
shader->uNVGain = egl_shader_get_uniform_location(shader->shader, "nvGain" );
|
|
|
|
shader->uCBMode = egl_shader_get_uniform_location(shader->shader, "cbMode" );
|
2019-05-22 01:36:18 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-08-03 00:45:47 +00:00
|
|
|
bool egl_desktop_init(EGL_Desktop ** desktop, EGLDisplay * display, bool useDMA, int maxRects)
|
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
|
|
|
|
2021-08-02 13:37:33 +00:00
|
|
|
if (!egl_texture_init(&(*desktop)->texture, display,
|
|
|
|
useDMA ? EGL_TEXTYPE_DMABUF : EGL_TEXTYPE_FRAMEBUFFER, true))
|
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;
|
|
|
|
}
|
|
|
|
|
2021-08-03 00:45:47 +00:00
|
|
|
if (!egl_desktopRectsInit(&(*desktop)->mesh, maxRects))
|
2018-12-12 10:41:51 +00:00
|
|
|
{
|
2021-08-03 00:45:47 +00:00
|
|
|
DEBUG_ERROR("Failed to initialize the desktop mesh");
|
2018-12-12 10:41:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-01-31 03:48:24 +00:00
|
|
|
app_registerKeybind(KEY_N, egl_desktop_toggle_nv, *desktop, "Toggle night vision mode");
|
2019-03-28 13:15:14 +00:00
|
|
|
|
2021-02-22 05:03:50 +00:00
|
|
|
(*desktop)->nvMax = option_get_int("egl", "nvGainMax");
|
|
|
|
(*desktop)->nvGain = option_get_int("egl", "nvGain" );
|
|
|
|
(*desktop)->cbMode = option_get_int("egl", "cbMode" );
|
|
|
|
(*desktop)->scaleAlgo = option_get_int("egl", "scale" );
|
2021-08-07 23:49:55 +00:00
|
|
|
(*desktop)->useDMA = useDMA;
|
2019-05-23 06:32:18 +00:00
|
|
|
|
2018-12-12 10:41:51 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-01-25 15:51:03 +00:00
|
|
|
void egl_desktop_toggle_nv(int key, void * opaque)
|
2019-03-28 13:15:14 +00:00
|
|
|
{
|
|
|
|
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);
|
2021-08-07 22:41:08 +00:00
|
|
|
|
|
|
|
app_invalidateWindow(true);
|
2019-03-28 13:15:14 +00:00
|
|
|
}
|
|
|
|
|
2021-02-22 05:03:50 +00:00
|
|
|
bool egl_desktop_scale_validate(struct Option * opt, const char ** error)
|
|
|
|
{
|
|
|
|
if (opt->value.x_int >= 0 && opt->value.x_int < EGL_SCALE_MAX)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
*error = "Invalid scale algorithm number";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-12-12 10:41:51 +00:00
|
|
|
void egl_desktop_free(EGL_Desktop ** desktop)
|
|
|
|
{
|
|
|
|
if (!*desktop)
|
|
|
|
return;
|
|
|
|
|
2021-08-03 00:45:47 +00:00
|
|
|
egl_texture_free (&(*desktop)->texture );
|
|
|
|
egl_shader_free (&(*desktop)->shader_generic.shader);
|
|
|
|
egl_desktopRectsFree(&(*desktop)->mesh );
|
2018-12-12 10:41:51 +00:00
|
|
|
|
|
|
|
free(*desktop);
|
|
|
|
*desktop = NULL;
|
|
|
|
}
|
|
|
|
|
2021-08-06 07:48:07 +00:00
|
|
|
static const char * algorithmNames[EGL_SCALE_MAX] = {
|
|
|
|
[EGL_SCALE_AUTO] = "Automatic (downscale: linear, upscale: nearest)",
|
|
|
|
[EGL_SCALE_NEAREST] = "Nearest",
|
|
|
|
[EGL_SCALE_LINEAR] = "Linear",
|
|
|
|
};
|
|
|
|
|
|
|
|
void egl_desktop_config_ui(EGL_Desktop * desktop)
|
|
|
|
{
|
|
|
|
igText("Scale algorithm:");
|
|
|
|
igPushItemWidth(igGetWindowWidth() - igGetStyle()->WindowPadding.x * 2);
|
|
|
|
if (igBeginCombo("##scale", algorithmNames[desktop->scaleAlgo], 0))
|
|
|
|
{
|
|
|
|
for (int i = 0; i < EGL_SCALE_MAX; ++i)
|
|
|
|
{
|
|
|
|
bool selected = i == desktop->scaleAlgo;
|
|
|
|
if (igSelectableBool(algorithmNames[i], selected, 0, (ImVec2) { 0.0f, 0.0f }))
|
|
|
|
desktop->scaleAlgo = i;
|
|
|
|
if (selected)
|
|
|
|
igSetItemDefaultFocus();
|
|
|
|
}
|
|
|
|
igEndCombo();
|
|
|
|
}
|
|
|
|
igPopItemWidth();
|
2021-08-07 10:31:05 +00:00
|
|
|
|
|
|
|
igText("Night vision mode:");
|
|
|
|
igSameLine(0.0f, -1.0f);
|
|
|
|
igPushItemWidth(igGetWindowWidth() - igGetCursorPosX() - igGetStyle()->WindowPadding.x);
|
|
|
|
|
|
|
|
const char * format;
|
|
|
|
switch (desktop->nvGain)
|
|
|
|
{
|
|
|
|
case 0: format = "off"; break;
|
|
|
|
case 1: format = "on"; break;
|
|
|
|
default: format = "gain: %d";
|
|
|
|
}
|
|
|
|
igSliderInt("##nvgain", &desktop->nvGain, 0, desktop->nvMax, format, 0);
|
|
|
|
igPopItemWidth();
|
2021-08-06 07:48:07 +00:00
|
|
|
}
|
|
|
|
|
2021-08-02 13:37:33 +00:00
|
|
|
bool egl_desktop_setup(EGL_Desktop * desktop, const LG_RendererFormat format)
|
2018-12-12 10:41:51 +00:00
|
|
|
{
|
2021-08-07 23:49:55 +00:00
|
|
|
memcpy(&desktop->format, &format, sizeof(LG_RendererFormat));
|
|
|
|
|
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,
|
2021-08-02 13:37:33 +00:00
|
|
|
format.pitch
|
2020-10-12 08:43:29 +00:00
|
|
|
))
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to setup the desktop texture");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-08-07 06:10:30 +00:00
|
|
|
bool egl_desktop_update(EGL_Desktop * desktop, const FrameBuffer * frame, int dmaFd,
|
|
|
|
const FrameDamageRect * damageRects, int damageRectsCount)
|
2020-10-12 08:43:29 +00:00
|
|
|
{
|
2021-08-07 23:49:55 +00:00
|
|
|
if (desktop->useDMA && dmaFd >= 0)
|
2020-10-29 15:32:25 +00:00
|
|
|
{
|
2021-08-07 23:49:55 +00:00
|
|
|
if (egl_texture_update_from_dma(desktop->texture, frame, dmaFd))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
DEBUG_WARN("DMA update failed, disabling DMABUF imports");
|
|
|
|
desktop->useDMA = false;
|
|
|
|
|
|
|
|
egl_texture_free(&desktop->texture);
|
|
|
|
if (!egl_texture_init(&desktop->texture, desktop->display, EGL_TEXTYPE_FRAMEBUFFER, true))
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to initialize the desktop texture");
|
2020-10-29 15:32:25 +00:00
|
|
|
return false;
|
2021-08-07 23:49:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!egl_desktop_setup(desktop, desktop->format))
|
2020-10-29 15:32:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-05-21 01:44:56 +00:00
|
|
|
|
2021-08-07 23:49:55 +00:00
|
|
|
return egl_texture_update_from_frame(desktop->texture, frame, damageRects, damageRectsCount);
|
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,
|
2021-02-21 00:50:59 +00:00
|
|
|
const float scaleX, const float scaleY, enum EGL_DesktopScaleType scaleType,
|
2021-08-03 00:45:47 +00:00
|
|
|
LG_RendererRotate rotate, const struct DamageRects * rects)
|
2018-12-12 10:41:51 +00:00
|
|
|
{
|
|
|
|
if (!desktop->shader)
|
2019-06-12 12:36:00 +00:00
|
|
|
return false;
|
|
|
|
|
2021-08-02 13:37:33 +00:00
|
|
|
enum EGL_TexStatus status;
|
|
|
|
if ((status = egl_texture_process(desktop->texture)) != EGL_TEX_STATUS_OK)
|
|
|
|
{
|
|
|
|
if (status != EGL_TEX_STATUS_NOTREADY)
|
|
|
|
DEBUG_ERROR("Failed to process the desktop texture");
|
|
|
|
}
|
|
|
|
|
2021-02-22 04:56:51 +00:00
|
|
|
int scaleAlgo = EGL_SCALE_NEAREST;
|
2021-02-21 00:50:59 +00:00
|
|
|
|
2021-02-22 04:56:51 +00:00
|
|
|
switch (desktop->scaleAlgo)
|
2021-01-18 17:26:59 +00:00
|
|
|
{
|
2021-02-22 04:56:51 +00:00
|
|
|
case EGL_SCALE_AUTO:
|
|
|
|
switch (scaleType)
|
|
|
|
{
|
|
|
|
case EGL_DESKTOP_NOSCALE:
|
|
|
|
case EGL_DESKTOP_UPSCALE:
|
|
|
|
scaleAlgo = EGL_SCALE_NEAREST;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EGL_DESKTOP_DOWNSCALE:
|
|
|
|
scaleAlgo = EGL_SCALE_LINEAR;
|
|
|
|
break;
|
|
|
|
}
|
2021-02-21 00:50:59 +00:00
|
|
|
break;
|
|
|
|
|
2021-02-22 04:56:51 +00:00
|
|
|
default:
|
|
|
|
scaleAlgo = desktop->scaleAlgo;
|
2021-01-18 17:26:59 +00:00
|
|
|
}
|
|
|
|
|
2021-08-03 00:45:47 +00:00
|
|
|
float matrix[6];
|
|
|
|
egl_desktopRectsMatrix(matrix, desktop->width, desktop->height, x, y, scaleX, scaleY, rotate);
|
|
|
|
egl_desktopRectsUpdate(desktop->mesh, rects, desktop->width, desktop->height);
|
|
|
|
|
2019-05-22 01:36:18 +00:00
|
|
|
const struct DesktopShader * shader = desktop->shader;
|
|
|
|
egl_shader_use(shader->shader);
|
2021-02-21 00:50:59 +00:00
|
|
|
glUniform1i(shader->uScaleAlgo , scaleAlgo);
|
2019-05-22 01:36:18 +00:00
|
|
|
glUniform2f(shader->uDesktopSize, desktop->width, desktop->height);
|
2021-08-03 00:45:47 +00:00
|
|
|
glUniform2f(shader->uUVScale , 1.0f / desktop->width, 1.0f / desktop->height);
|
|
|
|
glUniformMatrix3x2fv(shader->uTransform, 1, GL_FALSE, matrix);
|
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);
|
2021-08-03 00:45:47 +00:00
|
|
|
|
|
|
|
egl_texture_bind(desktop->texture);
|
|
|
|
egl_desktopRectsRender(desktop->mesh);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
2019-06-12 12:36:00 +00:00
|
|
|
return true;
|
2020-01-17 03:35:08 +00:00
|
|
|
}
|