hwc/overlay: Video on 4k2k external, 4k2k rotation.

-Add support for Video via overlay on 4k2k external panel.
-Add support for rotating videos on 4k2k panels. We use
pre-rotation in hwc to rotate a video into a single buffer,
irrespective of panel size. Then this buffer is fed to MDP.
Rotator objects are managed by the new RotMgr.
-Cleaup mdpcomp and overlay.

Change-Id: Ifb08534747e8e18b6c58dd8a3e1a9947409100f1
diff --git a/liboverlay/Android.mk b/liboverlay/Android.mk
index 82c6610..ed2f503 100644
--- a/liboverlay/Android.mk
+++ b/liboverlay/Android.mk
@@ -11,7 +11,6 @@
 LOCAL_ADDITIONAL_DEPENDENCIES := $(common_deps)
 LOCAL_SRC_FILES := \
       overlay.cpp \
-      overlayCtrl.cpp \
       overlayUtils.cpp \
       overlayMdp.cpp \
       overlayRotator.cpp \
diff --git a/liboverlay/overlayCtrl.cpp b/liboverlay/overlayCtrl.cpp
deleted file mode 100644
index 2cbf1c5..0000000
--- a/liboverlay/overlayCtrl.cpp
+++ /dev/null
@@ -1,91 +0,0 @@
-/*
-* Copyright (C) 2008 The Android Open Source Project
-* Copyright (c) 2010-2012, The Linux Foundation. All rights reserved.
-*
-* Licensed under the Apache License, Version 2.0 (the "License");
-* you may not use this file except in compliance with the License.
-* You may obtain a copy of the License at
-*
-*      http://www.apache.org/licenses/LICENSE-2.0
-*
-* Unless required by applicable law or agreed to in writing, software
-* distributed under the License is distributed on an "AS IS" BASIS,
-* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-* See the License for the specific language governing permissions and
-* limitations under the License.
-*/
-
-#include <cutils/properties.h>
-#include "overlayCtrlData.h"
-#include "fb_priv.h"
-#include "gralloc_priv.h" //for interlace
-
-namespace overlay{
-
-bool Ctrl::init(uint32_t fbnum) {
-    // MDP/FD init
-    if(!mMdp.init(fbnum)) {
-        ALOGE("Ctrl failed to init fbnum=%d", fbnum);
-        return false;
-    }
-
-    if(!getScreenInfo(mInfo)) {
-        ALOGE("Ctrl failed to getScreenInfo");
-        return false;
-    }
-
-    return true;
-}
-
-bool Ctrl::setSource(const utils::PipeArgs& args)
-{
-    return mMdp.setSource(args);
-}
-
-bool Ctrl::setPosition(const utils::Dim& dim)
-{
-    if(!dim.check(mInfo.mFBWidth, mInfo.mFBHeight)) {
-        ALOGE("Ctrl setPosition error in dim");
-        dim.dump();
-        return false;
-    }
-
-    if(!mMdp.setPosition(dim, mInfo.mFBWidth, mInfo.mFBHeight)) {
-        ALOGE("Ctrl failed MDP setPosition");
-        return false;
-    }
-    return true;
-}
-
-bool Ctrl::setTransform(const utils::eTransform& orient)
-{
-    if(!mMdp.setTransform(orient)) {
-        ALOGE("Ctrl setTransform failed for Mdp");
-        return false;
-    }
-    return true;
-}
-
-void Ctrl::setRotatorUsed(const bool& rotUsed) {
-    mMdp.setRotatorUsed(rotUsed);
-}
-
-bool Ctrl::setCrop(const utils::Dim& d)
-{
-    if(!mMdp.setCrop(d)) {
-        ALOGE("Data setCrop failed in MDP setCrop");
-        return false;
-    }
-    return true;
-}
-
-utils::FrameBufferInfo* utils::FrameBufferInfo::sFBInfoInstance = 0;
-
-void Ctrl::dump() const {
-    ALOGE("== Dump Ctrl start ==");
-    mInfo.dump("mInfo");
-    mMdp.dump();
-    ALOGE("== Dump Ctrl end ==");
-}
-
-} // overlay
diff --git a/liboverlay/overlayCtrlData.h b/liboverlay/overlayCtrlData.h
index c9a4949..5395834 100644
--- a/liboverlay/overlayCtrlData.h
+++ b/liboverlay/overlayCtrlData.h
@@ -58,15 +58,13 @@
     bool close();
 
     /* set source using whf, orient and wait flag */
-    bool setSource(const utils::PipeArgs& args);
+    void setSource(const utils::PipeArgs& args);
     /* set crop info and pass it down to mdp */
-    bool setCrop(const utils::Dim& d);
+    void setCrop(const utils::Dim& d);
     /* set orientation */
-    bool setTransform(const utils::eTransform& p);
-    /* set whether rotator can be used */
-    void setRotatorUsed(const bool& rotUsed);
+    void setTransform(const utils::eTransform& p);
     /* set mdp position using dim */
-    bool setPosition(const utils::Dim& dim);
+    void setPosition(const utils::Dim& dim);
     /* mdp set overlay/commit changes */
     bool commit();
 
@@ -74,40 +72,21 @@
     int  getPipeId() const;
     /* ctrl fd */
     int  getFd() const;
-
-    /* access for screen info */
-    utils::ScreenInfo getScreenInfo() const;
-
-    /* retrieve cached crop data */
+    /* retrieve crop data */
     utils::Dim getCrop() const;
-
-    /* Perform transformation calculations */
-    void doTransform();
-
-    /* Performs downscale calculations */
-    void doDownscale(int dscale_factor);
-
-    /* Get downscale factor */
-    int getDownscalefactor();
-
-    /* Update the src format */
-    void updateSrcformat(const uint32_t& inputsrcFormat);
-
+    utils::Dim getPosition() const;
+    /* Set downscale */
+    void setDownscale(int dscale_factor);
+    /* Update the src format based on rotator's dest */
+    void updateSrcFormat(const uint32_t& rotDstFormat);
     /* dump the state of the object */
     void dump() const;
-
     /* Return the dump in the specified buffer */
     void getDump(char *buf, size_t len);
 
 private:
-    /* Retrieve screen info from underlying mdp */
-    bool getScreenInfo(utils::ScreenInfo& info);
-
     // mdp ctrl struct(info e.g.)
     MdpCtrl mMdp;
-
-    /* Screen info */
-    utils::ScreenInfo mInfo;
 };
 
 
@@ -115,28 +94,20 @@
 public:
     /* init, reset */
     explicit Data();
-
     /* calls close */
     ~Data();
-
     /* init fd etc */
     bool init(uint32_t fbnum);
-
     /* 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 */
     int getPipeId() const;
-
     /* queue buffer to the overlay */
     bool queueBuffer(int fd, uint32_t offset);
-
     /* sump the state of the obj */
     void dump() const;
-
     /* Return the dump in the specified buffer */
     void getDump(char *buf, size_t len);
 
@@ -171,17 +142,44 @@
     return true;
 }
 
-inline bool Ctrl::commit() {
-    if(!mMdp.set()) {
-        ALOGE("Ctrl commit failed set overlay");
+inline bool Ctrl::init(uint32_t fbnum) {
+    // MDP/FD init
+    if(!mMdp.init(fbnum)) {
+        ALOGE("Ctrl failed to init fbnum=%d", fbnum);
         return false;
     }
     return true;
 }
 
-inline bool Ctrl::getScreenInfo(utils::ScreenInfo& info) {
-    if(!mMdp.getScreenInfo(info)){
-        ALOGE("Ctrl failed to get screen info");
+inline void Ctrl::setSource(const utils::PipeArgs& args)
+{
+    mMdp.setSource(args);
+}
+
+inline void Ctrl::setPosition(const utils::Dim& dim)
+{
+    mMdp.setPosition(dim);
+}
+
+inline void Ctrl::setTransform(const utils::eTransform& orient)
+{
+    mMdp.setTransform(orient);
+}
+
+inline void Ctrl::setCrop(const utils::Dim& d)
+{
+    mMdp.setCrop(d);
+}
+
+inline void Ctrl::dump() const {
+    ALOGE("== Dump Ctrl start ==");
+    mMdp.dump();
+    ALOGE("== Dump Ctrl end ==");
+}
+
+inline bool Ctrl::commit() {
+    if(!mMdp.set()) {
+        ALOGE("Ctrl commit failed set overlay");
         return false;
     }
     return true;
@@ -195,28 +193,20 @@
     return mMdp.getFd();
 }
 
-inline void Ctrl::updateSrcformat(const uint32_t& inputsrcFormat) {
-    mMdp.updateSrcformat(inputsrcFormat);
-}
-
-inline utils::ScreenInfo Ctrl::getScreenInfo() const {
-    return mInfo;
+inline void Ctrl::updateSrcFormat(const uint32_t& rotDstFmt) {
+    mMdp.updateSrcFormat(rotDstFmt);
 }
 
 inline utils::Dim Ctrl::getCrop() const {
     return mMdp.getSrcRectDim();
 }
 
-inline void Ctrl::doTransform() {
-    return mMdp.doTransform();
+inline utils::Dim Ctrl::getPosition() const {
+    return mMdp.getDstRectDim();
 }
 
-inline void Ctrl::doDownscale(int dscale_factor) {
-    mMdp.doDownscale(dscale_factor);
-}
-
-inline int Ctrl::getDownscalefactor() {
-    return mMdp.getDownscalefactor();
+inline void Ctrl::setDownscale(int dscale_factor) {
+    mMdp.setDownscale(dscale_factor);
 }
 
 inline void Ctrl::getDump(char *buf, size_t len) {
diff --git a/liboverlay/overlayMdp.cpp b/liboverlay/overlayMdp.cpp
index 8f2e2b6..d96066b 100644
--- a/liboverlay/overlayMdp.cpp
+++ b/liboverlay/overlayMdp.cpp
@@ -52,12 +52,11 @@
     mOVInfo.id = MSMFB_NEW_REQUEST;
     mLkgo.id = MSMFB_NEW_REQUEST;
     mOrientation = utils::OVERLAY_TRANSFORM_0;
-    mRotUsed = false;
+    mDownscale = 0;
 }
 
 bool MdpCtrl::close() {
     bool result = true;
-
     if(MSMFB_NEW_REQUEST != static_cast<int>(mOVInfo.id)) {
         if(!mdp_wrapper::unsetOverlay(mFd.getFD(), mOVInfo.id)) {
             ALOGE("MdpCtrl close error in unset");
@@ -66,6 +65,7 @@
     }
 
     reset();
+
     if(!mFd.close()) {
         result = false;
     }
@@ -73,8 +73,7 @@
     return result;
 }
 
-bool MdpCtrl::setSource(const utils::PipeArgs& args) {
-
+void MdpCtrl::setSource(const utils::PipeArgs& args) {
     setSrcWhf(args.whf);
 
     //TODO These are hardcoded. Can be moved out of setSource.
@@ -85,111 +84,44 @@
     setFlags(args.mdpFlags);
     setZ(args.zorder);
     setIsFg(args.isFg);
-    return true;
 }
 
-bool MdpCtrl::setCrop(const utils::Dim& d) {
+void MdpCtrl::setCrop(const utils::Dim& d) {
     setSrcRectDim(d);
-    return true;
 }
 
-bool MdpCtrl::setPosition(const overlay::utils::Dim& d,
-        int fbw, int fbh)
-{
-    ovutils::Dim dim(d);
-    ovutils::Dim ovsrcdim = getSrcRectDim();
-    // Scaling of upto a max of 20 times supported
-    if(dim.w >(ovsrcdim.w * ovutils::HW_OV_MAGNIFICATION_LIMIT)){
-        dim.w = ovutils::HW_OV_MAGNIFICATION_LIMIT * ovsrcdim.w;
-        dim.x = (fbw - dim.w) / 2;
-    }
-    if(dim.h >(ovsrcdim.h * ovutils::HW_OV_MAGNIFICATION_LIMIT)) {
-        dim.h = ovutils::HW_OV_MAGNIFICATION_LIMIT * ovsrcdim.h;
-        dim.y = (fbh - dim.h) / 2;
-    }
-
-    setDstRectDim(dim);
-    return true;
+void MdpCtrl::setPosition(const overlay::utils::Dim& d) {
+    setDstRectDim(d);
 }
 
-bool MdpCtrl::setTransform(const utils::eTransform& orient) {
+void MdpCtrl::setTransform(const utils::eTransform& orient) {
     int rot = utils::getMdpOrient(orient);
     setUserData(rot);
     //getMdpOrient will switch the flips if the source is 90 rotated.
     //Clients in Android dont factor in 90 rotation while deciding the flip.
     mOrientation = static_cast<utils::eTransform>(rot);
-
-    return true;
-}
-
-void MdpCtrl::setRotatorUsed(const bool& rotUsed) {
-    //rotUsed dictates whether rotator hardware can be used.
-    //It is set if transformation or downscaling is required.
-    mRotUsed = rotUsed;
 }
 
 void MdpCtrl::doTransform() {
-    adjustSrcWhf(mRotUsed);
     setRotationFlags();
-    //180 will be H + V
-    //270 will be H + V + 90
-    if(mOrientation & utils::OVERLAY_TRANSFORM_FLIP_H) {
-            overlayTransFlipH();
-    }
-    if(mOrientation & utils::OVERLAY_TRANSFORM_FLIP_V) {
-            overlayTransFlipV();
-    }
-    if(mOrientation & utils::OVERLAY_TRANSFORM_ROT_90) {
-            overlayTransRot90();
-    }
+    utils::Whf whf = getSrcWhf();
+    utils::Dim dim = getSrcRectDim();
+    utils::preRotateSource(mOrientation, whf, dim);
+    setSrcWhf(whf);
+    setSrcRectDim(dim);
 }
 
-int MdpCtrl::getDownscalefactor() {
-    int dscale_factor = utils::ROT_DS_NONE;
-    int src_w = mOVInfo.src_rect.w;
-    int src_h = mOVInfo.src_rect.h;
-    int dst_w = mOVInfo.dst_rect.w;
-    int dst_h = mOVInfo.dst_rect.h;
-    // We need this check to engage the rotator whenever possible to assist MDP
-    // in performing video downscale.
-    // This saves bandwidth and avoids causing the driver to make too many panel
-    // -mode switches between BLT (writeback) and non-BLT (Direct) modes.
-    // Use-case: Video playback [with downscaling and rotation].
-
-    if (dst_w && dst_h)
-    {
-        uint32_t dscale = (src_w * src_h) / (dst_w * dst_h);
-
-        if(dscale < 2) {
-            // Down-scale to > 50% of orig.
-            dscale_factor = utils::ROT_DS_NONE;
-        } else if(dscale < 4) {
-            // Down-scale to between > 25% to <= 50% of orig.
-            dscale_factor = utils::ROT_DS_HALF;
-        } else if(dscale < 8) {
-            // Down-scale to between > 12.5% to <= 25% of orig.
-            dscale_factor = utils::ROT_DS_FOURTH;
-        } else {
-            // Down-scale to <= 12.5% of orig.
-            dscale_factor = utils::ROT_DS_EIGHTH;
-        }
-    }
-
-    return dscale_factor;
-}
-
-void MdpCtrl::doDownscale(int dscale_factor) {
-
-    if( dscale_factor ) {
-        mOVInfo.src_rect.x >>= dscale_factor;
-        mOVInfo.src_rect.y >>= dscale_factor;
-        mOVInfo.src_rect.w >>= dscale_factor;
-        mOVInfo.src_rect.h >>= dscale_factor;
-    }
+void MdpCtrl::doDownscale() {
+    mOVInfo.src_rect.x >>= mDownscale;
+    mOVInfo.src_rect.y >>= mDownscale;
+    mOVInfo.src_rect.w >>= mDownscale;
+    mOVInfo.src_rect.h >>= mDownscale;
 }
 
 bool MdpCtrl::set() {
     //deferred calcs, so APIs could be called in any order.
+    doTransform();
+    doDownscale();
     utils::Whf whf = getSrcWhf();
     if(utils::isYuv(whf.format)) {
         normalizeCrop(mOVInfo.src_rect.x, mOVInfo.src_rect.w);
@@ -213,23 +145,6 @@
     return true;
 }
 
-bool MdpCtrl::getScreenInfo(overlay::utils::ScreenInfo& info) {
-    fb_fix_screeninfo finfo;
-    if (!mdp_wrapper::getFScreenInfo(mFd.getFD(), finfo)) {
-        return false;
-    }
-
-    fb_var_screeninfo vinfo;
-    if (!mdp_wrapper::getVScreenInfo(mFd.getFD(), vinfo)) {
-        return false;
-    }
-    info.mFBWidth   = vinfo.xres;
-    info.mFBHeight  = vinfo.yres;
-    info.mFBbpp     = vinfo.bits_per_pixel;
-    info.mFBystride = finfo.line_length;
-    return true;
-}
-
 bool MdpCtrl::get() {
     mdp_overlay ov;
     ov.id = mOVInfo.id;
@@ -241,24 +156,10 @@
     return true;
 }
 
-//Adjust width, height if rotator is used post transform calcs.
-//At this point the format is already updated by updateSrcFormat
-void MdpCtrl::adjustSrcWhf(const bool& rotUsed) {
-    if(rotUsed) {
-        utils::Whf whf = getSrcWhf();
-        if(whf.format == MDP_Y_CRCB_H2V2_TILE ||
-                whf.format == MDP_Y_CBCR_H2V2_TILE) {
-            whf.w = utils::alignup(whf.w, 64);
-            whf.h = utils::alignup(whf.h, 32);
-        }
-        setSrcWhf(whf);
-    }
-}
-
-//Update src format if rotator used based on rotator's destination format.
-void MdpCtrl::updateSrcformat(const uint32_t& inputformat) {
+//Update src format based on rotator's destination format.
+void MdpCtrl::updateSrcFormat(const uint32_t& rotDestFmt) {
     utils::Whf whf = getSrcWhf();
-    whf.format =  inputformat;
+    whf.format =  rotDestFmt;
     setSrcWhf(whf);
 }
 
diff --git a/liboverlay/overlayMdp.h b/liboverlay/overlayMdp.h
index 55e2787..7d3bbee 100644
--- a/liboverlay/overlayMdp.h
+++ b/liboverlay/overlayMdp.h
@@ -33,151 +33,92 @@
 public:
     /* ctor reset */
     explicit MdpCtrl();
-
     /* dtor close */
     ~MdpCtrl();
-
     /* init underlying device using fbnum */
     bool init(uint32_t fbnum);
-
     /* unset overlay, reset and close fd */
     bool close();
-
     /* reset and set ov id to -1 / MSMFB_NEW_REQUEST */
     void reset();
-
-    /* get orient / user_data[0] */
-    int getOrient() const;
-
-    /* returns session id */
-    int getPipeId() const;
-
-    /* returns the fd associated to ctrl*/
-    int getFd() const;
-
-    /* Get screen info. out: info*/
-    bool getScreenInfo(utils::ScreenInfo& info);
-
-    /* overlay get */
-    bool get();
-
-    /* returns flags from mdp structure */
-    int getFlags() const;
-
-    /* set flags to mdp structure */
-    void setFlags(int f);
-
-    /* set z order */
-    void setZ(utils::eZorder z);
-
-    /* set isFg flag */
-    void setIsFg(utils::eIsFg isFg);
-
     /* calls overlay set
      * Set would always consult last good known ov instance.
      * Only if it is different, set would actually exectue ioctl.
      * On a sucess ioctl. last good known ov instance is updated */
     bool set();
-
-    /* return a copy of src whf*/
-    utils::Whf getSrcWhf() const;
-
-    /* set src whf */
-    void setSrcWhf(const utils::Whf& whf);
-
-    /* adjust source width height format based on rot info */
-    void adjustSrcWhf(const bool& rotUsed);
-
-    /* swap src w/h*/
-    void swapSrcWH();
-
-    /* swap src rect w/h */
-    void swapSrcRectWH();
-
-    /* returns a copy to src rect dim */
-    utils::Dim getSrcRectDim() const;
-
-    /* set src/dst rect dim */
-    void setSrcRectDim(const utils::Dim d);
-    void setDstRectDim(const utils::Dim d);
-
-    /* returns a copy ro dst rect dim */
-    utils::Dim getDstRectDim() const;
-
-    /* returns user_data[0]*/
-    int getUserData() const;
-
-    /* sets user_data[0] */
-    void setUserData(int v);
-
-    /* return true if current overlay is different
-     * than last known good overlay */
-    bool ovChanged() const;
-
-    /* save mOVInfo to be last known good ov*/
-    void save();
-
-    /* restore last known good ov to be the current */
-    void restore();
-
     /* Sets the source total width, height, format */
-    bool setSource(const utils::PipeArgs& pargs);
-
+    void setSource(const utils::PipeArgs& pargs);
     /*
      * Sets ROI, the unpadded region, for source buffer.
-     * Should be called before a setPosition, for small clips.
      * Dim - ROI dimensions.
      */
-    bool setCrop(const utils::Dim& d);
-
-    bool setTransform(const utils::eTransform& orient);
-
-    /* set whether rotator can be used */
-    void setRotatorUsed(const bool& rotUsed);
-
+    void setCrop(const utils::Dim& d);
+    void setTransform(const utils::eTransform& orient);
     /* given a dim and w/h, set overlay dim */
-    bool setPosition(const utils::Dim& dim, int w, int h);
-
+    void setPosition(const utils::Dim& dim);
     /* using user_data, sets/unsets roationvalue in mdp flags */
     void setRotationFlags();
-
-    /* Perform transformation calculations */
-    void doTransform();
-
     /* Performs downscale calculations */
-    void doDownscale(int dscale_factor);
-
-    /* Get downscale factor */
-    int getDownscalefactor();
-
-    /* Update the src format */
-    void updateSrcformat(const uint32_t& inputsrcFormat);
-
+    void setDownscale(int dscale_factor);
+    /* Update the src format with rotator's dest*/
+    void updateSrcFormat(const uint32_t& rotDstFormat);
     /* dump state of the object */
     void dump() const;
-
     /* Return the dump in the specified buffer */
     void getDump(char *buf, size_t len);
 
-private:
+    /* returns session id */
+    int getPipeId() const;
+    /* returns the fd associated to ctrl*/
+    int getFd() const;
+    /* returns a copy ro dst rect dim */
+    utils::Dim getDstRectDim() const;
+    /* returns a copy to src rect dim */
+    utils::Dim getSrcRectDim() const;
 
-    /* helper functions for overlayTransform */
-    void overlayTransFlipH();
-    void overlayTransFlipV();
-    void overlayTransRot90();
+private:
+    /* Perform transformation calculations */
+    void doTransform();
+    void doDownscale();
+    /* get orient / user_data[0] */
+        int getOrient() const;
+    /* overlay get */
+    bool get();
+    /* returns flags from mdp structure */
+    int getFlags() const;
+    /* set flags to mdp structure */
+    void setFlags(int f);
+    /* set z order */
+    void setZ(utils::eZorder z);
+    /* set isFg flag */
+    void setIsFg(utils::eIsFg isFg);
+        /* return a copy of src whf*/
+    utils::Whf getSrcWhf() const;
+    /* set src whf */
+    void setSrcWhf(const utils::Whf& whf);
+    /* set src/dst rect dim */
+    void setSrcRectDim(const utils::Dim d);
+    void setDstRectDim(const utils::Dim d);
+    /* returns user_data[0]*/
+    int getUserData() const;
+    /* sets user_data[0] */
+    void setUserData(int v);
+    /* return true if current overlay is different
+     * than last known good overlay */
+    bool ovChanged() const;
+    /* save mOVInfo to be last known good ov*/
+    void save();
+    /* restore last known good ov to be the current */
+    void restore();
 
     utils::eTransform mOrientation; //Holds requested orientation
-    bool mRotUsed; //whether rotator should be used even if requested
-                   //orientation is 0.
-
     /* last good known ov info */
     mdp_overlay   mLkgo;
-
     /* Actual overlay mdp structure */
     mdp_overlay   mOVInfo;
-
     /* FD for the mdp fbnum */
     OvFD          mFd;
+    int mDownscale;
 };
 
 
@@ -210,37 +151,26 @@
 public:
     /* ctor reset data */
     explicit MdpData();
-
     /* dtor close*/
     ~MdpData();
-
     /* init FD */
     bool init(uint32_t fbnum);
-
     /* memset0 the underlying mdp object */
     void reset();
-
     /* close fd, and reset */
     bool close();
-
     /* set id of mdp data */
     void setPipeId(int id);
-
     /* return ses id of data */
     int getPipeId() const;
-
     /* get underlying fd*/
     int getFd() const;
-
     /* get memory_id */
     int getSrcMemoryId() const;
-
     /* calls wrapper play */
     bool play(int fd, uint32_t offset);
-
     /* dump state of the object */
     void dump() const;
-
     /* Return the dump in the specified buffer */
     void getDump(char *buf, size_t len);
 
@@ -248,18 +178,11 @@
 
     /* actual overlay mdp data */
     msmfb_overlay_data mOvData;
-
     /* fd to mdp fbnum */
     OvFD mFd;
 };
 
 //--------------Inlines---------------------------------
-namespace {
-// just a helper func for common operations x-(y+z)
-int compute(uint32_t x, uint32_t y, uint32_t z) {
-    return x-(y+z);
-}
-}
 
 /////   MdpCtrl  //////
 
@@ -299,6 +222,10 @@
     mOVInfo.is_fg = isFg;
 }
 
+inline void MdpCtrl::setDownscale(int dscale) {
+    mDownscale = dscale;
+}
+
 inline bool MdpCtrl::ovChanged() const {
     // 0 means same
     if(0 == ::memcmp(&mOVInfo, &mLkgo, sizeof (mdp_overlay))) {
@@ -375,47 +302,6 @@
         mOVInfo.flags &= ~MDP_SOURCE_ROTATED_90;
 }
 
-inline void MdpCtrl::swapSrcWH() {
-    utils::swap(mOVInfo.src.width,
-            mOVInfo.src.height);
-}
-
-inline void MdpCtrl::swapSrcRectWH() {
-    utils::swap(mOVInfo.src_rect.w,
-            mOVInfo.src_rect.h);
-}
-
-inline void MdpCtrl::overlayTransFlipH()
-{
-    utils::Dim d   = getSrcRectDim();
-    utils::Whf whf = getSrcWhf();
-    d.x = compute(whf.w, d.x, d.w);
-    setSrcRectDim(d);
-}
-
-inline void MdpCtrl::overlayTransFlipV()
-{
-    utils::Dim d   = getSrcRectDim();
-    utils::Whf whf = getSrcWhf();
-    d.y = compute(whf.h, d.y, d.h);
-    setSrcRectDim(d);
-}
-
-inline void MdpCtrl::overlayTransRot90()
-{
-    utils::Dim d   = getSrcRectDim();
-    utils::Whf whf = getSrcWhf();
-    int tmp = d.x;
-    d.x = compute(whf.h,
-            d.y,
-            d.h);
-    d.y = tmp;
-    setSrcRectDim(d);
-    swapSrcWH();
-    swapSrcRectWH();
-}
-
-
 ///////    MdpCtrl3D //////
 
 inline MdpCtrl3D::MdpCtrl3D() { reset(); }
diff --git a/liboverlay/overlayMdpRot.cpp b/liboverlay/overlayMdpRot.cpp
index c9843f2..d1e036c 100755
--- a/liboverlay/overlayMdpRot.cpp
+++ b/liboverlay/overlayMdpRot.cpp
@@ -31,48 +31,38 @@
 
 MdpRot::~MdpRot() { close(); }
 
-inline void MdpRot::setEnable() { mRotImgInfo.enable = 1; }
+bool MdpRot::enabled() const { return mRotImgInfo.enable; }
 
-inline void MdpRot::setDisable() { mRotImgInfo.enable = 0; }
+void MdpRot::setRotations(uint32_t r) { mRotImgInfo.rotations = r; }
 
-inline bool MdpRot::enabled() const { return mRotImgInfo.enable; }
-
-inline void MdpRot::setRotations(uint32_t r) { mRotImgInfo.rotations = r; }
-
-inline int MdpRot::getDstMemId() const {
+int MdpRot::getDstMemId() const {
     return mRotDataInfo.dst.memory_id;
 }
 
-inline uint32_t MdpRot::getDstOffset() const {
+uint32_t MdpRot::getDstOffset() const {
     return mRotDataInfo.dst.offset;
 }
 
-inline uint32_t MdpRot::getDstFormat() const {
+uint32_t MdpRot::getDstFormat() const {
     return mRotImgInfo.dst.format;
 }
 
-inline uint32_t MdpRot::getSessId() const { return mRotImgInfo.session_id; }
-
-inline void MdpRot::setSrcFB() {
-    mRotDataInfo.src.flags |= MDP_MEMORY_ID_TYPE_FB;
-}
+uint32_t MdpRot::getSessId() const { return mRotImgInfo.session_id; }
 
 void MdpRot::setDownscale(int ds) {
     if ((utils::ROT_DS_EIGHTH == ds) && (mRotImgInfo.src_rect.h & 0xF)) {
         // Ensure src_rect.h is a multiple of 16 for 1/8 downscaling.
         // This is an undocumented MDP Rotator constraint.
-        // Note that src_rect.h is already ensured to be 32 pixel height aligned
-        // for MDP_Y_CRCB_H2V2_TILE and MDP_Y_CBCR_H2V2_TILE formats.
         mRotImgInfo.src_rect.h = utils::aligndown(mRotImgInfo.src_rect.h, 16);
     }
     mRotImgInfo.downscale_ratio = ds;
 }
 
-inline void MdpRot::save() {
+void MdpRot::save() {
     mLSRotImgInfo = mRotImgInfo;
 }
 
-inline bool MdpRot::rotConfChanged() const {
+bool MdpRot::rotConfChanged() const {
     // 0 means same
     if(0 == ::memcmp(&mRotImgInfo, &mLSRotImgInfo,
                 sizeof (msm_rotator_img_info))) {
@@ -92,13 +82,7 @@
 
 void MdpRot::setSource(const overlay::utils::Whf& awhf) {
     utils::Whf whf(awhf);
-
     mRotImgInfo.src.format = whf.format;
-    if(whf.format == MDP_Y_CRCB_H2V2_TILE ||
-        whf.format == MDP_Y_CBCR_H2V2_TILE) {
-        whf.w =  utils::alignup(awhf.w, 64);
-        whf.h = utils::alignup(awhf.h, 32);
-    }
 
     mRotImgInfo.src.width = whf.w;
     mRotImgInfo.src.height = whf.h;
@@ -110,13 +94,13 @@
     mRotImgInfo.dst.height = whf.h;
 }
 
-inline void MdpRot::setFlags(const utils::eMdpFlags& flags) {
+void MdpRot::setFlags(const utils::eMdpFlags& flags) {
     mRotImgInfo.secure = 0;
     if(flags & utils::OV_MDP_SECURE_OVERLAY_SESSION)
         mRotImgInfo.secure = 1;
 }
 
-inline void MdpRot::setTransform(const utils::eTransform& rot)
+void MdpRot::setTransform(const utils::eTransform& rot)
 {
     int r = utils::getMdpOrient(rot);
     setRotations(r);
@@ -126,14 +110,7 @@
     ALOGE_IF(DEBUG_OVERLAY, "%s: r=%d", __FUNCTION__, r);
 }
 
-inline void MdpRot::setRotatorUsed(const bool& rotUsed) {
-    setDisable();
-    if(rotUsed) {
-        setEnable();
-    }
-}
-
-inline void MdpRot::doTransform() {
+void MdpRot::doTransform() {
     if(mOrientation & utils::OVERLAY_TRANSFORM_ROT_90)
         utils::swap(mRotImgInfo.dst.width, mRotImgInfo.dst.height);
 }
@@ -141,9 +118,11 @@
 bool MdpRot::commit() {
     doTransform();
     if(rotConfChanged()) {
+        mRotImgInfo.enable = 1;
         if(!overlay::mdp_wrapper::startRotator(mFd.getFD(), mRotImgInfo)) {
             ALOGE("MdpRot commit failed");
             dump();
+            mRotImgInfo.enable = 0;
             return false;
         }
         save();
diff --git a/liboverlay/overlayMdssRot.cpp b/liboverlay/overlayMdssRot.cpp
index fd747dd..70bf52b 100644
--- a/liboverlay/overlayMdssRot.cpp
+++ b/liboverlay/overlayMdssRot.cpp
@@ -43,34 +43,26 @@
 
 MdssRot::~MdssRot() { close(); }
 
-inline void MdssRot::setEnable() { mEnabled = true; }
+bool MdssRot::enabled() const { return mEnabled; }
 
-inline void MdssRot::setDisable() { mEnabled = false; }
+void MdssRot::setRotations(uint32_t flags) { mRotInfo.flags |= flags; }
 
-inline bool MdssRot::enabled() const { return mEnabled; }
-
-inline void MdssRot::setRotations(uint32_t flags) { mRotInfo.flags |= flags; }
-
-inline int MdssRot::getDstMemId() const {
+int MdssRot::getDstMemId() const {
     return mRotData.dst_data.memory_id;
 }
 
-inline uint32_t MdssRot::getDstOffset() const {
+uint32_t MdssRot::getDstOffset() const {
     return mRotData.dst_data.offset;
 }
 
-inline uint32_t MdssRot::getDstFormat() const {
+uint32_t MdssRot::getDstFormat() const {
     //For mdss src and dst formats are same
     return mRotInfo.src.format;
 }
 
-inline uint32_t MdssRot::getSessId() const { return mRotInfo.id; }
+uint32_t MdssRot::getSessId() const { return mRotInfo.id; }
 
-inline void MdssRot::setSrcFB() {
-    mRotData.data.flags |= MDP_MEMORY_ID_TYPE_FB;
-}
-
-inline bool MdssRot::init() {
+bool MdssRot::init() {
     if(!utils::openDev(mFd, 0, Res::fbPath, O_RDWR)) {
         ALOGE("MdssRot failed to init fb0");
         return false;
@@ -82,11 +74,6 @@
     utils::Whf whf(awhf);
 
     mRotInfo.src.format = whf.format;
-    if(whf.format == MDP_Y_CRCB_H2V2_TILE ||
-        whf.format == MDP_Y_CBCR_H2V2_TILE) {
-        whf.w =  utils::alignup(awhf.w, 64);
-        whf.h = utils::alignup(awhf.h, 32);
-    }
 
     mRotInfo.src.width = whf.w;
     mRotInfo.src.height = whf.h;
@@ -98,13 +85,13 @@
     mRotInfo.dst_rect.h = whf.h;
 }
 
-inline void MdssRot::setDownscale(int ds) {}
+void MdssRot::setDownscale(int ds) {}
 
-inline void MdssRot::setFlags(const utils::eMdpFlags& flags) {
+void MdssRot::setFlags(const utils::eMdpFlags& flags) {
     mRotInfo.flags |= flags;
 }
 
-inline void MdssRot::setTransform(const utils::eTransform& rot)
+void MdssRot::setTransform(const utils::eTransform& rot)
 {
     int flags = utils::getMdpOrient(rot);
     if (flags != -1)
@@ -115,14 +102,7 @@
     ALOGE_IF(DEBUG_OVERLAY, "%s: rot=%d", __FUNCTION__, flags);
 }
 
-inline void MdssRot::setRotatorUsed(const bool& rotUsed) {
-    setDisable();
-    if(rotUsed) {
-        setEnable();
-    }
-}
-
-inline void MdssRot::doTransform() {
+void MdssRot::doTransform() {
     if(mOrientation & utils::OVERLAY_TRANSFORM_ROT_90)
         utils::swap(mRotInfo.dst_rect.w, mRotInfo.dst_rect.h);
 }
@@ -130,10 +110,11 @@
 bool MdssRot::commit() {
     doTransform();
     mRotInfo.flags |= MDSS_MDP_ROT_ONLY;
+    mEnabled = true;
     if(!overlay::mdp_wrapper::setOverlay(mFd.getFD(), mRotInfo)) {
         ALOGE("MdssRot commit failed!");
         dump();
-        return false;
+        return (mEnabled = false);
     }
     mRotData.id = mRotInfo.id;
     // reset rotation flags to avoid stale orientation values
diff --git a/liboverlay/overlayRotator.cpp b/liboverlay/overlayRotator.cpp
index 90a1e7a..3861297 100644
--- a/liboverlay/overlayRotator.cpp
+++ b/liboverlay/overlayRotator.cpp
@@ -70,4 +70,65 @@
     return ret;
 }
 
+RotMgr::RotMgr() {
+    for(int i = 0; i < MAX_ROT_SESS; i++) {
+        mRot[i] = 0;
+    }
+    mUseCount = 0;
+}
+
+RotMgr::~RotMgr() {
+    clear();
+}
+
+void RotMgr::configBegin() {
+    //Reset the number of objects used
+    mUseCount = 0;
+}
+
+void RotMgr::configDone() {
+    //Remove the top most unused objects. Videos come and go.
+    for(int i = mUseCount; i < MAX_ROT_SESS; i++) {
+        if(mRot[i]) {
+            delete mRot[i];
+            mRot[i] = 0;
+        }
+    }
+}
+
+Rotator* RotMgr::getNext() {
+    //Return a rot object, creating one if necessary
+    overlay::Rotator *rot = NULL;
+    if(mUseCount >= MAX_ROT_SESS) {
+        ALOGE("%s, MAX rotator sessions reached", __func__);
+    } else {
+        if(mRot[mUseCount] == NULL)
+            mRot[mUseCount] = overlay::Rotator::getRotator();
+        rot = mRot[mUseCount++];
+    }
+    return rot;
+}
+
+void RotMgr::clear() {
+    //Brute force obj destruction, helpful in suspend.
+    for(int i = 0; i < MAX_ROT_SESS; i++) {
+        if(mRot[i]) {
+            delete mRot[i];
+            mRot[i] = 0;
+        }
+    }
+    mUseCount = 0;
+}
+
+void RotMgr::getDump(char *buf, size_t len) {
+    for(int i = 0; i < MAX_ROT_SESS; i++) {
+        if(mRot[i]) {
+            mRot[i]->getDump(buf, len);
+        }
+    }
+    char str[32] = {'\0'};
+    snprintf(str, 32, "\n================\n");
+    strncat(buf, str, strlen(str));
+}
+
 }
diff --git a/liboverlay/overlayRotator.h b/liboverlay/overlayRotator.h
index bd2fd7e..36fe581 100644
--- a/liboverlay/overlayRotator.h
+++ b/liboverlay/overlayRotator.h
@@ -43,22 +43,14 @@
 public:
     enum { TYPE_MDP, TYPE_MDSS };
     virtual ~Rotator();
-    virtual bool init() = 0;
-    virtual bool close() = 0;
     virtual void setSource(const utils::Whf& wfh) = 0;
     virtual void setFlags(const utils::eMdpFlags& flags) = 0;
     virtual void setTransform(const utils::eTransform& rot) = 0;
-    virtual void setRotatorUsed(const bool& rotUsed) = 0;
     virtual bool commit() = 0;
-    virtual void setRotations(uint32_t r) = 0;
-    virtual void setSrcFB() = 0;
     virtual void setDownscale(int ds) = 0;
     virtual int getDstMemId() const = 0;
     virtual uint32_t getDstOffset() const = 0;
     virtual uint32_t getDstFormat() const = 0;
-    virtual void setEnable() = 0;
-    virtual void setDisable() = 0;
-    virtual bool enabled () const = 0;
     virtual uint32_t getSessId() const = 0;
     virtual bool queueBuffer(int fd, uint32_t offset) = 0;
     virtual void dump() const = 0;
@@ -116,22 +108,14 @@
 class MdpRot : public Rotator {
 public:
     virtual ~MdpRot();
-    virtual bool init();
-    virtual bool close();
     virtual void setSource(const utils::Whf& wfh);
     virtual void setFlags(const utils::eMdpFlags& flags);
     virtual void setTransform(const utils::eTransform& rot);
-    virtual void setRotatorUsed(const bool& rotUsed);
     virtual bool commit();
-    virtual void setRotations(uint32_t r);
-    virtual void setSrcFB();
     virtual void setDownscale(int ds);
     virtual int getDstMemId() const;
     virtual uint32_t getDstOffset() const;
     virtual uint32_t getDstFormat() const;
-    virtual void setEnable();
-    virtual void setDisable();
-    virtual bool enabled () const;
     virtual uint32_t getSessId() const;
     virtual bool queueBuffer(int fd, uint32_t offset);
     virtual void dump() const;
@@ -139,6 +123,10 @@
 
 private:
     explicit MdpRot();
+    bool init();
+    bool close();
+    void setRotations(uint32_t r);
+    bool enabled () const;
     /* remap rot buffers */
     bool remap(uint32_t numbufs);
     bool open_i(uint32_t numbufs, uint32_t bufsz);
@@ -178,22 +166,14 @@
 class MdssRot : public Rotator {
 public:
     virtual ~MdssRot();
-    virtual bool init();
-    virtual bool close();
     virtual void setSource(const utils::Whf& wfh);
     virtual void setFlags(const utils::eMdpFlags& flags);
     virtual void setTransform(const utils::eTransform& rot);
-    virtual void setRotatorUsed(const bool& rotUsed);
     virtual bool commit();
-    virtual void setRotations(uint32_t r);
-    virtual void setSrcFB();
     virtual void setDownscale(int ds);
     virtual int getDstMemId() const;
     virtual uint32_t getDstOffset() const;
     virtual uint32_t getDstFormat() const;
-    virtual void setEnable();
-    virtual void setDisable();
-    virtual bool enabled () const;
     virtual uint32_t getSessId() const;
     virtual bool queueBuffer(int fd, uint32_t offset);
     virtual void dump() const;
@@ -201,6 +181,10 @@
 
 private:
     explicit MdssRot();
+    bool init();
+    bool close();
+    void setRotations(uint32_t r);
+    bool enabled () const;
     /* remap rot buffers */
     bool remap(uint32_t numbufs);
     bool open_i(uint32_t numbufs, uint32_t bufsz);
@@ -228,6 +212,28 @@
     friend Rotator* Rotator::getRotator();
 };
 
+// Holder of rotator objects. Manages lifetimes
+class RotMgr {
+public:
+    //Maximum sessions based on VG pipes, since rotator is used only for videos.
+    //Even though we can have 4 mixer stages, that much may be unnecessary.
+    enum { MAX_ROT_SESS = 3 };
+    RotMgr();
+    ~RotMgr();
+    void configBegin();
+    void configDone();
+    overlay::Rotator *getNext();
+    void clear(); //Removes all instances
+    /* Returns rot dump.
+     * Expects a NULL terminated buffer of big enough size.
+     */
+    void getDump(char *buf, size_t len);
+private:
+    overlay::Rotator *mRot[MAX_ROT_SESS];
+    int mUseCount;
+};
+
+
 } // overlay
 
 #endif // OVERlAY_ROTATOR_H
diff --git a/liboverlay/overlayUtils.cpp b/liboverlay/overlayUtils.cpp
index de10c9f..907597e 100644
--- a/liboverlay/overlayUtils.cpp
+++ b/liboverlay/overlayUtils.cpp
@@ -89,67 +89,6 @@
 #define NUM_FB_DEVICES 3
 
 //--------------------------------------------------------
-FrameBufferInfo::FrameBufferInfo() {
-    mFBWidth = 0;
-    mFBHeight = 0;
-    mBorderFillSupported = false;
-
-    OvFD mFd;
-
-    // Use open defined in overlayFD file to open fd for fb0
-    if(!overlay::open(mFd, 0, Res::fbPath)) {
-        ALOGE("FrameBufferInfo: failed to open fd");
-        return;
-    }
-
-    if (!mFd.valid()) {
-        ALOGE("FrameBufferInfo: FD not valid");
-        return;
-    }
-
-    fb_var_screeninfo vinfo;
-    if (!mdp_wrapper::getVScreenInfo(mFd.getFD(), vinfo)) {
-        ALOGE("FrameBufferInfo: failed getVScreenInfo on fb0");
-        mFd.close();
-        return;
-    }
-
-    int mdpVersion = qdutils::MDPVersion::getInstance().getMDPVersion();
-    if (mdpVersion < qdutils::MDSS_V5) {
-        mdp_overlay ov;
-        memset(&ov, 0, sizeof(ov));
-        ov.id = 1;
-        if (!mdp_wrapper::getOverlay(mFd.getFD(), ov)) {
-            ALOGE("FrameBufferInfo: failed getOverlay on fb0");
-            mFd.close();
-            return;
-        }
-        mBorderFillSupported = (ov.flags & MDP_BORDERFILL_SUPPORTED) ?
-                               true : false;
-    } else {
-        // badger always support border fill
-        mBorderFillSupported = true;
-    }
-
-    mFd.close();
-    mFBWidth = vinfo.xres;
-    mFBHeight = vinfo.yres;
-}
-
-FrameBufferInfo* FrameBufferInfo::getInstance() {
-    if (!sFBInfoInstance) {
-        sFBInfoInstance = new FrameBufferInfo;
-    }
-    return sFBInfoInstance;
-}
-
-int FrameBufferInfo::getWidth() const {
-    return mFBWidth;
-}
-
-int FrameBufferInfo::getHeight() const {
-    return mFBHeight;
-}
 
 /* clears any VG pipes allocated to the fb devices */
 int initOverlay() {
@@ -293,6 +232,58 @@
     return -1;
 }
 
+int getDownscaleFactor(const int& src_w, const int& src_h,
+        const int& dst_w, const int& dst_h) {
+    int dscale_factor = utils::ROT_DS_NONE;
+    // We need this check to engage the rotator whenever possible to assist MDP
+    // in performing video downscale.
+    // This saves bandwidth and avoids causing the driver to make too many panel
+    // -mode switches between BLT (writeback) and non-BLT (Direct) modes.
+    // Use-case: Video playback [with downscaling and rotation].
+    if (dst_w && dst_h)
+    {
+        uint32_t dscale = (src_w * src_h) / (dst_w * dst_h);
+        if(dscale < 2) {
+            // Down-scale to > 50% of orig.
+            dscale_factor = utils::ROT_DS_NONE;
+        } else if(dscale < 4) {
+            // Down-scale to between > 25% to <= 50% of orig.
+            dscale_factor = utils::ROT_DS_HALF;
+        } else if(dscale < 8) {
+            // Down-scale to between > 12.5% to <= 25% of orig.
+            dscale_factor = utils::ROT_DS_FOURTH;
+        } else {
+            // Down-scale to <= 12.5% of orig.
+            dscale_factor = utils::ROT_DS_EIGHTH;
+        }
+    }
+    return dscale_factor;
+}
+
+static inline int compute(const uint32_t& x, const uint32_t& y,
+        const uint32_t& z) {
+    return x - ( y + z );
+}
+
+void preRotateSource(eTransform& tr, Whf& whf, Dim& srcCrop) {
+    if(tr & OVERLAY_TRANSFORM_FLIP_H) {
+        srcCrop.x = compute(whf.w, srcCrop.x, srcCrop.w);
+    }
+    if(tr & OVERLAY_TRANSFORM_FLIP_V) {
+        srcCrop.y = compute(whf.h, srcCrop.y, srcCrop.h);
+    }
+    if(tr & OVERLAY_TRANSFORM_ROT_90) {
+        int tmp = srcCrop.x;
+        srcCrop.x = compute(whf.h,
+                srcCrop.y,
+                srcCrop.h);
+        srcCrop.y = tmp;
+        swap(whf.w, whf.h);
+        swap(srcCrop.w, srcCrop.h);
+    }
+    tr = OVERLAY_TRANSFORM_0;
+}
+
 bool is3DTV() {
     char is3DTV = '0';
     IOFile fp(Res::edid3dInfoFile, "r");
diff --git a/liboverlay/overlayUtils.h b/liboverlay/overlayUtils.h
index 0459ea4..555232a 100644
--- a/liboverlay/overlayUtils.h
+++ b/liboverlay/overlayUtils.h
@@ -114,36 +114,6 @@
     const NoCopy& operator=(const NoCopy&);
 };
 
-/*
-* Utility class to query the framebuffer info for primary display
-*
-* Usage:
-*    Outside of functions:
-*       utils::FrameBufferInfo* utils::FrameBufferInfo::sFBInfoInstance = 0;
-*    Inside functions:
-*       utils::FrameBufferInfo::getInstance()->supportTrueMirroring()
-*/
-class FrameBufferInfo {
-
-public:
-    /* ctor init */
-    explicit FrameBufferInfo();
-
-    /* Gets an instance if one does not already exist */
-    static FrameBufferInfo* getInstance();
-
-    /* Gets width of primary framebuffer */
-    int getWidth() const;
-
-    /* Gets height of primary framebuffer */
-    int getHeight() const;
-
-private:
-    int mFBWidth;
-    int mFBHeight;
-    bool mBorderFillSupported;
-    static FrameBufferInfo *sFBInfoInstance;
-};
 
 /* 3D related utils, defines etc...
  * The compound format passed to the overlay is
@@ -163,18 +133,9 @@
 int initOverlay(void);
 
 inline uint32_t format3D(uint32_t x) { return x & 0xFF000; }
-inline uint32_t colorFormat(uint32_t fmt) {
-    /*TODO enable this block only if format has interlace / 3D info in top bits.
-    if(fmt & INTERLACE_MASK) {
-        fmt = fmt ^ HAL_PIXEL_FORMAT_INTERLACE;
-    }
-    fmt = fmt & 0xFFF;*/
-    return fmt;
-}
 inline uint32_t format3DOutput(uint32_t x) {
     return (x & 0xF000) >> SHIFT_OUT_3D; }
 inline uint32_t format3DInput(uint32_t x) { return x & 0xF0000; }
-uint32_t getColorFormat(uint32_t format);
 
 bool isHDMIConnected ();
 bool is3DTV();
@@ -255,10 +216,8 @@
 
 /**
  * Rotator flags: not to be confused with orientation flags.
- * Ususally, you want to open the rotator to make sure it is
+ * Usually, you want to open the rotator to make sure it is
  * ready for business.
- * ROT_FLAG_DISABLED: Rotator not used unless required.
- * ROT_FLAG_ENABLED: Rotator used even if not required.
  * */
  enum eRotFlags {
     ROT_FLAGS_NONE = 0,
@@ -268,6 +227,7 @@
     //driver. If downscale optimizatation is required,
     //then rotator will be used even if its 0 rotation case.
     ROT_DOWNSCALE_ENABLED = 1 << 1,
+    ROT_PREROTATED = 1 << 2,
 };
 
 enum eRotDownscale {
@@ -298,7 +258,6 @@
     OV_MDP_DEINTERLACE = MDP_DEINTERLACE,
     OV_MDP_SECURE_OVERLAY_SESSION = MDP_SECURE_OVERLAY_SESSION,
     OV_MDP_SOURCE_ROTATED_90 = MDP_SOURCE_ROTATED_90,
-    OV_MDP_MEMORY_ID_TYPE_FB = MDP_MEMORY_ID_TYPE_FB,
     OV_MDP_BACKEND_COMPOSITION = MDP_BACKEND_COMPOSITION,
     OV_MDP_BLEND_FG_PREMULT = MDP_BLEND_FG_PREMULT,
     OV_MDP_FLIP_H = MDP_FLIP_LR,
@@ -307,7 +266,7 @@
 };
 
 enum eZorder {
-    ZORDER_0,
+    ZORDER_0 = 0,
     ZORDER_1,
     ZORDER_2,
     ZORDER_3,
@@ -422,6 +381,8 @@
 
 int getMdpFormat(int format);
 int getHALFormat(int mdpFormat);
+int getDownscaleFactor(const int& src_w, const int& src_h,
+        const int& dst_w, const int& dst_h);
 
 /* flip is upside down and such. V, H flip
  * rotation is 90, 180 etc
@@ -597,12 +558,6 @@
     return -1;
 }
 
-inline uint32_t getColorFormat(uint32_t format)
-{
-    return (format == HAL_PIXEL_FORMAT_YV12) ?
-            format : colorFormat(format);
-}
-
 // FB0
 template <int CHAN>
 inline Dim getPositionS3DImpl(const Whf& whf)
@@ -766,6 +721,7 @@
     return OV_MDP_PIPE_ANY;
 }
 
+void preRotateSource(eTransform& tr, Whf& whf, Dim& srcCrop);
 void getDump(char *buf, size_t len, const char *prefix, const mdp_overlay& ov);
 void getDump(char *buf, size_t len, const char *prefix, const msmfb_img& ov);
 void getDump(char *buf, size_t len, const char *prefix, const mdp_rect& ov);
diff --git a/liboverlay/pipes/overlayGenPipe.cpp b/liboverlay/pipes/overlayGenPipe.cpp
index 486cfda..9108acd 100644
--- a/liboverlay/pipes/overlayGenPipe.cpp
+++ b/liboverlay/pipes/overlayGenPipe.cpp
@@ -34,7 +34,7 @@
 namespace overlay {
 
 GenericPipe::GenericPipe(int dpy) : mFbNum(dpy), mRot(0), mRotUsed(false),
-        mRotDownscaleOpt(false), pipeState(CLOSED) {
+        mRotDownscaleOpt(false), mPreRotated(false), pipeState(CLOSED) {
     init();
 }
 
@@ -47,6 +47,7 @@
     ALOGE_IF(DEBUG_OVERLAY, "GenericPipe init");
     mRotUsed = false;
     mRotDownscaleOpt = false;
+    mPreRotated = false;
     if(mFbNum)
         mFbNum = Overlay::getInstance()->getExtFbNum();
 
@@ -87,54 +88,31 @@
     return ret;
 }
 
-bool GenericPipe::setSource(
-        const utils::PipeArgs& args)
-{
-    utils::PipeArgs newargs(args);
-    //Interlace video handling.
-    if(newargs.whf.format & INTERLACE_MASK) {
-        setMdpFlags(newargs.mdpFlags, utils::OV_MDP_DEINTERLACE);
-    }
-    utils::Whf whf(newargs.whf);
-    //Extract HAL format from lower bytes. Deinterlace if interlaced.
-    whf.format = utils::getColorFormat(whf.format);
-    //Get MDP equivalent of HAL format.
-    whf.format = utils::getMdpFormat(whf.format);
-    newargs.whf = whf;
-
+void GenericPipe::setSource(const utils::PipeArgs& args) {
     //Cache if user wants 0-rotation
-    mRotUsed = newargs.rotFlags & utils::ROT_0_ENABLED;
-    mRotDownscaleOpt = newargs.rotFlags & utils::ROT_DOWNSCALE_ENABLED;
-
-    mRot->setSource(newargs.whf);
-    mRot->setFlags(newargs.mdpFlags);
-    return mCtrlData.ctrl.setSource(newargs);
+    mRotUsed = args.rotFlags & utils::ROT_0_ENABLED;
+    mRotDownscaleOpt = args.rotFlags & utils::ROT_DOWNSCALE_ENABLED;
+    mPreRotated = args.rotFlags & utils::ROT_PREROTATED;
+    if(mPreRotated) mRotUsed = false;
+    mRot->setSource(args.whf);
+    mRot->setFlags(args.mdpFlags);
+    mCtrlData.ctrl.setSource(args);
 }
 
-bool GenericPipe::setCrop(
-        const overlay::utils::Dim& d) {
-    return mCtrlData.ctrl.setCrop(d);
+void GenericPipe::setCrop(const overlay::utils::Dim& d) {
+    mCtrlData.ctrl.setCrop(d);
 }
 
-bool GenericPipe::setTransform(
-        const utils::eTransform& orient)
-{
+void GenericPipe::setTransform(const utils::eTransform& orient) {
     //Rotation could be enabled by user for zero-rot or the layer could have
     //some transform. Mark rotation enabled in either case.
-    mRotUsed |= (orient != utils::OVERLAY_TRANSFORM_0);
+    mRotUsed |= ((orient & utils::OVERLAY_TRANSFORM_ROT_90) && !mPreRotated);
     mRot->setTransform(orient);
-
-    return mCtrlData.ctrl.setTransform(orient);
+    mCtrlData.ctrl.setTransform(orient);
 }
 
-bool GenericPipe::setPosition(const utils::Dim& d)
-{
-    return mCtrlData.ctrl.setPosition(d);
-}
-
-void GenericPipe::setRotatorUsed(const bool& rotUsed) {
-    mRot->setRotatorUsed(rotUsed);
-    mCtrlData.ctrl.setRotatorUsed(rotUsed);
+void GenericPipe::setPosition(const utils::Dim& d) {
+    mCtrlData.ctrl.setPosition(d);
 }
 
 bool GenericPipe::commit() {
@@ -142,20 +120,16 @@
     int downscale_factor = utils::ROT_DS_NONE;
 
     if(mRotDownscaleOpt) {
-        /* Can go ahead with calculation of downscale_factor since
-         * we consider area when calculating it */
-        downscale_factor = mCtrlData.ctrl.getDownscalefactor();
-        if(downscale_factor)
-            mRotUsed = true;
+        ovutils::Dim src(mCtrlData.ctrl.getCrop());
+        ovutils::Dim dst(mCtrlData.ctrl.getPosition());
+        downscale_factor = ovutils::getDownscaleFactor(
+                src.w, src.h, dst.w, dst.h);
+        mRotUsed |= (downscale_factor && !mPreRotated);
     }
 
-    setRotatorUsed(mRotUsed);
-    mCtrlData.ctrl.doTransform();
-
-    mCtrlData.ctrl.doDownscale(downscale_factor);
-    mRot->setDownscale(downscale_factor);
 
     if(mRotUsed) {
+        mRot->setDownscale(downscale_factor);
         //If wanting to use rotator, start it.
         if(!mRot->commit()) {
             ALOGE("GenPipe Rotator commit failed");
@@ -170,9 +144,10 @@
          * The output format of the rotator might be different depending on
          * whether fastyuv mode is enabled in the rotator.
          */
-        mCtrlData.ctrl.updateSrcformat(mRot->getDstFormat());
+        mCtrlData.ctrl.updateSrcFormat(mRot->getDstFormat());
     }
 
+    mCtrlData.ctrl.setDownscale(downscale_factor);
     ret = mCtrlData.ctrl.commit();
 
     //If mdp commit fails, flush rotator session, memory, fd and create a hollow
@@ -220,11 +195,6 @@
     return mCtrlData.ctrl.getFd();
 }
 
-utils::ScreenInfo GenericPipe::getScreenInfo() const
-{
-    return mCtrlData.ctrl.getScreenInfo();
-}
-
 utils::Dim GenericPipe::getCrop() const
 {
     return mCtrlData.ctrl.getCrop();
diff --git a/liboverlay/pipes/overlayGenPipe.h b/liboverlay/pipes/overlayGenPipe.h
index 1d1be25..c71f8d2 100644
--- a/liboverlay/pipes/overlayGenPipe.h
+++ b/liboverlay/pipes/overlayGenPipe.h
@@ -43,48 +43,34 @@
     explicit GenericPipe(int dpy);
     /* dtor */
     ~GenericPipe();
-    /* CTRL/DATA init. Not owning rotator, will not  init it */
     bool init();
-    /* CTRL/DATA close. Not owning rotator, will not close it */
     bool close();
-
     /* Control APIs */
     /* set source using whf, orient and wait flag */
-    bool setSource(const utils::PipeArgs& args);
+    void setSource(const utils::PipeArgs& args);
     /* set crop a.k.a the region of interest */
-    bool setCrop(const utils::Dim& d);
+    void setCrop(const utils::Dim& d);
     /* set orientation*/
-    bool setTransform(const utils::eTransform& param);
+    void setTransform(const utils::eTransform& param);
     /* set mdp posision using dim */
-    bool setPosition(const utils::Dim& dim);
+    void setPosition(const utils::Dim& dim);
     /* commit changes to the overlay "set"*/
     bool commit();
-
     /* Data APIs */
     /* queue buffer to the overlay */
     bool queueBuffer(int fd, uint32_t offset);
-
     /* return cached startup args */
     const utils::PipeArgs& getArgs() const;
-
-    /* retrieve screen info */
-    utils::ScreenInfo getScreenInfo() const;
-
     /* retrieve cached crop data */
     utils::Dim getCrop() const;
-
     /* is closed */
     bool isClosed() const;
-
     /* is open */
     bool isOpen() const;
-
     /* return Ctrl fd. Used for S3D */
     int getCtrlFd() const;
-
     /* dump the state of the object */
     void dump() const;
-
     /* Return the dump in the specified buffer */
     void getDump(char *buf, size_t len);
 
@@ -92,23 +78,17 @@
     /* set Closed pipe */
     bool setClosed();
 
-    /* Set whether rotator can be used */
-    void setRotatorUsed(const bool& rotUsed);
-
     int mFbNum;
-
     /* Ctrl/Data aggregator */
     CtrlData mCtrlData;
-
     Rotator* mRot;
-
     //Whether rotator is used for 0-rot or otherwise
     bool mRotUsed;
-
     //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;
-
+    //Whether the source is prerotated.
+    bool mPreRotated;
     /* Pipe open or closed */
     enum ePipeState {
         CLOSED,