Skip to content

The truly free, open-source, local-first, cross-platform CapCut replacement. Scriptable through a JSON-RPC API & MCP.

spike(android): the engine under a Kotlin + Compose shell, through UniFFI

Jareer77ce50bb2dc1
diff --git a/android-native/.gitignore b/android-native/.gitignore
new file mode 100644
index 00000000..1e1ba388
--- /dev/null
+++ b/android-native/.gitignore
@@ -0,0 +1,9 @@
+.gradle/
+build/
+app/build/
+local.properties
+.kotlin/
+# Built by build-rust.sh.
+app/src/main/jniLibs/
+app/src/main/java/uniffi/
+uniffi-bindgen/Cargo.lock
diff --git a/android-native/app/build.gradle.kts b/android-native/app/build.gradle.kts
new file mode 100644
index 00000000..df1d2e2f
--- /dev/null
+++ b/android-native/app/build.gradle.kts
@@ -0,0 +1,56 @@
+plugins {
+ id("com.android.application")
+ id("org.jetbrains.kotlin.android")
+ id("org.jetbrains.kotlin.plugin.compose")
+}
+
+android {
+ namespace = "app.concat.editor.spike"
+ compileSdk = 35
+ // The NDK the engine is built with; AGP strips the .so's symbols with it.
+ ndkVersion = "28.2.13676358"
+
+ defaultConfig {
+ // Never app.concat.editor: this installs beside the shipping app.
+ applicationId = "app.concat.editor.spike"
+ minSdk = 26
+ targetSdk = 35
+ versionCode = 1
+ versionName = "0.0.1-spike"
+ ndk { abiFilters += "arm64-v8a" }
+ }
+
+ buildTypes {
+ release {
+ isMinifyEnabled = false
+ // The debug key, so the spike's release build installs as-is.
+ signingConfig = signingConfigs.getByName("debug")
+ }
+ }
+
+ compileOptions {
+ sourceCompatibility = JavaVersion.VERSION_17
+ targetCompatibility = JavaVersion.VERSION_17
+ }
+ kotlinOptions { jvmTarget = "17" }
+ buildFeatures { compose = true }
+
+ packaging {
+ // Compressed in the APK, as cargo-apk packs the Slint app's, so the
+ // two sizes compare like for like. (AGP's default stores them
+ // uncompressed for mmap, which is a bigger download.)
+ jniLibs.useLegacyPackaging = true
+ }
+}
+
+dependencies {
+ val composeBom = platform("androidx.compose:compose-bom:2024.12.01")
+ implementation(composeBom)
+ implementation("androidx.activity:activity-compose:1.9.3")
+ implementation("androidx.compose.ui:ui")
+ implementation("androidx.compose.foundation:foundation")
+ implementation("androidx.compose.material3:material3")
+ implementation("androidx.core:core-ktx:1.15.0")
+ // UniFFI's Kotlin calls the engine through JNA.
+ implementation("net.java.dev.jna:jna:5.15.0@aar")
+}
diff --git a/android-native/app/src/main/AndroidManifest.xml b/android-native/app/src/main/AndroidManifest.xml
new file mode 100644
index 00000000..2799f397
--- /dev/null
+++ b/android-native/app/src/main/AndroidManifest.xml
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="utf-8"?>
+<manifest xmlns:android="http://schemas.android.com/apk/res/android">
+ <application
+ android:label="Concat Spike"
+ android:theme="@android:style/Theme.Material.NoActionBar">
+ <activity
+ android:name=".MainActivity"
+ android:exported="true"
+ android:screenOrientation="portrait"
+ android:configChanges="orientation|keyboardHidden|keyboard|navigation|screenSize|smallestScreenSize|screenLayout|uiMode|density|fontScale|locale|layoutDirection">
+ <intent-filter>
+ <action android:name="android.intent.action.MAIN" />
+ <category android:name="android.intent.category.LAUNCHER" />
+ </intent-filter>
+ </activity>
+ </application>
+</manifest>
diff --git a/android-native/app/src/main/java/app/concat/editor/spike/ConcatNative.kt b/android-native/app/src/main/java/app/concat/editor/spike/ConcatNative.kt
new file mode 100644
index 00000000..fc618fe0
--- /dev/null
+++ b/android-native/app/src/main/java/app/concat/editor/spike/ConcatNative.kt
@@ -0,0 +1,21 @@
+package app.concat.editor.spike
+
+import android.content.Context
+import android.view.Surface
+
+/**
+ * The two JNI entry points UniFFI cannot carry (src/crates/concat-mobile/src/jni.rs).
+ * Loading the library here, through System.loadLibrary, is also what runs it
+ * once before JNA opens the same .so for the UniFFI calls.
+ */
+object ConcatNative {
+ init {
+ System.loadLibrary("concat_mobile")
+ }
+
+ /** Logcat, and the JavaVM + Context that cpal's AAudio host reads. */
+ @JvmStatic external fun init(context: Context)
+
+ /** The Surface's ANativeWindow with one reference acquired; 0 for none. */
+ @JvmStatic external fun windowFromSurface(surface: Surface): Long
+}
diff --git a/android-native/app/src/main/java/app/concat/editor/spike/MainActivity.kt b/android-native/app/src/main/java/app/concat/editor/spike/MainActivity.kt
new file mode 100644
index 00000000..93678366
--- /dev/null
+++ b/android-native/app/src/main/java/app/concat/editor/spike/MainActivity.kt
@@ -0,0 +1,333 @@
+package app.concat.editor.spike
+
+import android.net.Uri
+import android.os.Bundle
+import android.os.SystemClock
+import android.provider.OpenableColumns
+import android.util.Log
+import android.view.SurfaceHolder
+import android.view.SurfaceView
+import androidx.activity.ComponentActivity
+import androidx.activity.compose.rememberLauncherForActivityResult
+import androidx.activity.compose.setContent
+import androidx.activity.result.PickVisualMediaRequest
+import androidx.activity.result.contract.ActivityResultContracts
+import androidx.compose.foundation.background
+import androidx.compose.foundation.border
+import androidx.compose.foundation.horizontalScroll
+import androidx.compose.foundation.layout.Arrangement
+import androidx.compose.foundation.layout.Box
+import androidx.compose.foundation.layout.Column
+import androidx.compose.foundation.layout.Row
+import androidx.compose.foundation.layout.Spacer
+import androidx.compose.foundation.layout.aspectRatio
+import androidx.compose.foundation.layout.fillMaxHeight
+import androidx.compose.foundation.layout.fillMaxSize
+import androidx.compose.foundation.layout.fillMaxWidth
+import androidx.compose.foundation.layout.height
+import androidx.compose.foundation.layout.offset
+import androidx.compose.foundation.layout.padding
+import androidx.compose.foundation.layout.safeDrawingPadding
+import androidx.compose.foundation.layout.width
+import androidx.compose.foundation.rememberScrollState
+import androidx.compose.material3.Button
+import androidx.compose.material3.FilterChip
+import androidx.compose.material3.MaterialTheme
+import androidx.compose.material3.Slider
+import androidx.compose.material3.Text
+import androidx.compose.material3.darkColorScheme
+import androidx.compose.runtime.Composable
+import androidx.compose.runtime.LaunchedEffect
+import androidx.compose.runtime.getValue
+import androidx.compose.runtime.key
+import androidx.compose.runtime.mutableStateOf
+import androidx.compose.runtime.remember
+import androidx.compose.runtime.rememberCoroutineScope
+import androidx.compose.runtime.setValue
+import androidx.compose.ui.Alignment
+import androidx.compose.ui.Modifier
+import androidx.compose.ui.graphics.Color
+import androidx.compose.ui.text.style.TextOverflow
+import androidx.compose.ui.unit.dp
+import androidx.compose.ui.unit.sp
+import androidx.compose.ui.viewinterop.AndroidView
+import java.io.File
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.delay
+import kotlinx.coroutines.launch
+import kotlinx.coroutines.withContext
+import uniffi.concat_mobile.ClipView
+import uniffi.concat_mobile.Engine
+import uniffi.concat_mobile.Stats
+
+private const val TAG = "concat-spike-ui"
+
+class MainActivity : ComponentActivity() {
+ private lateinit var engine: Engine
+
+ override fun onCreate(savedInstanceState: Bundle?) {
+ super.onCreate(savedInstanceState)
+ val started = SystemClock.elapsedRealtime()
+ ConcatNative.init(applicationContext)
+ val loaded = SystemClock.elapsedRealtime()
+ engine = Engine(filesDir.absolutePath)
+ val opened = SystemClock.elapsedRealtime()
+ Log.i(TAG, "library loaded in ${loaded - started} ms, engine open in ${opened - loaded} ms")
+ // Made by the app, so a file adb pushes into it is the app's to read.
+ Log.i(TAG, "push test media to ${getExternalFilesDir(null)}")
+ // For a run driven over adb: --es import <path> [--ez bench true] [--es mode vulkan|gl|cpu]
+ val importPath = intent.getStringExtra("import")
+ val bench = intent.getBooleanExtra("bench", false)
+ val mode = intent.getStringExtra("mode") ?: "vulkan"
+ setContent {
+ MaterialTheme(colorScheme = darkColorScheme()) {
+ Editor(engine, importPath, bench, mode, ::copyIn)
+ }
+ }
+ }
+
+ /** A picked document copied into the app's own storage: the engine reads paths. */
+ private fun copyIn(uri: Uri): File {
+ var name = "picked.mp4"
+ contentResolver.query(uri, arrayOf(OpenableColumns.DISPLAY_NAME), null, null, null)?.use {
+ if (it.moveToFirst()) name = it.getString(0) ?: name
+ }
+ val media = File(filesDir, "media").apply { mkdirs() }
+ val target = File(media, name)
+ contentResolver.openInputStream(uri)!!.use { input ->
+ target.outputStream().use { input.copyTo(it, 1 shl 20) }
+ }
+ return target
+ }
+}
+
+private fun seconds(value: Double) = "%.2f".format(value)
+
+@Composable
+private fun Editor(
+ engine: Engine,
+ importPath: String?,
+ bench: Boolean,
+ initialMode: String,
+ copyIn: (Uri) -> File,
+) {
+ val scope = rememberCoroutineScope()
+ var clips by remember { mutableStateOf(engine.clips()) }
+ var duration by remember { mutableStateOf(engine.duration()) }
+ var video by remember { mutableStateOf(engine.video()) }
+ var position by remember { mutableStateOf(0.0) }
+ var playing by remember { mutableStateOf(false) }
+ var stats by remember { mutableStateOf(Stats("", 0u, 0.0, 0.0, 0.0, 0u, 0.0, 0.0, 0.0, 0u, 0.0, "")) }
+ var mode by remember { mutableStateOf(initialMode) }
+ var message by remember { mutableStateOf("") }
+ var scrubbing by remember { mutableStateOf(false) }
+
+ fun refresh() {
+ clips = engine.clips()
+ duration = engine.duration()
+ video = engine.video()
+ }
+
+ suspend fun importFile(file: File) {
+ val started = SystemClock.elapsedRealtime()
+ try {
+ withContext(Dispatchers.IO) { engine.importVideo(file.absolutePath) }
+ refresh()
+ message = "imported ${file.name} in ${SystemClock.elapsedRealtime() - started} ms"
+ } catch (error: Exception) {
+ message = "import failed: ${error.message}"
+ }
+ Log.i(TAG, message)
+ }
+
+ val picker = rememberLauncherForActivityResult(ActivityResultContracts.PickVisualMedia()) { uri ->
+ if (uri != null) scope.launch {
+ val started = SystemClock.elapsedRealtime()
+ val file = withContext(Dispatchers.IO) { copyIn(uri) }
+ Log.i(TAG, "copied ${file.name} (${file.length() / 1024} KB) in ${SystemClock.elapsedRealtime() - started} ms")
+ importFile(file)
+ }
+ }
+
+ // The playhead and the numbers, read from the engine at the display's pace.
+ LaunchedEffect(Unit) {
+ while (true) {
+ if (!scrubbing) position = engine.position()
+ playing = engine.isPlaying()
+ stats = engine.stats()
+ delay(33)
+ }
+ }
+
+ LaunchedEffect(importPath, bench) {
+ if (importPath != null && clips.isEmpty()) importFile(File(importPath))
+ if (bench) runBench(engine)
+ }
+
+ Column(
+ Modifier.fillMaxSize().background(Color(0xFF111111)).safeDrawingPadding().padding(8.dp),
+ verticalArrangement = Arrangement.spacedBy(8.dp),
+ ) {
+ val aspect = if (video.height > 0u) video.width.toFloat() / video.height.toFloat() else 9f / 16f
+ Box(
+ Modifier.fillMaxWidth().weight(1f).background(Color.Black),
+ contentAlignment = Alignment.Center,
+ ) {
+ // A new SurfaceView for a new renderer: a window that has had a
+ // GPU API connected to it cannot be locked by the CPU path.
+ key(mode) {
+ AndroidView(
+ modifier = Modifier.aspectRatio(aspect, matchHeightConstraintsFirst = aspect < 1f),
+ factory = { context ->
+ SurfaceView(context).apply {
+ holder.addCallback(object : SurfaceHolder.Callback {
+ override fun surfaceCreated(holder: SurfaceHolder) {}
+ override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {
+ val window = ConcatNative.windowFromSurface(holder.surface)
+ if (window == 0L) return
+ val started = SystemClock.elapsedRealtime()
+ try {
+ val described = engine.attachSurface(window.toULong(), width.toUInt(), height.toUInt(), mode)
+ Log.i(TAG, "attached ${width}x$height in ${SystemClock.elapsedRealtime() - started} ms: $described")
+ } catch (error: Exception) {
+ message = "surface: ${error.message}"
+ Log.e(TAG, message)
+ }
+ }
+ override fun surfaceDestroyed(holder: SurfaceHolder) {
+ engine.detachSurface()
+ }
+ })
+ }
+ },
+ )
+ }
+ }
+
+ Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(8.dp)) {
+ Button(onClick = {
+ picker.launch(PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.VideoOnly))
+ }) { Text("Import") }
+ Button(onClick = { if (playing) engine.pause() else engine.play() }, enabled = duration > 0) {
+ Text(if (playing) "Pause" else "Play")
+ }
+ Text("${seconds(position)} / ${seconds(duration)} s", color = Color.White)
+ }
+
+ Slider(
+ value = position.toFloat(),
+ valueRange = 0f..maxOf(duration.toFloat(), 0.01f),
+ onValueChange = {
+ scrubbing = true
+ position = it.toDouble()
+ engine.seek(it.toDouble())
+ },
+ onValueChangeFinished = { scrubbing = false },
+ enabled = duration > 0,
+ )
+
+ ClipStrip(clips, position, duration) { engine.seek(it) }
+
+ Row(horizontalArrangement = Arrangement.spacedBy(6.dp)) {
+ for (option in listOf("vulkan", "gl", "cpu")) {
+ FilterChip(selected = mode == option, onClick = { mode = option }, label = { Text(option) })
+ }
+ }
+ Text(
+ buildString {
+ append(stats.renderer.ifEmpty { "no surface" })
+ append("\nplay ${"%.1f".format(stats.playFps)} fps, late ${stats.lateFrames}; ")
+ append("decode ${"%.1f".format(stats.lastDecodeMs)} ms, draw ${"%.1f".format(stats.lastDrawMs)} ms")
+ append("\nscrub last ${"%.0f".format(stats.lastScrubMs)} / mean ${"%.0f".format(stats.meanScrubMs)} / max ${"%.0f".format(stats.maxScrubMs)} ms over ${stats.scrubs}")
+ if (stats.lastError.isNotEmpty()) append("\n${stats.lastError}")
+ if (message.isNotEmpty()) append("\n$message")
+ },
+ color = Color(0xFFBBBBBB),
+ fontSize = 11.sp,
+ lineHeight = 13.sp,
+ )
+ }
+}
+
+/** The timeline: one block per clip at 48dp a second, the playhead over it. Tap to seek. */
+@Composable
+private fun ClipStrip(clips: List<ClipView>, position: Double, duration: Double, seek: (Double) -> Unit) {
+ val perSecond = 48.dp
+ Box(
+ Modifier.fillMaxWidth().height(56.dp).background(Color(0xFF1C1C1C)).horizontalScroll(rememberScrollState()),
+ ) {
+ Box(Modifier.width(perSecond * maxOf(duration.toFloat(), 1f)).fillMaxHeight()) {
+ for (clip in clips) {
+ Box(
+ Modifier
+ .offset(x = perSecond * clip.start.toFloat(), y = (clip.lane.toInt() * 4).dp)
+ .width(perSecond * clip.duration.toFloat())
+ .height(48.dp)
+ .padding(horizontal = 1.dp)
+ .background(Color(0xFF2F6FDE))
+ .border(1.dp, Color(0xFF9CC0FF)),
+ ) {
+ Text(
+ clip.name,
+ color = Color.White,
+ fontSize = 11.sp,
+ maxLines = 1,
+ overflow = TextOverflow.Ellipsis,
+ modifier = Modifier.padding(4.dp),
+ )
+ }
+ }
+ Spacer(
+ Modifier.offset(x = perSecond * position.toFloat()).width(2.dp).fillMaxHeight().background(Color.White),
+ )
+ }
+ }
+}
+
+/** A scripted run, for numbers without a finger: jumps, a drag, then playback. */
+private suspend fun runBench(engine: Engine) {
+ delay(1500)
+ val duration = engine.duration()
+ if (duration <= 0) {
+ Log.w("concat-spike-bench", "nothing on the timeline")
+ return
+ }
+ fun report(label: String) {
+ val s = engine.stats()
+ Log.i(
+ "concat-spike-bench",
+ "$label: renderer='${s.renderer}' frames=${s.frames} scrubs=${s.scrubs} " +
+ "scrub last=${"%.1f".format(s.lastScrubMs)} mean=${"%.1f".format(s.meanScrubMs)} max=${"%.1f".format(s.maxScrubMs)} ms " +
+ "decode=${"%.1f".format(s.lastDecodeMs)} draw=${"%.1f".format(s.lastDrawMs)} ms " +
+ "playFps=${"%.1f".format(s.playFps)} late=${s.lateFrames} audioPeak=${"%.3f".format(s.audioPeak)} error='${s.lastError}'",
+ )
+ }
+ // Jumps: each seek waited out, the way a tap on the strip lands.
+ engine.resetStats()
+ delay(200)
+ for (i in 0 until 30) {
+ engine.seek(duration * ((i * 7) % 30) / 30.0)
+ delay(250)
+ }
+ report("jumps")
+ // A drag: a seek every 16 ms, as a finger on the slider sends them.
+ engine.resetStats()
+ delay(200)
+ var at = 0.0
+ while (at < minOf(duration, 4.0)) {
+ engine.seek(at)
+ at += 0.05
+ delay(16)
+ }
+ delay(600)
+ report("drag")
+ // Playback for six seconds from the start.
+ engine.seek(0.0)
+ delay(300)
+ engine.resetStats()
+ delay(100)
+ engine.play()
+ delay(6000)
+ report("play")
+ engine.pause()
+}
diff --git a/android-native/build-rust.sh b/android-native/build-rust.sh
new file mode 100755
index 00000000..d9c10dd7
--- /dev/null
+++ b/android-native/build-rust.sh
@@ -0,0 +1,61 @@
+#!/usr/bin/env bash
+# Builds the engine for the spike shell and generates its Kotlin bindings:
+#
+# android-native/build-rust.sh [release|debug]
+#
+# libconcat_mobile.so goes into app/src/main/jniLibs/arm64-v8a beside the
+# runtime libraries the engine links (libc++_shared, ONNX Runtime), and
+# the UniFFI Kotlin into app/src/main/java/uniffi.
+#
+# FFmpeg and sherpa-onnx come from the main checkout's vendor/ (built by
+# src/scripts/ffmpeg-mobile.sh and sherpa-mobile.sh) when this checkout
+# has none, and CARGO_TARGET_DIR defaults to the main checkout's target/,
+# so a worktree never starts a second one.
+set -euo pipefail
+
+profile=${1:-release}
+here=$(cd "$(dirname "$0")" && pwd)
+repo=$(cd "$here/.." && pwd)
+workspace=$repo/src
+vendor_from=$workspace
+if [ ! -d "$workspace/vendor/ffmpeg/aarch64-linux-android" ]; then
+ # A worktree: the main checkout is the one whose .git is a directory.
+ main=$(git -C "$repo" worktree list --porcelain | sed -n '1s/^worktree //p')
+ vendor_from=$main/src
+fi
+export CARGO_TARGET_DIR=${CARGO_TARGET_DIR:-$vendor_from/target}
+
+eval "$("$vendor_from/scripts/mobile-env.sh" aarch64-linux-android)"
+
+flags=()
+[ "$profile" = release ] && flags+=(--release)
+cd "$workspace"
+started=$(date +%s)
+cargo ndk -t arm64-v8a --platform 26 build "${flags[@]}" -p concat-mobile
+echo "cargo ndk took $(( $(date +%s) - started ))s"
+
+so=$CARGO_TARGET_DIR/aarch64-linux-android/$profile/libconcat_mobile.so
+libs=$here/app/src/main/jniLibs/arm64-v8a
+mkdir -p "$libs"
+cp "$so" "$libs/"
+# The runtime libraries the Slint APK carries too, where the engine needs
+# them: ask the linker's own record.
+needed=$("$ANDROID_NDK_HOME"/toolchains/llvm/prebuilt/*/bin/llvm-readelf -d "$so" | sed -n 's/.*Shared library: \[\(.*\)\]/\1/p')
+echo "libconcat_mobile.so needs: $(echo $needed)"
+for lib in $needed; do
+ if [ -f "$SHERPA_ONNX_LIB_DIR/$lib" ]; then cp "$SHERPA_ONNX_LIB_DIR/$lib" "$libs/"; fi
+done
+if echo "$needed" | grep -q 'libc++_shared.so' && [ ! -f "$libs/libc++_shared.so" ]; then
+ cp "$ANDROID_NDK_HOME"/toolchains/llvm/prebuilt/*/sysroot/usr/lib/aarch64-linux-android/libc++_shared.so "$libs/"
+fi
+
+# The bindings, from the library's own metadata, by a bindgen built for
+# this machine from the same uniffi.
+(
+ unset RUSTFLAGS
+ cd "$here/uniffi-bindgen"
+ cargo run -q --release -- \
+ generate --library "$so" --language kotlin --no-format \
+ --out-dir "$here/app/src/main/java"
+)
+ls -la "$libs"
diff --git a/android-native/build.gradle.kts b/android-native/build.gradle.kts
new file mode 100644
index 00000000..cfd711d9
--- /dev/null
+++ b/android-native/build.gradle.kts
@@ -0,0 +1,5 @@
+plugins {
+ id("com.android.application") version "8.7.3" apply false
+ id("org.jetbrains.kotlin.android") version "2.1.0" apply false
+ id("org.jetbrains.kotlin.plugin.compose") version "2.1.0" apply false
+}
diff --git a/android-native/gradle.properties b/android-native/gradle.properties
new file mode 100644
index 00000000..a311f807
--- /dev/null
+++ b/android-native/gradle.properties
@@ -0,0 +1,4 @@
+org.gradle.jvmargs=-Xmx4g -Dfile.encoding=UTF-8
+android.useAndroidX=true
+android.nonTransitiveRClass=true
+kotlin.code.style=official
diff --git a/android-native/gradle/wrapper/gradle-wrapper.jar b/android-native/gradle/wrapper/gradle-wrapper.jar
new file mode 100644
index 00000000..a4b76b95
Binary files /dev/null and b/android-native/gradle/wrapper/gradle-wrapper.jar differ
diff --git a/android-native/gradle/wrapper/gradle-wrapper.properties b/android-native/gradle/wrapper/gradle-wrapper.properties
new file mode 100644
index 00000000..e0fd0202
--- /dev/null
+++ b/android-native/gradle/wrapper/gradle-wrapper.properties
@@ -0,0 +1,7 @@
+distributionBase=GRADLE_USER_HOME
+distributionPath=wrapper/dists
+distributionUrl=https\://services.gradle.org/distributions/gradle-8.12-all.zip
+networkTimeout=10000
+validateDistributionUrl=true
+zipStoreBase=GRADLE_USER_HOME
+zipStorePath=wrapper/dists
diff --git a/android-native/gradlew b/android-native/gradlew
new file mode 100755
index 00000000..f3b75f3b
--- /dev/null
+++ b/android-native/gradlew
@@ -0,0 +1,251 @@
+#!/bin/sh
+
+#
+# Copyright © 2015-2021 the original authors.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# https://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+# SPDX-License-Identifier: Apache-2.0
+#
+
+##############################################################################
+#
+# Gradle start up script for POSIX generated by Gradle.
+#
+# Important for running:
+#
+# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is
+# noncompliant, but you have some other compliant shell such as ksh or
+# bash, then to run this script, type that shell name before the whole
+# command line, like:
+#
+# ksh Gradle
+#
+# Busybox and similar reduced shells will NOT work, because this script
+# requires all of these POSIX shell features:
+# * functions;
+# * expansions «$var», «${var}», «${var:-default}», «${var+SET}»,
+# «${var#prefix}», «${var%suffix}», and «$( cmd )»;
+# * compound commands having a testable exit status, especially «case»;
+# * various built-in commands including «command», «set», and «ulimit».
+#
+# Important for patching:
+#
+# (2) This script targets any POSIX shell, so it avoids extensions provided
+# by Bash, Ksh, etc; in particular arrays are avoided.
+#
+# The "traditional" practice of packing multiple parameters into a
+# space-separated string is a well documented source of bugs and security
+# problems, so this is (mostly) avoided, by progressively accumulating
+# options in "$@", and eventually passing that to Java.
+#
+# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS,
+# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly;
+# see the in-line comments for details.
+#
+# There are tweaks for specific operating systems such as AIX, CygWin,
+# Darwin, MinGW, and NonStop.
+#
+# (3) This script is generated from the Groovy template
+# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
+# within the Gradle project.
+#
+# You can find Gradle at https://github.com/gradle/gradle/.
+#
+##############################################################################
+
+# Attempt to set APP_HOME
+
+# Resolve links: $0 may be a link
+app_path=$0
+
+# Need this for daisy-chained symlinks.
+while
+ APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path
+ [ -h "$app_path" ]
+do
+ ls=$( ls -ld "$app_path" )
+ link=${ls#*' -> '}
+ case $link in #(
+ /*) app_path=$link ;; #(
+ *) app_path=$APP_HOME$link ;;
+ esac
+done
+
+# This is normally unused
+# shellcheck disable=SC2034
+APP_BASE_NAME=${0##*/}
+# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
+APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s\n' "$PWD" ) || exit
+
+# Use the maximum available, or set MAX_FD != -1 to use that value.
+MAX_FD=maximum
+
+warn () {
+ echo "$*"
+} >&2
+
+die () {
+ echo
+ echo "$*"
+ echo
+ exit 1
+} >&2
+
+# OS specific support (must be 'true' or 'false').
+cygwin=false
+msys=false
+darwin=false
+nonstop=false
+case "$( uname )" in #(
+ CYGWIN* ) cygwin=true ;; #(
+ Darwin* ) darwin=true ;; #(
+ MSYS* | MINGW* ) msys=true ;; #(
+ NONSTOP* ) nonstop=true ;;
+esac
+
+CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
+
+
+# Determine the Java command to use to start the JVM.
+if [ -n "$JAVA_HOME" ] ; then
+ if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
+ # IBM's JDK on AIX uses strange locations for the executables
+ JAVACMD=$JAVA_HOME/jre/sh/java
+ else
+ JAVACMD=$JAVA_HOME/bin/java
+ fi
+ if [ ! -x "$JAVACMD" ] ; then
+ die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
+
+Please set the JAVA_HOME variable in your environment to match the
+location of your Java installation."
+ fi
+else
+ JAVACMD=java
+ if ! command -v java >/dev/null 2>&1
+ then
+ die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
+
+Please set the JAVA_HOME variable in your environment to match the
+location of your Java installation."
+ fi
+fi
+
+# Increase the maximum file descriptors if we can.
+if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
+ case $MAX_FD in #(
+ max*)
+ # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
+ # shellcheck disable=SC2039,SC3045
+ MAX_FD=$( ulimit -H -n ) ||
+ warn "Could not query maximum file descriptor limit"
+ esac
+ case $MAX_FD in #(
+ '' | soft) :;; #(
+ *)
+ # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
+ # shellcheck disable=SC2039,SC3045
+ ulimit -n "$MAX_FD" ||
+ warn "Could not set maximum file descriptor limit to $MAX_FD"
+ esac
+fi
+
+# Collect all arguments for the java command, stacking in reverse order:
+# * args from the command line
+# * the main class name
+# * -classpath
+# * -D...appname settings
+# * --module-path (only if needed)
+# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables.
+
+# For Cygwin or MSYS, switch paths to Windows format before running java
+if "$cygwin" || "$msys" ; then
+ APP_HOME=$( cygpath --path --mixed "$APP_HOME" )
+ CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" )
+
+ JAVACMD=$( cygpath --unix "$JAVACMD" )
+
+ # Now convert the arguments - kludge to limit ourselves to /bin/sh
+ for arg do
+ if
+ case $arg in #(
+ -*) false ;; # don't mess with options #(
+ /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath
+ [ -e "$t" ] ;; #(
+ *) false ;;
+ esac
+ then
+ arg=$( cygpath --path --ignore --mixed "$arg" )
+ fi
+ # Roll the args list around exactly as many times as the number of
+ # args, so each arg winds up back in the position where it started, but
+ # possibly modified.
+ #
+ # NB: a `for` loop captures its iteration list before it begins, so
+ # changing the positional parameters here affects neither the number of
+ # iterations, nor the values presented in `arg`.
+ shift # remove old arg
+ set -- "$@" "$arg" # push replacement arg
+ done
+fi
+
+
+# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
+DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
+
+# Collect all arguments for the java command:
+# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
+# and any embedded shellness will be escaped.
+# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
+# treated as '${Hostname}' itself on the command line.
+
+set -- \
+ "-Dorg.gradle.appname=$APP_BASE_NAME" \
+ -classpath "$CLASSPATH" \
+ org.gradle.wrapper.GradleWrapperMain \
+ "$@"
+
+# Stop when "xargs" is not available.
+if ! command -v xargs >/dev/null 2>&1
+then
+ die "xargs is not available"
+fi
+
+# Use "xargs" to parse quoted args.
+#
+# With -n1 it outputs one arg per line, with the quotes and backslashes removed.
+#
+# In Bash we could simply go:
+#
+# readarray ARGS < <( xargs -n1 <<<"$var" ) &&
+# set -- "${ARGS[@]}" "$@"
+#
+# but POSIX shell has neither arrays nor command substitution, so instead we
+# post-process each arg (as a line of input to sed) to backslash-escape any
+# character that might be a shell metacharacter, then use eval to reverse
+# that process (while maintaining the separation between arguments), and wrap
+# the whole thing up as a single "set" statement.
+#
+# This will of course break if any of these variables contains a newline or
+# an unmatched quote.
+#
+
+eval "set -- $(
+ printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" |
+ xargs -n1 |
+ sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' |
+ tr '\n' ' '
+ )" '"$@"'
+
+exec "$JAVACMD" "$@"
diff --git a/android-native/gradlew.bat b/android-native/gradlew.bat
new file mode 100644
index 00000000..9d21a218
--- /dev/null
+++ b/android-native/gradlew.bat
@@ -0,0 +1,94 @@
+@rem
+@rem Copyright 2015 the original author or authors.
+@rem
+@rem Licensed under the Apache License, Version 2.0 (the "License");
+@rem you may not use this file except in compliance with the License.
+@rem You may obtain a copy of the License at
+@rem
+@rem https://www.apache.org/licenses/LICENSE-2.0
+@rem
+@rem Unless required by applicable law or agreed to in writing, software
+@rem distributed under the License is distributed on an "AS IS" BASIS,
+@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+@rem See the License for the specific language governing permissions and
+@rem limitations under the License.
+@rem
+@rem SPDX-License-Identifier: Apache-2.0
+@rem
+
+@if "%DEBUG%"=="" @echo off
+@rem ##########################################################################
+@rem
+@rem Gradle startup script for Windows
+@rem
+@rem ##########################################################################
+
+@rem Set local scope for the variables with windows NT shell
+if "%OS%"=="Windows_NT" setlocal
+
+set DIRNAME=%~dp0
+if "%DIRNAME%"=="" set DIRNAME=.
+@rem This is normally unused
+set APP_BASE_NAME=%~n0
+set APP_HOME=%DIRNAME%
+
+@rem Resolve any "." and ".." in APP_HOME to make it shorter.
+for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
+
+@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
+set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
+
+@rem Find java.exe
+if defined JAVA_HOME goto findJavaFromJavaHome
+
+set JAVA_EXE=java.exe
+%JAVA_EXE% -version >NUL 2>&1
+if %ERRORLEVEL% equ 0 goto execute
+
+echo. 1>&2
+echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2
+echo. 1>&2
+echo Please set the JAVA_HOME variable in your environment to match the 1>&2
+echo location of your Java installation. 1>&2
+
+goto fail
+
+:findJavaFromJavaHome
+set JAVA_HOME=%JAVA_HOME:"=%
+set JAVA_EXE=%JAVA_HOME%/bin/java.exe
+
+if exist "%JAVA_EXE%" goto execute
+
+echo. 1>&2
+echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2
+echo. 1>&2
+echo Please set the JAVA_HOME variable in your environment to match the 1>&2
+echo location of your Java installation. 1>&2
+
+goto fail
+
+:execute
+@rem Setup the command line
+
+set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
+
+
+@rem Execute Gradle
+"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
+
+:end
+@rem End local scope for the variables with windows NT shell
+if %ERRORLEVEL% equ 0 goto mainEnd
+
+:fail
+rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
+rem the _cmd.exe /c_ return code!
+set EXIT_CODE=%ERRORLEVEL%
+if %EXIT_CODE% equ 0 set EXIT_CODE=1
+if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE%
+exit /b %EXIT_CODE%
+
+:mainEnd
+if "%OS%"=="Windows_NT" endlocal
+
+:omega
diff --git a/android-native/settings.gradle.kts b/android-native/settings.gradle.kts
new file mode 100644
index 00000000..62141b28
--- /dev/null
+++ b/android-native/settings.gradle.kts
@@ -0,0 +1,19 @@
+// The spike shell: a native Kotlin + Jetpack Compose app over the Rust
+// engine (src/crates/concat-mobile). Its own application id, so it installs
+// beside the shipping Slint app and never replaces it.
+pluginManagement {
+ repositories {
+ google()
+ mavenCentral()
+ gradlePluginPortal()
+ }
+}
+dependencyResolutionManagement {
+ repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
+ repositories {
+ google()
+ mavenCentral()
+ }
+}
+rootProject.name = "concat-spike"
+include(":app")
diff --git a/android-native/uniffi-bindgen/Cargo.toml b/android-native/uniffi-bindgen/Cargo.toml
new file mode 100644
index 00000000..2907e685
--- /dev/null
+++ b/android-native/uniffi-bindgen/Cargo.toml
@@ -0,0 +1,13 @@
+[package]
+name = "uniffi-bindgen"
+version = "0.1.0"
+edition = "2024"
+license = "AGPL-3.0-or-later"
+publish = false
+description = "UniFFI's binding generator for the spike shell, at the uniffi concat-mobile links."
+
+# Its own workspace: a host tool, built with none of the engine.
+[workspace]
+
+[dependencies]
+uniffi = { version = "=0.32.2", features = ["cli"] }
diff --git a/android-native/uniffi-bindgen/src/main.rs b/android-native/uniffi-bindgen/src/main.rs
new file mode 100644
index 00000000..b5163e52
--- /dev/null
+++ b/android-native/uniffi-bindgen/src/main.rs
@@ -0,0 +1,8 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+// SPDX-FileCopyrightText: 2026 Jareer Abdullah and Concat contributors
+
+//! UniFFI's binding generator, at the uniffi version concat-mobile links.
+
+fn main() {
+ uniffi::uniffi_bindgen_main()
+}
diff --git a/src/Cargo.lock b/src/Cargo.lock
index 91c6ddc8..096b8b59 100644
--- a/src/Cargo.lock
+++ b/src/Cargo.lock
@@ -396,6 +396,59 @@ dependencies = [
"libloading 0.8.9",
]
+[[package]]
+name = "askama"
+version = "0.16.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6024d73179f43f15ccd2b881bfea6fee7f3a46ec53f33b52210dea749ebebaa4"
+dependencies = [
+ "askama_macros",
+ "itoa",
+ "percent-encoding",
+ "serde",
+ "serde_json",
+]
+
+[[package]]
+name = "askama_derive"
+version = "0.16.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "071ee5ebf2138e3ad180e0aacf6940c2cab5e6d8333741d9925c7bee2b153f39"
+dependencies = [
+ "askama_parser",
+ "basic-toml",
+ "glob",
+ "memchr",
+ "proc-macro2",
+ "quote",
+ "rustc-hash 2.1.3",
+ "serde",
+ "serde_derive",
+ "syn 3.0.4",
+]
+
+[[package]]
+name = "askama_macros"
+version = "0.16.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "643e1c7cbb6aec1d920332fe51a7c0d8219e273dcb8602db03f5263e4d16487b"
+dependencies = [
+ "askama_derive",
+]
+
+[[package]]
+name = "askama_parser"
+version = "0.16.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2c5ae75772275d268b03ab8bdccdd12117b6169ee23256942b34e46c9f476583"
+dependencies = [
+ "rustc-hash 2.1.3",
+ "serde",
+ "serde_derive",
+ "unicode-ident",
+ "winnow",
+]
+
[[package]]
name = "async-broadcast"
version = "0.7.2"
@@ -643,6 +696,15 @@ version = "1.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2af50177e190e07a26ab74f8b1efbfe2ef87da2116221318cb1c2e82baf7de06"
+[[package]]
+name = "basic-toml"
+version = "0.1.10"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ba62675e8242a4c4e806d12f11d136e626e6c8361d6b829310732241652a178a"
+dependencies = [
+ "serde",
+]
+
[[package]]
name = "beef"
version = "0.5.2"
@@ -912,6 +974,39 @@ dependencies = [
"wayland-client",
]
+[[package]]
+name = "camino"
+version = "1.2.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "bbbad30e4b4c14a39e3cc8aed085a12a327257c316619c93581e017bc52be591"
+dependencies = [
+ "serde_core",
+]
+
+[[package]]
+name = "cargo-platform"
+version = "0.3.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "dd0061da739915fae12ea00e16397555ed4371a6bb285431aab930f61b0aa4ba"
+dependencies = [
+ "serde",
+ "serde_core",
+]
+
+[[package]]
+name = "cargo_metadata"
+version = "0.23.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ef987d17b0a113becdd19d3d0022d04d7ef41f9efe4f3fb63ac44ba61df3ade9"
+dependencies = [
+ "camino",
+ "cargo-platform",
+ "semver",
+ "serde",
+ "serde_json",
+ "thiserror 2.0.20",
+]
+
[[package]]
name = "castaway"
version = "0.2.4"
@@ -1290,6 +1385,28 @@ dependencies = [
"thiserror 2.0.20",
]
+[[package]]
+name = "concat-mobile"
+version = "0.2.8"
+dependencies = [
+ "android_logger",
+ "concat-core",
+ "concat-export",
+ "concat-host",
+ "concat-media",
+ "concat-project",
+ "jni-sys 0.3.1",
+ "log",
+ "ndk 0.9.0",
+ "ndk-context",
+ "ndk-sys 0.6.0+11769913",
+ "pollster",
+ "raw-window-handle",
+ "thiserror 2.0.20",
+ "uniffi",
+ "wgpu",
+]
+
[[package]]
name = "concat-project"
version = "0.2.8"
@@ -2303,6 +2420,15 @@ dependencies = [
"percent-encoding",
]
+[[package]]
+name = "fs-err"
+version = "3.3.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b5c95b673b8f6f7235229ae11c5642d81b04c2e64c1e2fb417bc0cf73ca45f29"
+dependencies = [
+ "autocfg",
+]
+
[[package]]
name = "fs_extra"
version = "1.3.0"
@@ -2605,6 +2731,17 @@ dependencies = [
"gl_generator",
]
+[[package]]
+name = "goblin"
+version = "0.8.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1b363a30c165f666402fe6a3024d3bec7ebc898f96a4a23bd1c99f8dbf3f4f47"
+dependencies = [
+ "log",
+ "plain",
+ "scroll",
+]
+
[[package]]
name = "gpu-allocator"
version = "0.28.0"
@@ -3430,6 +3567,8 @@ checksum = "d466e9454f08e4a911e14806c24e16fba1b4c121d1ea474396f396069cf949d9"
dependencies = [
"equivalent",
"hashbrown 0.17.1",
+ "serde",
+ "serde_core",
]
[[package]]
@@ -5870,6 +6009,26 @@ version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
+[[package]]
+name = "scroll"
+version = "0.12.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6ab8598aa408498679922eff7fa985c25d58a90771bd6be794434c5277eab1a6"
+dependencies = [
+ "scroll_derive",
+]
+
+[[package]]
+name = "scroll_derive"
+version = "0.12.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1783eabc414609e28a5ba76aee5ddd52199f7107a0b24c2e9746a1ecc34a683d"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn 2.0.119",
+]
+
[[package]]
name = "sctk-adwaita"
version = "0.10.1"
@@ -5911,6 +6070,10 @@ name = "semver"
version = "1.0.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a7852d02fc848982e0c167ef163aaff9cd91dc640ba85e263cb1ce46fae51cd"
+dependencies = [
+ "serde",
+ "serde_core",
+]
[[package]]
name = "serde"
@@ -6199,6 +6362,12 @@ version = "1.15.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ed6a63f02c8539c91a8685a86f4099661ba3da017932f6ebbea6de3f0fa7c90"
+[[package]]
+name = "smawk"
+version = "0.3.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e8e2fb0f499abb4d162f2bedad68f5ef91a1682b5a03596ddb67efd37768d100"
+
[[package]]
name = "smithay-client-toolkit"
version = "0.19.2"
@@ -6533,6 +6702,16 @@ version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f18aa187839b2bdb1ad2fa35ead8c4c2976b64e4363c386d45ac0f7ee85c9233"
+[[package]]
+name = "textwrap"
+version = "0.16.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6ecfad6c3abc80a577f2b91c1e412ee57e7a060d430b553c1b0c940974ebcd49"
+dependencies = [
+ "smawk",
+ "unicode-width 0.2.2",
+]
+
[[package]]
name = "thiserror"
version = "1.0.69"
@@ -7107,6 +7286,125 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "39ec24b3121d976906ece63c9daad25b85969647682eee313cb5779fdd69e14e"
+[[package]]
+name = "uniffi"
+version = "0.32.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "76407f5f396a2c949a069eff4a9fca9ce462410eb872bffef4c2b7b89804a77a"
+dependencies = [
+ "anyhow",
+ "cargo_metadata",
+ "uniffi_bindgen",
+ "uniffi_core",
+ "uniffi_macros",
+ "uniffi_pipeline",
+]
+
+[[package]]
+name = "uniffi_bindgen"
+version = "0.32.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3df3ced8f0eda3de99f6d50820e8628f443da8e4f447f477d1403ceba29dacbd"
+dependencies = [
+ "anyhow",
+ "askama",
+ "camino",
+ "cargo_metadata",
+ "fs-err",
+ "glob",
+ "goblin",
+ "heck",
+ "indexmap",
+ "once_cell",
+ "serde",
+ "tempfile",
+ "textwrap",
+ "toml",
+ "uniffi_internal_macros",
+ "uniffi_meta",
+ "uniffi_pipeline",
+ "uniffi_udl",
+]
+
+[[package]]
+name = "uniffi_core"
+version = "0.32.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f7530ae8efeaaa488622865966763fe0294832779fff80508c36d69c6a874a6a"
+dependencies = [
+ "anyhow",
+ "bytes",
+ "once_cell",
+ "static_assertions",
+]
+
+[[package]]
+name = "uniffi_internal_macros"
+version = "0.32.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0f4bad017164375d99450d70495014810d15a84ba9da6ed14b9628b1424223ba"
+dependencies = [
+ "anyhow",
+ "indexmap",
+ "proc-macro2",
+ "quote",
+ "syn 2.0.119",
+]
+
+[[package]]
+name = "uniffi_macros"
+version = "0.32.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5b855a58da153739ce6e863f6e296edc3716a093768c4d3d957f17f5e4317c60"
+dependencies = [
+ "camino",
+ "fs-err",
+ "once_cell",
+ "proc-macro2",
+ "quote",
+ "serde",
+ "syn 2.0.119",
+ "toml",
+ "uniffi_meta",
+]
+
+[[package]]
+name = "uniffi_meta"
+version = "0.32.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "af0c88664a9cb9559856f5a250283c9708923b303170e2e5c7db8f4ad65e9459"
+dependencies = [
+ "anyhow",
+ "siphasher",
+ "uniffi_internal_macros",
+ "uniffi_pipeline",
+]
+
+[[package]]
+name = "uniffi_pipeline"
+version = "0.32.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d00f5c044b4a3dca0b1226c92ddaeea626f337470e22180dd182160c87a06b0b"
+dependencies = [
+ "anyhow",
+ "heck",
+ "indexmap",
+ "tempfile",
+ "uniffi_internal_macros",
+]
+
+[[package]]
+name = "uniffi_udl"
+version = "0.32.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "55dfd9c778d6b39cb5acd541d9ff63431974035d2bad83c897b766a6ee3152ac"
+dependencies = [
+ "anyhow",
+ "textwrap",
+ "uniffi_meta",
+ "weedle2",
+]
+
[[package]]
name = "untrusted"
version = "0.9.0"
@@ -7531,6 +7829,15 @@ dependencies = [
"rustls-pki-types",
]
+[[package]]
+name = "weedle2"
+version = "5.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "998d2c24ec099a87daf9467808859f9d82b61f1d9c9701251aea037f514eae0e"
+dependencies = [
+ "nom 7.1.3",
+]
+
[[package]]
name = "weezl"
version = "0.1.12"
diff --git a/src/crates/concat-mobile/Cargo.toml b/src/crates/concat-mobile/Cargo.toml
new file mode 100644
index 00000000..1e4d0aac
--- /dev/null
+++ b/src/crates/concat-mobile/Cargo.toml
@@ -0,0 +1,39 @@
+[package]
+name = "concat-mobile"
+version.workspace = true
+edition.workspace = true
+rust-version.workspace = true
+license.workspace = true
+publish = false
+description = "Spike: the engine behind a native phone shell (Kotlin + Compose), through UniFFI."
+
+# A shared object the Kotlin side loads: JNA for the UniFFI calls, and
+# System.loadLibrary for the JNI entry points in jni.rs. The bindings are
+# generated from the built library by android-native/uniffi-bindgen.
+[lib]
+crate-type = ["cdylib"]
+
+[dependencies]
+concat-core.workspace = true
+concat-host = { workspace = true, features = ["gpu"] }
+concat-project.workspace = true
+concat-export.workspace = true
+concat-media.workspace = true
+uniffi = "=0.32.2"
+thiserror.workspace = true
+log = "0.4"
+wgpu.workspace = true
+pollster.workspace = true
+raw-window-handle = "0.6"
+
+[target.'cfg(target_os = "android")'.dependencies]
+android_logger = "0.15"
+# The Surface's ANativeWindow, and its CPU lock for the fallback path.
+ndk = { version = "0.9", default-features = false, features = ["nativewindow", "rwh_06"] }
+# cpal's AAudio host reads the JavaVM and the app's Context through this.
+ndk-context = "0.1"
+jni-sys = "0.3"
+ndk-sys = { version = "0.6", features = ["nativewindow"] }
+
+[lints]
+workspace = true
diff --git a/src/crates/concat-mobile/src/jni.rs b/src/crates/concat-mobile/src/jni.rs
new file mode 100644
index 00000000..ac6eec8b
--- /dev/null
+++ b/src/crates/concat-mobile/src/jni.rs
@@ -0,0 +1,55 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+// SPDX-FileCopyrightText: 2026 Jareer Abdullah and Concat contributors
+
+//! The two things UniFFI cannot carry: the app's `Context` (which cpal's
+//! AAudio host reads through ndk-context to size its buffers) and a
+//! `Surface` (whose ANativeWindow the render thread draws on). Both are
+//! plain JNI functions on `app.concat.editor.spike.ConcatNative`, which the
+//! Kotlin side reaches after `System.loadLibrary("concat_mobile")`.
+
+use jni_sys::{JNIEnv, jclass, jlong, jobject};
+
+/// `ConcatNative.init(context)`: logcat, and the JavaVM and Context for
+/// ndk-context. Once per process; later calls do nothing.
+#[unsafe(no_mangle)]
+pub unsafe extern "system" fn Java_app_concat_editor_spike_ConcatNative_init(
+ env: *mut JNIEnv,
+ _class: jclass,
+ context: jobject,
+) {
+ static ONCE: std::sync::Once = std::sync::Once::new();
+ ONCE.call_once(|| {
+ android_logger::init_once(
+ android_logger::Config::default()
+ .with_max_level(log::LevelFilter::Info)
+ .with_tag("concat-spike"),
+ );
+ // SAFETY: env is the calling thread's JNIEnv, valid for this call.
+ unsafe {
+ let functions = &**env;
+ let mut vm = std::ptr::null_mut();
+ let get_vm = functions.GetJavaVM.expect("GetJavaVM");
+ if get_vm(env, &mut vm) != 0 || vm.is_null() {
+ log::error!("no JavaVM");
+ return;
+ }
+ let global = functions.NewGlobalRef.expect("NewGlobalRef")(env, context);
+ ndk_context::initialize_android_context(vm.cast(), global.cast());
+ }
+ log::info!("concat-spike {} native side up", env!("CARGO_PKG_VERSION"));
+ });
+}
+
+/// `ConcatNative.windowFromSurface(surface)`: the Surface's ANativeWindow
+/// with one reference acquired, as a long, or 0. `Engine.attachSurface`
+/// takes that reference over.
+#[unsafe(no_mangle)]
+pub unsafe extern "system" fn Java_app_concat_editor_spike_ConcatNative_windowFromSurface(
+ env: *mut JNIEnv,
+ _class: jclass,
+ surface: jobject,
+) -> jlong {
+ // SAFETY: env and surface are this call's own arguments.
+ let window = unsafe { ndk_sys::ANativeWindow_fromSurface(env.cast(), surface.cast()) };
+ window as jlong
+}
diff --git a/src/crates/concat-mobile/src/lib.rs b/src/crates/concat-mobile/src/lib.rs
new file mode 100644
index 00000000..1af58dc8
--- /dev/null
+++ b/src/crates/concat-mobile/src/lib.rs
@@ -0,0 +1,414 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+// SPDX-FileCopyrightText: 2026 Jareer Abdullah and Concat contributors
+
+//! Spike: the engine behind a native phone shell.
+//!
+//! A narrow surface over `concat-host`, exported through UniFFI so a
+//! Kotlin + Jetpack Compose app can drive it: open a project in the app's
+//! storage, import a video onto the timeline, show the frame at an instant
+//! on an Android `Surface`, play and pause with sound, and say how long the
+//! edit is and where the playhead stands.
+//!
+//! Nothing here decides what an edit means. Edits are `concat_project`
+//! commands applied by the host's [`Session`]; pictures come from the
+//! host's [`concat_host::preview::Monitor`]; sound and the clock from
+//! [`concat_host::playback::Playback`]. What this crate adds is the thread
+//! that owns the GPU device and the Surface ([`render`]), and the JNI the
+//! Surface and the app's `Context` arrive through ([`jni`]).
+
+use std::sync::atomic::{AtomicU64, Ordering};
+use std::sync::{Arc, Mutex};
+
+use concat_host::Session;
+use concat_host::playback::{ClipSpec, GainKey, Playback, PlaybackEvents};
+use concat_project::Command;
+
+mod render;
+
+#[cfg(target_os = "android")]
+mod jni;
+
+uniffi::setup_scaffolding!();
+
+/// Everything that can go wrong, as one message the shell shows.
+#[derive(Debug, thiserror::Error, uniffi::Error)]
+pub enum MobileError {
+ /// The engine refused, with its reason. (Not `message`: Kotlin's
+ /// Throwable already has one, and UniFFI's class would clash with it.)
+ #[error("{reason}")]
+ Failed {
+ /// What happened, in words.
+ reason: String,
+ },
+}
+
+impl From<String> for MobileError {
+ fn from(reason: String) -> Self {
+ MobileError::Failed { reason }
+ }
+}
+
+/// One clip on the timeline, as the strip draws it.
+#[derive(Clone, Debug, uniffi::Record)]
+pub struct ClipView {
+ /// The clip's id in the edit.
+ pub id: String,
+ /// The bin item's name.
+ pub name: String,
+ /// Which lane, counted from the top.
+ pub lane: u32,
+ /// Timeline seconds where it starts.
+ pub start: f64,
+ /// Timeline seconds it lasts.
+ pub duration: f64,
+}
+
+/// The timeline's frame and rate.
+#[derive(Clone, Copy, Debug, uniffi::Record)]
+pub struct VideoInfo {
+ /// Width in pixels.
+ pub width: u32,
+ /// Height in pixels.
+ pub height: u32,
+ /// Frames per second.
+ pub fps: f64,
+}
+
+/// What the render thread measured, for the spike's numbers.
+#[derive(Clone, Debug, Default, uniffi::Record)]
+pub struct Stats {
+ /// "gpu: <adapter> (<backend>)" or "cpu", or "" before a surface.
+ pub renderer: String,
+ /// Frames presented since the surface was attached.
+ pub frames: u64,
+ /// Decode and placement of the last frame (frame_sources), ms.
+ pub last_decode_ms: f64,
+ /// Composite and present of the last frame, ms.
+ pub last_draw_ms: f64,
+ /// Frames presented per second over the last second of playback.
+ pub play_fps: f64,
+ /// Frames playback wanted and the render thread could not deliver in
+ /// time (a late frame is skipped, not queued), since play.
+ pub late_frames: u64,
+ /// From a seek() call to its frame presented, the last one, ms.
+ pub last_scrub_ms: f64,
+ /// The same, averaged over every seek since the surface was attached.
+ pub mean_scrub_ms: f64,
+ /// The largest scrub latency seen, ms.
+ pub max_scrub_ms: f64,
+ /// Seeks presented (seeks superseded before drawing are not counted).
+ pub scrubs: u64,
+ /// The loudest sample the mixer played since play, linear (1.0 full
+ /// scale): proof the sound reached the device, short of listening.
+ pub audio_peak: f64,
+ /// The last error the render thread hit, or "".
+ pub last_error: String,
+}
+
+/// The clock playback reports, kept for the shell to read.
+struct Clock {
+ position_bits: AtomicU64,
+}
+
+impl PlaybackEvents for Clock {
+ fn position(&self, seconds: f64) {
+ self.position_bits
+ .store(seconds.to_bits(), Ordering::Relaxed);
+ }
+ fn error(&self, message: String) {
+ log::warn!("playback: {message}");
+ }
+}
+
+/// The engine, as the shell holds it: one open project.
+#[derive(uniffi::Object)]
+pub struct Engine {
+ session: Mutex<Session>,
+ playback: Arc<Playback>,
+ renderer: render::Handle,
+}
+
+#[uniffi::export]
+impl Engine {
+ /// Opens the spike's project under `storage_dir` (the app's files
+ /// folder), creating it the first time. Also points the host's
+ /// directories there, as the Slint activity does.
+ #[uniffi::constructor]
+ pub fn new(storage_dir: String) -> Result<Arc<Self>, MobileError> {
+ let storage = std::path::PathBuf::from(&storage_dir);
+ // SAFETY: the engine's threads do not exist yet; the JVM's do, and
+ // none of them reads the C environment.
+ unsafe {
+ std::env::set_var("XDG_CONFIG_HOME", &storage);
+ std::env::set_var("XDG_DATA_HOME", &storage);
+ std::env::set_var("HOME", &storage);
+ }
+ // As the Slint window's settings pane does on open (panes/settings.rs):
+ // decode on MediaCodec, a file it will not take in software.
+ concat_media::set_hardware_decode(true);
+ let projects = storage.join("projects");
+ std::fs::create_dir_all(&projects)
+ .map_err(|error| format!("could not create {}: {error}", projects.display()))?;
+ let folder = projects.join(concat_host::projects::folder_name("Spike"));
+ let info = if concat_host::projects::is_project(&folder) {
+ concat_host::projects::open(&folder.to_string_lossy())?
+ } else {
+ concat_host::projects::create(&projects.to_string_lossy(), "Spike", 1080, 1920, 30, 1)?
+ };
+ let session = Session::open_info(&info)?;
+ let clock = Arc::new(Clock {
+ position_bits: AtomicU64::new(0f64.to_bits()),
+ });
+ let playback = Playback::start(clock)?;
+ let renderer = render::Handle::start(Arc::clone(&playback));
+ let engine = Arc::new(Engine {
+ session: Mutex::new(session),
+ playback,
+ renderer,
+ });
+ engine.edited();
+ log::info!("spike engine open on {}", info.path);
+ Ok(engine)
+ }
+
+ /// The project folder.
+ pub fn project_path(&self) -> String {
+ self.session().path().to_owned()
+ }
+
+ /// Imports one file and puts it at the end of the timeline. The first
+ /// video imported sets the timeline's frame and rate to its own.
+ pub fn import_video(&self, path: String) -> Result<Vec<ClipView>, MobileError> {
+ let started = std::time::Instant::now();
+ let summary = concat_host::media::probe(&path)?;
+ let probed_ms = started.elapsed().as_secs_f64() * 1000.0;
+ {
+ let mut session = self.session();
+ let first = session.project().active().clips.is_empty();
+ let added = session.apply(Command::AddMedia {
+ item: summary.to_new_media(),
+ })?;
+ let media_id = match added.created_id {
+ Some(id) => id,
+ None => session
+ .project()
+ .media
+ .iter()
+ .find(|item| item.path == path)
+ .map(|item| item.id.clone())
+ .ok_or_else(|| "the bin did not take the file".to_owned())?,
+ };
+ if first && let Some(video) = summary.video.as_ref() {
+ let (rate_num, rate_den) = rate_of(&video.frame_rate_fraction);
+ let mut settings = session.video();
+ settings.width = video.width;
+ settings.height = video.height;
+ settings.rate_num = rate_num;
+ settings.rate_den = rate_den;
+ session.set_video(settings)?;
+ }
+ let end = session.project().active().duration();
+ session.apply(Command::AddClipAtFirstFree {
+ media_id,
+ start: end,
+ })?;
+ session.save(None)?;
+ }
+ log::info!(
+ "imported {path}: probed in {probed_ms:.1} ms, placed in {:.1} ms",
+ started.elapsed().as_secs_f64() * 1000.0
+ );
+ self.edited();
+ Ok(self.clips())
+ }
+
+ /// The clips on the timeline, in time order.
+ pub fn clips(&self) -> Vec<ClipView> {
+ let session = self.session();
+ let timeline = session.project().active();
+ let mut clips: Vec<ClipView> = timeline
+ .clips
+ .iter()
+ .map(|clip| ClipView {
+ id: clip.id.clone(),
+ name: clip.name.clone(),
+ lane: timeline
+ .tracks
+ .iter()
+ .position(|track| track.id == clip.track_id)
+ .unwrap_or(0) as u32,
+ start: clip.start,
+ duration: clip.duration,
+ })
+ .collect();
+ clips.sort_by(|a, b| a.start.total_cmp(&b.start));
+ clips
+ }
+
+ /// The timeline's frame and rate.
+ pub fn video(&self) -> VideoInfo {
+ let video = self.session().video();
+ VideoInfo {
+ width: video.width,
+ height: video.height,
+ fps: video.rate_num as f64 / video.rate_den.max(1) as f64,
+ }
+ }
+
+ /// Where the last clip ends, in seconds.
+ pub fn duration(&self) -> f64 {
+ self.session().project().active().duration()
+ }
+
+ /// The playhead, in seconds: the audio device's clock while playing.
+ pub fn position(&self) -> f64 {
+ self.playback.position_now()
+ }
+
+ /// Whether playback is running.
+ pub fn is_playing(&self) -> bool {
+ self.playback.is_playing()
+ }
+
+ /// Starts playback from the playhead (from the start, at the end).
+ pub fn play(&self) {
+ let duration = self.duration();
+ let mut at = self.playback.position_now();
+ if at >= duration - 0.01 {
+ at = 0.0;
+ }
+ self.playback.play(at);
+ self.renderer.playing(true, duration);
+ }
+
+ /// Stops playback where it is.
+ pub fn pause(&self) {
+ self.playback.pause();
+ self.renderer.playing(false, self.duration());
+ self.renderer.seek(self.playback.position_now());
+ }
+
+ /// Moves the playhead and shows the frame there: a scrub.
+ pub fn seek(&self, seconds: f64) {
+ let seconds = seconds.clamp(0.0, self.duration().max(0.0));
+ self.playback.seek(seconds);
+ self.renderer.seek(seconds);
+ }
+
+ /// Takes the ANativeWindow of the preview's Surface (from
+ /// `ConcatNative.windowFromSurface`, whose reference this now owns) and
+ /// draws on it. `mode` is "vulkan" (wgpu on Vulkan, falling back to
+ /// OpenGL ES), "gl" (wgpu on OpenGL ES) or "cpu" (the CPU compositor
+ /// into the window's locked buffer). Returns which renderer took it.
+ pub fn attach_surface(
+ &self,
+ window: u64,
+ width: u32,
+ height: u32,
+ mode: String,
+ ) -> Result<String, MobileError> {
+ let described = self.renderer.attach(window, width, height, &mode)?;
+ self.renderer.seek(self.playback.position_now());
+ Ok(described)
+ }
+
+ /// Lets the Surface go. Returns once nothing draws on it any more, as
+ /// `surfaceDestroyed` requires.
+ pub fn detach_surface(&self) {
+ self.renderer.detach();
+ }
+
+ /// What the render thread measured.
+ pub fn stats(&self) -> Stats {
+ self.renderer.stats()
+ }
+
+ /// Clears the measured numbers, for a fresh run.
+ pub fn reset_stats(&self) {
+ self.renderer.reset_stats();
+ }
+}
+
+impl Engine {
+ fn session(&self) -> std::sync::MutexGuard<'_, Session> {
+ self.session
+ .lock()
+ .unwrap_or_else(|poisoned| poisoned.into_inner())
+ }
+
+ /// Hands the new edit to playback and the monitor.
+ fn edited(&self) {
+ let session = self.session();
+ let clips = Arc::new(session.flattened_clips());
+ let specs = audio_specs(&clips);
+ self.playback
+ .set_clips(std::path::PathBuf::from(session.path()), specs);
+ let video = session.video();
+ self.renderer
+ .edit(clips, session.settings(), video.color_space);
+ }
+}
+
+/// "30000/1001" as a fraction, 30/1 for anything unreadable.
+fn rate_of(fraction: &str) -> (i64, i64) {
+ let mut parts = fraction.split('/');
+ let num = parts.next().and_then(|part| part.trim().parse::<i64>().ok());
+ let den = parts
+ .next()
+ .map_or(Some(1), |part| part.trim().parse::<i64>().ok());
+ match (num, den) {
+ (Some(num), Some(den)) if num > 0 && den > 0 => (num, den),
+ _ => (30, 1),
+ }
+}
+
+/// The audible clips of a flattened edit, as playback takes them.
+///
+/// A copy of `studio::audio_specs` in the Slint window's crate - which is
+/// exactly the point of the spike's report: this belongs in the host, where
+/// both shells would share it.
+fn audio_specs(clips: &[concat_export::ExportClip]) -> Vec<ClipSpec> {
+ clips
+ .iter()
+ .filter(|clip| {
+ !clip.muted
+ && (clip.kind == concat_export::ClipKind::Audio
+ || (clip.kind == concat_export::ClipKind::Video
+ && clip.has_audio.unwrap_or(true)))
+ })
+ .flat_map(concat_export::audio_pieces)
+ .map(|piece| ClipSpec {
+ path: piece.path.to_string_lossy().into_owned(),
+ audio_stream: piece.stream.map(|index| index as u32),
+ start: piece.start,
+ duration: piece.duration,
+ source_start: piece.source_start,
+ volume: piece.volume as f32,
+ volume_curve: piece
+ .volume_curve
+ .keys()
+ .iter()
+ .map(|key| GainKey {
+ at: key.at,
+ gain: key.value,
+ ease: [key.ease.x1, key.ease.y1, key.ease.x2, key.ease.y2],
+ })
+ .collect(),
+ fade_in: piece.fade_in,
+ fade_out: piece.fade_out,
+ speed: piece.speed,
+ preserve_pitch: piece.preserve_pitch,
+ chain: piece.filter_chain,
+ })
+ .collect()
+}
+
+#[cfg(test)]
+mod tests {
+ #[test]
+ fn a_rate_reads_as_a_fraction() {
+ assert_eq!(super::rate_of("30000/1001"), (30000, 1001));
+ assert_eq!(super::rate_of("25"), (25, 1));
+ assert_eq!(super::rate_of("x"), (30, 1));
+ }
+}
diff --git a/src/crates/concat-mobile/src/render.rs b/src/crates/concat-mobile/src/render.rs
new file mode 100644
index 00000000..5ce2e1a8
--- /dev/null
+++ b/src/crates/concat-mobile/src/render.rs
@@ -0,0 +1,859 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+// SPDX-FileCopyrightText: 2026 Jareer Abdullah and Concat contributors
+
+//! The thread that owns the preview Surface and the GPU device behind it.
+//!
+//! The host's rule is that a monitor frame is decoded anywhere and drawn
+//! only on the thread that owns the device (`Monitor::texture_of`). With no
+//! Slint renderer here, this thread is that owner: it makes the wgpu device
+//! on the Surface's ANativeWindow, hands it to a `Monitor::with_gpu`, and
+//! draws each frame's texture straight onto the Surface - no readback, which
+//! the Slint phone build cannot avoid today (its monitor composites on the
+//! CPU, platform.rs). The CPU path (`Monitor::frame` into a locked
+//! ANativeWindow buffer) is kept beside it for comparison.
+//!
+//! Paused, it sleeps until a seek arrives and draws only the newest one.
+//! Playing, it draws at the timeline's rate from the audio clock, skipping a
+//! frame it is late for rather than queueing it.
+// Off Android there is no Surface, so the drawing half is never reached.
+#![cfg_attr(not(target_os = "android"), allow(dead_code))]
+
+use std::sync::mpsc;
+use std::sync::{Arc, Mutex};
+use std::time::{Duration, Instant};
+
+use concat_export::ExportClip;
+use concat_host::playback::Playback;
+use concat_project::DocumentSettings;
+use concat_project::model::ColorSpace;
+
+use crate::Stats;
+
+enum Msg {
+ Attach {
+ window: u64,
+ width: u32,
+ height: u32,
+ mode: String,
+ reply: mpsc::Sender<Result<String, String>>,
+ },
+ Detach {
+ reply: mpsc::Sender<()>,
+ },
+ Seek {
+ at: f64,
+ sent: Instant,
+ },
+ Edit {
+ clips: Arc<Vec<ExportClip>>,
+ settings: DocumentSettings,
+ color: ColorSpace,
+ },
+ Playing {
+ on: bool,
+ duration: f64,
+ },
+ ResetStats,
+}
+
+/// The edit as the monitor draws it.
+struct Doc {
+ clips: Arc<Vec<ExportClip>>,
+ settings: DocumentSettings,
+ color: ColorSpace,
+}
+
+/// The render thread, as the engine holds it.
+pub struct Handle {
+ tx: Mutex<mpsc::Sender<Msg>>,
+ stats: Arc<Mutex<Stats>>,
+}
+
+impl Handle {
+ /// Starts the thread.
+ pub fn start(playback: Arc<Playback>) -> Handle {
+ let (tx, rx) = mpsc::channel();
+ let stats = Arc::new(Mutex::new(Stats::default()));
+ let shared = Arc::clone(&stats);
+ std::thread::Builder::new()
+ .name("spike-render".into())
+ .spawn(move || run(rx, playback, shared))
+ .expect("could not start the render thread");
+ Handle {
+ tx: Mutex::new(tx),
+ stats,
+ }
+ }
+
+ fn send(&self, msg: Msg) {
+ let _ = self
+ .tx
+ .lock()
+ .unwrap_or_else(|poisoned| poisoned.into_inner())
+ .send(msg);
+ }
+
+ /// Draws on `window` from now on; see `Engine::attach_surface`.
+ pub fn attach(&self, window: u64, width: u32, height: u32, mode: &str) -> Result<String, String> {
+ let (reply, answer) = mpsc::channel();
+ self.send(Msg::Attach {
+ window,
+ width,
+ height,
+ mode: mode.to_owned(),
+ reply,
+ });
+ answer
+ .recv()
+ .unwrap_or_else(|_| Err("the render thread is gone".to_owned()))
+ }
+
+ /// Stops drawing on the window, and returns once it has.
+ pub fn detach(&self) {
+ let (reply, answer) = mpsc::channel();
+ self.send(Msg::Detach { reply });
+ let _ = answer.recv();
+ }
+
+ /// Shows the frame at `at`.
+ pub fn seek(&self, at: f64) {
+ self.send(Msg::Seek {
+ at,
+ sent: Instant::now(),
+ });
+ }
+
+ /// A new edit to draw.
+ pub fn edit(&self, clips: Arc<Vec<ExportClip>>, settings: DocumentSettings, color: ColorSpace) {
+ self.send(Msg::Edit {
+ clips,
+ settings,
+ color,
+ });
+ }
+
+ /// Playback started or stopped.
+ pub fn playing(&self, on: bool, duration: f64) {
+ self.send(Msg::Playing { on, duration });
+ }
+
+ /// The numbers so far.
+ pub fn stats(&self) -> Stats {
+ self.stats
+ .lock()
+ .unwrap_or_else(|poisoned| poisoned.into_inner())
+ .clone()
+ }
+
+ /// Starts the numbers again.
+ pub fn reset_stats(&self) {
+ self.send(Msg::ResetStats);
+ }
+}
+
+fn ms(duration: Duration) -> f64 {
+ duration.as_secs_f64() * 1000.0
+}
+
+/// The frame size for a surface: the timeline's frame fitted inside it,
+/// never larger than the frame itself, even in both directions.
+pub(crate) fn fit(video: (u32, u32), surface: (u32, u32)) -> (u32, u32) {
+ let (vw, vh) = (video.0.max(2) as f64, video.1.max(2) as f64);
+ let (sw, sh) = (surface.0.max(2) as f64, surface.1.max(2) as f64);
+ let scale = (sw / vw).min(sh / vh).min(1.0);
+ let even = |value: f64| ((value.round() as u32) & !1).max(2);
+ (even(vw * scale), even(vh * scale))
+}
+
+fn run(rx: mpsc::Receiver<Msg>, playback: Arc<Playback>, stats: Arc<Mutex<Stats>>) {
+ let mut target: Option<target::Target> = None;
+ let mut doc: Option<Doc> = None;
+ let mut playing = false;
+ let mut duration = 0.0f64;
+ let mut pending: Option<(f64, Instant)> = None;
+ let mut next_frame = Instant::now();
+ let mut window_start = Instant::now();
+ let mut window_frames = 0u32;
+ let mut scrub_total = 0.0f64;
+ let stat = |with: &dyn Fn(&mut Stats)| {
+ with(&mut stats.lock().unwrap_or_else(|poisoned| poisoned.into_inner()));
+ };
+
+ loop {
+ let message = if playing && target.is_some() {
+ let wait = next_frame.saturating_duration_since(Instant::now());
+ match rx.recv_timeout(wait) {
+ Ok(message) => Some(message),
+ Err(mpsc::RecvTimeoutError::Timeout) => None,
+ Err(mpsc::RecvTimeoutError::Disconnected) => return,
+ }
+ } else {
+ match rx.recv() {
+ Ok(message) => Some(message),
+ Err(_) => return,
+ }
+ };
+ let mut messages: Vec<Msg> = message.into_iter().collect();
+ messages.extend(rx.try_iter());
+ for message in messages {
+ match message {
+ Msg::Attach {
+ window,
+ width,
+ height,
+ mode,
+ reply,
+ } => {
+ // A new window replaces the old: drop the old surface
+ // first, the device stays.
+ if let Some(old) = target.take() {
+ old.release_surface();
+ }
+ let made = target::Target::attach(window, width, height, &mode);
+ let answer = made.as_ref().map(|made| made.describe()).map_err(Clone::clone);
+ if let Ok(made) = made {
+ stat(&|stats| {
+ *stats = Stats {
+ renderer: made.describe(),
+ ..Stats::default()
+ }
+ });
+ scrub_total = 0.0;
+ target = Some(made);
+ } else if let Err(error) = &answer {
+ log::error!("could not attach the surface: {error}");
+ stat(&|stats| stats.last_error = error.clone());
+ }
+ let _ = reply.send(answer);
+ }
+ Msg::Detach { reply } => {
+ if let Some(old) = target.take() {
+ old.release_surface();
+ }
+ let _ = reply.send(());
+ }
+ Msg::Seek { at, sent } => {
+ // The newest seek wins; the time it waited counts from
+ // the first one it superseded, as a finger feels it.
+ let since = pending.map_or(sent, |(_, first)| first);
+ pending = Some((at, since));
+ }
+ Msg::Edit {
+ clips,
+ settings,
+ color,
+ } => {
+ duration = clips
+ .iter()
+ .map(|clip| clip.start + clip.duration)
+ .fold(0.0, f64::max);
+ doc = Some(Doc {
+ clips,
+ settings,
+ color,
+ });
+ if pending.is_none() {
+ pending = Some((playback.position_now(), Instant::now()));
+ }
+ }
+ Msg::Playing { on, duration: total } => {
+ playing = on;
+ duration = total;
+ if on {
+ next_frame = Instant::now();
+ window_start = Instant::now();
+ window_frames = 0;
+ stat(&|stats| stats.late_frames = 0);
+ } else if let Some(target) = target.as_ref() {
+ target.monitor().shrink();
+ }
+ }
+ Msg::ResetStats => {
+ scrub_total = 0.0;
+ stat(&|stats| {
+ *stats = Stats {
+ renderer: stats.renderer.clone(),
+ ..Stats::default()
+ }
+ });
+ }
+ }
+ }
+
+ let (Some(drawing), Some(doc)) = (target.as_mut(), doc.as_ref()) else {
+ continue;
+ };
+ let fps = (doc.settings.rate_num as f64 / doc.settings.rate_den.max(1) as f64).max(1.0);
+ let interval = Duration::from_secs_f64(1.0 / fps);
+
+ if playing {
+ let now = Instant::now();
+ if now < next_frame {
+ continue;
+ }
+ let at = playback.position_now();
+ if at >= duration {
+ playback.pause();
+ playing = false;
+ pending = Some((duration, Instant::now()));
+ continue;
+ }
+ let drawn = drawing.draw(doc, at, true);
+ drawing.prefetch(doc, at);
+ let (left, right) = playback.take_levels();
+ let peak = f64::from(left.max(right));
+ stat(&|stats| stats.audio_peak = stats.audio_peak.max(peak));
+ window_frames += 1;
+ let mut late = 0u64;
+ next_frame += interval;
+ let after = Instant::now();
+ if next_frame < after {
+ // Late: the frames missed are skipped, not queued.
+ late = ((after - next_frame).as_secs_f64() / interval.as_secs_f64()).ceil() as u64;
+ next_frame = after + interval;
+ }
+ let elapsed = window_start.elapsed();
+ let window_fps = (elapsed >= Duration::from_secs(1)).then(|| {
+ let rate = window_frames as f64 / elapsed.as_secs_f64();
+ window_start = Instant::now();
+ window_frames = 0;
+ rate
+ });
+ stat(&|stats| {
+ stats.late_frames += late;
+ if let Some(rate) = window_fps {
+ stats.play_fps = rate;
+ }
+ note(stats, &drawn);
+ });
+ } else if let Some((at, since)) = pending.take() {
+ let drawn = drawing.draw(doc, at, false);
+ let waited = ms(since.elapsed());
+ if drawn.is_ok() {
+ scrub_total += waited;
+ }
+ let total = scrub_total;
+ stat(&|stats| {
+ if drawn.is_ok() {
+ stats.scrubs += 1;
+ stats.last_scrub_ms = waited;
+ stats.max_scrub_ms = stats.max_scrub_ms.max(waited);
+ stats.mean_scrub_ms = total / stats.scrubs as f64;
+ }
+ note(stats, &drawn);
+ });
+ }
+ }
+}
+
+/// What one draw took, or why it did not happen.
+pub(crate) type Drawn = Result<(Duration, Duration), String>;
+
+fn note(stats: &mut Stats, drawn: &Drawn) {
+ match drawn {
+ Ok((decode, draw)) => {
+ stats.frames += 1;
+ stats.last_decode_ms = ms(*decode);
+ stats.last_draw_ms = ms(*draw);
+ }
+ Err(error) => {
+ if stats.last_error != *error {
+ log::warn!("frame: {error}");
+ }
+ stats.last_error = error.clone();
+ }
+ }
+}
+
+fn spec(doc: &Doc, at: f64, size: (u32, u32), moving: bool) -> concat_host::preview::FrameSpec {
+ concat_host::preview::FrameSpec {
+ time: at,
+ width: size.0,
+ height: size.1,
+ moving,
+ proxy: false,
+ color_space: doc.color,
+ }
+}
+
+#[cfg(target_os = "android")]
+mod target {
+ use std::ptr::NonNull;
+ use std::sync::Arc;
+ use std::time::Instant;
+
+ use concat_host::preview::Monitor;
+ use ndk::hardware_buffer_format::HardwareBufferFormat;
+ use ndk::native_window::NativeWindow;
+
+ use super::{Doc, Drawn, fit, spec};
+
+ /// The device, kept across surfaces: a phone backgrounded and brought
+ /// back gets a new Surface, not a new GPU.
+ struct Device {
+ instance: wgpu::Instance,
+ adapter: wgpu::Adapter,
+ device: wgpu::Device,
+ queue: wgpu::Queue,
+ monitor: Monitor,
+ backend: wgpu::Backends,
+ }
+
+ /// Kept between attaches on this thread only.
+ static KEPT: std::sync::Mutex<Option<KeptDevice>> = std::sync::Mutex::new(None);
+ struct KeptDevice(Device);
+
+ pub enum Target {
+ Gpu(Box<GpuTarget>),
+ Cpu(CpuTarget),
+ }
+
+ pub struct GpuTarget {
+ gpu: Device,
+ surface: wgpu::Surface<'static>,
+ config: wgpu::SurfaceConfiguration,
+ pipeline: wgpu::RenderPipeline,
+ layout: wgpu::BindGroupLayout,
+ sampler: wgpu::Sampler,
+ described: String,
+ // Last: the surface above must go before the window's reference.
+ _window: NativeWindow,
+ }
+
+ pub struct CpuTarget {
+ window: NativeWindow,
+ monitor: Monitor,
+ geometry: (u32, u32),
+ size: (u32, u32),
+ }
+
+ const BLIT: &str = r#"
+@group(0) @binding(0) var picture: texture_2d<f32>;
+@group(0) @binding(1) var linear_sampler: sampler;
+struct Out { @builtin(position) position: vec4<f32>, @location(0) uv: vec2<f32> };
+@vertex fn vs(@builtin(vertex_index) index: u32) -> Out {
+ var corners = array<vec2<f32>, 3>(vec2(-1.0, -1.0), vec2(3.0, -1.0), vec2(-1.0, 3.0));
+ let at = corners[index];
+ var out: Out;
+ out.position = vec4(at, 0.0, 1.0);
+ out.uv = vec2((at.x + 1.0) * 0.5, 1.0 - (at.y + 1.0) * 0.5);
+ return out;
+}
+@fragment fn fs_plain(in: Out) -> @location(0) vec4<f32> {
+ return textureSample(picture, linear_sampler, in.uv);
+}
+// An sRGB surface encodes on write; the picture is encoded already.
+@fragment fn fs_linear(in: Out) -> @location(0) vec4<f32> {
+ let c = textureSample(picture, linear_sampler, in.uv);
+ return vec4(pow(c.rgb, vec3(2.2)), c.a);
+}
+"#;
+
+ impl Target {
+ pub fn attach(window: u64, width: u32, height: u32, mode: &str) -> Result<Target, String> {
+ let pointer = NonNull::new(window as *mut ndk_sys::ANativeWindow)
+ .ok_or_else(|| "no window".to_owned())?;
+ // SAFETY: the Kotlin side hands over the reference
+ // ANativeWindow_fromSurface acquired; this owns it now.
+ let window = unsafe { NativeWindow::from_ptr(pointer) };
+ match mode {
+ "cpu" => Ok(Target::Cpu(CpuTarget {
+ window,
+ monitor: Monitor::new(),
+ geometry: (0, 0),
+ size: (width, height),
+ })),
+ "gl" => GpuTarget::new(window, width, height, wgpu::Backends::GL)
+ .map(|target| Target::Gpu(Box::new(target))),
+ _ => GpuTarget::new(window.clone(), width, height, wgpu::Backends::VULKAN)
+ .or_else(|error| {
+ log::warn!("Vulkan would not draw on the surface ({error}); trying OpenGL ES");
+ GpuTarget::new(window, width, height, wgpu::Backends::GL)
+ })
+ .map(|target| Target::Gpu(Box::new(target))),
+ }
+ }
+
+ pub fn describe(&self) -> String {
+ match self {
+ Target::Gpu(gpu) => gpu.described.clone(),
+ Target::Cpu(_) => "cpu: Monitor::frame into ANativeWindow_lock".to_owned(),
+ }
+ }
+
+ pub fn monitor(&self) -> &Monitor {
+ match self {
+ Target::Gpu(gpu) => &gpu.gpu.monitor,
+ Target::Cpu(cpu) => &cpu.monitor,
+ }
+ }
+
+ /// Gives the surface up, keeping the device for the next one.
+ pub fn release_surface(self) -> Option<()> {
+ if let Target::Gpu(gpu) = self {
+ let GpuTarget { gpu, surface, .. } = *gpu;
+ drop(surface);
+ *KEPT.lock().unwrap_or_else(|poisoned| poisoned.into_inner()) =
+ Some(KeptDevice(gpu));
+ }
+ Some(())
+ }
+
+ pub fn prefetch(&self, doc: &Doc, at: f64) {
+ let size = match self {
+ Target::Gpu(gpu) => fit(
+ (doc.settings.width, doc.settings.height),
+ (gpu.config.width, gpu.config.height),
+ ),
+ Target::Cpu(cpu) => fit((doc.settings.width, doc.settings.height), cpu.size),
+ };
+ self.monitor().prefetch(
+ Arc::clone(&doc.clips),
+ &doc.settings,
+ spec(doc, at, size, true),
+ 8,
+ );
+ }
+
+ pub fn draw(&mut self, doc: &Doc, at: f64, moving: bool) -> Drawn {
+ match self {
+ Target::Gpu(gpu) => gpu.draw(doc, at, moving),
+ Target::Cpu(cpu) => cpu.draw(doc, at, moving),
+ }
+ }
+ }
+
+ impl GpuTarget {
+ fn new(
+ window: NativeWindow,
+ width: u32,
+ height: u32,
+ backend: wgpu::Backends,
+ ) -> Result<GpuTarget, String> {
+ use raw_window_handle::{
+ AndroidDisplayHandle, AndroidNdkWindowHandle, RawDisplayHandle, RawWindowHandle,
+ };
+ let kept = KEPT
+ .lock()
+ .unwrap_or_else(|poisoned| poisoned.into_inner())
+ .take()
+ .map(|kept| kept.0)
+ .filter(|kept| kept.backend == backend);
+ let instance = match &kept {
+ Some(kept) => kept.instance.clone(),
+ None => wgpu::Instance::new(wgpu::InstanceDescriptor {
+ backends: backend,
+ ..wgpu::InstanceDescriptor::new_without_display_handle()
+ }),
+ };
+ // SAFETY: the window outlives the surface - both live in the
+ // target, the surface dropped first.
+ let surface = unsafe {
+ instance.create_surface_unsafe(wgpu::SurfaceTargetUnsafe::RawHandle {
+ raw_display_handle: Some(RawDisplayHandle::Android(AndroidDisplayHandle::new())),
+ raw_window_handle: RawWindowHandle::AndroidNdk(AndroidNdkWindowHandle::new(
+ window.ptr().cast(),
+ )),
+ })
+ }
+ .map_err(|error| format!("no {backend:?} surface: {error}"))?;
+ let started = Instant::now();
+ let gpu = match kept {
+ Some(kept) if kept.adapter.is_surface_supported(&surface) => kept,
+ _ => {
+ let adapter = pollster::block_on(instance.request_adapter(
+ &wgpu::RequestAdapterOptions {
+ power_preference: wgpu::PowerPreference::HighPerformance,
+ compatible_surface: Some(&surface),
+ ..Default::default()
+ },
+ ))
+ .map_err(|error| format!("no {backend:?} adapter: {error}"))?;
+ let (device, queue) =
+ pollster::block_on(adapter.request_device(&wgpu::DeviceDescriptor {
+ label: Some("concat spike"),
+ required_limits: adapter.limits(),
+ ..Default::default()
+ }))
+ .map_err(|error| format!("no device: {error}"))?;
+ device.on_uncaptured_error(Arc::new(|error: wgpu::Error| {
+ log::error!("GPU error: {error}");
+ }));
+ let monitor = Monitor::with_gpu(device.clone(), queue.clone());
+ Device {
+ instance: instance.clone(),
+ adapter,
+ device,
+ queue,
+ monitor,
+ backend,
+ }
+ }
+ };
+ let info = gpu.adapter.get_info();
+ let capabilities = surface.get_capabilities(&gpu.adapter);
+ let format = capabilities
+ .formats
+ .iter()
+ .copied()
+ .find(|format| !format.is_srgb())
+ .or_else(|| capabilities.formats.first().copied())
+ .ok_or_else(|| "the surface offers no format".to_owned())?;
+ let mut config = surface
+ .get_default_config(&gpu.adapter, width.max(1), height.max(1))
+ .ok_or_else(|| "the adapter cannot draw on this surface".to_owned())?;
+ config.format = format;
+ config.present_mode = wgpu::PresentMode::Fifo;
+ surface.configure(&gpu.device, &config);
+
+ let device = &gpu.device;
+ let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
+ label: Some("spike blit"),
+ source: wgpu::ShaderSource::Wgsl(BLIT.into()),
+ });
+ let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
+ label: Some("spike blit"),
+ entries: &[
+ wgpu::BindGroupLayoutEntry {
+ binding: 0,
+ visibility: wgpu::ShaderStages::FRAGMENT,
+ ty: wgpu::BindingType::Texture {
+ sample_type: wgpu::TextureSampleType::Float { filterable: true },
+ view_dimension: wgpu::TextureViewDimension::D2,
+ multisampled: false,
+ },
+ count: None,
+ },
+ wgpu::BindGroupLayoutEntry {
+ binding: 1,
+ visibility: wgpu::ShaderStages::FRAGMENT,
+ ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
+ count: None,
+ },
+ ],
+ });
+ let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
+ label: Some("spike blit"),
+ bind_group_layouts: &[Some(&layout)],
+ immediate_size: 0,
+ });
+ let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
+ label: Some("spike blit"),
+ layout: Some(&pipeline_layout),
+ vertex: wgpu::VertexState {
+ module: &shader,
+ entry_point: Some("vs"),
+ compilation_options: Default::default(),
+ buffers: &[],
+ },
+ fragment: Some(wgpu::FragmentState {
+ module: &shader,
+ entry_point: Some(if format.is_srgb() { "fs_linear" } else { "fs_plain" }),
+ compilation_options: Default::default(),
+ targets: &[Some(wgpu::ColorTargetState {
+ format,
+ blend: None,
+ write_mask: wgpu::ColorWrites::ALL,
+ })],
+ }),
+ primitive: wgpu::PrimitiveState::default(),
+ depth_stencil: None,
+ multisample: wgpu::MultisampleState::default(),
+ multiview_mask: None,
+ cache: None,
+ });
+ let sampler = device.create_sampler(&wgpu::SamplerDescriptor {
+ label: Some("spike blit"),
+ mag_filter: wgpu::FilterMode::Linear,
+ min_filter: wgpu::FilterMode::Linear,
+ ..Default::default()
+ });
+ let described = format!(
+ "gpu: {} ({:?}, {:?}, {:?}) surface {}x{} up in {:.0} ms",
+ info.name,
+ info.backend,
+ info.device_type,
+ format,
+ config.width,
+ config.height,
+ started.elapsed().as_secs_f64() * 1000.0
+ );
+ log::info!("{described}");
+ Ok(GpuTarget {
+ gpu,
+ surface,
+ config,
+ pipeline,
+ layout,
+ sampler,
+ described,
+ _window: window,
+ })
+ }
+
+ fn draw(&mut self, doc: &Doc, at: f64, moving: bool) -> Drawn {
+ let size = fit(
+ (doc.settings.width, doc.settings.height),
+ (self.config.width, self.config.height),
+ );
+ let spec = spec(doc, at, size, moving);
+ let started = Instant::now();
+ let sources = self
+ .gpu
+ .monitor
+ .frame_sources(Arc::clone(&doc.clips), &doc.settings, spec)?;
+ let decoded = started.elapsed();
+ let drawing = Instant::now();
+ let texture = self.gpu.monitor.texture_of(&sources, spec)?;
+ let frame = match self.surface.get_current_texture() {
+ wgpu::CurrentSurfaceTexture::Success(frame)
+ | wgpu::CurrentSurfaceTexture::Suboptimal(frame) => frame,
+ wgpu::CurrentSurfaceTexture::Outdated | wgpu::CurrentSurfaceTexture::Lost => {
+ self.surface.configure(&self.gpu.device, &self.config);
+ return Err("the surface was outdated; reconfigured".to_owned());
+ }
+ other => return Err(format!("no surface texture: {other:?}")),
+ };
+ let view = frame.texture.create_view(&wgpu::TextureViewDescriptor::default());
+ let picture = texture.create_view(&wgpu::TextureViewDescriptor::default());
+ let bind = self
+ .gpu
+ .device
+ .create_bind_group(&wgpu::BindGroupDescriptor {
+ label: Some("spike blit"),
+ layout: &self.layout,
+ entries: &[
+ wgpu::BindGroupEntry {
+ binding: 0,
+ resource: wgpu::BindingResource::TextureView(&picture),
+ },
+ wgpu::BindGroupEntry {
+ binding: 1,
+ resource: wgpu::BindingResource::Sampler(&self.sampler),
+ },
+ ],
+ });
+ let mut encoder = self
+ .gpu
+ .device
+ .create_command_encoder(&wgpu::CommandEncoderDescriptor {
+ label: Some("spike blit"),
+ });
+ {
+ let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
+ label: Some("spike blit"),
+ color_attachments: &[Some(wgpu::RenderPassColorAttachment {
+ view: &view,
+ depth_slice: None,
+ resolve_target: None,
+ ops: wgpu::Operations {
+ load: wgpu::LoadOp::Clear(wgpu::Color::BLACK),
+ store: wgpu::StoreOp::Store,
+ },
+ })],
+ depth_stencil_attachment: None,
+ timestamp_writes: None,
+ occlusion_query_set: None,
+ multiview_mask: None,
+ });
+ // Letterboxed: the frame's shape, centred in the surface.
+ let (sw, sh) = (self.config.width as f32, self.config.height as f32);
+ let aspect = doc.settings.width.max(1) as f32 / doc.settings.height.max(1) as f32;
+ let (w, h) = if sw / sh > aspect {
+ (sh * aspect, sh)
+ } else {
+ (sw, sw / aspect)
+ };
+ pass.set_viewport((sw - w) / 2.0, (sh - h) / 2.0, w, h, 0.0, 1.0);
+ pass.set_pipeline(&self.pipeline);
+ pass.set_bind_group(0, &bind, &[]);
+ pass.draw(0..3, 0..1);
+ }
+ self.gpu.queue.submit([encoder.finish()]);
+ self.gpu.queue.present(frame);
+ Ok((decoded, drawing.elapsed()))
+ }
+ }
+
+ impl CpuTarget {
+ fn draw(&mut self, doc: &Doc, at: f64, moving: bool) -> Drawn {
+ let size = fit((doc.settings.width, doc.settings.height), self.size);
+ let spec = spec(doc, at, size, moving);
+ let started = Instant::now();
+ let rgba = self
+ .monitor
+ .frame(Arc::clone(&doc.clips), &doc.settings, spec)?;
+ let decoded = started.elapsed();
+ let drawing = Instant::now();
+ if self.geometry != size {
+ self.window
+ .set_buffers_geometry(
+ size.0 as i32,
+ size.1 as i32,
+ Some(HardwareBufferFormat::R8G8B8A8_UNORM),
+ )
+ .map_err(|error| format!("set geometry: {error}"))?;
+ self.geometry = size;
+ }
+ let mut buffer = self
+ .window
+ .lock(None)
+ .map_err(|error| format!("lock: {error}"))?;
+ let (width, height, stride) = (buffer.width(), buffer.height(), buffer.stride());
+ let row = (size.0 as usize).min(width) * 4;
+ let bits = buffer.bits().cast::<u8>();
+ for y in 0..(size.1 as usize).min(height) {
+ let from = &rgba[y * size.0 as usize * 4..][..row];
+ // SAFETY: the locked buffer is stride * height pixels of
+ // four bytes; `row` is at most `width` pixels of a line.
+ unsafe {
+ std::ptr::copy_nonoverlapping(from.as_ptr(), bits.add(y * stride * 4), row);
+ }
+ }
+ drop(buffer);
+ Ok((decoded, drawing.elapsed()))
+ }
+ }
+}
+
+/// Off Android there is no Surface to draw on; the engine still opens,
+/// imports and plays, which is what the host tests exercise.
+#[cfg(not(target_os = "android"))]
+mod target {
+ use concat_host::preview::Monitor;
+
+ use super::{Doc, Drawn};
+
+ pub enum Target {}
+
+ impl Target {
+ pub fn attach(_: u64, _: u32, _: u32, _: &str) -> Result<Target, String> {
+ Err("there is no Surface to draw on off Android".to_owned())
+ }
+ pub fn describe(&self) -> String {
+ match *self {}
+ }
+ pub fn monitor(&self) -> &Monitor {
+ match *self {}
+ }
+ pub fn release_surface(self) -> Option<()> {
+ match self {}
+ }
+ pub fn prefetch(&self, _: &Doc, _: f64) {
+ match *self {}
+ }
+ pub fn draw(&mut self, _: &Doc, _: f64, _: bool) -> Drawn {
+ match *self {}
+ }
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ #[test]
+ fn a_frame_fits_its_surface_without_growing() {
+ assert_eq!(super::fit((1920, 1080), (1080, 2000)), (1080, 608));
+ assert_eq!(super::fit((1080, 1920), (1080, 1200)), (674, 1200));
+ assert_eq!(super::fit((640, 360), (1080, 2000)), (640, 360));
+ }
+}