SlideShare a Scribd company logo
Android RenderScript
SK planet/Mobile Platform Dept.
Jungsoo Nam
namjungsoo@gmail.com
About RenderScript
• RenderScript computation
– high performance computation API at the native level that you
write in C (C99 standard)
– your apps the ability to run operations with automatic
parallelization across all available processor cores
• different types of processors such as the CPU, GPU or DSP
– useful for apps that do image processing, mathematical
modeling, or any operations that require lots of mathematical
computation
– access to all of these features without having to write code to
support different architectures or a different amount of
processing cores
– do not need to recompile your application for different
processor types, because Renderscript code is compiled on the
device at runtime.
RenderScript system overview
RenderScript on Android 4.1
• Deprecation Notice
– Earlier versions of Renderscript included an
experimental graphics engine component
• most of the APIs in rs_graphics.rsh and the
corresponding APIs in android.renderscript
– If you have apps that render graphics with
Renderscript, we highly recommend you convert
your code to another Android graphics rendering
option.
• http://docs.huihoo.com/android/4.2/guide/topics/rend
erscript/compute.html#overview
RenderScript basic 1/2
1. Write a .rs 2. Run ‘clean’ and ‘ScriptC_scriptname.java’ is generated
3. ScriptC_mono.java
RenderScript basic 2/2
4. Write a RenderScript code 5. A RenderScript result screenshot
RenderScript entry points
Function(in .rs) Comment
void root(const uchar4* v_in, uchar4* v_out) [Default]
forEach_root(in, out)
void root(const uchar4 *v_in, uchar4 *v_out, const uchar4* data, uint32_t x, uint32_t y) forEach_root(in, out)
Data can be renamed
x, y are not changeable order and
name
void root(const uchar4 *v_in, uchar4 *v_out, uint32_t x, uint32_t y) forEach_root(in, out)
void root(const uchar4 *v_in, uchar4 *v_out, const uchar4* data) forEach_root(in, out)
void root(uchar4* v_out) forEach_root(out)
uchar4 __attribute__((kernel)) functionname(uchar4 in, uint32_t x, uint32_t y) forEach_functionname(in, out)
int root() For graphics(deprecated)
Called by RenderScriptGL
void functionname() invoke_functionname()
FilterScript
• FilterScript
– Introduced in Android 4.2 (API Level 17), Filterscript defines a subset of Renderscript
that focuses on image processing operations, such as those that you would typically
write with an OpenGL ES fragment shader.
– Present Android Developer’s RenderScript contents are explaining basically using
FilterScript.
• http://developer.android.com/guide/topics/renderscript/compute.html
• Usage
– Inputs and return values of root functions cannot contain pointers. The default root
function signature contains pointers, so you must use the __attribute__((kernel))
attribute to declare a custom root function when using FilterScript.
– Built-in types cannot exceed 32-bits.
– FilterScript must always use relaxed floating point precision by using the rs_fp_relaxed
pragma.
• 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).
– FilterScript files must end with an .fs extension, instead of an .rs extension
• Android-18 allows using .rs instead of .fs
FilterScript example
Script
Nested Classes
class Script.Builder Only intended for use by generated reflected code.
class Script.FieldBase Only intended for use by generated reflected code.
class Script.FieldID FieldID is an identifier for a Script + exported field pair.
class Script.KernelID KernelID is an identifier for a Script + root function pair.
class Script.LaunchOptions Class used to specify clipping for a kernel launch.
Protected Methods
Script.FieldID createFieldID(int slot, Element e) Only to be used by generated reflected classes.
Script.KernelID createKernelID(int slot, int sig, Element ein, Element eout) Only to be used by generated reflected classes.
void forEach(int slot, Allocation ain, Allocation aout, FieldPacker v, Script.LaunchOptions sc) Only intended for use by generated reflected code.
void forEach(int slot, Allocation ain, Allocation aout, FieldPacker v) Only intended for use by generated reflected code.
void invoke(int slot) Only intended for use by generated reflected code.
void invoke(int slot, FieldPacker v) Only intended for use by generated reflected code.
Public Methods
Int getXEnd() Returns the current X end
Int getXStart() Returns the current X start
Int getYEnd() Returns the current Y end
Int getYStart() Returns the current Y start
Int getZEnd() Returns the current Z end
Int getZStart() Returns the current Z start
Script.LaunchOptions setX(int xstartArg, int xendArg) Set the X range.
Script.LaunchOptions setY(int ystartArg, int yendArg) Set the Y range.
Script.LaunchOptions setZ(int zstartArg, int zendArg) Set the Z range.
Variables
//uchar4 *gPixels;
private final static int mExportVarIdx_gPixels = 5;
private Allocation mExportVar_gPixels;
public void bind_gPixels(Allocation v) {
mExportVar_gPixels = v;
if (v == null) bindAllocation(null, mExportVarIdx_gPixels);
else bindAllocation(v, mExportVarIdx_gPixels);
}
public Allocation get_gPixels() {
return mExportVar_gPixels;
}
//rs_allocation gIn;
private final static int mExportVarIdx_gIn = 0;
private Allocation mExportVar_gIn;
public synchronized void set_gIn(Allocation v) {
setVar(mExportVarIdx_gIn, v);
mExportVar_gIn = v;
}
public Allocation get_gIn() {
return mExportVar_gIn;
}
public Script.FieldID getFieldID_gIn() {
return createFieldID(mExportVarIdx_gIn, null);
}
//float gFactor = 6;
private final static int mExportVarIdx_gFactor = 6;
private float mExportVar_gFactor;
public synchronized void set_gFactor(float v) {
setVar(mExportVarIdx_gFactor, v);
mExportVar_gFactor = v;
}
public float get_gFactor() {
return mExportVar_gFactor;
}
public Script.FieldID getFieldID_gFactor() {
return createFieldID(mExportVarIdx_gFactor, null);
}
//void root(…)
private final static int mExportForEachIdx_root = 0;
public Script.KernelID getKernelID_root() {
return createKernelID(mExportForEachIdx_root, 3, null, null);
}
public void forEach_root(Allocation ain, Allocation aout) {
// check ain
if (!ain.getType().getElement().isCompatible(__U8_4)) {
throw new RSRuntimeException("Type mismatch with U8_4!");
}
// check aout
if (!aout.getType().getElement().isCompatible(__U8_4)) {
throw new RSRuntimeException("Type mismatch with U8_4!");
}
// Verify dimensions
Type tIn = ain.getType();
Type tOut = aout.getType();
if ((tIn.getCount() != tOut.getCount()) ||
(tIn.getX() != tOut.getX()) ||
(tIn.getY() != tOut.getY()) ||
(tIn.getZ() != tOut.getZ()) ||
(tIn.hasFaces() != tOut.hasFaces()) ||
(tIn.hasMipmaps() != tOut.hasMipmaps())) {
throw new RSRuntimeException("Dimension mismatch between input and output
parameters!");
}
forEach(mExportForEachIdx_root, ain, aout, null);
Structs
/* typedef struct __attribute__((packed, aligned(4)))
Point {
float2 delta;
float2 position;
//uchar4 color;
} Point_t;
Point_t *point;
*/
public class ScriptField_Point extends
android.renderscript.Script.FieldBase {
static public class Item {
public static final int sizeof = 16;
Float2 delta;
Float2 position;
Item() {
delta = new Float2();
position = new Float2();
}
}
private Item mItemArray[];
private FieldPacker mIOBuffer;
private static java.lang.ref.WeakReference<Element> mElementCache =
new java.lang.ref.WeakReference<Element>(null);
public static Element createElement(RenderScript rs) {
Element.Builder eb = new Element.Builder(rs);
eb.add(Element.F32_2(rs), "delta");
eb.add(Element.F32_2(rs), "position");
return eb.create();
}
public synchronized void set(Item i, int index, boolean copyNow) {
if (mItemArray == null) mItemArray = new Item[getType().getX() /* count */];
mItemArray[index] = i;
if (copyNow) {
copyToArray(i, index);
FieldPacker fp = new FieldPacker(Item.sizeof);
copyToArrayLocal(i, fp);
mAllocation.setFromFieldPacker(index, fp);
}
}
public synchronized Item get(int index) {
if (mItemArray == null) return null;
return mItemArray[index];
}
public synchronized void set_delta(int index, Float2 v, boolean copyNow) {
if (mIOBuffer == null) mIOBuffer = new FieldPacker(Item.sizeof * getType().getX()/* count */);
if (mItemArray == null) mItemArray = new Item[getType().getX() /* count */];
if (mItemArray[index] == null) mItemArray[index] = new Item();
mItemArray[index].delta = v;
if (copyNow) {
mIOBuffer.reset(index * Item.sizeof);
mIOBuffer.addF32(v);
FieldPacker fp = new FieldPacker(8);
fp.addF32(v);
mAllocation.setFromFieldPacker(index, 0, fp);
}
}
public synchronized void set_position(int index, Float2 v, boolean copyNow) {
if (mIOBuffer == null) mIOBuffer = new FieldPacker(Item.sizeof * getType().getX()/* count */);
if (mItemArray == null) mItemArray = new Item[getType().getX() /* count */];
if (mItemArray[index] == null) mItemArray[index] = new Item();
mItemArray[index].position = v;
if (copyNow) {
mIOBuffer.reset(index * Item.sizeof + 8);
mIOBuffer.addF32(v);
FieldPacker fp = new FieldPacker(8);
fp.addF32(v);
mAllocation.setFromFieldPacker(index, 1, fp);
}
}
Pointers
/* typedef struct Point {
float2 position;
float size;
} Point_t;
Point_t *touchPoints;
int32_t *intPointer;
*/
private ScriptField_Point mExportVar_touchPoints;
public void bind_touchPoints(ScriptField_Point v) {
mExportVar_touchPoints = v;
if (v == null) bindAllocation(null, mExportVarIdx_touchPoints);
else bindAllocation(v.getAllocation(), mExportVarIdx_touchPoints);
}
public ScriptField_Point get_touchPoints() {
return mExportVar_touchPoints;
}
private Allocation mExportVar_intPointer;
public void bind_intPointer(Allocation v) {
mExportVar_intPointer = v;
if (v == null) bindAllocation(null, mExportVarIdx_intPointer);
else bindAllocation(v, mExportVarIdx_intPointer);
}
public Allocation get_intPointer() {
return mExportVar_intPointer;
}
Memory
Android
Object Type
Description
Element An element describes one cell of a memory allocation
and can have two forms: basic or complex.
Basic:
• Single float value
• 4 element float vector
• single RGB-565 color
• single unsigned int 16
Complex:
• Structs
Type A type is a memory allocation template and consists of
an element and one or more dimensions. It describes the
layout of the memory (basically an array of Elements)
but does not allocate the memory for the data that it
describes.
Allocation An allocation provides the memory for applications
based on a description of the memory that is
represented by a Type. Allocated memory can exist in
many memory spaces concurrently. If memory is
modified in one space, you must explicitly synchronize
the memory, so that it is updated in all the other spaces
in which it exists.
Allocation
• Lifecycle
– Immutable
– Once created
– The replacement is to create a new allocation and copy contents
• Memory usages
– USAGE_SCRIPT: Allocates in the script memory space. This is the default memory space if you do not
specify a memory space.
– USAGE_GRAPHICS_TEXTURE: Allocates in the texture memory space of the GPU.
• This was deprecated in API level 16.
– USAGE_GRAPHICS_VERTEX: Allocates in the vertex memory space of the GPU.
• This was deprecated in API level 16.
– USAGE_GRAPHICS_CONSTANTS: Allocates in the constants memory space of the GPU that is used by
the various program objects.
• This was deprecated in API level 16.
– USAGE_SHARED
• Memory management
– synchronized void destory()
• Frees any native resources associated with this object. The primary use is to force immediate cleanup of resources when it is believed
the GC will not respond quickly enough.
– synchronized void resize(int dimX)
• This method was deprecated in API level 18. RenderScript objects should be immutable once created. The replacement is to create a
new allocation and copy the contents.
RenderScript vs OpenGL ES
• Cons
– Not need to swizzling: ARGB -> BGRA (Low performance reason)
– Not need to Y flipping on bitmap: RenderScript is according to Android Bitmap order. (Low
performance reason)
– Using bitmap’s real pixel array index instead of texture coordinates
– GLSL is based texture and texture coordinates(float), accurate pixel value sampling(getting
pixel from texture) is hard.
– Android standard
– Not need Lifecycle management
– Interoperating rendering with normal views is available (RenderScript has no drawing routine)
– Interoperating parallel processing between CPU and GPU is available (If GPU doesn't support
parallel processing, use CPU)
• Pros
– No Vertex shader
• Vertex shader work -> per pixel work -> overhead
• RSSurfaceView can use vertex shader (But now deprecated API)
– RenderScript developer insufficient
• Documents and materials are insufficient
– Higher level C like language than GLSL
RenderScript Flow chart
RenderScript sample
single image sampling
• Single-image sampling(default)
– *v_in, *v_out are current image’s in/out pointers
– Using forEach_root()
– rsUnpackColor8888 unpacks uchar4 -> float4
– rsPackColorTo8888 packs float4 -> uchar4
RenderScript sample
multi image sampling
• Multi-image sampling
– Not only one given image pixel pointer *v_in, *v_out sampling, but multiple image pixel sampling by
additionally binded uchar4*
– Basically use when filtering after referencing near pixel
– Use invoke_filter() instead of forEach_root()
RenderScript sample
multi pass effect
• Multi-pass
– An effect after run RenderScript’s root function n times, run getBitmap() from an allocation.
– Between n-1 and n, data is transferred by allocation. Not bitmap copying.
Histogram sample(1/12)
Histogram sample(2/12)
Histogram sample(3/12)
Histogram sample(4/12)
Histogram sample(5/12)
Histogram sample(6/12)
Histogram sample(7/12)
Histogram sample(8/12)
Histogram sample(9/12)
Histogram sample(10/12)
Histogram sample(11/12)
Histogram sample(12/12)

More Related Content

PPTX
Android OpenGL ES Game ImageGrabber Final Report
PPTX
13th kandroid OpenGL and EGL
PDF
Android High performance in GPU using opengles and renderscript
PPTX
OpenGL ES EGL Spec&APIs
PPTX
OpenGL Shading Language
PDF
Android RenderScript on LLVM
PPTX
OpenGL basics
PDF
Understaing Android EGL
Android OpenGL ES Game ImageGrabber Final Report
13th kandroid OpenGL and EGL
Android High performance in GPU using opengles and renderscript
OpenGL ES EGL Spec&APIs
OpenGL Shading Language
Android RenderScript on LLVM
OpenGL basics
Understaing Android EGL

What's hot (20)

PPT
SIGGRAPH Asia 2008 Modern OpenGL
PDF
OpenGL Introduction.
PPT
OpenGL for 2015
PPT
Open Graphics Library
PDF
OpenGL ES 2.0 Programming and Cocos2d-x v3.3 Rendering System
PDF
OpenGL ES 2.x Programming Introduction
PPT
NVIDIA's OpenGL Functionality
PDF
Opengl basics
PPT
Programming with OpenGL
PPT
Open gl
PPTX
Chapter02 graphics-programming
PPTX
Baiscs of OpenGL
PPT
Opengl (1)
PDF
Android native gl
PPSX
Vertex Shader Tricks by Bill Bilodeau - AMD at GDC14
PPT
OpenGL Basics
PDF
OpenGL Transformation
PDF
[Osxdev]metal
PPT
CS 354 Introduction
PDF
Angular Weekend
SIGGRAPH Asia 2008 Modern OpenGL
OpenGL Introduction.
OpenGL for 2015
Open Graphics Library
OpenGL ES 2.0 Programming and Cocos2d-x v3.3 Rendering System
OpenGL ES 2.x Programming Introduction
NVIDIA's OpenGL Functionality
Opengl basics
Programming with OpenGL
Open gl
Chapter02 graphics-programming
Baiscs of OpenGL
Opengl (1)
Android native gl
Vertex Shader Tricks by Bill Bilodeau - AMD at GDC14
OpenGL Basics
OpenGL Transformation
[Osxdev]metal
CS 354 Introduction
Angular Weekend
Ad

Similar to Android RenderScript (20)

PPTX
RenderScript
PDF
Efficient Image Processing - Nicolas Roard
PDF
The Explanation the Pipeline design strategy.pdf
PPTX
Shader model 5 0 and compute shader
PDF
OpenGL 4.6 Reference Guide
PDF
High performance graphics and computation - OpenGL ES and RenderScript
PDF
OpenGL ES 3.1 Reference Card
PDF
Chameleon game engine
PDF
OpenGL ES 3.2 Reference Guide
PDF
OpenGL 4.5 Reference Card
PDF
Playing with camera preview buffers on BlackBerry 10
PPTX
Introduce to 3d rendering engine
PDF
[1D6]RE-view of Android L developer PRE-view
PDF
Game Programming 12 - Shaders
PDF
Cg in Two Pages
PDF
Android internals 07 - Android graphics (rev_1.1)
PDF
OpenGL ES and Mobile GPU
PDF
[GEG1] 10.camera-centric engine design for multithreaded rendering
PDF
OpenGL SC 2.0 Quick Reference
RenderScript
Efficient Image Processing - Nicolas Roard
The Explanation the Pipeline design strategy.pdf
Shader model 5 0 and compute shader
OpenGL 4.6 Reference Guide
High performance graphics and computation - OpenGL ES and RenderScript
OpenGL ES 3.1 Reference Card
Chameleon game engine
OpenGL ES 3.2 Reference Guide
OpenGL 4.5 Reference Card
Playing with camera preview buffers on BlackBerry 10
Introduce to 3d rendering engine
[1D6]RE-view of Android L developer PRE-view
Game Programming 12 - Shaders
Cg in Two Pages
Android internals 07 - Android graphics (rev_1.1)
OpenGL ES and Mobile GPU
[GEG1] 10.camera-centric engine design for multithreaded rendering
OpenGL SC 2.0 Quick Reference
Ad

Recently uploaded (20)

PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Approach and Philosophy of On baking technology
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Chapter 3 Spatial Domain Image Processing.pdf
Advanced methodologies resolving dimensionality complications for autism neur...
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
NewMind AI Weekly Chronicles - August'25 Week I
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Approach and Philosophy of On baking technology
The AUB Centre for AI in Media Proposal.docx
Encapsulation_ Review paper, used for researhc scholars
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
MIND Revenue Release Quarter 2 2025 Press Release
Diabetes mellitus diagnosis method based random forest with bat algorithm
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
The Rise and Fall of 3GPP – Time for a Sabbatical?
MYSQL Presentation for SQL database connectivity
Building Integrated photovoltaic BIPV_UPV.pdf
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy

Android RenderScript

  • 1. Android RenderScript SK planet/Mobile Platform Dept. Jungsoo Nam namjungsoo@gmail.com
  • 2. About RenderScript • RenderScript computation – high performance computation API at the native level that you write in C (C99 standard) – your apps the ability to run operations with automatic parallelization across all available processor cores • different types of processors such as the CPU, GPU or DSP – useful for apps that do image processing, mathematical modeling, or any operations that require lots of mathematical computation – access to all of these features without having to write code to support different architectures or a different amount of processing cores – do not need to recompile your application for different processor types, because Renderscript code is compiled on the device at runtime.
  • 4. RenderScript on Android 4.1 • Deprecation Notice – Earlier versions of Renderscript included an experimental graphics engine component • most of the APIs in rs_graphics.rsh and the corresponding APIs in android.renderscript – If you have apps that render graphics with Renderscript, we highly recommend you convert your code to another Android graphics rendering option. • http://docs.huihoo.com/android/4.2/guide/topics/rend erscript/compute.html#overview
  • 5. RenderScript basic 1/2 1. Write a .rs 2. Run ‘clean’ and ‘ScriptC_scriptname.java’ is generated 3. ScriptC_mono.java
  • 6. RenderScript basic 2/2 4. Write a RenderScript code 5. A RenderScript result screenshot
  • 7. RenderScript entry points Function(in .rs) Comment void root(const uchar4* v_in, uchar4* v_out) [Default] forEach_root(in, out) void root(const uchar4 *v_in, uchar4 *v_out, const uchar4* data, uint32_t x, uint32_t y) forEach_root(in, out) Data can be renamed x, y are not changeable order and name void root(const uchar4 *v_in, uchar4 *v_out, uint32_t x, uint32_t y) forEach_root(in, out) void root(const uchar4 *v_in, uchar4 *v_out, const uchar4* data) forEach_root(in, out) void root(uchar4* v_out) forEach_root(out) uchar4 __attribute__((kernel)) functionname(uchar4 in, uint32_t x, uint32_t y) forEach_functionname(in, out) int root() For graphics(deprecated) Called by RenderScriptGL void functionname() invoke_functionname()
  • 8. FilterScript • FilterScript – Introduced in Android 4.2 (API Level 17), Filterscript defines a subset of Renderscript that focuses on image processing operations, such as those that you would typically write with an OpenGL ES fragment shader. – Present Android Developer’s RenderScript contents are explaining basically using FilterScript. • http://developer.android.com/guide/topics/renderscript/compute.html • Usage – Inputs and return values of root functions cannot contain pointers. The default root function signature contains pointers, so you must use the __attribute__((kernel)) attribute to declare a custom root function when using FilterScript. – Built-in types cannot exceed 32-bits. – FilterScript must always use relaxed floating point precision by using the rs_fp_relaxed pragma. • 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). – FilterScript files must end with an .fs extension, instead of an .rs extension • Android-18 allows using .rs instead of .fs
  • 10. Script Nested Classes class Script.Builder Only intended for use by generated reflected code. class Script.FieldBase Only intended for use by generated reflected code. class Script.FieldID FieldID is an identifier for a Script + exported field pair. class Script.KernelID KernelID is an identifier for a Script + root function pair. class Script.LaunchOptions Class used to specify clipping for a kernel launch. Protected Methods Script.FieldID createFieldID(int slot, Element e) Only to be used by generated reflected classes. Script.KernelID createKernelID(int slot, int sig, Element ein, Element eout) Only to be used by generated reflected classes. void forEach(int slot, Allocation ain, Allocation aout, FieldPacker v, Script.LaunchOptions sc) Only intended for use by generated reflected code. void forEach(int slot, Allocation ain, Allocation aout, FieldPacker v) Only intended for use by generated reflected code. void invoke(int slot) Only intended for use by generated reflected code. void invoke(int slot, FieldPacker v) Only intended for use by generated reflected code. Public Methods Int getXEnd() Returns the current X end Int getXStart() Returns the current X start Int getYEnd() Returns the current Y end Int getYStart() Returns the current Y start Int getZEnd() Returns the current Z end Int getZStart() Returns the current Z start Script.LaunchOptions setX(int xstartArg, int xendArg) Set the X range. Script.LaunchOptions setY(int ystartArg, int yendArg) Set the Y range. Script.LaunchOptions setZ(int zstartArg, int zendArg) Set the Z range.
  • 11. Variables //uchar4 *gPixels; private final static int mExportVarIdx_gPixels = 5; private Allocation mExportVar_gPixels; public void bind_gPixels(Allocation v) { mExportVar_gPixels = v; if (v == null) bindAllocation(null, mExportVarIdx_gPixels); else bindAllocation(v, mExportVarIdx_gPixels); } public Allocation get_gPixels() { return mExportVar_gPixels; } //rs_allocation gIn; private final static int mExportVarIdx_gIn = 0; private Allocation mExportVar_gIn; public synchronized void set_gIn(Allocation v) { setVar(mExportVarIdx_gIn, v); mExportVar_gIn = v; } public Allocation get_gIn() { return mExportVar_gIn; } public Script.FieldID getFieldID_gIn() { return createFieldID(mExportVarIdx_gIn, null); } //float gFactor = 6; private final static int mExportVarIdx_gFactor = 6; private float mExportVar_gFactor; public synchronized void set_gFactor(float v) { setVar(mExportVarIdx_gFactor, v); mExportVar_gFactor = v; } public float get_gFactor() { return mExportVar_gFactor; } public Script.FieldID getFieldID_gFactor() { return createFieldID(mExportVarIdx_gFactor, null); } //void root(…) private final static int mExportForEachIdx_root = 0; public Script.KernelID getKernelID_root() { return createKernelID(mExportForEachIdx_root, 3, null, null); } public void forEach_root(Allocation ain, Allocation aout) { // check ain if (!ain.getType().getElement().isCompatible(__U8_4)) { throw new RSRuntimeException("Type mismatch with U8_4!"); } // check aout if (!aout.getType().getElement().isCompatible(__U8_4)) { throw new RSRuntimeException("Type mismatch with U8_4!"); } // Verify dimensions Type tIn = ain.getType(); Type tOut = aout.getType(); if ((tIn.getCount() != tOut.getCount()) || (tIn.getX() != tOut.getX()) || (tIn.getY() != tOut.getY()) || (tIn.getZ() != tOut.getZ()) || (tIn.hasFaces() != tOut.hasFaces()) || (tIn.hasMipmaps() != tOut.hasMipmaps())) { throw new RSRuntimeException("Dimension mismatch between input and output parameters!"); } forEach(mExportForEachIdx_root, ain, aout, null);
  • 12. Structs /* typedef struct __attribute__((packed, aligned(4))) Point { float2 delta; float2 position; //uchar4 color; } Point_t; Point_t *point; */ public class ScriptField_Point extends android.renderscript.Script.FieldBase { static public class Item { public static final int sizeof = 16; Float2 delta; Float2 position; Item() { delta = new Float2(); position = new Float2(); } } private Item mItemArray[]; private FieldPacker mIOBuffer; private static java.lang.ref.WeakReference<Element> mElementCache = new java.lang.ref.WeakReference<Element>(null); public static Element createElement(RenderScript rs) { Element.Builder eb = new Element.Builder(rs); eb.add(Element.F32_2(rs), "delta"); eb.add(Element.F32_2(rs), "position"); return eb.create(); } public synchronized void set(Item i, int index, boolean copyNow) { if (mItemArray == null) mItemArray = new Item[getType().getX() /* count */]; mItemArray[index] = i; if (copyNow) { copyToArray(i, index); FieldPacker fp = new FieldPacker(Item.sizeof); copyToArrayLocal(i, fp); mAllocation.setFromFieldPacker(index, fp); } } public synchronized Item get(int index) { if (mItemArray == null) return null; return mItemArray[index]; } public synchronized void set_delta(int index, Float2 v, boolean copyNow) { if (mIOBuffer == null) mIOBuffer = new FieldPacker(Item.sizeof * getType().getX()/* count */); if (mItemArray == null) mItemArray = new Item[getType().getX() /* count */]; if (mItemArray[index] == null) mItemArray[index] = new Item(); mItemArray[index].delta = v; if (copyNow) { mIOBuffer.reset(index * Item.sizeof); mIOBuffer.addF32(v); FieldPacker fp = new FieldPacker(8); fp.addF32(v); mAllocation.setFromFieldPacker(index, 0, fp); } } public synchronized void set_position(int index, Float2 v, boolean copyNow) { if (mIOBuffer == null) mIOBuffer = new FieldPacker(Item.sizeof * getType().getX()/* count */); if (mItemArray == null) mItemArray = new Item[getType().getX() /* count */]; if (mItemArray[index] == null) mItemArray[index] = new Item(); mItemArray[index].position = v; if (copyNow) { mIOBuffer.reset(index * Item.sizeof + 8); mIOBuffer.addF32(v); FieldPacker fp = new FieldPacker(8); fp.addF32(v); mAllocation.setFromFieldPacker(index, 1, fp); } }
  • 13. Pointers /* typedef struct Point { float2 position; float size; } Point_t; Point_t *touchPoints; int32_t *intPointer; */ private ScriptField_Point mExportVar_touchPoints; public void bind_touchPoints(ScriptField_Point v) { mExportVar_touchPoints = v; if (v == null) bindAllocation(null, mExportVarIdx_touchPoints); else bindAllocation(v.getAllocation(), mExportVarIdx_touchPoints); } public ScriptField_Point get_touchPoints() { return mExportVar_touchPoints; } private Allocation mExportVar_intPointer; public void bind_intPointer(Allocation v) { mExportVar_intPointer = v; if (v == null) bindAllocation(null, mExportVarIdx_intPointer); else bindAllocation(v, mExportVarIdx_intPointer); } public Allocation get_intPointer() { return mExportVar_intPointer; }
  • 14. Memory Android Object Type Description Element An element describes one cell of a memory allocation and can have two forms: basic or complex. Basic: • Single float value • 4 element float vector • single RGB-565 color • single unsigned int 16 Complex: • Structs Type A type is a memory allocation template and consists of an element and one or more dimensions. It describes the layout of the memory (basically an array of Elements) but does not allocate the memory for the data that it describes. Allocation An allocation provides the memory for applications based on a description of the memory that is represented by a Type. Allocated memory can exist in many memory spaces concurrently. If memory is modified in one space, you must explicitly synchronize the memory, so that it is updated in all the other spaces in which it exists.
  • 15. Allocation • Lifecycle – Immutable – Once created – The replacement is to create a new allocation and copy contents • Memory usages – USAGE_SCRIPT: Allocates in the script memory space. This is the default memory space if you do not specify a memory space. – USAGE_GRAPHICS_TEXTURE: Allocates in the texture memory space of the GPU. • This was deprecated in API level 16. – USAGE_GRAPHICS_VERTEX: Allocates in the vertex memory space of the GPU. • This was deprecated in API level 16. – USAGE_GRAPHICS_CONSTANTS: Allocates in the constants memory space of the GPU that is used by the various program objects. • This was deprecated in API level 16. – USAGE_SHARED • Memory management – synchronized void destory() • Frees any native resources associated with this object. The primary use is to force immediate cleanup of resources when it is believed the GC will not respond quickly enough. – synchronized void resize(int dimX) • This method was deprecated in API level 18. RenderScript objects should be immutable once created. The replacement is to create a new allocation and copy the contents.
  • 16. RenderScript vs OpenGL ES • Cons – Not need to swizzling: ARGB -> BGRA (Low performance reason) – Not need to Y flipping on bitmap: RenderScript is according to Android Bitmap order. (Low performance reason) – Using bitmap’s real pixel array index instead of texture coordinates – GLSL is based texture and texture coordinates(float), accurate pixel value sampling(getting pixel from texture) is hard. – Android standard – Not need Lifecycle management – Interoperating rendering with normal views is available (RenderScript has no drawing routine) – Interoperating parallel processing between CPU and GPU is available (If GPU doesn't support parallel processing, use CPU) • Pros – No Vertex shader • Vertex shader work -> per pixel work -> overhead • RSSurfaceView can use vertex shader (But now deprecated API) – RenderScript developer insufficient • Documents and materials are insufficient – Higher level C like language than GLSL
  • 18. RenderScript sample single image sampling • Single-image sampling(default) – *v_in, *v_out are current image’s in/out pointers – Using forEach_root() – rsUnpackColor8888 unpacks uchar4 -> float4 – rsPackColorTo8888 packs float4 -> uchar4
  • 19. RenderScript sample multi image sampling • Multi-image sampling – Not only one given image pixel pointer *v_in, *v_out sampling, but multiple image pixel sampling by additionally binded uchar4* – Basically use when filtering after referencing near pixel – Use invoke_filter() instead of forEach_root()
  • 20. RenderScript sample multi pass effect • Multi-pass – An effect after run RenderScript’s root function n times, run getBitmap() from an allocation. – Between n-1 and n, data is transferred by allocation. Not bitmap copying.

Editor's Notes