高难度(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
Elementwithin 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 theinargument and its type. In a RenderScript kernel, this is a special argument that is automatically filled in based on the inputAllocationpassed to the kernel launch. By default, the kernel is run across an entireAllocation, with one execution of the kernel body perElementin 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 theElementtypes 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_allocationscript 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, andzarguments. 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_relaxedalong 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.gradlefile 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 setrenderscriptSupportModeEnabledtotrue. 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.0or 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
RenderScriptcontext, 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
Allocationto be passed to a script. AnAllocationis a RenderScript object that provides storage for a fixed amount of data. Kernels in scripts takeAllocationobjects as their input and output, andAllocationobjects can be accessed in kernels usingrsGetElementAt_type()andrsSetElementAt_type()when bound as script globals.Allocationobjects allow arrays to be passed from Java code to RenderScript code and vice-versa.Allocationobjects 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.rsand 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 thecopymethods inAllocation. - Set any necessary script globals. Globals may be set using methods in the same
ScriptC_filenameclass with methods namedset_globalname. For example, in order to set anintnamedelements, use the Java methodset_elements(int). RenderScript objects can also be set in kernels; for example, thers_allocationvariable namedlookupcan be set with the methodset_lookup(Allocation). - Launch the appropriate kernels. Methods to launch a given kernel will be reflected in the same
ScriptC_filenameclass 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.LaunchOptionsas the last argument to theforEachmethod.Invoked functions can be launched using the
invoke_functionnamemethods reflected in the sameScriptC_filenameclass. - Copy data out of
Allocationobjects. In order to access data from anAllocationfrom Java code, that data must be copied back to Java buffers using one of thecopymethods 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 ...
随机推荐
- 我应该直接学Swift还是Objective-C?
当我们发布了Swift语言学习课程之后,收到了很多邮件和私信来问自己是否还需要学习C或者Objective-C.此外,人们似乎还在迷惑Swift到底适合iOS开发生态中的哪些部分.通过这篇文章,我希望 ...
- 【学习总结】声明变量在@interface括号中与使用@property的区别
方式一:直接在.h文件@interface中的大括号中声明. @interface Test : NSObject { NSString *str; // 私有变量 , 其他类无法访问,只能够该类内部 ...
- C与C++存储空间布局
每个程序一启动都有一个大小为4GB的内存,这个内存叫虚拟内存,是概念上的,真正能用到的,只是很小一部分,一般也就是在几百K到几百M.我们PC中内存,我们称之为物理内存,也就是256M,512M等,虚拟 ...
- sql2008 计划自动创建数据库分区【转】
本文转自:http://jingyan.baidu.com/article/6b97984d9a26ec1ca3b0bf77.html sql2008 计划自动创建数据库分区 固定增量的数据,自动创建 ...
- 【BZOJ】【1877】【SDOI2009】晨跑
网络流/费用流 费用流入门题……根本就是模板题好吗! 拆点搞定度数限制,也就是每个点最多经过一次……源点汇点除外. /***************************************** ...
- jquery(1.3.2)<--json-->spring(3.0)
发现spring 3已经对ajax支持的很好了,前端可以只使用html+jquery,后端 只使用spring再加上一种orm,两者之间用json交换数据就可以了,现在是放弃 jsp,struts这些 ...
- memcached-repcached
memcached的复制功能 下载对应的repcached版本:http://sourceforge.jp/projects/sfnet_repcached/,必须版本对应才行 当前只支持到1.2.8 ...
- D&F学数据结构系列——二叉堆
二叉堆(binary heap) 二叉堆数据结构是一种数组对象,它可以被视为一棵完全二叉树.同二叉查找树一样,堆也有两个性质,即结构性和堆序性.对于数组中任意位置i上的元素,其左儿子在位置2i上,右儿 ...
- javascript背景淡入淡出
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- django如何用orm增加manytomany关系字段(自定义表名)
不自定义表名的,网上有现成的,但如果自定义之后,则要变通一下了. app_insert = App.objects.get(name=app_name) site_insert = Site.obje ...