2019-09-19 05:02:35 +02:00
|
|
|
// Copyright 2019 The Ebiten Authors
|
|
|
|
//
|
|
|
|
// 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 buffered
|
|
|
|
|
|
|
|
import (
|
2020-02-16 13:45:26 +01:00
|
|
|
"fmt"
|
2020-01-07 16:15:46 +01:00
|
|
|
"image"
|
2019-09-19 05:02:35 +02:00
|
|
|
"image/color"
|
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten/internal/affine"
|
|
|
|
"github.com/hajimehoshi/ebiten/internal/driver"
|
2020-01-07 16:15:46 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/mipmap"
|
2019-09-19 05:02:35 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Image struct {
|
2020-01-07 16:15:46 +01:00
|
|
|
img *mipmap.Mipmap
|
2019-10-11 20:09:43 +02:00
|
|
|
width int
|
|
|
|
height int
|
|
|
|
|
2020-04-17 19:11:23 +02:00
|
|
|
hasFill bool
|
|
|
|
fillColor color.RGBA
|
|
|
|
|
2019-11-08 21:43:33 +01:00
|
|
|
pixels []byte
|
|
|
|
needsToResolvePixels bool
|
2019-09-19 05:02:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func BeginFrame() error {
|
2020-01-07 16:15:46 +01:00
|
|
|
if err := mipmap.BeginFrame(); err != nil {
|
2019-09-19 05:02:35 +02:00
|
|
|
return err
|
|
|
|
}
|
2020-01-18 17:18:56 +01:00
|
|
|
return flushDelayedCommands()
|
2019-09-19 05:02:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func EndFrame() error {
|
2020-01-07 16:15:46 +01:00
|
|
|
return mipmap.EndFrame()
|
2019-09-19 05:02:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewImage(width, height int, volatile bool) *Image {
|
|
|
|
i := &Image{}
|
2020-02-16 11:24:26 +01:00
|
|
|
i.initialize(width, height, volatile)
|
|
|
|
return i
|
|
|
|
}
|
2020-02-08 12:58:23 +01:00
|
|
|
|
2020-02-16 11:24:26 +01:00
|
|
|
func (i *Image) initialize(width, height int, volatile bool) {
|
2019-09-26 20:12:21 +02:00
|
|
|
delayedCommandsM.Lock()
|
2020-02-08 12:58:23 +01:00
|
|
|
defer delayedCommandsM.Unlock()
|
|
|
|
|
2019-09-26 20:12:21 +02:00
|
|
|
if needsToDelayCommands {
|
2020-01-18 17:18:56 +01:00
|
|
|
delayedCommands = append(delayedCommands, func() error {
|
2020-02-16 11:24:26 +01:00
|
|
|
i.initialize(width, height, volatile)
|
2020-01-18 17:18:56 +01:00
|
|
|
return nil
|
2019-09-26 20:12:21 +02:00
|
|
|
})
|
2020-02-16 11:24:26 +01:00
|
|
|
return
|
2019-09-26 20:12:21 +02:00
|
|
|
}
|
2020-01-07 16:15:46 +01:00
|
|
|
i.img = mipmap.New(width, height, volatile)
|
2019-10-11 20:09:43 +02:00
|
|
|
i.width = width
|
|
|
|
i.height = height
|
2019-09-19 05:02:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewScreenFramebufferImage(width, height int) *Image {
|
|
|
|
i := &Image{}
|
2020-02-16 11:24:26 +01:00
|
|
|
i.initializeAsScreenFramebuffer(width, height)
|
|
|
|
return i
|
|
|
|
}
|
2020-02-08 12:58:23 +01:00
|
|
|
|
2020-02-16 11:24:26 +01:00
|
|
|
func (i *Image) initializeAsScreenFramebuffer(width, height int) {
|
2019-09-26 20:12:21 +02:00
|
|
|
delayedCommandsM.Lock()
|
2020-02-08 12:58:23 +01:00
|
|
|
defer delayedCommandsM.Unlock()
|
|
|
|
|
2019-09-26 20:12:21 +02:00
|
|
|
if needsToDelayCommands {
|
2020-01-18 17:18:56 +01:00
|
|
|
delayedCommands = append(delayedCommands, func() error {
|
2020-02-16 11:24:26 +01:00
|
|
|
i.initializeAsScreenFramebuffer(width, height)
|
2020-01-18 17:18:56 +01:00
|
|
|
return nil
|
2019-09-26 20:12:21 +02:00
|
|
|
})
|
2020-02-16 11:24:26 +01:00
|
|
|
return
|
2019-09-26 20:12:21 +02:00
|
|
|
}
|
|
|
|
|
2020-01-07 16:15:46 +01:00
|
|
|
i.img = mipmap.NewScreenFramebufferMipmap(width, height)
|
2019-10-11 20:09:43 +02:00
|
|
|
i.width = width
|
|
|
|
i.height = height
|
2019-09-19 05:02:35 +02:00
|
|
|
}
|
|
|
|
|
2019-10-11 20:09:43 +02:00
|
|
|
func (i *Image) invalidatePendingPixels() {
|
2019-11-08 21:43:33 +01:00
|
|
|
i.pixels = nil
|
2019-11-08 21:54:24 +01:00
|
|
|
i.needsToResolvePixels = false
|
2020-04-17 19:11:23 +02:00
|
|
|
i.hasFill = false
|
2019-10-11 20:09:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Image) resolvePendingPixels(keepPendingPixels bool) {
|
2020-04-17 19:11:23 +02:00
|
|
|
if i.needsToResolvePixels && i.hasFill {
|
|
|
|
panic("buffered: needsToResolvePixels and hasFill must not be true at the same time")
|
|
|
|
}
|
|
|
|
if i.needsToResolvePixels {
|
|
|
|
i.img.ReplacePixels(i.pixels)
|
|
|
|
if !keepPendingPixels {
|
|
|
|
i.pixels = nil
|
|
|
|
}
|
|
|
|
i.needsToResolvePixels = false
|
2019-10-11 20:09:43 +02:00
|
|
|
}
|
2020-04-17 19:11:23 +02:00
|
|
|
i.resolvePendingFill()
|
|
|
|
}
|
2019-10-11 20:09:43 +02:00
|
|
|
|
2020-04-17 19:11:23 +02:00
|
|
|
func (i *Image) resolvePendingFill() {
|
|
|
|
if !i.hasFill {
|
|
|
|
return
|
2019-10-11 20:09:43 +02:00
|
|
|
}
|
2020-04-17 19:11:23 +02:00
|
|
|
i.img.Fill(i.fillColor)
|
|
|
|
i.hasFill = false
|
2019-10-11 20:09:43 +02:00
|
|
|
}
|
|
|
|
|
2019-09-19 05:02:35 +02:00
|
|
|
func (i *Image) MarkDisposed() {
|
2019-09-26 20:12:21 +02:00
|
|
|
delayedCommandsM.Lock()
|
2020-02-08 12:58:23 +01:00
|
|
|
defer delayedCommandsM.Unlock()
|
|
|
|
|
2019-09-26 20:12:21 +02:00
|
|
|
if needsToDelayCommands {
|
2020-01-18 17:18:56 +01:00
|
|
|
delayedCommands = append(delayedCommands, func() error {
|
2020-02-16 11:24:26 +01:00
|
|
|
i.MarkDisposed()
|
2020-01-18 17:18:56 +01:00
|
|
|
return nil
|
2019-09-26 20:12:21 +02:00
|
|
|
})
|
2019-10-11 20:09:43 +02:00
|
|
|
return
|
2019-09-26 20:12:21 +02:00
|
|
|
}
|
2019-10-11 20:09:43 +02:00
|
|
|
i.invalidatePendingPixels()
|
2020-02-16 11:01:16 +01:00
|
|
|
i.img.MarkDisposed()
|
2019-09-19 05:02:35 +02:00
|
|
|
}
|
|
|
|
|
2020-01-18 17:18:56 +01:00
|
|
|
func (i *Image) At(x, y int) (r, g, b, a byte, err error) {
|
2019-09-26 20:12:21 +02:00
|
|
|
delayedCommandsM.Lock()
|
|
|
|
defer delayedCommandsM.Unlock()
|
|
|
|
if needsToDelayCommands {
|
2019-09-19 05:02:35 +02:00
|
|
|
panic("buffered: the command queue is not available yet at At")
|
|
|
|
}
|
2020-04-17 19:11:23 +02:00
|
|
|
|
2020-04-18 13:01:40 +02:00
|
|
|
if x < 0 || y < 0 || x >= i.width || y >= i.height {
|
|
|
|
return 0, 0, 0, 0, nil
|
|
|
|
}
|
|
|
|
|
2020-04-17 19:11:23 +02:00
|
|
|
// If there are pixels or pending fillling that needs to be resolved, use this rather than resolving.
|
|
|
|
// Resolving them needs to access GPU and is expensive (#1137).
|
|
|
|
if i.hasFill {
|
|
|
|
return i.fillColor.R, i.fillColor.G, i.fillColor.B, i.fillColor.A, nil
|
|
|
|
}
|
2020-04-18 13:01:40 +02:00
|
|
|
|
|
|
|
if i.pixels == nil {
|
|
|
|
pix, err := i.img.Pixels(0, 0, i.width, i.height)
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, 0, 0, err
|
|
|
|
}
|
|
|
|
i.pixels = pix
|
2020-04-17 14:34:46 +02:00
|
|
|
}
|
2020-04-18 13:01:40 +02:00
|
|
|
|
|
|
|
idx := i.width*y + x
|
|
|
|
return i.pixels[4*idx], i.pixels[4*idx+1], i.pixels[4*idx+2], i.pixels[4*idx+3], nil
|
2019-09-19 05:02:35 +02:00
|
|
|
}
|
|
|
|
|
2020-02-05 19:03:38 +01:00
|
|
|
func (i *Image) Dump(name string, blackbg bool) error {
|
2019-09-26 20:12:21 +02:00
|
|
|
delayedCommandsM.Lock()
|
|
|
|
defer delayedCommandsM.Unlock()
|
|
|
|
if needsToDelayCommands {
|
2019-09-19 05:02:35 +02:00
|
|
|
panic("buffered: the command queue is not available yet at Dump")
|
|
|
|
}
|
2020-02-05 19:03:38 +01:00
|
|
|
return i.img.Dump(name, blackbg)
|
2019-09-19 05:02:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Image) Fill(clr color.RGBA) {
|
2019-09-26 20:12:21 +02:00
|
|
|
delayedCommandsM.Lock()
|
2020-02-08 12:58:23 +01:00
|
|
|
defer delayedCommandsM.Unlock()
|
|
|
|
|
2019-09-26 20:12:21 +02:00
|
|
|
if needsToDelayCommands {
|
2020-01-18 17:18:56 +01:00
|
|
|
delayedCommands = append(delayedCommands, func() error {
|
2020-02-16 11:24:26 +01:00
|
|
|
i.Fill(clr)
|
2020-01-18 17:18:56 +01:00
|
|
|
return nil
|
2019-09-26 20:12:21 +02:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-17 19:11:23 +02:00
|
|
|
// Defer filling the image so that successive fillings will be merged into one (#1134).
|
2019-10-11 20:09:43 +02:00
|
|
|
i.invalidatePendingPixels()
|
2020-04-17 19:11:23 +02:00
|
|
|
i.fillColor = clr
|
|
|
|
i.hasFill = true
|
2019-09-19 05:02:35 +02:00
|
|
|
}
|
|
|
|
|
2020-02-16 13:45:26 +01:00
|
|
|
func (i *Image) ReplacePixels(pix []byte, x, y, width, height int) error {
|
|
|
|
if l := 4 * width * height; len(pix) != l {
|
|
|
|
panic(fmt.Sprintf("buffered: len(pix) was %d but must be %d", len(pix), l))
|
|
|
|
}
|
|
|
|
|
2020-04-18 17:18:10 +02:00
|
|
|
// This is an optimization to avoid mutex for the case when ReplacePixels is called very often (e.g., Set).
|
|
|
|
// If i.pixels is not nil, delayed commands have already been flushed.
|
|
|
|
// needsToDelayCommands should be false, but we don't check it because this is out of the mutex lock.
|
|
|
|
// (#1137)
|
|
|
|
if i.pixels != nil {
|
|
|
|
// If the region is the whole image, don't use this optimization, or more memory is consumed by
|
|
|
|
// keeping pixels.
|
|
|
|
if !(x == 0 && y == 0 && width == i.width && height == i.height) {
|
|
|
|
i.replacePendingPixels(pix, x, y, width, height)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-26 20:12:21 +02:00
|
|
|
delayedCommandsM.Lock()
|
2020-02-08 12:58:23 +01:00
|
|
|
defer delayedCommandsM.Unlock()
|
|
|
|
|
2019-09-26 20:12:21 +02:00
|
|
|
if needsToDelayCommands {
|
2020-02-16 12:15:45 +01:00
|
|
|
copied := make([]byte, len(pix))
|
|
|
|
copy(copied, pix)
|
2020-01-18 17:18:56 +01:00
|
|
|
delayedCommands = append(delayedCommands, func() error {
|
2020-02-16 13:45:26 +01:00
|
|
|
i.ReplacePixels(copied, x, y, width, height)
|
2020-01-18 17:18:56 +01:00
|
|
|
return nil
|
2019-09-26 20:12:21 +02:00
|
|
|
})
|
2020-02-16 13:45:26 +01:00
|
|
|
return nil
|
2019-09-26 20:12:21 +02:00
|
|
|
}
|
|
|
|
|
2020-04-17 19:11:23 +02:00
|
|
|
i.resolvePendingFill()
|
|
|
|
|
2020-02-16 13:45:26 +01:00
|
|
|
if x == 0 && y == 0 && width == i.width && height == i.height {
|
|
|
|
i.invalidatePendingPixels()
|
|
|
|
i.img.ReplacePixels(pix)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-16 14:16:27 +01:00
|
|
|
// TODO: Can we use (*restorable.Image).ReplacePixels?
|
2020-02-16 13:45:26 +01:00
|
|
|
if i.pixels == nil {
|
2020-04-18 13:01:40 +02:00
|
|
|
pix, err := i.img.Pixels(0, 0, i.width, i.height)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-02-16 13:45:26 +01:00
|
|
|
}
|
|
|
|
i.pixels = pix
|
|
|
|
}
|
2020-04-18 17:18:10 +02:00
|
|
|
i.replacePendingPixels(pix, x, y, width, height)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Image) replacePendingPixels(pix []byte, x, y, width, height int) {
|
2020-02-16 13:45:26 +01:00
|
|
|
for j := 0; j < height; j++ {
|
|
|
|
copy(i.pixels[4*((j+y)*i.width+x):], pix[4*j*width:4*(j+1)*width])
|
|
|
|
}
|
|
|
|
i.needsToResolvePixels = true
|
2019-09-19 05:02:35 +02:00
|
|
|
}
|
|
|
|
|
2020-01-07 16:15:46 +01:00
|
|
|
func (i *Image) DrawImage(src *Image, bounds image.Rectangle, a, b, c, d, tx, ty float32, colorm *affine.ColorM, mode driver.CompositeMode, filter driver.Filter) {
|
|
|
|
if i == src {
|
|
|
|
panic("buffered: Image.DrawImage: src must be different from the receiver")
|
|
|
|
}
|
|
|
|
|
2020-02-22 05:40:17 +01:00
|
|
|
g := mipmap.GeoM{
|
2020-01-07 16:15:46 +01:00
|
|
|
A: a,
|
|
|
|
B: b,
|
|
|
|
C: c,
|
|
|
|
D: d,
|
|
|
|
Tx: tx,
|
|
|
|
Ty: ty,
|
|
|
|
}
|
|
|
|
|
|
|
|
delayedCommandsM.Lock()
|
2020-02-08 12:58:23 +01:00
|
|
|
defer delayedCommandsM.Unlock()
|
|
|
|
|
2020-01-07 16:15:46 +01:00
|
|
|
if needsToDelayCommands {
|
2020-01-18 17:18:56 +01:00
|
|
|
delayedCommands = append(delayedCommands, func() error {
|
2020-01-10 19:11:56 +01:00
|
|
|
i.drawImage(src, bounds, g, colorm, mode, filter)
|
2020-01-18 17:18:56 +01:00
|
|
|
return nil
|
2020-01-07 16:15:46 +01:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-10 19:11:56 +01:00
|
|
|
i.drawImage(src, bounds, g, colorm, mode, filter)
|
|
|
|
}
|
|
|
|
|
2020-02-22 05:40:17 +01:00
|
|
|
func (i *Image) drawImage(src *Image, bounds image.Rectangle, g mipmap.GeoM, colorm *affine.ColorM, mode driver.CompositeMode, filter driver.Filter) {
|
2020-01-07 16:15:46 +01:00
|
|
|
src.resolvePendingPixels(true)
|
|
|
|
i.resolvePendingPixels(false)
|
|
|
|
i.img.DrawImage(src.img, bounds, g, colorm, mode, filter)
|
|
|
|
}
|
|
|
|
|
2020-02-16 12:15:45 +01:00
|
|
|
// DrawTriangles draws the src image with the given vertices.
|
|
|
|
//
|
|
|
|
// Copying vertices and indices is the caller's responsibility.
|
2019-09-19 05:02:35 +02:00
|
|
|
func (i *Image) DrawTriangles(src *Image, vertices []float32, indices []uint16, colorm *affine.ColorM, mode driver.CompositeMode, filter driver.Filter, address driver.Address) {
|
|
|
|
if i == src {
|
|
|
|
panic("buffered: Image.DrawTriangles: src must be different from the receiver")
|
|
|
|
}
|
2019-09-26 20:12:21 +02:00
|
|
|
|
|
|
|
delayedCommandsM.Lock()
|
2020-02-08 12:58:23 +01:00
|
|
|
defer delayedCommandsM.Unlock()
|
|
|
|
|
2019-09-26 20:12:21 +02:00
|
|
|
if needsToDelayCommands {
|
2020-01-18 17:18:56 +01:00
|
|
|
delayedCommands = append(delayedCommands, func() error {
|
2020-02-16 12:15:45 +01:00
|
|
|
// Arguments are not copied. Copying is the caller's responsibility.
|
2020-02-16 12:03:04 +01:00
|
|
|
i.DrawTriangles(src, vertices, indices, colorm, mode, filter, address)
|
2020-01-18 17:18:56 +01:00
|
|
|
return nil
|
2019-09-26 20:12:21 +02:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-11 20:09:43 +02:00
|
|
|
src.resolvePendingPixels(true)
|
|
|
|
i.resolvePendingPixels(false)
|
2019-09-26 20:12:21 +02:00
|
|
|
i.img.DrawTriangles(src.img, vertices, indices, colorm, mode, filter, address)
|
2019-09-19 05:02:35 +02:00
|
|
|
}
|