Skip to content

Commit 9d9d2cb

Browse files
GeertJohanpchampio
authored andcommitted
Adds comments and renames internals. Also some re-ordering of init code. (#235)
1 parent a0a832e commit 9d9d2cb

File tree

6 files changed

+87
-46
lines changed

6 files changed

+87
-46
lines changed

application.go

+48-29
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,29 +227,29 @@ 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-
235239
// Not very nice, but we can only really fix this when there's a pluggable
236240
// renderer.
237241
defaultTextinputPlugin.keyboardLayout = a.config.keyboardLayout
238242

243+
// Set the glfw window user pointer to point to the FlutterEngine so that
244+
// callback functions may obtain the FlutterEngine from the glfw window
245+
// user pointer.
239246
flutterEnginePointer := uintptr(unsafe.Pointer(a.engine))
240247
defer func() {
241248
runtime.KeepAlive(flutterEnginePointer)
242249
}()
243250
a.window.SetUserPointer(unsafe.Pointer(&flutterEnginePointer))
244251

252+
// Start the engine
245253
result := a.engine.Run(unsafe.Pointer(&flutterEnginePointer), a.config.vmArguments)
246254
if result != embedder.ResultSuccess {
247255
switch result {
@@ -255,40 +263,48 @@ func (a *Application) Run() error {
255263
os.Exit(1)
256264
}
257265

258-
defaultPlatformPlugin.glfwTasker = tasker.New()
259-
260-
m := newWindowManager()
261-
m.forcedPixelRatio = a.config.forcePixelRatio
262-
263-
m.glfwRefreshCallback(a.window)
264-
a.window.SetRefreshCallback(m.glfwRefreshCallback)
265-
a.window.SetPosCallback(m.glfwPosCallback)
266+
// Setup a new windowManager to handle windows pixel ratio's and pointer
267+
// devices.
268+
windowManager := newWindowManager(a.config.forcePixelRatio)
269+
// force first refresh
270+
windowManager.glfwRefreshCallback(a.window)
271+
// Attach glfw window callbacks for refresh and position changes
272+
a.window.SetRefreshCallback(windowManager.glfwRefreshCallback)
273+
a.window.SetPosCallback(windowManager.glfwPosCallback)
266274

275+
// TODO: Can this only be done here? Why not in the plugin/glfwPlugin init loop above?
267276
for _, p := range a.config.plugins {
268277
// Extra init call for plugins that satisfy the PluginTexture interface.
269-
if glfwPlugin, ok := p.(PluginTexture); ok {
270-
err = glfwPlugin.InitPluginTexture(texturer)
278+
if texturePlugin, ok := p.(PluginTexture); ok {
279+
err = texturePlugin.InitPluginTexture(texturer)
271280
if err != nil {
272281
return errors.Wrap(err, "failed to initialize texture plugin"+fmt.Sprintf("%T", p))
273282
}
274283
}
275284
}
276285

286+
// Attach glfw window callbacks for text input
277287
a.window.SetKeyCallback(
278288
func(window *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {
279289
defaultTextinputPlugin.glfwKeyCallback(window, key, scancode, action, mods)
280290
defaultKeyeventsPlugin.sendKeyEvent(window, key, scancode, action, mods)
281291
})
282292
a.window.SetCharCallback(defaultTextinputPlugin.glfwCharCallback)
283293

294+
// Attach glfw window callback for iconification
284295
a.window.SetIconifyCallback(defaultLifecyclePlugin.glfwIconifyCallback)
285296

286-
a.window.SetCursorEnterCallback(m.glfwCursorEnterCallback)
287-
a.window.SetCursorPosCallback(m.glfwCursorPosCallback)
288-
a.window.SetMouseButtonCallback(m.glfwMouseButtonCallback)
289-
a.window.SetScrollCallback(m.glfwScrollCallback)
297+
// Attach glfw window callbacks for mouse input
298+
a.window.SetCursorEnterCallback(windowManager.glfwCursorEnterCallback)
299+
a.window.SetCursorPosCallback(windowManager.glfwCursorPosCallback)
300+
a.window.SetMouseButtonCallback(windowManager.glfwMouseButtonCallback)
301+
a.window.SetScrollCallback(windowManager.glfwScrollCallback)
302+
303+
// Shutdown the engine if we return from this function (on purpose or panic)
290304
defer a.engine.Shutdown()
291305

306+
// Handle events until the window indicates we should stop. An event may tell the window to stop, in which case
307+
// we'll exit on next iteration.
292308
for !a.window.ShouldClose() {
293309
eventLoop.WaitForEvents(func(duration float64) {
294310
glfw.WaitEventsTimeout(duration)
@@ -297,6 +313,9 @@ func (a *Application) Run() error {
297313
messenger.engineTasker.ExecuteTasks()
298314
}
299315

316+
// TODO: What if the window indicates to stop, but there are tasks left on
317+
// the queue?
318+
300319
fmt.Println("go-flutter: closing application")
301320

302321
return nil

embedder/embedder_proxy.go

+4-2
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

+14-6
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

+18-7
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

+1
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

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ type externalTextureHanlder struct {
3232
texture uint32
3333
}
3434

35-
func newRegistry(engine *embedder.FlutterEngine, window *glfw.Window) *TextureRegistry {
35+
func newTextureRegistry(engine *embedder.FlutterEngine, window *glfw.Window) *TextureRegistry {
3636
return &TextureRegistry{
3737
window: window,
3838
engine: engine,
@@ -45,7 +45,7 @@ func (t *TextureRegistry) init() error {
4545
// Important! Call gl.Init only under the presence of an active OpenGL context,
4646
// i.e., after MakeContextCurrent.
4747
if err := gl.Init(); err != nil {
48-
return errors.Wrap(err, "TextureRegistry gl init")
48+
return errors.Wrap(err, "TextureRegistry gl init failed")
4949
}
5050
return nil
5151
}

0 commit comments

Comments
 (0)