diff --git a/internal/graphics/opengl/context_mobile.go b/internal/graphics/opengl/context_mobile.go index 0775c2e35..025e189d4 100644 --- a/internal/graphics/opengl/context_mobile.go +++ b/internal/graphics/opengl/context_mobile.go @@ -41,9 +41,11 @@ func (p Program) id() programID { } type context struct { - gl mgl.Context - worker mgl.Worker - initialized chan struct{} + gl mgl.Context + worker mgl.Worker + initialized chan struct{} + screenFramebufferWidth int + screenFramebufferHeight int } func NewContext() (*Context, error) { @@ -79,11 +81,22 @@ func NewContext() (*Context, error) { c.BlendFunc(CompositeModeSourceOver) f := c.gl.GetInteger(mgl.FRAMEBUFFER_BINDING) c.screenFramebuffer = Framebuffer(mgl.Framebuffer{uint32(f)}) + // It is invalid to get the size of the deafult framebuffer. + if c.screenFramebuffer.Value != 0 { + width := c.gl.GetRenderbufferParameteri(mgl.RENDERBUFFER, mgl.RENDERBUFFER_WIDTH) + height := c.gl.GetRenderbufferParameteri(mgl.RENDERBUFFER, mgl.RENDERBUFFER_HEIGHT) + c.screenFramebufferWidth = width + c.screenFramebufferHeight = height + } close(c.initialized) }() return c, nil } +func (c *Context) ScreenFramebufferSize() (int, int) { + return c.screenFramebufferWidth, c.screenFramebufferHeight +} + func (c *Context) Resume() error { c.locationCache = newLocationCache() c.lastFramebuffer = invalidFramebuffer @@ -94,12 +107,12 @@ func (c *Context) Resume() error { c.BlendFunc(CompositeModeSourceOver) f := c.gl.GetInteger(mgl.FRAMEBUFFER_BINDING) c.screenFramebuffer = Framebuffer(mgl.Framebuffer{uint32(f)}) + // TODO: Need to update screenFramebufferWidth/Height? return nil } -func (c *Context) WaitUntilInitializingDone() { - // TODO: Call this function at an approriate place - <-c.initialized +func (c *Context) InitializedCh() <-chan struct{} { + return c.initialized } func (c *Context) Worker() mgl.Worker { diff --git a/internal/ui/ui_mobile.go b/internal/ui/ui_mobile.go index 4fb0c01e3..e9d3d0ddf 100644 --- a/internal/ui/ui_mobile.go +++ b/internal/ui/ui_mobile.go @@ -36,6 +36,13 @@ func Render(chError <-chan error) error { return errors.New("ui: chError must not be nil") } // TODO: Check this is called on the rendering thread + if chGLInitialized != nil { + if err := doGLWorks(chError, glContext.InitializedCh()); err != nil { + return err + } + close(chGLInitialized) + <-chGLInitializedEnd + } select { case chRender <- struct{}{}: return doGLWorks(chError, chRenderEnd) @@ -67,16 +74,19 @@ loop: } type userInterface struct { - width int - height int - scale int - sizeChanged bool + width int + height int + scale int + framebufferScale int + sizeChanged bool } var ( - chRender = make(chan struct{}) - chRenderEnd = make(chan struct{}) - currentUI = &userInterface{ + chRender = make(chan struct{}) + chRenderEnd = make(chan struct{}) + chGLInitialized = make(chan struct{}) + chGLInitializedEnd = make(chan struct{}) + currentUI = &userInterface{ sizeChanged: true, } ) @@ -99,6 +109,11 @@ func (u *userInterface) Terminate() error { func (u *userInterface) Update() (interface{}, error) { // TODO: Need lock? + if chGLInitialized != nil { + <-chGLInitialized + chGLInitialized = nil + close(chGLInitializedEnd) + } if u.sizeChanged { u.sizeChanged = false e := ScreenSizeEvent{ @@ -132,7 +147,17 @@ func (u *userInterface) ScreenScale() int { } func (u *userInterface) actualScreenScale() int { - return u.scale + if u.framebufferScale == 0 { + width, _ := glContext.ScreenFramebufferSize() + if width == 0 { + // Android + u.framebufferScale = 1 + } else { + // iOS + u.framebufferScale = width / u.width + } + } + return u.scale * u.framebufferScale } func UpdateTouches(touches []Touch) {