@@ -447,7 +447,7 @@ void CDRMRenderer::initContext(bool GLES2) {
447
447
backend->log (AQ_LOG_DEBUG, " CDRMRenderer: Got a high priority context" );
448
448
}
449
449
450
- setEGL ( );
450
+ CEglContextGuard eglContext (* this );
451
451
452
452
EGLEXTENSIONS = (const char *)glGetString (GL_EXTENSIONS);
453
453
@@ -466,12 +466,10 @@ void CDRMRenderer::initContext(bool GLES2) {
466
466
467
467
exts.EXT_read_format_bgra = EGLEXTENSIONS.contains (" GL_EXT_read_format_bgra" );
468
468
exts.EXT_texture_format_BGRA8888 = EGLEXTENSIONS.contains (" GL_EXT_texture_format_BGRA8888" );
469
-
470
- restoreEGL ();
471
469
}
472
470
473
471
void CDRMRenderer::initResources () {
474
- setEGL ( );
472
+ CEglContextGuard eglContext (* this );
475
473
476
474
if (!exts.EXT_image_dma_buf_import || !initDRMFormats ())
477
475
backend->log (AQ_LOG_ERROR, " CDRMRenderer: initDRMFormats failed, dma-buf won't work" );
@@ -493,8 +491,6 @@ void CDRMRenderer::initResources() {
493
491
gl.shaderExt .posAttrib = glGetAttribLocation (gl.shaderExt .program , " pos" );
494
492
gl.shaderExt .texAttrib = glGetAttribLocation (gl.shaderExt .program , " texcoord" );
495
493
gl.shaderExt .tex = glGetUniformLocation (gl.shaderExt .program , " tex" );
496
-
497
- restoreEGL ();
498
494
}
499
495
500
496
SP<CDRMRenderer> CDRMRenderer::attempt (SP<CBackend> backend_, int drmFD, bool GLES2) {
@@ -575,25 +571,25 @@ SP<CDRMRenderer> CDRMRenderer::attempt(SP<CBackend> backend_, Hyprutils::Memory:
575
571
return renderer;
576
572
}
577
573
578
- void CDRMRenderer::setEGL ( ) {
574
+ CEglContextGuard::CEglContextGuard ( const CDRMRenderer& renderer_) : renderer(renderer_ ) {
579
575
savedEGLState.display = eglGetCurrentDisplay ();
580
576
savedEGLState.context = eglGetCurrentContext ();
581
577
savedEGLState.draw = eglGetCurrentSurface (EGL_DRAW);
582
578
savedEGLState.read = eglGetCurrentSurface (EGL_READ);
583
579
584
- if (!eglMakeCurrent (egl.display , EGL_NO_SURFACE, EGL_NO_SURFACE, egl.context ))
585
- backend->log (AQ_LOG_WARNING, " CDRMRenderer: setEGL eglMakeCurrent failed" );
580
+ if (!eglMakeCurrent (renderer. egl .display , EGL_NO_SURFACE, EGL_NO_SURFACE, renderer. egl .context ))
581
+ renderer. backend ->log (AQ_LOG_WARNING, " CDRMRenderer: setEGL eglMakeCurrent failed" );
586
582
}
587
583
588
- void CDRMRenderer::restoreEGL () {
589
- EGLDisplay dpy = savedEGLState.display ? savedEGLState.display : egl.display ;
584
+ CEglContextGuard::~CEglContextGuard () {
585
+ EGLDisplay dpy = savedEGLState.display ? savedEGLState.display : renderer. egl .display ;
590
586
591
587
// egl can't handle this
592
588
if (dpy == EGL_NO_DISPLAY)
593
589
return ;
594
590
595
591
if (!eglMakeCurrent (dpy, savedEGLState.draw , savedEGLState.read , savedEGLState.context ))
596
- backend->log (AQ_LOG_WARNING, " CDRMRenderer: restoreEGL eglMakeCurrent failed" );
592
+ renderer. backend ->log (AQ_LOG_WARNING, " CDRMRenderer: restoreEGL eglMakeCurrent failed" );
597
593
}
598
594
599
595
EGLImageKHR CDRMRenderer::createEGLImage (const SDMABUFAttrs& attrs) {
@@ -783,10 +779,9 @@ int CDRMRenderer::recreateBlitSync() {
783
779
}
784
780
785
781
void CDRMRenderer::clearBuffer (IBuffer* buf) {
786
- setEGL ();
787
-
788
- auto dmabuf = buf->dmabuf ();
789
- GLuint rboID = 0 , fboID = 0 ;
782
+ CEglContextGuard eglContext (*this );
783
+ auto dmabuf = buf->dmabuf ();
784
+ GLuint rboID = 0 , fboID = 0 ;
790
785
791
786
if (!dmabuf.success ) {
792
787
backend->log (AQ_LOG_ERROR, " EGL (clear): cannot clear a non-dmabuf" );
@@ -824,12 +819,10 @@ void CDRMRenderer::clearBuffer(IBuffer* buf) {
824
819
glDeleteFramebuffers (1 , &fboID);
825
820
glDeleteRenderbuffers (1 , &rboID);
826
821
proc.eglDestroyImageKHR (egl.display , rboImage);
827
-
828
- restoreEGL ();
829
822
}
830
823
831
824
CDRMRenderer::SBlitResult CDRMRenderer::blit (SP<IBuffer> from, SP<IBuffer> to, int waitFD) {
832
- setEGL ( );
825
+ CEglContextGuard eglContext (* this );
833
826
834
827
if (from->dmabuf ().size != to->dmabuf ().size ) {
835
828
backend->log (AQ_LOG_ERROR, " EGL (blit): buffer sizes mismatched" );
@@ -997,13 +990,11 @@ CDRMRenderer::SBlitResult CDRMRenderer::blit(SP<IBuffer> from, SP<IBuffer> to, i
997
990
GLCALL (glBindFramebuffer (GL_FRAMEBUFFER, 0 ));
998
991
GLCALL (glBindRenderbuffer (GL_RENDERBUFFER, 0 ));
999
992
1000
- restoreEGL ();
1001
-
1002
993
return {.success = true , .syncFD = explicitFD == -1 ? std::nullopt : std::optional<int >{explicitFD}};
1003
994
}
1004
995
1005
996
void CDRMRenderer::onBufferAttachmentDrop (CDRMRendererBufferAttachment* attachment) {
1006
- setEGL ( );
997
+ CEglContextGuard eglContext (* this );
1007
998
1008
999
TRACE (backend->log (AQ_LOG_TRACE,
1009
1000
std::format (" EGL (onBufferAttachmentDrop): dropping fbo {} rbo {} image 0x{:x}" , attachment->fbo , attachment->rbo , (uintptr_t )attachment->eglImage )));
@@ -1018,8 +1009,6 @@ void CDRMRenderer::onBufferAttachmentDrop(CDRMRendererBufferAttachment* attachme
1018
1009
proc.eglDestroyImageKHR (egl.display , attachment->eglImage );
1019
1010
if (attachment->tex .image )
1020
1011
proc.eglDestroyImageKHR (egl.display , attachment->tex .image );
1021
-
1022
- restoreEGL ();
1023
1012
}
1024
1013
1025
1014
bool CDRMRenderer::verifyDestinationDMABUF (const SDMABUFAttrs& attrs) {
0 commit comments