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