1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.renderscript.cts.refocus.d1new;
18 
19 import android.graphics.Bitmap;
20 import android.renderscript.Allocation;
21 import android.renderscript.Element;
22 import android.renderscript.RenderScript;
23 import android.renderscript.cts.refocus.ImageBuffersForRenderScript;
24 import android.renderscript.cts.refocus.LayerInfo;
25 import android.renderscript.cts.refocus.ScriptC_layered_filter_fast_d1new;
26 import android.util.Log;
27 
28 /**
29  * A class that manages the image buffers that interface between Java and Render
30  * Script. These buffers are specialized for float32 pixel representation.
31  */
32 public class ImageBuffersForRenderScriptd1new extends
33         ImageBuffersForRenderScript {
34   /**
35    * SharpImage, FuzzyImage, and integralImage that are bound with memory in
36    * Render Script for layered filtering.
37    * Global allocation for images and meta data that are bound with memory in Render Script
38    *
39    */
40   private static final String myTAG = "RefocusFilterd1new";
41   public Allocation sharpRGBAAllocation;
42   public Allocation fuzzyRGBAAllocation;
43   public Allocation integralRGBAAllocation;
44 
45   public Allocation sharpActualDepthAllocation;
46   public Allocation sharpDilatedDepthAllocation;
47   public Allocation sharpActiveAllocation;
48   public Allocation sharpMatteAllocation;
49 
destroy()50   public void destroy() {
51       super.destroy();
52     sharpRGBAAllocation.destroy();
53     fuzzyRGBAAllocation.destroy();
54     integralRGBAAllocation.destroy();
55     sharpActualDepthAllocation.destroy();
56     sharpDilatedDepthAllocation.destroy();
57     sharpActiveAllocation.destroy();
58     sharpMatteAllocation.destroy();
59   }
60 
61   /**
62    * A constructor that allocates memory buffers in Java and binds the buffers
63    * with the global pointers in the Render Script.
64    *
65    * @param image an input (padded) RGBD image
66    * @param renderScript a RenderScript object that manages the {@code Context}
67    * in Java
68    * @param scriptC a RenderScript object that manages the filtering kernel
69    *                functions in .rs file
70    */
ImageBuffersForRenderScriptd1new(Bitmap image, int margin, RenderScript renderScript, ScriptC_layered_filter_fast_d1new scriptC)71   public ImageBuffersForRenderScriptd1new(Bitmap image, int margin,
72                                           RenderScript renderScript, ScriptC_layered_filter_fast_d1new scriptC) {
73     super(image, margin, renderScript);
74     sharpRGBAAllocation = Allocation.createSized(
75             renderScript, Element.F32_4(renderScript),
76             imageWidthPadded * imageHeightPadded);
77     sharpActualDepthAllocation = Allocation.createSized(
78             renderScript, Element.U8(renderScript),
79             imageWidthPadded * imageHeightPadded);
80     sharpDilatedDepthAllocation = Allocation.createSized(
81             renderScript, Element.U8(renderScript),
82             imageWidthPadded * imageHeightPadded);
83     sharpActiveAllocation = Allocation.createSized(
84             renderScript, Element.U8(renderScript),
85             imageWidthPadded * imageHeightPadded);
86     sharpMatteAllocation = Allocation.createSized(
87             renderScript, Element.U8(renderScript),
88             imageWidthPadded * imageHeightPadded);
89     fuzzyRGBAAllocation = Allocation.createSized(
90             renderScript, Element.F32_4(renderScript),
91             imageWidthPadded * imageHeightPadded);
92     integralRGBAAllocation = Allocation.createSized(
93             renderScript, Element.F32_4(renderScript),
94             imageWidthPadded * imageHeightPadded);
95 
96     scriptC.set_g_sharp_RGBA(sharpRGBAAllocation);
97     scriptC.set_g_fuzzy_RGBA(fuzzyRGBAAllocation);
98     scriptC.set_g_integral_RGBA(integralRGBAAllocation);
99 
100     scriptC.set_g_sharp_actual_depth(sharpActualDepthAllocation);
101     scriptC.set_g_sharp_active(sharpActiveAllocation);
102     scriptC.set_g_sharp_matte(sharpMatteAllocation);
103     scriptC.set_g_sharp_dilated_depth(sharpDilatedDepthAllocation);
104 
105   }
106 
107   /**
108    * A function that passes global parameters from Java to Render Script and
109    * sets up the input image.
110    *
111    * @param focalLayer a layer for the depth value interval that has zero blur.
112    * @param scriptC a RenderScript object that manages filtering kernels and
113    *        global variables in .rs file
114    */
initializeRenderScript(LayerInfo focalLayer, ScriptC_layered_filter_fast_d1new scriptC)115   public void initializeRenderScript(LayerInfo focalLayer,
116                                      ScriptC_layered_filter_fast_d1new scriptC) {
117     long startnow;
118     long endnow;
119     startnow = System.nanoTime();
120     scriptC.invoke_InitializeFast(imageWidthPadded, imageHeightPadded,
121             paddedMargin, focalLayer.frontDepth, focalLayer.backDepth);
122     endnow = System.nanoTime();
123     Log.d(myTAG, "Initialize: " + (endnow - startnow) + " ns");
124     // At this point, {@code inAllocation} contains input RGBD image in Java.
125     // {@code g_sharp_image} is a global pointer that points the focus image in
126     // Render Script. The following function copies {@code inAllocation} in
127     // Java to {@code g_sharp_image) in Render Script.
128     startnow = System.nanoTime();
129     scriptC.forEach_UnpackInputImage(inAllocation);
130     endnow = System.nanoTime();
131     Log.d(myTAG, "UnpackInputImage: " + (endnow - startnow) + " ns");
132   }
133 }
134