internal/graphicscommand: refactoring: unify NewScreenFrameBufferImage and NewImage

This commit is contained in:
Hajime Hoshi 2022-06-06 09:21:11 +09:00
parent cb0cbb4efa
commit 31fd736ca5
4 changed files with 24 additions and 54 deletions

View File

@ -615,6 +615,7 @@ type newImageCommand struct {
result *Image
width int
height int
screen bool
}
func (c *newImageCommand) String() string {
@ -623,29 +624,12 @@ func (c *newImageCommand) String() string {
// Exec executes a newImageCommand.
func (c *newImageCommand) Exec(graphicsDriver graphicsdriver.Graphics, indexOffset int) error {
i, err := graphicsDriver.NewImage(c.width, c.height)
if err != nil {
return err
}
c.result.image = i
return nil
}
// newScreenFramebufferImageCommand is a command to create a special image for the screen.
type newScreenFramebufferImageCommand struct {
result *Image
width int
height int
}
func (c *newScreenFramebufferImageCommand) String() string {
return fmt.Sprintf("new-screen-framebuffer-image: result: %d, width: %d, height: %d", c.result.id, c.width, c.height)
}
// Exec executes a newScreenFramebufferImageCommand.
func (c *newScreenFramebufferImageCommand) Exec(graphicsDriver graphicsdriver.Graphics, indexOffset int) error {
var err error
c.result.image, err = graphicsDriver.NewScreenFramebufferImage(c.width, c.height)
if c.screen {
c.result.image, err = graphicsDriver.NewScreenFramebufferImage(c.width, c.height)
} else {
c.result.image, err = graphicsDriver.NewImage(c.width, c.height)
}
return err
}

View File

@ -57,32 +57,18 @@ func genNextID() int {
// NewImage returns a new image.
//
// Note that the image is not initialized yet.
func NewImage(width, height int) *Image {
func NewImage(width, height int, screenFramebuffer bool) *Image {
i := &Image{
width: width,
height: height,
screen: screenFramebuffer,
id: genNextID(),
}
c := &newImageCommand{
result: i,
width: width,
height: height,
}
theCommandQueue.Enqueue(c)
return i
}
func NewScreenFramebufferImage(width, height int) *Image {
i := &Image{
width: width,
height: height,
screen: true,
id: genNextID(),
}
c := &newScreenFramebufferImageCommand{
result: i,
width: width,
height: height,
screen: screenFramebuffer,
}
theCommandQueue.Enqueue(c)
return i

View File

@ -43,8 +43,8 @@ func quadVertices(srcImage *graphicscommand.Image, w, h float32) []float32 {
func TestClear(t *testing.T) {
const w, h = 1024, 1024
src := graphicscommand.NewImage(w/2, h/2)
dst := graphicscommand.NewImage(w, h)
src := graphicscommand.NewImage(w/2, h/2, false)
dst := graphicscommand.NewImage(w, h, false)
vs := quadVertices(src, w/2, h/2)
is := graphics.QuadIndices()
@ -74,9 +74,9 @@ func TestClear(t *testing.T) {
func TestReplacePixelsPartAfterDrawTriangles(t *testing.T) {
const w, h = 32, 32
clr := graphicscommand.NewImage(w, h)
src := graphicscommand.NewImage(w/2, h/2)
dst := graphicscommand.NewImage(w, h)
clr := graphicscommand.NewImage(w, h, false)
src := graphicscommand.NewImage(w/2, h/2, false)
dst := graphicscommand.NewImage(w, h, false)
vs := quadVertices(src, w/2, h/2)
is := graphics.QuadIndices()
dr := graphicsdriver.Region{
@ -94,8 +94,8 @@ func TestReplacePixelsPartAfterDrawTriangles(t *testing.T) {
func TestReplacePixelsWithMask(t *testing.T) {
const w, h = 4, 3
src := graphicscommand.NewImage(w, h)
dst := graphicscommand.NewImage(w, h)
src := graphicscommand.NewImage(w, h, false)
dst := graphicscommand.NewImage(w, h, false)
vs := quadVertices(src, w, h)
is := graphics.QuadIndices()
@ -143,8 +143,8 @@ func TestReplacePixelsWithMask(t *testing.T) {
func TestShader(t *testing.T) {
const w, h = 16, 16
clr := graphicscommand.NewImage(w, h)
dst := graphicscommand.NewImage(w, h)
clr := graphicscommand.NewImage(w, h, false)
dst := graphicscommand.NewImage(w, h, false)
vs := quadVertices(clr, w, h)
is := graphics.QuadIndices()
dr := graphicsdriver.Region{

View File

@ -127,7 +127,7 @@ func ensureEmptyImage() *Image {
// w and h are the empty image's size. They indicate the 1x1 image with 1px padding around.
const w, h = 3, 3
emptyImage = &Image{
image: graphicscommand.NewImage(w, h),
image: graphicscommand.NewImage(w, h, false),
width: w,
height: h,
priority: true,
@ -155,7 +155,7 @@ func NewImage(width, height int) *Image {
}
i := &Image{
image: graphicscommand.NewImage(width, height),
image: graphicscommand.NewImage(width, height, false),
width: width,
height: height,
}
@ -237,7 +237,7 @@ func (i *Image) Extend(width, height int) *Image {
// Note that Dispose is not called automatically.
func NewScreenFramebufferImage(width, height int) *Image {
i := &Image{
image: graphicscommand.NewScreenFramebufferImage(width, height),
image: graphicscommand.NewImage(width, height, true),
width: width,
height: height,
imageType: ImageTypeScreenFramebuffer,
@ -600,13 +600,13 @@ func (i *Image) restore(graphicsDriver graphicsdriver.Graphics) error {
case ImageTypeScreenFramebuffer:
// The screen image should also be recreated because framebuffer might
// be changed.
i.image = graphicscommand.NewScreenFramebufferImage(w, h)
i.image = graphicscommand.NewImage(w, h, true)
i.basePixels = Pixels{}
i.clearDrawTrianglesHistory()
i.stale = false
return nil
case ImageTypeVolatile:
i.image = graphicscommand.NewImage(w, h)
i.image = graphicscommand.NewImage(w, h, false)
clearImage(i.image)
return nil
}
@ -615,7 +615,7 @@ func (i *Image) restore(graphicsDriver graphicsdriver.Graphics) error {
panic("restorable: pixels must not be stale when restoring")
}
gimg := graphicscommand.NewImage(w, h)
gimg := graphicscommand.NewImage(w, h, false)
// Clear the image explicitly.
if i != ensureEmptyImage() {
// As clearImage uses emptyImage, clearImage cannot be called on emptyImage.