Cleanup various clang warnings, use unique_ptrs in several places

Change-Id: I347904b25e51fcc7de14b1e72f1acd0f6ba26f3f
diff --git a/libs/hwui/Animator.cpp b/libs/hwui/Animator.cpp
index a1bbc06..805f844 100644
--- a/libs/hwui/Animator.cpp
+++ b/libs/hwui/Animator.cpp
@@ -36,7 +36,6 @@
         , mFinalValue(finalValue)
         , mDeltaValue(0)
         , mFromValue(0)
-        , mInterpolator(0)
         , mStagingPlayState(NOT_STARTED)
         , mPlayState(NOT_STARTED)
         , mHasStartValue(false)
@@ -47,7 +46,6 @@
 }
 
 BaseRenderNodeAnimator::~BaseRenderNodeAnimator() {
-    delete mInterpolator;
 }
 
 void BaseRenderNodeAnimator::checkMutable() {
@@ -58,8 +56,7 @@
 
 void BaseRenderNodeAnimator::setInterpolator(Interpolator* interpolator) {
     checkMutable();
-    delete mInterpolator;
-    mInterpolator = interpolator;
+    mInterpolator.reset(interpolator);
 }
 
 void BaseRenderNodeAnimator::setStartValue(float value) {
@@ -119,7 +116,7 @@
     }
     // No interpolator was set, use the default
     if (!mInterpolator) {
-        mInterpolator = Interpolator::createDefaultInterpolator();
+        mInterpolator.reset(Interpolator::createDefaultInterpolator());
     }
     if (mDuration < 0 || mDuration > 50000) {
         ALOGW("Your duration is strange and confusing: %" PRId64, mDuration);
diff --git a/libs/hwui/Animator.h b/libs/hwui/Animator.h
index 99f8956..aa3d301 100644
--- a/libs/hwui/Animator.h
+++ b/libs/hwui/Animator.h
@@ -16,6 +16,7 @@
 #ifndef ANIMATOR_H
 #define ANIMATOR_H
 
+#include <memory>
 #include <cutils/compiler.h>
 #include <utils/RefBase.h>
 #include <utils/StrongPointer.h>
@@ -99,7 +100,7 @@
     float mDeltaValue;
     float mFromValue;
 
-    Interpolator* mInterpolator;
+    std::unique_ptr<Interpolator> mInterpolator;
     PlayState mStagingPlayState;
     PlayState mPlayState;
     bool mHasStartValue;
diff --git a/libs/hwui/Caches.cpp b/libs/hwui/Caches.cpp
index bb4ac83..ebeb845 100644
--- a/libs/hwui/Caches.cpp
+++ b/libs/hwui/Caches.cpp
@@ -224,9 +224,8 @@
     mCurrentBuffer = 0;
 
     glDeleteBuffers(1, &mMeshIndices);
-    delete[] mRegionMesh;
     mMeshIndices = 0;
-    mRegionMesh = NULL;
+    mRegionMesh.release();
 
     glDeleteBuffers(1, &mShadowStripsIndices);
     mShadowStripsIndices = 0;
@@ -406,7 +405,7 @@
 
 bool Caches::bindQuadIndicesBuffer() {
     if (!mMeshIndices) {
-        uint16_t* regionIndices = new uint16_t[gMaxNumberOfQuads * 6];
+        std::unique_ptr<uint16_t[]> regionIndices(new uint16_t[gMaxNumberOfQuads * 6]);
         for (uint32_t i = 0; i < gMaxNumberOfQuads; i++) {
             uint16_t quad = i * 4;
             int index = i * 6;
@@ -421,9 +420,7 @@
         glGenBuffers(1, &mMeshIndices);
         bool force = bindIndicesBufferInternal(mMeshIndices);
         glBufferData(GL_ELEMENT_ARRAY_BUFFER, gMaxNumberOfQuads * 6 * sizeof(uint16_t),
-                regionIndices, GL_STATIC_DRAW);
-
-        delete[] regionIndices;
+                regionIndices.get(), GL_STATIC_DRAW);
         return force;
     }
 
@@ -432,14 +429,12 @@
 
 bool Caches::bindShadowIndicesBuffer() {
     if (!mShadowStripsIndices) {
-        uint16_t* shadowIndices = new uint16_t[MAX_SHADOW_INDEX_COUNT];
-        ShadowTessellator::generateShadowIndices(shadowIndices);
+        std::unique_ptr<uint16_t[]> shadowIndices(new uint16_t[MAX_SHADOW_INDEX_COUNT]);
+        ShadowTessellator::generateShadowIndices(shadowIndices.get());
         glGenBuffers(1, &mShadowStripsIndices);
         bool force = bindIndicesBufferInternal(mShadowStripsIndices);
         glBufferData(GL_ELEMENT_ARRAY_BUFFER, MAX_SHADOW_INDEX_COUNT * sizeof(uint16_t),
-            shadowIndices, GL_STATIC_DRAW);
-
-        delete[] shadowIndices;
+            shadowIndices.get(), GL_STATIC_DRAW);
         return force;
     }
 
@@ -687,10 +682,10 @@
 TextureVertex* Caches::getRegionMesh() {
     // Create the mesh, 2 triangles and 4 vertices per rectangle in the region
     if (!mRegionMesh) {
-        mRegionMesh = new TextureVertex[gMaxNumberOfQuads * 4];
+        mRegionMesh.reset(new TextureVertex[gMaxNumberOfQuads * 4]);
     }
 
-    return mRegionMesh;
+    return mRegionMesh.get();
 }
 
 ///////////////////////////////////////////////////////////////////////////////
diff --git a/libs/hwui/Caches.h b/libs/hwui/Caches.h
index fef01fa..b0eebd7 100644
--- a/libs/hwui/Caches.h
+++ b/libs/hwui/Caches.h
@@ -416,7 +416,7 @@
     Extensions& mExtensions;
 
     // Used to render layers
-    TextureVertex* mRegionMesh;
+    std::unique_ptr<TextureVertex[]> mRegionMesh;
 
     // Global index buffer
     GLuint mMeshIndices;
diff --git a/libs/hwui/DisplayList.cpp b/libs/hwui/DisplayList.cpp
index 8953166..249ada0 100644
--- a/libs/hwui/DisplayList.cpp
+++ b/libs/hwui/DisplayList.cpp
@@ -62,18 +62,6 @@
 
     resourceCache.unlock();
 
-    for (size_t i = 0; i < paints.size(); i++) {
-        delete paints.itemAt(i);
-    }
-
-    for (size_t i = 0; i < regions.size(); i++) {
-        delete regions.itemAt(i);
-    }
-
-    for (size_t i = 0; i < paths.size(); i++) {
-        delete paths.itemAt(i);
-    }
-
     bitmapResources.clear();
     ownedBitmapResources.clear();
     patchResources.clear();
diff --git a/libs/hwui/DisplayList.h b/libs/hwui/DisplayList.h
index 26637a7..cfd60ad 100644
--- a/libs/hwui/DisplayList.h
+++ b/libs/hwui/DisplayList.h
@@ -137,10 +137,10 @@
     Vector<const SkBitmap*> ownedBitmapResources;
     Vector<const Res_png_9patch*> patchResources;
 
-    Vector<const SkPaint*> paints;
-    Vector<const SkPath*> paths;
+    std::vector<std::unique_ptr<const SkPaint>> paints;
+    std::vector<std::unique_ptr<const SkRegion>> regions;
+    std::vector<std::unique_ptr<const SkPath>> paths;
     SortedVector<const SkPath*> sourcePaths;
-    Vector<const SkRegion*> regions;
     Vector<Functor*> functors;
 
     const Vector<Chunk>& getChunks() const {
diff --git a/libs/hwui/DisplayListOp.h b/libs/hwui/DisplayListOp.h
index e4e5dfa..e42a9e4 100644
--- a/libs/hwui/DisplayListOp.h
+++ b/libs/hwui/DisplayListOp.h
@@ -17,10 +17,6 @@
 #ifndef ANDROID_HWUI_DISPLAY_OPERATION_H
 #define ANDROID_HWUI_DISPLAY_OPERATION_H
 
-#ifndef LOG_TAG
-    #define LOG_TAG "OpenGLRenderer"
-#endif
-
 #include <SkColor.h>
 #include <SkPath.h>
 #include <SkPathOps.h>
@@ -39,11 +35,6 @@
 #include "UvMapper.h"
 #include "utils/LinearAllocator.h"
 
-#define CRASH() do { \
-    *(int *)(uintptr_t) 0xbbadbeef = 0; \
-    ((void(*)())0)(); /* More reliable, but doesn't say BBADBEEF */ \
-} while(false)
-
 // Use OP_LOG for logging with arglist, OP_LOGS if just printing char*
 #define OP_LOGS(s) OP_LOG("%s", (s))
 #define OP_LOG(s, ...) ALOGD( "%*s" s, level * 2, "", __VA_ARGS__ )
@@ -66,9 +57,9 @@
 public:
     // These objects should always be allocated with a LinearAllocator, and never destroyed/deleted.
     // standard new() intentionally not implemented, and delete/deconstructor should never be used.
-    virtual ~DisplayListOp() { CRASH(); }
-    static void operator delete(void* ptr) { CRASH(); }
-    /** static void* operator new(size_t size); PURPOSELY OMITTED **/
+    virtual ~DisplayListOp() { LOG_ALWAYS_FATAL("Destructor not supported"); }
+    static void operator delete(void* ptr) { LOG_ALWAYS_FATAL("delete not supported"); }
+    static void* operator new(size_t size) = delete; /** PURPOSELY OMITTED **/
     static void* operator new(size_t size, LinearAllocator& allocator) {
         return allocator.alloc(size);
     }
@@ -93,10 +84,6 @@
 
 class StateOp : public DisplayListOp {
 public:
-    StateOp() {};
-
-    virtual ~StateOp() {}
-
     virtual void defer(DeferStateStruct& deferStruct, int saveCount, int level,
             bool useQuickReject) {
         // default behavior only affects immediate, deferrable state, issue directly to renderer
@@ -869,7 +856,7 @@
                     patchOp->mLocalBounds.top + 0.5f);
 
             // Copy & transform all the vertices for the current operation
-            TextureVertex* opVertices = opMesh->vertices;
+            TextureVertex* opVertices = opMesh->vertices.get();
             for (uint32_t j = 0; j < vertexCount; j++, opVertices++) {
                 TextureVertex::set(vertex++,
                         opVertices->x + tx, opVertices->y + ty,
diff --git a/libs/hwui/DisplayListRenderer.h b/libs/hwui/DisplayListRenderer.h
index 0bfcb16b..34f9c38 100644
--- a/libs/hwui/DisplayListRenderer.h
+++ b/libs/hwui/DisplayListRenderer.h
@@ -234,21 +234,22 @@
     inline const SkPath* refPath(const SkPath* path) {
         if (!path) return nullptr;
 
-        const SkPath* pathCopy = mPathMap.valueFor(path);
-        if (pathCopy == nullptr || pathCopy->getGenerationID() != path->getGenerationID()) {
+        const SkPath* cachedPath = mPathMap.valueFor(path);
+        if (cachedPath == nullptr || cachedPath->getGenerationID() != path->getGenerationID()) {
             SkPath* newPathCopy = new SkPath(*path);
             newPathCopy->setSourcePath(path);
+            cachedPath = newPathCopy;
+            std::unique_ptr<const SkPath> copy(newPathCopy);
+            mDisplayListData->paths.push_back(std::move(copy));
 
-            pathCopy = newPathCopy;
             // replaceValueFor() performs an add if the entry doesn't exist
-            mPathMap.replaceValueFor(path, pathCopy);
-            mDisplayListData->paths.add(pathCopy);
+            mPathMap.replaceValueFor(path, cachedPath);
         }
         if (mDisplayListData->sourcePaths.indexOf(path) < 0) {
             mResourceCache.incrementRefcount(path);
             mDisplayListData->sourcePaths.add(path);
         }
-        return pathCopy;
+        return cachedPath;
     }
 
     inline const SkPaint* refPaint(const SkPaint* paint) {
@@ -258,8 +259,8 @@
         // so that we don't need to modify the paint every time we access it.
         SkTLazy<SkPaint> filteredPaint;
         if (mDrawFilter.get()) {
-           paint = filteredPaint.init();
-           mDrawFilter->filter(filteredPaint.get(), SkDrawFilter::kPaint_Type);
+            paint = filteredPaint.init();
+            mDrawFilter->filter(filteredPaint.get(), SkDrawFilter::kPaint_Type);
         }
 
         // compute the hash key for the paint and check the cache.
@@ -268,10 +269,12 @@
         // In the unlikely event that 2 unique paints have the same hash we do a
         // object equality check to ensure we don't erroneously dedup them.
         if (cachedPaint == nullptr || *cachedPaint != *paint) {
-            cachedPaint =  new SkPaint(*paint);
+            cachedPaint = new SkPaint(*paint);
+            std::unique_ptr<const SkPaint> copy(cachedPaint);
+            mDisplayListData->paints.push_back(std::move(copy));
+
             // replaceValueFor() performs an add if the entry doesn't exist
             mPaintMap.replaceValueFor(key, cachedPaint);
-            mDisplayListData->paints.add(cachedPaint);
         }
 
         return cachedPaint;
@@ -279,10 +282,12 @@
 
     inline SkPaint* copyPaint(const SkPaint* paint) {
         if (!paint) return nullptr;
-        SkPaint* paintCopy = new SkPaint(*paint);
-        mDisplayListData->paints.add(paintCopy);
 
-        return paintCopy;
+        SkPaint* returnPaint = new SkPaint(*paint);
+        std::unique_ptr<const SkPaint> copy(returnPaint);
+        mDisplayListData->paints.push_back(std::move(copy));
+
+        return returnPaint;
     }
 
     inline const SkRegion* refRegion(const SkRegion* region) {
@@ -290,16 +295,18 @@
             return region;
         }
 
-        const SkRegion* regionCopy = mRegionMap.valueFor(region);
+        const SkRegion* cachedRegion = mRegionMap.valueFor(region);
         // TODO: Add generation ID to SkRegion
-        if (regionCopy == nullptr) {
-            regionCopy = new SkRegion(*region);
+        if (cachedRegion == nullptr) {
+            std::unique_ptr<const SkRegion> copy(new SkRegion(*region));
+            cachedRegion = copy.get();
+            mDisplayListData->regions.push_back(std::move(copy));
+
             // replaceValueFor() performs an add if the entry doesn't exist
-            mRegionMap.replaceValueFor(region, regionCopy);
-            mDisplayListData->regions.add(regionCopy);
+            mRegionMap.replaceValueFor(region, cachedRegion);
         }
 
-        return regionCopy;
+        return cachedRegion;
     }
 
     inline const SkBitmap* refBitmap(const SkBitmap* bitmap) {
diff --git a/libs/hwui/FontRenderer.cpp b/libs/hwui/FontRenderer.cpp
index 5b5b098..693afcd 100644
--- a/libs/hwui/FontRenderer.cpp
+++ b/libs/hwui/FontRenderer.cpp
@@ -770,15 +770,12 @@
     }
 #endif
 
-    float *gaussian = new float[2 * intRadius + 1];
-    Blur::generateGaussianWeights(gaussian, intRadius);
+    std::unique_ptr<float[]> gaussian(new float[2 * intRadius + 1]);
+    Blur::generateGaussianWeights(gaussian.get(), intRadius);
 
-    uint8_t* scratch = new uint8_t[width * height];
-    Blur::horizontal(gaussian, intRadius, *image, scratch, width, height);
-    Blur::vertical(gaussian, intRadius, scratch, *image, width, height);
-
-    delete[] gaussian;
-    delete[] scratch;
+    std::unique_ptr<uint8_t[]> scratch(new uint8_t[width * height]);
+    Blur::horizontal(gaussian.get(), intRadius, *image, scratch.get(), width, height);
+    Blur::vertical(gaussian.get(), intRadius, scratch.get(), *image, width, height);
 }
 
 static uint32_t calculateCacheSize(const Vector<CacheTexture*>& cacheTextures) {
diff --git a/libs/hwui/GammaFontRenderer.cpp b/libs/hwui/GammaFontRenderer.cpp
index 06d2aad..0a98c29 100644
--- a/libs/hwui/GammaFontRenderer.cpp
+++ b/libs/hwui/GammaFontRenderer.cpp
@@ -183,12 +183,6 @@
     memset(mRenderersUsageCount, 0, sizeof(uint32_t) * kGammaCount);
 }
 
-Lookup3GammaFontRenderer::~Lookup3GammaFontRenderer() {
-    for (int i = 0; i < kGammaCount; i++) {
-        delete mRenderers[i];
-    }
-}
-
 void Lookup3GammaFontRenderer::endPrecaching() {
     for (int i = 0; i < kGammaCount; i++) {
         if (mRenderers[i]) {
@@ -199,8 +193,7 @@
 
 void Lookup3GammaFontRenderer::clear() {
     for (int i = 0; i < kGammaCount; i++) {
-        delete mRenderers[i];
-        mRenderers[i] = NULL;
+        mRenderers[i].release();
     }
 }
 
@@ -221,8 +214,7 @@
 
     if (count <= 1 || min < 0) return;
 
-    delete mRenderers[min];
-    mRenderers[min] = NULL;
+    mRenderers[min].release();
 
     // Also eliminate the caches for large glyphs, as they consume significant memory
     for (int i = 0; i < kGammaCount; ++i) {
@@ -233,14 +225,12 @@
 }
 
 FontRenderer* Lookup3GammaFontRenderer::getRenderer(Gamma gamma) {
-    FontRenderer* renderer = mRenderers[gamma];
-    if (!renderer) {
-        renderer = new FontRenderer();
-        mRenderers[gamma] = renderer;
-        renderer->setGammaTable(&mGammaTable[gamma * 256]);
+    if (!mRenderers[gamma]) {
+        mRenderers[gamma].reset(new FontRenderer());
+        mRenderers[gamma]->setGammaTable(&mGammaTable[gamma * 256]);
     }
     mRenderersUsageCount[gamma]++;
-    return renderer;
+    return mRenderers[gamma].get();
 }
 
 FontRenderer& Lookup3GammaFontRenderer::getFontRenderer(const SkPaint* paint) {
diff --git a/libs/hwui/GammaFontRenderer.h b/libs/hwui/GammaFontRenderer.h
index 623df1f..19352d7 100644
--- a/libs/hwui/GammaFontRenderer.h
+++ b/libs/hwui/GammaFontRenderer.h
@@ -151,8 +151,6 @@
 
 class Lookup3GammaFontRenderer: public GammaFontRenderer {
 public:
-    ~Lookup3GammaFontRenderer();
-
     void clear() override;
     void flush() override;
 
@@ -165,10 +163,9 @@
     uint32_t getFontRendererSize(uint32_t fontRenderer, GLenum format) const override {
         if (fontRenderer >= kGammaCount) return 0;
 
-        FontRenderer* renderer = mRenderers[fontRenderer];
-        if (!renderer) return 0;
+        if (!mRenderers[fontRenderer]) return 0;
 
-        return renderer->getCacheSize(format);
+        return mRenderers[fontRenderer]->getCacheSize(format);
     }
 
     void describe(ProgramDescription& description, const SkPaint* paint) const override {
@@ -192,7 +189,7 @@
     FontRenderer* getRenderer(Gamma gamma);
 
     uint32_t mRenderersUsageCount[kGammaCount];
-    FontRenderer* mRenderers[kGammaCount];
+    std::unique_ptr<FontRenderer> mRenderers[kGammaCount];
 
     uint8_t mGammaTable[256 * kGammaCount];
 
diff --git a/libs/hwui/GradientCache.cpp b/libs/hwui/GradientCache.cpp
index ffd1e8c..06d234c 100644
--- a/libs/hwui/GradientCache.cpp
+++ b/libs/hwui/GradientCache.cpp
@@ -52,10 +52,10 @@
     int deltaInt = int(lhs.count) - int(rhs.count);
     if (deltaInt != 0) return deltaInt;
 
-    deltaInt = memcmp(lhs.colors, rhs.colors, lhs.count * sizeof(uint32_t));
+    deltaInt = memcmp(lhs.colors.get(), rhs.colors.get(), lhs.count * sizeof(uint32_t));
     if (deltaInt != 0) return deltaInt;
 
-    return memcmp(lhs.positions, rhs.positions, lhs.count * sizeof(float));
+    return memcmp(lhs.positions.get(), rhs.positions.get(), lhs.count * sizeof(float));
 }
 
 ///////////////////////////////////////////////////////////////////////////////
diff --git a/libs/hwui/GradientCache.h b/libs/hwui/GradientCache.h
index ef4e0cd..9176c76 100644
--- a/libs/hwui/GradientCache.h
+++ b/libs/hwui/GradientCache.h
@@ -42,20 +42,12 @@
     }
 
     GradientCacheEntry(const GradientCacheEntry& entry) {
-        copy(entry.colors, entry.positions, entry.count);
-    }
-
-    ~GradientCacheEntry() {
-        delete[] colors;
-        delete[] positions;
+        copy(entry.colors.get(), entry.positions.get(), entry.count);
     }
 
     GradientCacheEntry& operator=(const GradientCacheEntry& entry) {
         if (this != &entry) {
-            delete[] colors;
-            delete[] positions;
-
-            copy(entry.colors, entry.positions, entry.count);
+            copy(entry.colors.get(), entry.positions.get(), entry.count);
         }
 
         return *this;
@@ -73,18 +65,18 @@
         return compare(*this, other) != 0;
     }
 
-    uint32_t* colors;
-    float* positions;
+    std::unique_ptr<uint32_t[]> colors;
+    std::unique_ptr<float[]> positions;
     uint32_t count;
 
 private:
     void copy(uint32_t* colors, float* positions, uint32_t count) {
         this->count = count;
-        this->colors = new uint32_t[count];
-        this->positions = new float[count];
+        this->colors.reset(new uint32_t[count]);
+        this->positions.reset(new float[count]);
 
-        memcpy(this->colors, colors, count * sizeof(uint32_t));
-        memcpy(this->positions, positions, count * sizeof(float));
+        memcpy(this->colors.get(), colors, count * sizeof(uint32_t));
+        memcpy(this->positions.get(), positions, count * sizeof(float));
     }
 
 }; // GradientCacheEntry
diff --git a/libs/hwui/Interpolator.cpp b/libs/hwui/Interpolator.cpp
index 2623f53..e1b0fc3 100644
--- a/libs/hwui/Interpolator.cpp
+++ b/libs/hwui/Interpolator.cpp
@@ -88,14 +88,12 @@
     return t * t * ((mTension + 1) * t + mTension) + 1.0f;
 }
 
-LUTInterpolator::LUTInterpolator(float* values, size_t size) {
-    mValues = values;
-    mSize = size;
+LUTInterpolator::LUTInterpolator(float* values, size_t size)
+    : mValues(values)
+    , mSize(size) {
 }
 
 LUTInterpolator::~LUTInterpolator() {
-    delete mValues;
-    mValues = 0;
 }
 
 float LUTInterpolator::interpolate(float input) {
@@ -112,7 +110,7 @@
 
     LOG_ALWAYS_FATAL_IF(i1 < 0 || i2 < 0, "negatives in interpolation!"
             " i1=%d, i2=%d, input=%f, lutpos=%f, size=%zu, values=%p, ipart=%f, weight=%f",
-            i1, i2, input, lutpos, mSize, mValues, ipart, weight);
+            i1, i2, input, lutpos, mSize, mValues.get(), ipart, weight);
 
     float v1 = mValues[i1];
     float v2 = mValues[i2];
diff --git a/libs/hwui/Interpolator.h b/libs/hwui/Interpolator.h
index dfa0a85..e636e11 100644
--- a/libs/hwui/Interpolator.h
+++ b/libs/hwui/Interpolator.h
@@ -17,6 +17,7 @@
 #define INTERPOLATOR_H
 
 #include <stddef.h>
+#include <memory>
 
 #include <cutils/compiler.h>
 
@@ -107,7 +108,7 @@
     virtual float interpolate(float input);
 
 private:
-    float* mValues;
+    std::unique_ptr<float[]> mValues;
     size_t mSize;
 };
 
diff --git a/libs/hwui/Layer.cpp b/libs/hwui/Layer.cpp
index 9aa29ca..8e77c5c 100644
--- a/libs/hwui/Layer.cpp
+++ b/libs/hwui/Layer.cpp
@@ -54,14 +54,12 @@
     texture.height = layerHeight;
     colorFilter = NULL;
     deferredUpdateScheduled = false;
-    renderer = NULL;
     renderNode = NULL;
     fbo = 0;
     stencil = NULL;
     debugDrawUpdate = false;
     hasDrawnSinceUpdate = false;
     forceFilter = false;
-    deferredList = NULL;
     convexMask = NULL;
     rendererLightPosDirty = true;
     wasBuildLayered = false;
@@ -76,8 +74,6 @@
     deleteTexture();
 
     delete[] mesh;
-    delete deferredList;
-    delete renderer;
 }
 
 uint32_t Layer::computeIdealWidth(uint32_t layerWidth) {
@@ -90,7 +86,7 @@
 
 void Layer::requireRenderer() {
     if (!renderer) {
-        renderer = new LayerRenderer(renderState, this);
+        renderer.reset(new LayerRenderer(renderState, this));
         renderer->initProperties();
     }
 }
@@ -241,8 +237,7 @@
         dirtyRect.set(0, 0, width, height);
     }
 
-    delete deferredList;
-    deferredList = new DeferredDisplayList(dirtyRect);
+    deferredList.reset(new DeferredDisplayList(dirtyRect));
 
     DeferStateStruct deferredState(*deferredList, *renderer,
             RenderNode::kReplayFlag_ClipChildren);
@@ -260,10 +255,7 @@
 void Layer::cancelDefer() {
     renderNode = NULL;
     deferredUpdateScheduled = false;
-    if (deferredList) {
-        delete deferredList;
-        deferredList = NULL;
-    }
+    deferredList.release();
 }
 
 void Layer::flush() {
diff --git a/libs/hwui/Layer.h b/libs/hwui/Layer.h
index 3b909d5..3b4f293 100644
--- a/libs/hwui/Layer.h
+++ b/libs/hwui/Layer.h
@@ -21,6 +21,7 @@
 #include <sys/types.h>
 #include <utils/StrongPointer.h>
 #include <utils/RefBase.h>
+#include <memory>
 
 #include <GLES2/gl2.h>
 
@@ -44,9 +45,9 @@
 
 // Forward declarations
 class Caches;
+class RenderNode;
 class RenderState;
 class OpenGLRenderer;
-class RenderNode;
 class DeferredDisplayList;
 struct DeferStateStruct;
 
@@ -320,7 +321,7 @@
      * Used for deferred updates.
      */
     bool deferredUpdateScheduled;
-    OpenGLRenderer* renderer;
+    std::unique_ptr<OpenGLRenderer> renderer;
     sp<RenderNode> renderNode;
     Rect dirtyRect;
     bool debugDrawUpdate;
@@ -417,7 +418,7 @@
      * Used to defer display lists when the layer is updated with a
      * display list.
      */
-    DeferredDisplayList* deferredList;
+    std::unique_ptr<DeferredDisplayList> deferredList;
 
     /**
      * This convex path should be used to mask the layer's draw to the screen.
diff --git a/libs/hwui/OpenGLRenderer.cpp b/libs/hwui/OpenGLRenderer.cpp
index 1c4c327..fb3d462 100755
--- a/libs/hwui/OpenGLRenderer.cpp
+++ b/libs/hwui/OpenGLRenderer.cpp
@@ -313,10 +313,6 @@
 bool OpenGLRenderer::finish() {
     renderOverdraw();
     endTiling();
-
-    for (size_t i = 0; i < mTempPaths.size(); i++) {
-        delete mTempPaths[i];
-    }
     mTempPaths.clear();
 
     // When finish() is invoked on FBO 0 we've reached the end
@@ -800,7 +796,7 @@
                     bounds.getWidth(), bounds.getHeight());
 
             // Enqueue the buffer coordinates to clear the corresponding region later
-            mLayers.push(new Rect(bounds));
+            mLayers.push_back(Rect(bounds));
         }
     }
 
@@ -1295,14 +1291,12 @@
         Vertex* vertex = mesh;
 
         for (uint32_t i = 0; i < count; i++) {
-            Rect* bounds = mLayers.itemAt(i);
+            const Rect& bounds = mLayers[i];
 
-            Vertex::set(vertex++, bounds->left, bounds->top);
-            Vertex::set(vertex++, bounds->right, bounds->top);
-            Vertex::set(vertex++, bounds->left, bounds->bottom);
-            Vertex::set(vertex++, bounds->right, bounds->bottom);
-
-            delete bounds;
+            Vertex::set(vertex++, bounds.left, bounds.top);
+            Vertex::set(vertex++, bounds.right, bounds.top);
+            Vertex::set(vertex++, bounds.left, bounds.bottom);
+            Vertex::set(vertex++, bounds.right, bounds.bottom);
         }
         // We must clear the list of dirty rects before we
         // call setupDraw() to prevent stencil setup to do
@@ -1324,9 +1318,6 @@
 
         if (scissorChanged) mCaches.enableScissor();
     } else {
-        for (uint32_t i = 0; i < count; i++) {
-            delete mLayers.itemAt(i);
-        }
         mLayers.clear();
     }
 }
@@ -2046,17 +2037,15 @@
 
     const uint32_t count = meshWidth * meshHeight * 6;
 
-    Vector<ColorTextureVertex> mesh; // TODO: use C++11 unique_ptr
-    mesh.setCapacity(count);
-    ColorTextureVertex* vertex = mesh.editArray();
+    std::unique_ptr<ColorTextureVertex[]> mesh(new ColorTextureVertex[count]);
+    ColorTextureVertex* vertex = &mesh[0];
 
-    bool cleanupColors = false;
+    std::unique_ptr<int[]> tempColors;
     if (!colors) {
         uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
-        int* newColors = new int[colorsCount];
-        memset(newColors, 0xff, colorsCount * sizeof(int));
-        colors = newColors;
-        cleanupColors = true;
+        tempColors.reset(new int[colorsCount]);
+        memset(tempColors.get(), 0xff, colorsCount * sizeof(int));
+        colors = tempColors.get();
     }
 
     mCaches.activeTexture(0);
@@ -2099,14 +2088,12 @@
     }
 
     if (quickRejectSetupScissor(left, top, right, bottom)) {
-        if (cleanupColors) delete[] colors;
         return;
     }
 
     if (!texture) {
         texture = mCaches.textureCache.get(bitmap);
         if (!texture) {
-            if (cleanupColors) delete[] colors;
             return;
         }
     }
@@ -2145,8 +2132,6 @@
         glDisableVertexAttribArray(slot);
     }
 
-    if (cleanupColors) delete[] colors;
-
     mDirty = true;
 }
 
diff --git a/libs/hwui/OpenGLRenderer.h b/libs/hwui/OpenGLRenderer.h
index fbb90a7..9de4149 100755
--- a/libs/hwui/OpenGLRenderer.h
+++ b/libs/hwui/OpenGLRenderer.h
@@ -62,12 +62,11 @@
 class VertexBuffer;
 
 struct DrawModifiers {
-    DrawModifiers() {
-        reset();
-    }
+    DrawModifiers()
+        : mOverrideLayerAlpha(0.0f) {}
 
     void reset() {
-        memset(this, 0, sizeof(DrawModifiers));
+        mOverrideLayerAlpha = 0.0f;
     }
 
     float mOverrideLayerAlpha;
@@ -391,9 +390,10 @@
     virtual GLuint onGetTargetFbo() const override { return 0; }
 
     SkPath* allocPathForFrame() {
-        SkPath* path = new SkPath();
-        mTempPaths.push_back(path);
-        return path;
+        std::unique_ptr<SkPath> path(new SkPath());
+        SkPath* returnPath = path.get();
+        mTempPaths.push_back(std::move(path));
+        return returnPath;
     }
 
 protected:
@@ -1029,7 +1029,7 @@
     RenderState& mRenderState;
 
     // List of rectangles to clear after saveLayer() is invoked
-    Vector<Rect*> mLayers;
+    std::vector<Rect> mLayers;
     // List of layers to update at the beginning of a frame
     Vector< sp<Layer> > mLayerUpdates;
 
@@ -1069,7 +1069,7 @@
     uint8_t mSpotShadowAlpha;
 
     // Paths kept alive for the duration of the frame
-    std::vector<SkPath*> mTempPaths;
+    std::vector<std::unique_ptr<SkPath>> mTempPaths;
 
     friend class Layer;
     friend class TextSetupFunctor;
diff --git a/libs/hwui/Patch.cpp b/libs/hwui/Patch.cpp
index 442e9ba..442dc5b 100644
--- a/libs/hwui/Patch.cpp
+++ b/libs/hwui/Patch.cpp
@@ -32,11 +32,7 @@
 // Constructors/destructor
 ///////////////////////////////////////////////////////////////////////////////
 
-Patch::Patch(): vertices(NULL), verticesCount(0), indexCount(0), hasEmptyQuads(false) {
-}
-
-Patch::~Patch() {
-    delete[] vertices;
+Patch::Patch(): vertices(), verticesCount(0), indexCount(0), hasEmptyQuads(false) {
 }
 
 ///////////////////////////////////////////////////////////////////////////////
@@ -55,7 +51,7 @@
 
 TextureVertex* Patch::createMesh(const float bitmapWidth, const float bitmapHeight,
         float width, float height, const UvMapper& mapper, const Res_png_9patch* patch) {
-    if (vertices) return vertices;
+    if (vertices) return vertices.get();
 
     int8_t emptyQuads = 0;
     mColors = patch->getColors();
@@ -77,8 +73,8 @@
     uint32_t maxVertices = ((xCount + 1) * (yCount + 1) - emptyQuads) * 4;
     if (maxVertices == 0) return NULL;
 
-    TextureVertex* tempVertices = new TextureVertex[maxVertices];
-    TextureVertex* vertex = tempVertices;
+    vertices.reset(new TextureVertex[maxVertices]);
+    TextureVertex* vertex = vertices.get();
 
     const int32_t* xDivs = patch->getXDivs();
     const int32_t* yDivs = patch->getYDivs();
@@ -157,15 +153,13 @@
                 width, bitmapWidth, quadCount);
     }
 
-    if (verticesCount == maxVertices) {
-        vertices = tempVertices;
-    } else {
-        vertices = new TextureVertex[verticesCount];
-        memcpy(vertices, tempVertices, verticesCount * sizeof(TextureVertex));
-        delete[] tempVertices;
+    if (verticesCount != maxVertices) {
+        std::unique_ptr<TextureVertex[]> reducedVertices(new TextureVertex[verticesCount]);
+        memcpy(reducedVertices.get(), vertices.get(), verticesCount * sizeof(TextureVertex));
+        vertices = std::move(reducedVertices);
     }
 
-    return vertices;
+    return vertices.get();
 }
 
 void Patch::generateRow(const int32_t* xDivs, uint32_t xCount, TextureVertex*& vertex,
diff --git a/libs/hwui/Patch.h b/libs/hwui/Patch.h
index 1af4bb7..ea8c8c2 100644
--- a/libs/hwui/Patch.h
+++ b/libs/hwui/Patch.h
@@ -40,14 +40,13 @@
 class Patch {
 public:
     Patch();
-    ~Patch();
 
     /**
      * Returns the size of this patch's mesh in bytes.
      */
     uint32_t getSize() const;
 
-    TextureVertex* vertices;
+    std::unique_ptr<TextureVertex[]> vertices;
     uint32_t verticesCount;
     uint32_t indexCount;
     bool hasEmptyQuads;
diff --git a/libs/hwui/PathTessellator.cpp b/libs/hwui/PathTessellator.cpp
index 9f7dd50..ceec4fc 100644
--- a/libs/hwui/PathTessellator.cpp
+++ b/libs/hwui/PathTessellator.cpp
@@ -229,7 +229,6 @@
                 current->x - totalOffset.x,
                 current->y - totalOffset.y);
 
-        last = current;
         current = next;
         lastNormal = nextNormal;
     }
@@ -372,7 +371,6 @@
                 current->y - totalOffset.y,
                 maxAlpha);
 
-        last = current;
         current = next;
         lastNormal = nextNormal;
     }
@@ -700,7 +698,6 @@
                 current->y - outerOffset.y,
                 0.0f);
 
-        last = current;
         current = next;
         lastNormal = nextNormal;
     }
diff --git a/libs/hwui/PixelBuffer.cpp b/libs/hwui/PixelBuffer.cpp
index 5b642b9..6c448f0 100644
--- a/libs/hwui/PixelBuffer.cpp
+++ b/libs/hwui/PixelBuffer.cpp
@@ -34,7 +34,6 @@
 class CpuPixelBuffer: public PixelBuffer {
 public:
     CpuPixelBuffer(GLenum format, uint32_t width, uint32_t height);
-    ~CpuPixelBuffer();
 
     uint8_t* map(AccessMode mode = kAccessMode_ReadWrite);
     void unmap();
@@ -44,23 +43,19 @@
     void upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height, int offset);
 
 private:
-    uint8_t* mBuffer;
+    std::unique_ptr<uint8_t[]> mBuffer;
 };
 
-CpuPixelBuffer::CpuPixelBuffer(GLenum format, uint32_t width, uint32_t height):
-        PixelBuffer(format, width, height) {
-    mBuffer = new uint8_t[width * height * formatSize(format)];
-}
-
-CpuPixelBuffer::~CpuPixelBuffer() {
-    delete[] mBuffer;
+CpuPixelBuffer::CpuPixelBuffer(GLenum format, uint32_t width, uint32_t height)
+        : PixelBuffer(format, width, height)
+        , mBuffer(new uint8_t[width * height * formatSize(format)]) {
 }
 
 uint8_t* CpuPixelBuffer::map(AccessMode mode) {
     if (mAccessMode == kAccessMode_None) {
         mAccessMode = mode;
     }
-    return mBuffer;
+    return mBuffer.get();
 }
 
 void CpuPixelBuffer::unmap() {
@@ -68,12 +63,12 @@
 }
 
 uint8_t* CpuPixelBuffer::getMappedPointer() const {
-    return mAccessMode == kAccessMode_None ? NULL : mBuffer;
+    return mAccessMode == kAccessMode_None ? nullptr : mBuffer.get();
 }
 
 void CpuPixelBuffer::upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height, int offset) {
     glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height,
-            mFormat, GL_UNSIGNED_BYTE, mBuffer + offset);
+            mFormat, GL_UNSIGNED_BYTE, &mBuffer[offset]);
 }
 
 ///////////////////////////////////////////////////////////////////////////////
diff --git a/libs/hwui/ProgramCache.cpp b/libs/hwui/ProgramCache.cpp
index 62835e0..6d3f0cb 100644
--- a/libs/hwui/ProgramCache.cpp
+++ b/libs/hwui/ProgramCache.cpp
@@ -417,11 +417,6 @@
 
 void ProgramCache::clear() {
     PROGRAM_LOGD("Clearing program cache");
-
-    size_t count = mCache.size();
-    for (size_t i = 0; i < count; i++) {
-        delete mCache.valueAt(i);
-    }
     mCache.clear();
 }
 
@@ -433,14 +428,14 @@
         key = PROGRAM_KEY_TEXTURE;
     }
 
-    ssize_t index = mCache.indexOfKey(key);
+    auto iter = mCache.find(key);
     Program* program = NULL;
-    if (index < 0) {
+    if (iter == mCache.end()) {
         description.log("Could not find program");
         program = generateProgram(description, key);
-        mCache.add(key, program);
+        mCache[key] = std::unique_ptr<Program>(program);
     } else {
-        program = mCache.valueAt(index);
+        program = iter->second.get();
     }
     return program;
 }
diff --git a/libs/hwui/ProgramCache.h b/libs/hwui/ProgramCache.h
index 9aadba6..30fa0df 100644
--- a/libs/hwui/ProgramCache.h
+++ b/libs/hwui/ProgramCache.h
@@ -20,6 +20,7 @@
 #include <utils/KeyedVector.h>
 #include <utils/Log.h>
 #include <utils/String8.h>
+#include <map>
 
 #include <GLES2/gl2.h>
 
@@ -55,7 +56,7 @@
 
     void printLongString(const String8& shader) const;
 
-    KeyedVector<programid, Program*> mCache;
+    std::map<programid, std::unique_ptr<Program>> mCache;
 
     const bool mHasES3;
 }; // class ProgramCache
diff --git a/libs/hwui/RenderNode.cpp b/libs/hwui/RenderNode.cpp
index 7931b35..ad38a12 100644
--- a/libs/hwui/RenderNode.cpp
+++ b/libs/hwui/RenderNode.cpp
@@ -870,7 +870,7 @@
         return;
     }
 
-    const bool drawLayer = (mLayer && (&renderer != mLayer->renderer));
+    const bool drawLayer = (mLayer && (&renderer != mLayer->renderer.get()));
     // If we are updating the contents of mLayer, we don't want to apply any of
     // the RenderNode's properties to this issueOperations pass. Those will all
     // be applied when the layer is drawn, aka when this is true.
diff --git a/libs/hwui/Vertex.h b/libs/hwui/Vertex.h
index 4ff0b18..fe8a752 100644
--- a/libs/hwui/Vertex.h
+++ b/libs/hwui/Vertex.h
@@ -39,8 +39,8 @@
     float x, y;
 
     static inline void set(Vertex* vertex, float x, float y) {
-        vertex[0].x = x;
-        vertex[0].y = y;
+        vertex->x = x;
+        vertex->y = y;
     }
 
     static inline void set(Vertex* vertex, Vector2 val) {
diff --git a/libs/hwui/renderthread/CanvasContext.cpp b/libs/hwui/renderthread/CanvasContext.cpp
index 5cd5f69..0e54169 100644
--- a/libs/hwui/renderthread/CanvasContext.cpp
+++ b/libs/hwui/renderthread/CanvasContext.cpp
@@ -47,14 +47,13 @@
         , mOpaque(!translucent)
         , mCanvas(NULL)
         , mHaveNewSurface(false)
+        , mAnimationContext(contextFactory->createAnimationContext(mRenderThread.timeLord()))
         , mRootRenderNode(rootRenderNode) {
-    mAnimationContext = contextFactory->createAnimationContext(mRenderThread.timeLord());
     mRenderThread.renderState().registerCanvasContext(this);
 }
 
 CanvasContext::~CanvasContext() {
     destroy();
-    delete mAnimationContext;
     mRenderThread.renderState().unregisterCanvasContext(this);
 }
 
diff --git a/libs/hwui/renderthread/CanvasContext.h b/libs/hwui/renderthread/CanvasContext.h
index 0cc2c7c..d7f2ebd 100644
--- a/libs/hwui/renderthread/CanvasContext.h
+++ b/libs/hwui/renderthread/CanvasContext.h
@@ -128,7 +128,7 @@
     OpenGLRenderer* mCanvas;
     bool mHaveNewSurface;
     DamageAccumulator mDamageAccumulator;
-    AnimationContext* mAnimationContext;
+    std::unique_ptr<AnimationContext> mAnimationContext;
 
     const sp<RenderNode> mRootRenderNode;
 
diff --git a/libs/hwui/tests/main.cpp b/libs/hwui/tests/main.cpp
index 64ee5f6..ee16991 100644
--- a/libs/hwui/tests/main.cpp
+++ b/libs/hwui/tests/main.cpp
@@ -83,7 +83,7 @@
     rootNode->setPropertyFieldsDirty(RenderNode::GENERIC);
 
     ContextFactory factory;
-    RenderProxy* proxy = new RenderProxy(false, rootNode, &factory);
+    std::unique_ptr<RenderProxy> proxy(new RenderProxy(false, rootNode, &factory));
     proxy->loadSystemProperties();
     proxy->initialize(surface);
     float lightX = width / 2.0;
@@ -123,7 +123,6 @@
 
     sleep(5);
 
-    delete proxy;
     rootNode->decStrong(0);
 
     printf("Success!\n");