internal/restorable: move DumpImages to internal/atlas

Updates #805
This commit is contained in:
Hajime Hoshi 2024-01-09 04:50:19 +09:00
parent fdf36026ae
commit 7c9266d8b6
3 changed files with 27 additions and 28 deletions

View File

@ -22,6 +22,7 @@ import (
"sync" "sync"
"github.com/hajimehoshi/ebiten/v2/internal/graphics" "github.com/hajimehoshi/ebiten/v2/internal/graphics"
"github.com/hajimehoshi/ebiten/v2/internal/graphicscommand"
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver" "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
"github.com/hajimehoshi/ebiten/v2/internal/packing" "github.com/hajimehoshi/ebiten/v2/internal/packing"
"github.com/hajimehoshi/ebiten/v2/internal/restorable" "github.com/hajimehoshi/ebiten/v2/internal/restorable"
@ -830,5 +831,12 @@ func DumpImages(graphicsDriver graphicsdriver.Graphics, dir string) (string, err
panic("atlas: DumpImages must be called in between BeginFrame and EndFrame") panic("atlas: DumpImages must be called in between BeginFrame and EndFrame")
} }
return restorable.DumpImages(graphicsDriver, dir) images := make([]*graphicscommand.Image, 0, len(theBackends))
for _, backend := range theBackends {
if backend.restorable == nil {
continue
}
images = append(images, backend.restorable.Image)
}
return graphicscommand.DumpImages(images, graphicsDriver, dir)
} }

View File

@ -35,7 +35,10 @@ const (
// Image represents an image. // Image represents an image.
type Image struct { type Image struct {
image *graphicscommand.Image // Image is the underlying image.
// This member is exported on purpose.
// TODO: Move the implementation to internal/atlas package (#805).
Image *graphicscommand.Image
width int width int
height int height int
@ -54,7 +57,7 @@ func NewImage(width, height int, imageType ImageType) *Image {
} }
i := &Image{ i := &Image{
image: graphicscommand.NewImage(width, height, imageType == ImageTypeScreen), Image: graphicscommand.NewImage(width, height, imageType == ImageTypeScreen),
width: width, width: width,
height: height, height: height,
imageType: imageType, imageType: imageType,
@ -62,8 +65,8 @@ func NewImage(width, height int, imageType ImageType) *Image {
// This needs to use 'InternalSize' to render the whole region, or edges are unexpectedly cleared on some // This needs to use 'InternalSize' to render the whole region, or edges are unexpectedly cleared on some
// devices. // devices.
iw, ih := i.image.InternalSize() iw, ih := i.Image.InternalSize()
clearImage(i.image, image.Rect(0, 0, iw, ih)) clearImage(i.Image, image.Rect(0, 0, iw, ih))
theImages.add(i) theImages.add(i)
return i return i
} }
@ -81,7 +84,7 @@ func (i *Image) Extend(width, height int) *Image {
// Use DrawTriangles instead of WritePixels because the image i might be stale and not have its pixels // Use DrawTriangles instead of WritePixels because the image i might be stale and not have its pixels
// information. // information.
srcs := [graphics.ShaderImageCount]*Image{i} srcs := [graphics.ShaderImageCount]*Image{i}
sw, sh := i.image.InternalSize() sw, sh := i.Image.InternalSize()
vs := quadVertices(0, 0, float32(sw), float32(sh), 0, 0, float32(sw), float32(sh), 1, 1, 1, 1) vs := quadVertices(0, 0, float32(sw), float32(sh), 0, 0, float32(sw), float32(sh), 1, 1, 1, 1)
is := graphics.QuadIndices() is := graphics.QuadIndices()
dr := image.Rect(0, 0, sw, sh) dr := image.Rect(0, 0, sw, sh)
@ -125,9 +128,9 @@ func (i *Image) WritePixels(pixels *graphics.ManagedBytes, region image.Rectangl
} }
if pixels != nil { if pixels != nil {
i.image.WritePixels(pixels, region) i.Image.WritePixels(pixels, region)
} else { } else {
clearImage(i.image, region) clearImage(i.Image, region)
} }
} }
@ -153,13 +156,13 @@ func (i *Image) DrawTriangles(srcs [graphics.ShaderImageCount]*Image, vertices [
if src == nil { if src == nil {
continue continue
} }
imgs[i] = src.image imgs[i] = src.Image
} }
i.image.DrawTriangles(imgs, vertices, indices, blend, dstRegion, srcRegions, shader.shader, uniforms, fillRule) i.Image.DrawTriangles(imgs, vertices, indices, blend, dstRegion, srcRegions, shader.shader, uniforms, fillRule)
} }
func (i *Image) ReadPixels(graphicsDriver graphicsdriver.Graphics, pixels []byte, region image.Rectangle) error { func (i *Image) ReadPixels(graphicsDriver graphicsdriver.Graphics, pixels []byte, region image.Rectangle) error {
if err := i.image.ReadPixels(graphicsDriver, []graphicsdriver.PixelsArgs{ if err := i.Image.ReadPixels(graphicsDriver, []graphicsdriver.PixelsArgs{
{ {
Pixels: pixels, Pixels: pixels,
Region: region, Region: region,
@ -175,14 +178,14 @@ func (i *Image) ReadPixels(graphicsDriver graphicsdriver.Graphics, pixels []byte
// After disposing, calling the function of the image causes unexpected results. // After disposing, calling the function of the image causes unexpected results.
func (i *Image) Dispose() { func (i *Image) Dispose() {
theImages.remove(i) theImages.remove(i)
i.image.Dispose() i.Image.Dispose()
i.image = nil i.Image = nil
} }
func (i *Image) Dump(graphicsDriver graphicsdriver.Graphics, path string, blackbg bool, rect image.Rectangle) (string, error) { func (i *Image) Dump(graphicsDriver graphicsdriver.Graphics, path string, blackbg bool, rect image.Rectangle) (string, error) {
return i.image.Dump(graphicsDriver, path, blackbg, rect) return i.Image.Dump(graphicsDriver, path, blackbg, rect)
} }
func (i *Image) InternalSize() (int, int) { func (i *Image) InternalSize() (int, int) {
return i.image.InternalSize() return i.Image.InternalSize()
} }

View File

@ -35,7 +35,7 @@ func SwapBuffers(graphicsDriver graphicsdriver.Graphics) error {
debug.Logf("Internal image sizes:\n") debug.Logf("Internal image sizes:\n")
imgs := make([]*graphicscommand.Image, 0, len(theImages.images)) imgs := make([]*graphicscommand.Image, 0, len(theImages.images))
for i := range theImages.images { for i := range theImages.images {
imgs = append(imgs, i.image) imgs = append(imgs, i.Image)
} }
graphicscommand.LogImagesInfo(imgs) graphicscommand.LogImagesInfo(imgs)
} }
@ -45,18 +45,6 @@ func SwapBuffers(graphicsDriver graphicsdriver.Graphics) error {
return nil return nil
} }
// DumpImages dumps all the current images to the specified directory.
//
// This is for testing usage.
func DumpImages(graphicsDriver graphicsdriver.Graphics, dir string) (string, error) {
images := make([]*graphicscommand.Image, 0, len(theImages.images))
for img := range theImages.images {
images = append(images, img.image)
}
return graphicscommand.DumpImages(images, graphicsDriver, dir)
}
// add adds img to the images. // add adds img to the images.
func (i *images) add(img *Image) { func (i *images) add(img *Image) {
i.images[img] = struct{}{} i.images[img] = struct{}{}