diff --git a/pkg/worker/caged/libretro/graphics/opengl.go b/pkg/worker/caged/libretro/graphics/opengl.go index ea8b8c092..70b3ceabb 100644 --- a/pkg/worker/caged/libretro/graphics/opengl.go +++ b/pkg/worker/caged/libretro/graphics/opengl.go @@ -117,7 +117,7 @@ func ReadFramebuffer(bytes, w, h uint) []byte { return data } -func getFbo() uint32 { return opt.fbo } +func fbo() uint32 { return opt.fbo } func SetBuffer(size int) { buf = make([]byte, size) } diff --git a/pkg/worker/caged/libretro/graphics/sdl.go b/pkg/worker/caged/libretro/graphics/sdl.go index d0df2c1dc..1d9271eb1 100644 --- a/pkg/worker/caged/libretro/graphics/sdl.go +++ b/pkg/worker/caged/libretro/graphics/sdl.go @@ -5,7 +5,6 @@ import ( "unsafe" "github.com/giongto35/cloud-game/v3/pkg/logger" - "github.com/giongto35/cloud-game/v3/pkg/worker/thread" "github.com/veandco/go-sdl2/sdl" ) @@ -61,7 +60,7 @@ func NewSDLContext(cfg Config, log *logger.Logger) (*SDL, error) { var err error // In OSX 10.14+ window creation and context creation must happen in the main thread - thread.Main(func() { display.w, display.glWCtx, err = createWindow() }) + display.w, display.glWCtx, err = createWindow() if err != nil { return nil, fmt.Errorf("window fail: %w", err) } @@ -81,14 +80,13 @@ func NewSDLContext(cfg Config, log *logger.Logger) (*SDL, error) { func (s *SDL) Deinit() error { s.log.Debug().Msg("[SDL/OpenGL] shutdown...") destroyFramebuffer() - var err error + // In OSX 10.14+ window deletion must happen in the main thread - thread.Main(func() { - err = s.destroyWindow() - }) + err := s.destroyWindow() if err != nil { return fmt.Errorf("[SDL/OpenGL] deinit fail: %w", err) } + sdl.Quit() s.log.Debug().Msgf("[SDL/OpenGL] shutdown codes:(%v, %v)", sdl.GetError(), GetGLError()) return nil @@ -134,6 +132,5 @@ func (s *SDL) setAttribute(attr sdl.GLattr, value int) { } } -func GetGlFbo() uint32 { return getFbo() } - -func GetGlProcAddress(proc string) unsafe.Pointer { return sdl.GLGetProcAddress(proc) } +func GlFbo() uint32 { return fbo() } +func GlProcAddress(proc string) unsafe.Pointer { return sdl.GLGetProcAddress(proc) } diff --git a/pkg/worker/caged/libretro/nanoarch/nanoarch.go b/pkg/worker/caged/libretro/nanoarch/nanoarch.go index e4884062a..3195b2796 100644 --- a/pkg/worker/caged/libretro/nanoarch/nanoarch.go +++ b/pkg/worker/caged/libretro/nanoarch/nanoarch.go @@ -277,13 +277,16 @@ func (n *Nanoarch) LoadGame(path string) error { bufS := uint(n.sysAvInfo.geometry.max_width*n.sysAvInfo.geometry.max_height) * n.Video.PixFmt.BPP graphics.SetBuffer(int(bufS)) n.log.Info().Msgf("Set buffer: %v", byteCountBinary(int64(bufS))) - if n.LibCo { - C.same_thread(C.init_video_cgo) - } else { - runtime.LockOSThread() - initVideo() - runtime.UnlockOSThread() - } + + thread.Main(func() { + if n.LibCo { + C.same_thread(C.init_video_cgo) + } else { + runtime.LockOSThread() + initVideo() + runtime.UnlockOSThread() + } + }) } // set default controller types on all ports @@ -671,11 +674,11 @@ func coreLog(level C.enum_retro_log_level, msg *C.char) { } //export coreGetCurrentFramebuffer -func coreGetCurrentFramebuffer() C.uintptr_t { return (C.uintptr_t)(graphics.GetGlFbo()) } +func coreGetCurrentFramebuffer() C.uintptr_t { return (C.uintptr_t)(graphics.GlFbo()) } //export coreGetProcAddress func coreGetProcAddress(sym *C.char) C.retro_proc_address_t { - return (C.retro_proc_address_t)(graphics.GetGlProcAddress(C.GoString(sym))) + return (C.retro_proc_address_t)(graphics.GlProcAddress(C.GoString(sym))) } //export coreEnvironment diff --git a/pkg/worker/room/room_test.go b/pkg/worker/room/room_test.go index a525d192c..9810069d9 100644 --- a/pkg/worker/room/room_test.go +++ b/pkg/worker/room/room_test.go @@ -14,7 +14,6 @@ import ( "runtime" "sync" "testing" - "time" "github.com/giongto35/cloud-game/v3/pkg/com" "github.com/giongto35/cloud-game/v3/pkg/config" @@ -57,11 +56,6 @@ type conf struct { noLog bool } -func (r testRoom) Close() { - r.Room.Close() - time.Sleep(1 * time.Second) // hack: wait room destruction (atm impossible to tell) -} - func (r testRoom) WaitFrame(n int) app.RawFrame { var wg sync.WaitGroup wg.Add(1) @@ -131,9 +125,9 @@ func TestAll(t *testing.T) { for _, test := range tests { var frame app.RawFrame - room := room(conf{game: test.game, codec: encoder.VP8, autoGlContext: autoGlContext, autoAppStart: false}) flip := test.system == "gl" - thread.Main(func() { frame = room.WaitFrame(test.frames) }) + room := room(conf{game: test.game, codec: encoder.VP8, autoGlContext: autoGlContext, autoAppStart: false}) + frame = room.WaitFrame(test.frames) room.Close() if renderFrames { diff --git a/pkg/worker/thread/mainthread_darwin.go b/pkg/worker/thread/mainthread_darwin.go index 730a3f272..475c130c5 100644 --- a/pkg/worker/thread/mainthread_darwin.go +++ b/pkg/worker/thread/mainthread_darwin.go @@ -1,16 +1,12 @@ package thread -import ( - "runtime" - "sync" -) +import "runtime" type fun struct { fn func() done chan struct{} } -var dPool = sync.Pool{New: func() any { return make(chan struct{}) }} var fq = make(chan fun, runtime.GOMAXPROCS(0)) func init() { @@ -38,8 +34,7 @@ func Run(run func()) { // Call queues function f on the main thread and blocks until the function f finishes. func Call(f func()) { - done := dPool.Get().(chan struct{}) - defer dPool.Put(done) + done := make(chan struct{}) fq <- fun{fn: f, done: done} <-done }