• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 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 androidx.renderscript;
18 
19 import android.util.Log;
20 
21 /**
22  * Intrinsic for applying a 5x5 convolve to an allocation.
23  *
24  * @deprecated Renderscript has been deprecated in API level 31. Please refer to the <a
25  * href="https://developer.android.com/guide/topics/renderscript/migration-guide">migration
26  * guide</a> for the proposed alternatives.
27  **/
28 @Deprecated
29 public class ScriptIntrinsicConvolve5x5 extends ScriptIntrinsic {
30     private final float[] mValues = new float[25];
31     private Allocation mInput;
32     // API level for the intrinsic
33     private static final int INTRINSIC_API_LEVEL = 19;
34 
ScriptIntrinsicConvolve5x5(long id, RenderScript rs)35     ScriptIntrinsicConvolve5x5(long id, RenderScript rs) {
36         super(id, rs);
37     }
38 
39     /**
40      * Supported elements types are {@link Element#U8}, {@link
41      * Element#U8_2}, {@link Element#U8_3}, {@link Element#U8_4},
42      * {@link Element#F32}, {@link Element#F32_2}, {@link
43      * Element#F32_3}, and {@link Element#F32_4}.
44      *
45      * <p> The default coefficients are:
46      * <code>
47      * <p> [ 0,  0,  0,  0,  0  ]
48      * <p> [ 0,  0,  0,  0,  0  ]
49      * <p> [ 0,  0,  1,  0,  0  ]
50      * <p> [ 0,  0,  0,  0,  0  ]
51      * <p> [ 0,  0,  0,  0,  0  ]
52      * </code>
53      *
54      * @param rs The RenderScript context
55      * @param e Element type for intputs and outputs
56      *
57      * @return ScriptIntrinsicConvolve5x5
58      */
create(RenderScript rs, Element e)59     public static ScriptIntrinsicConvolve5x5 create(RenderScript rs, Element e) {
60         if (!e.isCompatible(Element.U8(rs)) &&
61             !e.isCompatible(Element.U8_2(rs)) &&
62             !e.isCompatible(Element.U8_3(rs)) &&
63             !e.isCompatible(Element.U8_4(rs)) &&
64             !e.isCompatible(Element.F32(rs)) &&
65             !e.isCompatible(Element.F32_2(rs)) &&
66             !e.isCompatible(Element.F32_3(rs)) &&
67             !e.isCompatible(Element.F32_4(rs))) {
68             throw new RSIllegalArgumentException("Unsupported element type.");
69         }
70         long id;
71         boolean mUseIncSupp = rs.isUseNative() &&
72                               android.os.Build.VERSION.SDK_INT < INTRINSIC_API_LEVEL;
73 
74         id = rs.nScriptIntrinsicCreate(4, e.getID(rs), mUseIncSupp);
75 
76         ScriptIntrinsicConvolve5x5 si = new ScriptIntrinsicConvolve5x5(id, rs);
77         si.setIncSupp(mUseIncSupp);
78         return si;
79 
80     }
81 
82     /**
83      * Set the input of the 5x5 convolve.
84      * Must match the element type supplied during create.
85      *
86      * @param ain The input allocation.
87      */
88     public void setInput(Allocation ain) {
89         mInput = ain;
90         setVar(1, ain);
91     }
92 
93     /**
94     * Set the coefficients for the convolve.
95     *
96     * <p> The convolve layout is:
97     * <code>
98     * <p> [ 0,  1,  2,  3,  4  ]
99     * <p> [ 5,  6,  7,  8,  9  ]
100     * <p> [ 10, 11, 12, 13, 14 ]
101     * <p> [ 15, 16, 17, 18, 19 ]
102     * <p> [ 20, 21, 22, 23, 24 ]
103     * </code>
104     *
105     * @param v The array of coefficients to set
106     */
107     public void setCoefficients(float v[]) {
108         FieldPacker fp = new FieldPacker(25*4);
109         for (int ct=0; ct < mValues.length; ct++) {
110             mValues[ct] = v[ct];
111             fp.addF32(mValues[ct]);
112         }
113         setVar(0, fp);
114     }
115 
116     /**
117      * Apply the filter to the input and save to the specified
118      * allocation.
119      *
120      * @param aout Output allocation. Must match creation element
121      *             type.
122      */
123     public void forEach(Allocation aout) {
124         forEach(0, (Allocation) null, aout, null);
125     }
126 
127     /**
128      * Apply the filter to the input and save to the specified
129      * allocation.
130      *
131      * @param aout Output allocation. Must match creation element
132      *             type.
133      * @param opt LaunchOptions for clipping
134      */
135     public void forEach(Allocation aout, Script.LaunchOptions opt) {
136         forEach(0, (Allocation) null, aout, null, opt);
137     }
138 
139 
140     /**
141      * Get a KernelID for this intrinsic kernel.
142      *
143      * @return Script.KernelID The KernelID object.
144      */
145     public Script.KernelID getKernelID() {
146         return createKernelID(0, 2, null, null);
147     }
148 
149     /**
150      * Get a FieldID for the input field of this intrinsic.
151      *
152      * @return Script.FieldID The FieldID object.
153      */
154     public Script.FieldID getFieldID_Input() {
155         return createFieldID(1, null);
156     }
157 }
158 
159