buffered: Avoid creating func objects whenever possible

Fixes #1220
This commit is contained in:
Hajime Hoshi 2020-06-29 21:45:57 +09:00
parent dea3785750
commit 09322dfdc8
2 changed files with 60 additions and 48 deletions

View File

@ -64,16 +64,14 @@ func getDelayedFuncsAndClearSlow() []func() error {
return nil return nil
} }
// maybeCanAddDelayedCommand returns false if the delayed commands cannot be added.
// Otherwise, maybeCanAddDelayedCommand's returning value is not determined.
// For example, maybeCanAddDelayedCommand can return true even when flusing is being processed.
func maybeCanAddDelayedCommand() bool {
return atomic.LoadUint32(&delayedCommandsFlushed) == 0
}
func tryAddDelayedCommand(f func(obj interface{}) error, ondelayed func() interface{}) bool { func tryAddDelayedCommand(f func(obj interface{}) error, ondelayed func() interface{}) bool {
if atomic.LoadUint32(&delayedCommandsFlushed) == 0 {
// Outline the slow-path to expect the fast-path is inlined.
return tryAddDelayedCommandSlow(f, ondelayed)
}
return false
}
func tryAddDelayedCommandSlow(f func(obj interface{}) error, ondelayed func() interface{}) bool {
delayedCommandsM.Lock() delayedCommandsM.Lock()
defer delayedCommandsM.Unlock() defer delayedCommandsM.Unlock()

View File

@ -55,12 +55,14 @@ func NewImage(width, height int, volatile bool) *Image {
} }
func (i *Image) initialize(width, height int, volatile bool) { func (i *Image) initialize(width, height int, volatile bool) {
if maybeCanAddDelayedCommand() {
if tryAddDelayedCommand(func(obj interface{}) error { if tryAddDelayedCommand(func(obj interface{}) error {
i.initialize(width, height, volatile) i.initialize(width, height, volatile)
return nil return nil
}, nil) { }, nil) {
return return
} }
}
i.img = mipmap.New(width, height, volatile) i.img = mipmap.New(width, height, volatile)
i.width = width i.width = width
i.height = height i.height = height
@ -73,12 +75,14 @@ func NewScreenFramebufferImage(width, height int) *Image {
} }
func (i *Image) initializeAsScreenFramebuffer(width, height int) { func (i *Image) initializeAsScreenFramebuffer(width, height int) {
if maybeCanAddDelayedCommand() {
if tryAddDelayedCommand(func(obj interface{}) error { if tryAddDelayedCommand(func(obj interface{}) error {
i.initializeAsScreenFramebuffer(width, height) i.initializeAsScreenFramebuffer(width, height)
return nil return nil
}, nil) { }, nil) {
return return
} }
}
i.img = mipmap.NewScreenFramebufferMipmap(width, height) i.img = mipmap.NewScreenFramebufferMipmap(width, height)
i.width = width i.width = width
@ -114,12 +118,14 @@ func (i *Image) resolvePendingFill() {
} }
func (i *Image) MarkDisposed() { func (i *Image) MarkDisposed() {
if maybeCanAddDelayedCommand() {
if tryAddDelayedCommand(func(obj interface{}) error { if tryAddDelayedCommand(func(obj interface{}) error {
i.MarkDisposed() i.MarkDisposed()
return nil return nil
}, nil) { }, nil) {
return return
} }
}
i.invalidatePendingPixels() i.invalidatePendingPixels()
i.img.MarkDisposed() i.img.MarkDisposed()
} }
@ -165,12 +171,14 @@ func (i *Image) Dump(name string, blackbg bool) error {
} }
func (i *Image) Fill(clr color.RGBA) { func (i *Image) Fill(clr color.RGBA) {
if maybeCanAddDelayedCommand() {
if tryAddDelayedCommand(func(obj interface{}) error { if tryAddDelayedCommand(func(obj interface{}) error {
i.Fill(clr) i.Fill(clr)
return nil return nil
}, nil) { }, nil) {
return return
} }
}
// Defer filling the image so that successive fillings will be merged into one (#1134). // Defer filling the image so that successive fillings will be merged into one (#1134).
i.invalidatePendingPixels() i.invalidatePendingPixels()
@ -183,6 +191,7 @@ func (i *Image) ReplacePixels(pix []byte, x, y, width, height int) error {
panic(fmt.Sprintf("buffered: len(pix) was %d but must be %d", len(pix), l)) panic(fmt.Sprintf("buffered: len(pix) was %d but must be %d", len(pix), l))
} }
if maybeCanAddDelayedCommand() {
if tryAddDelayedCommand(func(copied interface{}) error { if tryAddDelayedCommand(func(copied interface{}) error {
i.ReplacePixels(copied.([]byte), x, y, width, height) i.ReplacePixels(copied.([]byte), x, y, width, height)
return nil return nil
@ -193,6 +202,7 @@ func (i *Image) ReplacePixels(pix []byte, x, y, width, height int) error {
}) { }) {
return nil return nil
} }
}
if x == 0 && y == 0 && width == i.width && height == i.height { if x == 0 && y == 0 && width == i.width && height == i.height {
i.invalidatePendingPixels() i.invalidatePendingPixels()
@ -238,12 +248,14 @@ func (i *Image) DrawImage(src *Image, bounds image.Rectangle, a, b, c, d, tx, ty
Ty: ty, Ty: ty,
} }
if maybeCanAddDelayedCommand() {
if tryAddDelayedCommand(func(obj interface{}) error { if tryAddDelayedCommand(func(obj interface{}) error {
i.drawImage(src, bounds, g, colorm, mode, filter) i.drawImage(src, bounds, g, colorm, mode, filter)
return nil return nil
}, nil) { }, nil) {
return return
} }
}
i.drawImage(src, bounds, g, colorm, mode, filter) i.drawImage(src, bounds, g, colorm, mode, filter)
} }
@ -275,6 +287,7 @@ func (i *Image) DrawTriangles(src *Image, vertices []float32, indices []uint16,
} }
} }
if maybeCanAddDelayedCommand() {
if tryAddDelayedCommand(func(obj interface{}) error { if tryAddDelayedCommand(func(obj interface{}) error {
// Arguments are not copied. Copying is the caller's responsibility. // Arguments are not copied. Copying is the caller's responsibility.
i.DrawTriangles(src, vertices, indices, colorm, mode, filter, address, shader, uniforms) i.DrawTriangles(src, vertices, indices, colorm, mode, filter, address, shader, uniforms)
@ -282,6 +295,7 @@ func (i *Image) DrawTriangles(src *Image, vertices []float32, indices []uint16,
}, nil) { }, nil) {
return return
} }
}
for _, src := range srcs { for _, src := range srcs {
src.resolvePendingPixels(true) src.resolvePendingPixels(true)