From 63f2c3cb9fb9f571cfcb117842c9dafaf87d5443 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 20 Feb 2016 04:39:43 +0900 Subject: [PATCH] graphics: Refactoring: remove ExecOnUIThread --- graphicscontext.go | 45 +++++++++------------- image.go | 85 ++++++++++++++---------------------------- init.go | 4 +- internal/ui/ui_glfw.go | 4 -- internal/ui/ui_js.go | 4 -- run.go | 4 +- 6 files changed, 46 insertions(+), 100 deletions(-) diff --git a/graphicscontext.go b/graphicscontext.go index b734c3169..c552253aa 100644 --- a/graphicscontext.go +++ b/graphicscontext.go @@ -16,16 +16,8 @@ package ebiten import ( "github.com/hajimehoshi/ebiten/internal/graphics" - "github.com/hajimehoshi/ebiten/internal/graphics/opengl" - "github.com/hajimehoshi/ebiten/internal/ui" ) -func useGLContext(f func(*opengl.Context)) { - ui.ExecOnUIThread(func() { - f(glContext) - }) -} - func newGraphicsContext(screenWidth, screenHeight, screenScale int) (*graphicsContext, error) { c := &graphicsContext{} if err := c.setSize(screenWidth, screenHeight, screenScale); err != nil { @@ -67,25 +59,22 @@ func (c *graphicsContext) setSize(screenWidth, screenHeight, screenScale int) er c.screen.Dispose() } - var err error - useGLContext(func(g *opengl.Context) { - f, err := graphics.NewZeroFramebuffer(g, screenWidth*screenScale, screenHeight*screenScale) - if err != nil { - return - } + f, err := graphics.NewZeroFramebuffer(glContext, screenWidth*screenScale, screenHeight*screenScale) + if err != nil { + return err + } - texture, err := graphics.NewTexture(g, screenWidth, screenHeight, g.Nearest) - if err != nil { - return - } - screenF, err := graphics.NewFramebufferFromTexture(g, texture) - if err != nil { - return - } - screen := &Image{framebuffer: screenF, texture: texture} - c.defaultRenderTarget = &Image{framebuffer: f, texture: nil} - c.screen = screen - c.screenScale = screenScale - }) - return err + texture, err := graphics.NewTexture(glContext, screenWidth, screenHeight, glContext.Nearest) + if err != nil { + return err + } + screenF, err := graphics.NewFramebufferFromTexture(glContext, texture) + if err != nil { + return err + } + screen := &Image{framebuffer: screenF, texture: texture} + c.defaultRenderTarget = &Image{framebuffer: f, texture: nil} + c.screen = screen + c.screenScale = screenScale + return nil } diff --git a/image.go b/image.go index 8573226fb..13a4bda4d 100644 --- a/image.go +++ b/image.go @@ -22,7 +22,6 @@ import ( "runtime" "github.com/hajimehoshi/ebiten/internal/graphics" - "github.com/hajimehoshi/ebiten/internal/graphics/opengl" ) // Image represents an image. @@ -58,10 +57,7 @@ func (i *Image) Fill(clr color.Color) (err error) { return errors.New("image is already disposed") } i.pixels = nil - useGLContext(func(c *opengl.Context) { - err = i.framebuffer.Fill(c, clr) - }) - return + return i.framebuffer.Fill(glContext, clr) } // DrawImage draws the given image on the receiver image. @@ -102,10 +98,7 @@ func (i *Image) DrawImage(image *Image, options *DrawImageOptions) (err error) { } w, h := image.Size() quads := &textureQuads{parts: parts, width: w, height: h} - useGLContext(func(c *opengl.Context) { - err = i.framebuffer.DrawTexture(c, image.texture, quads, &options.GeoM, &options.ColorM) - }) - return + return i.framebuffer.DrawTexture(glContext, image.texture, quads, &options.GeoM, &options.ColorM) } // Bounds returns the bounds of the image. @@ -127,13 +120,11 @@ func (i *Image) At(x, y int) color.Color { return color.Transparent } if i.pixels == nil { - useGLContext(func(c *opengl.Context) { - var err error - i.pixels, err = i.framebuffer.Pixels(c) - if err != nil { - panic(err) - } - }) + var err error + i.pixels, err = i.framebuffer.Pixels(glContext) + if err != nil { + panic(err) + } } w, _ := i.Size() w = graphics.NextPowerOf2Int(w) @@ -150,12 +141,10 @@ func (i *Image) Dispose() error { if i.isDisposed() { return errors.New("image is already disposed") } - useGLContext(func(c *opengl.Context) { - i.framebuffer.Dispose(c) - i.framebuffer = nil - i.texture.Dispose(c) - i.texture = nil - }) + i.framebuffer.Dispose(glContext) + i.framebuffer = nil + i.texture.Dispose(glContext) + i.texture = nil i.pixels = nil runtime.SetFinalizer(i, nil) return nil @@ -184,11 +173,7 @@ func (i *Image) ReplacePixels(p []uint8) error { if len(p) != l { return errors.New(fmt.Sprintf("p's length must be %d", l)) } - var err error - useGLContext(func(c *opengl.Context) { - err = i.texture.ReplacePixels(c, p) - }) - return err + return i.texture.ReplacePixels(glContext, p) } // A DrawImageOptions represents options to render an image on an image. @@ -208,28 +193,20 @@ type DrawImageOptions struct { // even though nothing refers the image object and GC works. // It is because there is no way to define finalizers for Go objects if you use GopherJS. func NewImage(width, height int, filter Filter) (*Image, error) { - var img *Image - var err error - useGLContext(func(c *opengl.Context) { - var texture *graphics.Texture - var framebuffer *graphics.Framebuffer - texture, err = graphics.NewTexture(c, width, height, glFilter(c, filter)) - if err != nil { - return - } - framebuffer, err = graphics.NewFramebufferFromTexture(c, texture) - if err != nil { - return - } - img = &Image{framebuffer: framebuffer, texture: texture} - }) + texture, err := graphics.NewTexture(glContext, width, height, glFilter(glContext, filter)) if err != nil { return nil, err } + framebuffer, err := graphics.NewFramebufferFromTexture(glContext, texture) + if err != nil { + // TODO: texture should be removed here? + return nil, err + } + img := &Image{framebuffer: framebuffer, texture: texture} + runtime.SetFinalizer(img, (*Image).Dispose) if err := img.Clear(); err != nil { return nil, err } - runtime.SetFinalizer(img, (*Image).Dispose) return img, nil } @@ -240,24 +217,16 @@ func NewImage(width, height int, filter Filter) (*Image, error) { // even though nothing refers the image object and GC works. // It is because there is no way to define finalizers for Go objects if you use GopherJS. func NewImageFromImage(img image.Image, filter Filter) (*Image, error) { - var eimg *Image - var err error - useGLContext(func(c *opengl.Context) { - var texture *graphics.Texture - var framebuffer *graphics.Framebuffer - texture, err = graphics.NewTextureFromImage(c, img, glFilter(c, filter)) - if err != nil { - return - } - framebuffer, err = graphics.NewFramebufferFromTexture(c, texture) - if err != nil { - return - } - eimg = &Image{framebuffer: framebuffer, texture: texture} - }) + texture, err := graphics.NewTextureFromImage(glContext, img, glFilter(glContext, filter)) if err != nil { return nil, err } + framebuffer, err := graphics.NewFramebufferFromTexture(glContext, texture) + if err != nil { + // TODO: texture should be removed here? + return nil, err + } + eimg := &Image{framebuffer: framebuffer, texture: texture} runtime.SetFinalizer(eimg, (*Image).Dispose) return eimg, nil } diff --git a/init.go b/init.go index ba7c7b959..918c23258 100644 --- a/init.go +++ b/init.go @@ -23,7 +23,5 @@ var glContext *opengl.Context func init() { ui.Init() - ui.ExecOnUIThread(func() { - glContext = opengl.NewContext() - }) + glContext = opengl.NewContext() } diff --git a/internal/ui/ui_glfw.go b/internal/ui/ui_glfw.go index 5ae88e075..238887d06 100644 --- a/internal/ui/ui_glfw.go +++ b/internal/ui/ui_glfw.go @@ -60,10 +60,6 @@ func Init() { currentUI = u } -func ExecOnUIThread(f func()) { - f() -} - func Start(width, height, scale int, title string) (actualScale int, err error) { return currentUI.start(width, height, scale, title) } diff --git a/internal/ui/ui_js.go b/internal/ui/ui_js.go index a177a6391..5f654eb31 100644 --- a/internal/ui/ui_js.go +++ b/internal/ui/ui_js.go @@ -27,10 +27,6 @@ func Now() int64 { return int64(js.Global.Get("performance").Call("now").Float() * float64(time.Millisecond)) } -func ExecOnUIThread(f func()) { - f() -} - func Start(width, height, scale int, title string) (actualScale int, err error) { return currentUI.start(width, height, scale, title) } diff --git a/run.go b/run.go index bbb9974a7..5ae950357 100644 --- a/run.go +++ b/run.go @@ -54,9 +54,7 @@ func Run(f func(*Image) error, width, height, scale int, title string) error { } defer ui.Terminate() - ui.ExecOnUIThread(func() { - glContext.Check() - }) + glContext.Check() graphicsContext, err := newGraphicsContext(width, height, actualScale) if err != nil { return err