cleanup, remove unused fields. Also make sure that we don't systematically allocate a Surface in Surface.java if only a SurfaceControl is needed (Common case).
diff --git a/core/jni/android_view_Surface.cpp b/core/jni/android_view_Surface.cpp
index c2eb5e8..823fafd 100644
--- a/core/jni/android_view_Surface.cpp
+++ b/core/jni/android_view_Surface.cpp
@@ -145,8 +145,27 @@
 
 static sp<Surface> getSurface(JNIEnv* env, jobject clazz)
 {
-    Surface* const p = (Surface*)env->GetIntField(clazz, so.surface);
-    return sp<Surface>(p);
+    sp<Surface> result((Surface*)env->GetIntField(clazz, so.surface));
+    if (result == 0) {
+        /*
+         * if this method is called from the WindowManager's process, it means
+         * the client is is not remote, and therefore is allowed to have
+         * a Surface (data), so we create it here. 
+         * If we don't have a SurfaceControl, it means we're in a different
+         * process.
+         */
+        
+        SurfaceControl* const control = 
+            (SurfaceControl*)env->GetIntField(clazz, so.surfaceControl);
+        if (control) {
+            result = control->getSurface();
+            if (result != 0) {
+                result->incStrong(clazz);
+                env->SetIntField(clazz, so.surface, (int)result.get());
+            }
+        }
+    }
+    return result;
 }
 
 static void setSurface(JNIEnv* env, jobject clazz, const sp<Surface>& surface)
@@ -510,7 +529,6 @@
      * This is used by the WindowManagerService just after constructing
      * a Surface and is necessary for returning the Surface reference to
      * the caller. At this point, we should only have a SurfaceControl.
-     * 
      */
     
     const sp<SurfaceControl>& surface = getSurfaceControl(env, clazz);
@@ -519,7 +537,6 @@
         // we reassign the surface only if it's a different one
         // otherwise we would loose our client-side state.
         setSurfaceControl(env, clazz, rhs);
-        setSurface(env, clazz, rhs->getSurface());
     }
 }
 
diff --git a/include/ui/Surface.h b/include/ui/Surface.h
index d1ead1d..16d2edb 100644
--- a/include/ui/Surface.h
+++ b/include/ui/Surface.h
@@ -138,8 +138,7 @@
             const sp<SurfaceComposerClient>& client,
             const sp<ISurface>& surface,
             const ISurfaceFlingerClient::surface_data_t& data,
-            uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
-            bool owner = true);
+            uint32_t w, uint32_t h, PixelFormat format, uint32_t flags);
 
     ~SurfaceControl();
 
@@ -152,7 +151,6 @@
     uint32_t                    mIdentity;
     PixelFormat                 mFormat;
     uint32_t                    mFlags;
-    const bool                  mOwner;
     mutable Mutex               mLock;
     
     mutable sp<Surface>         mSurfaceData;
@@ -252,7 +250,6 @@
     uint32_t                    mIdentity;
     PixelFormat                 mFormat;
     uint32_t                    mFlags;
-    const bool                  mOwner;
     mutable Region              mDirtyRegion;
     mutable Rect                mSwapRectangle;
     mutable uint8_t             mBackbufferIndex;
diff --git a/libs/ui/Surface.cpp b/libs/ui/Surface.cpp
index cf1b76c..47880f5 100644
--- a/libs/ui/Surface.cpp
+++ b/libs/ui/Surface.cpp
@@ -152,13 +152,13 @@
         const sp<SurfaceComposerClient>& client, 
         const sp<ISurface>& surface,
         const ISurfaceFlingerClient::surface_data_t& data,
-        uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
-        bool owner)
+        uint32_t w, uint32_t h, PixelFormat format, uint32_t flags)
     : mClient(client), mSurface(surface),
       mToken(data.token), mIdentity(data.identity),
-      mFormat(format), mFlags(flags), mOwner(owner)
+      mFormat(format), mFlags(flags)
 {
 }
+        
 SurfaceControl::~SurfaceControl()
 {
     destroy();
@@ -166,10 +166,7 @@
 
 void SurfaceControl::destroy()
 {
-    // Destroy the surface in SurfaceFlinger if we were the owner
-    // (in any case, a client won't be able to, because it won't have the
-    // right permission).
-    if (mOwner && mToken>=0 && mClient!=0) {
+    if (isValid()) {
         mClient->destroySurface(mToken);
     }
 
@@ -351,14 +348,12 @@
 Surface::Surface(const sp<SurfaceControl>& surface)
     : mClient(surface->mClient), mSurface(surface->mSurface),
       mToken(surface->mToken), mIdentity(surface->mIdentity),
-      mFormat(surface->mFormat), mFlags(surface->mFlags),
-      mOwner(surface->mOwner)
+      mFormat(surface->mFormat), mFlags(surface->mFlags)
 {
     init();
 }
 
 Surface::Surface(const Parcel& parcel)
-    : mOwner(false)
 {
     sp<IBinder> clientBinder = parcel.readStrongBinder();
     mSurface    = interface_cast<ISurface>(parcel.readStrongBinder());