overlay: Standardize init/close of internal components

Standardize the constructor/destructor init/close of internal
components.
Also internal components are allocated dynamically, with pointers
maintained in parent. This should facilitate gathering internals
from objects into arrays.

Change-Id: I8606fa436cf03fb75e43dc0f8341a05901751a11
diff --git a/liboverlay/overlayCtrlData.h b/liboverlay/overlayCtrlData.h
index 1b26b66..dbf328a 100644
--- a/liboverlay/overlayCtrlData.h
+++ b/liboverlay/overlayCtrlData.h
@@ -49,13 +49,9 @@
 public:
 
     /* ctor */
-    explicit Ctrl();
+    explicit Ctrl(const int& dpy);
     /* dtor close */
     ~Ctrl();
-    /* init fd etc*/
-    bool init(uint32_t dpy);
-    /* close underlying mdp */
-    bool close();
 
     /* set source using whf, orient and wait flag */
     void setSource(const utils::PipeArgs& args);
@@ -90,20 +86,16 @@
 
 private:
     // mdp ctrl struct(info e.g.)
-    MdpCtrl mMdp;
+    MdpCtrl *mMdp;
 };
 
 
 class Data : utils::NoCopy {
 public:
     /* init, reset */
-    explicit Data();
+    explicit Data(const int& dpy);
     /* calls close */
     ~Data();
-    /* init fd etc */
-    bool init(uint32_t dpy);
-    /* calls underlying mdp close */
-    bool close();
     /* set overlay pipe id in the mdp struct */
     void setPipeId(int id);
     /* get overlay id in the mdp struct */
@@ -117,72 +109,46 @@
 
 private:
     // mdp data struct
-    MdpData mMdp;
-};
-
-/* This class just creates a Ctrl Data pair to be used by a pipe.
- * Although this was legacy design, this separation still makes sense, since we
- * need to use the Ctrl channel in hwc_prepare (i.e config stage) and Data
- * channel in hwc_set (i.e draw stage)
- */
-struct CtrlData {
-    Ctrl ctrl;
-    Data data;
+    MdpData *mMdp;
 };
 
 //-------------Inlines-------------------------------
 
-inline Ctrl::Ctrl() {
-    mMdp.reset();
+inline Ctrl::Ctrl(const int& dpy) : mMdp(new MdpCtrl(dpy)) {
 }
 
 inline Ctrl::~Ctrl() {
-    close();
-}
-
-inline bool Ctrl::close() {
-    if(!mMdp.close())
-        return false;
-    return true;
-}
-
-inline bool Ctrl::init(uint32_t dpy) {
-    // MDP/FD init
-    if(!mMdp.init(dpy)) {
-        ALOGE("Ctrl failed to init dpy=%d", dpy);
-        return false;
-    }
-    return true;
+    delete mMdp;
 }
 
 inline void Ctrl::setSource(const utils::PipeArgs& args)
 {
-    mMdp.setSource(args);
+    mMdp->setSource(args);
 }
 
 inline void Ctrl::setPosition(const utils::Dim& dim)
 {
-    mMdp.setPosition(dim);
+    mMdp->setPosition(dim);
 }
 
 inline void Ctrl::setTransform(const utils::eTransform& orient)
 {
-    mMdp.setTransform(orient);
+    mMdp->setTransform(orient);
 }
 
 inline void Ctrl::setCrop(const utils::Dim& d)
 {
-    mMdp.setCrop(d);
+    mMdp->setCrop(d);
 }
 
 inline void Ctrl::setColor(const uint32_t color)
 {
-    mMdp.setColor(color);
+    mMdp->setColor(color);
 }
 
 inline bool Ctrl::setVisualParams(const MetaData_t &metadata)
 {
-    if (!mMdp.setVisualParams(metadata)) {
+    if (!mMdp->setVisualParams(metadata)) {
         ALOGE("Ctrl setVisualParams failed in MDP setVisualParams");
         return false;
     }
@@ -191,12 +157,12 @@
 
 inline void Ctrl::dump() const {
     ALOGE("== Dump Ctrl start ==");
-    mMdp.dump();
+    mMdp->dump();
     ALOGE("== Dump Ctrl end ==");
 }
 
 inline bool Ctrl::commit() {
-    if(!mMdp.set()) {
+    if(!mMdp->set()) {
         ALOGE("Ctrl commit failed set overlay");
         return false;
     }
@@ -204,71 +170,56 @@
 }
 
 inline int Ctrl::getPipeId() const {
-    return mMdp.getPipeId();
+    return mMdp->getPipeId();
 }
 
 inline int Ctrl::getFd() const {
-    return mMdp.getFd();
+    return mMdp->getFd();
 }
 
 inline void Ctrl::updateSrcFormat(const uint32_t& rotDstFmt) {
-    mMdp.updateSrcFormat(rotDstFmt);
+    mMdp->updateSrcFormat(rotDstFmt);
 }
 
 inline utils::Dim Ctrl::getCrop() const {
-    return mMdp.getSrcRectDim();
+    return mMdp->getSrcRectDim();
 }
 
 inline utils::Dim Ctrl::getPosition() const {
-    return mMdp.getDstRectDim();
+    return mMdp->getDstRectDim();
 }
 
 inline void Ctrl::setDownscale(int dscale_factor) {
-    mMdp.setDownscale(dscale_factor);
+    mMdp->setDownscale(dscale_factor);
 }
 
 inline void Ctrl::getDump(char *buf, size_t len) {
-    mMdp.getDump(buf, len);
+    mMdp->getDump(buf, len);
 }
 
-inline Data::Data() {
-    mMdp.reset();
+inline Data::Data(const int& dpy) : mMdp(new MdpData(dpy)) {
 }
 
-inline Data::~Data() { close(); }
-
-inline void Data::setPipeId(int id) { mMdp.setPipeId(id); }
-
-inline int Data::getPipeId() const { return mMdp.getPipeId(); }
-
-inline bool Data::init(uint32_t dpy) {
-    if(!mMdp.init(dpy)) {
-        ALOGE("Data cannot init mdp");
-        return false;
-    }
-    return true;
+inline Data::~Data() {
+    delete mMdp;
 }
 
-inline bool Data::close() {
-    if(!mMdp.close()) {
-        ALOGE("Data close failed");
-        return false;
-    }
-    return true;
-}
+inline void Data::setPipeId(int id) { mMdp->setPipeId(id); }
+
+inline int Data::getPipeId() const { return mMdp->getPipeId(); }
 
 inline bool Data::queueBuffer(int fd, uint32_t offset) {
-    return mMdp.play(fd, offset);
+    return mMdp->play(fd, offset);
 }
 
 inline void Data::dump() const {
     ALOGE("== Dump Data MDP start ==");
-    mMdp.dump();
+    mMdp->dump();
     ALOGE("== Dump Data MDP end ==");
 }
 
 inline void Data::getDump(char *buf, size_t len) {
-    mMdp.getDump(buf, len);
+    mMdp->getDump(buf, len);
 }
 
 } // overlay
diff --git a/liboverlay/overlayMdp.cpp b/liboverlay/overlayMdp.cpp
index 268480d..b278ce4 100644
--- a/liboverlay/overlayMdp.cpp
+++ b/liboverlay/overlayMdp.cpp
@@ -45,7 +45,7 @@
 namespace ovutils = overlay::utils;
 namespace overlay {
 
-bool MdpCtrl::init(uint32_t dpy) {
+bool MdpCtrl::init(const int& dpy) {
     int fbnum = Overlay::getFbForDpy(dpy);
     if( fbnum < 0 ) {
         ALOGE("%s: Invalid FB for the display: %d",__FUNCTION__, dpy);
@@ -390,7 +390,7 @@
 
 
 //// MdpData ////////////
-bool MdpData::init(uint32_t dpy) {
+bool MdpData::init(const int& dpy) {
     int fbnum = Overlay::getFbForDpy(dpy);
     if( fbnum < 0 ) {
         ALOGE("%s: Invalid FB for the display: %d",__FUNCTION__, dpy);
diff --git a/liboverlay/overlayMdp.h b/liboverlay/overlayMdp.h
index 8074724..daaeaf2 100644
--- a/liboverlay/overlayMdp.h
+++ b/liboverlay/overlayMdp.h
@@ -36,11 +36,11 @@
 class MdpCtrl {
 public:
     /* ctor reset */
-    explicit MdpCtrl();
+    explicit MdpCtrl(const int& dpy);
     /* dtor close */
     ~MdpCtrl();
     /* init underlying device using fbnum for dpy */
-    bool init(uint32_t dpy);
+    bool init(const int& dpy);
     /* unset overlay, reset and close fd */
     bool close();
     /* reset and set ov id to -1 / MSMFB_NEW_REQUEST */
@@ -160,11 +160,11 @@
 class MdpData {
 public:
     /* ctor reset data */
-    explicit MdpData();
+    explicit MdpData(const int& dpy);
     /* dtor close*/
     ~MdpData();
     /* init FD */
-    bool init(uint32_t dpy);
+    bool init(const int& dpy);
     /* memset0 the underlying mdp object */
     void reset();
     /* close fd, and reset */
@@ -196,8 +196,9 @@
 
 /////   MdpCtrl  //////
 
-inline MdpCtrl::MdpCtrl() {
+inline MdpCtrl::MdpCtrl(const int& dpy) {
     reset();
+    init(dpy);
 }
 
 inline MdpCtrl::~MdpCtrl() {
@@ -346,7 +347,10 @@
 
 ///////    MdpData   //////
 
-inline MdpData::MdpData() { reset(); }
+inline MdpData::MdpData(const int& dpy) {
+    reset();
+    init(dpy);
+}
 
 inline MdpData::~MdpData() { close(); }
 
diff --git a/liboverlay/pipes/overlayGenPipe.cpp b/liboverlay/pipes/overlayGenPipe.cpp
index e0b580b..394a56e 100644
--- a/liboverlay/pipes/overlayGenPipe.cpp
+++ b/liboverlay/pipes/overlayGenPipe.cpp
@@ -32,73 +32,40 @@
 
 namespace overlay {
 
-GenericPipe::GenericPipe(int dpy) : mDpy(dpy), mRotDownscaleOpt(false),
-    pipeState(CLOSED) {
-    init();
+GenericPipe::GenericPipe(const int& dpy) : mDpy(dpy), mRotDownscaleOpt(false),
+    pipeState(CLOSED), mCtrl(new Ctrl(dpy)), mData(new Data(dpy)) {
 }
 
 GenericPipe::~GenericPipe() {
-    close();
-}
-
-bool GenericPipe::init()
-{
-    ALOGE_IF(DEBUG_OVERLAY, "GenericPipe init");
-    mRotDownscaleOpt = false;
-
-    if(!mCtrlData.ctrl.init(mDpy)) {
-        ALOGE("GenericPipe failed to init ctrl");
-        return false;
-    }
-
-    if(!mCtrlData.data.init(mDpy)) {
-        ALOGE("GenericPipe failed to init data");
-        return false;
-    }
-
-    return true;
-}
-
-bool GenericPipe::close() {
-    bool ret = true;
-
-    if(!mCtrlData.ctrl.close()) {
-        ALOGE("GenericPipe failed to close ctrl");
-        ret = false;
-    }
-    if (!mCtrlData.data.close()) {
-        ALOGE("GenericPipe failed to close data");
-        ret = false;
-    }
-
+    delete mCtrl;
+    delete mData;
     setClosed();
-    return ret;
 }
 
 void GenericPipe::setSource(const utils::PipeArgs& args) {
     mRotDownscaleOpt = args.rotFlags & utils::ROT_DOWNSCALE_ENABLED;
-    mCtrlData.ctrl.setSource(args);
+    mCtrl->setSource(args);
 }
 
 void GenericPipe::setCrop(const overlay::utils::Dim& d) {
-    mCtrlData.ctrl.setCrop(d);
+    mCtrl->setCrop(d);
 }
 
 void GenericPipe::setColor(const uint32_t color) {
-    mCtrlData.ctrl.setColor(color);
+    mCtrl->setColor(color);
 }
 
 void GenericPipe::setTransform(const utils::eTransform& orient) {
-    mCtrlData.ctrl.setTransform(orient);
+    mCtrl->setTransform(orient);
 }
 
 void GenericPipe::setPosition(const utils::Dim& d) {
-    mCtrlData.ctrl.setPosition(d);
+    mCtrl->setPosition(d);
 }
 
 bool GenericPipe::setVisualParams(const MetaData_t &metadata)
 {
-        return mCtrlData.ctrl.setVisualParams(metadata);
+        return mCtrl->setVisualParams(metadata);
 }
 
 bool GenericPipe::commit() {
@@ -106,14 +73,14 @@
     int downscale_factor = utils::ROT_DS_NONE;
 
     if(mRotDownscaleOpt) {
-        ovutils::Dim src(mCtrlData.ctrl.getCrop());
-        ovutils::Dim dst(mCtrlData.ctrl.getPosition());
+        ovutils::Dim src(mCtrl->getCrop());
+        ovutils::Dim dst(mCtrl->getPosition());
         downscale_factor = ovutils::getDownscaleFactor(
                 src.w, src.h, dst.w, dst.h);
     }
 
-    mCtrlData.ctrl.setDownscale(downscale_factor);
-    ret = mCtrlData.ctrl.commit();
+    mCtrl->setDownscale(downscale_factor);
+    ret = mCtrl->commit();
 
     pipeState = ret ? OPEN : CLOSED;
     return ret;
@@ -122,36 +89,35 @@
 bool GenericPipe::queueBuffer(int fd, uint32_t offset) {
     //TODO Move pipe-id transfer to CtrlData class. Make ctrl and data private.
     OVASSERT(isOpen(), "State is closed, cannot queueBuffer");
-    int pipeId = mCtrlData.ctrl.getPipeId();
+    int pipeId = mCtrl->getPipeId();
     OVASSERT(-1 != pipeId, "Ctrl ID should not be -1");
     // set pipe id from ctrl to data
-    mCtrlData.data.setPipeId(pipeId);
+    mData->setPipeId(pipeId);
 
-    return mCtrlData.data.queueBuffer(fd, offset);
+    return mData->queueBuffer(fd, offset);
 }
 
 int GenericPipe::getCtrlFd() const {
-    return mCtrlData.ctrl.getFd();
+    return mCtrl->getFd();
 }
 
 utils::Dim GenericPipe::getCrop() const
 {
-    return mCtrlData.ctrl.getCrop();
+    return mCtrl->getCrop();
 }
 
 void GenericPipe::dump() const
 {
     ALOGE("== Dump Generic pipe start ==");
     ALOGE("pipe state = %d", (int)pipeState);
-    mCtrlData.ctrl.dump();
-    mCtrlData.data.dump();
-
+    mCtrl->dump();
+    mData->dump();
     ALOGE("== Dump Generic pipe end ==");
 }
 
 void GenericPipe::getDump(char *buf, size_t len) {
-    mCtrlData.ctrl.getDump(buf, len);
-    mCtrlData.data.getDump(buf, len);
+    mCtrl->getDump(buf, len);
+    mData->getDump(buf, len);
 }
 
 bool GenericPipe::isClosed() const  {
@@ -168,7 +134,7 @@
 }
 
 int GenericPipe::getPipeId() {
-    return mCtrlData.ctrl.getPipeId();
+    return mCtrl->getPipeId();
 }
 
 } //namespace overlay
diff --git a/liboverlay/pipes/overlayGenPipe.h b/liboverlay/pipes/overlayGenPipe.h
index ecdd001..57e1758 100644
--- a/liboverlay/pipes/overlayGenPipe.h
+++ b/liboverlay/pipes/overlayGenPipe.h
@@ -38,11 +38,9 @@
 class GenericPipe : utils::NoCopy {
 public:
     /* ctor */
-    explicit GenericPipe(int dpy);
+    explicit GenericPipe(const int& dpy);
     /* dtor */
     ~GenericPipe();
-    bool init();
-    bool close();
     /* Control APIs */
     /* set source using whf, orient and wait flag */
     void setSource(const utils::PipeArgs& args);
@@ -82,8 +80,6 @@
     bool setClosed();
 
     int mDpy;
-    /* Ctrl/Data aggregator */
-    CtrlData mCtrlData;
     //Whether we will do downscale opt. This is just a request. If the frame is
     //not a candidate, we might not do it.
     bool mRotDownscaleOpt;
@@ -93,6 +89,8 @@
         OPEN
     };
     ePipeState pipeState;
+    Ctrl *mCtrl;
+    Data *mData;
 };
 
 } //namespace overlay