graphics: Avoid creating struct as much as possible

This commit is contained in:
Hajime Hoshi 2017-05-02 22:45:09 +09:00
parent aa8e77e0bf
commit 5699822d64
2 changed files with 26 additions and 24 deletions

View File

@ -42,9 +42,7 @@ var theCommandQueue = &commandQueue{
vertices: []float32{}, vertices: []float32{},
} }
func (q *commandQueue) AppendVertices(vertices []float32) { func (q *commandQueue) appendVertices(vertices []float32) {
q.m.Lock()
defer q.m.Unlock()
if len(q.vertices) < q.verticesNum+len(vertices) { if len(q.vertices) < q.verticesNum+len(vertices) {
n := q.verticesNum + len(vertices) - len(q.vertices) n := q.verticesNum + len(vertices) - len(q.vertices)
q.vertices = append(q.vertices, make([]float32, n)...) q.vertices = append(q.vertices, make([]float32, n)...)
@ -53,19 +51,31 @@ func (q *commandQueue) AppendVertices(vertices []float32) {
q.verticesNum += len(vertices) q.verticesNum += len(vertices)
} }
func (q *commandQueue) Enqueue(command command) { func (q *commandQueue) EnqueueDrawImageCommand(dst, src *Image, vertices []float32, clr affine.ColorM, mode opengl.CompositeMode) {
q.m.Lock() q.m.Lock()
defer q.m.Unlock() defer q.m.Unlock()
q.appendVertices(vertices)
if 0 < len(q.commands) { if 0 < len(q.commands) {
if c1, ok := q.commands[len(q.commands)-1].(*drawImageCommand); ok { if c, ok := q.commands[len(q.commands)-1].(*drawImageCommand); ok {
if c2, ok := command.(*drawImageCommand); ok { if c.isMergeable(dst, src, clr, mode) {
if c1.isMergeable(c2) { c.verticesNum += len(vertices)
c1.verticesNum += c2.verticesNum
return return
} }
} }
} }
c := &drawImageCommand{
dst: dst,
src: src,
verticesNum: len(vertices),
color: clr,
mode: mode,
} }
q.commands = append(q.commands, c)
}
func (q *commandQueue) Enqueue(command command) {
q.m.Lock()
defer q.m.Unlock()
q.commands = append(q.commands, command) q.commands = append(q.commands, command)
} }
@ -225,17 +235,17 @@ func (c *drawImageCommand) split(quadsNum int) [2]*drawImageCommand {
return [2]*drawImageCommand{&c1, &c2} return [2]*drawImageCommand{&c1, &c2}
} }
func (c *drawImageCommand) isMergeable(other *drawImageCommand) bool { func (c *drawImageCommand) isMergeable(dst, src *Image, clr affine.ColorM, mode opengl.CompositeMode) bool {
if c.dst != other.dst { if c.dst != dst {
return false return false
} }
if c.src != other.src { if c.src != src {
return false return false
} }
if !c.color.Equals(&other.color) { if !c.color.Equals(&clr) {
return false return false
} }
if c.mode != other.mode { if c.mode != mode {
return false return false
} }
return true return true

View File

@ -142,15 +142,7 @@ func (i *Image) Fill(clr color.RGBA) {
} }
func (i *Image) DrawImage(src *Image, vertices []float32, clr affine.ColorM, mode opengl.CompositeMode) { func (i *Image) DrawImage(src *Image, vertices []float32, clr affine.ColorM, mode opengl.CompositeMode) {
c := &drawImageCommand{ theCommandQueue.EnqueueDrawImageCommand(i, src, vertices, clr, mode)
dst: i,
src: src,
verticesNum: len(vertices),
color: clr,
mode: mode,
}
theCommandQueue.AppendVertices(vertices)
theCommandQueue.Enqueue(c)
} }
func (i *Image) Pixels(context *opengl.Context) ([]uint8, error) { func (i *Image) Pixels(context *opengl.Context) ([]uint8, error) {