Skip to content

Commit

Permalink
Allow duplicate frames
Browse files Browse the repository at this point in the history
Some cores for performance reasons may return duplicate frames (i.e. previous frames) instead of rendering them again.
  • Loading branch information
sergystepanov committed Mar 21, 2024
1 parent ff6c344 commit 4d5033f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
2 changes: 2 additions & 0 deletions pkg/config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ emulator:
# Sets a limiter function for some spammy core callbacks.
# 0 - disabled, otherwise -- time in milliseconds for ignoring repeated calls except the last.
debounceMs: 0
# Allow duplicate frames
dup: true
# Libretro cores logging level: DEBUG = 0, INFO, WARN, ERROR, DUMMY = INT_MAX
logLevel: 1
cores:
Expand Down
1 change: 1 addition & 0 deletions pkg/config/emulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type LibretroConfig struct {
List map[string]LibretroCoreConfig
}
DebounceMs int
Dup bool
SaveCompression bool
LogLevel int
}
Expand Down
11 changes: 10 additions & 1 deletion pkg/worker/caged/libretro/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ var (
noData = func([]byte) {}
noVideo = func(app.Video) {}
videoPool sync.Pool
lastFrame *app.Video
)

// NewFrontend implements Emulator interface for a Libretro frontend.
Expand Down Expand Up @@ -156,6 +157,7 @@ func (f *Frontend) LoadCore(emu string) {
conf := f.conf.GetLibretroCoreConfig(emu)
meta := nanoarch.Metadata{
AutoGlContext: conf.AutoGlContext,
FrameDup: f.conf.Libretro.Dup,
Hacks: conf.Hacks,
HasVFR: conf.VFR,
Hid: conf.Hid,
Expand Down Expand Up @@ -190,7 +192,6 @@ func (f *Frontend) handleAudio(audio unsafe.Pointer, samples int) {
}

func (f *Frontend) handleVideo(data []byte, delta int32, fi nanoarch.FrameInfo) {
// !to merge both pools
fr, _ := videoPool.Get().(*app.Video)
if fr == nil {
fr = new(app.Video)
Expand All @@ -200,10 +201,17 @@ func (f *Frontend) handleVideo(data []byte, delta int32, fi nanoarch.FrameInfo)
fr.Frame.H = int(fi.H)
fr.Frame.Stride = int(fi.Stride)
fr.Duration = delta

lastFrame = fr
f.onVideo(*fr)

videoPool.Put(fr)
}

func (f *Frontend) handleDup() {
f.onVideo(*lastFrame)
}

func (f *Frontend) Shutdown() {
f.mu.Lock()
f.nano.Shutdown()
Expand All @@ -224,6 +232,7 @@ func (f *Frontend) linkNano(nano *nanoarch.Nanoarch) {
f.nano.OnDpad = f.input.isDpadTouched
f.nano.OnVideo = f.handleVideo
f.nano.OnAudio = f.handleAudio
f.nano.OnDup = f.handleDup
}

func (f *Frontend) SetVideoChangeCb(fn func()) {
Expand Down
13 changes: 8 additions & 5 deletions pkg/worker/caged/libretro/nanoarch/nanoarch.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ type Handlers struct {
OnKeyPress func(port uint, key int) int
OnAudio func(ptr unsafe.Pointer, frames int)
OnVideo func(data []byte, delta int32, fi FrameInfo)
OnDup func()
OnSystemAvInfo func()
}

Expand All @@ -88,6 +89,7 @@ type FrameInfo struct {
}

type Metadata struct {
FrameDup bool
LibPath string // the full path to some emulator lib
IsGlAllowed bool
UsesLibCo bool
Expand Down Expand Up @@ -127,6 +129,7 @@ var Nan0 = Nanoarch{
OnKeyPress: func(uint, int) int { return 0 },
OnAudio: func(unsafe.Pointer, int) {},
OnVideo: func([]byte, int32, FrameInfo) {},
OnDup: func() {},
},
}

Expand Down Expand Up @@ -559,9 +562,9 @@ func coreVideoRefresh(data unsafe.Pointer, width, height uint, packed uint) {
}
Nan0.LastFrameTime = t

// some cores can return nothing
// !to add duplicate if can dup
// when the core returns a duplicate frame
if data == nil {
Nan0.Handlers.OnDup()
return
}

Expand Down Expand Up @@ -694,9 +697,9 @@ func coreEnvironment(cmd C.unsigned, data unsafe.Pointer) C.bool {
setRotation((*(*uint)(data) % 4) * 90)
return true
case C.RETRO_ENVIRONMENT_GET_CAN_DUPE:
// !to implement frame dup (nil) some time later
*(*C.bool)(data) = C.bool(false)
return false
dup := C.bool(Nan0.meta.FrameDup)
*(*C.bool)(data) = dup
return dup
case C.RETRO_ENVIRONMENT_GET_USERNAME:
*(**C.char)(data) = Nan0.cUserName
return true
Expand Down

0 comments on commit 4d5033f

Please sign in to comment.