Home | History | Annotate | Download | only in hellocompute
      1 /*
      2  * Copyright (C) 2011 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.example.android.rs.hellocompute;
     18 
     19 import android.app.Activity;
     20 import android.os.Bundle;
     21 import android.graphics.BitmapFactory;
     22 import android.graphics.Bitmap;
     23 import android.renderscript.RenderScript;
     24 import android.renderscript.Allocation;
     25 import android.widget.ImageView;
     26 
     27 public class HelloCompute extends Activity {
     28     private Bitmap mBitmapIn;
     29     private Bitmap mBitmapOut;
     30 
     31     private RenderScript mRS;
     32     private Allocation mInAllocation;
     33     private Allocation mOutAllocation;
     34     private ScriptC_mono mScript;
     35 
     36     @Override
     37     protected void onCreate(Bundle savedInstanceState) {
     38         super.onCreate(savedInstanceState);
     39         setContentView(R.layout.main);
     40 
     41         mBitmapIn = loadBitmap(R.drawable.data);
     42         mBitmapOut = Bitmap.createBitmap(mBitmapIn.getWidth(), mBitmapIn.getHeight(),
     43                                          mBitmapIn.getConfig());
     44 
     45         ImageView in = (ImageView) findViewById(R.id.displayin);
     46         in.setImageBitmap(mBitmapIn);
     47 
     48         ImageView out = (ImageView) findViewById(R.id.displayout);
     49         out.setImageBitmap(mBitmapOut);
     50 
     51         createScript();
     52     }
     53 
     54 
     55     private void createScript() {
     56         mRS = RenderScript.create(this);
     57 
     58         mInAllocation = Allocation.createFromBitmap(mRS, mBitmapIn,
     59                                                     Allocation.MipmapControl.MIPMAP_NONE,
     60                                                     Allocation.USAGE_SCRIPT);
     61         mOutAllocation = Allocation.createFromBitmap(mRS, mBitmapOut,
     62                                                      Allocation.MipmapControl.MIPMAP_NONE,
     63                                                      Allocation.USAGE_SCRIPT);
     64 
     65         mScript = new ScriptC_mono(mRS);
     66 
     67         mScript.forEach_root(mInAllocation, mOutAllocation);
     68         mOutAllocation.copyTo(mBitmapOut);
     69     }
     70 
     71     private Bitmap loadBitmap(int resource) {
     72         final BitmapFactory.Options options = new BitmapFactory.Options();
     73         options.inPreferredConfig = Bitmap.Config.ARGB_8888;
     74         return BitmapFactory.decodeResource(getResources(), resource, options);
     75     }
     76 }
     77