From 31fd736ca5e2f8871522ef332a681d73977d740b Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Mon, 6 Jun 2022 09:21:11 +0900 Subject: [PATCH] internal/graphicscommand: refactoring: unify NewScreenFrameBufferImage and NewImage --- internal/graphicscommand/command.go | 28 ++++++-------------------- internal/graphicscommand/image.go | 20 +++--------------- internal/graphicscommand/image_test.go | 18 ++++++++--------- internal/restorable/image.go | 12 +++++------ 4 files changed, 24 insertions(+), 54 deletions(-) diff --git a/internal/graphicscommand/command.go b/internal/graphicscommand/command.go index ea47038f2..d8b6ae7b5 100644 --- a/internal/graphicscommand/command.go +++ b/internal/graphicscommand/command.go @@ -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 } diff --git a/internal/graphicscommand/image.go b/internal/graphicscommand/image.go index 0b7cb9e4a..67fdb780a 100644 --- a/internal/graphicscommand/image.go +++ b/internal/graphicscommand/image.go @@ -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 diff --git a/internal/graphicscommand/image_test.go b/internal/graphicscommand/image_test.go index 04badab5c..f205666b9 100644 --- a/internal/graphicscommand/image_test.go +++ b/internal/graphicscommand/image_test.go @@ -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{ diff --git a/internal/restorable/image.go b/internal/restorable/image.go index a50b9838d..36c95a728 100644 --- a/internal/restorable/image.go +++ b/internal/restorable/image.go @@ -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.