ui: introduce OpColor, a specialized OpImage for uniform colors

To avoid allocating an image.Image for OpImage.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2019-06-01 13:01:51 +02:00
parent 0061c73a89
commit 00a87f542d
3 changed files with 45 additions and 1 deletions
+13 -1
View File
@@ -5,6 +5,7 @@ package gpu
import ( import (
"fmt" "fmt"
"image" "image"
"image/color"
"math" "math"
"runtime" "runtime"
"strings" "strings"
@@ -70,6 +71,8 @@ type drawOps struct {
// Current OpImage image and rect, if any. // Current OpImage image and rect, if any.
img image.Image img image.Image
imgRect image.Rectangle imgRect image.Rectangle
// Current OpColor, if any.
color color.NRGBA
} }
type drawState struct { type drawState struct {
@@ -693,6 +696,11 @@ loop:
cpath.path = data cpath.path = data
d.pathOps = append(d.pathOps, cpath) d.pathOps = append(d.pathOps, cpath)
} }
case ops.TypeColor:
var op gdraw.OpColor
op.Decode(data, r.Refs)
d.img = nil
d.color = op.Col
case ops.TypeImage: case ops.TypeImage:
var op gdraw.OpImage var op gdraw.OpImage
op.Decode(data, r.Refs) op.Decode(data, r.Refs)
@@ -756,7 +764,11 @@ func expandPathOp(p *pathOp, clip image.Rectangle) {
func (d *drawOps) materialFor(cache *resourceCache, rect f32.Rectangle, off f32.Point, clip image.Rectangle) material { func (d *drawOps) materialFor(cache *resourceCache, rect f32.Rectangle, off f32.Point, clip image.Rectangle) material {
var m material var m material
if uniform, ok := d.img.(*image.Uniform); ok { if d.img == nil {
m.material = materialColor
m.color = gamma(d.color.RGBA())
m.opaque = m.color[3] == 1.0
} else if uniform, ok := d.img.(*image.Uniform); ok {
m.material = materialColor m.material = materialColor
m.color = gamma(uniform.RGBA()) m.color = gamma(uniform.RGBA())
m.opaque = m.color[3] == 1.0 m.opaque = m.color[3] == 1.0
+29
View File
@@ -5,6 +5,7 @@ package draw
import ( import (
"encoding/binary" "encoding/binary"
"image" "image"
"image/color"
"math" "math"
"gioui.org/ui" "gioui.org/ui"
@@ -18,6 +19,10 @@ type OpImage struct {
Rect image.Rectangle Rect image.Rectangle
} }
type OpColor struct {
Col color.NRGBA
}
type OpDraw struct { type OpDraw struct {
Rect f32.Rectangle Rect f32.Rectangle
} }
@@ -57,6 +62,30 @@ func (i *OpImage) Decode(data []byte, refs []interface{}) {
} }
} }
func (c OpColor) Add(o *ui.Ops) {
data := make([]byte, ops.TypeColorLen)
data[0] = byte(ops.TypeColor)
data[1] = c.Col.R
data[2] = c.Col.G
data[3] = c.Col.B
data[4] = c.Col.A
o.Write(data)
}
func (c *OpColor) Decode(data []byte, refs []interface{}) {
if ops.OpType(data[0]) != ops.TypeColor {
panic("invalid op")
}
*c = OpColor{
Col: color.NRGBA{
R: data[1],
G: data[2],
B: data[3],
A: data[4],
},
}
}
func (d OpDraw) Add(o *ui.Ops) { func (d OpDraw) Add(o *ui.Ops) {
data := make([]byte, ops.TypeDrawLen) data := make([]byte, ops.TypeDrawLen)
data[0] = byte(ops.TypeDraw) data[0] = byte(ops.TypeDraw)
+3
View File
@@ -29,6 +29,7 @@ const (
TypeClip TypeClip
TypeImage TypeImage
TypeDraw TypeDraw
TypeColor
TypePointerHandler TypePointerHandler
TypeKeyHandler TypeKeyHandler
TypeHideInput TypeHideInput
@@ -45,6 +46,7 @@ const (
TypeClipLen = 1 + 4 TypeClipLen = 1 + 4
TypeImageLen = 1 + 4 + 4*4 TypeImageLen = 1 + 4 + 4*4
TypeDrawLen = 1 + 4*4 TypeDrawLen = 1 + 4*4
TypeColorLen = 1 + 4
TypePointerHandlerLen = 1 + 4 + 4 + 1 TypePointerHandlerLen = 1 + 4 + 4 + 1
TypeKeyHandlerLen = 1 + 4 + 1 TypeKeyHandlerLen = 1 + 4 + 1
TypeHideInputLen = 1 TypeHideInputLen = 1
@@ -61,6 +63,7 @@ var typeLengths = [...]int{
TypeClipLen, TypeClipLen,
TypeImageLen, TypeImageLen,
TypeDrawLen, TypeDrawLen,
TypeColorLen,
TypePointerHandlerLen, TypePointerHandlerLen,
TypeKeyHandlerLen, TypeKeyHandlerLen,
TypeHideInputLen, TypeHideInputLen,