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"
|
|
|
|
"runtime"
|
|
|
|
"sync"
|
|
|
|
|
2016-11-04 19:06:18 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/graphics"
|
2016-11-03 15:31:25 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/opengl"
|
2016-09-03 15:05:05 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/restorable"
|
2016-07-03 07:42:28 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type imageImpl struct {
|
2016-09-03 15:24:37 +02:00
|
|
|
restorable *restorable.Image
|
2016-09-03 15:05:05 +02:00
|
|
|
m sync.Mutex
|
2016-07-03 07:42:28 +02:00
|
|
|
}
|
|
|
|
|
2016-11-04 19:06:18 +01:00
|
|
|
func checkSize(width, height int) error {
|
|
|
|
if width <= 0 {
|
|
|
|
return fmt.Errorf("ebiten: width must be more than 0")
|
|
|
|
}
|
|
|
|
if height <= 0 {
|
|
|
|
return fmt.Errorf("ebiten: height must be more than 0")
|
|
|
|
}
|
|
|
|
if width > graphics.ImageMaxSize {
|
|
|
|
return fmt.Errorf("ebiten: width must be less than or equal to %d", graphics.ImageMaxSize)
|
|
|
|
}
|
|
|
|
if height > graphics.ImageMaxSize {
|
|
|
|
return fmt.Errorf("ebiten: height must be less than or equal to %d", graphics.ImageMaxSize)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-07-04 20:40:40 +02:00
|
|
|
func newImageImpl(width, height int, filter Filter, volatile bool) (*imageImpl, error) {
|
2016-11-04 19:06:18 +01:00
|
|
|
if err := checkSize(width, height); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-07-04 17:24:06 +02:00
|
|
|
i := &imageImpl{
|
2017-03-03 15:51:25 +01:00
|
|
|
restorable: restorable.NewImage(width, height, glFilter(filter), 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
|
2016-11-04 19:06:18 +01:00
|
|
|
if err := checkSize(w, h); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-07-04 18:34:40 +02:00
|
|
|
// 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.
|
2016-12-26 14:49:58 +01:00
|
|
|
rgbaImg := graphics.CopyImage(source)
|
2016-07-04 18:34:40 +02:00
|
|
|
i := &imageImpl{
|
2017-03-03 15:51:25 +01:00
|
|
|
restorable: restorable.NewImageFromImage(rgbaImg, w, h, glFilter(filter)),
|
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-11-04 19:06:18 +01:00
|
|
|
if err := checkSize(width, height); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-07-04 20:15:32 +02:00
|
|
|
i := &imageImpl{
|
2017-03-03 15:51:25 +01:00
|
|
|
restorable: restorable.NewScreenFramebufferImage(width, height),
|
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-09-03 19:42:06 +02:00
|
|
|
if i.restorable == nil {
|
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)
|
2017-03-03 15:51:25 +01:00
|
|
|
i.restorable.Fill(rgba)
|
2016-09-03 16:42:44 +02:00
|
|
|
return nil
|
2016-07-03 07:42:28 +02:00
|
|
|
}
|
|
|
|
|
2017-03-03 15:51:25 +01:00
|
|
|
func (i *imageImpl) clearIfVolatile() {
|
2016-07-05 18:30:49 +02:00
|
|
|
i.m.Lock()
|
|
|
|
defer i.m.Unlock()
|
2016-09-03 19:42:06 +02:00
|
|
|
if i.restorable == nil {
|
2017-03-03 15:51:25 +01:00
|
|
|
return
|
2016-09-03 16:42:44 +02:00
|
|
|
}
|
2017-03-03 15:51:25 +01:00
|
|
|
i.restorable.ClearIfVolatile()
|
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 {
|
2016-09-03 18:25:02 +02:00
|
|
|
w, h := image.impl.restorable.Size()
|
|
|
|
parts = &wholeImage{w, h}
|
2016-07-03 07:42:28 +02:00
|
|
|
}
|
|
|
|
}
|
2016-09-03 18:25:02 +02:00
|
|
|
w, h := image.impl.restorable.Size()
|
2017-01-19 04:12:20 +01:00
|
|
|
vs := vertices(parts, w, h, &options.GeoM.impl)
|
2016-10-24 15:43:59 +02:00
|
|
|
if len(vs) == 0 {
|
2016-07-03 07:42:28 +02:00
|
|
|
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-09-03 19:42:06 +02:00
|
|
|
if i.restorable == nil {
|
2016-07-03 07:42:28 +02:00
|
|
|
return errors.New("ebiten: image is already disposed")
|
|
|
|
}
|
|
|
|
mode := opengl.CompositeMode(options.CompositeMode)
|
2017-03-03 15:51:25 +01:00
|
|
|
i.restorable.DrawImage(image.impl.restorable, vs, options.ColorM.impl, mode)
|
2016-07-03 07:42:28 +02:00
|
|
|
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-09-03 19:42:06 +02:00
|
|
|
if i.restorable == nil {
|
2016-07-03 07:42:28 +02:00
|
|
|
return color.Transparent
|
|
|
|
}
|
2016-12-26 19:18:56 +01:00
|
|
|
clr, err := i.restorable.At(x, y, 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()
|
2016-09-03 19:42:06 +02:00
|
|
|
if i.restorable == nil {
|
2016-07-23 18:28:39 +02:00
|
|
|
return nil
|
|
|
|
}
|
2016-09-03 16:54:52 +02:00
|
|
|
if err := i.restorable.ReadPixelsFromVRAMIfStale(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-09-03 19:42:06 +02:00
|
|
|
if i.restorable == nil {
|
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-09-03 16:54:52 +02:00
|
|
|
i.restorable.MakeStaleIfDependingOn(target.restorable)
|
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()
|
2017-01-20 21:10:10 +01:00
|
|
|
if i.restorable == nil {
|
|
|
|
return false
|
|
|
|
}
|
2016-09-03 15:05:05 +02:00
|
|
|
return i.restorable.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-09-03 19:42:06 +02:00
|
|
|
if i.restorable == nil {
|
2016-07-03 07:42:28 +02:00
|
|
|
return nil
|
|
|
|
}
|
2016-09-03 19:31:50 +02:00
|
|
|
if err := i.restorable.Restore(context); err != nil {
|
2016-07-03 07:42:28 +02:00
|
|
|
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-09-03 19:42:06 +02:00
|
|
|
if i.restorable == nil {
|
2016-07-03 07:42:28 +02:00
|
|
|
return errors.New("ebiten: image is already disposed")
|
|
|
|
}
|
2016-09-03 16:08:51 +02:00
|
|
|
if err := i.restorable.Dispose(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-09-03 19:42:06 +02:00
|
|
|
i.restorable = nil
|
2016-07-03 07:42:28 +02:00
|
|
|
runtime.SetFinalizer(i, nil)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *imageImpl) ReplacePixels(p []uint8) error {
|
2016-09-03 18:25:02 +02:00
|
|
|
w, h := i.restorable.Size()
|
|
|
|
if l := 4 * w * h; len(p) != l {
|
2016-07-03 07:42:28 +02:00
|
|
|
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-09-03 19:42:06 +02:00
|
|
|
if i.restorable == nil {
|
2016-07-03 07:42:28 +02:00
|
|
|
return errors.New("ebiten: image is already disposed")
|
|
|
|
}
|
2017-02-04 20:16:09 +01:00
|
|
|
w2, h2 := graphics.NextPowerOf2Int(w), graphics.NextPowerOf2Int(h)
|
|
|
|
pix := make([]uint8, 4*w2*h2)
|
|
|
|
for j := 0; j < h; j++ {
|
|
|
|
copy(pix[j*w2*4:], p[j*w*4:(j+1)*w*4])
|
|
|
|
}
|
2017-03-03 15:51:25 +01:00
|
|
|
i.restorable.ReplacePixels(pix)
|
2016-09-03 17:07:06 +02:00
|
|
|
return nil
|
2016-07-03 07:42:28 +02:00
|
|
|
}
|
|
|
|
|
2016-07-03 18:50:58 +02:00
|
|
|
func (i *imageImpl) isDisposed() bool {
|
|
|
|
i.m.Lock()
|
|
|
|
defer i.m.Unlock()
|
2016-09-03 19:42:06 +02:00
|
|
|
return i.restorable == nil
|
2016-07-03 18:50:58 +02:00
|
|
|
}
|
|
|
|
|
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-09-03 17:07:06 +02:00
|
|
|
return i.restorable.IsInvalidated(context)
|
2016-07-03 07:42:28 +02:00
|
|
|
}
|