1package com.android.rs.refocus.d1new;
2
3import android.graphics.Bitmap;
4import android.support.v8.renderscript.Allocation;
5import android.support.v8.renderscript.Element;
6import android.support.v8.renderscript.RenderScript;
7import android.util.Log;
8
9import com.android.rs.refocus.renderscript.ScriptC_layered_filter_fast_d1new;
10
11import com.android.rs.refocus.LayerInfo;
12import com.android.rs.refocus.ImageBuffersForRenderScript;
13
14/**
15 * A class that manages the image buffers that interface between Java and Render
16 * Script. These buffers are specialized for float32 pixel representation.
17 *
18 * @author zhl@google.com (Li Zhang)
19 */
20public class ImageBuffersForRenderScriptd1new extends
21        ImageBuffersForRenderScript {
22  /**
23   * SharpImage, FuzzyImage, and integralImage that are bound with memory in
24   * Render Script for layered filtering.
25   * Global allocation for images and meta data that are bound with memory in Render Script
26   *
27   */
28  private static final String myTAG = "RefocusFilterd1new";
29  public Allocation sharpRGBAAllocation;
30  public Allocation fuzzyRGBAAllocation;
31  public Allocation integralRGBAAllocation;
32
33  public Allocation sharpActualDepthAllocation;
34  public Allocation sharpDilatedDepthAllocation;
35  public Allocation sharpActiveAllocation;
36  public Allocation sharpMatteAllocation;
37
38  /**
39   * A constructor that allocates memory buffers in Java and binds the buffers
40   * with the global pointers in the Render Script.
41   *
42   * @param image an input (padded) RGBD image
43   * @param renderScript a RenderScript object that manages the {@code Context}
44   * in Java
45   * @param scriptC a RenderScript object that manages the filtering kernel
46   *                functions in .rs file
47   */
48  public ImageBuffersForRenderScriptd1new(Bitmap image, int margin,
49                                          RenderScript renderScript, ScriptC_layered_filter_fast_d1new scriptC) {
50    super(image, margin, renderScript);
51    sharpRGBAAllocation = Allocation.createSized(
52            renderScript, Element.F32_4(renderScript),
53            imageWidthPadded * imageHeightPadded);
54    sharpActualDepthAllocation = Allocation.createSized(
55            renderScript, Element.U8(renderScript),
56            imageWidthPadded * imageHeightPadded);
57    sharpDilatedDepthAllocation = Allocation.createSized(
58            renderScript, Element.U8(renderScript),
59            imageWidthPadded * imageHeightPadded);
60    sharpActiveAllocation = Allocation.createSized(
61            renderScript, Element.U8(renderScript),
62            imageWidthPadded * imageHeightPadded);
63    sharpMatteAllocation = Allocation.createSized(
64            renderScript, Element.U8(renderScript),
65            imageWidthPadded * imageHeightPadded);
66    fuzzyRGBAAllocation = Allocation.createSized(
67            renderScript, Element.F32_4(renderScript),
68            imageWidthPadded * imageHeightPadded);
69    integralRGBAAllocation = Allocation.createSized(
70            renderScript, Element.F32_4(renderScript),
71            imageWidthPadded * imageHeightPadded);
72
73    scriptC.set_g_sharp_RGBA(sharpRGBAAllocation);
74    scriptC.set_g_fuzzy_RGBA(fuzzyRGBAAllocation);
75    scriptC.set_g_integral_RGBA(integralRGBAAllocation);
76
77    scriptC.set_g_sharp_actual_depth(sharpActualDepthAllocation);
78    scriptC.set_g_sharp_active(sharpActiveAllocation);
79    scriptC.set_g_sharp_matte(sharpMatteAllocation);
80    scriptC.set_g_sharp_dilated_depth(sharpDilatedDepthAllocation);
81
82  }
83
84  /**
85   * A function that passes global parameters from Java to Render Script and
86   * sets up the input image.
87   *
88   * @param focalLayer a layer for the depth value interval that has zero blur.
89   * @param scriptC a RenderScript object that manages filtering kernels and
90   *        global variables in .rs file
91   */
92  public void initializeRenderScript(LayerInfo focalLayer,
93                                     ScriptC_layered_filter_fast_d1new scriptC) {
94    long startnow;
95    long endnow;
96    startnow = System.nanoTime();
97    scriptC.invoke_InitializeFast(imageWidthPadded, imageHeightPadded,
98            paddedMargin, focalLayer.frontDepth, focalLayer.backDepth);
99    endnow = System.nanoTime();
100    Log.d(myTAG, "Initialize: " + (endnow - startnow) + " ns");
101    // At this point, {@code inAllocation} contains input RGBD image in Java.
102    // {@code g_sharp_image} is a global pointer that points the focus image in
103    // Render Script. The following function copies {@code inAllocation} in
104    // Java to {@code g_sharp_image) in Render Script.
105    startnow = System.nanoTime();
106    scriptC.forEach_UnpackInputImage(inAllocation);
107    endnow = System.nanoTime();
108    Log.d(myTAG, "UnpackInputImage: " + (endnow - startnow) + " ns");
109  }
110}
111