Skip to content

Commit

Permalink
graphicscommand: Remove CopyPixels command
Browse files Browse the repository at this point in the history
This is an optimization. This change enables to avoid reading
pixels from GPU when extending an image.

Updates #897
  • Loading branch information
hajimehoshi committed Jul 17, 2019
1 parent 736a840 commit efb6f9c
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 60 deletions.
39 changes: 0 additions & 39 deletions internal/graphicscommand/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,45 +361,6 @@ func (c *replacePixelsCommand) CanMerge(dst, src *Image, color *affine.ColorM, m
return false
}

type copyPixelsCommand struct {
dst *Image
src *Image
}

func (c *copyPixelsCommand) String() string {
return fmt.Sprintf("copy-pixels: dst: %p <- src: %p", c.dst, c.src)
}

func (c *copyPixelsCommand) Exec(indexOffset int) error {
p, err := c.src.image.Pixels()
if err != nil {
return err
}
if c.dst.width < c.src.width || c.dst.height < c.src.height {
return fmt.Errorf("graphicscommand: the destination size (%d, %d) must include the source size (%d, %d)", c.dst.width, c.dst.height, c.src.width, c.src.height)
}
c.dst.image.ReplacePixels(p, 0, 0, c.src.width, c.src.height)
return nil
}

func (c *copyPixelsCommand) NumVertices() int {
return 0
}

func (c *copyPixelsCommand) NumIndices() int {
return 0
}

func (c *copyPixelsCommand) AddNumVertices(n int) {
}

func (c *copyPixelsCommand) AddNumIndices(n int) {
}

func (c *copyPixelsCommand) CanMerge(dst, src *Image, color *affine.ColorM, mode driver.CompositeMode, filter driver.Filter, address driver.Address) bool {
return false
}

type pixelsCommand struct {
result []byte
img *Image
Expand Down
18 changes: 0 additions & 18 deletions internal/graphicscommand/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,24 +145,6 @@ func (i *Image) ReplacePixels(p []byte, x, y, width, height int) {
i.lastCommand = lastCommandReplacePixels
}

// CopyPixels is basically same as Pixels and ReplacePixels, but reading pixels from GPU is done lazily.
func (i *Image) CopyPixels(src *Image) {
if i.lastCommand == lastCommandDrawTriangles {
if i.width != src.width || i.height != src.height {
panic("graphicscommand: Copy for a part after DrawTriangles is forbidden")
}
}

c := &copyPixelsCommand{
dst: i,
src: src,
}
theCommandQueue.Enqueue(c)

// The execution is basically same as replacing pixels.
i.lastCommand = lastCommandReplacePixels
}

func (i *Image) IsInvalidated() bool {
if i.screen {
// The screen image might not have a texture, and in this case it is impossible to detect whether
Expand Down
9 changes: 6 additions & 3 deletions internal/restorable/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,9 @@ func (i *Image) Extend(width, height int) *Image {

newImg := NewImage(width, height)

// Do not use DrawTriangles here. ReplacePixels will be called on a part of newImg later, and it looked like
// ReplacePixels on a part of image deletes other region that are rendered by DrawTriangles (#593, #758).
newImg.image.CopyPixels(i.image)
if i.basePixels != nil && i.basePixels.pixels != nil {
newImg.image.ReplacePixels(i.basePixels.pixels, 0, 0, w, h)
}

// Copy basePixels.
newImg.basePixels = &Pixels{
Expand Down Expand Up @@ -359,6 +359,9 @@ func (i *Image) ReplacePixels(pixels []byte, x, y, width, height int) {
return
}

// It looked like ReplacePixels on a part of image deletes other region that are rendered by DrawTriangles
// (#593, #758).

if len(i.drawTrianglesHistory) > 0 {
panic("restorable: ReplacePixels for a part after DrawTriangles is forbidden")
}
Expand Down

0 comments on commit efb6f9c

Please sign in to comment.