mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-24 18:58:54 +01:00
Rename pixels -> restorable
This commit is contained in:
parent
071b65f173
commit
9c079917f1
48
imageimpl.go
48
imageimpl.go
@ -25,19 +25,19 @@ import (
|
|||||||
|
|
||||||
"github.com/hajimehoshi/ebiten/internal/graphics"
|
"github.com/hajimehoshi/ebiten/internal/graphics"
|
||||||
"github.com/hajimehoshi/ebiten/internal/graphics/opengl"
|
"github.com/hajimehoshi/ebiten/internal/graphics/opengl"
|
||||||
"github.com/hajimehoshi/ebiten/internal/pixels"
|
"github.com/hajimehoshi/ebiten/internal/restorable"
|
||||||
)
|
)
|
||||||
|
|
||||||
type imageImpl struct {
|
type imageImpl struct {
|
||||||
image *graphics.Image
|
image *graphics.Image
|
||||||
disposed bool
|
disposed bool
|
||||||
width int
|
width int
|
||||||
height int
|
height int
|
||||||
filter Filter
|
filter Filter
|
||||||
pixels pixels.Pixels
|
restorable restorable.Image
|
||||||
volatile bool
|
volatile bool
|
||||||
screen bool
|
screen bool
|
||||||
m sync.Mutex
|
m sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func newImageImpl(width, height int, filter Filter, volatile bool) (*imageImpl, error) {
|
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,
|
height: h,
|
||||||
filter: filter,
|
filter: filter,
|
||||||
}
|
}
|
||||||
i.pixels.ReplacePixels(p)
|
i.restorable.ReplacePixels(p)
|
||||||
runtime.SetFinalizer(i, (*imageImpl).Dispose)
|
runtime.SetFinalizer(i, (*imageImpl).Dispose)
|
||||||
return i, nil
|
return i, nil
|
||||||
}
|
}
|
||||||
@ -112,7 +112,7 @@ func (i *imageImpl) Fill(clr color.Color) error {
|
|||||||
return errors.New("ebiten: image is already disposed")
|
return errors.New("ebiten: image is already disposed")
|
||||||
}
|
}
|
||||||
rgba := color.RGBAModel.Convert(clr).(color.RGBA)
|
rgba := color.RGBAModel.Convert(clr).(color.RGBA)
|
||||||
i.pixels.Fill(rgba)
|
i.restorable.Fill(rgba)
|
||||||
return i.image.Fill(rgba)
|
return i.image.Fill(rgba)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -125,7 +125,7 @@ func (i *imageImpl) clearIfVolatile() error {
|
|||||||
if !i.volatile {
|
if !i.volatile {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
i.pixels.Clear()
|
i.restorable.Clear()
|
||||||
return i.image.Fill(color.RGBA{})
|
return i.image.Fill(color.RGBA{})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -163,10 +163,10 @@ func (i *imageImpl) DrawImage(image *Image, options *DrawImageOptions) error {
|
|||||||
geom := options.GeoM
|
geom := options.GeoM
|
||||||
colorm := options.ColorM
|
colorm := options.ColorM
|
||||||
mode := opengl.CompositeMode(options.CompositeMode)
|
mode := opengl.CompositeMode(options.CompositeMode)
|
||||||
if image.impl.pixels.IsStale() {
|
if image.impl.restorable.IsStale() {
|
||||||
i.pixels.MakeStale()
|
i.restorable.MakeStale()
|
||||||
} else {
|
} 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 {
|
if err := i.image.DrawImage(image.impl.image, vertices, &geom, &colorm, mode); err != nil {
|
||||||
return err
|
return err
|
||||||
@ -184,7 +184,7 @@ func (i *imageImpl) At(x, y int, context *opengl.Context) color.Color {
|
|||||||
return color.Transparent
|
return color.Transparent
|
||||||
}
|
}
|
||||||
idx := 4*x + 4*y*i.width
|
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 {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
@ -200,7 +200,7 @@ func (i *imageImpl) resolveStalePixels(context *opengl.Context) error {
|
|||||||
if i.volatile {
|
if i.volatile {
|
||||||
return nil
|
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 err
|
||||||
}
|
}
|
||||||
return nil
|
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.
|
// 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 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
|
return nil
|
||||||
}
|
}
|
||||||
i.pixels.MakeStale()
|
i.restorable.MakeStale()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *imageImpl) hasDependency() bool {
|
func (i *imageImpl) hasDependency() bool {
|
||||||
i.m.Lock()
|
i.m.Lock()
|
||||||
defer i.m.Unlock()
|
defer i.m.Unlock()
|
||||||
return i.pixels.HasDependency()
|
return i.restorable.HasDependency()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *imageImpl) restore(context *opengl.Context) error {
|
func (i *imageImpl) restore(context *opengl.Context) error {
|
||||||
@ -258,7 +258,7 @@ func (i *imageImpl) restore(context *opengl.Context) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
var err error
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -278,7 +278,7 @@ func (i *imageImpl) Dispose() error {
|
|||||||
}
|
}
|
||||||
i.image = nil
|
i.image = nil
|
||||||
i.disposed = true
|
i.disposed = true
|
||||||
i.pixels.Clear()
|
i.restorable.Clear()
|
||||||
runtime.SetFinalizer(i, nil)
|
runtime.SetFinalizer(i, nil)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -289,7 +289,7 @@ func (i *imageImpl) ReplacePixels(p []uint8) error {
|
|||||||
}
|
}
|
||||||
i.m.Lock()
|
i.m.Lock()
|
||||||
defer i.m.Unlock()
|
defer i.m.Unlock()
|
||||||
i.pixels.ReplacePixels(p)
|
i.restorable.ReplacePixels(p)
|
||||||
if i.disposed {
|
if i.disposed {
|
||||||
return errors.New("ebiten: image is already disposed")
|
return errors.New("ebiten: image is already disposed")
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,7 @@
|
|||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
package pixels
|
package restorable
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
@ -31,41 +31,41 @@ type drawImageHistoryItem struct {
|
|||||||
mode opengl.CompositeMode
|
mode opengl.CompositeMode
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pixels represents pixels of an image for restoring when GL context is lost.
|
// Image represents an image of an image for restoring when GL context is lost.
|
||||||
type Pixels struct {
|
type Image struct {
|
||||||
// basePixels and baseColor are exclusive.
|
// baseImage and baseColor are exclusive.
|
||||||
basePixels []uint8
|
basePixels []uint8
|
||||||
baseColor color.RGBA
|
baseColor color.RGBA
|
||||||
drawImageHistory []*drawImageHistoryItem
|
drawImageHistory []*drawImageHistoryItem
|
||||||
stale bool
|
stale bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Pixels) IsStale() bool {
|
func (p *Image) IsStale() bool {
|
||||||
return p.stale
|
return p.stale
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Pixels) MakeStale() {
|
func (p *Image) MakeStale() {
|
||||||
p.basePixels = nil
|
p.basePixels = nil
|
||||||
p.baseColor = color.RGBA{}
|
p.baseColor = color.RGBA{}
|
||||||
p.drawImageHistory = nil
|
p.drawImageHistory = nil
|
||||||
p.stale = true
|
p.stale = true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Pixels) Clear() {
|
func (p *Image) Clear() {
|
||||||
p.basePixels = nil
|
p.basePixels = nil
|
||||||
p.baseColor = color.RGBA{}
|
p.baseColor = color.RGBA{}
|
||||||
p.drawImageHistory = nil
|
p.drawImageHistory = nil
|
||||||
p.stale = false
|
p.stale = false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Pixels) Fill(clr color.RGBA) {
|
func (p *Image) Fill(clr color.RGBA) {
|
||||||
p.basePixels = nil
|
p.basePixels = nil
|
||||||
p.baseColor = clr
|
p.baseColor = clr
|
||||||
p.drawImageHistory = nil
|
p.drawImageHistory = nil
|
||||||
p.stale = false
|
p.stale = false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Pixels) ReplacePixels(pixels []uint8) {
|
func (p *Image) ReplacePixels(pixels []uint8) {
|
||||||
if p.basePixels == nil {
|
if p.basePixels == nil {
|
||||||
p.basePixels = make([]uint8, len(pixels))
|
p.basePixels = make([]uint8, len(pixels))
|
||||||
}
|
}
|
||||||
@ -75,7 +75,7 @@ func (p *Pixels) ReplacePixels(pixels []uint8) {
|
|||||||
p.stale = false
|
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 {
|
if p.stale {
|
||||||
return
|
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.
|
// Note that this must not be called until context is available.
|
||||||
// This means Pixels members must match with acutal state in VRAM.
|
// 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 p.basePixels == nil || p.drawImageHistory != nil || p.stale {
|
||||||
if err := p.readPixelsFromVRAM(image, context); err != nil {
|
if err := p.readPixelsFromVRAM(image, context); err != nil {
|
||||||
return color.RGBA{}, err
|
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
|
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 {
|
if p.stale {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@ -118,7 +118,7 @@ func (p *Pixels) DependsOn(target *graphics.Image) bool {
|
|||||||
return false
|
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
|
var err error
|
||||||
p.basePixels, err = image.Pixels(context)
|
p.basePixels, err = image.Pixels(context)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -130,14 +130,14 @@ func (p *Pixels) readPixelsFromVRAM(image *graphics.Image, context *opengl.Conte
|
|||||||
return nil
|
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 {
|
if !p.stale {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return p.readPixelsFromVRAM(image, context)
|
return p.readPixelsFromVRAM(image, context)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Pixels) HasDependency() bool {
|
func (p *Image) HasDependency() bool {
|
||||||
if p.stale {
|
if p.stale {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@ -145,7 +145,7 @@ func (p *Pixels) HasDependency() bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CreateImage restores *graphics.Image from the pixels using its state.
|
// 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 {
|
if p.stale {
|
||||||
return nil, errors.New("pixels: pixels must not be stale when restoring")
|
return nil, errors.New("pixels: pixels must not be stale when restoring")
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user