graphics: Remove delayedImageTasks

This commit is contained in:
Hajime Hoshi 2016-06-12 00:32:35 +09:00
parent 32c2cb3ead
commit fabed66b4a
2 changed files with 64 additions and 144 deletions

View File

@ -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 {

View File

@ -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() {
@ -215,11 +182,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 {
return x == float64(int64(x))
@ -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() {
@ -266,11 +227,6 @@ func (i *imageImpl) DrawImage(image *Image, options *DrawImageOptions) error {
}
return nil
}
if theDelayedImageTasks.add(f) {
return nil
}
return f()
}
func (i *imageImpl) At(x, y int) color.Color {
if !loop.IsRunning() {
@ -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() {
@ -338,12 +293,6 @@ func (i *imageImpl) Dispose() error {
return nil
}
if theDelayedImageTasks.add(f) {
return nil
}
return f()
}
func (i *imageImpl) isDisposed() bool {
return i.disposed
}
@ -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?
@ -362,11 +310,6 @@ func (i *imageImpl) ReplacePixels(p []uint8) error {
}
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.
type DrawImageOptions struct {
@ -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
}