page.title=RenderScript parent.title=Computation parent.link=index.html @jd:body

In this document

  1. Writing a RenderScript Kernel
  2. Accessing RenderScript APIs
    1. Setting Up Your Development Environment
  3. Using RenderScript from Java Code

Related Samples

  1. Hello Compute

RenderScript is a framework for running computationally intensive tasks at high performance on Android. RenderScript is primarily oriented for use with data-parallel computation, although serial computationally intensive workloads can benefit as well. The RenderScript runtime will parallelize work across all processors available on a device, such as multi-core CPUs, GPUs, or DSPs, allowing you to focus on expressing algorithms rather than scheduling work or load balancing. RenderScript is especially useful for applications performing image processing, computational photography, or computer vision.

To begin with RenderScript, there are two main concepts you should understand:

Writing a RenderScript Kernel

A RenderScript kernel typically resides in a .rs file in the <project_root>/src/ directory; each .rs file is called a script. Every script contains its own set of kernels, functions, and variables. A script can contain:

Setting floating point precision

You can control the required level of floating point precision in a script. This is useful if full IEEE 754-2008 standard (used by default) is not required. The following pragmas can set a different level of floating point precision:

Most applications can use rs_fp_relaxed without any side effects. This may be very beneficial on some architectures due to additional optimizations only available with relaxed precision (such as SIMD CPU instructions).

Accessing RenderScript APIs

When developing an Android application that uses RenderScript, you can access its API in one of two ways:

We strongly recommend using the Support Library APIs for accessing RenderScript because they include the latest improvements to the RenderScript compute framework and provide a wider range of device compatibility.

Using the RenderScript Support Library APIs

In order to use the Support Library RenderScript APIs, you must configure your development environment to be able to access them. The following Android SDK tools are required for using these APIs:

You can check and update the installed version of these tools in the Android SDK Manager.

Note: Use of Support Library RenderScript APIs is not currently supported with Android Studio or Gradle-based builds.

To use the Support Library RenderScript APIs in Eclipse:

  1. Make sure you have the required Android SDK version and Build Tools version installed.
  2. Open the {@code project.properties} file in the root folder of your application project.
  3. Add the following lines to the file:
    renderscript.target=18
    renderscript.support.mode=true
    sdk.buildtools=18.1.0
    
  4. In your application classes that use RenderScript, add an import for the Support Library classes:
    import android.support.v8.renderscript.*;
    

The {@code project.properties} settings listed above control specific behavior in the Android build process:

Using RenderScript from Java Code

Using RenderScript from Java code relies on the API classes located in the {@link android.renderscript} or the {@link android.support.v8.renderscript} package. Most applications follow the same basic usage patterns:

  1. Initialize a RenderScript context. The {@link android.renderscript.RenderScript} context, created with {@link android.renderscript.RenderScript#create}, ensures that RenderScript can be used and provides an object to control the lifetime of all subsequent RenderScript objects. You should consider context creation to be a potentially long-running operation, since it may create resources on different pieces of hardware; it should not be in an application's critical path if at all possible. Typically, an application will have only a single RenderScript context at a time.
  2. Create at least one {@link android.renderscript.Allocation} to be passed to a script. An {@link android.renderscript.Allocation} is a RenderScript object that provides storage for a fixed amount of data. Kernels in scripts take {@link android.renderscript.Allocation} objects as their input and output, and {@link android.renderscript.Allocation} objects can be accessed in kernels using rsGetElementAt_type() and rsSetElementAt_type() when bound as script globals. {@link android.renderscript.Allocation} objects allow arrays to be passed from Java code to RenderScript code and vice-versa. {@link android.renderscript.Allocation} objects are typically created using {@link android.renderscript.Allocation#createTyped} or {@link android.renderscript.Allocation#createFromBitmap}.
  3. Create whatever scripts are necessary. There are two types of scripts available to you when using RenderScript:
  4. Populate Allocations with data. Except for Allocations created with {@link android.renderscript#createFromBitmap}, an Allocation will be populated with empty data when it is first created. To populate an Allocation, use one of the copy methods in {@link android.renderscript.Allocation}.
  5. Set any necessary script globals. Globals may be set using methods in the same ScriptC_filename class with methods named set_globalname. For example, in order to set an int named elements, use the Java method set_elements(int). RenderScript objects can also be set in kernels; for example, the rs_allocation variable named lookup can be set with the method set_lookup(Allocation).
  6. Launch the appropriate kernels. Methods to launch a given kernel will be reflected in the same ScriptC_filename class with methods named forEach_kernelname(). These launches are asynchronous, and launches will be serialized in the order in which they are launched. Depending on the arguments to the kernel, the method will take either one or two Allocations. By default, a kernel will execute over the entire input or output Allocation; to execute over a subset of that Allocation, pass an appropriate {@link android.renderscript.Script.LaunchOptions} as the last argument to the forEach method.

    Invoked functions can be launched using the invoke_functionname methods reflected in the same ScriptC_filename class.

  7. Copy data out of {@link android.renderscript.Allocation} objects. In order to access data from an {@link android.renderscript.Allocation} from Java code, that data must be copied back to Java buffers using one of the copy methods in {@link android.renderscript.Allocation}. These functions will synchronize with asynchronous kernel and function launches as necessary.
  8. Tear down the RenderScript context. The RenderScript context can be destroyed with {@link android.renderscript.RenderScript#destroy} or by allowing the RenderScript context object to be garbage collected. This will cause any further use of any object belonging to that context to throw an exception.