高难度(3)RenderScript
1.RenderScript
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:
- High-performance compute kernels are written in a C99-derived language.
- A Java API is used for managing the lifetime of RenderScript resources and controlling kernel execution.
2.Writing a RenderScript Kernel
2.1 introduction
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:
- A pragma declaration (
#pragma version(1)
) that declares the version of the RenderScript kernel language used in this script. Currently, 1 is the only valid value. - A pragma declaration (
#pragma rs java_package_name(com.example.app)
) that declares the package name of the Java classes reflected from this script. Note that your .rs file must be part of your application package, and not in a library project. - Some number of invokable functions. An invokable function is a single-threaded RenderScript function that you can call from your Java code with arbitrary arguments. These are often useful for initial setup or serial computations within a larger processing pipeline.
- Some number of script globals. A script global is equivalent to a global variable in C. You can access script globals from Java code, and these are often used for parameter passing to RenderScript kernels.
- Some number of compute kernels. A kernel is a parallel function that executes across every
Element
within anAllocation
.A simple kernel may look like the following:
uchar4 __attribute__((kernel)) invert(uchar4 in, uint32_t x, uint32_t y) {
uchar4 out = in;
out.r = - in.r;
out.g = - in.g;
out.b = - in.b;
return out;
}In most respects, this is identical to a standard C function. The first notable feature is the
__attribute__((kernel))
applied to the function prototype. This denotes that the function is a RenderScript kernel instead of an invokable function. The next feature is thein
argument and its type. In a RenderScript kernel, this is a special argument that is automatically filled in based on the inputAllocation
passed to the kernel launch. By default, the kernel is run across an entireAllocation
, with one execution of the kernel body perElement
in theAllocation
. The third notable feature is the return type of the kernel. The value returned from the kernel is automatically written to the appropriate location in the outputAllocation
. The RenderScript runtime checks to ensure that theElement
types of the input and output Allocations match the kernel's prototype; if they do not match, an exception is thrown.A kernel may have an input
Allocation
, an outputAllocation
, or both. A kernel may not have more than one input or one outputAllocation
. If more than one input or output is required, those objects should be bound tors_allocation
script globals and accessed from a kernel or invokable function viarsGetElementAt_type()
orrsSetElementAt_type()
.A kernel may access the coordinates of the current execution using the
x
,y
, andz
arguments. These arguments are optional, but the type of the coordinate arguments must beuint32_t
. - An optional
init()
function. Aninit()
function is a special type of invokable function that is run when the script is first instantiated. This allows for some computation to occur automatically at script creation. - Some number of static script globals and functions. A static script global is equivalent to a script global except that it cannot be set from Java code. A static function is a standard C function that can be called from any kernel or invokable function in the script but is not exposed to the Java API. If a script global or function does not need to be called from Java code, it is highly recommended that those be declared
static
.
2.1 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:
#pragma rs_fp_full
(default if nothing is specified): For apps that require floating point precision as outlined by the IEEE 754-2008 standard.#pragma rs_fp_relaxed
- For apps that don’t require strict IEEE 754-2008 compliance and can tolerate less precision. This mode enables flush-to-zero for denorms and round-towards-zero.#pragma rs_fp_imprecise
- For apps that don’t have stringent precision requirements. This mode enables everything inrs_fp_relaxed
along with the following:- Operations resulting in -0.0 can return +0.0 instead.
- Operations on INF and NAN are undefined.
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).
3.Accessing RenderScript APIs
When developing an Android application that uses RenderScript, you can access its API in one of two ways:
android.renderscript
- The APIs in this class package are available on devices running Android 3.0 (API level 11) and higher.android.support.v8.renderscript
- The APIs in this package are available through a Support Library, which allows you to use them on devices running Android 2.2 (API level 8) and higher.
We strongly recommend using the Support Library APIs for accessing RenderScript because they provide a wider range of device compatibility. Developers targeting specific versions of Android can useandroid.renderscript
if necessary.
3.1 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.
To use the Support Library RenderScript APIs:
- Make sure you have the required Android SDK version and Build Tools version installed.
- Update the settings for the Android build process to include the RenderScript settings:
- Open the
build.gradle
file in the app folder of your application module. - Add the following RenderScript settings to the file:
android {
compileSdkVersion
buildToolsVersion "19.0.3" defaultConfig {
minSdkVersion
targetSdkVersion renderscriptTargetApi
renderscriptSupportModeEnabled true }
}The settings listed above control specific behavior in the Android build process:
renderscriptTargetApi
- Specifies the bytecode version to be generated. We recommend you set this value to the highest available API level and setrenderscriptSupportModeEnabled
totrue
. 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 different value, that value is ignored and the target value in the build file is used to set the minimum SDK version.renderscriptSupportModeEnabled
- 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.buildToolsVersion
- The version of the Android SDK build tools to use. This value should be set to18.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.
- Open the
- In your application classes that use RenderScript, add an import for the Support Library classes:
import android.support.v8.renderscript.*;
4.Using RenderScript from Java Code
Using RenderScript from Java code relies on the API classes located in the android.renderscript
or theandroid.support.v8.renderscript
package. Most applications follow the same basic usage patterns:
- Initialize a RenderScript context. The
RenderScript
context, created withcreate(Context)
, 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. - Create at least one
Allocation
to be passed to a script. AnAllocation
is a RenderScript object that provides storage for a fixed amount of data. Kernels in scripts takeAllocation
objects as their input and output, andAllocation
objects can be accessed in kernels usingrsGetElementAt_type()
andrsSetElementAt_type()
when bound as script globals.Allocation
objects allow arrays to be passed from Java code to RenderScript code and vice-versa.Allocation
objects are typically created usingcreateTyped(RenderScript, Type)
orcreateFromBitmap(RenderScript, Bitmap)
. - Create whatever scripts are necessary. There are two types of scripts available to you when using RenderScript:
- ScriptC: These are the user-defined scripts as described in Writing a RenderScript Kernel above. Every script has a Java class reflected by the RenderScript compiler in order to make it easy to access the script from Java code; this class will have the name
ScriptC_filename
. For example, if the kernel above was located ininvert.rs
and a RenderScript context was already located inmRS
, the Java code to instantiate the script would be:ScriptC_invert invert = new ScriptC_invert(mRenderScript);
- ScriptIntrinsic: These are built-in RenderScript kernels for common operations, such as Gaussian blur, convolution, and image blending. For more information, see the subclasses of
ScriptIntrinsic
.
- ScriptC: These are the user-defined scripts as described in Writing a RenderScript Kernel above. Every script has a Java class reflected by the RenderScript compiler in order to make it easy to access the script from Java code; this class will have the name
- Populate Allocations with data. Except for Allocations created with
android.renderscript
, an Allocation will be populated with empty data when it is first created. To populate an Allocation, use one of thecopy
methods inAllocation
. - Set any necessary script globals. Globals may be set using methods in the same
ScriptC_filename
class with methods namedset_globalname
. For example, in order to set anint
namedelements
, use the Java methodset_elements(int)
. RenderScript objects can also be set in kernels; for example, thers_allocation
variable namedlookup
can be set with the methodset_lookup(Allocation)
. - Launch the appropriate kernels. Methods to launch a given kernel will be reflected in the same
ScriptC_filename
class with methods namedforEach_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 appropriateScript.LaunchOptions
as the last argument to theforEach
method.Invoked functions can be launched using the
invoke_functionname
methods reflected in the sameScriptC_filename
class. - Copy data out of
Allocation
objects. In order to access data from anAllocation
from Java code, that data must be copied back to Java buffers using one of thecopy
methods inAllocation
. These functions will synchronize with asynchronous kernel and function launches as necessary. - Tear down the RenderScript context. The RenderScript context can be destroyed with
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.
高难度(3)RenderScript的更多相关文章
- 使用Aspose.Cell控件实现Excel高难度报表的生成(三)
在之前几篇文章中,介绍了关于Apsose.cell这个强大的Excel操作控件的使用,相关文章如下: 使用Aspose.Cell控件实现Excel高难度报表的生成(一) 使用Aspose.Cell控件 ...
- 使用Aspose.Cell控件实现Excel高难度报表的生成(二)
继续在上篇<使用Aspose.Cell控件实现Excel高难度报表的生成(一)>随笔基础上,研究探讨基于模板的Aspose.cell报表实现,其中提到了下面两种报表的界面,如下所示: 或者 ...
- 使用Aspose.Cell控件实现Excel高难度报表的生成
1.使用Aspose.Cell控件实现Excel高难度报表的生成(一) http://www.cnblogs.com/wuhuacong/archive/2011/02/23/1962147.html ...
- 绝地求生模拟登陆!高难度JS解密教程,Python高级爬虫开发,
前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. PS:如有需要Python学习资料的小伙伴可以加点击下方链接自行获取htt ...
- 使用Aspose.Cell控件实现Excel高难度报表的生成(一)
时光飞逝,生活.工作.业余研究总是在不停忙碌着,转眼快到月底,该月的博客文章任务未完,停顿回忆一下,总结一些经验以及好的东西出来,大家一起分享一下.本文章主要介绍报表的生成,基于Aspose.Cell ...
- (转)使用Aspose.Cell控件实现Excel高难度报表的生成(一)
本文章主要介绍报表的生成,基于Aspose.Cell控件的报表生成.谈到报表,估计大家都有所领悟以及个人的理解,总的来说,一般的报表生成,基本上是基于以下几种方式:一种是基于微软Excel内置的引擎来 ...
- 高难度(1)常用的AR构架或库
Layar http://www.layar.com/ Layar旨在打造的一个开放的增强现实的平台,任何第三方都可以通过Layar的开发接口来打造基于Layar的自己的增强现实应用. 高通AR开发包 ...
- 高难度(1)什么是AR
在介绍增强现实(AR)之前,需要先说说虚拟现实(VR) 虚拟现实是从英文Virtual Reality 一词翻译过来的,简称VR.VR 技术是采用以计算机技术为核心的技术,生成逼真的视.听.触觉等一体 ...
- (高难度SQL)从产品表中找出相同前缀 (都云作者痴 谁解其中味)
--期盼值 找出AA,3;PDST,3;QPL-,3;TP-,2; --基本表 create table tb_product( id number(9,0) primary key, name nv ...
随机推荐
- cdev_系列函数
内核中每个字符设备都对应一个 cdev 结构的变量,下面是它的定义: linux-2.6.22/include/linux/cdev.h struct cdev { struct kobject ...
- 【python】 web开发入门
进入Web开发 现在你完成了Python忍者训练,准备深入Ptyhon的Web开发,但现在的问题是有很多的框架,从中选择最好的框架非常困难,但从初学者的角度出发,Flask基本Web框架将非常适合We ...
- DB天气app冲刺二阶段第四天
今天就进度来说没有丝毫进度..考虑直接把数据库文件弄到代码里.因为每次挑选城市的时候都有时会出bug ,所以我想明天试一下看看是不是这个的问题,虽然工程量有点大,但是应该不困难,所以明天试一下需要. ...
- 为什么使用long声明和double声明得到的结果不一样呢?
为什么使用long声明和double声明得到的结果不一样呢? 程序如下: public class P376{ public static void main(String[] atgs){ long ...
- 【转】解析Java finally
下文写的关于Java中的finally语句块什么时候执行的问题.什么时候执行呢?和return.continue.break.exit都有关系,尤其return语句非常有意思,于是分享给大家.谢谢Sm ...
- nginx 如何显示真实ip
nginx做反向代理显示在后台访问的真实ip总是显示127.0.0.1 只要添加如下内容: proxy_set_header Host $host; proxy_set_header X-For ...
- python 链接hive
http://blog.csdn.net/xubcing/article/details/8350287 http://www.centoscn.com/python/2014/0921/3801.h ...
- 为什么数据可以从pl/sql查出来而使用ado.net查询,结果却是空?
1.背景 一条记录(如select * from A where a='1'),使用pl/sql作为条件可以查询出记录,但用ado.net sql查询结果却是空. 2.原因 a字段的数据类型的char ...
- [C/CPP系列知识] C++中extern “C” name mangling -- Name Mangling and extern “C” in C++
http://www.geeksforgeeks.org/extern-c-in-c/ C++函数重载(function overloading),但是C++编译器是如何区分不同的函数的呢?----是 ...
- Mysql忘记密码修改密码
问题重现(以下讨论范围仅限Windows环境): C:\AppServ\MySQL> mysql -u root -p Enter password: ERROR 1045 (28000): A ...