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.AllocationAdapter; 24 import android.renderscript.Element; 25 import android.renderscript.Element.DataType; 26 import android.renderscript.RSIllegalArgumentException; 27 import android.renderscript.Type; 28 29 import android.util.Log; 30 31 public class AllocationByteBufferTest extends RSBaseCompute { 32 33 protected int MAX_DIM = 128; 34 protected int RAND_SEED = 2016; 35 36 Allocation createTypedAllocation(DataType dt, int size, int dimX, int dimY) { 37 Element e = getElement(mRS, dt, size); 38 Type t; 39 if (dimY <= 1) { 40 t = Type.createX(mRS, e, dimX); 41 } else { 42 t = Type.createXY(mRS, e, dimX, dimY); 43 } 44 45 return Allocation.createTyped(mRS, t); 46 } 47 48 void testByteBufferHelper(DataType dt, int byteSize, int dimX, int dimY) { 49 Random r = new Random(RAND_SEED); 50 51 for (int size = 1; size <= 4; size++) { 52 int vecWidth = (size == 3) ? 4 : size; 53 byte[] data = new byte[dimX * dimY * vecWidth * byteSize]; 54 RSUtils.genRandomBytes(RAND_SEED, data, true, 8); 55 56 Allocation alloc = createTypedAllocation(dt, size, dimX, dimY); 57 alloc.copyFromUnchecked(data); 58 59 ByteBuffer bb = alloc.getByteBuffer(); 60 int stride = (int)alloc.getStride(); 61 for (int i=0; i < 10; i++) { 62 int posX = r.nextInt(dimX); 63 int posY = r.nextInt(dimY); 64 byte byteInData = data[(posY * dimX + posX) * vecWidth * byteSize]; 65 byte byteInBuffer = bb.get(posY * stride + posX * vecWidth * byteSize); 66 assertEquals(byteInData, byteInBuffer); 67 } 68 alloc.destroy(); 69 } 70 } 71 72 void testByteBufferHelper1D(DataType dt, int byteSize) { 73 Random r = new Random(RAND_SEED); 74 int dimX = r.nextInt(MAX_DIM) + 1; 75 testByteBufferHelper(dt, byteSize, dimX, 1); 76 } 77 78 void testByteBufferHelper2D(DataType dt, int byteSize) { 79 Random r = new Random(RAND_SEED); 80 int dimX = r.nextInt(MAX_DIM) + 1; 81 int dimY = r.nextInt(MAX_DIM) + 2; //Make sure dimY is larger than 1; 82 testByteBufferHelper(dt, byteSize, dimX, dimY); 83 } 84 85 public void test1DWrite() { 86 Random r = new Random(RAND_SEED); 87 int vecWidth = 4; 88 int dimX = r.nextInt(MAX_DIM) + 1; 89 90 Type t = Type.createX(mRS, Element.U8_4(mRS), dimX); 91 Allocation alloc = Allocation.createTyped(mRS, t); 92 ByteBuffer bb = alloc.getByteBuffer(); 93 94 byte[] dataIn = new byte[dimX * vecWidth]; 95 byte[] dataOut = new byte[dimX * vecWidth]; 96 RSUtils.genRandomBytes(RAND_SEED, dataIn, true, 8); 97 bb.put(dataIn); 98 alloc.copyTo(dataOut); 99 for (int i = 0; i < dimX * vecWidth; i++) { 100 assertEquals(dataIn[i], dataOut[i]); 101 } 102 } 103 104 public void test2DWrite() { 105 Random r = new Random(RAND_SEED); 106 int vecWidth = 4; 107 int dimX = r.nextInt(MAX_DIM) + 1; 108 int dimY = r.nextInt(MAX_DIM) + 2; //Make sure dimY is larger than 1; 109 110 Type t = Type.createXY(mRS, Element.U8_4(mRS), dimX, dimY); 111 Allocation alloc = Allocation.createTyped(mRS, t); 112 ByteBuffer bb = alloc.getByteBuffer(); 113 114 int stride = (int)alloc.getStride(); 115 byte[] dataIn = new byte[stride * dimY]; 116 byte[] dataOut = new byte[dimX * dimY * vecWidth]; 117 RSUtils.genRandomBytes(RAND_SEED, dataIn, true, 8); 118 bb.put(dataIn); 119 alloc.copyTo(dataOut); 120 for (int i = 0; i < dimX*vecWidth; i++) { 121 for (int j = 0; j < dimY; j++) { 122 assertEquals(dataIn[j*stride + i], dataOut[j*dimX*vecWidth + i]); 123 } 124 } 125 } 126 127 public void testByteBufferU8_1D() { 128 testByteBufferHelper1D(DataType.UNSIGNED_8, 1); 129 } 130 131 public void testByteBufferU8_2D() { 132 testByteBufferHelper2D(DataType.UNSIGNED_8, 1); 133 } 134 135 public void testByteBufferU16_1D() { 136 testByteBufferHelper1D(DataType.UNSIGNED_16, 2); 137 } 138 139 public void testByteBufferU16_2D() { 140 testByteBufferHelper2D(DataType.UNSIGNED_16, 2); 141 } 142 143 public void testByteBufferU32_1D() { 144 testByteBufferHelper1D(DataType.UNSIGNED_32, 4); 145 } 146 147 public void testByteBufferU32_2D() { 148 testByteBufferHelper2D(DataType.UNSIGNED_32, 4); 149 } 150 151 public void testByteBufferU64_1D() { 152 testByteBufferHelper1D(DataType.UNSIGNED_64, 8); 153 } 154 155 public void testByteBufferU64_2D() { 156 testByteBufferHelper2D(DataType.UNSIGNED_64, 8); 157 } 158 159 public void testByteBufferS8_1D() { 160 testByteBufferHelper1D(DataType.SIGNED_8, 1); 161 } 162 163 public void testByteBufferS8_2D() { 164 testByteBufferHelper2D(DataType.SIGNED_8, 1); 165 } 166 167 public void testByteBufferS16_1D() { 168 testByteBufferHelper1D(DataType.SIGNED_16, 2); 169 } 170 171 public void testByteBufferS16_2D() { 172 testByteBufferHelper2D(DataType.SIGNED_16, 2); 173 } 174 175 public void testByteBufferS32_1D() { 176 testByteBufferHelper1D(DataType.SIGNED_32, 4); 177 } 178 179 public void testByteBufferS32_2D() { 180 testByteBufferHelper2D(DataType.SIGNED_32, 4); 181 } 182 183 public void testByteBufferS64_1D() { 184 testByteBufferHelper1D(DataType.SIGNED_64, 8); 185 } 186 187 public void testByteBufferS64_2D() { 188 testByteBufferHelper2D(DataType.UNSIGNED_64, 8); 189 } 190 191 public void testByteBufferF32_1D() { 192 testByteBufferHelper1D(DataType.FLOAT_32, 4); 193 } 194 195 public void testByteBufferF32_2D() { 196 testByteBufferHelper2D(DataType.FLOAT_32, 4); 197 } 198 199 public void testByteBufferF64_1D() { 200 testByteBufferHelper1D(DataType.FLOAT_64, 8); 201 } 202 203 public void testByteBufferF64_2D() { 204 testByteBufferHelper2D(DataType.FLOAT_64, 8); 205 } 206 } 207