display: Add support for HW Cursor

- Retrieve cursor pipe info from driver
- Configure the layer marked with flag HWC_IS_CURSOR_LAYER to the
  HWCursor using the fb_cursor ioctl.
- The config happens only when it satisfies the hw limitions of
  cursor
- HWCursor is supported on primary display
- Since cursor configuration happens first, make use of drop
  layer/count to handle other composition strategies
- Add support for hwc_setCursorPositionAsync as per HWC 1.4

Change-Id: I8663b6da89b0c2dd9b48af96d64a433b2b8a302c
diff --git a/libhwcomposer/hwc_utils.cpp b/libhwcomposer/hwc_utils.cpp
index a3cb196..51c208e 100644
--- a/libhwcomposer/hwc_utils.cpp
+++ b/libhwcomposer/hwc_utils.cpp
@@ -30,6 +30,7 @@
 #include <overlay.h>
 #include <overlayRotator.h>
 #include <overlayWriteback.h>
+#include <overlayCursor.h>
 #include "hwc_utils.h"
 #include "hwc_mdpcomp.h"
 #include "hwc_fbupdate.h"
@@ -1085,6 +1086,7 @@
     ctx->listStats[dpy].renderBufIndexforABC = -1;
     ctx->listStats[dpy].secureRGBCount = 0;
     ctx->listStats[dpy].refreshRateRequest = ctx->dpyAttr[dpy].refreshRate;
+    ctx->listStats[dpy].cursorLayerPresent = false;
     uint32_t refreshRate = 0;
     qdutils::MDPVersion& mdpHw = qdutils::MDPVersion::getInstance();
     int s3dFormat = HAL_NO_3D;
@@ -1116,6 +1118,12 @@
         if(ctx->listStats[dpy].numAppLayers > MAX_NUM_APP_LAYERS)
             continue;
 
+        // Valid cursor must be the top most layer
+        if((int)i == (ctx->listStats[dpy].numAppLayers - 1) &&
+                     isCursorLayer(&list->hwLayers[i])) {
+            ctx->listStats[dpy].cursorLayerPresent = true;
+        }
+
         //reset yuv indices
         ctx->listStats[dpy].yuvIndices[i] = -1;
         ctx->listStats[dpy].yuv4k2kIndices[i] = -1;
@@ -1942,6 +1950,46 @@
     crop.bottom = transformedCrop.y + transformedCrop.h;
 }
 
+bool configHwCursor(const int fd, int dpy, hwc_layer_1_t* layer) {
+    if(dpy > HWC_DISPLAY_PRIMARY) {
+        // HWCursor not supported on secondary displays
+        return false;
+    }
+    private_handle_t *hnd = (private_handle_t *)layer->handle;
+    hwc_rect dst = layer->displayFrame;
+    hwc_rect src = integerizeSourceCrop(layer->sourceCropf);
+    int srcW = src.right - src.left;
+    int srcH = src.bottom - src.top;
+    int dstW = dst.right - dst.left;
+    int dstH = dst.bottom - dst.top;
+
+    Whf whf(getWidth(hnd), getHeight(hnd), hnd->format);
+    Dim crop(src.left, src.top, srcW, srcH);
+    Dim dest(dst.left, dst.top, dstW, dstH);
+
+    ovutils::PipeArgs pargs(ovutils::OV_MDP_FLAGS_NONE,
+                            whf,
+                            Z_SYSTEM_ALLOC,
+                            ovutils::ROT_FLAGS_NONE,
+                            layer->planeAlpha,
+                            (ovutils::eBlending)
+                            getBlending(layer->blending));
+
+    ALOGD_IF(HWC_UTILS_DEBUG, "%s: CursorInfo: w = %d h = %d "
+        "crop [%d, %d, %d, %d] dst [%d, %d, %d, %d]", __FUNCTION__,
+        getWidth(hnd), getHeight(hnd), src.left, src.top, srcW, srcH,
+        dst.left, dst.top, dstW, dstH);
+
+    return HWCursor::getInstance()->config(fd, (void*)hnd->base, pargs,
+                crop, dest);
+}
+
+void freeHwCursor(const int fd, int dpy) {
+    if (dpy == HWC_DISPLAY_PRIMARY) {
+        HWCursor::getInstance()->free(fd);
+    }
+}
+
 int getRotDownscale(hwc_context_t *ctx, const hwc_layer_1_t *layer) {
     if(not qdutils::MDPVersion::getInstance().isRotDownscaleEnabled()) {
         return 0;