Merge "add HelloComputeNDK Java sample" into klp-dev
diff --git a/java/tests/HelloComputeNDK/Android.mk b/java/tests/HelloComputeNDK/Android.mk
new file mode 100644
index 0000000..5dbe19f
--- /dev/null
+++ b/java/tests/HelloComputeNDK/Android.mk
@@ -0,0 +1,31 @@
+#
+# Copyright (C) 2013 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 := tests
+
+LOCAL_SRC_FILES := $(call all-java-files-under, src) \
+                   $(call all-renderscript-files-under, src)
+
+LOCAL_PACKAGE_NAME := HelloComputeNDK
+LOCAL_SDK_VERSION := 14
+
+LOCAL_JNI_SHARED_LIBRARIES := libhellocomputendk
+
+include $(BUILD_PACKAGE)
+include $(LOCAL_PATH)/libhellocomputendk/Android.mk
\ No newline at end of file
diff --git a/java/tests/HelloComputeNDK/AndroidManifest.xml b/java/tests/HelloComputeNDK/AndroidManifest.xml
new file mode 100644
index 0000000..4db45e7
--- /dev/null
+++ b/java/tests/HelloComputeNDK/AndroidManifest.xml
@@ -0,0 +1,30 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2013 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.example.android.rs.hellocomputendk">
+
+    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
+    <uses-sdk android:minSdkVersion="14" />
+    <application android:label="HelloComputeNDK">
+        <activity android:name="HelloComputeNDK">
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="android.intent.category.LAUNCHER" />
+            </intent-filter>
+        </activity>
+    </application>
+</manifest>
diff --git a/java/tests/HelloComputeNDK/libhellocomputendk/Android.mk b/java/tests/HelloComputeNDK/libhellocomputendk/Android.mk
new file mode 100644
index 0000000..1c0e861
--- /dev/null
+++ b/java/tests/HelloComputeNDK/libhellocomputendk/Android.mk
@@ -0,0 +1,33 @@
+# Copyright (C) 2013 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.
+
+#
+# This is the shared library included by the JNI test app.
+#
+LOCAL_PATH := $(call my-dir)
+include $(CLEAR_VARS)
+LOCAL_CLANG := true
+
+LOCAL_MODULE := libhellocomputendk
+LOCAL_MODULE_TAGS := optional
+LOCAL_SRC_FILES := helloComputeNDK.cpp mono.rs
+
+LOCAL_C_INCLUDES := $(JNI_H_INCLUDE)
+LOCAL_C_INCLUDES += frameworks/rs/cpp
+LOCAL_C_INCLUDES += frameworks/rs
+LOCAL_C_INCLUDES += external/stlport/stlport bionic/ bionic/libstdc++/include
+
+LOCAL_SHARED_LIBRARIES := libdl liblog libjnigraphics
+LOCAL_STATIC_LIBRARIES := libRScpp_static libstlport_static libcutils
+include $(BUILD_SHARED_LIBRARY)
diff --git a/java/tests/HelloComputeNDK/libhellocomputendk/helloComputeNDK.cpp b/java/tests/HelloComputeNDK/libhellocomputendk/helloComputeNDK.cpp
new file mode 100644
index 0000000..6ed5589
--- /dev/null
+++ b/java/tests/HelloComputeNDK/libhellocomputendk/helloComputeNDK.cpp
@@ -0,0 +1,59 @@
+#include <jni.h>
+#include <android/log.h>
+#include <android/bitmap.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <math.h>
+
+#include <RenderScript.h>
+
+#include "ScriptC_mono.h"
+
+#define  LOG_TAG    "HelloComputeNDK"
+#define  LOGI(...)  __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
+#define  LOGE(...)  __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
+
+using namespace android::RSC;
+
+extern "C" JNIEXPORT void JNICALL
+Java_com_example_android_rs_hellocomputendk_HelloComputeNDK_nativeMono(JNIEnv * env,
+                                                                       jclass,
+                                                                       jint X,
+                                                                       jint Y,
+                                                                       jobject jbitmapIn,
+                                                                       jobject jbitmapOut
+                                                                       )
+{
+
+    void* inputPtr = NULL;
+    void* outputPtr = NULL;
+
+    AndroidBitmap_lockPixels(env, jbitmapIn, &inputPtr);
+    AndroidBitmap_lockPixels(env, jbitmapOut, &outputPtr);
+
+    sp<RS> rs = new RS();
+    rs->init();
+
+    sp<const Element> e = Element::RGBA_8888(rs);
+
+    sp<const Type> t = Type::create(rs, e, X, Y, 0);
+
+    sp<Allocation> inputAlloc = Allocation::createTyped(rs, t, RS_ALLOCATION_MIPMAP_NONE,
+                                                        RS_ALLOCATION_USAGE_SHARED | RS_ALLOCATION_USAGE_SCRIPT,
+                                                        inputPtr);
+    sp<Allocation> outputAlloc = Allocation::createTyped(rs, t, RS_ALLOCATION_MIPMAP_NONE,
+                                                         RS_ALLOCATION_USAGE_SHARED | RS_ALLOCATION_USAGE_SCRIPT,
+                                                         outputPtr);
+
+
+    inputAlloc->copy2DRangeFrom(0, 0, X, Y, inputPtr);
+    ScriptC_mono* sc = new ScriptC_mono(rs);
+    sc->forEach_root(inputAlloc, outputAlloc);
+    outputAlloc->copy2DRangeTo(0, 0, X, Y, outputPtr);
+
+
+    AndroidBitmap_unlockPixels(env, jbitmapIn);
+    AndroidBitmap_unlockPixels(env, jbitmapOut);
+
+}
diff --git a/java/tests/HelloComputeNDK/libhellocomputendk/mono.rs b/java/tests/HelloComputeNDK/libhellocomputendk/mono.rs
new file mode 100644
index 0000000..efa5c72
--- /dev/null
+++ b/java/tests/HelloComputeNDK/libhellocomputendk/mono.rs
@@ -0,0 +1,28 @@
+/*
+ * Copyright (C) 2013 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.example.android.rs.hellocomputendk)
+
+const static float3 gMonoMult = {0.299f, 0.587f, 0.114f};
+
+void root(const uchar4 *v_in, uchar4 *v_out) {
+    float4 f4 = rsUnpackColor8888(*v_in);
+
+    float3 mono = dot(f4.rgb, gMonoMult);
+    *v_out = rsPackColorTo8888(mono);
+}
+
diff --git a/java/tests/HelloComputeNDK/res/drawable/data.jpg b/java/tests/HelloComputeNDK/res/drawable/data.jpg
new file mode 100644
index 0000000..81a87b1
--- /dev/null
+++ b/java/tests/HelloComputeNDK/res/drawable/data.jpg
Binary files differ
diff --git a/java/tests/HelloComputeNDK/res/layout/main.xml b/java/tests/HelloComputeNDK/res/layout/main.xml
new file mode 100644
index 0000000..7b2c76a
--- /dev/null
+++ b/java/tests/HelloComputeNDK/res/layout/main.xml
@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2013 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:layout_width="match_parent"
+        android:layout_height="match_parent">
+
+    <ImageView
+        android:id="@+id/displayin"
+        android:layout_width="320dip"
+        android:layout_height="266dip" />
+
+    <ImageView
+        android:id="@+id/displayout"
+        android:layout_width="320dip"
+        android:layout_height="266dip" />
+
+</LinearLayout>
diff --git a/java/tests/HelloComputeNDK/src/com/example/android/rs/hellocomputendk/HelloComputeNDK.java b/java/tests/HelloComputeNDK/src/com/example/android/rs/hellocomputendk/HelloComputeNDK.java
new file mode 100644
index 0000000..aec6497
--- /dev/null
+++ b/java/tests/HelloComputeNDK/src/com/example/android/rs/hellocomputendk/HelloComputeNDK.java
@@ -0,0 +1,59 @@
+/*
+ * Copyright (C) 2013 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.example.android.rs.hellocomputendk;
+
+import android.app.Activity;
+import android.os.Bundle;
+import android.graphics.BitmapFactory;
+import android.graphics.Bitmap;
+import android.widget.ImageView;
+
+public class HelloComputeNDK extends Activity {
+    private Bitmap mBitmapIn;
+    private Bitmap mBitmapOut;
+
+    static {
+        System.loadLibrary("hellocomputendk");
+    }
+
+    native void nativeMono(int X, int Y, Bitmap in, Bitmap out);
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        setContentView(R.layout.main);
+
+        mBitmapIn = loadBitmap(R.drawable.data);
+        mBitmapOut = Bitmap.createBitmap(mBitmapIn.getWidth(), mBitmapIn.getHeight(),
+                                         mBitmapIn.getConfig());
+
+        ImageView in = (ImageView) findViewById(R.id.displayin);
+        in.setImageBitmap(mBitmapIn);
+
+        ImageView out = (ImageView) findViewById(R.id.displayout);
+        out.setImageBitmap(mBitmapOut);
+
+        nativeMono(mBitmapIn.getWidth(), mBitmapIn.getHeight(), mBitmapIn, mBitmapOut);
+
+    }
+
+    private Bitmap loadBitmap(int resource) {
+        final BitmapFactory.Options options = new BitmapFactory.Options();
+        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
+        return BitmapFactory.decodeResource(getResources(), resource, options);
+    }
+}