mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-24 18:58:54 +01:00
internal/restorable: minimize a region for restoring
This commit is contained in:
parent
ff926b2e9f
commit
09e0320309
@ -123,17 +123,6 @@ type Image struct {
|
|||||||
// staleRegion is valid only when stale is true.
|
// staleRegion is valid only when stale is true.
|
||||||
staleRegion image.Rectangle
|
staleRegion image.Rectangle
|
||||||
|
|
||||||
// pixelsForRestore is a cached byte slice for pixels.
|
|
||||||
// pixelsForRestore is just a cache to avoid allocations, and the data might not be reliable.
|
|
||||||
//
|
|
||||||
// pixelsForRestore might be shared by the records of basePixels.
|
|
||||||
// pixelsForRestore should not be modified until basePixels is invalidated.
|
|
||||||
//
|
|
||||||
// pixelsForRestore is an entire pixels of the image or nil.
|
|
||||||
//
|
|
||||||
// pixelsForRestore is for an optimization to reduce slice allocations (#2375).
|
|
||||||
pixelsForRestore []byte
|
|
||||||
|
|
||||||
imageType ImageType
|
imageType ImageType
|
||||||
|
|
||||||
// priority indicates whether the image is restored in high priority when context-lost happens.
|
// priority indicates whether the image is restored in high priority when context-lost happens.
|
||||||
@ -625,18 +614,34 @@ func (i *Image) restore(graphicsDriver graphicsdriver.Graphics) error {
|
|||||||
gimg.DrawTriangles(imgs, c.offsets, c.vertices, c.indices, c.blend, c.dstRegion, c.srcRegion, c.shader.shader, c.uniforms, c.evenOdd)
|
gimg.DrawTriangles(imgs, c.offsets, c.vertices, c.indices, c.blend, c.dstRegion, c.srcRegion, c.shader.shader, c.uniforms, c.evenOdd)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// In order to clear the draw-triangles history, read pixels from GPU.
|
||||||
if len(i.drawTrianglesHistory) > 0 {
|
if len(i.drawTrianglesHistory) > 0 {
|
||||||
i.basePixels = Pixels{}
|
var rs []image.Rectangle
|
||||||
// As basePixels was invalidated, pixelsForRestore can be reused.
|
for _, d := range i.drawTrianglesHistory {
|
||||||
l := 4 * w * h
|
r := regionToRectangle(d.dstRegion)
|
||||||
if len(i.pixelsForRestore) < l {
|
if r.Empty() {
|
||||||
i.pixelsForRestore = make([]byte, l)
|
continue
|
||||||
|
}
|
||||||
|
for i, rr := range rs {
|
||||||
|
if rr.Empty() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if rr.In(r) {
|
||||||
|
rs[i] = image.Rectangle{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
rs = append(rs, r)
|
||||||
}
|
}
|
||||||
pix := i.pixelsForRestore[:l]
|
for _, r := range rs {
|
||||||
if err := gimg.ReadPixels(graphicsDriver, pix, 0, 0, w, h); err != nil {
|
if r.Empty() {
|
||||||
return err
|
continue
|
||||||
|
}
|
||||||
|
pix := make([]byte, 4*r.Dx()*r.Dy())
|
||||||
|
if err := gimg.ReadPixels(graphicsDriver, pix, r.Min.X, r.Min.Y, r.Dx(), r.Dy()); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
i.basePixels.AddOrReplace(pix, r.Min.X, r.Min.Y, r.Dx(), r.Dy())
|
||||||
}
|
}
|
||||||
i.basePixels.AddOrReplace(pix, 0, 0, w, h)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
i.image = gimg
|
i.image = gimg
|
||||||
@ -654,7 +659,6 @@ func (i *Image) Dispose() {
|
|||||||
i.image.Dispose()
|
i.image.Dispose()
|
||||||
i.image = nil
|
i.image = nil
|
||||||
i.basePixels = Pixels{}
|
i.basePixels = Pixels{}
|
||||||
i.pixelsForRestore = nil
|
|
||||||
i.clearDrawTrianglesHistory()
|
i.clearDrawTrianglesHistory()
|
||||||
i.stale = false
|
i.stale = false
|
||||||
i.staleRegion = image.Rectangle{}
|
i.staleRegion = image.Rectangle{}
|
||||||
|
Loading…
Reference in New Issue
Block a user