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/libhwcomposer/hwc.cpp b/libhwcomposer/hwc.cpp
index ae7bb95..30555fa 100644
--- a/libhwcomposer/hwc.cpp
+++ b/libhwcomposer/hwc.cpp
@@ -27,6 +27,7 @@
 #include <utils/Trace.h>
 
 #include <overlay.h>
+#include <overlayRotator.h>
 #include <fb_priv.h>
 #include <mdp_version.h>
 #include "hwc_utils.h"
@@ -100,11 +101,11 @@
 
         if(ctx->mFBUpdate[i])
             ctx->mFBUpdate[i]->reset();
-
+        if(ctx->mVidOv[i])
+            ctx->mVidOv[i]->reset();
         if(ctx->mCopyBit[i])
             ctx->mCopyBit[i]->reset();
     }
-    VideoOverlay::reset();
 }
 
 //clear prev layer prop flags and realloc for current frame
@@ -135,7 +136,7 @@
             int ret = ctx->mMDPComp->prepare(ctx, list);
             if(!ret) {
                 // IF MDPcomp fails use this route
-                VideoOverlay::prepare(ctx, list, dpy);
+                ctx->mVidOv[dpy]->prepare(ctx, list);
                 ctx->mFBUpdate[dpy]->prepare(ctx, list);
             }
             ctx->mLayerCache[dpy]->updateLayerCache(list);
@@ -161,7 +162,7 @@
             if(fbLayer->handle) {
                 setListStats(ctx, list, dpy);
                 reset_layer_prop(ctx, dpy);
-                VideoOverlay::prepare(ctx, list, dpy);
+                ctx->mVidOv[dpy]->prepare(ctx, list);
                 ctx->mFBUpdate[dpy]->prepare(ctx, list);
                 ctx->mLayerCache[dpy]->updateLayerCache(list);
                 if(ctx->mCopyBit[dpy])
@@ -188,6 +189,7 @@
     reset(ctx, numDisplays, displays);
 
     ctx->mOverlay->configBegin();
+    ctx->mRotMgr->configBegin();
 
     for (int32_t i = numDisplays; i >= 0; i--) {
         hwc_display_contents_1_t *list = displays[i];
@@ -205,6 +207,8 @@
     }
 
     ctx->mOverlay->configDone();
+    ctx->mRotMgr->configDone();
+
     return ret;
 }
 
@@ -250,6 +254,7 @@
             if(blank) {
                 ctx->mOverlay->configBegin();
                 ctx->mOverlay->configDone();
+                ctx->mRotMgr->clear();
                 ret = ioctl(m->framebuffer->fd, FBIOBLANK, FB_BLANK_POWERDOWN);
 
                 if(ctx->dpyAttr[HWC_DISPLAY_VIRTUAL].connected == true) {
@@ -344,12 +349,12 @@
             copybitDone = ctx->mCopyBit[dpy]->draw(ctx, list, dpy, &fd);
         if(list->numHwLayers > 1)
             hwc_sync(ctx, list, dpy, fd);
-        if (!VideoOverlay::draw(ctx, list, dpy)) {
-            ALOGE("%s: VideoOverlay::draw fail!", __FUNCTION__);
+        if (!ctx->mVidOv[dpy]->draw(ctx, list)) {
+            ALOGE("%s: VideoOverlay draw failed", __FUNCTION__);
             ret = -1;
         }
         if (!ctx->mMDPComp->draw(ctx, list)) {
-            ALOGE("%s: MDPComp::draw fail!", __FUNCTION__);
+            ALOGE("%s: MDPComp draw failed", __FUNCTION__);
             ret = -1;
         }
 
@@ -365,7 +370,7 @@
             if(!(fbLayer->flags & HWC_SKIP_LAYER) &&
                 (list->numHwLayers > 1)) {
                 if (!ctx->mFBUpdate[dpy]->draw(ctx, hnd)) {
-                    ALOGE("%s: FBUpdate::draw fail!", __FUNCTION__);
+                    ALOGE("%s: FBUpdate draw failed", __FUNCTION__);
                     ret = -1;
                 }
             }
@@ -400,7 +405,7 @@
         if(list->numHwLayers > 1)
             hwc_sync(ctx, list, dpy, fd);
 
-        if (!VideoOverlay::draw(ctx, list, dpy)) {
+        if (!ctx->mVidOv[dpy]->draw(ctx, list)) {
             ALOGE("%s: VideoOverlay::draw fail!", __FUNCTION__);
             ret = -1;
         }
@@ -549,6 +554,9 @@
     char ovDump[2048] = {'\0'};
     ctx->mOverlay->getDump(ovDump, 2048);
     dumpsys_log(aBuf, ovDump);
+    ovDump[0] = '\0';
+    ctx->mRotMgr->getDump(ovDump, 2048);
+    dumpsys_log(aBuf, ovDump);
     strlcpy(buff, aBuf.string(), buff_len);
 }
 
diff --git a/libhwcomposer/hwc_fbupdate.cpp b/libhwcomposer/hwc_fbupdate.cpp
index f9161a3..e79523d 100644
--- a/libhwcomposer/hwc_fbupdate.cpp
+++ b/libhwcomposer/hwc_fbupdate.cpp
@@ -71,7 +71,8 @@
             ALOGE("%s:NULL private handle for layer!", __FUNCTION__);
             return false;
         }
-        ovutils::Whf info(hnd->width, hnd->height, hnd->format, hnd->size);
+        ovutils::Whf info(hnd->width, hnd->height,
+                ovutils::getMdpFormat(hnd->format), hnd->size);
 
         //Request an RGB pipe
         ovutils::eDest dest = ov.nextPipe(ovutils::OV_MDP_PIPE_RGB, mDpy);
@@ -171,7 +172,8 @@
             ALOGE("%s:NULL private handle for layer!", __FUNCTION__);
             return false;
         }
-        ovutils::Whf info(hnd->width, hnd->height, hnd->format, hnd->size);
+        ovutils::Whf info(hnd->width, hnd->height,
+                ovutils::getMdpFormat(hnd->format), hnd->size);
 
         //Request left RGB pipe
         ovutils::eDest destL = ov.nextPipe(ovutils::OV_MDP_PIPE_RGB, mDpy);
diff --git a/libhwcomposer/hwc_mdpcomp.cpp b/libhwcomposer/hwc_mdpcomp.cpp
index 4bdc3e9..f141af8 100644
--- a/libhwcomposer/hwc_mdpcomp.cpp
+++ b/libhwcomposer/hwc_mdpcomp.cpp
@@ -21,11 +21,14 @@
 #include "external.h"
 #include "qdMetaData.h"
 #include "mdp_version.h"
+#include <overlayRotator.h>
+
+using overlay::Rotator;
+using namespace overlay::utils;
+namespace ovutils = overlay::utils;
 
 namespace qhwc {
 
-namespace ovutils = overlay::utils;
-
 //==============MDPComp========================================================
 
 IdleInvalidator *MDPComp::idleInvalidator = NULL;
@@ -188,6 +191,8 @@
             if(mCurrentFrame.pipeLayer[i].pipeInfo) {
                 delete mCurrentFrame.pipeLayer[i].pipeInfo;
                 mCurrentFrame.pipeLayer[i].pipeInfo = NULL;
+                //We dont own the rotator
+                mCurrentFrame.pipeLayer[i].rot = NULL;
             }
         }
         free(mCurrentFrame.pipeLayer);
@@ -196,20 +201,6 @@
     mCurrentFrame.count = 0;
 }
 
-void MDPComp::setVidInfo(hwc_layer_1_t *layer,
-        ovutils::eMdpFlags &mdpFlags) {
-    private_handle_t *hnd = (private_handle_t *)layer->handle;
-    MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
-
-    if(isSecureBuffer(hnd)) {
-        ovutils::setMdpFlags(mdpFlags, ovutils::OV_MDP_SECURE_OVERLAY_SESSION);
-    }
-    if(metadata && (metadata->operation & PP_PARAM_INTERLACED) &&
-                                              metadata->interlaced) {
-        ovutils::setMdpFlags(mdpFlags, ovutils::OV_MDP_DEINTERLACE);
-    }
-}
-
 bool MDPComp::isWidthValid(hwc_context_t *ctx, hwc_layer_1_t *layer) {
 
     const int dpy = HWC_DISPLAY_PRIMARY;
@@ -363,9 +354,7 @@
 
     for (int index = 0 ; index < mCurrentFrame.count; index++) {
         hwc_layer_1_t* layer = &list->hwLayers[index];
-        MdpPipeInfo* cur_pipe = mCurrentFrame.pipeLayer[index].pipeInfo;
-
-        if(configure(ctx, layer, cur_pipe) != 0 ) {
+        if(configure(ctx, layer, mCurrentFrame.pipeLayer[index]) != 0 ) {
             ALOGD_IF(isDebug(), "%s: MDPComp failed to configure overlay for \
                     layer %d",__FUNCTION__, index);
             return false;
@@ -417,134 +406,17 @@
  * Configures pipe(s) for MDP composition
  */
 int MDPCompLowRes::configure(hwc_context_t *ctx, hwc_layer_1_t *layer,
-        MdpPipeInfo* mdpInfo) {
+        PipeLayerPair& pipeLayerPair) {
     const int dpy = HWC_DISPLAY_PRIMARY;
-    private_handle_t *hnd = (private_handle_t *)layer->handle;
-    overlay::Overlay& ov = *ctx->mOverlay;
+    MdpPipeInfoLowRes& mdp_info =
+            *(static_cast<MdpPipeInfoLowRes*>(pipeLayerPair.pipeInfo));
+    eMdpFlags mdpFlags = OV_MDP_BACKEND_COMPOSITION;
+    eZorder zOrder = static_cast<eZorder>(mdp_info.zOrder);
+    eIsFg isFg = IS_FG_OFF;
+    eDest dest = mdp_info.index;
 
-    if(!hnd) {
-        ALOGE("%s: layer handle is NULL", __FUNCTION__);
-        return -1;
-    }
-
-    MdpPipeInfoLowRes& mdp_info = *(MdpPipeInfoLowRes*)mdpInfo;
-
-    int hw_w = ctx->dpyAttr[dpy].xres;
-    int hw_h = ctx->dpyAttr[dpy].yres;
-
-    hwc_rect_t crop = layer->sourceCrop;
-    hwc_rect_t dst = layer->displayFrame;
-
-    int crop_w = crop.right - crop.left;
-    int crop_h = crop.bottom - crop.top;
-
-    int dst_w = dst.right - dst.left;
-    int dst_h = dst.bottom - dst.top;
-
-    if(dst.left < 0 || dst.top < 0 ||
-            dst.right > hw_w || dst.bottom > hw_h) {
-        ALOGD_IF(isDebug(),"%s: Destination has negative coordinates",
-                __FUNCTION__);
-        hwc_rect_t scissor = {0, 0, hw_w, hw_h };
-        qhwc::calculate_crop_rects(crop, dst, scissor, layer->transform);
-
-        //Update calulated width and height
-        crop_w = crop.right - crop.left;
-        crop_h = crop.bottom - crop.top;
-
-        dst_w = dst.right - dst.left;
-        dst_h = dst.bottom - dst.top;
-    }
-
-    if( (dst_w > hw_w)|| (dst_h > hw_h)) {
-        ALOGD_IF(isDebug(),"%s: Dest rect exceeds FB", __FUNCTION__);
-        dst_w = hw_w;
-        dst_h = hw_h;
-    }
-
-    // Determine pipe to set based on pipe index
-    ovutils::eDest dest = mdp_info.index;
-
-    ovutils::eZorder zOrder = ovutils::ZORDER_0;
-
-    if(mdp_info.zOrder == 0 ) {
-        zOrder = ovutils::ZORDER_0;
-    } else if(mdp_info.zOrder == 1 ) {
-        zOrder = ovutils::ZORDER_1;
-    } else if(mdp_info.zOrder == 2 ) {
-        zOrder = ovutils::ZORDER_2;
-    } else if(mdp_info.zOrder == 3) {
-        zOrder = ovutils::ZORDER_3;
-    }
-
-    // Order order order
-    // setSource - just setting source
-    // setParameter - changes src w/h/f accordingly
-    // setCrop - ROI - src_rect
-    // setPosition - dst_rect
-    // commit - commit changes to mdp driver
-    // queueBuffer - not here, happens when draw is called
-
-    ovutils::Whf info(hnd->width, hnd->height, hnd->format, hnd->size);
-
-    ovutils::eMdpFlags mdpFlags = ovutils::OV_MDP_FLAGS_NONE;
-
-    if(isYuvBuffer(hnd))
-        setVidInfo(layer, mdpFlags);
-
-    ovutils::setMdpFlags(mdpFlags,ovutils::OV_MDP_BACKEND_COMPOSITION);
-
-    if(layer->blending == HWC_BLENDING_PREMULT) {
-        ovutils::setMdpFlags(mdpFlags,
-                ovutils::OV_MDP_BLEND_FG_PREMULT);
-    }
-
-    ovutils::eTransform orient = overlay::utils::OVERLAY_TRANSFORM_0 ;
-
-    if(!(layer->transform & HWC_TRANSFORM_ROT_90)) {
-        if(layer->transform & HWC_TRANSFORM_FLIP_H) {
-            ovutils::setMdpFlags(mdpFlags, ovutils::OV_MDP_FLIP_H);
-        }
-
-        if(layer->transform & HWC_TRANSFORM_FLIP_V) {
-            ovutils::setMdpFlags(mdpFlags,  ovutils::OV_MDP_FLIP_V);
-        }
-    } else {
-        orient = static_cast<ovutils::eTransform>(layer->transform);
-    }
-
-    ovutils::eRotFlags rotFlags = ovutils::ROT_FLAGS_NONE;
-    if(isYuvBuffer(hnd) && (ctx->mMDP.version >= qdutils::MDP_V4_2 &&
-                           ctx->mMDP.version < qdutils::MDSS_V5)) {
-        rotFlags = ovutils::ROT_DOWNSCALE_ENABLED;
-    }
-
-    ovutils::PipeArgs parg(mdpFlags,
-            info,
-            zOrder,
-            ovutils::IS_FG_OFF,
-            rotFlags);
-
-    ov.setSource(parg, dest);
-
-    ov.setTransform(orient, dest);
-
-    ovutils::Dim dcrop(crop.left, crop.top, crop_w, crop_h);
-    ov.setCrop(dcrop, dest);
-
-    ovutils::Dim dim(dst.left, dst.top, dst_w, dst_h);
-    ov.setPosition(dim, dest);
-
-    ALOGD_IF(isDebug(),"%s: MDP set: crop[%d,%d,%d,%d] dst[%d,%d,%d,%d] \
-            nPipe: %d zorder: %d",__FUNCTION__, dcrop.x,
-            dcrop.y,dcrop.w, dcrop.h, dim.x, dim.y, dim.w, dim.h,
-            mdp_info.index, mdp_info.zOrder);
-
-    if (!ov.commit(dest)) {
-        ALOGE("%s: commit failed", __FUNCTION__);
-        return -1;
-    }
-    return 0;
+    return configureLowRes(ctx, layer, dpy, mdpFlags, zOrder, isFg, dest,
+            &pipeLayerPair.rot);
 }
 
 int MDPCompLowRes::pipesNeeded(hwc_context_t *ctx,
@@ -572,6 +444,7 @@
             hwc_layer_1_t* layer = &list->hwLayers[nYuvIndex];
             PipeLayerPair& info = currentFrame.pipeLayer[nYuvIndex];
             info.pipeInfo = new MdpPipeInfoLowRes;
+            info.rot = NULL;
             MdpPipeInfoLowRes& pipe_info = *(MdpPipeInfoLowRes*)info.pipeInfo;
             pipe_info.index = getMdpPipe(ctx, MDPCOMP_OV_VG);
             if(pipe_info.index == ovutils::OV_INVALID) {
@@ -592,6 +465,7 @@
 
         PipeLayerPair& info = currentFrame.pipeLayer[index];
         info.pipeInfo = new MdpPipeInfoLowRes;
+        info.rot = NULL;
         MdpPipeInfoLowRes& pipe_info = *(MdpPipeInfoLowRes*)info.pipeInfo;
 
         pipe_info.index = getMdpPipe(ctx, MDPCOMP_OV_ANY);
@@ -628,35 +502,41 @@
     for(int i = 0; i < numHwLayers; i++ )
     {
         hwc_layer_1_t *layer = &list->hwLayers[i];
-
-        if(!(layerProp[i].mFlags & HWC_MDPCOMP)) {
-            continue;
+        private_handle_t *hnd = (private_handle_t *)layer->handle;
+        if(!hnd) {
+            ALOGE("%s handle null", __FUNCTION__);
+            return false;
         }
 
         MdpPipeInfoLowRes& pipe_info =
                 *(MdpPipeInfoLowRes*)mCurrentFrame.pipeLayer[i].pipeInfo;
         ovutils::eDest dest = pipe_info.index;
-
         if(dest == ovutils::OV_INVALID) {
             ALOGE("%s: Invalid pipe index (%d)", __FUNCTION__, dest);
             return false;
         }
 
-        if (ctx ) {
-            private_handle_t *hnd = (private_handle_t *)layer->handle;
-            if(!hnd) {
-                ALOGE("%s handle null", __FUNCTION__);
-                return false;
-            }
+        if(!(layerProp[i].mFlags & HWC_MDPCOMP)) {
+            continue;
+        }
 
-            ALOGD_IF(isDebug(),"%s: MDP Comp: Drawing layer: %p hnd: %p \
-                    using  pipe: %d", __FUNCTION__, layer,
-                    hnd, dest );
+        ALOGD_IF(isDebug(),"%s: MDP Comp: Drawing layer: %p hnd: %p \
+                using  pipe: %d", __FUNCTION__, layer,
+                hnd, dest );
 
-            if (!ov.queueBuffer(hnd->fd, hnd->offset, dest)) {
-                ALOGE("%s: queueBuffer failed for external", __FUNCTION__);
+        int fd = hnd->fd;
+        uint32_t offset = hnd->offset;
+        Rotator *rot = mCurrentFrame.pipeLayer[i].rot;
+        if(rot) {
+            if(!rot->queueBuffer(fd, offset))
                 return false;
-            }
+            fd = rot->getDstMemId();
+            offset = rot->getDstOffset();
+        }
+
+        if (!ov.queueBuffer(fd, offset, dest)) {
+            ALOGE("%s: queueBuffer failed for external", __FUNCTION__);
+            return false;
         }
 
         layerProp[i].mFlags &= ~HWC_MDPCOMP;
@@ -774,212 +654,17 @@
  * Configures pipe(s) for MDP composition
  */
 int MDPCompHighRes::configure(hwc_context_t *ctx, hwc_layer_1_t *layer,
-        MdpPipeInfo* mdpInfo) {
+        PipeLayerPair& pipeLayerPair) {
     const int dpy = HWC_DISPLAY_PRIMARY;
-    private_handle_t *hnd = (private_handle_t *)layer->handle;
-    overlay::Overlay& ov = *ctx->mOverlay;
-
-    if(!hnd) {
-        ALOGE("%s: layer handle is NULL", __FUNCTION__);
-        return -1;
-    }
-
-    MdpPipeInfoHighRes& mdp_info = *(MdpPipeInfoHighRes*)mdpInfo;
-
-    int hw_w = ctx->dpyAttr[dpy].xres;
-    int hw_h = ctx->dpyAttr[dpy].yres;
-
-    hwc_rect_t crop = layer->sourceCrop;
-    hwc_rect_t dst = layer->displayFrame;
-
-    int crop_w = crop.right - crop.left;
-    int crop_h = crop.bottom - crop.top;
-
-    int dst_w = dst.right - dst.left;
-    int dst_h = dst.bottom - dst.top;
-
-    if(dst.left < 0 || dst.top < 0 ||
-            dst.right > hw_w || dst.bottom > hw_h) {
-        ALOGD_IF(isDebug(),"%s: Destination has negative coordinates",
-                __FUNCTION__);
-        hwc_rect_t scissor = {0, 0, hw_w, hw_h };
-        qhwc::calculate_crop_rects(crop, dst, scissor, 0);
-
-        //Update calulated width and height
-        crop_w = crop.right - crop.left;
-        crop_h = crop.bottom - crop.top;
-
-        dst_w = dst.right - dst.left;
-        dst_h = dst.bottom - dst.top;
-    }
-
-    if( (dst_w > hw_w)|| (dst_h > hw_h)) {
-        ALOGD_IF(isDebug(),"%s: Dest rect exceeds FB", __FUNCTION__);
-        dst_w = hw_w;
-        dst_h = hw_h;
-    }
-
-    // Determine pipe to set based on pipe index
-    ovutils::eDest l_dest = mdp_info.lIndex;
-    ovutils::eDest r_dest = mdp_info.rIndex;
-
-    ovutils::eZorder zOrder = ovutils::ZORDER_0;
-
-    if(mdp_info.zOrder == 0 ) {
-        zOrder = ovutils::ZORDER_0;
-    } else if(mdp_info.zOrder == 1 ) {
-        zOrder = ovutils::ZORDER_1;
-    } else if(mdp_info.zOrder == 2 ) {
-        zOrder = ovutils::ZORDER_2;
-    } else if(mdp_info.zOrder == 3) {
-        zOrder = ovutils::ZORDER_3;
-    }
-
-    // Order order order
-    // setSource - just setting source
-    // setParameter - changes src w/h/f accordingly
-    // setCrop - ROI - src_rect
-    // setPosition - dst_rect
-    // commit - commit changes to mdp driver
-    // queueBuffer - not here, happens when draw is called
-
-    ovutils::Whf info(hnd->width, hnd->height, hnd->format, hnd->size);
-
-    ovutils::eMdpFlags mdpFlagsL = ovutils::OV_MDP_FLAGS_NONE;
-
-    if(isYuvBuffer(hnd))
-        setVidInfo(layer, mdpFlagsL);
-
-    ovutils::setMdpFlags(mdpFlagsL,ovutils::OV_MDP_BACKEND_COMPOSITION);
-
-    if(layer->blending == HWC_BLENDING_PREMULT) {
-        ovutils::setMdpFlags(mdpFlagsL,
-                ovutils::OV_MDP_BLEND_FG_PREMULT);
-    }
-
-    ovutils::eTransform orient = overlay::utils::OVERLAY_TRANSFORM_0 ;
-
-    if(!(layer->transform & HWC_TRANSFORM_ROT_90)) {
-        if(layer->transform & HWC_TRANSFORM_FLIP_H) {
-            ovutils::setMdpFlags(mdpFlagsL, ovutils::OV_MDP_FLIP_H);
-        }
-
-        if(layer->transform & HWC_TRANSFORM_FLIP_V) {
-            ovutils::setMdpFlags(mdpFlagsL,  ovutils::OV_MDP_FLIP_V);
-        }
-    } else {
-        orient = static_cast<ovutils::eTransform>(layer->transform);
-    }
-
-    ovutils::eMdpFlags mdpFlagsR = mdpFlagsL;
-    ovutils::setMdpFlags(mdpFlagsR, ovutils::OV_MDSS_MDP_RIGHT_MIXER);
-
-    hwc_rect_t tmp_cropL, tmp_dstL;
-    hwc_rect_t tmp_cropR, tmp_dstR;
-
-    if(l_dest != ovutils::OV_INVALID) {
-        tmp_cropL = crop;
-        tmp_dstL = dst;
-        hwc_rect_t scissor = {0, 0, hw_w/2, hw_h };
-        qhwc::calculate_crop_rects(tmp_cropL, tmp_dstL, scissor, 0);
-    }
-    if(r_dest != ovutils::OV_INVALID) {
-        tmp_cropR = crop;
-        tmp_dstR = dst;
-        hwc_rect_t scissor = {hw_w/2, 0, hw_w, hw_h };
-        qhwc::calculate_crop_rects(tmp_cropR, tmp_dstR, scissor, 0);
-    }
-
-    //When buffer is flipped, contents of mixer config also needs to swapped.
-    //Not needed if the layer is confined to one half of the screen.
-    if(layer->transform & HWC_TRANSFORM_FLIP_V &&
-       l_dest != ovutils::OV_INVALID  && r_dest != ovutils::OV_INVALID ) {
-        hwc_rect_t new_cropR;
-        new_cropR.left = tmp_cropL.left;
-        new_cropR.right = new_cropR.left + (tmp_cropR.right - tmp_cropR.left);
-
-        hwc_rect_t new_cropL;
-        new_cropL.left  = new_cropR.right;
-        new_cropL.right = tmp_cropR.right;
-
-        tmp_cropL.left =  new_cropL.left;
-        tmp_cropL.right =  new_cropL.right;
-
-        tmp_cropR.left = new_cropR.left;
-        tmp_cropR.right =  new_cropR.right;
-
-        ALOGD_IF(isDebug(),"rects on V flip: \
-                 cropL(%d,%d,%d,%d) dstL(%d,%d,%d,%d) \
-                 cropR(%d,%d,%d,%d) dstR(%d,%d,%d,%d)",
-          tmp_cropL.left, tmp_cropL.top, tmp_cropL.right, tmp_cropL.bottom,
-          tmp_dstL.left, tmp_dstL.top, tmp_dstL.right, tmp_dstL.bottom,
-          tmp_cropR.left, tmp_cropR.top, tmp_cropR.right, tmp_cropR.bottom,
-          tmp_dstR.left, tmp_dstR.top, tmp_dstR.right, tmp_dstR.bottom);
-    }
-
-    //**** configure left mixer ****
-    if(l_dest != ovutils::OV_INVALID) {
-        ovutils::PipeArgs pargL(mdpFlagsL,
-                                info,
-                                zOrder,
-                                ovutils::IS_FG_OFF,
-                                ovutils::ROT_FLAGS_NONE);
-
-        ov.setSource(pargL, l_dest);
-        ov.setTransform(orient, l_dest);
-        ovutils::Dim dcropL(tmp_cropL.left, tmp_cropL.top,
-                            tmp_cropL.right - tmp_cropL.left,
-                            tmp_cropL.bottom - tmp_cropL.top);
-        ov.setCrop(dcropL, l_dest);
-        ovutils::Dim dimL(tmp_dstL.left , tmp_dstL.top,
-                          tmp_dstL.right - tmp_dstL.left,
-                          tmp_dstL.bottom - tmp_dstL.top);
-        ov.setPosition(dimL, l_dest);
-
-        ALOGD_IF(isDebug(),"%s: MDP set: LEFT: \
-                 crop[%d,%d,%d,%d] dst[%d,%d,%d,%d] pipeIndexL: %d  zorder: %d",
-                 __FUNCTION__, dcropL.x, dcropL.y,dcropL.w, dcropL.h,
-                 dimL.x, dimL.y, dimL.w, dimL.h,
-                 mdp_info.lIndex, mdp_info.zOrder);
-
-        if (!ov.commit(l_dest)) {
-            ALOGE("%s: commit failed for left mixer config", __FUNCTION__);
-            return -1;
-        }
-    }
-
-    //**** configure right mixer ****
-    if(r_dest != ovutils::OV_INVALID) {
-        ovutils::PipeArgs pargR(mdpFlagsR,
-                                info,
-                                zOrder,
-                                ovutils::IS_FG_OFF,
-                                ovutils::ROT_FLAGS_NONE);
-
-        ov.setSource(pargR, r_dest);
-        ov.setTransform(orient, r_dest);
-        ovutils::Dim dcropR(tmp_cropR.left, tmp_cropR.top,
-                            tmp_cropR.right - tmp_cropR.left,
-                            tmp_cropR.bottom - tmp_cropR.top);
-        ov.setCrop(dcropR, r_dest);
-        ovutils::Dim dimR(tmp_dstR.left - hw_w/2, tmp_dstR.top,
-                          tmp_dstR.right - tmp_dstR.left,
-                          tmp_dstR.bottom - tmp_dstR.top);
-        ov.setPosition(dimR, r_dest);
-
-        ALOGD_IF(isDebug(),"%s: MDP set: RIGHT: \
-                 crop[%d,%d,%d,%d] dst[%d,%d,%d,%d] pipeIndexR: %d zorder: %d",
-                 __FUNCTION__, dcropR.x, dcropR.y,dcropR.w, dcropR.h,
-                 dimR.x, dimR.y, dimR.w, dimR.h,
-                 mdp_info.rIndex, mdp_info.zOrder);
-
-        if (!ov.commit(r_dest)) {
-            ALOGE("%s: commit failed for right mixer config", __FUNCTION__);
-            return -1;
-        }
-    }
-
-    return 0;
+    MdpPipeInfoHighRes& mdp_info =
+            *(static_cast<MdpPipeInfoHighRes*>(pipeLayerPair.pipeInfo));
+    eZorder zOrder = static_cast<eZorder>(mdp_info.zOrder);
+    eIsFg isFg = IS_FG_OFF;
+    eMdpFlags mdpFlagsL = OV_MDP_BACKEND_COMPOSITION;
+    eDest lDest = mdp_info.lIndex;
+    eDest rDest = mdp_info.rIndex;
+    return configureHighRes(ctx, layer, dpy, mdpFlagsL, zOrder, isFg, lDest,
+            rDest, &pipeLayerPair.rot);
 }
 
 bool MDPCompHighRes::draw(hwc_context_t *ctx, hwc_display_contents_1_t* list) {
@@ -1006,6 +691,11 @@
     for(int i = 0; i < numHwLayers; i++ )
     {
         hwc_layer_1_t *layer = &list->hwLayers[i];
+        private_handle_t *hnd = (private_handle_t *)layer->handle;
+        if(!hnd) {
+            ALOGE("%s handle null", __FUNCTION__);
+            return false;
+        }
 
         if(!(layerProp[i].mFlags & HWC_MDPCOMP)) {
             continue;
@@ -1013,50 +703,44 @@
 
         MdpPipeInfoHighRes& pipe_info =
                 *(MdpPipeInfoHighRes*)mCurrentFrame.pipeLayer[i].pipeInfo;
+        Rotator *rot = mCurrentFrame.pipeLayer[i].rot;
+
         ovutils::eDest indexL = pipe_info.lIndex;
         ovutils::eDest indexR = pipe_info.rIndex;
+        int fd = hnd->fd;
+        int offset = hnd->offset;
+
+        if(rot) {
+            rot->queueBuffer(fd, offset);
+            fd = rot->getDstMemId();
+            offset = rot->getDstOffset();
+        }
 
         //************* play left mixer **********
         if(indexL != ovutils::OV_INVALID) {
             ovutils::eDest destL = (ovutils::eDest)indexL;
-            if (ctx ) {
-                private_handle_t *hnd = (private_handle_t *)layer->handle;
-                if(!hnd) {
-                    ALOGE("%s handle null", __FUNCTION__);
-                    return false;
-                }
-
-                ALOGD_IF(isDebug(),"%s: MDP Comp: Drawing layer: %p hnd: %p \
-                        using  pipe: %d", __FUNCTION__, layer, hnd, indexL );
-
-                if (!ov.queueBuffer(hnd->fd, hnd->offset, destL)) {
-                    ALOGE("%s: queueBuffer failed for external", __FUNCTION__);
-                    return false;
-                }
+            ALOGD_IF(isDebug(),"%s: MDP Comp: Drawing layer: %p hnd: %p \
+                    using  pipe: %d", __FUNCTION__, layer, hnd, indexL );
+            if (!ov.queueBuffer(fd, offset, destL)) {
+                ALOGE("%s: queueBuffer failed for left mixer", __FUNCTION__);
+                return false;
             }
         }
 
         //************* play right mixer **********
         if(indexR != ovutils::OV_INVALID) {
             ovutils::eDest destR = (ovutils::eDest)indexR;
-            if (ctx ) {
-                private_handle_t *hnd = (private_handle_t *)layer->handle;
-                if(!hnd) {
-                    ALOGE("%s handle null", __FUNCTION__);
-                    return false;
-                }
-
-                ALOGD_IF(isDebug(),"%s: MDP Comp: Drawing layer: %p hnd: %p \
-                        using  pipe: %d", __FUNCTION__, layer, hnd, indexR );
-
-                if (!ov.queueBuffer(hnd->fd, hnd->offset, destR)) {
-                    ALOGE("%s: queueBuffer failed for external", __FUNCTION__);
-                    return false;
-                }
+            ALOGD_IF(isDebug(),"%s: MDP Comp: Drawing layer: %p hnd: %p \
+                    using  pipe: %d", __FUNCTION__, layer, hnd, indexR );
+            if (!ov.queueBuffer(fd, offset, destR)) {
+                ALOGE("%s: queueBuffer failed for right mixer", __FUNCTION__);
+                return false;
             }
         }
+
         layerProp[i].mFlags &= ~HWC_MDPCOMP;
     }
+
     return true;
 }
 }; //namespace
diff --git a/libhwcomposer/hwc_mdpcomp.h b/libhwcomposer/hwc_mdpcomp.h
index fae460c..926f637 100644
--- a/libhwcomposer/hwc_mdpcomp.h
+++ b/libhwcomposer/hwc_mdpcomp.h
@@ -28,10 +28,13 @@
 #define DEFAULT_IDLE_TIME 2000
 #define MAX_PIPES_PER_MIXER 4
 
+namespace overlay {
+    class Rotator;
+};
+
 namespace qhwc {
 namespace ovutils = overlay::utils;
 
-
 class MDPComp {
 public:
     virtual ~MDPComp(){};
@@ -67,6 +70,7 @@
     struct PipeLayerPair {
         MdpPipeInfo *pipeInfo;
         native_handle_t* handle;
+        overlay::Rotator* rot;
     };
 
     /* introduced for mixed mode implementation */
@@ -83,7 +87,7 @@
                 hwc_display_contents_1_t* list,FrameInfo& current_frame) = 0;
     /* configures MPD pipes */
     virtual int configure(hwc_context_t *ctx, hwc_layer_1_t *layer,
-                        MdpPipeInfo* mdp_info) = 0;
+                PipeLayerPair& pipeLayerPair) = 0;
     /* Is rotation supported */
     virtual bool canRotate(){ return true; };
 
@@ -97,8 +101,6 @@
     eState getState() { return mState; };
     /* reset state */
     void reset( hwc_context_t *ctx, hwc_display_contents_1_t* list );
-    /* configure MDP flags for video buffers */
-    void setVidInfo(hwc_layer_1_t *layer, ovutils::eMdpFlags &mdpFlags);
     /* allocate MDP pipes from overlay */
     ovutils::eDest getMdpPipe(hwc_context_t *ctx, ePipeType type);
     /* checks for conditions where mdpcomp is not possible */
@@ -136,7 +138,7 @@
 
     /* configure's overlay pipes for the frame */
     virtual int configure(hwc_context_t *ctx, hwc_layer_1_t *layer,
-                        MdpPipeInfo* mdp_info);
+            PipeLayerPair& pipeLayerPair);
 
     /* allocates pipes to selected candidates */
     virtual bool allocLayerPipes(hwc_context_t *ctx,
@@ -144,7 +146,7 @@
             FrameInfo& current_frame);
 
     virtual int pipesNeeded(hwc_context_t *ctx,
-                        hwc_display_contents_1_t* list);
+            hwc_display_contents_1_t* list);
 };
 
 class MDPCompHighRes : public MDPComp {
@@ -163,7 +165,7 @@
 
     /* configure's overlay pipes for the frame */
     virtual int configure(hwc_context_t *ctx, hwc_layer_1_t *layer,
-                        MdpPipeInfo* mdp_info);
+            PipeLayerPair& pipeLayerPair);
 
     /* allocates pipes to selected candidates */
     virtual bool allocLayerPipes(hwc_context_t *ctx,
diff --git a/libhwcomposer/hwc_uevents.cpp b/libhwcomposer/hwc_uevents.cpp
index 752aaa2..d27128e 100644
--- a/libhwcomposer/hwc_uevents.cpp
+++ b/libhwcomposer/hwc_uevents.cpp
@@ -27,6 +27,7 @@
 #include <stdlib.h>
 #include "hwc_utils.h"
 #include "hwc_fbupdate.h"
+#include "hwc_video.h"
 #include "hwc_copybit.h"
 #include "comptype.h"
 #include "external.h"
@@ -115,6 +116,11 @@
                     delete ctx->mFBUpdate[dpy];
                     ctx->mFBUpdate[dpy] = NULL;
                 }
+                if(ctx->mVidOv[dpy]) {
+                    Locker::Autolock _l(ctx->mExtSetLock);
+                    delete ctx->mVidOv[dpy];
+                    ctx->mVidOv[dpy] = NULL;
+                }
                 if(ctx->mCopyBit[dpy]){
                     Locker::Autolock _l(ctx->mExtSetLock);
                     delete ctx->mCopyBit[dpy];
@@ -134,6 +140,8 @@
                 ctx->mExtDisplay->processUEventOnline(udata);
                 ctx->mFBUpdate[dpy] =
                         IFBUpdate::getObject(ctx->dpyAttr[dpy].xres, dpy);
+                ctx->mVidOv[dpy] =
+                        IVideoOverlay::getObject(ctx->dpyAttr[dpy].xres, dpy);
                 ctx->dpyAttr[dpy].isPause = false;
                 if(usecopybit)
                     ctx->mCopyBit[dpy] = new CopyBit();
diff --git a/libhwcomposer/hwc_utils.cpp b/libhwcomposer/hwc_utils.cpp
index e9446ad..74ab9e0 100644
--- a/libhwcomposer/hwc_utils.cpp
+++ b/libhwcomposer/hwc_utils.cpp
@@ -25,9 +25,11 @@
 #include <gralloc_priv.h>
 #include <fb_priv.h>
 #include <overlay.h>
+#include <overlayRotator.h>
 #include "hwc_utils.h"
 #include "hwc_mdpcomp.h"
 #include "hwc_fbupdate.h"
+#include "hwc_video.h"
 #include "mdp_version.h"
 #include "hwc_copybit.h"
 #include "external.h"
@@ -38,6 +40,9 @@
 using namespace qClient;
 using namespace qService;
 using namespace android;
+using namespace overlay;
+using namespace overlay::utils;
+namespace ovutils = overlay::utils;
 
 namespace qhwc {
 
@@ -67,6 +72,7 @@
     openFramebufferDevice(ctx);
     overlay::Overlay::initOverlay();
     ctx->mOverlay = overlay::Overlay::getInstance();
+    ctx->mRotMgr = new RotMgr();
     ctx->mMDP.version = qdutils::MDPVersion::getInstance().getMDPVersion();
     ctx->mMDP.hasOverlay = qdutils::MDPVersion::getInstance().hasOverlay();
     ctx->mMDP.panel = qdutils::MDPVersion::getInstance().getPanelType();
@@ -77,6 +83,10 @@
         IFBUpdate::getObject(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].xres,
         HWC_DISPLAY_PRIMARY);
 
+    ctx->mVidOv[HWC_DISPLAY_PRIMARY] =
+        IVideoOverlay::getObject(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].xres,
+        HWC_DISPLAY_PRIMARY);
+
     char value[PROPERTY_VALUE_MAX];
     // Check if the target supports copybit compostion (dyn/mdp/c2d) to
     // decide if we need to open the copybit module.
@@ -120,6 +130,11 @@
         ctx->mOverlay = NULL;
     }
 
+    if(ctx->mRotMgr) {
+        delete ctx->mRotMgr;
+        ctx->mRotMgr = NULL;
+    }
+
     for(int i = 0; i < MAX_DISPLAYS; i++) {
         if(ctx->mCopyBit[i]) {
             delete ctx->mCopyBit[i];
@@ -144,6 +159,10 @@
             delete ctx->mFBUpdate[i];
             ctx->mFBUpdate[i] = NULL;
         }
+        if(ctx->mVidOv[i]) {
+            delete ctx->mVidOv[i];
+            ctx->mVidOv[i] = NULL;
+        }
     }
 
     if(ctx->mMDPComp) {
@@ -492,6 +511,263 @@
     return ret;
 }
 
+void trimLayer(hwc_context_t *ctx, const int& dpy, const int& transform,
+        hwc_rect_t& crop, hwc_rect_t& dst) {
+    int hw_w = ctx->dpyAttr[dpy].xres;
+    int hw_h = ctx->dpyAttr[dpy].yres;
+    if(dst.left < 0 || dst.top < 0 ||
+            dst.right > hw_w || dst.bottom > hw_h) {
+        hwc_rect_t scissor = {0, 0, hw_w, hw_h };
+        qhwc::calculate_crop_rects(crop, dst, scissor, transform);
+    }
+}
+
+void setMdpFlags(hwc_layer_1_t *layer,
+        ovutils::eMdpFlags &mdpFlags,
+        int rotDownscale) {
+    private_handle_t *hnd = (private_handle_t *)layer->handle;
+    MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
+    const int& transform = layer->transform;
+
+    if(layer->blending == HWC_BLENDING_PREMULT) {
+        ovutils::setMdpFlags(mdpFlags,
+                ovutils::OV_MDP_BLEND_FG_PREMULT);
+    }
+
+    if(isYuvBuffer(hnd)) {
+        if(isSecureBuffer(hnd)) {
+            ovutils::setMdpFlags(mdpFlags,
+                    ovutils::OV_MDP_SECURE_OVERLAY_SESSION);
+        }
+        if(metadata && (metadata->operation & PP_PARAM_INTERLACED) &&
+                metadata->interlaced) {
+            ovutils::setMdpFlags(mdpFlags,
+                    ovutils::OV_MDP_DEINTERLACE);
+        }
+        //Pre-rotation will be used using rotator.
+        if(transform & HWC_TRANSFORM_ROT_90) {
+            ovutils::setMdpFlags(mdpFlags,
+                    ovutils::OV_MDP_SOURCE_ROTATED_90);
+        }
+    }
+
+    //No 90 component and no rot-downscale then flips done by MDP
+    //If we use rot then it might as well do flips
+    if(!(layer->transform & HWC_TRANSFORM_ROT_90) && !rotDownscale) {
+        if(layer->transform & HWC_TRANSFORM_FLIP_H) {
+            ovutils::setMdpFlags(mdpFlags, ovutils::OV_MDP_FLIP_H);
+        }
+
+        if(layer->transform & HWC_TRANSFORM_FLIP_V) {
+            ovutils::setMdpFlags(mdpFlags,  ovutils::OV_MDP_FLIP_V);
+        }
+    }
+}
+
+static inline int configRotator(Rotator *rot, const Whf& whf,
+        const eMdpFlags& mdpFlags, const eTransform& orient,
+        const int& downscale) {
+    rot->setSource(whf);
+    rot->setFlags(mdpFlags);
+    rot->setTransform(orient);
+    rot->setDownscale(downscale);
+    if(!rot->commit()) return -1;
+    return 0;
+}
+
+static inline int configMdp(Overlay *ov, const PipeArgs& parg,
+        const eTransform& orient, const hwc_rect_t& crop,
+        const hwc_rect_t& pos, const eDest& dest) {
+    ov->setSource(parg, dest);
+    ov->setTransform(orient, dest);
+
+    int crop_w = crop.right - crop.left;
+    int crop_h = crop.bottom - crop.top;
+    Dim dcrop(crop.left, crop.top, crop_w, crop_h);
+    ov->setCrop(dcrop, dest);
+
+    int posW = pos.right - pos.left;
+    int posH = pos.bottom - pos.top;
+    Dim position(pos.left, pos.top, posW, posH);
+    ov->setPosition(position, dest);
+
+    if (!ov->commit(dest)) {
+        return -1;
+    }
+    return 0;
+}
+
+static inline void updateSource(eTransform& orient, Whf& whf,
+        hwc_rect_t& crop) {
+    Dim srcCrop(crop.left, crop.top,
+            crop.right - crop.left,
+            crop.bottom - crop.top);
+    preRotateSource(orient, whf, srcCrop);
+    crop.left = srcCrop.x;
+    crop.top = srcCrop.y;
+    crop.right = srcCrop.x + srcCrop.w;
+    crop.bottom = srcCrop.y + srcCrop.h;
+}
+
+int configureLowRes(hwc_context_t *ctx, hwc_layer_1_t *layer,
+        const int& dpy, eMdpFlags& mdpFlags, const eZorder& z,
+        const eIsFg& isFg, const eDest& dest, Rotator **rot) {
+
+    private_handle_t *hnd = (private_handle_t *)layer->handle;
+    if(!hnd) {
+        ALOGE("%s: layer handle is NULL", __FUNCTION__);
+        return -1;
+    }
+
+    hwc_rect_t crop = layer->sourceCrop;
+    hwc_rect_t dst = layer->displayFrame;
+    int transform = layer->transform;
+    eTransform orient = static_cast<eTransform>(transform);
+    int downscale = 0;
+    int rotFlags = ovutils::ROT_FLAGS_NONE;
+    Whf whf(hnd->width, hnd->height,
+            getMdpFormat(hnd->format), hnd->size);
+
+    if(isYuvBuffer(hnd) && ctx->mMDP.version >= qdutils::MDP_V4_2 &&
+                ctx->mMDP.version < qdutils::MDSS_V5) {
+        downscale = getDownscaleFactor(
+                crop.right - crop.left,
+                crop.bottom - crop.top,
+                dst.right - dst.left,
+                dst.bottom - dst.top);
+        if(downscale) {
+            rotFlags = ROT_DOWNSCALE_ENABLED;
+        }
+    }
+
+    setMdpFlags(layer, mdpFlags, downscale);
+    trimLayer(ctx, dpy, transform, crop, dst);
+
+    if(isYuvBuffer(hnd) && //if 90 component or downscale, use rot
+            ((transform & HWC_TRANSFORM_ROT_90) || downscale)) {
+        *rot = ctx->mRotMgr->getNext();
+        if(*rot == NULL) return -1;
+        //Configure rotator for pre-rotation
+        if(configRotator(*rot, whf, mdpFlags, orient, downscale) < 0)
+            return -1;
+        whf.format = (*rot)->getDstFormat();
+        updateSource(orient, whf, crop);
+        //For the mdp, since we are pre-rotating
+        transform = 0;
+        rotFlags |= ovutils::ROT_PREROTATED;
+    }
+
+    PipeArgs parg(mdpFlags, whf, z, isFg, static_cast<eRotFlags>(rotFlags));
+    if(configMdp(ctx->mOverlay, parg, orient, crop, dst, dest) < 0) {
+        ALOGE("%s: commit failed for low res panel", __FUNCTION__);
+        return -1;
+    }
+    return 0;
+}
+
+int configureHighRes(hwc_context_t *ctx, hwc_layer_1_t *layer,
+        const int& dpy, eMdpFlags& mdpFlagsL, const eZorder& z,
+        const eIsFg& isFg, const eDest& lDest, const eDest& rDest,
+        Rotator **rot) {
+    private_handle_t *hnd = (private_handle_t *)layer->handle;
+    if(!hnd) {
+        ALOGE("%s: layer handle is NULL", __FUNCTION__);
+        return -1;
+    }
+
+    int hw_w = ctx->dpyAttr[dpy].xres;
+    int hw_h = ctx->dpyAttr[dpy].yres;
+    hwc_rect_t crop = layer->sourceCrop;
+    hwc_rect_t dst = layer->displayFrame;
+    int transform = layer->transform;
+    eTransform orient = static_cast<eTransform>(transform);
+    const int downscale = 0;
+    int rotFlags = ROT_FLAGS_NONE;
+
+    Whf whf(hnd->width, hnd->height,
+            getMdpFormat(hnd->format), hnd->size);
+
+    setMdpFlags(layer, mdpFlagsL);
+    trimLayer(ctx, dpy, transform, crop, dst);
+
+    if(isYuvBuffer(hnd) && (transform & HWC_TRANSFORM_ROT_90)) {
+        (*rot) = ctx->mRotMgr->getNext();
+        if((*rot) == NULL) return -1;
+        //Configure rotator for pre-rotation
+        if(configRotator(*rot, whf, mdpFlagsL, orient, downscale) < 0)
+            return -1;
+        whf.format = (*rot)->getDstFormat();
+        updateSource(orient, whf, crop);
+        //For the mdp, since we are pre-rotating
+        transform = 0;
+        rotFlags |= ROT_PREROTATED;
+    }
+
+    eMdpFlags mdpFlagsR = mdpFlagsL;
+    setMdpFlags(mdpFlagsR, OV_MDSS_MDP_RIGHT_MIXER);
+
+    hwc_rect_t tmp_cropL, tmp_dstL;
+    hwc_rect_t tmp_cropR, tmp_dstR;
+
+    if(lDest != OV_INVALID) {
+        tmp_cropL = crop;
+        tmp_dstL = dst;
+        hwc_rect_t scissor = {0, 0, hw_w/2, hw_h };
+        qhwc::calculate_crop_rects(tmp_cropL, tmp_dstL, scissor, 0);
+    }
+    if(rDest != OV_INVALID) {
+        tmp_cropR = crop;
+        tmp_dstR = dst;
+        hwc_rect_t scissor = {hw_w/2, 0, hw_w, hw_h };
+        qhwc::calculate_crop_rects(tmp_cropR, tmp_dstR, scissor, 0);
+    }
+
+    //When buffer is flipped, contents of mixer config also needs to swapped.
+    //Not needed if the layer is confined to one half of the screen.
+    //If rotator has been used then it has also done the flips, so ignore them.
+    if(layer->transform & HWC_TRANSFORM_FLIP_V && lDest != OV_INVALID
+            && rDest != OV_INVALID && rot == NULL) {
+        hwc_rect_t new_cropR;
+        new_cropR.left = tmp_cropL.left;
+        new_cropR.right = new_cropR.left + (tmp_cropR.right - tmp_cropR.left);
+
+        hwc_rect_t new_cropL;
+        new_cropL.left  = new_cropR.right;
+        new_cropL.right = tmp_cropR.right;
+
+        tmp_cropL.left =  new_cropL.left;
+        tmp_cropL.right =  new_cropL.right;
+
+        tmp_cropR.left = new_cropR.left;
+        tmp_cropR.right =  new_cropR.right;
+
+    }
+
+    //configure left mixer
+    if(lDest != OV_INVALID) {
+        PipeArgs pargL(mdpFlagsL, whf, z, isFg,
+                static_cast<eRotFlags>(rotFlags));
+        if(configMdp(ctx->mOverlay, pargL, orient,
+                tmp_cropL, tmp_dstL, lDest) < 0) {
+            ALOGE("%s: commit failed for left mixer config", __FUNCTION__);
+            return -1;
+        }
+    }
+
+    //configure right mixer
+    if(rDest != OV_INVALID) {
+        PipeArgs pargR(mdpFlagsR, whf, z, isFg,
+                static_cast<eRotFlags>(rotFlags));
+        if(configMdp(ctx->mOverlay, pargR, orient,
+                tmp_cropR, tmp_dstR, rDest) < 0) {
+            ALOGE("%s: commit failed for right mixer config", __FUNCTION__);
+            return -1;
+        }
+    }
+
+    return 0;
+}
+
 void LayerCache::resetLayerCache(int num) {
     for(uint32_t i = 0; i < MAX_NUM_LAYERS; i++) {
         hnd[i] = NULL;
@@ -556,7 +832,6 @@
             }
         }
     }
-
 }
 
-};//namespace
+};//namespace qhwc
diff --git a/libhwcomposer/hwc_utils.h b/libhwcomposer/hwc_utils.h
index de62b05..67745f4 100644
--- a/libhwcomposer/hwc_utils.h
+++ b/libhwcomposer/hwc_utils.h
@@ -27,6 +27,8 @@
 #include <gr.h>
 #include <gralloc_priv.h>
 #include <utils/String8.h>
+#include "qdMetaData.h"
+#include <overlayUtils.h>
 
 #define ALIGN_TO(x, align)     (((x) + ((align)-1)) & ~((align)-1))
 #define LIKELY( exp )       (__builtin_expect( (exp) != 0, true  ))
@@ -44,8 +46,12 @@
 struct hwc_context_t;
 struct framebuffer_device_t;
 
+namespace ovutils = overlay::utils;
+
 namespace overlay {
 class Overlay;
+class Rotator;
+class RotMgr;
 }
 
 namespace qhwc {
@@ -53,6 +59,7 @@
 class QueuedBufferStore;
 class ExternalDisplay;
 class IFBUpdate;
+class IVideoOverlay;
 class MDPComp;
 class CopyBit;
 
@@ -121,9 +128,6 @@
 
 };
 
-
-
-
 // -----------------------------------------------------------------------------
 // Utility functions - implemented in hwc_utils.cpp
 void dumpLayer(hwc_layer_1_t const* l);
@@ -154,7 +158,28 @@
 
 //Sync point impl.
 int hwc_sync(hwc_context_t *ctx, hwc_display_contents_1_t* list, int dpy,
-                                                    int fd);
+        int fd);
+
+//Trims a layer's source crop which is outside of screen boundary.
+void trimLayer(hwc_context_t *ctx, const int& dpy, const int& transform,
+        hwc_rect_t& crop, hwc_rect_t& dst);
+
+//Sets appropriate mdp flags for a layer.
+void setMdpFlags(hwc_layer_1_t *layer,
+        ovutils::eMdpFlags &mdpFlags,
+        int rotDownscale = 0);
+
+//Routine to configure low resolution panels (<= 2048 width)
+int configureLowRes(hwc_context_t *ctx, hwc_layer_1_t *layer, const int& dpy,
+        ovutils::eMdpFlags& mdpFlags, const ovutils::eZorder& z,
+        const ovutils::eIsFg& isFg, const ovutils::eDest& dest,
+        overlay::Rotator **rot);
+
+//Routine to configure high resolution panels (> 2048 width)
+int configureHighRes(hwc_context_t *ctx, hwc_layer_1_t *layer, const int& dpy,
+        ovutils::eMdpFlags& mdpFlags, const ovutils::eZorder& z,
+        const ovutils::eIsFg& isFg, const ovutils::eDest& lDest,
+        const ovutils::eDest& rDest, overlay::Rotator **rot);
 
 // Inline utility functions
 static inline bool isSkipLayer(const hwc_layer_1_t* l) {
@@ -245,9 +270,12 @@
 
     //Overlay object - NULL for non overlay devices
     overlay::Overlay *mOverlay;
+    //Holds a few rot objects
+    overlay::RotMgr *mRotMgr;
 
     //Primary and external FB updater
     qhwc::IFBUpdate *mFBUpdate[MAX_DISPLAYS];
+    qhwc::IVideoOverlay *mVidOv[MAX_DISPLAYS];
     // External display related information
     qhwc::ExternalDisplay *mExtDisplay;
     qhwc::MDPInfo mMDP;
@@ -273,6 +301,7 @@
     bool mDMAInUse;
 };
 
+namespace qhwc {
 static inline bool isSkipPresent (hwc_context_t *ctx, int dpy) {
     return  ctx->listStats[dpy].skipCount;
 }
@@ -280,5 +309,6 @@
 static inline bool isYuvPresent (hwc_context_t *ctx, int dpy) {
     return  ctx->listStats[dpy].yuvCount;
 }
+};
 
 #endif //HWC_UTILS_H
diff --git a/libhwcomposer/hwc_video.cpp b/libhwcomposer/hwc_video.cpp
index edd0ba8..2166a5b 100644
--- a/libhwcomposer/hwc_video.cpp
+++ b/libhwcomposer/hwc_video.cpp
@@ -24,26 +24,36 @@
 #include "hwc_utils.h"
 #include "qdMetaData.h"
 #include "mdp_version.h"
+#include <overlayRotator.h>
+
+using overlay::Rotator;
 
 namespace qhwc {
 
 namespace ovutils = overlay::utils;
 
-//Static Members
-bool VideoOverlay::sIsModeOn[] = {false};
-ovutils::eDest VideoOverlay::sDest[] = {ovutils::OV_INVALID};
+//===========IVideoOverlay=========================
+IVideoOverlay* IVideoOverlay::getObject(const int& width, const int& dpy) {
+    if(width > MAX_DISPLAY_DIM) {
+        return new VideoOverlayHighRes(dpy);
+    }
+    return new VideoOverlayLowRes(dpy);
+}
+
+//===========VideoOverlayLowRes=========================
+
+VideoOverlayLowRes::VideoOverlayLowRes(const int& dpy): IVideoOverlay(dpy) {}
 
 //Cache stats, figure out the state, config overlay
-bool VideoOverlay::prepare(hwc_context_t *ctx, hwc_display_contents_1_t *list,
-        int dpy) {
+bool VideoOverlayLowRes::prepare(hwc_context_t *ctx,
+        hwc_display_contents_1_t *list) {
 
-    if(ctx->listStats[dpy].yuvCount > 1)
+    if(ctx->listStats[mDpy].yuvCount > 1)
         return false;
 
-    int yuvIndex =  ctx->listStats[dpy].yuvIndices[0];
-    sIsModeOn[dpy] = false;
-
-    int hw_w = ctx->dpyAttr[dpy].xres;
+    int yuvIndex =  ctx->listStats[mDpy].yuvIndices[0];
+    int hw_w = ctx->dpyAttr[mDpy].xres;
+    mModeOn = false;
 
     if(hw_w > MAX_DISPLAY_DIM) {
        ALOGD_IF(VIDEO_DEBUG,"%s, \
@@ -61,7 +71,7 @@
        return false;
     }
 
-    if(yuvIndex == -1 || ctx->listStats[dpy].yuvCount != 1) {
+    if(yuvIndex == -1 || ctx->listStats[mDpy].yuvCount != 1) {
         return false;
     }
 
@@ -83,126 +93,55 @@
             }
         }
     }
-    if(configure(ctx, dpy, layer)) {
+
+    if(configure(ctx, layer)) {
         markFlags(layer);
-        sIsModeOn[dpy] = true;
+        mModeOn = true;
     }
 
-    return sIsModeOn[dpy];
+    return mModeOn;
 }
 
-void VideoOverlay::markFlags(hwc_layer_1_t *layer) {
+void VideoOverlayLowRes::markFlags(hwc_layer_1_t *layer) {
     if(layer) {
         layer->compositionType = HWC_OVERLAY;
         layer->hints |= HWC_HINT_CLEAR_FB;
     }
 }
 
-bool VideoOverlay::configure(hwc_context_t *ctx, int dpy,
+bool VideoOverlayLowRes::configure(hwc_context_t *ctx,
         hwc_layer_1_t *layer) {
+
     overlay::Overlay& ov = *(ctx->mOverlay);
     private_handle_t *hnd = (private_handle_t *)layer->handle;
-    ovutils::Whf info(hnd->width, hnd->height, hnd->format, hnd->size);
+    ovutils::Whf info(hnd->width, hnd->height,
+            ovutils::getMdpFormat(hnd->format), hnd->size);
 
     //Request a VG pipe
-    ovutils::eDest dest = ov.nextPipe(ovutils::OV_MDP_PIPE_VG, dpy);
+    ovutils::eDest dest = ov.nextPipe(ovutils::OV_MDP_PIPE_VG, mDpy);
     if(dest == ovutils::OV_INVALID) { //None available
         return false;
     }
 
-    sDest[dpy] = dest;
-
+    mDest = dest;
     ovutils::eMdpFlags mdpFlags = ovutils::OV_MDP_FLAGS_NONE;
-    if (isSecureBuffer(hnd)) {
-        ovutils::setMdpFlags(mdpFlags,
-                ovutils::OV_MDP_SECURE_OVERLAY_SESSION);
+    ovutils::eZorder zOrder = ovutils::ZORDER_1;
+    ovutils::eIsFg isFg = ovutils::IS_FG_OFF;
+    if (ctx->listStats[mDpy].numAppLayers == 1) {
+        isFg = ovutils::IS_FG_SET;
     }
 
-    if(layer->blending == HWC_BLENDING_PREMULT) {
-        ovutils::setMdpFlags(mdpFlags,
-                ovutils::OV_MDP_BLEND_FG_PREMULT);
-    }
-
-    MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
-    if (metadata && (metadata->operation & PP_PARAM_INTERLACED) &&
-                                             metadata->interlaced) {
-        ovutils::setMdpFlags(mdpFlags, ovutils::OV_MDP_DEINTERLACE);
-    }
-
-    ovutils::eIsFg isFgFlag = ovutils::IS_FG_OFF;
-    if (ctx->listStats[dpy].numAppLayers == 1) {
-        isFgFlag = ovutils::IS_FG_SET;
-    }
-
-    ovutils::eRotFlags rotFlags = ovutils::ROT_FLAGS_NONE;
-    if(ctx->mMDP.version >= qdutils::MDP_V4_2 &&
-                ctx->mMDP.version < qdutils::MDSS_V5) {
-        rotFlags = ovutils::ROT_DOWNSCALE_ENABLED;
-    }
-
-    ovutils::PipeArgs parg(mdpFlags,
-            info,
-            ovutils::ZORDER_1,
-            isFgFlag,
-            rotFlags);
-
-    ov.setSource(parg, dest);
-
-    int transform = layer->transform;
-    ovutils::eTransform orient =
-            static_cast<ovutils::eTransform>(transform);
-
-    hwc_rect_t sourceCrop = layer->sourceCrop;
-    hwc_rect_t displayFrame = layer->displayFrame;
-
-    //Calculate the rect for primary based on whether the supplied position
-    //is within or outside bounds.
-    const int fbWidth = ctx->dpyAttr[dpy].xres;
-    const int fbHeight = ctx->dpyAttr[dpy].yres;
-
-    if( displayFrame.left < 0 ||
-            displayFrame.top < 0 ||
-            displayFrame.right > fbWidth ||
-            displayFrame.bottom > fbHeight) {
-        hwc_rect_t scissor = {0, 0, fbWidth, fbHeight};
-        calculate_crop_rects(sourceCrop, displayFrame, scissor, transform);
-    }
-
-    // source crop x,y,w,h
-    ovutils::Dim dcrop(sourceCrop.left, sourceCrop.top,
-            sourceCrop.right - sourceCrop.left,
-            sourceCrop.bottom - sourceCrop.top);
-    //Only for Primary
-    ov.setCrop(dcrop, dest);
-
-    ov.setTransform(orient, dest);
-
-    // position x,y,w,h
-    ovutils::Dim dpos(displayFrame.left,
-            displayFrame.top,
-            displayFrame.right - displayFrame.left,
-            displayFrame.bottom - displayFrame.top);
-    // Calculate the actionsafe dimensions for External(dpy = 1 or 2)
-    if(dpy)
-        getActionSafePosition(ctx, dpy, dpos.x, dpos.y, dpos.w, dpos.h);
-
-    ov.setPosition(dpos, dest);
-
-    if (!ov.commit(dest)) {
-        ALOGE("%s: commit fails", __FUNCTION__);
-        return false;
-    }
-    return true;
+    return (configureLowRes(ctx, layer, mDpy, mdpFlags, zOrder, isFg, dest,
+            &mRot) == 0 );
 }
 
-bool VideoOverlay::draw(hwc_context_t *ctx, hwc_display_contents_1_t *list,
-        int dpy)
-{
-    if(!sIsModeOn[dpy]) {
+bool VideoOverlayLowRes::draw(hwc_context_t *ctx,
+        hwc_display_contents_1_t *list) {
+    if(!mModeOn) {
         return true;
     }
 
-    int yuvIndex = ctx->listStats[dpy].yuvIndices[0];
+    int yuvIndex = ctx->listStats[mDpy].yuvIndices[0];
     if(yuvIndex == -1) {
         return true;
     }
@@ -210,16 +149,148 @@
     private_handle_t *hnd = (private_handle_t *)
             list->hwLayers[yuvIndex].handle;
 
-    bool ret = true;
     overlay::Overlay& ov = *(ctx->mOverlay);
+    int fd = hnd->fd;
+    uint32_t offset = hnd->offset;
+    Rotator *rot = mRot;
 
-    if (!ov.queueBuffer(hnd->fd, hnd->offset,
-                sDest[dpy])) {
-        ALOGE("%s: queueBuffer failed for dpy=%d", __FUNCTION__, dpy);
-        ret = false;
+    if(rot) {
+        if(!rot->queueBuffer(fd, offset))
+            return false;
+        fd = rot->getDstMemId();
+        offset = rot->getDstOffset();
     }
 
-    return ret;
+    if (!ov.queueBuffer(fd, offset, mDest)) {
+        ALOGE("%s: queueBuffer failed for dpy=%d", __FUNCTION__, mDpy);
+        return false;
+    }
+
+    return true;
 }
 
+//===========VideoOverlayHighRes=========================
+
+VideoOverlayHighRes::VideoOverlayHighRes(const int& dpy): IVideoOverlay(dpy) {}
+
+//Cache stats, figure out the state, config overlay
+bool VideoOverlayHighRes::prepare(hwc_context_t *ctx,
+        hwc_display_contents_1_t *list) {
+
+    int yuvIndex =  ctx->listStats[mDpy].yuvIndices[0];
+    int hw_w = ctx->dpyAttr[mDpy].xres;
+    mModeOn = false;
+
+    if(!ctx->mMDP.hasOverlay) {
+       ALOGD_IF(VIDEO_DEBUG,"%s, this hw doesnt support overlay", __FUNCTION__);
+       return false;
+    }
+
+    if(yuvIndex == -1 || ctx->listStats[mDpy].yuvCount != 1) {
+        return false;
+    }
+
+    //index guaranteed to be not -1 at this point
+    hwc_layer_1_t *layer = &list->hwLayers[yuvIndex];
+    if(configure(ctx, layer)) {
+        markFlags(layer);
+        mModeOn = true;
+    }
+
+    return mModeOn;
+}
+
+void VideoOverlayHighRes::markFlags(hwc_layer_1_t *layer) {
+    if(layer) {
+        layer->compositionType = HWC_OVERLAY;
+        layer->hints |= HWC_HINT_CLEAR_FB;
+    }
+}
+
+bool VideoOverlayHighRes::configure(hwc_context_t *ctx,
+        hwc_layer_1_t *layer) {
+
+    int hw_w = ctx->dpyAttr[mDpy].xres;
+    overlay::Overlay& ov = *(ctx->mOverlay);
+    private_handle_t *hnd = (private_handle_t *)layer->handle;
+    ovutils::Whf info(hnd->width, hnd->height,
+            ovutils::getMdpFormat(hnd->format), hnd->size);
+
+    //Request a VG pipe
+    mDestL = ovutils::OV_INVALID;
+    mDestR = ovutils::OV_INVALID;
+    hwc_rect_t dst = layer->displayFrame;
+    if(dst.left > hw_w/2) {
+        mDestR = ov.nextPipe(ovutils::OV_MDP_PIPE_VG, mDpy);
+        if(mDestR == ovutils::OV_INVALID)
+            return false;
+    } else if (dst.right <= hw_w/2) {
+        mDestL = ov.nextPipe(ovutils::OV_MDP_PIPE_VG, mDpy);
+        if(mDestL == ovutils::OV_INVALID)
+            return false;
+    } else {
+        mDestL = ov.nextPipe(ovutils::OV_MDP_PIPE_VG, mDpy);
+        mDestR = ov.nextPipe(ovutils::OV_MDP_PIPE_VG, mDpy);
+        if(mDestL == ovutils::OV_INVALID ||
+                mDestR == ovutils::OV_INVALID)
+            return false;
+    }
+
+    ovutils::eMdpFlags mdpFlags = ovutils::OV_MDP_FLAGS_NONE;
+    ovutils::eZorder zOrder = ovutils::ZORDER_1;
+    ovutils::eIsFg isFg = ovutils::IS_FG_OFF;
+    if (ctx->listStats[mDpy].numAppLayers == 1) {
+        isFg = ovutils::IS_FG_SET;
+    }
+
+    return (configureHighRes(ctx, layer, mDpy, mdpFlags, zOrder, isFg, mDestL,
+            mDestR, &mRot) == 0 );
+}
+
+bool VideoOverlayHighRes::draw(hwc_context_t *ctx,
+        hwc_display_contents_1_t *list) {
+    if(!mModeOn) {
+        return true;
+    }
+
+    int yuvIndex = ctx->listStats[mDpy].yuvIndices[0];
+    if(yuvIndex == -1) {
+        return true;
+    }
+
+    private_handle_t *hnd = (private_handle_t *)
+            list->hwLayers[yuvIndex].handle;
+
+    overlay::Overlay& ov = *(ctx->mOverlay);
+    int fd = hnd->fd;
+    uint32_t offset = hnd->offset;
+    Rotator *rot = mRot;
+
+    if(rot) {
+        if(!rot->queueBuffer(fd, offset))
+            return false;
+        fd = rot->getDstMemId();
+        offset = rot->getDstOffset();
+    }
+
+    if(mDestL != ovutils::OV_INVALID) {
+        if (!ov.queueBuffer(fd, offset, mDestL)) {
+            ALOGE("%s: queueBuffer failed for dpy=%d's left mixer",
+                    __FUNCTION__, mDpy);
+            return false;
+        }
+    }
+
+    if(mDestR != ovutils::OV_INVALID) {
+        if (!ov.queueBuffer(fd, offset, mDestR)) {
+            ALOGE("%s: queueBuffer failed for dpy=%d's right mixer"
+                    , __FUNCTION__, mDpy);
+            return false;
+        }
+    }
+
+    return true;
+}
+
+
 }; //namespace qhwc
diff --git a/libhwcomposer/hwc_video.h b/libhwcomposer/hwc_video.h
index 93cdaa5..4acde9d 100644
--- a/libhwcomposer/hwc_video.h
+++ b/libhwcomposer/hwc_video.h
@@ -23,38 +23,80 @@
 #define LIKELY( exp )       (__builtin_expect( (exp) != 0, true  ))
 #define UNLIKELY( exp )     (__builtin_expect( (exp) != 0, false ))
 
+namespace overlay {
+    class Rotator;
+}
+
 namespace qhwc {
 namespace ovutils = overlay::utils;
 
-//Feature for using overlay to display videos.
-class VideoOverlay {
+class IVideoOverlay {
 public:
-    //Sets up members and prepares overlay if conditions are met
-    static bool prepare(hwc_context_t *ctx, hwc_display_contents_1_t *list,
-            int dpy);
-    //Draws layer if this feature is on
-    static bool draw(hwc_context_t *ctx, hwc_display_contents_1_t *list,
-            int dpy);
-    //resets values
-    static void reset();
-private:
-    //Configures overlay for video prim and ext
-    static bool configure(hwc_context_t *ctx, int dpy,
-            hwc_layer_1_t *yuvlayer);
-
-    //Marks layer flags if this feature is used
-    static void markFlags(hwc_layer_1_t *yuvLayer);
-    //Flags if this feature is on.
-    static bool sIsModeOn[MAX_DISPLAYS];
-    static ovutils::eDest sDest[MAX_DISPLAYS];
+    explicit IVideoOverlay(const int& dpy) : mDpy(dpy), mModeOn(false),
+            mRot(NULL) {}
+    virtual ~IVideoOverlay() {};
+    virtual bool prepare(hwc_context_t *ctx,
+            hwc_display_contents_1_t *list) = 0;
+    virtual bool draw(hwc_context_t *ctx,
+            hwc_display_contents_1_t *list) = 0;
+    virtual void reset() = 0;
+    //Factory method that returns a low-res or high-res version
+    static IVideoOverlay *getObject(const int& width, const int& dpy);
+protected:
+    const int mDpy; // display to update
+    bool mModeOn; // if prepare happened
+    overlay::Rotator *mRot;
 };
 
-inline void VideoOverlay::reset() {
-    for(uint32_t i = 0; i < MAX_DISPLAYS; i++) {
-        sIsModeOn[i] = false;
-        sDest[i] = ovutils::OV_INVALID;
-    }
+class VideoOverlayLowRes : public IVideoOverlay {
+public:
+    explicit VideoOverlayLowRes(const int& dpy);
+    virtual ~VideoOverlayLowRes() {};
+    bool prepare(hwc_context_t *ctx, hwc_display_contents_1_t *list);
+    bool draw(hwc_context_t *ctx, hwc_display_contents_1_t *list);
+    void reset();
+private:
+    //Configures overlay for video prim and ext
+    bool configure(hwc_context_t *ctx, hwc_layer_1_t *yuvlayer);
+    //Marks layer flags if this feature is used
+    void markFlags(hwc_layer_1_t *yuvLayer);
+    //Flags if this feature is on.
+    bool mModeOn;
+    ovutils::eDest mDest;
+};
+
+class VideoOverlayHighRes : public IVideoOverlay {
+public:
+    explicit VideoOverlayHighRes(const int& dpy);
+    virtual ~VideoOverlayHighRes() {};
+    bool prepare(hwc_context_t *ctx, hwc_display_contents_1_t *list);
+    bool draw(hwc_context_t *ctx, hwc_display_contents_1_t *list);
+    void reset();
+private:
+    //Configures overlay for video prim and ext
+    bool configure(hwc_context_t *ctx, hwc_layer_1_t *yuvlayer);
+    //Marks layer flags if this feature is used
+    void markFlags(hwc_layer_1_t *yuvLayer);
+    //Flags if this feature is on.
+    bool mModeOn;
+    ovutils::eDest mDestL;
+    ovutils::eDest mDestR;
+};
+
+//=================Inlines======================
+inline void VideoOverlayLowRes::reset() {
+    mModeOn = false;
+    mDest = ovutils::OV_INVALID;
+    mRot = NULL;
 }
+
+inline void VideoOverlayHighRes::reset() {
+    mModeOn = false;
+    mDestL = ovutils::OV_INVALID;
+    mDestR = ovutils::OV_INVALID;
+    mRot = NULL;
+}
+
 }; //namespace qhwc
 
 #endif //HWC_VIDEO_H