From 2328ddfeca04b13b2fc86510c40df8a99aadc0d0 Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Thu, 11 Mar 2021 11:27:02 +0100 Subject: [PATCH] internal/byteslice: rename package unsafe All functions left in the old package unsafe were provided byte slice views of other types. Rename the package accordingly and avoid a name clash with the standard library package unsafe. Signed-off-by: Elias Naur --- gpu/compute.go | 12 ++++++------ gpu/gpu.go | 4 ++-- gpu/headless/driver_test.go | 4 ++-- gpu/path.go | 4 ++-- .../{unsafe/unsafe.go => byteslice/byteslice.go} | 12 +++++++----- internal/srgb/srgb.go | 4 ++-- 6 files changed, 21 insertions(+), 19 deletions(-) rename internal/{unsafe/unsafe.go => byteslice/byteslice.go} (65%) diff --git a/gpu/compute.go b/gpu/compute.go index ebc9f97f..b5e0756a 100644 --- a/gpu/compute.go +++ b/gpu/compute.go @@ -15,8 +15,8 @@ import ( "gioui.org/f32" "gioui.org/gpu/internal/driver" + "gioui.org/internal/byteslice" "gioui.org/internal/f32color" - gunsafe "gioui.org/internal/unsafe" "gioui.org/layout" "gioui.org/op" ) @@ -459,7 +459,7 @@ restart: m.quads[i].posX = p.X m.quads[i].posY = p.Y } - vertexData := gunsafe.BytesView(m.quads) + vertexData := byteslice.Slice(m.quads) if len(vertexData) > m.bufSize { if m.buffer != nil { m.buffer.Release() @@ -757,7 +757,7 @@ func (g *compute) render(tileDims image.Point) error { return err } } - g.buffers.scene.buffer.Upload(gunsafe.BytesView(g.enc.scene)) + g.buffers.scene.buffer.Upload(byteslice.Slice(g.enc.scene)) w, h := tileDims.X*tileWidthPx, tileDims.Y*tileHeightPx if g.output.size.X != w || g.output.size.Y != h { @@ -805,7 +805,7 @@ func (g *compute) render(tileDims image.Point) error { } } - g.buffers.config.Upload(gunsafe.StructView(g.conf)) + g.buffers.config.Upload(byteslice.Struct(g.conf)) minSize := int(unsafe.Sizeof(memoryHeader{})) + int(alloc) if minSize > g.buffers.memory.size { @@ -821,7 +821,7 @@ func (g *compute) render(tileDims image.Point) error { *g.memHeader = memoryHeader{ mem_offset: alloc, } - g.buffers.memory.buffer.Upload(gunsafe.StructView(g.memHeader)) + g.buffers.memory.buffer.Upload(byteslice.Struct(g.memHeader)) g.buffers.state.buffer.Upload(g.zeros(clearSize)) if realloced { @@ -864,7 +864,7 @@ func (g *compute) render(tileDims image.Point) error { g.ctx.MemoryBarrier() t.kernel4.end() - if err := g.buffers.memory.buffer.Download(gunsafe.StructView(g.memHeader)); err != nil { + if err := g.buffers.memory.buffer.Download(byteslice.Struct(g.memHeader)); err != nil { if err == driver.ErrContentLost { continue } diff --git a/gpu/gpu.go b/gpu/gpu.go index c5e02590..afb7199c 100644 --- a/gpu/gpu.go +++ b/gpu/gpu.go @@ -21,10 +21,10 @@ import ( "gioui.org/f32" "gioui.org/gpu/internal/driver" + "gioui.org/internal/byteslice" "gioui.org/internal/f32color" "gioui.org/internal/opconst" "gioui.org/internal/ops" - gunsafe "gioui.org/internal/unsafe" "gioui.org/layout" "gioui.org/op" "gioui.org/op/clip" @@ -559,7 +559,7 @@ func (r *renderer) release() { func newBlitter(ctx driver.Device) *blitter { quadVerts, err := ctx.NewImmutableBuffer(driver.BufferBindingVertices, - gunsafe.BytesView([]float32{ + byteslice.Slice([]float32{ -1, +1, 0, 0, +1, +1, 1, 0, -1, -1, 0, 1, diff --git a/gpu/headless/driver_test.go b/gpu/headless/driver_test.go index ac45ba03..01323dee 100644 --- a/gpu/headless/driver_test.go +++ b/gpu/headless/driver_test.go @@ -13,8 +13,8 @@ import ( "testing" "gioui.org/gpu/internal/driver" + "gioui.org/internal/byteslice" "gioui.org/internal/f32color" - "gioui.org/internal/unsafe" ) var dumpImages = flag.Bool("saveimages", false, "save test images") @@ -66,7 +66,7 @@ func TestInputShader(t *testing.T) { defer p.Release() b.BindProgram(p) buf, err := b.NewImmutableBuffer(driver.BufferBindingVertices, - unsafe.BytesView([]float32{ + byteslice.Slice([]float32{ 0, .5, .5, 1, -.5, -.5, .5, 1, .5, -.5, .5, 1, diff --git a/gpu/path.go b/gpu/path.go index b7ec3ba2..c9656787 100644 --- a/gpu/path.go +++ b/gpu/path.go @@ -13,8 +13,8 @@ import ( "gioui.org/f32" "gioui.org/gpu/internal/driver" + "gioui.org/internal/byteslice" "gioui.org/internal/f32color" - gunsafe "gioui.org/internal/unsafe" ) type pather struct { @@ -185,7 +185,7 @@ func newStenciler(ctx driver.Device) *stenciler { indices[i*6+4] = i*4 + 1 indices[i*6+5] = i*4 + 3 } - indexBuf, err := ctx.NewImmutableBuffer(driver.BufferBindingIndices, gunsafe.BytesView(indices)) + indexBuf, err := ctx.NewImmutableBuffer(driver.BufferBindingIndices, byteslice.Slice(indices)) if err != nil { panic(err) } diff --git a/internal/unsafe/unsafe.go b/internal/byteslice/byteslice.go similarity index 65% rename from internal/unsafe/unsafe.go rename to internal/byteslice/byteslice.go index 5c6d6840..71bc7047 100644 --- a/internal/unsafe/unsafe.go +++ b/internal/byteslice/byteslice.go @@ -1,14 +1,16 @@ // SPDX-License-Identifier: Unlicense OR MIT -package unsafe +// Package byteslice provides byte slice views of other Go values such as +// slices and structs. +package byteslice import ( "reflect" "unsafe" ) -// StructView returns a byte slice view of a struct. -func StructView(s interface{}) []byte { +// Struct returns a byte slice view of a struct. +func Struct(s interface{}) []byte { v := reflect.ValueOf(s).Elem() sz := int(v.Type().Size()) var res []byte @@ -19,8 +21,8 @@ func StructView(s interface{}) []byte { return res } -// BytesView returns a byte slice view of a slice. -func BytesView(s interface{}) []byte { +// Slice returns a byte slice view of a slice. +func Slice(s interface{}) []byte { v := reflect.ValueOf(s) first := v.Index(0) sz := int(first.Type().Size()) diff --git a/internal/srgb/srgb.go b/internal/srgb/srgb.go index 8a58ce77..2109ef7e 100644 --- a/internal/srgb/srgb.go +++ b/internal/srgb/srgb.go @@ -7,8 +7,8 @@ import ( "runtime" "strings" + "gioui.org/internal/byteslice" "gioui.org/internal/gl" - "gioui.org/internal/unsafe" ) // FBO implements an intermediate sRGB FBO @@ -71,7 +71,7 @@ func (s *FBO) Blit() { s.c.Uniform1i(s.c.GetUniformLocation(prog, "tex"), 0) s.quad = s.c.CreateBuffer() s.c.BindBuffer(gl.ARRAY_BUFFER, s.quad) - coords := unsafe.BytesView([]float32{ + coords := byteslice.Slice([]float32{ -1, +1, 0, 1, +1, +1, 1, 1, -1, -1, 0, 0,