Guide to using RenderScript on Intel architecture running the Android OS

RenderScript is a scripting language on Android. You can use it to write high performance graphics rendering and raw computational code.

The RenderScript API includes high performance functions for 2D/3D rendering and mathematical calculations. It allows you to describe a task with the same type of independent calculations over a large volume of data and divide it into similar subtasks that can be executed quickly and in parallel on multi-core processor platforms running Android.

This technology can improve the performance of a number of Dalvik applications related to image processing, image recognition, physical models, etc., which in turn stay machine independent.

About the RenderScript technology within Android

RenderScript offline compilation

RenderScript first appeared in Honeycomb / Android 3.0 (in API 11). In the Android SDK directory platform tools you can find llvm-rs-cc (offline compiler) to compile RenderScript (*.rs file) into byte code (*.bc file) and generate Java* classes of objects (*.java files) for structures, global variables within RenderScript and RenderScript itself. llvm-rs-cc is based on the Clang compiler with a few changes for Android, which is a front-end for the LLVM compiler. This picture describes how offline compilation works.

RenderScript run-time compilation

The new Android framework is built on the LLVM back-end and is responsible for run-time compiling of byte code, linking with the right libraries, and launching and controlling RenderScript. This framework consists of the following components: libbcc engaged in LLVM context initialization, parsing pragmas and other metadata in the byte code, bytecode’s compilation and dynamic linking with the needed libraries from libRS. libRS contains libraries (math, time, drawing, ref counting, ...), structures, and data types (Script, Type, Element, Allocation, Mesh, various matrices, ...). The picture below shows runtime compilation.

Advantages of RenderScript:

The compiler produces machine-independent applications because the RenderScript byte code included in the apk file at run time will be compiled to native code for the CPU of the platform where it will be launched.

High speed execution is achieved due to parallelization of computing, run-time compiler optimization, and native code execution.

Disadvantages:

A lack of detailed documentation for RenderScript complicates application development. Limited runtime documentation for RenderScript run-time API is shown here. However, you can find a detailed "Getting Started" here.

There is no support to execute RenderScript threads on a GPU or DSP. You may encounter problems with the run-time balancing of threads in heterogeneous runs and using shared memory.

As you can see the code has a simple loop with independent iterations processing a stream of pixels.

For the experiment, we ran our code on a Lenovo K900 with an Intel® Atom™ processor and a 600x1024 picture of a LEGO* monster carrying Christmas gifts.

Here is an example of a function RS_MonoChromeFilter (the RenderScript implementation was taken from the Android SDK licensed under the Apache 2.0 license) to convert an RGB-color image to monochrome image:

RenderScript’s performance can be improved by using more aggressive optimizations for floating-point operations. In the next code snippet, we added the rs_fp_imprecise pragma to RenderScript to speed up the calculations without losing any quality in the picture.

01

//mono.rs

02

//or our small RenderScript

03

#pragma version(1)

04

#pragma rs java_package_name(com.example.hellocompute)

05

#pragma rs_fp_imprecise

06

//multipliers to convert a RGB colors to black and white

07

const static float3 gMonoMult = {0.299f, 0.587f, 0.114f};

08

09

void root(const uchar4 *v_in, uchar4 *v_out) {

10

//unpack a color to a float4

11

float4 f4 = rsUnpackColor8888(*v_in);

12

//take the dot product of the color and the multiplier

13

float3 mono = dot(f4.rgb, gMonoMult);

14

//repack the float to a color

15

*v_out = rsPackColorTo8888(mono);

16

}

The optimized code produces the same quality monochrome image, one without artifacts and distortion. There is no mechanism for explicit control of run-time compiler optimization RenderScript, in contrast to the NDK, because compiler flags were previously declared inside Android for each platform (x86, ARM, etc.).

About the Authors

Stanislav Pavlov works in the Software & Service Group at Intel Corporation. He has 10+ years of experience in software development. His main interest is optimization of performance, power consumption, and parallel programming. In his current role as an Application Engineer providing technical support for Intel®-based devices, Stanislav works closely with software developers and SoC architects to help them achieve the best possible performance on Intel platforms. Stanislav holds a Master's degree in Mathematical Economics from the National Research University Higher School of Economics. He is currently pursuing an MBA in the Moscow Business School.

Roman Kazantsev, the co-author of this blog, is also a Senior Software Engineer in the Software & Service Group and works on encryption projects. He holds a Master’s degree in Computer Science from Nizhniy Novgorod State University where he is currently pursuing his PhD.

Digit caters to the largest community of tech buyers, users and enthusiasts in India. The all new Digit.in continues the legacy of Thinkdigit.com as one of the largest portals in India committed to technology users and buyers. Digit is also one of the most trusted names when it comes to technology reviews and buying advice and is home to the Digit Test Lab, India's most proficient center for testing and reviewing technology products.