Android:Renderscript
lastmod 2018-08-17created 2014-02-1332 edits
RenderScript is a new API targeted at high-performance 3D rendering and compute operations. The goal of RenderScript is to give Android developers a high-performance API obtained at a low level. It is aimed at developers who are comfortable working down to the machine level to optimize the performance of their applications. RenderScript provides developers with three primary tools: a simple hardware-accelerated 3D rendering API, a developer-friendly compute API similar to CUDA, and a language similar to C99.
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:
- Android SDK Tools revision 22.2 or higher
- Android SDK Build-tools revision 18.1.0 or higher
- 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.
RenderScript APIs in Eclipse
To use the Support Library RenderScript APIs in Eclipse:
Make sure you have the required Android SDK version and Build Tools version installed.
Open the project.properties file in the root folder of your application project.
Add the following lines to the file:
-
renderscript.target=18 -
renderscript.support.mode=true -
sdk.buildtools=18.1.0
In your application classes that use RenderScript, add an import for the Support Library classes:
-
import android.support.v8.renderscript.*;
project.properties settings list
The project.properties settings listed above control specific behavior in the Android build process:
-
renderscript.target - Specifies the bytecode version to be generated. We recommend you set this value the highest available API level and set
renderscript.support.modeto true. Valid values for this setting are any integer value from 11 to the most recently released API level. If your minimum SDK version specified in your application manifest is set to a higher value, this value is ignored and the target value is set to the minimum SDK version. -
renderscript.support.mode - Specifies that the generated bytecode should fall back to a compatible version if the device it is running on does not support the target version.
-
sdk.buildtools - The version of the Android SDK build tools to use. This value should be set to 18.1.0 or higher. If this option is not specified, the highest installed build tools version is used. You should always set this value to ensure the consistency of builds across development machines with different configurations.
APIs & Implement detail
If you want to use the RenderScript Support Library in your app, there are a few things you should know.
- The RenderScript Support Library supports most of the native RenderScript API features supported at and after the API level. Notable exceptions are
Allocation.USAGE_IO_INPUTandAllocation.USAGE_IO_OUTPUT.
These are currently not supported by the RenderScript Support Library. - On 4.3 and later, RenderScript applications can run on the processors supported by the device, whereas on Android 4.2 and earlier devices, RenderScript applications run entirely on the CPU.
(Note: GPU execution is supported on some devices even on Android 4.3, such as the Nexus 10.)
Because the Support Library versions of scripts are precompiled to support all possible platforms, compiler optimization is limited compared to Android 4.3, which compiles at runtime, and there is a corresponding performance penalty.
Print Logcat
To print log messages from RenderScript, use a function like the following.
Sample code
mono.rs, the RenderScript input
// mono.rs
#pragma version(1)
#pragma rs java_package_name(com.example.renderscriptsample)
float3 gMonoMult = {0.2125, 0.7154, 0.0721};
void root(const uchar4 *v_in, uchar4 *v_out) {
float4 f4 = rsUnpackColor8888(*v_in);
float3 mono = dot(f4.rgb, gMonoMult);
*v_out = rsPackColorTo8888(mono);
}
MainActivity.java, which implements the Android screen (Activity)
package com.example.renderscriptsample;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
// import android.renderscript.Allocation;
// import android.renderscript.RenderScript;
import android.support.v8.renderscript.Allocation;
import android.support.v8.renderscript.RenderScript;
import android.view.Menu;
import android.widget.ImageView;
public class MainActivity extends Activity {
// RenderScript
private RenderScript mRS;
private Allocation mInAllocation;
private Allocation mOutAllocation;
private ScriptC_mono mScript;
private Bitmap loadBitmap(int resource) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
return BitmapFactory.decodeResource(getResources(), resource, options);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// bitmap load
final Bitmap bm = loadBitmap(R.drawable.data);
final Bitmap bmOut = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), bm.getConfig());
// renderscript init
mRS = RenderScript.create(this);
mInAllocation = Allocation.createFromBitmap(mRS, bm, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
mOutAllocation = Allocation.createFromBitmap(mRS, bm, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
mScript = new ScriptC_mono(mRS, getResources(), R.raw.mono);
// renderscript run
mScript.forEach_root(mInAllocation, mOutAllocation);
mOutAllocation.copyTo(bmOut);
// set output bitmap
final ImageView iv = (ImageView) findViewById(R.id.imageView1);
iv.setImageBitmap(bmOut);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
RenderScript Animation
Building on the example above, the following example uses ValueAnimator to update the Bitmap of an ImageView.
// set output bitmap
final ImageView iv = (ImageView) findViewById(R.id.activity_main__logo_icon_custom_bg);
iv.setImageBitmap(bmOut);
// ...
ValueAnimator ani = ValueAnimator.ofFloat(0.0f, 360.0f);
ani.setDuration(3000);
ani.setRepeatCount(200);
ani.setInterpolator(new AccelerateDecelerateInterpolator());
ani.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float val = (Float) animation.getAnimatedValue();
int rgba = Color.HSVToColor(new float[] {val, 1.0f, 1.0f});
float r = (float)((rgba >> 16) & 0xFF) / 255.0f;
float g = (float)((rgba >> 8) & 0xFF) / 255.0f;
float b = (float)( rgba & 0xFF) / 255.0f;
mScript.set_g_add(new Float4(r, g, b, 1.0f));
mScript.forEach_root(mInAllocation, mOutAllocation);
mOutAllocation.copyTo(bmOut);
iv.setImageBitmap(bmOut);
}
});
ani.start();
The RenderScript used by the code above is as follows.
It changes every color whose Alpha value is not 0 in the Bitmap passed in as v_in.
float4 g_add = 0.0f;
/// @brief RenderScript Entry point.
void root(const uchar4 * v_in, uchar4 * v_out)
{
// rsDebug("[RenderScript] test 1 call.", 0);
float4 f4 = rsUnpackColor8888(*v_in);
if (f4.w != 0) {
f4 = g_add;
// rsDebug("[RenderScript] test 2 call.", f4);
}
*v_out = rsPackColorTo8888(f4);
}
See also
Favorite site
- developer.android.com: Rendscript
- Developer Android: RenderScript API Guide
- Getting started with Android RenderScript 1 2
- Introducing RenderScript
- RenderScript Part 2
- Micro Software: Android parallel programming with RenderScript 3
- Stack Overflow: Allocating big bitmap for renderscript (android)
- Designing Apps for XOOM: Renderscript 4
- RenderScript 101, Part 1: RenderScript Basics Tutorial 5
- RenderScript 101, Part 2: RenderScript Interaction 6
- How to use RenderScript rsgBindTexture() 7
- Page flip effect in RenderScript 8
- RenderScript Support For All 9
- RenderScript Intrinsics (built-in functions)
- Open Expert Group: RenderScript 10
- Levels in Renderscript
- RenderScript Real time image processing (on mobile) 11 12
- Getting Started With RenderScript on Android
- Github: RenderScriptDemos 13
- edu4android.com RenderScript 14
References
-
Beginning_Android_RenderScript.pdf ↩
-
Sample Download: Renderscript-example-Gravity-r2.zip ↩
-
RenderScript_android_parallel_programming.pdf ↩
-
Designing_Apps_for_XOOM_pt2.pdf ↩
-
Blog.iangclifton_-RenderScript1-_Basics_Tutorial.pdf ↩
-
Blog.iangclifton_-RenderScript2-_Interaction.pdf ↩
-
RenderScript_Step4_(jp)_rsgBindTexture.pdf ↩
-
RenderScript_-_Page_flip.pdf ↩
-
RenderScript_Support_For_All_Double_Encore.pdf ↩
-
Open_Expert_Group_-_RenderScript.pdf ↩
-
RenderScript_Real_time_image_processing.pdf ↩
-
RSRTImgProc-master-52df27a126e5d3f1d4a32b99ce0a94d05a86cbad.tar.gz ↩
-
RenderScriptDemos-master-70307a646cb8f4192d292ff737c22b2dc5aed16c.tar.gz ↩
-
Edu4android.com_-_RenderScript.pdf ↩