From f47d24543d8851164229640d31143b5c0b286d8f Mon Sep 17 00:00:00 2001 From: Thomas Friedel Date: Sat, 21 Mar 2020 13:03:56 +0100 Subject: [PATCH] removed explicit deleting of gradients removed finalizers from backends, instead using a finalizer as an emergency catch on the frontend --- backend/goglbackend/gogl.go | 15 ----------- backend/goglbackend/gradients.go | 12 --------- backend/xmobilebackend/gradients.go | 12 --------- backend/xmobilebackend/xmobile.go | 15 ----------- gradients.go | 39 ++++++++++------------------- 5 files changed, 13 insertions(+), 80 deletions(-) diff --git a/backend/goglbackend/gogl.go b/backend/goglbackend/gogl.go index 2d104f6..7b63dcc 100644 --- a/backend/goglbackend/gogl.go +++ b/backend/goglbackend/gogl.go @@ -28,8 +28,6 @@ type GLContext struct { imageBuf []byte ptsBuf []float32 - - glChan chan func() } // NewGLContext creates all the necessary GL resources, @@ -37,7 +35,6 @@ type GLContext struct { func NewGLContext() (*GLContext, error) { ctx := &GLContext{ ptsBuf: make([]float32, 0, 4096), - glChan: make(chan func()), } err := gl.Init() @@ -231,18 +228,6 @@ func (b *GoGLBackend) activate() { activeContext = b b.activateFn() } - b.runGLQueue() -} - -func (b *GoGLBackend) runGLQueue() { - for { - select { - case f := <-b.glChan: - f() - default: - return - } - } } // Delete deletes the offscreen texture. After calling this diff --git a/backend/goglbackend/gradients.go b/backend/goglbackend/gradients.go index a9927ac..bc94978 100644 --- a/backend/goglbackend/gradients.go +++ b/backend/goglbackend/gradients.go @@ -1,8 +1,6 @@ package goglbackend import ( - "runtime" - "github.com/tfriedel6/canvas/backend/backendbase" "github.com/tfriedel6/canvas/backend/goglbackend/gl" ) @@ -42,11 +40,6 @@ func (b *GoGLBackend) LoadLinearGradient(data backendbase.Gradient) backendbase. gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE) lg.load(data) - runtime.SetFinalizer(lg, func(lg *LinearGradient) { - b.glChan <- func() { - gl.DeleteTextures(1, &lg.tex) - } - }) return lg } @@ -64,11 +57,6 @@ func (b *GoGLBackend) LoadRadialGradient(data backendbase.Gradient) backendbase. gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE) rg.load(data) - runtime.SetFinalizer(rg, func(rg *RadialGradient) { - b.glChan <- func() { - gl.DeleteTextures(1, &rg.tex) - } - }) return rg } diff --git a/backend/xmobilebackend/gradients.go b/backend/xmobilebackend/gradients.go index 9367dca..a3a51ea 100755 --- a/backend/xmobilebackend/gradients.go +++ b/backend/xmobilebackend/gradients.go @@ -1,8 +1,6 @@ package xmobilebackend import ( - "runtime" - "github.com/tfriedel6/canvas/backend/backendbase" "golang.org/x/mobile/gl" ) @@ -42,11 +40,6 @@ func (b *XMobileBackend) LoadLinearGradient(data backendbase.Gradient) backendba b.glctx.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE) b.glctx.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE) lg.load(data) - runtime.SetFinalizer(lg, func(lg *LinearGradient) { - b.glChan <- func() { - b.glctx.DeleteTexture(lg.tex) - } - }) return lg } @@ -64,11 +57,6 @@ func (b *XMobileBackend) LoadRadialGradient(data backendbase.Gradient) backendba b.glctx.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE) b.glctx.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE) rg.load(data) - runtime.SetFinalizer(rg, func(rg *RadialGradient) { - b.glChan <- func() { - b.glctx.DeleteTexture(rg.tex) - } - }) return rg } diff --git a/backend/xmobilebackend/xmobile.go b/backend/xmobilebackend/xmobile.go index 9290cfc..3c6fff8 100755 --- a/backend/xmobilebackend/xmobile.go +++ b/backend/xmobilebackend/xmobile.go @@ -32,8 +32,6 @@ type GLContext struct { imageBuf []byte ptsBuf []float32 - - glChan chan func() } // NewGLContext creates all the necessary GL resources, @@ -43,7 +41,6 @@ func NewGLContext(glctx gl.Context) (*GLContext, error) { glctx: glctx, ptsBuf: make([]float32, 0, 4096), - glChan: make(chan func()), } var err error @@ -224,18 +221,6 @@ func (b *XMobileBackend) activate() { activeContext = b b.activateFn() } - b.runGLQueue() -} - -func (b *XMobileBackend) runGLQueue() { - for { - select { - case f := <-b.glChan: - f() - default: - return - } - } } // Delete deletes the offscreen texture. After calling this diff --git a/gradients.go b/gradients.go index ac8b088..50d2ebb 100644 --- a/gradients.go +++ b/gradients.go @@ -2,6 +2,7 @@ package canvas import ( "image/color" + "runtime" "github.com/tfriedel6/canvas/backend/backendbase" ) @@ -15,7 +16,6 @@ type LinearGradient struct { from, to vec created bool loaded bool - deleted bool opaque bool grad backendbase.LinearGradient data backendbase.Gradient @@ -32,7 +32,6 @@ type RadialGradient struct { radTo float64 created bool loaded bool - deleted bool opaque bool grad backendbase.RadialGradient data backendbase.Gradient @@ -42,13 +41,17 @@ type RadialGradient struct { // the coordinates from where to where the gradient // will apply on the canvas func (cv *Canvas) CreateLinearGradient(x0, y0, x1, y1 float64) *LinearGradient { - return &LinearGradient{ + lg := &LinearGradient{ cv: cv, opaque: true, from: vec{x0, y0}, to: vec{x1, y1}, data: make(backendbase.Gradient, 0, 20), } + runtime.SetFinalizer(lg, func(*LinearGradient) { + lg.grad.Delete() + }) + return lg } // CreateRadialGradient creates a new radial gradient with @@ -56,7 +59,7 @@ func (cv *Canvas) CreateLinearGradient(x0, y0, x1, y1 float64) *LinearGradient { // gradient will apply from the first to the second // circle func (cv *Canvas) CreateRadialGradient(x0, y0, r0, x1, y1, r1 float64) *RadialGradient { - return &RadialGradient{ + rg := &RadialGradient{ cv: cv, opaque: true, from: vec{x0, y0}, @@ -65,30 +68,14 @@ func (cv *Canvas) CreateRadialGradient(x0, y0, r0, x1, y1, r1 float64) *RadialGr radTo: r1, data: make(backendbase.Gradient, 0, 20), } -} - -// Delete explicitly deletes the gradient -func (lg *LinearGradient) Delete() { - if lg.deleted { - return - } - lg.grad.Delete() - lg.grad = nil - lg.deleted = true -} - -// Delete explicitly deletes the gradient -func (rg *RadialGradient) Delete() { - if rg.deleted { - return - } - rg.grad.Delete() - rg.grad = nil - rg.deleted = true + runtime.SetFinalizer(rg, func(*RadialGradient) { + rg.grad.Delete() + }) + return rg } func (lg *LinearGradient) load() { - if lg.loaded || len(lg.data) < 1 || lg.deleted { + if lg.loaded || len(lg.data) < 1 { return } @@ -102,7 +89,7 @@ func (lg *LinearGradient) load() { } func (rg *RadialGradient) load() { - if rg.loaded || len(rg.data) < 1 || rg.deleted { + if rg.loaded || len(rg.data) < 1 { return }