2021-08-02 13:37:33 +00:00
|
|
|
/**
|
|
|
|
* Looking Glass
|
2023-10-20 04:36:34 +00:00
|
|
|
* Copyright © 2017-2023 The Looking Glass Authors
|
2021-08-02 13:37:33 +00:00
|
|
|
* https://looking-glass.io
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the Free
|
|
|
|
* Software Foundation; either version 2 of the License, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc., 59
|
|
|
|
* Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "texture_buffer.h"
|
|
|
|
|
|
|
|
#include "egldebug.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
// forwards
|
|
|
|
extern const EGL_TextureOps EGL_TextureBuffer;
|
|
|
|
extern const EGL_TextureOps EGL_TextureBufferStream;
|
|
|
|
|
|
|
|
// internal functions
|
|
|
|
|
2021-08-08 07:16:10 +00:00
|
|
|
static void egl_texBuffer_cleanup(TextureBuffer * this)
|
2021-08-02 13:37:33 +00:00
|
|
|
{
|
2021-08-08 07:16:10 +00:00
|
|
|
egl_texUtilFreeBuffers(this->buf, this->texCount);
|
2021-08-02 13:37:33 +00:00
|
|
|
|
|
|
|
if (this->tex[0])
|
|
|
|
glDeleteTextures(this->texCount, this->tex);
|
|
|
|
|
2021-08-07 23:44:38 +00:00
|
|
|
if (this->sync)
|
|
|
|
{
|
|
|
|
glDeleteSync(this->sync);
|
|
|
|
this->sync = 0;
|
|
|
|
}
|
2021-08-02 13:37:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// common functions
|
|
|
|
|
2022-05-22 08:19:58 +00:00
|
|
|
bool egl_texBufferInit(EGL_Texture ** texture, EGL_TexType type,
|
|
|
|
EGLDisplay * display)
|
2021-08-02 13:37:33 +00:00
|
|
|
{
|
2021-08-02 17:58:30 +00:00
|
|
|
TextureBuffer * this;
|
|
|
|
if (!*texture)
|
2021-08-02 13:37:33 +00:00
|
|
|
{
|
2021-08-15 22:55:19 +00:00
|
|
|
this = calloc(1, sizeof(*this));
|
2021-08-02 17:58:30 +00:00
|
|
|
if (!this)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to malloc TexB");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
*texture = &this->base;
|
|
|
|
this->free = true;
|
2021-08-02 13:37:33 +00:00
|
|
|
}
|
2021-08-02 17:58:30 +00:00
|
|
|
else
|
|
|
|
this = UPCAST(TextureBuffer, *texture);
|
2021-08-02 13:37:33 +00:00
|
|
|
|
|
|
|
this->texCount = 1;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-08-08 07:16:10 +00:00
|
|
|
void egl_texBufferFree(EGL_Texture * texture)
|
2021-08-02 13:37:33 +00:00
|
|
|
{
|
2021-08-02 17:58:30 +00:00
|
|
|
TextureBuffer * this = UPCAST(TextureBuffer, texture);
|
2021-08-02 13:37:33 +00:00
|
|
|
|
2021-08-08 07:16:10 +00:00
|
|
|
egl_texBuffer_cleanup(this);
|
2021-08-02 13:37:33 +00:00
|
|
|
LG_LOCK_FREE(this->copyLock);
|
2021-08-02 17:58:30 +00:00
|
|
|
|
|
|
|
if (this->free)
|
|
|
|
free(this);
|
2021-08-02 13:37:33 +00:00
|
|
|
}
|
|
|
|
|
2021-08-08 07:16:10 +00:00
|
|
|
bool egl_texBufferSetup(EGL_Texture * texture, const EGL_TexSetup * setup)
|
2021-08-02 13:37:33 +00:00
|
|
|
{
|
2021-08-02 17:58:30 +00:00
|
|
|
TextureBuffer * this = UPCAST(TextureBuffer, texture);
|
2021-08-02 13:37:33 +00:00
|
|
|
|
2021-08-08 07:16:10 +00:00
|
|
|
egl_texBuffer_cleanup(this);
|
2021-08-02 13:37:33 +00:00
|
|
|
|
|
|
|
glGenTextures(this->texCount, this->tex);
|
|
|
|
for(int i = 0; i < this->texCount; ++i)
|
|
|
|
{
|
|
|
|
glBindTexture(GL_TEXTURE_2D, this->tex[i]);
|
|
|
|
glTexImage2D(GL_TEXTURE_2D,
|
|
|
|
0,
|
2021-08-09 04:08:10 +00:00
|
|
|
texture->format.intFormat,
|
|
|
|
texture->format.width,
|
|
|
|
texture->format.height,
|
2021-08-02 13:37:33 +00:00
|
|
|
0,
|
2021-08-09 04:08:10 +00:00
|
|
|
texture->format.format,
|
|
|
|
texture->format.dataType,
|
2021-08-02 13:37:33 +00:00
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
|
|
this->rIndex = -1;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-08-09 04:08:10 +00:00
|
|
|
static bool egl_texBufferUpdate(EGL_Texture * texture, const EGL_TexUpdate * update)
|
2021-08-02 13:37:33 +00:00
|
|
|
{
|
2021-08-02 17:58:30 +00:00
|
|
|
TextureBuffer * this = UPCAST(TextureBuffer, texture);
|
2021-08-13 23:51:36 +00:00
|
|
|
DEBUG_ASSERT(update->type == EGL_TEXTYPE_BUFFER);
|
2021-08-02 13:37:33 +00:00
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, this->tex[0]);
|
2022-05-21 11:21:16 +00:00
|
|
|
glPixelStorei(GL_UNPACK_ROW_LENGTH, update->pitch);
|
2021-08-02 13:37:33 +00:00
|
|
|
glTexSubImage2D(GL_TEXTURE_2D,
|
2022-05-21 11:21:16 +00:00
|
|
|
0,
|
|
|
|
update->x,
|
|
|
|
update->y,
|
|
|
|
update->width,
|
|
|
|
update->height,
|
2021-08-09 04:08:10 +00:00
|
|
|
texture->format.format,
|
|
|
|
texture->format.dataType,
|
2021-08-02 13:37:33 +00:00
|
|
|
update->buffer);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-08-08 07:16:10 +00:00
|
|
|
EGL_TexStatus egl_texBufferProcess(EGL_Texture * texture)
|
2021-08-02 13:37:33 +00:00
|
|
|
{
|
|
|
|
return EGL_TEX_STATUS_OK;
|
|
|
|
}
|
|
|
|
|
2021-08-09 04:08:10 +00:00
|
|
|
EGL_TexStatus egl_texBufferGet(EGL_Texture * texture, GLuint * tex)
|
2021-08-02 13:37:33 +00:00
|
|
|
{
|
2021-08-02 17:58:30 +00:00
|
|
|
TextureBuffer * this = UPCAST(TextureBuffer, texture);
|
2021-08-09 04:08:10 +00:00
|
|
|
*tex = this->tex[0];
|
|
|
|
return EGL_TEX_STATUS_OK;
|
2021-08-02 13:37:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// streaming functions
|
|
|
|
|
2022-05-22 08:19:58 +00:00
|
|
|
bool egl_texBufferStreamInit(EGL_Texture ** texture, EGL_TexType type,
|
|
|
|
EGLDisplay * display)
|
2021-08-02 13:37:33 +00:00
|
|
|
{
|
2022-05-22 08:19:58 +00:00
|
|
|
if (!egl_texBufferInit(texture, type, display))
|
2021-08-02 13:37:33 +00:00
|
|
|
return false;
|
|
|
|
|
2021-08-02 17:58:30 +00:00
|
|
|
TextureBuffer * this = UPCAST(TextureBuffer, *texture);
|
2021-08-02 13:37:33 +00:00
|
|
|
|
2022-05-22 08:19:58 +00:00
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case EGL_TEXTYPE_BUFFER_STREAM:
|
2022-05-23 10:55:02 +00:00
|
|
|
case EGL_TEXTYPE_FRAMEBUFFER:
|
2022-05-22 08:19:58 +00:00
|
|
|
case EGL_TEXTYPE_DMABUF:
|
|
|
|
this->texCount = 2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EGL_TEXTYPE_BUFFER_MAP:
|
|
|
|
this->texCount = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
DEBUG_UNREACHABLE();
|
|
|
|
}
|
|
|
|
|
2021-08-02 13:37:33 +00:00
|
|
|
LG_LOCK_INIT(this->copyLock);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-08-08 07:16:10 +00:00
|
|
|
bool egl_texBufferStreamSetup(EGL_Texture * texture, const EGL_TexSetup * setup)
|
2021-08-02 13:37:33 +00:00
|
|
|
{
|
2021-08-08 07:16:10 +00:00
|
|
|
if (!egl_texBufferSetup(texture, setup))
|
2021-08-02 13:37:33 +00:00
|
|
|
return false;
|
|
|
|
|
2021-08-02 17:58:30 +00:00
|
|
|
TextureBuffer * this = UPCAST(TextureBuffer, texture);
|
2021-08-09 04:08:10 +00:00
|
|
|
return egl_texUtilGenBuffers(&texture->format, this->buf, this->texCount);
|
2021-08-02 13:37:33 +00:00
|
|
|
}
|
|
|
|
|
2021-08-08 07:16:10 +00:00
|
|
|
static bool egl_texBufferStreamUpdate(EGL_Texture * texture,
|
2021-08-02 13:37:33 +00:00
|
|
|
const EGL_TexUpdate * update)
|
|
|
|
{
|
2021-08-02 17:58:30 +00:00
|
|
|
TextureBuffer * this = UPCAST(TextureBuffer, texture);
|
2021-08-13 23:51:36 +00:00
|
|
|
DEBUG_ASSERT(update->type == EGL_TEXTYPE_BUFFER);
|
2021-08-02 13:37:33 +00:00
|
|
|
|
|
|
|
LG_LOCK(this->copyLock);
|
2022-05-22 08:19:58 +00:00
|
|
|
|
|
|
|
uint8_t * dst = this->buf[this->bufIndex].map +
|
|
|
|
texture->format.stride * update->y +
|
|
|
|
update->x * texture->format.bpp;
|
|
|
|
|
|
|
|
if (update->topDown)
|
|
|
|
{
|
|
|
|
const uint8_t * src = update->buffer;
|
|
|
|
for(int y = 0; y < update->height; ++y)
|
|
|
|
{
|
|
|
|
memcpy(dst, src, update->stride);
|
|
|
|
dst += texture->format.stride;
|
|
|
|
src += update->stride;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const uint8_t * src = update->buffer + update->stride * update->height;
|
|
|
|
for(int y = 0; y < update->height; ++y)
|
|
|
|
{
|
|
|
|
src -= update->stride;
|
|
|
|
memcpy(dst, src, update->stride);
|
|
|
|
dst += texture->format.stride;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-02 13:37:33 +00:00
|
|
|
this->buf[this->bufIndex].updated = true;
|
|
|
|
LG_UNLOCK(this->copyLock);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-08-08 07:16:10 +00:00
|
|
|
EGL_TexStatus egl_texBufferStreamProcess(EGL_Texture * texture)
|
2021-08-02 13:37:33 +00:00
|
|
|
{
|
2021-08-02 17:58:30 +00:00
|
|
|
TextureBuffer * this = UPCAST(TextureBuffer, texture);
|
2021-08-02 13:37:33 +00:00
|
|
|
|
|
|
|
LG_LOCK(this->copyLock);
|
|
|
|
|
|
|
|
GLuint tex = this->tex[this->bufIndex];
|
|
|
|
EGL_TexBuffer * buffer = &this->buf[this->bufIndex];
|
|
|
|
|
2021-08-07 23:44:38 +00:00
|
|
|
if (buffer->updated && this->sync == 0)
|
2021-08-02 13:37:33 +00:00
|
|
|
{
|
|
|
|
this->rIndex = this->bufIndex;
|
|
|
|
if (++this->bufIndex == this->texCount)
|
|
|
|
this->bufIndex = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
LG_UNLOCK(this->copyLock);
|
|
|
|
|
|
|
|
if (buffer->updated)
|
|
|
|
{
|
|
|
|
buffer->updated = false;
|
|
|
|
|
|
|
|
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, buffer->pbo);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, tex);
|
2021-08-09 04:08:10 +00:00
|
|
|
glPixelStorei(GL_UNPACK_ROW_LENGTH, texture->format.pitch);
|
2021-08-02 13:37:33 +00:00
|
|
|
glTexSubImage2D(GL_TEXTURE_2D,
|
|
|
|
0, 0, 0,
|
2021-08-09 04:08:10 +00:00
|
|
|
texture->format.width,
|
|
|
|
texture->format.height,
|
|
|
|
texture->format.format,
|
|
|
|
texture->format.dataType,
|
2021-08-02 13:37:33 +00:00
|
|
|
(const void *)0);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
|
|
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
|
|
|
|
|
2021-08-07 23:44:38 +00:00
|
|
|
this->sync = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
|
2021-08-02 13:37:33 +00:00
|
|
|
glFlush();
|
|
|
|
}
|
|
|
|
|
|
|
|
return EGL_TEX_STATUS_OK;
|
|
|
|
}
|
|
|
|
|
2021-08-09 04:08:10 +00:00
|
|
|
EGL_TexStatus egl_texBufferStreamGet(EGL_Texture * texture, GLuint * tex)
|
2021-08-02 13:37:33 +00:00
|
|
|
{
|
2021-08-02 17:58:30 +00:00
|
|
|
TextureBuffer * this = UPCAST(TextureBuffer, texture);
|
2021-08-02 13:37:33 +00:00
|
|
|
|
|
|
|
if (this->rIndex == -1)
|
|
|
|
return EGL_TEX_STATUS_NOTREADY;
|
|
|
|
|
2021-08-07 23:44:38 +00:00
|
|
|
if (this->sync)
|
2021-08-02 13:37:33 +00:00
|
|
|
{
|
2022-01-13 21:09:40 +00:00
|
|
|
switch(glClientWaitSync(this->sync, 0, 40000000)) // 40ms
|
2021-08-02 13:37:33 +00:00
|
|
|
{
|
|
|
|
case GL_ALREADY_SIGNALED:
|
|
|
|
case GL_CONDITION_SATISFIED:
|
2021-08-07 23:44:38 +00:00
|
|
|
glDeleteSync(this->sync);
|
|
|
|
this->sync = 0;
|
2021-08-02 13:37:33 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GL_TIMEOUT_EXPIRED:
|
|
|
|
return EGL_TEX_STATUS_NOTREADY;
|
|
|
|
|
|
|
|
case GL_WAIT_FAILED:
|
|
|
|
case GL_INVALID_VALUE:
|
2021-08-07 23:44:38 +00:00
|
|
|
glDeleteSync(this->sync);
|
|
|
|
this->sync = 0;
|
2021-08-02 13:37:33 +00:00
|
|
|
DEBUG_GL_ERROR("glClientWaitSync failed");
|
|
|
|
return EGL_TEX_STATUS_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-09 04:08:10 +00:00
|
|
|
*tex = this->tex[this->rIndex];
|
2021-08-02 13:37:33 +00:00
|
|
|
return EGL_TEX_STATUS_OK;
|
|
|
|
}
|
|
|
|
|
2023-03-08 22:20:01 +00:00
|
|
|
EGL_TexStatus egl_texBufferBind(EGL_Texture * texture)
|
|
|
|
{
|
|
|
|
GLuint tex;
|
|
|
|
EGL_TexStatus status;
|
|
|
|
|
|
|
|
if ((status = texture->ops.get(texture, &tex)) != EGL_TEX_STATUS_OK)
|
|
|
|
return status;
|
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, tex);
|
|
|
|
return EGL_TEX_STATUS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-08-02 13:37:33 +00:00
|
|
|
const EGL_TextureOps EGL_TextureBuffer =
|
|
|
|
{
|
2021-08-08 07:16:10 +00:00
|
|
|
.init = egl_texBufferInit,
|
|
|
|
.free = egl_texBufferFree,
|
|
|
|
.setup = egl_texBufferSetup,
|
2021-08-09 04:08:10 +00:00
|
|
|
.update = egl_texBufferUpdate,
|
2021-08-08 07:16:10 +00:00
|
|
|
.process = egl_texBufferProcess,
|
2023-03-08 22:20:01 +00:00
|
|
|
.get = egl_texBufferGet,
|
|
|
|
.bind = egl_texBufferBind
|
2021-08-02 13:37:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const EGL_TextureOps EGL_TextureBufferStream =
|
|
|
|
{
|
2021-08-08 07:16:10 +00:00
|
|
|
.init = egl_texBufferStreamInit,
|
|
|
|
.free = egl_texBufferFree,
|
|
|
|
.setup = egl_texBufferStreamSetup,
|
|
|
|
.update = egl_texBufferStreamUpdate,
|
|
|
|
.process = egl_texBufferStreamProcess,
|
2023-03-08 22:20:01 +00:00
|
|
|
.get = egl_texBufferStreamGet,
|
|
|
|
.bind = egl_texBufferBind
|
2021-08-02 13:37:33 +00:00
|
|
|
};
|