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
}
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
}

View File

@ -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 {
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 {

View File

@ -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()

View File

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