1

I have a very simple app with a textureview and renderscript code that draws red pixels on the textureview. This works as expected on all test devices except for Huawei P20. Could this be related to how the hardware handles this? Any help to point me to the right direction to find a workaround is appreciated.

Below you can see the expected results (on the left) vs. results on P20 (on the right):

enter image description here

Here is the code in my activity:

class MainActivity : AppCompatActivity() {

    private val handlerThread = HandlerThread("processing").also { it.start() }
    private val handler = Handler(handlerThread.looper)
    private lateinit var runnable: Runnable

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val textureView = findViewById<TextureView>(R.id.textureView)
        if(textureView.isAvailable) {
            start(textureView.width, textureView.height)
        } else {
            textureView.surfaceTextureListener = object : TextureView.SurfaceTextureListener {
                override fun onSurfaceTextureSizeChanged(surface: SurfaceTexture?, width: Int, height: Int) {
                    TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
                }

                override fun onSurfaceTextureUpdated(surface: SurfaceTexture?) {
                }

                override fun onSurfaceTextureDestroyed(surface: SurfaceTexture?): Boolean {
                    TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
                }

                override fun onSurfaceTextureAvailable(surface: SurfaceTexture?, width: Int, height: Int) {
                    start(width, height)
                }
            }
        }
    }

    private fun start(width: Int, height: Int) {
        val rsContext = RenderScript.create(this, RenderScript.ContextType.DEBUG)
        val script = ScriptC_script(rsContext)
        val outputAllocation = Allocation.createTyped(
            rsContext,
            Type.Builder(rsContext, Element.RGBA_8888(rsContext)).apply {
                setX(width)
                setY(height)
            }.create(),
            Allocation.USAGE_IO_OUTPUT or Allocation.USAGE_SCRIPT
        )

        outputAllocation.surface = Surface(textureView.surfaceTexture)

        runnable = Runnable {
            script.forEach_drawRed(outputAllocation)
            outputAllocation.ioSend()
            handler.postDelayed(runnable, 100)
        }

        handler.post(runnable)
    }

    override fun onStop() {
        handler.removeCallbacks(runnable)
        handlerThread.quit()
        super.onStop()
    }
}

And here is the renderscript code:

#pragma version(1)
#pragma rs java_package_name(com.example.myapplication)
#pragma rs_fp_relaxed

uchar4  __attribute__((kernel)) drawRed(uint32_t x, uint32_t y) {
    uchar4 pixel;
    pixel.r = 255;
    pixel.g = 0;
    pixel.b = 0;
    pixel.a = 255;
    return pixel;
}
Ehsan Khaveh
  • 1,444
  • 17
  • 21

0 Answers0