2021-08-02 13:37:33 +00:00
|
|
|
/**
|
|
|
|
* Looking Glass
|
2021-08-04 09:48:32 +00:00
|
|
|
* Copyright © 2017-2021 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.h"
|
2021-08-02 17:58:30 +00:00
|
|
|
#include "texture_buffer.h"
|
|
|
|
|
|
|
|
#include "egl_dynprocs.h"
|
|
|
|
#include "egldebug.h"
|
2021-08-02 13:37:33 +00:00
|
|
|
|
|
|
|
typedef struct TexDMABUF
|
|
|
|
{
|
2021-08-02 17:58:30 +00:00
|
|
|
TextureBuffer base;
|
|
|
|
|
|
|
|
EGLDisplay display;
|
|
|
|
|
|
|
|
size_t imageCount;
|
|
|
|
size_t imageUsed;
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
EGLImage image;
|
|
|
|
}
|
|
|
|
* images;
|
2021-08-02 13:37:33 +00:00
|
|
|
}
|
|
|
|
TexDMABUF;
|
|
|
|
|
|
|
|
EGL_TextureOps EGL_TextureDMABUF;
|
|
|
|
|
2021-08-02 17:58:30 +00:00
|
|
|
// internal functions
|
|
|
|
|
2021-08-08 07:16:10 +00:00
|
|
|
static void egl_texDMABUFCleanup(TexDMABUF * this)
|
2021-08-02 13:37:33 +00:00
|
|
|
{
|
2021-08-03 01:41:43 +00:00
|
|
|
for (size_t i = 0; i < this->imageUsed; ++i)
|
2021-08-02 17:58:30 +00:00
|
|
|
eglDestroyImage(this->display, this->images[i].image);
|
|
|
|
|
|
|
|
this->imageUsed = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// dmabuf functions
|
2021-08-02 13:37:33 +00:00
|
|
|
|
2021-08-08 07:16:10 +00:00
|
|
|
static bool egl_texDMABUFInit(EGL_Texture ** texture, EGLDisplay * display)
|
2021-08-02 17:58:30 +00:00
|
|
|
{
|
2021-08-15 22:55:19 +00:00
|
|
|
TexDMABUF * this = calloc(1, sizeof(*this));
|
2021-08-02 17:58:30 +00:00
|
|
|
*texture = &this->base.base;
|
|
|
|
|
|
|
|
EGL_Texture * parent = &this->base.base;
|
2021-08-08 10:11:01 +00:00
|
|
|
if (!egl_texBufferStreamInit(&parent, display))
|
2021-08-02 17:58:30 +00:00
|
|
|
{
|
|
|
|
free(this);
|
|
|
|
*texture = NULL;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->display = display;
|
2021-08-02 13:37:33 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-08-08 07:16:10 +00:00
|
|
|
static void egl_texDMABUFFree(EGL_Texture * texture)
|
2021-08-02 13:37:33 +00:00
|
|
|
{
|
2021-08-02 17:58:30 +00:00
|
|
|
TextureBuffer * parent = UPCAST(TextureBuffer, texture);
|
|
|
|
TexDMABUF * this = UPCAST(TexDMABUF , parent);
|
|
|
|
|
2021-08-08 07:16:10 +00:00
|
|
|
egl_texDMABUFCleanup(this);
|
2021-08-02 17:58:30 +00:00
|
|
|
free(this->images);
|
2021-08-02 13:37:33 +00:00
|
|
|
|
2021-08-08 07:16:10 +00:00
|
|
|
egl_texBufferFree(&parent->base);
|
2021-08-02 17:58:30 +00:00
|
|
|
free(this);
|
2021-08-02 13:37:33 +00:00
|
|
|
}
|
|
|
|
|
2021-08-08 07:16:10 +00:00
|
|
|
static bool egl_texDMABUFSetup(EGL_Texture * texture, const EGL_TexSetup * setup)
|
2021-08-02 13:37:33 +00:00
|
|
|
{
|
2021-08-02 17:58:30 +00:00
|
|
|
TextureBuffer * parent = UPCAST(TextureBuffer, texture);
|
|
|
|
TexDMABUF * this = UPCAST(TexDMABUF , parent);
|
2021-08-02 13:37:33 +00:00
|
|
|
|
2021-08-08 07:16:10 +00:00
|
|
|
egl_texDMABUFCleanup(this);
|
2021-08-02 17:58:30 +00:00
|
|
|
|
2021-08-08 10:11:01 +00:00
|
|
|
return egl_texBufferSetup(&parent->base, setup);
|
2021-08-02 13:37:33 +00:00
|
|
|
}
|
|
|
|
|
2021-08-08 07:16:10 +00:00
|
|
|
static bool egl_texDMABUFUpdate(EGL_Texture * texture,
|
2021-08-02 13:37:33 +00:00
|
|
|
const EGL_TexUpdate * update)
|
|
|
|
{
|
2021-08-02 17:58:30 +00:00
|
|
|
TextureBuffer * parent = UPCAST(TextureBuffer, texture);
|
|
|
|
TexDMABUF * this = UPCAST(TexDMABUF , parent);
|
2021-08-13 23:51:36 +00:00
|
|
|
|
|
|
|
DEBUG_ASSERT(update->type == EGL_TEXTYPE_DMABUF);
|
2021-08-02 17:58:30 +00:00
|
|
|
|
|
|
|
EGLImage image = EGL_NO_IMAGE;
|
|
|
|
|
|
|
|
for(int i = 0; i < this->imageUsed; ++i)
|
|
|
|
if (this->images[i].fd == update->dmaFD)
|
|
|
|
{
|
|
|
|
image = this->images[i].image;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (image == EGL_NO_IMAGE)
|
|
|
|
{
|
|
|
|
EGLAttrib const attribs[] =
|
|
|
|
{
|
2021-08-09 04:08:10 +00:00
|
|
|
EGL_WIDTH , texture->format.width,
|
|
|
|
EGL_HEIGHT , texture->format.height,
|
|
|
|
EGL_LINUX_DRM_FOURCC_EXT , texture->format.fourcc,
|
2021-08-02 17:58:30 +00:00
|
|
|
EGL_DMA_BUF_PLANE0_FD_EXT , update->dmaFD,
|
|
|
|
EGL_DMA_BUF_PLANE0_OFFSET_EXT, 0,
|
2021-08-09 04:08:10 +00:00
|
|
|
EGL_DMA_BUF_PLANE0_PITCH_EXT , texture->format.stride,
|
2021-08-02 17:58:30 +00:00
|
|
|
EGL_NONE , EGL_NONE
|
|
|
|
};
|
|
|
|
|
|
|
|
image = eglCreateImage(
|
|
|
|
this->display,
|
|
|
|
EGL_NO_CONTEXT,
|
|
|
|
EGL_LINUX_DMA_BUF_EXT,
|
|
|
|
(EGLClientBuffer)NULL,
|
|
|
|
attribs);
|
|
|
|
|
|
|
|
if (image == EGL_NO_IMAGE)
|
|
|
|
{
|
|
|
|
DEBUG_EGL_ERROR("Failed to create EGLImage for DMA transfer");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this->imageUsed == this->imageCount)
|
|
|
|
{
|
|
|
|
size_t newCount = this->imageCount * 2 + 2;
|
|
|
|
void * new = realloc(this->images, newCount * sizeof(*this->images));
|
|
|
|
if (!new)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to allocate memory");
|
|
|
|
eglDestroyImage(this->display, image);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->imageCount = newCount;
|
|
|
|
this->images = new;
|
|
|
|
}
|
|
|
|
|
|
|
|
const size_t index = this->imageUsed++;
|
|
|
|
this->images[index].fd = update->dmaFD;
|
|
|
|
this->images[index].image = image;
|
|
|
|
}
|
|
|
|
|
2021-08-08 10:11:01 +00:00
|
|
|
INTERLOCKED_SECTION(parent->copyLock,
|
|
|
|
{
|
|
|
|
glBindTexture(GL_TEXTURE_2D, parent->tex[parent->bufIndex]);
|
|
|
|
g_egl_dynProcs.glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, image);
|
|
|
|
|
|
|
|
if (parent->sync)
|
|
|
|
glDeleteSync(parent->sync);
|
|
|
|
|
|
|
|
parent->sync = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
|
|
|
|
});
|
|
|
|
glFlush();
|
2021-08-02 17:58:30 +00:00
|
|
|
return true;
|
2021-08-02 13:37:33 +00:00
|
|
|
}
|
|
|
|
|
2021-08-08 07:16:10 +00:00
|
|
|
static EGL_TexStatus egl_texDMABUFProcess(EGL_Texture * texture)
|
2021-08-02 13:37:33 +00:00
|
|
|
{
|
2021-08-02 17:58:30 +00:00
|
|
|
return EGL_TEX_STATUS_OK;
|
2021-08-02 13:37:33 +00:00
|
|
|
}
|
|
|
|
|
2021-08-09 04:08:10 +00:00
|
|
|
static EGL_TexStatus egl_texDMABUFGet(EGL_Texture * texture, GLuint * tex)
|
2021-08-02 13:37:33 +00:00
|
|
|
{
|
2021-08-02 17:58:30 +00:00
|
|
|
TextureBuffer * parent = UPCAST(TextureBuffer, texture);
|
2021-08-08 10:11:01 +00:00
|
|
|
GLsync sync = 0;
|
|
|
|
|
|
|
|
INTERLOCKED_SECTION(parent->copyLock,
|
|
|
|
{
|
|
|
|
if (parent->sync)
|
|
|
|
{
|
|
|
|
sync = parent->sync;
|
|
|
|
parent->sync = 0;
|
|
|
|
parent->rIndex = parent->bufIndex;
|
|
|
|
if (++parent->bufIndex == parent->texCount)
|
|
|
|
parent->bufIndex = 0;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (sync)
|
|
|
|
{
|
|
|
|
switch(glClientWaitSync(sync, 0, 20000000)) // 20ms
|
|
|
|
{
|
|
|
|
case GL_ALREADY_SIGNALED:
|
|
|
|
case GL_CONDITION_SATISFIED:
|
|
|
|
glDeleteSync(sync);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GL_TIMEOUT_EXPIRED:
|
|
|
|
INTERLOCKED_SECTION(parent->copyLock,
|
|
|
|
{
|
|
|
|
if (!parent->sync)
|
|
|
|
parent->sync = sync;
|
|
|
|
else
|
|
|
|
glDeleteSync(sync);
|
|
|
|
});
|
|
|
|
return EGL_TEX_STATUS_NOTREADY;
|
|
|
|
|
|
|
|
case GL_WAIT_FAILED:
|
|
|
|
case GL_INVALID_VALUE:
|
|
|
|
glDeleteSync(sync);
|
|
|
|
DEBUG_GL_ERROR("glClientWaitSync failed");
|
|
|
|
return EGL_TEX_STATUS_ERROR;
|
|
|
|
}
|
|
|
|
}
|
2021-08-02 17:58:30 +00:00
|
|
|
|
2021-08-08 10:11:01 +00:00
|
|
|
*tex = parent->tex[parent->rIndex];
|
2021-08-02 17:58:30 +00:00
|
|
|
return EGL_TEX_STATUS_OK;
|
2021-08-02 13:37:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
EGL_TextureOps EGL_TextureDMABUF =
|
|
|
|
{
|
2021-08-08 07:16:10 +00:00
|
|
|
.init = egl_texDMABUFInit,
|
|
|
|
.free = egl_texDMABUFFree,
|
|
|
|
.setup = egl_texDMABUFSetup,
|
|
|
|
.update = egl_texDMABUFUpdate,
|
|
|
|
.process = egl_texDMABUFProcess,
|
2021-08-09 04:08:10 +00:00
|
|
|
.get = egl_texDMABUFGet
|
2021-08-02 13:37:33 +00:00
|
|
|
};
|