r/android_devs • u/[deleted] • Jul 18 '20
Discussion Realtime blurring in Android
I am currently in a project where some design elements should have blurred background that updates as the user scrolls on a background with multiple images. I know Android generally doesn't play well with blurring and that its OK for static content but I expected it do be in a better state today as the phones itself are more than capable to handle it.
The implementation was done with some random library I found on GitHub, and it uses the classic Renderscript to make it work. The effect even worked very well on my OnePlus and the designer was very happy that we could be as close to the design as the iOS counterpart.
However, I took another phone one day to test stuff (Samsung Galaxy S8+) and the performance was horrible. Scrolling was laggy and choppy and we had to use fallback graphics because the user experience was really bad. I was really surprised that a device that was the flagship 2-3 years ago can't handle realtime blur.
I'm guessing the issue lies within Android since the device itself should be able to handle it. The developer of the lib did benchmarks on Nexus 5 and it could maintain 60 fps without issues.
My question is, is there a specific reason why Android still doesn't have native blur support (like imageView.blur(radius, sampling)? I guess it should be done in C or C++ since its a lot of calculations, but I can't imagine Google not having the capacity to find a way... Can anyone share their experiences with blurring? Maybe some hacks/libs that worked really well?
1
Jul 18 '20
Renderscript uses the best the hardware has to offer. If you can't maintain 60 fps (are you forgetting that Android supports 144Hz and more?) with your realtime animations, maybe your design needs to rethought.
Moving to NDK or adding yet another API will not magically improve performance, at best you could reduce latency.
2
Jul 20 '20
All libraries I have been using needed renderscript properties set in build.gradle to work, so I guess they did use it in the background. Still, its hard to explain why an iPhone can have realtime blur and Android can't. One library even had the option to calculate blur only once (for static content) but it still lagged when the entire page (blur + bg) was scrolling. Without the blur everything is super smooth so the problem should not be in the rest of the design or that something might be hogging the main thread.
3
Jul 20 '20
till, its hard to explain why an iPhone can have realtime blur and Android can't.
No it isn't. For starters, ALL iOS devices are high-end CPU+GPU combo. Besides that, Apple designs its own chips and once it decided that Blurs were part of the look of the OS, all chips have a dedicated pipeline to blur in realtime (MPSImageLaplacian + MPSImageStatisticsMeanAndVariance).
Androids are just generic Arm-A devices with no assurance of hardware presence NOR performance. Look at iOS history and you'll find device that didn't get the upgrade to modern iOS... can't do blurs properly.
2
Jul 20 '20 edited Jul 21 '20
True, now to think about it, the devices where blur did work well were Snapdragons, the S8+ has Exynos. I guess the problem lies in the Exynos chip because Snapdragon did not lag. EDIT: The other device it lagged on was a Huawei with a Kirin chip...
1
Jul 20 '20
Extra thoughts:
Android's renderscript is actually Gaussian blur, a.k.a. the best kind of natural blur.
1
u/SgtDirtyMike Jul 21 '20
I don't personally have an Exynos device to test with, but another thing you could do as a sanity check is try creating a demo project and using a blur widget with Flutter. It can blur images and create effects like UIVisualEffectView on iOS, and I've found it to be as performant as it gets on Android for that kind of thing. I realize that mentioning it is probably is blasphemous on this forum, but the fact of the matter is, Flutter has support for things that are lacking in the current Android developer experience, and those things generally perform quite well on Android devices.
1
u/ZieIony Jul 25 '20
Blur is a quite memory-access-heavy operation, since for each destination pixel it needs to get all surrounding source pixels first. Then a couple of adds/divs and another write to the destination pixel. It may be not only about computing power, but also fillrate, available hardware operations and pipeline structure. ScriptIntrisincBlur is a Gaussian blur - nice, but expensive. The fastest option would be a two pass linear blur with gather operations and/or rescaling. If there was a way to draw that directly to hardware canvas instead of copying pixels to JVM and back, that would save two memory operations per pixel and a lot of synchronization waiting. As far as I know, iOS also uses a simple trick that gives more priority to animation operations (if you scroll a list, it scrolls smoothly, but everything else lags).
8
u/DJDarkByte Jul 18 '20
Try this lib: https://github.com/CameraKit/blurkit-android
I had to do some blurring once and this is by far the best blur tool I found. All the others I tried have shit performance. If you can't get it to work with this you'd have to do some real intricate shit to get the result you want, which isn't worth it if you ask me.