Home | History | Annotate | Download | only in image
      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 com.android.rs.imagejb;
     18 
     19 import java.lang.Math;
     20 
     21 import android.renderscript.Allocation;
     22 import android.renderscript.Element;
     23 import android.renderscript.RenderScript;
     24 import android.renderscript.ScriptIntrinsicConvolve3x3;
     25 import android.renderscript.ScriptIntrinsicColorMatrix;
     26 import android.renderscript.Type;
     27 import android.renderscript.Matrix4f;
     28 import android.renderscript.ScriptGroup;
     29 import android.util.Log;
     30 
     31 public class GroupTest extends TestBase {
     32     private ScriptIntrinsicConvolve3x3 mConvolve;
     33     private ScriptIntrinsicColorMatrix mMatrix;
     34 
     35     private Allocation mScratchPixelsAllocation1;
     36     private ScriptGroup mGroup;
     37 
     38     private int mWidth;
     39     private int mHeight;
     40     private boolean mUseNative;
     41 
     42 
     43     public GroupTest(boolean useNative) {
     44         mUseNative = useNative;
     45     }
     46 
     47     public void createTest(android.content.res.Resources res) {
     48         mWidth = mInPixelsAllocation.getType().getX();
     49         mHeight = mInPixelsAllocation.getType().getY();
     50 
     51         mConvolve = ScriptIntrinsicConvolve3x3.create(mRS, Element.U8_4(mRS));
     52         mMatrix = ScriptIntrinsicColorMatrix.create(mRS, Element.U8_4(mRS));
     53 
     54         float f[] = new float[9];
     55         f[0] =  0.f;    f[1] = -1.f;    f[2] =  0.f;
     56         f[3] = -1.f;    f[4] =  5.f;    f[5] = -1.f;
     57         f[6] =  0.f;    f[7] = -1.f;    f[8] =  0.f;
     58         mConvolve.setCoefficients(f);
     59 
     60         Matrix4f m = new Matrix4f();
     61         m.set(1, 0, 0.2f);
     62         m.set(1, 1, 0.9f);
     63         m.set(1, 2, 0.2f);
     64         mMatrix.setColorMatrix(m);
     65 
     66         Type.Builder tb = new Type.Builder(mRS, Element.U8_4(mRS));
     67         tb.setX(mWidth);
     68         tb.setY(mHeight);
     69         Type connect = tb.create();
     70 
     71         if (mUseNative) {
     72             ScriptGroup.Builder b = new ScriptGroup.Builder(mRS);
     73             b.addKernel(mConvolve.getKernelID());
     74             b.addKernel(mMatrix.getKernelID());
     75             b.addConnection(connect, mConvolve.getKernelID(), mMatrix.getKernelID());
     76             mGroup = b.create();
     77         } else {
     78             mScratchPixelsAllocation1 = Allocation.createTyped(mRS, connect);
     79         }
     80     }
     81 
     82     public void runTest() {
     83         mConvolve.setInput(mInPixelsAllocation);
     84         if (mUseNative) {
     85             mGroup.setOutput(mMatrix.getKernelID(), mOutPixelsAllocation);
     86             mGroup.execute();
     87         } else {
     88             mConvolve.forEach(mScratchPixelsAllocation1);
     89             mMatrix.forEach(mScratchPixelsAllocation1, mOutPixelsAllocation);
     90         }
     91     }
     92 
     93 }
     94