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 return atomic.LoadUint32(&delayedCommandsFlushed) == 0
} }
func tryAddDelayedCommand(f func(obj interface{}) error, ondelayed func() interface{}) bool { func tryAddDelayedCommand(f func() error) bool {
delayedCommandsM.Lock() delayedCommandsM.Lock()
defer delayedCommandsM.Unlock() defer delayedCommandsM.Unlock()
if delayedCommandsFlushed == 0 { if delayedCommandsFlushed == 0 {
var obj interface{}
if ondelayed != nil {
obj = ondelayed()
}
delayedCommands = append(delayedCommands, func() error { delayedCommands = append(delayedCommands, func() error {
return f(obj) return f()
}) })
return true 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) { func (i *Image) initialize(width, height int, volatile bool) {
if maybeCanAddDelayedCommand() { if maybeCanAddDelayedCommand() {
if tryAddDelayedCommand(func(obj interface{}) error { if tryAddDelayedCommand(func() error {
i.initialize(width, height, volatile) i.initialize(width, height, volatile)
return nil return nil
}, nil) { }) {
return return
} }
} }
@ -76,10 +76,10 @@ func NewScreenFramebufferImage(width, height int) *Image {
func (i *Image) initializeAsScreenFramebuffer(width, height int) { func (i *Image) initializeAsScreenFramebuffer(width, height int) {
if maybeCanAddDelayedCommand() { if maybeCanAddDelayedCommand() {
if tryAddDelayedCommand(func(obj interface{}) error { if tryAddDelayedCommand(func() error {
i.initializeAsScreenFramebuffer(width, height) i.initializeAsScreenFramebuffer(width, height)
return nil return nil
}, nil) { }) {
return return
} }
} }
@ -119,10 +119,10 @@ func (i *Image) resolvePendingFill() {
func (i *Image) MarkDisposed() { func (i *Image) MarkDisposed() {
if maybeCanAddDelayedCommand() { if maybeCanAddDelayedCommand() {
if tryAddDelayedCommand(func(obj interface{}) error { if tryAddDelayedCommand(func() error {
i.MarkDisposed() i.MarkDisposed()
return nil return nil
}, nil) { }) {
return return
} }
} }
@ -172,10 +172,10 @@ 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 maybeCanAddDelayedCommand() {
if tryAddDelayedCommand(func(obj interface{}) error { if tryAddDelayedCommand(func() error {
i.Fill(clr) i.Fill(clr)
return nil return nil
}, nil) { }) {
return return
} }
} }
@ -192,13 +192,11 @@ func (i *Image) ReplacePixels(pix []byte, x, y, width, height int) error {
} }
if maybeCanAddDelayedCommand() { 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)) copied := make([]byte, len(pix))
copy(copied, pix) copy(copied, pix)
return copied if tryAddDelayedCommand(func() error {
i.ReplacePixels(copied, x, y, width, height)
return nil
}) { }) {
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 maybeCanAddDelayedCommand() {
if tryAddDelayedCommand(func(obj interface{}) error { if tryAddDelayedCommand(func() error {
i.drawImage(src, bounds, g, colorm, mode, filter) i.drawImage(src, bounds, g, colorm, mode, filter)
return nil return nil
}, nil) { }) {
return return
} }
} }
@ -288,11 +286,11 @@ func (i *Image) DrawTriangles(src *Image, vertices []float32, indices []uint16,
} }
if maybeCanAddDelayedCommand() { if maybeCanAddDelayedCommand() {
if tryAddDelayedCommand(func(obj interface{}) error { if tryAddDelayedCommand(func() 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)
return nil return nil
}, nil) { }) {
return return
} }
} }