Android RenderScript Tutorial


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 applicatons which performs following types of actions:
  • 3D Rendering
  • Image Processing
  • Computational Photography
  • Computer Vision

How RenderScript Works:

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 scheeduling. You can write more detailed and complex algorithms for your app without the worry of computational power.

How to Begin:

To use the RenderScript Framework you must have following two things:
  1. A RenderScript Kernel
  2. RenderScript APIs

A RENDERSCRIPT KERNEL

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.NoElements
1A Language declaration
It declares the version of RenderScript Kernel language used in this script.
2A package declaration 
This declaration names the package name of the Java class which will be affected by this Kernel Code.
3Invokable functions
You can call these invokable functions from your JAVA code with arbitrary arguments.
4Script Globals 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;
}

RENDERSCRIPT APIS

If you want to use RenderScrip in your API, you can do it in following two ways:
Sr.NoAPIs
1android.renderscript
This API is available on devices running Android 3.0 and higher.
2android.support.v8.renderscript
This API is available on devices running Android 2.2 and higher.
To android support library following tools are required:
  • Android SDK Tools version 22.2
  • Android SDK Build-tools version 18.1.0

HOW TO USE RENDERSCRIPT SUPPORT LIBRARY

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.NoProject properties
1renderscript.target
It specifies the bytecode version to be generated.
2renderscript.support.mode 
It specifies a compatible version for the generated byteCcode to fall back.
3sdk.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.

Comments

Popular Posts