feat: wire pullFrame path and runtime placeholder rendering

This commit is contained in:
OpenClaw committed 2026-03-09 18:50:42 +08:00
1 parent bde990ac82
commit 4613919277
6 files changed
+117 -3

No files matched your search

@@ -1,5 +1,17 @@
import nativeMrp from 'libmrp_napi.so'
export interface NativeFrameDTO {
ok: boolean
hasNewFrame: boolean
width: number
height: number
pixelFormat: 'RGBA_8888'
buffer?: ArrayBuffer
frameId?: number
errorCode?: number
errorMessage?: string
}
function assertOk(result: { ok: boolean; errorCode?: number; errorMessage?: string }, action: string): void {
if (!result.ok) {
throw new Error(`${action} failed: ${result.errorCode ?? -1} ${result.errorMessage ?? ''}`.trim())
@@ -27,6 +39,10 @@ export class NativeRuntimeAdapter {
assertOk(nativeMrp.release(), 'release')
}
async pullFrame(): Promise<NativeFrameDTO> {
return nativeMrp.pullFrame() as NativeFrameDTO
}
async notifyPageShow(): Promise<void> {
const result = nativeMrp.resume()
if (!result.ok) {
@@ -1,12 +1,36 @@
@Component
export struct RuntimeContainer {
@Prop hint: string = 'Runtime Surface Placeholder'
@Prop hasFrame: boolean = false
@Prop frameId: number = 0
@Prop frameWidth: number = 0
@Prop frameHeight: number = 0
private frameColor(): string {
const palette: string[] = ['#DCEBFF', '#C6F6D5', '#FEEBC8', '#FED7E2', '#E9D8FD']
return this.hasFrame ? palette[this.frameId % palette.length] : '#F3F4F6'
}
build() {
Column({ space: 12 }) {
Text('MRP Runtime Surface Placeholder')
.fontSize(18)
.fontWeight(FontWeight.Medium)
Stack() {
Column() {
Text(this.hasFrame ? `测试帧 #${this.frameId}` : '尚未拉到测试帧')
.fontSize(18)
.fontWeight(FontWeight.Bold)
Text(this.hasFrame ? `${this.frameWidth} x ${this.frameHeight}` : '等待 pullFrame()')
.fontSize(14)
.fontColor('#666666')
}
}
.width('88%')
.aspectRatio(3 / 4)
.backgroundColor(this.frameColor())
.borderRadius(16)
.padding(16)
Text(this.hint)
.fontSize(14)
.fontColor('#666666')
@@ -2,8 +2,10 @@
export struct RuntimeToolbar {
onBack?: () => void
onRestart?: () => void
onPullFrame?: () => void
onExit?: () => void
@Prop canRestart: boolean = true
@Prop canPullFrame: boolean = true
@Prop canExit: boolean = true
build() {
@@ -20,6 +22,13 @@ export struct RuntimeToolbar {
this.onRestart()
}
})
Button('拉帧')
.enabled(this.canPullFrame)
.onClick(() => {
if (this.onPullFrame) {
this.onPullFrame()
}
})
Button('退出')
.enabled(this.canExit)
.onClick(() => {
+13 -1
View File
@@ -45,6 +45,10 @@ export struct RunPage {
await this.store.restartSession()
}
private async onPullFrame(): Promise<void> {
await this.store.refreshFrame()
}
private async onExit(): Promise<void> {
await this.store.stopSession()
if (this.onBack) {
@@ -65,12 +69,20 @@ export struct RunPage {
}
RuntimeToolbar({
canRestart: this.store.canRestart,
canPullFrame: this.store.phase === 'running',
canExit: this.store.canExit,
onBack: () => this.onBackClick(),
onRestart: () => this.onRestart(),
onPullFrame: () => this.onPullFrame(),
onExit: () => this.onExit()
})
RuntimeContainer({ hint: this.store.runtimeHint })
RuntimeContainer({
hint: this.store.runtimeHint,
hasFrame: this.store.frame.hasFrame,
frameId: this.store.frame.frameId,
frameWidth: this.store.frame.width,
frameHeight: this.store.frame.height,
})
if (this.store.phase !== 'running') {
RuntimeStatusOverlay({ text: `当前状态:${this.store.phase}` })
}
@@ -1,5 +1,13 @@
import { NativeRuntimeAdapter } from '../adapters/NativeRuntimeAdapter'
export interface RuntimeFrameVM {
hasFrame: boolean
width: number
height: number
frameId: number
pixelFormat: string
}
export class RuntimeService {
constructor(private readonly adapter: NativeRuntimeAdapter = new NativeRuntimeAdapter()) {}
@@ -15,6 +23,20 @@ export class RuntimeService {
await this.adapter.stopSession()
}
async pullFrame(): Promise<RuntimeFrameVM> {
const frame = await this.adapter.pullFrame()
if (!frame.ok) {
throw new Error(frame.errorMessage || 'pullFrame failed')
}
return {
hasFrame: frame.hasNewFrame,
width: frame.width,
height: frame.height,
frameId: frame.frameId ?? 0,
pixelFormat: frame.pixelFormat,
}
}
async onPageShow(): Promise<void> {
await this.adapter.notifyPageShow()
}
@@ -1,4 +1,5 @@
import { RuntimeService } from '../services/RuntimeService'
import type { RuntimeFrameVM } from '../services/RuntimeService'
import type { UiError } from '../models/UiError'
import type { RuntimePhase } from '../models/RuntimeSessionState'
import { StoreObserver } from './StoreObserver'
@@ -10,6 +11,13 @@ export class RuntimeSessionStore extends StoreObserver {
canRestart: boolean = false
canExit: boolean = false
runtimeHint: string = '等待启动'
frame: RuntimeFrameVM = {
hasFrame: false,
width: 0,
height: 0,
frameId: 0,
pixelFormat: 'RGBA_8888',
}
constructor(private readonly service: RuntimeService = new RuntimeService()) {
super()
@@ -28,7 +36,8 @@ export class RuntimeSessionStore extends StoreObserver {
this.phase = 'running'
this.canRestart = true
this.canExit = true
this.runtimeHint = 'Native/NAPI 已连通,可进入后续拉帧联调'
this.runtimeHint = 'Native/NAPI 已连通,准备拉取测试帧'
await this.refreshFrame()
} catch (error) {
this.phase = 'error'
this.error = { message: '启动失败', retryable: true }
@@ -51,7 +60,8 @@ export class RuntimeSessionStore extends StoreObserver {
this.phase = 'running'
this.canRestart = true
this.canExit = true
this.runtimeHint = '重启完成,Native/NAPI 仍可用'
this.runtimeHint = '重启完成,准备重新拉取测试帧'
await this.refreshFrame()
} catch (error) {
this.phase = 'error'
this.error = { message: '重启失败', retryable: true }
@@ -66,6 +76,27 @@ export class RuntimeSessionStore extends StoreObserver {
this.canExit = false
this.canRestart = false
this.runtimeHint = '运行会话已释放'
this.frame = {
hasFrame: false,
width: 0,
height: 0,
frameId: 0,
pixelFormat: 'RGBA_8888',
}
this.notify()
}
async refreshFrame(): Promise<void> {
try {
const frame = await this.service.pullFrame()
this.frame = frame
this.runtimeHint = frame.hasFrame
? `已拉到测试帧 #${frame.frameId}(${frame.width}x${frame.height} ${frame.pixelFormat})`
: '当前未返回新帧'
} catch (error) {
this.runtimeHint = error instanceof Error ? error.message : 'pullFrame 失败'
this.error = { message: '拉帧失败', retryable: true }
}
this.notify()
}