Compare commits

...

2 Commits

Author SHA1 Message Date
Hajime Hoshi
4a10702f6c internal/ui: replace deprecated APIs
[UIScreen mainScreen] is now deprecated.
2024-09-16 18:20:03 +09:00
SolarLune
a113687d56
ebiten: don't create a vertex copy in DrawTriangles() / DrawTrianglesShader() (#3104)
Closes #3103
2024-09-16 14:45:16 +09:00
4 changed files with 61 additions and 32 deletions

View File

@ -554,30 +554,32 @@ func (i *Image) DrawTriangles(vertices []Vertex, indices []uint16, img *Image, o
vs := i.ensureTmpVertices(len(vertices) * graphics.VertexFloatCount) vs := i.ensureTmpVertices(len(vertices) * graphics.VertexFloatCount)
dst := i dst := i
if options.ColorScaleMode == ColorScaleModeStraightAlpha { if options.ColorScaleMode == ColorScaleModeStraightAlpha {
for i, v := range vertices { // Avoid using `for i, v := range vertices` as adding `v` creates a copy from `vertices` unnecessarily on each loop (#3103).
dx, dy := dst.adjustPositionF32(v.DstX, v.DstY) for i := range vertices {
dx, dy := dst.adjustPositionF32(vertices[i].DstX, vertices[i].DstY)
vs[i*graphics.VertexFloatCount] = dx vs[i*graphics.VertexFloatCount] = dx
vs[i*graphics.VertexFloatCount+1] = dy vs[i*graphics.VertexFloatCount+1] = dy
sx, sy := img.adjustPositionF32(v.SrcX, v.SrcY) sx, sy := img.adjustPositionF32(vertices[i].SrcX, vertices[i].SrcY)
vs[i*graphics.VertexFloatCount+2] = sx vs[i*graphics.VertexFloatCount+2] = sx
vs[i*graphics.VertexFloatCount+3] = sy vs[i*graphics.VertexFloatCount+3] = sy
vs[i*graphics.VertexFloatCount+4] = v.ColorR * v.ColorA * cr vs[i*graphics.VertexFloatCount+4] = vertices[i].ColorR * vertices[i].ColorA * cr
vs[i*graphics.VertexFloatCount+5] = v.ColorG * v.ColorA * cg vs[i*graphics.VertexFloatCount+5] = vertices[i].ColorG * vertices[i].ColorA * cg
vs[i*graphics.VertexFloatCount+6] = v.ColorB * v.ColorA * cb vs[i*graphics.VertexFloatCount+6] = vertices[i].ColorB * vertices[i].ColorA * cb
vs[i*graphics.VertexFloatCount+7] = v.ColorA * ca vs[i*graphics.VertexFloatCount+7] = vertices[i].ColorA * ca
} }
} else { } else {
for i, v := range vertices { // See comment above (#3103).
dx, dy := dst.adjustPositionF32(v.DstX, v.DstY) for i := range vertices {
dx, dy := dst.adjustPositionF32(vertices[i].DstX, vertices[i].DstY)
vs[i*graphics.VertexFloatCount] = dx vs[i*graphics.VertexFloatCount] = dx
vs[i*graphics.VertexFloatCount+1] = dy vs[i*graphics.VertexFloatCount+1] = dy
sx, sy := img.adjustPositionF32(v.SrcX, v.SrcY) sx, sy := img.adjustPositionF32(vertices[i].SrcX, vertices[i].SrcY)
vs[i*graphics.VertexFloatCount+2] = sx vs[i*graphics.VertexFloatCount+2] = sx
vs[i*graphics.VertexFloatCount+3] = sy vs[i*graphics.VertexFloatCount+3] = sy
vs[i*graphics.VertexFloatCount+4] = v.ColorR * cr vs[i*graphics.VertexFloatCount+4] = vertices[i].ColorR * cr
vs[i*graphics.VertexFloatCount+5] = v.ColorG * cg vs[i*graphics.VertexFloatCount+5] = vertices[i].ColorG * cg
vs[i*graphics.VertexFloatCount+6] = v.ColorB * cb vs[i*graphics.VertexFloatCount+6] = vertices[i].ColorB * cb
vs[i*graphics.VertexFloatCount+7] = v.ColorA * ca vs[i*graphics.VertexFloatCount+7] = vertices[i].ColorA * ca
} }
} }
is := i.ensureTmpIndices(len(indices)) is := i.ensureTmpIndices(len(indices))
@ -718,24 +720,25 @@ func (i *Image) DrawTrianglesShader(vertices []Vertex, indices []uint16, shader
vs := i.ensureTmpVertices(len(vertices) * graphics.VertexFloatCount) vs := i.ensureTmpVertices(len(vertices) * graphics.VertexFloatCount)
dst := i dst := i
src := options.Images[0] src := options.Images[0]
for i, v := range vertices { // Avoid using `for i, v := range vertices` as adding `v` creates a copy from `vertices` unnecessarily on each loop (#3103).
dx, dy := dst.adjustPositionF32(v.DstX, v.DstY) for i := range vertices {
dx, dy := dst.adjustPositionF32(vertices[i].DstX, vertices[i].DstY)
vs[i*graphics.VertexFloatCount] = dx vs[i*graphics.VertexFloatCount] = dx
vs[i*graphics.VertexFloatCount+1] = dy vs[i*graphics.VertexFloatCount+1] = dy
sx, sy := v.SrcX, v.SrcY sx, sy := vertices[i].SrcX, vertices[i].SrcY
if src != nil { if src != nil {
sx, sy = src.adjustPositionF32(sx, sy) sx, sy = src.adjustPositionF32(sx, sy)
} }
vs[i*graphics.VertexFloatCount+2] = sx vs[i*graphics.VertexFloatCount+2] = sx
vs[i*graphics.VertexFloatCount+3] = sy vs[i*graphics.VertexFloatCount+3] = sy
vs[i*graphics.VertexFloatCount+4] = v.ColorR vs[i*graphics.VertexFloatCount+4] = vertices[i].ColorR
vs[i*graphics.VertexFloatCount+5] = v.ColorG vs[i*graphics.VertexFloatCount+5] = vertices[i].ColorG
vs[i*graphics.VertexFloatCount+6] = v.ColorB vs[i*graphics.VertexFloatCount+6] = vertices[i].ColorB
vs[i*graphics.VertexFloatCount+7] = v.ColorA vs[i*graphics.VertexFloatCount+7] = vertices[i].ColorA
vs[i*graphics.VertexFloatCount+8] = v.Custom0 vs[i*graphics.VertexFloatCount+8] = vertices[i].Custom0
vs[i*graphics.VertexFloatCount+9] = v.Custom1 vs[i*graphics.VertexFloatCount+9] = vertices[i].Custom1
vs[i*graphics.VertexFloatCount+10] = v.Custom2 vs[i*graphics.VertexFloatCount+10] = vertices[i].Custom2
vs[i*graphics.VertexFloatCount+11] = v.Custom3 vs[i*graphics.VertexFloatCount+11] = vertices[i].Custom3
} }
is := i.ensureTmpIndices(len(indices)) is := i.ensureTmpIndices(len(indices))

View File

@ -119,7 +119,7 @@ func (*graphicsDriverCreatorImpl) newPlayStation5() (graphicsdriver.Graphics, er
return nil, errors.New("ui: PlayStation 5 is not supported in this environment") return nil, errors.New("ui: PlayStation 5 is not supported in this environment")
} }
func deviceScaleFactorImpl() float64 { func (u *UserInterface) deviceScaleFactor() float64 {
var s float64 var s float64
if err := app.RunOnJVM(func(vm, env, ctx uintptr) error { if err := app.RunOnJVM(func(vm, env, ctx uintptr) error {
// TODO: This might be crash when this is called from init(). How can we detect this? // TODO: This might be crash when this is called from init(). How can we detect this?

View File

@ -19,8 +19,31 @@ package ui
// //
// #import <UIKit/UIKit.h> // #import <UIKit/UIKit.h>
// //
// static double devicePixelRatio() { // static double devicePixelRatioOnMainThread(UIView* view) {
// return [[UIScreen mainScreen] nativeScale]; // UIWindow* window = view.window;
// if (!window) {
// return 1;
// }
// UIWindowScene* scene = window.windowScene;
// if (!scene) {
// return 1;
// }
// return scene.screen.nativeScale;
// }
//
// static double devicePixelRatio(uintptr_t viewPtr) {
// if (!viewPtr) {
// return 1;
// }
// UIView* view = (__bridge UIView*)(void*)viewPtr;
// if ([NSThread isMainThread]) {
// return devicePixelRatioOnMainThread(view);
// }
// __block double scale;
// dispatch_sync(dispatch_get_main_queue(), ^{
// scale = devicePixelRatioOnMainThread(view);
// });
// return scale;
// } // }
import "C" import "C"
@ -66,6 +89,7 @@ func (*graphicsDriverCreatorImpl) newPlayStation5() (graphicsdriver.Graphics, er
} }
func (u *UserInterface) SetUIView(uiview uintptr) error { func (u *UserInterface) SetUIView(uiview uintptr) error {
u.uiView.Store(uiview)
select { select {
case err := <-u.errCh: case err := <-u.errCh:
return err return err
@ -89,9 +113,8 @@ func (u *UserInterface) IsGL() (bool, error) {
return u.GraphicsLibrary() == GraphicsLibraryOpenGL, nil return u.GraphicsLibrary() == GraphicsLibraryOpenGL, nil
} }
func deviceScaleFactorImpl() float64 { func (u *UserInterface) deviceScaleFactor() float64 {
// TODO: Can this be called from non-main threads? return float64(C.devicePixelRatio(C.uintptr_t(u.uiView.Load())))
return float64(C.devicePixelRatio())
} }
func dipToNativePixels(x float64, scale float64) float64 { func dipToNativePixels(x float64, scale float64) float64 {

View File

@ -104,6 +104,9 @@ type userInterfaceImpl struct {
strictContextRestoration atomic.Bool strictContextRestoration atomic.Bool
strictContextRestorationOnce sync.Once strictContextRestorationOnce sync.Once
// uiView is used only on iOS.
uiView atomic.Uintptr
m sync.RWMutex m sync.RWMutex
} }
@ -285,7 +288,7 @@ func (m *Monitor) DeviceScaleFactor() float64 {
// Initialize this lazily. // Initialize this lazily.
m.deviceScaleFactorOnce.Do(func() { m.deviceScaleFactorOnce.Do(func() {
// Assume that the device scale factor never changes on mobiles. // Assume that the device scale factor never changes on mobiles.
m.deviceScaleFactor = deviceScaleFactorImpl() m.deviceScaleFactor = theUI.deviceScaleFactor()
}) })
return m.deviceScaleFactor return m.deviceScaleFactor
} }