Open In App

Migrate From RenderScript in Android 13

Improve
Improve
Like Article
Like
Save
Share
Report

The majority of the time, Android apps are made to use as few resources as feasible. However, some Android applications, such as some 3D games, require powerful processing. RenderScript is a framework for high-performance computation-intensive job execution on Android. Although serial workloads can sometimes benefit, RenderScript is primarily designed for use with data-parallel computation. Starting from Android 12, and continuing to Android 13, the Renderscript API is now depreciated, so Google advises us to take away our apps using those methods, as they will no longer be supported in the newer Android Versions.

How Does Renderscript Work?

Work was distributed among a device’s available processors, such as multi-core CPUs and GPUs, through the RenderScript runtime. This freed up your time to concentrate on expressing algorithms rather than planning tasks. Applications that conduct image processing, computational photography, or computer vision benefitted the most from RenderScript.

Understanding how Renderscript used to work

Image #1: Understanding how Renderscript used to work

Basics Before Migrating

Before we start the migrating process itself, we should know that there are certain things that we should keep in mind before we commence the process. After RenderScript is deprecated, the intrinsics functions will still work, but they might only run on the CPU rather than the GPU. The standalone replacement library can be used if your application makes use of intrinsics; according to our tests, it performs quicker than the current RenderScript CPU implementation. The set which we have contains some great tools which range from:

  1. Convolve
  2. Blur
  3. Histogram and histogramDot
  4. Blend
  5. Color matrix
  6. Resize
  7. Lookup table (LUT) and LUT 3D

Migrating Away From the Scripts

Now as we know about the changes that will happen when we will migrate from the API, we will now see how to change our current implementation, We advise switching RenderScript scripts to the cross-platform Vulkan API to fully utilize GPU acceleration. Even if you don’t migrate, your scripts will still function, but depending on the availability of drivers, they might use the CPU rather than the GPU to run. Use Vulkan for Android 7 (API level 24 and higher) and RenderScript for Android 6 (API level 23) if your application needs to support a variety of releases. You can use Vulkan on every device you support if your minSdkVersion is 24 or higher; you are not required to use RenderScript.

Initialising the Vulkan

Use the following instructions to establish a Vulkan context with the NDK instead of creating a RenderScript context object in Java or Kotlin:

  1. Establish a Vulkan instance.
  2. Select a physical Vulkan device that can accommodate a compute queue.
  3. The compute queue can be obtained by creating a Vulkan logical device.

Step #1: Migrating from the Blur Function

If your app has some drawables in the form of a bitmap, then you can use to blur the image using:

Kotlin




// Use this code to blue stuff in your image
var gfgBlurredMap = Toolkit.blur(gfgLogo, "PASS_RADIUS")


Step #2: Blurring the array

After step #1, you can now move to blur the image using the array option, to more effectively work on them:

Kotlin




// Use this to blue using the array values in your image, pixel-by-pixel.
val gfgArray = Toolkit.blur(gfgInput, gfgInputBytes, gfgWidth, "INPUT_HEIGHT", "INPUT_RADIUS")


Creating the Vulkan Pipeline to Support the new Blur

We will now use Vulkan to create a new pipeline to support the new images which we just created in the previous steps, the steps to follow are:

  1. With the SPIR-V shader that has been compiled, make a shader module.
  2. Make a descriptor set layout with the resource bindings specified (see Allocations for more details).
  3. From the descriptor set layout, create a descriptor set.
  4. Using the pipeline structure and shader module, construct a computation pipeline.

What about your current Renderscript scripts

You must transform your RenderScript scripts into Vulkan compute shaders. Depending on how RenderScript globals are used, you might also need to modify your code.

Conclusion

RenderScript support in the Android Gradle plugin is likewise being discontinued as a result of the deprecation of RenderScript in the Android platform. We can also see that time-to-time a few APIs keep getting depreciated, and now the RenderScript APIs are no longer supported by the Android Gradle plugin 7.2. Also note that future versions of AGP will totally remove them, but they will still operate with warnings.



Last Updated : 05 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads