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 17package com.android.rs.kernelvariables; 18 19import android.app.Activity; 20import android.graphics.Bitmap; 21import android.graphics.ImageFormat; 22import android.os.Bundle; 23import android.widget.ImageView; 24import android.renderscript.*; 25 26public class MainActivity extends Activity { 27 private Bitmap mBitmapIn; 28 private Bitmap mBitmapOut; 29 private ImageView mImageView; 30 31 private RenderScript mRS; 32 private Allocation mInAllocation; 33 private Allocation mOutAllocation; 34 private ScriptC_simple mScript; 35 36 @Override 37 protected void onCreate(Bundle savedInstanceState) { 38 super.onCreate(savedInstanceState); 39 40 setContentView(R.layout.main_layout); 41 42 mBitmapIn = Bitmap.createBitmap(500, 500, Bitmap.Config.ARGB_8888); 43 mBitmapOut = Bitmap.createBitmap(mBitmapIn.getWidth(), 44 mBitmapIn.getHeight(), mBitmapIn.getConfig()); 45 46 mImageView = findViewById(R.id.imageView); 47 mImageView.setImageBitmap(mBitmapOut); 48 49 createScript(); 50 updateImage(); 51 } 52 53 private void createScript() { 54 mRS = RenderScript.create(this, 55 RenderScript.ContextType.NORMAL, 56 RenderScript.CREATE_FLAG_LOW_LATENCY | 57 RenderScript.CREATE_FLAG_WAIT_FOR_ATTACH); 58 59 mInAllocation = Allocation.createFromBitmap(mRS, mBitmapIn); 60 mOutAllocation = Allocation.createFromBitmap(mRS, mBitmapOut); 61 62 mScript = new ScriptC_simple(mRS); 63 } 64 65 private void updateImage() { 66 int[] buffer_int = {1, 2, 3, 4}; 67 Allocation int_allocation = Allocation.createSized(mRS, Element.I32(mRS), 4); 68 int_allocation.copyFrom(buffer_int); 69 mScript.set_allocation_1D_global(int_allocation); 70 71 int[] buffer_int2 = {5, 6, 7, 8}; 72 73 Type.Builder typeI32Builder2D = new Type.Builder(mRS, Element.I32(mRS)); 74 typeI32Builder2D.setX(2); 75 typeI32Builder2D.setY(2); 76 77 Allocation int_allocation2 = Allocation.createTyped(mRS, typeI32Builder2D.create()); 78 int_allocation2.copyFrom(buffer_int2); 79 mScript.set_allocation_1D_global2(int_allocation2); 80 81 mScript.set_allocation_2D_global(mInAllocation); 82 mScript.set_allocation_2D_global2(mOutAllocation); 83 84 int[] buffer_int3 = new int[64]; 85 86 for (int i=0; i<4*4*4; ++i) 87 buffer_int3[i] = 9 + i; 88 89 Type.Builder typeI32Builder3D = new Type.Builder(mRS, Element.I32(mRS)); 90 typeI32Builder3D.setX(4); 91 typeI32Builder3D.setY(4); 92 typeI32Builder3D.setZ(4); 93 94 Allocation int_allocation3 = Allocation.createTyped(mRS, typeI32Builder3D.create()); 95 int_allocation3.copyFrom(buffer_int3); 96 mScript.set_allocation_3D_global(int_allocation3); 97 98 Type.Builder yuvTypeBuilder = new Type.Builder(mRS, Element.YUV(mRS)); 99 yuvTypeBuilder.setX(4); 100 yuvTypeBuilder.setY(4); 101 yuvTypeBuilder.setYuvFormat(ImageFormat.YV12); 102 Allocation yuv_allocation = Allocation.createTyped(mRS, yuvTypeBuilder.create()); 103 mScript.set_allocation_YUV_2D_global(yuv_allocation); 104 105 mScript.set_sampler_global(Sampler.CLAMP_LINEAR(mRS)); 106 107 mScript.forEach_kernel(mInAllocation, mOutAllocation); 108 mOutAllocation.copyTo(mBitmapOut); 109 } 110} 111