Android - RenderScript

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 −
3D Rendering
Image Processing
Computational Photography
Computer Vision
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 RenderScript Kernel
RenderScript APIs
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 −
A Language declaration
It declares the version of RenderScript Kernel language used in this script.
A package declaration
This declaration names the package name of the Java class which will be affected by this Kernel Code.
Invokable functions
You can call these invokable functions from your JAVA code with arbitrary arguments.
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 −
android.renderscript
This API is available on devices running Android 3.0 and higher.
android.support.v8.renderscript
This API is available on devices running Android 2.2 and higher.
To android support library following tools are required −
Latest Android SDK Tools version
Latest Android SDK Build-tools version
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.
renderscript.target
It specifies the byte code version to be generated.
renderscript.support.mode
It specifies a compatible version for the generated byte code to fall back.
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.

    46 Lectures 
     7.5 hours 

 Aditya Dua

    32 Lectures 
     3.5 hours 

 Sharad Kumar

    9 Lectures 
     1 hours 

 Abhilash Nelson

    14 Lectures 
     1.5 hours 

 Abhilash Nelson

    15 Lectures 
     1.5 hours 

 Abhilash Nelson

    10 Lectures 
     1 hours 

 Abhilash Nelson
 Print
 Add Notes
 Bookmark this page