Skip to content

Commit 43ca3cf

Browse files
author
Alex Sakhartchouk
committed
First draft of the sample function implementation.
Change-Id: I51bb999419b5b424a8549461a6d91f48f3fc9298
1 parent c730ace commit 43ca3cf

File tree

7 files changed

+421
-0
lines changed

7 files changed

+421
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
LOCAL_PATH := $(call my-dir)
18+
include $(CLEAR_VARS)
19+
20+
LOCAL_MODULE_TAGS := optional
21+
22+
LOCAL_SRC_FILES := $(call all-java-files-under, src) $(call all-renderscript-files-under, src)
23+
24+
LOCAL_PACKAGE_NAME := SampleRS
25+
26+
include $(BUILD_PACKAGE)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
/*
4+
* Copyright (C) 2012 The Android Open Source Project
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
-->
19+
20+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
21+
package="com.android.rs.sample">
22+
<uses-sdk android:minSdkVersion="14" />
23+
<application android:label="Sample Test"
24+
android:hardwareAccelerated="true">
25+
26+
<activity android:name="SampleRSActivity"
27+
android:label="Sample Test">
28+
<intent-filter>
29+
<action android:name="android.intent.action.MAIN" />
30+
<category android:name="android.intent.category.LAUNCHER" />
31+
</intent-filter>
32+
</activity>
33+
</application>
34+
</manifest>
2.73 KB
Loading
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?xml version="1.0" encoding="utf-8"?>
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+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
18+
android:orientation="vertical"
19+
android:layout_width="fill_parent"
20+
android:layout_height="fill_parent"
21+
android:id="@+id/toplevel">
22+
<ScrollView
23+
android:layout_width="fill_parent"
24+
android:layout_height="fill_parent">
25+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
26+
android:orientation="vertical"
27+
android:layout_width="fill_parent"
28+
android:layout_height="fill_parent">
29+
<TextureView
30+
android:id="@+id/display"
31+
android:layout_width="256sp"
32+
android:layout_height="256sp" />
33+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
34+
android:orientation="horizontal"
35+
android:layout_width="fill_parent"
36+
android:layout_height="wrap_content">
37+
<Button
38+
android:layout_width="wrap_content"
39+
android:layout_height="wrap_content"
40+
android:text="@string/benchmark"
41+
android:onClick="benchmark"/>
42+
<TextView
43+
android:id="@+id/benchmarkText"
44+
android:layout_width="match_parent"
45+
android:layout_height="wrap_content"
46+
android:textSize="8pt"
47+
android:text="@string/benchmark"/>
48+
</LinearLayout>
49+
</LinearLayout>
50+
</ScrollView>
51+
</LinearLayout>
52+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
/*
4+
* Copyright (C) 2012 The Android Open Source Project
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
-->
19+
20+
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
21+
<!-- General -->
22+
<skip />
23+
<string name="benchmark">Benchmark</string>
24+
</resources>
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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

Comments
 (0)