display: Add support for dynamic refresh rate

- The primary panels refresh rate can be change by clients
- Required refresh rate will be set by client using metadata
- If there are multiple clients requesting, it will be used only
  when the refresh rates are equal, else it resets to default
- Set the new refresh rate only when there are only YUV layers
  updating or when the list has only one RGB layer updating
- MdpVersion gets the dyn fps capabilities from panelInfo

Change-Id: If3e7e6b2f028eb301399c4d32c748eed8a97c41f
diff --git a/libhwcomposer/hwc_mdpcomp.cpp b/libhwcomposer/hwc_mdpcomp.cpp
index b222d32..552beae 100644
--- a/libhwcomposer/hwc_mdpcomp.cpp
+++ b/libhwcomposer/hwc_mdpcomp.cpp
@@ -1063,7 +1063,7 @@
 
     int numAppLayers = ctx->listStats[mDpy].numAppLayers;
     mCurrentFrame.reset(numAppLayers);
-    updateLayerCache(ctx, list);
+    updateLayerCache(ctx, list, mCurrentFrame);
 
     //If an MDP marked layer is unsupported cannot do partial MDP Comp
     for(int i = 0; i < numAppLayers; i++) {
@@ -1078,7 +1078,7 @@
         }
     }
 
-    updateYUV(ctx, list, false /*secure only*/);
+    updateYUV(ctx, list, false /*secure only*/, mCurrentFrame);
     /* mark secure RGB layers for MDP comp */
     updateSecureRGB(ctx, list);
     bool ret = markLayersForCaching(ctx, list); //sets up fbZ also
@@ -1238,7 +1238,7 @@
 
     mCurrentFrame.reset(numAppLayers);
     mCurrentFrame.fbCount -= mCurrentFrame.dropCount;
-    updateYUV(ctx, list, secureOnly);
+    updateYUV(ctx, list, secureOnly, mCurrentFrame);
     int mdpCount = mCurrentFrame.mdpCount;
 
     if(!isYuvPresent(ctx, mDpy) or (mdpCount == 0)) {
@@ -1297,7 +1297,7 @@
     mCurrentFrame.reset(numAppLayers);
     mCurrentFrame.fbCount -= mCurrentFrame.dropCount;
 
-    updateYUV(ctx, list, secureOnly);
+    updateYUV(ctx, list, secureOnly, mCurrentFrame);
     /* mark secure RGB layers for MDP comp */
     updateSecureRGB(ctx, list);
 
@@ -1548,56 +1548,53 @@
 }
 
 void MDPComp::updateLayerCache(hwc_context_t* ctx,
-        hwc_display_contents_1_t* list) {
+        hwc_display_contents_1_t* list, FrameInfo& frame) {
     int numAppLayers = ctx->listStats[mDpy].numAppLayers;
     int fbCount = 0;
 
     for(int i = 0; i < numAppLayers; i++) {
         if (mCachedFrame.hnd[i] == list->hwLayers[i].handle) {
-            if(!mCurrentFrame.drop[i])
+            if(!frame.drop[i])
                 fbCount++;
-            mCurrentFrame.isFBComposed[i] = true;
+            frame.isFBComposed[i] = true;
         } else {
-            mCurrentFrame.isFBComposed[i] = false;
+            frame.isFBComposed[i] = false;
         }
     }
 
-    mCurrentFrame.fbCount = fbCount;
-    mCurrentFrame.mdpCount = mCurrentFrame.layerCount - mCurrentFrame.fbCount
-                                                    - mCurrentFrame.dropCount;
+    frame.fbCount = fbCount;
+    frame.mdpCount = frame.layerCount - frame.fbCount
+                                            - frame.dropCount;
 
-    ALOGD_IF(isDebug(),"%s: MDP count: %d FB count %d drop count: %d"
-             ,__FUNCTION__, mCurrentFrame.mdpCount, mCurrentFrame.fbCount,
-            mCurrentFrame.dropCount);
+    ALOGD_IF(isDebug(),"%s: MDP count: %d FB count %d drop count: %d",
+            __FUNCTION__, frame.mdpCount, frame.fbCount, frame.dropCount);
 }
 
 void MDPComp::updateYUV(hwc_context_t* ctx, hwc_display_contents_1_t* list,
-        bool secureOnly) {
+        bool secureOnly, FrameInfo& frame) {
     int nYuvCount = ctx->listStats[mDpy].yuvCount;
     for(int index = 0;index < nYuvCount; index++){
         int nYuvIndex = ctx->listStats[mDpy].yuvIndices[index];
         hwc_layer_1_t* layer = &list->hwLayers[nYuvIndex];
 
         if(!isYUVDoable(ctx, layer)) {
-            if(!mCurrentFrame.isFBComposed[nYuvIndex]) {
-                mCurrentFrame.isFBComposed[nYuvIndex] = true;
-                mCurrentFrame.fbCount++;
+            if(!frame.isFBComposed[nYuvIndex]) {
+                frame.isFBComposed[nYuvIndex] = true;
+                frame.fbCount++;
             }
         } else {
-            if(mCurrentFrame.isFBComposed[nYuvIndex]) {
+            if(frame.isFBComposed[nYuvIndex]) {
                 private_handle_t *hnd = (private_handle_t *)layer->handle;
                 if(!secureOnly || isSecureBuffer(hnd)) {
-                    mCurrentFrame.isFBComposed[nYuvIndex] = false;
-                    mCurrentFrame.fbCount--;
+                    frame.isFBComposed[nYuvIndex] = false;
+                    frame.fbCount--;
                 }
             }
         }
     }
 
-    mCurrentFrame.mdpCount = mCurrentFrame.layerCount -
-            mCurrentFrame.fbCount - mCurrentFrame.dropCount;
-    ALOGD_IF(isDebug(),"%s: fb count: %d",__FUNCTION__,
-             mCurrentFrame.fbCount);
+    frame.mdpCount = frame.layerCount - frame.fbCount - frame.dropCount;
+    ALOGD_IF(isDebug(),"%s: fb count: %d",__FUNCTION__, frame.fbCount);
 }
 
 void MDPComp::updateSecureRGB(hwc_context_t* ctx,
@@ -1878,6 +1875,26 @@
         ALOGD("%s",sDump.string());
     }
 
+#ifdef DYNAMIC_FPS
+    //For primary display, set the dynamic refreshrate
+    if(!mDpy && qdutils::MDPVersion::getInstance().isDynFpsSupported()) {
+        FrameInfo frame;
+        frame.reset(mCurrentFrame.layerCount);
+        ALOGD_IF(isDebug(), "%s: Update Cache and YUVInfo for Dyn Refresh Rate",
+                 __FUNCTION__);
+        updateLayerCache(ctx, list, frame);
+        updateYUV(ctx, list, false /*secure only*/, frame);
+        uint32_t refreshRate = ctx->dpyAttr[mDpy].refreshRate;
+        //Set the new fresh rate, if there is only one updating YUV layer
+        //or there is one single RGB layer with this request
+        if((ctx->listStats[mDpy].yuvCount == frame.mdpCount) ||
+                                (frame.layerCount == 1)) {
+            refreshRate = ctx->listStats[mDpy].refreshRateRequest;
+        }
+        setRefreshRate(ctx, mDpy, refreshRate);
+    }
+#endif
+
     mCachedFrame.cacheAll(list);
     mCachedFrame.updateCounts(mCurrentFrame);
     return ret;