mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-25 11:18:54 +01:00
graphics: Remove delayedImageTasks
This commit is contained in:
parent
32c2cb3ead
commit
fabed66b4a
@ -30,7 +30,7 @@ type graphicsContext struct {
|
||||
screen *Image
|
||||
defaultRenderTarget *Image
|
||||
screenScale int
|
||||
imageTasksDone bool
|
||||
initialized bool
|
||||
}
|
||||
|
||||
func (c *graphicsContext) SetSize(screenWidth, screenHeight, screenScale int) error {
|
||||
@ -55,16 +55,11 @@ func (c *graphicsContext) SetSize(screenWidth, screenHeight, screenScale int) er
|
||||
}
|
||||
|
||||
func (c *graphicsContext) Update() error {
|
||||
if !c.imageTasksDone {
|
||||
if !c.initialized {
|
||||
if err := graphics.Initialize(ui.GLContext()); err != nil {
|
||||
return err
|
||||
}
|
||||
// This execution is called here because we can say actual GL function calls
|
||||
// should be done here (especailly on mobiles).
|
||||
if err := theDelayedImageTasks.exec(); err != nil {
|
||||
return err
|
||||
}
|
||||
c.imageTasksDone = true
|
||||
c.initialized = true
|
||||
}
|
||||
if err := c.screen.Clear(); err != nil {
|
||||
return err
|
||||
@ -104,7 +99,7 @@ func (c *graphicsContext) flush() error {
|
||||
|
||||
func (c *graphicsContext) Resume() error {
|
||||
ui.GLContext().Resume()
|
||||
if !c.imageTasksDone {
|
||||
if !c.initialized {
|
||||
return nil
|
||||
}
|
||||
if err := graphics.Initialize(ui.GLContext()); err != nil {
|
||||
|
79
image.go
79
image.go
@ -33,38 +33,6 @@ var (
|
||||
imageM sync.Mutex
|
||||
)
|
||||
|
||||
type delayedImageTasks struct {
|
||||
tasks []func() error
|
||||
m sync.Mutex
|
||||
execCalled bool
|
||||
}
|
||||
|
||||
var theDelayedImageTasks = &delayedImageTasks{
|
||||
tasks: []func() error{},
|
||||
}
|
||||
|
||||
func (t *delayedImageTasks) add(f func() error) bool {
|
||||
t.m.Lock()
|
||||
defer t.m.Unlock()
|
||||
if t.execCalled {
|
||||
return false
|
||||
}
|
||||
t.tasks = append(t.tasks, f)
|
||||
return true
|
||||
}
|
||||
|
||||
func (t *delayedImageTasks) exec() error {
|
||||
t.m.Lock()
|
||||
defer t.m.Unlock()
|
||||
t.execCalled = true
|
||||
for _, f := range t.tasks {
|
||||
if err := f(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type images struct {
|
||||
images map[*imageImpl]struct{}
|
||||
m sync.Mutex
|
||||
@ -206,7 +174,6 @@ type imageImpl struct {
|
||||
}
|
||||
|
||||
func (i *imageImpl) Fill(clr color.Color) error {
|
||||
f := func() error {
|
||||
imageM.Lock()
|
||||
defer imageM.Unlock()
|
||||
if i.isDisposed() {
|
||||
@ -214,11 +181,6 @@ func (i *imageImpl) Fill(clr color.Color) error {
|
||||
}
|
||||
i.pixels = nil
|
||||
return i.framebuffer.Fill(clr)
|
||||
}
|
||||
if theDelayedImageTasks.add(f) {
|
||||
return nil
|
||||
}
|
||||
return f()
|
||||
}
|
||||
|
||||
func isWholeNumber(x float64) bool {
|
||||
@ -251,7 +213,6 @@ func (i *imageImpl) DrawImage(image *Image, options *DrawImageOptions) error {
|
||||
if i == image.impl {
|
||||
return errors.New("ebiten: Image.DrawImage: image should be different from the receiver")
|
||||
}
|
||||
f := func() error {
|
||||
imageM.Lock()
|
||||
defer imageM.Unlock()
|
||||
if i.isDisposed() {
|
||||
@ -265,11 +226,6 @@ func (i *imageImpl) DrawImage(image *Image, options *DrawImageOptions) error {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if theDelayedImageTasks.add(f) {
|
||||
return nil
|
||||
}
|
||||
return f()
|
||||
}
|
||||
|
||||
func (i *imageImpl) At(x, y int) color.Color {
|
||||
@ -321,7 +277,6 @@ func (i *imageImpl) restorePixels(context *opengl.Context) error {
|
||||
}
|
||||
|
||||
func (i *imageImpl) Dispose() error {
|
||||
f := func() error {
|
||||
imageM.Lock()
|
||||
defer imageM.Unlock()
|
||||
if i.isDisposed() {
|
||||
@ -336,12 +291,6 @@ func (i *imageImpl) Dispose() error {
|
||||
i.pixels = nil
|
||||
runtime.SetFinalizer(i, nil)
|
||||
return nil
|
||||
}
|
||||
|
||||
if theDelayedImageTasks.add(f) {
|
||||
return nil
|
||||
}
|
||||
return f()
|
||||
}
|
||||
|
||||
func (i *imageImpl) isDisposed() bool {
|
||||
@ -352,7 +301,6 @@ func (i *imageImpl) ReplacePixels(p []uint8) error {
|
||||
if l := 4 * i.width * i.height; len(p) != l {
|
||||
return fmt.Errorf("ebiten: p's length must be %d", l)
|
||||
}
|
||||
f := func() error {
|
||||
imageM.Lock()
|
||||
defer imageM.Unlock()
|
||||
// TODO: Copy p?
|
||||
@ -361,11 +309,6 @@ func (i *imageImpl) ReplacePixels(p []uint8) error {
|
||||
return errors.New("ebiten: image is already disposed")
|
||||
}
|
||||
return i.framebuffer.ReplacePixels(i.texture, p)
|
||||
}
|
||||
if theDelayedImageTasks.add(f) {
|
||||
return nil
|
||||
}
|
||||
return f()
|
||||
}
|
||||
|
||||
// A DrawImageOptions represents options to render an image on an image.
|
||||
@ -394,25 +337,16 @@ func NewImage(width, height int, filter Filter) (*Image, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
f := func() error {
|
||||
imageM.Lock()
|
||||
defer imageM.Unlock()
|
||||
texture, framebuffer, err := graphics.NewImage(width, height, glFilter(ui.GLContext(), filter))
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
image.framebuffer = framebuffer
|
||||
image.texture = texture
|
||||
runtime.SetFinalizer(image, (*imageImpl).Dispose)
|
||||
if err := image.framebuffer.Fill(color.Transparent); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if theDelayedImageTasks.add(f) {
|
||||
return eimg, nil
|
||||
}
|
||||
if err := f(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return eimg, nil
|
||||
@ -436,7 +370,6 @@ func NewImageFromImage(source image.Image, filter Filter) (*Image, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
f := func() error {
|
||||
// Don't lock while manipulating an image.Image interface.
|
||||
rgbaImg, ok := source.(*image.RGBA)
|
||||
if !ok {
|
||||
@ -450,19 +383,11 @@ func NewImageFromImage(source image.Image, filter Filter) (*Image, error) {
|
||||
texture, framebuffer, err := graphics.NewImageFromImage(rgbaImg, glFilter(ui.GLContext(), filter))
|
||||
if err != nil {
|
||||
// TODO: texture should be removed here?
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
img.framebuffer = framebuffer
|
||||
img.texture = texture
|
||||
runtime.SetFinalizer(img, (*imageImpl).Dispose)
|
||||
return nil
|
||||
}
|
||||
if theDelayedImageTasks.add(f) {
|
||||
return eimg, nil
|
||||
}
|
||||
if err := f(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return eimg, nil
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user