高难度(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 ...
随机推荐
- java & xml parser
参考: JDK8 API: http://docs.oracle.com/javase/8/docs/api/ DOM: http://www.w3.org/TR/2004/REC-DOM-Level ...
- Encog
http://www.heatonresearch.com/encog/ https://www.mql5.com/zh/articles/252
- Week1 Team Homework #1: Study the projects done by previous student groups
我们研究了学长的项目:百度3D地图API的调用.下面是我们对该项目的一些看法: 优点: 界面清晰 各类之间调用及其他关系容易理清. 缺点: 前段html代码过于冗杂,很多(div)块间的层次关系不 ...
- 简单制作mib表
今天放假后第一天上班,将假前自学制作mib表的东西说一下. 在这里呢,我以世界-中国-上海-闵行这种包含关系介绍,感觉更容易理解. MIB file的开始和结束 所有的MIB file的都以DEFIN ...
- 解决在ubuntu下requests 无法找到模块packages
我明明用pip install requests安装成功了,但是依然报下面的错 错误1 requests.packages.urllib3.disable_warnings()AttributeErr ...
- ORA-12505, TNS:listener does not currently know of SID given in connect descriptor
引子: 本项目在老电脑上用的是oracle10g,换新电脑装的是oracle11g,但运行项目本没有什么关系,本来说创建个用户,用PLSQL手工导入数据,再改几下配置文件即可跑起来--但实际启动中遇到 ...
- QC缺陷管理操作-细说(转)
一.缺陷常用字段说明 二.缺陷管理流程图 三.开发人员修改缺陷填写规范 四.项目经理决定延期修改缺陷 一.缺陷常用字段说明 1.摘要 对缺陷的简单描述.摘要包括该缺陷所属的模块名称-子模块名称,以及简 ...
- contentType,pageEncoding,charset,setCharacterEncoding
contentType,pageEncoding,charset,setCharacterEncoding HTML中的meta标签,以及HTTP Header. -------------字符集.字 ...
- (转)开源爬虫larbin分析
转自风中之炎的博客:http://www.cnblogs.com/FengYan/archive/2012/02/04/2338630.html 1. larbin简介(百度百科) larbin是一种 ...
- POJ 1785 Binary Search Heap Construction(裸笛卡尔树的构造)
笛卡尔树: 每个节点有2个关键字key.value.从key的角度看,这是一颗二叉搜索树,每个节点的左子树的key都比它小,右子树都比它大:从value的角度看,这是一个堆. 题意:以字符串为关键字k ...