Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2016 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 java.nio.ByteBuffer;
     20 import java.util.Random;
     21 
     22 import android.renderscript.Allocation;
     23 import android.renderscript.Element;
     24 import android.renderscript.Element.DataType;
     25 import android.renderscript.RSIllegalArgumentException;
     26 import android.renderscript.Type;
     27 
     28 import android.util.Log;
     29 import android.view.Surface;
     30 
     31 public class AllocationCreateAllocationsTest extends RSBaseCompute {
     32     private int dimX = 1920;
     33     private int dimY = 1080;
     34     private final int MAX_NUM_IO_ALLOC = 16;
     35 
     36     Allocation[] createAllocationsHelper(int usage, int numAlloc) {
     37       Element e = Element.U8_4(mRS);
     38       Type t = Type.createXY(mRS, e, dimX, dimY);
     39       return Allocation.createAllocations(mRS, t, usage, numAlloc);
     40     }
     41 
     42     public void testCreateAllocations() {
     43         int usage = Allocation.USAGE_SCRIPT;
     44 
     45         int numAlloc = MAX_NUM_IO_ALLOC + 1;
     46         Allocation[] allocArray;
     47         allocArray = createAllocationsHelper(usage, numAlloc);
     48         assertTrue("failed to create AllocationQueue", allocArray != null);
     49         for (Allocation a : allocArray) {
     50             a.destroy();
     51         }
     52     }
     53 
     54     public void testCreateAllocations_USAGE_IO_INPUT() {
     55         int usage = Allocation.USAGE_IO_INPUT;
     56 
     57         int numAlloc = MAX_NUM_IO_ALLOC + 1;
     58         Allocation[] allocArray;
     59         try {
     60             allocArray = createAllocationsHelper(usage, MAX_NUM_IO_ALLOC + 1);
     61             fail("should throw RSIllegalArgumentException");
     62         } catch (RSIllegalArgumentException e) {
     63         }
     64         numAlloc = 10;
     65         allocArray = createAllocationsHelper(usage, numAlloc);
     66         assertTrue("failed to create AllocationQueue", allocArray != null);
     67         for (Allocation a : allocArray) {
     68             a.destroy();
     69         }
     70     }
     71 
     72     public void testGetProperties() {
     73         int numAlloc = 10;
     74         int usage = Allocation.USAGE_SCRIPT | Allocation.USAGE_IO_INPUT;
     75         Allocation[] allocArray = createAllocationsHelper(usage, numAlloc);
     76 
     77         Element eRef = allocArray[0].getElement();
     78         Type tRef = allocArray[0].getType();
     79         int uRef = allocArray[0].getUsage();
     80         Surface sRef = allocArray[0].getSurface();
     81         for (int i=1; i<numAlloc; i++) {
     82             Element e = allocArray[i].getElement();
     83             assertTrue("Element mismatch between AllocationQueue and Allocation",
     84                        e.equals(eRef));
     85             Type t = allocArray[i].getType();
     86             assertTrue("Type mismatch between AllocationQueue and Allocation",
     87                        t.equals(tRef));
     88             int u = allocArray[i].getUsage();
     89             assertTrue("Usage mismatch between AllocationQueue and Allocation",
     90                        u == uRef);
     91             Surface s = allocArray[i].getSurface();
     92             assertTrue("Surface mismatch between AllocationQueue and Allocation",
     93                        s.equals(sRef));
     94         }
     95         for (Allocation a : allocArray) {
     96             a.destroy();
     97         }
     98     }
     99 
    100     public void testMultipleIoReceive_USAGE_IO_INPUT() {
    101         int usage = Allocation.USAGE_SCRIPT | Allocation.USAGE_IO_INPUT;
    102         int dX = 64, dY = 64, numAlloc = 5;
    103         Type t = Type.createXY(mRS, Element.U8_4(mRS), dX, dY);
    104 
    105         Allocation[] allocArray = Allocation.createAllocations(mRS, t, usage, numAlloc);
    106         Allocation inputAlloc = Allocation.createTyped(mRS, t,
    107                                     Allocation.USAGE_SCRIPT | Allocation.USAGE_IO_OUTPUT);
    108         inputAlloc.setSurface(allocArray[0].getSurface());
    109 
    110         for (int i=0; i<numAlloc; i++) {
    111             Random r = new Random();
    112             byte[] dataIn = new byte[dX * dY * 4];
    113             byte[] dataOut = new byte[dX * dY * 4];
    114 
    115             r.nextBytes(dataIn);
    116             inputAlloc.copyFromUnchecked(dataIn);
    117             inputAlloc.ioSend();
    118             allocArray[i].ioReceive();
    119             allocArray[i].copyTo(dataOut);
    120             for (int j=0; j<dX*dY*4; j++) {
    121                 assertTrue("IoReceive Failed, Frame: " + i, dataIn[j] == dataOut[j]);
    122             }
    123         }
    124         for (Allocation a : allocArray) {
    125             a.destroy();
    126         }
    127         inputAlloc.destroy();
    128     }
    129 }
    130