overlay: pipe reservation mechanism for overlay

This change provides APIs for this mechanism. In a case where
pipeid corresponding to the layer needs to be reserved, then pipe
session could be initiated for that pipe. This will ensure that
the pipe is not garbage collected, and is manually destroyed when
the session (or pipe requirement) has been ended. This change
also provides the api to locally allocate the dest for the given
reserved overlay pipeid.

Change-Id: I3fec3e26f69305c280395b7a92edf9e457398052
diff --git a/liboverlay/overlay.cpp b/liboverlay/overlay.cpp
index 9222af5..6784d4f 100644
--- a/liboverlay/overlay.cpp
+++ b/liboverlay/overlay.cpp
@@ -80,7 +80,8 @@
 
 void Overlay::configDone() {
     for(int i = 0; i < PipeBook::NUM_PIPES; i++) {
-        if(PipeBook::isNotUsed(i)) {
+        if((PipeBook::isNotUsed(i) && !sessionInProgress((eDest)i)) ||
+                    isSessionEnded((eDest)i)) {
             //Forces UNSET on pipes, flushes rotator memory and session, closes
             //fds
             if(mPipeBook[i].valid()) {
@@ -106,6 +107,27 @@
 #endif
 }
 
+int Overlay::getPipeId(utils::eDest dest) {
+    return mPipeBook[(int)dest].mPipe->getPipeId();
+}
+
+eDest Overlay::getDest(int pipeid) {
+    eDest dest = OV_INVALID;
+    // finding the dest corresponding to the given pipe
+    for(int i=0; i < PipeBook::NUM_PIPES; ++i) {
+        if(mPipeBook[i].valid() && mPipeBook[i].mPipe->getPipeId() == pipeid) {
+            return (eDest)i;
+        }
+    }
+    return dest;
+}
+
+eDest Overlay::reservePipe(int pipeid) {
+    eDest dest = getDest(pipeid);
+    PipeBook::setAllocation((int)dest);
+    return dest;
+}
+
 eDest Overlay::nextPipe(eMdpPipeType type, int dpy, int mixer) {
     eDest dest = OV_INVALID;
 
@@ -131,6 +153,7 @@
         mPipeBook[index].mMixer = mixer;
         if(not mPipeBook[index].valid()) {
             mPipeBook[index].mPipe = new GenericPipe(dpy);
+            mPipeBook[index].mSession = PipeBook::NONE;
             char str[32];
             snprintf(str, 32, "Set=%s dpy=%d mix=%d; ",
                      PipeBook::getDestStr(dest), dpy, mixer);
@@ -146,6 +169,13 @@
     return dest;
 }
 
+void Overlay::endAllSessions() {
+    for(int i = 0; i < PipeBook::NUM_PIPES; i++) {
+        if(mPipeBook[i].valid() && mPipeBook[i].mSession==PipeBook::START)
+            mPipeBook[i].mSession = PipeBook::END;
+    }
+}
+
 bool Overlay::isPipeTypeAttached(eMdpPipeType type) {
     for(int i = 0; i < PipeBook::NUM_PIPES; i++) {
         if(type == PipeBook::getPipeType((eDest)i) &&
@@ -453,6 +483,7 @@
     }
     mDisplay = DPY_UNUSED;
     mMixer = MIXER_UNUSED;
+    mSession = NONE;
 }
 
 Overlay* Overlay::sInstance = 0;
diff --git a/liboverlay/overlay.h b/liboverlay/overlay.h
index 854fa30..13debb7 100644
--- a/liboverlay/overlay.h
+++ b/liboverlay/overlay.h
@@ -76,6 +76,15 @@
      * asisgned to a mixer within a display it cannot be reused for another
      * mixer without being UNSET once*/
     utils::eDest nextPipe(utils::eMdpPipeType, int dpy, int mixer);
+    /* Returns the eDest corresponding to an already allocated pipeid.
+     * Useful for the reservation case, when libvpu reserves the pipe at its
+     * end, and expect the overlay to allocate a given pipe for a layer.
+     */
+    utils::eDest reservePipe(int pipeid);
+    /* getting dest for the given pipeid */
+    utils::eDest getDest(int pipeid);
+    /* getting overlay.pipeid for the given dest */
+    int getPipeId(utils::eDest dest);
 
     void setSource(const utils::PipeArgs args, utils::eDest dest);
     void setCrop(const utils::Dim& d, utils::eDest dest);
@@ -85,6 +94,14 @@
     bool commit(utils::eDest dest);
     bool queueBuffer(int fd, uint32_t offset, utils::eDest dest);
 
+    /* pipe reservation session is running */
+    bool sessionInProgress(utils::eDest dest);
+    /* pipe reservation session has ended*/
+    bool isSessionEnded(utils::eDest dest);
+    /* start session for the pipe reservation */
+    void startSession(utils::eDest dest);
+    /* end all started sesisons */
+    void endAllSessions();
     /* Returns available ("unallocated") pipes for a display's mixer */
     int availablePipes(int dpy, int mixer);
     /* Returns available ("unallocated") pipes for a display */
@@ -160,7 +177,13 @@
 
         static int NUM_PIPES;
         static utils::eMdpPipeType pipeTypeLUT[utils::OV_MAX];
-
+        /* Session for reserved pipes */
+        enum Session {
+            NONE,
+            START,
+            END
+        };
+        Session mSession;
 
     private:
         //usage tracks if a successful commit happened. So a pipe could be
@@ -312,6 +335,18 @@
     return pipeTypeLUT[(int)dest];
 }
 
+inline void Overlay::startSession(utils::eDest dest) {
+    mPipeBook[(int)dest].mSession = PipeBook::START;
+}
+
+inline bool Overlay::sessionInProgress(utils::eDest dest) {
+    return (mPipeBook[(int)dest].mSession == PipeBook::START);
+}
+
+inline bool Overlay::isSessionEnded(utils::eDest dest) {
+    return (mPipeBook[(int)dest].mSession == PipeBook::END);
+}
+
 inline const char* Overlay::PipeBook::getDestStr(utils::eDest dest) {
     switch(getPipeType(dest)) {
         case utils::OV_MDP_PIPE_RGB: return "RGB";
diff --git a/liboverlay/pipes/overlayGenPipe.cpp b/liboverlay/pipes/overlayGenPipe.cpp
index 06e8257..54e847b 100644
--- a/liboverlay/pipes/overlayGenPipe.cpp
+++ b/liboverlay/pipes/overlayGenPipe.cpp
@@ -167,4 +167,8 @@
     mCtrlData.ctrl.forceSet();
 }
 
+int GenericPipe::getPipeId() {
+    return mCtrlData.ctrl.getPipeId();
+}
+
 } //namespace overlay
diff --git a/liboverlay/pipes/overlayGenPipe.h b/liboverlay/pipes/overlayGenPipe.h
index 2472f4e..b5c11fe 100644
--- a/liboverlay/pipes/overlayGenPipe.h
+++ b/liboverlay/pipes/overlayGenPipe.h
@@ -77,6 +77,7 @@
      * even if they haven't changed
      */
     void forceSet();
+    int getPipeId();
 
 private:
     /* set Closed pipe */