Reduce allocation of X buffers and canvases
This commit is contained in:
parent
b4a5bc7d03
commit
8f0f2be9e9
@ -195,19 +195,31 @@ func (window *Window) OnClose (callback func ()) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (window *Window) reallocateCanvas () {
|
func (window *Window) reallocateCanvas () {
|
||||||
window.canvas = tomo.NewBasicCanvas (
|
window.canvas.Reallocate(window.metrics.width, window.metrics.height)
|
||||||
window.metrics.width,
|
|
||||||
window.metrics.height)
|
previousWidth, previousHeight := 0, 0
|
||||||
if window.xCanvas != nil {
|
if window.xCanvas != nil {
|
||||||
window.xCanvas.Destroy()
|
previousWidth = window.xCanvas.Bounds().Dx()
|
||||||
|
previousHeight = window.xCanvas.Bounds().Dy()
|
||||||
}
|
}
|
||||||
window.xCanvas = xgraphics.New (
|
|
||||||
window.backend.connection,
|
newWidth := window.metrics.width
|
||||||
image.Rect (
|
newHeight := window.metrics.height
|
||||||
0, 0,
|
larger := newWidth > previousWidth || newHeight > previousHeight
|
||||||
window.metrics.width,
|
smaller := newWidth < previousWidth / 2 || newHeight < previousHeight / 2
|
||||||
window.metrics.height))
|
if larger || smaller {
|
||||||
window.xCanvas.CreatePixmap()
|
if window.xCanvas != nil {
|
||||||
|
window.xCanvas.Destroy()
|
||||||
|
}
|
||||||
|
window.xCanvas = xgraphics.New (
|
||||||
|
window.backend.connection,
|
||||||
|
image.Rect (
|
||||||
|
0, 0,
|
||||||
|
(newWidth / 64) * 64 + 64,
|
||||||
|
(newHeight / 64) * 64 + 64))
|
||||||
|
window.xCanvas.CreatePixmap()
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (window *Window) redrawChildEntirely () {
|
func (window *Window) redrawChildEntirely () {
|
||||||
|
16
canvas.go
16
canvas.go
@ -61,6 +61,22 @@ func (canvas BasicCanvas) Buffer () (data []color.RGBA, stride int) {
|
|||||||
return canvas.pix, canvas.stride
|
return canvas.pix, canvas.stride
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Reallocate efficiently reallocates the canvas. The data within will be
|
||||||
|
// garbage. This method will do nothing if this is a cut image.
|
||||||
|
func (canvas *BasicCanvas) Reallocate (width, height int) {
|
||||||
|
previousLen := len(canvas.pix)
|
||||||
|
newLen := width * height
|
||||||
|
bigger := newLen > previousLen
|
||||||
|
smaller := newLen < previousLen / 2
|
||||||
|
if bigger || smaller {
|
||||||
|
canvas.pix = make (
|
||||||
|
[]color.RGBA,
|
||||||
|
((height * width) / 4096) * 4096 + 4096)
|
||||||
|
}
|
||||||
|
canvas.stride = width
|
||||||
|
canvas.rect = image.Rect(0, 0, width, height)
|
||||||
|
}
|
||||||
|
|
||||||
// Cut returns a sub-canvas of a given canvas.
|
// Cut returns a sub-canvas of a given canvas.
|
||||||
func Cut (canvas Canvas, bounds image.Rectangle) (reduced BasicCanvas) {
|
func Cut (canvas Canvas, bounds image.Rectangle) (reduced BasicCanvas) {
|
||||||
// println(canvas.Bounds().String(), bounds.String())
|
// println(canvas.Bounds().String(), bounds.String())
|
||||||
|
Reference in New Issue
Block a user