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 (
|
2016-07-26 19:25:08 +02:00
|
|
|
"errors"
|
2016-07-13 19:35:20 +02:00
|
|
|
"image"
|
|
|
|
"image/color"
|
|
|
|
|
2016-10-31 16:20:27 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/affine"
|
2016-07-13 19:35:20 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/graphics"
|
2016-11-03 15:31:25 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/opengl"
|
2016-07-13 19:35:20 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type drawImageHistoryItem struct {
|
2017-01-20 20:24:39 +01:00
|
|
|
image *Image
|
2016-11-01 18:06:37 +01:00
|
|
|
vertices []float32
|
2016-10-31 16:20:27 +01:00
|
|
|
colorm affine.ColorM
|
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 {
|
2016-09-03 17:57:23 +02:00
|
|
|
image *graphics.Image
|
|
|
|
filter opengl.Filter
|
2016-09-03 16:08:51 +02:00
|
|
|
|
2016-09-03 15:05:05 +02:00
|
|
|
// baseImage and baseColor are exclusive.
|
2016-07-13 20:29:19 +02:00
|
|
|
basePixels []uint8
|
2016-08-30 16:31:59 +02:00
|
|
|
baseColor color.RGBA
|
2016-07-13 19:35:20 +02:00
|
|
|
drawImageHistory []*drawImageHistoryItem
|
2016-07-26 19:16:31 +02:00
|
|
|
stale bool
|
2016-09-03 19:06:54 +02:00
|
|
|
|
|
|
|
volatile bool
|
2016-09-03 19:31:50 +02:00
|
|
|
screen bool
|
2016-07-13 19:35:20 +02:00
|
|
|
}
|
|
|
|
|
2016-09-03 19:06:54 +02:00
|
|
|
func NewImage(width, height int, filter opengl.Filter, volatile bool) (*Image, error) {
|
2016-09-03 16:08:51 +02:00
|
|
|
img, err := graphics.NewImage(width, height, filter)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &Image{
|
2016-09-03 19:06:54 +02:00
|
|
|
image: img,
|
|
|
|
filter: filter,
|
|
|
|
volatile: volatile,
|
2016-09-03 16:08:51 +02:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2016-12-26 19:18:56 +01:00
|
|
|
func NewImageFromImage(source *image.RGBA, width, height int, filter opengl.Filter) (*Image, error) {
|
|
|
|
img, err := graphics.NewImageFromImage(source, width, height, filter)
|
2016-09-03 16:08:51 +02:00
|
|
|
if err != nil {
|
|
|
|
// TODO: texture should be removed here?
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-02-04 20:16:09 +01:00
|
|
|
w2, h2 := graphics.NextPowerOf2Int(width), graphics.NextPowerOf2Int(height)
|
|
|
|
p := make([]uint8, 4*w2*h2)
|
2017-02-04 18:19:30 +01:00
|
|
|
for j := 0; j < height; j++ {
|
2017-02-04 20:16:09 +01:00
|
|
|
copy(p[j*w2*4:(j+1)*w2*4], source.Pix[j*source.Stride:])
|
2017-02-04 18:19:30 +01:00
|
|
|
}
|
2016-09-03 16:08:51 +02:00
|
|
|
return &Image{
|
2017-02-04 18:19:30 +01:00
|
|
|
image: img,
|
|
|
|
basePixels: p,
|
|
|
|
filter: filter,
|
2016-09-03 16:08:51 +02:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewScreenFramebufferImage(width, height int) (*Image, error) {
|
|
|
|
img, err := graphics.NewScreenFramebufferImage(width, height)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &Image{
|
2016-09-03 19:06:54 +02:00
|
|
|
image: img,
|
|
|
|
volatile: true,
|
2016-09-03 19:31:50 +02:00
|
|
|
screen: true,
|
2016-09-03 16:08:51 +02:00
|
|
|
}, nil
|
2016-09-03 15:24:37 +02:00
|
|
|
}
|
2016-09-03 18:25:02 +02:00
|
|
|
|
|
|
|
func (p *Image) Size() (int, int) {
|
2016-12-27 02:58:46 +01:00
|
|
|
return p.image.Size()
|
2016-09-03 18:25:02 +02:00
|
|
|
}
|
2016-09-03 15:24:37 +02:00
|
|
|
|
2016-09-03 16:54:52 +02:00
|
|
|
func (p *Image) makeStale() {
|
2016-07-26 19:16:31 +02:00
|
|
|
p.basePixels = nil
|
2016-08-30 16:31:59 +02:00
|
|
|
p.baseColor = color.RGBA{}
|
2016-07-26 19:16:31 +02:00
|
|
|
p.drawImageHistory = nil
|
|
|
|
p.stale = true
|
|
|
|
}
|
|
|
|
|
2016-09-03 19:06:54 +02:00
|
|
|
func (p *Image) ClearIfVolatile() error {
|
|
|
|
if !p.volatile {
|
|
|
|
return nil
|
|
|
|
}
|
2016-07-13 20:29:19 +02:00
|
|
|
p.basePixels = nil
|
2016-08-30 16:31:59 +02:00
|
|
|
p.baseColor = color.RGBA{}
|
2016-07-13 19:52:24 +02:00
|
|
|
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
|
2016-07-13 19:52:24 +02:00
|
|
|
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
|
|
|
|
}
|
2017-02-04 20:16:09 +01:00
|
|
|
p.basePixels = pixels
|
2016-08-30 16:31:59 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-11-01 18:06:37 +01:00
|
|
|
func (p *Image) DrawImage(img *Image, vertices []float32, colorm affine.ColorM, mode opengl.CompositeMode) error {
|
2016-09-03 19:16:58 +02:00
|
|
|
if img.stale || img.volatile {
|
2016-09-03 16:54:52 +02:00
|
|
|
p.makeStale()
|
2016-09-03 16:42:44 +02:00
|
|
|
} else {
|
2017-01-20 20:24:39 +01:00
|
|
|
p.appendDrawImageHistory(img, vertices, colorm, mode)
|
2016-09-03 16:42:44 +02:00
|
|
|
}
|
2016-10-25 03:42:49 +02:00
|
|
|
if err := p.image.DrawImage(img.image, vertices, colorm, mode); err != nil {
|
2016-09-03 16:42:44 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-01-20 20:24:39 +01:00
|
|
|
func (p *Image) appendDrawImageHistory(image *Image, vertices []float32, colorm affine.ColorM, 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,
|
|
|
|
colorm: colorm,
|
|
|
|
mode: mode,
|
|
|
|
}
|
2016-07-13 19:52:24 +02:00
|
|
|
p.drawImageHistory = append(p.drawImageHistory, item)
|
2016-07-13 19:35:20 +02:00
|
|
|
}
|
|
|
|
|
2016-12-26 19:18:56 +01:00
|
|
|
// At returns a color value at (x, y).
|
2016-07-25 03:32:16 +02:00
|
|
|
//
|
|
|
|
// 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.
|
2016-12-26 19:18:56 +01:00
|
|
|
func (p *Image) At(x, y int, context *opengl.Context) (color.RGBA, error) {
|
2017-02-04 20:24:07 +01:00
|
|
|
w, h := p.image.Size()
|
|
|
|
w2, h2 := graphics.NextPowerOf2Int(w), graphics.NextPowerOf2Int(h)
|
|
|
|
if x < 0 || y < 0 || w2 <= x || h2 <= y {
|
|
|
|
return color.RGBA{}, nil
|
|
|
|
}
|
2016-07-26 19:16:31 +02:00
|
|
|
if p.basePixels == nil || p.drawImageHistory != nil || p.stale {
|
2016-09-03 16:54:52 +02:00
|
|
|
if err := p.readPixelsFromVRAM(p.image, context); err != nil {
|
2016-08-30 16:31:59 +02:00
|
|
|
return color.RGBA{}, err
|
2016-07-13 19:35:20 +02:00
|
|
|
}
|
|
|
|
}
|
2017-02-04 20:24:07 +01:00
|
|
|
idx := 4*x + 4*y*w2
|
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
|
|
|
|
}
|
|
|
|
|
2016-09-03 16:54:52 +02:00
|
|
|
func (p *Image) MakeStaleIfDependingOn(target *Image) {
|
2016-07-26 19:16:31 +02:00
|
|
|
if p.stale {
|
2016-09-03 16:54:52 +02:00
|
|
|
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.
|
2016-07-13 19:52:24 +02:00
|
|
|
for _, c := range p.drawImageHistory {
|
2017-01-20 20:24:39 +01:00
|
|
|
if c.image == target {
|
2016-09-03 16:54:52 +02:00
|
|
|
p.makeStale()
|
|
|
|
return
|
2016-07-13 19:35:20 +02:00
|
|
|
}
|
|
|
|
}
|
2016-09-03 16:54:52 +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 {
|
2016-07-23 18:28:39 +02:00
|
|
|
var err error
|
2016-07-27 05:24:30 +02:00
|
|
|
p.basePixels, err = image.Pixels(context)
|
2016-07-23 18:28:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-08-30 16:31:59 +02:00
|
|
|
p.baseColor = color.RGBA{}
|
2016-07-23 18:28:39 +02:00
|
|
|
p.drawImageHistory = nil
|
2016-07-26 19:16:31 +02:00
|
|
|
p.stale = false
|
2016-07-23 18:28:39 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-09-03 16:54:52 +02:00
|
|
|
func (p *Image) ReadPixelsFromVRAMIfStale(context *opengl.Context) error {
|
2016-09-03 19:06:54 +02:00
|
|
|
if p.volatile {
|
|
|
|
return nil
|
|
|
|
}
|
2016-07-26 19:20:42 +02:00
|
|
|
if !p.stale {
|
|
|
|
return nil
|
|
|
|
}
|
2016-09-03 16:54:52 +02:00
|
|
|
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
|
|
|
|
}
|
2016-07-13 19:52:24 +02:00
|
|
|
return p.drawImageHistory != nil
|
2016-07-13 19:35:20 +02:00
|
|
|
}
|
|
|
|
|
2016-09-03 16:08:51 +02:00
|
|
|
// RestoreImage restores *graphics.Image from the pixels using its state.
|
2016-09-03 19:31:50 +02:00
|
|
|
func (p *Image) Restore(context *opengl.Context) error {
|
2016-12-27 02:58:46 +01:00
|
|
|
w, h := p.image.Size()
|
2016-09-03 19:31:50 +02:00
|
|
|
if p.screen {
|
|
|
|
// The screen image should also be recreated because framebuffer might
|
|
|
|
// be changed.
|
|
|
|
var err error
|
2016-12-27 02:58:46 +01:00
|
|
|
p.image, err = graphics.NewScreenFramebufferImage(w, h)
|
2016-09-03 19:31:50 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
p.basePixels = nil
|
|
|
|
p.baseColor = color.RGBA{}
|
|
|
|
p.drawImageHistory = nil
|
|
|
|
p.stale = false
|
|
|
|
return nil
|
|
|
|
}
|
2016-09-03 19:06:54 +02:00
|
|
|
if p.volatile {
|
|
|
|
var err error
|
2016-12-27 02:58:46 +01:00
|
|
|
p.image, err = graphics.NewImage(w, h, p.filter)
|
2016-09-03 19:06:54 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-09-03 19:11:02 +02:00
|
|
|
p.basePixels = nil
|
|
|
|
p.baseColor = color.RGBA{}
|
|
|
|
p.drawImageHistory = nil
|
|
|
|
p.stale = false
|
2016-09-03 19:06:54 +02:00
|
|
|
return nil
|
|
|
|
}
|
2016-07-26 19:25:08 +02:00
|
|
|
if p.stale {
|
2016-09-03 16:08:51 +02:00
|
|
|
return errors.New("restorable: pixels must not be stale when restoring")
|
2016-07-26 19:25:08 +02:00
|
|
|
}
|
2017-02-04 20:16:09 +01:00
|
|
|
w2, h2 := graphics.NextPowerOf2Int(w), graphics.NextPowerOf2Int(h)
|
|
|
|
img := image.NewRGBA(image.Rect(0, 0, w2, h2))
|
2016-07-13 20:29:19 +02:00
|
|
|
if p.basePixels != nil {
|
2016-12-27 02:58:46 +01:00
|
|
|
for j := 0; j < h; j++ {
|
2017-02-04 20:16:09 +01:00
|
|
|
copy(img.Pix[j*img.Stride:], p.basePixels[j*w2*4:(j+1)*w2*4])
|
2016-07-13 19:35:20 +02:00
|
|
|
}
|
|
|
|
}
|
2016-12-27 02:58:46 +01:00
|
|
|
gimg, err := graphics.NewImageFromImage(img, w, h, p.filter)
|
2016-07-13 19:35:20 +02:00
|
|
|
if err != nil {
|
2016-09-03 16:08:51 +02:00
|
|
|
return err
|
2016-07-13 19:35:20 +02:00
|
|
|
}
|
2016-08-30 16:31:59 +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 {
|
2016-09-03 16:08:51 +02:00
|
|
|
return err
|
2016-07-15 17:11:37 +02:00
|
|
|
}
|
|
|
|
}
|
2016-07-13 19:52:24 +02:00
|
|
|
for _, c := range p.drawImageHistory {
|
2017-01-20 20:24:39 +01:00
|
|
|
// c.image.image must be already restored.
|
|
|
|
if c.image.HasDependency() {
|
2016-07-13 19:35:20 +02:00
|
|
|
panic("not reach")
|
2017-01-20 20:24:39 +01:00
|
|
|
}
|
|
|
|
if err := gimg.DrawImage(c.image.image, c.vertices, c.colorm, c.mode); err != nil {
|
2016-09-03 16:08:51 +02:00
|
|
|
return err
|
2016-07-13 19:35:20 +02:00
|
|
|
}
|
|
|
|
}
|
2016-09-03 16:08:51 +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 {
|
2016-09-03 16:08:51 +02:00
|
|
|
return err
|
2016-07-13 19:35:20 +02:00
|
|
|
}
|
2016-08-30 16:31:59 +02:00
|
|
|
p.baseColor = color.RGBA{}
|
2016-07-13 19:52:24 +02:00
|
|
|
p.drawImageHistory = nil
|
2016-07-26 19:16:31 +02:00
|
|
|
p.stale = false
|
2016-09-03 16:08:51 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Image) Dispose() error {
|
2016-09-03 16:42:28 +02:00
|
|
|
if err := p.image.Dispose(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-09-03 16:08:51 +02:00
|
|
|
p.image = nil
|
2016-09-03 16:42:28 +02:00
|
|
|
p.basePixels = nil
|
|
|
|
p.baseColor = color.RGBA{}
|
|
|
|
p.drawImageHistory = nil
|
|
|
|
p.stale = false
|
2016-09-03 16:08:51 +02:00
|
|
|
return nil
|
2016-07-13 19:35:20 +02:00
|
|
|
}
|
2016-09-03 17:07:06 +02:00
|
|
|
|
|
|
|
func (p *Image) IsInvalidated(context *opengl.Context) bool {
|
|
|
|
return p.image.IsInvalidated(context)
|
|
|
|
}
|