Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2011-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 android.renderscript.cts;
     18 
     19 import android.graphics.Bitmap;
     20 import android.renderscript.Allocation;
     21 import android.renderscript.AllocationAdapter;
     22 import android.renderscript.Allocation.MipmapControl;
     23 import android.renderscript.Element;
     24 import android.renderscript.RSIllegalArgumentException;
     25 import android.renderscript.Type;
     26 import android.renderscript.Type.Builder;
     27 import android.renderscript.Type.CubemapFace;
     28 
     29 import android.util.Log;
     30 
     31 public class AllocationTest extends RSBaseCompute {
     32 
     33     // Test power of two and non power of two, equal and non-equal sizes
     34     void createTypedHelper(Element e) {
     35 
     36         Type.Builder typeBuilder = new Type.Builder(mRS, e);
     37         for (int mips = 0; mips <= 1; mips ++) {
     38             boolean useMips = (mips == 1);
     39 
     40             for (int faces = 0; faces <= 1; faces++) {
     41                 boolean useFaces = (faces == 1);
     42 
     43                 for (int x = 1; x < 8; x ++) {
     44                     for (int y = 1; y < 8; y ++) {
     45                         typeBuilder.setMipmaps(useMips);
     46                         typeBuilder.setFaces(useFaces);
     47                         typeBuilder.setX(x).setY(y);
     48                         Allocation.createTyped(mRS, typeBuilder.create()).destroy();
     49                     }
     50                 }
     51             }
     52         }
     53 
     54     }
     55 
     56     void createTypedTextureHelper(Element e) {
     57         // No mips graphics
     58         Type.Builder typeBuilder = new Type.Builder(mRS, e);
     59         Allocation.createTyped(mRS, typeBuilder.setX(8).create(),
     60                                MipmapControl.MIPMAP_NONE,
     61                                Allocation.USAGE_GRAPHICS_TEXTURE).destroy();
     62         Allocation.createTyped(mRS, typeBuilder.setY(8).create(),
     63                                MipmapControl.MIPMAP_NONE,
     64                                Allocation.USAGE_GRAPHICS_TEXTURE).destroy();
     65         // No mips graphics and script
     66         Allocation.createTyped(mRS, typeBuilder.create(),
     67                                MipmapControl.MIPMAP_NONE,
     68                                Allocation.USAGE_GRAPHICS_TEXTURE |
     69                                Allocation.USAGE_SCRIPT).destroy();
     70         // With mips
     71         Allocation.createTyped(mRS, typeBuilder.create(),
     72                                MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE,
     73                                Allocation.USAGE_GRAPHICS_TEXTURE).destroy();
     74         Allocation.createTyped(mRS, typeBuilder.create(),
     75                                MipmapControl.MIPMAP_FULL,
     76                                Allocation.USAGE_GRAPHICS_TEXTURE |
     77                                Allocation.USAGE_SCRIPT).destroy();
     78 
     79         // Only texture npot
     80         Allocation.createTyped(mRS, typeBuilder.setX(7).setY(1).create(),
     81                                MipmapControl.MIPMAP_NONE,
     82                                Allocation.USAGE_GRAPHICS_TEXTURE).destroy();
     83         Allocation.createTyped(mRS, typeBuilder.setX(7).setY(3).create(),
     84                                MipmapControl.MIPMAP_NONE,
     85                                Allocation.USAGE_GRAPHICS_TEXTURE).destroy();
     86         Allocation.createTyped(mRS, typeBuilder.setX(7).setY(7).create(),
     87                                MipmapControl.MIPMAP_NONE,
     88                                Allocation.USAGE_GRAPHICS_TEXTURE).destroy();
     89 
     90         // Script and texture
     91         Allocation.createTyped(mRS, typeBuilder.setX(7).setY(1).create(),
     92                                MipmapControl.MIPMAP_NONE,
     93                                Allocation.USAGE_GRAPHICS_TEXTURE |
     94                                Allocation.USAGE_SCRIPT).destroy();
     95         Allocation.createTyped(mRS, typeBuilder.setX(7).setY(3).create(),
     96                                MipmapControl.MIPMAP_NONE,
     97                                Allocation.USAGE_GRAPHICS_TEXTURE |
     98                                Allocation.USAGE_SCRIPT).destroy();
     99         Allocation.createTyped(mRS, typeBuilder.setX(7).setY(7).create(),
    100                                MipmapControl.MIPMAP_NONE,
    101                                Allocation.USAGE_GRAPHICS_TEXTURE |
    102                                Allocation.USAGE_SCRIPT).destroy();
    103     }
    104 
    105     void createSizedHelper(Element e) {
    106         for (int i = 1; i <= 8; i ++) {
    107             Allocation A = Allocation.createSized(mRS, e, i);
    108             assertEquals(A.getType().getElement(), e);
    109             assertEquals(A.getType().getX(), i);
    110             A.destroy();
    111         }
    112     }
    113 
    114     public void testCreateTyped() {
    115          createTypedHelper(Element.A_8(mRS));
    116          createTypedHelper(Element.RGB_565(mRS));
    117          createTypedHelper(Element.RGB_888(mRS));
    118          createTypedHelper(Element.RGBA_8888(mRS));
    119          createTypedHelper(Element.F16(mRS));
    120          createTypedHelper(Element.F16_2(mRS));
    121          createTypedHelper(Element.F16_3(mRS));
    122          createTypedHelper(Element.F16_4(mRS));
    123          createTypedHelper(Element.F32(mRS));
    124          createTypedHelper(Element.F32_2(mRS));
    125          createTypedHelper(Element.F32_3(mRS));
    126          createTypedHelper(Element.F32_4(mRS));
    127          createTypedHelper(Element.BOOLEAN(mRS));
    128          createTypedHelper(Element.F64(mRS));
    129          createTypedHelper(Element.I8(mRS));
    130          createTypedHelper(Element.I16(mRS));
    131          createTypedHelper(Element.I32(mRS));
    132          createTypedHelper(Element.I64(mRS));
    133          createTypedHelper(Element.U8(mRS));
    134          createTypedHelper(Element.U8_4(mRS));
    135          createTypedHelper(Element.U16(mRS));
    136          createTypedHelper(Element.U32(mRS));
    137          createTypedHelper(Element.U64(mRS));
    138          createTypedHelper(Element.MATRIX_2X2(mRS));
    139          createTypedHelper(Element.MATRIX_3X3(mRS));
    140          createTypedHelper(Element.MATRIX_4X4(mRS));
    141          createTypedHelper(Element.MESH(mRS));
    142          createTypedHelper(Element.PROGRAM_FRAGMENT(mRS));
    143          createTypedHelper(Element.PROGRAM_RASTER(mRS));
    144          createTypedHelper(Element.PROGRAM_STORE(mRS));
    145          createTypedHelper(Element.PROGRAM_VERTEX(mRS));
    146          createTypedHelper(Element.ALLOCATION(mRS));
    147          createTypedHelper(Element.SAMPLER(mRS));
    148          createTypedHelper(Element.SCRIPT(mRS));
    149          createTypedHelper(Element.TYPE(mRS));
    150 
    151          createTypedTextureHelper(Element.A_8(mRS));
    152          createTypedTextureHelper(Element.RGB_565(mRS));
    153          createTypedTextureHelper(Element.RGB_888(mRS));
    154          createTypedTextureHelper(Element.RGBA_8888(mRS));
    155     }
    156 
    157     public void testCreateSized() {
    158          createSizedHelper(Element.A_8(mRS));
    159          createSizedHelper(Element.RGB_565(mRS));
    160          createSizedHelper(Element.RGB_888(mRS));
    161          createSizedHelper(Element.RGBA_8888(mRS));
    162          createSizedHelper(Element.F16(mRS));
    163          createSizedHelper(Element.F16_2(mRS));
    164          createSizedHelper(Element.F16_3(mRS));
    165          createSizedHelper(Element.F16_4(mRS));
    166          createSizedHelper(Element.F32(mRS));
    167          createSizedHelper(Element.F32_2(mRS));
    168          createSizedHelper(Element.F32_3(mRS));
    169          createSizedHelper(Element.F32_4(mRS));
    170          createSizedHelper(Element.BOOLEAN(mRS));
    171          createSizedHelper(Element.F64(mRS));
    172          createSizedHelper(Element.I8(mRS));
    173          createSizedHelper(Element.I16(mRS));
    174          createSizedHelper(Element.I32(mRS));
    175          createSizedHelper(Element.I64(mRS));
    176          createSizedHelper(Element.U8(mRS));
    177          createSizedHelper(Element.U8_4(mRS));
    178          createSizedHelper(Element.U16(mRS));
    179          createSizedHelper(Element.U32(mRS));
    180          createSizedHelper(Element.U64(mRS));
    181          createSizedHelper(Element.MATRIX_2X2(mRS));
    182          createSizedHelper(Element.MATRIX_3X3(mRS));
    183          createSizedHelper(Element.MATRIX_4X4(mRS));
    184          createSizedHelper(Element.MESH(mRS));
    185          createSizedHelper(Element.PROGRAM_FRAGMENT(mRS));
    186          createSizedHelper(Element.PROGRAM_RASTER(mRS));
    187          createSizedHelper(Element.PROGRAM_STORE(mRS));
    188          createSizedHelper(Element.PROGRAM_VERTEX(mRS));
    189          createSizedHelper(Element.ALLOCATION(mRS));
    190          createSizedHelper(Element.SAMPLER(mRS));
    191          createSizedHelper(Element.SCRIPT(mRS));
    192          createSizedHelper(Element.TYPE(mRS));
    193     }
    194 
    195     static int bDimX = 48;
    196     static int bDimY = 8;
    197 
    198     void helperCreateFromBitmap(Bitmap B,
    199                                 Allocation.MipmapControl mc) {
    200         for (int i = 0; i <= 1; i++) {
    201             for (int j = 0; j <= 1; j++) {
    202                 for (int k = 0; k <= 1; k++) {
    203                     for (int l = 0; l <= 1; l++) {
    204                         int u = 0;
    205                         u |= (i * Allocation.USAGE_SCRIPT);
    206                         u |= (j * Allocation.USAGE_GRAPHICS_TEXTURE);
    207                         u |= (k * Allocation.USAGE_GRAPHICS_VERTEX);
    208                         u |= (l * Allocation.USAGE_GRAPHICS_CONSTANTS);
    209                         Allocation.createFromBitmap(mRS, B, mc, u).destroy();
    210                         Allocation.createCubemapFromBitmap(mRS, B, mc, u).destroy();
    211                     }
    212                 }
    213             }
    214         }
    215     }
    216 
    217     public void testCreateFromBitmap() {
    218         Bitmap B = Bitmap.createBitmap(bDimX, bDimY, Bitmap.Config.ARGB_8888);
    219         Allocation.createFromBitmap(mRS, B).destroy();
    220         Allocation.createCubemapFromBitmap(mRS, B).destroy();
    221         for (Allocation.MipmapControl mc : Allocation.MipmapControl.values()) {
    222             helperCreateFromBitmap(B, mc);
    223         }
    224 
    225         try {
    226             int invalidUsage = 0x0100;
    227             Allocation.createFromBitmap(mRS, B,
    228                 Allocation.MipmapControl.MIPMAP_NONE, invalidUsage).destroy();
    229             fail("should throw RSIllegalArgumentException.");
    230         } catch (RSIllegalArgumentException e) {
    231         }
    232 
    233         try {
    234             // width % 6 != 0
    235             Bitmap badB = Bitmap.createBitmap(47, 8, Bitmap.Config.ARGB_8888);
    236             Allocation.createCubemapFromBitmap(mRS, badB,
    237                 Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT).destroy();
    238             fail("should throw RSIllegalArgumentException.");
    239         } catch (RSIllegalArgumentException e) {
    240         }
    241 
    242         try {
    243             // width / 6 != height
    244             Bitmap badB = Bitmap.createBitmap(48, 4, Bitmap.Config.ARGB_8888);
    245             Allocation.createCubemapFromBitmap(mRS, badB,
    246                 Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT).destroy();
    247             fail("should throw RSIllegalArgumentException.");
    248         } catch (RSIllegalArgumentException e) {
    249         }
    250 
    251         try {
    252             // height not power of 2
    253             Bitmap badB = Bitmap.createBitmap(36, 6, Bitmap.Config.ARGB_8888);
    254             Allocation.createCubemapFromBitmap(mRS, badB,
    255                 Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT).destroy();
    256             fail("should throw RSIllegalArgumentException.");
    257         } catch (RSIllegalArgumentException e) {
    258         }
    259     }
    260 
    261     public void testAllocationMipmapControl() {
    262         assertEquals(MipmapControl.MIPMAP_NONE,
    263                      MipmapControl.valueOf("MIPMAP_NONE"));
    264         assertEquals(MipmapControl.MIPMAP_FULL,
    265                      MipmapControl.valueOf("MIPMAP_FULL"));
    266         assertEquals(MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE,
    267                      MipmapControl.valueOf("MIPMAP_ON_SYNC_TO_TEXTURE"));
    268         // Make sure no new enums are added
    269         assertEquals(3, Allocation.MipmapControl.values().length);
    270 
    271         for (Allocation.MipmapControl mc : Allocation.MipmapControl.values()) {
    272             Type.Builder b = new Type.Builder(mRS, Element.U8(mRS));
    273             b.setX(8).setY(8);
    274             Allocation.createTyped(mRS, b.create(), mc,
    275                                    Allocation.USAGE_GRAPHICS_TEXTURE).destroy();
    276         }
    277     }
    278 
    279     public void testCubemapFaces() {
    280         Type.Builder b = new Type.Builder(mRS, Element.U8(mRS));
    281         b.setX(8).setY(8).setFaces(true);
    282         Allocation cubemap = Allocation.createTyped(mRS, b.create(),
    283                                                     MipmapControl.MIPMAP_NONE,
    284                                                     Allocation.USAGE_SCRIPT);
    285         AllocationAdapter adapter = AllocationAdapter.create2D(mRS, cubemap);
    286         for (Type.CubemapFace cf : Type.CubemapFace.values()) {
    287             adapter.setFace(cf);
    288         }
    289 
    290         cubemap.destroy();
    291     }
    292 
    293     /*
    294      * Test all copy from/to routines for byte/short/int/float
    295      */
    296 
    297     void helperFloatCopy(int nElems, int offset, int count, int copyMode) {
    298         Allocation A = Allocation.createSized(mRS, Element.F32(mRS), nElems);
    299 
    300         float src[], dst[];
    301         src = new float[nElems];
    302         dst = new float[nElems];
    303         for (int i = 0; i < count; i++) {
    304             src[i] = (float)i;
    305             dst[offset + i] = -1.0f;
    306         }
    307 
    308         switch (copyMode) {
    309         case 0: A.copyFrom(src); break;
    310         case 1: A.copyFromUnchecked(src); break;
    311         case 2: A.copy1DRangeFrom(offset, count, src); break;
    312         case 3: A.copy1DRangeFromUnchecked(offset, count, src); break;
    313         }
    314         A.copyTo(dst);
    315 
    316         for (int i = 0; i < count; i++) {
    317             assertEquals(dst[offset + i], src[i]);
    318         }
    319 
    320         A.destroy();
    321     }
    322 
    323     void helperByteCopy(int nElems, int offset, int count, int copyMode) {
    324         Allocation A = Allocation.createSized(mRS, Element.I8(mRS), nElems);
    325 
    326         byte src[], dst[];
    327         src = new byte[nElems];
    328         dst = new byte[nElems];
    329         for (int i = 0; i < count; i++) {
    330             src[i] = (byte)i;
    331             dst[offset + i] = -1;
    332         }
    333 
    334         switch (copyMode) {
    335         case 0: A.copyFrom(src); break;
    336         case 1: A.copyFromUnchecked(src); break;
    337         case 2: A.copy1DRangeFrom(offset, count, src); break;
    338         case 3: A.copy1DRangeFromUnchecked(offset, count, src); break;
    339         }
    340         A.copyTo(dst);
    341 
    342         for (int i = 0; i < count; i++) {
    343             assertEquals(dst[offset + i], src[i]);
    344         }
    345 
    346         A.destroy();
    347     }
    348 
    349     // Accept an Element parameter so this helper can test both I16 and F16 elements.
    350     void helperShortCopy(Element element, int nElems, int offset, int count, int copyMode) {
    351         Allocation A = Allocation.createSized(mRS, element, nElems);
    352 
    353         short src[], dst[];
    354         src = new short[nElems];
    355         dst = new short[nElems];
    356         for (int i = 0; i < count; i++) {
    357             src[i] = (short)i;
    358             dst[offset + i] = -1;
    359         }
    360 
    361         switch (copyMode) {
    362         case 0: A.copyFrom(src); break;
    363         case 1: A.copyFromUnchecked(src); break;
    364         case 2: A.copy1DRangeFrom(offset, count, src); break;
    365         case 3: A.copy1DRangeFromUnchecked(offset, count, src); break;
    366         }
    367         A.copyTo(dst);
    368 
    369         for (int i = 0; i < count; i++) {
    370             assertEquals(dst[offset + i], src[i]);
    371         }
    372 
    373         A.destroy();
    374     }
    375 
    376     void helperIntCopy(int nElems, int offset, int count, int copyMode) {
    377         Allocation A = Allocation.createSized(mRS, Element.I32(mRS), nElems);
    378 
    379         int src[], dst[];
    380         src = new int[nElems];
    381         dst = new int[nElems];
    382         for (int i = 0; i < count; i++) {
    383             src[i] = i;
    384             dst[offset + i] = -1;
    385         }
    386 
    387         switch (copyMode) {
    388         case 0: A.copyFrom(src); break;
    389         case 1: A.copyFromUnchecked(src); break;
    390         case 2: A.copy1DRangeFrom(offset, count, src); break;
    391         case 3: A.copy1DRangeFromUnchecked(offset, count, src); break;
    392         }
    393         A.copyTo(dst);
    394 
    395         for (int i = 0; i < count; i++) {
    396             assertEquals(dst[offset + i], src[i]);
    397         }
    398 
    399         A.destroy();
    400     }
    401 
    402     void helperBaseObjCopy(int nElems, int offset, int count, int copyMode) {
    403         Allocation A =
    404             Allocation.createSized(mRS, Element.ELEMENT(mRS), nElems);
    405         Element E[] = new Element[nElems];
    406         for (int i = 0; i < nElems; i++) {
    407             E[i] = Element.BOOLEAN(mRS);
    408         }
    409 
    410         A.copyFrom(E);
    411 
    412         A.destroy();
    413     }
    414 
    415     void helperBitmapCopy(int x, int y) {
    416         Bitmap bSrc = Bitmap.createBitmap(x, y, Bitmap.Config.ARGB_8888);
    417         Bitmap bDst = Bitmap.createBitmap(x, y, Bitmap.Config.ARGB_8888);
    418 
    419         for (int j = 0; j < y; j++) {
    420             for (int i = 0; i < x; i++) {
    421                 bSrc.setPixel(i, j, 9);
    422                 bDst.setPixel(i, j, 0);
    423             }
    424         }
    425 
    426         Type.Builder typeBuilder =
    427             new Type.Builder(mRS, Element.RGBA_8888(mRS));
    428         typeBuilder.setMipmaps(false);
    429         typeBuilder.setFaces(false);
    430         typeBuilder.setX(x).setY(y);
    431         Allocation A = Allocation.createTyped(mRS, typeBuilder.create());
    432 
    433         A.copyFrom(bSrc);
    434         A.copyTo(bDst);
    435 
    436         for (int j = 0; j < y; j++) {
    437             for (int i = 0; i < x; i++) {
    438                 assertEquals(bSrc.getPixel(i, j), bDst.getPixel(i, j));
    439             }
    440         }
    441 
    442         A.destroy();
    443     }
    444 
    445     void helperFloatAllocationCopy(int nElems, int offset, int count) {
    446 
    447         Allocation srcA = Allocation.createSized(mRS, Element.F32(mRS), nElems);
    448         Allocation dstA = Allocation.createSized(mRS, Element.F32(mRS), nElems);
    449 
    450         float src[], dst[];
    451         src = new float[nElems];
    452         dst = new float[nElems];
    453         for (int i = 0; i < nElems; i++) {
    454             src[i] = (float)i;
    455             dst[i] = -1.0f;
    456         }
    457 
    458         // First populate the source allocation
    459         srcA.copyFrom(src);
    460         // Now test allocation to allocation copy
    461         dstA.copy1DRangeFrom(offset, count, srcA, offset);
    462         dstA.copyTo(dst);
    463 
    464         for (int i = 0; i < count; i++) {
    465             assertEquals(dst[offset + i], src[offset + i]);
    466         }
    467 
    468         srcA.destroy();
    469         dstA.destroy();
    470     }
    471 
    472     void helperByteAllocationCopy(int nElems, int offset, int count) {
    473 
    474         Allocation srcA = Allocation.createSized(mRS, Element.I8(mRS), nElems);
    475         Allocation dstA = Allocation.createSized(mRS, Element.I8(mRS), nElems);
    476 
    477         byte src[], dst[];
    478         src = new byte[nElems];
    479         dst = new byte[nElems];
    480         for (int i = 0; i < nElems; i++) {
    481             src[i] = (byte)i;
    482             dst[i] = -1;
    483         }
    484 
    485         // First populate the source allocation
    486         srcA.copyFrom(src);
    487         // Now test allocation to allocation copy
    488         dstA.copy1DRangeFrom(offset, count, srcA, offset);
    489         dstA.copyTo(dst);
    490 
    491         for (int i = 0; i < count; i++) {
    492             assertEquals(dst[offset + i], src[offset + i]);
    493         }
    494 
    495         srcA.destroy();
    496         dstA.destroy();
    497     }
    498 
    499     void helperFloatCopy2D(int nElemsX, int nElemsY,
    500                            int xOffset, int yOffset,
    501                            int width, int height) {
    502         Type.Builder b = new Type.Builder(mRS, Element.F32(mRS));
    503         Allocation A = Allocation.createTyped(mRS, b.setX(nElemsX).setY(nElemsY).create());
    504 
    505         float src[], dst[];
    506         src = new float[width * height];
    507         dst = new float[nElemsX * nElemsY];
    508         for (int i = width * height - 1; i >= 0; i--) {
    509             src[i] = (float)i;
    510         }
    511         for (int i = nElemsX * nElemsY - 1; i >= 0; i--) {
    512             dst[i] = -1.0f;
    513         }
    514 
    515         A.copy2DRangeFrom(xOffset, yOffset, width, height, src);
    516         A.copyTo(dst);
    517 
    518         int sourceCount = width * height - 1;
    519         for (int y = yOffset + height - 1; y >= yOffset ; y--) {
    520             for (int x = xOffset + width - 1; x >= xOffset ; x--) {
    521                 assertEquals(dst[y * nElemsX + x], src[sourceCount--]);
    522             }
    523         }
    524 
    525         A.destroy();
    526     }
    527 
    528     void helperByteCopy2D(int nElemsX, int nElemsY,
    529                            int xOffset, int yOffset,
    530                            int width, int height) {
    531         Type.Builder b = new Type.Builder(mRS, Element.I8(mRS));
    532         Allocation A = Allocation.createTyped(mRS, b.setX(nElemsX).setY(nElemsY).create());
    533 
    534         byte src[], dst[];
    535         src = new byte[width * height];
    536         dst = new byte[nElemsX * nElemsY];
    537         for (int i = width * height - 1; i >= 0; i--) {
    538             src[i] = (byte)i;
    539         }
    540         for (int i = nElemsX * nElemsY - 1; i >= 0; i--) {
    541             dst[i] = -1;
    542         }
    543 
    544         A.copy2DRangeFrom(xOffset, yOffset, width, height, src);
    545         A.copyTo(dst);
    546 
    547         int sourceCount = width * height - 1;
    548         for (int y = yOffset + height - 1; y >= yOffset ; y--) {
    549             for (int x = xOffset + width - 1; x >= xOffset ; x--) {
    550                 assertEquals(dst[y * nElemsX + x], src[sourceCount--]);
    551             }
    552         }
    553 
    554         A.destroy();
    555     }
    556 
    557     // Accept an Element parameter so this helper can test both I16 and F16 elements.
    558     void helperShortCopy2D(Element element, int nElemsX, int nElemsY,
    559                            int xOffset, int yOffset,
    560                            int width, int height) {
    561         Type.Builder b = new Type.Builder(mRS, element);
    562         Allocation A = Allocation.createTyped(mRS, b.setX(nElemsX).setY(nElemsY).create());
    563 
    564         short src[], dst[];
    565         src = new short[width * height];
    566         dst = new short[nElemsX * nElemsY];
    567         for (int i = width * height - 1; i >= 0; i--) {
    568             src[i] = (short)i;
    569         }
    570         for (int i = nElemsX * nElemsY - 1; i >= 0; i--) {
    571             dst[i] = -1;
    572         }
    573 
    574         A.copy2DRangeFrom(xOffset, yOffset, width, height, src);
    575         A.copyTo(dst);
    576 
    577         int sourceCount = width * height - 1;
    578         for (int y = yOffset + height - 1; y >= yOffset ; y--) {
    579             for (int x = xOffset + width - 1; x >= xOffset ; x--) {
    580                 assertEquals(dst[y * nElemsX + x], src[sourceCount--]);
    581             }
    582         }
    583 
    584         A.destroy();
    585     }
    586 
    587     void helperIntCopy2D(int nElemsX, int nElemsY,
    588                            int xOffset, int yOffset,
    589                            int width, int height) {
    590         Type.Builder b = new Type.Builder(mRS, Element.I32(mRS));
    591         Allocation A = Allocation.createTyped(mRS, b.setX(nElemsX).setY(nElemsY).create());
    592 
    593         int src[], dst[];
    594         src = new int[width * height];
    595         dst = new int[nElemsX * nElemsY];
    596         for (int i = width * height - 1; i >= 0; i--) {
    597             src[i] = i;
    598         }
    599         for (int i = nElemsX * nElemsY - 1; i >= 0; i--) {
    600             dst[i] = -1;
    601         }
    602 
    603         A.copy2DRangeFrom(xOffset, yOffset, width, height, src);
    604         A.copyTo(dst);
    605 
    606         int sourceCount = width * height - 1;
    607         for (int y = yOffset + height - 1; y >= yOffset ; y--) {
    608             for (int x = xOffset + width - 1; x >= xOffset ; x--) {
    609                 assertEquals(dst[y * nElemsX + x], src[sourceCount--]);
    610             }
    611         }
    612 
    613         A.destroy();
    614     }
    615 
    616     void helperFloatAllocationCopy2D(int nElemsX, int nElemsY,
    617                            int xOffset, int yOffset,
    618                            int width, int height) {
    619         Type.Builder b = new Type.Builder(mRS, Element.F32(mRS));
    620         Allocation srcA = Allocation.createTyped(mRS, b.setX(nElemsX).setY(nElemsY).create());
    621         Allocation dstA = Allocation.createTyped(mRS, b.setX(nElemsX).setY(nElemsY).create());
    622 
    623         float src[], dst[];
    624         src = new float[nElemsX * nElemsY];
    625         dst = new float[nElemsX * nElemsY];
    626         for (int i = nElemsX * nElemsY - 1; i >= 0; i--) {
    627             src[i] = (float)i;
    628             dst[i] = -1.0f;
    629         }
    630         srcA.copyFrom(src);
    631         dstA.copy2DRangeFrom(xOffset, yOffset, width, height, srcA, xOffset, yOffset);
    632         dstA.copyTo(dst);
    633 
    634         int sourceCount = width * height - 1;
    635         for (int y = yOffset + height - 1; y >= yOffset ; y--) {
    636             for (int x = xOffset + width - 1; x >= xOffset ; x--) {
    637                 assertEquals(dst[y * nElemsX + x], src[y * nElemsX + x]);
    638             }
    639         }
    640 
    641         srcA.destroy();
    642         dstA.destroy();
    643     }
    644 
    645     void helperByteAllocationCopy2D(int nElemsX, int nElemsY,
    646                            int xOffset, int yOffset,
    647                            int width, int height) {
    648         Type.Builder b = new Type.Builder(mRS, Element.I8(mRS));
    649         Allocation srcA = Allocation.createTyped(mRS, b.setX(nElemsX).setY(nElemsY).create());
    650         Allocation dstA = Allocation.createTyped(mRS, b.setX(nElemsX).setY(nElemsY).create());
    651 
    652         byte src[], dst[];
    653         src = new byte[nElemsX * nElemsY];
    654         dst = new byte[nElemsX * nElemsY];
    655         for (int i = nElemsX * nElemsY - 1; i >= 0; i--) {
    656             src[i] = (byte)i;
    657             dst[i] = -1;
    658         }
    659         srcA.copyFrom(src);
    660         dstA.copy2DRangeFrom(xOffset, yOffset, width, height, srcA, xOffset, yOffset);
    661         dstA.copyTo(dst);
    662 
    663         int sourceCount = width * height - 1;
    664         for (int y = yOffset + height - 1; y >= yOffset ; y--) {
    665             for (int x = xOffset + width - 1; x >= xOffset ; x--) {
    666                 assertEquals(dst[y * nElemsX + x], src[y * nElemsX + x]);
    667             }
    668         }
    669 
    670         srcA.destroy();
    671         dstA.destroy();
    672     }
    673 
    674     static int elemsToTest = 20;
    675 
    676     public void test1DCopyOperations() {
    677         for (int s = 8; s <= elemsToTest; s += 2) {
    678             for (int mode = 0; mode <= 1; mode ++) {
    679                 helperFloatCopy(s, 0, s, mode);
    680                 helperByteCopy(s, 0, s, mode);
    681                 helperShortCopy(Element.I16(mRS), s, 0, s, mode);
    682                 helperShortCopy(Element.F16(mRS), s, 0, s, mode);
    683                 helperIntCopy(s, 0, s, mode);
    684                 helperBaseObjCopy(s, 0, s, mode);
    685             }
    686 
    687             // now test copy range
    688             for (int mode = 2; mode <= 3; mode ++) {
    689                 for (int off = 0; off < s; off ++) {
    690                     for (int count = 1; count <= s - off; count ++) {
    691                         helperFloatCopy(s, off, count, mode);
    692                         helperByteCopy(s, off, count, mode);
    693                         helperShortCopy(Element.I16(mRS), s, off, count, mode);
    694                         helperShortCopy(Element.F16(mRS), s, off, count, mode);
    695                         helperIntCopy(s, off, count, mode);
    696                         helperBaseObjCopy(s, off, count, mode);
    697                     }
    698                 }
    699             }
    700 
    701             for (int off = 0; off < s; off ++) {
    702                 for (int count = 1; count <= s - off; count ++) {
    703                     helperFloatAllocationCopy(s, off, count);
    704                     helperByteAllocationCopy(s, off, count);
    705                 }
    706             }
    707         }
    708 
    709         helperBitmapCopy(bDimX, bDimY);
    710     }
    711 
    712     public void test2DCopyOperations() {
    713         for (int sX = 8; sX <= elemsToTest; sX += 5) {
    714             for (int sY = 8; sY <= elemsToTest; sY += 5) {
    715                 for (int offX = 0; offX < sX; offX += 3) {
    716                     for (int offY = 0; offY < sY; offY += 3) {
    717                         for (int w = 1; w <= sX - offX; w += 3) {
    718                             for (int h = 1; h <= sY - offY; h += 3) {
    719                                 helperFloatCopy2D(sX, sY, offX, offY, w, h);
    720                                 helperByteCopy2D(sX, sY, offX, offY, w, h);
    721                                 helperShortCopy2D(Element.I16(mRS), sX, sY, offX, offY, w, h);
    722                                 helperShortCopy2D(Element.F16(mRS), sX, sY, offX, offY, w, h);
    723                                 helperIntCopy2D(sX, sY, offX, offY, w, h);
    724                                 helperFloatAllocationCopy2D(sX, sY, offX, offY, w, h);
    725                                 helperByteAllocationCopy2D(sX, sY, offX, offY, w, h);
    726                             }
    727                         }
    728                     }
    729                 }
    730             }
    731         }
    732     }
    733 
    734     public void testCopyFromAllocation() {
    735         int nElemsX = 256;
    736         int nElemsY = 32;
    737         Type.Builder b = new Type.Builder(mRS, Element.I8(mRS));
    738         Type.Builder b2 = new Type.Builder(mRS, Element.I32(mRS));
    739         Allocation srcA = Allocation.createTyped(mRS, b.setX(nElemsX).setY(nElemsY).create());
    740         Allocation dstA = Allocation.createTyped(mRS, b.setX(nElemsX).setY(nElemsY).create());
    741 
    742         // wrong dimensionality
    743         Allocation srcB_bad = Allocation.createTyped(mRS, b.setX(nElemsX).setY(1).create());
    744         Allocation srcC_bad = Allocation.createTyped(mRS, b.setX(nElemsY).setY(nElemsX).create());
    745         Allocation srcD_bad = Allocation.createTyped(mRS, b.setX(nElemsX*2).setY(nElemsY*2).create());
    746 
    747         // wrong element type
    748         Allocation srcE_bad = Allocation.createTyped(mRS, b2.setX(nElemsX).setY(nElemsY).create());
    749 
    750         try {
    751             dstA.copyFrom(srcB_bad);
    752             fail("should throw RSIllegalArgumentException");
    753         } catch (RSIllegalArgumentException e) {
    754         }
    755 
    756         try {
    757             dstA.copyFrom(srcC_bad);
    758             fail("should throw RSIllegalArgumentException");
    759         } catch (RSIllegalArgumentException e) {
    760         }
    761 
    762         try {
    763             dstA.copyFrom(srcD_bad);
    764             fail("should throw RSIllegalArgumentException");
    765         } catch (RSIllegalArgumentException e) {
    766         }
    767 
    768         try {
    769             dstA.copyFrom(srcE_bad);
    770             fail("should throw RSIllegalArgumentException");
    771         } catch (RSIllegalArgumentException e) {
    772         }
    773 
    774         dstA.copyFrom(srcA);
    775 
    776         srcA.destroy();
    777         dstA.destroy();
    778         srcB_bad.destroy();
    779         srcC_bad.destroy();
    780         srcD_bad.destroy();
    781         srcE_bad.destroy();
    782     }
    783 
    784     public void testSetElementAt() {
    785         Type.Builder b = new Type.Builder(mRS, Element.I32(mRS));
    786         Allocation largeArray = Allocation.createTyped(mRS, b.setX(48).create());
    787         Allocation singleElement = Allocation.createTyped(mRS, b.setX(1).create());
    788 
    789         ScriptC_setelementat script = new ScriptC_setelementat(mRS);
    790 
    791         script.set_memset_toValue(1);
    792         script.forEach_memset(singleElement);
    793 
    794         script.set_dimX(48);
    795         script.set_array(largeArray);
    796 
    797         script.forEach_setLargeArray(singleElement);
    798 
    799         int[] result = new int[1];
    800 
    801         script.set_compare_value(10);
    802         script.forEach_compare(largeArray);
    803         script.forEach_getCompareResult(singleElement);
    804         singleElement.copyTo(result);
    805         assertTrue(result[0] == 2);
    806 
    807         script.destroy();
    808         singleElement.destroy();
    809         largeArray.destroy();
    810     }
    811 
    812     public void testSetElementAt2D() {
    813         Type.Builder b = new Type.Builder(mRS, Element.I32(mRS));
    814 
    815         Allocation singleElement = Allocation.createTyped(mRS, b.setX(1).create());
    816         Allocation largeArray = Allocation.createTyped(mRS, b.setX(48).setY(16).create());
    817 
    818         ScriptC_setelementat script = new ScriptC_setelementat(mRS);
    819 
    820         script.set_memset_toValue(1);
    821         script.forEach_memset(singleElement);
    822 
    823         script.set_dimX(48);
    824         script.set_dimY(16);
    825         script.set_array(largeArray);
    826 
    827         script.forEach_setLargeArray2D(singleElement);
    828 
    829         int[] result = new int[1];
    830 
    831         script.set_compare_value(10);
    832         script.forEach_compare(largeArray);
    833         script.forEach_getCompareResult(singleElement);
    834         singleElement.copyTo(result);
    835         assertTrue(result[0] == 2);
    836 
    837         script.destroy();
    838         singleElement.destroy();
    839         largeArray.destroy();
    840     }
    841 
    842   public void testDimReturnsZero() {
    843     Allocation a;
    844     a = Allocation.createSized(mRS, Element.F32(mRS), 100);
    845     assertTrue(a.getType().getX() == 100);
    846     assertTrue(a.getType().getY() == 0);
    847     assertTrue(a.getType().getZ() == 0);
    848     a.destroy();
    849 
    850     Type.Builder b;
    851     b = new Type.Builder(mRS, Element.F32(mRS));
    852     a = Allocation.createTyped(mRS, b.create());
    853 
    854     assertTrue(a.getType().getX() == 1);
    855     assertTrue(a.getType().getY() == 0);
    856     assertTrue(a.getType().getZ() == 0);
    857 
    858     a.destroy();
    859 
    860     b = new Type.Builder(mRS, Element.F32(mRS)).setX(102);
    861     a = Allocation.createTyped(mRS, b.create());
    862 
    863     assertTrue(a.getType().getX() == 102);
    864     assertTrue(a.getType().getY() == 0);
    865     assertTrue(a.getType().getZ() == 0);
    866     a.destroy();
    867 
    868     b = new Type.Builder(mRS, Element.F32(mRS)).setX(102).setY(123);
    869     a = Allocation.createTyped(mRS, b.create());
    870 
    871     assertTrue(a.getType().getX() == 102);
    872     assertTrue(a.getType().getY() == 123);
    873     assertTrue(a.getType().getZ() == 0);
    874     a.destroy();
    875 
    876     b = new Type.Builder(mRS, Element.F32(mRS)).setX(2).setY(33).setZ(23);
    877     a = Allocation.createTyped(mRS, b.create());
    878 
    879     assertTrue(a.getType().getX() == 2);
    880     assertTrue(a.getType().getY() == 33);
    881     assertTrue(a.getType().getZ() == 23);
    882 
    883     a.destroy();
    884    }
    885 }
    886 
    887 
    888