Skip to content

Commit

Permalink
Paint frames in parallel (#67)
Browse files Browse the repository at this point in the history
Paint all frames in parallel goroutines. Painting is the slowest
operation we do, but fortunately we can do every frame at the same time.
  • Loading branch information
rohansingh authored Sep 2, 2021
1 parent 0aef544 commit 163b9a6
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions render/root.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package render

import (
"github.com/fogleman/gg"
"image"
"image/color"
"sync"

"github.com/fogleman/gg"
)

const (
Expand Down Expand Up @@ -33,16 +35,24 @@ func (r Root) Paint(solidBackground bool) []image.Image {
numFrames := r.Child.FrameCount()
frames := make([]image.Image, numFrames)

var wg sync.WaitGroup
for i := 0; i < numFrames; i++ {
dc := gg.NewContext(DefaultFrameWidth, DefaultFrameHeight)
if solidBackground {
dc.SetColor(color.Black)
dc.Clear()
}
im := r.Child.Paint(image.Rect(0, 0, DefaultFrameWidth, DefaultFrameHeight), i)
dc.DrawImage(im, 0, 0)
frames[i] = dc.Image()
wg.Add(1)
go func(i int) {
dc := gg.NewContext(DefaultFrameWidth, DefaultFrameHeight)
if solidBackground {
dc.SetColor(color.Black)
dc.Clear()
}

im := r.Child.Paint(image.Rect(0, 0, DefaultFrameWidth, DefaultFrameHeight), i)
dc.DrawImage(im, 0, 0)
frames[i] = dc.Image()
wg.Done()
}(i)
}

wg.Wait()
return frames
}

Expand Down

0 comments on commit 163b9a6

Please sign in to comment.