From 3076dc4a69040f837864adb2024e49c022e250b5 Mon Sep 17 00:00:00 2001 From: shenlei Date: Wed, 14 Oct 2026 16:54:51 +0900 Subject: [PATCH] Android: full frame rate after returning from background; no black blocks at the top on entering a run - nvn_vk.c: present-link tick period estimated over a 60-tick window minimum and reset on refresh-rate change, so frame pacing no longer locks to ~20 fps after resume. Swapchain acquire barrier srcStage is TRANSFER (spec fix). - GameActivity.java: HUD overlay without a hardware layer; compositing that offscreen layer on Adreno produced stepped black blocks at the top of the screen during the first ~18 s in game. Co-Authored-By: Claude Opus 5.5 --- .../java/org/d2rmobile/app/GameActivity.java | 5 ++-- dm/host/android/nvn_vk.c | 23 ++++++++++++++++--- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/dm/host/android/app/java/org/d2rmobile/app/GameActivity.java b/dm/host/android/app/java/org/d2rmobile/app/GameActivity.java index 43e38a2..c05ed2a 100644 --- a/dm/host/android/app/java/org/d2rmobile/app/GameActivity.java +++ b/dm/host/android/app/java/org/d2rmobile/app/GameActivity.java @@ -88,8 +88,9 @@ public final class GameActivity extends Activity implements SurfaceHolder.Callba view = new SurfaceView(this); view.getHolder().addCallback(this); view.setFocusable(true); view.setFocusableInTouchMode(true); - // 主体层缓存成 GPU 纹理:手指在摇杆上动时只重画摇杆层(见 touch_app.c op_list) | the body layer is cached as a GPU texture: while a finger moves on the stick only the joystick layer is redrawn (see touch_app.c op_list) - overlay = new TouchOverlay(this, 0); overlay.setLayerType(View.LAYER_TYPE_HARDWARE, null); + // 主体层与摇杆层分开:手指在摇杆上动时只重画摇杆层(见 touch_app.c op_list)。主体层不设 LAYER_TYPE_HARDWARE:在 Adreno 上那块离屏层刚进游戏的十几秒里会把屏幕顶部合成成台阶状黑块 + // the body and joystick layers are separate: while a finger moves on the stick only the joystick layer is redrawn (see touch_app.c op_list). No LAYER_TYPE_HARDWARE on the body layer: on Adreno that offscreen layer composited stepped black blocks at the top of the screen for the first dozen seconds in game + overlay = new TouchOverlay(this, 0); joy = new TouchOverlay(this, 1); goals = new TouchOverlay(this, 2); // 小地图任务终点:跟着玩家走每帧变,单独一层不牵动主体层 | minimap quest goals: change every frame as the player walks, own layer so the body layer is untouched FrameLayout root = new FrameLayout(this); diff --git a/dm/host/android/nvn_vk.c b/dm/host/android/nvn_vk.c index 3def89a..7d3f2cc 100644 --- a/dm/host/android/nvn_vk.c +++ b/dm/host/android/nvn_vk.c @@ -2547,7 +2547,9 @@ uint64_t mtl_present(const tex_t *source,double period,const int crop[4]) { VkImageMemoryBarrier barrier={.sType=VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,.oldLayout=G.swap_layouts[index], .newLayout=VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,.srcQueueFamilyIndex=VK_QUEUE_FAMILY_IGNORED,.dstQueueFamilyIndex=VK_QUEUE_FAMILY_IGNORED, .dstAccessMask=VK_ACCESS_TRANSFER_WRITE_BIT,.image=G.swap_images[index],.subresourceRange={VK_IMAGE_ASPECT_COLOR_BIT,0,1,0,1}}; - vkCmdPipelineBarrier(G.batch->command,VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,VK_PIPELINE_STAGE_TRANSFER_BIT,0,0,NULL,0,NULL,1,&barrier); + /* 源阶段须与取图信号量的等待阶段(TRANSFER)相同,布局转换才排在信号量之后;用 TOP_OF_PIPE 时转换可能抢在合成器读完上一帧前执行,Adreno 上 UBWC 元数据被改,上屏出现台阶状黑块 + * The source stage must match the acquire semaphore's wait stage (TRANSFER) so the layout transition is ordered after it; with TOP_OF_PIPE the transition could run while the compositor still reads the previous frame, and on Adreno the rewritten UBWC metadata showed as stepped black blocks on screen */ + vkCmdPipelineBarrier(G.batch->command,VK_PIPELINE_STAGE_TRANSFER_BIT,VK_PIPELINE_STAGE_TRANSFER_BIT,0,0,NULL,0,NULL,1,&barrier); int x=0,y=0,w=t->width,h=t->height;if(crop && crop[2]>0 && crop[3]>0){x=crop[0];y=crop[1];w=crop[2];h=crop[3];} if(x<0||y<0||w<=0||h<=0||(uint64_t)x+w>t->width||(uint64_t)y+h>t->height)die("present crop bounds",w); VkImageBlit blit={.srcSubresource={VK_IMAGE_ASPECT_COLOR_BIT,0,0,1},.srcOffsets={{x,y,0},{x+w,y+h,1}}, @@ -2683,18 +2685,33 @@ static pthread_mutex_t link_mu=PTHREAD_MUTEX_INITIALIZER;static pthread_cond_t l static uint64_t link_tick_n,link_last_ns,link_vsync_ns; /* 节拍号 / 上一拍时刻 / 实测节拍周期 | tick number / last tick time / measured tick period */ static int link_state; /* 0 未起,1 节拍线程在跑,-1 关 / 起不来 | 0 not started, 1 tick thread running, -1 off / failed */ static void link_frame(int64_t frame_ns,void *arg) { + static uint64_t win_min,win_n; /* 近 60 拍间隔的最小值 | minimum interval over the last 60 ticks */ pthread_mutex_lock(&link_mu); uint64_t t=(uint64_t)frame_ns,d=link_last_ns && t>link_last_ns?t-link_last_ns:0; - /* 周期取近况的下沿:漏回调的两拍间隔不算;换刷新率(120→60)时 1.5 倍以内的变长才慢慢跟上 | the period tracks the lower edge: a gap from a missed callback does not count; a longer period after a refresh switch (120->60) within 1.5x is followed */ - if(d>2000000 && d<100000000){if(!link_vsync_ns || d60, e.g. the launcher boosting to 120 Hz while the app is in the background, then back to 60) + * is replaced wholesale by the minimum interval of a 60-tick window. The old code only followed growth within 1.5x, so 120->60 stayed at 8.3 ms, 60 fps counted as 2 ticks a frame and dropped to the low 20s. The system refresh-rate callback (link_rate) also clears it for a re-measure */ + if(d>5000000 && d<100000000){if(!link_vsync_ns || d=60){if(win_min>link_vsync_ns*5/4){fprintf(stderr,"[nvn-vulkan] PRESENT_LINK 节拍周期 %.2f → %.2f ms\n",link_vsync_ns/1e6,win_min/1e6);link_vsync_ns=win_min;}win_min=win_n=0;}} link_last_ns=t;link_tick_n++;pthread_cond_broadcast(&link_cv);pthread_mutex_unlock(&link_mu); AChoreographer_postFrameCallback64(AChoreographer_getInstance(),link_frame,arg); } +/* 显示换刷新率(Choreographer 线程上回调):只清掉估计、从下一拍起重测——屏幕周期不一定是本应用收到的节拍(系统可按应用帧率限流成 60 Hz 回调) + * Display refresh rate changed (called on the Choreographer thread): only clear the estimate and re-measure from the next tick -- the panel period is not necessarily the tick this app receives (the system may throttle callbacks to the app's frame rate) */ +static void link_rate(int64_t period_ns,void *arg) { + (void)arg;pthread_mutex_lock(&link_mu); + fprintf(stderr,"[nvn-vulkan] PRESENT_LINK 刷新率 %.1f Hz,节拍周期重测(原 %.2f ms)\n",period_ns>0?1e9/period_ns:0.0,link_vsync_ns/1e6); + link_vsync_ns=0;pthread_mutex_unlock(&link_mu); +} static void *link_main(void *arg) { (void)arg;pthread_setname_np("nvn-present-link"); ALooper_prepare(0);AChoreographer *c=AChoreographer_getInstance(); if(!c){pthread_mutex_lock(&link_mu);link_state=-1;pthread_mutex_unlock(&link_mu);fprintf(stderr,"[nvn-vulkan] PRESENT_LINK no choreographer\n");return NULL;} AChoreographer_postFrameCallback64(c,link_frame,NULL); + { void (*reg)(AChoreographer *,void (*)(int64_t,void *),void *)=(void (*)(AChoreographer *,void (*)(int64_t,void *),void *))dlsym(RTLD_DEFAULT,"AChoreographer_registerRefreshRateCallback"); /* API 30+ */ + if(reg)reg(c,link_rate,NULL); } for(;;)ALooper_pollOnce(-1,NULL,NULL,NULL); } static int link_on(void) {