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"
|
2023-04-27 17:55:10 +02:00
|
|
|
"image"
|
2019-09-19 05:02:35 +02:00
|
|
|
|
2021-03-11 15:13:24 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/atlas"
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphics"
|
2022-02-06 12:41:32 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
|
2023-02-25 17:31:11 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/restorable"
|
2019-09-19 05:02:35 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Image struct {
|
2021-03-11 15:13:24 +01:00
|
|
|
img *atlas.Image
|
2019-10-11 20:09:43 +02:00
|
|
|
width int
|
|
|
|
height int
|
|
|
|
|
2023-02-25 17:31:11 +01:00
|
|
|
// pixels is valid only when restorable.AlwaysReadPixelsFromGPU() returns true.
|
2022-07-05 06:26:45 +02:00
|
|
|
pixels []byte
|
2019-09-19 05:02:35 +02:00
|
|
|
}
|
|
|
|
|
2022-03-19 15:55:14 +01:00
|
|
|
func BeginFrame(graphicsDriver graphicsdriver.Graphics) error {
|
2023-10-18 16:18:08 +02:00
|
|
|
return atlas.BeginFrame(graphicsDriver)
|
2019-09-19 05:02:35 +02:00
|
|
|
}
|
|
|
|
|
2023-07-29 19:25:10 +02:00
|
|
|
func EndFrame(graphicsDriver graphicsdriver.Graphics, swapBuffersForGL func()) error {
|
|
|
|
return atlas.EndFrame(graphicsDriver, swapBuffersForGL)
|
2019-09-19 05:02:35 +02:00
|
|
|
}
|
|
|
|
|
2022-06-07 16:57:56 +02:00
|
|
|
func NewImage(width, height int, imageType atlas.ImageType) *Image {
|
2023-10-18 15:30:01 +02:00
|
|
|
return &Image{
|
2022-03-20 10:01:27 +01:00
|
|
|
width: width,
|
|
|
|
height: height,
|
2023-10-18 15:30:01 +02:00
|
|
|
img: atlas.NewImage(width, height, imageType),
|
2022-03-20 10:01:27 +01:00
|
|
|
}
|
2019-09-19 05:02:35 +02:00
|
|
|
}
|
|
|
|
|
2022-03-21 07:27:21 +01:00
|
|
|
func (i *Image) invalidatePixels() {
|
2019-11-08 21:43:33 +01:00
|
|
|
i.pixels = nil
|
2019-10-11 20:09:43 +02:00
|
|
|
}
|
|
|
|
|
2019-09-19 05:02:35 +02:00
|
|
|
func (i *Image) MarkDisposed() {
|
2020-02-16 11:01:16 +01:00
|
|
|
i.img.MarkDisposed()
|
2019-09-19 05:02:35 +02:00
|
|
|
}
|
|
|
|
|
2023-04-27 17:55:10 +02:00
|
|
|
func (i *Image) ReadPixels(graphicsDriver graphicsdriver.Graphics, pixels []byte, region image.Rectangle) error {
|
2023-02-25 17:31:11 +01:00
|
|
|
// If restorable.AlwaysReadPixelsFromGPU() returns false, the pixel data is cached in the restorable package.
|
|
|
|
if !restorable.AlwaysReadPixelsFromGPU() {
|
2023-04-27 17:55:10 +02:00
|
|
|
if err := i.img.ReadPixels(graphicsDriver, pixels, region); err != nil {
|
2023-02-25 17:31:11 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-05 16:57:56 +02:00
|
|
|
if i.pixels == nil {
|
|
|
|
pix := make([]byte, 4*i.width*i.height)
|
2023-04-27 17:55:10 +02:00
|
|
|
if err := i.img.ReadPixels(graphicsDriver, pix, image.Rect(0, 0, i.width, i.height)); err != nil {
|
2022-08-05 16:57:56 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
i.pixels = pix
|
2022-03-20 20:11:36 +01:00
|
|
|
}
|
2022-08-05 16:57:56 +02:00
|
|
|
|
2023-04-27 17:55:10 +02:00
|
|
|
lineWidth := 4 * region.Dx()
|
|
|
|
for j := 0; j < region.Dy(); j++ {
|
|
|
|
dstX := 4 * j * region.Dx()
|
|
|
|
srcX := 4 * ((region.Min.Y+j)*i.width + region.Min.X)
|
2022-08-05 16:57:56 +02:00
|
|
|
copy(pixels[dstX:dstX+lineWidth], i.pixels[srcX:srcX+lineWidth])
|
|
|
|
}
|
|
|
|
return nil
|
2019-09-19 05:02:35 +02:00
|
|
|
}
|
|
|
|
|
2022-08-31 06:10:41 +02:00
|
|
|
func (i *Image) DumpScreenshot(graphicsDriver graphicsdriver.Graphics, name string, blackbg bool) (string, error) {
|
2022-03-19 15:55:14 +01:00
|
|
|
return i.img.DumpScreenshot(graphicsDriver, name, blackbg)
|
2019-09-19 05:02:35 +02:00
|
|
|
}
|
|
|
|
|
2022-08-07 20:29:12 +02:00
|
|
|
// WritePixels replaces the pixels at the specified region.
|
2023-04-27 17:55:10 +02:00
|
|
|
func (i *Image) WritePixels(pix []byte, region image.Rectangle) {
|
|
|
|
if l := 4 * region.Dx() * region.Dy(); len(pix) != l {
|
2020-02-16 13:45:26 +01:00
|
|
|
panic(fmt.Sprintf("buffered: len(pix) was %d but must be %d", len(pix), l))
|
|
|
|
}
|
2023-10-18 16:03:05 +02:00
|
|
|
i.invalidatePixels()
|
2020-02-16 13:45:26 +01:00
|
|
|
|
2023-04-27 17:55:10 +02:00
|
|
|
i.img.WritePixels(pix, region)
|
2019-09-19 05:02:35 +02:00
|
|
|
}
|
|
|
|
|
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.
|
2023-10-18 17:42:20 +02:00
|
|
|
func (i *Image) DrawTriangles(srcs [graphics.ShaderImageCount]*Image, vertices []float32, indices []uint16, blend graphicsdriver.Blend, dstRegion image.Rectangle, srcRegions [graphics.ShaderImageCount]image.Rectangle, shader *atlas.Shader, uniforms []uint32, evenOdd bool) {
|
2020-07-17 18:09:58 +02:00
|
|
|
for _, src := range srcs {
|
2020-06-03 18:35:35 +02:00
|
|
|
if i == src {
|
2020-07-14 20:37:35 +02:00
|
|
|
panic("buffered: Image.DrawTriangles: source images must be different from the receiver")
|
2020-06-03 18:35:35 +02:00
|
|
|
}
|
2019-09-19 05:02:35 +02:00
|
|
|
}
|
2023-10-18 16:03:05 +02:00
|
|
|
i.invalidatePixels()
|
2019-09-26 20:12:21 +02:00
|
|
|
|
2022-07-12 18:46:02 +02:00
|
|
|
var imgs [graphics.ShaderImageCount]*atlas.Image
|
2022-10-02 16:49:07 +02:00
|
|
|
for i, img := range srcs {
|
|
|
|
if img == nil {
|
|
|
|
continue
|
2020-07-17 18:09:58 +02:00
|
|
|
}
|
2022-10-02 16:49:07 +02:00
|
|
|
imgs[i] = img.img
|
2020-07-05 18:55:46 +02:00
|
|
|
}
|
|
|
|
|
2023-10-18 17:42:20 +02:00
|
|
|
i.img.DrawTriangles(imgs, vertices, indices, blend, dstRegion, srcRegions, shader, uniforms, evenOdd)
|
2020-05-29 20:21:45 +02:00
|
|
|
}
|