Blurring images on Android
One of the most underused Android feature is probably RenderScript (do you even remember what it is?).
As a reminder, RenderScript is essentially Google’s version of OpenCL in that it allows you to write small general-purpose piece of code (called kernel) that executes on the GPU.
As such, it’s very efficient when doing parallelizable work on large data-set. The main hindrance as with any GPGPU scenario is that to interoperate with the CPU, data needs to be copied back and forth between RAM and GPU memory.
One of the main area where RenderScript is useful is with images and pixel-oriented algorithms. Since with recent Android version
View instances are hardware-accelerated by default, showing up these GPU-processed images on screen is efficient as they don’t even need to be piggy backed to main memory.
Out of the box, Android ships with a couple of pre-made kernels, mainly picture-related. In that sense it can be considered equivalent to iOS CoreGraphics albeit much less rich in available filters.
One of those default script,
ScriptIntrinsicBlur, allows you to blur the content of an image very easily:
The code will certainly look familiar to those of you who have already used OpenCL. You basically create a context (
RenderScript), setup typed buffers (
Allocation), create the kernel (
ScriptIntrinsicBlur), define its entry point (
SetInput) and execute it (
Below is the result in a small app to select different blur radius:
You can read up Android basic guide on RenderScript for a more in-depth explanation of each step.
Blur effects are becoming more and more trendy these days, being one of the design focus of iOS 7 and also used extensively by apps such as Rdio. With RenderScript you can now cheaply use that trick in your Android app too.