diff --git a/internal/shareable/shareable.go b/internal/shareable/shareable.go index fdeb50e56..05d4e5960 100644 --- a/internal/shareable/shareable.go +++ b/internal/shareable/shareable.go @@ -145,6 +145,7 @@ var ( backendsM sync.Mutex backendsOnce sync.Once + initOnce sync.Once // theBackends is a set of actually shared images. theBackends = []*backend{} @@ -583,12 +584,6 @@ func NewScreenFramebufferImage(width, height int) *Image { return i } -func InitializeGraphicsDriverState() error { - backendsM.Lock() - defer backendsM.Unlock() - return restorable.InitializeGraphicsDriverState() -} - func EndFrame() error { backendsM.Lock() restorable.ResolveStaleImages() @@ -613,6 +608,15 @@ func BeginFrame() error { backendsM.Unlock() } }() + + var err error + initOnce.Do(func() { + err = restorable.InitializeGraphicsDriverState() + }) + if err != nil { + return err + } + return restorable.RestoreIfNeeded() } diff --git a/uicontext.go b/uicontext.go index a19c57d09..6cb4b71a4 100644 --- a/uicontext.go +++ b/uicontext.go @@ -46,7 +46,6 @@ type uiContext struct { screenWidth int screenHeight int screenScale float64 - initialized bool offsetX float64 offsetY float64 } @@ -75,25 +74,12 @@ func (c *uiContext) SetSize(screenWidth, screenHeight int, screenScale float64) c.offsetY = py0 } -func (c *uiContext) initializeIfNeeded() error { - if !c.initialized { - if err := shareable.InitializeGraphicsDriverState(); err != nil { - return err - } - c.initialized = true - } - return nil -} - func (c *uiContext) Update(afterFrameUpdate func()) error { tps := int(MaxTPS()) updateCount := clock.Update(tps) // TODO: If updateCount is 0 and vsync is disabled, swapping buffers can be skipped. - if err := c.initializeIfNeeded(); err != nil { - return err - } if err := shareable.BeginFrame(); err != nil { return err }