2016-07-03 07:42:28 +02:00
|
|
|
// 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 (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"image"
|
|
|
|
"image/color"
|
2016-07-04 18:34:40 +02:00
|
|
|
"image/draw"
|
2016-07-03 07:42:28 +02:00
|
|
|
"runtime"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten/internal/graphics"
|
|
|
|
"github.com/hajimehoshi/ebiten/internal/graphics/opengl"
|
2016-07-24 19:28:59 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/pixels"
|
2016-07-03 07:42:28 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type imageImpl struct {
|
2016-07-13 19:52:24 +02:00
|
|
|
image *graphics.Image
|
|
|
|
disposed bool
|
|
|
|
width int
|
|
|
|
height int
|
|
|
|
filter Filter
|
2016-07-27 05:24:30 +02:00
|
|
|
pixels pixels.Pixels
|
2016-07-13 19:52:24 +02:00
|
|
|
volatile bool
|
|
|
|
screen bool
|
|
|
|
m sync.Mutex
|
2016-07-03 07:42:28 +02:00
|
|
|
}
|
|
|
|
|
2016-07-04 20:40:40 +02:00
|
|
|
func newImageImpl(width, height int, filter Filter, volatile bool) (*imageImpl, error) {
|
2016-07-04 20:15:32 +02:00
|
|
|
img, err := graphics.NewImage(width, height, glFilter(filter))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-07-04 17:24:06 +02:00
|
|
|
i := &imageImpl{
|
2016-07-04 20:40:40 +02:00
|
|
|
image: img,
|
|
|
|
width: width,
|
|
|
|
height: height,
|
|
|
|
filter: filter,
|
|
|
|
volatile: volatile,
|
2016-07-04 17:24:06 +02:00
|
|
|
}
|
|
|
|
runtime.SetFinalizer(i, (*imageImpl).Dispose)
|
|
|
|
return i, nil
|
|
|
|
}
|
|
|
|
|
2016-07-04 18:34:40 +02:00
|
|
|
func newImageImplFromImage(source image.Image, filter Filter) (*imageImpl, error) {
|
|
|
|
size := source.Bounds().Size()
|
|
|
|
w, h := size.X, size.Y
|
|
|
|
// TODO: Return error when the image is too big!
|
|
|
|
// Don't lock while manipulating an image.Image interface.
|
2016-08-20 19:19:03 +02:00
|
|
|
|
|
|
|
// It is necessary to copy the source image since the actual construction of
|
|
|
|
// an image is delayed and we can't expect the source image is not modified
|
|
|
|
// until the construction.
|
|
|
|
origImg := source
|
|
|
|
newImg := image.NewRGBA(image.Rect(0, 0, w, h))
|
|
|
|
draw.Draw(newImg, newImg.Bounds(), origImg, origImg.Bounds().Min, draw.Src)
|
|
|
|
rgbaImg := newImg
|
2016-07-23 20:39:25 +02:00
|
|
|
p := make([]uint8, 4*w*h)
|
2016-07-04 18:34:40 +02:00
|
|
|
for j := 0; j < h; j++ {
|
2016-07-23 20:39:25 +02:00
|
|
|
copy(p[j*w*4:(j+1)*w*4], rgbaImg.Pix[j*rgbaImg.Stride:])
|
2016-07-04 18:34:40 +02:00
|
|
|
}
|
|
|
|
img, err := graphics.NewImageFromImage(rgbaImg, glFilter(filter))
|
|
|
|
if err != nil {
|
|
|
|
// TODO: texture should be removed here?
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
i := &imageImpl{
|
|
|
|
image: img,
|
|
|
|
width: w,
|
|
|
|
height: h,
|
|
|
|
filter: filter,
|
|
|
|
}
|
2016-07-26 03:51:48 +02:00
|
|
|
i.pixels.ReplacePixels(p)
|
2016-07-04 18:34:40 +02:00
|
|
|
runtime.SetFinalizer(i, (*imageImpl).Dispose)
|
|
|
|
return i, nil
|
|
|
|
}
|
|
|
|
|
2016-07-04 19:57:41 +02:00
|
|
|
func newScreenImageImpl(width, height int) (*imageImpl, error) {
|
2016-07-04 20:15:32 +02:00
|
|
|
img, err := graphics.NewScreenFramebufferImage(width, height)
|
2016-07-04 19:57:41 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-07-04 20:15:32 +02:00
|
|
|
i := &imageImpl{
|
2016-07-04 20:18:55 +02:00
|
|
|
image: img,
|
|
|
|
width: width,
|
|
|
|
height: height,
|
|
|
|
volatile: true,
|
|
|
|
screen: true,
|
2016-07-04 19:57:41 +02:00
|
|
|
}
|
2016-07-04 20:15:32 +02:00
|
|
|
runtime.SetFinalizer(i, (*imageImpl).Dispose)
|
|
|
|
return i, nil
|
2016-07-04 19:57:41 +02:00
|
|
|
}
|
|
|
|
|
2016-07-03 07:42:28 +02:00
|
|
|
func (i *imageImpl) Fill(clr color.Color) error {
|
2016-07-03 15:55:15 +02:00
|
|
|
i.m.Lock()
|
|
|
|
defer i.m.Unlock()
|
2016-07-03 18:50:58 +02:00
|
|
|
if i.disposed {
|
2016-07-03 07:42:28 +02:00
|
|
|
return errors.New("ebiten: image is already disposed")
|
|
|
|
}
|
2016-08-30 16:31:59 +02:00
|
|
|
rgba := color.RGBAModel.Convert(clr).(color.RGBA)
|
|
|
|
i.pixels.Fill(rgba)
|
|
|
|
return i.image.Fill(rgba)
|
2016-07-03 07:42:28 +02:00
|
|
|
}
|
|
|
|
|
2016-07-05 18:30:49 +02:00
|
|
|
func (i *imageImpl) clearIfVolatile() error {
|
|
|
|
i.m.Lock()
|
|
|
|
defer i.m.Unlock()
|
|
|
|
if i.disposed {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if !i.volatile {
|
|
|
|
return nil
|
|
|
|
}
|
2016-07-24 19:28:59 +02:00
|
|
|
i.pixels.Clear()
|
2016-08-30 16:31:59 +02:00
|
|
|
return i.image.Fill(color.RGBA{})
|
2016-07-05 18:30:49 +02:00
|
|
|
}
|
|
|
|
|
2016-07-03 07:42:28 +02:00
|
|
|
func (i *imageImpl) DrawImage(image *Image, options *DrawImageOptions) error {
|
|
|
|
// Calculate vertices before locking because the user can do anything in
|
|
|
|
// options.ImageParts interface without deadlock (e.g. Call Image functions).
|
|
|
|
if options == nil {
|
|
|
|
options = &DrawImageOptions{}
|
|
|
|
}
|
|
|
|
parts := options.ImageParts
|
|
|
|
if parts == nil {
|
|
|
|
// Check options.Parts for backward-compatibility.
|
|
|
|
dparts := options.Parts
|
|
|
|
if dparts != nil {
|
|
|
|
parts = imageParts(dparts)
|
|
|
|
} else {
|
|
|
|
parts = &wholeImage{image.impl.width, image.impl.height}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
quads := &textureQuads{parts: parts, width: image.impl.width, height: image.impl.height}
|
|
|
|
// TODO: Reuse one vertices instead of making here, but this would need locking.
|
|
|
|
vertices := make([]int16, parts.Len()*16)
|
|
|
|
n := quads.vertices(vertices)
|
|
|
|
if n == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if i == image.impl {
|
|
|
|
return errors.New("ebiten: Image.DrawImage: image should be different from the receiver")
|
|
|
|
}
|
2016-07-03 15:55:15 +02:00
|
|
|
i.m.Lock()
|
|
|
|
defer i.m.Unlock()
|
2016-07-03 18:50:58 +02:00
|
|
|
if i.disposed {
|
2016-07-03 07:42:28 +02:00
|
|
|
return errors.New("ebiten: image is already disposed")
|
|
|
|
}
|
2016-07-23 23:46:47 +02:00
|
|
|
geom := options.GeoM
|
|
|
|
colorm := options.ColorM
|
2016-07-03 07:42:28 +02:00
|
|
|
mode := opengl.CompositeMode(options.CompositeMode)
|
2016-08-22 17:04:15 +02:00
|
|
|
if image.impl.pixels.IsStale() {
|
|
|
|
i.pixels.MakeStale()
|
|
|
|
} else {
|
|
|
|
i.pixels.AppendDrawImageHistory(image.impl.image, vertices, &geom, &colorm, mode)
|
|
|
|
}
|
2016-07-23 23:46:47 +02:00
|
|
|
if err := i.image.DrawImage(image.impl.image, vertices, &geom, &colorm, mode); err != nil {
|
2016-07-03 07:42:28 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-07-13 18:20:45 +02:00
|
|
|
func (i *imageImpl) At(x, y int, context *opengl.Context) color.Color {
|
2016-07-23 15:24:40 +02:00
|
|
|
if context == nil {
|
2016-07-03 07:42:28 +02:00
|
|
|
panic("ebiten: At can't be called when the GL context is not initialized (this panic happens as of version 1.4.0-alpha)")
|
|
|
|
}
|
2016-07-03 16:31:03 +02:00
|
|
|
i.m.Lock()
|
|
|
|
defer i.m.Unlock()
|
2016-07-03 18:50:58 +02:00
|
|
|
if i.disposed {
|
2016-07-03 07:42:28 +02:00
|
|
|
return color.Transparent
|
|
|
|
}
|
2016-07-13 20:00:50 +02:00
|
|
|
idx := 4*x + 4*y*i.width
|
2016-07-27 05:24:30 +02:00
|
|
|
clr, err := i.pixels.At(idx, i.image, context)
|
2016-07-13 19:35:20 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2016-07-12 19:07:35 +02:00
|
|
|
}
|
2016-07-13 19:35:20 +02:00
|
|
|
return clr
|
2016-07-12 19:07:35 +02:00
|
|
|
}
|
|
|
|
|
2016-07-27 15:49:23 +02:00
|
|
|
func (i *imageImpl) resolveStalePixels(context *opengl.Context) error {
|
2016-07-23 18:28:39 +02:00
|
|
|
i.m.Lock()
|
|
|
|
defer i.m.Unlock()
|
|
|
|
if i.disposed {
|
|
|
|
return nil
|
|
|
|
}
|
2016-07-26 20:09:56 +02:00
|
|
|
if i.volatile {
|
|
|
|
return nil
|
|
|
|
}
|
2016-07-27 05:24:30 +02:00
|
|
|
if err := i.pixels.ReadPixelsFromVRAMIfStale(i.image, context); err != nil {
|
2016-07-23 18:28:39 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-07-26 05:28:05 +02:00
|
|
|
func (i *imageImpl) resetPixelsIfDependingOn(target *imageImpl, context *opengl.Context) error {
|
2016-07-03 18:50:58 +02:00
|
|
|
i.m.Lock()
|
|
|
|
defer i.m.Unlock()
|
2016-07-23 23:22:33 +02:00
|
|
|
if i == target {
|
|
|
|
return nil
|
|
|
|
}
|
2016-07-12 19:07:35 +02:00
|
|
|
if i.disposed {
|
2016-07-03 07:42:28 +02:00
|
|
|
return nil
|
|
|
|
}
|
2016-07-23 23:22:33 +02:00
|
|
|
if target.isDisposed() {
|
|
|
|
return errors.New("ebiten: target is already disposed")
|
|
|
|
}
|
2016-08-20 16:25:45 +02:00
|
|
|
// target is an image that is about to be tried mutating.
|
2016-07-26 05:17:15 +02:00
|
|
|
// If pixels object is related to that image, the pixels must be reset.
|
2016-07-26 05:28:05 +02:00
|
|
|
if !i.pixels.DependsOn(target.image) {
|
2016-07-26 04:40:41 +02:00
|
|
|
return nil
|
|
|
|
}
|
2016-08-20 16:25:45 +02:00
|
|
|
i.pixels.MakeStale()
|
2016-07-03 07:42:28 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-07-26 18:28:16 +02:00
|
|
|
func (i *imageImpl) hasDependency() bool {
|
2016-07-12 19:07:35 +02:00
|
|
|
i.m.Lock()
|
|
|
|
defer i.m.Unlock()
|
2016-07-26 18:28:16 +02:00
|
|
|
return i.pixels.HasDependency()
|
2016-07-12 19:07:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *imageImpl) restore(context *opengl.Context) error {
|
2016-07-03 18:50:58 +02:00
|
|
|
i.m.Lock()
|
|
|
|
defer i.m.Unlock()
|
2016-07-03 07:42:28 +02:00
|
|
|
if i.disposed {
|
|
|
|
return nil
|
|
|
|
}
|
2016-07-04 19:57:41 +02:00
|
|
|
if i.screen {
|
|
|
|
// The screen image should also be recreated because framebuffer might
|
|
|
|
// be changed.
|
|
|
|
var err error
|
|
|
|
i.image, err = graphics.NewScreenFramebufferImage(i.width, i.height)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2016-07-13 05:40:21 +02:00
|
|
|
if i.volatile {
|
2016-07-03 07:42:28 +02:00
|
|
|
var err error
|
2016-07-13 05:40:21 +02:00
|
|
|
i.image, err = graphics.NewImage(i.width, i.height, glFilter(i.filter))
|
2016-07-03 07:42:28 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-07-13 05:40:21 +02:00
|
|
|
return nil
|
|
|
|
}
|
2016-07-03 07:42:28 +02:00
|
|
|
var err error
|
2016-07-26 19:50:53 +02:00
|
|
|
i.image, err = i.pixels.CreateImage(context, i.width, i.height, glFilter(i.filter))
|
2016-07-03 07:42:28 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *imageImpl) Dispose() error {
|
2016-07-03 15:55:15 +02:00
|
|
|
i.m.Lock()
|
|
|
|
defer i.m.Unlock()
|
2016-07-03 18:50:58 +02:00
|
|
|
if i.disposed {
|
2016-07-03 07:42:28 +02:00
|
|
|
return errors.New("ebiten: image is already disposed")
|
|
|
|
}
|
2016-07-04 19:57:41 +02:00
|
|
|
if !i.screen {
|
|
|
|
if err := i.image.Dispose(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-07-03 07:42:28 +02:00
|
|
|
}
|
|
|
|
i.image = nil
|
|
|
|
i.disposed = true
|
2016-07-27 05:24:30 +02:00
|
|
|
i.pixels.Clear()
|
2016-07-03 07:42:28 +02:00
|
|
|
runtime.SetFinalizer(i, nil)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *imageImpl) ReplacePixels(p []uint8) error {
|
|
|
|
if l := 4 * i.width * i.height; len(p) != l {
|
|
|
|
return fmt.Errorf("ebiten: p's length must be %d", l)
|
|
|
|
}
|
2016-07-03 15:55:15 +02:00
|
|
|
i.m.Lock()
|
|
|
|
defer i.m.Unlock()
|
2016-07-26 03:51:48 +02:00
|
|
|
i.pixels.ReplacePixels(p)
|
2016-07-03 18:50:58 +02:00
|
|
|
if i.disposed {
|
2016-07-03 07:42:28 +02:00
|
|
|
return errors.New("ebiten: image is already disposed")
|
|
|
|
}
|
|
|
|
return i.image.ReplacePixels(p)
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:50:58 +02:00
|
|
|
func (i *imageImpl) isDisposed() bool {
|
|
|
|
i.m.Lock()
|
|
|
|
defer i.m.Unlock()
|
|
|
|
return i.disposed
|
|
|
|
}
|
|
|
|
|
2016-07-03 07:42:28 +02:00
|
|
|
func (i *imageImpl) isInvalidated(context *opengl.Context) bool {
|
2016-07-03 18:50:58 +02:00
|
|
|
i.m.Lock()
|
|
|
|
defer i.m.Unlock()
|
2016-07-03 07:42:28 +02:00
|
|
|
return i.image.IsInvalidated(context)
|
|
|
|
}
|