Skip to content

Commit

Permalink
Texture support (#217)
Browse files Browse the repository at this point in the history
  • Loading branch information
pchampio committed Aug 4, 2019
1 parent e75d3ab commit 440a5f0
Show file tree
Hide file tree
Showing 11 changed files with 453 additions and 141 deletions.
20 changes: 20 additions & 0 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)
glfw.WindowHint(glfw.ContextVersionMinor, 1)
glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True)

if a.config.windowInitialLocations.xpos != 0 {
// To create the window at a specific position, make it initially invisible
// using the Visible window hint, set its position and then show it.
Expand Down Expand Up @@ -217,6 +222,11 @@ func (a *Application) Run() error {

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 @@ -254,6 +264,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 Down
57 changes: 51 additions & 6 deletions embedder/embedder.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ 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
}

// FlutterEngine corresponds to the C.FlutterEngine with his associated callback's method.
type FlutterEngine struct {
// Flutter Engine.
Expand All @@ -65,12 +75,13 @@ 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

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

// 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)
}

// FlutterEngineFlushPendingTasksNow flush tasks on a message loop not
// controlled by the Flutter engine.
//
Expand Down
Loading

0 comments on commit 440a5f0

Please sign in to comment.