In this chapter, we will learn about Android RenderScript. Usually the apps on android are designed as to consume as minimum resources as possible. But some applications like some 3D games need high level processing on android.
To provide these applications high performance android introduced the RenderScript. It is android based framework which is used for running applications that perform very highly computational tasks. The development on this framework is done in Native Development Kit(NDK) provided by android. RenderScript is extremely useful for applications which performs following types of actions −
RenderScript framework is basically based on data parallel computation. It distributes your application workload on all the processors available on your device like multi-core CPUs or GPUs.
This parallel distribution of workload frees the programmer from the tension of load balancing and work scheduling. You can write more detailed and complex algorithms for your app without the worry of computational power.
To use the RenderScript Framework you must have following two things −
A kernel is a program which manages data processing instructions and manage workload on Central Processing Units.A kernel is a fundamental part of the operating system.
Similarly to run the RenderScript framework we need a written script named as Kernel to manage all the data processing requests from our app and utilize more features of the android OS provided by the NDK and as mentioned earlier that the development of RenderScript is done in the Native Development Kit of Android.
The Kernel Script is written in C-99 standard of C-language. This Standard was before the development of C++. A RenderScript kernel script file usually placed in .rs file. Each file is called as a script. A RenderScript Kernel script can contain following elements −
Sr.No | Elements & Description |
---|---|
1 |
A Language declaration It declares the version of RenderScript Kernel language used in this script. |
2 |
A package declaration This declaration names the package name of the Java class which will be affected by this Kernel Code. |
3 |
Invokable functions You can call these invokable functions from your JAVA code with arbitrary arguments. |
4 |
Script Global Variables These are just like the variables defined in C and C++ programming language. You can access these variables from your JAVA code. |
Following is the Sample Code of a Kernel −
uchar4 __convert__((kernel)) invert(uchar4 in, uint32_t x, uint32_t y) { uchar4 out = in; out.r = 255 - in.r; out.g = 255 - in.g; return out; }
If you want to use RenderScript in your API, you can do it in following two ways −
Sr.No | APIs & Description |
---|---|
1 |
android.renderscript This API is available on devices running Android 3.0 and higher. |
2 |
android.support.v8.renderscript This API is available on devices running Android 2.2 and higher. |
To android support library following tools are required −
First Open the project.properties file in your project and add following lines in the file −
renderscript.target=18 renderscript.support.mode=true sdk.buildtools=18.1.0
Now open your main class which use RenderScript and add an import for the Support Library classes as following −
import android.support.v8.renderscript.*;
Following are the purposes of above mentioned properties that we add in the project.properties file.
Sr.No | Project properties & Description |
---|---|
1 |
renderscript.target It specifies the byte code version to be generated. |
2 |
renderscript.support.mode It specifies a compatible version for the generated byte code to fall back. |
3 |
sdk.buildtools It Specifies the versions of Android SDK build tools to use. |
Now call your RenderScript Kernel functions and compute complex algorithms in your app.