shareable: Rename variables

This commit is contained in:
Hajime Hoshi 2018-04-09 02:18:46 +09:00
parent c976ccc66c
commit 070c8ec737

View File

@ -87,44 +87,44 @@ type Image struct {
node *packing.Node node *packing.Node
} }
func (s *Image) ensureNotShared() { func (i *Image) ensureNotShared() {
if s.node == nil { if i.node == nil {
return return
} }
x, y, w, h := s.region() x, y, w, h := i.region()
newImg := restorable.NewImage(w, h, false) newImg := restorable.NewImage(w, h, false)
newImg.DrawImage(s.backend.restorable, x, y, x+w, y+h, nil, nil, opengl.CompositeModeCopy, graphics.FilterNearest) newImg.DrawImage(i.backend.restorable, x, y, x+w, y+h, nil, nil, opengl.CompositeModeCopy, graphics.FilterNearest)
s.dispose() i.dispose()
s.backend = &backend{ i.backend = &backend{
restorable: newImg, restorable: newImg,
} }
} }
func (s *Image) region() (x, y, width, height int) { func (i *Image) region() (x, y, width, height int) {
if s.node == nil { if i.node == nil {
w, h := s.backend.restorable.Size() w, h := i.backend.restorable.Size()
return 0, 0, w, h return 0, 0, w, h
} }
return s.node.Region() return i.node.Region()
} }
func (s *Image) Size() (width, height int) { func (i *Image) Size() (width, height int) {
backendsM.Lock() backendsM.Lock()
defer backendsM.Unlock() defer backendsM.Unlock()
_, _, w, h := s.region() _, _, w, h := i.region()
return w, h return w, h
} }
func (s *Image) DrawImage(img *Image, sx0, sy0, sx1, sy1 int, geom *affine.GeoM, colorm *affine.ColorM, mode opengl.CompositeMode, filter graphics.Filter) { func (i *Image) DrawImage(img *Image, sx0, sy0, sx1, sy1 int, geom *affine.GeoM, colorm *affine.ColorM, mode opengl.CompositeMode, filter graphics.Filter) {
backendsM.Lock() backendsM.Lock()
defer backendsM.Unlock() defer backendsM.Unlock()
s.ensureNotShared() i.ensureNotShared()
// Compare i and img after ensuring i is not shared, or // Compare i and img after ensuring i is not shared, or
// i and img might share the same texture even though i != img. // i and img might share the same texture even though i != img.
if s.backend.restorable == img.backend.restorable { if i.backend.restorable == img.backend.restorable {
panic("shareable: Image.DrawImage: img must be different from the receiver") panic("shareable: Image.DrawImage: img must be different from the receiver")
} }
@ -133,71 +133,71 @@ func (s *Image) DrawImage(img *Image, sx0, sy0, sx1, sy1 int, geom *affine.GeoM,
sy0 += dy sy0 += dy
sx1 += dx sx1 += dx
sy1 += dy sy1 += dy
s.backend.restorable.DrawImage(img.backend.restorable, sx0, sy0, sx1, sy1, geom, colorm, mode, filter) i.backend.restorable.DrawImage(img.backend.restorable, sx0, sy0, sx1, sy1, geom, colorm, mode, filter)
} }
func (s *Image) ReplacePixels(p []byte) { func (i *Image) ReplacePixels(p []byte) {
backendsM.Lock() backendsM.Lock()
defer backendsM.Unlock() defer backendsM.Unlock()
x, y, w, h := s.region() x, y, w, h := i.region()
if l := 4 * w * h; len(p) != l { if l := 4 * w * h; len(p) != l {
panic(fmt.Sprintf("shareable: len(p) was %d but must be %d", len(p), l)) panic(fmt.Sprintf("shareable: len(p) was %d but must be %d", len(p), l))
} }
s.backend.restorable.ReplacePixels(p, x, y, w, h) i.backend.restorable.ReplacePixels(p, x, y, w, h)
} }
func (s *Image) At(x, y int) (color.Color, error) { func (i *Image) At(x, y int) (color.Color, error) {
backendsM.Lock() backendsM.Lock()
defer backendsM.Unlock() defer backendsM.Unlock()
ox, oy, w, h := s.region() ox, oy, w, h := i.region()
if x < 0 || y < 0 || x >= w || y >= h { if x < 0 || y < 0 || x >= w || y >= h {
return color.RGBA{}, nil return color.RGBA{}, nil
} }
clr, err := s.backend.restorable.At(x+ox, y+oy) clr, err := i.backend.restorable.At(x+ox, y+oy)
return clr, err return clr, err
} }
func (s *Image) isDisposed() bool { func (i *Image) isDisposed() bool {
return s.backend == nil return i.backend == nil
} }
func (s *Image) Dispose() { func (i *Image) Dispose() {
backendsM.Lock() backendsM.Lock()
defer backendsM.Unlock() defer backendsM.Unlock()
s.dispose() i.dispose()
} }
func (s *Image) dispose() { func (i *Image) dispose() {
if s.isDisposed() { if i.isDisposed() {
return return
} }
defer func() { defer func() {
s.backend = nil i.backend = nil
s.node = nil i.node = nil
runtime.SetFinalizer(s, nil) runtime.SetFinalizer(i, nil)
}() }()
if s.node == nil { if i.node == nil {
s.backend.restorable.Dispose() i.backend.restorable.Dispose()
return return
} }
s.backend.page.Free(s.node) i.backend.page.Free(i.node)
if !s.backend.page.IsEmpty() { if !i.backend.page.IsEmpty() {
// As this part can be reused, this should be cleared explicitly. // As this part can be reused, this should be cleared explicitly.
s.backend.restorable.Clear(s.region()) i.backend.restorable.Clear(i.region())
return return
} }
s.backend.restorable.Dispose() i.backend.restorable.Dispose()
index := -1 index := -1
for i, sh := range theBackends { for idx, sh := range theBackends {
if sh == s.backend { if sh == i.backend {
index = i index = idx
break break
} }
} }
@ -207,10 +207,10 @@ func (s *Image) dispose() {
theBackends = append(theBackends[:index], theBackends[index+1:]...) theBackends = append(theBackends[:index], theBackends[index+1:]...)
} }
func (s *Image) IsInvalidated() (bool, error) { func (i *Image) IsInvalidated() (bool, error) {
backendsM.Lock() backendsM.Lock()
defer backendsM.Unlock() defer backendsM.Unlock()
v, err := s.backend.restorable.IsInvalidated() v, err := i.backend.restorable.IsInvalidated()
return v, err return v, err
} }