ebiten/internal/restorable/image.go

320 lines
7.6 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"
2017-05-03 16:24:00 +02:00
"runtime"
2016-07-13 19:35:20 +02:00
"github.com/hajimehoshi/ebiten/internal/affine"
2016-07-13 19:35:20 +02:00
"github.com/hajimehoshi/ebiten/internal/graphics"
"github.com/hajimehoshi/ebiten/internal/opengl"
2016-07-13 19:35:20 +02:00
)
const drawImageHistoryMax = 100
2016-07-13 19:35:20 +02:00
type drawImageHistoryItem struct {
image *Image
vertices []float32
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 {
image *graphics.Image
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
2017-05-29 20:31:29 +02:00
// stale indicates whether the image needs to be synced with GPU as soon as possible.
stale bool
// volatile indicates whether the image is cleared whenever a frame starts.
volatile bool
2017-05-29 19:06:45 +02:00
// screen indicates whether the image is used as an actual screen.
screen bool
2016-07-13 19:35:20 +02:00
}
func NewImage(width, height int, filter opengl.Filter, volatile bool) *Image {
2017-05-03 16:15:18 +02:00
i := &Image{
image: graphics.NewImage(width, height, filter),
filter: filter,
volatile: volatile,
}
2017-05-03 16:15:18 +02:00
theImages.add(i)
2017-05-03 16:24:00 +02:00
runtime.SetFinalizer(i, (*Image).Dispose)
2017-05-03 16:15:18 +02:00
return i
}
func NewImageFromImage(source *image.RGBA, width, height int, filter opengl.Filter) *Image {
w2, h2 := graphics.NextPowerOf2Int(width), graphics.NextPowerOf2Int(height)
p := make([]uint8, 4*w2*h2)
for j := 0; j < height; j++ {
copy(p[j*w2*4:(j+1)*w2*4], source.Pix[j*source.Stride:])
}
2017-05-03 16:15:18 +02:00
i := &Image{
image: graphics.NewImageFromImage(source, width, height, filter),
basePixels: p,
filter: filter,
}
2017-05-03 16:15:18 +02:00
theImages.add(i)
2017-05-03 16:24:00 +02:00
runtime.SetFinalizer(i, (*Image).Dispose)
2017-05-03 16:15:18 +02:00
return i
}
func NewScreenFramebufferImage(width, height int) *Image {
2017-05-03 16:15:18 +02:00
i := &Image{
image: graphics.NewScreenFramebufferImage(width, height),
volatile: true,
screen: true,
}
2017-05-03 16:15:18 +02:00
theImages.add(i)
2017-05-03 16:24:00 +02:00
runtime.SetFinalizer(i, (*Image).Dispose)
2017-05-03 16:15:18 +02:00
return i
2016-09-03 15:24:37 +02:00
}
2017-05-31 18:27:56 +02:00
func (p *Image) BasePixelsForTesting() []uint8 {
return p.basePixels
}
func (p *Image) Size() (int, int) {
return p.image.Size()
}
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
}
2017-05-03 16:18:35 +02:00
func (p *Image) clearIfVolatile() {
if !p.volatile {
return
}
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 {
2017-05-31 18:27:56 +02:00
panic("not reached")
2016-09-03 16:42:44 +02:00
}
p.image.Fill(color.RGBA{})
2016-07-13 19:35:20 +02:00
}
func (p *Image) Fill(clr color.RGBA) {
theImages.resetPixelsIfDependingOn(p)
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
p.image.Fill(clr)
2016-07-13 19:35:20 +02:00
}
func (p *Image) ReplacePixels(pixels []uint8) {
theImages.resetPixelsIfDependingOn(p)
p.image.ReplacePixels(pixels)
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-07-26 03:51:48 +02:00
}
2017-05-27 09:14:48 +02:00
func (p *Image) DrawImage(img *Image, vertices []float32, colorm *affine.ColorM, mode opengl.CompositeMode) {
theImages.resetPixelsIfDependingOn(p)
if img.stale || img.volatile {
// TODO: What will happen if there are images depending on p?
p.makeStale()
2016-09-03 16:42:44 +02:00
} else {
p.appendDrawImageHistory(img, vertices, colorm, mode)
2016-09-03 16:42:44 +02:00
}
p.image.DrawImage(img.image, vertices, colorm, mode)
2016-09-03 16:42:44 +02:00
}
2017-05-27 09:14:48 +02: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
}
if len(p.drawImageHistory)+1 > drawImageHistoryMax {
p.makeStale()
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,
2017-05-27 09:14:48 +02:00
colorm: *colorm,
2016-07-24 19:28:59 +02:00
mode: mode,
}
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.
func (p *Image) At(x, y int) (color.RGBA, error) {
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 {
if err := p.readPixelsFromGPU(p.image); err != nil {
return color.RGBA{}, err
2016-07-13 19:35:20 +02: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
}
2017-05-03 16:18:35 +02:00
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
}
if p.dependsOn(target) {
p.makeStale()
2016-07-13 19:35:20 +02:00
}
}
func (p *Image) readPixelsFromGPU(image *graphics.Image) error {
var err error
p.basePixels, err = image.Pixels()
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) resolveStalePixels() error {
if p.volatile {
return nil
}
2016-07-26 19:20:42 +02:00
if !p.stale {
return nil
}
return p.readPixelsFromGPU(p.image)
2016-07-26 19:20:42 +02:00
}
func (p *Image) dependsOn(target *Image) bool {
for _, c := range p.drawImageHistory {
if c.image == target {
return true
}
}
return false
}
func (p *Image) dependingImages() map[*Image]struct{} {
r := map[*Image]struct{}{}
for _, c := range p.drawImageHistory {
r[c.image] = struct{}{}
}
return r
}
func (p *Image) hasDependency() bool {
2016-07-26 19:16:31 +02:00
if p.stale {
return false
}
return len(p.drawImageHistory) > 0
2016-07-13 19:35:20 +02:00
}
2017-05-02 17:10:23 +02:00
// Restore restores *graphics.Image from the pixels using its state.
func (p *Image) restore() error {
w, h := p.image.Size()
if p.screen {
// The screen image should also be recreated because framebuffer might
// be changed.
p.image = graphics.NewScreenFramebufferImage(w, h)
p.basePixels = nil
p.baseColor = color.RGBA{}
p.drawImageHistory = nil
p.stale = false
return nil
}
if p.volatile {
p.image = graphics.NewImage(w, h, p.filter)
p.basePixels = nil
p.baseColor = color.RGBA{}
p.drawImageHistory = nil
p.stale = false
return nil
}
if p.stale {
// TODO: panic here?
return errors.New("restorable: pixels must not be stale when restoring")
}
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 {
for j := 0; j < h; j++ {
copy(img.Pix[j*img.Stride:], p.basePixels[j*w2*4:(j+1)*w2*4])
2016-07-13 19:35:20 +02:00
}
}
gimg := graphics.NewImageFromImage(img, w, h, p.filter)
if p.baseColor != (color.RGBA{}) {
2016-07-15 17:11:37 +02:00
if p.basePixels != nil {
2017-05-31 18:27:56 +02:00
panic("not reached")
2016-07-15 17:11:37 +02:00
}
gimg.Fill(p.baseColor)
2016-07-15 17:11:37 +02:00
}
for _, c := range p.drawImageHistory {
// All dependencies must be already resolved.
if c.image.hasDependency() {
2017-05-31 18:27:56 +02:00
panic("not reached")
}
2017-05-27 09:14:48 +02:00
gimg.DrawImage(c.image.image, c.vertices, &c.colorm, c.mode)
2016-07-13 19:35:20 +02:00
}
p.image = gimg
var err error
p.basePixels, err = gimg.Pixels()
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
}
2017-05-03 16:24:00 +02:00
func (p *Image) Dispose() {
theImages.resetPixelsIfDependingOn(p)
p.image.Dispose()
p.image = nil
p.basePixels = nil
p.baseColor = color.RGBA{}
p.drawImageHistory = nil
p.stale = false
2017-05-03 16:24:00 +02:00
theImages.remove(p)
runtime.SetFinalizer(p, nil)
2016-07-13 19:35:20 +02:00
}
2016-09-03 17:07:06 +02:00
func (p *Image) IsInvalidated() bool {
return p.image.IsInvalidated()
2016-09-03 17:07:06 +02:00
}