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            return true;
70        }
71    }
72
73    private final String TAG = "Img";
74    private Bitmap mBitmapTwoByTwo;
75    private Bitmap mBitmapCity;
76
77    private TextView mBenchmarkResult;
78
79    private RenderScript mRS;
80    private Allocation mTwoByTwoAlloc;
81    private Allocation mCityAlloc;
82    private ScriptC_sample mScript;
83
84    public void onStartTrackingTouch(SeekBar seekBar) {
85    }
86
87    public void onStopTrackingTouch(SeekBar seekBar) {
88    }
89
90    @Override
91    protected void onCreate(Bundle savedInstanceState) {
92        super.onCreate(savedInstanceState);
93        setContentView(R.layout.rs);
94
95        mBitmapTwoByTwo = loadBitmap(R.drawable.twobytwo);
96        mBitmapCity = loadBitmap(R.drawable.city);
97
98        mBenchmarkResult = (TextView) findViewById(R.id.benchmarkText);
99        mBenchmarkResult.setText("Result: not run");
100
101        mRS = RenderScript.create(this);
102        mTwoByTwoAlloc = Allocation.createFromBitmap(mRS, mBitmapTwoByTwo,
103                                                          Allocation.MipmapControl.MIPMAP_NONE,
104                                                          Allocation.USAGE_SCRIPT | Allocation.USAGE_GRAPHICS_TEXTURE);
105
106        mCityAlloc = Allocation.createFromBitmap(mRS, mBitmapCity,
107                                                          Allocation.MipmapControl.MIPMAP_NONE,
108                                                          Allocation.USAGE_SCRIPT | Allocation.USAGE_GRAPHICS_TEXTURE);
109
110        Type.Builder b = new Type.Builder(mRS, Element.RGBA_8888(mRS));
111
112        int usage = Allocation.USAGE_SCRIPT | Allocation.USAGE_IO_OUTPUT;
113
114        int outX = 256;
115        int outY = 256;
116
117        // Wrap Linear
118        Allocation outAlloc = Allocation.createTyped(mRS, b.setX(outX).setY(outY).create(), usage);
119        TextureViewUpdater updater = new TextureViewUpdater(outAlloc, Sampler.WRAP_LINEAR(mRS));
120        TextureView displayView = (TextureView) findViewById(R.id.display);
121        displayView.setSurfaceTextureListener(updater);
122
123        // Clamp Linear
124        outAlloc = Allocation.createTyped(mRS, b.setX(outX).setY(outY).create(), usage);
125        updater = new TextureViewUpdater(outAlloc, Sampler.CLAMP_LINEAR(mRS));
126        displayView = (TextureView) findViewById(R.id.display2);
127        displayView.setSurfaceTextureListener(updater);
128
129        // Wrap Nearest
130        outAlloc = Allocation.createTyped(mRS, b.setX(outX).setY(outY).create(), usage);
131        updater = new TextureViewUpdater(outAlloc, Sampler.WRAP_NEAREST(mRS));
132        displayView = (TextureView) findViewById(R.id.display3);
133        displayView.setSurfaceTextureListener(updater);
134
135        // Clamp Nearest
136        outAlloc = Allocation.createTyped(mRS, b.setX(outX).setY(outY).create(), usage);
137        updater = new TextureViewUpdater(outAlloc, Sampler.CLAMP_NEAREST(mRS));
138        displayView = (TextureView) findViewById(R.id.display4);
139        displayView.setSurfaceTextureListener(updater);
140
141        mScript = new ScriptC_sample(mRS);
142    }
143
144    private Bitmap loadBitmap(int resource) {
145        final BitmapFactory.Options options = new BitmapFactory.Options();
146        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
147        Bitmap b = BitmapFactory.decodeResource(getResources(), resource, options);
148        Bitmap b2 = Bitmap.createBitmap(b.getWidth(), b.getHeight(), b.getConfig());
149        Canvas c = new Canvas(b2);
150        c.drawBitmap(b, 0, 0, null);
151        b.recycle();
152        return b2;
153    }
154
155    private synchronized void filterAlloc(Allocation alloc, Sampler sampler) {
156        long t = java.lang.System.currentTimeMillis();
157        mScript.invoke_setSampleData(alloc, mTwoByTwoAlloc, sampler);
158        mScript.forEach_root(alloc);
159        alloc.ioSend();
160        mRS.finish();
161        t = java.lang.System.currentTimeMillis() - t;
162        Log.i(TAG, "Filter time is: " + t + " ms");
163    }
164
165    public void benchmark(View v) {
166        /*filterAlloc();
167        long t = java.lang.System.currentTimeMillis();
168        filterAlloc();
169        t = java.lang.System.currentTimeMillis() - t;
170        mDisplayView.invalidate();
171        mBenchmarkResult.setText("Result: " + t + " ms");*/
172    }
173}
174