# SPDX-License-Identifier: MIT
# Local source snapshot: python packaging/make-dist.py
pkgname=qcom-va-driver
pkgver=1.1.0
pkgrel=1
pkgdesc='VA-API backend for Qualcomm Iris stateful V4L2 M2M decoding and H.264/HEVC encoding'
arch=('aarch64')
url='https://github.com/radxa-pkg/qcom-va-driver'
provides=("libva-v4l2=$pkgver")
conflicts=('libva-v4l2')
replaces=('libva-v4l2')
license=('MIT' 'GPL-2.0-only') # backend MIT; bundled Linux patch GPL-2.0-only
depends=('glibc' 'gcc-libs' 'libva' 'libdrm' 'mesa' 'libglvnd' 'qcom-fastrpc' 'vulkan-icd-loader' 'vulkan-freedreno')
makedepends=('meson' 'ninja' 'ghc' 'haskell-syb' 'vulkan-headers' 'glslang')
# Native strip cannot process the Hexagon DSP skeleton.
options=('!strip')
source=("$pkgname-$pkgver.tar.gz")
# The archive is generated locally from the current working tree.
sha256sums=('SKIP')

prepare() {
  # Fetch required build tools here so build() can run with nodownload.
  # FastRPC is supplied by qcom-fastrpc, not the fallback subproject.
  meson subprojects download --sourcedir="$srcdir/$pkgname-$pkgver" qaic hexagon
}

build() {
  # make-dist.py fixes source mtimes to zero. Reusing Ninja objects from an
  # earlier snapshot can silently package the old driver despite new sources.
  rm -rf "$srcdir/build"
  arch-meson "$srcdir/$pkgname-$pkgver" "$srcdir/build" \
    -Dgpu_copy=enabled -Dcdsp_vpp=enabled -Dfastrpc=system -Dvulkan_vpp=enabled
  meson compile -C "$srcdir/build"
}

package() {
  DESTDIR="$pkgdir" meson install -C "$srcdir/build" --no-rebuild
  cp -a "$srcdir/$pkgname-$pkgver/patches" "$pkgdir/usr/share/doc/$pkgname/"

  # Enable and prefer GStreamer VA elements in user sessions and login shells.
  local feature_rank='vah264dec:1000,vah264enc:1000,vah265dec:1000,vah265enc:1000,vavp9dec:1000'
  install -d "$pkgdir/usr/lib/environment.d" "$pkgdir/etc/profile.d"
  printf '%s\n' 'GST_VA_ALL_DRIVERS=1' \
    "GST_PLUGIN_FEATURE_RANK=$feature_rank" \
    > "$pkgdir/usr/lib/environment.d/60-libva-v4l2.conf"
  printf '%s\n' 'export GST_VA_ALL_DRIVERS=1' \
    "export GST_PLUGIN_FEATURE_RANK=$feature_rank" \
    > "$pkgdir/etc/profile.d/libva-v4l2.sh"
  chmod 644 "$pkgdir/usr/lib/environment.d/60-libva-v4l2.conf" \
    "$pkgdir/etc/profile.d/libva-v4l2.sh"
}
