Align secure video surfaces to 1 MB
[GitHub/LineageOS/android_hardware_samsung_slsi_exynos5.git] / gralloc / gralloc.cpp
index 06233328ffb5ea4944cdf24775833ff3827b0ed7..d10dc274af7484b3aa8bfb4785e19e15b25eb3b2 100644 (file)
@@ -48,6 +48,7 @@
 #define ION_EXYNOS_FIMD_VIDEO_MASK  (1 << 28)
 #define ION_EXYNOS_MFC_OUTPUT_MASK  (1 << 26)
 #define ION_EXYNOS_MFC_INPUT_MASK   (1 << 25)
+#define MB_1 (1024*1024)
 
 
 /*****************************************************************************/
@@ -130,7 +131,7 @@ static unsigned int _select_heap(int usage)
 static int gralloc_alloc_rgb(int ionfd, int w, int h, int format, int usage,
                              unsigned int ion_flags, private_handle_t **hnd, int *stride)
 {
-    size_t size, bpr;
+    size_t size, bpr, alignment = 0;
     int bpp = 0, vstride, fd, err;
     unsigned int heap_mask = _select_heap(usage);
 
@@ -161,18 +162,20 @@ static int gralloc_alloc_rgb(int ionfd, int w, int h, int format, int usage,
     if (format != HAL_PIXEL_FORMAT_BLOB) {
         bpr = ALIGN(w*bpp, 16);
         vstride = ALIGN(h, 16);
-        if (vstride == h)
-            size = bpr * (vstride + 1);
+        if (vstride < h + 2)
+            size = bpr * (h + 2);
         else
             size = bpr * vstride;
         *stride = bpr / bpp;
         size = ALIGN(size, PAGE_SIZE);
     }
 
-    if (usage & GRALLOC_USAGE_PROTECTED)
+    if (usage & GRALLOC_USAGE_PROTECTED) {
+        alignment = MB_1;
         ion_flags |= ION_EXYNOS_FIMD_VIDEO_MASK;
+    }
 
-    err = ion_alloc_fd(ionfd, size, 0, heap_mask, ion_flags,
+    err = ion_alloc_fd(ionfd, size, alignment, heap_mask, ion_flags,
                        &fd);
     *hnd = new private_handle_t(fd, size, usage, w, h, format, *stride,
                                 vstride);
@@ -330,8 +333,6 @@ static int gralloc_alloc(alloc_device_t* dev,
     if (err)
         return err;
 
-    err = grallocMap(module, hnd);
-
     if (err != 0)
         goto err;
 
@@ -358,8 +359,9 @@ static int gralloc_free(alloc_device_t* dev,
     private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(handle);
     gralloc_module_t* module = reinterpret_cast<gralloc_module_t*>(
                                                                    dev->common.module);
+    if (hnd->base)
+        grallocUnmap(module, const_cast<private_handle_t*>(hnd));
 
-    grallocUnmap(module, const_cast<private_handle_t*>(hnd));
     close(hnd->fd);
     if (hnd->fd1 >= 0)
         close(hnd->fd1);