gralloc: Allocate cached by default, don't use bitops on SW flags
Allocate gralloc buffers cached by default unless clients specify
uncached using PRIVATE_UNCACHED or READ_RARELY or WRITE_RARELY at
allocation time. Some clients could use gralloc for allocation but
later won't use lock()/unlock() for CPU operations and likely use
their own caching methods. Cached by default helps such clients.
SW usage flags are not defined as bit values, so do not use bitops
on those flags.
Change-Id: Id371de2ec6efbfa0ed84172b3540f3ebc8f5d459
diff --git a/libgralloc/alloc_controller.cpp b/libgralloc/alloc_controller.cpp
index cd6d565..fd81c70 100644
--- a/libgralloc/alloc_controller.cpp
+++ b/libgralloc/alloc_controller.cpp
@@ -76,6 +76,21 @@
return true;
}
+/* 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. Any combination with a _RARELY_ flag will be
+ * treated as uncached. */
+static bool useUncached(const int& usage) {
+ if((usage & GRALLOC_USAGE_PRIVATE_UNCACHED) or
+ ((usage & GRALLOC_USAGE_SW_WRITE_MASK) ==
+ GRALLOC_USAGE_SW_WRITE_RARELY) or
+ ((usage & GRALLOC_USAGE_SW_READ_MASK) ==
+ GRALLOC_USAGE_SW_READ_RARELY))
+ return true;
+
+ return false;
+}
+
//-------------- AdrenoMemInfo-----------------------//
AdrenoMemInfo::AdrenoMemInfo()
{
@@ -664,14 +679,3 @@
delete hnd;
}
-
-bool useUncached(const int& usage) {
- if(usage & GRALLOC_USAGE_PRIVATE_UNCACHED)
- return true;
-
- if(not (usage & (GRALLOC_USAGE_SW_WRITE_OFTEN |
- GRALLOC_USAGE_SW_READ_OFTEN)))
- return true;
-
- return false;
-}
diff --git a/libgralloc/gpu.cpp b/libgralloc/gpu.cpp
index 551f188..5533ffb 100644
--- a/libgralloc/gpu.cpp
+++ b/libgralloc/gpu.cpp
@@ -141,14 +141,6 @@
flags |= private_handle_t::PRIV_FLAGS_HW_TEXTURE;
}
- if (usage & GRALLOC_USAGE_HW_RENDER) {
- flags |= private_handle_t::PRIV_FLAGS_HW_RENDER;
- }
-
- if (usage & GRALLOC_USAGE_HW_FB) {
- flags |= private_handle_t::PRIV_FLAGS_HW_FB;
- }
-
if(usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY) {
flags |= private_handle_t::PRIV_FLAGS_SECURE_DISPLAY;
}
@@ -161,6 +153,17 @@
flags |= private_handle_t::PRIV_FLAGS_CPU_RENDERED;
}
+ if (usage & (GRALLOC_USAGE_HW_VIDEO_ENCODER |
+ GRALLOC_USAGE_HW_CAMERA_WRITE |
+ GRALLOC_USAGE_HW_RENDER |
+ GRALLOC_USAGE_HW_FB)) {
+ flags |= private_handle_t::PRIV_FLAGS_NON_CPU_WRITER;
+ }
+
+ if(false == data.uncached) {
+ flags |= private_handle_t::PRIV_FLAGS_CACHED;
+ }
+
flags |= data.allocType;
uint64_t eBaseAddr = (uint64_t)(eData.base) + eData.offset;
private_handle_t *hnd = new private_handle_t(data.fd, size, flags,
diff --git a/libgralloc/gr.h b/libgralloc/gr.h
index 37ee4e5..797d57e 100644
--- a/libgralloc/gr.h
+++ b/libgralloc/gr.h
@@ -71,10 +71,6 @@
void free_buffer(private_handle_t *hnd);
int getYUVPlaneInfo(private_handle_t* pHnd, struct android_ycbcr* ycbcr);
-// Use uncached for all scenarios except when the CPU needs to read or write
-// often
-bool useUncached(const int& usage);
-
/*****************************************************************************/
class Locker {
diff --git a/libgralloc/gralloc_priv.h b/libgralloc/gralloc_priv.h
index 9e2a6cf..d866ef4 100644
--- a/libgralloc/gralloc_priv.h
+++ b/libgralloc/gralloc_priv.h
@@ -180,11 +180,9 @@
PRIV_FLAGS_USES_ION = 0x00000008,
PRIV_FLAGS_USES_ASHMEM = 0x00000010,
PRIV_FLAGS_NEEDS_FLUSH = 0x00000020,
- // Uncached memory or no CPU writers
- PRIV_FLAGS_DO_NOT_FLUSH = 0x00000040,
- PRIV_FLAGS_HW_RENDER = 0x00000080,
+ PRIV_FLAGS_NON_CPU_WRITER = 0x00000080,
PRIV_FLAGS_NONCONTIGUOUS_MEM = 0x00000100,
- PRIV_FLAGS_HW_FB = 0x00000200,
+ PRIV_FLAGS_CACHED = 0x00000200,
PRIV_FLAGS_SECURE_BUFFER = 0x00000400,
// For explicit synchronization
PRIV_FLAGS_UNSYNCHRONIZED = 0x00000800,
diff --git a/libgralloc/mapper.cpp b/libgralloc/mapper.cpp
index 3053d47..bc98e07 100644
--- a/libgralloc/mapper.cpp
+++ b/libgralloc/mapper.cpp
@@ -237,36 +237,25 @@
pthread_mutex_unlock(lock);
}
if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION and
- not useUncached(usage)) {
- bool nonCPUWriters = hnd->flags & (
- private_handle_t::PRIV_FLAGS_HW_RENDER |
- private_handle_t::PRIV_FLAGS_HW_FB |
- private_handle_t::PRIV_FLAGS_VIDEO_ENCODER |
- private_handle_t::PRIV_FLAGS_CAMERA_WRITE);
-
+ hnd->flags & private_handle_t::PRIV_FLAGS_CACHED) {
//Invalidate if CPU reads in software and there are non-CPU
//writers. No need to do this for the metadata buffer as it is
//only read/written in software.
- //Corner case: If we reach here with a READ_RARELY, then there must
- //be a WRITE_OFTEN that caused caching to be used.
- if ((usage & GRALLOC_USAGE_SW_READ_MASK) and nonCPUWriters) {
+ if ((usage & GRALLOC_USAGE_SW_READ_MASK) and
+ (hnd->flags & private_handle_t::PRIV_FLAGS_NON_CPU_WRITER))
+ {
IMemAlloc* memalloc = getAllocator(hnd->flags) ;
err = memalloc->clean_buffer((void*)hnd->base,
hnd->size, hnd->offset, hnd->fd,
CACHE_INVALIDATE);
}
//Mark the buffer to be flushed after CPU write.
- //Corner case: If we reach here with a WRITE_RARELY, then there
- //must be a READ_OFTEN that caused caching to be used.
if (usage & GRALLOC_USAGE_SW_WRITE_MASK) {
hnd->flags |= private_handle_t::PRIV_FLAGS_NEEDS_FLUSH;
}
}
}
- if(useUncached(usage))
- hnd->flags |= private_handle_t::PRIV_FLAGS_DO_NOT_FLUSH;
-
return err;
}
@@ -314,9 +303,6 @@
hnd->flags &= ~private_handle_t::PRIV_FLAGS_NEEDS_FLUSH;
}
- if(hnd->flags & private_handle_t::PRIV_FLAGS_DO_NOT_FLUSH)
- hnd->flags &= ~private_handle_t::PRIV_FLAGS_DO_NOT_FLUSH;
-
return err;
}