libui: Enable -Weverything and -Werror

Enables -Weverything and -Werror, with just a few exceptions for
warnings we can't (or shouldn't need to) work around.

Change-Id: I470a267e9b1c373f2d5c960f005c3b0e02b2db63
diff --git a/libs/ui/Android.mk b/libs/ui/Android.mk
index eff540a..72430d2 100644
--- a/libs/ui/Android.mk
+++ b/libs/ui/Android.mk
@@ -16,7 +16,19 @@
 include $(CLEAR_VARS)
 
 LOCAL_CLANG := true
-LOCAL_CPPFLAGS := -std=c++11
+LOCAL_CPPFLAGS := -std=c++1y -Weverything -Werror
+
+# The static constructors and destructors in this library have not been noted to
+# introduce significant overheads
+LOCAL_CPPFLAGS += -Wno-exit-time-destructors
+LOCAL_CPPFLAGS += -Wno-global-constructors
+
+# We only care about compiling as C++14
+LOCAL_CPPFLAGS += -Wno-c++98-compat-pedantic
+
+# We use four-character constants for the GraphicBuffer header, and don't care
+# that they're non-portable as long as they're consistent within one execution
+LOCAL_CPPFLAGS += -Wno-four-char-constants
 
 LOCAL_SRC_FILES := \
 	Fence.cpp \
diff --git a/libs/ui/Fence.cpp b/libs/ui/Fence.cpp
index 3c0306c..9cf2881 100644
--- a/libs/ui/Fence.cpp
+++ b/libs/ui/Fence.cpp
@@ -18,10 +18,13 @@
 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
 //#define LOG_NDEBUG 0
 
- // This is needed for stdint.h to define INT64_MAX in C++
- #define __STDC_LIMIT_MACROS
-
+// We would eliminate the non-conforming zero-length array, but we can't since
+// this is effectively included from the Linux kernel
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wzero-length-array"
 #include <sync/sync.h>
+#pragma clang diagnostic pop
+
 #include <ui/Fence.h>
 #include <unistd.h>
 #include <utils/Log.h>
@@ -45,7 +48,7 @@
     }
 }
 
-status_t Fence::wait(unsigned int timeout) {
+status_t Fence::wait(int timeout) {
     ATRACE_CALL();
     if (mFenceFd == -1) {
         return NO_ERROR;
@@ -59,7 +62,7 @@
     if (mFenceFd == -1) {
         return NO_ERROR;
     }
-    unsigned int warningTimeout = 3000;
+    int warningTimeout = 3000;
     int err = sync_wait(mFenceFd, warningTimeout);
     if (err < 0 && errno == ETIME) {
         ALOGE("%s: fence %d didn't signal in %u ms", logname, mFenceFd,
@@ -138,7 +141,7 @@
     if (size < getFlattenedSize() || count < getFdCount()) {
         return NO_MEMORY;
     }
-    FlattenableUtils::write(buffer, size, (uint32_t)getFdCount());
+    FlattenableUtils::write(buffer, size, getFdCount());
     if (isValid()) {
         *fds++ = mFenceFd;
         count--;
diff --git a/libs/ui/FramebufferNativeWindow.cpp b/libs/ui/FramebufferNativeWindow.cpp
index 295300e..30f2ae2 100644
--- a/libs/ui/FramebufferNativeWindow.cpp
+++ b/libs/ui/FramebufferNativeWindow.cpp
@@ -1,17 +1,17 @@
-/* 
+/*
 **
 ** Copyright 2007 The Android Open Source Project
 **
-** Licensed under the Apache License Version 2.0(the "License"); 
-** you may not use this file except in compliance with the License. 
-** You may obtain a copy of the License at 
+** Licensed under the Apache License Version 2.0(the "License");
+** you may not use this file except in compliance with the License.
+** You may obtain a copy of the License at
 **
-**     http://www.apache.org/licenses/LICENSE-2.0 
+**     http://www.apache.org/licenses/LICENSE-2.0
 **
-** Unless required by applicable law or agreed to in writing software 
-** distributed under the License is distributed on an "AS IS" BASIS 
-** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND either express or implied. 
-** See the License for the specific language governing permissions and 
+** Unless required by applicable law or agreed to in writing software
+** distributed under the License is distributed on an "AS IS" BASIS
+** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND either express or implied.
+** See the License for the specific language governing permissions and
 ** limitations under the License.
 */
 
@@ -43,11 +43,11 @@
 namespace android {
 // ----------------------------------------------------------------------------
 
-class NativeBuffer 
+class NativeBuffer final
     : public ANativeObjectBase<
-        ANativeWindowBuffer, 
-        NativeBuffer, 
-        LightRefBase<NativeBuffer> >
+        ANativeWindowBuffer,
+        NativeBuffer,
+        LightRefBase<NativeBuffer>>
 {
 public:
     NativeBuffer(int w, int h, int f, int u) : BASE() {
@@ -57,24 +57,23 @@
         ANativeWindowBuffer::usage  = u;
     }
 private:
-    friend class LightRefBase<NativeBuffer>;    
-    ~NativeBuffer() { }; // this class cannot be overloaded
+    friend class LightRefBase<NativeBuffer>;
 };
 
 
 /*
  * This implements the (main) framebuffer management. This class is used
  * mostly by SurfaceFlinger, but also by command line GL application.
- * 
+ *
  * In fact this is an implementation of ANativeWindow on top of
  * the framebuffer.
- * 
- * Currently it is pretty simple, it manages only two buffers (the front and 
+ *
+ * Currently it is pretty simple, it manages only two buffers (the front and
  * back buffer).
- * 
+ *
  */
 
-FramebufferNativeWindow::FramebufferNativeWindow() 
+FramebufferNativeWindow::FramebufferNativeWindow()
     : BASE(), fbDev(0), grDev(0), mUpdateOnDemand(false)
 {
     hw_module_t const* module;
@@ -84,16 +83,16 @@
         int i;
         err = framebuffer_open(module, &fbDev);
         ALOGE_IF(err, "couldn't open framebuffer HAL (%s)", strerror(-err));
-        
+
         err = gralloc_open(module, &grDev);
         ALOGE_IF(err, "couldn't open gralloc HAL (%s)", strerror(-err));
 
         // bail out if we can't initialize the modules
         if (!fbDev || !grDev)
             return;
-        
+
         mUpdateOnDemand = (fbDev->setUpdateRect != 0);
-        
+
         // initialize the buffer FIFO
         if(fbDev->numFramebuffers >= MIN_NUM_FRAME_BUFFERS &&
            fbDev->numFramebuffers <= MAX_NUM_FRAME_BUFFERS){
@@ -116,36 +115,37 @@
         *((uint32_t *)&fbDev->format) = FRAMEBUFFER_FORCE_FORMAT;
 #endif
 
-        for (i = 0; i < mNumBuffers; i++)
-        {
-                buffers[i] = new NativeBuffer(
-                        fbDev->width, fbDev->height, fbDev->format, GRALLOC_USAGE_HW_FB);
+        for (i = 0; i < mNumBuffers; i++) {
+            buffers[i] = new NativeBuffer(
+                    static_cast<int>(fbDev->width),
+                    static_cast<int>(fbDev->height),
+                    fbDev->format, GRALLOC_USAGE_HW_FB);
         }
 
-        for (i = 0; i < mNumBuffers; i++)
-        {
-                err = grDev->alloc(grDev,
-                        fbDev->width, fbDev->height, fbDev->format,
-                        GRALLOC_USAGE_HW_FB, &buffers[i]->handle, &buffers[i]->stride);
+        for (i = 0; i < mNumBuffers; i++) {
+            err = grDev->alloc(grDev,
+                    static_cast<int>(fbDev->width),
+                    static_cast<int>(fbDev->height),
+                    fbDev->format, GRALLOC_USAGE_HW_FB,
+                    &buffers[i]->handle, &buffers[i]->stride);
 
-                ALOGE_IF(err, "fb buffer %d allocation failed w=%d, h=%d, err=%s",
-                        i, fbDev->width, fbDev->height, strerror(-err));
+            ALOGE_IF(err, "fb buffer %d allocation failed w=%d, h=%d, err=%s",
+                    i, fbDev->width, fbDev->height, strerror(-err));
 
-                if (err)
-                {
-                        mNumBuffers = i;
-                        mNumFreeBuffers = i;
-                        mBufferHead = mNumBuffers-1;
-                        break;
-                }
+            if (err) {
+                mNumBuffers = i;
+                mNumFreeBuffers = i;
+                mBufferHead = mNumBuffers-1;
+                break;
+            }
         }
 
-        const_cast<uint32_t&>(ANativeWindow::flags) = fbDev->flags; 
+        const_cast<uint32_t&>(ANativeWindow::flags) = fbDev->flags;
         const_cast<float&>(ANativeWindow::xdpi) = fbDev->xdpi;
         const_cast<float&>(ANativeWindow::ydpi) = fbDev->ydpi;
-        const_cast<int&>(ANativeWindow::minSwapInterval) = 
+        const_cast<int&>(ANativeWindow::minSwapInterval) =
             fbDev->minSwapInterval;
-        const_cast<int&>(ANativeWindow::maxSwapInterval) = 
+        const_cast<int&>(ANativeWindow::maxSwapInterval) =
             fbDev->maxSwapInterval;
     } else {
         ALOGE("Couldn't get gralloc module");
@@ -162,7 +162,7 @@
     ANativeWindow::queueBuffer_DEPRECATED = queueBuffer_DEPRECATED;
 }
 
-FramebufferNativeWindow::~FramebufferNativeWindow() 
+FramebufferNativeWindow::~FramebufferNativeWindow()
 {
     if (grDev) {
         for(int i = 0; i < mNumBuffers; i++) {
@@ -178,7 +178,7 @@
     }
 }
 
-status_t FramebufferNativeWindow::setUpdateRectangle(const Rect& r) 
+status_t FramebufferNativeWindow::setUpdateRectangle(const Rect& r)
 {
     if (!mUpdateOnDemand) {
         return INVALID_OPERATION;
@@ -195,7 +195,7 @@
 }
 
 int FramebufferNativeWindow::setSwapInterval(
-        ANativeWindow* window, int interval) 
+        ANativeWindow* window, int interval)
 {
     framebuffer_device_t* fb = getSelf(window)->fbDev;
     return fb->setSwapInterval(fb, interval);
@@ -219,7 +219,7 @@
     return index;
 }
 
-int FramebufferNativeWindow::dequeueBuffer_DEPRECATED(ANativeWindow* window, 
+int FramebufferNativeWindow::dequeueBuffer_DEPRECATED(ANativeWindow* window,
         ANativeWindowBuffer** buffer)
 {
     int fenceFd = -1;
@@ -234,7 +234,7 @@
     return result;
 }
 
-int FramebufferNativeWindow::dequeueBuffer(ANativeWindow* window, 
+int FramebufferNativeWindow::dequeueBuffer(ANativeWindow* window,
         ANativeWindowBuffer** buffer, int* fenceFd)
 {
     FramebufferNativeWindow* self = getSelf(window);
@@ -249,7 +249,7 @@
     while (self->mNumFreeBuffers < 2) {
         self->mCondition.wait(self->mutex);
     }
-    ALOG_ASSERT(self->buffers[index] != self->front);
+    ALOG_ASSERT(self->buffers[index] != self->front, "");
 
     // get this buffer
     self->mNumFreeBuffers--;
@@ -261,19 +261,19 @@
     return 0;
 }
 
-int FramebufferNativeWindow::lockBuffer_DEPRECATED(ANativeWindow* /*window*/, 
+int FramebufferNativeWindow::lockBuffer_DEPRECATED(ANativeWindow* /*window*/,
         ANativeWindowBuffer* /*buffer*/)
 {
     return NO_ERROR;
 }
 
-int FramebufferNativeWindow::queueBuffer_DEPRECATED(ANativeWindow* window, 
+int FramebufferNativeWindow::queueBuffer_DEPRECATED(ANativeWindow* window,
         ANativeWindowBuffer* buffer)
 {
     return queueBuffer(window, buffer, -1);
 }
 
-int FramebufferNativeWindow::queueBuffer(ANativeWindow* window, 
+int FramebufferNativeWindow::queueBuffer(ANativeWindow* window,
         ANativeWindowBuffer* buffer, int fenceFd)
 {
     FramebufferNativeWindow* self = getSelf(window);
@@ -293,17 +293,17 @@
 }
 
 int FramebufferNativeWindow::query(const ANativeWindow* window,
-        int what, int* value) 
+        int what, int* value)
 {
     const FramebufferNativeWindow* self = getSelf(window);
     Mutex::Autolock _l(self->mutex);
     framebuffer_device_t* fb = self->fbDev;
     switch (what) {
         case NATIVE_WINDOW_WIDTH:
-            *value = fb->width;
+            *value = static_cast<int>(fb->width);
             return NO_ERROR;
         case NATIVE_WINDOW_HEIGHT:
-            *value = fb->height;
+            *value = static_cast<int>(fb->height);
             return NO_ERROR;
         case NATIVE_WINDOW_FORMAT:
             *value = fb->format;
@@ -315,10 +315,10 @@
             *value = 0;
             return NO_ERROR;
         case NATIVE_WINDOW_DEFAULT_WIDTH:
-            *value = fb->width;
+            *value = static_cast<int>(fb->width);
             return NO_ERROR;
         case NATIVE_WINDOW_DEFAULT_HEIGHT:
-            *value = fb->height;
+            *value = static_cast<int>(fb->height);
             return NO_ERROR;
         case NATIVE_WINDOW_TRANSFORM_HINT:
             *value = 0;
@@ -359,7 +359,8 @@
 }; // namespace android
 // ----------------------------------------------------------------------------
 
-using namespace android;
+using android::sp;
+using android::FramebufferNativeWindow;
 
 EGLNativeWindowType android_createDisplaySurface(void)
 {
@@ -370,5 +371,5 @@
         sp<FramebufferNativeWindow> ref(w);
         return NULL;
     }
-    return (EGLNativeWindowType)w;
+    return static_cast<EGLNativeWindowType>(w);
 }
diff --git a/libs/ui/GraphicBuffer.cpp b/libs/ui/GraphicBuffer.cpp
index f4e781b..825baf4 100644
--- a/libs/ui/GraphicBuffer.cpp
+++ b/libs/ui/GraphicBuffer.cpp
@@ -53,7 +53,8 @@
     handle = NULL;
 }
 
-GraphicBuffer::GraphicBuffer(int w, int h, PixelFormat reqFormat, int reqUsage)
+GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight,
+        PixelFormat inFormat, uint32_t inUsage)
     : BASE(), mOwner(ownData), mBufferMapper(GraphicBufferMapper::get()),
       mInitCheck(NO_ERROR), mId(getUniqueId())
 {
@@ -63,20 +64,21 @@
     format =
     usage  = 0;
     handle = NULL;
-    mInitCheck = initSize(w, h, reqFormat, reqUsage);
+    mInitCheck = initSize(inWidth, inHeight, inFormat, inUsage);
 }
 
-GraphicBuffer::GraphicBuffer(int w, int h, PixelFormat inFormat, int inUsage,
-        int inStride, native_handle_t* inHandle, bool keepOwnership)
+GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight,
+        PixelFormat inFormat, uint32_t inUsage, uint32_t inStride,
+        native_handle_t* inHandle, bool keepOwnership)
     : BASE(), mOwner(keepOwnership ? ownHandle : ownNone),
       mBufferMapper(GraphicBufferMapper::get()),
       mInitCheck(NO_ERROR), mId(getUniqueId())
 {
-    width  = w;
-    height = h;
-    stride = inStride;
+    width  = static_cast<int>(inWidth);
+    height = static_cast<int>(inHeight);
+    stride = static_cast<int>(inStride);
     format = inFormat;
-    usage  = inUsage;
+    usage  = static_cast<int>(inUsage);
     handle = inHandle;
 }
 
@@ -129,12 +131,17 @@
             const_cast<GraphicBuffer*>(this));
 }
 
-status_t GraphicBuffer::reallocate(int w, int h, PixelFormat f, int reqUsage)
+status_t GraphicBuffer::reallocate(uint32_t inWidth, uint32_t inHeight,
+        PixelFormat inFormat, uint32_t inUsage)
 {
     if (mOwner != ownData)
         return INVALID_OPERATION;
 
-    if (handle && w==width && h==height && f==format && reqUsage==usage)
+    if (handle &&
+            static_cast<int>(inWidth) == width &&
+            static_cast<int>(inHeight) == height &&
+            inFormat == format &&
+            static_cast<int>(inUsage) == usage)
         return NO_ERROR;
 
     if (handle) {
@@ -142,61 +149,64 @@
         allocator.free(handle);
         handle = 0;
     }
-    return initSize(w, h, f, reqUsage);
+    return initSize(inWidth, inHeight, inFormat, inUsage);
 }
 
-status_t GraphicBuffer::initSize(uint32_t w, uint32_t h, PixelFormat format,
-        uint32_t reqUsage)
+status_t GraphicBuffer::initSize(uint32_t inWidth, uint32_t inHeight,
+        PixelFormat inFormat, uint32_t inUsage)
 {
     GraphicBufferAllocator& allocator = GraphicBufferAllocator::get();
-    status_t err = allocator.alloc(w, h, format, reqUsage, &handle, &stride);
+    uint32_t outStride = 0;
+    status_t err = allocator.alloc(inWidth, inHeight, inFormat, inUsage,
+            &handle, &outStride);
     if (err == NO_ERROR) {
-        this->width  = w;
-        this->height = h;
-        this->format = format;
-        this->usage  = reqUsage;
+        width = static_cast<int>(inWidth);
+        height = static_cast<int>(inHeight);
+        format = inFormat;
+        usage = static_cast<int>(inUsage);
+        stride = static_cast<int>(outStride);
     }
     return err;
 }
 
-status_t GraphicBuffer::lock(uint32_t usage, void** vaddr)
+status_t GraphicBuffer::lock(uint32_t inUsage, void** vaddr)
 {
     const Rect lockBounds(width, height);
-    status_t res = lock(usage, lockBounds, vaddr);
+    status_t res = lock(inUsage, lockBounds, vaddr);
     return res;
 }
 
-status_t GraphicBuffer::lock(uint32_t usage, const Rect& rect, void** vaddr)
+status_t GraphicBuffer::lock(uint32_t inUsage, const Rect& rect, void** vaddr)
 {
-    if (rect.left < 0 || rect.right  > this->width ||
-        rect.top  < 0 || rect.bottom > this->height) {
+    if (rect.left < 0 || rect.right  > width ||
+        rect.top  < 0 || rect.bottom > height) {
         ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
                 rect.left, rect.top, rect.right, rect.bottom,
-                this->width, this->height);
+                width, height);
         return BAD_VALUE;
     }
-    status_t res = getBufferMapper().lock(handle, usage, rect, vaddr);
+    status_t res = getBufferMapper().lock(handle, inUsage, rect, vaddr);
     return res;
 }
 
-status_t GraphicBuffer::lockYCbCr(uint32_t usage, android_ycbcr *ycbcr)
+status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, android_ycbcr* ycbcr)
 {
     const Rect lockBounds(width, height);
-    status_t res = lockYCbCr(usage, lockBounds, ycbcr);
+    status_t res = lockYCbCr(inUsage, lockBounds, ycbcr);
     return res;
 }
 
-status_t GraphicBuffer::lockYCbCr(uint32_t usage, const Rect& rect,
-        android_ycbcr *ycbcr)
+status_t GraphicBuffer::lockYCbCr(uint32_t inUsage, const Rect& rect,
+        android_ycbcr* ycbcr)
 {
-    if (rect.left < 0 || rect.right  > this->width ||
-        rect.top  < 0 || rect.bottom > this->height) {
+    if (rect.left < 0 || rect.right  > width ||
+        rect.top  < 0 || rect.bottom > height) {
         ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
                 rect.left, rect.top, rect.right, rect.bottom,
-                this->width, this->height);
+                width, height);
         return BAD_VALUE;
     }
-    status_t res = getBufferMapper().lockYCbCr(handle, usage, rect, ycbcr);
+    status_t res = getBufferMapper().lockYCbCr(handle, inUsage, rect, ycbcr);
     return res;
 }
 
@@ -206,43 +216,48 @@
     return res;
 }
 
-status_t GraphicBuffer::lockAsync(uint32_t usage, void** vaddr, int fenceFd)
+status_t GraphicBuffer::lockAsync(uint32_t inUsage, void** vaddr, int fenceFd)
 {
     const Rect lockBounds(width, height);
-    status_t res = lockAsync(usage, lockBounds, vaddr, fenceFd);
+    status_t res = lockAsync(inUsage, lockBounds, vaddr, fenceFd);
     return res;
 }
 
-status_t GraphicBuffer::lockAsync(uint32_t usage, const Rect& rect, void** vaddr, int fenceFd)
+status_t GraphicBuffer::lockAsync(uint32_t inUsage, const Rect& rect,
+        void** vaddr, int fenceFd)
 {
-    if (rect.left < 0 || rect.right  > this->width ||
-        rect.top  < 0 || rect.bottom > this->height) {
+    if (rect.left < 0 || rect.right  > width ||
+        rect.top  < 0 || rect.bottom > height) {
         ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
                 rect.left, rect.top, rect.right, rect.bottom,
-                this->width, this->height);
+                width, height);
         return BAD_VALUE;
     }
-    status_t res = getBufferMapper().lockAsync(handle, usage, rect, vaddr, fenceFd);
+    status_t res = getBufferMapper().lockAsync(handle, inUsage, rect, vaddr,
+            fenceFd);
     return res;
 }
 
-status_t GraphicBuffer::lockAsyncYCbCr(uint32_t usage, android_ycbcr *ycbcr, int fenceFd)
+status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, android_ycbcr* ycbcr,
+        int fenceFd)
 {
     const Rect lockBounds(width, height);
-    status_t res = lockAsyncYCbCr(usage, lockBounds, ycbcr, fenceFd);
+    status_t res = lockAsyncYCbCr(inUsage, lockBounds, ycbcr, fenceFd);
     return res;
 }
 
-status_t GraphicBuffer::lockAsyncYCbCr(uint32_t usage, const Rect& rect, android_ycbcr *ycbcr, int fenceFd)
+status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, const Rect& rect,
+        android_ycbcr* ycbcr, int fenceFd)
 {
-    if (rect.left < 0 || rect.right  > this->width ||
-        rect.top  < 0 || rect.bottom > this->height) {
+    if (rect.left < 0 || rect.right  > width ||
+        rect.top  < 0 || rect.bottom > height) {
         ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
                 rect.left, rect.top, rect.right, rect.bottom,
-                this->width, this->height);
+                width, height);
         return BAD_VALUE;
     }
-    status_t res = getBufferMapper().lockAsyncYCbCr(handle, usage, rect, ycbcr, fenceFd);
+    status_t res = getBufferMapper().lockAsyncYCbCr(handle, inUsage, rect,
+            ycbcr, fenceFd);
     return res;
 }
 
@@ -253,11 +268,11 @@
 }
 
 size_t GraphicBuffer::getFlattenedSize() const {
-    return (10 + (handle ? handle->numInts : 0))*sizeof(int);
+    return static_cast<size_t>(10 + (handle ? handle->numInts : 0)) * sizeof(int);
 }
 
 size_t GraphicBuffer::getFdCount() const {
-    return handle ? handle->numFds : 0;
+    return static_cast<size_t>(handle ? handle->numFds : 0);
 }
 
 status_t GraphicBuffer::flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const {
@@ -282,16 +297,17 @@
     if (handle) {
         buf[8] = handle->numFds;
         buf[9] = handle->numInts;
-        native_handle_t const* const h = handle;
-        memcpy(fds,     h->data,             h->numFds*sizeof(int));
-        memcpy(&buf[10], h->data + h->numFds, h->numInts*sizeof(int));
+        memcpy(fds, handle->data,
+                static_cast<size_t>(handle->numFds) * sizeof(int));
+        memcpy(&buf[10], handle->data + handle->numFds,
+                static_cast<size_t>(handle->numInts) * sizeof(int));
     }
 
     buffer = reinterpret_cast<void*>(static_cast<int*>(buffer) + sizeNeeded);
     size -= sizeNeeded;
     if (handle) {
         fds += handle->numFds;
-        count -= handle->numFds;
+        count -= static_cast<size_t>(handle->numFds);
     }
 
     return NO_ERROR;
@@ -304,8 +320,8 @@
     int const* buf = static_cast<int const*>(buffer);
     if (buf[0] != 'GBFR') return BAD_TYPE;
 
-    const size_t numFds  = buf[8];
-    const size_t numInts = buf[9];
+    const size_t numFds  = static_cast<size_t>(buf[8]);
+    const size_t numInts = static_cast<size_t>(buf[9]);
 
     const size_t maxNumber = UINT_MAX / sizeof(int);
     if (numFds >= maxNumber || numInts >= (maxNumber - 10)) {
@@ -333,15 +349,16 @@
         stride = buf[3];
         format = buf[4];
         usage  = buf[5];
-        native_handle* h = native_handle_create(numFds, numInts);
+        native_handle* h = native_handle_create(
+                static_cast<int>(numFds), static_cast<int>(numInts));
         if (!h) {
             width = height = stride = format = usage = 0;
             handle = NULL;
             ALOGE("unflatten: native_handle_create failed");
             return NO_MEMORY;
         }
-        memcpy(h->data,          fds,     numFds*sizeof(int));
-        memcpy(h->data + numFds, &buf[10], numInts*sizeof(int));
+        memcpy(h->data, fds, numFds * sizeof(int));
+        memcpy(h->data + numFds, &buf[10], numInts * sizeof(int));
         handle = h;
     } else {
         width = height = stride = format = usage = 0;
diff --git a/libs/ui/GraphicBufferAllocator.cpp b/libs/ui/GraphicBufferAllocator.cpp
index ff550d9..85e9675 100644
--- a/libs/ui/GraphicBufferAllocator.cpp
+++ b/libs/ui/GraphicBufferAllocator.cpp
@@ -1,17 +1,17 @@
-/* 
+/*
 **
 ** Copyright 2009, The Android Open Source Project
 **
-** Licensed under the Apache License, Version 2.0 (the "License"); 
-** you may not use this file except in compliance with the License. 
-** You may obtain a copy of the License at 
+** Licensed under the Apache License, Version 2.0 (the "License");
+** you may not use this file except in compliance with the License.
+** You may obtain a copy of the License at
 **
-**     http://www.apache.org/licenses/LICENSE-2.0 
+**     http://www.apache.org/licenses/LICENSE-2.0
 **
-** Unless required by applicable law or agreed to in writing, software 
-** distributed under the License is distributed on an "AS IS" BASIS, 
-** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
-** See the License for the specific language governing permissions and 
+** Unless required by applicable law or agreed to in writing, software
+** distributed under the License is distributed on an "AS IS" BASIS,
+** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+** See the License for the specific language governing permissions and
 ** limitations under the License.
 */
 
@@ -66,11 +66,11 @@
         if (rec.size) {
             snprintf(buffer, SIZE, "%10p: %7.2f KiB | %4u (%4u) x %4u | %8X | 0x%08x\n",
                     list.keyAt(i), rec.size/1024.0f,
-                    rec.w, rec.s, rec.h, rec.format, rec.usage);
+                    rec.width, rec.stride, rec.height, rec.format, rec.usage);
         } else {
             snprintf(buffer, SIZE, "%10p: unknown     | %4u (%4u) x %4u | %8X | 0x%08x\n",
                     list.keyAt(i),
-                    rec.w, rec.s, rec.h, rec.format, rec.usage);
+                    rec.width, rec.stride, rec.height, rec.format, rec.usage);
         }
         result.append(buffer);
         total += rec.size;
@@ -90,39 +90,40 @@
     ALOGD("%s", s.string());
 }
 
-status_t GraphicBufferAllocator::alloc(uint32_t w, uint32_t h, PixelFormat format,
-        int usage, buffer_handle_t* handle, int32_t* stride)
+status_t GraphicBufferAllocator::alloc(uint32_t width, uint32_t height,
+        PixelFormat format, uint32_t usage, buffer_handle_t* handle,
+        uint32_t* stride)
 {
     ATRACE_CALL();
+
     // make sure to not allocate a N x 0 or 0 x N buffer, since this is
     // allowed from an API stand-point allocate a 1x1 buffer instead.
-    if (!w || !h)
-        w = h = 1;
+    if (!width || !height)
+        width = height = 1;
 
     // we have a h/w allocator and h/w buffer is requested
-    status_t err; 
-    
-    err = mAllocDev->alloc(mAllocDev, w, h, format, usage, handle, stride);
+    status_t err;
+
+    int outStride = 0;
+    err = mAllocDev->alloc(mAllocDev, static_cast<int>(width),
+            static_cast<int>(height), format, static_cast<int>(usage), handle,
+            &outStride);
+    *stride = static_cast<uint32_t>(outStride);
 
     ALOGW_IF(err, "alloc(%u, %u, %d, %08x, ...) failed %d (%s)",
-            w, h, format, usage, err, strerror(-err));
-    
+            width, height, format, usage, err, strerror(-err));
+
     if (err == NO_ERROR) {
         Mutex::Autolock _l(sLock);
         KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
-        int bpp = bytesPerPixel(format);
-        if (bpp < 0) {
-            // probably a HAL custom format. in any case, we don't know
-            // what its pixel size is.
-            bpp = 0;
-        }
+        uint32_t bpp = bytesPerPixel(format);
         alloc_rec_t rec;
-        rec.w = w;
-        rec.h = h;
-        rec.s = *stride;
+        rec.width = width;
+        rec.height = height;
+        rec.stride = *stride;
         rec.format = format;
         rec.usage = usage;
-        rec.size = h * stride[0] * bpp;
+        rec.size = static_cast<size_t>(height * (*stride) * bpp);
         list.add(*handle, rec);
     }
 
diff --git a/libs/ui/GraphicBufferMapper.cpp b/libs/ui/GraphicBufferMapper.cpp
index 320b6c0..01acdc8 100644
--- a/libs/ui/GraphicBufferMapper.cpp
+++ b/libs/ui/GraphicBufferMapper.cpp
@@ -20,7 +20,12 @@
 #include <stdint.h>
 #include <errno.h>
 
+// We would eliminate the non-conforming zero-length array, but we can't since
+// this is effectively included from the Linux kernel
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wzero-length-array"
 #include <sync/sync.h>
+#pragma clang diagnostic pop
 
 #include <utils/Errors.h>
 #include <utils/Log.h>
@@ -44,7 +49,7 @@
     int err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module);
     ALOGE_IF(err, "FATAL: can't find the %s module", GRALLOC_HARDWARE_MODULE_ID);
     if (err == 0) {
-        mAllocMod = (gralloc_module_t const *)module;
+        mAllocMod = reinterpret_cast<gralloc_module_t const *>(module);
     }
 }
 
@@ -72,13 +77,13 @@
     return err;
 }
 
-status_t GraphicBufferMapper::lock(buffer_handle_t handle, 
-        int usage, const Rect& bounds, void** vaddr)
+status_t GraphicBufferMapper::lock(buffer_handle_t handle,
+        uint32_t usage, const Rect& bounds, void** vaddr)
 {
     ATRACE_CALL();
     status_t err;
 
-    err = mAllocMod->lock(mAllocMod, handle, usage,
+    err = mAllocMod->lock(mAllocMod, handle, static_cast<int>(usage),
             bounds.left, bounds.top, bounds.width(), bounds.height(),
             vaddr);
 
@@ -87,12 +92,12 @@
 }
 
 status_t GraphicBufferMapper::lockYCbCr(buffer_handle_t handle,
-        int usage, const Rect& bounds, android_ycbcr *ycbcr)
+        uint32_t usage, const Rect& bounds, android_ycbcr *ycbcr)
 {
     ATRACE_CALL();
     status_t err;
 
-    err = mAllocMod->lock_ycbcr(mAllocMod, handle, usage,
+    err = mAllocMod->lock_ycbcr(mAllocMod, handle, static_cast<int>(usage),
             bounds.left, bounds.top, bounds.width(), bounds.height(),
             ycbcr);
 
@@ -112,19 +117,19 @@
 }
 
 status_t GraphicBufferMapper::lockAsync(buffer_handle_t handle,
-        int usage, const Rect& bounds, void** vaddr, int fenceFd)
+        uint32_t usage, const Rect& bounds, void** vaddr, int fenceFd)
 {
     ATRACE_CALL();
     status_t err;
 
     if (mAllocMod->common.module_api_version >= GRALLOC_MODULE_API_VERSION_0_3) {
-        err = mAllocMod->lockAsync(mAllocMod, handle, usage,
+        err = mAllocMod->lockAsync(mAllocMod, handle, static_cast<int>(usage),
                 bounds.left, bounds.top, bounds.width(), bounds.height(),
                 vaddr, fenceFd);
     } else {
         sync_wait(fenceFd, -1);
         close(fenceFd);
-        err = mAllocMod->lock(mAllocMod, handle, usage,
+        err = mAllocMod->lock(mAllocMod, handle, static_cast<int>(usage),
                 bounds.left, bounds.top, bounds.width(), bounds.height(),
                 vaddr);
     }
@@ -134,19 +139,19 @@
 }
 
 status_t GraphicBufferMapper::lockAsyncYCbCr(buffer_handle_t handle,
-        int usage, const Rect& bounds, android_ycbcr *ycbcr, int fenceFd)
+        uint32_t usage, const Rect& bounds, android_ycbcr *ycbcr, int fenceFd)
 {
     ATRACE_CALL();
     status_t err;
 
     if (mAllocMod->common.module_api_version >= GRALLOC_MODULE_API_VERSION_0_3) {
-        err = mAllocMod->lockAsync_ycbcr(mAllocMod, handle, usage,
-                bounds.left, bounds.top, bounds.width(), bounds.height(),
-                ycbcr, fenceFd);
+        err = mAllocMod->lockAsync_ycbcr(mAllocMod, handle,
+                static_cast<int>(usage), bounds.left, bounds.top,
+                bounds.width(), bounds.height(), ycbcr, fenceFd);
     } else {
         sync_wait(fenceFd, -1);
         close(fenceFd);
-        err = mAllocMod->lock_ycbcr(mAllocMod, handle, usage,
+        err = mAllocMod->lock_ycbcr(mAllocMod, handle, static_cast<int>(usage),
                 bounds.left, bounds.top, bounds.width(), bounds.height(),
                 ycbcr);
     }
diff --git a/libs/ui/PixelFormat.cpp b/libs/ui/PixelFormat.cpp
index 5ce7fba..99ed6f7 100644
--- a/libs/ui/PixelFormat.cpp
+++ b/libs/ui/PixelFormat.cpp
@@ -15,13 +15,12 @@
  */
 
 #include <ui/PixelFormat.h>
-#include <hardware/hardware.h>
 
 // ----------------------------------------------------------------------------
 namespace android {
 // ----------------------------------------------------------------------------
 
-ssize_t bytesPerPixel(PixelFormat format) {
+uint32_t bytesPerPixel(PixelFormat format) {
     switch (format) {
         case PIXEL_FORMAT_RGBA_8888:
         case PIXEL_FORMAT_RGBX_8888:
@@ -36,10 +35,10 @@
         case PIXEL_FORMAT_RGBA_4444:
             return 2;
     }
-    return BAD_VALUE;
+    return 0;
 }
 
-ssize_t bitsPerPixel(PixelFormat format) {
+uint32_t bitsPerPixel(PixelFormat format) {
     switch (format) {
         case PIXEL_FORMAT_RGBA_8888:
         case PIXEL_FORMAT_RGBX_8888:
@@ -52,7 +51,7 @@
         case PIXEL_FORMAT_RGBA_4444:
             return 16;
     }
-    return BAD_VALUE;
+    return 0;
 }
 
 // ----------------------------------------------------------------------------
diff --git a/libs/ui/Region.cpp b/libs/ui/Region.cpp
index fa812f4..06ab3d0 100644
--- a/libs/ui/Region.cpp
+++ b/libs/ui/Region.cpp
@@ -102,8 +102,8 @@
         current--;
     } while (current->top == lastTop && current >= begin);
 
-    unsigned int beginLastSpan = -1;
-    unsigned int endLastSpan = -1;
+    int beginLastSpan = -1;
+    int endLastSpan = -1;
     int top = -1;
     int bottom = -1;
 
@@ -118,7 +118,7 @@
             } else {
                 beginLastSpan = endLastSpan + 1;
             }
-            endLastSpan = dst.size() - 1;
+            endLastSpan = static_cast<int>(dst.size()) - 1;
 
             top = current->top;
             bottom = current->bottom;
@@ -126,8 +126,12 @@
         int left = current->left;
         int right = current->right;
 
-        for (unsigned int prevIndex = beginLastSpan; prevIndex <= endLastSpan; prevIndex++) {
-            const Rect* prev = &dst[prevIndex];
+        for (int prevIndex = beginLastSpan; prevIndex <= endLastSpan; prevIndex++) {
+            // prevIndex can't be -1 here because if endLastSpan is set to a
+            // value greater than -1 (allowing the loop to execute),
+            // beginLastSpan (and therefore prevIndex) will also be increased
+            const Rect* prev = &dst[static_cast<size_t>(prevIndex)];
+
             if (spanDirection == direction_RTL) {
                 // iterating over previous span RTL, quit if it's too far left
                 if (prev->right <= left) break;
@@ -250,10 +254,10 @@
     mStorage.add(r);
 }
 
-void Region::set(uint32_t w, uint32_t h)
+void Region::set(int32_t w, int32_t h)
 {
     mStorage.clear();
-    mStorage.add(Rect(w,h));
+    mStorage.add(Rect(w, h));
 }
 
 bool Region::isTriviallyEqual(const Region& region) const {
@@ -404,7 +408,7 @@
 
 // This is our region rasterizer, which merges rects and spans together
 // to obtain an optimal region.
-class Region::rasterizer : public region_operator<Rect>::region_rasterizer 
+class Region::rasterizer : public region_operator<Rect>::region_rasterizer
 {
     Rect bounds;
     Vector<Rect>& storage;
@@ -413,81 +417,92 @@
     Vector<Rect> span;
     Rect* cur;
 public:
-    rasterizer(Region& reg) 
+    rasterizer(Region& reg)
         : bounds(INT_MAX, 0, INT_MIN, 0), storage(reg.mStorage), head(), tail(), cur() {
         storage.clear();
     }
 
-    ~rasterizer() {
-        if (span.size()) {
-            flushSpan();
-        }
-        if (storage.size()) {
-            bounds.top = storage.itemAt(0).top;
-            bounds.bottom = storage.top().bottom;
-            if (storage.size() == 1) {
-                storage.clear();
-            }
-        } else {
-            bounds.left  = 0;
-            bounds.right = 0;
-        }
-        storage.add(bounds);
-    }
-    
-    virtual void operator()(const Rect& rect) {
-        //ALOGD(">>> %3d, %3d, %3d, %3d",
-        //        rect.left, rect.top, rect.right, rect.bottom);
-        if (span.size()) {
-            if (cur->top != rect.top) {
-                flushSpan();
-            } else if (cur->right == rect.left) {
-                cur->right = rect.right;
-                return;
-            }
-        }
-        span.add(rect);
-        cur = span.editArray() + (span.size() - 1);
-    }
+    virtual ~rasterizer();
+
+    virtual void operator()(const Rect& rect);
+
 private:
-    template<typename T> 
+    template<typename T>
     static inline T min(T rhs, T lhs) { return rhs < lhs ? rhs : lhs; }
-    template<typename T> 
+    template<typename T>
     static inline T max(T rhs, T lhs) { return rhs > lhs ? rhs : lhs; }
-    void flushSpan() {
-        bool merge = false;
-        if (tail-head == ssize_t(span.size())) {
-            Rect const* p = span.editArray();
-            Rect const* q = head;
-            if (p->top == q->bottom) {
-                merge = true;
-                while (q != tail) {
-                    if ((p->left != q->left) || (p->right != q->right)) {
-                        merge = false;
-                        break;
-                    }
-                    p++, q++;
-                }
-            }
-        }
-        if (merge) {
-            const int bottom = span[0].bottom;
-            Rect* r = head;
-            while (r != tail) {
-                r->bottom = bottom;
-                r++;
-            }
-        } else {
-            bounds.left = min(span.itemAt(0).left, bounds.left);
-            bounds.right = max(span.top().right, bounds.right);
-            storage.appendVector(span);
-            tail = storage.editArray() + storage.size();
-            head = tail - span.size();
-        }
-        span.clear();
-    }
+
+    void flushSpan();
 };
 
+Region::rasterizer::~rasterizer()
+{
+    if (span.size()) {
+        flushSpan();
+    }
+    if (storage.size()) {
+        bounds.top = storage.itemAt(0).top;
+        bounds.bottom = storage.top().bottom;
+        if (storage.size() == 1) {
+            storage.clear();
+        }
+    } else {
+        bounds.left  = 0;
+        bounds.right = 0;
+    }
+    storage.add(bounds);
+}
+
+void Region::rasterizer::operator()(const Rect& rect)
+{
+    //ALOGD(">>> %3d, %3d, %3d, %3d",
+    //        rect.left, rect.top, rect.right, rect.bottom);
+    if (span.size()) {
+        if (cur->top != rect.top) {
+            flushSpan();
+        } else if (cur->right == rect.left) {
+            cur->right = rect.right;
+            return;
+        }
+    }
+    span.add(rect);
+    cur = span.editArray() + (span.size() - 1);
+}
+
+void Region::rasterizer::flushSpan()
+{
+    bool merge = false;
+    if (tail-head == ssize_t(span.size())) {
+        Rect const* p = span.editArray();
+        Rect const* q = head;
+        if (p->top == q->bottom) {
+            merge = true;
+            while (q != tail) {
+                if ((p->left != q->left) || (p->right != q->right)) {
+                    merge = false;
+                    break;
+                }
+                p++, q++;
+            }
+        }
+    }
+    if (merge) {
+        const int bottom = span[0].bottom;
+        Rect* r = head;
+        while (r != tail) {
+            r->bottom = bottom;
+            r++;
+        }
+    } else {
+        bounds.left = min(span.itemAt(0).left, bounds.left);
+        bounds.right = max(span.top().right, bounds.right);
+        storage.appendVector(span);
+        tail = storage.editArray() + storage.size();
+        head = tail - span.size();
+    }
+    span.clear();
+}
+
 bool Region::validate(const Region& reg, const char* name, bool silent)
 {
     bool result = true;
@@ -786,10 +801,8 @@
 }
 
 Rect const* Region::getArray(size_t* count) const {
-    const_iterator const b(begin());
-    const_iterator const e(end());
-    if (count) *count = e-b;
-    return b;
+    if (count) *count = static_cast<size_t>(end() - begin());
+    return begin();
 }
 
 SharedBuffer const* Region::getSharedBuffer(size_t* count) const {
@@ -806,29 +819,22 @@
 
 // ----------------------------------------------------------------------------
 
-void Region::dump(String8& out, const char* what, uint32_t flags) const
+void Region::dump(String8& out, const char* what, uint32_t /* flags */) const
 {
-    (void)flags;
     const_iterator head = begin();
     const_iterator const tail = end();
 
-    size_t SIZE = 256;
-    char buffer[SIZE];
-
-    snprintf(buffer, SIZE, "  Region %s (this=%p, count=%" PRIdPTR ")\n",
-            what, this, tail-head);
-    out.append(buffer);
+    out.appendFormat("  Region %s (this=%p, count=%" PRIdPTR ")\n",
+            what, this, tail - head);
     while (head != tail) {
-        snprintf(buffer, SIZE, "    [%3d, %3d, %3d, %3d]\n",
-                head->left, head->top, head->right, head->bottom);
-        out.append(buffer);
-        head++;
+        out.appendFormat("    [%3d, %3d, %3d, %3d]\n", head->left, head->top,
+                head->right, head->bottom);
+        ++head;
     }
 }
 
-void Region::dump(const char* what, uint32_t flags) const
+void Region::dump(const char* what, uint32_t /* flags */) const
 {
-    (void)flags;
     const_iterator head = begin();
     const_iterator const tail = end();
     ALOGD("  Region %s (this=%p, count=%" PRIdPTR ")\n", what, this, tail-head);
diff --git a/libs/ui/UiConfig.cpp b/libs/ui/UiConfig.cpp
index 8b2130e..9e7ba8e 100644
--- a/libs/ui/UiConfig.cpp
+++ b/libs/ui/UiConfig.cpp
@@ -18,8 +18,11 @@
 
 namespace android {
 
+#ifdef FRAMEBUFFER_FORCE_FORMAT
+// We need the two-level macro to stringify the contents of a macro argument
 #define STRINGIFY(x) #x
 #define TOSTRING(x) STRINGIFY(x)
+#endif
 
 void appendUiConfigString(String8& configStr)
 {