2021-06-06 01:26:18 +00:00
|
|
|
/**
|
|
|
|
* Looking Glass
|
|
|
|
* Copyright (C) 2017-2021 The Looking Glass Authors
|
|
|
|
* https://looking-glass.io
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the Free
|
|
|
|
* Software Foundation; either version 2 of the License, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc., 59
|
|
|
|
* Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
2018-09-23 05:48:44 +00:00
|
|
|
|
2018-12-12 05:39:04 +00:00
|
|
|
#include "texture.h"
|
2019-04-11 01:12:59 +00:00
|
|
|
#include "common/debug.h"
|
2019-10-01 13:17:20 +00:00
|
|
|
#include "common/framebuffer.h"
|
2021-01-26 10:46:30 +00:00
|
|
|
#include "egl_dynprocs.h"
|
2021-01-24 02:17:11 +00:00
|
|
|
#include "egldebug.h"
|
2018-09-23 05:48:44 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
2020-05-17 23:06:11 +00:00
|
|
|
#include <stdatomic.h>
|
2021-01-16 11:11:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* the following comes from drm_fourcc.h and is included here to avoid the
|
|
|
|
* external dependency for the few simple defines we need
|
|
|
|
*/
|
|
|
|
#define fourcc_code(a, b, c, d) ((uint32_t)(a) | ((uint32_t)(b) << 8) | \
|
|
|
|
((uint32_t)(c) << 16) | ((uint32_t)(d) << 24))
|
|
|
|
#define DRM_FORMAT_ARGB8888 fourcc_code('A', 'R', '2', '4')
|
|
|
|
#define DRM_FORMAT_ABGR8888 fourcc_code('A', 'B', '2', '4')
|
|
|
|
#define DRM_FORMAT_BGRA1010102 fourcc_code('B', 'A', '3', '0')
|
|
|
|
#define DRM_FORMAT_ABGR16161616F fourcc_code('A', 'B', '4', 'H')
|
2018-09-23 05:48:44 +00:00
|
|
|
|
2020-07-24 07:39:16 +00:00
|
|
|
/* this must be a multiple of 2 */
|
2020-12-31 04:31:24 +00:00
|
|
|
#define BUFFER_COUNT 4
|
2020-05-19 07:34:24 +00:00
|
|
|
|
2020-12-31 03:18:38 +00:00
|
|
|
struct Buffer
|
2020-05-19 07:34:24 +00:00
|
|
|
{
|
2020-10-29 15:32:25 +00:00
|
|
|
bool hasPBO;
|
|
|
|
GLuint pbo;
|
|
|
|
void * map;
|
|
|
|
GLsync sync;
|
2020-05-19 07:34:24 +00:00
|
|
|
};
|
|
|
|
|
2020-12-31 03:18:38 +00:00
|
|
|
struct BufferState
|
2020-05-19 07:34:24 +00:00
|
|
|
{
|
2020-07-24 07:39:16 +00:00
|
|
|
_Atomic(uint8_t) w, u, s, d;
|
2020-05-19 07:34:24 +00:00
|
|
|
};
|
|
|
|
|
2018-09-23 05:48:44 +00:00
|
|
|
struct EGL_Texture
|
|
|
|
{
|
2020-10-29 15:32:25 +00:00
|
|
|
EGLDisplay * display;
|
|
|
|
|
2018-09-23 10:45:20 +00:00
|
|
|
enum EGL_PixelFormat pixFmt;
|
2020-04-14 03:27:07 +00:00
|
|
|
size_t bpp;
|
2018-09-24 09:48:11 +00:00
|
|
|
bool streaming;
|
2020-10-29 15:32:25 +00:00
|
|
|
bool dma;
|
2019-06-12 12:36:00 +00:00
|
|
|
bool ready;
|
2018-09-23 05:56:47 +00:00
|
|
|
|
2020-10-30 08:31:48 +00:00
|
|
|
GLuint sampler;
|
|
|
|
size_t width, height, stride, pitch;
|
2020-10-30 00:55:47 +00:00
|
|
|
GLenum intFormat;
|
|
|
|
GLenum format;
|
|
|
|
GLenum dataType;
|
|
|
|
unsigned int fourcc;
|
|
|
|
size_t pboBufferSize;
|
2018-09-23 10:45:20 +00:00
|
|
|
|
2020-12-31 03:18:38 +00:00
|
|
|
struct BufferState state;
|
|
|
|
int bufferCount;
|
2021-06-08 10:39:10 +00:00
|
|
|
GLuint tex[BUFFER_COUNT];
|
2020-12-31 03:18:38 +00:00
|
|
|
struct Buffer buf[BUFFER_COUNT];
|
2021-02-05 06:37:48 +00:00
|
|
|
|
|
|
|
size_t dmaImageCount;
|
|
|
|
size_t dmaImageUsed;
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
EGLImage image;
|
|
|
|
}
|
|
|
|
* dmaImages;
|
|
|
|
|
|
|
|
GLuint dmaFBO;
|
|
|
|
GLuint dmaTex;
|
2018-09-23 05:48:44 +00:00
|
|
|
};
|
|
|
|
|
2020-10-29 15:32:25 +00:00
|
|
|
bool egl_texture_init(EGL_Texture ** texture, EGLDisplay * display)
|
2018-09-23 05:48:44 +00:00
|
|
|
{
|
|
|
|
*texture = (EGL_Texture *)malloc(sizeof(EGL_Texture));
|
|
|
|
if (!*texture)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to malloc EGL_Texture");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(*texture, 0, sizeof(EGL_Texture));
|
2020-10-29 15:32:25 +00:00
|
|
|
(*texture)->display = display;
|
2018-09-23 05:48:44 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void egl_texture_free(EGL_Texture ** texture)
|
|
|
|
{
|
|
|
|
if (!*texture)
|
|
|
|
return;
|
|
|
|
|
2020-10-30 08:31:48 +00:00
|
|
|
glDeleteSamplers(1, &(*texture)->sampler);
|
2018-09-23 05:48:44 +00:00
|
|
|
|
2020-12-31 03:18:38 +00:00
|
|
|
for(int i = 0; i < (*texture)->bufferCount; ++i)
|
2019-05-22 01:37:27 +00:00
|
|
|
{
|
2020-12-31 03:18:38 +00:00
|
|
|
struct Buffer * b = &(*texture)->buf[i];
|
|
|
|
if (b->hasPBO)
|
2019-05-22 01:37:27 +00:00
|
|
|
{
|
2020-12-31 03:18:38 +00:00
|
|
|
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, b->pbo);
|
|
|
|
if ((*texture)->buf[i].map)
|
2020-05-22 07:47:19 +00:00
|
|
|
{
|
|
|
|
glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
|
2020-12-31 03:18:38 +00:00
|
|
|
(*texture)->buf[i].map = NULL;
|
2020-05-22 07:47:19 +00:00
|
|
|
}
|
2020-12-31 03:18:38 +00:00
|
|
|
glDeleteBuffers(1, &b->pbo);
|
|
|
|
if (b->sync)
|
|
|
|
glDeleteSync(b->sync);
|
2019-05-22 01:37:27 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-19 07:34:24 +00:00
|
|
|
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
|
2021-06-08 10:39:10 +00:00
|
|
|
glDeleteTextures((*texture)->bufferCount, (*texture)->tex);
|
2018-09-23 05:48:44 +00:00
|
|
|
|
2021-02-05 06:37:48 +00:00
|
|
|
for (size_t i = 0; i < (*texture)->dmaImageUsed; ++i)
|
|
|
|
eglDestroyImage((*texture)->display, (*texture)->dmaImages[i].image);
|
|
|
|
free((*texture)->dmaImages);
|
|
|
|
|
2018-09-23 05:48:44 +00:00
|
|
|
free(*texture);
|
|
|
|
*texture = NULL;
|
|
|
|
}
|
|
|
|
|
2020-05-29 05:48:59 +00:00
|
|
|
static bool egl_texture_map(EGL_Texture * texture, uint8_t i)
|
2020-05-21 01:44:56 +00:00
|
|
|
{
|
2020-12-31 03:18:38 +00:00
|
|
|
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, texture->buf[i].pbo);
|
|
|
|
texture->buf[i].map = glMapBufferRange(
|
2020-05-29 05:48:59 +00:00
|
|
|
GL_PIXEL_UNPACK_BUFFER,
|
|
|
|
0,
|
|
|
|
texture->pboBufferSize,
|
|
|
|
GL_MAP_WRITE_BIT |
|
|
|
|
GL_MAP_UNSYNCHRONIZED_BIT |
|
2020-12-31 03:18:38 +00:00
|
|
|
GL_MAP_INVALIDATE_BUFFER_BIT |
|
2021-06-08 10:39:10 +00:00
|
|
|
GL_MAP_PERSISTENT_BIT |
|
|
|
|
GL_MAP_COHERENT_BIT
|
2020-05-29 05:48:59 +00:00
|
|
|
);
|
|
|
|
|
2020-12-31 03:18:38 +00:00
|
|
|
if (!texture->buf[i].map)
|
2020-05-21 01:44:56 +00:00
|
|
|
{
|
2021-01-24 02:17:11 +00:00
|
|
|
DEBUG_EGL_ERROR("glMapBufferRange failed for %d of %lu bytes", i,
|
|
|
|
texture->pboBufferSize);
|
2020-05-29 05:48:59 +00:00
|
|
|
}
|
2020-05-21 01:44:56 +00:00
|
|
|
|
2021-05-02 14:48:36 +00:00
|
|
|
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
|
|
|
|
|
|
|
|
return texture->buf[i].map;
|
2020-05-29 05:48:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void egl_texture_unmap(EGL_Texture * texture, uint8_t i)
|
2020-05-21 01:44:56 +00:00
|
|
|
{
|
2020-12-31 03:18:38 +00:00
|
|
|
if (!texture->buf[i].map)
|
2020-05-29 05:48:59 +00:00
|
|
|
return;
|
2020-05-22 07:47:19 +00:00
|
|
|
|
2020-12-31 03:18:38 +00:00
|
|
|
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, texture->buf[i].pbo);
|
2020-05-29 05:48:59 +00:00
|
|
|
glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
|
2020-05-29 06:54:25 +00:00
|
|
|
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
|
2020-12-31 03:18:38 +00:00
|
|
|
texture->buf[i].map = NULL;
|
2020-05-29 05:48:59 +00:00
|
|
|
}
|
|
|
|
|
2020-10-29 15:32:25 +00:00
|
|
|
bool egl_texture_setup(EGL_Texture * texture, enum EGL_PixelFormat pixFmt, size_t width, size_t height, size_t stride, bool streaming, bool useDMA)
|
2018-09-23 05:48:44 +00:00
|
|
|
{
|
2020-12-31 01:58:22 +00:00
|
|
|
if (texture->streaming && !useDMA)
|
2020-05-29 06:54:25 +00:00
|
|
|
{
|
2020-12-31 03:18:38 +00:00
|
|
|
for(int i = 0; i < texture->bufferCount; ++i)
|
2020-05-29 06:54:25 +00:00
|
|
|
{
|
2020-12-31 01:58:22 +00:00
|
|
|
egl_texture_unmap(texture, i);
|
2020-12-31 03:18:38 +00:00
|
|
|
if (texture->buf[i].hasPBO)
|
2020-05-29 06:54:25 +00:00
|
|
|
{
|
2020-12-31 03:18:38 +00:00
|
|
|
glDeleteBuffers(1, &texture->buf[i].pbo);
|
|
|
|
texture->buf[i].hasPBO = false;
|
2020-05-29 06:54:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-31 03:18:38 +00:00
|
|
|
texture->pixFmt = pixFmt;
|
|
|
|
texture->width = width;
|
|
|
|
texture->height = height;
|
|
|
|
texture->stride = stride;
|
|
|
|
texture->streaming = streaming;
|
|
|
|
texture->bufferCount = streaming ? BUFFER_COUNT : 1;
|
2021-01-09 11:16:40 +00:00
|
|
|
texture->dma = useDMA;
|
2020-12-31 03:18:38 +00:00
|
|
|
texture->ready = false;
|
2020-05-29 06:54:25 +00:00
|
|
|
|
2020-07-24 07:39:16 +00:00
|
|
|
atomic_store_explicit(&texture->state.w, 0, memory_order_relaxed);
|
|
|
|
atomic_store_explicit(&texture->state.u, 0, memory_order_relaxed);
|
|
|
|
atomic_store_explicit(&texture->state.s, 0, memory_order_relaxed);
|
|
|
|
atomic_store_explicit(&texture->state.d, 0, memory_order_relaxed);
|
2018-09-23 05:48:44 +00:00
|
|
|
|
2018-09-23 10:45:20 +00:00
|
|
|
switch(pixFmt)
|
|
|
|
{
|
|
|
|
case EGL_PF_BGRA:
|
2020-04-14 03:27:07 +00:00
|
|
|
texture->bpp = 4;
|
2019-01-01 23:30:19 +00:00
|
|
|
texture->format = GL_BGRA;
|
|
|
|
texture->intFormat = GL_BGRA;
|
|
|
|
texture->dataType = GL_UNSIGNED_BYTE;
|
2020-10-30 00:55:47 +00:00
|
|
|
texture->fourcc = DRM_FORMAT_ARGB8888;
|
2019-01-01 23:30:19 +00:00
|
|
|
texture->pboBufferSize = height * stride;
|
2018-12-04 10:23:28 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case EGL_PF_RGBA:
|
2020-04-14 03:27:07 +00:00
|
|
|
texture->bpp = 4;
|
2019-01-01 23:30:19 +00:00
|
|
|
texture->format = GL_RGBA;
|
|
|
|
texture->intFormat = GL_BGRA;
|
|
|
|
texture->dataType = GL_UNSIGNED_BYTE;
|
2020-10-30 00:55:47 +00:00
|
|
|
texture->fourcc = DRM_FORMAT_ABGR8888;
|
2019-01-01 23:30:19 +00:00
|
|
|
texture->pboBufferSize = height * stride;
|
2018-12-04 10:23:28 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case EGL_PF_RGBA10:
|
2020-04-14 03:27:07 +00:00
|
|
|
texture->bpp = 4;
|
2019-01-01 23:30:19 +00:00
|
|
|
texture->format = GL_RGBA;
|
|
|
|
texture->intFormat = GL_RGB10_A2;
|
|
|
|
texture->dataType = GL_UNSIGNED_INT_2_10_10_10_REV;
|
2020-10-30 00:55:47 +00:00
|
|
|
texture->fourcc = DRM_FORMAT_BGRA1010102;
|
2019-01-01 23:30:19 +00:00
|
|
|
texture->pboBufferSize = height * stride;
|
2018-09-23 10:45:20 +00:00
|
|
|
break;
|
|
|
|
|
2020-10-11 08:22:31 +00:00
|
|
|
case EGL_PF_RGBA16F:
|
|
|
|
texture->bpp = 8;
|
|
|
|
texture->format = GL_RGBA;
|
2020-10-11 08:39:47 +00:00
|
|
|
texture->intFormat = GL_RGBA16F;
|
2020-10-11 09:34:34 +00:00
|
|
|
texture->dataType = GL_HALF_FLOAT;
|
2020-10-30 00:55:47 +00:00
|
|
|
texture->fourcc = DRM_FORMAT_ABGR16161616F;
|
2020-10-11 08:22:31 +00:00
|
|
|
texture->pboBufferSize = height * stride;
|
|
|
|
break;
|
|
|
|
|
2018-09-23 10:45:20 +00:00
|
|
|
default:
|
|
|
|
DEBUG_ERROR("Unsupported pixel format");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-10-30 08:31:48 +00:00
|
|
|
texture->pitch = stride / texture->bpp;
|
2020-05-19 07:34:24 +00:00
|
|
|
|
2021-06-08 10:45:37 +00:00
|
|
|
if (texture->tex[0])
|
2021-06-08 10:39:10 +00:00
|
|
|
glDeleteTextures(texture->bufferCount, texture->tex);
|
|
|
|
glGenTextures(texture->bufferCount, texture->tex);
|
2020-05-19 07:34:24 +00:00
|
|
|
|
2020-10-30 08:31:48 +00:00
|
|
|
if (!texture->sampler)
|
|
|
|
{
|
|
|
|
glGenSamplers(1, &texture->sampler);
|
|
|
|
glSamplerParameteri(texture->sampler, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
|
|
glSamplerParameteri(texture->sampler, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
glSamplerParameteri(texture->sampler, GL_TEXTURE_WRAP_S , GL_CLAMP_TO_EDGE);
|
|
|
|
glSamplerParameteri(texture->sampler, GL_TEXTURE_WRAP_T , GL_CLAMP_TO_EDGE);
|
2018-09-24 09:48:11 +00:00
|
|
|
}
|
|
|
|
|
2020-10-30 11:19:15 +00:00
|
|
|
if (useDMA)
|
2021-02-05 06:37:48 +00:00
|
|
|
{
|
|
|
|
if (texture->dmaFBO)
|
|
|
|
glDeleteFramebuffers(1, &texture->dmaFBO);
|
|
|
|
if (texture->dmaTex)
|
|
|
|
glDeleteTextures(1, &texture->dmaTex);
|
|
|
|
glGenFramebuffers(1, &texture->dmaFBO);
|
|
|
|
glGenTextures(1, &texture->dmaTex);
|
2021-02-21 06:43:37 +00:00
|
|
|
|
2021-07-16 01:53:29 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, texture->tex[0]);
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, texture->intFormat, texture->width,
|
|
|
|
texture->height, 0, texture->format, texture->dataType, NULL);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
|
|
|
2021-02-21 06:43:37 +00:00
|
|
|
for (size_t i = 0; i < texture->dmaImageUsed; ++i)
|
|
|
|
eglDestroyImage(texture->display, texture->dmaImages[i].image);
|
|
|
|
texture->dmaImageUsed = 0;
|
2020-10-30 11:19:15 +00:00
|
|
|
return true;
|
2021-02-05 06:37:48 +00:00
|
|
|
}
|
2020-10-30 11:19:15 +00:00
|
|
|
|
2021-06-08 10:39:10 +00:00
|
|
|
for(int i = 0; i < texture->bufferCount; ++i)
|
|
|
|
{
|
|
|
|
glBindTexture(GL_TEXTURE_2D, texture->tex[i]);
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, texture->intFormat, texture->width,
|
|
|
|
texture->height, 0, texture->format, texture->dataType, NULL);
|
|
|
|
}
|
2018-09-24 09:48:11 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
2018-09-23 05:48:44 +00:00
|
|
|
|
2020-10-30 11:19:15 +00:00
|
|
|
if (!streaming)
|
2020-05-29 06:54:25 +00:00
|
|
|
return true;
|
2020-05-21 01:44:56 +00:00
|
|
|
|
2020-12-31 03:18:38 +00:00
|
|
|
for(int i = 0; i < texture->bufferCount; ++i)
|
2018-09-23 05:48:44 +00:00
|
|
|
{
|
2020-12-31 03:18:38 +00:00
|
|
|
glGenBuffers(1, &texture->buf[i].pbo);
|
|
|
|
texture->buf[i].hasPBO = true;
|
2020-05-21 01:44:56 +00:00
|
|
|
|
2020-12-31 03:18:38 +00:00
|
|
|
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, texture->buf[i].pbo);
|
2020-05-21 01:44:56 +00:00
|
|
|
glBufferStorage(
|
|
|
|
GL_PIXEL_UNPACK_BUFFER,
|
|
|
|
texture->pboBufferSize,
|
|
|
|
NULL,
|
2021-06-08 10:39:10 +00:00
|
|
|
GL_MAP_WRITE_BIT |
|
|
|
|
GL_MAP_PERSISTENT_BIT |
|
|
|
|
GL_MAP_COHERENT_BIT
|
2020-05-21 01:44:56 +00:00
|
|
|
);
|
2020-12-31 03:18:38 +00:00
|
|
|
|
|
|
|
if (!egl_texture_map(texture, i))
|
|
|
|
return false;
|
2020-05-29 06:54:25 +00:00
|
|
|
}
|
2020-05-21 01:44:56 +00:00
|
|
|
|
2018-09-23 05:48:44 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-01-14 06:05:26 +00:00
|
|
|
static void egl_warn_slow(void)
|
2020-05-19 12:42:55 +00:00
|
|
|
{
|
|
|
|
static bool warnDone = false;
|
|
|
|
if (!warnDone)
|
|
|
|
{
|
|
|
|
warnDone = true;
|
|
|
|
DEBUG_BREAK();
|
2020-05-21 04:09:51 +00:00
|
|
|
DEBUG_WARN("The guest is providing updates faster then your computer can display them");
|
|
|
|
DEBUG_WARN("This is a hardware limitation, expect microstutters & frame skips");
|
2020-05-19 12:42:55 +00:00
|
|
|
DEBUG_BREAK();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2020-07-24 07:39:16 +00:00
|
|
|
const uint8_t sw =
|
|
|
|
atomic_load_explicit(&texture->state.w, memory_order_acquire);
|
2018-09-24 09:48:11 +00:00
|
|
|
|
2020-08-15 12:37:54 +00:00
|
|
|
if (atomic_load_explicit(&texture->state.u, memory_order_acquire) == (uint8_t)(sw + 1))
|
2020-05-19 12:42:55 +00:00
|
|
|
{
|
|
|
|
egl_warn_slow();
|
2020-05-19 07:34:24 +00:00
|
|
|
return true;
|
2020-05-19 12:42:55 +00:00
|
|
|
}
|
2019-05-22 01:37:27 +00:00
|
|
|
|
2020-12-31 03:18:38 +00:00
|
|
|
const uint8_t b = sw % BUFFER_COUNT;
|
|
|
|
memcpy(texture->buf[b].map, buffer, texture->pboBufferSize);
|
2020-07-24 07:39:16 +00:00
|
|
|
atomic_fetch_add_explicit(&texture->state.w, 1, memory_order_release);
|
2018-09-24 09:48:11 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-06-08 10:39:10 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, texture->tex[0]);
|
2020-10-30 08:31:48 +00:00
|
|
|
glPixelStorei(GL_UNPACK_ROW_LENGTH, texture->pitch);
|
|
|
|
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, texture->width, texture->height,
|
|
|
|
texture->format, texture->dataType, buffer);
|
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;
|
|
|
|
}
|
|
|
|
|
2020-01-13 08:30:49 +00:00
|
|
|
bool egl_texture_update_from_frame(EGL_Texture * texture, const FrameBuffer * frame)
|
2019-10-01 13:17:20 +00:00
|
|
|
{
|
|
|
|
if (!texture->streaming)
|
|
|
|
return false;
|
|
|
|
|
2020-07-24 07:39:16 +00:00
|
|
|
const uint8_t sw =
|
|
|
|
atomic_load_explicit(&texture->state.w, memory_order_acquire);
|
2020-05-19 07:34:24 +00:00
|
|
|
|
2020-08-15 12:37:54 +00:00
|
|
|
if (atomic_load_explicit(&texture->state.u, memory_order_acquire) == (uint8_t)(sw + 1))
|
2020-05-19 12:42:55 +00:00
|
|
|
{
|
|
|
|
egl_warn_slow();
|
2019-10-01 13:17:20 +00:00
|
|
|
return true;
|
2020-05-19 12:42:55 +00:00
|
|
|
}
|
2019-10-01 13:17:20 +00:00
|
|
|
|
2020-12-31 03:18:38 +00:00
|
|
|
const uint8_t b = sw % BUFFER_COUNT;
|
2020-05-30 06:50:27 +00:00
|
|
|
|
2020-04-14 03:27:07 +00:00
|
|
|
framebuffer_read(
|
|
|
|
frame,
|
2020-12-31 03:18:38 +00:00
|
|
|
texture->buf[b].map,
|
2020-04-14 03:27:07 +00:00
|
|
|
texture->stride,
|
|
|
|
texture->height,
|
|
|
|
texture->width,
|
|
|
|
texture->bpp,
|
|
|
|
texture->stride
|
|
|
|
);
|
|
|
|
|
2020-07-24 07:39:16 +00:00
|
|
|
atomic_fetch_add_explicit(&texture->state.w, 1, memory_order_release);
|
2020-05-30 06:50:27 +00:00
|
|
|
|
2019-10-01 13:17:20 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-07-18 08:42:48 +00:00
|
|
|
bool egl_texture_update_from_dma(EGL_Texture * texture, const FrameBuffer * frame, const int dmaFd)
|
2020-10-29 15:32:25 +00:00
|
|
|
{
|
|
|
|
if (!texture->streaming)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const uint8_t sw =
|
|
|
|
atomic_load_explicit(&texture->state.w, memory_order_acquire);
|
|
|
|
|
|
|
|
if (atomic_load_explicit(&texture->state.u, memory_order_acquire) == (uint8_t)(sw + 1))
|
|
|
|
{
|
|
|
|
egl_warn_slow();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-02-05 06:37:48 +00:00
|
|
|
EGLImage image = EGL_NO_IMAGE;
|
|
|
|
|
|
|
|
for (int i = 0; i < texture->dmaImageUsed; ++i)
|
2020-10-29 15:32:25 +00:00
|
|
|
{
|
2021-02-05 06:37:48 +00:00
|
|
|
if (texture->dmaImages[i].fd == dmaFd)
|
|
|
|
{
|
|
|
|
image = texture->dmaImages[i].image;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-10-29 15:32:25 +00:00
|
|
|
|
2020-12-31 01:58:40 +00:00
|
|
|
if (image == EGL_NO_IMAGE)
|
|
|
|
{
|
2021-02-05 06:37:48 +00:00
|
|
|
EGLAttrib const attribs[] =
|
|
|
|
{
|
|
|
|
EGL_WIDTH , texture->width,
|
|
|
|
EGL_HEIGHT , texture->height,
|
|
|
|
EGL_LINUX_DRM_FOURCC_EXT , texture->fourcc,
|
|
|
|
EGL_DMA_BUF_PLANE0_FD_EXT , dmaFd,
|
|
|
|
EGL_DMA_BUF_PLANE0_OFFSET_EXT, 0,
|
|
|
|
EGL_DMA_BUF_PLANE0_PITCH_EXT , texture->stride,
|
|
|
|
EGL_NONE , EGL_NONE
|
|
|
|
};
|
|
|
|
|
|
|
|
/* create the image backed by the dma buffer */
|
|
|
|
image = eglCreateImage(
|
|
|
|
texture->display,
|
|
|
|
EGL_NO_CONTEXT,
|
|
|
|
EGL_LINUX_DMA_BUF_EXT,
|
|
|
|
(EGLClientBuffer)NULL,
|
|
|
|
attribs
|
|
|
|
);
|
2020-12-31 01:58:40 +00:00
|
|
|
|
2021-02-05 06:37:48 +00:00
|
|
|
if (image == EGL_NO_IMAGE)
|
|
|
|
{
|
|
|
|
DEBUG_EGL_ERROR("Failed to create ELGImage for DMA transfer");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (texture->dmaImageUsed == texture->dmaImageCount)
|
|
|
|
{
|
|
|
|
size_t newCount = texture->dmaImageCount * 2 + 2;
|
|
|
|
void * new = realloc(texture->dmaImages, newCount * sizeof *texture->dmaImages);
|
|
|
|
if (!new)
|
|
|
|
{
|
|
|
|
DEBUG_EGL_ERROR("Failed to allocate memory");
|
|
|
|
eglDestroyImage(texture->display, image);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
texture->dmaImageCount = newCount;
|
|
|
|
texture->dmaImages = new;
|
|
|
|
}
|
|
|
|
|
|
|
|
const size_t index = texture->dmaImageUsed++;
|
|
|
|
texture->dmaImages[index].fd = dmaFd;
|
|
|
|
texture->dmaImages[index].image = image;
|
|
|
|
}
|
2020-10-29 15:32:25 +00:00
|
|
|
|
|
|
|
/* wait for completion */
|
|
|
|
framebuffer_wait(frame, texture->height * texture->stride);
|
|
|
|
|
2021-02-05 06:37:48 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, texture->dmaTex);
|
|
|
|
g_egl_dynProcs.glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, image);
|
|
|
|
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, texture->dmaFBO);
|
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture->dmaTex, 0);
|
|
|
|
|
2021-06-08 10:39:10 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, texture->tex[0]);
|
2021-07-18 08:42:48 +00:00
|
|
|
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, texture->width, texture->height);
|
2021-02-05 06:37:48 +00:00
|
|
|
|
|
|
|
GLsync fence = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
|
|
|
|
glFlush();
|
|
|
|
|
|
|
|
switch (glClientWaitSync(fence, 0, 10000000)) // 10ms
|
|
|
|
{
|
|
|
|
case GL_ALREADY_SIGNALED:
|
|
|
|
case GL_CONDITION_SATISFIED:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GL_TIMEOUT_EXPIRED:
|
|
|
|
egl_warn_slow();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GL_WAIT_FAILED:
|
|
|
|
case GL_INVALID_VALUE:
|
|
|
|
DEBUG_EGL_ERROR("glClientWaitSync failed");
|
|
|
|
}
|
2020-10-29 15:32:25 +00:00
|
|
|
|
2021-04-25 02:43:07 +00:00
|
|
|
glDeleteSync(fence);
|
2020-10-29 15:32:25 +00:00
|
|
|
atomic_fetch_add_explicit(&texture->state.w, 1, memory_order_release);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-06-12 12:36:00 +00:00
|
|
|
enum EGL_TexStatus egl_texture_process(EGL_Texture * texture)
|
2018-09-23 05:48:44 +00:00
|
|
|
{
|
2019-06-12 12:36:00 +00:00
|
|
|
if (!texture->streaming)
|
|
|
|
return EGL_TEX_STATUS_OK;
|
|
|
|
|
2020-07-24 07:39:16 +00:00
|
|
|
const uint8_t su =
|
|
|
|
atomic_load_explicit(&texture->state.u, memory_order_acquire);
|
2020-05-19 07:34:24 +00:00
|
|
|
|
2020-07-24 07:39:16 +00:00
|
|
|
const uint8_t nextu = su + 1;
|
|
|
|
if (
|
|
|
|
su == atomic_load_explicit(&texture->state.w, memory_order_acquire) ||
|
|
|
|
nextu == atomic_load_explicit(&texture->state.s, memory_order_acquire) ||
|
|
|
|
nextu == atomic_load_explicit(&texture->state.d, memory_order_acquire))
|
2019-06-12 12:36:00 +00:00
|
|
|
return texture->ready ? EGL_TEX_STATUS_OK : EGL_TEX_STATUS_NOTREADY;
|
|
|
|
|
2020-12-31 03:18:38 +00:00
|
|
|
const uint8_t b = su % BUFFER_COUNT;
|
2020-10-29 15:32:25 +00:00
|
|
|
|
|
|
|
/* update the texture */
|
|
|
|
if (!texture->dma)
|
2018-10-03 14:09:10 +00:00
|
|
|
{
|
2020-12-31 03:18:38 +00:00
|
|
|
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, texture->buf[b].pbo);
|
2021-06-08 10:39:10 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, texture->tex[b]);
|
2020-10-30 08:31:48 +00:00
|
|
|
glPixelStorei(GL_UNPACK_ROW_LENGTH, texture->pitch);
|
|
|
|
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, texture->width, texture->height,
|
|
|
|
texture->format, texture->dataType, (const void *)0);
|
2020-10-29 15:32:25 +00:00
|
|
|
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
|
2019-06-12 12:36:00 +00:00
|
|
|
|
2020-10-29 15:32:25 +00:00
|
|
|
/* create a fence to prevent usage before the update is complete */
|
2020-12-31 03:18:38 +00:00
|
|
|
texture->buf[b].sync = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
|
2019-06-12 12:36:00 +00:00
|
|
|
|
2020-10-29 15:32:25 +00:00
|
|
|
/* we must flush to ensure the sync is in the command buffer */
|
|
|
|
glFlush();
|
|
|
|
}
|
2020-05-22 07:47:19 +00:00
|
|
|
|
2020-05-29 05:48:59 +00:00
|
|
|
texture->ready = true;
|
2020-07-24 07:39:16 +00:00
|
|
|
atomic_fetch_add_explicit(&texture->state.u, 1, memory_order_release);
|
2019-06-12 12:36:00 +00:00
|
|
|
|
|
|
|
return EGL_TEX_STATUS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum EGL_TexStatus egl_texture_bind(EGL_Texture * texture)
|
|
|
|
{
|
2020-07-24 07:39:16 +00:00
|
|
|
uint8_t ss = atomic_load_explicit(&texture->state.s, memory_order_acquire);
|
|
|
|
uint8_t sd = atomic_load_explicit(&texture->state.d, memory_order_acquire);
|
2021-06-08 10:39:10 +00:00
|
|
|
GLuint tex = texture->tex[0];
|
2020-05-19 07:34:24 +00:00
|
|
|
|
|
|
|
if (texture->streaming)
|
|
|
|
{
|
|
|
|
if (!texture->ready)
|
|
|
|
return EGL_TEX_STATUS_NOTREADY;
|
|
|
|
|
2020-12-31 03:18:38 +00:00
|
|
|
const uint8_t b = ss % BUFFER_COUNT;
|
2020-10-29 15:32:25 +00:00
|
|
|
if (texture->dma)
|
|
|
|
{
|
|
|
|
ss = atomic_fetch_add_explicit(&texture->state.s, 1,
|
|
|
|
memory_order_release) + 1;
|
|
|
|
}
|
2020-12-31 03:18:38 +00:00
|
|
|
else if (texture->buf[b].sync != 0)
|
2020-05-19 07:34:24 +00:00
|
|
|
{
|
2020-12-31 03:18:38 +00:00
|
|
|
switch(glClientWaitSync(texture->buf[b].sync, 0, 20000000)) // 20ms
|
2020-05-19 07:34:24 +00:00
|
|
|
{
|
|
|
|
case GL_ALREADY_SIGNALED:
|
|
|
|
case GL_CONDITION_SATISFIED:
|
2020-12-31 03:18:38 +00:00
|
|
|
glDeleteSync(texture->buf[b].sync);
|
|
|
|
texture->buf[b].sync = 0;
|
2020-05-19 07:34:24 +00:00
|
|
|
|
2020-07-24 07:39:16 +00:00
|
|
|
ss = atomic_fetch_add_explicit(&texture->state.s, 1,
|
|
|
|
memory_order_release) + 1;
|
2020-05-19 07:34:24 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GL_TIMEOUT_EXPIRED:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GL_WAIT_FAILED:
|
2020-05-22 07:47:19 +00:00
|
|
|
case GL_INVALID_VALUE:
|
2020-12-31 03:18:38 +00:00
|
|
|
glDeleteSync(texture->buf[b].sync);
|
|
|
|
texture->buf[b].sync = 0;
|
2021-01-24 02:17:11 +00:00
|
|
|
DEBUG_EGL_ERROR("glClientWaitSync failed");
|
2020-05-19 07:34:24 +00:00
|
|
|
return EGL_TEX_STATUS_ERROR;
|
|
|
|
}
|
|
|
|
}
|
2020-05-19 12:42:55 +00:00
|
|
|
|
2020-08-15 12:37:54 +00:00
|
|
|
if (ss != sd && ss != (uint8_t)(sd + 1))
|
2020-07-24 07:39:16 +00:00
|
|
|
sd = atomic_fetch_add_explicit(&texture->state.d, 1,
|
|
|
|
memory_order_release) + 1;
|
2021-06-08 10:39:10 +00:00
|
|
|
|
2021-06-08 10:51:09 +00:00
|
|
|
if (!texture->dma)
|
|
|
|
tex = texture->tex[sd % BUFFER_COUNT];
|
2020-05-19 07:34:24 +00:00
|
|
|
}
|
2018-10-03 14:09:10 +00:00
|
|
|
|
2020-10-30 08:31:48 +00:00
|
|
|
glActiveTexture(GL_TEXTURE0);
|
2021-06-08 10:39:10 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, tex);
|
2020-10-30 08:31:48 +00:00
|
|
|
glBindSampler(0, texture->sampler);
|
2019-06-12 12:36:00 +00:00
|
|
|
|
|
|
|
return EGL_TEX_STATUS_OK;
|
2018-09-23 10:45:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int egl_texture_count(EGL_Texture * texture)
|
|
|
|
{
|
2020-10-30 08:31:48 +00:00
|
|
|
return 1;
|
2020-04-14 03:27:07 +00:00
|
|
|
}
|