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