mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-26 11:48:55 +01:00
graphics: Add imageRestoringInfo
This commit is contained in:
parent
2ba835373b
commit
3b8d5026c3
110
imageimpl.go
110
imageimpl.go
@ -28,23 +28,13 @@ import (
|
|||||||
"github.com/hajimehoshi/ebiten/internal/loop"
|
"github.com/hajimehoshi/ebiten/internal/loop"
|
||||||
)
|
)
|
||||||
|
|
||||||
type drawImageHistoryItem struct {
|
|
||||||
image *Image
|
|
||||||
vertices []int16
|
|
||||||
geom GeoM
|
|
||||||
colorm ColorM
|
|
||||||
mode opengl.CompositeMode
|
|
||||||
}
|
|
||||||
|
|
||||||
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 []uint8
|
restoringInfo imageRestoringInfo
|
||||||
baseColor color.Color
|
|
||||||
drawImageHistory []*drawImageHistoryItem
|
|
||||||
volatile bool
|
volatile bool
|
||||||
screen bool
|
screen bool
|
||||||
m sync.Mutex
|
m sync.Mutex
|
||||||
@ -61,8 +51,9 @@ func newImageImpl(width, height int, filter Filter, volatile bool) (*imageImpl,
|
|||||||
height: height,
|
height: height,
|
||||||
filter: filter,
|
filter: filter,
|
||||||
volatile: volatile,
|
volatile: volatile,
|
||||||
pixels: make([]uint8, width*height*4),
|
|
||||||
}
|
}
|
||||||
|
i.restoringInfo.imageImpl = i
|
||||||
|
i.restoringInfo.resetWithPixels(make([]uint8, width*height*4))
|
||||||
runtime.SetFinalizer(i, (*imageImpl).Dispose)
|
runtime.SetFinalizer(i, (*imageImpl).Dispose)
|
||||||
return i, nil
|
return i, nil
|
||||||
}
|
}
|
||||||
@ -93,8 +84,9 @@ func newImageImplFromImage(source image.Image, filter Filter) (*imageImpl, error
|
|||||||
width: w,
|
width: w,
|
||||||
height: h,
|
height: h,
|
||||||
filter: filter,
|
filter: filter,
|
||||||
pixels: pixels,
|
|
||||||
}
|
}
|
||||||
|
i.restoringInfo.imageImpl = i
|
||||||
|
i.restoringInfo.resetWithPixels(pixels)
|
||||||
runtime.SetFinalizer(i, (*imageImpl).Dispose)
|
runtime.SetFinalizer(i, (*imageImpl).Dispose)
|
||||||
return i, nil
|
return i, nil
|
||||||
}
|
}
|
||||||
@ -110,8 +102,9 @@ func newScreenImageImpl(width, height int) (*imageImpl, error) {
|
|||||||
height: height,
|
height: height,
|
||||||
volatile: true,
|
volatile: true,
|
||||||
screen: true,
|
screen: true,
|
||||||
pixels: make([]uint8, width*height*4),
|
|
||||||
}
|
}
|
||||||
|
i.restoringInfo.imageImpl = i
|
||||||
|
i.restoringInfo.resetWithPixels(make([]uint8, width*height*4))
|
||||||
runtime.SetFinalizer(i, (*imageImpl).Dispose)
|
runtime.SetFinalizer(i, (*imageImpl).Dispose)
|
||||||
return i, nil
|
return i, nil
|
||||||
}
|
}
|
||||||
@ -122,9 +115,7 @@ func (i *imageImpl) Fill(clr color.Color) error {
|
|||||||
if i.disposed {
|
if i.disposed {
|
||||||
return errors.New("ebiten: image is already disposed")
|
return errors.New("ebiten: image is already disposed")
|
||||||
}
|
}
|
||||||
i.pixels = nil
|
i.restoringInfo.fill(clr)
|
||||||
i.baseColor = clr
|
|
||||||
i.drawImageHistory = nil
|
|
||||||
return i.image.Fill(clr)
|
return i.image.Fill(clr)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -137,9 +128,7 @@ func (i *imageImpl) clearIfVolatile() error {
|
|||||||
if !i.volatile {
|
if !i.volatile {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
i.pixels = nil
|
i.restoringInfo.clear()
|
||||||
i.baseColor = nil
|
|
||||||
i.drawImageHistory = nil
|
|
||||||
return i.image.Fill(color.Transparent)
|
return i.image.Fill(color.Transparent)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -181,7 +170,7 @@ func (i *imageImpl) DrawImage(image *Image, options *DrawImageOptions) error {
|
|||||||
colorm: options.ColorM,
|
colorm: options.ColorM,
|
||||||
mode: opengl.CompositeMode(options.CompositeMode),
|
mode: opengl.CompositeMode(options.CompositeMode),
|
||||||
}
|
}
|
||||||
i.drawImageHistory = append(i.drawImageHistory, c)
|
i.restoringInfo.appendDrawImageHistory(c)
|
||||||
geom := &options.GeoM
|
geom := &options.GeoM
|
||||||
colorm := &options.ColorM
|
colorm := &options.ColorM
|
||||||
mode := opengl.CompositeMode(options.CompositeMode)
|
mode := opengl.CompositeMode(options.CompositeMode)
|
||||||
@ -200,26 +189,11 @@ func (i *imageImpl) At(x, y int, context *opengl.Context) color.Color {
|
|||||||
if i.disposed {
|
if i.disposed {
|
||||||
return color.Transparent
|
return color.Transparent
|
||||||
}
|
}
|
||||||
if i.pixels == nil || i.drawImageHistory != nil {
|
clr, err := i.restoringInfo.at(x, y, context)
|
||||||
var err error
|
|
||||||
i.pixels, err = i.image.Pixels(context)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
i.drawImageHistory = nil
|
return clr
|
||||||
}
|
|
||||||
idx := 4*x + 4*y*i.width
|
|
||||||
r, g, b, a := i.pixels[idx], i.pixels[idx+1], i.pixels[idx+2], i.pixels[idx+3]
|
|
||||||
return color.RGBA{r, g, b, a}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (i *imageImpl) hasHistoryWith(target *Image) bool {
|
|
||||||
for _, c := range i.drawImageHistory {
|
|
||||||
if c.image == target {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *imageImpl) resetHistoryIfNeeded(target *Image, context *opengl.Context) error {
|
func (i *imageImpl) resetHistoryIfNeeded(target *Image, context *opengl.Context) error {
|
||||||
@ -228,26 +202,16 @@ func (i *imageImpl) resetHistoryIfNeeded(target *Image, context *opengl.Context)
|
|||||||
if i.disposed {
|
if i.disposed {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if i.drawImageHistory == nil {
|
if err := i.restoringInfo.resetHistoryIfNeeded(target, context); err != nil {
|
||||||
return nil
|
return err
|
||||||
}
|
}
|
||||||
if !i.hasHistoryWith(target) {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
var err error
|
|
||||||
i.pixels, err = i.image.Pixels(context)
|
|
||||||
if err != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
i.baseColor = nil
|
|
||||||
i.drawImageHistory = nil
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *imageImpl) hasHistory() bool {
|
func (i *imageImpl) hasHistory() bool {
|
||||||
i.m.Lock()
|
i.m.Lock()
|
||||||
defer i.m.Unlock()
|
defer i.m.Unlock()
|
||||||
return i.drawImageHistory != nil
|
return i.restoringInfo.hasHistory()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *imageImpl) restore(context *opengl.Context) error {
|
func (i *imageImpl) restore(context *opengl.Context) error {
|
||||||
@ -274,42 +238,11 @@ func (i *imageImpl) restore(context *opengl.Context) error {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
img := image.NewRGBA(image.Rect(0, 0, i.width, i.height))
|
|
||||||
if i.pixels != nil {
|
|
||||||
for j := 0; j < i.height; j++ {
|
|
||||||
copy(img.Pix[j*img.Stride:], i.pixels[j*i.width*4:(j+1)*i.width*4])
|
|
||||||
}
|
|
||||||
} else if i.baseColor != nil {
|
|
||||||
r32, g32, b32, a32 := i.baseColor.RGBA()
|
|
||||||
r, g, b, a := uint8(r32), uint8(g32), uint8(b32), uint8(a32)
|
|
||||||
for idx := 0; idx < len(img.Pix)/4; idx++ {
|
|
||||||
img.Pix[4*idx] = r
|
|
||||||
img.Pix[4*idx+1] = g
|
|
||||||
img.Pix[4*idx+2] = b
|
|
||||||
img.Pix[4*idx+3] = a
|
|
||||||
}
|
|
||||||
}
|
|
||||||
var err error
|
var err error
|
||||||
i.image, err = graphics.NewImageFromImage(img, glFilter(i.filter))
|
i.image, err = i.restoringInfo.restore(context)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
for _, c := range i.drawImageHistory {
|
|
||||||
if c.image.impl.hasHistory() {
|
|
||||||
panic("not reach")
|
|
||||||
}
|
|
||||||
if err := i.image.DrawImage(c.image.impl.image, c.vertices, &c.geom, &c.colorm, c.mode); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if 0 < len(i.drawImageHistory) {
|
|
||||||
i.pixels, err = i.image.Pixels(context)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
i.baseColor = nil
|
|
||||||
i.drawImageHistory = nil
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -326,9 +259,7 @@ func (i *imageImpl) Dispose() error {
|
|||||||
}
|
}
|
||||||
i.image = nil
|
i.image = nil
|
||||||
i.disposed = true
|
i.disposed = true
|
||||||
i.pixels = nil
|
i.restoringInfo.clear()
|
||||||
i.baseColor = nil
|
|
||||||
i.drawImageHistory = nil
|
|
||||||
runtime.SetFinalizer(i, nil)
|
runtime.SetFinalizer(i, nil)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -339,12 +270,7 @@ func (i *imageImpl) ReplacePixels(p []uint8) error {
|
|||||||
}
|
}
|
||||||
i.m.Lock()
|
i.m.Lock()
|
||||||
defer i.m.Unlock()
|
defer i.m.Unlock()
|
||||||
if i.pixels == nil {
|
i.restoringInfo.resetWithPixels(p)
|
||||||
i.pixels = make([]uint8, i.width*i.height*4)
|
|
||||||
}
|
|
||||||
copy(i.pixels, p)
|
|
||||||
i.baseColor = nil
|
|
||||||
i.drawImageHistory = nil
|
|
||||||
if i.disposed {
|
if i.disposed {
|
||||||
return errors.New("ebiten: image is already disposed")
|
return errors.New("ebiten: image is already disposed")
|
||||||
}
|
}
|
||||||
|
146
imagerestoringinfo.go
Normal file
146
imagerestoringinfo.go
Normal file
@ -0,0 +1,146 @@
|
|||||||
|
// Copyright 2016 Hajime Hoshi
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package ebiten
|
||||||
|
|
||||||
|
import (
|
||||||
|
"image"
|
||||||
|
"image/color"
|
||||||
|
|
||||||
|
"github.com/hajimehoshi/ebiten/internal/graphics"
|
||||||
|
"github.com/hajimehoshi/ebiten/internal/graphics/opengl"
|
||||||
|
)
|
||||||
|
|
||||||
|
type drawImageHistoryItem struct {
|
||||||
|
image *Image
|
||||||
|
vertices []int16
|
||||||
|
geom GeoM
|
||||||
|
colorm ColorM
|
||||||
|
mode opengl.CompositeMode
|
||||||
|
}
|
||||||
|
|
||||||
|
type imageRestoringInfo struct {
|
||||||
|
imageImpl *imageImpl
|
||||||
|
pixels []uint8
|
||||||
|
baseColor color.Color
|
||||||
|
drawImageHistory []*drawImageHistoryItem
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *imageRestoringInfo) resetWithPixels(pixels []uint8) {
|
||||||
|
if i.pixels == nil {
|
||||||
|
i.pixels = make([]uint8, len(pixels))
|
||||||
|
}
|
||||||
|
copy(i.pixels, pixels)
|
||||||
|
i.baseColor = nil
|
||||||
|
i.drawImageHistory = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *imageRestoringInfo) clear() {
|
||||||
|
i.pixels = nil
|
||||||
|
i.baseColor = nil
|
||||||
|
i.drawImageHistory = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *imageRestoringInfo) fill(clr color.Color) {
|
||||||
|
i.pixels = nil
|
||||||
|
i.baseColor = clr
|
||||||
|
i.drawImageHistory = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *imageRestoringInfo) appendDrawImageHistory(item *drawImageHistoryItem) {
|
||||||
|
i.drawImageHistory = append(i.drawImageHistory, item)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *imageRestoringInfo) at(x, y int, context *opengl.Context) (color.Color, error) {
|
||||||
|
if i.pixels == nil || i.drawImageHistory != nil {
|
||||||
|
var err error
|
||||||
|
i.pixels, err = i.imageImpl.image.Pixels(context)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
i.baseColor = nil
|
||||||
|
i.drawImageHistory = nil
|
||||||
|
}
|
||||||
|
idx := 4*x + 4*y*i.imageImpl.width
|
||||||
|
r, g, b, a := i.pixels[idx], i.pixels[idx+1], i.pixels[idx+2], i.pixels[idx+3]
|
||||||
|
return color.RGBA{r, g, b, a}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *imageRestoringInfo) hasHistoryWith(target *Image) bool {
|
||||||
|
for _, c := range i.drawImageHistory {
|
||||||
|
if c.image == target {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *imageRestoringInfo) resetHistoryIfNeeded(target *Image, context *opengl.Context) error {
|
||||||
|
if i.drawImageHistory == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if !i.hasHistoryWith(target) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
var err error
|
||||||
|
i.pixels, err = i.imageImpl.image.Pixels(context)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
i.baseColor = nil
|
||||||
|
i.drawImageHistory = nil
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *imageRestoringInfo) hasHistory() bool {
|
||||||
|
return i.drawImageHistory != nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *imageRestoringInfo) restore(context *opengl.Context) (*graphics.Image, error) {
|
||||||
|
w, h := i.imageImpl.width, i.imageImpl.height
|
||||||
|
img := image.NewRGBA(image.Rect(0, 0, w, h))
|
||||||
|
if i.pixels != nil {
|
||||||
|
for j := 0; j < h; j++ {
|
||||||
|
copy(img.Pix[j*img.Stride:], i.pixels[j*w*4:(j+1)*w*4])
|
||||||
|
}
|
||||||
|
} else if i.baseColor != nil {
|
||||||
|
r32, g32, b32, a32 := i.baseColor.RGBA()
|
||||||
|
r, g, b, a := uint8(r32), uint8(g32), uint8(b32), uint8(a32)
|
||||||
|
for idx := 0; idx < len(img.Pix)/4; idx++ {
|
||||||
|
img.Pix[4*idx] = r
|
||||||
|
img.Pix[4*idx+1] = g
|
||||||
|
img.Pix[4*idx+2] = b
|
||||||
|
img.Pix[4*idx+3] = a
|
||||||
|
}
|
||||||
|
}
|
||||||
|
gimg, err := graphics.NewImageFromImage(img, glFilter(i.imageImpl.filter))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for _, c := range i.drawImageHistory {
|
||||||
|
if c.image.impl.hasHistory() {
|
||||||
|
panic("not reach")
|
||||||
|
}
|
||||||
|
if err := gimg.DrawImage(c.image.impl.image, c.vertices, &c.geom, &c.colorm, c.mode); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
i.pixels, err = gimg.Pixels(context)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
i.baseColor = nil
|
||||||
|
i.drawImageHistory = nil
|
||||||
|
return gimg, nil
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user