From 2a9fd5ef13eed6491c6029d2799e270accee253d Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Tue, 14 Jul 2020 12:14:56 +0900 Subject: [PATCH] mipmap: Avoid using the vertex backend when GopherJS is not used The backend logic is especially for GopherJS. As the backend logic tends to consume a lot of memory, let's avoid this when possible. Updates #797 --- image_test.go | 8 ++------ internal/mipmap/vertex.go | 14 +++++++------- internal/web/js.go | 5 +++++ internal/web/notjs.go | 4 ++++ 4 files changed, 18 insertions(+), 13 deletions(-) diff --git a/image_test.go b/image_test.go index c15eb0198..8d44b5dc1 100644 --- a/image_test.go +++ b/image_test.go @@ -68,10 +68,6 @@ func sameColors(c1, c2 color.RGBA, delta int) bool { abs(int(c1.A)-int(c2.A)) <= delta } -func isGopherJS() bool { - return web.IsBrowser() && runtime.GOOS != "js" -} - func TestImagePixels(t *testing.T) { img0, img, err := openEbitenImage() if err != nil { @@ -549,7 +545,7 @@ func TestImageClear(t *testing.T) { func TestImageEdge(t *testing.T) { // TODO: This test is not so meaningful after #1218. Do we remove this? - if isGopherJS() { + if web.IsGopherJS() { t.Skip("too slow on GopherJS") return } @@ -903,7 +899,7 @@ func TestImageCopy(t *testing.T) { // Issue #611, #907 func TestImageStretch(t *testing.T) { - if isGopherJS() { + if web.IsGopherJS() { t.Skip("too slow on GopherJS") return } diff --git a/internal/mipmap/vertex.go b/internal/mipmap/vertex.go index 6608e1e02..7f70d5124 100644 --- a/internal/mipmap/vertex.go +++ b/internal/mipmap/vertex.go @@ -15,9 +15,8 @@ package mipmap import ( - "sync" - "github.com/hajimehoshi/ebiten/internal/graphics" + "github.com/hajimehoshi/ebiten/internal/web" ) var ( @@ -29,11 +28,10 @@ var ( type verticesBackend struct { backend []float32 head int - m sync.Mutex } func (v *verticesBackend) slice(n int, last bool) []float32 { - v.m.Lock() + // As this is called only from GopherJS, mutex is not required. need := n * graphics.VertexFloatNum if l := len(v.backend); v.head+need > l { @@ -51,13 +49,15 @@ func (v *verticesBackend) slice(n int, last bool) []float32 { } else { v.head += need } - - v.m.Unlock() return s } func vertexSlice(n int, last bool) []float32 { - return theVerticesBackend.slice(n, last) + if web.IsGopherJS() { + // In GopherJS, allocating memory by make is expensive. Use the backend instead. + return theVerticesBackend.slice(n, last) + } + return make([]float32, n*graphics.VertexFloatNum) } func quadVertices(sx0, sy0, sx1, sy1 int, a, b, c, d, tx, ty float32, cr, cg, cb, ca float32, last bool) []float32 { diff --git a/internal/web/js.go b/internal/web/js.go index 9db61a059..9ebb188f3 100644 --- a/internal/web/js.go +++ b/internal/web/js.go @@ -17,6 +17,7 @@ package web import ( + "runtime" "strings" "syscall/js" ) @@ -25,6 +26,10 @@ func IsBrowser() bool { return true } +func IsGopherJS() bool { + return IsBrowser() && runtime.GOOS != "js" +} + var ( userAgent = js.Global().Get("navigator").Get("userAgent").String() diff --git a/internal/web/notjs.go b/internal/web/notjs.go index 65fd6cd89..313a6ee3b 100644 --- a/internal/web/notjs.go +++ b/internal/web/notjs.go @@ -20,6 +20,10 @@ func IsBrowser() bool { return false } +func IsGopherJS() bool { + return false +} + func IsIOSSafari() bool { return false }