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.singlesource; 18 19 import android.app.Activity; 20 import android.graphics.Bitmap; 21 import android.os.Bundle; 22 import android.widget.ImageView; 23 import android.renderscript.*; 24 25 public class MainActivity extends Activity { 26 27 private RenderScript mRS; 28 private Allocation mAllocIn1; 29 private Allocation mAllocIn2; 30 private Allocation mAllocOut; 31 private ScriptC_rs_single_source mScript; 32 33 @Override onCreate(Bundle savedInstanceState)34 protected void onCreate(Bundle savedInstanceState) { 35 36 super.onCreate(savedInstanceState); 37 38 setContentView(R.layout.main_layout); 39 40 // create renderscript context 41 mRS = RenderScript.create( 42 this, 43 RenderScript.ContextType.NORMAL, 44 RenderScript.CREATE_FLAG_WAIT_FOR_ATTACH | 45 RenderScript.CREATE_FLAG_LOW_LATENCY); 46 47 // create a new instance of the script 48 mScript = new ScriptC_rs_single_source(mRS); 49 50 // create the first input allocation 51 mAllocIn1 = Allocation.createSized(mRS, Element.F32(mRS), 4); 52 float [] in1 = new float[]{ 1.f, 2.f, 3.f, 4.f }; 53 mAllocIn1.copyFrom(in1); 54 55 // create second input allocation 56 mAllocIn2 = Allocation.createSized(mRS, Element.F32(mRS), 4); 57 float [] in2 = new float[]{ 5.f, 6.f, 7.f, 8.f }; 58 mAllocIn2.copyFrom(in2); 59 60 // create output allocation 61 mAllocOut = Allocation.createSized(mRS, Element.F32(mRS), 4); 62 63 // setup the global output allocation 64 mScript.set_global_alloc(Allocation.createSized(mRS, Element.F32(mRS), 4)); 65 66 // invoke static function 1 67 mScript.invoke_script_invoke_1(mAllocOut, mAllocIn1, mAllocIn2); 68 69 // invoke static function 2 70 mScript.invoke_script_invoke_2(); 71 72 // invoke void kernel 73 Script.LaunchOptions options = new Script.LaunchOptions(); 74 options.setX(0, 4); 75 mScript.forEach_void_kernel_1(options); 76 } 77 } 78