hwc/overlay: Dynamic pipe tracking.

Remove state based pipe handling and make pipes tracked dynamically.
Add the configStart, configDone, nextPipe availablePipes APIs.
Remove setState API.

-configStart marks all pipes available.
-configDone garbage-collects unused pipe objects.
-nextPipe returns the index of next available pipe and create a corresponding
pipe object if not present
-availablePipes returns total of "unallocated" pipes. (Pipes could be allocated
but unused)

Changes in hwc adapt to the new overlay APIs.
Compile out MDP comp.
Remove unused files in overlay and hwc.
Update licenses.

Conflicts:

	libhwcomposer/hwc.cpp
	libhwcomposer/hwc_utils.cpp

Bug: 7626586
Change-Id: Id8e56901d34e5dc0fd088260d05e3e46f68ff090
Signed-off-by: Saurabh Shah <saurshah@codeaurora.org>
Signed-off-by: Iliyan Malchev <malchev@google.com>
diff --git a/libhwcomposer/hwc_video.cpp b/libhwcomposer/hwc_video.cpp
index aad2939..5fba951 100644
--- a/libhwcomposer/hwc_video.cpp
+++ b/libhwcomposer/hwc_video.cpp
@@ -22,11 +22,11 @@
 
 namespace qhwc {
 
-#define FINAL_TRANSFORM_MASK 0x000F
+namespace ovutils = overlay::utils;
 
 //Static Members
-ovutils::eOverlayState VideoOverlay::sState[] = {ovutils::OV_CLOSED};
 bool VideoOverlay::sIsModeOn[] = {false};
+ovutils::eDest VideoOverlay::sDest[] = {ovutils::OV_INVALID};
 
 //Cache stats, figure out the state, config overlay
 bool VideoOverlay::prepare(hwc_context_t *ctx, hwc_display_contents_1_t *list,
@@ -45,9 +45,9 @@
     }
 
     //index guaranteed to be not -1 at this point
-    hwc_layer_1_t *yuvLayer = &list->hwLayers[yuvIndex];
+    hwc_layer_1_t *layer = &list->hwLayers[yuvIndex];
 
-    private_handle_t *hnd = (private_handle_t *)yuvLayer->handle;
+    private_handle_t *hnd = (private_handle_t *)layer->handle;
     if(ctx->mSecureMode) {
         if (! isSecureBuffer(hnd)) {
             ALOGD_IF(VIDEO_DEBUG, "%s: Handle non-secure video layer"
@@ -61,64 +61,35 @@
             return false;
         }
     }
-    chooseState(ctx, dpy, yuvLayer);
-    if(configure(ctx, dpy, yuvLayer)) {
-        markFlags(yuvLayer);
+    if(configure(ctx, dpy, layer)) {
+        markFlags(layer);
         sIsModeOn[dpy] = true;
     }
 
     return sIsModeOn[dpy];
 }
 
-void VideoOverlay::chooseState(hwc_context_t *ctx, int dpy,
-        hwc_layer_1_t *yuvLayer) {
-    ALOGD_IF(VIDEO_DEBUG, "%s: old state = %s", __FUNCTION__,
-            ovutils::getStateString(sState[dpy]));
-
-    private_handle_t *hnd = NULL;
-    if(yuvLayer) {
-        hnd = (private_handle_t *)yuvLayer->handle;
-    }
-    ovutils::eOverlayState newState = ovutils::OV_CLOSED;
-    switch(dpy) {
-        case HWC_DISPLAY_PRIMARY:
-            if(ctx->listStats[dpy].yuvCount == 1) {
-                newState = ovutils::OV_2D_VIDEO_ON_PANEL;
-                if(isSkipLayer(yuvLayer) && !isSecureBuffer(hnd)) {
-                    newState = ovutils::OV_CLOSED;
-                }
-            }
-            break;
-        case HWC_DISPLAY_EXTERNAL:
-            newState = ctx->mOverlay[HWC_DISPLAY_EXTERNAL]->getState(); //If we are here, external is active
-            if(ctx->listStats[dpy].yuvCount == 1) {
-                if(!isSkipLayer(yuvLayer) || isSecureBuffer(hnd)) {
-                    newState = ovutils::OV_UI_VIDEO_TV;
-                }
-            }
-            break;
-        default:
-            break;
-    }
-
-    sState[dpy] = newState;
-    ALOGD_IF(VIDEO_DEBUG, "%s: new chosen state = %s", __FUNCTION__,
-            ovutils::getStateString(sState[dpy]));
-}
-
-void VideoOverlay::markFlags(hwc_layer_1_t *yuvLayer) {
-    if(yuvLayer) {
-        yuvLayer->compositionType = HWC_OVERLAY;
-        yuvLayer->hints |= HWC_HINT_CLEAR_FB;
+void VideoOverlay::markFlags(hwc_layer_1_t *layer) {
+    if(layer) {
+        layer->compositionType = HWC_OVERLAY;
+        layer->hints |= HWC_HINT_CLEAR_FB;
     }
 }
 
-/* Helpers */
-bool configPrimVid(hwc_context_t *ctx, hwc_layer_1_t *layer) {
-    overlay::Overlay& ov = *(ctx->mOverlay[HWC_DISPLAY_PRIMARY]);
+bool VideoOverlay::configure(hwc_context_t *ctx, int dpy,
+        hwc_layer_1_t *layer) {
+    overlay::Overlay& ov = *(ctx->mOverlay);
     private_handle_t *hnd = (private_handle_t *)layer->handle;
     ovutils::Whf info(hnd->width, hnd->height, hnd->format, hnd->size);
 
+    //Request a VG pipe
+    ovutils::eDest dest = ov.nextPipe(ovutils::OV_MDP_PIPE_VG, dpy);
+    if(dest == ovutils::OV_INVALID) { //None available
+        return false;
+    }
+
+    sDest[dpy] = dest;
+
     ovutils::eMdpFlags mdpFlags = ovutils::OV_MDP_FLAGS_NONE;
     if (isSecureBuffer(hnd)) {
         ovutils::setMdpFlags(mdpFlags,
@@ -131,19 +102,23 @@
     }
 
     ovutils::eIsFg isFgFlag = ovutils::IS_FG_OFF;
-    if (ctx->listStats[HWC_DISPLAY_PRIMARY].numAppLayers == 1) {
+    if (ctx->listStats[dpy].numAppLayers == 1) {
         isFgFlag = ovutils::IS_FG_SET;
     }
 
+    //TODO change to 1 always when primary FB uses overlay.
+    const ovutils::eZorder zorder = (dpy == HWC_DISPLAY_PRIMARY) ?
+            ovutils::ZORDER_0 : ovutils::ZORDER_1;
+
     ovutils::PipeArgs parg(mdpFlags,
             info,
-            ovutils::ZORDER_0,
+            zorder,
             isFgFlag,
             ovutils::ROT_FLAG_DISABLED);
-    ovutils::PipeArgs pargs[ovutils::MAX_PIPES] = { parg, parg, parg };
-    ov.setSource(pargs, ovutils::OV_PIPE0);
 
-    int transform = layer->transform & FINAL_TRANSFORM_MASK;
+    ov.setSource(parg, dest);
+
+    int transform = layer->transform;
     ovutils::eTransform orient =
             static_cast<ovutils::eTransform>(transform);
 
@@ -152,8 +127,8 @@
 
     //Calculate the rect for primary based on whether the supplied position
     //is within or outside bounds.
-    const int fbWidth = ctx->dpyAttr[HWC_DISPLAY_PRIMARY].xres;
-    const int fbHeight = ctx->dpyAttr[HWC_DISPLAY_PRIMARY].yres;
+    const int fbWidth = ctx->dpyAttr[dpy].xres;
+    const int fbHeight = ctx->dpyAttr[dpy].yres;
 
     if( displayFrame.left < 0 ||
             displayFrame.top < 0 ||
@@ -168,127 +143,24 @@
             sourceCrop.right - sourceCrop.left,
             sourceCrop.bottom - sourceCrop.top);
     //Only for Primary
-    ov.setCrop(dcrop, ovutils::OV_PIPE0);
+    ov.setCrop(dcrop, dest);
 
-    ov.setTransform(orient, ovutils::OV_PIPE0);
+    ov.setTransform(orient, dest);
 
     // position x,y,w,h
     ovutils::Dim dpos(displayFrame.left,
             displayFrame.top,
             displayFrame.right - displayFrame.left,
             displayFrame.bottom - displayFrame.top);
-    ov.setPosition(dpos, ovutils::OV_PIPE0);
+    ov.setPosition(dpos, dest);
 
-    if (!ov.commit(ovutils::OV_PIPE0)) {
+    if (!ov.commit(dest)) {
         ALOGE("%s: commit fails", __FUNCTION__);
         return false;
     }
     return true;
 }
 
-bool configExtVid(hwc_context_t *ctx, hwc_layer_1_t *layer) {
-    overlay::Overlay& ov = *(ctx->mOverlay[HWC_DISPLAY_EXTERNAL]);
-    private_handle_t *hnd = (private_handle_t *)layer->handle;
-    ovutils::Whf info(hnd->width, hnd->height, hnd->format, hnd->size);
-
-    ovutils::eMdpFlags mdpFlags = ovutils::OV_MDP_FLAGS_NONE;
-    if (isSecureBuffer(hnd)) {
-        ovutils::setMdpFlags(mdpFlags,
-                ovutils::OV_MDP_SECURE_OVERLAY_SESSION);
-    }
-
-    if(layer->blending == HWC_BLENDING_PREMULT) {
-        ovutils::setMdpFlags(mdpFlags,
-                ovutils::OV_MDP_BLEND_FG_PREMULT);
-    }
-
-    ovutils::eIsFg isFgFlag = ovutils::IS_FG_OFF;
-    if (ctx->listStats[HWC_DISPLAY_EXTERNAL].numAppLayers == 1) {
-        isFgFlag = ovutils::IS_FG_SET;
-    }
-
-    ovutils::PipeArgs parg(mdpFlags,
-            info,
-            ovutils::ZORDER_1,
-            isFgFlag,
-            ovutils::ROT_FLAG_DISABLED);
-    ovutils::PipeArgs pargs[ovutils::MAX_PIPES] = { parg, parg, parg };
-    ov.setSource(pargs, ovutils::OV_PIPE1);
-
-    int transform = layer->transform;
-    ovutils::eTransform orient =
-            static_cast<ovutils::eTransform>(transform);
-
-    hwc_rect_t sourceCrop = layer->sourceCrop;
-    hwc_rect_t displayFrame = layer->displayFrame;
-
-    //Calculate the rect for primary based on whether the supplied position
-    //is within or outside bounds.
-    const int fbWidth = ctx->dpyAttr[HWC_DISPLAY_EXTERNAL].xres;
-    const int fbHeight = ctx->dpyAttr[HWC_DISPLAY_EXTERNAL].yres;
-
-    if( displayFrame.left < 0 ||
-            displayFrame.top < 0 ||
-            displayFrame.right > fbWidth ||
-            displayFrame.bottom > fbHeight) {
-        calculate_crop_rects(sourceCrop, displayFrame, fbWidth, fbHeight,
-                transform);
-    }
-
-    // x,y,w,h
-    ovutils::Dim dcrop(sourceCrop.left, sourceCrop.top,
-            sourceCrop.right - sourceCrop.left,
-            sourceCrop.bottom - sourceCrop.top);
-    //Only for External
-    ov.setCrop(dcrop, ovutils::OV_PIPE1);
-
-    ov.setTransform(orient, ovutils::OV_PIPE1);
-
-    ovutils::Dim dpos(displayFrame.left,
-            displayFrame.top,
-            (displayFrame.right - displayFrame.left),
-            (displayFrame.bottom - displayFrame.top));
-
-    //Only for External
-    ov.setPosition(dpos, ovutils::OV_PIPE1);
-
-    if (!ov.commit(ovutils::OV_PIPE1)) {
-        ALOGE("%s: commit fails", __FUNCTION__);
-        return false;
-    }
-    return true;
-}
-
-bool VideoOverlay::configure(hwc_context_t *ctx, int dpy,
-        hwc_layer_1_t *yuvLayer) {
-    bool ret = true;
-    overlay::Overlay& ov = *(ctx->mOverlay[dpy]);
-    switch(dpy) {
-        case HWC_DISPLAY_PRIMARY:
-            // Set overlay state
-            ov.setState(sState[dpy]);
-            switch(sState[dpy]) {
-                case ovutils::OV_2D_VIDEO_ON_PANEL:
-                    ret &= configPrimVid(ctx, yuvLayer);
-                    break;
-                default:
-                    return false;
-            }
-            break;
-        case HWC_DISPLAY_EXTERNAL:
-            ov.setState(sState[dpy]);
-            switch(sState[dpy]) {
-                case ovutils::OV_UI_VIDEO_TV:
-                    ret = configExtVid(ctx, yuvLayer);
-                    break;
-                default:
-                    return false;
-            }
-            break;
-    }
-    return ret;
-}
-
 bool VideoOverlay::draw(hwc_context_t *ctx, hwc_display_contents_1_t *list,
         int dpy)
 {
@@ -305,39 +177,14 @@
             list->hwLayers[yuvIndex].handle;
 
     bool ret = true;
-    overlay::Overlay& ov = *(ctx->mOverlay[dpy]);
-    ovutils::eOverlayState state = ov.getState();
+    overlay::Overlay& ov = *(ctx->mOverlay);
 
-    switch(dpy) {
-        case HWC_DISPLAY_PRIMARY:
-            switch (state) {
-                case ovutils::OV_2D_VIDEO_ON_PANEL:
-                    // Play primary
-                    if (!ov.queueBuffer(hnd->fd, hnd->offset, ovutils::OV_PIPE0)) {
-                        ALOGE("%s: queueBuffer failed for primary", __FUNCTION__);
-                        ret = false;
-                    }
-                    break;
-                default:
-                    ret = false;
-                    break;
-            }
-            break;
-        case HWC_DISPLAY_EXTERNAL:
-            switch(state) {
-                case ovutils::OV_UI_VIDEO_TV:
-                    // Play external
-                    if (!ov.queueBuffer(hnd->fd, hnd->offset, ovutils::OV_PIPE1)) {
-                        ALOGE("%s: queueBuffer failed for external", __FUNCTION__);
-                        ret = false;
-                    }
-                    break;
-                default:
-                    ret = false;
-                    break;
-            }
-            break;
+    if (!ov.queueBuffer(hnd->fd, hnd->offset,
+                sDest[dpy])) {
+        ALOGE("%s: queueBuffer failed for dpy=%d", __FUNCTION__, dpy);
+        ret = false;
     }
+
     return ret;
 }