drm: reduce duplicated code in render creation (#247)

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-19 01:26:56 +01:00 committed by GitHub
parent 6d0209205a
commit ed26bdbab9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

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");
@ -1097,21 +1096,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) {
@ -1675,7 +1668,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;
}