Android RenderScript 浅谈

前言

     RenderScript是一个Google出品的,在Android平台上的并行计算框架,官方的简介是说RenderScript运行时可在设备上提供的多个处理器(如多核 CPU 和 GPU)间并行调度工作。在日常Android开发中,RenderScript主要用于图像处理。比如对图片做高斯模糊等,都可以用RenderScript处理。

在这里插入图片描述
内容来自RenderScript 概览

扯淡的文档说明, 加上这玩意要被舍弃了, 觉得有点可惜
在这里插入图片描述
内容来自从 RenderScript 迁移

使用

     总的来说,RenderScript能干的事情很多,但有限。常见用于处理Bitmap的效果、转换。
如(参考RenderScript Intrinsics Replacement Toolkit):

  • blend,
  • blur,
  • color matrix,
  • convolve,
  • histogram and histogramDot,
  • LUT (lookup table) and LUT 3D,
  • resize, and YUV to RGB.

反色参考代码:

uchar4 RS_KERNEL invert(uchar4 in, uint32_t x, uint32_t y) {
    
    
  uchar4 out = in;
  out.r = 255 - in.r;
  out.g = 255 - in.g;
  out.b = 255 - in.b;
  return out;
}

对应java调用代码

	public static Bitmap invert(Context context, Bitmap bm){
    
    
		Bitmap bmOut = Bitmap.createBitmap(bm);
		RenderScript RS = RenderScript.create(context);
		ScriptC_singlesource script = new ScriptC_singlesource(RS);
		//Allocation inputAllocation = Allocation.createFromBitmapResource(
		//		RS, getResources(), R.drawable.image);
		Allocation inputAllocation = Allocation.createFromBitmap(RS, bm);
		Allocation outputAllocation = Allocation.createFromBitmap(RS, bmOut);
		//Allocation outputAllocation = Allocation.createTyped(
		//		RS, inputAllocation.getType(),
		//		Allocation.USAGE_SCRIPT | Allocation.USAGE_IO_OUTPUT);
		script.forEach_invert(inputAllocation, outputAllocation);
		outputAllocation.copyTo(bmOut);
		RS.destroy();
		return bmOut;
	}

    其他基础用法案例可以参考本文末尾参考部分连接资料,接下来是一些新的尝试。

  1. 前面大部分的接口的入参都是bitmap, 如果要使用int[] 或 **byte[]**呢?
//https://blog.csdn.net/oDongFangZhiZi/article/details/71422510
int __attribute__((kernel)) test0(int in, uint32_t x) {
    
    
    int out = in;
    out = x;
    return out;
}
	public static void test0(Context context, int[] src) {
    
    

		// 创建 RenderScript 对象
		RenderScript rs = RenderScript.create(context);

		// 创建输入、输出 Allocation
		Allocation allIn  = Allocation.createSized(rs, Element.I32(rs), src.length);
		allIn.copyFrom(src);

		byte[] out = new byte[src.length];
		Allocation allOut  = Allocation.createSized(rs, Element.I32(rs), src.length);

		ScriptC_test rsTest = new ScriptC_test(rs);
		rsTest.forEach_test0(allIn, allOut);
		allOut.copyTo(out);
		// 释放资源
		rs.destroy();
	}

重点在于Allocation.createSized(rs, Element.I32(rs), src.length); 关于Element的文档,真的让人无从下手…
在这里插入图片描述

int[] 相对简单,如果在RS中使用的是uchar4, 入参怎么传? 答案是可以用byte[](非标准答案,仅供参考)

//map for byte[4]
//rgba = [0][1][2][3]
uchar4 __attribute__((kernel)) test1(uchar4 in, uint32_t x, uint32_t y) {
    
    
    uchar4 out = in;
    //out.r = x;
    out.g = x + 1;
    out.b = x + 2;
    out.a = x + 3;
    return out;
}
	public static void test(Context context, byte[] src) {
    
    
		// 创建 RenderScript 对象
		RenderScript rs = RenderScript.create(context);

		// 创建输入、输出 Allocation
		Allocation allIn  = Allocation.createSized(rs, Element.RGBA_8888(rs), src.length/4);
		allIn.copyFrom(src);

		byte[] out = new byte[src.length];
		Allocation allOut  = Allocation.createSized(rs, Element.RGBA_8888(rs), src.length/4);

		ScriptC_test rsTest = new ScriptC_test(rs);
		rsTest.forEach_test1(allIn, allOut);
		allOut.copyTo(out);
		// 释放资源
		rs.destroy();
	}
test(getContext(), new byte[]{
    
    
				10, 0, 0, 0,
				11, 0, 0, 0,
				12, 0, 0, 0,
				13, 0, 0, 0,
				14, 0, 0, 0,
				15, 0, 0, 0,
				16, 0, 0, 0,
				17, 0, 0, 0});
//结果:
//0x0a,0x01,0x02,0x03,
//0x0b,0x02,0x03,0x04,
//0x0c,0x03,0x04,0x05,
//0x0d,0x04,0x05,0x06,
//0x0e,0x05,0x06,0x07,
//0x0f,0x06,0x07,0x08,
//0x10,0x07,0x08,0x09,
//0x11,0x08,0x09,0x0a

RenderScript源码位置

frameworks/base/rs/
├── java
│ └── android
│ └── renderscript
│ ├── AllocationAdapter.java
│ ├── Allocation.java
│ ├── BaseObj.java
│ ├── Byte2.java
│ ├── Byte3.java
│ ├── Byte4.java
│ ├── Double2.java
│ ├── Double3.java
│ ├── Double4.java
│ ├── Element.java
│ ├── FieldPacker.java
│ ├── FileA3D.java
│ ├── Float2.java
│ ├── Float3.java
│ ├── Float4.java
│ ├── Font.java
│ ├── Int2.java
│ ├── Int3.java
│ ├── Int4.java
│ ├── Long2.java
│ ├── Long3.java
│ ├── Long4.java
│ ├── Matrix2f.java
│ ├── Matrix3f.java
│ ├── Matrix4f.java
│ ├── Mesh.java
│ ├── package.html
│ ├── ProgramFragmentFixedFunction.java
│ ├── ProgramFragment.java
│ ├── Program.java
│ ├── ProgramRaster.java
│ ├── ProgramStore.java
│ ├── ProgramVertexFixedFunction.java
│ ├── ProgramVertex.java
│ ├── RenderScriptCacheDir.java
│ ├── RenderScriptGL.java
│ ├── RenderScript.java
│ ├── RSDriverException.java
│ ├── RSIllegalArgumentException.java
│ ├── RSInvalidStateException.java
│ ├── RSRuntimeException.java
│ ├── RSSurfaceView.java
│ ├── RSTextureView.java
│ ├── Sampler.java
│ ├── ScriptC.java
│ ├── ScriptGroup.java
│ ├── ScriptIntrinsic3DLUT.java
│ ├── ScriptIntrinsicBLAS.java
│ ├── ScriptIntrinsicBlend.java
│ ├── ScriptIntrinsicBlur.java
│ ├── ScriptIntrinsicColorMatrix.java
│ ├── ScriptIntrinsicConvolve3x3.java
│ ├── ScriptIntrinsicConvolve5x5.java
│ ├── ScriptIntrinsicHistogram.java
│ ├── ScriptIntrinsic.java
│ ├── ScriptIntrinsicLUT.java
│ ├── ScriptIntrinsicResize.java
│ ├── ScriptIntrinsicYuvToRGB.java
│ ├── Script.java
│ ├── Short2.java
│ ├── Short3.java
│ ├── Short4.java
│ └── Type.java
├── jni
│ ├── Android.mk
│ └── android_renderscript_RenderScript.cpp
└── OWNERS

frameworks/rs

参考

RenderScript使用教程(一)
RenderScript :简单而快速的图像处理
RenderScript简单使用
Android 如何使用 RenderScript ?
BlurTestAndroid/BlurBenchmark/src/main/rs/contrast.rs

猜你喜欢

转载自blog.csdn.net/ansondroider/article/details/129581584