Tuesday, July 12, 2016

Android RenderScript FilterScript 예제


  • Introduced in Android 4.2 (API Level 17), Filterscript defines a subset of Renderscript that focuses on image processing operations, such as those that you would typically write with an OpenGL ES fragment shader.
  • 현재 android developer의 renderscript 항목에는 기본적으로 filterscript를 사용하기를 설명하고 있다.
    • http://developer.android.com/guide/topics/renderscript/compute.html
  • Inputs and return values of root functions cannot contain pointers. The default root function signature contains pointers, so you must use the __attribute__((kernel)) attribute to declare a custom root function when using Filterscript.
  • Built-in types cannot exceed 32-bits.
  • Filterscript must always use relaxed floating point precision by using the rs_fp_relaxed pragma.
    • Most applications can use rs_fp_relaxed without any side effects. This may be very beneficial on some architectures due to additional optimizations only available with relaxed precision (such as SIMD CPU instructions).
  • Filterscript files must end with an .fs extension, instead of an .rs extension
    • Android-18에서는 fs대신에 rs를 써도 된다.

#pragma version(1)
#pragma rs java_package_name(com.example.renderscriptsample)
#pragma rs_fp_relaxed
// for version 17
// can called by forEach_invert(in, out)
uchar4 __attribute__((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;

// renderscript init
mRS = RenderScript.create(this);
mInAllocation = Allocation.createFromBitmap(mRS, bm, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
mOutAllocation = Allocation.createFromBitmap(mRS, bm, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
mScript = new ScriptC_mono(mRS, getResources(), R.raw.mono);
// renderscript run
mScript.forEach_invert(mInAllocation, mOutAllocation);

No comments:

Post a Comment