Merge "copybit: Export c2d header"
diff --git a/libgralloc/mapper.cpp b/libgralloc/mapper.cpp
index 4adc67d..338f9db 100644
--- a/libgralloc/mapper.cpp
+++ b/libgralloc/mapper.cpp
@@ -431,7 +431,7 @@
             {
                 private_handle_t* hnd =  va_arg(args, private_handle_t*);
                 android_ycbcr* ycbcr = va_arg(args, struct android_ycbcr *);
-                if (private_handle_t::validate(hnd)) {
+                if (!private_handle_t::validate(hnd)) {
                     res = getYUVPlaneInfo(hnd, ycbcr);
                 }
             } break;
diff --git a/libhwcomposer/hwc.cpp b/libhwcomposer/hwc.cpp
index 154fb4e..0396c91 100644
--- a/libhwcomposer/hwc.cpp
+++ b/libhwcomposer/hwc.cpp
@@ -132,19 +132,21 @@
     if(ctx->mRotMgr->getNumActiveSessions() == 0)
         Overlay::setDMAMode(Overlay::DMA_LINE_MODE);
 
-    for(int i = 0; i < numDisplays; i++) {
-        hwc_display_contents_1_t *list = displays[i];
+    for(int dpy = 0; dpy < numDisplays; dpy++) {
+        hwc_display_contents_1_t *list = displays[dpy];
         if (LIKELY(list && list->numHwLayers > 0)) {
-            for(size_t j = 0; j < list->numHwLayers; j++) {
-                if(list->hwLayers[j].compositionType != HWC_FRAMEBUFFER_TARGET)
+            for(size_t layerIndex = 0; layerIndex < list->numHwLayers;
+                                                  layerIndex++) {
+                if(list->hwLayers[layerIndex].compositionType !=
+                                            HWC_FRAMEBUFFER_TARGET)
                 {
-                    hwc_layer_1_t const* layer = &list->hwLayers[i];
+                    hwc_layer_1_t const* layer = &list->hwLayers[layerIndex];
                     private_handle_t *hnd = (private_handle_t *)layer->handle;
 
                     /* If a video layer requires rotation, set the DMA state
                      * to BLOCK_MODE */
 
-                    if (UNLIKELY(isYuvBuffer(hnd)) && canUseRotator(ctx,i) &&
+                    if (UNLIKELY(isYuvBuffer(hnd)) && canUseRotator(ctx, dpy) &&
                         (layer->transform & HWC_TRANSFORM_ROT_90)) {
                         if(not ctx->mOverlay->isDMAMultiplexingSupported()) {
                             if(ctx->mOverlay->isPipeTypeAttached(
@@ -155,7 +157,7 @@
                     }
                 }
             }
-            if(i) {
+            if(dpy) {
                 /* Uncomment the below code for testing purpose.
                    Assuming the orientation value is in terms of HAL_TRANSFORM,
                    this needs mapping to HAL, if its in different convention */
diff --git a/libhwcomposer/hwc_fbupdate.cpp b/libhwcomposer/hwc_fbupdate.cpp
index eb74ae8..4ab8eb8 100644
--- a/libhwcomposer/hwc_fbupdate.cpp
+++ b/libhwcomposer/hwc_fbupdate.cpp
@@ -38,10 +38,9 @@
 namespace ovutils = overlay::utils;
 
 IFBUpdate* IFBUpdate::getObject(hwc_context_t *ctx, const int& dpy) {
-    if(isDisplaySplit(ctx, dpy)) {
-        if(qdutils::MDPVersion::getInstance().isSrcSplit()) {
-            return new FBSrcSplit(ctx, dpy);
-        }
+    if(qdutils::MDPVersion::getInstance().isSrcSplit()) {
+        return new FBSrcSplit(ctx, dpy);
+    } else if(isDisplaySplit(ctx, dpy)) {
         return new FBUpdateSplit(ctx, dpy);
     }
     return new FBUpdateNonSplit(ctx, dpy);
diff --git a/libhwcomposer/hwc_mdpcomp.cpp b/libhwcomposer/hwc_mdpcomp.cpp
index 23f9154..8a717c0 100644
--- a/libhwcomposer/hwc_mdpcomp.cpp
+++ b/libhwcomposer/hwc_mdpcomp.cpp
@@ -47,12 +47,10 @@
 bool MDPComp::sEnable4k2kYUVSplit = false;
 bool MDPComp::sSrcSplitEnabled = false;
 MDPComp* MDPComp::getObject(hwc_context_t *ctx, const int& dpy) {
-
-    if(isDisplaySplit(ctx, dpy)) {
-        if(qdutils::MDPVersion::getInstance().isSrcSplit()) {
-            sSrcSplitEnabled = true;
-            return new MDPCompSrcSplit(dpy);
-        }
+    if(qdutils::MDPVersion::getInstance().isSrcSplit()) {
+        sSrcSplitEnabled = true;
+        return new MDPCompSrcSplit(dpy);
+    } else if(isDisplaySplit(ctx, dpy)) {
         return new MDPCompSplit(dpy);
     }
     return new MDPCompNonSplit(dpy);
diff --git a/libhwcomposer/hwc_utils.cpp b/libhwcomposer/hwc_utils.cpp
index cd47b61..4cc8243 100644
--- a/libhwcomposer/hwc_utils.cpp
+++ b/libhwcomposer/hwc_utils.cpp
@@ -1275,6 +1275,9 @@
                 &currLayer->acquireFenceFd;
         rotData.rel_fen_fd = &rotReleaseFd; //driver to populate this
         rotData.session_id = currRot->getSessId();
+        if(currLayer->acquireFenceFd >= 0) {
+            rotData.acq_fen_fd_cnt = 1; //1 ioctl call per rot session
+        }
         int ret = 0;
         ret = ioctl(rotFd, MSMFB_BUFFER_SYNC, &rotData);
         if(ret < 0) {
diff --git a/liblight/lights.c b/liblight/lights.c
index 1aa0b58..6fd1290 100644
--- a/liblight/lights.c
+++ b/liblight/lights.c
@@ -83,7 +83,7 @@
     fd = open(path, O_RDWR);
     if (fd >= 0) {
         char buffer[20];
-        int bytes = sprintf(buffer, "%d\n", value);
+        int bytes = snprintf(buffer, sizeof(buffer), "%d\n", value);
         ssize_t amt = write(fd, buffer, (size_t)bytes);
         close(fd);
         return amt == -1 ? -errno : 0;
@@ -271,6 +271,10 @@
     pthread_once(&g_init, init_globals);
 
     struct light_device_t *dev = malloc(sizeof(struct light_device_t));
+
+    if(!dev)
+        return -ENOMEM;
+
     memset(dev, 0, sizeof(*dev));
 
     dev->common.tag = HARDWARE_DEVICE_TAG;
diff --git a/liboverlay/overlayMdssRot.cpp b/liboverlay/overlayMdssRot.cpp
index ce9d9d5..f7bc87a 100644
--- a/liboverlay/overlayMdssRot.cpp
+++ b/liboverlay/overlayMdssRot.cpp
@@ -37,6 +37,8 @@
 namespace ovutils = overlay::utils;
 
 namespace overlay {
+using namespace utils;
+
 MdssRot::MdssRot() {
     reset();
     init();
@@ -265,19 +267,52 @@
 // Calculate the compressed o/p buffer size for BWC
 uint32_t MdssRot::calcCompressedBufSize(const ovutils::Whf& destWhf) {
     uint32_t bufSize = 0;
+    //Worst case alignments
     int aWidth = ovutils::align(destWhf.w, 64);
     int aHeight = ovutils::align(destWhf.h, 4);
-    int rau_cnt = aWidth/64;
-    int stride0 = (64 * 4 * rau_cnt) + rau_cnt/8;
-    int stride1 = ((64 * 2 * rau_cnt) + rau_cnt/8) * 2;
-    int stride0_off = (aHeight/4);
-    int stride1_off = (aHeight/2);
+    /*
+       Format           |   RAU size (width x height)
+       ----------------------------------------------
+       ARGB             |       32 pixel x 4 line
+       RGB888           |       32 pixel x 4 line
+       Y (Luma)         |       64 pixel x 4 line
+       CRCB 420         |       32 pixel x 2 line
+       CRCB 422 H2V1    |       32 pixel x 4 line
+       CRCB 422 H1V2    |       64 pixel x 2 line
 
-    //New o/p size for BWC
-    bufSize = (stride0 * stride0_off + stride1 * stride1_off) +
-                (rau_cnt * 2 * (stride0_off + stride1_off));
-    ALOGD_IF(DEBUG_MDSS_ROT, "%s: width = %d, height = %d raucount = %d"
-         "opBufSize = %d ", __FUNCTION__, aWidth, aHeight, rau_cnt, bufSize);
+       Metadata requirements:-
+       1 byte meta data for every 8 RAUs
+       2 byte meta data per RAU
+     */
+
+    //These blocks attempt to allocate for the worst case in each of the
+    //respective format classes, yuv/rgb. The table above is for reference
+    if(utils::isYuv(destWhf.format)) {
+        int yRauCount = aWidth / 64; //Y
+        int cRauCount = aWidth / 32; //C
+        int yStride = (64 * 4 * yRauCount) + alignup(yRauCount, 8) / 8;
+        int cStride = ((32 * 2 * cRauCount) + alignup(cRauCount, 8) / 8) * 2;
+        int yStrideOffset = (aHeight / 4);
+        int cStrideOffset = (aHeight / 2);
+        bufSize = (yStride * yStrideOffset + cStride * cStrideOffset) +
+                (yRauCount * yStrideOffset * 2) +
+                (cRauCount * cStrideOffset * 2) * 2;
+        ALOGD_IF(DEBUG_MDSS_ROT, "%s:YUV Y RAU Count = %d C RAU Count = %d",
+                __FUNCTION__, yRauCount, cRauCount);
+    } else {
+        int rauCount = aWidth / 32;
+        //Single plane
+        int stride = (32 * 4 * rauCount) + alignup(rauCount, 8) / 8;
+        int strideOffset = (aHeight / 4);
+        bufSize = (stride * strideOffset * 4 /*bpp*/) +
+            (rauCount * strideOffset * 2);
+        ALOGD_IF(DEBUG_MDSS_ROT, "%s:RGB RAU count = %d", __FUNCTION__,
+                rauCount);
+    }
+
+    ALOGD_IF(DEBUG_MDSS_ROT, "%s: aligned width = %d, aligned height = %d "
+            "Buf Size = %d", __FUNCTION__, aWidth, aHeight, bufSize);
+
     return bufSize;
 }