ScriptIntrinsicConvolve5x5.java revision 7e8e92f79a6dfcb7e6ec80028f648579f29450e5
1ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams/* 2ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * Copyright (C) 2012 The Android Open Source Project 3ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * 4ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * Licensed under the Apache License, Version 2.0 (the "License"); 5ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * you may not use this file except in compliance with the License. 6ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * You may obtain a copy of the License at 7ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * 8ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * http://www.apache.org/licenses/LICENSE-2.0 9ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * 10ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * Unless required by applicable law or agreed to in writing, software 11ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * distributed under the License is distributed on an "AS IS" BASIS, 12ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * See the License for the specific language governing permissions and 14ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * limitations under the License. 15ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams */ 16ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams 17ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Samspackage android.support.v8.renderscript; 18ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams 19ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Samsimport android.util.Log; 20ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams 21ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams/** 22ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * Intrinsic for applying a 5x5 convolve to an allocation. 23ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * 24ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams **/ 25ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murraypublic class ScriptIntrinsicConvolve5x5 extends ScriptIntrinsic { 26ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams private final float[] mValues = new float[25]; 27ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams private Allocation mInput; 28059fede7f200350b6131fc131f76248085485722Miao Wang // API level for the intrinsic 29059fede7f200350b6131fc131f76248085485722Miao Wang private static final int INTRINSIC_API_LEVEL = 19; 30ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams 313d9b60c9ae71c4c09df0b4e59c825a5d631e1254Miao Wang ScriptIntrinsicConvolve5x5(long id, RenderScript rs) { 32ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams super(id, rs); 33ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams } 34ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams 35ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams /** 36059fede7f200350b6131fc131f76248085485722Miao Wang * Supported elements types are {@link Element#U8}, {@link 37059fede7f200350b6131fc131f76248085485722Miao Wang * Element#U8_2}, {@link Element#U8_3}, {@link Element#U8_4}, 38059fede7f200350b6131fc131f76248085485722Miao Wang * {@link Element#F32}, {@link Element#F32_2}, {@link 397e8e92f79a6dfcb7e6ec80028f648579f29450e5Miao Wang * Element#F32_3}, and {@link Element#F32_4}. 40ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * 417e8e92f79a6dfcb7e6ec80028f648579f29450e5Miao Wang * <p> The default coefficients are: 42ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * <code> 43ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * <p> [ 0, 0, 0, 0, 0 ] 44ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * <p> [ 0, 0, 0, 0, 0 ] 45ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * <p> [ 0, 0, 1, 0, 0 ] 46ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * <p> [ 0, 0, 0, 0, 0 ] 47ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * <p> [ 0, 0, 0, 0, 0 ] 48ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * </code> 49ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * 507d435ae5ba100be5710b685653cc351cab159c11Stephen Hines * @param rs The RenderScript context 51ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * @param e Element type for intputs and outputs 52ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * 53ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * @return ScriptIntrinsicConvolve5x5 54ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams */ 55ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams public static ScriptIntrinsicConvolve5x5 create(RenderScript rs, Element e) { 56059fede7f200350b6131fc131f76248085485722Miao Wang if (!e.isCompatible(Element.U8(rs)) && 57059fede7f200350b6131fc131f76248085485722Miao Wang !e.isCompatible(Element.U8_2(rs)) && 58059fede7f200350b6131fc131f76248085485722Miao Wang !e.isCompatible(Element.U8_3(rs)) && 59059fede7f200350b6131fc131f76248085485722Miao Wang !e.isCompatible(Element.U8_4(rs)) && 60059fede7f200350b6131fc131f76248085485722Miao Wang !e.isCompatible(Element.F32(rs)) && 61059fede7f200350b6131fc131f76248085485722Miao Wang !e.isCompatible(Element.F32_2(rs)) && 62059fede7f200350b6131fc131f76248085485722Miao Wang !e.isCompatible(Element.F32_3(rs)) && 63059fede7f200350b6131fc131f76248085485722Miao Wang !e.isCompatible(Element.F32_4(rs))) { 64059fede7f200350b6131fc131f76248085485722Miao Wang throw new RSIllegalArgumentException("Unsuported element type."); 65059fede7f200350b6131fc131f76248085485722Miao Wang } 66059fede7f200350b6131fc131f76248085485722Miao Wang long id; 67059fede7f200350b6131fc131f76248085485722Miao Wang boolean mUseIncSupp = rs.isUseNative() && 68059fede7f200350b6131fc131f76248085485722Miao Wang android.os.Build.VERSION.SDK_INT < INTRINSIC_API_LEVEL; 69059fede7f200350b6131fc131f76248085485722Miao Wang 70059fede7f200350b6131fc131f76248085485722Miao Wang id = rs.nScriptIntrinsicCreate(4, e.getID(rs), mUseIncSupp); 71059fede7f200350b6131fc131f76248085485722Miao Wang 72059fede7f200350b6131fc131f76248085485722Miao Wang ScriptIntrinsicConvolve5x5 si = new ScriptIntrinsicConvolve5x5(id, rs); 73059fede7f200350b6131fc131f76248085485722Miao Wang si.setIncSupp(mUseIncSupp); 74059fede7f200350b6131fc131f76248085485722Miao Wang return si; 75ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams 76ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams } 77ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams 78ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams /** 797e8e92f79a6dfcb7e6ec80028f648579f29450e5Miao Wang * Set the input of the 5x5 convolve. 80ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * Must match the element type supplied during create. 81ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * 82ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * @param ain The input allocation. 83ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams */ 84ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams public void setInput(Allocation ain) { 85ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams mInput = ain; 861b370e358d16cc3b50b169511d6b387db09f972dJason Sams setVar(1, ain); 87ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams } 88ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams 89ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams /** 90ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * Set the coefficients for the convolve. 91ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * 927e8e92f79a6dfcb7e6ec80028f648579f29450e5Miao Wang * <p> The convolve layout is: 93ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * <code> 94ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * <p> [ 0, 1, 2, 3, 4 ] 95ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * <p> [ 5, 6, 7, 8, 9 ] 96ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * <p> [ 10, 11, 12, 13, 14 ] 97ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * <p> [ 15, 16, 17, 18, 19 ] 98ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * <p> [ 20, 21, 22, 23, 24 ] 99ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * </code> 100ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * 101ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * @param v The array of coefficients to set 102ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams */ 103ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams public void setCoefficients(float v[]) { 104ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams FieldPacker fp = new FieldPacker(25*4); 105ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams for (int ct=0; ct < mValues.length; ct++) { 106ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams mValues[ct] = v[ct]; 107ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams fp.addF32(mValues[ct]); 108ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams } 109ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams setVar(0, fp); 110ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams } 111ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams 112ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams /** 113ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * Apply the filter to the input and save to the specified 114ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * allocation. 115ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * 116ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * @param aout Output allocation. Must match creation element 117ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * type. 118ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams */ 119ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams public void forEach(Allocation aout) { 1203cf9bc14128f46345ba95294c30873ae52e89aecMiao Wang forEach(0, (Allocation) null, aout, null); 121ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams } 122ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams 123ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams /** 124059fede7f200350b6131fc131f76248085485722Miao Wang * Apply the filter to the input and save to the specified 125059fede7f200350b6131fc131f76248085485722Miao Wang * allocation. 126059fede7f200350b6131fc131f76248085485722Miao Wang * 127059fede7f200350b6131fc131f76248085485722Miao Wang * @param aout Output allocation. Must match creation element 128059fede7f200350b6131fc131f76248085485722Miao Wang * type. 129059fede7f200350b6131fc131f76248085485722Miao Wang * @param opt LaunchOptions for clipping 130059fede7f200350b6131fc131f76248085485722Miao Wang */ 131059fede7f200350b6131fc131f76248085485722Miao Wang public void forEach(Allocation aout, Script.LaunchOptions opt) { 132059fede7f200350b6131fc131f76248085485722Miao Wang forEach(0, (Allocation) null, aout, null, opt); 133059fede7f200350b6131fc131f76248085485722Miao Wang } 134059fede7f200350b6131fc131f76248085485722Miao Wang 135059fede7f200350b6131fc131f76248085485722Miao Wang 136059fede7f200350b6131fc131f76248085485722Miao Wang /** 137ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * Get a KernelID for this intrinsic kernel. 138ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * 139ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * @return Script.KernelID The KernelID object. 140ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams */ 141ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams public Script.KernelID getKernelID() { 142ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams return createKernelID(0, 2, null, null); 143ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams } 144ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams 145ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams /** 146ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * Get a FieldID for the input field of this intrinsic. 147ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * 148ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams * @return Script.FieldID The FieldID object. 149ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams */ 150ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams public Script.FieldID getFieldID_Input() { 151ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams return createFieldID(1, null); 152ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams } 153ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams} 154ca29b8caf56fa4866752f9cea4ec02b2a271dceeJason Sams 155