coronalabs / corona

Solar2D Game Engine main repository (ex Corona SDK)
https://solar2d.com/
MIT License
2.54k stars 273 forks source link

Android: avoid main thread io and mmap the resource.car #682

Closed clang-clang-clang closed 8 months ago

clang-clang-clang commented 8 months ago

This PR removes a main thread IO to avoid ANR; allows Android to use mmap to load Lua code packages; and an additional Rtt_ASSERT correction.

Remove a main thread IO

There is a risk of ANR when loadExpansionFiles() in the main UI thread, so the main UI thread reading is changed to the main thread modifying the state, and the GLThread is used to read.

Android to use mmap to load Lua code packages

This PR allows Android to load the Lua code package (resource.car) like other platforms, using mmap, to reduce runtime memory by extracting the resource.car at first launch. It doesn't need to be uncompressed every launch, so it also positively affects startup time.

During the public beta phase, it was observed that some models showed the situation where AssetManager prematurely returns -1 when decompressing resources (approximately 0.3%). This issue was resolved by improving the accuracy, reliability, and usability of the extractAssetFile() method (at least this exception is no longer observed online). The worstcase scenario here is the same as before, when the getBytesFromFile() method failed to load the code package into memory all at once, resulting in a black screen after startup, but usually, it will not happen again when restarting the App.

Rtt_ASSERT correction

Additionally, while debugging other issues, I found an Rtt_ASSERT that could not be false. After correcting it to match the context, I guess it's not allowed to move DisplayObject from the Orphanage() group to the Orphanage() group again, that is, to removeSelf repeatedly.

Any suggestions are welcome. Thank you.

Shchvova commented 8 months ago

This is awesome. Earlier versions of Android had issues with mmap. I see that you have it in the wild and it seems to work now?

clang-clang-clang commented 8 months ago

mmap works like a charm.

Earlier versions of Android had issues with mmap.

Are there specific problem records for early versions? We have covered the API 15, which is the minimum supported by Solar2D. Since the launch of mmap, we have never observed mmap errors in the location of loading resource.car, but it may not be sufficient to reflect compatibility issues.