From 32fb2e97ab0a48fa1be5b32841f339da1350a154 Mon Sep 17 00:00:00 2001 From: Laurent Le Goff Date: Fri, 22 Apr 2011 18:08:10 +0200 Subject: [PATCH] optim --- draw2d/path_storage.go | 28 +++++++++++++++++++-- draw2d/stack_gc.go | 4 +-- draw2d/stroker.go | 8 +++--- draw2dgl/gc.go | 55 +++++++++++++++++++++++++++++++++--------- 4 files changed, 76 insertions(+), 19 deletions(-) diff --git a/draw2d/path_storage.go b/draw2d/path_storage.go index fff521a..8cd2547 100644 --- a/draw2d/path_storage.go +++ b/draw2d/path_storage.go @@ -24,9 +24,32 @@ type PathStorage struct { x, y float64 } +func NewPathStorage() (p *PathStorage) { + p = new(PathStorage) + p.commands = make([]PathCmd, 0, 256) + p.vertices = make([]float64, 0, 256) + return +} + +func (p *PathStorage) Clear() { + p.commands = p.commands[0:0] + p.vertices = p.vertices[0:0] + return +} + func (p *PathStorage) appendToPath(cmd PathCmd, vertices ...float64) { - p.commands = append(p.commands, cmd) - p.vertices = append(p.vertices, vertices...) + if cap(p.vertices) <= len(p.vertices) + 6 { + a := make([]PathCmd, len(p.commands), cap(p.commands) + 256) + b := make([]float64, len(p.vertices), cap(p.vertices) + 256) + copy(a, p.commands) + p.commands = a + copy(b, p.vertices) + p.vertices = b + } + p.commands = p.commands[0:len(p.commands)+1] + p.commands[len(p.commands) - 1]= cmd + copy(p.vertices[len(p.vertices):len(p.vertices)+len(vertices)], vertices) + p.vertices = p.vertices[0:len(p.vertices)+ len(vertices)] } func (src *PathStorage) Copy() (dest *PathStorage) { @@ -37,6 +60,7 @@ func (src *PathStorage) Copy() (dest *PathStorage) { copy(dest.vertices, src.vertices) return dest } + func (p *PathStorage) LastPoint() (x, y float64) { return p.x, p.y } diff --git a/draw2d/stack_gc.go b/draw2d/stack_gc.go index b1e09e5..94d275f 100644 --- a/draw2d/stack_gc.go +++ b/draw2d/stack_gc.go @@ -34,7 +34,7 @@ func NewStackGraphicContext() *StackGraphicContext { gc := &StackGraphicContext{} gc.Current = new(ContextStack) gc.Current.Tr = NewIdentityMatrix() - gc.Current.Path = new(PathStorage) + gc.Current.Path = NewPathStorage() gc.Current.LineWidth = 1.0 gc.Current.StrokeColor = image.Black gc.Current.FillColor = image.White @@ -117,7 +117,7 @@ func (gc *StackGraphicContext) GetFontData() FontData { } func (gc *StackGraphicContext) BeginPath() { - gc.Current.Path = new(PathStorage) + gc.Current.Path.Clear() } func (gc *StackGraphicContext) IsEmpty() bool { diff --git a/draw2d/stroker.go b/draw2d/stroker.go index 983dde3..8f4fb2c 100644 --- a/draw2d/stroker.go +++ b/draw2d/stroker.go @@ -34,8 +34,8 @@ func NewLineStroker(c Cap, j Join, converter VertexConverter) *LineStroker { l := new(LineStroker) l.Next = converter l.HalfLineWidth = 0.5 - l.vertices = make([]float64, 0) - l.rewind = make([]float64, 0) + l.vertices = make([]float64, 0, 256) + l.rewind = make([]float64, 0, 256) l.Cap = c l.Join = j l.command = VertexNoCommand @@ -61,8 +61,8 @@ func (l *LineStroker) NextCommand(command VertexCommand) { } l.Next.NextCommand(VertexStopCommand) // reinit vertices - l.vertices = make([]float64, 0) - l.rewind = make([]float64, 0) + l.vertices = l.vertices[0:0] + l.rewind = l.rewind[0:0] l.x, l.y, l.nx, l.ny = 0, 0, 0, 0 } } diff --git a/draw2dgl/gc.go b/draw2dgl/gc.go index 1cbb978..c17559d 100644 --- a/draw2dgl/gc.go +++ b/draw2dgl/gc.go @@ -24,16 +24,49 @@ const M16 uint32 = 1<<16 - 1 // Paint satisfies the Painter interface by painting ss onto an image.RGBA. func (p *GLPainter) Paint(ss []raster.Span, done bool) { //gl.Begin(gl.LINES) + sslen := len(ss) + clenrequired := sslen * 8 + vlenrequired := sslen * 4 + if clenrequired >= (cap(p.colors) - len(p.colors)) { + p.Flush() + + if clenrequired >= cap(p.colors) { + p.vertices = make([]int32, 0, vlenrequired + (vlenrequired / 2 )) + p.colors = make([]uint8, 0, clenrequired + (clenrequired /2)) + } + } + vi := len(p.vertices) + ci := len(p.colors) + p.vertices = p.vertices[0:vi+vlenrequired] + p.colors = p.colors[0:ci+clenrequired] for _, s := range ss { ma := s.A >> 16 a := ma * p.ca / M16 - /*gl.Color4ub(p.cr, p.cg, p.cb, uint8(a>>8)) - gl.Vertex2i(s.X0, s.Y) - gl.Vertex2i(s.X1, s.Y)*/ - p.colors = append(p.colors, p.cr, p.cg, p.cb, uint8(a>>8), p.cr, p.cg, p.cb, uint8(a>>8)) - p.vertices = append(p.vertices, int32(s.X0), int32(s.Y), int32(s.X1), int32(s.Y)) + p.colors[ci] = p.cr + ci++ + p.colors[ci] = p.cg + ci++ + p.colors[ci] = p.cb + ci++ + p.colors[ci] = uint8(a>>8) + ci++ + p.colors[ci] = p.cr + ci++ + p.colors[ci] = p.cg + ci++ + p.colors[ci] = p.cb + ci++ + p.colors[ci] = uint8(a>>8) + ci++ + p.vertices[vi] = int32(s.X0) + vi++ + p.vertices[vi] = int32(s.Y) + vi++ + p.vertices[vi] = int32(s.X1) + vi++ + p.vertices[vi] = int32(s.Y) + vi++ } - //gl.End() } func (p *GLPainter) Flush() { @@ -47,8 +80,8 @@ func (p *GLPainter) Flush() { gl.DrawArrays(gl.LINES, 0, len(p.vertices)/2) gl.DisableClientState(gl.VERTEX_ARRAY) gl.DisableClientState(gl.COLOR_ARRAY) - p.vertices = make([]int32, 0, 1024) - p.colors = make([]uint8, 0, 1024) + p.vertices = p.vertices[0:0] + p.colors = p.colors[0:0] } } @@ -147,7 +180,7 @@ func (gc *GraphicContext) Stroke(paths ...*draw2d.PathStorage) { pathConverter.Convert(paths...) gc.paint(gc.strokeRasterizer, gc.Current.StrokeColor) - gc.Current.Path = new(draw2d.PathStorage) + gc.Current.Path.Clear() } func (gc *GraphicContext) Fill(paths ...*draw2d.PathStorage) { @@ -159,7 +192,7 @@ func (gc *GraphicContext) Fill(paths ...*draw2d.PathStorage) { pathConverter.Convert(paths...) gc.paint(gc.fillRasterizer, gc.Current.FillColor) - gc.Current.Path = new(draw2d.PathStorage) + gc.Current.Path.Clear() } func (gc *GraphicContext) FillStroke(paths ...*draw2d.PathStorage) { @@ -179,5 +212,5 @@ func (gc *GraphicContext) FillStroke(paths ...*draw2d.PathStorage) { gc.paint(gc.fillRasterizer, gc.Current.FillColor) gc.paint(gc.strokeRasterizer, gc.Current.StrokeColor) - gc.Current.Path = new(draw2d.PathStorage) + gc.Current.Path = draw2d.NewPathStorage() }