display: Use L3 DRM for non TZ content

When the GRALLOC_USAGE_PROTECTED flag is set, use L3 DRM if the
buffer isn't also allocated with the MM heap.
Per the gralloc spec, this means that such buffers shouldn't be
routed to external displays since the hardware protected path is
not available due to allocation from an insecure heap.

b/11071443 - (related) [HH] Green frame happens during playback
             of Forward-lock DRM with Miracast connetion
Change-Id: Ifd858f874b4e3c2c3ec54c6d6a33372ec55b0aee
diff --git a/msm8974/libgralloc/alloc_controller.cpp b/msm8974/libgralloc/alloc_controller.cpp
index d8c5b54..bda3dfd 100644
--- a/msm8974/libgralloc/alloc_controller.cpp
+++ b/msm8974/libgralloc/alloc_controller.cpp
@@ -183,12 +183,6 @@
 IonController::IonController()
 {
     mIonAlloc = new IonAlloc();
-    mUseTZProtection = false;
-    char property[PROPERTY_VALUE_MAX];
-    if ((property_get("persist.gralloc.cp.level3", property, NULL) <= 0) ||
-                            (atoi(property) != 1)) {
-        mUseTZProtection = true;
-    }
 }
 
 int IonController::allocate(alloc_data& data, int usage)
@@ -209,12 +203,14 @@
         ionFlags |= ION_HEAP(ION_IOMMU_HEAP_ID);
 
     if(usage & GRALLOC_USAGE_PROTECTED) {
-        if ((mUseTZProtection) && (usage & GRALLOC_USAGE_PRIVATE_MM_HEAP)) {
+        if (usage & GRALLOC_USAGE_PRIVATE_MM_HEAP) {
             ionFlags |= ION_HEAP(ION_CP_MM_HEAP_ID);
             ionFlags |= ION_SECURE;
         } else {
             // for targets/OEMs which do not need HW level protection
-            // do not set ion secure flag & MM heap. Fallback to IOMMU heap.
+            // do not set ion secure flag & MM heap. Fallback to IOMMU heap
+            // and use DRM for such buffers
+            data.allocType |= private_handle_t::PRIV_FLAGS_L3_SECURE_BUFFER;
             ionFlags |= ION_HEAP(ION_IOMMU_HEAP_ID);
         }
     } else if(usage & GRALLOC_USAGE_PRIVATE_MM_HEAP) {
diff --git a/msm8974/libgralloc/alloc_controller.h b/msm8974/libgralloc/alloc_controller.h
index 8954d39..5fe81fa 100644
--- a/msm8974/libgralloc/alloc_controller.h
+++ b/msm8974/libgralloc/alloc_controller.h
@@ -65,7 +65,6 @@
 
     private:
     IonAlloc* mIonAlloc;
-    bool mUseTZProtection;
 
 };
 } //end namespace gralloc
diff --git a/msm8974/libgralloc/gralloc_priv.h b/msm8974/libgralloc/gralloc_priv.h
index a7ceb34..94d4334 100644
--- a/msm8974/libgralloc/gralloc_priv.h
+++ b/msm8974/libgralloc/gralloc_priv.h
@@ -160,6 +160,7 @@
             PRIV_FLAGS_ITU_R_601          = 0x00200000,
             PRIV_FLAGS_ITU_R_601_FR       = 0x00400000,
             PRIV_FLAGS_ITU_R_709          = 0x00800000,
+            PRIV_FLAGS_L3_SECURE_BUFFER   = 0x01000000,
         };
 
         // file-descriptors
diff --git a/msm8974/libhwcomposer/hwc_mdpcomp.cpp b/msm8974/libhwcomposer/hwc_mdpcomp.cpp
index 61b8d63..bbf4a73 100644
--- a/msm8974/libhwcomposer/hwc_mdpcomp.cpp
+++ b/msm8974/libhwcomposer/hwc_mdpcomp.cpp
@@ -401,6 +401,10 @@
             }
         }
 
+        if(mDpy > HWC_DISPLAY_PRIMARY && isL3SecureBuffer(hnd)) {
+            return false;
+        }
+
         //For 8x26 with panel width>1k, if RGB layer needs HFLIP fail mdp comp
         // may not need it if Gfx pre-rotation can handle all flips & rotations
         if(qdutils::MDPVersion::getInstance().is8x26() &&
@@ -630,6 +634,10 @@
                     __FUNCTION__);
             return false;
         }
+        private_handle_t *hnd = (private_handle_t *)layer->handle;
+        if(mDpy > HWC_DISPLAY_PRIMARY && isL3SecureBuffer(hnd)) {
+            return false;
+        }
     }
 
     return true;
diff --git a/msm8974/libhwcomposer/hwc_utils.h b/msm8974/libhwcomposer/hwc_utils.h
index 34c749f..65b3098 100644
--- a/msm8974/libhwcomposer/hwc_utils.h
+++ b/msm8974/libhwcomposer/hwc_utils.h
@@ -270,6 +270,12 @@
 static inline bool isSecureBuffer(const private_handle_t* hnd) {
     return (hnd && (private_handle_t::PRIV_FLAGS_SECURE_BUFFER & hnd->flags));
 }
+
+// Returns true if the buffer is marked for L3 DRM
+static inline bool isL3SecureBuffer(const private_handle_t* hnd) {
+    return (hnd &&
+            (private_handle_t::PRIV_FLAGS_L3_SECURE_BUFFER & hnd->flags));
+}
 //Return true if buffer is marked locked
 static inline bool isBufferLocked(const private_handle_t* hnd) {
     return (hnd && (private_handle_t::PRIV_FLAGS_HWC_LOCK & hnd->flags));