背景:
我想根据Android相机应用程序的代码添加实时滤镜。但是,Android相机应用程序的架构基于OpenGL ES 1.x。我需要使用着色器来自定义我们的滤镜实现。但是,将相机应用程序更新为OpenGL ES 2.0太困难了。因此,我必须找到其他方法来实现实时滤镜而不是OpenGL。经过一些研究,我决定使用RenderScript。
问题:
我已经编写了一个使用RenderScript的简单滤镜演示。结果显示它的帧率比通过OpenGL实现要低得多。大约是5fps对15fps。
问题如下:
Android官方网站表示:RenderScript运行时将并行处理所有可用设备上的处理器,例如多核CPU、GPU或DSP,使您可以专注于表达算法而不是调度工作或负载平衡。那么为什么RenderScript实现速度较慢?
如果RenderScript不能满足我的需求,有更好的方法吗?
代码细节:
嗨,我和提问者在同一个团队。我们想编写基于RenderScript的实时滤镜相机。在我们的测试演示项目中,我们使用了一个简单的滤镜:一个YuvToRGB IntrinsicScript,加上一个overlay-filter ScriptC脚本。在OpenGL版本中,我们将相机数据设置为纹理,并使用着色器进行图像滤镜处理。像这样:
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureYHandle);
GLES20.glUniform1i(shader.uniforms.get("uTextureY"), 0);
GLES20.glTexSubImage2D(GLES20.GL_TEXTURE_2D, 0, 0, 0, mTextureWidth,
mTextureHeight, GLES20.GL_LUMINANCE, GLES20.GL_UNSIGNED_BYTE,
mPixelsYBuffer.position(0));
在RenderScript版本中,我们将相机数据设置为Allocation,并使用脚本内核进行图像过滤处理。就像这样:
// The belowing code is from onPreviewFrame(byte[] data, Camera camera) which gives the camera frame data
byte[] imageData = datas[0];
long timeBegin = System.currentTimeMillis();
mYUVInAllocation.copyFrom(imageData);
mYuv.setInput(mYUVInAllocation);
mYuv.forEach(mRGBAAllocationA);
// To make sure the process of YUVtoRGBA has finished!
mRGBAAllocationA.copyTo(mOutBitmap);
Log.e(TAG, "RS time: YUV to RGBA : " + String.valueOf((System.currentTimeMillis() - timeBegin)));
mLayerScript.forEach_overlay(mRGBAAllocationA, mRGBAAllocationB);
mRGBAAllocationB.copyTo(mOutBitmap);
Log.e(TAG, "RS time: overlay : " + String.valueOf((System.currentTimeMillis() - timeBegin)));
mCameraSurPreview.refresh(mOutBitmap, mCameraDisplayOrientation, timeBegin);
这里有两个问题: (1) RenderScript进程似乎比OpenGL进程慢。 (2) 根据我们的时间日志,使用内置脚本的YUV到RGBA过程非常快,大约需要6毫秒;但使用scriptC的叠加过程非常缓慢,大约需要180毫秒。这是怎么回事?
以下是我们使用的ScriptC的rs-kernal代码(mLayerScript):
#pragma version(1)
#pragma rs java_package_name(**.renderscript)
#pragma stateFragment(parent)
#include "rs_graphics.rsh"
static rs_allocation layer;
static uint32_t dimX;
static uint32_t dimY;
void setLayer(rs_allocation layer1) {
layer = layer1;
}
void setBitmapDim(uint32_t dimX1, uint32_t dimY1) {
dimX = dimX1;
dimY = dimY1;
}
static float BlendOverlayf(float base, float blend) {
return (base < 0.5 ? (2.0 * base * blend) : (1.0 - 2.0 * (1.0 - base) * (1.0 - blend)));
}
static float3 BlendOverlay(float3 base, float3 blend) {
float3 blendOverLayPixel = {BlendOverlayf(base.r, blend.r), BlendOverlayf(base.g, blend.g), BlendOverlayf(base.b, blend.b)};
return blendOverLayPixel;
}
uchar4 __attribute__((kernel)) overlay(uchar4 in, uint32_t x, uint32_t y) {
float4 inPixel = rsUnpackColor8888(in);
uint32_t layerDimX = rsAllocationGetDimX(layer);
uint32_t layerDimY = rsAllocationGetDimY(layer);
uint32_t layerX = x * layerDimX / dimX;
uint32_t layerY = y * layerDimY / dimY;
uchar4* p = (uchar4*)rsGetElementAt(layer, layerX, layerY);
float4 layerPixel = rsUnpackColor8888(*p);
float3 color = BlendOverlay(inPixel.rgb, layerPixel.rgb);
float4 outf = {color.r, color.g, color.b, inPixel.a};
uchar4 outc = rsPackColorTo8888(outf.r, outf.g, outf.b, outf.a);
return outc;
}