Android API Guides---renderscript

Source: Internet
Author: User
Tags function prototype android sdk manager

Renderscript

Renderscript is the framework for high performance running compute-intensive tasks on Android. Renderscript is primarily intended for use with data parallel computing, although serial compute-intensive workloads can benefit. The Renderscript runtime will work in parallel with all available processors on this multicore CPU,GPU or DSP device, allowing you to focus on the expression algorithm rather than scheduling work or load balancing. Renderscript is especially useful for image processing, computational photography or computer vision applications.


First Renderscript, there are two main concepts that you should understand:


The high-performance computing kernel writes a C99-derived language.
A Java API is used to manage the lifetime of renderscript resources and to control kernel execution.
Write a renderscript kernel


A renderscript kernel typically resides in a file in the <project_root>/src directory/directory. RS, each. The RS file is called a script. Each script contains its own set of kernels, functions, and variables. The script can contain:


The compiler directive declaration (version #pragma (1)) declares the version of the Renderscript kernel language used in this script. Currently, 1 is the only valid value.
A compilation instruction declares that this script reflects the package name of the Java class (#pragma RS java_package_name (Com.example.app)). Please note that your. rs file must be part of the application package in the library project, not.
Some number of callable functions. The callable function is a single-threaded renderscript function that you can invoke from any parameter in your Java code. These are often useful for initial setup or serial calculations within a larger processing pipeline.
There are global variables for some number scripts. Global scripting equals C. Scripts can be accessed from Java code globally with a global variable, which is often used to pass to the Renderscript kernel parameter.
Some numbers compute the kernel. The kernel is a parallel function that allocates every element in the entire scope to execute.
A simple kernel might look like the following

UCHAR4 __attribute__((Kernel))Invert(UCHAR4inch,uint32_t x,uint32_t y) {UCHAR4 out = inch;   out.R= 255 - inch.R;   out.g= 255 - inch.g;   out.b= 255 - inch.b;  return  out;}
In many ways, this equates to a standard C function. The first notable feature is the function prototype __attribute __ ((kernel)). This means that the function is a renderscript kernel, not a callable function. The next feature is the parameters and their types. In the Renderscript kernel, this is automatically populated based on a special parameter passed to the kernel boot input assignment. By default, the kernel runs throughout the allocation, and each element is executed in an allocation kernel. A third notable feature is the return type of the kernel. The values returned from the kernel are automatically written to the appropriate location for the output assignment. The Renderscript runtime checks to ensure that the input and output of the element type are partitioned with the kernel's prototype, and throws an exception if they do not match.
The kernel may have an input assignment, an output assignment, or both. The kernel may not have more than one input or one output allocation. If more than one input or output is required, these objects should be bound to the Rs_allocation script globally and accessed via Rsgetelementat_type () or Rssetelementat_type () from a kernel or callable function.
The kernel can access the coordinates currently executing using the X, y, and Z parameters. These parameters are optional, but the type of the coordinate parameter must be uint32_t.
The optional init () function. An init () function is a special type of callable function that runs when the script is first instantiated. This enables some calculations to be performed automatically in the script creation.
Some numbers are static script global variables and functions. The global static script is equivalent to just that it cannot set global scripts from Java code. A static function is a standard C function that can be called from any kernel or callable function in the script, but does not expose the Java API. If the script is global or functional and does not need to be called from Java code, it is strongly recommended that those be declared as static.
Set floating point precision


You can control?? Floating-point precision is required at the level of the script. This is useful if you do not need the full IEEE 754-2008 standard, which is used by default. The following compilation indicates that different levels of floating-point precision can be set:


#pragma rs_fp_full (if not specified by default): For applications that require floating-point precision as listed by the IEEE 754-2008 standard.
#pragma rs_fp_relaxed-for applications that do not require strict IEEE 754-2008 compliance and are not highly tolerant to accuracy requirements. This mode can flush the denorms to zero and round zero.
#pragma rs_fp_imprecise-for applications that do not have strict precision requirements. This mode conforms to all of the following rs_fp_relaxed:
The resulting-0.0 operation can return +0.0 instead.
The INF and NAN operations are not deterministic.
Most applications can use rs_fp_relaxed without any?? Any side effects. This may be very advantageous in some architectures due to the additional optimizations that are only applicable to loose precision (such as SIMD CPU instructions).


Accessing the Renderscript API


When developing an Android application using Renderscript, you can access its APIs in one of the following two ways:


Android.renderscript-The API in this class package can run Android 3.0 (API level 11) and later devices.
Android.support.v8.renderscript-the API in this package is available through a support library that allows you to use them to run Android 2.2 (API Level 8) and later devices.
We strongly recommend that you use the Support library API to access Renderscript because they offer a wider range of device compatibility. If it is necessary for the developer of Android? A specific version of the OID can use Android.renderscript.


Using the Renderscript Support Library API


In order to use APIs that support library Renderscript, the development environment must be configured to have access to them. The following Android?? The OID SDK tools all need to use these APIs:


Android SDK Tools version 22.2 or later
Android SDK built-in Tools version 18.1.0 or later
You can check and update the installation versions of these tools in the Android SDK manager.


To use an API that supports library Renderscript:


Please make sure you have the required Android?? The OID SDK version and build tool installed version.
The update for Android build process includes settings for Renderscript settings:
Built for Android studio or Cradle based
Open the Build.gradle file in the application folder of the application module.
Add the following renderscript settings to the file:

Android{compilesdkversion +buildtoolsversion"19.0.3"Defaultconfig{minsdkversion8targetsdkversion - renderscripttargetapi    renderscriptsupportmodeenabled  true    }}
The settings listed above in the specific behavior of the Android build Process Control:
RENDERSCRIPTTARGETAPI-Specifies the version of the bytecode to generate. We recommend that you set this value to the highest API level and set that renderscriptsupportmodeenabled is really available. The valid value for this setting is 11 of any integer to the most recently published API level. If the minimum SDK version specified in your application manifest is set to a different value, the value is ignored, and the target value in the makefile is the minimum SDK version that is used to set it.
Renderscriptsupportmodeenabled-Specifies that the generated bytecode should fall back to a compatible version if it is running on a device that does not support the target version.
Buildtoolsversion--the android?? The build tool version of the OID SDK is used. This value should be set to 18.1.0 or higher. If this option is not specified, the version of the tool with the highest installed version is used. You should always set this value to ensure consistency across different configurations of machines spanning development.
For Eclipse
Open the Project.Properties file in the root folder of the application project.
Add the following lines to the file:

Renderscript.Target= -Renderscript. Support.Mode=trueSDK.Buildtools=18.1.0
The settings listed above in the specific behavior of the Android build Process Control:
Renderscript.target-Specifies the version of the bytecode to generate. We recommend that you set this value to the highest available API level and set Renderscript.support.mode to True. The valid value for this setting is 11 of any integer to the most recently published API level. If the minimum SDK version specified in your application manifest is set to a higher value, this value is ignored and the target value is set to the minimum SDK version.
Renderscript.support.mode-Specifies that the generated bytecode should fall back to a compatible version if it is running on a device that does not support the target version.
The Sdk.buildtools-android SDK version Builder tool to use. This value should be set to 18.1.0 or higher. If this option is not specified, the version of the tool with the highest installed version is used. You should always set this value to ensure consistency across different configurations of machines spanning development.
In using Renderscript your application class, add an imported support Library class:

Import Android .  Support . V8 . Renderscript .*;
Using Renderscript from Java code


Using renderscript from Java code relies on the API classes located in the Android.renderscript or Android.support.v8.renderscript package. Most applications follow the same basic usage pattern:


Initializes the Renderscript context. The Renderscript context, created by creation (context), ensures that renderscript can be used and provides a purpose to control the lifetime of all subsequent Renderscript objects. You should consider context creation to be a potentially long-running operation because it can create different hardware resources; it should not be a critical path in the application, if at all possible. Typically, an application will have a Renderscript context only in a single time.
Create at least one script to be passed to the assignment. An assignment is a Renderscript object that provides storage for a fixed amount of data. The kernel in the script takes the allocation object as their input and output, and the allocation object can be accessed as a script global constraint when the kernel uses Rsgetelementat_type () and Rssetelementat_type (). The allocation object allows the array to be passed from Java code to the Renderscript code, and vice versa. Allocation objects are typically created using createtyped (Renderscript, type) or Createfrombitmap (Renderscript, bitmaps).
It is necessary to create any script. There are two types of scripts available to you when using Renderscript:
SCRIPTC: These are the user-defined scripts that are written in the Renderscript kernel. Each script has a Java class that has a renderscript compiler to make it easy to access from Java code scripts, and this class will name Scriptc_filename. For example, if the kernel is located above invert.rs and a renderscript context is already in Mrs,java code to instantiate the script will be:

Scriptc_invert=newscriptc_invert (  Mrenderscript);
Scriptintrinsic: These built-in renderscript cores work together, such as Gaussian blur, convolution, and image blending. For more information, see Subclasses of Scriptintrinsic.
Populate allocations with data. In addition to creating an assignment with Android.renderscript, the assignment will be populated with empty data when it is first created. To populate the allocation, use one of the assigned replication methods.
Set any necessary script global variables. The global can be set using the same Scriptc_filename class as the method named Set_globalname. For example, to set an int named element, use the Java method set_elements (int). Renderscript objects can also be set in a kernel set, for example, a variable named find Rs_allocation can be configured with method Set_lookup (Assignment).
Launch the appropriate kernel. The method introduces a foreach kernel name () that a given kernel will reflect in the same Scriptc_filename class named method. These launches are asynchronous and start to serialize in the order in which they are launched. Depending on the parameters passed to the kernel, the method takes one or two allocations. By default, the kernel performs an entire input or output assignment, executes a subset of the allocation, and passes the appropriate script.launchoptions as the last parameter to the Foreach method.
The functions that are called can be introduced using the same Scriptc_filename class Invoke_functionname method.
The data allocation object that was copied. In order to allocate access data from Java code, the data must be replicated back to the Java buffer with one of the allocated replication methods. These features will be necessary with the introduction of asynchronous synchronous kernels and features.
The renderscript context is torn down. The Renderscript context can be destroyed by using the Destroy () method, or by allowing garbage collection Renderscript context objects. This will cause any further use of the thrown exception that belongs to any object in this respect.

Android API Guides---renderscript

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.