diff --git a/graphicscontext.go b/graphicscontext.go index f5871f857..df3e68cf5 100644 --- a/graphicscontext.go +++ b/graphicscontext.go @@ -95,9 +95,6 @@ func (c *graphicsContext) initializeIfNeeded(context *opengl.Context) error { if err := graphics.Reset(context); err != nil { return err } - if err := theImagesForRestoring.resetPixels(context); err != nil { - return err - } atomic.StoreInt32(&c.initialized, 1) } r, err := c.needsRestoring(context) @@ -110,7 +107,6 @@ func (c *graphicsContext) initializeIfNeeded(context *opengl.Context) error { if err := c.restore(context); err != nil { return err } - return nil } @@ -144,6 +140,9 @@ func (c *graphicsContext) UpdateAndDraw(context *opengl.Context, updateCount int if err := c.initializeIfNeeded(context); err != nil { return err } + if err := theImagesForRestoring.resetPixels(context); err != nil { + return err + } for i := 0; i < updateCount; i++ { if err := theImagesForRestoring.clearVolatileImages(); err != nil { return err diff --git a/imageimpl.go b/imageimpl.go index f5f93fd88..d5e821ad4 100644 --- a/imageimpl.go +++ b/imageimpl.go @@ -115,6 +115,9 @@ func (i *imageImpl) Fill(clr color.Color) error { if i.disposed { return errors.New("ebiten: image is already disposed") } + if i.pixels == nil { + i.pixels = pixels.NewPixels(i.image) + } i.pixels.Fill(clr) return i.image.Fill(clr) } @@ -128,6 +131,9 @@ func (i *imageImpl) clearIfVolatile() error { if !i.volatile { return nil } + if i.pixels == nil { + i.pixels = pixels.NewPixels(i.image) + } i.pixels.Clear() return i.image.Fill(color.Transparent) } @@ -165,8 +171,10 @@ func (i *imageImpl) DrawImage(image *Image, options *DrawImageOptions) error { } geom := options.GeoM colorm := options.ColorM - i.pixels.AppendDrawImageHistory(image.impl.image, vertices, &geom, &colorm, opengl.CompositeMode(options.CompositeMode)) mode := opengl.CompositeMode(options.CompositeMode) + if i.pixels != nil { + i.pixels.AppendDrawImageHistory(image.impl.image, vertices, &geom, &colorm, mode) + } if err := i.image.DrawImage(image.impl.image, vertices, &geom, &colorm, mode); err != nil { return err } @@ -196,6 +204,10 @@ func (i *imageImpl) resetPixels(context *opengl.Context) error { if i.disposed { return nil } + if i.pixels != nil { + return nil + } + i.pixels = pixels.NewPixels(i.image) if err := i.pixels.Reset(context); err != nil { return err } @@ -216,7 +228,7 @@ func (i *imageImpl) resetPixelsIfNeeded(target *imageImpl, context *opengl.Conte } if context == nil { // context is null when this is not initialized yet. - // In this case, pixels is inconsistent with the image. + i.pixels = nil return nil } if err := i.pixels.ResetIfNeeded(target.image, context); err != nil { @@ -228,6 +240,9 @@ func (i *imageImpl) resetPixelsIfNeeded(target *imageImpl, context *opengl.Conte func (i *imageImpl) hasHistory() bool { i.m.Lock() defer i.m.Unlock() + if i.pixels == nil { + return false + } return i.pixels.HasHistory() } @@ -287,6 +302,9 @@ func (i *imageImpl) ReplacePixels(p []uint8) error { } i.m.Lock() defer i.m.Unlock() + if i.pixels == nil { + i.pixels = pixels.NewPixels(i.image) + } i.pixels.ResetWithPixels(p) if i.disposed { return errors.New("ebiten: image is already disposed") diff --git a/internal/pixels/pixels.go b/internal/pixels/pixels.go index 9dfa1d096..a9fb0bd53 100644 --- a/internal/pixels/pixels.go +++ b/internal/pixels/pixels.go @@ -31,10 +31,6 @@ type drawImageHistoryItem struct { } // Pixels represents pixels of an image for restoring when GL context is lost. -// -// Until GL context is available, -// Pixels member states might not match with actual pixels in GPU so -// there is few function to retrieve the current state. type Pixels struct { image *graphics.Image