Home | History | Annotate | Download | only in sample
      1 /*
      2  * Copyright (C) 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 com.android.rs.sample;
     18 
     19 import android.app.Activity;
     20 import android.graphics.Bitmap;
     21 import android.graphics.Bitmap.Config;
     22 import android.graphics.BitmapFactory;
     23 import android.graphics.Canvas;
     24 import android.graphics.SurfaceTexture;
     25 import android.os.Bundle;
     26 import android.renderscript.Allocation;
     27 import android.renderscript.Element;
     28 import android.renderscript.Matrix3f;
     29 import android.renderscript.RenderScript;
     30 import android.renderscript.Sampler;
     31 import android.renderscript.Type;
     32 import android.renderscript.Type.Builder;
     33 import android.util.Log;
     34 import android.view.TextureView;
     35 import android.view.TextureView.SurfaceTextureListener;
     36 import android.view.View;
     37 import android.widget.ImageView;
     38 import android.widget.SeekBar;
     39 import android.widget.TextView;
     40 
     41 public class SampleRSActivity extends Activity {
     42     class TextureViewUpdater implements TextureView.SurfaceTextureListener {
     43         private Allocation mOutPixelsAllocation;
     44         private Sampler mSampler;
     45 
     46         TextureViewUpdater(Allocation outAlloc, Sampler sampler) {
     47             mOutPixelsAllocation = outAlloc;
     48             mSampler = sampler;
     49         }
     50 
     51         public void onSurfaceTextureUpdated(SurfaceTexture surface) {
     52         }
     53 
     54         public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
     55             mOutPixelsAllocation.setSurfaceTexture(surface);
     56         }
     57 
     58         public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
     59             mOutPixelsAllocation.setSurfaceTexture(surface);
     60             filterAlloc(mOutPixelsAllocation, mSampler);
     61         }
     62 
     63         public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
     64             mOutPixelsAllocation.setSurfaceTexture(null);
     65             return true;
     66         }
     67     }
     68 
     69     private final String TAG = "Img";
     70     private Bitmap mBitmapTwoByTwo;
     71     private Bitmap mBitmapCity;
     72 
     73     private TextView mBenchmarkResult;
     74 
     75     private RenderScript mRS;
     76     private Allocation mTwoByTwoAlloc;
     77     private Allocation mCityAlloc;
     78     private ScriptC_sample mScript;
     79 
     80     public void onStartTrackingTouch(SeekBar seekBar) {
     81     }
     82 
     83     public void onStopTrackingTouch(SeekBar seekBar) {
     84     }
     85 
     86     @Override
     87     protected void onCreate(Bundle savedInstanceState) {
     88         super.onCreate(savedInstanceState);
     89         setContentView(R.layout.rs);
     90 
     91         mBitmapTwoByTwo = loadBitmap(R.drawable.twobytwo);
     92         mBitmapCity = loadBitmap(R.drawable.city);
     93 
     94         mBenchmarkResult = (TextView) findViewById(R.id.benchmarkText);
     95         mBenchmarkResult.setText("Result: not run");
     96 
     97         mRS = RenderScript.create(this);
     98         mTwoByTwoAlloc = Allocation.createFromBitmap(mRS, mBitmapTwoByTwo,
     99                                                           Allocation.MipmapControl.MIPMAP_NONE,
    100                                                           Allocation.USAGE_SCRIPT);
    101 
    102         mCityAlloc = Allocation.createFromBitmap(mRS, mBitmapCity,
    103                                                           Allocation.MipmapControl.MIPMAP_NONE,
    104                                                           Allocation.USAGE_SCRIPT);
    105 
    106         Type.Builder b = new Type.Builder(mRS, Element.RGBA_8888(mRS));
    107 
    108         int usage = Allocation.USAGE_SCRIPT | Allocation.USAGE_IO_OUTPUT;
    109 
    110         int outX = 256;
    111         int outY = 256;
    112 
    113         // Wrap Linear
    114         Allocation outAlloc = Allocation.createTyped(mRS, b.setX(outX).setY(outY).create(), usage);
    115         TextureViewUpdater updater = new TextureViewUpdater(outAlloc, Sampler.WRAP_LINEAR(mRS));
    116         TextureView displayView = (TextureView) findViewById(R.id.display);
    117         displayView.setSurfaceTextureListener(updater);
    118 
    119         // Clamp Linear
    120         outAlloc = Allocation.createTyped(mRS, b.setX(outX).setY(outY).create(), usage);
    121         updater = new TextureViewUpdater(outAlloc, Sampler.CLAMP_LINEAR(mRS));
    122         displayView = (TextureView) findViewById(R.id.display2);
    123         displayView.setSurfaceTextureListener(updater);
    124 
    125         // Wrap Nearest
    126         outAlloc = Allocation.createTyped(mRS, b.setX(outX).setY(outY).create(), usage);
    127         updater = new TextureViewUpdater(outAlloc, Sampler.WRAP_NEAREST(mRS));
    128         displayView = (TextureView) findViewById(R.id.display3);
    129         displayView.setSurfaceTextureListener(updater);
    130 
    131         // Clamp Nearest
    132         outAlloc = Allocation.createTyped(mRS, b.setX(outX).setY(outY).create(), usage);
    133         updater = new TextureViewUpdater(outAlloc, Sampler.CLAMP_NEAREST(mRS));
    134         displayView = (TextureView) findViewById(R.id.display4);
    135         displayView.setSurfaceTextureListener(updater);
    136 
    137         mScript = new ScriptC_sample(mRS, getResources(), R.raw.sample);
    138     }
    139 
    140     private Bitmap loadBitmap(int resource) {
    141         final BitmapFactory.Options options = new BitmapFactory.Options();
    142         options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    143         Bitmap b = BitmapFactory.decodeResource(getResources(), resource, options);
    144         Bitmap b2 = Bitmap.createBitmap(b.getWidth(), b.getHeight(), b.getConfig());
    145         Canvas c = new Canvas(b2);
    146         c.drawBitmap(b, 0, 0, null);
    147         b.recycle();
    148         return b2;
    149     }
    150 
    151     private synchronized void filterAlloc(Allocation alloc, Sampler sampler) {
    152         long t = java.lang.System.currentTimeMillis();
    153         mScript.invoke_setSampleData(alloc, mTwoByTwoAlloc, sampler);
    154         mScript.forEach_root(alloc);
    155         alloc.ioSendOutput();
    156         mRS.finish();
    157         t = java.lang.System.currentTimeMillis() - t;
    158         Log.i(TAG, "Filter time is: " + t + " ms");
    159     }
    160 
    161     public void benchmark(View v) {
    162         /*filterAlloc();
    163         long t = java.lang.System.currentTimeMillis();
    164         filterAlloc();
    165         t = java.lang.System.currentTimeMillis() - t;
    166         mDisplayView.invalidate();
    167         mBenchmarkResult.setText("Result: " + t + " ms");*/
    168     }
    169 }
    170