# Maintainer: Markus Fritsche <fritsche.markus@gmail.com>
#
# mesa-panvk-bifrost-video — sibling of mesa-panvk-bifrost (r4) that adds
# VK_KHR_video_decode_h264 on Mali Bifrost SBCs (PAN_ARCH 6/7) backed by
# the SoC's V4L2-stateless hantro VPU (RK3566/RK3568).
#
# Campaign: ~/src/panvk-bifrost-video/ — Phase 4 byte-exact validated
# 2026-05-21 (48/48 BBB display frames match ffmpeg+libva-v4l2-request-
# fourier byte-for-byte on the same hantro). Phase 5 second-model review
# completed; load-bearing findings (output_map OOB, static counter,
# session_init unwind, probe_hantro gate) all applied.
#
# What it does (on top of r4):
#   - 0001..0004: inherited from mesa-panvk-bifrost (robustness2/null-
#     descriptor, vk1.1/1.2 advertisement, EXT_transform_feedback, XFB
#     primitive decomposition) — symlinked from the r4 package directory
#     so the patches don't drift between siblings.
#   - 0005: VK_KHR_video_queue + VK_KHR_video_decode_queue +
#     VK_KHR_video_decode_h264 backed by V4L2-stateless hantro.
#     Touches 14 files in src/panfrost/vulkan/; full diff in
#     0005-panvk-bifrost-video-KHR-video-decode-h264.patch.
#
# Co-existence:
#   - Installs to /usr/lib/panvk-bifrost-video/ (parallel to r4's
#     /usr/lib/panvk-bifrost/). Pick at runtime via VK_ICD_FILENAMES.
#   - r4 stays the recommended default for the Chromium-GPU-process
#     consumer (no video needed there). Use this package when the
#     consumer wants Vulkan video decode (mpv-fourier, ffmpeg-vulkan,
#     future Chromium-VulkanVideoDecoder).
#
# Phase 1 limitations to know about (documented in source comments):
#   - Single video session per device (active_video singleton)
#   - Synchronous decode at record time — no pipelining yet
#   - Hardcoded /dev/video1 + /dev/media0 (matches RK3566/68, blocks
#     other SoCs without a topology-walk port)
#   - Bitstream source buffer assumed HOST_VISIBLE (true on panvk-
#     bifrost, would need fallback on other backends)
#
# Build target: arch-aarch64 runner via marfrit-packages Gitea Actions.
# Mesa build is slow (~30-60min on Cortex-A55).

pkgname=mesa-panvk-bifrost-video
_mesaver=26.0.6
pkgver=26.0.6.r5.video1
pkgrel=1
pkgdesc="Patched Mesa libvulkan_panfrost.so adding VK_KHR_video_decode_h264 on Bifrost SBCs (sibling of mesa-panvk-bifrost-r4)"
arch=('aarch64')
url="https://git.reauktion.de/marfrit/panvk-bifrost"
license=('MIT')

depends=(
    'mesa'              # for shared mesa runtime libs
    'libdrm'
    'wayland'
    'libxcb'
    'libx11'
    'libxshmfence'
    'zlib'
    'zstd'
    'libelf'
    'libffi'
    'expat'
    'llvm-libs'
    'lm_sensors'
)
makedepends=(
    'meson'
    'ninja'
    'glslang'
    'python-mako'
    'python-packaging'
    'wayland-protocols'
    'libxrandr'
    'xorgproto'
    'libdrm'
    'llvm'
    'libclc'
    'spirv-llvm-translator'
    'spirv-tools'
    'rust-bindgen'
    'patch'
)

source=(
    "https://archive.mesa3d.org/mesa-${_mesaver}.tar.xz"
    "0001-panvk-expose-robustness2-nullDescriptor-bifrost.patch"
    "0002-panvk-expose-vulkan-1.1-1.2-on-bifrost.patch"
    "0003-panvk-bifrost-vk-ext-transform-feedback.patch"
    "0004-panvk-bifrost-xfb-primitive-decomposition.patch"
    "0005-panvk-bifrost-video-KHR-video-decode-h264.patch"
    "icd.json"
)
# Mesa tarball checksum matches the sibling r4 package — same upstream version.
sha256sums=(
    'SKIP'  # mesa tarball — co-trust w/ r4 sibling
    'SKIP'  # patches are local
    'SKIP'
    'SKIP'
    'SKIP'
    'SKIP'
    'SKIP'  # icd.json
)

prepare() {
    cd "mesa-${_mesaver}"

    # r1+r2: small sed-based edits inherited from r4 (verbatim from the
    # sibling PKGBUILD — keep in sync).
    sed -i 's|\.KHR_robustness2 = PAN_ARCH >= 10,|.KHR_robustness2 = true,|' src/panfrost/vulkan/panvk_vX_physical_device.c
    sed -i 's|\.EXT_robustness2 = PAN_ARCH >= 10,|.EXT_robustness2 = true,|' src/panfrost/vulkan/panvk_vX_physical_device.c
    sed -i 's|\.nullDescriptor = PAN_ARCH >= 10,|.nullDescriptor = true,|' src/panfrost/vulkan/panvk_vX_physical_device.c
    sed -i 's|bool has_vk1_1 = PAN_ARCH >= 10;|bool has_vk1_1 = true;|' src/panfrost/vulkan/panvk_vX_physical_device.c
    sed -i 's|bool has_vk1_2 = PAN_ARCH >= 10;|bool has_vk1_2 = true;|' src/panfrost/vulkan/panvk_vX_physical_device.c

    # r3: EXT_transform_feedback for Bifrost.
    patch -p1 < "${srcdir}/0003-panvk-bifrost-vk-ext-transform-feedback.patch"

    # r4: XFB primitive decomposition NIR pass.
    patch -p1 < "${srcdir}/0004-panvk-bifrost-xfb-primitive-decomposition.patch"

    # video: VK_KHR_video_decode_h264 via V4L2-hantro.
    patch -p1 < "${srcdir}/0005-panvk-bifrost-video-KHR-video-decode-h264.patch"

    # Sanity-check r1..r4 (inherited).
    grep -q "KHR_robustness2 = true," src/panfrost/vulkan/panvk_vX_physical_device.c
    grep -q "EXT_robustness2 = true," src/panfrost/vulkan/panvk_vX_physical_device.c
    grep -q "nullDescriptor = true," src/panfrost/vulkan/panvk_vX_physical_device.c
    grep -q "has_vk1_1 = true;" src/panfrost/vulkan/panvk_vX_physical_device.c
    grep -q "has_vk1_2 = true;" src/panfrost/vulkan/panvk_vX_physical_device.c
    grep -q "EXT_transform_feedback = PAN_ARCH < 9," src/panfrost/vulkan/panvk_vX_physical_device.c
    test -f src/panfrost/vulkan/jm/panvk_vX_cmd_xfb.c
    grep -q "panvk_per_arch(nir_lower_xfb)" src/panfrost/vulkan/panvk_vX_shader.c
    test -f src/panfrost/vulkan/panvk_vX_xfb_lower.c

    # Sanity-check video patch landed.
    grep -q "KHR_video_queue = PAN_ARCH < 9 && panvk_v4l2_probe_hantro()" \
        src/panfrost/vulkan/panvk_vX_physical_device.c
    grep -q "PANVK_QUEUE_FAMILY_VIDEO_DECODE" src/panfrost/vulkan/panvk_device.h
    test -f src/panfrost/vulkan/panvk_video_decode.c
    test -f src/panfrost/vulkan/panvk_video_decode.h
    test -f src/panfrost/vulkan/panvk_v4l2.c
    test -f src/panfrost/vulkan/panvk_v4l2_h264.c
    test -f src/panfrost/vulkan/panvk_v4l2_h264_slice_header.c
    test -f src/panfrost/vulkan/panvk_v4l2_h264_slice_header.h
    grep -q "panvk_v4l2_h264_slice_header.c" src/panfrost/vulkan/meson.build
    grep -q "panvk_video_queue_submit_noop" src/panfrost/vulkan/panvk_vX_device.c
}

build() {
    cd "mesa-${_mesaver}"
    # Mirror r4's narrow build profile.
    meson setup build/ \
        --prefix=/usr \
        --libdir=lib \
        --buildtype=release \
        -Dvulkan-drivers=panfrost \
        -Dgallium-drivers= \
        -Dplatforms=wayland,x11 \
        -Dglx=disabled \
        -Degl=disabled \
        -Dgles1=disabled \
        -Dgles2=disabled \
        -Dvulkan-layers= \
        -Dtools= \
        -Dgallium-rusticl=false \
        -Dmicrosoft-clc=disabled
    meson compile -C build
}

package() {
    cd "${srcdir}/mesa-${_mesaver}"

    # Co-install path — parallel to r4's /usr/lib/panvk-bifrost/.
    install -Dm755 build/src/panfrost/vulkan/libvulkan_panfrost.so \
        "$pkgdir/usr/lib/panvk-bifrost-video/libvulkan_panfrost.so"

    # ICD JSON pointing at the video build. Opt-in via VK_ICD_FILENAMES;
    # NOT in /usr/share/vulkan/icd.d/ so it doesn't override stock or r4.
    install -Dm644 "$srcdir/icd.json" \
        "$pkgdir/usr/lib/panvk-bifrost-video/icd.json"
}
