From a9d8f374c83b677a054e32010a81fc83522e49cc Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Fri, 6 Sep 2024 14:33:48 +0900 Subject: [PATCH] Revert "internal/restorable: move SwapBuffers to internal/atlas" This reverts commit f610cb5724caf7ad73e1b90868c24fa55d869835. Updates #3083 --- internal/atlas/image.go | 14 +------------- internal/restorable/image.go | 2 ++ internal/restorable/images.go | 36 +++++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 13 deletions(-) diff --git a/internal/atlas/image.go b/internal/atlas/image.go index dbdb00a7e..d4d4ce4d6 100644 --- a/internal/atlas/image.go +++ b/internal/atlas/image.go @@ -22,7 +22,6 @@ import ( "runtime" "sync" - "github.com/hajimehoshi/ebiten/v2/internal/debug" "github.com/hajimehoshi/ebiten/v2/internal/graphics" "github.com/hajimehoshi/ebiten/v2/internal/graphicscommand" "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver" @@ -779,18 +778,7 @@ func SwapBuffers(graphicsDriver graphicsdriver.Graphics) error { } }() - if debug.IsDebug { - debug.FrameLogf("Internal image sizes:\n") - imgs := make([]*graphicscommand.Image, 0, len(theBackends)) - for _, backend := range theBackends { - if backend.restorable == nil { - continue - } - imgs = append(imgs, backend.restorable.Image) - } - graphicscommand.LogImagesInfo(imgs) - } - if err := graphicscommand.FlushCommands(graphicsDriver, true); err != nil { + if err := restorable.SwapBuffers(graphicsDriver); err != nil { return err } return nil diff --git a/internal/restorable/image.go b/internal/restorable/image.go index e69bfab57..d2275a762 100644 --- a/internal/restorable/image.go +++ b/internal/restorable/image.go @@ -67,6 +67,7 @@ func NewImage(width, height int, imageType ImageType) *Image { // devices. iw, ih := i.Image.InternalSize() clearImage(i.Image, image.Rect(0, 0, iw, ih)) + theImages.add(i) return i } @@ -168,6 +169,7 @@ func (i *Image) ReadPixels(graphicsDriver graphicsdriver.Graphics, pixels []byte // // After disposing, calling the function of the image causes unexpected results. func (i *Image) Dispose() { + theImages.remove(i) i.Image.Dispose() i.Image = nil } diff --git a/internal/restorable/images.go b/internal/restorable/images.go index 03bac0eaa..d3e15f738 100644 --- a/internal/restorable/images.go +++ b/internal/restorable/images.go @@ -15,10 +15,46 @@ package restorable import ( + "github.com/hajimehoshi/ebiten/v2/internal/debug" "github.com/hajimehoshi/ebiten/v2/internal/graphicscommand" "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver" ) +// images is a set of Image objects. +type images struct { + images map[*Image]struct{} +} + +// theImages represents the images for the current process. +var theImages = &images{ + images: map[*Image]struct{}{}, +} + +func SwapBuffers(graphicsDriver graphicsdriver.Graphics) error { + if debug.IsDebug { + debug.FrameLogf("Internal image sizes:\n") + imgs := make([]*graphicscommand.Image, 0, len(theImages.images)) + for i := range theImages.images { + imgs = append(imgs, i.Image) + } + graphicscommand.LogImagesInfo(imgs) + } + if err := graphicscommand.FlushCommands(graphicsDriver, true); err != nil { + return err + } + return nil +} + +// add adds img to the images. +func (i *images) add(img *Image) { + i.images[img] = struct{}{} +} + +// remove removes img from the images. +func (i *images) remove(img *Image) { + delete(i.images, img) +} + var graphicsDriverInitialized bool // InitializeGraphicsDriverState initializes the graphics driver state.