feat: wire pullFrame path and runtime placeholder rendering
This commit is contained in:
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'
|
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 {
|
function assertOk(result: { ok: boolean; errorCode?: number; errorMessage?: string }, action: string): void {
|
||||||
if (!result.ok) {
|
if (!result.ok) {
|
||||||
throw new Error(`${action} failed: ${result.errorCode ?? -1} ${result.errorMessage ?? ''}`.trim())
|
throw new Error(`${action} failed: ${result.errorCode ?? -1} ${result.errorMessage ?? ''}`.trim())
|
||||||
@@ -27,6 +39,10 @@ export class NativeRuntimeAdapter {
|
|||||||
assertOk(nativeMrp.release(), 'release')
|
assertOk(nativeMrp.release(), 'release')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async pullFrame(): Promise<NativeFrameDTO> {
|
||||||
|
return nativeMrp.pullFrame() as NativeFrameDTO
|
||||||
|
}
|
||||||
|
|
||||||
async notifyPageShow(): Promise<void> {
|
async notifyPageShow(): Promise<void> {
|
||||||
const result = nativeMrp.resume()
|
const result = nativeMrp.resume()
|
||||||
if (!result.ok) {
|
if (!result.ok) {
|
||||||
|
|||||||
@@ -1,12 +1,36 @@
|
|||||||
@Component
|
@Component
|
||||||
export struct RuntimeContainer {
|
export struct RuntimeContainer {
|
||||||
@Prop hint: string = 'Runtime Surface Placeholder'
|
@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() {
|
build() {
|
||||||
Column({ space: 12 }) {
|
Column({ space: 12 }) {
|
||||||
Text('MRP Runtime Surface Placeholder')
|
Text('MRP Runtime Surface Placeholder')
|
||||||
.fontSize(18)
|
.fontSize(18)
|
||||||
.fontWeight(FontWeight.Medium)
|
.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)
|
Text(this.hint)
|
||||||
.fontSize(14)
|
.fontSize(14)
|
||||||
.fontColor('#666666')
|
.fontColor('#666666')
|
||||||
|
|||||||
@@ -2,8 +2,10 @@
|
|||||||
export struct RuntimeToolbar {
|
export struct RuntimeToolbar {
|
||||||
onBack?: () => void
|
onBack?: () => void
|
||||||
onRestart?: () => void
|
onRestart?: () => void
|
||||||
|
onPullFrame?: () => void
|
||||||
onExit?: () => void
|
onExit?: () => void
|
||||||
@Prop canRestart: boolean = true
|
@Prop canRestart: boolean = true
|
||||||
|
@Prop canPullFrame: boolean = true
|
||||||
@Prop canExit: boolean = true
|
@Prop canExit: boolean = true
|
||||||
|
|
||||||
build() {
|
build() {
|
||||||
@@ -20,6 +22,13 @@ export struct RuntimeToolbar {
|
|||||||
this.onRestart()
|
this.onRestart()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
Button('拉帧')
|
||||||
|
.enabled(this.canPullFrame)
|
||||||
|
.onClick(() => {
|
||||||
|
if (this.onPullFrame) {
|
||||||
|
this.onPullFrame()
|
||||||
|
}
|
||||||
|
})
|
||||||
Button('退出')
|
Button('退出')
|
||||||
.enabled(this.canExit)
|
.enabled(this.canExit)
|
||||||
.onClick(() => {
|
.onClick(() => {
|
||||||
|
|||||||
@@ -45,6 +45,10 @@ export struct RunPage {
|
|||||||
await this.store.restartSession()
|
await this.store.restartSession()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async onPullFrame(): Promise<void> {
|
||||||
|
await this.store.refreshFrame()
|
||||||
|
}
|
||||||
|
|
||||||
private async onExit(): Promise<void> {
|
private async onExit(): Promise<void> {
|
||||||
await this.store.stopSession()
|
await this.store.stopSession()
|
||||||
if (this.onBack) {
|
if (this.onBack) {
|
||||||
@@ -65,12 +69,20 @@ export struct RunPage {
|
|||||||
}
|
}
|
||||||
RuntimeToolbar({
|
RuntimeToolbar({
|
||||||
canRestart: this.store.canRestart,
|
canRestart: this.store.canRestart,
|
||||||
|
canPullFrame: this.store.phase === 'running',
|
||||||
canExit: this.store.canExit,
|
canExit: this.store.canExit,
|
||||||
onBack: () => this.onBackClick(),
|
onBack: () => this.onBackClick(),
|
||||||
onRestart: () => this.onRestart(),
|
onRestart: () => this.onRestart(),
|
||||||
|
onPullFrame: () => this.onPullFrame(),
|
||||||
onExit: () => this.onExit()
|
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') {
|
if (this.store.phase !== 'running') {
|
||||||
RuntimeStatusOverlay({ text: `当前状态:${this.store.phase}` })
|
RuntimeStatusOverlay({ text: `当前状态:${this.store.phase}` })
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,13 @@
|
|||||||
import { NativeRuntimeAdapter } from '../adapters/NativeRuntimeAdapter'
|
import { NativeRuntimeAdapter } from '../adapters/NativeRuntimeAdapter'
|
||||||
|
|
||||||
|
export interface RuntimeFrameVM {
|
||||||
|
hasFrame: boolean
|
||||||
|
width: number
|
||||||
|
height: number
|
||||||
|
frameId: number
|
||||||
|
pixelFormat: string
|
||||||
|
}
|
||||||
|
|
||||||
export class RuntimeService {
|
export class RuntimeService {
|
||||||
constructor(private readonly adapter: NativeRuntimeAdapter = new NativeRuntimeAdapter()) {}
|
constructor(private readonly adapter: NativeRuntimeAdapter = new NativeRuntimeAdapter()) {}
|
||||||
|
|
||||||
@@ -15,6 +23,20 @@ export class RuntimeService {
|
|||||||
await this.adapter.stopSession()
|
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> {
|
async onPageShow(): Promise<void> {
|
||||||
await this.adapter.notifyPageShow()
|
await this.adapter.notifyPageShow()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { RuntimeService } from '../services/RuntimeService'
|
import { RuntimeService } from '../services/RuntimeService'
|
||||||
|
import type { RuntimeFrameVM } from '../services/RuntimeService'
|
||||||
import type { UiError } from '../models/UiError'
|
import type { UiError } from '../models/UiError'
|
||||||
import type { RuntimePhase } from '../models/RuntimeSessionState'
|
import type { RuntimePhase } from '../models/RuntimeSessionState'
|
||||||
import { StoreObserver } from './StoreObserver'
|
import { StoreObserver } from './StoreObserver'
|
||||||
@@ -10,6 +11,13 @@ export class RuntimeSessionStore extends StoreObserver {
|
|||||||
canRestart: boolean = false
|
canRestart: boolean = false
|
||||||
canExit: boolean = false
|
canExit: boolean = false
|
||||||
runtimeHint: string = '等待启动'
|
runtimeHint: string = '等待启动'
|
||||||
|
frame: RuntimeFrameVM = {
|
||||||
|
hasFrame: false,
|
||||||
|
width: 0,
|
||||||
|
height: 0,
|
||||||
|
frameId: 0,
|
||||||
|
pixelFormat: 'RGBA_8888',
|
||||||
|
}
|
||||||
|
|
||||||
constructor(private readonly service: RuntimeService = new RuntimeService()) {
|
constructor(private readonly service: RuntimeService = new RuntimeService()) {
|
||||||
super()
|
super()
|
||||||
@@ -28,7 +36,8 @@ export class RuntimeSessionStore extends StoreObserver {
|
|||||||
this.phase = 'running'
|
this.phase = 'running'
|
||||||
this.canRestart = true
|
this.canRestart = true
|
||||||
this.canExit = true
|
this.canExit = true
|
||||||
this.runtimeHint = 'Native/NAPI 已连通,可进入后续拉帧联调'
|
this.runtimeHint = 'Native/NAPI 已连通,准备拉取测试帧'
|
||||||
|
await this.refreshFrame()
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.phase = 'error'
|
this.phase = 'error'
|
||||||
this.error = { message: '启动失败', retryable: true }
|
this.error = { message: '启动失败', retryable: true }
|
||||||
@@ -51,7 +60,8 @@ export class RuntimeSessionStore extends StoreObserver {
|
|||||||
this.phase = 'running'
|
this.phase = 'running'
|
||||||
this.canRestart = true
|
this.canRestart = true
|
||||||
this.canExit = true
|
this.canExit = true
|
||||||
this.runtimeHint = '重启完成,Native/NAPI 仍可用'
|
this.runtimeHint = '重启完成,准备重新拉取测试帧'
|
||||||
|
await this.refreshFrame()
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.phase = 'error'
|
this.phase = 'error'
|
||||||
this.error = { message: '重启失败', retryable: true }
|
this.error = { message: '重启失败', retryable: true }
|
||||||
@@ -66,6 +76,27 @@ export class RuntimeSessionStore extends StoreObserver {
|
|||||||
this.canExit = false
|
this.canExit = false
|
||||||
this.canRestart = false
|
this.canRestart = false
|
||||||
this.runtimeHint = '运行会话已释放'
|
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()
|
this.notify()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user