kivy / python-for-android

Turn your Python application into an Android APK
https://python-for-android.readthedocs.io
MIT License
8.1k stars 1.81k forks source link

Possible SDL2 issues introduced with P4A 2019.06.06 #2002

Closed jere-botes closed 4 years ago

jere-botes commented 4 years ago

Kivy is an amazing framework and it is great that Kivy and P4A is moving forward and keeping up with the times. Unfortunately, I have been experiencing a few issues since moving from the “trusty and very stable P4A v0.6” to P4A 2019.06.06 and later.

My suspicion is that the issues might be related to SDL2 being upgraded from 2.0.4 to 2.0.9, as this seems to be the major variable that was changed from P4A 2019.06.06 onwards.

1. Severe drop in frame rate on android.

I have an app that requires moving around appx. 30-40 widgets at a fast pace. Previously (with P4A v0.6 & SDL2.0.4), I achieved 60fps without a hitch, but since the upgrade, building an apk using the exact same code the frame rate has dropped to less than 20fps.

Could this be SDL2 related, or something else?

2. App crashes on exit on android.

In the past I used one of two methods to gracefully exit an app:

Method 1

App.get_running_app().stop() Window.close()

Method 2

import stopTouchApp kivy.base.stopTouchApp stopTouchApp()

Since P4A 2019.06.06 and later releases, the app crashes on exit using these methods or causes and ANR on android (Tested on android 5.1 and android 9 – happens about 70% of the time.)

For the time being, I managed to circumvent the issue by running the exit code on UI thread eg: (It still crashes on exit sometimes, but much less frequently) Anyone with an idea why this happens?

3. Screen irregularities on android on_start and on_pause.

Using P4A 2019.06.06 or later, I have been noticing some issues with the Window / Screen on android device. The android status bar is shown on the splash screen while the app is loading – previously this was hidden. But, more importantly there intermittently appear a 'space' or 'black bar' at the top of the screen when on_resume is called causing the screen to ‘hop’ down and up, before being restored to normal. (Previously such screen transitions happened very smoothly, and was imperceptible.)

I created a minimal kivy app example to clearly demonstrate these issues on android. Please see built environment comparison below:

Test APK: KivyBench1 (No issues) Build environment:

Test APK: KivyBench2 (Issues 1-3 as described previously) Build environment:

Both apks are built with the exact same code, but if you run a performance test with KivyBench1 (built with P4A 0.6) with 100 widgets a frame rate of 60fps is achieved. Whereas, if you run KivyBench2 (built with P4A 2019.07.08) with 100 widgets, the frame rate is way down to 15 fps. (Tested with Android 9 device: Galaxy Note 9)

I also tried variations to build KivyBench2 for example changing Kivy version, NDK versions and all new P4A versions (2019.xx.xx), but the slow performance & other issues remain present. It looks like the issues is caused by the later versions of P4A, possibly SDL2 related… You will also notice the app crashes on exit with KivyBench2.

I’m attaching the app source code & sample apk packages etc, if anyone would like to build the apks to see if they get the same results.

LINK: https://github.com/jere-botes/KivyBench/

inclement commented 4 years ago

Thanks for the detailed report. I'll try to look into this. @jtc0de have you seen anything like this during the SDL2 testing?

ghost commented 4 years ago

Hmmm I vaguely remember there being performance issues in between but I can't tell off the top of my head if that was ever in the specific version we use. (Since I'm usually ahead of p4a master anyway with the SDL2 version I use) The crash sounds like kivy maybe is accessing some UI things in a non main thread on shutdown?

As for the status bar, there definitely have been multiple issues with SDL2 with this I've witnessed on the SDL2 bugtracker, so if we simply bump up the SDL2 version this might easily go away. If not, then this and the perf issue most likely need to be addressed upstream. SDL2 has been quite responsive to android bugs recently, so it's all a matter of seeing if the latest version still has this and if it does, report it

jere-botes commented 4 years ago

Thanks for looking into this. I wish I could help, but unfortunately this is beyond my skill set… The performance drop is quite severe in recent P4A versions. Even moving around as little as 15 widgets on screen, there is a perceptible drop in frame rate. This would badly affect any app that relies on minor animations etc.

Not sure if it will help, but here are some more info regarding my troubleshooting efforts: I did tests building with arm64-v8a arch, hoping this might improve things, but has no effect. The mentioned issues are all present in P4A 2019.06.06, 2019.07.08, 2019.08.06, but it seems there is a slight improvement in performance with P4A 2019.10.06 and the crashes / ANR's on exit are also less frequent.

jere-botes commented 4 years ago

I think I have made some progress in my pursuit to pinpoint where the performance issues come from...

I was able to build an apk version of ‘KivyBench’ using P4A_2019.08.09 but forced it to package with Kivy 1.10.1 instead of Kivy 1.11 as is the default with recent P4A releases. I am happy to report that this gets rid of the very slow performance issue. Using 100 widgets again as an experiment appx 60 fps is achieved. (Using Kivy 1.11, achieves only +-15fps)

It seems however, that the recent P4A versions does not play nice with the older Kivy 1.10.1, as this brings about other issues, for example: screen orientation issues where even if you specify portrait, the app still opens in landscape. Anyone with an idea how this screen orientation can be fixed?

As for the performance issue, if looks like the issue lies with Kivy 1.11 and not P4A or SDL2 as I originally thought. Has anyone perhaps tried to test / recreate the performance issues mentioned in this thread?

inclement commented 4 years ago

Very interesting, thanks. If I have time I'll do some Kivy version comparisons. It ought to be possible to git bisect the slowdown, although it's inconvenient if it's necessary to build for android each time.

inclement commented 4 years ago

Anyone with an idea how this screen orientation can be fixed?

There were some specific commits, some by me, to make Kivy fully compatible with the newer SDL version. If you can find those commits, you can see what they changed.

quitegreensky commented 4 years ago

Yeah. The problem lies with current version of kivy. version 1.10.1 works fine! I'm facing with same issue

jere-botes commented 4 years ago

Just a quick update from my side…

Issue 1: Slow fps Using the old Kivy 1.10.1 with P4A 2019.08.09 gets rid of the slow fps issues, but causes a problem with the screen orientation on android. If it helps anyone, the screen orientation can be overridden in the SDLactivity.java file in the setOrientationBis() class – because the screen orientation setting in buildozer.spec file does not work anymore.

I don’t have any experience with java, but comparing the SDLacticity.java file from the stable P4A v0.6 with the SDLactivity.java file from the latest P4A 2019.08.09, I think this is also where the other two problems mentioned earlier in this thread is coming from…

Issue 2: Crashes on exit The on_destroy method in SDLacticity.java has been rewritten in the latest P4A – Could there be an issue in the new java code that causes the crashes on exit?

Issue 3: Screen irregularities on android ‘sticky immersive’ mode It also seems that there was a lot of changes made to SDLacticity.java with regard to the on_start, on_resume methods. Could it be that the rewritten methods are causing the screen issues on android immersive mode for example the status bar is shown intermittently on_start & on_resume causing the screen to ‘hop’ down & up and flash etc. Previously, the status bar was completely hidden and never ‘popped-up’ during screen transitions…

jere-botes commented 4 years ago

My 'issue 3', appears to be related to this thread: https://github.com/kivy/python-for-android/issues/1117

The SYSTEM_UI_FLAG_FULLSCREEN flag in setSystemUiVisibility() is required to hide the status bar in android's 'sticky_immersive' mode. Although this is set in the onSystemUiVisibilityChange() class in SDLactivity.java, perhaps there is a problem with the implementation here that causes this issue...

quitegreensky commented 4 years ago

@inclement @jere-botes
Here is my update: I did some version comparison and found that the fps regression issue comes from #4219. Every version before this commit works fine .

inclement commented 4 years ago

@quitegreensky Great work, thanks! I'll look at getting that revert in Kivy ASAP.

yunus-ceyhan commented 4 years ago

I emphasized this issue few monts ago on Kivy's discord channel but nobody beleved me even though i recorded a video to prove it. I knew soon or later every kivy developer would face that issue on android. I'm looking forward to a solution to be released regarding it which has made me stop developing apps with Kivy since then.

quitegreensky commented 4 years ago

@yunus-ceyhan Me too. I have opened a lot of issues in different places and it seemed no one is facing with this issue! But that was a BIG bug indeed so i tried to find the problem on my own and finaly i did . I solved this problem in my Kivy fork. You can use my fork for now.

yunus-ceyhan commented 4 years ago

@yunus-ceyhan Me too. I have opened a lot of issues in different places and it seemed no one is facing with this issue! But that was a BIG bug indeed so i tried to find the problem on my own and finaly i did . I solved this problem in my Kivy fork. You can use my fork for now.

thank you so much but i got that error.

https://paste.ubuntu.com/p/wjw2VjnwCB/

quitegreensky commented 4 years ago

@yunus-ceyhan Try cleaning your .buildozer folder byBuildozer android clean in path first, then add kivy==5ed7a1335bd66c4365b666eccce049f27ebfc51b to your buildozer.spec requirement and build your package again. keep me updated.

jere-botes commented 4 years ago

@quitegreensky Thanks, I will also try out your fork... At the moment I build apks using the old kivy 1.10.1, but this is not ideal as it brings about other issues.

Any of you perhaps also having issues with screen showing the android taskbar & flashing on_pause / on_resume with latest p4a?

On Fri, 01 Nov 2019, 17:35 quitegreensky, notifications@github.com wrote:

@yunus-ceyhan https://github.com/yunus-ceyhan Try cleaning your .buildozer folder byBuildozer android clean in path first, then add kivy==5ed7a1335bd66c4365b666eccce049f27ebfc51b to your buildozer.spec requirement and build your package again. keep me updated.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/kivy/python-for-android/issues/2002?email_source=notifications&email_token=ANPFOJ6O7HOA4LFBXFQTW2DQRRECTA5CNFSM4JAHYP4KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEC3ITBQ#issuecomment-548833670, or unsubscribe https://github.com/notifications/unsubscribe-auth/ANPFOJ66VFALHDFAECR52ZLQRRECTANCNFSM4JAHYP4A .

quitegreensky commented 4 years ago

Any of you perhaps also having issues with screen showing the android taskbar & flashing on_pause / on_resume with latest p4a?

I think these kind of issues are not about p4a. This is because you are using an old version of kivy (like screen orientation issue you mentioned before)

yunus-ceyhan commented 4 years ago

@quitegreensky wow it's like a magic. thank you so so much. :) i dont care about kivy's new releases, i'll use your fix for a long time. thanks again.

jere-botes commented 4 years ago

Thx, I already tested this with kivy 1.11, but unfortunately the issue still persists... The task bar flashes for about a second when on_resume is called. This causes the kivy window size to change on android immersive mode and causes display problems.

On Fri, 01 Nov 2019, 18:00 quitegreensky, notifications@github.com wrote:

Any of you perhaps also having issues with screen showing the android taskbar & flashing on_pause / on_resume with latest p4a?

I think these kind of issues are not about p4a. This is because you are using an old version of kivy (like screen orientation issue you mentioned before)

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/kivy/python-for-android/issues/2002?email_source=notifications&email_token=ANPFOJZFDBRL7CRU26S2RUTQRRHBJA5CNFSM4JAHYP4KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEC3K6LA#issuecomment-548843308, or unsubscribe https://github.com/notifications/unsubscribe-auth/ANPFOJ6DRQCRY2UP6IWOJC3QRRHBJANCNFSM4JAHYP4A .

quitegreensky commented 4 years ago

@jere-botes Well i guess you better open a new issue and put your codes there and tell which versions you are exactly using . Can you please try my fix exactly as i described?

@yunus-ceyhan Try cleaning your .buildozer folder byBuildozer android clean in path first, then add kivy==5ed7a1335bd66c4365b666eccce049f27ebfc51b to your buildozer.spec requirement and build your package again. keep me updated.

yunus-ceyhan commented 4 years ago

@jere-botes for orientation issue on 1.10.1 add this code at top of your main.py

import os os.environ['SDL_IOS_ORIENTATIONS'] = "Landscape"

inclement commented 4 years ago

Should be fixed by https://github.com/kivy/kivy/pull/6578, which I believe should be in the next kivy release

inclement commented 4 years ago

Thanks all for the debugging and discussion on this issue.

Whatnoww commented 4 years ago

@yunus-ceyhan Try cleaning your .buildozer folder byBuildozer android clean in path first, then add kivy==5ed7a1335bd66c4365b666eccce049f27ebfc51b to your buildozer.spec requirement and build your package again. keep me updated.

Out of curiosity, is this applied to the application requirements, or elsewhere?

quitegreensky commented 4 years ago

@yunus-ceyhan Try cleaning your .buildozer folder byBuildozer android clean in path first, then add kivy==5ed7a1335bd66c4365b666eccce049f27ebfc51b to your buildozer.spec requirement and build your package again. keep me updated.

Out of curiosity, is this applied to the application requirements, or elsewhere?

Yes , applied to requirements. This issue is fixed in version 1.11.1 so add kivy==1.11.1 to requirements

Whatnoww commented 4 years ago

@yunus-ceyhan Try cleaning your .buildozer folder byBuildozer android clean in path first, then add kivy==5ed7a1335bd66c4365b666eccce049f27ebfc51b to your buildozer.spec requirement and build your package again. keep me updated.

Out of curiosity, is this applied to the application requirements, or elsewhere?

Yes , applied to requirements. This issue is fixed in version 1.11.1 so add kivy==1.11.1 to requirements

Strange. Using 1.11.1 doesn't seem to work as well as the other one in terms of fps. The other one was hitting 60fps interestingly enough. Maybe there's still some work to be done?

mkc2019 commented 4 years ago

@Whatnoww Me neither. Although the bug has been fixed in the master branch of kivy 1.11.1, they probably haven't pushed the change to PyPI. So I guess we have to stick with the fork for now.

AM-ash-OR-AM-I commented 3 years ago

This issue is closed however no-one seem to have found way to fix last issue that is app crashing on exit is there a solution?

inclement commented 3 years ago

@AM-ash-OR-AM-I Please feel free to open a new issue about the app crashing on exit.

AM-ash-OR-AM-I commented 3 years ago

Alright!

AM-ash-OR-AM-I commented 3 years ago

@inclement BTW I'm using KivyMD so what do you suggest should I open issue here or in KivyMD?

inclement commented 3 years ago

@AM-ash-OR-AM-I Try to reproduce it using a pure kivy app. If it happens there, open it on the kivy repository.

AM-ash-OR-AM-I commented 3 years ago

@yunus-ceyhan Try cleaning your .buildozer folder byBuildozer android clean in path first, then add kivy==5ed7a1335bd66c4365b666eccce049f27ebfc51b to your buildozer.spec requirement and build your package again. keep me updated.

Out of curiosity, is this applied to the application requirements, or elsewhere?

Yes , applied to requirements. This issue is fixed in version 1.11.1 so add kivy==1.11.1 to requirements

@quitegreensky Ur version works great on mid-range smartphones Snapdragon 600 series and up but in case of Low end devices it still has frame drops... Can u fix it? I was using kivy==5ed7a1335bd66c4365b666eccce049f27ebfc51b exactly as instructed. Btw, its Kivy Version is 2.0.0rc1 right?

olek5andr commented 3 years ago

@yunus-ceyhan Try cleaning your .buildozer folder byBuildozer android clean in path first, then add kivy==5ed7a1335bd66c4365b666eccce049f27ebfc51b to your buildozer.spec requirement and build your package again. keep me updated.

Hi, I have the same issue of extremely low fps on android, but after cleaning buildozer and using your branch, the KivyMD causes app to crash: ValueError: MDRaisedButton._md_bg_color has an invalid format (got [0.0, 0.5882352941176471, 0.5333333333333333, 1]). Here are the requirements I used: requirements = python3,kivy==5ed7a1335bd66c4365b666eccce049f27ebfc51b,git+https://github.com/kivymd/KivyMD.git@master,sdl2_ttf==2.0.15,python-dateutil,requests,urllib3,chardet,idna,android,plyer,jnius,oscpy,pillow

quitegreensky commented 3 years ago

@yunus-ceyhan Try cleaning your .buildozer folder byBuildozer android clean in path first, then add kivy==5ed7a1335bd66c4365b666eccce049f27ebfc51b to your buildozer.spec requirement and build your package again. keep me updated.

Hi, I have the same issue of extremely low fps on android, but after cleaning buildozer and using your branch, the KivyMD causes app to crash: ValueError: MDRaisedButton._md_bg_color has an invalid format (got [0.0, 0.5882352941176471, 0.5333333333333333, 1]). Here are the requirements I used: requirements = python3,kivy==5ed7a1335bd66c4365b666eccce049f27ebfc51b,git+https://github.com/kivymd/KivyMD.git@master,sdl2_ttf==2.0.15,python-dateutil,requests,urllib3,chardet,idna,android,plyer,jnius,oscpy,pillow

There is no seed to use that branch anymore. the bug is already fixed in 2.0.0 .

olek5andr commented 3 years ago

There is no seed to use that branch anymore. the bug is already fixed in 2.0.0 .

Oh, ok. The thing is I've been using kivy 2.0.0 before I've decided to try your branch as a solution to the low fps issues. (It is extremely laggy) Here are my original requirements = python3,kivy==2.0.0,git+https://github.com/kivymd/KivyMD.git@master,sdl2_ttf==2.0.15,python-dateutil,requests,urllib3,chardet,idna,android,plyer,jnius,oscpy,pillow Any suggestion what the problem might be? (Device is Android 9)

woohoufeng commented 2 years ago

@yunus-ceyhan Try cleaning your .buildozer folder byBuildozer android clean in path first, then add kivy==5ed7a1335bd66c4365b666eccce049f27ebfc51b to your buildozer.spec requirement and build your package again. keep me updated.

Hi, I have the same issue of extremely low fps on android, but after cleaning buildozer and using your branch, the KivyMD causes app to crash: ValueError: MDRaisedButton._md_bg_color has an invalid format (got [0.0, 0.5882352941176471, 0.5333333333333333, 1]). Here are the requirements I used: requirements = python3,kivy==5ed7a1335bd66c4365b666eccce049f27ebfc51b,git+https://github.com/kivymd/KivyMD.git@master,sdl2_ttf==2.0.15,python-dateutil,requests,urllib3,chardet,idna,android,plyer,jnius,oscpy,pillow

Does anybody know what to do if I got this error Valueerror: MDRaisedButton._md_bg_color has an invalid format (got [0.12941176470588237, 0.5882352941176471, 0.9529411764705882, 1]) in my ubuntu buildozer. Please help me.

akshayaurora commented 2 years ago

This seems to have become a hot bed for unrelated issue posts. Closing all comments on this now as original issue has been fixed and merged upstream.