From 9856563f8966856871a95f70757362f694ff22a6 Mon Sep 17 00:00:00 2001 From: Tom Englund Date: Mon, 7 Jul 2025 23:44:35 +0200 Subject: [PATCH] opengl: avoid reallocations in EGLImage (#10960) use a std::array instead of vector and avoid reallocations. it should at most be 49 entries, so make the array 50. and RASSERT check it incase more entries gets added in the future. --- src/render/OpenGL.cpp | 45 +++++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/src/render/OpenGL.cpp b/src/render/OpenGL.cpp index 6b311a25a..de6434859 100644 --- a/src/render/OpenGL.cpp +++ b/src/render/OpenGL.cpp @@ -517,14 +517,15 @@ void CHyprOpenGLImpl::initDRMFormats() { } EGLImageKHR CHyprOpenGLImpl::createEGLImage(const Aquamarine::SDMABUFAttrs& attrs) { - std::vector attribs; + std::array attribs; + size_t idx = 0; - attribs.push_back(EGL_WIDTH); - attribs.push_back(attrs.size.x); - attribs.push_back(EGL_HEIGHT); - attribs.push_back(attrs.size.y); - attribs.push_back(EGL_LINUX_DRM_FOURCC_EXT); - attribs.push_back(attrs.format); + attribs[idx++] = EGL_WIDTH; + attribs[idx++] = attrs.size.x; + attribs[idx++] = EGL_HEIGHT; + attribs[idx++] = attrs.size.y; + attribs[idx++] = EGL_LINUX_DRM_FOURCC_EXT; + attribs[idx++] = attrs.format; struct { EGLint fd; @@ -538,25 +539,27 @@ EGLImageKHR CHyprOpenGLImpl::createEGLImage(const Aquamarine::SDMABUFAttrs& attr {EGL_DMA_BUF_PLANE2_FD_EXT, EGL_DMA_BUF_PLANE2_OFFSET_EXT, EGL_DMA_BUF_PLANE2_PITCH_EXT, EGL_DMA_BUF_PLANE2_MODIFIER_LO_EXT, EGL_DMA_BUF_PLANE2_MODIFIER_HI_EXT}, {EGL_DMA_BUF_PLANE3_FD_EXT, EGL_DMA_BUF_PLANE3_OFFSET_EXT, EGL_DMA_BUF_PLANE3_PITCH_EXT, EGL_DMA_BUF_PLANE3_MODIFIER_LO_EXT, EGL_DMA_BUF_PLANE3_MODIFIER_HI_EXT}}; - for (int i = 0; i < attrs.planes; i++) { - attribs.push_back(attrNames[i].fd); - attribs.push_back(attrs.fds[i]); - attribs.push_back(attrNames[i].offset); - attribs.push_back(attrs.offsets[i]); - attribs.push_back(attrNames[i].pitch); - attribs.push_back(attrs.strides[i]); + for (int i = 0; i < attrs.planes; ++i) { + attribs[idx++] = attrNames[i].fd; + attribs[idx++] = attrs.fds[i]; + attribs[idx++] = attrNames[i].offset; + attribs[idx++] = attrs.offsets[i]; + attribs[idx++] = attrNames[i].pitch; + attribs[idx++] = attrs.strides[i]; + if (m_hasModifiers && attrs.modifier != DRM_FORMAT_MOD_INVALID) { - attribs.push_back(attrNames[i].modlo); - attribs.push_back(attrs.modifier & 0xFFFFFFFF); - attribs.push_back(attrNames[i].modhi); - attribs.push_back(attrs.modifier >> 32); + attribs[idx++] = attrNames[i].modlo; + attribs[idx++] = static_cast(attrs.modifier & 0xFFFFFFFF); + attribs[idx++] = attrNames[i].modhi; + attribs[idx++] = static_cast(attrs.modifier >> 32); } } - attribs.push_back(EGL_IMAGE_PRESERVED_KHR); - attribs.push_back(EGL_TRUE); + attribs[idx++] = EGL_IMAGE_PRESERVED_KHR; + attribs[idx++] = EGL_TRUE; + attribs[idx++] = EGL_NONE; - attribs.push_back(EGL_NONE); + RASSERT(idx <= attribs.size(), "createEglImage: attribs array out of bounds."); EGLImageKHR image = m_proc.eglCreateImageKHR(m_eglDisplay, EGL_NO_CONTEXT, EGL_LINUX_DMA_BUF_EXT, nullptr, (int*)attribs.data()); if (image == EGL_NO_IMAGE_KHR) {