Home | History | Annotate | Download | only in allocations
      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 com.android.rs.allocations;
     18 
     19 import android.app.Activity;
     20 import android.os.Bundle;
     21 import android.graphics.Bitmap;
     22 import android.renderscript.*;
     23 
     24 public class MainActivity extends Activity {
     25     private RenderScript mRS;
     26 
     27     private Allocation mInAllocation;      // script input
     28     private Allocation mOutAllocation;     // script output
     29 
     30     private Allocation mStructInAlloc;     // complexStruct input
     31     private Allocation mStructOutAlloc;    // complexStruct output
     32 
     33     private Allocation mBoolAllocation;    // boolean
     34 
     35     private Allocation mCharAllocation;    // char
     36     private Allocation mChar2Allocation;   // char2
     37     private Allocation mChar3Allocation;   // char3
     38     private Allocation mChar4Allocation;   // char4
     39 
     40     private Allocation mUCharAllocation;   // uchar
     41     private Allocation mUChar2Allocation;  // uchar2
     42     private Allocation mUChar3Allocation;  // uchar3
     43     private Allocation mUChar4Allocation;  // uchar4
     44 
     45     private Allocation mShortAllocation;   // short
     46     private Allocation mShort2Allocation;  // short2
     47     private Allocation mShort3Allocation;  // short3
     48     private Allocation mShort4Allocation;  // short4
     49 
     50     private Allocation mUShortAllocation;  // ushort
     51     private Allocation mUShort2Allocation; // ushort2
     52     private Allocation mUShort3Allocation; // ushort3
     53     private Allocation mUShort4Allocation; // ushort4
     54 
     55     private Allocation mIntAllocation;     // int
     56     private Allocation mInt2Allocation;    // int2
     57     private Allocation mInt3Allocation;    // int3
     58     private Allocation mInt4Allocation;    // int4
     59 
     60     private Allocation mUIntAllocation;    // uint
     61     private Allocation mUInt2Allocation;   // uint2
     62     private Allocation mUInt3Allocation;   // uint3
     63     private Allocation mUInt4Allocation;   // uint4
     64 
     65     private Allocation mLongAllocation;    // long
     66     private Allocation mLong2Allocation;   // long2
     67     private Allocation mLong3Allocation;   // long3
     68     private Allocation mLong4Allocation;   // long4
     69 
     70     private Allocation mULongAllocation;   // ulong
     71     private Allocation mULong2Allocation;  // ulong2
     72     private Allocation mULong3Allocation;  // ulong3
     73     private Allocation mULong4Allocation;  // ulong4
     74 
     75     private Allocation mHalfAllocation;    // half
     76     private Allocation mHalf2Allocation;   // half2
     77     private Allocation mHalf3Allocation;   // half3
     78     private Allocation mHalf4Allocation;   // half4
     79 
     80     private Allocation mFloatAllocation;   // float
     81     private Allocation mFloat2Allocation;  // float2
     82     private Allocation mFloat3Allocation;  // float3
     83     private Allocation mFloat4Allocation;  // float4
     84 
     85     private Allocation mDoubleAllocation;  // double
     86     private Allocation mDouble2Allocation; // double2
     87     private Allocation mDouble3Allocation; // double3
     88     private Allocation mDouble4Allocation; // double4
     89 
     90     private ScriptC_allocs mScript;
     91 
     92     private int mAllocSize = 24; // Chosen as allocation size since it's easily divisible
     93 
     94     private Bitmap mBitmapIn;
     95     private Bitmap mBitmapOut;
     96 
     97     @Override
     98     protected void onCreate(Bundle savedInstanceState) {
     99         super.onCreate(savedInstanceState);
    100 
    101         setContentView(R.layout.main_layout);
    102 
    103         mBitmapIn = Bitmap.createBitmap(64, 64, Bitmap.Config.ARGB_8888);
    104         mBitmapOut = Bitmap.createBitmap(mBitmapIn.getWidth(), mBitmapIn.getHeight(), mBitmapIn.getConfig());
    105 
    106         createScript();
    107         runScript();
    108     }
    109 
    110     private void createScript() {
    111         mRS = RenderScript.create(this,
    112             RenderScript.ContextType.NORMAL,
    113             RenderScript.CREATE_FLAG_LOW_LATENCY |
    114             RenderScript.CREATE_FLAG_WAIT_FOR_ATTACH);
    115 
    116         mScript = new ScriptC_allocs(mRS);
    117     }
    118 
    119     private void createSignedAllocations() {
    120         Type.Builder typeI8Builder = new Type.Builder(mRS, Element.I8(mRS));
    121         typeI8Builder.setX(1); // One element here to test 16 byte memory alignment
    122         typeI8Builder.setY(3);
    123         typeI8Builder.setZ(8);
    124 
    125         mCharAllocation = Allocation.createTyped(mRS, typeI8Builder.create());
    126         mRS.finish();
    127         mChar2Allocation = Allocation.createSized(mRS, Element.I8_2(mRS), mAllocSize / 2);
    128         mRS.finish();
    129         mChar3Allocation = Allocation.createSized(mRS, Element.I8_3(mRS), mAllocSize / 4);
    130         mRS.finish();
    131         mChar4Allocation = Allocation.createSized(mRS, Element.I8_4(mRS), mAllocSize / 4);
    132         mRS.finish();
    133 
    134         Type.Builder typeI16_2Builder = new Type.Builder(mRS, Element.I16_2(mRS));
    135         typeI16_2Builder.setX(6);
    136         typeI16_2Builder.setY(1);
    137         typeI16_2Builder.setZ(2);
    138 
    139         mShortAllocation = Allocation.createSized(mRS, Element.I16(mRS), mAllocSize);
    140         mRS.finish();
    141         mShort2Allocation = Allocation.createTyped(mRS, typeI16_2Builder.create());
    142         mRS.finish();
    143         mShort3Allocation = Allocation.createSized(mRS, Element.I16_3(mRS), mAllocSize / 4);
    144         mRS.finish();
    145         mShort4Allocation = Allocation.createSized(mRS, Element.I16_4(mRS), mAllocSize / 4);
    146         mRS.finish();
    147 
    148         Type.Builder typeI32_3Builder = new Type.Builder(mRS, Element.I32_3(mRS));
    149         typeI32_3Builder.setX(3);
    150         typeI32_3Builder.setY(2);
    151 
    152         mIntAllocation = Allocation.createSized(mRS, Element.I32(mRS), mAllocSize);
    153         mRS.finish();
    154         mInt2Allocation = Allocation.createSized(mRS, Element.I32_2(mRS), mAllocSize / 2);
    155         mRS.finish();
    156         mInt3Allocation = Allocation.createTyped(mRS, typeI32_3Builder.create());
    157         mRS.finish();
    158         mInt4Allocation = Allocation.createSized(mRS, Element.I32_4(mRS), mAllocSize / 4);
    159         mRS.finish();
    160 
    161         Type.Builder typeI64_4Builder = new Type.Builder(mRS, Element.I64_4(mRS));
    162         typeI64_4Builder.setX(1);
    163         typeI64_4Builder.setY(6);
    164 
    165         mLongAllocation = Allocation.createSized(mRS, Element.I64(mRS), mAllocSize);
    166         mRS.finish();
    167         mLong2Allocation = Allocation.createSized(mRS, Element.I64_2(mRS), mAllocSize / 2);
    168         mRS.finish();
    169         mLong3Allocation = Allocation.createSized(mRS, Element.I64_3(mRS), mAllocSize / 4);
    170         mRS.finish();
    171         mLong4Allocation = Allocation.createTyped(mRS, typeI64_4Builder.create());
    172         mRS.finish();
    173 
    174         mBoolAllocation = Allocation.createSized(mRS, Element.BOOLEAN(mRS), mAllocSize);
    175         mRS.finish();
    176     }
    177 
    178     private void initSignedAllocations() {
    179         byte[] buffer_char = new byte[mAllocSize];
    180         short[] buffer_short = new short[mAllocSize];
    181         int[] buffer_int = new int[mAllocSize];
    182         long[] buffer_long = new long[mAllocSize];
    183         byte[] buffer_bool = new byte[mAllocSize];
    184 
    185         for(int i = 0; i < mAllocSize; ++i) {
    186             buffer_char[i] = (byte) i;
    187             buffer_short[i] = (short) i;
    188             buffer_int[i] = (int) i;
    189             buffer_long[i] = (long) i;
    190             buffer_bool[i] =  (byte) (0x01 & i);
    191         }
    192 
    193         mCharAllocation.copyFrom(buffer_char);
    194         mChar2Allocation.copyFrom(buffer_char);
    195         mChar3Allocation.copyFrom(buffer_char);
    196         mChar4Allocation.copyFrom(buffer_char);
    197 
    198         mShortAllocation.copyFrom(buffer_short);
    199         mShort2Allocation.copyFrom(buffer_short);
    200         mShort3Allocation.copyFrom(buffer_short);
    201         mShort4Allocation.copyFrom(buffer_short);
    202 
    203         mIntAllocation.copyFrom(buffer_int);
    204         mInt2Allocation.copyFrom(buffer_int);
    205         mInt3Allocation.copyFrom(buffer_int);
    206         mInt4Allocation.copyFrom(buffer_int);
    207 
    208         mLongAllocation.copyFrom(buffer_long);
    209         mLong2Allocation.copyFrom(buffer_long);
    210         mLong3Allocation.copyFrom(buffer_long);
    211         mLong4Allocation.copyFrom(buffer_long);
    212 
    213         mBoolAllocation.copyFromUnchecked(buffer_bool);
    214     }
    215 
    216     private void createUnsignedAllocations() {
    217         Type.Builder typeU8_2Builder = new Type.Builder(mRS, Element.U8_2(mRS));
    218         typeU8_2Builder.setX(2);
    219         typeU8_2Builder.setY(6);
    220 
    221         mUCharAllocation = Allocation.createSized(mRS, Element.U8(mRS), mAllocSize);
    222         mRS.finish();
    223         mUChar2Allocation = Allocation.createTyped(mRS, typeU8_2Builder.create());
    224         mRS.finish();
    225         mUChar3Allocation = Allocation.createSized(mRS, Element.U8_3(mRS), mAllocSize / 4);
    226         mRS.finish();
    227         mUChar4Allocation = Allocation.createSized(mRS, Element.U8_4(mRS), mAllocSize / 4);
    228         mRS.finish();
    229 
    230         Type.Builder typeU16_3Builder = new Type.Builder(mRS, Element.U16_3(mRS));
    231         typeU16_3Builder.setX(1);
    232         typeU16_3Builder.setY(6);
    233 
    234         mUShortAllocation = Allocation.createSized(mRS, Element.U16(mRS), mAllocSize);
    235         mRS.finish();
    236         mUShort2Allocation = Allocation.createSized(mRS, Element.U16_2(mRS), mAllocSize / 2);
    237         mRS.finish();
    238         mUShort3Allocation = Allocation.createTyped(mRS, typeU16_3Builder.create());
    239         mRS.finish();
    240         mUShort4Allocation = Allocation.createSized(mRS, Element.U16_4(mRS), mAllocSize / 4);
    241         mRS.finish();
    242 
    243         Type.Builder typeU32_4Builder = new Type.Builder(mRS, Element.U32_4(mRS));
    244         typeU32_4Builder.setX(1);
    245         typeU32_4Builder.setY(1);
    246         typeU32_4Builder.setZ(6);
    247 
    248         mUIntAllocation = Allocation.createSized(mRS, Element.U32(mRS), mAllocSize);
    249         mRS.finish();
    250         mUInt2Allocation = Allocation.createSized(mRS, Element.U32_2(mRS), mAllocSize / 2);
    251         mRS.finish();
    252         mUInt3Allocation = Allocation.createSized(mRS, Element.U32_3(mRS), mAllocSize / 4);
    253         mRS.finish();
    254         mUInt4Allocation = Allocation.createTyped(mRS, typeU32_4Builder.create());
    255         mRS.finish();
    256 
    257         Type.Builder typeU64Builder = new Type.Builder(mRS, Element.U64(mRS));
    258         typeU64Builder.setX(4);
    259         typeU64Builder.setY(3);
    260         typeU64Builder.setZ(2);
    261 
    262         mULongAllocation = Allocation.createTyped(mRS, typeU64Builder.create());
    263         mRS.finish();
    264         mULong2Allocation = Allocation.createSized(mRS, Element.U64_2(mRS), mAllocSize / 2);
    265         mRS.finish();
    266         mULong3Allocation = Allocation.createSized(mRS, Element.U64_3(mRS), mAllocSize / 4);
    267         mRS.finish();
    268         mULong4Allocation = Allocation.createSized(mRS, Element.U64_4(mRS), mAllocSize / 4);
    269         mRS.finish();
    270     }
    271 
    272     private void initUnsignedAllocations() {
    273         byte[] buffer_char = new byte[mAllocSize];
    274         short[] buffer_short = new short[mAllocSize];
    275         int[] buffer_int = new int[mAllocSize];
    276         long[] buffer_long = new long[mAllocSize];
    277 
    278         for(int i = 0; i < mAllocSize; ++i) {
    279             buffer_char[i] = (byte) i;
    280             buffer_short[i] = (short) i;
    281             buffer_int[i] = (int) i;
    282             buffer_long[i] = (long) i;
    283         }
    284 
    285         mUCharAllocation.copyFrom(buffer_char);
    286         mUChar2Allocation.copyFrom(buffer_char);
    287         mUChar3Allocation.copyFrom(buffer_char);
    288         mUChar4Allocation.copyFrom(buffer_char);
    289 
    290         mUShortAllocation.copyFrom(buffer_short);
    291         mUShort2Allocation.copyFrom(buffer_short);
    292         mUShort3Allocation.copyFrom(buffer_short);
    293         mUShort4Allocation.copyFrom(buffer_short);
    294 
    295         mUIntAllocation.copyFrom(buffer_int);
    296         mUInt2Allocation.copyFrom(buffer_int);
    297         mUInt3Allocation.copyFrom(buffer_int);
    298         mUInt4Allocation.copyFrom(buffer_int);
    299 
    300         mULongAllocation.copyFrom(buffer_long);
    301         mULong2Allocation.copyFrom(buffer_long);
    302         mULong3Allocation.copyFrom(buffer_long);
    303         mULong4Allocation.copyFrom(buffer_long);
    304     }
    305 
    306     private void createFloatAllocations() {
    307         Type.Builder typeF16_3Builder = new Type.Builder(mRS, Element.F16_3(mRS));
    308         typeF16_3Builder.setX(1);
    309         typeF16_3Builder.setY(6);
    310 
    311         mHalfAllocation = Allocation.createSized(mRS, Element.F16(mRS), mAllocSize);
    312         mRS.finish();
    313         mHalf2Allocation = Allocation.createSized(mRS, Element.F16_2(mRS), mAllocSize / 2);
    314         mRS.finish();
    315         mHalf3Allocation = Allocation.createTyped(mRS, typeF16_3Builder.create());
    316         mRS.finish();
    317         mHalf4Allocation = Allocation.createSized(mRS, Element.F16_4(mRS), mAllocSize / 4);
    318         mRS.finish();
    319 
    320         Type.Builder typeF32_4Builder = new Type.Builder(mRS, Element.F32_4(mRS));
    321         typeF32_4Builder.setX(3);
    322         typeF32_4Builder.setY(2);
    323 
    324         mFloatAllocation = Allocation.createSized(mRS, Element.F32(mRS), mAllocSize);
    325         mRS.finish();
    326         mFloat2Allocation = Allocation.createSized(mRS, Element.F32_2(mRS), mAllocSize / 2);
    327         mRS.finish();
    328         mFloat3Allocation = Allocation.createSized(mRS, Element.F32_3(mRS), mAllocSize / 4);
    329         mRS.finish();
    330         mFloat4Allocation = Allocation.createTyped(mRS, typeF32_4Builder.create());
    331         mRS.finish();
    332 
    333         Type.Builder typeF64_2Builder = new Type.Builder(mRS, Element.F64_2(mRS));
    334         typeF64_2Builder.setX(4);
    335         typeF64_2Builder.setY(1);
    336         typeF64_2Builder.setZ(3);
    337 
    338         mDoubleAllocation = Allocation.createSized(mRS, Element.F64(mRS), mAllocSize);
    339         mRS.finish();
    340         mDouble2Allocation = Allocation.createTyped(mRS, typeF64_2Builder.create());
    341         mRS.finish();
    342 
    343         Type.Builder typeF64_3Builder = new Type.Builder(mRS, Element.F64_3(mRS));
    344         typeF64_3Builder.setX(1);
    345         typeF64_3Builder.setY(2);
    346         typeF64_3Builder.setZ(3);
    347 
    348         Type.Builder typeF64_4Builder = new Type.Builder(mRS, Element.F64_4(mRS));
    349         typeF64_4Builder.setX(1);
    350         typeF64_4Builder.setY(2);
    351         typeF64_4Builder.setZ(3);
    352 
    353         mDouble3Allocation = Allocation.createTyped(mRS, typeF64_3Builder.create());
    354         mRS.finish();
    355         mDouble4Allocation = Allocation.createTyped(mRS, typeF64_4Builder.create());
    356         mRS.finish();
    357     }
    358 
    359     private void initFloatAllocations() {
    360         // No java type for half precision float, so bitcast 16-bit int
    361         short[] buffer_half = new short[mAllocSize];
    362         float[] buffer_float = new float[mAllocSize];
    363         double[] buffer_double = new double[mAllocSize];
    364 
    365         for(int i = 0; i < mAllocSize; ++i) {
    366             // Construct IEEE 754 half with increasing fraction.
    367             byte mantissa = (byte)(i);
    368             byte exponent = 0b00111100; // keep exponent constant at one
    369             buffer_half[i] = (short)((exponent << 8) | mantissa);
    370 
    371             buffer_float[i] = (float) 1 / i;
    372             buffer_double[i] = (double) 1 / i;
    373         }
    374 
    375         mHalfAllocation.copyFromUnchecked(buffer_half);
    376         mHalf2Allocation.copyFromUnchecked(buffer_half);
    377         mHalf3Allocation.copyFromUnchecked(buffer_half);
    378         mHalf4Allocation.copyFromUnchecked(buffer_half);
    379 
    380         mFloatAllocation.copyFrom(buffer_float);
    381         mFloat2Allocation.copyFrom(buffer_float);
    382         mFloat3Allocation.copyFrom(buffer_float);
    383         mFloat4Allocation.copyFrom(buffer_float);
    384 
    385         mDoubleAllocation.copyFrom(buffer_double);
    386         mDouble2Allocation.copyFrom(buffer_double);
    387         mDouble3Allocation.copyFrom(buffer_double);
    388         mDouble4Allocation.copyFrom(buffer_double);
    389     }
    390 
    391     private void createStructAllocations() {
    392         ScriptField_complexStruct complex_struct;
    393 
    394         complex_struct = new ScriptField_complexStruct(mRS, mAllocSize);
    395         mRS.finish();
    396         mScript.bind_g_complexStruct_in(complex_struct);
    397         mRS.finish();
    398         mStructInAlloc = complex_struct.getAllocation();
    399         mRS.finish();
    400 
    401         complex_struct = new ScriptField_complexStruct(mRS, mAllocSize);
    402         mRS.finish();
    403         mScript.bind_g_complexStruct_out(complex_struct);
    404         mRS.finish();
    405         mStructOutAlloc = complex_struct.getAllocation();
    406         mRS.finish();
    407     }
    408 
    409     private void overwriteFloatAllocations() {
    410         float[] buffer_float = new float[mAllocSize];
    411 
    412         // Set float allocations to -1/n
    413         for(int i = 0; i < mAllocSize; ++i) {
    414             buffer_float[i] = -1f / i;
    415         }
    416 
    417         mFloatAllocation.copyFrom(buffer_float);
    418         mFloat2Allocation.copyFrom(buffer_float);
    419         mFloat3Allocation.copyFrom(buffer_float);
    420         mFloat4Allocation.copyFrom(buffer_float);
    421     }
    422 
    423     private void runScript() {
    424         mInAllocation = Allocation.createFromBitmap(mRS, mBitmapIn);
    425         mRS.finish();
    426         mOutAllocation = Allocation.createFromBitmap(mRS, mBitmapOut);
    427         mRS.finish();
    428 
    429 
    430         createSignedAllocations();
    431         initSignedAllocations();
    432 
    433         mRS.finish();
    434         mScript.forEach_swizzle_kernel(mInAllocation, mOutAllocation);
    435         mRS.finish();
    436 
    437         mOutAllocation.copyTo(mBitmapOut);
    438 
    439         mCharAllocation.destroy();
    440         mRS.finish();
    441         mChar2Allocation.destroy();
    442         mRS.finish();
    443         mChar3Allocation.destroy();
    444         mRS.finish();
    445         mChar4Allocation.destroy();
    446         mRS.finish();
    447 
    448         mShort2Allocation.destroy();
    449         mRS.finish();
    450         mShort3Allocation.destroy();
    451         mRS.finish();
    452         mShort4Allocation.destroy();
    453         mRS.finish();
    454 
    455         mIntAllocation.destroy();
    456         mRS.finish();
    457         mInt2Allocation.destroy();
    458         mRS.finish();
    459         mInt3Allocation.destroy();
    460         mRS.finish();
    461         mInt4Allocation.destroy();
    462         mRS.finish();
    463 
    464         mLongAllocation.destroy();
    465         mRS.finish();
    466         mLong2Allocation.destroy();
    467         mRS.finish();
    468         mLong3Allocation.destroy();
    469         mRS.finish();
    470         mLong4Allocation.destroy();
    471         mRS.finish();
    472 
    473         mBoolAllocation.destroy();
    474         mRS.finish();
    475 
    476 
    477         createUnsignedAllocations();
    478         initUnsignedAllocations();
    479 
    480         mInAllocation = mUShortAllocation; // Host side assignment
    481 
    482         mRS.finish();
    483         mScript.forEach_square_kernel(mInAllocation, mUIntAllocation);
    484         mRS.finish();
    485 
    486         mUCharAllocation.destroy();
    487         mRS.finish();
    488         mUChar2Allocation.destroy();
    489         mRS.finish();
    490         mUChar3Allocation.destroy();
    491         mRS.finish();
    492         mUChar4Allocation.destroy();
    493         mRS.finish();
    494 
    495         mUShortAllocation.destroy();
    496         mRS.finish();
    497         mUShort2Allocation.destroy();
    498         mRS.finish();
    499         mUShort3Allocation.destroy();
    500         mRS.finish();
    501         mUShort4Allocation.destroy();
    502         mRS.finish();
    503 
    504         mUInt2Allocation.destroy();
    505         mRS.finish();
    506         mUInt3Allocation.destroy();
    507         mRS.finish();
    508         mUInt4Allocation.destroy();
    509         mRS.finish();
    510 
    511         mULongAllocation.destroy();
    512         mRS.finish();
    513         mULong2Allocation.destroy();
    514         mRS.finish();
    515         mULong3Allocation.destroy();
    516         mRS.finish();
    517         mULong4Allocation.destroy();
    518         mRS.finish();
    519 
    520 
    521         createFloatAllocations();
    522         initFloatAllocations();
    523 
    524         mRS.finish();
    525         mScript.forEach_add_half_kernel(mDouble4Allocation, mDouble3Allocation);
    526         mRS.finish();
    527 
    528         mHalfAllocation.destroy();
    529         mRS.finish();
    530         mHalf2Allocation.destroy();
    531         mRS.finish();
    532         mHalf3Allocation.destroy();
    533         mRS.finish();
    534         mHalf4Allocation.destroy();
    535         mRS.finish();
    536 
    537         mDoubleAllocation.destroy();
    538         mRS.finish();
    539         mDouble2Allocation.destroy();
    540         mRS.finish();
    541         mDouble4Allocation.destroy();
    542         mRS.finish();
    543 
    544         overwriteFloatAllocations();
    545 
    546         createStructAllocations();
    547 
    548         mRS.finish();
    549         mScript.forEach_struct_kernel(mStructInAlloc, mStructOutAlloc);
    550         mRS.finish();
    551     }
    552 }
    553