Revert "internal/restorable: integrate Image.Extend into internal/atlas"

This reverts commit 6151fd313f.

Updates #3083
This commit is contained in:
Hajime Hoshi 2024-09-06 13:59:19 +09:00
parent 54c117b0de
commit 104cc18477
2 changed files with 30 additions and 29 deletions

View File

@ -122,39 +122,14 @@ func (b *backend) tryAlloc(width, height int) (*packing.Node, bool) {
return nil, false return nil, false
} }
b.extendIfNeeded(b.page.Size()) w, h := b.page.Size()
b.restorable = b.restorable.Extend(w, h)
b.width = w
b.height = h
return n, true return n, true
} }
// extendIfNeeded extends the image by the given size if necessary.
// extendIfNeeded creates a new image with the given size and copies the pixels of the given source image.
// extendIfNeeded disposes an old image after its call when a new image is created.
func (b *backend) extendIfNeeded(width, height int) {
if b.width >= width && b.height >= height {
return
}
// Assume that the screen image is never extended.
newImg := restorable.NewImage(width, height, false)
// Use DrawTriangles instead of WritePixels because the image i might be stale and not have its pixels
// information.
srcs := [graphics.ShaderSrcImageCount]*graphicscommand.Image{b.restorable.Image}
sw, sh := b.restorable.Image.InternalSize()
vs := make([]float32, 4*graphics.VertexFloatCount)
graphics.QuadVerticesFromDstAndSrc(vs, 0, 0, float32(sw), float32(sh), 0, 0, float32(sw), float32(sh), 1, 1, 1, 1)
is := graphics.QuadIndices()
dr := image.Rect(0, 0, sw, sh)
newImg.Image.DrawTriangles(srcs, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderSrcImageCount]image.Rectangle{}, NearestFilterShader.ensureShader().Shader, nil, graphicsdriver.FillRuleFillAll)
b.restorable.Image.Dispose()
b.restorable.Image = nil
b.restorable = newImg
b.width = width
b.height = height
}
var ( var (
// backendsM is a mutex for critical sections of the backend and packing.Node objects. // backendsM is a mutex for critical sections of the backend and packing.Node objects.
backendsM sync.Mutex backendsM sync.Mutex

View File

@ -52,6 +52,32 @@ func NewImage(width, height int, screen bool) *Image {
return i return i
} }
// Extend extends the image by the given size.
// Extend creates a new image with the given size and copies the pixels of the given source image.
// Extend disposes itself after its call.
func (i *Image) Extend(width, height int) *Image {
if i.width >= width && i.height >= height {
return i
}
// Assume that the screen image is never extended.
newImg := NewImage(width, height, false)
// Use DrawTriangles instead of WritePixels because the image i might be stale and not have its pixels
// information.
srcs := [graphics.ShaderSrcImageCount]*graphicscommand.Image{i.Image}
sw, sh := i.Image.InternalSize()
vs := make([]float32, 4*graphics.VertexFloatCount)
graphics.QuadVerticesFromDstAndSrc(vs, 0, 0, float32(sw), float32(sh), 0, 0, float32(sw), float32(sh), 1, 1, 1, 1)
is := graphics.QuadIndices()
dr := image.Rect(0, 0, sw, sh)
newImg.Image.DrawTriangles(srcs, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderSrcImageCount]image.Rectangle{}, NearestFilterShader.Shader, nil, graphicsdriver.FillRuleFillAll)
i.Image.Dispose()
i.Image = nil
return newImg
}
func clearImage(i *graphicscommand.Image, region image.Rectangle) { func clearImage(i *graphicscommand.Image, region image.Rectangle) {
vs := make([]float32, 4*graphics.VertexFloatCount) vs := make([]float32, 4*graphics.VertexFloatCount)
graphics.QuadVerticesFromDstAndSrc(vs, float32(region.Min.X), float32(region.Min.Y), float32(region.Max.X), float32(region.Max.Y), 0, 0, 0, 0, 0, 0, 0, 0) graphics.QuadVerticesFromDstAndSrc(vs, float32(region.Min.X), float32(region.Min.Y), float32(region.Max.X), float32(region.Max.Y), 0, 0, 0, 0, 0, 0, 0, 0)