canvas bounds update fix, gomobile example works again

This commit is contained in:
Thomas Friedel 2019-02-27 15:46:08 +01:00
parent 9c3cccabdd
commit ba8238ba66
3 changed files with 119 additions and 95 deletions

View file

@ -236,14 +236,14 @@ func NewOffscreen(w, h int, alpha bool) (*GoGLBackendOffscreen, error) {
}
bo := &GoGLBackendOffscreen{}
bo.offscrBuf.alpha = alpha
bo.offscrImg.flip = true
b.activateFn = func() {
b.enableTextureRenderTarget(&bo.offscrBuf)
gl.Viewport(0, 0, int32(b.w), int32(b.h))
gl.Viewport(0, 0, int32(bo.GoGLBackend.w), int32(bo.GoGLBackend.h))
bo.offscrImg.w = bo.offscrBuf.w
bo.offscrImg.h = bo.offscrBuf.h
bo.offscrImg.tex = bo.offscrBuf.tex
bo.offscrImg.flip = true
}
b.disableTextureRenderTarget = func() {
b.enableTextureRenderTarget(&bo.offscrBuf)
@ -270,6 +270,9 @@ func (b *GoGLBackend) SetBounds(x, y, w, h int) {
// SetBounds updates the size of the offscreen texture
func (b *GoGLBackendOffscreen) SetBounds(w, h int) {
b.GoGLBackend.SetBounds(0, 0, w, h)
b.enableTextureRenderTarget(&b.offscrBuf)
b.offscrImg.w = b.offscrBuf.w
b.offscrImg.h = b.offscrBuf.h
}
func (b *GoGLBackend) Size() (int, int) {
@ -452,7 +455,15 @@ func (b *GoGLBackend) useAlphaShader(style *backendbase.FillStyle, alphaTexSlot
}
func (b *GoGLBackend) enableTextureRenderTarget(offscr *offscreenBuffer) {
if offscr.w != b.w || offscr.h != b.h {
if offscr.w == b.w && offscr.h == b.h {
gl.BindFramebuffer(gl.FRAMEBUFFER, offscr.frameBuf)
return
}
if b.w == 0 || b.h == 0 {
return
}
if offscr.w != 0 && offscr.h != 0 {
gl.DeleteTextures(1, &offscr.tex)
gl.DeleteFramebuffers(1, &offscr.frameBuf)
@ -491,9 +502,6 @@ func (b *GoGLBackend) enableTextureRenderTarget(offscr *offscreenBuffer) {
}
gl.Clear(gl.COLOR_BUFFER_BIT | gl.STENCIL_BUFFER_BIT)
} else {
gl.BindFramebuffer(gl.FRAMEBUFFER, offscr.frameBuf)
}
}
type vec [2]float64

View file

@ -239,14 +239,14 @@ func NewOffscreen(glctx gl.Context, w, h int, alpha bool) (*XMobileBackendOffscr
}
bo := &XMobileBackendOffscreen{}
bo.offscrBuf.alpha = alpha
bo.offscrImg.flip = true
b.activateFn = func() {
b.enableTextureRenderTarget(&bo.offscrBuf)
b.glctx.Viewport(0, 0, b.w, b.h)
b.glctx.Viewport(0, 0, bo.XMobileBackend.w, bo.XMobileBackend.h)
bo.offscrImg.w = bo.offscrBuf.w
bo.offscrImg.h = bo.offscrBuf.h
bo.offscrImg.tex = bo.offscrBuf.tex
bo.offscrImg.flip = true
}
b.disableTextureRenderTarget = func() {
b.enableTextureRenderTarget(&bo.offscrBuf)
@ -273,6 +273,9 @@ func (b *XMobileBackend) SetBounds(x, y, w, h int) {
// SetBounds updates the size of the offscreen texture
func (b *XMobileBackendOffscreen) SetBounds(w, h int) {
b.XMobileBackend.SetBounds(0, 0, w, h)
b.enableTextureRenderTarget(&b.offscrBuf)
b.offscrImg.w = b.offscrBuf.w
b.offscrImg.h = b.offscrBuf.h
}
func (b *XMobileBackend) Size() (int, int) {
@ -455,7 +458,15 @@ func (b *XMobileBackend) useAlphaShader(style *backendbase.FillStyle, alphaTexSl
}
func (b *XMobileBackend) enableTextureRenderTarget(offscr *offscreenBuffer) {
if offscr.w != b.w || offscr.h != b.h {
if offscr.w == b.w && offscr.h == b.h {
b.glctx.BindFramebuffer(gl.FRAMEBUFFER, offscr.frameBuf)
return
}
if b.w == 0 || b.h == 0 {
return
}
if offscr.w != 0 && offscr.h != 0 {
b.glctx.DeleteTexture(offscr.tex)
b.glctx.DeleteFramebuffer(offscr.frameBuf)
@ -494,9 +505,6 @@ func (b *XMobileBackend) enableTextureRenderTarget(offscr *offscreenBuffer) {
}
b.glctx.Clear(gl.COLOR_BUFFER_BIT | gl.STENCIL_BUFFER_BIT)
} else {
b.glctx.BindFramebuffer(gl.FRAMEBUFFER, offscr.frameBuf)
}
}
type vec [2]float64

View file

@ -1,11 +1,12 @@
package main
import (
"log"
"math"
"time"
"github.com/tfriedel6/canvas"
"github.com/tfriedel6/canvas/glimpl/xmobile"
"github.com/tfriedel6/canvas/backend/xmobile"
"golang.org/x/mobile/app"
"golang.org/x/mobile/event/lifecycle"
"golang.org/x/mobile/event/paint"
@ -16,6 +17,8 @@ import (
func main() {
app.Main(func(a app.App) {
var cv, painter *canvas.Canvas
var cvb *xmobilebackend.XMobileBackendOffscreen
var painterb *xmobilebackend.XMobileBackend
var w, h int
var glctx gl.Context
@ -24,30 +27,35 @@ func main() {
case lifecycle.Event:
switch e.Crosses(lifecycle.StageVisible) {
case lifecycle.CrossOn:
glctx, _ = e.DrawContext.(gl.Context)
canvas.LoadGL(glimplxmobile.New(glctx))
cv = canvas.NewOffscreen(0, 0)
painter = canvas.New(0, 0, 0, 0)
var err error
glctx = e.DrawContext.(gl.Context)
cvb, err = xmobilebackend.NewOffscreen(glctx, 0, 0, false)
if err != nil {
log.Fatalln(err)
}
painterb, err = xmobilebackend.New(glctx, 0, 0, 0, 0)
if err != nil {
log.Fatalln(err)
}
cv = canvas.New(cvb)
painter = canvas.New(painterb)
a.Send(paint.Event{})
case lifecycle.CrossOff:
cvb.Delete()
glctx = nil
}
case size.Event:
w, h = e.WidthPx, e.HeightPx
case paint.Event:
if glctx != nil {
glctx.ClearColor(0, 0, 0, 0)
glctx.Clear(gl.COLOR_BUFFER_BIT)
cv.SetBounds(0, 0, w, h)
painter.SetBounds(0, 0, w, h)
fw, fh := float64(w), float64(h)
color := math.Sin(float64(time.Now().UnixNano())*0.000000002)*0.3 + 0.7
cvb.SetBounds(w, h)
cv.SetFillStyle(color*0.2, color*0.2, color*0.8)
cv.FillRect(fw*0.25, fh*0.25, fw*0.5, fh*0.5)
painterb.SetBounds(0, 0, w, h)
painter.DrawImage(cv)
a.Publish()