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-07-24 19:28:59 +02:00
|
|
|
package pixels
|
2016-07-13 19:35:20 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"image"
|
|
|
|
"image/color"
|
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten/internal/graphics"
|
|
|
|
"github.com/hajimehoshi/ebiten/internal/graphics/opengl"
|
|
|
|
)
|
|
|
|
|
|
|
|
type drawImageHistoryItem struct {
|
2016-07-23 23:22:33 +02:00
|
|
|
image *graphics.Image
|
2016-07-13 19:35:20 +02:00
|
|
|
vertices []int16
|
2016-07-23 23:46:47 +02:00
|
|
|
geom graphics.Matrix
|
|
|
|
colorm graphics.Matrix
|
2016-07-13 19:35:20 +02:00
|
|
|
mode opengl.CompositeMode
|
|
|
|
}
|
|
|
|
|
2016-07-25 03:32:16 +02:00
|
|
|
// Pixels represents pixels of an image for restoring when GL context is lost.
|
2016-07-24 19:28:59 +02:00
|
|
|
type Pixels struct {
|
2016-07-25 03:32:16 +02:00
|
|
|
image *graphics.Image
|
|
|
|
|
|
|
|
// basePixels and baseColor are exclusive.
|
2016-07-13 20:29:19 +02:00
|
|
|
basePixels []uint8
|
2016-07-13 19:35:20 +02:00
|
|
|
baseColor color.Color
|
|
|
|
drawImageHistory []*drawImageHistoryItem
|
2016-07-26 19:16:31 +02:00
|
|
|
stale bool
|
2016-07-13 19:35:20 +02:00
|
|
|
}
|
|
|
|
|
2016-07-24 19:28:59 +02:00
|
|
|
func NewPixels(image *graphics.Image) *Pixels {
|
|
|
|
return &Pixels{
|
|
|
|
image: image,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-26 19:16:31 +02:00
|
|
|
func (p *Pixels) IsStale() bool {
|
|
|
|
return p.stale
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Pixels) MakeStale() {
|
|
|
|
p.basePixels = nil
|
|
|
|
p.baseColor = nil
|
|
|
|
p.drawImageHistory = nil
|
|
|
|
p.stale = true
|
|
|
|
}
|
|
|
|
|
2016-07-24 19:28:59 +02:00
|
|
|
func (p *Pixels) Clear() {
|
2016-07-13 20:29:19 +02:00
|
|
|
p.basePixels = nil
|
2016-07-13 19:52:24 +02:00
|
|
|
p.baseColor = nil
|
|
|
|
p.drawImageHistory = nil
|
2016-07-26 19:16:31 +02:00
|
|
|
p.stale = false
|
2016-07-13 19:35:20 +02:00
|
|
|
}
|
|
|
|
|
2016-07-24 19:28:59 +02:00
|
|
|
func (p *Pixels) Fill(clr color.Color) {
|
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-07-13 19:35:20 +02:00
|
|
|
}
|
|
|
|
|
2016-07-26 03:51:48 +02:00
|
|
|
func (p *Pixels) ReplacePixels(pixels []uint8) {
|
|
|
|
if p.basePixels == nil {
|
|
|
|
p.basePixels = make([]uint8, len(pixels))
|
|
|
|
}
|
|
|
|
copy(p.basePixels, pixels)
|
|
|
|
p.baseColor = nil
|
|
|
|
p.drawImageHistory = nil
|
2016-07-26 19:16:31 +02:00
|
|
|
p.stale = false
|
2016-07-26 03:51:48 +02:00
|
|
|
}
|
|
|
|
|
2016-07-24 19:28:59 +02:00
|
|
|
func (p *Pixels) AppendDrawImageHistory(image *graphics.Image, vertices []int16, geom graphics.Matrix, colorm graphics.Matrix, mode opengl.CompositeMode) {
|
2016-07-26 19:16:31 +02:00
|
|
|
if p.stale {
|
|
|
|
return
|
|
|
|
}
|
2016-07-24 19:28:59 +02:00
|
|
|
item := &drawImageHistoryItem{
|
|
|
|
image: image,
|
|
|
|
vertices: vertices,
|
|
|
|
geom: geom,
|
|
|
|
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-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.
|
|
|
|
// This means Pixels members must match with acutal state in GPU.
|
2016-07-24 19:28:59 +02:00
|
|
|
func (p *Pixels) At(idx int, context *opengl.Context) (color.Color, error) {
|
2016-07-26 19:16:31 +02:00
|
|
|
if p.basePixels == nil || p.drawImageHistory != nil || p.stale {
|
2016-07-26 18:54:23 +02:00
|
|
|
if err := p.Reset(context); err != nil {
|
2016-07-13 19:35:20 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
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-07-26 05:28:05 +02:00
|
|
|
func (p *Pixels) DependsOn(target *graphics.Image) bool {
|
2016-07-26 19:16:31 +02:00
|
|
|
if p.stale {
|
|
|
|
return false
|
|
|
|
}
|
2016-07-13 19:52:24 +02:00
|
|
|
for _, c := range p.drawImageHistory {
|
2016-07-13 19:35:20 +02:00
|
|
|
if c.image == target {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-07-25 02:10:51 +02:00
|
|
|
func (p *Pixels) Reset(context *opengl.Context) error {
|
2016-07-23 18:28:39 +02:00
|
|
|
var err error
|
2016-07-23 18:50:22 +02:00
|
|
|
p.basePixels, err = p.image.Pixels(context)
|
2016-07-23 18:28:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
p.baseColor = nil
|
|
|
|
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-07-26 18:28:16 +02:00
|
|
|
func (p *Pixels) 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-07-25 03:32:16 +02:00
|
|
|
// Restore restores the pixels using its history.
|
2016-07-13 20:29:19 +02:00
|
|
|
//
|
2016-07-26 18:54:23 +02:00
|
|
|
// Restore is the only function that the pixel data is not present on GPU when this is called.
|
2016-07-24 19:28:59 +02:00
|
|
|
func (p *Pixels) Restore(context *opengl.Context, width, height int, filter opengl.Filter) (*graphics.Image, error) {
|
2016-07-13 20:00:50 +02:00
|
|
|
img := image.NewRGBA(image.Rect(0, 0, width, height))
|
2016-07-13 20:29:19 +02:00
|
|
|
if p.basePixels != nil {
|
2016-07-13 20:00:50 +02:00
|
|
|
for j := 0; j < height; j++ {
|
2016-07-13 20:29:19 +02:00
|
|
|
copy(img.Pix[j*img.Stride:], p.basePixels[j*width*4:(j+1)*width*4])
|
2016-07-13 19:35:20 +02:00
|
|
|
}
|
|
|
|
}
|
2016-07-24 19:28:59 +02:00
|
|
|
gimg, err := graphics.NewImageFromImage(img, filter)
|
2016-07-13 19:35:20 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-07-15 17:11:37 +02:00
|
|
|
if p.baseColor != nil {
|
|
|
|
if p.basePixels != nil {
|
|
|
|
panic("not reach")
|
|
|
|
}
|
|
|
|
if err := gimg.Fill(p.baseColor); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2016-07-13 19:52:24 +02:00
|
|
|
for _, c := range p.drawImageHistory {
|
2016-07-13 20:29:19 +02:00
|
|
|
// c.image.impl must be already restored.
|
2016-07-23 23:22:33 +02:00
|
|
|
/*if c.image.impl.hasHistory() {
|
2016-07-13 19:35:20 +02:00
|
|
|
panic("not reach")
|
2016-07-23 23:22:33 +02:00
|
|
|
}*/
|
2016-07-23 23:46:47 +02:00
|
|
|
if err := gimg.DrawImage(c.image, c.vertices, c.geom, c.colorm, c.mode); err != nil {
|
2016-07-13 19:35:20 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
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 nil, err
|
|
|
|
}
|
2016-07-13 19:52:24 +02:00
|
|
|
p.baseColor = nil
|
|
|
|
p.drawImageHistory = nil
|
2016-07-26 19:16:31 +02:00
|
|
|
p.stale = false
|
2016-07-13 19:35:20 +02:00
|
|
|
return gimg, nil
|
|
|
|
}
|