January 13, 2020

Implementing Intrinsic Renderscripts in Xamarin.Android

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:

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 Allocations. 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,

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. ForEachwill 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.SetRadius(25);  // Set a pamaeter

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.

https://docs.microsoft.com – https://developer.android.com

Leave a Reply

Your email address will not be published. Required fields are marked *