op/paint: make every ImageOp unique

The gioui.org/commit/74407a50d598bfd27e8f8e48b6832cc5df04de77
added a NewImageOp constructor that always copies the supplied
image. It does that for two reasons:

First, the image.Image reference is used in the image=>texture
map of cached textures. Without a copy, we wouldn't detect a
modified image even if a new ImageOp was created.

Second, we don't want the program to touch the image while the GPU
is uploading it.

The second reason was removed in a previous change that blocks
FrameEvent.Frame until we're done with the operations, including
uploading images to the GPU.

The first reason is easily fixed by using a unique per ImageOp,
as pointed out by Alessandro Arzilli.

This change switches to using the unique key. Alessandro's patch
avoids the copy when possible.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2019-11-07 18:27:06 +01:00
parent d980c4652a
commit 97299dc2f9
3 changed files with 52 additions and 37 deletions
+8 -6
View File
@@ -20,6 +20,10 @@ type ImageOp struct {
color color.RGBA
src *image.RGBA
size image.Point
// handle is a key to uniquely identify this ImageOp
// in a map of cached textures.
handle interface{}
}
// ColorOp sets the material to a constant color.
@@ -49,8 +53,9 @@ func NewImageOp(src image.Image) ImageOp {
})
draw.Draw(dst, src.Bounds(), src, image.Point{}, draw.Src)
return ImageOp{
src: dst,
size: sz,
src: dst,
size: sz,
handle: new(int),
}
}
}
@@ -66,11 +71,8 @@ func (i ImageOp) Add(o *op.Ops) {
}.Add(o)
return
}
data := o.Write(opconst.TypeImageLen, i.src)
data := o.Write(opconst.TypeImageLen, i.src, i.handle)
data[0] = byte(opconst.TypeImage)
bo := binary.LittleEndian
bo.PutUint32(data[1:], uint32(i.size.X))
bo.PutUint32(data[5:], uint32(i.size.Y))
}
func (c ColorOp) Add(o *op.Ops) {