Banish DisplayID from the SurfaceFlinger API.

Use only display tokens in the API to refer to new displays.

Don't require the caller to specify the display when creating
a surface (since in general a surface could be shown on
any display).

This is intended to be a minimum change just to update the API.
Note that SurfaceFlinger still uses DisplayID in a few places
internally that might cause some features not to work properly
when there are multiple displays (LayerScreenshot, for example).

Change-Id: I3d91eec2da406eefd97bcd53655d403ad865a7e6
diff --git a/services/surfaceflinger/Client.cpp b/services/surfaceflinger/Client.cpp
index d5d551e..b0f2330 100644
--- a/services/surfaceflinger/Client.cpp
+++ b/services/surfaceflinger/Client.cpp
@@ -111,7 +111,7 @@
 sp<ISurface> Client::createSurface(
         ISurfaceComposerClient::surface_data_t* params,
         const String8& name,
-        DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
+        uint32_t w, uint32_t h, PixelFormat format,
         uint32_t flags)
 {
     /*
@@ -125,7 +125,6 @@
         ISurfaceComposerClient::surface_data_t* params;
         Client* client;
         const String8& name;
-        DisplayID display;
         uint32_t w, h;
         PixelFormat format;
         uint32_t flags;
@@ -133,22 +132,23 @@
         MessageCreateLayer(SurfaceFlinger* flinger,
                 ISurfaceComposerClient::surface_data_t* params,
                 const String8& name, Client* client,
-                DisplayID display, uint32_t w, uint32_t h, PixelFormat format,
+                uint32_t w, uint32_t h, PixelFormat format,
                 uint32_t flags)
             : flinger(flinger), params(params), client(client), name(name),
-              display(display), w(w), h(h), format(format), flags(flags)
+              w(w), h(h), format(format), flags(flags)
         {
         }
         sp<ISurface> getResult() const { return result; }
         virtual bool handler() {
+            // TODO don't require display id to create a layer
             result = flinger->createLayer(params, name, client,
-                    display, w, h, format, flags);
+                    ISurfaceComposer::eDisplayIdMain, w, h, format, flags);
             return true;
         }
     };
 
     sp<MessageBase> msg = new MessageCreateLayer(mFlinger.get(),
-            params, name, this, display, w, h, format, flags);
+            params, name, this, w, h, format, flags);
     mFlinger->postMessageSync(msg);
     return static_cast<MessageCreateLayer*>( msg.get() )->getResult();
 }
diff --git a/services/surfaceflinger/Client.h b/services/surfaceflinger/Client.h
index 9bfee72..d6c6931 100644
--- a/services/surfaceflinger/Client.h
+++ b/services/surfaceflinger/Client.h
@@ -54,7 +54,7 @@
     // ISurfaceComposerClient interface
     virtual sp<ISurface> createSurface(
             surface_data_t* params, const String8& name,
-            DisplayID display, uint32_t w, uint32_t h,PixelFormat format,
+            uint32_t w, uint32_t h,PixelFormat format,
             uint32_t flags);
 
     virtual status_t destroySurface(SurfaceID surfaceId);
diff --git a/services/surfaceflinger/SurfaceFlinger.cpp b/services/surfaceflinger/SurfaceFlinger.cpp
index 1406deb..ed687e3 100644
--- a/services/surfaceflinger/SurfaceFlinger.cpp
+++ b/services/surfaceflinger/SurfaceFlinger.cpp
@@ -511,9 +511,11 @@
     return false;
 }
 
-status_t SurfaceFlinger::getDisplayInfo(DisplayID dpy, DisplayInfo* info) {
-    // TODO: this is here only for compatibility -- should go away eventually.
-    if (uint32_t(dpy) >= 1) {
+status_t SurfaceFlinger::getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info) {
+    // TODO: this is mostly here only for compatibility
+    //       the display size is needed but the display metrics should come from elsewhere
+    if (display != mDefaultDisplays[ISurfaceComposer::eDisplayIdMain]) {
+        // TODO: additional displays not yet supported
         return BAD_INDEX;
     }
 
@@ -570,7 +572,7 @@
     return mEventThread->createEventConnection();
 }
 
-void SurfaceFlinger::connectDisplay(const sp<ISurfaceTexture> surface) {
+void SurfaceFlinger::connectDisplay(const sp<ISurfaceTexture>& surface) {
 
     sp<IBinder> token;
     { // scope for the lock
@@ -2242,7 +2244,7 @@
 
 // ---------------------------------------------------------------------------
 
-status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy,
+status_t SurfaceFlinger::captureScreenImplLocked(const sp<IBinder>& display,
         sp<IMemoryHeap>* heap,
         uint32_t* w, uint32_t* h, PixelFormat* f,
         uint32_t sw, uint32_t sh,
@@ -2252,9 +2254,8 @@
 
     status_t result = PERMISSION_DENIED;
 
-    // only one display supported for now
-    if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT)) {
-        ALOGE("invalid display %d", dpy);
+    const DisplayDeviceState& disp(mDrawingState.displays.valueFor(display));
+    if (CC_UNLIKELY(disp.id < 0)) {
         return BAD_VALUE;
     }
 
@@ -2263,7 +2264,7 @@
     }
 
     // get screen geometry
-    sp<const DisplayDevice> hw(getDisplayDevice(dpy));
+    sp<const DisplayDevice> hw(getDisplayDevice(disp.id));
     const uint32_t hw_w = hw->getWidth();
     const uint32_t hw_h = hw->getHeight();
 
@@ -2316,6 +2317,7 @@
         glClearColor(0,0,0,1);
         glClear(GL_COLOR_BUFFER_BIT);
 
+        // TODO: filter the layers that are drawn based on the layer stack of the display.
         const LayerVector& layers(mDrawingState.layersSortedByZ);
         const size_t count = layers.size();
         for (size_t i=0 ; i<count ; ++i) {
@@ -2377,14 +2379,13 @@
 }
 
 
-status_t SurfaceFlinger::captureScreen(DisplayID dpy,
+status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
         sp<IMemoryHeap>* heap,
         uint32_t* width, uint32_t* height, PixelFormat* format,
         uint32_t sw, uint32_t sh,
         uint32_t minLayerZ, uint32_t maxLayerZ)
 {
-    // only one display supported for now
-    if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT))
+    if (CC_UNLIKELY(display == 0))
         return BAD_VALUE;
 
     if (!GLExtensions::getInstance().haveFramebufferObject())
@@ -2392,7 +2393,7 @@
 
     class MessageCaptureScreen : public MessageBase {
         SurfaceFlinger* flinger;
-        DisplayID dpy;
+        sp<IBinder> display;
         sp<IMemoryHeap>* heap;
         uint32_t* w;
         uint32_t* h;
@@ -2403,11 +2404,11 @@
         uint32_t maxLayerZ;
         status_t result;
     public:
-        MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy,
+        MessageCaptureScreen(SurfaceFlinger* flinger, const sp<IBinder>& display,
                 sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f,
                 uint32_t sw, uint32_t sh,
                 uint32_t minLayerZ, uint32_t maxLayerZ)
-            : flinger(flinger), dpy(dpy),
+            : flinger(flinger), display(display),
               heap(heap), w(w), h(h), f(f), sw(sw), sh(sh),
               minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
               result(PERMISSION_DENIED)
@@ -2418,14 +2419,14 @@
         }
         virtual bool handler() {
             Mutex::Autolock _l(flinger->mStateLock);
-            result = flinger->captureScreenImplLocked(dpy,
+            result = flinger->captureScreenImplLocked(display,
                     heap, w, h, f, sw, sh, minLayerZ, maxLayerZ);
             return true;
         }
     };
 
     sp<MessageBase> msg = new MessageCaptureScreen(this,
-            dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
+            display, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ);
     status_t res = postMessageSync(msg);
     if (res == NO_ERROR) {
         res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult();
diff --git a/services/surfaceflinger/SurfaceFlinger.h b/services/surfaceflinger/SurfaceFlinger.h
index 7ad5450..653a631 100644
--- a/services/surfaceflinger/SurfaceFlinger.h
+++ b/services/surfaceflinger/SurfaceFlinger.h
@@ -195,7 +195,7 @@
     virtual bool authenticateSurfaceTexture(
         const sp<ISurfaceTexture>& surface) const;
     virtual sp<IDisplayEventConnection> createDisplayEventConnection();
-    virtual status_t captureScreen(DisplayID dpy, sp<IMemoryHeap>* heap,
+    virtual status_t captureScreen(const sp<IBinder>& display, sp<IMemoryHeap>* heap,
         uint32_t* width, uint32_t* height, PixelFormat* format,
         uint32_t reqWidth, uint32_t reqHeight, uint32_t minLayerZ,
         uint32_t maxLayerZ);
@@ -203,8 +203,8 @@
     virtual void blank();
     // called when screen is turning back on
     virtual void unblank();
-    virtual status_t getDisplayInfo(DisplayID dpy, DisplayInfo* info);
-    virtual void connectDisplay(const sp<ISurfaceTexture> display);
+    virtual status_t getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info);
+    virtual void connectDisplay(const sp<ISurfaceTexture>& display);
 
     /* ------------------------------------------------------------------------
      * DeathRecipient interface
@@ -304,7 +304,7 @@
 
     void startBootAnim();
 
-    status_t captureScreenImplLocked(DisplayID dpy, sp<IMemoryHeap>* heap,
+    status_t captureScreenImplLocked(const sp<IBinder>& display, sp<IMemoryHeap>* heap,
         uint32_t* width, uint32_t* height, PixelFormat* format,
         uint32_t reqWidth, uint32_t reqHeight, uint32_t minLayerZ,
         uint32_t maxLayerZ);
diff --git a/services/surfaceflinger/tests/Transaction_test.cpp b/services/surfaceflinger/tests/Transaction_test.cpp
index e3a98ff..0592c5b 100644
--- a/services/surfaceflinger/tests/Transaction_test.cpp
+++ b/services/surfaceflinger/tests/Transaction_test.cpp
@@ -57,7 +57,8 @@
         uint32_t w=0, h=0;
         PixelFormat fmt=0;
         sp<ISurfaceComposer> sf(ComposerService::getComposerService());
-        ASSERT_EQ(NO_ERROR, sf->captureScreen(0, &heap, &w, &h, &fmt, 0, 0,
+        sp<IBinder> display(sf->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
+        ASSERT_EQ(NO_ERROR, sf->captureScreen(display, &heap, &w, &h, &fmt, 0, 0,
                 0, INT_MAX));
         ASSERT_TRUE(heap != NULL);
         ASSERT_EQ(PIXEL_FORMAT_RGBA_8888, fmt);
@@ -93,15 +94,17 @@
         mComposerClient = new SurfaceComposerClient;
         ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
 
+        sp<IBinder> display(SurfaceComposerClient::getBuiltInDisplay(
+                ISurfaceComposer::eDisplayIdMain));
         DisplayInfo info;
-        SurfaceComposerClient::getDisplayInfo(0, &info);
+        SurfaceComposerClient::getDisplayInfo(display, &info);
 
         ssize_t displayWidth = info.w;
         ssize_t displayHeight = info.h;
 
         // Background surface
         mBGSurfaceControl = mComposerClient->createSurface(
-                String8("BG Test Surface"), 0, displayWidth, displayHeight,
+                String8("BG Test Surface"), displayWidth, displayHeight,
                 PIXEL_FORMAT_RGBA_8888, 0);
         ASSERT_TRUE(mBGSurfaceControl != NULL);
         ASSERT_TRUE(mBGSurfaceControl->isValid());
@@ -109,7 +112,7 @@
 
         // Foreground surface
         mFGSurfaceControl = mComposerClient->createSurface(
-                String8("FG Test Surface"), 0, 64, 64, PIXEL_FORMAT_RGBA_8888, 0);
+                String8("FG Test Surface"), 64, 64, PIXEL_FORMAT_RGBA_8888, 0);
         ASSERT_TRUE(mFGSurfaceControl != NULL);
         ASSERT_TRUE(mFGSurfaceControl->isValid());
 
@@ -117,7 +120,7 @@
 
         // Synchronization surface
         mSyncSurfaceControl = mComposerClient->createSurface(
-                String8("Sync Test Surface"), 0, 1, 1, PIXEL_FORMAT_RGBA_8888, 0);
+                String8("Sync Test Surface"), 1, 1, PIXEL_FORMAT_RGBA_8888, 0);
         ASSERT_TRUE(mSyncSurfaceControl != NULL);
         ASSERT_TRUE(mSyncSurfaceControl->isValid());
 
diff --git a/services/surfaceflinger/tests/resize/resize.cpp b/services/surfaceflinger/tests/resize/resize.cpp
index c143b3d..d61ea70 100644
--- a/services/surfaceflinger/tests/resize/resize.cpp
+++ b/services/surfaceflinger/tests/resize/resize.cpp
@@ -38,8 +38,8 @@
     // create a client to surfaceflinger
     sp<SurfaceComposerClient> client = new SurfaceComposerClient();
     
-    sp<Surface> surface = client->createSurface(0, 160, 240,
-            PIXEL_FORMAT_RGB_565);
+    sp<Surface> surface = client->createSurface(String8("resize"),
+            160, 240, PIXEL_FORMAT_RGB_565, 0);
 
 
     SurfaceComposerClient::openGlobalTransaction();
diff --git a/services/surfaceflinger/tests/screencap/screencap.cpp b/services/surfaceflinger/tests/screencap/screencap.cpp
index 53566e0..f842fc3 100644
--- a/services/surfaceflinger/tests/screencap/screencap.cpp
+++ b/services/surfaceflinger/tests/screencap/screencap.cpp
@@ -42,7 +42,8 @@
     sp<IMemoryHeap> heap;
     uint32_t w, h;
     PixelFormat f;
-    status_t err = composer->captureScreen(0, &heap, &w, &h, &f, 0, 0);
+    sp<IBinder> display(composer->getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain));
+    status_t err = composer->captureScreen(display, &heap, &w, &h, &f, 0, 0);
     if (err != NO_ERROR) {
         fprintf(stderr, "screen capture failed: %s\n", strerror(-err));
         exit(0);
diff --git a/services/surfaceflinger/tests/surface/surface.cpp b/services/surfaceflinger/tests/surface/surface.cpp
index a8878f7..9c41cc3 100644
--- a/services/surfaceflinger/tests/surface/surface.cpp
+++ b/services/surfaceflinger/tests/surface/surface.cpp
@@ -37,7 +37,7 @@
     sp<SurfaceComposerClient> client = new SurfaceComposerClient();
     
     sp<SurfaceControl> surfaceControl = client->createSurface(
-            0, 160, 240, PIXEL_FORMAT_RGB_565);
+            String8("surface"), 160, 240, PIXEL_FORMAT_RGB_565, 0);
     SurfaceComposerClient::openGlobalTransaction();
     surfaceControl->setLayer(100000);
     SurfaceComposerClient::closeGlobalTransaction();