overlay: rotator: Remove previous allocation for a session

When ION was not ref counted, we needed to maintain the previous
session memory until the new one was taken by MDP. ION being
ref counted now, it is not necessary to maintain previous memory
allocation.

Change-Id: I102df065bb288a661d509912f4c7b894505df393
diff --git a/liboverlay/overlayMdpRot.cpp b/liboverlay/overlayMdpRot.cpp
index 3e549b1..38b0a92 100755
--- a/liboverlay/overlayMdpRot.cpp
+++ b/liboverlay/overlayMdpRot.cpp
@@ -156,7 +156,7 @@
 
     mRotDataInfo.dst.memory_id = mem.getFD();
     mRotDataInfo.dst.offset = 0;
-    mMem.curr().m = mem;
+    mMem.mem = mem;
     return true;
 }
 
@@ -184,23 +184,27 @@
 bool MdpRot::remap(uint32_t numbufs) {
     // if current size changed, remap
     uint32_t opBufSize = calcOutputBufSize();
-    if(opBufSize == mMem.curr().size()) {
+    if(opBufSize == mMem.size()) {
         ALOGE_IF(DEBUG_OVERLAY, "%s: same size %d", __FUNCTION__, opBufSize);
         return true;
     }
 
-    ALOGE_IF(DEBUG_OVERLAY, "%s: size changed - remapping", __FUNCTION__);
-    OVASSERT(!mMem.prev().valid(), "Prev should not be valid");
+    if(!mMem.close()) {
+        ALOGE("%s error in closing prev rot mem", __FUNCTION__);
+        return false;
+    }
 
-    // ++mMem will make curr to be prev, and prev will be curr
-    ++mMem;
+    ALOGE_IF(DEBUG_OVERLAY, "%s: size changed - remapping", __FUNCTION__);
+
     if(!open_i(numbufs, opBufSize)) {
         ALOGE("%s Error could not open", __FUNCTION__);
         return false;
     }
+
     for (uint32_t i = 0; i < numbufs; ++i) {
-        mMem.curr().mRotOffset[i] = i * opBufSize;
+        mMem.mRotOffset[i] = i * opBufSize;
     }
+
     return true;
 }
 
@@ -208,10 +212,8 @@
     ovutils::memset0(mRotImgInfo);
     ovutils::memset0(mLSRotImgInfo);
     ovutils::memset0(mRotDataInfo);
-    ovutils::memset0(mMem.curr().mRotOffset);
-    ovutils::memset0(mMem.prev().mRotOffset);
-    mMem.curr().mCurrOffset = 0;
-    mMem.prev().mCurrOffset = 0;
+    ovutils::memset0(mMem.mRotOffset);
+    mMem.mCurrIndex = 0;
     mOrientation = utils::OVERLAY_TRANSFORM_0;
 }
 
@@ -220,30 +222,21 @@
         mRotDataInfo.src.memory_id = fd;
         mRotDataInfo.src.offset = offset;
 
-        remap(RotMem::Mem::ROT_NUM_BUFS);
-        OVASSERT(mMem.curr().m.numBufs(),
-                "queueBuffer numbufs is 0");
+        if(false == remap(RotMem::ROT_NUM_BUFS)) {
+            ALOGE("%s Remap failed, not queueing", __FUNCTION__);
+            return false;
+        }
+
         mRotDataInfo.dst.offset =
-                mMem.curr().mRotOffset[mMem.curr().mCurrOffset];
-        mMem.curr().mCurrOffset =
-                (mMem.curr().mCurrOffset + 1) % mMem.curr().m.numBufs();
+                mMem.mRotOffset[mMem.mCurrIndex];
+        mMem.mCurrIndex =
+                (mMem.mCurrIndex + 1) % mMem.mem.numBufs();
 
         if(!overlay::mdp_wrapper::rotate(mFd.getFD(), mRotDataInfo)) {
             ALOGE("MdpRot failed rotate");
             dump();
             return false;
         }
-
-        // if the prev mem is valid, we need to close
-        if(mMem.prev().valid()) {
-            // FIXME if no wait for vsync the above
-            // play will return immediatly and might cause
-            // tearing when prev.close is called.
-            if(!mMem.prev().close()) {
-                ALOGE("%s error in closing prev rot mem", __FUNCTION__);
-                return false;
-            }
-        }
     }
     return true;
 }
@@ -251,7 +244,7 @@
 void MdpRot::dump() const {
     ALOGE("== Dump MdpRot start ==");
     mFd.dump();
-    mMem.curr().m.dump();
+    mMem.mem.dump();
     mdp_wrapper::dump("mRotImgInfo", mRotImgInfo);
     mdp_wrapper::dump("mRotDataInfo", mRotDataInfo);
     ALOGE("== Dump MdpRot end ==");
diff --git a/liboverlay/overlayMdssRot.cpp b/liboverlay/overlayMdssRot.cpp
index 083e507..ce9d9d5 100644
--- a/liboverlay/overlayMdssRot.cpp
+++ b/liboverlay/overlayMdssRot.cpp
@@ -134,30 +134,21 @@
         mRotData.data.memory_id = fd;
         mRotData.data.offset = offset;
 
-        remap(RotMem::Mem::ROT_NUM_BUFS);
-        OVASSERT(mMem.curr().m.numBufs(), "queueBuffer numbufs is 0");
+        if(false == remap(RotMem::ROT_NUM_BUFS)) {
+            ALOGE("%s Remap failed, not queuing", __FUNCTION__);
+            return false;
+        }
 
         mRotData.dst_data.offset =
-                mMem.curr().mRotOffset[mMem.curr().mCurrOffset];
-        mMem.curr().mCurrOffset =
-                (mMem.curr().mCurrOffset + 1) % mMem.curr().m.numBufs();
+                mMem.mRotOffset[mMem.mCurrIndex];
+        mMem.mCurrIndex =
+                (mMem.mCurrIndex + 1) % mMem.mem.numBufs();
 
         if(!overlay::mdp_wrapper::play(mFd.getFD(), mRotData)) {
             ALOGE("MdssRot play failed!");
             dump();
             return false;
         }
-
-        // if the prev mem is valid, we need to close
-        if(mMem.prev().valid()) {
-            // FIXME if no wait for vsync the above
-            // play will return immediatly and might cause
-            // tearing when prev.close is called.
-            if(!mMem.prev().close()) {
-                ALOGE("%s error in closing prev rot mem", __FUNCTION__);
-                return false;
-            }
-        }
     }
     return true;
 }
@@ -179,7 +170,7 @@
 
     mRotData.dst_data.memory_id = mem.getFD();
     mRotData.dst_data.offset = 0;
-    mMem.curr().m = mem;
+    mMem.mem = mem;
     return true;
 }
 
@@ -187,23 +178,27 @@
     // Calculate the size based on rotator's dst format, w and h.
     uint32_t opBufSize = calcOutputBufSize();
     // If current size changed, remap
-    if(opBufSize == mMem.curr().size()) {
+    if(opBufSize == mMem.size()) {
         ALOGE_IF(DEBUG_OVERLAY, "%s: same size %d", __FUNCTION__, opBufSize);
         return true;
     }
 
     ALOGE_IF(DEBUG_OVERLAY, "%s: size changed - remapping", __FUNCTION__);
-    OVASSERT(!mMem.prev().valid(), "Prev should not be valid");
 
-    // ++mMem will make curr to be prev, and prev will be curr
-    ++mMem;
+    if(!mMem.close()) {
+        ALOGE("%s error in closing prev rot mem", __FUNCTION__);
+        return false;
+    }
+
     if(!open_i(numbufs, opBufSize)) {
         ALOGE("%s Error could not open", __FUNCTION__);
         return false;
     }
+
     for (uint32_t i = 0; i < numbufs; ++i) {
-        mMem.curr().mRotOffset[i] = i * opBufSize;
+        mMem.mRotOffset[i] = i * opBufSize;
     }
+
     return true;
 }
 
@@ -234,17 +229,15 @@
     ovutils::memset0(mRotData);
     mRotData.data.memory_id = -1;
     mRotInfo.id = MSMFB_NEW_REQUEST;
-    ovutils::memset0(mMem.curr().mRotOffset);
-    ovutils::memset0(mMem.prev().mRotOffset);
-    mMem.curr().mCurrOffset = 0;
-    mMem.prev().mCurrOffset = 0;
+    ovutils::memset0(mMem.mRotOffset);
+    mMem.mCurrIndex = 0;
     mOrientation = utils::OVERLAY_TRANSFORM_0;
 }
 
 void MdssRot::dump() const {
     ALOGE("== Dump MdssRot start ==");
     mFd.dump();
-    mMem.curr().m.dump();
+    mMem.mem.dump();
     mdp_wrapper::dump("mRotInfo", mRotInfo);
     mdp_wrapper::dump("mRotData", mRotData);
     ALOGE("== Dump MdssRot end ==");
diff --git a/liboverlay/overlayRotator.cpp b/liboverlay/overlayRotator.cpp
index d912dc7..336d434 100644
--- a/liboverlay/overlayRotator.cpp
+++ b/liboverlay/overlayRotator.cpp
@@ -63,48 +63,45 @@
 
 bool RotMem::close() {
     bool ret = true;
-    for(uint32_t i=0; i < RotMem::MAX_ROT_MEM; ++i) {
-        // skip current, and if valid, close
-        if(m[i].valid()) {
-            if(m[i].close() == false) {
-                ALOGE("%s error in closing rot mem %d", __FUNCTION__, i);
-                ret = false;
-            }
+    if(valid()) {
+        if(mem.close() == false) {
+            ALOGE("%s error in closing rot mem", __FUNCTION__);
+            ret = false;
         }
     }
     return ret;
 }
 
-RotMem::Mem::Mem() : mCurrOffset(0) {
+RotMem::RotMem() : mCurrIndex(0) {
     utils::memset0(mRotOffset);
     for(int i = 0; i < ROT_NUM_BUFS; i++) {
         mRelFence[i] = -1;
     }
 }
 
-RotMem::Mem::~Mem() {
+RotMem::~RotMem() {
     for(int i = 0; i < ROT_NUM_BUFS; i++) {
         ::close(mRelFence[i]);
         mRelFence[i] = -1;
     }
 }
 
-void RotMem::Mem::setReleaseFd(const int& fence) {
+void RotMem::setReleaseFd(const int& fence) {
     int ret = 0;
 
-    if(mRelFence[mCurrOffset] >= 0) {
+    if(mRelFence[mCurrIndex] >= 0) {
         //Wait for previous usage of this buffer to be over.
         //Can happen if rotation takes > vsync and a fast producer. i.e queue
         //happens in subsequent vsyncs either because content is 60fps or
         //because the producer is hasty sometimes.
-        ret = sync_wait(mRelFence[mCurrOffset], 1000);
+        ret = sync_wait(mRelFence[mCurrIndex], 1000);
         if(ret < 0) {
             ALOGE("%s: sync_wait error!! error no = %d err str = %s",
                 __FUNCTION__, errno, strerror(errno));
         }
-        ::close(mRelFence[mCurrOffset]);
+        ::close(mRelFence[mCurrIndex]);
     }
-    mRelFence[mCurrOffset] = fence;
+    mRelFence[mCurrIndex] = fence;
 }
 
 //============RotMgr=========================
diff --git a/liboverlay/overlayRotator.h b/liboverlay/overlayRotator.h
index 2bbfb91..be0c8bd 100644
--- a/liboverlay/overlayRotator.h
+++ b/liboverlay/overlayRotator.h
@@ -45,36 +45,21 @@
    we don't need this RotMem wrapper. The inner class is sufficient.
 */
 struct RotMem {
-    // Max rotator memory allocations
-    enum { MAX_ROT_MEM = 2};
-
-    //Manages the rotator buffer offsets.
-    struct Mem {
-        Mem();
-        ~Mem();
-        bool valid() { return m.valid(); }
-        bool close() { return m.close(); }
-        uint32_t size() const { return m.bufSz(); }
-        void setReleaseFd(const int& fence);
-        // Max rotator buffers
-        enum { ROT_NUM_BUFS = 2 };
-        // rotator data info dst offset
-        uint32_t mRotOffset[ROT_NUM_BUFS];
-        int mRelFence[ROT_NUM_BUFS];
-        // current offset slot from mRotOffset
-        uint32_t mCurrOffset;
-        OvMem m;
-    };
-
-    RotMem() : _curr(0) {}
-    Mem& curr() { return m[_curr % MAX_ROT_MEM]; }
-    const Mem& curr() const { return m[_curr % MAX_ROT_MEM]; }
-    Mem& prev() { return m[(_curr+1) % MAX_ROT_MEM]; }
-    RotMem& operator++() { ++_curr; return *this; }
-    void setReleaseFd(const int& fence) { curr().setReleaseFd(fence); }
+    // Max rotator buffers
+    enum { ROT_NUM_BUFS = 2 };
+    RotMem();
+    ~RotMem();
     bool close();
-    uint32_t _curr;
-    Mem m[MAX_ROT_MEM];
+    bool valid() { return mem.valid(); }
+    uint32_t size() const { return mem.bufSz(); }
+    void setReleaseFd(const int& fence);
+
+    // rotator data info dst offset
+    uint32_t mRotOffset[ROT_NUM_BUFS];
+    int mRelFence[ROT_NUM_BUFS];
+    // current slot being used
+    uint32_t mCurrIndex;
+    OvMem mem;
 };
 
 class Rotator
diff --git a/liboverlay/overlayUtils.h b/liboverlay/overlayUtils.h
index 24fba33..959dadf 100644
--- a/liboverlay/overlayUtils.h
+++ b/liboverlay/overlayUtils.h
@@ -424,7 +424,7 @@
 const char* getFormatString(int format);
 
 template <class T>
-inline void memset0(T& t) { ::memset(&t, 0, sizeof(T)); }
+inline void memset0(T& t) { ::memset(&t, 0, sizeof(t)); }
 
 template <class T> inline void swap ( T& a, T& b )
 {