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 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. // disposeCommand represents a command to dispose an image.
type disposeCommand struct { type disposeCommand struct {
target *Image 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) { 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 { if err := theCommandQueue.Flush(); err != nil {
return nil, err return nil, err
} }
f, err := i.createFramebufferIfNeeded() return c.result, nil
if err != nil {
return nil, err
}
return opengl.GetContext().FramebufferPixels(f.native, i.width, i.height)
} }
func (i *Image) ReplacePixels(p []byte, x, y, width, height int) { func (i *Image) ReplacePixels(p []byte, x, y, width, height int) {