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
This commit is contained in:
Hajime Hoshi 2020-07-14 12:14:56 +09:00
parent 385ff8efdf
commit 2a9fd5ef13
4 changed files with 18 additions and 13 deletions

View File

@ -68,10 +68,6 @@ func sameColors(c1, c2 color.RGBA, delta int) bool {
abs(int(c1.A)-int(c2.A)) <= delta abs(int(c1.A)-int(c2.A)) <= delta
} }
func isGopherJS() bool {
return web.IsBrowser() && runtime.GOOS != "js"
}
func TestImagePixels(t *testing.T) { func TestImagePixels(t *testing.T) {
img0, img, err := openEbitenImage() img0, img, err := openEbitenImage()
if err != nil { if err != nil {
@ -549,7 +545,7 @@ func TestImageClear(t *testing.T) {
func TestImageEdge(t *testing.T) { func TestImageEdge(t *testing.T) {
// TODO: This test is not so meaningful after #1218. Do we remove this? // TODO: This test is not so meaningful after #1218. Do we remove this?
if isGopherJS() { if web.IsGopherJS() {
t.Skip("too slow on GopherJS") t.Skip("too slow on GopherJS")
return return
} }
@ -903,7 +899,7 @@ func TestImageCopy(t *testing.T) {
// Issue #611, #907 // Issue #611, #907
func TestImageStretch(t *testing.T) { func TestImageStretch(t *testing.T) {
if isGopherJS() { if web.IsGopherJS() {
t.Skip("too slow on GopherJS") t.Skip("too slow on GopherJS")
return return
} }

View File

@ -15,9 +15,8 @@
package mipmap package mipmap
import ( import (
"sync"
"github.com/hajimehoshi/ebiten/internal/graphics" "github.com/hajimehoshi/ebiten/internal/graphics"
"github.com/hajimehoshi/ebiten/internal/web"
) )
var ( var (
@ -29,11 +28,10 @@ var (
type verticesBackend struct { type verticesBackend struct {
backend []float32 backend []float32
head int head int
m sync.Mutex
} }
func (v *verticesBackend) slice(n int, last bool) []float32 { 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 need := n * graphics.VertexFloatNum
if l := len(v.backend); v.head+need > l { if l := len(v.backend); v.head+need > l {
@ -51,13 +49,15 @@ func (v *verticesBackend) slice(n int, last bool) []float32 {
} else { } else {
v.head += need v.head += need
} }
v.m.Unlock()
return s return s
} }
func vertexSlice(n int, last bool) []float32 { func vertexSlice(n int, last bool) []float32 {
if web.IsGopherJS() {
// In GopherJS, allocating memory by make is expensive. Use the backend instead.
return theVerticesBackend.slice(n, last) 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 { func quadVertices(sx0, sy0, sx1, sy1 int, a, b, c, d, tx, ty float32, cr, cg, cb, ca float32, last bool) []float32 {

View File

@ -17,6 +17,7 @@
package web package web
import ( import (
"runtime"
"strings" "strings"
"syscall/js" "syscall/js"
) )
@ -25,6 +26,10 @@ func IsBrowser() bool {
return true return true
} }
func IsGopherJS() bool {
return IsBrowser() && runtime.GOOS != "js"
}
var ( var (
userAgent = js.Global().Get("navigator").Get("userAgent").String() userAgent = js.Global().Get("navigator").Get("userAgent").String()

View File

@ -20,6 +20,10 @@ func IsBrowser() bool {
return false return false
} }
func IsGopherJS() bool {
return false
}
func IsIOSSafari() bool { func IsIOSSafari() bool {
return false return false
} }