I released a video for the AsyncAndroid 2020 and this article contains some additional information on the subject matter of that video. I would strongly suggest that you give it a watch first. There’s also some great content from other folks, so please drop by – it’s all free content.
First things first, I thought I should add some links to those odd sounding blog posts in the introduction. The first Dude, Where’s My Button is about weird button behaviour caused by elevation, and the second Irregular Shapes – Part 4 is where we put dogs in love hearts.
With that silliness out of the way, let’s focus on some additional information around the main subject matter of the video. Firstly it is worth talking about the outputBitmap
in the sample code. While the code disposes of everything else that gets created by the RenderscriptImageProcessor
class, this is one thing which doesn’t get cleaned up. The reasoning behind this is that this object is returned from the processImage()
function and we have no way of knowing if an external actor may still be using it.For that reason there is no explicit clean up of the image, but we do clean up all of the object which are only used internally by the class – the allocations, script, and RenderScript context.
The current implementation of this may actually cause some issues because the outputBitmap
is reused if the size of the source bitmap does not change. If something is using the bitmap returned by a previous invocation of processImage()
then the function again with an image of the same dimensions will cause the image to be updated. For such cases, it might be better to return a new Bitmap
object each time that processImage()
is invoked.
Another thing worth mentioning is that there are some RenderScript scripts that are actually supplied for us. With these we can perform various operations such as blurring, blending, and applying convolution matrices. Check out the subclasses of ScriptIntrinsic
to see these intrinsic scripts. Further to this, I have written blog posts in the past which cover the use of some of these. Specifically check out the series on Blurring Images for examples of using ScriptIntrinsicBlur
; and check out Image Convolution: Implementation for examples of using ScriptIntrinsicConvolve3x3
and ScriptIntrinsicConvolve5x5
.
For those who have now got a taste for RenderScript and wish to explore custom kernels further, I have a couple of projects where we do something a little more complex than the image inversion that we do in the video.
Firstly there is the series on rendering a colour wheel initially we do it on the JVM but then switch to RenderScript for performance gains. There is also a series on rendering the Mandelbrot Set, once again this is initially done on the JVM but we switch to a custom RenderScript kernel to offload this to the GPU.
The source code for the sample project is available here.
Happy RenderScripting!
© 2020, Mark Allison. All rights reserved.
Copyright © 2020 Styling Android. All Rights Reserved.
Information about how to reuse or republish this work may be available at http://blog.stylingandroid.com/license-information.
Github link is dead
Apologies – my mistake. It should be available now.