Skip to content

Feature/event loop interop #215

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions application.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ func (a *Application) Run() error {
return errors.Errorf("invalid window mode %T", a.config.windowMode)
}

glfw.WindowHint(glfw.ContextVersionMajor, 4) // OR 2
glfw.WindowHint(glfw.ContextVersionMinor, 1)
glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True)

a.window, err = glfw.CreateWindow(a.config.windowInitialDimensions.width, a.config.windowInitialDimensions.height, "Loading..", monitor, nil)
if err != nil {
return errors.Wrap(err, "creating glfw window")
Expand Down Expand Up @@ -203,8 +208,20 @@ func (a *Application) Run() error {
return glfw.GetProcAddress(procName)
}

eventLoop := newEventLoop(
glfw.PostEmptyEvent, // Wakeup GLFW
a.engine.RunTask, // Flush tasks
)
a.engine.TaskRunnerRunOnCurrentThread = eventLoop.RunOnCurrentThread
a.engine.TaskRunnerPostTask = eventLoop.PostTask

a.engine.PlatfromMessage = messenger.handlePlatformMessage

texturer := newRegistry(a.engine, a.window)
a.engine.GLExternalTextureFrameCallback = texturer.handleExternalTexture

texturer.init()

// Not very nice, but we can only really fix this when there's a pluggable
// renderer.
defaultTextinputPlugin.keyboardLayout = a.config.keyboardLayout
Expand Down Expand Up @@ -242,6 +259,16 @@ func (a *Application) Run() error {
// platfrom message can be corectly handled by ui.Window.onPlatformMessage.
glfw.WaitEvents()

for _, p := range a.config.plugins {
// Extra init call for plugins that satisfy the PluginTexture interface.
if glfwPlugin, ok := p.(PluginTexture); ok {
err = glfwPlugin.InitPluginTexture(texturer)
if err != nil {
return errors.Wrap(err, "failed to initialize texture plugin"+fmt.Sprintf("%T", p))
}
}
}

a.window.SetKeyCallback(
func(window *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {
defaultTextinputPlugin.glfwKeyCallback(window, key, scancode, action, mods)
Expand All @@ -258,8 +285,9 @@ func (a *Application) Run() error {
defer a.engine.Shutdown()

for !a.window.ShouldClose() {
glfw.WaitEventsTimeout(0.016) // timeout to get 60fps-ish iterations
embedder.FlutterEngineFlushPendingTasksNow()
eventLoop.WaitForEvents(func(duration float64) {
glfw.WaitEventsTimeout(duration)
})
defaultPlatformPlugin.glfwTasker.ExecuteTasks()
messenger.engineTasker.ExecuteTasks()
}
Expand Down
80 changes: 68 additions & 12 deletions embedder/embedder.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@ const (
ResultEngineNotRunning Result = -1
)

// FlutterOpenGLTexture corresponds to the C.FlutterOpenGLTexture struct.
type FlutterOpenGLTexture struct {
// Target texture of the active texture unit (example GL_TEXTURE_2D)
Target uint32
// The name of the texture
Name uint32
// The texture format (example GL_RGBA8)
Format uint32
}

// FlutterTask is a type alias to C.FlutterTask
type FlutterTask = C.FlutterTask

// FlutterEngine corresponds to the C.FlutterEngine with his associated callback's method.
type FlutterEngine struct {
// Flutter Engine.
Expand All @@ -65,12 +78,17 @@ type FlutterEngine struct {
index int

// GL callback functions
GLMakeCurrent func() bool
GLClearCurrent func() bool
GLPresent func() bool
GLFboCallback func() int32
GLMakeResourceCurrent func() bool
GLProcResolver func(procName string) unsafe.Pointer
GLMakeCurrent func() bool
GLClearCurrent func() bool
GLPresent func() bool
GLFboCallback func() int32
GLMakeResourceCurrent func() bool
GLProcResolver func(procName string) unsafe.Pointer
GLExternalTextureFrameCallback func(textureID int64, width int, height int) *FlutterOpenGLTexture

// task runner interop
TaskRunnerRunOnCurrentThread func() bool
TaskRunnerPostTask func(trask FlutterTask, targetTimeNanos uint64)

// platform message callback function
PlatfromMessage func(message *PlatformMessage)
Expand Down Expand Up @@ -299,10 +317,48 @@ func (flu *FlutterEngine) SendPlatformMessageResponse(
return (Result)(res)
}

// FlutterEngineFlushPendingTasksNow flush tasks on a message loop not
// controlled by the Flutter engine.
//
// deprecated soon.
func FlutterEngineFlushPendingTasksNow() {
C.__FlutterEngineFlushPendingTasksNow()
// RegisterExternalTexture registers an external texture with a unique identifier.
func (flu *FlutterEngine) RegisterExternalTexture(textureID int64) Result {
flu.sync.Lock()
defer flu.sync.Unlock()
if flu.closed {
return ResultEngineNotRunning
}
res := C.FlutterEngineRegisterExternalTexture(flu.Engine, C.int64_t(textureID))
return (Result)(res)
}

// UnregisterExternalTexture unregisters a previous texture registration.
func (flu *FlutterEngine) UnregisterExternalTexture(textureID int64) Result {
flu.sync.Lock()
defer flu.sync.Unlock()
if flu.closed {
return ResultEngineNotRunning
}
res := C.FlutterEngineUnregisterExternalTexture(flu.Engine, C.int64_t(textureID))
return (Result)(res)
}

// MarkExternalTextureFrameAvailable marks that a new texture frame is
// available for a given texture identifier.
func (flu *FlutterEngine) MarkExternalTextureFrameAvailable(textureID int64) Result {
flu.sync.Lock()
defer flu.sync.Unlock()
if flu.closed {
return ResultEngineNotRunning
}
res := C.FlutterEngineMarkExternalTextureFrameAvailable(flu.Engine, C.int64_t(textureID))
return (Result)(res)
}

// Get the current time in nanoseconds from the clock used by the flutter
// engine.
func FlutterEngineGetCurrentTime() uint64 {
return uint64(C.FlutterEngineGetCurrentTime())
}

// RunTask inform the engine to run the specified task.
func (flu *FlutterEngine) RunTask(task *FlutterTask) Result {
res := C.FlutterEngineRunTask(flu.Engine, task)
return (Result)(res)
}
Loading