Flowit-Game / Flowit

Minimalistic puzzle game
GNU General Public License v3.0
77 stars 7 forks source link

Fix blank screen issue [#18] #26

Closed ywnico closed 1 year ago

ywnico commented 1 year ago

Fix #18, in which switching apps or turning the screen off and on results in a blank screen.

When Flowit launches, onSurfaceChanged is called once. But when it resumes, onSurfaceChanged is called twice. The loaded matrix previously was not reset between calls, meaning that the second onSurfaceChanged would mess up all the coordinates. Adding gl.glLoadIdentity() in onSurfaceChanged fixes the issue.

On my phone, if I unlock using pin instead of fingerprint sensor, the initial dimensions of the created surface change. Therefore, to fix the graphics appearing in a slightly incorrect position, another gl.glLoadIdentity() in onSurfaceCreated is applied. (This one I don't really understand, so I'm not sure if there's a better fix, but it seems to work in my testing).

I think these two changes resolve the blank screen problem, but I'm not an expert on OpenGL and there may be some other edge cases that it doesn't address.

ywnico commented 1 year ago

Actually, there is some vertical stretching of the graphics sometimes when I lock screen and unlock. I will investigate a bit more.

ywnico commented 1 year ago

Although adding gl.glLoadIdentity() on the projection matrix in onSurfaceChanged solves the blank screen issue, it uncovers a second bug that I have not been able to solve.

If I change apps or lock and unlock the screen with the fingerprint reader (I'm on Galaxy S9 with Lineage 18.1 [Android 11]), there is no problem, and the app works as expected. But if I lock and unlock using pin code instead of fingerprint, then the interface sometimes becomes stretched vertically.

Normal Stretched

From debugging, I have determined that the available screen size on my phone (screen size minus the bottom navigation bar) is 1440 x 2792. These are the dimensions that are normally passed to onSurfaceChanged (which is always called twice on resume). However, in the case that I unlock the phone with the pin, the first onSurfaceChanged call is given dimensions of 1440 x 2960 (the size of my screen without the soft navigation bar) and the second call has the expected 1440 x 2792. Thus, it seems that perhaps the game is stretching a rendered 1440 x 2792 screen to 1440 x 2960, and placing it above the navigation bar, leading to the stretching and clipping effect.

What's strange is that even though unlocking with pin always first calls onSurfaceChanged with 1440 x 2960 and then with 1440 x 2792, the graphics are stretched only sometimes (approx 10%-20% of the times I lock and unlock). I have not been able to find a deterministic source; I just unlock and relock many times (without interacting with the app during that time) and eventually the bug will show up.

I thought maybe the GLSurfaceView itself was stretched, but it always reports its size [from glSurfaceView.getWidth(), glSurfaceView.getHeight() or glSurfaceView.getMeasuredWidth(), glSurfaceView.getMeasuredHeight()] normally as 1440 x 2792 and the reported position from glSurfaceView.getX() and glSurfaceView.getY() does not change.

So, I'm not sure if this is a bug related to OpenGL or related to the GLSurfaceView and layout (e.g., how fullscreen is set).

I will keep trying to figure it out, but I wanted to document the situation. Sorry for submitting the PR prematurely.