mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-25 03:08:54 +01:00
parent
dea3785750
commit
09322dfdc8
@ -64,16 +64,14 @@ func getDelayedFuncsAndClearSlow() []func() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
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
|
||||
// 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 tryAddDelayedCommandSlow(f func(obj interface{}) error, ondelayed func() interface{}) bool {
|
||||
func tryAddDelayedCommand(f func(obj interface{}) error, ondelayed func() interface{}) bool {
|
||||
delayedCommandsM.Lock()
|
||||
defer delayedCommandsM.Unlock()
|
||||
|
||||
|
@ -55,12 +55,14 @@ 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 {
|
||||
i.initialize(width, height, volatile)
|
||||
return nil
|
||||
}, nil) {
|
||||
return
|
||||
}
|
||||
}
|
||||
i.img = mipmap.New(width, height, volatile)
|
||||
i.width = width
|
||||
i.height = height
|
||||
@ -73,12 +75,14 @@ func NewScreenFramebufferImage(width, height int) *Image {
|
||||
}
|
||||
|
||||
func (i *Image) initializeAsScreenFramebuffer(width, height int) {
|
||||
if maybeCanAddDelayedCommand() {
|
||||
if tryAddDelayedCommand(func(obj interface{}) error {
|
||||
i.initializeAsScreenFramebuffer(width, height)
|
||||
return nil
|
||||
}, nil) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
i.img = mipmap.NewScreenFramebufferMipmap(width, height)
|
||||
i.width = width
|
||||
@ -114,12 +118,14 @@ func (i *Image) resolvePendingFill() {
|
||||
}
|
||||
|
||||
func (i *Image) MarkDisposed() {
|
||||
if maybeCanAddDelayedCommand() {
|
||||
if tryAddDelayedCommand(func(obj interface{}) error {
|
||||
i.MarkDisposed()
|
||||
return nil
|
||||
}, nil) {
|
||||
return
|
||||
}
|
||||
}
|
||||
i.invalidatePendingPixels()
|
||||
i.img.MarkDisposed()
|
||||
}
|
||||
@ -165,12 +171,14 @@ func (i *Image) Dump(name string, blackbg bool) error {
|
||||
}
|
||||
|
||||
func (i *Image) Fill(clr color.RGBA) {
|
||||
if maybeCanAddDelayedCommand() {
|
||||
if tryAddDelayedCommand(func(obj interface{}) error {
|
||||
i.Fill(clr)
|
||||
return nil
|
||||
}, nil) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Defer filling the image so that successive fillings will be merged into one (#1134).
|
||||
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))
|
||||
}
|
||||
|
||||
if maybeCanAddDelayedCommand() {
|
||||
if tryAddDelayedCommand(func(copied interface{}) error {
|
||||
i.ReplacePixels(copied.([]byte), x, y, width, height)
|
||||
return nil
|
||||
@ -193,6 +202,7 @@ func (i *Image) ReplacePixels(pix []byte, x, y, width, height int) error {
|
||||
}) {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
if x == 0 && y == 0 && width == i.width && height == i.height {
|
||||
i.invalidatePendingPixels()
|
||||
@ -238,12 +248,14 @@ func (i *Image) DrawImage(src *Image, bounds image.Rectangle, a, b, c, d, tx, ty
|
||||
Ty: ty,
|
||||
}
|
||||
|
||||
if maybeCanAddDelayedCommand() {
|
||||
if tryAddDelayedCommand(func(obj interface{}) error {
|
||||
i.drawImage(src, bounds, g, colorm, mode, filter)
|
||||
return nil
|
||||
}, nil) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
// Arguments are not copied. Copying is the caller's responsibility.
|
||||
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) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
for _, src := range srcs {
|
||||
src.resolvePendingPixels(true)
|
||||
|
Loading…
Reference in New Issue
Block a user