Skip to content

Commit

Permalink
Replace glFinish() with EGLSyncKHR fences
Browse files Browse the repository at this point in the history
  • Loading branch information
ehopperdietzel committed Oct 30, 2024
1 parent ee157b9 commit 52f46ac
Show file tree
Hide file tree
Showing 13 changed files with 193 additions and 97 deletions.
28 changes: 27 additions & 1 deletion src/backends/graphic/DRM/LGraphicBackendDRM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
#include <LGammaTable.h>
#include <LOutputMode.h>

#include <private/SRMBufferPrivate.h>
#include <SRMCore.h>
#include <SRMDevice.h>
#include <SRMConnector.h>
#include <SRMConnectorMode.h>
#include <SRMBuffer.h>
#include <SRMListener.h>
#include <SRMList.h>
#include <SRMFormat.h>
Expand Down Expand Up @@ -551,6 +551,24 @@ bool LGraphicBackend::textureCreateFromDMA(LTexture *texture, const LDMAPlanes *
return false;
}

bool LGraphicBackend::textureCreateFromGL(LTexture *texture, GLuint id, GLenum target, UInt32 format, const LSize &size, bool transferOwnership)
{
Backend *bknd = (Backend*)compositor()->imp()->graphicBackendData;
SRMBuffer *bkndBuffer = srmBufferCreateGLTextureWrapper(
srmCoreGetAllocatorDevice(bknd->core), id, target, format, size.w(), size.h(), transferOwnership);

if (bkndBuffer)
{
texture->m_graphicBackendData = bkndBuffer;
texture->m_format = srmBufferGetFormat(bkndBuffer);
texture->m_sizeB.setW(srmBufferGetWidth(bkndBuffer));
texture->m_sizeB.setH(srmBufferGetHeight(bkndBuffer));
return true;
}

return false;
}

bool LGraphicBackend::textureUpdateRect(LTexture *texture, UInt32 stride, const LRect &dst, const void *pixels)
{
SRMBuffer *bkndBuffer = (SRMBuffer*)texture->m_graphicBackendData;
Expand Down Expand Up @@ -581,6 +599,12 @@ GLenum LGraphicBackend::textureGetTarget(LTexture *texture)
return srmBufferGetTextureTarget(bkndBuffer);
}

void LGraphicBackend::textureSetFence(LTexture *texture)
{
SRMBuffer *bkndBuffer = (SRMBuffer*)texture->m_graphicBackendData;
return srmBufferCreateSync(bkndBuffer);
}

void LGraphicBackend::textureDestroy(LTexture *texture)
{
SRMBuffer *buffer = (SRMBuffer*)texture->m_graphicBackendData;
Expand Down Expand Up @@ -969,9 +993,11 @@ extern "C" LGraphicBackendInterface *getAPI()
API.textureCreateFromCPUBuffer = &LGraphicBackend::textureCreateFromCPUBuffer;
API.textureCreateFromWaylandDRM = &LGraphicBackend::textureCreateFromWaylandDRM;
API.textureCreateFromDMA = &LGraphicBackend::textureCreateFromDMA;
API.textureCreateFromGL = &LGraphicBackend::textureCreateFromGL;
API.textureUpdateRect = &LGraphicBackend::textureUpdateRect;
API.textureGetID = &LGraphicBackend::textureGetID;
API.textureGetTarget = &LGraphicBackend::textureGetTarget;
API.textureSetFence = &LGraphicBackend::textureSetFence;
API.textureDestroy = &LGraphicBackend::textureDestroy;

/* OUTPUT */
Expand Down
2 changes: 2 additions & 0 deletions src/backends/graphic/LGraphicBackend.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ class Louvre::LGraphicBackend
static bool textureCreateFromCPUBuffer(LTexture *texture, const LSize &size, UInt32 stride, UInt32 format, const void *pixels);
static bool textureCreateFromWaylandDRM(LTexture *texture,void *wlBuffer);
static bool textureCreateFromDMA(LTexture *texture, const LDMAPlanes *planes);
static bool textureCreateFromGL(LTexture *texture, GLuint id, GLenum target, UInt32 format, const LSize &size, bool transferOwnership);
static bool textureUpdateRect(LTexture *texture, UInt32 stride, const LRect &dst, const void *pixels);
static UInt32 textureGetID(LOutput *output, LTexture *texture);
static GLenum textureGetTarget(LTexture *texture);
static void textureSetFence(LTexture *texture);
static void textureDestroy(LTexture *texture);

/* OUTPUT */
Expand Down
75 changes: 60 additions & 15 deletions src/backends/graphic/Wayland/LGraphicBackendWayland.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ struct CPUTexture
Texture texture;
UInt32 pixelSize;
const SRMGLFormat *glFmt;
bool destroy;
};

struct DRMTexture
Expand Down Expand Up @@ -714,6 +715,7 @@ class Louvre::LGraphicBackend
cpuTexture->texture.target = GL_TEXTURE_2D;
cpuTexture->glFmt = glFmt;
cpuTexture->pixelSize = pixelSize;
cpuTexture->destroy = true;
texture->m_graphicBackendData = cpuTexture;
return true;
}
Expand Down Expand Up @@ -772,6 +774,31 @@ class Louvre::LGraphicBackend
return false;
}

static bool textureCreateFromGL(LTexture *texture, GLuint id, GLenum target, UInt32 format, const LSize &/*size*/, bool transferOwnership)
{
const SRMGLFormat *glFmt { srmFormatDRMToGL(format) };

if (!glFmt)
return false;

UInt32 depth, bpp;

if (!srmFormatGetDepthBpp(format, &depth, &bpp))
return false;

if (bpp % 8 != 0)
return false;

CPUTexture *cpuTexture { new CPUTexture() };
cpuTexture->texture.id = id;
cpuTexture->texture.target = target;
cpuTexture->glFmt = glFmt;
cpuTexture->pixelSize = bpp/8;
cpuTexture->destroy = transferOwnership;
texture->m_graphicBackendData = cpuTexture;
return true;
}

static bool textureUpdateRect(LTexture *texture, UInt32 stride, const LRect &dst, const void *pixels)
{
if (texture->sourceType() != LTexture::CPU)
Expand Down Expand Up @@ -815,28 +842,44 @@ class Louvre::LGraphicBackend
return GL_TEXTURE_2D;
}

static void textureSetFence(LTexture */*texture*/)
{
/* TODO: Use fence */
glFlush();
}

static void textureDestroy(LTexture *texture)
{
if (texture->sourceType() == LTexture::CPU)
switch (texture->sourceType())
{
CPUTexture *cpuTexture = (CPUTexture*)texture->m_graphicBackendData;

if (cpuTexture)
case LTexture::CPU:
case LTexture::Framebuffer:
case LTexture::GL:
{
glDeleteTextures(1, &cpuTexture->texture.id);
delete cpuTexture;
}
}
else if (texture->sourceType() == LTexture::WL_DRM)
{
DRMTexture *drmTexture = (DRMTexture*)texture->m_graphicBackendData;
CPUTexture *cpuTexture = (CPUTexture*)texture->m_graphicBackendData;

if (drmTexture)
if (cpuTexture)
{
if (cpuTexture->destroy)
glDeleteTextures(1, &cpuTexture->texture.id);
delete cpuTexture;
}
}
break;
case LTexture::WL_DRM:
{
glDeleteTextures(1, &drmTexture->texture.id);
eglDestroyImage(LCompositor::eglDisplay(), drmTexture->image);
delete drmTexture;
DRMTexture *drmTexture = (DRMTexture*)texture->m_graphicBackendData;

if (drmTexture)
{
glDeleteTextures(1, &drmTexture->texture.id);
eglDestroyImage(LCompositor::eglDisplay(), drmTexture->image);
delete drmTexture;
}
}
break;
case LTexture::DMA:
break;
}
}

Expand Down Expand Up @@ -1200,9 +1243,11 @@ extern "C" LGraphicBackendInterface *getAPI()
API.textureCreateFromCPUBuffer = &LGraphicBackend::textureCreateFromCPUBuffer;
API.textureCreateFromWaylandDRM = &LGraphicBackend::textureCreateFromWaylandDRM;
API.textureCreateFromDMA = &LGraphicBackend::textureCreateFromDMA;
API.textureCreateFromGL = &LGraphicBackend::textureCreateFromGL;
API.textureUpdateRect = &LGraphicBackend::textureUpdateRect;
API.textureGetID = &LGraphicBackend::textureGetID;
API.textureGetTarget = &LGraphicBackend::textureGetTarget;
API.textureSetFence = &LGraphicBackend::textureSetFence;
API.textureDestroy = &LGraphicBackend::textureDestroy;

/* OUTPUT */
Expand Down
82 changes: 33 additions & 49 deletions src/lib/core/LTexture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,26 @@ bool LTexture::setDataFromDMA(const LDMAPlanes &planes) noexcept
return false;
}

bool LTexture::updateRect(const LRect &rect, UInt32 stride, const void *buffer) noexcept
bool LTexture::setDataFromGL(GLuint id, GLenum target, UInt32 format, const LSize &size, bool transferOwnership) noexcept
{
if (m_sourceType == Framebuffer)
return false;

reset();

if (compositor()->imp()->graphicBackend->textureCreateFromGL(this, id, target, format, size, transferOwnership))
{
m_sourceType = GL;
m_format = format;
m_sizeB = size;
return true;
}

return false;
}

bool LTexture::updateRect(const LRect &rect, UInt32 stride, const void *buffer) noexcept
{
if (initialized() && m_sourceType != Framebuffer)
{
m_serial++;
Expand Down Expand Up @@ -246,7 +263,10 @@ LTexture *LTexture::copy(const LSize &dst, const LRect &src, bool highQualitySca

// Check if HQ downscaling is needed
if (wScaleF <= 2.f && hScaleF <= 2.f)
{
LLog::debug("[LTexture::copyB] Scale <= 2. Skipping HQ scaling as it's not required.");
goto skipHQ;
}

GLenum textureTarget = target();
GLuint prevProgram = painter->imp()->currentProgram;
Expand Down Expand Up @@ -332,14 +352,14 @@ LTexture *LTexture::copy(const LSize &dst, const LRect &src, bool highQualitySca
painter->imp()->shaderSetMode(LPainter::LPainterPrivate::LegacyMode);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
textureCopy = new LTexture(premultipliedAlpha());
ret = textureCopy->setDataB(texCopy, GL_TEXTURE_2D, DRM_FORMAT_ABGR8888, dstSize, painter->imp()->output);
ret = textureCopy->setDataFromGL(texCopy, GL_TEXTURE_2D, DRM_FORMAT_ABGR8888, dstSize, true);
glDeleteFramebuffers(1, &framebuffer);
glUseProgram(prevProgram);

if (ret)
{
LLog::debug("[LTexture::copyB] New texture copy (highQualityScaling = true).");
glFinish();
textureCopy->setFence();
return textureCopy;
}

Expand Down Expand Up @@ -381,7 +401,7 @@ LTexture *LTexture::copy(const LSize &dst, const LRect &src, bool highQualitySca
GL_LINEAR, GL_LINEAR);
glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, srcRect.x(), srcRect.y(), srcRect.w(), srcRect.h(), 0);
textureCopy = new LTexture(premultipliedAlpha());
ret = textureCopy->setDataB(texCopy, GL_TEXTURE_2D, DRM_FORMAT_ABGR8888, dstSize, painter->imp()->output);
ret = textureCopy->setDataFromGL(texCopy, GL_TEXTURE_2D, DRM_FORMAT_ABGR8888, dstSize, true);
glDeleteFramebuffers(1, &framebuffer);
LLog::debug("[LTexture::copyB] New texture copy (glCopyTexImage2 method).");

Expand Down Expand Up @@ -427,7 +447,7 @@ LTexture *LTexture::copy(const LSize &dst, const LRect &src, bool highQualitySca
painter->drawRect(LRect(0, dstSize));
glEnable(GL_BLEND);
textureCopy = new LTexture(premultipliedAlpha());
ret = textureCopy->setDataB(texCopy, GL_TEXTURE_2D, DRM_FORMAT_ABGR8888, dstSize, painter->imp()->output);
ret = textureCopy->setDataFromGL(texCopy, GL_TEXTURE_2D, DRM_FORMAT_ABGR8888, dstSize, true);
glDeleteFramebuffers(1, &framebuffer);
painter->bindFramebuffer(prevFb);
LLog::debug("[LTexture::copyB] New texture copy (highQualityScaling = false).");
Expand All @@ -440,7 +460,7 @@ LTexture *LTexture::copy(const LSize &dst, const LRect &src, bool highQualitySca

if (ret)
{
glFinish();
textureCopy->setFence();
return textureCopy;
}

Expand Down Expand Up @@ -571,17 +591,16 @@ bool LTexture::save(const std::filesystem::path &name) const noexcept
return false;
}

void LTexture::setFence() noexcept
{
if (initialized() && (m_sourceType == GL || m_sourceType == Framebuffer))
compositor()->imp()->graphicBackend->textureSetFence(this);
}

GLuint LTexture::id(LOutput *output) const noexcept
{
if (initialized())
{
if (sourceType() == Framebuffer)
return m_nativeId;
else if (sourceType() == Native)
return m_nativeId;
else
return compositor()->imp()->graphicBackend->textureGetID(output, (LTexture*)this);
}
return compositor()->imp()->graphicBackend->textureGetID(output, (LTexture*)this);

return 0;
}
Expand All @@ -603,44 +622,9 @@ void LTexture::reset() noexcept

m_serial++;

if (sourceType() == Framebuffer)
{
if (m_nativeId)
{
LLog::debug("[LTexture::reset] Native texture %d deleted.", m_nativeId);
glDeleteTextures(1, &m_nativeId);
m_nativeId = 0;
}
return;
}

if (sourceType() == Native)
{
glDeleteTextures(1, &m_nativeId);
m_nativeId = 0;
m_graphicBackendData = nullptr;
return;
}

if (m_graphicBackendData)
{
compositor()->imp()->graphicBackend->textureDestroy(this);
m_graphicBackendData = nullptr;
}
}

bool LTexture::setDataB(GLuint textureId, GLenum target, UInt32 format, const LSize &size, LOutput *output) noexcept
{
if (m_sourceType == Framebuffer)
return false;

reset();
m_sourceType = Native;
m_graphicBackendData = &m_nativeId;
m_nativeId = textureId;
m_nativeTarget = target;
m_nativeOutput.reset(output);
m_format = format;
m_sizeB = size;
return true;
}
Loading

0 comments on commit 52f46ac

Please sign in to comment.