graphics: Refactoring: Add pixelCommand

This commit is contained in:
Hajime Hoshi 2018-07-12 02:40:06 +09:00
parent aa06c5ffa5
commit a9a21132ae
2 changed files with 42 additions and 6 deletions

View File

@ -309,6 +309,43 @@ func (c *replacePixelsCommand) CanMerge(dst, src *Image, color *affine.ColorM, m
return false
}
type pixelsCommand struct {
result []byte
img *Image
}
// Exec executes a pixelsCommand.
func (c *pixelsCommand) Exec(indexOffsetInBytes int) error {
f, err := c.img.createFramebufferIfNeeded()
if err != nil {
return err
}
p, err := opengl.GetContext().FramebufferPixels(f.native, c.img.width, c.img.height)
if err != nil {
return err
}
c.result = p
return nil
}
func (c *pixelsCommand) NumVertices() int {
return 0
}
func (c *pixelsCommand) NumIndices() int {
return 0
}
func (c *pixelsCommand) AddNumVertices(n int) {
}
func (c *pixelsCommand) AddNumIndices(n int) {
}
func (c *pixelsCommand) CanMerge(dst, src *Image, color *affine.ColorM, mode opengl.CompositeMode, filter Filter) bool {
return false
}
// disposeCommand represents a command to dispose an image.
type disposeCommand struct {
target *Image

View File

@ -91,15 +91,14 @@ func (i *Image) DrawImage(src *Image, vertices []float32, indices []uint16, clr
}
func (i *Image) Pixels() ([]byte, error) {
// Flush the enqueued commands so that pixels are certainly read.
c := &pixelsCommand{
img: i,
}
theCommandQueue.Enqueue(c)
if err := theCommandQueue.Flush(); err != nil {
return nil, err
}
f, err := i.createFramebufferIfNeeded()
if err != nil {
return nil, err
}
return opengl.GetContext().FramebufferPixels(f.native, i.width, i.height)
return c.result, nil
}
func (i *Image) ReplacePixels(p []byte, x, y, width, height int) {