Update gralloc to use new ion flag

Switches cached allocations from faulting in mappings, to
allocating them at mmap time.  Caches now have to be managed manually
from gralloc lock/unlock

Bug: 6854979
Change-Id: I2fa31719c70bb1ce836ca34c49cb34ab509ff1db
Signed-off-by: Rebecca Schultz Zavin <rebecca@android.com>
diff --git a/gralloc/gralloc.cpp b/gralloc/gralloc.cpp
index c212896..a9f40c5 100644
--- a/gralloc/gralloc.cpp
+++ b/gralloc/gralloc.cpp
@@ -309,7 +309,7 @@
         return -EINVAL;
 
     if( (usage & GRALLOC_USAGE_SW_READ_MASK) == GRALLOC_USAGE_SW_READ_OFTEN )
-        ion_flags = ION_FLAG_CACHED;
+        ion_flags = ION_FLAG_CACHED | ION_FLAG_CACHED_NEEDS_SYNC;
 
     private_module_t* m = reinterpret_cast<private_module_t*>
         (dev->common.module);
diff --git a/gralloc/mapper.cpp b/gralloc/mapper.cpp
index eb82ed9..0e7b8b9 100644
--- a/gralloc/mapper.cpp
+++ b/gralloc/mapper.cpp
@@ -173,6 +173,12 @@
 {
     // we're done with a software buffer. nothing to do in this
     // implementation. typically this is used to flush the data cache.
+    private_handle_t* hnd = (private_handle_t*)handle;
+    ion_sync_fd(getIonFd(module), hnd->fd);
+    if (hnd->fd1 >= 0)
+        ion_sync_fd(getIonFd(module), hnd->fd1);
+    if (hnd->fd2 >= 0)
+        ion_sync_fd(getIonFd(module), hnd->fd2);
 
     if (private_handle_t::validate(handle) < 0)
         return -EINVAL;