Merge "Add method to create a ParcelSurfaceTexture from android.view.Surface."
diff --git a/core/jni/android/graphics/ParcelSurfaceTexture.cpp b/core/jni/android/graphics/ParcelSurfaceTexture.cpp
index 40966e1..754485f 100644
--- a/core/jni/android/graphics/ParcelSurfaceTexture.cpp
+++ b/core/jni/android/graphics/ParcelSurfaceTexture.cpp
@@ -17,9 +17,11 @@
 #define LOG_TAG "ParcelSurfaceTexture"
 
 #include <gui/SurfaceTextureClient.h>
+#include <surfaceflinger/Surface.h>
 
 #include <android_runtime/AndroidRuntime.h>
 #include <android_runtime/android_graphics_SurfaceTexture.h>
+#include <android_runtime/android_view_Surface.h>
 
 #include <utils/Log.h>
 
@@ -96,7 +98,16 @@
     }
 }
 
-static void ParcelSurfaceTexture_init(JNIEnv* env, jobject thiz, jobject jSurfaceTexture)
+static void ParcelSurfaceTexture_initFromSurface(
+        JNIEnv* env, jobject thiz, jobject jSurface)
+{
+    sp<Surface> surface(Surface_getSurface(env, jSurface));
+    sp<ISurfaceTexture> iSurfaceTexture(surface->getSurfaceTexture());
+    ParcelSurfaceTexture_setISurfaceTexture(env, thiz, iSurfaceTexture);
+}
+
+static void ParcelSurfaceTexture_initFromSurfaceTexture(
+        JNIEnv* env, jobject thiz, jobject jSurfaceTexture)
 {
     sp<ISurfaceTexture> iSurfaceTexture(
             SurfaceTexture_getSurfaceTexture(env, jSurfaceTexture));
@@ -131,8 +142,10 @@
 
 static JNINativeMethod gParcelSurfaceTextureMethods[] = {
     {"nativeClassInit", "()V",   (void*)ParcelSurfaceTexture_classInit },
-    {"nativeInit", "(Landroid/graphics/SurfaceTexture;)V",
-      (void *)ParcelSurfaceTexture_init },
+    {"nativeInitFromSurface", "(Landroid/view/Surface;)V",
+      (void *)ParcelSurfaceTexture_initFromSurface },
+    {"nativeInitFromSurfaceTexture", "(Landroid/graphics/SurfaceTexture;)V",
+      (void *)ParcelSurfaceTexture_initFromSurfaceTexture },
     { "nativeFinalize", "()V", (void *)ParcelSurfaceTexture_finalize },
     { "nativeWriteToParcel", "(Landroid/os/Parcel;I)V",
       (void *)ParcelSurfaceTexture_writeToParcel },
diff --git a/core/jni/android_view_Surface.cpp b/core/jni/android_view_Surface.cpp
index 70c2f7b..0dc9293 100644
--- a/core/jni/android_view_Surface.cpp
+++ b/core/jni/android_view_Surface.cpp
@@ -156,7 +156,7 @@
 
 static sp<Surface> getSurface(JNIEnv* env, jobject clazz)
 {
-    sp<Surface> result((Surface*)env->GetIntField(clazz, so.surface));
+    sp<Surface> result(Surface_getSurface(env, clazz));
     if (result == 0) {
         /*
          * if this method is called from the WindowManager's process, it means
@@ -189,6 +189,11 @@
     return env->IsInstanceOf(obj, surfaceClass);
 }
 
+sp<Surface> Surface_getSurface(JNIEnv* env, jobject clazz) {
+    sp<Surface> surface((Surface*)env->GetIntField(clazz, so.surface));
+    return surface;
+}
+
 static void setSurface(JNIEnv* env, jobject clazz, const sp<Surface>& surface)
 {
     Surface* const p = (Surface*)env->GetIntField(clazz, so.surface);
diff --git a/graphics/java/android/graphics/ParcelSurfaceTexture.java b/graphics/java/android/graphics/ParcelSurfaceTexture.java
index 5272cc6..cc8bd02 100644
--- a/graphics/java/android/graphics/ParcelSurfaceTexture.java
+++ b/graphics/java/android/graphics/ParcelSurfaceTexture.java
@@ -19,6 +19,7 @@
 import android.graphics.SurfaceTexture;
 import android.os.Parcel;
 import android.os.Parcelable;
+import android.view.Surface;
 
 /**
  *
@@ -34,6 +35,17 @@
     private int mISurfaceTexture;
 
     /**
+     * Create a new ParcelSurfaceTexture from a Surface
+     *
+     * @param surface The Surface to create a ParcelSurfaceTexture from.
+     *
+     * @return Returns a new ParcelSurfaceTexture for the given Surface.
+     */
+    public static ParcelSurfaceTexture fromSurface(Surface surface) {
+        return new ParcelSurfaceTexture(surface);
+    }
+
+    /**
      * Create a new ParcelSurfaceTexture from a SurfaceTexture
      *
      * @param surfaceTexture The SurfaceTexture to transport.
@@ -75,8 +87,11 @@
     private ParcelSurfaceTexture(Parcel in) {
         nativeReadFromParcel(in);
     }
+    private ParcelSurfaceTexture(Surface surface) {
+        nativeInitFromSurface(surface);
+    }
     private ParcelSurfaceTexture(SurfaceTexture surfaceTexture) {
-        nativeInit(surfaceTexture);
+        nativeInitFromSurfaceTexture(surfaceTexture);
     }
 
     @Override
@@ -88,7 +103,8 @@
         }
     }
 
-    private native void nativeInit(SurfaceTexture surfaceTexture);
+    private native void nativeInitFromSurface(Surface surface);
+    private native void nativeInitFromSurfaceTexture(SurfaceTexture surfaceTexture);
     private native void nativeFinalize();
     private native void nativeWriteToParcel(Parcel dest, int flags);
     private native void nativeReadFromParcel(Parcel in);
diff --git a/include/android_runtime/android_view_Surface.h b/include/android_runtime/android_view_Surface.h
index 317f1e7..fb0b057 100644
--- a/include/android_runtime/android_view_Surface.h
+++ b/include/android_runtime/android_view_Surface.h
@@ -23,10 +23,15 @@
 
 namespace android {
 
+class Surface;
+
 extern sp<ANativeWindow> android_Surface_getNativeWindow(
         JNIEnv* env, jobject clazz);
 extern bool android_Surface_isInstanceOf(JNIEnv* env, jobject obj);
 
+/* Gets the underlying Surface from a Surface Java object. */
+extern sp<Surface> Surface_getSurface(JNIEnv* env, jobject thiz);
+
 } // namespace android
 
 #endif // _ANDROID_VIEW_SURFACE_H
diff --git a/include/surfaceflinger/Surface.h b/include/surfaceflinger/Surface.h
index 8845dc9..dc2a845 100644
--- a/include/surfaceflinger/Surface.h
+++ b/include/surfaceflinger/Surface.h
@@ -40,6 +40,7 @@
 class GraphicBuffer;
 class GraphicBufferMapper;
 class IOMX;
+class ISurfaceTexture;
 class Rect;
 class Surface;
 class SurfaceComposerClient;
@@ -154,6 +155,7 @@
     bool        isValid();
     uint32_t    getFlags() const    { return mFlags; }
     uint32_t    getIdentity() const { return mIdentity; }
+    sp<ISurfaceTexture> getSurfaceTexture();
 
     // the lock/unlock APIs must be used from the same thread
     status_t    lock(SurfaceInfo* info, bool blocking = true);
diff --git a/libs/gui/Surface.cpp b/libs/gui/Surface.cpp
index 4d1d923..9185e1e 100644
--- a/libs/gui/Surface.cpp
+++ b/libs/gui/Surface.cpp
@@ -421,6 +421,10 @@
     return NO_ERROR;
 }
 
+sp<ISurfaceTexture> Surface::getSurfaceTexture() {
+    return mSurface != NULL ? mSurface->getSurfaceTexture() : NULL;
+}
+
 sp<IBinder> Surface::asBinder() const {
     return mSurface!=0 ? mSurface->asBinder() : 0;
 }