liboverlay: Refactor, bug-fixes, upgrade.

* Fix memory leak during copying pipe objects.
* Remove unused / unnecessary code.
* setMemoryId API is merged with queueBuffer.
* setParameter API is setTransform now.
* Rotator upgraded to:
  --Allow different rotator hardware types.
  --Remove dependency on MDP code.
  --Allocate memory only during first playback,
  close when the associated pipe is closed.
* Have single commit implementation.
* Include new format types.
* Remove WAIT and CHANNEL enums and usage. Replace BypassPipe with
  GenericPipe. Client expected to set alignments and parameters.
  Add transform combination enums.
* Allow APIs to be called in any order. Do transform calcs in commit.
  Move ext type setter and getter functions.
* Add calculations for 180 transform.
* Add secure session support in rotator
* Implement all rotations in terms of H flip, V flip and 90 rotation.

Change-Id: I34a9a2a0f1255b3467a0abbaa254d0b584e901ce
diff --git a/liboverlay/overlay.cpp b/liboverlay/overlay.cpp
index ed968c1..a05a307 100644
--- a/liboverlay/overlay.cpp
+++ b/liboverlay/overlay.cpp
@@ -31,10 +31,8 @@
 #include "overlayImpl.h"
 #include "overlay.h"
 
-// MDP related FIXME move to state
 #include "overlayMdp.h"
 #include "overlayCtrlData.h"
-#include "overlayRotator.h"
 
 namespace overlay {
 
@@ -42,64 +40,16 @@
 }
 
 Overlay::~Overlay() {
-    if(mState.state() == utils::OV_CLOSED) return;
-    close();
+    mOv = mState.handleEvent(utils::OV_CLOSED, mOv);
     delete mOv;
     mOv = 0;
 }
 
-bool Overlay::open() {
-    // We need an empty open to just open the bare minimum for business
-    return true;
-}
-
-void Overlay::reset(){
-    if(mOv && !mOv->close()) {
-        ALOGE("%s Overlay failed", __FUNCTION__);
-    }
-
-    delete mOv;
-    mOv = 0;
-}
-
-bool Overlay::close()
-{
-    OVASSERT(mOv,
-            "%s Overlay and Rotator should be init at this point",
-            __FUNCTION__);
-    // FIXME that one needs to move to the state machine class
-    utils::eOverlayState st = mState.state();
-    switch (st) {
-        case utils::OV_CLOSED:
-            // try to close any partially opened items
-            break;
-        case utils::OV_2D_VIDEO_ON_PANEL:
-        case utils::OV_2D_VIDEO_ON_PANEL_TV:
-        case utils::OV_3D_VIDEO_ON_2D_PANEL:
-        case utils::OV_3D_VIDEO_ON_3D_PANEL:
-        case utils::OV_3D_VIDEO_ON_3D_TV:
-        case utils::OV_3D_VIDEO_ON_2D_PANEL_2D_TV:
-        case utils::OV_UI_MIRROR:
-        case utils::OV_2D_TRUE_UI_MIRROR:
-        case utils::OV_BYPASS_1_LAYER:
-        case utils::OV_BYPASS_2_LAYER:
-        case utils::OV_BYPASS_3_LAYER:
-            mOv = mState.handleEvent(utils::OV_CLOSED, mOv);
-            this->reset();
-            break;
-        default:
-            OVASSERT(false, "close Unknown state %d", st);
-            return false;
-    }
-    return true;
-}
-
 bool Overlay::commit(utils::eDest dest)
 {
     OVASSERT(mOv,
             "%s Overlay and Rotator should be init at this point",
             __FUNCTION__);
-    // FIXME that one needs to move to the state machine class
     utils::eOverlayState st = mState.state();
     switch (st) {
         case utils::OV_2D_VIDEO_ON_PANEL:
@@ -125,13 +75,12 @@
     return true;
 }
 
-bool Overlay::queueBuffer(uint32_t offset,
+bool Overlay::queueBuffer(int fd, uint32_t offset,
         utils::eDest dest)
 {
     OVASSERT(mOv,
             "%s Overlay and Rotator should be init at this point",
             __FUNCTION__);
-    // FIXME that one needs to move to the state machine class
     utils::eOverlayState st = mState.state();
     switch (st) {
         case utils::OV_2D_VIDEO_ON_PANEL:
@@ -145,39 +94,7 @@
         case utils::OV_BYPASS_1_LAYER:
         case utils::OV_BYPASS_2_LAYER:
         case utils::OV_BYPASS_3_LAYER:
-            if(!mOv->queueBuffer(offset, dest)) {
-                ALOGE("Overlay %s failed", __FUNCTION__);
-                return false;
-            }
-            break;
-        default:
-            OVASSERT(false, "%s Unknown state %d", __FUNCTION__, st);
-            return false;
-    }
-    return true;
-}
-
-bool Overlay::dequeueBuffer(void*& buf,
-        utils::eDest dest)
-{
-    OVASSERT(mOv,
-            "%s Overlay and Rotator should be init at this point",
-            __FUNCTION__);
-    // FIXME that one needs to move to the state machine class
-    utils::eOverlayState st = mState.state();
-    switch (st) {
-        case utils::OV_2D_VIDEO_ON_PANEL:
-        case utils::OV_2D_VIDEO_ON_PANEL_TV:
-        case utils::OV_3D_VIDEO_ON_2D_PANEL:
-        case utils::OV_3D_VIDEO_ON_3D_PANEL:
-        case utils::OV_3D_VIDEO_ON_3D_TV:
-        case utils::OV_3D_VIDEO_ON_2D_PANEL_2D_TV:
-        case utils::OV_UI_MIRROR:
-        case utils::OV_2D_TRUE_UI_MIRROR:
-        case utils::OV_BYPASS_1_LAYER:
-        case utils::OV_BYPASS_2_LAYER:
-        case utils::OV_BYPASS_3_LAYER:
-            if(!mOv->dequeueBuffer(buf, dest)) {
+            if(!mOv->queueBuffer(fd, offset, dest)) {
                 ALOGE("Overlay %s failed", __FUNCTION__);
                 return false;
             }
@@ -194,7 +111,6 @@
     OVASSERT(mOv,
             "%s Overlay and Rotator should be init at this point",
             __FUNCTION__);
-    // FIXME that one needs to move to the state machine class
     utils::eOverlayState st = mState.state();
     switch (st) {
         case utils::OV_2D_VIDEO_ON_PANEL:
@@ -226,7 +142,6 @@
     OVASSERT(mOv,
             "%s Overlay and Rotator should be init at this point",
             __FUNCTION__);
-    // FIXME that one needs to move to the state machine class
     utils::eOverlayState st = mState.state();
     switch (st) {
         case utils::OV_2D_VIDEO_ON_PANEL:
@@ -257,7 +172,6 @@
     OVASSERT(mOv,
             "%s Overlay and Rotator should be init at this point",
             __FUNCTION__);
-    // FIXME that one needs to move to the state machine class
     utils::eOverlayState st = mState.state();
     switch (st) {
         case utils::OV_2D_VIDEO_ON_PANEL:
@@ -282,13 +196,13 @@
     }
     return true;
 }
-bool Overlay::setParameter(const utils::Params& param,
+
+bool Overlay::setTransform(const int orient,
         utils::eDest dest)
 {
-    OVASSERT(mOv,
-            "%s Overlay and Rotator should be init at this point",
-            __FUNCTION__);
-    // FIXME that one needs to move to the state machine class
+    utils::eTransform transform =
+            static_cast<utils::eTransform>(orient);
+
     utils::eOverlayState st = mState.state();
     switch (st) {
         case utils::OV_2D_VIDEO_ON_PANEL:
@@ -302,7 +216,7 @@
         case utils::OV_BYPASS_1_LAYER:
         case utils::OV_BYPASS_2_LAYER:
         case utils::OV_BYPASS_3_LAYER:
-            if(!mOv->setParameter(param, dest)) {
+            if(!mOv->setTransform(transform, dest)) {
                 ALOGE("Overlay %s failed", __FUNCTION__);
                 return false;
             }
@@ -313,25 +227,19 @@
     }
     return true;
 }
+
 bool Overlay::setSource(const utils::PipeArgs args[utils::MAX_PIPES],
         utils::eDest dest)
 {
-    // FIXME that one needs to move to the state machine class
     utils::PipeArgs margs[utils::MAX_PIPES] = {
         args[0], args[1], args[2] };
     utils::eOverlayState st = mState.state();
 
     switch (st) {
         case utils::OV_CLOSED:
-            // if we get setSource when we are closed, then
-            // we will assume tranistion to OV_2D_VIDEO_ON_PANEL
-            // returns overlay
-            mOv = mState.handle_closed(utils::OV_2D_VIDEO_ON_PANEL);
-            if (!mOv) {
-                ALOGE("Overlay %s failed", __FUNCTION__);
-                this->reset(); // cleanup
-                return false;
-            }
+            ALOGE("Overlay %s failed, state is OV_CLOSED, set state first",
+                    __FUNCTION__);
+            return false;
             break;
         case utils::OV_2D_VIDEO_ON_PANEL:
         case utils::OV_3D_VIDEO_ON_2D_PANEL:
@@ -339,25 +247,15 @@
         case utils::OV_BYPASS_1_LAYER:
         case utils::OV_BYPASS_2_LAYER:
         case utils::OV_BYPASS_3_LAYER:
-            // no tweaking
             break;
         case utils::OV_3D_VIDEO_ON_3D_PANEL:
         case utils::OV_3D_VIDEO_ON_3D_TV:
-            margs[utils::CHANNEL_1].zorder = utils::ZORDER_1;
-            break;
+            //TODO set zorder for channel 1 as 1 in 3D pipe
         case utils::OV_2D_VIDEO_ON_PANEL_TV:
         case utils::OV_3D_VIDEO_ON_2D_PANEL_2D_TV:
-            // If displaying on both, external VG pipe set to be no wait
-            margs[utils::CHANNEL_1].wait = utils::NO_WAIT;
             break;
         case utils::OV_2D_TRUE_UI_MIRROR:
-            // Set zorder -- external VG pipe (video) gets 0, RGB pipe (UI) gets 1
-            margs[utils::CHANNEL_1].zorder = utils::ZORDER_0;
-            margs[utils::CHANNEL_2].zorder = utils::ZORDER_1;
-            // External VG (video) and RGB (UI) pipe set to be no wait
-            margs[utils::CHANNEL_0].wait = utils::WAIT;
-            margs[utils::CHANNEL_1].wait = utils::NO_WAIT;
-            margs[utils::CHANNEL_2].wait = utils::NO_WAIT;
+            // TODO Set zorder, external VG pipe (video) gets 0, RGB pipe (UI) gets 1
             break;
         default:
             OVASSERT(false, "%s Unknown state %d", __FUNCTION__, st);
@@ -371,39 +269,12 @@
 
     return true;
 }
-void Overlay::setMemoryId(int id, utils::eDest dest)
-{
-    OVASSERT(mOv,
-            "%s Overlay and Rotator should be init at this point",
-            __FUNCTION__);
-    // FIXME that one needs to move to the state machine class
-    utils::eOverlayState st = mState.state();
-    switch (st) {
-        case utils::OV_2D_VIDEO_ON_PANEL:
-        case utils::OV_2D_VIDEO_ON_PANEL_TV:
-        case utils::OV_3D_VIDEO_ON_2D_PANEL:
-        case utils::OV_3D_VIDEO_ON_3D_PANEL:
-        case utils::OV_3D_VIDEO_ON_3D_TV:
-        case utils::OV_3D_VIDEO_ON_2D_PANEL_2D_TV:
-        case utils::OV_UI_MIRROR:
-        case utils::OV_2D_TRUE_UI_MIRROR:
-        case utils::OV_BYPASS_1_LAYER:
-        case utils::OV_BYPASS_2_LAYER:
-        case utils::OV_BYPASS_3_LAYER:
-            mOv->setMemoryId(id, dest);
-            break;
-        default:
-            OVASSERT(false, "setMemId Unknown state %d", st);
-    }
-}
-
 
 void Overlay::dump() const
 {
     OVASSERT(mOv,
             "%s Overlay and Rotator should be init at this point",
             __FUNCTION__);
-    // FIXME dump tate object, factory
     ALOGE("== Dump Overlay start ==");
     mState.dump();
     mOv->dump();
@@ -426,30 +297,4 @@
     return sInstance;
 }
 
-/****  NullPipe  ****/
-
-bool NullPipe::open(RotatorBase*) {
-    ALOGE_IF(DEBUG_OVERLAY, "NullPipe open");
-    return true;
-}
-bool NullPipe::close() { return true; }
-bool NullPipe::commit() { return true; }
-bool NullPipe::start(const utils::PipeArgs&) { return true; }
-bool NullPipe::setCrop(const utils::Dim&) { return true; }
-bool NullPipe::setPosition(const utils::Dim&) { return true; }
-bool NullPipe::setParameter(const utils::Params&) { return true; }
-bool NullPipe::setSource(const utils::PipeArgs&) { return true; }
-bool NullPipe::queueBuffer(uint32_t offset) { return true; }
-bool NullPipe::dequeueBuffer(void*&) { return true; }
-bool NullPipe::waitForVsync() { return true; }
-void NullPipe::setMemoryId(int) {}
-// NullPipe will return by val here as opposed to other Pipes.
-utils::PipeArgs NullPipe::getArgs() const { return utils::PipeArgs(); }
-utils::eOverlayPipeType NullPipe::getOvPipeType() const {
-    return utils::OV_PIPE_TYPE_NULL;
-}
-void NullPipe::dump() const {
-    ALOGE("== NullPipe (null) start/end ==");
-}
-
 } // overlay