RenderScript not generating output

0

I'm new to RenderScript on Android and I'm trying to set up the most basic "hello world" kind of usage of RenderScript to process a bitmap image. This minimal example is supposed to just set the output bitmap to a solid color (red) and ignore the input image.

Here is my Kotlin code that is called from my MainActivity.

        var rs = RenderScript.create(this)
        val script = ScriptC_simple_effect(rs)

        val conf = Bitmap.Config.ARGB_8888
        val width = 2
        val height = 2
        val inbmp = Bitmap.createBitmap(width, height, conf)
        val outbmp = Bitmap.createBitmap(width, height, conf)

        var inAllocation = Allocation.createFromBitmap(rs, inbmp)
        var outAllocation = Allocation.createFromBitmap(rs, outbmp)
        
        inAllocation.copyFrom(inbmp)
        script.forEach_makeRed(inAllocation, outAllocation)
        outAllocation.copyTo(outbmp)

        val pixels = IntArray(width * height)
        outbmp.getPixels(pixels, 0, outbmp.width, 0, 0, width, height)

And here is the simple_effect renderscript code

#pragma version(1)
#pragma rs java_package_name(com.xxxxx.yyyyy.zzzzz) // redacted

uchar4 RS_KERNEL makeRed(uchar4 in)
{
    uchar4 out;
    out.r = 255;
    out.g = 0;
    out.b = 0;
    out.a = 255;
    return out;
}

build.gradle includes the following in the android defaultConfig section.

        renderscriptTargetApi 18
        renderscriptSupportModeEnabled true

I was expecting to see my final output bitmap contain all RED colored pixels.

Actually results are that all the Ints of the output pixels IntArray are 0 when running in the emulator or are all -1 (0xFFFFFFFF) when running on an actual device. I get no run-time errors or warnings.

What did I do wrong? Why am I not getting output?

(Can you tell from this code? If the problem might be elsewhere, please let me know what additional information might be required.)

Edit: Update

It turns out that if I include in my build.gradle:

renderscriptSupportModeEnabled true

... and I use these imports:

import android.graphics.Bitmap
import android.graphics.Color
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.renderscript.Allocation
import androidx.renderscript.RenderScript

... then it doesn't work correctly. The output is just white.

But if I remove the renderscriptSupportModeEnabled declaration, and change the renderscript imports from androidx. to just android., then it does work correctly and the output is red as intended.

import android.graphics.Bitmap
import android.graphics.Color
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import android.renderscript.Allocation
import android.renderscript.RenderScript

I'm confused as to why. Can anyone explain?

android
renderscript
asked on Stack Overflow Oct 5, 2020 by Wyck • edited Oct 7, 2020 by Wyck

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0