Rename pixels -> restorable

This commit is contained in:
Hajime Hoshi 2016-09-03 22:05:05 +09:00
parent 071b65f173
commit 9c079917f1
2 changed files with 40 additions and 40 deletions

View File

@ -25,19 +25,19 @@ import (
"github.com/hajimehoshi/ebiten/internal/graphics"
"github.com/hajimehoshi/ebiten/internal/graphics/opengl"
"github.com/hajimehoshi/ebiten/internal/pixels"
"github.com/hajimehoshi/ebiten/internal/restorable"
)
type imageImpl struct {
image *graphics.Image
disposed bool
width int
height int
filter Filter
pixels pixels.Pixels
volatile bool
screen bool
m sync.Mutex
image *graphics.Image
disposed bool
width int
height int
filter Filter
restorable restorable.Image
volatile bool
screen bool
m sync.Mutex
}
func newImageImpl(width, height int, filter Filter, volatile bool) (*imageImpl, error) {
@ -84,7 +84,7 @@ func newImageImplFromImage(source image.Image, filter Filter) (*imageImpl, error
height: h,
filter: filter,
}
i.pixels.ReplacePixels(p)
i.restorable.ReplacePixels(p)
runtime.SetFinalizer(i, (*imageImpl).Dispose)
return i, nil
}
@ -112,7 +112,7 @@ func (i *imageImpl) Fill(clr color.Color) error {
return errors.New("ebiten: image is already disposed")
}
rgba := color.RGBAModel.Convert(clr).(color.RGBA)
i.pixels.Fill(rgba)
i.restorable.Fill(rgba)
return i.image.Fill(rgba)
}
@ -125,7 +125,7 @@ func (i *imageImpl) clearIfVolatile() error {
if !i.volatile {
return nil
}
i.pixels.Clear()
i.restorable.Clear()
return i.image.Fill(color.RGBA{})
}
@ -163,10 +163,10 @@ func (i *imageImpl) DrawImage(image *Image, options *DrawImageOptions) error {
geom := options.GeoM
colorm := options.ColorM
mode := opengl.CompositeMode(options.CompositeMode)
if image.impl.pixels.IsStale() {
i.pixels.MakeStale()
if image.impl.restorable.IsStale() {
i.restorable.MakeStale()
} else {
i.pixels.AppendDrawImageHistory(image.impl.image, vertices, &geom, &colorm, mode)
i.restorable.AppendDrawImageHistory(image.impl.image, vertices, &geom, &colorm, mode)
}
if err := i.image.DrawImage(image.impl.image, vertices, &geom, &colorm, mode); err != nil {
return err
@ -184,7 +184,7 @@ func (i *imageImpl) At(x, y int, context *opengl.Context) color.Color {
return color.Transparent
}
idx := 4*x + 4*y*i.width
clr, err := i.pixels.At(idx, i.image, context)
clr, err := i.restorable.At(idx, i.image, context)
if err != nil {
panic(err)
}
@ -200,7 +200,7 @@ func (i *imageImpl) resolveStalePixels(context *opengl.Context) error {
if i.volatile {
return nil
}
if err := i.pixels.ReadPixelsFromVRAMIfStale(i.image, context); err != nil {
if err := i.restorable.ReadPixelsFromVRAMIfStale(i.image, context); err != nil {
return err
}
return nil
@ -220,17 +220,17 @@ func (i *imageImpl) resetPixelsIfDependingOn(target *imageImpl, context *opengl.
}
// target is an image that is about to be tried mutating.
// If pixels object is related to that image, the pixels must be reset.
if !i.pixels.DependsOn(target.image) {
if !i.restorable.DependsOn(target.image) {
return nil
}
i.pixels.MakeStale()
i.restorable.MakeStale()
return nil
}
func (i *imageImpl) hasDependency() bool {
i.m.Lock()
defer i.m.Unlock()
return i.pixels.HasDependency()
return i.restorable.HasDependency()
}
func (i *imageImpl) restore(context *opengl.Context) error {
@ -258,7 +258,7 @@ func (i *imageImpl) restore(context *opengl.Context) error {
return nil
}
var err error
i.image, err = i.pixels.CreateImage(context, i.width, i.height, glFilter(i.filter))
i.image, err = i.restorable.CreateImage(context, i.width, i.height, glFilter(i.filter))
if err != nil {
return err
}
@ -278,7 +278,7 @@ func (i *imageImpl) Dispose() error {
}
i.image = nil
i.disposed = true
i.pixels.Clear()
i.restorable.Clear()
runtime.SetFinalizer(i, nil)
return nil
}
@ -289,7 +289,7 @@ func (i *imageImpl) ReplacePixels(p []uint8) error {
}
i.m.Lock()
defer i.m.Unlock()
i.pixels.ReplacePixels(p)
i.restorable.ReplacePixels(p)
if i.disposed {
return errors.New("ebiten: image is already disposed")
}

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package pixels
package restorable
import (
"errors"
@ -31,41 +31,41 @@ type drawImageHistoryItem struct {
mode opengl.CompositeMode
}
// Pixels represents pixels of an image for restoring when GL context is lost.
type Pixels struct {
// basePixels and baseColor are exclusive.
// Image represents an image of an image for restoring when GL context is lost.
type Image struct {
// baseImage and baseColor are exclusive.
basePixels []uint8
baseColor color.RGBA
drawImageHistory []*drawImageHistoryItem
stale bool
}
func (p *Pixels) IsStale() bool {
func (p *Image) IsStale() bool {
return p.stale
}
func (p *Pixels) MakeStale() {
func (p *Image) MakeStale() {
p.basePixels = nil
p.baseColor = color.RGBA{}
p.drawImageHistory = nil
p.stale = true
}
func (p *Pixels) Clear() {
func (p *Image) Clear() {
p.basePixels = nil
p.baseColor = color.RGBA{}
p.drawImageHistory = nil
p.stale = false
}
func (p *Pixels) Fill(clr color.RGBA) {
func (p *Image) Fill(clr color.RGBA) {
p.basePixels = nil
p.baseColor = clr
p.drawImageHistory = nil
p.stale = false
}
func (p *Pixels) ReplacePixels(pixels []uint8) {
func (p *Image) ReplacePixels(pixels []uint8) {
if p.basePixels == nil {
p.basePixels = make([]uint8, len(pixels))
}
@ -75,7 +75,7 @@ func (p *Pixels) ReplacePixels(pixels []uint8) {
p.stale = false
}
func (p *Pixels) AppendDrawImageHistory(image *graphics.Image, vertices []int16, geom graphics.Matrix, colorm graphics.Matrix, mode opengl.CompositeMode) {
func (p *Image) AppendDrawImageHistory(image *graphics.Image, vertices []int16, geom graphics.Matrix, colorm graphics.Matrix, mode opengl.CompositeMode) {
if p.stale {
return
}
@ -95,7 +95,7 @@ func (p *Pixels) AppendDrawImageHistory(image *graphics.Image, vertices []int16,
//
// Note that this must not be called until context is available.
// This means Pixels members must match with acutal state in VRAM.
func (p *Pixels) At(idx int, image *graphics.Image, context *opengl.Context) (color.RGBA, error) {
func (p *Image) At(idx int, image *graphics.Image, context *opengl.Context) (color.RGBA, error) {
if p.basePixels == nil || p.drawImageHistory != nil || p.stale {
if err := p.readPixelsFromVRAM(image, context); err != nil {
return color.RGBA{}, err
@ -105,7 +105,7 @@ func (p *Pixels) At(idx int, image *graphics.Image, context *opengl.Context) (co
return color.RGBA{r, g, b, a}, nil
}
func (p *Pixels) DependsOn(target *graphics.Image) bool {
func (p *Image) DependsOn(target *graphics.Image) bool {
if p.stale {
return false
}
@ -118,7 +118,7 @@ func (p *Pixels) DependsOn(target *graphics.Image) bool {
return false
}
func (p *Pixels) readPixelsFromVRAM(image *graphics.Image, context *opengl.Context) error {
func (p *Image) readPixelsFromVRAM(image *graphics.Image, context *opengl.Context) error {
var err error
p.basePixels, err = image.Pixels(context)
if err != nil {
@ -130,14 +130,14 @@ func (p *Pixels) readPixelsFromVRAM(image *graphics.Image, context *opengl.Conte
return nil
}
func (p *Pixels) ReadPixelsFromVRAMIfStale(image *graphics.Image, context *opengl.Context) error {
func (p *Image) ReadPixelsFromVRAMIfStale(image *graphics.Image, context *opengl.Context) error {
if !p.stale {
return nil
}
return p.readPixelsFromVRAM(image, context)
}
func (p *Pixels) HasDependency() bool {
func (p *Image) HasDependency() bool {
if p.stale {
return false
}
@ -145,7 +145,7 @@ func (p *Pixels) HasDependency() bool {
}
// CreateImage restores *graphics.Image from the pixels using its state.
func (p *Pixels) CreateImage(context *opengl.Context, width, height int, filter opengl.Filter) (*graphics.Image, error) {
func (p *Image) CreateImage(context *opengl.Context, width, height int, filter opengl.Filter) (*graphics.Image, error) {
if p.stale {
return nil, errors.New("pixels: pixels must not be stale when restoring")
}