2018-09-22 06:26:10 +00:00
|
|
|
/*
|
|
|
|
Looking Glass - KVM FrameRelay (KVMFR) Client
|
|
|
|
Copyright (C) 2017 Geoffrey McRae <geoff@hostfission.com>
|
|
|
|
https://looking-glass.hostfission.com
|
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "lg-renderer.h"
|
2018-11-19 22:50:09 +00:00
|
|
|
|
2018-09-22 06:26:10 +00:00
|
|
|
#include "debug.h"
|
2018-09-24 09:48:11 +00:00
|
|
|
#include "utils.h"
|
2018-11-19 22:50:09 +00:00
|
|
|
#include "lg-fonts.h"
|
2018-09-22 06:26:10 +00:00
|
|
|
|
|
|
|
#include <SDL2/SDL_syswm.h>
|
|
|
|
#include <SDL2/SDL_egl.h>
|
|
|
|
|
2018-12-12 05:39:04 +00:00
|
|
|
#include "egl/model.h"
|
|
|
|
#include "egl/shader.h"
|
|
|
|
#include "egl/progs.h"
|
2018-12-12 07:53:55 +00:00
|
|
|
#include "egl/cursor.h"
|
2018-09-22 06:26:10 +00:00
|
|
|
|
|
|
|
struct Options
|
|
|
|
{
|
2018-09-22 08:00:52 +00:00
|
|
|
bool vsync;
|
2018-09-22 06:26:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct Options defaultOptions =
|
|
|
|
{
|
2018-10-03 14:09:10 +00:00
|
|
|
.vsync = false
|
2018-09-22 06:26:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct Models
|
|
|
|
{
|
|
|
|
struct EGL_Model * desktop;
|
2018-11-19 22:50:09 +00:00
|
|
|
struct EGL_Model * fps;
|
2018-09-22 06:26:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct Shaders
|
|
|
|
{
|
2018-09-23 10:45:20 +00:00
|
|
|
struct EGL_Shader * rgba;
|
|
|
|
struct EGL_Shader * bgra;
|
|
|
|
struct EGL_Shader * yuv;
|
2018-11-19 22:50:09 +00:00
|
|
|
struct EGL_Shader * fps;
|
2018-09-22 06:26:10 +00:00
|
|
|
};
|
|
|
|
|
2018-09-23 05:48:44 +00:00
|
|
|
struct Textures
|
|
|
|
{
|
|
|
|
struct EGL_Texture * desktop;
|
2018-11-19 22:50:09 +00:00
|
|
|
struct EGL_Texture * fps;
|
2018-09-23 05:48:44 +00:00
|
|
|
};
|
|
|
|
|
2018-09-22 06:26:10 +00:00
|
|
|
struct Inst
|
|
|
|
{
|
|
|
|
LG_RendererParams params;
|
|
|
|
struct Options opt;
|
|
|
|
|
|
|
|
Display * xDisplay;
|
|
|
|
Window xWindow;
|
|
|
|
EGLDisplay display;
|
|
|
|
EGLConfig configs;
|
|
|
|
EGLSurface surface;
|
|
|
|
EGLContext context;
|
|
|
|
|
2018-12-12 07:53:55 +00:00
|
|
|
EGL_Cursor * cursor; // the mouse cursor
|
|
|
|
|
2018-09-22 06:26:10 +00:00
|
|
|
struct Models models;
|
|
|
|
struct Shaders shaders;
|
2018-09-23 05:48:44 +00:00
|
|
|
struct Textures textures;
|
2018-09-22 06:26:10 +00:00
|
|
|
|
2018-09-23 10:45:20 +00:00
|
|
|
LG_RendererFormat format;
|
|
|
|
enum EGL_PixelFormat pixFmt;
|
|
|
|
EGL_Shader * shader;
|
|
|
|
bool sourceChanged;
|
|
|
|
size_t frameSize;
|
|
|
|
const uint8_t * data;
|
|
|
|
bool update;
|
2018-09-24 09:48:11 +00:00
|
|
|
|
2018-11-19 22:50:09 +00:00
|
|
|
int width, height;
|
|
|
|
LG_RendererRect destRect;
|
|
|
|
|
2018-10-03 16:31:37 +00:00
|
|
|
float translateX, translateY;
|
|
|
|
float scaleX , scaleY;
|
|
|
|
GLint uDesktopPos;
|
|
|
|
|
2018-12-12 07:53:55 +00:00
|
|
|
float mouseWidth , mouseHeight;
|
2018-09-24 09:48:11 +00:00
|
|
|
float mouseScaleX, mouseScaleY;
|
2018-11-19 22:50:09 +00:00
|
|
|
|
|
|
|
const LG_Font * font;
|
|
|
|
LG_FontObj fontObj;
|
|
|
|
|
|
|
|
bool fpsReady;
|
|
|
|
float fpsWidth, fpsHeight;
|
|
|
|
GLint uFPSScreen, uFPSSize;
|
2018-09-22 06:26:10 +00:00
|
|
|
};
|
|
|
|
|
2018-09-24 09:48:11 +00:00
|
|
|
|
|
|
|
void update_mouse_shape(struct Inst * this);
|
|
|
|
|
2018-09-22 06:26:10 +00:00
|
|
|
const char * egl_get_name()
|
|
|
|
{
|
|
|
|
return "EGL";
|
|
|
|
}
|
|
|
|
|
|
|
|
bool egl_create(void ** opaque, const LG_RendererParams params)
|
|
|
|
{
|
|
|
|
// create our local storage
|
|
|
|
*opaque = malloc(sizeof(struct Inst));
|
|
|
|
if (!*opaque)
|
|
|
|
{
|
|
|
|
DEBUG_INFO("Failed to allocate %lu bytes", sizeof(struct Inst));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
memset(*opaque, 0, sizeof(struct Inst));
|
|
|
|
|
|
|
|
// safe off parameteres and init our default option values
|
|
|
|
struct Inst * this = (struct Inst *)*opaque;
|
|
|
|
memcpy(&this->params, ¶ms , sizeof(LG_RendererParams));
|
|
|
|
memcpy(&this->opt , &defaultOptions, sizeof(struct Options ));
|
|
|
|
|
2018-10-03 16:31:37 +00:00
|
|
|
this->translateX = 0;
|
|
|
|
this->translateY = 0;
|
|
|
|
this->scaleX = 1.0f;
|
|
|
|
this->scaleY = 1.0f;
|
2018-09-24 09:48:11 +00:00
|
|
|
|
2018-11-19 22:50:09 +00:00
|
|
|
this->font = LG_Fonts[0];
|
|
|
|
if (!this->font->create(&this->fontObj, NULL, 14))
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to create a font instance");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-09-22 06:26:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool egl_initialize(void * opaque, Uint32 * sdlFlags)
|
|
|
|
{
|
|
|
|
*sdlFlags = SDL_WINDOW_OPENGL;
|
|
|
|
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER , 1);
|
|
|
|
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS , 1);
|
|
|
|
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES , 4);
|
|
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
|
2018-12-12 07:53:55 +00:00
|
|
|
|
2018-09-22 06:26:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void egl_deinitialize(void * opaque)
|
|
|
|
{
|
|
|
|
struct Inst * this = (struct Inst *)opaque;
|
|
|
|
|
2018-11-19 22:50:09 +00:00
|
|
|
if (this->font && this->fontObj)
|
|
|
|
this->font->destroy(this->fontObj);
|
|
|
|
|
2018-12-12 07:53:55 +00:00
|
|
|
egl_cursor_free(&this->cursor);
|
|
|
|
|
2018-09-25 13:04:29 +00:00
|
|
|
egl_model_free (&this->models .desktop );
|
2018-11-19 22:50:09 +00:00
|
|
|
egl_model_free (&this->models .fps );
|
2018-09-24 09:48:11 +00:00
|
|
|
egl_shader_free (&this->shaders .rgba );
|
|
|
|
egl_shader_free (&this->shaders .bgra );
|
|
|
|
egl_shader_free (&this->shaders .yuv );
|
2018-11-19 22:50:09 +00:00
|
|
|
egl_shader_free (&this->shaders .fps );
|
2018-09-25 13:04:29 +00:00
|
|
|
egl_texture_free(&this->textures.desktop );
|
2018-11-19 22:50:09 +00:00
|
|
|
egl_texture_free(&this->textures.fps );
|
2018-09-24 09:48:11 +00:00
|
|
|
|
2018-09-22 06:26:10 +00:00
|
|
|
free(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void egl_on_resize(void * opaque, const int width, const int height, const LG_RendererRect destRect)
|
|
|
|
{
|
2018-09-24 09:48:11 +00:00
|
|
|
struct Inst * this = (struct Inst *)opaque;
|
|
|
|
|
2018-11-19 22:50:09 +00:00
|
|
|
this->width = width;
|
|
|
|
this->height = height;
|
|
|
|
memcpy(&this->destRect, &destRect, sizeof(LG_RendererRect));
|
|
|
|
|
2018-09-22 06:26:10 +00:00
|
|
|
glViewport(0, 0, width, height);
|
2018-09-24 09:48:11 +00:00
|
|
|
|
2018-10-03 16:31:37 +00:00
|
|
|
if (destRect.valid)
|
|
|
|
{
|
|
|
|
this->translateX = 1.0f - (((destRect.w / 2) + destRect.x) * 2) / (float)width;
|
|
|
|
this->translateY = 1.0f - (((destRect.h / 2) + destRect.y) * 2) / (float)height;
|
|
|
|
this->scaleX = (float)destRect.w / (float)width;
|
|
|
|
this->scaleY = (float)destRect.h / (float)height;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->mouseScaleX = 2.0f / this->format.width ;
|
2018-09-24 10:11:42 +00:00
|
|
|
this->mouseScaleY = 2.0f / this->format.height;
|
2018-12-12 07:53:55 +00:00
|
|
|
egl_cursor_set_size(this->cursor,
|
|
|
|
(this->mouseWidth * (1.0f / this->format.width )) * this->scaleX,
|
|
|
|
(this->mouseHeight * (1.0f / this->format.height)) * this->scaleY
|
|
|
|
);
|
2018-09-22 06:26:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool egl_on_mouse_shape(void * opaque, const LG_RendererCursor cursor, const int width, const int height, const int pitch, const uint8_t * data)
|
|
|
|
{
|
2018-09-24 09:48:11 +00:00
|
|
|
struct Inst * this = (struct Inst *)opaque;
|
2018-12-12 07:53:55 +00:00
|
|
|
if (!egl_cursor_set_shape(this->cursor, cursor, width, height, pitch, data))
|
2018-09-24 09:48:11 +00:00
|
|
|
{
|
2018-12-12 07:53:55 +00:00
|
|
|
DEBUG_ERROR("Failed to update the cursor shape");
|
|
|
|
return false;
|
2018-09-24 09:48:11 +00:00
|
|
|
}
|
|
|
|
|
2018-12-12 07:53:55 +00:00
|
|
|
this->mouseWidth = width;
|
|
|
|
this->mouseHeight = height;
|
|
|
|
egl_cursor_set_size(this->cursor,
|
|
|
|
(this->mouseWidth * (1.0f / this->format.width )) * this->scaleX,
|
|
|
|
(this->mouseHeight * (1.0f / this->format.height)) * this->scaleY
|
|
|
|
);
|
2018-09-24 09:48:11 +00:00
|
|
|
|
2018-09-22 06:26:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool egl_on_mouse_event(void * opaque, const bool visible , const int x, const int y)
|
|
|
|
{
|
2018-09-24 09:48:11 +00:00
|
|
|
struct Inst * this = (struct Inst *)opaque;
|
2018-12-12 07:53:55 +00:00
|
|
|
|
|
|
|
egl_cursor_set_state(
|
|
|
|
this->cursor,
|
|
|
|
visible,
|
|
|
|
(((float)x * this->mouseScaleX) - 1.0f) * this->scaleX,
|
|
|
|
(((float)y * this->mouseScaleY) - 1.0f) * this->scaleY
|
|
|
|
);
|
|
|
|
|
2018-09-22 06:26:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool egl_on_frame_event(void * opaque, const LG_RendererFormat format, const uint8_t * data)
|
|
|
|
{
|
|
|
|
struct Inst * this = (struct Inst *)opaque;
|
|
|
|
|
2018-09-23 05:48:44 +00:00
|
|
|
this->sourceChanged = (
|
|
|
|
this->sourceChanged ||
|
|
|
|
this->format.type != format.type ||
|
|
|
|
this->format.width != format.width ||
|
|
|
|
this->format.height != format.height ||
|
|
|
|
this->format.pitch != format.pitch
|
|
|
|
);
|
|
|
|
|
|
|
|
if (this->sourceChanged)
|
|
|
|
{
|
|
|
|
memcpy(&this->format, &format, sizeof(LG_RendererFormat));
|
2018-09-23 10:45:20 +00:00
|
|
|
|
|
|
|
switch(format.type)
|
|
|
|
{
|
2018-12-04 10:23:28 +00:00
|
|
|
case FRAME_TYPE_BGRA:
|
|
|
|
this->pixFmt = EGL_PF_BGRA;
|
|
|
|
this->shader = this->shaders.bgra;
|
|
|
|
this->frameSize = format.height * format.pitch;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FRAME_TYPE_RGBA:
|
2018-09-23 10:45:20 +00:00
|
|
|
this->pixFmt = EGL_PF_RGBA;
|
|
|
|
this->shader = this->shaders.rgba;
|
|
|
|
this->frameSize = format.height * format.pitch;
|
|
|
|
break;
|
|
|
|
|
2018-12-04 10:23:28 +00:00
|
|
|
case FRAME_TYPE_RGBA10:
|
|
|
|
this->pixFmt = EGL_PF_RGBA10;
|
|
|
|
this->shader = this->shaders.rgba;
|
|
|
|
this->frameSize = format.height * format.pitch;
|
|
|
|
break;
|
|
|
|
|
2018-09-23 10:45:20 +00:00
|
|
|
case FRAME_TYPE_YUV420:
|
|
|
|
this->pixFmt = EGL_PF_YUV420;
|
|
|
|
this->shader = this->shaders.yuv;
|
|
|
|
this->frameSize = format.width * format.height * 3 / 2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2018-12-04 10:23:28 +00:00
|
|
|
DEBUG_ERROR("Unsupported frame format");
|
2018-09-23 10:45:20 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-11-19 22:50:09 +00:00
|
|
|
|
|
|
|
this->uDesktopPos = egl_shader_get_uniform_location(this->shader, "position");
|
2018-09-23 05:48:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this->data = data;
|
|
|
|
this->update = true;
|
2018-09-22 06:26:10 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void egl_on_alert(void * opaque, const LG_RendererAlert alert, const char * message, bool ** closeFlag)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool egl_render_startup(void * opaque, SDL_Window * window)
|
|
|
|
{
|
|
|
|
struct Inst * this = (struct Inst *)opaque;
|
|
|
|
|
|
|
|
SDL_SysWMinfo wminfo;
|
|
|
|
SDL_VERSION(&wminfo.version);
|
|
|
|
if (!SDL_GetWindowWMInfo(window, &wminfo))
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("SDL_GetWindowWMInfo failed");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->xDisplay = wminfo.info.x11.display;
|
|
|
|
this->xWindow = wminfo.info.x11.window;
|
|
|
|
|
|
|
|
this->display = eglGetDisplay((EGLNativeDisplayType)this->xDisplay);
|
|
|
|
if (this->display == EGL_NO_DISPLAY)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("eglGetDisplay failed");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!eglInitialize(this->display, NULL, NULL))
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Unable to initialize EGL");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
EGLint attr[] =
|
|
|
|
{
|
2018-09-25 13:04:29 +00:00
|
|
|
EGL_BUFFER_SIZE , 16,
|
|
|
|
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
|
2018-09-22 06:26:10 +00:00
|
|
|
EGL_NONE
|
|
|
|
};
|
|
|
|
|
|
|
|
EGLint num_config;
|
|
|
|
if (!eglChooseConfig(this->display, attr, &this->configs, 1, &num_config))
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to choose config (eglError: 0x%x)", eglGetError());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->surface = eglCreateWindowSurface(this->display, this->configs, this->xWindow, NULL);
|
|
|
|
if (this->surface == EGL_NO_SURFACE)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to create EGL surface (eglError: 0x%x)", eglGetError());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
EGLint ctxattr[] =
|
|
|
|
{
|
|
|
|
EGL_CONTEXT_CLIENT_VERSION, 2,
|
|
|
|
EGL_NONE
|
|
|
|
};
|
|
|
|
|
|
|
|
this->context = eglCreateContext(this->display, this->configs, EGL_NO_CONTEXT, ctxattr);
|
|
|
|
if (this->context == EGL_NO_CONTEXT)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to create EGL context (eglError: 0x%x)", eglGetError());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
eglMakeCurrent(this->display, this->surface, this->surface, this->context);
|
|
|
|
|
|
|
|
DEBUG_INFO("Vendor : %s", glGetString(GL_VENDOR ));
|
|
|
|
DEBUG_INFO("Renderer: %s", glGetString(GL_RENDERER));
|
|
|
|
DEBUG_INFO("Version : %s", glGetString(GL_VERSION ));
|
|
|
|
|
2018-09-24 09:48:11 +00:00
|
|
|
static const GLfloat square[] =
|
2018-09-22 06:26:10 +00:00
|
|
|
{
|
|
|
|
-1.0f, -1.0f, 0.0f,
|
|
|
|
1.0f, -1.0f, 0.0f,
|
|
|
|
-1.0f, 1.0f, 0.0f,
|
|
|
|
1.0f, 1.0f, 0.0f
|
|
|
|
};
|
|
|
|
|
|
|
|
static const GLfloat uvs[] =
|
|
|
|
{
|
|
|
|
0.0f, 1.0f,
|
|
|
|
1.0f, 1.0f,
|
|
|
|
0.0f, 0.0f,
|
|
|
|
1.0f, 0.0f
|
|
|
|
};
|
|
|
|
|
2018-09-24 09:48:11 +00:00
|
|
|
if (!egl_shader_init(&this->shaders.rgba))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!egl_shader_init(&this->shaders.bgra))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!egl_shader_init(&this->shaders.yuv))
|
|
|
|
return false;
|
|
|
|
|
2018-11-19 22:50:09 +00:00
|
|
|
if (!egl_shader_init(&this->shaders.fps))
|
|
|
|
return false;
|
|
|
|
|
2018-10-03 16:31:37 +00:00
|
|
|
if (!egl_shader_compile(this->shaders.rgba, egl_vertex_shader_desktop, sizeof(egl_vertex_shader_desktop), egl_fragment_shader_rgba, sizeof(egl_fragment_shader_rgba)))
|
2018-09-24 09:48:11 +00:00
|
|
|
return false;
|
|
|
|
|
2018-10-03 16:31:37 +00:00
|
|
|
if (!egl_shader_compile(this->shaders.bgra, egl_vertex_shader_desktop, sizeof(egl_vertex_shader_desktop), egl_fragment_shader_bgra, sizeof(egl_fragment_shader_bgra)))
|
2018-09-24 09:48:11 +00:00
|
|
|
return false;
|
|
|
|
|
2018-10-03 16:31:37 +00:00
|
|
|
if (!egl_shader_compile(this->shaders.yuv, egl_vertex_shader_desktop, sizeof(egl_vertex_shader_desktop), egl_fragment_shader_yuv, sizeof(egl_fragment_shader_yuv)))
|
2018-09-24 09:48:11 +00:00
|
|
|
return false;
|
2018-09-22 08:00:52 +00:00
|
|
|
|
2018-11-19 22:50:09 +00:00
|
|
|
if (!egl_shader_compile(this->shaders.fps, egl_vertex_shader_fps, sizeof(egl_vertex_shader_fps), egl_fragment_shader_fps, sizeof(egl_fragment_shader_fps)))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
this->uFPSSize = egl_shader_get_uniform_location(this->shaders.fps , "size" );
|
|
|
|
this->uFPSScreen = egl_shader_get_uniform_location(this->shaders.fps , "screen" );
|
2018-09-25 13:04:29 +00:00
|
|
|
|
2018-09-23 05:48:44 +00:00
|
|
|
if (!egl_texture_init(&this->textures.desktop))
|
|
|
|
return false;
|
|
|
|
|
2018-11-19 22:50:09 +00:00
|
|
|
if (!egl_texture_init(&this->textures.fps))
|
|
|
|
return false;
|
|
|
|
|
2018-09-22 08:00:52 +00:00
|
|
|
if (!egl_model_init(&this->models.desktop))
|
|
|
|
return false;
|
2018-09-23 05:48:44 +00:00
|
|
|
|
2018-11-19 22:50:09 +00:00
|
|
|
if (!egl_model_init(&this->models.fps))
|
|
|
|
return false;
|
|
|
|
|
2018-09-25 13:04:29 +00:00
|
|
|
|
2018-09-24 09:48:11 +00:00
|
|
|
egl_model_set_verticies(this->models.desktop, square , sizeof(square) / sizeof(GLfloat));
|
|
|
|
egl_model_set_uvs (this->models.desktop, uvs , sizeof(uvs ) / sizeof(GLfloat));
|
2018-09-23 05:48:44 +00:00
|
|
|
egl_model_set_texture (this->models.desktop, this->textures.desktop);
|
2018-09-22 06:26:10 +00:00
|
|
|
|
2018-11-19 22:50:09 +00:00
|
|
|
egl_model_set_verticies(this->models.fps, square, sizeof(square) / sizeof(GLfloat));
|
|
|
|
egl_model_set_uvs (this->models.fps, uvs , sizeof(uvs ) / sizeof(GLfloat));
|
|
|
|
egl_model_set_texture (this->models.fps, this->textures.fps);
|
2018-09-24 09:48:11 +00:00
|
|
|
|
2018-09-22 08:00:52 +00:00
|
|
|
eglSwapInterval(this->display, this->opt.vsync ? 1 : 0);
|
2018-09-24 09:48:11 +00:00
|
|
|
|
2018-12-12 07:53:55 +00:00
|
|
|
if (!egl_cursor_init(&this->cursor))
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to initialize the cursor");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-09-22 06:26:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool egl_render(void * opaque, SDL_Window * window)
|
|
|
|
{
|
|
|
|
struct Inst * this = (struct Inst *)opaque;
|
|
|
|
|
|
|
|
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
|
2018-10-03 16:31:37 +00:00
|
|
|
if (this->shader)
|
|
|
|
{
|
|
|
|
egl_shader_use(this->shader);
|
|
|
|
glUniform4f(this->uDesktopPos, this->translateX, this->translateY, this->scaleX, this->scaleY);
|
|
|
|
egl_model_render(this->models.desktop);
|
|
|
|
}
|
2018-09-22 06:26:10 +00:00
|
|
|
|
2018-12-12 07:53:55 +00:00
|
|
|
egl_cursor_render(this->cursor);
|
2018-09-24 09:48:11 +00:00
|
|
|
|
2018-11-19 22:50:09 +00:00
|
|
|
if (this->fpsReady)
|
|
|
|
{
|
|
|
|
glEnable(GL_BLEND);
|
|
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
|
|
|
egl_shader_use(this->shaders.fps);
|
|
|
|
glUniform2f(this->uFPSScreen, this->width , this->height );
|
|
|
|
glUniform2f(this->uFPSSize , this->fpsWidth, this->fpsHeight);
|
|
|
|
egl_model_render(this->models.fps);
|
|
|
|
glDisable(GL_BLEND);
|
|
|
|
}
|
|
|
|
|
2018-09-22 06:26:10 +00:00
|
|
|
eglSwapBuffers(this->display, this->surface);
|
2018-10-03 14:09:10 +00:00
|
|
|
|
|
|
|
// defer texture uploads until after the flip to avoid stalling
|
|
|
|
if (this->update)
|
|
|
|
{
|
|
|
|
if (this->sourceChanged)
|
|
|
|
{
|
|
|
|
this->sourceChanged = false;
|
|
|
|
if (!egl_texture_setup(
|
|
|
|
this->textures.desktop,
|
|
|
|
this->pixFmt,
|
|
|
|
this->format.width,
|
|
|
|
this->format.height,
|
|
|
|
this->frameSize,
|
|
|
|
true
|
|
|
|
))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
egl_model_set_shader(this->models.desktop, this->shader);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!egl_texture_update(this->textures.desktop, this->data))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
this->update = false;
|
|
|
|
}
|
|
|
|
|
2018-09-22 06:26:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-11-19 22:50:09 +00:00
|
|
|
void egl_update_fps(void * opaque, const float avgFPS, const float renderFPS)
|
2018-11-19 18:26:51 +00:00
|
|
|
{
|
2018-11-19 22:50:09 +00:00
|
|
|
struct Inst * this = (struct Inst *)opaque;
|
|
|
|
if (!this->params.showFPS)
|
|
|
|
return;
|
|
|
|
|
|
|
|
char str[128];
|
|
|
|
snprintf(str, sizeof(str), "UPS: %8.4f, FPS: %8.4f", avgFPS, renderFPS);
|
|
|
|
|
|
|
|
LG_FontBitmap * bmp = this->font->render(this->fontObj, 0xffffff00, str);
|
|
|
|
if (!bmp)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to render FPS text");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
egl_texture_setup(
|
|
|
|
this->textures.fps,
|
|
|
|
EGL_PF_BGRA,
|
|
|
|
bmp->width ,
|
|
|
|
bmp->height,
|
|
|
|
bmp->width * bmp->height * bmp->bpp,
|
|
|
|
false
|
|
|
|
);
|
|
|
|
|
|
|
|
egl_texture_update
|
|
|
|
(
|
|
|
|
this->textures.fps,
|
|
|
|
bmp->pixels
|
|
|
|
);
|
|
|
|
|
|
|
|
this->fpsWidth = bmp->width;
|
|
|
|
this->fpsHeight = bmp->height;
|
|
|
|
this->fpsReady = true;
|
|
|
|
|
|
|
|
this->font->release(this->fontObj, bmp);
|
2018-11-19 18:26:51 +00:00
|
|
|
}
|
|
|
|
|
2018-09-22 08:00:52 +00:00
|
|
|
static void handle_opt_vsync(void * opaque, const char *value)
|
|
|
|
{
|
|
|
|
struct Inst * this = (struct Inst *)opaque;
|
|
|
|
if (!this)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this->opt.vsync = LG_RendererValueToBool(value);
|
|
|
|
}
|
|
|
|
|
2018-09-22 06:26:10 +00:00
|
|
|
static LG_RendererOpt egl_options[] =
|
|
|
|
{
|
2018-09-22 08:00:52 +00:00
|
|
|
{
|
|
|
|
.name = "vsync",
|
|
|
|
.desc ="Enable or disable vsync [default: enabled]",
|
|
|
|
.validator = LG_RendererValidatorBool,
|
|
|
|
.handler = handle_opt_vsync
|
|
|
|
}
|
2018-09-22 06:26:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct LG_Renderer LGR_EGL =
|
|
|
|
{
|
|
|
|
.create = egl_create,
|
|
|
|
.get_name = egl_get_name,
|
|
|
|
.options = egl_options,
|
|
|
|
.option_count = LGR_OPTION_COUNT(egl_options),
|
|
|
|
.initialize = egl_initialize,
|
|
|
|
.deinitialize = egl_deinitialize,
|
|
|
|
.on_resize = egl_on_resize,
|
|
|
|
.on_mouse_shape = egl_on_mouse_shape,
|
|
|
|
.on_mouse_event = egl_on_mouse_event,
|
|
|
|
.on_frame_event = egl_on_frame_event,
|
|
|
|
.on_alert = egl_on_alert,
|
|
|
|
.render_startup = egl_render_startup,
|
2018-11-19 18:26:51 +00:00
|
|
|
.render = egl_render,
|
|
|
|
.update_fps = egl_update_fps
|
2018-09-22 06:26:10 +00:00
|
|
|
};
|