1
0
mirror of https://github.com/jojo61/vdr-plugin-softhdcuvid.git synced 2023-10-10 13:37:41 +02:00
vdr-plugin-softhdcuvid/openglosd.cpp

2310 lines
62 KiB
C++
Raw Normal View History

2018-09-05 20:39:12 +02:00
#define __STL_CONFIG_H
#include <algorithm>
#include "openglosd.h"
/****************************************************************************************
* Helpers
****************************************************************************************/
void ConvertColor(const GLint & colARGB, glm::vec4 & col)
{
2018-09-05 20:39:12 +02:00
col.a = ((colARGB & 0xFF000000) >> 24) / 255.0;
col.r = ((colARGB & 0x00FF0000) >> 16) / 255.0;
col.g = ((colARGB & 0x0000FF00) >> 8) / 255.0;
col.b = ((colARGB & 0x000000FF)) / 255.0;
2018-09-05 20:39:12 +02:00
}
/****************************************************************************************
* cShader
****************************************************************************************/
2019-08-22 12:34:29 +02:00
#ifdef CUVID
2019-12-23 14:53:30 +01:00
const char *glversion = "#version 330 core ";
#else
#ifdef RASPI
const char *glversion = "#version 300 es";
2019-08-22 12:34:29 +02:00
#else
2020-01-03 12:22:47 +01:00
const char *glversion = "#version 300 es ";
2019-12-23 14:53:30 +01:00
#endif
#endif
2019-08-22 12:34:29 +02:00
const char *rectVertexShader = "%s\n \
2019-08-22 12:34:29 +02:00
\
layout (location = 0) in vec2 position; \
out vec4 rectCol; \
uniform vec4 inColor; \
uniform mat4 projection; \
\
void main() \
{ \
gl_Position = projection * vec4(position.x, position.y, 0.0, 1.0); \
rectCol = inColor; \
} \
";
const char *rectFragmentShader = "%s\n \
2019-08-22 12:34:29 +02:00
\
2019-10-04 10:37:57 +02:00
precision mediump float; \
2019-08-22 12:34:29 +02:00
in vec4 rectCol; \
out vec4 color; \
\
void main() \
{ \
color = rectCol; \
} \
";
const char *textureVertexShader = "%s\n \
2019-08-22 12:34:29 +02:00
\
layout (location = 0) in vec2 position; \
layout (location = 1) in vec2 texCoords; \
\
out vec2 TexCoords; \
out vec4 alphaValue;\
\
uniform mat4 projection; \
uniform vec4 alpha; \
\
void main() \
{ \
gl_Position = projection * vec4(position.x, position.y, 0.0, 1.0); \
TexCoords = texCoords; \
alphaValue = alpha; \
} \
";
const char *textureFragmentShader = "%s\n \
2019-10-04 10:37:57 +02:00
precision mediump float; \
2019-08-22 12:34:29 +02:00
in vec2 TexCoords; \
in vec4 alphaValue; \
out vec4 color; \
\
uniform sampler2D screenTexture; \
\
void main() \
{ \
color = texture(screenTexture, TexCoords) * alphaValue; \
} \
";
const char *textVertexShader = "%s\n \
2019-08-22 12:34:29 +02:00
\
layout (location = 0) in vec2 position; \
layout (location = 1) in vec2 texCoords; \
\
out vec2 TexCoords; \
out vec4 textColor; \
\
uniform mat4 projection; \
uniform vec4 inColor; \
\
void main() \
{ \
gl_Position = projection * vec4(position.x, position.y, 0.0, 1.0); \
TexCoords = texCoords; \
textColor = inColor; \
} \
";
const char *textFragmentShader = "%s\n \
2019-10-04 10:37:57 +02:00
precision mediump float; \
2019-08-22 12:34:29 +02:00
in vec2 TexCoords; \
in vec4 textColor; \
\
out vec4 color; \
\
uniform sampler2D glyphTexture; \
\
void main() \
{ \
vec4 sampled = vec4(1.0, 1.0, 1.0, texture(glyphTexture, TexCoords).r); \
color = textColor * sampled; \
} \
";
2019-12-23 14:53:30 +01:00
///
/// GLX check error.
///
#define GlxCheck(void)\
{\
GLenum err;\
\
if ((err = glGetError()) != GL_NO_ERROR) {\
esyslog( "video/glx: error %s:%d %d '%s'\n",__FILE__,__LINE__, err, gluErrorString(err));\
}\
}
2019-10-28 21:43:37 +01:00
static cShader *Shaders[stCount];
2018-09-05 20:39:12 +02:00
void cShader::Use(void)
{
2018-09-05 20:39:12 +02:00
glUseProgram(id);
}
bool cShader::Load(eShaderType type)
{
2018-09-05 20:39:12 +02:00
this->type = type;
const char *vertexCode = NULL;
const char *fragmentCode = NULL;
switch (type) {
case stRect:
vertexCode = rectVertexShader;
fragmentCode = rectFragmentShader;
break;
case stTexture:
vertexCode = textureVertexShader;
fragmentCode = textureFragmentShader;
break;
case stText:
vertexCode = textVertexShader;
fragmentCode = textFragmentShader;
break;
default:
esyslog("[softhddev]unknown shader type\n");
break;
}
if (vertexCode == NULL || fragmentCode == NULL) {
esyslog("[softhddev]ERROR reading shader\n");
return false;
}
if (!Compile(vertexCode, fragmentCode)) {
esyslog("[softhddev]ERROR compiling shader\n");
return false;
}
return true;
}
void cShader::SetFloat(const GLchar * name, GLfloat value)
{
2018-09-05 20:39:12 +02:00
glUniform1f(glGetUniformLocation(id, name), value);
}
void cShader::SetInteger(const GLchar * name, GLint value)
{
2018-09-05 20:39:12 +02:00
glUniform1i(glGetUniformLocation(id, name), value);
}
void cShader::SetVector2f(const GLchar * name, GLfloat x, GLfloat y)
{
2018-09-05 20:39:12 +02:00
glUniform2f(glGetUniformLocation(id, name), x, y);
}
void cShader::SetVector3f(const GLchar * name, GLfloat x, GLfloat y, GLfloat z)
{
2018-09-05 20:39:12 +02:00
glUniform3f(glGetUniformLocation(id, name), x, y, z);
}
void cShader::SetVector4f(const GLchar * name, GLfloat x, GLfloat y, GLfloat z, GLfloat w)
{
2018-09-05 20:39:12 +02:00
glUniform4f(glGetUniformLocation(id, name), x, y, z, w);
}
void cShader::SetMatrix4(const GLchar * name, const glm::mat4 & matrix)
{
2018-09-05 20:39:12 +02:00
glUniformMatrix4fv(glGetUniformLocation(id, name), 1, GL_FALSE, glm::value_ptr(matrix));
}
bool cShader::Compile(const char *vertexCode, const char *fragmentCode)
{
2018-09-05 20:39:12 +02:00
GLuint sVertex, sFragment;
char *buffer = (char *)malloc(1000);
2018-09-05 20:39:12 +02:00
// Vertex Shader
sVertex = glCreateShader(GL_VERTEX_SHADER);
sprintf(buffer, vertexCode, glversion);
glShaderSource(sVertex, 1, (const GLchar **)&buffer, NULL);
2018-09-05 20:39:12 +02:00
glCompileShader(sVertex);
2019-10-28 21:43:37 +01:00
// esyslog("[softhddev]:SHADER:VERTEX %s\n",vertexCode);
2019-12-23 14:53:30 +01:00
if (!CheckCompileErrors(sVertex)) {
free(buffer);
2018-09-05 20:39:12 +02:00
return false;
}
2018-09-05 20:39:12 +02:00
// Fragment Shader
sFragment = glCreateShader(GL_FRAGMENT_SHADER);
sprintf(buffer, fragmentCode, glversion);
glShaderSource(sFragment, 1, (const GLchar **)&buffer, NULL);
2018-09-05 20:39:12 +02:00
glCompileShader(sFragment);
2019-10-28 21:43:37 +01:00
// esyslog("[softhddev]:SHADER:FRAGMENT %s\n",fragmentCode);
2019-12-23 14:53:30 +01:00
if (!CheckCompileErrors(sFragment)) {
free(buffer);
2018-09-05 20:39:12 +02:00
return false;
}
2018-09-05 20:39:12 +02:00
// link Program
id = glCreateProgram();
glAttachShader(id, sVertex);
glAttachShader(id, sFragment);
glLinkProgram(id);
if (!CheckCompileErrors(id, true))
return false;
// Delete the shaders as they're linked into our program now and no longer necessery
glDeleteShader(sVertex);
glDeleteShader(sFragment);
free(buffer);
2018-09-05 20:39:12 +02:00
return true;
}
bool cShader::CheckCompileErrors(GLuint object, bool program)
{
2018-09-05 20:39:12 +02:00
GLint success;
GLchar infoLog[1024];
2018-09-05 20:39:12 +02:00
if (!program) {
glGetShaderiv(object, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(object, 1024, NULL, infoLog);
esyslog("\n[softhddev]:SHADER: Compile-time error: Type: %d - \n>%s<\n", type, infoLog);
2018-09-05 20:39:12 +02:00
return false;
}
} else {
glGetProgramiv(object, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(object, 1024, NULL, infoLog);
esyslog("[softhddev]:SHADER: Link-time error: Type: %d - \n>%s<\n", type, infoLog);
2018-09-05 20:39:12 +02:00
return false;
}
}
return true;
}
#define KERNING_UNKNOWN (-10000)
2018-09-05 20:39:12 +02:00
/****************************************************************************************
* cOglGlyph
****************************************************************************************/
cOglGlyph::cOglGlyph(FT_ULong charCode, FT_BitmapGlyph ftGlyph)
{
2018-09-05 20:39:12 +02:00
this->charCode = charCode;
bearingLeft = ftGlyph->left;
bearingTop = ftGlyph->top;
width = ftGlyph->bitmap.width;
height = ftGlyph->bitmap.rows;
advanceX = ftGlyph->root.advance.x >> 16; //value in 1/2^16 pixel
LoadTexture(ftGlyph);
}
cOglGlyph::~cOglGlyph(void)
{
2018-09-05 20:39:12 +02:00
}
int cOglGlyph::GetKerningCache(FT_ULong prevSym)
{
for (int i = kerningCache.Size(); --i > 0;) {
2018-09-05 20:39:12 +02:00
if (kerningCache[i].prevSym == prevSym)
return kerningCache[i].kerning;
}
return KERNING_UNKNOWN;
}
void cOglGlyph::SetKerningCache(FT_ULong prevSym, int kerning)
{
2018-09-05 20:39:12 +02:00
kerningCache.Append(tKerning(prevSym, kerning));
}
void cOglGlyph::BindTexture(void)
{
2018-09-05 20:39:12 +02:00
glBindTexture(GL_TEXTURE_2D, texture);
}
void cOglGlyph::LoadTexture(FT_BitmapGlyph ftGlyph)
{
2018-09-05 20:39:12 +02:00
// Disable byte-alignment restriction
2019-11-05 22:06:54 +01:00
2018-09-05 20:39:12 +02:00
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, ftGlyph->bitmap.width, ftGlyph->bitmap.rows, 0, GL_RED, GL_UNSIGNED_BYTE,
ftGlyph->bitmap.buffer);
2018-09-05 20:39:12 +02:00
// Set texture options
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glBindTexture(GL_TEXTURE_2D, 0);
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
2019-11-05 22:06:54 +01:00
2018-09-05 20:39:12 +02:00
}
extern "C" void GlxInitopengl();
extern "C" void GlxDrawopengl();
2020-01-13 18:17:07 +01:00
extern "C" void GlxDestroy();
extern "C" void makejpg(uint8_t * data, int width, int height);
2018-09-05 20:39:12 +02:00
/****************************************************************************************
* cOglFont
****************************************************************************************/
FT_Library cOglFont::ftLib = 0;
cList < cOglFont > *cOglFont::fonts = 0;
2018-09-05 20:39:12 +02:00
bool cOglFont::initiated = false;
cOglFont::cOglFont(const char *fontName, int charHeight):name(fontName)
{
2018-09-05 20:39:12 +02:00
size = charHeight;
height = 0;
bottom = 0;
int error = FT_New_Face(ftLib, fontName, 0, &face);
2018-09-05 20:39:12 +02:00
if (error)
esyslog("[softhddev]ERROR: failed to open %s!", *name);
FT_Set_Char_Size(face, 0, charHeight * 64, 0, 0);
height = (face->size->metrics.ascender - face->size->metrics.descender + 63) / 64;
bottom = abs((face->size->metrics.descender - 63) / 64);
}
cOglFont::~cOglFont(void)
{
2018-09-05 20:39:12 +02:00
FT_Done_Face(face);
}
cOglFont *cOglFont::Get(const char *name, int charHeight)
{
2018-09-05 20:39:12 +02:00
if (!fonts)
Init();
cOglFont *font;
2018-09-05 20:39:12 +02:00
for (font = fonts->First(); font; font = fonts->Next(font))
if (!strcmp(font->Name(), name) && charHeight == font->Size()) {
return font;
}
font = new cOglFont(name, charHeight);
fonts->Add(font);
return font;
}
2019-10-28 21:43:37 +01:00
void cOglFont::Init(void)
{
2019-11-17 11:07:15 +01:00
if (FT_Init_FreeType(&ftLib)) {
2018-09-05 20:39:12 +02:00
esyslog("[softhddev]failed to initialize FreeType library!");
return;
}
fonts = new cList < cOglFont >;
2018-09-05 20:39:12 +02:00
initiated = true;
}
void cOglFont::Cleanup(void)
{
2018-09-05 20:39:12 +02:00
if (!initiated)
return;
delete fonts;
2018-09-05 20:39:12 +02:00
fonts = 0;
if (FT_Done_FreeType(ftLib))
esyslog("failed to deinitialize FreeType library!");
}
cOglGlyph *cOglFont::Glyph(FT_ULong charCode) const
{
2018-09-05 20:39:12 +02:00
// Non-breaking space:
if (charCode == 0xA0)
charCode = 0x20;
// Lookup in cache:
for (cOglGlyph * g = glyphCache.First(); g; g = glyphCache.Next(g)) {
2018-09-05 20:39:12 +02:00
if (g->CharCode() == charCode) {
return g;
}
}
FT_UInt glyph_index = FT_Get_Char_Index(face, charCode);
FT_Int32 loadFlags = FT_LOAD_NO_BITMAP;
2018-09-05 20:39:12 +02:00
// Load glyph image into the slot (erase previous one):
int error = FT_Load_Glyph(face, glyph_index, loadFlags);
2018-09-05 20:39:12 +02:00
if (error) {
esyslog("[softhddev]FT_Error (0x%02x) : %s\n", FT_Errors[error].code, FT_Errors[error].message);
return NULL;
}
FT_Glyph ftGlyph;
FT_Stroker stroker;
error = FT_Stroker_New(ftLib, &stroker);
2018-09-05 20:39:12 +02:00
if (error) {
esyslog("[softhddev]FT_Stroker_New FT_Error (0x%02x) : %s\n", FT_Errors[error].code, FT_Errors[error].message);
2019-10-28 21:43:37 +01:00
return NULL;
2018-09-05 20:39:12 +02:00
}
float outlineWidth = 0.25f;
FT_Stroker_Set(stroker, (int)(outlineWidth * 64), FT_STROKER_LINECAP_ROUND, FT_STROKER_LINEJOIN_ROUND, 0);
2019-10-28 21:43:37 +01:00
2018-09-05 20:39:12 +02:00
error = FT_Get_Glyph(face->glyph, &ftGlyph);
if (error) {
esyslog("[softhddev]FT_Get_Glyph FT_Error (0x%02x) : %s\n", FT_Errors[error].code, FT_Errors[error].message);
2019-10-28 21:43:37 +01:00
return NULL;
2018-09-05 20:39:12 +02:00
}
error = FT_Glyph_StrokeBorder(&ftGlyph, stroker, 0, 1);
if (error) {
esyslog("[softhddev]FT_Glyph_StrokeBorder FT_Error (0x%02x) : %s\n", FT_Errors[error].code,
FT_Errors[error].message);
2018-09-05 20:39:12 +02:00
return NULL;
}
FT_Stroker_Done(stroker);
error = FT_Glyph_To_Bitmap(&ftGlyph, FT_RENDER_MODE_NORMAL, 0, 1);
2018-09-05 20:39:12 +02:00
if (error) {
esyslog("[softhddev]FT_Glyph_To_Bitmap FT_Error (0x%02x) : %s\n", FT_Errors[error].code,
FT_Errors[error].message);
2019-10-28 21:43:37 +01:00
return NULL;
2018-09-05 20:39:12 +02:00
}
2019-10-28 21:43:37 +01:00
cOglGlyph *Glyph = new cOglGlyph(charCode, (FT_BitmapGlyph) ftGlyph);
2018-09-05 20:39:12 +02:00
glyphCache.Add(Glyph);
FT_Done_Glyph(ftGlyph);
return Glyph;
}
int cOglFont::Kerning(cOglGlyph * glyph, FT_ULong prevSym) const
{
2018-09-05 20:39:12 +02:00
int kerning = 0;
2018-09-05 20:39:12 +02:00
if (glyph && prevSym) {
kerning = glyph->GetKerningCache(prevSym);
if (kerning == KERNING_UNKNOWN) {
FT_Vector delta;
FT_UInt glyph_index = FT_Get_Char_Index(face, glyph->CharCode());
FT_UInt glyph_index_prev = FT_Get_Char_Index(face, prevSym);
2018-09-05 20:39:12 +02:00
FT_Get_Kerning(face, glyph_index_prev, glyph_index, FT_KERNING_DEFAULT, &delta);
kerning = delta.x / 64;
glyph->SetKerningCache(prevSym, kerning);
}
}
return kerning;
}
/****************************************************************************************
* cOglFb
****************************************************************************************/
cOglFb::cOglFb(GLint width, GLint height, GLint viewPortWidth, GLint viewPortHeight)
{
2018-09-05 20:39:12 +02:00
initiated = false;
fb = 0;
texture = 0;
2020-03-26 12:36:00 +01:00
2018-09-05 20:39:12 +02:00
this->width = width;
this->height = height;
this->viewPortWidth = viewPortWidth;
this->viewPortHeight = viewPortHeight;
if (width != viewPortWidth || height != viewPortHeight)
scrollable = true;
else
scrollable = false;
}
cOglFb::~cOglFb(void)
{
2019-10-28 21:43:37 +01:00
if (texture)
glDeleteTextures(1, &texture);
if (fb)
glDeleteFramebuffers(1, &fb);
2018-09-05 20:39:12 +02:00
}
bool cOglFb::Init(void)
{
2018-09-05 20:39:12 +02:00
initiated = true;
glGenTextures(1, &texture);
2018-09-05 20:39:12 +02:00
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
2018-09-26 17:20:03 +02:00
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2018-09-05 20:39:12 +02:00
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
glGenFramebuffers(1, &fb);
glBindFramebuffer(GL_FRAMEBUFFER, fb);
2019-10-28 21:43:37 +01:00
2018-09-05 20:39:12 +02:00
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
2019-10-28 21:43:37 +01:00
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
esyslog("[softhddev]ERROR: %d Framebuffer is not complete!\n", __LINE__);
2019-11-05 22:06:54 +01:00
2018-09-05 20:39:12 +02:00
return false;
}
2019-11-05 22:06:54 +01:00
2018-09-05 20:39:12 +02:00
return true;
}
void cOglFb::Bind(void)
{
2018-09-05 20:39:12 +02:00
if (!initiated)
Init();
glViewport(0, 0, width, height);
glBindFramebuffer(GL_FRAMEBUFFER, fb);
}
void cOglFb::BindRead(void)
{
2018-09-05 20:39:12 +02:00
glBindFramebuffer(GL_READ_FRAMEBUFFER, fb);
}
void cOglFb::BindWrite(void)
{
2018-09-05 20:39:12 +02:00
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fb);
}
void cOglFb::Unbind(void)
{
2019-10-28 21:43:37 +01:00
glBindFramebuffer(GL_FRAMEBUFFER, 0);
2018-09-05 20:39:12 +02:00
glBindTexture(GL_TEXTURE_2D, 0);
}
bool cOglFb::BindTexture(void)
{
2018-09-05 20:39:12 +02:00
if (!initiated)
return false;
glBindTexture(GL_TEXTURE_2D, texture);
return true;
}
void cOglFb::Blit(GLint destX1, GLint destY1, GLint destX2, GLint destY2)
{
2018-09-05 20:39:12 +02:00
glBlitFramebuffer(0, 0, width, height, destX1, destY1, destX2, destY2, GL_COLOR_BUFFER_BIT, GL_NEAREST);
glFlush();
}
/****************************************************************************************
* cOglOutputFb
****************************************************************************************/
cOglOutputFb::cOglOutputFb(GLint width, GLint height):cOglFb(width, height, width, height)
{
2019-10-28 21:43:37 +01:00
// surface = 0;
initiated = false;
2018-10-12 12:51:52 +02:00
fb = 0;
texture = 0;
2018-09-05 20:39:12 +02:00
}
cOglOutputFb::~cOglOutputFb(void)
{
2019-10-28 21:43:37 +01:00
// glVDPAUUnregisterSurfaceNV(surface);
glDeleteTextures(1, &texture);
2018-10-12 12:51:52 +02:00
glDeleteFramebuffers(1, &fb);
2018-09-05 20:39:12 +02:00
}
bool cOglOutputFb::Init(void)
{
2019-10-28 21:43:37 +01:00
initiated = true;
2019-10-04 10:37:57 +02:00
2018-09-05 20:39:12 +02:00
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
2019-10-28 21:43:37 +01:00
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
2018-09-28 08:55:44 +02:00
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
2019-10-28 21:43:37 +01:00
2018-09-05 20:39:12 +02:00
glGenFramebuffers(1, &fb);
glBindFramebuffer(GL_FRAMEBUFFER, fb);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
2018-09-05 20:39:12 +02:00
esyslog("[softhddev]ERROR::cOglOutputFb: Framebuffer is not complete!");
return false;
}
return true;
}
void cOglOutputFb::BindWrite(void)
{
2019-10-28 21:43:37 +01:00
if (!initiated)
2018-10-12 12:51:52 +02:00
Init();
2018-09-28 08:55:44 +02:00
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fb);
2018-09-05 20:39:12 +02:00
}
void cOglOutputFb::Unbind(void)
{
2018-09-28 08:55:44 +02:00
glBindFramebuffer(GL_FRAMEBUFFER, 0);
2018-09-05 20:39:12 +02:00
}
/****************************************************************************************
* cOglVb
****************************************************************************************/
2019-10-28 21:43:37 +01:00
static cOglVb *VertexBuffers[vbCount];
2018-09-05 20:39:12 +02:00
cOglVb::cOglVb(int type)
{
this->type = (eVertexBufferType) type;
2018-09-05 20:39:12 +02:00
vao = 0;
vbo = 0;
sizeVertex1 = 0;
sizeVertex2 = 0;
numVertices = 0;
drawMode = 0;
}
cOglVb::~cOglVb(void)
{
2018-09-05 20:39:12 +02:00
}
bool cOglVb::Init(void)
{
2018-09-05 20:39:12 +02:00
if (type == vbTexture) {
2019-10-28 21:43:37 +01:00
// Texture VBO definition
2018-09-05 20:39:12 +02:00
sizeVertex1 = 2;
sizeVertex2 = 2;
numVertices = 6;
drawMode = GL_TRIANGLES;
shader = stTexture;
} else if (type == vbRect) {
2019-10-28 21:43:37 +01:00
// Rectangle VBO definition
2018-09-05 20:39:12 +02:00
sizeVertex1 = 2;
sizeVertex2 = 0;
numVertices = 4;
drawMode = GL_TRIANGLE_FAN;
shader = stRect;
} else if (type == vbEllipse) {
2019-10-28 21:43:37 +01:00
// Ellipse VBO definition
2018-09-05 20:39:12 +02:00
sizeVertex1 = 2;
sizeVertex2 = 0;
numVertices = 182;
drawMode = GL_TRIANGLE_FAN;
shader = stRect;
} else if (type == vbSlope) {
2019-10-28 21:43:37 +01:00
// Slope VBO definition
2018-09-05 20:39:12 +02:00
sizeVertex1 = 2;
sizeVertex2 = 0;
numVertices = 102;
drawMode = GL_TRIANGLE_FAN;
shader = stRect;
} else if (type == vbText) {
2019-10-28 21:43:37 +01:00
// Text VBO definition
2018-09-05 20:39:12 +02:00
sizeVertex1 = 2;
sizeVertex2 = 2;
numVertices = 6;
drawMode = GL_TRIANGLES;
shader = stText;
}
glGenVertexArrays(1, &vao);
glGenBuffers(1, &vbo);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * (sizeVertex1 + sizeVertex2) * numVertices, NULL, GL_DYNAMIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, sizeVertex1, GL_FLOAT, GL_FALSE, (sizeVertex1 + sizeVertex2) * sizeof(GLfloat),
(GLvoid *) 0);
2018-09-05 20:39:12 +02:00
if (sizeVertex2 > 0) {
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, sizeVertex2, GL_FLOAT, GL_FALSE, (sizeVertex1 + sizeVertex2) * sizeof(GLfloat),
(GLvoid *) (sizeVertex1 * sizeof(GLfloat)));
2018-09-05 20:39:12 +02:00
}
2019-10-28 21:43:37 +01:00
2018-09-05 20:39:12 +02:00
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
return true;
}
void cOglVb::Bind(void)
{
2018-09-05 20:39:12 +02:00
glBindVertexArray(vao);
}
void cOglVb::Unbind(void)
{
2018-09-05 20:39:12 +02:00
glBindVertexArray(0);
}
void cOglVb::ActivateShader(void)
{
2018-09-05 20:39:12 +02:00
Shaders[shader]->Use();
}
void cOglVb::EnableBlending(void)
{
2018-09-05 20:39:12 +02:00
glEnable(GL_BLEND);
glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
}
void cOglVb::DisableBlending(void)
{
2018-09-05 20:39:12 +02:00
glDisable(GL_BLEND);
}
void cOglVb::SetShaderColor(GLint color)
{
2018-09-05 20:39:12 +02:00
glm::vec4 col;
ConvertColor(color, col);
Shaders[shader]->SetVector4f("inColor", col.r, col.g, col.b, col.a);
}
void cOglVb::SetShaderAlpha(GLint alpha)
{
Shaders[shader]->SetVector4f("alpha", 1.0f, 1.0f, 1.0f, (GLfloat) (alpha) / 255.0f);
2018-09-05 20:39:12 +02:00
}
void cOglVb::SetShaderProjectionMatrix(GLint width, GLint height)
{
glm::mat4 projection = glm::ortho(0.0f, (GLfloat) width, (GLfloat) height, 0.0f, -1.0f, 1.0f);
2018-09-05 20:39:12 +02:00
Shaders[shader]->SetMatrix4("projection", projection);
}
void cOglVb::SetVertexData(GLfloat * vertices, int count)
{
2018-09-05 20:39:12 +02:00
if (count == 0)
count = numVertices;
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(GLfloat) * (sizeVertex1 + sizeVertex2) * count, vertices);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void cOglVb::DrawArrays(int count)
{
2018-09-05 20:39:12 +02:00
if (count == 0)
count = numVertices;
glDrawArrays(drawMode, 0, count);
2019-10-28 21:43:37 +01:00
glFlush();
2018-09-05 20:39:12 +02:00
}
/****************************************************************************************
* cOpenGLCmd
****************************************************************************************/
//------------------ cOglCmdInitOutputFb --------------------
cOglCmdInitOutputFb::cOglCmdInitOutputFb(cOglOutputFb * oFb):cOglCmd(NULL)
{
2018-09-05 20:39:12 +02:00
this->oFb = oFb;
}
bool cOglCmdInitOutputFb::Execute(void)
{
2018-09-05 20:39:12 +02:00
bool ok = oFb->Init();
2018-09-05 20:39:12 +02:00
oFb->Unbind();
return ok;
}
2018-09-28 08:55:44 +02:00
2018-09-05 20:39:12 +02:00
//------------------ cOglCmdInitFb --------------------
cOglCmdInitFb::cOglCmdInitFb(cOglFb * fb, cCondWait * wait):cOglCmd(fb)
{
2018-09-05 20:39:12 +02:00
this->wait = wait;
}
bool cOglCmdInitFb::Execute(void)
{
2018-09-05 20:39:12 +02:00
bool ok = fb->Init();
2018-09-05 20:39:12 +02:00
fb->Unbind();
if (wait)
wait->Signal();
return ok;
}
//------------------ cOglCmdDeleteFb --------------------
cOglCmdDeleteFb::cOglCmdDeleteFb(cOglFb * fb):cOglCmd(fb)
{
2018-09-05 20:39:12 +02:00
}
bool cOglCmdDeleteFb::Execute(void)
{
2019-10-28 21:43:37 +01:00
if (fb)
delete fb;
2018-09-05 20:39:12 +02:00
return true;
}
//------------------ cOglCmdRenderFbToBufferFb --------------------
cOglCmdRenderFbToBufferFb::cOglCmdRenderFbToBufferFb(cOglFb * fb, cOglFb * buffer, GLint x, GLint y, GLint transparency, GLint drawPortX, GLint drawPortY):cOglCmd
(fb)
{
2018-09-05 20:39:12 +02:00
this->buffer = buffer;
this->x = (GLfloat) x;
this->y = (GLfloat) y;
this->drawPortX = (GLfloat) drawPortX;
this->drawPortY = (GLfloat) drawPortY;
2018-09-05 20:39:12 +02:00
this->transparency = transparency;
2020-03-26 12:36:00 +01:00
2018-09-05 20:39:12 +02:00
}
bool cOglCmdRenderFbToBufferFb::Execute(void)
{
GLfloat x2 = x + fb->ViewportWidth(); //right
GLfloat y2 = y + fb->ViewportHeight(); //bottom
2018-09-05 20:39:12 +02:00
GLfloat texX1 = drawPortX / (GLfloat) fb->Width();
GLfloat texY1 = drawPortY / (GLfloat) fb->Height();
2018-09-05 20:39:12 +02:00
GLfloat texX2 = texX1 + 1.0f;
GLfloat texY2 = texY1 + 1.0f;
2018-09-05 20:39:12 +02:00
if (fb->Scrollable()) {
GLfloat pageHeight = (GLfloat) fb->ViewportHeight() / (GLfloat) fb->Height();
texX1 = abs(drawPortX) / (GLfloat) fb->Width();
texY1 = 1.0f - pageHeight - abs(drawPortY) / (GLfloat) fb->Height();
texX2 = texX1 + (GLfloat) fb->ViewportWidth() / (GLfloat) fb->Width();
2020-03-29 18:36:54 +02:00
// x2 = x + fb->Width();
2018-09-05 20:39:12 +02:00
texY2 = texY1 + pageHeight;
}
GLfloat quadVertices[] = {
// Pos // TexCoords
x, y, texX1, texY2, //left top
x, y2, texX1, texY1, //left bottom
x2, y2, texX2, texY1, //right bottom
2018-09-05 20:39:12 +02:00
x, y, texX1, texY2, //left top
x2, y2, texX2, texY1, //right bottom
x2, y, texX2, texY2 //right top
2018-09-05 20:39:12 +02:00
};
VertexBuffers[vbTexture]->ActivateShader();
VertexBuffers[vbTexture]->SetShaderAlpha(transparency);
VertexBuffers[vbTexture]->SetShaderProjectionMatrix(buffer->Width(), buffer->Height());
buffer->Bind();
2020-03-26 12:36:00 +01:00
2018-09-05 20:39:12 +02:00
if (!fb->BindTexture())
return false;
VertexBuffers[vbTexture]->Bind();
2018-09-05 20:39:12 +02:00
VertexBuffers[vbTexture]->SetVertexData(quadVertices);
VertexBuffers[vbTexture]->DrawArrays();
VertexBuffers[vbTexture]->Unbind();
buffer->Unbind();
return true;
}
2018-09-28 08:55:44 +02:00
2018-09-05 20:39:12 +02:00
//------------------ cOglCmdCopyBufferToOutputFb --------------------
cOglCmdCopyBufferToOutputFb::cOglCmdCopyBufferToOutputFb(cOglFb * fb, cOglOutputFb * oFb, GLint x, GLint y):cOglCmd(fb)
{
2018-09-05 20:39:12 +02:00
this->oFb = oFb;
this->x = x;
this->y = y;
}
extern unsigned char *posd;
bool cOglCmdCopyBufferToOutputFb::Execute(void)
{
2019-10-04 10:37:57 +02:00
2018-09-28 08:55:44 +02:00
pthread_mutex_lock(&OSDMutex);
2018-09-05 20:39:12 +02:00
fb->BindRead();
2018-11-17 14:58:25 +01:00
2019-10-28 21:43:37 +01:00
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glPixelStorei(GL_PACK_ALIGNMENT, 1);
if (posd)
glReadPixels(0, 0, fb->Width(), fb->Height(), GL_RGBA, GL_UNSIGNED_BYTE, posd);
glFlush();
2020-03-26 12:36:00 +01:00
2019-10-28 21:43:37 +01:00
pthread_mutex_unlock(&OSDMutex);
ActivateOsd(oFb->texture, x, y, fb->Width(), fb->Height());
2018-09-05 20:39:12 +02:00
return true;
}
2018-09-28 08:55:44 +02:00
2018-09-05 20:39:12 +02:00
//------------------ cOglCmdFill --------------------
cOglCmdFill::cOglCmdFill(cOglFb * fb, GLint color):cOglCmd(fb)
{
2018-09-05 20:39:12 +02:00
this->color = color;
}
bool cOglCmdFill::Execute(void)
{
2018-09-05 20:39:12 +02:00
glm::vec4 col;
ConvertColor(color, col);
fb->Bind();
2019-10-28 21:43:37 +01:00
glClearColor(col.r, col.g, col.b, col.a);
2018-09-05 20:39:12 +02:00
glClear(GL_COLOR_BUFFER_BIT);
fb->Unbind();
return true;
}
//------------------ cOglCmdDrawRectangle --------------------
cOglCmdDrawRectangle::cOglCmdDrawRectangle(cOglFb * fb, GLint x, GLint y, GLint width, GLint height, GLint color):cOglCmd
(fb)
{
2018-09-05 20:39:12 +02:00
this->x = x;
this->y = y;
this->width = width;
this->height = height;
this->color = color;
}
bool cOglCmdDrawRectangle::Execute(void)
{
2018-09-05 20:39:12 +02:00
GLfloat x1 = x;
GLfloat y1 = y;
GLfloat x2 = x + width;
GLfloat y2 = y + height;
GLfloat vertices[] = {
x1, y1, //left top
x2, y1, //right top
x2, y2, //right bottom
x1, y2 //left bottom
2018-09-05 20:39:12 +02:00
};
VertexBuffers[vbRect]->ActivateShader();
VertexBuffers[vbRect]->SetShaderColor(color);
VertexBuffers[vbRect]->SetShaderProjectionMatrix(fb->Width(), fb->Height());
2019-10-28 21:43:37 +01:00
2018-09-05 20:39:12 +02:00
fb->Bind();
VertexBuffers[vbRect]->DisableBlending();
VertexBuffers[vbRect]->Bind();
VertexBuffers[vbRect]->SetVertexData(vertices);
VertexBuffers[vbRect]->DrawArrays();
VertexBuffers[vbRect]->Unbind();
VertexBuffers[vbRect]->EnableBlending();
fb->Unbind();
return true;
}
//------------------ cOglCmdDrawEllipse --------------------
///quadrants:
///< 0 draws the entire ellipse
///< 1..4 draws only the first, second, third or fourth quadrant, respectively
///< 5..8 draws the right, top, left or bottom half, respectively
///< -1..-4 draws the inverted part of the given quadrant
cOglCmdDrawEllipse::cOglCmdDrawEllipse(cOglFb * fb, GLint x, GLint y, GLint width, GLint height, GLint color, GLint quadrants):cOglCmd
(fb)
{
2018-09-05 20:39:12 +02:00
this->x = x;
this->y = y;
this->width = width;
this->height = height;
this->color = color;
this->quadrants = quadrants;
}
bool cOglCmdDrawEllipse::Execute(void)
{
2018-09-05 20:39:12 +02:00
int numVertices = 0;
GLfloat *vertices = NULL;
switch (quadrants) {
case 0:
vertices = CreateVerticesFull(numVertices);
break;
case 1:
case 2:
case 3:
case 4:
case -1:
case -2:
case -3:
case -4:
2018-09-05 20:39:12 +02:00
vertices = CreateVerticesQuadrant(numVertices);
break;
case 5:
case 6:
case 7:
case 8:
2018-09-05 20:39:12 +02:00
vertices = CreateVerticesHalf(numVertices);
break;
default:
break;
}
VertexBuffers[vbEllipse]->ActivateShader();
VertexBuffers[vbEllipse]->SetShaderColor(color);
VertexBuffers[vbEllipse]->SetShaderProjectionMatrix(fb->Width(), fb->Height());
2019-10-28 21:43:37 +01:00
// not antialiased
2018-09-05 20:39:12 +02:00
fb->Bind();
VertexBuffers[vbEllipse]->DisableBlending();
VertexBuffers[vbEllipse]->Bind();
VertexBuffers[vbEllipse]->SetVertexData(vertices, numVertices);
VertexBuffers[vbEllipse]->DrawArrays(numVertices);
VertexBuffers[vbEllipse]->Unbind();
VertexBuffers[vbEllipse]->EnableBlending();
fb->Unbind();
delete[]vertices;
2018-09-05 20:39:12 +02:00
return true;
}
GLfloat *cOglCmdDrawEllipse::CreateVerticesFull(int &numVertices)
{
2018-09-05 20:39:12 +02:00
int size = 364;
numVertices = size / 2;
GLfloat radiusX = (GLfloat) width / 2;
GLfloat radiusY = (GLfloat) height / 2;
2018-09-05 20:39:12 +02:00
GLfloat *vertices = new GLfloat[size];
2018-09-05 20:39:12 +02:00
vertices[0] = x + radiusX;
vertices[1] = y + radiusY;
for (int i = 0; i <= 180; i++) {
vertices[2 * i + 2] = x + radiusX + (GLfloat) cos(2 * i * M_PI / 180.0f) * radiusX;
vertices[2 * i + 3] = y + radiusY - (GLfloat) sin(2 * i * M_PI / 180.0f) * radiusY;
2018-09-05 20:39:12 +02:00
}
return vertices;
}
GLfloat *cOglCmdDrawEllipse::CreateVerticesQuadrant(int &numVertices)
{
2018-09-05 20:39:12 +02:00
int size = 94;
numVertices = size / 2;
GLfloat radiusX = (GLfloat) width;
GLfloat radiusY = (GLfloat) height;
2018-09-05 20:39:12 +02:00
GLint transX = 0;
GLint transY = 0;
GLint startAngle = 0;
GLfloat *vertices = new GLfloat[size];
2018-09-05 20:39:12 +02:00
switch (quadrants) {
case 1:
vertices[0] = x;
vertices[1] = y + height;
transY = radiusY;
break;
case 2:
vertices[0] = x + width;
vertices[1] = y + height;
transX = radiusX;
transY = radiusY;
startAngle = 90;
break;
case 3:
vertices[0] = x + width;
vertices[1] = y;
transX = radiusX;
startAngle = 180;
break;
case 4:
vertices[0] = x;
vertices[1] = y;
startAngle = 270;
break;
case -1:
vertices[0] = x + width;
vertices[1] = y;
transY = radiusY;
break;
case -2:
vertices[0] = x;
vertices[1] = y;
transX = radiusX;
transY = radiusY;
startAngle = 90;
break;
case -3:
vertices[0] = x;
vertices[1] = y + height;
transX = radiusX;
startAngle = 180;
break;
case -4:
vertices[0] = x + width;
vertices[1] = y + height;
startAngle = 270;
2019-10-28 21:43:37 +01:00
break;
2018-09-05 20:39:12 +02:00
default:
break;
}
for (int i = 0; i <= 45; i++) {
vertices[2 * i + 2] = x + transX + (GLfloat) cos((2 * i + startAngle) * M_PI / 180.0f) * radiusX;
vertices[2 * i + 3] = y + transY - (GLfloat) sin((2 * i + startAngle) * M_PI / 180.0f) * radiusY;
2018-09-05 20:39:12 +02:00
}
return vertices;
}
GLfloat *cOglCmdDrawEllipse::CreateVerticesHalf(int &numVertices)
{
2018-09-05 20:39:12 +02:00
int size = 184;
numVertices = size / 2;
2018-09-05 20:39:12 +02:00
GLfloat radiusX = 0.0f;
GLfloat radiusY = 0.0f;
GLint transX = 0;
GLint transY = 0;
2019-10-28 21:43:37 +01:00
GLint startAngle = 0;
2018-09-05 20:39:12 +02:00
GLfloat *vertices = new GLfloat[size];
2018-09-05 20:39:12 +02:00
switch (quadrants) {
case 5:
radiusX = (GLfloat) width;
radiusY = (GLfloat) height / 2;
2018-09-05 20:39:12 +02:00
vertices[0] = x;
vertices[1] = y + radiusY;
startAngle = 270;
transY = radiusY;
break;
case 6:
radiusX = (GLfloat) width / 2;
radiusY = (GLfloat) height;
2018-09-05 20:39:12 +02:00
vertices[0] = x + radiusX;
vertices[1] = y + radiusY;
startAngle = 0;
transX = radiusX;
transY = radiusY;
break;
case 7:
radiusX = (GLfloat) width;
radiusY = (GLfloat) height / 2;
2018-09-05 20:39:12 +02:00
vertices[0] = x + radiusX;
vertices[1] = y + radiusY;
startAngle = 90;
transX = radiusX;
transY = radiusY;
break;
case 8:
radiusX = (GLfloat) width / 2;
radiusY = (GLfloat) height;
2018-09-05 20:39:12 +02:00
vertices[0] = x + radiusX;
vertices[1] = y;
startAngle = 180;
transX = radiusX;
break;
default:
break;
}
for (int i = 0; i <= 90; i++) {
vertices[2 * i + 2] = x + transX + (GLfloat) cos((2 * i + startAngle) * M_PI / 180.0f) * radiusX;
vertices[2 * i + 3] = y + transY - (GLfloat) sin((2 * i + startAngle) * M_PI / 180.0f) * radiusY;
2018-09-05 20:39:12 +02:00
}
return vertices;
}
//------------------ cOglCmdDrawSlope --------------------
///type:
///< 0: horizontal, rising, lower
///< 1: horizontal, rising, upper
///< 2: horizontal, falling, lower
///< 3: horizontal, falling, upper
///< 4: vertical, rising, lower
///< 5: vertical, rising, upper
///< 6: vertical, falling, lower
///< 7: vertical, falling, upper
cOglCmdDrawSlope::cOglCmdDrawSlope(cOglFb * fb, GLint x, GLint y, GLint width, GLint height, GLint color, GLint type):cOglCmd
(fb)
{
2018-09-05 20:39:12 +02:00
this->x = x;
this->y = y;
this->width = width;
this->height = height;
this->color = color;
this->type = type;
}
bool cOglCmdDrawSlope::Execute(void)
{
bool falling = type & 0x02;
2018-09-05 20:39:12 +02:00
bool vertical = type & 0x04;
int steps = 100;
2018-09-05 20:39:12 +02:00
if (width < 100)
steps = 25;
int numVertices = steps + 2;
GLfloat *vertices = new GLfloat[numVertices * 2];
2018-09-05 20:39:12 +02:00
switch (type) {
case 0:
case 4:
vertices[0] = (GLfloat) (x + width);
vertices[1] = (GLfloat) (y + height);
2018-09-05 20:39:12 +02:00
break;
case 1:
case 5:
vertices[0] = (GLfloat) x;
vertices[1] = (GLfloat) y;
2018-09-05 20:39:12 +02:00
break;
case 2:
case 6:
vertices[0] = (GLfloat) x;
vertices[1] = (GLfloat) (y + height);
2018-09-05 20:39:12 +02:00
break;
case 3:
case 7:
vertices[0] = (GLfloat) (x + width);
vertices[1] = (GLfloat) y;
2018-09-05 20:39:12 +02:00
break;
default:
vertices[0] = (GLfloat) (x);
vertices[1] = (GLfloat) (y);
2018-09-05 20:39:12 +02:00
break;
}
for (int i = 0; i <= steps; i++) {
GLfloat c = cos(i * M_PI / steps);
2018-09-05 20:39:12 +02:00
if (falling)
c = -c;
if (vertical) {
vertices[2 * i + 2] = (GLfloat) x + (GLfloat) width / 2.0f + (GLfloat) width *c / 2.0f;
vertices[2 * i + 3] = (GLfloat) y + (GLfloat) i *((GLfloat) height) / steps;
2018-09-05 20:39:12 +02:00
} else {
vertices[2 * i + 2] = (GLfloat) x + (GLfloat) i *((GLfloat) width) / steps;
vertices[2 * i + 3] = (GLfloat) y + (GLfloat) height / 2.0f + (GLfloat) height *c / 2.0f;
2018-09-05 20:39:12 +02:00
}
}
VertexBuffers[vbSlope]->ActivateShader();
VertexBuffers[vbSlope]->SetShaderColor(color);
VertexBuffers[vbSlope]->SetShaderProjectionMatrix(fb->Width(), fb->Height());
2019-10-28 21:43:37 +01:00
// not antialiased
2018-09-05 20:39:12 +02:00
fb->Bind();
VertexBuffers[vbSlope]->DisableBlending();
VertexBuffers[vbSlope]->Bind();
VertexBuffers[vbSlope]->SetVertexData(vertices, numVertices);
VertexBuffers[vbSlope]->DrawArrays(numVertices);
VertexBuffers[vbSlope]->Unbind();
VertexBuffers[vbSlope]->EnableBlending();
fb->Unbind();
delete[]vertices;
2018-09-05 20:39:12 +02:00
return true;
}
//------------------ cOglCmdDrawText --------------------
cOglCmdDrawText::cOglCmdDrawText(cOglFb * fb, GLint x, GLint y, unsigned int *symbols, GLint limitX, const char *name,
int fontSize, tColor colorText):cOglCmd(fb), fontName(name)
{
2018-09-05 20:39:12 +02:00
this->x = x;
this->y = y;
this->limitX = limitX;
this->colorText = colorText;
this->fontSize = fontSize;
this->symbols = symbols;
this->fontName = name;
2018-09-05 20:39:12 +02:00
}
cOglCmdDrawText::~cOglCmdDrawText(void)
{
2018-09-05 20:39:12 +02:00
free(symbols);
}
bool cOglCmdDrawText::Execute(void)
{
2018-09-05 20:39:12 +02:00
cOglFont *f = cOglFont::Get(*fontName, fontSize);
2018-09-05 20:39:12 +02:00
if (!f)
return false;
VertexBuffers[vbText]->ActivateShader();
VertexBuffers[vbText]->SetShaderColor(colorText);
VertexBuffers[vbText]->SetShaderProjectionMatrix(fb->Width(), fb->Height());
fb->Bind();
VertexBuffers[vbText]->Bind();
int xGlyph = x;
int fontHeight = f->Height();
int bottom = f->Bottom();
2020-03-02 16:06:27 +01:00
FT_ULong sym = 0;
FT_ULong prevSym = 0;
2018-09-05 20:39:12 +02:00
int kerning = 0;
for (int i = 0; symbols[i]; i++) {
sym = symbols[i];
cOglGlyph *g = f->Glyph(sym);
2018-09-05 20:39:12 +02:00
if (!g) {
2020-03-02 16:06:27 +01:00
esyslog("[softhddev]ERROR: could not load glyph %lx", sym);
2018-09-05 20:39:12 +02:00
}
if (limitX && xGlyph + g->AdvanceX() > limitX) {
2018-09-05 20:39:12 +02:00
break;
}
2018-09-05 20:39:12 +02:00
kerning = f->Kerning(g, prevSym);
prevSym = sym;
GLfloat x1 = xGlyph + kerning + g->BearingLeft(); //left
GLfloat y1 = y + (fontHeight - bottom - g->BearingTop()); //top
GLfloat x2 = x1 + g->Width(); //right
GLfloat y2 = y1 + g->Height(); //bottom
2018-09-05 20:39:12 +02:00
GLfloat vertices[] = {
x1, y2, 0.0, 1.0, // left bottom
x1, y1, 0.0, 0.0, // left top
x2, y1, 1.0, 0.0, // right top
2018-09-05 20:39:12 +02:00
x1, y2, 0.0, 1.0, // left bottom
x2, y1, 1.0, 0.0, // right top
x2, y2, 1.0, 1.0 // right bottom
2018-09-05 20:39:12 +02:00
};
2019-10-28 21:43:37 +01:00
2018-09-05 20:39:12 +02:00
g->BindTexture();
VertexBuffers[vbText]->SetVertexData(vertices);
VertexBuffers[vbText]->DrawArrays();
xGlyph += kerning + g->AdvanceX();
if (xGlyph > fb->Width() - 1)
2018-09-05 20:39:12 +02:00
break;
}
glBindTexture(GL_TEXTURE_2D, 0);
VertexBuffers[vbText]->Unbind();
fb->Unbind();
return true;
}
//------------------ cOglCmdDrawImage --------------------
cOglCmdDrawImage::cOglCmdDrawImage(cOglFb * fb, tColor * argb, GLint width, GLint height, GLint x, GLint y,
bool overlay, double scaleX, double scaleY):cOglCmd(fb)
{
2018-09-05 20:39:12 +02:00
this->argb = argb;
this->x = x;
this->y = y;
this->width = width;
this->height = height;
this->overlay = overlay;
this->scaleX = scaleX;
this->scaleY = scaleY;
2018-09-05 20:39:12 +02:00
}
cOglCmdDrawImage::~cOglCmdDrawImage(void)
{
2018-09-05 20:39:12 +02:00
free(argb);
}
bool cOglCmdDrawImage::Execute(void)
{
2018-09-05 20:39:12 +02:00
GLuint texture;
#ifdef USE_DRM
// pthread_mutex_lock(&OSDMutex);
GlxDrawopengl(); // here we need the Shared Context for upload
GlxCheck();
#endif
2018-09-05 20:39:12 +02:00
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, argb);
2018-09-05 20:39:12 +02:00
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glBindTexture(GL_TEXTURE_2D, 0);
glFlush();
#ifdef USE_DRM
GlxInitopengl(); // Reset Context
GlxCheck();
// pthread_mutex_unlock(&OSDMutex);
#endif
2019-10-28 21:43:37 +01:00
GLfloat x1 = x; //left
GLfloat y1 = y; //top
GLfloat x2 = x + width; //right
GLfloat y2 = y + height; //bottom
2018-09-05 20:39:12 +02:00
GLfloat quadVertices[] = {
x1, y2, 0.0, 1.0, // left bottom
x1, y1, 0.0, 0.0, // left top
x2, y1, 1.0, 0.0, // right top
2018-09-05 20:39:12 +02:00
x1, y2, 0.0, 1.0, // left bottom
x2, y1, 1.0, 0.0, // right top
x2, y2, 1.0, 1.0 // right bottom
2018-09-05 20:39:12 +02:00
};
VertexBuffers[vbTexture]->ActivateShader();
VertexBuffers[vbTexture]->SetShaderAlpha(255);
VertexBuffers[vbTexture]->SetShaderProjectionMatrix(fb->Width(), fb->Height());
fb->Bind();
glBindTexture(GL_TEXTURE_2D, texture);
if (overlay)
VertexBuffers[vbTexture]->DisableBlending();
VertexBuffers[vbTexture]->Bind();
VertexBuffers[vbTexture]->SetVertexData(quadVertices);
VertexBuffers[vbTexture]->DrawArrays();
VertexBuffers[vbTexture]->Unbind();
if (overlay)
VertexBuffers[vbTexture]->EnableBlending();
fb->Unbind();
glBindTexture(GL_TEXTURE_2D, 0);
glDeleteTextures(1, &texture);
return true;
}
//------------------ cOglCmdDrawTexture --------------------
cOglCmdDrawTexture::cOglCmdDrawTexture(cOglFb * fb, sOglImage * imageRef, GLint x, GLint y):cOglCmd(fb)
{
2018-09-05 20:39:12 +02:00
this->imageRef = imageRef;
this->x = x;
this->y = y;
}
bool cOglCmdDrawTexture::Execute(void)
{
GLfloat x1 = x; //top
GLfloat y1 = y; //left
GLfloat x2 = x + imageRef->width; //right
GLfloat y2 = y + imageRef->height; //bottom
2018-09-05 20:39:12 +02:00
GLfloat quadVertices[] = {
// Pos // TexCoords
x1, y1, 0.0f, 0.0f, //left bottom
x1, y2, 0.0f, 1.0f, //left top
x2, y2, 1.0f, 1.0f, //right top
2018-09-05 20:39:12 +02:00
x1, y1, 0.0f, 0.0f, //left bottom
x2, y2, 1.0f, 1.0f, //right top
x2, y1, 1.0f, 0.0f //right bottom
2018-09-05 20:39:12 +02:00
};
VertexBuffers[vbTexture]->ActivateShader();
2018-09-26 17:20:03 +02:00
VertexBuffers[vbTexture]->SetShaderAlpha(255);
2018-09-05 20:39:12 +02:00
VertexBuffers[vbTexture]->SetShaderProjectionMatrix(fb->Width(), fb->Height());
fb->Bind();
glBindTexture(GL_TEXTURE_2D, imageRef->texture);
VertexBuffers[vbTexture]->Bind();
VertexBuffers[vbTexture]->SetVertexData(quadVertices);
VertexBuffers[vbTexture]->DrawArrays();
VertexBuffers[vbTexture]->Unbind();
fb->Unbind();
return true;
}
//------------------ cOglCmdStoreImage --------------------
cOglCmdStoreImage::cOglCmdStoreImage(sOglImage * imageRef, tColor * argb):cOglCmd(NULL)
{
2018-09-05 20:39:12 +02:00
this->imageRef = imageRef;
2019-10-28 21:43:37 +01:00
data = argb;
2018-09-05 20:39:12 +02:00
}
cOglCmdStoreImage::~cOglCmdStoreImage(void)
{
2018-09-05 20:39:12 +02:00
free(data);
}
bool cOglCmdStoreImage::Execute(void)
{
#ifdef USE_DRM
// pthread_mutex_lock(&OSDMutex);
GlxDrawopengl(); // here we need the Shared Context for upload
GlxCheck();
#endif
2018-09-05 20:39:12 +02:00
glGenTextures(1, &imageRef->texture);
glBindTexture(GL_TEXTURE_2D, imageRef->texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, imageRef->width, imageRef->height, 0, GL_BGRA,
GL_UNSIGNED_INT_8_8_8_8_REV, data);
2018-09-05 20:39:12 +02:00
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glBindTexture(GL_TEXTURE_2D, 0);
glFlush();
#ifdef USE_DRM
GlxInitopengl(); // Reset Context
GlxCheck();
// pthread_mutex_lock(&OSDMutex);
#endif
2018-09-05 20:39:12 +02:00
return true;
}
//------------------ cOglCmdDropImage --------------------
cOglCmdDropImage::cOglCmdDropImage(sOglImage * imageRef, cCondWait * wait):cOglCmd(NULL)
{
2018-09-05 20:39:12 +02:00
this->imageRef = imageRef;
this->wait = wait;
}
bool cOglCmdDropImage::Execute(void)
{
2018-09-05 20:39:12 +02:00
if (imageRef->texture != GL_NONE)
glDeleteTextures(1, &imageRef->texture);
wait->Signal();
return true;
}
/******************************************************************************
* cOglThread
******************************************************************************/
cOglThread::cOglThread(cCondWait * startWait, int maxCacheSize):cThread("oglThread")
{
2018-09-05 20:39:12 +02:00
stalled = false;
memCached = 0;
2018-09-05 20:39:12 +02:00
this->maxCacheSize = maxCacheSize * 1024 * 1024;
this->startWait = startWait;
wait = new cCondWait();
maxTextureSize = 0;
for (int i = 0; i < OGL_MAX_OSDIMAGES; i++) {
imageCache[i].used = false;
2019-10-28 21:43:37 +01:00
imageCache[i].texture = GL_NONE;
2018-09-05 20:39:12 +02:00
imageCache[i].width = 0;
imageCache[i].height = 0;
}
Start();
2018-09-05 20:39:12 +02:00
}
cOglThread::~cOglThread()
{
2018-09-05 20:39:12 +02:00
delete wait;
2018-09-05 20:39:12 +02:00
wait = NULL;
}
void cOglThread::Stop(void)
{
2018-09-05 20:39:12 +02:00
for (int i = 0; i < OGL_MAX_OSDIMAGES; i++) {
if (imageCache[i].used) {
DropImageData(i);
}
}
Cancel(2);
stalled = false;
}
void cOglThread::DoCmd(cOglCmd * cmd)
{
2018-09-05 20:39:12 +02:00
while (stalled)
cCondWait::SleepMs(10);
2019-10-28 21:43:37 +01:00
2018-09-05 20:39:12 +02:00
bool doSignal = false;
2018-09-05 20:39:12 +02:00
Lock();
if (commands.size() == 0)
doSignal = true;
commands.push(cmd);
Unlock();
if (commands.size() > OGL_CMDQUEUE_SIZE) {
stalled = true;
}
if (doSignal || stalled)
wait->Signal();
}
int cOglThread::StoreImage(const cImage & image)
{
if (!maxCacheSize) {
return 0;
}
2019-11-05 22:06:54 +01:00
2018-09-05 20:39:12 +02:00
if (image.Width() > maxTextureSize || image.Height() > maxTextureSize) {
esyslog("[softhddev] cannot store image of %dpx x %dpx " "(maximum size is %dpx x %dpx) - falling back to "
"cOsdProvider::StoreImageData()", image.Width(), image.Height(), maxTextureSize, maxTextureSize);
2018-09-05 20:39:12 +02:00
return 0;
}
int imgSize = image.Width() * image.Height();
int newMemUsed = imgSize * sizeof(tColor) + memCached;
2018-09-05 20:39:12 +02:00
if (newMemUsed > maxCacheSize) {
float cachedMB = memCached / 1024.0f / 1024.0f;
float maxMB = maxCacheSize / 1024.0f / 1024.0f;
2018-09-05 20:39:12 +02:00
esyslog("[softhddev]Maximum size for GPU cache reached. Used: %.2fMB Max: %.2fMB", cachedMB, maxMB);
return 0;
}
2019-10-28 21:43:37 +01:00
2018-09-05 20:39:12 +02:00
int slot = GetFreeSlot();
2018-09-05 20:39:12 +02:00
if (!slot)
return 0;
tColor *argb = MALLOC(tColor, imgSize);
2018-09-05 20:39:12 +02:00
if (!argb) {
esyslog("[softhddev]memory allocation of %ld kb for OSD image failed", (imgSize * sizeof(tColor)) / 1024);
2018-09-05 20:39:12 +02:00
ClearSlot(slot);
slot = 0;
return 0;
}
memcpy(argb, image.Data(), sizeof(tColor) * imgSize);
sOglImage *imageRef = GetImageRef(slot);
2018-09-05 20:39:12 +02:00
imageRef->width = image.Width();
imageRef->height = image.Height();
DoCmd(new cOglCmdStoreImage(imageRef, argb));
cTimeMs timer(5000);
2018-09-05 20:39:12 +02:00
while (imageRef->used && imageRef->texture == 0 && !timer.TimedOut())
cCondWait::SleepMs(2);
if (imageRef->texture == GL_NONE) {
esyslog("[softhddev]failed to store OSD image texture! (%s)",
timer.TimedOut()? "timed out" : "allocation failed");
2018-09-05 20:39:12 +02:00
DropImageData(slot);
slot = 0;
}
memCached += imgSize * sizeof(tColor);
2018-09-05 20:39:12 +02:00
return slot;
}
int cOglThread::GetFreeSlot(void)
{
2018-09-05 20:39:12 +02:00
Lock();
int slot = 0;
2018-09-05 20:39:12 +02:00
for (int i = 0; i < OGL_MAX_OSDIMAGES && !slot; i++) {
if (!imageCache[i].used) {
imageCache[i].used = true;
slot = -i - 1;
}
}
Unlock();
return slot;
}
void cOglThread::ClearSlot(int slot)
{
2018-09-05 20:39:12 +02:00
int i = -slot - 1;
2018-09-05 20:39:12 +02:00
if (i >= 0 && i < OGL_MAX_OSDIMAGES) {
Lock();
2019-10-28 21:43:37 +01:00
imageCache[i].used = false;
2018-09-05 20:39:12 +02:00
imageCache[i].texture = GL_NONE;
imageCache[i].width = 0;
imageCache[i].height = 0;
Unlock();
}
}
sOglImage *cOglThread::GetImageRef(int slot)
{
2018-09-05 20:39:12 +02:00
int i = -slot - 1;
2018-09-05 20:39:12 +02:00
if (0 <= i && i < OGL_MAX_OSDIMAGES)
return &imageCache[i];
return 0;
}
void cOglThread::DropImageData(int imageHandle)
{
2018-09-05 20:39:12 +02:00
sOglImage *imageRef = GetImageRef(imageHandle);
2018-09-05 20:39:12 +02:00
if (!imageRef)
return;
int imgSize = imageRef->width * imageRef->height * sizeof(tColor);
2018-09-05 20:39:12 +02:00
memCached -= imgSize;
cCondWait dropWait;
2018-09-05 20:39:12 +02:00
DoCmd(new cOglCmdDropImage(imageRef, &dropWait));
dropWait.Wait();
ClearSlot(imageHandle);
}
void cOglThread::Action(void)
{
2018-09-05 20:39:12 +02:00
if (!InitOpenGL()) {
esyslog("[softhddev]Could not initiate OpenGL Context");
Cleanup();
startWait->Signal();
return;
}
dsyslog("[softhddev]OpenGL Context initialized");
2019-10-28 21:43:37 +01:00
2018-09-05 20:39:12 +02:00
if (!InitShaders()) {
esyslog("[softhddev]Could not initiate Shaders");
Cleanup();
startWait->Signal();
return;
}
dsyslog("[softhddev]Shaders initialized");
2019-10-28 21:43:37 +01:00
2018-09-05 20:39:12 +02:00
if (!InitVdpauInterop()) {
esyslog("[softhddev]: vdpau interop NOT initialized");
Cleanup();
startWait->Signal();
return;
}
dsyslog("[softhddev]vdpau interop initialized");
if (!InitVertexBuffers()) {
esyslog("[softhddev]: Vertex Buffers NOT initialized");
Cleanup();
startWait->Signal();
return;
}
dsyslog("[softhddev]Vertex buffers initialized");
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
dsyslog("[softhddev]Maximum Pixmap size: %dx%dpx", maxTextureSize, maxTextureSize);
//now Thread is ready to do his job
startWait->Signal();
stalled = false;
2019-10-28 21:43:37 +01:00
while (Running()) {
2018-09-05 20:39:12 +02:00
if (commands.empty()) {
wait->Wait(20);
continue;
}
Lock();
cOglCmd *cmd = commands.front();
2018-09-05 20:39:12 +02:00
commands.pop();
Unlock();
2019-10-28 21:43:37 +01:00
// uint64_t start = cTimeMs::Now();
2018-09-05 20:39:12 +02:00
cmd->Execute();
2019-10-28 21:43:37 +01:00
// esyslog("[softhddev]\"%s\", %dms, %d commands left, time %" PRIu64 "", cmd->Description(), (int)(cTimeMs::Now() - start), commands.size(), cTimeMs::Now());
2018-09-05 20:39:12 +02:00
delete cmd;
2018-09-05 20:39:12 +02:00
if (stalled && commands.size() < OGL_CMDQUEUE_SIZE / 2)
stalled = false;
}
2019-10-28 21:43:37 +01:00
2018-09-05 20:39:12 +02:00
dsyslog("[softhddev]Cleaning up OpenGL stuff");
Cleanup();
dsyslog("[softhddev]OpenGL Worker Thread Ended");
}
bool cOglThread::InitOpenGL(void)
{
#ifdef USE_DRM
GlxInitopengl();
#else
2018-09-05 20:39:12 +02:00
const char *displayName = X11DisplayName;
2018-09-05 20:39:12 +02:00
if (!displayName) {
displayName = getenv("DISPLAY");
if (!displayName) {
displayName = ":0.0";
}
}
2018-11-17 14:58:25 +01:00
2018-09-05 20:39:12 +02:00
dsyslog("[softhddev]OpenGL using display %s", displayName);
int argc = 3;
char *buffer[3];
2018-09-05 20:39:12 +02:00
buffer[0] = strdup("openglosd");
buffer[1] = strdup("-display");
buffer[2] = strdup(displayName);
2019-01-04 09:21:47 +01:00
2018-09-05 20:39:12 +02:00
char **argv = buffer;
2018-09-05 20:39:12 +02:00
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA | GLUT_ALPHA);
glutInitWindowSize(1, 1);
glutInitWindowPosition(0, 0);
2018-09-05 20:39:12 +02:00
glutCreateWindow("openglosd");
glutHideWindow();
free(buffer[0]);
free(buffer[1]);
free(buffer[2]);
2019-10-28 21:43:37 +01:00
2018-09-05 20:39:12 +02:00
GLenum err = glewInit();
if (err != GLEW_OK) {
2018-09-05 20:39:12 +02:00
esyslog("[softhddev]glewInit failed, aborting\n");
return false;
}
#endif
2018-09-05 20:39:12 +02:00
VertexBuffers[vbText]->EnableBlending();
glDisable(GL_DEPTH_TEST);
return true;
}
bool cOglThread::InitShaders(void)
{
for (int i = 0; i < stCount; i++) {
2018-09-05 20:39:12 +02:00
cShader *shader = new cShader();
if (!shader->Load((eShaderType) i))
2018-09-05 20:39:12 +02:00
return false;
Shaders[i] = shader;
}
return true;
}
void cOglThread::DeleteShaders(void)
{
for (int i = 0; i < stCount; i++)
2018-09-05 20:39:12 +02:00
delete Shaders[i];
}
bool cOglThread::InitVdpauInterop(void)
{
2018-09-05 20:39:12 +02:00
#if 0
void *vdpDevice = GetVDPAUDevice();
void *procAdress = GetVDPAUProcAdress();
while (glGetError() != GL_NO_ERROR) ;
2018-09-05 20:39:12 +02:00
glVDPAUInitNV(vdpDevice, procAdress);
2019-10-28 21:43:37 +01:00
if (glGetError() != GL_NO_ERROR)
2018-09-05 20:39:12 +02:00
return false;
#endif
return true;
}
bool cOglThread::InitVertexBuffers(void)
{
for (int i = 0; i < vbCount; i++) {
2018-09-05 20:39:12 +02:00
cOglVb *vb = new cOglVb(i);
2018-09-05 20:39:12 +02:00
if (!vb->Init())
return false;
VertexBuffers[i] = vb;
}
return true;
}
void cOglThread::DeleteVertexBuffers(void)
{
for (int i = 0; i < vbCount; i++) {
2018-09-05 20:39:12 +02:00
delete VertexBuffers[i];
}
}
void cOglThread::Cleanup(void)
{
2019-10-28 21:43:37 +01:00
esyslog("[softhddev]OglThread cleanup\n");
pthread_mutex_lock(&OSDMutex);
OsdClose();
2018-09-05 20:39:12 +02:00
DeleteVertexBuffers();
2018-10-12 12:51:52 +02:00
delete cOglOsd::oFb;
2018-09-05 20:39:12 +02:00
cOglOsd::oFb = NULL;
DeleteShaders();
2019-10-28 21:43:37 +01:00
// glVDPAUFiniNV();
2018-09-05 20:39:12 +02:00
cOglFont::Cleanup();
#ifndef USE_DRM
2018-11-17 14:58:25 +01:00
glutExit();
2020-01-13 18:17:07 +01:00
#else
GlxDestroy();
#endif
2019-10-28 21:43:37 +01:00
pthread_mutex_unlock(&OSDMutex);
2018-09-05 20:39:12 +02:00
}
/****************************************************************************************
* cOglPixmap
****************************************************************************************/
cOglPixmap::cOglPixmap(std::shared_ptr < cOglThread > oglThread, int Layer, const cRect & ViewPort,
const cRect & DrawPort):cPixmap(Layer, ViewPort, DrawPort)
{
2018-09-05 20:39:12 +02:00
this->oglThread = oglThread;
int width = DrawPort.IsEmpty()? ViewPort.Width() : DrawPort.Width();
int height = DrawPort.IsEmpty()? ViewPort.Height() : DrawPort.Height();
2018-09-05 20:39:12 +02:00
fb = new cOglFb(width, height, ViewPort.Width(), ViewPort.Height());
2019-10-28 21:43:37 +01:00
dirty = true;
2018-09-05 20:39:12 +02:00
}
cOglPixmap::~cOglPixmap(void)
{
2018-09-05 20:39:12 +02:00
if (!oglThread->Active())
return;
oglThread->DoCmd(new cOglCmdDeleteFb(fb));
}
void cOglPixmap::SetAlpha(int Alpha)
{
2018-09-05 20:39:12 +02:00
Alpha = constrain(Alpha, ALPHA_TRANSPARENT, ALPHA_OPAQUE);
if (Alpha != cPixmap::Alpha()) {
cPixmap::SetAlpha(Alpha);
SetDirty();
}
}
void cOglPixmap::SetTile(bool Tile)
{
2018-09-05 20:39:12 +02:00
cPixmap::SetTile(Tile);
SetDirty();
}
void cOglPixmap::SetViewPort(const cRect & Rect)
{
2018-09-05 20:39:12 +02:00
cPixmap::SetViewPort(Rect);
SetDirty();
}
void cOglPixmap::SetDrawPortPoint(const cPoint & Point, bool Dirty)
{
2018-09-05 20:39:12 +02:00
cPixmap::SetDrawPortPoint(Point, Dirty);
if (Dirty)
SetDirty();
}
void cOglPixmap::Clear(void)
{
2018-09-05 20:39:12 +02:00
if (!oglThread->Active())
return;
LOCK_PIXMAPS;
oglThread->DoCmd(new cOglCmdFill(fb, clrTransparent));
SetDirty();
MarkDrawPortDirty(DrawPort());
}
void cOglPixmap::Fill(tColor Color)
{
2018-09-05 20:39:12 +02:00
if (!oglThread->Active())
return;
LOCK_PIXMAPS;
oglThread->DoCmd(new cOglCmdFill(fb, Color));
SetDirty();
MarkDrawPortDirty(DrawPort());
}
void cOglPixmap::DrawImage(const cPoint & Point, const cImage & Image)
{
2018-09-05 20:39:12 +02:00
if (!oglThread->Active())
return;
tColor *argb = MALLOC(tColor, Image.Width() * Image.Height());
2018-09-05 20:39:12 +02:00
if (!argb)
return;
memcpy(argb, Image.Data(), sizeof(tColor) * Image.Width() * Image.Height());
oglThread->DoCmd(new cOglCmdDrawImage(fb, argb, Image.Width(), Image.Height(), Point.X(), Point.Y()));
SetDirty();
MarkDrawPortDirty(cRect(Point, cSize(Image.Width(), Image.Height())).Intersected(DrawPort().Size()));
}
void cOglPixmap::DrawImage(const cPoint & Point, int ImageHandle)
{
2018-09-05 20:39:12 +02:00
if (!oglThread->Active())
return;
2020-03-26 12:36:00 +01:00
2018-09-05 20:39:12 +02:00
if (ImageHandle < 0 && oglThread->GetImageRef(ImageHandle)) {
sOglImage *img = oglThread->GetImageRef(ImageHandle);
oglThread->DoCmd(new cOglCmdDrawTexture(fb, img, Point.X(), Point.Y()));
2018-09-05 20:39:12 +02:00
}
/*
Fallback to VDR implementation, needs to separate cSoftOsdProvider from softhddevice.cpp
else {
if (cSoftOsdProvider::GetImageData(ImageHandle))
DrawImage(Point, *cSoftOsdProvider::GetImageData(ImageHandle));
}
*/
2018-09-05 20:39:12 +02:00
SetDirty();
MarkDrawPortDirty(DrawPort());
}
void cOglPixmap::DrawPixel(const cPoint & Point, tColor Color)
{
2019-01-04 10:15:51 +01:00
cRect r(Point.X(), Point.Y(), 1, 1);
2019-01-04 10:15:51 +01:00
oglThread->DoCmd(new cOglCmdDrawRectangle(fb, r.X(), r.Y(), r.Width(), r.Height(), Color));
2019-10-28 21:43:37 +01:00
2019-01-04 10:15:51 +01:00
SetDirty();
MarkDrawPortDirty(r);
2018-09-05 20:39:12 +02:00
}
void cOglPixmap::DrawBitmap(const cPoint & Point, const cBitmap & Bitmap, tColor ColorFg, tColor ColorBg, bool Overlay)
{
2018-09-05 20:39:12 +02:00
if (!oglThread->Active())
return;
LOCK_PIXMAPS;
bool specialColors = ColorFg || ColorBg;
tColor *argb = MALLOC(tColor, Bitmap.Width() * Bitmap.Height());
2018-09-05 20:39:12 +02:00
if (!argb)
return;
tColor *p = argb;
2018-09-05 20:39:12 +02:00
for (int py = 0; py < Bitmap.Height(); py++)
for (int px = 0; px < Bitmap.Width(); px++) {
tIndex index = *Bitmap.Data(px, py);
*p++ = (!index
&& Overlay) ? clrTransparent : (specialColors ? (index == 0 ? ColorBg : index ==
1 ? ColorFg : Bitmap.Color(index)) : Bitmap.Color(index));
2018-09-05 20:39:12 +02:00
}
2018-09-05 20:39:12 +02:00
oglThread->DoCmd(new cOglCmdDrawImage(fb, argb, Bitmap.Width(), Bitmap.Height(), Point.X(), Point.Y(), Overlay));
SetDirty();
MarkDrawPortDirty(cRect(Point, cSize(Bitmap.Width(), Bitmap.Height())).Intersected(DrawPort().Size()));
}
void cOglPixmap::DrawText(const cPoint & Point, const char *s, tColor ColorFg, tColor ColorBg, const cFont * Font,
int Width, int Height, int Alignment)
{
2018-09-05 20:39:12 +02:00
if (!oglThread->Active())
return;
LOCK_PIXMAPS;
int len = s ? Utf8StrLen(s) : 0;
unsigned int *symbols = MALLOC(unsigned int, len + 1);
2018-09-05 20:39:12 +02:00
if (!symbols)
return;
if (len)
Utf8ToArray(s, symbols, len + 1);
else
symbols[0] = 0;
int x = Point.X();
int y = Point.Y();
int w = Font->Width(s);
int h = Font->Height();
int limitX = 0;
int cw = Width ? Width : w;
int ch = Height ? Height : h;
cRect r(x, y, cw, ch);
2019-10-28 21:43:37 +01:00
2018-09-05 20:39:12 +02:00
if (ColorBg != clrTransparent)
oglThread->DoCmd(new cOglCmdDrawRectangle(fb, r.X(), r.Y(), r.Width(), r.Height(), ColorBg));
if (Width || Height) {
limitX = x + cw;
if (Width) {
if ((Alignment & taLeft) != 0) {
if ((Alignment & taBorder) != 0)
x += std::max(h / TEXT_ALIGN_BORDER, 1);
} else if ((Alignment & taRight) != 0) {
if (w < Width)
x += Width - w;
if ((Alignment & taBorder) != 0)
x -= std::max(h / TEXT_ALIGN_BORDER, 1);
} else { // taCentered
if (w < Width)
x += (Width - w) / 2;
}
2018-09-05 20:39:12 +02:00
}
if (Height) {
if ((Alignment & taTop) != 0) ;
2018-09-05 20:39:12 +02:00
else if ((Alignment & taBottom) != 0) {
if (h < Height)
y += Height - h;
} else { // taCentered
2018-09-05 20:39:12 +02:00
if (h < Height)
y += (Height - h) / 2;
2018-09-05 20:39:12 +02:00
}
}
}
oglThread->DoCmd(new cOglCmdDrawText(fb, x, y, symbols, limitX, Font->FontName(), Font->Size(), ColorFg));
SetDirty();
MarkDrawPortDirty(r);
}
void cOglPixmap::DrawRectangle(const cRect & Rect, tColor Color)
{
2018-09-05 20:39:12 +02:00
if (!oglThread->Active())
return;
LOCK_PIXMAPS;
oglThread->DoCmd(new cOglCmdDrawRectangle(fb, Rect.X(), Rect.Y(), Rect.Width(), Rect.Height(), Color));
SetDirty();
MarkDrawPortDirty(Rect);
}
void cOglPixmap::DrawEllipse(const cRect & Rect, tColor Color, int Quadrants)
{
2018-09-05 20:39:12 +02:00
if (!oglThread->Active())
return;
LOCK_PIXMAPS;
oglThread->DoCmd(new cOglCmdDrawEllipse(fb, Rect.X(), Rect.Y(), Rect.Width(), Rect.Height(), Color, Quadrants));
SetDirty();
MarkDrawPortDirty(Rect);
}
void cOglPixmap::DrawSlope(const cRect & Rect, tColor Color, int Type)
{
2018-09-05 20:39:12 +02:00
if (!oglThread->Active())
return;
LOCK_PIXMAPS;
oglThread->DoCmd(new cOglCmdDrawSlope(fb, Rect.X(), Rect.Y(), Rect.Width(), Rect.Height(), Color, Type));
SetDirty();
MarkDrawPortDirty(Rect);
}
void cOglPixmap::Render(const cPixmap * Pixmap, const cRect & Source, const cPoint & Dest)
{
2018-09-05 20:39:12 +02:00
esyslog("[softhddev] Render %d %d %d not implemented in OpenGl OSD", Pixmap->ViewPort().X(), Source.X(), Dest.X());
}
void cOglPixmap::Copy(const cPixmap * Pixmap, const cRect & Source, const cPoint & Dest)
{
2018-09-05 20:39:12 +02:00
esyslog("[softhddev] Copy %d %d %d not implemented in OpenGl OSD", Pixmap->ViewPort().X(), Source.X(), Dest.X());
}
void cOglPixmap::Scroll(const cPoint & Dest, const cRect & Source)
{
2018-09-05 20:39:12 +02:00
esyslog("[softhddev] Scroll %d %d not implemented in OpenGl OSD", Source.X(), Dest.X());
}
void cOglPixmap::Pan(const cPoint & Dest, const cRect & Source)
{
2018-09-05 20:39:12 +02:00
esyslog("[softhddev] Pan %d %d not implemented in OpenGl OSD", Source.X(), Dest.X());
}
/******************************************************************************
* cOglOsd
******************************************************************************/
cOglOutputFb *cOglOsd::oFb = NULL;
cOglOsd::cOglOsd(int Left, int Top, uint Level, std::shared_ptr < cOglThread > oglThread):cOsd(Left, Top, Level)
{
2018-09-05 20:39:12 +02:00
this->oglThread = oglThread;
bFb = NULL;
isSubtitleOsd = false;
int osdWidth = 0;
int osdHeight = 0;
2019-10-28 21:43:37 +01:00
2020-03-26 12:36:00 +01:00
// pthread_mutex_lock(&OSDMutex);
2018-09-05 20:39:12 +02:00
VideoGetOsdSize(&osdWidth, &osdHeight);
2019-10-28 21:43:37 +01:00
// osdWidth = 1920;
// osdHeight = 1080;
2018-09-05 20:39:12 +02:00
dsyslog("[softhddev]cOglOsd osdLeft %d osdTop %d screenWidth %d screenHeight %d", Left, Top, osdWidth, osdHeight);
2019-11-05 22:06:54 +01:00
2020-03-04 16:34:40 +01:00
#if 0
2019-10-28 21:43:37 +01:00
if (posd)
free(posd);
posd = (unsigned char *)calloc(osdWidth * osdHeight * 4, 1);
2020-03-04 16:34:40 +01:00
#endif
2019-10-28 21:43:37 +01:00
// create output framebuffer
2019-11-05 22:06:54 +01:00
2018-09-05 20:39:12 +02:00
if (!oFb) {
oFb = new cOglOutputFb(osdWidth, osdHeight);
oglThread->DoCmd(new cOglCmdInitOutputFb(oFb));
}
2020-03-26 12:36:00 +01:00
// pthread_mutex_unlock(&OSDMutex);
2018-09-05 20:39:12 +02:00
}
cOglOsd::~cOglOsd()
{
2018-09-05 20:39:12 +02:00
OsdClose();
SetActive(false);
2020-03-04 16:34:40 +01:00
2018-09-05 20:39:12 +02:00
oglThread->DoCmd(new cOglCmdDeleteFb(bFb));
}
eOsdError cOglOsd::SetAreas(const tArea * Areas, int NumAreas)
{
2018-09-05 20:39:12 +02:00
cRect r;
2018-09-05 20:39:12 +02:00
if (NumAreas > 1)
isSubtitleOsd = true;
for (int i = 0; i < NumAreas; i++)
r.Combine(cRect(Areas[i].x1, Areas[i].y1, Areas[i].Width(), Areas[i].Height()));
tArea area = { r.Left(), r.Top(), r.Right(), r.Bottom(), 32 };
2019-10-28 21:43:37 +01:00
// now we know the actuaL osd size, create double buffer frame buffer
2018-09-05 20:39:12 +02:00
if (bFb) {
2019-10-28 21:43:37 +01:00
oglThread->DoCmd(new cOglCmdDeleteFb(bFb));
2018-09-05 20:39:12 +02:00
DestroyPixmap(oglPixmaps[0]);
}
bFb = new cOglFb(r.Width(), r.Height(), r.Width(), r.Height());
cCondWait initiated;
2018-09-05 20:39:12 +02:00
oglThread->DoCmd(new cOglCmdInitFb(bFb, &initiated));
initiated.Wait();
return cOsd::SetAreas(&area, 1);
}
cPixmap *cOglOsd::CreatePixmap(int Layer, const cRect & ViewPort, const cRect & DrawPort)
{
2018-09-05 20:39:12 +02:00
if (!oglThread->Active())
return NULL;
LOCK_PIXMAPS;
int width = DrawPort.IsEmpty()? ViewPort.Width() : DrawPort.Width();
int height = DrawPort.IsEmpty()? ViewPort.Height() : DrawPort.Height();
2018-09-05 20:39:12 +02:00
if (width > oglThread->MaxTextureSize() || height > oglThread->MaxTextureSize()) {
esyslog("[softhddev] cannot allocate pixmap of %dpx x %dpx, clipped to %dpx x %dpx!", width, height,
std::min(width, oglThread->MaxTextureSize()), std::min(height, oglThread->MaxTextureSize()));
2018-09-05 20:39:12 +02:00
width = std::min(width, oglThread->MaxTextureSize());
height = std::min(height, oglThread->MaxTextureSize());
}
cOglPixmap *p = new cOglPixmap(oglThread, Layer, ViewPort, DrawPort);
if (cOsd::AddPixmap(p)) {
2019-10-28 21:43:37 +01:00
// find free slot
2018-09-05 20:39:12 +02:00
for (int i = 0; i < oglPixmaps.Size(); i++)
if (!oglPixmaps[i])
return oglPixmaps[i] = p;
2019-10-28 21:43:37 +01:00
// append at end
2018-09-05 20:39:12 +02:00
oglPixmaps.Append(p);
return p;
}
delete p;
2018-09-05 20:39:12 +02:00
return NULL;
}
void cOglOsd::DestroyPixmap(cPixmap * Pixmap)
{
2018-09-05 20:39:12 +02:00
if (!oglThread->Active())
return;
if (!Pixmap)
return;
2020-03-26 12:36:00 +01:00
2018-09-05 20:39:12 +02:00
LOCK_PIXMAPS;
int start = 1;
2018-09-05 20:39:12 +02:00
if (isSubtitleOsd)
start = 0;
for (int i = start; i < oglPixmaps.Size(); i++) {
if (oglPixmaps[i] == Pixmap) {
if (Pixmap->Layer() >= 0)
oglPixmaps[0]->SetDirty();
oglPixmaps[i] = NULL;
cOsd::DestroyPixmap(Pixmap);
return;
}
}
}
void cOglOsd::Flush(void)
{
2018-09-05 20:39:12 +02:00
if (!oglThread->Active())
return;
LOCK_PIXMAPS;
2019-10-28 21:43:37 +01:00
// check if any pixmap is dirty
2018-09-05 20:39:12 +02:00
bool dirty = false;
2018-09-05 20:39:12 +02:00
for (int i = 0; i < oglPixmaps.Size() && !dirty; i++)
if (oglPixmaps[i] && oglPixmaps[i]->Layer() >= 0 && oglPixmaps[i]->IsDirty())
dirty = true;
if (!dirty)
return;
2019-10-28 21:43:37 +01:00
// clear buffer
// uint64_t start = cTimeMs::Now();
// dsyslog("[softhddev]Start Flush at %" PRIu64 "", cTimeMs::Now());
2019-01-04 09:21:47 +01:00
2018-09-05 20:39:12 +02:00
oglThread->DoCmd(new cOglCmdFill(bFb, clrTransparent));
2019-10-28 21:43:37 +01:00
// render pixmap textures blended to buffer
2018-09-05 20:39:12 +02:00
for (int layer = 0; layer < MAXPIXMAPLAYERS; layer++) {
for (int i = 0; i < oglPixmaps.Size(); i++) {
if (oglPixmaps[i]) {
if (oglPixmaps[i]->Layer() == layer) {
oglThread->DoCmd(new cOglCmdRenderFbToBufferFb(oglPixmaps[i]->Fb(), bFb,
oglPixmaps[i]->ViewPort().X(), (!isSubtitleOsd) ? oglPixmaps[i]->ViewPort().Y() : 0,
oglPixmaps[i]->Alpha(), oglPixmaps[i]->DrawPort().X(), oglPixmaps[i]->DrawPort().Y()));
2018-09-05 20:39:12 +02:00
oglPixmaps[i]->SetDirty(false);
}
}
}
}
2018-09-28 08:55:44 +02:00
oglThread->DoCmd(new cOglCmdCopyBufferToOutputFb(bFb, oFb, Left(), Top()));
2018-09-30 13:20:34 +02:00
2019-10-28 21:43:37 +01:00
// dsyslog("[softhddev]End Flush at %" PRIu64 ", duration %d", cTimeMs::Now(), (int)(cTimeMs::Now()-start));
2018-09-05 20:39:12 +02:00
}
void cOglOsd::DrawScaledBitmap(int x, int y, const cBitmap & Bitmap, double FactorX, double FactorY, bool AntiAlias)
{
2018-09-05 20:39:12 +02:00
(void)FactorX;
(void)FactorY;
(void)AntiAlias;
int yNew = y - oglPixmaps[0]->ViewPort().Y();
2018-09-05 20:39:12 +02:00
oglPixmaps[0]->DrawBitmap(cPoint(x, yNew), Bitmap);
}