backends: port most algorithm calls to ranges

This commit is contained in:
Xaver Hugl 2024-02-21 17:22:53 +01:00
parent ff4cfe279a
commit 4f008c0231
11 changed files with 47 additions and 50 deletions

View file

@ -33,6 +33,7 @@
// system
#include <algorithm>
#include <cerrno>
#include <ranges>
#include <sys/stat.h>
#include <unistd.h>
// drm
@ -136,7 +137,7 @@ void DrmBackend::handleUdevEvent()
// Ignore the device seat if the KWIN_DRM_DEVICES envvar is set.
if (!m_explicitGpus.isEmpty()) {
const auto canonicalPath = QFileInfo(device->devNode()).canonicalPath();
const bool foundMatch = std::any_of(m_explicitGpus.begin(), m_explicitGpus.end(), [&canonicalPath](const QString &explicitPath) {
const bool foundMatch = std::ranges::any_of(m_explicitGpus, [&canonicalPath](const QString &explicitPath) {
return QFileInfo(explicitPath).canonicalPath() == canonicalPath;
});
if (!foundMatch) {
@ -214,7 +215,7 @@ DrmGpu *DrmBackend::addGpu(const QString &fileName)
void DrmBackend::addOutput(DrmAbstractOutput *o)
{
const bool allOff = std::all_of(m_outputs.begin(), m_outputs.end(), [](Output *output) {
const bool allOff = std::ranges::all_of(m_outputs, [](Output *output) {
return output->dpmsMode() != Output::DpmsMode::On;
});
if (allOff && m_recentlyUnpluggedDpmsOffOutputs.contains(o->uuid())) {
@ -341,7 +342,7 @@ DrmGpu *DrmBackend::primaryGpu() const
DrmGpu *DrmBackend::findGpu(dev_t deviceId) const
{
auto it = std::find_if(m_gpus.begin(), m_gpus.end(), [deviceId](const auto &gpu) {
auto it = std::ranges::find_if(m_gpus, [deviceId](const auto &gpu) {
return gpu->deviceId() == deviceId;
});
return it == m_gpus.end() ? nullptr : it->get();
@ -356,7 +357,7 @@ bool DrmBackend::applyOutputChanges(const OutputConfiguration &config)
{
QList<DrmOutput *> toBeEnabled;
QList<DrmOutput *> toBeDisabled;
for (const auto &gpu : std::as_const(m_gpus)) {
for (const auto &gpu : m_gpus) {
const auto &outputs = gpu->drmOutputs();
for (const auto &output : outputs) {
if (output->isNonDesktop()) {

View file

@ -98,9 +98,10 @@ bool DrmFramebuffer::isReadable()
return m_readable = m_syncFd.isReadable();
} else {
const auto &fds = m_bufferRef->dmabufAttributes()->fd;
return m_readable = std::all_of(fds.begin(), fds.end(), [](const auto &fd) {
return !fd.isValid() || fd.isReadable();
});
m_readable = std::ranges::all_of(fds, [](const auto &fd) {
return !fd.isValid() || fd.isReadable();
});
return m_readable;
}
}

View file

@ -150,7 +150,7 @@ void DrmAtomicCommit::pageFlipped(std::chrono::nanoseconds timestamp) const
bool DrmAtomicCommit::areBuffersReadable() const
{
return std::all_of(m_buffers.begin(), m_buffers.end(), [](const auto &pair) {
return std::ranges::all_of(m_buffers, [](const auto &pair) {
const auto &[plane, buffer] = pair;
return !buffer || buffer->isReadable();
});

View file

@ -84,7 +84,7 @@ DrmCommitThread::DrmCommitThread(DrmGpu *gpu, const QString &name)
// wait for a primary plane commit to be in, while still enforcing
// a minimum cursor refresh rate of 30Hz
const auto cursorTarget = m_lastPageflip + std::chrono::duration_cast<std::chrono::nanoseconds>(1s) / 30;
const bool cursorOnly = std::all_of(m_commits.begin(), m_commits.end(), [](const auto &commit) {
const bool cursorOnly = std::ranges::all_of(m_commits, [](const auto &commit) {
return commit->isCursorOnly();
});
if (cursorOnly) {
@ -136,7 +136,7 @@ void DrmCommitThread::submit()
return;
}
}
const bool cursorOnly = std::all_of(m_commits.begin(), m_commits.end(), [](const auto &commit) {
const bool cursorOnly = std::ranges::all_of(m_commits, [](const auto &commit) {
return commit->isCursorOnly();
});
for (auto &commit : m_commits) {
@ -190,7 +190,7 @@ void DrmCommitThread::optimizeCommits()
// commits that target the same plane(s) need to stay in the same order
const auto &planes = commit->modifiedPlanes();
const bool skipping = std::any_of(m_commits.begin(), it, [&planes](const auto &other) {
return std::any_of(planes.begin(), planes.end(), [&other](DrmPlane *plane) {
return std::ranges::any_of(planes, [&other](DrmPlane *plane) {
return other->modifiedPlanes().contains(plane);
});
});

View file

@ -210,7 +210,7 @@ QList<std::shared_ptr<DrmConnectorMode>> DrmConnector::modes() const
std::shared_ptr<DrmConnectorMode> DrmConnector::findMode(const drmModeModeInfo &modeInfo) const
{
const auto it = std::find_if(m_modes.constBegin(), m_modes.constEnd(), [&modeInfo](const auto &mode) {
const auto it = std::ranges::find_if(m_modes, [&modeInfo](const auto &mode) {
return checkIfEqual(mode->nativeMode(), &modeInfo);
});
return it == m_modes.constEnd() ? nullptr : *it;
@ -399,9 +399,10 @@ QList<std::shared_ptr<DrmConnectorMode>> DrmConnector::generateCommonModes()
continue;
}
const auto generatedMode = generateMode(size, 60);
if (std::any_of(m_driverModes.cbegin(), m_driverModes.cend(), [generatedMode](const auto &mode) {
return mode->size() == generatedMode->size() && mode->refreshRate() == generatedMode->refreshRate();
})) {
const bool alreadyExists = std::ranges::any_of(m_driverModes, [generatedMode](const auto &mode) {
return mode->size() == generatedMode->size() && mode->refreshRate() == generatedMode->refreshRate();
});
if (alreadyExists) {
continue;
}
ret << generatedMode;

View file

@ -324,20 +324,18 @@ std::unique_ptr<EglGbmLayerSurface::Surface> EglGbmLayerSurface::createSurface(c
if (m_gpu == m_eglBackend->gpu()) {
const auto checkSurfaceNeedsLinear = [&formats](const FormatInfo &fmt) {
const auto &mods = formats[fmt.drmFormat];
return std::all_of(mods.cbegin(), mods.cend(), [](const auto &mod) {
return std::ranges::all_of(mods, [](const auto &mod) {
return mod == DRM_FORMAT_MOD_LINEAR;
});
};
const bool needsLinear =
std::all_of(preferredFormats.cbegin(), preferredFormats.cend(), checkSurfaceNeedsLinear) && std::all_of(fallbackFormats.cbegin(), fallbackFormats.cend(), checkSurfaceNeedsLinear);
const bool needsLinear = std::ranges::all_of(preferredFormats, checkSurfaceNeedsLinear) && std::ranges::all_of(fallbackFormats, checkSurfaceNeedsLinear);
if (needsLinear) {
const auto renderFormats = m_eglBackend->eglDisplayObject()->allSupportedDrmFormats();
const auto checkFormatSupportsLinearRender = [&renderFormats](const auto &formatInfo) {
const auto it = renderFormats.constFind(formatInfo.drmFormat);
return it != renderFormats.cend() && it->nonExternalOnlyModifiers.contains(DRM_FORMAT_MOD_LINEAR);
};
const bool noLinearSupport =
std::none_of(preferredFormats.cbegin(), preferredFormats.cend(), checkFormatSupportsLinearRender) && std::none_of(fallbackFormats.cbegin(), fallbackFormats.cend(), checkFormatSupportsLinearRender);
const bool noLinearSupport = std::ranges::none_of(preferredFormats, checkFormatSupportsLinearRender) && std::ranges::none_of(fallbackFormats, checkFormatSupportsLinearRender);
if (noLinearSupport) {
bufferTarget = BufferTarget::Dumb;
}

View file

@ -29,6 +29,7 @@
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <ranges>
#include <unistd.h>
// drm
#include <drm_fourcc.h>
@ -207,14 +208,14 @@ void DrmGpu::initDrmResources()
}
const auto findBestPlane = [crtcId](const QList<DrmPlane *> &list) {
// if the plane is already used with this crtc, prefer it
const auto connected = std::find_if(list.begin(), list.end(), [crtcId](DrmPlane *plane) {
const auto connected = std::ranges::find_if(list, [crtcId](DrmPlane *plane) {
return plane->crtcId.value() == crtcId;
});
if (connected != list.end()) {
return *connected;
}
// don't take away planes from other crtcs. The kernel currently rejects such commits
const auto notconnected = std::find_if(list.begin(), list.end(), [](DrmPlane *plane) {
const auto notconnected = std::ranges::find_if(list, [](DrmPlane *plane) {
return plane->crtcId.value() == 0;
});
if (notconnected != list.end()) {
@ -254,13 +255,9 @@ bool DrmGpu::updateOutputs()
DrmUniquePtr<drmModeLesseeListRes> lessees{drmModeListLessees(m_fd)};
for (const auto &output : std::as_const(m_drmOutputs)) {
if (output->lease()) {
bool leaseActive = false;
for (uint i = 0; i < lessees->count; i++) {
if (lessees->lessees[i] == output->lease()->lesseeId()) {
leaseActive = true;
break;
}
}
const bool leaseActive = std::ranges::any_of(std::span(lessees->lessees, lessees->count), [output](uint32_t id) {
return output->lease()->lesseeId() == id;
});
if (!leaseActive) {
Q_EMIT output->lease()->revokeRequested();
}
@ -272,7 +269,7 @@ bool DrmGpu::updateOutputs()
QList<DrmOutput *> addedOutputs;
for (int i = 0; i < resources->count_connectors; ++i) {
const uint32_t currentConnector = resources->connectors[i];
const auto it = std::find_if(m_connectors.begin(), m_connectors.end(), [currentConnector](const auto &connector) {
const auto it = std::ranges::find_if(m_connectors, [currentConnector](const auto &connector) {
return connector->id() == currentConnector;
});
if (it == m_connectors.end()) {
@ -339,7 +336,7 @@ bool DrmGpu::updateOutputs()
}
for (const auto &output : std::as_const(addedOutputs)) {
removeOutput(output);
const auto it = std::find_if(m_connectors.begin(), m_connectors.end(), [output](const auto &conn) {
const auto it = std::ranges::find_if(m_connectors, [output](const auto &conn) {
return conn.get() == output->connector();
});
Q_ASSERT(it != m_connectors.end());
@ -397,7 +394,7 @@ DrmPipeline::Error DrmGpu::checkCrtcAssignment(QList<DrmConnector *> connectors,
if (m_atomicModeSetting) {
// try the crtc that this connector is already connected to first
const uint32_t id = connector->crtcId.value();
auto it = std::find_if(crtcs.begin(), crtcs.end(), [id](const auto &crtc) {
auto it = std::ranges::find_if(crtcs, [id](const auto &crtc) {
return id == crtc->id();
});
if (it != crtcs.end()) {
@ -435,7 +432,7 @@ DrmPipeline::Error DrmGpu::testPendingConfiguration()
QList<DrmCrtc *> crtcs;
// only change resources that aren't currently leased away
for (const auto &conn : m_connectors) {
bool isLeased = std::any_of(m_drmOutputs.cbegin(), m_drmOutputs.cend(), [&conn](const auto output) {
const bool isLeased = std::ranges::any_of(m_drmOutputs, [&conn](const auto output) {
return output->lease() && output->pipeline()->connector() == conn.get();
});
if (!isLeased) {
@ -443,7 +440,7 @@ DrmPipeline::Error DrmGpu::testPendingConfiguration()
}
}
for (const auto &crtc : m_crtcs) {
bool isLeased = std::any_of(m_drmOutputs.cbegin(), m_drmOutputs.cend(), [&crtc](const auto output) {
const bool isLeased = std::ranges::any_of(m_drmOutputs, [&crtc](const auto output) {
return output->lease() && output->pipeline()->crtc() == crtc.get();
});
if (!isLeased) {
@ -482,7 +479,7 @@ DrmPipeline::Error DrmGpu::testPipelines()
DrmOutput *DrmGpu::findOutput(quint32 connector)
{
auto it = std::find_if(m_drmOutputs.constBegin(), m_drmOutputs.constEnd(), [connector](DrmOutput *o) {
auto it = std::ranges::find_if(m_drmOutputs, [connector](DrmOutput *o) {
return o->connector()->id() == connector;
});
if (it != m_drmOutputs.constEnd()) {
@ -495,7 +492,7 @@ void DrmGpu::waitIdle()
{
m_socketNotifier->setEnabled(false);
while (true) {
const bool idle = std::all_of(m_drmOutputs.constBegin(), m_drmOutputs.constEnd(), [](DrmOutput *output) {
const bool idle = std::ranges::all_of(m_drmOutputs, [](DrmOutput *output) {
return !output->pipeline()->pageflipsPending();
});
if (idle) {
@ -748,7 +745,7 @@ bool DrmGpu::isActive() const
bool DrmGpu::needsModeset() const
{
return m_forceModeset || std::any_of(m_pipelines.constBegin(), m_pipelines.constEnd(), [](const auto &pipeline) {
return m_forceModeset || std::ranges::any_of(m_pipelines, [](const auto &pipeline) {
return pipeline->needsModeset();
});
}
@ -761,7 +758,7 @@ bool DrmGpu::maybeModeset()
pipelines.removeOne(output->pipeline());
}
}
bool presentPendingForAll = std::all_of(pipelines.constBegin(), pipelines.constEnd(), [](const auto &pipeline) {
const bool presentPendingForAll = std::ranges::all_of(pipelines, [](const auto &pipeline) {
return pipeline->modesetPresentPending() || !pipeline->activePending();
});
if (!presentPendingForAll) {

View file

@ -85,7 +85,7 @@ void DrmPropertyList::addProperty(DrmUniquePtr<drmModePropertyRes> &&prop, uint6
std::optional<std::pair<DrmUniquePtr<drmModePropertyRes>, uint64_t>> DrmPropertyList::takeProperty(const QByteArray &name)
{
const auto it = std::find_if(m_properties.begin(), m_properties.end(), [&name](const auto &pair) {
const auto it = std::ranges::find_if(m_properties, [&name](const auto &pair) {
return pair.first->name == name;
});
if (it != m_properties.end()) {

View file

@ -128,7 +128,7 @@ DrmPipeline::Error DrmPipeline::commitPipelinesAtomic(const QList<DrmPipeline *>
if (mode == CommitMode::Test) {
// if there's a modeset pending, the tests on top of that state
// also have to allow modesets or they'll always fail
const bool wantsModeset = std::any_of(pipelines.begin(), pipelines.end(), [](DrmPipeline *pipeline) {
const bool wantsModeset = std::ranges::any_of(pipelines, [](DrmPipeline *pipeline) {
return pipeline->needsModeset();
});
if (wantsModeset) {
@ -149,7 +149,7 @@ DrmPipeline::Error DrmPipeline::commitPipelinesAtomic(const QList<DrmPipeline *>
qCDebug(KWIN_DRM) << "Atomic modeset test failed!" << strerror(errno);
return errnoToError();
}
const bool withoutModeset = std::all_of(pipelines.begin(), pipelines.end(), [](DrmPipeline *pipeline) {
const bool withoutModeset = std::ranges::all_of(pipelines, [](DrmPipeline *pipeline) {
auto commit = std::make_unique<DrmAtomicCommit>(QVector<DrmPipeline *>{pipeline});
return pipeline->prepareAtomicCommit(commit.get(), CommitMode::TestAllowModeset) == Error::None && commit->test();
});

View file

@ -287,7 +287,7 @@ void Connection::processEvents()
break;
}
case LIBINPUT_EVENT_DEVICE_REMOVED: {
auto it = std::find_if(m_devices.begin(), m_devices.end(), [&event](Device *d) {
auto it = std::ranges::find_if(m_devices, [&event](Device *d) {
return event->device() == d;
});
if (it == m_devices.end()) {

View file

@ -42,6 +42,7 @@
#include <fcntl.h>
#include <gbm.h>
#include <linux/input.h>
#include <ranges>
namespace KWin
{
@ -343,10 +344,9 @@ void X11WindowedBackend::initDri3()
X11WindowedOutput *X11WindowedBackend::findOutput(xcb_window_t window) const
{
auto it = std::find_if(m_outputs.constBegin(), m_outputs.constEnd(),
[window](X11WindowedOutput *output) {
return output->window() == window;
});
const auto it = std::ranges::find_if(m_outputs, [window](X11WindowedOutput *output) {
return output->window() == window;
});
if (it != m_outputs.constEnd()) {
return *it;
}
@ -516,10 +516,9 @@ void X11WindowedBackend::updateWindowTitle()
void X11WindowedBackend::handleClientMessage(xcb_client_message_event_t *event)
{
auto it = std::find_if(m_outputs.begin(), m_outputs.end(),
[event](X11WindowedOutput *output) {
return output->window() == event->window;
});
auto it = std::ranges::find_if(m_outputs, [event](X11WindowedOutput *output) {
return output->window() == event->window;
});
if (it == m_outputs.end()) {
return;
}