RenderScript for Android

RenderScript for Android

RenderScript + Blur

6dd0483f1353a4a359e92633cfd65c64?s=128

Daichi Furiya (Wasabeef)

September 29, 2015
Tweet

Transcript

  1. RenderScript + Blur Wasabeef

  2. About Me @wasabeef_jp CyberAgent, Inc.

  3. RenderScript?

  4. RenderScript? ɾAPI Level 11 (Support Library v8) ɾUsing C99 lang

    (RenderScript Kernel) ɾUsing Java API ɾImage Processing ɾBlur … etc
  5. RenderScript Kernel? blur.rs LLVM RS CC blur.bc ScriptC_blur .java ScriptField_point

    .java
  6. RenderScript Intrinsics ɾScriptIntrinsicConvolve3x3 ɾScriptIntrinsicConvolve5x5 ɾScriptIntrinsicBlur ɾScriptIntrinsicYuvToRGB ɾScriptIntrinsicColorMatrix ɾScriptIntrinsicBlend ɾScriptIntrinsicLUT ɾScriptIntrinsic3DLUT

  7. RenderScript Settings

  8. Gradle android { compileSdkVersion 23 buildToolsVersion “23.0.1” defaultConfig { minSdkVersion

    23 targetSdkVersion 23 renderscriptTargetApi 23 renderscriptSupportModeEnabled true } }
  9. Gradle android { compileSdkVersion 23 buildToolsVersion “23.0.1” defaultConfig { minSdkVersion

    23 targetSdkVersion 23 renderscriptTargetApi 23 renderscriptSupportModeEnabled true } }
  10. Blur?

  11. Blur

  12. Algorithms ɾRS Gaussian Fast (ScriptIntrinsicBlur) ɾRS Box, Gaussian 5x5 (ScriptIntrinsicConvolve5x5)

    ɾStack Blur ɾGaussian Fast Blur ɾBox Blur
  13. Gaussian Blur (Java) public static void blur(Bitmap bmp, int radius)

    {
 int w = bmp.getWidth();
 int h = bmp.getHeight();
 int[] pix = new int[w * h];
 bmp.getPixels(pix, 0, w, 0, 0, w, h);
 
 for (int r = radius; r >= 1; r /= 2) {
 for (int i = r; i < h - r; i++) {
 for (int j = r; j < w - r; j++) {
 int tl = pix[(i - r) * w + j - r];
 int tr = pix[(i - r) * w + j + r];
 int tc = pix[(i - r) * w + j];
 int bl = pix[(i + r) * w + j - r];
 int br = pix[(i + r) * w + j + r];
 int bc = pix[(i + r) * w + j];
 int cl = pix[i * w + j - r];
 int cr = pix[i * w + j + r];
 
 pix[(i * w) + j] = 0xFF000000 |
 (((tl & 0xFF) + (tr & 0xFF) + (tc & 0xFF) + (bl & 0xFF) + (br & 0xFF) + (bc & 0xFF)
 + (cl & 0xFF) + (cr & 0xFF)) >> 3) & 0xFF |
 (((tl & 0xFF00) + (tr & 0xFF00) + (tc & 0xFF00) + (bl & 0xFF00) + (br & 0xFF00) + (bc
 & 0xFF00) + (cl & 0xFF00) + (cr & 0xFF00)) >> 3) & 0xFF00 |
 (((tl & 0xFF0000) + (tr & 0xFF0000) + (tc & 0xFF0000) + (bl & 0xFF0000) + (br
 & 0xFF0000) + (bc & 0xFF0000) + (cl & 0xFF0000) + (cr & 0xFF0000)) >> 3)
 & 0xFF0000;
 }
 }
 }
 bmp.setPixels(pix, 0, w, 0, 0, w, h);
 }
  14. RS Gaussian Blur (Java) public static void blur(Bitmap src, int

    radius) { RenderScript rs = RenderScript.create(context);
 Allocation input = Allocation.createFromBitmap(rs, src); Allocation output = Allocation.createTyped(rs, input.getType()); ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); script.setRadius(radius); script.setInput(input); script.forEach(output); output.copyTo(src);
 rs.destroy(); }
  15. RenderScript public static void blur(Bitmap src, int radius) { RenderScript

    rs = RenderScript.create(context);
 Allocation input = Allocation.createFromBitmap(rs, src); Allocation output = Allocation.createTyped(rs, input.getType()); ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); script.setRadius(radius); script.setInput(input); script.forEach(output); output.copyTo(src);
 rs.destroy(); }
  16. Allocation public static void blur(Bitmap src, int radius) { RenderScript

    rs = RenderScript.create(context);
 Allocation input = Allocation.createFromBitmap(rs, src); Allocation output = Allocation.createTyped(rs, input.getType()); ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); script.setRadius(radius); script.setInput(input); script.forEach(output); output.copyTo(src);
 rs.destroy(); }
  17. ScriptIntrinsicBlur public static void blur(Bitmap src, int radius) { RenderScript

    rs = RenderScript.create(context);
 Allocation input = Allocation.createFromBitmap(rs, src); Allocation output = Allocation.createTyped(rs, input.getType()); ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); script.setRadius(radius); script.setInput(input); script.forEach(output); output.copyTo(src);
 rs.destroy(); }
  18. RenderScript Intrinsics ɾScriptIntrinsicConvolve3x3 ɾScriptIntrinsicConvolve5x5 ɾScriptIntrinsicBlur ɾScriptIntrinsicYuvToRGB ɾScriptIntrinsicColorMatrix ɾScriptIntrinsicBlend ɾScriptIntrinsicLUT ɾScriptIntrinsic3DLUT

  19. Element ɾRoughly equivalent to a C type ɾfloat ɾfloat4 ɾunsigned

    32-bit int ɾsigned 8-bit int
  20. ScriptIntrinsicBlur::setRadius public static void blur(Bitmap src, int radius) { RenderScript

    rs = RenderScript.create(context);
 Allocation input = Allocation.createFromBitmap(rs, src); Allocation output = Allocation.createTyped(rs, input.getType()); ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); script.setRadius(radius); script.setInput(input); script.forEach(output); output.copyTo(src);
 rs.destroy(); }
  21. ScriptIntrinsicBlur::setInput public static void blur(Bitmap src, int radius) { RenderScript

    rs = RenderScript.create(context);
 Allocation input = Allocation.createFromBitmap(rs, src); Allocation output = Allocation.createTyped(rs, input.getType()); ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); script.setRadius(radius); script.setInput(input); script.forEach(output); output.copyTo(src);
 rs.destroy(); }
  22. ScriptIntrinsicBlur::forEach public static void blur(Bitmap src, int radius) { RenderScript

    rs = RenderScript.create(context);
 Allocation input = Allocation.createFromBitmap(rs, src); Allocation output = Allocation.createTyped(rs, input.getType()); ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); script.setRadius(radius); script.setInput(input); script.forEach(output); output.copyTo(src);
 rs.destroy(); }
  23. Allocation::copyTo public static void blur(Bitmap src, int radius) { RenderScript

    rs = RenderScript.create(context);
 Allocation input = Allocation.createFromBitmap(rs, src); Allocation output = Allocation.createTyped(rs, input.getType()); ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); script.setRadius(radius); script.setInput(input); script.forEach(output); output.copyTo(src);
 rs.destroy(); }
  24. RenderScript::destroy public static void blur(Bitmap src, int radius) { RenderScript

    rs = RenderScript.create(context);
 Allocation input = Allocation.createFromBitmap(rs, src); Allocation output = Allocation.createTyped(rs, input.getType()); ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); script.setRadius(radius); script.setInput(input); script.forEach(output); output.copyTo(src);
 rs.destroy(); }
  25. RS Gaussian Blur public static void blur(Bitmap src, int radius)

    { RenderScript rs = RenderScript.create(context);
 Allocation input = Allocation.createFromBitmap(rs, src); Allocation output = Allocation.createTyped(rs, input.getType()); ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); script.setRadius(radius); script.setInput(input); script.forEach(output); output.copyTo(src);
 rs.destroy(); }
  26. github.com/wasabeef Blurry Glide Transformations Picasso Transformations Fresco Processors

  27. Thanks.