|
| 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.BitmapFactory; |
| 22 | +import android.graphics.Canvas; |
| 23 | +import android.graphics.SurfaceTexture; |
| 24 | +import android.os.Bundle; |
| 25 | +import android.renderscript.Allocation; |
| 26 | +import android.renderscript.Element; |
| 27 | +import android.renderscript.Matrix3f; |
| 28 | +import android.renderscript.RenderScript; |
| 29 | +import android.renderscript.Sampler; |
| 30 | +import android.renderscript.Type; |
| 31 | +import android.renderscript.Type.Builder; |
| 32 | +import android.util.Log; |
| 33 | +import android.view.TextureView; |
| 34 | +import android.view.View; |
| 35 | +import android.widget.ImageView; |
| 36 | +import android.widget.SeekBar; |
| 37 | +import android.widget.TextView; |
| 38 | + |
| 39 | +public class SampleRSActivity extends Activity |
| 40 | + implements TextureView.SurfaceTextureListener |
| 41 | +{ |
| 42 | + private final String TAG = "Img"; |
| 43 | + private Bitmap mBitmapIn; |
| 44 | + private TextureView mDisplayView; |
| 45 | + |
| 46 | + private TextView mBenchmarkResult; |
| 47 | + |
| 48 | + private RenderScript mRS; |
| 49 | + private Allocation mInPixelsAllocation; |
| 50 | + private Allocation mOutPixelsAllocation; |
| 51 | + private ScriptC_sample mScript; |
| 52 | + |
| 53 | + public void onStartTrackingTouch(SeekBar seekBar) { |
| 54 | + } |
| 55 | + |
| 56 | + public void onStopTrackingTouch(SeekBar seekBar) { |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + protected void onCreate(Bundle savedInstanceState) { |
| 61 | + super.onCreate(savedInstanceState); |
| 62 | + setContentView(R.layout.rs); |
| 63 | + |
| 64 | + mBitmapIn = loadBitmap(R.drawable.twobytwo); |
| 65 | + mDisplayView = (TextureView) findViewById(R.id.display); |
| 66 | + |
| 67 | + mBenchmarkResult = (TextView) findViewById(R.id.benchmarkText); |
| 68 | + mBenchmarkResult.setText("Result: not run"); |
| 69 | + |
| 70 | + mRS = RenderScript.create(this); |
| 71 | + mInPixelsAllocation = Allocation.createFromBitmap(mRS, mBitmapIn, |
| 72 | + Allocation.MipmapControl.MIPMAP_NONE, |
| 73 | + Allocation.USAGE_SCRIPT); |
| 74 | + |
| 75 | + Type.Builder b = new Type.Builder(mRS, Element.RGBA_8888(mRS)); |
| 76 | + |
| 77 | + mOutPixelsAllocation = Allocation.createTyped(mRS, b.setX(32).setY(32).create(), |
| 78 | + Allocation.USAGE_SCRIPT | |
| 79 | + Allocation.USAGE_IO_OUTPUT); |
| 80 | + mDisplayView.setSurfaceTextureListener(this); |
| 81 | + |
| 82 | + mScript = new ScriptC_sample(mRS, getResources(), R.raw.sample); |
| 83 | + |
| 84 | + mScript.set_sourceAlloc(mInPixelsAllocation); |
| 85 | + mScript.set_destAlloc(mOutPixelsAllocation); |
| 86 | + mScript.set_wrapUV(Sampler.WRAP_LINEAR(mRS)); |
| 87 | + mScript.set_clampUV(Sampler.CLAMP_LINEAR(mRS)); |
| 88 | + } |
| 89 | + |
| 90 | + private Bitmap loadBitmap(int resource) { |
| 91 | + final BitmapFactory.Options options = new BitmapFactory.Options(); |
| 92 | + options.inPreferredConfig = Bitmap.Config.ARGB_8888; |
| 93 | + Bitmap b = BitmapFactory.decodeResource(getResources(), resource, options); |
| 94 | + Bitmap b2 = Bitmap.createBitmap(b.getWidth(), b.getHeight(), b.getConfig()); |
| 95 | + Canvas c = new Canvas(b2); |
| 96 | + c.drawBitmap(b, 0, 0, null); |
| 97 | + b.recycle(); |
| 98 | + return b2; |
| 99 | + } |
| 100 | + |
| 101 | + private void filter() { |
| 102 | + long t = java.lang.System.currentTimeMillis(); |
| 103 | + mScript.forEach_root(mOutPixelsAllocation); |
| 104 | + mOutPixelsAllocation.ioSendOutput(); |
| 105 | + mRS.finish(); |
| 106 | + t = java.lang.System.currentTimeMillis() - t; |
| 107 | + Log.i(TAG, "Filter time is: " + t + " ms"); |
| 108 | + } |
| 109 | + |
| 110 | + public void benchmark(View v) { |
| 111 | + filter(); |
| 112 | + long t = java.lang.System.currentTimeMillis(); |
| 113 | + filter(); |
| 114 | + t = java.lang.System.currentTimeMillis() - t; |
| 115 | + mDisplayView.invalidate(); |
| 116 | + mBenchmarkResult.setText("Result: " + t + " ms"); |
| 117 | + } |
| 118 | + |
| 119 | + |
| 120 | + @Override |
| 121 | + public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) { |
| 122 | + mOutPixelsAllocation.setSurfaceTexture(surface); |
| 123 | + filter(); |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) { |
| 128 | + mOutPixelsAllocation.setSurfaceTexture(surface); |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) { |
| 133 | + mOutPixelsAllocation.setSurfaceTexture(null); |
| 134 | + return true; |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public void onSurfaceTextureUpdated(SurfaceTexture surface) { |
| 139 | + } |
| 140 | +} |
0 commit comments