diff --git a/pkgbuilds/aquamarine/0005-drm-use-the-alpha-variant-when-the-scanout-plane-lacks-it.patch b/pkgbuilds/aquamarine/0005-drm-use-the-alpha-variant-when-the-scanout-plane-lacks-it.patch new file mode 100644 index 0000000000..dac5ca5dbb --- /dev/null +++ b/pkgbuilds/aquamarine/0005-drm-use-the-alpha-variant-when-the-scanout-plane-lacks-it.patch @@ -0,0 +1,221 @@ +diff --git a/CMakeLists.txt b/CMakeLists.txt +index 8558e2e..3d1166b 100644 +--- a/CMakeLists.txt ++++ b/CMakeLists.txt +@@ -167,6 +167,15 @@ add_test( + COMMAND commitThread "commitThread") + add_dependencies(tests commitThread) + ++add_executable(scanoutAlias "tests/ScanoutAlias.cpp") ++target_include_directories(scanoutAlias PRIVATE "./src/include" "./include") ++target_link_libraries(scanoutAlias PRIVATE PkgConfig::deps) ++add_test( ++ NAME "scanoutAlias" ++ WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/tests ++ COMMAND scanoutAlias) ++add_dependencies(tests scanoutAlias) ++ + # Installation + install(TARGETS aquamarine) + install(DIRECTORY "include/aquamarine" DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) +diff --git a/src/allocator/GBM.cpp b/src/allocator/GBM.cpp +index 6f596b7..6bade96 100644 +--- a/src/allocator/GBM.cpp ++++ b/src/allocator/GBM.cpp +@@ -4,6 +4,7 @@ + #include + #include + #include "FormatUtils.hpp" ++#include "ScanoutAlias.hpp" + #include "Shared.hpp" + #include + #include +@@ -101,6 +102,13 @@ Aquamarine::CGBMBuffer::CGBMBuffer(const SAllocatorBufferParams& params, Hypruti + return; + } + ++ // Scanout planes that offer only the alpha variant of an 8 or 10 bpc format (Apple M5 internal panel): ++ // allocate the A variant when the X variant is requested but not offered. ++ if (const uint32_t alt = scanoutAlphaAlias(FORMATS, attrs.format, params.scanout, CURSOR); alt != DRM_FORMAT_INVALID) { ++ allocator->backend->log(AQ_LOG_DEBUG, std::format("GBM: {} not offered by the scanout plane, using {}", fourccToName(attrs.format), fourccToName(alt))); ++ attrs.format = alt; ++ } ++ + bool foundFormat = false; + // check if we can use modifiers. If the requested support has any explicit modifier + // supported by the primary backend, we can. +diff --git a/src/backend/drm/DRM.cpp b/src/backend/drm/DRM.cpp +index 754e9c4..d39b233 100644 +--- a/src/backend/drm/DRM.cpp ++++ b/src/backend/drm/DRM.cpp +@@ -35,6 +35,7 @@ extern "C" { + + #include "Props.hpp" + #include "FormatUtils.hpp" ++#include "ScanoutAlias.hpp" + #include "Shared.hpp" + #include "hwdata.hpp" + #include "Renderer.hpp" +@@ -2562,9 +2563,11 @@ bool Aquamarine::CDRMOutput::commitState(bool onlyTest) { + + if (COMMITTED & COutputState::eOutputStateProperties::AQ_OUTPUT_STATE_FORMAT) { + // verify the format is valid for the primary plane +- bool ok = false; +- for (auto const& f : getRenderFormats()) { +- if (f.drmFormat == STATE.drmFormat) { ++ const auto FORMATS = getRenderFormats(); ++ const auto ALIAS = scanoutAlphaAlias(FORMATS, STATE.drmFormat, true, false); ++ bool ok = false; ++ for (auto const& f : FORMATS) { ++ if (f.drmFormat == STATE.drmFormat || f.drmFormat == ALIAS) { + ok = true; + break; + } +@@ -2784,7 +2787,8 @@ bool Aquamarine::CDRMOutput::commitState(bool onlyTest) { + // sometimes, our consumer could f up the swapchain format and change it without the state changing + bool formatMismatch = false; + if (data.mainFB) { +- if (const auto params = data.mainFB->buffer->dmabuf(); params.success && params.format != STATE.drmFormat) { ++ if (const auto params = data.mainFB->buffer->dmabuf(); params.success && params.format != STATE.drmFormat && ++ !scanoutAlphaAliasAccepted(getRenderFormats(), STATE.drmFormat, params.format, true, false)) { + // formats mismatch. Update the state format and roll with it + backend->backend->log(AQ_LOG_WARNING, + std::format("drm: Formats mismatch in commit, buffer is {} but output is set to {}. Modesetting to {}", fourccToName(params.format), +@@ -3184,7 +3188,7 @@ Aquamarine::IOutput::SCommitSubmission Aquamarine::CDRMOutput::commitAsync(const + + const auto MAIN_BUFFER = data.mainFB ? data.mainFB->buffer.lock() : nullptr; + const auto ATTRS = MAIN_BUFFER ? MAIN_BUFFER->dmabuf() : SDMABUFAttrs{}; +- if (ATTRS.success && ATTRS.format != STATE.drmFormat) ++ if (ATTRS.success && ATTRS.format != STATE.drmFormat && !scanoutAlphaAliasAccepted(getRenderFormats(), STATE.drmFormat, ATTRS.format, true, false)) + return {.error = ENOTSUP}; + + if (options.lateCursor && cursorVisible && connector->crtc->cursor) +diff --git a/src/include/FormatUtils.hpp b/src/include/FormatUtils.hpp +index 75c8733..33f0e6a 100644 +--- a/src/include/FormatUtils.hpp ++++ b/src/include/FormatUtils.hpp +@@ -5,3 +5,4 @@ + + std::string fourccToName(uint32_t drmFormat); + std::string drmModifierToName(uint64_t drmModifier); ++ +diff --git a/src/include/ScanoutAlias.hpp b/src/include/ScanoutAlias.hpp +new file mode 100644 +index 0000000..15b099c +--- /dev/null ++++ b/src/include/ScanoutAlias.hpp +@@ -0,0 +1,48 @@ ++#pragma once ++ ++#include ++#include ++#include ++ ++namespace Aquamarine { ++ // Alpha variant of an opaque 8 or 10 bpc format, or DRM_FORMAT_INVALID. ++ inline uint32_t opaqueToAlphaFormat(uint32_t fmt) { ++ switch (fmt) { ++ case DRM_FORMAT_XRGB8888: return DRM_FORMAT_ARGB8888; ++ case DRM_FORMAT_XBGR8888: return DRM_FORMAT_ABGR8888; ++ case DRM_FORMAT_XRGB2101010: return DRM_FORMAT_ARGB2101010; ++ case DRM_FORMAT_XBGR2101010: return DRM_FORMAT_ABGR2101010; ++ default: return DRM_FORMAT_INVALID; ++ } ++ } ++ ++ inline bool planeHasFormat(const std::vector& planeFormats, uint32_t fmt) { ++ if (fmt == DRM_FORMAT_INVALID) ++ return false; ++ for (auto const& f : planeFormats) { ++ if (f.drmFormat == fmt) ++ return true; ++ } ++ return false; ++ } ++ ++ // The alpha variant to substitute for `requested` on this plane, or DRM_FORMAT_INVALID. ++ // Eligibility is shared by the allocator and the commit-time checks: scanout only, never ++ // cursor, and only when the plane lacks the opaque format but offers the alpha variant. ++ // A plane that offers both formats must keep its original behavior. ++ inline uint32_t scanoutAlphaAlias(const std::vector& planeFormats, uint32_t requested, bool scanout, bool cursor) { ++ const uint32_t alias = opaqueToAlphaFormat(requested); ++ if (!scanout || cursor) ++ return DRM_FORMAT_INVALID; ++ if (planeHasFormat(planeFormats, requested)) ++ return DRM_FORMAT_INVALID; ++ if (!planeHasFormat(planeFormats, alias)) ++ return DRM_FORMAT_INVALID; ++ return alias; ++ } ++ ++ // True when `bufferFormat` is exactly the eligible alias of `requested` on this plane. ++ inline bool scanoutAlphaAliasAccepted(const std::vector& planeFormats, uint32_t requested, uint32_t bufferFormat, bool scanout, bool cursor) { ++ return bufferFormat != requested && bufferFormat == scanoutAlphaAlias(planeFormats, requested, scanout, cursor); ++ } ++} +diff --git a/tests/ScanoutAlias.cpp b/tests/ScanoutAlias.cpp +new file mode 100644 +index 0000000..437e50f +--- /dev/null ++++ b/tests/ScanoutAlias.cpp +@@ -0,0 +1,60 @@ ++#include ++#include ++#include ++#include "ScanoutAlias.hpp" ++ ++using namespace Aquamarine; ++ ++// Survives -DNDEBUG, unlike assert(). ++#define CHECK(cond) \ ++ do { \ ++ if (!(cond)) { \ ++ std::fprintf(stderr, "scanoutAlias: FAILED %s at line %d\n", #cond, __LINE__); \ ++ std::exit(1); \ ++ } \ ++ } while (0) ++ ++static std::vector plane(std::initializer_list fmts) { ++ std::vector v; ++ for (auto f : fmts) ++ v.push_back(SDRMFormat{.drmFormat = f}); ++ return v; ++} ++ ++int main() { ++ const auto ALPHA_ONLY = plane({DRM_FORMAT_ARGB8888, DRM_FORMAT_ABGR8888}); ++ const auto BOTH = plane({DRM_FORMAT_XRGB8888, DRM_FORMAT_ARGB8888}); ++ const auto OPAQUE_ONLY = plane({DRM_FORMAT_XRGB8888}); ++ ++ // Alpha-only plane: the fallback applies. ++ CHECK(scanoutAlphaAlias(ALPHA_ONLY, DRM_FORMAT_XRGB8888, true, false) == DRM_FORMAT_ARGB8888); ++ CHECK(scanoutAlphaAliasAccepted(ALPHA_ONLY, DRM_FORMAT_XRGB8888, DRM_FORMAT_ARGB8888, true, false)); ++ CHECK(!scanoutAlphaAliasAccepted(ALPHA_ONLY, DRM_FORMAT_XRGB8888, DRM_FORMAT_XRGB8888, true, false)); ++ ++ // Both-format plane: no alias. An AR24 buffer next to an XR24 state is a real mismatch, ++ // and the old mismatch/async handling must apply as before. ++ CHECK(scanoutAlphaAlias(BOTH, DRM_FORMAT_XRGB8888, true, false) == DRM_FORMAT_INVALID); ++ CHECK(!scanoutAlphaAliasAccepted(BOTH, DRM_FORMAT_XRGB8888, DRM_FORMAT_ARGB8888, true, false)); ++ ++ // Opaque-only plane: nothing to fall back to. ++ CHECK(scanoutAlphaAlias(OPAQUE_ONLY, DRM_FORMAT_XRGB8888, true, false) == DRM_FORMAT_INVALID); ++ ++ // Controls: cursor buffers and render-only allocations never alias. ++ CHECK(scanoutAlphaAlias(ALPHA_ONLY, DRM_FORMAT_XRGB8888, true, true) == DRM_FORMAT_INVALID); ++ CHECK(scanoutAlphaAlias(ALPHA_ONLY, DRM_FORMAT_XRGB8888, false, false) == DRM_FORMAT_INVALID); ++ ++ // An already-alpha request has no alias. ++ CHECK(scanoutAlphaAlias(ALPHA_ONLY, DRM_FORMAT_ARGB8888, true, false) == DRM_FORMAT_INVALID); ++ CHECK(opaqueToAlphaFormat(DRM_FORMAT_ARGB8888) == DRM_FORMAT_INVALID); ++ ++ // 10 bpc: the M5 Max internal primary plane offers AR30 but neither XR30 nor XB30. ++ const auto M5_PRIMARY = plane({DRM_FORMAT_ARGB2101010, DRM_FORMAT_ARGB8888, DRM_FORMAT_ABGR8888}); ++ CHECK(scanoutAlphaAlias(M5_PRIMARY, DRM_FORMAT_XRGB2101010, true, false) == DRM_FORMAT_ARGB2101010); ++ CHECK(scanoutAlphaAliasAccepted(M5_PRIMARY, DRM_FORMAT_XRGB2101010, DRM_FORMAT_ARGB2101010, true, false)); ++ CHECK(scanoutAlphaAlias(M5_PRIMARY, DRM_FORMAT_XBGR2101010, true, false) == DRM_FORMAT_INVALID); ++ CHECK(scanoutAlphaAlias(plane({DRM_FORMAT_ABGR2101010}), DRM_FORMAT_XBGR2101010, true, false) == DRM_FORMAT_ABGR2101010); ++ CHECK(scanoutAlphaAlias(plane({DRM_FORMAT_XRGB2101010, DRM_FORMAT_ARGB2101010}), DRM_FORMAT_XRGB2101010, true, false) == DRM_FORMAT_INVALID); ++ ++ std::puts("scanoutAlias: all checks passed"); ++ return 0; ++} diff --git a/pkgbuilds/aquamarine/PKGBUILD b/pkgbuilds/aquamarine/PKGBUILD index 1cd3ce6129..02c74fee29 100644 --- a/pkgbuilds/aquamarine/PKGBUILD +++ b/pkgbuilds/aquamarine/PKGBUILD @@ -7,13 +7,17 @@ # restarting the compositor (Apple Silicon); # - reconnect an output whose connector gained modes after it connected, so a # monitor behind a USB4 dock that wakes slowly is not left without a mode -# (NVIDIA N1x). +# (NVIDIA N1x); +# - allocate the alpha variant of an 8 or 10 bpc format when the scanout +# plane offers only the alpha variant, so compositors that ask for XRGB8888 +# or XRGB2101010 start on panels without the opaque formats (M5 Max +# internal panel; hyprwm/aquamarine#496). # hyprwm's fixes fc77bd58 and 04bfb7db, carried with 0.15.1, ship in 0.16.0. -# Drop the carry once hyprwm releases the fixes. +# Drop each carry once hyprwm releases the fix. pkgname=aquamarine pkgver=0.16.0 -pkgrel=1.1 +pkgrel=1.2 pkgdesc='a very light linux rendering backend library' arch=(aarch64) url="https://github.com/hyprwm/$pkgname" @@ -39,15 +43,18 @@ provides=("lib$pkgname.so") _archive="$pkgname-$pkgver" source=("$url/archive/v$pkgver/$_archive.tar.gz" 0001-drm-re-read-possible-CRTCs-when-rescanning-connectors.patch - 0004-drm-reconnect-an-output-whose-connector-gained-modes.patch) + 0004-drm-reconnect-an-output-whose-connector-gained-modes.patch + 0005-drm-use-the-alpha-variant-when-the-scanout-plane-lacks-it.patch) sha256sums=('42339c3096c9de6582c0d71bc86b7e63334a3faf6a2b917d385ad093caf66c3c' '50e9e38ff915b18074dc9b5dd1d07d7f24e340e99f254476d6abe578eece7864' - '7fe7620c222d9f323b6a424301b98176f798f41a97945282ec71b361e742511a') + '7fe7620c222d9f323b6a424301b98176f798f41a97945282ec71b361e742511a' + 'c1a7f2025c0ee7e2d303141dad134fdc26b270d456549174611d69df3232d1cc') prepare() { cd "$_archive" patch -Np1 -i ../0001-drm-re-read-possible-CRTCs-when-rescanning-connectors.patch patch -Np1 -i ../0004-drm-reconnect-an-output-whose-connector-gained-modes.patch + patch -Np1 -i ../0005-drm-use-the-alpha-variant-when-the-scanout-plane-lacks-it.patch } build() {