~eliasnaur/gio

3 3

[Android] Blank Screen Issue and Android/Gio Best Practices / Common Pitfalls List

lormann michael
Details
Message ID
<CANF-HL5PG2gKvEr0TVVyx+70iWN3G4L_OsQ8XT1aWyViYHpd-w@mail.gmail.com>
DKIM signature
pass
Download raw message
Hi,
I'm having problems with the blank screen on Android.
That issue appears to have been reported on this list before
 [https://lists.sr.ht/~eliasnaur/gio/%3C98e9f30badee082d6555fc2794c3fa7f70207ae0.camel%40gmail.com%3E],
(still has no clear answer).
I have compiled the kitchen example for android and it works fine.
But my project just shows a blank screen,
probably due to the complexity of the code.
So I was wondering if anyone versed in gio/android programming would
take the time to compile a list of Best Gio/Android Practices and
Common Pitfalls.
I believe this would be very helpful to both newbies like me and the experts.
The list can always serve as a reference material for newbies and
also help reduce traffic of redundant help requests.

Thanks,
-Michael.
Details
Message ID
<1A1604A3-E2A8-4006-9448-AF8C9A80C183@gmail.com>
In-Reply-To
<CANF-HL5PG2gKvEr0TVVyx+70iWN3G4L_OsQ8XT1aWyViYHpd-w@mail.gmail.com> (view parent)
DKIM signature
pass
Download raw message
On September 1, 2020 9:24:46 PM EDT, lormann michael <michaellormann@gmail.com> wrote:
>Hi,
>I'm having problems with the blank screen on Android.
>That issue appears to have been reported on this list before
> [https://lists.sr.ht/~eliasnaur/gio/%3C98e9f30badee082d6555fc2794c3fa7f70207ae0.camel%40gmail.com%3E],
>(still has no clear answer).
>I have compiled the kitchen example for android and it works fine.
>But my project just shows a blank screen,
>probably due to the complexity of the code.
>So I was wondering if anyone versed in gio/android programming would
>take the time to compile a list of Best Gio/Android Practices and
>Common Pitfalls.
>I believe this would be very helpful to both newbies like me and the experts.
>The list can always serve as a reference material for newbies and
>also help reduce traffic of redundant help requests.


You are absolutely right that a resource like what you describe would help. I don't think I have the bandwidth to work on such a thing right now, but perhaps other community members could?

I've hit the problem that you describe by unintentionally deadlocking in my non-gio concurrent code if that helps you find your problem.

Are you able to share the problematic code?

Cheers, Chris
Gregory Pomerantz
Details
Message ID
<f7c5a533-4b73-87e6-ef8b-7f958cefa2b2@wow.st>
In-Reply-To
<CANF-HL5PG2gKvEr0TVVyx+70iWN3G4L_OsQ8XT1aWyViYHpd-w@mail.gmail.com> (view parent)
DKIM signature
pass
Download raw message
On 9/1/20 9:24 PM, lormann michael wrote:
> Hi,
> I'm having problems with the blank screen on Android.
> That issue appears to have been reported on this list before
>   [https://lists.sr.ht/~eliasnaur/gio/%3C98e9f30badee082d6555fc2794c3fa7f70207ae0.camel%40gmail.com%3E],
> (still has no clear answer).
> I have compiled the kitchen example for android and it works fine.
> But my project just shows a blank screen,
> probably due to the complexity of the code.
> So I was wondering if anyone versed in gio/android programming would
> take the time to compile a list of Best Gio/Android Practices and
> Common Pitfalls.
> I believe this would be very helpful to both newbies like me and the experts.
> The list can always serve as a reference material for newbies and
> also help reduce traffic of redundant help requests.

It is hard to know what is going on without seeing code, but in the 
earlier example there were potentially blocking calls in the startup 
path that might have been related to the blank screen. It is important 
to carefully separate blocking operations like network and file IO from 
your core UI drawing loop. Another likely cause could be a concurrency 
bug. The different platforms that Gio runs on have different threading 
models internally, so there is potential for concurrency bugs to show up 
on some platforms but not others, especially if you are using cgo.

Gio is still a moving target so it is hard to come up with a guide that 
includes specific code examples, but I would certainly recommend 
starting with the simplest code you can and building up from there. For 
a quick try at debugging, You can disable as much as possible in your 
code to get your main event loop running and at least something drawn on 
the screen. Then turn stuff on until you run into trouble. Use "adb 
logcat" (with debug messages in your code) to see where your program is 
stalling.

The Gio example apps are a good place to start learning how to structure 
Gio programs, and they are kept up to date with the latest Gio API. You 
can also look at Tailscale's Android client for something more complex. 
Here are a few useful principles:

1. app.Main() never returns, this should be the last thing in func main().

2. Do this in separate goroutines and send updates via channels to the 
main event loop.

3. use one giant select{} in your UI event loop to multiplex events from 
other parts of your program alongside the Gio events.

4. Do not do any IO or compute tasks in your main UI loop as that could 
stall your frame updates.

5. Be very careful about sending user input back into the IO and compute 
routines, as this can lead to deadlocks.


As an overall architecture I do something like this:

   func main() {
     // set up globals, make channels
     go ioTask()  // new goroutine for some IO-bound task
     go computeTask()  // new goroutine for some compute-intensive task
     go eventloop()  // start the main event loop
     app.Main()  // never returns
   }

   func eventloop() {
     // create theme, ops, and a Window
     // set up UI elements with persistent state (e.g. editors)

     // define functions for my UI elements, pages, etc. Each of these 
functions is
     // a Widget (i.e. func(Context) Dimensions)

     for {
       select {
       case x := <-myIOchannel:  // receive results from IO and compute 
tasks.
         // Call window.Invalidate() if we need to request a new frame
         // do not do any IO or compute here and do not touch the UI

       case x := <- myComputeChannel:
         // use as many channels as is convenient to your architecture.

       case e := <-w.Events():
         switch e:= e.(type) {
           // receive GIo events, read UI state (buttons, editors, etc) 
and draw the UI
           // do not do any IO or compute tasks, or anything that can block.
           // use a mutex (carefully) or write to a channel from a 
goroutine here to
           // communicate back to IO and compute goroutines.
       }
     }
   }

In the above framework, the entire application state is held in 
function-scoped variables (outside of main()). This is not the only way 
to do it. For larger apps I've seen approaches with an App struct 
holding the persistent application-wide state (e.g. the gophers example).
Details
Message ID
<CANF-HL4Mx+68q-ymT8wC5N_sGCqHVdAXQhtGdUpgL3cakMt7_g@mail.gmail.com>
In-Reply-To
<CANF-HL5PG2gKvEr0TVVyx+70iWN3G4L_OsQ8XT1aWyViYHpd-w@mail.gmail.com> (view parent)
DKIM signature
pass
Download raw message
Hi all,
I tried the App struct model suggested by Gregory in the previous post
on this topic and it worked. Blank screen problem solved.
I made all the functions in "main.go" into methods of an App struct.
Like so:

"""
package main

import (
   ...
)

type App struct {
    win    *app.Window
    field1 string
    field2 int
    ...
    ...
}

func newApp(test bool) *App {
    return &App{....}
}

func main() {
   myApp := newApp(true)
   myApp.loadConfig()
   myApp.loadTheme()
   myApp.doOtherStuff()
   myApp.win = app.NewWindow(...) // Can be defined in a different file..
   go func() {
       if err := myApp.win.Loop(); err != nil {
           log.Fatal(err)
       }
       os.Exit(0)
   }()
   app.Main()
}

func (ap *App) loadConfig() {
// Load Config
}
func (ap *App) loadTheme() {
// Load Theme
}
func (ap *App) doOtherStuff() {
// Do other stuff
}


I think this should be a recommended approach for newbies.
It makes things easier.
Thanks a bunch.
-Michael

On Wed, Sep 2, 2020 at 2:24 AM lormann michael <michaellormann@gmail.com> wrote:
>
> Hi,
> I'm having problems with the blank screen on Android.
> That issue appears to have been reported on this list before
>  [https://lists.sr.ht/~eliasnaur/gio/%3C98e9f30badee082d6555fc2794c3fa7f70207ae0.camel%40gmail.com%3E],
> (still has no clear answer).
> I have compiled the kitchen example for android and it works fine.
> But my project just shows a blank screen,
> probably due to the complexity of the code.
> So I was wondering if anyone versed in gio/android programming would
> take the time to compile a list of Best Gio/Android Practices and
> Common Pitfalls.
> I believe this would be very helpful to both newbies like me and the experts.
> The list can always serve as a reference material for newbies and
> also help reduce traffic of redundant help requests.
>
> Thanks,
> -Michael.
Export thread (mbox)