RenderScript - yszheda/wiki GitHub Wiki


NDK

floating point

rs_fp_relaxed

CNNdroid

Examples

convolution

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

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;
}

sp<ScriptIntrinsicYuvToRGB>

Trouble-shooting

cannot find symbol class

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);
        // 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);
            }
        }

Unable to load libRSSupportIO.so, USAGE_IO not supported