mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-19 21:06:09 +00:00
fix: time the HUD graph from the game's frames as shown on screen
This commit is contained in:
1 parent
eaaecf0661
commit
1f60e57749
6 files changed
+110
-30
No files matched your search
@@ -634,6 +634,49 @@ static struct surface *g_hud_surface, *g_hud_shm;
|
||||
static int64_t g_hud_shm_ns;
|
||||
static int64_t g_hud_last_ns, g_hud_alive_ns;
|
||||
static int g_hud_fresh;
|
||||
static int g_hud_new;
|
||||
#define HUD_TIMES 256
|
||||
static pthread_mutex_t g_hud_times_lock = PTHREAD_MUTEX_INITIALIZER;
|
||||
static float g_hud_times[HUD_TIMES];
|
||||
static int64_t g_hud_times_total, g_hud_shown_ns;
|
||||
|
||||
static void hud_frame_shown(int64_t t) {
|
||||
const int64_t previous = g_hud_shown_ns;
|
||||
g_hud_shown_ns = t;
|
||||
if (!previous || t <= previous) return;
|
||||
pthread_mutex_lock(&g_hud_times_lock);
|
||||
g_hud_times[g_hud_times_total % HUD_TIMES] = (float)((t - previous) / 1e6);
|
||||
g_hud_times_total++;
|
||||
pthread_mutex_unlock(&g_hud_times_lock);
|
||||
}
|
||||
|
||||
static int inside_surface(const struct surface *s, const struct surface *ancestor) {
|
||||
int depth = 0;
|
||||
for (s = s->parent; s && depth++ < 8; s = s->parent)
|
||||
if (s == ancestor) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int below_surface(const struct surface *s, const struct surface *other) {
|
||||
if (inside_surface(other, s)) return 1;
|
||||
if (!s->parent || s->parent != other->parent) return 0;
|
||||
const struct surface *child;
|
||||
wl_list_for_each(child, &s->parent->children, child_link) {
|
||||
if (child == s) return 1;
|
||||
if (child == other) return 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int droiddeck_hud_frame_times(float *out, int len, int64_t *total) {
|
||||
pthread_mutex_lock(&g_hud_times_lock);
|
||||
*total = g_hud_times_total;
|
||||
int64_t n = *total < len ? *total : len;
|
||||
if (n > HUD_TIMES) n = HUD_TIMES;
|
||||
for (int64_t i = 0; i < n; i++) out[i] = g_hud_times[(*total - n + i) % HUD_TIMES];
|
||||
pthread_mutex_unlock(&g_hud_times_lock);
|
||||
return (int)n;
|
||||
}
|
||||
static uint64_t g_game_frames;
|
||||
extern void droiddeck_on_game_surface(const char *window, const char *gpu);
|
||||
extern void droiddeck_on_game_frame(void);
|
||||
@@ -684,6 +727,7 @@ static void take_shm(struct surface *s, struct wl_shm_buffer *shm, struct wl_res
|
||||
if (top->role == ROLE_TOPLEVEL && top->mapped && shown * 2 >= (int64_t)scene_w * scene_h) {
|
||||
g_hud_shm = s;
|
||||
g_hud_shm_ns = now;
|
||||
g_hud_new = 1;
|
||||
droiddeck_on_game_frame();
|
||||
}
|
||||
}
|
||||
@@ -757,8 +801,9 @@ static void take_dmabuf(struct surface *s, struct dmabuf_buffer *b, struct wl_re
|
||||
name, b->width, b->height, (unsigned long long)b->modifier);
|
||||
}
|
||||
if (s != g_hud_surface && (b->img || ahb_swapchain_has_ahb(b)) &&
|
||||
(!g_hud_surface || now_ns() - g_hud_alive_ns > 1000000000LL ||
|
||||
(announced_now && (int64_t)b->width * b->height >= (int64_t)g_hud_surface->buf_w * g_hud_surface->buf_h))) {
|
||||
(!g_hud_surface || now_ns() - g_hud_alive_ns > 1000000000LL || (!repeat && below_surface(s, g_hud_surface)) ||
|
||||
(announced_now && !below_surface(g_hud_surface, s) &&
|
||||
(int64_t)b->width * b->height >= (int64_t)g_hud_surface->buf_w * g_hud_surface->buf_h))) {
|
||||
char name[160];
|
||||
describe(s, name, sizeof(name));
|
||||
g_hud_surface = s;
|
||||
@@ -782,7 +827,7 @@ static void take_dmabuf(struct surface *s, struct dmabuf_buffer *b, struct wl_re
|
||||
}
|
||||
if (s == g_hud_surface) {
|
||||
g_hud_alive_ns = now_ns();
|
||||
if (!repeat) { g_hud_last_ns = g_hud_alive_ns; g_hud_fresh = 1; g_game_frames++; droiddeck_on_game_frame(); }
|
||||
if (!repeat) { g_hud_last_ns = g_hud_alive_ns; g_hud_fresh = 1; g_hud_new = 1; g_game_frames++; droiddeck_on_game_frame(); }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2180,6 +2225,10 @@ static void render_scene(void) {
|
||||
if (rendered) {
|
||||
int64_t t = presented_ns(t_scene);
|
||||
g_stat_frames++;
|
||||
if (g_hud_new) {
|
||||
g_hud_new = 0;
|
||||
hud_frame_shown(t);
|
||||
}
|
||||
wl_list_for_each(s, &g_surfaces, link) {
|
||||
if (s->drawn) feedback_present_all(&s->feedback, t);
|
||||
else feedback_discard_all(&s->feedback);
|
||||
|
||||
@@ -25,6 +25,7 @@ extern void droiddeck_wayland_send_pointer(int action, int x, int y);
|
||||
extern void droiddeck_wayland_send_touch(int action, int pointer_id, int x, int y);
|
||||
extern int droiddeck_cursor_snapshot(int *out, int cap);
|
||||
extern int droiddeck_cursor_serial(void);
|
||||
extern int droiddeck_hud_frame_times(float *out, int len, int64_t *total);
|
||||
extern void droiddeck_wayland_send_key(int evdev, int state);
|
||||
extern void droiddeck_wayland_send_scene_input(int type, int a, int b);
|
||||
extern void droiddeck_wayland_vsync(int64_t frame_time_ns);
|
||||
@@ -268,6 +269,20 @@ Java_com_droiddeck_launcher_wayland_WaylandCompositor_nativeCursorSnapshot(
|
||||
return n;
|
||||
}
|
||||
|
||||
JNIEXPORT jlong JNICALL
|
||||
Java_com_droiddeck_launcher_wayland_WaylandCompositor_nativeHudFrameTimes(JNIEnv *env, jclass clazz, jfloatArray out) {
|
||||
(void)clazz;
|
||||
if (!out) return 0;
|
||||
jint cap = (*env)->GetArrayLength(env, out);
|
||||
float *buf = cap > 0 ? malloc(sizeof(float) * (size_t)cap) : NULL;
|
||||
if (!buf) return 0;
|
||||
int64_t total = 0;
|
||||
int n = droiddeck_hud_frame_times(buf, cap, &total);
|
||||
if (n > 0) (*env)->SetFloatArrayRegion(env, out, 0, n, buf);
|
||||
free(buf);
|
||||
return (jlong)total;
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_com_droiddeck_launcher_wayland_WaylandCompositor_nativeCursorSerial(JNIEnv *env, jclass clazz) {
|
||||
(void)env; (void)clazz;
|
||||
|
||||
@@ -18,7 +18,6 @@ import com.droiddeck.launcher.wayland.WaylandCompositor
|
||||
import java.io.File
|
||||
import java.util.Locale
|
||||
import java.util.concurrent.atomic.AtomicInteger
|
||||
import java.util.concurrent.atomic.AtomicLong
|
||||
|
||||
class PerfHud(context: Context) {
|
||||
var text by mutableStateOf("")
|
||||
@@ -32,9 +31,9 @@ class PerfHud(context: Context) {
|
||||
private var lastFps = 0f
|
||||
private var running = false
|
||||
private val handler = Handler(Looper.getMainLooper())
|
||||
private val intervals = FloatArray(GRAPH)
|
||||
private val graphWrite = AtomicInteger()
|
||||
private val lastFrameNs = AtomicLong()
|
||||
private val samples = FloatArray(GRAPH)
|
||||
private var graphBase = 0L
|
||||
private var graphSeen = 0L
|
||||
|
||||
@Volatile private var showFps = true
|
||||
@Volatile private var showBattery = true
|
||||
@@ -59,14 +58,6 @@ class PerfHud(context: Context) {
|
||||
}
|
||||
override fun onGameFrame() {
|
||||
frames.incrementAndGet()
|
||||
if (!showGraph) return
|
||||
val now = System.nanoTime()
|
||||
val previous = lastFrameNs.getAndSet(now)
|
||||
if (previous == 0L) return
|
||||
val ms = ((now - previous) / 1_000_000f).coerceIn(0f, 50f)
|
||||
val slot = graphWrite.get()
|
||||
intervals[slot and (GRAPH - 1)] = ms
|
||||
graphWrite.compareAndSet(slot, slot + 1)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -100,9 +91,9 @@ class PerfHud(context: Context) {
|
||||
if (!enabled || !anyPart || running) return
|
||||
running = true
|
||||
lastTick = SystemClock.elapsedRealtime()
|
||||
lastFrameNs.set(0L)
|
||||
frames.set(0)
|
||||
graphWrite.set(0)
|
||||
graphBase = shownTotal()
|
||||
graphSeen = graphBase
|
||||
WaylandCompositor.setGameListener(listener)
|
||||
handler.postDelayed(tick, TICK_MS)
|
||||
}
|
||||
@@ -135,23 +126,19 @@ class PerfHud(context: Context) {
|
||||
return sticky.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, 0)
|
||||
}
|
||||
|
||||
private fun shownTotal(): Long = try { WaylandCompositor.nativeHudFrameTimes(samples) } catch (t: Throwable) { 0L }
|
||||
|
||||
private fun clearGraph() {
|
||||
lastFrameNs.set(0L)
|
||||
graphWrite.set(0)
|
||||
graphBase = shownTotal()
|
||||
graphSeen = graphBase
|
||||
if (graph.isNotEmpty()) graph = FloatArray(0)
|
||||
}
|
||||
|
||||
private fun publishGraph() {
|
||||
val count = graphWrite.get()
|
||||
val shown = minOf(count, GRAPH)
|
||||
if (shown < 2) {
|
||||
graph = FloatArray(0)
|
||||
return
|
||||
}
|
||||
val out = FloatArray(shown)
|
||||
val start = count - shown
|
||||
for (i in 0 until shown) out[i] = intervals[(start + i) and (GRAPH - 1)]
|
||||
graph = out
|
||||
val total = shownTotal()
|
||||
if (total == graphSeen && graph.isNotEmpty()) return
|
||||
graphSeen = total
|
||||
graph = recent(samples, total, graphBase)
|
||||
}
|
||||
|
||||
private fun line(base: Float): String {
|
||||
@@ -176,6 +163,14 @@ class PerfHud(context: Context) {
|
||||
companion object {
|
||||
private const val TICK_MS = 1_000L
|
||||
private const val GRAPH = 128
|
||||
internal const val GRAPH_MAX_MS = 50f
|
||||
|
||||
internal fun recent(samples: FloatArray, total: Long, base: Long): FloatArray {
|
||||
val filled = minOf(total, samples.size.toLong()).toInt()
|
||||
val shown = minOf(filled.toLong(), (total - base).coerceAtLeast(0L)).toInt()
|
||||
if (shown < 2) return FloatArray(0)
|
||||
return FloatArray(shown) { samples[filled - shown + it].coerceIn(0f, GRAPH_MAX_MS) }
|
||||
}
|
||||
|
||||
internal fun batteryCelsius(deci: Int): String? =
|
||||
if (deci <= 0) null else String.format(Locale.US, "%.1f°C", deci / 10f)
|
||||
|
||||
@@ -18,6 +18,7 @@ import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.semantics.Role
|
||||
import com.droiddeck.launcher.frontend.Library
|
||||
import com.droiddeck.launcher.session.LoadingState
|
||||
import com.droiddeck.launcher.session.PerfHud
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import androidx.compose.animation.core.FastOutSlowInEasing
|
||||
@@ -116,7 +117,7 @@ fun HudText(text: String, graph: FloatArray = FloatArray(0), color: String = "mi
|
||||
@Composable
|
||||
private fun HudGraph(samples: FloatArray, accent: Color, wash: Float) {
|
||||
Canvas(Modifier.width(148.dp).height(44.dp)) {
|
||||
val maxMs = (samples.maxOrNull() ?: 16.7f).coerceAtLeast(16.7f)
|
||||
val maxMs = PerfHud.GRAPH_MAX_MS
|
||||
val step = size.width / (samples.size - 1)
|
||||
fun yOf(ms: Float) = size.height * (1f - (ms / maxMs).coerceIn(0f, 1f))
|
||||
val fillAlpha = wash * 0.5f
|
||||
|
||||
@@ -307,6 +307,8 @@ public final class WaylandCompositor {
|
||||
|
||||
public static native float[] nativeFrameGenStats();
|
||||
|
||||
public static native long nativeHudFrameTimes(float[] out);
|
||||
|
||||
/** Stream mode: the scene is the Xbox Cloud Gaming native player's picture, handed over frame by frame. */
|
||||
public static native void nativeStreamMode(boolean on);
|
||||
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.droiddeck.launcher.session
|
||||
|
||||
import org.junit.Assert.assertArrayEquals
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Test
|
||||
|
||||
class PerfHudGraphTest {
|
||||
@Test fun theGraphShowsOnlyFramesFromThisHudRun() {
|
||||
val samples = floatArrayOf(9f, 9f, 9f, 9f, 9f, 16.7f, 33.4f, 16.6f)
|
||||
assertArrayEquals(floatArrayOf(16.7f, 33.4f, 16.6f), PerfHud.recent(samples, 105, 102), 0f)
|
||||
assertEquals(0, PerfHud.recent(samples, 103, 102).size)
|
||||
}
|
||||
|
||||
@Test fun anEarlySessionShowsWhatWasShownSoFar() {
|
||||
val samples = floatArrayOf(16.6f, 16.7f, 120f, 0f, 0f, 0f)
|
||||
assertArrayEquals(floatArrayOf(16.6f, 16.7f, 50f), PerfHud.recent(samples, 3, 0), 0f)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user