我有一个非常简单的应用程序,其中包含纹理视图和渲染脚本代码,可在纹理视图上绘制红色像素。这在除华为 P20 之外的所有测试设备上都按预期工作。这可能与硬件如何处理有关吗?感谢任何帮助我找到正确的方向以找到解决方法。
您可以在下面看到预期结果(左侧)与 P20 上的结果(右侧):
这是我活动中的代码:
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;
}