Skip to content

Commit

Permalink
custom texture binding for Reflective to avoid double free, and race …
Browse files Browse the repository at this point in the history
…condition fix in appending draw commands for paint example
  • Loading branch information
cjbrigato committed Oct 4, 2024
1 parent 2fc7d72 commit 9b89462
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
13 changes: 10 additions & 3 deletions ReflectiveBoundTexture.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"image/color"
"sync"

"github.com/AllenDang/cimgui-go/backend"
"github.com/AllenDang/cimgui-go/imgui"
)

Expand Down Expand Up @@ -193,10 +194,16 @@ func (i *ReflectiveBoundTexture) unbind() {
}

// bind creates a new texture from the RGBA surface and assigns it to the ReflectiveBoundTexture.
// note it bypasses normal texture management up to cimgui-go to avoid double free from finalizers.
func (i *ReflectiveBoundTexture) bind() {
NewTextureFromRgba(i.Surface, func(tex *Texture) {
i.tex = tex
})
img := ImageToRgba(i.Surface)
i.tex = &Texture{
&backend.Texture{
ID: backend.TextureManager(Context.backend).CreateTextureRgba(img, img.Bounds().Dx(), img.Bounds().Dy()),
Width: img.Bounds().Dx(),
Height: img.Bounds().Dy(),
},
}
}

// GetSurfaceWidth returns the width of the RGBA surface.
Expand Down
15 changes: 12 additions & 3 deletions examples/paint/canvas.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"image"
"image/color"
"sync"

"github.com/AllenDang/cimgui-go/imgui"

Expand Down Expand Up @@ -57,11 +58,13 @@ type Canvas struct {

// inited is a boolean flag indicating whether the canvas has been initialized.
inited bool

appendMu sync.Mutex
}

// GetDrawCommands returns a slice of drawCommand starting from the specified index.
// getDrawCommands returns a slice of drawCommand starting from the specified index.
// It allows retrieval of draw commands that have been added since a given point in time.
func (c *Canvas) GetDrawCommands(sinceIndex int) []DrawCommand {
func (c *Canvas) getDrawCommands(sinceIndex int) []DrawCommand {
return c.DrawCommands[sinceIndex:]
}

Expand All @@ -79,6 +82,8 @@ func (c *Canvas) PushImageToBackend(commit bool) error {
// AppendDrawCommands adds a slice of drawCommand to the canvas's existing draw commands.
// It appends the provided commands to the DrawCommands slice.
func (c *Canvas) AppendDrawCommands(cmds *[]DrawCommand) {
c.appendMu.Lock()
defer c.appendMu.Unlock()
c.DrawCommands = append(c.DrawCommands, *cmds...)
}

Expand All @@ -104,6 +109,8 @@ func (c *Canvas) Compute() {
return
}

c.appendMu.Lock()

// Return if there are no draw commands to process
if len(c.DrawCommands) < 1 {
return
Expand All @@ -115,7 +122,9 @@ func (c *Canvas) Compute() {
}

// Get the new draw commands that need to be processed
draws := c.GetDrawCommands(c.LastComputedLen)
draws := c.getDrawCommands(c.LastComputedLen)
c.appendMu.Unlock()

for _, r := range draws {
switch r.Tool {
case 0:
Expand Down

0 comments on commit 9b89462

Please sign in to comment.