Repository navigation
截图按帧序号协议判断整帧,外壳不再交出撕裂帧 - #37
Merged
Merged
Conversation
模拟器原地往共享内存写像素,旧的判断(复制前后帧号相等就算完整)漏掉一种情况:复制开始时模拟器已经在写下一帧、帧号还没变,复制又在写完前结束,撕裂的帧照样通过。 协议:新模拟器把帧头 fps 的最高位(0x80000000)置上,表示按序号写帧号:写像素前加 1(奇数 = 正在写),写完像素和 ts_us 后再加 1(偶数 = 完整)。帧头布局不变,帧号每帧跳 2,可能回绕。 - mx_copy_frame():带这一位时,序号是奇数就让出 CPU 等它变偶数(每次截图从第一次看到奇数起最多等 MX_SEQ_WAIT_MS = 50 ms,超时算失败);读序号后、读回序号前各放一道 acquire 屏障(x86 上是编译器屏障,ARM64 上是 dmb),ts_us 在第二道屏障之前读;前后序号相等才算成功。交给调用方的帧号是序号 / 2,外面看到的仍是每帧加 1。重试次数仍是 3,用完 modavd_capture() 照旧返回 MODAVD_E_NOT_READY。 - 不带这一位(旧模拟器):沿用原来的前后比较,奇数帧号不代表正在写,不等待。 - 外壳 external_renderer_ipc.dll:收到 MX_TORN 不再当成功交出,再试最多 10 轮,还不行就按无帧返回 -2,并在 avdshim.log 记一行。 - open、connect 日志里的 fps 去掉标志位,带这一位时行里标 seqlock。 兼容性:新库配旧模拟器,截图行为不变(只有外壳在一直撕裂时改为返回失败);新库配新模拟器,撕裂帧被挡住;旧库配新模拟器,截图照旧能用,帧号每帧跳 2、日志里 fps 是个很大的数,撕裂照旧可能漏过。 测试:假共享内存加上新模拟器模式。单线程用例覆盖序号一直是奇数时等到超时失败、复制中途写了一帧或开始写下一帧时重试、序号回绕、旧模式奇数帧号不等待、日志里 fps 不带标志位;多线程压力用例一个线程按序号不停写帧、本线程连续截 5000 次,断言每次成功的帧所有像素一致、帧号和时间戳对得上;外壳在帧一直写不完时返回 -2、不动缓冲区。 Refs ModAVD/ModAVD#431
This was referenced Dec 4, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
改了什么 / What changed
现象:截图偶尔拿到撕裂帧,上下两部分来自不同画面。模拟器原地往共享内存写像素,库原来的判断是「复制前后帧号相等就算完整」;旧模拟器写完像素才把帧号加 1,所以复制开始时模拟器已经在写下一帧(帧号还没变)、复制又在它写完前结束时,前后帧号相等,撕裂帧照样通过。MuMu 形状的外壳
external_renderer_ipc.dll还会把连续三次都对不上的那份拷贝当成功交给 MaaCore。协议(模拟器那边同步改,帧头布局不变):新模拟器把帧头
fps的最高位(0x80000000)置上,表示帧号按序号写:写像素前加 1(奇数 = 正在写),写完像素和ts_us后再加 1(偶数 = 完整)。帧号每帧跳 2,可能回绕。库(
src/core.c的mx_copy_frame()):MX_SEQ_WAIT_MS;超时说明模拟器停在半帧,这次截图失败);偶数就复制,读回ts_us,再读序号,前后相等才算成功。读序号之后、读回序号之前各有一道 acquire 屏障(x86 上是编译器屏障,ARM64 上是dmb)。交给调用方的帧号是序号除以 2,外面看到的帧号仍是每帧加 1。modavd_capture()照旧返回MODAVD_E_NOT_READY,调用方下一次再来。seqlock,看日志就知道配的是新模拟器。外壳(
shim/mumu_shim.c):收到MX_TORN不再当成功交出,再试最多 10 轮,还对不上就按「没有帧」返回 -2(和其他失败分支一致),并在avdshim.log记一行。连接时只问尺寸的调用不受影响。兼容:
要真正挡住撕裂,需要配合模拟器那边的补丁,随下一次模拟器重编一起发。
怎么验证的 / How it was verified
只用假共享内存(
tests/fake_frame.h),没有连真实模拟器。pwsh -NoProfile -File .\build.ps1,ctest 3 条全过(test_core、test_input、test_shim)。__atomic_thread_fence。这个工具链在 main 上本来就有和本次无关的%zu、未用函数警告,本地构建时把这几类警告降成了非错误;CI 不用这个工具链。nemu_capture_display返回 -2、宽高和缓冲区不动;帧写完后恢复正常;connect 日志里 fps 不带标志位、带seqlock。python -I -X utf8 scripts/test_repo_checks.py、scripts/repo_checks.py、scripts/privacy.py都过。上机测试(12-04,配合模拟器补丁的开发构建):
modavd_extras.dll、external_renderer_ipc.dll。回归时要做的:随模拟器补丁重编以后,在实例上用高频截图(比如 MaaCore 或连续调用
modavd_capture,画面在动的时候)核对没有撕裂,open / connect 日志里能看到seqlock。核对 / Checklist
changelog.d/<名字>.<feat|fix|change|remove>.md,两行:先中文、再英文(只动开发、测试、CI、文档的不用) / A change users can see comes withchangelog.d/<name>.<feat|fix|change|remove>.md, two lines: Chinese, then English (not needed for development, tests, CI or docs)X.md、X.en.md)两份都改了;只会一种语言的在上面说一声 / Both files of a Chinese / English pair (X.md,X.en.md) are changed; if you write only one of the languages, say so aboveRefs ModAVD/ModAVD#431