Merge "hwc: Clean up ExternalDisplay class"
diff --git a/common.mk b/common.mk
index 44bef71..14d8ded 100644
--- a/common.mk
+++ b/common.mk
@@ -19,8 +19,7 @@
 
 #Common C flags
 common_flags := -DDEBUG_CALC_FPS -Wno-missing-field-initializers
-#TODO: Add -Werror back once all the current warnings are fixed
-common_flags += -Wconversion -Wall
+common_flags += -Wconversion -Wall -Werror
 
 ifeq ($(ARCH_ARM_HAVE_NEON),true)
     common_flags += -D__ARM_HAVE_NEON
diff --git a/libcopybit/copybit_c2d.cpp b/libcopybit/copybit_c2d.cpp
index d2b00dd..7841bb7 100644
--- a/libcopybit/copybit_c2d.cpp
+++ b/libcopybit/copybit_c2d.cpp
@@ -503,7 +503,7 @@
     struct private_handle_t* handle = (struct private_handle_t*)rhs->handle;
     C2D_SURFACE_TYPE surfaceType;
     int status = COPYBIT_SUCCESS;
-    uintptr_t gpuaddr = 0;
+    uint64_t gpuaddr = 0;
     int c2d_format;
     mapped_idx = -1;
 
@@ -1179,7 +1179,7 @@
         return COPYBIT_FAILURE;
     }
     if (need_temp_dst) {
-        if (get_size(dst_info) != ctx->temp_dst_buffer.size) {
+        if (get_size(dst_info) != (int) ctx->temp_dst_buffer.size) {
             free_temp_buffer(ctx->temp_dst_buffer);
             // Create a temp buffer and set that as the destination.
             if (COPYBIT_FAILURE == get_temp_buffer(dst_info, ctx->temp_dst_buffer)) {
@@ -1261,7 +1261,7 @@
         return COPYBIT_FAILURE;
     }
     if (need_temp_src) {
-        if (get_size(src_info) != ctx->temp_src_buffer.size) {
+        if (get_size(src_info) != (int) ctx->temp_src_buffer.size) {
             free_temp_buffer(ctx->temp_src_buffer);
             // Create a temp buffer and set that as the destination.
             if (COPYBIT_SUCCESS != get_temp_buffer(src_info,
diff --git a/libcopybit/software_converter.cpp b/libcopybit/software_converter.cpp
index 9ea8767..e5c03b5 100644
--- a/libcopybit/software_converter.cpp
+++ b/libcopybit/software_converter.cpp
@@ -213,7 +213,7 @@
             return COPYBIT_FAILURE;
     }
 
-    ret = copy_source_to_destination(hnd->base, dst_hnd->base, info);
+    ret = copy_source_to_destination((uintptr_t) hnd->base, (uintptr_t) dst_hnd->base, info);
     return ret;
 }
 
@@ -258,6 +258,6 @@
             return -1;
     }
 
-    ret = copy_source_to_destination(hnd->base, dst_hnd->base, info);
+    ret = copy_source_to_destination((uintptr_t) hnd->base, (uintptr_t) dst_hnd->base, info);
     return ret;
 }
diff --git a/libgralloc/alloc_controller.cpp b/libgralloc/alloc_controller.cpp
index 0e80843..c361095 100644
--- a/libgralloc/alloc_controller.cpp
+++ b/libgralloc/alloc_controller.cpp
@@ -76,16 +76,6 @@
     return true;
 }
 
-static bool useUncached(int usage)
-{
-    if (usage & GRALLOC_USAGE_PRIVATE_UNCACHED)
-        return true;
-    if(((usage & GRALLOC_USAGE_SW_WRITE_MASK) == GRALLOC_USAGE_SW_WRITE_RARELY)
-       ||((usage & GRALLOC_USAGE_SW_READ_MASK) == GRALLOC_USAGE_SW_READ_RARELY))
-        return true;
-    return false;
-}
-
 //-------------- AdrenoMemInfo-----------------------//
 AdrenoMemInfo::AdrenoMemInfo()
 {
@@ -664,3 +654,14 @@
         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/gr.h b/libgralloc/gr.h
index 797d57e..37ee4e5 100644
--- a/libgralloc/gr.h
+++ b/libgralloc/gr.h
@@ -71,6 +71,10 @@
 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 f029b80..75b8349 100644
--- a/libgralloc/gralloc_priv.h
+++ b/libgralloc/gralloc_priv.h
@@ -180,6 +180,7 @@
             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_SW_LOCK            = 0x00000080,
             PRIV_FLAGS_NONCONTIGUOUS_MEM  = 0x00000100,
diff --git a/libgralloc/ionalloc.cpp b/libgralloc/ionalloc.cpp
index 73bbe23..23e225d 100644
--- a/libgralloc/ionalloc.cpp
+++ b/libgralloc/ionalloc.cpp
@@ -28,6 +28,7 @@
  */
 
 #define DEBUG 0
+#define ATRACE_TAG (ATRACE_TAG_GRAPHICS | ATRACE_TAG_HAL)
 #include <sys/ioctl.h>
 #include <sys/mman.h>
 #include <stdlib.h>
@@ -41,7 +42,6 @@
 using gralloc::IonAlloc;
 
 #define ION_DEVICE "/dev/ion"
-#define ATRACE_TAG (ATRACE_TAG_GRAPHICS | ATRACE_TAG_HAL)
 
 int IonAlloc::open_device()
 {
diff --git a/libgralloc/mapper.cpp b/libgralloc/mapper.cpp
index 13e662a..5ba737a 100644
--- a/libgralloc/mapper.cpp
+++ b/libgralloc/mapper.cpp
@@ -15,6 +15,7 @@
  * limitations under the License.
  */
 
+#define ATRACE_TAG (ATRACE_TAG_GRAPHICS | ATRACE_TAG_HAL)
 #include <limits.h>
 #include <errno.h>
 #include <pthread.h>
@@ -40,7 +41,6 @@
 #include "memalloc.h"
 #include <qdMetaData.h>
 
-#define ATRACE_TAG (ATRACE_TAG_GRAPHICS | ATRACE_TAG_HAL)
 
 using namespace gralloc;
 /*****************************************************************************/
@@ -229,21 +229,37 @@
             err = gralloc_map(module, handle);
             pthread_mutex_unlock(lock);
         }
-        if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION) {
-            //Invalidate if reading in software. No need to do this for the
-            //metadata buffer as it is only read/written in software.
-            IMemAlloc* memalloc = getAllocator(hnd->flags) ;
-            err = memalloc->clean_buffer((void*)hnd->base,
-                                         hnd->size, hnd->offset, hnd->fd,
-                                         CACHE_INVALIDATE);
+        if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION and
+                    not useUncached(usage)) {
+            bool nonCPUWriters = usage & (
+                        GRALLOC_USAGE_HW_RENDER |
+                        GRALLOC_USAGE_HW_FB |
+                        GRALLOC_USAGE_HW_VIDEO_ENCODER |
+                        GRALLOC_USAGE_HW_CAMERA_WRITE);
+
+            //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) {
+                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) {
-                // Mark the buffer to be flushed after cpu read/write
                 hnd->flags |= private_handle_t::PRIV_FLAGS_NEEDS_FLUSH;
             }
         }
-    } else {
-        hnd->flags |= private_handle_t::PRIV_FLAGS_DO_NOT_FLUSH;
     }
+
+    if(useUncached(usage))
+        hnd->flags |= private_handle_t::PRIV_FLAGS_DO_NOT_FLUSH;
+
     return err;
 }
 
@@ -283,24 +299,17 @@
     int err = 0;
     private_handle_t* hnd = (private_handle_t*)handle;
 
-    if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION) {
-        IMemAlloc* memalloc = getAllocator(hnd->flags);
-        if (hnd->flags & private_handle_t::PRIV_FLAGS_NEEDS_FLUSH) {
-            err = memalloc->clean_buffer((void*)hnd->base,
-                                         hnd->size, hnd->offset, hnd->fd,
-                                         CACHE_CLEAN_AND_INVALIDATE);
-            hnd->flags &= ~private_handle_t::PRIV_FLAGS_NEEDS_FLUSH;
-        } else if(hnd->flags & private_handle_t::PRIV_FLAGS_DO_NOT_FLUSH) {
-            hnd->flags &= ~private_handle_t::PRIV_FLAGS_DO_NOT_FLUSH;
-        } else {
-            //Probably a round about way to do this, but this avoids adding new
-            //flags
-            err = memalloc->clean_buffer((void*)hnd->base,
-                                         hnd->size, hnd->offset, hnd->fd,
-                                         CACHE_INVALIDATE);
-        }
+    IMemAlloc* memalloc = getAllocator(hnd->flags);
+    if (hnd->flags & private_handle_t::PRIV_FLAGS_NEEDS_FLUSH) {
+        err = memalloc->clean_buffer((void*)hnd->base,
+                hnd->size, hnd->offset, hnd->fd,
+                CACHE_CLEAN);
+        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;
 }
 
diff --git a/libhwcomposer/hwc_ad.cpp b/libhwcomposer/hwc_ad.cpp
index f849450..679e6d6 100644
--- a/libhwcomposer/hwc_ad.cpp
+++ b/libhwcomposer/hwc_ad.cpp
@@ -135,7 +135,7 @@
         const hwc_layer_1_t* layer = &list->hwLayers[nYuvIndex];
         private_handle_t *hnd = (private_handle_t *)layer->handle;
         qdutils::MDPVersion& mdpHw =  qdutils::MDPVersion::getInstance();
-        if(hnd && hnd->width <= mdpHw.getMaxMixerWidth()) {
+        if(hnd && hnd->width <= (int) mdpHw.getMaxMixerWidth()) {
             mDoable = true;
         }
     }
diff --git a/libhwcomposer/hwc_dump_layers.cpp b/libhwcomposer/hwc_dump_layers.cpp
index c8ebac9..0a92def 100644
--- a/libhwcomposer/hwc_dump_layers.cpp
+++ b/libhwcomposer/hwc_dump_layers.cpp
@@ -37,8 +37,12 @@
 #include <sys/stat.h>
 #include <comptype.h>
 #ifdef QCOM_BSP
+// Ignore Wconversion errors for external headers
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wconversion"
 #include <SkBitmap.h>
 #include <SkImageEncoder.h>
+#pragma GCC diagnostic pop
 #endif
 #ifdef STDC_FORMAT_MACROS
 #include <inttypes.h>
diff --git a/libhwcomposer/hwc_mdpcomp.cpp b/libhwcomposer/hwc_mdpcomp.cpp
index 08cfbde..072eda3 100644
--- a/libhwcomposer/hwc_mdpcomp.cpp
+++ b/libhwcomposer/hwc_mdpcomp.cpp
@@ -372,7 +372,7 @@
                 /* On targets that doesnt support Decimation (eg.,8x26)
                  * maximum downscale support is overlay pipe downscale.
                  */
-                if(crop_w > mdpHw.getMaxMixerWidth() ||
+                if(crop_w > (int) mdpHw.getMaxMixerWidth() ||
                         w_dscale > maxMDPDownscale ||
                         h_dscale > maxMDPDownscale)
                     return false;
@@ -383,7 +383,7 @@
                      *      1. Src crop > Mixer limit on nonsplit MDPComp
                      *      2. exceeds maximum downscale limit
                      */
-                    if(((crop_w > mdpHw.getMaxMixerWidth()) &&
+                    if(((crop_w > (int) mdpHw.getMaxMixerWidth()) &&
                                 !sSrcSplitEnabled) ||
                             w_dscale > maxMDPDownscale ||
                             h_dscale > maxMDPDownscale) {
@@ -712,7 +712,7 @@
 
     MDPVersion& mdpHw = MDPVersion::getInstance();
     if(mDpy > HWC_DISPLAY_PRIMARY &&
-            (priDispW >  mdpHw.getMaxMixerWidth()) &&
+            (priDispW >  (int) mdpHw.getMaxMixerWidth()) &&
             (ctx->dpyAttr[mDpy].xres <  mdpHw.getMaxMixerWidth())) {
         // Disable MDP comp on Secondary when the primary is highres panel and
         // the secondary is a normal 1080p, because, MDP comp on secondary under
@@ -2475,8 +2475,8 @@
     //pipe line length, we are still using 2 pipes. This is fine just because
     //this is source split where destination doesn't matter. Evaluate later to
     //see if going through all the calcs to save a pipe is worth it
-    if(dstWidth > mdpHw.getMaxMixerWidth() or
-            cropWidth > mdpHw.getMaxMixerWidth() or
+    if(dstWidth > (int) mdpHw.getMaxMixerWidth() or
+            cropWidth > (int) mdpHw.getMaxMixerWidth() or
             (primarySplitAlways and (cropWidth > lSplit))) {
         pipe_info.rIndex = ctx->mOverlay->getPipe(pipeSpecs);
         if(pipe_info.rIndex == ovutils::OV_INVALID) {
diff --git a/libhwcomposer/hwc_utils.cpp b/libhwcomposer/hwc_utils.cpp
index a477fc0..44725de 100644
--- a/libhwcomposer/hwc_utils.cpp
+++ b/libhwcomposer/hwc_utils.cpp
@@ -2337,7 +2337,7 @@
         swap(src_w, src_h);
     }
     //src width > MAX mixer supported dim
-    if(src_w > qdutils::MDPVersion::getInstance().getMaxMixerWidth()) {
+    if(src_w > (int) qdutils::MDPVersion::getInstance().getMaxMixerWidth()) {
         return;
     }
     //Decimation necessary, cannot use BWC. H/W requirement.
diff --git a/libhwcomposer/hwc_virtual.cpp b/libhwcomposer/hwc_virtual.cpp
index 415a5e7..d33a827 100644
--- a/libhwcomposer/hwc_virtual.cpp
+++ b/libhwcomposer/hwc_virtual.cpp
@@ -21,7 +21,6 @@
 #include <errno.h>
 
 #include <cutils/log.h>
-#include <utils/Trace.h>
 #include <overlayWriteback.h>
 #include "hwc_utils.h"
 #include "hwc_fbupdate.h"
@@ -30,6 +29,7 @@
 #include "hwc_copybit.h"
 #include "hwc_virtual.h"
 #include "sync/sync.h"
+#include <utils/Trace.h>
 
 #define HWCVIRTUAL_LOG 0
 
diff --git a/libmemtrack/Android.mk b/libmemtrack/Android.mk
index 2fda0b7..3ed37a3 100644
--- a/libmemtrack/Android.mk
+++ b/libmemtrack/Android.mk
@@ -20,6 +20,7 @@
 
 LOCAL_MODULE_RELATIVE_PATH := hw
 LOCAL_C_INCLUDES += hardware/libhardware/include
+LOCAL_CFAGS := -Wconversion -Wall -Werror
 LOCAL_SHARED_LIBRARIES := liblog
 LOCAL_SRC_FILES := memtrack_msm.c kgsl.c
 LOCAL_MODULE := memtrack.$(TARGET_BOARD_PLATFORM)
diff --git a/libmemtrack/kgsl.c b/libmemtrack/kgsl.c
index 208b22f..3026175 100644
--- a/libmemtrack/kgsl.c
+++ b/libmemtrack/kgsl.c
@@ -99,7 +99,7 @@
          *  gpuaddr useraddr     size    id flags       type            usage sglen
          * 545ba000 545ba000     4096     1 ----pY     gpumem      arraybuffer     1
          */
-        ret = sscanf(line, "%*x %*lx %lu %*d %6s %6s %18s %*d\n",
+        ret = sscanf(line, "%*x %*x %lu %*d %6s %6s %18s %*d\n",
                      &size, flags, line_type, line_usage);
         if (ret != 4) {
             continue;
diff --git a/liboverlay/overlayUtils.cpp b/liboverlay/overlayUtils.cpp
index f9ee326..cbd52ae 100644
--- a/liboverlay/overlayUtils.cpp
+++ b/liboverlay/overlayUtils.cpp
@@ -272,7 +272,7 @@
     if((int)verDscale)
         vertDeci = (uint8_t)log2f(verDscale);
 
-    if(src_w > mdpHw.getMaxMixerWidth()) {
+    if(src_w > (int) mdpHw.getMaxMixerWidth()) {
         //If the client sends us something > what a layer mixer supports
         //then it means it doesn't want to use split-pipe but wants us to
         //decimate. A minimum decimation of 2 will ensure that the width is
diff --git a/libqdutils/mdp_version.h b/libqdutils/mdp_version.h
index dd1aa3d..d26c55c 100644
--- a/libqdutils/mdp_version.h
+++ b/libqdutils/mdp_version.h
@@ -137,7 +137,7 @@
     bool isDynFpsSupported() { return mPanelInfo.mDynFpsSupported; }
     uint32_t getMinFpsSupported() { return mPanelInfo.mMinFps; }
     uint32_t getMaxFpsSupported() { return mPanelInfo.mMaxFps; }
-    int getMaxMixerWidth() const { return mMaxMixerWidth; }
+    uint32_t getMaxMixerWidth() const { return mMaxMixerWidth; }
     bool isSrcSplit() const;
     bool isSrcSplitAlways() const;
     bool isRGBScalarSupported() const;