Skip to content

Commit 62fe831

Browse files
authored
Bugfix/texture thread (#304)
* Ensure openg.DeleteTextures runs on main thread * feat: add other opengl version * fix: add mising WindowHint for resourceWindow * remove older opengl options
1 parent e989718 commit 62fe831

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

application.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@ func NewApplication(opt ...Option) *Application {
6464
// createResourceWindow creates an invisible GLFW window that shares the 'view'
6565
// window's resource context. This window is used to upload resources in the
6666
// background. Must be call after the 'view' window is created.
67+
//
68+
// Though optional, it is recommended that all embedders set this callback as
69+
// it will lead to better performance in texture handling.
6770
func createResourceWindow(window *glfw.Window) (*glfw.Window, error) {
71+
opengl.GLFWWindowHint()
6872
glfw.WindowHint(glfw.Decorated, glfw.False)
6973
glfw.WindowHint(glfw.Visible, glfw.False)
7074
resourceWindow, err := glfw.CreateWindow(1, 1, "", nil, window)
@@ -326,9 +330,12 @@ func (a *Application) Run() error {
326330
eventLoop.WaitForEvents(func(duration float64) {
327331
glfw.WaitEventsTimeout(duration)
328332
})
333+
334+
// Execute tasks that MUST be run in the engine thread (!blocks rendering!)
329335
glfwDebouceTasker.ExecuteTasks()
330336
defaultPlatformPlugin.glfwTasker.ExecuteTasks()
331337
messenger.engineTasker.ExecuteTasks()
338+
texturer.engineTasker.ExecuteTasks()
332339
}
333340

334341
fmt.Println("go-flutter: closing application")

texture-registry.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/go-flutter-desktop/go-flutter/embedder"
88
"github.com/go-flutter-desktop/go-flutter/internal/opengl"
9+
"github.com/go-flutter-desktop/go-flutter/internal/tasker"
910
"github.com/go-gl/glfw/v3.2/glfw"
1011
"github.com/pkg/errors"
1112
)
@@ -21,6 +22,9 @@ type TextureRegistry struct {
2122
channels map[int64]*externalTextureHanlder
2223
channelsLock sync.RWMutex
2324

25+
// engineTasker holds tasks which must be executed in the engine thread
26+
engineTasker *tasker.Tasker
27+
2428
texture int64
2529
texturesLock sync.Mutex
2630
}
@@ -34,12 +38,14 @@ type externalTextureHanlder struct {
3438

3539
func newTextureRegistry(engine *embedder.FlutterEngine, window *glfw.Window) *TextureRegistry {
3640
return &TextureRegistry{
37-
window: window,
38-
engine: engine,
39-
channels: make(map[int64]*externalTextureHanlder),
41+
window: window,
42+
engine: engine,
43+
channels: make(map[int64]*externalTextureHanlder),
44+
engineTasker: tasker.New(),
4045
}
4146
}
4247

48+
// init must happen in engine thread
4349
func (t *TextureRegistry) init() error {
4450
t.window.MakeContextCurrent()
4551
// Important! Call open.Init only under the presence of an active OpenGL context,
@@ -80,7 +86,10 @@ func (t *TextureRegistry) setTextureHandler(textureID int64, handler ExternalTex
8086
if handler == nil {
8187
texture := t.channels[textureID]
8288
if texture != nil {
83-
opengl.DeleteTextures(1, &texture.texture)
89+
t.engineTasker.Do(func() {
90+
// Must run on the main tread
91+
opengl.DeleteTextures(1, &texture.texture)
92+
})
8493
}
8594
delete(t.channels, textureID)
8695
} else {
@@ -91,6 +100,10 @@ func (t *TextureRegistry) setTextureHandler(textureID int64, handler ExternalTex
91100
t.channelsLock.Unlock()
92101
}
93102

103+
// handleExternalTexture receive low level C calls to create and/or update the
104+
// content of a OpenGL TexImage2D.
105+
// Calls must happen on the engine thread, no need to use engineTasker as this
106+
// function is a callback directly managed by the engine.
94107
func (t *TextureRegistry) handleExternalTexture(textureID int64,
95108
width int, height int) *embedder.FlutterOpenGLTexture {
96109

0 commit comments

Comments
 (0)