all: clean up code, upgrade to modern Go

Signed-off-by: ddkwork
This commit is contained in:
Admin
2025-05-05 23:27:04 +08:00
committed by Elias Naur
parent 86668e8b45
commit f73287be87
58 changed files with 413 additions and 241 deletions
+1 -1
View File
@@ -10,7 +10,7 @@ import (
func BenchmarkEncodeQuadTo(b *testing.B) {
var data [vertStride * 4]byte
for i := 0; i < b.N; i++ {
for i := 0; b.Loop(); i++ {
v := float32(i)
encodeQuadTo(data[:], 123,
f32.Point{X: v, Y: v},
+7 -6
View File
@@ -15,6 +15,7 @@ import (
"image/color"
"math"
"reflect"
"slices"
"time"
"unsafe"
@@ -189,7 +190,7 @@ const (
// imageOpData is the shadow of paint.ImageOp.
type imageOpData struct {
src *image.RGBA
handle interface{}
handle any
filter byte
}
@@ -200,7 +201,7 @@ type linearGradientOpData struct {
color2 color.NRGBA
}
func decodeImageOp(data []byte, refs []interface{}) imageOpData {
func decodeImageOp(data []byte, refs []any) imageOpData {
handle := refs[1]
if handle == nil {
return imageOpData{}
@@ -547,7 +548,7 @@ func newBlitter(ctx driver.Device) *blitter {
b.texUniforms = new(blitTexUniforms)
b.linearGradientUniforms = new(blitLinearGradientUniforms)
pipelines, err := createColorPrograms(ctx, gio.Shader_blit_vert, gio.Shader_blit_frag,
[3]interface{}{b.colUniforms, b.linearGradientUniforms, b.texUniforms},
[3]any{b.colUniforms, b.linearGradientUniforms, b.texUniforms},
)
if err != nil {
panic(err)
@@ -565,7 +566,7 @@ func (b *blitter) release() {
}
}
func createColorPrograms(b driver.Device, vsSrc shader.Sources, fsSrc [3]shader.Sources, uniforms [3]interface{}) (pipelines [2][3]*pipeline, err error) {
func createColorPrograms(b driver.Device, vsSrc shader.Sources, fsSrc [3]shader.Sources, uniforms [3]any) (pipelines [2][3]*pipeline, err error) {
defer func() {
if err != nil {
for _, p := range pipelines {
@@ -822,7 +823,7 @@ func (r *renderer) packLayers(layers []opacityLayer) []opacityLayer {
layers[l.parent].clip = b.Union(l.clip)
}
if l.clip.Empty() {
layers = append(layers[:i], layers[i+1:]...)
layers = slices.Delete(layers, i, i+1)
}
}
// Pack layers.
@@ -1316,7 +1317,7 @@ func (b *blitter) blit(mat materialType, fbo bool, col f32color.RGBA, col1, col2
// newUniformBuffer creates a new GPU uniform buffer backed by the
// structure uniformBlock points to.
func newUniformBuffer(b driver.Device, uniformBlock interface{}) *uniformBuffer {
func newUniformBuffer(b driver.Device, uniformBlock any) *uniformBuffer {
ref := reflect.ValueOf(uniformBlock)
// Determine the size of the uniforms structure, *uniforms.
size := ref.Elem().Type().Size()
+5 -3
View File
@@ -21,8 +21,10 @@ import (
var dumpImages = flag.Bool("saveimages", false, "save test images")
var clearCol = color.NRGBA{A: 0xff, R: 0xde, G: 0xad, B: 0xbe}
var clearColExpect = f32color.NRGBAToRGBA(clearCol)
var (
clearCol = color.NRGBA{A: 0xff, R: 0xde, G: 0xad, B: 0xbe}
clearColExpect = f32color.NRGBAToRGBA(clearCol)
)
func TestFramebufferClear(t *testing.T) {
b := newDriver(t)
@@ -202,5 +204,5 @@ func saveImage(file string, img image.Image) error {
if err := png.Encode(&buf, img); err != nil {
return err
}
return os.WriteFile(file, buf.Bytes(), 0666)
return os.WriteFile(file, buf.Bytes(), 0o666)
}
+3 -8
View File
@@ -152,9 +152,7 @@ func newDirect3D11Device(api driver.Direct3D11) (driver.Device, error) {
}
func (b *Backend) BeginFrame(target driver.RenderTarget, clear bool, viewport image.Point) driver.Texture {
var (
renderTarget *d3d11.RenderTargetView
)
var renderTarget *d3d11.RenderTargetView
if target != nil {
switch t := target.(type) {
case driver.Direct3D11RenderTarget:
@@ -229,10 +227,7 @@ func (b *Backend) NewTexture(format driver.TextureFormat, width, height int, min
// Flags required by ID3D11DeviceContext::GenerateMips.
bindFlags |= d3d11.BIND_SHADER_RESOURCE | d3d11.BIND_RENDER_TARGET
miscFlags |= d3d11.RESOURCE_MISC_GENERATE_MIPS
dim := width
if height > dim {
dim = height
}
dim := max(height, width)
log2 := 32 - bits.LeadingZeros32(uint32(dim)) - 1
nmipmaps = log2 + 1
}
@@ -802,7 +797,7 @@ func (t *Texture) ReadPixels(src image.Rectangle, pixels []byte, stride int) err
mapSize := dstPitch * h
data := sliceOf(resMap.PData, mapSize)
width := w * 4
for r := 0; r < h; r++ {
for r := range h {
pixels := pixels[r*srcPitch:]
copy(pixels[:width], data[r*dstPitch:])
}
+5 -3
View File
@@ -96,8 +96,10 @@ type BlendFactor uint8
type Topology uint8
type TextureFilter uint8
type TextureFormat uint8
type (
TextureFilter uint8
TextureFormat uint8
)
type BufferBinding uint8
@@ -217,7 +219,7 @@ func flipImageY(stride, height int, pixels []byte) {
// Flip image in y-direction. OpenGL's origin is in the lower
// left corner.
row := make([]uint8, stride)
for y := 0; y < height/2; y++ {
for y := range height / 2 {
y1 := height - y - 1
dest := y1 * stride
src := y * stride
+5 -12
View File
@@ -8,6 +8,7 @@ import (
"image"
"math/bits"
"runtime"
"slices"
"strings"
"time"
"unsafe"
@@ -717,10 +718,7 @@ func (b *Backend) NewTexture(format driver.TextureFormat, width, height int, min
if mipmap {
nmipmaps := 1
if mipmap {
dim := width
if height > dim {
dim = height
}
dim := max(height, width)
log2 := 32 - bits.LeadingZeros32(uint32(dim)) - 1
nmipmaps = log2 + 1
}
@@ -1132,7 +1130,7 @@ func (b *Backend) setupVertexArrays() {
enabled[inp.Location] = true
b.glstate.vertexAttribPointer(b.funcs, buf.obj, inp.Location, l.Size, gltyp, false, p.layout.Stride, buf.offset+l.Offset)
}
for i := 0; i < max; i++ {
for i := range max {
b.glstate.setVertexAttribArray(b.funcs, i, enabled[i])
}
}
@@ -1175,7 +1173,7 @@ func (t *texture) ReadPixels(src image.Rectangle, pixels []byte, stride int) err
} else {
tmp := make([]byte, w*h*4)
t.backend.funcs.ReadPixels(src.Min.X, src.Min.Y, w, h, gl.RGBA, gl.UNSIGNED_BYTE, tmp)
for y := 0; y < h; y++ {
for y := range h {
copy(pixels[y*stride:], tmp[y*w*4:])
}
}
@@ -1357,12 +1355,7 @@ func alphaTripleFor(ver [2]int) textureTriple {
}
func hasExtension(exts []string, ext string) bool {
for _, e := range exts {
if ext == e {
return true
}
}
return false
return slices.Contains(exts, ext)
}
func firstBufferType(typ driver.BufferBinding) gl.Enum {
+19 -19
View File
@@ -66,8 +66,8 @@ func BenchmarkDrawUICached(b *testing.B) {
defer w.Release()
drawCore(gtx, th)
w.Frame(gtx.Ops)
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
w.Frame(gtx.Ops)
}
finishBenchmark(b, w)
@@ -83,8 +83,8 @@ func BenchmarkDrawUI(b *testing.B) {
drawCore(gtx, th)
w.Frame(gtx.Ops)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
for i := 0; b.Loop(); i++ {
resetOps(gtx)
off := float32(math.Mod(float64(i)/10, 10))
@@ -105,8 +105,8 @@ func BenchmarkDrawUITransformed(b *testing.B) {
drawCore(gtx, th)
w.Frame(gtx.Ops)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
for i := 0; b.Loop(); i++ {
resetOps(gtx)
angle := float32(math.Mod(float64(i)/1000, 0.05))
@@ -130,8 +130,8 @@ func Benchmark1000Circles(b *testing.B) {
draw1000Circles(gtx)
w.Frame(gtx.Ops)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
resetOps(gtx)
draw1000Circles(gtx)
w.Frame(gtx.Ops)
@@ -147,8 +147,8 @@ func Benchmark1000CirclesInstanced(b *testing.B) {
draw1000CirclesInstanced(gtx)
w.Frame(gtx.Ops)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
resetOps(gtx)
draw1000CirclesInstanced(gtx)
w.Frame(gtx.Ops)
@@ -158,9 +158,9 @@ func Benchmark1000CirclesInstanced(b *testing.B) {
func draw1000Circles(gtx layout.Context) {
ops := gtx.Ops
for x := 0; x < 100; x++ {
for x := range 100 {
op.Offset(image.Pt(x*10, 0)).Add(ops)
for y := 0; y < 10; y++ {
for y := range 10 {
paint.FillShape(ops,
color.NRGBA{R: 100 + uint8(x), G: 100 + uint8(y), B: 100, A: 120},
clip.RRect{Rect: image.Rect(0, 0, 10, 10), NE: 5, SE: 5, SW: 5, NW: 5}.Op(ops),
@@ -179,9 +179,9 @@ func draw1000CirclesInstanced(gtx layout.Context) {
cl.Pop()
c := r.Stop()
for x := 0; x < 100; x++ {
for x := range 100 {
op.Offset(image.Pt(x*10, 0)).Add(ops)
for y := 0; y < 10; y++ {
for y := range 10 {
paint.ColorOp{Color: color.NRGBA{R: 100 + uint8(x), G: 100 + uint8(y), B: 100, A: 120}}.Add(ops)
c.Add(ops)
op.Offset(image.Pt(0, 100)).Add(ops)
@@ -204,9 +204,9 @@ func drawIndividualShapes(gtx layout.Context, th *material.Theme) chan op.CallOp
go func() {
ops := &op1
c := op.Record(ops)
for x := 0; x < 9; x++ {
for x := range 9 {
op.Offset(image.Pt(x*50, 0)).Add(ops)
for y := 0; y < 9; y++ {
for y := range 9 {
paint.FillShape(ops,
color.NRGBA{R: 100 + uint8(x), G: 100 + uint8(y), B: 100, A: 120},
clip.RRect{Rect: image.Rect(0, 0, 25, 25), NE: 10, SE: 10, SW: 10, NW: 10}.Op(ops),
@@ -233,8 +233,8 @@ func drawShapeInstances(gtx layout.Context, th *material.Theme) chan op.CallOp {
squares.Add(ops)
rad := float32(0)
for x := 0; x < 20; x++ {
for y := 0; y < 20; y++ {
for x := range 20 {
for y := range 20 {
t := op.Offset(image.Pt(x*50+25, y*50+25)).Push(ops)
c.Add(ops)
t.Pop()
@@ -253,7 +253,7 @@ func drawText(gtx layout.Context, th *material.Theme) chan op.CallOp {
c := op.Record(ops)
txt := material.H6(th, "")
for x := 0; x < 40; x++ {
for x := range 40 {
txt.Text = textRows[x]
t := op.Offset(image.Pt(0, 24*x)).Push(ops)
gtx.Ops = ops
+1 -2
View File
@@ -176,7 +176,6 @@ func TestStrokedPathZeroWidth(t *testing.T) {
paint.Fill(o, red)
cl.Pop()
}
}, func(r result) {
r.expect(0, 0, transparent)
r.expect(10, 50, colornames.Black)
@@ -299,7 +298,7 @@ func TestInstancedRects(t *testing.T) {
clip.Pop()
c := macro.Stop()
for i := 0; i < 2; i++ {
for range 2 {
op.Affine(f32.Affine2D{}.Rotate(f32.Pt(0, 0), .2)).Add(o)
c.Add(o)
op.Offset(image.Pt(20, 20)).Add(o)
+4 -3
View File
@@ -24,7 +24,6 @@ func TestTransformMacro(t *testing.T) {
c := constSqPath()
run(t, func(o *op.Ops) {
// render the first Stacked item
m1 := op.Record(o)
dr := image.Rect(0, 0, 128, 50)
@@ -142,8 +141,10 @@ func constSqPath() clip.Op {
func constSqCirc() clip.Op {
innerOps := new(op.Ops)
return clip.RRect{Rect: image.Rect(0, 0, 40, 40),
NW: 20, NE: 20, SW: 20, SE: 20}.Op(innerOps)
return clip.RRect{
Rect: image.Rect(0, 0, 40, 40),
NW: 20, NE: 20, SW: 20, SE: 20,
}.Op(innerOps)
}
func drawChild(ops *op.Ops, text clip.Op) op.CallOp {
+5 -5
View File
@@ -49,8 +49,8 @@ func buildSquares(size int) paint.ImageOp {
sub := size / 4
im := image.NewNRGBA(image.Rect(0, 0, size, size))
c1, c2 := image.NewUniform(colornames.Green), image.NewUniform(colornames.Blue)
for r := 0; r < 4; r++ {
for c := 0; c < 4; c++ {
for r := range 4 {
for c := range 4 {
c1, c2 = c2, c1
draw.Draw(im, image.Rect(r*sub, c*sub, r*sub+sub, c*sub+sub), c1, image.Point{}, draw.Over)
}
@@ -78,7 +78,7 @@ func run(t *testing.T, f func(o *op.Ops), c func(r result)) {
var img *image.RGBA
var err error
ops := new(op.Ops)
for i := 0; i < 3; i++ {
for i := range 3 {
ops.Reset()
img, err = drawImage(t, 128, ops, f)
if err != nil {
@@ -153,7 +153,7 @@ func verifyRef(t *testing.T, img *image.RGBA, frame int) (ok bool) {
}
path = filepath.Join("refs", path+".png")
if *dumpImages {
if err := os.MkdirAll(filepath.Dir(path), 0766); err != nil {
if err := os.MkdirAll(filepath.Dir(path), 0o766); err != nil {
if !os.IsExist(err) {
t.Error(err)
return
@@ -287,7 +287,7 @@ func saveImage(t testing.TB, file string, img *image.RGBA) {
t.Error(err)
return
}
if err := os.WriteFile(file, buf.Bytes(), 0666); err != nil {
if err := os.WriteFile(file, buf.Bytes(), 0o666); err != nil {
t.Error(err)
return
}
+2 -2
View File
@@ -10,10 +10,10 @@ import (
func BenchmarkPacker(b *testing.B) {
var p packer
p.maxDims = image.Point{X: 4096, Y: 4096}
for i := 0; i < b.N; i++ {
for i := 0; b.Loop(); i++ {
p.clear()
p.newPage()
for k := 0; k < 500; k++ {
for k := range 500 {
_, ok := p.tryAdd(xy(k))
if !ok {
b.Fatal("add failed", i, k, xy(k))
+2 -2
View File
@@ -150,7 +150,7 @@ func newCoverer(ctx driver.Device) *coverer {
c.texUniforms = new(coverTexUniforms)
c.linearGradientUniforms = new(coverLinearGradientUniforms)
pipelines, err := createColorPrograms(ctx, gio.Shader_cover_vert, gio.Shader_cover_frag,
[3]interface{}{c.colUniforms, c.linearGradientUniforms, c.texUniforms},
[3]any{c.colUniforms, c.linearGradientUniforms, c.texUniforms},
)
if err != nil {
panic(err)
@@ -162,7 +162,7 @@ func newCoverer(ctx driver.Device) *coverer {
func newStenciler(ctx driver.Device) *stenciler {
// Allocate a suitably large index buffer for drawing paths.
indices := make([]uint16, pathBatchSize*6)
for i := 0; i < pathBatchSize; i++ {
for i := range pathBatchSize {
i := uint16(i)
indices[i*6+0] = i*4 + 0
indices[i*6+1] = i*4 + 1