buffered: Refactoring

This commit is contained in:
Hajime Hoshi 2020-06-30 01:16:50 +09:00
parent 999b726b72
commit 8c5f8c03b7
2 changed files with 18 additions and 24 deletions

View File

@ -71,17 +71,13 @@ func maybeCanAddDelayedCommand() bool {
return atomic.LoadUint32(&delayedCommandsFlushed) == 0
}
func tryAddDelayedCommand(f func(obj interface{}) error, ondelayed func() interface{}) bool {
func tryAddDelayedCommand(f func() error) bool {
delayedCommandsM.Lock()
defer delayedCommandsM.Unlock()
if delayedCommandsFlushed == 0 {
var obj interface{}
if ondelayed != nil {
obj = ondelayed()
}
delayedCommands = append(delayedCommands, func() error {
return f(obj)
return f()
})
return true
}

View File

@ -56,10 +56,10 @@ func NewImage(width, height int, volatile bool) *Image {
func (i *Image) initialize(width, height int, volatile bool) {
if maybeCanAddDelayedCommand() {
if tryAddDelayedCommand(func(obj interface{}) error {
if tryAddDelayedCommand(func() error {
i.initialize(width, height, volatile)
return nil
}, nil) {
}) {
return
}
}
@ -76,10 +76,10 @@ func NewScreenFramebufferImage(width, height int) *Image {
func (i *Image) initializeAsScreenFramebuffer(width, height int) {
if maybeCanAddDelayedCommand() {
if tryAddDelayedCommand(func(obj interface{}) error {
if tryAddDelayedCommand(func() error {
i.initializeAsScreenFramebuffer(width, height)
return nil
}, nil) {
}) {
return
}
}
@ -119,10 +119,10 @@ func (i *Image) resolvePendingFill() {
func (i *Image) MarkDisposed() {
if maybeCanAddDelayedCommand() {
if tryAddDelayedCommand(func(obj interface{}) error {
if tryAddDelayedCommand(func() error {
i.MarkDisposed()
return nil
}, nil) {
}) {
return
}
}
@ -172,10 +172,10 @@ func (i *Image) Dump(name string, blackbg bool) error {
func (i *Image) Fill(clr color.RGBA) {
if maybeCanAddDelayedCommand() {
if tryAddDelayedCommand(func(obj interface{}) error {
if tryAddDelayedCommand(func() error {
i.Fill(clr)
return nil
}, nil) {
}) {
return
}
}
@ -192,13 +192,11 @@ func (i *Image) ReplacePixels(pix []byte, x, y, width, height int) error {
}
if maybeCanAddDelayedCommand() {
if tryAddDelayedCommand(func(copied interface{}) error {
i.ReplacePixels(copied.([]byte), x, y, width, height)
return nil
}, func() interface{} {
copied := make([]byte, len(pix))
copy(copied, pix)
return copied
if tryAddDelayedCommand(func() error {
i.ReplacePixels(copied, x, y, width, height)
return nil
}) {
return nil
}
@ -249,10 +247,10 @@ func (i *Image) DrawImage(src *Image, bounds image.Rectangle, a, b, c, d, tx, ty
}
if maybeCanAddDelayedCommand() {
if tryAddDelayedCommand(func(obj interface{}) error {
if tryAddDelayedCommand(func() error {
i.drawImage(src, bounds, g, colorm, mode, filter)
return nil
}, nil) {
}) {
return
}
}
@ -288,11 +286,11 @@ func (i *Image) DrawTriangles(src *Image, vertices []float32, indices []uint16,
}
if maybeCanAddDelayedCommand() {
if tryAddDelayedCommand(func(obj interface{}) error {
if tryAddDelayedCommand(func() error {
// Arguments are not copied. Copying is the caller's responsibility.
i.DrawTriangles(src, vertices, indices, colorm, mode, filter, address, shader, uniforms)
return nil
}, nil) {
}) {
return
}
}