RenderScript - yszheda/wiki GitHub Wiki
- https://developer.android.com/guide/topics/renderscript/compute.html
- https://stuff.mit.edu/afs/sipb/project/android/docs/guide/topics/renderscript/compute.html
- RenderScript speedup 10x when forcing default CPU implementation
- How to do correct timing of Android RenderScript code on Nvidia Shield
NDK
floating point
rs_fp_relaxed
CNNdroid
Examples
- https://github.com/googlesamples/android-BasicRenderScript
- https://github.com/AlphaBoom/BlurDemo
- Android : Simple and fast image processing with RenderScript
- Levels in Renderscript
convolution
- https://github.com/mharkus/Image-Processing-Renderscript-Compute/blob/master/src/com/mlst/imageprocessing/rs/convolution.rs
- https://stackoverflow.com/questions/10090583/how-to-write-a-convolution-multiplication-in-android-renderscript
- How to write a convolution multiplication in Android Renderscript?
- Sharpen and blur bitmap, convolution using ScriptIntrinsicConvolve3x3
- ScriptIntrincisConvolve3x3: Output-Allocation float4 instead of U8_4?
- android::RSC::ScriptIntrinsicConvolve3x3 Class Reference
- Java Code Examples for android.renderscript.ScriptIntrinsicConvolve3x3
- android.renderscript.ScriptIntrinsicConvolve3x3
- https://github.com/EE202B/RenderScriptOps/blob/master/app/libs/rsKernels_jni/jni/rsConv.h
- https://github.com/thenewcircle/renderscriptimaging/blob/master/app/src/main/java/com/example/android/renderscriptimaging/image/ImageFilter.java
rotate
#pragma version(1)
#pragma rs java_package_name(ua.kulku.rs)
rs_allocation inImage;
int inWidth;
int inHeight;
uchar4 __attribute__ ((kernel)) rotate_90_clockwise (uchar4 in, uint32_t x, uint32_t y) {
uint32_t inX = inWidth - 1 - y;
uint32_t inY = x;
const uchar4 *out = rsGetElementAt(inImage, inX, inY);
return *out;
}
uchar4 __attribute__ ((kernel)) rotate_270_clockwise (uchar4 in, uint32_t x, uint32_t y) {
uint32_t inX = y;
uint32_t inY = inHeight - 1 - x;
const uchar4 *out = rsGetElementAt(inImage, inX, inY);
return *out;
}
import ua.kulku.rs.ScriptC_rotator;
public Bitmap rotate(Bitmap bitmap) {
RenderScript rs = RenderScript.create(mContext);
ScriptC_rotator script = new ScriptC_rotator(rs);
script.set_inWidth(bitmap.getWidth());
script.set_inHeight(bitmap.getHeight());
Allocation sourceAllocation = Allocation.createFromBitmap(rs, bitmap,
Allocation.MipmapControl.MIPMAP_NONE,
Allocation.USAGE_SCRIPT);
bitmap.recycle();
script.set_inImage(sourceAllocation);
int targetHeight = bitmap.getWidth();
int targetWidth = bitmap.getHeight();
Bitmap.Config config = bitmap.getConfig();
Bitmap target = Bitmap.createBitmap(targetWidth, targetHeight, config);
final Allocation targetAllocation = Allocation.createFromBitmap(rs, target,
Allocation.MipmapControl.MIPMAP_NONE,
Allocation.USAGE_SCRIPT);
script.forEach_rotate_90_clockwise(targetAllocation, targetAllocation);
targetAllocation.copyTo(target);
rs.destroy();
return target;
}
FFT
- https://stackoverflow.com/questions/31195653/android-fourier-transform-realtime-renderscript
- https://github.com/nesl/renderScriptFFT
blur
yuv2rgb
buildin intrinsic
float4 rsYuvToRGBA_float4(uchar y, uchar u, uchar v);
uchar4 rsYuvToRGBA_uchar4(uchar y, uchar u, uchar v);
public Bitmap YUV_toRGB(byte[] yuv,int W,int H) {
RenderScript rs = RenderScript.create(this);
Type.Builder yuvBlder = new Type.Builder(rs, Element.U8(rs))
.setX(W).setY(H*3/2);
Allocation allocIn = Allocation.createTyped(rs,yuvBlder.create(),Allocation.USAGE_SCRIPT);
Type rgbType = Type.createXY(rs, Element.RGBA_8888(rs), W, H);
Allocation allocOut = Allocation.createTyped(rs,rgbType,Allocation.USAGE_SCRIPT);
ScriptC_yuv2rgb scriptC_yuv2rgb = new ScriptC_yuv2rgb(rs);
allocIn.copyFrom(yuv);
scriptC_yuv2rgb.set_gW(W);
scriptC_yuv2rgb.set_gH(H);
scriptC_yuv2rgb.set_gYUV(allocIn);
scriptC_yuv2rgb.forEach_YUV2RGB(allocOut);
Bitmap bmp = Bitmap.createBitmap(W, H, Bitmap.Config.ARGB_8888);
allocOut.copyTo(bmp);
allocIn.destroy();
scriptC_yuv2rgb.destroy();
return bmp;
}
#pragma version(1)
#pragma rs java_package_name(com.example.myexam)
#pragma rs_fp_relaxed
rs_allocation gYUV;
uint32_t gW;
uint32_t gH;
uchar4 __attribute__((kernel)) YUV2RGB(uint32_t x,uint32_t y)
{
uchar yps = rsGetElementAt_uchar(gYUV, x, y);
uchar u = rsGetElementAt_uchar(gYUV,(x & ~1),gH + (y>>1));
uchar v = rsGetElementAt_uchar(gYUV,(x & ~1)+1,gH + (y>>1));
uchar4 rgb = rsYuvToRGBA_uchar4(yps, u, v);
return rgb;
}
- https://stackoverflow.com/questions/41154500/scriptintrinsicyuvtorgb-or-yuv420-allocation-is-broken
- https://stackoverflow.com/questions/20358803/how-to-use-scriptintrinsicyuvtorgb-converting-byte-yuv-to-byte-rgba
- 人脸识别在Android端的实现
- [問題] Android 如何把 RGBA_8888的格式轉成 YUV
- android如何使用图像作为 Allocation 脚本
- 用 Renderscript 实现高效率的图片模糊效果
- https://stackoverflow.com/questions/43623817/android-yuv-nv12-to-rgb-conversion-with-renderscript
- https://stackoverflow.com/questions/13509360/converting-camera-yuv-data-to-argb-with-renderscript
- Android : Preview frame, converted from YCbCr_420_SP (NV21) format to RGB render correct picture but in green
sp<ScriptIntrinsicYuvToRGB>
- http://mobilepearls.com/labs/native-android-api/ndk/docs/renderscript/classandroid_1_1RSC_1_1ScriptIntrinsicYuvToRGB.html
- https://android-developers.googleblog.com/2013/08/renderscript-intrinsics.html
- Example: https://github.com/yigalomer/Yuv2RgbRenderScript/blob/master/src/com/example/yuv2rgbrenderscript/RenderScriptHelper.java
Trouble-shooting
cannot find symbol class
- ScriptC_“name” is not being generated after making a .rs file (in Android Studio with Gradle)
- Android Renderscript with Gradle
- Can't find ScriptC_saturation in BasicRenderScript Sample
- Error with renderscript on android studio 0.2.2
non fatal RS error, rsForEach called with null out allocations
Unable to load BLAS lib, ONLY BNNM will be supported
脚本有runtime error,建议采用RenderScript.ContextType.DEBUG
排查:
RenderScript rs = RenderScript.create(context, RenderScript.ContextType.DEBUG);
- 缺少libblasV8.so,见RenderScript.java源代码
// For old APIs with dlopen bug, need to load blas lib in Java first.
// Only try load to blasV8 when the desired API level includes IntrinsicBLAS.
if (dispatchAPI >= 23) {
// Enable multi-input kernels only when diapatchAPI is M+.
rs.mEnableMultiInput = true;
try {
System.loadLibrary("blasV8");
} catch (UnsatisfiedLinkError e) {
Log.v(LOG_TAG, "Unable to load BLAS lib, ONLY BNNM will be supported: " + e);
}
}