2018-09-23 05:48:44 +00:00
|
|
|
/*
|
|
|
|
Looking Glass - KVM FrameRelay (KVMFR) Client
|
2019-02-22 11:16:14 +00:00
|
|
|
Copyright (C) 2017-2019 Geoffrey McRae <geoff@hostfission.com>
|
2018-09-23 05:48:44 +00:00
|
|
|
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
|
|
|
|
*/
|
|
|
|
|
2018-12-12 05:39:04 +00:00
|
|
|
#include "texture.h"
|
2019-04-11 01:12:59 +00:00
|
|
|
#include "common/debug.h"
|
2018-09-23 05:48:44 +00:00
|
|
|
#include "utils.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <SDL2/SDL_egl.h>
|
|
|
|
|
|
|
|
struct EGL_Texture
|
|
|
|
{
|
2018-09-23 10:45:20 +00:00
|
|
|
enum EGL_PixelFormat pixFmt;
|
2018-09-23 05:56:47 +00:00
|
|
|
size_t width, height;
|
2018-09-24 09:48:11 +00:00
|
|
|
bool streaming;
|
2018-09-23 05:56:47 +00:00
|
|
|
|
2018-09-23 10:45:20 +00:00
|
|
|
int textureCount;
|
|
|
|
GLuint textures[3];
|
|
|
|
GLuint samplers[3];
|
2019-01-01 23:30:19 +00:00
|
|
|
size_t planes[3][3];
|
2018-09-23 10:45:20 +00:00
|
|
|
GLintptr offsets[3];
|
2018-12-04 10:23:28 +00:00
|
|
|
GLenum intFormat;
|
2018-09-23 10:45:20 +00:00
|
|
|
GLenum format;
|
2018-12-04 10:23:28 +00:00
|
|
|
GLenum dataType;
|
2018-09-23 10:45:20 +00:00
|
|
|
|
2018-09-23 05:48:44 +00:00
|
|
|
bool hasPBO;
|
|
|
|
GLuint pbo[2];
|
|
|
|
int pboIndex;
|
2018-10-03 14:09:10 +00:00
|
|
|
bool needsUpdate;
|
2018-09-23 05:48:44 +00:00
|
|
|
size_t pboBufferSize;
|
2019-05-22 01:37:27 +00:00
|
|
|
void * pboMap[2];
|
2018-09-23 05:48:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
bool egl_texture_init(EGL_Texture ** texture)
|
|
|
|
{
|
|
|
|
*texture = (EGL_Texture *)malloc(sizeof(EGL_Texture));
|
|
|
|
if (!*texture)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to malloc EGL_Texture");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(*texture, 0, sizeof(EGL_Texture));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void egl_texture_free(EGL_Texture ** texture)
|
|
|
|
{
|
|
|
|
if (!*texture)
|
|
|
|
return;
|
|
|
|
|
2018-09-23 10:45:20 +00:00
|
|
|
if ((*texture)->textureCount > 0)
|
|
|
|
{
|
|
|
|
glDeleteTextures((*texture)->textureCount, (*texture)->textures);
|
|
|
|
glDeleteSamplers((*texture)->textureCount, (*texture)->samplers);
|
|
|
|
}
|
2018-09-23 05:48:44 +00:00
|
|
|
|
|
|
|
if ((*texture)->hasPBO)
|
2019-05-22 01:37:27 +00:00
|
|
|
{
|
|
|
|
for(int i = 0; i < 2; ++i)
|
|
|
|
{
|
|
|
|
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, (*texture)->pbo[i]);
|
|
|
|
glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
|
|
|
|
}
|
|
|
|
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
|
2018-09-23 05:48:44 +00:00
|
|
|
glDeleteBuffers(2, (*texture)->pbo);
|
2019-05-22 01:37:27 +00:00
|
|
|
}
|
2018-09-23 05:48:44 +00:00
|
|
|
|
|
|
|
free(*texture);
|
|
|
|
*texture = NULL;
|
|
|
|
}
|
|
|
|
|
2019-01-01 23:30:19 +00:00
|
|
|
bool egl_texture_setup(EGL_Texture * texture, enum EGL_PixelFormat pixFmt, size_t width, size_t height, size_t stride, bool streaming)
|
2018-09-23 05:48:44 +00:00
|
|
|
{
|
2018-09-24 09:48:11 +00:00
|
|
|
int textureCount;
|
2018-09-23 10:45:20 +00:00
|
|
|
|
|
|
|
texture->pixFmt = pixFmt;
|
|
|
|
texture->width = width;
|
|
|
|
texture->height = height;
|
2018-09-24 09:48:11 +00:00
|
|
|
texture->streaming = streaming;
|
2018-09-23 05:48:44 +00:00
|
|
|
|
2018-09-23 10:45:20 +00:00
|
|
|
switch(pixFmt)
|
|
|
|
{
|
|
|
|
case EGL_PF_BGRA:
|
2019-01-01 23:30:19 +00:00
|
|
|
textureCount = 1;
|
|
|
|
texture->format = GL_BGRA;
|
|
|
|
texture->planes[0][0] = width;
|
|
|
|
texture->planes[0][1] = height;
|
|
|
|
texture->planes[0][2] = stride / 4;
|
|
|
|
texture->offsets[0] = 0;
|
|
|
|
texture->intFormat = GL_BGRA;
|
|
|
|
texture->dataType = GL_UNSIGNED_BYTE;
|
|
|
|
texture->pboBufferSize = height * stride;
|
2018-12-04 10:23:28 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case EGL_PF_RGBA:
|
2019-01-01 23:30:19 +00:00
|
|
|
textureCount = 1;
|
|
|
|
texture->format = GL_RGBA;
|
|
|
|
texture->planes[0][0] = width;
|
|
|
|
texture->planes[0][1] = height;
|
|
|
|
texture->planes[0][2] = stride / 4;
|
|
|
|
texture->offsets[0] = 0;
|
|
|
|
texture->intFormat = GL_BGRA;
|
|
|
|
texture->dataType = GL_UNSIGNED_BYTE;
|
|
|
|
texture->pboBufferSize = height * stride;
|
2018-12-04 10:23:28 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case EGL_PF_RGBA10:
|
2019-01-01 23:30:19 +00:00
|
|
|
textureCount = 1;
|
|
|
|
texture->format = GL_RGBA;
|
|
|
|
texture->planes[0][0] = width;
|
|
|
|
texture->planes[0][1] = height;
|
|
|
|
texture->planes[0][2] = stride / 4;
|
|
|
|
texture->offsets[0] = 0;
|
|
|
|
texture->intFormat = GL_RGB10_A2;
|
|
|
|
texture->dataType = GL_UNSIGNED_INT_2_10_10_10_REV;
|
|
|
|
texture->pboBufferSize = height * stride;
|
2018-09-23 10:45:20 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case EGL_PF_YUV420:
|
2019-01-01 23:30:19 +00:00
|
|
|
textureCount = 3;
|
|
|
|
texture->format = GL_RED;
|
|
|
|
texture->planes[0][0] = width;
|
|
|
|
texture->planes[0][1] = height;
|
|
|
|
texture->planes[0][2] = stride;
|
|
|
|
texture->planes[1][0] = width / 2;
|
|
|
|
texture->planes[1][1] = height / 2;
|
|
|
|
texture->planes[1][2] = stride / 2;
|
|
|
|
texture->planes[2][0] = width / 2;
|
|
|
|
texture->planes[2][1] = height / 2;
|
|
|
|
texture->planes[2][2] = stride / 2;
|
|
|
|
texture->offsets[0] = 0;
|
|
|
|
texture->offsets[1] = stride * height;
|
|
|
|
texture->offsets[2] = texture->offsets[1] + (texture->offsets[1] / 4);
|
|
|
|
texture->dataType = GL_UNSIGNED_BYTE;
|
|
|
|
texture->pboBufferSize = texture->offsets[2] + (texture->offsets[1] / 4);
|
2018-09-23 10:45:20 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
DEBUG_ERROR("Unsupported pixel format");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-09-24 09:48:11 +00:00
|
|
|
if (textureCount > texture->textureCount)
|
|
|
|
{
|
|
|
|
if (texture->textureCount > 0)
|
|
|
|
{
|
|
|
|
glDeleteTextures(texture->textureCount, texture->textures);
|
|
|
|
glDeleteSamplers(texture->textureCount, texture->samplers);
|
|
|
|
}
|
|
|
|
|
|
|
|
texture->textureCount = textureCount;
|
|
|
|
glGenTextures(texture->textureCount, texture->textures);
|
|
|
|
glGenSamplers(texture->textureCount, texture->samplers);
|
|
|
|
}
|
|
|
|
|
|
|
|
for(int i = 0; i < textureCount; ++i)
|
2018-09-23 10:45:20 +00:00
|
|
|
{
|
|
|
|
glSamplerParameteri(texture->samplers[i], GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
|
|
glSamplerParameteri(texture->samplers[i], GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
glSamplerParameteri(texture->samplers[i], GL_TEXTURE_WRAP_S , GL_CLAMP_TO_EDGE);
|
|
|
|
glSamplerParameteri(texture->samplers[i], GL_TEXTURE_WRAP_T , GL_CLAMP_TO_EDGE);
|
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, texture->textures[i]);
|
2018-12-04 10:23:28 +00:00
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, texture->intFormat, texture->planes[i][0], texture->planes[i][1],
|
|
|
|
0, texture->format, texture->dataType, NULL);
|
2018-09-23 05:56:47 +00:00
|
|
|
}
|
2018-09-24 09:48:11 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
2018-09-23 05:48:44 +00:00
|
|
|
|
2018-09-24 09:48:11 +00:00
|
|
|
if (streaming)
|
2018-09-23 05:48:44 +00:00
|
|
|
{
|
2019-05-22 02:19:03 +00:00
|
|
|
if (texture->hasPBO)
|
2018-09-24 09:48:11 +00:00
|
|
|
{
|
2019-05-22 02:19:03 +00:00
|
|
|
// release old PBOs and delete the buffers
|
|
|
|
for(int i = 0; i < 2; ++i)
|
|
|
|
{
|
|
|
|
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, texture->pbo[i]);
|
|
|
|
glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
|
|
|
|
}
|
|
|
|
glDeleteBuffers(2, texture->pbo);
|
2018-09-24 09:48:11 +00:00
|
|
|
}
|
|
|
|
|
2019-05-22 02:19:03 +00:00
|
|
|
glGenBuffers(2, texture->pbo);
|
|
|
|
texture->hasPBO = true;
|
2018-09-24 09:48:11 +00:00
|
|
|
for(int i = 0; i < 2; ++i)
|
|
|
|
{
|
|
|
|
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, texture->pbo[i]);
|
2019-05-22 01:37:27 +00:00
|
|
|
glBufferStorage(
|
2018-09-24 09:48:11 +00:00
|
|
|
GL_PIXEL_UNPACK_BUFFER,
|
2019-05-22 01:37:27 +00:00
|
|
|
texture->pboBufferSize,
|
2019-05-22 02:19:03 +00:00
|
|
|
NULL,
|
2019-05-22 01:37:27 +00:00
|
|
|
GL_MAP_PERSISTENT_BIT |
|
|
|
|
GL_MAP_WRITE_BIT |
|
|
|
|
GL_MAP_COHERENT_BIT
|
|
|
|
);
|
|
|
|
|
|
|
|
texture->pboMap[i] = glMapBufferRange(
|
|
|
|
GL_PIXEL_UNPACK_BUFFER,
|
|
|
|
0,
|
|
|
|
texture->pboBufferSize,
|
|
|
|
GL_MAP_PERSISTENT_BIT |
|
|
|
|
GL_MAP_WRITE_BIT |
|
|
|
|
GL_MAP_UNSYNCHRONIZED_BIT |
|
|
|
|
GL_MAP_INVALIDATE_BUFFER_BIT
|
2018-09-24 09:48:11 +00:00
|
|
|
);
|
|
|
|
}
|
2019-05-22 01:37:27 +00:00
|
|
|
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
|
2018-09-23 05:48:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-09-24 09:48:11 +00:00
|
|
|
bool egl_texture_update(EGL_Texture * texture, const uint8_t * buffer)
|
2018-09-23 05:48:44 +00:00
|
|
|
{
|
2018-09-24 09:48:11 +00:00
|
|
|
if (texture->streaming)
|
2018-09-23 10:45:20 +00:00
|
|
|
{
|
2018-10-03 14:09:10 +00:00
|
|
|
if (texture->needsUpdate)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Previous frame was not consumed");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-09-24 09:48:11 +00:00
|
|
|
if (++texture->pboIndex == 2)
|
|
|
|
texture->pboIndex = 0;
|
|
|
|
|
2019-05-22 01:37:27 +00:00
|
|
|
/* update the GPU buffer */
|
|
|
|
memcpy(texture->pboMap[texture->pboIndex], buffer, texture->pboBufferSize);
|
|
|
|
|
2018-10-03 14:09:10 +00:00
|
|
|
texture->needsUpdate = true;
|
2018-09-24 09:48:11 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for(int i = 0; i < texture->textureCount; ++i)
|
|
|
|
{
|
|
|
|
glBindTexture(GL_TEXTURE_2D, texture->textures[i]);
|
2019-01-01 23:30:19 +00:00
|
|
|
glPixelStorei(GL_UNPACK_ROW_LENGTH, texture->planes[i][0]);
|
2018-09-24 09:48:11 +00:00
|
|
|
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, texture->planes[i][0], texture->planes[i][1],
|
2018-12-04 10:23:28 +00:00
|
|
|
texture->format, texture->dataType, buffer + texture->offsets[i]);
|
2018-09-24 09:48:11 +00:00
|
|
|
}
|
2018-10-03 14:09:10 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
2018-09-23 10:45:20 +00:00
|
|
|
}
|
2018-09-23 05:48:44 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void egl_texture_bind(EGL_Texture * texture)
|
|
|
|
{
|
2018-10-03 14:09:10 +00:00
|
|
|
if (texture->streaming && texture->needsUpdate)
|
|
|
|
{
|
|
|
|
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, texture->pbo[texture->pboIndex]);
|
|
|
|
for(int i = 0; i < texture->textureCount; ++i)
|
|
|
|
{
|
|
|
|
glBindTexture(GL_TEXTURE_2D, texture->textures[i]);
|
2019-01-01 23:30:19 +00:00
|
|
|
glPixelStorei(GL_UNPACK_ROW_LENGTH, texture->planes[i][2]);
|
2018-10-03 14:09:10 +00:00
|
|
|
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, texture->planes[i][0], texture->planes[i][1],
|
2018-12-04 10:23:28 +00:00
|
|
|
texture->format, texture->dataType, (const void *)texture->offsets[i]);
|
2018-10-03 14:09:10 +00:00
|
|
|
}
|
|
|
|
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
|
|
|
|
|
|
texture->needsUpdate = false;
|
|
|
|
}
|
|
|
|
|
2018-09-23 10:45:20 +00:00
|
|
|
for(int i = 0; i < texture->textureCount; ++i)
|
|
|
|
{
|
|
|
|
glActiveTexture(GL_TEXTURE0 + i);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, texture->textures[i]);
|
|
|
|
glBindSampler(i, texture->samplers[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int egl_texture_count(EGL_Texture * texture)
|
|
|
|
{
|
|
|
|
return texture->textureCount;
|
2018-09-23 05:48:44 +00:00
|
|
|
}
|