ebiten/internal/buffered/image.go

283 lines
7.1 KiB
Go
Raw Normal View History

// 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 (
"fmt"
2020-10-03 19:35:13 +02:00
"github.com/hajimehoshi/ebiten/v2/internal/affine"
"github.com/hajimehoshi/ebiten/v2/internal/atlas"
2020-10-03 19:35:13 +02:00
"github.com/hajimehoshi/ebiten/v2/internal/graphics"
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
2020-10-03 19:35:13 +02:00
"github.com/hajimehoshi/ebiten/v2/internal/shaderir"
)
type Image struct {
img *atlas.Image
width int
height int
pixels []byte
needsToResolvePixels bool
}
func BeginFrame(graphicsDriver graphicsdriver.Graphics) error {
if err := atlas.BeginFrame(graphicsDriver); err != nil {
return err
}
return flushDelayedCommands()
}
func EndFrame(graphicsDriver graphicsdriver.Graphics) error {
return atlas.EndFrame(graphicsDriver)
}
func NewImage(width, height int) *Image {
i := &Image{
width: width,
height: height,
}
i.initialize()
return i
}
func (i *Image) initialize() {
if maybeCanAddDelayedCommand() {
2020-06-29 18:16:50 +02:00
if tryAddDelayedCommand(func() error {
i.initialize()
return nil
2020-06-29 18:16:50 +02:00
}) {
return
}
}
i.img = atlas.NewImage(i.width, i.height)
}
func (i *Image) SetIndependent(independent bool) {
if maybeCanAddDelayedCommand() {
if tryAddDelayedCommand(func() error {
i.SetIndependent(independent)
return nil
}) {
return
}
}
i.img.SetIndependent(independent)
}
func (i *Image) SetVolatile(volatile bool) {
if maybeCanAddDelayedCommand() {
if tryAddDelayedCommand(func() error {
i.SetVolatile(volatile)
return nil
}) {
return
}
}
i.img.SetVolatile(volatile)
}
func NewScreenFramebufferImage(width, height int) *Image {
i := &Image{}
i.initializeAsScreenFramebuffer(width, height)
return i
}
func (i *Image) initializeAsScreenFramebuffer(width, height int) {
if maybeCanAddDelayedCommand() {
2020-06-29 18:16:50 +02:00
if tryAddDelayedCommand(func() error {
i.initializeAsScreenFramebuffer(width, height)
return nil
2020-06-29 18:16:50 +02:00
}) {
return
}
}
i.img = atlas.NewScreenFramebufferImage(width, height)
i.width = width
i.height = height
}
func (i *Image) invalidatePendingPixels() {
i.pixels = nil
i.needsToResolvePixels = false
}
func (i *Image) resolvePendingPixels(keepPendingPixels bool) {
if i.needsToResolvePixels {
2022-03-20 10:07:29 +01:00
i.img.ReplacePixels(i.pixels)
if !keepPendingPixels {
i.pixels = nil
}
i.needsToResolvePixels = false
}
}
func (i *Image) MarkDisposed() {
if maybeCanAddDelayedCommand() {
2020-06-29 18:16:50 +02:00
if tryAddDelayedCommand(func() error {
i.MarkDisposed()
return nil
2020-06-29 18:16:50 +02:00
}) {
return
}
}
i.invalidatePendingPixels()
i.img.MarkDisposed()
}
2022-03-20 11:03:41 +01:00
func (i *Image) ensurePixels(graphicsDriver graphicsdriver.Graphics) error {
if i.pixels != nil {
return nil
}
pix, err := i.img.Pixels(graphicsDriver)
if err != nil {
return err
}
i.pixels = pix
return nil
}
func (img *Image) At(graphicsDriver graphicsdriver.Graphics, x, y int) (r, g, b, a byte, err error) {
checkDelayedCommandsFlushed("At")
2022-03-20 11:03:41 +01:00
if err := img.ensurePixels(graphicsDriver); err != nil {
return 0, 0, 0, 0, err
}
idx := 4 * (y*img.width + x)
return img.pixels[idx], img.pixels[idx+1], img.pixels[idx+2], img.pixels[idx+3], nil
}
func (i *Image) DumpScreenshot(graphicsDriver graphicsdriver.Graphics, name string, blackbg bool) error {
checkDelayedCommandsFlushed("Dump")
return i.img.DumpScreenshot(graphicsDriver, name, blackbg)
}
2022-03-20 08:44:11 +01:00
// ReplacePixels replaces the pixels at the specified region.
// This call is not accumulated and send one draw call to replace pixels.
func (i *Image) ReplacePixels(pix []byte) {
if l := 4 * i.width * i.height; len(pix) != l {
panic(fmt.Sprintf("buffered: len(pix) was %d but must be %d", len(pix), l))
}
if maybeCanAddDelayedCommand() {
copied := make([]byte, len(pix))
copy(copied, pix)
2020-06-29 18:16:50 +02:00
if tryAddDelayedCommand(func() error {
i.ReplacePixels(copied)
return nil
}) {
2022-03-20 08:44:11 +01:00
return
}
}
i.invalidatePendingPixels()
2022-03-20 10:07:29 +01:00
i.img.ReplacePixels(pix)
}
// ReplacePartial replaces the pixel at the specified partial region.
// This call might be accumulated and send one draw call to replace pixels for the accumulated calls.
func (i *Image) ReplacePartialPixels(graphicsDriver graphicsdriver.Graphics, 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))
}
if maybeCanAddDelayedCommand() {
if tryAddDelayedCommand(func() error {
copied := make([]byte, len(pix))
copy(copied, pix)
i.ReplacePartialPixels(graphicsDriver, copied, x, y, width, height)
return nil
}) {
return nil
}
}
2022-03-20 11:03:41 +01:00
if err := i.ensurePixels(graphicsDriver); err != nil {
return err
}
2022-03-20 11:03:41 +01:00
i.replacePendingPixels(pix, x, y, width, height)
return nil
}
func (i *Image) replacePendingPixels(pix []byte, x, y, width, height int) {
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
}
// DrawTriangles draws the src image with the given vertices.
//
// Copying vertices and indices is the caller's responsibility.
func (i *Image) DrawTriangles(srcs [graphics.ShaderImageNum]*Image, vertices []float32, indices []uint16, colorm affine.ColorM, mode graphicsdriver.CompositeMode, filter graphicsdriver.Filter, address graphicsdriver.Address, dstRegion, srcRegion graphicsdriver.Region, subimageOffsets [graphics.ShaderImageNum - 1][2]float32, shader *Shader, uniforms [][]float32, evenOdd bool) {
for _, src := range srcs {
if i == src {
panic("buffered: Image.DrawTriangles: source images must be different from the receiver")
}
}
if maybeCanAddDelayedCommand() {
2020-06-29 18:16:50 +02:00
if tryAddDelayedCommand(func() error {
// Arguments are not copied. Copying is the caller's responsibility.
i.DrawTriangles(srcs, vertices, indices, colorm, mode, filter, address, dstRegion, srcRegion, subimageOffsets, shader, uniforms, evenOdd)
return nil
2020-06-29 18:16:50 +02:00
}) {
return
}
}
var s *atlas.Shader
var imgs [graphics.ShaderImageNum]*atlas.Image
if shader == nil {
// Fast path for rendering without a shader (#1355).
img := srcs[0]
img.resolvePendingPixels(true)
imgs[0] = img.img
} else {
for i, img := range srcs {
if img == nil {
continue
}
img.resolvePendingPixels(true)
imgs[i] = img.img
}
s = shader.shader
}
i.resolvePendingPixels(false)
i.img.DrawTriangles(imgs, vertices, indices, colorm, mode, filter, address, dstRegion, srcRegion, subimageOffsets, s, uniforms, evenOdd)
i.invalidatePendingPixels()
2020-05-29 20:21:45 +02:00
}
type Shader struct {
shader *atlas.Shader
2020-05-29 20:21:45 +02:00
}
func NewShader(program *shaderir.Program) *Shader {
return &Shader{
shader: atlas.NewShader(program),
2020-05-29 20:21:45 +02:00
}
}
func (s *Shader) MarkDisposed() {
s.shader.MarkDisposed()
s.shader = nil
}