背景:
我想根据Android相机应用程序的代码添加实时滤镜。但Android相机应用程序的架构是基于OpenGL ES 1.x。我需要使用着色器来自定义我们的过滤器实现。然而,将相机应用程序更新到OpenGL ES 2.0太困难了。然后我必须找到一些其他方法来实现实时滤镜而不是OpenGL。经过一番研究后我决定使用渲染脚本。
PROBLEM:
我已经通过渲染脚本编写了一个简单过滤器的演示。它表明fps比OpenGL实现的要低得多。大约 5 fps 与 15 fps。
问题:
Android官方场外表示:RenderScript运行时将在设备上所有可用的处理器(例如多核CPU、GPU或DSP)上并行工作,让您专注于表达算法而不是调度工作或负载平衡。那么为什么渲染脚本执行速度较慢呢?
如果渲染脚本不能满足我的要求,有更好的方法吗?
代码详细信息:
你好,我和提问者在同一个团队。我们想编写一个基于渲染脚本的实时滤镜相机。在我们的测试演示项目中,我们使用一个简单的过滤器:添加了覆盖过滤器 ScriptC 脚本的 YuvToRGB IntrinsicScript。
在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,并使用script-kernals进行图像过滤处理。像这样:
// 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的过程非常快,大约需要6ms;但是使用scriptC的overlay过程非常慢,大约需要180ms。这是怎么发生的?
这是我们使用的脚本(pLayScript)的rs内核代码:
#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;
}