ebiten/internal/restorable/image.go

325 lines
7.5 KiB
Go
Raw Normal View History

2016-07-16 21:41:28 +02:00
// Copyright 2016 The Ebiten Authors
2016-07-13 19:35:20 +02:00
//
// 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.
2016-09-03 15:05:05 +02:00
package restorable
2016-07-13 19:35:20 +02:00
import (
"errors"
2016-07-13 19:35:20 +02:00
"image"
"image/color"
"github.com/hajimehoshi/ebiten/internal/graphics"
"github.com/hajimehoshi/ebiten/internal/graphics/opengl"
)
type drawImageHistoryItem struct {
image *graphics.Image
vertices []uint8
geom graphics.Matrix
colorm graphics.Matrix
2016-07-13 19:35:20 +02:00
mode opengl.CompositeMode
}
2016-09-09 18:36:56 +02:00
// Image represents an image that can be restored when GL context is lost.
2016-09-03 15:05:05 +02:00
type Image struct {
image *graphics.Image
width int
height int
filter opengl.Filter
2016-09-03 15:05:05 +02:00
// baseImage and baseColor are exclusive.
2016-07-13 20:29:19 +02:00
basePixels []uint8
baseColor color.RGBA
2016-07-13 19:35:20 +02:00
drawImageHistory []*drawImageHistoryItem
2016-07-26 19:16:31 +02:00
stale bool
volatile bool
screen bool
2016-07-13 19:35:20 +02:00
}
func NewImage(width, height int, filter opengl.Filter, volatile bool) (*Image, error) {
img, err := graphics.NewImage(width, height, filter)
if err != nil {
return nil, err
}
return &Image{
image: img,
width: width,
height: height,
filter: filter,
volatile: volatile,
}, nil
}
func NewImageFromImage(source *image.RGBA, filter opengl.Filter) (*Image, error) {
img, err := graphics.NewImageFromImage(source, filter)
if err != nil {
// TODO: texture should be removed here?
return nil, err
}
size := source.Bounds().Size()
width, height := size.X, size.Y
return &Image{
image: img,
width: width,
height: height,
filter: filter,
}, nil
}
func NewScreenFramebufferImage(width, height int) (*Image, error) {
img, err := graphics.NewScreenFramebufferImage(width, height)
if err != nil {
return nil, err
}
return &Image{
image: img,
width: width,
height: height,
volatile: true,
screen: true,
}, nil
2016-09-03 15:24:37 +02:00
}
func (p *Image) Size() (int, int) {
return p.width, p.height
}
2016-09-03 15:24:37 +02:00
func (p *Image) makeStale() {
2016-07-26 19:16:31 +02:00
p.basePixels = nil
p.baseColor = color.RGBA{}
2016-07-26 19:16:31 +02:00
p.drawImageHistory = nil
p.stale = true
}
func (p *Image) ClearIfVolatile() error {
if !p.volatile {
return nil
}
2016-07-13 20:29:19 +02:00
p.basePixels = nil
p.baseColor = color.RGBA{}
p.drawImageHistory = nil
2016-07-26 19:16:31 +02:00
p.stale = false
2016-09-03 16:42:44 +02:00
if p.image == nil {
panic("not reach")
}
if err := p.image.Fill(color.RGBA{}); err != nil {
return err
}
return nil
2016-07-13 19:35:20 +02:00
}
2016-09-03 16:42:44 +02:00
func (p *Image) Fill(clr color.RGBA) error {
2016-07-13 20:29:19 +02:00
p.basePixels = nil
p.baseColor = clr
p.drawImageHistory = nil
2016-07-26 19:16:31 +02:00
p.stale = false
2016-09-03 16:42:44 +02:00
if err := p.image.Fill(clr); err != nil {
return err
}
return nil
2016-07-13 19:35:20 +02:00
}
2016-09-03 17:07:06 +02:00
func (p *Image) ReplacePixels(pixels []uint8) error {
if err := p.image.ReplacePixels(pixels); err != nil {
return err
}
2016-07-26 03:51:48 +02:00
if p.basePixels == nil {
p.basePixels = make([]uint8, len(pixels))
}
copy(p.basePixels, pixels)
p.baseColor = color.RGBA{}
2016-07-26 03:51:48 +02:00
p.drawImageHistory = nil
2016-07-26 19:16:31 +02:00
p.stale = false
2016-09-03 17:07:06 +02:00
return nil
2016-07-26 03:51:48 +02:00
}
func (p *Image) DrawImage(img *Image, vertices []uint8, geom graphics.Matrix, colorm graphics.Matrix, mode opengl.CompositeMode) error {
if img.stale || img.volatile {
p.makeStale()
2016-09-03 16:42:44 +02:00
} else {
p.appendDrawImageHistory(img.image, vertices, geom, colorm, mode)
}
if err := p.image.DrawImage(img.image, vertices, geom, colorm, mode); err != nil {
return err
}
return nil
}
func (p *Image) appendDrawImageHistory(image *graphics.Image, vertices []uint8, geom graphics.Matrix, colorm graphics.Matrix, mode opengl.CompositeMode) {
2016-07-26 19:16:31 +02:00
if p.stale {
return
}
2016-08-22 17:05:23 +02:00
// All images must be resolved and not stale each after frame.
// So we don't have to care if image is stale or not here.
2016-07-24 19:28:59 +02:00
item := &drawImageHistoryItem{
image: image,
vertices: vertices,
geom: geom,
colorm: colorm,
mode: mode,
}
p.drawImageHistory = append(p.drawImageHistory, item)
2016-07-13 19:35:20 +02:00
}
2016-07-25 03:32:16 +02:00
// At returns a color value at idx.
//
// Note that this must not be called until context is available.
2016-07-26 19:50:53 +02:00
// This means Pixels members must match with acutal state in VRAM.
func (p *Image) At(idx int, context *opengl.Context) (color.RGBA, error) {
2016-07-26 19:16:31 +02:00
if p.basePixels == nil || p.drawImageHistory != nil || p.stale {
if err := p.readPixelsFromVRAM(p.image, context); err != nil {
return color.RGBA{}, err
2016-07-13 19:35:20 +02:00
}
}
2016-07-13 20:29:19 +02:00
r, g, b, a := p.basePixels[idx], p.basePixels[idx+1], p.basePixels[idx+2], p.basePixels[idx+3]
2016-07-13 19:35:20 +02:00
return color.RGBA{r, g, b, a}, nil
}
func (p *Image) MakeStaleIfDependingOn(target *Image) {
2016-07-26 19:16:31 +02:00
if p.stale {
return
2016-07-26 19:16:31 +02:00
}
2016-08-22 17:05:23 +02:00
// TODO: Performance is bad when drawImageHistory is too many.
for _, c := range p.drawImageHistory {
if c.image == target.image {
p.makeStale()
return
2016-07-13 19:35:20 +02:00
}
}
return
2016-07-13 19:35:20 +02:00
}
2016-09-03 15:05:05 +02:00
func (p *Image) readPixelsFromVRAM(image *graphics.Image, context *opengl.Context) error {
var err error
2016-07-27 05:24:30 +02:00
p.basePixels, err = image.Pixels(context)
if err != nil {
return err
}
p.baseColor = color.RGBA{}
p.drawImageHistory = nil
2016-07-26 19:16:31 +02:00
p.stale = false
return nil
}
func (p *Image) ReadPixelsFromVRAMIfStale(context *opengl.Context) error {
if p.volatile {
return nil
}
2016-07-26 19:20:42 +02:00
if !p.stale {
return nil
}
return p.readPixelsFromVRAM(p.image, context)
2016-07-26 19:20:42 +02:00
}
2016-09-03 15:05:05 +02:00
func (p *Image) HasDependency() bool {
2016-07-26 19:16:31 +02:00
if p.stale {
return false
}
return p.drawImageHistory != nil
2016-07-13 19:35:20 +02:00
}
// RestoreImage restores *graphics.Image from the pixels using its state.
func (p *Image) Restore(context *opengl.Context) error {
if p.screen {
// The screen image should also be recreated because framebuffer might
// be changed.
var err error
p.image, err = graphics.NewScreenFramebufferImage(p.width, p.height)
if err != nil {
return err
}
p.basePixels = nil
p.baseColor = color.RGBA{}
p.drawImageHistory = nil
p.stale = false
return nil
}
if p.volatile {
var err error
p.image, err = graphics.NewImage(p.width, p.height, p.filter)
if err != nil {
return err
}
p.basePixels = nil
p.baseColor = color.RGBA{}
p.drawImageHistory = nil
p.stale = false
return nil
}
if p.stale {
return errors.New("restorable: pixels must not be stale when restoring")
}
img := image.NewRGBA(image.Rect(0, 0, p.width, p.height))
2016-07-13 20:29:19 +02:00
if p.basePixels != nil {
for j := 0; j < p.height; j++ {
copy(img.Pix[j*img.Stride:], p.basePixels[j*p.width*4:(j+1)*p.width*4])
2016-07-13 19:35:20 +02:00
}
}
gimg, err := graphics.NewImageFromImage(img, p.filter)
2016-07-13 19:35:20 +02:00
if err != nil {
return err
2016-07-13 19:35:20 +02:00
}
if p.baseColor != (color.RGBA{}) {
2016-07-15 17:11:37 +02:00
if p.basePixels != nil {
panic("not reach")
}
if err := gimg.Fill(p.baseColor); err != nil {
return err
2016-07-15 17:11:37 +02:00
}
}
for _, c := range p.drawImageHistory {
2016-07-13 20:29:19 +02:00
// c.image.impl must be already restored.
/*if c.image.impl.hasHistory() {
2016-07-13 19:35:20 +02:00
panic("not reach")
}*/
if err := gimg.DrawImage(c.image, c.vertices, c.geom, c.colorm, c.mode); err != nil {
return err
2016-07-13 19:35:20 +02:00
}
}
p.image = gimg
2016-07-13 20:29:19 +02:00
p.basePixels, err = gimg.Pixels(context)
2016-07-13 19:35:20 +02:00
if err != nil {
return err
2016-07-13 19:35:20 +02:00
}
p.baseColor = color.RGBA{}
p.drawImageHistory = nil
2016-07-26 19:16:31 +02:00
p.stale = false
return nil
}
func (p *Image) Dispose() error {
if err := p.image.Dispose(); err != nil {
return err
}
p.image = nil
p.basePixels = nil
p.baseColor = color.RGBA{}
p.drawImageHistory = nil
p.stale = false
return nil
2016-07-13 19:35:20 +02:00
}
2016-09-03 17:07:06 +02:00
func (p *Image) DisposeOnlyImage() error {
if err := p.image.Dispose(); err != nil {
return err
}
return nil
}
func (p *Image) IsInvalidated(context *opengl.Context) bool {
return p.image.IsInvalidated(context)
}