From 8d73016db8ffca32963decf6ad79e31d120aadcb Mon Sep 17 00:00:00 2001 From: Vaxry Date: Fri, 12 Jul 2024 23:23:39 +0200 Subject: [PATCH] drm/renderer: use the duped fd for gl --- include/aquamarine/allocator/GBM.hpp | 1 + src/backend/drm/DRM.cpp | 5 +-- src/backend/drm/Renderer.cpp | 54 +++------------------------- src/backend/drm/Renderer.hpp | 10 +++--- 4 files changed, 12 insertions(+), 58 deletions(-) diff --git a/include/aquamarine/allocator/GBM.hpp b/include/aquamarine/allocator/GBM.hpp index 5033187..3f1af0b 100644 --- a/include/aquamarine/allocator/GBM.hpp +++ b/include/aquamarine/allocator/GBM.hpp @@ -64,5 +64,6 @@ namespace Aquamarine { std::string drmName = ""; friend class CGBMBuffer; + friend class CDRMRenderer; }; }; diff --git a/src/backend/drm/DRM.cpp b/src/backend/drm/DRM.cpp index db5ca79..5babad2 100644 --- a/src/backend/drm/DRM.cpp +++ b/src/backend/drm/DRM.cpp @@ -475,14 +475,15 @@ bool Aquamarine::CDRMBackend::initMgpu() { if (!primary) return true; - mgpu.allocator = CGBMAllocator::create(backend->reopenDRMNode(gpu->fd), backend); + auto newAllocator = CGBMAllocator::create(backend->reopenDRMNode(gpu->fd), backend); + mgpu.allocator = newAllocator; if (!mgpu.allocator) { backend->log(AQ_LOG_ERROR, "drm: initMgpu: no allocator"); return false; } - mgpu.renderer = CDRMRenderer::attempt(gpu->fd, backend.lock()); + mgpu.renderer = CDRMRenderer::attempt(newAllocator, backend.lock()); if (!mgpu.renderer) { backend->log(AQ_LOG_ERROR, "drm: initMgpu: no renderer"); diff --git a/src/backend/drm/Renderer.cpp b/src/backend/drm/Renderer.cpp index 7ce9e7a..663a63f 100644 --- a/src/backend/drm/Renderer.cpp +++ b/src/backend/drm/Renderer.cpp @@ -7,6 +7,7 @@ #include "Shared.hpp" #include "FormatUtils.hpp" #include +#include using namespace Aquamarine; using namespace Hyprutils::Memory; @@ -93,39 +94,6 @@ void main() { gl_FragColor = texture2D(texture0, v_texcoord); })#"; -// ------------------- gbm stuff - -static int openRenderNode(int drmFd) { - auto renderName = drmGetRenderDeviceNameFromFd(drmFd); - if (!renderName) { - // This can happen on split render/display platforms, fallback to - // primary node - renderName = drmGetPrimaryDeviceNameFromFd(drmFd); - if (!renderName) { - gBackend->log(AQ_LOG_ERROR, "drmRenderer: drmGetPrimaryDeviceNameFromFd failed"); - return -1; - } - gBackend->log(AQ_LOG_WARNING, std::format("DRM dev {} has no render node, falling back to primary", renderName)); - - drmVersion* render_version = drmGetVersion(drmFd); - if (render_version && render_version->name) { - if (strcmp(render_version->name, "evdi") == 0) { - free(renderName); - renderName = (char*)malloc(sizeof(char) * 15); - strcpy(renderName, "/dev/dri/card0"); - } - drmFreeVersion(render_version); - } - } - - int renderFD = open(renderName, O_RDWR | O_CLOEXEC); - if (renderFD < 0) - gBackend->log(AQ_LOG_ERROR, std::format("openRenderNode failed to open drm device {}", renderName)); - - free(renderName); - return renderFD; -} - // ------------------- egl stuff inline void loadGLProc(void* pProc, const char* name) { @@ -228,9 +196,9 @@ bool CDRMRenderer::initDRMFormats() { return true; } -SP CDRMRenderer::attempt(int drmfd, SP backend_) { +SP CDRMRenderer::attempt(Hyprutils::Memory::CSharedPointer allocator_, SP backend_) { SP renderer = SP(new CDRMRenderer()); - renderer->drmFD = drmfd; + renderer->drmFD = allocator_->drmFD(); renderer->backend = backend_; gBackend = backend_; @@ -241,20 +209,6 @@ SP CDRMRenderer::attempt(int drmfd, SP backend_) { return nullptr; } - // init gbm stuff - - renderer->gbm.fd = openRenderNode(drmfd); - if (!renderer->gbm.fd) { - backend_->log(AQ_LOG_ERROR, "CDRMRenderer: fail, no gbm fd"); - return nullptr; - } - - renderer->gbm.device = gbm_create_device(renderer->gbm.fd); - if (!renderer->gbm.device) { - backend_->log(AQ_LOG_ERROR, "CDRMRenderer: fail, no gbm device"); - return nullptr; - } - // init egl if (eglBindAPI(EGL_OPENGL_ES_API) == EGL_FALSE) { @@ -291,7 +245,7 @@ SP CDRMRenderer::attempt(int drmfd, SP backend_) { } std::vector attrs = {EGL_NONE}; - renderer->egl.display = renderer->egl.eglGetPlatformDisplayEXT(EGL_PLATFORM_GBM_KHR, renderer->gbm.device, attrs.data()); + renderer->egl.display = renderer->egl.eglGetPlatformDisplayEXT(EGL_PLATFORM_GBM_KHR, allocator_->gbmDevice, attrs.data()); if (renderer->egl.display == EGL_NO_DISPLAY) { backend_->log(AQ_LOG_ERROR, "CDRMRenderer: fail, eglGetPlatformDisplayEXT failed"); return nullptr; diff --git a/src/backend/drm/Renderer.hpp b/src/backend/drm/Renderer.hpp index 55d1420..7fe6840 100644 --- a/src/backend/drm/Renderer.hpp +++ b/src/backend/drm/Renderer.hpp @@ -12,6 +12,8 @@ namespace Aquamarine { + class CGBMAllocator; + struct SGLTex { EGLImage image = nullptr; GLuint texid = 0; @@ -38,7 +40,8 @@ namespace Aquamarine { class CDRMRenderer { public: - static Hyprutils::Memory::CSharedPointer attempt(int drmfd, Hyprutils::Memory::CSharedPointer backend_); + static Hyprutils::Memory::CSharedPointer attempt(Hyprutils::Memory::CSharedPointer allocator_, + Hyprutils::Memory::CSharedPointer backend_); int drmFD = -1; @@ -56,11 +59,6 @@ namespace Aquamarine { } shader, shaderExt; } gl; - struct { - int fd = -1; - gbm_device* device = nullptr; - } gbm; - struct { EGLDisplay display = nullptr; EGLContext context = nullptr;