hwc: Configure MDP with updated crop and whf for PTOR layers

1. For PTOR layers, MDP needs to be configured with width, height
and format of the Copybit render buffer, otherwise it will lead to
artifacts due to incorrect render buffer reads, when PTOR layer
handles have un-equal strides.

2. Remove render buffer offset for a PTOR layer and configure MDP
with modified source crop, based on its position on render buffer.

3. A layer cannot qualify for PTOR feature, if none of below layers
intersect with it.

Change-Id: Idba65789b9ed3b699a5c70107881868c0385322d
diff --git a/libhwcomposer/hwc_mdpcomp.cpp b/libhwcomposer/hwc_mdpcomp.cpp
index 481d629..44fcfbf 100644
--- a/libhwcomposer/hwc_mdpcomp.cpp
+++ b/libhwcomposer/hwc_mdpcomp.cpp
@@ -712,18 +712,19 @@
             // Overlap area > (1/3 * FrameBuffer) area, based on Perf inputs.
             continue;
         }
-        // Found the PTOR layer
-        bool found = true;
+        bool found = false;
         for (int j = i-1; j >= 0; j--) {
             // Check if the layers below this layer qualifies for PTOR comp
             hwc_layer_1_t* layer = &list->hwLayers[j];
             hwc_rect_t disFrame = layer->displayFrame;
-            //layer below PTOR is intersecting and has 90 degree transform or
+            // Layer below PTOR is intersecting and has 90 degree transform or
             // needs scaling cannot be supported.
-            if ((isValidRect(getIntersection(dispFrame, disFrame)))
-                            && (has90Transform(layer) || needsScaling(layer))) {
-                found = false;
-                break;
+            if (isValidRect(getIntersection(dispFrame, disFrame))) {
+                if (has90Transform(layer) || needsScaling(layer)) {
+                    found = false;
+                    break;
+                }
+                found = true;
             }
         }
         // Store the minLayer Index
@@ -766,11 +767,30 @@
         sourceCrop[i] = integerizeSourceCrop(layer->sourceCropf);
     }
 
+    private_handle_t *renderBuf = ctx->mCopyBit[mDpy]->getCurrentRenderBuffer();
+    Whf layerWhf[numPTORLayersFound]; // To store w,h,f of PTOR layers
+
     for(int j = 0; j < numPTORLayersFound; j++) {
         int index =  ctx->mPtorInfo.layerIndex[j];
+
+        // Update src crop of PTOR layer
+        hwc_layer_1_t* layer = &list->hwLayers[index];
+        layer->sourceCropf.left = (float)ctx->mPtorInfo.displayFrame[j].left;
+        layer->sourceCropf.top = (float)ctx->mPtorInfo.displayFrame[j].top;
+        layer->sourceCropf.right = (float)ctx->mPtorInfo.displayFrame[j].right;
+        layer->sourceCropf.bottom =(float)ctx->mPtorInfo.displayFrame[j].bottom;
+
+        // Store & update w, h, format of PTOR layer
+        private_handle_t *hnd = (private_handle_t *)layer->handle;
+        Whf whf(hnd->width, hnd->height, hnd->format, hnd->size);
+        layerWhf[j] = whf;
+        hnd->width = renderBuf->width;
+        hnd->height = renderBuf->height;
+        hnd->format = renderBuf->format;
+
         // Remove overlap from crop & displayFrame of below layers
         for (int i = 0; i < index && index !=-1; i++) {
-            hwc_layer_1_t* layer = &list->hwLayers[i];
+            layer = &list->hwLayers[i];
             if(!isValidRect(getIntersection(layer->displayFrame,
                                             overlapRect[j])))  {
                 continue;
@@ -807,6 +827,15 @@
         layer->sourceCropf.bottom = (float)sourceCrop[i].bottom;
     }
 
+    // Restore w,h,f of PTOR layers
+    for (int i = 0; i < numPTORLayersFound; i++) {
+        int idx = ctx->mPtorInfo.layerIndex[i];
+        private_handle_t *hnd = (private_handle_t *)list->hwLayers[idx].handle;
+        hnd->width = layerWhf[i].w;
+        hnd->height = layerWhf[i].h;
+        hnd->format = layerWhf[i].format;
+    }
+
     if (!result) {
         // reset PTOR
         ctx->mPtorInfo.count = 0;
@@ -1735,8 +1764,7 @@
             if (!mDpy && (index != -1)) {
                 hnd = ctx->mCopyBit[mDpy]->getCurrentRenderBuffer();
                 fd = hnd->fd;
-                // Use the offset of the RenderBuffer
-                offset = ctx->mPtorInfo.mRenderBuffOffset[index];
+                offset = 0;
             }
 
             ALOGD_IF(isDebug(),"%s: MDP Comp: Drawing layer: %p hnd: %p \
@@ -1984,7 +2012,7 @@
             if (!mDpy && (index != -1)) {
                 hnd = ctx->mCopyBit[mDpy]->getCurrentRenderBuffer();
                 fd = hnd->fd;
-                offset = ctx->mPtorInfo.mRenderBuffOffset[index];
+                offset = 0;
             }
 
             if(ctx->mAD->draw(ctx, fd, offset)) {