Skip to content

Commit 15e4884

Browse files
committed
Adds comments and renames internals. Also some re-ordering of init code.
1 parent 0e810c5 commit 15e4884

File tree

6 files changed

+93
-47
lines changed

6 files changed

+93
-47
lines changed

application.go

Lines changed: 54 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212

1313
"github.com/go-flutter-desktop/go-flutter/embedder"
1414
"github.com/go-flutter-desktop/go-flutter/internal/execpath"
15-
"github.com/go-flutter-desktop/go-flutter/internal/tasker"
1615
)
1716

1817
// Run executes a flutter application with the provided options.
@@ -105,7 +104,7 @@ func (a *Application) Run() error {
105104
glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
106105
glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True)
107106

108-
if a.config.windowInitialLocations.xpos != 0 {
107+
if a.config.windowInitialLocation.xpos != 0 {
109108
// To create the window at a specific position, make it initially invisible
110109
// using the Visible window hint, set its position and then show it.
111110
glfw.WindowHint(glfw.Visible, glfw.False)
@@ -115,12 +114,11 @@ func (a *Application) Run() error {
115114
if err != nil {
116115
return errors.Wrap(err, "creating glfw window")
117116
}
118-
glfw.DefaultWindowHints()
119117
defer a.window.Destroy()
118+
glfw.DefaultWindowHints()
120119

121-
if a.config.windowInitialLocations.xpos != 0 {
122-
a.window.SetPos(a.config.windowInitialLocations.xpos,
123-
a.config.windowInitialLocations.ypos)
120+
if a.config.windowInitialLocation.xpos != 0 {
121+
a.window.SetPos(a.config.windowInitialLocation.xpos, a.config.windowInitialLocation.ypos)
124122
a.window.Show()
125123
}
126124

@@ -157,6 +155,7 @@ func (a *Application) Run() error {
157155

158156
a.engine = embedder.NewFlutterEngine()
159157

158+
// Create a messenger and init plugins
160159
messenger := newMessenger(a.engine)
161160
for _, p := range a.config.plugins {
162161
err = p.InitPlugin(messenger)
@@ -173,6 +172,16 @@ func (a *Application) Run() error {
173172
}
174173
}
175174

175+
// Create a TextureRegistry
176+
texturer := newTextureRegistry(a.engine, a.window)
177+
178+
// Create a new eventloop
179+
eventLoop := newEventLoop(
180+
glfw.PostEmptyEvent, // Wakeup GLFW
181+
a.engine.RunTask, // Flush tasks
182+
)
183+
184+
// Set configuration values to engine, with fallbacks to sane defaults.
176185
if a.config.flutterAssetsPath != "" {
177186
a.engine.AssetsPath = a.config.flutterAssetsPath
178187
} else {
@@ -182,7 +191,6 @@ func (a *Application) Run() error {
182191
}
183192
a.engine.AssetsPath = filepath.Join(filepath.Dir(execPath), "flutter_assets")
184193
}
185-
186194
if a.config.icuDataPath != "" {
187195
a.engine.IcuDataPath = a.config.icuDataPath
188196
} else {
@@ -193,7 +201,7 @@ func (a *Application) Run() error {
193201
a.engine.IcuDataPath = filepath.Join(filepath.Dir(execPath), "icudtl.dat")
194202
}
195203

196-
// Render callbacks
204+
// Attach GL callback functions onto the engine
197205
a.engine.GLMakeCurrent = func() bool {
198206
a.window.MakeContextCurrent()
199207
return true
@@ -219,31 +227,36 @@ func (a *Application) Run() error {
219227
a.engine.GLProcResolver = func(procName string) unsafe.Pointer {
220228
return glfw.GetProcAddress(procName)
221229
}
230+
a.engine.GLExternalTextureFrameCallback = texturer.handleExternalTexture
222231

223-
eventLoop := newEventLoop(
224-
glfw.PostEmptyEvent, // Wakeup GLFW
225-
a.engine.RunTask, // Flush tasks
226-
)
232+
// Attach TaskRunner callback functions onto the engine
227233
a.engine.TaskRunnerRunOnCurrentThread = eventLoop.RunOnCurrentThread
228234
a.engine.TaskRunnerPostTask = eventLoop.PostTask
229235

236+
// Attach PlatformMessage callback functions onto the engine
230237
a.engine.PlatfromMessage = messenger.handlePlatformMessage
231238

232-
texturer := newRegistry(a.engine, a.window)
233-
a.engine.GLExternalTextureFrameCallback = texturer.handleExternalTexture
234-
235-
texturer.init()
239+
// Initialize the texturer
240+
err = texturer.init()
241+
if err != nil {
242+
fmt.Printf("go-fluter: Failed to init TextureRegistry: %v\n", err)
243+
os.Exit(1)
244+
}
236245

237246
// Not very nice, but we can only really fix this when there's a pluggable
238247
// renderer.
239248
defaultTextinputPlugin.keyboardLayout = a.config.keyboardLayout
240249

250+
// Set the glfw window user pointer to point to the FlutterEngine so that
251+
// callback functions may obtain the FlutterEngine from the glfw window
252+
// user pointer.
241253
flutterEnginePointer := uintptr(unsafe.Pointer(a.engine))
242254
defer func() {
243255
runtime.KeepAlive(flutterEnginePointer)
244256
}()
245257
a.window.SetUserPointer(unsafe.Pointer(&flutterEnginePointer))
246258

259+
// Start the engine
247260
result := a.engine.Run(unsafe.Pointer(&flutterEnginePointer), a.config.vmArguments)
248261
if result != embedder.ResultSuccess {
249262
switch result {
@@ -257,40 +270,48 @@ func (a *Application) Run() error {
257270
os.Exit(1)
258271
}
259272

260-
defaultPlatformPlugin.glfwTasker = tasker.New()
261-
262-
m := newWindowManager()
263-
m.forcedPixelRatio = a.config.forcePixelRatio
264-
265-
m.glfwRefreshCallback(a.window)
266-
a.window.SetRefreshCallback(m.glfwRefreshCallback)
267-
a.window.SetPosCallback(m.glfwPosCallback)
273+
// Setup a new windowManager to handle windows pixel ratio's and pointer
274+
// devices.
275+
windowManager := newWindowManager(a.config.forcePixelRatio)
276+
// force first refresh
277+
windowManager.glfwRefreshCallback(a.window)
278+
// Attach glfw window callbacks for refresh and position changes
279+
a.window.SetRefreshCallback(windowManager.glfwRefreshCallback)
280+
a.window.SetPosCallback(windowManager.glfwPosCallback)
268281

282+
// TODO: Can this only be done here? Why not in the plugin/glfwPlugin init loop above?
269283
for _, p := range a.config.plugins {
270284
// Extra init call for plugins that satisfy the PluginTexture interface.
271-
if glfwPlugin, ok := p.(PluginTexture); ok {
272-
err = glfwPlugin.InitPluginTexture(texturer)
285+
if texturePlugin, ok := p.(PluginTexture); ok {
286+
err = texturePlugin.InitPluginTexture(texturer)
273287
if err != nil {
274288
return errors.Wrap(err, "failed to initialize texture plugin"+fmt.Sprintf("%T", p))
275289
}
276290
}
277291
}
278292

293+
// Attach glfw window callbacks for text input
279294
a.window.SetKeyCallback(
280295
func(window *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {
281296
defaultTextinputPlugin.glfwKeyCallback(window, key, scancode, action, mods)
282297
defaultKeyeventsPlugin.sendKeyEvent(window, key, scancode, action, mods)
283298
})
284299
a.window.SetCharCallback(defaultTextinputPlugin.glfwCharCallback)
285300

301+
// Attach glfw window callback for iconification
286302
a.window.SetIconifyCallback(defaultLifecyclePlugin.glfwIconifyCallback)
287303

288-
a.window.SetCursorEnterCallback(m.glfwCursorEnterCallback)
289-
a.window.SetCursorPosCallback(m.glfwCursorPosCallback)
290-
a.window.SetMouseButtonCallback(m.glfwMouseButtonCallback)
291-
a.window.SetScrollCallback(m.glfwScrollCallback)
304+
// Attach glfw window callbacks for mouse input
305+
a.window.SetCursorEnterCallback(windowManager.glfwCursorEnterCallback)
306+
a.window.SetCursorPosCallback(windowManager.glfwCursorPosCallback)
307+
a.window.SetMouseButtonCallback(windowManager.glfwMouseButtonCallback)
308+
a.window.SetScrollCallback(windowManager.glfwScrollCallback)
309+
310+
// Shutdown the engine if we return from this function (on purpose or panic)
292311
defer a.engine.Shutdown()
293312

313+
// Handle events until the window indicates we should stop. An event may tell the window to stop, in which case
314+
// we'll exit on next iteration.
294315
for !a.window.ShouldClose() {
295316
eventLoop.WaitForEvents(func(duration float64) {
296317
glfw.WaitEventsTimeout(duration)
@@ -299,6 +320,9 @@ func (a *Application) Run() error {
299320
messenger.engineTasker.ExecuteTasks()
300321
}
301322

323+
// TODO: What if the window indicates to stop, but there are tasks left on
324+
// the queue?
325+
302326
fmt.Println("go-flutter: closing application")
303327

304328
return nil

embedder/embedder_proxy.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,13 @@ func proxy_gl_proc_resolver(userData unsafe.Pointer, procname *C.char) unsafe.Po
6161
}
6262

6363
//export proxy_gl_external_texture_frame_callback
64-
func proxy_gl_external_texture_frame_callback(userData unsafe.Pointer,
64+
func proxy_gl_external_texture_frame_callback(
65+
userData unsafe.Pointer,
6566
textureID int64,
6667
width C.size_t,
6768
height C.size_t,
68-
texture *C.FlutterOpenGLTexture) C.bool {
69+
texture *C.FlutterOpenGLTexture,
70+
) C.bool {
6971
flutterEnginePointer := *(*uintptr)(userData)
7072
flutterEngine := (*FlutterEngine)(unsafe.Pointer(flutterEnginePointer))
7173
embedderGLTexture := flutterEngine.GLExternalTextureFrameCallback(textureID, int(width), int(height))

glfw.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,24 @@ const dpPerInch = 160.0
1919
// glfwRenderer or glfwManager? All the attaching to glfw.Window must be done
2020
// during manager init in that case. Cannot be done by Application.
2121
type windowManager struct {
22-
forcedPixelRatio float64
23-
oncePrintPixelRatioLimit sync.Once
24-
pointerPhase embedder.PointerPhase
22+
// forcedPixelRatio forces the pixelRatio to given value, when value is not zero.
23+
forcedPixelRatio float64
24+
25+
// sync.Once to limit pixelRatio warning messages.
26+
oncePrintPixelRatioLimit sync.Once
27+
28+
// current pointer state
29+
pointerPhase embedder.PointerPhase
30+
pointerButton embedder.PointerButtonMouse
31+
pointerCurrentlyAdded bool
32+
33+
// caching of ppsc to avoid re-calculating every event
2534
pixelsPerScreenCoordinate float64
26-
pointerCurrentlyAdded bool
27-
pointerButton embedder.PointerButtonMouse
2835
}
2936

30-
func newWindowManager() *windowManager {
37+
func newWindowManager(forcedPixelRatio float64) *windowManager {
3138
return &windowManager{
39+
forcedPixelRatio: forcedPixelRatio,
3240
pixelsPerScreenCoordinate: 1.0,
3341
pointerPhase: embedder.PointerPhaseHover,
3442
}

option.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type config struct {
1616
windowInitializerDeprecated func(*glfw.Window) error
1717
windowIconProvider func() ([]image.Image, error)
1818
windowInitialDimensions windowDimensions
19-
windowInitialLocations windowLocations
19+
windowInitialLocation windowLocation
2020
windowDimensionLimits windowDimensionLimits
2121
windowMode windowMode
2222

@@ -31,7 +31,7 @@ type windowDimensions struct {
3131
height int
3232
}
3333

34-
type windowLocations struct {
34+
type windowLocation struct {
3535
xpos int
3636
ypos int
3737
}
@@ -102,8 +102,8 @@ func OptionVMArguments(a []string) Option {
102102
//
103103
// Deprecated, please use WindowInitialDimensions(x, y).
104104
func ApplicationWindowDimension(x, y int) Option {
105-
// deprecated on 2019-03-10
106-
fmt.Println("go-flutter: ApplicationWindowDimension is deprecated, use WindowInitialDimensions(x, y).")
105+
// deprecated on 2019-03-10, to be removed 2020-01-01
106+
fmt.Println("go-flutter: ApplicationWindowDimension (singular) is deprecated, use WindowInitialDimensions (plural).")
107107
return WindowInitialDimensions(x, y)
108108
}
109109

@@ -127,7 +127,18 @@ func WindowInitialDimensions(width, height int) Option {
127127
// WindowInitialLocations specify the startup's position of the window.
128128
// Location, in screen coordinates, of the upper-left corner of the client area
129129
// of the window.
130+
//
131+
// Deprecated, please use WindowInitialLocation(xpos, ypos).
130132
func WindowInitialLocations(xpos, ypos int) Option {
133+
// deprecated on 2019-08-18, to be removed 2020-06-01
134+
fmt.Println("go-flutter: WindowInitialLocations (plural) is deprecated, use WindowInitialLocation (singular).")
135+
return WindowInitialLocation(xpos, ypos)
136+
}
137+
138+
// WindowInitialLocation specify the startup's position of the window.
139+
// Location, in screen coordinates, of the upper-left corner of the client area
140+
// of the window.
141+
func WindowInitialLocation(xpos, ypos int) Option {
131142
if xpos < 1 {
132143
fmt.Println("go-flutter: invalid initial value for xpos location, must be 1 or greater.")
133144
os.Exit(1)
@@ -138,8 +149,8 @@ func WindowInitialLocations(xpos, ypos int) Option {
138149
}
139150

140151
return func(c *config) {
141-
c.windowInitialLocations.xpos = xpos
142-
c.windowInitialLocations.ypos = ypos
152+
c.windowInitialLocation.xpos = xpos
153+
c.windowInitialLocation.ypos = ypos
143154
}
144155
}
145156

@@ -175,7 +186,7 @@ func WindowDimensionLimits(minWidth, minHeight, maxWidth, maxHeight int) Option
175186
//
176187
// Deprecated, please use WindowIcon if you'd like to set the window icon.
177188
func OptionWindowInitializer(ini func(*glfw.Window) error) Option {
178-
// deprecated on 2019-03-05
189+
// deprecated on 2019-03-05, to be removed 2020-01-01
179190
fmt.Println("go-flutter: OptionWindowInitializer is deprecated. Please read https://is.gd/gflut_window_init_deprecated")
180191
return func(c *config) {
181192
c.windowInitializerDeprecated = ini

platform.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ var _ PluginGLFW = &platformPlugin{} // compile-time type check
3131

3232
func (p *platformPlugin) InitPlugin(messenger plugin.BinaryMessenger) error {
3333
p.messenger = messenger
34+
p.glfwTasker = tasker.New()
3435
return nil
3536
}
3637

texture-registry.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ type externalTextureHanlder struct {
2828
texture uint32
2929
}
3030

31-
func newRegistry(engine *embedder.FlutterEngine, window *glfw.Window) *TextureRegistry {
31+
func newTextureRegistry(engine *embedder.FlutterEngine, window *glfw.Window) *TextureRegistry {
3232
return &TextureRegistry{
3333
window: window,
3434
engine: engine,
@@ -41,7 +41,7 @@ func (t *TextureRegistry) init() error {
4141
// Important! Call gl.Init only under the presence of an active OpenGL context,
4242
// i.e., after MakeContextCurrent.
4343
if err := gl.Init(); err != nil {
44-
return errors.Wrap(err, "TextureRegistry gl init")
44+
return errors.Wrap(err, "TextureRegistry gl init failed")
4545
}
4646
return nil
4747
}

0 commit comments

Comments
 (0)