Refactoring: Don't access restorable package directly from ebiten package

This commit is contained in:
Hajime Hoshi 2018-03-25 23:37:32 +09:00
parent ce8b9283a1
commit 97ce612785
3 changed files with 33 additions and 38 deletions

View File

@ -18,7 +18,7 @@ import (
"github.com/hajimehoshi/ebiten/internal/clock"
"github.com/hajimehoshi/ebiten/internal/graphics"
"github.com/hajimehoshi/ebiten/internal/hooks"
"github.com/hajimehoshi/ebiten/internal/restorable"
"github.com/hajimehoshi/ebiten/internal/shareable"
"github.com/hajimehoshi/ebiten/internal/ui"
"github.com/hajimehoshi/ebiten/internal/web"
)
@ -67,7 +67,7 @@ func (c *graphicsContext) SetSize(screenWidth, screenHeight int, screenScale flo
func (c *graphicsContext) initializeIfNeeded() error {
if !c.initialized {
if err := restorable.InitializeGLState(); err != nil {
if err := shareable.InitializeGLState(); err != nil {
return err
}
c.initialized = true
@ -124,7 +124,7 @@ func (c *graphicsContext) Update(afterFrameUpdate func()) error {
op.Filter = filterScreen
_ = c.screen.DrawImage(c.offscreen, op)
if err := restorable.ResolveStaleImages(); err != nil {
if err := shareable.ResolveStaleImages(); err != nil {
return err
}
return nil
@ -138,7 +138,7 @@ func (c *graphicsContext) needsRestoring() (bool, error) {
}
func (c *graphicsContext) restoreIfNeeded() error {
if !restorable.IsRestoringEnabled() {
if !shareable.IsRestoringEnabled() {
return nil
}
r, err := c.needsRestoring()
@ -148,7 +148,7 @@ func (c *graphicsContext) restoreIfNeeded() error {
if !r {
return nil
}
if err := restorable.Restore(); err != nil {
if err := shareable.Restore(); err != nil {
return err
}
c.invalidated = false

View File

@ -23,7 +23,6 @@ import (
"github.com/hajimehoshi/ebiten/internal/affine"
"github.com/hajimehoshi/ebiten/internal/graphics"
"github.com/hajimehoshi/ebiten/internal/opengl"
"github.com/hajimehoshi/ebiten/internal/sync"
)
// drawImageHistoryItem is an item for history of draw-image commands.
@ -71,8 +70,6 @@ type Image struct {
// screen indicates whether the image is used as an actual screen.
screen bool
m sync.Mutex
}
// NewImage creates an empty image with the given size.
@ -134,9 +131,6 @@ func (i *Image) ReplacePixels(pixels []byte, x, y, width, height int) {
// For this purpuse, images should remember which part of that is used for DrawImage.
theImages.makeStaleIfDependingOn(i)
i.m.Lock()
defer i.m.Unlock()
i.image.ReplacePixels(pixels, x, y, width, height)
if x == 0 && y == 0 && width == w && height == h {
@ -173,9 +167,6 @@ func (i *Image) DrawImage(img *Image, sx0, sy0, sx1, sy1 int, geom *affine.GeoM,
}
theImages.makeStaleIfDependingOn(i)
i.m.Lock()
defer i.m.Unlock()
if img.stale || img.volatile || i.screen || !IsRestoringEnabled() {
i.makeStale()
} else {
@ -222,9 +213,6 @@ func (i *Image) At(x, y int) (color.RGBA, error) {
return color.RGBA{}, nil
}
i.m.Lock()
defer i.m.Unlock()
if err := graphics.FlushCommands(); err != nil {
return color.RGBA{}, err
}
@ -240,9 +228,6 @@ func (i *Image) At(x, y int) (color.RGBA, error) {
// makeStaleIfDependingOn makes the image stale if the image depends on target.
func (i *Image) makeStaleIfDependingOn(target *Image) {
i.m.Lock()
defer i.m.Unlock()
if i.stale {
return
}
@ -269,9 +254,6 @@ func (i *Image) resolveStale() error {
return nil
}
i.m.Lock()
defer i.m.Unlock()
if i.volatile {
return nil
}
@ -296,9 +278,6 @@ func (i *Image) dependsOn(target *Image) bool {
// dependingImages returns all images that is depended by the image.
func (i *Image) dependingImages() map[*Image]struct{} {
i.m.Lock()
defer i.m.Unlock()
r := map[*Image]struct{}{}
for _, c := range i.drawImageHistory {
r[c.image] = struct{}{}
@ -308,9 +287,6 @@ func (i *Image) dependingImages() map[*Image]struct{} {
// hasDependency returns a boolean value indicating whether the image depends on another image.
func (i *Image) hasDependency() bool {
i.m.Lock()
defer i.m.Unlock()
if i.stale {
return false
}
@ -319,9 +295,6 @@ func (i *Image) hasDependency() bool {
// Restore restores *graphics.Image from the pixels using its state.
func (i *Image) restore() error {
i.m.Lock()
defer i.m.Unlock()
w, h := i.image.Size()
if i.screen {
// The screen image should also be recreated because framebuffer might
@ -380,9 +353,6 @@ func (i *Image) restore() error {
func (i *Image) Dispose() {
theImages.remove(i)
i.m.Lock()
defer i.m.Unlock()
i.image.Dispose()
i.image = nil
i.basePixels = nil
@ -403,8 +373,5 @@ func (i *Image) IsInvalidated() (bool, error) {
return false, nil
}
i.m.Lock()
defer i.m.Unlock()
return i.image.IsInvalidated(), nil
}

View File

@ -269,6 +269,9 @@ func NewImage(width, height int) *Image {
}
func NewVolatileImage(width, height int) *Image {
backendsM.Lock()
defer backendsM.Unlock()
r := restorable.NewImage(width, height, true)
i := &Image{
backend: &backend{
@ -280,6 +283,9 @@ func NewVolatileImage(width, height int) *Image {
}
func NewScreenFramebufferImage(width, height int) *Image {
backendsM.Lock()
defer backendsM.Unlock()
r := restorable.NewScreenFramebufferImage(width, height)
i := &Image{
backend: &backend{
@ -289,3 +295,25 @@ func NewScreenFramebufferImage(width, height int) *Image {
runtime.SetFinalizer(i, (*Image).Dispose)
return i
}
func InitializeGLState() error {
backendsM.Lock()
defer backendsM.Unlock()
return restorable.InitializeGLState()
}
func ResolveStaleImages() error {
backendsM.Lock()
defer backendsM.Unlock()
return restorable.ResolveStaleImages()
}
func IsRestoringEnabled() bool {
return restorable.IsRestoringEnabled()
}
func Restore() error {
backendsM.Lock()
defer backendsM.Unlock()
return restorable.Restore()
}