* This articles applies for Xamarin.Android applications that target API level 17 or higher
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 workloads can benefit as well. The RenderScript runtime parallelizes work across processors available on a device, such as multi-core CPUs and GPUs. This allows you to focus on expressing algorithms rather than scheduling work. RenderScript is especially useful for applications performing image processing, computational photography, or computer vision.
Using Intrinsic Renderscripts in Xamarin.Android
The intrinsic scripts are a great way to perform intensive computing tasks with a minimal amount of additional code. They have been hand tuned to offer optimal performance on a large cross section of devices. It is not uncommon for an intrinsic script to run 10x faster than managed code and 2-3x times after than a custom C implementation. Many of the typical processing scenarios are covered by the intrinsic scripts. This list of the intrinsic scripts describes the current scripts in Xamarin.Android:
- ScriptIntrinsic3DLUT – Converts RGB to RGBA using a 3D lookup table.
- ScriptIntrinsicBLAS – Provideshigh performance Renderscript APIs to BLAS. The BLAS (Basic Linear Algebra Subprograms) are routines that provide standard building blocks for performing basic vector and matrix operations.
- ScriptIntrinsicBlend – Blends two Allocations together.
- ScriptIntrinsicBlur – Applies a Gaussian blur to an Allocation.
- ScriptIntrinsicColorMatrix – Applies a color matrix to an Allocation (i.e. change colours, adjust hue).
- ScriptIntrinsicConvolve3x3 – Applies a 3×3 color matrix to an Allocation.
- ScriptIntrinsicConvolve5x5 – Applies a 5×5 color matrix to an Allocation.
- ScriptIntrinsicHistogram – An intrinsic histogram filter.
- ScriptIntrinsicLUT – Applies a per-channel lookup table to a buffer.
- ScriptIntrinsicResize – Script for performing the resize of a 2D allocation.
- ScriptIntrinsicYuvToRGB – Converts a YUV buffer to RGB.
Please consult the API documentation for details on each of the intrinsic scripts.
The basic steps for using Renderscript in an Android application are described next.
Create a Renderscript Context – The Renderscript
class is a managed wrapper around the Renderscript context and will control initialization, resource management, and clean up. The Renderscript object is created using the RenderScript.Create
factory method, which takes an Android Context (such as an Activity) as a parameter. The following line of code demonstrates how to initialize the Renderscript context:C#Copy
Android.Renderscripts.RenderScript renderScript = RenderScript.Create(this);
Create Allocations – Depending on the intrinsic script, it may be necessary to create one or two Allocation
s. The Android.Renderscripts.Allocation
class has several factory methods to help with instantiating an allocation for an intrinsic. As an example, the following code snippet demonstrates how to create Allocation for Bitmaps.C#Copy
Android.Graphics.Bitmap originalBitmap;
Android.Renderscripts.Allocation inputAllocation = Allocation.CreateFromBitmap(renderScript,
originalBitmap,
Allocation.MipmapControl.MipmapFull,
AllocationUsage.Script);
Often, it will be necessary to create an Allocation
to hold the output data of a script. This following snippet shows how to use the Allocation.CreateTyped
helper to instantiate a second Allocation
that the same type as the original:C#Copy
Android.Renderscripts.Allocation outputAllocation = Allocation.CreateTyped(renderScript, inputAllocation.Type);
Instantiate the Script wrapper – Each of the intrinsic script wrapper classes should have helper methods (typically called Create
)for instantiating a wrapper object for that script. The following code snippet is an example of how to instantiate a ScriptIntrinsicBlur
blur object. The Element.U8_4
helper method will create an Element that describes a data type that is 4 fields of 8-bit, unsigned integer values, suitable for holding the data of a Bitmap
object:C#Copy
Android.Renderscripts.ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.Create(renderScript, Element.U8_4(renderScript));
Assign Allocation(s), Set Parameters, & Run Script – The Script
class provides a ForEach
method to actually run the Renderscript. This method will iterate over each Element
in the Allocation
holding the input data. In some cases, it may be necessary to provide an Allocation
that holds the output. ForEach
will overwrite the contents of the output Allocation. To carry on with the code snippets from the previous steps, this example shows how to assign an input Allocation, set a parameter, and then finally run the script (copying the results to the output Allocation):C#Copy
blurScript.SetInput(inputAllocation);
blurScript.SetRadius(25); // Set a pamaeter
blurScript.ForEach(outputAllocation);
You may wish to check out the Blur an Image with Renderscript recipe, it is a complete example of how to use an intrinsic script in Xamarin.Android.
Ref
https://docs.microsoft.com – https://developer.android.com