Want to make image editing fast with a few lines of code? Want to use the computing power of your phone’s GPU without the complexity of OpenCL? Well, renderscript is meant for you.

Example of blur on the right of the Image

Still not convinced of the utility of this powerful tool? Let’s speak numbers:

I compared the renderscript blur to the java based fastblur that you can find here. The image of the moutain has a resolution of 4806x3604 pixels. When processing blur on it on my Nexus 6P, it took 738ms for the renderscript. The fastblur didn’t even work (out of memory)! So I tried on a smaller image (1944x1944), and the fastblur worked in 1,354ms, so I tried again with renderscript and it took 160ms, it’s more than 8 times faster.

You can find below a comparison of the java’s an renderscript’s performances on the Gaussian Blur:

Blur performances (taken from Jonathan’s article on degree53)

I won’t talk here about NDK as I don’t have enough knowledge, but you can find a renderscript vs NDK comparison here. I didn’t go further in this direction as it was hard to set up and would not work for every phone unlike renderscript.

Renderscript is based on C99 (Ed. C language), so you need to be familiar with that language. It shouldn’t be hard to know the basics if you already know java.

First of all, you need to add those two bold lines in your build.gradle file :

android {

compileSdkVersion 23

buildToolsVersion "23.0.3"



defaultConfig {

minSdkVersion 8

targetSdkVersion 19



renderscriptTargetApi 18

renderscriptSupportModeEnabled true



}

}

If your app is minSDK 16 and lower, you should use support mode as a lot of methods were added since API 17.

The renderscriptTargetApi goes up to 23, but you should set it to the lowest API level able to provide all the functionality you are using in the scripts. If you want to target API 21+ with support mode you have to use gradle-plugin 2.1.0 and buildToolsVersion “23.0.3” or above.

Renderscript will use scripts written in C which will parallelize calculations for each pixel of your image. A script is a file with ‘.rs’ extension that must be placed in app/src/main/rs. Android Studio won’t generate this folder or any script file for you.

To illustrate this article, I’ve made a sample app that computes histogram equalization on the Y channel of the YUV colorspace (see picture below), and that blurs the image. It’s available on github.