Android 简明教程
Android - RenderScript
本章内容将介绍 Android RenderScript。通常情况下,Android 应用程序的设计旨在消耗尽可能少的资源。但是,某些应用程序(如一些 3D 游戏)需要在 Android 上执行高级处理。
In this chapter, we will learn about Android RenderScript. Usually the apps on android are designed as to consume as minimum resources as possible. But some applications like some 3D games need high level processing on android.
为了向这些应用程序提供高性能,Android 引入了 RenderScript。它是一个基于 Android 的框架,用于运行执行非常高计算任务的应用程序。该框架上的开发是在 Android 提供的 Native Development Kit(NDK) 中完成的。RenderScript 对于执行以下类型操作的应用程序非常有用 -
To provide these applications high performance android introduced the RenderScript. It is android based framework which is used for running applications that perform very highly computational tasks. The development on this framework is done in Native Development Kit(NDK) provided by android. RenderScript is extremely useful for applications which performs following types of actions −
-
3D Rendering
-
Image Processing
-
Computational Photography
-
Computer Vision
How RenderScript Works
RenderScript 框架基本上基于数据并行计算。它将您的应用程序工作负载分布到设备上可用的所有处理器上,例如多核 CPU 或 GPU。
RenderScript framework is basically based on data parallel computation. It distributes your application workload on all the processors available on your device like multi-core CPUs or GPUs.
这种并行工作负载分布使程序员摆脱了负载平衡和工作调度的紧张感。您可以为您的应用程序编写更详细、更复杂的算法,而无需担心计算能力。
This parallel distribution of workload frees the programmer from the tension of load balancing and work scheduling. You can write more detailed and complex algorithms for your app without the worry of computational power.
How to Begin
要使用 RenderScript 框架,您必须具备以下两项 -
To use the RenderScript Framework you must have following two things −
-
A RenderScript Kernel
-
RenderScript APIs
A RenderScript Kernel
内核是一个管理数据处理指令并管理中央处理器单元上工作负载的程序。内核是操作系统的一个基本部分。
A kernel is a program which manages data processing instructions and manage workload on Central Processing Units.A kernel is a fundamental part of the operating system.
类似地,为运行 RenderScript 框架,我们需要一个名为 Kernel 的书面脚本,以管理来自我们的应用程序的所有数据处理请求并利用 NDK 提供的 Android 操作系统更多功能,并且如上所述,RenderScript 的开发是在 Android 的 Native Development Kit 中完成的。
Similarly to run the RenderScript framework we need a written script named as Kernel to manage all the data processing requests from our app and utilize more features of the android OS provided by the NDK and as mentioned earlier that the development of RenderScript is done in the Native Development Kit of Android.
内核脚本使用 C 语言的 C-99 标准编写。此标准早于 C++ 的开发。RenderScript 内核脚本文件通常放置在 .rs 文件中。每个文件都称为脚本。RenderScript 内核脚本可以包含以下元素 -
The Kernel Script is written in C-99 standard of C-language. This Standard was before the development of C++. A RenderScript kernel script file usually placed in .rs file. Each file is called as a script. A RenderScript Kernel script can contain following elements −
Sr.No |
Elements & Description |
1 |
A Language declaration It declares the version of RenderScript Kernel language used in this script. |
2 |
A package declaration This declaration names the package name of the Java class which will be affected by this Kernel Code. |
3 |
Invokable functions You can call these invokable functions from your JAVA code with arbitrary arguments. |
4 |
Script Global Variables These are just like the variables defined in C and C++ programming language. You can access these variables from your JAVA code. |
以下是内核的示例代码 -
Following is the Sample Code of a Kernel −
uchar4 __convert__((kernel)) invert(uchar4 in, uint32_t x, uint32_t y) {
uchar4 out = in;
out.r = 255 - in.r;
out.g = 255 - in.g;
return out;
}
RenderScript APIs
如果您想在您的 API 中使用 RenderScript,可以通过以下两种方式进行 -
If you want to use RenderScript in your API, you can do it in following two ways −
Sr.No |
APIs & Description |
1 |
android.renderscript This API is available on devices running Android 3.0 and higher. |
2 |
android.support.v8.renderscript This API is available on devices running Android 2.2 and higher. |
对于 Android 支持库,需要以下工具 -
To android support library following tools are required −
-
Latest Android SDK Tools version
-
Latest Android SDK Build-tools version
How to use RenderScript Support Library
首先打开项目中的 project.properties 文件,并在文件中添加以下行-
First Open the project.properties file in your project and add following lines in the file −
renderscript.target=18
renderscript.support.mode=true
sdk.buildtools=18.1.0
现在打开使用RenderScript的主类,并添加对支持库类的导入,如下所示-
Now open your main class which use RenderScript and add an import for the Support Library classes as following −
import android.support.v8.renderscript.*;
以下是我们在 project.properties 文件中添加的上述属性的目的。
Following are the purposes of above mentioned properties that we add in the project.properties file.
Sr.No |
Project properties & Description |
1 |
renderscript.target It specifies the byte code version to be generated. |
2 |
renderscript.support.mode It specifies a compatible version for the generated byte code to fall back. |
3 |
sdk.buildtools It Specifies the versions of Android SDK build tools to use. |
现在调用RenderScript Kernel函数,并在应用程序中计算复杂算法。
Now call your RenderScript Kernel functions and compute complex algorithms in your app.