Use new surface flinger API.

Change-Id: Ic888577408a59a36481a48010e19c5e77c24e211
diff --git a/cmds/bootanimation/BootAnimation.cpp b/cmds/bootanimation/BootAnimation.cpp
index 2471a2e..8511735 100644
--- a/cmds/bootanimation/BootAnimation.cpp
+++ b/cmds/bootanimation/BootAnimation.cpp
@@ -38,6 +38,7 @@
 #include <ui/DisplayInfo.h>
 #include <ui/FramebufferNativeWindow.h>
 
+#include <gui/ISurfaceComposer.h>
 #include <gui/Surface.h>
 #include <gui/SurfaceComposerClient.h>
 
@@ -216,14 +217,16 @@
 status_t BootAnimation::readyToRun() {
     mAssets.addDefaultAssets();
 
+    sp<IBinder> dtoken(SurfaceComposerClient::getBuiltInDisplay(
+            ISurfaceComposer::eDisplayIdMain));
     DisplayInfo dinfo;
-    status_t status = SurfaceComposerClient::getDisplayInfo(0, &dinfo);
+    status_t status = SurfaceComposerClient::getDisplayInfo(dtoken, &dinfo);
     if (status)
         return -1;
 
     // create the native surface
-    sp<SurfaceControl> control = session()->createSurface(
-            0, dinfo.w, dinfo.h, PIXEL_FORMAT_RGB_565);
+    sp<SurfaceControl> control = session()->createSurface(String8("BootAnimation"),
+            dinfo.w, dinfo.h, PIXEL_FORMAT_RGB_565);
 
     SurfaceComposerClient::openGlobalTransaction();
     control->setLayer(0x40000000);
diff --git a/cmds/screencap/screencap.cpp b/cmds/screencap/screencap.cpp
index 46e41e3..a1ea81a 100644
--- a/cmds/screencap/screencap.cpp
+++ b/cmds/screencap/screencap.cpp
@@ -25,6 +25,7 @@
 
 #include <binder/IMemory.h>
 #include <gui/SurfaceComposerClient.h>
+#include <gui/ISurfaceComposer.h>
 
 #include <SkImageEncoder.h>
 #include <SkBitmap.h>
@@ -33,15 +34,18 @@
 
 using namespace android;
 
+static uint32_t DEFAULT_DISPLAY_ID = ISurfaceComposer::eDisplayIdMain;
+
 static void usage(const char* pname)
 {
     fprintf(stderr,
-            "usage: %s [-hp] [FILENAME]\n"
+            "usage: %s [-hp] [-d display-id] [FILENAME]\n"
             "   -h: this message\n"
             "   -p: save the file as a png.\n"
+            "   -d: specify the display id to capture, default %d.\n"
             "If FILENAME ends with .png it will be saved as a png.\n"
             "If FILENAME is not given, the results will be printed to stdout.\n",
-            pname
+            pname, DEFAULT_DISPLAY_ID
     );
 }
 
@@ -87,12 +91,16 @@
 {
     const char* pname = argv[0];
     bool png = false;
+    int32_t displayId = DEFAULT_DISPLAY_ID;
     int c;
-    while ((c = getopt(argc, argv, "ph")) != -1) {
+    while ((c = getopt(argc, argv, "phd:")) != -1) {
         switch (c) {
             case 'p':
                 png = true;
                 break;
+            case 'd':
+                displayId = atoi(optarg);
+                break;
             case '?':
             case 'h':
                 usage(pname);
@@ -131,7 +139,8 @@
     size_t size = 0;
 
     ScreenshotClient screenshot;
-    if (screenshot.update() == NO_ERROR) {
+    sp<IBinder> display = SurfaceComposerClient::getBuiltInDisplay(displayId);
+    if (display != NULL && screenshot.update(display) == NO_ERROR) {
         base = screenshot.getPixels();
         w = screenshot.getWidth();
         h = screenshot.getHeight();
diff --git a/core/java/android/view/Surface.java b/core/java/android/view/Surface.java
index a6d1a3f..84b3d64 100644
--- a/core/java/android/view/Surface.java
+++ b/core/java/android/view/Surface.java
@@ -17,9 +17,15 @@
 package android.view;
 
 import android.content.res.CompatibilityInfo.Translator;
-import android.graphics.*;
+import android.graphics.Bitmap;
+import android.graphics.Canvas;
+import android.graphics.Matrix;
+import android.graphics.Rect;
+import android.graphics.Region;
+import android.graphics.SurfaceTexture;
 import android.os.Parcelable;
 import android.os.Parcel;
+import android.os.Process;
 import android.os.SystemProperties;
 import android.util.Log;
 
@@ -250,13 +256,20 @@
     public Surface(SurfaceSession s,
             int pid, String name, int layerStack, int w, int h, int format, int flags)
         throws OutOfResourcesException {
+        // FIXME: remove pid and layerstack arguments
         checkHeadless();
 
         if (DEBUG_RELEASE) {
             mCreationStack = new Exception();
         }
+
+        if (name == null) {
+            name = "<pid " + Process.myPid() + ">";
+        }
+
         mCanvas = new CompatibleCanvas();
-        init(s, pid, name, layerStack, w, h, format, flags);
+        init(s, name, w, h, format, flags);
+        setLayerStack(layerStack);
         mName = name;
     }
 
@@ -496,8 +509,8 @@
         }
     }
     
-    private native void init(SurfaceSession s,
-            int pid, String name, int layerStack, int w, int h, int format, int flags)
+    private native void init(SurfaceSession s, String name,
+            int w, int h, int format, int flags)
             throws OutOfResourcesException;
 
     private native void init(Parcel source) throws OutOfResourcesException;
diff --git a/core/java/android/view/SurfaceSession.java b/core/java/android/view/SurfaceSession.java
index 2a04675..2491abe 100644
--- a/core/java/android/view/SurfaceSession.java
+++ b/core/java/android/view/SurfaceSession.java
@@ -24,26 +24,34 @@
  * {@hide}
  */
 public class SurfaceSession {
+    private int mClient;
+
+    private native void nativeInit();
+    private native void nativeDestroy();
+    private native void nativeKill();
+
     /** Create a new connection with the surface flinger. */
     public SurfaceSession() {
-        init();
+        nativeInit();
     }
 
-    /** Forcibly detach native resources associated with this object.
-     *  Unlike destroy(), after this call any surfaces that were created
-     *  from the session will no longer work. The session itself is destroyed.
-     */
-    public native void kill();
-
     /* no user serviceable parts here ... */
     @Override
     protected void finalize() throws Throwable {
-        destroy();
+        try {
+            nativeDestroy();
+        } finally {
+            super.finalize();
+        }
     }
-    
-    private native void init();
-    private native void destroy();
-    
-    private int mClient;
+
+    /**
+     * Forcibly detach native resources associated with this object.
+     * Unlike destroy(), after this call any surfaces that were created
+     * from the session will no longer work. The session itself is destroyed.
+     */
+    public void kill() {
+        nativeKill();
+    }
 }
 
diff --git a/core/jni/android_view_Surface.cpp b/core/jni/android_view_Surface.cpp
index bada329..c302b23 100644
--- a/core/jni/android_view_Surface.cpp
+++ b/core/jni/android_view_Surface.cpp
@@ -23,6 +23,7 @@
 
 #include <binder/IMemory.h>
 
+#include <gui/ISurfaceComposer.h>
 #include <gui/Surface.h>
 #include <gui/SurfaceComposerClient.h>
 #include <gui/SurfaceTexture.h>
@@ -107,14 +108,14 @@
 // ----------------------------------------------------------------------------
 // ----------------------------------------------------------------------------
 
-static void SurfaceSession_init(JNIEnv* env, jobject clazz)
+static void SurfaceSession_nativeInit(JNIEnv* env, jobject clazz)
 {
     sp<SurfaceComposerClient> client = new SurfaceComposerClient;
     client->incStrong(clazz);
     env->SetIntField(clazz, sso.client, (int)client.get());
 }
 
-static void SurfaceSession_destroy(JNIEnv* env, jobject clazz)
+static void SurfaceSession_nativeDestroy(JNIEnv* env, jobject clazz)
 {
     SurfaceComposerClient* client =
             (SurfaceComposerClient*)env->GetIntField(clazz, sso.client);
@@ -124,7 +125,7 @@
     }
 }
 
-static void SurfaceSession_kill(JNIEnv* env, jobject clazz)
+static void SurfaceSession_nativeKill(JNIEnv* env, jobject clazz)
 {
     SurfaceComposerClient* client =
             (SurfaceComposerClient*)env->GetIntField(clazz, sso.client);
@@ -221,7 +222,7 @@
 static void Surface_init(
         JNIEnv* env, jobject clazz,
         jobject session,
-        jint, jstring jname, jint layerStack, jint w, jint h, jint format, jint flags)
+        jstring jname, jint w, jint h, jint format, jint flags)
 {
     if (session == NULL) {
         doThrowNPE(env);
@@ -231,15 +232,10 @@
     SurfaceComposerClient* client =
             (SurfaceComposerClient*)env->GetIntField(session, sso.client);
 
-    sp<SurfaceControl> surface;
-    if (jname == NULL) {
-        surface = client->createSurface(layerStack, w, h, format, flags);
-    } else {
-        const jchar* str = env->GetStringCritical(jname, 0);
-        const String8 name(str, env->GetStringLength(jname));
-        env->ReleaseStringCritical(jname, str);
-        surface = client->createSurface(name, layerStack, w, h, format, flags);
-    }
+    const jchar* str = env->GetStringCritical(jname, 0);
+    const String8 name(str, env->GetStringLength(jname));
+    env->ReleaseStringCritical(jname, str);
+    sp<SurfaceControl> surface = client->createSurface(name, w, h, format, flags);
 
     if (surface == 0) {
         jniThrowException(env, OutOfResourcesException, NULL);
@@ -473,12 +469,11 @@
 }
 
 static void Surface_setOrientation(
-        JNIEnv* env, jobject clazz, jint display, jint orientation)
+        JNIEnv* env, jobject clazz, jint, jint orientation)
 {
-    int err = SurfaceComposerClient::setOrientation(display, orientation, 0);
-    if (err < 0) {
-        doThrowIAE(env);
-    }
+    sp<IBinder> display = SurfaceComposerClient::getBuiltInDisplay(
+            ISurfaceComposer::eDisplayIdMain);
+    SurfaceComposerClient::setDisplayOrientation(display, orientation);
 }
 
 class ScreenshotPixelRef : public SkPixelRef {
@@ -492,12 +487,13 @@
         SkSafeUnref(fCTable);
     }
 
-    status_t update(int width, int height, int minLayer, int maxLayer, bool allLayers) {
+    status_t update(const sp<IBinder>& display, int width, int height,
+            int minLayer, int maxLayer, bool allLayers) {
         status_t res = (width > 0 && height > 0)
                 ? (allLayers
-                        ? mScreenshot.update(width, height)
-                        : mScreenshot.update(width, height, minLayer, maxLayer))
-                : mScreenshot.update();
+                        ? mScreenshot.update(display, width, height)
+                        : mScreenshot.update(display, width, height, minLayer, maxLayer))
+                : mScreenshot.update(display);
         if (res != NO_ERROR) {
             return res;
         }
@@ -538,11 +534,15 @@
     typedef SkPixelRef INHERITED;
 };
 
-static jobject doScreenshot(JNIEnv* env, jobject clazz, jint width, jint height,
+static jobject doScreenshot(JNIEnv* env, jobject clazz,
+        jint width, jint height,
         jint minLayer, jint maxLayer, bool allLayers)
 {
+    sp<IBinder> display = SurfaceComposerClient::getBuiltInDisplay(
+            ISurfaceComposer::eDisplayIdMain);
     ScreenshotPixelRef* pixels = new ScreenshotPixelRef(NULL);
-    if (pixels->update(width, height, minLayer, maxLayer, allLayers) != NO_ERROR) {
+    if (pixels->update(display, width, height,
+            minLayer, maxLayer, allLayers) != NO_ERROR) {
         delete pixels;
         return 0;
     }
@@ -721,8 +721,10 @@
 {
     const sp<SurfaceControl>& surface(getSurfaceControl(env, thiz));
     if (surface == 0) return;
-
-    // TODO(mathias): Everything.
+    status_t err = surface->setLayerStack(layerStack);
+    if (err<0 && err!=NO_INIT) {
+        doThrowIAE(env);
+    }
 }
 
 // ----------------------------------------------------------------------------
@@ -826,14 +828,14 @@
 static void nativeClassInit(JNIEnv* env, jclass clazz);
 
 static JNINativeMethod gSurfaceSessionMethods[] = {
-    {"init",     "()V",  (void*)SurfaceSession_init },
-    {"destroy",  "()V",  (void*)SurfaceSession_destroy },
-    {"kill",     "()V",  (void*)SurfaceSession_kill },
+    {"nativeInit",     "()V",  (void*)SurfaceSession_nativeInit },
+    {"nativeDestroy",  "()V",  (void*)SurfaceSession_nativeDestroy },
+    {"nativeKill",     "()V",  (void*)SurfaceSession_nativeKill },
 };
 
 static JNINativeMethod gSurfaceMethods[] = {
     {"nativeClassInit",     "()V",  (void*)nativeClassInit },
-    {"init",                "(Landroid/view/SurfaceSession;ILjava/lang/String;IIIII)V",  (void*)Surface_init },
+    {"init",                "(Landroid/view/SurfaceSession;Ljava/lang/String;IIII)V",  (void*)Surface_init },
     {"init",                "(Landroid/os/Parcel;)V",  (void*)Surface_initParcel },
     {"initFromSurfaceTexture", "(Landroid/graphics/SurfaceTexture;)V", (void*)Surface_initFromSurfaceTexture },
     {"getIdentity",         "()I",  (void*)Surface_getIdentity },
diff --git a/services/input/SpriteController.cpp b/services/input/SpriteController.cpp
index b15d4c8..5bbaa48 100644
--- a/services/input/SpriteController.cpp
+++ b/services/input/SpriteController.cpp
@@ -369,7 +369,7 @@
     ensureSurfaceComposerClient();
 
     sp<SurfaceControl> surfaceControl = mSurfaceComposerClient->createSurface(
-            String8("Sprite"), 0, width, height, PIXEL_FORMAT_RGBA_8888);
+            String8("Sprite"), width, height, PIXEL_FORMAT_RGBA_8888);
     if (surfaceControl == NULL || !surfaceControl->isValid()
             || !surfaceControl->getSurface()->isValid()) {
         ALOGE("Error creating sprite surface.");
diff --git a/services/jni/com_android_server_display_SurfaceFlingerDisplayAdapter.cpp b/services/jni/com_android_server_display_SurfaceFlingerDisplayAdapter.cpp
index 05a74d3..709d225 100644
--- a/services/jni/com_android_server_display_SurfaceFlingerDisplayAdapter.cpp
+++ b/services/jni/com_android_server_display_SurfaceFlingerDisplayAdapter.cpp
@@ -20,6 +20,7 @@
 #include "jni.h"
 #include <android_runtime/AndroidRuntime.h>
 
+#include <gui/ISurfaceComposer.h>
 #include <gui/SurfaceComposerClient.h>
 #include <ui/DisplayInfo.h>
 
@@ -38,8 +39,11 @@
 
 
 static void nativeGetDefaultDisplayDeviceInfo(JNIEnv* env, jclass clazz, jobject infoObj) {
+    sp<IBinder> display(SurfaceComposerClient::getBuiltInDisplay(
+            ISurfaceComposer::eDisplayIdMain));
+
     DisplayInfo info;
-    status_t err = SurfaceComposerClient::getDisplayInfo(0, &info);
+    status_t err = SurfaceComposerClient::getDisplayInfo(display, &info);
     if (err < 0) {
         jniThrowExceptionFmt(env, "java/lang/RuntimeException",
                 "Could not get display info.  err=%d", err);