drm: reduce duplicated code in render creation

initMgpu already does build gl formats so remove the temp creation of
allocator and renderer in onReady() that only built glformats,
also if guard the creation of renderers and allocators to make it less
likely to recreate one that is already existing.
This commit is contained in:
Tom Englund 2026-02-15 23:46:59 +01:00
parent a494d50d32
commit 8e30af3bb7

View file

@ -595,13 +595,11 @@ bool Aquamarine::CDRMBackend::shouldBlit() {
}
bool Aquamarine::CDRMBackend::initMgpu() {
SP<CGBMAllocator> newAllocator;
if (primary || backend->primaryAllocator->type() != AQ_ALLOCATOR_TYPE_GBM) {
newAllocator = CGBMAllocator::create(backend->reopenDRMNode(gpu->fd), backend);
rendererState.allocator = newAllocator;
} else {
newAllocator = ((CGBMAllocator*)backend->primaryAllocator.get())->self.lock();
rendererState.allocator = newAllocator;
if (!rendererState.allocator) {
if (primary || backend->primaryAllocator->type() != AQ_ALLOCATOR_TYPE_GBM)
rendererState.allocator = CGBMAllocator::create(backend->reopenDRMNode(gpu->fd), backend);
else
rendererState.allocator = ((CGBMAllocator*)backend->primaryAllocator.get())->self.lock();
}
if (!rendererState.allocator) {
@ -609,7 +607,8 @@ bool Aquamarine::CDRMBackend::initMgpu() {
return false;
}
rendererState.renderer = CDRMRenderer::attempt(backend.lock(), gpu->renderNodeFd >= 0 ? gpu->renderNodeFd : gpu->fd);
if (!rendererState.renderer)
rendererState.renderer = CDRMRenderer::attempt(backend.lock(), gpu->renderNodeFd >= 0 ? gpu->renderNodeFd : gpu->fd);
if (!rendererState.renderer) {
backend->log(AQ_LOG_ERROR, "drm: initMgpu: no renderer");
@ -1092,21 +1091,15 @@ void Aquamarine::CDRMBackend::onReady() {
// init a drm renderer to gather gl formats.
// if we are secondary, initMgpu will have done that
if (!primary) {
auto a = CGBMAllocator::create(backend->reopenDRMNode(gpu->fd), backend);
if (!a)
backend->log(AQ_LOG_ERROR, "drm: onReady: no renderer for gl formats");
else {
auto r = CDRMRenderer::attempt(backend.lock(), gpu->renderNodeFd >= 0 ? gpu->renderNodeFd : gpu->fd);
if (!r)
backend->log(AQ_LOG_ERROR, "drm: onReady: no renderer for gl formats");
else {
TRACE(backend->log(AQ_LOG_TRACE, std::format("drm: onReady: gathered {} gl formats", r->formats.size())));
buildGlFormats(r->formats);
r.reset();
a.reset();
}
}
bool ok = true;
if (primary)
ok = updateSecondaryRendererState();
else if (!rendererState.renderer || !rendererState.allocator)
ok = initMgpu();
if (!ok) {
backend->log(AQ_LOG_ERROR, std::format("drm: Failed to initialize renderer state for {}", gpu->path));
return;
}
for (auto const& c : connectors) {
@ -1670,7 +1663,13 @@ void Aquamarine::SDRMConnector::connect(drmModeConnector* connector) {
if (!backend->backend->ready)
return;
if (!backend->updateSecondaryRendererState()) {
bool ok = true;
if (backend->primary)
ok = backend->updateSecondaryRendererState();
else if (!backend->rendererState.renderer || !backend->rendererState.allocator)
ok = backend->initMgpu();
if (!ok) {
backend->backend->log(AQ_LOG_ERROR, std::format("drm: Failed to update renderer state for {} on connect", szName));
return;
}