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 result *Image
width int width int
height int height int
screen bool
} }
func (c *newImageCommand) String() string { func (c *newImageCommand) String() string {
@ -623,29 +624,12 @@ func (c *newImageCommand) String() string {
// Exec executes a newImageCommand. // Exec executes a newImageCommand.
func (c *newImageCommand) Exec(graphicsDriver graphicsdriver.Graphics, indexOffset int) error { 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 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 return err
} }

View File

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

View File

@ -43,8 +43,8 @@ func quadVertices(srcImage *graphicscommand.Image, w, h float32) []float32 {
func TestClear(t *testing.T) { func TestClear(t *testing.T) {
const w, h = 1024, 1024 const w, h = 1024, 1024
src := graphicscommand.NewImage(w/2, h/2) src := graphicscommand.NewImage(w/2, h/2, false)
dst := graphicscommand.NewImage(w, h) dst := graphicscommand.NewImage(w, h, false)
vs := quadVertices(src, w/2, h/2) vs := quadVertices(src, w/2, h/2)
is := graphics.QuadIndices() is := graphics.QuadIndices()
@ -74,9 +74,9 @@ func TestClear(t *testing.T) {
func TestReplacePixelsPartAfterDrawTriangles(t *testing.T) { func TestReplacePixelsPartAfterDrawTriangles(t *testing.T) {
const w, h = 32, 32 const w, h = 32, 32
clr := graphicscommand.NewImage(w, h) clr := graphicscommand.NewImage(w, h, false)
src := graphicscommand.NewImage(w/2, h/2) src := graphicscommand.NewImage(w/2, h/2, false)
dst := graphicscommand.NewImage(w, h) dst := graphicscommand.NewImage(w, h, false)
vs := quadVertices(src, w/2, h/2) vs := quadVertices(src, w/2, h/2)
is := graphics.QuadIndices() is := graphics.QuadIndices()
dr := graphicsdriver.Region{ dr := graphicsdriver.Region{
@ -94,8 +94,8 @@ func TestReplacePixelsPartAfterDrawTriangles(t *testing.T) {
func TestReplacePixelsWithMask(t *testing.T) { func TestReplacePixelsWithMask(t *testing.T) {
const w, h = 4, 3 const w, h = 4, 3
src := graphicscommand.NewImage(w, h) src := graphicscommand.NewImage(w, h, false)
dst := graphicscommand.NewImage(w, h) dst := graphicscommand.NewImage(w, h, false)
vs := quadVertices(src, w, h) vs := quadVertices(src, w, h)
is := graphics.QuadIndices() is := graphics.QuadIndices()
@ -143,8 +143,8 @@ func TestReplacePixelsWithMask(t *testing.T) {
func TestShader(t *testing.T) { func TestShader(t *testing.T) {
const w, h = 16, 16 const w, h = 16, 16
clr := graphicscommand.NewImage(w, h) clr := graphicscommand.NewImage(w, h, false)
dst := graphicscommand.NewImage(w, h) dst := graphicscommand.NewImage(w, h, false)
vs := quadVertices(clr, w, h) vs := quadVertices(clr, w, h)
is := graphics.QuadIndices() is := graphics.QuadIndices()
dr := graphicsdriver.Region{ 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. // w and h are the empty image's size. They indicate the 1x1 image with 1px padding around.
const w, h = 3, 3 const w, h = 3, 3
emptyImage = &Image{ emptyImage = &Image{
image: graphicscommand.NewImage(w, h), image: graphicscommand.NewImage(w, h, false),
width: w, width: w,
height: h, height: h,
priority: true, priority: true,
@ -155,7 +155,7 @@ func NewImage(width, height int) *Image {
} }
i := &Image{ i := &Image{
image: graphicscommand.NewImage(width, height), image: graphicscommand.NewImage(width, height, false),
width: width, width: width,
height: height, height: height,
} }
@ -237,7 +237,7 @@ func (i *Image) Extend(width, height int) *Image {
// Note that Dispose is not called automatically. // Note that Dispose is not called automatically.
func NewScreenFramebufferImage(width, height int) *Image { func NewScreenFramebufferImage(width, height int) *Image {
i := &Image{ i := &Image{
image: graphicscommand.NewScreenFramebufferImage(width, height), image: graphicscommand.NewImage(width, height, true),
width: width, width: width,
height: height, height: height,
imageType: ImageTypeScreenFramebuffer, imageType: ImageTypeScreenFramebuffer,
@ -600,13 +600,13 @@ func (i *Image) restore(graphicsDriver graphicsdriver.Graphics) error {
case ImageTypeScreenFramebuffer: case ImageTypeScreenFramebuffer:
// The screen image should also be recreated because framebuffer might // The screen image should also be recreated because framebuffer might
// be changed. // be changed.
i.image = graphicscommand.NewScreenFramebufferImage(w, h) i.image = graphicscommand.NewImage(w, h, true)
i.basePixels = Pixels{} i.basePixels = Pixels{}
i.clearDrawTrianglesHistory() i.clearDrawTrianglesHistory()
i.stale = false i.stale = false
return nil return nil
case ImageTypeVolatile: case ImageTypeVolatile:
i.image = graphicscommand.NewImage(w, h) i.image = graphicscommand.NewImage(w, h, false)
clearImage(i.image) clearImage(i.image)
return nil return nil
} }
@ -615,7 +615,7 @@ func (i *Image) restore(graphicsDriver graphicsdriver.Graphics) error {
panic("restorable: pixels must not be stale when restoring") panic("restorable: pixels must not be stale when restoring")
} }
gimg := graphicscommand.NewImage(w, h) gimg := graphicscommand.NewImage(w, h, false)
// Clear the image explicitly. // Clear the image explicitly.
if i != ensureEmptyImage() { if i != ensureEmptyImage() {
// As clearImage uses emptyImage, clearImage cannot be called on emptyImage. // As clearImage uses emptyImage, clearImage cannot be called on emptyImage.