在华为P20上的Textureview上绘制时出现问题

时间:2019-06-03 10:25:02

标签: android renderscript android-textureview

我有一个非常简单的应用程序,带有textureview和renderscript代码,该代码在textureview上绘制红色像素。除华为P20之外,此功能均可在所有测试设备上正常运行。这可能与硬件处理方式有关吗?感谢您为我找到正确的解决方法提供帮助。

在下面您可以看到预期的结果(在左侧)与在P20上的结果(在右侧):

enter image description here

这是我活动中的代码:

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()
    }
}

这是渲染脚本代码:

#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;
}

0 个答案:

没有答案