Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2011 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;
     18 
     19 import android.renderscript.RenderScript;
     20 import android.renderscript.Allocation;
     21 import android.renderscript.Element;
     22 import android.renderscript.RSRuntimeException;
     23 import android.util.Log;
     24 
     25 /**
     26  * Base RenderScript test class. This class provides a message handler and a
     27  * convenient way to wait for compute scripts to complete their execution.
     28  */
     29 class RSBaseCompute extends RSBase {
     30     RenderScript mRS;
     31 
     32     static final int TEST_F32 = 0;
     33     static final int TEST_F32_2 = 1;
     34     static final int TEST_F32_3 = 2;
     35     static final int TEST_F32_4 = 3;
     36     static final int TEST_RELAXED_F32 = 4;
     37     static final int TEST_RELAXED_F32_2 = 5;
     38     static final int TEST_RELAXED_F32_3 = 6;
     39     static final int TEST_RELAXED_F32_4 = 7;
     40     protected int INPUTSIZE = 512;
     41 
     42     @Override
     43     protected void setUp() throws Exception {
     44         super.setUp();
     45         mRS = RenderScript.create(mCtx);
     46         mRS.setMessageHandler(mRsMessage);
     47     }
     48 
     49     @Override
     50     protected void tearDown() throws Exception {
     51         if (mRS != null) {
     52             mRS.destroy();
     53             mRS = null;
     54         }
     55         super.tearDown();
     56     }
     57 
     58     public void checkArray(float[] ref, float[] out, int height, int refStride,
     59              int outStride, float ulpCount) {
     60         int minStride = refStride > outStride ? outStride : refStride;
     61         for (int i = 0; i < height; i++) {
     62             for (int j = 0; j < minStride; j++) {
     63                 int refIdx = i * refStride + j;
     64                 int outIdx = i * outStride + j;
     65                 float ulp = Math.ulp(ref[refIdx]) * ulpCount;
     66                 assertEquals("Incorrect value @ idx = " + i + " |",
     67                         ref[refIdx],
     68                         out[outIdx],
     69                         ulp);
     70             }
     71         }
     72     }
     73 
     74     public void checkArray(int[] ref, int[] out, int height, int refStride,
     75              int outStride) {
     76         int minStride = refStride > outStride ? outStride : refStride;
     77         for (int i = 0; i < height; i++) {
     78             for (int j = 0; j < minStride; j++) {
     79                 int refIdx = i * refStride + j;
     80                 int outIdx = i * outStride + j;
     81                 assertEquals("Incorrect value @ idx = " + i + " |",
     82                         ref[refIdx],
     83                         out[outIdx]);
     84             }
     85         }
     86     }
     87 
     88     private void baseTestHelper(int testid, Element inElement, Element outElement, long seed, int fact,
     89                                 int offset, int rStride, int rSkip, int refStride, int outStride,
     90                                 int inStride, int skip, int ulp) {
     91         float[] inArray = makeInArray(INPUTSIZE * inStride);
     92         fillRandom(seed, fact, offset, inArray, rStride, rSkip);
     93         float[] refArray = getRefArray(inArray, INPUTSIZE, inStride, skip);
     94 
     95         Allocation mAllocationIn = setInAlloc(inElement);
     96         fillInAlloc(mAllocationIn, inArray);
     97 
     98         Allocation mAllocationOut = setOutAlloc(outElement);
     99         try {
    100             RSUtils.forEach(this, testid, mAllocationIn, mAllocationOut);
    101         } catch (RSRuntimeException e) {
    102             Log.e("RenderscriptCTS", "Caught RSRuntimeException: " +
    103                   e.getMessage());
    104         }
    105         float[] outArray = makeOutArray(INPUTSIZE * outStride);
    106         mAllocationOut.copyTo(outArray);
    107         checkArray(refArray, outArray, INPUTSIZE, refStride, outStride, ulp);
    108     }
    109 
    110     public void baseTest(int testid, long seed, int refStride, int outStride, int inStride, int skip, int ulp) {
    111         baseTestHelper(testid, null, null, seed, 1, 0, 1, 0, refStride, outStride, inStride, skip, ulp);
    112     }
    113 
    114     public void doF32(long seed, int ulp) {
    115         baseTestHelper(TEST_F32, Element.F32(mRS), Element.F32(mRS), seed, 1, 0, 1, 0, 1, 1, 1, 0, ulp);
    116     }
    117 
    118     public void doF32_2(long seed, int ulp) {
    119         baseTestHelper(TEST_F32_2, Element.F32_2(mRS), Element.F32_2(mRS), seed, 1, 0, 1, 0, 2, 2, 2, 0, ulp);
    120     }
    121 
    122     public void doF32_3(long seed, int ulp) {
    123         baseTestHelper(TEST_F32_3, Element.F32_3(mRS), Element.F32_3(mRS), seed, 1, 0, 4, 1, 3, 4, 4, 1, ulp);
    124     }
    125 
    126     public void doF32_4(long seed, int ulp) {
    127         baseTestHelper(TEST_F32_4, Element.F32_4(mRS), Element.F32_4(mRS), seed, 1, 0, 1, 0, 4, 4, 4, 0, ulp);
    128     }
    129 
    130     public void doF32_relaxed(long seed, int ulp) {
    131         baseTestHelper(TEST_RELAXED_F32, Element.F32(mRS), Element.F32(mRS), seed, 1, 0, 1, 0, 1, 1, 1, 0, ulp);
    132     }
    133 
    134     public void doF32_2_relaxed(long seed, int ulp) {
    135         baseTestHelper(TEST_RELAXED_F32_2, Element.F32_2(mRS), Element.F32_2(mRS), seed, 1, 0, 1, 0, 2, 2, 2, 0, ulp);
    136     }
    137 
    138     public void doF32_3_relaxed(long seed, int ulp) {
    139         baseTestHelper(TEST_RELAXED_F32_3, Element.F32_3(mRS), Element.F32_3(mRS), seed, 1, 0, 4, 1, 3, 4, 4, 1, ulp);
    140     }
    141 
    142     public void doF32_4_relaxed(long seed, int ulp) {
    143         baseTestHelper(TEST_RELAXED_F32_4, Element.F32_4(mRS), Element.F32_4(mRS), seed, 1, 0, 1, 0, 4, 4, 4, 0, ulp);
    144     }
    145 
    146 
    147     public void forEach(int testId, Allocation mIn, Allocation mOut) throws RSRuntimeException {
    148         // Intentionally empty... subclass will likely define only one, but not both
    149     }
    150 
    151     public void forEach(int testId, Allocation mIn) throws RSRuntimeException {
    152         // Intentionally empty... subclass will likely define only one, but not both
    153     }
    154 
    155     //These are default actions for these functions, specific tests overload them
    156     protected float[] getRefArray(float[] inArray, int size, int stride, int skip) {
    157         return null;
    158     }
    159 
    160     protected Allocation setInAlloc(Element e) {
    161         return Allocation.createSized(mRS, e, INPUTSIZE);
    162     }
    163 
    164     protected Allocation setOutAlloc(Element e) {
    165         return Allocation.createSized(mRS, e, INPUTSIZE);
    166     }
    167 
    168     protected float[] makeInArray(int size) {
    169         return new float[size];
    170     }
    171 
    172     protected float[] makeOutArray(int size) {
    173         return new float[size];
    174     }
    175 
    176     protected void fillRandom(long seed, int fact, int offset, float[] inArray, int rStride, int rSkip) {
    177         RSUtils.genRandom(seed, fact, offset, inArray, rStride, rSkip);
    178     }
    179 
    180     protected void fillInAlloc(Allocation mIn, float[] inArray) {
    181         mIn.copy1DRangeFromUnchecked(0, INPUTSIZE, inArray);
    182     }
    183 }
    184