高难度(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 ...
随机推荐
- IOS开发一些资源收集
从别的地方看到的,多谢作者,现贴在这里备忘. 在线教程 iOS技术概述 我个人感觉这是开始iOS开发第一步,了解一下iOS整体的结构,有哪些支持类库.如何使用类库等等一切基础的概念性指示 iPh ...
- IOS 控件的生命周期
ViewController的生命周期包括: Initialize ViewDidLoad ViewWillAppear ViewDidAppear ViewWillDisappear ViewDid ...
- mysql绿色版安装问题解决(ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (10061))
原来一直是使用MySQL安装版没有出现过问题,今天在安装绿色版MySQL时出现了点问题 在安装成windows服务成功后,用net start mysql 启动时提示启动成功,但当我连接mysql就报 ...
- iOS 进阶 第三天(0326)
0326 Xib:描述软件界面,轻量级,一般用来描述局部界面 Xib的获取,如下图所示: xib在我们开发的时候叫xib,但如果运行在我们手机里会生成nib.所以xib和nib两种方法来访问xib
- Windows程序消息机制浅析
1.消息 消息是由MSG结构体来表示的.如下: typedef struct tagMSG { HWND hwnd; UINT message; WPARAM wParam; LPARAM lPar ...
- python pip和easy_install使用方式(转载)
easy_install 跟 pip 都是Python 的套件管理程式,有了它们,在使用 Python 开发程式的时候会带来不少方便. easy_install 和pip 有什麼不一样?据 pip 官 ...
- 2433: [Noi2011]智能车比赛 - BZOJ
Description 新一届智能车大赛在JL大学开始啦!比赛赛道可以看作是由n个矩形区域拼接而成(如下图所示),每个矩形的边都平行于坐标轴,第i个矩形区域的左下角和右上角坐标分别为(xi,1,yi, ...
- mysql Host ‘XXXXXX’ is blocked because of many connection errors
mysql Host ‘XXXXXX’ is blocked because of many connection errors ERROR 1129 (00000): Host ‘XXXXXX’ i ...
- struts2多文件上传(带进度条)demo+说明
利用plupload插件实现多文件上传,实现图片: 在jsp写入js代码: z<%@ page language="java" contentType="text/ ...
- 在线最优化求解(Online Optimization)之二:截断梯度法(TG)
在线最优化求解(Online Optimization)之二:截断梯度法(TG) 在预备篇中我们做了一些热身,并且介绍了L1正则化在Online模式下也不能产生较好的稀疏性,而稀疏性对于高维特征向量以 ...