graphics: Remove 'Resume'

This commit is contained in:
Hajime Hoshi 2016-06-12 23:54:36 +09:00
parent 5fbbb6dc6a
commit a1fbf2cd2d
10 changed files with 53 additions and 48 deletions

View File

@ -16,6 +16,7 @@ package ebiten
import (
"github.com/hajimehoshi/ebiten/internal/graphics"
"github.com/hajimehoshi/ebiten/internal/graphics/opengl"
"github.com/hajimehoshi/ebiten/internal/ui"
)
@ -54,6 +55,16 @@ func (c *graphicsContext) SetSize(screenWidth, screenHeight, screenScale int) er
return nil
}
func (c *graphicsContext) needsRestoring(context *opengl.Context) (bool, error) {
imageM.Lock()
defer imageM.Unlock()
// FlushCommands is required because c.screen.impl might not have an actual texture.
if err := graphics.FlushCommands(ui.GLContext()); err != nil {
return false, err
}
return c.screen.impl.isInvalidated(context), nil
}
func (c *graphicsContext) Update() error {
if !c.initialized {
if err := graphics.Initialize(ui.GLContext()); err != nil {
@ -61,6 +72,15 @@ func (c *graphicsContext) Update() error {
}
c.initialized = true
}
r, err := c.needsRestoring(ui.GLContext())
if err != nil {
return err
}
if r {
if err := c.restore(); err != nil {
return err
}
}
if err := c.screen.Clear(); err != nil {
return err
}
@ -104,7 +124,7 @@ func (c *graphicsContext) flush() error {
return nil
}
func (c *graphicsContext) Resume() error {
func (c *graphicsContext) restore() error {
imageM.Lock()
defer imageM.Unlock()
ui.GLContext().Resume()

View File

@ -347,6 +347,10 @@ func (i *imageImpl) ReplacePixels(p []uint8) error {
return i.image.ReplacePixels(p)
}
func (i *imageImpl) isInvalidated(context *opengl.Context) bool {
return i.image.IsInvalidated(context)
}
// A DrawImageOptions represents options to render an image on an image.
type DrawImageOptions struct {
ImageParts ImageParts

View File

@ -107,3 +107,7 @@ func (i *Image) ReplacePixels(p []uint8) error {
theCommandQueue.Enqueue(c)
return nil
}
func (i *Image) IsInvalidated(context *opengl.Context) bool {
return !context.IsTexture(i.texture.native)
}

View File

@ -195,6 +195,15 @@ func (c *Context) DeleteTexture(t Texture) {
})
}
func (c *Context) IsTexture(t Texture) bool {
r := false
c.RunOnContextThread(func() error {
r = gl.IsTexture(uint32(t))
return nil
})
return r
}
func (c *Context) TexSubImage2D(p []uint8, width, height int) {
c.RunOnContextThread(func() error {
gl.TexSubImage2D(gl.TEXTURE_2D, 0, 0, 0, int32(width), int32(height), gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(p))

View File

@ -196,6 +196,11 @@ func (c *Context) DeleteTexture(t Texture) {
gl.DeleteTexture(t.Object)
}
func (c *Context) IsTexture(t Texture) bool {
gl := c.gl
return gl.IsTexture(t.Object)
}
func (c *Context) TexSubImage2D(p []uint8, width, height int) {
gl := c.gl
// void texSubImage2D(GLenum target, GLint level, GLint xoffset, GLint yoffset,

View File

@ -162,6 +162,11 @@ func (c *Context) DeleteTexture(t Texture) {
gl.DeleteTexture(mgl.Texture(t))
}
func (c *Context) IsTexture(t Texture) bool {
gl := c.gl
return gl.IsTexture(mgl.Texture(t))
}
func (c *Context) TexSubImage2D(p []uint8, width, height int) {
gl := c.gl
gl.TexSubImage2D(mgl.TEXTURE_2D, 0, 0, 0, width, height, mgl.RGBA, mgl.UNSIGNED_BYTE, p)

View File

@ -96,7 +96,6 @@ func (c *runContext) setRunningSlowly(isRunningSlowly bool) {
type GraphicsContext interface {
SetSize(width, height, scale int) error
Update() error
Resume() error
}
func Run(g GraphicsContext, width, height, scale int, title string, fps int) error {
@ -165,11 +164,6 @@ func Run(g GraphicsContext, width, height, scale int, title string, fps int) err
frames = 0
}
e.Done <- struct{}{}
case ui.ResumeEvent:
if err := g.Resume(); err != nil {
return err
}
e.Done <- struct{}{}
default:
panic("not reach")
}

View File

@ -27,11 +27,3 @@ type ScreenSizeEvent struct {
type RenderEvent struct {
Done chan struct{}
}
type PauseEvent struct {
Done chan struct{}
}
type ResumeEvent struct {
Done chan struct{}
}

View File

@ -37,11 +37,6 @@ func Render(chError <-chan error) error {
}
// TODO: Check this is called on the rendering thread
select {
case <-chResumeStart:
if err := doGLWorks(chError, chResumeEnd); err != nil {
return err
}
return nil
case chRender <- struct{}{}:
return doGLWorks(chError, chRenderEnd)
case <-time.After(500 * time.Millisecond):
@ -81,9 +76,6 @@ type userInterface struct {
var (
chRender = make(chan struct{})
chRenderEnd = make(chan struct{})
chResume = make(chan struct{})
chResumeStart = make(chan struct{})
chResumeEnd = make(chan struct{})
currentUI = &userInterface{
sizeChanged: true,
}
@ -117,13 +109,8 @@ func (u *userInterface) Update() (interface{}, error) {
}
return e, nil
}
select {
case <-chRender:
<-chRender
return RenderEvent{chRenderEnd}, nil
case <-chResume:
chResumeStart <- struct{}{}
return ResumeEvent{chResumeEnd}, nil
}
}
func (u *userInterface) SwapBuffers() error {
@ -148,16 +135,6 @@ func (u *userInterface) actualScreenScale() int {
return u.scale
}
// TODO: Remove Resume() and do resuming in Update instead.
// In Update, we'd be able to detect GL context lost by glIsTexture or something,
// and we can do resuming when detecting it.
func Resume() error {
chResume <- struct{}{}
// Don't have to wait for resumeing done.
return nil
}
func UpdateTouches(touches []Touch) {
currentInput.updateTouches(touches)
}

View File

@ -29,7 +29,6 @@ type EventDispatcher interface {
SetScreenSize(width, height int)
SetScreenScale(scale int)
Render() error
Resume()
TouchDown(id int, x, y int)
TouchMove(id int, x, y int)
TouchUp(id int)
@ -69,10 +68,6 @@ func (e *eventDispatcher) Render() error {
return ui.Render(chError)
}
func (e *eventDispatcher) Resume() {
ui.Resume()
}
// touch implements ui.Touch.
type touch struct {
id int