diff options
author | Alex Sakhartchouk <alexst@google.com> | 2012-03-05 18:03:32 -0800 |
---|---|---|
committer | Alex Sakhartchouk <alexst@google.com> | 2012-03-05 18:03:32 -0800 |
commit | 43ca3cfa8760ba945bb3dce3d7c1a23428b751d9 (patch) | |
tree | b823afa0e5673d778623c8499f005f65ed4ed502 /tests/RenderScriptTests | |
parent | c730aceb64233592c5a0d8b6a429d645443b3818 (diff) | |
download | frameworks_base-43ca3cfa8760ba945bb3dce3d7c1a23428b751d9.zip frameworks_base-43ca3cfa8760ba945bb3dce3d7c1a23428b751d9.tar.gz frameworks_base-43ca3cfa8760ba945bb3dce3d7c1a23428b751d9.tar.bz2 |
First draft of the sample function implementation.
Change-Id: I51bb999419b5b424a8549461a6d91f48f3fc9298
Diffstat (limited to 'tests/RenderScriptTests')
-rw-r--r-- | tests/RenderScriptTests/SampleTest/Android.mk | 26 | ||||
-rw-r--r-- | tests/RenderScriptTests/SampleTest/AndroidManifest.xml | 34 | ||||
-rw-r--r-- | tests/RenderScriptTests/SampleTest/res/drawable-nodpi/twobytwo.png | bin | 0 -> 2796 bytes | |||
-rw-r--r-- | tests/RenderScriptTests/SampleTest/res/layout/rs.xml | 52 | ||||
-rw-r--r-- | tests/RenderScriptTests/SampleTest/res/values/strings.xml | 24 | ||||
-rw-r--r-- | tests/RenderScriptTests/SampleTest/src/com/android/rs/sample/SampleRSActivity.java | 140 | ||||
-rw-r--r-- | tests/RenderScriptTests/SampleTest/src/com/android/rs/sample/sample.rs | 145 |
7 files changed, 421 insertions, 0 deletions
diff --git a/tests/RenderScriptTests/SampleTest/Android.mk b/tests/RenderScriptTests/SampleTest/Android.mk new file mode 100644 index 0000000..7d74c55 --- /dev/null +++ b/tests/RenderScriptTests/SampleTest/Android.mk @@ -0,0 +1,26 @@ +# +# Copyright (C) 2012 The Android Open Source Project +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +LOCAL_PATH := $(call my-dir) +include $(CLEAR_VARS) + +LOCAL_MODULE_TAGS := optional + +LOCAL_SRC_FILES := $(call all-java-files-under, src) $(call all-renderscript-files-under, src) + +LOCAL_PACKAGE_NAME := SampleRS + +include $(BUILD_PACKAGE) diff --git a/tests/RenderScriptTests/SampleTest/AndroidManifest.xml b/tests/RenderScriptTests/SampleTest/AndroidManifest.xml new file mode 100644 index 0000000..ec115f7 --- /dev/null +++ b/tests/RenderScriptTests/SampleTest/AndroidManifest.xml @@ -0,0 +1,34 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- +/* +* Copyright (C) 2012 The Android Open Source Project +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +--> + +<manifest xmlns:android="http://schemas.android.com/apk/res/android" + package="com.android.rs.sample"> + <uses-sdk android:minSdkVersion="14" /> + <application android:label="Sample Test" + android:hardwareAccelerated="true"> + + <activity android:name="SampleRSActivity" + android:label="Sample Test"> + <intent-filter> + <action android:name="android.intent.action.MAIN" /> + <category android:name="android.intent.category.LAUNCHER" /> + </intent-filter> + </activity> + </application> +</manifest> diff --git a/tests/RenderScriptTests/SampleTest/res/drawable-nodpi/twobytwo.png b/tests/RenderScriptTests/SampleTest/res/drawable-nodpi/twobytwo.png Binary files differnew file mode 100644 index 0000000..98cf963 --- /dev/null +++ b/tests/RenderScriptTests/SampleTest/res/drawable-nodpi/twobytwo.png diff --git a/tests/RenderScriptTests/SampleTest/res/layout/rs.xml b/tests/RenderScriptTests/SampleTest/res/layout/rs.xml new file mode 100644 index 0000000..61c339a --- /dev/null +++ b/tests/RenderScriptTests/SampleTest/res/layout/rs.xml @@ -0,0 +1,52 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- Copyright (C) 2012 The Android Open Source Project + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> + +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" + android:orientation="vertical" + android:layout_width="fill_parent" + android:layout_height="fill_parent" + android:id="@+id/toplevel"> + <ScrollView + android:layout_width="fill_parent" + android:layout_height="fill_parent"> + <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" + android:orientation="vertical" + android:layout_width="fill_parent" + android:layout_height="fill_parent"> + <TextureView + android:id="@+id/display" + android:layout_width="256sp" + android:layout_height="256sp" /> + <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" + android:orientation="horizontal" + android:layout_width="fill_parent" + android:layout_height="wrap_content"> + <Button + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:text="@string/benchmark" + android:onClick="benchmark"/> + <TextView + android:id="@+id/benchmarkText" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:textSize="8pt" + android:text="@string/benchmark"/> + </LinearLayout> + </LinearLayout> + </ScrollView> +</LinearLayout> + diff --git a/tests/RenderScriptTests/SampleTest/res/values/strings.xml b/tests/RenderScriptTests/SampleTest/res/values/strings.xml new file mode 100644 index 0000000..a92eac0 --- /dev/null +++ b/tests/RenderScriptTests/SampleTest/res/values/strings.xml @@ -0,0 +1,24 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- +/* +* Copyright (C) 2012 The Android Open Source Project +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +--> + +<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"> + <!-- General --> + <skip /> + <string name="benchmark">Benchmark</string> +</resources> diff --git a/tests/RenderScriptTests/SampleTest/src/com/android/rs/sample/SampleRSActivity.java b/tests/RenderScriptTests/SampleTest/src/com/android/rs/sample/SampleRSActivity.java new file mode 100644 index 0000000..91e3c4c --- /dev/null +++ b/tests/RenderScriptTests/SampleTest/src/com/android/rs/sample/SampleRSActivity.java @@ -0,0 +1,140 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.rs.sample; + +import android.app.Activity; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.graphics.Canvas; +import android.graphics.SurfaceTexture; +import android.os.Bundle; +import android.renderscript.Allocation; +import android.renderscript.Element; +import android.renderscript.Matrix3f; +import android.renderscript.RenderScript; +import android.renderscript.Sampler; +import android.renderscript.Type; +import android.renderscript.Type.Builder; +import android.util.Log; +import android.view.TextureView; +import android.view.View; +import android.widget.ImageView; +import android.widget.SeekBar; +import android.widget.TextView; + +public class SampleRSActivity extends Activity + implements TextureView.SurfaceTextureListener +{ + private final String TAG = "Img"; + private Bitmap mBitmapIn; + private TextureView mDisplayView; + + private TextView mBenchmarkResult; + + private RenderScript mRS; + private Allocation mInPixelsAllocation; + private Allocation mOutPixelsAllocation; + private ScriptC_sample mScript; + + public void onStartTrackingTouch(SeekBar seekBar) { + } + + public void onStopTrackingTouch(SeekBar seekBar) { + } + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.rs); + + mBitmapIn = loadBitmap(R.drawable.twobytwo); + mDisplayView = (TextureView) findViewById(R.id.display); + + mBenchmarkResult = (TextView) findViewById(R.id.benchmarkText); + mBenchmarkResult.setText("Result: not run"); + + mRS = RenderScript.create(this); + mInPixelsAllocation = Allocation.createFromBitmap(mRS, mBitmapIn, + Allocation.MipmapControl.MIPMAP_NONE, + Allocation.USAGE_SCRIPT); + + Type.Builder b = new Type.Builder(mRS, Element.RGBA_8888(mRS)); + + mOutPixelsAllocation = Allocation.createTyped(mRS, b.setX(32).setY(32).create(), + Allocation.USAGE_SCRIPT | + Allocation.USAGE_IO_OUTPUT); + mDisplayView.setSurfaceTextureListener(this); + + mScript = new ScriptC_sample(mRS, getResources(), R.raw.sample); + + mScript.set_sourceAlloc(mInPixelsAllocation); + mScript.set_destAlloc(mOutPixelsAllocation); + mScript.set_wrapUV(Sampler.WRAP_LINEAR(mRS)); + mScript.set_clampUV(Sampler.CLAMP_LINEAR(mRS)); + } + + private Bitmap loadBitmap(int resource) { + final BitmapFactory.Options options = new BitmapFactory.Options(); + options.inPreferredConfig = Bitmap.Config.ARGB_8888; + Bitmap b = BitmapFactory.decodeResource(getResources(), resource, options); + Bitmap b2 = Bitmap.createBitmap(b.getWidth(), b.getHeight(), b.getConfig()); + Canvas c = new Canvas(b2); + c.drawBitmap(b, 0, 0, null); + b.recycle(); + return b2; + } + + private void filter() { + long t = java.lang.System.currentTimeMillis(); + mScript.forEach_root(mOutPixelsAllocation); + mOutPixelsAllocation.ioSendOutput(); + mRS.finish(); + t = java.lang.System.currentTimeMillis() - t; + Log.i(TAG, "Filter time is: " + t + " ms"); + } + + public void benchmark(View v) { + filter(); + long t = java.lang.System.currentTimeMillis(); + filter(); + t = java.lang.System.currentTimeMillis() - t; + mDisplayView.invalidate(); + mBenchmarkResult.setText("Result: " + t + " ms"); + } + + + @Override + public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) { + mOutPixelsAllocation.setSurfaceTexture(surface); + filter(); + } + + @Override + public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) { + mOutPixelsAllocation.setSurfaceTexture(surface); + } + + @Override + public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) { + mOutPixelsAllocation.setSurfaceTexture(null); + return true; + } + + @Override + public void onSurfaceTextureUpdated(SurfaceTexture surface) { + } +} diff --git a/tests/RenderScriptTests/SampleTest/src/com/android/rs/sample/sample.rs b/tests/RenderScriptTests/SampleTest/src/com/android/rs/sample/sample.rs new file mode 100644 index 0000000..8f09be0 --- /dev/null +++ b/tests/RenderScriptTests/SampleTest/src/com/android/rs/sample/sample.rs @@ -0,0 +1,145 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma version(1) +#pragma rs java_package_name(com.android.rs.sample) +#include "rs_graphics.rsh" + +rs_sampler wrapUV; +rs_sampler clampUV; +rs_allocation sourceAlloc; +rs_allocation destAlloc; + +static uint32_t wrapI(rs_sampler_value wrap, uint32_t coord, uint32_t size) { + if (wrap == RS_SAMPLER_WRAP) { + return coord % (size + 1); + } + return min(coord, size); +} + +static float2 wrap(rs_sampler_value wrapS, rs_sampler_value wrapT, float2 coord) { + float2 wrappedCoord; + float temp; + if (wrapS == RS_SAMPLER_WRAP) { + wrappedCoord.x = fract(coord.x, &temp); + // Make sure that non zero integer uv's map to one + if (wrappedCoord.x == 0.0f && coord.x != 0.0f) { + wrappedCoord.x = 1.0f; + } + } else { + wrappedCoord.x = min(coord.x, 1.0f); + } + + if (wrapT == RS_SAMPLER_WRAP) { + wrappedCoord.y = fract(coord.y, &temp); + // Make sure that non zero integer uv's map to one + if (wrappedCoord.y == 0.0f && coord.y != 0.0f) { + wrappedCoord.y = 1.0f; + } + } else { + wrappedCoord.y = min(coord.y, 1.0f); + } + return wrappedCoord; +} + +// Naive implementation of texture filtering for prototyping purposes +static float4 sample(rs_allocation a, rs_sampler s, float2 uv) { + rs_sampler_value wrapS = rsgSamplerGetWrapS(s); + rs_sampler_value wrapT = rsgSamplerGetWrapT(s); + + rs_sampler_value sampleMin = rsgSamplerGetMinification(s); + rs_sampler_value sampleMag = rsgSamplerGetMagnification(s); + + uv = wrap(wrapS, wrapT, uv); + + uint32_t sourceW = rsAllocationGetDimX(a) - 1; + uint32_t sourceH = rsAllocationGetDimY(a) - 1; + + float2 dimF; + dimF.x = (float)(sourceW); + dimF.y = (float)(sourceH); + float2 pixelUV = uv * dimF; + uint2 iPixel = convert_uint2(pixelUV); + + if (sampleMin == RS_SAMPLER_NEAREST || + sampleMag == RS_SAMPLER_NEAREST) { + uchar4 *nearestSample = (uchar4*)rsGetElementAt(a, iPixel.x, iPixel.y); + return convert_float4(*nearestSample); + } + + float2 frac = pixelUV - convert_float2(iPixel); + float2 oneMinusFrac = 1.0f - frac; + + uint32_t nextX = wrapI(wrapS, iPixel.x + 1, sourceW); + uint32_t nextY = wrapI(wrapT, iPixel.y + 1, sourceH); + + uchar4 *p0c = (uchar4*)rsGetElementAt(a, iPixel.x, iPixel.y); + uchar4 *p1c = (uchar4*)rsGetElementAt(a, nextX, iPixel.y); + uchar4 *p2c = (uchar4*)rsGetElementAt(a, iPixel.x, nextY); + uchar4 *p3c = (uchar4*)rsGetElementAt(a, nextX, nextY); + + float4 p0 = convert_float4(*p0c); + float4 p1 = convert_float4(*p1c); + float4 p2 = convert_float4(*p2c); + float4 p3 = convert_float4(*p3c); + + float4 weights; + weights.x = oneMinusFrac.x * oneMinusFrac.y; + weights.y = frac.x * oneMinusFrac.y; + weights.z = oneMinusFrac.x * frac.y; + weights.w = frac.x * frac.y; + + float4 result = p0 * weights.x + p1 * weights.y + p2 * weights.z + p3 * weights.w; + + /*rsDebug("*****************************************", 0); + rsDebug("u", uv.x); + rsDebug("v", uv.y); + rsDebug("sourceW", sourceW); + rsDebug("sourceH", sourceH); + rsDebug("iPixelX", iPixel.x); + rsDebug("iPixelY", iPixel.y); + rsDebug("fiPixel", (float2)iPixel); + rsDebug("whole", wholeUV); + rsDebug("pixelUV", pixelUV); + rsDebug("frac", frac); + rsDebug("oneMinusFrac", oneMinusFrac); + rsDebug("p0", p0); + rsDebug("p1", p1); + rsDebug("p2", p2); + rsDebug("p3", p3); + rsDebug("w", weights); + rsDebug("result", result);*/ + + return result; +} + +void root(uchar4 *out, uint32_t x, uint32_t y) { + + float destX = (float)rsAllocationGetDimX(destAlloc) - 1.0f; + float destY = (float)rsAllocationGetDimY(destAlloc) - 1.0f; + + /*rsDebug("*****************************************", 0); + rsDebug("x", x); + rsDebug("y", y);*/ + + float2 uv; + uv.x = (float)x / destX; + uv.y = (float)y / destY; + + out->xyz = convert_uchar3(sample(sourceAlloc, wrapUV, uv*2.0f).xyz); + out->w = 0xff; +} + |