Merge "hwc: Fix code handling downscale limitation"
diff --git a/libgralloc/framebuffer.cpp b/libgralloc/framebuffer.cpp
index d95b376..3828680 100644
--- a/libgralloc/framebuffer.cpp
+++ b/libgralloc/framebuffer.cpp
@@ -86,11 +86,13 @@
 {
     private_module_t* m =
         reinterpret_cast<private_module_t*>(dev->common.module);
-    struct mdp_display_commit prim_commit;
-    memset(&prim_commit, 0, sizeof(struct mdp_display_commit));
-    prim_commit.wait_for_finish = 1;
-    if (ioctl(m->framebuffer->fd, MSMFB_DISPLAY_COMMIT, &prim_commit) == -1) {
-        ALOGE("%s: MSMFB_DISPLAY_COMMIT for primary failed, str: %s",
+    private_handle_t *hnd = static_cast<private_handle_t*>
+        (const_cast<native_handle_t*>(buffer));
+    const size_t offset = hnd->base - m->framebuffer->base;
+    m->info.activate = FB_ACTIVATE_VBL;
+    m->info.yoffset = offset / m->finfo.line_length;
+    if (ioctl(m->framebuffer->fd, FBIOPUT_VSCREENINFO, &m->info) == -1) {
+        ALOGE("%s: FBIOPUT_VSCREENINFO for primary failed, str: %s",
                 __FUNCTION__, strerror(errno));
         return -errno;
     }
@@ -308,7 +310,7 @@
      */
 
     int err;
-    module->numBuffers = 2;
+    module->numBuffers = info.yres_virtual / info.yres;
     module->bufferMask = 0;
     //adreno needs page aligned offsets. Align the fbsize to pagesize.
     size_t fbSize = roundUpToPageSize(finfo.line_length * info.yres)*
@@ -325,6 +327,10 @@
     module->framebuffer->base = intptr_t(vaddr);
     memset(vaddr, 0, fbSize);
     module->currentOffset = 0;
+    //Enable vsync
+    int enable = 1;
+    ioctl(module->framebuffer->fd, MSMFB_OVERLAY_VSYNC_CTRL,
+             &enable);
     return 0;
 }
 
diff --git a/libgralloc/gpu.cpp b/libgralloc/gpu.cpp
index 60b57fe..b8ee1f2 100644
--- a/libgralloc/gpu.cpp
+++ b/libgralloc/gpu.cpp
@@ -151,6 +151,76 @@
     }
 }
 
+int gpu_context_t::gralloc_alloc_framebuffer_locked(size_t size, int usage,
+                                                    buffer_handle_t* pHandle)
+{
+    private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
+
+    // we don't support framebuffer allocations with graphics heap flags
+    if (usage & GRALLOC_HEAP_MASK) {
+        return -EINVAL;
+    }
+
+    if (m->framebuffer == NULL) {
+        ALOGE("%s: Invalid framebuffer", __FUNCTION__);
+        return -EINVAL;
+    }
+
+    const uint32_t bufferMask = m->bufferMask;
+    const uint32_t numBuffers = m->numBuffers;
+    size_t bufferSize = m->finfo.line_length * m->info.yres;
+
+    //adreno needs FB size to be page aligned
+    bufferSize = roundUpToPageSize(bufferSize);
+
+    if (numBuffers == 1) {
+        // If we have only one buffer, we never use page-flipping. Instead,
+        // we return a regular buffer which will be memcpy'ed to the main
+        // screen when post is called.
+        int newUsage = (usage & ~GRALLOC_USAGE_HW_FB) | GRALLOC_USAGE_HW_2D;
+        return gralloc_alloc_buffer(bufferSize, newUsage, pHandle, BUFFER_TYPE_UI,
+                                    m->fbFormat, m->info.xres, m->info.yres);
+    }
+
+    if (bufferMask >= ((1LU<<numBuffers)-1)) {
+        // We ran out of buffers.
+        return -ENOMEM;
+    }
+
+    // create a "fake" handle for it
+    intptr_t vaddr = intptr_t(m->framebuffer->base);
+    private_handle_t* hnd = new private_handle_t(
+        dup(m->framebuffer->fd), bufferSize,
+        private_handle_t::PRIV_FLAGS_USES_PMEM |
+        private_handle_t::PRIV_FLAGS_FRAMEBUFFER,
+        BUFFER_TYPE_UI, m->fbFormat, m->info.xres,
+        m->info.yres);
+
+    // find a free slot
+    for (uint32_t i=0 ; i<numBuffers ; i++) {
+        if ((bufferMask & (1LU<<i)) == 0) {
+            m->bufferMask |= (1LU<<i);
+            break;
+        }
+        vaddr += bufferSize;
+    }
+    hnd->base = vaddr;
+    hnd->offset = vaddr - intptr_t(m->framebuffer->base);
+    *pHandle = hnd;
+    return 0;
+}
+
+
+int gpu_context_t::gralloc_alloc_framebuffer(size_t size, int usage,
+                                             buffer_handle_t* pHandle)
+{
+    private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
+    pthread_mutex_lock(&m->lock);
+    int err = gralloc_alloc_framebuffer_locked(size, usage, pHandle);
+    pthread_mutex_unlock(&m->lock);
+    return err;
+}
+
 int gpu_context_t::alloc_impl(int w, int h, int format, int usage,
                               buffer_handle_t* pHandle, int* pStride,
                               size_t bufferSize) {
@@ -187,8 +257,22 @@
         bufferType = BUFFER_TYPE_VIDEO;
     }
 
-    int err = gralloc_alloc_buffer(size, usage, pHandle, bufferType,
-            grallocFormat, alignedw, alignedh);
+    bool useFbMem = false;
+    char property[PROPERTY_VALUE_MAX];
+    if((usage & GRALLOC_USAGE_HW_FB) &&
+       (property_get("debug.gralloc.map_fb_memory", property, NULL) > 0) &&
+       (!strncmp(property, "1", PROPERTY_VALUE_MAX ) ||
+        (!strncasecmp(property,"true", PROPERTY_VALUE_MAX )))) {
+        useFbMem = true;
+    }
+
+    int err = 0;
+    if(useFbMem) {
+        err = gralloc_alloc_framebuffer(size, usage, pHandle);
+    } else {
+        err = gralloc_alloc_buffer(size, usage, pHandle, bufferType,
+                                   grallocFormat, alignedw, alignedh);
+    }
 
     if (err < 0) {
         return err;
@@ -200,21 +284,26 @@
 
 int gpu_context_t::free_impl(private_handle_t const* hnd) {
     private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
+    if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) {
+        const size_t bufferSize = m->finfo.line_length * m->info.yres;
+        int index = (hnd->base - m->framebuffer->base) / bufferSize;
+        m->bufferMask &= ~(1<<index);
+    } else {
 
-    terminateBuffer(&m->base, const_cast<private_handle_t*>(hnd));
-    IMemAlloc* memalloc = mAllocCtrl->getAllocator(hnd->flags);
-    int err = memalloc->free_buffer((void*)hnd->base, (size_t) hnd->size,
-                                    hnd->offset, hnd->fd);
-    if(err)
-        return err;
-    // free the metadata space
-    unsigned long size = ROUND_UP_PAGESIZE(sizeof(MetaData_t));
-    err = memalloc->free_buffer((void*)hnd->base_metadata,
-                                (size_t) size, hnd->offset_metadata,
-                                hnd->fd_metadata);
-    if (err)
-        return err;
-
+        terminateBuffer(&m->base, const_cast<private_handle_t*>(hnd));
+        IMemAlloc* memalloc = mAllocCtrl->getAllocator(hnd->flags);
+        int err = memalloc->free_buffer((void*)hnd->base, (size_t) hnd->size,
+                                        hnd->offset, hnd->fd);
+        if(err)
+            return err;
+        // free the metadata space
+        unsigned long size = ROUND_UP_PAGESIZE(sizeof(MetaData_t));
+        err = memalloc->free_buffer((void*)hnd->base_metadata,
+                                    (size_t) size, hnd->offset_metadata,
+                                    hnd->fd_metadata);
+        if (err)
+            return err;
+    }
     delete hnd;
     return 0;
 }
diff --git a/libgralloc/gpu.h b/libgralloc/gpu.h
index 6826ffe..8712c6c 100644
--- a/libgralloc/gpu.h
+++ b/libgralloc/gpu.h
@@ -51,6 +51,11 @@
                              int format, int usage,
                              buffer_handle_t* pHandle,
                              int* pStride);
+    int gralloc_alloc_framebuffer_locked(size_t size, int usage,
+                                         buffer_handle_t* pHandle);
+
+    int gralloc_alloc_framebuffer(size_t size, int usage,
+                                  buffer_handle_t* pHandle);
 
     static int gralloc_free(alloc_device_t* dev, buffer_handle_t handle);
 
diff --git a/libhwcomposer/hwc.cpp b/libhwcomposer/hwc.cpp
index 8e7afa2..4f26191 100644
--- a/libhwcomposer/hwc.cpp
+++ b/libhwcomposer/hwc.cpp
@@ -38,6 +38,8 @@
 #include "profiler.h"
 
 using namespace qhwc;
+using namespace overlay;
+
 #define VSYNC_DEBUG 0
 #define BLANK_DEBUG 0
 
@@ -200,7 +202,7 @@
 
     ctx->mOverlay->configBegin();
     ctx->mRotMgr->configBegin();
-    ctx->mNeedsRotator = false;
+    Overlay::setDMAMode(Overlay::DMA_LINE_MODE);
 
     for (int32_t i = numDisplays; i >= 0; i--) {
         hwc_display_contents_1_t *list = displays[i];
diff --git a/libhwcomposer/hwc_mdpcomp.cpp b/libhwcomposer/hwc_mdpcomp.cpp
index 8935041..b81eb5e 100644
--- a/libhwcomposer/hwc_mdpcomp.cpp
+++ b/libhwcomposer/hwc_mdpcomp.cpp
@@ -23,7 +23,7 @@
 #include "mdp_version.h"
 #include <overlayRotator.h>
 
-using overlay::Rotator;
+using namespace overlay;
 using namespace overlay::utils;
 namespace ovutils = overlay::utils;
 
@@ -103,9 +103,10 @@
     }
 
     sMaxPipesPerMixer = MAX_PIPES_PER_MIXER;
-    if(property_get("debug.mdpcomp.maxpermixer", property, NULL) > 0) {
-        if(atoi(property) != 0)
-            sMaxPipesPerMixer = true;
+    if(property_get("debug.mdpcomp.maxpermixer", property, "-1") > 0) {
+        int val = atoi(property);
+        if(val >= 0)
+            sMaxPipesPerMixer = min(val, MAX_PIPES_PER_MIXER);
     }
 
     unsigned long idle_timeout = DEFAULT_IDLE_TIME;
@@ -289,7 +290,6 @@
     case MDPCOMP_OV_DMA:
         mdp_pipe = ov.nextPipe(ovutils::OV_MDP_PIPE_DMA, mDpy);
         if(mdp_pipe != ovutils::OV_INVALID) {
-            ctx->mDMAInUse = true;
             return mdp_pipe;
         }
     case MDPCOMP_OV_ANY:
@@ -350,7 +350,7 @@
         return false;
     }
 
-    if(mdpCount > (sMaxPipesPerMixer-fbNeeded)) {
+    if(mdpCount > (sMaxPipesPerMixer - fbNeeded)) {
         ALOGD_IF(isDebug(), "%s: Exceeds MAX_PIPES_PER_MIXER",__FUNCTION__);
         return false;
     }
@@ -400,11 +400,6 @@
         return false;
     }
 
-    if(ctx->mNeedsRotator && ctx->mDMAInUse) {
-        ALOGE("%s: No DMA for Rotator",__FUNCTION__);
-        return false;
-    }
-
     if(isSecuring(ctx, layer)) {
         ALOGD_IF(isDebug(), "%s: MDP securing is active", __FUNCTION__);
         return false;
@@ -472,6 +467,7 @@
     ALOGD_IF(isDebug(),"%s: cached count: %d",__FUNCTION__,
              mCurrentFrame.fbCount);
 }
+
 void MDPComp::updateLayerCache(hwc_context_t* ctx,
                                hwc_display_contents_1_t* list) {
 
@@ -505,7 +501,7 @@
     int numAvailable = ov.availablePipes(mDpy);
 
     //Reserve DMA for rotator
-    if(ctx->mNeedsRotator)
+    if(Overlay::getDMAMode() == Overlay::DMA_BLOCK_MODE)
         numAvailable -= numDMAPipes;
 
     //Reserve pipe(s)for FB
@@ -548,7 +544,6 @@
 
 int MDPComp::programMDP(hwc_context_t *ctx, hwc_display_contents_1_t* list) {
     int fbZOrder = -1;
-    ctx->mDMAInUse = false;
 
     if(!allocLayerPipes(ctx, list)) {
         ALOGD_IF(isDebug(), "%s: Unable to allocate MDP pipes", __FUNCTION__);
@@ -722,8 +717,9 @@
 
         ePipeType type = MDPCOMP_OV_ANY;
 
-        if(!qhwc::needsScaling(layer) && !ctx->mNeedsRotator
-           && ctx->mMDP.version >= qdutils::MDSS_V5) {
+        if(!qhwc::needsScaling(layer)
+            && Overlay::getDMAMode() != Overlay::DMA_BLOCK_MODE
+            && ctx->mMDP.version >= qdutils::MDSS_V5) {
             type = MDPCOMP_OV_DMA;
         }
 
@@ -893,8 +889,9 @@
 
         ePipeType type = MDPCOMP_OV_ANY;
 
-        if(!qhwc::needsScaling(layer) && !ctx->mNeedsRotator
-           && ctx->mMDP.version >= qdutils::MDSS_V5)
+        if(!qhwc::needsScaling(layer)
+            && Overlay::getDMAMode() != Overlay::DMA_BLOCK_MODE
+            && ctx->mMDP.version >= qdutils::MDSS_V5)
             type = MDPCOMP_OV_DMA;
 
         if(!acquireMDPPipes(ctx, layer, pipe_info, type)) {
diff --git a/libhwcomposer/hwc_utils.cpp b/libhwcomposer/hwc_utils.cpp
index 8299200..a578bb0 100644
--- a/libhwcomposer/hwc_utils.cpp
+++ b/libhwcomposer/hwc_utils.cpp
@@ -332,8 +332,9 @@
             ctx->listStats[dpy].yuvIndices[yuvCount] = i;
             yuvCount++;
 
-            if(layer->transform & HWC_TRANSFORM_ROT_90)
-                ctx->mNeedsRotator = true;
+            if(layer->transform & HWC_TRANSFORM_ROT_90) {
+                Overlay::setDMAMode(Overlay::DMA_BLOCK_MODE);
+            }
         }
         if(layer->blending == HWC_BLENDING_PREMULT)
             ctx->listStats[dpy].preMultipliedAlpha = true;
@@ -678,8 +679,6 @@
     Dim srcCrop(crop.left, crop.top,
             crop.right - crop.left,
             crop.bottom - crop.top);
-    //getMdpOrient will switch the flips if the source is 90 rotated.
-    //Clients in Android dont factor in 90 rotation while deciding the flip.
     orient = static_cast<eTransform>(ovutils::getMdpOrient(orient));
     preRotateSource(orient, whf, srcCrop);
     crop.left = srcCrop.x;
diff --git a/libhwcomposer/hwc_utils.h b/libhwcomposer/hwc_utils.h
index 8c51b38..001e3e8 100644
--- a/libhwcomposer/hwc_utils.h
+++ b/libhwcomposer/hwc_utils.h
@@ -274,10 +274,6 @@
     mutable Locker mExtSetLock;
     //Vsync
     struct vsync_state vstate;
-    //DMA used for rotator
-    bool mDMAInUse;
-    //MDP rotater needed
-    bool mNeedsRotator;
 };
 
 namespace qhwc {
diff --git a/liboverlay/overlay.cpp b/liboverlay/overlay.cpp
index 1faeadf..43b6589 100644
--- a/liboverlay/overlay.cpp
+++ b/liboverlay/overlay.cpp
@@ -92,6 +92,11 @@
             if((mPipeBook[i].mDisplay == PipeBook::DPY_UNUSED ||
                     mPipeBook[i].mDisplay == dpy) &&
                     PipeBook::isNotAllocated(i)) {
+                //In block mode we don't allow line operations
+                if(sDMAMode == DMA_BLOCK_MODE &&
+                    PipeBook::getPipeType((eDest)i) == OV_MDP_PIPE_DMA)
+                    continue;
+
                 dest = (eDest)i;
                 PipeBook::setAllocation(i);
                 break;
@@ -311,6 +316,7 @@
 
 Overlay* Overlay::sInstance = 0;
 int Overlay::sExtFbIndex = 1;
+int Overlay::sDMAMode = DMA_LINE_MODE;
 int Overlay::PipeBook::NUM_PIPES = 0;
 int Overlay::PipeBook::sPipeUsageBitmap = 0;
 int Overlay::PipeBook::sLastUsageBitmap = 0;
diff --git a/liboverlay/overlay.h b/liboverlay/overlay.h
index fdeebc2..0cedac9 100644
--- a/liboverlay/overlay.h
+++ b/liboverlay/overlay.h
@@ -40,6 +40,8 @@
 
 class Overlay : utils::NoCopy {
 public:
+    enum { DMA_BLOCK_MODE, DMA_LINE_MODE };
+
     /* dtor close */
     ~Overlay();
 
@@ -84,6 +86,8 @@
      * to populate.
      */
     void getDump(char *buf, size_t len);
+    static void setDMAMode(const int& mode);
+    static int getDMAMode();
 
 private:
     /* Ctor setup */
@@ -148,6 +152,7 @@
     /* Singleton Instance*/
     static Overlay *sInstance;
     static int sExtFbIndex;
+    static int sDMAMode;
 };
 
 inline void Overlay::validate(int index) {
@@ -176,6 +181,15 @@
     return sExtFbIndex;
 }
 
+inline void Overlay::setDMAMode(const int& mode) {
+    if(mode == DMA_LINE_MODE || mode == DMA_BLOCK_MODE)
+        sDMAMode = mode;
+}
+
+inline int Overlay::getDMAMode() {
+    return sDMAMode;
+}
+
 inline bool Overlay::PipeBook::valid() {
     return (mPipe != NULL);
 }
diff --git a/liboverlay/overlayMdp.cpp b/liboverlay/overlayMdp.cpp
index 84c0e2c..96cb56e 100644
--- a/liboverlay/overlayMdp.cpp
+++ b/liboverlay/overlayMdp.cpp
@@ -117,8 +117,6 @@
 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);
 }
 
diff --git a/liboverlay/overlayMdpRot.cpp b/liboverlay/overlayMdpRot.cpp
index d1e036c..ecf31fa 100755
--- a/liboverlay/overlayMdpRot.cpp
+++ b/liboverlay/overlayMdpRot.cpp
@@ -104,8 +104,6 @@
 {
     int r = utils::getMdpOrient(rot);
     setRotations(r);
-    //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>(r);
     ALOGE_IF(DEBUG_OVERLAY, "%s: r=%d", __FUNCTION__, r);
 }
diff --git a/liboverlay/overlayMdssRot.cpp b/liboverlay/overlayMdssRot.cpp
index cf6d6fa..b9a22a9 100644
--- a/liboverlay/overlayMdssRot.cpp
+++ b/liboverlay/overlayMdssRot.cpp
@@ -98,8 +98,6 @@
     int flags = utils::getMdpOrient(rot);
     if (flags != -1)
         setRotations(flags);
-    //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>(flags);
     ALOGE_IF(DEBUG_OVERLAY, "%s: rot=%d", __FUNCTION__, flags);
 }
diff --git a/liboverlay/overlayUtils.cpp b/liboverlay/overlayUtils.cpp
index 898132f..edf3cec 100644
--- a/liboverlay/overlayUtils.cpp
+++ b/liboverlay/overlayUtils.cpp
@@ -180,6 +180,40 @@
     return -1;
 }
 
+int getMdpOrient(eTransform rotation) {
+    int retTrans = 0;
+    bool trans90 = false;
+    int mdpVersion = qdutils::MDPVersion::getInstance().getMDPVersion();
+    bool aFamily = (mdpVersion < qdutils::MDSS_V5);
+
+    ALOGD_IF(DEBUG_OVERLAY, "%s: In rotation = %d", __FUNCTION__, rotation);
+    if(rotation & OVERLAY_TRANSFORM_ROT_90) {
+        retTrans |= MDP_ROT_90;
+        trans90 = true;
+    }
+
+    if(rotation & OVERLAY_TRANSFORM_FLIP_H) {
+        if(trans90 && aFamily) {
+            //Swap for a-family, since its driver does 90 first
+            retTrans |= MDP_FLIP_UD;
+        } else {
+            retTrans |= MDP_FLIP_LR;
+        }
+    }
+
+    if(rotation & OVERLAY_TRANSFORM_FLIP_V) {
+        if(trans90 && aFamily) {
+            //Swap for a-family, since its driver does 90 first
+            retTrans |= MDP_FLIP_LR;
+        } else {
+            retTrans |= MDP_FLIP_UD;
+        }
+    }
+
+    ALOGD_IF(DEBUG_OVERLAY, "%s: Out rotation = %d", __FUNCTION__, retTrans);
+    return retTrans;
+}
+
 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;
@@ -215,9 +249,6 @@
     return x - ( y + z );
 }
 
-//Expects transform to be adjusted for clients of Android.
-//i.e flips switched if 90 component present.
-//See getMdpOrient()
 void preRotateSource(const eTransform& tr, Whf& whf, Dim& srcCrop) {
     if(tr & OVERLAY_TRANSFORM_FLIP_H) {
         srcCrop.x = compute(whf.w, srcCrop.x, srcCrop.w);
diff --git a/liboverlay/overlayUtils.h b/liboverlay/overlayUtils.h
index c189bb7..ceb238d 100644
--- a/liboverlay/overlayUtils.h
+++ b/liboverlay/overlayUtils.h
@@ -47,7 +47,7 @@
 
 // Older platforms do not support Venus
 #ifndef VENUS_COLOR_FORMAT
-#define MDP_Y_CBCR_H2V2_VENUS (MDP_IMGTYPE_LIMIT2 + 1)
+#define MDP_Y_CBCR_H2V2_VENUS MDP_IMGTYPE_LIMIT
 #endif
 
 /*
@@ -488,43 +488,47 @@
 }
 
 inline const char* getFormatString(int format){
-    static const char* const formats[] = {
-        "MDP_RGB_565",
-        "MDP_XRGB_8888",
-        "MDP_Y_CBCR_H2V2",
-        "MDP_Y_CBCR_H2V2_ADRENO",
-        "MDP_ARGB_8888",
-        "MDP_RGB_888",
-        "MDP_Y_CRCB_H2V2",
-        "MDP_YCRYCB_H2V1",
-        "MDP_Y_CRCB_H2V1",
-        "MDP_Y_CBCR_H2V1",
-        "MDP_Y_CRCB_H1V2",
-        "MDP_Y_CBCR_H1V2",
-        "MDP_RGBA_8888",
-        "MDP_BGRA_8888",
-        "MDP_RGBX_8888",
-        "MDP_Y_CRCB_H2V2_TILE",
-        "MDP_Y_CBCR_H2V2_TILE",
-        "MDP_Y_CR_CB_H2V2",
-        "MDP_Y_CR_CB_GH2V2",
-        "MDP_Y_CB_CR_H2V2",
-        "MDP_Y_CRCB_H1V1",
-        "MDP_Y_CBCR_H1V1",
-        "MDP_YCRCB_H1V1",
-        "MDP_YCBCR_H1V1",
-        "MDP_BGR_565",
-        "MDP_BGR_888",
-        "MDP_Y_CBCR_H2V2_VENUS",
-        "MDP_IMGTYPE_LIMIT",
-        "MDP_RGB_BORDERFILL",
-        "MDP_FB_FORMAT",
-        "MDP_IMGTYPE_LIMIT2"
-    };
-    if(format < 0 || format >= (int)(sizeof(formats) / sizeof(formats[0]))) {
+    #define STR(f) #f;
+    static const char* formats[MDP_IMGTYPE_LIMIT + 1] = {0};
+    formats[MDP_RGB_565] = STR(MDP_RGB_565);
+    formats[MDP_XRGB_8888] = STR(MDP_XRGB_8888);
+    formats[MDP_Y_CBCR_H2V2] = STR(MDP_Y_CBCR_H2V2);
+    formats[MDP_Y_CBCR_H2V2_ADRENO] = STR(MDP_Y_CBCR_H2V2_ADRENO);
+    formats[MDP_ARGB_8888] = STR(MDP_ARGB_8888);
+    formats[MDP_RGB_888] = STR(MDP_RGB_888);
+    formats[MDP_Y_CRCB_H2V2] = STR(MDP_Y_CRCB_H2V2);
+    formats[MDP_YCRYCB_H2V1] = STR(MDP_YCRYCB_H2V1);
+    formats[MDP_CBYCRY_H2V1] = STR(MDP_CBYCRY_H2V1);
+    formats[MDP_Y_CRCB_H2V1] = STR(MDP_Y_CRCB_H2V1);
+    formats[MDP_Y_CBCR_H2V1] = STR(MDP_Y_CBCR_H2V1);
+    formats[MDP_Y_CRCB_H1V2] = STR(MDP_Y_CRCB_H1V2);
+    formats[MDP_Y_CBCR_H1V2] = STR(MDP_Y_CBCR_H1V2);
+    formats[MDP_RGBA_8888] = STR(MDP_RGBA_8888);
+    formats[MDP_BGRA_8888] = STR(MDP_BGRA_8888);
+    formats[MDP_RGBX_8888] = STR(MDP_RGBX_8888);
+    formats[MDP_Y_CRCB_H2V2_TILE] = STR(MDP_Y_CRCB_H2V2_TILE);
+    formats[MDP_Y_CBCR_H2V2_TILE] = STR(MDP_Y_CBCR_H2V2_TILE);
+    formats[MDP_Y_CR_CB_H2V2] = STR(MDP_Y_CR_CB_H2V2);
+    formats[MDP_Y_CR_CB_GH2V2] = STR(MDP_Y_CR_CB_GH2V2);
+    formats[MDP_Y_CB_CR_H2V2] = STR(MDP_Y_CB_CR_H2V2);
+    formats[MDP_Y_CRCB_H1V1] = STR(MDP_Y_CRCB_H1V1);
+    formats[MDP_Y_CBCR_H1V1] = STR(MDP_Y_CBCR_H1V1);
+    formats[MDP_YCRCB_H1V1] = STR(MDP_YCRCB_H1V1);
+    formats[MDP_YCBCR_H1V1] = STR(MDP_YCBCR_H1V1);
+    formats[MDP_BGR_565] = STR(MDP_BGR_565);
+    formats[MDP_BGR_888] = STR(MDP_BGR_888);
+    formats[MDP_Y_CBCR_H2V2_VENUS] = STR(MDP_Y_CBCR_H2V2_VENUS);
+    formats[MDP_BGRX_8888] = STR(MDP_BGRX_8888);
+    formats[MDP_IMGTYPE_LIMIT] = STR(MDP_IMGTYPE_LIMIT);
+
+    if(format < 0 || format >= MDP_IMGTYPE_LIMIT) {
         ALOGE("%s wrong fmt %d", __FUNCTION__, format);
         return "Unsupported format";
     }
+    if(formats[format] == 0) {
+        ALOGE("%s: table missing format %d from header", __FUNCTION__, format);
+        return "";
+    }
     return formats[format];
 }
 
@@ -537,29 +541,6 @@
     ALOGE("== Dump Dim x=%d y=%d w=%d h=%d start/end ==", x, y, w, h);
 }
 
-inline int getMdpOrient(eTransform rotation) {
-    ALOGE_IF(DEBUG_OVERLAY, "%s: rot=%d", __FUNCTION__, rotation);
-    switch(rotation)
-    {
-        case OVERLAY_TRANSFORM_0 : return 0;
-        case OVERLAY_TRANSFORM_FLIP_V:  return MDP_FLIP_UD;
-        case OVERLAY_TRANSFORM_FLIP_H:  return MDP_FLIP_LR;
-        case OVERLAY_TRANSFORM_ROT_90:  return MDP_ROT_90;
-        //getMdpOrient will switch the flips if the source is 90 rotated.
-        //Clients in Android dont factor in 90 rotation while deciding flip.
-        case OVERLAY_TRANSFORM_ROT_90_FLIP_V:
-                return MDP_ROT_90 | MDP_FLIP_LR;
-        case OVERLAY_TRANSFORM_ROT_90_FLIP_H:
-                return MDP_ROT_90 | MDP_FLIP_UD;
-        case OVERLAY_TRANSFORM_ROT_180: return MDP_ROT_180;
-        case OVERLAY_TRANSFORM_ROT_270: return MDP_ROT_270;
-        default:
-            ALOGE("%s: invalid rotation value (value = 0x%x",
-                    __FUNCTION__, rotation);
-    }
-    return -1;
-}
-
 // FB0
 template <int CHAN>
 inline Dim getPositionS3DImpl(const Whf& whf)