gralloc: Cache maintenance handling updates

- Make buffer allocations for ubwc formats as uncached since
  access from cpu is not expected.
- Bracket each cache maintenance operation with dma buf sync
  start and sync end.

Change-Id: Ic2676ad9fff9a7ca6111f14e24aaec4c3d7f6ef2
CRs-Fixed: 2210256
diff --git a/gralloc/gr_allocator.cpp b/gralloc/gr_allocator.cpp
index 970278e..e5ec78c 100644
--- a/gralloc/gr_allocator.cpp
+++ b/gralloc/gr_allocator.cpp
@@ -96,9 +96,9 @@
   }
 }
 
-int Allocator::AllocateMem(AllocData *alloc_data, uint64_t usage) {
+int Allocator::AllocateMem(AllocData *alloc_data, uint64_t usage, int format) {
   int ret;
-  alloc_data->uncached = UseUncached(usage);
+  alloc_data->uncached = UseUncached(format, usage);
 
   // After this point we should have the right heap set, there is no fallback
   GetIonHeapInfo(usage, &alloc_data->heap_id, &alloc_data->alloc_type, &alloc_data->flags);
@@ -159,7 +159,7 @@
   *max_index = -1;
   for (uint32_t i = 0; i < num_descriptors; i++) {
     // Check Cached vs non-cached and all the ION flags
-    cur_uncached = UseUncached(descriptors[i]->GetUsage());
+    cur_uncached = UseUncached(descriptors[i]->GetFormat(), descriptors[i]->GetUsage());
     GetIonHeapInfo(descriptors[i]->GetUsage(), &cur_heap_id, &cur_alloc_type, &cur_ion_flags);
 
     if (i > 0 && (cur_heap_id != prev_heap_id || cur_alloc_type != prev_alloc_type ||
@@ -239,7 +239,7 @@
 /* The default policy is to return cached buffers unless the client explicity
  * sets the PRIVATE_UNCACHED flag or indicates that the buffer will be rarely
  * read or written in software. */
-bool Allocator::UseUncached(uint64_t usage) {
+bool Allocator::UseUncached(int format, uint64_t usage) {
   if ((usage & GRALLOC_USAGE_PRIVATE_UNCACHED) || (usage & BufferUsage::PROTECTED)) {
     return true;
   }
@@ -259,6 +259,10 @@
     return true;
   }
 
+  if (format && IsUBwcEnabled(format, usage)) {
+    return true;
+  }
+
   return false;
 }
 
diff --git a/gralloc/gr_allocator.h b/gralloc/gr_allocator.h
index 46ec09d..17fb0cb 100644
--- a/gralloc/gr_allocator.h
+++ b/gralloc/gr_allocator.h
@@ -48,13 +48,13 @@
   int ImportBuffer(int fd);
   int FreeBuffer(void *base, unsigned int size, unsigned int offset, int fd, int handle);
   int CleanBuffer(void *base, unsigned int size, unsigned int offset, int handle, int op, int fd);
-  int AllocateMem(AllocData *data, uint64_t usage);
+  int AllocateMem(AllocData *data, uint64_t usage, int format);
   // @return : index of the descriptor with maximum buffer size req
   bool CheckForBufferSharing(uint32_t num_descriptors,
                              const std::vector<std::shared_ptr<BufferDescriptor>> &descriptors,
                              ssize_t *max_index);
   int GetImplDefinedFormat(uint64_t usage, int format);
-  bool UseUncached(uint64_t usage);
+  bool UseUncached(int format, uint64_t usage);
 
  private:
   void GetIonHeapInfo(uint64_t usage, unsigned int *ion_heap_id, unsigned int *alloc_type,
diff --git a/gralloc/gr_buf_mgr.cpp b/gralloc/gr_buf_mgr.cpp
index 9f7b22e..6b6ec87 100644
--- a/gralloc/gr_buf_mgr.cpp
+++ b/gralloc/gr_buf_mgr.cpp
@@ -229,6 +229,11 @@
       status = Error::BAD_BUFFER;
     }
     hnd->flags &= ~private_handle_t::PRIV_FLAGS_NEEDS_FLUSH;
+  } else {
+    if (allocator_->CleanBuffer(reinterpret_cast<void *>(hnd->base), hnd->size, hnd->offset,
+                                buf->ion_handle_main, CACHE_READ_DONE, hnd->fd) != 0) {
+      status = Error::BAD_BUFFER;
+    }
   }
 
   return status;
@@ -273,7 +278,7 @@
     flags |= private_handle_t::PRIV_FLAGS_NON_CPU_WRITER;
   }
 
-  if (!allocator_->UseUncached(usage)) {
+  if (!allocator_->UseUncached(format, usage)) {
     flags |= private_handle_t::PRIV_FLAGS_CACHED;
   }
 
@@ -316,10 +321,10 @@
   data.align = GetDataAlignment(format, usage);
   data.size = size;
   data.handle = (uintptr_t)handle;
-  data.uncached = allocator_->UseUncached(usage);
+  data.uncached = allocator_->UseUncached(format, usage);
 
   // Allocate buffer memory
-  err = allocator_->AllocateMem(&data, usage);
+  err = allocator_->AllocateMem(&data, usage, format);
   if (err) {
     ALOGE("gralloc failed to allocate err=%s", strerror(-err));
     return Error::NO_RESOURCES;
@@ -331,7 +336,7 @@
   e_data.handle = data.handle;
   e_data.align = page_size;
 
-  err = allocator_->AllocateMem(&e_data, 0);
+  err = allocator_->AllocateMem(&e_data, 0, 0);
   if (err) {
     ALOGE("gralloc failed to allocate metadata error=%s", strerror(-err));
     return Error::NO_RESOURCES;
diff --git a/gralloc/gr_ion_alloc.cpp b/gralloc/gr_ion_alloc.cpp
index 36de059..2c7a90b 100644
--- a/gralloc/gr_ion_alloc.cpp
+++ b/gralloc/gr_ion_alloc.cpp
@@ -140,6 +140,9 @@
     case CACHE_INVALIDATE:
       sync.flags = DMA_BUF_SYNC_START | DMA_BUF_SYNC_RW;
       break;
+    case CACHE_READ_DONE:
+      sync.flags = DMA_BUF_SYNC_END | DMA_BUF_SYNC_READ;
+      break;
     default:
       ALOGE("%s: Invalid operation %d", __FUNCTION__, op);
       return -1;
@@ -247,6 +250,10 @@
 
 int IonAlloc::CleanBuffer(void *base, unsigned int size, unsigned int offset, int handle, int op,
                           int /*fd*/) {
+  if (op == CACHE_READ_DONE)  {
+    return 0;
+  }
+
   ATRACE_CALL();
   ATRACE_INT("operation id", op);
   struct ion_flush_data flush_data;
diff --git a/gralloc/gr_ion_alloc.h b/gralloc/gr_ion_alloc.h
index 763ac54..8cb1568 100644
--- a/gralloc/gr_ion_alloc.h
+++ b/gralloc/gr_ion_alloc.h
@@ -40,6 +40,7 @@
   CACHE_CLEAN = 0x1,
   CACHE_INVALIDATE,
   CACHE_CLEAN_AND_INVALIDATE,
+  CACHE_READ_DONE
 };
 
 struct AllocData {